blob: 26c073f06a07e148662341b7097c0a380367703c [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 Delaunay18660a62019-02-27 17:01:12 +010033u32 get_bootmode(void)
34{
35 /* read bootmode from TAMP backup register */
36 return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >>
37 TAMP_BOOT_MODE_SHIFT;
Patrick Delaunayc5d15652018-03-20 10:54:53 +010038}
39
Igor Opaniuk100e0ec2023-11-06 11:41:52 +010040u32 get_bootauth(void)
41{
42 /* read boot auth status and partition from TAMP backup register */
43 return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_AUTH_MASK) >>
44 TAMP_BOOT_AUTH_SHIFT;
45}
46
Patrick Delaunayc5d15652018-03-20 10:54:53 +010047/*
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010048 * weak function overidde: set the DDR/SYSRAM executable before to enable the
49 * MMU and configure DACR, for early early_enable_caches (SPL or pre-reloc)
50 */
51void dram_bank_mmu_setup(int bank)
52{
53 struct bd_info *bd = gd->bd;
54 int i;
55 phys_addr_t start;
56 phys_size_t size;
Patrick Delaunayc9468742021-05-07 14:50:35 +020057 bool use_lmb = false;
58 enum dcache_option option;
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010059
Simon Glass85ed77d2024-09-29 19:49:46 -060060 if (IS_ENABLED(CONFIG_XPL_BUILD)) {
Patrick Delaunay123687c2022-05-20 18:24:46 +020061/* STM32_SYSRAM_BASE exist only when SPL is supported */
62#ifdef CONFIG_SPL
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010063 start = ALIGN_DOWN(STM32_SYSRAM_BASE, MMU_SECTION_SIZE);
64 size = ALIGN(STM32_SYSRAM_SIZE, MMU_SECTION_SIZE);
Patrick Delaunay123687c2022-05-20 18:24:46 +020065#endif
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010066 } else if (gd->flags & GD_FLG_RELOC) {
67 /* bd->bi_dram is available only after relocation */
68 start = bd->bi_dram[bank].start;
69 size = bd->bi_dram[bank].size;
Patrick Delaunayc9468742021-05-07 14:50:35 +020070 use_lmb = true;
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010071 } else {
72 /* mark cacheable and executable the beggining of the DDR */
73 start = STM32_DDR_BASE;
74 size = CONFIG_DDR_CACHEABLE_SIZE;
75 }
76
77 for (i = start >> MMU_SECTION_SHIFT;
78 i < (start >> MMU_SECTION_SHIFT) + (size >> MMU_SECTION_SHIFT);
Patrick Delaunayc9468742021-05-07 14:50:35 +020079 i++) {
80 option = DCACHE_DEFAULT_OPTION;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053081 if (use_lmb && lmb_is_reserved_flags(i << MMU_SECTION_SHIFT, LMB_NOMAP))
Patrick Delaunayc9468742021-05-07 14:50:35 +020082 option = 0; /* INVALID ENTRY in TLB */
83 set_section_dcache(i, option);
84 }
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010085}
86/*
Patrick Delaunay8e6985b2020-04-30 16:30:20 +020087 * initialize the MMU and activate cache in SPL or in U-Boot pre-reloc stage
88 * MMU/TLB is updated in enable_caches() for U-Boot after relocation
89 * or is deactivated in U-Boot entry function start.S::cpu_init_cp15
90 */
91static void early_enable_caches(void)
92{
93 /* I-cache is already enabled in start.S: cpu_init_cp15 */
94
95 if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
96 return;
97
Bhupesh Sharma58af3fb2023-08-22 13:21:11 +053098#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
Patrice Chotard18a87162021-02-24 13:53:27 +010099 gd->arch.tlb_size = PGTABLE_SIZE;
100 gd->arch.tlb_addr = (unsigned long)&early_tlb;
Bhupesh Sharma58af3fb2023-08-22 13:21:11 +0530101#endif
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200102
Patrick Delaunay4ad5a122021-02-05 13:53:33 +0100103 /* enable MMU (default configuration) */
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200104 dcache_enable();
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200105}
106
107/*
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100108 * Early system init
109 */
Patrick Delaunay85b53972018-03-12 10:46:10 +0100110int arch_cpu_init(void)
111{
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200112 early_enable_caches();
113
Patrick Delaunay85b53972018-03-12 10:46:10 +0100114 /* early armv7 timer init: needed for polling */
115 timer_init();
116
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200117 return 0;
118}
119
120/* weak function for SOC specific initialization */
121__weak void stm32mp_cpu_init(void)
122{
123}
124
125int mach_cpu_init(void)
126{
127 u32 boot_mode;
128
129 stm32mp_cpu_init();
Patrick Delaunay82168e82018-05-17 14:50:46 +0200130
Patrick Delaunay82168e82018-05-17 14:50:46 +0200131 boot_mode = get_bootmode();
132
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100133 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) &&
134 (boot_mode & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_UART)
Patrick Delaunay82168e82018-05-17 14:50:46 +0200135 gd->flags |= GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE;
Simon Glass85ed77d2024-09-29 19:49:46 -0600136 else if (IS_ENABLED(CONFIG_DEBUG_UART) && IS_ENABLED(CONFIG_XPL_BUILD))
Patrick Delaunay82168e82018-05-17 14:50:46 +0200137 debug_uart_init();
Patrick Delaunay85b53972018-03-12 10:46:10 +0100138
139 return 0;
140}
141
Patrick Delaunay58e95532018-03-19 19:09:20 +0100142void enable_caches(void)
143{
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200144 /* I-cache is already enabled in start.S: icache_enable() not needed */
145
146 /* deactivate the data cache, early enabled in arch_cpu_init() */
147 dcache_disable();
148 /*
149 * update MMU after relocation and enable the data cache
150 * warning: the TLB location udpated in board_f.c::reserve_mmu
151 */
Patrick Delaunay58e95532018-03-19 19:09:20 +0100152 dcache_enable();
153}
154
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100155static void setup_boot_mode(void)
156{
Patrick Delaunay18660a62019-02-27 17:01:12 +0100157 const u32 serial_addr[] = {
158 STM32_USART1_BASE,
159 STM32_USART2_BASE,
160 STM32_USART3_BASE,
161 STM32_UART4_BASE,
162 STM32_UART5_BASE,
163 STM32_USART6_BASE,
164 STM32_UART7_BASE,
165 STM32_UART8_BASE
166 };
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200167 const u32 sdmmc_addr[] = {
168 STM32_SDMMC1_BASE,
169 STM32_SDMMC2_BASE,
170 STM32_SDMMC3_BASE
171 };
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100172 char cmd[60];
173 u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
174 u32 boot_mode =
175 (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
Patrick Delaunay1b03eb02019-06-21 15:26:39 +0200176 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100177 u32 forced_mode = (boot_ctx & TAMP_BOOT_FORCED_MASK);
Patrick Delaunay18660a62019-02-27 17:01:12 +0100178 struct udevice *dev;
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100179
Patrick Delaunayba779402020-11-06 19:01:29 +0100180 log_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d forced=%x\n",
181 __func__, boot_ctx, boot_mode, instance, forced_mode);
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100182 switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
183 case BOOT_SERIAL_UART:
Rasmus Villemoes6d83f3c2023-03-24 08:55:19 +0100184 if (instance >= ARRAY_SIZE(serial_addr))
Patrick Delaunay18660a62019-02-27 17:01:12 +0100185 break;
Patrick Delaunaye2592992021-02-25 13:37:03 +0100186 /* serial : search associated node in devicetree */
Patrick Delaunay18660a62019-02-27 17:01:12 +0100187 sprintf(cmd, "serial@%x", serial_addr[instance]);
Patrick Delaunaye2592992021-02-25 13:37:03 +0100188 if (uclass_get_device_by_name(UCLASS_SERIAL, cmd, &dev)) {
Patrick Delaunay7540d872021-02-25 13:37:02 +0100189 /* restore console on error */
190 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL))
191 gd->flags &= ~(GD_FLG_SILENT |
192 GD_FLG_DISABLE_CONSOLE);
Patrick Delaunay643e4042021-04-06 09:27:39 +0200193 log_err("uart%d = %s not found in device tree!\n",
194 instance + 1, cmd);
Patrick Delaunay18660a62019-02-27 17:01:12 +0100195 break;
Patrick Delaunay7540d872021-02-25 13:37:02 +0100196 }
Patrick Delaunaye2592992021-02-25 13:37:03 +0100197 sprintf(cmd, "%d", dev_seq(dev));
Patrick Delaunay18660a62019-02-27 17:01:12 +0100198 env_set("boot_device", "serial");
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100199 env_set("boot_instance", cmd);
Patrick Delaunay18660a62019-02-27 17:01:12 +0100200
201 /* restore console on uart when not used */
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100202 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) && gd->cur_serial_dev != dev) {
Patrick Delaunay18660a62019-02-27 17:01:12 +0100203 gd->flags &= ~(GD_FLG_SILENT |
204 GD_FLG_DISABLE_CONSOLE);
Patrick Delaunay643e4042021-04-06 09:27:39 +0200205 log_info("serial boot with console enabled!\n");
Patrick Delaunay18660a62019-02-27 17:01:12 +0100206 }
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100207 break;
208 case BOOT_SERIAL_USB:
209 env_set("boot_device", "usb");
210 env_set("boot_instance", "0");
211 break;
212 case BOOT_FLASH_SD:
213 case BOOT_FLASH_EMMC:
Rasmus Villemoes6d83f3c2023-03-24 08:55:19 +0100214 if (instance >= ARRAY_SIZE(sdmmc_addr))
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200215 break;
216 /* search associated sdmmc node in devicetree */
217 sprintf(cmd, "mmc@%x", sdmmc_addr[instance]);
218 if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) {
219 printf("mmc%d = %s not found in device tree!\n",
220 instance, cmd);
221 break;
222 }
223 sprintf(cmd, "%d", dev_seq(dev));
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100224 env_set("boot_device", "mmc");
225 env_set("boot_instance", cmd);
226 break;
227 case BOOT_FLASH_NAND:
228 env_set("boot_device", "nand");
229 env_set("boot_instance", "0");
230 break;
Patrick Delaunayb5a7ca22020-03-18 09:22:52 +0100231 case BOOT_FLASH_SPINAND:
232 env_set("boot_device", "spi-nand");
233 env_set("boot_instance", "0");
234 break;
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100235 case BOOT_FLASH_NOR:
236 env_set("boot_device", "nor");
237 env_set("boot_instance", "0");
238 break;
239 default:
Patrick Delaunay02e91972021-07-08 10:53:56 +0200240 env_set("boot_device", "invalid");
241 env_set("boot_instance", "");
242 log_err("unexpected boot mode = %x\n", boot_mode);
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100243 break;
244 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100245
246 switch (forced_mode) {
247 case BOOT_FASTBOOT:
Patrick Delaunay643e4042021-04-06 09:27:39 +0200248 log_info("Enter fastboot!\n");
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100249 env_set("preboot", "env set preboot; fastboot 0");
250 break;
251 case BOOT_STM32PROG:
252 env_set("boot_device", "usb");
253 env_set("boot_instance", "0");
254 break;
255 case BOOT_UMS_MMC0:
256 case BOOT_UMS_MMC1:
257 case BOOT_UMS_MMC2:
Patrick Delaunay643e4042021-04-06 09:27:39 +0200258 log_info("Enter UMS!\n");
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100259 instance = forced_mode - BOOT_UMS_MMC0;
260 sprintf(cmd, "env set preboot; ums 0 mmc %d", instance);
261 env_set("preboot", cmd);
262 break;
263 case BOOT_RECOVERY:
264 env_set("preboot", "env set preboot; run altbootcmd");
265 break;
266 case BOOT_NORMAL:
267 break;
268 default:
Patrick Delaunayba779402020-11-06 19:01:29 +0100269 log_debug("unexpected forced boot mode = %x\n", forced_mode);
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100270 break;
271 }
272
273 /* clear TAMP for next reboot */
274 clrsetbits_le32(TAMP_BOOT_CONTEXT, TAMP_BOOT_FORCED_MASK, BOOT_NORMAL);
Patrick Delaunayf3674a42018-05-17 15:24:07 +0200275}
276
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200277__weak void stm32mp_misc_init(void)
Marek Vasut0eda28c2021-03-31 14:15:09 +0200278{
Igor Opaniuk100e0ec2023-11-06 11:41:52 +0100279}
280
281static int setup_boot_auth_info(void)
282{
283 char buf[10];
284 u32 bootauth = get_bootauth();
285
286 snprintf(buf, sizeof(buf), "%d", bootauth >> 4);
287 env_set("boot_auth", buf);
288
289 snprintf(buf, sizeof(buf), "%d", bootauth &
290 (u32)TAMP_BOOT_PARTITION_MASK);
291 env_set("boot_part", buf);
292
293 return 0;
Marek Vasut0eda28c2021-03-31 14:15:09 +0200294}
295
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100296int arch_misc_init(void)
297{
Igor Opaniuk100e0ec2023-11-06 11:41:52 +0100298 setup_boot_auth_info();
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100299 setup_boot_mode();
Patrick Delaunayf3674a42018-05-17 15:24:07 +0200300 setup_mac_address();
301 setup_serial_number();
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200302 stm32mp_misc_init();
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100303
304 return 0;
305}
Marek Vasutefdedcb2023-01-12 18:58:40 +0100306
307/*
308 * Without forcing the ".data" section, this would get saved in ".bss". BSS
309 * will be cleared soon after, so it's not suitable.
310 */
311static uintptr_t rom_api_table __section(".data");
312static uintptr_t nt_fw_dtb __section(".data");
313
314/*
315 * The ROM gives us the API location in r0 when starting. This is only available
316 * during SPL, as there isn't (yet) a mechanism to pass this on to u-boot. Save
317 * the FDT address provided by TF-A in r2 at boot time. This function is called
318 * from start.S
319 */
320void save_boot_params(unsigned long r0, unsigned long r1, unsigned long r2,
321 unsigned long r3)
322{
323 if (IS_ENABLED(CONFIG_STM32_ECDSA_VERIFY))
324 rom_api_table = r0;
325
326 if (IS_ENABLED(CONFIG_TFABOOT))
327 nt_fw_dtb = r2;
328
329 save_boot_params_ret();
330}
331
332uintptr_t get_stm32mp_rom_api_table(void)
333{
334 return rom_api_table;
335}
336
337uintptr_t get_stm32mp_bl2_dtb(void)
338{
339 return nt_fw_dtb;
340}
Marek Vasut7cf2c332023-01-12 18:58:41 +0100341
Simon Glass85ed77d2024-09-29 19:49:46 -0600342#ifdef CONFIG_XPL_BUILD
Marek Vasut7cf2c332023-01-12 18:58:41 +0100343void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
344{
345 typedef void __noreturn (*image_entry_stm32_t)(u32 romapi);
346 uintptr_t romapi = get_stm32mp_rom_api_table();
347
348 image_entry_stm32_t image_entry =
349 (image_entry_stm32_t)spl_image->entry_point;
350
351 printf("image entry point: 0x%lx\n", spl_image->entry_point);
352 image_entry(romapi);
353}
354#endif