1
2
3
4
5
6#define LOG_CATEGORY LOGC_ARCH
7
8#include <common.h>
9#include <fdtdec.h>
10#include <fdt_support.h>
11#include <log.h>
12#include <tee.h>
13#include <asm/arch/sys_proto.h>
14#include <dt-bindings/pinctrl/stm32-pinfunc.h>
15#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
31#define STM32MP13_FDCAN_BASE 0x4400F000
32#define STM32MP13_ADC1_BASE 0x48003000
33#define STM32MP13_TSC_BASE 0x5000B000
34#define STM32MP13_CRYP_BASE 0x54002000
35#define STM32MP13_ETH2_BASE 0x5800E000
36#define STM32MP13_DCMIPP_BASE 0x5A000000
37#define STM32MP13_LTDC_BASE 0x5A010000
38
39#define STM32MP15_FDCAN_BASE 0x4400e000
40#define STM32MP15_CRYP2_BASE 0x4c005000
41#define STM32MP15_CRYP1_BASE 0x54001000
42#define STM32MP15_GPU_BASE 0x59000000
43#define STM32MP15_DSI_BASE 0x5a000000
44
45static const u32 stm32mp13_ip_addr[] = {
46 0x50025000,
47 0x50021000,
48 0x50022000,
49 STM32MP13_LTDC_BASE,
50 STM32MP13_DCMIPP_BASE,
51 0x5A006000,
52 0x5A003000,
53 ETZPC_RESERVED,
54 ETZPC_RESERVED,
55 ETZPC_RESERVED,
56 0x5C006000,
57 0x58001000,
58 0x5C000000,
59 0x5C008000,
60 ETZPC_RESERVED,
61 ETZPC_RESERVED,
62 0x4C000000,
63 0x4C001000,
64 0x4C002000,
65 0x4C003000,
66 0x4C004000,
67 0x4C005000,
68 0x4C006000,
69 0x4C007000,
70 0x4C008000,
71 0x4C009000,
72 0x4C00A000,
73 0x4C00B000,
74 0x4C00C000,
75 ETZPC_RESERVED,
76 ETZPC_RESERVED,
77 ETZPC_RESERVED,
78 STM32MP13_ADC1_BASE,
79 0x48004000,
80 0x49000000,
81 ETZPC_RESERVED,
82 ETZPC_RESERVED,
83 STM32MP13_TSC_BASE,
84 ETZPC_RESERVED,
85 ETZPC_RESERVED,
86 0x54004000,
87 0x54003000,
88 STM32MP13_CRYP_BASE,
89 0x54005000,
90 0x54006000,
91 0x54000000,
92 ETZPC_RESERVED,
93 ETZPC_RESERVED,
94 0x5800A000,
95 STM32MP13_ETH2_BASE,
96 0x58005000,
97 0x58007000,
98 ETZPC_RESERVED,
99 ETZPC_RESERVED,
100 0x58002000,
101 0x58003000,
102 ETZPC_RESERVED,
103 ETZPC_RESERVED,
104 ETZPC_RESERVED,
105 ETZPC_RESERVED,
106 0x30000000,
107 0x30004000,
108 0x30006000,
109 ETZPC_RESERVED,
110 ETZPC_RESERVED,
111 ETZPC_RESERVED,
112 ETZPC_RESERVED,
113 ETZPC_RESERVED,
114 ETZPC_RESERVED,
115 ETZPC_RESERVED,
116 ETZPC_RESERVED,
117 ETZPC_RESERVED,
118 ETZPC_RESERVED,
119 ETZPC_RESERVED,
120 ETZPC_RESERVED,
121 ETZPC_RESERVED,
122 ETZPC_RESERVED,
123 ETZPC_RESERVED,
124 ETZPC_RESERVED,
125 ETZPC_RESERVED,
126 ETZPC_RESERVED,
127 ETZPC_RESERVED,
128 ETZPC_RESERVED,
129 ETZPC_RESERVED,
130 ETZPC_RESERVED,
131 ETZPC_RESERVED,
132 ETZPC_RESERVED,
133 ETZPC_RESERVED,
134 ETZPC_RESERVED,
135 ETZPC_RESERVED,
136 ETZPC_RESERVED,
137 ETZPC_RESERVED,
138 ETZPC_RESERVED,
139 ETZPC_RESERVED,
140 ETZPC_RESERVED,
141 ETZPC_RESERVED,
142};
143
144static const u32 stm32mp15_ip_addr[] = {
145 0x5c008000,
146 0x54000000,
147 0x5c003000,
148 0x5c000000,
149 0x5c001000,
150 0x5c002000,
151 ETZPC_RESERVED,
152 0x54003000,
153 0x54002000,
154 STM32MP15_CRYP1_BASE,
155 0x5a003000,
156 0x5a004000,
157 0x5c009000,
158 ETZPC_RESERVED,
159 ETZPC_RESERVED,
160 ETZPC_RESERVED,
161 0x40000000,
162 0x40001000,
163 0x40002000,
164 0x40003000,
165 0x40004000,
166 0x40005000,
167 0x40006000,
168 0x40007000,
169 0x40008000,
170 0x40009000,
171 0x4000a000,
172 0x4000b000,
173 0x4000c000,
174 0x4000d000,
175 0x4000e000,
176 0x4000f000,
177 0x40010000,
178 0x40011000,
179 0x40012000,
180 0x40013000,
181 0x40014000,
182 0x40015000,
183 0x40016000,
184 0x40017000,
185 0x40018000,
186 0x40019000,
187 ETZPC_RESERVED,
188 ETZPC_RESERVED,
189 0x4001c000,
190 ETZPC_RESERVED,
191 ETZPC_RESERVED,
192 ETZPC_RESERVED,
193 0x44000000,
194 0x44001000,
195 ETZPC_RESERVED,
196 0x44003000,
197 0x44004000,
198 0x44005000,
199 0x44006000,
200 0x44007000,
201 0x44008000,
202 0x44009000,
203 0x4400a000,
204 0x4400b000,
205 0x4400c000,
206 0x4400d000,
207 STM32MP15_FDCAN_BASE,
208 ETZPC_RESERVED,
209 0x50021000,
210 0x50022000,
211 0x50023000,
212 0x50024000,
213 0x50027000,
214 0x50025000,
215 0x4c006000,
216 0x4c004000,
217 0x48003000,
218 0x4c002000,
219 0x4c003000,
220 STM32MP15_CRYP2_BASE,
221 ETZPC_RESERVED,
222 ETZPC_RESERVED,
223 ETZPC_RESERVED,
224 ETZPC_RESERVED,
225 ETZPC_RESERVED,
226 ETZPC_RESERVED,
227 ETZPC_RESERVED,
228 ETZPC_RESERVED,
229 ETZPC_RESERVED,
230 0x49000000,
231 0x48004000,
232 0x48005000,
233 0x48000000,
234 0x48001000,
235 0x48002000,
236 0x58002000,
237 0x58003000,
238 0x58004000,
239 0x5800a000,
240 ETZPC_RESERVED,
241};
242
243
244static bool fdt_disable_subnode_by_address(void *fdt, int offset, u32 addr)
245{
246 int node;
247 fdt_addr_t regs;
248
249 for (node = fdt_first_subnode(fdt, offset);
250 node >= 0;
251 node = fdt_next_subnode(fdt, node)) {
252 regs = fdtdec_get_addr(fdt, node, "reg");
253 if (addr == regs) {
254 if (fdtdec_get_is_enabled(fdt, node)) {
255 fdt_status_disabled(fdt, node);
256
257 return true;
258 }
259 return false;
260 }
261 }
262
263 return false;
264}
265
266static int stm32_fdt_fixup_etzpc(void *fdt, int soc_node)
267{
268 const u32 *array;
269 int array_size, i;
270 int offset, shift;
271 u32 addr, status, decprot[ETZPC_DECPROT_NB];
272
273 if (IS_ENABLED(CONFIG_STM32MP13x)) {
274 array = stm32mp13_ip_addr;
275 array_size = ARRAY_SIZE(stm32mp13_ip_addr);
276 }
277
278 if (IS_ENABLED(CONFIG_STM32MP15x)) {
279 array = stm32mp15_ip_addr;
280 array_size = ARRAY_SIZE(stm32mp15_ip_addr);
281 }
282
283 for (i = 0; i < ETZPC_DECPROT_NB; i++)
284 decprot[i] = readl(ETZPC_DECPROT(i));
285
286 for (i = 0; i < array_size; i++) {
287 offset = i / NB_PROT_PER_REG;
288 shift = (i % NB_PROT_PER_REG) * DECPROT_NB_BITS;
289 status = (decprot[offset] >> shift) & DECPROT_MASK;
290 addr = array[i];
291
292 log_debug("ETZPC: 0x%08x decprot %d=%d\n", addr, i, status);
293
294 if (addr == ETZPC_RESERVED ||
295 status == DECPROT_NON_SECURED)
296 continue;
297
298 if (fdt_disable_subnode_by_address(fdt, soc_node, addr))
299 log_notice("ETZPC: 0x%08x node disabled, decprot %d=%d\n",
300 addr, i, status);
301 }
302
303 return 0;
304}
305
306
307static void stm32_fdt_fixup_cpu(void *blob, char *name)
308{
309 int off;
310 u32 reg;
311
312 off = fdt_path_offset(blob, "/cpus");
313 if (off < 0) {
314 log_warning("%s: couldn't find /cpus node\n", __func__);
315 return;
316 }
317
318 off = fdt_node_offset_by_prop_value(blob, -1, "device_type", "cpu", 4);
319 while (off != -FDT_ERR_NOTFOUND) {
320 reg = fdtdec_get_addr(blob, off, "reg");
321 if (reg != 0) {
322 fdt_del_node(blob, off);
323 log_notice("FDT: cpu %d node remove for %s\n",
324 reg, name);
325
326 off = -1;
327 }
328 off = fdt_node_offset_by_prop_value(blob, off,
329 "device_type", "cpu", 4);
330 }
331}
332
333static void stm32_fdt_disable(void *fdt, int offset, u32 addr,
334 const char *string, const char *name)
335{
336 if (fdt_disable_subnode_by_address(fdt, offset, addr))
337 log_notice("FDT: %s@%08x node disabled for %s\n",
338 string, addr, name);
339}
340
341static void stm32_fdt_disable_optee(void *blob)
342{
343 int off, node;
344
345
346 off = fdt_node_offset_by_compatible(blob, -1, "linaro,optee-tz");
347 if (off >= 0 && fdtdec_get_is_enabled(blob, off))
348 fdt_del_node(blob, off);
349
350
351 off = fdt_path_offset(blob, "/reserved-memory/");
352 if (off < 0)
353 return;
354 for (node = fdt_first_subnode(blob, off);
355 node >= 0;
356 node = fdt_next_subnode(blob, node)) {
357 if (strncmp(fdt_get_name(blob, node, NULL), "optee@", 6))
358 continue;
359
360 if (fdt_del_node(blob, node))
361 printf("Failed to remove optee reserved-memory node\n");
362 }
363}
364
365static void stm32mp13_fdt_fixup(void *blob, int soc, u32 cpu, char *name)
366{
367 switch (cpu) {
368 case CPU_STM32MP131Fxx:
369 case CPU_STM32MP131Dxx:
370 case CPU_STM32MP131Cxx:
371 case CPU_STM32MP131Axx:
372 stm32_fdt_disable(blob, soc, STM32MP13_FDCAN_BASE, "can", name);
373 stm32_fdt_disable(blob, soc, STM32MP13_ADC1_BASE, "adc", name);
374 fallthrough;
375 case CPU_STM32MP133Fxx:
376 case CPU_STM32MP133Dxx:
377 case CPU_STM32MP133Cxx:
378 case CPU_STM32MP133Axx:
379 stm32_fdt_disable(blob, soc, STM32MP13_LTDC_BASE, "ltdc", name);
380 stm32_fdt_disable(blob, soc, STM32MP13_DCMIPP_BASE, "dcmipp",
381 name);
382 stm32_fdt_disable(blob, soc, STM32MP13_TSC_BASE, "tsc", name);
383 break;
384 default:
385 break;
386 }
387
388 switch (cpu) {
389 case CPU_STM32MP135Dxx:
390 case CPU_STM32MP135Axx:
391 case CPU_STM32MP133Dxx:
392 case CPU_STM32MP133Axx:
393 case CPU_STM32MP131Dxx:
394 case CPU_STM32MP131Axx:
395 stm32_fdt_disable(blob, soc, STM32MP13_CRYP_BASE, "cryp", name);
396 break;
397 default:
398 break;
399 }
400}
401
402static void stm32mp15_fdt_fixup(void *blob, int soc, u32 cpu, char *name)
403{
404 u32 pkg;
405
406 switch (cpu) {
407 case CPU_STM32MP151Fxx:
408 case CPU_STM32MP151Dxx:
409 case CPU_STM32MP151Cxx:
410 case CPU_STM32MP151Axx:
411 stm32_fdt_fixup_cpu(blob, name);
412
413 soc = fdt_path_offset(blob, "/soc");
414 stm32_fdt_disable(blob, soc, STM32MP15_FDCAN_BASE, "can", name);
415 fallthrough;
416 case CPU_STM32MP153Fxx:
417 case CPU_STM32MP153Dxx:
418 case CPU_STM32MP153Cxx:
419 case CPU_STM32MP153Axx:
420 stm32_fdt_disable(blob, soc, STM32MP15_GPU_BASE, "gpu", name);
421 stm32_fdt_disable(blob, soc, STM32MP15_DSI_BASE, "dsi", name);
422 break;
423 default:
424 break;
425 }
426 switch (cpu) {
427 case CPU_STM32MP157Dxx:
428 case CPU_STM32MP157Axx:
429 case CPU_STM32MP153Dxx:
430 case CPU_STM32MP153Axx:
431 case CPU_STM32MP151Dxx:
432 case CPU_STM32MP151Axx:
433 stm32_fdt_disable(blob, soc, STM32MP15_CRYP1_BASE, "cryp",
434 name);
435 stm32_fdt_disable(blob, soc, STM32MP15_CRYP2_BASE, "cryp",
436 name);
437 break;
438 default:
439 break;
440 }
441 switch (get_cpu_package()) {
442 case STM32MP15_PKG_AA_LBGA448:
443 pkg = STM32MP_PKG_AA;
444 break;
445 case STM32MP15_PKG_AB_LBGA354:
446 pkg = STM32MP_PKG_AB;
447 break;
448 case STM32MP15_PKG_AC_TFBGA361:
449 pkg = STM32MP_PKG_AC;
450 break;
451 case STM32MP15_PKG_AD_TFBGA257:
452 pkg = STM32MP_PKG_AD;
453 break;
454 default:
455 pkg = 0;
456 break;
457 }
458 if (pkg) {
459 do_fixup_by_compat_u32(blob, "st,stm32mp157-pinctrl",
460 "st,package", pkg, false);
461 do_fixup_by_compat_u32(blob, "st,stm32mp157-z-pinctrl",
462 "st,package", pkg, false);
463 }
464}
465
466
467
468
469
470int ft_system_setup(void *blob, struct bd_info *bd)
471{
472 int ret = 0;
473 int soc;
474 u32 cpu;
475 char name[SOC_NAME_SIZE];
476
477 soc = fdt_path_offset(blob, "/soc");
478
479 if (soc == -FDT_ERR_NOTFOUND)
480 return 0;
481 if (soc < 0)
482 return soc;
483
484 if (CONFIG_IS_ENABLED(STM32_ETZPC)) {
485 ret = stm32_fdt_fixup_etzpc(blob, soc);
486 if (ret)
487 return ret;
488 }
489
490
491 cpu = get_cpu_type();
492 get_soc_name(name);
493
494 if (IS_ENABLED(CONFIG_STM32MP13x))
495 stm32mp13_fdt_fixup(blob, soc, cpu, name);
496
497 if (IS_ENABLED(CONFIG_STM32MP15x)) {
498 stm32mp15_fdt_fixup(blob, soc, cpu, name);
499
500
501
502
503
504
505
506
507
508 if (CONFIG_IS_ENABLED(STM32MP15x_STM32IMAGE) &&
509 !tee_find_device(NULL, NULL, NULL, NULL))
510 stm32_fdt_disable_optee(blob);
511 }
512
513 return ret;
514}
515