blob: 855fc755fe08862d46cbb6d277c958c716d31f07 [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)) {
Patrick Delaunay960debb2022-05-20 18:24:46 +020055/* STM32_SYSRAM_BASE exist only when SPL is supported */
56#ifdef CONFIG_SPL
Patrick Delaunayaad84142021-02-05 13:53:33 +010057 start = ALIGN_DOWN(STM32_SYSRAM_BASE, MMU_SECTION_SIZE);
58 size = ALIGN(STM32_SYSRAM_SIZE, MMU_SECTION_SIZE);
Patrick Delaunay960debb2022-05-20 18:24:46 +020059#endif
Patrick Delaunayaad84142021-02-05 13:53:33 +010060 } else if (gd->flags & GD_FLG_RELOC) {
61 /* bd->bi_dram is available only after relocation */
62 start = bd->bi_dram[bank].start;
63 size = bd->bi_dram[bank].size;
Patrick Delaunayade4e042021-05-07 14:50:35 +020064 use_lmb = true;
Patrick Delaunayaad84142021-02-05 13:53:33 +010065 } else {
66 /* mark cacheable and executable the beggining of the DDR */
67 start = STM32_DDR_BASE;
68 size = CONFIG_DDR_CACHEABLE_SIZE;
69 }
70
71 for (i = start >> MMU_SECTION_SHIFT;
72 i < (start >> MMU_SECTION_SHIFT) + (size >> MMU_SECTION_SHIFT);
Patrick Delaunayade4e042021-05-07 14:50:35 +020073 i++) {
74 option = DCACHE_DEFAULT_OPTION;
75 if (use_lmb && lmb_is_reserved_flags(&lmb, i << MMU_SECTION_SHIFT, LMB_NOMAP))
76 option = 0; /* INVALID ENTRY in TLB */
77 set_section_dcache(i, option);
78 }
Patrick Delaunayaad84142021-02-05 13:53:33 +010079}
80/*
Patrick Delaunay7e8471c2020-04-30 16:30:20 +020081 * initialize the MMU and activate cache in SPL or in U-Boot pre-reloc stage
82 * MMU/TLB is updated in enable_caches() for U-Boot after relocation
83 * or is deactivated in U-Boot entry function start.S::cpu_init_cp15
84 */
85static void early_enable_caches(void)
86{
87 /* I-cache is already enabled in start.S: cpu_init_cp15 */
88
89 if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
90 return;
91
Patrice Chotard23e20b22021-02-24 13:53:27 +010092 if (!(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))) {
93 gd->arch.tlb_size = PGTABLE_SIZE;
94 gd->arch.tlb_addr = (unsigned long)&early_tlb;
95 }
Patrick Delaunay7e8471c2020-04-30 16:30:20 +020096
Patrick Delaunayaad84142021-02-05 13:53:33 +010097 /* enable MMU (default configuration) */
Patrick Delaunay7e8471c2020-04-30 16:30:20 +020098 dcache_enable();
Patrick Delaunay7e8471c2020-04-30 16:30:20 +020099}
100
101/*
Patrick Delaunay08772f62018-03-20 10:54:53 +0100102 * Early system init
103 */
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100104int arch_cpu_init(void)
105{
Patrick Delaunay7e8471c2020-04-30 16:30:20 +0200106 early_enable_caches();
107
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100108 /* early armv7 timer init: needed for polling */
109 timer_init();
110
Patrick Delaunay6df271a2022-05-20 18:24:42 +0200111 return 0;
112}
113
114/* weak function for SOC specific initialization */
115__weak void stm32mp_cpu_init(void)
116{
117}
118
119int mach_cpu_init(void)
120{
121 u32 boot_mode;
122
123 stm32mp_cpu_init();
Patrick Delaunay320d2662018-05-17 14:50:46 +0200124
Patrick Delaunay320d2662018-05-17 14:50:46 +0200125 boot_mode = get_bootmode();
126
Patrick Delaunay5a05af82021-02-25 13:37:01 +0100127 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) &&
128 (boot_mode & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_UART)
Patrick Delaunay320d2662018-05-17 14:50:46 +0200129 gd->flags |= GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE;
Patrick Delaunayc8b2eef2021-10-11 09:52:51 +0200130 else if (IS_ENABLED(CONFIG_DEBUG_UART) && IS_ENABLED(CONFIG_SPL_BUILD))
Patrick Delaunay320d2662018-05-17 14:50:46 +0200131 debug_uart_init();
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100132
133 return 0;
134}
135
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +0100136void enable_caches(void)
137{
Patrick Delaunayade4e042021-05-07 14:50:35 +0200138 /* parse device tree when data cache is still activated */
139 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
140
Patrick Delaunay7e8471c2020-04-30 16:30:20 +0200141 /* I-cache is already enabled in start.S: icache_enable() not needed */
142
143 /* deactivate the data cache, early enabled in arch_cpu_init() */
144 dcache_disable();
145 /*
146 * update MMU after relocation and enable the data cache
147 * warning: the TLB location udpated in board_f.c::reserve_mmu
148 */
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +0100149 dcache_enable();
150}
151
Patrick Delaunayc8b2eef2021-10-11 09:52:51 +0200152/* used when CONFIG_DISPLAY_CPUINFO is activated */
Patrick Delaunayac5e4d82020-02-12 19:37:43 +0100153int print_cpuinfo(void)
154{
155 char name[SOC_NAME_SIZE];
156
157 get_soc_name(name);
158 printf("CPU: %s\n", name);
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100159
160 return 0;
161}
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100162
Patrick Delaunay08772f62018-03-20 10:54:53 +0100163static void setup_boot_mode(void)
164{
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100165 const u32 serial_addr[] = {
166 STM32_USART1_BASE,
167 STM32_USART2_BASE,
168 STM32_USART3_BASE,
169 STM32_UART4_BASE,
170 STM32_UART5_BASE,
171 STM32_USART6_BASE,
172 STM32_UART7_BASE,
173 STM32_UART8_BASE
174 };
Patrick Delaunay3c1057c2021-07-06 17:19:45 +0200175 const u32 sdmmc_addr[] = {
176 STM32_SDMMC1_BASE,
177 STM32_SDMMC2_BASE,
178 STM32_SDMMC3_BASE
179 };
Patrick Delaunay08772f62018-03-20 10:54:53 +0100180 char cmd[60];
181 u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
182 u32 boot_mode =
183 (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
Patrick Delaunaye609e132019-06-21 15:26:39 +0200184 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100185 u32 forced_mode = (boot_ctx & TAMP_BOOT_FORCED_MASK);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100186 struct udevice *dev;
Patrick Delaunay08772f62018-03-20 10:54:53 +0100187
Patrick Delaunayeb653ac2020-11-06 19:01:29 +0100188 log_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d forced=%x\n",
189 __func__, boot_ctx, boot_mode, instance, forced_mode);
Patrick Delaunay08772f62018-03-20 10:54:53 +0100190 switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
191 case BOOT_SERIAL_UART:
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100192 if (instance > ARRAY_SIZE(serial_addr))
193 break;
Patrick Delaunayf49eb162021-02-25 13:37:03 +0100194 /* serial : search associated node in devicetree */
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100195 sprintf(cmd, "serial@%x", serial_addr[instance]);
Patrick Delaunayf49eb162021-02-25 13:37:03 +0100196 if (uclass_get_device_by_name(UCLASS_SERIAL, cmd, &dev)) {
Patrick Delaunayb9d5e3a2021-02-25 13:37:02 +0100197 /* restore console on error */
198 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL))
199 gd->flags &= ~(GD_FLG_SILENT |
200 GD_FLG_DISABLE_CONSOLE);
Patrick Delaunaycbea7b32021-04-06 09:27:39 +0200201 log_err("uart%d = %s not found in device tree!\n",
202 instance + 1, cmd);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100203 break;
Patrick Delaunayb9d5e3a2021-02-25 13:37:02 +0100204 }
Patrick Delaunayf49eb162021-02-25 13:37:03 +0100205 sprintf(cmd, "%d", dev_seq(dev));
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100206 env_set("boot_device", "serial");
Patrick Delaunay08772f62018-03-20 10:54:53 +0100207 env_set("boot_instance", cmd);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100208
209 /* restore console on uart when not used */
Patrick Delaunay5a05af82021-02-25 13:37:01 +0100210 if (IS_ENABLED(CONFIG_CMD_STM32PROG_SERIAL) && gd->cur_serial_dev != dev) {
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100211 gd->flags &= ~(GD_FLG_SILENT |
212 GD_FLG_DISABLE_CONSOLE);
Patrick Delaunaycbea7b32021-04-06 09:27:39 +0200213 log_info("serial boot with console enabled!\n");
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100214 }
Patrick Delaunay08772f62018-03-20 10:54:53 +0100215 break;
216 case BOOT_SERIAL_USB:
217 env_set("boot_device", "usb");
218 env_set("boot_instance", "0");
219 break;
220 case BOOT_FLASH_SD:
221 case BOOT_FLASH_EMMC:
Patrick Delaunay3c1057c2021-07-06 17:19:45 +0200222 if (instance > ARRAY_SIZE(sdmmc_addr))
223 break;
224 /* search associated sdmmc node in devicetree */
225 sprintf(cmd, "mmc@%x", sdmmc_addr[instance]);
226 if (uclass_get_device_by_name(UCLASS_MMC, cmd, &dev)) {
227 printf("mmc%d = %s not found in device tree!\n",
228 instance, cmd);
229 break;
230 }
231 sprintf(cmd, "%d", dev_seq(dev));
Patrick Delaunay08772f62018-03-20 10:54:53 +0100232 env_set("boot_device", "mmc");
233 env_set("boot_instance", cmd);
234 break;
235 case BOOT_FLASH_NAND:
236 env_set("boot_device", "nand");
237 env_set("boot_instance", "0");
238 break;
Patrick Delaunayb664a742020-03-18 09:22:52 +0100239 case BOOT_FLASH_SPINAND:
240 env_set("boot_device", "spi-nand");
241 env_set("boot_instance", "0");
242 break;
Patrick Delaunay08772f62018-03-20 10:54:53 +0100243 case BOOT_FLASH_NOR:
244 env_set("boot_device", "nor");
245 env_set("boot_instance", "0");
246 break;
247 default:
Patrick Delaunay8b71b202021-07-08 10:53:56 +0200248 env_set("boot_device", "invalid");
249 env_set("boot_instance", "");
250 log_err("unexpected boot mode = %x\n", boot_mode);
Patrick Delaunay08772f62018-03-20 10:54:53 +0100251 break;
252 }
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100253
254 switch (forced_mode) {
255 case BOOT_FASTBOOT:
Patrick Delaunaycbea7b32021-04-06 09:27:39 +0200256 log_info("Enter fastboot!\n");
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100257 env_set("preboot", "env set preboot; fastboot 0");
258 break;
259 case BOOT_STM32PROG:
260 env_set("boot_device", "usb");
261 env_set("boot_instance", "0");
262 break;
263 case BOOT_UMS_MMC0:
264 case BOOT_UMS_MMC1:
265 case BOOT_UMS_MMC2:
Patrick Delaunaycbea7b32021-04-06 09:27:39 +0200266 log_info("Enter UMS!\n");
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100267 instance = forced_mode - BOOT_UMS_MMC0;
268 sprintf(cmd, "env set preboot; ums 0 mmc %d", instance);
269 env_set("preboot", cmd);
270 break;
271 case BOOT_RECOVERY:
272 env_set("preboot", "env set preboot; run altbootcmd");
273 break;
274 case BOOT_NORMAL:
275 break;
276 default:
Patrick Delaunayeb653ac2020-11-06 19:01:29 +0100277 log_debug("unexpected forced boot mode = %x\n", forced_mode);
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100278 break;
279 }
280
281 /* clear TAMP for next reboot */
282 clrsetbits_le32(TAMP_BOOT_CONTEXT, TAMP_BOOT_FORCED_MASK, BOOT_NORMAL);
Patrick Delaunay08772f62018-03-20 10:54:53 +0100283}
284
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200285/*
286 * If there is no MAC address in the environment, then it will be initialized
287 * (silently) from the value in the OTP.
288 */
Marek Vasute71b9a62019-12-18 16:52:19 +0100289__weak int setup_mac_address(void)
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200290{
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200291 int ret;
292 int i;
Patrick Delaunay46f9eb52022-05-20 18:24:47 +0200293 u32 otp[3];
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200294 uchar enetaddr[6];
295 struct udevice *dev;
Patrick Delaunay46f9eb52022-05-20 18:24:47 +0200296 int nb_eth, nb_otp, index;
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200297
Patrick Delaunayc8b2eef2021-10-11 09:52:51 +0200298 if (!IS_ENABLED(CONFIG_NET))
299 return 0;
300
Patrick Delaunay46f9eb52022-05-20 18:24:47 +0200301 nb_eth = get_eth_nb();
302
303 /* 6 bytes for each MAC addr and 4 bytes for each OTP */
304 nb_otp = DIV_ROUND_UP(6 * nb_eth, 4);
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200305
306 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65e25be2020-12-28 20:34:56 -0700307 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200308 &dev);
309 if (ret)
310 return ret;
311
Patrick Delaunay46f9eb52022-05-20 18:24:47 +0200312 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_MAC), otp, 4 * nb_otp);
Simon Glass8729b1a2018-11-06 15:21:39 -0700313 if (ret < 0)
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200314 return ret;
315
Patrick Delaunay46f9eb52022-05-20 18:24:47 +0200316 for (index = 0; index < nb_eth; index++) {
317 /* MAC already in environment */
318 if (eth_env_get_enetaddr_by_index("eth", index, enetaddr))
319 continue;
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200320
Patrick Delaunay46f9eb52022-05-20 18:24:47 +0200321 for (i = 0; i < 6; i++)
322 enetaddr[i] = ((uint8_t *)&otp)[i + 6 * index];
323
324 if (!is_valid_ethaddr(enetaddr)) {
325 log_err("invalid MAC address %d in OTP %pM\n",
326 index, enetaddr);
327 return -EINVAL;
328 }
329 log_debug("OTP MAC address %d = %pM\n", index, enetaddr);
330 ret = eth_env_set_enetaddr_by_index("eth", index, enetaddr);
331 if (ret) {
332 log_err("Failed to set mac address %pM from OTP: %d\n",
333 enetaddr, ret);
334 return ret;
335 }
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200336 }
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200337
338 return 0;
339}
340
341static int setup_serial_number(void)
342{
343 char serial_string[25];
344 u32 otp[3] = {0, 0, 0 };
345 struct udevice *dev;
346 int ret;
347
348 if (env_get("serial#"))
349 return 0;
350
351 ret = uclass_get_device_by_driver(UCLASS_MISC,
Simon Glass65e25be2020-12-28 20:34:56 -0700352 DM_DRIVER_GET(stm32mp_bsec),
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200353 &dev);
354 if (ret)
355 return ret;
356
Patrick Delaunay17f1f9b2019-02-27 17:01:29 +0100357 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_SERIAL),
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200358 otp, sizeof(otp));
Simon Glass8729b1a2018-11-06 15:21:39 -0700359 if (ret < 0)
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200360 return ret;
361
Patrick Delaunay8983ba22019-02-27 17:01:25 +0100362 sprintf(serial_string, "%08X%08X%08X", otp[0], otp[1], otp[2]);
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200363 env_set("serial#", serial_string);
364
365 return 0;
366}
367
Patrick Delaunay6df271a2022-05-20 18:24:42 +0200368__weak void stm32mp_misc_init(void)
Marek Vasut2c2d7d62021-03-31 14:15:09 +0200369{
Marek Vasut2c2d7d62021-03-31 14:15:09 +0200370}
371
Patrick Delaunay08772f62018-03-20 10:54:53 +0100372int arch_misc_init(void)
373{
374 setup_boot_mode();
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200375 setup_mac_address();
376 setup_serial_number();
Patrick Delaunay6df271a2022-05-20 18:24:42 +0200377 stm32mp_misc_init();
Patrick Delaunay08772f62018-03-20 10:54:53 +0100378
379 return 0;
380}