blob: 2d31abac9121fcdb8eea7cfd18e4cb1dc4accb7d [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>
Joao Paulo Goncalves908ee7b2024-03-20 09:16:32 -030017#include <dm/ofnode.h>
Suman Anna27fa4122022-05-25 13:38:42 +053018
Nishanth Menond8af3ac2023-05-16 18:06:21 -050019#define RTC_BASE_ADDRESS 0x2b1f0000
20#define REG_K3RTC_S_CNT_LSW (RTC_BASE_ADDRESS + 0x18)
21#define REG_K3RTC_KICK0 (RTC_BASE_ADDRESS + 0x70)
22#define REG_K3RTC_KICK1 (RTC_BASE_ADDRESS + 0x74)
23
24/* Magic values for lock/unlock */
25#define K3RTC_KICK0_UNLOCK_VALUE 0x83e70b13
26#define K3RTC_KICK1_UNLOCK_VALUE 0x95a4f1e0
27
Joao Paulo Goncalves908ee7b2024-03-20 09:16:32 -030028/* TISCI DEV ID for A53 Clock */
29#define AM62X_DEV_A53SS0_CORE_0_DEV_ID 135
30
Suman Anna27fa4122022-05-25 13:38:42 +053031/*
32 * This uninitialized global variable would normal end up in the .bss section,
33 * but the .bss is cleared between writing and reading this variable, so move
34 * it to the .data section.
35 */
36u32 bootindex __section(".data");
37static struct rom_extended_boot_data bootdata __section(".data");
38
39static void store_boot_info_from_rom(void)
40{
41 bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
Bryan Brattlof270537c2022-11-22 13:28:11 -060042 memcpy(&bootdata, (uintptr_t *)ROM_EXTENDED_BOOT_DATA_INFO,
Suman Anna27fa4122022-05-25 13:38:42 +053043 sizeof(struct rom_extended_boot_data));
44}
45
46static void ctrl_mmr_unlock(void)
47{
48 /* Unlock all WKUP_CTRL_MMR0 module registers */
49 mmr_unlock(WKUP_CTRL_MMR0_BASE, 0);
50 mmr_unlock(WKUP_CTRL_MMR0_BASE, 1);
51 mmr_unlock(WKUP_CTRL_MMR0_BASE, 2);
52 mmr_unlock(WKUP_CTRL_MMR0_BASE, 3);
53 mmr_unlock(WKUP_CTRL_MMR0_BASE, 4);
54 mmr_unlock(WKUP_CTRL_MMR0_BASE, 5);
55 mmr_unlock(WKUP_CTRL_MMR0_BASE, 6);
56 mmr_unlock(WKUP_CTRL_MMR0_BASE, 7);
57
58 /* Unlock all CTRL_MMR0 module registers */
59 mmr_unlock(CTRL_MMR0_BASE, 0);
60 mmr_unlock(CTRL_MMR0_BASE, 1);
61 mmr_unlock(CTRL_MMR0_BASE, 2);
62 mmr_unlock(CTRL_MMR0_BASE, 4);
63 mmr_unlock(CTRL_MMR0_BASE, 6);
64
65 /* Unlock all MCU_CTRL_MMR0 module registers */
66 mmr_unlock(MCU_CTRL_MMR0_BASE, 0);
67 mmr_unlock(MCU_CTRL_MMR0_BASE, 1);
68 mmr_unlock(MCU_CTRL_MMR0_BASE, 2);
69 mmr_unlock(MCU_CTRL_MMR0_BASE, 3);
70 mmr_unlock(MCU_CTRL_MMR0_BASE, 4);
71 mmr_unlock(MCU_CTRL_MMR0_BASE, 6);
72
73 /* Unlock PADCFG_CTRL_MMR padconf registers */
74 mmr_unlock(PADCFG_MMR0_BASE, 1);
75 mmr_unlock(PADCFG_MMR1_BASE, 1);
76}
77
Julien Panis01b00d42022-07-01 14:30:11 +020078static __maybe_unused void enable_mcu_esm_reset(void)
79{
80 /* Set CTRLMMR_MCU_RST_CTRL:MCU_ESM_ERROR_RST_EN_Z to '0' (low active) */
81 u32 stat = readl(CTRLMMR_MCU_RST_CTRL);
82
83 stat &= RST_CTRL_ESM_ERROR_RST_EN_Z_MASK;
84 writel(stat, CTRLMMR_MCU_RST_CTRL);
85}
Nishanth Menond8af3ac2023-05-16 18:06:21 -050086
Nishanth Menond8af3ac2023-05-16 18:06:21 -050087/*
88 * RTC Erratum i2327 Workaround for Silicon Revision 1
89 *
90 * Due to a bug in initial synchronization out of cold power on,
91 * IRQ status can get locked infinitely if we do not unlock RTC
92 *
93 * This workaround *must* be applied within 1 second of power on,
94 * So, this is closest point to be able to guarantee the max
95 * timing.
96 *
97 * https://www.ti.com/lit/er/sprz487c/sprz487c.pdf
98 */
Nishanth Menond654f682023-08-25 13:02:58 -050099static __maybe_unused void rtc_erratumi2327_init(void)
Nishanth Menond8af3ac2023-05-16 18:06:21 -0500100{
101 u32 counter;
102
103 /*
104 * If counter has gone past 1, nothing we can do, leave
105 * system locked! This is the only way we know if RTC
106 * can be used for all practical purposes.
107 */
108 counter = readl(REG_K3RTC_S_CNT_LSW);
109 if (counter > 1)
110 return;
111 /*
112 * Need to set this up at the very start
113 * MUST BE DONE under 1 second of boot.
114 */
115 writel(K3RTC_KICK0_UNLOCK_VALUE, REG_K3RTC_KICK0);
116 writel(K3RTC_KICK1_UNLOCK_VALUE, REG_K3RTC_KICK1);
Nishanth Menond8af3ac2023-05-16 18:06:21 -0500117}
Joao Paulo Goncalves908ee7b2024-03-20 09:16:32 -0300118
119#if CONFIG_IS_ENABLED(OF_CONTROL)
120static int get_a53_cpu_clock_index(ofnode node)
121{
122 int count, i;
123 struct ofnode_phandle_args *args;
124 ofnode clknode;
125
126 clknode = ofnode_path("/bus@f0000/system-controller@44043000/clock-controller");
127 if (!ofnode_valid(clknode))
128 return -1;
129
130 count = ofnode_count_phandle_with_args(node, "assigned-clocks", "#clock-cells", 0);
131
132 for (i = 0; i < count; i++) {
133 if (!ofnode_parse_phandle_with_args(node, "assigned-clocks",
134 "#clock-cells", 0, i, args)) {
135 if (ofnode_equal(clknode, args->node) &&
136 args->args[0] == AM62X_DEV_A53SS0_CORE_0_DEV_ID)
137 return i;
138 }
139 }
140
141 return -1;
142}
143
144static void fixup_a53_cpu_freq_by_speed_grade(void)
145{
146 int index, size;
147 u32 *rates;
148 ofnode node;
149
150 node = ofnode_path("/a53@0");
151 if (!ofnode_valid(node))
152 return;
153
154 rates = fdt_getprop_w(ofnode_to_fdt(node), ofnode_to_offset(node),
155 "assigned-clock-rates", &size);
156
157 index = get_a53_cpu_clock_index(node);
158
159 if (!rates || index < 0 || index >= (size / sizeof(u32))) {
160 printf("Wrong A53 assigned-clocks configuration\n");
161 return;
162 }
163
164 rates[index] = cpu_to_fdt32(k3_get_a53_max_frequency());
165
166 printf("Changed A53 CPU frequency to %dHz (%c grade) in DT\n",
167 k3_get_a53_max_frequency(), k3_get_speed_grade());
168}
169#else
170static void fixup_a53_cpu_freq_by_speed_grade(void)
171{
172}
173#endif
Julien Panis01b00d42022-07-01 14:30:11 +0200174
Suman Anna27fa4122022-05-25 13:38:42 +0530175void board_init_f(ulong dummy)
176{
177 struct udevice *dev;
178 int ret;
179
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500180 if (IS_ENABLED(CONFIG_CPU_V7R)) {
181 setup_k3_mpu_regions();
182 rtc_erratumi2327_init();
183 }
Suman Anna27fa4122022-05-25 13:38:42 +0530184
185 /*
186 * Cannot delay this further as there is a chance that
187 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
188 */
189 store_boot_info_from_rom();
190
191 ctrl_mmr_unlock();
192
193 /* Init DM early */
194 spl_early_init();
195
196 /*
197 * Process pinctrl for the serial0 and serial3, aka WKUP_UART0 and
198 * MAIN_UART1 modules and continue regardless of the result of pinctrl.
199 * Do this without probing the device, but instead by searching the
200 * device that would request the given sequence number if probed. The
201 * UARTs will be used by the DM firmware and TIFS firmware images
202 * respectively and the firmware depend on SPL to initialize the pin
203 * settings.
204 */
205 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 0, &dev);
206 if (!ret)
207 pinctrl_select_state(dev, "default");
208
209 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev);
210 if (!ret)
211 pinctrl_select_state(dev, "default");
212
213 preloader_console_init();
214
Suman Anna27fa4122022-05-25 13:38:42 +0530215 /*
216 * Allow establishing an early console as required for example when
217 * doing a UART-based boot. Note that this console may not "survive"
218 * through a SYSFW PM-init step and will need a re-init in some way
219 * due to changing module clock frequencies.
220 */
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500221 if (IS_ENABLED(CONFIG_K3_EARLY_CONS))
222 early_console_init();
Suman Anna27fa4122022-05-25 13:38:42 +0530223
Suman Anna27fa4122022-05-25 13:38:42 +0530224 /*
225 * Configure and start up system controller firmware. Provide
226 * the U-Boot console init function to the SYSFW post-PM configuration
227 * callback hook, effectively switching on (or over) the console
228 * output.
229 */
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500230 if (IS_ENABLED(CONFIG_K3_LOAD_SYSFW)) {
231 ret = is_rom_loaded_sysfw(&bootdata);
232 if (!ret)
233 panic("ROM has not loaded TIFS firmware\n");
Suman Anna27fa4122022-05-25 13:38:42 +0530234
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500235 k3_sysfw_loader(true, NULL, NULL);
236 }
Suman Anna27fa4122022-05-25 13:38:42 +0530237
238 /*
Wadim Egorova85a8fa2024-04-03 15:59:09 +0200239 * Relocate boot information to OCRAM (after TIFS has opend this
240 * region for us) so the next bootloader stages can keep access to
241 * primary vs backup bootmodes.
242 */
243 if (IS_ENABLED(CONFIG_CPU_V7R))
244 writel(bootindex, K3_BOOT_PARAM_TABLE_INDEX_OCRAM);
245
246 /*
Suman Anna27fa4122022-05-25 13:38:42 +0530247 * Force probe of clk_k3 driver here to ensure basic default clock
248 * configuration is always done.
249 */
250 if (IS_ENABLED(CONFIG_SPL_CLK_K3)) {
251 ret = uclass_get_device_by_driver(UCLASS_CLK,
252 DM_DRIVER_GET(ti_clk),
253 &dev);
254 if (ret)
255 printf("Failed to initialize clk-k3!\n");
256 }
257
258 /* Output System Firmware version info */
259 k3_sysfw_print_ver();
260
Julien Panis01b00d42022-07-01 14:30:11 +0200261 if (IS_ENABLED(CONFIG_ESM_K3)) {
262 /* Probe/configure ESM0 */
263 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@420000", &dev);
264 if (ret)
265 printf("esm main init failed: %d\n", ret);
266
267 /* Probe/configure MCUESM */
268 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@4100000", &dev);
269 if (ret)
270 printf("esm mcu init failed: %d\n", ret);
271
272 enable_mcu_esm_reset();
273 }
274
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500275 if (IS_ENABLED(CONFIG_K3_AM64_DDRSS)) {
276 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
277 if (ret)
278 panic("DRAM init failed: %d\n", ret);
279 }
Joao Paulo Goncalvesfc3557f2023-11-13 16:07:21 -0300280 spl_enable_cache();
Joao Paulo Goncalves908ee7b2024-03-20 09:16:32 -0300281
282 fixup_a53_cpu_freq_by_speed_grade();
Suman Anna27fa4122022-05-25 13:38:42 +0530283}
284
285u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
286{
287 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
Martyn Welch5713c622022-12-20 18:38:18 +0000288 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
289 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
Suman Anna27fa4122022-05-25 13:38:42 +0530290 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
291 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
292
Martyn Welch5713c622022-12-20 18:38:18 +0000293 switch (bootmode) {
294 case BOOT_DEVICE_EMMC:
Michael Trimarchib96f93b2023-12-08 08:53:05 +0100295 if (IS_ENABLED(CONFIG_SUPPORT_EMMC_BOOT))
296 return MMCSD_MODE_EMMCBOOT;
Nishanth Menondd343862023-08-25 13:03:00 -0500297 if (IS_ENABLED(CONFIG_SPL_FS_FAT) || IS_ENABLED(CONFIG_SPL_FS_EXT4))
298 return MMCSD_MODE_FS;
Martyn Welch5713c622022-12-20 18:38:18 +0000299 return MMCSD_MODE_EMMCBOOT;
300 case BOOT_DEVICE_MMC:
301 if (bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_FS_RAW_MASK)
302 return MMCSD_MODE_RAW;
Suman Anna27fa4122022-05-25 13:38:42 +0530303 default:
Martyn Welch5713c622022-12-20 18:38:18 +0000304 return MMCSD_MODE_FS;
Suman Anna27fa4122022-05-25 13:38:42 +0530305 }
306}
307
308static u32 __get_backup_bootmedia(u32 devstat)
309{
310 u32 bkup_bootmode = (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
311 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
312 u32 bkup_bootmode_cfg =
313 (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
314 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
315
316 switch (bkup_bootmode) {
317 case BACKUP_BOOT_DEVICE_UART:
318 return BOOT_DEVICE_UART;
319
320 case BACKUP_BOOT_DEVICE_USB:
321 return BOOT_DEVICE_USB;
322
323 case BACKUP_BOOT_DEVICE_ETHERNET:
324 return BOOT_DEVICE_ETHERNET;
325
326 case BACKUP_BOOT_DEVICE_MMC:
327 if (bkup_bootmode_cfg)
328 return BOOT_DEVICE_MMC2;
329 return BOOT_DEVICE_MMC1;
330
331 case BACKUP_BOOT_DEVICE_SPI:
332 return BOOT_DEVICE_SPI;
333
334 case BACKUP_BOOT_DEVICE_I2C:
335 return BOOT_DEVICE_I2C;
336
337 case BACKUP_BOOT_DEVICE_DFU:
338 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
339 return BOOT_DEVICE_USB;
340 return BOOT_DEVICE_DFU;
341 };
342
343 return BOOT_DEVICE_RAM;
344}
345
346static u32 __get_primary_bootmedia(u32 devstat)
347{
348 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
349 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
350 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
351 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
352
353 switch (bootmode) {
354 case BOOT_DEVICE_OSPI:
355 fallthrough;
356 case BOOT_DEVICE_QSPI:
357 fallthrough;
358 case BOOT_DEVICE_XSPI:
359 fallthrough;
360 case BOOT_DEVICE_SPI:
361 return BOOT_DEVICE_SPI;
362
363 case BOOT_DEVICE_ETHERNET_RGMII:
364 fallthrough;
365 case BOOT_DEVICE_ETHERNET_RMII:
366 return BOOT_DEVICE_ETHERNET;
367
368 case BOOT_DEVICE_EMMC:
369 return BOOT_DEVICE_MMC1;
370
371 case BOOT_DEVICE_MMC:
372 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
373 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
374 return BOOT_DEVICE_MMC2;
375 return BOOT_DEVICE_MMC1;
376
377 case BOOT_DEVICE_DFU:
378 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
379 MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
380 return BOOT_DEVICE_USB;
381 return BOOT_DEVICE_DFU;
382
383 case BOOT_DEVICE_NOBOOT:
384 return BOOT_DEVICE_RAM;
385 }
386
387 return bootmode;
388}
389
390u32 spl_boot_device(void)
391{
392 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
393 u32 bootmedia;
394
395 if (bootindex == K3_PRIMARY_BOOTMODE)
396 bootmedia = __get_primary_bootmedia(devstat);
397 else
398 bootmedia = __get_backup_bootmedia(devstat);
399
400 debug("am625_init: %s: devstat = 0x%x bootmedia = 0x%x bootindex = %d\n",
401 __func__, devstat, bootmedia, bootindex);
402
403 return bootmedia;
404}