1 // SPDX-License-Identifier: GPL-2.0+
4 * Texas Instruments, <www.ti.com>
6 * Aneesh V <aneesh@ti.com>
14 #include <linux/compiler.h>
16 #include <asm/u-boot.h>
20 #include <imx_container.h>
22 static ulong
h_spl_load_read(struct spl_load_info
*load
, ulong off
,
23 ulong size
, void *buf
)
25 struct blk_desc
*bd
= load
->priv
;
26 lbaint_t sector
= off
>> bd
->log2blksz
;
27 lbaint_t count
= size
>> bd
->log2blksz
;
29 return blk_dread(bd
, sector
, count
, buf
) << bd
->log2blksz
;
32 static __maybe_unused
unsigned long spl_mmc_raw_uboot_offset(int part
)
34 #if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
36 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET
;
43 int mmc_load_image_raw_sector(struct spl_image_info
*spl_image
,
44 struct spl_boot_device
*bootdev
,
45 struct mmc
*mmc
, unsigned long sector
)
48 struct blk_desc
*bd
= mmc_get_blk_desc(mmc
);
49 struct spl_load_info load
;
52 spl_set_bl_len(&load
, bd
->blksz
);
53 load
.read
= h_spl_load_read
;
54 ret
= spl_load(spl_image
, bootdev
, &load
, 0, sector
<< bd
->log2blksz
);
56 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
57 puts("mmc_load_image_raw_sector: mmc block read error\n");
65 static int spl_mmc_get_device_index(u32 boot_device
)
67 switch (boot_device
) {
68 case BOOT_DEVICE_MMC1
:
70 case BOOT_DEVICE_MMC2
:
71 case BOOT_DEVICE_MMC2_2
:
75 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
76 printf("spl: unsupported mmc boot device.\n");
82 static int spl_mmc_find_device(struct mmc
**mmcp
, u32 boot_device
)
86 mmc_dev
= spl_mmc_get_device_index(boot_device
);
90 #if CONFIG_IS_ENABLED(DM_MMC)
91 err
= mmc_init_device(mmc_dev
);
93 err
= mmc_initialize(NULL
);
96 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
97 printf("spl: could not initialize mmc. error: %d\n", err
);
101 *mmcp
= find_mmc_device(mmc_dev
);
102 err
= *mmcp
? 0 : -ENODEV
;
104 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
105 printf("spl: could not find mmc device %d. error: %d\n",
114 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
115 static int mmc_load_image_raw_partition(struct spl_image_info
*spl_image
,
116 struct spl_boot_device
*bootdev
,
117 struct mmc
*mmc
, int partition
,
118 unsigned long sector
)
120 struct disk_partition info
;
123 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
125 /* Only support MBR so DOS_ENTRY_NUMBERS */
126 for (type_part
= 1; type_part
<= DOS_ENTRY_NUMBERS
; type_part
++) {
127 err
= part_get_info(mmc_get_blk_desc(mmc
), type_part
, &info
);
131 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE
) {
132 partition
= type_part
;
138 err
= part_get_info(mmc_get_blk_desc(mmc
), partition
, &info
);
140 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
141 puts("spl: partition error\n");
146 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
147 return mmc_load_image_raw_sector(spl_image
, bootdev
, mmc
, info
.start
+ sector
);
149 return mmc_load_image_raw_sector(spl_image
, bootdev
, mmc
, info
.start
);
154 #if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
155 static int mmc_load_image_raw_os(struct spl_image_info
*spl_image
,
156 struct spl_boot_device
*bootdev
,
161 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
164 count
= blk_dread(mmc_get_blk_desc(mmc
),
165 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR
,
166 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS
,
167 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR
);
168 if (count
!= CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS
) {
169 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
170 puts("mmc_load_image_raw_os: mmc block read error\n");
174 #endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
176 ret
= mmc_load_image_raw_sector(spl_image
, bootdev
, mmc
,
177 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR
);
181 if (spl_image
->os
!= IH_OS_LINUX
&& spl_image
->os
!= IH_OS_TEE
) {
182 puts("Expected image is not found. Trying to start U-Boot\n");
189 static int mmc_load_image_raw_os(struct spl_image_info
*spl_image
,
190 struct spl_boot_device
*bootdev
,
197 #ifndef CONFIG_SPL_OS_BOOT
198 int spl_start_uboot(void)
204 #ifdef CONFIG_SYS_MMCSD_FS_BOOT
205 static int spl_mmc_do_fs_boot(struct spl_image_info
*spl_image
,
206 struct spl_boot_device
*bootdev
,
208 const char *filename
)
212 __maybe_unused
int partition
= CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
;
214 #if CONFIG_SYS_MMCSD_FS_BOOT_PARTITION == -1
216 struct disk_partition info
;
217 debug("Checking for the first MBR bootable partition\n");
218 for (int type_part
= 1; type_part
<= DOS_ENTRY_NUMBERS
; type_part
++) {
219 err
= part_get_info(mmc_get_blk_desc(mmc
), type_part
, &info
);
222 debug("Partition %d is of type %d and bootable=%d\n", type_part
, info
.sys_ind
, info
.bootable
);
223 if (info
.bootable
!= 0) {
224 debug("Partition %d is bootable, using it\n", type_part
);
225 partition
= type_part
;
229 printf("Using first bootable partition: %d\n", partition
);
230 if (partition
== CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
) {
236 #ifdef CONFIG_SPL_FS_FAT
237 if (!spl_start_uboot()) {
238 err
= spl_load_image_fat_os(spl_image
, bootdev
, mmc_get_blk_desc(mmc
),
243 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
244 err
= spl_load_image_fat(spl_image
, bootdev
, mmc_get_blk_desc(mmc
),
251 #ifdef CONFIG_SPL_FS_EXT4
252 if (!spl_start_uboot()) {
253 err
= spl_load_image_ext_os(spl_image
, bootdev
, mmc_get_blk_desc(mmc
),
258 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
259 err
= spl_load_image_ext(spl_image
, bootdev
, mmc_get_blk_desc(mmc
),
267 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
275 u32 __weak
spl_mmc_boot_mode(struct mmc
*mmc
, const u32 boot_device
)
277 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
278 return MMCSD_MODE_FS
;
279 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
280 return MMCSD_MODE_EMMCBOOT
;
282 return MMCSD_MODE_RAW
;
286 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
287 int __weak
spl_mmc_boot_partition(const u32 boot_device
)
289 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
;
293 unsigned long __weak
arch_spl_mmc_get_uboot_raw_sector(struct mmc
*mmc
,
294 unsigned long raw_sect
)
299 unsigned long __weak
board_spl_mmc_get_uboot_raw_sector(struct mmc
*mmc
,
300 unsigned long raw_sect
)
302 return arch_spl_mmc_get_uboot_raw_sector(mmc
, raw_sect
);
305 unsigned long __weak
spl_mmc_get_uboot_raw_sector(struct mmc
*mmc
,
306 unsigned long raw_sect
)
308 return board_spl_mmc_get_uboot_raw_sector(mmc
, raw_sect
);
311 int default_spl_mmc_emmc_boot_partition(struct mmc
*mmc
)
314 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
315 part
= CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
;
318 * We need to check what the partition is configured to.
319 * 1 and 2 match up to boot0 / boot1 and 7 is user data
320 * which is the first physical partition (0).
322 part
= EXT_CSD_EXTRACT_BOOT_PART(mmc
->part_config
);
329 int __weak
spl_mmc_emmc_boot_partition(struct mmc
*mmc
)
331 return default_spl_mmc_emmc_boot_partition(mmc
);
334 static int spl_mmc_get_mmc_devnum(struct mmc
*mmc
)
336 struct blk_desc
*block_dev
;
337 #if !CONFIG_IS_ENABLED(BLK)
338 block_dev
= &mmc
->block_dev
;
340 block_dev
= mmc_get_blk_desc(mmc
);
342 return block_dev
->devnum
;
345 static struct mmc
*mmc
;
347 void spl_mmc_clear_cache(void)
352 int spl_mmc_load(struct spl_image_info
*spl_image
,
353 struct spl_boot_device
*bootdev
,
354 const char *filename
,
356 unsigned long raw_sect
)
360 __maybe_unused
int part
= 0;
363 /* Perform peripheral init only once for an mmc device */
364 mmc_dev
= spl_mmc_get_device_index(bootdev
->boot_device
);
365 if (!mmc
|| spl_mmc_get_mmc_devnum(mmc
) != mmc_dev
) {
366 err
= spl_mmc_find_device(&mmc
, bootdev
->boot_device
);
373 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
374 printf("spl: mmc init failed with error: %d\n", err
);
380 boot_mode
= spl_mmc_boot_mode(mmc
, bootdev
->boot_device
);
383 case MMCSD_MODE_EMMCBOOT
:
384 part
= spl_mmc_emmc_boot_partition(mmc
);
386 if (CONFIG_IS_ENABLED(MMC_TINY
))
387 err
= mmc_switch_part(mmc
, part
);
389 err
= blk_dselect_hwpart(mmc_get_blk_desc(mmc
), part
);
392 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
393 puts("spl: mmc partition switch failed\n");
399 debug("spl: mmc boot mode: raw\n");
401 if (!spl_start_uboot()) {
402 err
= mmc_load_image_raw_os(spl_image
, bootdev
, mmc
);
407 raw_sect
= spl_mmc_get_uboot_raw_sector(mmc
, raw_sect
);
409 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
410 err
= mmc_load_image_raw_partition(spl_image
, bootdev
,
416 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
417 err
= mmc_load_image_raw_sector(spl_image
, bootdev
, mmc
,
418 raw_sect
+ spl_mmc_raw_uboot_offset(part
));
422 /* If RAW mode fails, try FS mode. */
423 #ifdef CONFIG_SYS_MMCSD_FS_BOOT
425 debug("spl: mmc boot mode: fs\n");
427 err
= spl_mmc_do_fs_boot(spl_image
, bootdev
, mmc
, filename
);
433 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
435 puts("spl: mmc: wrong boot mode\n");
442 int spl_mmc_load_image(struct spl_image_info
*spl_image
,
443 struct spl_boot_device
*bootdev
)
445 return spl_mmc_load(spl_image
, bootdev
,
446 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
447 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
,
451 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
452 spl_mmc_boot_partition(bootdev
->boot_device
),
456 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
457 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
463 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1
, spl_mmc_load_image
);
464 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2
, spl_mmc_load_image
);
465 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2
, spl_mmc_load_image
);