1 // SPDX-License-Identifier: GPL-2.0+
4 * Texas Instruments, <www.ti.com>
6 * Aneesh V <aneesh@ti.com>
11 #include <linux/compiler.h>
13 #include <asm/u-boot.h>
18 static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc,
19 ulong sector, struct image_header *header)
21 u32 image_size_sectors;
25 ret = spl_parse_image_header(spl_image, header);
29 /* convert size to sectors - round up */
30 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
33 /* Read the header too to avoid extra memcpy */
34 count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
35 (void *)(ulong)spl_image->load_addr);
36 debug("read %x sectors to %lx\n", image_size_sectors,
37 spl_image->load_addr);
38 if (count != image_size_sectors)
44 static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
45 ulong count, void *buf)
47 struct mmc *mmc = load->dev;
49 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
53 int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
54 struct mmc *mmc, unsigned long sector)
57 struct image_header *header;
58 struct blk_desc *bd = mmc_get_blk_desc(mmc);
61 header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
63 /* read image header to find the image size & load address */
64 count = blk_dread(bd, sector, 1, header);
65 debug("hdr read sector %lx, count=%lu\n", sector, count);
71 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
72 image_get_magic(header) == FDT_MAGIC) {
73 struct spl_load_info load;
79 load.bl_len = mmc->read_bl_len;
80 load.read = h_spl_load_read;
81 ret = spl_load_simple_fit(spl_image, &load, sector, header);
83 ret = mmc_load_legacy(spl_image, mmc, sector, header);
88 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
89 puts("mmc_load_image_raw_sector: mmc block read error\n");
97 static int spl_mmc_get_device_index(u32 boot_device)
99 switch (boot_device) {
100 case BOOT_DEVICE_MMC1:
102 case BOOT_DEVICE_MMC2:
103 case BOOT_DEVICE_MMC2_2:
107 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
108 printf("spl: unsupported mmc boot device.\n");
114 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
116 #if CONFIG_IS_ENABLED(DM_MMC)
121 mmc_dev = spl_mmc_get_device_index(boot_device);
125 err = mmc_initialize(NULL);
127 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
128 printf("spl: could not initialize mmc. error: %d\n", err);
133 #if CONFIG_IS_ENABLED(DM_MMC)
134 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
136 *mmcp = mmc_get_mmc_dev(dev);
138 *mmcp = find_mmc_device(mmc_dev);
139 err = *mmcp ? 0 : -ENODEV;
142 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
143 printf("spl: could not find mmc device %d. error: %d\n",
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,
155 unsigned long sector)
157 disk_partition_t info;
160 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
162 /* Only support MBR so DOS_ENTRY_NUMBERS */
163 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
164 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
168 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
169 partition = type_part;
175 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
177 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
178 puts("spl: partition error\n");
183 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
184 return mmc_load_image_raw_sector(spl_image, mmc, info.start + 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,
197 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
200 count = blk_dread(mmc_get_blk_desc(mmc),
201 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
202 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
203 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
204 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
205 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
206 puts("mmc_load_image_raw_os: mmc block read error\n");
210 #endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
212 ret = mmc_load_image_raw_sector(spl_image, mmc,
213 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
217 if (spl_image->os != IH_OS_LINUX) {
218 puts("Expected Linux image is not found. Trying to start U-boot\n");
225 int spl_start_uboot(void)
229 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
236 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
237 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
238 const char *filename)
242 #ifdef CONFIG_SPL_FS_FAT
243 if (!spl_start_uboot()) {
244 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
245 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
249 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
250 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
251 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
257 #ifdef CONFIG_SPL_FS_EXT4
258 if (!spl_start_uboot()) {
259 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
260 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
264 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
265 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
266 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
273 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
280 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
281 const char *filename)
287 u32 __weak spl_boot_mode(const u32 boot_device)
289 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
290 return MMCSD_MODE_FS;
291 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
292 return MMCSD_MODE_EMMCBOOT;
294 return MMCSD_MODE_RAW;
298 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
300 int spl_boot_partition(const u32 boot_device)
302 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
306 int spl_mmc_load(struct spl_image_info *spl_image,
307 struct spl_boot_device *bootdev,
308 const char *filename,
310 unsigned long raw_sect)
312 static struct mmc *mmc;
315 __maybe_unused int part;
317 /* Perform peripheral init only once */
319 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
326 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
327 printf("spl: mmc init failed with error: %d\n", err);
333 boot_mode = spl_boot_mode(bootdev->boot_device);
336 case MMCSD_MODE_EMMCBOOT:
338 * We need to check what the partition is configured to.
339 * 1 and 2 match up to boot0 / boot1 and 7 is user data
340 * which is the first physical partition (0).
342 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
347 if (CONFIG_IS_ENABLED(MMC_TINY))
348 err = mmc_switch_part(mmc, part);
350 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
353 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
354 puts("spl: mmc partition switch failed\n");
360 debug("spl: mmc boot mode: raw\n");
362 if (!spl_start_uboot()) {
363 err = mmc_load_image_raw_os(spl_image, mmc);
367 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
368 err = mmc_load_image_raw_partition(spl_image, mmc, raw_part,
373 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
374 err = mmc_load_image_raw_sector(spl_image, mmc, raw_sect);
378 /* If RAW mode fails, try FS mode. */
380 debug("spl: mmc boot mode: fs\n");
382 err = spl_mmc_do_fs_boot(spl_image, mmc, filename);
387 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
389 puts("spl: mmc: wrong boot mode\n");
396 int spl_mmc_load_image(struct spl_image_info *spl_image,
397 struct spl_boot_device *bootdev)
399 return spl_mmc_load(spl_image, bootdev,
400 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
401 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
405 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
406 spl_boot_partition(bootdev->boot_device),
410 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
411 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
417 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
418 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
419 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);