blob: ccab0be4be23d4d5ab83bc72fc7e2828e0c3b4d7 [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>
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020015#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040016#include <mmc.h>
Tom Rini1530e8a2013-06-28 14:43:01 -040017#include <image.h>
Sean Anderson952ed672023-10-14 16:47:44 -040018#include <imx_container.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040019
Sean Anderson7d8d6132023-11-08 11:48:40 -050020static ulong h_spl_load_read(struct spl_load_info *load, ulong off,
21 ulong size, void *buf)
Simon Glassb52c7fc2016-02-22 22:55:58 -070022{
Sean Anderson7d8d6132023-11-08 11:48:40 -050023 struct blk_desc *bd = load->priv;
24 lbaint_t sector = off >> bd->log2blksz;
25 lbaint_t count = size >> bd->log2blksz;
Simon Glassb52c7fc2016-02-22 22:55:58 -070026
Sean Anderson7d8d6132023-11-08 11:48:40 -050027 return blk_dread(bd, sector, count, buf) << bd->log2blksz;
Simon Glassb52c7fc2016-02-22 22:55:58 -070028}
Simon Glassb52c7fc2016-02-22 22:55:58 -070029
Baruch Siachfef81be2020-01-15 09:08:08 +020030static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
31{
32#if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
33 if (part == 0)
34 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
35#endif
36
37 return 0;
38}
39
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090040static __maybe_unused
41int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010042 struct spl_boot_device *bootdev,
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090043 struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040044{
Sean Anderson29e338f2023-11-08 11:48:50 -050045 int ret;
Marek Vasut1dda46f2018-08-14 11:27:02 +020046 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Sean Anderson29e338f2023-11-08 11:48:50 -050047 struct spl_load_info load;
Tom Rini1530e8a2013-06-28 14:43:01 -040048
Sean Anderson29e338f2023-11-08 11:48:50 -050049 load.priv = bd;
50 spl_set_bl_len(&load, bd->blksz);
51 load.read = h_spl_load_read;
52 ret = spl_load(spl_image, bootdev, &load, 0, sector << bd->log2blksz);
Simon Glassb52c7fc2016-02-22 22:55:58 -070053 if (ret) {
Paul Burtona8015fd2013-09-04 16:12:24 +010054#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -030055 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010056#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020057 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020058 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020059
60 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040061}
62
Masahiro Yamada2a4a20c2017-06-22 16:52:36 +090063static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +020064{
65 switch (boot_device) {
66 case BOOT_DEVICE_MMC1:
67 return 0;
68 case BOOT_DEVICE_MMC2:
69 case BOOT_DEVICE_MMC2_2:
70 return 1;
71 }
72
73#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
74 printf("spl: unsupported mmc boot device.\n");
75#endif
76
77 return -ENODEV;
78}
79
Simon Glass4faf63b2015-12-02 10:59:12 -070080static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020081{
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +020082 int err, mmc_dev;
83
84 mmc_dev = spl_mmc_get_device_index(boot_device);
85 if (mmc_dev < 0)
86 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020087
Lokesh Vutlac59b41c2019-09-09 14:40:36 +053088#if CONFIG_IS_ENABLED(DM_MMC)
89 err = mmc_init_device(mmc_dev);
90#else
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020091 err = mmc_initialize(NULL);
Lokesh Vutlac59b41c2019-09-09 14:40:36 +053092#endif /* DM_MMC */
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020093 if (err) {
94#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
95 printf("spl: could not initialize mmc. error: %d\n", err);
96#endif
97 return err;
98 }
Simon Glass51ace322015-12-02 10:59:13 -070099 *mmcp = find_mmc_device(mmc_dev);
100 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200101 if (err) {
102#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanf5920d02018-09-18 08:11:55 +0000103 printf("spl: could not find mmc device %d. error: %d\n",
104 mmc_dev, err);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200105#endif
106 return err;
107 }
108
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200109 return 0;
110}
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200111
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800112#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glassee306792016-09-24 18:20:13 -0600113static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100114 struct spl_boot_device *bootdev,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500115 struct mmc *mmc, int partition,
116 unsigned long sector)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100117{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600118 struct disk_partition info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200119 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100120
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800121#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
122 int type_part;
123 /* Only support MBR so DOS_ENTRY_NUMBERS */
124 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
125 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
126 if (err)
127 continue;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200128 if (info.sys_ind ==
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800129 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
130 partition = type_part;
131 break;
132 }
133 }
134#endif
135
Simon Glass55877f92016-07-05 17:10:18 -0600136 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200137 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100138#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200139 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100140#endif
141 return -1;
142 }
143
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200144#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohárdda8f882022-01-14 14:31:38 +0100145 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200146#else
Pali Rohárdda8f882022-01-14 14:31:38 +0100147 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200148#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100149}
150#endif
151
Alexandru Gagniuc99426c82021-12-30 10:39:59 -0600152#if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
Simon Glassee306792016-09-24 18:20:13 -0600153static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100154 struct spl_boot_device *bootdev,
Simon Glassee306792016-09-24 18:20:13 -0600155 struct mmc *mmc)
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000156{
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530157 int ret;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200158
Elena Popa6532fab2023-08-08 16:42:15 +0300159#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
York Suncea88212018-06-26 09:44:39 -0700160 unsigned long count;
161
Simon Glass70061ba2016-06-12 23:30:32 -0600162 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200163 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
164 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
Simon Glass259cdb42023-09-26 08:14:17 -0600165 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
Tom Rini195bc4b2017-08-14 21:01:30 -0400166 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100167#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300168 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100169#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000170 return -1;
171 }
York Suncea88212018-06-26 09:44:39 -0700172#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000173
Pali Rohárdda8f882022-01-14 14:31:38 +0100174 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200175 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530176 if (ret)
177 return ret;
178
Alexandru Gagniuc36fd3ba2021-07-15 14:19:23 -0500179 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
Michal Simek50fa1182023-05-17 09:17:16 +0200180 puts("Expected image is not found. Trying to start U-Boot\n");
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530181 return -ENOENT;
182 }
183
184 return 0;
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000185}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200186#else
Simon Glassee306792016-09-24 18:20:13 -0600187static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100188 struct spl_boot_device *bootdev,
Simon Glassee306792016-09-24 18:20:13 -0600189 struct mmc *mmc)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200190{
191 return -ENOSYS;
192}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000193#endif
194
Alexandru Gagniuc99426c82021-12-30 10:39:59 -0600195#ifndef CONFIG_SPL_OS_BOOT
196int spl_start_uboot(void)
197{
198 return 1;
199}
200#endif
201
Pali Rohára0811552023-01-21 15:13:08 +0100202#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Pali Rohárdda8f882022-01-14 14:31:38 +0100203static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
204 struct spl_boot_device *bootdev,
205 struct mmc *mmc,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500206 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200207{
208 int err = -ENOSYS;
209
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400210 __maybe_unused int partition = CONFIG_SYS_MMCSD_FS_BOOT_PARTITION;
211
212#if CONFIG_SYS_MMCSD_FS_BOOT_PARTITION == -1
213 {
214 struct disk_partition info;
215 debug("Checking for the first MBR bootable partition\n");
216 for (int type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
217 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
218 if (err)
219 continue;
220 debug("Partition %d is of type %d and bootable=%d\n", type_part, info.sys_ind, info.bootable);
221 if (info.bootable != 0) {
222 debug("Partition %d is bootable, using it\n", type_part);
223 partition = type_part;
224 break;
225 }
226 }
227 printf("Using first bootable partition: %d\n", partition);
228 if (partition == CONFIG_SYS_MMCSD_FS_BOOT_PARTITION) {
229 return -ENOSYS;
230 }
231 }
232#endif
233
Tien Fong Chee6fd0a712019-01-23 14:20:03 +0800234#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200235 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100236 err = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400237 partition);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200238 if (!err)
239 return err;
240 }
241#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohárdda8f882022-01-14 14:31:38 +0100242 err = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400243 partition,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500244 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200245 if (!err)
246 return err;
247#endif
248#endif
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800249#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200250 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100251 err = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400252 partition);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200253 if (!err)
254 return err;
255 }
256#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohárdda8f882022-01-14 14:31:38 +0100257 err = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400258 partition,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500259 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200260 if (!err)
261 return err;
262#endif
263#endif
264
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800265#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200266 err = -ENOENT;
267#endif
268
269 return err;
270}
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200271#endif
272
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100273u32 __weak spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100274{
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800275#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100276 return MMCSD_MODE_FS;
277#elif defined(CONFIG_SUPPORT_EMMC_BOOT)
278 return MMCSD_MODE_EMMCBOOT;
279#else
280 return MMCSD_MODE_RAW;
281#endif
282}
283
Patrick Delaunay113aa502018-03-20 10:54:51 +0100284#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200285int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay113aa502018-03-20 10:54:51 +0100286{
287 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
288}
289#endif
290
Marek Vasutf9a921e2023-10-16 18:16:12 +0200291unsigned long __weak arch_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
292 unsigned long raw_sect)
293{
294 return raw_sect;
295}
296
297unsigned long __weak board_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
298 unsigned long raw_sect)
299{
300 return arch_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
301}
302
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530303unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
304 unsigned long raw_sect)
Peng Fan315cf592019-09-23 10:18:40 +0800305{
Marek Vasutf9a921e2023-10-16 18:16:12 +0200306 return board_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fan315cf592019-09-23 10:18:40 +0800307}
308
Marek Vasutde1ce232021-07-03 04:55:32 +0200309int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
310{
311 int part;
312#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
313 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
314#else
315 /*
316 * We need to check what the partition is configured to.
317 * 1 and 2 match up to boot0 / boot1 and 7 is user data
318 * which is the first physical partition (0).
319 */
Pali Roháreafdbe52023-03-11 11:44:27 +0100320 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
Marek Vasutde1ce232021-07-03 04:55:32 +0200321 if (part == 7)
322 part = 0;
323#endif
324 return part;
325}
326
327int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
328{
329 return default_spl_mmc_emmc_boot_partition(mmc);
330}
331
Harald Seileraf8b7b72022-07-11 14:35:32 +0200332static int spl_mmc_get_mmc_devnum(struct mmc *mmc)
333{
334 struct blk_desc *block_dev;
335#if !CONFIG_IS_ENABLED(BLK)
336 block_dev = &mmc->block_dev;
337#else
Sean Andersond2b254e2023-10-07 21:47:48 -0400338 block_dev = mmc_get_blk_desc(mmc);
Harald Seileraf8b7b72022-07-11 14:35:32 +0200339#endif
340 return block_dev->devnum;
341}
342
Sean Anderson13e6d912023-10-14 16:47:54 -0400343static struct mmc *mmc;
344
345void spl_mmc_clear_cache(void)
346{
347 mmc = NULL;
348}
349
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500350int spl_mmc_load(struct spl_image_info *spl_image,
351 struct spl_boot_device *bootdev,
352 const char *filename,
353 int raw_part,
354 unsigned long raw_sect)
Simon Schwarze21d2d82011-09-14 15:33:34 -0400355{
Simon Schwarze21d2d82011-09-14 15:33:34 -0400356 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600357 int err = 0;
Baruch Siachfef81be2020-01-15 09:08:08 +0200358 __maybe_unused int part = 0;
Harald Seileraf8b7b72022-07-11 14:35:32 +0200359 int mmc_dev;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400360
Harald Seileraf8b7b72022-07-11 14:35:32 +0200361 /* Perform peripheral init only once for an mmc device */
362 mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
363 if (!mmc || spl_mmc_get_mmc_devnum(mmc) != mmc_dev) {
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500364 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
365 if (err)
366 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400367
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500368 err = mmc_init(mmc);
369 if (err) {
370 mmc = NULL;
Paul Burtona8015fd2013-09-04 16:12:24 +0100371#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500372 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100373#endif
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500374 return err;
375 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400376 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000377
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100378 boot_mode = spl_mmc_boot_mode(mmc, bootdev->boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200379 err = -EINVAL;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200380 switch (boot_mode) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200381 case MMCSD_MODE_EMMCBOOT:
Marek Vasutde1ce232021-07-03 04:55:32 +0200382 part = spl_mmc_emmc_boot_partition(mmc);
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200383
Mans Rullgardec046a52019-09-03 16:17:17 +0200384 if (CONFIG_IS_ENABLED(MMC_TINY))
385 err = mmc_switch_part(mmc, part);
386 else
387 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutf537e392016-12-01 02:06:33 +0100388
Mans Rullgardec046a52019-09-03 16:17:17 +0200389 if (err) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200390#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgardec046a52019-09-03 16:17:17 +0200391 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200392#endif
Mans Rullgardec046a52019-09-03 16:17:17 +0200393 return err;
394 }
395 /* Fall through */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200396 case MMCSD_MODE_RAW:
397 debug("spl: mmc boot mode: raw\n");
398
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200399 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100400 err = mmc_load_image_raw_os(spl_image, bootdev, mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200401 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200402 return err;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200403 }
Peng Fandc7d3222019-10-23 01:43:30 +0000404
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530405 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fandc7d3222019-10-23 01:43:30 +0000406
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800407#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Pali Rohárdda8f882022-01-14 14:31:38 +0100408 err = mmc_load_image_raw_partition(spl_image, bootdev,
409 mmc, raw_part,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500410 raw_sect);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200411 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200412 return err;
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800413#endif
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200414#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohárdda8f882022-01-14 14:31:38 +0100415 err = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Baruch Siachfef81be2020-01-15 09:08:08 +0200416 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200417 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200418 return err;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200419#endif
Guillaume GARDET834dd882016-02-18 18:17:36 +0100420 /* If RAW mode fails, try FS mode. */
Pali Rohára0811552023-01-21 15:13:08 +0100421#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200422 case MMCSD_MODE_FS:
423 debug("spl: mmc boot mode: fs\n");
424
Pali Rohárdda8f882022-01-14 14:31:38 +0100425 err = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200426 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200427 return err;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200428
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200429 break;
Pali Rohára0811552023-01-21 15:13:08 +0100430#endif
Paul Burtona8015fd2013-09-04 16:12:24 +0100431#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200432 default:
433 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100434#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200435 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200436
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200437 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400438}
Simon Glassd449cb82016-09-24 18:20:00 -0600439
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500440int spl_mmc_load_image(struct spl_image_info *spl_image,
441 struct spl_boot_device *bootdev)
442{
443 return spl_mmc_load(spl_image, bootdev,
444#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
445 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
446#else
447 NULL,
448#endif
449#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200450 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500451#else
452 0,
453#endif
454#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
455 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
456#else
457 0);
458#endif
459}
460
Simon Glass4fc1f252016-11-30 15:30:50 -0700461SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
462SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
463SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);