1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
7 #include <fdt_support.h>
9 #include <asm/arch/sys_proto.h>
10 #include <dt-bindings/pinctrl/stm32-pinfunc.h>
13 #define ETZPC_DECPROT(n) (STM32_ETZPC_BASE + 0x10 + 4 * (n))
14 #define ETZPC_DECPROT_NB 6
16 #define DECPROT_MASK 0x03
17 #define NB_PROT_PER_REG 0x10
18 #define DECPROT_NB_BITS 2
20 #define DECPROT_SECURED 0x00
21 #define DECPROT_WRITE_SECURE 0x01
22 #define DECPROT_MCU_ISOLATION 0x02
23 #define DECPROT_NON_SECURED 0x03
25 #define ETZPC_RESERVED 0xffffffff
27 #define STM32_FDCAN_BASE 0x4400e000
28 #define STM32_CRYP2_BASE 0x4c005000
29 #define STM32_CRYP1_BASE 0x54001000
30 #define STM32_GPU_BASE 0x59000000
31 #define STM32_DSI_BASE 0x5a000000
33 static const u32 stm32mp1_ip_addr[] = {
34 0x5c008000, /* 00 stgenc */
35 0x54000000, /* 01 bkpsram */
36 0x5c003000, /* 02 iwdg1 */
37 0x5c000000, /* 03 usart1 */
38 0x5c001000, /* 04 spi6 */
39 0x5c002000, /* 05 i2c4 */
40 ETZPC_RESERVED, /* 06 reserved */
41 0x54003000, /* 07 rng1 */
42 0x54002000, /* 08 hash1 */
43 STM32_CRYP1_BASE, /* 09 cryp1 */
44 0x5a003000, /* 0A ddrctrl */
45 0x5a004000, /* 0B ddrphyc */
46 0x5c009000, /* 0C i2c6 */
47 ETZPC_RESERVED, /* 0D reserved */
48 ETZPC_RESERVED, /* 0E reserved */
49 ETZPC_RESERVED, /* 0F reserved */
50 0x40000000, /* 10 tim2 */
51 0x40001000, /* 11 tim3 */
52 0x40002000, /* 12 tim4 */
53 0x40003000, /* 13 tim5 */
54 0x40004000, /* 14 tim6 */
55 0x40005000, /* 15 tim7 */
56 0x40006000, /* 16 tim12 */
57 0x40007000, /* 17 tim13 */
58 0x40008000, /* 18 tim14 */
59 0x40009000, /* 19 lptim1 */
60 0x4000a000, /* 1A wwdg1 */
61 0x4000b000, /* 1B spi2 */
62 0x4000c000, /* 1C spi3 */
63 0x4000d000, /* 1D spdifrx */
64 0x4000e000, /* 1E usart2 */
65 0x4000f000, /* 1F usart3 */
66 0x40010000, /* 20 uart4 */
67 0x40011000, /* 21 uart5 */
68 0x40012000, /* 22 i2c1 */
69 0x40013000, /* 23 i2c2 */
70 0x40014000, /* 24 i2c3 */
71 0x40015000, /* 25 i2c5 */
72 0x40016000, /* 26 cec */
73 0x40017000, /* 27 dac */
74 0x40018000, /* 28 uart7 */
75 0x40019000, /* 29 uart8 */
76 ETZPC_RESERVED, /* 2A reserved */
77 ETZPC_RESERVED, /* 2B reserved */
78 0x4001c000, /* 2C mdios */
79 ETZPC_RESERVED, /* 2D reserved */
80 ETZPC_RESERVED, /* 2E reserved */
81 ETZPC_RESERVED, /* 2F reserved */
82 0x44000000, /* 30 tim1 */
83 0x44001000, /* 31 tim8 */
84 ETZPC_RESERVED, /* 32 reserved */
85 0x44003000, /* 33 usart6 */
86 0x44004000, /* 34 spi1 */
87 0x44005000, /* 35 spi4 */
88 0x44006000, /* 36 tim15 */
89 0x44007000, /* 37 tim16 */
90 0x44008000, /* 38 tim17 */
91 0x44009000, /* 39 spi5 */
92 0x4400a000, /* 3A sai1 */
93 0x4400b000, /* 3B sai2 */
94 0x4400c000, /* 3C sai3 */
95 0x4400d000, /* 3D dfsdm */
96 STM32_FDCAN_BASE, /* 3E tt_fdcan */
97 ETZPC_RESERVED, /* 3F reserved */
98 0x50021000, /* 40 lptim2 */
99 0x50022000, /* 41 lptim3 */
100 0x50023000, /* 42 lptim4 */
101 0x50024000, /* 43 lptim5 */
102 0x50027000, /* 44 sai4 */
103 0x50025000, /* 45 vrefbuf */
104 0x4c006000, /* 46 dcmi */
105 0x4c004000, /* 47 crc2 */
106 0x48003000, /* 48 adc */
107 0x4c002000, /* 49 hash2 */
108 0x4c003000, /* 4A rng2 */
109 STM32_CRYP2_BASE, /* 4B cryp2 */
110 ETZPC_RESERVED, /* 4C reserved */
111 ETZPC_RESERVED, /* 4D reserved */
112 ETZPC_RESERVED, /* 4E reserved */
113 ETZPC_RESERVED, /* 4F reserved */
114 ETZPC_RESERVED, /* 50 sram1 */
115 ETZPC_RESERVED, /* 51 sram2 */
116 ETZPC_RESERVED, /* 52 sram3 */
117 ETZPC_RESERVED, /* 53 sram4 */
118 ETZPC_RESERVED, /* 54 retram */
119 0x49000000, /* 55 otg */
120 0x48004000, /* 56 sdmmc3 */
121 0x48005000, /* 57 dlybsd3 */
122 0x48000000, /* 58 dma1 */
123 0x48001000, /* 59 dma2 */
124 0x48002000, /* 5A dmamux */
125 0x58002000, /* 5B fmc */
126 0x58003000, /* 5C qspi */
127 0x58004000, /* 5D dlybq */
128 0x5800a000, /* 5E eth */
129 ETZPC_RESERVED, /* 5F reserved */
133 static bool fdt_disable_subnode_by_address(void *fdt, int offset, u32 addr)
138 for (node = fdt_first_subnode(fdt, offset);
140 node = fdt_next_subnode(fdt, node)) {
141 regs = fdtdec_get_addr(fdt, node, "reg");
143 if (fdtdec_get_is_enabled(fdt, node)) {
144 fdt_status_disabled(fdt, node);
155 static int stm32_fdt_fixup_etzpc(void *fdt, int soc_node)
160 u32 addr, status, decprot[ETZPC_DECPROT_NB];
162 array = stm32mp1_ip_addr;
163 array_size = ARRAY_SIZE(stm32mp1_ip_addr);
165 for (i = 0; i < ETZPC_DECPROT_NB; i++)
166 decprot[i] = readl(ETZPC_DECPROT(i));
168 for (i = 0; i < array_size; i++) {
169 offset = i / NB_PROT_PER_REG;
170 shift = (i % NB_PROT_PER_REG) * DECPROT_NB_BITS;
171 status = (decprot[offset] >> shift) & DECPROT_MASK;
174 debug("ETZPC: 0x%08x decprot %d=%d\n", addr, i, status);
176 if (addr == ETZPC_RESERVED ||
177 status == DECPROT_NON_SECURED)
180 if (fdt_disable_subnode_by_address(fdt, soc_node, addr))
181 printf("ETZPC: 0x%08x node disabled, decprot %d=%d\n",
188 /* deactivate all the cpu except core 0 */
189 static void stm32_fdt_fixup_cpu(void *blob, char *name)
194 off = fdt_path_offset(blob, "/cpus");
196 printf("%s: couldn't find /cpus node\n", __func__);
200 off = fdt_node_offset_by_prop_value(blob, -1, "device_type", "cpu", 4);
201 while (off != -FDT_ERR_NOTFOUND) {
202 reg = fdtdec_get_addr(blob, off, "reg");
204 fdt_del_node(blob, off);
205 printf("FDT: cpu %d node remove for %s\n", reg, name);
206 /* after delete we can't trust the offsets anymore */
209 off = fdt_node_offset_by_prop_value(blob, off,
210 "device_type", "cpu", 4);
214 static void stm32_fdt_disable(void *fdt, int offset, u32 addr,
215 const char *string, const char *name)
217 if (fdt_disable_subnode_by_address(fdt, offset, addr))
218 printf("FDT: %s@%08x node disabled for %s\n",
222 static void stm32_fdt_disable_optee(void *blob)
226 off = fdt_node_offset_by_compatible(blob, -1, "linaro,optee-tz");
227 if (off >= 0 && fdtdec_get_is_enabled(blob, off))
228 fdt_status_disabled(blob, off);
230 /* Disabled "optee@..." reserved-memory node */
231 off = fdt_path_offset(blob, "/reserved-memory/");
234 for (node = fdt_first_subnode(blob, off);
236 node = fdt_next_subnode(blob, node)) {
237 if (!strncmp(fdt_get_name(blob, node, NULL), "optee@", 6))
238 fdt_status_disabled(blob, node);
243 * This function is called right before the kernel is booted. "blob" is the
244 * device tree that will be passed to the kernel.
246 int ft_system_setup(void *blob, bd_t *bd)
251 char name[SOC_NAME_SIZE];
253 soc = fdt_path_offset(blob, "/soc");
257 if (CONFIG_IS_ENABLED(STM32_ETZPC)) {
258 ret = stm32_fdt_fixup_etzpc(blob, soc);
263 /* MPUs Part Numbers and name*/
264 cpu = get_cpu_type();
268 case CPU_STM32MP151Fxx:
269 case CPU_STM32MP151Dxx:
270 case CPU_STM32MP151Cxx:
271 case CPU_STM32MP151Axx:
272 stm32_fdt_fixup_cpu(blob, name);
273 /* after cpu delete we can't trust the soc offsets anymore */
274 soc = fdt_path_offset(blob, "/soc");
275 stm32_fdt_disable(blob, soc, STM32_FDCAN_BASE, "can", name);
277 case CPU_STM32MP153Fxx:
278 case CPU_STM32MP153Dxx:
279 case CPU_STM32MP153Cxx:
280 case CPU_STM32MP153Axx:
281 stm32_fdt_disable(blob, soc, STM32_GPU_BASE, "gpu", name);
282 stm32_fdt_disable(blob, soc, STM32_DSI_BASE, "dsi", name);
289 case CPU_STM32MP157Dxx:
290 case CPU_STM32MP157Axx:
291 case CPU_STM32MP153Dxx:
292 case CPU_STM32MP153Axx:
293 case CPU_STM32MP151Dxx:
294 case CPU_STM32MP151Axx:
295 stm32_fdt_disable(blob, soc, STM32_CRYP1_BASE, "cryp", name);
296 stm32_fdt_disable(blob, soc, STM32_CRYP2_BASE, "cryp", name);
302 switch (get_cpu_package()) {
304 pkg = STM32MP_PKG_AA;
307 pkg = STM32MP_PKG_AB;
309 case PKG_AC_TFBGA361:
310 pkg = STM32MP_PKG_AC;
312 case PKG_AD_TFBGA257:
313 pkg = STM32MP_PKG_AD;
320 do_fixup_by_compat_u32(blob, "st,stm32mp157-pinctrl",
321 "st,package", pkg, false);
322 do_fixup_by_compat_u32(blob, "st,stm32mp157-z-pinctrl",
323 "st,package", pkg, false);
326 if (!CONFIG_IS_ENABLED(OPTEE) ||
327 !tee_find_device(NULL, NULL, NULL, NULL))
328 stm32_fdt_disable_optee(blob);