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