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
;
27 spl_parse_image_header(header
);
28 /* convert size to sectors - round up */
29 image_size_sectors
= (spl_image
.size
+ mmc
->read_bl_len
- 1) /
32 /* Read the header too to avoid extra memcpy */
33 count
= mmc
->block_dev
.block_read(&mmc
->block_dev
, sector
,
35 (void *)(ulong
)spl_image
.load_addr
);
36 debug("read %x sectors to %x\n", image_size_sectors
,
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 mmc
->block_dev
.block_read(&mmc
->block_dev
, sector
, count
, buf
);
52 static int mmc_load_image_raw_sector(struct mmc
*mmc
, unsigned long sector
)
55 struct image_header
*header
;
58 header
= (struct image_header
*)(CONFIG_SYS_TEXT_BASE
-
59 sizeof(struct image_header
));
61 /* read image header to find the image size & load address */
62 count
= mmc
->block_dev
.block_read(&mmc
->block_dev
, sector
, 1, header
);
63 debug("hdr read sector %lx, count=%lu\n", sector
, count
);
69 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT
) &&
70 image_get_magic(header
) == FDT_MAGIC
) {
71 struct spl_load_info load
;
76 load
.bl_len
= mmc
->read_bl_len
;
77 load
.read
= h_spl_load_read
;
78 ret
= spl_load_simple_fit(&load
, sector
, header
);
80 ret
= mmc_load_legacy(mmc
, sector
, header
);
85 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
86 puts("spl: mmc block read error\n");
94 int spl_mmc_get_device_index(u32 boot_device
)
96 switch (boot_device
) {
97 case BOOT_DEVICE_MMC1
:
99 case BOOT_DEVICE_MMC2
:
100 case BOOT_DEVICE_MMC2_2
:
104 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
105 printf("spl: unsupported mmc boot device.\n");
111 static int spl_mmc_find_device(struct mmc
**mmcp
, u32 boot_device
)
118 mmc_dev
= spl_mmc_get_device_index(boot_device
);
122 err
= mmc_initialize(NULL
);
124 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
125 printf("spl: could not initialize mmc. error: %d\n", err
);
131 err
= uclass_get_device(UCLASS_MMC
, mmc_dev
, &dev
);
133 *mmcp
= mmc_get_mmc_dev(dev
);
135 *mmcp
= find_mmc_device(mmc_dev
);
136 err
= *mmcp
? 0 : -ENODEV
;
139 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
140 printf("spl: could not find mmc device. error: %d\n", err
);
148 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
149 static int mmc_load_image_raw_partition(struct mmc
*mmc
, int partition
)
151 disk_partition_t info
;
154 err
= part_get_info(&mmc
->block_dev
, partition
, &info
);
156 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
157 puts("spl: partition error\n");
162 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
163 return mmc_load_image_raw_sector(mmc
, info
.start
+
164 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
166 return mmc_load_image_raw_sector(mmc
, info
.start
);
170 #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
171 static int mmc_load_image_raw_partition(struct mmc
*mmc
, int partition
)
177 #ifdef CONFIG_SPL_OS_BOOT
178 static int mmc_load_image_raw_os(struct mmc
*mmc
)
182 count
= mmc
->block_dev
.block_read(&mmc
->block_dev
,
183 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR
,
184 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS
,
185 (void *) CONFIG_SYS_SPL_ARGS_ADDR
);
187 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
188 puts("spl: mmc block read error\n");
193 return mmc_load_image_raw_sector(mmc
,
194 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR
);
197 int spl_start_uboot(void)
201 static int mmc_load_image_raw_os(struct mmc
*mmc
)
207 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
208 int spl_mmc_do_fs_boot(struct mmc
*mmc
)
212 #ifdef CONFIG_SPL_FAT_SUPPORT
213 if (!spl_start_uboot()) {
214 err
= spl_load_image_fat_os(&mmc
->block_dev
,
215 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
219 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
220 err
= spl_load_image_fat(&mmc
->block_dev
,
221 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
222 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
227 #ifdef CONFIG_SPL_EXT_SUPPORT
228 if (!spl_start_uboot()) {
229 err
= spl_load_image_ext_os(&mmc
->block_dev
,
230 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
234 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
235 err
= spl_load_image_ext(&mmc
->block_dev
,
236 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
237 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
243 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
250 int spl_mmc_do_fs_boot(struct mmc
*mmc
)
256 int spl_mmc_load_image(u32 boot_device
)
258 struct mmc
*mmc
= NULL
;
261 __maybe_unused
int part
;
263 err
= spl_mmc_find_device(&mmc
, boot_device
);
269 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
270 printf("spl: mmc init failed with error: %d\n", err
);
275 boot_mode
= spl_boot_mode();
278 case MMCSD_MODE_EMMCBOOT
:
280 * We need to check what the partition is configured to.
281 * 1 and 2 match up to boot0 / boot1 and 7 is user data
282 * which is the first physical partition (0).
284 part
= (mmc
->part_config
>> 3) & PART_ACCESS_MASK
;
289 err
= mmc_switch_part(0, part
);
291 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
292 puts("spl: mmc partition switch failed\n");
298 debug("spl: mmc boot mode: raw\n");
300 if (!spl_start_uboot()) {
301 err
= mmc_load_image_raw_os(mmc
);
306 err
= mmc_load_image_raw_partition(mmc
,
307 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
);
310 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
311 err
= mmc_load_image_raw_sector(mmc
,
312 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
316 /* If RAW mode fails, try FS mode. */
318 debug("spl: mmc boot mode: fs\n");
320 err
= spl_mmc_do_fs_boot(mmc
);
325 case MMCSD_MODE_UNDEFINED
:
326 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
328 puts("spl: mmc: wrong boot mode\n");