blob: ce58c5824f6d80add3705a588134d876da7bc474 [file] [log] [blame]
Simon Schwarz9ea5c6e2011-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 Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04008 */
9#include <common.h>
Simon Glassdc3dedf2015-06-23 15:38:51 -060010#include <dm.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070011#include <spl.h>
Paul Kocialkowski91199f42015-04-27 10:20:22 +020012#include <linux/compiler.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040013#include <asm/u-boot.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040014#include <mmc.h>
Tom Rinie4c444b2013-06-28 14:43:01 -040015#include <image.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040016
17DECLARE_GLOBAL_DATA_PTR;
18
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010019static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040020{
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020021 unsigned long count;
Peter Korsgaard2e222102013-03-21 04:55:17 +000022 u32 image_size_sectors;
23 struct image_header *header;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040024
25 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
Paul Kocialkowski91199f42015-04-27 10:20:22 +020026 sizeof(struct image_header));
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040027
28 /* read image header to find the image size & load address */
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020029 count = mmc->block_dev.block_read(0, sector, 1, header);
Simon Glassfdfa39d2015-06-23 15:38:47 -060030 debug("read sector %lx, count=%lu\n", sector, count);
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020031 if (count == 0)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040032 goto end;
33
Simon Glassfdfa39d2015-06-23 15:38:47 -060034 if (image_get_magic(header) != IH_MAGIC) {
35 puts("bad magic\n");
Tom Rinie4c444b2013-06-28 14:43:01 -040036 return -1;
Simon Glassfdfa39d2015-06-23 15:38:47 -060037 }
Tom Rinie4c444b2013-06-28 14:43:01 -040038
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040039 spl_parse_image_header(header);
40
41 /* convert size to sectors - round up */
Tom Rinif0881252012-08-14 10:25:15 -070042 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
Paul Kocialkowski91199f42015-04-27 10:20:22 +020043 mmc->read_bl_len;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040044
45 /* Read the header too to avoid extra memcpy */
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020046 count = mmc->block_dev.block_read(0, sector, image_size_sectors,
Gong Qianyu7ef4c452015-07-30 14:07:12 +080047 (void *)(ulong)spl_image.load_addr);
Simon Glassfdfa39d2015-06-23 15:38:47 -060048 debug("read %x sectors to %x\n", image_size_sectors,
49 spl_image.load_addr);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040050
51end:
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020052 if (count == 0) {
Paul Burton8112f5f2013-09-04 16:12:24 +010053#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020054 puts("spl: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010055#endif
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020056 return -1;
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020057 }
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020058
59 return 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040060}
61
Paul Kocialkowskib97300b2014-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 Kocialkowski3bc37b62015-05-22 12:45:35 +020066 int err;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010067
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020068 err = get_partition_info(&mmc->block_dev, partition, &info);
69 if (err) {
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010070#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020071 puts("spl: partition error\n");
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010072#endif
73 return -1;
74 }
75
Stefan Roese4bfcc542015-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 Kocialkowskib97300b2014-11-08 23:14:56 +010080 return mmc_load_image_raw_sector(mmc, info.start);
Stefan Roese4bfcc542015-07-20 11:20:39 +020081#endif
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010082}
83#endif
84
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000085#ifdef CONFIG_SPL_OS_BOOT
86static int mmc_load_image_raw_os(struct mmc *mmc)
87{
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020088 unsigned long count;
Paul Kocialkowski91199f42015-04-27 10:20:22 +020089
Paul Kocialkowski3bc37b62015-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 Burton8112f5f2013-09-04 16:12:24 +010095#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020096 puts("spl: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010097#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000098 return -1;
99 }
100
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100101 return mmc_load_image_raw_sector(mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200102 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000103}
104#endif
105
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400106void spl_mmc_load_image(void)
107{
108 struct mmc *mmc;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400109 u32 boot_mode;
Simon Glassdc3dedf2015-06-23 15:38:51 -0600110 int err = 0;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200111 __maybe_unused int part;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400112
Simon Glassdc3dedf2015-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 Schwarz9ea5c6e2011-09-14 15:33:34 -0400122 mmc_initialize(gd->bd);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200123
Simon Schwarz9ea5c6e2011-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 Burton8112f5f2013-09-04 16:12:24 +0100127#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200128 puts("spl: mmc device not found\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100129#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400130 hang();
131 }
Simon Glassdc3dedf2015-06-23 15:38:51 -0600132#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400133
Simon Glassdc3dedf2015-06-23 15:38:51 -0600134 if (!err)
135 err = mmc_init(mmc);
136
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400137 if (err) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100138#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200139 printf("spl: mmc init failed with error: %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100140#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400141 hang();
142 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000143
Tom Rini37189a12012-08-14 09:19:44 -0700144 boot_mode = spl_boot_mode();
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200145 switch (boot_mode) {
146 case MMCSD_MODE_RAW:
147 debug("spl: mmc boot mode: raw\n");
148
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000149#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-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 Korsgaard2b75b0a2013-05-13 08:36:29 +0000155#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200156#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100157 err = mmc_load_image_raw_partition(mmc,
158 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200159 if (!err)
160 return;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200161#elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
162 err = mmc_load_image_raw_sector(mmc,
163 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
164 if (!err)
165 return;
166#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200167 case MMCSD_MODE_FS:
168 debug("spl: mmc boot mode: fs\n");
169
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200170#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
Guillaume GARDET592f9222014-10-15 17:53:12 +0200171#ifdef CONFIG_SPL_FAT_SUPPORT
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000172#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-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 Korsgaard7ad2cc72013-05-13 08:36:27 +0000179#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200180#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Dan Murphy773b5942014-01-16 11:23:29 -0600181 err = spl_load_image_fat(&mmc->block_dev,
Paul Kocialkowski91199f42015-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 Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200187#endif
Guillaume GARDET592f9222014-10-15 17:53:12 +0200188#ifdef CONFIG_SPL_EXT_SUPPORT
189#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-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 Rini0da113e2012-08-10 09:27:14 -0700196#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200197#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Guillaume GARDET592f9222014-10-15 17:53:12 +0200198 err = spl_load_image_ext(&mmc->block_dev,
Paul Kocialkowski91199f42015-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 Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200205#endif
Tom Rini7dbe63b2014-02-05 10:24:18 -0500206#ifdef CONFIG_SUPPORT_EMMC_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200207 case MMCSD_MODE_EMMCBOOT:
Tom Rini7dbe63b2014-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 Kocialkowski91199f42015-04-27 10:20:22 +0200213 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
Tom Rini7dbe63b2014-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 Kocialkowski91199f42015-04-27 10:20:22 +0200220 puts("spl: mmc partition switch failed\n");
Tom Rini7dbe63b2014-02-05 10:24:18 -0500221#endif
222 hang();
223 }
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200224
Tom Rini7dbe63b2014-02-05 10:24:18 -0500225#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-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 Rini7dbe63b2014-02-05 10:24:18 -0500231#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200232#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
Paul Kocialkowskiecb30132015-04-27 10:20:23 +0200233 err = mmc_load_image_raw_partition(mmc,
234 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200235 if (!err)
236 return;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200237#elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
238 err = mmc_load_image_raw_sector(mmc,
239 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
240 if (!err)
241 return;
242#endif
Tom Rini7dbe63b2014-02-05 10:24:18 -0500243#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200244 case MMCSD_MODE_UNDEFINED:
245 default:
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100246#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-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");
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100251#endif
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000252 hang();
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200253 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400254}