]> git.ipfire.org Git - thirdparty/u-boot.git/blob - board/renesas/rcar-common/common.c
Merge tag v2023.07-rc4 into next
[thirdparty/u-boot.git] / board / renesas / rcar-common / common.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * board/renesas/rcar-common/common.c
4 *
5 * Copyright (C) 2013 Renesas Electronics Corporation
6 * Copyright (C) 2013 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
7 * Copyright (C) 2015 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
8 */
9
10 #include <common.h>
11 #include <dm.h>
12 #include <fdt_support.h>
13 #include <hang.h>
14 #include <init.h>
15 #include <asm/global_data.h>
16 #include <asm/io.h>
17 #include <dm/uclass-internal.h>
18 #include <asm/arch/rmobile.h>
19 #include <linux/libfdt.h>
20
21 #ifdef CONFIG_RCAR_64
22
23 DECLARE_GLOBAL_DATA_PTR;
24
25 /* If the firmware passed a device tree use it for e.g. U-Boot DRAM setup. */
26 extern u64 rcar_atf_boot_args[];
27
28 #define FDT_RPC_PATH "/soc/spi@ee200000"
29
30 static void apply_atf_overlay(void *fdt_blob)
31 {
32 void *atf_fdt_blob = (void *)(rcar_atf_boot_args[1]);
33
34 if (fdt_magic(atf_fdt_blob) == FDT_MAGIC)
35 fdt_overlay_apply_node(fdt_blob, 0, atf_fdt_blob, 0);
36 }
37
38 int fdtdec_board_setup(const void *fdt_blob)
39 {
40 apply_atf_overlay((void *)fdt_blob);
41
42 return 0;
43 }
44
45 int dram_init(void)
46 {
47 return fdtdec_setup_mem_size_base();
48 }
49
50 int dram_init_banksize(void)
51 {
52 fdtdec_setup_memory_banksize();
53
54 return 0;
55 }
56
57 int __weak board_init(void)
58 {
59 return 0;
60 }
61
62 #if defined(CONFIG_RCAR_GEN3)
63 #define RST_BASE 0xE6160000
64 #define RST_CA57RESCNT (RST_BASE + 0x40)
65 #define RST_CA53RESCNT (RST_BASE + 0x44)
66 #define RST_RSTOUTCR (RST_BASE + 0x58)
67 #define RST_CA57_CODE 0xA5A5000F
68 #define RST_CA53_CODE 0x5A5A000F
69
70 void __weak reset_cpu(void)
71 {
72 unsigned long midr, cputype;
73
74 asm volatile("mrs %0, midr_el1" : "=r" (midr));
75 cputype = (midr >> 4) & 0xfff;
76
77 if (cputype == 0xd03)
78 writel(RST_CA53_CODE, RST_CA53RESCNT);
79 else if (cputype == 0xd07)
80 writel(RST_CA57_CODE, RST_CA57RESCNT);
81 else
82 hang();
83 }
84 #elif defined(CONFIG_RCAR_GEN4)
85 #define RST_BASE 0xE6160000 /* Domain0 */
86 #define RST_SRESCR0 (RST_BASE + 0x18)
87 #define RST_SPRES 0x5AA58000
88
89 void __weak reset_cpu(void)
90 {
91 writel(RST_SPRES, RST_SRESCR0);
92 }
93 #else
94 #error Neither CONFIG_RCAR_GEN3 nor CONFIG_RCAR_GEN4 are set
95 #endif
96
97 #if defined(CONFIG_OF_BOARD_SETUP)
98 static int is_mem_overlap(void *blob, int first_mem_node, int curr_mem_node)
99 {
100 struct fdt_resource first_mem_res, curr_mem_res;
101 int curr_mem_reg, first_mem_reg = 0;
102 int ret;
103
104 for (;;) {
105 ret = fdt_get_resource(blob, first_mem_node, "reg",
106 first_mem_reg++, &first_mem_res);
107 if (ret) /* No more entries, no overlap found */
108 return 0;
109
110 curr_mem_reg = 0;
111 for (;;) {
112 ret = fdt_get_resource(blob, curr_mem_node, "reg",
113 curr_mem_reg++, &curr_mem_res);
114 if (ret) /* No more entries, check next tuple */
115 break;
116
117 if (curr_mem_res.end < first_mem_res.start)
118 continue;
119
120 if (curr_mem_res.start >= first_mem_res.end)
121 continue;
122
123 log_debug("Overlap found: 0x%llx..0x%llx / 0x%llx..0x%llx\n",
124 first_mem_res.start, first_mem_res.end,
125 curr_mem_res.start, curr_mem_res.end);
126
127 return 1;
128 }
129 }
130
131 return 0;
132 }
133
134 static void scrub_duplicate_memory(void *blob)
135 {
136 /*
137 * Scrub duplicate /memory@* node entries here. Some R-Car DTs might
138 * contain multiple /memory@* nodes, however fdt_fixup_memory_banks()
139 * either generates single /memory node or updates the first /memory
140 * node. Any remaining memory nodes are thus potential duplicates.
141 *
142 * However, it is not possible to delete all the memory nodes right
143 * away, since some of those might not be DRAM memory nodes, but some
144 * sort of other memory. Thus, delete only the memory nodes which are
145 * in the R-Car3 DBSC ranges.
146 */
147 int mem = 0, first_mem_node = 0;
148
149 for (;;) {
150 mem = fdt_node_offset_by_prop_value(blob, mem,
151 "device_type", "memory", 7);
152 if (mem < 0)
153 break;
154 if (!fdtdec_get_is_enabled(blob, mem))
155 continue;
156
157 /* First memory node, patched by U-Boot */
158 if (!first_mem_node) {
159 first_mem_node = mem;
160 continue;
161 }
162
163 /* Check the remaining nodes and delete duplicates */
164 if (!is_mem_overlap(blob, first_mem_node, mem))
165 continue;
166
167 /* Delete duplicate node, start again */
168 fdt_del_node(blob, mem);
169 first_mem_node = 0;
170 mem = 0;
171 }
172 }
173
174 static void update_rpc_status(void *blob)
175 {
176 void *atf_fdt_blob = (void *)(rcar_atf_boot_args[1]);
177 int offset, enabled;
178
179 /*
180 * Check if the DT fragment received from TF-A had its RPC-IF device node
181 * enabled.
182 */
183 if (fdt_magic(atf_fdt_blob) != FDT_MAGIC)
184 return;
185
186 offset = fdt_path_offset(atf_fdt_blob, FDT_RPC_PATH);
187 if (offset < 0)
188 return;
189
190 enabled = fdtdec_get_is_enabled(atf_fdt_blob, offset);
191 if (!enabled)
192 return;
193
194 /*
195 * Find the RPC-IF device node, and enable it if it has a flash subnode.
196 */
197 offset = fdt_path_offset(blob, FDT_RPC_PATH);
198 if (offset < 0)
199 return;
200
201 if (fdt_subnode_offset(blob, offset, "flash") < 0)
202 return;
203
204 fdt_status_okay(blob, offset);
205 }
206
207 int ft_board_setup(void *blob, struct bd_info *bd)
208 {
209 apply_atf_overlay(blob);
210 scrub_duplicate_memory(blob);
211 update_rpc_status(blob);
212
213 return 0;
214 }
215 #endif
216 #endif