blob: b16de9c9f084ec73c4e94929cce59879c1a38177 [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)
Hari Nagallad383c5e2022-03-09 14:42:30 -060027#define MCU_CTRL_MMR0_BASE 0x04500000
28#define CTRLMMR_MCU_RST_CTRL 0x04518170
Dave Gerlach96571ec2021-04-23 11:27:32 -050029
Dave Gerlacheaef1292021-04-23 11:27:34 -050030static void ctrl_mmr_unlock(void)
31{
32 /* Unlock all PADCFG_MMR1 module registers */
33 mmr_unlock(PADCFG_MMR1_BASE, 1);
34
Hari Nagallad383c5e2022-03-09 14:42:30 -060035 /* Unlock all MCU_CTRL_MMR0 module registers */
36 mmr_unlock(MCU_CTRL_MMR0_BASE, 0);
37 mmr_unlock(MCU_CTRL_MMR0_BASE, 1);
38 mmr_unlock(MCU_CTRL_MMR0_BASE, 2);
39 mmr_unlock(MCU_CTRL_MMR0_BASE, 3);
40 mmr_unlock(MCU_CTRL_MMR0_BASE, 4);
41 mmr_unlock(MCU_CTRL_MMR0_BASE, 6);
42
Dave Gerlacheaef1292021-04-23 11:27:34 -050043 /* Unlock all CTRL_MMR0 module registers */
44 mmr_unlock(CTRL_MMR0_BASE, 0);
45 mmr_unlock(CTRL_MMR0_BASE, 1);
46 mmr_unlock(CTRL_MMR0_BASE, 2);
47 mmr_unlock(CTRL_MMR0_BASE, 3);
48 mmr_unlock(CTRL_MMR0_BASE, 5);
49 mmr_unlock(CTRL_MMR0_BASE, 6);
Christian Gmeiner33a9d8e2022-05-12 08:21:01 +020050
51 /* Unlock all MCU_PADCFG_MMR1 module registers */
52 mmr_unlock(MCU_PADCFG_MMR1_BASE, 1);
Dave Gerlacheaef1292021-04-23 11:27:34 -050053}
54
Dave Gerlachb27a9f22021-04-23 11:27:35 -050055/*
56 * This uninitialized global variable would normal end up in the .bss section,
57 * but the .bss is cleared between writing and reading this variable, so move
58 * it to the .data section.
59 */
60u32 bootindex __section(".data");
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020061static struct rom_extended_boot_data bootdata __section(".data");
Dave Gerlachb27a9f22021-04-23 11:27:35 -050062
63static void store_boot_info_from_rom(void)
64{
65 bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
66 memcpy(&bootdata, (uintptr_t *)ROM_ENTENDED_BOOT_DATA_INFO,
67 sizeof(struct rom_extended_boot_data));
68}
69
Dave Gerlacha89f7a42021-04-23 11:27:37 -050070#if defined(CONFIG_K3_LOAD_SYSFW) && CONFIG_IS_ENABLED(DM_MMC)
71void k3_mmc_stop_clock(void)
72{
73 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
74 struct mmc *mmc = find_mmc_device(0);
75
76 if (!mmc)
77 return;
78
79 mmc->saved_clock = mmc->clock;
80 mmc_set_clock(mmc, 0, true);
81 }
82}
83
84void k3_mmc_restart_clock(void)
85{
86 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
87 struct mmc *mmc = find_mmc_device(0);
88
89 if (!mmc)
90 return;
91
92 mmc_set_clock(mmc, mmc->saved_clock, false);
93 }
94}
95#else
96void k3_mmc_stop_clock(void) {}
97void k3_mmc_restart_clock(void) {}
98#endif
99
Lokesh Vutla17ad6e02021-05-06 16:44:52 +0530100#ifdef CONFIG_SPL_OF_LIST
101void do_dt_magic(void)
102{
103 int ret, rescan;
104
105 if (IS_ENABLED(CONFIG_TI_I2C_BOARD_DETECT))
106 do_board_detect();
107
108 /*
109 * Board detection has been done.
110 * Let us see if another dtb wouldn't be a better match
111 * for our board
112 */
113 if (IS_ENABLED(CONFIG_CPU_V7R)) {
114 ret = fdtdec_resetup(&rescan);
115 if (!ret && rescan) {
116 dm_uninit();
117 dm_init_and_scan(true);
118 }
119 }
120}
121#endif
122
Aswath Govindraju70bc5e32021-06-04 22:00:33 +0530123#if CONFIG_IS_ENABLED(USB_STORAGE)
124static int fixup_usb_boot(const void *fdt_blob)
125{
126 int ret = 0;
127
128 switch (spl_boot_device()) {
129 case BOOT_DEVICE_USB:
130 /*
131 * If the boot mode is host, fixup the dr_mode to host
132 * before cdns3 bind takes place
133 */
134 ret = fdt_find_and_setprop((void *)fdt_blob,
135 "/bus@f4000/cdns-usb@f900000/usb@f400000",
136 "dr_mode", "host", 5, 0);
137 if (ret)
138 printf("%s: fdt_find_and_setprop() failed:%d\n",
139 __func__, ret);
140 fallthrough;
141 default:
142 break;
143 }
144
145 return ret;
146}
147
148int fdtdec_board_setup(const void *fdt_blob)
149{
150 /* Can use the pointer from the function parameters */
151 return fixup_usb_boot(fdt_blob);
152}
153#endif
154
Hari Nagallad383c5e2022-03-09 14:42:30 -0600155#if defined(CONFIG_ESM_K3)
156static void enable_mcu_esm_reset(void)
157{
158 /* Set CTRLMMR_MCU_RST_CTRL:MCU_ESM_ERROR_RST_EN_Z to '0' (low active) */
159 u32 stat = readl(CTRLMMR_MCU_RST_CTRL);
160
161 stat &= 0xFFFDFFFF;
162 writel(stat, CTRLMMR_MCU_RST_CTRL);
163}
164#endif
165
Dave Gerlach96571ec2021-04-23 11:27:32 -0500166void board_init_f(ulong dummy)
167{
Hari Nagallad383c5e2022-03-09 14:42:30 -0600168#if defined(CONFIG_K3_LOAD_SYSFW) || defined(CONFIG_K3_AM64_DDRSS) || defined(CONFIG_ESM_K3)
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500169 struct udevice *dev;
170 int ret;
171#endif
172
Dave Gerlach96571ec2021-04-23 11:27:32 -0500173#if defined(CONFIG_CPU_V7R)
174 setup_k3_mpu_regions();
175#endif
176
Dave Gerlachb27a9f22021-04-23 11:27:35 -0500177 /*
178 * Cannot delay this further as there is a chance that
179 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
180 */
181 store_boot_info_from_rom();
182
Dave Gerlacheaef1292021-04-23 11:27:34 -0500183 ctrl_mmr_unlock();
184
Dave Gerlach96571ec2021-04-23 11:27:32 -0500185 /* Init DM early */
186 spl_early_init();
187
188 preloader_console_init();
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500189
Lokesh Vutla17ad6e02021-05-06 16:44:52 +0530190 do_dt_magic();
191
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500192#if defined(CONFIG_K3_LOAD_SYSFW)
193 /*
194 * Process pinctrl for serial3 a.k.a. MAIN UART1 module and continue
195 * regardless of the result of pinctrl. Do this without probing the
196 * device, but instead by searching the device that would request the
197 * given sequence number if probed. The UART will be used by the system
198 * firmware (SYSFW) image for various purposes and SYSFW depends on us
199 * to initialize its pin settings.
200 */
201 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev);
202 if (!ret)
203 pinctrl_select_state(dev, "default");
204
205 /*
206 * Load, start up, and configure system controller firmware.
207 * This will determine whether or not ROM has already loaded
208 * system firmware and if so, will only perform needed config
209 * and not attempt to load firmware again.
210 */
Dave Gerlacha89f7a42021-04-23 11:27:37 -0500211 k3_sysfw_loader(is_rom_loaded_sysfw(&bootdata), k3_mmc_stop_clock,
212 k3_mmc_restart_clock);
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500213#endif
214
215 /* Output System Firmware version info */
216 k3_sysfw_print_ver();
Dave Gerlachad38f512021-05-04 18:00:53 -0500217
Hari Nagallad383c5e2022-03-09 14:42:30 -0600218#if defined(CONFIG_ESM_K3)
219 /* Probe/configure ESM0 */
220 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@420000", &dev);
221 if (ret)
222 printf("esm main init failed: %d\n", ret);
223
224 /* Probe/configure MCUESM */
225 ret = uclass_get_device_by_name(UCLASS_MISC, "esm@4100000", &dev);
226 if (ret)
227 printf("esm mcu init failed: %d\n", ret);
228
229 enable_mcu_esm_reset();
230#endif
231
Dave Gerlachad38f512021-05-04 18:00:53 -0500232#if defined(CONFIG_K3_AM64_DDRSS)
233 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
234 if (ret)
235 panic("DRAM init failed: %d\n", ret);
236#endif
Vignesh Raghavendra82cc43e2021-12-24 12:55:32 +0530237 if (IS_ENABLED(CONFIG_SPL_ETH) && IS_ENABLED(CONFIG_TI_AM65_CPSW_NUSS) &&
238 spl_boot_device() == BOOT_DEVICE_ETHERNET) {
239 struct udevice *cpswdev;
240
241 if (uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(am65_cpsw_nuss), &cpswdev))
242 printf("Failed to probe am65_cpsw_nuss driver\n");
243 }
Dave Gerlach96571ec2021-04-23 11:27:32 -0500244}
Keerthy05d670e2021-04-23 11:27:33 -0500245
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100246u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Keerthy05d670e2021-04-23 11:27:33 -0500247{
248 switch (boot_device) {
249 case BOOT_DEVICE_MMC1:
250 return MMCSD_MODE_EMMCBOOT;
251
252 case BOOT_DEVICE_MMC2:
253 return MMCSD_MODE_FS;
254
255 default:
256 return MMCSD_MODE_RAW;
257 }
258}
259
260static u32 __get_backup_bootmedia(u32 main_devstat)
261{
262 u32 bkup_bootmode =
263 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
264 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
265 u32 bkup_bootmode_cfg =
266 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
267 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
268
269 switch (bkup_bootmode) {
270 case BACKUP_BOOT_DEVICE_UART:
271 return BOOT_DEVICE_UART;
272
Aswath Govindraju8a05c9a2021-06-04 22:00:32 +0530273 case BACKUP_BOOT_DEVICE_DFU:
274 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
275 return BOOT_DEVICE_USB;
276 return BOOT_DEVICE_DFU;
277
Keerthy05d670e2021-04-23 11:27:33 -0500278
279 case BACKUP_BOOT_DEVICE_ETHERNET:
280 return BOOT_DEVICE_ETHERNET;
281
282 case BACKUP_BOOT_DEVICE_MMC:
283 if (bkup_bootmode_cfg)
284 return BOOT_DEVICE_MMC2;
285 return BOOT_DEVICE_MMC1;
286
287 case BACKUP_BOOT_DEVICE_SPI:
288 return BOOT_DEVICE_SPI;
289
290 case BACKUP_BOOT_DEVICE_I2C:
291 return BOOT_DEVICE_I2C;
292 };
293
294 return BOOT_DEVICE_RAM;
295}
296
297static u32 __get_primary_bootmedia(u32 main_devstat)
298{
299 u32 bootmode = (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
300 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
301 u32 bootmode_cfg =
302 (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
303 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
304
305 switch (bootmode) {
306 case BOOT_DEVICE_OSPI:
307 fallthrough;
308 case BOOT_DEVICE_QSPI:
309 fallthrough;
310 case BOOT_DEVICE_XSPI:
311 fallthrough;
312 case BOOT_DEVICE_SPI:
313 return BOOT_DEVICE_SPI;
314
315 case BOOT_DEVICE_ETHERNET_RGMII:
316 fallthrough;
317 case BOOT_DEVICE_ETHERNET_RMII:
318 return BOOT_DEVICE_ETHERNET;
319
320 case BOOT_DEVICE_EMMC:
321 return BOOT_DEVICE_MMC1;
322
323 case BOOT_DEVICE_MMC:
324 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
325 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
326 return BOOT_DEVICE_MMC2;
327 return BOOT_DEVICE_MMC1;
328
Aswath Govindraju8a05c9a2021-06-04 22:00:32 +0530329 case BOOT_DEVICE_DFU:
330 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
331 MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
332 return BOOT_DEVICE_USB;
333 return BOOT_DEVICE_DFU;
334
Keerthy05d670e2021-04-23 11:27:33 -0500335 case BOOT_DEVICE_NOBOOT:
336 return BOOT_DEVICE_RAM;
337 }
338
339 return bootmode;
340}
341
342u32 spl_boot_device(void)
343{
344 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
345
346 if (bootindex == K3_PRIMARY_BOOTMODE)
347 return __get_primary_bootmedia(devstat);
348 else
349 return __get_backup_bootmedia(devstat);
350}
Dave Gerlach96571ec2021-04-23 11:27:32 -0500351#endif
Suman Anna320c0202021-04-23 11:27:38 -0500352
353#if defined(CONFIG_SYS_K3_SPL_ATF)
354
355#define AM64X_DEV_RTI8 127
356#define AM64X_DEV_RTI9 128
357#define AM64X_DEV_R5FSS0_CORE0 121
358#define AM64X_DEV_R5FSS0_CORE1 122
359
360void release_resources_for_core_shutdown(void)
361{
362 struct ti_sci_handle *ti_sci = get_ti_sci_handle();
363 struct ti_sci_dev_ops *dev_ops = &ti_sci->ops.dev_ops;
364 struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops;
365 int ret;
366 u32 i;
367
368 const u32 put_device_ids[] = {
369 AM64X_DEV_RTI9,
370 AM64X_DEV_RTI8,
371 };
372
373 /* Iterate through list of devices to put (shutdown) */
374 for (i = 0; i < ARRAY_SIZE(put_device_ids); i++) {
375 u32 id = put_device_ids[i];
376
377 ret = dev_ops->put_device(ti_sci, id);
378 if (ret)
379 panic("Failed to put device %u (%d)\n", id, ret);
380 }
381
382 const u32 put_core_ids[] = {
383 AM64X_DEV_R5FSS0_CORE1,
384 AM64X_DEV_R5FSS0_CORE0, /* Handle CPU0 after CPU1 */
385 };
386
387 /* Iterate through list of cores to put (shutdown) */
388 for (i = 0; i < ARRAY_SIZE(put_core_ids); i++) {
389 u32 id = put_core_ids[i];
390
391 /*
392 * Queue up the core shutdown request. Note that this call
393 * needs to be followed up by an actual invocation of an WFE
394 * or WFI CPU instruction.
395 */
396 ret = proc_ops->proc_shutdown_no_wait(ti_sci, id);
397 if (ret)
398 panic("Failed sending core %u shutdown message (%d)\n",
399 id, ret);
400 }
401}
402#endif