blob: 7d3bfc60bae0030bb7aac5b01d2f73c94e2b8ff9 [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;
26
27 spl_parse_image_header(header);
28 /* convert size to sectors - round up */
29 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
30 mmc->read_bl_len;
31
32 /* Read the header too to avoid extra memcpy */
33 count = mmc->block_dev.block_read(&mmc->block_dev, sector,
34 image_size_sectors,
35 (void *)(ulong)spl_image.load_addr);
36 debug("read %x sectors to %x\n", image_size_sectors,
37 spl_image.load_addr);
38 if (count != image_size_sectors)
39 return -EIO;
40
41 return 0;
42}
43
Simon Glassb52c7fc2016-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 Glassb52c7fc2016-02-22 22:55:58 -070051
Paul Kocialkowski17675c82014-11-08 23:14:56 +010052static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040053{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020054 unsigned long count;
Peter Korsgaardf2471292013-03-21 04:55:17 +000055 struct image_header *header;
Simon Glassb52c7fc2016-02-22 22:55:58 -070056 int ret = 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040057
58 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
Paul Kocialkowski0d139722015-04-27 10:20:22 +020059 sizeof(struct image_header));
Simon Schwarze21d2d82011-09-14 15:33:34 -040060
61 /* read image header to find the image size & load address */
Stephen Warrene73f2962015-12-07 11:38:48 -070062 count = mmc->block_dev.block_read(&mmc->block_dev, sector, 1, header);
Simon Glassb52c7fc2016-02-22 22:55:58 -070063 debug("hdr read sector %lx, count=%lu\n", sector, count);
64 if (count == 0) {
65 ret = -EIO;
Simon Schwarze21d2d82011-09-14 15:33:34 -040066 goto end;
Simon Glassb52c7fc2016-02-22 22:55:58 -070067 }
Simon Schwarze21d2d82011-09-14 15:33:34 -040068
Masahiro Yamada802f1c32016-03-16 12:10:00 +090069 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
70 image_get_magic(header) == FDT_MAGIC) {
Simon Glassb52c7fc2016-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 Yamada802f1c32016-03-16 12:10:00 +090079 } else {
80 ret = mmc_load_legacy(mmc, sector, header);
Simon Glass5614db82015-06-23 15:38:47 -060081 }
Tom Rini1530e8a2013-06-28 14:43:01 -040082
Simon Schwarze21d2d82011-09-14 15:33:34 -040083end:
Simon Glassb52c7fc2016-02-22 22:55:58 -070084 if (ret) {
Paul Burtona8015fd2013-09-04 16:12:24 +010085#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020086 puts("spl: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010087#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020088 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020089 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020090
91 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040092}
93
Nikita Kiryanove63b2dd2015-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 Glass4faf63b2015-12-02 10:59:12 -0700111static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200112{
Simon Glass51ace322015-12-02 10:59:13 -0700113#ifdef CONFIG_DM_MMC
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200114 struct udevice *dev;
Simon Glass51ace322015-12-02 10:59:13 -0700115#endif
Nikita Kiryanove63b2dd2015-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 Kiryanov022ddb82015-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 Glass51ace322015-12-02 10:59:13 -0700130#ifdef CONFIG_DM_MMC
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200131 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
Simon Glass51ace322015-12-02 10:59:13 -0700132 if (!err)
133 *mmcp = mmc_get_mmc_dev(dev);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200134#else
Simon Glass51ace322015-12-02 10:59:13 -0700135 *mmcp = find_mmc_device(mmc_dev);
136 err = *mmcp ? 0 : -ENODEV;
137#endif
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200138 if (err) {
139#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Simon Glass51ace322015-12-02 10:59:13 -0700140 printf("spl: could not find mmc device. error: %d\n", err);
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200141#endif
142 return err;
143 }
144
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200145 return 0;
146}
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200147
Paul Kocialkowski17675c82014-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 Kocialkowski2d69f252015-05-22 12:45:35 +0200152 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100153
Simon Glassb89a8442016-02-29 15:25:48 -0700154 err = part_get_info(&mmc->block_dev, partition, &info);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200155 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100156#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200157 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100158#endif
159 return -1;
160 }
161
Stefan Roesed0503cd2015-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 Kocialkowski17675c82014-11-08 23:14:56 +0100166 return mmc_load_image_raw_sector(mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200167#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100168}
Nikita Kiryanov011cd8f2015-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 Kocialkowski17675c82014-11-08 23:14:56 +0100175#endif
176
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000177#ifdef CONFIG_SPL_OS_BOOT
178static int mmc_load_image_raw_os(struct mmc *mmc)
179{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200180 unsigned long count;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200181
Stephen Warrene73f2962015-12-07 11:38:48 -0700182 count = mmc->block_dev.block_read(&mmc->block_dev,
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200183 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
184 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
185 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
186 if (count == 0) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100187#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200188 puts("spl: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100189#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000190 return -1;
191 }
192
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100193 return mmc_load_image_raw_sector(mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200194 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000195}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200196#else
197int spl_start_uboot(void)
198{
199 return 1;
200}
201static int mmc_load_image_raw_os(struct mmc *mmc)
202{
203 return -ENOSYS;
204}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000205#endif
206
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200207#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
208int spl_mmc_do_fs_boot(struct mmc *mmc)
209{
210 int err = -ENOSYS;
211
212#ifdef CONFIG_SPL_FAT_SUPPORT
213 if (!spl_start_uboot()) {
214 err = spl_load_image_fat_os(&mmc->block_dev,
215 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
216 if (!err)
217 return err;
218 }
219#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
220 err = spl_load_image_fat(&mmc->block_dev,
221 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
222 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
223 if (!err)
224 return err;
225#endif
226#endif
227#ifdef CONFIG_SPL_EXT_SUPPORT
228 if (!spl_start_uboot()) {
229 err = spl_load_image_ext_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_ext(&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
243#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
244 err = -ENOENT;
245#endif
246
247 return err;
248}
249#else
250int spl_mmc_do_fs_boot(struct mmc *mmc)
251{
252 return -ENOSYS;
253}
254#endif
255
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200256int spl_mmc_load_image(u32 boot_device)
Simon Schwarze21d2d82011-09-14 15:33:34 -0400257{
Simon Glassa2b771c2015-12-02 10:59:11 -0700258 struct mmc *mmc = NULL;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400259 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600260 int err = 0;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200261 __maybe_unused int part;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400262
Nikita Kiryanove63b2dd2015-11-08 17:11:54 +0200263 err = spl_mmc_find_device(&mmc, boot_device);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200264 if (err)
265 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400266
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200267 err = mmc_init(mmc);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400268 if (err) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100269#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200270 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100271#endif
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200272 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400273 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000274
Tom Rinia76ff952012-08-14 09:19:44 -0700275 boot_mode = spl_boot_mode();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200276 err = -EINVAL;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200277 switch (boot_mode) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200278 case MMCSD_MODE_EMMCBOOT:
279 /*
280 * We need to check what the partition is configured to.
281 * 1 and 2 match up to boot0 / boot1 and 7 is user data
282 * which is the first physical partition (0).
283 */
284 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
285
286 if (part == 7)
287 part = 0;
288
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200289 err = mmc_switch_part(0, part);
290 if (err) {
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200291#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
292 puts("spl: mmc partition switch failed\n");
293#endif
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200294 return err;
Nikita Kiryanov2b3ae5f2015-11-08 17:11:48 +0200295 }
296 /* Fall through */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200297 case MMCSD_MODE_RAW:
298 debug("spl: mmc boot mode: raw\n");
299
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200300 if (!spl_start_uboot()) {
301 err = mmc_load_image_raw_os(mmc);
302 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200303 return err;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200304 }
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200305
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100306 err = mmc_load_image_raw_partition(mmc,
307 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200308 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200309 return err;
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200310#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100311 err = mmc_load_image_raw_sector(mmc,
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600312 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200313 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200314 return err;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200315#endif
Guillaume GARDET834dd882016-02-18 18:17:36 +0100316 /* If RAW mode fails, try FS mode. */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200317 case MMCSD_MODE_FS:
318 debug("spl: mmc boot mode: fs\n");
319
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200320 err = spl_mmc_do_fs_boot(mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200321 if (!err)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200322 return err;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200323
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200324 break;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200325 case MMCSD_MODE_UNDEFINED:
Paul Burtona8015fd2013-09-04 16:12:24 +0100326#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200327 default:
328 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100329#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200330 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200331
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200332 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400333}