blob: 0e5d44269ebfb69736bae24bcff04bb771262621 [file] [log] [blame]
Suman Annad98e8602022-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 Davisf5e49442023-04-06 11:38:16 -050012#include "sysfw-loader.h"
Suman Annad98e8602022-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 Menonfb3474b2023-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 Annad98e8602022-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 Brattlof4c710fa2022-11-22 13:28:11 -060038 memcpy(&bootdata, (uintptr_t *)ROM_EXTENDED_BOOT_DATA_INFO,
Suman Annad98e8602022-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 Panis16958202022-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}
82
Nishanth Menonfb3474b2023-05-16 18:06:21 -050083#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
118
Suman Annad98e8602022-05-25 13:38:42 +0530119void board_init_f(ulong dummy)
120{
121 struct udevice *dev;
122 int ret;
123
124#if defined(CONFIG_CPU_V7R)
125 setup_k3_mpu_regions();
Nishanth Menonfb3474b2023-05-16 18:06:21 -0500126 rtc_erratumi2327_init();
Suman Annad98e8602022-05-25 13:38:42 +0530127#endif
128
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
159#ifdef CONFIG_K3_EARLY_CONS
160 /*
161 * Allow establishing an early console as required for example when
162 * doing a UART-based boot. Note that this console may not "survive"
163 * through a SYSFW PM-init step and will need a re-init in some way
164 * due to changing module clock frequencies.
165 */
166 early_console_init();
167#endif
168
169#if defined(CONFIG_K3_LOAD_SYSFW)
170 /*
171 * Configure and start up system controller firmware. Provide
172 * the U-Boot console init function to the SYSFW post-PM configuration
173 * callback hook, effectively switching on (or over) the console
174 * output.
175 */
176 ret = is_rom_loaded_sysfw(&bootdata);
177 if (!ret)
178 panic("ROM has not loaded TIFS firmware\n");
179
180 k3_sysfw_loader(true, NULL, NULL);
181#endif
182
183 /*
184 * Force probe of clk_k3 driver here to ensure basic default clock
185 * configuration is always done.
186 */
187 if (IS_ENABLED(CONFIG_SPL_CLK_K3)) {
188 ret = uclass_get_device_by_driver(UCLASS_CLK,
189 DM_DRIVER_GET(ti_clk),
190 &dev);
191 if (ret)
192 printf("Failed to initialize clk-k3!\n");
193 }
194
195 /* Output System Firmware version info */
196 k3_sysfw_print_ver();
197
Julien Panis16958202022-07-01 14:30:11 +0200198 if (IS_ENABLED(CONFIG_ESM_K3)) {
199 /* Probe/configure ESM0 */
200 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@420000", &dev);
201 if (ret)
202 printf("esm main init failed: %d\n", ret);
203
204 /* Probe/configure MCUESM */
205 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@4100000", &dev);
206 if (ret)
207 printf("esm mcu init failed: %d\n", ret);
208
209 enable_mcu_esm_reset();
210 }
211
Suman Annad98e8602022-05-25 13:38:42 +0530212#if defined(CONFIG_K3_AM64_DDRSS)
213 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
214 if (ret)
215 panic("DRAM init failed: %d\n", ret);
216#endif
Nikhil M Jain12fdace2023-07-18 14:27:29 +0530217 spl_enable_dcache();
Suman Annad98e8602022-05-25 13:38:42 +0530218}
219
220u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
221{
222 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
Martyn Welch7c34b712022-12-20 18:38:18 +0000223 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
224 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
Suman Annad98e8602022-05-25 13:38:42 +0530225 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
226 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
227
Suman Annad98e8602022-05-25 13:38:42 +0530228
Martyn Welch7c34b712022-12-20 18:38:18 +0000229 switch (bootmode) {
230 case BOOT_DEVICE_EMMC:
231 return MMCSD_MODE_EMMCBOOT;
232 case BOOT_DEVICE_MMC:
233 if (bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_FS_RAW_MASK)
234 return MMCSD_MODE_RAW;
Suman Annad98e8602022-05-25 13:38:42 +0530235 default:
Martyn Welch7c34b712022-12-20 18:38:18 +0000236 return MMCSD_MODE_FS;
Suman Annad98e8602022-05-25 13:38:42 +0530237 }
238}
239
240static u32 __get_backup_bootmedia(u32 devstat)
241{
242 u32 bkup_bootmode = (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
243 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
244 u32 bkup_bootmode_cfg =
245 (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
246 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
247
248 switch (bkup_bootmode) {
249 case BACKUP_BOOT_DEVICE_UART:
250 return BOOT_DEVICE_UART;
251
252 case BACKUP_BOOT_DEVICE_USB:
253 return BOOT_DEVICE_USB;
254
255 case BACKUP_BOOT_DEVICE_ETHERNET:
256 return BOOT_DEVICE_ETHERNET;
257
258 case BACKUP_BOOT_DEVICE_MMC:
259 if (bkup_bootmode_cfg)
260 return BOOT_DEVICE_MMC2;
261 return BOOT_DEVICE_MMC1;
262
263 case BACKUP_BOOT_DEVICE_SPI:
264 return BOOT_DEVICE_SPI;
265
266 case BACKUP_BOOT_DEVICE_I2C:
267 return BOOT_DEVICE_I2C;
268
269 case BACKUP_BOOT_DEVICE_DFU:
270 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
271 return BOOT_DEVICE_USB;
272 return BOOT_DEVICE_DFU;
273 };
274
275 return BOOT_DEVICE_RAM;
276}
277
278static u32 __get_primary_bootmedia(u32 devstat)
279{
280 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
281 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
282 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
283 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
284
285 switch (bootmode) {
286 case BOOT_DEVICE_OSPI:
287 fallthrough;
288 case BOOT_DEVICE_QSPI:
289 fallthrough;
290 case BOOT_DEVICE_XSPI:
291 fallthrough;
292 case BOOT_DEVICE_SPI:
293 return BOOT_DEVICE_SPI;
294
295 case BOOT_DEVICE_ETHERNET_RGMII:
296 fallthrough;
297 case BOOT_DEVICE_ETHERNET_RMII:
298 return BOOT_DEVICE_ETHERNET;
299
300 case BOOT_DEVICE_EMMC:
301 return BOOT_DEVICE_MMC1;
302
303 case BOOT_DEVICE_MMC:
304 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
305 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
306 return BOOT_DEVICE_MMC2;
307 return BOOT_DEVICE_MMC1;
308
309 case BOOT_DEVICE_DFU:
310 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
311 MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
312 return BOOT_DEVICE_USB;
313 return BOOT_DEVICE_DFU;
314
315 case BOOT_DEVICE_NOBOOT:
316 return BOOT_DEVICE_RAM;
317 }
318
319 return bootmode;
320}
321
322u32 spl_boot_device(void)
323{
324 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
325 u32 bootmedia;
326
327 if (bootindex == K3_PRIMARY_BOOTMODE)
328 bootmedia = __get_primary_bootmedia(devstat);
329 else
330 bootmedia = __get_backup_bootmedia(devstat);
331
332 debug("am625_init: %s: devstat = 0x%x bootmedia = 0x%x bootindex = %d\n",
333 __func__, devstat, bootmedia, bootindex);
334
335 return bootmedia;
336}