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. error: %d\n", err);
151 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
152 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
153 struct mmc *mmc, int partition)
155 disk_partition_t info;
158 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
160 /* Only support MBR so DOS_ENTRY_NUMBERS */
161 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
162 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
166 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
167 partition = type_part;
173 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
175 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
176 puts("spl: partition error\n");
181 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
182 return mmc_load_image_raw_sector(spl_image, mmc,
183 info.start + CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
185 return mmc_load_image_raw_sector(spl_image, mmc, info.start);
190 #ifdef CONFIG_SPL_OS_BOOT
191 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
196 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
199 count = blk_dread(mmc_get_blk_desc(mmc),
200 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
201 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
202 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
203 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
204 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
205 puts("mmc_load_image_raw_os: mmc block read error\n");
209 #endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
211 ret = mmc_load_image_raw_sector(spl_image, mmc,
212 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
216 if (spl_image->os != IH_OS_LINUX) {
217 puts("Expected Linux image is not found. Trying to start U-boot\n");
224 int spl_start_uboot(void)
228 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
235 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
236 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc)
240 #ifdef CONFIG_SPL_FAT_SUPPORT
241 if (!spl_start_uboot()) {
242 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
243 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
247 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
248 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
249 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
250 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
255 #ifdef CONFIG_SPL_EXT_SUPPORT
256 if (!spl_start_uboot()) {
257 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
258 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
262 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
263 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
264 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
265 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
271 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
278 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc)
284 u32 __weak spl_boot_mode(const u32 boot_device)
286 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
287 return MMCSD_MODE_FS;
288 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
289 return MMCSD_MODE_EMMCBOOT;
291 return MMCSD_MODE_RAW;
295 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
297 int spl_boot_partition(const u32 boot_device)
299 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
303 int spl_mmc_load_image(struct spl_image_info *spl_image,
304 struct spl_boot_device *bootdev)
306 struct mmc *mmc = NULL;
309 __maybe_unused int part;
311 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
317 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
318 printf("spl: mmc init failed with error: %d\n", err);
323 boot_mode = spl_boot_mode(bootdev->boot_device);
326 case MMCSD_MODE_EMMCBOOT:
328 * We need to check what the partition is configured to.
329 * 1 and 2 match up to boot0 / boot1 and 7 is user data
330 * which is the first physical partition (0).
332 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
337 if (CONFIG_IS_ENABLED(MMC_TINY))
338 err = mmc_switch_part(mmc, part);
340 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
343 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
344 puts("spl: mmc partition switch failed\n");
350 debug("spl: mmc boot mode: raw\n");
352 if (!spl_start_uboot()) {
353 err = mmc_load_image_raw_os(spl_image, mmc);
357 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
358 err = spl_boot_partition(bootdev->boot_device);
362 err = mmc_load_image_raw_partition(spl_image, mmc, err);
366 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
367 err = mmc_load_image_raw_sector(spl_image, mmc,
368 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
372 /* If RAW mode fails, try FS mode. */
374 debug("spl: mmc boot mode: fs\n");
376 err = spl_mmc_do_fs_boot(spl_image, mmc);
381 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
383 puts("spl: mmc: wrong boot mode\n");
390 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
391 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
392 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);