Merge tag 'efi-2020-07-rc6' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[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 <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
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)
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
42 static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
43                                ulong size, void *dst)
44 {
45         int ret;
46
47         ret = nand_spl_load_image(offs, size, dst);
48         if (!ret)
49                 return size;
50         else
51                 return 0;
52 }
53
54 static int spl_nand_load_element(struct spl_image_info *spl_image,
55                                  int offset, struct image_header *header)
56 {
57         int err;
58
59         err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
60         if (err)
61                 return err;
62
63         if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
64             image_get_magic(header) == FDT_MAGIC) {
65                 struct spl_load_info load;
66
67                 debug("Found FIT\n");
68                 load.dev = NULL;
69                 load.priv = NULL;
70                 load.filename = NULL;
71                 load.bl_len = 1;
72                 load.read = spl_nand_fit_read;
73                 return spl_load_simple_fit(spl_image, &load, offset, header);
74         } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
75                 struct spl_load_info load;
76
77                 load.dev = NULL;
78                 load.priv = NULL;
79                 load.filename = NULL;
80                 load.bl_len = 1;
81                 load.read = spl_nand_fit_read;
82                 return spl_load_imx_container(spl_image, &load, offset);
83         } else {
84                 err = spl_parse_image_header(spl_image, header);
85                 if (err)
86                         return err;
87                 return nand_spl_load_image(offset, spl_image->size,
88                                            (void *)(ulong)spl_image->load_addr);
89         }
90 }
91
92 static int spl_nand_load_image(struct spl_image_info *spl_image,
93                                struct spl_boot_device *bootdev)
94 {
95         int err;
96         struct image_header *header;
97         int *src __attribute__((unused));
98         int *dst __attribute__((unused));
99
100 #ifdef CONFIG_SPL_NAND_SOFTECC
101         debug("spl: nand - using sw ecc\n");
102 #else
103         debug("spl: nand - using hw ecc\n");
104 #endif
105         nand_init();
106
107         header = spl_get_load_buffer(0, sizeof(*header));
108
109 #ifdef CONFIG_SPL_OS_BOOT
110         if (!spl_start_uboot()) {
111                 /*
112                  * load parameter image
113                  * load to temp position since nand_spl_load_image reads
114                  * a whole block which is typically larger than
115                  * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
116                  * following sections like BSS
117                  */
118                 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
119                         CONFIG_CMD_SPL_WRITE_SIZE,
120                         (void *)CONFIG_SYS_TEXT_BASE);
121                 /* copy to destintion */
122                 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
123                                 src = (int *)CONFIG_SYS_TEXT_BASE;
124                                 src < (int *)(CONFIG_SYS_TEXT_BASE +
125                                 CONFIG_CMD_SPL_WRITE_SIZE);
126                                 src++, dst++) {
127                         writel(readl(src), dst);
128                 }
129
130                 /* load linux */
131                 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
132                         sizeof(*header), (void *)header);
133                 err = spl_parse_image_header(spl_image, header);
134                 if (err)
135                         return err;
136                 if (header->ih_os == IH_OS_LINUX) {
137                         /* happy - was a linux */
138                         err = nand_spl_load_image(
139                                 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
140                                 spl_image->size,
141                                 (void *)spl_image->load_addr);
142                         nand_deselect();
143                         return err;
144                 } else {
145                         puts("The Expected Linux image was not "
146                                 "found. Please check your NAND "
147                                 "configuration.\n");
148                         puts("Trying to start u-boot now...\n");
149                 }
150         }
151 #endif
152 #ifdef CONFIG_NAND_ENV_DST
153         spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
154 #ifdef CONFIG_ENV_OFFSET_REDUND
155         spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
156 #endif
157 #endif
158         /* Load u-boot */
159         err = spl_nand_load_element(spl_image, spl_nand_get_uboot_raw_page(),
160                                     header);
161 #ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
162 #if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
163         if (err)
164                 err = spl_nand_load_element(spl_image,
165                                             CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
166                                             header);
167 #endif
168 #endif
169         nand_deselect();
170         return err;
171 }
172 #endif
173 /* Use priorty 1 so that Ubi can override this */
174 SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);