blob: 887ea760b84fb7be752896f2ebbd3d394862fb03 [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 */
Simon Glassdc3dedf2015-06-23 15:38:51 -06008#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060010#include <part.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070011#include <spl.h>
Sean Anderson5d3401a2023-11-08 11:48:50 -050012#include <spl_load.h>
Paul Kocialkowski91199f42015-04-27 10:20:22 +020013#include <linux/compiler.h>
Nikita Kiryanov36afd452015-11-08 17:11:49 +020014#include <errno.h>
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020015#include <errno.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040016#include <mmc.h>
Tom Rinie4c444b2013-06-28 14:43:01 -040017#include <image.h>
Sean Andersonab121792023-10-14 16:47:44 -040018#include <imx_container.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040019
Sean Anderson73c40fc2023-11-08 11:48:40 -050020static ulong h_spl_load_read(struct spl_load_info *load, ulong off,
21 ulong size, void *buf)
Simon Glass96debd12016-02-22 22:55:58 -070022{
Sean Anderson73c40fc2023-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 Glass96debd12016-02-22 22:55:58 -070026
Sean Anderson73c40fc2023-11-08 11:48:40 -050027 return blk_dread(bd, sector, count, buf) << bd->log2blksz;
Simon Glass96debd12016-02-22 22:55:58 -070028}
Simon Glass96debd12016-02-22 22:55:58 -070029
Baruch Siache42afd82020-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 Kimb016b582017-09-19 13:29:45 +090040static __maybe_unused
41int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010042 struct spl_boot_device *bootdev,
Seung-Woo Kimb016b582017-09-19 13:29:45 +090043 struct mmc *mmc, unsigned long sector)
Simon Glass96debd12016-02-22 22:55:58 -070044{
Sean Anderson5d3401a2023-11-08 11:48:50 -050045 int ret;
Marek Vasut04ce5422018-08-14 11:27:02 +020046 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Sean Anderson5d3401a2023-11-08 11:48:50 -050047 struct spl_load_info load;
Simon Glass96debd12016-02-22 22:55:58 -070048
Sean Anderson5d3401a2023-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 Glass96debd12016-02-22 22:55:58 -070053 if (ret) {
Fabio Estevam5bbf4092016-04-20 23:12:04 -030054 puts("mmc_load_image_raw_sector: mmc block read error\n");
Simon Glass8a369de2024-08-22 07:54:58 -060055 return ret;
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020056 }
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020057
58 return 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040059}
60
Simon Glassc5cb9e52024-08-22 07:54:59 -060061static int spl_mmc_get_device_index(uint boot_device)
Nikita Kiryanova1e56cf2015-11-08 17:11:54 +020062{
63 switch (boot_device) {
64 case BOOT_DEVICE_MMC1:
65 return 0;
66 case BOOT_DEVICE_MMC2:
67 case BOOT_DEVICE_MMC2_2:
68 return 1;
69 }
70
Nikita Kiryanova1e56cf2015-11-08 17:11:54 +020071 printf("spl: unsupported mmc boot device.\n");
Nikita Kiryanova1e56cf2015-11-08 17:11:54 +020072
73 return -ENODEV;
74}
75
Simon Glassc5cb9e52024-08-22 07:54:59 -060076static int spl_mmc_find_device(struct mmc **mmcp, int mmc_dev)
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020077{
Simon Glassc5cb9e52024-08-22 07:54:59 -060078 int ret;
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020079
Lokesh Vutla80f02012019-09-09 14:40:36 +053080#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass8a369de2024-08-22 07:54:58 -060081 ret = mmc_init_device(mmc_dev);
Lokesh Vutla80f02012019-09-09 14:40:36 +053082#else
Simon Glass8a369de2024-08-22 07:54:58 -060083 ret = mmc_initialize(NULL);
Lokesh Vutla80f02012019-09-09 14:40:36 +053084#endif /* DM_MMC */
Simon Glass8a369de2024-08-22 07:54:58 -060085 if (ret) {
86 printf("spl: could not initialize mmc. error: %d\n", ret);
87 return ret;
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020088 }
Simon Glassb4857aa2015-12-02 10:59:13 -070089 *mmcp = find_mmc_device(mmc_dev);
Simon Glass8a369de2024-08-22 07:54:58 -060090 ret = *mmcp ? 0 : -ENODEV;
91 if (ret) {
Alex Kiernanaa608b62018-09-18 08:11:55 +000092 printf("spl: could not find mmc device %d. error: %d\n",
Simon Glass8a369de2024-08-22 07:54:58 -060093 mmc_dev, ret);
94 return ret;
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020095 }
96
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020097 return 0;
98}
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020099
Dalon Westergreen949123e2017-02-10 17:15:35 -0800100#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600101static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100102 struct spl_boot_device *bootdev,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500103 struct mmc *mmc, int partition,
104 unsigned long sector)
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100105{
Simon Glass05289792020-05-10 11:39:57 -0600106 struct disk_partition info;
Simon Glass8a369de2024-08-22 07:54:58 -0600107 int ret;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100108
Dalon Westergreenf0fb4fa2017-02-10 17:15:34 -0800109#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
110 int type_part;
111 /* Only support MBR so DOS_ENTRY_NUMBERS */
112 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
Simon Glass8a369de2024-08-22 07:54:58 -0600113 ret = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
114 if (ret)
Dalon Westergreenf0fb4fa2017-02-10 17:15:34 -0800115 continue;
Wolfgang Denk0a50b3c2021-09-27 17:42:38 +0200116 if (info.sys_ind ==
Dalon Westergreenf0fb4fa2017-02-10 17:15:34 -0800117 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
118 partition = type_part;
119 break;
120 }
121 }
122#endif
123
Simon Glass8a369de2024-08-22 07:54:58 -0600124 ret = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
125 if (ret) {
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200126 puts("spl: partition error\n");
Simon Glass8a369de2024-08-22 07:54:58 -0600127 return ret;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100128 }
129
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200130#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohár2e0429b2022-01-14 14:31:38 +0100131 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200132#else
Pali Rohár2e0429b2022-01-14 14:31:38 +0100133 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200134#endif
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100135}
136#endif
137
Alexandru Gagniuc22eb1522021-12-30 10:39:59 -0600138#if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600139static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100140 struct spl_boot_device *bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600141 struct mmc *mmc)
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000142{
Lokesh Vutla811906a2016-04-11 15:24:50 +0530143 int ret;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200144
Elena Popa86b1aad2023-08-08 16:42:15 +0300145#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
York Sun7267fbf2018-06-26 09:44:39 -0700146 unsigned long count;
147
Simon Glass87bce4e2016-06-12 23:30:32 -0600148 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200149 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
150 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
Simon Glass9cbdc3a2023-09-26 08:14:17 -0600151 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
Tom Rini9585dd32017-08-14 21:01:30 -0400152 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Fabio Estevam5bbf4092016-04-20 23:12:04 -0300153 puts("mmc_load_image_raw_os: mmc block read error\n");
Simon Glass8a369de2024-08-22 07:54:58 -0600154 return -EIO;
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000155 }
York Sun7267fbf2018-06-26 09:44:39 -0700156#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000157
Pali Rohár2e0429b2022-01-14 14:31:38 +0100158 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200159 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla811906a2016-04-11 15:24:50 +0530160 if (ret)
161 return ret;
162
Alexandru Gagniuc5f6e5c32021-07-15 14:19:23 -0500163 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
Michal Simek1be82af2023-05-17 09:17:16 +0200164 puts("Expected image is not found. Trying to start U-Boot\n");
Lokesh Vutla811906a2016-04-11 15:24:50 +0530165 return -ENOENT;
166 }
167
168 return 0;
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000169}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200170#else
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600171static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100172 struct spl_boot_device *bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600173 struct mmc *mmc)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200174{
175 return -ENOSYS;
176}
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000177#endif
178
Alexandru Gagniuc22eb1522021-12-30 10:39:59 -0600179#ifndef CONFIG_SPL_OS_BOOT
180int spl_start_uboot(void)
181{
182 return 1;
183}
184#endif
185
Pali Rohár718d1c72023-01-21 15:13:08 +0100186#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Pali Rohár2e0429b2022-01-14 14:31:38 +0100187static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
188 struct spl_boot_device *bootdev,
189 struct mmc *mmc,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500190 const char *filename)
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200191{
Simon Glass8a369de2024-08-22 07:54:58 -0600192 int ret = -ENOSYS;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200193
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400194 __maybe_unused int partition = CONFIG_SYS_MMCSD_FS_BOOT_PARTITION;
195
196#if CONFIG_SYS_MMCSD_FS_BOOT_PARTITION == -1
197 {
198 struct disk_partition info;
199 debug("Checking for the first MBR bootable partition\n");
200 for (int type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
Simon Glass8a369de2024-08-22 07:54:58 -0600201 ret = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
202 if (ret)
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400203 continue;
204 debug("Partition %d is of type %d and bootable=%d\n", type_part, info.sys_ind, info.bootable);
205 if (info.bootable != 0) {
206 debug("Partition %d is bootable, using it\n", type_part);
207 partition = type_part;
208 break;
209 }
210 }
211 printf("Using first bootable partition: %d\n", partition);
212 if (partition == CONFIG_SYS_MMCSD_FS_BOOT_PARTITION) {
213 return -ENOSYS;
214 }
215 }
216#endif
217
Tien Fong Chee0c3a9ed2019-01-23 14:20:03 +0800218#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200219 if (!spl_start_uboot()) {
Simon Glass8a369de2024-08-22 07:54:58 -0600220 ret = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
221 partition);
222 if (!ret)
223 return 0;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200224 }
225#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Simon Glass8a369de2024-08-22 07:54:58 -0600226 ret = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400227 partition,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500228 filename);
Simon Glass8a369de2024-08-22 07:54:58 -0600229 if (!ret)
230 return ret;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200231#endif
232#endif
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800233#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200234 if (!spl_start_uboot()) {
Simon Glass8a369de2024-08-22 07:54:58 -0600235 ret = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
236 partition);
237 if (!ret)
238 return 0;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200239 }
240#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Simon Glass8a369de2024-08-22 07:54:58 -0600241 ret = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400242 partition,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500243 filename);
Simon Glass8a369de2024-08-22 07:54:58 -0600244 if (!ret)
245 return 0;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200246#endif
247#endif
248
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800249#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Simon Glass8a369de2024-08-22 07:54:58 -0600250 ret = -ENOENT;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200251#endif
252
Simon Glass8a369de2024-08-22 07:54:58 -0600253 return ret;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200254}
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200255#endif
256
Andre Przywara59073572021-07-12 11:06:49 +0100257u32 __weak spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Lukasz Majewskid695d662018-02-03 08:29:52 +0100258{
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800259#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewskid695d662018-02-03 08:29:52 +0100260 return MMCSD_MODE_FS;
261#elif defined(CONFIG_SUPPORT_EMMC_BOOT)
262 return MMCSD_MODE_EMMCBOOT;
263#else
264 return MMCSD_MODE_RAW;
265#endif
266}
267
Patrick Delaunay35a66962018-03-20 10:54:51 +0100268#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +0200269int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay35a66962018-03-20 10:54:51 +0100270{
271 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
272}
273#endif
274
Marek Vasute936db92023-10-16 18:16:12 +0200275unsigned long __weak arch_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
276 unsigned long raw_sect)
277{
278 return raw_sect;
279}
280
281unsigned long __weak board_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
282 unsigned long raw_sect)
283{
284 return arch_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
285}
286
Faiz Abbascf008252020-02-26 13:44:35 +0530287unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
288 unsigned long raw_sect)
Peng Fan30ecc9c2019-09-23 10:18:40 +0800289{
Marek Vasute936db92023-10-16 18:16:12 +0200290 return board_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fan30ecc9c2019-09-23 10:18:40 +0800291}
292
Marek Vasut9b191592021-07-03 04:55:32 +0200293int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
294{
295 int part;
296#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
297 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
298#else
299 /*
300 * We need to check what the partition is configured to.
301 * 1 and 2 match up to boot0 / boot1 and 7 is user data
302 * which is the first physical partition (0).
303 */
Pali Rohár8b882062023-03-11 11:44:27 +0100304 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
Marek Vasut9b191592021-07-03 04:55:32 +0200305 if (part == 7)
306 part = 0;
307#endif
308 return part;
309}
310
311int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
312{
313 return default_spl_mmc_emmc_boot_partition(mmc);
314}
315
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200316static int spl_mmc_get_mmc_devnum(struct mmc *mmc)
317{
318 struct blk_desc *block_dev;
319#if !CONFIG_IS_ENABLED(BLK)
320 block_dev = &mmc->block_dev;
321#else
Sean Andersona0733b32023-10-07 21:47:48 -0400322 block_dev = mmc_get_blk_desc(mmc);
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200323#endif
324 return block_dev->devnum;
325}
326
Sean Anderson93caa3e2023-10-14 16:47:54 -0400327static struct mmc *mmc;
328
329void spl_mmc_clear_cache(void)
330{
331 mmc = NULL;
332}
333
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500334int spl_mmc_load(struct spl_image_info *spl_image,
335 struct spl_boot_device *bootdev,
336 const char *filename,
337 int raw_part,
338 unsigned long raw_sect)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400339{
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400340 u32 boot_mode;
Simon Glass8a369de2024-08-22 07:54:58 -0600341 int ret = 0;
Baruch Siache42afd82020-01-15 09:08:08 +0200342 __maybe_unused int part = 0;
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200343 int mmc_dev;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400344
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200345 /* Perform peripheral init only once for an mmc device */
346 mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
347 if (!mmc || spl_mmc_get_mmc_devnum(mmc) != mmc_dev) {
Simon Glassc5cb9e52024-08-22 07:54:59 -0600348 ret = spl_mmc_find_device(&mmc, mmc_dev);
Simon Glass8a369de2024-08-22 07:54:58 -0600349 if (ret)
350 return ret;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400351
Simon Glass8a369de2024-08-22 07:54:58 -0600352 ret = mmc_init(mmc);
353 if (ret) {
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500354 mmc = NULL;
Simon Glass8a369de2024-08-22 07:54:58 -0600355 printf("spl: mmc init failed with error: %d\n", ret);
356 return ret;
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500357 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400358 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000359
Andre Przywara59073572021-07-12 11:06:49 +0100360 boot_mode = spl_mmc_boot_mode(mmc, bootdev->boot_device);
Simon Glass8a369de2024-08-22 07:54:58 -0600361 ret = -EINVAL;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200362 switch (boot_mode) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200363 case MMCSD_MODE_EMMCBOOT:
Marek Vasut9b191592021-07-03 04:55:32 +0200364 part = spl_mmc_emmc_boot_partition(mmc);
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200365
Mans Rullgard92439902019-09-03 16:17:17 +0200366 if (CONFIG_IS_ENABLED(MMC_TINY))
Simon Glass8a369de2024-08-22 07:54:58 -0600367 ret = mmc_switch_part(mmc, part);
Mans Rullgard92439902019-09-03 16:17:17 +0200368 else
Simon Glass8a369de2024-08-22 07:54:58 -0600369 ret = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutb5b838f2016-12-01 02:06:33 +0100370
Simon Glass8a369de2024-08-22 07:54:58 -0600371 if (ret) {
Mans Rullgard92439902019-09-03 16:17:17 +0200372 puts("spl: mmc partition switch failed\n");
Simon Glass8a369de2024-08-22 07:54:58 -0600373 return ret;
Mans Rullgard92439902019-09-03 16:17:17 +0200374 }
375 /* Fall through */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200376 case MMCSD_MODE_RAW:
377 debug("spl: mmc boot mode: raw\n");
378
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200379 if (!spl_start_uboot()) {
Simon Glass8a369de2024-08-22 07:54:58 -0600380 ret = mmc_load_image_raw_os(spl_image, bootdev, mmc);
381 if (!ret)
382 return 0;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200383 }
Peng Fana335f802019-10-23 01:43:30 +0000384
Faiz Abbascf008252020-02-26 13:44:35 +0530385 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fana335f802019-10-23 01:43:30 +0000386
Dalon Westergreen949123e2017-02-10 17:15:35 -0800387#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glass8a369de2024-08-22 07:54:58 -0600388 ret = mmc_load_image_raw_partition(spl_image, bootdev,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100389 mmc, raw_part,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500390 raw_sect);
Simon Glass8a369de2024-08-22 07:54:58 -0600391 if (!ret)
392 return 0;
Dalon Westergreen949123e2017-02-10 17:15:35 -0800393#endif
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200394#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Simon Glass8a369de2024-08-22 07:54:58 -0600395 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
396 raw_sect +
397 spl_mmc_raw_uboot_offset(part));
398 if (!ret)
399 return 0;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200400#endif
Guillaume GARDET86a0df72016-02-18 18:17:36 +0100401 /* If RAW mode fails, try FS mode. */
Pali Rohár718d1c72023-01-21 15:13:08 +0100402#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200403 case MMCSD_MODE_FS:
404 debug("spl: mmc boot mode: fs\n");
405
Simon Glass8a369de2024-08-22 07:54:58 -0600406 ret = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
407 if (!ret)
408 return 0;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200409
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200410 break;
Pali Rohár718d1c72023-01-21 15:13:08 +0100411#endif
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200412 default:
413 puts("spl: mmc: wrong boot mode\n");
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200414 }
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200415
Simon Glass8a369de2024-08-22 07:54:58 -0600416 return ret;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400417}
Simon Glass0fed9c72016-09-24 18:20:00 -0600418
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500419int spl_mmc_load_image(struct spl_image_info *spl_image,
420 struct spl_boot_device *bootdev)
421{
422 return spl_mmc_load(spl_image, bootdev,
423#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
424 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
425#else
426 NULL,
427#endif
428#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +0200429 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500430#else
431 0,
432#endif
433#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
434 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
435#else
436 0);
437#endif
438}
439
Simon Glassebc4ef62016-11-30 15:30:50 -0700440SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
441SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
442SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);