Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / arch / arm / mach-stm32mp / fdt.c
1 // 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>
8 #include <log.h>
9 #include <tee.h>
10 #include <asm/arch/sys_proto.h>
11 #include <dt-bindings/pinctrl/stm32-pinfunc.h>
12 #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
28 #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
34 static 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 */
44         STM32_CRYP1_BASE,       /* 09 cryp1 */
45         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 */
97         STM32_FDCAN_BASE,       /* 3E tt_fdcan */
98         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 */
110         STM32_CRYP2_BASE,       /* 4B cryp2 */
111         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 */
134 static bool fdt_disable_subnode_by_address(void *fdt, int offset, u32 addr)
135 {
136         int node;
137         fdt_addr_t regs;
138
139         for (node = fdt_first_subnode(fdt, offset);
140              node >= 0;
141              node = fdt_next_subnode(fdt, node)) {
142                 regs = fdtdec_get_addr(fdt, node, "reg");
143                 if (addr == regs) {
144                         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
156 static int stm32_fdt_fixup_etzpc(void *fdt, int soc_node)
157 {
158         const u32 *array;
159         int array_size, i;
160         int offset, shift;
161         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
169         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 }
188
189 /* deactivate all the cpu except core 0 */
190 static 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
215 static 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
223 static void stm32_fdt_disable_optee(void *blob)
224 {
225         int off, node;
226
227         off = fdt_node_offset_by_compatible(blob, -1, "linaro,optee-tz");
228         if (off >= 0 && fdtdec_get_is_enabled(blob, off))
229                 fdt_status_disabled(blob, off);
230
231         /* Disabled "optee@..." reserved-memory node */
232         off = fdt_path_offset(blob, "/reserved-memory/");
233         if (off < 0)
234                 return;
235         for (node = fdt_first_subnode(blob, off);
236              node >= 0;
237              node = fdt_next_subnode(blob, node)) {
238                 if (!strncmp(fdt_get_name(blob, node, NULL), "optee@", 6))
239                         fdt_status_disabled(blob, node);
240         }
241 }
242
243 /*
244  * This function is called right before the kernel is booted. "blob" is the
245  * device tree that will be passed to the kernel.
246  */
247 int ft_system_setup(void *blob, bd_t *bd)
248 {
249         int ret = 0;
250         int soc;
251         u32 pkg, cpu;
252         char name[SOC_NAME_SIZE];
253
254         soc = fdt_path_offset(blob, "/soc");
255         if (soc < 0)
256                 return soc;
257
258         if (CONFIG_IS_ENABLED(STM32_ETZPC)) {
259                 ret = stm32_fdt_fixup_etzpc(blob, soc);
260                 if (ret)
261                         return ret;
262         }
263
264         /* MPUs Part Numbers and name*/
265         cpu = get_cpu_type();
266         get_soc_name(name);
267
268         switch (cpu) {
269         case CPU_STM32MP151Fxx:
270         case CPU_STM32MP151Dxx:
271         case CPU_STM32MP151Cxx:
272         case CPU_STM32MP151Axx:
273                 stm32_fdt_fixup_cpu(blob, name);
274                 /* after cpu delete we can't trust the soc offsets anymore */
275                 soc = fdt_path_offset(blob, "/soc");
276                 stm32_fdt_disable(blob, soc, STM32_FDCAN_BASE, "can", name);
277                 /* fall through */
278         case CPU_STM32MP153Fxx:
279         case CPU_STM32MP153Dxx:
280         case CPU_STM32MP153Cxx:
281         case CPU_STM32MP153Axx:
282                 stm32_fdt_disable(blob, soc, STM32_GPU_BASE, "gpu", name);
283                 stm32_fdt_disable(blob, soc, STM32_DSI_BASE, "dsi", name);
284                 break;
285         default:
286                 break;
287         }
288
289         switch (cpu) {
290         case CPU_STM32MP157Dxx:
291         case CPU_STM32MP157Axx:
292         case CPU_STM32MP153Dxx:
293         case CPU_STM32MP153Axx:
294         case CPU_STM32MP151Dxx:
295         case CPU_STM32MP151Axx:
296                 stm32_fdt_disable(blob, soc, STM32_CRYP1_BASE, "cryp", name);
297                 stm32_fdt_disable(blob, soc, STM32_CRYP2_BASE, "cryp", name);
298                 break;
299         default:
300                 break;
301         }
302
303         switch (get_cpu_package()) {
304         case PKG_AA_LBGA448:
305                 pkg = STM32MP_PKG_AA;
306                 break;
307         case PKG_AB_LBGA354:
308                 pkg = STM32MP_PKG_AB;
309                 break;
310         case PKG_AC_TFBGA361:
311                 pkg = STM32MP_PKG_AC;
312                 break;
313         case PKG_AD_TFBGA257:
314                 pkg = STM32MP_PKG_AD;
315                 break;
316         default:
317                 pkg = 0;
318                 break;
319         }
320         if (pkg) {
321                 do_fixup_by_compat_u32(blob, "st,stm32mp157-pinctrl",
322                                        "st,package", pkg, false);
323                 do_fixup_by_compat_u32(blob, "st,stm32mp157-z-pinctrl",
324                                        "st,package", pkg, false);
325         }
326
327         if (!CONFIG_IS_ENABLED(OPTEE) ||
328             !tee_find_device(NULL, NULL, NULL, NULL))
329                 stm32_fdt_disable_optee(blob);
330
331         return ret;
332 }