blob: 01b00681f68ea2ddde6dbb603623dea179a83d75 [file] [log] [blame]
Jayesh Choudhary732d2ff2024-06-12 14:41:18 +05301// SPDX-License-Identifier: GPL-2.0
2/*
3 * J722S: SoC specific initialization
4 *
5 * Copyright (C) 2024 Texas Instruments Incorporated - https://www.ti.com/
6 */
7
8#include <spl.h>
9#include <asm/io.h>
10#include <asm/arch/hardware.h>
11#include <dm.h>
12#include <dm/uclass-internal.h>
13#include <dm/pinctrl.h>
14
15#include "../sysfw-loader.h"
16#include "../common.h"
17
18struct fwl_data cbass_main_fwls[] = {
19 { "FSS_DAT_REG3", 7, 8 },
20};
21
22/*
23 * This uninitialized global variable would normal end up in the .bss section,
24 * but the .bss is cleared between writing and reading this variable, so move
25 * it to the .data section.
26 */
27u32 bootindex __section(".data");
28static struct rom_extended_boot_data bootdata __section(".data");
29
30static void store_boot_info_from_rom(void)
31{
32 bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
33 memcpy(&bootdata, (uintptr_t *)ROM_EXTENDED_BOOT_DATA_INFO,
34 sizeof(struct rom_extended_boot_data));
35}
36
37static void ctrl_mmr_unlock(void)
38{
39 /* Unlock all WKUP_CTRL_MMR0 module registers */
40 mmr_unlock(WKUP_CTRL_MMR0_BASE, 0);
41 mmr_unlock(WKUP_CTRL_MMR0_BASE, 1);
42 mmr_unlock(WKUP_CTRL_MMR0_BASE, 2);
43 mmr_unlock(WKUP_CTRL_MMR0_BASE, 3);
44 mmr_unlock(WKUP_CTRL_MMR0_BASE, 4);
45 mmr_unlock(WKUP_CTRL_MMR0_BASE, 5);
46 mmr_unlock(WKUP_CTRL_MMR0_BASE, 6);
47 mmr_unlock(WKUP_CTRL_MMR0_BASE, 7);
48
49 /* Unlock all CTRL_MMR0 module registers */
50 mmr_unlock(CTRL_MMR0_BASE, 0);
51 mmr_unlock(CTRL_MMR0_BASE, 1);
52 mmr_unlock(CTRL_MMR0_BASE, 2);
53 mmr_unlock(CTRL_MMR0_BASE, 4);
54 mmr_unlock(CTRL_MMR0_BASE, 5);
55 mmr_unlock(CTRL_MMR0_BASE, 6);
56
57 /* Unlock all MCU_CTRL_MMR0 module registers */
58 mmr_unlock(MCU_CTRL_MMR0_BASE, 0);
59 mmr_unlock(MCU_CTRL_MMR0_BASE, 1);
60 mmr_unlock(MCU_CTRL_MMR0_BASE, 2);
61 mmr_unlock(MCU_CTRL_MMR0_BASE, 3);
62 mmr_unlock(MCU_CTRL_MMR0_BASE, 4);
63 mmr_unlock(MCU_CTRL_MMR0_BASE, 6);
64
65 /* Unlock PADCFG_CTRL_MMR padconf registers */
66 mmr_unlock(PADCFG_MMR0_BASE, 1);
67 mmr_unlock(PADCFG_MMR1_BASE, 1);
68}
69
70static void k3_spl_init(void)
71{
72 struct udevice *dev;
73 int ret;
74
75 if (IS_ENABLED(CONFIG_CPU_V7R))
76 setup_k3_mpu_regions();
77
78 /*
79 * Cannot delay this further as there is a chance that
80 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
81 */
82 store_boot_info_from_rom();
83
84 ctrl_mmr_unlock();
85
86 /* Init DM early */
87 ret = spl_early_init();
88 if (ret)
89 panic("spl_early_init() failed: %d\n", ret);
90
91 /*
92 * Process pinctrl for the serial0 a.k.a. WKUP_UART0 module and continue
93 * regardless of the result of pinctrl. Do this without probing the
94 * device, but instead by searching the device that would request the
95 * given sequence number if probed. The UART will be used by the DM
96 * firmware image for various purposes and TIFS depends on us to
97 * initialize its pin settings.
98 */
99 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 0, &dev);
100 if (!ret)
101 pinctrl_select_state(dev, "default");
102
103 if (IS_ENABLED(CONFIG_K3_EARLY_CONS)) {
104 /*
105 * Allow establishing an early console as required for example
106 * when doing a UART-based boot. Note that this console may not
107 * "survive" through a SYSFW PM-init step and will need a re-init
108 * in some way due to changing module clock frequencies.
109 */
110 ret = early_console_init();
111 if (ret)
112 panic("early_console_init() failed: %d\n", ret);
113 }
114
115 if (IS_ENABLED(CONFIG_K3_LOAD_SYSFW)) {
116 /*
117 * Configure and start up system controller firmware. Provide
118 * the U-Boot console init function to the SYSFW post-PM
119 * configuration callback hook, effectively switching on (or
120 * over) the console output.
121 */
122 ret = is_rom_loaded_sysfw(&bootdata);
123 if (!ret)
124 panic("ROM has not loaded TIFS firmware\n");
125
126 k3_sysfw_loader(true, NULL, NULL);
127 }
128
129 /*
130 * Force probe of clk_k3 driver here to ensure basic default clock
131 * configuration is always done.
132 */
133 if (IS_ENABLED(CONFIG_SPL_CLK_K3)) {
134 ret = uclass_get_device_by_driver(UCLASS_CLK,
135 DM_DRIVER_GET(ti_clk),
136 &dev);
137 if (ret)
138 printf("Failed to initialize clk-k3!\n");
139 }
140
141 preloader_console_init();
142
143 if (IS_ENABLED(CONFIG_CPU_V7R)) {
144 /* Disable ROM configured firewalls right after loading sysfw */
145 remove_fwl_configs(cbass_main_fwls, ARRAY_SIZE(cbass_main_fwls));
146 }
147
148 /* Output System Firmware version info */
149 k3_sysfw_print_ver();
150}
151
152static void k3_mem_init(void)
153{
154 struct udevice *dev;
155 int ret;
156
157 if (IS_ENABLED(CONFIG_K3_AM62A_DDRSS)) {
158 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
159 if (ret)
160 panic("DRAM init failed: %d\n", ret);
161 }
162}
163
164void board_init_f(ulong dummy)
165{
166 k3_spl_init();
167 k3_mem_init();
168}
169
170static u32 __get_backup_bootmedia(u32 devstat)
171{
172 u32 bkup_bootmode = (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
173 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
174 u32 bkup_bootmode_cfg =
175 (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
176 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
177
178 switch (bkup_bootmode) {
179 case BACKUP_BOOT_DEVICE_UART:
180 return BOOT_DEVICE_UART;
181
182 case BACKUP_BOOT_DEVICE_USB:
183 return BOOT_DEVICE_USB;
184
185 case BACKUP_BOOT_DEVICE_ETHERNET:
186 return BOOT_DEVICE_ETHERNET;
187
188 case BACKUP_BOOT_DEVICE_MMC:
189 if (bkup_bootmode_cfg)
190 return BOOT_DEVICE_MMC2;
191 return BOOT_DEVICE_MMC1;
192
193 case BACKUP_BOOT_DEVICE_SPI:
194 return BOOT_DEVICE_SPI;
195
196 case BACKUP_BOOT_DEVICE_I2C:
197 return BOOT_DEVICE_I2C;
198
199 case BACKUP_BOOT_DEVICE_DFU:
200 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
201 return BOOT_DEVICE_USB;
202 return BOOT_DEVICE_DFU;
203 };
204
205 return BOOT_DEVICE_RAM;
206}
207
208static u32 __get_primary_bootmedia(u32 devstat)
209{
210 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
211 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
212 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
213 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
214
215 switch (bootmode) {
216 case BOOT_DEVICE_OSPI:
217 fallthrough;
218 case BOOT_DEVICE_QSPI:
219 fallthrough;
220 case BOOT_DEVICE_XSPI:
221 fallthrough;
222 case BOOT_DEVICE_SPI:
223 return BOOT_DEVICE_SPI;
224
225 case BOOT_DEVICE_ETHERNET_RGMII:
226 fallthrough;
227 case BOOT_DEVICE_ETHERNET_RMII:
228 return BOOT_DEVICE_ETHERNET;
229
230 case BOOT_DEVICE_EMMC:
231 return BOOT_DEVICE_MMC1;
232
233 case BOOT_DEVICE_SPI_NAND:
234 return BOOT_DEVICE_SPINAND;
235
236 case BOOT_DEVICE_MMC:
237 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
238 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
239 return BOOT_DEVICE_MMC2;
240 return BOOT_DEVICE_MMC1;
241
242 case BOOT_DEVICE_DFU:
243 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
244 MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
245 return BOOT_DEVICE_USB;
246 return BOOT_DEVICE_DFU;
247
248 case BOOT_DEVICE_NOBOOT:
249 return BOOT_DEVICE_RAM;
250 }
251
252 return bootmode;
253}
254
255u32 spl_boot_device(void)
256{
257 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
258 u32 bootmedia;
259
260 if (bootindex == K3_PRIMARY_BOOTMODE)
261 bootmedia = __get_primary_bootmedia(devstat);
262 else
263 bootmedia = __get_backup_bootmedia(devstat);
264
265 debug("j722s_init: %s: devstat = 0x%x bootmedia = 0x%x bootindex = %d\n",
266 __func__, devstat, bootmedia, bootindex);
267 return bootmedia;
268}
269
270u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
271{
272 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
273 u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
274 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
275 u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
276 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
277
278 switch (bootmode) {
279 case BOOT_DEVICE_EMMC:
280 return MMCSD_MODE_EMMCBOOT;
281 case BOOT_DEVICE_MMC:
282 if (bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_FS_RAW_MASK)
283 return MMCSD_MODE_RAW;
284 default:
285 return MMCSD_MODE_FS;
286 }
287}