xref: /OK3568_Linux_fs/u-boot/common/spl/spl_ubi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2016
3*4882a593Smuzhiyun  * Ladislav Michl <ladis@linux-mips.org>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier: GPL 2.0+ BSD-3-Clause
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <common.h>
9*4882a593Smuzhiyun #include <config.h>
10*4882a593Smuzhiyun #include <nand.h>
11*4882a593Smuzhiyun #include <onenand_uboot.h>
12*4882a593Smuzhiyun #include <ubispl.h>
13*4882a593Smuzhiyun #include <spl.h>
14*4882a593Smuzhiyun 
spl_ubi_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)15*4882a593Smuzhiyun int spl_ubi_load_image(struct spl_image_info *spl_image,
16*4882a593Smuzhiyun 		       struct spl_boot_device *bootdev)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun 	struct image_header *header;
19*4882a593Smuzhiyun 	struct ubispl_info info;
20*4882a593Smuzhiyun 	struct ubispl_load volumes[2];
21*4882a593Smuzhiyun 	int ret = 1;
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun 	switch (bootdev->boot_device) {
24*4882a593Smuzhiyun #ifdef CONFIG_SPL_NAND_SUPPORT
25*4882a593Smuzhiyun 	case BOOT_DEVICE_NAND:
26*4882a593Smuzhiyun 		nand_init();
27*4882a593Smuzhiyun 		info.read = nand_spl_read_block;
28*4882a593Smuzhiyun 		info.peb_size = CONFIG_SYS_NAND_BLOCK_SIZE;
29*4882a593Smuzhiyun 		break;
30*4882a593Smuzhiyun #endif
31*4882a593Smuzhiyun #ifdef CONFIG_SPL_ONENAND_SUPPORT
32*4882a593Smuzhiyun 	case BOOT_DEVICE_ONENAND:
33*4882a593Smuzhiyun 		info.read = onenand_spl_read_block;
34*4882a593Smuzhiyun 		info.peb_size = CONFIG_SYS_ONENAND_BLOCK_SIZE;
35*4882a593Smuzhiyun 		break;
36*4882a593Smuzhiyun #endif
37*4882a593Smuzhiyun 	default:
38*4882a593Smuzhiyun 		goto out;
39*4882a593Smuzhiyun 	}
40*4882a593Smuzhiyun 	info.ubi = (struct ubi_scan_info *)CONFIG_SPL_UBI_INFO_ADDR;
41*4882a593Smuzhiyun 	info.fastmap = IS_ENABLED(CONFIG_MTD_UBI_FASTMAP);
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	info.peb_offset = CONFIG_SPL_UBI_PEB_OFFSET;
44*4882a593Smuzhiyun 	info.vid_offset = CONFIG_SPL_UBI_VID_OFFSET;
45*4882a593Smuzhiyun 	info.leb_start = CONFIG_SPL_UBI_LEB_START;
46*4882a593Smuzhiyun 	info.peb_count = CONFIG_SPL_UBI_MAX_PEBS - info.peb_offset;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #ifdef CONFIG_SPL_OS_BOOT
49*4882a593Smuzhiyun 	if (!spl_start_uboot()) {
50*4882a593Smuzhiyun 		volumes[0].vol_id = CONFIG_SPL_UBI_LOAD_KERNEL_ID;
51*4882a593Smuzhiyun 		volumes[0].load_addr = (void *)CONFIG_SYS_LOAD_ADDR;
52*4882a593Smuzhiyun 		volumes[1].vol_id = CONFIG_SPL_UBI_LOAD_ARGS_ID;
53*4882a593Smuzhiyun 		volumes[1].load_addr = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 		ret = ubispl_load_volumes(&info, volumes, 2);
56*4882a593Smuzhiyun 		if (!ret) {
57*4882a593Smuzhiyun 			header = (struct image_header *)volumes[0].load_addr;
58*4882a593Smuzhiyun 			spl_parse_image_header(spl_image, header);
59*4882a593Smuzhiyun 			puts("Linux loaded.\n");
60*4882a593Smuzhiyun 			goto out;
61*4882a593Smuzhiyun 		}
62*4882a593Smuzhiyun 		puts("Loading Linux failed, falling back to U-Boot.\n");
63*4882a593Smuzhiyun 	}
64*4882a593Smuzhiyun #endif
65*4882a593Smuzhiyun 	header = (struct image_header *)
66*4882a593Smuzhiyun 		(CONFIG_SYS_TEXT_BASE - sizeof(struct image_header));
67*4882a593Smuzhiyun 	volumes[0].vol_id = CONFIG_SPL_UBI_LOAD_MONITOR_ID;
68*4882a593Smuzhiyun 	volumes[0].load_addr = (void *)header;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	ret = ubispl_load_volumes(&info, volumes, 1);
71*4882a593Smuzhiyun 	if (!ret)
72*4882a593Smuzhiyun 		spl_parse_image_header(spl_image, header);
73*4882a593Smuzhiyun out:
74*4882a593Smuzhiyun #ifdef CONFIG_SPL_NAND_SUPPORT
75*4882a593Smuzhiyun 	if (bootdev->boot_device == BOOT_DEVICE_NAND)
76*4882a593Smuzhiyun 		nand_deselect();
77*4882a593Smuzhiyun #endif
78*4882a593Smuzhiyun 	return ret;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun /* Use priorty 0 so that Ubi will override NAND and ONENAND methods */
81*4882a593Smuzhiyun SPL_LOAD_IMAGE_METHOD("NAND", 0, BOOT_DEVICE_NAND, spl_ubi_load_image);
82*4882a593Smuzhiyun SPL_LOAD_IMAGE_METHOD("OneNAND", 0, BOOT_DEVICE_ONENAND, spl_ubi_load_image);
83