blob: 193860a87a4c29916b7a79441a8ae58273448ddd [file] [log] [blame]
wdenk2262cfe2002-11-18 00:14:45 +00001/*
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 <devices.h>
35#include <version.h>
36#include <malloc.h>
37#include <syscall.h>
38#include <net.h>
39#include <ide.h>
40
41extern long _i386boot_start;
42extern long _i386boot_end;
43extern long _i386boot_romdata_start;
44extern long _i386boot_romdata_dest;
45extern long _i386boot_romdata_size;
46extern long _i386boot_bss_start;
47extern long _i386boot_bss_size;
48
49extern long _i386boot_realmode;
50extern long _i386boot_realmode_size;
51extern long _i386boot_bios;
52extern long _i386boot_bios_size;
53
54/* The symbols defined by the linker script becomes pointers
55 * which is somewhat inconveient ... */
56ulong i386boot_start = (ulong)&_i386boot_start; /* code start (in flash) defined in start.S */
57ulong i386boot_end = (ulong)&_i386boot_end; /* code end (in flash) */
58ulong i386boot_romdata_start = (ulong)&_i386boot_romdata_start; /* datasegment in flash (also code+rodata end) */
59ulong i386boot_romdata_dest = (ulong)&_i386boot_romdata_dest; /* data location segment in ram */
60ulong i386boot_romdata_size = (ulong)&_i386boot_romdata_size; /* size of data segment */
61ulong i386boot_bss_start = (ulong)&_i386boot_bss_start; /* bss start */
62ulong i386boot_bss_size = (ulong)&_i386boot_bss_size; /* bss size */
63
64ulong i386boot_realmode = (ulong)&_i386boot_realmode; /* start of realmode entry code */
65ulong i386boot_realmode_size = (ulong)&_i386boot_realmode_size; /* size of realmode entry code */
66ulong i386boot_bios = (ulong)&_i386boot_bios; /* start of BIOS emulation code */
67ulong i386boot_bios_size = (ulong)&_i386boot_bios_size; /* size of BIOS emulation code */
68
69
70const char version_string[] =
71 U_BOOT_VERSION" (" __DATE__ " - " __TIME__ ")";
72
73
74/*
75 * Begin and End of memory area for malloc(), and current "brk"
76 */
77static ulong mem_malloc_start = 0;
78static ulong mem_malloc_end = 0;
79static ulong mem_malloc_brk = 0;
80
81static int mem_malloc_init(void)
82{
83 DECLARE_GLOBAL_DATA_PTR;
84
85#if 1
86 /* start malloc area right after the stack */
87 mem_malloc_start = i386boot_bss_start +
88 i386boot_bss_size + CFG_STACK_SIZE;
89 mem_malloc_start = (mem_malloc_start+3)&~3;
90#else
91 mem_malloc_start = 0x400000;
92#endif
93#if 1
94 /* Use all available RAM for malloc() */
95 mem_malloc_end = gd->ram_size;
96#else
97 /* Use only CONFIG_MALLOC_SIZE bytes of RAM for malloc() */
98 mem_malloc_end = mem_malloc_start + CONFIG_MALLOC_SIZE;
99#endif
100 mem_malloc_brk = mem_malloc_start;
101
102 return 0;
103}
104
105void *sbrk (ptrdiff_t increment)
106{
107 ulong old = mem_malloc_brk;
108 ulong new = old + increment;
109
110 if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
111 return (NULL);
112 }
113 mem_malloc_brk = new;
114
115 return ((void *) old);
116}
117
118char *strmhz (char *buf, long hz)
119{
120 long l, n;
121 long m;
122
123 n = hz / 1000000L;
124 l = sprintf (buf, "%ld", n);
125 m = (hz % 1000000L) / 1000L;
126 if (m != 0)
127 sprintf (buf + l, ".%03ld", m);
128 return (buf);
129}
130
131/************************************************************************
132 * Init Utilities *
133 ************************************************************************
134 * Some of this code should be moved into the core functions,
135 * or dropped completely,
136 * but let's get it working (again) first...
137 */
138static void syscalls_init (void)
139{
140 syscall_tbl[SYSCALL_MALLOC] = (void *) malloc;
141 syscall_tbl[SYSCALL_FREE] = (void *) free;
142
143 syscall_tbl[SYSCALL_INSTALL_HDLR] = (void *) irq_install_handler;
144 syscall_tbl[SYSCALL_FREE_HDLR] = (void *) irq_free_handler;
145
146}
147
148static int init_baudrate (void)
149{
150 DECLARE_GLOBAL_DATA_PTR;
151
152 uchar tmp[64]; /* long enough for environment variables */
153 int i = getenv_r ("baudrate", tmp, sizeof (tmp));
154
155 gd->baudrate = (i > 0)
156 ? (int) simple_strtoul (tmp, NULL, 10)
157 : CONFIG_BAUDRATE;
158
159 return (0);
160}
161
162static int display_banner (void)
163{
164
165 printf ("\n\n%s\n\n", version_string);
166 printf ("U-Boot code: %08lX -> %08lX data: %08lX -> %08lX\n"
167 " BSS: %08lX -> %08lX stack: %08lX -> %08lX\n",
168 i386boot_start, i386boot_romdata_start-1,
169 i386boot_romdata_dest, i386boot_romdata_dest+i386boot_romdata_size-1,
170 i386boot_bss_start, i386boot_bss_start+i386boot_bss_size-1,
171 i386boot_bss_start+i386boot_bss_size,
172 i386boot_bss_start+i386boot_bss_size+CFG_STACK_SIZE-1);
173
174
175 return (0);
176}
177
178/*
179 * WARNING: this code looks "cleaner" than the PowerPC version, but
180 * has the disadvantage that you either get nothing, or everything.
181 * On PowerPC, you might see "DRAM: " before the system hangs - which
182 * gives a simple yet clear indication which part of the
183 * initialization if failing.
184 */
185static int display_dram_config (void)
186{
187 DECLARE_GLOBAL_DATA_PTR;
188 int i;
189
190 puts ("DRAM Configuration:\n");
191
192 for (i=0; i<CONFIG_NR_DRAM_BANKS; i++) {
193 printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
194 print_size (gd->bd->bi_dram[i].size, "\n");
195 }
196
197 return (0);
198}
199
200static void display_flash_config (ulong size)
201{
202 puts ("Flash: ");
203 print_size (size, "\n");
204}
205
206
207
208/*
209 * Breath some life into the board...
210 *
211 * Initialize an SMC for serial comms, and carry out some hardware
212 * tests.
213 *
214 * The first part of initialization is running from Flash memory;
215 * its main purpose is to initialize the RAM so that we
216 * can relocate the monitor code to RAM.
217 */
218
219/*
220 * All attempts to come up with a "common" initialization sequence
221 * that works for all boards and architectures failed: some of the
222 * requirements are just _too_ different. To get rid of the resulting
223 * mess of board dependend #ifdef'ed code we now make the whole
224 * initialization sequence configurable to the user.
225 *
226 * The requirements for any new initalization function is simple: it
227 * receives a pointer to the "global data" structure as it's only
228 * argument, and returns an integer return code, where 0 means
229 * "continue" and != 0 means "fatal error, hang the system".
230 */
231typedef int (init_fnc_t) (void);
232
233init_fnc_t *init_sequence[] = {
234 cpu_init, /* basic cpu dependent setup */
235 board_init, /* basic board dependent setup */
236 dram_init, /* configure available RAM banks */
237 mem_malloc_init, /* dependant on dram_init */
238 interrupt_init, /* set up exceptions */
239 timer_init,
240 env_init, /* initialize environment */
241 init_baudrate, /* initialze baudrate settings */
242 serial_init, /* serial communications setup */
243 display_banner,
244 display_dram_config,
245
246 NULL,
247};
248
249gd_t *global_data;
250
251void start_i386boot (void)
252{
253 DECLARE_GLOBAL_DATA_PTR;
254 char *s;
255 int i;
256 ulong size;
257 static gd_t gd_data;
258 static bd_t bd_data;
259 init_fnc_t **init_fnc_ptr;
260
261 show_boot_progress(0x21);
262
263 gd = global_data = &gd_data;
264
265 memset (gd, 0, sizeof (gd_t));
266 gd->bd = &bd_data;
267 memset (gd->bd, 0, sizeof (bd_t));
268 show_boot_progress(0x22);
269
270
271 for (init_fnc_ptr = init_sequence, i=0; *init_fnc_ptr; ++init_fnc_ptr, i++) {
272 show_boot_progress(0xa130|i);
273
274 if ((*init_fnc_ptr)() != 0) {
275 hang ();
276 }
277 }
278 show_boot_progress(0x23);
279
280 /* configure available FLASH banks */
281 size = flash_init();
282 display_flash_config(size);
283 show_boot_progress(0x24);
284
285 show_boot_progress(0x25);
286
287 /* initialize environment */
288 env_relocate ();
289 show_boot_progress(0x26);
290
291
292 /* IP Address */
293 bd_data.bi_ip_addr = getenv_IPaddr ("ipaddr");
294
295 /* MAC Address */
296 {
297 int i;
298 ulong reg;
299 char *s, *e;
300 uchar tmp[64];
301
302 i = getenv_r ("ethaddr", tmp, sizeof (tmp));
303 s = (i > 0) ? tmp : NULL;
304
305 for (reg = 0; reg < 6; ++reg) {
306 bd_data.bi_enetaddr[reg] = s ? simple_strtoul (s, &e, 16) : 0;
307 if (s)
308 s = (*e) ? e + 1 : e;
309 }
310 }
311
312#if defined(CONFIG_PCI)
313 /*
314 * Do pci configuration
315 */
316 pci_init();
317#endif
318
319 show_boot_progress(0x27);
320
321
322 devices_init ();
323
324 /* allocate syscalls table (console_init_r will fill it in */
325 syscall_tbl = (void **) malloc (NR_SYSCALLS * sizeof (void *));
326
327 /* Initialize the console (after the relocation and devices init) */
328 console_init_r();
329 syscalls_init();
330
331#ifdef CONFIG_MISC_INIT_R
332 /* miscellaneous platform dependent initialisations */
333 misc_init_r();
334#endif
335
336
337#if (CONFIG_COMMANDS & CFG_CMD_NET) && (0)
338 WATCHDOG_RESET();
339# ifdef DEBUG
340 puts ("Reset Ethernet PHY\n");
341# endif
342 reset_phy();
343#endif
344
345#if (CONFIG_COMMANDS & CFG_CMD_PCMCIA) && !(CONFIG_COMMANDS & CFG_CMD_IDE)
346 WATCHDOG_RESET();
347 puts ("PCMCIA:");
348 pcmcia_init();
349#endif
350
351#if (CONFIG_COMMANDS & CFG_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 /* Must happen after interrupts are initialized since
362 * an irq handler gets installed
363 */
364#ifdef CONFIG_SERIAL_SOFTWARE_FIFO
365 serial_buffered_init();
366#endif
367
368#ifdef CONFIG_STATUS_LED
369 status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
370#endif
371
372 udelay (20);
373
374 set_timer (0);
375
376 /* Initialize from environment */
377 if ((s = getenv ("loadaddr")) != NULL) {
378 load_addr = simple_strtoul (s, NULL, 16);
379 }
380#if (CONFIG_COMMANDS & CFG_CMD_NET)
381 if ((s = getenv ("bootfile")) != NULL) {
382 copy_filename (BootFile, s, sizeof (BootFile));
383 }
384#endif /* CFG_CMD_NET */
385
386 WATCHDOG_RESET();
387
388#if (CONFIG_COMMANDS & CFG_CMD_IDE)
389 WATCHDOG_RESET();
390 puts("IDE: ");
391 ide_init();
392#endif /* CFG_CMD_IDE */
393
394#if (CONFIG_COMMANDS & CFG_CMD_SCSI)
395 WATCHDOG_RESET();
396 puts("SCSI: ");
397 scsi_init();
398#endif
399
400#if (CONFIG_COMMANDS & CFG_CMD_DOC)
401 WATCHDOG_RESET();
402 puts ("DOC: ");
403 doc_init();
404#endif
405
406#if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI)
407 WATCHDOG_RESET();
408 puts("Net: ");
409 eth_initialize(gd->bd);
410#endif
411
412#ifdef CONFIG_LAST_STAGE_INIT
413 WATCHDOG_RESET();
414 /*
415 * Some parts can be only initialized if all others (like
416 * Interrupts) are up and running (i.e. the PC-style ISA
417 * keyboard).
418 */
419 last_stage_init();
420#endif
421
422
423
424#ifdef CONFIG_POST
425 post_run (NULL, POST_RAM | post_bootmode_get(0));
426 if (post_bootmode_get(0) & POST_POWERFAIL) {
427 post_bootmode_clear();
428 board_poweroff();
429 }
430#endif
431
432 show_boot_progress(0x29);
433
434 /* main_loop() can return to retry autoboot, if so just run it again. */
435 for (;;) {
436 main_loop ();
437 }
438
439 /* NOTREACHED - no way out of command loop except booting */
440}
441
442void hang (void)
443{
444 puts ("### ERROR ### Please RESET the board ###\n");
445 for (;;);
446}
447
448