stm32mp1: update boot mode management
[platform/kernel/u-boot.git] / arch / arm / mach-stm32mp / cpu.c
1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3  * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4  */
5 #include <common.h>
6 #include <clk.h>
7 #include <debug_uart.h>
8 #include <environment.h>
9 #include <misc.h>
10 #include <asm/io.h>
11 #include <asm/arch/stm32.h>
12 #include <asm/arch/sys_proto.h>
13 #include <dm/device.h>
14 #include <dm/uclass.h>
15
16 /* RCC register */
17 #define RCC_TZCR                (STM32_RCC_BASE + 0x00)
18 #define RCC_DBGCFGR             (STM32_RCC_BASE + 0x080C)
19 #define RCC_BDCR                (STM32_RCC_BASE + 0x0140)
20 #define RCC_MP_APB5ENSETR       (STM32_RCC_BASE + 0x0208)
21 #define RCC_BDCR_VSWRST         BIT(31)
22 #define RCC_BDCR_RTCSRC         GENMASK(17, 16)
23 #define RCC_DBGCFGR_DBGCKEN     BIT(8)
24
25 /* Security register */
26 #define ETZPC_TZMA1_SIZE        (STM32_ETZPC_BASE + 0x04)
27 #define ETZPC_DECPROT0          (STM32_ETZPC_BASE + 0x10)
28
29 #define TZC_GATE_KEEPER         (STM32_TZC_BASE + 0x008)
30 #define TZC_REGION_ATTRIBUTE0   (STM32_TZC_BASE + 0x110)
31 #define TZC_REGION_ID_ACCESS0   (STM32_TZC_BASE + 0x114)
32
33 #define TAMP_CR1                (STM32_TAMP_BASE + 0x00)
34
35 #define PWR_CR1                 (STM32_PWR_BASE + 0x00)
36 #define PWR_CR1_DBP             BIT(8)
37
38 /* DBGMCU register */
39 #define DBGMCU_IDC              (STM32_DBGMCU_BASE + 0x00)
40 #define DBGMCU_APB4FZ1          (STM32_DBGMCU_BASE + 0x2C)
41 #define DBGMCU_APB4FZ1_IWDG2    BIT(2)
42 #define DBGMCU_IDC_DEV_ID_MASK  GENMASK(11, 0)
43 #define DBGMCU_IDC_DEV_ID_SHIFT 0
44 #define DBGMCU_IDC_REV_ID_MASK  GENMASK(31, 16)
45 #define DBGMCU_IDC_REV_ID_SHIFT 16
46
47 /* boot interface from Bootrom
48  * - boot instance = bit 31:16
49  * - boot device = bit 15:0
50  */
51 #define BOOTROM_PARAM_ADDR      0x2FFC0078
52 #define BOOTROM_MODE_MASK       GENMASK(15, 0)
53 #define BOOTROM_MODE_SHIFT      0
54 #define BOOTROM_INSTANCE_MASK    GENMASK(31, 16)
55 #define BOOTROM_INSTANCE_SHIFT  16
56
57 /* BSEC OTP index */
58 #define BSEC_OTP_SERIAL 13
59 #define BSEC_OTP_MAC    57
60
61 #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
62 #ifndef CONFIG_STM32MP1_TRUSTED
63 static void security_init(void)
64 {
65         /* Disable the backup domain write protection */
66         /* the protection is enable at each reset by hardware */
67         /* And must be disable by software */
68         setbits_le32(PWR_CR1, PWR_CR1_DBP);
69
70         while (!(readl(PWR_CR1) & PWR_CR1_DBP))
71                 ;
72
73         /* If RTC clock isn't enable so this is a cold boot then we need
74          * to reset the backup domain
75          */
76         if (!(readl(RCC_BDCR) & RCC_BDCR_RTCSRC)) {
77                 setbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
78                 while (!(readl(RCC_BDCR) & RCC_BDCR_VSWRST))
79                         ;
80                 clrbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
81         }
82
83         /* allow non secure access in Write/Read for all peripheral */
84         writel(GENMASK(25, 0), ETZPC_DECPROT0);
85
86         /* Open SYSRAM for no secure access */
87         writel(0x0, ETZPC_TZMA1_SIZE);
88
89         /* enable TZC1 TZC2 clock */
90         writel(BIT(11) | BIT(12), RCC_MP_APB5ENSETR);
91
92         /* Region 0 set to no access by default */
93         /* bit 0 / 16 => nsaid0 read/write Enable
94          * bit 1 / 17 => nsaid1 read/write Enable
95          * ...
96          * bit 15 / 31 => nsaid15 read/write Enable
97          */
98         writel(0xFFFFFFFF, TZC_REGION_ID_ACCESS0);
99         /* bit 30 / 31 => Secure Global Enable : write/read */
100         /* bit 0 / 1 => Region Enable for filter 0/1 */
101         writel(BIT(0) | BIT(1) | BIT(30) | BIT(31), TZC_REGION_ATTRIBUTE0);
102
103         /* Enable Filter 0 and 1 */
104         setbits_le32(TZC_GATE_KEEPER, BIT(0) | BIT(1));
105
106         /* RCC trust zone deactivated */
107         writel(0x0, RCC_TZCR);
108
109         /* TAMP: deactivate the internal tamper
110          * Bit 23 ITAMP8E: monotonic counter overflow
111          * Bit 20 ITAMP5E: RTC calendar overflow
112          * Bit 19 ITAMP4E: HSE monitoring
113          * Bit 18 ITAMP3E: LSE monitoring
114          * Bit 16 ITAMP1E: RTC power domain supply monitoring
115          */
116         writel(0x0, TAMP_CR1);
117 }
118 #endif /* CONFIG_STM32MP1_TRUSTED */
119
120 /*
121  * Debug init
122  */
123 static void dbgmcu_init(void)
124 {
125         setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
126
127         /* Freeze IWDG2 if Cortex-A7 is in debug mode */
128         setbits_le32(DBGMCU_APB4FZ1, DBGMCU_APB4FZ1_IWDG2);
129 }
130 #endif /* !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD) */
131
132 #if !defined(CONFIG_STM32MP1_TRUSTED) && \
133         (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD))
134 /* get bootmode from ROM code boot context: saved in TAMP register */
135 static void update_bootmode(void)
136 {
137         u32 boot_mode;
138         u32 bootrom_itf = readl(BOOTROM_PARAM_ADDR);
139         u32 bootrom_device, bootrom_instance;
140
141         /* enable TAMP clock = RTCAPBEN */
142         writel(BIT(8), RCC_MP_APB5ENSETR);
143
144         /* read bootrom context */
145         bootrom_device =
146                 (bootrom_itf & BOOTROM_MODE_MASK) >> BOOTROM_MODE_SHIFT;
147         bootrom_instance =
148                 (bootrom_itf & BOOTROM_INSTANCE_MASK) >> BOOTROM_INSTANCE_SHIFT;
149         boot_mode =
150                 ((bootrom_device << BOOT_TYPE_SHIFT) & BOOT_TYPE_MASK) |
151                 ((bootrom_instance << BOOT_INSTANCE_SHIFT) &
152                  BOOT_INSTANCE_MASK);
153
154         /* save the boot mode in TAMP backup register */
155         clrsetbits_le32(TAMP_BOOT_CONTEXT,
156                         TAMP_BOOT_MODE_MASK,
157                         boot_mode << TAMP_BOOT_MODE_SHIFT);
158 }
159 #endif
160
161 u32 get_bootmode(void)
162 {
163         /* read bootmode from TAMP backup register */
164         return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >>
165                     TAMP_BOOT_MODE_SHIFT;
166 }
167
168 /*
169  * Early system init
170  */
171 int arch_cpu_init(void)
172 {
173         u32 boot_mode;
174
175         /* early armv7 timer init: needed for polling */
176         timer_init();
177
178 #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
179         dbgmcu_init();
180 #ifndef CONFIG_STM32MP1_TRUSTED
181         security_init();
182         update_bootmode();
183 #endif
184 #endif
185
186         boot_mode = get_bootmode();
187
188         if ((boot_mode & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_UART)
189                 gd->flags |= GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE;
190 #if defined(CONFIG_DEBUG_UART) && \
191         !defined(CONFIG_STM32MP1_TRUSTED) && \
192         (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD))
193         else
194                 debug_uart_init();
195 #endif
196
197         return 0;
198 }
199
200 void enable_caches(void)
201 {
202         /* Enable D-cache. I-cache is already enabled in start.S */
203         dcache_enable();
204 }
205
206 static u32 read_idc(void)
207 {
208         setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
209
210         return readl(DBGMCU_IDC);
211 }
212
213 u32 get_cpu_rev(void)
214 {
215         return (read_idc() & DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT;
216 }
217
218 u32 get_cpu_type(void)
219 {
220         return (read_idc() & DBGMCU_IDC_DEV_ID_MASK) >> DBGMCU_IDC_DEV_ID_SHIFT;
221 }
222
223 #if defined(CONFIG_DISPLAY_CPUINFO)
224 int print_cpuinfo(void)
225 {
226         char *cpu_s, *cpu_r;
227
228         switch (get_cpu_type()) {
229         case CPU_STMP32MP15x:
230                 cpu_s = "15x";
231                 break;
232         default:
233                 cpu_s = "?";
234                 break;
235         }
236
237         switch (get_cpu_rev()) {
238         case CPU_REVA:
239                 cpu_r = "A";
240                 break;
241         case CPU_REVB:
242                 cpu_r = "B";
243                 break;
244         default:
245                 cpu_r = "?";
246                 break;
247         }
248
249         printf("CPU: STM32MP%s.%s\n", cpu_s, cpu_r);
250
251         return 0;
252 }
253 #endif /* CONFIG_DISPLAY_CPUINFO */
254
255 static void setup_boot_mode(void)
256 {
257         const u32 serial_addr[] = {
258                 STM32_USART1_BASE,
259                 STM32_USART2_BASE,
260                 STM32_USART3_BASE,
261                 STM32_UART4_BASE,
262                 STM32_UART5_BASE,
263                 STM32_USART6_BASE,
264                 STM32_UART7_BASE,
265                 STM32_UART8_BASE
266         };
267         char cmd[60];
268         u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
269         u32 boot_mode =
270                 (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
271         int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
272         struct udevice *dev;
273         int alias;
274
275         pr_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d\n",
276                  __func__, boot_ctx, boot_mode, instance);
277
278         switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
279         case BOOT_SERIAL_UART:
280                 if (instance > ARRAY_SIZE(serial_addr))
281                         break;
282                 /* serial : search associated alias in devicetree */
283                 sprintf(cmd, "serial@%x", serial_addr[instance]);
284                 if (uclass_get_device_by_name(UCLASS_SERIAL, cmd, &dev))
285                         break;
286                 if (fdtdec_get_alias_seq(gd->fdt_blob, "serial",
287                                          dev_of_offset(dev), &alias))
288                         break;
289                 sprintf(cmd, "%d", alias);
290                 env_set("boot_device", "serial");
291                 env_set("boot_instance", cmd);
292
293                 /* restore console on uart when not used */
294                 if (gd->cur_serial_dev != dev) {
295                         gd->flags &= ~(GD_FLG_SILENT |
296                                        GD_FLG_DISABLE_CONSOLE);
297                         printf("serial boot with console enabled!\n");
298                 }
299                 break;
300         case BOOT_SERIAL_USB:
301                 env_set("boot_device", "usb");
302                 env_set("boot_instance", "0");
303                 break;
304         case BOOT_FLASH_SD:
305         case BOOT_FLASH_EMMC:
306                 sprintf(cmd, "%d", instance);
307                 env_set("boot_device", "mmc");
308                 env_set("boot_instance", cmd);
309                 break;
310         case BOOT_FLASH_NAND:
311                 env_set("boot_device", "nand");
312                 env_set("boot_instance", "0");
313                 break;
314         case BOOT_FLASH_NOR:
315                 env_set("boot_device", "nor");
316                 env_set("boot_instance", "0");
317                 break;
318         default:
319                 pr_debug("unexpected boot mode = %x\n", boot_mode);
320                 break;
321         }
322 }
323
324 /*
325  * If there is no MAC address in the environment, then it will be initialized
326  * (silently) from the value in the OTP.
327  */
328 static int setup_mac_address(void)
329 {
330 #if defined(CONFIG_NET)
331         int ret;
332         int i;
333         u32 otp[2];
334         uchar enetaddr[6];
335         struct udevice *dev;
336
337         /* MAC already in environment */
338         if (eth_env_get_enetaddr("ethaddr", enetaddr))
339                 return 0;
340
341         ret = uclass_get_device_by_driver(UCLASS_MISC,
342                                           DM_GET_DRIVER(stm32mp_bsec),
343                                           &dev);
344         if (ret)
345                 return ret;
346
347         ret = misc_read(dev, BSEC_OTP_MAC * 4 + STM32_BSEC_OTP_OFFSET,
348                         otp, sizeof(otp));
349         if (ret < 0)
350                 return ret;
351
352         for (i = 0; i < 6; i++)
353                 enetaddr[i] = ((uint8_t *)&otp)[i];
354
355         if (!is_valid_ethaddr(enetaddr)) {
356                 pr_err("invalid MAC address in OTP %pM", enetaddr);
357                 return -EINVAL;
358         }
359         pr_debug("OTP MAC address = %pM\n", enetaddr);
360         ret = !eth_env_set_enetaddr("ethaddr", enetaddr);
361         if (!ret)
362                 pr_err("Failed to set mac address %pM from OTP: %d\n",
363                        enetaddr, ret);
364 #endif
365
366         return 0;
367 }
368
369 static int setup_serial_number(void)
370 {
371         char serial_string[25];
372         u32 otp[3] = {0, 0, 0 };
373         struct udevice *dev;
374         int ret;
375
376         if (env_get("serial#"))
377                 return 0;
378
379         ret = uclass_get_device_by_driver(UCLASS_MISC,
380                                           DM_GET_DRIVER(stm32mp_bsec),
381                                           &dev);
382         if (ret)
383                 return ret;
384
385         ret = misc_read(dev, BSEC_OTP_SERIAL * 4 + STM32_BSEC_OTP_OFFSET,
386                         otp, sizeof(otp));
387         if (ret < 0)
388                 return ret;
389
390         sprintf(serial_string, "%08x%08x%08x", otp[0], otp[1], otp[2]);
391         env_set("serial#", serial_string);
392
393         return 0;
394 }
395
396 int arch_misc_init(void)
397 {
398         setup_boot_mode();
399         setup_mac_address();
400         setup_serial_number();
401
402         return 0;
403 }