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);
56 int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
57 struct mmc *mmc, unsigned long sector)
60 struct image_header *header;
63 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
64 sizeof(struct image_header));
66 /* read image header to find the image size & load address */
67 count = blk_dread(mmc_get_blk_desc(mmc), sector, 1, header);
68 debug("hdr read sector %lx, count=%lu\n", sector, count);
74 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
75 image_get_magic(header) == FDT_MAGIC) {
76 struct spl_load_info load;
82 load.bl_len = mmc->read_bl_len;
83 load.read = h_spl_load_read;
84 ret = spl_load_simple_fit(spl_image, &load, sector, header);
86 ret = mmc_load_legacy(spl_image, mmc, sector, header);
91 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
92 puts("mmc_load_image_raw_sector: mmc block read error\n");
100 static int spl_mmc_get_device_index(u32 boot_device)
102 switch (boot_device) {
103 case BOOT_DEVICE_MMC1:
105 case BOOT_DEVICE_MMC2:
106 case BOOT_DEVICE_MMC2_2:
110 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
111 printf("spl: unsupported mmc boot device.\n");
117 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
119 #if CONFIG_IS_ENABLED(DM_MMC)
124 mmc_dev = spl_mmc_get_device_index(boot_device);
128 err = mmc_initialize(NULL);
130 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
131 printf("spl: could not initialize mmc. error: %d\n", err);
136 #if CONFIG_IS_ENABLED(DM_MMC)
137 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
139 *mmcp = mmc_get_mmc_dev(dev);
141 *mmcp = find_mmc_device(mmc_dev);
142 err = *mmcp ? 0 : -ENODEV;
145 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
146 printf("spl: could not find mmc device. error: %d\n", err);
154 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
155 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
156 struct mmc *mmc, int partition)
158 disk_partition_t info;
161 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
163 /* Only support MBR so DOS_ENTRY_NUMBERS */
164 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
165 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
169 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
170 partition = type_part;
176 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
178 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
179 puts("spl: partition error\n");
184 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
185 return mmc_load_image_raw_sector(spl_image, mmc,
186 info.start + CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
188 return mmc_load_image_raw_sector(spl_image, mmc, info.start);
193 #ifdef CONFIG_SPL_OS_BOOT
194 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
200 count = blk_dread(mmc_get_blk_desc(mmc),
201 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
202 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
203 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
204 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
205 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
206 puts("mmc_load_image_raw_os: mmc block read error\n");
211 ret = mmc_load_image_raw_sector(spl_image, mmc,
212 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
216 if (spl_image->os != IH_OS_LINUX) {
217 puts("Expected Linux image is not found. Trying to start U-boot\n");
224 int spl_start_uboot(void)
228 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
235 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
236 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc)
240 #ifdef CONFIG_SPL_FAT_SUPPORT
241 if (!spl_start_uboot()) {
242 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
243 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
247 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
248 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
249 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
250 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
255 #ifdef CONFIG_SPL_EXT_SUPPORT
256 if (!spl_start_uboot()) {
257 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
258 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
262 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
263 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
264 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
265 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
271 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
278 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc)
284 int spl_mmc_load_image(struct spl_image_info *spl_image,
285 struct spl_boot_device *bootdev)
287 struct mmc *mmc = NULL;
290 __maybe_unused int part;
292 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
298 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
299 printf("spl: mmc init failed with error: %d\n", err);
304 boot_mode = spl_boot_mode(bootdev->boot_device);
307 case MMCSD_MODE_EMMCBOOT:
309 * We need to check what the partition is configured to.
310 * 1 and 2 match up to boot0 / boot1 and 7 is user data
311 * which is the first physical partition (0).
313 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
318 if (CONFIG_IS_ENABLED(MMC_TINY))
319 err = mmc_switch_part(mmc, part);
321 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
324 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
325 puts("spl: mmc partition switch failed\n");
331 debug("spl: mmc boot mode: raw\n");
333 if (!spl_start_uboot()) {
334 err = mmc_load_image_raw_os(spl_image, mmc);
338 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
339 err = mmc_load_image_raw_partition(spl_image, mmc,
340 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
344 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
345 err = mmc_load_image_raw_sector(spl_image, mmc,
346 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
350 /* If RAW mode fails, try FS mode. */
352 debug("spl: mmc boot mode: fs\n");
354 err = spl_mmc_do_fs_boot(spl_image, mmc);
359 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
361 puts("spl: mmc: wrong boot mode\n");
368 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
369 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
370 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);