1 // SPDX-License-Identifier: GPL-2.0+
4 * Texas Instruments, <www.ti.com>
13 #include <linux/compiler.h>
15 #include <asm/u-boot.h>
19 #include <imx_container.h>
21 static ulong h_spl_load_read(struct spl_load_info *load, ulong off,
22 ulong size, void *buf)
24 struct blk_desc *bd = load->priv;
25 lbaint_t sector = off >> bd->log2blksz;
26 lbaint_t count = size >> bd->log2blksz;
28 return blk_dread(bd, sector, count, buf) << bd->log2blksz;
31 static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
33 #if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
35 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
42 int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
43 struct spl_boot_device *bootdev,
44 struct mmc *mmc, unsigned long sector)
47 struct blk_desc *bd = mmc_get_blk_desc(mmc);
48 struct spl_load_info load;
51 spl_set_bl_len(&load, bd->blksz);
52 load.read = h_spl_load_read;
53 ret = spl_load(spl_image, bootdev, &load, 0, sector << bd->log2blksz);
55 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
56 puts("mmc_load_image_raw_sector: mmc block read error\n");
64 static int spl_mmc_get_device_index(u32 boot_device)
66 switch (boot_device) {
67 case BOOT_DEVICE_MMC1:
69 case BOOT_DEVICE_MMC2:
70 case BOOT_DEVICE_MMC2_2:
74 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
75 printf("spl: unsupported mmc boot device.\n");
81 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
85 mmc_dev = spl_mmc_get_device_index(boot_device);
89 #if CONFIG_IS_ENABLED(DM_MMC)
90 err = mmc_init_device(mmc_dev);
92 err = mmc_initialize(NULL);
95 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
96 printf("spl: could not initialize mmc. error: %d\n", err);
100 *mmcp = find_mmc_device(mmc_dev);
101 err = *mmcp ? 0 : -ENODEV;
103 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
104 printf("spl: could not find mmc device %d. error: %d\n",
113 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
114 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
115 struct spl_boot_device *bootdev,
116 struct mmc *mmc, int partition,
117 unsigned long sector)
119 struct disk_partition info;
122 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
124 /* Only support MBR so DOS_ENTRY_NUMBERS */
125 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
126 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
130 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
131 partition = type_part;
137 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
139 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
140 puts("spl: partition error\n");
145 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
146 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
148 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
153 #if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
154 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
155 struct spl_boot_device *bootdev,
160 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
163 count = blk_dread(mmc_get_blk_desc(mmc),
164 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
165 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
166 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
167 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
168 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
169 puts("mmc_load_image_raw_os: mmc block read error\n");
173 #endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
175 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
176 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
180 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
181 puts("Expected image is not found. Trying to start U-Boot\n");
188 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
189 struct spl_boot_device *bootdev,
196 #ifndef CONFIG_SPL_OS_BOOT
197 int spl_start_uboot(void)
203 #ifdef CONFIG_SYS_MMCSD_FS_BOOT
204 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
205 struct spl_boot_device *bootdev,
207 const char *filename)
211 __maybe_unused int partition = CONFIG_SYS_MMCSD_FS_BOOT_PARTITION;
213 #if CONFIG_SYS_MMCSD_FS_BOOT_PARTITION == -1
215 struct disk_partition info;
216 debug("Checking for the first MBR bootable partition\n");
217 for (int type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
218 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
221 debug("Partition %d is of type %d and bootable=%d\n", type_part, info.sys_ind, info.bootable);
222 if (info.bootable != 0) {
223 debug("Partition %d is bootable, using it\n", type_part);
224 partition = type_part;
228 printf("Using first bootable partition: %d\n", partition);
229 if (partition == CONFIG_SYS_MMCSD_FS_BOOT_PARTITION) {
235 #ifdef CONFIG_SPL_FS_FAT
236 if (!spl_start_uboot()) {
237 err = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
242 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
243 err = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
250 #ifdef CONFIG_SPL_FS_EXT4
251 if (!spl_start_uboot()) {
252 err = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
257 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
258 err = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
266 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
274 u32 __weak spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
276 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
277 return MMCSD_MODE_FS;
278 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
279 return MMCSD_MODE_EMMCBOOT;
281 return MMCSD_MODE_RAW;
285 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
286 int __weak spl_mmc_boot_partition(const u32 boot_device)
288 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
292 unsigned long __weak arch_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
293 unsigned long raw_sect)
298 unsigned long __weak board_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
299 unsigned long raw_sect)
301 return arch_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
304 unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
305 unsigned long raw_sect)
307 return board_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
310 int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
313 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
314 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
317 * We need to check what the partition is configured to.
318 * 1 and 2 match up to boot0 / boot1 and 7 is user data
319 * which is the first physical partition (0).
321 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
328 int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
330 return default_spl_mmc_emmc_boot_partition(mmc);
333 static int spl_mmc_get_mmc_devnum(struct mmc *mmc)
335 struct blk_desc *block_dev;
336 #if !CONFIG_IS_ENABLED(BLK)
337 block_dev = &mmc->block_dev;
339 block_dev = mmc_get_blk_desc(mmc);
341 return block_dev->devnum;
344 static struct mmc *mmc;
346 void spl_mmc_clear_cache(void)
351 int spl_mmc_load(struct spl_image_info *spl_image,
352 struct spl_boot_device *bootdev,
353 const char *filename,
355 unsigned long raw_sect)
359 __maybe_unused int part = 0;
362 /* Perform peripheral init only once for an mmc device */
363 mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
364 if (!mmc || spl_mmc_get_mmc_devnum(mmc) != mmc_dev) {
365 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
372 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
373 printf("spl: mmc init failed with error: %d\n", err);
379 boot_mode = spl_mmc_boot_mode(mmc, bootdev->boot_device);
382 case MMCSD_MODE_EMMCBOOT:
383 part = spl_mmc_emmc_boot_partition(mmc);
385 if (CONFIG_IS_ENABLED(MMC_TINY))
386 err = mmc_switch_part(mmc, part);
388 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
391 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
392 puts("spl: mmc partition switch failed\n");
398 debug("spl: mmc boot mode: raw\n");
400 if (!spl_start_uboot()) {
401 err = mmc_load_image_raw_os(spl_image, bootdev, mmc);
406 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
408 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
409 err = mmc_load_image_raw_partition(spl_image, bootdev,
415 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
416 err = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
417 raw_sect + spl_mmc_raw_uboot_offset(part));
421 /* If RAW mode fails, try FS mode. */
422 #ifdef CONFIG_SYS_MMCSD_FS_BOOT
424 debug("spl: mmc boot mode: fs\n");
426 err = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
432 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
434 puts("spl: mmc: wrong boot mode\n");
441 int spl_mmc_load_image(struct spl_image_info *spl_image,
442 struct spl_boot_device *bootdev)
444 return spl_mmc_load(spl_image, bootdev,
445 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
446 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
450 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
451 spl_mmc_boot_partition(bootdev->boot_device),
455 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
456 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
462 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
463 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
464 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);