]>
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> |
9ea5c6ef | 13 | #include <asm/u-boot.h> |
4188ba32 | 14 | #include <errno.h> |
9ea5c6ef | 15 | #include <mmc.h> |
e4c444b3 | 16 | #include <image.h> |
9ea5c6ef SS |
17 | |
18 | DECLARE_GLOBAL_DATA_PTR; | |
19 | ||
b97300b6 | 20 | static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector) |
9ea5c6ef | 21 | { |
3bc37b6d | 22 | unsigned long count; |
2e222105 PK |
23 | u32 image_size_sectors; |
24 | struct image_header *header; | |
9ea5c6ef SS |
25 | |
26 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - | |
91199f4a | 27 | sizeof(struct image_header)); |
9ea5c6ef SS |
28 | |
29 | /* read image header to find the image size & load address */ | |
3bc37b6d | 30 | count = mmc->block_dev.block_read(0, sector, 1, header); |
fdfa39d3 | 31 | debug("read sector %lx, count=%lu\n", sector, count); |
3bc37b6d | 32 | if (count == 0) |
9ea5c6ef SS |
33 | goto end; |
34 | ||
fdfa39d3 SG |
35 | if (image_get_magic(header) != IH_MAGIC) { |
36 | puts("bad magic\n"); | |
e4c444b3 | 37 | return -1; |
fdfa39d3 | 38 | } |
e4c444b3 | 39 | |
9ea5c6ef SS |
40 | spl_parse_image_header(header); |
41 | ||
42 | /* convert size to sectors - round up */ | |
f0881250 | 43 | image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) / |
91199f4a | 44 | mmc->read_bl_len; |
9ea5c6ef SS |
45 | |
46 | /* Read the header too to avoid extra memcpy */ | |
3bc37b6d | 47 | count = mmc->block_dev.block_read(0, sector, image_size_sectors, |
7ef4c45c | 48 | (void *)(ulong)spl_image.load_addr); |
fdfa39d3 SG |
49 | debug("read %x sectors to %x\n", image_size_sectors, |
50 | spl_image.load_addr); | |
9ea5c6ef SS |
51 | |
52 | end: | |
1ec26469 | 53 | if (count == 0) { |
8112f5fa | 54 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
1ec26469 | 55 | puts("spl: mmc block read error\n"); |
8112f5fa | 56 | #endif |
3bc37b6d | 57 | return -1; |
1ec26469 | 58 | } |
3bc37b6d PK |
59 | |
60 | return 0; | |
9ea5c6ef SS |
61 | } |
62 | ||
4188ba32 NK |
63 | #ifdef CONFIG_DM_MMC |
64 | static 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 | |
90 | static 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 | ||
b97300b6 PK |
115 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION |
116 | static int mmc_load_image_raw_partition(struct mmc *mmc, int partition) | |
117 | { | |
118 | disk_partition_t info; | |
3bc37b6d | 119 | int err; |
b97300b6 | 120 | |
3bc37b6d PK |
121 | err = get_partition_info(&mmc->block_dev, partition, &info); |
122 | if (err) { | |
b97300b6 | 123 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
1ec26469 | 124 | puts("spl: partition error\n"); |
b97300b6 PK |
125 | #endif |
126 | return -1; | |
127 | } | |
128 | ||
4bfcc54c SR |
129 | #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 | |
b97300b6 | 133 | return mmc_load_image_raw_sector(mmc, info.start); |
4bfcc54c | 134 | #endif |
b97300b6 | 135 | } |
d074ebb9 NK |
136 | #else |
137 | #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1 | |
138 | static int mmc_load_image_raw_partition(struct mmc *mmc, int partition) | |
139 | { | |
140 | return -ENOSYS; | |
141 | } | |
b97300b6 PK |
142 | #endif |
143 | ||
2b75b0ad PK |
144 | #ifdef CONFIG_SPL_OS_BOOT |
145 | static int mmc_load_image_raw_os(struct mmc *mmc) | |
146 | { | |
3bc37b6d | 147 | unsigned long count; |
91199f4a | 148 | |
3bc37b6d PK |
149 | 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) { | |
8112f5fa | 154 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
1ec26469 | 155 | puts("spl: mmc block read error\n"); |
8112f5fa | 156 | #endif |
2b75b0ad PK |
157 | return -1; |
158 | } | |
159 | ||
b97300b6 | 160 | return mmc_load_image_raw_sector(mmc, |
91199f4a | 161 | CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
2b75b0ad | 162 | } |
339245b7 NK |
163 | #else |
164 | int spl_start_uboot(void) | |
165 | { | |
166 | return 1; | |
167 | } | |
168 | static int mmc_load_image_raw_os(struct mmc *mmc) | |
169 | { | |
170 | return -ENOSYS; | |
171 | } | |
2b75b0ad PK |
172 | #endif |
173 | ||
9ea5c6ef SS |
174 | void spl_mmc_load_image(void) |
175 | { | |
176 | struct mmc *mmc; | |
9ea5c6ef | 177 | u32 boot_mode; |
dc3dedfe | 178 | int err = 0; |
91199f4a | 179 | __maybe_unused int part; |
9ea5c6ef | 180 | |
4188ba32 | 181 | if (spl_mmc_find_device(&mmc)) |
9ea5c6ef | 182 | hang(); |
9ea5c6ef | 183 | |
4188ba32 | 184 | err = mmc_init(mmc); |
9ea5c6ef | 185 | if (err) { |
8112f5fa | 186 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
91199f4a | 187 | printf("spl: mmc init failed with error: %d\n", err); |
8112f5fa | 188 | #endif |
9ea5c6ef SS |
189 | hang(); |
190 | } | |
79adb7a2 | 191 | |
37189a19 | 192 | boot_mode = spl_boot_mode(); |
91199f4a PK |
193 | switch (boot_mode) { |
194 | case MMCSD_MODE_RAW: | |
195 | debug("spl: mmc boot mode: raw\n"); | |
196 | ||
91199f4a PK |
197 | if (!spl_start_uboot()) { |
198 | err = mmc_load_image_raw_os(mmc); | |
199 | if (!err) | |
200 | return; | |
201 | } | |
d074ebb9 | 202 | |
b97300b6 PK |
203 | err = mmc_load_image_raw_partition(mmc, |
204 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); | |
3ae8f4c8 PK |
205 | if (!err) |
206 | return; | |
d074ebb9 | 207 | #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) |
b97300b6 | 208 | err = mmc_load_image_raw_sector(mmc, |
773b5940 | 209 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
91199f4a PK |
210 | if (!err) |
211 | return; | |
3ae8f4c8 | 212 | #endif |
fd61d399 | 213 | break; |
91199f4a PK |
214 | case MMCSD_MODE_FS: |
215 | debug("spl: mmc boot mode: fs\n"); | |
216 | ||
3ae8f4c8 | 217 | #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION |
592f9222 | 218 | #ifdef CONFIG_SPL_FAT_SUPPORT |
91199f4a PK |
219 | 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 | } | |
3ae8f4c8 | 225 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
773b5940 | 226 | err = spl_load_image_fat(&mmc->block_dev, |
91199f4a PK |
227 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
228 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); | |
229 | if (!err) | |
230 | return; | |
231 | #endif | |
3ae8f4c8 | 232 | #endif |
592f9222 | 233 | #ifdef CONFIG_SPL_EXT_SUPPORT |
91199f4a PK |
234 | 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 | } | |
3ae8f4c8 | 240 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
592f9222 | 241 | err = spl_load_image_ext(&mmc->block_dev, |
91199f4a PK |
242 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
243 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); | |
244 | if (!err) | |
245 | return; | |
246 | #endif | |
247 | #endif | |
3ae8f4c8 | 248 | #endif |
fd61d399 | 249 | break; |
7dbe63bc | 250 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
91199f4a | 251 | case MMCSD_MODE_EMMCBOOT: |
7dbe63bc TR |
252 | /* |
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 | */ | |
91199f4a | 257 | part = (mmc->part_config >> 3) & PART_ACCESS_MASK; |
7dbe63bc TR |
258 | |
259 | if (part == 7) | |
260 | part = 0; | |
261 | ||
262 | if (mmc_switch_part(0, part)) { | |
263 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT | |
91199f4a | 264 | puts("spl: mmc partition switch failed\n"); |
7dbe63bc TR |
265 | #endif |
266 | hang(); | |
267 | } | |
91199f4a | 268 | |
91199f4a PK |
269 | if (!spl_start_uboot()) { |
270 | err = mmc_load_image_raw_os(mmc); | |
271 | if (!err) | |
272 | return; | |
273 | } | |
ecb30139 PK |
274 | err = mmc_load_image_raw_partition(mmc, |
275 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); | |
3ae8f4c8 PK |
276 | if (!err) |
277 | return; | |
d074ebb9 | 278 | #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) |
b97300b6 | 279 | err = mmc_load_image_raw_sector(mmc, |
7dbe63bc | 280 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
91199f4a PK |
281 | if (!err) |
282 | return; | |
3ae8f4c8 | 283 | #endif |
fd61d399 | 284 | break; |
0da113e9 | 285 | #endif |
91199f4a | 286 | case MMCSD_MODE_UNDEFINED: |
8112f5fa | 287 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
fd61d399 NK |
288 | default: |
289 | puts("spl: mmc: wrong boot mode\n"); | |
8112f5fa | 290 | #endif |
91199f4a | 291 | } |
fd61d399 NK |
292 | |
293 | hang(); | |
9ea5c6ef | 294 | } |