blob: bc291dcd1296c3dbfdc308eaf8fd6444e829e14f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tim Harvey354a7e32014-06-02 16:13:20 -07002/*
3 * Copyright (C) 2014 Gateworks Corporation
4 * Copyright (C) 2011-2012 Freescale Semiconductor, Inc.
5 *
6 * Author: Tim Harvey <tharvey@gateworks.com>
Tim Harvey354a7e32014-06-02 16:13:20 -07007 */
8
Tom Rinidec7ea02024-05-20 13:35:03 -06009#include <config.h>
Simon Glassf11478f2019-12-28 10:45:07 -070010#include <hang.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Tim Harvey354a7e32014-06-02 16:13:20 -070014#include <asm/io.h>
15#include <asm/arch/imx-regs.h>
Jagan Tekie5f970b2017-02-24 15:45:12 +053016#include <asm/arch/sys_proto.h>
Tim Harvey354a7e32014-06-02 16:13:20 -070017#include <asm/spl.h>
18#include <spl.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020019#include <asm/mach-imx/hab.h>
Uri Mashiach5dd46892017-09-24 09:00:24 +030020#include <asm/mach-imx/boot_mode.h>
Fabio Estevama0e462b2017-09-05 20:46:39 -030021#include <g_dnl.h>
Alexandru Gagniuc44af2a52021-01-20 10:46:49 -060022#include <linux/libfdt.h>
Marek Vasuta7416eb2023-05-28 23:00:30 +020023#include <memalign.h>
Tim Harvey354a7e32014-06-02 16:13:20 -070024
Jagan Teki7bb61bb2017-08-28 16:45:47 +053025DECLARE_GLOBAL_DATA_PTR;
26
Peng Fancffe7322019-08-27 06:25:07 +000027__weak int spl_board_boot_device(enum boot_device boot_dev_spl)
28{
Ye Li732d6e62022-07-26 16:40:34 +080029 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 }
Peng Fancffe7322019-08-27 06:25:07 +000066}
67
Tim Harvey354a7e32014-06-02 16:13:20 -070068#if defined(CONFIG_MX6)
Nikita Kiryanov9fba8422014-10-29 19:28:33 +020069/* determine boot device from SRC_SBMR1 (BOOT_CFG[4:1]) or SRC_GPR9 register */
Tim Harvey354a7e32014-06-02 16:13:20 -070070u32 spl_boot_device(void)
71{
Jagan Tekic981a942017-02-24 15:45:15 +053072 unsigned int bmode = readl(&src_base->sbmr2);
Jagan Tekie5f970b2017-02-24 15:45:12 +053073 u32 reg = imx6_src_get_boot_mode();
Tim Harvey354a7e32014-06-02 16:13:20 -070074
Stefano Babic587e72e2015-12-11 17:30:42 +010075 /*
76 * Check for BMODE if serial downloader is enabled
77 * BOOT_MODE - see IMX6DQRM Table 8-1
78 */
Stefan Agner15390962016-12-27 17:01:42 +010079 if (((bmode >> 24) & 0x03) == 0x01) /* Serial Downloader */
Stefan Agnera0a50ce2017-08-15 17:49:43 -070080 return BOOT_DEVICE_BOARD;
Jagan Teki32a71142017-02-24 15:45:14 +053081
Stefan Agner47f547e2017-09-13 14:29:45 -070082 /*
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
Tim Harvey354a7e32014-06-02 16:13:20 -070094 /* BOOT_CFG1[7:4] - see IMX6DQRM Table 8-8 */
Jagan Teki32a71142017-02-24 15:45:14 +053095 switch ((reg & IMX6_BMODE_MASK) >> IMX6_BMODE_SHIFT) {
Tim Harvey354a7e32014-06-02 16:13:20 -070096 /* EIM: See 8.5.1, Table 8-9 */
Harald Seiler808c5d62021-12-01 10:02:54 +010097 case IMX6_BMODE_EIM:
Tim Harvey354a7e32014-06-02 16:13:20 -070098 /* BOOT_CFG1[3]: NOR/OneNAND Selection */
Harald Seiler808c5d62021-12-01 10:02:54 +010099 switch ((reg & IMX6_BMODE_EIM_MASK) >> IMX6_BMODE_EIM_SHIFT) {
Jagan Teki32a71142017-02-24 15:45:14 +0530100 case IMX6_BMODE_ONENAND:
Tim Harvey354a7e32014-06-02 16:13:20 -0700101 return BOOT_DEVICE_ONENAND;
Jagan Teki32a71142017-02-24 15:45:14 +0530102 case IMX6_BMODE_NOR:
Tim Harvey354a7e32014-06-02 16:13:20 -0700103 return BOOT_DEVICE_NOR;
104 break;
Jagan Teki32a71142017-02-24 15:45:14 +0530105 }
Stefan Agner15390962016-12-27 17:01:42 +0100106 /* Reserved: Used to force Serial Downloader */
Stefan Agner53456f52017-08-15 17:49:42 -0700107 case IMX6_BMODE_RESERVED:
Stefan Agnera0a50ce2017-08-15 17:49:43 -0700108 return BOOT_DEVICE_BOARD;
Tim Harvey354a7e32014-06-02 16:13:20 -0700109 /* SATA: See 8.5.4, Table 8-20 */
Stefan Agner34da4732017-08-29 09:10:11 -0700110#if !defined(CONFIG_MX6UL) && !defined(CONFIG_MX6ULL)
Jagan Teki32a71142017-02-24 15:45:14 +0530111 case IMX6_BMODE_SATA:
Tim Harvey354a7e32014-06-02 16:13:20 -0700112 return BOOT_DEVICE_SATA;
Stefan Agner34da4732017-08-29 09:10:11 -0700113#endif
Tim Harvey354a7e32014-06-02 16:13:20 -0700114 /* Serial ROM: See 8.5.5.1, Table 8-22 */
Jagan Teki32a71142017-02-24 15:45:14 +0530115 case IMX6_BMODE_SERIAL_ROM:
Tim Harvey354a7e32014-06-02 16:13:20 -0700116 /* BOOT_CFG4[2:0] */
Jagan Teki32a71142017-02-24 15:45:14 +0530117 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:
Tim Harvey354a7e32014-06-02 16:13:20 -0700124 return BOOT_DEVICE_SPI;
Jagan Teki32a71142017-02-24 15:45:14 +0530125 case IMX6_BMODE_I2C1:
126 case IMX6_BMODE_I2C2:
127 case IMX6_BMODE_I2C3:
Tim Harvey354a7e32014-06-02 16:13:20 -0700128 return BOOT_DEVICE_I2C;
129 }
130 break;
131 /* SD/eSD: 8.5.3, Table 8-15 */
Jagan Teki32a71142017-02-24 15:45:14 +0530132 case IMX6_BMODE_SD:
133 case IMX6_BMODE_ESD:
Adam Forda2a91e22019-10-08 07:56:44 -0500134 return BOOT_DEVICE_MMC1;
135 /* MMC/eMMC: 8.5.3 */
Jagan Teki32a71142017-02-24 15:45:14 +0530136 case IMX6_BMODE_MMC:
137 case IMX6_BMODE_EMMC:
Adam Forda2a91e22019-10-08 07:56:44 -0500138 return BOOT_DEVICE_MMC1;
Jagan Teki4191b222017-02-24 15:45:13 +0530139 /* NAND Flash: 8.5.2, Table 8-10 */
Eran Matityahu5fa7ca62017-12-14 20:20:02 +0200140 case IMX6_BMODE_NAND_MIN ... IMX6_BMODE_NAND_MAX:
Tim Harvey354a7e32014-06-02 16:13:20 -0700141 return BOOT_DEVICE_NAND;
Stefan Roesefe41e322019-09-20 18:09:08 +0200142#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
143 /* QSPI boot */
144 case IMX6_BMODE_QSPI:
145 return BOOT_DEVICE_SPI;
146#endif
Tim Harvey354a7e32014-06-02 16:13:20 -0700147 }
148 return BOOT_DEVICE_NONE;
149}
Fabio Estevama0e462b2017-09-05 20:46:39 -0300150
Peng Fanbbcd2c42022-07-26 16:40:39 +0800151#elif defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || defined(CONFIG_IMX8) || defined(CONFIG_IMX9)
Peng Fan39945c12018-11-20 10:19:25 +0000152/* Translate iMX7/i.MX8M boot device to the SPL boot device enumeration */
Uri Mashiach5dd46892017-09-24 09:00:24 +0300153u32 spl_boot_device(void)
154{
Eran Matityahu52fef1c2018-03-06 16:38:00 +0200155#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
Uri Mashiach5dd46892017-09-24 09:00:24 +0300178 enum boot_device boot_device_spl = get_boot_device();
179
Ye Li732d6e62022-07-26 16:40:34 +0800180 return spl_board_boot_device(boot_device_spl);
Uri Mashiach5dd46892017-09-24 09:00:24 +0300181}
Peng Fan9d5bc072018-12-21 06:21:23 +0000182#endif /* CONFIG_MX7 || CONFIG_IMX8M || CONFIG_IMX8 */
Uri Mashiach5dd46892017-09-24 09:00:24 +0300183
Jean-Jacques Hiblot6a228522019-01-10 15:44:13 +0100184#ifdef CONFIG_SPL_USB_GADGET
Fabio Estevama0e462b2017-09-05 20:46:39 -0300185int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
186{
Maxime Ripard7f78b9d2017-09-07 08:58:08 +0200187 put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM + 0xfff, &dev->idProduct);
Fabio Estevama0e462b2017-09-05 20:46:39 -0300188
189 return 0;
190}
Peng Fan8f97c262020-08-18 18:16:47 +0800191
192#define SDPV_BCD_DEVICE 0x500
193int g_dnl_get_board_bcd_device_number(int gcnum)
194{
195 return SDPV_BCD_DEVICE;
196}
Fabio Estevama0e462b2017-09-05 20:46:39 -0300197#endif
Tim Harvey354a7e32014-06-02 16:13:20 -0700198
Simon Glassb58bfe02021-08-08 12:20:09 -0600199#if defined(CONFIG_SPL_MMC)
Fabio Estevam550b52e2018-02-15 12:41:40 -0200200/* called from spl_mmc to see type of boot mode for storage (RAW or FAT) */
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100201u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Fabio Estevam550b52e2018-02-15 12:41:40 -0200202{
Peng Fan1dd4cc32019-10-16 10:24:25 +0000203#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:
Harald Seilerfbbb3182020-04-23 13:07:53 +0200209 if (IS_ENABLED(CONFIG_SPL_FS_FAT))
Harald Seiler51526812020-04-23 13:07:52 +0200210 return MMCSD_MODE_FS;
211 else
212 return MMCSD_MODE_RAW;
Peng Fan1dd4cc32019-10-16 10:24:25 +0000213 case MMC1_BOOT:
214 case MMC2_BOOT:
215 case MMC3_BOOT:
Harald Seilerfbbb3182020-04-23 13:07:53 +0200216 if (IS_ENABLED(CONFIG_SPL_FS_FAT))
Harald Seiler51526812020-04-23 13:07:52 +0200217 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;
Peng Fan1dd4cc32019-10-16 10:24:25 +0000222 default:
223 puts("spl: ERROR: unsupported device\n");
224 hang();
225 }
226#else
Anatolij Gustschin62499ff2020-04-23 13:07:49 +0200227 switch (boot_device) {
Fabio Estevam550b52e2018-02-15 12:41:40 -0200228 /* for MMC return either RAW or FAT mode */
229 case BOOT_DEVICE_MMC1:
230 case BOOT_DEVICE_MMC2:
Peng Fan9d5bc072018-12-21 06:21:23 +0000231 case BOOT_DEVICE_MMC2_2:
Harald Seiler51526812020-04-23 13:07:52 +0200232 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;
Fabio Estevam550b52e2018-02-15 12:41:40 -0200238 default:
239 puts("spl: ERROR: unsupported device\n");
240 hang();
241 }
Peng Fan1dd4cc32019-10-16 10:24:25 +0000242#endif
Fabio Estevam550b52e2018-02-15 12:41:40 -0200243}
244#endif
245
Stefano Babicf8b509b2019-09-20 08:47:53 +0200246#if defined(CONFIG_IMX_HAB)
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100247
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000248/*
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
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100279__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
280{
281 typedef void __noreturn (*image_entry_noargs_t)(void);
Bryan O'Donoghue88919962018-01-12 12:40:01 +0000282 uint32_t offset;
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100283
284 image_entry_noargs_t image_entry =
285 (image_entry_noargs_t)(unsigned long)spl_image->entry_point;
286
Tom Rinif1c2fc02017-01-11 10:45:48 -0500287 debug("image entry point: 0x%lX\n", spl_image->entry_point);
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100288
Ye Li9d5b0f42018-11-17 09:10:25 +0000289 if (spl_image->flags & SPL_FIT_FOUND) {
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100290 image_entry();
291 } else {
Ye Li9d5b0f42018-11-17 09:10:25 +0000292 /*
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 {
Marek Vasut579fd512020-05-30 20:29:00 +0200303 panic("spl: ERROR: image authentication fail\n");
Ye Li9d5b0f42018-11-17 09:10:25 +0000304 }
305 }
306}
307
Ricardo Salvetid7d649c2019-09-02 18:18:52 -0300308#if !defined(CONFIG_SPL_FIT_SIGNATURE)
Ye Li9d5b0f42018-11-17 09:10:25 +0000309ulong 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
Marek Vasuta7416eb2023-05-28 23:00:30 +0200319 if (size > CONFIG_SYS_BOOTM_LEN)
320 panic("spl: ERROR: image too big\n");
Ye Li9d5b0f42018-11-17 09:10:25 +0000321
Marek Vasuta7416eb2023-05-28 23:00:30 +0200322 return size;
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100323}
Ricardo Salvetid7d649c2019-09-02 18:18:52 -0300324#endif
Sven Ebenfeldeba5e332016-11-06 16:37:55 +0100325
Heiko Schocher7d7c3672021-08-17 08:17:18 +0200326void *board_spl_fit_buffer_addr(ulong fit_size, int sectors, int bl_len)
327{
Marek Vasuta7416eb2023-05-28 23:00:30 +0200328#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);
Jagan Teki7bb61bb2017-08-28 16:45:47 +0530332#endif
Jagan Teki7bb61bb2017-08-28 16:45:47 +0530333}
Heiko Schocherf3c450b2021-08-06 06:44:27 +0200334/*
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 */
340void *spl_load_simple_fit_fix_load(const void *fit)
341{
342 struct ivt *ivt;
Heiko Schocherf3c450b2021-08-06 06:44:27 +0200343 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;
Marek Vasuta7416eb2023-05-28 23:00:30 +0200352
Heiko Schocherf3c450b2021-08-06 06:44:27 +0200353 debug("%s: ivt: %p offset: %lx size: %lx\n", __func__, ivt, offset, size);
354 debug("%s: ivt self: %x\n", __func__, ivt->self);
Marek Vasuta7416eb2023-05-28 23:00:30 +0200355
Marek Vasut7dc11f72023-08-31 16:57:24 +0200356 if (imx_hab_authenticate_image((uintptr_t)fit, (uintptr_t)size, offset))
Marek Vasuta7416eb2023-05-28 23:00:30 +0200357 panic("spl: ERROR: image authentication unsuccessful\n");
Heiko Schocherf3c450b2021-08-06 06:44:27 +0200358
Marek Vasuta7416eb2023-05-28 23:00:30 +0200359 return (void *)fit;
Heiko Schocherf3c450b2021-08-06 06:44:27 +0200360}
Marek Vasuta7416eb2023-05-28 23:00:30 +0200361#endif /* CONFIG_IMX_HAB */
362
363#if defined(CONFIG_MX6) && defined(CONFIG_SPL_OS_BOOT)
364int 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