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