]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/spl/spl_mmc.c
Merge git://git.denx.de/u-boot-marvell
[people/ms/u-boot.git] / common / spl / spl_mmc.c
CommitLineData
9ea5c6ef
SS
1/*
2 * (C) Copyright 2010
3 * Texas Instruments, <www.ti.com>
4 *
5 * Aneesh V <aneesh@ti.com>
6 *
1a459660 7 * SPDX-License-Identifier: GPL-2.0+
9ea5c6ef
SS
8 */
9#include <common.h>
dc3dedfe 10#include <dm.h>
47f7bcae 11#include <spl.h>
91199f4a 12#include <linux/compiler.h>
36afd451 13#include <errno.h>
9ea5c6ef 14#include <asm/u-boot.h>
4188ba32 15#include <errno.h>
9ea5c6ef 16#include <mmc.h>
e4c444b3 17#include <image.h>
9ea5c6ef
SS
18
19DECLARE_GLOBAL_DATA_PTR;
20
96debd1f
SG
21static int mmc_load_legacy(struct mmc *mmc, ulong sector,
22 struct image_header *header)
23{
24 u32 image_size_sectors;
25 unsigned long count;
26
27 spl_parse_image_header(header);
28 /* convert size to sectors - round up */
29 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
30 mmc->read_bl_len;
31
32 /* Read the header too to avoid extra memcpy */
33 count = mmc->block_dev.block_read(&mmc->block_dev, sector,
34 image_size_sectors,
35 (void *)(ulong)spl_image.load_addr);
36 debug("read %x sectors to %x\n", image_size_sectors,
37 spl_image.load_addr);
38 if (count != image_size_sectors)
39 return -EIO;
40
41 return 0;
42}
43
44#ifdef CONFIG_SPL_LOAD_FIT
45static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
46 ulong count, void *buf)
47{
48 struct mmc *mmc = load->dev;
49
50 return mmc->block_dev.block_read(&mmc->block_dev, sector, count, buf);
51}
52#endif
53
b97300b6 54static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
9ea5c6ef 55{
3bc37b6d 56 unsigned long count;
2e222105 57 struct image_header *header;
96debd1f 58 int ret = 0;
9ea5c6ef
SS
59
60 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
91199f4a 61 sizeof(struct image_header));
9ea5c6ef
SS
62
63 /* read image header to find the image size & load address */
7c4213f6 64 count = mmc->block_dev.block_read(&mmc->block_dev, sector, 1, header);
96debd1f
SG
65 debug("hdr read sector %lx, count=%lu\n", sector, count);
66 if (count == 0) {
67 ret = -EIO;
9ea5c6ef 68 goto end;
96debd1f 69 }
9ea5c6ef 70
96debd1f
SG
71 switch (image_get_magic(header)) {
72 case IH_MAGIC:
73 ret = mmc_load_legacy(mmc, sector, header);
74 break;
75#ifdef CONFIG_SPL_LOAD_FIT
76 case FDT_MAGIC: {
77 struct spl_load_info load;
78
79 debug("Found FIT\n");
80 load.dev = mmc;
81 load.priv = NULL;
82 load.bl_len = mmc->read_bl_len;
83 load.read = h_spl_load_read;
84 ret = spl_load_simple_fit(&load, sector, header);
85 break;
86 }
87#endif
88 default:
fdfa39d3 89 puts("bad magic\n");
e4c444b3 90 return -1;
fdfa39d3 91 }
e4c444b3 92
9ea5c6ef 93end:
96debd1f 94 if (ret) {
8112f5fa 95#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
1ec26469 96 puts("spl: mmc block read error\n");
8112f5fa 97#endif
3bc37b6d 98 return -1;
1ec26469 99 }
3bc37b6d
PK
100
101 return 0;
9ea5c6ef
SS
102}
103
a1e56cf6
NK
104int spl_mmc_get_device_index(u32 boot_device)
105{
106 switch (boot_device) {
107 case BOOT_DEVICE_MMC1:
108 return 0;
109 case BOOT_DEVICE_MMC2:
110 case BOOT_DEVICE_MMC2_2:
111 return 1;
112 }
113
114#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
115 printf("spl: unsupported mmc boot device.\n");
116#endif
117
118 return -ENODEV;
119}
120
99c7a51a 121static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
4188ba32 122{
b4857aa9 123#ifdef CONFIG_DM_MMC
4188ba32 124 struct udevice *dev;
b4857aa9 125#endif
a1e56cf6
NK
126 int err, mmc_dev;
127
128 mmc_dev = spl_mmc_get_device_index(boot_device);
129 if (mmc_dev < 0)
130 return mmc_dev;
4188ba32
NK
131
132 err = mmc_initialize(NULL);
133 if (err) {
134#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
135 printf("spl: could not initialize mmc. error: %d\n", err);
136#endif
137 return err;
138 }
139
b4857aa9 140#ifdef CONFIG_DM_MMC
a1e56cf6 141 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
b4857aa9
SG
142 if (!err)
143 *mmcp = mmc_get_mmc_dev(dev);
4188ba32 144#else
b4857aa9
SG
145 *mmcp = find_mmc_device(mmc_dev);
146 err = *mmcp ? 0 : -ENODEV;
147#endif
4188ba32
NK
148 if (err) {
149#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
b4857aa9 150 printf("spl: could not find mmc device. error: %d\n", err);
4188ba32
NK
151#endif
152 return err;
153 }
154
4188ba32
NK
155 return 0;
156}
4188ba32 157
b97300b6
PK
158#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
159static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
160{
161 disk_partition_t info;
3bc37b6d 162 int err;
b97300b6 163
3bc37b6d
PK
164 err = get_partition_info(&mmc->block_dev, partition, &info);
165 if (err) {
b97300b6 166#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
1ec26469 167 puts("spl: partition error\n");
b97300b6
PK
168#endif
169 return -1;
170 }
171
4bfcc54c
SR
172#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
173 return mmc_load_image_raw_sector(mmc, info.start +
174 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
175#else
b97300b6 176 return mmc_load_image_raw_sector(mmc, info.start);
4bfcc54c 177#endif
b97300b6 178}
d074ebb9
NK
179#else
180#define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
181static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
182{
183 return -ENOSYS;
184}
b97300b6
PK
185#endif
186
2b75b0ad
PK
187#ifdef CONFIG_SPL_OS_BOOT
188static int mmc_load_image_raw_os(struct mmc *mmc)
189{
3bc37b6d 190 unsigned long count;
91199f4a 191
7c4213f6 192 count = mmc->block_dev.block_read(&mmc->block_dev,
3bc37b6d
PK
193 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
194 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
195 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
196 if (count == 0) {
8112f5fa 197#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
1ec26469 198 puts("spl: mmc block read error\n");
8112f5fa 199#endif
2b75b0ad
PK
200 return -1;
201 }
202
b97300b6 203 return mmc_load_image_raw_sector(mmc,
91199f4a 204 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
2b75b0ad 205}
339245b7
NK
206#else
207int spl_start_uboot(void)
208{
209 return 1;
210}
211static int mmc_load_image_raw_os(struct mmc *mmc)
212{
213 return -ENOSYS;
214}
2b75b0ad
PK
215#endif
216
f52b7293
NK
217#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
218int spl_mmc_do_fs_boot(struct mmc *mmc)
219{
220 int err = -ENOSYS;
221
222#ifdef CONFIG_SPL_FAT_SUPPORT
223 if (!spl_start_uboot()) {
224 err = spl_load_image_fat_os(&mmc->block_dev,
225 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
226 if (!err)
227 return err;
228 }
229#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
230 err = spl_load_image_fat(&mmc->block_dev,
231 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
232 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
233 if (!err)
234 return err;
235#endif
236#endif
237#ifdef CONFIG_SPL_EXT_SUPPORT
238 if (!spl_start_uboot()) {
239 err = spl_load_image_ext_os(&mmc->block_dev,
240 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
241 if (!err)
242 return err;
243 }
244#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
245 err = spl_load_image_ext(&mmc->block_dev,
246 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
247 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
248 if (!err)
249 return err;
250#endif
251#endif
252
253#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
254 err = -ENOENT;
255#endif
256
257 return err;
258}
259#else
260int spl_mmc_do_fs_boot(struct mmc *mmc)
261{
262 return -ENOSYS;
263}
264#endif
265
a1e56cf6 266int spl_mmc_load_image(u32 boot_device)
9ea5c6ef 267{
d773a008 268 struct mmc *mmc = NULL;
9ea5c6ef 269 u32 boot_mode;
dc3dedfe 270 int err = 0;
91199f4a 271 __maybe_unused int part;
9ea5c6ef 272
a1e56cf6 273 err = spl_mmc_find_device(&mmc, boot_device);
36afd451
NK
274 if (err)
275 return err;
9ea5c6ef 276
4188ba32 277 err = mmc_init(mmc);
9ea5c6ef 278 if (err) {
8112f5fa 279#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
91199f4a 280 printf("spl: mmc init failed with error: %d\n", err);
8112f5fa 281#endif
36afd451 282 return err;
9ea5c6ef 283 }
79adb7a2 284
37189a19 285 boot_mode = spl_boot_mode();
36afd451 286 err = -EINVAL;
91199f4a 287 switch (boot_mode) {
83cdf6fa
NK
288 case MMCSD_MODE_EMMCBOOT:
289 /*
290 * We need to check what the partition is configured to.
291 * 1 and 2 match up to boot0 / boot1 and 7 is user data
292 * which is the first physical partition (0).
293 */
294 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
295
296 if (part == 7)
297 part = 0;
298
36afd451
NK
299 err = mmc_switch_part(0, part);
300 if (err) {
83cdf6fa
NK
301#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
302 puts("spl: mmc partition switch failed\n");
303#endif
36afd451 304 return err;
83cdf6fa
NK
305 }
306 /* Fall through */
91199f4a
PK
307 case MMCSD_MODE_RAW:
308 debug("spl: mmc boot mode: raw\n");
309
91199f4a
PK
310 if (!spl_start_uboot()) {
311 err = mmc_load_image_raw_os(mmc);
312 if (!err)
36afd451 313 return err;
91199f4a 314 }
d074ebb9 315
b97300b6
PK
316 err = mmc_load_image_raw_partition(mmc,
317 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
3ae8f4c8 318 if (!err)
36afd451 319 return err;
d074ebb9 320#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
b97300b6 321 err = mmc_load_image_raw_sector(mmc,
773b5940 322 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
91199f4a 323 if (!err)
36afd451 324 return err;
3ae8f4c8 325#endif
86a0df73 326 /* If RAW mode fails, try FS mode. */
91199f4a
PK
327 case MMCSD_MODE_FS:
328 debug("spl: mmc boot mode: fs\n");
329
f52b7293 330 err = spl_mmc_do_fs_boot(mmc);
91199f4a 331 if (!err)
36afd451 332 return err;
f52b7293 333
fd61d399 334 break;
91199f4a 335 case MMCSD_MODE_UNDEFINED:
8112f5fa 336#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
fd61d399
NK
337 default:
338 puts("spl: mmc: wrong boot mode\n");
8112f5fa 339#endif
91199f4a 340 }
fd61d399 341
36afd451 342 return err;
9ea5c6ef 343}