blob: 5f1cfbf98e97daf107d0e07a649534534d153423 [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,
Simon Glassfdfa39d2015-06-23 15:38:47 -060047 (void *)spl_image.load_addr);
48 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
76 return mmc_load_image_raw_sector(mmc, info.start);
77}
78#endif
79
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000080#ifdef CONFIG_SPL_OS_BOOT
81static int mmc_load_image_raw_os(struct mmc *mmc)
82{
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020083 unsigned long count;
Paul Kocialkowski91199f42015-04-27 10:20:22 +020084
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020085 count = mmc->block_dev.block_read(0,
86 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
87 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
88 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
89 if (count == 0) {
Paul Burton8112f5f2013-09-04 16:12:24 +010090#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020091 puts("spl: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010092#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000093 return -1;
94 }
95
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010096 return mmc_load_image_raw_sector(mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +020097 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000098}
99#endif
100
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400101void spl_mmc_load_image(void)
102{
103 struct mmc *mmc;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400104 u32 boot_mode;
Simon Glassdc3dedf2015-06-23 15:38:51 -0600105 int err = 0;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200106 __maybe_unused int part;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400107
Simon Glassdc3dedf2015-06-23 15:38:51 -0600108#ifdef CONFIG_DM_MMC
109 struct udevice *dev;
110
111 mmc_initialize(NULL);
112 err = uclass_get_device(UCLASS_MMC, 0, &dev);
113 mmc = NULL;
114 if (!err)
115 mmc = mmc_get_mmc_dev(dev);
116#else
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400117 mmc_initialize(gd->bd);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200118
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400119 /* We register only one device. So, the dev id is always 0 */
120 mmc = find_mmc_device(0);
121 if (!mmc) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100122#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200123 puts("spl: mmc device not found\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100124#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400125 hang();
126 }
Simon Glassdc3dedf2015-06-23 15:38:51 -0600127#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400128
Simon Glassdc3dedf2015-06-23 15:38:51 -0600129 if (!err)
130 err = mmc_init(mmc);
131
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400132 if (err) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100133#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200134 printf("spl: mmc init failed with error: %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100135#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400136 hang();
137 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000138
Tom Rini37189a12012-08-14 09:19:44 -0700139 boot_mode = spl_boot_mode();
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200140 switch (boot_mode) {
141 case MMCSD_MODE_RAW:
142 debug("spl: mmc boot mode: raw\n");
143
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000144#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200145 if (!spl_start_uboot()) {
146 err = mmc_load_image_raw_os(mmc);
147 if (!err)
148 return;
149 }
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000150#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200151#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100152 err = mmc_load_image_raw_partition(mmc,
153 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200154 if (!err)
155 return;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200156#elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
157 err = mmc_load_image_raw_sector(mmc,
158 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
159 if (!err)
160 return;
161#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200162 case MMCSD_MODE_FS:
163 debug("spl: mmc boot mode: fs\n");
164
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200165#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
Guillaume GARDET592f9222014-10-15 17:53:12 +0200166#ifdef CONFIG_SPL_FAT_SUPPORT
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000167#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200168 if (!spl_start_uboot()) {
169 err = spl_load_image_fat_os(&mmc->block_dev,
170 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
171 if (!err)
172 return;
173 }
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000174#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200175#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Dan Murphy773b5942014-01-16 11:23:29 -0600176 err = spl_load_image_fat(&mmc->block_dev,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200177 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
178 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
179 if (!err)
180 return;
181#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200182#endif
Guillaume GARDET592f9222014-10-15 17:53:12 +0200183#ifdef CONFIG_SPL_EXT_SUPPORT
184#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200185 if (!spl_start_uboot()) {
186 err = spl_load_image_ext_os(&mmc->block_dev,
187 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
188 if (!err)
189 return;
190 }
Tom Rini0da113e2012-08-10 09:27:14 -0700191#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200192#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Guillaume GARDET592f9222014-10-15 17:53:12 +0200193 err = spl_load_image_ext(&mmc->block_dev,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200194 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
195 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
196 if (!err)
197 return;
198#endif
199#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200200#endif
Tom Rini7dbe63b2014-02-05 10:24:18 -0500201#ifdef CONFIG_SUPPORT_EMMC_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200202 case MMCSD_MODE_EMMCBOOT:
Tom Rini7dbe63b2014-02-05 10:24:18 -0500203 /*
204 * We need to check what the partition is configured to.
205 * 1 and 2 match up to boot0 / boot1 and 7 is user data
206 * which is the first physical partition (0).
207 */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200208 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
Tom Rini7dbe63b2014-02-05 10:24:18 -0500209
210 if (part == 7)
211 part = 0;
212
213 if (mmc_switch_part(0, part)) {
214#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200215 puts("spl: mmc partition switch failed\n");
Tom Rini7dbe63b2014-02-05 10:24:18 -0500216#endif
217 hang();
218 }
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200219
Tom Rini7dbe63b2014-02-05 10:24:18 -0500220#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200221 if (!spl_start_uboot()) {
222 err = mmc_load_image_raw_os(mmc);
223 if (!err)
224 return;
225 }
Tom Rini7dbe63b2014-02-05 10:24:18 -0500226#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200227#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
Paul Kocialkowskiecb30132015-04-27 10:20:23 +0200228 err = mmc_load_image_raw_partition(mmc,
229 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200230 if (!err)
231 return;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200232#elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
233 err = mmc_load_image_raw_sector(mmc,
234 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
235 if (!err)
236 return;
237#endif
Tom Rini7dbe63b2014-02-05 10:24:18 -0500238#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200239 case MMCSD_MODE_UNDEFINED:
240 default:
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100241#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200242 if (err)
243 puts("spl: mmc: no boot mode left to try\n");
244 else
245 puts("spl: mmc: wrong boot mode\n");
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100246#endif
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000247 hang();
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200248 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400249}