2 * Copyright (c) 2011 The Chromium OS Authors.
3 * (C) Copyright 2002-2006
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Marius Groeger <mgroeger@sysgo.de>
10 * See file CREDITS for list of people who contributed to this
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #include <linux/compiler.h>
32 #include <environment.h>
38 #include <asm/sections.h>
39 #include <linux/compiler.h>
42 * Pointer to initial global data area
44 * Here we initialize it if needed.
46 #ifdef XTRN_DECLARE_GLOBAL_DATA_PTR
47 #undef XTRN_DECLARE_GLOBAL_DATA_PTR
48 #define XTRN_DECLARE_GLOBAL_DATA_PTR /* empty = allocate here */
49 DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
51 DECLARE_GLOBAL_DATA_PTR;
55 * sjg: IMO this code should be
56 * refactored to a single function, something like:
58 * void led_set_state(enum led_colour_t colour, int on);
60 /************************************************************************
61 * Coloured LED functionality
62 ************************************************************************
63 * May be supplied by boards if desired
65 inline void __coloured_LED_init(void) {}
66 void coloured_LED_init(void)
67 __attribute__((weak, alias("__coloured_LED_init")));
68 inline void __red_led_on(void) {}
69 void red_led_on(void) __attribute__((weak, alias("__red_led_on")));
70 inline void __red_led_off(void) {}
71 void red_led_off(void) __attribute__((weak, alias("__red_led_off")));
72 inline void __green_led_on(void) {}
73 void green_led_on(void) __attribute__((weak, alias("__green_led_on")));
74 inline void __green_led_off(void) {}
75 void green_led_off(void) __attribute__((weak, alias("__green_led_off")));
76 inline void __yellow_led_on(void) {}
77 void yellow_led_on(void) __attribute__((weak, alias("__yellow_led_on")));
78 inline void __yellow_led_off(void) {}
79 void yellow_led_off(void) __attribute__((weak, alias("__yellow_led_off")));
80 inline void __blue_led_on(void) {}
81 void blue_led_on(void) __attribute__((weak, alias("__blue_led_on")));
82 inline void __blue_led_off(void) {}
83 void blue_led_off(void) __attribute__((weak, alias("__blue_led_off")));
86 * Why is gd allocated a register? Prior to reloc it might be better to
87 * just pass it around to each function in this file?
89 * After reloc one could argue that it is hardly used and doesn't need
90 * to be in a register. Or if it is it should perhaps hold pointers to all
91 * global data for all modules, so that post-reloc we can avoid the massive
92 * literal pool we get on ARM. Or perhaps just encourage each module to use
97 * Could the CONFIG_SPL_BUILD infection become a flag in gd?
100 static int init_baud_rate(void)
102 gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
106 static int display_text_info(void)
108 ulong bss_start, bss_end;
110 bss_start = _bss_start_ofs + _TEXT_BASE;
111 bss_end = _bss_end_ofs + _TEXT_BASE;
112 debug("U-Boot code: %08X -> %08lX BSS: -> %08lX\n",
113 CONFIG_SYS_TEXT_BASE, bss_start, bss_end);
115 #ifdef CONFIG_MODEM_SUPPORT
116 debug("Modem Support enabled\n");
118 #ifdef CONFIG_USE_IRQ
119 debug("IRQ Stack: %08lx\n", IRQ_STACK_START);
120 debug("FIQ Stack: %08lx\n", FIQ_STACK_START);
126 static int announce_dram_init(void)
132 static int show_dram_config(void)
136 #ifdef CONFIG_NR_DRAM_BANKS
139 debug("\nRAM Configuration:\n");
140 for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
141 size += gd->bd->bi_dram[i].size;
142 debug("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
144 print_size(gd->bd->bi_dram[i].size, "\n");
152 print_size(size, "\n");
157 void __dram_init_banksize(void)
159 #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
160 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
161 gd->bd->bi_dram[0].size = get_effective_memsize();
165 void dram_init_banksize(void)
166 __attribute__((weak, alias("__dram_init_banksize")));
168 static int zero_global_data(void)
170 memset((void *)gd, '\0', sizeof(gd_t));
175 static int setup_mon_len(void)
177 gd->mon_len = _bss_end_ofs;
181 __weak int arch_cpu_init(void)
186 static int setup_fdt(void)
188 #ifdef CONFIG_OF_EMBED
189 /* Get a pointer to the FDT */
190 gd->fdt_blob = _binary_dt_dtb_start;
191 #elif defined CONFIG_OF_SEPARATE
192 /* FDT is at end of image */
193 gd->fdt_blob = (void *)(_end_ofs + CONFIG_SYS_TEXT_BASE);
195 /* Allow the early environment to override the fdt address */
196 gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
197 (uintptr_t)gd->fdt_blob);
201 /* Get the top of usable RAM */
202 __weak ulong board_get_usable_ram_top(ulong total_size)
207 static int setup_dest_addr(void)
209 debug("Monitor len: %08lX\n", gd->mon_len);
211 * Ram is setup, size stored in gd !!
213 debug("Ram size: %08lX\n", (ulong)gd->ram_size);
214 #if defined(CONFIG_SYS_MEM_TOP_HIDE)
216 * Subtract specified amount of memory to hide so that it won't
217 * get "touched" at all by U-Boot. By fixing up gd->ram_size
218 * the Linux kernel should now get passed the now "corrected"
219 * memory size and won't touch it either. This should work
220 * for arch/ppc and arch/powerpc. Only Linux board ports in
221 * arch/powerpc with bootwrapper support, that recalculate the
222 * memory size from the SDRAM controller setup will have to
225 gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
227 #ifdef CONFIG_SYS_SDRAM_BASE
228 gd->ram_top = CONFIG_SYS_SDRAM_BASE;
230 gd->ram_top = board_get_usable_ram_top(gd->mon_len);
231 gd->dest_addr = gd->ram_top;
232 debug("Ram top: %08lX\n", (ulong)gd->ram_top);
233 gd->dest_addr_sp = gd->dest_addr;
237 #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
238 static int reserve_logbuffer(void)
240 /* reserve kernel log buffer */
241 gd->dest_addr -= LOGBUFF_RESERVE;
242 debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
249 /* reserve protected RAM */
250 static int reserve_pram(void)
254 reg = getenv_ulong("pram", 10, CONFIG_PRAM);
255 gd->dest_addr -= (reg << 10); /* size is in kB */
256 debug("Reserving %ldk for protected RAM at %08lx\n", reg,
260 #endif /* CONFIG_PRAM */
262 /* Round memory pointer down to next 4 kB limit */
263 static int reserve_round_4k(void)
265 gd->dest_addr &= ~(4096 - 1);
269 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
271 static int reserve_mmu(void)
273 /* reserve TLB table */
274 gd->arch.tlb_size = 4096 * 4;
275 gd->dest_addr -= gd->arch.tlb_size;
277 /* round down to next 64 kB limit */
278 gd->dest_addr &= ~(0x10000 - 1);
280 gd->arch.tlb_addr = gd->dest_addr;
281 debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
282 gd->arch.tlb_addr + gd->arch.tlb_size);
288 static int reserve_lcd(void)
290 #ifdef CONFIG_FB_ADDR
291 gd->fb_base = CONFIG_FB_ADDR;
293 /* reserve memory for LCD display (always full pages) */
294 gd->dest_addr = lcd_setmem(gd->dest_addr);
295 gd->fb_base = gd->dest_addr;
296 #endif /* CONFIG_FB_ADDR */
299 #endif /* CONFIG_LCD */
301 static int reserve_uboot(void)
304 * reserve memory for U-Boot code, data & bss
305 * round down to next 4 kB limit
307 gd->dest_addr -= gd->mon_len;
308 gd->dest_addr &= ~(4096 - 1);
310 debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
315 /* reserve memory for malloc() area */
316 static int reserve_malloc(void)
318 gd->dest_addr_sp = gd->dest_addr - TOTAL_MALLOC_LEN;
319 debug("Reserving %dk for malloc() at: %08lx\n",
320 TOTAL_MALLOC_LEN >> 10, gd->dest_addr_sp);
324 /* (permanently) allocate a Board Info struct */
325 static int reserve_board(void)
327 gd->dest_addr_sp -= sizeof(bd_t);
328 gd->bd = (bd_t *)gd->dest_addr_sp;
329 memset(gd->bd, '\0', sizeof(bd_t));
330 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
331 sizeof(bd_t), gd->dest_addr_sp);
335 static int setup_machine(void)
337 #ifdef CONFIG_MACH_TYPE
338 gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
343 static int reserve_global_data(void)
345 gd->dest_addr_sp -= sizeof(gd_t);
346 gd->new_gd = (gd_t *)gd->dest_addr_sp;
347 debug("Reserving %zu Bytes for Global Data at: %08lx\n",
348 sizeof(gd_t), gd->dest_addr_sp);
352 static int reserve_fdt(void)
355 * If the device tree is sitting immediate above our image then we
356 * must relocate it. If it is embedded in the data section, then it
357 * will be relocated with other data.
360 gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
362 gd->dest_addr_sp -= gd->fdt_size;
363 gd->new_fdt = (void *)gd->dest_addr_sp;
364 debug("Reserving %lu Bytes for FDT at: %p\n",
365 gd->fdt_size, gd->new_fdt);
371 static int reserve_stacks(void)
373 /* setup stack pointer for exceptions */
374 gd->dest_addr_sp -= 16;
375 gd->dest_addr_sp &= ~0xf;
376 gd->irq_sp = gd->dest_addr_sp;
379 * Handle architecture-specific things here
380 * TODO(sjg@chromium.org): Perhaps create arch_reserve_stack()
381 * to handle this and put in arch/xxx/lib/stack.c
384 # ifdef CONFIG_USE_IRQ
385 gd->dest_addr_sp -= (CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ);
386 debug("Reserving %zu Bytes for IRQ stack at: %08lx\n",
387 CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ, gd->dest_addr_sp);
389 /* 8-byte alignment for ARM ABI compliance */
390 gd->dest_addr_sp &= ~0x07;
392 /* leave 3 words for abort-stack, plus 1 for alignment */
393 gd->dest_addr_sp -= 16;
398 static int display_new_sp(void)
400 debug("New Stack Pointer is: %08lx\n", gd->dest_addr_sp);
406 static int init_post(void)
408 post_bootmode_init();
409 post_run(NULL, POST_ROM | post_bootmode_get(0));
415 static int setup_baud_rate(void)
417 /* Ick, can we get rid of this line? */
418 gd->bd->bi_baudrate = gd->baudrate;
423 static int setup_dram_config(void)
425 /* Ram is board specific, so move it to board code ... */
426 dram_init_banksize();
431 static int reloc_fdt(void)
434 memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
435 gd->fdt_blob = gd->new_fdt;
441 static int setup_reloc(void)
443 gd->relocaddr = gd->dest_addr;
444 gd->start_addr_sp = gd->dest_addr_sp;
445 gd->reloc_off = gd->dest_addr - CONFIG_SYS_TEXT_BASE;
446 memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
448 debug("Relocation Offset is: %08lx\n", gd->reloc_off);
449 debug("Relocating to %08lx, new gd at %p, sp at %08lx\n",
450 gd->dest_addr, gd->new_gd, gd->dest_addr_sp);
455 /* ARM calls relocate_code from its crt0.S */
456 #if !defined(CONFIG_ARM)
458 static int jump_to_copy(void)
460 relocate_code(gd->dest_addr_sp, gd->new_gd, gd->dest_addr);
466 /* Record the board_init_f() bootstage (after arch_cpu_init()) */
467 static int mark_bootstage(void)
469 bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
474 static init_fnc_t init_sequence_f[] = {
477 arch_cpu_init, /* basic arch cpu dependent setup */
479 #ifdef CONFIG_OF_CONTROL
482 #if defined(CONFIG_BOARD_EARLY_INIT_F)
485 timer_init, /* initialize timer */
486 #ifdef CONFIG_BOARD_POSTCLK_INIT
489 #ifdef CONFIG_FSL_ESDHC
492 env_init, /* initialize environment */
493 init_baud_rate, /* initialze baudrate settings */
494 serial_init, /* serial communications setup */
495 console_init_f, /* stage 1 init of console */
496 display_options, /* say that we are here */
497 display_text_info, /* show debugging info if required */
498 #if defined(CONFIG_DISPLAY_CPUINFO)
499 print_cpuinfo, /* display cpu info (and speed) */
501 #if defined(CONFIG_DISPLAY_BOARDINFO)
502 checkboard, /* display board info */
505 /* TODO: unify all these dram functions? */
507 dram_init, /* configure available RAM banks */
513 * Now that we have DRAM mapped and working, we can
514 * relocate the code and continue running from DRAM.
516 * Reserve memory at end of RAM for (top down in that order):
517 * - area that won't get touched by U-Boot and Linux (optional)
518 * - kernel log buffer
522 * - board info struct
525 #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
532 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
558 void board_init_f(ulong boot_flags)
564 gd->flags = boot_flags;
566 if (initcall_run_list(init_sequence_f))
570 /* NOTREACHED - jump_to_copy() does not return */
577 puts("### ERROR ### Please RESET the board ###\n");