blob: 3d032bb27ce3ff89dff674498eaa48098cf6d9cf [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04002/*
3 * (C) Copyright 2010
4 * Texas Instruments, <www.ti.com>
5 *
6 * Aneesh V <aneesh@ti.com>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04007 */
8#include <common.h>
Simon Glassdc3dedf2015-06-23 15:38:51 -06009#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060011#include <part.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070012#include <spl.h>
Sean Anderson5d3401a2023-11-08 11:48:50 -050013#include <spl_load.h>
Paul Kocialkowski91199f42015-04-27 10:20:22 +020014#include <linux/compiler.h>
Nikita Kiryanov36afd452015-11-08 17:11:49 +020015#include <errno.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040016#include <asm/u-boot.h>
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020017#include <errno.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040018#include <mmc.h>
Tom Rinie4c444b2013-06-28 14:43:01 -040019#include <image.h>
Sean Andersonab121792023-10-14 16:47:44 -040020#include <imx_container.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040021
Sean Anderson73c40fc2023-11-08 11:48:40 -050022static ulong h_spl_load_read(struct spl_load_info *load, ulong off,
23 ulong size, void *buf)
Simon Glass96debd12016-02-22 22:55:58 -070024{
Sean Anderson73c40fc2023-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 Glass96debd12016-02-22 22:55:58 -070028
Sean Anderson73c40fc2023-11-08 11:48:40 -050029 return blk_dread(bd, sector, count, buf) << bd->log2blksz;
Simon Glass96debd12016-02-22 22:55:58 -070030}
Simon Glass96debd12016-02-22 22:55:58 -070031
Baruch Siache42afd82020-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 Kimb016b582017-09-19 13:29:45 +090042static __maybe_unused
43int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010044 struct spl_boot_device *bootdev,
Seung-Woo Kimb016b582017-09-19 13:29:45 +090045 struct mmc *mmc, unsigned long sector)
Simon Glass96debd12016-02-22 22:55:58 -070046{
Sean Anderson5d3401a2023-11-08 11:48:50 -050047 int ret;
Marek Vasut04ce5422018-08-14 11:27:02 +020048 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Sean Anderson5d3401a2023-11-08 11:48:50 -050049 struct spl_load_info load;
Simon Glass96debd12016-02-22 22:55:58 -070050
Sean Anderson5d3401a2023-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 Glass96debd12016-02-22 22:55:58 -070055 if (ret) {
Paul Burton8112f5f2013-09-04 16:12:24 +010056#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam5bbf4092016-04-20 23:12:04 -030057 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010058#endif
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020059 return -1;
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020060 }
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020061
62 return 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040063}
64
Masahiro Yamada207d8b32017-06-22 16:52:36 +090065static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanova1e56cf2015-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 Glass99c7a512015-12-02 10:59:12 -070082static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020083{
Nikita Kiryanova1e56cf2015-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 Kiryanov4188ba32015-11-08 17:11:44 +020089
Lokesh Vutla80f02012019-09-09 14:40:36 +053090#if CONFIG_IS_ENABLED(DM_MMC)
91 err = mmc_init_device(mmc_dev);
92#else
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020093 err = mmc_initialize(NULL);
Lokesh Vutla80f02012019-09-09 14:40:36 +053094#endif /* DM_MMC */
Nikita Kiryanov4188ba32015-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 Glassb4857aa2015-12-02 10:59:13 -0700101 *mmcp = find_mmc_device(mmc_dev);
102 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200103 if (err) {
104#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanaa608b62018-09-18 08:11:55 +0000105 printf("spl: could not find mmc device %d. error: %d\n",
106 mmc_dev, err);
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200107#endif
108 return err;
109 }
110
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200111 return 0;
112}
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200113
Dalon Westergreen949123e2017-02-10 17:15:35 -0800114#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600115static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100116 struct spl_boot_device *bootdev,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500117 struct mmc *mmc, int partition,
118 unsigned long sector)
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100119{
Simon Glass05289792020-05-10 11:39:57 -0600120 struct disk_partition info;
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200121 int err;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100122
Dalon Westergreenf0fb4fa2017-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 Denk0a50b3c2021-09-27 17:42:38 +0200130 if (info.sys_ind ==
Dalon Westergreenf0fb4fa2017-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 Glass1e2b3ef2016-07-05 17:10:18 -0600138 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200139 if (err) {
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100140#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200141 puts("spl: partition error\n");
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100142#endif
143 return -1;
144 }
145
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200146#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohár2e0429b2022-01-14 14:31:38 +0100147 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200148#else
Pali Rohár2e0429b2022-01-14 14:31:38 +0100149 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200150#endif
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100151}
152#endif
153
Alexandru Gagniuc22eb1522021-12-30 10:39:59 -0600154#if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600155static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100156 struct spl_boot_device *bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600157 struct mmc *mmc)
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000158{
Lokesh Vutla811906a2016-04-11 15:24:50 +0530159 int ret;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200160
Elena Popa86b1aad2023-08-08 16:42:15 +0300161#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
York Sun7267fbf2018-06-26 09:44:39 -0700162 unsigned long count;
163
Simon Glass87bce4e2016-06-12 23:30:32 -0600164 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200165 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
166 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
Simon Glass9cbdc3a2023-09-26 08:14:17 -0600167 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
Tom Rini9585dd32017-08-14 21:01:30 -0400168 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100169#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam5bbf4092016-04-20 23:12:04 -0300170 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100171#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000172 return -1;
173 }
York Sun7267fbf2018-06-26 09:44:39 -0700174#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000175
Pali Rohár2e0429b2022-01-14 14:31:38 +0100176 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200177 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla811906a2016-04-11 15:24:50 +0530178 if (ret)
179 return ret;
180
Alexandru Gagniuc5f6e5c32021-07-15 14:19:23 -0500181 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
Michal Simek1be82af2023-05-17 09:17:16 +0200182 puts("Expected image is not found. Trying to start U-Boot\n");
Lokesh Vutla811906a2016-04-11 15:24:50 +0530183 return -ENOENT;
184 }
185
186 return 0;
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000187}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200188#else
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600189static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100190 struct spl_boot_device *bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600191 struct mmc *mmc)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200192{
193 return -ENOSYS;
194}
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000195#endif
196
Alexandru Gagniuc22eb1522021-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ár718d1c72023-01-21 15:13:08 +0100204#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Pali Rohár2e0429b2022-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 Dannenberge1eb6ad2019-06-04 17:55:46 -0500208 const char *filename)
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200209{
210 int err = -ENOSYS;
211
Jérôme Carretero5fa973e2022-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 Chee0c3a9ed2019-01-23 14:20:03 +0800236#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200237 if (!spl_start_uboot()) {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100238 err = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400239 partition);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200240 if (!err)
241 return err;
242 }
243#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohár2e0429b2022-01-14 14:31:38 +0100244 err = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400245 partition,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500246 filename);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200247 if (!err)
248 return err;
249#endif
250#endif
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800251#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200252 if (!spl_start_uboot()) {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100253 err = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400254 partition);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200255 if (!err)
256 return err;
257 }
258#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohár2e0429b2022-01-14 14:31:38 +0100259 err = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400260 partition,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500261 filename);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200262 if (!err)
263 return err;
264#endif
265#endif
266
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800267#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200268 err = -ENOENT;
269#endif
270
271 return err;
272}
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200273#endif
274
Andre Przywara59073572021-07-12 11:06:49 +0100275u32 __weak spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Lukasz Majewskid695d662018-02-03 08:29:52 +0100276{
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800277#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewskid695d662018-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 Delaunay35a66962018-03-20 10:54:51 +0100286#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +0200287int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay35a66962018-03-20 10:54:51 +0100288{
289 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
290}
291#endif
292
Marek Vasute936db92023-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 Abbascf008252020-02-26 13:44:35 +0530305unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
306 unsigned long raw_sect)
Peng Fan30ecc9c2019-09-23 10:18:40 +0800307{
Marek Vasute936db92023-10-16 18:16:12 +0200308 return board_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fan30ecc9c2019-09-23 10:18:40 +0800309}
310
Marek Vasut9b191592021-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ár8b882062023-03-11 11:44:27 +0100322 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
Marek Vasut9b191592021-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 Seilerbf28d9a2022-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 Andersona0733b32023-10-07 21:47:48 -0400340 block_dev = mmc_get_blk_desc(mmc);
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200341#endif
342 return block_dev->devnum;
343}
344
Sean Anderson93caa3e2023-10-14 16:47:54 -0400345static struct mmc *mmc;
346
347void spl_mmc_clear_cache(void)
348{
349 mmc = NULL;
350}
351
Andreas Dannenberge1eb6ad2019-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 Schwarz9ea5c6e2011-09-14 15:33:34 -0400357{
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400358 u32 boot_mode;
Simon Glassdc3dedf2015-06-23 15:38:51 -0600359 int err = 0;
Baruch Siache42afd82020-01-15 09:08:08 +0200360 __maybe_unused int part = 0;
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200361 int mmc_dev;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400362
Harald Seilerbf28d9a2022-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 Dannenberge1eb6ad2019-06-04 17:55:46 -0500366 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
367 if (err)
368 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400369
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500370 err = mmc_init(mmc);
371 if (err) {
372 mmc = NULL;
Paul Burton8112f5f2013-09-04 16:12:24 +0100373#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500374 printf("spl: mmc init failed with error: %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100375#endif
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500376 return err;
377 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400378 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000379
Andre Przywara59073572021-07-12 11:06:49 +0100380 boot_mode = spl_mmc_boot_mode(mmc, bootdev->boot_device);
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200381 err = -EINVAL;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200382 switch (boot_mode) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200383 case MMCSD_MODE_EMMCBOOT:
Marek Vasut9b191592021-07-03 04:55:32 +0200384 part = spl_mmc_emmc_boot_partition(mmc);
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200385
Mans Rullgard92439902019-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 Vasutb5b838f2016-12-01 02:06:33 +0100390
Mans Rullgard92439902019-09-03 16:17:17 +0200391 if (err) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200392#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgard92439902019-09-03 16:17:17 +0200393 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200394#endif
Mans Rullgard92439902019-09-03 16:17:17 +0200395 return err;
396 }
397 /* Fall through */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200398 case MMCSD_MODE_RAW:
399 debug("spl: mmc boot mode: raw\n");
400
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200401 if (!spl_start_uboot()) {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100402 err = mmc_load_image_raw_os(spl_image, bootdev, mmc);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200403 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200404 return err;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200405 }
Peng Fana335f802019-10-23 01:43:30 +0000406
Faiz Abbascf008252020-02-26 13:44:35 +0530407 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fana335f802019-10-23 01:43:30 +0000408
Dalon Westergreen949123e2017-02-10 17:15:35 -0800409#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Pali Rohár2e0429b2022-01-14 14:31:38 +0100410 err = mmc_load_image_raw_partition(spl_image, bootdev,
411 mmc, raw_part,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500412 raw_sect);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200413 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200414 return err;
Dalon Westergreen949123e2017-02-10 17:15:35 -0800415#endif
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200416#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohár2e0429b2022-01-14 14:31:38 +0100417 err = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Baruch Siache42afd82020-01-15 09:08:08 +0200418 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200419 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200420 return err;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200421#endif
Guillaume GARDET86a0df72016-02-18 18:17:36 +0100422 /* If RAW mode fails, try FS mode. */
Pali Rohár718d1c72023-01-21 15:13:08 +0100423#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200424 case MMCSD_MODE_FS:
425 debug("spl: mmc boot mode: fs\n");
426
Pali Rohár2e0429b2022-01-14 14:31:38 +0100427 err = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200428 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200429 return err;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200430
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200431 break;
Pali Rohár718d1c72023-01-21 15:13:08 +0100432#endif
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100433#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200434 default:
435 puts("spl: mmc: wrong boot mode\n");
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100436#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200437 }
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200438
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200439 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400440}
Simon Glass0fed9c72016-09-24 18:20:00 -0600441
Andreas Dannenberge1eb6ad2019-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 Seilerc51b7512020-04-15 11:33:31 +0200452 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberge1eb6ad2019-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 Glassebc4ef62016-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);