blob: 1f69659321620739d066bd806acd1d282b66bad3 [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
Simon Glass3fd11272024-08-22 07:55:02 -060049 spl_load_init(&load, h_spl_load_read, bd, bd->blksz);
Sean Anderson5d3401a2023-11-08 11:48:50 -050050 ret = spl_load(spl_image, bootdev, &load, 0, sector << bd->log2blksz);
Simon Glass96debd12016-02-22 22:55:58 -070051 if (ret) {
Fabio Estevam5bbf4092016-04-20 23:12:04 -030052 puts("mmc_load_image_raw_sector: mmc block read error\n");
Simon Glass69bd5172024-09-20 09:24:37 +020053 log_debug("(error=%d)\n", ret);
Simon Glass8a369de2024-08-22 07:54:58 -060054 return ret;
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020055 }
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020056
57 return 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040058}
59
Simon Glassc5cb9e52024-08-22 07:54:59 -060060static int spl_mmc_get_device_index(uint boot_device)
Nikita Kiryanova1e56cf2015-11-08 17:11:54 +020061{
62 switch (boot_device) {
63 case BOOT_DEVICE_MMC1:
64 return 0;
65 case BOOT_DEVICE_MMC2:
66 case BOOT_DEVICE_MMC2_2:
67 return 1;
68 }
69
Nikita Kiryanova1e56cf2015-11-08 17:11:54 +020070 printf("spl: unsupported mmc boot device.\n");
Nikita Kiryanova1e56cf2015-11-08 17:11:54 +020071
72 return -ENODEV;
73}
74
Simon Glassc5cb9e52024-08-22 07:54:59 -060075static int spl_mmc_find_device(struct mmc **mmcp, int mmc_dev)
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020076{
Simon Glassc5cb9e52024-08-22 07:54:59 -060077 int ret;
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020078
Lokesh Vutla80f02012019-09-09 14:40:36 +053079#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass69bd5172024-09-20 09:24:37 +020080 struct udevice *dev;
81 struct uclass *uc;
82
83 log_debug("Selecting MMC dev %d; seqs:\n", mmc_dev);
84 uclass_id_foreach_dev(UCLASS_MMC, dev, uc)
85 log_debug("%d: %s\n", dev_seq(dev), dev->name);
Simon Glass8a369de2024-08-22 07:54:58 -060086 ret = mmc_init_device(mmc_dev);
Lokesh Vutla80f02012019-09-09 14:40:36 +053087#else
Simon Glass8a369de2024-08-22 07:54:58 -060088 ret = mmc_initialize(NULL);
Lokesh Vutla80f02012019-09-09 14:40:36 +053089#endif /* DM_MMC */
Simon Glass8a369de2024-08-22 07:54:58 -060090 if (ret) {
91 printf("spl: could not initialize mmc. error: %d\n", ret);
92 return ret;
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020093 }
Simon Glassb4857aa2015-12-02 10:59:13 -070094 *mmcp = find_mmc_device(mmc_dev);
Simon Glass8a369de2024-08-22 07:54:58 -060095 ret = *mmcp ? 0 : -ENODEV;
96 if (ret) {
Alex Kiernanaa608b62018-09-18 08:11:55 +000097 printf("spl: could not find mmc device %d. error: %d\n",
Simon Glass8a369de2024-08-22 07:54:58 -060098 mmc_dev, ret);
99 return ret;
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200100 }
Simon Glass69bd5172024-09-20 09:24:37 +0200101#if CONFIG_IS_ENABLED(DM_MMC)
102 log_debug("mmc %d: %s\n", mmc_dev, (*mmcp)->dev->name);
103#endif
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200104
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200105 return 0;
106}
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200107
Dalon Westergreen949123e2017-02-10 17:15:35 -0800108#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600109static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100110 struct spl_boot_device *bootdev,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500111 struct mmc *mmc, int partition,
112 unsigned long sector)
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100113{
Simon Glass05289792020-05-10 11:39:57 -0600114 struct disk_partition info;
Simon Glass8a369de2024-08-22 07:54:58 -0600115 int ret;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100116
Dalon Westergreenf0fb4fa2017-02-10 17:15:34 -0800117#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
118 int type_part;
119 /* Only support MBR so DOS_ENTRY_NUMBERS */
120 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
Simon Glass8a369de2024-08-22 07:54:58 -0600121 ret = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
122 if (ret)
Dalon Westergreenf0fb4fa2017-02-10 17:15:34 -0800123 continue;
Wolfgang Denk0a50b3c2021-09-27 17:42:38 +0200124 if (info.sys_ind ==
Dalon Westergreenf0fb4fa2017-02-10 17:15:34 -0800125 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
126 partition = type_part;
127 break;
128 }
129 }
130#endif
131
Simon Glass8a369de2024-08-22 07:54:58 -0600132 ret = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
133 if (ret) {
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200134 puts("spl: partition error\n");
Simon Glass8a369de2024-08-22 07:54:58 -0600135 return ret;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100136 }
137
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200138#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohár2e0429b2022-01-14 14:31:38 +0100139 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200140#else
Pali Rohár2e0429b2022-01-14 14:31:38 +0100141 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200142#endif
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100143}
144#endif
145
Alexandru Gagniuc22eb1522021-12-30 10:39:59 -0600146#if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600147static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100148 struct spl_boot_device *bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600149 struct mmc *mmc)
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000150{
Lokesh Vutla811906a2016-04-11 15:24:50 +0530151 int ret;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200152
Elena Popa86b1aad2023-08-08 16:42:15 +0300153#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
York Sun7267fbf2018-06-26 09:44:39 -0700154 unsigned long count;
155
Simon Glass87bce4e2016-06-12 23:30:32 -0600156 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200157 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
158 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
Simon Glass9cbdc3a2023-09-26 08:14:17 -0600159 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
Tom Rini9585dd32017-08-14 21:01:30 -0400160 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Fabio Estevam5bbf4092016-04-20 23:12:04 -0300161 puts("mmc_load_image_raw_os: mmc block read error\n");
Simon Glass8a369de2024-08-22 07:54:58 -0600162 return -EIO;
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000163 }
York Sun7267fbf2018-06-26 09:44:39 -0700164#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000165
Pali Rohár2e0429b2022-01-14 14:31:38 +0100166 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200167 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla811906a2016-04-11 15:24:50 +0530168 if (ret)
169 return ret;
170
Alexandru Gagniuc5f6e5c32021-07-15 14:19:23 -0500171 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
Michal Simek1be82af2023-05-17 09:17:16 +0200172 puts("Expected image is not found. Trying to start U-Boot\n");
Lokesh Vutla811906a2016-04-11 15:24:50 +0530173 return -ENOENT;
174 }
175
176 return 0;
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000177}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200178#else
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600179static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100180 struct spl_boot_device *bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600181 struct mmc *mmc)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200182{
183 return -ENOSYS;
184}
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000185#endif
186
Alexandru Gagniuc22eb1522021-12-30 10:39:59 -0600187#ifndef CONFIG_SPL_OS_BOOT
188int spl_start_uboot(void)
189{
190 return 1;
191}
192#endif
193
Pali Rohár718d1c72023-01-21 15:13:08 +0100194#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Pali Rohár2e0429b2022-01-14 14:31:38 +0100195static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
196 struct spl_boot_device *bootdev,
197 struct mmc *mmc,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500198 const char *filename)
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200199{
Simon Glass8a369de2024-08-22 07:54:58 -0600200 int ret = -ENOSYS;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200201
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400202 __maybe_unused int partition = CONFIG_SYS_MMCSD_FS_BOOT_PARTITION;
203
204#if CONFIG_SYS_MMCSD_FS_BOOT_PARTITION == -1
205 {
206 struct disk_partition info;
207 debug("Checking for the first MBR bootable partition\n");
208 for (int type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
Simon Glass8a369de2024-08-22 07:54:58 -0600209 ret = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
210 if (ret)
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400211 continue;
212 debug("Partition %d is of type %d and bootable=%d\n", type_part, info.sys_ind, info.bootable);
213 if (info.bootable != 0) {
214 debug("Partition %d is bootable, using it\n", type_part);
215 partition = type_part;
216 break;
217 }
218 }
219 printf("Using first bootable partition: %d\n", partition);
220 if (partition == CONFIG_SYS_MMCSD_FS_BOOT_PARTITION) {
221 return -ENOSYS;
222 }
223 }
224#endif
225
Tien Fong Chee0c3a9ed2019-01-23 14:20:03 +0800226#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200227 if (!spl_start_uboot()) {
Simon Glass8a369de2024-08-22 07:54:58 -0600228 ret = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
229 partition);
230 if (!ret)
231 return 0;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200232 }
233#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Simon Glass8a369de2024-08-22 07:54:58 -0600234 ret = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400235 partition,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500236 filename);
Simon Glass8a369de2024-08-22 07:54:58 -0600237 if (!ret)
238 return ret;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200239#endif
240#endif
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800241#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200242 if (!spl_start_uboot()) {
Simon Glass8a369de2024-08-22 07:54:58 -0600243 ret = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
244 partition);
245 if (!ret)
246 return 0;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200247 }
248#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Simon Glass8a369de2024-08-22 07:54:58 -0600249 ret = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400250 partition,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500251 filename);
Simon Glass8a369de2024-08-22 07:54:58 -0600252 if (!ret)
253 return 0;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200254#endif
255#endif
256
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800257#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Simon Glass8a369de2024-08-22 07:54:58 -0600258 ret = -ENOENT;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200259#endif
260
Simon Glass8a369de2024-08-22 07:54:58 -0600261 return ret;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200262}
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200263#endif
264
Andre Przywara59073572021-07-12 11:06:49 +0100265u32 __weak spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Lukasz Majewskid695d662018-02-03 08:29:52 +0100266{
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800267#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewskid695d662018-02-03 08:29:52 +0100268 return MMCSD_MODE_FS;
269#elif defined(CONFIG_SUPPORT_EMMC_BOOT)
270 return MMCSD_MODE_EMMCBOOT;
271#else
272 return MMCSD_MODE_RAW;
273#endif
274}
275
Patrick Delaunay35a66962018-03-20 10:54:51 +0100276#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +0200277int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay35a66962018-03-20 10:54:51 +0100278{
279 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
280}
281#endif
282
Marek Vasute936db92023-10-16 18:16:12 +0200283unsigned long __weak arch_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
284 unsigned long raw_sect)
285{
286 return raw_sect;
287}
288
289unsigned long __weak board_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
290 unsigned long raw_sect)
291{
292 return arch_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
293}
294
Faiz Abbascf008252020-02-26 13:44:35 +0530295unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
296 unsigned long raw_sect)
Peng Fan30ecc9c2019-09-23 10:18:40 +0800297{
Marek Vasute936db92023-10-16 18:16:12 +0200298 return board_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fan30ecc9c2019-09-23 10:18:40 +0800299}
300
Marek Vasut9b191592021-07-03 04:55:32 +0200301int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
302{
303 int part;
304#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
305 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
306#else
307 /*
308 * We need to check what the partition is configured to.
309 * 1 and 2 match up to boot0 / boot1 and 7 is user data
310 * which is the first physical partition (0).
311 */
Pali Rohár8b882062023-03-11 11:44:27 +0100312 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
Tim Harvey8746aa02024-05-31 08:36:33 -0700313 if (part == EMMC_BOOT_PART_USER)
314 part = EMMC_HWPART_DEFAULT;
Marek Vasut9b191592021-07-03 04:55:32 +0200315#endif
316 return part;
317}
318
319int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
320{
321 return default_spl_mmc_emmc_boot_partition(mmc);
322}
323
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200324static int spl_mmc_get_mmc_devnum(struct mmc *mmc)
325{
326 struct blk_desc *block_dev;
327#if !CONFIG_IS_ENABLED(BLK)
328 block_dev = &mmc->block_dev;
329#else
Sean Andersona0733b32023-10-07 21:47:48 -0400330 block_dev = mmc_get_blk_desc(mmc);
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200331#endif
332 return block_dev->devnum;
333}
334
Sean Anderson93caa3e2023-10-14 16:47:54 -0400335static struct mmc *mmc;
336
337void spl_mmc_clear_cache(void)
338{
339 mmc = NULL;
340}
341
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500342int spl_mmc_load(struct spl_image_info *spl_image,
343 struct spl_boot_device *bootdev,
344 const char *filename,
345 int raw_part,
346 unsigned long raw_sect)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400347{
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400348 u32 boot_mode;
Simon Glass8a369de2024-08-22 07:54:58 -0600349 int ret = 0;
Baruch Siache42afd82020-01-15 09:08:08 +0200350 __maybe_unused int part = 0;
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200351 int mmc_dev;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400352
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200353 /* Perform peripheral init only once for an mmc device */
354 mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
Simon Glass69bd5172024-09-20 09:24:37 +0200355 log_debug("boot_device=%d, mmc_dev=%d\n", bootdev->boot_device,
356 mmc_dev);
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200357 if (!mmc || spl_mmc_get_mmc_devnum(mmc) != mmc_dev) {
Simon Glassc5cb9e52024-08-22 07:54:59 -0600358 ret = spl_mmc_find_device(&mmc, mmc_dev);
Simon Glass8a369de2024-08-22 07:54:58 -0600359 if (ret)
360 return ret;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400361
Simon Glass8a369de2024-08-22 07:54:58 -0600362 ret = mmc_init(mmc);
363 if (ret) {
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500364 mmc = NULL;
Simon Glass8a369de2024-08-22 07:54:58 -0600365 printf("spl: mmc init failed with error: %d\n", ret);
366 return ret;
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500367 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400368 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000369
Andre Przywara59073572021-07-12 11:06:49 +0100370 boot_mode = spl_mmc_boot_mode(mmc, bootdev->boot_device);
Simon Glass8a369de2024-08-22 07:54:58 -0600371 ret = -EINVAL;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200372 switch (boot_mode) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200373 case MMCSD_MODE_EMMCBOOT:
Marek Vasut9b191592021-07-03 04:55:32 +0200374 part = spl_mmc_emmc_boot_partition(mmc);
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200375
Mans Rullgard92439902019-09-03 16:17:17 +0200376 if (CONFIG_IS_ENABLED(MMC_TINY))
Simon Glass8a369de2024-08-22 07:54:58 -0600377 ret = mmc_switch_part(mmc, part);
Mans Rullgard92439902019-09-03 16:17:17 +0200378 else
Simon Glass8a369de2024-08-22 07:54:58 -0600379 ret = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutb5b838f2016-12-01 02:06:33 +0100380
Simon Glass8a369de2024-08-22 07:54:58 -0600381 if (ret) {
Mans Rullgard92439902019-09-03 16:17:17 +0200382 puts("spl: mmc partition switch failed\n");
Simon Glass8a369de2024-08-22 07:54:58 -0600383 return ret;
Mans Rullgard92439902019-09-03 16:17:17 +0200384 }
385 /* Fall through */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200386 case MMCSD_MODE_RAW:
387 debug("spl: mmc boot mode: raw\n");
388
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200389 if (!spl_start_uboot()) {
Simon Glass8a369de2024-08-22 07:54:58 -0600390 ret = mmc_load_image_raw_os(spl_image, bootdev, mmc);
391 if (!ret)
392 return 0;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200393 }
Peng Fana335f802019-10-23 01:43:30 +0000394
Faiz Abbascf008252020-02-26 13:44:35 +0530395 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fana335f802019-10-23 01:43:30 +0000396
Dalon Westergreen949123e2017-02-10 17:15:35 -0800397#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glass8a369de2024-08-22 07:54:58 -0600398 ret = mmc_load_image_raw_partition(spl_image, bootdev,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100399 mmc, raw_part,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500400 raw_sect);
Simon Glass8a369de2024-08-22 07:54:58 -0600401 if (!ret)
402 return 0;
Dalon Westergreen949123e2017-02-10 17:15:35 -0800403#endif
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200404#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Simon Glass8a369de2024-08-22 07:54:58 -0600405 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
406 raw_sect +
407 spl_mmc_raw_uboot_offset(part));
408 if (!ret)
409 return 0;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200410#endif
Guillaume GARDET86a0df72016-02-18 18:17:36 +0100411 /* If RAW mode fails, try FS mode. */
Pali Rohár718d1c72023-01-21 15:13:08 +0100412#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200413 case MMCSD_MODE_FS:
414 debug("spl: mmc boot mode: fs\n");
415
Simon Glass8a369de2024-08-22 07:54:58 -0600416 ret = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
417 if (!ret)
418 return 0;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200419
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200420 break;
Pali Rohár718d1c72023-01-21 15:13:08 +0100421#endif
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200422 default:
423 puts("spl: mmc: wrong boot mode\n");
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200424 }
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200425
Simon Glass8a369de2024-08-22 07:54:58 -0600426 return ret;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400427}
Simon Glass0fed9c72016-09-24 18:20:00 -0600428
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500429int spl_mmc_load_image(struct spl_image_info *spl_image,
430 struct spl_boot_device *bootdev)
431{
432 return spl_mmc_load(spl_image, bootdev,
433#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
434 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
435#else
436 NULL,
437#endif
438#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +0200439 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500440#else
441 0,
442#endif
443#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
444 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
445#else
446 0);
447#endif
448}
449
Simon Glassebc4ef62016-11-30 15:30:50 -0700450SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
451SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
452SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);