blob: 7c7f32959b224a44b789bc8b4642c60b2e28655f [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 */
Simon Glass670d3042016-05-14 14:03:06 -060037 count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
38 (void *)(ulong)spl_image.load_addr);
Simon Glassb52c7fc2016-02-22 22:55:58 -070039 debug("read %x sectors to %x\n", image_size_sectors,
40 spl_image.load_addr);
41 if (count != image_size_sectors)
42 return -EIO;
43
44 return 0;
45}
46
Simon Glassb52c7fc2016-02-22 22:55:58 -070047static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
48 ulong count, void *buf)
49{
50 struct mmc *mmc = load->dev;
51
Simon Glass670d3042016-05-14 14:03:06 -060052 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
Simon Glassb52c7fc2016-02-22 22:55:58 -070053}
Simon Glassb52c7fc2016-02-22 22:55:58 -070054
Paul Kocialkowski17675c82014-11-08 23:14:56 +010055static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040056{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020057 unsigned long count;
Peter Korsgaardf2471292013-03-21 04:55:17 +000058 struct image_header *header;
Simon Glassb52c7fc2016-02-22 22:55:58 -070059 int ret = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040060
61 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
Paul Kocialkowski0d139722015-04-27 10:20:22 +020062 sizeof(struct image_header));
Simon Schwarze21d2d82011-09-14 15:33:34 -040063
64 /* read image header to find the image size & load address */
Simon Glass670d3042016-05-14 14:03:06 -060065 count = blk_dread(mmc_get_blk_desc(mmc), sector, 1, header);
Simon Glassb52c7fc2016-02-22 22:55:58 -070066 debug("hdr read sector %lx, count=%lu\n", sector, count);
67 if (count == 0) {
68 ret = -EIO;
Simon Schwarze21d2d82011-09-14 15:33:34 -040069 goto end;
Simon Glassb52c7fc2016-02-22 22:55:58 -070070 }
Simon Schwarze21d2d82011-09-14 15:33:34 -040071
Masahiro Yamada802f1c32016-03-16 12:10:00 +090072 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
73 image_get_magic(header) == FDT_MAGIC) {
Simon Glassb52c7fc2016-02-22 22:55:58 -070074 struct spl_load_info load;
75
76 debug("Found FIT\n");
77 load.dev = mmc;
78 load.priv = NULL;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +053079 load.filename = NULL;
Simon Glassb52c7fc2016-02-22 22:55:58 -070080 load.bl_len = mmc->read_bl_len;
81 load.read = h_spl_load_read;
82 ret = spl_load_simple_fit(&load, sector, header);
Masahiro Yamada802f1c32016-03-16 12:10:00 +090083 } else {
84 ret = mmc_load_legacy(mmc, sector, header);
Simon Glass5614db82015-06-23 15:38:47 -060085 }
Tom Rini1530e8a2013-06-28 14:43:01 -040086
Simon Schwarze21d2d82011-09-14 15:33:34 -040087end:
Simon Glassb52c7fc2016-02-22 22:55:58 -070088 if (ret) {
Paul Burtona8015fd2013-09-04 16:12:24 +010089#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -030090 puts("mmc_load_image_raw_sector: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010091#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020092 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020093 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020094
95 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040096}
97
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +020098int spl_mmc_get_device_index(u32 boot_device)
99{
100 switch (boot_device) {
101 case BOOT_DEVICE_MMC1:
102 return 0;
103 case BOOT_DEVICE_MMC2:
104 case BOOT_DEVICE_MMC2_2:
105 return 1;
106 }
107
108#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
109 printf("spl: unsupported mmc boot device.\n");
110#endif
111
112 return -ENODEV;
113}
114
Simon Glass4faf63b2015-12-02 10:59:12 -0700115static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200116{
Simon Glass51ace322015-12-02 10:59:13 -0700117#ifdef CONFIG_DM_MMC
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200118 struct udevice *dev;
Simon Glass51ace322015-12-02 10:59:13 -0700119#endif
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200120 int err, mmc_dev;
121
122 mmc_dev = spl_mmc_get_device_index(boot_device);
123 if (mmc_dev < 0)
124 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200125
126 err = mmc_initialize(NULL);
127 if (err) {
128#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
129 printf("spl: could not initialize mmc. error: %d\n", err);
130#endif
131 return err;
132 }
133
Simon Glass51ace322015-12-02 10:59:13 -0700134#ifdef CONFIG_DM_MMC
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200135 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
Simon Glass51ace322015-12-02 10:59:13 -0700136 if (!err)
137 *mmcp = mmc_get_mmc_dev(dev);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200138#else
Simon Glass51ace322015-12-02 10:59:13 -0700139 *mmcp = find_mmc_device(mmc_dev);
140 err = *mmcp ? 0 : -ENODEV;
141#endif
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200142 if (err) {
143#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Simon Glass51ace322015-12-02 10:59:13 -0700144 printf("spl: could not find mmc device. error: %d\n", err);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200145#endif
146 return err;
147 }
148
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200149 return 0;
150}
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200151
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100152#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
153static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
154{
155 disk_partition_t info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200156 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100157
Simon Glass55877f92016-07-05 17:10:18 -0600158 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200159 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100160#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200161 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100162#endif
163 return -1;
164 }
165
Stefan Roesed0503cd2015-07-20 11:20:39 +0200166#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
167 return mmc_load_image_raw_sector(mmc, info.start +
168 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
169#else
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100170 return mmc_load_image_raw_sector(mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200171#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100172}
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200173#else
174#define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
175static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
176{
177 return -ENOSYS;
178}
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100179#endif
180
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000181#ifdef CONFIG_SPL_OS_BOOT
182static int mmc_load_image_raw_os(struct mmc *mmc)
183{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200184 unsigned long count;
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530185 int ret;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200186
Simon Glass70061ba2016-06-12 23:30:32 -0600187 count = blk_dread(mmc_get_blk_desc(mmc),
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200188 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
189 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
190 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
191 if (count == 0) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100192#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Fabio Estevam37342ab2016-04-20 23:12:04 -0300193 puts("mmc_load_image_raw_os: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100194#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000195 return -1;
196 }
197
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530198 ret = mmc_load_image_raw_sector(mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200199 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Lokesh Vutla9dee77f2016-04-11 15:24:50 +0530200 if (ret)
201 return ret;
202
203 if (spl_image.os != IH_OS_LINUX) {
204 puts("Expected Linux image is not found. Trying to start U-boot\n");
205 return -ENOENT;
206 }
207
208 return 0;
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000209}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200210#else
211int spl_start_uboot(void)
212{
213 return 1;
214}
215static int mmc_load_image_raw_os(struct mmc *mmc)
216{
217 return -ENOSYS;
218}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000219#endif
220
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200221#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
222int spl_mmc_do_fs_boot(struct mmc *mmc)
223{
224 int err = -ENOSYS;
225
226#ifdef CONFIG_SPL_FAT_SUPPORT
227 if (!spl_start_uboot()) {
Simon Glass70061ba2016-06-12 23:30:32 -0600228 err = spl_load_image_fat_os(mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200229 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
230 if (!err)
231 return err;
232 }
233#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Simon Glass70061ba2016-06-12 23:30:32 -0600234 err = spl_load_image_fat(mmc_get_blk_desc(mmc),
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200235 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
236 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
237 if (!err)
238 return err;
239#endif
240#endif
241#ifdef CONFIG_SPL_EXT_SUPPORT
242 if (!spl_start_uboot()) {
243 err = spl_load_image_ext_os(&mmc->block_dev,
244 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
245 if (!err)
246 return err;
247 }
248#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
249 err = spl_load_image_ext(&mmc->block_dev,
250 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
251 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
252 if (!err)
253 return err;
254#endif
255#endif
256
257#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
258 err = -ENOENT;
259#endif
260
261 return err;
262}
263#else
264int spl_mmc_do_fs_boot(struct mmc *mmc)
265{
266 return -ENOSYS;
267}
268#endif
269
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200270int spl_mmc_load_image(u32 boot_device)
Simon Schwarze21d2d82011-09-14 15:33:34 -0400271{
Simon Glassa2b771c2015-12-02 10:59:11 -0700272 struct mmc *mmc = NULL;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400273 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600274 int err = 0;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200275 __maybe_unused int part;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400276
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200277 err = spl_mmc_find_device(&mmc, boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200278 if (err)
279 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400280
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200281 err = mmc_init(mmc);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400282 if (err) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100283#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200284 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100285#endif
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200286 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400287 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000288
Marek Vasut64d64bb2016-05-14 23:42:07 +0200289 boot_mode = spl_boot_mode(boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200290 err = -EINVAL;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200291 switch (boot_mode) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200292 case MMCSD_MODE_EMMCBOOT:
293 /*
294 * We need to check what the partition is configured to.
295 * 1 and 2 match up to boot0 / boot1 and 7 is user data
296 * which is the first physical partition (0).
297 */
298 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
299
300 if (part == 7)
301 part = 0;
302
Simon Glass11f2bb62016-05-01 13:52:29 -0600303 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200304 if (err) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200305#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
306 puts("spl: mmc partition switch failed\n");
307#endif
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200308 return err;
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200309 }
310 /* Fall through */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200311 case MMCSD_MODE_RAW:
312 debug("spl: mmc boot mode: raw\n");
313
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200314 if (!spl_start_uboot()) {
315 err = mmc_load_image_raw_os(mmc);
316 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200317 return err;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200318 }
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200319
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100320 err = mmc_load_image_raw_partition(mmc,
321 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200322 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200323 return err;
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200324#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100325 err = mmc_load_image_raw_sector(mmc,
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600326 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200327 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200328 return err;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200329#endif
Guillaume GARDET834dd882016-02-18 18:17:36 +0100330 /* If RAW mode fails, try FS mode. */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200331 case MMCSD_MODE_FS:
332 debug("spl: mmc boot mode: fs\n");
333
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200334 err = spl_mmc_do_fs_boot(mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200335 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200336 return err;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200337
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200338 break;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200339 case MMCSD_MODE_UNDEFINED:
Paul Burtona8015fd2013-09-04 16:12:24 +0100340#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200341 default:
342 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100343#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200344 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200345
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200346 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400347}