blob: 2377d0937d1d731774deeab2fc04f73f35119a0e [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{
23 u32 image_size_sectors;
24 unsigned long count;
Marek Vasut02266e22016-04-29 00:44:54 +020025 int ret;
26
Simon Glassee306792016-09-24 18:20:13 -060027 ret = spl_parse_image_header(spl_image, header);
Marek Vasut02266e22016-04-29 00:44:54 +020028 if (ret)
29 return ret;
Simon Glassb52c7fc2016-02-22 22:55:58 -070030
Simon Glassb52c7fc2016-02-22 22:55:58 -070031 /* convert size to sectors - round up */
Simon Glassee306792016-09-24 18:20:13 -060032 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
Simon Glassb52c7fc2016-02-22 22:55:58 -070033 mmc->read_bl_len;
34
35 /* Read the header too to avoid extra memcpy */
Simon Glass670d3042016-05-14 14:03:06 -060036 count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
Simon Glassee306792016-09-24 18:20:13 -060037 (void *)(ulong)spl_image->load_addr);
Andre Przywara6c526072017-01-02 11:48:31 +000038 debug("read %x sectors to %lx\n", image_size_sectors,
Simon Glassee306792016-09-24 18:20:13 -060039 spl_image->load_addr);
Simon Glassb52c7fc2016-02-22 22:55:58 -070040 if (count != image_size_sectors)
41 return -EIO;
42
43 return 0;
44}
45
Simon Glassb52c7fc2016-02-22 22:55:58 -070046static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
47 ulong count, void *buf)
48{
49 struct mmc *mmc = load->dev;
50
Simon Glass670d3042016-05-14 14:03:06 -060051 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
Simon Glassb52c7fc2016-02-22 22:55:58 -070052}
Simon Glassb52c7fc2016-02-22 22:55:58 -070053
Baruch Siachfef81be2020-01-15 09:08:08 +020054static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
55{
56#if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
57 if (part == 0)
58 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
59#endif
60
61 return 0;
62}
63
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090064static __maybe_unused
65int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
66 struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040067{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020068 unsigned long count;
Peter Korsgaardf2471292013-03-21 04:55:17 +000069 struct image_header *header;
Marek Vasut1dda46f2018-08-14 11:27:02 +020070 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glassb52c7fc2016-02-22 22:55:58 -070071 int ret = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040072
Marek Vasut1dda46f2018-08-14 11:27:02 +020073 header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
Simon Schwarze21d2d82011-09-14 15:33:34 -040074
75 /* read image header to find the image size & load address */
Marek Vasut1dda46f2018-08-14 11:27:02 +020076 count = blk_dread(bd, sector, 1, header);
Simon Glassb52c7fc2016-02-22 22:55:58 -070077 debug("hdr read sector %lx, count=%lu\n", sector, count);
78 if (count == 0) {
79 ret = -EIO;
Simon Schwarze21d2d82011-09-14 15:33:34 -040080 goto end;
Simon Glassb52c7fc2016-02-22 22:55:58 -070081 }
Simon Schwarze21d2d82011-09-14 15:33:34 -040082
Masahiro Yamada802f1c32016-03-16 12:10:00 +090083 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
84 image_get_magic(header) == FDT_MAGIC) {
Simon Glassb52c7fc2016-02-22 22:55:58 -070085 struct spl_load_info load;
86
87 debug("Found FIT\n");
88 load.dev = mmc;
89 load.priv = NULL;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +053090 load.filename = NULL;
Simon Glassb52c7fc2016-02-22 22:55:58 -070091 load.bl_len = mmc->read_bl_len;
92 load.read = h_spl_load_read;
Simon Glass43a734f2016-09-24 18:20:16 -060093 ret = spl_load_simple_fit(spl_image, &load, sector, header);
Peng Fand8670762019-08-22 07:42:38 +000094 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
95 struct spl_load_info load;
96
97 load.dev = mmc;
98 load.priv = NULL;
99 load.filename = NULL;
100 load.bl_len = mmc->read_bl_len;
101 load.read = h_spl_load_read;
102
103 ret = spl_load_imx_container(spl_image, &load, sector);
Masahiro Yamada802f1c32016-03-16 12:10:00 +0900104 } else {
Simon Glassee306792016-09-24 18:20:13 -0600105 ret = mmc_load_legacy(spl_image, mmc, sector, header);
Simon Glass5614db82015-06-23 15:38:47 -0600106 }
Tom Rini1530e8a2013-06-28 14:43:01 -0400107
Simon Schwarze21d2d82011-09-14 15:33:34 -0400108end:
Simon Glassb52c7fc2016-02-22 22:55:58 -0700109 if (ret) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100110#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300111 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100112#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200113 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200114 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200115
116 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400117}
118
Masahiro Yamada2a4a20c2017-06-22 16:52:36 +0900119static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200120{
121 switch (boot_device) {
122 case BOOT_DEVICE_MMC1:
123 return 0;
124 case BOOT_DEVICE_MMC2:
125 case BOOT_DEVICE_MMC2_2:
126 return 1;
127 }
128
129#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
130 printf("spl: unsupported mmc boot device.\n");
131#endif
132
133 return -ENODEV;
134}
135
Simon Glass4faf63b2015-12-02 10:59:12 -0700136static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200137{
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200138 int err, mmc_dev;
139
140 mmc_dev = spl_mmc_get_device_index(boot_device);
141 if (mmc_dev < 0)
142 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200143
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530144#if CONFIG_IS_ENABLED(DM_MMC)
145 err = mmc_init_device(mmc_dev);
146#else
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200147 err = mmc_initialize(NULL);
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530148#endif /* DM_MMC */
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200149 if (err) {
150#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
151 printf("spl: could not initialize mmc. error: %d\n", err);
152#endif
153 return err;
154 }
Simon Glass51ace322015-12-02 10:59:13 -0700155 *mmcp = find_mmc_device(mmc_dev);
156 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200157 if (err) {
158#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanf5920d02018-09-18 08:11:55 +0000159 printf("spl: could not find mmc device %d. error: %d\n",
160 mmc_dev, err);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200161#endif
162 return err;
163 }
164
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200165 return 0;
166}
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200167
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800168#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glassee306792016-09-24 18:20:13 -0600169static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500170 struct mmc *mmc, int partition,
171 unsigned long sector)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100172{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600173 struct disk_partition info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200174 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100175
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800176#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
177 int type_part;
178 /* Only support MBR so DOS_ENTRY_NUMBERS */
179 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
180 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
181 if (err)
182 continue;
183 if (info.sys_ind ==
184 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
185 partition = type_part;
186 break;
187 }
188 }
189#endif
190
Simon Glass55877f92016-07-05 17:10:18 -0600191 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200192 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100193#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200194 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100195#endif
196 return -1;
197 }
198
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200199#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500200 return mmc_load_image_raw_sector(spl_image, mmc, info.start + sector);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200201#else
Simon Glassee306792016-09-24 18:20:13 -0600202 return mmc_load_image_raw_sector(spl_image, mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200203#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100204}
205#endif
206
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000207#ifdef CONFIG_SPL_OS_BOOT
Simon Glassee306792016-09-24 18:20:13 -0600208static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
209 struct mmc *mmc)
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000210{
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530211 int ret;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200212
York Suncea88212018-06-26 09:44:39 -0700213#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
214 unsigned long count;
215
Simon Glass70061ba2016-06-12 23:30:32 -0600216 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200217 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
218 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
219 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
Tom Rini195bc4b2017-08-14 21:01:30 -0400220 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100221#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300222 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100223#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000224 return -1;
225 }
York Suncea88212018-06-26 09:44:39 -0700226#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000227
Simon Glassee306792016-09-24 18:20:13 -0600228 ret = mmc_load_image_raw_sector(spl_image, mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200229 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530230 if (ret)
231 return ret;
232
Simon Glassee306792016-09-24 18:20:13 -0600233 if (spl_image->os != IH_OS_LINUX) {
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530234 puts("Expected Linux image is not found. Trying to start U-boot\n");
235 return -ENOENT;
236 }
237
238 return 0;
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000239}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200240#else
241int spl_start_uboot(void)
242{
243 return 1;
244}
Simon Glassee306792016-09-24 18:20:13 -0600245static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
246 struct mmc *mmc)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200247{
248 return -ENOSYS;
249}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000250#endif
251
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200252#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500253static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
254 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200255{
256 int err = -ENOSYS;
257
Tien Fong Chee6fd0a712019-01-23 14:20:03 +0800258#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200259 if (!spl_start_uboot()) {
Simon Glass3eb382a2016-09-24 18:20:15 -0600260 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200261 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
262 if (!err)
263 return err;
264 }
265#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Simon Glass3eb382a2016-09-24 18:20:15 -0600266 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200267 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500268 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200269 if (!err)
270 return err;
271#endif
272#endif
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800273#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200274 if (!spl_start_uboot()) {
Michal Simek0ef6ec82016-12-01 11:09:01 +0100275 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200276 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
277 if (!err)
278 return err;
279 }
280#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Michal Simek0ef6ec82016-12-01 11:09:01 +0100281 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200282 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500283 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200284 if (!err)
285 return err;
286#endif
287#endif
288
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800289#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200290 err = -ENOENT;
291#endif
292
293 return err;
294}
295#else
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500296static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
297 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200298{
299 return -ENOSYS;
300}
301#endif
302
Harald Seiler0bf7ab12020-04-15 11:33:30 +0200303u32 __weak spl_mmc_boot_mode(const u32 boot_device)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100304{
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800305#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100306 return MMCSD_MODE_FS;
307#elif defined(CONFIG_SUPPORT_EMMC_BOOT)
308 return MMCSD_MODE_EMMCBOOT;
309#else
310 return MMCSD_MODE_RAW;
311#endif
312}
313
Patrick Delaunay113aa502018-03-20 10:54:51 +0100314#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200315int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay113aa502018-03-20 10:54:51 +0100316{
317 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
318}
319#endif
320
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530321unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
322 unsigned long raw_sect)
Peng Fan315cf592019-09-23 10:18:40 +0800323{
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530324 return raw_sect;
Peng Fan315cf592019-09-23 10:18:40 +0800325}
326
Marek Vasutde1ce232021-07-03 04:55:32 +0200327int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
328{
329 int part;
330#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
331 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
332#else
333 /*
334 * We need to check what the partition is configured to.
335 * 1 and 2 match up to boot0 / boot1 and 7 is user data
336 * which is the first physical partition (0).
337 */
338 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
339 if (part == 7)
340 part = 0;
341#endif
342 return part;
343}
344
345int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
346{
347 return default_spl_mmc_emmc_boot_partition(mmc);
348}
349
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500350int spl_mmc_load(struct spl_image_info *spl_image,
351 struct spl_boot_device *bootdev,
352 const char *filename,
353 int raw_part,
354 unsigned long raw_sect)
Simon Schwarze21d2d82011-09-14 15:33:34 -0400355{
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500356 static struct mmc *mmc;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400357 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600358 int err = 0;
Baruch Siachfef81be2020-01-15 09:08:08 +0200359 __maybe_unused int part = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400360
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500361 /* Perform peripheral init only once */
362 if (!mmc) {
363 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
364 if (err)
365 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400366
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500367 err = mmc_init(mmc);
368 if (err) {
369 mmc = NULL;
Paul Burtona8015fd2013-09-04 16:12:24 +0100370#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500371 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100372#endif
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500373 return err;
374 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400375 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000376
Harald Seiler0bf7ab12020-04-15 11:33:30 +0200377 boot_mode = spl_mmc_boot_mode(bootdev->boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200378 err = -EINVAL;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200379 switch (boot_mode) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200380 case MMCSD_MODE_EMMCBOOT:
Marek Vasutde1ce232021-07-03 04:55:32 +0200381 part = spl_mmc_emmc_boot_partition(mmc);
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200382
Mans Rullgardec046a52019-09-03 16:17:17 +0200383 if (CONFIG_IS_ENABLED(MMC_TINY))
384 err = mmc_switch_part(mmc, part);
385 else
386 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutf537e392016-12-01 02:06:33 +0100387
Mans Rullgardec046a52019-09-03 16:17:17 +0200388 if (err) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200389#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgardec046a52019-09-03 16:17:17 +0200390 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200391#endif
Mans Rullgardec046a52019-09-03 16:17:17 +0200392 return err;
393 }
394 /* Fall through */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200395 case MMCSD_MODE_RAW:
396 debug("spl: mmc boot mode: raw\n");
397
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200398 if (!spl_start_uboot()) {
Simon Glassee306792016-09-24 18:20:13 -0600399 err = mmc_load_image_raw_os(spl_image, mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200400 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200401 return err;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200402 }
Peng Fandc7d3222019-10-23 01:43:30 +0000403
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530404 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fandc7d3222019-10-23 01:43:30 +0000405
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800406#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500407 err = mmc_load_image_raw_partition(spl_image, mmc, raw_part,
408 raw_sect);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200409 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200410 return err;
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800411#endif
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200412#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Baruch Siachfef81be2020-01-15 09:08:08 +0200413 err = mmc_load_image_raw_sector(spl_image, mmc,
414 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200415 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200416 return err;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200417#endif
Guillaume GARDET834dd882016-02-18 18:17:36 +0100418 /* If RAW mode fails, try FS mode. */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200419 case MMCSD_MODE_FS:
420 debug("spl: mmc boot mode: fs\n");
421
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500422 err = spl_mmc_do_fs_boot(spl_image, mmc, filename);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200423 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200424 return err;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200425
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200426 break;
Paul Burtona8015fd2013-09-04 16:12:24 +0100427#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200428 default:
429 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100430#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200431 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200432
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200433 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400434}
Simon Glassd449cb82016-09-24 18:20:00 -0600435
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500436int spl_mmc_load_image(struct spl_image_info *spl_image,
437 struct spl_boot_device *bootdev)
438{
439 return spl_mmc_load(spl_image, bootdev,
440#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
441 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
442#else
443 NULL,
444#endif
445#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200446 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500447#else
448 0,
449#endif
450#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
451 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
452#else
453 0);
454#endif
455}
456
Simon Glass4fc1f252016-11-30 15:30:50 -0700457SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
458SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
459SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);