blob: ce58c5824f6d80add3705a588134d876da7bc474 [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>
Simon Schwarze21d2d82011-09-14 15:33:34 -040014#include <mmc.h>
Tom Rini1530e8a2013-06-28 14:43:01 -040015#include <image.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040016
17DECLARE_GLOBAL_DATA_PTR;
18
Paul Kocialkowski17675c82014-11-08 23:14:56 +010019static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040020{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020021 unsigned long count;
Peter Korsgaardf2471292013-03-21 04:55:17 +000022 u32 image_size_sectors;
23 struct image_header *header;
Simon Schwarze21d2d82011-09-14 15:33:34 -040024
25 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
Paul Kocialkowski0d139722015-04-27 10:20:22 +020026 sizeof(struct image_header));
Simon Schwarze21d2d82011-09-14 15:33:34 -040027
28 /* read image header to find the image size & load address */
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020029 count = mmc->block_dev.block_read(0, sector, 1, header);
Simon Glass5614db82015-06-23 15:38:47 -060030 debug("read sector %lx, count=%lu\n", sector, count);
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020031 if (count == 0)
Simon Schwarze21d2d82011-09-14 15:33:34 -040032 goto end;
33
Simon Glass5614db82015-06-23 15:38:47 -060034 if (image_get_magic(header) != IH_MAGIC) {
35 puts("bad magic\n");
Tom Rini1530e8a2013-06-28 14:43:01 -040036 return -1;
Simon Glass5614db82015-06-23 15:38:47 -060037 }
Tom Rini1530e8a2013-06-28 14:43:01 -040038
Simon Schwarze21d2d82011-09-14 15:33:34 -040039 spl_parse_image_header(header);
40
41 /* convert size to sectors - round up */
Tom Rinia0b9fa52012-08-14 10:25:15 -070042 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
Paul Kocialkowski0d139722015-04-27 10:20:22 +020043 mmc->read_bl_len;
Simon Schwarze21d2d82011-09-14 15:33:34 -040044
45 /* Read the header too to avoid extra memcpy */
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020046 count = mmc->block_dev.block_read(0, sector, image_size_sectors,
Gong Qianyu2ae45892015-07-30 14:07:12 +080047 (void *)(ulong)spl_image.load_addr);
Simon Glass5614db82015-06-23 15:38:47 -060048 debug("read %x sectors to %x\n", image_size_sectors,
49 spl_image.load_addr);
Simon Schwarze21d2d82011-09-14 15:33:34 -040050
51end:
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020052 if (count == 0) {
Paul Burtona8015fd2013-09-04 16:12:24 +010053#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020054 puts("spl: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010055#endif
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020056 return -1;
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020057 }
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020058
59 return 0;
Simon Schwarze21d2d82011-09-14 15:33:34 -040060}
61
Paul Kocialkowski17675c82014-11-08 23:14:56 +010062#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
63static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
64{
65 disk_partition_t info;
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020066 int err;
Paul Kocialkowski17675c82014-11-08 23:14:56 +010067
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020068 err = get_partition_info(&mmc->block_dev, partition, &info);
69 if (err) {
Paul Kocialkowski17675c82014-11-08 23:14:56 +010070#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020071 puts("spl: partition error\n");
Paul Kocialkowski17675c82014-11-08 23:14:56 +010072#endif
73 return -1;
74 }
75
Stefan Roesed0503cd2015-07-20 11:20:39 +020076#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
77 return mmc_load_image_raw_sector(mmc, info.start +
78 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
79#else
Paul Kocialkowski17675c82014-11-08 23:14:56 +010080 return mmc_load_image_raw_sector(mmc, info.start);
Stefan Roesed0503cd2015-07-20 11:20:39 +020081#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +010082}
83#endif
84
Peter Korsgaard01b542f2013-05-13 08:36:29 +000085#ifdef CONFIG_SPL_OS_BOOT
86static int mmc_load_image_raw_os(struct mmc *mmc)
87{
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020088 unsigned long count;
Paul Kocialkowski0d139722015-04-27 10:20:22 +020089
Paul Kocialkowski2d69f252015-05-22 12:45:35 +020090 count = mmc->block_dev.block_read(0,
91 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
92 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
93 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
94 if (count == 0) {
Paul Burtona8015fd2013-09-04 16:12:24 +010095#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski01c723f2015-06-03 18:48:51 +020096 puts("spl: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010097#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +000098 return -1;
99 }
100
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100101 return mmc_load_image_raw_sector(mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200102 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000103}
104#endif
105
Simon Schwarze21d2d82011-09-14 15:33:34 -0400106void spl_mmc_load_image(void)
107{
108 struct mmc *mmc;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400109 u32 boot_mode;
Simon Glassf22851a2015-06-23 15:38:51 -0600110 int err = 0;
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200111 __maybe_unused int part;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400112
Simon Glassf22851a2015-06-23 15:38:51 -0600113#ifdef CONFIG_DM_MMC
114 struct udevice *dev;
115
116 mmc_initialize(NULL);
117 err = uclass_get_device(UCLASS_MMC, 0, &dev);
118 mmc = NULL;
119 if (!err)
120 mmc = mmc_get_mmc_dev(dev);
121#else
Simon Schwarze21d2d82011-09-14 15:33:34 -0400122 mmc_initialize(gd->bd);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200123
Simon Schwarze21d2d82011-09-14 15:33:34 -0400124 /* We register only one device. So, the dev id is always 0 */
125 mmc = find_mmc_device(0);
126 if (!mmc) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100127#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200128 puts("spl: mmc device not found\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100129#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400130 hang();
131 }
Simon Glassf22851a2015-06-23 15:38:51 -0600132#endif
133
134 if (!err)
135 err = mmc_init(mmc);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400136
Simon Schwarze21d2d82011-09-14 15:33:34 -0400137 if (err) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100138#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200139 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100140#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400141 hang();
142 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000143
Tom Rinia76ff952012-08-14 09:19:44 -0700144 boot_mode = spl_boot_mode();
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200145 switch (boot_mode) {
146 case MMCSD_MODE_RAW:
147 debug("spl: mmc boot mode: raw\n");
148
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000149#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200150 if (!spl_start_uboot()) {
151 err = mmc_load_image_raw_os(mmc);
152 if (!err)
153 return;
154 }
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000155#endif
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200156#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100157 err = mmc_load_image_raw_partition(mmc,
158 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200159 if (!err)
160 return;
161#elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100162 err = mmc_load_image_raw_sector(mmc,
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600163 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200164 if (!err)
165 return;
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200166#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200167 case MMCSD_MODE_FS:
168 debug("spl: mmc boot mode: fs\n");
169
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200170#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200171#ifdef CONFIG_SPL_FAT_SUPPORT
Peter Korsgaard465f1f82013-05-13 08:36:27 +0000172#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200173 if (!spl_start_uboot()) {
174 err = spl_load_image_fat_os(&mmc->block_dev,
175 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
176 if (!err)
177 return;
178 }
Peter Korsgaard465f1f82013-05-13 08:36:27 +0000179#endif
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200180#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600181 err = spl_load_image_fat(&mmc->block_dev,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200182 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
183 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
184 if (!err)
185 return;
186#endif
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200187#endif
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200188#ifdef CONFIG_SPL_EXT_SUPPORT
189#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200190 if (!spl_start_uboot()) {
191 err = spl_load_image_ext_os(&mmc->block_dev,
192 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
193 if (!err)
194 return;
195 }
Tom Rinia0d8cca2012-08-10 09:27:14 -0700196#endif
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200197#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200198 err = spl_load_image_ext(&mmc->block_dev,
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200199 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
200 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
201 if (!err)
202 return;
203#endif
204#endif
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200205#endif
Tom Rinicc612692014-02-05 10:24:18 -0500206#ifdef CONFIG_SUPPORT_EMMC_BOOT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200207 case MMCSD_MODE_EMMCBOOT:
Tom Rinicc612692014-02-05 10:24:18 -0500208 /*
209 * We need to check what the partition is configured to.
210 * 1 and 2 match up to boot0 / boot1 and 7 is user data
211 * which is the first physical partition (0).
212 */
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200213 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
Tom Rinicc612692014-02-05 10:24:18 -0500214
215 if (part == 7)
216 part = 0;
217
218 if (mmc_switch_part(0, part)) {
219#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200220 puts("spl: mmc partition switch failed\n");
Tom Rinicc612692014-02-05 10:24:18 -0500221#endif
222 hang();
223 }
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200224
Tom Rinicc612692014-02-05 10:24:18 -0500225#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200226 if (!spl_start_uboot()) {
227 err = mmc_load_image_raw_os(mmc);
228 if (!err)
229 return;
230 }
Tom Rinicc612692014-02-05 10:24:18 -0500231#endif
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200232#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
Paul Kocialkowski409d4f72015-04-27 10:20:23 +0200233 err = mmc_load_image_raw_partition(mmc,
234 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200235 if (!err)
236 return;
237#elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100238 err = mmc_load_image_raw_sector(mmc,
Tom Rinicc612692014-02-05 10:24:18 -0500239 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200240 if (!err)
241 return;
Guillaume GARDET1143b0a2014-12-16 12:00:44 +0100242#endif
Paul Kocialkowski4bd66e72015-06-08 23:05:09 +0200243#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200244 case MMCSD_MODE_UNDEFINED:
245 default:
Paul Burtona8015fd2013-09-04 16:12:24 +0100246#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200247 if (err)
248 puts("spl: mmc: no boot mode left to try\n");
249 else
250 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100251#endif
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000252 hang();
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200253 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400254}