blob: eea60b20c6b4605fe15f225388e66f5a209847f5 [file] [log] [blame]
Wolfram Sangc44e1822018-06-14 10:56:06 +09001// SPDX-License-Identifier: GPL-2.0
Magnus Damm50c517d2013-09-12 09:32:49 +09002/*
3 * R-Car Generation 2 support
4 *
5 * Copyright (C) 2013 Renesas Solutions Corp.
6 * Copyright (C) 2013 Magnus Damm
Ulrich Hecht9ce3fa62014-09-12 10:52:05 +02007 * Copyright (C) 2014 Ulrich Hecht
Magnus Damm50c517d2013-09-12 09:32:49 +09008 */
9
Geert Uytterhoeven80951f02015-07-07 15:06:34 +020010#include <linux/clk-provider.h>
Magnus Damm50c517d2013-09-12 09:32:49 +090011#include <linux/clocksource.h>
Magnus Dammf8e81932014-06-09 21:38:45 +090012#include <linux/device.h>
13#include <linux/dma-contiguous.h>
Magnus Damm50c517d2013-09-12 09:32:49 +090014#include <linux/io.h>
15#include <linux/kernel.h>
Magnus Damm744cadb2015-01-21 14:01:37 +090016#include <linux/memblock.h>
Ulrich Hecht9ce3fa62014-09-12 10:52:05 +020017#include <linux/of.h>
Magnus Dammf8e81932014-06-09 21:38:45 +090018#include <linux/of_fdt.h>
Laurent Pincharta0c4e2c2016-10-17 16:59:04 +030019#include <linux/of_platform.h>
Magnus Damm50c517d2013-09-12 09:32:49 +090020#include <asm/mach/arch.h>
Mylène Josserandcad160e2018-05-04 21:05:45 +020021#include <asm/secure_cntvoff.h>
Magnus Dammfd44aa52014-06-17 16:47:37 +090022#include "common.h"
Magnus Damm62872982014-06-17 16:48:01 +090023#include "rcar-gen2.h"
Magnus Damm50c517d2013-09-12 09:32:49 +090024
Geert Uytterhoevencdcdfaa2017-07-12 12:39:32 +020025static const struct of_device_id cpg_matches[] __initconst = {
26 { .compatible = "renesas,rcar-gen2-cpg-clocks", },
27 { .compatible = "renesas,r8a7743-cpg-mssr", .data = "extal" },
Biju Dasbc887152018-09-11 11:12:50 +010028 { .compatible = "renesas,r8a7744-cpg-mssr", .data = "extal" },
Geert Uytterhoevencdcdfaa2017-07-12 12:39:32 +020029 { .compatible = "renesas,r8a7790-cpg-mssr", .data = "extal" },
30 { .compatible = "renesas,r8a7791-cpg-mssr", .data = "extal" },
31 { .compatible = "renesas,r8a7793-cpg-mssr", .data = "extal" },
32 { /* sentinel */ }
33};
34
Geert Uytterhoeven9f5ce392016-06-13 12:29:37 +020035static unsigned int __init get_extal_freq(void)
36{
Geert Uytterhoevencdcdfaa2017-07-12 12:39:32 +020037 const struct of_device_id *match;
Geert Uytterhoeven9f5ce392016-06-13 12:29:37 +020038 struct device_node *cpg, *extal;
39 u32 freq = 20000000;
Geert Uytterhoevencdcdfaa2017-07-12 12:39:32 +020040 int idx = 0;
Geert Uytterhoeven9f5ce392016-06-13 12:29:37 +020041
Geert Uytterhoevencdcdfaa2017-07-12 12:39:32 +020042 cpg = of_find_matching_node_and_match(NULL, cpg_matches, &match);
Geert Uytterhoeven9f5ce392016-06-13 12:29:37 +020043 if (!cpg)
44 return freq;
45
Geert Uytterhoevencdcdfaa2017-07-12 12:39:32 +020046 if (match->data)
47 idx = of_property_match_string(cpg, "clock-names", match->data);
48 extal = of_parse_phandle(cpg, "clocks", idx);
Geert Uytterhoeven9f5ce392016-06-13 12:29:37 +020049 of_node_put(cpg);
50 if (!extal)
51 return freq;
52
53 of_property_read_u32(extal, "clock-frequency", &freq);
54 of_node_put(extal);
55 return freq;
56}
57
Magnus Damm50c517d2013-09-12 09:32:49 +090058#define CNTCR 0
59#define CNTFID0 0x20
60
61void __init rcar_gen2_timer_init(void)
62{
Magnus Damm50c517d2013-09-12 09:32:49 +090063 void __iomem *base;
Magnus Damm50c517d2013-09-12 09:32:49 +090064 u32 freq;
65
Mylène Josserandcad160e2018-05-04 21:05:45 +020066 secure_cntvoff_init();
Geert Uytterhoeven3fd45a12017-09-01 10:37:45 +020067
Geert Uytterhoevencd66fa42017-07-18 15:30:15 +020068 if (of_machine_is_compatible("renesas,r8a7745") ||
Biju Das0c1d5432018-03-28 20:26:13 +010069 of_machine_is_compatible("renesas,r8a77470") ||
Geert Uytterhoevencd66fa42017-07-18 15:30:15 +020070 of_machine_is_compatible("renesas,r8a7792") ||
Geert Uytterhoeven2477a352016-06-13 12:31:12 +020071 of_machine_is_compatible("renesas,r8a7794")) {
Ulrich Hecht9ce3fa62014-09-12 10:52:05 +020072 freq = 260000000 / 8; /* ZS / 8 */
Ulrich Hecht9ce3fa62014-09-12 10:52:05 +020073 } else {
74 /* At Linux boot time the r8a7790 arch timer comes up
75 * with the counter disabled. Moreover, it may also report
76 * a potentially incorrect fixed 13 MHz frequency. To be
77 * correct these registers need to be updated to use the
Geert Uytterhoeven9f5ce392016-06-13 12:29:37 +020078 * frequency EXTAL / 2.
Ulrich Hecht9ce3fa62014-09-12 10:52:05 +020079 */
Geert Uytterhoeven9f5ce392016-06-13 12:29:37 +020080 freq = get_extal_freq() / 2;
Magnus Damm50c517d2013-09-12 09:32:49 +090081 }
82
Magnus Damm50c517d2013-09-12 09:32:49 +090083 /* Remap "armgcnt address map" space */
84 base = ioremap(0xe6080000, PAGE_SIZE);
85
Ben Dooks0fe35072013-12-11 10:07:42 +000086 /*
87 * Update the timer if it is either not running, or is not at the
88 * right frequency. The timer is only configurable in secure mode
89 * so this avoids an abort if the loader started the timer and
90 * entered the kernel in non-secure mode.
91 */
Magnus Damm50c517d2013-09-12 09:32:49 +090092
Ben Dooks0fe35072013-12-11 10:07:42 +000093 if ((ioread32(base + CNTCR) & 1) == 0 ||
94 ioread32(base + CNTFID0) != freq) {
95 /* Update registers with correct frequency */
96 iowrite32(freq, base + CNTFID0);
97 asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
98
99 /* make sure arch timer is started by setting bit 0 of CNTCR */
100 iowrite32(1, base + CNTCR);
101 }
102
Magnus Damm50c517d2013-09-12 09:32:49 +0900103 iounmap(base);
Magnus Damm50c517d2013-09-12 09:32:49 +0900104
Geert Uytterhoeven80951f02015-07-07 15:06:34 +0200105 of_clk_init(NULL);
Daniel Lezcanoba5d08c2017-05-26 17:40:46 +0200106 timer_probe();
Magnus Damm50c517d2013-09-12 09:32:49 +0900107}
Geert Uytterhoeven83850b02014-06-12 10:42:22 +0200108
109struct memory_reserve_config {
110 u64 reserved;
111 u64 base, size;
112};
113
114static int __init rcar_gen2_scan_mem(unsigned long node, const char *uname,
115 int depth, void *data)
116{
117 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
118 const __be32 *reg, *endp;
119 int l;
120 struct memory_reserve_config *mrc = data;
Geert Uytterhoevenb69f47c2014-06-12 10:42:23 +0200121 u64 lpae_start = 1ULL << 32;
Geert Uytterhoeven83850b02014-06-12 10:42:22 +0200122
123 /* We are scanning "memory" nodes only */
Geert Uytterhoevenea2a0d582014-06-12 10:42:24 +0200124 if (type == NULL || strcmp(type, "memory"))
Geert Uytterhoeven83850b02014-06-12 10:42:22 +0200125 return 0;
126
127 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
128 if (reg == NULL)
129 reg = of_get_flat_dt_prop(node, "reg", &l);
130 if (reg == NULL)
131 return 0;
132
133 endp = reg + (l / sizeof(__be32));
134 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
135 u64 base, size;
136
137 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
138 size = dt_mem_next_cell(dt_root_size_cells, &reg);
139
140 if (base >= lpae_start)
141 continue;
142
143 if ((base + size) >= lpae_start)
144 size = lpae_start - base;
145
146 if (size < mrc->reserved)
147 continue;
148
149 if (base < mrc->base)
150 continue;
151
152 /* keep the area at top near the 32-bit legacy limit */
153 mrc->base = base + size - mrc->reserved;
154 mrc->size = mrc->reserved;
155 }
156
157 return 0;
158}
159
Geert Uytterhoeven83850b02014-06-12 10:42:22 +0200160void __init rcar_gen2_reserve(void)
161{
162 struct memory_reserve_config mrc;
163
164 /* reserve 256 MiB at the top of the physical legacy 32-bit space */
165 memset(&mrc, 0, sizeof(mrc));
166 mrc.reserved = SZ_256M;
167
168 of_scan_flat_dt(rcar_gen2_scan_mem, &mrc);
169#ifdef CONFIG_DMA_CMA
Geert Uytterhoeven0c169072016-01-28 16:20:47 +0100170 if (mrc.size && memblock_is_region_memory(mrc.base, mrc.size)) {
171 static struct cma *rcar_gen2_dma_contiguous;
172
Geert Uytterhoeven83850b02014-06-12 10:42:22 +0200173 dma_contiguous_reserve_area(mrc.size, mrc.base, 0,
Vincent Stehlé14a5e922014-06-19 11:31:10 +0200174 &rcar_gen2_dma_contiguous, true);
Geert Uytterhoeven0c169072016-01-28 16:20:47 +0100175 }
Geert Uytterhoeven83850b02014-06-12 10:42:22 +0200176#endif
177}
Laurent Pincharta0c4e2c2016-10-17 16:59:04 +0300178
179static const char * const rcar_gen2_boards_compat_dt[] __initconst = {
Geert Uytterhoeven78a90572018-05-18 14:48:51 +0200180 "renesas,r8a7790",
Geert Uytterhoeven1e90fea2018-05-18 14:48:53 +0200181 "renesas,r8a7791",
Laurent Pincharta0c4e2c2016-10-17 16:59:04 +0300182 "renesas,r8a7792",
183 "renesas,r8a7793",
184 "renesas,r8a7794",
185 NULL,
186};
187
188DT_MACHINE_START(RCAR_GEN2_DT, "Generic R-Car Gen2 (Flattened Device Tree)")
Laurent Pincharta0c4e2c2016-10-17 16:59:04 +0300189 .init_late = shmobile_init_late,
190 .init_time = rcar_gen2_timer_init,
191 .reserve = rcar_gen2_reserve,
192 .dt_compat = rcar_gen2_boards_compat_dt,
193MACHINE_END
194
195static const char * const rz_g1_boards_compat_dt[] __initconst = {
196 "renesas,r8a7743",
Biju Dasbc887152018-09-11 11:12:50 +0100197 "renesas,r8a7744",
Sergei Shtylyov47802fd2016-11-05 00:49:55 +0300198 "renesas,r8a7745",
Biju Das0c1d5432018-03-28 20:26:13 +0100199 "renesas,r8a77470",
Laurent Pincharta0c4e2c2016-10-17 16:59:04 +0300200 NULL,
201};
202
203DT_MACHINE_START(RZ_G1_DT, "Generic RZ/G1 (Flattened Device Tree)")
Laurent Pincharta0c4e2c2016-10-17 16:59:04 +0300204 .init_late = shmobile_init_late,
205 .init_time = rcar_gen2_timer_init,
206 .reserve = rcar_gen2_reserve,
207 .dt_compat = rz_g1_boards_compat_dt,
208MACHINE_END