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