blob: d550da2d97ec4939e989d5020aafee42b014ed6d [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
Pali Rohárdda8f882022-01-14 14:31:38 +010020static int mmc_load_legacy(struct spl_image_info *spl_image,
21 struct spl_boot_device *bootdev,
22 struct mmc *mmc,
Simon Glassee306792016-09-24 18:20:13 -060023 ulong sector, struct image_header *header)
Simon Glassb52c7fc2016-02-22 22:55:58 -070024{
Pali Rohár11041672021-07-23 11:14:27 +020025 u32 image_offset_sectors;
Simon Glassb52c7fc2016-02-22 22:55:58 -070026 u32 image_size_sectors;
27 unsigned long count;
Pali Rohár11041672021-07-23 11:14:27 +020028 u32 image_offset;
Marek Vasut02266e22016-04-29 00:44:54 +020029 int ret;
30
Pali Rohárdda8f882022-01-14 14:31:38 +010031 ret = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut02266e22016-04-29 00:44:54 +020032 if (ret)
33 return ret;
Simon Glassb52c7fc2016-02-22 22:55:58 -070034
Pali Rohár11041672021-07-23 11:14:27 +020035 /* convert offset to sectors - round down */
36 image_offset_sectors = spl_image->offset / mmc->read_bl_len;
37 /* calculate remaining offset */
38 image_offset = spl_image->offset % mmc->read_bl_len;
39
Simon Glassb52c7fc2016-02-22 22:55:58 -070040 /* convert size to sectors - round up */
Simon Glassee306792016-09-24 18:20:13 -060041 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
Simon Glassb52c7fc2016-02-22 22:55:58 -070042 mmc->read_bl_len;
43
44 /* Read the header too to avoid extra memcpy */
Pali Rohár11041672021-07-23 11:14:27 +020045 count = blk_dread(mmc_get_blk_desc(mmc),
46 sector + image_offset_sectors,
47 image_size_sectors,
Simon Glassee306792016-09-24 18:20:13 -060048 (void *)(ulong)spl_image->load_addr);
Andre Przywara6c526072017-01-02 11:48:31 +000049 debug("read %x sectors to %lx\n", image_size_sectors,
Simon Glassee306792016-09-24 18:20:13 -060050 spl_image->load_addr);
Simon Glassb52c7fc2016-02-22 22:55:58 -070051 if (count != image_size_sectors)
52 return -EIO;
53
Pali Rohár11041672021-07-23 11:14:27 +020054 if (image_offset)
55 memmove((void *)(ulong)spl_image->load_addr,
56 (void *)(ulong)spl_image->load_addr + image_offset,
57 spl_image->size);
58
Simon Glassb52c7fc2016-02-22 22:55:58 -070059 return 0;
60}
61
Simon Glassb52c7fc2016-02-22 22:55:58 -070062static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
63 ulong count, void *buf)
64{
65 struct mmc *mmc = load->dev;
66
Simon Glass670d3042016-05-14 14:03:06 -060067 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
Simon Glassb52c7fc2016-02-22 22:55:58 -070068}
Simon Glassb52c7fc2016-02-22 22:55:58 -070069
Baruch Siachfef81be2020-01-15 09:08:08 +020070static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
71{
72#if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
73 if (part == 0)
74 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
75#endif
76
77 return 0;
78}
79
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090080static __maybe_unused
81int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010082 struct spl_boot_device *bootdev,
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090083 struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040084{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020085 unsigned long count;
Peter Korsgaardf2471292013-03-21 04:55:17 +000086 struct image_header *header;
Marek Vasut1dda46f2018-08-14 11:27:02 +020087 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glassb52c7fc2016-02-22 22:55:58 -070088 int ret = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040089
Marek Vasut1dda46f2018-08-14 11:27:02 +020090 header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
Simon Schwarze21d2d82011-09-14 15:33:34 -040091
92 /* read image header to find the image size & load address */
Marek Vasut1dda46f2018-08-14 11:27:02 +020093 count = blk_dread(bd, sector, 1, header);
Simon Glassb52c7fc2016-02-22 22:55:58 -070094 debug("hdr read sector %lx, count=%lu\n", sector, count);
95 if (count == 0) {
96 ret = -EIO;
Simon Schwarze21d2d82011-09-14 15:33:34 -040097 goto end;
Simon Glassb52c7fc2016-02-22 22:55:58 -070098 }
Simon Schwarze21d2d82011-09-14 15:33:34 -040099
Masahiro Yamada802f1c32016-03-16 12:10:00 +0900100 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
101 image_get_magic(header) == FDT_MAGIC) {
Simon Glassb52c7fc2016-02-22 22:55:58 -0700102 struct spl_load_info load;
103
104 debug("Found FIT\n");
105 load.dev = mmc;
106 load.priv = NULL;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530107 load.filename = NULL;
Simon Glassb52c7fc2016-02-22 22:55:58 -0700108 load.bl_len = mmc->read_bl_len;
109 load.read = h_spl_load_read;
Simon Glass43a734f2016-09-24 18:20:16 -0600110 ret = spl_load_simple_fit(spl_image, &load, sector, header);
Peng Fand8670762019-08-22 07:42:38 +0000111 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
112 struct spl_load_info load;
113
114 load.dev = mmc;
115 load.priv = NULL;
116 load.filename = NULL;
117 load.bl_len = mmc->read_bl_len;
118 load.read = h_spl_load_read;
119
120 ret = spl_load_imx_container(spl_image, &load, sector);
Masahiro Yamada802f1c32016-03-16 12:10:00 +0900121 } else {
Pali Rohárdda8f882022-01-14 14:31:38 +0100122 ret = mmc_load_legacy(spl_image, bootdev, mmc, sector, header);
Simon Glass5614db82015-06-23 15:38:47 -0600123 }
Tom Rini1530e8a2013-06-28 14:43:01 -0400124
Simon Schwarze21d2d82011-09-14 15:33:34 -0400125end:
Simon Glassb52c7fc2016-02-22 22:55:58 -0700126 if (ret) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100127#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300128 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100129#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200130 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200131 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200132
133 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400134}
135
Masahiro Yamada2a4a20c2017-06-22 16:52:36 +0900136static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200137{
138 switch (boot_device) {
139 case BOOT_DEVICE_MMC1:
140 return 0;
141 case BOOT_DEVICE_MMC2:
142 case BOOT_DEVICE_MMC2_2:
143 return 1;
144 }
145
146#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
147 printf("spl: unsupported mmc boot device.\n");
148#endif
149
150 return -ENODEV;
151}
152
Simon Glass4faf63b2015-12-02 10:59:12 -0700153static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200154{
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200155 int err, mmc_dev;
156
157 mmc_dev = spl_mmc_get_device_index(boot_device);
158 if (mmc_dev < 0)
159 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200160
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530161#if CONFIG_IS_ENABLED(DM_MMC)
162 err = mmc_init_device(mmc_dev);
163#else
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200164 err = mmc_initialize(NULL);
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530165#endif /* DM_MMC */
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200166 if (err) {
167#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
168 printf("spl: could not initialize mmc. error: %d\n", err);
169#endif
170 return err;
171 }
Simon Glass51ace322015-12-02 10:59:13 -0700172 *mmcp = find_mmc_device(mmc_dev);
173 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200174 if (err) {
175#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanf5920d02018-09-18 08:11:55 +0000176 printf("spl: could not find mmc device %d. error: %d\n",
177 mmc_dev, err);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200178#endif
179 return err;
180 }
181
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200182 return 0;
183}
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200184
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800185#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glassee306792016-09-24 18:20:13 -0600186static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100187 struct spl_boot_device *bootdev,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500188 struct mmc *mmc, int partition,
189 unsigned long sector)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100190{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600191 struct disk_partition info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200192 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100193
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800194#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
195 int type_part;
196 /* Only support MBR so DOS_ENTRY_NUMBERS */
197 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
198 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
199 if (err)
200 continue;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200201 if (info.sys_ind ==
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800202 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
203 partition = type_part;
204 break;
205 }
206 }
207#endif
208
Simon Glass55877f92016-07-05 17:10:18 -0600209 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200210 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100211#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200212 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100213#endif
214 return -1;
215 }
216
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200217#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohárdda8f882022-01-14 14:31:38 +0100218 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200219#else
Pali Rohárdda8f882022-01-14 14:31:38 +0100220 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200221#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100222}
223#endif
224
Tom Rinic2e78882021-10-30 23:03:48 -0400225#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glassee306792016-09-24 18:20:13 -0600226static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100227 struct spl_boot_device *bootdev,
Simon Glassee306792016-09-24 18:20:13 -0600228 struct mmc *mmc)
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000229{
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530230 int ret;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200231
York Suncea88212018-06-26 09:44:39 -0700232#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
233 unsigned long count;
234
Simon Glass70061ba2016-06-12 23:30:32 -0600235 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200236 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
237 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
238 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
Tom Rini195bc4b2017-08-14 21:01:30 -0400239 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100240#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300241 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100242#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000243 return -1;
244 }
York Suncea88212018-06-26 09:44:39 -0700245#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000246
Pali Rohárdda8f882022-01-14 14:31:38 +0100247 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200248 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530249 if (ret)
250 return ret;
251
Alexandru Gagniuc36fd3ba2021-07-15 14:19:23 -0500252 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
253 puts("Expected image is not found. Trying to start U-boot\n");
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530254 return -ENOENT;
255 }
256
257 return 0;
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000258}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200259#else
260int spl_start_uboot(void)
261{
262 return 1;
263}
Simon Glassee306792016-09-24 18:20:13 -0600264static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100265 struct spl_boot_device *bootdev,
Simon Glassee306792016-09-24 18:20:13 -0600266 struct mmc *mmc)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200267{
268 return -ENOSYS;
269}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000270#endif
271
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200272#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
Pali Rohárdda8f882022-01-14 14:31:38 +0100273static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
274 struct spl_boot_device *bootdev,
275 struct mmc *mmc,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500276 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200277{
278 int err = -ENOSYS;
279
Tien Fong Chee6fd0a712019-01-23 14:20:03 +0800280#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200281 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100282 err = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200283 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
284 if (!err)
285 return err;
286 }
287#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohárdda8f882022-01-14 14:31:38 +0100288 err = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200289 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500290 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200291 if (!err)
292 return err;
293#endif
294#endif
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800295#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200296 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100297 err = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200298 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
299 if (!err)
300 return err;
301 }
302#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohárdda8f882022-01-14 14:31:38 +0100303 err = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200304 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500305 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200306 if (!err)
307 return err;
308#endif
309#endif
310
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800311#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200312 err = -ENOENT;
313#endif
314
315 return err;
316}
317#else
Pali Rohárdda8f882022-01-14 14:31:38 +0100318static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
319 struct spl_boot_device *bootdev,
320 struct mmc *mmc,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500321 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200322{
323 return -ENOSYS;
324}
325#endif
326
Harald Seiler0bf7ab12020-04-15 11:33:30 +0200327u32 __weak spl_mmc_boot_mode(const u32 boot_device)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100328{
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800329#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100330 return MMCSD_MODE_FS;
331#elif defined(CONFIG_SUPPORT_EMMC_BOOT)
332 return MMCSD_MODE_EMMCBOOT;
333#else
334 return MMCSD_MODE_RAW;
335#endif
336}
337
Patrick Delaunay113aa502018-03-20 10:54:51 +0100338#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200339int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay113aa502018-03-20 10:54:51 +0100340{
341 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
342}
343#endif
344
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530345unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
346 unsigned long raw_sect)
Peng Fan315cf592019-09-23 10:18:40 +0800347{
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530348 return raw_sect;
Peng Fan315cf592019-09-23 10:18:40 +0800349}
350
Marek Vasutde1ce232021-07-03 04:55:32 +0200351int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
352{
353 int part;
354#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
355 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
356#else
357 /*
358 * We need to check what the partition is configured to.
359 * 1 and 2 match up to boot0 / boot1 and 7 is user data
360 * which is the first physical partition (0).
361 */
362 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
363 if (part == 7)
364 part = 0;
365#endif
366 return part;
367}
368
369int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
370{
371 return default_spl_mmc_emmc_boot_partition(mmc);
372}
373
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500374int spl_mmc_load(struct spl_image_info *spl_image,
375 struct spl_boot_device *bootdev,
376 const char *filename,
377 int raw_part,
378 unsigned long raw_sect)
Simon Schwarze21d2d82011-09-14 15:33:34 -0400379{
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500380 static struct mmc *mmc;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400381 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600382 int err = 0;
Baruch Siachfef81be2020-01-15 09:08:08 +0200383 __maybe_unused int part = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400384
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500385 /* Perform peripheral init only once */
386 if (!mmc) {
387 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
388 if (err)
389 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400390
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500391 err = mmc_init(mmc);
392 if (err) {
393 mmc = NULL;
Paul Burtona8015fd2013-09-04 16:12:24 +0100394#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500395 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100396#endif
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500397 return err;
398 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400399 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000400
Harald Seiler0bf7ab12020-04-15 11:33:30 +0200401 boot_mode = spl_mmc_boot_mode(bootdev->boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200402 err = -EINVAL;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200403 switch (boot_mode) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200404 case MMCSD_MODE_EMMCBOOT:
Marek Vasutde1ce232021-07-03 04:55:32 +0200405 part = spl_mmc_emmc_boot_partition(mmc);
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200406
Mans Rullgardec046a52019-09-03 16:17:17 +0200407 if (CONFIG_IS_ENABLED(MMC_TINY))
408 err = mmc_switch_part(mmc, part);
409 else
410 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutf537e392016-12-01 02:06:33 +0100411
Mans Rullgardec046a52019-09-03 16:17:17 +0200412 if (err) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200413#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgardec046a52019-09-03 16:17:17 +0200414 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200415#endif
Mans Rullgardec046a52019-09-03 16:17:17 +0200416 return err;
417 }
418 /* Fall through */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200419 case MMCSD_MODE_RAW:
420 debug("spl: mmc boot mode: raw\n");
421
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200422 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100423 err = mmc_load_image_raw_os(spl_image, bootdev, mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200424 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200425 return err;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200426 }
Peng Fandc7d3222019-10-23 01:43:30 +0000427
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530428 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fandc7d3222019-10-23 01:43:30 +0000429
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800430#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Pali Rohárdda8f882022-01-14 14:31:38 +0100431 err = mmc_load_image_raw_partition(spl_image, bootdev,
432 mmc, raw_part,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500433 raw_sect);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200434 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200435 return err;
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800436#endif
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200437#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohárdda8f882022-01-14 14:31:38 +0100438 err = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Baruch Siachfef81be2020-01-15 09:08:08 +0200439 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200440 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200441 return err;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200442#endif
Guillaume GARDET834dd882016-02-18 18:17:36 +0100443 /* If RAW mode fails, try FS mode. */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200444 case MMCSD_MODE_FS:
445 debug("spl: mmc boot mode: fs\n");
446
Pali Rohárdda8f882022-01-14 14:31:38 +0100447 err = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200448 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200449 return err;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200450
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200451 break;
Paul Burtona8015fd2013-09-04 16:12:24 +0100452#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200453 default:
454 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100455#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200456 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200457
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200458 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400459}
Simon Glassd449cb82016-09-24 18:20:00 -0600460
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500461int spl_mmc_load_image(struct spl_image_info *spl_image,
462 struct spl_boot_device *bootdev)
463{
464 return spl_mmc_load(spl_image, bootdev,
465#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
466 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
467#else
468 NULL,
469#endif
470#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200471 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500472#else
473 0,
474#endif
475#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
476 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
477#else
478 0);
479#endif
480}
481
Simon Glass4fc1f252016-11-30 15:30:50 -0700482SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
483SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
484SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);