]> git.sur5r.net Git - u-boot/blob - arch/x86/lib/board.c
x86: Add multiboot header
[u-boot] / arch / x86 / lib / board.c
1 /*
2  * (C) Copyright 2008-2011
3  * Graeme Russ, <graeme.russ@gmail.com>
4  *
5  * (C) Copyright 2002
6  * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
7  *
8  * (C) Copyright 2002
9  * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
10  *
11  * (C) Copyright 2002
12  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
13  * Marius Groeger <mgroeger@sysgo.de>
14  *
15  * See file CREDITS for list of people who contributed to this
16  * project.
17  *
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.
22  *
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.
27  *
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,
31  * MA 02111-1307 USA
32  */
33
34 #include <common.h>
35 #include <watchdog.h>
36 #include <command.h>
37 #include <stdio_dev.h>
38 #include <version.h>
39 #include <malloc.h>
40 #include <net.h>
41 #include <ide.h>
42 #include <serial.h>
43 #include <asm/u-boot-x86.h>
44 #include <elf.h>
45
46 #ifdef CONFIG_BITBANGMII
47 #include <miiphy.h>
48 #endif
49
50 /*
51  * Pointer to initial global data area
52  *
53  * Here we initialize it.
54  */
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);
58
59 /************************************************************************
60  * Init Utilities                                                       *
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...
65  */
66 static int init_baudrate(void)
67 {
68         gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
69         return 0;
70 }
71
72 static int display_banner(void)
73 {
74
75         printf("\n\n%s\n\n", version_string);
76
77         return 0;
78 }
79
80 static int display_dram_config(void)
81 {
82         int i;
83
84         puts("DRAM Configuration:\n");
85
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");
89         }
90
91         return 0;
92 }
93
94 static void display_flash_config(ulong size)
95 {
96         puts("Flash: ");
97         print_size(size, "\n");
98 }
99
100 /*
101  * Breath some life into the board...
102  *
103  * Initialize an SMC for serial comms, and carry out some hardware
104  * tests.
105  *
106  * The first part of initialization is running from Flash memory;
107  * its main purpose is to initialize the RAM so that we
108  * can relocate the monitor code to RAM.
109  */
110
111 /*
112  * All attempts to come up with a "common" initialization sequence
113  * that works for all boards and architectures failed: some of the
114  * requirements are just _too_ different. To get rid of the resulting
115  * mess of board dependend #ifdef'ed code we now make the whole
116  * initialization sequence configurable to the user.
117  *
118  * The requirements for any new initalization function is simple: it
119  * receives a pointer to the "global data" structure as it's only
120  * argument, and returns an integer return code, where 0 means
121  * "continue" and != 0 means "fatal error, hang the system".
122  */
123 typedef int (init_fnc_t) (void);
124
125 static int calculate_relocation_address(void);
126 static int copy_uboot_to_ram(void);
127 static int clear_bss(void);
128 static int do_elf_reloc_fixups(void);
129
130 init_fnc_t *init_sequence_f[] = {
131         cpu_init_f,
132         board_early_init_f,
133         env_init,
134         init_baudrate,
135         serial_init,
136         console_init_f,
137         dram_init_f,
138         calculate_relocation_address,
139         copy_uboot_to_ram,
140         clear_bss,
141         do_elf_reloc_fixups,
142
143         NULL,
144 };
145
146 init_fnc_t *init_sequence_r[] = {
147         cpu_init_r,             /* basic cpu dependent setup */
148         board_early_init_r,     /* basic board dependent setup */
149         dram_init,              /* configure available RAM banks */
150         interrupt_init,         /* set up exceptions */
151         timer_init,
152         display_banner,
153         display_dram_config,
154
155         NULL,
156 };
157
158 gd_t *gd;
159
160 static int calculate_relocation_address(void)
161 {
162         void *text_start = &__text_start;
163         void *bss_end = &__bss_end;
164         void *dest_addr;
165         ulong rel_offset;
166
167         /* Calculate destination RAM Address and relocation offset */
168         dest_addr = (void *)gd->ram_size;
169         dest_addr -= CONFIG_SYS_STACK_SIZE;
170         dest_addr -= (bss_end - text_start);
171         rel_offset = dest_addr - text_start;
172
173         gd->start_addr_sp = gd->ram_size;
174         gd->relocaddr = (ulong)dest_addr;
175         gd->reloc_off = rel_offset;
176
177         return 0;
178 }
179
180 static int copy_uboot_to_ram(void)
181 {
182         ulong *dst_addr = (ulong *)gd->relocaddr;
183         ulong *src_addr = (ulong *)&__text_start;
184         ulong *end_addr = (ulong *)&__data_end;
185
186         while (src_addr < end_addr)
187                 *dst_addr++ = *src_addr++;
188
189         return 0;
190 }
191
192 static int clear_bss(void)
193 {
194         void *bss_start = &__bss_start;
195         void *bss_end = &__bss_end;
196
197         ulong *dst_addr = (ulong *)(bss_start + gd->reloc_off);
198         ulong *end_addr = (ulong *)(bss_end + gd->reloc_off);
199
200         while (dst_addr < end_addr)
201                 *dst_addr++ = 0x00000000;
202
203         return 0;
204 }
205
206 static int do_elf_reloc_fixups(void)
207 {
208         Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start);
209         Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end);
210
211         Elf32_Addr *offset_ptr_rom;
212         Elf32_Addr *offset_ptr_ram;
213
214         do {
215                 /* Get the location from the relocation entry */
216                 offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
217
218                 /* Check that the location of the relocation is in .text */
219                 if (offset_ptr_rom >= (Elf32_Addr *)CONFIG_SYS_TEXT_BASE) {
220
221                         /* Switch to the in-RAM version */
222                         offset_ptr_ram = offset_ptr_rom + gd->reloc_off;
223
224                         /* Check that the target points into .text */
225                         if (*offset_ptr_ram >= CONFIG_SYS_TEXT_BASE)
226                                 *offset_ptr_ram += gd->reloc_off;
227                 }
228         } while (re_src++ < re_end);
229
230         return 0;
231 }
232
233 /* Load U-Boot into RAM, initialize BSS, perform relocation adjustments */
234 void board_init_f(ulong boot_flags)
235 {
236         init_fnc_t **init_fnc_ptr;
237
238         gd->flags = boot_flags;
239
240         for (init_fnc_ptr = init_sequence_f; *init_fnc_ptr; ++init_fnc_ptr) {
241                 if ((*init_fnc_ptr)() != 0)
242                         hang();
243         }
244
245         gd->flags |= GD_FLG_RELOC;
246
247         /* Enter the relocated U-Boot! */
248         relocate_code(gd->start_addr_sp, gd, gd->relocaddr);
249
250         /* NOTREACHED - relocate_code() does not return */
251         while (1)
252                 ;
253 }
254
255 void board_init_r(gd_t *id, ulong dest_addr)
256 {
257         char *s;
258         ulong size;
259         static bd_t bd_data;
260         static gd_t gd_data;
261         init_fnc_t **init_fnc_ptr;
262
263         show_boot_progress(0x21);
264
265         /* Global data pointer is now writable */
266         gd = &gd_data;
267         memcpy(gd, id, sizeof(gd_t));
268
269         /* compiler optimization barrier needed for GCC >= 3.4 */
270         __asm__ __volatile__("" : : : "memory");
271
272         gd->bd = &bd_data;
273         memset(gd->bd, 0, sizeof(bd_t));
274         show_boot_progress(0x22);
275
276         gd->baudrate =  CONFIG_BAUDRATE;
277
278         mem_malloc_init((((ulong)dest_addr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
279                         CONFIG_SYS_MALLOC_LEN);
280
281         for (init_fnc_ptr = init_sequence_r; *init_fnc_ptr; ++init_fnc_ptr) {
282                 if ((*init_fnc_ptr)() != 0)
283                         hang();
284         }
285         show_boot_progress(0x23);
286
287 #ifdef CONFIG_SERIAL_MULTI
288         serial_initialize();
289 #endif
290         /* configure available FLASH banks */
291         size = flash_init();
292         display_flash_config(size);
293         show_boot_progress(0x24);
294
295         show_boot_progress(0x25);
296
297         /* initialize environment */
298         env_relocate();
299         show_boot_progress(0x26);
300
301
302 #ifdef CONFIG_CMD_NET
303         /* IP Address */
304         bd_data.bi_ip_addr = getenv_IPaddr("ipaddr");
305 #endif
306
307 #if defined(CONFIG_PCI)
308         /*
309          * Do pci configuration
310          */
311         pci_init();
312 #endif
313
314         show_boot_progress(0x27);
315
316
317         stdio_init();
318
319         jumptable_init();
320
321         /* Initialize the console (after the relocation and devices init) */
322         console_init_r();
323
324 #ifdef CONFIG_MISC_INIT_R
325         /* miscellaneous platform dependent initialisations */
326         misc_init_r();
327 #endif
328
329 #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
330         WATCHDOG_RESET();
331         puts("PCMCIA:");
332         pcmcia_init();
333 #endif
334
335 #if defined(CONFIG_CMD_KGDB)
336         WATCHDOG_RESET();
337         puts("KGDB:  ");
338         kgdb_init();
339 #endif
340
341         /* enable exceptions */
342         enable_interrupts();
343         show_boot_progress(0x28);
344
345 #ifdef CONFIG_STATUS_LED
346         status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
347 #endif
348
349         udelay(20);
350
351         /* Initialize from environment */
352         load_addr = getenv_ulong("loadaddr", 16, load_addr);
353 #if defined(CONFIG_CMD_NET)
354         s = getenv("bootfile");
355
356         if (s != NULL)
357                 copy_filename(BootFile, s, sizeof(BootFile));
358 #endif
359
360         WATCHDOG_RESET();
361
362 #if defined(CONFIG_CMD_IDE)
363         WATCHDOG_RESET();
364         puts("IDE:   ");
365         ide_init();
366 #endif
367
368 #if defined(CONFIG_CMD_SCSI)
369         WATCHDOG_RESET();
370         puts("SCSI:  ");
371         scsi_init();
372 #endif
373
374 #if defined(CONFIG_CMD_DOC)
375         WATCHDOG_RESET();
376         puts("DOC:   ");
377         doc_init();
378 #endif
379
380 #ifdef CONFIG_BITBANGMII
381         bb_miiphy_init();
382 #endif
383 #if defined(CONFIG_CMD_NET)
384         WATCHDOG_RESET();
385         puts("Net:   ");
386         eth_initialize(gd->bd);
387 #endif
388
389 #if (defined(CONFIG_CMD_NET)) && (0)
390         WATCHDOG_RESET();
391 # ifdef DEBUG
392         puts("Reset Ethernet PHY\n");
393 # endif
394         reset_phy();
395 #endif
396
397 #ifdef CONFIG_LAST_STAGE_INIT
398         WATCHDOG_RESET();
399         /*
400          * Some parts can be only initialized if all others (like
401          * Interrupts) are up and running (i.e. the PC-style ISA
402          * keyboard).
403          */
404         last_stage_init();
405 #endif
406
407
408 #ifdef CONFIG_POST
409         post_run(NULL, POST_RAM | post_bootmode_get(0));
410 #endif
411
412         show_boot_progress(0x29);
413
414         /* main_loop() can return to retry autoboot, if so just run it again. */
415         for (;;)
416                 main_loop();
417
418         /* NOTREACHED - no way out of command loop except booting */
419 }
420
421 void hang(void)
422 {
423         puts("### ERROR ### Please RESET the board ###\n");
424         for (;;)
425                 ;
426 }
427
428 unsigned long do_go_exec(ulong (*entry)(int, char * const []),
429                          int argc, char * const argv[])
430 {
431         unsigned long ret = 0;
432         char **argv_tmp;
433
434         /*
435          * x86 does not use a dedicated register to pass the pointer to
436          * the global_data, so it is instead passed as argv[-1]. By using
437          * argv[-1], the called 'Application' can use the contents of
438          * argv natively. However, to safely use argv[-1] a new copy of
439          * argv is needed with the extra element
440          */
441         argv_tmp = malloc(sizeof(char *) * (argc + 1));
442
443         if (argv_tmp) {
444                 argv_tmp[0] = (char *)gd;
445
446                 memcpy(&argv_tmp[1], argv, (size_t)(sizeof(char *) * argc));
447
448                 ret = (entry) (argc, &argv_tmp[1]);
449                 free(argv_tmp);
450         }
451
452         return ret;
453 }
454
455 void setup_pcat_compatibility(void)
456         __attribute__((weak, alias("__setup_pcat_compatibility")));
457
458 void __setup_pcat_compatibility(void)
459 {
460 }