blob: 524778f00c67d409017d3767cce57601f35ef119 [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 <common.h>
9#include <clk.h>
Simon Glass1d91ba72019-11-14 12:57:37 -070010#include <cpu_func.h>
Patrick Delaunay82168e82018-05-17 14:50:46 +020011#include <debug_uart.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060012#include <env.h>
Simon Glass97589732020-05-10 11:40:02 -060013#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Patrick Delaunayc9468742021-05-07 14:50:35 +020015#include <lmb.h>
Patrick Delaunayf3674a42018-05-17 15:24:07 +020016#include <misc.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060017#include <spl.h>
Patrick Delaunay3fa644b2024-01-15 15:05:51 +010018#include <asm/cache.h>
Patrick Delaunay85b53972018-03-12 10:46:10 +010019#include <asm/io.h>
20#include <asm/arch/stm32.h>
Patrick Delaunay01e3afe2018-03-19 19:09:21 +010021#include <asm/arch/sys_proto.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
Patrick Delaunayf3674a42018-05-17 15:24:07 +020023#include <dm/device.h>
Patrick Delaunayc5d15652018-03-20 10:54:53 +010024#include <dm/uclass.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#include <linux/bitops.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060026#include <linux/printk.h>
Patrick Delaunay85b53972018-03-12 10:46:10 +010027
Patrick Delaunay8e6985b2020-04-30 16:30:20 +020028/*
29 * early TLB into the .data section so that it not get cleared
30 * with 16kB allignment (see TTBR0_BASE_ADDR_MASK)
31 */
32u8 early_tlb[PGTABLE_SIZE] __section(".data") __aligned(0x4000);
33
Patrick Delaunayc9468742021-05-07 14:50:35 +020034struct lmb lmb;
35
Patrick Delaunay18660a62019-02-27 17:01:12 +010036u32 get_bootmode(void)
37{
38 /* read bootmode from TAMP backup register */
39 return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >>
40 TAMP_BOOT_MODE_SHIFT;
Patrick Delaunayc5d15652018-03-20 10:54:53 +010041}
42
Igor Opaniuk100e0ec2023-11-06 11:41:52 +010043u32 get_bootauth(void)
44{
45 /* read boot auth status and partition from TAMP backup register */
46 return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_AUTH_MASK) >>
47 TAMP_BOOT_AUTH_SHIFT;
48}
49
Patrick Delaunayc5d15652018-03-20 10:54:53 +010050/*
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010051 * 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)
53 */
54void dram_bank_mmu_setup(int bank)
55{
56 struct bd_info *bd = gd->bd;
57 int i;
58 phys_addr_t start;
59 phys_size_t size;
Patrick Delaunayc9468742021-05-07 14:50:35 +020060 bool use_lmb = false;
61 enum dcache_option option;
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010062
63 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
Patrick Delaunay123687c2022-05-20 18:24:46 +020064/* STM32_SYSRAM_BASE exist only when SPL is supported */
65#ifdef CONFIG_SPL
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010066 start = ALIGN_DOWN(STM32_SYSRAM_BASE, MMU_SECTION_SIZE);
67 size = ALIGN(STM32_SYSRAM_SIZE, MMU_SECTION_SIZE);
Patrick Delaunay123687c2022-05-20 18:24:46 +020068#endif
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010069 } 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;
Patrick Delaunayc9468742021-05-07 14:50:35 +020073 use_lmb = true;
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010074 } else {
75 /* mark cacheable and executable the beggining of the DDR */
76 start = STM32_DDR_BASE;
77 size = CONFIG_DDR_CACHEABLE_SIZE;
78 }
79
80 for (i = start >> MMU_SECTION_SHIFT;
81 i < (start >> MMU_SECTION_SHIFT) + (size >> MMU_SECTION_SHIFT);
Patrick Delaunayc9468742021-05-07 14:50:35 +020082 i++) {
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);
87 }
Patrick Delaunay4ad5a122021-02-05 13:53:33 +010088}
89/*
Patrick Delaunay8e6985b2020-04-30 16:30:20 +020090 * 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
93 */
94static void early_enable_caches(void)
95{
96 /* I-cache is already enabled in start.S: cpu_init_cp15 */
97
98 if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
99 return;
100
Bhupesh Sharma58af3fb2023-08-22 13:21:11 +0530101#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
Patrice Chotard18a87162021-02-24 13:53:27 +0100102 gd->arch.tlb_size = PGTABLE_SIZE;
103 gd->arch.tlb_addr = (unsigned long)&early_tlb;
Bhupesh Sharma58af3fb2023-08-22 13:21:11 +0530104#endif
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200105
Patrick Delaunay4ad5a122021-02-05 13:53:33 +0100106 /* enable MMU (default configuration) */
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200107 dcache_enable();
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200108}
109
110/*
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100111 * Early system init
112 */
Patrick Delaunay85b53972018-03-12 10:46:10 +0100113int arch_cpu_init(void)
114{
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200115 early_enable_caches();
116
Patrick Delaunay85b53972018-03-12 10:46:10 +0100117 /* early armv7 timer init: needed for polling */
118 timer_init();
119
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200120 return 0;
121}
122
123/* weak function for SOC specific initialization */
124__weak void stm32mp_cpu_init(void)
125{
126}
127
128int mach_cpu_init(void)
129{
130 u32 boot_mode;
131
132 stm32mp_cpu_init();
Patrick Delaunay82168e82018-05-17 14:50:46 +0200133
Patrick Delaunay82168e82018-05-17 14:50:46 +0200134 boot_mode = get_bootmode();
135
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100136 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) &&
137 (boot_mode & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_UART)
Patrick Delaunay82168e82018-05-17 14:50:46 +0200138 gd->flags |= GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE;
Patrick Delaunayd8299de2021-10-11 09:52:51 +0200139 else if (IS_ENABLED(CONFIG_DEBUG_UART) && IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay82168e82018-05-17 14:50:46 +0200140 debug_uart_init();
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 Delaunayc9468742021-05-07 14:50:35 +0200147 /* parse device tree when data cache is still activated */
148 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
149
Patrick Delaunay8e6985b2020-04-30 16:30:20 +0200150 /* I-cache is already enabled in start.S: icache_enable() not needed */
151
152 /* deactivate the data cache, early enabled in arch_cpu_init() */
153 dcache_disable();
154 /*
155 * update MMU after relocation and enable the data cache
156 * warning: the TLB location udpated in board_f.c::reserve_mmu
157 */
Patrick Delaunay58e95532018-03-19 19:09:20 +0100158 dcache_enable();
159}
160
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100161static void setup_boot_mode(void)
162{
Patrick Delaunay18660a62019-02-27 17:01:12 +0100163 const u32 serial_addr[] = {
164 STM32_USART1_BASE,
165 STM32_USART2_BASE,
166 STM32_USART3_BASE,
167 STM32_UART4_BASE,
168 STM32_UART5_BASE,
169 STM32_USART6_BASE,
170 STM32_UART7_BASE,
171 STM32_UART8_BASE
172 };
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200173 const u32 sdmmc_addr[] = {
174 STM32_SDMMC1_BASE,
175 STM32_SDMMC2_BASE,
176 STM32_SDMMC3_BASE
177 };
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100178 char cmd[60];
179 u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
180 u32 boot_mode =
181 (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
Patrick Delaunay1b03eb02019-06-21 15:26:39 +0200182 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100183 u32 forced_mode = (boot_ctx & TAMP_BOOT_FORCED_MASK);
Patrick Delaunay18660a62019-02-27 17:01:12 +0100184 struct udevice *dev;
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100185
Patrick Delaunayba779402020-11-06 19:01:29 +0100186 log_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d forced=%x\n",
187 __func__, boot_ctx, boot_mode, instance, forced_mode);
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100188 switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
189 case BOOT_SERIAL_UART:
Rasmus Villemoes6d83f3c2023-03-24 08:55:19 +0100190 if (instance >= ARRAY_SIZE(serial_addr))
Patrick Delaunay18660a62019-02-27 17:01:12 +0100191 break;
Patrick Delaunaye2592992021-02-25 13:37:03 +0100192 /* serial : search associated node in devicetree */
Patrick Delaunay18660a62019-02-27 17:01:12 +0100193 sprintf(cmd, "serial@%x", serial_addr[instance]);
Patrick Delaunaye2592992021-02-25 13:37:03 +0100194 if (uclass_get_device_by_name(UCLASS_SERIAL, cmd, &dev)) {
Patrick Delaunay7540d872021-02-25 13:37:02 +0100195 /* restore console on error */
196 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL))
197 gd->flags &= ~(GD_FLG_SILENT |
198 GD_FLG_DISABLE_CONSOLE);
Patrick Delaunay643e4042021-04-06 09:27:39 +0200199 log_err("uart%d = %s not found in device tree!\n",
200 instance + 1, cmd);
Patrick Delaunay18660a62019-02-27 17:01:12 +0100201 break;
Patrick Delaunay7540d872021-02-25 13:37:02 +0100202 }
Patrick Delaunaye2592992021-02-25 13:37:03 +0100203 sprintf(cmd, "%d", dev_seq(dev));
Patrick Delaunay18660a62019-02-27 17:01:12 +0100204 env_set("boot_device", "serial");
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100205 env_set("boot_instance", cmd);
Patrick Delaunay18660a62019-02-27 17:01:12 +0100206
207 /* restore console on uart when not used */
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100208 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) && gd->cur_serial_dev != dev) {
Patrick Delaunay18660a62019-02-27 17:01:12 +0100209 gd->flags &= ~(GD_FLG_SILENT |
210 GD_FLG_DISABLE_CONSOLE);
Patrick Delaunay643e4042021-04-06 09:27:39 +0200211 log_info("serial boot with console enabled!\n");
Patrick Delaunay18660a62019-02-27 17:01:12 +0100212 }
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100213 break;
214 case BOOT_SERIAL_USB:
215 env_set("boot_device", "usb");
216 env_set("boot_instance", "0");
217 break;
218 case BOOT_FLASH_SD:
219 case BOOT_FLASH_EMMC:
Rasmus Villemoes6d83f3c2023-03-24 08:55:19 +0100220 if (instance >= ARRAY_SIZE(sdmmc_addr))
Patrick Delaunay5c2f6d72021-07-06 17:19:45 +0200221 break;
222 /* search associated sdmmc node in devicetree */
223 sprintf(cmd, "mmc@%x", sdmmc_addr[instance]);
224 if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) {
225 printf("mmc%d = %s not found in device tree!\n",
226 instance, cmd);
227 break;
228 }
229 sprintf(cmd, "%d", dev_seq(dev));
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100230 env_set("boot_device", "mmc");
231 env_set("boot_instance", cmd);
232 break;
233 case BOOT_FLASH_NAND:
234 env_set("boot_device", "nand");
235 env_set("boot_instance", "0");
236 break;
Patrick Delaunayb5a7ca22020-03-18 09:22:52 +0100237 case BOOT_FLASH_SPINAND:
238 env_set("boot_device", "spi-nand");
239 env_set("boot_instance", "0");
240 break;
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100241 case BOOT_FLASH_NOR:
242 env_set("boot_device", "nor");
243 env_set("boot_instance", "0");
244 break;
245 default:
Patrick Delaunay02e91972021-07-08 10:53:56 +0200246 env_set("boot_device", "invalid");
247 env_set("boot_instance", "");
248 log_err("unexpected boot mode = %x\n", boot_mode);
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100249 break;
250 }
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100251
252 switch (forced_mode) {
253 case BOOT_FASTBOOT:
Patrick Delaunay643e4042021-04-06 09:27:39 +0200254 log_info("Enter fastboot!\n");
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100255 env_set("preboot", "env set preboot; fastboot 0");
256 break;
257 case BOOT_STM32PROG:
258 env_set("boot_device", "usb");
259 env_set("boot_instance", "0");
260 break;
261 case BOOT_UMS_MMC0:
262 case BOOT_UMS_MMC1:
263 case BOOT_UMS_MMC2:
Patrick Delaunay643e4042021-04-06 09:27:39 +0200264 log_info("Enter UMS!\n");
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100265 instance = forced_mode - BOOT_UMS_MMC0;
266 sprintf(cmd, "env set preboot; ums 0 mmc %d", instance);
267 env_set("preboot", cmd);
268 break;
269 case BOOT_RECOVERY:
270 env_set("preboot", "env set preboot; run altbootcmd");
271 break;
272 case BOOT_NORMAL:
273 break;
274 default:
Patrick Delaunayba779402020-11-06 19:01:29 +0100275 log_debug("unexpected forced boot mode = %x\n", forced_mode);
Patrick Delaunay008d3c32019-02-27 17:01:20 +0100276 break;
277 }
278
279 /* clear TAMP for next reboot */
280 clrsetbits_le32(TAMP_BOOT_CONTEXT, TAMP_BOOT_FORCED_MASK, BOOT_NORMAL);
Patrick Delaunayf3674a42018-05-17 15:24:07 +0200281}
282
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200283__weak void stm32mp_misc_init(void)
Marek Vasut0eda28c2021-03-31 14:15:09 +0200284{
Igor Opaniuk100e0ec2023-11-06 11:41:52 +0100285}
286
287static int setup_boot_auth_info(void)
288{
289 char buf[10];
290 u32 bootauth = get_bootauth();
291
292 snprintf(buf, sizeof(buf), "%d", bootauth >> 4);
293 env_set("boot_auth", buf);
294
295 snprintf(buf, sizeof(buf), "%d", bootauth &
296 (u32)TAMP_BOOT_PARTITION_MASK);
297 env_set("boot_part", buf);
298
299 return 0;
Marek Vasut0eda28c2021-03-31 14:15:09 +0200300}
301
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100302int arch_misc_init(void)
303{
Igor Opaniuk100e0ec2023-11-06 11:41:52 +0100304 setup_boot_auth_info();
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100305 setup_boot_mode();
Patrick Delaunayf3674a42018-05-17 15:24:07 +0200306 setup_mac_address();
307 setup_serial_number();
Patrick Delaunaye4bdd542022-05-20 18:24:42 +0200308 stm32mp_misc_init();
Patrick Delaunayc5d15652018-03-20 10:54:53 +0100309
310 return 0;
311}
Marek Vasutefdedcb2023-01-12 18:58:40 +0100312
313/*
314 * Without forcing the ".data" section, this would get saved in ".bss". BSS
315 * will be cleared soon after, so it's not suitable.
316 */
317static uintptr_t rom_api_table __section(".data");
318static uintptr_t nt_fw_dtb __section(".data");
319
320/*
321 * The ROM gives us the API location in r0 when starting. This is only available
322 * during SPL, as there isn't (yet) a mechanism to pass this on to u-boot. Save
323 * the FDT address provided by TF-A in r2 at boot time. This function is called
324 * from start.S
325 */
326void save_boot_params(unsigned long r0, unsigned long r1, unsigned long r2,
327 unsigned long r3)
328{
329 if (IS_ENABLED(CONFIG_STM32_ECDSA_VERIFY))
330 rom_api_table = r0;
331
332 if (IS_ENABLED(CONFIG_TFABOOT))
333 nt_fw_dtb = r2;
334
335 save_boot_params_ret();
336}
337
338uintptr_t get_stm32mp_rom_api_table(void)
339{
340 return rom_api_table;
341}
342
343uintptr_t get_stm32mp_bl2_dtb(void)
344{
345 return nt_fw_dtb;
346}
Marek Vasut7cf2c332023-01-12 18:58:41 +0100347
348#ifdef CONFIG_SPL_BUILD
349void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
350{
351 typedef void __noreturn (*image_entry_stm32_t)(u32 romapi);
352 uintptr_t romapi = get_stm32mp_rom_api_table();
353
354 image_entry_stm32_t image_entry =
355 (image_entry_stm32_t)spl_image->entry_point;
356
357 printf("image entry point: 0x%lx\n", spl_image->entry_point);
358 image_entry(romapi);
359}
360#endif