xref: /openbmc/u-boot/common/spl/spl_mmc.c (revision fabbeb33)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2010
4  * Texas Instruments, <www.ti.com>
5  *
6  * Aneesh V <aneesh@ti.com>
7  */
8 #include <common.h>
9 #include <dm.h>
10 #include <spl.h>
11 #include <linux/compiler.h>
12 #include <errno.h>
13 #include <asm/u-boot.h>
14 #include <errno.h>
15 #include <mmc.h>
16 #include <image.h>
17 
18 static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc,
19 			   ulong sector, struct image_header *header)
20 {
21 	u32 image_size_sectors;
22 	unsigned long count;
23 	int ret;
24 
25 	ret = spl_parse_image_header(spl_image, header);
26 	if (ret)
27 		return ret;
28 
29 	/* convert size to sectors - round up */
30 	image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
31 			     mmc->read_bl_len;
32 
33 	/* Read the header too to avoid extra memcpy */
34 	count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors,
35 			  (void *)(ulong)spl_image->load_addr);
36 	debug("read %x sectors to %lx\n", image_size_sectors,
37 	      spl_image->load_addr);
38 	if (count != image_size_sectors)
39 		return -EIO;
40 
41 	return 0;
42 }
43 
44 static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
45 			     ulong count, void *buf)
46 {
47 	struct mmc *mmc = load->dev;
48 
49 	return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
50 }
51 
52 static __maybe_unused
53 int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
54 			      struct mmc *mmc, unsigned long sector)
55 {
56 	unsigned long count;
57 	struct image_header *header;
58 	struct blk_desc *bd = mmc_get_blk_desc(mmc);
59 	int ret = 0;
60 
61 	header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
62 
63 	/* read image header to find the image size & load address */
64 	count = blk_dread(bd, sector, 1, header);
65 	debug("hdr read sector %lx, count=%lu\n", sector, count);
66 	if (count == 0) {
67 		ret = -EIO;
68 		goto end;
69 	}
70 
71 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
72 	    image_get_magic(header) == FDT_MAGIC) {
73 		struct spl_load_info load;
74 
75 		debug("Found FIT\n");
76 		load.dev = mmc;
77 		load.priv = NULL;
78 		load.filename = NULL;
79 		load.bl_len = mmc->read_bl_len;
80 		load.read = h_spl_load_read;
81 		ret = spl_load_simple_fit(spl_image, &load, sector, header);
82 	} else {
83 		ret = mmc_load_legacy(spl_image, mmc, sector, header);
84 	}
85 
86 end:
87 	if (ret) {
88 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
89 		puts("mmc_load_image_raw_sector: mmc block read error\n");
90 #endif
91 		return -1;
92 	}
93 
94 	return 0;
95 }
96 
97 static int spl_mmc_get_device_index(u32 boot_device)
98 {
99 	switch (boot_device) {
100 	case BOOT_DEVICE_MMC1:
101 		return 0;
102 	case BOOT_DEVICE_MMC2:
103 	case BOOT_DEVICE_MMC2_2:
104 		return 1;
105 	}
106 
107 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
108 	printf("spl: unsupported mmc boot device.\n");
109 #endif
110 
111 	return -ENODEV;
112 }
113 
114 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
115 {
116 #if CONFIG_IS_ENABLED(DM_MMC)
117 	struct udevice *dev;
118 #endif
119 	int err, mmc_dev;
120 
121 	mmc_dev = spl_mmc_get_device_index(boot_device);
122 	if (mmc_dev < 0)
123 		return mmc_dev;
124 
125 	err = mmc_initialize(NULL);
126 	if (err) {
127 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
128 		printf("spl: could not initialize mmc. error: %d\n", err);
129 #endif
130 		return err;
131 	}
132 
133 #if CONFIG_IS_ENABLED(DM_MMC)
134 	err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
135 	if (!err)
136 		*mmcp = mmc_get_mmc_dev(dev);
137 #else
138 	*mmcp = find_mmc_device(mmc_dev);
139 	err = *mmcp ? 0 : -ENODEV;
140 #endif
141 	if (err) {
142 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
143 		printf("spl: could not find mmc device %d. error: %d\n",
144 		       mmc_dev, err);
145 #endif
146 		return err;
147 	}
148 
149 	return 0;
150 }
151 
152 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
153 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
154 					struct mmc *mmc, int partition,
155 					unsigned long sector)
156 {
157 	disk_partition_t info;
158 	int err;
159 
160 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
161 	int type_part;
162 	/* Only support MBR so DOS_ENTRY_NUMBERS */
163 	for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
164 		err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
165 		if (err)
166 			continue;
167 		if (info.sys_ind ==
168 			CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
169 			partition = type_part;
170 			break;
171 		}
172 	}
173 #endif
174 
175 	err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
176 	if (err) {
177 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
178 		puts("spl: partition error\n");
179 #endif
180 		return -1;
181 	}
182 
183 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
184 	return mmc_load_image_raw_sector(spl_image, mmc, info.start + sector);
185 #else
186 	return mmc_load_image_raw_sector(spl_image, mmc, info.start);
187 #endif
188 }
189 #endif
190 
191 #ifdef CONFIG_SPL_OS_BOOT
192 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
193 				 struct mmc *mmc)
194 {
195 	int ret;
196 
197 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
198 	unsigned long count;
199 
200 	count = blk_dread(mmc_get_blk_desc(mmc),
201 		CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
202 		CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
203 		(void *) CONFIG_SYS_SPL_ARGS_ADDR);
204 	if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
205 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
206 		puts("mmc_load_image_raw_os: mmc block read error\n");
207 #endif
208 		return -1;
209 	}
210 #endif	/* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
211 
212 	ret = mmc_load_image_raw_sector(spl_image, mmc,
213 		CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
214 	if (ret)
215 		return ret;
216 
217 	if (spl_image->os != IH_OS_LINUX) {
218 		puts("Expected Linux image is not found. Trying to start U-boot\n");
219 		return -ENOENT;
220 	}
221 
222 	return 0;
223 }
224 #else
225 int spl_start_uboot(void)
226 {
227 	return 1;
228 }
229 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
230 				 struct mmc *mmc)
231 {
232 	return -ENOSYS;
233 }
234 #endif
235 
236 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
237 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
238 			      const char *filename)
239 {
240 	int err = -ENOSYS;
241 
242 #ifdef CONFIG_SPL_FS_FAT
243 	if (!spl_start_uboot()) {
244 		err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
245 			CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
246 		if (!err)
247 			return err;
248 	}
249 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
250 	err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
251 				 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
252 				 filename);
253 	if (!err)
254 		return err;
255 #endif
256 #endif
257 #ifdef CONFIG_SPL_FS_EXT4
258 	if (!spl_start_uboot()) {
259 		err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
260 			CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
261 		if (!err)
262 			return err;
263 	}
264 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
265 	err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
266 				 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
267 				 filename);
268 	if (!err)
269 		return err;
270 #endif
271 #endif
272 
273 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
274 	err = -ENOENT;
275 #endif
276 
277 	return err;
278 }
279 #else
280 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
281 			      const char *filename)
282 {
283 	return -ENOSYS;
284 }
285 #endif
286 
287 u32 __weak spl_boot_mode(const u32 boot_device)
288 {
289 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
290 	return MMCSD_MODE_FS;
291 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
292 	return MMCSD_MODE_EMMCBOOT;
293 #else
294 	return MMCSD_MODE_RAW;
295 #endif
296 }
297 
298 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
299 __weak
300 int spl_boot_partition(const u32 boot_device)
301 {
302 	return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
303 }
304 #endif
305 
306 int spl_mmc_load(struct spl_image_info *spl_image,
307 		 struct spl_boot_device *bootdev,
308 		 const char *filename,
309 		 int raw_part,
310 		 unsigned long raw_sect)
311 {
312 	static struct mmc *mmc;
313 	u32 boot_mode;
314 	int err = 0;
315 	__maybe_unused int part;
316 
317 	/* Perform peripheral init only once */
318 	if (!mmc) {
319 		err = spl_mmc_find_device(&mmc, bootdev->boot_device);
320 		if (err)
321 			return err;
322 
323 		err = mmc_init(mmc);
324 		if (err) {
325 			mmc = NULL;
326 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
327 			printf("spl: mmc init failed with error: %d\n", err);
328 #endif
329 			return err;
330 		}
331 	}
332 
333 	boot_mode = spl_boot_mode(bootdev->boot_device);
334 	err = -EINVAL;
335 	switch (boot_mode) {
336 	case MMCSD_MODE_EMMCBOOT:
337 			/*
338 			 * We need to check what the partition is configured to.
339 			 * 1 and 2 match up to boot0 / boot1 and 7 is user data
340 			 * which is the first physical partition (0).
341 			 */
342 			part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
343 
344 			if (part == 7)
345 				part = 0;
346 
347 			if (CONFIG_IS_ENABLED(MMC_TINY))
348 				err = mmc_switch_part(mmc, part);
349 			else
350 				err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
351 
352 			if (err) {
353 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
354 				puts("spl: mmc partition switch failed\n");
355 #endif
356 				return err;
357 			}
358 			/* Fall through */
359 	case MMCSD_MODE_RAW:
360 		debug("spl: mmc boot mode: raw\n");
361 
362 		if (!spl_start_uboot()) {
363 			err = mmc_load_image_raw_os(spl_image, mmc);
364 			if (!err)
365 				return err;
366 		}
367 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
368 		err = mmc_load_image_raw_partition(spl_image, mmc, raw_part,
369 						   raw_sect);
370 		if (!err)
371 			return err;
372 #endif
373 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
374 		err = mmc_load_image_raw_sector(spl_image, mmc, raw_sect);
375 		if (!err)
376 			return err;
377 #endif
378 		/* If RAW mode fails, try FS mode. */
379 	case MMCSD_MODE_FS:
380 		debug("spl: mmc boot mode: fs\n");
381 
382 		err = spl_mmc_do_fs_boot(spl_image, mmc, filename);
383 		if (!err)
384 			return err;
385 
386 		break;
387 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
388 	default:
389 		puts("spl: mmc: wrong boot mode\n");
390 #endif
391 	}
392 
393 	return err;
394 }
395 
396 int spl_mmc_load_image(struct spl_image_info *spl_image,
397 		       struct spl_boot_device *bootdev)
398 {
399 	return spl_mmc_load(spl_image, bootdev,
400 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
401 			    CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
402 #else
403 			    NULL,
404 #endif
405 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
406 			    spl_boot_partition(bootdev->boot_device),
407 #else
408 			    0,
409 #endif
410 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
411 			    CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
412 #else
413 			    0);
414 #endif
415 }
416 
417 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
418 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
419 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);
420