3 * Texas Instruments, <www.ti.com>
5 * Aneesh V <aneesh@ti.com>
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/compiler.h>
13 #include <asm/u-boot.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
22 u32 image_size_sectors;
23 struct image_header *header;
25 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
26 sizeof(struct image_header));
28 /* read image header to find the image size & load address */
29 count = mmc->block_dev.block_read(0, sector, 1, header);
30 debug("read sector %lx, count=%lu\n", sector, count);
34 if (image_get_magic(header) != IH_MAGIC) {
39 spl_parse_image_header(header);
41 /* convert size to sectors - round up */
42 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
45 /* Read the header too to avoid extra memcpy */
46 count = mmc->block_dev.block_read(0, sector, image_size_sectors,
47 (void *)spl_image.load_addr);
48 debug("read %x sectors to %x\n", image_size_sectors,
53 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
54 puts("spl: mmc block read error\n");
62 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
63 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
65 disk_partition_t info;
68 err = get_partition_info(&mmc->block_dev, partition, &info);
70 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
71 puts("spl: partition error\n");
76 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
77 return mmc_load_image_raw_sector(mmc, info.start +
78 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
80 return mmc_load_image_raw_sector(mmc, info.start);
85 #ifdef CONFIG_SPL_OS_BOOT
86 static int mmc_load_image_raw_os(struct mmc *mmc)
90 count = mmc->block_dev.block_read(0,
91 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
92 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
93 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
95 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
96 puts("spl: mmc block read error\n");
101 return mmc_load_image_raw_sector(mmc,
102 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
106 void spl_mmc_load_image(void)
111 __maybe_unused int part;
116 mmc_initialize(NULL);
117 err = uclass_get_device(UCLASS_MMC, 0, &dev);
120 mmc = mmc_get_mmc_dev(dev);
122 mmc_initialize(gd->bd);
124 /* We register only one device. So, the dev id is always 0 */
125 mmc = find_mmc_device(0);
127 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
128 puts("spl: mmc device not found\n");
138 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
139 printf("spl: mmc init failed with error: %d\n", err);
144 boot_mode = spl_boot_mode();
147 debug("spl: mmc boot mode: raw\n");
149 #ifdef CONFIG_SPL_OS_BOOT
150 if (!spl_start_uboot()) {
151 err = mmc_load_image_raw_os(mmc);
156 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
157 err = mmc_load_image_raw_partition(mmc,
158 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
161 #elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
162 err = mmc_load_image_raw_sector(mmc,
163 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
168 debug("spl: mmc boot mode: fs\n");
170 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
171 #ifdef CONFIG_SPL_FAT_SUPPORT
172 #ifdef CONFIG_SPL_OS_BOOT
173 if (!spl_start_uboot()) {
174 err = spl_load_image_fat_os(&mmc->block_dev,
175 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
180 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
181 err = spl_load_image_fat(&mmc->block_dev,
182 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
183 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
188 #ifdef CONFIG_SPL_EXT_SUPPORT
189 #ifdef CONFIG_SPL_OS_BOOT
190 if (!spl_start_uboot()) {
191 err = spl_load_image_ext_os(&mmc->block_dev,
192 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
197 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
198 err = spl_load_image_ext(&mmc->block_dev,
199 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
200 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
206 #ifdef CONFIG_SUPPORT_EMMC_BOOT
207 case MMCSD_MODE_EMMCBOOT:
209 * We need to check what the partition is configured to.
210 * 1 and 2 match up to boot0 / boot1 and 7 is user data
211 * which is the first physical partition (0).
213 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
218 if (mmc_switch_part(0, part)) {
219 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
220 puts("spl: mmc partition switch failed\n");
225 #ifdef CONFIG_SPL_OS_BOOT
226 if (!spl_start_uboot()) {
227 err = mmc_load_image_raw_os(mmc);
232 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
233 err = mmc_load_image_raw_partition(mmc,
234 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
237 #elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
238 err = mmc_load_image_raw_sector(mmc,
239 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
244 case MMCSD_MODE_UNDEFINED:
246 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
248 puts("spl: mmc: no boot mode left to try\n");
250 puts("spl: mmc: wrong boot mode\n");