blob: d52f8a3eefe6221cca7b3acfa22beab21f127d30 [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>
Paul Kocialkowski0d139722015-04-27 10:20:22 +020013#include <linux/compiler.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020014#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040015#include <asm/u-boot.h>
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020016#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040017#include <mmc.h>
Tom Rini1530e8a2013-06-28 14:43:01 -040018#include <image.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040019
Simon Glassee306792016-09-24 18:20:13 -060020static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc,
21 ulong sector, struct image_header *header)
Simon Glassb52c7fc2016-02-22 22:55:58 -070022{
Pali Rohár11041672021-07-23 11:14:27 +020023 u32 image_offset_sectors;
Simon Glassb52c7fc2016-02-22 22:55:58 -070024 u32 image_size_sectors;
25 unsigned long count;
Pali Rohár11041672021-07-23 11:14:27 +020026 u32 image_offset;
Marek Vasut02266e22016-04-29 00:44:54 +020027 int ret;
28
Simon Glassee306792016-09-24 18:20:13 -060029 ret = spl_parse_image_header(spl_image, header);
Marek Vasut02266e22016-04-29 00:44:54 +020030 if (ret)
31 return ret;
Simon Glassb52c7fc2016-02-22 22:55:58 -070032
Pali Rohár11041672021-07-23 11:14:27 +020033 /* convert offset to sectors - round down */
34 image_offset_sectors = spl_image->offset / mmc->read_bl_len;
35 /* calculate remaining offset */
36 image_offset = spl_image->offset % mmc->read_bl_len;
37
Simon Glassb52c7fc2016-02-22 22:55:58 -070038 /* convert size to sectors - round up */
Simon Glassee306792016-09-24 18:20:13 -060039 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
Simon Glassb52c7fc2016-02-22 22:55:58 -070040 mmc->read_bl_len;
41
42 /* Read the header too to avoid extra memcpy */
Pali Rohár11041672021-07-23 11:14:27 +020043 count = blk_dread(mmc_get_blk_desc(mmc),
44 sector + image_offset_sectors,
45 image_size_sectors,
Simon Glassee306792016-09-24 18:20:13 -060046 (void *)(ulong)spl_image->load_addr);
Andre Przywara6c526072017-01-02 11:48:31 +000047 debug("read %x sectors to %lx\n", image_size_sectors,
Simon Glassee306792016-09-24 18:20:13 -060048 spl_image->load_addr);
Simon Glassb52c7fc2016-02-22 22:55:58 -070049 if (count != image_size_sectors)
50 return -EIO;
51
Pali Rohár11041672021-07-23 11:14:27 +020052 if (image_offset)
53 memmove((void *)(ulong)spl_image->load_addr,
54 (void *)(ulong)spl_image->load_addr + image_offset,
55 spl_image->size);
56
Simon Glassb52c7fc2016-02-22 22:55:58 -070057 return 0;
58}
59
Simon Glassb52c7fc2016-02-22 22:55:58 -070060static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
61 ulong count, void *buf)
62{
63 struct mmc *mmc = load->dev;
64
Simon Glass670d3042016-05-14 14:03:06 -060065 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
Simon Glassb52c7fc2016-02-22 22:55:58 -070066}
Simon Glassb52c7fc2016-02-22 22:55:58 -070067
Baruch Siachfef81be2020-01-15 09:08:08 +020068static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
69{
70#if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
71 if (part == 0)
72 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
73#endif
74
75 return 0;
76}
77
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090078static __maybe_unused
79int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
80 struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040081{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020082 unsigned long count;
Peter Korsgaardf2471292013-03-21 04:55:17 +000083 struct image_header *header;
Marek Vasut1dda46f2018-08-14 11:27:02 +020084 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glassb52c7fc2016-02-22 22:55:58 -070085 int ret = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040086
Marek Vasut1dda46f2018-08-14 11:27:02 +020087 header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
Simon Schwarze21d2d82011-09-14 15:33:34 -040088
89 /* read image header to find the image size & load address */
Marek Vasut1dda46f2018-08-14 11:27:02 +020090 count = blk_dread(bd, sector, 1, header);
Simon Glassb52c7fc2016-02-22 22:55:58 -070091 debug("hdr read sector %lx, count=%lu\n", sector, count);
92 if (count == 0) {
93 ret = -EIO;
Simon Schwarze21d2d82011-09-14 15:33:34 -040094 goto end;
Simon Glassb52c7fc2016-02-22 22:55:58 -070095 }
Simon Schwarze21d2d82011-09-14 15:33:34 -040096
Masahiro Yamada802f1c32016-03-16 12:10:00 +090097 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
98 image_get_magic(header) == FDT_MAGIC) {
Simon Glassb52c7fc2016-02-22 22:55:58 -070099 struct spl_load_info load;
100
101 debug("Found FIT\n");
102 load.dev = mmc;
103 load.priv = NULL;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530104 load.filename = NULL;
Simon Glassb52c7fc2016-02-22 22:55:58 -0700105 load.bl_len = mmc->read_bl_len;
106 load.read = h_spl_load_read;
Simon Glass43a734f2016-09-24 18:20:16 -0600107 ret = spl_load_simple_fit(spl_image, &load, sector, header);
Peng Fand8670762019-08-22 07:42:38 +0000108 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
109 struct spl_load_info load;
110
111 load.dev = mmc;
112 load.priv = NULL;
113 load.filename = NULL;
114 load.bl_len = mmc->read_bl_len;
115 load.read = h_spl_load_read;
116
117 ret = spl_load_imx_container(spl_image, &load, sector);
Masahiro Yamada802f1c32016-03-16 12:10:00 +0900118 } else {
Simon Glassee306792016-09-24 18:20:13 -0600119 ret = mmc_load_legacy(spl_image, mmc, sector, header);
Simon Glass5614db82015-06-23 15:38:47 -0600120 }
Tom Rini1530e8a2013-06-28 14:43:01 -0400121
Simon Schwarze21d2d82011-09-14 15:33:34 -0400122end:
Simon Glassb52c7fc2016-02-22 22:55:58 -0700123 if (ret) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100124#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300125 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100126#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200127 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200128 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200129
130 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400131}
132
Masahiro Yamada2a4a20c2017-06-22 16:52:36 +0900133static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200134{
135 switch (boot_device) {
136 case BOOT_DEVICE_MMC1:
137 return 0;
138 case BOOT_DEVICE_MMC2:
139 case BOOT_DEVICE_MMC2_2:
140 return 1;
141 }
142
143#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
144 printf("spl: unsupported mmc boot device.\n");
145#endif
146
147 return -ENODEV;
148}
149
Simon Glass4faf63b2015-12-02 10:59:12 -0700150static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200151{
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200152 int err, mmc_dev;
153
154 mmc_dev = spl_mmc_get_device_index(boot_device);
155 if (mmc_dev < 0)
156 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200157
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530158#if CONFIG_IS_ENABLED(DM_MMC)
159 err = mmc_init_device(mmc_dev);
160#else
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200161 err = mmc_initialize(NULL);
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530162#endif /* DM_MMC */
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200163 if (err) {
164#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
165 printf("spl: could not initialize mmc. error: %d\n", err);
166#endif
167 return err;
168 }
Simon Glass51ace322015-12-02 10:59:13 -0700169 *mmcp = find_mmc_device(mmc_dev);
170 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200171 if (err) {
172#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanf5920d02018-09-18 08:11:55 +0000173 printf("spl: could not find mmc device %d. error: %d\n",
174 mmc_dev, err);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200175#endif
176 return err;
177 }
178
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200179 return 0;
180}
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200181
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800182#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glassee306792016-09-24 18:20:13 -0600183static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500184 struct mmc *mmc, int partition,
185 unsigned long sector)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100186{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600187 struct disk_partition info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200188 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100189
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800190#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
191 int type_part;
192 /* Only support MBR so DOS_ENTRY_NUMBERS */
193 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
194 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
195 if (err)
196 continue;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200197 if (info.sys_ind ==
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800198 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
199 partition = type_part;
200 break;
201 }
202 }
203#endif
204
Simon Glass55877f92016-07-05 17:10:18 -0600205 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200206 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100207#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200208 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100209#endif
210 return -1;
211 }
212
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200213#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500214 return mmc_load_image_raw_sector(spl_image, mmc, info.start + sector);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200215#else
Simon Glassee306792016-09-24 18:20:13 -0600216 return mmc_load_image_raw_sector(spl_image, mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200217#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100218}
219#endif
220
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000221#ifdef CONFIG_SPL_OS_BOOT
Simon Glassee306792016-09-24 18:20:13 -0600222static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
223 struct mmc *mmc)
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000224{
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530225 int ret;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200226
York Suncea88212018-06-26 09:44:39 -0700227#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
228 unsigned long count;
229
Simon Glass70061ba2016-06-12 23:30:32 -0600230 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200231 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
232 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
233 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
Tom Rini195bc4b2017-08-14 21:01:30 -0400234 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100235#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300236 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100237#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000238 return -1;
239 }
York Suncea88212018-06-26 09:44:39 -0700240#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000241
Simon Glassee306792016-09-24 18:20:13 -0600242 ret = mmc_load_image_raw_sector(spl_image, mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200243 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530244 if (ret)
245 return ret;
246
Alexandru Gagniuc36fd3ba2021-07-15 14:19:23 -0500247 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
248 puts("Expected image is not found. Trying to start U-boot\n");
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530249 return -ENOENT;
250 }
251
252 return 0;
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000253}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200254#else
255int spl_start_uboot(void)
256{
257 return 1;
258}
Simon Glassee306792016-09-24 18:20:13 -0600259static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
260 struct mmc *mmc)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200261{
262 return -ENOSYS;
263}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000264#endif
265
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200266#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500267static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
268 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200269{
270 int err = -ENOSYS;
271
Tien Fong Chee6fd0a712019-01-23 14:20:03 +0800272#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200273 if (!spl_start_uboot()) {
Simon Glass3eb382a2016-09-24 18:20:15 -0600274 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200275 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
276 if (!err)
277 return err;
278 }
279#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Simon Glass3eb382a2016-09-24 18:20:15 -0600280 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200281 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500282 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200283 if (!err)
284 return err;
285#endif
286#endif
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800287#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200288 if (!spl_start_uboot()) {
Michal Simek0ef6ec82016-12-01 11:09:01 +0100289 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200290 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
291 if (!err)
292 return err;
293 }
294#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Michal Simek0ef6ec82016-12-01 11:09:01 +0100295 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200296 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500297 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200298 if (!err)
299 return err;
300#endif
301#endif
302
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800303#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200304 err = -ENOENT;
305#endif
306
307 return err;
308}
309#else
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500310static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
311 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200312{
313 return -ENOSYS;
314}
315#endif
316
Harald Seiler0bf7ab12020-04-15 11:33:30 +0200317u32 __weak spl_mmc_boot_mode(const u32 boot_device)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100318{
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800319#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100320 return MMCSD_MODE_FS;
321#elif defined(CONFIG_SUPPORT_EMMC_BOOT)
322 return MMCSD_MODE_EMMCBOOT;
323#else
324 return MMCSD_MODE_RAW;
325#endif
326}
327
Patrick Delaunay113aa502018-03-20 10:54:51 +0100328#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200329int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay113aa502018-03-20 10:54:51 +0100330{
331 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
332}
333#endif
334
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530335unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
336 unsigned long raw_sect)
Peng Fan315cf592019-09-23 10:18:40 +0800337{
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530338 return raw_sect;
Peng Fan315cf592019-09-23 10:18:40 +0800339}
340
Marek Vasutde1ce232021-07-03 04:55:32 +0200341int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
342{
343 int part;
344#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
345 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
346#else
347 /*
348 * We need to check what the partition is configured to.
349 * 1 and 2 match up to boot0 / boot1 and 7 is user data
350 * which is the first physical partition (0).
351 */
352 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
353 if (part == 7)
354 part = 0;
355#endif
356 return part;
357}
358
359int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
360{
361 return default_spl_mmc_emmc_boot_partition(mmc);
362}
363
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500364int spl_mmc_load(struct spl_image_info *spl_image,
365 struct spl_boot_device *bootdev,
366 const char *filename,
367 int raw_part,
368 unsigned long raw_sect)
Simon Schwarze21d2d82011-09-14 15:33:34 -0400369{
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500370 static struct mmc *mmc;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400371 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600372 int err = 0;
Baruch Siachfef81be2020-01-15 09:08:08 +0200373 __maybe_unused int part = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400374
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500375 /* Perform peripheral init only once */
376 if (!mmc) {
377 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
378 if (err)
379 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400380
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500381 err = mmc_init(mmc);
382 if (err) {
383 mmc = NULL;
Paul Burtona8015fd2013-09-04 16:12:24 +0100384#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500385 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100386#endif
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500387 return err;
388 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400389 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000390
Harald Seiler0bf7ab12020-04-15 11:33:30 +0200391 boot_mode = spl_mmc_boot_mode(bootdev->boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200392 err = -EINVAL;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200393 switch (boot_mode) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200394 case MMCSD_MODE_EMMCBOOT:
Marek Vasutde1ce232021-07-03 04:55:32 +0200395 part = spl_mmc_emmc_boot_partition(mmc);
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200396
Mans Rullgardec046a52019-09-03 16:17:17 +0200397 if (CONFIG_IS_ENABLED(MMC_TINY))
398 err = mmc_switch_part(mmc, part);
399 else
400 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutf537e392016-12-01 02:06:33 +0100401
Mans Rullgardec046a52019-09-03 16:17:17 +0200402 if (err) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200403#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgardec046a52019-09-03 16:17:17 +0200404 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200405#endif
Mans Rullgardec046a52019-09-03 16:17:17 +0200406 return err;
407 }
408 /* Fall through */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200409 case MMCSD_MODE_RAW:
410 debug("spl: mmc boot mode: raw\n");
411
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200412 if (!spl_start_uboot()) {
Simon Glassee306792016-09-24 18:20:13 -0600413 err = mmc_load_image_raw_os(spl_image, mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200414 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200415 return err;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200416 }
Peng Fandc7d3222019-10-23 01:43:30 +0000417
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530418 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fandc7d3222019-10-23 01:43:30 +0000419
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800420#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500421 err = mmc_load_image_raw_partition(spl_image, mmc, raw_part,
422 raw_sect);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200423 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200424 return err;
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800425#endif
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200426#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Baruch Siachfef81be2020-01-15 09:08:08 +0200427 err = mmc_load_image_raw_sector(spl_image, mmc,
428 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200429 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200430 return err;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200431#endif
Guillaume GARDET834dd882016-02-18 18:17:36 +0100432 /* If RAW mode fails, try FS mode. */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200433 case MMCSD_MODE_FS:
434 debug("spl: mmc boot mode: fs\n");
435
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500436 err = spl_mmc_do_fs_boot(spl_image, mmc, filename);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200437 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200438 return err;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200439
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200440 break;
Paul Burtona8015fd2013-09-04 16:12:24 +0100441#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200442 default:
443 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100444#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200445 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200446
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200447 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400448}
Simon Glassd449cb82016-09-24 18:20:00 -0600449
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500450int spl_mmc_load_image(struct spl_image_info *spl_image,
451 struct spl_boot_device *bootdev)
452{
453 return spl_mmc_load(spl_image, bootdev,
454#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
455 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
456#else
457 NULL,
458#endif
459#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200460 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500461#else
462 0,
463#endif
464#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
465 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
466#else
467 0);
468#endif
469}
470
Simon Glass4fc1f252016-11-30 15:30:50 -0700471SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
472SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
473SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);