1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2011
4 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
5 */
6 #include <common.h>
7 #include <config.h>
8 #include <fdt_support.h>
9 #include <image.h>
10 #include <log.h>
11 #include <spl.h>
12 #include <asm/io.h>
13 #include <nand.h>
14 #include <linux/libfdt_env.h>
15 #include <fdt.h>
16
spl_nand_get_uboot_raw_page(void)17 uint32_t __weak spl_nand_get_uboot_raw_page(void)
18 {
19 return CONFIG_SYS_NAND_U_BOOT_OFFS;
20 }
21
22 #if defined(CONFIG_SPL_NAND_RAW_ONLY)
spl_nand_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)23 static int spl_nand_load_image(struct spl_image_info *spl_image,
24 struct spl_boot_device *bootdev)
25 {
26 nand_init();
27
28 printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n",
29 CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
30 CONFIG_SYS_NAND_U_BOOT_DST);
31
32 nand_spl_load_image(spl_nand_get_uboot_raw_page(),
33 CONFIG_SYS_NAND_U_BOOT_SIZE,
34 (void *)CONFIG_SYS_NAND_U_BOOT_DST);
35 spl_set_header_raw_uboot(spl_image);
36 nand_deselect();
37
38 return 0;
39 }
40 #else
41
spl_nand_fit_read(struct spl_load_info * load,ulong offs,ulong size,void * dst)42 static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
43 ulong size, void *dst)
44 {
45 int err;
46 #ifdef CONFIG_SYS_NAND_BLOCK_SIZE
47 ulong sector;
48
49 sector = *(int *)load->priv;
50 offs = sector + nand_spl_adjust_offset(sector, offs - sector);
51 #else
52 offs *= load->bl_len;
53 size *= load->bl_len;
54 #endif
55 err = nand_spl_load_image(offs, size, dst);
56 if (err)
57 return 0;
58
59 return size / load->bl_len;
60 }
61
nand_get_mtd(void)62 struct mtd_info * __weak nand_get_mtd(void)
63 {
64 return NULL;
65 }
66
spl_nand_load_element(struct spl_image_info * spl_image,int offset,struct image_header * header)67 static int spl_nand_load_element(struct spl_image_info *spl_image,
68 int offset, struct image_header *header)
69 {
70 struct mtd_info *mtd = nand_get_mtd();
71 int bl_len = mtd ? mtd->writesize : 1;
72 int err;
73
74 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
75 if (err)
76 return err;
77
78 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
79 image_get_magic(header) == FDT_MAGIC) {
80 struct spl_load_info load;
81
82 debug("Found FIT\n");
83 load.dev = NULL;
84 load.priv = &offset;
85 load.filename = NULL;
86 load.bl_len = bl_len;
87 load.read = spl_nand_fit_read;
88 return spl_load_simple_fit(spl_image, &load, offset / bl_len, header);
89 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
90 struct spl_load_info load;
91
92 load.dev = NULL;
93 load.priv = NULL;
94 load.filename = NULL;
95 load.bl_len = bl_len;
96 load.read = spl_nand_fit_read;
97 return spl_load_imx_container(spl_image, &load, offset / bl_len);
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
spl_nand_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)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 header = spl_get_load_buffer(0, sizeof(*header));
123
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, spl_nand_get_uboot_raw_page(),
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