3 * Texas Instruments, <www.ti.com>
5 * Aneesh V <aneesh@ti.com>
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/compiler.h>
13 #include <asm/u-boot.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 static int mmc_load_image_raw_sector(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 count = mmc->block_dev.block_read(0, sector, 1, header);
31 debug("read sector %lx, count=%lu\n", sector, count);
35 if (image_get_magic(header) != IH_MAGIC) {
40 spl_parse_image_header(header);
42 /* convert size to sectors - round up */
43 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
46 /* Read the header too to avoid extra memcpy */
47 count = mmc->block_dev.block_read(0, sector, image_size_sectors,
48 (void *)(ulong)spl_image.load_addr);
49 debug("read %x sectors to %x\n", image_size_sectors,
54 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
55 puts("spl: mmc block read error\n");
64 static int spl_mmc_find_device(struct mmc **mmc)
69 err = mmc_initialize(NULL);
71 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
72 printf("spl: could not initialize mmc. error: %d\n", err);
77 err = uclass_get_device(UCLASS_MMC, 0, &dev);
79 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
80 printf("spl: could not find mmc device. error: %d\n", err);
86 *mmc = mmc_get_mmc_dev(dev);
87 return *mmc != NULL ? 0 : -ENODEV;
90 static int spl_mmc_find_device(struct mmc **mmc)
94 err = mmc_initialize(gd->bd);
96 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
97 printf("spl: could not initialize mmc. error: %d\n", err);
102 /* We register only one device. So, the dev id is always 0 */
103 *mmc = find_mmc_device(0);
105 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
106 puts("spl: mmc device not found\n");
115 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
116 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
118 disk_partition_t info;
121 err = get_partition_info(&mmc->block_dev, partition, &info);
123 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
124 puts("spl: partition error\n");
129 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
130 return mmc_load_image_raw_sector(mmc, info.start +
131 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
133 return mmc_load_image_raw_sector(mmc, info.start);
137 #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
138 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
144 #ifdef CONFIG_SPL_OS_BOOT
145 static int mmc_load_image_raw_os(struct mmc *mmc)
149 count = mmc->block_dev.block_read(0,
150 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
151 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
152 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
154 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
155 puts("spl: mmc block read error\n");
160 return mmc_load_image_raw_sector(mmc,
161 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
164 int spl_start_uboot(void)
168 static int mmc_load_image_raw_os(struct mmc *mmc)
174 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
175 int spl_mmc_do_fs_boot(struct mmc *mmc)
179 #ifdef CONFIG_SPL_FAT_SUPPORT
180 if (!spl_start_uboot()) {
181 err = spl_load_image_fat_os(&mmc->block_dev,
182 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
186 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
187 err = spl_load_image_fat(&mmc->block_dev,
188 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
189 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
194 #ifdef CONFIG_SPL_EXT_SUPPORT
195 if (!spl_start_uboot()) {
196 err = spl_load_image_ext_os(&mmc->block_dev,
197 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
201 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
202 err = spl_load_image_ext(&mmc->block_dev,
203 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
204 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
210 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
217 int spl_mmc_do_fs_boot(struct mmc *mmc)
223 void spl_mmc_load_image(void)
228 __maybe_unused int part;
230 if (spl_mmc_find_device(&mmc))
235 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
236 printf("spl: mmc init failed with error: %d\n", err);
241 boot_mode = spl_boot_mode();
243 case MMCSD_MODE_EMMCBOOT:
245 * We need to check what the partition is configured to.
246 * 1 and 2 match up to boot0 / boot1 and 7 is user data
247 * which is the first physical partition (0).
249 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
254 if (mmc_switch_part(0, part)) {
255 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
256 puts("spl: mmc partition switch failed\n");
262 debug("spl: mmc boot mode: raw\n");
264 if (!spl_start_uboot()) {
265 err = mmc_load_image_raw_os(mmc);
270 err = mmc_load_image_raw_partition(mmc,
271 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
274 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
275 err = mmc_load_image_raw_sector(mmc,
276 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
282 debug("spl: mmc boot mode: fs\n");
284 err = spl_mmc_do_fs_boot(mmc);
289 case MMCSD_MODE_UNDEFINED:
290 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
292 puts("spl: mmc: wrong boot mode\n");