blob: 4235296774cfa0022800113791dc374fb37d272e [file] [log] [blame]
Lokesh Vutla32886442018-08-27 15:57:09 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
Andrew F. Davis2dbe3892020-01-10 14:35:20 -05003 * AM6: SoC specific initialization
Lokesh Vutla32886442018-08-27 15:57:09 +05304 *
Nishanth Menoneaa39c62023-11-01 15:56:03 -05005 * Copyright (C) 2017-2018 Texas Instruments Incorporated - https://www.ti.com/
Lokesh Vutla32886442018-08-27 15:57:09 +05306 * Lokesh Vutla <lokeshvutla@ti.com>
7 */
8
Faiz Abbas3385a862020-08-03 11:35:09 +05309#include <fdt_support.h>
Simon Glass97589732020-05-10 11:40:02 -060010#include <init.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Lokesh Vutlac7bfb852018-08-27 15:57:11 +053012#include <asm/io.h>
Lokesh Vutla32886442018-08-27 15:57:09 +053013#include <spl.h>
Lokesh Vutlac7bfb852018-08-27 15:57:11 +053014#include <asm/arch/hardware.h>
Andrew Davisf1799852023-04-06 11:38:16 -050015#include "sysfw-loader.h"
Lokesh Vutlac1e60e82018-11-02 19:51:03 +053016#include "common.h"
Lokesh Vutla2c69d5c2018-11-02 19:51:06 +053017#include <dm.h>
Andreas Dannenbergea91da12019-06-04 17:55:50 -050018#include <dm/uclass-internal.h>
19#include <dm/pinctrl.h>
Andreas Dannenberg31175f82019-06-07 19:24:42 +053020#include <linux/soc/ti/ti_sci_protocol.h>
Faiz Abbasfe1217e2020-08-03 11:35:06 +053021#include <log.h>
Faiz Abbas68393212020-02-26 13:44:36 +053022#include <mmc.h>
Faiz Abbasfe1217e2020-08-03 11:35:06 +053023#include <stdlib.h>
Lokesh Vutla32886442018-08-27 15:57:09 +053024
Faiz Abbas3385a862020-08-03 11:35:09 +053025DECLARE_GLOBAL_DATA_PTR;
26
Andrew F. Davisf0bcb662020-01-10 14:35:21 -050027#ifdef CONFIG_K3_LOAD_SYSFW
Andrew F. Davisf0bcb662020-01-10 14:35:21 -050028struct fwl_data main_cbass_fwls[] = {
29 { "MMCSD1_CFG", 2057, 1 },
30 { "MMCSD0_CFG", 2058, 1 },
31 { "USB3SS0_SLV0", 2176, 2 },
32 { "PCIE0_SLV", 2336, 8 },
33 { "PCIE1_SLV", 2337, 8 },
34 { "PCIE0_CFG", 2688, 1 },
35 { "PCIE1_CFG", 2689, 1 },
36}, mcu_cbass_fwls[] = {
37 { "MCU_ARMSS0_CORE0_SLV", 1024, 1 },
38 { "MCU_ARMSS0_CORE1_SLV", 1028, 1 },
39 { "MCU_FSS0_S1", 1033, 8 },
40 { "MCU_FSS0_S0", 1036, 8 },
41 { "MCU_CPSW0", 1220, 1 },
42};
43#endif
Andrew F. Davisf0bcb662020-01-10 14:35:21 -050044
Andreas Dannenberg1c855c12018-08-27 15:57:12 +053045static void ctrl_mmr_unlock(void)
46{
47 /* Unlock all WKUP_CTRL_MMR0 module registers */
48 mmr_unlock(WKUP_CTRL_MMR0_BASE, 0);
49 mmr_unlock(WKUP_CTRL_MMR0_BASE, 1);
50 mmr_unlock(WKUP_CTRL_MMR0_BASE, 2);
51 mmr_unlock(WKUP_CTRL_MMR0_BASE, 3);
52 mmr_unlock(WKUP_CTRL_MMR0_BASE, 6);
53 mmr_unlock(WKUP_CTRL_MMR0_BASE, 7);
54
55 /* Unlock all MCU_CTRL_MMR0 module registers */
56 mmr_unlock(MCU_CTRL_MMR0_BASE, 0);
57 mmr_unlock(MCU_CTRL_MMR0_BASE, 1);
58 mmr_unlock(MCU_CTRL_MMR0_BASE, 2);
59 mmr_unlock(MCU_CTRL_MMR0_BASE, 6);
60
61 /* Unlock all CTRL_MMR0 module registers */
62 mmr_unlock(CTRL_MMR0_BASE, 0);
63 mmr_unlock(CTRL_MMR0_BASE, 1);
64 mmr_unlock(CTRL_MMR0_BASE, 2);
65 mmr_unlock(CTRL_MMR0_BASE, 3);
66 mmr_unlock(CTRL_MMR0_BASE, 6);
67 mmr_unlock(CTRL_MMR0_BASE, 7);
68}
69
Andrew F. Davis9ffea342019-04-12 12:54:42 -040070/*
71 * This uninitialized global variable would normal end up in the .bss section,
72 * but the .bss is cleared between writing and reading this variable, so move
73 * it to the .data section.
74 */
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020075u32 bootindex __section(".data");
Andrew F. Davis9ffea342019-04-12 12:54:42 -040076
Lokesh Vutlac7bfb852018-08-27 15:57:11 +053077static void store_boot_index_from_rom(void)
78{
Andrew F. Davis9ffea342019-04-12 12:54:42 -040079 bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
Lokesh Vutlac7bfb852018-08-27 15:57:11 +053080}
81
Faiz Abbasb4372bf2020-08-03 11:35:08 +053082#if defined(CONFIG_K3_LOAD_SYSFW) && CONFIG_IS_ENABLED(DM_MMC)
Faiz Abbas68393212020-02-26 13:44:36 +053083void k3_mmc_stop_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->saved_clock = mmc->clock;
92 mmc_set_clock(mmc, 0, true);
93 }
94}
95
96void k3_mmc_restart_clock(void)
97{
98 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
99 struct mmc *mmc = find_mmc_device(0);
100
101 if (!mmc)
102 return;
103
104 mmc_set_clock(mmc, mmc->saved_clock, false);
105 }
106}
Faiz Abbasb4372bf2020-08-03 11:35:08 +0530107#else
108void k3_mmc_stop_clock(void) {}
109void k3_mmc_restart_clock(void) {}
Faiz Abbas68393212020-02-26 13:44:36 +0530110#endif
Faiz Abbas3385a862020-08-03 11:35:09 +0530111#if CONFIG_IS_ENABLED(DFU) || CONFIG_IS_ENABLED(USB_STORAGE)
112#define CTRLMMR_SERDES0_CTRL 0x00104080
113#define PCIE_LANE0 0x1
114static int fixup_usb_boot(void)
115{
116 int ret;
Faiz Abbas68393212020-02-26 13:44:36 +0530117
Faiz Abbas3385a862020-08-03 11:35:09 +0530118 switch (spl_boot_device()) {
119 case BOOT_DEVICE_USB:
120 /*
121 * If bootmode is Host bootmode, fixup the dr_mode to host
122 * before the dwc3 bind takes place
123 */
124 ret = fdt_find_and_setprop((void *)gd->fdt_blob,
Aswath Govindraju271d76e2022-05-18 16:49:13 +0530125 "/bus@100000/dwc3@4000000/usb@10000",
126 "dr_mode", "host", 5, 0);
Faiz Abbas3385a862020-08-03 11:35:09 +0530127 if (ret)
128 printf("%s: fdt_find_and_setprop() failed:%d\n", __func__,
129 ret);
130 fallthrough;
131 case BOOT_DEVICE_DFU:
132 /*
133 * The serdes mux between PCIe and USB3 needs to be set to PCIe for
134 * accessing the interface at USB 2.0
135 */
136 writel(PCIE_LANE0, CTRLMMR_SERDES0_CTRL);
137 default:
138 break;
139 }
140
141 return 0;
142}
143
144int fdtdec_board_setup(const void *fdt_blob)
145{
146 return fixup_usb_boot();
147}
148#endif
Roger Quadros768e6672021-09-08 15:28:59 -0500149
150static void setup_am654_navss_northbridge(void)
151{
152 /*
153 * NB0 is bridge to SRAM and NB1 is bridge to DDR.
154 * To ensure that SRAM transfers are not stalled due to
155 * delays during DDR refreshes, SRAM traffic should be higher
156 * priority (threadmap=2) than DDR traffic (threadmap=0).
157 */
158 writel(0x2, NAVSS0_NBSS_NB0_CFG_BASE + NAVSS_NBSS_THREADMAP);
159 writel(0x0, NAVSS0_NBSS_NB1_CFG_BASE + NAVSS_NBSS_THREADMAP);
160}
161
Lokesh Vutla32886442018-08-27 15:57:09 +0530162void board_init_f(ulong dummy)
163{
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500164#if defined(CONFIG_K3_LOAD_SYSFW) || defined(CONFIG_K3_AM654_DDRSS)
Lokesh Vutla2c69d5c2018-11-02 19:51:06 +0530165 struct udevice *dev;
Faiz Abbasfe1217e2020-08-03 11:35:06 +0530166 size_t pool_size;
167 void *pool_addr;
Lokesh Vutla2c69d5c2018-11-02 19:51:06 +0530168 int ret;
169#endif
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530170 /*
171 * Cannot delay this further as there is a chance that
172 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
173 */
174 store_boot_index_from_rom();
175
Andreas Dannenberg1c855c12018-08-27 15:57:12 +0530176 /* Make all control module registers accessible */
177 ctrl_mmr_unlock();
178
Roger Quadros768e6672021-09-08 15:28:59 -0500179 setup_am654_navss_northbridge();
180
Lokesh Vutlac1e60e82018-11-02 19:51:03 +0530181#ifdef CONFIG_CPU_V7R
Lokesh Vutla5fbd6fe2019-12-31 15:49:55 +0530182 disable_linefill_optimization();
Lokesh Vutlac1e60e82018-11-02 19:51:03 +0530183 setup_k3_mpu_regions();
184#endif
185
Lokesh Vutla32886442018-08-27 15:57:09 +0530186 /* Init DM early in-order to invoke system controller */
187 spl_early_init();
188
Andreas Dannenbergb45d2cd2018-12-04 22:29:47 -0600189#ifdef CONFIG_K3_EARLY_CONS
190 /*
191 * Allow establishing an early console as required for example when
192 * doing a UART-based boot. Note that this console may not "survive"
193 * through a SYSFW PM-init step and will need a re-init in some way
194 * due to changing module clock frequencies.
195 */
196 early_console_init();
197#endif
198
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500199#ifdef CONFIG_K3_LOAD_SYSFW
200 /*
Faiz Abbasfe1217e2020-08-03 11:35:06 +0530201 * Initialize an early full malloc environment. Do so by allocating a
202 * new malloc area inside the currently active pre-relocation "first"
203 * malloc pool of which we use all that's left.
204 */
205 pool_size = CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr;
206 pool_addr = malloc(pool_size);
207 if (!pool_addr)
208 panic("ERROR: Can't allocate full malloc pool!\n");
209
210 mem_malloc_init((ulong)pool_addr, (ulong)pool_size);
211 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
212 debug("%s: initialized an early full malloc pool at 0x%08lx of 0x%lx bytes\n",
213 __func__, (unsigned long)pool_addr, (unsigned long)pool_size);
214 /*
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500215 * Process pinctrl for the serial0 a.k.a. WKUP_UART0 module and continue
216 * regardless of the result of pinctrl. Do this without probing the
217 * device, but instead by searching the device that would request the
218 * given sequence number if probed. The UART will be used by the system
219 * firmware (SYSFW) image for various purposes and SYSFW depends on us
220 * to initialize its pin settings.
221 */
Simon Glass07e13382020-12-16 21:20:29 -0700222 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 0, &dev);
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500223 if (!ret)
224 pinctrl_select_state(dev, "default");
225
226 /*
Lokesh Vutla5fafe442020-03-10 16:50:58 +0530227 * Load, start up, and configure system controller firmware while
228 * also populating the SYSFW post-PM configuration callback hook.
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500229 */
Lokesh Vutla17951b72020-08-05 22:44:18 +0530230 k3_sysfw_loader(false, k3_mmc_stop_clock, k3_mmc_restart_clock);
Faiz Abbas68393212020-02-26 13:44:36 +0530231
232 /* Prepare console output */
233 preloader_console_init();
Andrew F. Davisf0bcb662020-01-10 14:35:21 -0500234
235 /* Disable ROM configured firewalls right after loading sysfw */
Andrew F. Davisf0bcb662020-01-10 14:35:21 -0500236 remove_fwl_configs(main_cbass_fwls, ARRAY_SIZE(main_cbass_fwls));
237 remove_fwl_configs(mcu_cbass_fwls, ARRAY_SIZE(mcu_cbass_fwls));
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500238#else
Lokesh Vutla32886442018-08-27 15:57:09 +0530239 /* Prepare console output */
240 preloader_console_init();
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500241#endif
Lokesh Vutla2c69d5c2018-11-02 19:51:06 +0530242
Lokesh Vutla5fafe442020-03-10 16:50:58 +0530243 /* Output System Firmware version info */
244 k3_sysfw_print_ver();
245
Andrew Davis2dde9a72023-04-06 11:38:17 -0500246 /* Perform board detection */
247 do_board_detect();
Andreas Dannenberg63f5c852019-06-04 18:08:26 -0500248
Keerthy2cd50972019-10-24 15:00:52 +0530249#if defined(CONFIG_CPU_V7R) && defined(CONFIG_K3_AVS0)
Simon Glass65130cd2020-12-28 20:34:56 -0700250 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(k3_avs),
Keerthy2cd50972019-10-24 15:00:52 +0530251 &dev);
252 if (ret)
253 printf("AVS init failed: %d\n", ret);
254#endif
255
Lokesh Vutla2c69d5c2018-11-02 19:51:06 +0530256#ifdef CONFIG_K3_AM654_DDRSS
257 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
Andreas Dannenberg7f6b62e2019-03-11 15:15:43 -0500258 if (ret)
259 panic("DRAM init failed: %d\n", ret);
Lokesh Vutla2c69d5c2018-11-02 19:51:06 +0530260#endif
Jan Kiszka7ce99f72020-05-18 07:57:22 +0200261 spl_enable_dcache();
Lokesh Vutla32886442018-08-27 15:57:09 +0530262}
263
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100264u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Andrew F. Davisc5161462018-10-03 10:03:23 -0500265{
266#if defined(CONFIG_SUPPORT_EMMC_BOOT)
267 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
Andrew F. Davisc5161462018-10-03 10:03:23 -0500268
269 u32 bootmode = (devstat & CTRLMMR_MAIN_DEVSTAT_BOOTMODE_MASK) >>
270 CTRLMMR_MAIN_DEVSTAT_BOOTMODE_SHIFT;
271
272 /* eMMC boot0 mode is only supported for primary boot */
273 if (bootindex == K3_PRIMARY_BOOTMODE &&
274 bootmode == BOOT_DEVICE_MMC1)
275 return MMCSD_MODE_EMMCBOOT;
276#endif
277
278 /* Everything else use filesystem if available */
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800279#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Andrew F. Davisc5161462018-10-03 10:03:23 -0500280 return MMCSD_MODE_FS;
281#else
282 return MMCSD_MODE_RAW;
283#endif
284}
285
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530286static u32 __get_backup_bootmedia(u32 devstat)
Lokesh Vutla32886442018-08-27 15:57:09 +0530287{
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530288 u32 bkup_boot = (devstat & CTRLMMR_MAIN_DEVSTAT_BKUP_BOOTMODE_MASK) >>
289 CTRLMMR_MAIN_DEVSTAT_BKUP_BOOTMODE_SHIFT;
290
291 switch (bkup_boot) {
292 case BACKUP_BOOT_DEVICE_USB:
293 return BOOT_DEVICE_USB;
294 case BACKUP_BOOT_DEVICE_UART:
295 return BOOT_DEVICE_UART;
296 case BACKUP_BOOT_DEVICE_ETHERNET:
297 return BOOT_DEVICE_ETHERNET;
298 case BACKUP_BOOT_DEVICE_MMC2:
Andrew F. Davisf515cf02018-10-03 10:03:22 -0500299 {
300 u32 port = (devstat & CTRLMMR_MAIN_DEVSTAT_BKUP_MMC_PORT_MASK) >>
301 CTRLMMR_MAIN_DEVSTAT_BKUP_MMC_PORT_SHIFT;
302 if (port == 0x0)
303 return BOOT_DEVICE_MMC1;
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530304 return BOOT_DEVICE_MMC2;
Andrew F. Davisf515cf02018-10-03 10:03:22 -0500305 }
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530306 case BACKUP_BOOT_DEVICE_SPI:
307 return BOOT_DEVICE_SPI;
308 case BACKUP_BOOT_DEVICE_HYPERFLASH:
309 return BOOT_DEVICE_HYPERFLASH;
310 case BACKUP_BOOT_DEVICE_I2C:
311 return BOOT_DEVICE_I2C;
312 };
313
Lokesh Vutla32886442018-08-27 15:57:09 +0530314 return BOOT_DEVICE_RAM;
315}
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530316
317static u32 __get_primary_bootmedia(u32 devstat)
318{
Andrew F. Davisf515cf02018-10-03 10:03:22 -0500319 u32 bootmode = (devstat & CTRLMMR_MAIN_DEVSTAT_BOOTMODE_MASK) >>
320 CTRLMMR_MAIN_DEVSTAT_BOOTMODE_SHIFT;
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530321
322 if (bootmode == BOOT_DEVICE_OSPI || bootmode == BOOT_DEVICE_QSPI)
323 bootmode = BOOT_DEVICE_SPI;
324
Andrew F. Davisf515cf02018-10-03 10:03:22 -0500325 if (bootmode == BOOT_DEVICE_MMC2) {
326 u32 port = (devstat & CTRLMMR_MAIN_DEVSTAT_MMC_PORT_MASK) >>
327 CTRLMMR_MAIN_DEVSTAT_MMC_PORT_SHIFT;
328 if (port == 0x0)
329 bootmode = BOOT_DEVICE_MMC1;
330 } else if (bootmode == BOOT_DEVICE_MMC1) {
331 u32 port = (devstat & CTRLMMR_MAIN_DEVSTAT_EMMC_PORT_MASK) >>
332 CTRLMMR_MAIN_DEVSTAT_EMMC_PORT_SHIFT;
333 if (port == 0x1)
334 bootmode = BOOT_DEVICE_MMC2;
Faiz Abbas0ae20ed2020-08-03 11:35:10 +0530335 } else if (bootmode == BOOT_DEVICE_DFU) {
336 u32 mode = (devstat & CTRLMMR_MAIN_DEVSTAT_USB_MODE_MASK) >>
337 CTRLMMR_MAIN_DEVSTAT_USB_MODE_SHIFT;
338 if (mode == 0x2)
339 bootmode = BOOT_DEVICE_USB;
Andrew F. Davisf515cf02018-10-03 10:03:22 -0500340 }
341
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530342 return bootmode;
343}
344
345u32 spl_boot_device(void)
346{
347 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530348
349 if (bootindex == K3_PRIMARY_BOOTMODE)
350 return __get_primary_bootmedia(devstat);
351 else
352 return __get_backup_bootmedia(devstat);
353}