blob: 1a2bc4bb9048ddd5a62c3fd43ce01e6663d2712a [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>
11#include <spl.h>
12#include <asm/io.h>
Keerthy05d670e2021-04-23 11:27:33 -050013#include <asm/arch/hardware.h>
Dave Gerlach8e0689b2021-04-23 11:27:36 -050014#include <asm/arch/sysfw-loader.h>
15#include <asm/arch/sys_proto.h>
Dave Gerlach96571ec2021-04-23 11:27:32 -050016#include "common.h"
Dave Gerlach8e0689b2021-04-23 11:27:36 -050017#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>
Dave Gerlacha89f7a42021-04-23 11:27:37 -050022#include <mmc.h>
Lokesh Vutla17ad6e02021-05-06 16:44:52 +053023#include <dm/root.h>
Dave Gerlach96571ec2021-04-23 11:27:32 -050024
25#if defined(CONFIG_SPL_BUILD)
26
Dave Gerlacheaef1292021-04-23 11:27:34 -050027static void ctrl_mmr_unlock(void)
28{
29 /* Unlock all PADCFG_MMR1 module registers */
30 mmr_unlock(PADCFG_MMR1_BASE, 1);
31
32 /* Unlock all CTRL_MMR0 module registers */
33 mmr_unlock(CTRL_MMR0_BASE, 0);
34 mmr_unlock(CTRL_MMR0_BASE, 1);
35 mmr_unlock(CTRL_MMR0_BASE, 2);
36 mmr_unlock(CTRL_MMR0_BASE, 3);
37 mmr_unlock(CTRL_MMR0_BASE, 5);
38 mmr_unlock(CTRL_MMR0_BASE, 6);
39}
40
Dave Gerlachb27a9f22021-04-23 11:27:35 -050041/*
42 * This uninitialized global variable would normal end up in the .bss section,
43 * but the .bss is cleared between writing and reading this variable, so move
44 * it to the .data section.
45 */
46u32 bootindex __section(".data");
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020047static struct rom_extended_boot_data bootdata __section(".data");
Dave Gerlachb27a9f22021-04-23 11:27:35 -050048
49static void store_boot_info_from_rom(void)
50{
51 bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
52 memcpy(&bootdata, (uintptr_t *)ROM_ENTENDED_BOOT_DATA_INFO,
53 sizeof(struct rom_extended_boot_data));
54}
55
Dave Gerlacha89f7a42021-04-23 11:27:37 -050056#if defined(CONFIG_K3_LOAD_SYSFW) && CONFIG_IS_ENABLED(DM_MMC)
57void k3_mmc_stop_clock(void)
58{
59 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
60 struct mmc *mmc = find_mmc_device(0);
61
62 if (!mmc)
63 return;
64
65 mmc->saved_clock = mmc->clock;
66 mmc_set_clock(mmc, 0, true);
67 }
68}
69
70void k3_mmc_restart_clock(void)
71{
72 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
73 struct mmc *mmc = find_mmc_device(0);
74
75 if (!mmc)
76 return;
77
78 mmc_set_clock(mmc, mmc->saved_clock, false);
79 }
80}
81#else
82void k3_mmc_stop_clock(void) {}
83void k3_mmc_restart_clock(void) {}
84#endif
85
Lokesh Vutla17ad6e02021-05-06 16:44:52 +053086#ifdef CONFIG_SPL_OF_LIST
87void do_dt_magic(void)
88{
89 int ret, rescan;
90
91 if (IS_ENABLED(CONFIG_TI_I2C_BOARD_DETECT))
92 do_board_detect();
93
94 /*
95 * Board detection has been done.
96 * Let us see if another dtb wouldn't be a better match
97 * for our board
98 */
99 if (IS_ENABLED(CONFIG_CPU_V7R)) {
100 ret = fdtdec_resetup(&rescan);
101 if (!ret && rescan) {
102 dm_uninit();
103 dm_init_and_scan(true);
104 }
105 }
106}
107#endif
108
Dave Gerlach96571ec2021-04-23 11:27:32 -0500109void board_init_f(ulong dummy)
110{
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500111#if defined(CONFIG_K3_LOAD_SYSFW)
112 struct udevice *dev;
113 int ret;
114#endif
115
Dave Gerlach96571ec2021-04-23 11:27:32 -0500116#if defined(CONFIG_CPU_V7R)
117 setup_k3_mpu_regions();
118#endif
119
Dave Gerlachb27a9f22021-04-23 11:27:35 -0500120 /*
121 * Cannot delay this further as there is a chance that
122 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
123 */
124 store_boot_info_from_rom();
125
Dave Gerlacheaef1292021-04-23 11:27:34 -0500126 ctrl_mmr_unlock();
127
Dave Gerlach96571ec2021-04-23 11:27:32 -0500128 /* Init DM early */
129 spl_early_init();
130
131 preloader_console_init();
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500132
Lokesh Vutla17ad6e02021-05-06 16:44:52 +0530133 do_dt_magic();
134
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500135#if defined(CONFIG_K3_LOAD_SYSFW)
136 /*
137 * Process pinctrl for serial3 a.k.a. MAIN UART1 module and continue
138 * regardless of the result of pinctrl. Do this without probing the
139 * device, but instead by searching the device that would request the
140 * given sequence number if probed. The UART will be used by the system
141 * firmware (SYSFW) image for various purposes and SYSFW depends on us
142 * to initialize its pin settings.
143 */
144 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev);
145 if (!ret)
146 pinctrl_select_state(dev, "default");
147
148 /*
149 * Load, start up, and configure system controller firmware.
150 * This will determine whether or not ROM has already loaded
151 * system firmware and if so, will only perform needed config
152 * and not attempt to load firmware again.
153 */
Dave Gerlacha89f7a42021-04-23 11:27:37 -0500154 k3_sysfw_loader(is_rom_loaded_sysfw(&bootdata), k3_mmc_stop_clock,
155 k3_mmc_restart_clock);
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500156#endif
157
158 /* Output System Firmware version info */
159 k3_sysfw_print_ver();
Dave Gerlachad38f512021-05-04 18:00:53 -0500160
161#if defined(CONFIG_K3_AM64_DDRSS)
162 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
163 if (ret)
164 panic("DRAM init failed: %d\n", ret);
165#endif
Dave Gerlach96571ec2021-04-23 11:27:32 -0500166}
Keerthy05d670e2021-04-23 11:27:33 -0500167
168u32 spl_boot_mode(const u32 boot_device)
169{
170 switch (boot_device) {
171 case BOOT_DEVICE_MMC1:
172 return MMCSD_MODE_EMMCBOOT;
173
174 case BOOT_DEVICE_MMC2:
175 return MMCSD_MODE_FS;
176
177 default:
178 return MMCSD_MODE_RAW;
179 }
180}
181
182static u32 __get_backup_bootmedia(u32 main_devstat)
183{
184 u32 bkup_bootmode =
185 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
186 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
187 u32 bkup_bootmode_cfg =
188 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
189 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
190
191 switch (bkup_bootmode) {
192 case BACKUP_BOOT_DEVICE_UART:
193 return BOOT_DEVICE_UART;
194
Aswath Govindraju8a05c9a2021-06-04 22:00:32 +0530195 case BACKUP_BOOT_DEVICE_DFU:
196 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
197 return BOOT_DEVICE_USB;
198 return BOOT_DEVICE_DFU;
199
Keerthy05d670e2021-04-23 11:27:33 -0500200
201 case BACKUP_BOOT_DEVICE_ETHERNET:
202 return BOOT_DEVICE_ETHERNET;
203
204 case BACKUP_BOOT_DEVICE_MMC:
205 if (bkup_bootmode_cfg)
206 return BOOT_DEVICE_MMC2;
207 return BOOT_DEVICE_MMC1;
208
209 case BACKUP_BOOT_DEVICE_SPI:
210 return BOOT_DEVICE_SPI;
211
212 case BACKUP_BOOT_DEVICE_I2C:
213 return BOOT_DEVICE_I2C;
214 };
215
216 return BOOT_DEVICE_RAM;
217}
218
219static u32 __get_primary_bootmedia(u32 main_devstat)
220{
221 u32 bootmode = (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
222 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
223 u32 bootmode_cfg =
224 (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
225 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
226
227 switch (bootmode) {
228 case BOOT_DEVICE_OSPI:
229 fallthrough;
230 case BOOT_DEVICE_QSPI:
231 fallthrough;
232 case BOOT_DEVICE_XSPI:
233 fallthrough;
234 case BOOT_DEVICE_SPI:
235 return BOOT_DEVICE_SPI;
236
237 case BOOT_DEVICE_ETHERNET_RGMII:
238 fallthrough;
239 case BOOT_DEVICE_ETHERNET_RMII:
240 return BOOT_DEVICE_ETHERNET;
241
242 case BOOT_DEVICE_EMMC:
243 return BOOT_DEVICE_MMC1;
244
245 case BOOT_DEVICE_MMC:
246 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
247 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
248 return BOOT_DEVICE_MMC2;
249 return BOOT_DEVICE_MMC1;
250
Aswath Govindraju8a05c9a2021-06-04 22:00:32 +0530251 case BOOT_DEVICE_DFU:
252 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
253 MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
254 return BOOT_DEVICE_USB;
255 return BOOT_DEVICE_DFU;
256
Keerthy05d670e2021-04-23 11:27:33 -0500257 case BOOT_DEVICE_NOBOOT:
258 return BOOT_DEVICE_RAM;
259 }
260
261 return bootmode;
262}
263
264u32 spl_boot_device(void)
265{
266 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
267
268 if (bootindex == K3_PRIMARY_BOOTMODE)
269 return __get_primary_bootmedia(devstat);
270 else
271 return __get_backup_bootmedia(devstat);
272}
Dave Gerlach96571ec2021-04-23 11:27:32 -0500273#endif
Suman Anna320c0202021-04-23 11:27:38 -0500274
275#if defined(CONFIG_SYS_K3_SPL_ATF)
276
277#define AM64X_DEV_RTI8 127
278#define AM64X_DEV_RTI9 128
279#define AM64X_DEV_R5FSS0_CORE0 121
280#define AM64X_DEV_R5FSS0_CORE1 122
281
282void release_resources_for_core_shutdown(void)
283{
284 struct ti_sci_handle *ti_sci = get_ti_sci_handle();
285 struct ti_sci_dev_ops *dev_ops = &ti_sci->ops.dev_ops;
286 struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops;
287 int ret;
288 u32 i;
289
290 const u32 put_device_ids[] = {
291 AM64X_DEV_RTI9,
292 AM64X_DEV_RTI8,
293 };
294
295 /* Iterate through list of devices to put (shutdown) */
296 for (i = 0; i < ARRAY_SIZE(put_device_ids); i++) {
297 u32 id = put_device_ids[i];
298
299 ret = dev_ops->put_device(ti_sci, id);
300 if (ret)
301 panic("Failed to put device %u (%d)\n", id, ret);
302 }
303
304 const u32 put_core_ids[] = {
305 AM64X_DEV_R5FSS0_CORE1,
306 AM64X_DEV_R5FSS0_CORE0, /* Handle CPU0 after CPU1 */
307 };
308
309 /* Iterate through list of cores to put (shutdown) */
310 for (i = 0; i < ARRAY_SIZE(put_core_ids); i++) {
311 u32 id = put_core_ids[i];
312
313 /*
314 * Queue up the core shutdown request. Note that this call
315 * needs to be followed up by an actual invocation of an WFE
316 * or WFI CPU instruction.
317 */
318 ret = proc_ops->proc_shutdown_no_wait(ti_sci, id);
319 if (ret)
320 panic("Failed sending core %u shutdown message (%d)\n",
321 id, ret);
322 }
323}
324#endif