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