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 spl_image_info *spl_image, struct mmc *mmc,
22 ulong sector, 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 %lx\n", image_size_sectors,
40 spl_image->load_addr);
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 spl_image_info *spl_image,
56 struct mmc *mmc, unsigned long sector)
59 struct image_header *header;
62 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
63 sizeof(struct image_header));
65 /* read image header to find the image size & load address */
66 count = blk_dread(mmc_get_blk_desc(mmc), sector, 1, header);
67 debug("hdr read sector %lx, count=%lu\n", sector, count);
73 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
74 image_get_magic(header) == FDT_MAGIC) {
75 struct spl_load_info load;
81 load.bl_len = mmc->read_bl_len;
82 load.read = h_spl_load_read;
83 ret = spl_load_simple_fit(spl_image, &load, sector, header);
85 ret = mmc_load_legacy(spl_image, mmc, sector, header);
90 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
91 puts("mmc_load_image_raw_sector: mmc block read error\n");
99 static int spl_mmc_get_device_index(u32 boot_device)
101 switch (boot_device) {
102 case BOOT_DEVICE_MMC1:
104 case BOOT_DEVICE_MMC2:
105 case BOOT_DEVICE_MMC2_2:
109 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
110 printf("spl: unsupported mmc boot device.\n");
116 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
123 mmc_dev = spl_mmc_get_device_index(boot_device);
127 err = mmc_initialize(NULL);
129 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
130 printf("spl: could not initialize mmc. error: %d\n", err);
136 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
138 *mmcp = mmc_get_mmc_dev(dev);
140 *mmcp = find_mmc_device(mmc_dev);
141 err = *mmcp ? 0 : -ENODEV;
144 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
145 printf("spl: could not find mmc device. error: %d\n", err);
153 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
154 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
155 struct mmc *mmc, int partition)
157 disk_partition_t info;
160 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
162 /* Only support MBR so DOS_ENTRY_NUMBERS */
163 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
164 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
168 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
169 partition = type_part;
175 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
177 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
178 puts("spl: partition error\n");
183 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
184 return mmc_load_image_raw_sector(spl_image, mmc,
185 info.start + CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
187 return mmc_load_image_raw_sector(spl_image, mmc, info.start);
192 #ifdef CONFIG_SPL_OS_BOOT
193 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
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);
204 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
205 puts("mmc_load_image_raw_os: mmc block read error\n");
210 ret = mmc_load_image_raw_sector(spl_image, mmc,
211 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
215 if (spl_image->os != IH_OS_LINUX) {
216 puts("Expected Linux image is not found. Trying to start U-boot\n");
223 int spl_start_uboot(void)
227 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
234 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
235 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc)
239 #ifdef CONFIG_SPL_FAT_SUPPORT
240 if (!spl_start_uboot()) {
241 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
242 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
246 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
247 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
248 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
249 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
254 #ifdef CONFIG_SPL_EXT_SUPPORT
255 if (!spl_start_uboot()) {
256 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
257 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
261 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
262 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
263 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
264 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
270 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
277 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc)
283 int spl_mmc_load_image(struct spl_image_info *spl_image,
284 struct spl_boot_device *bootdev)
286 struct mmc *mmc = NULL;
289 __maybe_unused int part;
291 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
297 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
298 printf("spl: mmc init failed with error: %d\n", err);
303 boot_mode = spl_boot_mode(bootdev->boot_device);
306 case MMCSD_MODE_EMMCBOOT:
308 * We need to check what the partition is configured to.
309 * 1 and 2 match up to boot0 / boot1 and 7 is user data
310 * which is the first physical partition (0).
312 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
317 if (CONFIG_IS_ENABLED(MMC_TINY))
318 err = mmc_switch_part(mmc, part);
320 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
323 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
324 puts("spl: mmc partition switch failed\n");
330 debug("spl: mmc boot mode: raw\n");
332 if (!spl_start_uboot()) {
333 err = mmc_load_image_raw_os(spl_image, mmc);
337 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
338 err = mmc_load_image_raw_partition(spl_image, mmc,
339 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
343 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
344 err = mmc_load_image_raw_sector(spl_image, mmc,
345 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
349 /* If RAW mode fails, try FS mode. */
351 debug("spl: mmc boot mode: fs\n");
353 err = spl_mmc_do_fs_boot(spl_image, mmc);
358 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
360 puts("spl: mmc: wrong boot mode\n");
367 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
368 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
369 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);