1129918fe2d4aa7b61be198b93e5be6cb012b4b4
[platform/kernel/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 DECLARE_GLOBAL_DATA_PTR;
49
50 /* Exports from the Linker Script */
51 extern ulong __text_start;
52 extern ulong __data_end;
53 extern ulong __rel_dyn_start;
54 extern ulong __rel_dyn_end;
55 extern ulong __bss_start;
56 extern ulong __bss_end;
57
58 const char version_string[] =
59         U_BOOT_VERSION" (" U_BOOT_DATE " - " U_BOOT_TIME ")";
60
61 /************************************************************************
62  * Init Utilities                                                       *
63  ************************************************************************
64  * Some of this code should be moved into the core functions,
65  * or dropped completely,
66  * but let's get it working (again) first...
67  */
68 static int init_baudrate (void)
69 {
70         char tmp[64];   /* long enough for environment variables */
71         int i = getenv_f("baudrate", tmp, 64);
72
73         gd->baudrate = (i != 0)
74                         ? (int) simple_strtoul (tmp, NULL, 10)
75                         : CONFIG_BAUDRATE;
76
77         return (0);
78 }
79
80 static int display_banner (void)
81 {
82
83         printf ("\n\n%s\n\n", version_string);
84 /*
85         printf ("U-Boot code: %08lX -> %08lX  data: %08lX -> %08lX\n"
86                 "        BSS: %08lX -> %08lX stack: %08lX -> %08lX\n",
87                 i386boot_start, i386boot_romdata_start-1,
88                 i386boot_romdata_dest, i386boot_romdata_dest+i386boot_romdata_size-1,
89                 i386boot_bss_start, i386boot_bss_start+i386boot_bss_size-1,
90                 i386boot_bss_start+i386boot_bss_size,
91                 i386boot_bss_start+i386boot_bss_size+CONFIG_SYS_STACK_SIZE-1);
92
93 */
94
95         return (0);
96 }
97
98 /*
99  * WARNING: this code looks "cleaner" than the PowerPC version, but
100  * has the disadvantage that you either get nothing, or everything.
101  * On PowerPC, you might see "DRAM: " before the system hangs - which
102  * gives a simple yet clear indication which part of the
103  * initialization if failing.
104  */
105 static int display_dram_config (void)
106 {
107         int i;
108
109         puts ("DRAM Configuration:\n");
110
111         for (i=0; i<CONFIG_NR_DRAM_BANKS; i++) {
112                 printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
113                 print_size (gd->bd->bi_dram[i].size, "\n");
114         }
115
116         return (0);
117 }
118
119 static void display_flash_config (ulong size)
120 {
121         puts ("Flash: ");
122         print_size (size, "\n");
123 }
124
125 /*
126  * Breath some life into the board...
127  *
128  * Initialize an SMC for serial comms, and carry out some hardware
129  * tests.
130  *
131  * The first part of initialization is running from Flash memory;
132  * its main purpose is to initialize the RAM so that we
133  * can relocate the monitor code to RAM.
134  */
135
136
137 /*
138  * All attempts to come up with a "common" initialization sequence
139  * that works for all boards and architectures failed: some of the
140  * requirements are just _too_ different. To get rid of the resulting
141  * mess of board dependend #ifdef'ed code we now make the whole
142  * initialization sequence configurable to the user.
143  *
144  * The requirements for any new initalization function is simple: it
145  * receives a pointer to the "global data" structure as it's only
146  * argument, and returns an integer return code, where 0 means
147  * "continue" and != 0 means "fatal error, hang the system".
148  */
149 typedef int (init_fnc_t) (void);
150
151 init_fnc_t *init_sequence[] = {
152         cpu_init_r,             /* basic cpu dependent setup */
153         board_early_init_r,     /* basic board dependent setup */
154         dram_init,              /* configure available RAM banks */
155         interrupt_init,         /* set up exceptions */
156         timer_init,
157         env_init,               /* initialize environment */
158         init_baudrate,          /* initialze baudrate settings */
159         serial_init,            /* serial communications setup */
160         display_banner,
161         display_dram_config,
162
163         NULL,
164 };
165
166 gd_t *gd;
167
168 /*
169  * Load U-Boot into RAM, initialize BSS, perform relocation adjustments
170  */
171 void board_init_f (ulong gdp)
172 {
173         void *text_start = &__text_start;
174         void *data_end = &__data_end;
175         void *rel_dyn_start = &__rel_dyn_start;
176         void *rel_dyn_end = &__rel_dyn_end;
177         void *bss_start = &__bss_start;
178         void *bss_end = &__bss_end;
179
180         ulong *dst_addr;
181         ulong *src_addr;
182         ulong *end_addr;
183
184         void *dest_addr;
185         ulong rel_offset;
186         Elf32_Rel *re_src;
187         Elf32_Rel *re_end;
188
189         /* Calculate destination RAM Address and relocation offset */
190         dest_addr  = (void *)gdp - (bss_end - text_start);
191         rel_offset = text_start - dest_addr;
192
193         /* Perform low-level initialization only when cold booted */
194         if (((gd_t *)gdp)->flags & GD_FLG_COLD_BOOT) {
195                 /* First stage CPU initialization */
196                 if (cpu_init_f() != 0)
197                         hang();
198
199                 /* First stage Board initialization */
200                 if (board_early_init_f() != 0)
201                         hang();
202         }
203
204         /* Copy U-Boot into RAM */
205         dst_addr = (ulong *)dest_addr;
206         src_addr = (ulong *)(text_start + ((gd_t *)gdp)->load_off);
207         end_addr = (ulong *)(data_end  + ((gd_t *)gdp)->load_off);
208
209         while (src_addr < end_addr)
210                 *dst_addr++ = *src_addr++;
211
212         /* Clear BSS */
213         dst_addr = (ulong *)(bss_start - rel_offset);
214         end_addr = (ulong *)(bss_end - rel_offset);
215
216         while (dst_addr < end_addr)
217                 *dst_addr++ = 0x00000000;
218
219         /* Perform relocation adjustments */
220         re_src = (Elf32_Rel *)(rel_dyn_start + ((gd_t *)gdp)->load_off);
221         re_end = (Elf32_Rel *)(rel_dyn_end + ((gd_t *)gdp)->load_off);
222
223         do {
224                 if (re_src->r_offset >= TEXT_BASE)
225                         if (*(Elf32_Addr *)(re_src->r_offset - rel_offset) >= TEXT_BASE)
226                                 *(Elf32_Addr *)(re_src->r_offset - rel_offset) -= rel_offset;
227         } while (re_src++ < re_end);
228
229         ((gd_t *)gdp)->reloc_off = rel_offset;
230         ((gd_t *)gdp)->flags |= GD_FLG_RELOC;
231
232         /* Enter the relocated U-Boot! */
233         (board_init_r - rel_offset)((gd_t *)gdp, (ulong)dest_addr);
234
235         /* NOTREACHED - board_init_f() does not return */
236         while(1);
237 }
238
239 void board_init_r(gd_t *id, ulong dest_addr)
240 {
241         char *s;
242         int i;
243         ulong size;
244         static bd_t bd_data;
245         init_fnc_t **init_fnc_ptr;
246
247         show_boot_progress(0x21);
248
249         gd = id;
250         /* compiler optimization barrier needed for GCC >= 3.4 */
251         __asm__ __volatile__("": : :"memory");
252
253         gd->bd = &bd_data;
254         memset (gd->bd, 0, sizeof (bd_t));
255         show_boot_progress(0x22);
256
257         gd->baudrate =  CONFIG_BAUDRATE;
258
259         mem_malloc_init((((ulong)dest_addr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
260                         CONFIG_SYS_MALLOC_LEN);
261
262         for (init_fnc_ptr = init_sequence, i=0; *init_fnc_ptr; ++init_fnc_ptr, i++) {
263                 show_boot_progress(0xa130|i);
264
265                 if ((*init_fnc_ptr)() != 0) {
266                         hang ();
267                 }
268         }
269         show_boot_progress(0x23);
270
271 #ifdef CONFIG_SERIAL_MULTI
272         serial_initialize();
273 #endif
274         /* configure available FLASH banks */
275         size = flash_init();
276         display_flash_config(size);
277         show_boot_progress(0x24);
278
279         show_boot_progress(0x25);
280
281         /* initialize environment */
282         env_relocate ();
283         show_boot_progress(0x26);
284
285
286 #ifdef CONFIG_CMD_NET
287         /* IP Address */
288         bd_data.bi_ip_addr = getenv_IPaddr ("ipaddr");
289 #endif
290
291 #if defined(CONFIG_PCI)
292         /*
293          * Do pci configuration
294          */
295         pci_init();
296 #endif
297
298         show_boot_progress(0x27);
299
300
301         stdio_init ();
302
303         jumptable_init ();
304
305         /* Initialize the console (after the relocation and devices init) */
306         console_init_r();
307
308 #ifdef CONFIG_MISC_INIT_R
309         /* miscellaneous platform dependent initialisations */
310         misc_init_r();
311 #endif
312
313 #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
314         WATCHDOG_RESET();
315         puts ("PCMCIA:");
316         pcmcia_init();
317 #endif
318
319 #if defined(CONFIG_CMD_KGDB)
320         WATCHDOG_RESET();
321         puts("KGDB:  ");
322         kgdb_init();
323 #endif
324
325         /* enable exceptions */
326         enable_interrupts();
327         show_boot_progress(0x28);
328
329 #ifdef CONFIG_STATUS_LED
330         status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
331 #endif
332
333         udelay(20);
334
335         set_timer (0);
336
337         /* Initialize from environment */
338         if ((s = getenv ("loadaddr")) != NULL) {
339                 load_addr = simple_strtoul (s, NULL, 16);
340         }
341 #if defined(CONFIG_CMD_NET)
342         if ((s = getenv ("bootfile")) != NULL) {
343                 copy_filename (BootFile, s, sizeof (BootFile));
344         }
345 #endif
346
347         WATCHDOG_RESET();
348
349 #if defined(CONFIG_CMD_IDE)
350         WATCHDOG_RESET();
351         puts("IDE:   ");
352         ide_init();
353 #endif
354
355 #if defined(CONFIG_CMD_SCSI)
356         WATCHDOG_RESET();
357         puts("SCSI:  ");
358         scsi_init();
359 #endif
360
361 #if defined(CONFIG_CMD_DOC)
362         WATCHDOG_RESET();
363         puts("DOC:   ");
364         doc_init();
365 #endif
366
367 #ifdef CONFIG_BITBANGMII
368         bb_miiphy_init();
369 #endif
370 #if defined(CONFIG_CMD_NET)
371 #if defined(CONFIG_NET_MULTI)
372         WATCHDOG_RESET();
373         puts("Net:   ");
374 #endif
375         eth_initialize(gd->bd);
376 #endif
377
378 #if ( defined(CONFIG_CMD_NET)) && (0)
379         WATCHDOG_RESET();
380 # ifdef DEBUG
381         puts ("Reset Ethernet PHY\n");
382 # endif
383         reset_phy();
384 #endif
385
386 #ifdef CONFIG_LAST_STAGE_INIT
387         WATCHDOG_RESET();
388         /*
389          * Some parts can be only initialized if all others (like
390          * Interrupts) are up and running (i.e. the PC-style ISA
391          * keyboard).
392          */
393         last_stage_init();
394 #endif
395
396
397 #ifdef CONFIG_POST
398         post_run (NULL, POST_RAM | post_bootmode_get(0));
399 #endif
400
401
402         show_boot_progress(0x29);
403
404         /* main_loop() can return to retry autoboot, if so just run it again. */
405         for (;;) {
406                 main_loop();
407         }
408
409         /* NOTREACHED - no way out of command loop except booting */
410 }
411
412 void hang (void)
413 {
414         puts ("### ERROR ### Please RESET the board ###\n");
415         for (;;);
416 }
417
418 unsigned long do_go_exec (ulong (*entry)(int, char * const []), int argc, char * const argv[])
419 {
420         unsigned long ret = 0;
421         char **argv_tmp;
422
423         /*
424          * x86 does not use a dedicated register to pass the pointer to
425          * the global_data, so it is instead passed as argv[-1]. By using
426          * argv[-1], the called 'Application' can use the contents of
427          * argv natively. However, to safely use argv[-1] a new copy of
428          * argv is needed with the extra element
429          */
430         argv_tmp = malloc(sizeof(char *) * (argc + 1));
431
432         if (argv_tmp) {
433                 argv_tmp[0] = (char *)gd;
434
435                 memcpy(&argv_tmp[1], argv, (size_t)(sizeof(char *) * argc));
436
437                 ret = (entry) (argc, &argv_tmp[1]);
438                 free(argv_tmp);
439         }
440
441         return ret;
442 }
443
444 void setup_pcat_compatibility(void)
445         __attribute__((weak, alias("__setup_pcat_compatibility")));
446
447 void __setup_pcat_compatibility(void)
448 {
449 }