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 #ifdef CONFIG_SPL_LOAD_FIT
45 static ulong
h_spl_load_read(struct spl_load_info
*load
, ulong sector
,
46 ulong count
, void *buf
)
48 struct mmc
*mmc
= load
->dev
;
50 return mmc
->block_dev
.block_read(&mmc
->block_dev
, sector
, count
, buf
);
54 static int mmc_load_image_raw_sector(struct mmc
*mmc
, unsigned long sector
)
57 struct image_header
*header
;
60 header
= (struct image_header
*)(CONFIG_SYS_TEXT_BASE
-
61 sizeof(struct image_header
));
63 /* read image header to find the image size & load address */
64 count
= mmc
->block_dev
.block_read(&mmc
->block_dev
, sector
, 1, header
);
65 debug("hdr read sector %lx, count=%lu\n", sector
, count
);
71 switch (image_get_magic(header
)) {
73 ret
= mmc_load_legacy(mmc
, sector
, header
);
75 #ifdef CONFIG_SPL_LOAD_FIT
77 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(&load
, sector
, header
);
95 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
96 puts("spl: mmc block read error\n");
104 int spl_mmc_get_device_index(u32 boot_device
)
106 switch (boot_device
) {
107 case BOOT_DEVICE_MMC1
:
109 case BOOT_DEVICE_MMC2
:
110 case BOOT_DEVICE_MMC2_2
:
114 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
115 printf("spl: unsupported mmc boot device.\n");
121 static int spl_mmc_find_device(struct mmc
**mmcp
, u32 boot_device
)
128 mmc_dev
= spl_mmc_get_device_index(boot_device
);
132 err
= mmc_initialize(NULL
);
134 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
135 printf("spl: could not initialize mmc. error: %d\n", err
);
141 err
= uclass_get_device(UCLASS_MMC
, mmc_dev
, &dev
);
143 *mmcp
= mmc_get_mmc_dev(dev
);
145 *mmcp
= find_mmc_device(mmc_dev
);
146 err
= *mmcp
? 0 : -ENODEV
;
149 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
150 printf("spl: could not find mmc device. error: %d\n", err
);
158 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
159 static int mmc_load_image_raw_partition(struct mmc
*mmc
, int partition
)
161 disk_partition_t info
;
164 err
= part_get_info(&mmc
->block_dev
, partition
, &info
);
166 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
167 puts("spl: partition error\n");
172 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
173 return mmc_load_image_raw_sector(mmc
, info
.start
+
174 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
176 return mmc_load_image_raw_sector(mmc
, info
.start
);
180 #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
181 static int mmc_load_image_raw_partition(struct mmc
*mmc
, int partition
)
187 #ifdef CONFIG_SPL_OS_BOOT
188 static int mmc_load_image_raw_os(struct mmc
*mmc
)
192 count
= mmc
->block_dev
.block_read(&mmc
->block_dev
,
193 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR
,
194 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS
,
195 (void *) CONFIG_SYS_SPL_ARGS_ADDR
);
197 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
198 puts("spl: mmc block read error\n");
203 return mmc_load_image_raw_sector(mmc
,
204 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR
);
207 int spl_start_uboot(void)
211 static int mmc_load_image_raw_os(struct mmc
*mmc
)
217 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
218 int spl_mmc_do_fs_boot(struct mmc
*mmc
)
222 #ifdef CONFIG_SPL_FAT_SUPPORT
223 if (!spl_start_uboot()) {
224 err
= spl_load_image_fat_os(&mmc
->block_dev
,
225 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
229 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
230 err
= spl_load_image_fat(&mmc
->block_dev
,
231 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
232 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
237 #ifdef CONFIG_SPL_EXT_SUPPORT
238 if (!spl_start_uboot()) {
239 err
= spl_load_image_ext_os(&mmc
->block_dev
,
240 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
244 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
245 err
= spl_load_image_ext(&mmc
->block_dev
,
246 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
247 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
253 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
260 int spl_mmc_do_fs_boot(struct mmc
*mmc
)
266 int spl_mmc_load_image(u32 boot_device
)
268 struct mmc
*mmc
= NULL
;
271 __maybe_unused
int part
;
273 err
= spl_mmc_find_device(&mmc
, boot_device
);
279 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
280 printf("spl: mmc init failed with error: %d\n", err
);
285 boot_mode
= spl_boot_mode();
288 case MMCSD_MODE_EMMCBOOT
:
290 * We need to check what the partition is configured to.
291 * 1 and 2 match up to boot0 / boot1 and 7 is user data
292 * which is the first physical partition (0).
294 part
= (mmc
->part_config
>> 3) & PART_ACCESS_MASK
;
299 err
= mmc_switch_part(0, part
);
301 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
302 puts("spl: mmc partition switch failed\n");
308 debug("spl: mmc boot mode: raw\n");
310 if (!spl_start_uboot()) {
311 err
= mmc_load_image_raw_os(mmc
);
316 err
= mmc_load_image_raw_partition(mmc
,
317 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
);
320 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
321 err
= mmc_load_image_raw_sector(mmc
,
322 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
326 /* If RAW mode fails, try FS mode. */
328 debug("spl: mmc boot mode: fs\n");
330 err
= spl_mmc_do_fs_boot(mmc
);
335 case MMCSD_MODE_UNDEFINED
:
336 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
338 puts("spl: mmc: wrong boot mode\n");