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