]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/spl/spl_nand.c
karo: merge with Ka-Ro specific tree for secure boot support
[karo-tx-uboot.git] / common / spl / spl_nand.c
1 /*
2  * Copyright (C) 2011
3  * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7 #include <common.h>
8 #include <config.h>
9 #include <spl.h>
10 #include <asm/io.h>
11 #include <nand.h>
12
13 #if defined(CONFIG_SPL_NAND_RAW_ONLY)
14 int spl_nand_load_image(void)
15 {
16         int ret;
17
18         nand_init();
19
20         ret = nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
21                             CONFIG_SYS_NAND_U_BOOT_SIZE,
22                             (void *)CONFIG_SYS_NAND_U_BOOT_DST);
23         nand_deselect();
24         if (ret)
25                 return ret;
26
27         spl_set_header_raw_uboot();
28         return 0;
29 }
30 #else
31 int spl_nand_load_image(void)
32 {
33         int ret;
34         struct image_header *header;
35         int *src __attribute__((unused));
36         int *dst __attribute__((unused));
37
38         debug("spl: nand - using hw ecc\n");
39         nand_init();
40
41         /*use CONFIG_SYS_TEXT_BASE as temporary storage area */
42         header = (struct image_header *)CONFIG_SYS_TEXT_BASE;
43 #ifdef CONFIG_SPL_OS_BOOT
44         if (!spl_start_uboot()) {
45                 /*
46                  * load parameter image
47                  * load to temp position since nand_spl_load_image reads
48                  * a whole block which is typically larger than
49                  * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
50                  * following sections like BSS
51                  */
52                 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
53                         CONFIG_CMD_SPL_WRITE_SIZE,
54                         (void *)CONFIG_SYS_TEXT_BASE);
55                 /* copy to destintion */
56                 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
57                                 src = (int *)CONFIG_SYS_TEXT_BASE;
58                                 src < (int *)(CONFIG_SYS_TEXT_BASE +
59                                 CONFIG_CMD_SPL_WRITE_SIZE);
60                                 src++, dst++) {
61                         writel(readl(src), dst);
62                 }
63
64                 /* load linux */
65                 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
66                         sizeof(*header), (void *)header);
67                 spl_parse_image_header(header);
68                 if (header->ih_os == IH_OS_LINUX) {
69                         /* happy - was a linux */
70                         nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
71                                 spl_image.size, (void *)spl_image.load_addr);
72                         nand_deselect();
73                         return 0;
74                 } else {
75                         printf("The Expected Linux image was not"
76                                 "found. Please check your NAND"
77                                 "configuration.\n");
78                         printf("Trying to start u-boot now...\n");
79                 }
80         }
81 #endif
82 #ifdef CONFIG_NAND_ENV_DST
83         nand_spl_load_image(CONFIG_ENV_OFFSET,
84                 sizeof(*header), (void *)header);
85         spl_parse_image_header(header);
86         nand_spl_load_image(CONFIG_ENV_OFFSET, spl_image.size,
87                 (void *)spl_image.load_addr);
88 #ifdef CONFIG_ENV_OFFSET_REDUND
89         nand_spl_load_image(CONFIG_ENV_OFFSET_REDUND,
90                 sizeof(*header), (void *)header);
91         spl_parse_image_header(header);
92         nand_spl_load_image(CONFIG_ENV_OFFSET_REDUND, spl_image.size,
93                 (void *)spl_image.load_addr);
94 #endif
95 #endif
96         /* Load u-boot */
97         ret = nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
98                                 CONFIG_SYS_NAND_PAGE_SIZE, (void *)header);
99         if (ret == 0) {
100                 spl_parse_image_header(header);
101                 ret = nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
102                                         spl_image.size, (void *)spl_image.load_addr);
103         }
104         nand_deselect();
105         return ret;
106 }
107 #endif