blob: add2785b4e39d2bafb953b9757853ecc8f0caee6 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060011#include <part.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070012#include <spl.h>
Paul Kocialkowski91199f42015-04-27 10:20:22 +020013#include <linux/compiler.h>
Nikita Kiryanov36afd452015-11-08 17:11:49 +020014#include <errno.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040015#include <asm/u-boot.h>
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020016#include <errno.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040017#include <mmc.h>
Tom Rinie4c444b2013-06-28 14:43:01 -040018#include <image.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040019
Simon Glass2a2ee2a2016-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 Schwarz9ea5c6e2011-09-14 15:33:34 -040022{
Peter Korsgaard2e222102013-03-21 04:55:17 +000023 u32 image_size_sectors;
Simon Glass96debd12016-02-22 22:55:58 -070024 unsigned long count;
Marek Vasut7e0f2262016-04-29 00:44:54 +020025 int ret;
Tom Rinie4c444b2013-06-28 14:43:01 -040026
Simon Glass2a2ee2a2016-09-24 18:20:13 -060027 ret = spl_parse_image_header(spl_image, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +020028 if (ret)
29 return ret;
30
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040031 /* convert size to sectors - round up */
Simon Glass2a2ee2a2016-09-24 18:20:13 -060032 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
Paul Kocialkowski91199f42015-04-27 10:20:22 +020033 mmc->read_bl_len;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040034
35 /* Read the header too to avoid extra memcpy */
Simon Glassef5609c2016-05-14 14:03:06 -060036 count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060037 (void *)(ulong)spl_image->load_addr);
Andre Przywara11e14792017-01-02 11:48:31 +000038 debug("read %x sectors to %lx\n", image_size_sectors,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060039 spl_image->load_addr);
Simon Glass96debd12016-02-22 22:55:58 -070040 if (count != image_size_sectors)
41 return -EIO;
42
43 return 0;
44}
45
Simon Glass96debd12016-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 Glassef5609c2016-05-14 14:03:06 -060051 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
Simon Glass96debd12016-02-22 22:55:58 -070052}
Simon Glass96debd12016-02-22 22:55:58 -070053
Baruch Siache42afd82020-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 Kimb016b582017-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 Glass96debd12016-02-22 22:55:58 -070067{
68 unsigned long count;
69 struct image_header *header;
Marek Vasut04ce5422018-08-14 11:27:02 +020070 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glass96debd12016-02-22 22:55:58 -070071 int ret = 0;
72
Marek Vasut04ce5422018-08-14 11:27:02 +020073 header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
Simon Glass96debd12016-02-22 22:55:58 -070074
75 /* read image header to find the image size & load address */
Marek Vasut04ce5422018-08-14 11:27:02 +020076 count = blk_dread(bd, sector, 1, header);
Simon Glass96debd12016-02-22 22:55:58 -070077 debug("hdr read sector %lx, count=%lu\n", sector, count);
78 if (count == 0) {
79 ret = -EIO;
80 goto end;
81 }
82
Masahiro Yamada4976f482016-03-16 12:10:00 +090083 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
84 image_get_magic(header) == FDT_MAGIC) {
Simon Glass96debd12016-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 Vutlaeafd5412016-05-24 10:34:38 +053090 load.filename = NULL;
Simon Glass96debd12016-02-22 22:55:58 -070091 load.bl_len = mmc->read_bl_len;
92 load.read = h_spl_load_read;
Simon Glassf4d7d852016-09-24 18:20:16 -060093 ret = spl_load_simple_fit(spl_image, &load, sector, header);
Peng Fandd7d0912019-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 Yamada4976f482016-03-16 12:10:00 +0900104 } else {
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600105 ret = mmc_load_legacy(spl_image, mmc, sector, header);
Simon Glass96debd12016-02-22 22:55:58 -0700106 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400107
108end:
Simon Glass96debd12016-02-22 22:55:58 -0700109 if (ret) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100110#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam5bbf4092016-04-20 23:12:04 -0300111 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100112#endif
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200113 return -1;
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200114 }
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200115
116 return 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400117}
118
Masahiro Yamada207d8b32017-06-22 16:52:36 +0900119static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanova1e56cf2015-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 Glass99c7a512015-12-02 10:59:12 -0700136static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200137{
Nikita Kiryanova1e56cf2015-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 Kiryanov4188ba32015-11-08 17:11:44 +0200143
Lokesh Vutla80f02012019-09-09 14:40:36 +0530144#if CONFIG_IS_ENABLED(DM_MMC)
145 err = mmc_init_device(mmc_dev);
146#else
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200147 err = mmc_initialize(NULL);
Lokesh Vutla80f02012019-09-09 14:40:36 +0530148#endif /* DM_MMC */
Nikita Kiryanov4188ba32015-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 Glassb4857aa2015-12-02 10:59:13 -0700155 *mmcp = find_mmc_device(mmc_dev);
156 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200157 if (err) {
158#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanaa608b62018-09-18 08:11:55 +0000159 printf("spl: could not find mmc device %d. error: %d\n",
160 mmc_dev, err);
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200161#endif
162 return err;
163 }
164
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200165 return 0;
166}
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200167
Dalon Westergreen949123e2017-02-10 17:15:35 -0800168#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600169static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500170 struct mmc *mmc, int partition,
171 unsigned long sector)
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100172{
Simon Glass05289792020-05-10 11:39:57 -0600173 struct disk_partition info;
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200174 int err;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100175
Dalon Westergreenf0fb4fa2017-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 Glass1e2b3ef2016-07-05 17:10:18 -0600191 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200192 if (err) {
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100193#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200194 puts("spl: partition error\n");
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100195#endif
196 return -1;
197 }
198
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200199#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500200 return mmc_load_image_raw_sector(spl_image, mmc, info.start + sector);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200201#else
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600202 return mmc_load_image_raw_sector(spl_image, mmc, info.start);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200203#endif
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100204}
205#endif
206
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000207#ifdef CONFIG_SPL_OS_BOOT
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600208static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
209 struct mmc *mmc)
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000210{
Lokesh Vutla811906a2016-04-11 15:24:50 +0530211 int ret;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200212
York Sun7267fbf2018-06-26 09:44:39 -0700213#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
214 unsigned long count;
215
Simon Glass87bce4e2016-06-12 23:30:32 -0600216 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski3bc37b62015-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 Rini9585dd32017-08-14 21:01:30 -0400220 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100221#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam5bbf4092016-04-20 23:12:04 -0300222 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100223#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000224 return -1;
225 }
York Sun7267fbf2018-06-26 09:44:39 -0700226#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000227
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600228 ret = mmc_load_image_raw_sector(spl_image, mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200229 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla811906a2016-04-11 15:24:50 +0530230 if (ret)
231 return ret;
232
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600233 if (spl_image->os != IH_OS_LINUX) {
Lokesh Vutla811906a2016-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 Korsgaard2b75b0a2013-05-13 08:36:29 +0000239}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200240#else
241int spl_start_uboot(void)
242{
243 return 1;
244}
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600245static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
246 struct mmc *mmc)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200247{
248 return -ENOSYS;
249}
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000250#endif
251
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200252#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
Andreas Dannenberge1eb6ad2019-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 Kiryanovf52b7292015-11-08 17:11:47 +0200255{
256 int err = -ENOSYS;
257
Tien Fong Chee0c3a9ed2019-01-23 14:20:03 +0800258#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200259 if (!spl_start_uboot()) {
Simon Glass710e9ca2016-09-24 18:20:15 -0600260 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanovf52b7292015-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 Glass710e9ca2016-09-24 18:20:15 -0600266 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200267 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500268 filename);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200269 if (!err)
270 return err;
271#endif
272#endif
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800273#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200274 if (!spl_start_uboot()) {
Michal Simek1a925412016-12-01 11:09:01 +0100275 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanovf52b7292015-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 Simek1a925412016-12-01 11:09:01 +0100281 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200282 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500283 filename);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200284 if (!err)
285 return err;
286#endif
287#endif
288
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800289#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200290 err = -ENOENT;
291#endif
292
293 return err;
294}
295#else
Andreas Dannenberge1eb6ad2019-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 Kiryanovf52b7292015-11-08 17:11:47 +0200298{
299 return -ENOSYS;
300}
301#endif
302
Harald Seilere9759062020-04-15 11:33:30 +0200303u32 __weak spl_mmc_boot_mode(const u32 boot_device)
Lukasz Majewskid695d662018-02-03 08:29:52 +0100304{
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800305#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewskid695d662018-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 Delaunay35a66962018-03-20 10:54:51 +0100314#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +0200315int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay35a66962018-03-20 10:54:51 +0100316{
317 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
318}
319#endif
320
Faiz Abbascf008252020-02-26 13:44:35 +0530321unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
322 unsigned long raw_sect)
Peng Fan30ecc9c2019-09-23 10:18:40 +0800323{
Faiz Abbascf008252020-02-26 13:44:35 +0530324 return raw_sect;
Peng Fan30ecc9c2019-09-23 10:18:40 +0800325}
326
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500327int spl_mmc_load(struct spl_image_info *spl_image,
328 struct spl_boot_device *bootdev,
329 const char *filename,
330 int raw_part,
331 unsigned long raw_sect)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400332{
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500333 static struct mmc *mmc;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400334 u32 boot_mode;
Simon Glassdc3dedf2015-06-23 15:38:51 -0600335 int err = 0;
Baruch Siache42afd82020-01-15 09:08:08 +0200336 __maybe_unused int part = 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400337
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500338 /* Perform peripheral init only once */
339 if (!mmc) {
340 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
341 if (err)
342 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400343
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500344 err = mmc_init(mmc);
345 if (err) {
346 mmc = NULL;
Paul Burton8112f5f2013-09-04 16:12:24 +0100347#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500348 printf("spl: mmc init failed with error: %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100349#endif
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500350 return err;
351 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400352 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000353
Harald Seilere9759062020-04-15 11:33:30 +0200354 boot_mode = spl_mmc_boot_mode(bootdev->boot_device);
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200355 err = -EINVAL;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200356 switch (boot_mode) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200357 case MMCSD_MODE_EMMCBOOT:
Mans Rullgard17241ea2019-09-03 16:16:50 +0200358#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
359 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
360#else
Mans Rullgard92439902019-09-03 16:17:17 +0200361 /*
362 * We need to check what the partition is configured to.
363 * 1 and 2 match up to boot0 / boot1 and 7 is user data
364 * which is the first physical partition (0).
365 */
366 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200367
Mans Rullgard92439902019-09-03 16:17:17 +0200368 if (part == 7)
369 part = 0;
Mans Rullgard17241ea2019-09-03 16:16:50 +0200370#endif
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200371
Mans Rullgard92439902019-09-03 16:17:17 +0200372 if (CONFIG_IS_ENABLED(MMC_TINY))
373 err = mmc_switch_part(mmc, part);
374 else
375 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutb5b838f2016-12-01 02:06:33 +0100376
Mans Rullgard92439902019-09-03 16:17:17 +0200377 if (err) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200378#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgard92439902019-09-03 16:17:17 +0200379 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200380#endif
Mans Rullgard92439902019-09-03 16:17:17 +0200381 return err;
382 }
383 /* Fall through */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200384 case MMCSD_MODE_RAW:
385 debug("spl: mmc boot mode: raw\n");
386
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200387 if (!spl_start_uboot()) {
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600388 err = mmc_load_image_raw_os(spl_image, mmc);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200389 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200390 return err;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200391 }
Peng Fana335f802019-10-23 01:43:30 +0000392
Faiz Abbascf008252020-02-26 13:44:35 +0530393 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fana335f802019-10-23 01:43:30 +0000394
Dalon Westergreen949123e2017-02-10 17:15:35 -0800395#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500396 err = mmc_load_image_raw_partition(spl_image, mmc, raw_part,
397 raw_sect);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200398 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200399 return err;
Dalon Westergreen949123e2017-02-10 17:15:35 -0800400#endif
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200401#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Baruch Siache42afd82020-01-15 09:08:08 +0200402 err = mmc_load_image_raw_sector(spl_image, mmc,
403 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200404 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200405 return err;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200406#endif
Guillaume GARDET86a0df72016-02-18 18:17:36 +0100407 /* If RAW mode fails, try FS mode. */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200408 case MMCSD_MODE_FS:
409 debug("spl: mmc boot mode: fs\n");
410
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500411 err = spl_mmc_do_fs_boot(spl_image, mmc, filename);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200412 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200413 return err;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200414
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200415 break;
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100416#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200417 default:
418 puts("spl: mmc: wrong boot mode\n");
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100419#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200420 }
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200421
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200422 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400423}
Simon Glass0fed9c72016-09-24 18:20:00 -0600424
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500425int spl_mmc_load_image(struct spl_image_info *spl_image,
426 struct spl_boot_device *bootdev)
427{
428 return spl_mmc_load(spl_image, bootdev,
429#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
430 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
431#else
432 NULL,
433#endif
434#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +0200435 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500436#else
437 0,
438#endif
439#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
440 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
441#else
442 0);
443#endif
444}
445
Simon Glassebc4ef62016-11-30 15:30:50 -0700446SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
447SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
448SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);