setup-rcar-gen2.c 5.35 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6
/*
 * R-Car Generation 2 support
 *
 * Copyright (C) 2013  Renesas Solutions Corp.
 * Copyright (C) 2013  Magnus Damm
7
 * Copyright (C) 2014  Ulrich Hecht
8 9
 */

10
#include <linux/clk-provider.h>
11
#include <linux/clocksource.h>
12 13
#include <linux/device.h>
#include <linux/dma-contiguous.h>
14 15
#include <linux/io.h>
#include <linux/kernel.h>
16
#include <linux/memblock.h>
17
#include <linux/of.h>
18
#include <linux/of_fdt.h>
19
#include <linux/of_platform.h>
20
#include <asm/mach/arch.h>
21
#include <asm/secure_cntvoff.h>
Magnus Damm's avatar
Magnus Damm committed
22
#include "common.h"
23
#include "rcar-gen2.h"
24

25 26 27 28 29 30 31 32 33
static const struct of_device_id cpg_matches[] __initconst = {
	{ .compatible = "renesas,rcar-gen2-cpg-clocks", },
	{ .compatible = "renesas,r8a7743-cpg-mssr", .data = "extal" },
	{ .compatible = "renesas,r8a7790-cpg-mssr", .data = "extal" },
	{ .compatible = "renesas,r8a7791-cpg-mssr", .data = "extal" },
	{ .compatible = "renesas,r8a7793-cpg-mssr", .data = "extal" },
	{ /* sentinel */ }
};

34 35
static unsigned int __init get_extal_freq(void)
{
36
	const struct of_device_id *match;
37 38
	struct device_node *cpg, *extal;
	u32 freq = 20000000;
39
	int idx = 0;
40

41
	cpg = of_find_matching_node_and_match(NULL, cpg_matches, &match);
42 43 44
	if (!cpg)
		return freq;

45 46 47
	if (match->data)
		idx = of_property_match_string(cpg, "clock-names", match->data);
	extal = of_parse_phandle(cpg, "clocks", idx);
48 49 50 51 52 53 54 55 56
	of_node_put(cpg);
	if (!extal)
		return freq;

	of_property_read_u32(extal, "clock-frequency", &freq);
	of_node_put(extal);
	return freq;
}

57 58 59 60 61
#define CNTCR 0
#define CNTFID0 0x20

void __init rcar_gen2_timer_init(void)
{
62
#ifdef CONFIG_ARM_ARCH_TIMER
63 64 65
	void __iomem *base;
	u32 freq;

66
	secure_cntvoff_init();
67

68
	if (of_machine_is_compatible("renesas,r8a7745") ||
69
	    of_machine_is_compatible("renesas,r8a77470") ||
70
	    of_machine_is_compatible("renesas,r8a7792") ||
71
	    of_machine_is_compatible("renesas,r8a7794")) {
72 73 74 75 76 77
		freq = 260000000 / 8;	/* ZS / 8 */
	} else {
		/* At Linux boot time the r8a7790 arch timer comes up
		 * with the counter disabled. Moreover, it may also report
		 * a potentially incorrect fixed 13 MHz frequency. To be
		 * correct these registers need to be updated to use the
78
		 * frequency EXTAL / 2.
79
		 */
80
		freq = get_extal_freq() / 2;
81
	}
82 83 84 85

	/* Remap "armgcnt address map" space */
	base = ioremap(0xe6080000, PAGE_SIZE);

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	/*
	 * Update the timer if it is either not running, or is not at the
	 * right frequency. The timer is only configurable in secure mode
	 * so this avoids an abort if the loader started the timer and
	 * entered the kernel in non-secure mode.
	 */

	if ((ioread32(base + CNTCR) & 1) == 0 ||
	    ioread32(base + CNTFID0) != freq) {
		/* Update registers with correct frequency */
		iowrite32(freq, base + CNTFID0);
		asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));

		/* make sure arch timer is started by setting bit 0 of CNTCR */
		iowrite32(1, base + CNTCR);
	}
102 103 104 105

	iounmap(base);
#endif /* CONFIG_ARM_ARCH_TIMER */

106
	of_clk_init(NULL);
107
	timer_probe();
108
}
109 110 111 112 113 114 115 116 117 118 119 120 121

struct memory_reserve_config {
	u64 reserved;
	u64 base, size;
};

static int __init rcar_gen2_scan_mem(unsigned long node, const char *uname,
				     int depth, void *data)
{
	const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
	const __be32 *reg, *endp;
	int l;
	struct memory_reserve_config *mrc = data;
122
	u64 lpae_start = 1ULL << 32;
123 124

	/* We are scanning "memory" nodes only */
125
	if (type == NULL || strcmp(type, "memory"))
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
		return 0;

	reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
	if (reg == NULL)
		reg = of_get_flat_dt_prop(node, "reg", &l);
	if (reg == NULL)
		return 0;

	endp = reg + (l / sizeof(__be32));
	while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
		u64 base, size;

		base = dt_mem_next_cell(dt_root_addr_cells, &reg);
		size = dt_mem_next_cell(dt_root_size_cells, &reg);

		if (base >= lpae_start)
			continue;

		if ((base + size) >= lpae_start)
			size = lpae_start - base;

		if (size < mrc->reserved)
			continue;

		if (base < mrc->base)
			continue;

		/* keep the area at top near the 32-bit legacy limit */
		mrc->base = base + size - mrc->reserved;
		mrc->size = mrc->reserved;
	}

	return 0;
}

void __init rcar_gen2_reserve(void)
{
	struct memory_reserve_config mrc;

	/* reserve 256 MiB at the top of the physical legacy 32-bit space */
	memset(&mrc, 0, sizeof(mrc));
	mrc.reserved = SZ_256M;

	of_scan_flat_dt(rcar_gen2_scan_mem, &mrc);
#ifdef CONFIG_DMA_CMA
171 172 173
	if (mrc.size && memblock_is_region_memory(mrc.base, mrc.size)) {
		static struct cma *rcar_gen2_dma_contiguous;

174
		dma_contiguous_reserve_area(mrc.size, mrc.base, 0,
175
					    &rcar_gen2_dma_contiguous, true);
176
	}
177 178
#endif
}
179 180

static const char * const rcar_gen2_boards_compat_dt[] __initconst = {
181
	"renesas,r8a7790",
182
	"renesas,r8a7791",
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	"renesas,r8a7792",
	"renesas,r8a7793",
	"renesas,r8a7794",
	NULL,
};

DT_MACHINE_START(RCAR_GEN2_DT, "Generic R-Car Gen2 (Flattened Device Tree)")
	.init_early	= shmobile_init_delay,
	.init_late	= shmobile_init_late,
	.init_time	= rcar_gen2_timer_init,
	.reserve	= rcar_gen2_reserve,
	.dt_compat	= rcar_gen2_boards_compat_dt,
MACHINE_END

static const char * const rz_g1_boards_compat_dt[] __initconst = {
	"renesas,r8a7743",
199
	"renesas,r8a7745",
200
	"renesas,r8a77470",
201 202 203 204 205 206 207 208 209 210
	NULL,
};

DT_MACHINE_START(RZ_G1_DT, "Generic RZ/G1 (Flattened Device Tree)")
	.init_early	= shmobile_init_delay,
	.init_late	= shmobile_init_late,
	.init_time	= rcar_gen2_timer_init,
	.reserve	= rcar_gen2_reserve,
	.dt_compat	= rz_g1_boards_compat_dt,
MACHINE_END