Merge https://gitlab.denx.de/u-boot/custodians/u-boot-x86
[oweals/u-boot.git] / arch / x86 / lib / spl.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016 Google, Inc
4  */
5
6 #include <common.h>
7 #include <debug_uart.h>
8 #include <malloc.h>
9 #include <spl.h>
10 #include <asm/cpu.h>
11 #include <asm/mrccache.h>
12 #include <asm/mtrr.h>
13 #include <asm/processor.h>
14 #include <asm/spl.h>
15 #include <asm-generic/sections.h>
16
17 DECLARE_GLOBAL_DATA_PTR;
18
19 __weak int arch_cpu_init_dm(void)
20 {
21         return 0;
22 }
23
24 static int x86_spl_init(void)
25 {
26 #ifndef CONFIG_TPL
27         /*
28          * TODO(sjg@chromium.org): We use this area of RAM for the stack
29          * and global_data in SPL. Once U-Boot starts up and releocates it
30          * is not needed. We could make this a CONFIG option or perhaps
31          * place it immediately below CONFIG_SYS_TEXT_BASE.
32          */
33         char *ptr = (char *)0x110000;
34 #endif
35         int ret;
36
37         debug("%s starting\n", __func__);
38         ret = spl_init();
39         if (ret) {
40                 debug("%s: spl_init() failed\n", __func__);
41                 return ret;
42         }
43         ret = arch_cpu_init();
44         if (ret) {
45                 debug("%s: arch_cpu_init() failed\n", __func__);
46                 return ret;
47         }
48 #ifndef CONFIG_TPL
49         ret = arch_cpu_init_dm();
50         if (ret) {
51                 debug("%s: arch_cpu_init_dm() failed\n", __func__);
52                 return ret;
53         }
54 #endif
55         preloader_console_init();
56 #ifndef CONFIG_TPL
57         ret = print_cpuinfo();
58         if (ret) {
59                 debug("%s: print_cpuinfo() failed\n", __func__);
60                 return ret;
61         }
62 #endif
63         ret = dram_init();
64         if (ret) {
65                 debug("%s: dram_init() failed\n", __func__);
66                 return ret;
67         }
68         if (IS_ENABLED(CONFIG_ENABLE_MRC_CACHE)) {
69                 ret = mrccache_spl_save();
70                 if (ret)
71                         debug("%s: Failed to write to mrccache (err=%d)\n",
72                               __func__, ret);
73         }
74
75 #ifndef CONFIG_TPL
76         memset(&__bss_start, 0, (ulong)&__bss_end - (ulong)&__bss_start);
77
78         /* TODO(sjg@chromium.org): Consider calling cpu_init_r() here */
79         ret = interrupt_init();
80         if (ret) {
81                 debug("%s: interrupt_init() failed\n", __func__);
82                 return ret;
83         }
84
85         /*
86          * The stack grows down from ptr. Put the global data at ptr. This
87          * will only be used for SPL. Once SPL loads U-Boot proper it will
88          * set up its own stack.
89          */
90         gd->new_gd = (struct global_data *)ptr;
91         memcpy(gd->new_gd, gd, sizeof(*gd));
92         arch_setup_gd(gd->new_gd);
93         gd->start_addr_sp = (ulong)ptr;
94
95         /* Cache the SPI flash. Otherwise copying the code to RAM takes ages */
96         ret = mtrr_add_request(MTRR_TYPE_WRBACK,
97                                (1ULL << 32) - CONFIG_XIP_ROM_SIZE,
98                                CONFIG_XIP_ROM_SIZE);
99         if (ret) {
100                 debug("%s: SPI cache setup failed (err=%d)\n", __func__, ret);
101                 return ret;
102         }
103         mtrr_commit(true);
104 #endif
105
106         return 0;
107 }
108
109 void board_init_f(ulong flags)
110 {
111         int ret;
112
113         ret = x86_spl_init();
114         if (ret) {
115                 debug("Error %d\n", ret);
116                 panic("x86_spl_init fail");
117         }
118 #ifdef CONFIG_TPL
119         gd->bd = malloc(sizeof(*gd->bd));
120         if (!gd->bd) {
121                 printf("Out of memory for bd_info size %x\n", sizeof(*gd->bd));
122                 hang();
123         }
124         board_init_r(gd, 0);
125 #else
126         /* Uninit CAR and jump to board_init_f_r() */
127         board_init_f_r_trampoline(gd->start_addr_sp);
128 #endif
129 }
130
131 void board_init_f_r(void)
132 {
133         init_cache_f_r();
134         gd->flags &= ~GD_FLG_SERIAL_READY;
135         debug("cache status %d\n", dcache_status());
136         board_init_r(gd, 0);
137 }
138
139 u32 spl_boot_device(void)
140 {
141         return BOOT_DEVICE_SPI_MMAP;
142 }
143
144 int spl_start_uboot(void)
145 {
146         return 0;
147 }
148
149 void spl_board_announce_boot_device(void)
150 {
151         printf("SPI flash");
152 }
153
154 static int spl_board_load_image(struct spl_image_info *spl_image,
155                                 struct spl_boot_device *bootdev)
156 {
157         spl_image->size = CONFIG_SYS_MONITOR_LEN;
158         spl_image->entry_point = CONFIG_SYS_TEXT_BASE;
159         spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
160         spl_image->os = IH_OS_U_BOOT;
161         spl_image->name = "U-Boot";
162
163         debug("Loading to %lx\n", spl_image->load_addr);
164
165         return 0;
166 }
167 SPL_LOAD_IMAGE_METHOD("SPI", 5, BOOT_DEVICE_SPI_MMAP, spl_board_load_image);
168
169 int spl_spi_load_image(void)
170 {
171         return -EPERM;
172 }
173
174 #ifdef CONFIG_X86_RUN_64BIT
175 void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
176 {
177         int ret;
178
179         printf("Jumping to 64-bit U-Boot: Note many features are missing\n");
180         ret = cpu_jump_to_64bit_uboot(spl_image->entry_point);
181         debug("ret=%d\n", ret);
182         hang();
183 }
184 #endif
185
186 void spl_board_init(void)
187 {
188 #ifndef CONFIG_TPL
189         preloader_console_init();
190 #endif
191 }