3 * Texas Instruments, <www.ti.com>
5 * Aneesh V <aneesh@ti.com>
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/compiler.h>
14 #include <asm/u-boot.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
24 u32 image_size_sectors;
25 struct image_header *header;
26 int dev_num = mmc->block_dev.dev;
28 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
29 sizeof(struct image_header));
31 /* read image header to find the image size & load address */
32 count = mmc->block_dev.block_read(dev_num, sector, 1, header);
33 debug("read sector %lx, count=%lu\n", sector, count);
37 if (image_get_magic(header) != IH_MAGIC) {
42 spl_parse_image_header(header);
44 /* convert size to sectors - round up */
45 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
48 /* Read the header too to avoid extra memcpy */
49 count = mmc->block_dev.block_read(dev_num, sector, image_size_sectors,
50 (void *)(ulong)spl_image.load_addr);
51 debug("read %x sectors to %x\n", image_size_sectors,
56 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
57 puts("spl: mmc block read error\n");
65 int spl_mmc_get_device_index(u32 boot_device)
67 switch (boot_device) {
68 case BOOT_DEVICE_MMC1:
70 case BOOT_DEVICE_MMC2:
71 case BOOT_DEVICE_MMC2_2:
75 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
76 printf("spl: unsupported mmc boot device.\n");
82 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
89 mmc_dev = spl_mmc_get_device_index(boot_device);
93 err = mmc_initialize(NULL);
95 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
96 printf("spl: could not initialize mmc. error: %d\n", err);
102 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
104 *mmcp = mmc_get_mmc_dev(dev);
106 *mmcp = find_mmc_device(mmc_dev);
107 err = *mmcp ? 0 : -ENODEV;
110 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
111 printf("spl: could not find mmc device. error: %d\n", err);
119 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
120 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
122 disk_partition_t info;
125 err = get_partition_info(&mmc->block_dev, partition, &info);
127 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
128 puts("spl: partition error\n");
133 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
134 return mmc_load_image_raw_sector(mmc, info.start +
135 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
137 return mmc_load_image_raw_sector(mmc, info.start);
141 #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
142 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
148 #ifdef CONFIG_SPL_OS_BOOT
149 static int mmc_load_image_raw_os(struct mmc *mmc)
153 count = mmc->block_dev.block_read(
155 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
156 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
157 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
159 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
160 puts("spl: mmc block read error\n");
165 return mmc_load_image_raw_sector(mmc,
166 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
169 int spl_start_uboot(void)
173 static int mmc_load_image_raw_os(struct mmc *mmc)
179 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
180 int spl_mmc_do_fs_boot(struct mmc *mmc)
184 #ifdef CONFIG_SPL_FAT_SUPPORT
185 if (!spl_start_uboot()) {
186 err = spl_load_image_fat_os(&mmc->block_dev,
187 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
191 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
192 err = spl_load_image_fat(&mmc->block_dev,
193 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
194 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
199 #ifdef CONFIG_SPL_EXT_SUPPORT
200 if (!spl_start_uboot()) {
201 err = spl_load_image_ext_os(&mmc->block_dev,
202 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
206 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
207 err = spl_load_image_ext(&mmc->block_dev,
208 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
209 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
215 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
222 int spl_mmc_do_fs_boot(struct mmc *mmc)
228 int spl_mmc_load_image(u32 boot_device)
230 struct mmc *mmc = NULL;
233 __maybe_unused int part;
235 err = spl_mmc_find_device(&mmc, boot_device);
241 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
242 printf("spl: mmc init failed with error: %d\n", err);
247 boot_mode = spl_boot_mode();
250 case MMCSD_MODE_EMMCBOOT:
252 * We need to check what the partition is configured to.
253 * 1 and 2 match up to boot0 / boot1 and 7 is user data
254 * which is the first physical partition (0).
256 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
261 err = mmc_switch_part(0, part);
263 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
264 puts("spl: mmc partition switch failed\n");
270 debug("spl: mmc boot mode: raw\n");
272 if (!spl_start_uboot()) {
273 err = mmc_load_image_raw_os(mmc);
278 err = mmc_load_image_raw_partition(mmc,
279 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
282 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
283 err = mmc_load_image_raw_sector(mmc,
284 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
290 debug("spl: mmc boot mode: fs\n");
292 err = spl_mmc_do_fs_boot(mmc);
297 case MMCSD_MODE_UNDEFINED:
298 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
300 puts("spl: mmc: wrong boot mode\n");