]>
Commit | Line | Data |
---|---|---|
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 | |
19 | DECLARE_GLOBAL_DATA_PTR; | |
20 | ||
2a2ee2ac SG |
21 | static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc, |
22 | ulong sector, struct image_header *header) | |
96debd1f SG |
23 | { |
24 | u32 image_size_sectors; | |
25 | unsigned long count; | |
7e0f2267 MV |
26 | int ret; |
27 | ||
2a2ee2ac | 28 | ret = spl_parse_image_header(spl_image, header); |
7e0f2267 MV |
29 | if (ret) |
30 | return ret; | |
96debd1f | 31 | |
96debd1f | 32 | /* convert size to sectors - round up */ |
2a2ee2ac | 33 | image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) / |
96debd1f SG |
34 | mmc->read_bl_len; |
35 | ||
36 | /* Read the header too to avoid extra memcpy */ | |
ef5609c3 | 37 | count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors, |
2a2ee2ac | 38 | (void *)(ulong)spl_image->load_addr); |
11e1479b | 39 | debug("read %x sectors to %lx\n", image_size_sectors, |
2a2ee2ac | 40 | spl_image->load_addr); |
96debd1f SG |
41 | if (count != image_size_sectors) |
42 | return -EIO; | |
43 | ||
44 | return 0; | |
45 | } | |
46 | ||
96debd1f SG |
47 | static ulong h_spl_load_read(struct spl_load_info *load, ulong sector, |
48 | ulong count, void *buf) | |
49 | { | |
50 | struct mmc *mmc = load->dev; | |
51 | ||
ef5609c3 | 52 | return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf); |
96debd1f | 53 | } |
96debd1f | 54 | |
2a2ee2ac SG |
55 | static int mmc_load_image_raw_sector(struct spl_image_info *spl_image, |
56 | struct mmc *mmc, unsigned long sector) | |
9ea5c6ef | 57 | { |
3bc37b6d | 58 | unsigned long count; |
2e222105 | 59 | struct image_header *header; |
96debd1f | 60 | int ret = 0; |
9ea5c6ef SS |
61 | |
62 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - | |
91199f4a | 63 | sizeof(struct image_header)); |
9ea5c6ef SS |
64 | |
65 | /* read image header to find the image size & load address */ | |
ef5609c3 | 66 | count = blk_dread(mmc_get_blk_desc(mmc), sector, 1, header); |
96debd1f SG |
67 | debug("hdr read sector %lx, count=%lu\n", sector, count); |
68 | if (count == 0) { | |
69 | ret = -EIO; | |
9ea5c6ef | 70 | goto end; |
96debd1f | 71 | } |
9ea5c6ef | 72 | |
4976f482 MY |
73 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
74 | image_get_magic(header) == FDT_MAGIC) { | |
96debd1f SG |
75 | struct spl_load_info load; |
76 | ||
77 | debug("Found FIT\n"); | |
78 | load.dev = mmc; | |
79 | load.priv = NULL; | |
eafd5410 | 80 | load.filename = NULL; |
96debd1f SG |
81 | load.bl_len = mmc->read_bl_len; |
82 | load.read = h_spl_load_read; | |
f4d7d859 | 83 | ret = spl_load_simple_fit(spl_image, &load, sector, header); |
4976f482 | 84 | } else { |
2a2ee2ac | 85 | ret = mmc_load_legacy(spl_image, mmc, sector, header); |
fdfa39d3 | 86 | } |
e4c444b3 | 87 | |
9ea5c6ef | 88 | end: |
96debd1f | 89 | if (ret) { |
8112f5fa | 90 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
5bbf4099 | 91 | puts("mmc_load_image_raw_sector: mmc block read error\n"); |
8112f5fa | 92 | #endif |
3bc37b6d | 93 | return -1; |
1ec26469 | 94 | } |
3bc37b6d PK |
95 | |
96 | return 0; | |
9ea5c6ef SS |
97 | } |
98 | ||
a1e56cf6 NK |
99 | int spl_mmc_get_device_index(u32 boot_device) |
100 | { | |
101 | switch (boot_device) { | |
102 | case BOOT_DEVICE_MMC1: | |
103 | return 0; | |
104 | case BOOT_DEVICE_MMC2: | |
105 | case BOOT_DEVICE_MMC2_2: | |
106 | return 1; | |
107 | } | |
108 | ||
109 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT | |
110 | printf("spl: unsupported mmc boot device.\n"); | |
111 | #endif | |
112 | ||
113 | return -ENODEV; | |
114 | } | |
115 | ||
99c7a51a | 116 | static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device) |
4188ba32 | 117 | { |
b4857aa9 | 118 | #ifdef CONFIG_DM_MMC |
4188ba32 | 119 | struct udevice *dev; |
b4857aa9 | 120 | #endif |
a1e56cf6 NK |
121 | int err, mmc_dev; |
122 | ||
123 | mmc_dev = spl_mmc_get_device_index(boot_device); | |
124 | if (mmc_dev < 0) | |
125 | return mmc_dev; | |
4188ba32 NK |
126 | |
127 | err = mmc_initialize(NULL); | |
128 | if (err) { | |
129 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT | |
130 | printf("spl: could not initialize mmc. error: %d\n", err); | |
131 | #endif | |
132 | return err; | |
133 | } | |
134 | ||
b4857aa9 | 135 | #ifdef CONFIG_DM_MMC |
a1e56cf6 | 136 | err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev); |
b4857aa9 SG |
137 | if (!err) |
138 | *mmcp = mmc_get_mmc_dev(dev); | |
4188ba32 | 139 | #else |
b4857aa9 SG |
140 | *mmcp = find_mmc_device(mmc_dev); |
141 | err = *mmcp ? 0 : -ENODEV; | |
142 | #endif | |
4188ba32 NK |
143 | if (err) { |
144 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT | |
b4857aa9 | 145 | printf("spl: could not find mmc device. error: %d\n", err); |
4188ba32 NK |
146 | #endif |
147 | return err; | |
148 | } | |
149 | ||
4188ba32 NK |
150 | return 0; |
151 | } | |
4188ba32 | 152 | |
949123e3 | 153 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
2a2ee2ac SG |
154 | static int mmc_load_image_raw_partition(struct spl_image_info *spl_image, |
155 | struct mmc *mmc, int partition) | |
b97300b6 PK |
156 | { |
157 | disk_partition_t info; | |
3bc37b6d | 158 | int err; |
b97300b6 | 159 | |
f0fb4fa7 DW |
160 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE |
161 | int type_part; | |
162 | /* Only support MBR so DOS_ENTRY_NUMBERS */ | |
163 | for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) { | |
164 | err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info); | |
165 | if (err) | |
166 | continue; | |
167 | if (info.sys_ind == | |
168 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) { | |
169 | partition = type_part; | |
170 | break; | |
171 | } | |
172 | } | |
173 | #endif | |
174 | ||
1e2b3ef8 | 175 | err = part_get_info(mmc_get_blk_desc(mmc), partition, &info); |
3bc37b6d | 176 | if (err) { |
b97300b6 | 177 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
1ec26469 | 178 | puts("spl: partition error\n"); |
b97300b6 PK |
179 | #endif |
180 | return -1; | |
181 | } | |
182 | ||
38fed8ab | 183 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR |
2a2ee2ac SG |
184 | return mmc_load_image_raw_sector(spl_image, mmc, |
185 | info.start + CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); | |
4bfcc54c | 186 | #else |
2a2ee2ac | 187 | return mmc_load_image_raw_sector(spl_image, mmc, info.start); |
4bfcc54c | 188 | #endif |
b97300b6 PK |
189 | } |
190 | #endif | |
191 | ||
2b75b0ad | 192 | #ifdef CONFIG_SPL_OS_BOOT |
2a2ee2ac SG |
193 | static int mmc_load_image_raw_os(struct spl_image_info *spl_image, |
194 | struct mmc *mmc) | |
2b75b0ad | 195 | { |
3bc37b6d | 196 | unsigned long count; |
811906ae | 197 | int ret; |
91199f4a | 198 | |
87bce4e5 | 199 | count = blk_dread(mmc_get_blk_desc(mmc), |
3bc37b6d PK |
200 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, |
201 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, | |
202 | (void *) CONFIG_SYS_SPL_ARGS_ADDR); | |
203 | if (count == 0) { | |
8112f5fa | 204 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
5bbf4099 | 205 | puts("mmc_load_image_raw_os: mmc block read error\n"); |
8112f5fa | 206 | #endif |
2b75b0ad PK |
207 | return -1; |
208 | } | |
209 | ||
2a2ee2ac | 210 | ret = mmc_load_image_raw_sector(spl_image, mmc, |
91199f4a | 211 | CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
811906ae LV |
212 | if (ret) |
213 | return ret; | |
214 | ||
2a2ee2ac | 215 | if (spl_image->os != IH_OS_LINUX) { |
811906ae LV |
216 | puts("Expected Linux image is not found. Trying to start U-boot\n"); |
217 | return -ENOENT; | |
218 | } | |
219 | ||
220 | return 0; | |
2b75b0ad | 221 | } |
339245b7 NK |
222 | #else |
223 | int spl_start_uboot(void) | |
224 | { | |
225 | return 1; | |
226 | } | |
2a2ee2ac SG |
227 | static int mmc_load_image_raw_os(struct spl_image_info *spl_image, |
228 | struct mmc *mmc) | |
339245b7 NK |
229 | { |
230 | return -ENOSYS; | |
231 | } | |
2b75b0ad PK |
232 | #endif |
233 | ||
f52b7293 | 234 | #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION |
2a2ee2ac | 235 | static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc) |
f52b7293 NK |
236 | { |
237 | int err = -ENOSYS; | |
238 | ||
239 | #ifdef CONFIG_SPL_FAT_SUPPORT | |
240 | if (!spl_start_uboot()) { | |
710e9ca5 | 241 | err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc), |
f52b7293 NK |
242 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
243 | if (!err) | |
244 | return err; | |
245 | } | |
246 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME | |
710e9ca5 | 247 | err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc), |
f52b7293 NK |
248 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
249 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); | |
250 | if (!err) | |
251 | return err; | |
252 | #endif | |
253 | #endif | |
254 | #ifdef CONFIG_SPL_EXT_SUPPORT | |
255 | if (!spl_start_uboot()) { | |
1a92541d | 256 | err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc), |
f52b7293 NK |
257 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
258 | if (!err) | |
259 | return err; | |
260 | } | |
261 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME | |
1a92541d | 262 | err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc), |
f52b7293 NK |
263 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
264 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); | |
265 | if (!err) | |
266 | return err; | |
267 | #endif | |
268 | #endif | |
269 | ||
270 | #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) | |
271 | err = -ENOENT; | |
272 | #endif | |
273 | ||
274 | return err; | |
275 | } | |
276 | #else | |
2a2ee2ac | 277 | static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc) |
f52b7293 NK |
278 | { |
279 | return -ENOSYS; | |
280 | } | |
281 | #endif | |
282 | ||
09410c65 MV |
283 | int spl_mmc_load_image(struct spl_image_info *spl_image, |
284 | struct spl_boot_device *bootdev) | |
9ea5c6ef | 285 | { |
d773a008 | 286 | struct mmc *mmc = NULL; |
9ea5c6ef | 287 | u32 boot_mode; |
dc3dedfe | 288 | int err = 0; |
91199f4a | 289 | __maybe_unused int part; |
9ea5c6ef | 290 | |
ecdfd69a | 291 | err = spl_mmc_find_device(&mmc, bootdev->boot_device); |
36afd451 NK |
292 | if (err) |
293 | return err; | |
9ea5c6ef | 294 | |
4188ba32 | 295 | err = mmc_init(mmc); |
9ea5c6ef | 296 | if (err) { |
8112f5fa | 297 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
91199f4a | 298 | printf("spl: mmc init failed with error: %d\n", err); |
8112f5fa | 299 | #endif |
36afd451 | 300 | return err; |
9ea5c6ef | 301 | } |
79adb7a2 | 302 | |
ecdfd69a | 303 | boot_mode = spl_boot_mode(bootdev->boot_device); |
36afd451 | 304 | err = -EINVAL; |
91199f4a | 305 | switch (boot_mode) { |
83cdf6fa NK |
306 | case MMCSD_MODE_EMMCBOOT: |
307 | /* | |
308 | * We need to check what the partition is configured to. | |
309 | * 1 and 2 match up to boot0 / boot1 and 7 is user data | |
310 | * which is the first physical partition (0). | |
311 | */ | |
312 | part = (mmc->part_config >> 3) & PART_ACCESS_MASK; | |
313 | ||
314 | if (part == 7) | |
315 | part = 0; | |
316 | ||
b5b838f1 MV |
317 | if (CONFIG_IS_ENABLED(MMC_TINY)) |
318 | err = mmc_switch_part(mmc, part); | |
319 | else | |
320 | err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part); | |
321 | ||
36afd451 | 322 | if (err) { |
83cdf6fa NK |
323 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
324 | puts("spl: mmc partition switch failed\n"); | |
325 | #endif | |
36afd451 | 326 | return err; |
83cdf6fa NK |
327 | } |
328 | /* Fall through */ | |
91199f4a PK |
329 | case MMCSD_MODE_RAW: |
330 | debug("spl: mmc boot mode: raw\n"); | |
331 | ||
91199f4a | 332 | if (!spl_start_uboot()) { |
2a2ee2ac | 333 | err = mmc_load_image_raw_os(spl_image, mmc); |
91199f4a | 334 | if (!err) |
36afd451 | 335 | return err; |
91199f4a | 336 | } |
949123e3 | 337 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
2a2ee2ac | 338 | err = mmc_load_image_raw_partition(spl_image, mmc, |
b97300b6 | 339 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); |
3ae8f4c8 | 340 | if (!err) |
36afd451 | 341 | return err; |
949123e3 | 342 | #endif |
38fed8ab | 343 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR |
2a2ee2ac | 344 | err = mmc_load_image_raw_sector(spl_image, mmc, |
773b5940 | 345 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
91199f4a | 346 | if (!err) |
36afd451 | 347 | return err; |
3ae8f4c8 | 348 | #endif |
86a0df73 | 349 | /* If RAW mode fails, try FS mode. */ |
91199f4a PK |
350 | case MMCSD_MODE_FS: |
351 | debug("spl: mmc boot mode: fs\n"); | |
352 | ||
2a2ee2ac | 353 | err = spl_mmc_do_fs_boot(spl_image, mmc); |
91199f4a | 354 | if (!err) |
36afd451 | 355 | return err; |
f52b7293 | 356 | |
fd61d399 | 357 | break; |
8112f5fa | 358 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
fd61d399 NK |
359 | default: |
360 | puts("spl: mmc: wrong boot mode\n"); | |
8112f5fa | 361 | #endif |
91199f4a | 362 | } |
fd61d399 | 363 | |
36afd451 | 364 | return err; |
9ea5c6ef | 365 | } |
0fed9c7e | 366 | |
ebc4ef61 SG |
367 | SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image); |
368 | SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image); | |
369 | SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image); |