rockchip: rk3399: Add Nanopi M4 2GB board support
[platform/kernel/u-boot.git] / common / spl / spl_nor.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2012 Stefan Roese <sr@denx.de>
4  */
5
6 #include <common.h>
7 #include <spl.h>
8
9 static ulong spl_nor_load_read(struct spl_load_info *load, ulong sector,
10                                ulong count, void *buf)
11 {
12         debug("%s: sector %lx, count %lx, buf %p\n",
13               __func__, sector, count, buf);
14         memcpy(buf, (void *)sector, count);
15
16         return count;
17 }
18
19 unsigned long __weak spl_nor_get_uboot_base(void)
20 {
21         return CONFIG_SYS_UBOOT_BASE;
22 }
23
24 static int spl_nor_load_image(struct spl_image_info *spl_image,
25                               struct spl_boot_device *bootdev)
26 {
27         int ret;
28         __maybe_unused const struct image_header *header;
29         __maybe_unused struct spl_load_info load;
30
31         /*
32          * Loading of the payload to SDRAM is done with skipping of
33          * the mkimage header in this SPL NOR driver
34          */
35         spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
36
37 #ifdef CONFIG_SPL_OS_BOOT
38         if (!spl_start_uboot()) {
39                 /*
40                  * Load Linux from its location in NOR flash to its defined
41                  * location in SDRAM
42                  */
43                 header = (const struct image_header *)CONFIG_SYS_OS_BASE;
44 #ifdef CONFIG_SPL_LOAD_FIT
45                 if (image_get_magic(header) == FDT_MAGIC) {
46                         debug("Found FIT\n");
47                         load.bl_len = 1;
48                         load.read = spl_nor_load_read;
49
50                         ret = spl_load_simple_fit(spl_image, &load,
51                                                   CONFIG_SYS_OS_BASE,
52                                                   (void *)header);
53
54 #if defined CONFIG_SYS_SPL_ARGS_ADDR && defined CONFIG_CMD_SPL_NOR_OFS
55                         memcpy((void *)CONFIG_SYS_SPL_ARGS_ADDR,
56                                (void *)CONFIG_CMD_SPL_NOR_OFS,
57                                CONFIG_CMD_SPL_WRITE_SIZE);
58 #endif
59                         return ret;
60                 }
61 #endif
62                 if (image_get_os(header) == IH_OS_LINUX) {
63                         /* happy - was a Linux */
64
65                         ret = spl_parse_image_header(spl_image, header);
66                         if (ret)
67                                 return ret;
68
69                         memcpy((void *)spl_image->load_addr,
70                                (void *)(CONFIG_SYS_OS_BASE +
71                                         sizeof(struct image_header)),
72                                spl_image->size);
73 #ifdef CONFIG_SYS_FDT_BASE
74                         spl_image->arg = (void *)CONFIG_SYS_FDT_BASE;
75 #endif
76
77                         return 0;
78                 } else {
79                         puts("The Expected Linux image was not found.\n"
80                              "Please check your NOR configuration.\n"
81                              "Trying to start u-boot now...\n");
82                 }
83         }
84 #endif
85
86         /*
87          * Load real U-Boot from its location in NOR flash to its
88          * defined location in SDRAM
89          */
90 #ifdef CONFIG_SPL_LOAD_FIT
91         header = (const struct image_header *)spl_nor_get_uboot_base();
92         if (image_get_magic(header) == FDT_MAGIC) {
93                 debug("Found FIT format U-Boot\n");
94                 load.bl_len = 1;
95                 load.read = spl_nor_load_read;
96                 ret = spl_load_simple_fit(spl_image, &load,
97                                           spl_nor_get_uboot_base(),
98                                           (void *)header);
99
100                 return ret;
101         }
102 #endif
103         if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
104                 load.bl_len = 1;
105                 load.read = spl_nor_load_read;
106                 return spl_load_imx_container(spl_image, &load,
107                                               spl_nor_get_uboot_base());
108         }
109
110         ret = spl_parse_image_header(spl_image,
111                         (const struct image_header *)spl_nor_get_uboot_base());
112         if (ret)
113                 return ret;
114
115         memcpy((void *)(unsigned long)spl_image->load_addr,
116                (void *)(spl_nor_get_uboot_base() + sizeof(struct image_header)),
117                spl_image->size);
118
119         return 0;
120 }
121 SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image);