Prepare v2023.10
[platform/kernel/u-boot.git] / arch / arm / mach-imx / spl.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2014 Gateworks Corporation
4  * Copyright (C) 2011-2012 Freescale Semiconductor, Inc.
5  *
6  * Author: Tim Harvey <tharvey@gateworks.com>
7  */
8
9 #include <common.h>
10 #include <hang.h>
11 #include <init.h>
12 #include <log.h>
13 #include <asm/global_data.h>
14 #include <asm/io.h>
15 #include <asm/arch/imx-regs.h>
16 #include <asm/arch/sys_proto.h>
17 #include <asm/spl.h>
18 #include <spl.h>
19 #include <asm/mach-imx/hab.h>
20 #include <asm/mach-imx/boot_mode.h>
21 #include <g_dnl.h>
22 #include <linux/libfdt.h>
23 #include <memalign.h>
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 __weak int spl_board_boot_device(enum boot_device boot_dev_spl)
28 {
29         switch (boot_dev_spl) {
30 #if defined(CONFIG_MX7)
31         case SD1_BOOT:
32         case MMC1_BOOT:
33         case SD2_BOOT:
34         case MMC2_BOOT:
35         case SD3_BOOT:
36         case MMC3_BOOT:
37                 return BOOT_DEVICE_MMC1;
38 #elif defined(CONFIG_IMX8)
39         case MMC1_BOOT:
40                 return BOOT_DEVICE_MMC1;
41         case SD2_BOOT:
42                 return BOOT_DEVICE_MMC2_2;
43         case SD3_BOOT:
44                 return BOOT_DEVICE_MMC1;
45         case FLEXSPI_BOOT:
46                 return BOOT_DEVICE_SPI;
47 #elif defined(CONFIG_IMX8M)
48         case SD1_BOOT:
49         case MMC1_BOOT:
50                 return BOOT_DEVICE_MMC1;
51         case SD2_BOOT:
52         case MMC2_BOOT:
53                 return BOOT_DEVICE_MMC2;
54 #endif
55         case NAND_BOOT:
56                 return BOOT_DEVICE_NAND;
57         case SPI_NOR_BOOT:
58                 return BOOT_DEVICE_SPI;
59         case QSPI_BOOT:
60                 return BOOT_DEVICE_NOR;
61         case USB_BOOT:
62                 return BOOT_DEVICE_BOARD;
63         default:
64                 return BOOT_DEVICE_NONE;
65         }
66 }
67
68 #if defined(CONFIG_MX6)
69 /* determine boot device from SRC_SBMR1 (BOOT_CFG[4:1]) or SRC_GPR9 register */
70 u32 spl_boot_device(void)
71 {
72         unsigned int bmode = readl(&src_base->sbmr2);
73         u32 reg = imx6_src_get_boot_mode();
74
75         /*
76          * Check for BMODE if serial downloader is enabled
77          * BOOT_MODE - see IMX6DQRM Table 8-1
78          */
79         if (((bmode >> 24) & 0x03) == 0x01) /* Serial Downloader */
80                 return BOOT_DEVICE_BOARD;
81
82         /*
83          * The above method does not detect that the boot ROM used
84          * serial downloader in case the boot ROM decided to use the
85          * serial downloader as a fall back (primary boot source failed).
86          *
87          * Infer that the boot ROM used the USB serial downloader by
88          * checking whether the USB PHY is currently active... This
89          * assumes that SPL did not (yet) initialize the USB PHY...
90          */
91         if (is_usbotg_phy_active())
92                 return BOOT_DEVICE_BOARD;
93
94         /* BOOT_CFG1[7:4] - see IMX6DQRM Table 8-8 */
95         switch ((reg & IMX6_BMODE_MASK) >> IMX6_BMODE_SHIFT) {
96          /* EIM: See 8.5.1, Table 8-9 */
97         case IMX6_BMODE_EIM:
98                 /* BOOT_CFG1[3]: NOR/OneNAND Selection */
99                 switch ((reg & IMX6_BMODE_EIM_MASK) >> IMX6_BMODE_EIM_SHIFT) {
100                 case IMX6_BMODE_ONENAND:
101                         return BOOT_DEVICE_ONENAND;
102                 case IMX6_BMODE_NOR:
103                         return BOOT_DEVICE_NOR;
104                 break;
105                 }
106         /* Reserved: Used to force Serial Downloader */
107         case IMX6_BMODE_RESERVED:
108                 return BOOT_DEVICE_BOARD;
109         /* SATA: See 8.5.4, Table 8-20 */
110 #if !defined(CONFIG_MX6UL) && !defined(CONFIG_MX6ULL)
111         case IMX6_BMODE_SATA:
112                 return BOOT_DEVICE_SATA;
113 #endif
114         /* Serial ROM: See 8.5.5.1, Table 8-22 */
115         case IMX6_BMODE_SERIAL_ROM:
116                 /* BOOT_CFG4[2:0] */
117                 switch ((reg & IMX6_BMODE_SERIAL_ROM_MASK) >>
118                         IMX6_BMODE_SERIAL_ROM_SHIFT) {
119                 case IMX6_BMODE_ECSPI1:
120                 case IMX6_BMODE_ECSPI2:
121                 case IMX6_BMODE_ECSPI3:
122                 case IMX6_BMODE_ECSPI4:
123                 case IMX6_BMODE_ECSPI5:
124                         return BOOT_DEVICE_SPI;
125                 case IMX6_BMODE_I2C1:
126                 case IMX6_BMODE_I2C2:
127                 case IMX6_BMODE_I2C3:
128                         return BOOT_DEVICE_I2C;
129                 }
130                 break;
131         /* SD/eSD: 8.5.3, Table 8-15  */
132         case IMX6_BMODE_SD:
133         case IMX6_BMODE_ESD:
134                 return BOOT_DEVICE_MMC1;
135         /* MMC/eMMC: 8.5.3 */
136         case IMX6_BMODE_MMC:
137         case IMX6_BMODE_EMMC:
138                 return BOOT_DEVICE_MMC1;
139         /* NAND Flash: 8.5.2, Table 8-10 */
140         case IMX6_BMODE_NAND_MIN ... IMX6_BMODE_NAND_MAX:
141                 return BOOT_DEVICE_NAND;
142 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
143         /* QSPI boot */
144         case IMX6_BMODE_QSPI:
145                 return BOOT_DEVICE_SPI;
146 #endif
147         }
148         return BOOT_DEVICE_NONE;
149 }
150
151 #elif defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || defined(CONFIG_IMX8) || defined(CONFIG_IMX9)
152 /* Translate iMX7/i.MX8M boot device to the SPL boot device enumeration */
153 u32 spl_boot_device(void)
154 {
155 #if defined(CONFIG_MX7)
156         unsigned int bmode = readl(&src_base->sbmr2);
157
158         /*
159          * Check for BMODE if serial downloader is enabled
160          * BOOT_MODE - see IMX7DRM Table 6-24
161          */
162         if (((bmode >> 24) & 0x03) == 0x01) /* Serial Downloader */
163                 return BOOT_DEVICE_BOARD;
164
165         /*
166          * The above method does not detect that the boot ROM used
167          * serial downloader in case the boot ROM decided to use the
168          * serial downloader as a fall back (primary boot source failed).
169          *
170          * Infer that the boot ROM used the USB serial downloader by
171          * checking whether the USB PHY is currently active... This
172          * assumes that SPL did not (yet) initialize the USB PHY...
173          */
174         if (is_boot_from_usb())
175                 return BOOT_DEVICE_BOARD;
176 #endif
177
178         enum boot_device boot_device_spl = get_boot_device();
179
180         return spl_board_boot_device(boot_device_spl);
181 }
182 #endif /* CONFIG_MX7 || CONFIG_IMX8M || CONFIG_IMX8 */
183
184 #ifdef CONFIG_SPL_USB_GADGET
185 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
186 {
187         put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM + 0xfff, &dev->idProduct);
188
189         return 0;
190 }
191
192 #define SDPV_BCD_DEVICE 0x500
193 int g_dnl_get_board_bcd_device_number(int gcnum)
194 {
195         return SDPV_BCD_DEVICE;
196 }
197 #endif
198
199 #if defined(CONFIG_SPL_MMC)
200 /* called from spl_mmc to see type of boot mode for storage (RAW or FAT) */
201 u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
202 {
203 #if defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || defined(CONFIG_IMX8)
204         switch (get_boot_device()) {
205         /* for MMC return either RAW or FAT mode */
206         case SD1_BOOT:
207         case SD2_BOOT:
208         case SD3_BOOT:
209                 if (IS_ENABLED(CONFIG_SPL_FS_FAT))
210                         return MMCSD_MODE_FS;
211                 else
212                         return MMCSD_MODE_RAW;
213         case MMC1_BOOT:
214         case MMC2_BOOT:
215         case MMC3_BOOT:
216                 if (IS_ENABLED(CONFIG_SPL_FS_FAT))
217                         return MMCSD_MODE_FS;
218                 else if (IS_ENABLED(CONFIG_SUPPORT_EMMC_BOOT))
219                         return MMCSD_MODE_EMMCBOOT;
220                 else
221                         return MMCSD_MODE_RAW;
222         default:
223                 puts("spl: ERROR:  unsupported device\n");
224                 hang();
225         }
226 #else
227         switch (boot_device) {
228         /* for MMC return either RAW or FAT mode */
229         case BOOT_DEVICE_MMC1:
230         case BOOT_DEVICE_MMC2:
231         case BOOT_DEVICE_MMC2_2:
232                 if (IS_ENABLED(CONFIG_SPL_FS_FAT))
233                         return MMCSD_MODE_FS;
234                 else if (IS_ENABLED(CONFIG_SUPPORT_EMMC_BOOT))
235                         return MMCSD_MODE_EMMCBOOT;
236                 else
237                         return MMCSD_MODE_RAW;
238         default:
239                 puts("spl: ERROR:  unsupported device\n");
240                 hang();
241         }
242 #endif
243 }
244 #endif
245
246 #if defined(CONFIG_IMX_HAB)
247
248 /*
249  * +------------+  0x0 (DDR_UIMAGE_START) -
250  * |   Header   |                          |
251  * +------------+  0x40                    |
252  * |            |                          |
253  * |            |                          |
254  * |            |                          |
255  * |            |                          |
256  * | Image Data |                          |
257  * .            |                          |
258  * .            |                           > Stuff to be authenticated ----+
259  * .            |                          |                                |
260  * |            |                          |                                |
261  * |            |                          |                                |
262  * +------------+                          |                                |
263  * |            |                          |                                |
264  * | Fill Data  |                          |                                |
265  * |            |                          |                                |
266  * +------------+ Align to ALIGN_SIZE      |                                |
267  * |    IVT     |                          |                                |
268  * +------------+ + IVT_SIZE              -                                 |
269  * |            |                                                           |
270  * |  CSF DATA  | <---------------------------------------------------------+
271  * |            |
272  * +------------+
273  * |            |
274  * | Fill Data  |
275  * |            |
276  * +------------+ + CSF_PAD_SIZE
277  */
278
279 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
280 {
281         typedef void __noreturn (*image_entry_noargs_t)(void);
282         uint32_t offset;
283
284         image_entry_noargs_t image_entry =
285                 (image_entry_noargs_t)(unsigned long)spl_image->entry_point;
286
287         debug("image entry point: 0x%lX\n", spl_image->entry_point);
288
289         if (spl_image->flags & SPL_FIT_FOUND) {
290                 image_entry();
291         } else {
292                 /*
293                  * HAB looks for the CSF at the end of the authenticated
294                  * data therefore, we need to subtract the size of the
295                  * CSF from the actual filesize
296                  */
297                 offset = spl_image->size - CONFIG_CSF_SIZE;
298                 if (!imx_hab_authenticate_image(spl_image->load_addr,
299                                                 offset + IVT_SIZE +
300                                                 CSF_PAD_SIZE, offset)) {
301                         image_entry();
302                 } else {
303                         panic("spl: ERROR:  image authentication fail\n");
304                 }
305         }
306 }
307
308 #if !defined(CONFIG_SPL_FIT_SIGNATURE)
309 ulong board_spl_fit_size_align(ulong size)
310 {
311         /*
312          * HAB authenticate_image requests the IVT offset is
313          * aligned to 0x1000
314          */
315
316         size = ALIGN(size, 0x1000);
317         size += CONFIG_CSF_SIZE;
318
319         if (size > CONFIG_SYS_BOOTM_LEN)
320                 panic("spl: ERROR: image too big\n");
321
322         return size;
323 }
324 #endif
325
326 void *board_spl_fit_buffer_addr(ulong fit_size, int sectors, int bl_len)
327 {
328 #if defined(CONFIG_SPL_LOAD_FIT_ADDRESS)
329         return (void *)CONFIG_SPL_LOAD_FIT_ADDRESS;
330 #else
331         return (void *)(CONFIG_TEXT_BASE + CONFIG_SYS_BOOTM_LEN);
332 #endif
333 }
334 /*
335  * read the address where the IVT header must sit
336  * from IVT image header, loaded from SPL into
337  * an malloced buffer and copy the IVT header
338  * to this address
339  */
340 void *spl_load_simple_fit_fix_load(const void *fit)
341 {
342         struct ivt *ivt;
343         unsigned long offset;
344         unsigned long size;
345         u8 *tmp = (u8 *)fit;
346
347         offset = ALIGN(fdt_totalsize(fit), 0x1000);
348         size = ALIGN(fdt_totalsize(fit), 4);
349         size = board_spl_fit_size_align(size);
350         tmp += offset;
351         ivt = (struct ivt *)tmp;
352
353         debug("%s: ivt: %p offset: %lx size: %lx\n", __func__, ivt, offset, size);
354         debug("%s: ivt self: %x\n", __func__, ivt->self);
355
356         if (imx_hab_authenticate_image((uintptr_t)fit, (uintptr_t)ivt, offset))
357                 panic("spl: ERROR: image authentication unsuccessful\n");
358
359         return (void *)fit;
360 }
361 #endif /* CONFIG_IMX_HAB */
362
363 #if defined(CONFIG_MX6) && defined(CONFIG_SPL_OS_BOOT)
364 int dram_init_banksize(void)
365 {
366         gd->bd->bi_dram[0].start = CFG_SYS_SDRAM_BASE;
367         gd->bd->bi_dram[0].size = imx_ddr_size();
368
369         return 0;
370 }
371 #endif