blob: f4481b90463a9a65f8ec9a498251fa73663d9c35 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Schwarze21d2d82011-09-14 15:33:34 -04002/*
3 * (C) Copyright 2010
4 * Texas Instruments, <www.ti.com>
5 *
6 * Aneesh V <aneesh@ti.com>
Simon Schwarze21d2d82011-09-14 15:33:34 -04007 */
Simon Glassf22851a2015-06-23 15:38:51 -06008#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass655306c2020-05-10 11:39:58 -060010#include <part.h>
Tom Rini28591df2012-08-13 12:03:19 -070011#include <spl.h>
Sean Anderson29e338f2023-11-08 11:48:50 -050012#include <spl_load.h>
Paul Kocialkowski0d139722015-04-27 10:20:22 +020013#include <linux/compiler.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020014#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040015#include <asm/u-boot.h>
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020016#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040017#include <mmc.h>
Tom Rini1530e8a2013-06-28 14:43:01 -040018#include <image.h>
Sean Anderson952ed672023-10-14 16:47:44 -040019#include <imx_container.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040020
Sean Anderson7d8d6132023-11-08 11:48:40 -050021static ulong h_spl_load_read(struct spl_load_info *load, ulong off,
22 ulong size, void *buf)
Simon Glassb52c7fc2016-02-22 22:55:58 -070023{
Sean Anderson7d8d6132023-11-08 11:48:40 -050024 struct blk_desc *bd = load->priv;
25 lbaint_t sector = off >> bd->log2blksz;
26 lbaint_t count = size >> bd->log2blksz;
Simon Glassb52c7fc2016-02-22 22:55:58 -070027
Sean Anderson7d8d6132023-11-08 11:48:40 -050028 return blk_dread(bd, sector, count, buf) << bd->log2blksz;
Simon Glassb52c7fc2016-02-22 22:55:58 -070029}
Simon Glassb52c7fc2016-02-22 22:55:58 -070030
Baruch Siachfef81be2020-01-15 09:08:08 +020031static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
32{
33#if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
34 if (part == 0)
35 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
36#endif
37
38 return 0;
39}
40
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090041static __maybe_unused
42int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010043 struct spl_boot_device *bootdev,
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090044 struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040045{
Sean Anderson29e338f2023-11-08 11:48:50 -050046 int ret;
Marek Vasut1dda46f2018-08-14 11:27:02 +020047 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Sean Anderson29e338f2023-11-08 11:48:50 -050048 struct spl_load_info load;
Tom Rini1530e8a2013-06-28 14:43:01 -040049
Sean Anderson29e338f2023-11-08 11:48:50 -050050 load.priv = bd;
51 spl_set_bl_len(&load, bd->blksz);
52 load.read = h_spl_load_read;
53 ret = spl_load(spl_image, bootdev, &load, 0, sector << bd->log2blksz);
Simon Glassb52c7fc2016-02-22 22:55:58 -070054 if (ret) {
Paul Burtona8015fd2013-09-04 16:12:24 +010055#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -030056 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010057#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020058 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020059 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020060
61 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040062}
63
Masahiro Yamada2a4a20c2017-06-22 16:52:36 +090064static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +020065{
66 switch (boot_device) {
67 case BOOT_DEVICE_MMC1:
68 return 0;
69 case BOOT_DEVICE_MMC2:
70 case BOOT_DEVICE_MMC2_2:
71 return 1;
72 }
73
74#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
75 printf("spl: unsupported mmc boot device.\n");
76#endif
77
78 return -ENODEV;
79}
80
Simon Glass4faf63b2015-12-02 10:59:12 -070081static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020082{
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +020083 int err, mmc_dev;
84
85 mmc_dev = spl_mmc_get_device_index(boot_device);
86 if (mmc_dev < 0)
87 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020088
Lokesh Vutlac59b41c2019-09-09 14:40:36 +053089#if CONFIG_IS_ENABLED(DM_MMC)
90 err = mmc_init_device(mmc_dev);
91#else
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020092 err = mmc_initialize(NULL);
Lokesh Vutlac59b41c2019-09-09 14:40:36 +053093#endif /* DM_MMC */
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020094 if (err) {
95#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
96 printf("spl: could not initialize mmc. error: %d\n", err);
97#endif
98 return err;
99 }
Simon Glass51ace322015-12-02 10:59:13 -0700100 *mmcp = find_mmc_device(mmc_dev);
101 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200102 if (err) {
103#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanf5920d02018-09-18 08:11:55 +0000104 printf("spl: could not find mmc device %d. error: %d\n",
105 mmc_dev, err);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200106#endif
107 return err;
108 }
109
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200110 return 0;
111}
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200112
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800113#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glassee306792016-09-24 18:20:13 -0600114static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100115 struct spl_boot_device *bootdev,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500116 struct mmc *mmc, int partition,
117 unsigned long sector)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100118{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600119 struct disk_partition info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200120 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100121
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800122#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
123 int type_part;
124 /* Only support MBR so DOS_ENTRY_NUMBERS */
125 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
126 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
127 if (err)
128 continue;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200129 if (info.sys_ind ==
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800130 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
131 partition = type_part;
132 break;
133 }
134 }
135#endif
136
Simon Glass55877f92016-07-05 17:10:18 -0600137 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200138 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100139#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200140 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100141#endif
142 return -1;
143 }
144
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200145#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohárdda8f882022-01-14 14:31:38 +0100146 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200147#else
Pali Rohárdda8f882022-01-14 14:31:38 +0100148 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200149#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100150}
151#endif
152
Alexandru Gagniuc99426c82021-12-30 10:39:59 -0600153#if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
Simon Glassee306792016-09-24 18:20:13 -0600154static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100155 struct spl_boot_device *bootdev,
Simon Glassee306792016-09-24 18:20:13 -0600156 struct mmc *mmc)
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000157{
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530158 int ret;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200159
Elena Popa6532fab2023-08-08 16:42:15 +0300160#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
York Suncea88212018-06-26 09:44:39 -0700161 unsigned long count;
162
Simon Glass70061ba2016-06-12 23:30:32 -0600163 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200164 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
165 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
Simon Glass259cdb42023-09-26 08:14:17 -0600166 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
Tom Rini195bc4b2017-08-14 21:01:30 -0400167 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100168#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300169 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100170#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000171 return -1;
172 }
York Suncea88212018-06-26 09:44:39 -0700173#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000174
Pali Rohárdda8f882022-01-14 14:31:38 +0100175 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200176 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530177 if (ret)
178 return ret;
179
Alexandru Gagniuc36fd3ba2021-07-15 14:19:23 -0500180 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
Michal Simek50fa1182023-05-17 09:17:16 +0200181 puts("Expected image is not found. Trying to start U-Boot\n");
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530182 return -ENOENT;
183 }
184
185 return 0;
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000186}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200187#else
Simon Glassee306792016-09-24 18:20:13 -0600188static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100189 struct spl_boot_device *bootdev,
Simon Glassee306792016-09-24 18:20:13 -0600190 struct mmc *mmc)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200191{
192 return -ENOSYS;
193}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000194#endif
195
Alexandru Gagniuc99426c82021-12-30 10:39:59 -0600196#ifndef CONFIG_SPL_OS_BOOT
197int spl_start_uboot(void)
198{
199 return 1;
200}
201#endif
202
Pali Rohára0811552023-01-21 15:13:08 +0100203#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Pali Rohárdda8f882022-01-14 14:31:38 +0100204static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
205 struct spl_boot_device *bootdev,
206 struct mmc *mmc,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500207 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200208{
209 int err = -ENOSYS;
210
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400211 __maybe_unused int partition = CONFIG_SYS_MMCSD_FS_BOOT_PARTITION;
212
213#if CONFIG_SYS_MMCSD_FS_BOOT_PARTITION == -1
214 {
215 struct disk_partition info;
216 debug("Checking for the first MBR bootable partition\n");
217 for (int type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
218 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
219 if (err)
220 continue;
221 debug("Partition %d is of type %d and bootable=%d\n", type_part, info.sys_ind, info.bootable);
222 if (info.bootable != 0) {
223 debug("Partition %d is bootable, using it\n", type_part);
224 partition = type_part;
225 break;
226 }
227 }
228 printf("Using first bootable partition: %d\n", partition);
229 if (partition == CONFIG_SYS_MMCSD_FS_BOOT_PARTITION) {
230 return -ENOSYS;
231 }
232 }
233#endif
234
Tien Fong Chee6fd0a712019-01-23 14:20:03 +0800235#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200236 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100237 err = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400238 partition);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200239 if (!err)
240 return err;
241 }
242#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohárdda8f882022-01-14 14:31:38 +0100243 err = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400244 partition,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500245 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200246 if (!err)
247 return err;
248#endif
249#endif
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800250#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200251 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100252 err = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400253 partition);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200254 if (!err)
255 return err;
256 }
257#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohárdda8f882022-01-14 14:31:38 +0100258 err = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400259 partition,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500260 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200261 if (!err)
262 return err;
263#endif
264#endif
265
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800266#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200267 err = -ENOENT;
268#endif
269
270 return err;
271}
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200272#endif
273
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100274u32 __weak spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100275{
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800276#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100277 return MMCSD_MODE_FS;
278#elif defined(CONFIG_SUPPORT_EMMC_BOOT)
279 return MMCSD_MODE_EMMCBOOT;
280#else
281 return MMCSD_MODE_RAW;
282#endif
283}
284
Patrick Delaunay113aa502018-03-20 10:54:51 +0100285#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200286int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay113aa502018-03-20 10:54:51 +0100287{
288 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
289}
290#endif
291
Marek Vasutf9a921e2023-10-16 18:16:12 +0200292unsigned long __weak arch_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
293 unsigned long raw_sect)
294{
295 return raw_sect;
296}
297
298unsigned long __weak board_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
299 unsigned long raw_sect)
300{
301 return arch_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
302}
303
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530304unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
305 unsigned long raw_sect)
Peng Fan315cf592019-09-23 10:18:40 +0800306{
Marek Vasutf9a921e2023-10-16 18:16:12 +0200307 return board_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fan315cf592019-09-23 10:18:40 +0800308}
309
Marek Vasutde1ce232021-07-03 04:55:32 +0200310int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
311{
312 int part;
313#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
314 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
315#else
316 /*
317 * We need to check what the partition is configured to.
318 * 1 and 2 match up to boot0 / boot1 and 7 is user data
319 * which is the first physical partition (0).
320 */
Pali Roháreafdbe52023-03-11 11:44:27 +0100321 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
Marek Vasutde1ce232021-07-03 04:55:32 +0200322 if (part == 7)
323 part = 0;
324#endif
325 return part;
326}
327
328int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
329{
330 return default_spl_mmc_emmc_boot_partition(mmc);
331}
332
Harald Seileraf8b7b72022-07-11 14:35:32 +0200333static int spl_mmc_get_mmc_devnum(struct mmc *mmc)
334{
335 struct blk_desc *block_dev;
336#if !CONFIG_IS_ENABLED(BLK)
337 block_dev = &mmc->block_dev;
338#else
Sean Andersond2b254e2023-10-07 21:47:48 -0400339 block_dev = mmc_get_blk_desc(mmc);
Harald Seileraf8b7b72022-07-11 14:35:32 +0200340#endif
341 return block_dev->devnum;
342}
343
Sean Anderson13e6d912023-10-14 16:47:54 -0400344static struct mmc *mmc;
345
346void spl_mmc_clear_cache(void)
347{
348 mmc = NULL;
349}
350
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500351int spl_mmc_load(struct spl_image_info *spl_image,
352 struct spl_boot_device *bootdev,
353 const char *filename,
354 int raw_part,
355 unsigned long raw_sect)
Simon Schwarze21d2d82011-09-14 15:33:34 -0400356{
Simon Schwarze21d2d82011-09-14 15:33:34 -0400357 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600358 int err = 0;
Baruch Siachfef81be2020-01-15 09:08:08 +0200359 __maybe_unused int part = 0;
Harald Seileraf8b7b72022-07-11 14:35:32 +0200360 int mmc_dev;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400361
Harald Seileraf8b7b72022-07-11 14:35:32 +0200362 /* Perform peripheral init only once for an mmc device */
363 mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
364 if (!mmc || spl_mmc_get_mmc_devnum(mmc) != mmc_dev) {
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500365 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
366 if (err)
367 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400368
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500369 err = mmc_init(mmc);
370 if (err) {
371 mmc = NULL;
Paul Burtona8015fd2013-09-04 16:12:24 +0100372#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500373 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100374#endif
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500375 return err;
376 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400377 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000378
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100379 boot_mode = spl_mmc_boot_mode(mmc, bootdev->boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200380 err = -EINVAL;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200381 switch (boot_mode) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200382 case MMCSD_MODE_EMMCBOOT:
Marek Vasutde1ce232021-07-03 04:55:32 +0200383 part = spl_mmc_emmc_boot_partition(mmc);
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200384
Mans Rullgardec046a52019-09-03 16:17:17 +0200385 if (CONFIG_IS_ENABLED(MMC_TINY))
386 err = mmc_switch_part(mmc, part);
387 else
388 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutf537e392016-12-01 02:06:33 +0100389
Mans Rullgardec046a52019-09-03 16:17:17 +0200390 if (err) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200391#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgardec046a52019-09-03 16:17:17 +0200392 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200393#endif
Mans Rullgardec046a52019-09-03 16:17:17 +0200394 return err;
395 }
396 /* Fall through */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200397 case MMCSD_MODE_RAW:
398 debug("spl: mmc boot mode: raw\n");
399
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200400 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100401 err = mmc_load_image_raw_os(spl_image, bootdev, mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200402 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200403 return err;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200404 }
Peng Fandc7d3222019-10-23 01:43:30 +0000405
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530406 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fandc7d3222019-10-23 01:43:30 +0000407
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800408#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Pali Rohárdda8f882022-01-14 14:31:38 +0100409 err = mmc_load_image_raw_partition(spl_image, bootdev,
410 mmc, raw_part,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500411 raw_sect);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200412 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200413 return err;
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800414#endif
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200415#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohárdda8f882022-01-14 14:31:38 +0100416 err = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Baruch Siachfef81be2020-01-15 09:08:08 +0200417 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200418 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200419 return err;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200420#endif
Guillaume GARDET834dd882016-02-18 18:17:36 +0100421 /* If RAW mode fails, try FS mode. */
Pali Rohára0811552023-01-21 15:13:08 +0100422#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200423 case MMCSD_MODE_FS:
424 debug("spl: mmc boot mode: fs\n");
425
Pali Rohárdda8f882022-01-14 14:31:38 +0100426 err = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200427 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200428 return err;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200429
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200430 break;
Pali Rohára0811552023-01-21 15:13:08 +0100431#endif
Paul Burtona8015fd2013-09-04 16:12:24 +0100432#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200433 default:
434 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100435#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200436 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200437
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200438 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400439}
Simon Glassd449cb82016-09-24 18:20:00 -0600440
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500441int spl_mmc_load_image(struct spl_image_info *spl_image,
442 struct spl_boot_device *bootdev)
443{
444 return spl_mmc_load(spl_image, bootdev,
445#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
446 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
447#else
448 NULL,
449#endif
450#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200451 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500452#else
453 0,
454#endif
455#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
456 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
457#else
458 0);
459#endif
460}
461
Simon Glass4fc1f252016-11-30 15:30:50 -0700462SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
463SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
464SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);