3 * Texas Instruments, <www.ti.com>
5 * Aneesh V <aneesh@ti.com>
7 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/u-boot.h>
12 #include <asm/utils.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 static int mmc_load_image_raw(struct mmc *mmc, unsigned long sector)
23 u32 image_size_sectors;
24 struct image_header *header;
26 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
27 sizeof(struct image_header));
29 /* read image header to find the image size & load address */
30 err = mmc->block_dev.block_read(0, sector, 1, header);
34 if (image_get_magic(header) != IH_MAGIC)
37 spl_parse_image_header(header);
39 /* convert size to sectors - round up */
40 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
43 /* Read the header too to avoid extra memcpy */
44 err = mmc->block_dev.block_read(0, sector, image_size_sectors,
45 (void *)spl_image.load_addr);
49 printf("spl: mmc blk read err - %lu\n", err);
54 #ifdef CONFIG_SPL_OS_BOOT
55 static int mmc_load_image_raw_os(struct mmc *mmc)
57 if (!mmc->block_dev.block_read(0,
58 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
59 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
60 (void *)CONFIG_SYS_SPL_ARGS_ADDR)) {
61 printf("mmc args blk read error\n");
65 return mmc_load_image_raw(mmc, CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
69 #ifdef CONFIG_SPL_FAT_SUPPORT
70 static int mmc_load_image_fat(struct mmc *mmc, const char *filename)
73 struct image_header *header;
75 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
76 sizeof(struct image_header));
78 err = file_fat_read(filename, header, sizeof(struct image_header));
82 spl_parse_image_header(header);
84 err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
88 printf("spl: error reading image %s, err - %d\n",
94 #ifdef CONFIG_SPL_OS_BOOT
95 static int mmc_load_image_fat_os(struct mmc *mmc)
99 err = file_fat_read(CONFIG_SPL_FAT_LOAD_ARGS_NAME,
100 (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
102 printf("spl: error reading image %s, err - %d\n",
103 CONFIG_SPL_FAT_LOAD_ARGS_NAME, err);
107 return mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_KERNEL_NAME);
113 void spl_mmc_load_image(void)
119 mmc_initialize(gd->bd);
120 /* We register only one device. So, the dev id is always 0 */
121 mmc = find_mmc_device(0);
123 puts("spl: mmc device not found!!\n");
129 printf("spl: mmc init failed: err - %d\n", err);
133 boot_mode = spl_boot_mode();
134 if (boot_mode == MMCSD_MODE_RAW) {
135 debug("boot mode - RAW\n");
136 #ifdef CONFIG_SPL_OS_BOOT
137 if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
139 err = mmc_load_image_raw(mmc,
140 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
141 #ifdef CONFIG_SPL_FAT_SUPPORT
142 } else if (boot_mode == MMCSD_MODE_FAT) {
143 debug("boot mode - FAT\n");
145 err = fat_register_device(&mmc->block_dev,
146 CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
148 printf("spl: fat register err - %d\n", err);
152 #ifdef CONFIG_SPL_OS_BOOT
153 if (spl_start_uboot() || mmc_load_image_fat_os(mmc))
155 err = mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME);
158 puts("spl: wrong MMC boot mode\n");