spl: mmc: extend spl_mmc_boot_mode() to take mmc argument
[platform/kernel/u-boot.git] / arch / arm / mach-k3 / am642_init.c
1 // 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 <fdt_support.h>
12 #include <spl.h>
13 #include <asm/io.h>
14 #include <asm/arch/hardware.h>
15 #include <asm/arch/sysfw-loader.h>
16 #include <asm/arch/sys_proto.h>
17 #include "common.h"
18 #include <asm/arch/sys_proto.h>
19 #include <linux/soc/ti/ti_sci_protocol.h>
20 #include <dm.h>
21 #include <dm/uclass-internal.h>
22 #include <dm/pinctrl.h>
23 #include <mmc.h>
24 #include <dm/root.h>
25
26 #if defined(CONFIG_SPL_BUILD)
27
28 static void ctrl_mmr_unlock(void)
29 {
30         /* Unlock all PADCFG_MMR1 module registers */
31         mmr_unlock(PADCFG_MMR1_BASE, 1);
32
33         /* Unlock all CTRL_MMR0 module registers */
34         mmr_unlock(CTRL_MMR0_BASE, 0);
35         mmr_unlock(CTRL_MMR0_BASE, 1);
36         mmr_unlock(CTRL_MMR0_BASE, 2);
37         mmr_unlock(CTRL_MMR0_BASE, 3);
38         mmr_unlock(CTRL_MMR0_BASE, 5);
39         mmr_unlock(CTRL_MMR0_BASE, 6);
40
41         /* Unlock all MCU_PADCFG_MMR1 module registers */
42         mmr_unlock(MCU_PADCFG_MMR1_BASE, 1);
43 }
44
45 /*
46  * This uninitialized global variable would normal end up in the .bss section,
47  * but the .bss is cleared between writing and reading this variable, so move
48  * it to the .data section.
49  */
50 u32 bootindex __section(".data");
51 static struct rom_extended_boot_data bootdata __section(".data");
52
53 static void store_boot_info_from_rom(void)
54 {
55         bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX);
56         memcpy(&bootdata, (uintptr_t *)ROM_ENTENDED_BOOT_DATA_INFO,
57                sizeof(struct rom_extended_boot_data));
58 }
59
60 #if defined(CONFIG_K3_LOAD_SYSFW) && CONFIG_IS_ENABLED(DM_MMC)
61 void k3_mmc_stop_clock(void)
62 {
63         if (spl_boot_device() == BOOT_DEVICE_MMC1) {
64                 struct mmc *mmc = find_mmc_device(0);
65
66                 if (!mmc)
67                         return;
68
69                 mmc->saved_clock = mmc->clock;
70                 mmc_set_clock(mmc, 0, true);
71         }
72 }
73
74 void k3_mmc_restart_clock(void)
75 {
76         if (spl_boot_device() == BOOT_DEVICE_MMC1) {
77                 struct mmc *mmc = find_mmc_device(0);
78
79                 if (!mmc)
80                         return;
81
82                 mmc_set_clock(mmc, mmc->saved_clock, false);
83         }
84 }
85 #else
86 void k3_mmc_stop_clock(void) {}
87 void k3_mmc_restart_clock(void) {}
88 #endif
89
90 #ifdef CONFIG_SPL_OF_LIST
91 void do_dt_magic(void)
92 {
93         int ret, rescan;
94
95         if (IS_ENABLED(CONFIG_TI_I2C_BOARD_DETECT))
96                 do_board_detect();
97
98         /*
99          * Board detection has been done.
100          * Let us see if another dtb wouldn't be a better match
101          * for our board
102          */
103         if (IS_ENABLED(CONFIG_CPU_V7R)) {
104                 ret = fdtdec_resetup(&rescan);
105                 if (!ret && rescan) {
106                         dm_uninit();
107                         dm_init_and_scan(true);
108                 }
109         }
110 }
111 #endif
112
113 #if CONFIG_IS_ENABLED(USB_STORAGE)
114 static int fixup_usb_boot(const void *fdt_blob)
115 {
116         int ret = 0;
117
118         switch (spl_boot_device()) {
119         case BOOT_DEVICE_USB:
120                 /*
121                  * If the boot mode is host, fixup the dr_mode to host
122                  * before cdns3 bind takes place
123                  */
124                 ret = fdt_find_and_setprop((void *)fdt_blob,
125                                            "/bus@f4000/cdns-usb@f900000/usb@f400000",
126                                            "dr_mode", "host", 5, 0);
127                 if (ret)
128                         printf("%s: fdt_find_and_setprop() failed:%d\n",
129                                __func__, ret);
130                 fallthrough;
131         default:
132                 break;
133         }
134
135         return ret;
136 }
137
138 int fdtdec_board_setup(const void *fdt_blob)
139 {
140         /* Can use the pointer from the function parameters */
141         return fixup_usb_boot(fdt_blob);
142 }
143 #endif
144
145 void board_init_f(ulong dummy)
146 {
147 #if defined(CONFIG_K3_LOAD_SYSFW) || defined(CONFIG_K3_AM64_DDRSS)
148         struct udevice *dev;
149         int ret;
150 #endif
151
152 #if defined(CONFIG_CPU_V7R)
153         setup_k3_mpu_regions();
154 #endif
155
156         /*
157          * Cannot delay this further as there is a chance that
158          * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section.
159          */
160         store_boot_info_from_rom();
161
162         ctrl_mmr_unlock();
163
164         /* Init DM early */
165         spl_early_init();
166
167         preloader_console_init();
168
169         do_dt_magic();
170
171 #if defined(CONFIG_K3_LOAD_SYSFW)
172         /*
173          * Process pinctrl for serial3 a.k.a. MAIN UART1 module and continue
174          * regardless of the result of pinctrl. Do this without probing the
175          * device, but instead by searching the device that would request the
176          * given sequence number if probed. The UART will be used by the system
177          * firmware (SYSFW) image for various purposes and SYSFW depends on us
178          * to initialize its pin settings.
179          */
180         ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev);
181         if (!ret)
182                 pinctrl_select_state(dev, "default");
183
184         /*
185          * Load, start up, and configure system controller firmware.
186          * This will determine whether or not ROM has already loaded
187          * system firmware and if so, will only perform needed config
188          * and not attempt to load firmware again.
189          */
190         k3_sysfw_loader(is_rom_loaded_sysfw(&bootdata), k3_mmc_stop_clock,
191                         k3_mmc_restart_clock);
192 #endif
193
194         /* Output System Firmware version info */
195         k3_sysfw_print_ver();
196
197 #if defined(CONFIG_K3_AM64_DDRSS)
198         ret = uclass_get_device(UCLASS_RAM, 0, &dev);
199         if (ret)
200                 panic("DRAM init failed: %d\n", ret);
201 #endif
202         if (IS_ENABLED(CONFIG_SPL_ETH) && IS_ENABLED(CONFIG_TI_AM65_CPSW_NUSS) &&
203             spl_boot_device() == BOOT_DEVICE_ETHERNET) {
204                 struct udevice *cpswdev;
205
206                 if (uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(am65_cpsw_nuss), &cpswdev))
207                         printf("Failed to probe am65_cpsw_nuss driver\n");
208         }
209 }
210
211 u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
212 {
213         switch (boot_device) {
214         case BOOT_DEVICE_MMC1:
215                 return MMCSD_MODE_EMMCBOOT;
216
217         case BOOT_DEVICE_MMC2:
218                 return MMCSD_MODE_FS;
219
220         default:
221                 return MMCSD_MODE_RAW;
222         }
223 }
224
225 static u32 __get_backup_bootmedia(u32 main_devstat)
226 {
227         u32 bkup_bootmode =
228             (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >>
229             MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT;
230         u32 bkup_bootmode_cfg =
231             (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >>
232             MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT;
233
234         switch (bkup_bootmode) {
235         case BACKUP_BOOT_DEVICE_UART:
236                 return BOOT_DEVICE_UART;
237
238         case BACKUP_BOOT_DEVICE_DFU:
239                 if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK)
240                         return BOOT_DEVICE_USB;
241                 return BOOT_DEVICE_DFU;
242
243
244         case BACKUP_BOOT_DEVICE_ETHERNET:
245                 return BOOT_DEVICE_ETHERNET;
246
247         case BACKUP_BOOT_DEVICE_MMC:
248                 if (bkup_bootmode_cfg)
249                         return BOOT_DEVICE_MMC2;
250                 return BOOT_DEVICE_MMC1;
251
252         case BACKUP_BOOT_DEVICE_SPI:
253                 return BOOT_DEVICE_SPI;
254
255         case BACKUP_BOOT_DEVICE_I2C:
256                 return BOOT_DEVICE_I2C;
257         };
258
259         return BOOT_DEVICE_RAM;
260 }
261
262 static u32 __get_primary_bootmedia(u32 main_devstat)
263 {
264         u32 bootmode = (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >>
265             MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT;
266         u32 bootmode_cfg =
267             (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >>
268             MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT;
269
270         switch (bootmode) {
271         case BOOT_DEVICE_OSPI:
272                 fallthrough;
273         case BOOT_DEVICE_QSPI:
274                 fallthrough;
275         case BOOT_DEVICE_XSPI:
276                 fallthrough;
277         case BOOT_DEVICE_SPI:
278                 return BOOT_DEVICE_SPI;
279
280         case BOOT_DEVICE_ETHERNET_RGMII:
281                 fallthrough;
282         case BOOT_DEVICE_ETHERNET_RMII:
283                 return BOOT_DEVICE_ETHERNET;
284
285         case BOOT_DEVICE_EMMC:
286                 return BOOT_DEVICE_MMC1;
287
288         case BOOT_DEVICE_MMC:
289                 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >>
290                      MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT)
291                         return BOOT_DEVICE_MMC2;
292                 return BOOT_DEVICE_MMC1;
293
294         case BOOT_DEVICE_DFU:
295                 if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >>
296                     MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT)
297                         return BOOT_DEVICE_USB;
298                 return BOOT_DEVICE_DFU;
299
300         case BOOT_DEVICE_NOBOOT:
301                 return BOOT_DEVICE_RAM;
302         }
303
304         return bootmode;
305 }
306
307 u32 spl_boot_device(void)
308 {
309         u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT);
310
311         if (bootindex == K3_PRIMARY_BOOTMODE)
312                 return __get_primary_bootmedia(devstat);
313         else
314                 return __get_backup_bootmedia(devstat);
315 }
316 #endif
317
318 #if defined(CONFIG_SYS_K3_SPL_ATF)
319
320 #define AM64X_DEV_RTI8                  127
321 #define AM64X_DEV_RTI9                  128
322 #define AM64X_DEV_R5FSS0_CORE0          121
323 #define AM64X_DEV_R5FSS0_CORE1          122
324
325 void release_resources_for_core_shutdown(void)
326 {
327         struct ti_sci_handle *ti_sci = get_ti_sci_handle();
328         struct ti_sci_dev_ops *dev_ops = &ti_sci->ops.dev_ops;
329         struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops;
330         int ret;
331         u32 i;
332
333         const u32 put_device_ids[] = {
334                 AM64X_DEV_RTI9,
335                 AM64X_DEV_RTI8,
336         };
337
338         /* Iterate through list of devices to put (shutdown) */
339         for (i = 0; i < ARRAY_SIZE(put_device_ids); i++) {
340                 u32 id = put_device_ids[i];
341
342                 ret = dev_ops->put_device(ti_sci, id);
343                 if (ret)
344                         panic("Failed to put device %u (%d)\n", id, ret);
345         }
346
347         const u32 put_core_ids[] = {
348                 AM64X_DEV_R5FSS0_CORE1,
349                 AM64X_DEV_R5FSS0_CORE0, /* Handle CPU0 after CPU1 */
350         };
351
352         /* Iterate through list of cores to put (shutdown) */
353         for (i = 0; i < ARRAY_SIZE(put_core_ids); i++) {
354                 u32 id = put_core_ids[i];
355
356                 /*
357                  * Queue up the core shutdown request. Note that this call
358                  * needs to be followed up by an actual invocation of an WFE
359                  * or WFI CPU instruction.
360                  */
361                 ret = proc_ops->proc_shutdown_no_wait(ti_sci, id);
362                 if (ret)
363                         panic("Failed sending core %u shutdown message (%d)\n",
364                               id, ret);
365         }
366 }
367 #endif