blob: 3d032bb27ce3ff89dff674498eaa48098cf6d9cf [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 */
8#include <common.h>
Simon Glassf22851a2015-06-23 15:38:51 -06009#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass655306c2020-05-10 11:39:58 -060011#include <part.h>
Tom Rini28591df2012-08-13 12:03:19 -070012#include <spl.h>
Sean Anderson29e338f2023-11-08 11:48:50 -050013#include <spl_load.h>
Paul Kocialkowski0d139722015-04-27 10:20:22 +020014#include <linux/compiler.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020015#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040016#include <asm/u-boot.h>
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020017#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040018#include <mmc.h>
Tom Rini1530e8a2013-06-28 14:43:01 -040019#include <image.h>
Sean Anderson952ed672023-10-14 16:47:44 -040020#include <imx_container.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040021
Sean Anderson7d8d6132023-11-08 11:48:40 -050022static ulong h_spl_load_read(struct spl_load_info *load, ulong off,
23 ulong size, void *buf)
Simon Glassb52c7fc2016-02-22 22:55:58 -070024{
Sean Anderson7d8d6132023-11-08 11:48:40 -050025 struct blk_desc *bd = load->priv;
26 lbaint_t sector = off >> bd->log2blksz;
27 lbaint_t count = size >> bd->log2blksz;
Simon Glassb52c7fc2016-02-22 22:55:58 -070028
Sean Anderson7d8d6132023-11-08 11:48:40 -050029 return blk_dread(bd, sector, count, buf) << bd->log2blksz;
Simon Glassb52c7fc2016-02-22 22:55:58 -070030}
Simon Glassb52c7fc2016-02-22 22:55:58 -070031
Baruch Siachfef81be2020-01-15 09:08:08 +020032static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
33{
34#if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
35 if (part == 0)
36 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
37#endif
38
39 return 0;
40}
41
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090042static __maybe_unused
43int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010044 struct spl_boot_device *bootdev,
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090045 struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040046{
Sean Anderson29e338f2023-11-08 11:48:50 -050047 int ret;
Marek Vasut1dda46f2018-08-14 11:27:02 +020048 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Sean Anderson29e338f2023-11-08 11:48:50 -050049 struct spl_load_info load;
Tom Rini1530e8a2013-06-28 14:43:01 -040050
Sean Anderson29e338f2023-11-08 11:48:50 -050051 load.priv = bd;
52 spl_set_bl_len(&load, bd->blksz);
53 load.read = h_spl_load_read;
54 ret = spl_load(spl_image, bootdev, &load, 0, sector << bd->log2blksz);
Simon Glassb52c7fc2016-02-22 22:55:58 -070055 if (ret) {
Paul Burtona8015fd2013-09-04 16:12:24 +010056#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -030057 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010058#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020059 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020060 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020061
62 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040063}
64
Masahiro Yamada2a4a20c2017-06-22 16:52:36 +090065static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +020066{
67 switch (boot_device) {
68 case BOOT_DEVICE_MMC1:
69 return 0;
70 case BOOT_DEVICE_MMC2:
71 case BOOT_DEVICE_MMC2_2:
72 return 1;
73 }
74
75#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
76 printf("spl: unsupported mmc boot device.\n");
77#endif
78
79 return -ENODEV;
80}
81
Simon Glass4faf63b2015-12-02 10:59:12 -070082static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020083{
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +020084 int err, mmc_dev;
85
86 mmc_dev = spl_mmc_get_device_index(boot_device);
87 if (mmc_dev < 0)
88 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020089
Lokesh Vutlac59b41c2019-09-09 14:40:36 +053090#if CONFIG_IS_ENABLED(DM_MMC)
91 err = mmc_init_device(mmc_dev);
92#else
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020093 err = mmc_initialize(NULL);
Lokesh Vutlac59b41c2019-09-09 14:40:36 +053094#endif /* DM_MMC */
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020095 if (err) {
96#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
97 printf("spl: could not initialize mmc. error: %d\n", err);
98#endif
99 return err;
100 }
Simon Glass51ace322015-12-02 10:59:13 -0700101 *mmcp = find_mmc_device(mmc_dev);
102 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200103 if (err) {
104#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanf5920d02018-09-18 08:11:55 +0000105 printf("spl: could not find mmc device %d. error: %d\n",
106 mmc_dev, err);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200107#endif
108 return err;
109 }
110
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200111 return 0;
112}
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200113
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800114#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glassee306792016-09-24 18:20:13 -0600115static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100116 struct spl_boot_device *bootdev,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500117 struct mmc *mmc, int partition,
118 unsigned long sector)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100119{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600120 struct disk_partition info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200121 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100122
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800123#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
124 int type_part;
125 /* Only support MBR so DOS_ENTRY_NUMBERS */
126 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
127 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
128 if (err)
129 continue;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200130 if (info.sys_ind ==
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800131 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
132 partition = type_part;
133 break;
134 }
135 }
136#endif
137
Simon Glass55877f92016-07-05 17:10:18 -0600138 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200139 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100140#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200141 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100142#endif
143 return -1;
144 }
145
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200146#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohárdda8f882022-01-14 14:31:38 +0100147 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200148#else
Pali Rohárdda8f882022-01-14 14:31:38 +0100149 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200150#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100151}
152#endif
153
Alexandru Gagniuc99426c82021-12-30 10:39:59 -0600154#if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
Simon Glassee306792016-09-24 18:20:13 -0600155static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100156 struct spl_boot_device *bootdev,
Simon Glassee306792016-09-24 18:20:13 -0600157 struct mmc *mmc)
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000158{
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530159 int ret;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200160
Elena Popa6532fab2023-08-08 16:42:15 +0300161#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
York Suncea88212018-06-26 09:44:39 -0700162 unsigned long count;
163
Simon Glass70061ba2016-06-12 23:30:32 -0600164 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200165 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
166 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
Simon Glass259cdb42023-09-26 08:14:17 -0600167 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
Tom Rini195bc4b2017-08-14 21:01:30 -0400168 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100169#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300170 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100171#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000172 return -1;
173 }
York Suncea88212018-06-26 09:44:39 -0700174#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000175
Pali Rohárdda8f882022-01-14 14:31:38 +0100176 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200177 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530178 if (ret)
179 return ret;
180
Alexandru Gagniuc36fd3ba2021-07-15 14:19:23 -0500181 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
Michal Simek50fa1182023-05-17 09:17:16 +0200182 puts("Expected image is not found. Trying to start U-Boot\n");
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530183 return -ENOENT;
184 }
185
186 return 0;
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000187}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200188#else
Simon Glassee306792016-09-24 18:20:13 -0600189static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100190 struct spl_boot_device *bootdev,
Simon Glassee306792016-09-24 18:20:13 -0600191 struct mmc *mmc)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200192{
193 return -ENOSYS;
194}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000195#endif
196
Alexandru Gagniuc99426c82021-12-30 10:39:59 -0600197#ifndef CONFIG_SPL_OS_BOOT
198int spl_start_uboot(void)
199{
200 return 1;
201}
202#endif
203
Pali Rohára0811552023-01-21 15:13:08 +0100204#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Pali Rohárdda8f882022-01-14 14:31:38 +0100205static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
206 struct spl_boot_device *bootdev,
207 struct mmc *mmc,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500208 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200209{
210 int err = -ENOSYS;
211
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400212 __maybe_unused int partition = CONFIG_SYS_MMCSD_FS_BOOT_PARTITION;
213
214#if CONFIG_SYS_MMCSD_FS_BOOT_PARTITION == -1
215 {
216 struct disk_partition info;
217 debug("Checking for the first MBR bootable partition\n");
218 for (int type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
219 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
220 if (err)
221 continue;
222 debug("Partition %d is of type %d and bootable=%d\n", type_part, info.sys_ind, info.bootable);
223 if (info.bootable != 0) {
224 debug("Partition %d is bootable, using it\n", type_part);
225 partition = type_part;
226 break;
227 }
228 }
229 printf("Using first bootable partition: %d\n", partition);
230 if (partition == CONFIG_SYS_MMCSD_FS_BOOT_PARTITION) {
231 return -ENOSYS;
232 }
233 }
234#endif
235
Tien Fong Chee6fd0a712019-01-23 14:20:03 +0800236#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200237 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100238 err = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400239 partition);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200240 if (!err)
241 return err;
242 }
243#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohárdda8f882022-01-14 14:31:38 +0100244 err = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400245 partition,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500246 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200247 if (!err)
248 return err;
249#endif
250#endif
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800251#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200252 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100253 err = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400254 partition);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200255 if (!err)
256 return err;
257 }
258#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohárdda8f882022-01-14 14:31:38 +0100259 err = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400260 partition,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500261 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200262 if (!err)
263 return err;
264#endif
265#endif
266
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800267#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200268 err = -ENOENT;
269#endif
270
271 return err;
272}
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200273#endif
274
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100275u32 __weak spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100276{
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800277#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100278 return MMCSD_MODE_FS;
279#elif defined(CONFIG_SUPPORT_EMMC_BOOT)
280 return MMCSD_MODE_EMMCBOOT;
281#else
282 return MMCSD_MODE_RAW;
283#endif
284}
285
Patrick Delaunay113aa502018-03-20 10:54:51 +0100286#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200287int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay113aa502018-03-20 10:54:51 +0100288{
289 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
290}
291#endif
292
Marek Vasutf9a921e2023-10-16 18:16:12 +0200293unsigned long __weak arch_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
294 unsigned long raw_sect)
295{
296 return raw_sect;
297}
298
299unsigned long __weak board_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
300 unsigned long raw_sect)
301{
302 return arch_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
303}
304
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530305unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
306 unsigned long raw_sect)
Peng Fan315cf592019-09-23 10:18:40 +0800307{
Marek Vasutf9a921e2023-10-16 18:16:12 +0200308 return board_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fan315cf592019-09-23 10:18:40 +0800309}
310
Marek Vasutde1ce232021-07-03 04:55:32 +0200311int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
312{
313 int part;
314#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
315 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
316#else
317 /*
318 * We need to check what the partition is configured to.
319 * 1 and 2 match up to boot0 / boot1 and 7 is user data
320 * which is the first physical partition (0).
321 */
Pali Roháreafdbe52023-03-11 11:44:27 +0100322 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
Marek Vasutde1ce232021-07-03 04:55:32 +0200323 if (part == 7)
324 part = 0;
325#endif
326 return part;
327}
328
329int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
330{
331 return default_spl_mmc_emmc_boot_partition(mmc);
332}
333
Harald Seileraf8b7b72022-07-11 14:35:32 +0200334static int spl_mmc_get_mmc_devnum(struct mmc *mmc)
335{
336 struct blk_desc *block_dev;
337#if !CONFIG_IS_ENABLED(BLK)
338 block_dev = &mmc->block_dev;
339#else
Sean Andersond2b254e2023-10-07 21:47:48 -0400340 block_dev = mmc_get_blk_desc(mmc);
Harald Seileraf8b7b72022-07-11 14:35:32 +0200341#endif
342 return block_dev->devnum;
343}
344
Sean Anderson13e6d912023-10-14 16:47:54 -0400345static struct mmc *mmc;
346
347void spl_mmc_clear_cache(void)
348{
349 mmc = NULL;
350}
351
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500352int spl_mmc_load(struct spl_image_info *spl_image,
353 struct spl_boot_device *bootdev,
354 const char *filename,
355 int raw_part,
356 unsigned long raw_sect)
Simon Schwarze21d2d82011-09-14 15:33:34 -0400357{
Simon Schwarze21d2d82011-09-14 15:33:34 -0400358 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600359 int err = 0;
Baruch Siachfef81be2020-01-15 09:08:08 +0200360 __maybe_unused int part = 0;
Harald Seileraf8b7b72022-07-11 14:35:32 +0200361 int mmc_dev;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400362
Harald Seileraf8b7b72022-07-11 14:35:32 +0200363 /* Perform peripheral init only once for an mmc device */
364 mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
365 if (!mmc || spl_mmc_get_mmc_devnum(mmc) != mmc_dev) {
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500366 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
367 if (err)
368 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400369
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500370 err = mmc_init(mmc);
371 if (err) {
372 mmc = NULL;
Paul Burtona8015fd2013-09-04 16:12:24 +0100373#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500374 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100375#endif
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500376 return err;
377 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400378 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000379
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100380 boot_mode = spl_mmc_boot_mode(mmc, bootdev->boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200381 err = -EINVAL;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200382 switch (boot_mode) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200383 case MMCSD_MODE_EMMCBOOT:
Marek Vasutde1ce232021-07-03 04:55:32 +0200384 part = spl_mmc_emmc_boot_partition(mmc);
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200385
Mans Rullgardec046a52019-09-03 16:17:17 +0200386 if (CONFIG_IS_ENABLED(MMC_TINY))
387 err = mmc_switch_part(mmc, part);
388 else
389 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutf537e392016-12-01 02:06:33 +0100390
Mans Rullgardec046a52019-09-03 16:17:17 +0200391 if (err) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200392#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgardec046a52019-09-03 16:17:17 +0200393 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200394#endif
Mans Rullgardec046a52019-09-03 16:17:17 +0200395 return err;
396 }
397 /* Fall through */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200398 case MMCSD_MODE_RAW:
399 debug("spl: mmc boot mode: raw\n");
400
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200401 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100402 err = mmc_load_image_raw_os(spl_image, bootdev, mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200403 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200404 return err;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200405 }
Peng Fandc7d3222019-10-23 01:43:30 +0000406
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530407 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fandc7d3222019-10-23 01:43:30 +0000408
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800409#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Pali Rohárdda8f882022-01-14 14:31:38 +0100410 err = mmc_load_image_raw_partition(spl_image, bootdev,
411 mmc, raw_part,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500412 raw_sect);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200413 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200414 return err;
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800415#endif
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200416#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohárdda8f882022-01-14 14:31:38 +0100417 err = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Baruch Siachfef81be2020-01-15 09:08:08 +0200418 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200419 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200420 return err;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200421#endif
Guillaume GARDET834dd882016-02-18 18:17:36 +0100422 /* If RAW mode fails, try FS mode. */
Pali Rohára0811552023-01-21 15:13:08 +0100423#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200424 case MMCSD_MODE_FS:
425 debug("spl: mmc boot mode: fs\n");
426
Pali Rohárdda8f882022-01-14 14:31:38 +0100427 err = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200428 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200429 return err;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200430
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200431 break;
Pali Rohára0811552023-01-21 15:13:08 +0100432#endif
Paul Burtona8015fd2013-09-04 16:12:24 +0100433#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200434 default:
435 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100436#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200437 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200438
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200439 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400440}
Simon Glassd449cb82016-09-24 18:20:00 -0600441
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500442int spl_mmc_load_image(struct spl_image_info *spl_image,
443 struct spl_boot_device *bootdev)
444{
445 return spl_mmc_load(spl_image, bootdev,
446#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
447 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
448#else
449 NULL,
450#endif
451#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200452 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500453#else
454 0,
455#endif
456#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
457 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
458#else
459 0);
460#endif
461}
462
Simon Glass4fc1f252016-11-30 15:30:50 -0700463SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
464SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
465SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);