3 * Texas Instruments, <www.ti.com>
5 * Aneesh V <aneesh@ti.com>
7 * SPDX-License-Identifier: GPL-2.0+
11 #include <linux/compiler.h>
12 #include <asm/u-boot.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
21 u32 image_size_sectors;
22 struct image_header *header;
24 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
25 sizeof(struct image_header));
27 /* read image header to find the image size & load address */
28 count = mmc->block_dev.block_read(0, sector, 1, header);
32 if (image_get_magic(header) != IH_MAGIC)
35 spl_parse_image_header(header);
37 /* convert size to sectors - round up */
38 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
41 /* Read the header too to avoid extra memcpy */
42 count = mmc->block_dev.block_read(0, sector, image_size_sectors,
43 (void *) spl_image.load_addr);
47 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
48 puts("spl: mmc block read error\n");
56 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
57 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
59 disk_partition_t info;
62 err = get_partition_info(&mmc->block_dev, partition, &info);
64 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
65 puts("spl: partition error\n");
70 return mmc_load_image_raw_sector(mmc, info.start);
74 #ifdef CONFIG_SPL_OS_BOOT
75 static int mmc_load_image_raw_os(struct mmc *mmc)
79 count = mmc->block_dev.block_read(0,
80 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
81 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
82 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
84 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
85 puts("spl: mmc block read error\n");
90 return mmc_load_image_raw_sector(mmc,
91 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
95 void spl_mmc_load_image(void)
100 __maybe_unused int part;
102 mmc_initialize(gd->bd);
104 /* We register only one device. So, the dev id is always 0 */
105 mmc = find_mmc_device(0);
107 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
108 puts("spl: mmc device not found\n");
115 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
116 printf("spl: mmc init failed with error: %d\n", err);
121 boot_mode = spl_boot_mode();
124 debug("spl: mmc boot mode: raw\n");
126 #ifdef CONFIG_SPL_OS_BOOT
127 if (!spl_start_uboot()) {
128 err = mmc_load_image_raw_os(mmc);
133 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
134 err = mmc_load_image_raw_partition(mmc,
135 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
138 #elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
139 err = mmc_load_image_raw_sector(mmc,
140 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
145 debug("spl: mmc boot mode: fs\n");
147 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
148 #ifdef CONFIG_SPL_FAT_SUPPORT
149 #ifdef CONFIG_SPL_OS_BOOT
150 if (!spl_start_uboot()) {
151 err = spl_load_image_fat_os(&mmc->block_dev,
152 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
157 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
158 err = spl_load_image_fat(&mmc->block_dev,
159 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
160 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
165 #ifdef CONFIG_SPL_EXT_SUPPORT
166 #ifdef CONFIG_SPL_OS_BOOT
167 if (!spl_start_uboot()) {
168 err = spl_load_image_ext_os(&mmc->block_dev,
169 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
174 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
175 err = spl_load_image_ext(&mmc->block_dev,
176 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
177 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
183 #ifdef CONFIG_SUPPORT_EMMC_BOOT
184 case MMCSD_MODE_EMMCBOOT:
186 * We need to check what the partition is configured to.
187 * 1 and 2 match up to boot0 / boot1 and 7 is user data
188 * which is the first physical partition (0).
190 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
195 if (mmc_switch_part(0, part)) {
196 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
197 puts("spl: mmc partition switch failed\n");
202 #ifdef CONFIG_SPL_OS_BOOT
203 if (!spl_start_uboot()) {
204 err = mmc_load_image_raw_os(mmc);
209 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
210 err = mmc_load_image_raw_partition(mmc,
211 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
214 #elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
215 err = mmc_load_image_raw_sector(mmc,
216 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
221 case MMCSD_MODE_UNDEFINED:
223 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
225 puts("spl: mmc: no boot mode left to try\n");
227 puts("spl: mmc: wrong boot mode\n");