blob: 0de1d8229122b36e8d8b841fe40454a83eab1480 [file] [log] [blame]
Patrick Delaunay05d36932019-07-05 17:20:14 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
Etienne Carriere164873a2020-06-05 09:24:29 +02003 * Copyright (C) 2019-2020, STMicroelectronics - All Rights Reserved
Patrick Delaunay05d36932019-07-05 17:20:14 +02004 */
5
6#include <common.h>
7#include <fdt_support.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Patrick Delaunay43df0a12020-03-18 09:22:49 +01009#include <tee.h>
Patrick Delaunay05d36932019-07-05 17:20:14 +020010#include <asm/arch/sys_proto.h>
11#include <dt-bindings/pinctrl/stm32-pinfunc.h>
Patrick Delaunayc60f3b32019-07-05 17:20:15 +020012#include <linux/io.h>
13
14#define ETZPC_DECPROT(n) (STM32_ETZPC_BASE + 0x10 + 4 * (n))
15#define ETZPC_DECPROT_NB 6
16
17#define DECPROT_MASK 0x03
18#define NB_PROT_PER_REG 0x10
19#define DECPROT_NB_BITS 2
20
21#define DECPROT_SECURED 0x00
22#define DECPROT_WRITE_SECURE 0x01
23#define DECPROT_MCU_ISOLATION 0x02
24#define DECPROT_NON_SECURED 0x03
25
26#define ETZPC_RESERVED 0xffffffff
27
Patrick Delaunay73306a12020-02-12 19:37:44 +010028#define STM32_FDCAN_BASE 0x4400e000
29#define STM32_CRYP2_BASE 0x4c005000
30#define STM32_CRYP1_BASE 0x54001000
31#define STM32_GPU_BASE 0x59000000
32#define STM32_DSI_BASE 0x5a000000
33
Patrick Delaunayc60f3b32019-07-05 17:20:15 +020034static const u32 stm32mp1_ip_addr[] = {
35 0x5c008000, /* 00 stgenc */
36 0x54000000, /* 01 bkpsram */
37 0x5c003000, /* 02 iwdg1 */
38 0x5c000000, /* 03 usart1 */
39 0x5c001000, /* 04 spi6 */
40 0x5c002000, /* 05 i2c4 */
41 ETZPC_RESERVED, /* 06 reserved */
42 0x54003000, /* 07 rng1 */
43 0x54002000, /* 08 hash1 */
Patrick Delaunay73306a12020-02-12 19:37:44 +010044 STM32_CRYP1_BASE, /* 09 cryp1 */
Patrick Delaunayc60f3b32019-07-05 17:20:15 +020045 0x5a003000, /* 0A ddrctrl */
46 0x5a004000, /* 0B ddrphyc */
47 0x5c009000, /* 0C i2c6 */
48 ETZPC_RESERVED, /* 0D reserved */
49 ETZPC_RESERVED, /* 0E reserved */
50 ETZPC_RESERVED, /* 0F reserved */
51 0x40000000, /* 10 tim2 */
52 0x40001000, /* 11 tim3 */
53 0x40002000, /* 12 tim4 */
54 0x40003000, /* 13 tim5 */
55 0x40004000, /* 14 tim6 */
56 0x40005000, /* 15 tim7 */
57 0x40006000, /* 16 tim12 */
58 0x40007000, /* 17 tim13 */
59 0x40008000, /* 18 tim14 */
60 0x40009000, /* 19 lptim1 */
61 0x4000a000, /* 1A wwdg1 */
62 0x4000b000, /* 1B spi2 */
63 0x4000c000, /* 1C spi3 */
64 0x4000d000, /* 1D spdifrx */
65 0x4000e000, /* 1E usart2 */
66 0x4000f000, /* 1F usart3 */
67 0x40010000, /* 20 uart4 */
68 0x40011000, /* 21 uart5 */
69 0x40012000, /* 22 i2c1 */
70 0x40013000, /* 23 i2c2 */
71 0x40014000, /* 24 i2c3 */
72 0x40015000, /* 25 i2c5 */
73 0x40016000, /* 26 cec */
74 0x40017000, /* 27 dac */
75 0x40018000, /* 28 uart7 */
76 0x40019000, /* 29 uart8 */
77 ETZPC_RESERVED, /* 2A reserved */
78 ETZPC_RESERVED, /* 2B reserved */
79 0x4001c000, /* 2C mdios */
80 ETZPC_RESERVED, /* 2D reserved */
81 ETZPC_RESERVED, /* 2E reserved */
82 ETZPC_RESERVED, /* 2F reserved */
83 0x44000000, /* 30 tim1 */
84 0x44001000, /* 31 tim8 */
85 ETZPC_RESERVED, /* 32 reserved */
86 0x44003000, /* 33 usart6 */
87 0x44004000, /* 34 spi1 */
88 0x44005000, /* 35 spi4 */
89 0x44006000, /* 36 tim15 */
90 0x44007000, /* 37 tim16 */
91 0x44008000, /* 38 tim17 */
92 0x44009000, /* 39 spi5 */
93 0x4400a000, /* 3A sai1 */
94 0x4400b000, /* 3B sai2 */
95 0x4400c000, /* 3C sai3 */
96 0x4400d000, /* 3D dfsdm */
Patrick Delaunay73306a12020-02-12 19:37:44 +010097 STM32_FDCAN_BASE, /* 3E tt_fdcan */
Patrick Delaunayc60f3b32019-07-05 17:20:15 +020098 ETZPC_RESERVED, /* 3F reserved */
99 0x50021000, /* 40 lptim2 */
100 0x50022000, /* 41 lptim3 */
101 0x50023000, /* 42 lptim4 */
102 0x50024000, /* 43 lptim5 */
103 0x50027000, /* 44 sai4 */
104 0x50025000, /* 45 vrefbuf */
105 0x4c006000, /* 46 dcmi */
106 0x4c004000, /* 47 crc2 */
107 0x48003000, /* 48 adc */
108 0x4c002000, /* 49 hash2 */
109 0x4c003000, /* 4A rng2 */
Patrick Delaunay73306a12020-02-12 19:37:44 +0100110 STM32_CRYP2_BASE, /* 4B cryp2 */
Patrick Delaunayc60f3b32019-07-05 17:20:15 +0200111 ETZPC_RESERVED, /* 4C reserved */
112 ETZPC_RESERVED, /* 4D reserved */
113 ETZPC_RESERVED, /* 4E reserved */
114 ETZPC_RESERVED, /* 4F reserved */
115 ETZPC_RESERVED, /* 50 sram1 */
116 ETZPC_RESERVED, /* 51 sram2 */
117 ETZPC_RESERVED, /* 52 sram3 */
118 ETZPC_RESERVED, /* 53 sram4 */
119 ETZPC_RESERVED, /* 54 retram */
120 0x49000000, /* 55 otg */
121 0x48004000, /* 56 sdmmc3 */
122 0x48005000, /* 57 dlybsd3 */
123 0x48000000, /* 58 dma1 */
124 0x48001000, /* 59 dma2 */
125 0x48002000, /* 5A dmamux */
126 0x58002000, /* 5B fmc */
127 0x58003000, /* 5C qspi */
128 0x58004000, /* 5D dlybq */
129 0x5800a000, /* 5E eth */
130 ETZPC_RESERVED, /* 5F reserved */
131};
132
133/* fdt helper */
134static bool fdt_disable_subnode_by_address(void *fdt, int offset, u32 addr)
135{
136 int node;
Patrick Delaunay73306a12020-02-12 19:37:44 +0100137 fdt_addr_t regs;
Patrick Delaunayc60f3b32019-07-05 17:20:15 +0200138
139 for (node = fdt_first_subnode(fdt, offset);
140 node >= 0;
141 node = fdt_next_subnode(fdt, node)) {
Patrick Delaunay73306a12020-02-12 19:37:44 +0100142 regs = fdtdec_get_addr(fdt, node, "reg");
143 if (addr == regs) {
Patrick Delaunayc60f3b32019-07-05 17:20:15 +0200144 if (fdtdec_get_is_enabled(fdt, node)) {
145 fdt_status_disabled(fdt, node);
146
147 return true;
148 }
149 return false;
150 }
151 }
152
153 return false;
154}
155
Patrick Delaunay73306a12020-02-12 19:37:44 +0100156static int stm32_fdt_fixup_etzpc(void *fdt, int soc_node)
Patrick Delaunayc60f3b32019-07-05 17:20:15 +0200157{
158 const u32 *array;
159 int array_size, i;
Patrick Delaunay73306a12020-02-12 19:37:44 +0100160 int offset, shift;
Patrick Delaunayc60f3b32019-07-05 17:20:15 +0200161 u32 addr, status, decprot[ETZPC_DECPROT_NB];
162
163 array = stm32mp1_ip_addr;
164 array_size = ARRAY_SIZE(stm32mp1_ip_addr);
165
166 for (i = 0; i < ETZPC_DECPROT_NB; i++)
167 decprot[i] = readl(ETZPC_DECPROT(i));
168
Patrick Delaunayc60f3b32019-07-05 17:20:15 +0200169 for (i = 0; i < array_size; i++) {
170 offset = i / NB_PROT_PER_REG;
171 shift = (i % NB_PROT_PER_REG) * DECPROT_NB_BITS;
172 status = (decprot[offset] >> shift) & DECPROT_MASK;
173 addr = array[i];
174
175 debug("ETZPC: 0x%08x decprot %d=%d\n", addr, i, status);
176
177 if (addr == ETZPC_RESERVED ||
178 status == DECPROT_NON_SECURED)
179 continue;
180
181 if (fdt_disable_subnode_by_address(fdt, soc_node, addr))
182 printf("ETZPC: 0x%08x node disabled, decprot %d=%d\n",
183 addr, i, status);
184 }
185
186 return 0;
187}
Patrick Delaunay05d36932019-07-05 17:20:14 +0200188
Patrick Delaunay73306a12020-02-12 19:37:44 +0100189/* deactivate all the cpu except core 0 */
190static void stm32_fdt_fixup_cpu(void *blob, char *name)
191{
192 int off;
193 u32 reg;
194
195 off = fdt_path_offset(blob, "/cpus");
196 if (off < 0) {
197 printf("%s: couldn't find /cpus node\n", __func__);
198 return;
199 }
200
201 off = fdt_node_offset_by_prop_value(blob, -1, "device_type", "cpu", 4);
202 while (off != -FDT_ERR_NOTFOUND) {
203 reg = fdtdec_get_addr(blob, off, "reg");
204 if (reg != 0) {
205 fdt_del_node(blob, off);
206 printf("FDT: cpu %d node remove for %s\n", reg, name);
207 /* after delete we can't trust the offsets anymore */
208 off = -1;
209 }
210 off = fdt_node_offset_by_prop_value(blob, off,
211 "device_type", "cpu", 4);
212 }
213}
214
215static void stm32_fdt_disable(void *fdt, int offset, u32 addr,
216 const char *string, const char *name)
217{
218 if (fdt_disable_subnode_by_address(fdt, offset, addr))
219 printf("FDT: %s@%08x node disabled for %s\n",
220 string, addr, name);
221}
222
Patrick Delaunay4a1b9752020-03-18 09:22:48 +0100223static void stm32_fdt_disable_optee(void *blob)
224{
225 int off, node;
226
Etienne Carriere164873a2020-06-05 09:24:29 +0200227 /* Delete "optee" firmware node */
Patrick Delaunay4a1b9752020-03-18 09:22:48 +0100228 off = fdt_node_offset_by_compatible(blob, -1, "linaro,optee-tz");
229 if (off >= 0 && fdtdec_get_is_enabled(blob, off))
Etienne Carriere164873a2020-06-05 09:24:29 +0200230 fdt_del_node(blob, off);
Patrick Delaunay4a1b9752020-03-18 09:22:48 +0100231
Etienne Carriere164873a2020-06-05 09:24:29 +0200232 /* Delete "optee@..." reserved-memory node */
Patrick Delaunay4a1b9752020-03-18 09:22:48 +0100233 off = fdt_path_offset(blob, "/reserved-memory/");
234 if (off < 0)
235 return;
236 for (node = fdt_first_subnode(blob, off);
237 node >= 0;
238 node = fdt_next_subnode(blob, node)) {
Etienne Carriere164873a2020-06-05 09:24:29 +0200239 if (strncmp(fdt_get_name(blob, node, NULL), "optee@", 6))
240 continue;
241
242 if (fdt_del_node(blob, node))
243 printf("Failed to remove optee reserved-memory node\n");
Patrick Delaunay4a1b9752020-03-18 09:22:48 +0100244 }
245}
246
Patrick Delaunay05d36932019-07-05 17:20:14 +0200247/*
248 * This function is called right before the kernel is booted. "blob" is the
249 * device tree that will be passed to the kernel.
250 */
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900251int ft_system_setup(void *blob, struct bd_info *bd)
Patrick Delaunay05d36932019-07-05 17:20:14 +0200252{
253 int ret = 0;
Patrick Delaunay73306a12020-02-12 19:37:44 +0100254 int soc;
255 u32 pkg, cpu;
256 char name[SOC_NAME_SIZE];
257
258 soc = fdt_path_offset(blob, "/soc");
259 if (soc < 0)
260 return soc;
Patrick Delaunay05d36932019-07-05 17:20:14 +0200261
Patrick Delaunayc60f3b32019-07-05 17:20:15 +0200262 if (CONFIG_IS_ENABLED(STM32_ETZPC)) {
Patrick Delaunay73306a12020-02-12 19:37:44 +0100263 ret = stm32_fdt_fixup_etzpc(blob, soc);
Patrick Delaunayc60f3b32019-07-05 17:20:15 +0200264 if (ret)
265 return ret;
266 }
267
Patrick Delaunay73306a12020-02-12 19:37:44 +0100268 /* MPUs Part Numbers and name*/
269 cpu = get_cpu_type();
270 get_soc_name(name);
271
272 switch (cpu) {
Patrick Delaunay050fed82020-02-26 11:26:43 +0100273 case CPU_STM32MP151Fxx:
274 case CPU_STM32MP151Dxx:
Patrick Delaunay73306a12020-02-12 19:37:44 +0100275 case CPU_STM32MP151Cxx:
276 case CPU_STM32MP151Axx:
277 stm32_fdt_fixup_cpu(blob, name);
278 /* after cpu delete we can't trust the soc offsets anymore */
279 soc = fdt_path_offset(blob, "/soc");
280 stm32_fdt_disable(blob, soc, STM32_FDCAN_BASE, "can", name);
281 /* fall through */
Patrick Delaunay050fed82020-02-26 11:26:43 +0100282 case CPU_STM32MP153Fxx:
283 case CPU_STM32MP153Dxx:
Patrick Delaunay73306a12020-02-12 19:37:44 +0100284 case CPU_STM32MP153Cxx:
285 case CPU_STM32MP153Axx:
286 stm32_fdt_disable(blob, soc, STM32_GPU_BASE, "gpu", name);
287 stm32_fdt_disable(blob, soc, STM32_DSI_BASE, "dsi", name);
288 break;
289 default:
290 break;
291 }
292
293 switch (cpu) {
Patrick Delaunay050fed82020-02-26 11:26:43 +0100294 case CPU_STM32MP157Dxx:
Patrick Delaunay73306a12020-02-12 19:37:44 +0100295 case CPU_STM32MP157Axx:
Patrick Delaunay050fed82020-02-26 11:26:43 +0100296 case CPU_STM32MP153Dxx:
Patrick Delaunay73306a12020-02-12 19:37:44 +0100297 case CPU_STM32MP153Axx:
Patrick Delaunay050fed82020-02-26 11:26:43 +0100298 case CPU_STM32MP151Dxx:
Patrick Delaunay73306a12020-02-12 19:37:44 +0100299 case CPU_STM32MP151Axx:
300 stm32_fdt_disable(blob, soc, STM32_CRYP1_BASE, "cryp", name);
301 stm32_fdt_disable(blob, soc, STM32_CRYP2_BASE, "cryp", name);
302 break;
303 default:
304 break;
305 }
306
Patrick Delaunay05d36932019-07-05 17:20:14 +0200307 switch (get_cpu_package()) {
308 case PKG_AA_LBGA448:
309 pkg = STM32MP_PKG_AA;
310 break;
311 case PKG_AB_LBGA354:
312 pkg = STM32MP_PKG_AB;
313 break;
314 case PKG_AC_TFBGA361:
315 pkg = STM32MP_PKG_AC;
316 break;
317 case PKG_AD_TFBGA257:
318 pkg = STM32MP_PKG_AD;
319 break;
320 default:
321 pkg = 0;
322 break;
323 }
324 if (pkg) {
325 do_fixup_by_compat_u32(blob, "st,stm32mp157-pinctrl",
326 "st,package", pkg, false);
327 do_fixup_by_compat_u32(blob, "st,stm32mp157-z-pinctrl",
328 "st,package", pkg, false);
329 }
330
Patrick Delaunay43df0a12020-03-18 09:22:49 +0100331 if (!CONFIG_IS_ENABLED(OPTEE) ||
332 !tee_find_device(NULL, NULL, NULL, NULL))
Patrick Delaunay4a1b9752020-03-18 09:22:48 +0100333 stm32_fdt_disable_optee(blob);
334
Patrick Delaunay05d36932019-07-05 17:20:14 +0200335 return ret;
336}