xref: /OK3568_Linux_fs/u-boot/common/spl/spl_nand.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2011
3  * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 #include <common.h>
8 #include <config.h>
9 #include <spl.h>
10 #include <spl_rkfw.h>
11 #include <asm/io.h>
12 #include <nand.h>
13 #include <linux/libfdt_env.h>
14 #include <fdt.h>
15 
16 #if defined(CONFIG_SPL_NAND_RAW_ONLY)
spl_nand_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)17 int spl_nand_load_image(struct spl_image_info *spl_image,
18 			struct spl_boot_device *bootdev)
19 {
20 	nand_init();
21 
22 	nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
23 			    CONFIG_SYS_NAND_U_BOOT_SIZE,
24 			    (void *)CONFIG_SYS_NAND_U_BOOT_DST);
25 	spl_set_header_raw_uboot(spl_image);
26 	nand_deselect();
27 
28 	return 0;
29 }
30 #else
31 
spl_nand_fit_read(struct spl_load_info * load,ulong offs,ulong size,void * dst)32 static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
33 			       ulong size, void *dst)
34 {
35 	int ret;
36 
37 	ret = nand_spl_load_image(offs, size, dst);
38 	if (!ret)
39 		return size;
40 	else
41 		return 0;
42 }
43 
44 #ifdef CONFIG_SPL_LOAD_RKFW
spl_nand_rkfw_read(struct spl_load_info * load,ulong offs,ulong size,void * dst)45 static ulong spl_nand_rkfw_read(struct spl_load_info *load, ulong offs,
46 				ulong size, void *dst)
47 {
48 	int ret;
49 
50 	ret = nand_spl_load_image(offs * 512, size * 512, dst);
51 	if (!ret)
52 		return size;
53 	else
54 		return 0;
55 }
56 #endif
57 
spl_nand_load_element(struct spl_image_info * spl_image,int offset,struct image_header * header)58 static int spl_nand_load_element(struct spl_image_info *spl_image,
59 				 int offset, struct image_header *header)
60 {
61 	int err;
62 
63 #ifdef CONFIG_SPL_LOAD_RKFW
64 	struct spl_load_info load;
65 	int ret;
66 
67 	load.dev = NULL;
68 	load.priv = NULL;
69 	load.filename = NULL;
70 	load.bl_len = 1;
71 	load.read = spl_nand_rkfw_read;
72 
73 	ret = spl_load_rkfw_image(spl_image, &load);
74 	if (!ret || ret != -EAGAIN)
75 		return ret;
76 #endif
77 	err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
78 	if (err)
79 		return err;
80 
81 #ifdef CONFIG_SPL_FIT_IMAGE_MULTIPLE
82 	if ((IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
83 	     image_get_magic(header) == FDT_MAGIC) ||
84 	     CONFIG_SPL_FIT_IMAGE_MULTIPLE > 1) {
85 #else
86 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
87 	    image_get_magic(header) == FDT_MAGIC) {
88 #endif
89 		struct spl_load_info load;
90 
91 		debug("Found FIT\n");
92 		load.dev = NULL;
93 		load.priv = NULL;
94 		load.filename = NULL;
95 		load.bl_len = 1;
96 		load.read = spl_nand_fit_read;
97 		return spl_load_simple_fit(spl_image, &load, offset, header);
98 	} else {
99 		err = spl_parse_image_header(spl_image, header);
100 		if (err)
101 			return err;
102 		return nand_spl_load_image(offset, spl_image->size,
103 					   (void *)(ulong)spl_image->load_addr);
104 	}
105 }
106 
107 static int spl_nand_load_image(struct spl_image_info *spl_image,
108 			       struct spl_boot_device *bootdev)
109 {
110 	int err;
111 	struct image_header *header;
112 	int *src __attribute__((unused));
113 	int *dst __attribute__((unused));
114 
115 #ifdef CONFIG_SPL_NAND_SOFTECC
116 	debug("spl: nand - using sw ecc\n");
117 #else
118 	debug("spl: nand - using hw ecc\n");
119 #endif
120 	nand_init();
121 
122 	/*use CONFIG_SYS_TEXT_BASE as temporary storage area */
123 	header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
124 #ifdef CONFIG_SPL_OS_BOOT
125 	if (!spl_start_uboot()) {
126 		/*
127 		 * load parameter image
128 		 * load to temp position since nand_spl_load_image reads
129 		 * a whole block which is typically larger than
130 		 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
131 		 * following sections like BSS
132 		 */
133 		nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
134 			CONFIG_CMD_SPL_WRITE_SIZE,
135 			(void *)CONFIG_SYS_TEXT_BASE);
136 		/* copy to destintion */
137 		for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
138 				src = (int *)CONFIG_SYS_TEXT_BASE;
139 				src < (int *)(CONFIG_SYS_TEXT_BASE +
140 				CONFIG_CMD_SPL_WRITE_SIZE);
141 				src++, dst++) {
142 			writel(readl(src), dst);
143 		}
144 
145 		/* load linux */
146 		nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
147 			sizeof(*header), (void *)header);
148 		err = spl_parse_image_header(spl_image, header);
149 		if (err)
150 			return err;
151 		if (header->ih_os == IH_OS_LINUX) {
152 			/* happy - was a linux */
153 			err = nand_spl_load_image(
154 				CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
155 				spl_image->size,
156 				(void *)spl_image->load_addr);
157 			nand_deselect();
158 			return err;
159 		} else {
160 			puts("The Expected Linux image was not "
161 				"found. Please check your NAND "
162 				"configuration.\n");
163 			puts("Trying to start u-boot now...\n");
164 		}
165 	}
166 #endif
167 #ifdef CONFIG_NAND_ENV_DST
168 	spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
169 #ifdef CONFIG_ENV_OFFSET_REDUND
170 	spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
171 #endif
172 #endif
173 	/* Load u-boot */
174 	err = spl_nand_load_element(spl_image, CONFIG_SYS_NAND_U_BOOT_OFFS,
175 				    header);
176 #ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
177 #if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
178 	if (err)
179 		err = spl_nand_load_element(spl_image,
180 					    CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
181 					    header);
182 #endif
183 #endif
184 	nand_deselect();
185 	return err;
186 }
187 #endif
188 /* Use priorty 1 so that Ubi can override this */
189 SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);
190