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