blob: 94c85fedcd40548b6f69caf62c311c809c882a1f [file] [log] [blame]
Patrick Delaunaya6f03912019-07-05 17:20:14 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
Etienne Carrieree6341132020-06-05 09:24:29 +02003 * Copyright (C) 2019-2020, STMicroelectronics - All Rights Reserved
Patrick Delaunaya6f03912019-07-05 17:20:14 +02004 */
5
Patrick Delaunayba779402020-11-06 19:01:29 +01006#define LOG_CATEGORY LOGC_ARCH
7
Patrick Delaunaya6f03912019-07-05 17:20:14 +02008#include <common.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <fdtdec.h>
Patrick Delaunaya6f03912019-07-05 17:20:14 +020010#include <fdt_support.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Patrick Delaunay472407a2020-03-18 09:22:49 +010012#include <tee.h>
Patrick Delaunaya6f03912019-07-05 17:20:14 +020013#include <asm/arch/sys_proto.h>
14#include <dt-bindings/pinctrl/stm32-pinfunc.h>
Patrick Delaunay43f214c2019-07-05 17:20:15 +020015#include <linux/io.h>
16
17#define ETZPC_DECPROT(n) (STM32_ETZPC_BASE + 0x10 + 4 * (n))
18#define ETZPC_DECPROT_NB 6
19
20#define DECPROT_MASK 0x03
21#define NB_PROT_PER_REG 0x10
22#define DECPROT_NB_BITS 2
23
24#define DECPROT_SECURED 0x00
25#define DECPROT_WRITE_SECURE 0x01
26#define DECPROT_MCU_ISOLATION 0x02
27#define DECPROT_NON_SECURED 0x03
28
29#define ETZPC_RESERVED 0xffffffff
30
Patrick Delaunaye65e80e2022-05-09 17:13:21 +020031#define STM32MP15_FDCAN_BASE 0x4400e000
32#define STM32MP15_CRYP2_BASE 0x4c005000
33#define STM32MP15_CRYP1_BASE 0x54001000
34#define STM32MP15_GPU_BASE 0x59000000
35#define STM32MP15_DSI_BASE 0x5a000000
Patrick Delaunayfac5ba82020-02-12 19:37:44 +010036
Patrick Delaunaye65e80e2022-05-09 17:13:21 +020037static const u32 stm32mp15_ip_addr[] = {
Patrick Delaunay43f214c2019-07-05 17:20:15 +020038 0x5c008000, /* 00 stgenc */
39 0x54000000, /* 01 bkpsram */
40 0x5c003000, /* 02 iwdg1 */
41 0x5c000000, /* 03 usart1 */
42 0x5c001000, /* 04 spi6 */
43 0x5c002000, /* 05 i2c4 */
44 ETZPC_RESERVED, /* 06 reserved */
45 0x54003000, /* 07 rng1 */
46 0x54002000, /* 08 hash1 */
Patrick Delaunaye65e80e2022-05-09 17:13:21 +020047 STM32MP15_CRYP1_BASE, /* 09 cryp1 */
Patrick Delaunay43f214c2019-07-05 17:20:15 +020048 0x5a003000, /* 0A ddrctrl */
49 0x5a004000, /* 0B ddrphyc */
50 0x5c009000, /* 0C i2c6 */
51 ETZPC_RESERVED, /* 0D reserved */
52 ETZPC_RESERVED, /* 0E reserved */
53 ETZPC_RESERVED, /* 0F reserved */
54 0x40000000, /* 10 tim2 */
55 0x40001000, /* 11 tim3 */
56 0x40002000, /* 12 tim4 */
57 0x40003000, /* 13 tim5 */
58 0x40004000, /* 14 tim6 */
59 0x40005000, /* 15 tim7 */
60 0x40006000, /* 16 tim12 */
61 0x40007000, /* 17 tim13 */
62 0x40008000, /* 18 tim14 */
63 0x40009000, /* 19 lptim1 */
64 0x4000a000, /* 1A wwdg1 */
65 0x4000b000, /* 1B spi2 */
66 0x4000c000, /* 1C spi3 */
67 0x4000d000, /* 1D spdifrx */
68 0x4000e000, /* 1E usart2 */
69 0x4000f000, /* 1F usart3 */
70 0x40010000, /* 20 uart4 */
71 0x40011000, /* 21 uart5 */
72 0x40012000, /* 22 i2c1 */
73 0x40013000, /* 23 i2c2 */
74 0x40014000, /* 24 i2c3 */
75 0x40015000, /* 25 i2c5 */
76 0x40016000, /* 26 cec */
77 0x40017000, /* 27 dac */
78 0x40018000, /* 28 uart7 */
79 0x40019000, /* 29 uart8 */
80 ETZPC_RESERVED, /* 2A reserved */
81 ETZPC_RESERVED, /* 2B reserved */
82 0x4001c000, /* 2C mdios */
83 ETZPC_RESERVED, /* 2D reserved */
84 ETZPC_RESERVED, /* 2E reserved */
85 ETZPC_RESERVED, /* 2F reserved */
86 0x44000000, /* 30 tim1 */
87 0x44001000, /* 31 tim8 */
88 ETZPC_RESERVED, /* 32 reserved */
89 0x44003000, /* 33 usart6 */
90 0x44004000, /* 34 spi1 */
91 0x44005000, /* 35 spi4 */
92 0x44006000, /* 36 tim15 */
93 0x44007000, /* 37 tim16 */
94 0x44008000, /* 38 tim17 */
95 0x44009000, /* 39 spi5 */
96 0x4400a000, /* 3A sai1 */
97 0x4400b000, /* 3B sai2 */
98 0x4400c000, /* 3C sai3 */
99 0x4400d000, /* 3D dfsdm */
Patrick Delaunaye65e80e2022-05-09 17:13:21 +0200100 STM32MP15_FDCAN_BASE, /* 3E tt_fdcan */
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200101 ETZPC_RESERVED, /* 3F reserved */
102 0x50021000, /* 40 lptim2 */
103 0x50022000, /* 41 lptim3 */
104 0x50023000, /* 42 lptim4 */
105 0x50024000, /* 43 lptim5 */
106 0x50027000, /* 44 sai4 */
107 0x50025000, /* 45 vrefbuf */
108 0x4c006000, /* 46 dcmi */
109 0x4c004000, /* 47 crc2 */
110 0x48003000, /* 48 adc */
111 0x4c002000, /* 49 hash2 */
112 0x4c003000, /* 4A rng2 */
Patrick Delaunaye65e80e2022-05-09 17:13:21 +0200113 STM32MP15_CRYP2_BASE, /* 4B cryp2 */
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200114 ETZPC_RESERVED, /* 4C reserved */
115 ETZPC_RESERVED, /* 4D reserved */
116 ETZPC_RESERVED, /* 4E reserved */
117 ETZPC_RESERVED, /* 4F reserved */
118 ETZPC_RESERVED, /* 50 sram1 */
119 ETZPC_RESERVED, /* 51 sram2 */
120 ETZPC_RESERVED, /* 52 sram3 */
121 ETZPC_RESERVED, /* 53 sram4 */
122 ETZPC_RESERVED, /* 54 retram */
123 0x49000000, /* 55 otg */
124 0x48004000, /* 56 sdmmc3 */
125 0x48005000, /* 57 dlybsd3 */
126 0x48000000, /* 58 dma1 */
127 0x48001000, /* 59 dma2 */
128 0x48002000, /* 5A dmamux */
129 0x58002000, /* 5B fmc */
130 0x58003000, /* 5C qspi */
131 0x58004000, /* 5D dlybq */
132 0x5800a000, /* 5E eth */
133 ETZPC_RESERVED, /* 5F reserved */
134};
135
136/* fdt helper */
137static bool fdt_disable_subnode_by_address(void *fdt, int offset, u32 addr)
138{
139 int node;
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100140 fdt_addr_t regs;
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200141
142 for (node = fdt_first_subnode(fdt, offset);
143 node >= 0;
144 node = fdt_next_subnode(fdt, node)) {
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100145 regs = fdtdec_get_addr(fdt, node, "reg");
146 if (addr == regs) {
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200147 if (fdtdec_get_is_enabled(fdt, node)) {
148 fdt_status_disabled(fdt, node);
149
150 return true;
151 }
152 return false;
153 }
154 }
155
156 return false;
157}
158
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100159static int stm32_fdt_fixup_etzpc(void *fdt, int soc_node)
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200160{
161 const u32 *array;
162 int array_size, i;
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100163 int offset, shift;
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200164 u32 addr, status, decprot[ETZPC_DECPROT_NB];
165
Patrick Delaunaye65e80e2022-05-09 17:13:21 +0200166 if (IS_ENABLED(CONFIG_STM32MP13x))
167 return 0;
168
169 if (IS_ENABLED(CONFIG_STM32MP15x)) {
170 array = stm32mp15_ip_addr;
171 array_size = ARRAY_SIZE(stm32mp15_ip_addr);
172 }
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200173
174 for (i = 0; i < ETZPC_DECPROT_NB; i++)
175 decprot[i] = readl(ETZPC_DECPROT(i));
176
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200177 for (i = 0; i < array_size; i++) {
178 offset = i / NB_PROT_PER_REG;
179 shift = (i % NB_PROT_PER_REG) * DECPROT_NB_BITS;
180 status = (decprot[offset] >> shift) & DECPROT_MASK;
181 addr = array[i];
182
Patrick Delaunayba779402020-11-06 19:01:29 +0100183 log_debug("ETZPC: 0x%08x decprot %d=%d\n", addr, i, status);
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200184
185 if (addr == ETZPC_RESERVED ||
186 status == DECPROT_NON_SECURED)
187 continue;
188
189 if (fdt_disable_subnode_by_address(fdt, soc_node, addr))
Patrick Delaunayba779402020-11-06 19:01:29 +0100190 log_notice("ETZPC: 0x%08x node disabled, decprot %d=%d\n",
191 addr, i, status);
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200192 }
193
194 return 0;
195}
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200196
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100197/* deactivate all the cpu except core 0 */
198static void stm32_fdt_fixup_cpu(void *blob, char *name)
199{
200 int off;
201 u32 reg;
202
203 off = fdt_path_offset(blob, "/cpus");
204 if (off < 0) {
Patrick Delaunayba779402020-11-06 19:01:29 +0100205 log_warning("%s: couldn't find /cpus node\n", __func__);
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100206 return;
207 }
208
209 off = fdt_node_offset_by_prop_value(blob, -1, "device_type", "cpu", 4);
210 while (off != -FDT_ERR_NOTFOUND) {
211 reg = fdtdec_get_addr(blob, off, "reg");
212 if (reg != 0) {
213 fdt_del_node(blob, off);
Patrick Delaunayba779402020-11-06 19:01:29 +0100214 log_notice("FDT: cpu %d node remove for %s\n",
215 reg, name);
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100216 /* after delete we can't trust the offsets anymore */
217 off = -1;
218 }
219 off = fdt_node_offset_by_prop_value(blob, off,
220 "device_type", "cpu", 4);
221 }
222}
223
224static void stm32_fdt_disable(void *fdt, int offset, u32 addr,
225 const char *string, const char *name)
226{
227 if (fdt_disable_subnode_by_address(fdt, offset, addr))
Patrick Delaunayba779402020-11-06 19:01:29 +0100228 log_notice("FDT: %s@%08x node disabled for %s\n",
229 string, addr, name);
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100230}
231
Patrick Delaunayd1633b32020-03-18 09:22:48 +0100232static void stm32_fdt_disable_optee(void *blob)
233{
234 int off, node;
235
Etienne Carrieree6341132020-06-05 09:24:29 +0200236 /* Delete "optee" firmware node */
Patrick Delaunayd1633b32020-03-18 09:22:48 +0100237 off = fdt_node_offset_by_compatible(blob, -1, "linaro,optee-tz");
238 if (off >= 0 && fdtdec_get_is_enabled(blob, off))
Etienne Carrieree6341132020-06-05 09:24:29 +0200239 fdt_del_node(blob, off);
Patrick Delaunayd1633b32020-03-18 09:22:48 +0100240
Etienne Carrieree6341132020-06-05 09:24:29 +0200241 /* Delete "optee@..." reserved-memory node */
Patrick Delaunayd1633b32020-03-18 09:22:48 +0100242 off = fdt_path_offset(blob, "/reserved-memory/");
243 if (off < 0)
244 return;
245 for (node = fdt_first_subnode(blob, off);
246 node >= 0;
247 node = fdt_next_subnode(blob, node)) {
Etienne Carrieree6341132020-06-05 09:24:29 +0200248 if (strncmp(fdt_get_name(blob, node, NULL), "optee@", 6))
249 continue;
250
251 if (fdt_del_node(blob, node))
252 printf("Failed to remove optee reserved-memory node\n");
Patrick Delaunayd1633b32020-03-18 09:22:48 +0100253 }
254}
255
Patrick Delaunaye65e80e2022-05-09 17:13:21 +0200256static void stm32mp15_fdt_fixup(void *blob, int soc, u32 cpu, char *name)
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200257{
Patrick Delaunaye65e80e2022-05-09 17:13:21 +0200258 u32 pkg;
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100259
260 switch (cpu) {
Patrick Delaunaydb33b0e2020-02-26 11:26:43 +0100261 case CPU_STM32MP151Fxx:
262 case CPU_STM32MP151Dxx:
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100263 case CPU_STM32MP151Cxx:
264 case CPU_STM32MP151Axx:
265 stm32_fdt_fixup_cpu(blob, name);
266 /* after cpu delete we can't trust the soc offsets anymore */
267 soc = fdt_path_offset(blob, "/soc");
Patrick Delaunaye65e80e2022-05-09 17:13:21 +0200268 stm32_fdt_disable(blob, soc, STM32MP15_FDCAN_BASE, "can", name);
269 fallthrough;
Patrick Delaunaydb33b0e2020-02-26 11:26:43 +0100270 case CPU_STM32MP153Fxx:
271 case CPU_STM32MP153Dxx:
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100272 case CPU_STM32MP153Cxx:
273 case CPU_STM32MP153Axx:
Patrick Delaunaye65e80e2022-05-09 17:13:21 +0200274 stm32_fdt_disable(blob, soc, STM32MP15_GPU_BASE, "gpu", name);
275 stm32_fdt_disable(blob, soc, STM32MP15_DSI_BASE, "dsi", name);
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100276 break;
277 default:
278 break;
279 }
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100280 switch (cpu) {
Patrick Delaunaydb33b0e2020-02-26 11:26:43 +0100281 case CPU_STM32MP157Dxx:
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100282 case CPU_STM32MP157Axx:
Patrick Delaunaydb33b0e2020-02-26 11:26:43 +0100283 case CPU_STM32MP153Dxx:
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100284 case CPU_STM32MP153Axx:
Patrick Delaunaydb33b0e2020-02-26 11:26:43 +0100285 case CPU_STM32MP151Dxx:
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100286 case CPU_STM32MP151Axx:
Patrick Delaunaye65e80e2022-05-09 17:13:21 +0200287 stm32_fdt_disable(blob, soc, STM32MP15_CRYP1_BASE, "cryp",
288 name);
289 stm32_fdt_disable(blob, soc, STM32MP15_CRYP2_BASE, "cryp",
290 name);
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100291 break;
292 default:
293 break;
294 }
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200295 switch (get_cpu_package()) {
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200296 case STM32MP15_PKG_AA_LBGA448:
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200297 pkg = STM32MP_PKG_AA;
298 break;
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200299 case STM32MP15_PKG_AB_LBGA354:
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200300 pkg = STM32MP_PKG_AB;
301 break;
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200302 case STM32MP15_PKG_AC_TFBGA361:
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200303 pkg = STM32MP_PKG_AC;
304 break;
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200305 case STM32MP15_PKG_AD_TFBGA257:
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200306 pkg = STM32MP_PKG_AD;
307 break;
308 default:
309 pkg = 0;
310 break;
311 }
312 if (pkg) {
313 do_fixup_by_compat_u32(blob, "st,stm32mp157-pinctrl",
314 "st,package", pkg, false);
315 do_fixup_by_compat_u32(blob, "st,stm32mp157-z-pinctrl",
316 "st,package", pkg, false);
317 }
Patrick Delaunaye65e80e2022-05-09 17:13:21 +0200318}
319
320/*
321 * This function is called right before the kernel is booted. "blob" is the
322 * device tree that will be passed to the kernel.
323 */
324int ft_system_setup(void *blob, struct bd_info *bd)
325{
326 int ret = 0;
327 int soc;
328 u32 cpu;
329 char name[SOC_NAME_SIZE];
330
331 soc = fdt_path_offset(blob, "/soc");
332 /* when absent, nothing to do */
333 if (soc == -FDT_ERR_NOTFOUND)
334 return 0;
335 if (soc < 0)
336 return soc;
337
338 if (CONFIG_IS_ENABLED(STM32_ETZPC)) {
339 ret = stm32_fdt_fixup_etzpc(blob, soc);
340 if (ret)
341 return ret;
342 }
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200343
Patrick Delaunaye65e80e2022-05-09 17:13:21 +0200344 /* MPUs Part Numbers and name*/
345 cpu = get_cpu_type();
346 get_soc_name(name);
347
348 if (IS_ENABLED(CONFIG_STM32MP15x)) {
349 stm32mp15_fdt_fixup(blob, soc, cpu, name);
350
351 /*
352 * TEMP: remove OP-TEE nodes in kernel device tree
353 * copied from U-Boot device tree by optee_copy_fdt_nodes
354 * when OP-TEE is not detected (probe failed)
355 * these OP-TEE nodes are present in <board>-u-boot.dtsi
356 * under CONFIG_STM32MP15x_STM32IMAGE only for compatibility
357 * when FIP is not used by TF-A
358 */
359 if (CONFIG_IS_ENABLED(STM32MP15x_STM32IMAGE) &&
360 !tee_find_device(NULL, NULL, NULL, NULL))
361 stm32_fdt_disable_optee(blob);
362 }
Patrick Delaunayd1633b32020-03-18 09:22:48 +0100363
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200364 return ret;
365}