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(0, 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(0, sector
, image_size_sectors
,
49 (void *)(ulong
)spl_image
.load_addr
);
50 debug("read %x sectors to %x\n", image_size_sectors
,
55 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
56 puts("spl: mmc block read error\n");
64 int spl_mmc_get_device_index(u32 boot_device
)
66 switch (boot_device
) {
67 case BOOT_DEVICE_MMC1
:
69 case BOOT_DEVICE_MMC2
:
70 case BOOT_DEVICE_MMC2_2
:
74 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
75 printf("spl: unsupported mmc boot device.\n");
82 static int spl_mmc_find_device(struct mmc
**mmc
, u32 boot_device
)
87 mmc_dev
= spl_mmc_get_device_index(boot_device
);
91 err
= mmc_initialize(NULL
);
93 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
94 printf("spl: could not initialize mmc. error: %d\n", err
);
99 err
= uclass_get_device(UCLASS_MMC
, mmc_dev
, &dev
);
101 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
102 printf("spl: could not find mmc device. error: %d\n", err
);
108 *mmc
= mmc_get_mmc_dev(dev
);
109 return *mmc
!= NULL
? 0 : -ENODEV
;
112 static int spl_mmc_find_device(struct mmc
**mmc
, u32 boot_device
)
116 mmc_dev
= spl_mmc_get_device_index(boot_device
);
120 err
= mmc_initialize(gd
->bd
);
122 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
123 printf("spl: could not initialize mmc. error: %d\n", err
);
128 /* We register only one device. So, the dev id is always 0 */
129 *mmc
= find_mmc_device(mmc_dev
);
131 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
132 puts("spl: mmc device not found\n");
141 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
142 static int mmc_load_image_raw_partition(struct mmc
*mmc
, int partition
)
144 disk_partition_t info
;
147 err
= get_partition_info(&mmc
->block_dev
, partition
, &info
);
149 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
150 puts("spl: partition error\n");
155 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
156 return mmc_load_image_raw_sector(mmc
, info
.start
+
157 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
159 return mmc_load_image_raw_sector(mmc
, info
.start
);
163 #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
164 static int mmc_load_image_raw_partition(struct mmc
*mmc
, int partition
)
170 #ifdef CONFIG_SPL_OS_BOOT
171 static int mmc_load_image_raw_os(struct mmc
*mmc
)
175 count
= mmc
->block_dev
.block_read(0,
176 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR
,
177 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS
,
178 (void *) CONFIG_SYS_SPL_ARGS_ADDR
);
180 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
181 puts("spl: mmc block read error\n");
186 return mmc_load_image_raw_sector(mmc
,
187 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR
);
190 int spl_start_uboot(void)
194 static int mmc_load_image_raw_os(struct mmc
*mmc
)
200 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
201 int spl_mmc_do_fs_boot(struct mmc
*mmc
)
205 #ifdef CONFIG_SPL_FAT_SUPPORT
206 if (!spl_start_uboot()) {
207 err
= spl_load_image_fat_os(&mmc
->block_dev
,
208 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
212 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
213 err
= spl_load_image_fat(&mmc
->block_dev
,
214 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
215 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
220 #ifdef CONFIG_SPL_EXT_SUPPORT
221 if (!spl_start_uboot()) {
222 err
= spl_load_image_ext_os(&mmc
->block_dev
,
223 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
227 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
228 err
= spl_load_image_ext(&mmc
->block_dev
,
229 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
230 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
236 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
243 int spl_mmc_do_fs_boot(struct mmc
*mmc
)
249 int spl_mmc_load_image(u32 boot_device
)
254 __maybe_unused
int part
;
256 err
= spl_mmc_find_device(&mmc
, boot_device
);
262 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
263 printf("spl: mmc init failed with error: %d\n", err
);
268 boot_mode
= spl_boot_mode();
271 case MMCSD_MODE_EMMCBOOT
:
273 * We need to check what the partition is configured to.
274 * 1 and 2 match up to boot0 / boot1 and 7 is user data
275 * which is the first physical partition (0).
277 part
= (mmc
->part_config
>> 3) & PART_ACCESS_MASK
;
282 err
= mmc_switch_part(0, part
);
284 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
285 puts("spl: mmc partition switch failed\n");
291 debug("spl: mmc boot mode: raw\n");
293 if (!spl_start_uboot()) {
294 err
= mmc_load_image_raw_os(mmc
);
299 err
= mmc_load_image_raw_partition(mmc
,
300 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
);
303 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
304 err
= mmc_load_image_raw_sector(mmc
,
305 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
311 debug("spl: mmc boot mode: fs\n");
313 err
= spl_mmc_do_fs_boot(mmc
);
318 case MMCSD_MODE_UNDEFINED
:
319 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
321 puts("spl: mmc: wrong boot mode\n");