]> Git Repo - u-boot.git/blob - arch/arm/mach-stm32mp/stm32mp1/cpu.c
Merge tag 'v2024.01-rc3' into next
[u-boot.git] / arch / arm / mach-stm32mp / stm32mp1 / cpu.c
1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3  * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4  */
5
6 #define LOG_CATEGORY LOGC_ARCH
7
8 #include <common.h>
9 #include <clk.h>
10 #include <cpu_func.h>
11 #include <debug_uart.h>
12 #include <env.h>
13 #include <init.h>
14 #include <log.h>
15 #include <lmb.h>
16 #include <misc.h>
17 #include <net.h>
18 #include <spl.h>
19 #include <asm/io.h>
20 #include <asm/arch/stm32.h>
21 #include <asm/arch/sys_proto.h>
22 #include <asm/global_data.h>
23 #include <dm/device.h>
24 #include <dm/uclass.h>
25 #include <linux/bitops.h>
26 #include <linux/printk.h>
27
28 /*
29  * early TLB into the .data section so that it not get cleared
30  * with 16kB allignment (see TTBR0_BASE_ADDR_MASK)
31  */
32 u8 early_tlb[PGTABLE_SIZE] __section(".data") __aligned(0x4000);
33
34 struct lmb lmb;
35
36 u32 get_bootmode(void)
37 {
38         /* read bootmode from TAMP backup register */
39         return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >>
40                     TAMP_BOOT_MODE_SHIFT;
41 }
42
43 u32 get_bootauth(void)
44 {
45         /* read boot auth status and partition from TAMP backup register */
46         return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_AUTH_MASK) >>
47                     TAMP_BOOT_AUTH_SHIFT;
48 }
49
50 /*
51  * weak function overidde: set the DDR/SYSRAM executable before to enable the
52  * MMU and configure DACR, for early early_enable_caches (SPL or pre-reloc)
53  */
54 void dram_bank_mmu_setup(int bank)
55 {
56         struct bd_info *bd = gd->bd;
57         int     i;
58         phys_addr_t start;
59         phys_size_t size;
60         bool use_lmb = false;
61         enum dcache_option option;
62
63         if (IS_ENABLED(CONFIG_SPL_BUILD)) {
64 /* STM32_SYSRAM_BASE exist only when SPL is supported */
65 #ifdef CONFIG_SPL
66                 start = ALIGN_DOWN(STM32_SYSRAM_BASE, MMU_SECTION_SIZE);
67                 size = ALIGN(STM32_SYSRAM_SIZE, MMU_SECTION_SIZE);
68 #endif
69         } else if (gd->flags & GD_FLG_RELOC) {
70                 /* bd->bi_dram is available only after relocation */
71                 start = bd->bi_dram[bank].start;
72                 size =  bd->bi_dram[bank].size;
73                 use_lmb = true;
74         } else {
75                 /* mark cacheable and executable the beggining of the DDR */
76                 start = STM32_DDR_BASE;
77                 size = CONFIG_DDR_CACHEABLE_SIZE;
78         }
79
80         for (i = start >> MMU_SECTION_SHIFT;
81              i < (start >> MMU_SECTION_SHIFT) + (size >> MMU_SECTION_SHIFT);
82              i++) {
83                 option = DCACHE_DEFAULT_OPTION;
84                 if (use_lmb && lmb_is_reserved_flags(&lmb, i << MMU_SECTION_SHIFT, LMB_NOMAP))
85                         option = 0; /* INVALID ENTRY in TLB */
86                 set_section_dcache(i, option);
87         }
88 }
89 /*
90  * initialize the MMU and activate cache in SPL or in U-Boot pre-reloc stage
91  * MMU/TLB is updated in enable_caches() for U-Boot after relocation
92  * or is deactivated in U-Boot entry function start.S::cpu_init_cp15
93  */
94 static void early_enable_caches(void)
95 {
96         /* I-cache is already enabled in start.S: cpu_init_cp15 */
97
98         if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
99                 return;
100
101 #if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
102                 gd->arch.tlb_size = PGTABLE_SIZE;
103                 gd->arch.tlb_addr = (unsigned long)&early_tlb;
104 #endif
105
106         /* enable MMU (default configuration) */
107         dcache_enable();
108 }
109
110 /*
111  * Early system init
112  */
113 int arch_cpu_init(void)
114 {
115         early_enable_caches();
116
117         /* early armv7 timer init: needed for polling */
118         timer_init();
119
120         return 0;
121 }
122
123 /* weak function for SOC specific initialization */
124 __weak void stm32mp_cpu_init(void)
125 {
126 }
127
128 int mach_cpu_init(void)
129 {
130         u32 boot_mode;
131
132         stm32mp_cpu_init();
133
134         boot_mode = get_bootmode();
135
136         if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) &&
137             (boot_mode & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_UART)
138                 gd->flags |= GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE;
139         else if (IS_ENABLED(CONFIG_DEBUG_UART) && IS_ENABLED(CONFIG_SPL_BUILD))
140                 debug_uart_init();
141
142         return 0;
143 }
144
145 void enable_caches(void)
146 {
147         /* parse device tree when data cache is still activated */
148         lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
149
150         /* I-cache is already enabled in start.S: icache_enable() not needed */
151
152         /* deactivate the data cache, early enabled in arch_cpu_init() */
153         dcache_disable();
154         /*
155          * update MMU after relocation and enable the data cache
156          * warning: the TLB location udpated in board_f.c::reserve_mmu
157          */
158         dcache_enable();
159 }
160
161 /* used when CONFIG_DISPLAY_CPUINFO is activated */
162 int print_cpuinfo(void)
163 {
164         char name[SOC_NAME_SIZE];
165
166         get_soc_name(name);
167         printf("CPU: %s\n", name);
168
169         return 0;
170 }
171
172 static void setup_boot_mode(void)
173 {
174         const u32 serial_addr[] = {
175                 STM32_USART1_BASE,
176                 STM32_USART2_BASE,
177                 STM32_USART3_BASE,
178                 STM32_UART4_BASE,
179                 STM32_UART5_BASE,
180                 STM32_USART6_BASE,
181                 STM32_UART7_BASE,
182                 STM32_UART8_BASE
183         };
184         const u32 sdmmc_addr[] = {
185                 STM32_SDMMC1_BASE,
186                 STM32_SDMMC2_BASE,
187                 STM32_SDMMC3_BASE
188         };
189         char cmd[60];
190         u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
191         u32 boot_mode =
192                 (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
193         unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
194         u32 forced_mode = (boot_ctx & TAMP_BOOT_FORCED_MASK);
195         struct udevice *dev;
196
197         log_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d forced=%x\n",
198                   __func__, boot_ctx, boot_mode, instance, forced_mode);
199         switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
200         case BOOT_SERIAL_UART:
201                 if (instance >= ARRAY_SIZE(serial_addr))
202                         break;
203                 /* serial : search associated node in devicetree */
204                 sprintf(cmd, "serial@%x", serial_addr[instance]);
205                 if (uclass_get_device_by_name(UCLASS_SERIAL, cmd, &dev)) {
206                         /* restore console on error */
207                         if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL))
208                                 gd->flags &= ~(GD_FLG_SILENT |
209                                                GD_FLG_DISABLE_CONSOLE);
210                         log_err("uart%d = %s not found in device tree!\n",
211                                 instance + 1, cmd);
212                         break;
213                 }
214                 sprintf(cmd, "%d", dev_seq(dev));
215                 env_set("boot_device", "serial");
216                 env_set("boot_instance", cmd);
217
218                 /* restore console on uart when not used */
219                 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) && gd->cur_serial_dev != dev) {
220                         gd->flags &= ~(GD_FLG_SILENT |
221                                        GD_FLG_DISABLE_CONSOLE);
222                         log_info("serial boot with console enabled!\n");
223                 }
224                 break;
225         case BOOT_SERIAL_USB:
226                 env_set("boot_device", "usb");
227                 env_set("boot_instance", "0");
228                 break;
229         case BOOT_FLASH_SD:
230         case BOOT_FLASH_EMMC:
231                 if (instance >= ARRAY_SIZE(sdmmc_addr))
232                         break;
233                 /* search associated sdmmc node in devicetree */
234                 sprintf(cmd, "mmc@%x", sdmmc_addr[instance]);
235                 if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) {
236                         printf("mmc%d = %s not found in device tree!\n",
237                                instance, cmd);
238                         break;
239                 }
240                 sprintf(cmd, "%d", dev_seq(dev));
241                 env_set("boot_device", "mmc");
242                 env_set("boot_instance", cmd);
243                 break;
244         case BOOT_FLASH_NAND:
245                 env_set("boot_device", "nand");
246                 env_set("boot_instance", "0");
247                 break;
248         case BOOT_FLASH_SPINAND:
249                 env_set("boot_device", "spi-nand");
250                 env_set("boot_instance", "0");
251                 break;
252         case BOOT_FLASH_NOR:
253                 env_set("boot_device", "nor");
254                 env_set("boot_instance", "0");
255                 break;
256         default:
257                 env_set("boot_device", "invalid");
258                 env_set("boot_instance", "");
259                 log_err("unexpected boot mode = %x\n", boot_mode);
260                 break;
261         }
262
263         switch (forced_mode) {
264         case BOOT_FASTBOOT:
265                 log_info("Enter fastboot!\n");
266                 env_set("preboot", "env set preboot; fastboot 0");
267                 break;
268         case BOOT_STM32PROG:
269                 env_set("boot_device", "usb");
270                 env_set("boot_instance", "0");
271                 break;
272         case BOOT_UMS_MMC0:
273         case BOOT_UMS_MMC1:
274         case BOOT_UMS_MMC2:
275                 log_info("Enter UMS!\n");
276                 instance = forced_mode - BOOT_UMS_MMC0;
277                 sprintf(cmd, "env set preboot; ums 0 mmc %d", instance);
278                 env_set("preboot", cmd);
279                 break;
280         case BOOT_RECOVERY:
281                 env_set("preboot", "env set preboot; run altbootcmd");
282                 break;
283         case BOOT_NORMAL:
284                 break;
285         default:
286                 log_debug("unexpected forced boot mode = %x\n", forced_mode);
287                 break;
288         }
289
290         /* clear TAMP for next reboot */
291         clrsetbits_le32(TAMP_BOOT_CONTEXT, TAMP_BOOT_FORCED_MASK, BOOT_NORMAL);
292 }
293
294 /*
295  * If there is no MAC address in the environment, then it will be initialized
296  * (silently) from the value in the OTP.
297  */
298 __weak int setup_mac_address(void)
299 {
300         int ret;
301         int i;
302         u32 otp[3];
303         uchar enetaddr[6];
304         struct udevice *dev;
305         int nb_eth, nb_otp, index;
306
307         if (!IS_ENABLED(CONFIG_NET))
308                 return 0;
309
310         nb_eth = get_eth_nb();
311
312         /* 6 bytes for each MAC addr and 4 bytes for each OTP */
313         nb_otp = DIV_ROUND_UP(6 * nb_eth, 4);
314
315         ret = uclass_get_device_by_driver(UCLASS_MISC,
316                                           DM_DRIVER_GET(stm32mp_bsec),
317                                           &dev);
318         if (ret)
319                 return ret;
320
321         ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_MAC), otp, 4 * nb_otp);
322         if (ret < 0)
323                 return ret;
324
325         for (index = 0; index < nb_eth; index++) {
326                 /* MAC already in environment */
327                 if (eth_env_get_enetaddr_by_index("eth", index, enetaddr))
328                         continue;
329
330                 for (i = 0; i < 6; i++)
331                         enetaddr[i] = ((uint8_t *)&otp)[i + 6 * index];
332
333                 if (!is_valid_ethaddr(enetaddr)) {
334                         log_err("invalid MAC address %d in OTP %pM\n",
335                                 index, enetaddr);
336                         return -EINVAL;
337                 }
338                 log_debug("OTP MAC address %d = %pM\n", index, enetaddr);
339                 ret = eth_env_set_enetaddr_by_index("eth", index, enetaddr);
340                 if (ret) {
341                         log_err("Failed to set mac address %pM from OTP: %d\n",
342                                 enetaddr, ret);
343                         return ret;
344                 }
345         }
346
347         return 0;
348 }
349
350 static int setup_serial_number(void)
351 {
352         char serial_string[25];
353         u32 otp[3] = {0, 0, 0 };
354         struct udevice *dev;
355         int ret;
356
357         if (env_get("serial#"))
358                 return 0;
359
360         ret = uclass_get_device_by_driver(UCLASS_MISC,
361                                           DM_DRIVER_GET(stm32mp_bsec),
362                                           &dev);
363         if (ret)
364                 return ret;
365
366         ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_SERIAL),
367                         otp, sizeof(otp));
368         if (ret < 0)
369                 return ret;
370
371         sprintf(serial_string, "%08X%08X%08X", otp[0], otp[1], otp[2]);
372         env_set("serial#", serial_string);
373
374         return 0;
375 }
376
377 __weak void stm32mp_misc_init(void)
378 {
379 }
380
381 static int setup_boot_auth_info(void)
382 {
383         char buf[10];
384         u32 bootauth = get_bootauth();
385
386         snprintf(buf, sizeof(buf), "%d", bootauth >> 4);
387         env_set("boot_auth", buf);
388
389         snprintf(buf, sizeof(buf), "%d", bootauth &
390                  (u32)TAMP_BOOT_PARTITION_MASK);
391         env_set("boot_part", buf);
392
393         return 0;
394 }
395
396 int arch_misc_init(void)
397 {
398         setup_boot_auth_info();
399         setup_boot_mode();
400         setup_mac_address();
401         setup_serial_number();
402         stm32mp_misc_init();
403
404         return 0;
405 }
406
407 /*
408  * Without forcing the ".data" section, this would get saved in ".bss". BSS
409  * will be cleared soon after, so it's not suitable.
410  */
411 static uintptr_t rom_api_table __section(".data");
412 static uintptr_t nt_fw_dtb __section(".data");
413
414 /*
415  * The ROM gives us the API location in r0 when starting. This is only available
416  * during SPL, as there isn't (yet) a mechanism to pass this on to u-boot. Save
417  * the FDT address provided by TF-A in r2 at boot time. This function is called
418  * from start.S
419  */
420 void save_boot_params(unsigned long r0, unsigned long r1, unsigned long r2,
421                       unsigned long r3)
422 {
423         if (IS_ENABLED(CONFIG_STM32_ECDSA_VERIFY))
424                 rom_api_table = r0;
425
426         if (IS_ENABLED(CONFIG_TFABOOT))
427                 nt_fw_dtb = r2;
428
429         save_boot_params_ret();
430 }
431
432 uintptr_t get_stm32mp_rom_api_table(void)
433 {
434         return rom_api_table;
435 }
436
437 uintptr_t get_stm32mp_bl2_dtb(void)
438 {
439         return nt_fw_dtb;
440 }
441
442 #ifdef CONFIG_SPL_BUILD
443 void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
444 {
445         typedef void __noreturn (*image_entry_stm32_t)(u32 romapi);
446         uintptr_t romapi = get_stm32mp_rom_api_table();
447
448         image_entry_stm32_t image_entry =
449                 (image_entry_stm32_t)spl_image->entry_point;
450
451         printf("image entry point: 0x%lx\n", spl_image->entry_point);
452         image_entry(romapi);
453 }
454 #endif
This page took 0.053958 seconds and 4 git commands to generate.