blob: 95d5a7271f5e9b69a1050f90c88dce16af71adb4 [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 Glass655306c2020-05-10 11:39:58 -060010#include <part.h>
Tom Rini28591df2012-08-13 12:03:19 -070011#include <spl.h>
Paul Kocialkowski0d139722015-04-27 10:20:22 +020012#include <linux/compiler.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020013#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040014#include <asm/u-boot.h>
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020015#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040016#include <mmc.h>
Tom Rini1530e8a2013-06-28 14:43:01 -040017#include <image.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040018
Simon Glassee306792016-09-24 18:20:13 -060019static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc,
20 ulong sector, struct image_header *header)
Simon Glassb52c7fc2016-02-22 22:55:58 -070021{
22 u32 image_size_sectors;
23 unsigned long count;
Marek Vasut02266e22016-04-29 00:44:54 +020024 int ret;
25
Simon Glassee306792016-09-24 18:20:13 -060026 ret = spl_parse_image_header(spl_image, header);
Marek Vasut02266e22016-04-29 00:44:54 +020027 if (ret)
28 return ret;
Simon Glassb52c7fc2016-02-22 22:55:58 -070029
Simon Glassb52c7fc2016-02-22 22:55:58 -070030 /* convert size to sectors - round up */
Simon Glassee306792016-09-24 18:20:13 -060031 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
Simon Glassb52c7fc2016-02-22 22:55:58 -070032 mmc->read_bl_len;
33
34 /* Read the header too to avoid extra memcpy */
Simon Glass670d3042016-05-14 14:03:06 -060035 count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
Simon Glassee306792016-09-24 18:20:13 -060036 (void *)(ulong)spl_image->load_addr);
Andre Przywara6c526072017-01-02 11:48:31 +000037 debug("read %x sectors to %lx\n", image_size_sectors,
Simon Glassee306792016-09-24 18:20:13 -060038 spl_image->load_addr);
Simon Glassb52c7fc2016-02-22 22:55:58 -070039 if (count != image_size_sectors)
40 return -EIO;
41
42 return 0;
43}
44
Simon Glassb52c7fc2016-02-22 22:55:58 -070045static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
46 ulong count, void *buf)
47{
48 struct mmc *mmc = load->dev;
49
Simon Glass670d3042016-05-14 14:03:06 -060050 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
Simon Glassb52c7fc2016-02-22 22:55:58 -070051}
Simon Glassb52c7fc2016-02-22 22:55:58 -070052
Baruch Siachfef81be2020-01-15 09:08:08 +020053static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
54{
55#if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
56 if (part == 0)
57 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
58#endif
59
60 return 0;
61}
62
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090063static __maybe_unused
64int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
65 struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040066{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020067 unsigned long count;
Peter Korsgaardf2471292013-03-21 04:55:17 +000068 struct image_header *header;
Marek Vasut1dda46f2018-08-14 11:27:02 +020069 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glassb52c7fc2016-02-22 22:55:58 -070070 int ret = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040071
Marek Vasut1dda46f2018-08-14 11:27:02 +020072 header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
Simon Schwarze21d2d82011-09-14 15:33:34 -040073
74 /* read image header to find the image size & load address */
Marek Vasut1dda46f2018-08-14 11:27:02 +020075 count = blk_dread(bd, sector, 1, header);
Simon Glassb52c7fc2016-02-22 22:55:58 -070076 debug("hdr read sector %lx, count=%lu\n", sector, count);
77 if (count == 0) {
78 ret = -EIO;
Simon Schwarze21d2d82011-09-14 15:33:34 -040079 goto end;
Simon Glassb52c7fc2016-02-22 22:55:58 -070080 }
Simon Schwarze21d2d82011-09-14 15:33:34 -040081
Masahiro Yamada802f1c32016-03-16 12:10:00 +090082 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
83 image_get_magic(header) == FDT_MAGIC) {
Simon Glassb52c7fc2016-02-22 22:55:58 -070084 struct spl_load_info load;
85
86 debug("Found FIT\n");
87 load.dev = mmc;
88 load.priv = NULL;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +053089 load.filename = NULL;
Simon Glassb52c7fc2016-02-22 22:55:58 -070090 load.bl_len = mmc->read_bl_len;
91 load.read = h_spl_load_read;
Simon Glass43a734f2016-09-24 18:20:16 -060092 ret = spl_load_simple_fit(spl_image, &load, sector, header);
Peng Fand8670762019-08-22 07:42:38 +000093 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
94 struct spl_load_info load;
95
96 load.dev = mmc;
97 load.priv = NULL;
98 load.filename = NULL;
99 load.bl_len = mmc->read_bl_len;
100 load.read = h_spl_load_read;
101
102 ret = spl_load_imx_container(spl_image, &load, sector);
Masahiro Yamada802f1c32016-03-16 12:10:00 +0900103 } else {
Simon Glassee306792016-09-24 18:20:13 -0600104 ret = mmc_load_legacy(spl_image, mmc, sector, header);
Simon Glass5614db82015-06-23 15:38:47 -0600105 }
Tom Rini1530e8a2013-06-28 14:43:01 -0400106
Simon Schwarze21d2d82011-09-14 15:33:34 -0400107end:
Simon Glassb52c7fc2016-02-22 22:55:58 -0700108 if (ret) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100109#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300110 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100111#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200112 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200113 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200114
115 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400116}
117
Masahiro Yamada2a4a20c2017-06-22 16:52:36 +0900118static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200119{
120 switch (boot_device) {
121 case BOOT_DEVICE_MMC1:
122 return 0;
123 case BOOT_DEVICE_MMC2:
124 case BOOT_DEVICE_MMC2_2:
125 return 1;
126 }
127
128#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
129 printf("spl: unsupported mmc boot device.\n");
130#endif
131
132 return -ENODEV;
133}
134
Simon Glass4faf63b2015-12-02 10:59:12 -0700135static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200136{
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200137 int err, mmc_dev;
138
139 mmc_dev = spl_mmc_get_device_index(boot_device);
140 if (mmc_dev < 0)
141 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200142
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530143#if CONFIG_IS_ENABLED(DM_MMC)
144 err = mmc_init_device(mmc_dev);
145#else
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200146 err = mmc_initialize(NULL);
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530147#endif /* DM_MMC */
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200148 if (err) {
149#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
150 printf("spl: could not initialize mmc. error: %d\n", err);
151#endif
152 return err;
153 }
Simon Glass51ace322015-12-02 10:59:13 -0700154 *mmcp = find_mmc_device(mmc_dev);
155 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200156 if (err) {
157#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanf5920d02018-09-18 08:11:55 +0000158 printf("spl: could not find mmc device %d. error: %d\n",
159 mmc_dev, err);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200160#endif
161 return err;
162 }
163
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200164 return 0;
165}
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200166
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800167#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glassee306792016-09-24 18:20:13 -0600168static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500169 struct mmc *mmc, int partition,
170 unsigned long sector)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100171{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600172 struct disk_partition info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200173 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100174
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800175#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
176 int type_part;
177 /* Only support MBR so DOS_ENTRY_NUMBERS */
178 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
179 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
180 if (err)
181 continue;
182 if (info.sys_ind ==
183 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
184 partition = type_part;
185 break;
186 }
187 }
188#endif
189
Simon Glass55877f92016-07-05 17:10:18 -0600190 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200191 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100192#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200193 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100194#endif
195 return -1;
196 }
197
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200198#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500199 return mmc_load_image_raw_sector(spl_image, mmc, info.start + sector);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200200#else
Simon Glassee306792016-09-24 18:20:13 -0600201 return mmc_load_image_raw_sector(spl_image, mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200202#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100203}
204#endif
205
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000206#ifdef CONFIG_SPL_OS_BOOT
Simon Glassee306792016-09-24 18:20:13 -0600207static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
208 struct mmc *mmc)
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000209{
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530210 int ret;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200211
York Suncea88212018-06-26 09:44:39 -0700212#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
213 unsigned long count;
214
Simon Glass70061ba2016-06-12 23:30:32 -0600215 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200216 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
217 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
218 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
Tom Rini195bc4b2017-08-14 21:01:30 -0400219 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100220#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300221 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100222#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000223 return -1;
224 }
York Suncea88212018-06-26 09:44:39 -0700225#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000226
Simon Glassee306792016-09-24 18:20:13 -0600227 ret = mmc_load_image_raw_sector(spl_image, mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200228 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530229 if (ret)
230 return ret;
231
Simon Glassee306792016-09-24 18:20:13 -0600232 if (spl_image->os != IH_OS_LINUX) {
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530233 puts("Expected Linux image is not found. Trying to start U-boot\n");
234 return -ENOENT;
235 }
236
237 return 0;
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000238}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200239#else
240int spl_start_uboot(void)
241{
242 return 1;
243}
Simon Glassee306792016-09-24 18:20:13 -0600244static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
245 struct mmc *mmc)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200246{
247 return -ENOSYS;
248}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000249#endif
250
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200251#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500252static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
253 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200254{
255 int err = -ENOSYS;
256
Tien Fong Chee6fd0a712019-01-23 14:20:03 +0800257#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200258 if (!spl_start_uboot()) {
Simon Glass3eb382a2016-09-24 18:20:15 -0600259 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200260 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
261 if (!err)
262 return err;
263 }
264#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Simon Glass3eb382a2016-09-24 18:20:15 -0600265 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200266 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500267 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200268 if (!err)
269 return err;
270#endif
271#endif
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800272#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200273 if (!spl_start_uboot()) {
Michal Simek0ef6ec82016-12-01 11:09:01 +0100274 err = spl_load_image_ext_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
Michal Simek0ef6ec82016-12-01 11:09:01 +0100280 err = spl_load_image_ext(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
287
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800288#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200289 err = -ENOENT;
290#endif
291
292 return err;
293}
294#else
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500295static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
296 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200297{
298 return -ENOSYS;
299}
300#endif
301
Harald Seiler0bf7ab12020-04-15 11:33:30 +0200302u32 __weak spl_mmc_boot_mode(const u32 boot_device)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100303{
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800304#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100305 return MMCSD_MODE_FS;
306#elif defined(CONFIG_SUPPORT_EMMC_BOOT)
307 return MMCSD_MODE_EMMCBOOT;
308#else
309 return MMCSD_MODE_RAW;
310#endif
311}
312
Patrick Delaunay113aa502018-03-20 10:54:51 +0100313#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200314int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay113aa502018-03-20 10:54:51 +0100315{
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
Harald Seiler0bf7ab12020-04-15 11:33:30 +0200353 boot_mode = spl_mmc_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
Harald Seilerbf16c302020-04-15 11:33:31 +0200434 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500435#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);