1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
6 #define LOG_CATEGORY LOGC_ARCH
11 #include <debug_uart.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>
29 * early TLB into the .data section so that it not get cleared
30 * with 16kB allignment (see TTBR0_BASE_ADDR_MASK)
32 u8 early_tlb[PGTABLE_SIZE] __section(".data") __aligned(0x4000);
36 u32 get_bootmode(void)
38 /* read bootmode from TAMP backup register */
39 return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >>
43 u32 get_bootauth(void)
45 /* read boot auth status and partition from TAMP backup register */
46 return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_AUTH_MASK) >>
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)
54 void dram_bank_mmu_setup(int bank)
56 struct bd_info *bd = gd->bd;
61 enum dcache_option option;
63 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
64 /* STM32_SYSRAM_BASE exist only when SPL is supported */
66 start = ALIGN_DOWN(STM32_SYSRAM_BASE, MMU_SECTION_SIZE);
67 size = ALIGN(STM32_SYSRAM_SIZE, MMU_SECTION_SIZE);
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;
75 /* mark cacheable and executable the beggining of the DDR */
76 start = STM32_DDR_BASE;
77 size = CONFIG_DDR_CACHEABLE_SIZE;
80 for (i = start >> MMU_SECTION_SHIFT;
81 i < (start >> MMU_SECTION_SHIFT) + (size >> MMU_SECTION_SHIFT);
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);
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
94 static void early_enable_caches(void)
96 /* I-cache is already enabled in start.S: cpu_init_cp15 */
98 if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
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;
106 /* enable MMU (default configuration) */
113 int arch_cpu_init(void)
115 early_enable_caches();
117 /* early armv7 timer init: needed for polling */
123 /* weak function for SOC specific initialization */
124 __weak void stm32mp_cpu_init(void)
128 int mach_cpu_init(void)
134 boot_mode = get_bootmode();
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))
145 void enable_caches(void)
147 /* parse device tree when data cache is still activated */
148 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
150 /* I-cache is already enabled in start.S: icache_enable() not needed */
152 /* deactivate the data cache, early enabled in arch_cpu_init() */
155 * update MMU after relocation and enable the data cache
156 * warning: the TLB location udpated in board_f.c::reserve_mmu
161 /* used when CONFIG_DISPLAY_CPUINFO is activated */
162 int print_cpuinfo(void)
164 char name[SOC_NAME_SIZE];
167 printf("CPU: %s\n", name);
172 static void setup_boot_mode(void)
174 const u32 serial_addr[] = {
184 const u32 sdmmc_addr[] = {
190 u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
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);
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))
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",
214 sprintf(cmd, "%d", dev_seq(dev));
215 env_set("boot_device", "serial");
216 env_set("boot_instance", cmd);
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");
225 case BOOT_SERIAL_USB:
226 env_set("boot_device", "usb");
227 env_set("boot_instance", "0");
230 case BOOT_FLASH_EMMC:
231 if (instance >= ARRAY_SIZE(sdmmc_addr))
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",
240 sprintf(cmd, "%d", dev_seq(dev));
241 env_set("boot_device", "mmc");
242 env_set("boot_instance", cmd);
244 case BOOT_FLASH_NAND:
245 env_set("boot_device", "nand");
246 env_set("boot_instance", "0");
248 case BOOT_FLASH_SPINAND:
249 env_set("boot_device", "spi-nand");
250 env_set("boot_instance", "0");
253 env_set("boot_device", "nor");
254 env_set("boot_instance", "0");
257 env_set("boot_device", "invalid");
258 env_set("boot_instance", "");
259 log_err("unexpected boot mode = %x\n", boot_mode);
263 switch (forced_mode) {
265 log_info("Enter fastboot!\n");
266 env_set("preboot", "env set preboot; fastboot 0");
269 env_set("boot_device", "usb");
270 env_set("boot_instance", "0");
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);
281 env_set("preboot", "env set preboot; run altbootcmd");
286 log_debug("unexpected forced boot mode = %x\n", forced_mode);
290 /* clear TAMP for next reboot */
291 clrsetbits_le32(TAMP_BOOT_CONTEXT, TAMP_BOOT_FORCED_MASK, BOOT_NORMAL);
295 * If there is no MAC address in the environment, then it will be initialized
296 * (silently) from the value in the OTP.
298 __weak int setup_mac_address(void)
305 int nb_eth, nb_otp, index;
307 if (!IS_ENABLED(CONFIG_NET))
310 nb_eth = get_eth_nb();
312 /* 6 bytes for each MAC addr and 4 bytes for each OTP */
313 nb_otp = DIV_ROUND_UP(6 * nb_eth, 4);
315 ret = uclass_get_device_by_driver(UCLASS_MISC,
316 DM_DRIVER_GET(stm32mp_bsec),
321 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_MAC), otp, 4 * nb_otp);
325 for (index = 0; index < nb_eth; index++) {
326 /* MAC already in environment */
327 if (eth_env_get_enetaddr_by_index("eth", index, enetaddr))
330 for (i = 0; i < 6; i++)
331 enetaddr[i] = ((uint8_t *)&otp)[i + 6 * index];
333 if (!is_valid_ethaddr(enetaddr)) {
334 log_err("invalid MAC address %d in OTP %pM\n",
338 log_debug("OTP MAC address %d = %pM\n", index, enetaddr);
339 ret = eth_env_set_enetaddr_by_index("eth", index, enetaddr);
341 log_err("Failed to set mac address %pM from OTP: %d\n",
350 static int setup_serial_number(void)
352 char serial_string[25];
353 u32 otp[3] = {0, 0, 0 };
357 if (env_get("serial#"))
360 ret = uclass_get_device_by_driver(UCLASS_MISC,
361 DM_DRIVER_GET(stm32mp_bsec),
366 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_SERIAL),
371 sprintf(serial_string, "%08X%08X%08X", otp[0], otp[1], otp[2]);
372 env_set("serial#", serial_string);
377 __weak void stm32mp_misc_init(void)
381 static int setup_boot_auth_info(void)
384 u32 bootauth = get_bootauth();
386 snprintf(buf, sizeof(buf), "%d", bootauth >> 4);
387 env_set("boot_auth", buf);
389 snprintf(buf, sizeof(buf), "%d", bootauth &
390 (u32)TAMP_BOOT_PARTITION_MASK);
391 env_set("boot_part", buf);
396 int arch_misc_init(void)
398 setup_boot_auth_info();
401 setup_serial_number();
408 * Without forcing the ".data" section, this would get saved in ".bss". BSS
409 * will be cleared soon after, so it's not suitable.
411 static uintptr_t rom_api_table __section(".data");
412 static uintptr_t nt_fw_dtb __section(".data");
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
420 void save_boot_params(unsigned long r0, unsigned long r1, unsigned long r2,
423 if (IS_ENABLED(CONFIG_STM32_ECDSA_VERIFY))
426 if (IS_ENABLED(CONFIG_TFABOOT))
429 save_boot_params_ret();
432 uintptr_t get_stm32mp_rom_api_table(void)
434 return rom_api_table;
437 uintptr_t get_stm32mp_bl2_dtb(void)
442 #ifdef CONFIG_SPL_BUILD
443 void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
445 typedef void __noreturn (*image_entry_stm32_t)(u32 romapi);
446 uintptr_t romapi = get_stm32mp_rom_api_table();
448 image_entry_stm32_t image_entry =
449 (image_entry_stm32_t)spl_image->entry_point;
451 printf("image entry point: 0x%lx\n", spl_image->entry_point);