Merge branch '2019-11-04-ti-imports'
[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 <dm.h>
9 #include <malloc.h>
10 #include <spl.h>
11 #include <syscon.h>
12 #include <asm/cpu.h>
13 #include <asm/cpu_common.h>
14 #include <asm/mrccache.h>
15 #include <asm/mtrr.h>
16 #include <asm/pci.h>
17 #include <asm/processor.h>
18 #include <asm/spl.h>
19 #include <asm-generic/sections.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 __weak int arch_cpu_init_dm(void)
24 {
25         return 0;
26 }
27
28 #ifdef CONFIG_TPL
29
30 static int set_max_freq(void)
31 {
32         if (cpu_get_burst_mode_state() == BURST_MODE_UNAVAILABLE) {
33                 /*
34                  * Burst Mode has been factory-configured as disabled and is not
35                  * available in this physical processor package
36                  */
37                 debug("Burst Mode is factory-disabled\n");
38                 return -ENOENT;
39         }
40
41         /* Enable burst mode */
42         cpu_set_burst_mode(true);
43
44         /* Enable speed step */
45         cpu_set_eist(true);
46
47         /* Set P-State ratio */
48         cpu_set_p_state_to_turbo_ratio();
49
50         return 0;
51 }
52 #endif
53
54 static int x86_spl_init(void)
55 {
56 #ifndef CONFIG_TPL
57         /*
58          * TODO(sjg@chromium.org): We use this area of RAM for the stack
59          * and global_data in SPL. Once U-Boot starts up and releocates it
60          * is not needed. We could make this a CONFIG option or perhaps
61          * place it immediately below CONFIG_SYS_TEXT_BASE.
62          */
63         char *ptr = (char *)0x110000;
64 #else
65         struct udevice *punit;
66 #endif
67         int ret;
68
69         debug("%s starting\n", __func__);
70         if (IS_ENABLED(TPL))
71                 ret = x86_cpu_reinit_f();
72         else
73                 ret = x86_cpu_init_f();
74         ret = spl_init();
75         if (ret) {
76                 debug("%s: spl_init() failed\n", __func__);
77                 return ret;
78         }
79         ret = arch_cpu_init();
80         if (ret) {
81                 debug("%s: arch_cpu_init() failed\n", __func__);
82                 return ret;
83         }
84 #ifndef CONFIG_TPL
85         ret = arch_cpu_init_dm();
86         if (ret) {
87                 debug("%s: arch_cpu_init_dm() failed\n", __func__);
88                 return ret;
89         }
90 #endif
91         preloader_console_init();
92 #ifndef CONFIG_TPL
93         ret = print_cpuinfo();
94         if (ret) {
95                 debug("%s: print_cpuinfo() failed\n", __func__);
96                 return ret;
97         }
98 #endif
99         ret = dram_init();
100         if (ret) {
101                 debug("%s: dram_init() failed\n", __func__);
102                 return ret;
103         }
104         if (IS_ENABLED(CONFIG_ENABLE_MRC_CACHE)) {
105                 ret = mrccache_spl_save();
106                 if (ret)
107                         debug("%s: Failed to write to mrccache (err=%d)\n",
108                               __func__, ret);
109         }
110
111 #ifndef CONFIG_TPL
112         memset(&__bss_start, 0, (ulong)&__bss_end - (ulong)&__bss_start);
113
114         /* TODO(sjg@chromium.org): Consider calling cpu_init_r() here */
115         ret = interrupt_init();
116         if (ret) {
117                 debug("%s: interrupt_init() failed\n", __func__);
118                 return ret;
119         }
120
121         /*
122          * The stack grows down from ptr. Put the global data at ptr. This
123          * will only be used for SPL. Once SPL loads U-Boot proper it will
124          * set up its own stack.
125          */
126         gd->new_gd = (struct global_data *)ptr;
127         memcpy(gd->new_gd, gd, sizeof(*gd));
128         arch_setup_gd(gd->new_gd);
129         gd->start_addr_sp = (ulong)ptr;
130
131         /* Cache the SPI flash. Otherwise copying the code to RAM takes ages */
132         ret = mtrr_add_request(MTRR_TYPE_WRBACK,
133                                (1ULL << 32) - CONFIG_XIP_ROM_SIZE,
134                                CONFIG_XIP_ROM_SIZE);
135         if (ret) {
136                 debug("%s: SPI cache setup failed (err=%d)\n", __func__, ret);
137                 return ret;
138         }
139         mtrr_commit(true);
140 #else
141         ret = syscon_get_by_driver_data(X86_SYSCON_PUNIT, &punit);
142         if (ret)
143                 debug("Could not find PUNIT (err=%d)\n", ret);
144
145         ret = set_max_freq();
146         if (ret)
147                 debug("Failed to set CPU frequency (err=%d)\n", ret);
148 #endif
149
150         return 0;
151 }
152
153 void board_init_f(ulong flags)
154 {
155         int ret;
156
157         ret = x86_spl_init();
158         if (ret) {
159                 debug("Error %d\n", ret);
160                 panic("x86_spl_init fail");
161         }
162 #ifdef CONFIG_TPL
163         gd->bd = malloc(sizeof(*gd->bd));
164         if (!gd->bd) {
165                 printf("Out of memory for bd_info size %x\n", sizeof(*gd->bd));
166                 hang();
167         }
168         board_init_r(gd, 0);
169 #else
170         /* Uninit CAR and jump to board_init_f_r() */
171         board_init_f_r_trampoline(gd->start_addr_sp);
172 #endif
173 }
174
175 void board_init_f_r(void)
176 {
177         init_cache_f_r();
178         gd->flags &= ~GD_FLG_SERIAL_READY;
179         debug("cache status %d\n", dcache_status());
180         board_init_r(gd, 0);
181 }
182
183 u32 spl_boot_device(void)
184 {
185         return BOOT_DEVICE_SPI_MMAP;
186 }
187
188 int spl_start_uboot(void)
189 {
190         return 0;
191 }
192
193 void spl_board_announce_boot_device(void)
194 {
195         printf("SPI flash");
196 }
197
198 static int spl_board_load_image(struct spl_image_info *spl_image,
199                                 struct spl_boot_device *bootdev)
200 {
201         spl_image->size = CONFIG_SYS_MONITOR_LEN;
202         spl_image->entry_point = CONFIG_SYS_TEXT_BASE;
203         spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
204         spl_image->os = IH_OS_U_BOOT;
205         spl_image->name = "U-Boot";
206
207         debug("Loading to %lx\n", spl_image->load_addr);
208
209         return 0;
210 }
211 SPL_LOAD_IMAGE_METHOD("SPI", 5, BOOT_DEVICE_SPI_MMAP, spl_board_load_image);
212
213 int spl_spi_load_image(void)
214 {
215         return -EPERM;
216 }
217
218 #ifdef CONFIG_X86_RUN_64BIT
219 void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
220 {
221         int ret;
222
223         printf("Jumping to 64-bit U-Boot: Note many features are missing\n");
224         ret = cpu_jump_to_64bit_uboot(spl_image->entry_point);
225         debug("ret=%d\n", ret);
226         hang();
227 }
228 #endif
229
230 void spl_board_init(void)
231 {
232 #ifndef CONFIG_TPL
233         preloader_console_init();
234 #endif
235 }