0c2e008e00d18b2e2a701ce65d4bd7cce3e9f72c
[platform/kernel/u-boot.git] / arch / powerpc / lib / board.c
1 /*
2  * (C) Copyright 2000-2011
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 <stdio_dev.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_SCSI)
42 #include <scsi.h>
43 #endif
44 #if defined(CONFIG_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 CONFIG_GENERIC_MMC
52 #include <mmc.h>
53 #endif
54 #include <serial.h>
55 #ifdef CONFIG_SYS_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(CONFIG_SYS_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 CONFIG_ADDR_MAP
79 #include <asm/mmu.h>
80 #endif
81
82 #ifdef CONFIG_MP
83 #include <asm/mp.h>
84 #endif
85
86 #ifdef CONFIG_BITBANGMII
87 #include <miiphy.h>
88 #endif
89
90 #ifdef CONFIG_SYS_UPDATE_FLASH_SIZE
91 extern int update_flash_size(int flash_size);
92 #endif
93
94 #if defined(CONFIG_SC3)
95 extern void sc3_read_eeprom(void);
96 #endif
97
98 #if defined(CONFIG_CMD_DOC)
99 void doc_init(void);
100 #endif
101 #if defined(CONFIG_HARD_I2C) || \
102         defined(CONFIG_SOFT_I2C)
103 #include <i2c.h>
104 #endif
105 #include <spi.h>
106 #include <nand.h>
107
108 static char *failed = "*** failed ***\n";
109
110 #if defined(CONFIG_OXC) || defined(CONFIG_RMU)
111 extern flash_info_t flash_info[];
112 #endif
113
114 #if defined(CONFIG_START_IDE)
115 extern int board_start_ide(void);
116 #endif
117 #include <environment.h>
118
119 DECLARE_GLOBAL_DATA_PTR;
120
121 #if !defined(CONFIG_SYS_MEM_TOP_HIDE)
122 #define CONFIG_SYS_MEM_TOP_HIDE 0
123 #endif
124
125 extern ulong __init_end;
126 extern ulong __bss_end;
127 ulong monitor_flash_len;
128
129 #if defined(CONFIG_CMD_BEDBUG)
130 #include <bedbug/type.h>
131 #endif
132
133 /*
134  * Utilities
135  */
136
137 /*
138  * All attempts to come up with a "common" initialization sequence
139  * that works for all boards and architectures failed: some of the
140  * requirements are just _too_ different. To get rid of the resulting
141  * mess of board dependend #ifdef'ed code we now make the whole
142  * initialization sequence configurable to the user.
143  *
144  * The requirements for any new initalization function is simple: it
145  * receives a pointer to the "global data" structure as it's only
146  * argument, and returns an integer return code, where 0 means
147  * "continue" and != 0 means "fatal error, hang the system".
148  */
149 typedef int (init_fnc_t)(void);
150
151 /*
152  * Init Utilities
153  *
154  * Some of this code should be moved into the core functions,
155  * but let's get it working (again) first...
156  */
157
158 static int init_baudrate(void)
159 {
160         gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
161         return 0;
162 }
163
164 /***********************************************************************/
165
166 static void __board_add_ram_info(int use_default)
167 {
168         /* please define platform specific board_add_ram_info() */
169 }
170
171 void board_add_ram_info(int)
172         __attribute__ ((weak, alias("__board_add_ram_info")));
173
174 static int __board_flash_wp_on(void)
175 {
176         /*
177          * Most flashes can't be detected when write protection is enabled,
178          * so provide a way to let U-Boot gracefully ignore write protected
179          * devices.
180          */
181         return 0;
182 }
183
184 int board_flash_wp_on(void)
185         __attribute__ ((weak, alias("__board_flash_wp_on")));
186
187 static void __cpu_secondary_init_r(void)
188 {
189 }
190
191 void cpu_secondary_init_r(void)
192         __attribute__ ((weak, alias("__cpu_secondary_init_r")));
193
194 static int init_func_ram(void)
195 {
196 #ifdef  CONFIG_BOARD_TYPES
197         int board_type = gd->board_type;
198 #else
199         int board_type = 0;     /* use dummy arg */
200 #endif
201         puts("DRAM:  ");
202
203         gd->ram_size = initdram(board_type);
204
205         if (gd->ram_size > 0) {
206                 print_size(gd->ram_size, "");
207                 board_add_ram_info(0);
208                 putc('\n');
209                 return 0;
210         }
211         puts(failed);
212         return 1;
213 }
214
215 /***********************************************************************/
216
217 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C) || \
218                 defined(CONFIG_SYS_I2C)
219 static int init_func_i2c(void)
220 {
221         puts("I2C:   ");
222 #ifdef CONFIG_SYS_I2C
223         i2c_init_all();
224 #else
225         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
226 #endif
227         puts("ready\n");
228         return 0;
229 }
230 #endif
231
232 #if defined(CONFIG_HARD_SPI)
233 static int init_func_spi(void)
234 {
235         puts("SPI:   ");
236         spi_init();
237         puts("ready\n");
238         return 0;
239 }
240 #endif
241
242 /***********************************************************************/
243
244 #if defined(CONFIG_WATCHDOG)
245 int init_func_watchdog_init(void)
246 {
247         puts("       Watchdog enabled\n");
248         WATCHDOG_RESET();
249         return 0;
250 }
251
252 int init_func_watchdog_reset(void)
253 {
254         WATCHDOG_RESET();
255         return 0;
256 }
257 #endif /* CONFIG_WATCHDOG */
258
259 /*
260  * Initialization sequence
261  */
262
263 static init_fnc_t *init_sequence[] = {
264 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
265         probecpu,
266 #endif
267 #if defined(CONFIG_BOARD_EARLY_INIT_F)
268         board_early_init_f,
269 #endif
270 #if !defined(CONFIG_8xx_CPUCLK_DEFAULT)
271         get_clocks,             /* get CPU and bus clocks (etc.) */
272 #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
273     && !defined(CONFIG_TQM885D)
274         adjust_sdram_tbs_8xx,
275 #endif
276         init_timebase,
277 #endif
278 #ifdef CONFIG_SYS_ALLOC_DPRAM
279 #if !defined(CONFIG_CPM2)
280         dpram_init,
281 #endif
282 #endif
283 #if defined(CONFIG_BOARD_POSTCLK_INIT)
284         board_postclk_init,
285 #endif
286         env_init,
287 #if defined(CONFIG_8xx_CPUCLK_DEFAULT)
288         /* get CPU and bus clocks according to the environment variable */
289         get_clocks_866,
290         /* adjust sdram refresh rate according to the new clock */
291         sdram_adjust_866,
292         init_timebase,
293 #endif
294         init_baudrate,
295         serial_init,
296         console_init_f,
297         display_options,
298 #if defined(CONFIG_8260)
299         prt_8260_rsr,
300         prt_8260_clks,
301 #endif /* CONFIG_8260 */
302 #if defined(CONFIG_MPC83xx)
303         prt_83xx_rsr,
304 #endif
305         checkcpu,
306 #if defined(CONFIG_MPC5xxx)
307         prt_mpc5xxx_clks,
308 #endif /* CONFIG_MPC5xxx */
309         checkboard,
310         INIT_FUNC_WATCHDOG_INIT
311 #if defined(CONFIG_MISC_INIT_F)
312         misc_init_f,
313 #endif
314         INIT_FUNC_WATCHDOG_RESET
315 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C) || \
316                 defined(CONFIG_SYS_I2C)
317         init_func_i2c,
318 #endif
319 #if defined(CONFIG_HARD_SPI)
320         init_func_spi,
321 #endif
322 #ifdef CONFIG_POST
323         post_init_f,
324 #endif
325         INIT_FUNC_WATCHDOG_RESET
326         init_func_ram,
327 #if defined(CONFIG_SYS_DRAM_TEST)
328         testdram,
329 #endif /* CONFIG_SYS_DRAM_TEST */
330         INIT_FUNC_WATCHDOG_RESET
331         NULL,   /* Terminate this list */
332 };
333
334 ulong get_effective_memsize(void)
335 {
336 #ifndef CONFIG_VERY_BIG_RAM
337         return gd->ram_size;
338 #else
339         /* limit stack to what we can reasonable map */
340         return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ?
341                 CONFIG_MAX_MEM_MAPPED : gd->ram_size);
342 #endif
343 }
344
345 static int __fixup_cpu(void)
346 {
347         return 0;
348 }
349
350 int fixup_cpu(void) __attribute__((weak, alias("__fixup_cpu")));
351
352 /*
353  * This is the first part of the initialization sequence that is
354  * implemented in C, but still running from ROM.
355  *
356  * The main purpose is to provide a (serial) console interface as
357  * soon as possible (so we can see any error messages), and to
358  * initialize the RAM so that we can relocate the monitor code to
359  * RAM.
360  *
361  * Be aware of the restrictions: global data is read-only, BSS is not
362  * initialized, and stack space is limited to a few kB.
363  */
364
365 #ifdef CONFIG_LOGBUFFER
366 unsigned long logbuffer_base(void)
367 {
368         return CONFIG_SYS_SDRAM_BASE + get_effective_memsize() - LOGBUFF_LEN;
369 }
370 #endif
371
372 void board_init_f(ulong bootflag)
373 {
374         bd_t *bd;
375         ulong len, addr, addr_sp;
376         ulong *s;
377         gd_t *id;
378         init_fnc_t **init_fnc_ptr;
379
380 #ifdef CONFIG_PRAM
381         ulong reg;
382 #endif
383
384         /* Pointer is writable since we allocated a register for it */
385         gd = (gd_t *) (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET);
386         /* compiler optimization barrier needed for GCC >= 3.4 */
387         __asm__ __volatile__("":::"memory");
388
389 #if !defined(CONFIG_CPM2) && !defined(CONFIG_MPC512X) && \
390     !defined(CONFIG_MPC83xx) && !defined(CONFIG_MPC85xx) && \
391     !defined(CONFIG_MPC86xx)
392         /* Clear initial global data */
393         memset((void *) gd, 0, sizeof(gd_t));
394 #endif
395
396         for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr)
397                 if ((*init_fnc_ptr) () != 0)
398                         hang();
399
400 #ifdef CONFIG_POST
401         post_bootmode_init();
402         post_run(NULL, POST_ROM | post_bootmode_get(NULL));
403 #endif
404
405         WATCHDOG_RESET();
406
407         /*
408          * Now that we have DRAM mapped and working, we can
409          * relocate the code and continue running from DRAM.
410          *
411          * Reserve memory at end of RAM for (top down in that order):
412          *  - area that won't get touched by U-Boot and Linux (optional)
413          *  - kernel log buffer
414          *  - protected RAM
415          *  - LCD framebuffer
416          *  - monitor code
417          *  - board info struct
418          */
419         len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
420
421         /*
422          * Subtract specified amount of memory to hide so that it won't
423          * get "touched" at all by U-Boot. By fixing up gd->ram_size
424          * the Linux kernel should now get passed the now "corrected"
425          * memory size and won't touch it either. This should work
426          * for arch/ppc and arch/powerpc. Only Linux board ports in
427          * arch/powerpc with bootwrapper support, that recalculate the
428          * memory size from the SDRAM controller setup will have to
429          * get fixed.
430          */
431         gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
432
433         addr = CONFIG_SYS_SDRAM_BASE + get_effective_memsize();
434
435 #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
436         /*
437          * We need to make sure the location we intend to put secondary core
438          * boot code is reserved and not used by any part of u-boot
439          */
440         if (addr > determine_mp_bootpg(NULL)) {
441                 addr = determine_mp_bootpg(NULL);
442                 debug("Reserving MP boot page to %08lx\n", addr);
443         }
444 #endif
445
446 #ifdef CONFIG_LOGBUFFER
447 #ifndef CONFIG_ALT_LB_ADDR
448         /* reserve kernel log buffer */
449         addr -= (LOGBUFF_RESERVE);
450         debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
451               addr);
452 #endif
453 #endif
454
455 #ifdef CONFIG_PRAM
456         /*
457          * reserve protected RAM
458          */
459         reg = getenv_ulong("pram", 10, CONFIG_PRAM);
460         addr -= (reg << 10);    /* size is in kB */
461         debug("Reserving %ldk for protected RAM at %08lx\n", reg, addr);
462 #endif /* CONFIG_PRAM */
463
464         /* round down to next 4 kB limit */
465         addr &= ~(4096 - 1);
466         debug("Top of RAM usable for U-Boot at: %08lx\n", addr);
467
468 #ifdef CONFIG_LCD
469 #ifdef CONFIG_FB_ADDR
470         gd->fb_base = CONFIG_FB_ADDR;
471 #else
472         /* reserve memory for LCD display (always full pages) */
473         addr = lcd_setmem(addr);
474         gd->fb_base = addr;
475 #endif /* CONFIG_FB_ADDR */
476 #endif /* CONFIG_LCD */
477
478 #if defined(CONFIG_VIDEO) && defined(CONFIG_8xx)
479         /* reserve memory for video display (always full pages) */
480         addr = video_setmem(addr);
481         gd->fb_base = addr;
482 #endif /* CONFIG_VIDEO  */
483
484         /*
485          * reserve memory for U-Boot code, data & bss
486          * round down to next 4 kB limit
487          */
488         addr -= len;
489         addr &= ~(4096 - 1);
490 #ifdef CONFIG_E500
491         /* round down to next 64 kB limit so that IVPR stays aligned */
492         addr &= ~(65536 - 1);
493 #endif
494
495         debug("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
496
497         /*
498          * reserve memory for malloc() arena
499          */
500         addr_sp = addr - TOTAL_MALLOC_LEN;
501         debug("Reserving %dk for malloc() at: %08lx\n",
502               TOTAL_MALLOC_LEN >> 10, addr_sp);
503
504         /*
505          * (permanently) allocate a Board Info struct
506          * and a permanent copy of the "global" data
507          */
508         addr_sp -= sizeof(bd_t);
509         bd = (bd_t *) addr_sp;
510         memset(bd, 0, sizeof(bd_t));
511         gd->bd = bd;
512         debug("Reserving %zu Bytes for Board Info at: %08lx\n",
513               sizeof(bd_t), addr_sp);
514         addr_sp -= sizeof(gd_t);
515         id = (gd_t *) addr_sp;
516         debug("Reserving %zu Bytes for Global Data at: %08lx\n",
517               sizeof(gd_t), addr_sp);
518
519         /*
520          * Finally, we set up a new (bigger) stack.
521          *
522          * Leave some safety gap for SP, force alignment on 16 byte boundary
523          * Clear initial stack frame
524          */
525         addr_sp -= 16;
526         addr_sp &= ~0xF;
527         s = (ulong *) addr_sp;
528         *s = 0; /* Terminate back chain */
529         *++s = 0; /* NULL return address */
530         debug("Stack Pointer at: %08lx\n", addr_sp);
531
532         /*
533          * Save local variables to board info struct
534          */
535
536         bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;        /* start of memory */
537         bd->bi_memsize = gd->ram_size;                  /* size in bytes */
538
539 #ifdef CONFIG_SYS_SRAM_BASE
540         bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;        /* start of SRAM */
541         bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;         /* size  of SRAM */
542 #endif
543
544 #if defined(CONFIG_8xx) || defined(CONFIG_8260) || defined(CONFIG_5xx) || \
545     defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
546         bd->bi_immr_base = CONFIG_SYS_IMMR;     /* base  of IMMR register     */
547 #endif
548 #if defined(CONFIG_MPC5xxx)
549         bd->bi_mbar_base = CONFIG_SYS_MBAR;     /* base of internal registers */
550 #endif
551 #if defined(CONFIG_MPC83xx)
552         bd->bi_immrbar = CONFIG_SYS_IMMR;
553 #endif
554
555         WATCHDOG_RESET();
556         bd->bi_intfreq = gd->cpu_clk;   /* Internal Freq, in Hz */
557         bd->bi_busfreq = gd->bus_clk;   /* Bus Freq,      in Hz */
558 #if defined(CONFIG_CPM2)
559         bd->bi_cpmfreq = gd->arch.cpm_clk;
560         bd->bi_brgfreq = gd->arch.brg_clk;
561         bd->bi_sccfreq = gd->arch.scc_clk;
562         bd->bi_vco = gd->arch.vco_out;
563 #endif /* CONFIG_CPM2 */
564 #if defined(CONFIG_MPC512X)
565         bd->bi_ipsfreq = gd->arch.ips_clk;
566 #endif /* CONFIG_MPC512X */
567 #if defined(CONFIG_MPC5xxx)
568         bd->bi_ipbfreq = gd->arch.ipb_clk;
569         bd->bi_pcifreq = gd->pci_clk;
570 #endif /* CONFIG_MPC5xxx */
571         bd->bi_baudrate = gd->baudrate; /* Console Baudrate     */
572
573 #ifdef CONFIG_SYS_EXTBDINFO
574         strncpy((char *) bd->bi_s_version, "1.2", sizeof(bd->bi_s_version));
575         strncpy((char *) bd->bi_r_version, U_BOOT_VERSION,
576                 sizeof(bd->bi_r_version));
577
578         bd->bi_procfreq = gd->cpu_clk;  /* Processor Speed, In Hz */
579         bd->bi_plb_busfreq = gd->bus_clk;
580 #if defined(CONFIG_405GP) || defined(CONFIG_405EP) || \
581     defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
582     defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
583         bd->bi_pci_busfreq = get_PCI_freq();
584         bd->bi_opbfreq = get_OPB_freq();
585 #elif defined(CONFIG_XILINX_405)
586         bd->bi_pci_busfreq = get_PCI_freq();
587 #endif
588 #endif
589
590         debug("New Stack Pointer is: %08lx\n", addr_sp);
591
592         WATCHDOG_RESET();
593
594         gd->relocaddr = addr;   /* Store relocation addr, useful for debug */
595
596         memcpy(id, (void *) gd, sizeof(gd_t));
597
598         relocate_code(addr_sp, id, addr);
599
600         /* NOTREACHED - relocate_code() does not return */
601 }
602
603 /*
604  * This is the next part if the initialization sequence: we are now
605  * running from RAM and have a "normal" C environment, i. e. global
606  * data can be written, BSS has been cleared, the stack size in not
607  * that critical any more, etc.
608  */
609 void board_init_r(gd_t *id, ulong dest_addr)
610 {
611         bd_t *bd;
612         ulong malloc_start;
613
614 #ifndef CONFIG_SYS_NO_FLASH
615         ulong flash_size;
616 #endif
617
618         gd = id;                /* initialize RAM version of global data */
619         bd = gd->bd;
620
621         gd->flags |= GD_FLG_RELOC;      /* tell others: relocation done */
622
623         /* The Malloc area is immediately below the monitor copy in DRAM */
624         malloc_start = dest_addr - TOTAL_MALLOC_LEN;
625
626 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
627         /*
628          * The gd->arch.cpu pointer is set to an address in flash before
629          * relocation.  We need to update it to point to the same CPU entry
630          * in RAM.
631          */
632         gd->arch.cpu += dest_addr - CONFIG_SYS_MONITOR_BASE;
633
634         /*
635          * If we didn't know the cpu mask & # cores, we can save them of
636          * now rather than 'computing' them constantly
637          */
638         fixup_cpu();
639 #endif
640
641 #ifdef CONFIG_SYS_EXTRA_ENV_RELOC
642         /*
643          * Some systems need to relocate the env_addr pointer early because the
644          * location it points to will get invalidated before env_relocate is
645          * called.  One example is on systems that might use a L2 or L3 cache
646          * in SRAM mode and initialize that cache from SRAM mode back to being
647          * a cache in cpu_init_r.
648          */
649         gd->env_addr += dest_addr - CONFIG_SYS_MONITOR_BASE;
650 #endif
651
652         serial_initialize();
653
654         debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
655
656         WATCHDOG_RESET();
657
658         /*
659          * Setup trap handlers
660          */
661         trap_init(dest_addr);
662
663 #ifdef CONFIG_ADDR_MAP
664         init_addr_map();
665 #endif
666
667 #if defined(CONFIG_BOARD_EARLY_INIT_R)
668         board_early_init_r();
669 #endif
670
671         monitor_flash_len = (ulong)&__init_end - dest_addr;
672
673         WATCHDOG_RESET();
674
675 #ifdef CONFIG_LOGBUFFER
676         logbuff_init_ptrs();
677 #endif
678 #ifdef CONFIG_POST
679         post_output_backlog();
680 #endif
681
682         WATCHDOG_RESET();
683
684 #if defined(CONFIG_SYS_DELAYED_ICACHE)
685         icache_enable();        /* it's time to enable the instruction cache */
686 #endif
687
688 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
689         unlock_ram_in_cache();  /* it's time to unlock D-cache in e500 */
690 #endif
691
692 #if defined(CONFIG_PCI) && defined(CONFIG_SYS_EARLY_PCI_INIT)
693         /*
694          * Do early PCI configuration _before_ the flash gets initialised,
695          * because PCU ressources are crucial for flash access on some boards.
696          */
697         pci_init();
698 #endif
699 #if defined(CONFIG_WINBOND_83C553)
700         /*
701          * Initialise the ISA bridge
702          */
703         initialise_w83c553f();
704 #endif
705
706         asm("sync ; isync");
707
708         mem_malloc_init(malloc_start, TOTAL_MALLOC_LEN);
709
710 #if !defined(CONFIG_SYS_NO_FLASH)
711         puts("Flash: ");
712
713         if (board_flash_wp_on()) {
714                 printf("Uninitialized - Write Protect On\n");
715                 /* Since WP is on, we can't find real size.  Set to 0 */
716                 flash_size = 0;
717         } else if ((flash_size = flash_init()) > 0) {
718 #ifdef CONFIG_SYS_FLASH_CHECKSUM
719                 print_size(flash_size, "");
720                 /*
721                  * Compute and print flash CRC if flashchecksum is set to 'y'
722                  *
723                  * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
724                  */
725                 if (getenv_yesno("flashchecksum") == 1) {
726                         printf("  CRC: %08X",
727                                crc32(0,
728                                      (const unsigned char *)
729                                      CONFIG_SYS_FLASH_BASE, flash_size)
730                                 );
731                 }
732                 putc('\n');
733 #else  /* !CONFIG_SYS_FLASH_CHECKSUM */
734                 print_size(flash_size, "\n");
735 #endif /* CONFIG_SYS_FLASH_CHECKSUM */
736         } else {
737                 puts(failed);
738                 hang();
739         }
740
741         /* update start of FLASH memory    */
742         bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
743         /* size of FLASH memory (final value) */
744         bd->bi_flashsize = flash_size;
745
746 #if defined(CONFIG_SYS_UPDATE_FLASH_SIZE)
747         /* Make a update of the Memctrl. */
748         update_flash_size(flash_size);
749 #endif
750
751
752 #if defined(CONFIG_OXC) || defined(CONFIG_RMU)
753         /* flash mapped at end of memory map */
754         bd->bi_flashoffset = CONFIG_SYS_TEXT_BASE + flash_size;
755 #elif CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
756         bd->bi_flashoffset = monitor_flash_len; /* reserved area for monitor */
757 #endif
758 #endif /* !CONFIG_SYS_NO_FLASH */
759
760         WATCHDOG_RESET();
761
762         /* initialize higher level parts of CPU like time base and timers */
763         cpu_init_r();
764
765         WATCHDOG_RESET();
766
767 #ifdef CONFIG_SPI
768 #if !defined(CONFIG_ENV_IS_IN_EEPROM)
769         spi_init_f();
770 #endif
771         spi_init_r();
772 #endif
773
774 #if defined(CONFIG_CMD_NAND)
775         WATCHDOG_RESET();
776         puts("NAND:  ");
777         nand_init();            /* go init the NAND */
778 #endif
779
780 #ifdef CONFIG_GENERIC_MMC
781 /*
782  * MMC initialization is called before relocating env.
783  * Thus It is required that operations like pin multiplexer
784  * be put in board_init.
785  */
786         WATCHDOG_RESET();
787         puts("MMC:  ");
788         mmc_initialize(bd);
789 #endif
790
791         /* relocate environment function pointers etc. */
792         env_relocate();
793
794         /*
795          * after non-volatile devices & environment is setup and cpu code have
796          * another round to deal with any initialization that might require
797          * full access to the environment or loading of some image (firmware)
798          * from a non-volatile device
799          */
800         cpu_secondary_init_r();
801
802         /*
803          * Fill in missing fields of bd_info.
804          * We do this here, where we have "normal" access to the
805          * environment; we used to do this still running from ROM,
806          * where had to use getenv_f(), which can be pretty slow when
807          * the environment is in EEPROM.
808          */
809
810 #if defined(CONFIG_SYS_EXTBDINFO)
811 #if defined(CONFIG_405GP) || defined(CONFIG_405EP)
812 #if defined(CONFIG_I2CFAST)
813         /*
814          * set bi_iic_fast for linux taking environment variable
815          * "i2cfast" into account
816          */
817         {
818                 if (getenv_yesno("i2cfast") == 1) {
819                         bd->bi_iic_fast[0] = 1;
820                         bd->bi_iic_fast[1] = 1;
821                 }
822         }
823 #endif /* CONFIG_I2CFAST */
824 #endif /* CONFIG_405GP, CONFIG_405EP */
825 #endif /* CONFIG_SYS_EXTBDINFO */
826
827 #if defined(CONFIG_SC3)
828         sc3_read_eeprom();
829 #endif
830
831 #if defined(CONFIG_ID_EEPROM) || defined(CONFIG_SYS_I2C_MAC_OFFSET)
832         mac_read_from_eeprom();
833 #endif
834
835 #ifdef  CONFIG_HERMES
836         if ((gd->board_type >> 16) == 2)
837                 bd->bi_ethspeed = gd->board_type & 0xFFFF;
838         else
839                 bd->bi_ethspeed = 0xFFFF;
840 #endif
841
842 #ifdef CONFIG_CMD_NET
843         /* kept around for legacy kernels only ... ignore the next section */
844         eth_getenv_enetaddr("ethaddr", bd->bi_enetaddr);
845 #ifdef CONFIG_HAS_ETH1
846         eth_getenv_enetaddr("eth1addr", bd->bi_enet1addr);
847 #endif
848 #ifdef CONFIG_HAS_ETH2
849         eth_getenv_enetaddr("eth2addr", bd->bi_enet2addr);
850 #endif
851 #ifdef CONFIG_HAS_ETH3
852         eth_getenv_enetaddr("eth3addr", bd->bi_enet3addr);
853 #endif
854 #ifdef CONFIG_HAS_ETH4
855         eth_getenv_enetaddr("eth4addr", bd->bi_enet4addr);
856 #endif
857 #ifdef CONFIG_HAS_ETH5
858         eth_getenv_enetaddr("eth5addr", bd->bi_enet5addr);
859 #endif
860 #endif /* CONFIG_CMD_NET */
861
862         WATCHDOG_RESET();
863
864 #if defined(CONFIG_PCI) && !defined(CONFIG_SYS_EARLY_PCI_INIT)
865         /*
866          * Do pci configuration
867          */
868         pci_init();
869 #endif
870
871 /** leave this here (after malloc(), environment and PCI are working) **/
872         /* Initialize stdio devices */
873         stdio_init();
874
875         /* Initialize the jump table for applications */
876         jumptable_init();
877
878 #if defined(CONFIG_API)
879         /* Initialize API */
880         api_init();
881 #endif
882
883         /* Initialize the console (after the relocation and devices init) */
884         console_init_r();
885
886 #if defined(CONFIG_MISC_INIT_R)
887         /* miscellaneous platform dependent initialisations */
888         misc_init_r();
889 #endif
890
891 #ifdef  CONFIG_HERMES
892         if (bd->bi_ethspeed != 0xFFFF)
893                 hermes_start_lxt980((int) bd->bi_ethspeed);
894 #endif
895
896 #if defined(CONFIG_CMD_KGDB)
897         WATCHDOG_RESET();
898         puts("KGDB:  ");
899         kgdb_init();
900 #endif
901
902         debug("U-Boot relocated to %08lx\n", dest_addr);
903
904         /*
905          * Enable Interrupts
906          */
907         interrupt_init();
908
909 #if defined(CONFIG_STATUS_LED) && defined(STATUS_LED_BOOT)
910         status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
911 #endif
912
913         udelay(20);
914
915         /* Initialize from environment */
916         load_addr = getenv_ulong("loadaddr", 16, load_addr);
917
918         WATCHDOG_RESET();
919
920 #if defined(CONFIG_CMD_SCSI)
921         WATCHDOG_RESET();
922         puts("SCSI:  ");
923         scsi_init();
924 #endif
925
926 #if defined(CONFIG_CMD_DOC)
927         WATCHDOG_RESET();
928         puts("DOC:   ");
929         doc_init();
930 #endif
931
932 #ifdef CONFIG_BITBANGMII
933         bb_miiphy_init();
934 #endif
935 #if defined(CONFIG_CMD_NET)
936         WATCHDOG_RESET();
937         puts("Net:   ");
938         eth_initialize(bd);
939 #endif
940
941 #if defined(CONFIG_CMD_NET) && defined(CONFIG_RESET_PHY_R)
942         WATCHDOG_RESET();
943         debug("Reset Ethernet PHY\n");
944         reset_phy();
945 #endif
946
947 #ifdef CONFIG_POST
948         post_run(NULL, POST_RAM | post_bootmode_get(0));
949 #endif
950
951 #if defined(CONFIG_CMD_PCMCIA) \
952     && !defined(CONFIG_CMD_IDE)
953         WATCHDOG_RESET();
954         puts("PCMCIA:");
955         pcmcia_init();
956 #endif
957
958 #if defined(CONFIG_CMD_IDE)
959         WATCHDOG_RESET();
960 #ifdef  CONFIG_IDE_8xx_PCCARD
961         puts("PCMCIA:");
962 #else
963         puts("IDE:   ");
964 #endif
965 #if defined(CONFIG_START_IDE)
966         if (board_start_ide())
967                 ide_init();
968 #else
969         ide_init();
970 #endif
971 #endif
972
973 #ifdef CONFIG_LAST_STAGE_INIT
974         WATCHDOG_RESET();
975         /*
976          * Some parts can be only initialized if all others (like
977          * Interrupts) are up and running (i.e. the PC-style ISA
978          * keyboard).
979          */
980         last_stage_init();
981 #endif
982
983 #if defined(CONFIG_CMD_BEDBUG)
984         WATCHDOG_RESET();
985         bedbug_init();
986 #endif
987
988 #if defined(CONFIG_PRAM) || defined(CONFIG_LOGBUFFER)
989         /*
990          * Export available size of memory for Linux,
991          * taking into account the protected RAM at top of memory
992          */
993         {
994                 ulong pram = 0;
995                 char memsz[32];
996
997 #ifdef CONFIG_PRAM
998                 pram = getenv_ulong("pram", 10, CONFIG_PRAM);
999 #endif
1000 #ifdef CONFIG_LOGBUFFER
1001 #ifndef CONFIG_ALT_LB_ADDR
1002                 /* Also take the logbuffer into account (pram is in kB) */
1003                 pram += (LOGBUFF_LEN + LOGBUFF_OVERHEAD) / 1024;
1004 #endif
1005 #endif
1006                 sprintf(memsz, "%ldk", (bd->bi_memsize / 1024) - pram);
1007                 setenv("mem", memsz);
1008         }
1009 #endif
1010
1011 #ifdef CONFIG_PS2KBD
1012         puts("PS/2:  ");
1013         kbd_init();
1014 #endif
1015
1016 #ifdef CONFIG_MODEM_SUPPORT
1017         {
1018                 extern int do_mdm_init;
1019
1020                 do_mdm_init = gd->do_mdm_init;
1021         }
1022 #endif
1023
1024         /* Initialization complete - start the monitor */
1025
1026         /* main_loop() can return to retry autoboot, if so just run it again. */
1027         for (;;) {
1028                 WATCHDOG_RESET();
1029                 main_loop();
1030         }
1031
1032         /* NOTREACHED - no way out of command loop except booting */
1033 }
1034
1035 #if 0   /* We could use plain global data, but the resulting code is bigger */
1036 /*
1037  * Pointer to initial global data area
1038  *
1039  * Here we initialize it.
1040  */
1041 #undef  XTRN_DECLARE_GLOBAL_DATA_PTR
1042 #define XTRN_DECLARE_GLOBAL_DATA_PTR    /* empty = allocate here */
1043 DECLARE_GLOBAL_DATA_PTR =
1044         (gd_t *) (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET);
1045 #endif /* 0 */
1046
1047 /************************************************************************/