blob: d8058d63999657c2d73b68e13ffd7776a518655f [file] [log] [blame]
Simon Schwarze21d2d82011-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 Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Simon Schwarze21d2d82011-09-14 15:33:34 -04008 */
9#include <common.h>
Simon Glassf22851a2015-06-23 15:38:51 -060010#include <dm.h>
Tom Rini28591df2012-08-13 12:03:19 -070011#include <spl.h>
Paul Kocialkowski0d139722015-04-27 10:20:22 +020012#include <linux/compiler.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020013#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040014#include <asm/u-boot.h>
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020015#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040016#include <mmc.h>
Tom Rini1530e8a2013-06-28 14:43:01 -040017#include <image.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040018
19DECLARE_GLOBAL_DATA_PTR;
20
Simon Glassb52c7fc2016-02-22 22:55:58 -070021static int mmc_load_legacy(struct mmc *mmc, ulong sector,
22 struct image_header *header)
23{
24 u32 image_size_sectors;
25 unsigned long count;
Marek Vasut02266e22016-04-29 00:44:54 +020026 int ret;
27
28 ret = spl_parse_image_header(header);
29 if (ret)
30 return ret;
Simon Glassb52c7fc2016-02-22 22:55:58 -070031
Simon Glassb52c7fc2016-02-22 22:55:58 -070032 /* convert size to sectors - round up */
33 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
34 mmc->read_bl_len;
35
36 /* Read the header too to avoid extra memcpy */
37 count = mmc->block_dev.block_read(&mmc->block_dev, sector,
38 image_size_sectors,
39 (void *)(ulong)spl_image.load_addr);
40 debug("read %x sectors to %x\n", image_size_sectors,
41 spl_image.load_addr);
42 if (count != image_size_sectors)
43 return -EIO;
44
45 return 0;
46}
47
Simon Glassb52c7fc2016-02-22 22:55:58 -070048static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
49 ulong count, void *buf)
50{
51 struct mmc *mmc = load->dev;
52
53 return mmc->block_dev.block_read(&mmc->block_dev, sector, count, buf);
54}
Simon Glassb52c7fc2016-02-22 22:55:58 -070055
Paul Kocialkowski17675c82014-11-08 23:14:56 +010056static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040057{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020058 unsigned long count;
Peter Korsgaardf2471292013-03-21 04:55:17 +000059 struct image_header *header;
Simon Glassb52c7fc2016-02-22 22:55:58 -070060 int ret = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040061
62 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
Paul Kocialkowski0d139722015-04-27 10:20:22 +020063 sizeof(struct image_header));
Simon Schwarze21d2d82011-09-14 15:33:34 -040064
65 /* read image header to find the image size & load address */
Stephen Warrene73f2962015-12-07 11:38:48 -070066 count = mmc->block_dev.block_read(&mmc->block_dev, sector, 1, header);
Simon Glassb52c7fc2016-02-22 22:55:58 -070067 debug("hdr read sector %lx, count=%lu\n", sector, count);
68 if (count == 0) {
69 ret = -EIO;
Simon Schwarze21d2d82011-09-14 15:33:34 -040070 goto end;
Simon Glassb52c7fc2016-02-22 22:55:58 -070071 }
Simon Schwarze21d2d82011-09-14 15:33:34 -040072
Masahiro Yamada802f1c32016-03-16 12:10:00 +090073 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
74 image_get_magic(header) == FDT_MAGIC) {
Simon Glassb52c7fc2016-02-22 22:55:58 -070075 struct spl_load_info load;
76
77 debug("Found FIT\n");
78 load.dev = mmc;
79 load.priv = NULL;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +053080 load.filename = NULL;
Simon Glassb52c7fc2016-02-22 22:55:58 -070081 load.bl_len = mmc->read_bl_len;
82 load.read = h_spl_load_read;
83 ret = spl_load_simple_fit(&load, sector, header);
Masahiro Yamada802f1c32016-03-16 12:10:00 +090084 } else {
85 ret = mmc_load_legacy(mmc, sector, header);
Simon Glass5614db82015-06-23 15:38:47 -060086 }
Tom Rini1530e8a2013-06-28 14:43:01 -040087
Simon Schwarze21d2d82011-09-14 15:33:34 -040088end:
Simon Glassb52c7fc2016-02-22 22:55:58 -070089 if (ret) {
Paul Burtona8015fd2013-09-04 16:12:24 +010090#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -030091 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010092#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020093 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020094 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020095
96 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040097}
98
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +020099int spl_mmc_get_device_index(u32 boot_device)
100{
101 switch (boot_device) {
102 case BOOT_DEVICE_MMC1:
103 return 0;
104 case BOOT_DEVICE_MMC2:
105 case BOOT_DEVICE_MMC2_2:
106 return 1;
107 }
108
109#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
110 printf("spl: unsupported mmc boot device.\n");
111#endif
112
113 return -ENODEV;
114}
115
Simon Glass4faf63b2015-12-02 10:59:12 -0700116static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200117{
Simon Glass51ace322015-12-02 10:59:13 -0700118#ifdef CONFIG_DM_MMC
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200119 struct udevice *dev;
Simon Glass51ace322015-12-02 10:59:13 -0700120#endif
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200121 int err, mmc_dev;
122
123 mmc_dev = spl_mmc_get_device_index(boot_device);
124 if (mmc_dev < 0)
125 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200126
127 err = mmc_initialize(NULL);
128 if (err) {
129#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
130 printf("spl: could not initialize mmc. error: %d\n", err);
131#endif
132 return err;
133 }
134
Simon Glass51ace322015-12-02 10:59:13 -0700135#ifdef CONFIG_DM_MMC
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200136 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
Simon Glass51ace322015-12-02 10:59:13 -0700137 if (!err)
138 *mmcp = mmc_get_mmc_dev(dev);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200139#else
Simon Glass51ace322015-12-02 10:59:13 -0700140 *mmcp = find_mmc_device(mmc_dev);
141 err = *mmcp ? 0 : -ENODEV;
142#endif
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200143 if (err) {
144#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Simon Glass51ace322015-12-02 10:59:13 -0700145 printf("spl: could not find mmc device. error: %d\n", err);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200146#endif
147 return err;
148 }
149
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200150 return 0;
151}
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200152
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100153#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
154static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
155{
156 disk_partition_t info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200157 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100158
Simon Glassb89a8442016-02-29 15:25:48 -0700159 err = part_get_info(&mmc->block_dev, partition, &info);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200160 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100161#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200162 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100163#endif
164 return -1;
165 }
166
Stefan Roesed0503cd2015-07-20 11:20:39 +0200167#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
168 return mmc_load_image_raw_sector(mmc, info.start +
169 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
170#else
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100171 return mmc_load_image_raw_sector(mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200172#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100173}
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200174#else
175#define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
176static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
177{
178 return -ENOSYS;
179}
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100180#endif
181
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000182#ifdef CONFIG_SPL_OS_BOOT
183static int mmc_load_image_raw_os(struct mmc *mmc)
184{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200185 unsigned long count;
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530186 int ret;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200187
Stephen Warrene73f2962015-12-07 11:38:48 -0700188 count = mmc->block_dev.block_read(&mmc->block_dev,
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200189 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
190 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
191 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
192 if (count == 0) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100193#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300194 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100195#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000196 return -1;
197 }
198
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530199 ret = mmc_load_image_raw_sector(mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200200 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530201 if (ret)
202 return ret;
203
204 if (spl_image.os != IH_OS_LINUX) {
205 puts("Expected Linux image is not found. Trying to start U-boot\n");
206 return -ENOENT;
207 }
208
209 return 0;
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000210}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200211#else
212int spl_start_uboot(void)
213{
214 return 1;
215}
216static int mmc_load_image_raw_os(struct mmc *mmc)
217{
218 return -ENOSYS;
219}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000220#endif
221
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200222#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
223int spl_mmc_do_fs_boot(struct mmc *mmc)
224{
225 int err = -ENOSYS;
226
227#ifdef CONFIG_SPL_FAT_SUPPORT
228 if (!spl_start_uboot()) {
229 err = spl_load_image_fat_os(&mmc->block_dev,
230 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
231 if (!err)
232 return err;
233 }
234#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
235 err = spl_load_image_fat(&mmc->block_dev,
236 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
237 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
238 if (!err)
239 return err;
240#endif
241#endif
242#ifdef CONFIG_SPL_EXT_SUPPORT
243 if (!spl_start_uboot()) {
244 err = spl_load_image_ext_os(&mmc->block_dev,
245 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
246 if (!err)
247 return err;
248 }
249#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
250 err = spl_load_image_ext(&mmc->block_dev,
251 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
252 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
253 if (!err)
254 return err;
255#endif
256#endif
257
258#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
259 err = -ENOENT;
260#endif
261
262 return err;
263}
264#else
265int spl_mmc_do_fs_boot(struct mmc *mmc)
266{
267 return -ENOSYS;
268}
269#endif
270
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200271int spl_mmc_load_image(u32 boot_device)
Simon Schwarze21d2d82011-09-14 15:33:34 -0400272{
Simon Glassa2b771c2015-12-02 10:59:11 -0700273 struct mmc *mmc = NULL;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400274 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600275 int err = 0;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200276 __maybe_unused int part;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400277
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200278 err = spl_mmc_find_device(&mmc, boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200279 if (err)
280 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400281
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200282 err = mmc_init(mmc);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400283 if (err) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100284#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200285 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100286#endif
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200287 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400288 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000289
Tom Rinia76ff952012-08-14 09:19:44 -0700290 boot_mode = spl_boot_mode();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200291 err = -EINVAL;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200292 switch (boot_mode) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200293 case MMCSD_MODE_EMMCBOOT:
294 /*
295 * We need to check what the partition is configured to.
296 * 1 and 2 match up to boot0 / boot1 and 7 is user data
297 * which is the first physical partition (0).
298 */
299 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
300
301 if (part == 7)
302 part = 0;
303
Simon Glass11f2bb62016-05-01 13:52:29 -0600304 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200305 if (err) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200306#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
307 puts("spl: mmc partition switch failed\n");
308#endif
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200309 return err;
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200310 }
311 /* Fall through */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200312 case MMCSD_MODE_RAW:
313 debug("spl: mmc boot mode: raw\n");
314
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200315 if (!spl_start_uboot()) {
316 err = mmc_load_image_raw_os(mmc);
317 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200318 return err;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200319 }
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200320
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100321 err = mmc_load_image_raw_partition(mmc,
322 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200323 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200324 return err;
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200325#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100326 err = mmc_load_image_raw_sector(mmc,
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600327 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200328 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200329 return err;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200330#endif
Guillaume GARDET834dd882016-02-18 18:17:36 +0100331 /* If RAW mode fails, try FS mode. */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200332 case MMCSD_MODE_FS:
333 debug("spl: mmc boot mode: fs\n");
334
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200335 err = spl_mmc_do_fs_boot(mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200336 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200337 return err;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200338
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200339 break;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200340 case MMCSD_MODE_UNDEFINED:
Paul Burtona8015fd2013-09-04 16:12:24 +0100341#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200342 default:
343 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100344#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200345 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200346
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200347 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400348}