blob: b18c1cd6092126eb2f4ee6b61a8a5f4cff72d63d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Simon Glass030777d2017-01-16 07:03:56 -07002/*
3 * Copyright (c) 2016 Google, Inc
Simon Glass030777d2017-01-16 07:03:56 -07004 */
5
6#include <common.h>
Simon Glass1d91ba72019-11-14 12:57:37 -07007#include <cpu_func.h>
Simon Glass030777d2017-01-16 07:03:56 -07008#include <debug_uart.h>
Simon Glass0b3c5762019-10-20 21:37:49 -06009#include <dm.h>
Simon Glassf11478f2019-12-28 10:45:07 -070010#include <hang.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060011#include <image.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Simon Glass9b61c7c2019-11-14 12:57:41 -070013#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass7cf5fe02019-05-02 10:52:12 -060015#include <malloc.h>
Simon Glass030777d2017-01-16 07:03:56 -070016#include <spl.h>
Simon Glass0b3c5762019-10-20 21:37:49 -060017#include <syscon.h>
Simon Glass030777d2017-01-16 07:03:56 -070018#include <asm/cpu.h>
Simon Glass0b3c5762019-10-20 21:37:49 -060019#include <asm/cpu_common.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.h>
Simon Glass7cf5fe02019-05-02 10:52:12 -060021#include <asm/mrccache.h>
Simon Glass030777d2017-01-16 07:03:56 -070022#include <asm/mtrr.h>
Simon Glass0b3c5762019-10-20 21:37:49 -060023#include <asm/pci.h>
Simon Glass030777d2017-01-16 07:03:56 -070024#include <asm/processor.h>
Simon Glass19da9c42019-09-25 08:11:39 -060025#include <asm/spl.h>
Simon Glass030777d2017-01-16 07:03:56 -070026#include <asm-generic/sections.h>
27
28DECLARE_GLOBAL_DATA_PTR;
29
Bin Meng2240fde2017-01-18 03:32:53 -080030__weak int arch_cpu_init_dm(void)
31{
32 return 0;
33}
34
Simon Glass0b3c5762019-10-20 21:37:49 -060035#ifdef CONFIG_TPL
36
37static int set_max_freq(void)
38{
39 if (cpu_get_burst_mode_state() == BURST_MODE_UNAVAILABLE) {
40 /*
41 * Burst Mode has been factory-configured as disabled and is not
42 * available in this physical processor package
43 */
44 debug("Burst Mode is factory-disabled\n");
45 return -ENOENT;
46 }
47
48 /* Enable burst mode */
49 cpu_set_burst_mode(true);
50
51 /* Enable speed step */
52 cpu_set_eist(true);
53
54 /* Set P-State ratio */
55 cpu_set_p_state_to_turbo_ratio();
56
57 return 0;
58}
59#endif
60
Simon Glass030777d2017-01-16 07:03:56 -070061static int x86_spl_init(void)
62{
Simon Glass7cf5fe02019-05-02 10:52:12 -060063#ifndef CONFIG_TPL
Simon Glass030777d2017-01-16 07:03:56 -070064 /*
65 * TODO(sjg@chromium.org): We use this area of RAM for the stack
66 * and global_data in SPL. Once U-Boot starts up and releocates it
67 * is not needed. We could make this a CONFIG option or perhaps
68 * place it immediately below CONFIG_SYS_TEXT_BASE.
69 */
Simon Glassdae11532020-04-30 21:21:42 -060070 __maybe_unused char *ptr = (char *)0x110000;
Simon Glass0b3c5762019-10-20 21:37:49 -060071#else
72 struct udevice *punit;
Simon Glass7cf5fe02019-05-02 10:52:12 -060073#endif
Simon Glass030777d2017-01-16 07:03:56 -070074 int ret;
75
76 debug("%s starting\n", __func__);
Simon Glass81f14622019-10-20 21:37:55 -060077 if (IS_ENABLED(TPL))
78 ret = x86_cpu_reinit_f();
79 else
80 ret = x86_cpu_init_f();
Simon Glass030777d2017-01-16 07:03:56 -070081 ret = spl_init();
82 if (ret) {
83 debug("%s: spl_init() failed\n", __func__);
84 return ret;
85 }
Simon Glass030777d2017-01-16 07:03:56 -070086 ret = arch_cpu_init();
87 if (ret) {
88 debug("%s: arch_cpu_init() failed\n", __func__);
89 return ret;
90 }
Simon Glass7cf5fe02019-05-02 10:52:12 -060091#ifndef CONFIG_TPL
Simon Glass030777d2017-01-16 07:03:56 -070092 ret = arch_cpu_init_dm();
93 if (ret) {
94 debug("%s: arch_cpu_init_dm() failed\n", __func__);
95 return ret;
96 }
Simon Glass7cf5fe02019-05-02 10:52:12 -060097#endif
Simon Glass6b19b4d2017-03-19 12:59:21 -060098 preloader_console_init();
Simon Glass2f002162021-03-15 18:11:18 +130099#if !defined(CONFIG_TPL) && !CONFIG_IS_ENABLED(CPU)
Simon Glass030777d2017-01-16 07:03:56 -0700100 ret = print_cpuinfo();
101 if (ret) {
102 debug("%s: print_cpuinfo() failed\n", __func__);
103 return ret;
104 }
Simon Glass7cf5fe02019-05-02 10:52:12 -0600105#endif
Simon Glass030777d2017-01-16 07:03:56 -0700106 ret = dram_init();
107 if (ret) {
108 debug("%s: dram_init() failed\n", __func__);
109 return ret;
110 }
Simon Glass7cf5fe02019-05-02 10:52:12 -0600111 if (IS_ENABLED(CONFIG_ENABLE_MRC_CACHE)) {
112 ret = mrccache_spl_save();
113 if (ret)
114 debug("%s: Failed to write to mrccache (err=%d)\n",
115 __func__, ret);
116 }
117
Simon Glassdae11532020-04-30 21:21:42 -0600118#ifndef CONFIG_SYS_COREBOOT
Simon Glass030777d2017-01-16 07:03:56 -0700119 memset(&__bss_start, 0, (ulong)&__bss_end - (ulong)&__bss_start);
Simon Glass47717592021-01-24 10:06:10 -0700120# ifndef CONFIG_TPL
Simon Glass030777d2017-01-16 07:03:56 -0700121
122 /* TODO(sjg@chromium.org): Consider calling cpu_init_r() here */
123 ret = interrupt_init();
124 if (ret) {
125 debug("%s: interrupt_init() failed\n", __func__);
126 return ret;
127 }
128
129 /*
130 * The stack grows down from ptr. Put the global data at ptr. This
131 * will only be used for SPL. Once SPL loads U-Boot proper it will
132 * set up its own stack.
133 */
134 gd->new_gd = (struct global_data *)ptr;
135 memcpy(gd->new_gd, gd, sizeof(*gd));
136 arch_setup_gd(gd->new_gd);
137 gd->start_addr_sp = (ulong)ptr;
138
139 /* Cache the SPI flash. Otherwise copying the code to RAM takes ages */
140 ret = mtrr_add_request(MTRR_TYPE_WRBACK,
141 (1ULL << 32) - CONFIG_XIP_ROM_SIZE,
142 CONFIG_XIP_ROM_SIZE);
143 if (ret) {
Simon Glass7cf5fe02019-05-02 10:52:12 -0600144 debug("%s: SPI cache setup failed (err=%d)\n", __func__, ret);
Simon Glass030777d2017-01-16 07:03:56 -0700145 return ret;
146 }
Simon Glass7cf5fe02019-05-02 10:52:12 -0600147 mtrr_commit(true);
Simon Glassdae11532020-04-30 21:21:42 -0600148# else
Simon Glass0b3c5762019-10-20 21:37:49 -0600149 ret = syscon_get_by_driver_data(X86_SYSCON_PUNIT, &punit);
150 if (ret)
151 debug("Could not find PUNIT (err=%d)\n", ret);
152
153 ret = set_max_freq();
154 if (ret)
155 debug("Failed to set CPU frequency (err=%d)\n", ret);
Simon Glassdae11532020-04-30 21:21:42 -0600156# endif
Simon Glass7cf5fe02019-05-02 10:52:12 -0600157#endif
Simon Glass030777d2017-01-16 07:03:56 -0700158
159 return 0;
160}
161
162void board_init_f(ulong flags)
163{
164 int ret;
165
166 ret = x86_spl_init();
167 if (ret) {
Simon Glassa0185fa2020-05-27 06:58:48 -0600168 printf("x86_spl_init: error %d\n", ret);
169 hang();
Simon Glass030777d2017-01-16 07:03:56 -0700170 }
Simon Glassdae11532020-04-30 21:21:42 -0600171#if IS_ENABLED(CONFIG_TPL) || IS_ENABLED(CONFIG_SYS_COREBOOT)
Simon Glass7cf5fe02019-05-02 10:52:12 -0600172 gd->bd = malloc(sizeof(*gd->bd));
173 if (!gd->bd) {
174 printf("Out of memory for bd_info size %x\n", sizeof(*gd->bd));
175 hang();
176 }
177 board_init_r(gd, 0);
178#else
Simon Glass030777d2017-01-16 07:03:56 -0700179 /* Uninit CAR and jump to board_init_f_r() */
180 board_init_f_r_trampoline(gd->start_addr_sp);
Simon Glass7cf5fe02019-05-02 10:52:12 -0600181#endif
Simon Glass030777d2017-01-16 07:03:56 -0700182}
183
184void board_init_f_r(void)
185{
186 init_cache_f_r();
187 gd->flags &= ~GD_FLG_SERIAL_READY;
188 debug("cache status %d\n", dcache_status());
189 board_init_r(gd, 0);
190}
191
192u32 spl_boot_device(void)
193{
Simon Glass19da9c42019-09-25 08:11:39 -0600194 return BOOT_DEVICE_SPI_MMAP;
Simon Glass030777d2017-01-16 07:03:56 -0700195}
196
197int spl_start_uboot(void)
198{
199 return 0;
200}
201
202void spl_board_announce_boot_device(void)
203{
204 printf("SPI flash");
205}
206
207static int spl_board_load_image(struct spl_image_info *spl_image,
208 struct spl_boot_device *bootdev)
209{
210 spl_image->size = CONFIG_SYS_MONITOR_LEN;
211 spl_image->entry_point = CONFIG_SYS_TEXT_BASE;
212 spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
213 spl_image->os = IH_OS_U_BOOT;
214 spl_image->name = "U-Boot";
215
Simon Glass91fcd1d2020-04-30 21:21:41 -0600216 if (!IS_ENABLED(CONFIG_SYS_COREBOOT)) {
217 /*
218 * Copy U-Boot from ROM
219 * TODO(sjg@chromium.org): Figure out a way to get the text base
220 * correctly here, and in the device-tree binman definition.
221 *
222 * Also consider using FIT so we get the correct image length
223 * and parameters.
224 */
225 memcpy((char *)spl_image->load_addr, (char *)0xfff00000,
226 0x100000);
227 }
228
Simon Glass030777d2017-01-16 07:03:56 -0700229 debug("Loading to %lx\n", spl_image->load_addr);
230
231 return 0;
232}
Simon Glass19da9c42019-09-25 08:11:39 -0600233SPL_LOAD_IMAGE_METHOD("SPI", 5, BOOT_DEVICE_SPI_MMAP, spl_board_load_image);
Simon Glass030777d2017-01-16 07:03:56 -0700234
235int spl_spi_load_image(void)
236{
237 return -EPERM;
238}
239
Simon Glass7cf5fe02019-05-02 10:52:12 -0600240#ifdef CONFIG_X86_RUN_64BIT
Simon Glass030777d2017-01-16 07:03:56 -0700241void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
242{
243 int ret;
244
245 printf("Jumping to 64-bit U-Boot: Note many features are missing\n");
246 ret = cpu_jump_to_64bit_uboot(spl_image->entry_point);
247 debug("ret=%d\n", ret);
Simon Glass39c6f9b2019-09-25 08:11:38 -0600248 hang();
Simon Glass030777d2017-01-16 07:03:56 -0700249}
Simon Glass7cf5fe02019-05-02 10:52:12 -0600250#endif
251
252void spl_board_init(void)
253{
254#ifndef CONFIG_TPL
255 preloader_console_init();
256#endif
257}