blob: 499cb57267b906081f3339d1635bc191d012a468 [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/*
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 Menon8b5c4cd2023-08-25 13:02:58 -050095static __maybe_unused void rtc_erratumi2327_init(void)
Nishanth Menonfb3474b2023-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 Menonfb3474b2023-05-16 18:06:21 -0500113}
Nishanth Menonfb3474b2023-05-16 18:06:21 -0500114
Suman Annad98e8602022-05-25 13:38:42 +0530115void board_init_f(ulong dummy)
116{
117 struct udevice *dev;
118 int ret;
119
Nishanth Menon6beb43e2023-08-25 13:02:57 -0500120 if (IS_ENABLED(CONFIG_CPU_V7R)) {
121 setup_k3_mpu_regions();
122 rtc_erratumi2327_init();
123 }
Suman Annad98e8602022-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 Annad98e8602022-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 Menon6beb43e2023-08-25 13:02:57 -0500161 if (IS_ENABLED(CONFIG_K3_EARLY_CONS))
162 early_console_init();
Suman Annad98e8602022-05-25 13:38:42 +0530163
Suman Annad98e8602022-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 Menon6beb43e2023-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 Annad98e8602022-05-25 13:38:42 +0530174
Nishanth Menon6beb43e2023-08-25 13:02:57 -0500175 k3_sysfw_loader(true, NULL, NULL);
176 }
Suman Annad98e8602022-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 Panis16958202022-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 Menon6beb43e2023-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 Jain12fdace2023-07-18 14:27:29 +0530212 spl_enable_dcache();
Suman Annad98e8602022-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 Welch7c34b712022-12-20 18:38:18 +0000218 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
219 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
Suman Annad98e8602022-05-25 13:38:42 +0530220 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
221 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
222
Suman Annad98e8602022-05-25 13:38:42 +0530223
Martyn Welch7c34b712022-12-20 18:38:18 +0000224 switch (bootmode) {
225 case BOOT_DEVICE_EMMC:
226 return MMCSD_MODE_EMMCBOOT;
227 case BOOT_DEVICE_MMC:
228 if (bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_FS_RAW_MASK)
229 return MMCSD_MODE_RAW;
Suman Annad98e8602022-05-25 13:38:42 +0530230 default:
Martyn Welch7c34b712022-12-20 18:38:18 +0000231 return MMCSD_MODE_FS;
Suman Annad98e8602022-05-25 13:38:42 +0530232 }
233}
234
235static u32 __get_backup_bootmedia(u32 devstat)
236{
237 u32 bkup_bootmode = (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
238 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
239 u32 bkup_bootmode_cfg =
240 (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
241 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
242
243 switch (bkup_bootmode) {
244 case BACKUP_BOOT_DEVICE_UART:
245 return BOOT_DEVICE_UART;
246
247 case BACKUP_BOOT_DEVICE_USB:
248 return BOOT_DEVICE_USB;
249
250 case BACKUP_BOOT_DEVICE_ETHERNET:
251 return BOOT_DEVICE_ETHERNET;
252
253 case BACKUP_BOOT_DEVICE_MMC:
254 if (bkup_bootmode_cfg)
255 return BOOT_DEVICE_MMC2;
256 return BOOT_DEVICE_MMC1;
257
258 case BACKUP_BOOT_DEVICE_SPI:
259 return BOOT_DEVICE_SPI;
260
261 case BACKUP_BOOT_DEVICE_I2C:
262 return BOOT_DEVICE_I2C;
263
264 case BACKUP_BOOT_DEVICE_DFU:
265 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
266 return BOOT_DEVICE_USB;
267 return BOOT_DEVICE_DFU;
268 };
269
270 return BOOT_DEVICE_RAM;
271}
272
273static u32 __get_primary_bootmedia(u32 devstat)
274{
275 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
276 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
277 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
278 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
279
280 switch (bootmode) {
281 case BOOT_DEVICE_OSPI:
282 fallthrough;
283 case BOOT_DEVICE_QSPI:
284 fallthrough;
285 case BOOT_DEVICE_XSPI:
286 fallthrough;
287 case BOOT_DEVICE_SPI:
288 return BOOT_DEVICE_SPI;
289
290 case BOOT_DEVICE_ETHERNET_RGMII:
291 fallthrough;
292 case BOOT_DEVICE_ETHERNET_RMII:
293 return BOOT_DEVICE_ETHERNET;
294
295 case BOOT_DEVICE_EMMC:
296 return BOOT_DEVICE_MMC1;
297
298 case BOOT_DEVICE_MMC:
299 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
300 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
301 return BOOT_DEVICE_MMC2;
302 return BOOT_DEVICE_MMC1;
303
304 case BOOT_DEVICE_DFU:
305 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
306 MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
307 return BOOT_DEVICE_USB;
308 return BOOT_DEVICE_DFU;
309
310 case BOOT_DEVICE_NOBOOT:
311 return BOOT_DEVICE_RAM;
312 }
313
314 return bootmode;
315}
316
317u32 spl_boot_device(void)
318{
319 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
320 u32 bootmedia;
321
322 if (bootindex == K3_PRIMARY_BOOTMODE)
323 bootmedia = __get_primary_bootmedia(devstat);
324 else
325 bootmedia = __get_backup_bootmedia(devstat);
326
327 debug("am625_init: %s: devstat = 0x%x bootmedia = 0x%x bootindex = %d\n",
328 __func__, devstat, bootmedia, bootindex);
329
330 return bootmedia;
331}