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>
15 DECLARE_GLOBAL_DATA_PTR;
17 static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
20 u32 image_size_sectors;
21 struct image_header *header;
23 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
24 sizeof(struct image_header));
26 /* read image header to find the image size & load address */
27 err = mmc->block_dev.block_read(0, sector, 1, header);
31 if (image_get_magic(header) != IH_MAGIC)
34 spl_parse_image_header(header);
36 /* convert size to sectors - round up */
37 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
40 /* Read the header too to avoid extra memcpy */
41 err = mmc->block_dev.block_read(0, sector, image_size_sectors,
42 (void *)spl_image.load_addr);
45 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
47 printf("spl: mmc blk read err - %lu\n", err);
53 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
54 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
56 disk_partition_t info;
58 if (get_partition_info(&mmc->block_dev, partition, &info)) {
59 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
60 printf("spl: partition error\n");
65 return mmc_load_image_raw_sector(mmc, info.start);
69 #ifdef CONFIG_SPL_OS_BOOT
70 static int mmc_load_image_raw_os(struct mmc *mmc)
72 if (!mmc->block_dev.block_read(0,
73 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
74 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
75 (void *)CONFIG_SYS_SPL_ARGS_ADDR)) {
76 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
77 printf("mmc args blk read error\n");
82 return mmc_load_image_raw_sector(mmc,
83 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
87 void spl_mmc_load_image(void)
93 mmc_initialize(gd->bd);
94 /* We register only one device. So, the dev id is always 0 */
95 mmc = find_mmc_device(0);
97 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
98 puts("spl: mmc device not found!!\n");
105 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
106 printf("spl: mmc init failed: err - %d\n", err);
111 boot_mode = spl_boot_mode();
112 if (boot_mode == MMCSD_MODE_RAW) {
113 debug("boot mode - RAW\n");
114 #ifdef CONFIG_SPL_OS_BOOT
115 if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
117 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
118 err = mmc_load_image_raw_partition(mmc,
119 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
121 err = mmc_load_image_raw_sector(mmc,
122 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
124 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
126 if (err || boot_mode == MMCSD_MODE_FS) {
127 debug("boot mode - FS\n");
128 #ifdef CONFIG_SPL_FAT_SUPPORT
129 #ifdef CONFIG_SPL_OS_BOOT
130 if (spl_start_uboot() || spl_load_image_fat_os(&mmc->block_dev,
131 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION))
133 err = spl_load_image_fat(&mmc->block_dev,
134 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
135 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
137 #endif /* CONFIG_SPL_FAT_SUPPORT */
139 #ifdef CONFIG_SPL_EXT_SUPPORT
140 #ifdef CONFIG_SPL_OS_BOOT
141 if (spl_start_uboot() || spl_load_image_ext_os(&mmc->block_dev,
142 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION))
144 err = spl_load_image_ext(&mmc->block_dev,
145 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
146 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
147 #endif /* CONFIG_SPL_EXT_SUPPORT */
149 #endif /* defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) */
150 #ifdef CONFIG_SUPPORT_EMMC_BOOT
151 } else if (boot_mode == MMCSD_MODE_EMMCBOOT) {
153 * We need to check what the partition is configured to.
154 * 1 and 2 match up to boot0 / boot1 and 7 is user data
155 * which is the first physical partition (0).
157 int part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
162 if (mmc_switch_part(0, part)) {
163 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
164 puts("MMC partition switch failed\n");
168 #ifdef CONFIG_SPL_OS_BOOT
169 if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
171 err = mmc_load_image_raw_sector(mmc,
172 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
178 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
181 #ifdef CONFIG_SUPPORT_EMMC_BOOT
182 case MMCSD_MODE_EMMCBOOT:
184 /* Boot mode is ok. Nothing to do. */
186 case MMCSD_MODE_UNDEFINED:
188 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
189 puts("spl: wrong MMC boot mode\n");