2 * (C) Copyright 2008-2011
3 * Graeme Russ, <graeme.russ@gmail.com>
6 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
9 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
12 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
13 * Marius Groeger <mgroeger@sysgo.de>
15 * See file CREDITS for list of people who contributed to this
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License as
20 * published by the Free Software Foundation; either version 2 of
21 * the License, or (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 #include <stdio_dev.h>
43 #include <asm/u-boot-x86.h>
45 #include <asm/processor.h>
47 #ifdef CONFIG_BITBANGMII
51 /************************************************************************
53 ************************************************************************
54 * Some of this code should be moved into the core functions,
55 * or dropped completely,
56 * but let's get it working (again) first...
58 static int init_baudrate(void)
60 gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
64 static int display_banner(void)
67 printf("\n\n%s\n\n", version_string);
72 static int display_dram_config(void)
76 puts("DRAM Configuration:\n");
78 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
79 printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
80 print_size(gd->bd->bi_dram[i].size, "\n");
86 #ifndef CONFIG_SYS_NO_FLASH
87 static void display_flash_config(ulong size)
90 print_size(size, "\n");
95 * Breath some life into the board...
97 * Initialize an SMC for serial comms, and carry out some hardware
100 * The first part of initialization is running from Flash memory;
101 * its main purpose is to initialize the RAM so that we
102 * can relocate the monitor code to RAM.
106 * All attempts to come up with a "common" initialization sequence
107 * that works for all boards and architectures failed: some of the
108 * requirements are just _too_ different. To get rid of the resulting
109 * mess of board dependend #ifdef'ed code we now make the whole
110 * initialization sequence configurable to the user.
112 * The requirements for any new initalization function is simple: it
113 * receives a pointer to the "global data" structure as it's only
114 * argument, and returns an integer return code, where 0 means
115 * "continue" and != 0 means "fatal error, hang the system".
117 typedef int (init_fnc_t) (void);
119 static int calculate_relocation_address(void);
120 static int copy_uboot_to_ram(void);
121 static int clear_bss(void);
122 static int do_elf_reloc_fixups(void);
123 static int copy_gd_to_ram(void);
125 init_fnc_t *init_sequence_f[] = {
133 calculate_relocation_address,
138 init_fnc_t *init_sequence_r[] = {
139 cpu_init_r, /* basic cpu dependent setup */
140 board_early_init_r, /* basic board dependent setup */
141 dram_init, /* configure available RAM banks */
142 interrupt_init, /* set up exceptions */
150 static int calculate_relocation_address(void)
152 ulong text_start = (ulong)&__text_start;
153 ulong bss_end = (ulong)&__bss_end;
157 * NOTE: All destination address are rounded down to 16-byte
158 * boundary to satisfy various worst-case alignment
162 /* Global Data is at top of available memory */
163 dest_addr = gd->ram_size;
164 dest_addr -= GENERATED_GBL_DATA_SIZE;
166 gd->new_gd_addr = dest_addr;
168 /* GDT is below Global Data */
169 dest_addr -= X86_GDT_SIZE;
171 gd->gdt_addr = dest_addr;
173 /* Stack is below GDT */
174 gd->start_addr_sp = dest_addr;
176 /* U-Boot is below the stack */
177 dest_addr -= CONFIG_SYS_STACK_SIZE;
178 dest_addr -= (bss_end - text_start);
180 gd->relocaddr = dest_addr;
181 gd->reloc_off = (dest_addr - text_start);
186 static int copy_uboot_to_ram(void)
188 size_t len = (size_t)&__data_end - (size_t)&__text_start;
190 memcpy((void *)gd->relocaddr, (void *)&__text_start, len);
195 static int clear_bss(void)
197 ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
198 size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
200 memset((void *)dst_addr, 0x00, len);
205 static int do_elf_reloc_fixups(void)
207 Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start);
208 Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end);
210 Elf32_Addr *offset_ptr_rom;
211 Elf32_Addr *offset_ptr_ram;
213 /* The size of the region of u-boot that runs out of RAM. */
214 uintptr_t size = (uintptr_t)&__bss_end - (uintptr_t)&__text_start;
217 /* Get the location from the relocation entry */
218 offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
220 /* Check that the location of the relocation is in .text */
221 if (offset_ptr_rom >= (Elf32_Addr *)CONFIG_SYS_TEXT_BASE) {
223 /* Switch to the in-RAM version */
224 offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
227 /* Check that the target points into .text */
228 if (*offset_ptr_ram >= CONFIG_SYS_TEXT_BASE &&
230 (CONFIG_SYS_TEXT_BASE + size)) {
231 *offset_ptr_ram += gd->reloc_off;
234 } while (re_src++ < re_end);
239 /* Load U-Boot into RAM, initialize BSS, perform relocation adjustments */
240 void board_init_f(ulong boot_flags)
242 init_fnc_t **init_fnc_ptr;
244 gd->flags = boot_flags;
246 for (init_fnc_ptr = init_sequence_f; *init_fnc_ptr; ++init_fnc_ptr) {
247 if ((*init_fnc_ptr)() != 0)
252 * SDRAM is now initialised, U-Boot has been copied into SDRAM,
253 * the BSS has been cleared etc. The final stack can now be setup
254 * in SDRAM. Code execution will continue (momentarily) in Flash,
255 * but with the stack in SDRAM and Global Data in temporary memory
258 board_init_f_r_trampoline(gd->start_addr_sp);
260 /* NOTREACHED - board_init_f_r_trampoline() does not return */
265 void board_init_f_r(void)
267 if (copy_gd_to_ram() != 0)
270 if (init_cache() != 0)
275 do_elf_reloc_fixups();
278 * Transfer execution from Flash to RAM by calculating the address
279 * of the in-RAM copy of board_init_r() and calling it
281 (board_init_r + gd->reloc_off)(gd, gd->relocaddr);
283 /* NOTREACHED - board_init_r() does not return */
288 static int copy_gd_to_ram(void)
293 * Global data is still in temporary memory (the CPU cache).
294 * calculate_relocation_address() has set gd->new_gd_addr to
295 * where the global data lives in RAM but getting it there
296 * safely is a bit tricky due to the 'F-Segment Hack' that
297 * we need to use for x86
299 ram_gd = (gd_t *)gd->new_gd_addr;
300 memcpy((void *)ram_gd, gd, sizeof(gd_t));
303 * Reload the Global Descriptor Table so FS points to the
304 * in-RAM copy of Global Data (calculate_relocation_address()
305 * has already calculated the in-RAM location of the GDT)
307 ram_gd->gd_addr = (ulong)ram_gd;
308 init_gd(ram_gd, (u64 *)gd->gdt_addr);
313 void board_init_r(gd_t *id, ulong dest_addr)
315 #if defined(CONFIG_CMD_NET)
318 #ifndef CONFIG_SYS_NO_FLASH
322 init_fnc_t **init_fnc_ptr;
324 show_boot_progress(0x21);
326 /* compiler optimization barrier needed for GCC >= 3.4 */
327 __asm__ __volatile__("" : : : "memory");
329 gd->flags |= GD_FLG_RELOC;
332 memset(gd->bd, 0, sizeof(bd_t));
333 show_boot_progress(0x22);
335 gd->baudrate = CONFIG_BAUDRATE;
337 mem_malloc_init((((ulong)dest_addr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
338 CONFIG_SYS_MALLOC_LEN);
340 for (init_fnc_ptr = init_sequence_r; *init_fnc_ptr; ++init_fnc_ptr) {
341 if ((*init_fnc_ptr)() != 0)
344 show_boot_progress(0x23);
346 #ifdef CONFIG_SERIAL_MULTI
350 #ifndef CONFIG_SYS_NO_FLASH
351 /* configure available FLASH banks */
353 display_flash_config(size);
354 show_boot_progress(0x24);
357 show_boot_progress(0x25);
359 /* initialize environment */
361 show_boot_progress(0x26);
364 #ifdef CONFIG_CMD_NET
366 bd_data.bi_ip_addr = getenv_IPaddr("ipaddr");
369 #if defined(CONFIG_PCI)
371 * Do pci configuration
376 show_boot_progress(0x27);
383 /* Initialize the console (after the relocation and devices init) */
386 #ifdef CONFIG_MISC_INIT_R
387 /* miscellaneous platform dependent initialisations */
391 #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
397 #if defined(CONFIG_CMD_KGDB)
403 /* enable exceptions */
405 show_boot_progress(0x28);
407 #ifdef CONFIG_STATUS_LED
408 status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
413 /* Initialize from environment */
414 load_addr = getenv_ulong("loadaddr", 16, load_addr);
415 #if defined(CONFIG_CMD_NET)
416 s = getenv("bootfile");
419 copy_filename(BootFile, s, sizeof(BootFile));
424 #if defined(CONFIG_CMD_IDE)
430 #if defined(CONFIG_CMD_SCSI)
436 #if defined(CONFIG_CMD_DOC)
442 #ifdef CONFIG_BITBANGMII
445 #if defined(CONFIG_CMD_NET)
448 eth_initialize(gd->bd);
451 #if (defined(CONFIG_CMD_NET)) && (0)
454 puts("Reset Ethernet PHY\n");
459 #ifdef CONFIG_LAST_STAGE_INIT
462 * Some parts can be only initialized if all others (like
463 * Interrupts) are up and running (i.e. the PC-style ISA
471 post_run(NULL, POST_RAM | post_bootmode_get(0));
474 show_boot_progress(0x29);
476 /* main_loop() can return to retry autoboot, if so just run it again. */
480 /* NOTREACHED - no way out of command loop except booting */
485 puts("### ERROR ### Please RESET the board ###\n");