1 // SPDX-License-Identifier: GPL-2.0+
4 * Texas Instruments, <www.ti.com>
6 * Aneesh V <aneesh@ti.com>
13 #include <linux/compiler.h>
15 #include <asm/u-boot.h>
20 static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc,
21 ulong sector, struct image_header *header)
23 u32 image_offset_sectors;
24 u32 image_size_sectors;
29 ret = spl_parse_image_header(spl_image, header);
33 /* convert offset to sectors - round down */
34 image_offset_sectors = spl_image->offset / mmc->read_bl_len;
35 /* calculate remaining offset */
36 image_offset = spl_image->offset % mmc->read_bl_len;
38 /* convert size to sectors - round up */
39 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
42 /* Read the header too to avoid extra memcpy */
43 count = blk_dread(mmc_get_blk_desc(mmc),
44 sector + image_offset_sectors,
46 (void *)(ulong)spl_image->load_addr);
47 debug("read %x sectors to %lx\n", image_size_sectors,
48 spl_image->load_addr);
49 if (count != image_size_sectors)
53 memmove((void *)(ulong)spl_image->load_addr,
54 (void *)(ulong)spl_image->load_addr + image_offset,
60 static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
61 ulong count, void *buf)
63 struct mmc *mmc = load->dev;
65 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
68 static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
70 #if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
72 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
79 int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
80 struct mmc *mmc, unsigned long sector)
83 struct image_header *header;
84 struct blk_desc *bd = mmc_get_blk_desc(mmc);
87 header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
89 /* read image header to find the image size & load address */
90 count = blk_dread(bd, sector, 1, header);
91 debug("hdr read sector %lx, count=%lu\n", sector, count);
97 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
98 image_get_magic(header) == FDT_MAGIC) {
99 struct spl_load_info load;
101 debug("Found FIT\n");
104 load.filename = NULL;
105 load.bl_len = mmc->read_bl_len;
106 load.read = h_spl_load_read;
107 ret = spl_load_simple_fit(spl_image, &load, sector, header);
108 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
109 struct spl_load_info load;
113 load.filename = NULL;
114 load.bl_len = mmc->read_bl_len;
115 load.read = h_spl_load_read;
117 ret = spl_load_imx_container(spl_image, &load, sector);
119 ret = mmc_load_legacy(spl_image, mmc, sector, header);
124 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
125 puts("mmc_load_image_raw_sector: mmc block read error\n");
133 static int spl_mmc_get_device_index(u32 boot_device)
135 switch (boot_device) {
136 case BOOT_DEVICE_MMC1:
138 case BOOT_DEVICE_MMC2:
139 case BOOT_DEVICE_MMC2_2:
143 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
144 printf("spl: unsupported mmc boot device.\n");
150 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
154 mmc_dev = spl_mmc_get_device_index(boot_device);
158 #if CONFIG_IS_ENABLED(DM_MMC)
159 err = mmc_init_device(mmc_dev);
161 err = mmc_initialize(NULL);
164 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
165 printf("spl: could not initialize mmc. error: %d\n", err);
169 *mmcp = find_mmc_device(mmc_dev);
170 err = *mmcp ? 0 : -ENODEV;
172 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
173 printf("spl: could not find mmc device %d. error: %d\n",
182 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
183 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
184 struct mmc *mmc, int partition,
185 unsigned long sector)
187 struct disk_partition info;
190 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
192 /* Only support MBR so DOS_ENTRY_NUMBERS */
193 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
194 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
198 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
199 partition = type_part;
205 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
207 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
208 puts("spl: partition error\n");
213 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
214 return mmc_load_image_raw_sector(spl_image, mmc, info.start + sector);
216 return mmc_load_image_raw_sector(spl_image, mmc, info.start);
221 #ifdef CONFIG_SPL_OS_BOOT
222 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
227 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
230 count = blk_dread(mmc_get_blk_desc(mmc),
231 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
232 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
233 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
234 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
235 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
236 puts("mmc_load_image_raw_os: mmc block read error\n");
240 #endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
242 ret = mmc_load_image_raw_sector(spl_image, mmc,
243 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
247 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
248 puts("Expected image is not found. Trying to start U-boot\n");
255 int spl_start_uboot(void)
259 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
266 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
267 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
268 const char *filename)
272 #ifdef CONFIG_SPL_FS_FAT
273 if (!spl_start_uboot()) {
274 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
275 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
279 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
280 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
281 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
287 #ifdef CONFIG_SPL_FS_EXT4
288 if (!spl_start_uboot()) {
289 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
290 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
294 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
295 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
296 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
303 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
310 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
311 const char *filename)
317 u32 __weak spl_mmc_boot_mode(const u32 boot_device)
319 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
320 return MMCSD_MODE_FS;
321 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
322 return MMCSD_MODE_EMMCBOOT;
324 return MMCSD_MODE_RAW;
328 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
329 int __weak spl_mmc_boot_partition(const u32 boot_device)
331 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
335 unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
336 unsigned long raw_sect)
341 int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
344 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
345 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
348 * We need to check what the partition is configured to.
349 * 1 and 2 match up to boot0 / boot1 and 7 is user data
350 * which is the first physical partition (0).
352 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
359 int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
361 return default_spl_mmc_emmc_boot_partition(mmc);
364 int spl_mmc_load(struct spl_image_info *spl_image,
365 struct spl_boot_device *bootdev,
366 const char *filename,
368 unsigned long raw_sect)
370 static struct mmc *mmc;
373 __maybe_unused int part = 0;
375 /* Perform peripheral init only once */
377 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
384 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
385 printf("spl: mmc init failed with error: %d\n", err);
391 boot_mode = spl_mmc_boot_mode(bootdev->boot_device);
394 case MMCSD_MODE_EMMCBOOT:
395 part = spl_mmc_emmc_boot_partition(mmc);
397 if (CONFIG_IS_ENABLED(MMC_TINY))
398 err = mmc_switch_part(mmc, part);
400 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
403 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
404 puts("spl: mmc partition switch failed\n");
410 debug("spl: mmc boot mode: raw\n");
412 if (!spl_start_uboot()) {
413 err = mmc_load_image_raw_os(spl_image, mmc);
418 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
420 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
421 err = mmc_load_image_raw_partition(spl_image, mmc, raw_part,
426 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
427 err = mmc_load_image_raw_sector(spl_image, mmc,
428 raw_sect + spl_mmc_raw_uboot_offset(part));
432 /* If RAW mode fails, try FS mode. */
434 debug("spl: mmc boot mode: fs\n");
436 err = spl_mmc_do_fs_boot(spl_image, mmc, filename);
441 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
443 puts("spl: mmc: wrong boot mode\n");
450 int spl_mmc_load_image(struct spl_image_info *spl_image,
451 struct spl_boot_device *bootdev)
453 return spl_mmc_load(spl_image, bootdev,
454 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
455 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
459 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
460 spl_mmc_boot_partition(bootdev->boot_device),
464 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
465 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
471 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
472 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
473 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);