blob: 21b5f09728c26ce39904a18a8e200e44076020ea [file] [log] [blame]
Patrick Delaunaya6f03912019-07-05 17:20:14 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
4 */
5
6#include <common.h>
7#include <fdt_support.h>
Patrick Delaunay472407a2020-03-18 09:22:49 +01008#include <tee.h>
Patrick Delaunaya6f03912019-07-05 17:20:14 +02009#include <asm/arch/sys_proto.h>
10#include <dt-bindings/pinctrl/stm32-pinfunc.h>
Patrick Delaunay43f214c2019-07-05 17:20:15 +020011#include <linux/io.h>
12
13#define ETZPC_DECPROT(n) (STM32_ETZPC_BASE + 0x10 + 4 * (n))
14#define ETZPC_DECPROT_NB 6
15
16#define DECPROT_MASK 0x03
17#define NB_PROT_PER_REG 0x10
18#define DECPROT_NB_BITS 2
19
20#define DECPROT_SECURED 0x00
21#define DECPROT_WRITE_SECURE 0x01
22#define DECPROT_MCU_ISOLATION 0x02
23#define DECPROT_NON_SECURED 0x03
24
25#define ETZPC_RESERVED 0xffffffff
26
Patrick Delaunayfac5ba82020-02-12 19:37:44 +010027#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
32
Patrick Delaunay43f214c2019-07-05 17:20:15 +020033static 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 */
Patrick Delaunayfac5ba82020-02-12 19:37:44 +010043 STM32_CRYP1_BASE, /* 09 cryp1 */
Patrick Delaunay43f214c2019-07-05 17:20:15 +020044 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 */
Patrick Delaunayfac5ba82020-02-12 19:37:44 +010096 STM32_FDCAN_BASE, /* 3E tt_fdcan */
Patrick Delaunay43f214c2019-07-05 17:20:15 +020097 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 */
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100109 STM32_CRYP2_BASE, /* 4B cryp2 */
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200110 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 */
130};
131
132/* fdt helper */
133static bool fdt_disable_subnode_by_address(void *fdt, int offset, u32 addr)
134{
135 int node;
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100136 fdt_addr_t regs;
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200137
138 for (node = fdt_first_subnode(fdt, offset);
139 node >= 0;
140 node = fdt_next_subnode(fdt, node)) {
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100141 regs = fdtdec_get_addr(fdt, node, "reg");
142 if (addr == regs) {
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200143 if (fdtdec_get_is_enabled(fdt, node)) {
144 fdt_status_disabled(fdt, node);
145
146 return true;
147 }
148 return false;
149 }
150 }
151
152 return false;
153}
154
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100155static int stm32_fdt_fixup_etzpc(void *fdt, int soc_node)
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200156{
157 const u32 *array;
158 int array_size, i;
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100159 int offset, shift;
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200160 u32 addr, status, decprot[ETZPC_DECPROT_NB];
161
162 array = stm32mp1_ip_addr;
163 array_size = ARRAY_SIZE(stm32mp1_ip_addr);
164
165 for (i = 0; i < ETZPC_DECPROT_NB; i++)
166 decprot[i] = readl(ETZPC_DECPROT(i));
167
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200168 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;
172 addr = array[i];
173
174 debug("ETZPC: 0x%08x decprot %d=%d\n", addr, i, status);
175
176 if (addr == ETZPC_RESERVED ||
177 status == DECPROT_NON_SECURED)
178 continue;
179
180 if (fdt_disable_subnode_by_address(fdt, soc_node, addr))
181 printf("ETZPC: 0x%08x node disabled, decprot %d=%d\n",
182 addr, i, status);
183 }
184
185 return 0;
186}
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200187
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100188/* deactivate all the cpu except core 0 */
189static void stm32_fdt_fixup_cpu(void *blob, char *name)
190{
191 int off;
192 u32 reg;
193
194 off = fdt_path_offset(blob, "/cpus");
195 if (off < 0) {
196 printf("%s: couldn't find /cpus node\n", __func__);
197 return;
198 }
199
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");
203 if (reg != 0) {
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 */
207 off = -1;
208 }
209 off = fdt_node_offset_by_prop_value(blob, off,
210 "device_type", "cpu", 4);
211 }
212}
213
214static void stm32_fdt_disable(void *fdt, int offset, u32 addr,
215 const char *string, const char *name)
216{
217 if (fdt_disable_subnode_by_address(fdt, offset, addr))
218 printf("FDT: %s@%08x node disabled for %s\n",
219 string, addr, name);
220}
221
Patrick Delaunayd1633b32020-03-18 09:22:48 +0100222static void stm32_fdt_disable_optee(void *blob)
223{
224 int off, node;
225
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);
229
230 /* Disabled "optee@..." reserved-memory node */
231 off = fdt_path_offset(blob, "/reserved-memory/");
232 if (off < 0)
233 return;
234 for (node = fdt_first_subnode(blob, off);
235 node >= 0;
236 node = fdt_next_subnode(blob, node)) {
237 if (!strncmp(fdt_get_name(blob, node, NULL), "optee@", 6))
238 fdt_status_disabled(blob, node);
239 }
240}
241
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200242/*
243 * This function is called right before the kernel is booted. "blob" is the
244 * device tree that will be passed to the kernel.
245 */
246int ft_system_setup(void *blob, bd_t *bd)
247{
248 int ret = 0;
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100249 int soc;
250 u32 pkg, cpu;
251 char name[SOC_NAME_SIZE];
252
253 soc = fdt_path_offset(blob, "/soc");
254 if (soc < 0)
255 return soc;
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200256
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200257 if (CONFIG_IS_ENABLED(STM32_ETZPC)) {
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100258 ret = stm32_fdt_fixup_etzpc(blob, soc);
Patrick Delaunay43f214c2019-07-05 17:20:15 +0200259 if (ret)
260 return ret;
261 }
262
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100263 /* MPUs Part Numbers and name*/
264 cpu = get_cpu_type();
265 get_soc_name(name);
266
267 switch (cpu) {
Patrick Delaunaydb33b0e2020-02-26 11:26:43 +0100268 case CPU_STM32MP151Fxx:
269 case CPU_STM32MP151Dxx:
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100270 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);
276 /* fall through */
Patrick Delaunaydb33b0e2020-02-26 11:26:43 +0100277 case CPU_STM32MP153Fxx:
278 case CPU_STM32MP153Dxx:
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100279 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);
283 break;
284 default:
285 break;
286 }
287
288 switch (cpu) {
Patrick Delaunaydb33b0e2020-02-26 11:26:43 +0100289 case CPU_STM32MP157Dxx:
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100290 case CPU_STM32MP157Axx:
Patrick Delaunaydb33b0e2020-02-26 11:26:43 +0100291 case CPU_STM32MP153Dxx:
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100292 case CPU_STM32MP153Axx:
Patrick Delaunaydb33b0e2020-02-26 11:26:43 +0100293 case CPU_STM32MP151Dxx:
Patrick Delaunayfac5ba82020-02-12 19:37:44 +0100294 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);
297 break;
298 default:
299 break;
300 }
301
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200302 switch (get_cpu_package()) {
303 case PKG_AA_LBGA448:
304 pkg = STM32MP_PKG_AA;
305 break;
306 case PKG_AB_LBGA354:
307 pkg = STM32MP_PKG_AB;
308 break;
309 case PKG_AC_TFBGA361:
310 pkg = STM32MP_PKG_AC;
311 break;
312 case PKG_AD_TFBGA257:
313 pkg = STM32MP_PKG_AD;
314 break;
315 default:
316 pkg = 0;
317 break;
318 }
319 if (pkg) {
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);
324 }
325
Patrick Delaunay472407a2020-03-18 09:22:49 +0100326 if (!CONFIG_IS_ENABLED(OPTEE) ||
327 !tee_find_device(NULL, NULL, NULL, NULL))
Patrick Delaunayd1633b32020-03-18 09:22:48 +0100328 stm32_fdt_disable_optee(blob);
329
Patrick Delaunaya6f03912019-07-05 17:20:14 +0200330 return ret;
331}