blob: 03a081fa47e042c1cfee00e53adf947ed70aa903 [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>
Sean Anderson952ed672023-10-14 16:47:44 -040019#include <imx_container.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040020
Pali Rohárdda8f882022-01-14 14:31:38 +010021static int mmc_load_legacy(struct spl_image_info *spl_image,
22 struct spl_boot_device *bootdev,
23 struct mmc *mmc,
Simon Glassbb7d3bb2022-09-06 20:26:52 -060024 ulong sector, struct legacy_img_hdr *header)
Simon Glassb52c7fc2016-02-22 22:55:58 -070025{
Pali Rohár11041672021-07-23 11:14:27 +020026 u32 image_offset_sectors;
Simon Glassb52c7fc2016-02-22 22:55:58 -070027 u32 image_size_sectors;
28 unsigned long count;
Pali Rohár11041672021-07-23 11:14:27 +020029 u32 image_offset;
Marek Vasut02266e22016-04-29 00:44:54 +020030 int ret;
31
Pali Rohárdda8f882022-01-14 14:31:38 +010032 ret = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut02266e22016-04-29 00:44:54 +020033 if (ret)
34 return ret;
Simon Glassb52c7fc2016-02-22 22:55:58 -070035
Pali Rohár11041672021-07-23 11:14:27 +020036 /* convert offset to sectors - round down */
37 image_offset_sectors = spl_image->offset / mmc->read_bl_len;
38 /* calculate remaining offset */
39 image_offset = spl_image->offset % mmc->read_bl_len;
40
Simon Glassb52c7fc2016-02-22 22:55:58 -070041 /* convert size to sectors - round up */
Simon Glassee306792016-09-24 18:20:13 -060042 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
Simon Glassb52c7fc2016-02-22 22:55:58 -070043 mmc->read_bl_len;
44
45 /* Read the header too to avoid extra memcpy */
Pali Rohár11041672021-07-23 11:14:27 +020046 count = blk_dread(mmc_get_blk_desc(mmc),
47 sector + image_offset_sectors,
48 image_size_sectors,
Simon Glassee306792016-09-24 18:20:13 -060049 (void *)(ulong)spl_image->load_addr);
Andre Przywara6c526072017-01-02 11:48:31 +000050 debug("read %x sectors to %lx\n", image_size_sectors,
Simon Glassee306792016-09-24 18:20:13 -060051 spl_image->load_addr);
Simon Glassb52c7fc2016-02-22 22:55:58 -070052 if (count != image_size_sectors)
53 return -EIO;
54
Pali Rohár11041672021-07-23 11:14:27 +020055 if (image_offset)
56 memmove((void *)(ulong)spl_image->load_addr,
57 (void *)(ulong)spl_image->load_addr + image_offset,
58 spl_image->size);
59
Simon Glassb52c7fc2016-02-22 22:55:58 -070060 return 0;
61}
62
Simon Glassb52c7fc2016-02-22 22:55:58 -070063static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
64 ulong count, void *buf)
65{
66 struct mmc *mmc = load->dev;
67
Simon Glass670d3042016-05-14 14:03:06 -060068 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
Simon Glassb52c7fc2016-02-22 22:55:58 -070069}
Simon Glassb52c7fc2016-02-22 22:55:58 -070070
Baruch Siachfef81be2020-01-15 09:08:08 +020071static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
72{
73#if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
74 if (part == 0)
75 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
76#endif
77
78 return 0;
79}
80
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090081static __maybe_unused
82int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010083 struct spl_boot_device *bootdev,
Seung-Woo Kimc0a4e2d2017-09-19 13:29:45 +090084 struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040085{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020086 unsigned long count;
Simon Glassbb7d3bb2022-09-06 20:26:52 -060087 struct legacy_img_hdr *header;
Marek Vasut1dda46f2018-08-14 11:27:02 +020088 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glassb52c7fc2016-02-22 22:55:58 -070089 int ret = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040090
Marek Vasut1dda46f2018-08-14 11:27:02 +020091 header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
Simon Schwarze21d2d82011-09-14 15:33:34 -040092
93 /* read image header to find the image size & load address */
Marek Vasut1dda46f2018-08-14 11:27:02 +020094 count = blk_dread(bd, sector, 1, header);
Simon Glassb52c7fc2016-02-22 22:55:58 -070095 debug("hdr read sector %lx, count=%lu\n", sector, count);
96 if (count == 0) {
97 ret = -EIO;
Simon Schwarze21d2d82011-09-14 15:33:34 -040098 goto end;
Simon Glassb52c7fc2016-02-22 22:55:58 -070099 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400100
Masahiro Yamada802f1c32016-03-16 12:10:00 +0900101 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
102 image_get_magic(header) == FDT_MAGIC) {
Simon Glassb52c7fc2016-02-22 22:55:58 -0700103 struct spl_load_info load;
104
105 debug("Found FIT\n");
106 load.dev = mmc;
107 load.priv = NULL;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530108 load.filename = NULL;
Simon Glassb52c7fc2016-02-22 22:55:58 -0700109 load.bl_len = mmc->read_bl_len;
110 load.read = h_spl_load_read;
Simon Glass43a734f2016-09-24 18:20:16 -0600111 ret = spl_load_simple_fit(spl_image, &load, sector, header);
Sean Anderson952ed672023-10-14 16:47:44 -0400112 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
113 valid_container_hdr((void *)header)) {
Peng Fand8670762019-08-22 07:42:38 +0000114 struct spl_load_info load;
115
116 load.dev = mmc;
117 load.priv = NULL;
118 load.filename = NULL;
119 load.bl_len = mmc->read_bl_len;
120 load.read = h_spl_load_read;
121
122 ret = spl_load_imx_container(spl_image, &load, sector);
Masahiro Yamada802f1c32016-03-16 12:10:00 +0900123 } else {
Pali Rohárdda8f882022-01-14 14:31:38 +0100124 ret = mmc_load_legacy(spl_image, bootdev, mmc, sector, header);
Simon Glass5614db82015-06-23 15:38:47 -0600125 }
Tom Rini1530e8a2013-06-28 14:43:01 -0400126
Simon Schwarze21d2d82011-09-14 15:33:34 -0400127end:
Simon Glassb52c7fc2016-02-22 22:55:58 -0700128 if (ret) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100129#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300130 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100131#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200132 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200133 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200134
135 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400136}
137
Masahiro Yamada2a4a20c2017-06-22 16:52:36 +0900138static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200139{
140 switch (boot_device) {
141 case BOOT_DEVICE_MMC1:
142 return 0;
143 case BOOT_DEVICE_MMC2:
144 case BOOT_DEVICE_MMC2_2:
145 return 1;
146 }
147
148#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
149 printf("spl: unsupported mmc boot device.\n");
150#endif
151
152 return -ENODEV;
153}
154
Simon Glass4faf63b2015-12-02 10:59:12 -0700155static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200156{
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200157 int err, mmc_dev;
158
159 mmc_dev = spl_mmc_get_device_index(boot_device);
160 if (mmc_dev < 0)
161 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200162
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530163#if CONFIG_IS_ENABLED(DM_MMC)
164 err = mmc_init_device(mmc_dev);
165#else
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200166 err = mmc_initialize(NULL);
Lokesh Vutlac59b41c2019-09-09 14:40:36 +0530167#endif /* DM_MMC */
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200168 if (err) {
169#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
170 printf("spl: could not initialize mmc. error: %d\n", err);
171#endif
172 return err;
173 }
Simon Glass51ace322015-12-02 10:59:13 -0700174 *mmcp = find_mmc_device(mmc_dev);
175 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200176 if (err) {
177#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanf5920d02018-09-18 08:11:55 +0000178 printf("spl: could not find mmc device %d. error: %d\n",
179 mmc_dev, err);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200180#endif
181 return err;
182 }
183
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200184 return 0;
185}
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200186
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800187#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glassee306792016-09-24 18:20:13 -0600188static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100189 struct spl_boot_device *bootdev,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500190 struct mmc *mmc, int partition,
191 unsigned long sector)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100192{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600193 struct disk_partition info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200194 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100195
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800196#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
197 int type_part;
198 /* Only support MBR so DOS_ENTRY_NUMBERS */
199 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
200 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
201 if (err)
202 continue;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200203 if (info.sys_ind ==
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800204 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
205 partition = type_part;
206 break;
207 }
208 }
209#endif
210
Simon Glass55877f92016-07-05 17:10:18 -0600211 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200212 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100213#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200214 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100215#endif
216 return -1;
217 }
218
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200219#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohárdda8f882022-01-14 14:31:38 +0100220 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200221#else
Pali Rohárdda8f882022-01-14 14:31:38 +0100222 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200223#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100224}
225#endif
226
Alexandru Gagniuc99426c82021-12-30 10:39:59 -0600227#if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
Simon Glassee306792016-09-24 18:20:13 -0600228static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100229 struct spl_boot_device *bootdev,
Simon Glassee306792016-09-24 18:20:13 -0600230 struct mmc *mmc)
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000231{
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530232 int ret;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200233
Elena Popa6532fab2023-08-08 16:42:15 +0300234#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
York Suncea88212018-06-26 09:44:39 -0700235 unsigned long count;
236
Simon Glass70061ba2016-06-12 23:30:32 -0600237 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200238 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
239 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
Simon Glass259cdb42023-09-26 08:14:17 -0600240 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
Tom Rini195bc4b2017-08-14 21:01:30 -0400241 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100242#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300243 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100244#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000245 return -1;
246 }
York Suncea88212018-06-26 09:44:39 -0700247#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000248
Pali Rohárdda8f882022-01-14 14:31:38 +0100249 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200250 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530251 if (ret)
252 return ret;
253
Alexandru Gagniuc36fd3ba2021-07-15 14:19:23 -0500254 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
Michal Simek50fa1182023-05-17 09:17:16 +0200255 puts("Expected image is not found. Trying to start U-Boot\n");
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530256 return -ENOENT;
257 }
258
259 return 0;
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000260}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200261#else
Simon Glassee306792016-09-24 18:20:13 -0600262static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100263 struct spl_boot_device *bootdev,
Simon Glassee306792016-09-24 18:20:13 -0600264 struct mmc *mmc)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200265{
266 return -ENOSYS;
267}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000268#endif
269
Alexandru Gagniuc99426c82021-12-30 10:39:59 -0600270#ifndef CONFIG_SPL_OS_BOOT
271int spl_start_uboot(void)
272{
273 return 1;
274}
275#endif
276
Pali Rohára0811552023-01-21 15:13:08 +0100277#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Pali Rohárdda8f882022-01-14 14:31:38 +0100278static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
279 struct spl_boot_device *bootdev,
280 struct mmc *mmc,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500281 const char *filename)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200282{
283 int err = -ENOSYS;
284
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400285 __maybe_unused int partition = CONFIG_SYS_MMCSD_FS_BOOT_PARTITION;
286
287#if CONFIG_SYS_MMCSD_FS_BOOT_PARTITION == -1
288 {
289 struct disk_partition info;
290 debug("Checking for the first MBR bootable partition\n");
291 for (int type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
292 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
293 if (err)
294 continue;
295 debug("Partition %d is of type %d and bootable=%d\n", type_part, info.sys_ind, info.bootable);
296 if (info.bootable != 0) {
297 debug("Partition %d is bootable, using it\n", type_part);
298 partition = type_part;
299 break;
300 }
301 }
302 printf("Using first bootable partition: %d\n", partition);
303 if (partition == CONFIG_SYS_MMCSD_FS_BOOT_PARTITION) {
304 return -ENOSYS;
305 }
306 }
307#endif
308
Tien Fong Chee6fd0a712019-01-23 14:20:03 +0800309#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200310 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100311 err = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400312 partition);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200313 if (!err)
314 return err;
315 }
316#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohárdda8f882022-01-14 14:31:38 +0100317 err = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400318 partition,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500319 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200320 if (!err)
321 return err;
322#endif
323#endif
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800324#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200325 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100326 err = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400327 partition);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200328 if (!err)
329 return err;
330 }
331#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohárdda8f882022-01-14 14:31:38 +0100332 err = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carreterod0a8e592022-03-15 16:34:51 -0400333 partition,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500334 filename);
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200335 if (!err)
336 return err;
337#endif
338#endif
339
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800340#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200341 err = -ENOENT;
342#endif
343
344 return err;
345}
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200346#endif
347
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100348u32 __weak spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100349{
Tien Fong Chee6091dd12019-01-23 14:20:05 +0800350#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewski7c88d102018-02-03 08:29:52 +0100351 return MMCSD_MODE_FS;
352#elif defined(CONFIG_SUPPORT_EMMC_BOOT)
353 return MMCSD_MODE_EMMCBOOT;
354#else
355 return MMCSD_MODE_RAW;
356#endif
357}
358
Patrick Delaunay113aa502018-03-20 10:54:51 +0100359#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200360int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay113aa502018-03-20 10:54:51 +0100361{
362 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
363}
364#endif
365
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530366unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
367 unsigned long raw_sect)
Peng Fan315cf592019-09-23 10:18:40 +0800368{
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530369 return raw_sect;
Peng Fan315cf592019-09-23 10:18:40 +0800370}
371
Marek Vasutde1ce232021-07-03 04:55:32 +0200372int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
373{
374 int part;
375#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
376 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
377#else
378 /*
379 * We need to check what the partition is configured to.
380 * 1 and 2 match up to boot0 / boot1 and 7 is user data
381 * which is the first physical partition (0).
382 */
Pali Roháreafdbe52023-03-11 11:44:27 +0100383 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
Marek Vasutde1ce232021-07-03 04:55:32 +0200384 if (part == 7)
385 part = 0;
386#endif
387 return part;
388}
389
390int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
391{
392 return default_spl_mmc_emmc_boot_partition(mmc);
393}
394
Harald Seileraf8b7b72022-07-11 14:35:32 +0200395static int spl_mmc_get_mmc_devnum(struct mmc *mmc)
396{
397 struct blk_desc *block_dev;
398#if !CONFIG_IS_ENABLED(BLK)
399 block_dev = &mmc->block_dev;
400#else
Sean Andersond2b254e2023-10-07 21:47:48 -0400401 block_dev = mmc_get_blk_desc(mmc);
Harald Seileraf8b7b72022-07-11 14:35:32 +0200402#endif
403 return block_dev->devnum;
404}
405
Sean Anderson13e6d912023-10-14 16:47:54 -0400406static struct mmc *mmc;
407
408void spl_mmc_clear_cache(void)
409{
410 mmc = NULL;
411}
412
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500413int spl_mmc_load(struct spl_image_info *spl_image,
414 struct spl_boot_device *bootdev,
415 const char *filename,
416 int raw_part,
417 unsigned long raw_sect)
Simon Schwarze21d2d82011-09-14 15:33:34 -0400418{
Simon Schwarze21d2d82011-09-14 15:33:34 -0400419 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600420 int err = 0;
Baruch Siachfef81be2020-01-15 09:08:08 +0200421 __maybe_unused int part = 0;
Harald Seileraf8b7b72022-07-11 14:35:32 +0200422 int mmc_dev;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400423
Harald Seileraf8b7b72022-07-11 14:35:32 +0200424 /* Perform peripheral init only once for an mmc device */
425 mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
426 if (!mmc || spl_mmc_get_mmc_devnum(mmc) != mmc_dev) {
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500427 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
428 if (err)
429 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400430
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500431 err = mmc_init(mmc);
432 if (err) {
433 mmc = NULL;
Paul Burtona8015fd2013-09-04 16:12:24 +0100434#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500435 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100436#endif
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500437 return err;
438 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400439 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000440
Andre Przywara3cb12ef2021-07-12 11:06:49 +0100441 boot_mode = spl_mmc_boot_mode(mmc, bootdev->boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200442 err = -EINVAL;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200443 switch (boot_mode) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200444 case MMCSD_MODE_EMMCBOOT:
Marek Vasutde1ce232021-07-03 04:55:32 +0200445 part = spl_mmc_emmc_boot_partition(mmc);
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200446
Mans Rullgardec046a52019-09-03 16:17:17 +0200447 if (CONFIG_IS_ENABLED(MMC_TINY))
448 err = mmc_switch_part(mmc, part);
449 else
450 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutf537e392016-12-01 02:06:33 +0100451
Mans Rullgardec046a52019-09-03 16:17:17 +0200452 if (err) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200453#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgardec046a52019-09-03 16:17:17 +0200454 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200455#endif
Mans Rullgardec046a52019-09-03 16:17:17 +0200456 return err;
457 }
458 /* Fall through */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200459 case MMCSD_MODE_RAW:
460 debug("spl: mmc boot mode: raw\n");
461
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200462 if (!spl_start_uboot()) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100463 err = mmc_load_image_raw_os(spl_image, bootdev, mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200464 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200465 return err;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200466 }
Peng Fandc7d3222019-10-23 01:43:30 +0000467
Faiz Abbas0bdf5632020-02-26 13:44:35 +0530468 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fandc7d3222019-10-23 01:43:30 +0000469
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800470#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Pali Rohárdda8f882022-01-14 14:31:38 +0100471 err = mmc_load_image_raw_partition(spl_image, bootdev,
472 mmc, raw_part,
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500473 raw_sect);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200474 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200475 return err;
Dalon Westergreenf5ba9852017-02-10 17:15:35 -0800476#endif
Semen Protsenko7e3ba482016-11-16 19:19:05 +0200477#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohárdda8f882022-01-14 14:31:38 +0100478 err = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Baruch Siachfef81be2020-01-15 09:08:08 +0200479 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200480 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200481 return err;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200482#endif
Guillaume GARDET834dd882016-02-18 18:17:36 +0100483 /* If RAW mode fails, try FS mode. */
Pali Rohára0811552023-01-21 15:13:08 +0100484#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200485 case MMCSD_MODE_FS:
486 debug("spl: mmc boot mode: fs\n");
487
Pali Rohárdda8f882022-01-14 14:31:38 +0100488 err = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200489 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200490 return err;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200491
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200492 break;
Pali Rohára0811552023-01-21 15:13:08 +0100493#endif
Paul Burtona8015fd2013-09-04 16:12:24 +0100494#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200495 default:
496 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100497#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200498 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200499
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200500 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400501}
Simon Glassd449cb82016-09-24 18:20:00 -0600502
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500503int spl_mmc_load_image(struct spl_image_info *spl_image,
504 struct spl_boot_device *bootdev)
505{
506 return spl_mmc_load(spl_image, bootdev,
507#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
508 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
509#else
510 NULL,
511#endif
512#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerbf16c302020-04-15 11:33:31 +0200513 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500514#else
515 0,
516#endif
517#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
518 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
519#else
520 0);
521#endif
522}
523
Simon Glass4fc1f252016-11-30 15:30:50 -0700524SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
525SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
526SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);