blob: 12da8136f9e1450d4809506b4570ab90bca85803 [file] [log] [blame]
David Huang61098202022-01-25 20:56:31 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * J721E: SoC specific initialization
4 *
5 * Copyright (C) 2021 Texas Instruments Incorporated - http://www.ti.com/
6 * David Huang <d-huang@ti.com>
7 */
8
9#include <common.h>
10#include <init.h>
11#include <spl.h>
12#include <asm/io.h>
13#include <asm/armv7_mpu.h>
14#include <asm/arch/hardware.h>
15#include <asm/arch/sysfw-loader.h>
16#include "common.h"
17#include <asm/arch/sys_proto.h>
18#include <linux/soc/ti/ti_sci_protocol.h>
19#include <dm.h>
20#include <dm/uclass-internal.h>
21#include <dm/pinctrl.h>
22#include <mmc.h>
23#include <remoteproc.h>
24
David Huang61098202022-01-25 20:56:31 +053025static void ctrl_mmr_unlock(void)
26{
27 /* Unlock all WKUP_CTRL_MMR0 module registers */
28 mmr_unlock(WKUP_CTRL_MMR0_BASE, 0);
29 mmr_unlock(WKUP_CTRL_MMR0_BASE, 1);
30 mmr_unlock(WKUP_CTRL_MMR0_BASE, 2);
31 mmr_unlock(WKUP_CTRL_MMR0_BASE, 3);
32 mmr_unlock(WKUP_CTRL_MMR0_BASE, 4);
33 mmr_unlock(WKUP_CTRL_MMR0_BASE, 6);
34 mmr_unlock(WKUP_CTRL_MMR0_BASE, 7);
35
36 /* Unlock all MCU_CTRL_MMR0 module registers */
37 mmr_unlock(MCU_CTRL_MMR0_BASE, 0);
38 mmr_unlock(MCU_CTRL_MMR0_BASE, 1);
39 mmr_unlock(MCU_CTRL_MMR0_BASE, 2);
40 mmr_unlock(MCU_CTRL_MMR0_BASE, 3);
41 mmr_unlock(MCU_CTRL_MMR0_BASE, 4);
42
43 /* 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, 7);
50}
51
52void k3_mmc_stop_clock(void)
53{
54 if (IS_ENABLED(CONFIG_K3_LOAD_SYSFW)) {
55 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
56 struct mmc *mmc = find_mmc_device(0);
57
58 if (!mmc)
59 return;
60
61 mmc->saved_clock = mmc->clock;
62 mmc_set_clock(mmc, 0, true);
63 }
64 }
65}
66
67void k3_mmc_restart_clock(void)
68{
69 if (IS_ENABLED(CONFIG_K3_LOAD_SYSFW)) {
70 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
71 struct mmc *mmc = find_mmc_device(0);
72
73 if (!mmc)
74 return;
75
76 mmc_set_clock(mmc, mmc->saved_clock, false);
77 }
78 }
79}
80
81/*
82 * This uninitialized global variable would normal end up in the .bss section,
83 * but the .bss is cleared between writing and reading this variable, so move
84 * it to the .data section.
85 */
86u32 bootindex __attribute__((section(".data")));
87static struct rom_extended_boot_data bootdata __section(".data");
88
89static void store_boot_info_from_rom(void)
90{
91 bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
92 memcpy(&bootdata, (uintptr_t *)ROM_ENTENDED_BOOT_DATA_INFO,
93 sizeof(struct rom_extended_boot_data));
94}
95
96void board_init_f(ulong dummy)
97{
98 struct udevice *dev;
99 int ret;
100 /*
101 * Cannot delay this further as there is a chance that
102 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
103 */
104 store_boot_info_from_rom();
105
106 /* Make all control module registers accessible */
107 ctrl_mmr_unlock();
108
109 if (IS_ENABLED(CONFIG_CPU_V7R)) {
110 disable_linefill_optimization();
111 setup_k3_mpu_regions();
112 }
113
114 /* Init DM early */
115 spl_early_init();
116
117 /* Prepare console output */
118 preloader_console_init();
119
120 if (IS_ENABLED(CONFIG_K3_LOAD_SYSFW)) {
121 /*
122 * Process pinctrl for the serial0 a.k.a. WKUP_UART0 module and continue
123 * regardless of the result of pinctrl. Do this without probing the
124 * device, but instead by searching the device that would request the
125 * given sequence number if probed. The UART will be used by the system
126 * firmware (SYSFW) image for various purposes and SYSFW depends on us
127 * to initialize its pin settings.
128 */
129 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 0, &dev);
130 if (!ret)
131 pinctrl_select_state(dev, "default");
132
133 /*
134 * Load, start up, and configure system controller firmware. Provide
135 * the U-Boot console init function to the SYSFW post-PM configuration
136 * callback hook, effectively switching on (or over) the console
137 * output.
138 */
139 k3_sysfw_loader(is_rom_loaded_sysfw(&bootdata),
140 k3_mmc_stop_clock, k3_mmc_restart_clock);
141
142 if (IS_ENABLED(CONFIG_SPL_CLK_K3)) {
143 /*
144 * Force probe of clk_k3 driver here to ensure basic default clock
145 * configuration is always done for enabling PM services.
146 */
147 ret = uclass_get_device_by_driver(UCLASS_CLK,
148 DM_DRIVER_GET(ti_clk),
149 &dev);
150 if (ret)
151 panic("Failed to initialize clk-k3!\n");
152 }
153 }
154
155 /* Output System Firmware version info */
156 k3_sysfw_print_ver();
157
158 if (IS_ENABLED(CONFIG_TARGET_J721S2_R5_EVM)) {
159 ret = uclass_get_device_by_name(UCLASS_MISC, "msmc", &dev);
160 if (ret)
161 panic("Probe of msmc failed: %d\n", ret);
162
163 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
164 if (ret)
165 panic("DRAM 0 init failed: %d\n", ret);
166
167 ret = uclass_next_device(&dev);
168 if (ret)
169 panic("DRAM 1 init failed: %d\n", ret);
170 }
171 spl_enable_dcache();
172}
173
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100174u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
David Huang61098202022-01-25 20:56:31 +0530175{
176 switch (boot_device) {
177 case BOOT_DEVICE_MMC1:
178 return MMCSD_MODE_EMMCBOOT;
179 case BOOT_DEVICE_MMC2:
180 return MMCSD_MODE_FS;
181 default:
182 return MMCSD_MODE_RAW;
183 }
184}
185
186static u32 __get_backup_bootmedia(u32 main_devstat)
187{
188 u32 bkup_boot = (main_devstat & MAIN_DEVSTAT_BKUP_BOOTMODE_MASK) >>
189 MAIN_DEVSTAT_BKUP_BOOTMODE_SHIFT;
190
191 switch (bkup_boot) {
192 case BACKUP_BOOT_DEVICE_USB:
193 return BOOT_DEVICE_DFU;
194 case BACKUP_BOOT_DEVICE_UART:
195 return BOOT_DEVICE_UART;
196 case BACKUP_BOOT_DEVICE_ETHERNET:
197 return BOOT_DEVICE_ETHERNET;
198 case BACKUP_BOOT_DEVICE_MMC2:
199 {
200 u32 port = (main_devstat & MAIN_DEVSTAT_BKUP_MMC_PORT_MASK) >>
201 MAIN_DEVSTAT_BKUP_MMC_PORT_SHIFT;
202 if (port == 0x0)
203 return BOOT_DEVICE_MMC1;
204 return BOOT_DEVICE_MMC2;
205 }
206 case BACKUP_BOOT_DEVICE_SPI:
207 return BOOT_DEVICE_SPI;
208 case BACKUP_BOOT_DEVICE_I2C:
209 return BOOT_DEVICE_I2C;
210 }
211
212 return BOOT_DEVICE_RAM;
213}
214
215static u32 __get_primary_bootmedia(u32 main_devstat, u32 wkup_devstat)
216{
217 u32 bootmode = (wkup_devstat & WKUP_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
218 WKUP_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
219
220 bootmode |= (main_devstat & MAIN_DEVSTAT_BOOT_MODE_B_MASK) <<
221 BOOT_MODE_B_SHIFT;
222
223 if (bootmode == BOOT_DEVICE_OSPI || bootmode == BOOT_DEVICE_QSPI ||
224 bootmode == BOOT_DEVICE_XSPI)
225 bootmode = BOOT_DEVICE_SPI;
226
227 if (bootmode == BOOT_DEVICE_MMC2) {
228 u32 port = (main_devstat &
229 MAIN_DEVSTAT_PRIM_BOOTMODE_MMC_PORT_MASK) >>
230 MAIN_DEVSTAT_PRIM_BOOTMODE_PORT_SHIFT;
231 if (port == 0x0)
232 bootmode = BOOT_DEVICE_MMC1;
233 }
234
235 return bootmode;
236}
237
238u32 spl_boot_device(void)
239{
240 u32 wkup_devstat = readl(CTRLMMR_WKUP_DEVSTAT);
241 u32 main_devstat;
242
243 if (wkup_devstat & WKUP_DEVSTAT_MCU_OMLY_MASK) {
244 printf("ERROR: MCU only boot is not yet supported\n");
245 return BOOT_DEVICE_RAM;
246 }
247
248 /* MAIN CTRL MMR can only be read if MCU ONLY is 0 */
249 main_devstat = readl(CTRLMMR_MAIN_DEVSTAT);
250
251 if (bootindex == K3_PRIMARY_BOOTMODE)
252 return __get_primary_bootmedia(main_devstat, wkup_devstat);
253 else
254 return __get_backup_bootmedia(main_devstat);
255}
David Huang61098202022-01-25 20:56:31 +0530256
257#define J721S2_DEV_MCU_RTI0 295
258#define J721S2_DEV_MCU_RTI1 296
259#define J721S2_DEV_MCU_ARMSS0_CPU0 284
260#define J721S2_DEV_MCU_ARMSS0_CPU1 285
261
262void release_resources_for_core_shutdown(void)
263{
264 if (IS_ENABLED(CONFIG_SYS_K3_SPL_ATF)) {
265 struct ti_sci_handle *ti_sci;
266 struct ti_sci_dev_ops *dev_ops;
267 struct ti_sci_proc_ops *proc_ops;
268 int ret;
269 u32 i;
270
271 const u32 put_device_ids[] = {
272 J721S2_DEV_MCU_RTI0,
273 J721S2_DEV_MCU_RTI1,
274 };
275
276 ti_sci = get_ti_sci_handle();
277 dev_ops = &ti_sci->ops.dev_ops;
278 proc_ops = &ti_sci->ops.proc_ops;
279
280 /* Iterate through list of devices to put (shutdown) */
281 for (i = 0; i < ARRAY_SIZE(put_device_ids); i++) {
282 u32 id = put_device_ids[i];
283
284 ret = dev_ops->put_device(ti_sci, id);
285 if (ret)
286 panic("Failed to put device %u (%d)\n", id, ret);
287 }
288
289 const u32 put_core_ids[] = {
290 J721S2_DEV_MCU_ARMSS0_CPU1,
291 J721S2_DEV_MCU_ARMSS0_CPU0, /* Handle CPU0 after CPU1 */
292 };
293
294 /* Iterate through list of cores to put (shutdown) */
295 for (i = 0; i < ARRAY_SIZE(put_core_ids); i++) {
296 u32 id = put_core_ids[i];
297
298 /*
299 * Queue up the core shutdown request. Note that this call
300 * needs to be followed up by an actual invocation of an WFE
301 * or WFI CPU instruction.
302 */
303 ret = proc_ops->proc_shutdown_no_wait(ti_sci, id);
304 if (ret)
305 panic("Failed sending core %u shutdown message (%d)\n",
306 id, ret);
307 }
308 }
309}