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>
46 #ifdef CONFIG_BITBANGMII
51 * Pointer to initial global data area
53 * Here we initialize it.
55 #undef XTRN_DECLARE_GLOBAL_DATA_PTR
56 #define XTRN_DECLARE_GLOBAL_DATA_PTR /* empty = allocate here */
57 DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
59 /************************************************************************
61 ************************************************************************
62 * Some of this code should be moved into the core functions,
63 * or dropped completely,
64 * but let's get it working (again) first...
66 static int init_baudrate(void)
68 gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
72 static int display_banner(void)
75 printf("\n\n%s\n\n", version_string);
80 static int display_dram_config(void)
84 puts("DRAM Configuration:\n");
86 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
87 printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
88 print_size(gd->bd->bi_dram[i].size, "\n");
94 #ifndef CONFIG_SYS_NO_FLASH
95 static void display_flash_config(ulong size)
98 print_size(size, "\n");
103 * Breath some life into the board...
105 * Initialize an SMC for serial comms, and carry out some hardware
108 * The first part of initialization is running from Flash memory;
109 * its main purpose is to initialize the RAM so that we
110 * can relocate the monitor code to RAM.
114 * All attempts to come up with a "common" initialization sequence
115 * that works for all boards and architectures failed: some of the
116 * requirements are just _too_ different. To get rid of the resulting
117 * mess of board dependend #ifdef'ed code we now make the whole
118 * initialization sequence configurable to the user.
120 * The requirements for any new initalization function is simple: it
121 * receives a pointer to the "global data" structure as it's only
122 * argument, and returns an integer return code, where 0 means
123 * "continue" and != 0 means "fatal error, hang the system".
125 typedef int (init_fnc_t) (void);
127 static int calculate_relocation_address(void);
128 static int copy_uboot_to_ram(void);
129 static int clear_bss(void);
130 static int do_elf_reloc_fixups(void);
132 init_fnc_t *init_sequence_f[] = {
140 calculate_relocation_address,
148 init_fnc_t *init_sequence_r[] = {
149 cpu_init_r, /* basic cpu dependent setup */
150 board_early_init_r, /* basic board dependent setup */
151 dram_init, /* configure available RAM banks */
152 interrupt_init, /* set up exceptions */
162 static int calculate_relocation_address(void)
164 ulong text_start = (ulong)&__text_start;
165 ulong bss_end = (ulong)&__bss_end;
169 /* Calculate destination RAM Address and relocation offset */
170 dest_addr = gd->ram_size;
171 dest_addr -= CONFIG_SYS_STACK_SIZE;
172 dest_addr -= (bss_end - text_start);
175 * Round destination address down to 16-byte boundary to keep
176 * IDT and GDT 16-byte aligned
180 rel_offset = dest_addr - text_start;
182 gd->start_addr_sp = gd->ram_size;
183 gd->relocaddr = dest_addr;
184 gd->reloc_off = rel_offset;
189 static int copy_uboot_to_ram(void)
191 ulong *dst_addr = (ulong *)gd->relocaddr;
192 ulong *src_addr = (ulong *)&__text_start;
193 ulong *end_addr = (ulong *)&__data_end;
195 while (src_addr < end_addr)
196 *dst_addr++ = *src_addr++;
201 static int clear_bss(void)
203 void *bss_start = &__bss_start;
204 void *bss_end = &__bss_end;
206 ulong *dst_addr = (ulong *)(bss_start + gd->reloc_off);
207 ulong *end_addr = (ulong *)(bss_end + gd->reloc_off);
209 while (dst_addr < end_addr)
210 *dst_addr++ = 0x00000000;
215 static int do_elf_reloc_fixups(void)
217 Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start);
218 Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end);
220 Elf32_Addr *offset_ptr_rom;
221 Elf32_Addr *offset_ptr_ram;
223 /* The size of the region of u-boot that runs out of RAM. */
224 uintptr_t size = (uintptr_t)&__bss_end - (uintptr_t)&__text_start;
227 /* Get the location from the relocation entry */
228 offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
230 /* Check that the location of the relocation is in .text */
231 if (offset_ptr_rom >= (Elf32_Addr *)CONFIG_SYS_TEXT_BASE) {
233 /* Switch to the in-RAM version */
234 offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
237 /* Check that the target points into .text */
238 if (*offset_ptr_ram >= CONFIG_SYS_TEXT_BASE &&
240 (CONFIG_SYS_TEXT_BASE + size)) {
241 *offset_ptr_ram += gd->reloc_off;
244 } while (re_src++ < re_end);
249 /* Load U-Boot into RAM, initialize BSS, perform relocation adjustments */
250 void board_init_f(ulong boot_flags)
252 init_fnc_t **init_fnc_ptr;
254 gd->flags = boot_flags;
256 for (init_fnc_ptr = init_sequence_f; *init_fnc_ptr; ++init_fnc_ptr) {
257 if ((*init_fnc_ptr)() != 0)
261 gd->flags |= GD_FLG_RELOC;
263 /* Enter the relocated U-Boot! */
264 relocate_code(gd->start_addr_sp, gd, gd->relocaddr);
266 /* NOTREACHED - relocate_code() does not return */
271 void board_init_r(gd_t *id, ulong dest_addr)
273 #if defined(CONFIG_CMD_NET)
276 #ifndef CONFIG_SYS_NO_FLASH
281 init_fnc_t **init_fnc_ptr;
283 show_boot_progress(0x21);
285 /* Global data pointer is now writable */
287 memcpy(gd, id, sizeof(gd_t));
289 /* compiler optimization barrier needed for GCC >= 3.4 */
290 __asm__ __volatile__("" : : : "memory");
293 memset(gd->bd, 0, sizeof(bd_t));
294 show_boot_progress(0x22);
296 gd->baudrate = CONFIG_BAUDRATE;
298 mem_malloc_init((((ulong)dest_addr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
299 CONFIG_SYS_MALLOC_LEN);
301 for (init_fnc_ptr = init_sequence_r; *init_fnc_ptr; ++init_fnc_ptr) {
302 if ((*init_fnc_ptr)() != 0)
305 show_boot_progress(0x23);
307 #ifdef CONFIG_SERIAL_MULTI
311 #ifndef CONFIG_SYS_NO_FLASH
312 /* configure available FLASH banks */
314 display_flash_config(size);
315 show_boot_progress(0x24);
318 show_boot_progress(0x25);
320 /* initialize environment */
322 show_boot_progress(0x26);
325 #ifdef CONFIG_CMD_NET
327 bd_data.bi_ip_addr = getenv_IPaddr("ipaddr");
330 #if defined(CONFIG_PCI)
332 * Do pci configuration
337 show_boot_progress(0x27);
344 /* Initialize the console (after the relocation and devices init) */
347 #ifdef CONFIG_MISC_INIT_R
348 /* miscellaneous platform dependent initialisations */
352 #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
358 #if defined(CONFIG_CMD_KGDB)
364 /* enable exceptions */
366 show_boot_progress(0x28);
368 #ifdef CONFIG_STATUS_LED
369 status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
374 /* Initialize from environment */
375 load_addr = getenv_ulong("loadaddr", 16, load_addr);
376 #if defined(CONFIG_CMD_NET)
377 s = getenv("bootfile");
380 copy_filename(BootFile, s, sizeof(BootFile));
385 #if defined(CONFIG_CMD_IDE)
391 #if defined(CONFIG_CMD_SCSI)
397 #if defined(CONFIG_CMD_DOC)
403 #ifdef CONFIG_BITBANGMII
406 #if defined(CONFIG_CMD_NET)
409 eth_initialize(gd->bd);
412 #if (defined(CONFIG_CMD_NET)) && (0)
415 puts("Reset Ethernet PHY\n");
420 #ifdef CONFIG_LAST_STAGE_INIT
423 * Some parts can be only initialized if all others (like
424 * Interrupts) are up and running (i.e. the PC-style ISA
432 post_run(NULL, POST_RAM | post_bootmode_get(0));
435 show_boot_progress(0x29);
437 /* main_loop() can return to retry autoboot, if so just run it again. */
441 /* NOTREACHED - no way out of command loop except booting */
446 puts("### ERROR ### Please RESET the board ###\n");
451 unsigned long do_go_exec(ulong (*entry)(int, char * const []),
452 int argc, char * const argv[])
454 unsigned long ret = 0;
458 * x86 does not use a dedicated register to pass the pointer to
459 * the global_data, so it is instead passed as argv[-1]. By using
460 * argv[-1], the called 'Application' can use the contents of
461 * argv natively. However, to safely use argv[-1] a new copy of
462 * argv is needed with the extra element
464 argv_tmp = malloc(sizeof(char *) * (argc + 1));
467 argv_tmp[0] = (char *)gd;
469 memcpy(&argv_tmp[1], argv, (size_t)(sizeof(char *) * argc));
471 ret = (entry) (argc, &argv_tmp[1]);
478 void setup_pcat_compatibility(void)
479 __attribute__((weak, alias("__setup_pcat_compatibility")));
481 void __setup_pcat_compatibility(void)