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