1 // SPDX-License-Identifier: GPL-2.0+
4 * Texas Instruments, <www.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);
82 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
83 struct spl_load_info load;
88 load.bl_len = mmc->read_bl_len;
89 load.read = h_spl_load_read;
91 ret = spl_load_imx_container(spl_image, &load, sector);
93 ret = mmc_load_legacy(spl_image, mmc, sector, header);
98 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
99 puts("mmc_load_image_raw_sector: mmc block read error\n");
107 static int spl_mmc_get_device_index(u32 boot_device)
109 switch (boot_device) {
110 case BOOT_DEVICE_MMC1:
112 case BOOT_DEVICE_MMC2:
113 case BOOT_DEVICE_MMC2_2:
117 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
118 printf("spl: unsupported mmc boot device.\n");
124 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
128 mmc_dev = spl_mmc_get_device_index(boot_device);
132 #if CONFIG_IS_ENABLED(DM_MMC)
133 err = mmc_init_device(mmc_dev);
135 err = mmc_initialize(NULL);
138 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
139 printf("spl: could not initialize mmc. error: %d\n", err);
143 *mmcp = find_mmc_device(mmc_dev);
144 err = *mmcp ? 0 : -ENODEV;
146 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
147 printf("spl: could not find mmc device %d. error: %d\n",
156 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
157 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
158 struct mmc *mmc, int partition,
159 unsigned long sector)
161 disk_partition_t info;
164 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
166 /* Only support MBR so DOS_ENTRY_NUMBERS */
167 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
168 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
172 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
173 partition = type_part;
179 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
181 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
182 puts("spl: partition error\n");
187 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
188 return mmc_load_image_raw_sector(spl_image, mmc, info.start + sector);
190 return mmc_load_image_raw_sector(spl_image, mmc, info.start);
195 #ifdef CONFIG_SPL_OS_BOOT
196 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
201 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
204 count = blk_dread(mmc_get_blk_desc(mmc),
205 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
206 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
207 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
208 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
209 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
210 puts("mmc_load_image_raw_os: mmc block read error\n");
214 #endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
216 ret = mmc_load_image_raw_sector(spl_image, mmc,
217 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
221 if (spl_image->os != IH_OS_LINUX) {
222 puts("Expected Linux image is not found. Trying to start U-boot\n");
229 int spl_start_uboot(void)
233 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
240 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
241 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
242 const char *filename)
246 #ifdef CONFIG_SPL_FS_FAT
247 if (!spl_start_uboot()) {
248 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
249 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
253 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
254 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
255 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
261 #ifdef CONFIG_SPL_FS_EXT4
262 if (!spl_start_uboot()) {
263 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
264 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
268 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
269 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
270 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
277 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
284 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
285 const char *filename)
291 u32 __weak spl_boot_mode(const u32 boot_device)
293 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
294 return MMCSD_MODE_FS;
295 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
296 return MMCSD_MODE_EMMCBOOT;
298 return MMCSD_MODE_RAW;
302 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
304 int spl_boot_partition(const u32 boot_device)
306 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
310 unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc)
312 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
313 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR;
319 int spl_mmc_load(struct spl_image_info *spl_image,
320 struct spl_boot_device *bootdev,
321 const char *filename,
323 unsigned long raw_sect)
325 static struct mmc *mmc;
328 __maybe_unused int part;
330 /* Perform peripheral init only once */
332 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
339 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
340 printf("spl: mmc init failed with error: %d\n", err);
346 boot_mode = spl_boot_mode(bootdev->boot_device);
349 case MMCSD_MODE_EMMCBOOT:
350 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
351 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
354 * We need to check what the partition is configured to.
355 * 1 and 2 match up to boot0 / boot1 and 7 is user data
356 * which is the first physical partition (0).
358 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
364 if (CONFIG_IS_ENABLED(MMC_TINY))
365 err = mmc_switch_part(mmc, part);
367 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
370 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
371 puts("spl: mmc partition switch failed\n");
377 debug("spl: mmc boot mode: raw\n");
379 if (!spl_start_uboot()) {
380 err = mmc_load_image_raw_os(spl_image, mmc);
385 raw_sect = spl_mmc_get_uboot_raw_sector(mmc);
387 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
388 err = mmc_load_image_raw_partition(spl_image, mmc, raw_part,
393 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
394 err = mmc_load_image_raw_sector(spl_image, mmc, raw_sect);
398 /* If RAW mode fails, try FS mode. */
400 debug("spl: mmc boot mode: fs\n");
402 err = spl_mmc_do_fs_boot(spl_image, mmc, filename);
407 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
409 puts("spl: mmc: wrong boot mode\n");
416 int spl_mmc_load_image(struct spl_image_info *spl_image,
417 struct spl_boot_device *bootdev)
419 return spl_mmc_load(spl_image, bootdev,
420 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
421 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
425 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
426 spl_boot_partition(bootdev->boot_device),
430 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
431 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
437 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
438 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
439 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);