3 * Texas Instruments, <www.ti.com>
5 * Aneesh V <aneesh@ti.com>
7 * SPDX-License-Identifier: GPL-2.0+
11 #include <linux/compiler.h>
12 #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 block read error\n");
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
)
75 err
= mmc
->block_dev
.block_read(0,
76 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR
,
77 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS
,
78 (void *)CONFIG_SYS_SPL_ARGS_ADDR
);
80 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
81 printf("spl: mmc block read error\n");
86 return mmc_load_image_raw_sector(mmc
,
87 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR
);
91 void spl_mmc_load_image(void)
96 __maybe_unused
int part
;
98 mmc_initialize(gd
->bd
);
100 /* We register only one device. So, the dev id is always 0 */
101 mmc
= find_mmc_device(0);
103 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
104 puts("spl: mmc device not found\n");
111 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
112 printf("spl: mmc init failed with error: %d\n", err
);
117 boot_mode
= spl_boot_mode();
120 debug("spl: mmc boot mode: raw\n");
122 #ifdef CONFIG_SPL_OS_BOOT
123 if (!spl_start_uboot()) {
124 err
= mmc_load_image_raw_os(mmc
);
129 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
130 err
= mmc_load_image_raw_partition(mmc
,
131 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
);
133 err
= mmc_load_image_raw_sector(mmc
,
134 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
138 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
140 debug("spl: mmc boot mode: fs\n");
142 #ifdef CONFIG_SPL_FAT_SUPPORT
143 #ifdef CONFIG_SPL_OS_BOOT
144 if (!spl_start_uboot()) {
145 err
= spl_load_image_fat_os(&mmc
->block_dev
,
146 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
151 err
= spl_load_image_fat(&mmc
->block_dev
,
152 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
153 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
157 #ifdef CONFIG_SPL_EXT_SUPPORT
158 #ifdef CONFIG_SPL_OS_BOOT
159 if (!spl_start_uboot()) {
160 err
= spl_load_image_ext_os(&mmc
->block_dev
,
161 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
166 err
= spl_load_image_ext(&mmc
->block_dev
,
167 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
168 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
173 #ifdef CONFIG_SUPPORT_EMMC_BOOT
174 case MMCSD_MODE_EMMCBOOT
:
176 * We need to check what the partition is configured to.
177 * 1 and 2 match up to boot0 / boot1 and 7 is user data
178 * which is the first physical partition (0).
180 part
= (mmc
->part_config
>> 3) & PART_ACCESS_MASK
;
185 if (mmc_switch_part(0, part
)) {
186 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
187 puts("spl: mmc partition switch failed\n");
192 #ifdef CONFIG_SPL_OS_BOOT
193 if (!spl_start_uboot()) {
194 err
= mmc_load_image_raw_os(mmc
);
199 err
= mmc_load_image_raw_sector(mmc
,
200 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
204 case MMCSD_MODE_UNDEFINED
:
206 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
208 puts("spl: mmc: no boot mode left to try\n");
210 puts("spl: mmc: wrong boot mode\n");