blob: 165bca6885efe3208039c6673d03cdd7b414ea8d [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
83#if defined(CONFIG_CPU_V7R)
84
85/*
86 * RTC Erratum i2327 Workaround for Silicon Revision 1
87 *
88 * Due to a bug in initial synchronization out of cold power on,
89 * IRQ status can get locked infinitely if we do not unlock RTC
90 *
91 * This workaround *must* be applied within 1 second of power on,
92 * So, this is closest point to be able to guarantee the max
93 * timing.
94 *
95 * https://www.ti.com/lit/er/sprz487c/sprz487c.pdf
96 */
97void rtc_erratumi2327_init(void)
98{
99 u32 counter;
100
101 /*
102 * If counter has gone past 1, nothing we can do, leave
103 * system locked! This is the only way we know if RTC
104 * can be used for all practical purposes.
105 */
106 counter = readl(REG_K3RTC_S_CNT_LSW);
107 if (counter > 1)
108 return;
109 /*
110 * Need to set this up at the very start
111 * MUST BE DONE under 1 second of boot.
112 */
113 writel(K3RTC_KICK0_UNLOCK_VALUE, REG_K3RTC_KICK0);
114 writel(K3RTC_KICK1_UNLOCK_VALUE, REG_K3RTC_KICK1);
115 return;
116}
117#endif
Julien Panis01b00d42022-07-01 14:30:11 +0200118
Suman Anna27fa4122022-05-25 13:38:42 +0530119void board_init_f(ulong dummy)
120{
121 struct udevice *dev;
122 int ret;
123
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500124 if (IS_ENABLED(CONFIG_CPU_V7R)) {
125 setup_k3_mpu_regions();
126 rtc_erratumi2327_init();
127 }
Suman Anna27fa4122022-05-25 13:38:42 +0530128
129 /*
130 * Cannot delay this further as there is a chance that
131 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
132 */
133 store_boot_info_from_rom();
134
135 ctrl_mmr_unlock();
136
137 /* Init DM early */
138 spl_early_init();
139
140 /*
141 * Process pinctrl for the serial0 and serial3, aka WKUP_UART0 and
142 * MAIN_UART1 modules and continue regardless of the result of pinctrl.
143 * Do this without probing the device, but instead by searching the
144 * device that would request the given sequence number if probed. The
145 * UARTs will be used by the DM firmware and TIFS firmware images
146 * respectively and the firmware depend on SPL to initialize the pin
147 * settings.
148 */
149 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 0, &dev);
150 if (!ret)
151 pinctrl_select_state(dev, "default");
152
153 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev);
154 if (!ret)
155 pinctrl_select_state(dev, "default");
156
157 preloader_console_init();
158
Suman Anna27fa4122022-05-25 13:38:42 +0530159 /*
160 * Allow establishing an early console as required for example when
161 * doing a UART-based boot. Note that this console may not "survive"
162 * through a SYSFW PM-init step and will need a re-init in some way
163 * due to changing module clock frequencies.
164 */
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500165 if (IS_ENABLED(CONFIG_K3_EARLY_CONS))
166 early_console_init();
Suman Anna27fa4122022-05-25 13:38:42 +0530167
Suman Anna27fa4122022-05-25 13:38:42 +0530168 /*
169 * Configure and start up system controller firmware. Provide
170 * the U-Boot console init function to the SYSFW post-PM configuration
171 * callback hook, effectively switching on (or over) the console
172 * output.
173 */
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500174 if (IS_ENABLED(CONFIG_K3_LOAD_SYSFW)) {
175 ret = is_rom_loaded_sysfw(&bootdata);
176 if (!ret)
177 panic("ROM has not loaded TIFS firmware\n");
Suman Anna27fa4122022-05-25 13:38:42 +0530178
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500179 k3_sysfw_loader(true, NULL, NULL);
180 }
Suman Anna27fa4122022-05-25 13:38:42 +0530181
182 /*
183 * Force probe of clk_k3 driver here to ensure basic default clock
184 * configuration is always done.
185 */
186 if (IS_ENABLED(CONFIG_SPL_CLK_K3)) {
187 ret = uclass_get_device_by_driver(UCLASS_CLK,
188 DM_DRIVER_GET(ti_clk),
189 &dev);
190 if (ret)
191 printf("Failed to initialize clk-k3!\n");
192 }
193
194 /* Output System Firmware version info */
195 k3_sysfw_print_ver();
196
Julien Panis01b00d42022-07-01 14:30:11 +0200197 if (IS_ENABLED(CONFIG_ESM_K3)) {
198 /* Probe/configure ESM0 */
199 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@420000", &dev);
200 if (ret)
201 printf("esm main init failed: %d\n", ret);
202
203 /* Probe/configure MCUESM */
204 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@4100000", &dev);
205 if (ret)
206 printf("esm mcu init failed: %d\n", ret);
207
208 enable_mcu_esm_reset();
209 }
210
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500211 if (IS_ENABLED(CONFIG_K3_AM64_DDRSS)) {
212 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
213 if (ret)
214 panic("DRAM init failed: %d\n", ret);
215 }
Nikhil M Jaindafda902023-07-18 14:27:29 +0530216 spl_enable_dcache();
Suman Anna27fa4122022-05-25 13:38:42 +0530217}
218
219u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
220{
221 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
Martyn Welch5713c622022-12-20 18:38:18 +0000222 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
223 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
Suman Anna27fa4122022-05-25 13:38:42 +0530224 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
225 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
226
Suman Anna27fa4122022-05-25 13:38:42 +0530227
Martyn Welch5713c622022-12-20 18:38:18 +0000228 switch (bootmode) {
229 case BOOT_DEVICE_EMMC:
230 return MMCSD_MODE_EMMCBOOT;
231 case BOOT_DEVICE_MMC:
232 if (bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_FS_RAW_MASK)
233 return MMCSD_MODE_RAW;
Suman Anna27fa4122022-05-25 13:38:42 +0530234 default:
Martyn Welch5713c622022-12-20 18:38:18 +0000235 return MMCSD_MODE_FS;
Suman Anna27fa4122022-05-25 13:38:42 +0530236 }
237}
238
239static u32 __get_backup_bootmedia(u32 devstat)
240{
241 u32 bkup_bootmode = (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
242 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
243 u32 bkup_bootmode_cfg =
244 (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
245 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
246
247 switch (bkup_bootmode) {
248 case BACKUP_BOOT_DEVICE_UART:
249 return BOOT_DEVICE_UART;
250
251 case BACKUP_BOOT_DEVICE_USB:
252 return BOOT_DEVICE_USB;
253
254 case BACKUP_BOOT_DEVICE_ETHERNET:
255 return BOOT_DEVICE_ETHERNET;
256
257 case BACKUP_BOOT_DEVICE_MMC:
258 if (bkup_bootmode_cfg)
259 return BOOT_DEVICE_MMC2;
260 return BOOT_DEVICE_MMC1;
261
262 case BACKUP_BOOT_DEVICE_SPI:
263 return BOOT_DEVICE_SPI;
264
265 case BACKUP_BOOT_DEVICE_I2C:
266 return BOOT_DEVICE_I2C;
267
268 case BACKUP_BOOT_DEVICE_DFU:
269 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
270 return BOOT_DEVICE_USB;
271 return BOOT_DEVICE_DFU;
272 };
273
274 return BOOT_DEVICE_RAM;
275}
276
277static u32 __get_primary_bootmedia(u32 devstat)
278{
279 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
280 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
281 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
282 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
283
284 switch (bootmode) {
285 case BOOT_DEVICE_OSPI:
286 fallthrough;
287 case BOOT_DEVICE_QSPI:
288 fallthrough;
289 case BOOT_DEVICE_XSPI:
290 fallthrough;
291 case BOOT_DEVICE_SPI:
292 return BOOT_DEVICE_SPI;
293
294 case BOOT_DEVICE_ETHERNET_RGMII:
295 fallthrough;
296 case BOOT_DEVICE_ETHERNET_RMII:
297 return BOOT_DEVICE_ETHERNET;
298
299 case BOOT_DEVICE_EMMC:
300 return BOOT_DEVICE_MMC1;
301
302 case BOOT_DEVICE_MMC:
303 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
304 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
305 return BOOT_DEVICE_MMC2;
306 return BOOT_DEVICE_MMC1;
307
308 case BOOT_DEVICE_DFU:
309 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
310 MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
311 return BOOT_DEVICE_USB;
312 return BOOT_DEVICE_DFU;
313
314 case BOOT_DEVICE_NOBOOT:
315 return BOOT_DEVICE_RAM;
316 }
317
318 return bootmode;
319}
320
321u32 spl_boot_device(void)
322{
323 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
324 u32 bootmedia;
325
326 if (bootindex == K3_PRIMARY_BOOTMODE)
327 bootmedia = __get_primary_bootmedia(devstat);
328 else
329 bootmedia = __get_backup_bootmedia(devstat);
330
331 debug("am625_init: %s: devstat = 0x%x bootmedia = 0x%x bootindex = %d\n",
332 __func__, devstat, bootmedia, bootindex);
333
334 return bootmedia;
335}