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)
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,
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)
241 #ifdef CONFIG_SPL_FS_FAT
242 if (!spl_start_uboot()) {
243 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
244 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
248 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
249 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
250 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
251 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
256 #ifdef CONFIG_SPL_FS_EXT4
257 if (!spl_start_uboot()) {
258 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
259 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
263 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
264 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
265 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
266 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
272 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
279 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc)
285 u32 __weak spl_boot_mode(const u32 boot_device)
287 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
288 return MMCSD_MODE_FS;
289 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
290 return MMCSD_MODE_EMMCBOOT;
292 return MMCSD_MODE_RAW;
296 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
298 int spl_boot_partition(const u32 boot_device)
300 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
304 int spl_mmc_load_image(struct spl_image_info *spl_image,
305 struct spl_boot_device *bootdev)
307 struct mmc *mmc = NULL;
310 __maybe_unused int part;
312 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
318 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
319 printf("spl: mmc init failed with error: %d\n", err);
324 boot_mode = spl_boot_mode(bootdev->boot_device);
327 case MMCSD_MODE_EMMCBOOT:
329 * We need to check what the partition is configured to.
330 * 1 and 2 match up to boot0 / boot1 and 7 is user data
331 * which is the first physical partition (0).
333 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
338 if (CONFIG_IS_ENABLED(MMC_TINY))
339 err = mmc_switch_part(mmc, part);
341 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
344 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
345 puts("spl: mmc partition switch failed\n");
351 debug("spl: mmc boot mode: raw\n");
353 if (!spl_start_uboot()) {
354 err = mmc_load_image_raw_os(spl_image, mmc);
358 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
359 err = spl_boot_partition(bootdev->boot_device);
363 err = mmc_load_image_raw_partition(spl_image, mmc, err);
367 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
368 err = mmc_load_image_raw_sector(spl_image, mmc,
369 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
373 /* If RAW mode fails, try FS mode. */
375 debug("spl: mmc boot mode: fs\n");
377 err = spl_mmc_do_fs_boot(spl_image, mmc);
382 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
384 puts("spl: mmc: wrong boot mode\n");
391 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
392 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
393 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);