]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/x86/lib/board.c
244a02163ce544eb5db246e0af2d217b2f54e43b
[karo-tx-uboot.git] / 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 #ifndef CONFIG_SYS_NO_FLASH
95 static void display_flash_config(ulong size)
96 {
97         puts("Flash: ");
98         print_size(size, "\n");
99 }
100 #endif
101
102 /*
103  * Breath some life into the board...
104  *
105  * Initialize an SMC for serial comms, and carry out some hardware
106  * tests.
107  *
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.
111  */
112
113 /*
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.
119  *
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".
124  */
125 typedef int (init_fnc_t) (void);
126
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);
131
132 init_fnc_t *init_sequence_f[] = {
133         cpu_init_f,
134         board_early_init_f,
135         env_init,
136         init_baudrate,
137         serial_init,
138         console_init_f,
139         dram_init_f,
140         calculate_relocation_address,
141         copy_uboot_to_ram,
142         clear_bss,
143         do_elf_reloc_fixups,
144
145         NULL,
146 };
147
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 */
153         timer_init,
154         display_banner,
155         display_dram_config,
156
157         NULL,
158 };
159
160 gd_t *gd;
161
162 static int calculate_relocation_address(void)
163 {
164         ulong text_start = (ulong)&__text_start;
165         ulong bss_end = (ulong)&__bss_end;
166         ulong dest_addr;
167         ulong rel_offset;
168
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);
173
174         /*
175          * Round destination address down to 16-byte boundary to keep
176          * IDT and GDT 16-byte aligned
177          */
178         dest_addr &= ~15;
179
180         rel_offset = dest_addr - text_start;
181
182         gd->start_addr_sp = gd->ram_size;
183         gd->relocaddr = dest_addr;
184         gd->reloc_off = rel_offset;
185
186         return 0;
187 }
188
189 static int copy_uboot_to_ram(void)
190 {
191         ulong *dst_addr = (ulong *)gd->relocaddr;
192         ulong *src_addr = (ulong *)&__text_start;
193         ulong *end_addr = (ulong *)&__data_end;
194
195         while (src_addr < end_addr)
196                 *dst_addr++ = *src_addr++;
197
198         return 0;
199 }
200
201 static int clear_bss(void)
202 {
203         void *bss_start = &__bss_start;
204         void *bss_end = &__bss_end;
205
206         ulong *dst_addr = (ulong *)(bss_start + gd->reloc_off);
207         ulong *end_addr = (ulong *)(bss_end + gd->reloc_off);
208
209         while (dst_addr < end_addr)
210                 *dst_addr++ = 0x00000000;
211
212         return 0;
213 }
214
215 static int do_elf_reloc_fixups(void)
216 {
217         Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start);
218         Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end);
219
220         Elf32_Addr *offset_ptr_rom;
221         Elf32_Addr *offset_ptr_ram;
222
223         do {
224                 /* Get the location from the relocation entry */
225                 offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
226
227                 /* Check that the location of the relocation is in .text */
228                 if (offset_ptr_rom >= (Elf32_Addr *)CONFIG_SYS_TEXT_BASE) {
229
230                         /* Switch to the in-RAM version */
231                         offset_ptr_ram = offset_ptr_rom + gd->reloc_off;
232
233                         /* Check that the target points into .text */
234                         if (*offset_ptr_ram >= CONFIG_SYS_TEXT_BASE)
235                                 *offset_ptr_ram += gd->reloc_off;
236                 }
237         } while (re_src++ < re_end);
238
239         return 0;
240 }
241
242 /* Load U-Boot into RAM, initialize BSS, perform relocation adjustments */
243 void board_init_f(ulong boot_flags)
244 {
245         init_fnc_t **init_fnc_ptr;
246
247         gd->flags = boot_flags;
248
249         for (init_fnc_ptr = init_sequence_f; *init_fnc_ptr; ++init_fnc_ptr) {
250                 if ((*init_fnc_ptr)() != 0)
251                         hang();
252         }
253
254         gd->flags |= GD_FLG_RELOC;
255
256         /* Enter the relocated U-Boot! */
257         relocate_code(gd->start_addr_sp, gd, gd->relocaddr);
258
259         /* NOTREACHED - relocate_code() does not return */
260         while (1)
261                 ;
262 }
263
264 void board_init_r(gd_t *id, ulong dest_addr)
265 {
266 #if defined(CONFIG_CMD_NET)
267         char *s;
268 #endif
269 #ifndef CONFIG_SYS_NO_FLASH
270         ulong size;
271 #endif
272         static bd_t bd_data;
273         static gd_t gd_data;
274         init_fnc_t **init_fnc_ptr;
275
276         show_boot_progress(0x21);
277
278         /* Global data pointer is now writable */
279         gd = &gd_data;
280         memcpy(gd, id, sizeof(gd_t));
281
282         /* compiler optimization barrier needed for GCC >= 3.4 */
283         __asm__ __volatile__("" : : : "memory");
284
285         gd->bd = &bd_data;
286         memset(gd->bd, 0, sizeof(bd_t));
287         show_boot_progress(0x22);
288
289         gd->baudrate =  CONFIG_BAUDRATE;
290
291         mem_malloc_init((((ulong)dest_addr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
292                         CONFIG_SYS_MALLOC_LEN);
293
294         for (init_fnc_ptr = init_sequence_r; *init_fnc_ptr; ++init_fnc_ptr) {
295                 if ((*init_fnc_ptr)() != 0)
296                         hang();
297         }
298         show_boot_progress(0x23);
299
300 #ifdef CONFIG_SERIAL_MULTI
301         serial_initialize();
302 #endif
303
304 #ifndef CONFIG_SYS_NO_FLASH
305         /* configure available FLASH banks */
306         size = flash_init();
307         display_flash_config(size);
308         show_boot_progress(0x24);
309 #endif
310
311         show_boot_progress(0x25);
312
313         /* initialize environment */
314         env_relocate();
315         show_boot_progress(0x26);
316
317
318 #ifdef CONFIG_CMD_NET
319         /* IP Address */
320         bd_data.bi_ip_addr = getenv_IPaddr("ipaddr");
321 #endif
322
323 #if defined(CONFIG_PCI)
324         /*
325          * Do pci configuration
326          */
327         pci_init();
328 #endif
329
330         show_boot_progress(0x27);
331
332
333         stdio_init();
334
335         jumptable_init();
336
337         /* Initialize the console (after the relocation and devices init) */
338         console_init_r();
339
340 #ifdef CONFIG_MISC_INIT_R
341         /* miscellaneous platform dependent initialisations */
342         misc_init_r();
343 #endif
344
345 #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
346         WATCHDOG_RESET();
347         puts("PCMCIA:");
348         pcmcia_init();
349 #endif
350
351 #if defined(CONFIG_CMD_KGDB)
352         WATCHDOG_RESET();
353         puts("KGDB:  ");
354         kgdb_init();
355 #endif
356
357         /* enable exceptions */
358         enable_interrupts();
359         show_boot_progress(0x28);
360
361 #ifdef CONFIG_STATUS_LED
362         status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
363 #endif
364
365         udelay(20);
366
367         /* Initialize from environment */
368         load_addr = getenv_ulong("loadaddr", 16, load_addr);
369 #if defined(CONFIG_CMD_NET)
370         s = getenv("bootfile");
371
372         if (s != NULL)
373                 copy_filename(BootFile, s, sizeof(BootFile));
374 #endif
375
376         WATCHDOG_RESET();
377
378 #if defined(CONFIG_CMD_IDE)
379         WATCHDOG_RESET();
380         puts("IDE:   ");
381         ide_init();
382 #endif
383
384 #if defined(CONFIG_CMD_SCSI)
385         WATCHDOG_RESET();
386         puts("SCSI:  ");
387         scsi_init();
388 #endif
389
390 #if defined(CONFIG_CMD_DOC)
391         WATCHDOG_RESET();
392         puts("DOC:   ");
393         doc_init();
394 #endif
395
396 #ifdef CONFIG_BITBANGMII
397         bb_miiphy_init();
398 #endif
399 #if defined(CONFIG_CMD_NET)
400         WATCHDOG_RESET();
401         puts("Net:   ");
402         eth_initialize(gd->bd);
403 #endif
404
405 #if (defined(CONFIG_CMD_NET)) && (0)
406         WATCHDOG_RESET();
407 # ifdef DEBUG
408         puts("Reset Ethernet PHY\n");
409 # endif
410         reset_phy();
411 #endif
412
413 #ifdef CONFIG_LAST_STAGE_INIT
414         WATCHDOG_RESET();
415         /*
416          * Some parts can be only initialized if all others (like
417          * Interrupts) are up and running (i.e. the PC-style ISA
418          * keyboard).
419          */
420         last_stage_init();
421 #endif
422
423
424 #ifdef CONFIG_POST
425         post_run(NULL, POST_RAM | post_bootmode_get(0));
426 #endif
427
428         show_boot_progress(0x29);
429
430         /* main_loop() can return to retry autoboot, if so just run it again. */
431         for (;;)
432                 main_loop();
433
434         /* NOTREACHED - no way out of command loop except booting */
435 }
436
437 void hang(void)
438 {
439         puts("### ERROR ### Please RESET the board ###\n");
440         for (;;)
441                 ;
442 }
443
444 unsigned long do_go_exec(ulong (*entry)(int, char * const []),
445                          int argc, char * const argv[])
446 {
447         unsigned long ret = 0;
448         char **argv_tmp;
449
450         /*
451          * x86 does not use a dedicated register to pass the pointer to
452          * the global_data, so it is instead passed as argv[-1]. By using
453          * argv[-1], the called 'Application' can use the contents of
454          * argv natively. However, to safely use argv[-1] a new copy of
455          * argv is needed with the extra element
456          */
457         argv_tmp = malloc(sizeof(char *) * (argc + 1));
458
459         if (argv_tmp) {
460                 argv_tmp[0] = (char *)gd;
461
462                 memcpy(&argv_tmp[1], argv, (size_t)(sizeof(char *) * argc));
463
464                 ret = (entry) (argc, &argv_tmp[1]);
465                 free(argv_tmp);
466         }
467
468         return ret;
469 }
470
471 void setup_pcat_compatibility(void)
472         __attribute__((weak, alias("__setup_pcat_compatibility")));
473
474 void __setup_pcat_compatibility(void)
475 {
476 }