blob: 67c7ae34a58550b41b7d0b1918f112b15a5e66e5 [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>
Sean Andersonab121792023-10-14 16:47:44 -040019#include <imx_container.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040020
Pali Rohár2e0429b2022-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 Glassf3543e62022-09-06 20:26:52 -060024 ulong sector, struct legacy_img_hdr *header)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040025{
Pali Rohár5fce2872021-07-23 11:14:27 +020026 u32 image_offset_sectors;
Peter Korsgaard2e222102013-03-21 04:55:17 +000027 u32 image_size_sectors;
Simon Glass96debd12016-02-22 22:55:58 -070028 unsigned long count;
Pali Rohár5fce2872021-07-23 11:14:27 +020029 u32 image_offset;
Marek Vasut7e0f2262016-04-29 00:44:54 +020030 int ret;
Tom Rinie4c444b2013-06-28 14:43:01 -040031
Pali Rohár2e0429b2022-01-14 14:31:38 +010032 ret = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +020033 if (ret)
34 return ret;
35
Pali Rohár5fce2872021-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 Schwarz9ea5c6e2011-09-14 15:33:34 -040041 /* convert size to sectors - round up */
Simon Glass2a2ee2a2016-09-24 18:20:13 -060042 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
Paul Kocialkowski91199f42015-04-27 10:20:22 +020043 mmc->read_bl_len;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040044
45 /* Read the header too to avoid extra memcpy */
Pali Rohár5fce2872021-07-23 11:14:27 +020046 count = blk_dread(mmc_get_blk_desc(mmc),
47 sector + image_offset_sectors,
48 image_size_sectors,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060049 (void *)(ulong)spl_image->load_addr);
Andre Przywara11e14792017-01-02 11:48:31 +000050 debug("read %x sectors to %lx\n", image_size_sectors,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060051 spl_image->load_addr);
Simon Glass96debd12016-02-22 22:55:58 -070052 if (count != image_size_sectors)
53 return -EIO;
54
Pali Rohár5fce2872021-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 Glass96debd12016-02-22 22:55:58 -070060 return 0;
61}
62
Simon Glass96debd12016-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 Glassef5609c2016-05-14 14:03:06 -060068 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
Simon Glass96debd12016-02-22 22:55:58 -070069}
Simon Glass96debd12016-02-22 22:55:58 -070070
Baruch Siache42afd82020-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 Kimb016b582017-09-19 13:29:45 +090081static __maybe_unused
82int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010083 struct spl_boot_device *bootdev,
Seung-Woo Kimb016b582017-09-19 13:29:45 +090084 struct mmc *mmc, unsigned long sector)
Simon Glass96debd12016-02-22 22:55:58 -070085{
86 unsigned long count;
Simon Glassf3543e62022-09-06 20:26:52 -060087 struct legacy_img_hdr *header;
Marek Vasut04ce5422018-08-14 11:27:02 +020088 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glass96debd12016-02-22 22:55:58 -070089 int ret = 0;
90
Marek Vasut04ce5422018-08-14 11:27:02 +020091 header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
Simon Glass96debd12016-02-22 22:55:58 -070092
93 /* read image header to find the image size & load address */
Marek Vasut04ce5422018-08-14 11:27:02 +020094 count = blk_dread(bd, sector, 1, header);
Simon Glass96debd12016-02-22 22:55:58 -070095 debug("hdr read sector %lx, count=%lu\n", sector, count);
96 if (count == 0) {
97 ret = -EIO;
98 goto end;
99 }
100
Masahiro Yamada4976f482016-03-16 12:10:00 +0900101 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
102 image_get_magic(header) == FDT_MAGIC) {
Simon Glass96debd12016-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 Vutlaeafd5412016-05-24 10:34:38 +0530108 load.filename = NULL;
Simon Glass96debd12016-02-22 22:55:58 -0700109 load.bl_len = mmc->read_bl_len;
110 load.read = h_spl_load_read;
Simon Glassf4d7d852016-09-24 18:20:16 -0600111 ret = spl_load_simple_fit(spl_image, &load, sector, header);
Sean Andersonab121792023-10-14 16:47:44 -0400112 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
113 valid_container_hdr((void *)header)) {
Peng Fandd7d0912019-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 Yamada4976f482016-03-16 12:10:00 +0900123 } else {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100124 ret = mmc_load_legacy(spl_image, bootdev, mmc, sector, header);
Simon Glass96debd12016-02-22 22:55:58 -0700125 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400126
127end:
Simon Glass96debd12016-02-22 22:55:58 -0700128 if (ret) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100129#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam5bbf4092016-04-20 23:12:04 -0300130 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100131#endif
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200132 return -1;
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200133 }
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200134
135 return 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400136}
137
Masahiro Yamada207d8b32017-06-22 16:52:36 +0900138static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanova1e56cf2015-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 Glass99c7a512015-12-02 10:59:12 -0700155static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200156{
Nikita Kiryanova1e56cf2015-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 Kiryanov4188ba32015-11-08 17:11:44 +0200162
Lokesh Vutla80f02012019-09-09 14:40:36 +0530163#if CONFIG_IS_ENABLED(DM_MMC)
164 err = mmc_init_device(mmc_dev);
165#else
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200166 err = mmc_initialize(NULL);
Lokesh Vutla80f02012019-09-09 14:40:36 +0530167#endif /* DM_MMC */
Nikita Kiryanov4188ba32015-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 Glassb4857aa2015-12-02 10:59:13 -0700174 *mmcp = find_mmc_device(mmc_dev);
175 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200176 if (err) {
177#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanaa608b62018-09-18 08:11:55 +0000178 printf("spl: could not find mmc device %d. error: %d\n",
179 mmc_dev, err);
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200180#endif
181 return err;
182 }
183
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200184 return 0;
185}
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200186
Dalon Westergreen949123e2017-02-10 17:15:35 -0800187#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600188static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100189 struct spl_boot_device *bootdev,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500190 struct mmc *mmc, int partition,
191 unsigned long sector)
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100192{
Simon Glass05289792020-05-10 11:39:57 -0600193 struct disk_partition info;
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200194 int err;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100195
Dalon Westergreenf0fb4fa2017-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 Denk0a50b3c2021-09-27 17:42:38 +0200203 if (info.sys_ind ==
Dalon Westergreenf0fb4fa2017-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 Glass1e2b3ef2016-07-05 17:10:18 -0600211 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200212 if (err) {
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100213#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200214 puts("spl: partition error\n");
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100215#endif
216 return -1;
217 }
218
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200219#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohár2e0429b2022-01-14 14:31:38 +0100220 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200221#else
Pali Rohár2e0429b2022-01-14 14:31:38 +0100222 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200223#endif
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100224}
225#endif
226
Alexandru Gagniuc22eb1522021-12-30 10:39:59 -0600227#if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600228static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100229 struct spl_boot_device *bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600230 struct mmc *mmc)
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000231{
Lokesh Vutla811906a2016-04-11 15:24:50 +0530232 int ret;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200233
Elena Popa86b1aad2023-08-08 16:42:15 +0300234#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
York Sun7267fbf2018-06-26 09:44:39 -0700235 unsigned long count;
236
Simon Glass87bce4e2016-06-12 23:30:32 -0600237 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200238 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
239 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
Simon Glass9cbdc3a2023-09-26 08:14:17 -0600240 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
Tom Rini9585dd32017-08-14 21:01:30 -0400241 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100242#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam5bbf4092016-04-20 23:12:04 -0300243 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100244#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000245 return -1;
246 }
York Sun7267fbf2018-06-26 09:44:39 -0700247#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000248
Pali Rohár2e0429b2022-01-14 14:31:38 +0100249 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200250 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla811906a2016-04-11 15:24:50 +0530251 if (ret)
252 return ret;
253
Alexandru Gagniuc5f6e5c32021-07-15 14:19:23 -0500254 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
Michal Simek1be82af2023-05-17 09:17:16 +0200255 puts("Expected image is not found. Trying to start U-Boot\n");
Lokesh Vutla811906a2016-04-11 15:24:50 +0530256 return -ENOENT;
257 }
258
259 return 0;
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000260}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200261#else
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600262static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100263 struct spl_boot_device *bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600264 struct mmc *mmc)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200265{
266 return -ENOSYS;
267}
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000268#endif
269
Alexandru Gagniuc22eb1522021-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ár718d1c72023-01-21 15:13:08 +0100277#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Pali Rohár2e0429b2022-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 Dannenberge1eb6ad2019-06-04 17:55:46 -0500281 const char *filename)
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200282{
283 int err = -ENOSYS;
284
Jérôme Carretero5fa973e2022-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 Chee0c3a9ed2019-01-23 14:20:03 +0800309#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200310 if (!spl_start_uboot()) {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100311 err = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400312 partition);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200313 if (!err)
314 return err;
315 }
316#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohár2e0429b2022-01-14 14:31:38 +0100317 err = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400318 partition,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500319 filename);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200320 if (!err)
321 return err;
322#endif
323#endif
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800324#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200325 if (!spl_start_uboot()) {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100326 err = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400327 partition);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200328 if (!err)
329 return err;
330 }
331#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohár2e0429b2022-01-14 14:31:38 +0100332 err = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400333 partition,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500334 filename);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200335 if (!err)
336 return err;
337#endif
338#endif
339
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800340#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200341 err = -ENOENT;
342#endif
343
344 return err;
345}
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200346#endif
347
Andre Przywara59073572021-07-12 11:06:49 +0100348u32 __weak spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Lukasz Majewskid695d662018-02-03 08:29:52 +0100349{
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800350#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewskid695d662018-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 Delaunay35a66962018-03-20 10:54:51 +0100359#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +0200360int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay35a66962018-03-20 10:54:51 +0100361{
362 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
363}
364#endif
365
Faiz Abbascf008252020-02-26 13:44:35 +0530366unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
367 unsigned long raw_sect)
Peng Fan30ecc9c2019-09-23 10:18:40 +0800368{
Faiz Abbascf008252020-02-26 13:44:35 +0530369 return raw_sect;
Peng Fan30ecc9c2019-09-23 10:18:40 +0800370}
371
Marek Vasut9b191592021-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ár8b882062023-03-11 11:44:27 +0100383 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
Marek Vasut9b191592021-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 Seilerbf28d9a2022-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 Andersona0733b32023-10-07 21:47:48 -0400401 block_dev = mmc_get_blk_desc(mmc);
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200402#endif
403 return block_dev->devnum;
404}
405
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500406int spl_mmc_load(struct spl_image_info *spl_image,
407 struct spl_boot_device *bootdev,
408 const char *filename,
409 int raw_part,
410 unsigned long raw_sect)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400411{
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500412 static struct mmc *mmc;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400413 u32 boot_mode;
Simon Glassdc3dedf2015-06-23 15:38:51 -0600414 int err = 0;
Baruch Siache42afd82020-01-15 09:08:08 +0200415 __maybe_unused int part = 0;
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200416 int mmc_dev;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400417
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200418 /* Perform peripheral init only once for an mmc device */
419 mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
420 if (!mmc || spl_mmc_get_mmc_devnum(mmc) != mmc_dev) {
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500421 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
422 if (err)
423 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400424
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500425 err = mmc_init(mmc);
426 if (err) {
427 mmc = NULL;
Paul Burton8112f5f2013-09-04 16:12:24 +0100428#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500429 printf("spl: mmc init failed with error: %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100430#endif
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500431 return err;
432 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400433 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000434
Andre Przywara59073572021-07-12 11:06:49 +0100435 boot_mode = spl_mmc_boot_mode(mmc, bootdev->boot_device);
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200436 err = -EINVAL;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200437 switch (boot_mode) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200438 case MMCSD_MODE_EMMCBOOT:
Marek Vasut9b191592021-07-03 04:55:32 +0200439 part = spl_mmc_emmc_boot_partition(mmc);
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200440
Mans Rullgard92439902019-09-03 16:17:17 +0200441 if (CONFIG_IS_ENABLED(MMC_TINY))
442 err = mmc_switch_part(mmc, part);
443 else
444 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutb5b838f2016-12-01 02:06:33 +0100445
Mans Rullgard92439902019-09-03 16:17:17 +0200446 if (err) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200447#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgard92439902019-09-03 16:17:17 +0200448 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200449#endif
Mans Rullgard92439902019-09-03 16:17:17 +0200450 return err;
451 }
452 /* Fall through */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200453 case MMCSD_MODE_RAW:
454 debug("spl: mmc boot mode: raw\n");
455
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200456 if (!spl_start_uboot()) {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100457 err = mmc_load_image_raw_os(spl_image, bootdev, mmc);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200458 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200459 return err;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200460 }
Peng Fana335f802019-10-23 01:43:30 +0000461
Faiz Abbascf008252020-02-26 13:44:35 +0530462 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fana335f802019-10-23 01:43:30 +0000463
Dalon Westergreen949123e2017-02-10 17:15:35 -0800464#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Pali Rohár2e0429b2022-01-14 14:31:38 +0100465 err = mmc_load_image_raw_partition(spl_image, bootdev,
466 mmc, raw_part,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500467 raw_sect);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200468 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200469 return err;
Dalon Westergreen949123e2017-02-10 17:15:35 -0800470#endif
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200471#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohár2e0429b2022-01-14 14:31:38 +0100472 err = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Baruch Siache42afd82020-01-15 09:08:08 +0200473 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200474 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200475 return err;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200476#endif
Guillaume GARDET86a0df72016-02-18 18:17:36 +0100477 /* If RAW mode fails, try FS mode. */
Pali Rohár718d1c72023-01-21 15:13:08 +0100478#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200479 case MMCSD_MODE_FS:
480 debug("spl: mmc boot mode: fs\n");
481
Pali Rohár2e0429b2022-01-14 14:31:38 +0100482 err = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200483 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200484 return err;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200485
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200486 break;
Pali Rohár718d1c72023-01-21 15:13:08 +0100487#endif
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100488#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200489 default:
490 puts("spl: mmc: wrong boot mode\n");
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100491#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200492 }
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200493
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200494 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400495}
Simon Glass0fed9c72016-09-24 18:20:00 -0600496
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500497int spl_mmc_load_image(struct spl_image_info *spl_image,
498 struct spl_boot_device *bootdev)
499{
500 return spl_mmc_load(spl_image, bootdev,
501#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
502 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
503#else
504 NULL,
505#endif
506#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +0200507 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500508#else
509 0,
510#endif
511#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
512 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
513#else
514 0);
515#endif
516}
517
Simon Glassebc4ef62016-11-30 15:30:50 -0700518SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
519SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
520SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);