1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2011 The Chromium OS Authors.
3*4882a593Smuzhiyun * (C) Copyright 2002-2006
4*4882a593Smuzhiyun * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * (C) Copyright 2002
7*4882a593Smuzhiyun * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8*4882a593Smuzhiyun * Marius Groeger <mgroeger@sysgo.de>
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <common.h>
14*4882a593Smuzhiyun #include <console.h>
15*4882a593Smuzhiyun #include <environment.h>
16*4882a593Smuzhiyun #include <dm.h>
17*4882a593Smuzhiyun #include <fdtdec.h>
18*4882a593Smuzhiyun #include <fs.h>
19*4882a593Smuzhiyun #include <i2c.h>
20*4882a593Smuzhiyun #include <initcall.h>
21*4882a593Smuzhiyun #include <init_helpers.h>
22*4882a593Smuzhiyun #include <malloc.h>
23*4882a593Smuzhiyun #include <mapmem.h>
24*4882a593Smuzhiyun #include <mp_boot.h>
25*4882a593Smuzhiyun #include <os.h>
26*4882a593Smuzhiyun #include <post.h>
27*4882a593Smuzhiyun #include <relocate.h>
28*4882a593Smuzhiyun #include <spi.h>
29*4882a593Smuzhiyun #include <status_led.h>
30*4882a593Smuzhiyun #include <timer.h>
31*4882a593Smuzhiyun #include <trace.h>
32*4882a593Smuzhiyun #include <video.h>
33*4882a593Smuzhiyun #include <watchdog.h>
34*4882a593Smuzhiyun #ifdef CONFIG_MACH_TYPE
35*4882a593Smuzhiyun #include <asm/mach-types.h>
36*4882a593Smuzhiyun #endif
37*4882a593Smuzhiyun #if defined(CONFIG_MP) && defined(CONFIG_PPC)
38*4882a593Smuzhiyun #include <asm/mp.h>
39*4882a593Smuzhiyun #endif
40*4882a593Smuzhiyun #include <asm/io.h>
41*4882a593Smuzhiyun #include <asm/sections.h>
42*4882a593Smuzhiyun #include <dm/root.h>
43*4882a593Smuzhiyun #include <linux/errno.h>
44*4882a593Smuzhiyun #include <bidram.h>
45*4882a593Smuzhiyun #include <sysmem.h>
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun * Pointer to initial global data area
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun * Here we initialize it if needed.
51*4882a593Smuzhiyun */
52*4882a593Smuzhiyun #ifdef XTRN_DECLARE_GLOBAL_DATA_PTR
53*4882a593Smuzhiyun #undef XTRN_DECLARE_GLOBAL_DATA_PTR
54*4882a593Smuzhiyun #define XTRN_DECLARE_GLOBAL_DATA_PTR /* empty = allocate here */
55*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
56*4882a593Smuzhiyun #else
57*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
58*4882a593Smuzhiyun #endif
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /*
61*4882a593Smuzhiyun * TODO(sjg@chromium.org): IMO this code should be
62*4882a593Smuzhiyun * refactored to a single function, something like:
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * void led_set_state(enum led_colour_t colour, int on);
65*4882a593Smuzhiyun */
66*4882a593Smuzhiyun /************************************************************************
67*4882a593Smuzhiyun * Coloured LED functionality
68*4882a593Smuzhiyun ************************************************************************
69*4882a593Smuzhiyun * May be supplied by boards if desired
70*4882a593Smuzhiyun */
coloured_LED_init(void)71*4882a593Smuzhiyun __weak void coloured_LED_init(void) {}
red_led_on(void)72*4882a593Smuzhiyun __weak void red_led_on(void) {}
red_led_off(void)73*4882a593Smuzhiyun __weak void red_led_off(void) {}
green_led_on(void)74*4882a593Smuzhiyun __weak void green_led_on(void) {}
green_led_off(void)75*4882a593Smuzhiyun __weak void green_led_off(void) {}
yellow_led_on(void)76*4882a593Smuzhiyun __weak void yellow_led_on(void) {}
yellow_led_off(void)77*4882a593Smuzhiyun __weak void yellow_led_off(void) {}
blue_led_on(void)78*4882a593Smuzhiyun __weak void blue_led_on(void) {}
blue_led_off(void)79*4882a593Smuzhiyun __weak void blue_led_off(void) {}
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun * Why is gd allocated a register? Prior to reloc it might be better to
83*4882a593Smuzhiyun * just pass it around to each function in this file?
84*4882a593Smuzhiyun *
85*4882a593Smuzhiyun * After reloc one could argue that it is hardly used and doesn't need
86*4882a593Smuzhiyun * to be in a register. Or if it is it should perhaps hold pointers to all
87*4882a593Smuzhiyun * global data for all modules, so that post-reloc we can avoid the massive
88*4882a593Smuzhiyun * literal pool we get on ARM. Or perhaps just encourage each module to use
89*4882a593Smuzhiyun * a structure...
90*4882a593Smuzhiyun */
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun #if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
init_func_watchdog_init(void)93*4882a593Smuzhiyun static int init_func_watchdog_init(void)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun # if defined(CONFIG_HW_WATCHDOG) && \
96*4882a593Smuzhiyun (defined(CONFIG_M68K) || defined(CONFIG_MICROBLAZE) || \
97*4882a593Smuzhiyun defined(CONFIG_SH) || defined(CONFIG_AT91SAM9_WATCHDOG) || \
98*4882a593Smuzhiyun defined(CONFIG_DESIGNWARE_WATCHDOG) || \
99*4882a593Smuzhiyun defined(CONFIG_IMX_WATCHDOG))
100*4882a593Smuzhiyun hw_watchdog_init();
101*4882a593Smuzhiyun puts(" Watchdog enabled\n");
102*4882a593Smuzhiyun # endif
103*4882a593Smuzhiyun WATCHDOG_RESET();
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun return 0;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
init_func_watchdog_reset(void)108*4882a593Smuzhiyun int init_func_watchdog_reset(void)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun WATCHDOG_RESET();
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun #endif /* CONFIG_WATCHDOG */
115*4882a593Smuzhiyun
board_add_ram_info(int use_default)116*4882a593Smuzhiyun __weak void board_add_ram_info(int use_default)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun /* please define platform specific board_add_ram_info() */
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
init_baud_rate(void)121*4882a593Smuzhiyun static int init_baud_rate(void)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun if (gd && gd->serial.baudrate)
124*4882a593Smuzhiyun gd->baudrate = gd->serial.baudrate;
125*4882a593Smuzhiyun else
126*4882a593Smuzhiyun gd->baudrate = env_get_ulong("baudrate", 10, CONFIG_BAUDRATE);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun return 0;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
display_text_info(void)131*4882a593Smuzhiyun static int display_text_info(void)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun #if !defined(CONFIG_SANDBOX) && !defined(CONFIG_EFI_APP)
134*4882a593Smuzhiyun ulong bss_start, bss_end, text_base;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun bss_start = (ulong)&__bss_start;
137*4882a593Smuzhiyun bss_end = (ulong)&__bss_end;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun #ifdef CONFIG_SYS_TEXT_BASE
140*4882a593Smuzhiyun text_base = CONFIG_SYS_TEXT_BASE;
141*4882a593Smuzhiyun #else
142*4882a593Smuzhiyun text_base = CONFIG_SYS_MONITOR_BASE;
143*4882a593Smuzhiyun #endif
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun debug("U-Boot code: %08lX -> %08lX BSS: -> %08lX\n",
146*4882a593Smuzhiyun text_base, bss_start, bss_end);
147*4882a593Smuzhiyun #endif
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun return 0;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
announce_serial(void)152*4882a593Smuzhiyun static int announce_serial(void)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun if (gd && gd->serial.using_pre_serial)
155*4882a593Smuzhiyun printf("PreSerial: %d, ", gd->serial.id);
156*4882a593Smuzhiyun else
157*4882a593Smuzhiyun printf("Serial: ");
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_UART_ALWAYS
160*4882a593Smuzhiyun printf("raw");
161*4882a593Smuzhiyun #else
162*4882a593Smuzhiyun printf("console");
163*4882a593Smuzhiyun #endif
164*4882a593Smuzhiyun printf(", 0x%lx\n", gd->serial.addr);
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun return 0;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
announce_dram_init(void)169*4882a593Smuzhiyun static int announce_dram_init(void)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun #ifndef CONFIG_SUPPORT_USBPLUG
172*4882a593Smuzhiyun puts("DRAM: ");
173*4882a593Smuzhiyun #endif
174*4882a593Smuzhiyun return 0;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
show_dram_config(void)177*4882a593Smuzhiyun static int show_dram_config(void)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun unsigned long long size;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun #ifdef CONFIG_NR_DRAM_BANKS
182*4882a593Smuzhiyun int i;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun debug("\nRAM Configuration:\n");
185*4882a593Smuzhiyun for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
186*4882a593Smuzhiyun size += gd->bd->bi_dram[i].size;
187*4882a593Smuzhiyun debug("Bank #%d: %llx ", i,
188*4882a593Smuzhiyun (unsigned long long)(gd->bd->bi_dram[i].start));
189*4882a593Smuzhiyun #ifdef DEBUG
190*4882a593Smuzhiyun print_size(gd->bd->bi_dram[i].size, "\n");
191*4882a593Smuzhiyun #endif
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun debug("\nDRAM: ");
194*4882a593Smuzhiyun #else
195*4882a593Smuzhiyun size = gd->ram_size;
196*4882a593Smuzhiyun #endif
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun #ifdef CONFIG_BIDRAM
199*4882a593Smuzhiyun size += bidram_append_size();
200*4882a593Smuzhiyun #endif
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun #ifndef CONFIG_SUPPORT_USBPLUG
203*4882a593Smuzhiyun print_size(size, "");
204*4882a593Smuzhiyun board_add_ram_info(0);
205*4882a593Smuzhiyun putc('\n');
206*4882a593Smuzhiyun #endif
207*4882a593Smuzhiyun return 0;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
dram_init_banksize(void)210*4882a593Smuzhiyun __weak int dram_init_banksize(void)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
213*4882a593Smuzhiyun gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
214*4882a593Smuzhiyun gd->bd->bi_dram[0].size = get_effective_memsize();
215*4882a593Smuzhiyun #endif
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun return 0;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun #if defined(CONFIG_SYS_I2C)
init_func_i2c(void)221*4882a593Smuzhiyun static int init_func_i2c(void)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun puts("I2C: ");
224*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C
225*4882a593Smuzhiyun i2c_init_all();
226*4882a593Smuzhiyun #else
227*4882a593Smuzhiyun i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
228*4882a593Smuzhiyun #endif
229*4882a593Smuzhiyun puts("ready\n");
230*4882a593Smuzhiyun return 0;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun #endif
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun #if defined(CONFIG_HARD_SPI)
init_func_spi(void)235*4882a593Smuzhiyun static int init_func_spi(void)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun puts("SPI: ");
238*4882a593Smuzhiyun spi_init();
239*4882a593Smuzhiyun puts("ready\n");
240*4882a593Smuzhiyun return 0;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun #endif
243*4882a593Smuzhiyun
setup_mon_len(void)244*4882a593Smuzhiyun static int setup_mon_len(void)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun #if defined(__ARM__) || defined(__MICROBLAZE__)
247*4882a593Smuzhiyun gd->mon_len = (ulong)&__bss_end - (ulong)_start;
248*4882a593Smuzhiyun #elif defined(CONFIG_SANDBOX) || defined(CONFIG_EFI_APP)
249*4882a593Smuzhiyun gd->mon_len = (ulong)&_end - (ulong)_init;
250*4882a593Smuzhiyun #elif defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
251*4882a593Smuzhiyun gd->mon_len = CONFIG_SYS_MONITOR_LEN;
252*4882a593Smuzhiyun #elif defined(CONFIG_NDS32) || defined(CONFIG_SH) || defined(CONFIG_RISCV)
253*4882a593Smuzhiyun gd->mon_len = (ulong)(&__bss_end) - (ulong)(&_start);
254*4882a593Smuzhiyun #elif defined(CONFIG_SYS_MONITOR_BASE)
255*4882a593Smuzhiyun /* TODO: use (ulong)&__bss_end - (ulong)&__text_start; ? */
256*4882a593Smuzhiyun gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
257*4882a593Smuzhiyun #endif
258*4882a593Smuzhiyun #ifdef CONFIG_MP_BOOT
259*4882a593Smuzhiyun mpb_init_x(3);
260*4882a593Smuzhiyun #endif
261*4882a593Smuzhiyun return 0;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun
arch_cpu_init(void)264*4882a593Smuzhiyun __weak int arch_cpu_init(void)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun return 0;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun
mach_cpu_init(void)269*4882a593Smuzhiyun __weak int mach_cpu_init(void)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun return 0;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun /* Get the top of usable RAM */
board_get_usable_ram_top(ulong total_size)275*4882a593Smuzhiyun __weak ulong board_get_usable_ram_top(ulong total_size)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun #ifdef CONFIG_SYS_SDRAM_BASE
278*4882a593Smuzhiyun /*
279*4882a593Smuzhiyun * Detect whether we have so much RAM that it goes past the end of our
280*4882a593Smuzhiyun * 32-bit address space. If so, clip the usable RAM so it doesn't.
281*4882a593Smuzhiyun */
282*4882a593Smuzhiyun if (gd->ram_top < CONFIG_SYS_SDRAM_BASE)
283*4882a593Smuzhiyun /*
284*4882a593Smuzhiyun * Will wrap back to top of 32-bit space when reservations
285*4882a593Smuzhiyun * are made.
286*4882a593Smuzhiyun */
287*4882a593Smuzhiyun return 0;
288*4882a593Smuzhiyun #endif
289*4882a593Smuzhiyun return gd->ram_top;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
setup_dest_addr(void)292*4882a593Smuzhiyun static int setup_dest_addr(void)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun debug("Monitor len: %08lX\n", gd->mon_len);
295*4882a593Smuzhiyun /*
296*4882a593Smuzhiyun * Ram is setup, size stored in gd !!
297*4882a593Smuzhiyun */
298*4882a593Smuzhiyun debug("Ram size: %08lX\n", (ulong)gd->ram_size);
299*4882a593Smuzhiyun #if defined(CONFIG_SYS_MEM_TOP_HIDE)
300*4882a593Smuzhiyun /*
301*4882a593Smuzhiyun * Subtract specified amount of memory to hide so that it won't
302*4882a593Smuzhiyun * get "touched" at all by U-Boot. By fixing up gd->ram_size
303*4882a593Smuzhiyun * the Linux kernel should now get passed the now "corrected"
304*4882a593Smuzhiyun * memory size and won't touch it either. This should work
305*4882a593Smuzhiyun * for arch/ppc and arch/powerpc. Only Linux board ports in
306*4882a593Smuzhiyun * arch/powerpc with bootwrapper support, that recalculate the
307*4882a593Smuzhiyun * memory size from the SDRAM controller setup will have to
308*4882a593Smuzhiyun * get fixed.
309*4882a593Smuzhiyun */
310*4882a593Smuzhiyun gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
311*4882a593Smuzhiyun #endif
312*4882a593Smuzhiyun #ifdef CONFIG_SYS_SDRAM_BASE
313*4882a593Smuzhiyun gd->ram_top = CONFIG_SYS_SDRAM_BASE;
314*4882a593Smuzhiyun #endif
315*4882a593Smuzhiyun gd->ram_top += get_effective_memsize();
316*4882a593Smuzhiyun gd->ram_top = board_get_usable_ram_top(gd->mon_len);
317*4882a593Smuzhiyun gd->relocaddr = gd->ram_top;
318*4882a593Smuzhiyun debug("Ram top: %08lX\n", (ulong)gd->ram_top);
319*4882a593Smuzhiyun #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
320*4882a593Smuzhiyun /*
321*4882a593Smuzhiyun * We need to make sure the location we intend to put secondary core
322*4882a593Smuzhiyun * boot code is reserved and not used by any part of u-boot
323*4882a593Smuzhiyun */
324*4882a593Smuzhiyun if (gd->relocaddr > determine_mp_bootpg(NULL)) {
325*4882a593Smuzhiyun gd->relocaddr = determine_mp_bootpg(NULL);
326*4882a593Smuzhiyun debug("Reserving MP boot page to %08lx\n", gd->relocaddr);
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun #endif
329*4882a593Smuzhiyun return 0;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun #ifdef CONFIG_PRAM
333*4882a593Smuzhiyun /* reserve protected RAM */
reserve_pram(void)334*4882a593Smuzhiyun static int reserve_pram(void)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun ulong reg;
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun reg = env_get_ulong("pram", 10, CONFIG_PRAM);
339*4882a593Smuzhiyun gd->relocaddr -= (reg << 10); /* size is in kB */
340*4882a593Smuzhiyun debug("Reserving %ldk for protected RAM at %08lx\n", reg,
341*4882a593Smuzhiyun gd->relocaddr);
342*4882a593Smuzhiyun return 0;
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun #endif /* CONFIG_PRAM */
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun /* Round memory pointer down to next 4 kB limit */
reserve_round_4k(void)347*4882a593Smuzhiyun static int reserve_round_4k(void)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun gd->relocaddr &= ~(4096 - 1);
350*4882a593Smuzhiyun return 0;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun #ifdef CONFIG_ARM
reserve_mmu(void)354*4882a593Smuzhiyun __weak int reserve_mmu(void)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
357*4882a593Smuzhiyun /* reserve TLB table */
358*4882a593Smuzhiyun gd->arch.tlb_size = PGTABLE_SIZE;
359*4882a593Smuzhiyun gd->relocaddr -= gd->arch.tlb_size;
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun /* round down to next 64 kB limit */
362*4882a593Smuzhiyun gd->relocaddr &= ~(0x10000 - 1);
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun gd->arch.tlb_addr = gd->relocaddr;
365*4882a593Smuzhiyun debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
366*4882a593Smuzhiyun gd->arch.tlb_addr + gd->arch.tlb_size);
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
369*4882a593Smuzhiyun /*
370*4882a593Smuzhiyun * Record allocated tlb_addr in case gd->tlb_addr to be overwritten
371*4882a593Smuzhiyun * with location within secure ram.
372*4882a593Smuzhiyun */
373*4882a593Smuzhiyun gd->arch.tlb_allocated = gd->arch.tlb_addr;
374*4882a593Smuzhiyun #endif
375*4882a593Smuzhiyun #endif
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun return 0;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun #endif
380*4882a593Smuzhiyun
reserve_video(void)381*4882a593Smuzhiyun static int reserve_video(void)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun #ifdef CONFIG_DM_VIDEO
384*4882a593Smuzhiyun ulong addr;
385*4882a593Smuzhiyun int ret;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun addr = gd->relocaddr;
388*4882a593Smuzhiyun ret = video_reserve(&addr);
389*4882a593Smuzhiyun if (ret)
390*4882a593Smuzhiyun return ret;
391*4882a593Smuzhiyun gd->relocaddr = addr;
392*4882a593Smuzhiyun #elif defined(CONFIG_LCD)
393*4882a593Smuzhiyun # ifdef CONFIG_FB_ADDR
394*4882a593Smuzhiyun gd->fb_base = CONFIG_FB_ADDR;
395*4882a593Smuzhiyun # else
396*4882a593Smuzhiyun /* reserve memory for LCD display (always full pages) */
397*4882a593Smuzhiyun gd->relocaddr = lcd_setmem(gd->relocaddr);
398*4882a593Smuzhiyun gd->fb_base = gd->relocaddr;
399*4882a593Smuzhiyun # endif /* CONFIG_FB_ADDR */
400*4882a593Smuzhiyun #elif defined(CONFIG_VIDEO) && \
401*4882a593Smuzhiyun (!defined(CONFIG_PPC)) && \
402*4882a593Smuzhiyun !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
403*4882a593Smuzhiyun !defined(CONFIG_M68K)
404*4882a593Smuzhiyun /* reserve memory for video display (always full pages) */
405*4882a593Smuzhiyun gd->relocaddr = video_setmem(gd->relocaddr);
406*4882a593Smuzhiyun gd->fb_base = gd->relocaddr;
407*4882a593Smuzhiyun #endif
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun return 0;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun
reserve_trace(void)412*4882a593Smuzhiyun static int reserve_trace(void)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun #ifdef CONFIG_TRACE
415*4882a593Smuzhiyun gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
416*4882a593Smuzhiyun gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
417*4882a593Smuzhiyun debug("Reserving %dk for trace data at: %08lx\n",
418*4882a593Smuzhiyun CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
419*4882a593Smuzhiyun #endif
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun return 0;
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun
reserve_uboot(void)424*4882a593Smuzhiyun static int reserve_uboot(void)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun /*
427*4882a593Smuzhiyun * reserve memory for U-Boot code, data & bss
428*4882a593Smuzhiyun * round down to next 4 kB limit
429*4882a593Smuzhiyun */
430*4882a593Smuzhiyun gd->relocaddr -= gd->mon_len;
431*4882a593Smuzhiyun gd->relocaddr &= ~(4096 - 1);
432*4882a593Smuzhiyun #if defined(CONFIG_E500) || defined(CONFIG_MIPS)
433*4882a593Smuzhiyun /* round down to next 64 kB limit so that IVPR stays aligned */
434*4882a593Smuzhiyun gd->relocaddr &= ~(65536 - 1);
435*4882a593Smuzhiyun #endif
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
438*4882a593Smuzhiyun gd->relocaddr);
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun gd->start_addr_sp = gd->relocaddr;
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun return 0;
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun /* reserve memory for malloc() area */
reserve_malloc(void)446*4882a593Smuzhiyun static int reserve_malloc(void)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
449*4882a593Smuzhiyun debug("Reserving %dk for malloc() at: %08lx\n",
450*4882a593Smuzhiyun TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
451*4882a593Smuzhiyun return 0;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun #ifdef CONFIG_SYS_NONCACHED_MEMORY
reserve_noncached(void)455*4882a593Smuzhiyun static int reserve_noncached(void)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun phys_addr_t start, end;
458*4882a593Smuzhiyun size_t size;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun end = ALIGN(gd->start_addr_sp, MMU_SECTION_SIZE) - MMU_SECTION_SIZE;
461*4882a593Smuzhiyun size = ALIGN(CONFIG_SYS_NONCACHED_MEMORY, MMU_SECTION_SIZE);
462*4882a593Smuzhiyun start = end - size;
463*4882a593Smuzhiyun gd->start_addr_sp = start;
464*4882a593Smuzhiyun debug("Reserving %zu for noncached_alloc() at: %08lx\n",
465*4882a593Smuzhiyun size, gd->start_addr_sp);
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun return 0;
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun #endif
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun /* (permanently) allocate a Board Info struct */
reserve_board(void)472*4882a593Smuzhiyun static int reserve_board(void)
473*4882a593Smuzhiyun {
474*4882a593Smuzhiyun if (!gd->bd) {
475*4882a593Smuzhiyun gd->start_addr_sp -= sizeof(bd_t);
476*4882a593Smuzhiyun gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
477*4882a593Smuzhiyun memset(gd->bd, '\0', sizeof(bd_t));
478*4882a593Smuzhiyun debug("Reserving %zu Bytes for Board Info at: %08lx\n",
479*4882a593Smuzhiyun sizeof(bd_t), gd->start_addr_sp);
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun return 0;
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun
setup_machine(void)484*4882a593Smuzhiyun static int setup_machine(void)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun #ifdef CONFIG_MACH_TYPE
487*4882a593Smuzhiyun gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
488*4882a593Smuzhiyun #endif
489*4882a593Smuzhiyun return 0;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
reserve_global_data(void)492*4882a593Smuzhiyun static int reserve_global_data(void)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun gd->start_addr_sp -= sizeof(gd_t);
495*4882a593Smuzhiyun gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
496*4882a593Smuzhiyun debug("Reserving %zu Bytes for Global Data at: %08lx\n",
497*4882a593Smuzhiyun sizeof(gd_t), gd->start_addr_sp);
498*4882a593Smuzhiyun return 0;
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun
reserve_fdt(void)501*4882a593Smuzhiyun static int reserve_fdt(void)
502*4882a593Smuzhiyun {
503*4882a593Smuzhiyun #ifndef CONFIG_OF_EMBED
504*4882a593Smuzhiyun /*
505*4882a593Smuzhiyun * If the device tree is sitting immediately above our image then we
506*4882a593Smuzhiyun * must relocate it. If it is embedded in the data section, then it
507*4882a593Smuzhiyun * will be relocated with other data.
508*4882a593Smuzhiyun */
509*4882a593Smuzhiyun if (gd->fdt_blob) {
510*4882a593Smuzhiyun u32 extrasize = 0;
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun if (gd->fdt_blob_kern)
513*4882a593Smuzhiyun extrasize = fdt_totalsize(gd->fdt_blob_kern);
514*4882a593Smuzhiyun gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + extrasize + 0x1000, 32);
515*4882a593Smuzhiyun gd->start_addr_sp -= gd->fdt_size;
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun /* 8-byte align */
518*4882a593Smuzhiyun gd->start_addr_sp -= 8;
519*4882a593Smuzhiyun gd->start_addr_sp &= ~0x7;
520*4882a593Smuzhiyun gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun if (gd->fdt_blob_kern)
523*4882a593Smuzhiyun gd->fdt_blob_kern = (ulong *)ALIGN((ulong)gd->new_fdt +
524*4882a593Smuzhiyun fdt_totalsize(gd->fdt_blob), 8);
525*4882a593Smuzhiyun debug("Reserving %lu Bytes for FDT at: %08lx\n",
526*4882a593Smuzhiyun gd->fdt_size, gd->start_addr_sp);
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun #endif
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun return 0;
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun
reserve_bootstage(void)533*4882a593Smuzhiyun static int reserve_bootstage(void)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun #ifdef CONFIG_BOOTSTAGE
536*4882a593Smuzhiyun int size = bootstage_get_size();
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun gd->start_addr_sp -= size;
539*4882a593Smuzhiyun gd->new_bootstage = map_sysmem(gd->start_addr_sp, size);
540*4882a593Smuzhiyun debug("Reserving %#x Bytes for bootstage at: %08lx\n", size,
541*4882a593Smuzhiyun gd->start_addr_sp);
542*4882a593Smuzhiyun #endif
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun return 0;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun
arch_reserve_stacks(void)547*4882a593Smuzhiyun int arch_reserve_stacks(void)
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun return 0;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun
reserve_stacks(void)552*4882a593Smuzhiyun static int reserve_stacks(void)
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun /* make stack pointer 16-byte aligned */
555*4882a593Smuzhiyun gd->start_addr_sp -= 16;
556*4882a593Smuzhiyun gd->start_addr_sp &= ~0xf;
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun /*
559*4882a593Smuzhiyun * let the architecture-specific code tailor gd->start_addr_sp and
560*4882a593Smuzhiyun * gd->irq_sp
561*4882a593Smuzhiyun */
562*4882a593Smuzhiyun return arch_reserve_stacks();
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun
display_new_sp(void)565*4882a593Smuzhiyun static int display_new_sp(void)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun return 0;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
573*4882a593Smuzhiyun defined(CONFIG_SH)
setup_board_part1(void)574*4882a593Smuzhiyun static int setup_board_part1(void)
575*4882a593Smuzhiyun {
576*4882a593Smuzhiyun bd_t *bd = gd->bd;
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun /*
579*4882a593Smuzhiyun * Save local variables to board info struct
580*4882a593Smuzhiyun */
581*4882a593Smuzhiyun bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of memory */
582*4882a593Smuzhiyun bd->bi_memsize = gd->ram_size; /* size in bytes */
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun #ifdef CONFIG_SYS_SRAM_BASE
585*4882a593Smuzhiyun bd->bi_sramstart = CONFIG_SYS_SRAM_BASE; /* start of SRAM */
586*4882a593Smuzhiyun bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE; /* size of SRAM */
587*4882a593Smuzhiyun #endif
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun #if defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
590*4882a593Smuzhiyun bd->bi_immr_base = CONFIG_SYS_IMMR; /* base of IMMR register */
591*4882a593Smuzhiyun #endif
592*4882a593Smuzhiyun #if defined(CONFIG_M68K)
593*4882a593Smuzhiyun bd->bi_mbar_base = CONFIG_SYS_MBAR; /* base of internal registers */
594*4882a593Smuzhiyun #endif
595*4882a593Smuzhiyun #if defined(CONFIG_MPC83xx)
596*4882a593Smuzhiyun bd->bi_immrbar = CONFIG_SYS_IMMR;
597*4882a593Smuzhiyun #endif
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun return 0;
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun #endif
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
setup_board_part2(void)604*4882a593Smuzhiyun static int setup_board_part2(void)
605*4882a593Smuzhiyun {
606*4882a593Smuzhiyun bd_t *bd = gd->bd;
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun bd->bi_intfreq = gd->cpu_clk; /* Internal Freq, in Hz */
609*4882a593Smuzhiyun bd->bi_busfreq = gd->bus_clk; /* Bus Freq, in Hz */
610*4882a593Smuzhiyun #if defined(CONFIG_CPM2)
611*4882a593Smuzhiyun bd->bi_cpmfreq = gd->arch.cpm_clk;
612*4882a593Smuzhiyun bd->bi_brgfreq = gd->arch.brg_clk;
613*4882a593Smuzhiyun bd->bi_sccfreq = gd->arch.scc_clk;
614*4882a593Smuzhiyun bd->bi_vco = gd->arch.vco_out;
615*4882a593Smuzhiyun #endif /* CONFIG_CPM2 */
616*4882a593Smuzhiyun #if defined(CONFIG_M68K) && defined(CONFIG_PCI)
617*4882a593Smuzhiyun bd->bi_pcifreq = gd->pci_clk;
618*4882a593Smuzhiyun #endif
619*4882a593Smuzhiyun #if defined(CONFIG_EXTRA_CLOCK)
620*4882a593Smuzhiyun bd->bi_inpfreq = gd->arch.inp_clk; /* input Freq in Hz */
621*4882a593Smuzhiyun bd->bi_vcofreq = gd->arch.vco_clk; /* vco Freq in Hz */
622*4882a593Smuzhiyun bd->bi_flbfreq = gd->arch.flb_clk; /* flexbus Freq in Hz */
623*4882a593Smuzhiyun #endif
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun return 0;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun #endif
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun #ifdef CONFIG_POST
init_post(void)630*4882a593Smuzhiyun static int init_post(void)
631*4882a593Smuzhiyun {
632*4882a593Smuzhiyun post_bootmode_init();
633*4882a593Smuzhiyun post_run(NULL, POST_ROM | post_bootmode_get(0));
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun return 0;
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun #endif
638*4882a593Smuzhiyun
reloc_fdt(void)639*4882a593Smuzhiyun static int reloc_fdt(void)
640*4882a593Smuzhiyun {
641*4882a593Smuzhiyun #ifndef CONFIG_OF_EMBED
642*4882a593Smuzhiyun if (gd->flags & GD_FLG_SKIP_RELOC)
643*4882a593Smuzhiyun return 0;
644*4882a593Smuzhiyun if (gd->new_fdt) {
645*4882a593Smuzhiyun memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
646*4882a593Smuzhiyun gd->fdt_blob = gd->new_fdt;
647*4882a593Smuzhiyun #ifdef CONFIG_USING_KERNEL_DTB
648*4882a593Smuzhiyun gd->ufdt_blob = gd->new_fdt;
649*4882a593Smuzhiyun #endif
650*4882a593Smuzhiyun }
651*4882a593Smuzhiyun #endif
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun return 0;
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun
reloc_bootstage(void)656*4882a593Smuzhiyun static int reloc_bootstage(void)
657*4882a593Smuzhiyun {
658*4882a593Smuzhiyun #ifdef CONFIG_BOOTSTAGE
659*4882a593Smuzhiyun if (gd->flags & GD_FLG_SKIP_RELOC)
660*4882a593Smuzhiyun return 0;
661*4882a593Smuzhiyun if (gd->new_bootstage) {
662*4882a593Smuzhiyun int size = bootstage_get_size();
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun debug("Copying bootstage from %p to %p, size %x\n",
665*4882a593Smuzhiyun gd->bootstage, gd->new_bootstage, size);
666*4882a593Smuzhiyun memcpy(gd->new_bootstage, gd->bootstage, size);
667*4882a593Smuzhiyun gd->bootstage = gd->new_bootstage;
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun #endif
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun return 0;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun
setup_reloc(void)674*4882a593Smuzhiyun static int setup_reloc(void)
675*4882a593Smuzhiyun {
676*4882a593Smuzhiyun if (gd->flags & GD_FLG_SKIP_RELOC) {
677*4882a593Smuzhiyun debug("Skipping relocation due to flag\n");
678*4882a593Smuzhiyun return 0;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun #ifndef CONFIG_SKIP_RELOCATE_UBOOT
682*4882a593Smuzhiyun #ifdef CONFIG_SYS_TEXT_BASE
683*4882a593Smuzhiyun #ifdef ARM
684*4882a593Smuzhiyun gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
685*4882a593Smuzhiyun #elif defined(CONFIG_M68K)
686*4882a593Smuzhiyun /*
687*4882a593Smuzhiyun * On all ColdFire arch cpu, monitor code starts always
688*4882a593Smuzhiyun * just after the default vector table location, so at 0x400
689*4882a593Smuzhiyun */
690*4882a593Smuzhiyun gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
691*4882a593Smuzhiyun #else
692*4882a593Smuzhiyun gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
693*4882a593Smuzhiyun #endif
694*4882a593Smuzhiyun #endif
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun #else
697*4882a593Smuzhiyun gd->reloc_off = 0;
698*4882a593Smuzhiyun #endif
699*4882a593Smuzhiyun memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun #ifndef CONFIG_SUPPORT_USBPLUG
702*4882a593Smuzhiyun printf("Relocation Offset: %08lx\n", gd->reloc_off);
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun printf("Relocation fdt: %08lx - %08lx", (ulong)gd->new_fdt,
705*4882a593Smuzhiyun (ulong)gd->new_fdt + fdt_totalsize(gd->fdt_blob));
706*4882a593Smuzhiyun if (gd->fdt_blob_kern) {
707*4882a593Smuzhiyun printf(", kfdt: %08lx - %08lx", (ulong)gd->fdt_blob_kern,
708*4882a593Smuzhiyun (ulong)gd->fdt_blob_kern + fdt_totalsize(gd->fdt_blob_kern));
709*4882a593Smuzhiyun }
710*4882a593Smuzhiyun puts("\n");
711*4882a593Smuzhiyun #endif
712*4882a593Smuzhiyun debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
713*4882a593Smuzhiyun gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
714*4882a593Smuzhiyun gd->start_addr_sp);
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun return 0;
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun #ifdef CONFIG_OF_BOARD_FIXUP
fix_fdt(void)720*4882a593Smuzhiyun static int fix_fdt(void)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun return board_fix_fdt((void *)gd->fdt_blob);
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun #endif
725*4882a593Smuzhiyun
726*4882a593Smuzhiyun /* ARM calls relocate_code from its crt0.S */
727*4882a593Smuzhiyun #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
728*4882a593Smuzhiyun !CONFIG_IS_ENABLED(X86_64)
729*4882a593Smuzhiyun
jump_to_copy(void)730*4882a593Smuzhiyun static int jump_to_copy(void)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun if (gd->flags & GD_FLG_SKIP_RELOC)
733*4882a593Smuzhiyun return 0;
734*4882a593Smuzhiyun /*
735*4882a593Smuzhiyun * x86 is special, but in a nice way. It uses a trampoline which
736*4882a593Smuzhiyun * enables the dcache if possible.
737*4882a593Smuzhiyun *
738*4882a593Smuzhiyun * For now, other archs use relocate_code(), which is implemented
739*4882a593Smuzhiyun * similarly for all archs. When we do generic relocation, hopefully
740*4882a593Smuzhiyun * we can make all archs enable the dcache prior to relocation.
741*4882a593Smuzhiyun */
742*4882a593Smuzhiyun #if defined(CONFIG_X86) || defined(CONFIG_ARC)
743*4882a593Smuzhiyun /*
744*4882a593Smuzhiyun * SDRAM and console are now initialised. The final stack can now
745*4882a593Smuzhiyun * be setup in SDRAM. Code execution will continue in Flash, but
746*4882a593Smuzhiyun * with the stack in SDRAM and Global Data in temporary memory
747*4882a593Smuzhiyun * (CPU cache)
748*4882a593Smuzhiyun */
749*4882a593Smuzhiyun arch_setup_gd(gd->new_gd);
750*4882a593Smuzhiyun board_init_f_r_trampoline(gd->start_addr_sp);
751*4882a593Smuzhiyun #else
752*4882a593Smuzhiyun relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
753*4882a593Smuzhiyun #endif
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun return 0;
756*4882a593Smuzhiyun }
757*4882a593Smuzhiyun #endif
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun /* Record the board_init_f() bootstage (after arch_cpu_init()) */
initf_bootstage(void)760*4882a593Smuzhiyun static int initf_bootstage(void)
761*4882a593Smuzhiyun {
762*4882a593Smuzhiyun bool from_spl = IS_ENABLED(CONFIG_SPL_BOOTSTAGE) &&
763*4882a593Smuzhiyun IS_ENABLED(CONFIG_BOOTSTAGE_STASH);
764*4882a593Smuzhiyun int ret;
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun ret = bootstage_init(!from_spl);
767*4882a593Smuzhiyun if (ret)
768*4882a593Smuzhiyun return ret;
769*4882a593Smuzhiyun if (from_spl) {
770*4882a593Smuzhiyun const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
771*4882a593Smuzhiyun CONFIG_BOOTSTAGE_STASH_SIZE);
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
774*4882a593Smuzhiyun if (ret && ret != -ENOENT) {
775*4882a593Smuzhiyun debug("Failed to unstash bootstage: err=%d\n", ret);
776*4882a593Smuzhiyun return ret;
777*4882a593Smuzhiyun }
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun
780*4882a593Smuzhiyun bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun return 0;
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun
initf_console_record(void)785*4882a593Smuzhiyun static int initf_console_record(void)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun #if defined(CONFIG_CONSOLE_RECORD) && CONFIG_VAL(SYS_MALLOC_F_LEN)
788*4882a593Smuzhiyun return console_record_init();
789*4882a593Smuzhiyun #else
790*4882a593Smuzhiyun return 0;
791*4882a593Smuzhiyun #endif
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun
initf_dm(void)794*4882a593Smuzhiyun static int initf_dm(void)
795*4882a593Smuzhiyun {
796*4882a593Smuzhiyun #if defined(CONFIG_DM) && CONFIG_VAL(SYS_MALLOC_F_LEN)
797*4882a593Smuzhiyun int ret;
798*4882a593Smuzhiyun
799*4882a593Smuzhiyun bootstage_start(BOOTSTATE_ID_ACCUM_DM_F, "dm_f");
800*4882a593Smuzhiyun ret = dm_init_and_scan(true);
801*4882a593Smuzhiyun bootstage_accum(BOOTSTATE_ID_ACCUM_DM_F);
802*4882a593Smuzhiyun if (ret)
803*4882a593Smuzhiyun return ret;
804*4882a593Smuzhiyun #endif
805*4882a593Smuzhiyun #ifdef CONFIG_TIMER_EARLY
806*4882a593Smuzhiyun ret = dm_timer_init();
807*4882a593Smuzhiyun if (ret)
808*4882a593Smuzhiyun return ret;
809*4882a593Smuzhiyun #endif
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun return 0;
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun /* Architecture-specific memory reservation */
reserve_arch(void)815*4882a593Smuzhiyun __weak int reserve_arch(void)
816*4882a593Smuzhiyun {
817*4882a593Smuzhiyun return 0;
818*4882a593Smuzhiyun }
819*4882a593Smuzhiyun
arch_cpu_init_dm(void)820*4882a593Smuzhiyun __weak int arch_cpu_init_dm(void)
821*4882a593Smuzhiyun {
822*4882a593Smuzhiyun return 0;
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun static const init_fnc_t init_sequence_f[] = {
826*4882a593Smuzhiyun setup_mon_len,
827*4882a593Smuzhiyun #ifdef CONFIG_OF_CONTROL
828*4882a593Smuzhiyun fdtdec_setup,
829*4882a593Smuzhiyun #endif
830*4882a593Smuzhiyun #ifdef CONFIG_TRACE
831*4882a593Smuzhiyun trace_early_init,
832*4882a593Smuzhiyun #endif
833*4882a593Smuzhiyun initf_malloc,
834*4882a593Smuzhiyun log_init,
835*4882a593Smuzhiyun initf_bootstage, /* uses its own timer, so does not need DM */
836*4882a593Smuzhiyun initf_console_record,
837*4882a593Smuzhiyun #if defined(CONFIG_HAVE_FSP)
838*4882a593Smuzhiyun arch_fsp_init,
839*4882a593Smuzhiyun #endif
840*4882a593Smuzhiyun arch_cpu_init, /* basic arch cpu dependent setup */
841*4882a593Smuzhiyun mach_cpu_init, /* SoC/machine dependent CPU setup */
842*4882a593Smuzhiyun initf_dm,
843*4882a593Smuzhiyun arch_cpu_init_dm,
844*4882a593Smuzhiyun #if defined(CONFIG_BOARD_EARLY_INIT_F)
845*4882a593Smuzhiyun board_early_init_f,
846*4882a593Smuzhiyun #endif
847*4882a593Smuzhiyun #if defined(CONFIG_PPC) || defined(CONFIG_SYS_FSL_CLK) || defined(CONFIG_M68K)
848*4882a593Smuzhiyun /* get CPU and bus clocks according to the environment variable */
849*4882a593Smuzhiyun get_clocks, /* get CPU and bus clocks (etc.) */
850*4882a593Smuzhiyun #endif
851*4882a593Smuzhiyun #if !defined(CONFIG_M68K)
852*4882a593Smuzhiyun timer_init, /* initialize timer */
853*4882a593Smuzhiyun #endif
854*4882a593Smuzhiyun #if defined(CONFIG_BOARD_POSTCLK_INIT)
855*4882a593Smuzhiyun board_postclk_init,
856*4882a593Smuzhiyun #endif
857*4882a593Smuzhiyun env_init, /* initialize environment */
858*4882a593Smuzhiyun init_baud_rate, /* initialze baudrate settings */
859*4882a593Smuzhiyun serial_init, /* serial communications setup */
860*4882a593Smuzhiyun console_init_f, /* stage 1 init of console */
861*4882a593Smuzhiyun display_options, /* say that we are here */
862*4882a593Smuzhiyun display_text_info, /* show debugging info if required */
863*4882a593Smuzhiyun #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SH) || \
864*4882a593Smuzhiyun defined(CONFIG_X86)
865*4882a593Smuzhiyun checkcpu,
866*4882a593Smuzhiyun #endif
867*4882a593Smuzhiyun #if defined(CONFIG_DISPLAY_CPUINFO)
868*4882a593Smuzhiyun print_cpuinfo, /* display cpu info (and speed) */
869*4882a593Smuzhiyun #endif
870*4882a593Smuzhiyun #if defined(CONFIG_DTB_RESELECT)
871*4882a593Smuzhiyun embedded_dtb_select,
872*4882a593Smuzhiyun #endif
873*4882a593Smuzhiyun #if defined(CONFIG_DISPLAY_BOARDINFO)
874*4882a593Smuzhiyun show_board_info,
875*4882a593Smuzhiyun #endif
876*4882a593Smuzhiyun INIT_FUNC_WATCHDOG_INIT
877*4882a593Smuzhiyun #if defined(CONFIG_MISC_INIT_F)
878*4882a593Smuzhiyun misc_init_f,
879*4882a593Smuzhiyun #endif
880*4882a593Smuzhiyun INIT_FUNC_WATCHDOG_RESET
881*4882a593Smuzhiyun #if defined(CONFIG_SYS_I2C)
882*4882a593Smuzhiyun init_func_i2c,
883*4882a593Smuzhiyun #endif
884*4882a593Smuzhiyun #if defined(CONFIG_HARD_SPI)
885*4882a593Smuzhiyun init_func_spi,
886*4882a593Smuzhiyun #endif
887*4882a593Smuzhiyun announce_serial,
888*4882a593Smuzhiyun
889*4882a593Smuzhiyun announce_dram_init,
890*4882a593Smuzhiyun dram_init, /* configure available RAM banks */
891*4882a593Smuzhiyun #ifdef CONFIG_POST
892*4882a593Smuzhiyun post_init_f,
893*4882a593Smuzhiyun #endif
894*4882a593Smuzhiyun INIT_FUNC_WATCHDOG_RESET
895*4882a593Smuzhiyun #if defined(CONFIG_SYS_DRAM_TEST)
896*4882a593Smuzhiyun testdram,
897*4882a593Smuzhiyun #endif /* CONFIG_SYS_DRAM_TEST */
898*4882a593Smuzhiyun INIT_FUNC_WATCHDOG_RESET
899*4882a593Smuzhiyun
900*4882a593Smuzhiyun #ifdef CONFIG_POST
901*4882a593Smuzhiyun init_post,
902*4882a593Smuzhiyun #endif
903*4882a593Smuzhiyun INIT_FUNC_WATCHDOG_RESET
904*4882a593Smuzhiyun /*
905*4882a593Smuzhiyun * Now that we have DRAM mapped and working, we can
906*4882a593Smuzhiyun * relocate the code and continue running from DRAM.
907*4882a593Smuzhiyun *
908*4882a593Smuzhiyun * Reserve memory at end of RAM for (top down in that order):
909*4882a593Smuzhiyun * - area that won't get touched by U-Boot and Linux (optional)
910*4882a593Smuzhiyun * - kernel log buffer
911*4882a593Smuzhiyun * - protected RAM
912*4882a593Smuzhiyun * - LCD framebuffer
913*4882a593Smuzhiyun * - monitor code
914*4882a593Smuzhiyun * - board info struct
915*4882a593Smuzhiyun */
916*4882a593Smuzhiyun setup_dest_addr,
917*4882a593Smuzhiyun #ifdef CONFIG_PRAM
918*4882a593Smuzhiyun reserve_pram,
919*4882a593Smuzhiyun #endif
920*4882a593Smuzhiyun reserve_round_4k,
921*4882a593Smuzhiyun #ifdef CONFIG_ARM
922*4882a593Smuzhiyun reserve_mmu,
923*4882a593Smuzhiyun #endif
924*4882a593Smuzhiyun reserve_video,
925*4882a593Smuzhiyun reserve_trace,
926*4882a593Smuzhiyun reserve_uboot,
927*4882a593Smuzhiyun reserve_malloc,
928*4882a593Smuzhiyun #ifdef CONFIG_SYS_NONCACHED_MEMORY
929*4882a593Smuzhiyun reserve_noncached,
930*4882a593Smuzhiyun #endif
931*4882a593Smuzhiyun reserve_board,
932*4882a593Smuzhiyun setup_machine,
933*4882a593Smuzhiyun reserve_global_data,
934*4882a593Smuzhiyun reserve_fdt,
935*4882a593Smuzhiyun reserve_bootstage,
936*4882a593Smuzhiyun reserve_arch,
937*4882a593Smuzhiyun reserve_stacks,
938*4882a593Smuzhiyun dram_init_banksize,
939*4882a593Smuzhiyun show_dram_config,
940*4882a593Smuzhiyun #ifdef CONFIG_SYSMEM
941*4882a593Smuzhiyun sysmem_init, /* Validate above reserve memory */
942*4882a593Smuzhiyun #endif
943*4882a593Smuzhiyun #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
944*4882a593Smuzhiyun defined(CONFIG_SH)
945*4882a593Smuzhiyun setup_board_part1,
946*4882a593Smuzhiyun #endif
947*4882a593Smuzhiyun #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
948*4882a593Smuzhiyun INIT_FUNC_WATCHDOG_RESET
949*4882a593Smuzhiyun setup_board_part2,
950*4882a593Smuzhiyun #endif
951*4882a593Smuzhiyun display_new_sp,
952*4882a593Smuzhiyun #ifdef CONFIG_OF_BOARD_FIXUP
953*4882a593Smuzhiyun fix_fdt,
954*4882a593Smuzhiyun #endif
955*4882a593Smuzhiyun INIT_FUNC_WATCHDOG_RESET
956*4882a593Smuzhiyun reloc_fdt,
957*4882a593Smuzhiyun reloc_bootstage,
958*4882a593Smuzhiyun setup_reloc,
959*4882a593Smuzhiyun #if defined(CONFIG_X86) || defined(CONFIG_ARC)
960*4882a593Smuzhiyun copy_uboot_to_ram,
961*4882a593Smuzhiyun do_elf_reloc_fixups,
962*4882a593Smuzhiyun clear_bss,
963*4882a593Smuzhiyun #endif
964*4882a593Smuzhiyun #if defined(CONFIG_XTENSA)
965*4882a593Smuzhiyun clear_bss,
966*4882a593Smuzhiyun #endif
967*4882a593Smuzhiyun #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
968*4882a593Smuzhiyun !CONFIG_IS_ENABLED(X86_64)
969*4882a593Smuzhiyun jump_to_copy,
970*4882a593Smuzhiyun #endif
971*4882a593Smuzhiyun NULL,
972*4882a593Smuzhiyun };
973*4882a593Smuzhiyun
board_init_f(ulong boot_flags)974*4882a593Smuzhiyun void board_init_f(ulong boot_flags)
975*4882a593Smuzhiyun {
976*4882a593Smuzhiyun gd->flags = boot_flags;
977*4882a593Smuzhiyun gd->have_console = 0;
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun if (initcall_run_list(init_sequence_f))
980*4882a593Smuzhiyun hang();
981*4882a593Smuzhiyun
982*4882a593Smuzhiyun #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
983*4882a593Smuzhiyun !defined(CONFIG_EFI_APP) && !CONFIG_IS_ENABLED(X86_64)
984*4882a593Smuzhiyun /* NOTREACHED - jump_to_copy() does not return */
985*4882a593Smuzhiyun hang();
986*4882a593Smuzhiyun #endif
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun #if defined(CONFIG_X86) || defined(CONFIG_ARC)
990*4882a593Smuzhiyun /*
991*4882a593Smuzhiyun * For now this code is only used on x86.
992*4882a593Smuzhiyun *
993*4882a593Smuzhiyun * init_sequence_f_r is the list of init functions which are run when
994*4882a593Smuzhiyun * U-Boot is executing from Flash with a semi-limited 'C' environment.
995*4882a593Smuzhiyun * The following limitations must be considered when implementing an
996*4882a593Smuzhiyun * '_f_r' function:
997*4882a593Smuzhiyun * - 'static' variables are read-only
998*4882a593Smuzhiyun * - Global Data (gd->xxx) is read/write
999*4882a593Smuzhiyun *
1000*4882a593Smuzhiyun * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
1001*4882a593Smuzhiyun * supported). It _should_, if possible, copy global data to RAM and
1002*4882a593Smuzhiyun * initialise the CPU caches (to speed up the relocation process)
1003*4882a593Smuzhiyun *
1004*4882a593Smuzhiyun * NOTE: At present only x86 uses this route, but it is intended that
1005*4882a593Smuzhiyun * all archs will move to this when generic relocation is implemented.
1006*4882a593Smuzhiyun */
1007*4882a593Smuzhiyun static const init_fnc_t init_sequence_f_r[] = {
1008*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(X86_64)
1009*4882a593Smuzhiyun init_cache_f_r,
1010*4882a593Smuzhiyun #endif
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun NULL,
1013*4882a593Smuzhiyun };
1014*4882a593Smuzhiyun
board_init_f_r(void)1015*4882a593Smuzhiyun void board_init_f_r(void)
1016*4882a593Smuzhiyun {
1017*4882a593Smuzhiyun if (initcall_run_list(init_sequence_f_r))
1018*4882a593Smuzhiyun hang();
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun /*
1021*4882a593Smuzhiyun * The pre-relocation drivers may be using memory that has now gone
1022*4882a593Smuzhiyun * away. Mark serial as unavailable - this will fall back to the debug
1023*4882a593Smuzhiyun * UART if available.
1024*4882a593Smuzhiyun *
1025*4882a593Smuzhiyun * Do the same with log drivers since the memory may not be available.
1026*4882a593Smuzhiyun */
1027*4882a593Smuzhiyun gd->flags &= ~(GD_FLG_SERIAL_READY | GD_FLG_LOG_READY);
1028*4882a593Smuzhiyun #ifdef CONFIG_TIMER
1029*4882a593Smuzhiyun gd->timer = NULL;
1030*4882a593Smuzhiyun #endif
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun /*
1033*4882a593Smuzhiyun * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
1034*4882a593Smuzhiyun * Transfer execution from Flash to RAM by calculating the address
1035*4882a593Smuzhiyun * of the in-RAM copy of board_init_r() and calling it
1036*4882a593Smuzhiyun */
1037*4882a593Smuzhiyun (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
1038*4882a593Smuzhiyun
1039*4882a593Smuzhiyun /* NOTREACHED - board_init_r() does not return */
1040*4882a593Smuzhiyun hang();
1041*4882a593Smuzhiyun }
1042*4882a593Smuzhiyun #endif /* CONFIG_X86 */
1043