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
;
28 ret
= spl_parse_image_header(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
= mmc
->block_dev
.block_read(&mmc
->block_dev
, sector
,
39 (void *)(ulong
)spl_image
.load_addr
);
40 debug("read %x sectors to %x\n", image_size_sectors
,
42 if (count
!= image_size_sectors
)
48 static ulong
h_spl_load_read(struct spl_load_info
*load
, ulong sector
,
49 ulong count
, void *buf
)
51 struct mmc
*mmc
= load
->dev
;
53 return mmc
->block_dev
.block_read(&mmc
->block_dev
, sector
, count
, buf
);
56 static int mmc_load_image_raw_sector(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
= mmc
->block_dev
.block_read(&mmc
->block_dev
, 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(&load
, sector
, header
);
85 ret
= mmc_load_legacy(mmc
, sector
, header
);
90 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
91 puts("mmc_load_image_raw_sector: mmc block read error\n");
99 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_PARTITION
154 static int mmc_load_image_raw_partition(struct mmc
*mmc
, int partition
)
156 disk_partition_t info
;
159 err
= part_get_info(&mmc
->block_dev
, partition
, &info
);
161 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
162 puts("spl: partition error\n");
167 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
168 return mmc_load_image_raw_sector(mmc
, info
.start
+
169 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
171 return mmc_load_image_raw_sector(mmc
, info
.start
);
175 #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
176 static int mmc_load_image_raw_partition(struct mmc
*mmc
, int partition
)
182 #ifdef CONFIG_SPL_OS_BOOT
183 static int mmc_load_image_raw_os(struct mmc
*mmc
)
188 count
= mmc
->block_dev
.block_read(&mmc
->block_dev
,
189 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR
,
190 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS
,
191 (void *) CONFIG_SYS_SPL_ARGS_ADDR
);
193 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
194 puts("mmc_load_image_raw_os: mmc block read error\n");
199 ret
= mmc_load_image_raw_sector(mmc
,
200 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR
);
204 if (spl_image
.os
!= IH_OS_LINUX
) {
205 puts("Expected Linux image is not found. Trying to start U-boot\n");
212 int spl_start_uboot(void)
216 static int mmc_load_image_raw_os(struct mmc
*mmc
)
222 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
223 int spl_mmc_do_fs_boot(struct mmc
*mmc
)
227 #ifdef CONFIG_SPL_FAT_SUPPORT
228 if (!spl_start_uboot()) {
229 err
= spl_load_image_fat_os(&mmc
->block_dev
,
230 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
234 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
235 err
= spl_load_image_fat(&mmc
->block_dev
,
236 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
237 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
242 #ifdef CONFIG_SPL_EXT_SUPPORT
243 if (!spl_start_uboot()) {
244 err
= spl_load_image_ext_os(&mmc
->block_dev
,
245 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
249 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
250 err
= spl_load_image_ext(&mmc
->block_dev
,
251 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
252 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
258 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
265 int spl_mmc_do_fs_boot(struct mmc
*mmc
)
271 int spl_mmc_load_image(u32 boot_device
)
273 struct mmc
*mmc
= NULL
;
276 __maybe_unused
int part
;
278 err
= spl_mmc_find_device(&mmc
, boot_device
);
284 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
285 printf("spl: mmc init failed with error: %d\n", err
);
290 boot_mode
= spl_boot_mode();
293 case MMCSD_MODE_EMMCBOOT
:
295 * We need to check what the partition is configured to.
296 * 1 and 2 match up to boot0 / boot1 and 7 is user data
297 * which is the first physical partition (0).
299 part
= (mmc
->part_config
>> 3) & PART_ACCESS_MASK
;
304 err
= blk_dselect_hwpart(mmc_get_blk_desc(mmc
), part
);
306 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
307 puts("spl: mmc partition switch failed\n");
313 debug("spl: mmc boot mode: raw\n");
315 if (!spl_start_uboot()) {
316 err
= mmc_load_image_raw_os(mmc
);
321 err
= mmc_load_image_raw_partition(mmc
,
322 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
);
325 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
326 err
= mmc_load_image_raw_sector(mmc
,
327 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
331 /* If RAW mode fails, try FS mode. */
333 debug("spl: mmc boot mode: fs\n");
335 err
= spl_mmc_do_fs_boot(mmc
);
340 case MMCSD_MODE_UNDEFINED
:
341 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
343 puts("spl: mmc: wrong boot mode\n");