blob: 5dc576bdff3b3728f10f1842384c2a7c62c094eb [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>
Simon Schwarze21d2d82011-09-14 15:33:34 -040013#include <asm/u-boot.h>
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020014#include <errno.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040015#include <mmc.h>
Tom Rini1530e8a2013-06-28 14:43:01 -040016#include <image.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040017
18DECLARE_GLOBAL_DATA_PTR;
19
Paul Kocialkowski17675c82014-11-08 23:14:56 +010020static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040021{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020022 unsigned long count;
Peter Korsgaardf2471292013-03-21 04:55:17 +000023 u32 image_size_sectors;
24 struct image_header *header;
Simon Schwarze21d2d82011-09-14 15:33:34 -040025
26 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
Paul Kocialkowski0d139722015-04-27 10:20:22 +020027 sizeof(struct image_header));
Simon Schwarze21d2d82011-09-14 15:33:34 -040028
29 /* read image header to find the image size & load address */
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020030 count = mmc->block_dev.block_read(0, sector, 1, header);
Simon Glass5614db82015-06-23 15:38:47 -060031 debug("read sector %lx, count=%lu\n", sector, count);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020032 if (count == 0)
Simon Schwarze21d2d82011-09-14 15:33:34 -040033 goto end;
34
Simon Glass5614db82015-06-23 15:38:47 -060035 if (image_get_magic(header) != IH_MAGIC) {
36 puts("bad magic\n");
Tom Rini1530e8a2013-06-28 14:43:01 -040037 return -1;
Simon Glass5614db82015-06-23 15:38:47 -060038 }
Tom Rini1530e8a2013-06-28 14:43:01 -040039
Simon Schwarze21d2d82011-09-14 15:33:34 -040040 spl_parse_image_header(header);
41
42 /* convert size to sectors - round up */
Tom Rinia0b9fa52012-08-14 10:25:15 -070043 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
Paul Kocialkowski0d139722015-04-27 10:20:22 +020044 mmc->read_bl_len;
Simon Schwarze21d2d82011-09-14 15:33:34 -040045
46 /* Read the header too to avoid extra memcpy */
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020047 count = mmc->block_dev.block_read(0, sector, image_size_sectors,
Gong Qianyu2ae45892015-07-30 14:07:12 +080048 (void *)(ulong)spl_image.load_addr);
Simon Glass5614db82015-06-23 15:38:47 -060049 debug("read %x sectors to %x\n", image_size_sectors,
50 spl_image.load_addr);
Simon Schwarze21d2d82011-09-14 15:33:34 -040051
52end:
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020053 if (count == 0) {
Paul Burtona8015fd2013-09-04 16:12:24 +010054#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020055 puts("spl: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010056#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020057 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020058 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020059
60 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040061}
62
Nikita Kiryanov022ddb82015-11-08 17:11:44 +020063#ifdef CONFIG_DM_MMC
64static int spl_mmc_find_device(struct mmc **mmc)
65{
66 struct udevice *dev;
67 int err;
68
69 err = mmc_initialize(NULL);
70 if (err) {
71#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
72 printf("spl: could not initialize mmc. error: %d\n", err);
73#endif
74 return err;
75 }
76
77 err = uclass_get_device(UCLASS_MMC, 0, &dev);
78 if (err) {
79#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
80 printf("spl: could not find mmc device. error: %d\n", err);
81#endif
82 return err;
83 }
84
85 *mmc = NULL;
86 *mmc = mmc_get_mmc_dev(dev);
87 return *mmc != NULL ? 0 : -ENODEV;
88}
89#else
90static int spl_mmc_find_device(struct mmc **mmc)
91{
92 int err;
93
94 err = mmc_initialize(gd->bd);
95 if (err) {
96#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
97 printf("spl: could not initialize mmc. error: %d\n", err);
98#endif
99 return err;
100 }
101
102 /* We register only one device. So, the dev id is always 0 */
103 *mmc = find_mmc_device(0);
104 if (!*mmc) {
105#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
106 puts("spl: mmc device not found\n");
107#endif
108 return -ENODEV;
109 }
110
111 return 0;
112}
113#endif
114
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100115#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
116static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
117{
118 disk_partition_t info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200119 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100120
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200121 err = get_partition_info(&mmc->block_dev, partition, &info);
122 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100123#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200124 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100125#endif
126 return -1;
127 }
128
Stefan Roesed0503cd2015-07-20 11:20:39 +0200129#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
130 return mmc_load_image_raw_sector(mmc, info.start +
131 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
132#else
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100133 return mmc_load_image_raw_sector(mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +0200134#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100135}
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200136#else
137#define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
138static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
139{
140 return -ENOSYS;
141}
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100142#endif
143
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000144#ifdef CONFIG_SPL_OS_BOOT
145static int mmc_load_image_raw_os(struct mmc *mmc)
146{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200147 unsigned long count;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200148
Paul Kocialkowski2d69f252015-05-22 12:45:35 +0200149 count = mmc->block_dev.block_read(0,
150 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
151 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
152 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
153 if (count == 0) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100154#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +0200155 puts("spl: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100156#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000157 return -1;
158 }
159
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100160 return mmc_load_image_raw_sector(mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200161 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000162}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200163#else
164int spl_start_uboot(void)
165{
166 return 1;
167}
168static int mmc_load_image_raw_os(struct mmc *mmc)
169{
170 return -ENOSYS;
171}
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000172#endif
173
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200174#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
175int spl_mmc_do_fs_boot(struct mmc *mmc)
176{
177 int err = -ENOSYS;
178
179#ifdef CONFIG_SPL_FAT_SUPPORT
180 if (!spl_start_uboot()) {
181 err = spl_load_image_fat_os(&mmc->block_dev,
182 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
183 if (!err)
184 return err;
185 }
186#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
187 err = spl_load_image_fat(&mmc->block_dev,
188 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
189 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
190 if (!err)
191 return err;
192#endif
193#endif
194#ifdef CONFIG_SPL_EXT_SUPPORT
195 if (!spl_start_uboot()) {
196 err = spl_load_image_ext_os(&mmc->block_dev,
197 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
198 if (!err)
199 return err;
200 }
201#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
202 err = spl_load_image_ext(&mmc->block_dev,
203 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
204 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
205 if (!err)
206 return err;
207#endif
208#endif
209
210#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
211 err = -ENOENT;
212#endif
213
214 return err;
215}
216#else
217int spl_mmc_do_fs_boot(struct mmc *mmc)
218{
219 return -ENOSYS;
220}
221#endif
222
Simon Schwarze21d2d82011-09-14 15:33:34 -0400223void spl_mmc_load_image(void)
224{
225 struct mmc *mmc;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400226 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600227 int err = 0;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200228 __maybe_unused int part;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400229
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200230 if (spl_mmc_find_device(&mmc))
Simon Schwarze21d2d82011-09-14 15:33:34 -0400231 hang();
Simon Schwarze21d2d82011-09-14 15:33:34 -0400232
Nikita Kiryanov022ddb82015-11-08 17:11:44 +0200233 err = mmc_init(mmc);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400234 if (err) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100235#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200236 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100237#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400238 hang();
239 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000240
Tom Rinia76ff952012-08-14 09:19:44 -0700241 boot_mode = spl_boot_mode();
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200242 switch (boot_mode) {
243 case MMCSD_MODE_RAW:
244 debug("spl: mmc boot mode: raw\n");
245
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200246 if (!spl_start_uboot()) {
247 err = mmc_load_image_raw_os(mmc);
248 if (!err)
249 return;
250 }
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200251
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100252 err = mmc_load_image_raw_partition(mmc,
253 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200254 if (!err)
255 return;
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200256#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100257 err = mmc_load_image_raw_sector(mmc,
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600258 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200259 if (!err)
260 return;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200261#endif
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200262 break;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200263 case MMCSD_MODE_FS:
264 debug("spl: mmc boot mode: fs\n");
265
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200266 err = spl_mmc_do_fs_boot(mmc);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200267 if (!err)
268 return;
Nikita Kiryanov642648c2015-11-08 17:11:47 +0200269
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200270 break;
Tom Rinicc612692014-02-05 10:24:18 -0500271#ifdef CONFIG_SUPPORT_EMMC_BOOT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200272 case MMCSD_MODE_EMMCBOOT:
Tom Rinicc612692014-02-05 10:24:18 -0500273 /*
274 * We need to check what the partition is configured to.
275 * 1 and 2 match up to boot0 / boot1 and 7 is user data
276 * which is the first physical partition (0).
277 */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200278 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
Tom Rinicc612692014-02-05 10:24:18 -0500279
280 if (part == 7)
281 part = 0;
282
283 if (mmc_switch_part(0, part)) {
284#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200285 puts("spl: mmc partition switch failed\n");
Tom Rinicc612692014-02-05 10:24:18 -0500286#endif
287 hang();
288 }
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200289
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200290 if (!spl_start_uboot()) {
291 err = mmc_load_image_raw_os(mmc);
292 if (!err)
293 return;
294 }
Paul Kocialkowski409d4f72015-04-27 10:20:23 +0200295 err = mmc_load_image_raw_partition(mmc,
296 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200297 if (!err)
298 return;
Nikita Kiryanov011cd8f2015-11-08 17:11:46 +0200299#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100300 err = mmc_load_image_raw_sector(mmc,
Tom Rinicc612692014-02-05 10:24:18 -0500301 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200302 if (!err)
303 return;
Guillaume GARDET1143b0a2014-12-16 12:00:44 +0100304#endif
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200305 break;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200306#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200307 case MMCSD_MODE_UNDEFINED:
Paul Burtona8015fd2013-09-04 16:12:24 +0100308#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200309 default:
310 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100311#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200312 }
Nikita Kiryanoved52f4d2015-11-08 17:11:43 +0200313
314 hang();
Simon Schwarze21d2d82011-09-14 15:33:34 -0400315}