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