3 * Texas Instruments, <www.ti.com>
5 * Aneesh V <aneesh@ti.com>
7 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/u-boot.h>
16 DECLARE_GLOBAL_DATA_PTR
;
18 static int mmc_load_image_raw_sector(struct mmc
*mmc
, unsigned long sector
)
21 u32 image_size_sectors
;
22 struct image_header
*header
;
24 header
= (struct image_header
*)(CONFIG_SYS_TEXT_BASE
-
25 sizeof(struct image_header
));
27 /* read image header to find the image size & load address */
28 err
= mmc
->block_dev
.block_read(0, sector
, 1, header
);
32 if (image_get_magic(header
) != IH_MAGIC
)
35 spl_parse_image_header(header
);
37 /* convert size to sectors - round up */
38 image_size_sectors
= (spl_image
.size
+ mmc
->read_bl_len
- 1) /
41 /* Read the header too to avoid extra memcpy */
42 err
= mmc
->block_dev
.block_read(0, sector
, image_size_sectors
,
43 (void *)spl_image
.load_addr
);
46 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
48 printf("spl: mmc blk read err - %lu\n", err
);
54 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
55 static int mmc_load_image_raw_partition(struct mmc
*mmc
, int partition
)
57 disk_partition_t info
;
59 if (get_partition_info(&mmc
->block_dev
, partition
, &info
)) {
60 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
61 printf("spl: partition error\n");
66 return mmc_load_image_raw_sector(mmc
, info
.start
);
70 #ifdef CONFIG_SPL_OS_BOOT
71 static int mmc_load_image_raw_os(struct mmc
*mmc
)
73 if (!mmc
->block_dev
.block_read(0,
74 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR
,
75 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS
,
76 (void *)CONFIG_SYS_SPL_ARGS_ADDR
)) {
77 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
78 printf("mmc args blk read error\n");
83 return mmc_load_image_raw_sector(mmc
,
84 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR
);
88 void spl_mmc_load_image(void)
94 mmc_initialize(gd
->bd
);
95 /* We register only one device. So, the dev id is always 0 */
96 mmc
= find_mmc_device(0);
98 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
99 puts("spl: mmc device not found!!\n");
106 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
107 printf("spl: mmc init failed: err - %d\n", err
);
112 boot_mode
= spl_boot_mode();
113 if (boot_mode
== MMCSD_MODE_RAW
) {
114 debug("boot mode - RAW\n");
115 #ifdef CONFIG_SPL_OS_BOOT
116 if (spl_start_uboot() || mmc_load_image_raw_os(mmc
))
118 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
119 err
= mmc_load_image_raw_partition(mmc
,
120 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
);
122 err
= mmc_load_image_raw_sector(mmc
,
123 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
125 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
127 if (err
|| boot_mode
== MMCSD_MODE_FS
) {
128 debug("boot mode - FS\n");
129 #ifdef CONFIG_SPL_FAT_SUPPORT
130 #ifdef CONFIG_SPL_OS_BOOT
131 if (spl_start_uboot() || spl_load_image_fat_os(&mmc
->block_dev
,
132 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
))
134 err
= spl_load_image_fat(&mmc
->block_dev
,
135 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
136 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
138 #endif /* CONFIG_SPL_FAT_SUPPORT */
140 #ifdef CONFIG_SPL_EXT_SUPPORT
141 #ifdef CONFIG_SPL_OS_BOOT
142 if (spl_start_uboot() || spl_load_image_ext_os(&mmc
->block_dev
,
143 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
))
145 err
= spl_load_image_ext(&mmc
->block_dev
,
146 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
147 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
148 #endif /* CONFIG_SPL_EXT_SUPPORT */
150 #endif /* defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) */
151 #ifdef CONFIG_SUPPORT_EMMC_BOOT
152 } else if (boot_mode
== MMCSD_MODE_EMMCBOOT
) {
154 * We need to check what the partition is configured to.
155 * 1 and 2 match up to boot0 / boot1 and 7 is user data
156 * which is the first physical partition (0).
158 int part
= (mmc
->part_config
>> 3) & PART_ACCESS_MASK
;
163 if (mmc_switch_part(0, part
)) {
164 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
165 puts("MMC partition switch failed\n");
169 #ifdef CONFIG_SPL_OS_BOOT
170 if (spl_start_uboot() || mmc_load_image_raw_os(mmc
))
172 err
= mmc_load_image_raw_sector(mmc
,
173 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
176 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
177 puts("spl: wrong MMC boot mode\n");