blob: 8d588d13a365cae687437a58606069f27509d465 [file] [log] [blame]
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04001/*
2 * (C) Copyright 2010
3 * Texas Instruments, <www.ti.com>
4 *
5 * Aneesh V <aneesh@ti.com>
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04008 */
9#include <common.h>
Simon Glassdc3dedf2015-06-23 15:38:51 -060010#include <dm.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070011#include <spl.h>
Paul Kocialkowski91199f42015-04-27 10:20:22 +020012#include <linux/compiler.h>
Nikita Kiryanov36afd452015-11-08 17:11:49 +020013#include <errno.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040014#include <asm/u-boot.h>
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020015#include <errno.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040016#include <mmc.h>
Tom Rinie4c444b2013-06-28 14:43:01 -040017#include <image.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040018
19DECLARE_GLOBAL_DATA_PTR;
20
Simon Glass96debd12016-02-22 22:55:58 -070021static int mmc_load_legacy(struct mmc *mmc, ulong sector,
22 struct image_header *header)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040023{
Peter Korsgaard2e222102013-03-21 04:55:17 +000024 u32 image_size_sectors;
Simon Glass96debd12016-02-22 22:55:58 -070025 unsigned long count;
Tom Rinie4c444b2013-06-28 14:43:01 -040026
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040027 spl_parse_image_header(header);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040028 /* convert size to sectors - round up */
Tom Rinif0881252012-08-14 10:25:15 -070029 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
Paul Kocialkowski91199f42015-04-27 10:20:22 +020030 mmc->read_bl_len;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040031
32 /* Read the header too to avoid extra memcpy */
Stephen Warren7c4213f2015-12-07 11:38:48 -070033 count = mmc->block_dev.block_read(&mmc->block_dev, sector,
34 image_size_sectors,
Gong Qianyu7ef4c452015-07-30 14:07:12 +080035 (void *)(ulong)spl_image.load_addr);
Simon Glassfdfa39d2015-06-23 15:38:47 -060036 debug("read %x sectors to %x\n", image_size_sectors,
37 spl_image.load_addr);
Simon Glass96debd12016-02-22 22:55:58 -070038 if (count != image_size_sectors)
39 return -EIO;
40
41 return 0;
42}
43
Simon Glass96debd12016-02-22 22:55:58 -070044static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
45 ulong count, void *buf)
46{
47 struct mmc *mmc = load->dev;
48
49 return mmc->block_dev.block_read(&mmc->block_dev, sector, count, buf);
50}
Simon Glass96debd12016-02-22 22:55:58 -070051
52static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
53{
54 unsigned long count;
55 struct image_header *header;
56 int ret = 0;
57
58 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
59 sizeof(struct image_header));
60
61 /* read image header to find the image size & load address */
62 count = mmc->block_dev.block_read(&mmc->block_dev, sector, 1, header);
63 debug("hdr read sector %lx, count=%lu\n", sector, count);
64 if (count == 0) {
65 ret = -EIO;
66 goto end;
67 }
68
Masahiro Yamada4976f482016-03-16 12:10:00 +090069 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
70 image_get_magic(header) == FDT_MAGIC) {
Simon Glass96debd12016-02-22 22:55:58 -070071 struct spl_load_info load;
72
73 debug("Found FIT\n");
74 load.dev = mmc;
75 load.priv = NULL;
76 load.bl_len = mmc->read_bl_len;
77 load.read = h_spl_load_read;
78 ret = spl_load_simple_fit(&load, sector, header);
Masahiro Yamada4976f482016-03-16 12:10:00 +090079 } else {
80 ret = mmc_load_legacy(mmc, sector, header);
Simon Glass96debd12016-02-22 22:55:58 -070081 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040082
83end:
Simon Glass96debd12016-02-22 22:55:58 -070084 if (ret) {
Paul Burton8112f5f2013-09-04 16:12:24 +010085#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam5bbf4092016-04-20 23:12:04 -030086 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010087#endif
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020088 return -1;
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020089 }
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020090
91 return 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040092}
93
Nikita Kiryanova1e56cf2015-11-08 17:11:54 +020094int spl_mmc_get_device_index(u32 boot_device)
95{
96 switch (boot_device) {
97 case BOOT_DEVICE_MMC1:
98 return 0;
99 case BOOT_DEVICE_MMC2:
100 case BOOT_DEVICE_MMC2_2:
101 return 1;
102 }
103
104#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
105 printf("spl: unsupported mmc boot device.\n");
106#endif
107
108 return -ENODEV;
109}
110
Simon Glass99c7a512015-12-02 10:59:12 -0700111static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200112{
Simon Glassb4857aa2015-12-02 10:59:13 -0700113#ifdef CONFIG_DM_MMC
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200114 struct udevice *dev;
Simon Glassb4857aa2015-12-02 10:59:13 -0700115#endif
Nikita Kiryanova1e56cf2015-11-08 17:11:54 +0200116 int err, mmc_dev;
117
118 mmc_dev = spl_mmc_get_device_index(boot_device);
119 if (mmc_dev < 0)
120 return mmc_dev;
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200121
122 err = mmc_initialize(NULL);
123 if (err) {
124#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
125 printf("spl: could not initialize mmc. error: %d\n", err);
126#endif
127 return err;
128 }
129
Simon Glassb4857aa2015-12-02 10:59:13 -0700130#ifdef CONFIG_DM_MMC
Nikita Kiryanova1e56cf2015-11-08 17:11:54 +0200131 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
Simon Glassb4857aa2015-12-02 10:59:13 -0700132 if (!err)
133 *mmcp = mmc_get_mmc_dev(dev);
134#else
135 *mmcp = find_mmc_device(mmc_dev);
136 err = *mmcp ? 0 : -ENODEV;
137#endif
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200138 if (err) {
139#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
140 printf("spl: could not find mmc device. error: %d\n", err);
141#endif
142 return err;
143 }
144
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200145 return 0;
146}
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200147
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100148#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
149static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
150{
151 disk_partition_t info;
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200152 int err;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100153
Simon Glass3e8bd462016-02-29 15:25:48 -0700154 err = part_get_info(&mmc->block_dev, partition, &info);
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200155 if (err) {
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100156#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200157 puts("spl: partition error\n");
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100158#endif
159 return -1;
160 }
161
Stefan Roese4bfcc542015-07-20 11:20:39 +0200162#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
163 return mmc_load_image_raw_sector(mmc, info.start +
164 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
165#else
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100166 return mmc_load_image_raw_sector(mmc, info.start);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200167#endif
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100168}
Nikita Kiryanovd074ebb2015-11-08 17:11:46 +0200169#else
170#define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
171static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
172{
173 return -ENOSYS;
174}
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100175#endif
176
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000177#ifdef CONFIG_SPL_OS_BOOT
178static int mmc_load_image_raw_os(struct mmc *mmc)
179{
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200180 unsigned long count;
Lokesh Vutla811906a2016-04-11 15:24:50 +0530181 int ret;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200182
Stephen Warren7c4213f2015-12-07 11:38:48 -0700183 count = mmc->block_dev.block_read(&mmc->block_dev,
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200184 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
185 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
186 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
187 if (count == 0) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100188#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam5bbf4092016-04-20 23:12:04 -0300189 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100190#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000191 return -1;
192 }
193
Lokesh Vutla811906a2016-04-11 15:24:50 +0530194 ret = mmc_load_image_raw_sector(mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200195 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla811906a2016-04-11 15:24:50 +0530196 if (ret)
197 return ret;
198
199 if (spl_image.os != IH_OS_LINUX) {
200 puts("Expected Linux image is not found. Trying to start U-boot\n");
201 return -ENOENT;
202 }
203
204 return 0;
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000205}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200206#else
207int spl_start_uboot(void)
208{
209 return 1;
210}
211static int mmc_load_image_raw_os(struct mmc *mmc)
212{
213 return -ENOSYS;
214}
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000215#endif
216
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200217#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
218int spl_mmc_do_fs_boot(struct mmc *mmc)
219{
220 int err = -ENOSYS;
221
222#ifdef CONFIG_SPL_FAT_SUPPORT
223 if (!spl_start_uboot()) {
224 err = spl_load_image_fat_os(&mmc->block_dev,
225 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
226 if (!err)
227 return err;
228 }
229#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
230 err = spl_load_image_fat(&mmc->block_dev,
231 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
232 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
233 if (!err)
234 return err;
235#endif
236#endif
237#ifdef CONFIG_SPL_EXT_SUPPORT
238 if (!spl_start_uboot()) {
239 err = spl_load_image_ext_os(&mmc->block_dev,
240 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
241 if (!err)
242 return err;
243 }
244#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
245 err = spl_load_image_ext(&mmc->block_dev,
246 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
247 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
248 if (!err)
249 return err;
250#endif
251#endif
252
253#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
254 err = -ENOENT;
255#endif
256
257 return err;
258}
259#else
260int spl_mmc_do_fs_boot(struct mmc *mmc)
261{
262 return -ENOSYS;
263}
264#endif
265
Nikita Kiryanova1e56cf2015-11-08 17:11:54 +0200266int spl_mmc_load_image(u32 boot_device)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400267{
Simon Glassd773a002015-12-02 10:59:11 -0700268 struct mmc *mmc = NULL;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400269 u32 boot_mode;
Simon Glassdc3dedf2015-06-23 15:38:51 -0600270 int err = 0;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200271 __maybe_unused int part;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400272
Nikita Kiryanova1e56cf2015-11-08 17:11:54 +0200273 err = spl_mmc_find_device(&mmc, boot_device);
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200274 if (err)
275 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400276
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200277 err = mmc_init(mmc);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400278 if (err) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100279#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200280 printf("spl: mmc init failed with error: %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100281#endif
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200282 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400283 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000284
Tom Rini37189a12012-08-14 09:19:44 -0700285 boot_mode = spl_boot_mode();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200286 err = -EINVAL;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200287 switch (boot_mode) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200288 case MMCSD_MODE_EMMCBOOT:
289 /*
290 * We need to check what the partition is configured to.
291 * 1 and 2 match up to boot0 / boot1 and 7 is user data
292 * which is the first physical partition (0).
293 */
294 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
295
296 if (part == 7)
297 part = 0;
298
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200299 err = mmc_switch_part(0, part);
300 if (err) {
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200301#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
302 puts("spl: mmc partition switch failed\n");
303#endif
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200304 return err;
Nikita Kiryanov83cdf6f2015-11-08 17:11:48 +0200305 }
306 /* Fall through */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200307 case MMCSD_MODE_RAW:
308 debug("spl: mmc boot mode: raw\n");
309
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200310 if (!spl_start_uboot()) {
311 err = mmc_load_image_raw_os(mmc);
312 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200313 return err;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200314 }
Nikita Kiryanovd074ebb2015-11-08 17:11:46 +0200315
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100316 err = mmc_load_image_raw_partition(mmc,
317 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200318 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200319 return err;
Nikita Kiryanovd074ebb2015-11-08 17:11:46 +0200320#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200321 err = mmc_load_image_raw_sector(mmc,
322 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
323 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200324 return err;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200325#endif
Guillaume GARDET86a0df72016-02-18 18:17:36 +0100326 /* If RAW mode fails, try FS mode. */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200327 case MMCSD_MODE_FS:
328 debug("spl: mmc boot mode: fs\n");
329
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200330 err = spl_mmc_do_fs_boot(mmc);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200331 if (!err)
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200332 return err;
Nikita Kiryanovf52b7292015-11-08 17:11:47 +0200333
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200334 break;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200335 case MMCSD_MODE_UNDEFINED:
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100336#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200337 default:
338 puts("spl: mmc: wrong boot mode\n");
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100339#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200340 }
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200341
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200342 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400343}