blob: e90eb6e569b8374c35731ac1ed40f368d568aa09 [file] [log] [blame]
wdenk2262cfe2002-11-18 00:14:45 +00001/*
2 * (C) Copyright 2002
3 * Daniel Engström, Omicron Ceti AB, daniel@omicron.se
wdenk8bde7f72003-06-27 21:31:46 +00004 *
wdenk2262cfe2002-11-18 00:14:45 +00005 * (C) Copyright 2002
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk8bde7f72003-06-27 21:31:46 +00007 *
wdenk2262cfe2002-11-18 00:14:45 +00008 * (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>
wdenk2262cfe2002-11-18 00:14:45 +000037#include <net.h>
38#include <ide.h>
wdenk7a8e9bed2003-05-31 18:35:21 +000039#include <asm/u-boot-i386.h>
wdenk2262cfe2002-11-18 00:14:45 +000040
wdenk8bde7f72003-06-27 21:31:46 +000041extern long _i386boot_start;
42extern long _i386boot_end;
wdenk2262cfe2002-11-18 00:14:45 +000043extern long _i386boot_romdata_start;
wdenk8bde7f72003-06-27 21:31:46 +000044extern long _i386boot_romdata_dest;
45extern long _i386boot_romdata_size;
46extern long _i386boot_bss_start;
47extern long _i386boot_bss_size;
wdenk2262cfe2002-11-18 00:14:45 +000048
wdenk8bde7f72003-06-27 21:31:46 +000049extern long _i386boot_realmode;
wdenk2262cfe2002-11-18 00:14:45 +000050extern long _i386boot_realmode_size;
wdenk8bde7f72003-06-27 21:31:46 +000051extern long _i386boot_bios;
52extern long _i386boot_bios_size;
wdenk2262cfe2002-11-18 00:14:45 +000053
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;
wdenk8bde7f72003-06-27 21:31:46 +000084
wdenk2262cfe2002-11-18 00:14:45 +000085 /* start malloc area right after the stack */
wdenk8bde7f72003-06-27 21:31:46 +000086 mem_malloc_start = i386boot_bss_start +
wdenk2262cfe2002-11-18 00:14:45 +000087 i386boot_bss_size + CFG_STACK_SIZE;
88 mem_malloc_start = (mem_malloc_start+3)&~3;
wdenk8bde7f72003-06-27 21:31:46 +000089
wdenk2262cfe2002-11-18 00:14:45 +000090 /* Use all available RAM for malloc() */
91 mem_malloc_end = gd->ram_size;
wdenk8bde7f72003-06-27 21:31:46 +000092
wdenk2262cfe2002-11-18 00:14:45 +000093 mem_malloc_brk = mem_malloc_start;
94
95 return 0;
96}
97
98void *sbrk (ptrdiff_t increment)
99{
100 ulong old = mem_malloc_brk;
101 ulong new = old + increment;
102
103 if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
104 return (NULL);
105 }
106 mem_malloc_brk = new;
107
108 return ((void *) old);
109}
110
111char *strmhz (char *buf, long hz)
112{
113 long l, n;
114 long m;
115
116 n = hz / 1000000L;
117 l = sprintf (buf, "%ld", n);
118 m = (hz % 1000000L) / 1000L;
119 if (m != 0)
120 sprintf (buf + l, ".%03ld", m);
121 return (buf);
122}
123
124/************************************************************************
125 * Init Utilities *
126 ************************************************************************
127 * Some of this code should be moved into the core functions,
128 * or dropped completely,
129 * but let's get it working (again) first...
130 */
wdenk2262cfe2002-11-18 00:14:45 +0000131static int init_baudrate (void)
132{
133 DECLARE_GLOBAL_DATA_PTR;
134
wdenk7a8e9bed2003-05-31 18:35:21 +0000135 char tmp[64]; /* long enough for environment variables */
136 int i = getenv_r("baudrate", tmp, 64);
wdenk2262cfe2002-11-18 00:14:45 +0000137
wdenk7a8e9bed2003-05-31 18:35:21 +0000138 gd->baudrate = (i != 0)
wdenk2262cfe2002-11-18 00:14:45 +0000139 ? (int) simple_strtoul (tmp, NULL, 10)
140 : CONFIG_BAUDRATE;
141
142 return (0);
143}
144
145static int display_banner (void)
146{
147
148 printf ("\n\n%s\n\n", version_string);
149 printf ("U-Boot code: %08lX -> %08lX data: %08lX -> %08lX\n"
150 " BSS: %08lX -> %08lX stack: %08lX -> %08lX\n",
151 i386boot_start, i386boot_romdata_start-1,
152 i386boot_romdata_dest, i386boot_romdata_dest+i386boot_romdata_size-1,
153 i386boot_bss_start, i386boot_bss_start+i386boot_bss_size-1,
wdenk8bde7f72003-06-27 21:31:46 +0000154 i386boot_bss_start+i386boot_bss_size,
wdenk2262cfe2002-11-18 00:14:45 +0000155 i386boot_bss_start+i386boot_bss_size+CFG_STACK_SIZE-1);
wdenk8bde7f72003-06-27 21:31:46 +0000156
wdenk2262cfe2002-11-18 00:14:45 +0000157
158 return (0);
159}
160
161/*
162 * WARNING: this code looks "cleaner" than the PowerPC version, but
163 * has the disadvantage that you either get nothing, or everything.
164 * On PowerPC, you might see "DRAM: " before the system hangs - which
165 * gives a simple yet clear indication which part of the
166 * initialization if failing.
167 */
168static int display_dram_config (void)
169{
170 DECLARE_GLOBAL_DATA_PTR;
171 int i;
172
173 puts ("DRAM Configuration:\n");
174
175 for (i=0; i<CONFIG_NR_DRAM_BANKS; i++) {
176 printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
177 print_size (gd->bd->bi_dram[i].size, "\n");
178 }
wdenk8bde7f72003-06-27 21:31:46 +0000179
wdenk2262cfe2002-11-18 00:14:45 +0000180 return (0);
181}
182
183static void display_flash_config (ulong size)
184{
185 puts ("Flash: ");
186 print_size (size, "\n");
187}
188
189
wdenk2262cfe2002-11-18 00:14:45 +0000190/*
191 * Breath some life into the board...
192 *
193 * Initialize an SMC for serial comms, and carry out some hardware
194 * tests.
195 *
196 * The first part of initialization is running from Flash memory;
197 * its main purpose is to initialize the RAM so that we
198 * can relocate the monitor code to RAM.
199 */
200
201/*
202 * All attempts to come up with a "common" initialization sequence
203 * that works for all boards and architectures failed: some of the
204 * requirements are just _too_ different. To get rid of the resulting
205 * mess of board dependend #ifdef'ed code we now make the whole
206 * initialization sequence configurable to the user.
207 *
208 * The requirements for any new initalization function is simple: it
209 * receives a pointer to the "global data" structure as it's only
210 * argument, and returns an integer return code, where 0 means
211 * "continue" and != 0 means "fatal error, hang the system".
212 */
213typedef int (init_fnc_t) (void);
214
215init_fnc_t *init_sequence[] = {
216 cpu_init, /* basic cpu dependent setup */
217 board_init, /* basic board dependent setup */
218 dram_init, /* configure available RAM banks */
219 mem_malloc_init, /* dependant on dram_init */
220 interrupt_init, /* set up exceptions */
wdenk8bde7f72003-06-27 21:31:46 +0000221 timer_init,
wdenk7a8e9bed2003-05-31 18:35:21 +0000222 serial_init,
wdenk2262cfe2002-11-18 00:14:45 +0000223 env_init, /* initialize environment */
224 init_baudrate, /* initialze baudrate settings */
225 serial_init, /* serial communications setup */
226 display_banner,
227 display_dram_config,
228
229 NULL,
230};
231
232gd_t *global_data;
233
234void start_i386boot (void)
235{
236 DECLARE_GLOBAL_DATA_PTR;
237 char *s;
238 int i;
239 ulong size;
240 static gd_t gd_data;
241 static bd_t bd_data;
242 init_fnc_t **init_fnc_ptr;
wdenk8bde7f72003-06-27 21:31:46 +0000243
wdenk2262cfe2002-11-18 00:14:45 +0000244 show_boot_progress(0x21);
245
246 gd = global_data = &gd_data;
wdenk93f6a672004-07-01 20:28:03 +0000247 /* compiler optimization barrier needed for GCC >= 3.4 */
248 __asm__ __volatile__("": : :"memory");
wdenk8bde7f72003-06-27 21:31:46 +0000249
wdenk2262cfe2002-11-18 00:14:45 +0000250 memset (gd, 0, sizeof (gd_t));
251 gd->bd = &bd_data;
252 memset (gd->bd, 0, sizeof (bd_t));
253 show_boot_progress(0x22);
254
wdenk7a8e9bed2003-05-31 18:35:21 +0000255 gd->baudrate = CONFIG_BAUDRATE;
wdenk8bde7f72003-06-27 21:31:46 +0000256
wdenk2262cfe2002-11-18 00:14:45 +0000257 for (init_fnc_ptr = init_sequence, i=0; *init_fnc_ptr; ++init_fnc_ptr, i++) {
258 show_boot_progress(0xa130|i);
259
260 if ((*init_fnc_ptr)() != 0) {
261 hang ();
262 }
263 }
264 show_boot_progress(0x23);
265
266 /* configure available FLASH banks */
267 size = flash_init();
268 display_flash_config(size);
269 show_boot_progress(0x24);
270
271 show_boot_progress(0x25);
272
273 /* initialize environment */
274 env_relocate ();
275 show_boot_progress(0x26);
276
277
278 /* IP Address */
279 bd_data.bi_ip_addr = getenv_IPaddr ("ipaddr");
280
281 /* MAC Address */
282 {
283 int i;
284 ulong reg;
285 char *s, *e;
286 uchar tmp[64];
287
288 i = getenv_r ("ethaddr", tmp, sizeof (tmp));
289 s = (i > 0) ? tmp : NULL;
290
291 for (reg = 0; reg < 6; ++reg) {
292 bd_data.bi_enetaddr[reg] = s ? simple_strtoul (s, &e, 16) : 0;
293 if (s)
294 s = (*e) ? e + 1 : e;
295 }
296 }
297
298#if defined(CONFIG_PCI)
299 /*
300 * Do pci configuration
301 */
302 pci_init();
303#endif
304
305 show_boot_progress(0x27);
306
307
308 devices_init ();
309
wdenk27b207f2003-07-24 23:38:38 +0000310 jumptable_init ();
wdenk8bde7f72003-06-27 21:31:46 +0000311
wdenk2262cfe2002-11-18 00:14:45 +0000312 /* Initialize the console (after the relocation and devices init) */
313 console_init_r();
wdenk2262cfe2002-11-18 00:14:45 +0000314
315#ifdef CONFIG_MISC_INIT_R
316 /* miscellaneous platform dependent initialisations */
317 misc_init_r();
318#endif
319
wdenk2262cfe2002-11-18 00:14:45 +0000320#if (CONFIG_COMMANDS & CFG_CMD_PCMCIA) && !(CONFIG_COMMANDS & CFG_CMD_IDE)
321 WATCHDOG_RESET();
322 puts ("PCMCIA:");
323 pcmcia_init();
324#endif
325
326#if (CONFIG_COMMANDS & CFG_CMD_KGDB)
327 WATCHDOG_RESET();
328 puts("KGDB: ");
329 kgdb_init();
330#endif
331
332 /* enable exceptions */
wdenk7a8e9bed2003-05-31 18:35:21 +0000333 enable_interrupts();
wdenk2262cfe2002-11-18 00:14:45 +0000334 show_boot_progress(0x28);
335
336 /* Must happen after interrupts are initialized since
337 * an irq handler gets installed
338 */
wdenk42dfe7a2004-03-14 22:25:36 +0000339#ifdef CONFIG_SERIAL_SOFTWARE_FIFO
wdenk2262cfe2002-11-18 00:14:45 +0000340 serial_buffered_init();
341#endif
wdenk8bde7f72003-06-27 21:31:46 +0000342
wdenk2262cfe2002-11-18 00:14:45 +0000343#ifdef CONFIG_STATUS_LED
344 status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
345#endif
346
wdenk7a8e9bed2003-05-31 18:35:21 +0000347 udelay(20);
wdenk2262cfe2002-11-18 00:14:45 +0000348
349 set_timer (0);
350
351 /* Initialize from environment */
352 if ((s = getenv ("loadaddr")) != NULL) {
353 load_addr = simple_strtoul (s, NULL, 16);
354 }
355#if (CONFIG_COMMANDS & CFG_CMD_NET)
356 if ((s = getenv ("bootfile")) != NULL) {
357 copy_filename (BootFile, s, sizeof (BootFile));
358 }
359#endif /* CFG_CMD_NET */
360
361 WATCHDOG_RESET();
362
363#if (CONFIG_COMMANDS & CFG_CMD_IDE)
364 WATCHDOG_RESET();
365 puts("IDE: ");
366 ide_init();
367#endif /* CFG_CMD_IDE */
368
369#if (CONFIG_COMMANDS & CFG_CMD_SCSI)
370 WATCHDOG_RESET();
371 puts("SCSI: ");
372 scsi_init();
373#endif
374
375#if (CONFIG_COMMANDS & CFG_CMD_DOC)
376 WATCHDOG_RESET();
wdenk7a8e9bed2003-05-31 18:35:21 +0000377 puts("DOC: ");
wdenk2262cfe2002-11-18 00:14:45 +0000378 doc_init();
379#endif
380
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200381#if (CONFIG_COMMANDS & CFG_CMD_NET)
382#if defined(CONFIG_NET_MULTI)
wdenk2262cfe2002-11-18 00:14:45 +0000383 WATCHDOG_RESET();
384 puts("Net: ");
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200385#endif
wdenk2262cfe2002-11-18 00:14:45 +0000386 eth_initialize(gd->bd);
387#endif
388
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200389#if (CONFIG_COMMANDS & CFG_CMD_NET) && (0)
390 WATCHDOG_RESET();
391# ifdef DEBUG
392 puts ("Reset Ethernet PHY\n");
393# endif
394 reset_phy();
395#endif
396
wdenk2262cfe2002-11-18 00:14:45 +0000397#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
wdenk2262cfe2002-11-18 00:14:45 +0000408#ifdef CONFIG_POST
409 post_run (NULL, POST_RAM | post_bootmode_get(0));
wdenk2262cfe2002-11-18 00:14:45 +0000410#endif
wdenk8bde7f72003-06-27 21:31:46 +0000411
412
wdenk2262cfe2002-11-18 00:14:45 +0000413 show_boot_progress(0x29);
wdenk8bde7f72003-06-27 21:31:46 +0000414
wdenk2262cfe2002-11-18 00:14:45 +0000415 /* main_loop() can return to retry autoboot, if so just run it again. */
416 for (;;) {
wdenk7a8e9bed2003-05-31 18:35:21 +0000417 main_loop();
wdenk2262cfe2002-11-18 00:14:45 +0000418 }
419
420 /* NOTREACHED - no way out of command loop except booting */
421}
422
423void hang (void)
424{
425 puts ("### ERROR ### Please RESET the board ###\n");
426 for (;;);
427}