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 static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc,
20 ulong sector, struct image_header *header)
22 u32 image_size_sectors;
26 ret = spl_parse_image_header(spl_image, header);
30 /* convert size to sectors - round up */
31 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
34 /* Read the header too to avoid extra memcpy */
35 count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
36 (void *)(ulong)spl_image->load_addr);
37 debug("read %x sectors to %lx\n", image_size_sectors,
38 spl_image->load_addr);
39 if (count != image_size_sectors)
45 static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
46 ulong count, void *buf)
48 struct mmc *mmc = load->dev;
50 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
54 int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
55 struct mmc *mmc, unsigned long sector)
58 struct image_header *header;
61 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
62 sizeof(struct image_header));
64 /* read image header to find the image size & load address */
65 count = blk_dread(mmc_get_blk_desc(mmc), sector, 1, header);
66 debug("hdr read sector %lx, count=%lu\n", sector, count);
72 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
73 image_get_magic(header) == FDT_MAGIC) {
74 struct spl_load_info load;
80 load.bl_len = mmc->read_bl_len;
81 load.read = h_spl_load_read;
82 ret = spl_load_simple_fit(spl_image, &load, sector, header);
84 ret = mmc_load_legacy(spl_image, mmc, sector, header);
89 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
90 puts("mmc_load_image_raw_sector: mmc block read error\n");
98 static int spl_mmc_get_device_index(u32 boot_device)
100 switch (boot_device) {
101 case BOOT_DEVICE_MMC1:
103 case BOOT_DEVICE_MMC2:
104 case BOOT_DEVICE_MMC2_2:
108 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
109 printf("spl: unsupported mmc boot device.\n");
115 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
117 #if CONFIG_IS_ENABLED(DM_MMC)
122 mmc_dev = spl_mmc_get_device_index(boot_device);
126 err = mmc_initialize(NULL);
128 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
129 printf("spl: could not initialize mmc. error: %d\n", err);
134 #if CONFIG_IS_ENABLED(DM_MMC)
135 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
137 *mmcp = mmc_get_mmc_dev(dev);
139 *mmcp = find_mmc_device(mmc_dev);
140 err = *mmcp ? 0 : -ENODEV;
143 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
144 printf("spl: could not find mmc device. error: %d\n", err);
152 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
153 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
154 struct mmc *mmc, int partition)
156 disk_partition_t info;
159 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
161 /* Only support MBR so DOS_ENTRY_NUMBERS */
162 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
163 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
167 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
168 partition = type_part;
174 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
176 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
177 puts("spl: partition error\n");
182 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
183 return mmc_load_image_raw_sector(spl_image, mmc,
184 info.start + CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
186 return mmc_load_image_raw_sector(spl_image, mmc, info.start);
191 #ifdef CONFIG_SPL_OS_BOOT
192 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
198 count = blk_dread(mmc_get_blk_desc(mmc),
199 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
200 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
201 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
202 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
203 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
204 puts("mmc_load_image_raw_os: mmc block read error\n");
209 ret = mmc_load_image_raw_sector(spl_image, mmc,
210 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
214 if (spl_image->os != IH_OS_LINUX) {
215 puts("Expected Linux image is not found. Trying to start U-boot\n");
222 int spl_start_uboot(void)
226 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
233 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
234 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc)
238 #ifdef CONFIG_SPL_FAT_SUPPORT
239 if (!spl_start_uboot()) {
240 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
241 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
245 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
246 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
247 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
248 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
253 #ifdef CONFIG_SPL_EXT_SUPPORT
254 if (!spl_start_uboot()) {
255 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
256 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
260 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
261 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
262 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
263 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
269 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
276 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc)
282 u32 __weak spl_boot_mode(const u32 boot_device)
284 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
285 return MMCSD_MODE_FS;
286 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
287 return MMCSD_MODE_EMMCBOOT;
289 return MMCSD_MODE_RAW;
293 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
295 int spl_boot_partition(const u32 boot_device)
297 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
301 int spl_mmc_load_image(struct spl_image_info *spl_image,
302 struct spl_boot_device *bootdev)
304 struct mmc *mmc = NULL;
307 __maybe_unused int part;
309 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
315 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
316 printf("spl: mmc init failed with error: %d\n", err);
321 boot_mode = spl_boot_mode(bootdev->boot_device);
324 case MMCSD_MODE_EMMCBOOT:
326 * We need to check what the partition is configured to.
327 * 1 and 2 match up to boot0 / boot1 and 7 is user data
328 * which is the first physical partition (0).
330 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
335 if (CONFIG_IS_ENABLED(MMC_TINY))
336 err = mmc_switch_part(mmc, part);
338 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
341 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
342 puts("spl: mmc partition switch failed\n");
348 debug("spl: mmc boot mode: raw\n");
350 if (!spl_start_uboot()) {
351 err = mmc_load_image_raw_os(spl_image, mmc);
355 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
356 err = spl_boot_partition(bootdev->boot_device);
360 err = mmc_load_image_raw_partition(spl_image, mmc, err);
364 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
365 err = mmc_load_image_raw_sector(spl_image, mmc,
366 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
370 /* If RAW mode fails, try FS mode. */
372 debug("spl: mmc boot mode: fs\n");
374 err = spl_mmc_do_fs_boot(spl_image, mmc);
379 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
381 puts("spl: mmc: wrong boot mode\n");
388 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
389 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
390 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);