blob: 8428322ed9a7f5cb553e11f6c486c5fd4530a8d5 [file] [log] [blame]
Dave Gerlach96571ec2021-04-23 11:27:32 -05001// SPDX-License-Identifier: GPL-2.0
2/*
3 * AM642: SoC specific initialization
4 *
5 * Copyright (C) 2020-2021 Texas Instruments Incorporated - https://www.ti.com/
6 * Keerthy <j-keerthy@ti.com>
7 * Dave Gerlach <d-gerlach@ti.com>
8 */
9
10#include <common.h>
Aswath Govindraju70bc5e32021-06-04 22:00:33 +053011#include <fdt_support.h>
Dave Gerlach96571ec2021-04-23 11:27:32 -050012#include <spl.h>
13#include <asm/io.h>
Keerthy05d670e2021-04-23 11:27:33 -050014#include <asm/arch/hardware.h>
Dave Gerlach8e0689b2021-04-23 11:27:36 -050015#include <asm/arch/sysfw-loader.h>
16#include <asm/arch/sys_proto.h>
Dave Gerlach96571ec2021-04-23 11:27:32 -050017#include "common.h"
Dave Gerlach8e0689b2021-04-23 11:27:36 -050018#include <asm/arch/sys_proto.h>
19#include <linux/soc/ti/ti_sci_protocol.h>
20#include <dm.h>
21#include <dm/uclass-internal.h>
22#include <dm/pinctrl.h>
Dave Gerlacha89f7a42021-04-23 11:27:37 -050023#include <mmc.h>
Lokesh Vutla17ad6e02021-05-06 16:44:52 +053024#include <dm/root.h>
Dave Gerlach96571ec2021-04-23 11:27:32 -050025
Hari Nagallad383c5e2022-03-09 14:42:30 -060026#define MCU_CTRL_MMR0_BASE 0x04500000
27#define CTRLMMR_MCU_RST_CTRL 0x04518170
Dave Gerlach96571ec2021-04-23 11:27:32 -050028
Dave Gerlacheaef1292021-04-23 11:27:34 -050029static void ctrl_mmr_unlock(void)
30{
31 /* Unlock all PADCFG_MMR1 module registers */
32 mmr_unlock(PADCFG_MMR1_BASE, 1);
33
Hari Nagallad383c5e2022-03-09 14:42:30 -060034 /* Unlock all MCU_CTRL_MMR0 module registers */
35 mmr_unlock(MCU_CTRL_MMR0_BASE, 0);
36 mmr_unlock(MCU_CTRL_MMR0_BASE, 1);
37 mmr_unlock(MCU_CTRL_MMR0_BASE, 2);
38 mmr_unlock(MCU_CTRL_MMR0_BASE, 3);
39 mmr_unlock(MCU_CTRL_MMR0_BASE, 4);
40 mmr_unlock(MCU_CTRL_MMR0_BASE, 6);
41
Dave Gerlacheaef1292021-04-23 11:27:34 -050042 /* Unlock all CTRL_MMR0 module registers */
43 mmr_unlock(CTRL_MMR0_BASE, 0);
44 mmr_unlock(CTRL_MMR0_BASE, 1);
45 mmr_unlock(CTRL_MMR0_BASE, 2);
46 mmr_unlock(CTRL_MMR0_BASE, 3);
47 mmr_unlock(CTRL_MMR0_BASE, 5);
48 mmr_unlock(CTRL_MMR0_BASE, 6);
Christian Gmeiner33a9d8e2022-05-12 08:21:01 +020049
50 /* Unlock all MCU_PADCFG_MMR1 module registers */
51 mmr_unlock(MCU_PADCFG_MMR1_BASE, 1);
Dave Gerlacheaef1292021-04-23 11:27:34 -050052}
53
Dave Gerlachb27a9f22021-04-23 11:27:35 -050054/*
55 * This uninitialized global variable would normal end up in the .bss section,
56 * but the .bss is cleared between writing and reading this variable, so move
57 * it to the .data section.
58 */
59u32 bootindex __section(".data");
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020060static struct rom_extended_boot_data bootdata __section(".data");
Dave Gerlachb27a9f22021-04-23 11:27:35 -050061
62static void store_boot_info_from_rom(void)
63{
64 bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
65 memcpy(&bootdata, (uintptr_t *)ROM_ENTENDED_BOOT_DATA_INFO,
66 sizeof(struct rom_extended_boot_data));
67}
68
Dave Gerlacha89f7a42021-04-23 11:27:37 -050069#if defined(CONFIG_K3_LOAD_SYSFW) && CONFIG_IS_ENABLED(DM_MMC)
70void k3_mmc_stop_clock(void)
71{
72 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
73 struct mmc *mmc = find_mmc_device(0);
74
75 if (!mmc)
76 return;
77
78 mmc->saved_clock = mmc->clock;
79 mmc_set_clock(mmc, 0, true);
80 }
81}
82
83void k3_mmc_restart_clock(void)
84{
85 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
86 struct mmc *mmc = find_mmc_device(0);
87
88 if (!mmc)
89 return;
90
91 mmc_set_clock(mmc, mmc->saved_clock, false);
92 }
93}
94#else
95void k3_mmc_stop_clock(void) {}
96void k3_mmc_restart_clock(void) {}
97#endif
98
Lokesh Vutla17ad6e02021-05-06 16:44:52 +053099#ifdef CONFIG_SPL_OF_LIST
100void do_dt_magic(void)
101{
102 int ret, rescan;
103
104 if (IS_ENABLED(CONFIG_TI_I2C_BOARD_DETECT))
105 do_board_detect();
106
107 /*
108 * Board detection has been done.
109 * Let us see if another dtb wouldn't be a better match
110 * for our board
111 */
112 if (IS_ENABLED(CONFIG_CPU_V7R)) {
113 ret = fdtdec_resetup(&rescan);
114 if (!ret && rescan) {
115 dm_uninit();
116 dm_init_and_scan(true);
117 }
118 }
119}
120#endif
121
Aswath Govindraju70bc5e32021-06-04 22:00:33 +0530122#if CONFIG_IS_ENABLED(USB_STORAGE)
123static int fixup_usb_boot(const void *fdt_blob)
124{
125 int ret = 0;
126
127 switch (spl_boot_device()) {
128 case BOOT_DEVICE_USB:
129 /*
130 * If the boot mode is host, fixup the dr_mode to host
131 * before cdns3 bind takes place
132 */
133 ret = fdt_find_and_setprop((void *)fdt_blob,
134 "/bus@f4000/cdns-usb@f900000/usb@f400000",
135 "dr_mode", "host", 5, 0);
136 if (ret)
137 printf("%s: fdt_find_and_setprop() failed:%d\n",
138 __func__, ret);
139 fallthrough;
140 default:
141 break;
142 }
143
144 return ret;
145}
146
147int fdtdec_board_setup(const void *fdt_blob)
148{
149 /* Can use the pointer from the function parameters */
150 return fixup_usb_boot(fdt_blob);
151}
152#endif
153
Hari Nagallad383c5e2022-03-09 14:42:30 -0600154#if defined(CONFIG_ESM_K3)
155static void enable_mcu_esm_reset(void)
156{
157 /* Set CTRLMMR_MCU_RST_CTRL:MCU_ESM_ERROR_RST_EN_Z to '0' (low active) */
158 u32 stat = readl(CTRLMMR_MCU_RST_CTRL);
159
160 stat &= 0xFFFDFFFF;
161 writel(stat, CTRLMMR_MCU_RST_CTRL);
162}
163#endif
164
Dave Gerlach96571ec2021-04-23 11:27:32 -0500165void board_init_f(ulong dummy)
166{
Hari Nagallad383c5e2022-03-09 14:42:30 -0600167#if defined(CONFIG_K3_LOAD_SYSFW) || defined(CONFIG_K3_AM64_DDRSS) || defined(CONFIG_ESM_K3)
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500168 struct udevice *dev;
169 int ret;
170#endif
171
Dave Gerlach96571ec2021-04-23 11:27:32 -0500172#if defined(CONFIG_CPU_V7R)
173 setup_k3_mpu_regions();
174#endif
175
Dave Gerlachb27a9f22021-04-23 11:27:35 -0500176 /*
177 * Cannot delay this further as there is a chance that
178 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
179 */
180 store_boot_info_from_rom();
181
Dave Gerlacheaef1292021-04-23 11:27:34 -0500182 ctrl_mmr_unlock();
183
Dave Gerlach96571ec2021-04-23 11:27:32 -0500184 /* Init DM early */
185 spl_early_init();
186
187 preloader_console_init();
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500188
Lokesh Vutla17ad6e02021-05-06 16:44:52 +0530189 do_dt_magic();
190
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500191#if defined(CONFIG_K3_LOAD_SYSFW)
192 /*
193 * Process pinctrl for serial3 a.k.a. MAIN UART1 module and continue
194 * regardless of the result of pinctrl. Do this without probing the
195 * device, but instead by searching the device that would request the
196 * given sequence number if probed. The UART will be used by the system
197 * firmware (SYSFW) image for various purposes and SYSFW depends on us
198 * to initialize its pin settings.
199 */
200 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev);
201 if (!ret)
202 pinctrl_select_state(dev, "default");
203
204 /*
205 * Load, start up, and configure system controller firmware.
206 * This will determine whether or not ROM has already loaded
207 * system firmware and if so, will only perform needed config
208 * and not attempt to load firmware again.
209 */
Dave Gerlacha89f7a42021-04-23 11:27:37 -0500210 k3_sysfw_loader(is_rom_loaded_sysfw(&bootdata), k3_mmc_stop_clock,
211 k3_mmc_restart_clock);
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500212#endif
213
214 /* Output System Firmware version info */
215 k3_sysfw_print_ver();
Dave Gerlachad38f512021-05-04 18:00:53 -0500216
Hari Nagallad383c5e2022-03-09 14:42:30 -0600217#if defined(CONFIG_ESM_K3)
218 /* Probe/configure ESM0 */
219 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@420000", &dev);
220 if (ret)
221 printf("esm main init failed: %d\n", ret);
222
223 /* Probe/configure MCUESM */
224 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@4100000", &dev);
225 if (ret)
226 printf("esm mcu init failed: %d\n", ret);
227
228 enable_mcu_esm_reset();
229#endif
230
Dave Gerlachad38f512021-05-04 18:00:53 -0500231#if defined(CONFIG_K3_AM64_DDRSS)
232 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
233 if (ret)
234 panic("DRAM init failed: %d\n", ret);
235#endif
Vignesh Raghavendra82cc43e2021-12-24 12:55:32 +0530236 if (IS_ENABLED(CONFIG_SPL_ETH) && IS_ENABLED(CONFIG_TI_AM65_CPSW_NUSS) &&
237 spl_boot_device() == BOOT_DEVICE_ETHERNET) {
238 struct udevice *cpswdev;
239
240 if (uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(am65_cpsw_nuss), &cpswdev))
241 printf("Failed to probe am65_cpsw_nuss driver\n");
242 }
Dave Gerlach96571ec2021-04-23 11:27:32 -0500243}
Keerthy05d670e2021-04-23 11:27:33 -0500244
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100245u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Keerthy05d670e2021-04-23 11:27:33 -0500246{
247 switch (boot_device) {
248 case BOOT_DEVICE_MMC1:
249 return MMCSD_MODE_EMMCBOOT;
250
251 case BOOT_DEVICE_MMC2:
252 return MMCSD_MODE_FS;
253
254 default:
255 return MMCSD_MODE_RAW;
256 }
257}
258
259static u32 __get_backup_bootmedia(u32 main_devstat)
260{
261 u32 bkup_bootmode =
262 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
263 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
264 u32 bkup_bootmode_cfg =
265 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
266 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
267
268 switch (bkup_bootmode) {
269 case BACKUP_BOOT_DEVICE_UART:
270 return BOOT_DEVICE_UART;
271
Aswath Govindraju8a05c9a2021-06-04 22:00:32 +0530272 case BACKUP_BOOT_DEVICE_DFU:
273 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
274 return BOOT_DEVICE_USB;
275 return BOOT_DEVICE_DFU;
276
Keerthy05d670e2021-04-23 11:27:33 -0500277
278 case BACKUP_BOOT_DEVICE_ETHERNET:
279 return BOOT_DEVICE_ETHERNET;
280
281 case BACKUP_BOOT_DEVICE_MMC:
282 if (bkup_bootmode_cfg)
283 return BOOT_DEVICE_MMC2;
284 return BOOT_DEVICE_MMC1;
285
286 case BACKUP_BOOT_DEVICE_SPI:
287 return BOOT_DEVICE_SPI;
288
289 case BACKUP_BOOT_DEVICE_I2C:
290 return BOOT_DEVICE_I2C;
291 };
292
293 return BOOT_DEVICE_RAM;
294}
295
296static u32 __get_primary_bootmedia(u32 main_devstat)
297{
298 u32 bootmode = (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
299 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
300 u32 bootmode_cfg =
301 (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
302 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
303
304 switch (bootmode) {
305 case BOOT_DEVICE_OSPI:
306 fallthrough;
307 case BOOT_DEVICE_QSPI:
308 fallthrough;
309 case BOOT_DEVICE_XSPI:
310 fallthrough;
311 case BOOT_DEVICE_SPI:
312 return BOOT_DEVICE_SPI;
313
314 case BOOT_DEVICE_ETHERNET_RGMII:
315 fallthrough;
316 case BOOT_DEVICE_ETHERNET_RMII:
317 return BOOT_DEVICE_ETHERNET;
318
319 case BOOT_DEVICE_EMMC:
320 return BOOT_DEVICE_MMC1;
321
322 case BOOT_DEVICE_MMC:
323 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
324 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
325 return BOOT_DEVICE_MMC2;
326 return BOOT_DEVICE_MMC1;
327
Aswath Govindraju8a05c9a2021-06-04 22:00:32 +0530328 case BOOT_DEVICE_DFU:
329 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
330 MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
331 return BOOT_DEVICE_USB;
332 return BOOT_DEVICE_DFU;
333
Keerthy05d670e2021-04-23 11:27:33 -0500334 case BOOT_DEVICE_NOBOOT:
335 return BOOT_DEVICE_RAM;
336 }
337
338 return bootmode;
339}
340
341u32 spl_boot_device(void)
342{
343 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
344
345 if (bootindex == K3_PRIMARY_BOOTMODE)
346 return __get_primary_bootmedia(devstat);
347 else
348 return __get_backup_bootmedia(devstat);
349}
Suman Anna320c0202021-04-23 11:27:38 -0500350
351#if defined(CONFIG_SYS_K3_SPL_ATF)
352
353#define AM64X_DEV_RTI8 127
354#define AM64X_DEV_RTI9 128
355#define AM64X_DEV_R5FSS0_CORE0 121
356#define AM64X_DEV_R5FSS0_CORE1 122
357
358void release_resources_for_core_shutdown(void)
359{
360 struct ti_sci_handle *ti_sci = get_ti_sci_handle();
361 struct ti_sci_dev_ops *dev_ops = &ti_sci->ops.dev_ops;
362 struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops;
363 int ret;
364 u32 i;
365
366 const u32 put_device_ids[] = {
367 AM64X_DEV_RTI9,
368 AM64X_DEV_RTI8,
369 };
370
371 /* Iterate through list of devices to put (shutdown) */
372 for (i = 0; i < ARRAY_SIZE(put_device_ids); i++) {
373 u32 id = put_device_ids[i];
374
375 ret = dev_ops->put_device(ti_sci, id);
376 if (ret)
377 panic("Failed to put device %u (%d)\n", id, ret);
378 }
379
380 const u32 put_core_ids[] = {
381 AM64X_DEV_R5FSS0_CORE1,
382 AM64X_DEV_R5FSS0_CORE0, /* Handle CPU0 after CPU1 */
383 };
384
385 /* Iterate through list of cores to put (shutdown) */
386 for (i = 0; i < ARRAY_SIZE(put_core_ids); i++) {
387 u32 id = put_core_ids[i];
388
389 /*
390 * Queue up the core shutdown request. Note that this call
391 * needs to be followed up by an actual invocation of an WFE
392 * or WFI CPU instruction.
393 */
394 ret = proc_ops->proc_shutdown_no_wait(ti_sci, id);
395 if (ret)
396 panic("Failed sending core %u shutdown message (%d)\n",
397 id, ret);
398 }
399}
400#endif