xref: /rk3399_rockchip-uboot/common/spl/spl_nand.c (revision e65bf00a8cc4457af9d351e0129b24292fd5b7ff)
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)
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 
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
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 
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 	u32 trust_sectors = CONFIG_RKFW_TRUST_SECTOR;
65 	u32 uboot_sectors = CONFIG_RKFW_U_BOOT_SECTOR;
66 	u32 boot_sectors = CONFIG_RKFW_BOOT_SECTOR;
67 	struct spl_load_info load;
68 	int ret;
69 
70 	load.dev = NULL;
71 	load.priv = NULL;
72 	load.filename = NULL;
73 	load.bl_len = 1;
74 	load.read = spl_nand_rkfw_read;
75 
76 	ret = spl_load_rkfw_image(spl_image, &load,
77 				  trust_sectors,
78 				  uboot_sectors,
79 				  boot_sectors);
80 	if (!ret || ret != -EAGAIN)
81 		return ret;
82 #endif
83 
84 	err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
85 	if (err)
86 		return err;
87 
88 #ifdef CONFIG_SPL_FIT_IMAGE_MULTIPLE
89 	if ((IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
90 	     image_get_magic(header) == FDT_MAGIC) ||
91 	     CONFIG_SPL_FIT_IMAGE_MULTIPLE > 1) {
92 #else
93 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
94 	    image_get_magic(header) == FDT_MAGIC) {
95 #endif
96 		struct spl_load_info load;
97 
98 		debug("Found FIT\n");
99 		load.dev = NULL;
100 		load.priv = NULL;
101 		load.filename = NULL;
102 		load.bl_len = 1;
103 		load.read = spl_nand_fit_read;
104 		return spl_load_simple_fit(spl_image, &load, offset, header);
105 	} else {
106 		err = spl_parse_image_header(spl_image, header);
107 		if (err)
108 			return err;
109 		return nand_spl_load_image(offset, spl_image->size,
110 					   (void *)(ulong)spl_image->load_addr);
111 	}
112 }
113 
114 static int spl_nand_load_image(struct spl_image_info *spl_image,
115 			       struct spl_boot_device *bootdev)
116 {
117 	int err;
118 	struct image_header *header;
119 	int *src __attribute__((unused));
120 	int *dst __attribute__((unused));
121 
122 #ifdef CONFIG_SPL_NAND_SOFTECC
123 	debug("spl: nand - using sw ecc\n");
124 #else
125 	debug("spl: nand - using hw ecc\n");
126 #endif
127 	nand_init();
128 
129 	/*use CONFIG_SYS_TEXT_BASE as temporary storage area */
130 	header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
131 #ifdef CONFIG_SPL_OS_BOOT
132 	if (!spl_start_uboot()) {
133 		/*
134 		 * load parameter image
135 		 * load to temp position since nand_spl_load_image reads
136 		 * a whole block which is typically larger than
137 		 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
138 		 * following sections like BSS
139 		 */
140 		nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
141 			CONFIG_CMD_SPL_WRITE_SIZE,
142 			(void *)CONFIG_SYS_TEXT_BASE);
143 		/* copy to destintion */
144 		for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
145 				src = (int *)CONFIG_SYS_TEXT_BASE;
146 				src < (int *)(CONFIG_SYS_TEXT_BASE +
147 				CONFIG_CMD_SPL_WRITE_SIZE);
148 				src++, dst++) {
149 			writel(readl(src), dst);
150 		}
151 
152 		/* load linux */
153 		nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
154 			sizeof(*header), (void *)header);
155 		err = spl_parse_image_header(spl_image, header);
156 		if (err)
157 			return err;
158 		if (header->ih_os == IH_OS_LINUX) {
159 			/* happy - was a linux */
160 			err = nand_spl_load_image(
161 				CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
162 				spl_image->size,
163 				(void *)spl_image->load_addr);
164 			nand_deselect();
165 			return err;
166 		} else {
167 			puts("The Expected Linux image was not "
168 				"found. Please check your NAND "
169 				"configuration.\n");
170 			puts("Trying to start u-boot now...\n");
171 		}
172 	}
173 #endif
174 #ifdef CONFIG_NAND_ENV_DST
175 	spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
176 #ifdef CONFIG_ENV_OFFSET_REDUND
177 	spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
178 #endif
179 #endif
180 	/* Load u-boot */
181 	err = spl_nand_load_element(spl_image, CONFIG_SYS_NAND_U_BOOT_OFFS,
182 				    header);
183 #ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
184 #if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
185 	if (err)
186 		err = spl_nand_load_element(spl_image,
187 					    CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
188 					    header);
189 #endif
190 #endif
191 	nand_deselect();
192 	return err;
193 }
194 #endif
195 /* Use priorty 1 so that Ubi can override this */
196 SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);
197