blob: 43748d05b28ee962e310b36b9ab0baa9a732605e [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
Paul Kocialkowski17675c82014-11-08 23:14:56 +010021static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040022{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020023 unsigned long count;
Peter Korsgaardf2471292013-03-21 04:55:17 +000024 u32 image_size_sectors;
25 struct image_header *header;
Simon Schwarze21d2d82011-09-14 15:33:34 -040026
27 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
Paul Kocialkowski0d139722015-04-27 10:20:22 +020028 sizeof(struct image_header));
Simon Schwarze21d2d82011-09-14 15:33:34 -040029
30 /* read image header to find the image size & load address */
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020031 count = mmc->block_dev.block_read(0, sector, 1, header);
Simon Glass5614db82015-06-23 15:38:47 -060032 debug("read sector %lx, count=%lu\n", sector, count);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020033 if (count == 0)
Simon Schwarze21d2d82011-09-14 15:33:34 -040034 goto end;
35
Simon Glass5614db82015-06-23 15:38:47 -060036 if (image_get_magic(header) != IH_MAGIC) {
37 puts("bad magic\n");
Tom Rini1530e8a2013-06-28 14:43:01 -040038 return -1;
Simon Glass5614db82015-06-23 15:38:47 -060039 }
Tom Rini1530e8a2013-06-28 14:43:01 -040040
Simon Schwarze21d2d82011-09-14 15:33:34 -040041 spl_parse_image_header(header);
42
43 /* convert size to sectors - round up */
Tom Rinia0b9fa52012-08-14 10:25:15 -070044 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
Paul Kocialkowski0d139722015-04-27 10:20:22 +020045 mmc->read_bl_len;
Simon Schwarze21d2d82011-09-14 15:33:34 -040046
47 /* Read the header too to avoid extra memcpy */
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020048 count = mmc->block_dev.block_read(0, sector, image_size_sectors,
Gong Qianyu2ae45892015-07-30 14:07:12 +080049 (void *)(ulong)spl_image.load_addr);
Simon Glass5614db82015-06-23 15:38:47 -060050 debug("read %x sectors to %x\n", image_size_sectors,
51 spl_image.load_addr);
Simon Schwarze21d2d82011-09-14 15:33:34 -040052
53end:
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020054 if (count == 0) {
Paul Burtona8015fd2013-09-04 16:12:24 +010055#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020056 puts("spl: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010057#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020058 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020059 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020060
61 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040062}
63
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +020064int spl_mmc_get_device_index(u32 boot_device)
65{
66 switch (boot_device) {
67 case BOOT_DEVICE_MMC1:
68 return 0;
69 case BOOT_DEVICE_MMC2:
70 case BOOT_DEVICE_MMC2_2:
71 return 1;
72 }
73
74#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
75 printf("spl: unsupported mmc boot device.\n");
76#endif
77
78 return -ENODEV;
79}
80
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020081#ifdef CONFIG_DM_MMC
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +020082static int spl_mmc_find_device(struct mmc **mmc, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020083{
84 struct udevice *dev;
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +020085 int err, mmc_dev;
86
87 mmc_dev = spl_mmc_get_device_index(boot_device);
88 if (mmc_dev < 0)
89 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020090
91 err = mmc_initialize(NULL);
92 if (err) {
93#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
94 printf("spl: could not initialize mmc. error: %d\n", err);
95#endif
96 return err;
97 }
98
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +020099 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200100 if (err) {
101#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
102 printf("spl: could not find mmc device. error: %d\n", err);
103#endif
104 return err;
105 }
106
107 *mmc = NULL;
108 *mmc = mmc_get_mmc_dev(dev);
109 return *mmc != NULL ? 0 : -ENODEV;
110}
111#else
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200112static int spl_mmc_find_device(struct mmc **mmc, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200113{
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200114 int err, mmc_dev;
115
116 mmc_dev = spl_mmc_get_device_index(boot_device);
117 if (mmc_dev < 0)
118 return mmc_dev;
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200119
120 err = mmc_initialize(gd->bd);
121 if (err) {
122#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
123 printf("spl: could not initialize mmc. error: %d\n", err);
124#endif
125 return err;
126 }
127
128 /* We register only one device. So, the dev id is always 0 */
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200129 *mmc = find_mmc_device(mmc_dev);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200130 if (!*mmc) {
131#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
132 puts("spl: mmc device not found\n");
133#endif
134 return -ENODEV;
135 }
136
137 return 0;
138}
139#endif
140
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100141#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
142static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
143{
144 disk_partition_t info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200145 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100146
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200147 err = get_partition_info(&mmc->block_dev, partition, &info);
148 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100149#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200150 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100151#endif
152 return -1;
153 }
154
Stefan Roesed0503cd2015-07-20 11:20:39 +0200155#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
156 return mmc_load_image_raw_sector(mmc, info.start +
157 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
158#else
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100159 return mmc_load_image_raw_sector(mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200160#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100161}
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200162#else
163#define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
164static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
165{
166 return -ENOSYS;
167}
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100168#endif
169
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000170#ifdef CONFIG_SPL_OS_BOOT
171static int mmc_load_image_raw_os(struct mmc *mmc)
172{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200173 unsigned long count;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200174
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200175 count = mmc->block_dev.block_read(0,
176 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
177 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
178 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
179 if (count == 0) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100180#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200181 puts("spl: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100182#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000183 return -1;
184 }
185
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100186 return mmc_load_image_raw_sector(mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200187 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000188}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200189#else
190int spl_start_uboot(void)
191{
192 return 1;
193}
194static int mmc_load_image_raw_os(struct mmc *mmc)
195{
196 return -ENOSYS;
197}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000198#endif
199
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200200#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
201int spl_mmc_do_fs_boot(struct mmc *mmc)
202{
203 int err = -ENOSYS;
204
205#ifdef CONFIG_SPL_FAT_SUPPORT
206 if (!spl_start_uboot()) {
207 err = spl_load_image_fat_os(&mmc->block_dev,
208 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
209 if (!err)
210 return err;
211 }
212#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
213 err = spl_load_image_fat(&mmc->block_dev,
214 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
215 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
216 if (!err)
217 return err;
218#endif
219#endif
220#ifdef CONFIG_SPL_EXT_SUPPORT
221 if (!spl_start_uboot()) {
222 err = spl_load_image_ext_os(&mmc->block_dev,
223 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
224 if (!err)
225 return err;
226 }
227#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
228 err = spl_load_image_ext(&mmc->block_dev,
229 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
230 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
231 if (!err)
232 return err;
233#endif
234#endif
235
236#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
237 err = -ENOENT;
238#endif
239
240 return err;
241}
242#else
243int spl_mmc_do_fs_boot(struct mmc *mmc)
244{
245 return -ENOSYS;
246}
247#endif
248
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200249int spl_mmc_load_image(u32 boot_device)
Simon Schwarze21d2d82011-09-14 15:33:34 -0400250{
Simon Glassa2b771c2015-12-02 10:59:11 -0700251 struct mmc *mmc = NULL;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400252 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600253 int err = 0;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200254 __maybe_unused int part;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400255
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200256 err = spl_mmc_find_device(&mmc, boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200257 if (err)
258 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400259
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200260 err = mmc_init(mmc);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400261 if (err) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100262#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200263 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100264#endif
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200265 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400266 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000267
Tom Rinia76ff952012-08-14 09:19:44 -0700268 boot_mode = spl_boot_mode();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200269 err = -EINVAL;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200270 switch (boot_mode) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200271 case MMCSD_MODE_EMMCBOOT:
272 /*
273 * We need to check what the partition is configured to.
274 * 1 and 2 match up to boot0 / boot1 and 7 is user data
275 * which is the first physical partition (0).
276 */
277 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
278
279 if (part == 7)
280 part = 0;
281
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200282 err = mmc_switch_part(0, part);
283 if (err) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200284#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
285 puts("spl: mmc partition switch failed\n");
286#endif
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200287 return err;
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200288 }
289 /* Fall through */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200290 case MMCSD_MODE_RAW:
291 debug("spl: mmc boot mode: raw\n");
292
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200293 if (!spl_start_uboot()) {
294 err = mmc_load_image_raw_os(mmc);
295 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200296 return err;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200297 }
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200298
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100299 err = mmc_load_image_raw_partition(mmc,
300 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200301 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200302 return err;
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200303#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100304 err = mmc_load_image_raw_sector(mmc,
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600305 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200306 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200307 return err;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200308#endif
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200309 break;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200310 case MMCSD_MODE_FS:
311 debug("spl: mmc boot mode: fs\n");
312
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200313 err = spl_mmc_do_fs_boot(mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200314 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200315 return err;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200316
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200317 break;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200318 case MMCSD_MODE_UNDEFINED:
Paul Burtona8015fd2013-09-04 16:12:24 +0100319#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200320 default:
321 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100322#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200323 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200324
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200325 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400326}