blob: 02ad32a23e018d9db935848a9dc9ab67f45a537c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04002/*
3 * (C) Copyright 2010
4 * Texas Instruments, <www.ti.com>
5 *
6 * Aneesh V <aneesh@ti.com>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04007 */
8#include <common.h>
Simon Glassdc3dedf2015-06-23 15:38:51 -06009#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060011#include <part.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070012#include <spl.h>
Paul Kocialkowski91199f42015-04-27 10:20:22 +020013#include <linux/compiler.h>
Nikita Kiryanov36afd452015-11-08 17:11:49 +020014#include <errno.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040015#include <asm/u-boot.h>
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020016#include <errno.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040017#include <mmc.h>
Tom Rinie4c444b2013-06-28 14:43:01 -040018#include <image.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040019
Pali Rohár2e0429b2022-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 Glassf3543e62022-09-06 20:26:52 -060023 ulong sector, struct legacy_img_hdr *header)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040024{
Pali Rohár5fce2872021-07-23 11:14:27 +020025 u32 image_offset_sectors;
Peter Korsgaard2e222102013-03-21 04:55:17 +000026 u32 image_size_sectors;
Simon Glass96debd12016-02-22 22:55:58 -070027 unsigned long count;
Pali Rohár5fce2872021-07-23 11:14:27 +020028 u32 image_offset;
Marek Vasut7e0f2262016-04-29 00:44:54 +020029 int ret;
Tom Rinie4c444b2013-06-28 14:43:01 -040030
Pali Rohár2e0429b2022-01-14 14:31:38 +010031 ret = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +020032 if (ret)
33 return ret;
34
Pali Rohár5fce2872021-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 Schwarz9ea5c6e2011-09-14 15:33:34 -040040 /* convert size to sectors - round up */
Simon Glass2a2ee2a2016-09-24 18:20:13 -060041 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
Paul Kocialkowski91199f42015-04-27 10:20:22 +020042 mmc->read_bl_len;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040043
44 /* Read the header too to avoid extra memcpy */
Pali Rohár5fce2872021-07-23 11:14:27 +020045 count = blk_dread(mmc_get_blk_desc(mmc),
46 sector + image_offset_sectors,
47 image_size_sectors,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060048 (void *)(ulong)spl_image->load_addr);
Andre Przywara11e14792017-01-02 11:48:31 +000049 debug("read %x sectors to %lx\n", image_size_sectors,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060050 spl_image->load_addr);
Simon Glass96debd12016-02-22 22:55:58 -070051 if (count != image_size_sectors)
52 return -EIO;
53
Pali Rohár5fce2872021-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 Glass96debd12016-02-22 22:55:58 -070059 return 0;
60}
61
Simon Glass96debd12016-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 Glassef5609c2016-05-14 14:03:06 -060067 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
Simon Glass96debd12016-02-22 22:55:58 -070068}
Simon Glass96debd12016-02-22 22:55:58 -070069
Baruch Siache42afd82020-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 Kimb016b582017-09-19 13:29:45 +090080static __maybe_unused
81int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010082 struct spl_boot_device *bootdev,
Seung-Woo Kimb016b582017-09-19 13:29:45 +090083 struct mmc *mmc, unsigned long sector)
Simon Glass96debd12016-02-22 22:55:58 -070084{
85 unsigned long count;
Simon Glassf3543e62022-09-06 20:26:52 -060086 struct legacy_img_hdr *header;
Marek Vasut04ce5422018-08-14 11:27:02 +020087 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glass96debd12016-02-22 22:55:58 -070088 int ret = 0;
89
Marek Vasut04ce5422018-08-14 11:27:02 +020090 header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
Simon Glass96debd12016-02-22 22:55:58 -070091
92 /* read image header to find the image size & load address */
Marek Vasut04ce5422018-08-14 11:27:02 +020093 count = blk_dread(bd, sector, 1, header);
Simon Glass96debd12016-02-22 22:55:58 -070094 debug("hdr read sector %lx, count=%lu\n", sector, count);
95 if (count == 0) {
96 ret = -EIO;
97 goto end;
98 }
99
Masahiro Yamada4976f482016-03-16 12:10:00 +0900100 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
101 image_get_magic(header) == FDT_MAGIC) {
Simon Glass96debd12016-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 Vutlaeafd5412016-05-24 10:34:38 +0530107 load.filename = NULL;
Simon Glass96debd12016-02-22 22:55:58 -0700108 load.bl_len = mmc->read_bl_len;
109 load.read = h_spl_load_read;
Simon Glassf4d7d852016-09-24 18:20:16 -0600110 ret = spl_load_simple_fit(spl_image, &load, sector, header);
Peng Fandd7d0912019-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 Yamada4976f482016-03-16 12:10:00 +0900121 } else {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100122 ret = mmc_load_legacy(spl_image, bootdev, mmc, sector, header);
Simon Glass96debd12016-02-22 22:55:58 -0700123 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400124
125end:
Simon Glass96debd12016-02-22 22:55:58 -0700126 if (ret) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100127#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam5bbf4092016-04-20 23:12:04 -0300128 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100129#endif
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200130 return -1;
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200131 }
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200132
133 return 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400134}
135
Masahiro Yamada207d8b32017-06-22 16:52:36 +0900136static int spl_mmc_get_device_index(u32 boot_device)
Nikita Kiryanova1e56cf2015-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 Glass99c7a512015-12-02 10:59:12 -0700153static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200154{
Nikita Kiryanova1e56cf2015-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 Kiryanov4188ba32015-11-08 17:11:44 +0200160
Lokesh Vutla80f02012019-09-09 14:40:36 +0530161#if CONFIG_IS_ENABLED(DM_MMC)
162 err = mmc_init_device(mmc_dev);
163#else
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200164 err = mmc_initialize(NULL);
Lokesh Vutla80f02012019-09-09 14:40:36 +0530165#endif /* DM_MMC */
Nikita Kiryanov4188ba32015-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 Glassb4857aa2015-12-02 10:59:13 -0700172 *mmcp = find_mmc_device(mmc_dev);
173 err = *mmcp ? 0 : -ENODEV;
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200174 if (err) {
175#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Alex Kiernanaa608b62018-09-18 08:11:55 +0000176 printf("spl: could not find mmc device %d. error: %d\n",
177 mmc_dev, err);
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200178#endif
179 return err;
180 }
181
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200182 return 0;
183}
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200184
Dalon Westergreen949123e2017-02-10 17:15:35 -0800185#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600186static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100187 struct spl_boot_device *bootdev,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500188 struct mmc *mmc, int partition,
189 unsigned long sector)
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100190{
Simon Glass05289792020-05-10 11:39:57 -0600191 struct disk_partition info;
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200192 int err;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100193
Dalon Westergreenf0fb4fa2017-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 Denk0a50b3c2021-09-27 17:42:38 +0200201 if (info.sys_ind ==
Dalon Westergreenf0fb4fa2017-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 Glass1e2b3ef2016-07-05 17:10:18 -0600209 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200210 if (err) {
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100211#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200212 puts("spl: partition error\n");
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100213#endif
214 return -1;
215 }
216
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200217#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohár2e0429b2022-01-14 14:31:38 +0100218 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200219#else
Pali Rohár2e0429b2022-01-14 14:31:38 +0100220 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200221#endif
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100222}
223#endif
224
Alexandru Gagniuc22eb1522021-12-30 10:39:59 -0600225#if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600226static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100227 struct spl_boot_device *bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600228 struct mmc *mmc)
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000229{
Lokesh Vutla811906a2016-04-11 15:24:50 +0530230 int ret;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200231
Elena Popa86b1aad2023-08-08 16:42:15 +0300232#if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
York Sun7267fbf2018-06-26 09:44:39 -0700233 unsigned long count;
234
Simon Glass87bce4e2016-06-12 23:30:32 -0600235 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200236 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
237 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
Simon Glass9cbdc3a2023-09-26 08:14:17 -0600238 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
Tom Rini9585dd32017-08-14 21:01:30 -0400239 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100240#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam5bbf4092016-04-20 23:12:04 -0300241 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100242#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000243 return -1;
244 }
York Sun7267fbf2018-06-26 09:44:39 -0700245#endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000246
Pali Rohár2e0429b2022-01-14 14:31:38 +0100247 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200248 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla811906a2016-04-11 15:24:50 +0530249 if (ret)
250 return ret;
251
Alexandru Gagniuc5f6e5c32021-07-15 14:19:23 -0500252 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
Michal Simek1be82af2023-05-17 09:17:16 +0200253 puts("Expected image is not found. Trying to start U-Boot\n");
Lokesh Vutla811906a2016-04-11 15:24:50 +0530254 return -ENOENT;
255 }
256
257 return 0;
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000258}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200259#else
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600260static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100261 struct spl_boot_device *bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600262 struct mmc *mmc)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200263{
264 return -ENOSYS;
265}
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000266#endif
267
Alexandru Gagniuc22eb1522021-12-30 10:39:59 -0600268#ifndef CONFIG_SPL_OS_BOOT
269int spl_start_uboot(void)
270{
271 return 1;
272}
273#endif
274
Pali Rohár718d1c72023-01-21 15:13:08 +0100275#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Pali Rohár2e0429b2022-01-14 14:31:38 +0100276static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
277 struct spl_boot_device *bootdev,
278 struct mmc *mmc,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500279 const char *filename)
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200280{
281 int err = -ENOSYS;
282
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400283 __maybe_unused int partition = CONFIG_SYS_MMCSD_FS_BOOT_PARTITION;
284
285#if CONFIG_SYS_MMCSD_FS_BOOT_PARTITION == -1
286 {
287 struct disk_partition info;
288 debug("Checking for the first MBR bootable partition\n");
289 for (int type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
290 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
291 if (err)
292 continue;
293 debug("Partition %d is of type %d and bootable=%d\n", type_part, info.sys_ind, info.bootable);
294 if (info.bootable != 0) {
295 debug("Partition %d is bootable, using it\n", type_part);
296 partition = type_part;
297 break;
298 }
299 }
300 printf("Using first bootable partition: %d\n", partition);
301 if (partition == CONFIG_SYS_MMCSD_FS_BOOT_PARTITION) {
302 return -ENOSYS;
303 }
304 }
305#endif
306
Tien Fong Chee0c3a9ed2019-01-23 14:20:03 +0800307#ifdef CONFIG_SPL_FS_FAT
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200308 if (!spl_start_uboot()) {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100309 err = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400310 partition);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200311 if (!err)
312 return err;
313 }
314#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohár2e0429b2022-01-14 14:31:38 +0100315 err = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400316 partition,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500317 filename);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200318 if (!err)
319 return err;
320#endif
321#endif
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800322#ifdef CONFIG_SPL_FS_EXT4
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200323 if (!spl_start_uboot()) {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100324 err = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400325 partition);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200326 if (!err)
327 return err;
328 }
329#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Pali Rohár2e0429b2022-01-14 14:31:38 +0100330 err = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
Jérôme Carretero5fa973e2022-03-15 16:34:51 -0400331 partition,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500332 filename);
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200333 if (!err)
334 return err;
335#endif
336#endif
337
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800338#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200339 err = -ENOENT;
340#endif
341
342 return err;
343}
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200344#endif
345
Andre Przywara59073572021-07-12 11:06:49 +0100346u32 __weak spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Lukasz Majewskid695d662018-02-03 08:29:52 +0100347{
Tien Fong Cheef4b40922019-01-23 14:20:05 +0800348#if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
Lukasz Majewskid695d662018-02-03 08:29:52 +0100349 return MMCSD_MODE_FS;
350#elif defined(CONFIG_SUPPORT_EMMC_BOOT)
351 return MMCSD_MODE_EMMCBOOT;
352#else
353 return MMCSD_MODE_RAW;
354#endif
355}
356
Patrick Delaunay35a66962018-03-20 10:54:51 +0100357#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +0200358int __weak spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay35a66962018-03-20 10:54:51 +0100359{
360 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
361}
362#endif
363
Faiz Abbascf008252020-02-26 13:44:35 +0530364unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
365 unsigned long raw_sect)
Peng Fan30ecc9c2019-09-23 10:18:40 +0800366{
Faiz Abbascf008252020-02-26 13:44:35 +0530367 return raw_sect;
Peng Fan30ecc9c2019-09-23 10:18:40 +0800368}
369
Marek Vasut9b191592021-07-03 04:55:32 +0200370int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
371{
372 int part;
373#ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
374 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
375#else
376 /*
377 * We need to check what the partition is configured to.
378 * 1 and 2 match up to boot0 / boot1 and 7 is user data
379 * which is the first physical partition (0).
380 */
Pali Rohár8b882062023-03-11 11:44:27 +0100381 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
Marek Vasut9b191592021-07-03 04:55:32 +0200382 if (part == 7)
383 part = 0;
384#endif
385 return part;
386}
387
388int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
389{
390 return default_spl_mmc_emmc_boot_partition(mmc);
391}
392
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200393static int spl_mmc_get_mmc_devnum(struct mmc *mmc)
394{
395 struct blk_desc *block_dev;
396#if !CONFIG_IS_ENABLED(BLK)
397 block_dev = &mmc->block_dev;
398#else
Sean Andersona0733b32023-10-07 21:47:48 -0400399 block_dev = mmc_get_blk_desc(mmc);
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200400#endif
401 return block_dev->devnum;
402}
403
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500404int spl_mmc_load(struct spl_image_info *spl_image,
405 struct spl_boot_device *bootdev,
406 const char *filename,
407 int raw_part,
408 unsigned long raw_sect)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400409{
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500410 static struct mmc *mmc;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400411 u32 boot_mode;
Simon Glassdc3dedf2015-06-23 15:38:51 -0600412 int err = 0;
Baruch Siache42afd82020-01-15 09:08:08 +0200413 __maybe_unused int part = 0;
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200414 int mmc_dev;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400415
Harald Seilerbf28d9a2022-07-11 14:35:32 +0200416 /* Perform peripheral init only once for an mmc device */
417 mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
418 if (!mmc || spl_mmc_get_mmc_devnum(mmc) != mmc_dev) {
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500419 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
420 if (err)
421 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400422
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500423 err = mmc_init(mmc);
424 if (err) {
425 mmc = NULL;
Paul Burton8112f5f2013-09-04 16:12:24 +0100426#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500427 printf("spl: mmc init failed with error: %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100428#endif
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500429 return err;
430 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400431 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000432
Andre Przywara59073572021-07-12 11:06:49 +0100433 boot_mode = spl_mmc_boot_mode(mmc, bootdev->boot_device);
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200434 err = -EINVAL;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200435 switch (boot_mode) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200436 case MMCSD_MODE_EMMCBOOT:
Marek Vasut9b191592021-07-03 04:55:32 +0200437 part = spl_mmc_emmc_boot_partition(mmc);
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200438
Mans Rullgard92439902019-09-03 16:17:17 +0200439 if (CONFIG_IS_ENABLED(MMC_TINY))
440 err = mmc_switch_part(mmc, part);
441 else
442 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Marek Vasutb5b838f2016-12-01 02:06:33 +0100443
Mans Rullgard92439902019-09-03 16:17:17 +0200444 if (err) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200445#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Mans Rullgard92439902019-09-03 16:17:17 +0200446 puts("spl: mmc partition switch failed\n");
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200447#endif
Mans Rullgard92439902019-09-03 16:17:17 +0200448 return err;
449 }
450 /* Fall through */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200451 case MMCSD_MODE_RAW:
452 debug("spl: mmc boot mode: raw\n");
453
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200454 if (!spl_start_uboot()) {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100455 err = mmc_load_image_raw_os(spl_image, bootdev, mmc);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200456 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200457 return err;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200458 }
Peng Fana335f802019-10-23 01:43:30 +0000459
Faiz Abbascf008252020-02-26 13:44:35 +0530460 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
Peng Fana335f802019-10-23 01:43:30 +0000461
Dalon Westergreen949123e2017-02-10 17:15:35 -0800462#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Pali Rohár2e0429b2022-01-14 14:31:38 +0100463 err = mmc_load_image_raw_partition(spl_image, bootdev,
464 mmc, raw_part,
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500465 raw_sect);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200466 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200467 return err;
Dalon Westergreen949123e2017-02-10 17:15:35 -0800468#endif
Semen Protsenko38fed8a2016-11-16 19:19:05 +0200469#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
Pali Rohár2e0429b2022-01-14 14:31:38 +0100470 err = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
Baruch Siache42afd82020-01-15 09:08:08 +0200471 raw_sect + spl_mmc_raw_uboot_offset(part));
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200472 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200473 return err;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200474#endif
Guillaume GARDET86a0df72016-02-18 18:17:36 +0100475 /* If RAW mode fails, try FS mode. */
Pali Rohár718d1c72023-01-21 15:13:08 +0100476#ifdef CONFIG_SYS_MMCSD_FS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200477 case MMCSD_MODE_FS:
478 debug("spl: mmc boot mode: fs\n");
479
Pali Rohár2e0429b2022-01-14 14:31:38 +0100480 err = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200481 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200482 return err;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200483
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200484 break;
Pali Rohár718d1c72023-01-21 15:13:08 +0100485#endif
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100486#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200487 default:
488 puts("spl: mmc: wrong boot mode\n");
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100489#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200490 }
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200491
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200492 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400493}
Simon Glass0fed9c72016-09-24 18:20:00 -0600494
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500495int spl_mmc_load_image(struct spl_image_info *spl_image,
496 struct spl_boot_device *bootdev)
497{
498 return spl_mmc_load(spl_image, bootdev,
499#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
500 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
501#else
502 NULL,
503#endif
504#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +0200505 spl_mmc_boot_partition(bootdev->boot_device),
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500506#else
507 0,
508#endif
509#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
510 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
511#else
512 0);
513#endif
514}
515
Simon Glassebc4ef62016-11-30 15:30:50 -0700516SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
517SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
518SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);