1 // SPDX-License-Identifier: GPL-2.0
3 * R-Car Generation 2 support
5 * Copyright (C) 2013 Renesas Solutions Corp.
6 * Copyright (C) 2013 Magnus Damm
7 * Copyright (C) 2014 Ulrich Hecht
10 #include <linux/clocksource.h>
11 #include <linux/device.h>
12 #include <linux/dma-contiguous.h>
14 #include <linux/kernel.h>
15 #include <linux/memblock.h>
17 #include <linux/of_clk.h>
18 #include <linux/of_fdt.h>
19 #include <linux/of_platform.h>
20 #include <linux/psci.h>
21 #include <asm/mach/arch.h>
22 #include <asm/secure_cntvoff.h>
24 #include "rcar-gen2.h"
26 static const struct of_device_id cpg_matches
[] __initconst
= {
27 { .compatible
= "renesas,r8a7743-cpg-mssr", .data
= "extal" },
28 { .compatible
= "renesas,r8a7744-cpg-mssr", .data
= "extal" },
29 { .compatible
= "renesas,r8a7790-cpg-mssr", .data
= "extal" },
30 { .compatible
= "renesas,r8a7791-cpg-mssr", .data
= "extal" },
31 { .compatible
= "renesas,r8a7793-cpg-mssr", .data
= "extal" },
35 static unsigned int __init
get_extal_freq(void)
37 const struct of_device_id
*match
;
38 struct device_node
*cpg
, *extal
;
42 cpg
= of_find_matching_node_and_match(NULL
, cpg_matches
, &match
);
47 idx
= of_property_match_string(cpg
, "clock-names", match
->data
);
48 extal
= of_parse_phandle(cpg
, "clocks", idx
);
53 of_property_read_u32(extal
, "clock-frequency", &freq
);
61 void __init
rcar_gen2_timer_init(void)
63 bool need_update
= true;
68 * If PSCI is available then most likely we are running on PSCI-enabled
69 * U-Boot which, we assume, has already taken care of resetting CNTVOFF
70 * and updating counter module before switching to non-secure mode
71 * and we don't need to.
73 #ifdef CONFIG_ARM_PSCI_FW
78 if (need_update
== false)
81 secure_cntvoff_init();
83 if (of_machine_is_compatible("renesas,r8a7745") ||
84 of_machine_is_compatible("renesas,r8a77470") ||
85 of_machine_is_compatible("renesas,r8a7792") ||
86 of_machine_is_compatible("renesas,r8a7794")) {
87 freq
= 260000000 / 8; /* ZS / 8 */
89 /* At Linux boot time the r8a7790 arch timer comes up
90 * with the counter disabled. Moreover, it may also report
91 * a potentially incorrect fixed 13 MHz frequency. To be
92 * correct these registers need to be updated to use the
93 * frequency EXTAL / 2.
95 freq
= get_extal_freq() / 2;
98 /* Remap "armgcnt address map" space */
99 base
= ioremap(0xe6080000, PAGE_SIZE
);
102 * Update the timer if it is either not running, or is not at the
103 * right frequency. The timer is only configurable in secure mode
104 * so this avoids an abort if the loader started the timer and
105 * entered the kernel in non-secure mode.
108 if ((ioread32(base
+ CNTCR
) & 1) == 0 ||
109 ioread32(base
+ CNTFID0
) != freq
) {
110 /* Update registers with correct frequency */
111 iowrite32(freq
, base
+ CNTFID0
);
112 asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq
));
114 /* make sure arch timer is started by setting bit 0 of CNTCR */
115 iowrite32(1, base
+ CNTCR
);
125 struct memory_reserve_config
{
130 static int __init
rcar_gen2_scan_mem(unsigned long node
, const char *uname
,
131 int depth
, void *data
)
133 const char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
134 const __be32
*reg
, *endp
;
136 struct memory_reserve_config
*mrc
= data
;
137 u64 lpae_start
= 1ULL << 32;
139 /* We are scanning "memory" nodes only */
140 if (type
== NULL
|| strcmp(type
, "memory"))
143 reg
= of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
145 reg
= of_get_flat_dt_prop(node
, "reg", &l
);
149 endp
= reg
+ (l
/ sizeof(__be32
));
150 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
153 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
154 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
156 if (base
>= lpae_start
)
159 if ((base
+ size
) >= lpae_start
)
160 size
= lpae_start
- base
;
162 if (size
< mrc
->reserved
)
165 if (base
< mrc
->base
)
168 /* keep the area at top near the 32-bit legacy limit */
169 mrc
->base
= base
+ size
- mrc
->reserved
;
170 mrc
->size
= mrc
->reserved
;
176 void __init
rcar_gen2_reserve(void)
178 struct memory_reserve_config mrc
;
180 /* reserve 256 MiB at the top of the physical legacy 32-bit space */
181 memset(&mrc
, 0, sizeof(mrc
));
182 mrc
.reserved
= SZ_256M
;
184 of_scan_flat_dt(rcar_gen2_scan_mem
, &mrc
);
185 #ifdef CONFIG_DMA_CMA
186 if (mrc
.size
&& memblock_is_region_memory(mrc
.base
, mrc
.size
)) {
187 static struct cma
*rcar_gen2_dma_contiguous
;
189 dma_contiguous_reserve_area(mrc
.size
, mrc
.base
, 0,
190 &rcar_gen2_dma_contiguous
, true);
195 static const char * const rcar_gen2_boards_compat_dt
[] __initconst
= {
204 DT_MACHINE_START(RCAR_GEN2_DT
, "Generic R-Car Gen2 (Flattened Device Tree)")
205 .init_late
= shmobile_init_late
,
206 .init_time
= rcar_gen2_timer_init
,
207 .reserve
= rcar_gen2_reserve
,
208 .dt_compat
= rcar_gen2_boards_compat_dt
,
211 static const char * const rz_g1_boards_compat_dt
[] __initconst
= {
219 DT_MACHINE_START(RZ_G1_DT
, "Generic RZ/G1 (Flattened Device Tree)")
220 .init_late
= shmobile_init_late
,
221 .init_time
= rcar_gen2_timer_init
,
222 .reserve
= rcar_gen2_reserve
,
223 .dt_compat
= rz_g1_boards_compat_dt
,