blob: 86c1a349f1fc6d49d78a641a21f43a08bc44ae53 [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 *
5 * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
6 * Lokesh Vutla <lokeshvutla@ti.com>
7 */
8
9#include <common.h>
Faiz Abbas3385a862020-08-03 11:35:09 +053010#include <fdt_support.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Lokesh Vutlac7bfb852018-08-27 15:57:11 +053013#include <asm/io.h>
Lokesh Vutla32886442018-08-27 15:57:09 +053014#include <spl.h>
Lokesh Vutlac7bfb852018-08-27 15:57:11 +053015#include <asm/arch/hardware.h>
Andreas Dannenbergea91da12019-06-04 17:55:50 -050016#include <asm/arch/sysfw-loader.h>
Andreas Dannenberg63f5c852019-06-04 18:08:26 -050017#include <asm/arch/sys_proto.h>
Lokesh Vutlac1e60e82018-11-02 19:51:03 +053018#include "common.h"
Lokesh Vutla2c69d5c2018-11-02 19:51:06 +053019#include <dm.h>
Andreas Dannenbergea91da12019-06-04 17:55:50 -050020#include <dm/uclass-internal.h>
21#include <dm/pinctrl.h>
Andreas Dannenberg31175f82019-06-07 19:24:42 +053022#include <linux/soc/ti/ti_sci_protocol.h>
Faiz Abbasfe1217e2020-08-03 11:35:06 +053023#include <log.h>
Faiz Abbas68393212020-02-26 13:44:36 +053024#include <mmc.h>
Faiz Abbasfe1217e2020-08-03 11:35:06 +053025#include <stdlib.h>
Lokesh Vutla32886442018-08-27 15:57:09 +053026
Faiz Abbas3385a862020-08-03 11:35:09 +053027DECLARE_GLOBAL_DATA_PTR;
28
Lokesh Vutla32886442018-08-27 15:57:09 +053029#ifdef CONFIG_SPL_BUILD
Andrew F. Davisf0bcb662020-01-10 14:35:21 -050030#ifdef CONFIG_K3_LOAD_SYSFW
31#ifdef CONFIG_TI_SECURE_DEVICE
32struct fwl_data main_cbass_fwls[] = {
33 { "MMCSD1_CFG", 2057, 1 },
34 { "MMCSD0_CFG", 2058, 1 },
35 { "USB3SS0_SLV0", 2176, 2 },
36 { "PCIE0_SLV", 2336, 8 },
37 { "PCIE1_SLV", 2337, 8 },
38 { "PCIE0_CFG", 2688, 1 },
39 { "PCIE1_CFG", 2689, 1 },
40}, mcu_cbass_fwls[] = {
41 { "MCU_ARMSS0_CORE0_SLV", 1024, 1 },
42 { "MCU_ARMSS0_CORE1_SLV", 1028, 1 },
43 { "MCU_FSS0_S1", 1033, 8 },
44 { "MCU_FSS0_S0", 1036, 8 },
45 { "MCU_CPSW0", 1220, 1 },
46};
47#endif
48#endif
49
Andreas Dannenberg1c855c12018-08-27 15:57:12 +053050static void ctrl_mmr_unlock(void)
51{
52 /* Unlock all WKUP_CTRL_MMR0 module registers */
53 mmr_unlock(WKUP_CTRL_MMR0_BASE, 0);
54 mmr_unlock(WKUP_CTRL_MMR0_BASE, 1);
55 mmr_unlock(WKUP_CTRL_MMR0_BASE, 2);
56 mmr_unlock(WKUP_CTRL_MMR0_BASE, 3);
57 mmr_unlock(WKUP_CTRL_MMR0_BASE, 6);
58 mmr_unlock(WKUP_CTRL_MMR0_BASE, 7);
59
60 /* Unlock all MCU_CTRL_MMR0 module registers */
61 mmr_unlock(MCU_CTRL_MMR0_BASE, 0);
62 mmr_unlock(MCU_CTRL_MMR0_BASE, 1);
63 mmr_unlock(MCU_CTRL_MMR0_BASE, 2);
64 mmr_unlock(MCU_CTRL_MMR0_BASE, 6);
65
66 /* Unlock all CTRL_MMR0 module registers */
67 mmr_unlock(CTRL_MMR0_BASE, 0);
68 mmr_unlock(CTRL_MMR0_BASE, 1);
69 mmr_unlock(CTRL_MMR0_BASE, 2);
70 mmr_unlock(CTRL_MMR0_BASE, 3);
71 mmr_unlock(CTRL_MMR0_BASE, 6);
72 mmr_unlock(CTRL_MMR0_BASE, 7);
73}
74
Andrew F. Davis9ffea342019-04-12 12:54:42 -040075/*
76 * This uninitialized global variable would normal end up in the .bss section,
77 * but the .bss is cleared between writing and reading this variable, so move
78 * it to the .data section.
79 */
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020080u32 bootindex __section(".data");
Andrew F. Davis9ffea342019-04-12 12:54:42 -040081
Lokesh Vutlac7bfb852018-08-27 15:57:11 +053082static void store_boot_index_from_rom(void)
83{
Andrew F. Davis9ffea342019-04-12 12:54:42 -040084 bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
Lokesh Vutlac7bfb852018-08-27 15:57:11 +053085}
86
Faiz Abbasb4372bf2020-08-03 11:35:08 +053087#if defined(CONFIG_K3_LOAD_SYSFW) && CONFIG_IS_ENABLED(DM_MMC)
Faiz Abbas68393212020-02-26 13:44:36 +053088void k3_mmc_stop_clock(void)
89{
90 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
91 struct mmc *mmc = find_mmc_device(0);
92
93 if (!mmc)
94 return;
95
96 mmc->saved_clock = mmc->clock;
97 mmc_set_clock(mmc, 0, true);
98 }
99}
100
101void k3_mmc_restart_clock(void)
102{
103 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
104 struct mmc *mmc = find_mmc_device(0);
105
106 if (!mmc)
107 return;
108
109 mmc_set_clock(mmc, mmc->saved_clock, false);
110 }
111}
Faiz Abbasb4372bf2020-08-03 11:35:08 +0530112#else
113void k3_mmc_stop_clock(void) {}
114void k3_mmc_restart_clock(void) {}
Faiz Abbas68393212020-02-26 13:44:36 +0530115#endif
Faiz Abbas3385a862020-08-03 11:35:09 +0530116#if CONFIG_IS_ENABLED(DFU) || CONFIG_IS_ENABLED(USB_STORAGE)
117#define CTRLMMR_SERDES0_CTRL 0x00104080
118#define PCIE_LANE0 0x1
119static int fixup_usb_boot(void)
120{
121 int ret;
Faiz Abbas68393212020-02-26 13:44:36 +0530122
Faiz Abbas3385a862020-08-03 11:35:09 +0530123 switch (spl_boot_device()) {
124 case BOOT_DEVICE_USB:
125 /*
126 * If bootmode is Host bootmode, fixup the dr_mode to host
127 * before the dwc3 bind takes place
128 */
129 ret = fdt_find_and_setprop((void *)gd->fdt_blob,
130 "/interconnect@100000/dwc3@4000000/usb@10000",
131 "dr_mode", "host", 11, 0);
132 if (ret)
133 printf("%s: fdt_find_and_setprop() failed:%d\n", __func__,
134 ret);
135 fallthrough;
136 case BOOT_DEVICE_DFU:
137 /*
138 * The serdes mux between PCIe and USB3 needs to be set to PCIe for
139 * accessing the interface at USB 2.0
140 */
141 writel(PCIE_LANE0, CTRLMMR_SERDES0_CTRL);
142 default:
143 break;
144 }
145
146 return 0;
147}
148
149int fdtdec_board_setup(const void *fdt_blob)
150{
151 return fixup_usb_boot();
152}
153#endif
Roger Quadros768e6672021-09-08 15:28:59 -0500154
155static void setup_am654_navss_northbridge(void)
156{
157 /*
158 * NB0 is bridge to SRAM and NB1 is bridge to DDR.
159 * To ensure that SRAM transfers are not stalled due to
160 * delays during DDR refreshes, SRAM traffic should be higher
161 * priority (threadmap=2) than DDR traffic (threadmap=0).
162 */
163 writel(0x2, NAVSS0_NBSS_NB0_CFG_BASE + NAVSS_NBSS_THREADMAP);
164 writel(0x0, NAVSS0_NBSS_NB1_CFG_BASE + NAVSS_NBSS_THREADMAP);
165}
166
Lokesh Vutla32886442018-08-27 15:57:09 +0530167void board_init_f(ulong dummy)
168{
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500169#if defined(CONFIG_K3_LOAD_SYSFW) || defined(CONFIG_K3_AM654_DDRSS)
Lokesh Vutla2c69d5c2018-11-02 19:51:06 +0530170 struct udevice *dev;
Faiz Abbasfe1217e2020-08-03 11:35:06 +0530171 size_t pool_size;
172 void *pool_addr;
Lokesh Vutla2c69d5c2018-11-02 19:51:06 +0530173 int ret;
174#endif
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530175 /*
176 * Cannot delay this further as there is a chance that
177 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
178 */
179 store_boot_index_from_rom();
180
Andreas Dannenberg1c855c12018-08-27 15:57:12 +0530181 /* Make all control module registers accessible */
182 ctrl_mmr_unlock();
183
Roger Quadros768e6672021-09-08 15:28:59 -0500184 setup_am654_navss_northbridge();
185
Lokesh Vutlac1e60e82018-11-02 19:51:03 +0530186#ifdef CONFIG_CPU_V7R
Lokesh Vutla5fbd6fe2019-12-31 15:49:55 +0530187 disable_linefill_optimization();
Lokesh Vutlac1e60e82018-11-02 19:51:03 +0530188 setup_k3_mpu_regions();
189#endif
190
Lokesh Vutla32886442018-08-27 15:57:09 +0530191 /* Init DM early in-order to invoke system controller */
192 spl_early_init();
193
Andreas Dannenbergb45d2cd2018-12-04 22:29:47 -0600194#ifdef CONFIG_K3_EARLY_CONS
195 /*
196 * Allow establishing an early console as required for example when
197 * doing a UART-based boot. Note that this console may not "survive"
198 * through a SYSFW PM-init step and will need a re-init in some way
199 * due to changing module clock frequencies.
200 */
201 early_console_init();
202#endif
203
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500204#ifdef CONFIG_K3_LOAD_SYSFW
205 /*
Faiz Abbasfe1217e2020-08-03 11:35:06 +0530206 * Initialize an early full malloc environment. Do so by allocating a
207 * new malloc area inside the currently active pre-relocation "first"
208 * malloc pool of which we use all that's left.
209 */
210 pool_size = CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr;
211 pool_addr = malloc(pool_size);
212 if (!pool_addr)
213 panic("ERROR: Can't allocate full malloc pool!\n");
214
215 mem_malloc_init((ulong)pool_addr, (ulong)pool_size);
216 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
217 debug("%s: initialized an early full malloc pool at 0x%08lx of 0x%lx bytes\n",
218 __func__, (unsigned long)pool_addr, (unsigned long)pool_size);
219 /*
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500220 * Process pinctrl for the serial0 a.k.a. WKUP_UART0 module and continue
221 * regardless of the result of pinctrl. Do this without probing the
222 * device, but instead by searching the device that would request the
223 * given sequence number if probed. The UART will be used by the system
224 * firmware (SYSFW) image for various purposes and SYSFW depends on us
225 * to initialize its pin settings.
226 */
Simon Glass07e13382020-12-16 21:20:29 -0700227 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 0, &dev);
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500228 if (!ret)
229 pinctrl_select_state(dev, "default");
230
231 /*
Lokesh Vutla5fafe442020-03-10 16:50:58 +0530232 * Load, start up, and configure system controller firmware while
233 * also populating the SYSFW post-PM configuration callback hook.
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500234 */
Lokesh Vutla17951b72020-08-05 22:44:18 +0530235 k3_sysfw_loader(false, k3_mmc_stop_clock, k3_mmc_restart_clock);
Faiz Abbas68393212020-02-26 13:44:36 +0530236
237 /* Prepare console output */
238 preloader_console_init();
Andrew F. Davisf0bcb662020-01-10 14:35:21 -0500239
240 /* Disable ROM configured firewalls right after loading sysfw */
241#ifdef CONFIG_TI_SECURE_DEVICE
242 remove_fwl_configs(main_cbass_fwls, ARRAY_SIZE(main_cbass_fwls));
243 remove_fwl_configs(mcu_cbass_fwls, ARRAY_SIZE(mcu_cbass_fwls));
244#endif
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500245#else
Lokesh Vutla32886442018-08-27 15:57:09 +0530246 /* Prepare console output */
247 preloader_console_init();
Andreas Dannenbergea91da12019-06-04 17:55:50 -0500248#endif
Lokesh Vutla2c69d5c2018-11-02 19:51:06 +0530249
Lokesh Vutla5fafe442020-03-10 16:50:58 +0530250 /* Output System Firmware version info */
251 k3_sysfw_print_ver();
252
Andreas Dannenberg63f5c852019-06-04 18:08:26 -0500253 /* Perform EEPROM-based board detection */
Christian Gmeiner955bc4f2022-02-15 07:47:55 +0100254 if (IS_ENABLED(CONFIG_TI_I2C_BOARD_DETECT))
255 do_board_detect();
Andreas Dannenberg63f5c852019-06-04 18:08:26 -0500256
Keerthy2cd50972019-10-24 15:00:52 +0530257#if defined(CONFIG_CPU_V7R) && defined(CONFIG_K3_AVS0)
Simon Glass65130cd2020-12-28 20:34:56 -0700258 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(k3_avs),
Keerthy2cd50972019-10-24 15:00:52 +0530259 &dev);
260 if (ret)
261 printf("AVS init failed: %d\n", ret);
262#endif
263
Lokesh Vutla2c69d5c2018-11-02 19:51:06 +0530264#ifdef CONFIG_K3_AM654_DDRSS
265 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
Andreas Dannenberg7f6b62e2019-03-11 15:15:43 -0500266 if (ret)
267 panic("DRAM init failed: %d\n", ret);
Lokesh Vutla2c69d5c2018-11-02 19:51:06 +0530268#endif
Jan Kiszka7ce99f72020-05-18 07:57:22 +0200269 spl_enable_dcache();
Lokesh Vutla32886442018-08-27 15:57:09 +0530270}
271
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100272u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Andrew F. Davisc5161462018-10-03 10:03:23 -0500273{
274#if defined(CONFIG_SUPPORT_EMMC_BOOT)
275 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
Andrew F. Davisc5161462018-10-03 10:03:23 -0500276
277 u32 bootmode = (devstat & CTRLMMR_MAIN_DEVSTAT_BOOTMODE_MASK) >>
278 CTRLMMR_MAIN_DEVSTAT_BOOTMODE_SHIFT;
279
280 /* eMMC boot0 mode is only supported for primary boot */
281 if (bootindex == K3_PRIMARY_BOOTMODE &&
282 bootmode == BOOT_DEVICE_MMC1)
283 return MMCSD_MODE_EMMCBOOT;
284#endif
285
286 /* Everything else use filesystem if available */
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800287#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Andrew F. Davisc5161462018-10-03 10:03:23 -0500288 return MMCSD_MODE_FS;
289#else
290 return MMCSD_MODE_RAW;
291#endif
292}
293
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530294static u32 __get_backup_bootmedia(u32 devstat)
Lokesh Vutla32886442018-08-27 15:57:09 +0530295{
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530296 u32 bkup_boot = (devstat & CTRLMMR_MAIN_DEVSTAT_BKUP_BOOTMODE_MASK) >>
297 CTRLMMR_MAIN_DEVSTAT_BKUP_BOOTMODE_SHIFT;
298
299 switch (bkup_boot) {
300 case BACKUP_BOOT_DEVICE_USB:
301 return BOOT_DEVICE_USB;
302 case BACKUP_BOOT_DEVICE_UART:
303 return BOOT_DEVICE_UART;
304 case BACKUP_BOOT_DEVICE_ETHERNET:
305 return BOOT_DEVICE_ETHERNET;
306 case BACKUP_BOOT_DEVICE_MMC2:
Andrew F. Davisf515cf02018-10-03 10:03:22 -0500307 {
308 u32 port = (devstat & CTRLMMR_MAIN_DEVSTAT_BKUP_MMC_PORT_MASK) >>
309 CTRLMMR_MAIN_DEVSTAT_BKUP_MMC_PORT_SHIFT;
310 if (port == 0x0)
311 return BOOT_DEVICE_MMC1;
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530312 return BOOT_DEVICE_MMC2;
Andrew F. Davisf515cf02018-10-03 10:03:22 -0500313 }
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530314 case BACKUP_BOOT_DEVICE_SPI:
315 return BOOT_DEVICE_SPI;
316 case BACKUP_BOOT_DEVICE_HYPERFLASH:
317 return BOOT_DEVICE_HYPERFLASH;
318 case BACKUP_BOOT_DEVICE_I2C:
319 return BOOT_DEVICE_I2C;
320 };
321
Lokesh Vutla32886442018-08-27 15:57:09 +0530322 return BOOT_DEVICE_RAM;
323}
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530324
325static u32 __get_primary_bootmedia(u32 devstat)
326{
Andrew F. Davisf515cf02018-10-03 10:03:22 -0500327 u32 bootmode = (devstat & CTRLMMR_MAIN_DEVSTAT_BOOTMODE_MASK) >>
328 CTRLMMR_MAIN_DEVSTAT_BOOTMODE_SHIFT;
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530329
330 if (bootmode == BOOT_DEVICE_OSPI || bootmode == BOOT_DEVICE_QSPI)
331 bootmode = BOOT_DEVICE_SPI;
332
Andrew F. Davisf515cf02018-10-03 10:03:22 -0500333 if (bootmode == BOOT_DEVICE_MMC2) {
334 u32 port = (devstat & CTRLMMR_MAIN_DEVSTAT_MMC_PORT_MASK) >>
335 CTRLMMR_MAIN_DEVSTAT_MMC_PORT_SHIFT;
336 if (port == 0x0)
337 bootmode = BOOT_DEVICE_MMC1;
338 } else if (bootmode == BOOT_DEVICE_MMC1) {
339 u32 port = (devstat & CTRLMMR_MAIN_DEVSTAT_EMMC_PORT_MASK) >>
340 CTRLMMR_MAIN_DEVSTAT_EMMC_PORT_SHIFT;
341 if (port == 0x1)
342 bootmode = BOOT_DEVICE_MMC2;
Faiz Abbas0ae20ed2020-08-03 11:35:10 +0530343 } else if (bootmode == BOOT_DEVICE_DFU) {
344 u32 mode = (devstat & CTRLMMR_MAIN_DEVSTAT_USB_MODE_MASK) >>
345 CTRLMMR_MAIN_DEVSTAT_USB_MODE_SHIFT;
346 if (mode == 0x2)
347 bootmode = BOOT_DEVICE_USB;
Andrew F. Davisf515cf02018-10-03 10:03:22 -0500348 }
349
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530350 return bootmode;
351}
352
353u32 spl_boot_device(void)
354{
355 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
Lokesh Vutlac7bfb852018-08-27 15:57:11 +0530356
357 if (bootindex == K3_PRIMARY_BOOTMODE)
358 return __get_primary_bootmedia(devstat);
359 else
360 return __get_backup_bootmedia(devstat);
361}
Lokesh Vutla32886442018-08-27 15:57:09 +0530362#endif
363
Andreas Dannenberg31175f82019-06-07 19:24:42 +0530364#ifdef CONFIG_SYS_K3_SPL_ATF
365
366#define AM6_DEV_MCU_RTI0 134
367#define AM6_DEV_MCU_RTI1 135
368#define AM6_DEV_MCU_ARMSS0_CPU0 159
369#define AM6_DEV_MCU_ARMSS0_CPU1 245
370
371void release_resources_for_core_shutdown(void)
372{
Lokesh Vutlaac9ca952019-09-09 12:47:38 +0530373 struct ti_sci_handle *ti_sci = get_ti_sci_handle();
374 struct ti_sci_dev_ops *dev_ops = &ti_sci->ops.dev_ops;
375 struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops;
Andreas Dannenberg31175f82019-06-07 19:24:42 +0530376 int ret;
377 u32 i;
378
379 const u32 put_device_ids[] = {
380 AM6_DEV_MCU_RTI0,
381 AM6_DEV_MCU_RTI1,
382 };
383
Andreas Dannenberg31175f82019-06-07 19:24:42 +0530384 /* Iterate through list of devices to put (shutdown) */
385 for (i = 0; i < ARRAY_SIZE(put_device_ids); i++) {
386 u32 id = put_device_ids[i];
387
388 ret = dev_ops->put_device(ti_sci, id);
389 if (ret)
390 panic("Failed to put device %u (%d)\n", id, ret);
391 }
392
393 const u32 put_core_ids[] = {
394 AM6_DEV_MCU_ARMSS0_CPU1,
395 AM6_DEV_MCU_ARMSS0_CPU0, /* Handle CPU0 after CPU1 */
396 };
397
398 /* Iterate through list of cores to put (shutdown) */
399 for (i = 0; i < ARRAY_SIZE(put_core_ids); i++) {
400 u32 id = put_core_ids[i];
401
402 /*
403 * Queue up the core shutdown request. Note that this call
404 * needs to be followed up by an actual invocation of an WFE
405 * or WFI CPU instruction.
406 */
407 ret = proc_ops->proc_shutdown_no_wait(ti_sci, id);
408 if (ret)
409 panic("Failed sending core %u shutdown message (%d)\n",
410 id, ret);
411 }
412}
413#endif