blob: 478c3efae73e65317c8430ea735f1f68c742bbe9 [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunay85b53972018-03-12 10:46:10 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunay85b53972018-03-12 10:46:10 +01004 */
Patrick Delaunayba779402020-11-06 19:01:29 +01005
6#define LOG_CATEGORY LOGC_ARCH
7
Patrick Delaunay85b53972018-03-12 10:46:10 +01008#include <clk.h>
Simon Glass1d91ba72019-11-14 12:57:37 -07009#include <cpu_func.h>
Patrick Delaunay82168e82018-05-17 14:50:46 +020010#include <debug_uart.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060011#include <env.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Patrick Delaunayc9468742021-05-07 14:50:35 +020014#include <lmb.h>
Patrick Delaunayf3674a42018-05-17 15:24:07 +020015#include <misc.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060016#include <spl.h>
Patrick Delaunay3fa644b2024-01-15 15:05:51 +010017#include <asm/cache.h>
Patrick Delaunay85b53972018-03-12 10:46:10 +010018#include <asm/io.h>
19#include <asm/arch/stm32.h>
Patrick Delaunay01e3afe2018-03-19 19:09:21 +010020#include <asm/arch/sys_proto.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Patrick Delaunayf3674a42018-05-17 15:24:07 +020022#include <dm/device.h>
Patrick Delaunayc5d15652018-03-20 10:54:53 +010023#include <dm/uclass.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060025#include <linux/printk.h>
Patrick Delaunay85b53972018-03-12 10:46:10 +010026
Patrick Delaunay8e6985b2020-04-30 16:30:20 +020027/*
28 * early TLB into the .data section so that it not get cleared
29 * with 16kB allignment (see TTBR0_BASE_ADDR_MASK)
30 */
31u8 early_tlb[PGTABLE_SIZE] __section(".data") __aligned(0x4000);
32
Patrick Delaunayc9468742021-05-07 14:50:35 +020033struct lmb lmb;
34
Patrick Delaunay18660a62019-02-27 17:01:12 +010035u32 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 Delaunayc5d15652018-03-20 10:54:53 +010040}
41
Igor Opaniuk100e0ec2023-11-06 11:41:52 +010042u32 get_bootauth(void)
43{
44 /* read boot auth status and partition from TAMP backup register */
45 return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_AUTH_MASK) >>
46 TAMP_BOOT_AUTH_SHIFT;
47}
48
Patrick Delaunayc5d15652018-03-20 10:54:53 +010049/*
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010050 * weak function overidde: set the DDR/SYSRAM executable before to enable the
51 * MMU and configure DACR, for early early_enable_caches (SPL or pre-reloc)
52 */
53void dram_bank_mmu_setup(int bank)
54{
55 struct bd_info *bd = gd->bd;
56 int i;
57 phys_addr_t start;
58 phys_size_t size;
Patrick Delaunayc9468742021-05-07 14:50:35 +020059 bool use_lmb = false;
60 enum dcache_option option;
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010061
62 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
Patrick Delaunay123687c2022-05-20 18:24:46 +020063/* STM32_SYSRAM_BASE exist only when SPL is supported */
64#ifdef CONFIG_SPL
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010065 start = ALIGN_DOWN(STM32_SYSRAM_BASE, MMU_SECTION_SIZE);
66 size = ALIGN(STM32_SYSRAM_SIZE, MMU_SECTION_SIZE);
Patrick Delaunay123687c2022-05-20 18:24:46 +020067#endif
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010068 } else if (gd->flags & GD_FLG_RELOC) {
69 /* bd->bi_dram is available only after relocation */
70 start = bd->bi_dram[bank].start;
71 size = bd->bi_dram[bank].size;
Patrick Delaunayc9468742021-05-07 14:50:35 +020072 use_lmb = true;
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010073 } else {
74 /* mark cacheable and executable the beggining of the DDR */
75 start = STM32_DDR_BASE;
76 size = CONFIG_DDR_CACHEABLE_SIZE;
77 }
78
79 for (i = start >> MMU_SECTION_SHIFT;
80 i < (start >> MMU_SECTION_SHIFT) + (size >> MMU_SECTION_SHIFT);
Patrick Delaunayc9468742021-05-07 14:50:35 +020081 i++) {
82 option = DCACHE_DEFAULT_OPTION;
83 if (use_lmb && lmb_is_reserved_flags(&lmb, i << MMU_SECTION_SHIFT, LMB_NOMAP))
84 option = 0; /* INVALID ENTRY in TLB */
85 set_section_dcache(i, option);
86 }
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010087}
88/*
Patrick Delaunay8e6985b2020-04-30 16:30:20 +020089 * initialize the MMU and activate cache in SPL or in U-Boot pre-reloc stage
90 * MMU/TLB is updated in enable_caches() for U-Boot after relocation
91 * or is deactivated in U-Boot entry function start.S::cpu_init_cp15
92 */
93static void early_enable_caches(void)
94{
95 /* I-cache is already enabled in start.S: cpu_init_cp15 */
96
97 if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
98 return;
99
Bhupesh Sharma58af3fb2023-08-22 13:21:11 +0530100#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
Patrice Chotard18a87162021-02-24 13:53:27 +0100101 gd->arch.tlb_size = PGTABLE_SIZE;
102 gd->arch.tlb_addr = (unsigned long)&early_tlb;
Bhupesh Sharma58af3fb2023-08-22 13:21:11 +0530103#endif
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200104
Patrick Delaunay4ad5a122021-02-05 13:53:33 +0100105 /* enable MMU (default configuration) */
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200106 dcache_enable();
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200107}
108
109/*
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100110 * Early system init
111 */
Patrick Delaunay85b53972018-03-12 10:46:10 +0100112int arch_cpu_init(void)
113{
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200114 early_enable_caches();
115
Patrick Delaunay85b53972018-03-12 10:46:10 +0100116 /* early armv7 timer init: needed for polling */
117 timer_init();
118
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200119 return 0;
120}
121
122/* weak function for SOC specific initialization */
123__weak void stm32mp_cpu_init(void)
124{
125}
126
127int mach_cpu_init(void)
128{
129 u32 boot_mode;
130
131 stm32mp_cpu_init();
Patrick Delaunay82168e82018-05-17 14:50:46 +0200132
Patrick Delaunay82168e82018-05-17 14:50:46 +0200133 boot_mode = get_bootmode();
134
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100135 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) &&
136 (boot_mode & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_UART)
Patrick Delaunay82168e82018-05-17 14:50:46 +0200137 gd->flags |= GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE;
Patrick Delaunayd8299de2021-10-11 09:52:51 +0200138 else if (IS_ENABLED(CONFIG_DEBUG_UART) && IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay82168e82018-05-17 14:50:46 +0200139 debug_uart_init();
Patrick Delaunay85b53972018-03-12 10:46:10 +0100140
141 return 0;
142}
143
Patrick Delaunay58e95532018-03-19 19:09:20 +0100144void enable_caches(void)
145{
Patrick Delaunayc9468742021-05-07 14:50:35 +0200146 /* parse device tree when data cache is still activated */
147 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
148
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200149 /* I-cache is already enabled in start.S: icache_enable() not needed */
150
151 /* deactivate the data cache, early enabled in arch_cpu_init() */
152 dcache_disable();
153 /*
154 * update MMU after relocation and enable the data cache
155 * warning: the TLB location udpated in board_f.c::reserve_mmu
156 */
Patrick Delaunay58e95532018-03-19 19:09:20 +0100157 dcache_enable();
158}
159
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100160static void setup_boot_mode(void)
161{
Patrick Delaunay18660a62019-02-27 17:01:12 +0100162 const u32 serial_addr[] = {
163 STM32_USART1_BASE,
164 STM32_USART2_BASE,
165 STM32_USART3_BASE,
166 STM32_UART4_BASE,
167 STM32_UART5_BASE,
168 STM32_USART6_BASE,
169 STM32_UART7_BASE,
170 STM32_UART8_BASE
171 };
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200172 const u32 sdmmc_addr[] = {
173 STM32_SDMMC1_BASE,
174 STM32_SDMMC2_BASE,
175 STM32_SDMMC3_BASE
176 };
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100177 char cmd[60];
178 u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
179 u32 boot_mode =
180 (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
Patrick Delaunay1b03eb02019-06-21 15:26:39 +0200181 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100182 u32 forced_mode = (boot_ctx & TAMP_BOOT_FORCED_MASK);
Patrick Delaunay18660a62019-02-27 17:01:12 +0100183 struct udevice *dev;
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100184
Patrick Delaunayba779402020-11-06 19:01:29 +0100185 log_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d forced=%x\n",
186 __func__, boot_ctx, boot_mode, instance, forced_mode);
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100187 switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
188 case BOOT_SERIAL_UART:
Rasmus Villemoes6d83f3c2023-03-24 08:55:19 +0100189 if (instance >= ARRAY_SIZE(serial_addr))
Patrick Delaunay18660a62019-02-27 17:01:12 +0100190 break;
Patrick Delaunaye2592992021-02-25 13:37:03 +0100191 /* serial : search associated node in devicetree */
Patrick Delaunay18660a62019-02-27 17:01:12 +0100192 sprintf(cmd, "serial@%x", serial_addr[instance]);
Patrick Delaunaye2592992021-02-25 13:37:03 +0100193 if (uclass_get_device_by_name(UCLASS_SERIAL, cmd, &dev)) {
Patrick Delaunay7540d872021-02-25 13:37:02 +0100194 /* restore console on error */
195 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL))
196 gd->flags &= ~(GD_FLG_SILENT |
197 GD_FLG_DISABLE_CONSOLE);
Patrick Delaunay643e4042021-04-06 09:27:39 +0200198 log_err("uart%d = %s not found in device tree!\n",
199 instance + 1, cmd);
Patrick Delaunay18660a62019-02-27 17:01:12 +0100200 break;
Patrick Delaunay7540d872021-02-25 13:37:02 +0100201 }
Patrick Delaunaye2592992021-02-25 13:37:03 +0100202 sprintf(cmd, "%d", dev_seq(dev));
Patrick Delaunay18660a62019-02-27 17:01:12 +0100203 env_set("boot_device", "serial");
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100204 env_set("boot_instance", cmd);
Patrick Delaunay18660a62019-02-27 17:01:12 +0100205
206 /* restore console on uart when not used */
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100207 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) && gd->cur_serial_dev != dev) {
Patrick Delaunay18660a62019-02-27 17:01:12 +0100208 gd->flags &= ~(GD_FLG_SILENT |
209 GD_FLG_DISABLE_CONSOLE);
Patrick Delaunay643e4042021-04-06 09:27:39 +0200210 log_info("serial boot with console enabled!\n");
Patrick Delaunay18660a62019-02-27 17:01:12 +0100211 }
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100212 break;
213 case BOOT_SERIAL_USB:
214 env_set("boot_device", "usb");
215 env_set("boot_instance", "0");
216 break;
217 case BOOT_FLASH_SD:
218 case BOOT_FLASH_EMMC:
Rasmus Villemoes6d83f3c2023-03-24 08:55:19 +0100219 if (instance >= ARRAY_SIZE(sdmmc_addr))
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200220 break;
221 /* search associated sdmmc node in devicetree */
222 sprintf(cmd, "mmc@%x", sdmmc_addr[instance]);
223 if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) {
224 printf("mmc%d = %s not found in device tree!\n",
225 instance, cmd);
226 break;
227 }
228 sprintf(cmd, "%d", dev_seq(dev));
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100229 env_set("boot_device", "mmc");
230 env_set("boot_instance", cmd);
231 break;
232 case BOOT_FLASH_NAND:
233 env_set("boot_device", "nand");
234 env_set("boot_instance", "0");
235 break;
Patrick Delaunayb5a7ca22020-03-18 09:22:52 +0100236 case BOOT_FLASH_SPINAND:
237 env_set("boot_device", "spi-nand");
238 env_set("boot_instance", "0");
239 break;
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100240 case BOOT_FLASH_NOR:
241 env_set("boot_device", "nor");
242 env_set("boot_instance", "0");
243 break;
244 default:
Patrick Delaunay02e91972021-07-08 10:53:56 +0200245 env_set("boot_device", "invalid");
246 env_set("boot_instance", "");
247 log_err("unexpected boot mode = %x\n", boot_mode);
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100248 break;
249 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100250
251 switch (forced_mode) {
252 case BOOT_FASTBOOT:
Patrick Delaunay643e4042021-04-06 09:27:39 +0200253 log_info("Enter fastboot!\n");
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100254 env_set("preboot", "env set preboot; fastboot 0");
255 break;
256 case BOOT_STM32PROG:
257 env_set("boot_device", "usb");
258 env_set("boot_instance", "0");
259 break;
260 case BOOT_UMS_MMC0:
261 case BOOT_UMS_MMC1:
262 case BOOT_UMS_MMC2:
Patrick Delaunay643e4042021-04-06 09:27:39 +0200263 log_info("Enter UMS!\n");
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100264 instance = forced_mode - BOOT_UMS_MMC0;
265 sprintf(cmd, "env set preboot; ums 0 mmc %d", instance);
266 env_set("preboot", cmd);
267 break;
268 case BOOT_RECOVERY:
269 env_set("preboot", "env set preboot; run altbootcmd");
270 break;
271 case BOOT_NORMAL:
272 break;
273 default:
Patrick Delaunayba779402020-11-06 19:01:29 +0100274 log_debug("unexpected forced boot mode = %x\n", forced_mode);
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100275 break;
276 }
277
278 /* clear TAMP for next reboot */
279 clrsetbits_le32(TAMP_BOOT_CONTEXT, TAMP_BOOT_FORCED_MASK, BOOT_NORMAL);
Patrick Delaunayf3674a42018-05-17 15:24:07 +0200280}
281
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200282__weak void stm32mp_misc_init(void)
Marek Vasut0eda28c2021-03-31 14:15:09 +0200283{
Igor Opaniuk100e0ec2023-11-06 11:41:52 +0100284}
285
286static int setup_boot_auth_info(void)
287{
288 char buf[10];
289 u32 bootauth = get_bootauth();
290
291 snprintf(buf, sizeof(buf), "%d", bootauth >> 4);
292 env_set("boot_auth", buf);
293
294 snprintf(buf, sizeof(buf), "%d", bootauth &
295 (u32)TAMP_BOOT_PARTITION_MASK);
296 env_set("boot_part", buf);
297
298 return 0;
Marek Vasut0eda28c2021-03-31 14:15:09 +0200299}
300
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100301int arch_misc_init(void)
302{
Igor Opaniuk100e0ec2023-11-06 11:41:52 +0100303 setup_boot_auth_info();
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100304 setup_boot_mode();
Patrick Delaunayf3674a42018-05-17 15:24:07 +0200305 setup_mac_address();
306 setup_serial_number();
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200307 stm32mp_misc_init();
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100308
309 return 0;
310}
Marek Vasutefdedcb2023-01-12 18:58:40 +0100311
312/*
313 * Without forcing the ".data" section, this would get saved in ".bss". BSS
314 * will be cleared soon after, so it's not suitable.
315 */
316static uintptr_t rom_api_table __section(".data");
317static uintptr_t nt_fw_dtb __section(".data");
318
319/*
320 * The ROM gives us the API location in r0 when starting. This is only available
321 * during SPL, as there isn't (yet) a mechanism to pass this on to u-boot. Save
322 * the FDT address provided by TF-A in r2 at boot time. This function is called
323 * from start.S
324 */
325void save_boot_params(unsigned long r0, unsigned long r1, unsigned long r2,
326 unsigned long r3)
327{
328 if (IS_ENABLED(CONFIG_STM32_ECDSA_VERIFY))
329 rom_api_table = r0;
330
331 if (IS_ENABLED(CONFIG_TFABOOT))
332 nt_fw_dtb = r2;
333
334 save_boot_params_ret();
335}
336
337uintptr_t get_stm32mp_rom_api_table(void)
338{
339 return rom_api_table;
340}
341
342uintptr_t get_stm32mp_bl2_dtb(void)
343{
344 return nt_fw_dtb;
345}
Marek Vasut7cf2c332023-01-12 18:58:41 +0100346
347#ifdef CONFIG_SPL_BUILD
348void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
349{
350 typedef void __noreturn (*image_entry_stm32_t)(u32 romapi);
351 uintptr_t romapi = get_stm32mp_rom_api_table();
352
353 image_entry_stm32_t image_entry =
354 (image_entry_stm32_t)spl_image->entry_point;
355
356 printf("image entry point: 0x%lx\n", spl_image->entry_point);
357 image_entry(romapi);
358}
359#endif