blob: d646dc8ffa301aef31232d4e3639d28ebb84fc50 [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>
Nikita Kiryanov4188ba32015-11-08 17:11:44 +020014#include <errno.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040015#include <mmc.h>
Tom Rinie4c444b2013-06-28 14:43:01 -040016#include <image.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040017
18DECLARE_GLOBAL_DATA_PTR;
19
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010020static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040021{
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020022 unsigned long count;
Peter Korsgaard2e222102013-03-21 04:55:17 +000023 u32 image_size_sectors;
24 struct image_header *header;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040025
26 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
Paul Kocialkowski91199f42015-04-27 10:20:22 +020027 sizeof(struct image_header));
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040028
29 /* read image header to find the image size & load address */
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020030 count = mmc->block_dev.block_read(0, sector, 1, header);
Simon Glassfdfa39d2015-06-23 15:38:47 -060031 debug("read sector %lx, count=%lu\n", sector, count);
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020032 if (count == 0)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040033 goto end;
34
Simon Glassfdfa39d2015-06-23 15:38:47 -060035 if (image_get_magic(header) != IH_MAGIC) {
36 puts("bad magic\n");
Tom Rinie4c444b2013-06-28 14:43:01 -040037 return -1;
Simon Glassfdfa39d2015-06-23 15:38:47 -060038 }
Tom Rinie4c444b2013-06-28 14:43:01 -040039
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040040 spl_parse_image_header(header);
41
42 /* convert size to sectors - round up */
Tom Rinif0881252012-08-14 10:25:15 -070043 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
Paul Kocialkowski91199f42015-04-27 10:20:22 +020044 mmc->read_bl_len;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040045
46 /* Read the header too to avoid extra memcpy */
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020047 count = mmc->block_dev.block_read(0, sector, image_size_sectors,
Gong Qianyu7ef4c452015-07-30 14:07:12 +080048 (void *)(ulong)spl_image.load_addr);
Simon Glassfdfa39d2015-06-23 15:38:47 -060049 debug("read %x sectors to %x\n", image_size_sectors,
50 spl_image.load_addr);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040051
52end:
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020053 if (count == 0) {
Paul Burton8112f5f2013-09-04 16:12:24 +010054#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020055 puts("spl: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010056#endif
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020057 return -1;
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020058 }
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020059
60 return 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040061}
62
Nikita Kiryanov4188ba32015-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 Kocialkowskib97300b2014-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 Kocialkowski3bc37b62015-05-22 12:45:35 +0200119 int err;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100120
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200121 err = get_partition_info(&mmc->block_dev, partition, &info);
122 if (err) {
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100123#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200124 puts("spl: partition error\n");
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100125#endif
126 return -1;
127 }
128
Stefan Roese4bfcc542015-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 Kocialkowskib97300b2014-11-08 23:14:56 +0100133 return mmc_load_image_raw_sector(mmc, info.start);
Stefan Roese4bfcc542015-07-20 11:20:39 +0200134#endif
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100135}
Nikita Kiryanovd074ebb2015-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 Kocialkowskib97300b2014-11-08 23:14:56 +0100142#endif
143
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000144#ifdef CONFIG_SPL_OS_BOOT
145static int mmc_load_image_raw_os(struct mmc *mmc)
146{
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +0200147 unsigned long count;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200148
Paul Kocialkowski3bc37b62015-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 Burton8112f5f2013-09-04 16:12:24 +0100154#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +0200155 puts("spl: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100156#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000157 return -1;
158 }
159
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100160 return mmc_load_image_raw_sector(mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200161 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000162}
Nikita Kiryanov339245b2015-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 Korsgaard2b75b0a2013-05-13 08:36:29 +0000172#endif
173
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400174void spl_mmc_load_image(void)
175{
176 struct mmc *mmc;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400177 u32 boot_mode;
Simon Glassdc3dedf2015-06-23 15:38:51 -0600178 int err = 0;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200179 __maybe_unused int part;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400180
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200181 if (spl_mmc_find_device(&mmc))
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400182 hang();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400183
Nikita Kiryanov4188ba32015-11-08 17:11:44 +0200184 err = mmc_init(mmc);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400185 if (err) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100186#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200187 printf("spl: mmc init failed with error: %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100188#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400189 hang();
190 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000191
Tom Rini37189a12012-08-14 09:19:44 -0700192 boot_mode = spl_boot_mode();
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200193 switch (boot_mode) {
194 case MMCSD_MODE_RAW:
195 debug("spl: mmc boot mode: raw\n");
196
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200197 if (!spl_start_uboot()) {
198 err = mmc_load_image_raw_os(mmc);
199 if (!err)
200 return;
201 }
Nikita Kiryanovd074ebb2015-11-08 17:11:46 +0200202
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100203 err = mmc_load_image_raw_partition(mmc,
204 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200205 if (!err)
206 return;
Nikita Kiryanovd074ebb2015-11-08 17:11:46 +0200207#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200208 err = mmc_load_image_raw_sector(mmc,
209 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
210 if (!err)
211 return;
212#endif
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200213 break;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200214 case MMCSD_MODE_FS:
215 debug("spl: mmc boot mode: fs\n");
216
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200217#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
Guillaume GARDET592f9222014-10-15 17:53:12 +0200218#ifdef CONFIG_SPL_FAT_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200219 if (!spl_start_uboot()) {
220 err = spl_load_image_fat_os(&mmc->block_dev,
221 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
222 if (!err)
223 return;
224 }
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200225#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Dan Murphy773b5942014-01-16 11:23:29 -0600226 err = spl_load_image_fat(&mmc->block_dev,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200227 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
228 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
229 if (!err)
230 return;
231#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200232#endif
Guillaume GARDET592f9222014-10-15 17:53:12 +0200233#ifdef CONFIG_SPL_EXT_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200234 if (!spl_start_uboot()) {
235 err = spl_load_image_ext_os(&mmc->block_dev,
236 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
237 if (!err)
238 return;
239 }
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200240#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Guillaume GARDET592f9222014-10-15 17:53:12 +0200241 err = spl_load_image_ext(&mmc->block_dev,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200242 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
243 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
244 if (!err)
245 return;
246#endif
247#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200248#endif
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200249 break;
Tom Rini7dbe63b2014-02-05 10:24:18 -0500250#ifdef CONFIG_SUPPORT_EMMC_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200251 case MMCSD_MODE_EMMCBOOT:
Tom Rini7dbe63b2014-02-05 10:24:18 -0500252 /*
253 * We need to check what the partition is configured to.
254 * 1 and 2 match up to boot0 / boot1 and 7 is user data
255 * which is the first physical partition (0).
256 */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200257 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
Tom Rini7dbe63b2014-02-05 10:24:18 -0500258
259 if (part == 7)
260 part = 0;
261
262 if (mmc_switch_part(0, part)) {
263#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200264 puts("spl: mmc partition switch failed\n");
Tom Rini7dbe63b2014-02-05 10:24:18 -0500265#endif
266 hang();
267 }
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200268
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200269 if (!spl_start_uboot()) {
270 err = mmc_load_image_raw_os(mmc);
271 if (!err)
272 return;
273 }
Paul Kocialkowskiecb30132015-04-27 10:20:23 +0200274 err = mmc_load_image_raw_partition(mmc,
275 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200276 if (!err)
277 return;
Nikita Kiryanovd074ebb2015-11-08 17:11:46 +0200278#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200279 err = mmc_load_image_raw_sector(mmc,
280 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
281 if (!err)
282 return;
283#endif
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200284 break;
Tom Rini7dbe63b2014-02-05 10:24:18 -0500285#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200286 case MMCSD_MODE_UNDEFINED:
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100287#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200288 default:
289 puts("spl: mmc: wrong boot mode\n");
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100290#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200291 }
Nikita Kiryanovfd61d392015-11-08 17:11:43 +0200292
293 hang();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400294}