blob: 958fa05af4d0c2f49942636ebab05625ebec2c85 [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
Dave Gerlach96571ec2021-04-23 11:27:32 -0500202}
Keerthy05d670e2021-04-23 11:27:33 -0500203
Aswath Govindrajudc32ddf2021-07-26 20:58:02 +0530204u32 spl_mmc_boot_mode(const u32 boot_device)
Keerthy05d670e2021-04-23 11:27:33 -0500205{
206 switch (boot_device) {
207 case BOOT_DEVICE_MMC1:
208 return MMCSD_MODE_EMMCBOOT;
209
210 case BOOT_DEVICE_MMC2:
211 return MMCSD_MODE_FS;
212
213 default:
214 return MMCSD_MODE_RAW;
215 }
216}
217
218static u32 __get_backup_bootmedia(u32 main_devstat)
219{
220 u32 bkup_bootmode =
221 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
222 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
223 u32 bkup_bootmode_cfg =
224 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
225 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
226
227 switch (bkup_bootmode) {
228 case BACKUP_BOOT_DEVICE_UART:
229 return BOOT_DEVICE_UART;
230
Aswath Govindraju8a05c9a2021-06-04 22:00:32 +0530231 case BACKUP_BOOT_DEVICE_DFU:
232 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
233 return BOOT_DEVICE_USB;
234 return BOOT_DEVICE_DFU;
235
Keerthy05d670e2021-04-23 11:27:33 -0500236
237 case BACKUP_BOOT_DEVICE_ETHERNET:
238 return BOOT_DEVICE_ETHERNET;
239
240 case BACKUP_BOOT_DEVICE_MMC:
241 if (bkup_bootmode_cfg)
242 return BOOT_DEVICE_MMC2;
243 return BOOT_DEVICE_MMC1;
244
245 case BACKUP_BOOT_DEVICE_SPI:
246 return BOOT_DEVICE_SPI;
247
248 case BACKUP_BOOT_DEVICE_I2C:
249 return BOOT_DEVICE_I2C;
250 };
251
252 return BOOT_DEVICE_RAM;
253}
254
255static u32 __get_primary_bootmedia(u32 main_devstat)
256{
257 u32 bootmode = (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
258 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
259 u32 bootmode_cfg =
260 (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
261 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
262
263 switch (bootmode) {
264 case BOOT_DEVICE_OSPI:
265 fallthrough;
266 case BOOT_DEVICE_QSPI:
267 fallthrough;
268 case BOOT_DEVICE_XSPI:
269 fallthrough;
270 case BOOT_DEVICE_SPI:
271 return BOOT_DEVICE_SPI;
272
273 case BOOT_DEVICE_ETHERNET_RGMII:
274 fallthrough;
275 case BOOT_DEVICE_ETHERNET_RMII:
276 return BOOT_DEVICE_ETHERNET;
277
278 case BOOT_DEVICE_EMMC:
279 return BOOT_DEVICE_MMC1;
280
281 case BOOT_DEVICE_MMC:
282 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
283 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
284 return BOOT_DEVICE_MMC2;
285 return BOOT_DEVICE_MMC1;
286
Aswath Govindraju8a05c9a2021-06-04 22:00:32 +0530287 case BOOT_DEVICE_DFU:
288 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
289 MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
290 return BOOT_DEVICE_USB;
291 return BOOT_DEVICE_DFU;
292
Keerthy05d670e2021-04-23 11:27:33 -0500293 case BOOT_DEVICE_NOBOOT:
294 return BOOT_DEVICE_RAM;
295 }
296
297 return bootmode;
298}
299
300u32 spl_boot_device(void)
301{
302 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
303
304 if (bootindex == K3_PRIMARY_BOOTMODE)
305 return __get_primary_bootmedia(devstat);
306 else
307 return __get_backup_bootmedia(devstat);
308}
Dave Gerlach96571ec2021-04-23 11:27:32 -0500309#endif
Suman Anna320c0202021-04-23 11:27:38 -0500310
311#if defined(CONFIG_SYS_K3_SPL_ATF)
312
313#define AM64X_DEV_RTI8 127
314#define AM64X_DEV_RTI9 128
315#define AM64X_DEV_R5FSS0_CORE0 121
316#define AM64X_DEV_R5FSS0_CORE1 122
317
318void release_resources_for_core_shutdown(void)
319{
320 struct ti_sci_handle *ti_sci = get_ti_sci_handle();
321 struct ti_sci_dev_ops *dev_ops = &ti_sci->ops.dev_ops;
322 struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops;
323 int ret;
324 u32 i;
325
326 const u32 put_device_ids[] = {
327 AM64X_DEV_RTI9,
328 AM64X_DEV_RTI8,
329 };
330
331 /* Iterate through list of devices to put (shutdown) */
332 for (i = 0; i < ARRAY_SIZE(put_device_ids); i++) {
333 u32 id = put_device_ids[i];
334
335 ret = dev_ops->put_device(ti_sci, id);
336 if (ret)
337 panic("Failed to put device %u (%d)\n", id, ret);
338 }
339
340 const u32 put_core_ids[] = {
341 AM64X_DEV_R5FSS0_CORE1,
342 AM64X_DEV_R5FSS0_CORE0, /* Handle CPU0 after CPU1 */
343 };
344
345 /* Iterate through list of cores to put (shutdown) */
346 for (i = 0; i < ARRAY_SIZE(put_core_ids); i++) {
347 u32 id = put_core_ids[i];
348
349 /*
350 * Queue up the core shutdown request. Note that this call
351 * needs to be followed up by an actual invocation of an WFE
352 * or WFI CPU instruction.
353 */
354 ret = proc_ops->proc_shutdown_no_wait(ti_sci, id);
355 if (ret)
356 panic("Failed sending core %u shutdown message (%d)\n",
357 id, ret);
358 }
359}
360#endif