blob: 8fa36f7b913ea1cd3b395087367f2ed74282c2a3 [file] [log] [blame]
Suman Anna27fa4122022-05-25 13:38:42 +05301// SPDX-License-Identifier: GPL-2.0
2/*
3 * AM625: SoC specific initialization
4 *
5 * Copyright (C) 2020-2022 Texas Instruments Incorporated - https://www.ti.com/
6 * Suman Anna <s-anna@ti.com>
7 */
8
9#include <spl.h>
10#include <asm/io.h>
11#include <asm/arch/hardware.h>
Andrew Davisf1799852023-04-06 11:38:16 -050012#include "sysfw-loader.h"
Suman Anna27fa4122022-05-25 13:38:42 +053013#include "common.h"
14#include <dm.h>
15#include <dm/uclass-internal.h>
16#include <dm/pinctrl.h>
17
Nishanth Menond8af3ac2023-05-16 18:06:21 -050018#define RTC_BASE_ADDRESS 0x2b1f0000
19#define REG_K3RTC_S_CNT_LSW (RTC_BASE_ADDRESS + 0x18)
20#define REG_K3RTC_KICK0 (RTC_BASE_ADDRESS + 0x70)
21#define REG_K3RTC_KICK1 (RTC_BASE_ADDRESS + 0x74)
22
23/* Magic values for lock/unlock */
24#define K3RTC_KICK0_UNLOCK_VALUE 0x83e70b13
25#define K3RTC_KICK1_UNLOCK_VALUE 0x95a4f1e0
26
Suman Anna27fa4122022-05-25 13:38:42 +053027/*
28 * This uninitialized global variable would normal end up in the .bss section,
29 * but the .bss is cleared between writing and reading this variable, so move
30 * it to the .data section.
31 */
32u32 bootindex __section(".data");
33static struct rom_extended_boot_data bootdata __section(".data");
34
35static void store_boot_info_from_rom(void)
36{
37 bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
Bryan Brattlof270537c2022-11-22 13:28:11 -060038 memcpy(&bootdata, (uintptr_t *)ROM_EXTENDED_BOOT_DATA_INFO,
Suman Anna27fa4122022-05-25 13:38:42 +053039 sizeof(struct rom_extended_boot_data));
40}
41
42static void ctrl_mmr_unlock(void)
43{
44 /* Unlock all WKUP_CTRL_MMR0 module registers */
45 mmr_unlock(WKUP_CTRL_MMR0_BASE, 0);
46 mmr_unlock(WKUP_CTRL_MMR0_BASE, 1);
47 mmr_unlock(WKUP_CTRL_MMR0_BASE, 2);
48 mmr_unlock(WKUP_CTRL_MMR0_BASE, 3);
49 mmr_unlock(WKUP_CTRL_MMR0_BASE, 4);
50 mmr_unlock(WKUP_CTRL_MMR0_BASE, 5);
51 mmr_unlock(WKUP_CTRL_MMR0_BASE, 6);
52 mmr_unlock(WKUP_CTRL_MMR0_BASE, 7);
53
54 /* Unlock all CTRL_MMR0 module registers */
55 mmr_unlock(CTRL_MMR0_BASE, 0);
56 mmr_unlock(CTRL_MMR0_BASE, 1);
57 mmr_unlock(CTRL_MMR0_BASE, 2);
58 mmr_unlock(CTRL_MMR0_BASE, 4);
59 mmr_unlock(CTRL_MMR0_BASE, 6);
60
61 /* Unlock all MCU_CTRL_MMR0 module registers */
62 mmr_unlock(MCU_CTRL_MMR0_BASE, 0);
63 mmr_unlock(MCU_CTRL_MMR0_BASE, 1);
64 mmr_unlock(MCU_CTRL_MMR0_BASE, 2);
65 mmr_unlock(MCU_CTRL_MMR0_BASE, 3);
66 mmr_unlock(MCU_CTRL_MMR0_BASE, 4);
67 mmr_unlock(MCU_CTRL_MMR0_BASE, 6);
68
69 /* Unlock PADCFG_CTRL_MMR padconf registers */
70 mmr_unlock(PADCFG_MMR0_BASE, 1);
71 mmr_unlock(PADCFG_MMR1_BASE, 1);
72}
73
Julien Panis01b00d42022-07-01 14:30:11 +020074static __maybe_unused void enable_mcu_esm_reset(void)
75{
76 /* Set CTRLMMR_MCU_RST_CTRL:MCU_ESM_ERROR_RST_EN_Z to '0' (low active) */
77 u32 stat = readl(CTRLMMR_MCU_RST_CTRL);
78
79 stat &= RST_CTRL_ESM_ERROR_RST_EN_Z_MASK;
80 writel(stat, CTRLMMR_MCU_RST_CTRL);
81}
Nishanth Menond8af3ac2023-05-16 18:06:21 -050082
Nishanth Menond8af3ac2023-05-16 18:06:21 -050083/*
84 * RTC Erratum i2327 Workaround for Silicon Revision 1
85 *
86 * Due to a bug in initial synchronization out of cold power on,
87 * IRQ status can get locked infinitely if we do not unlock RTC
88 *
89 * This workaround *must* be applied within 1 second of power on,
90 * So, this is closest point to be able to guarantee the max
91 * timing.
92 *
93 * https://www.ti.com/lit/er/sprz487c/sprz487c.pdf
94 */
Nishanth Menond654f682023-08-25 13:02:58 -050095static __maybe_unused void rtc_erratumi2327_init(void)
Nishanth Menond8af3ac2023-05-16 18:06:21 -050096{
97 u32 counter;
98
99 /*
100 * If counter has gone past 1, nothing we can do, leave
101 * system locked! This is the only way we know if RTC
102 * can be used for all practical purposes.
103 */
104 counter = readl(REG_K3RTC_S_CNT_LSW);
105 if (counter > 1)
106 return;
107 /*
108 * Need to set this up at the very start
109 * MUST BE DONE under 1 second of boot.
110 */
111 writel(K3RTC_KICK0_UNLOCK_VALUE, REG_K3RTC_KICK0);
112 writel(K3RTC_KICK1_UNLOCK_VALUE, REG_K3RTC_KICK1);
Nishanth Menond8af3ac2023-05-16 18:06:21 -0500113}
Julien Panis01b00d42022-07-01 14:30:11 +0200114
Suman Anna27fa4122022-05-25 13:38:42 +0530115void board_init_f(ulong dummy)
116{
117 struct udevice *dev;
118 int ret;
119
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500120 if (IS_ENABLED(CONFIG_CPU_V7R)) {
121 setup_k3_mpu_regions();
122 rtc_erratumi2327_init();
123 }
Suman Anna27fa4122022-05-25 13:38:42 +0530124
125 /*
126 * Cannot delay this further as there is a chance that
127 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
128 */
129 store_boot_info_from_rom();
130
131 ctrl_mmr_unlock();
132
133 /* Init DM early */
134 spl_early_init();
135
136 /*
137 * Process pinctrl for the serial0 and serial3, aka WKUP_UART0 and
138 * MAIN_UART1 modules and continue regardless of the result of pinctrl.
139 * Do this without probing the device, but instead by searching the
140 * device that would request the given sequence number if probed. The
141 * UARTs will be used by the DM firmware and TIFS firmware images
142 * respectively and the firmware depend on SPL to initialize the pin
143 * settings.
144 */
145 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 0, &dev);
146 if (!ret)
147 pinctrl_select_state(dev, "default");
148
149 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev);
150 if (!ret)
151 pinctrl_select_state(dev, "default");
152
153 preloader_console_init();
154
Suman Anna27fa4122022-05-25 13:38:42 +0530155 /*
156 * Allow establishing an early console as required for example when
157 * doing a UART-based boot. Note that this console may not "survive"
158 * through a SYSFW PM-init step and will need a re-init in some way
159 * due to changing module clock frequencies.
160 */
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500161 if (IS_ENABLED(CONFIG_K3_EARLY_CONS))
162 early_console_init();
Suman Anna27fa4122022-05-25 13:38:42 +0530163
Suman Anna27fa4122022-05-25 13:38:42 +0530164 /*
165 * Configure and start up system controller firmware. Provide
166 * the U-Boot console init function to the SYSFW post-PM configuration
167 * callback hook, effectively switching on (or over) the console
168 * output.
169 */
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500170 if (IS_ENABLED(CONFIG_K3_LOAD_SYSFW)) {
171 ret = is_rom_loaded_sysfw(&bootdata);
172 if (!ret)
173 panic("ROM has not loaded TIFS firmware\n");
Suman Anna27fa4122022-05-25 13:38:42 +0530174
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500175 k3_sysfw_loader(true, NULL, NULL);
176 }
Suman Anna27fa4122022-05-25 13:38:42 +0530177
178 /*
179 * Force probe of clk_k3 driver here to ensure basic default clock
180 * configuration is always done.
181 */
182 if (IS_ENABLED(CONFIG_SPL_CLK_K3)) {
183 ret = uclass_get_device_by_driver(UCLASS_CLK,
184 DM_DRIVER_GET(ti_clk),
185 &dev);
186 if (ret)
187 printf("Failed to initialize clk-k3!\n");
188 }
189
190 /* Output System Firmware version info */
191 k3_sysfw_print_ver();
192
Julien Panis01b00d42022-07-01 14:30:11 +0200193 if (IS_ENABLED(CONFIG_ESM_K3)) {
194 /* Probe/configure ESM0 */
195 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@420000", &dev);
196 if (ret)
197 printf("esm main init failed: %d\n", ret);
198
199 /* Probe/configure MCUESM */
200 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@4100000", &dev);
201 if (ret)
202 printf("esm mcu init failed: %d\n", ret);
203
204 enable_mcu_esm_reset();
205 }
206
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500207 if (IS_ENABLED(CONFIG_K3_AM64_DDRSS)) {
208 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
209 if (ret)
210 panic("DRAM init failed: %d\n", ret);
211 }
Nikhil M Jaindafda902023-07-18 14:27:29 +0530212 spl_enable_dcache();
Suman Anna27fa4122022-05-25 13:38:42 +0530213}
214
215u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
216{
217 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
Martyn Welch5713c622022-12-20 18:38:18 +0000218 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
219 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
Suman Anna27fa4122022-05-25 13:38:42 +0530220 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
221 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
222
Martyn Welch5713c622022-12-20 18:38:18 +0000223 switch (bootmode) {
224 case BOOT_DEVICE_EMMC:
Nishanth Menondd343862023-08-25 13:03:00 -0500225 if (IS_ENABLED(CONFIG_SUPPORT_EMMC_BOOT)) {
226 if (spl_mmc_emmc_boot_partition(mmc))
227 return MMCSD_MODE_EMMCBOOT;
228 return MMCSD_MODE_FS;
229 }
230 if (IS_ENABLED(CONFIG_SPL_FS_FAT) || IS_ENABLED(CONFIG_SPL_FS_EXT4))
231 return MMCSD_MODE_FS;
Martyn Welch5713c622022-12-20 18:38:18 +0000232 return MMCSD_MODE_EMMCBOOT;
233 case BOOT_DEVICE_MMC:
234 if (bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_FS_RAW_MASK)
235 return MMCSD_MODE_RAW;
Suman Anna27fa4122022-05-25 13:38:42 +0530236 default:
Martyn Welch5713c622022-12-20 18:38:18 +0000237 return MMCSD_MODE_FS;
Suman Anna27fa4122022-05-25 13:38:42 +0530238 }
239}
240
241static u32 __get_backup_bootmedia(u32 devstat)
242{
243 u32 bkup_bootmode = (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
244 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
245 u32 bkup_bootmode_cfg =
246 (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
247 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
248
249 switch (bkup_bootmode) {
250 case BACKUP_BOOT_DEVICE_UART:
251 return BOOT_DEVICE_UART;
252
253 case BACKUP_BOOT_DEVICE_USB:
254 return BOOT_DEVICE_USB;
255
256 case BACKUP_BOOT_DEVICE_ETHERNET:
257 return BOOT_DEVICE_ETHERNET;
258
259 case BACKUP_BOOT_DEVICE_MMC:
260 if (bkup_bootmode_cfg)
261 return BOOT_DEVICE_MMC2;
262 return BOOT_DEVICE_MMC1;
263
264 case BACKUP_BOOT_DEVICE_SPI:
265 return BOOT_DEVICE_SPI;
266
267 case BACKUP_BOOT_DEVICE_I2C:
268 return BOOT_DEVICE_I2C;
269
270 case BACKUP_BOOT_DEVICE_DFU:
271 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
272 return BOOT_DEVICE_USB;
273 return BOOT_DEVICE_DFU;
274 };
275
276 return BOOT_DEVICE_RAM;
277}
278
279static u32 __get_primary_bootmedia(u32 devstat)
280{
281 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
282 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
283 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
284 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
285
286 switch (bootmode) {
287 case BOOT_DEVICE_OSPI:
288 fallthrough;
289 case BOOT_DEVICE_QSPI:
290 fallthrough;
291 case BOOT_DEVICE_XSPI:
292 fallthrough;
293 case BOOT_DEVICE_SPI:
294 return BOOT_DEVICE_SPI;
295
296 case BOOT_DEVICE_ETHERNET_RGMII:
297 fallthrough;
298 case BOOT_DEVICE_ETHERNET_RMII:
299 return BOOT_DEVICE_ETHERNET;
300
301 case BOOT_DEVICE_EMMC:
302 return BOOT_DEVICE_MMC1;
303
304 case BOOT_DEVICE_MMC:
305 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
306 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
307 return BOOT_DEVICE_MMC2;
308 return BOOT_DEVICE_MMC1;
309
310 case BOOT_DEVICE_DFU:
311 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
312 MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
313 return BOOT_DEVICE_USB;
314 return BOOT_DEVICE_DFU;
315
316 case BOOT_DEVICE_NOBOOT:
317 return BOOT_DEVICE_RAM;
318 }
319
320 return bootmode;
321}
322
323u32 spl_boot_device(void)
324{
325 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
326 u32 bootmedia;
327
328 if (bootindex == K3_PRIMARY_BOOTMODE)
329 bootmedia = __get_primary_bootmedia(devstat);
330 else
331 bootmedia = __get_backup_bootmedia(devstat);
332
333 debug("am625_init: %s: devstat = 0x%x bootmedia = 0x%x bootindex = %d\n",
334 __func__, devstat, bootmedia, bootindex);
335
336 return bootmedia;
337}