blob: 82689da140140956f187b70592a9ffc6d208d801 [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>
Sean Anderson5ff77722023-10-14 16:47:55 -040011#include <mapmem.h>
Simon Glass655306c2020-05-10 11:39:58 -060012#include <part.h>
Tom Rini28591df2012-08-13 12:03:19 -070013#include <spl.h>
Paul Kocialkowski0d139722015-04-27 10:20:22 +020014#include <linux/compiler.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020015#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040016#include <asm/u-boot.h>
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020017#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040018#include <mmc.h>
Tom Rini1530e8a2013-06-28 14:43:01 -040019#include <image.h>
Sean Anderson952ed672023-10-14 16:47:44 -040020#include <imx_container.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040021
Pali Rohárdda8f882022-01-14 14:31:38 +010022static int mmc_load_legacy(struct spl_image_info *spl_image,
23 struct spl_boot_device *bootdev,
24 struct mmc *mmc,
Simon Glassbb7d3bb2022-09-06 20:26:52 -060025 ulong sector, struct legacy_img_hdr *header)
Simon Glassb52c7fc2016-02-22 22:55:58 -070026{
Pali Rohár11041672021-07-23 11:14:27 +020027 u32 image_offset_sectors;
Simon Glassb52c7fc2016-02-22 22:55:58 -070028 u32 image_size_sectors;
29 unsigned long count;
Pali Rohár11041672021-07-23 11:14:27 +020030 u32 image_offset;
Marek Vasut02266e22016-04-29 00:44:54 +020031 int ret;
32
Pali Rohárdda8f882022-01-14 14:31:38 +010033 ret = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut02266e22016-04-29 00:44:54 +020034 if (ret)
35 return ret;
Simon Glassb52c7fc2016-02-22 22:55:58 -070036
Pali Rohár11041672021-07-23 11:14:27 +020037 /* convert offset to sectors - round down */
38 image_offset_sectors = spl_image->offset / mmc->read_bl_len;
39 /* calculate remaining offset */
40 image_offset = spl_image->offset % mmc->read_bl_len;
41
Simon Glassb52c7fc2016-02-22 22:55:58 -070042 /* convert size to sectors - round up */
Simon Glassee306792016-09-24 18:20:13 -060043 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
Simon Glassb52c7fc2016-02-22 22:55:58 -070044 mmc->read_bl_len;
45
46 /* Read the header too to avoid extra memcpy */
Pali Rohár11041672021-07-23 11:14:27 +020047 count = blk_dread(mmc_get_blk_desc(mmc),
48 sector + image_offset_sectors,
49 image_size_sectors,
Sean Anderson5ff77722023-10-14 16:47:55 -040050 map_sysmem(spl_image->load_addr,
51 image_size_sectors * mmc->read_bl_len));
Andre Przywara6c526072017-01-02 11:48:31 +000052 debug("read %x sectors to %lx\n", image_size_sectors,
Simon Glassee306792016-09-24 18:20:13 -060053 spl_image->load_addr);
Simon Glassb52c7fc2016-02-22 22:55:58 -070054 if (count != image_size_sectors)
55 return -EIO;
56
Pali Rohár11041672021-07-23 11:14:27 +020057 if (image_offset)
58 memmove((void *)(ulong)spl_image->load_addr,
59 (void *)(ulong)spl_image->load_addr + image_offset,
60 spl_image->size);
61
Simon Glassb52c7fc2016-02-22 22:55:58 -070062 return 0;
63}
64
Simon Glassb52c7fc2016-02-22 22:55:58 -070065static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
66 ulong count, void *buf)
67{
68 struct mmc *mmc = load->dev;
69
Simon Glass670d3042016-05-14 14:03:06 -060070 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
Simon Glassb52c7fc2016-02-22 22:55:58 -070071}
Simon Glassb52c7fc2016-02-22 22:55:58 -070072
Baruch Siachfef81be2020-01-15 09:08:08 +020073static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
74{
75#if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
76 if (part == 0)
77 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
78#endif
79
80 return 0;
81}
82
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090083static __maybe_unused
84int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010085 struct spl_boot_device *bootdev,
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090086 struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040087{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020088 unsigned long count;
Simon Glassbb7d3bb2022-09-06 20:26:52 -060089 struct legacy_img_hdr *header;
Marek Vasut1dda46f2018-08-14 11:27:02 +020090 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glassb52c7fc2016-02-22 22:55:58 -070091 int ret = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040092
Marek Vasut1dda46f2018-08-14 11:27:02 +020093 header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
Simon Schwarze21d2d82011-09-14 15:33:34 -040094
95 /* read image header to find the image size & load address */
Marek Vasut1dda46f2018-08-14 11:27:02 +020096 count = blk_dread(bd, sector, 1, header);
Simon Glassb52c7fc2016-02-22 22:55:58 -070097 debug("hdr read sector %lx, count=%lu\n", sector, count);
98 if (count == 0) {
99 ret = -EIO;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400100 goto end;
Simon Glassb52c7fc2016-02-22 22:55:58 -0700101 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400102
Masahiro Yamada802f1c32016-03-16 12:10:00 +0900103 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
104 image_get_magic(header) == FDT_MAGIC) {
Simon Glassb52c7fc2016-02-22 22:55:58 -0700105 struct spl_load_info load;
106
107 debug("Found FIT\n");
108 load.dev = mmc;
109 load.priv = NULL;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530110 load.filename = NULL;
Simon Glassb52c7fc2016-02-22 22:55:58 -0700111 load.bl_len = mmc->read_bl_len;
112 load.read = h_spl_load_read;
Simon Glass43a734f2016-09-24 18:20:16 -0600113 ret = spl_load_simple_fit(spl_image, &load, sector, header);
Sean Anderson952ed672023-10-14 16:47:44 -0400114 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
115 valid_container_hdr((void *)header)) {
Peng Fand8670762019-08-22 07:42:38 +0000116 struct spl_load_info load;
117
118 load.dev = mmc;
119 load.priv = NULL;
120 load.filename = NULL;
121 load.bl_len = mmc->read_bl_len;
122 load.read = h_spl_load_read;
123
124 ret = spl_load_imx_container(spl_image, &load, sector);
Masahiro Yamada802f1c32016-03-16 12:10:00 +0900125 } else {
Pali Rohárdda8f882022-01-14 14:31:38 +0100126 ret = mmc_load_legacy(spl_image, bootdev, mmc, sector, header);
Simon Glass5614db82015-06-23 15:38:47 -0600127 }
Tom Rini1530e8a2013-06-28 14:43:01 -0400128
Simon Schwarze21d2d82011-09-14 15:33:34 -0400129end:
Simon Glassb52c7fc2016-02-22 22:55:58 -0700130 if (ret) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100131#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300132 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100133#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200134 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200135 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200136
137 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400138}
139
Masahiro Yamada2a4a20c2017-06-22 16:52:36 +0900140static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200141{
142 switch (boot_device) {
143 case BOOT_DEVICE_MMC1:
144 return 0;
145 case BOOT_DEVICE_MMC2:
146 case BOOT_DEVICE_MMC2_2:
147 return 1;
148 }
149
150#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
151 printf("spl: unsupported mmc boot device.\n");
152#endif
153
154 return -ENODEV;
155}
156
Simon Glass4faf63b2015-12-02 10:59:12 -0700157static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200158{
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200159 int err, mmc_dev;
160
161 mmc_dev = spl_mmc_get_device_index(boot_device);
162 if (mmc_dev < 0)
163 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200164
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530165#if CONFIG_IS_ENABLED(DM_MMC)
166 err = mmc_init_device(mmc_dev);
167#else
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200168 err = mmc_initialize(NULL);
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530169#endif /* DM_MMC */
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200170 if (err) {
171#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
172 printf("spl: could not initialize mmc. error: %d\n", err);
173#endif
174 return err;
175 }
Simon Glass51ace322015-12-02 10:59:13 -0700176 *mmcp = find_mmc_device(mmc_dev);
177 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200178 if (err) {
179#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanf5920d02018-09-18 08:11:55 +0000180 printf("spl: could not find mmc device %d. error: %d\n",
181 mmc_dev, err);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200182#endif
183 return err;
184 }
185
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200186 return 0;
187}
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200188
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800189#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glassee306792016-09-24 18:20:13 -0600190static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100191 struct spl_boot_device *bootdev,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500192 struct mmc *mmc, int partition,
193 unsigned long sector)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100194{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600195 struct disk_partition info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200196 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100197
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800198#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
199 int type_part;
200 /* Only support MBR so DOS_ENTRY_NUMBERS */
201 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
202 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
203 if (err)
204 continue;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200205 if (info.sys_ind ==
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800206 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
207 partition = type_part;
208 break;
209 }
210 }
211#endif
212
Simon Glass55877f92016-07-05 17:10:18 -0600213 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200214 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100215#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200216 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100217#endif
218 return -1;
219 }
220
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200221#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohárdda8f882022-01-14 14:31:38 +0100222 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200223#else
Pali Rohárdda8f882022-01-14 14:31:38 +0100224 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200225#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100226}
227#endif
228
Alexandru Gagniuc99426c82021-12-30 10:39:59 -0600229#if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
Simon Glassee306792016-09-24 18:20:13 -0600230static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100231 struct spl_boot_device *bootdev,
Simon Glassee306792016-09-24 18:20:13 -0600232 struct mmc *mmc)
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000233{
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530234 int ret;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200235
Elena Popa6532fab2023-08-08 16:42:15 +0300236#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
York Suncea88212018-06-26 09:44:39 -0700237 unsigned long count;
238
Simon Glass70061ba2016-06-12 23:30:32 -0600239 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200240 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
241 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
Simon Glass259cdb42023-09-26 08:14:17 -0600242 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
Tom Rini195bc4b2017-08-14 21:01:30 -0400243 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100244#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300245 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100246#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000247 return -1;
248 }
York Suncea88212018-06-26 09:44:39 -0700249#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000250
Pali Rohárdda8f882022-01-14 14:31:38 +0100251 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200252 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530253 if (ret)
254 return ret;
255
Alexandru Gagniuc36fd3ba2021-07-15 14:19:23 -0500256 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
Michal Simek50fa1182023-05-17 09:17:16 +0200257 puts("Expected image is not found. Trying to start U-Boot\n");
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530258 return -ENOENT;
259 }
260
261 return 0;
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000262}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200263#else
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
Alexandru Gagniuc99426c82021-12-30 10:39:59 -0600272#ifndef CONFIG_SPL_OS_BOOT
273int spl_start_uboot(void)
274{
275 return 1;
276}
277#endif
278
Pali Rohára0811552023-01-21 15:13:08 +0100279#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Pali Rohárdda8f882022-01-14 14:31:38 +0100280static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
281 struct spl_boot_device *bootdev,
282 struct mmc *mmc,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500283 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200284{
285 int err = -ENOSYS;
286
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400287 __maybe_unused int partition = CONFIG_SYS_MMCSD_FS_BOOT_PARTITION;
288
289#if CONFIG_SYS_MMCSD_FS_BOOT_PARTITION == -1
290 {
291 struct disk_partition info;
292 debug("Checking for the first MBR bootable partition\n");
293 for (int type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
294 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
295 if (err)
296 continue;
297 debug("Partition %d is of type %d and bootable=%d\n", type_part, info.sys_ind, info.bootable);
298 if (info.bootable != 0) {
299 debug("Partition %d is bootable, using it\n", type_part);
300 partition = type_part;
301 break;
302 }
303 }
304 printf("Using first bootable partition: %d\n", partition);
305 if (partition == CONFIG_SYS_MMCSD_FS_BOOT_PARTITION) {
306 return -ENOSYS;
307 }
308 }
309#endif
310
Tien Fong Chee6fd0a712019-01-23 14:20:03 +0800311#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200312 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100313 err = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400314 partition);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200315 if (!err)
316 return err;
317 }
318#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohárdda8f882022-01-14 14:31:38 +0100319 err = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400320 partition,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500321 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200322 if (!err)
323 return err;
324#endif
325#endif
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800326#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200327 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100328 err = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400329 partition);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200330 if (!err)
331 return err;
332 }
333#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohárdda8f882022-01-14 14:31:38 +0100334 err = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400335 partition,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500336 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200337 if (!err)
338 return err;
339#endif
340#endif
341
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800342#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200343 err = -ENOENT;
344#endif
345
346 return err;
347}
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200348#endif
349
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100350u32 __weak spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100351{
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800352#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100353 return MMCSD_MODE_FS;
354#elif defined(CONFIG_SUPPORT_EMMC_BOOT)
355 return MMCSD_MODE_EMMCBOOT;
356#else
357 return MMCSD_MODE_RAW;
358#endif
359}
360
Patrick Delaunay113aa502018-03-20 10:54:51 +0100361#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200362int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay113aa502018-03-20 10:54:51 +0100363{
364 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
365}
366#endif
367
Marek Vasutf9a921e2023-10-16 18:16:12 +0200368unsigned long __weak arch_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
369 unsigned long raw_sect)
370{
371 return raw_sect;
372}
373
374unsigned long __weak board_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
375 unsigned long raw_sect)
376{
377 return arch_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
378}
379
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530380unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
381 unsigned long raw_sect)
Peng Fan315cf592019-09-23 10:18:40 +0800382{
Marek Vasutf9a921e2023-10-16 18:16:12 +0200383 return board_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fan315cf592019-09-23 10:18:40 +0800384}
385
Marek Vasutde1ce232021-07-03 04:55:32 +0200386int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
387{
388 int part;
389#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
390 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
391#else
392 /*
393 * We need to check what the partition is configured to.
394 * 1 and 2 match up to boot0 / boot1 and 7 is user data
395 * which is the first physical partition (0).
396 */
Pali Roháreafdbe52023-03-11 11:44:27 +0100397 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
Marek Vasutde1ce232021-07-03 04:55:32 +0200398 if (part == 7)
399 part = 0;
400#endif
401 return part;
402}
403
404int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
405{
406 return default_spl_mmc_emmc_boot_partition(mmc);
407}
408
Harald Seileraf8b7b72022-07-11 14:35:32 +0200409static int spl_mmc_get_mmc_devnum(struct mmc *mmc)
410{
411 struct blk_desc *block_dev;
412#if !CONFIG_IS_ENABLED(BLK)
413 block_dev = &mmc->block_dev;
414#else
Sean Andersond2b254e2023-10-07 21:47:48 -0400415 block_dev = mmc_get_blk_desc(mmc);
Harald Seileraf8b7b72022-07-11 14:35:32 +0200416#endif
417 return block_dev->devnum;
418}
419
Sean Anderson13e6d912023-10-14 16:47:54 -0400420static struct mmc *mmc;
421
422void spl_mmc_clear_cache(void)
423{
424 mmc = NULL;
425}
426
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500427int spl_mmc_load(struct spl_image_info *spl_image,
428 struct spl_boot_device *bootdev,
429 const char *filename,
430 int raw_part,
431 unsigned long raw_sect)
Simon Schwarze21d2d82011-09-14 15:33:34 -0400432{
Simon Schwarze21d2d82011-09-14 15:33:34 -0400433 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600434 int err = 0;
Baruch Siachfef81be2020-01-15 09:08:08 +0200435 __maybe_unused int part = 0;
Harald Seileraf8b7b72022-07-11 14:35:32 +0200436 int mmc_dev;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400437
Harald Seileraf8b7b72022-07-11 14:35:32 +0200438 /* Perform peripheral init only once for an mmc device */
439 mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
440 if (!mmc || spl_mmc_get_mmc_devnum(mmc) != mmc_dev) {
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500441 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
442 if (err)
443 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400444
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500445 err = mmc_init(mmc);
446 if (err) {
447 mmc = NULL;
Paul Burtona8015fd2013-09-04 16:12:24 +0100448#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500449 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100450#endif
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500451 return err;
452 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400453 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000454
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100455 boot_mode = spl_mmc_boot_mode(mmc, bootdev->boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200456 err = -EINVAL;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200457 switch (boot_mode) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200458 case MMCSD_MODE_EMMCBOOT:
Marek Vasutde1ce232021-07-03 04:55:32 +0200459 part = spl_mmc_emmc_boot_partition(mmc);
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200460
Mans Rullgardec046a52019-09-03 16:17:17 +0200461 if (CONFIG_IS_ENABLED(MMC_TINY))
462 err = mmc_switch_part(mmc, part);
463 else
464 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutf537e392016-12-01 02:06:33 +0100465
Mans Rullgardec046a52019-09-03 16:17:17 +0200466 if (err) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200467#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgardec046a52019-09-03 16:17:17 +0200468 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200469#endif
Mans Rullgardec046a52019-09-03 16:17:17 +0200470 return err;
471 }
472 /* Fall through */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200473 case MMCSD_MODE_RAW:
474 debug("spl: mmc boot mode: raw\n");
475
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200476 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100477 err = mmc_load_image_raw_os(spl_image, bootdev, mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200478 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200479 return err;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200480 }
Peng Fandc7d3222019-10-23 01:43:30 +0000481
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530482 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fandc7d3222019-10-23 01:43:30 +0000483
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800484#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Pali Rohárdda8f882022-01-14 14:31:38 +0100485 err = mmc_load_image_raw_partition(spl_image, bootdev,
486 mmc, raw_part,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500487 raw_sect);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200488 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200489 return err;
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800490#endif
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200491#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohárdda8f882022-01-14 14:31:38 +0100492 err = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Baruch Siachfef81be2020-01-15 09:08:08 +0200493 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200494 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200495 return err;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200496#endif
Guillaume GARDET834dd882016-02-18 18:17:36 +0100497 /* If RAW mode fails, try FS mode. */
Pali Rohára0811552023-01-21 15:13:08 +0100498#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200499 case MMCSD_MODE_FS:
500 debug("spl: mmc boot mode: fs\n");
501
Pali Rohárdda8f882022-01-14 14:31:38 +0100502 err = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200503 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200504 return err;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200505
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200506 break;
Pali Rohára0811552023-01-21 15:13:08 +0100507#endif
Paul Burtona8015fd2013-09-04 16:12:24 +0100508#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200509 default:
510 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100511#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200512 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200513
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200514 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400515}
Simon Glassd449cb82016-09-24 18:20:00 -0600516
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500517int spl_mmc_load_image(struct spl_image_info *spl_image,
518 struct spl_boot_device *bootdev)
519{
520 return spl_mmc_load(spl_image, bootdev,
521#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
522 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
523#else
524 NULL,
525#endif
526#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200527 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500528#else
529 0,
530#endif
531#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
532 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
533#else
534 0);
535#endif
536}
537
Simon Glass4fc1f252016-11-30 15:30:50 -0700538SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
539SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
540SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);