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
)
183 count
= mmc
->block_dev
.block_read(&mmc
->block_dev
,
184 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR
,
185 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS
,
186 (void *) CONFIG_SYS_SPL_ARGS_ADDR
);
188 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
189 puts("spl: mmc block read error\n");
194 ret
= mmc_load_image_raw_sector(mmc
,
195 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR
);
199 if (spl_image
.os
!= IH_OS_LINUX
) {
200 puts("Expected Linux image is not found. Trying to start U-boot\n");
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");