blob: b808964d3e4ebf9b4400e258e93f279dc388cdcb [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunay2514c2d2018-03-12 10:46:10 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunay2514c2d2018-03-12 10:46:10 +01004 */
Patrick Delaunayeb653ac2020-11-06 19:01:29 +01005
6#define LOG_CATEGORY LOGC_ARCH
7
Patrick Delaunay2514c2d2018-03-12 10:46:10 +01008#include <common.h>
9#include <clk.h>
Simon Glass9edefc22019-11-14 12:57:37 -070010#include <cpu_func.h>
Patrick Delaunay320d2662018-05-17 14:50:46 +020011#include <debug_uart.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060012#include <env.h>
Simon Glass691d7192020-05-10 11:40:02 -060013#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Patrick Delaunayade4e042021-05-07 14:50:35 +020015#include <lmb.h>
Patrick Delaunay7f7deb02018-05-17 15:24:07 +020016#include <misc.h>
Simon Glass90526e92020-05-10 11:39:56 -060017#include <net.h>
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010018#include <asm/io.h>
19#include <asm/arch/stm32.h>
Patrick Delaunay96583cd2018-03-19 19:09:21 +010020#include <asm/arch/sys_proto.h>
Simon Glass401d1c42020-10-30 21:38:53 -060021#include <asm/global_data.h>
Patrick Delaunay7f7deb02018-05-17 15:24:07 +020022#include <dm/device.h>
Patrick Delaunay08772f62018-03-20 10:54:53 +010023#include <dm/uclass.h>
Simon Glasscd93d622020-05-10 11:40:13 -060024#include <linux/bitops.h>
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010025
Patrick Delaunay7e8471c2020-04-30 16:30:20 +020026/*
27 * early TLB into the .data section so that it not get cleared
28 * with 16kB allignment (see TTBR0_BASE_ADDR_MASK)
29 */
30u8 early_tlb[PGTABLE_SIZE] __section(".data") __aligned(0x4000);
31
Patrick Delaunayade4e042021-05-07 14:50:35 +020032struct lmb lmb;
33
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +010034u32 get_bootmode(void)
35{
36 /* read bootmode from TAMP backup register */
37 return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >>
38 TAMP_BOOT_MODE_SHIFT;
Patrick Delaunay08772f62018-03-20 10:54:53 +010039}
40
41/*
Patrick Delaunayaad84142021-02-05 13:53:33 +010042 * weak function overidde: set the DDR/SYSRAM executable before to enable the
43 * MMU and configure DACR, for early early_enable_caches (SPL or pre-reloc)
44 */
45void dram_bank_mmu_setup(int bank)
46{
47 struct bd_info *bd = gd->bd;
48 int i;
49 phys_addr_t start;
50 phys_size_t size;
Patrick Delaunayade4e042021-05-07 14:50:35 +020051 bool use_lmb = false;
52 enum dcache_option option;
Patrick Delaunayaad84142021-02-05 13:53:33 +010053
54 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
55 start = ALIGN_DOWN(STM32_SYSRAM_BASE, MMU_SECTION_SIZE);
56 size = ALIGN(STM32_SYSRAM_SIZE, MMU_SECTION_SIZE);
57 } else if (gd->flags & GD_FLG_RELOC) {
58 /* bd->bi_dram is available only after relocation */
59 start = bd->bi_dram[bank].start;
60 size = bd->bi_dram[bank].size;
Patrick Delaunayade4e042021-05-07 14:50:35 +020061 use_lmb = true;
Patrick Delaunayaad84142021-02-05 13:53:33 +010062 } else {
63 /* mark cacheable and executable the beggining of the DDR */
64 start = STM32_DDR_BASE;
65 size = CONFIG_DDR_CACHEABLE_SIZE;
66 }
67
68 for (i = start >> MMU_SECTION_SHIFT;
69 i < (start >> MMU_SECTION_SHIFT) + (size >> MMU_SECTION_SHIFT);
Patrick Delaunayade4e042021-05-07 14:50:35 +020070 i++) {
71 option = DCACHE_DEFAULT_OPTION;
72 if (use_lmb && lmb_is_reserved_flags(&lmb, i << MMU_SECTION_SHIFT, LMB_NOMAP))
73 option = 0; /* INVALID ENTRY in TLB */
74 set_section_dcache(i, option);
75 }
Patrick Delaunayaad84142021-02-05 13:53:33 +010076}
77/*
Patrick Delaunay7e8471c2020-04-30 16:30:20 +020078 * initialize the MMU and activate cache in SPL or in U-Boot pre-reloc stage
79 * MMU/TLB is updated in enable_caches() for U-Boot after relocation
80 * or is deactivated in U-Boot entry function start.S::cpu_init_cp15
81 */
82static void early_enable_caches(void)
83{
84 /* I-cache is already enabled in start.S: cpu_init_cp15 */
85
86 if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
87 return;
88
Patrice Chotard23e20b22021-02-24 13:53:27 +010089 if (!(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))) {
90 gd->arch.tlb_size = PGTABLE_SIZE;
91 gd->arch.tlb_addr = (unsigned long)&early_tlb;
92 }
Patrick Delaunay7e8471c2020-04-30 16:30:20 +020093
Patrick Delaunayaad84142021-02-05 13:53:33 +010094 /* enable MMU (default configuration) */
Patrick Delaunay7e8471c2020-04-30 16:30:20 +020095 dcache_enable();
Patrick Delaunay7e8471c2020-04-30 16:30:20 +020096}
97
98/*
Patrick Delaunay08772f62018-03-20 10:54:53 +010099 * Early system init
100 */
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100101int arch_cpu_init(void)
102{
Patrick Delaunay7e8471c2020-04-30 16:30:20 +0200103 early_enable_caches();
104
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100105 /* early armv7 timer init: needed for polling */
106 timer_init();
107
Patrick Delaunay6df271a2022-05-20 18:24:42 +0200108 return 0;
109}
110
111/* weak function for SOC specific initialization */
112__weak void stm32mp_cpu_init(void)
113{
114}
115
116int mach_cpu_init(void)
117{
118 u32 boot_mode;
119
120 stm32mp_cpu_init();
Patrick Delaunay320d2662018-05-17 14:50:46 +0200121
Patrick Delaunay320d2662018-05-17 14:50:46 +0200122 boot_mode = get_bootmode();
123
Patrick Delaunay5a05af82021-02-25 13:37:01 +0100124 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) &&
125 (boot_mode & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_UART)
Patrick Delaunay320d2662018-05-17 14:50:46 +0200126 gd->flags |= GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE;
Patrick Delaunayc8b2eef2021-10-11 09:52:51 +0200127 else if (IS_ENABLED(CONFIG_DEBUG_UART) && IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay320d2662018-05-17 14:50:46 +0200128 debug_uart_init();
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100129
130 return 0;
131}
132
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +0100133void enable_caches(void)
134{
Patrick Delaunayade4e042021-05-07 14:50:35 +0200135 /* parse device tree when data cache is still activated */
136 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
137
Patrick Delaunay7e8471c2020-04-30 16:30:20 +0200138 /* I-cache is already enabled in start.S: icache_enable() not needed */
139
140 /* deactivate the data cache, early enabled in arch_cpu_init() */
141 dcache_disable();
142 /*
143 * update MMU after relocation and enable the data cache
144 * warning: the TLB location udpated in board_f.c::reserve_mmu
145 */
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +0100146 dcache_enable();
147}
148
Patrick Delaunayc8b2eef2021-10-11 09:52:51 +0200149/* used when CONFIG_DISPLAY_CPUINFO is activated */
Patrick Delaunayac5e4d82020-02-12 19:37:43 +0100150int print_cpuinfo(void)
151{
152 char name[SOC_NAME_SIZE];
153
154 get_soc_name(name);
155 printf("CPU: %s\n", name);
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100156
157 return 0;
158}
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100159
Patrick Delaunay08772f62018-03-20 10:54:53 +0100160static void setup_boot_mode(void)
161{
Patrick Delaunay7f63c1e2019-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 Delaunay3c1057c2021-07-06 17:19:45 +0200172 const u32 sdmmc_addr[] = {
173 STM32_SDMMC1_BASE,
174 STM32_SDMMC2_BASE,
175 STM32_SDMMC3_BASE
176 };
Patrick Delaunay08772f62018-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 Delaunaye609e132019-06-21 15:26:39 +0200181 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100182 u32 forced_mode = (boot_ctx & TAMP_BOOT_FORCED_MASK);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100183 struct udevice *dev;
Patrick Delaunay08772f62018-03-20 10:54:53 +0100184
Patrick Delaunayeb653ac2020-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 Delaunay08772f62018-03-20 10:54:53 +0100187 switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
188 case BOOT_SERIAL_UART:
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100189 if (instance > ARRAY_SIZE(serial_addr))
190 break;
Patrick Delaunayf49eb162021-02-25 13:37:03 +0100191 /* serial : search associated node in devicetree */
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100192 sprintf(cmd, "serial@%x", serial_addr[instance]);
Patrick Delaunayf49eb162021-02-25 13:37:03 +0100193 if (uclass_get_device_by_name(UCLASS_SERIAL, cmd, &dev)) {
Patrick Delaunayb9d5e3a2021-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 Delaunaycbea7b32021-04-06 09:27:39 +0200198 log_err("uart%d = %s not found in device tree!\n",
199 instance + 1, cmd);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100200 break;
Patrick Delaunayb9d5e3a2021-02-25 13:37:02 +0100201 }
Patrick Delaunayf49eb162021-02-25 13:37:03 +0100202 sprintf(cmd, "%d", dev_seq(dev));
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100203 env_set("boot_device", "serial");
Patrick Delaunay08772f62018-03-20 10:54:53 +0100204 env_set("boot_instance", cmd);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100205
206 /* restore console on uart when not used */
Patrick Delaunay5a05af82021-02-25 13:37:01 +0100207 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) && gd->cur_serial_dev != dev) {
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100208 gd->flags &= ~(GD_FLG_SILENT |
209 GD_FLG_DISABLE_CONSOLE);
Patrick Delaunaycbea7b32021-04-06 09:27:39 +0200210 log_info("serial boot with console enabled!\n");
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100211 }
Patrick Delaunay08772f62018-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:
Patrick Delaunay3c1057c2021-07-06 17:19:45 +0200219 if (instance > ARRAY_SIZE(sdmmc_addr))
220 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 Delaunay08772f62018-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 Delaunayb664a742020-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 Delaunay08772f62018-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 Delaunay8b71b202021-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 Delaunay08772f62018-03-20 10:54:53 +0100248 break;
249 }
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100250
251 switch (forced_mode) {
252 case BOOT_FASTBOOT:
Patrick Delaunaycbea7b32021-04-06 09:27:39 +0200253 log_info("Enter fastboot!\n");
Patrick Delaunay9a2ba282019-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 Delaunaycbea7b32021-04-06 09:27:39 +0200263 log_info("Enter UMS!\n");
Patrick Delaunay9a2ba282019-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 Delaunayeb653ac2020-11-06 19:01:29 +0100274 log_debug("unexpected forced boot mode = %x\n", forced_mode);
Patrick Delaunay9a2ba282019-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 Delaunay08772f62018-03-20 10:54:53 +0100280}
281
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200282/*
283 * If there is no MAC address in the environment, then it will be initialized
284 * (silently) from the value in the OTP.
285 */
Marek Vasute71b9a62019-12-18 16:52:19 +0100286__weak int setup_mac_address(void)
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200287{
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200288 int ret;
289 int i;
290 u32 otp[2];
291 uchar enetaddr[6];
292 struct udevice *dev;
293
Patrick Delaunayc8b2eef2021-10-11 09:52:51 +0200294 if (!IS_ENABLED(CONFIG_NET))
295 return 0;
296
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200297 /* MAC already in environment */
298 if (eth_env_get_enetaddr("ethaddr", enetaddr))
299 return 0;
300
301 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65e25be2020-12-28 20:34:56 -0700302 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200303 &dev);
304 if (ret)
305 return ret;
306
Patrick Delaunay17f1f9b2019-02-27 17:01:29 +0100307 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_MAC),
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200308 otp, sizeof(otp));
Simon Glass8729b1a2018-11-06 15:21:39 -0700309 if (ret < 0)
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200310 return ret;
311
312 for (i = 0; i < 6; i++)
313 enetaddr[i] = ((uint8_t *)&otp)[i];
314
315 if (!is_valid_ethaddr(enetaddr)) {
Patrick Delaunayeb653ac2020-11-06 19:01:29 +0100316 log_err("invalid MAC address in OTP %pM\n", enetaddr);
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200317 return -EINVAL;
318 }
Patrick Delaunayeb653ac2020-11-06 19:01:29 +0100319 log_debug("OTP MAC address = %pM\n", enetaddr);
Patrick Delaunaycf8df342020-04-07 16:07:46 +0200320 ret = eth_env_set_enetaddr("ethaddr", enetaddr);
321 if (ret)
Patrick Delaunayeb653ac2020-11-06 19:01:29 +0100322 log_err("Failed to set mac address %pM from OTP: %d\n", enetaddr, ret);
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200323
324 return 0;
325}
326
327static int setup_serial_number(void)
328{
329 char serial_string[25];
330 u32 otp[3] = {0, 0, 0 };
331 struct udevice *dev;
332 int ret;
333
334 if (env_get("serial#"))
335 return 0;
336
337 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65e25be2020-12-28 20:34:56 -0700338 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200339 &dev);
340 if (ret)
341 return ret;
342
Patrick Delaunay17f1f9b2019-02-27 17:01:29 +0100343 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_SERIAL),
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200344 otp, sizeof(otp));
Simon Glass8729b1a2018-11-06 15:21:39 -0700345 if (ret < 0)
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200346 return ret;
347
Patrick Delaunay8983ba22019-02-27 17:01:25 +0100348 sprintf(serial_string, "%08X%08X%08X", otp[0], otp[1], otp[2]);
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200349 env_set("serial#", serial_string);
350
351 return 0;
352}
353
Patrick Delaunay6df271a2022-05-20 18:24:42 +0200354__weak void stm32mp_misc_init(void)
Marek Vasut2c2d7d62021-03-31 14:15:09 +0200355{
Marek Vasut2c2d7d62021-03-31 14:15:09 +0200356}
357
Patrick Delaunay08772f62018-03-20 10:54:53 +0100358int arch_misc_init(void)
359{
360 setup_boot_mode();
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200361 setup_mac_address();
362 setup_serial_number();
Patrick Delaunay6df271a2022-05-20 18:24:42 +0200363 stm32mp_misc_init();
Patrick Delaunay08772f62018-03-20 10:54:53 +0100364
365 return 0;
366}