]>
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> |
9ea5c6ef | 14 | #include <mmc.h> |
e4c444b3 | 15 | #include <image.h> |
9ea5c6ef SS |
16 | |
17 | DECLARE_GLOBAL_DATA_PTR; | |
18 | ||
b97300b6 | 19 | static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector) |
9ea5c6ef | 20 | { |
3bc37b6d | 21 | unsigned long count; |
2e222105 PK |
22 | u32 image_size_sectors; |
23 | struct image_header *header; | |
9ea5c6ef SS |
24 | |
25 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - | |
91199f4a | 26 | sizeof(struct image_header)); |
9ea5c6ef SS |
27 | |
28 | /* read image header to find the image size & load address */ | |
3bc37b6d | 29 | count = mmc->block_dev.block_read(0, sector, 1, header); |
fdfa39d3 | 30 | debug("read sector %lx, count=%lu\n", sector, count); |
3bc37b6d | 31 | if (count == 0) |
9ea5c6ef SS |
32 | goto end; |
33 | ||
fdfa39d3 SG |
34 | if (image_get_magic(header) != IH_MAGIC) { |
35 | puts("bad magic\n"); | |
e4c444b3 | 36 | return -1; |
fdfa39d3 | 37 | } |
e4c444b3 | 38 | |
9ea5c6ef SS |
39 | spl_parse_image_header(header); |
40 | ||
41 | /* convert size to sectors - round up */ | |
f0881250 | 42 | image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) / |
91199f4a | 43 | mmc->read_bl_len; |
9ea5c6ef SS |
44 | |
45 | /* Read the header too to avoid extra memcpy */ | |
3bc37b6d | 46 | count = mmc->block_dev.block_read(0, sector, image_size_sectors, |
7ef4c45c | 47 | (void *)(ulong)spl_image.load_addr); |
fdfa39d3 SG |
48 | debug("read %x sectors to %x\n", image_size_sectors, |
49 | spl_image.load_addr); | |
9ea5c6ef SS |
50 | |
51 | end: | |
1ec26469 | 52 | if (count == 0) { |
8112f5fa | 53 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
1ec26469 | 54 | puts("spl: mmc block read error\n"); |
8112f5fa | 55 | #endif |
3bc37b6d | 56 | return -1; |
1ec26469 | 57 | } |
3bc37b6d PK |
58 | |
59 | return 0; | |
9ea5c6ef SS |
60 | } |
61 | ||
b97300b6 PK |
62 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION |
63 | static int mmc_load_image_raw_partition(struct mmc *mmc, int partition) | |
64 | { | |
65 | disk_partition_t info; | |
3bc37b6d | 66 | int err; |
b97300b6 | 67 | |
3bc37b6d PK |
68 | err = get_partition_info(&mmc->block_dev, partition, &info); |
69 | if (err) { | |
b97300b6 | 70 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
1ec26469 | 71 | puts("spl: partition error\n"); |
b97300b6 PK |
72 | #endif |
73 | return -1; | |
74 | } | |
75 | ||
4bfcc54c SR |
76 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR |
77 | return mmc_load_image_raw_sector(mmc, info.start + | |
78 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); | |
79 | #else | |
b97300b6 | 80 | return mmc_load_image_raw_sector(mmc, info.start); |
4bfcc54c | 81 | #endif |
b97300b6 PK |
82 | } |
83 | #endif | |
84 | ||
2b75b0ad PK |
85 | #ifdef CONFIG_SPL_OS_BOOT |
86 | static int mmc_load_image_raw_os(struct mmc *mmc) | |
87 | { | |
3bc37b6d | 88 | unsigned long count; |
91199f4a | 89 | |
3bc37b6d PK |
90 | count = mmc->block_dev.block_read(0, |
91 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, | |
92 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, | |
93 | (void *) CONFIG_SYS_SPL_ARGS_ADDR); | |
94 | if (count == 0) { | |
8112f5fa | 95 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
1ec26469 | 96 | puts("spl: mmc block read error\n"); |
8112f5fa | 97 | #endif |
2b75b0ad PK |
98 | return -1; |
99 | } | |
100 | ||
b97300b6 | 101 | return mmc_load_image_raw_sector(mmc, |
91199f4a | 102 | CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
2b75b0ad PK |
103 | } |
104 | #endif | |
105 | ||
9ea5c6ef SS |
106 | void spl_mmc_load_image(void) |
107 | { | |
108 | struct mmc *mmc; | |
9ea5c6ef | 109 | u32 boot_mode; |
dc3dedfe | 110 | int err = 0; |
91199f4a | 111 | __maybe_unused int part; |
9ea5c6ef | 112 | |
dc3dedfe SG |
113 | #ifdef CONFIG_DM_MMC |
114 | struct udevice *dev; | |
115 | ||
116 | mmc_initialize(NULL); | |
117 | err = uclass_get_device(UCLASS_MMC, 0, &dev); | |
118 | mmc = NULL; | |
119 | if (!err) | |
120 | mmc = mmc_get_mmc_dev(dev); | |
121 | #else | |
9ea5c6ef | 122 | mmc_initialize(gd->bd); |
91199f4a | 123 | |
9ea5c6ef SS |
124 | /* We register only one device. So, the dev id is always 0 */ |
125 | mmc = find_mmc_device(0); | |
126 | if (!mmc) { | |
8112f5fa | 127 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
91199f4a | 128 | puts("spl: mmc device not found\n"); |
8112f5fa | 129 | #endif |
9ea5c6ef SS |
130 | hang(); |
131 | } | |
dc3dedfe SG |
132 | #endif |
133 | ||
134 | if (!err) | |
135 | err = mmc_init(mmc); | |
9ea5c6ef | 136 | |
9ea5c6ef | 137 | if (err) { |
8112f5fa | 138 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
91199f4a | 139 | printf("spl: mmc init failed with error: %d\n", err); |
8112f5fa | 140 | #endif |
9ea5c6ef SS |
141 | hang(); |
142 | } | |
79adb7a2 | 143 | |
37189a19 | 144 | boot_mode = spl_boot_mode(); |
91199f4a PK |
145 | switch (boot_mode) { |
146 | case MMCSD_MODE_RAW: | |
147 | debug("spl: mmc boot mode: raw\n"); | |
148 | ||
2b75b0ad | 149 | #ifdef CONFIG_SPL_OS_BOOT |
91199f4a PK |
150 | if (!spl_start_uboot()) { |
151 | err = mmc_load_image_raw_os(mmc); | |
152 | if (!err) | |
153 | return; | |
154 | } | |
2b75b0ad | 155 | #endif |
3ae8f4c8 | 156 | #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION) |
b97300b6 PK |
157 | err = mmc_load_image_raw_partition(mmc, |
158 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); | |
3ae8f4c8 PK |
159 | if (!err) |
160 | return; | |
161 | #elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) | |
b97300b6 | 162 | err = mmc_load_image_raw_sector(mmc, |
773b5940 | 163 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
91199f4a PK |
164 | if (!err) |
165 | return; | |
3ae8f4c8 | 166 | #endif |
fd61d399 | 167 | break; |
91199f4a PK |
168 | case MMCSD_MODE_FS: |
169 | debug("spl: mmc boot mode: fs\n"); | |
170 | ||
3ae8f4c8 | 171 | #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION |
592f9222 | 172 | #ifdef CONFIG_SPL_FAT_SUPPORT |
7ad2cc79 | 173 | #ifdef CONFIG_SPL_OS_BOOT |
91199f4a PK |
174 | if (!spl_start_uboot()) { |
175 | err = spl_load_image_fat_os(&mmc->block_dev, | |
176 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); | |
177 | if (!err) | |
178 | return; | |
179 | } | |
7ad2cc79 | 180 | #endif |
3ae8f4c8 | 181 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
773b5940 | 182 | err = spl_load_image_fat(&mmc->block_dev, |
91199f4a PK |
183 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
184 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); | |
185 | if (!err) | |
186 | return; | |
187 | #endif | |
3ae8f4c8 | 188 | #endif |
592f9222 GG |
189 | #ifdef CONFIG_SPL_EXT_SUPPORT |
190 | #ifdef CONFIG_SPL_OS_BOOT | |
91199f4a PK |
191 | if (!spl_start_uboot()) { |
192 | err = spl_load_image_ext_os(&mmc->block_dev, | |
193 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); | |
194 | if (!err) | |
195 | return; | |
196 | } | |
7dbe63bc | 197 | #endif |
3ae8f4c8 | 198 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
592f9222 | 199 | err = spl_load_image_ext(&mmc->block_dev, |
91199f4a PK |
200 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
201 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); | |
202 | if (!err) | |
203 | return; | |
204 | #endif | |
205 | #endif | |
3ae8f4c8 | 206 | #endif |
fd61d399 | 207 | break; |
7dbe63bc | 208 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
91199f4a | 209 | case MMCSD_MODE_EMMCBOOT: |
7dbe63bc TR |
210 | /* |
211 | * We need to check what the partition is configured to. | |
212 | * 1 and 2 match up to boot0 / boot1 and 7 is user data | |
213 | * which is the first physical partition (0). | |
214 | */ | |
91199f4a | 215 | part = (mmc->part_config >> 3) & PART_ACCESS_MASK; |
7dbe63bc TR |
216 | |
217 | if (part == 7) | |
218 | part = 0; | |
219 | ||
220 | if (mmc_switch_part(0, part)) { | |
221 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT | |
91199f4a | 222 | puts("spl: mmc partition switch failed\n"); |
7dbe63bc TR |
223 | #endif |
224 | hang(); | |
225 | } | |
91199f4a | 226 | |
7dbe63bc | 227 | #ifdef CONFIG_SPL_OS_BOOT |
91199f4a PK |
228 | if (!spl_start_uboot()) { |
229 | err = mmc_load_image_raw_os(mmc); | |
230 | if (!err) | |
231 | return; | |
232 | } | |
7dbe63bc | 233 | #endif |
3ae8f4c8 | 234 | #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION) |
ecb30139 PK |
235 | err = mmc_load_image_raw_partition(mmc, |
236 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); | |
3ae8f4c8 PK |
237 | if (!err) |
238 | return; | |
239 | #elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) | |
b97300b6 | 240 | err = mmc_load_image_raw_sector(mmc, |
7dbe63bc | 241 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
91199f4a PK |
242 | if (!err) |
243 | return; | |
3ae8f4c8 | 244 | #endif |
fd61d399 | 245 | break; |
0da113e9 | 246 | #endif |
91199f4a | 247 | case MMCSD_MODE_UNDEFINED: |
8112f5fa | 248 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
fd61d399 NK |
249 | default: |
250 | puts("spl: mmc: wrong boot mode\n"); | |
8112f5fa | 251 | #endif |
91199f4a | 252 | } |
fd61d399 NK |
253 | |
254 | hang(); | |
9ea5c6ef | 255 | } |