]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/spl/spl_mmc.c
ARM: hikey: Simplify README instructions.
[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
96debd1f
SG
44static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
45 ulong count, void *buf)
46{
47 struct mmc *mmc = load->dev;
48
49 return mmc->block_dev.block_read(&mmc->block_dev, sector, count, buf);
50}
96debd1f 51
b97300b6 52static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
9ea5c6ef 53{
3bc37b6d 54 unsigned long count;
2e222105 55 struct image_header *header;
96debd1f 56 int ret = 0;
9ea5c6ef
SS
57
58 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
91199f4a 59 sizeof(struct image_header));
9ea5c6ef
SS
60
61 /* read image header to find the image size & load address */
7c4213f6 62 count = mmc->block_dev.block_read(&mmc->block_dev, sector, 1, header);
96debd1f
SG
63 debug("hdr read sector %lx, count=%lu\n", sector, count);
64 if (count == 0) {
65 ret = -EIO;
9ea5c6ef 66 goto end;
96debd1f 67 }
9ea5c6ef 68
4976f482
MY
69 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
70 image_get_magic(header) == FDT_MAGIC) {
96debd1f
SG
71 struct spl_load_info load;
72
73 debug("Found FIT\n");
74 load.dev = mmc;
75 load.priv = NULL;
76 load.bl_len = mmc->read_bl_len;
77 load.read = h_spl_load_read;
78 ret = spl_load_simple_fit(&load, sector, header);
4976f482
MY
79 } else {
80 ret = mmc_load_legacy(mmc, sector, header);
fdfa39d3 81 }
e4c444b3 82
9ea5c6ef 83end:
96debd1f 84 if (ret) {
8112f5fa 85#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
1ec26469 86 puts("spl: mmc block read error\n");
8112f5fa 87#endif
3bc37b6d 88 return -1;
1ec26469 89 }
3bc37b6d
PK
90
91 return 0;
9ea5c6ef
SS
92}
93
a1e56cf6
NK
94int spl_mmc_get_device_index(u32 boot_device)
95{
96 switch (boot_device) {
97 case BOOT_DEVICE_MMC1:
98 return 0;
99 case BOOT_DEVICE_MMC2:
100 case BOOT_DEVICE_MMC2_2:
101 return 1;
102 }
103
104#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
105 printf("spl: unsupported mmc boot device.\n");
106#endif
107
108 return -ENODEV;
109}
110
99c7a51a 111static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
4188ba32 112{
b4857aa9 113#ifdef CONFIG_DM_MMC
4188ba32 114 struct udevice *dev;
b4857aa9 115#endif
a1e56cf6
NK
116 int err, mmc_dev;
117
118 mmc_dev = spl_mmc_get_device_index(boot_device);
119 if (mmc_dev < 0)
120 return mmc_dev;
4188ba32
NK
121
122 err = mmc_initialize(NULL);
123 if (err) {
124#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
125 printf("spl: could not initialize mmc. error: %d\n", err);
126#endif
127 return err;
128 }
129
b4857aa9 130#ifdef CONFIG_DM_MMC
a1e56cf6 131 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
b4857aa9
SG
132 if (!err)
133 *mmcp = mmc_get_mmc_dev(dev);
4188ba32 134#else
b4857aa9
SG
135 *mmcp = find_mmc_device(mmc_dev);
136 err = *mmcp ? 0 : -ENODEV;
137#endif
4188ba32
NK
138 if (err) {
139#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
b4857aa9 140 printf("spl: could not find mmc device. error: %d\n", err);
4188ba32
NK
141#endif
142 return err;
143 }
144
4188ba32
NK
145 return 0;
146}
4188ba32 147
b97300b6
PK
148#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
149static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
150{
151 disk_partition_t info;
3bc37b6d 152 int err;
b97300b6 153
3e8bd469 154 err = part_get_info(&mmc->block_dev, partition, &info);
3bc37b6d 155 if (err) {
b97300b6 156#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
1ec26469 157 puts("spl: partition error\n");
b97300b6
PK
158#endif
159 return -1;
160 }
161
4bfcc54c
SR
162#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
163 return mmc_load_image_raw_sector(mmc, info.start +
164 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
165#else
b97300b6 166 return mmc_load_image_raw_sector(mmc, info.start);
4bfcc54c 167#endif
b97300b6 168}
d074ebb9
NK
169#else
170#define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
171static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
172{
173 return -ENOSYS;
174}
b97300b6
PK
175#endif
176
2b75b0ad
PK
177#ifdef CONFIG_SPL_OS_BOOT
178static int mmc_load_image_raw_os(struct mmc *mmc)
179{
3bc37b6d 180 unsigned long count;
811906ae 181 int ret;
91199f4a 182
7c4213f6 183 count = mmc->block_dev.block_read(&mmc->block_dev,
3bc37b6d
PK
184 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
185 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
186 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
187 if (count == 0) {
8112f5fa 188#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
1ec26469 189 puts("spl: mmc block read error\n");
8112f5fa 190#endif
2b75b0ad
PK
191 return -1;
192 }
193
811906ae 194 ret = mmc_load_image_raw_sector(mmc,
91199f4a 195 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
811906ae
LV
196 if (ret)
197 return ret;
198
199 if (spl_image.os != IH_OS_LINUX) {
200 puts("Expected Linux image is not found. Trying to start U-boot\n");
201 return -ENOENT;
202 }
203
204 return 0;
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}