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