3 * Texas Instruments, <www.ti.com>
5 * Aneesh V <aneesh@ti.com>
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/compiler.h>
14 #include <asm/u-boot.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 static int mmc_load_legacy(struct mmc *mmc, ulong sector,
22 struct image_header *header)
24 u32 image_size_sectors;
28 ret = spl_parse_image_header(&spl_image, header);
32 /* convert size to sectors - round up */
33 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
36 /* Read the header too to avoid extra memcpy */
37 count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
38 (void *)(ulong)spl_image.load_addr);
39 debug("read %x sectors to %x\n", image_size_sectors,
41 if (count != image_size_sectors)
47 static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
48 ulong count, void *buf)
50 struct mmc *mmc = load->dev;
52 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
55 static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
58 struct image_header *header;
61 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
62 sizeof(struct image_header));
64 /* read image header to find the image size & load address */
65 count = blk_dread(mmc_get_blk_desc(mmc), sector, 1, header);
66 debug("hdr read sector %lx, count=%lu\n", sector, count);
72 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
73 image_get_magic(header) == FDT_MAGIC) {
74 struct spl_load_info load;
80 load.bl_len = mmc->read_bl_len;
81 load.read = h_spl_load_read;
82 ret = spl_load_simple_fit(&load, sector, header);
84 ret = mmc_load_legacy(mmc, sector, header);
89 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
90 puts("mmc_load_image_raw_sector: mmc block read error\n");
98 int spl_mmc_get_device_index(u32 boot_device)
100 switch (boot_device) {
101 case BOOT_DEVICE_MMC1:
103 case BOOT_DEVICE_MMC2:
104 case BOOT_DEVICE_MMC2_2:
108 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
109 printf("spl: unsupported mmc boot device.\n");
115 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
122 mmc_dev = spl_mmc_get_device_index(boot_device);
126 err = mmc_initialize(NULL);
128 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
129 printf("spl: could not initialize mmc. error: %d\n", err);
135 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
137 *mmcp = mmc_get_mmc_dev(dev);
139 *mmcp = find_mmc_device(mmc_dev);
140 err = *mmcp ? 0 : -ENODEV;
143 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
144 printf("spl: could not find mmc device. error: %d\n", err);
152 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
153 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
155 disk_partition_t info;
158 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
160 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
161 puts("spl: partition error\n");
166 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
167 return mmc_load_image_raw_sector(mmc, info.start +
168 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
170 return mmc_load_image_raw_sector(mmc, info.start);
174 #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
175 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
181 #ifdef CONFIG_SPL_OS_BOOT
182 static int mmc_load_image_raw_os(struct mmc *mmc)
187 count = blk_dread(mmc_get_blk_desc(mmc),
188 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
189 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
190 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
192 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
193 puts("mmc_load_image_raw_os: mmc block read error\n");
198 ret = mmc_load_image_raw_sector(mmc,
199 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
203 if (spl_image.os != IH_OS_LINUX) {
204 puts("Expected Linux image is not found. Trying to start U-boot\n");
211 int spl_start_uboot(void)
215 static int mmc_load_image_raw_os(struct mmc *mmc)
221 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
222 int spl_mmc_do_fs_boot(struct mmc *mmc)
226 #ifdef CONFIG_SPL_FAT_SUPPORT
227 if (!spl_start_uboot()) {
228 err = spl_load_image_fat_os(mmc_get_blk_desc(mmc),
229 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
233 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
234 err = spl_load_image_fat(mmc_get_blk_desc(mmc),
235 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
236 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
241 #ifdef CONFIG_SPL_EXT_SUPPORT
242 if (!spl_start_uboot()) {
243 err = spl_load_image_ext_os(&mmc->block_dev,
244 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
248 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
249 err = spl_load_image_ext(&mmc->block_dev,
250 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
251 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
257 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
264 int spl_mmc_do_fs_boot(struct mmc *mmc)
270 int spl_mmc_load_image(u32 boot_device)
272 struct mmc *mmc = NULL;
275 __maybe_unused int part;
277 err = spl_mmc_find_device(&mmc, boot_device);
283 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
284 printf("spl: mmc init failed with error: %d\n", err);
289 boot_mode = spl_boot_mode(boot_device);
292 case MMCSD_MODE_EMMCBOOT:
294 * We need to check what the partition is configured to.
295 * 1 and 2 match up to boot0 / boot1 and 7 is user data
296 * which is the first physical partition (0).
298 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
303 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
305 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
306 puts("spl: mmc partition switch failed\n");
312 debug("spl: mmc boot mode: raw\n");
314 if (!spl_start_uboot()) {
315 err = mmc_load_image_raw_os(mmc);
320 err = mmc_load_image_raw_partition(mmc,
321 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
324 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
325 err = mmc_load_image_raw_sector(mmc,
326 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
330 /* If RAW mode fails, try FS mode. */
332 debug("spl: mmc boot mode: fs\n");
334 err = spl_mmc_do_fs_boot(mmc);
339 case MMCSD_MODE_UNDEFINED:
340 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
342 puts("spl: mmc: wrong boot mode\n");