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");
81 static int spl_mmc_find_device(struct mmc
**mmcp
, u32 boot_device
)
88 mmc_dev
= spl_mmc_get_device_index(boot_device
);
92 err
= mmc_initialize(NULL
);
94 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
95 printf("spl: could not initialize mmc. error: %d\n", err
);
101 err
= uclass_get_device(UCLASS_MMC
, mmc_dev
, &dev
);
103 *mmcp
= mmc_get_mmc_dev(dev
);
105 *mmcp
= find_mmc_device(mmc_dev
);
106 err
= *mmcp
? 0 : -ENODEV
;
109 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
110 printf("spl: could not find mmc device. error: %d\n", err
);
118 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
119 static int mmc_load_image_raw_partition(struct mmc
*mmc
, int partition
)
121 disk_partition_t info
;
124 err
= get_partition_info(&mmc
->block_dev
, partition
, &info
);
126 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
127 puts("spl: partition error\n");
132 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
133 return mmc_load_image_raw_sector(mmc
, info
.start
+
134 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
136 return mmc_load_image_raw_sector(mmc
, info
.start
);
140 #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
141 static int mmc_load_image_raw_partition(struct mmc
*mmc
, int partition
)
147 #ifdef CONFIG_SPL_OS_BOOT
148 static int mmc_load_image_raw_os(struct mmc
*mmc
)
152 count
= mmc
->block_dev
.block_read(0,
153 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR
,
154 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS
,
155 (void *) CONFIG_SYS_SPL_ARGS_ADDR
);
157 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
158 puts("spl: mmc block read error\n");
163 return mmc_load_image_raw_sector(mmc
,
164 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR
);
167 int spl_start_uboot(void)
171 static int mmc_load_image_raw_os(struct mmc
*mmc
)
177 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
178 int spl_mmc_do_fs_boot(struct mmc
*mmc
)
182 #ifdef CONFIG_SPL_FAT_SUPPORT
183 if (!spl_start_uboot()) {
184 err
= spl_load_image_fat_os(&mmc
->block_dev
,
185 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
189 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
190 err
= spl_load_image_fat(&mmc
->block_dev
,
191 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
192 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
197 #ifdef CONFIG_SPL_EXT_SUPPORT
198 if (!spl_start_uboot()) {
199 err
= spl_load_image_ext_os(&mmc
->block_dev
,
200 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
);
204 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
205 err
= spl_load_image_ext(&mmc
->block_dev
,
206 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
,
207 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
);
213 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
220 int spl_mmc_do_fs_boot(struct mmc
*mmc
)
226 int spl_mmc_load_image(u32 boot_device
)
228 struct mmc
*mmc
= NULL
;
231 __maybe_unused
int part
;
233 err
= spl_mmc_find_device(&mmc
, boot_device
);
239 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
240 printf("spl: mmc init failed with error: %d\n", err
);
245 boot_mode
= spl_boot_mode();
248 case MMCSD_MODE_EMMCBOOT
:
250 * We need to check what the partition is configured to.
251 * 1 and 2 match up to boot0 / boot1 and 7 is user data
252 * which is the first physical partition (0).
254 part
= (mmc
->part_config
>> 3) & PART_ACCESS_MASK
;
259 err
= mmc_switch_part(0, part
);
261 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
262 puts("spl: mmc partition switch failed\n");
268 debug("spl: mmc boot mode: raw\n");
270 if (!spl_start_uboot()) {
271 err
= mmc_load_image_raw_os(mmc
);
276 err
= mmc_load_image_raw_partition(mmc
,
277 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
);
280 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
281 err
= mmc_load_image_raw_sector(mmc
,
282 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
);
288 debug("spl: mmc boot mode: fs\n");
290 err
= spl_mmc_do_fs_boot(mmc
);
295 case MMCSD_MODE_UNDEFINED
:
296 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
298 puts("spl: mmc: wrong boot mode\n");