blob: 18175fd12ccd3767ae15c923f6261236778ecce7 [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;
Patrice Chotard226dc382024-12-13 14:26:55 +010056 phys_addr_t addr;
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010057 phys_size_t size;
Patrick Delaunayc9468742021-05-07 14:50:35 +020058 bool use_lmb = false;
59 enum dcache_option option;
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010060
Simon Glass85ed77d2024-09-29 19:49:46 -060061 if (IS_ENABLED(CONFIG_XPL_BUILD)) {
Patrick Delaunay123687c2022-05-20 18:24:46 +020062/* STM32_SYSRAM_BASE exist only when SPL is supported */
63#ifdef CONFIG_SPL
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010064 start = ALIGN_DOWN(STM32_SYSRAM_BASE, MMU_SECTION_SIZE);
65 size = ALIGN(STM32_SYSRAM_SIZE, MMU_SECTION_SIZE);
Patrick Delaunay123687c2022-05-20 18:24:46 +020066#endif
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010067 } else if (gd->flags & GD_FLG_RELOC) {
68 /* bd->bi_dram is available only after relocation */
69 start = bd->bi_dram[bank].start;
70 size = bd->bi_dram[bank].size;
Patrick Delaunayc9468742021-05-07 14:50:35 +020071 use_lmb = true;
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010072 } else {
73 /* mark cacheable and executable the beggining of the DDR */
74 start = STM32_DDR_BASE;
75 size = CONFIG_DDR_CACHEABLE_SIZE;
76 }
77
78 for (i = start >> MMU_SECTION_SHIFT;
79 i < (start >> MMU_SECTION_SHIFT) + (size >> MMU_SECTION_SHIFT);
Patrick Delaunayc9468742021-05-07 14:50:35 +020080 i++) {
Patrice Chotard226dc382024-12-13 14:26:55 +010081 addr = i << MMU_SECTION_SHIFT;
Patrick Delaunayc9468742021-05-07 14:50:35 +020082 option = DCACHE_DEFAULT_OPTION;
Patrice Chotard226dc382024-12-13 14:26:55 +010083 if (use_lmb &&
84 (lmb_is_reserved_flags(i << MMU_SECTION_SHIFT, LMB_NOMAP) ||
Marek Vasutddc6e822025-03-09 03:05:54 +010085 (gd->ram_top && addr >= gd->ram_top))
Patrice Chotard226dc382024-12-13 14:26:55 +010086 )
Patrick Delaunayc9468742021-05-07 14:50:35 +020087 option = 0; /* INVALID ENTRY in TLB */
88 set_section_dcache(i, option);
89 }
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010090}
91/*
Patrick Delaunay8e6985b2020-04-30 16:30:20 +020092 * initialize the MMU and activate cache in SPL or in U-Boot pre-reloc stage
93 * MMU/TLB is updated in enable_caches() for U-Boot after relocation
94 * or is deactivated in U-Boot entry function start.S::cpu_init_cp15
95 */
96static void early_enable_caches(void)
97{
98 /* I-cache is already enabled in start.S: cpu_init_cp15 */
99
100 if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
101 return;
102
Bhupesh Sharma58af3fb2023-08-22 13:21:11 +0530103#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
Patrice Chotard18a87162021-02-24 13:53:27 +0100104 gd->arch.tlb_size = PGTABLE_SIZE;
105 gd->arch.tlb_addr = (unsigned long)&early_tlb;
Bhupesh Sharma58af3fb2023-08-22 13:21:11 +0530106#endif
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200107
Patrick Delaunay4ad5a122021-02-05 13:53:33 +0100108 /* enable MMU (default configuration) */
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200109 dcache_enable();
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200110}
111
112/*
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100113 * Early system init
114 */
Patrick Delaunay85b53972018-03-12 10:46:10 +0100115int arch_cpu_init(void)
116{
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200117 early_enable_caches();
118
Patrick Delaunay85b53972018-03-12 10:46:10 +0100119 /* early armv7 timer init: needed for polling */
120 timer_init();
121
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200122 return 0;
123}
124
125/* weak function for SOC specific initialization */
126__weak void stm32mp_cpu_init(void)
127{
128}
129
130int mach_cpu_init(void)
131{
132 u32 boot_mode;
133
134 stm32mp_cpu_init();
Patrick Delaunay82168e82018-05-17 14:50:46 +0200135
Patrick Delaunay82168e82018-05-17 14:50:46 +0200136 boot_mode = get_bootmode();
137
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100138 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) &&
139 (boot_mode & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_UART)
Patrick Delaunay82168e82018-05-17 14:50:46 +0200140 gd->flags |= GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE;
Patrick Delaunay85b53972018-03-12 10:46:10 +0100141
142 return 0;
143}
144
Patrick Delaunay58e95532018-03-19 19:09:20 +0100145void enable_caches(void)
146{
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200147 /* I-cache is already enabled in start.S: icache_enable() not needed */
148
Patrick Delaunaye880ea62024-10-11 17:31:49 +0200149 /* keep D-cache configuration done before relocation, wait arch_early_init_r*/
150}
151
152int arch_early_init_r(void)
153{
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200154 /* deactivate the data cache, early enabled in arch_cpu_init() */
155 dcache_disable();
156 /*
157 * update MMU after relocation and enable the data cache
158 * warning: the TLB location udpated in board_f.c::reserve_mmu
159 */
Patrick Delaunay58e95532018-03-19 19:09:20 +0100160 dcache_enable();
Patrick Delaunaye880ea62024-10-11 17:31:49 +0200161
162 return 0;
Patrick Delaunay58e95532018-03-19 19:09:20 +0100163}
164
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100165static void setup_boot_mode(void)
166{
Patrick Delaunay18660a62019-02-27 17:01:12 +0100167 const u32 serial_addr[] = {
168 STM32_USART1_BASE,
169 STM32_USART2_BASE,
170 STM32_USART3_BASE,
171 STM32_UART4_BASE,
172 STM32_UART5_BASE,
173 STM32_USART6_BASE,
174 STM32_UART7_BASE,
175 STM32_UART8_BASE
176 };
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200177 const u32 sdmmc_addr[] = {
178 STM32_SDMMC1_BASE,
179 STM32_SDMMC2_BASE,
180 STM32_SDMMC3_BASE
181 };
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100182 char cmd[60];
183 u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
184 u32 boot_mode =
185 (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
Patrick Delaunay1b03eb02019-06-21 15:26:39 +0200186 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100187 u32 forced_mode = (boot_ctx & TAMP_BOOT_FORCED_MASK);
Patrick Delaunay18660a62019-02-27 17:01:12 +0100188 struct udevice *dev;
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100189
Patrick Delaunayba779402020-11-06 19:01:29 +0100190 log_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d forced=%x\n",
191 __func__, boot_ctx, boot_mode, instance, forced_mode);
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100192 switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
193 case BOOT_SERIAL_UART:
Rasmus Villemoes6d83f3c2023-03-24 08:55:19 +0100194 if (instance >= ARRAY_SIZE(serial_addr))
Patrick Delaunay18660a62019-02-27 17:01:12 +0100195 break;
Patrick Delaunaye2592992021-02-25 13:37:03 +0100196 /* serial : search associated node in devicetree */
Patrick Delaunay18660a62019-02-27 17:01:12 +0100197 sprintf(cmd, "serial@%x", serial_addr[instance]);
Patrick Delaunaye2592992021-02-25 13:37:03 +0100198 if (uclass_get_device_by_name(UCLASS_SERIAL, cmd, &dev)) {
Patrick Delaunay7540d872021-02-25 13:37:02 +0100199 /* restore console on error */
200 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL))
201 gd->flags &= ~(GD_FLG_SILENT |
202 GD_FLG_DISABLE_CONSOLE);
Patrick Delaunay643e4042021-04-06 09:27:39 +0200203 log_err("uart%d = %s not found in device tree!\n",
204 instance + 1, cmd);
Patrick Delaunay18660a62019-02-27 17:01:12 +0100205 break;
Patrick Delaunay7540d872021-02-25 13:37:02 +0100206 }
Patrick Delaunaye2592992021-02-25 13:37:03 +0100207 sprintf(cmd, "%d", dev_seq(dev));
Patrick Delaunay18660a62019-02-27 17:01:12 +0100208 env_set("boot_device", "serial");
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100209 env_set("boot_instance", cmd);
Patrick Delaunay18660a62019-02-27 17:01:12 +0100210
211 /* restore console on uart when not used */
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100212 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) && gd->cur_serial_dev != dev) {
Patrick Delaunay18660a62019-02-27 17:01:12 +0100213 gd->flags &= ~(GD_FLG_SILENT |
214 GD_FLG_DISABLE_CONSOLE);
Patrick Delaunay643e4042021-04-06 09:27:39 +0200215 log_info("serial boot with console enabled!\n");
Patrick Delaunay18660a62019-02-27 17:01:12 +0100216 }
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100217 break;
218 case BOOT_SERIAL_USB:
219 env_set("boot_device", "usb");
220 env_set("boot_instance", "0");
221 break;
222 case BOOT_FLASH_SD:
223 case BOOT_FLASH_EMMC:
Rasmus Villemoes6d83f3c2023-03-24 08:55:19 +0100224 if (instance >= ARRAY_SIZE(sdmmc_addr))
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200225 break;
226 /* search associated sdmmc node in devicetree */
227 sprintf(cmd, "mmc@%x", sdmmc_addr[instance]);
228 if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) {
229 printf("mmc%d = %s not found in device tree!\n",
230 instance, cmd);
231 break;
232 }
233 sprintf(cmd, "%d", dev_seq(dev));
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100234 env_set("boot_device", "mmc");
235 env_set("boot_instance", cmd);
236 break;
237 case BOOT_FLASH_NAND:
238 env_set("boot_device", "nand");
239 env_set("boot_instance", "0");
240 break;
Patrick Delaunayb5a7ca22020-03-18 09:22:52 +0100241 case BOOT_FLASH_SPINAND:
242 env_set("boot_device", "spi-nand");
243 env_set("boot_instance", "0");
244 break;
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100245 case BOOT_FLASH_NOR:
246 env_set("boot_device", "nor");
247 env_set("boot_instance", "0");
248 break;
249 default:
Patrick Delaunay02e91972021-07-08 10:53:56 +0200250 env_set("boot_device", "invalid");
251 env_set("boot_instance", "");
252 log_err("unexpected boot mode = %x\n", boot_mode);
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100253 break;
254 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100255
256 switch (forced_mode) {
257 case BOOT_FASTBOOT:
Patrick Delaunay643e4042021-04-06 09:27:39 +0200258 log_info("Enter fastboot!\n");
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100259 env_set("preboot", "env set preboot; fastboot 0");
260 break;
261 case BOOT_STM32PROG:
262 env_set("boot_device", "usb");
263 env_set("boot_instance", "0");
264 break;
265 case BOOT_UMS_MMC0:
266 case BOOT_UMS_MMC1:
267 case BOOT_UMS_MMC2:
Patrick Delaunay643e4042021-04-06 09:27:39 +0200268 log_info("Enter UMS!\n");
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100269 instance = forced_mode - BOOT_UMS_MMC0;
270 sprintf(cmd, "env set preboot; ums 0 mmc %d", instance);
271 env_set("preboot", cmd);
272 break;
273 case BOOT_RECOVERY:
274 env_set("preboot", "env set preboot; run altbootcmd");
275 break;
276 case BOOT_NORMAL:
277 break;
278 default:
Patrick Delaunayba779402020-11-06 19:01:29 +0100279 log_debug("unexpected forced boot mode = %x\n", forced_mode);
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100280 break;
281 }
282
283 /* clear TAMP for next reboot */
284 clrsetbits_le32(TAMP_BOOT_CONTEXT, TAMP_BOOT_FORCED_MASK, BOOT_NORMAL);
Patrick Delaunayf3674a42018-05-17 15:24:07 +0200285}
286
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200287__weak void stm32mp_misc_init(void)
Marek Vasut0eda28c2021-03-31 14:15:09 +0200288{
Igor Opaniuk100e0ec2023-11-06 11:41:52 +0100289}
290
291static int setup_boot_auth_info(void)
292{
293 char buf[10];
294 u32 bootauth = get_bootauth();
295
296 snprintf(buf, sizeof(buf), "%d", bootauth >> 4);
297 env_set("boot_auth", buf);
298
299 snprintf(buf, sizeof(buf), "%d", bootauth &
300 (u32)TAMP_BOOT_PARTITION_MASK);
301 env_set("boot_part", buf);
302
303 return 0;
Marek Vasut0eda28c2021-03-31 14:15:09 +0200304}
305
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100306int arch_misc_init(void)
307{
Igor Opaniuk100e0ec2023-11-06 11:41:52 +0100308 setup_boot_auth_info();
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100309 setup_boot_mode();
Patrick Delaunayf3674a42018-05-17 15:24:07 +0200310 setup_mac_address();
311 setup_serial_number();
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200312 stm32mp_misc_init();
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100313
314 return 0;
315}
Marek Vasutefdedcb2023-01-12 18:58:40 +0100316
317/*
318 * Without forcing the ".data" section, this would get saved in ".bss". BSS
319 * will be cleared soon after, so it's not suitable.
320 */
321static uintptr_t rom_api_table __section(".data");
322static uintptr_t nt_fw_dtb __section(".data");
323
324/*
325 * The ROM gives us the API location in r0 when starting. This is only available
326 * during SPL, as there isn't (yet) a mechanism to pass this on to u-boot. Save
327 * the FDT address provided by TF-A in r2 at boot time. This function is called
328 * from start.S
329 */
330void save_boot_params(unsigned long r0, unsigned long r1, unsigned long r2,
331 unsigned long r3)
332{
333 if (IS_ENABLED(CONFIG_STM32_ECDSA_VERIFY))
334 rom_api_table = r0;
335
336 if (IS_ENABLED(CONFIG_TFABOOT))
337 nt_fw_dtb = r2;
338
339 save_boot_params_ret();
340}
341
342uintptr_t get_stm32mp_rom_api_table(void)
343{
344 return rom_api_table;
345}
346
347uintptr_t get_stm32mp_bl2_dtb(void)
348{
349 return nt_fw_dtb;
350}
Marek Vasut7cf2c332023-01-12 18:58:41 +0100351
Simon Glass85ed77d2024-09-29 19:49:46 -0600352#ifdef CONFIG_XPL_BUILD
Marek Vasut7cf2c332023-01-12 18:58:41 +0100353void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
354{
355 typedef void __noreturn (*image_entry_stm32_t)(u32 romapi);
356 uintptr_t romapi = get_stm32mp_rom_api_table();
357
358 image_entry_stm32_t image_entry =
359 (image_entry_stm32_t)spl_image->entry_point;
360
361 printf("image entry point: 0x%lx\n", spl_image->entry_point);
362 image_entry(romapi);
363}
364#endif