8ec04462b9197f0a8122c4b068a666021149032d
[pandora-u-boot.git] / arch / i386 / lib / board.c
1 /*
2  * (C) Copyright 2002
3  * Daniel Engström, Omicron Ceti AB, daniel@omicron.se
4  *
5  * (C) Copyright 2002
6  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7  *
8  * (C) Copyright 2002
9  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
10  * Marius Groeger <mgroeger@sysgo.de>
11  *
12  * See file CREDITS for list of people who contributed to this
13  * project.
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License as
17  * published by the Free Software Foundation; either version 2 of
18  * the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28  * MA 02111-1307 USA
29  */
30
31 #include <common.h>
32 #include <watchdog.h>
33 #include <command.h>
34 #include <stdio_dev.h>
35 #include <timestamp.h>
36 #include <version.h>
37 #include <malloc.h>
38 #include <net.h>
39 #include <ide.h>
40 #include <serial.h>
41 #include <asm/u-boot-i386.h>
42 #include <elf.h>
43
44 #ifdef CONFIG_BITBANGMII
45 #include <miiphy.h>
46 #endif
47
48 /*
49  * Pointer to initial global data area
50  *
51  * Here we initialize it.
52  */
53 #undef  XTRN_DECLARE_GLOBAL_DATA_PTR
54 #define XTRN_DECLARE_GLOBAL_DATA_PTR    /* empty = allocate here */
55 DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
56
57
58 /* Exports from the Linker Script */
59 extern ulong __text_start;
60 extern ulong __data_end;
61 extern ulong __rel_dyn_start;
62 extern ulong __rel_dyn_end;
63 extern ulong __bss_start;
64 extern ulong __bss_end;
65
66 const char version_string[] =
67         U_BOOT_VERSION" (" U_BOOT_DATE " - " U_BOOT_TIME ")";
68
69 /************************************************************************
70  * Init Utilities                                                       *
71  ************************************************************************
72  * Some of this code should be moved into the core functions,
73  * or dropped completely,
74  * but let's get it working (again) first...
75  */
76 static int init_baudrate (void)
77 {
78         char tmp[64];   /* long enough for environment variables */
79         int i = getenv_f("baudrate", tmp, 64);
80
81         gd->baudrate = (i != 0)
82                         ? (int) simple_strtoul (tmp, NULL, 10)
83                         : CONFIG_BAUDRATE;
84
85         return (0);
86 }
87
88 static int display_banner (void)
89 {
90
91         printf ("\n\n%s\n\n", version_string);
92 /*
93         printf ("U-Boot code: %08lX -> %08lX  data: %08lX -> %08lX\n"
94                 "        BSS: %08lX -> %08lX stack: %08lX -> %08lX\n",
95                 i386boot_start, i386boot_romdata_start-1,
96                 i386boot_romdata_dest, i386boot_romdata_dest+i386boot_romdata_size-1,
97                 i386boot_bss_start, i386boot_bss_start+i386boot_bss_size-1,
98                 i386boot_bss_start+i386boot_bss_size,
99                 i386boot_bss_start+i386boot_bss_size+CONFIG_SYS_STACK_SIZE-1);
100
101 */
102
103         return (0);
104 }
105
106 /*
107  * WARNING: this code looks "cleaner" than the PowerPC version, but
108  * has the disadvantage that you either get nothing, or everything.
109  * On PowerPC, you might see "DRAM: " before the system hangs - which
110  * gives a simple yet clear indication which part of the
111  * initialization if failing.
112  */
113 static int display_dram_config (void)
114 {
115         int i;
116
117         puts ("DRAM Configuration:\n");
118
119         for (i=0; i<CONFIG_NR_DRAM_BANKS; i++) {
120                 printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
121                 print_size (gd->bd->bi_dram[i].size, "\n");
122         }
123
124         return (0);
125 }
126
127 static void display_flash_config (ulong size)
128 {
129         puts ("Flash: ");
130         print_size (size, "\n");
131 }
132
133 /*
134  * Breath some life into the board...
135  *
136  * Initialize an SMC for serial comms, and carry out some hardware
137  * tests.
138  *
139  * The first part of initialization is running from Flash memory;
140  * its main purpose is to initialize the RAM so that we
141  * can relocate the monitor code to RAM.
142  */
143
144
145 /*
146  * All attempts to come up with a "common" initialization sequence
147  * that works for all boards and architectures failed: some of the
148  * requirements are just _too_ different. To get rid of the resulting
149  * mess of board dependend #ifdef'ed code we now make the whole
150  * initialization sequence configurable to the user.
151  *
152  * The requirements for any new initalization function is simple: it
153  * receives a pointer to the "global data" structure as it's only
154  * argument, and returns an integer return code, where 0 means
155  * "continue" and != 0 means "fatal error, hang the system".
156  */
157 typedef int (init_fnc_t) (void);
158
159 init_fnc_t *init_sequence[] = {
160         cpu_init_r,             /* basic cpu dependent setup */
161         board_early_init_r,     /* basic board dependent setup */
162         dram_init,              /* configure available RAM banks */
163         interrupt_init,         /* set up exceptions */
164         timer_init,
165         display_banner,
166         display_dram_config,
167
168         NULL,
169 };
170
171 gd_t *gd;
172
173 /*
174  * Load U-Boot into RAM, initialize BSS, perform relocation adjustments
175  */
176 void board_init_f(ulong boot_flags)
177 {
178         void *text_start = &__text_start;
179         void *data_end = &__data_end;
180         void *rel_dyn_start = &__rel_dyn_start;
181         void *rel_dyn_end = &__rel_dyn_end;
182         void *bss_start = &__bss_start;
183         void *bss_end = &__bss_end;
184
185         ulong *dst_addr;
186         ulong *src_addr;
187         ulong *end_addr;
188
189         void *addr_sp;
190         void *dest_addr;
191         ulong rel_offset;
192         Elf32_Rel *re_src;
193         Elf32_Rel *re_end;
194
195         gd->flags = boot_flags;
196
197         if (env_init() != 0)
198                 hang();
199
200         if (init_baudrate() != 0)
201                 hang();
202
203         if (serial_init() != 0)
204                 hang();
205
206         if (console_init_f() != 0)
207                 hang();
208
209         if (dram_init_f() != 0)
210                 hang();
211
212         /* Calculate destination RAM Address and relocation offset */
213         dest_addr = (void *)gd->ram_size;
214         addr_sp = dest_addr;
215         dest_addr -= CONFIG_SYS_STACK_SIZE;
216         dest_addr -= (bss_end - text_start);
217         rel_offset = dest_addr - text_start;
218
219         /* First stage CPU initialization */
220         if (cpu_init_f() != 0)
221                 hang();
222
223         /* First stage Board initialization */
224         if (board_early_init_f() != 0)
225                 hang();
226
227         /* Copy U-Boot into RAM */
228         dst_addr = (ulong *)dest_addr;
229         src_addr = (ulong *)(text_start + gd->load_off);
230         end_addr = (ulong *)(data_end  + gd->load_off);
231
232         while (src_addr < end_addr)
233                 *dst_addr++ = *src_addr++;
234
235         /* Clear BSS */
236         dst_addr = (ulong *)(bss_start + rel_offset);
237         end_addr = (ulong *)(bss_end + rel_offset);
238
239         while (dst_addr < end_addr)
240                 *dst_addr++ = 0x00000000;
241
242         /* Perform relocation adjustments */
243         re_src = (Elf32_Rel *)(rel_dyn_start + gd->load_off);
244         re_end = (Elf32_Rel *)(rel_dyn_end + gd->load_off);
245
246         do {
247                 if (re_src->r_offset >= CONFIG_SYS_TEXT_BASE)
248                         if (*(Elf32_Addr *)(re_src->r_offset + rel_offset) >= CONFIG_SYS_TEXT_BASE)
249                                 *(Elf32_Addr *)(re_src->r_offset + rel_offset) += rel_offset;
250         } while (re_src++ < re_end);
251
252         gd->reloc_off = rel_offset;
253         gd->flags |= GD_FLG_RELOC;
254
255         /* Enter the relocated U-Boot! */
256         relocate_code((ulong)addr_sp, gd, (ulong)dest_addr);
257
258         /* NOTREACHED - relocate_code() does not return */
259         while(1);
260 }
261
262 void board_init_r(gd_t *id, ulong dest_addr)
263 {
264         char *s;
265         int i;
266         ulong size;
267         static bd_t bd_data;
268         static gd_t gd_data;
269         init_fnc_t **init_fnc_ptr;
270
271         show_boot_progress(0x21);
272
273         /* Global data pointer is now writable */
274         gd = &gd_data;
275         memcpy(gd, id, sizeof(gd_t));
276
277         /* compiler optimization barrier needed for GCC >= 3.4 */
278         __asm__ __volatile__("": : :"memory");
279
280         gd->bd = &bd_data;
281         memset (gd->bd, 0, sizeof (bd_t));
282         show_boot_progress(0x22);
283
284         gd->baudrate =  CONFIG_BAUDRATE;
285
286         mem_malloc_init((((ulong)dest_addr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
287                         CONFIG_SYS_MALLOC_LEN);
288
289         for (init_fnc_ptr = init_sequence, i=0; *init_fnc_ptr; ++init_fnc_ptr, i++) {
290                 show_boot_progress(0xa130|i);
291
292                 if ((*init_fnc_ptr)() != 0) {
293                         hang ();
294                 }
295         }
296         show_boot_progress(0x23);
297
298 #ifdef CONFIG_SERIAL_MULTI
299         serial_initialize();
300 #endif
301         /* configure available FLASH banks */
302         size = flash_init();
303         display_flash_config(size);
304         show_boot_progress(0x24);
305
306         show_boot_progress(0x25);
307
308         /* initialize environment */
309         env_relocate ();
310         show_boot_progress(0x26);
311
312
313 #ifdef CONFIG_CMD_NET
314         /* IP Address */
315         bd_data.bi_ip_addr = getenv_IPaddr ("ipaddr");
316 #endif
317
318 #if defined(CONFIG_PCI)
319         /*
320          * Do pci configuration
321          */
322         pci_init();
323 #endif
324
325         show_boot_progress(0x27);
326
327
328         stdio_init ();
329
330         jumptable_init ();
331
332         /* Initialize the console (after the relocation and devices init) */
333         console_init_r();
334
335 #ifdef CONFIG_MISC_INIT_R
336         /* miscellaneous platform dependent initialisations */
337         misc_init_r();
338 #endif
339
340 #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
341         WATCHDOG_RESET();
342         puts ("PCMCIA:");
343         pcmcia_init();
344 #endif
345
346 #if defined(CONFIG_CMD_KGDB)
347         WATCHDOG_RESET();
348         puts("KGDB:  ");
349         kgdb_init();
350 #endif
351
352         /* enable exceptions */
353         enable_interrupts();
354         show_boot_progress(0x28);
355
356 #ifdef CONFIG_STATUS_LED
357         status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
358 #endif
359
360         udelay(20);
361
362         set_timer (0);
363
364         /* Initialize from environment */
365         if ((s = getenv ("loadaddr")) != NULL) {
366                 load_addr = simple_strtoul (s, NULL, 16);
367         }
368 #if defined(CONFIG_CMD_NET)
369         if ((s = getenv ("bootfile")) != NULL) {
370                 copy_filename (BootFile, s, sizeof (BootFile));
371         }
372 #endif
373
374         WATCHDOG_RESET();
375
376 #if defined(CONFIG_CMD_IDE)
377         WATCHDOG_RESET();
378         puts("IDE:   ");
379         ide_init();
380 #endif
381
382 #if defined(CONFIG_CMD_SCSI)
383         WATCHDOG_RESET();
384         puts("SCSI:  ");
385         scsi_init();
386 #endif
387
388 #if defined(CONFIG_CMD_DOC)
389         WATCHDOG_RESET();
390         puts("DOC:   ");
391         doc_init();
392 #endif
393
394 #ifdef CONFIG_BITBANGMII
395         bb_miiphy_init();
396 #endif
397 #if defined(CONFIG_CMD_NET)
398 #if defined(CONFIG_NET_MULTI)
399         WATCHDOG_RESET();
400         puts("Net:   ");
401 #endif
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
429         show_boot_progress(0x29);
430
431         /* main_loop() can return to retry autoboot, if so just run it again. */
432         for (;;) {
433                 main_loop();
434         }
435
436         /* NOTREACHED - no way out of command loop except booting */
437 }
438
439 void hang (void)
440 {
441         puts ("### ERROR ### Please RESET the board ###\n");
442         for (;;);
443 }
444
445 unsigned long do_go_exec (ulong (*entry)(int, char * const []), 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 }