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_image_raw_sector(struct mmc
*mmc
, unsigned long sector
)
24 u32 image_size_sectors
;
25 struct image_header
*header
;
27 header
= (struct image_header
*)(CONFIG_SYS_TEXT_BASE
-
28 sizeof(struct image_header
));
30 /* read image header to find the image size & load address */
31 count
= mmc
->block_dev
.block_read(&mmc
->block_dev
, sector
, 1, header
);
32 debug("read sector %lx, count=%lu\n", sector
, count
);
36 if (image_get_magic(header
) != IH_MAGIC
) {
41 spl_parse_image_header(header
);
43 /* convert size to sectors - round up */
44 image_size_sectors
= (spl_image
.size
+ mmc
->read_bl_len
- 1) /
47 /* Read the header too to avoid extra memcpy */
48 count
= mmc
->block_dev
.block_read(&mmc
->block_dev
, sector
,
50 (void *)(ulong
)spl_image
.load_addr
);
51 debug("read %x sectors to %x\n", image_size_sectors
,
56 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
57 puts("spl: mmc block read error\n");
65 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
)
89 mmc_dev
= spl_mmc_get_device_index(boot_device
);
93 err
= mmc_initialize(NULL
);
95 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
96 printf("spl: could not initialize mmc. error: %d\n", err
);
102 err
= uclass_get_device(UCLASS_MMC
, mmc_dev
, &dev
);
104 *mmcp
= mmc_get_mmc_dev(dev
);
106 *mmcp
= find_mmc_device(mmc_dev
);
107 err
= *mmcp
? 0 : -ENODEV
;
110 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
111 printf("spl: could not find mmc device. error: %d\n", err
);
119 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
120 static int mmc_load_image_raw_partition(struct mmc
*mmc
, int partition
)
122 disk_partition_t info
;
125 err
= get_partition_info(&mmc
->block_dev
, partition
, &info
);
127 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
128 puts("spl: partition error\n");
133 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
134 return mmc_load_image_raw_sector(mmc
, info
.start
+
135 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
137 return mmc_load_image_raw_sector(mmc
, info
.start
);
141 #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
142 static int mmc_load_image_raw_partition(struct mmc
*mmc
, int partition
)
148 #ifdef CONFIG_SPL_OS_BOOT
149 static int mmc_load_image_raw_os(struct mmc
*mmc
)
153 count
= mmc
->block_dev
.block_read(&mmc
->block_dev
,
154 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR
,
155 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS
,
156 (void *) CONFIG_SYS_SPL_ARGS_ADDR
);
158 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
159 puts("spl: mmc block read error\n");
164 return mmc_load_image_raw_sector(mmc
,
165 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR
);
168 int spl_start_uboot(void)
172 static int mmc_load_image_raw_os(struct mmc
*mmc
)
178 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
179 int spl_mmc_do_fs_boot(struct mmc
*mmc
)
183 #ifdef CONFIG_SPL_FAT_SUPPORT
184 if (!spl_start_uboot()) {
185 err
= spl_load_image_fat_os(&mmc
->block_dev
,
186 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
190 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
191 err
= spl_load_image_fat(&mmc
->block_dev
,
192 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
193 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
198 #ifdef CONFIG_SPL_EXT_SUPPORT
199 if (!spl_start_uboot()) {
200 err
= spl_load_image_ext_os(&mmc
->block_dev
,
201 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
205 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
206 err
= spl_load_image_ext(&mmc
->block_dev
,
207 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
208 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
214 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
221 int spl_mmc_do_fs_boot(struct mmc
*mmc
)
227 int spl_mmc_load_image(u32 boot_device
)
229 struct mmc
*mmc
= NULL
;
232 __maybe_unused
int part
;
234 err
= spl_mmc_find_device(&mmc
, boot_device
);
240 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
241 printf("spl: mmc init failed with error: %d\n", err
);
246 boot_mode
= spl_boot_mode();
249 case MMCSD_MODE_EMMCBOOT
:
251 * We need to check what the partition is configured to.
252 * 1 and 2 match up to boot0 / boot1 and 7 is user data
253 * which is the first physical partition (0).
255 part
= (mmc
->part_config
>> 3) & PART_ACCESS_MASK
;
260 err
= mmc_switch_part(0, part
);
262 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
263 puts("spl: mmc partition switch failed\n");
269 debug("spl: mmc boot mode: raw\n");
271 if (!spl_start_uboot()) {
272 err
= mmc_load_image_raw_os(mmc
);
277 err
= mmc_load_image_raw_partition(mmc
,
278 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
);
281 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
282 err
= mmc_load_image_raw_sector(mmc
,
283 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
287 /* If RAW mode fails, try FS mode. */
289 debug("spl: mmc boot mode: fs\n");
291 err
= spl_mmc_do_fs_boot(mmc
);
296 case MMCSD_MODE_UNDEFINED
:
297 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
299 puts("spl: mmc: wrong boot mode\n");