blob: 543dea02bca95d2811607b461515165a493e09a0 [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
26#if defined(CONFIG_SPL_BUILD)
27
Dave Gerlacheaef1292021-04-23 11:27:34 -050028static void ctrl_mmr_unlock(void)
29{
30 /* Unlock all PADCFG_MMR1 module registers */
31 mmr_unlock(PADCFG_MMR1_BASE, 1);
32
33 /* Unlock all CTRL_MMR0 module registers */
34 mmr_unlock(CTRL_MMR0_BASE, 0);
35 mmr_unlock(CTRL_MMR0_BASE, 1);
36 mmr_unlock(CTRL_MMR0_BASE, 2);
37 mmr_unlock(CTRL_MMR0_BASE, 3);
38 mmr_unlock(CTRL_MMR0_BASE, 5);
39 mmr_unlock(CTRL_MMR0_BASE, 6);
Michael Liebertf19e3a62021-12-15 16:14:28 +010040
41 /* Unlock all MCU_PADCFG_MMR1 module registers */
42 mmr_unlock(MCU_PADCFG_MMR1_BASE, 1);
Dave Gerlacheaef1292021-04-23 11:27:34 -050043}
44
Dave Gerlachb27a9f22021-04-23 11:27:35 -050045/*
46 * This uninitialized global variable would normal end up in the .bss section,
47 * but the .bss is cleared between writing and reading this variable, so move
48 * it to the .data section.
49 */
50u32 bootindex __section(".data");
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020051static struct rom_extended_boot_data bootdata __section(".data");
Dave Gerlachb27a9f22021-04-23 11:27:35 -050052
53static void store_boot_info_from_rom(void)
54{
55 bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
56 memcpy(&bootdata, (uintptr_t *)ROM_ENTENDED_BOOT_DATA_INFO,
57 sizeof(struct rom_extended_boot_data));
58}
59
Dave Gerlacha89f7a42021-04-23 11:27:37 -050060#if defined(CONFIG_K3_LOAD_SYSFW) && CONFIG_IS_ENABLED(DM_MMC)
61void k3_mmc_stop_clock(void)
62{
63 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
64 struct mmc *mmc = find_mmc_device(0);
65
66 if (!mmc)
67 return;
68
69 mmc->saved_clock = mmc->clock;
70 mmc_set_clock(mmc, 0, true);
71 }
72}
73
74void k3_mmc_restart_clock(void)
75{
76 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
77 struct mmc *mmc = find_mmc_device(0);
78
79 if (!mmc)
80 return;
81
82 mmc_set_clock(mmc, mmc->saved_clock, false);
83 }
84}
85#else
86void k3_mmc_stop_clock(void) {}
87void k3_mmc_restart_clock(void) {}
88#endif
89
Lokesh Vutla17ad6e02021-05-06 16:44:52 +053090#ifdef CONFIG_SPL_OF_LIST
91void do_dt_magic(void)
92{
93 int ret, rescan;
94
95 if (IS_ENABLED(CONFIG_TI_I2C_BOARD_DETECT))
96 do_board_detect();
97
98 /*
99 * Board detection has been done.
100 * Let us see if another dtb wouldn't be a better match
101 * for our board
102 */
103 if (IS_ENABLED(CONFIG_CPU_V7R)) {
104 ret = fdtdec_resetup(&rescan);
105 if (!ret && rescan) {
106 dm_uninit();
107 dm_init_and_scan(true);
108 }
109 }
110}
111#endif
112
Aswath Govindraju70bc5e32021-06-04 22:00:33 +0530113#if CONFIG_IS_ENABLED(USB_STORAGE)
114static int fixup_usb_boot(const void *fdt_blob)
115{
116 int ret = 0;
117
118 switch (spl_boot_device()) {
119 case BOOT_DEVICE_USB:
120 /*
121 * If the boot mode is host, fixup the dr_mode to host
122 * before cdns3 bind takes place
123 */
124 ret = fdt_find_and_setprop((void *)fdt_blob,
125 "/bus@f4000/cdns-usb@f900000/usb@f400000",
126 "dr_mode", "host", 5, 0);
127 if (ret)
128 printf("%s: fdt_find_and_setprop() failed:%d\n",
129 __func__, ret);
130 fallthrough;
131 default:
132 break;
133 }
134
135 return ret;
136}
137
138int fdtdec_board_setup(const void *fdt_blob)
139{
140 /* Can use the pointer from the function parameters */
141 return fixup_usb_boot(fdt_blob);
142}
143#endif
144
Dave Gerlach96571ec2021-04-23 11:27:32 -0500145void board_init_f(ulong dummy)
146{
Gowtham Tammana411c6092021-06-24 12:16:14 -0500147#if defined(CONFIG_K3_LOAD_SYSFW) || defined(CONFIG_K3_AM64_DDRSS)
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500148 struct udevice *dev;
149 int ret;
150#endif
151
Dave Gerlach96571ec2021-04-23 11:27:32 -0500152#if defined(CONFIG_CPU_V7R)
153 setup_k3_mpu_regions();
154#endif
155
Dave Gerlachb27a9f22021-04-23 11:27:35 -0500156 /*
157 * Cannot delay this further as there is a chance that
158 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
159 */
160 store_boot_info_from_rom();
161
Dave Gerlacheaef1292021-04-23 11:27:34 -0500162 ctrl_mmr_unlock();
163
Dave Gerlach96571ec2021-04-23 11:27:32 -0500164 /* Init DM early */
165 spl_early_init();
166
167 preloader_console_init();
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500168
Lokesh Vutla17ad6e02021-05-06 16:44:52 +0530169 do_dt_magic();
170
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500171#if defined(CONFIG_K3_LOAD_SYSFW)
172 /*
173 * Process pinctrl for serial3 a.k.a. MAIN UART1 module and continue
174 * regardless of the result of pinctrl. Do this without probing the
175 * device, but instead by searching the device that would request the
176 * given sequence number if probed. The UART will be used by the system
177 * firmware (SYSFW) image for various purposes and SYSFW depends on us
178 * to initialize its pin settings.
179 */
180 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev);
181 if (!ret)
182 pinctrl_select_state(dev, "default");
183
184 /*
185 * Load, start up, and configure system controller firmware.
186 * This will determine whether or not ROM has already loaded
187 * system firmware and if so, will only perform needed config
188 * and not attempt to load firmware again.
189 */
Dave Gerlacha89f7a42021-04-23 11:27:37 -0500190 k3_sysfw_loader(is_rom_loaded_sysfw(&bootdata), k3_mmc_stop_clock,
191 k3_mmc_restart_clock);
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500192#endif
193
194 /* Output System Firmware version info */
195 k3_sysfw_print_ver();
Dave Gerlachad38f512021-05-04 18:00:53 -0500196
197#if defined(CONFIG_K3_AM64_DDRSS)
198 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
199 if (ret)
200 panic("DRAM init failed: %d\n", ret);
201#endif
Vignesh Raghavendra82cc43e2021-12-24 12:55:32 +0530202 if (IS_ENABLED(CONFIG_SPL_ETH) && IS_ENABLED(CONFIG_TI_AM65_CPSW_NUSS) &&
203 spl_boot_device() == BOOT_DEVICE_ETHERNET) {
204 struct udevice *cpswdev;
205
206 if (uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(am65_cpsw_nuss), &cpswdev))
207 printf("Failed to probe am65_cpsw_nuss driver\n");
208 }
Dave Gerlach96571ec2021-04-23 11:27:32 -0500209}
Keerthy05d670e2021-04-23 11:27:33 -0500210
Aswath Govindrajudc32ddf2021-07-26 20:58:02 +0530211u32 spl_mmc_boot_mode(const u32 boot_device)
Keerthy05d670e2021-04-23 11:27:33 -0500212{
213 switch (boot_device) {
214 case BOOT_DEVICE_MMC1:
215 return MMCSD_MODE_EMMCBOOT;
216
217 case BOOT_DEVICE_MMC2:
218 return MMCSD_MODE_FS;
219
220 default:
221 return MMCSD_MODE_RAW;
222 }
223}
224
225static u32 __get_backup_bootmedia(u32 main_devstat)
226{
227 u32 bkup_bootmode =
228 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
229 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
230 u32 bkup_bootmode_cfg =
231 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
232 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
233
234 switch (bkup_bootmode) {
235 case BACKUP_BOOT_DEVICE_UART:
236 return BOOT_DEVICE_UART;
237
Aswath Govindraju8a05c9a2021-06-04 22:00:32 +0530238 case BACKUP_BOOT_DEVICE_DFU:
239 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
240 return BOOT_DEVICE_USB;
241 return BOOT_DEVICE_DFU;
242
Keerthy05d670e2021-04-23 11:27:33 -0500243
244 case BACKUP_BOOT_DEVICE_ETHERNET:
245 return BOOT_DEVICE_ETHERNET;
246
247 case BACKUP_BOOT_DEVICE_MMC:
248 if (bkup_bootmode_cfg)
249 return BOOT_DEVICE_MMC2;
250 return BOOT_DEVICE_MMC1;
251
252 case BACKUP_BOOT_DEVICE_SPI:
253 return BOOT_DEVICE_SPI;
254
255 case BACKUP_BOOT_DEVICE_I2C:
256 return BOOT_DEVICE_I2C;
257 };
258
259 return BOOT_DEVICE_RAM;
260}
261
262static u32 __get_primary_bootmedia(u32 main_devstat)
263{
264 u32 bootmode = (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
265 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
266 u32 bootmode_cfg =
267 (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
268 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
269
270 switch (bootmode) {
271 case BOOT_DEVICE_OSPI:
272 fallthrough;
273 case BOOT_DEVICE_QSPI:
274 fallthrough;
275 case BOOT_DEVICE_XSPI:
276 fallthrough;
277 case BOOT_DEVICE_SPI:
278 return BOOT_DEVICE_SPI;
279
280 case BOOT_DEVICE_ETHERNET_RGMII:
281 fallthrough;
282 case BOOT_DEVICE_ETHERNET_RMII:
283 return BOOT_DEVICE_ETHERNET;
284
285 case BOOT_DEVICE_EMMC:
286 return BOOT_DEVICE_MMC1;
287
288 case BOOT_DEVICE_MMC:
289 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
290 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
291 return BOOT_DEVICE_MMC2;
292 return BOOT_DEVICE_MMC1;
293
Aswath Govindraju8a05c9a2021-06-04 22:00:32 +0530294 case BOOT_DEVICE_DFU:
295 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
296 MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
297 return BOOT_DEVICE_USB;
298 return BOOT_DEVICE_DFU;
299
Keerthy05d670e2021-04-23 11:27:33 -0500300 case BOOT_DEVICE_NOBOOT:
301 return BOOT_DEVICE_RAM;
302 }
303
304 return bootmode;
305}
306
307u32 spl_boot_device(void)
308{
309 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
310
311 if (bootindex == K3_PRIMARY_BOOTMODE)
312 return __get_primary_bootmedia(devstat);
313 else
314 return __get_backup_bootmedia(devstat);
315}
Dave Gerlach96571ec2021-04-23 11:27:32 -0500316#endif
Suman Anna320c0202021-04-23 11:27:38 -0500317
318#if defined(CONFIG_SYS_K3_SPL_ATF)
319
320#define AM64X_DEV_RTI8 127
321#define AM64X_DEV_RTI9 128
322#define AM64X_DEV_R5FSS0_CORE0 121
323#define AM64X_DEV_R5FSS0_CORE1 122
324
325void release_resources_for_core_shutdown(void)
326{
327 struct ti_sci_handle *ti_sci = get_ti_sci_handle();
328 struct ti_sci_dev_ops *dev_ops = &ti_sci->ops.dev_ops;
329 struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops;
330 int ret;
331 u32 i;
332
333 const u32 put_device_ids[] = {
334 AM64X_DEV_RTI9,
335 AM64X_DEV_RTI8,
336 };
337
338 /* Iterate through list of devices to put (shutdown) */
339 for (i = 0; i < ARRAY_SIZE(put_device_ids); i++) {
340 u32 id = put_device_ids[i];
341
342 ret = dev_ops->put_device(ti_sci, id);
343 if (ret)
344 panic("Failed to put device %u (%d)\n", id, ret);
345 }
346
347 const u32 put_core_ids[] = {
348 AM64X_DEV_R5FSS0_CORE1,
349 AM64X_DEV_R5FSS0_CORE0, /* Handle CPU0 after CPU1 */
350 };
351
352 /* Iterate through list of cores to put (shutdown) */
353 for (i = 0; i < ARRAY_SIZE(put_core_ids); i++) {
354 u32 id = put_core_ids[i];
355
356 /*
357 * Queue up the core shutdown request. Note that this call
358 * needs to be followed up by an actual invocation of an WFE
359 * or WFI CPU instruction.
360 */
361 ret = proc_ops->proc_shutdown_no_wait(ti_sci, id);
362 if (ret)
363 panic("Failed sending core %u shutdown message (%d)\n",
364 id, ret);
365 }
366}
367#endif