blob: 961f36e654988b9cc32723364718c85b9b30483b [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 /*
239 * Force probe of clk_k3 driver here to ensure basic default clock
240 * configuration is always done.
241 */
242 if (IS_ENABLED(CONFIG_SPL_CLK_K3)) {
243 ret = uclass_get_device_by_driver(UCLASS_CLK,
244 DM_DRIVER_GET(ti_clk),
245 &dev);
246 if (ret)
247 printf("Failed to initialize clk-k3!\n");
248 }
249
250 /* Output System Firmware version info */
251 k3_sysfw_print_ver();
252
Julien Panis01b00d42022-07-01 14:30:11 +0200253 if (IS_ENABLED(CONFIG_ESM_K3)) {
254 /* Probe/configure ESM0 */
255 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@420000", &dev);
256 if (ret)
257 printf("esm main init failed: %d\n", ret);
258
259 /* Probe/configure MCUESM */
260 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@4100000", &dev);
261 if (ret)
262 printf("esm mcu init failed: %d\n", ret);
263
264 enable_mcu_esm_reset();
265 }
266
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500267 if (IS_ENABLED(CONFIG_K3_AM64_DDRSS)) {
268 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
269 if (ret)
270 panic("DRAM init failed: %d\n", ret);
271 }
Joao Paulo Goncalvesfc3557f2023-11-13 16:07:21 -0300272 spl_enable_cache();
Joao Paulo Goncalves908ee7b2024-03-20 09:16:32 -0300273
274 fixup_a53_cpu_freq_by_speed_grade();
Suman Anna27fa4122022-05-25 13:38:42 +0530275}
276
277u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
278{
279 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
Martyn Welch5713c622022-12-20 18:38:18 +0000280 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
281 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
Suman Anna27fa4122022-05-25 13:38:42 +0530282 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
283 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
284
Martyn Welch5713c622022-12-20 18:38:18 +0000285 switch (bootmode) {
286 case BOOT_DEVICE_EMMC:
Michael Trimarchib96f93b2023-12-08 08:53:05 +0100287 if (IS_ENABLED(CONFIG_SUPPORT_EMMC_BOOT))
288 return MMCSD_MODE_EMMCBOOT;
Nishanth Menondd343862023-08-25 13:03:00 -0500289 if (IS_ENABLED(CONFIG_SPL_FS_FAT) || IS_ENABLED(CONFIG_SPL_FS_EXT4))
290 return MMCSD_MODE_FS;
Martyn Welch5713c622022-12-20 18:38:18 +0000291 return MMCSD_MODE_EMMCBOOT;
292 case BOOT_DEVICE_MMC:
293 if (bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_FS_RAW_MASK)
294 return MMCSD_MODE_RAW;
Suman Anna27fa4122022-05-25 13:38:42 +0530295 default:
Martyn Welch5713c622022-12-20 18:38:18 +0000296 return MMCSD_MODE_FS;
Suman Anna27fa4122022-05-25 13:38:42 +0530297 }
298}
299
300static u32 __get_backup_bootmedia(u32 devstat)
301{
302 u32 bkup_bootmode = (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
303 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
304 u32 bkup_bootmode_cfg =
305 (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
306 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
307
308 switch (bkup_bootmode) {
309 case BACKUP_BOOT_DEVICE_UART:
310 return BOOT_DEVICE_UART;
311
312 case BACKUP_BOOT_DEVICE_USB:
313 return BOOT_DEVICE_USB;
314
315 case BACKUP_BOOT_DEVICE_ETHERNET:
316 return BOOT_DEVICE_ETHERNET;
317
318 case BACKUP_BOOT_DEVICE_MMC:
319 if (bkup_bootmode_cfg)
320 return BOOT_DEVICE_MMC2;
321 return BOOT_DEVICE_MMC1;
322
323 case BACKUP_BOOT_DEVICE_SPI:
324 return BOOT_DEVICE_SPI;
325
326 case BACKUP_BOOT_DEVICE_I2C:
327 return BOOT_DEVICE_I2C;
328
329 case BACKUP_BOOT_DEVICE_DFU:
330 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
331 return BOOT_DEVICE_USB;
332 return BOOT_DEVICE_DFU;
333 };
334
335 return BOOT_DEVICE_RAM;
336}
337
338static u32 __get_primary_bootmedia(u32 devstat)
339{
340 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
341 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
342 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
343 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
344
345 switch (bootmode) {
346 case BOOT_DEVICE_OSPI:
347 fallthrough;
348 case BOOT_DEVICE_QSPI:
349 fallthrough;
350 case BOOT_DEVICE_XSPI:
351 fallthrough;
352 case BOOT_DEVICE_SPI:
353 return BOOT_DEVICE_SPI;
354
355 case BOOT_DEVICE_ETHERNET_RGMII:
356 fallthrough;
357 case BOOT_DEVICE_ETHERNET_RMII:
358 return BOOT_DEVICE_ETHERNET;
359
360 case BOOT_DEVICE_EMMC:
361 return BOOT_DEVICE_MMC1;
362
363 case BOOT_DEVICE_MMC:
364 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
365 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
366 return BOOT_DEVICE_MMC2;
367 return BOOT_DEVICE_MMC1;
368
369 case BOOT_DEVICE_DFU:
370 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
371 MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
372 return BOOT_DEVICE_USB;
373 return BOOT_DEVICE_DFU;
374
375 case BOOT_DEVICE_NOBOOT:
376 return BOOT_DEVICE_RAM;
377 }
378
379 return bootmode;
380}
381
382u32 spl_boot_device(void)
383{
384 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
385 u32 bootmedia;
386
387 if (bootindex == K3_PRIMARY_BOOTMODE)
388 bootmedia = __get_primary_bootmedia(devstat);
389 else
390 bootmedia = __get_backup_bootmedia(devstat);
391
392 debug("am625_init: %s: devstat = 0x%x bootmedia = 0x%x bootindex = %d\n",
393 __func__, devstat, bootmedia, bootindex);
394
395 return bootmedia;
396}