blob: a2ea363e96a9a8d561519ba5634c60fe32cb106f [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>
Tom Rini28591df2012-08-13 12:03:19 -070010#include <spl.h>
Paul Kocialkowski0d139722015-04-27 10:20:22 +020011#include <linux/compiler.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020012#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040013#include <asm/u-boot.h>
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020014#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040015#include <mmc.h>
Tom Rini1530e8a2013-06-28 14:43:01 -040016#include <image.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040017
Simon Glassee306792016-09-24 18:20:13 -060018static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc,
19 ulong sector, struct image_header *header)
Simon Glassb52c7fc2016-02-22 22:55:58 -070020{
21 u32 image_size_sectors;
22 unsigned long count;
Marek Vasut02266e22016-04-29 00:44:54 +020023 int ret;
24
Simon Glassee306792016-09-24 18:20:13 -060025 ret = spl_parse_image_header(spl_image, header);
Marek Vasut02266e22016-04-29 00:44:54 +020026 if (ret)
27 return ret;
Simon Glassb52c7fc2016-02-22 22:55:58 -070028
Simon Glassb52c7fc2016-02-22 22:55:58 -070029 /* convert size to sectors - round up */
Simon Glassee306792016-09-24 18:20:13 -060030 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
Simon Glassb52c7fc2016-02-22 22:55:58 -070031 mmc->read_bl_len;
32
33 /* Read the header too to avoid extra memcpy */
Simon Glass670d3042016-05-14 14:03:06 -060034 count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
Simon Glassee306792016-09-24 18:20:13 -060035 (void *)(ulong)spl_image->load_addr);
Andre Przywara6c526072017-01-02 11:48:31 +000036 debug("read %x sectors to %lx\n", image_size_sectors,
Simon Glassee306792016-09-24 18:20:13 -060037 spl_image->load_addr);
Simon Glassb52c7fc2016-02-22 22:55:58 -070038 if (count != image_size_sectors)
39 return -EIO;
40
41 return 0;
42}
43
Simon Glassb52c7fc2016-02-22 22:55:58 -070044static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
45 ulong count, void *buf)
46{
47 struct mmc *mmc = load->dev;
48
Simon Glass670d3042016-05-14 14:03:06 -060049 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
Simon Glassb52c7fc2016-02-22 22:55:58 -070050}
Simon Glassb52c7fc2016-02-22 22:55:58 -070051
Baruch Siachfef81be2020-01-15 09:08:08 +020052static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
53{
54#if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
55 if (part == 0)
56 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
57#endif
58
59 return 0;
60}
61
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090062static __maybe_unused
63int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
64 struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040065{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020066 unsigned long count;
Peter Korsgaardf2471292013-03-21 04:55:17 +000067 struct image_header *header;
Marek Vasut1dda46f2018-08-14 11:27:02 +020068 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glassb52c7fc2016-02-22 22:55:58 -070069 int ret = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040070
Marek Vasut1dda46f2018-08-14 11:27:02 +020071 header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
Simon Schwarze21d2d82011-09-14 15:33:34 -040072
73 /* read image header to find the image size & load address */
Marek Vasut1dda46f2018-08-14 11:27:02 +020074 count = blk_dread(bd, sector, 1, header);
Simon Glassb52c7fc2016-02-22 22:55:58 -070075 debug("hdr read sector %lx, count=%lu\n", sector, count);
76 if (count == 0) {
77 ret = -EIO;
Simon Schwarze21d2d82011-09-14 15:33:34 -040078 goto end;
Simon Glassb52c7fc2016-02-22 22:55:58 -070079 }
Simon Schwarze21d2d82011-09-14 15:33:34 -040080
Masahiro Yamada802f1c32016-03-16 12:10:00 +090081 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
82 image_get_magic(header) == FDT_MAGIC) {
Simon Glassb52c7fc2016-02-22 22:55:58 -070083 struct spl_load_info load;
84
85 debug("Found FIT\n");
86 load.dev = mmc;
87 load.priv = NULL;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +053088 load.filename = NULL;
Simon Glassb52c7fc2016-02-22 22:55:58 -070089 load.bl_len = mmc->read_bl_len;
90 load.read = h_spl_load_read;
Simon Glass43a734f2016-09-24 18:20:16 -060091 ret = spl_load_simple_fit(spl_image, &load, sector, header);
Peng Fand8670762019-08-22 07:42:38 +000092 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
93 struct spl_load_info load;
94
95 load.dev = mmc;
96 load.priv = NULL;
97 load.filename = NULL;
98 load.bl_len = mmc->read_bl_len;
99 load.read = h_spl_load_read;
100
101 ret = spl_load_imx_container(spl_image, &load, sector);
Masahiro Yamada802f1c32016-03-16 12:10:00 +0900102 } else {
Simon Glassee306792016-09-24 18:20:13 -0600103 ret = mmc_load_legacy(spl_image, mmc, sector, header);
Simon Glass5614db82015-06-23 15:38:47 -0600104 }
Tom Rini1530e8a2013-06-28 14:43:01 -0400105
Simon Schwarze21d2d82011-09-14 15:33:34 -0400106end:
Simon Glassb52c7fc2016-02-22 22:55:58 -0700107 if (ret) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100108#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300109 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100110#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200111 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200112 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200113
114 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400115}
116
Masahiro Yamada2a4a20c2017-06-22 16:52:36 +0900117static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200118{
119 switch (boot_device) {
120 case BOOT_DEVICE_MMC1:
121 return 0;
122 case BOOT_DEVICE_MMC2:
123 case BOOT_DEVICE_MMC2_2:
124 return 1;
125 }
126
127#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
128 printf("spl: unsupported mmc boot device.\n");
129#endif
130
131 return -ENODEV;
132}
133
Simon Glass4faf63b2015-12-02 10:59:12 -0700134static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200135{
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200136 int err, mmc_dev;
137
138 mmc_dev = spl_mmc_get_device_index(boot_device);
139 if (mmc_dev < 0)
140 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200141
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530142#if CONFIG_IS_ENABLED(DM_MMC)
143 err = mmc_init_device(mmc_dev);
144#else
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200145 err = mmc_initialize(NULL);
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530146#endif /* DM_MMC */
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200147 if (err) {
148#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
149 printf("spl: could not initialize mmc. error: %d\n", err);
150#endif
151 return err;
152 }
Simon Glass51ace322015-12-02 10:59:13 -0700153 *mmcp = find_mmc_device(mmc_dev);
154 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200155 if (err) {
156#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanf5920d02018-09-18 08:11:55 +0000157 printf("spl: could not find mmc device %d. error: %d\n",
158 mmc_dev, err);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200159#endif
160 return err;
161 }
162
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200163 return 0;
164}
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200165
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800166#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glassee306792016-09-24 18:20:13 -0600167static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500168 struct mmc *mmc, int partition,
169 unsigned long sector)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100170{
171 disk_partition_t info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200172 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100173
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800174#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
175 int type_part;
176 /* Only support MBR so DOS_ENTRY_NUMBERS */
177 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
178 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
179 if (err)
180 continue;
181 if (info.sys_ind ==
182 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
183 partition = type_part;
184 break;
185 }
186 }
187#endif
188
Simon Glass55877f92016-07-05 17:10:18 -0600189 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200190 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100191#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200192 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100193#endif
194 return -1;
195 }
196
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200197#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500198 return mmc_load_image_raw_sector(spl_image, mmc, info.start + sector);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200199#else
Simon Glassee306792016-09-24 18:20:13 -0600200 return mmc_load_image_raw_sector(spl_image, mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200201#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100202}
203#endif
204
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000205#ifdef CONFIG_SPL_OS_BOOT
Simon Glassee306792016-09-24 18:20:13 -0600206static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
207 struct mmc *mmc)
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000208{
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530209 int ret;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200210
York Suncea88212018-06-26 09:44:39 -0700211#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
212 unsigned long count;
213
Simon Glass70061ba2016-06-12 23:30:32 -0600214 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200215 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
216 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
217 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
Tom Rini195bc4b2017-08-14 21:01:30 -0400218 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100219#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300220 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100221#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000222 return -1;
223 }
York Suncea88212018-06-26 09:44:39 -0700224#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000225
Simon Glassee306792016-09-24 18:20:13 -0600226 ret = mmc_load_image_raw_sector(spl_image, mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200227 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530228 if (ret)
229 return ret;
230
Simon Glassee306792016-09-24 18:20:13 -0600231 if (spl_image->os != IH_OS_LINUX) {
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530232 puts("Expected Linux image is not found. Trying to start U-boot\n");
233 return -ENOENT;
234 }
235
236 return 0;
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000237}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200238#else
239int spl_start_uboot(void)
240{
241 return 1;
242}
Simon Glassee306792016-09-24 18:20:13 -0600243static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
244 struct mmc *mmc)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200245{
246 return -ENOSYS;
247}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000248#endif
249
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200250#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500251static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
252 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200253{
254 int err = -ENOSYS;
255
Tien Fong Chee6fd0a712019-01-23 14:20:03 +0800256#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200257 if (!spl_start_uboot()) {
Simon Glass3eb382a2016-09-24 18:20:15 -0600258 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200259 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
260 if (!err)
261 return err;
262 }
263#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Simon Glass3eb382a2016-09-24 18:20:15 -0600264 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200265 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500266 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200267 if (!err)
268 return err;
269#endif
270#endif
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800271#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200272 if (!spl_start_uboot()) {
Michal Simek0ef6ec82016-12-01 11:09:01 +0100273 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200274 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
275 if (!err)
276 return err;
277 }
278#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Michal Simek0ef6ec82016-12-01 11:09:01 +0100279 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200280 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500281 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200282 if (!err)
283 return err;
284#endif
285#endif
286
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800287#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200288 err = -ENOENT;
289#endif
290
291 return err;
292}
293#else
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500294static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
295 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200296{
297 return -ENOSYS;
298}
299#endif
300
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100301u32 __weak spl_boot_mode(const u32 boot_device)
302{
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800303#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100304 return MMCSD_MODE_FS;
305#elif defined(CONFIG_SUPPORT_EMMC_BOOT)
306 return MMCSD_MODE_EMMCBOOT;
307#else
308 return MMCSD_MODE_RAW;
309#endif
310}
311
Patrick Delaunay113aa502018-03-20 10:54:51 +0100312#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
313__weak
314int spl_boot_partition(const u32 boot_device)
315{
316 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
317}
318#endif
319
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530320unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
321 unsigned long raw_sect)
Peng Fan315cf592019-09-23 10:18:40 +0800322{
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530323 return raw_sect;
Peng Fan315cf592019-09-23 10:18:40 +0800324}
325
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500326int spl_mmc_load(struct spl_image_info *spl_image,
327 struct spl_boot_device *bootdev,
328 const char *filename,
329 int raw_part,
330 unsigned long raw_sect)
Simon Schwarze21d2d82011-09-14 15:33:34 -0400331{
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500332 static struct mmc *mmc;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400333 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600334 int err = 0;
Baruch Siachfef81be2020-01-15 09:08:08 +0200335 __maybe_unused int part = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400336
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500337 /* Perform peripheral init only once */
338 if (!mmc) {
339 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
340 if (err)
341 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400342
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500343 err = mmc_init(mmc);
344 if (err) {
345 mmc = NULL;
Paul Burtona8015fd2013-09-04 16:12:24 +0100346#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500347 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100348#endif
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500349 return err;
350 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400351 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000352
Simon Glassc0b6c9b2016-09-24 18:19:57 -0600353 boot_mode = spl_boot_mode(bootdev->boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200354 err = -EINVAL;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200355 switch (boot_mode) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200356 case MMCSD_MODE_EMMCBOOT:
Mans Rullgarde3b39c82019-09-03 16:16:50 +0200357#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
358 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
359#else
Mans Rullgardec046a52019-09-03 16:17:17 +0200360 /*
361 * We need to check what the partition is configured to.
362 * 1 and 2 match up to boot0 / boot1 and 7 is user data
363 * which is the first physical partition (0).
364 */
365 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200366
Mans Rullgardec046a52019-09-03 16:17:17 +0200367 if (part == 7)
368 part = 0;
Mans Rullgarde3b39c82019-09-03 16:16:50 +0200369#endif
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200370
Mans Rullgardec046a52019-09-03 16:17:17 +0200371 if (CONFIG_IS_ENABLED(MMC_TINY))
372 err = mmc_switch_part(mmc, part);
373 else
374 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutf537e392016-12-01 02:06:33 +0100375
Mans Rullgardec046a52019-09-03 16:17:17 +0200376 if (err) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200377#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgardec046a52019-09-03 16:17:17 +0200378 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200379#endif
Mans Rullgardec046a52019-09-03 16:17:17 +0200380 return err;
381 }
382 /* Fall through */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200383 case MMCSD_MODE_RAW:
384 debug("spl: mmc boot mode: raw\n");
385
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200386 if (!spl_start_uboot()) {
Simon Glassee306792016-09-24 18:20:13 -0600387 err = mmc_load_image_raw_os(spl_image, mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200388 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200389 return err;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200390 }
Peng Fandc7d3222019-10-23 01:43:30 +0000391
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530392 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fandc7d3222019-10-23 01:43:30 +0000393
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800394#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500395 err = mmc_load_image_raw_partition(spl_image, mmc, raw_part,
396 raw_sect);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200397 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200398 return err;
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800399#endif
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200400#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Baruch Siachfef81be2020-01-15 09:08:08 +0200401 err = mmc_load_image_raw_sector(spl_image, mmc,
402 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200403 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200404 return err;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200405#endif
Guillaume GARDET834dd882016-02-18 18:17:36 +0100406 /* If RAW mode fails, try FS mode. */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200407 case MMCSD_MODE_FS:
408 debug("spl: mmc boot mode: fs\n");
409
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500410 err = spl_mmc_do_fs_boot(spl_image, mmc, filename);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200411 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200412 return err;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200413
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200414 break;
Paul Burtona8015fd2013-09-04 16:12:24 +0100415#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200416 default:
417 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100418#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200419 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200420
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200421 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400422}
Simon Glassd449cb82016-09-24 18:20:00 -0600423
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500424int spl_mmc_load_image(struct spl_image_info *spl_image,
425 struct spl_boot_device *bootdev)
426{
427 return spl_mmc_load(spl_image, bootdev,
428#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
429 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
430#else
431 NULL,
432#endif
433#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
434 spl_boot_partition(bootdev->boot_device),
435#else
436 0,
437#endif
438#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
439 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
440#else
441 0);
442#endif
443}
444
Simon Glass4fc1f252016-11-30 15:30:50 -0700445SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
446SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
447SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);