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