stm32mp: add the command stm32prog
[oweals/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 <cpu_func.h>
8 #include <debug_uart.h>
9 #include <env.h>
10 #include <misc.h>
11 #include <asm/io.h>
12 #include <asm/arch/stm32.h>
13 #include <asm/arch/sys_proto.h>
14 #include <dm/device.h>
15 #include <dm/uclass.h>
16
17 /* RCC register */
18 #define RCC_TZCR                (STM32_RCC_BASE + 0x00)
19 #define RCC_DBGCFGR             (STM32_RCC_BASE + 0x080C)
20 #define RCC_BDCR                (STM32_RCC_BASE + 0x0140)
21 #define RCC_MP_APB5ENSETR       (STM32_RCC_BASE + 0x0208)
22 #define RCC_MP_AHB5ENSETR       (STM32_RCC_BASE + 0x0210)
23 #define RCC_BDCR_VSWRST         BIT(31)
24 #define RCC_BDCR_RTCSRC         GENMASK(17, 16)
25 #define RCC_DBGCFGR_DBGCKEN     BIT(8)
26
27 /* Security register */
28 #define ETZPC_TZMA1_SIZE        (STM32_ETZPC_BASE + 0x04)
29 #define ETZPC_DECPROT0          (STM32_ETZPC_BASE + 0x10)
30
31 #define TZC_GATE_KEEPER         (STM32_TZC_BASE + 0x008)
32 #define TZC_REGION_ATTRIBUTE0   (STM32_TZC_BASE + 0x110)
33 #define TZC_REGION_ID_ACCESS0   (STM32_TZC_BASE + 0x114)
34
35 #define TAMP_CR1                (STM32_TAMP_BASE + 0x00)
36
37 #define PWR_CR1                 (STM32_PWR_BASE + 0x00)
38 #define PWR_MCUCR               (STM32_PWR_BASE + 0x14)
39 #define PWR_CR1_DBP             BIT(8)
40 #define PWR_MCUCR_SBF           BIT(6)
41
42 /* DBGMCU register */
43 #define DBGMCU_IDC              (STM32_DBGMCU_BASE + 0x00)
44 #define DBGMCU_APB4FZ1          (STM32_DBGMCU_BASE + 0x2C)
45 #define DBGMCU_APB4FZ1_IWDG2    BIT(2)
46 #define DBGMCU_IDC_DEV_ID_MASK  GENMASK(11, 0)
47 #define DBGMCU_IDC_DEV_ID_SHIFT 0
48 #define DBGMCU_IDC_REV_ID_MASK  GENMASK(31, 16)
49 #define DBGMCU_IDC_REV_ID_SHIFT 16
50
51 /* GPIOZ registers */
52 #define GPIOZ_SECCFGR           0x54004030
53
54 /* boot interface from Bootrom
55  * - boot instance = bit 31:16
56  * - boot device = bit 15:0
57  */
58 #define BOOTROM_PARAM_ADDR      0x2FFC0078
59 #define BOOTROM_MODE_MASK       GENMASK(15, 0)
60 #define BOOTROM_MODE_SHIFT      0
61 #define BOOTROM_INSTANCE_MASK    GENMASK(31, 16)
62 #define BOOTROM_INSTANCE_SHIFT  16
63
64 /* Device Part Number (RPN) = OTP_DATA1 lower 8 bits */
65 #define RPN_SHIFT       0
66 #define RPN_MASK        GENMASK(7, 0)
67
68 /* Package = bit 27:29 of OTP16
69  * - 100: LBGA448 (FFI) => AA = LFBGA 18x18mm 448 balls p. 0.8mm
70  * - 011: LBGA354 (LCI) => AB = LFBGA 16x16mm 359 balls p. 0.8mm
71  * - 010: TFBGA361 (FFC) => AC = TFBGA 12x12mm 361 balls p. 0.5mm
72  * - 001: TFBGA257 (LCC) => AD = TFBGA 10x10mm 257 balls p. 0.5mm
73  * - others: Reserved
74  */
75 #define PKG_SHIFT       27
76 #define PKG_MASK        GENMASK(2, 0)
77
78 #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
79 #ifndef CONFIG_TFABOOT
80 static void security_init(void)
81 {
82         /* Disable the backup domain write protection */
83         /* the protection is enable at each reset by hardware */
84         /* And must be disable by software */
85         setbits_le32(PWR_CR1, PWR_CR1_DBP);
86
87         while (!(readl(PWR_CR1) & PWR_CR1_DBP))
88                 ;
89
90         /* If RTC clock isn't enable so this is a cold boot then we need
91          * to reset the backup domain
92          */
93         if (!(readl(RCC_BDCR) & RCC_BDCR_RTCSRC)) {
94                 setbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
95                 while (!(readl(RCC_BDCR) & RCC_BDCR_VSWRST))
96                         ;
97                 clrbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
98         }
99
100         /* allow non secure access in Write/Read for all peripheral */
101         writel(GENMASK(25, 0), ETZPC_DECPROT0);
102
103         /* Open SYSRAM for no secure access */
104         writel(0x0, ETZPC_TZMA1_SIZE);
105
106         /* enable TZC1 TZC2 clock */
107         writel(BIT(11) | BIT(12), RCC_MP_APB5ENSETR);
108
109         /* Region 0 set to no access by default */
110         /* bit 0 / 16 => nsaid0 read/write Enable
111          * bit 1 / 17 => nsaid1 read/write Enable
112          * ...
113          * bit 15 / 31 => nsaid15 read/write Enable
114          */
115         writel(0xFFFFFFFF, TZC_REGION_ID_ACCESS0);
116         /* bit 30 / 31 => Secure Global Enable : write/read */
117         /* bit 0 / 1 => Region Enable for filter 0/1 */
118         writel(BIT(0) | BIT(1) | BIT(30) | BIT(31), TZC_REGION_ATTRIBUTE0);
119
120         /* Enable Filter 0 and 1 */
121         setbits_le32(TZC_GATE_KEEPER, BIT(0) | BIT(1));
122
123         /* RCC trust zone deactivated */
124         writel(0x0, RCC_TZCR);
125
126         /* TAMP: deactivate the internal tamper
127          * Bit 23 ITAMP8E: monotonic counter overflow
128          * Bit 20 ITAMP5E: RTC calendar overflow
129          * Bit 19 ITAMP4E: HSE monitoring
130          * Bit 18 ITAMP3E: LSE monitoring
131          * Bit 16 ITAMP1E: RTC power domain supply monitoring
132          */
133         writel(0x0, TAMP_CR1);
134
135         /* GPIOZ: deactivate the security */
136         writel(BIT(0), RCC_MP_AHB5ENSETR);
137         writel(0x0, GPIOZ_SECCFGR);
138 }
139 #endif /* CONFIG_TFABOOT */
140
141 /*
142  * Debug init
143  */
144 static void dbgmcu_init(void)
145 {
146         setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
147
148         /* Freeze IWDG2 if Cortex-A7 is in debug mode */
149         setbits_le32(DBGMCU_APB4FZ1, DBGMCU_APB4FZ1_IWDG2);
150 }
151 #endif /* !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD) */
152
153 #if !defined(CONFIG_TFABOOT) && \
154         (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD))
155 /* get bootmode from ROM code boot context: saved in TAMP register */
156 static void update_bootmode(void)
157 {
158         u32 boot_mode;
159         u32 bootrom_itf = readl(BOOTROM_PARAM_ADDR);
160         u32 bootrom_device, bootrom_instance;
161
162         /* enable TAMP clock = RTCAPBEN */
163         writel(BIT(8), RCC_MP_APB5ENSETR);
164
165         /* read bootrom context */
166         bootrom_device =
167                 (bootrom_itf & BOOTROM_MODE_MASK) >> BOOTROM_MODE_SHIFT;
168         bootrom_instance =
169                 (bootrom_itf & BOOTROM_INSTANCE_MASK) >> BOOTROM_INSTANCE_SHIFT;
170         boot_mode =
171                 ((bootrom_device << BOOT_TYPE_SHIFT) & BOOT_TYPE_MASK) |
172                 ((bootrom_instance << BOOT_INSTANCE_SHIFT) &
173                  BOOT_INSTANCE_MASK);
174
175         /* save the boot mode in TAMP backup register */
176         clrsetbits_le32(TAMP_BOOT_CONTEXT,
177                         TAMP_BOOT_MODE_MASK,
178                         boot_mode << TAMP_BOOT_MODE_SHIFT);
179 }
180 #endif
181
182 u32 get_bootmode(void)
183 {
184         /* read bootmode from TAMP backup register */
185         return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >>
186                     TAMP_BOOT_MODE_SHIFT;
187 }
188
189 /*
190  * Early system init
191  */
192 int arch_cpu_init(void)
193 {
194         u32 boot_mode;
195
196         /* early armv7 timer init: needed for polling */
197         timer_init();
198
199 #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
200         dbgmcu_init();
201 #ifndef CONFIG_TFABOOT
202         security_init();
203         update_bootmode();
204 #endif
205         /* Reset Coprocessor state unless it wakes up from Standby power mode */
206         if (!(readl(PWR_MCUCR) & PWR_MCUCR_SBF)) {
207                 writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
208                 writel(0, TAMP_COPRO_RSC_TBL_ADDRESS);
209         }
210 #endif
211
212         boot_mode = get_bootmode();
213
214         if ((boot_mode & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_UART)
215                 gd->flags |= GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE;
216 #if defined(CONFIG_DEBUG_UART) && \
217         !defined(CONFIG_TFABOOT) && \
218         (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD))
219         else
220                 debug_uart_init();
221 #endif
222
223         return 0;
224 }
225
226 void enable_caches(void)
227 {
228         /* Enable D-cache. I-cache is already enabled in start.S */
229         dcache_enable();
230 }
231
232 static u32 read_idc(void)
233 {
234         setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
235
236         return readl(DBGMCU_IDC);
237 }
238
239 u32 get_cpu_dev(void)
240 {
241         return (read_idc() & DBGMCU_IDC_DEV_ID_MASK) >> DBGMCU_IDC_DEV_ID_SHIFT;
242 }
243
244 u32 get_cpu_rev(void)
245 {
246         return (read_idc() & DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT;
247 }
248
249 static u32 get_otp(int index, int shift, int mask)
250 {
251         int ret;
252         struct udevice *dev;
253         u32 otp = 0;
254
255         ret = uclass_get_device_by_driver(UCLASS_MISC,
256                                           DM_GET_DRIVER(stm32mp_bsec),
257                                           &dev);
258
259         if (!ret)
260                 ret = misc_read(dev, STM32_BSEC_SHADOW(index),
261                                 &otp, sizeof(otp));
262
263         return (otp >> shift) & mask;
264 }
265
266 /* Get Device Part Number (RPN) from OTP */
267 static u32 get_cpu_rpn(void)
268 {
269         return get_otp(BSEC_OTP_RPN, RPN_SHIFT, RPN_MASK);
270 }
271
272 u32 get_cpu_type(void)
273 {
274         return (get_cpu_dev() << 16) | get_cpu_rpn();
275 }
276
277 /* Get Package options from OTP */
278 u32 get_cpu_package(void)
279 {
280         return get_otp(BSEC_OTP_PKG, PKG_SHIFT, PKG_MASK);
281 }
282
283 void get_soc_name(char name[SOC_NAME_SIZE])
284 {
285         char *cpu_s, *cpu_r, *pkg;
286
287         /* MPUs Part Numbers */
288         switch (get_cpu_type()) {
289         case CPU_STM32MP157Fxx:
290                 cpu_s = "157F";
291                 break;
292         case CPU_STM32MP157Dxx:
293                 cpu_s = "157D";
294                 break;
295         case CPU_STM32MP157Cxx:
296                 cpu_s = "157C";
297                 break;
298         case CPU_STM32MP157Axx:
299                 cpu_s = "157A";
300                 break;
301         case CPU_STM32MP153Fxx:
302                 cpu_s = "153F";
303                 break;
304         case CPU_STM32MP153Dxx:
305                 cpu_s = "153D";
306                 break;
307         case CPU_STM32MP153Cxx:
308                 cpu_s = "153C";
309                 break;
310         case CPU_STM32MP153Axx:
311                 cpu_s = "153A";
312                 break;
313         case CPU_STM32MP151Fxx:
314                 cpu_s = "151F";
315                 break;
316         case CPU_STM32MP151Dxx:
317                 cpu_s = "151D";
318                 break;
319         case CPU_STM32MP151Cxx:
320                 cpu_s = "151C";
321                 break;
322         case CPU_STM32MP151Axx:
323                 cpu_s = "151A";
324                 break;
325         default:
326                 cpu_s = "????";
327                 break;
328         }
329
330         /* Package */
331         switch (get_cpu_package()) {
332         case PKG_AA_LBGA448:
333                 pkg = "AA";
334                 break;
335         case PKG_AB_LBGA354:
336                 pkg = "AB";
337                 break;
338         case PKG_AC_TFBGA361:
339                 pkg = "AC";
340                 break;
341         case PKG_AD_TFBGA257:
342                 pkg = "AD";
343                 break;
344         default:
345                 pkg = "??";
346                 break;
347         }
348
349         /* REVISION */
350         switch (get_cpu_rev()) {
351         case CPU_REVA:
352                 cpu_r = "A";
353                 break;
354         case CPU_REVB:
355                 cpu_r = "B";
356                 break;
357         case CPU_REVZ:
358                 cpu_r = "Z";
359                 break;
360         default:
361                 cpu_r = "?";
362                 break;
363         }
364
365         snprintf(name, SOC_NAME_SIZE, "STM32MP%s%s Rev.%s", cpu_s, pkg, cpu_r);
366 }
367
368 #if defined(CONFIG_DISPLAY_CPUINFO)
369 int print_cpuinfo(void)
370 {
371         char name[SOC_NAME_SIZE];
372
373         get_soc_name(name);
374         printf("CPU: %s\n", name);
375
376         return 0;
377 }
378 #endif /* CONFIG_DISPLAY_CPUINFO */
379
380 static void setup_boot_mode(void)
381 {
382         const u32 serial_addr[] = {
383                 STM32_USART1_BASE,
384                 STM32_USART2_BASE,
385                 STM32_USART3_BASE,
386                 STM32_UART4_BASE,
387                 STM32_UART5_BASE,
388                 STM32_USART6_BASE,
389                 STM32_UART7_BASE,
390                 STM32_UART8_BASE
391         };
392         char cmd[60];
393         u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
394         u32 boot_mode =
395                 (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
396         unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
397         u32 forced_mode = (boot_ctx & TAMP_BOOT_FORCED_MASK);
398         struct udevice *dev;
399         int alias;
400
401         pr_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d forced=%x\n",
402                  __func__, boot_ctx, boot_mode, instance, forced_mode);
403         switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
404         case BOOT_SERIAL_UART:
405                 if (instance > ARRAY_SIZE(serial_addr))
406                         break;
407                 /* serial : search associated alias in devicetree */
408                 sprintf(cmd, "serial@%x", serial_addr[instance]);
409                 if (uclass_get_device_by_name(UCLASS_SERIAL, cmd, &dev))
410                         break;
411                 if (fdtdec_get_alias_seq(gd->fdt_blob, "serial",
412                                          dev_of_offset(dev), &alias))
413                         break;
414                 sprintf(cmd, "%d", alias);
415                 env_set("boot_device", "serial");
416                 env_set("boot_instance", cmd);
417
418                 /* restore console on uart when not used */
419                 if (gd->cur_serial_dev != dev) {
420                         gd->flags &= ~(GD_FLG_SILENT |
421                                        GD_FLG_DISABLE_CONSOLE);
422                         printf("serial boot with console enabled!\n");
423                 }
424                 break;
425         case BOOT_SERIAL_USB:
426                 env_set("boot_device", "usb");
427                 env_set("boot_instance", "0");
428                 break;
429         case BOOT_FLASH_SD:
430         case BOOT_FLASH_EMMC:
431                 sprintf(cmd, "%d", instance);
432                 env_set("boot_device", "mmc");
433                 env_set("boot_instance", cmd);
434                 break;
435         case BOOT_FLASH_NAND:
436                 env_set("boot_device", "nand");
437                 env_set("boot_instance", "0");
438                 break;
439         case BOOT_FLASH_SPINAND:
440                 env_set("boot_device", "spi-nand");
441                 env_set("boot_instance", "0");
442                 break;
443         case BOOT_FLASH_NOR:
444                 env_set("boot_device", "nor");
445                 env_set("boot_instance", "0");
446                 break;
447         default:
448                 pr_debug("unexpected boot mode = %x\n", boot_mode);
449                 break;
450         }
451
452         switch (forced_mode) {
453         case BOOT_FASTBOOT:
454                 printf("Enter fastboot!\n");
455                 env_set("preboot", "env set preboot; fastboot 0");
456                 break;
457         case BOOT_STM32PROG:
458                 env_set("boot_device", "usb");
459                 env_set("boot_instance", "0");
460                 break;
461         case BOOT_UMS_MMC0:
462         case BOOT_UMS_MMC1:
463         case BOOT_UMS_MMC2:
464                 printf("Enter UMS!\n");
465                 instance = forced_mode - BOOT_UMS_MMC0;
466                 sprintf(cmd, "env set preboot; ums 0 mmc %d", instance);
467                 env_set("preboot", cmd);
468                 break;
469         case BOOT_RECOVERY:
470                 env_set("preboot", "env set preboot; run altbootcmd");
471                 break;
472         case BOOT_NORMAL:
473                 break;
474         default:
475                 pr_debug("unexpected forced boot mode = %x\n", forced_mode);
476                 break;
477         }
478
479         /* clear TAMP for next reboot */
480         clrsetbits_le32(TAMP_BOOT_CONTEXT, TAMP_BOOT_FORCED_MASK, BOOT_NORMAL);
481 }
482
483 /*
484  * If there is no MAC address in the environment, then it will be initialized
485  * (silently) from the value in the OTP.
486  */
487 __weak int setup_mac_address(void)
488 {
489 #if defined(CONFIG_NET)
490         int ret;
491         int i;
492         u32 otp[2];
493         uchar enetaddr[6];
494         struct udevice *dev;
495
496         /* MAC already in environment */
497         if (eth_env_get_enetaddr("ethaddr", enetaddr))
498                 return 0;
499
500         ret = uclass_get_device_by_driver(UCLASS_MISC,
501                                           DM_GET_DRIVER(stm32mp_bsec),
502                                           &dev);
503         if (ret)
504                 return ret;
505
506         ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_MAC),
507                         otp, sizeof(otp));
508         if (ret < 0)
509                 return ret;
510
511         for (i = 0; i < 6; i++)
512                 enetaddr[i] = ((uint8_t *)&otp)[i];
513
514         if (!is_valid_ethaddr(enetaddr)) {
515                 pr_err("invalid MAC address in OTP %pM\n", enetaddr);
516                 return -EINVAL;
517         }
518         pr_debug("OTP MAC address = %pM\n", enetaddr);
519         ret = !eth_env_set_enetaddr("ethaddr", enetaddr);
520         if (!ret)
521                 pr_err("Failed to set mac address %pM from OTP: %d\n",
522                        enetaddr, ret);
523 #endif
524
525         return 0;
526 }
527
528 static int setup_serial_number(void)
529 {
530         char serial_string[25];
531         u32 otp[3] = {0, 0, 0 };
532         struct udevice *dev;
533         int ret;
534
535         if (env_get("serial#"))
536                 return 0;
537
538         ret = uclass_get_device_by_driver(UCLASS_MISC,
539                                           DM_GET_DRIVER(stm32mp_bsec),
540                                           &dev);
541         if (ret)
542                 return ret;
543
544         ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_SERIAL),
545                         otp, sizeof(otp));
546         if (ret < 0)
547                 return ret;
548
549         sprintf(serial_string, "%08X%08X%08X", otp[0], otp[1], otp[2]);
550         env_set("serial#", serial_string);
551
552         return 0;
553 }
554
555 int arch_misc_init(void)
556 {
557         setup_boot_mode();
558         setup_mac_address();
559         setup_serial_number();
560
561         return 0;
562 }