blob: c871e92330bc2d9c5ab9b9439f1c549406766ad3 [file] [log] [blame]
Dave Gerlach96571ec2021-04-23 11:27:32 -05001// SPDX-License-Identifier: GPL-2.0
2/*
3 * AM642: SoC specific initialization
4 *
5 * Copyright (C) 2020-2021 Texas Instruments Incorporated - https://www.ti.com/
6 * Keerthy <j-keerthy@ti.com>
7 * Dave Gerlach <d-gerlach@ti.com>
8 */
9
10#include <common.h>
Aswath Govindraju70bc5e32021-06-04 22:00:33 +053011#include <fdt_support.h>
Dave Gerlach96571ec2021-04-23 11:27:32 -050012#include <spl.h>
13#include <asm/io.h>
Keerthy05d670e2021-04-23 11:27:33 -050014#include <asm/arch/hardware.h>
Andrew Davisf1799852023-04-06 11:38:16 -050015#include "sysfw-loader.h"
Dave Gerlach96571ec2021-04-23 11:27:32 -050016#include "common.h"
Dave Gerlach8e0689b2021-04-23 11:27:36 -050017#include <linux/soc/ti/ti_sci_protocol.h>
18#include <dm.h>
19#include <dm/uclass-internal.h>
20#include <dm/pinctrl.h>
Dave Gerlacha89f7a42021-04-23 11:27:37 -050021#include <mmc.h>
Lokesh Vutla17ad6e02021-05-06 16:44:52 +053022#include <dm/root.h>
Nitin Yadav2eda10a2023-04-06 13:29:36 +053023#include <command.h>
Dave Gerlach96571ec2021-04-23 11:27:32 -050024
Hari Nagallad383c5e2022-03-09 14:42:30 -060025#define CTRLMMR_MCU_RST_CTRL 0x04518170
Dave Gerlach96571ec2021-04-23 11:27:32 -050026
Nitin Yadav2eda10a2023-04-06 13:29:36 +053027#define CTRLMMR_MCU_RST_SRC (MCU_CTRL_MMR0_BASE + 0x18178)
28#define COLD_BOOT 0
29#define SW_POR_MCU BIT(24)
30#define SW_POR_MAIN BIT(25)
31
Dave Gerlacheaef1292021-04-23 11:27:34 -050032static void ctrl_mmr_unlock(void)
33{
34 /* Unlock all PADCFG_MMR1 module registers */
35 mmr_unlock(PADCFG_MMR1_BASE, 1);
36
Hari Nagallad383c5e2022-03-09 14:42:30 -060037 /* Unlock all MCU_CTRL_MMR0 module registers */
38 mmr_unlock(MCU_CTRL_MMR0_BASE, 0);
39 mmr_unlock(MCU_CTRL_MMR0_BASE, 1);
40 mmr_unlock(MCU_CTRL_MMR0_BASE, 2);
41 mmr_unlock(MCU_CTRL_MMR0_BASE, 3);
42 mmr_unlock(MCU_CTRL_MMR0_BASE, 4);
43 mmr_unlock(MCU_CTRL_MMR0_BASE, 6);
44
Dave Gerlacheaef1292021-04-23 11:27:34 -050045 /* Unlock all CTRL_MMR0 module registers */
46 mmr_unlock(CTRL_MMR0_BASE, 0);
47 mmr_unlock(CTRL_MMR0_BASE, 1);
48 mmr_unlock(CTRL_MMR0_BASE, 2);
49 mmr_unlock(CTRL_MMR0_BASE, 3);
50 mmr_unlock(CTRL_MMR0_BASE, 5);
51 mmr_unlock(CTRL_MMR0_BASE, 6);
Christian Gmeiner33a9d8e2022-05-12 08:21:01 +020052
53 /* Unlock all MCU_PADCFG_MMR1 module registers */
54 mmr_unlock(MCU_PADCFG_MMR1_BASE, 1);
Dave Gerlacheaef1292021-04-23 11:27:34 -050055}
56
Dave Gerlachb27a9f22021-04-23 11:27:35 -050057/*
58 * This uninitialized global variable would normal end up in the .bss section,
59 * but the .bss is cleared between writing and reading this variable, so move
60 * it to the .data section.
61 */
62u32 bootindex __section(".data");
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020063static struct rom_extended_boot_data bootdata __section(".data");
Dave Gerlachb27a9f22021-04-23 11:27:35 -050064
65static void store_boot_info_from_rom(void)
66{
67 bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
Bryan Brattlof270537c2022-11-22 13:28:11 -060068 memcpy(&bootdata, (uintptr_t *)ROM_EXTENDED_BOOT_DATA_INFO,
Dave Gerlachb27a9f22021-04-23 11:27:35 -050069 sizeof(struct rom_extended_boot_data));
70}
71
Dave Gerlacha89f7a42021-04-23 11:27:37 -050072#if defined(CONFIG_K3_LOAD_SYSFW) && CONFIG_IS_ENABLED(DM_MMC)
73void k3_mmc_stop_clock(void)
74{
75 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
76 struct mmc *mmc = find_mmc_device(0);
77
78 if (!mmc)
79 return;
80
81 mmc->saved_clock = mmc->clock;
82 mmc_set_clock(mmc, 0, true);
83 }
84}
85
86void k3_mmc_restart_clock(void)
87{
88 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
89 struct mmc *mmc = find_mmc_device(0);
90
91 if (!mmc)
92 return;
93
94 mmc_set_clock(mmc, mmc->saved_clock, false);
95 }
96}
97#else
98void k3_mmc_stop_clock(void) {}
99void k3_mmc_restart_clock(void) {}
100#endif
101
Lokesh Vutla17ad6e02021-05-06 16:44:52 +0530102#ifdef CONFIG_SPL_OF_LIST
103void do_dt_magic(void)
104{
105 int ret, rescan;
106
Andrew Davis2dde9a72023-04-06 11:38:17 -0500107 /* Perform board detection */
108 do_board_detect();
Lokesh Vutla17ad6e02021-05-06 16:44:52 +0530109
110 /*
111 * Board detection has been done.
112 * Let us see if another dtb wouldn't be a better match
113 * for our board
114 */
115 if (IS_ENABLED(CONFIG_CPU_V7R)) {
116 ret = fdtdec_resetup(&rescan);
117 if (!ret && rescan) {
118 dm_uninit();
119 dm_init_and_scan(true);
120 }
121 }
122}
123#endif
124
Aswath Govindraju70bc5e32021-06-04 22:00:33 +0530125#if CONFIG_IS_ENABLED(USB_STORAGE)
126static int fixup_usb_boot(const void *fdt_blob)
127{
128 int ret = 0;
129
130 switch (spl_boot_device()) {
131 case BOOT_DEVICE_USB:
132 /*
133 * If the boot mode is host, fixup the dr_mode to host
134 * before cdns3 bind takes place
135 */
136 ret = fdt_find_and_setprop((void *)fdt_blob,
137 "/bus@f4000/cdns-usb@f900000/usb@f400000",
138 "dr_mode", "host", 5, 0);
139 if (ret)
140 printf("%s: fdt_find_and_setprop() failed:%d\n",
141 __func__, ret);
142 fallthrough;
143 default:
144 break;
145 }
146
147 return ret;
148}
149
150int fdtdec_board_setup(const void *fdt_blob)
151{
152 /* Can use the pointer from the function parameters */
153 return fixup_usb_boot(fdt_blob);
154}
155#endif
156
Hari Nagallad383c5e2022-03-09 14:42:30 -0600157#if defined(CONFIG_ESM_K3)
158static void enable_mcu_esm_reset(void)
159{
160 /* Set CTRLMMR_MCU_RST_CTRL:MCU_ESM_ERROR_RST_EN_Z to '0' (low active) */
161 u32 stat = readl(CTRLMMR_MCU_RST_CTRL);
162
163 stat &= 0xFFFDFFFF;
164 writel(stat, CTRLMMR_MCU_RST_CTRL);
165}
166#endif
167
Dave Gerlach96571ec2021-04-23 11:27:32 -0500168void board_init_f(ulong dummy)
169{
Hari Nagallad383c5e2022-03-09 14:42:30 -0600170#if defined(CONFIG_K3_LOAD_SYSFW) || defined(CONFIG_K3_AM64_DDRSS) || defined(CONFIG_ESM_K3)
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500171 struct udevice *dev;
172 int ret;
Nitin Yadav2eda10a2023-04-06 13:29:36 +0530173 int rst_src;
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500174#endif
175
Dave Gerlach96571ec2021-04-23 11:27:32 -0500176#if defined(CONFIG_CPU_V7R)
177 setup_k3_mpu_regions();
178#endif
179
Dave Gerlachb27a9f22021-04-23 11:27:35 -0500180 /*
181 * Cannot delay this further as there is a chance that
182 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
183 */
184 store_boot_info_from_rom();
185
Dave Gerlacheaef1292021-04-23 11:27:34 -0500186 ctrl_mmr_unlock();
187
Dave Gerlach96571ec2021-04-23 11:27:32 -0500188 /* Init DM early */
189 spl_early_init();
190
191 preloader_console_init();
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500192
193#if defined(CONFIG_K3_LOAD_SYSFW)
194 /*
195 * Process pinctrl for serial3 a.k.a. MAIN UART1 module and continue
196 * regardless of the result of pinctrl. Do this without probing the
197 * device, but instead by searching the device that would request the
198 * given sequence number if probed. The UART will be used by the system
199 * firmware (SYSFW) image for various purposes and SYSFW depends on us
200 * to initialize its pin settings.
201 */
202 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev);
203 if (!ret)
204 pinctrl_select_state(dev, "default");
205
206 /*
207 * Load, start up, and configure system controller firmware.
208 * This will determine whether or not ROM has already loaded
209 * system firmware and if so, will only perform needed config
210 * and not attempt to load firmware again.
211 */
Dave Gerlacha89f7a42021-04-23 11:27:37 -0500212 k3_sysfw_loader(is_rom_loaded_sysfw(&bootdata), k3_mmc_stop_clock,
213 k3_mmc_restart_clock);
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500214#endif
215
Nitin Yadav2eda10a2023-04-06 13:29:36 +0530216#if defined(CONFIG_CPU_V7R)
217 /*
218 * Errata ID i2331 CPSW: A device lockup can occur during the second
219 * read of any CPSW subsystem register after any MAIN domain power on
220 * reset (POR). A MAIN domain POR occurs using the hardware MCU_PORz
221 * signal, or via software using CTRLMMR_RST_CTRL.SW_MAIN_POR or
222 * CTRLMMR_MCU_RST_CTRL.SW_MAIN_POR. After these resets, the processor
223 * and internal bus structures may get into a state which is only
224 * recoverable with full device reset using MCU_PORz.
225 * Workaround(s): To avoid the lockup, a warm reset should be issued
226 * after a MAIN domain POR and before any access to the CPSW registers.
227 * The warm reset realigns internal clocks and prevents the lockup from
228 * happening.
229 */
230 ret = uclass_first_device_err(UCLASS_SYSRESET, &dev);
231 if (ret)
232 printf("\n%s:uclass device error [%d]\n",__func__,ret);
233
234 rst_src = readl(CTRLMMR_MCU_RST_SRC);
235 if (rst_src == COLD_BOOT || rst_src & (SW_POR_MCU | SW_POR_MAIN)) {
236 printf("Resetting on cold boot to workaround ErrataID:i2331\n");
237 printf("Please resend tiboot3.bin in case of UART/DFU boot\n");
238 do_reset(NULL, 0, 0, NULL);
239 }
240#endif
241
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500242 /* Output System Firmware version info */
243 k3_sysfw_print_ver();
Dave Gerlachad38f512021-05-04 18:00:53 -0500244
Christian Gmeinerfe8b5452023-03-28 16:13:14 +0200245 do_dt_magic();
246
Hari Nagallad383c5e2022-03-09 14:42:30 -0600247#if defined(CONFIG_ESM_K3)
248 /* Probe/configure ESM0 */
249 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@420000", &dev);
250 if (ret)
251 printf("esm main init failed: %d\n", ret);
252
253 /* Probe/configure MCUESM */
254 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@4100000", &dev);
255 if (ret)
256 printf("esm mcu init failed: %d\n", ret);
257
258 enable_mcu_esm_reset();
259#endif
260
Dave Gerlachad38f512021-05-04 18:00:53 -0500261#if defined(CONFIG_K3_AM64_DDRSS)
262 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
263 if (ret)
264 panic("DRAM init failed: %d\n", ret);
265#endif
Vignesh Raghavendra82cc43e2021-12-24 12:55:32 +0530266 if (IS_ENABLED(CONFIG_SPL_ETH) && IS_ENABLED(CONFIG_TI_AM65_CPSW_NUSS) &&
267 spl_boot_device() == BOOT_DEVICE_ETHERNET) {
268 struct udevice *cpswdev;
269
270 if (uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(am65_cpsw_nuss), &cpswdev))
271 printf("Failed to probe am65_cpsw_nuss driver\n");
272 }
Dave Gerlach96571ec2021-04-23 11:27:32 -0500273}
Keerthy05d670e2021-04-23 11:27:33 -0500274
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100275u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Keerthy05d670e2021-04-23 11:27:33 -0500276{
277 switch (boot_device) {
278 case BOOT_DEVICE_MMC1:
279 return MMCSD_MODE_EMMCBOOT;
280
281 case BOOT_DEVICE_MMC2:
282 return MMCSD_MODE_FS;
283
284 default:
285 return MMCSD_MODE_RAW;
286 }
287}
288
289static u32 __get_backup_bootmedia(u32 main_devstat)
290{
291 u32 bkup_bootmode =
292 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
293 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
294 u32 bkup_bootmode_cfg =
295 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
296 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
297
298 switch (bkup_bootmode) {
299 case BACKUP_BOOT_DEVICE_UART:
300 return BOOT_DEVICE_UART;
301
Aswath Govindraju8a05c9a2021-06-04 22:00:32 +0530302 case BACKUP_BOOT_DEVICE_DFU:
303 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
304 return BOOT_DEVICE_USB;
305 return BOOT_DEVICE_DFU;
306
Keerthy05d670e2021-04-23 11:27:33 -0500307
308 case BACKUP_BOOT_DEVICE_ETHERNET:
309 return BOOT_DEVICE_ETHERNET;
310
311 case BACKUP_BOOT_DEVICE_MMC:
312 if (bkup_bootmode_cfg)
313 return BOOT_DEVICE_MMC2;
314 return BOOT_DEVICE_MMC1;
315
316 case BACKUP_BOOT_DEVICE_SPI:
317 return BOOT_DEVICE_SPI;
318
319 case BACKUP_BOOT_DEVICE_I2C:
320 return BOOT_DEVICE_I2C;
321 };
322
323 return BOOT_DEVICE_RAM;
324}
325
326static u32 __get_primary_bootmedia(u32 main_devstat)
327{
328 u32 bootmode = (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
329 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
330 u32 bootmode_cfg =
331 (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
332 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
333
334 switch (bootmode) {
335 case BOOT_DEVICE_OSPI:
336 fallthrough;
337 case BOOT_DEVICE_QSPI:
338 fallthrough;
339 case BOOT_DEVICE_XSPI:
340 fallthrough;
341 case BOOT_DEVICE_SPI:
342 return BOOT_DEVICE_SPI;
343
344 case BOOT_DEVICE_ETHERNET_RGMII:
345 fallthrough;
346 case BOOT_DEVICE_ETHERNET_RMII:
347 return BOOT_DEVICE_ETHERNET;
348
349 case BOOT_DEVICE_EMMC:
350 return BOOT_DEVICE_MMC1;
351
352 case BOOT_DEVICE_MMC:
353 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
354 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
355 return BOOT_DEVICE_MMC2;
356 return BOOT_DEVICE_MMC1;
357
Aswath Govindraju8a05c9a2021-06-04 22:00:32 +0530358 case BOOT_DEVICE_DFU:
359 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
360 MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
361 return BOOT_DEVICE_USB;
362 return BOOT_DEVICE_DFU;
363
Keerthy05d670e2021-04-23 11:27:33 -0500364 case BOOT_DEVICE_NOBOOT:
365 return BOOT_DEVICE_RAM;
366 }
367
368 return bootmode;
369}
370
371u32 spl_boot_device(void)
372{
373 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
374
375 if (bootindex == K3_PRIMARY_BOOTMODE)
376 return __get_primary_bootmedia(devstat);
377 else
378 return __get_backup_bootmedia(devstat);
379}