blob: 2b58a9f94c6820641b71973f9d304ecee3e28406 [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>
Dave Gerlach96571ec2021-04-23 11:27:32 -050023
24#if defined(CONFIG_SPL_BUILD)
25
Dave Gerlacheaef1292021-04-23 11:27:34 -050026static void ctrl_mmr_unlock(void)
27{
28 /* Unlock all PADCFG_MMR1 module registers */
29 mmr_unlock(PADCFG_MMR1_BASE, 1);
30
31 /* Unlock all CTRL_MMR0 module registers */
32 mmr_unlock(CTRL_MMR0_BASE, 0);
33 mmr_unlock(CTRL_MMR0_BASE, 1);
34 mmr_unlock(CTRL_MMR0_BASE, 2);
35 mmr_unlock(CTRL_MMR0_BASE, 3);
36 mmr_unlock(CTRL_MMR0_BASE, 5);
37 mmr_unlock(CTRL_MMR0_BASE, 6);
38}
39
Dave Gerlachb27a9f22021-04-23 11:27:35 -050040/*
41 * This uninitialized global variable would normal end up in the .bss section,
42 * but the .bss is cleared between writing and reading this variable, so move
43 * it to the .data section.
44 */
45u32 bootindex __section(".data");
46static struct rom_extended_boot_data bootdata __section(.data);
47
48static void store_boot_info_from_rom(void)
49{
50 bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
51 memcpy(&bootdata, (uintptr_t *)ROM_ENTENDED_BOOT_DATA_INFO,
52 sizeof(struct rom_extended_boot_data));
53}
54
Dave Gerlacha89f7a42021-04-23 11:27:37 -050055#if defined(CONFIG_K3_LOAD_SYSFW) && CONFIG_IS_ENABLED(DM_MMC)
56void k3_mmc_stop_clock(void)
57{
58 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
59 struct mmc *mmc = find_mmc_device(0);
60
61 if (!mmc)
62 return;
63
64 mmc->saved_clock = mmc->clock;
65 mmc_set_clock(mmc, 0, true);
66 }
67}
68
69void k3_mmc_restart_clock(void)
70{
71 if (spl_boot_device() == BOOT_DEVICE_MMC1) {
72 struct mmc *mmc = find_mmc_device(0);
73
74 if (!mmc)
75 return;
76
77 mmc_set_clock(mmc, mmc->saved_clock, false);
78 }
79}
80#else
81void k3_mmc_stop_clock(void) {}
82void k3_mmc_restart_clock(void) {}
83#endif
84
Dave Gerlach96571ec2021-04-23 11:27:32 -050085void board_init_f(ulong dummy)
86{
Dave Gerlach8e0689b2021-04-23 11:27:36 -050087#if defined(CONFIG_K3_LOAD_SYSFW)
88 struct udevice *dev;
89 int ret;
90#endif
91
Dave Gerlach96571ec2021-04-23 11:27:32 -050092#if defined(CONFIG_CPU_V7R)
93 setup_k3_mpu_regions();
94#endif
95
Dave Gerlachb27a9f22021-04-23 11:27:35 -050096 /*
97 * Cannot delay this further as there is a chance that
98 * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
99 */
100 store_boot_info_from_rom();
101
Dave Gerlacheaef1292021-04-23 11:27:34 -0500102 ctrl_mmr_unlock();
103
Dave Gerlach96571ec2021-04-23 11:27:32 -0500104 /* Init DM early */
105 spl_early_init();
106
107 preloader_console_init();
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500108
109#if defined(CONFIG_K3_LOAD_SYSFW)
110 /*
111 * Process pinctrl for serial3 a.k.a. MAIN UART1 module and continue
112 * regardless of the result of pinctrl. Do this without probing the
113 * device, but instead by searching the device that would request the
114 * given sequence number if probed. The UART will be used by the system
115 * firmware (SYSFW) image for various purposes and SYSFW depends on us
116 * to initialize its pin settings.
117 */
118 ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev);
119 if (!ret)
120 pinctrl_select_state(dev, "default");
121
122 /*
123 * Load, start up, and configure system controller firmware.
124 * This will determine whether or not ROM has already loaded
125 * system firmware and if so, will only perform needed config
126 * and not attempt to load firmware again.
127 */
Dave Gerlacha89f7a42021-04-23 11:27:37 -0500128 k3_sysfw_loader(is_rom_loaded_sysfw(&bootdata), k3_mmc_stop_clock,
129 k3_mmc_restart_clock);
Dave Gerlach8e0689b2021-04-23 11:27:36 -0500130#endif
131
132 /* Output System Firmware version info */
133 k3_sysfw_print_ver();
Dave Gerlach96571ec2021-04-23 11:27:32 -0500134}
Keerthy05d670e2021-04-23 11:27:33 -0500135
136u32 spl_boot_mode(const u32 boot_device)
137{
138 switch (boot_device) {
139 case BOOT_DEVICE_MMC1:
140 return MMCSD_MODE_EMMCBOOT;
141
142 case BOOT_DEVICE_MMC2:
143 return MMCSD_MODE_FS;
144
145 default:
146 return MMCSD_MODE_RAW;
147 }
148}
149
150static u32 __get_backup_bootmedia(u32 main_devstat)
151{
152 u32 bkup_bootmode =
153 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
154 MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
155 u32 bkup_bootmode_cfg =
156 (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
157 MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
158
159 switch (bkup_bootmode) {
160 case BACKUP_BOOT_DEVICE_UART:
161 return BOOT_DEVICE_UART;
162
163 case BACKUP_BOOT_DEVICE_USB:
164 return BOOT_DEVICE_USB;
165
166 case BACKUP_BOOT_DEVICE_ETHERNET:
167 return BOOT_DEVICE_ETHERNET;
168
169 case BACKUP_BOOT_DEVICE_MMC:
170 if (bkup_bootmode_cfg)
171 return BOOT_DEVICE_MMC2;
172 return BOOT_DEVICE_MMC1;
173
174 case BACKUP_BOOT_DEVICE_SPI:
175 return BOOT_DEVICE_SPI;
176
177 case BACKUP_BOOT_DEVICE_I2C:
178 return BOOT_DEVICE_I2C;
179 };
180
181 return BOOT_DEVICE_RAM;
182}
183
184static u32 __get_primary_bootmedia(u32 main_devstat)
185{
186 u32 bootmode = (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
187 MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
188 u32 bootmode_cfg =
189 (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
190 MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
191
192 switch (bootmode) {
193 case BOOT_DEVICE_OSPI:
194 fallthrough;
195 case BOOT_DEVICE_QSPI:
196 fallthrough;
197 case BOOT_DEVICE_XSPI:
198 fallthrough;
199 case BOOT_DEVICE_SPI:
200 return BOOT_DEVICE_SPI;
201
202 case BOOT_DEVICE_ETHERNET_RGMII:
203 fallthrough;
204 case BOOT_DEVICE_ETHERNET_RMII:
205 return BOOT_DEVICE_ETHERNET;
206
207 case BOOT_DEVICE_EMMC:
208 return BOOT_DEVICE_MMC1;
209
210 case BOOT_DEVICE_MMC:
211 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
212 MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
213 return BOOT_DEVICE_MMC2;
214 return BOOT_DEVICE_MMC1;
215
216 case BOOT_DEVICE_NOBOOT:
217 return BOOT_DEVICE_RAM;
218 }
219
220 return bootmode;
221}
222
223u32 spl_boot_device(void)
224{
225 u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
226
227 if (bootindex == K3_PRIMARY_BOOTMODE)
228 return __get_primary_bootmedia(devstat);
229 else
230 return __get_backup_bootmedia(devstat);
231}
Dave Gerlach96571ec2021-04-23 11:27:32 -0500232#endif
Suman Anna320c0202021-04-23 11:27:38 -0500233
234#if defined(CONFIG_SYS_K3_SPL_ATF)
235
236#define AM64X_DEV_RTI8 127
237#define AM64X_DEV_RTI9 128
238#define AM64X_DEV_R5FSS0_CORE0 121
239#define AM64X_DEV_R5FSS0_CORE1 122
240
241void release_resources_for_core_shutdown(void)
242{
243 struct ti_sci_handle *ti_sci = get_ti_sci_handle();
244 struct ti_sci_dev_ops *dev_ops = &ti_sci->ops.dev_ops;
245 struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops;
246 int ret;
247 u32 i;
248
249 const u32 put_device_ids[] = {
250 AM64X_DEV_RTI9,
251 AM64X_DEV_RTI8,
252 };
253
254 /* Iterate through list of devices to put (shutdown) */
255 for (i = 0; i < ARRAY_SIZE(put_device_ids); i++) {
256 u32 id = put_device_ids[i];
257
258 ret = dev_ops->put_device(ti_sci, id);
259 if (ret)
260 panic("Failed to put device %u (%d)\n", id, ret);
261 }
262
263 const u32 put_core_ids[] = {
264 AM64X_DEV_R5FSS0_CORE1,
265 AM64X_DEV_R5FSS0_CORE0, /* Handle CPU0 after CPU1 */
266 };
267
268 /* Iterate through list of cores to put (shutdown) */
269 for (i = 0; i < ARRAY_SIZE(put_core_ids); i++) {
270 u32 id = put_core_ids[i];
271
272 /*
273 * Queue up the core shutdown request. Note that this call
274 * needs to be followed up by an actual invocation of an WFE
275 * or WFI CPU instruction.
276 */
277 ret = proc_ops->proc_shutdown_no_wait(ti_sci, id);
278 if (ret)
279 panic("Failed sending core %u shutdown message (%d)\n",
280 id, ret);
281 }
282}
283#endif