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