blob: fcdd3caeac76efa1d03fc74125aeacfb783b2ddf [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 */
8#include <common.h>
Simon Glassdc3dedf2015-06-23 15:38:51 -06009#include <dm.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070010#include <spl.h>
Paul Kocialkowski91199f42015-04-27 10:20:22 +020011#include <linux/compiler.h>
Nikita Kiryanov36afd452015-11-08 17:11:49 +020012#include <errno.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040013#include <asm/u-boot.h>
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020014#include <errno.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040015#include <mmc.h>
Tom Rinie4c444b2013-06-28 14:43:01 -040016#include <image.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040017
Simon Glass2a2ee2a2016-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 Schwarz9ea5c6e2011-09-14 15:33:34 -040020{
Peter Korsgaard2e222102013-03-21 04:55:17 +000021 u32 image_size_sectors;
Simon Glass96debd12016-02-22 22:55:58 -070022 unsigned long count;
Marek Vasut7e0f2262016-04-29 00:44:54 +020023 int ret;
Tom Rinie4c444b2013-06-28 14:43:01 -040024
Simon Glass2a2ee2a2016-09-24 18:20:13 -060025 ret = spl_parse_image_header(spl_image, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +020026 if (ret)
27 return ret;
28
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040029 /* convert size to sectors - round up */
Simon Glass2a2ee2a2016-09-24 18:20:13 -060030 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
Paul Kocialkowski91199f42015-04-27 10:20:22 +020031 mmc->read_bl_len;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040032
33 /* Read the header too to avoid extra memcpy */
Simon Glassef5609c2016-05-14 14:03:06 -060034 count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060035 (void *)(ulong)spl_image->load_addr);
Andre Przywara11e14792017-01-02 11:48:31 +000036 debug("read %x sectors to %lx\n", image_size_sectors,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060037 spl_image->load_addr);
Simon Glass96debd12016-02-22 22:55:58 -070038 if (count != image_size_sectors)
39 return -EIO;
40
41 return 0;
42}
43
Simon Glass96debd12016-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 Glassef5609c2016-05-14 14:03:06 -060049 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
Simon Glass96debd12016-02-22 22:55:58 -070050}
Simon Glass96debd12016-02-22 22:55:58 -070051
Baruch Siache42afd82020-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 Kimb016b582017-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 Glass96debd12016-02-22 22:55:58 -070065{
66 unsigned long count;
67 struct image_header *header;
Marek Vasut04ce5422018-08-14 11:27:02 +020068 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glass96debd12016-02-22 22:55:58 -070069 int ret = 0;
70
Marek Vasut04ce5422018-08-14 11:27:02 +020071 header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
Simon Glass96debd12016-02-22 22:55:58 -070072
73 /* read image header to find the image size & load address */
Marek Vasut04ce5422018-08-14 11:27:02 +020074 count = blk_dread(bd, sector, 1, header);
Simon Glass96debd12016-02-22 22:55:58 -070075 debug("hdr read sector %lx, count=%lu\n", sector, count);
76 if (count == 0) {
77 ret = -EIO;
78 goto end;
79 }
80
Masahiro Yamada4976f482016-03-16 12:10:00 +090081 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
82 image_get_magic(header) == FDT_MAGIC) {
Simon Glass96debd12016-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 Vutlaeafd5412016-05-24 10:34:38 +053088 load.filename = NULL;
Simon Glass96debd12016-02-22 22:55:58 -070089 load.bl_len = mmc->read_bl_len;
90 load.read = h_spl_load_read;
Simon Glassf4d7d852016-09-24 18:20:16 -060091 ret = spl_load_simple_fit(spl_image, &load, sector, header);
Peng Fandd7d0912019-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 Yamada4976f482016-03-16 12:10:00 +0900102 } else {
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600103 ret = mmc_load_legacy(spl_image, mmc, sector, header);
Simon Glass96debd12016-02-22 22:55:58 -0700104 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400105
106end:
Simon Glass96debd12016-02-22 22:55:58 -0700107 if (ret) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100108#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam5bbf4092016-04-20 23:12:04 -0300109 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100110#endif
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200111 return -1;
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200112 }
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200113
114 return 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400115}
116
Masahiro Yamada207d8b32017-06-22 16:52:36 +0900117static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanova1e56cf2015-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 Glass99c7a512015-12-02 10:59:12 -0700134static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200135{
Nikita Kiryanova1e56cf2015-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 Kiryanov4188ba32015-11-08 17:11:44 +0200141
Lokesh Vutla80f02012019-09-09 14:40:36 +0530142#if CONFIG_IS_ENABLED(DM_MMC)
143 err = mmc_init_device(mmc_dev);
144#else
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200145 err = mmc_initialize(NULL);
Lokesh Vutla80f02012019-09-09 14:40:36 +0530146#endif /* DM_MMC */
Nikita Kiryanov4188ba32015-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 Glassb4857aa2015-12-02 10:59:13 -0700153 *mmcp = find_mmc_device(mmc_dev);
154 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200155 if (err) {
156#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanaa608b62018-09-18 08:11:55 +0000157 printf("spl: could not find mmc device %d. error: %d\n",
158 mmc_dev, err);
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200159#endif
160 return err;
161 }
162
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200163 return 0;
164}
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200165
Dalon Westergreen949123e2017-02-10 17:15:35 -0800166#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600167static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500168 struct mmc *mmc, int partition,
169 unsigned long sector)
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100170{
Simon Glass05289792020-05-10 11:39:57 -0600171 struct disk_partition info;
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200172 int err;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100173
Dalon Westergreenf0fb4fa2017-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 Glass1e2b3ef2016-07-05 17:10:18 -0600189 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200190 if (err) {
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100191#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200192 puts("spl: partition error\n");
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100193#endif
194 return -1;
195 }
196
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200197#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500198 return mmc_load_image_raw_sector(spl_image, mmc, info.start + sector);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200199#else
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600200 return mmc_load_image_raw_sector(spl_image, mmc, info.start);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200201#endif
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100202}
203#endif
204
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000205#ifdef CONFIG_SPL_OS_BOOT
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600206static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
207 struct mmc *mmc)
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000208{
Lokesh Vutla811906a2016-04-11 15:24:50 +0530209 int ret;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200210
York Sun7267fbf2018-06-26 09:44:39 -0700211#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
212 unsigned long count;
213
Simon Glass87bce4e2016-06-12 23:30:32 -0600214 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski3bc37b62015-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 Rini9585dd32017-08-14 21:01:30 -0400218 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100219#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam5bbf4092016-04-20 23:12:04 -0300220 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100221#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000222 return -1;
223 }
York Sun7267fbf2018-06-26 09:44:39 -0700224#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000225
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600226 ret = mmc_load_image_raw_sector(spl_image, mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200227 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla811906a2016-04-11 15:24:50 +0530228 if (ret)
229 return ret;
230
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600231 if (spl_image->os != IH_OS_LINUX) {
Lokesh Vutla811906a2016-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 Korsgaard2b75b0a2013-05-13 08:36:29 +0000237}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200238#else
239int spl_start_uboot(void)
240{
241 return 1;
242}
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600243static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
244 struct mmc *mmc)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200245{
246 return -ENOSYS;
247}
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000248#endif
249
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200250#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
Andreas Dannenberge1eb6ad2019-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 Kiryanovf52b7292015-11-08 17:11:47 +0200253{
254 int err = -ENOSYS;
255
Tien Fong Chee0c3a9ed2019-01-23 14:20:03 +0800256#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200257 if (!spl_start_uboot()) {
Simon Glass710e9ca2016-09-24 18:20:15 -0600258 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanovf52b7292015-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 Glass710e9ca2016-09-24 18:20:15 -0600264 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200265 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500266 filename);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200267 if (!err)
268 return err;
269#endif
270#endif
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800271#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200272 if (!spl_start_uboot()) {
Michal Simek1a925412016-12-01 11:09:01 +0100273 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanovf52b7292015-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 Simek1a925412016-12-01 11:09:01 +0100279 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200280 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500281 filename);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200282 if (!err)
283 return err;
284#endif
285#endif
286
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800287#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200288 err = -ENOENT;
289#endif
290
291 return err;
292}
293#else
Andreas Dannenberge1eb6ad2019-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 Kiryanovf52b7292015-11-08 17:11:47 +0200296{
297 return -ENOSYS;
298}
299#endif
300
Harald Seilere9759062020-04-15 11:33:30 +0200301u32 __weak spl_mmc_boot_mode(const u32 boot_device)
Lukasz Majewskid695d662018-02-03 08:29:52 +0100302{
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800303#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewskid695d662018-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 Delaunay35a66962018-03-20 10:54:51 +0100312#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +0200313int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay35a66962018-03-20 10:54:51 +0100314{
315 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
316}
317#endif
318
Faiz Abbascf008252020-02-26 13:44:35 +0530319unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
320 unsigned long raw_sect)
Peng Fan30ecc9c2019-09-23 10:18:40 +0800321{
Faiz Abbascf008252020-02-26 13:44:35 +0530322 return raw_sect;
Peng Fan30ecc9c2019-09-23 10:18:40 +0800323}
324
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500325int spl_mmc_load(struct spl_image_info *spl_image,
326 struct spl_boot_device *bootdev,
327 const char *filename,
328 int raw_part,
329 unsigned long raw_sect)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400330{
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500331 static struct mmc *mmc;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400332 u32 boot_mode;
Simon Glassdc3dedf2015-06-23 15:38:51 -0600333 int err = 0;
Baruch Siache42afd82020-01-15 09:08:08 +0200334 __maybe_unused int part = 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400335
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500336 /* Perform peripheral init only once */
337 if (!mmc) {
338 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
339 if (err)
340 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400341
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500342 err = mmc_init(mmc);
343 if (err) {
344 mmc = NULL;
Paul Burton8112f5f2013-09-04 16:12:24 +0100345#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500346 printf("spl: mmc init failed with error: %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100347#endif
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500348 return err;
349 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400350 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000351
Harald Seilere9759062020-04-15 11:33:30 +0200352 boot_mode = spl_mmc_boot_mode(bootdev->boot_device);
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200353 err = -EINVAL;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200354 switch (boot_mode) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200355 case MMCSD_MODE_EMMCBOOT:
Mans Rullgard17241ea2019-09-03 16:16:50 +0200356#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
357 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
358#else
Mans Rullgard92439902019-09-03 16:17:17 +0200359 /*
360 * We need to check what the partition is configured to.
361 * 1 and 2 match up to boot0 / boot1 and 7 is user data
362 * which is the first physical partition (0).
363 */
364 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200365
Mans Rullgard92439902019-09-03 16:17:17 +0200366 if (part == 7)
367 part = 0;
Mans Rullgard17241ea2019-09-03 16:16:50 +0200368#endif
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200369
Mans Rullgard92439902019-09-03 16:17:17 +0200370 if (CONFIG_IS_ENABLED(MMC_TINY))
371 err = mmc_switch_part(mmc, part);
372 else
373 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutb5b838f2016-12-01 02:06:33 +0100374
Mans Rullgard92439902019-09-03 16:17:17 +0200375 if (err) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200376#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgard92439902019-09-03 16:17:17 +0200377 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200378#endif
Mans Rullgard92439902019-09-03 16:17:17 +0200379 return err;
380 }
381 /* Fall through */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200382 case MMCSD_MODE_RAW:
383 debug("spl: mmc boot mode: raw\n");
384
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200385 if (!spl_start_uboot()) {
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600386 err = mmc_load_image_raw_os(spl_image, mmc);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200387 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200388 return err;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200389 }
Peng Fana335f802019-10-23 01:43:30 +0000390
Faiz Abbascf008252020-02-26 13:44:35 +0530391 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fana335f802019-10-23 01:43:30 +0000392
Dalon Westergreen949123e2017-02-10 17:15:35 -0800393#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500394 err = mmc_load_image_raw_partition(spl_image, mmc, raw_part,
395 raw_sect);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200396 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200397 return err;
Dalon Westergreen949123e2017-02-10 17:15:35 -0800398#endif
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200399#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Baruch Siache42afd82020-01-15 09:08:08 +0200400 err = mmc_load_image_raw_sector(spl_image, mmc,
401 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200402 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200403 return err;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200404#endif
Guillaume GARDET86a0df72016-02-18 18:17:36 +0100405 /* If RAW mode fails, try FS mode. */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200406 case MMCSD_MODE_FS:
407 debug("spl: mmc boot mode: fs\n");
408
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500409 err = spl_mmc_do_fs_boot(spl_image, mmc, filename);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200410 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200411 return err;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200412
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200413 break;
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100414#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200415 default:
416 puts("spl: mmc: wrong boot mode\n");
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100417#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200418 }
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200419
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200420 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400421}
Simon Glass0fed9c72016-09-24 18:20:00 -0600422
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500423int spl_mmc_load_image(struct spl_image_info *spl_image,
424 struct spl_boot_device *bootdev)
425{
426 return spl_mmc_load(spl_image, bootdev,
427#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
428 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
429#else
430 NULL,
431#endif
432#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +0200433 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500434#else
435 0,
436#endif
437#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
438 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
439#else
440 0);
441#endif
442}
443
Simon Glassebc4ef62016-11-30 15:30:50 -0700444SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
445SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
446SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);