blob: a422919fab131a099b1f7786f2a84ca0a413dd38 [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>
Suman Anna27fa4122022-05-25 13:38:42 +053012#include <dm.h>
13#include <dm/uclass-internal.h>
14#include <dm/pinctrl.h>
Joao Paulo Goncalves908ee7b2024-03-20 09:16:32 -030015#include <dm/ofnode.h>
Suman Anna27fa4122022-05-25 13:38:42 +053016
Andrew Davis336b0792024-05-10 15:21:24 -050017#include "../sysfw-loader.h"
18#include "../common.h"
19
Nishanth Menond8af3ac2023-05-16 18:06:21 -050020#define RTC_BASE_ADDRESS 0x2b1f0000
21#define REG_K3RTC_S_CNT_LSW (RTC_BASE_ADDRESS + 0x18)
22#define REG_K3RTC_KICK0 (RTC_BASE_ADDRESS + 0x70)
23#define REG_K3RTC_KICK1 (RTC_BASE_ADDRESS + 0x74)
24
25/* Magic values for lock/unlock */
26#define K3RTC_KICK0_UNLOCK_VALUE 0x83e70b13
27#define K3RTC_KICK1_UNLOCK_VALUE 0x95a4f1e0
28
Joao Paulo Goncalves908ee7b2024-03-20 09:16:32 -030029/* TISCI DEV ID for A53 Clock */
30#define AM62X_DEV_A53SS0_CORE_0_DEV_ID 135
31
Bryan Brattlof3cf94322025-04-14 15:20:03 -050032struct fwl_data rom_fwls[] = {
33 { "SOC_DEVGRP_MAIN", 641, 1 },
34 { "SOC_DEVGRP_MAIN", 642, 1 },
35 { "SOC_DEVGRP_MAIN", 642, 2 },
36};
37
Suman Anna27fa4122022-05-25 13:38:42 +053038/*
39 * This uninitialized global variable would normal end up in the .bss section,
40 * but the .bss is cleared between writing and reading this variable, so move
41 * it to the .data section.
42 */
43u32 bootindex __section(".data");
44static struct rom_extended_boot_data bootdata __section(".data");
45
46static void store_boot_info_from_rom(void)
47{
48 bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
Bryan Brattlof270537c2022-11-22 13:28:11 -060049 memcpy(&bootdata, (uintptr_t *)ROM_EXTENDED_BOOT_DATA_INFO,
Suman Anna27fa4122022-05-25 13:38:42 +053050 sizeof(struct rom_extended_boot_data));
51}
52
53static void ctrl_mmr_unlock(void)
54{
55 /* Unlock all WKUP_CTRL_MMR0 module registers */
56 mmr_unlock(WKUP_CTRL_MMR0_BASE, 0);
57 mmr_unlock(WKUP_CTRL_MMR0_BASE, 1);
58 mmr_unlock(WKUP_CTRL_MMR0_BASE, 2);
59 mmr_unlock(WKUP_CTRL_MMR0_BASE, 3);
60 mmr_unlock(WKUP_CTRL_MMR0_BASE, 4);
61 mmr_unlock(WKUP_CTRL_MMR0_BASE, 5);
62 mmr_unlock(WKUP_CTRL_MMR0_BASE, 6);
63 mmr_unlock(WKUP_CTRL_MMR0_BASE, 7);
64
65 /* Unlock all CTRL_MMR0 module registers */
66 mmr_unlock(CTRL_MMR0_BASE, 0);
67 mmr_unlock(CTRL_MMR0_BASE, 1);
68 mmr_unlock(CTRL_MMR0_BASE, 2);
69 mmr_unlock(CTRL_MMR0_BASE, 4);
70 mmr_unlock(CTRL_MMR0_BASE, 6);
71
72 /* Unlock all MCU_CTRL_MMR0 module registers */
73 mmr_unlock(MCU_CTRL_MMR0_BASE, 0);
74 mmr_unlock(MCU_CTRL_MMR0_BASE, 1);
75 mmr_unlock(MCU_CTRL_MMR0_BASE, 2);
76 mmr_unlock(MCU_CTRL_MMR0_BASE, 3);
77 mmr_unlock(MCU_CTRL_MMR0_BASE, 4);
78 mmr_unlock(MCU_CTRL_MMR0_BASE, 6);
79
80 /* Unlock PADCFG_CTRL_MMR padconf registers */
81 mmr_unlock(PADCFG_MMR0_BASE, 1);
82 mmr_unlock(PADCFG_MMR1_BASE, 1);
83}
84
Julien Panis01b00d42022-07-01 14:30:11 +020085static __maybe_unused void enable_mcu_esm_reset(void)
86{
87 /* Set CTRLMMR_MCU_RST_CTRL:MCU_ESM_ERROR_RST_EN_Z to '0' (low active) */
88 u32 stat = readl(CTRLMMR_MCU_RST_CTRL);
89
90 stat &= RST_CTRL_ESM_ERROR_RST_EN_Z_MASK;
91 writel(stat, CTRLMMR_MCU_RST_CTRL);
92}
Nishanth Menond8af3ac2023-05-16 18:06:21 -050093
Nishanth Menond8af3ac2023-05-16 18:06:21 -050094/*
95 * RTC Erratum i2327 Workaround for Silicon Revision 1
96 *
97 * Due to a bug in initial synchronization out of cold power on,
98 * IRQ status can get locked infinitely if we do not unlock RTC
99 *
100 * This workaround *must* be applied within 1 second of power on,
101 * So, this is closest point to be able to guarantee the max
102 * timing.
103 *
104 * https://www.ti.com/lit/er/sprz487c/sprz487c.pdf
105 */
Nishanth Menond654f682023-08-25 13:02:58 -0500106static __maybe_unused void rtc_erratumi2327_init(void)
Nishanth Menond8af3ac2023-05-16 18:06:21 -0500107{
108 u32 counter;
109
110 /*
111 * If counter has gone past 1, nothing we can do, leave
112 * system locked! This is the only way we know if RTC
113 * can be used for all practical purposes.
114 */
115 counter = readl(REG_K3RTC_S_CNT_LSW);
116 if (counter > 1)
117 return;
118 /*
119 * Need to set this up at the very start
120 * MUST BE DONE under 1 second of boot.
121 */
122 writel(K3RTC_KICK0_UNLOCK_VALUE, REG_K3RTC_KICK0);
123 writel(K3RTC_KICK1_UNLOCK_VALUE, REG_K3RTC_KICK1);
Nishanth Menond8af3ac2023-05-16 18:06:21 -0500124}
Joao Paulo Goncalves908ee7b2024-03-20 09:16:32 -0300125
126#if CONFIG_IS_ENABLED(OF_CONTROL)
127static int get_a53_cpu_clock_index(ofnode node)
128{
129 int count, i;
130 struct ofnode_phandle_args *args;
131 ofnode clknode;
132
133 clknode = ofnode_path("/bus@f0000/system-controller@44043000/clock-controller");
134 if (!ofnode_valid(clknode))
135 return -1;
136
137 count = ofnode_count_phandle_with_args(node, "assigned-clocks", "#clock-cells", 0);
138
139 for (i = 0; i < count; i++) {
140 if (!ofnode_parse_phandle_with_args(node, "assigned-clocks",
141 "#clock-cells", 0, i, args)) {
142 if (ofnode_equal(clknode, args->node) &&
143 args->args[0] == AM62X_DEV_A53SS0_CORE_0_DEV_ID)
144 return i;
145 }
146 }
147
148 return -1;
149}
150
151static void fixup_a53_cpu_freq_by_speed_grade(void)
152{
153 int index, size;
154 u32 *rates;
155 ofnode node;
156
157 node = ofnode_path("/a53@0");
158 if (!ofnode_valid(node))
159 return;
160
161 rates = fdt_getprop_w(ofnode_to_fdt(node), ofnode_to_offset(node),
162 "assigned-clock-rates", &size);
163
164 index = get_a53_cpu_clock_index(node);
165
166 if (!rates || index < 0 || index >= (size / sizeof(u32))) {
167 printf("Wrong A53 assigned-clocks configuration\n");
168 return;
169 }
170
171 rates[index] = cpu_to_fdt32(k3_get_a53_max_frequency());
172
173 printf("Changed A53 CPU frequency to %dHz (%c grade) in DT\n",
174 k3_get_a53_max_frequency(), k3_get_speed_grade());
175}
176#else
177static void fixup_a53_cpu_freq_by_speed_grade(void)
178{
179}
180#endif
Julien Panis01b00d42022-07-01 14:30:11 +0200181
Suman Anna27fa4122022-05-25 13:38:42 +0530182void board_init_f(ulong dummy)
183{
184 struct udevice *dev;
185 int ret;
Bryan Brattlof3cf94322025-04-14 15:20:03 -0500186 int i;
Suman Anna27fa4122022-05-25 13:38:42 +0530187
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500188 if (IS_ENABLED(CONFIG_CPU_V7R)) {
189 setup_k3_mpu_regions();
190 rtc_erratumi2327_init();
191 }
Suman Anna27fa4122022-05-25 13:38:42 +0530192
193 /*
194 * Cannot delay this further as there is a chance that
195 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
196 */
197 store_boot_info_from_rom();
198
199 ctrl_mmr_unlock();
200
201 /* Init DM early */
202 spl_early_init();
203
204 /*
205 * Process pinctrl for the serial0 and serial3, aka WKUP_UART0 and
206 * MAIN_UART1 modules and continue regardless of the result of pinctrl.
207 * Do this without probing the device, but instead by searching the
208 * device that would request the given sequence number if probed. The
209 * UARTs will be used by the DM firmware and TIFS firmware images
210 * respectively and the firmware depend on SPL to initialize the pin
211 * settings.
212 */
213 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 0, &dev);
214 if (!ret)
215 pinctrl_select_state(dev, "default");
216
217 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev);
218 if (!ret)
219 pinctrl_select_state(dev, "default");
220
221 preloader_console_init();
222
Wadim Egorov51b71d42024-05-22 09:55:03 +0200223 do_board_detect();
224
Suman Anna27fa4122022-05-25 13:38:42 +0530225 /*
226 * Allow establishing an early console as required for example when
227 * doing a UART-based boot. Note that this console may not "survive"
228 * through a SYSFW PM-init step and will need a re-init in some way
229 * due to changing module clock frequencies.
230 */
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500231 if (IS_ENABLED(CONFIG_K3_EARLY_CONS))
232 early_console_init();
Suman Anna27fa4122022-05-25 13:38:42 +0530233
Suman Anna27fa4122022-05-25 13:38:42 +0530234 /*
235 * Configure and start up system controller firmware. Provide
236 * the U-Boot console init function to the SYSFW post-PM configuration
237 * callback hook, effectively switching on (or over) the console
238 * output.
239 */
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500240 if (IS_ENABLED(CONFIG_K3_LOAD_SYSFW)) {
241 ret = is_rom_loaded_sysfw(&bootdata);
242 if (!ret)
243 panic("ROM has not loaded TIFS firmware\n");
Suman Anna27fa4122022-05-25 13:38:42 +0530244
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500245 k3_sysfw_loader(true, NULL, NULL);
246 }
Suman Anna27fa4122022-05-25 13:38:42 +0530247
248 /*
Wadim Egorova85a8fa2024-04-03 15:59:09 +0200249 * Relocate boot information to OCRAM (after TIFS has opend this
250 * region for us) so the next bootloader stages can keep access to
251 * primary vs backup bootmodes.
252 */
253 if (IS_ENABLED(CONFIG_CPU_V7R))
254 writel(bootindex, K3_BOOT_PARAM_TABLE_INDEX_OCRAM);
255
256 /*
Suman Anna27fa4122022-05-25 13:38:42 +0530257 * Force probe of clk_k3 driver here to ensure basic default clock
258 * configuration is always done.
259 */
260 if (IS_ENABLED(CONFIG_SPL_CLK_K3)) {
261 ret = uclass_get_device_by_driver(UCLASS_CLK,
262 DM_DRIVER_GET(ti_clk),
263 &dev);
264 if (ret)
265 printf("Failed to initialize clk-k3!\n");
266 }
267
268 /* Output System Firmware version info */
269 k3_sysfw_print_ver();
270
Bryan Brattlof3cf94322025-04-14 15:20:03 -0500271 /* Disable firewalls ROM has configured. */
272 if (IS_ENABLED(CONFIG_CPU_V7R))
273 for (i = 0; i < ARRAY_SIZE(rom_fwls); i++)
274 remove_fwl_region(&rom_fwls[i]);
275
Julien Panis01b00d42022-07-01 14:30:11 +0200276 if (IS_ENABLED(CONFIG_ESM_K3)) {
277 /* Probe/configure ESM0 */
278 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@420000", &dev);
279 if (ret)
280 printf("esm main init failed: %d\n", ret);
281
282 /* Probe/configure MCUESM */
283 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@4100000", &dev);
284 if (ret)
285 printf("esm mcu init failed: %d\n", ret);
286
287 enable_mcu_esm_reset();
288 }
289
Nishanth Menon3bb13ba2023-08-25 13:02:57 -0500290 if (IS_ENABLED(CONFIG_K3_AM64_DDRSS)) {
291 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
292 if (ret)
293 panic("DRAM init failed: %d\n", ret);
294 }
Joao Paulo Goncalvesfc3557f2023-11-13 16:07:21 -0300295 spl_enable_cache();
Joao Paulo Goncalves908ee7b2024-03-20 09:16:32 -0300296
Kishon Vijay Abraham I44558bc2024-08-26 15:55:11 +0530297 if (IS_ENABLED(CONFIG_SPL_ETH) && IS_ENABLED(CONFIG_TI_AM65_CPSW_NUSS) &&
298 spl_boot_device() == BOOT_DEVICE_ETHERNET) {
299 struct udevice *cpswdev;
300
301 if (uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(am65_cpsw_nuss),
302 &cpswdev))
303 printf("Failed to probe am65_cpsw_nuss driver\n");
304 }
305
Joao Paulo Goncalves908ee7b2024-03-20 09:16:32 -0300306 fixup_a53_cpu_freq_by_speed_grade();
Suman Anna27fa4122022-05-25 13:38:42 +0530307}
308
309u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
310{
311 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
Martyn Welch5713c622022-12-20 18:38:18 +0000312 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
313 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
Suman Anna27fa4122022-05-25 13:38:42 +0530314 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
315 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
316
Martyn Welch5713c622022-12-20 18:38:18 +0000317 switch (bootmode) {
318 case BOOT_DEVICE_EMMC:
Michael Trimarchib96f93b2023-12-08 08:53:05 +0100319 if (IS_ENABLED(CONFIG_SUPPORT_EMMC_BOOT))
320 return MMCSD_MODE_EMMCBOOT;
Nishanth Menondd343862023-08-25 13:03:00 -0500321 if (IS_ENABLED(CONFIG_SPL_FS_FAT) || IS_ENABLED(CONFIG_SPL_FS_EXT4))
322 return MMCSD_MODE_FS;
Martyn Welch5713c622022-12-20 18:38:18 +0000323 return MMCSD_MODE_EMMCBOOT;
324 case BOOT_DEVICE_MMC:
325 if (bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_FS_RAW_MASK)
326 return MMCSD_MODE_RAW;
Suman Anna27fa4122022-05-25 13:38:42 +0530327 default:
Martyn Welch5713c622022-12-20 18:38:18 +0000328 return MMCSD_MODE_FS;
Suman Anna27fa4122022-05-25 13:38:42 +0530329 }
330}
331
Suman Anna27fa4122022-05-25 13:38:42 +0530332u32 spl_boot_device(void)
333{
Wadim Egorov3eab2062024-04-03 15:59:10 +0200334 return get_boot_device();
Suman Anna27fa4122022-05-25 13:38:42 +0530335}