* Patch by Josef Wagner, 12 Mar 2003:
[platform/kernel/u-boot.git] / lib_ppc / board.c
1 /*
2  * (C) Copyright 2000-2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <watchdog.h>
26 #include <command.h>
27 #include <malloc.h>
28 #include <devices.h>
29 #include <syscall.h>
30 #ifdef CONFIG_8xx
31 #include <mpc8xx.h>
32 #endif
33 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
34 #include <ide.h>
35 #endif
36 #if (CONFIG_COMMANDS & CFG_CMD_SCSI)
37 #include <scsi.h>
38 #endif
39 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
40 #include <kgdb.h>
41 #endif
42 #ifdef CONFIG_STATUS_LED
43 #include <status_led.h>
44 #endif
45 #include <net.h>
46 #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
47 #include <cmd_bedbug.h>
48 #endif
49 #ifdef CFG_ALLOC_DPRAM
50 #include <commproc.h>
51 #endif
52 #include <version.h>
53 #if defined(CONFIG_BAB7xx)
54 #include <w83c553f.h>
55 #endif
56 #include <dtt.h>
57 #if defined(CONFIG_POST)
58 #include <post.h>
59 #endif
60 #if defined(CONFIG_LOGBUFFER)
61 #include <logbuff.h>
62 #endif
63
64 #if (CONFIG_COMMANDS & CFG_CMD_DOC)
65 void doc_init (void);
66 #endif
67 #if defined(CONFIG_HARD_I2C) || \
68     defined(CONFIG_SOFT_I2C)
69 #include <i2c.h>
70 #endif
71
72 static char *failed = "*** failed ***\n";
73
74 #if defined(CONFIG_PCU_E) || defined(CONFIG_OXC)
75 extern flash_info_t flash_info[];
76 #endif
77
78 #include <environment.h>
79
80 #if ( ((CFG_ENV_ADDR+CFG_ENV_SIZE) < CFG_MONITOR_BASE) || \
81       (CFG_ENV_ADDR >= (CFG_MONITOR_BASE + CFG_MONITOR_LEN)) ) || \
82     defined(CFG_ENV_IS_IN_NVRAM)
83 #define TOTAL_MALLOC_LEN        (CFG_MALLOC_LEN + CFG_ENV_SIZE)
84 #else
85 #define TOTAL_MALLOC_LEN        CFG_MALLOC_LEN
86 #endif
87
88 /*
89  * Begin and End of memory area for malloc(), and current "brk"
90  */
91 static  ulong   mem_malloc_start = 0;
92 static  ulong   mem_malloc_end   = 0;
93 static  ulong   mem_malloc_brk   = 0;
94
95 /************************************************************************
96  * Utilities                                                            *
97  ************************************************************************
98  */
99
100 /*
101  * The Malloc area is immediately below the monitor copy in DRAM
102  */
103 static void mem_malloc_init (void)
104 {
105         DECLARE_GLOBAL_DATA_PTR;
106
107         ulong dest_addr = CFG_MONITOR_BASE + gd->reloc_off;
108
109         mem_malloc_end = dest_addr;
110         mem_malloc_start = dest_addr - TOTAL_MALLOC_LEN;
111         mem_malloc_brk = mem_malloc_start;
112
113         memset ((void *) mem_malloc_start,
114                 0,
115                 mem_malloc_end - mem_malloc_start);
116 }
117
118 void *sbrk (ptrdiff_t increment)
119 {
120         ulong old = mem_malloc_brk;
121         ulong new = old + increment;
122
123         if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
124                 return (NULL);
125         }
126         mem_malloc_brk = new;
127         return ((void *) old);
128 }
129
130 char *strmhz (char *buf, long hz)
131 {
132         long l, n;
133         long m;
134
135         n = hz / 1000000L;
136         l = sprintf (buf, "%ld", n);
137         m = (hz % 1000000L) / 1000L;
138         if (m != 0)
139                 sprintf (buf + l, ".%03ld", m);
140         return (buf);
141 }
142
143 static void syscalls_init (void)
144 {
145         ulong *addr;
146
147         syscall_tbl[SYSCALL_MALLOC] = (void *) malloc;
148         syscall_tbl[SYSCALL_FREE] = (void *) free;
149
150         syscall_tbl[SYSCALL_INSTALL_HDLR] = (void *) irq_install_handler;
151         syscall_tbl[SYSCALL_FREE_HDLR] = (void *) irq_free_handler;
152         syscall_tbl[SYSCALL_GET_TIMER] = (void *)get_timer;
153         syscall_tbl[SYSCALL_UDELAY] = (void *)udelay;
154
155         addr = (ulong *) 0xc00;         /* syscall ISR addr */
156
157         /* patch ISR code */
158         *addr++ |= (ulong) syscall_tbl >> 16;
159         *addr++ |= (ulong) syscall_tbl & 0xFFFF;
160         *addr++ |= NR_SYSCALLS >> 16;
161         *addr++ |= NR_SYSCALLS & 0xFFFF;
162
163         flush_cache (0x0C00, 0x10);
164
165         /* Initialize syscalls stack pointer                                 */
166         addr = (ulong *) 0xCFC;
167         *addr = (ulong)addr;
168         flush_cache ((ulong)addr, 0x10);
169 }
170
171 /*
172  * All attempts to come up with a "common" initialization sequence
173  * that works for all boards and architectures failed: some of the
174  * requirements are just _too_ different. To get rid of the resulting
175  * mess of board dependend #ifdef'ed code we now make the whole
176  * initialization sequence configurable to the user.
177  *
178  * The requirements for any new initalization function is simple: it
179  * receives a pointer to the "global data" structure as it's only
180  * argument, and returns an integer return code, where 0 means
181  * "continue" and != 0 means "fatal error, hang the system".
182  */
183 typedef int (init_fnc_t) (void);
184
185 /************************************************************************
186  * Init Utilities                                                       *
187  ************************************************************************
188  * Some of this code should be moved into the core functions,
189  * but let's get it working (again) first...
190  */
191
192 static int init_baudrate (void)
193 {
194         DECLARE_GLOBAL_DATA_PTR;
195
196         uchar tmp[64];  /* long enough for environment variables */
197         int i = getenv_r ("baudrate", tmp, sizeof (tmp));
198
199         gd->baudrate = (i > 0)
200                         ? (int) simple_strtoul (tmp, NULL, 10)
201                         : CONFIG_BAUDRATE;
202
203         return (0);
204 }
205
206 /***********************************************************************/
207
208 static int init_func_ram (void)
209 {
210         DECLARE_GLOBAL_DATA_PTR;
211
212 #ifdef  CONFIG_BOARD_TYPES
213         int board_type = gd->board_type;
214 #else
215         int board_type = 0;     /* use dummy arg */
216 #endif
217         puts ("DRAM:  ");
218
219         if ((gd->ram_size = initdram (board_type)) > 0) {
220                 print_size (gd->ram_size, "\n");
221                 return (0);
222         }
223         puts (failed);
224         return (1);
225 }
226
227 /***********************************************************************/
228
229 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
230 static int init_func_i2c (void)
231 {
232         puts ("I2C:   ");
233         i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
234         puts ("ready\n");
235         return (0);
236 }
237 #endif
238
239 /***********************************************************************/
240
241 #if defined(CONFIG_WATCHDOG)
242 static int init_func_watchdog_init (void)
243 {
244         puts ("       Watchdog enabled\n");
245         WATCHDOG_RESET ();
246         return (0);
247 }
248 # define INIT_FUNC_WATCHDOG_INIT        init_func_watchdog_init,
249
250 static int init_func_watchdog_reset (void)
251 {
252         WATCHDOG_RESET ();
253         return (0);
254 }
255 # define INIT_FUNC_WATCHDOG_RESET       init_func_watchdog_reset,
256 #else
257 # define INIT_FUNC_WATCHDOG_INIT        /* undef */
258 # define INIT_FUNC_WATCHDOG_RESET       /* undef */
259 #endif /* CONFIG_WATCHDOG */
260
261 /************************************************************************
262  * Initialization sequence                                              *
263  ************************************************************************
264  */
265
266 init_fnc_t *init_sequence[] = {
267
268 #if defined(CONFIG_BOARD_PRE_INIT)
269         board_pre_init,         /* very early board init code (fpga boot, etc.) */
270 #endif
271
272         get_clocks,             /* get CPU and bus clocks (etc.) */
273         init_timebase,
274 #ifdef CFG_ALLOC_DPRAM
275         dpram_init,
276 #endif
277 #if defined(CONFIG_BOARD_POSTCLK_INIT)
278         board_postclk_init,
279 #endif
280         env_init,
281         init_baudrate,
282         serial_init,
283         console_init_f,
284         display_options,
285 #if defined(CONFIG_8260)
286         prt_8260_rsr,
287         prt_8260_clks,
288 #endif /* CONFIG_8260 */
289         checkcpu,
290         checkboard,
291         INIT_FUNC_WATCHDOG_INIT
292 #if defined(CONFIG_BMW)         || \
293     defined(CONFIG_COGENT)      || \
294     defined(CONFIG_HYMOD)       || \
295     defined(CONFIG_RSD_PROTO)   || \
296     defined(CONFIG_W7O)
297         misc_init_f,
298 #endif
299         INIT_FUNC_WATCHDOG_RESET
300 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
301         init_func_i2c,
302 #endif
303 #if defined(CONFIG_DTT)         /* Digital Thermometers and Thermostats */
304         dtt_init,
305 #endif
306         INIT_FUNC_WATCHDOG_RESET
307         init_func_ram,
308 #if defined(CFG_DRAM_TEST)
309         testdram,
310 #endif /* CFG_DRAM_TEST */
311         INIT_FUNC_WATCHDOG_RESET
312
313         NULL,                   /* Terminate this list */
314 };
315
316 /************************************************************************
317  *
318  * This is the first part of the initialization sequence that is
319  * implemented in C, but still running from ROM.
320  *
321  * The main purpose is to provide a (serial) console interface as
322  * soon as possible (so we can see any error messages), and to
323  * initialize the RAM so that we can relocate the monitor code to
324  * RAM.
325  *
326  * Be aware of the restrictions: global data is read-only, BSS is not
327  * initialized, and stack space is limited to a few kB.
328  *
329  ************************************************************************
330  */
331
332 void board_init_f (ulong bootflag)
333 {
334         DECLARE_GLOBAL_DATA_PTR;
335
336         bd_t *bd;
337         ulong len, addr, addr_sp;
338         gd_t *id;
339         init_fnc_t **init_fnc_ptr;
340 #ifdef CONFIG_PRAM
341         int i;
342         ulong reg;
343         uchar tmp[64];          /* long enough for environment variables */
344 #endif
345
346         /* Pointer is writable since we allocated a register for it */
347         gd = (gd_t *) (CFG_INIT_RAM_ADDR + CFG_GBL_DATA_OFFSET);
348
349 #ifndef CONFIG_8260
350         /* Clear initial global data */
351         memset ((void *) gd, 0, sizeof (gd_t));
352 #endif
353
354         for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
355                 if ((*init_fnc_ptr) () != 0) {
356                         hang ();
357                 }
358         }
359
360         /*
361          * Now that we have DRAM mapped and working, we can
362          * relocate the code and continue running from DRAM.
363          *
364          * Reserve memory at end of RAM for (top down in that order):
365          *  - kernel log buffer
366          *  - protected RAM
367          *  - LCD framebuffer
368          *  - monitor code
369          *  - board info struct
370          */
371         len = get_endaddr () - CFG_MONITOR_BASE;
372
373         if (len > CFG_MONITOR_LEN) {
374                 printf ("*** U-Boot size %ld > reserved memory (%d)\n",
375                                 len, CFG_MONITOR_LEN);
376                 hang ();
377         }
378
379         if (CFG_MONITOR_LEN > len)
380                 len = CFG_MONITOR_LEN;
381
382 #ifndef CONFIG_VERY_BIG_RAM
383         addr = CFG_SDRAM_BASE + gd->ram_size;
384 #else
385         /* only allow stack below 256M */
386         addr = CFG_SDRAM_BASE +
387                (gd->ram_size > 256 << 20) ? 256 << 20 : gd->ram_size;
388 #endif
389
390 #ifdef CONFIG_LOGBUFFER
391         /* reserve kernel log buffer */
392         addr -= (LOGBUFF_RESERVE);
393 # ifdef DEBUG
394         printf ("Reserving %ldk for kernel logbuffer at %08lx\n", LOGBUFF_LEN, addr);
395 # endif
396 #endif
397
398 #ifdef CONFIG_PRAM
399         /*
400          * reserve protected RAM
401          */
402         i = getenv_r ("pram", tmp, sizeof (tmp));
403         reg = (i > 0) ? simple_strtoul (tmp, NULL, 10) : CONFIG_PRAM;
404         addr -= (reg << 10);            /* size is in kB */
405 # ifdef DEBUG
406         printf ("Reserving %ldk for protected RAM at %08lx\n", reg, addr);
407 # endif
408 #endif /* CONFIG_PRAM */
409
410         /* round down to next 4 kB limit */
411         addr &= ~(4096 - 1);
412 #ifdef DEBUG
413         printf ("Top of RAM usable for U-Boot at: %08lx\n", addr);
414 #endif
415
416 #ifdef CONFIG_LCD
417         /* reserve memory for LCD display (always full pages) */
418         addr = lcd_setmem (addr);
419         gd->fb_base = addr;
420 #endif /* CONFIG_LCD */
421
422 #if defined(CONFIG_VIDEO) && defined(CONFIG_8xx)
423         /* reserve memory for video display (always full pages) */
424         addr = video_setmem (addr);
425         gd->fb_base = addr;
426 #endif /* CONFIG_VIDEO  */
427
428         /*
429          * reserve memory for U-Boot code, data & bss
430          * round down to next 4 kB limit
431          */
432         addr -= len;
433         addr &= ~(4096 - 1);
434
435 #ifdef DEBUG
436         printf ("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
437 #endif
438
439 #ifdef CONFIG_AMIGAONEG3SE
440         gd->relocaddr = addr;
441 #endif
442
443         /*
444          * reserve memory for malloc() arena
445          */
446         addr_sp = addr - TOTAL_MALLOC_LEN;
447 #ifdef DEBUG
448         printf ("Reserving %dk for malloc() at: %08lx\n",
449                         TOTAL_MALLOC_LEN >> 10, addr_sp);
450 #endif
451
452         /*
453          * (permanently) allocate a Board Info struct
454          * and a permanent copy of the "global" data
455          */
456         addr_sp -= sizeof (bd_t);
457         bd = (bd_t *) addr_sp;
458         gd->bd = bd;
459 #ifdef DEBUG
460         printf ("Reserving %d Bytes for Board Info at: %08lx\n",
461                         sizeof (bd_t), addr_sp);
462 #endif
463         addr_sp -= sizeof (gd_t);
464         id = (gd_t *) addr_sp;
465 #ifdef DEBUG
466         printf ("Reserving %d Bytes for Global Data at: %08lx\n",
467                         sizeof (gd_t), addr_sp);
468 #endif
469
470         /*
471          * Finally, we set up a new (bigger) stack.
472          *
473          * Leave some safety gap for SP, force alignment on 16 byte boundary
474          * Clear initial stack frame
475          */
476         addr_sp -= 16;
477         addr_sp &= ~0xF;
478         *((ulong *) addr_sp)-- = 0;
479         *((ulong *) addr_sp)-- = 0;
480 #ifdef DEBUG
481         printf ("Stack Pointer at: %08lx\n", addr_sp);
482 #endif
483
484         /*
485          * Save local variables to board info struct
486          */
487
488         bd->bi_memstart  = CFG_SDRAM_BASE;      /* start of  DRAM memory      */
489         bd->bi_memsize   = gd->ram_size;        /* size  of  DRAM memory in bytes */
490
491 #ifdef CONFIG_IP860
492         bd->bi_sramstart = SRAM_BASE;   /* start of  SRAM memory      */
493         bd->bi_sramsize  = SRAM_SIZE;   /* size  of  SRAM memory      */
494 #else
495         bd->bi_sramstart = 0;           /* FIXME */ /* start of  SRAM memory      */
496         bd->bi_sramsize  = 0;           /* FIXME */ /* size  of  SRAM memory      */
497 #endif
498
499 #if defined(CONFIG_8xx) || defined(CONFIG_8260)
500         bd->bi_immr_base = CFG_IMMR;    /* base  of IMMR register     */
501 #endif
502
503         bd->bi_bootflags = bootflag;    /* boot / reboot flag (for LynxOS)    */
504
505         WATCHDOG_RESET ();
506         bd->bi_intfreq = gd->cpu_clk;   /* Internal Freq, in Hz */
507         bd->bi_busfreq = gd->bus_clk;   /* Bus Freq,      in Hz */
508 #if defined(CONFIG_8260)
509         bd->bi_cpmfreq = gd->cpm_clk;
510         bd->bi_brgfreq = gd->brg_clk;
511         bd->bi_sccfreq = gd->scc_clk;
512         bd->bi_vco     = gd->vco_out;
513 #endif /* CONFIG_8260 */
514
515         bd->bi_baudrate = gd->baudrate; /* Console Baudrate     */
516
517 #ifdef CFG_EXTBDINFO
518         strncpy (bd->bi_s_version, "1.2", sizeof (bd->bi_s_version));
519         strncpy (bd->bi_r_version, U_BOOT_VERSION, sizeof (bd->bi_r_version));
520
521         bd->bi_procfreq = gd->cpu_clk;  /* Processor Speed, In Hz */
522         bd->bi_plb_busfreq = gd->bus_clk;
523 #ifdef CONFIG_405GP
524         bd->bi_pci_busfreq = get_PCI_freq ();
525 #endif
526 #endif
527
528 #ifdef DEBUG
529         printf ("New Stack Pointer is: %08lx\n", addr_sp);
530 #endif
531
532         WATCHDOG_RESET ();
533
534 #ifdef CONFIG_POST
535         post_bootmode_init();
536         post_run (NULL, POST_ROM | post_bootmode_get(0));
537 #endif
538
539         WATCHDOG_RESET();
540
541         memcpy (id, gd, sizeof (gd_t));
542
543         relocate_code (addr_sp, id, addr);
544
545         /* NOTREACHED - relocate_code() does not return */
546 }
547
548
549 /************************************************************************
550  *
551  * This is the next part if the initialization sequence: we are now
552  * running from RAM and have a "normal" C environment, i. e. global
553  * data can be written, BSS has been cleared, the stack size in not
554  * that critical any more, etc.
555  *
556  ************************************************************************
557  */
558
559 void board_init_r (gd_t *id, ulong dest_addr)
560 {
561         DECLARE_GLOBAL_DATA_PTR;
562
563         cmd_tbl_t *cmdtp;
564         char *s, *e;
565         bd_t *bd;
566         int i;
567         extern void malloc_bin_reloc (void);
568 #ifndef CFG_ENV_IS_NOWHERE
569         extern char * env_name_spec;
570 #endif
571
572 #ifndef CFG_NO_FLASH
573         ulong flash_size;
574 #endif
575
576         gd = id;                /* initialize RAM version of global data */
577         bd = gd->bd;
578
579         gd->flags |= GD_FLG_RELOC;      /* tell others: relocation done */
580
581 #ifdef DEBUG
582         printf ("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
583 #endif
584
585         WATCHDOG_RESET ();
586
587         gd->reloc_off = dest_addr - CFG_MONITOR_BASE;
588
589         /*
590          * We have to relocate the command table manually
591          */
592         for (cmdtp = &cmd_tbl[0]; cmdtp->name; cmdtp++) {
593                 ulong addr;
594
595                 addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
596 #if 0
597                 printf ("Command \"%s\": 0x%08lx => 0x%08lx\n",
598                                 cmdtp->name, (ulong) (cmdtp->cmd), addr);
599 #endif
600                 cmdtp->cmd =
601                         (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;
602
603                 addr = (ulong)(cmdtp->name) + gd->reloc_off;
604                 cmdtp->name = (char *)addr;
605
606                 if (cmdtp->usage) {
607                         addr = (ulong)(cmdtp->usage) + gd->reloc_off;
608                         cmdtp->usage = (char *)addr;
609                 }
610 #ifdef  CFG_LONGHELP
611                 if (cmdtp->help) {
612                         addr = (ulong)(cmdtp->help) + gd->reloc_off;
613                         cmdtp->help = (char *)addr;
614                 }
615 #endif
616         }
617         /* there are some other pointer constants we must deal with */
618 #ifndef CFG_ENV_IS_NOWHERE
619         env_name_spec += gd->reloc_off;
620 #endif
621
622         WATCHDOG_RESET ();
623
624 #ifdef CONFIG_LOGBUFFER
625         logbuff_init_ptrs ();
626 #endif
627 #ifdef CONFIG_POST
628         post_output_backlog ();
629         post_reloc ();
630 #endif
631
632         WATCHDOG_RESET();
633
634 #if defined(CONFIG_IP860) || defined(CONFIG_PCU_E) || defined (CONFIG_FLAGADM)
635         icache_enable ();       /* it's time to enable the instruction cache */
636 #endif
637
638 #if defined(CONFIG_BAB7xx) || defined(CONFIG_CPC45)
639         /*
640          * Do PCI configuration on BAB7xx and CPC45 _before_ the flash
641          * gets initialised, because we need the ISA resp. PCI_to_LOCAL bus
642          * bridge there.
643          */
644         pci_init ();
645 #endif
646 #if defined(CONFIG_BAB7xx)
647         /*
648          * Initialise the ISA bridge
649          */
650         initialise_w83c553f ();
651 #endif
652
653         asm ("sync ; isync");
654
655         /*
656          * Setup trap handlers
657          */
658         trap_init (dest_addr);
659
660 #if !defined(CFG_NO_FLASH)
661         puts ("FLASH: ");
662
663         if ((flash_size = flash_init ()) > 0) {
664 #ifdef CFG_FLASH_CHECKSUM
665                 print_size (flash_size, "");
666                 /*
667                  * Compute and print flash CRC if flashchecksum is set to 'y'
668                  *
669                  * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
670                  */
671                 s = getenv ("flashchecksum");
672                 if (s && (*s == 'y')) {
673                         printf ("  CRC: %08lX",
674                                         crc32 (0,
675                                                    (const unsigned char *) CFG_FLASH_BASE,
676                                                    flash_size)
677                                         );
678                 }
679                 putc ('\n');
680 #else
681                 print_size (flash_size, "\n");
682 #endif /* CFG_FLASH_CHECKSUM */
683         } else {
684                 puts (failed);
685                 hang ();
686         }
687
688         bd->bi_flashstart = CFG_FLASH_BASE;     /* update start of FLASH memory    */
689         bd->bi_flashsize = flash_size;  /* size of FLASH memory (final value) */
690 #if defined(CONFIG_PCU_E) || defined(CONFIG_OXC)
691         bd->bi_flashoffset = 0;
692 #elif CFG_MONITOR_BASE == CFG_FLASH_BASE
693         bd->bi_flashoffset = CFG_MONITOR_LEN;   /* reserved area for startup monitor  */
694 #else
695         bd->bi_flashoffset = 0;
696 #endif
697 #else
698
699         bd->bi_flashsize = 0;
700         bd->bi_flashstart = 0;
701         bd->bi_flashoffset = 0;
702 #endif /* !CFG_NO_FLASH */
703
704         WATCHDOG_RESET ();
705
706         /* initialize higher level parts of CPU like time base and timers */
707         cpu_init_r ();
708
709         WATCHDOG_RESET ();
710
711         /* initialize malloc() area */
712         mem_malloc_init ();
713         malloc_bin_reloc ();
714
715 #ifdef CONFIG_SPI
716 # if !defined(CFG_ENV_IS_IN_EEPROM)
717         spi_init_f ();
718 # endif
719         spi_init_r ();
720 #endif
721
722         /* relocate environment function pointers etc. */
723         env_relocate ();
724
725         /*
726          * Fill in missing fields of bd_info.
727          * We do this here, where we have "normal" access to the
728          * environment; we used to do this still running from ROM,
729          * where had to use getenv_r(), which can be pretty slow when
730          * the environment is in EEPROM.
731          */
732         s = getenv ("ethaddr");
733 #if defined (CONFIG_MBX) || defined (CONFIG_RPXCLASSIC) || defined(CONFIG_IAD210)
734         if (s == NULL)
735                 board_get_enetaddr (bd->bi_enetaddr);
736         else
737 #endif
738                 for (i = 0; i < 6; ++i) {
739                         bd->bi_enetaddr[i] = s ? simple_strtoul (s, &e, 16) : 0;
740                         if (s)
741                                 s = (*e) ? e + 1 : e;
742                 }
743 #ifdef  CONFIG_HERMES
744         if ((gd->board_type >> 16) == 2)
745                 bd->bi_ethspeed = gd->board_type & 0xFFFF;
746         else
747                 bd->bi_ethspeed = 0xFFFF;
748 #endif
749
750 #ifdef CONFIG_NX823
751         load_sernum_ethaddr ();
752 #endif
753
754 #if defined(CFG_GT_6426x) || defined(CONFIG_PN62)
755         /* handle the 2nd ethernet address */
756
757         s = getenv ("eth1addr");
758
759         for (i = 0; i < 6; ++i) {
760                 bd->bi_enet1addr[i] = s ? simple_strtoul (s, &e, 16) : 0;
761                 if (s)
762                         s = (*e) ? e + 1 : e;
763         }
764 #endif
765 #if defined(CFG_GT_6426x)
766         /* handle the 3rd ethernet address */
767
768         s = getenv ("eth2addr");
769
770         for (i = 0; i < 6; ++i) {
771                 bd->bi_enet2addr[i] = s ? simple_strtoul (s, &e, 16) : 0;
772                 if (s)
773                         s = (*e) ? e + 1 : e;
774         }
775 #endif
776
777
778 #if defined(CONFIG_TQM8xxL) || defined(CONFIG_TQM8260) || \
779     defined(CONFIG_CCM)
780         load_sernum_ethaddr ();
781 #endif
782         /* IP Address */
783         bd->bi_ip_addr = getenv_IPaddr ("ipaddr");
784
785         WATCHDOG_RESET ();
786
787 #if defined(CONFIG_PCI) && !defined(CONFIG_BAB7xx)
788         /*
789          * Do pci configuration
790          */
791         pci_init ();
792 #endif
793
794 /** leave this here (after malloc(), environment and PCI are working) **/
795         /* Initialize devices */
796         devices_init ();
797
798         /* allocate syscalls table (console_init_r will fill it in */
799         syscall_tbl = (void **) malloc (NR_SYSCALLS * sizeof (void *));
800
801         /* Initialize the console (after the relocation and devices init) */
802         console_init_r ();
803 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
804         syscalls_init ();
805
806 #if defined(CONFIG_CCM)         || \
807     defined(CONFIG_COGENT)      || \
808     defined(CONFIG_CPCI405)     || \
809     defined(CONFIG_EVB64260)    || \
810     defined(CONFIG_HYMOD)       || \
811     defined(CONFIG_KUP4K)       || \
812     defined(CONFIG_LWMON)       || \
813     defined(CONFIG_PCU_E)       || \
814     defined(CONFIG_W7O)         || \
815     defined(CONFIG_MISC_INIT_R)
816         /* miscellaneous platform dependent initialisations */
817         misc_init_r ();
818 #endif
819
820 #ifdef  CONFIG_HERMES
821         if (bd->bi_ethspeed != 0xFFFF)
822                 hermes_start_lxt980 ((int) bd->bi_ethspeed);
823 #endif
824
825 #if (CONFIG_COMMANDS & CFG_CMD_NET) && ( \
826     defined(CONFIG_CCM)         || \
827     defined(CONFIG_ELPT860)     || \
828     defined(CONFIG_EP8260)      || \
829     defined(CONFIG_IP860)       || \
830     defined(CONFIG_IVML24)      || \
831     defined(CONFIG_IVMS8)       || \
832     defined(CONFIG_LWMON)       || \
833     defined(CONFIG_MPC8260ADS)  || \
834     defined(CONFIG_PCU_E)       || \
835     defined(CONFIG_RPXSUPER)    || \
836     defined(CONFIG_SPD823TS)    )
837
838         WATCHDOG_RESET ();
839 # ifdef DEBUG
840         puts ("Reset Ethernet PHY\n");
841 # endif
842         reset_phy ();
843 #endif
844
845 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
846         WATCHDOG_RESET ();
847         puts ("KGDB:  ");
848         kgdb_init ();
849 #endif
850
851 #ifdef DEBUG
852         printf ("U-Boot relocated to %08lx\n", dest_addr);
853 #endif
854
855         /*
856          * Enable Interrupts
857          */
858         interrupt_init ();
859
860         /* Must happen after interrupts are initialized since
861          * an irq handler gets installed
862          */
863 #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
864         serial_buffered_init();
865 #endif
866
867 #ifdef CONFIG_STATUS_LED
868         status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
869 #endif
870
871         udelay (20);
872
873         set_timer (0);
874
875         /* Insert function pointers now that we have relocated the code */
876
877         /* Initialize from environment */
878         if ((s = getenv ("loadaddr")) != NULL) {
879                 load_addr = simple_strtoul (s, NULL, 16);
880         }
881 #if (CONFIG_COMMANDS & CFG_CMD_NET)
882         if ((s = getenv ("bootfile")) != NULL) {
883                 copy_filename (BootFile, s, sizeof (BootFile));
884         }
885 #endif /* CFG_CMD_NET */
886
887         WATCHDOG_RESET ();
888
889 #if (CONFIG_COMMANDS & CFG_CMD_SCSI)
890         WATCHDOG_RESET ();
891         puts ("SCSI:  ");
892         scsi_init ();
893 #endif
894
895 #if (CONFIG_COMMANDS & CFG_CMD_DOC)
896         WATCHDOG_RESET ();
897         puts ("DOC:   ");
898         doc_init ();
899 #endif
900
901 #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI)
902         WATCHDOG_RESET ();
903         puts ("Net:   ");
904         eth_initialize (bd);
905 #endif
906
907 #ifdef CONFIG_POST
908         post_run (NULL, POST_RAM | post_bootmode_get(0));
909         if (post_bootmode_get(0) & POST_POWERFAIL) {
910                 post_bootmode_clear();
911                 board_poweroff();
912         }
913 #endif
914
915 #if (CONFIG_COMMANDS & CFG_CMD_PCMCIA) && !(CONFIG_COMMANDS & CFG_CMD_IDE)
916         WATCHDOG_RESET ();
917         puts ("PCMCIA:");
918         pcmcia_init ();
919 #endif
920
921 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
922         WATCHDOG_RESET ();
923 # ifdef CONFIG_IDE_8xx_PCCARD
924         puts ("PCMCIA:");
925 # else
926         puts ("IDE:   ");
927 #endif
928         ide_init ();
929 #endif /* CFG_CMD_IDE */
930
931 #ifdef CONFIG_LAST_STAGE_INIT
932         WATCHDOG_RESET ();
933         /*
934          * Some parts can be only initialized if all others (like
935          * Interrupts) are up and running (i.e. the PC-style ISA
936          * keyboard).
937          */
938         last_stage_init ();
939 #endif
940
941 #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
942         WATCHDOG_RESET ();
943         bedbug_init ();
944 #endif
945
946 #if defined(CONFIG_PRAM) || defined(CONFIG_LOGBUFFER)
947         /*
948          * Export available size of memory for Linux,
949          * taking into account the protected RAM at top of memory
950          */
951         {
952                 ulong pram;
953                 uchar memsz[32];
954 #ifdef CONFIG_PRAM
955                 char *s;
956
957                 if ((s = getenv ("pram")) != NULL) {
958                         pram = simple_strtoul (s, NULL, 10);
959                 } else {
960                         pram = CONFIG_PRAM;
961                 }
962 #else
963                 pram=0;
964 #endif
965 #ifdef CONFIG_LOGBUFFER
966                 /* Also take the logbuffer into account (pram is in kB) */
967                 pram += (LOGBUFF_LEN+LOGBUFF_OVERHEAD)/1024;
968 #endif
969                 sprintf (memsz, "%ldk", (bd->bi_memsize / 1024) - pram);
970                 setenv ("mem", memsz);
971         }
972 #endif
973
974         /* Initialization complete - start the monitor */
975
976         /* main_loop() can return to retry autoboot, if so just run it again. */
977         for (;;) {
978                 WATCHDOG_RESET ();
979                 main_loop ();
980         }
981
982         /* NOTREACHED - no way out of command loop except booting */
983 }
984
985 void hang (void)
986 {
987         puts ("### ERROR ### Please RESET the board ###\n");
988         for (;;);
989 }
990
991 #if 0 /* We could use plain global data, but the resulting code is bigger */
992 /*
993  * Pointer to initial global data area
994  *
995  * Here we initialize it.
996  */
997 #undef  XTRN_DECLARE_GLOBAL_DATA_PTR
998 #define XTRN_DECLARE_GLOBAL_DATA_PTR    /* empty = allocate here */
999 DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CFG_INIT_RAM_ADDR + CFG_GBL_DATA_OFFSET);
1000 #endif  /* 0 */
1001
1002 /************************************************************************/