Merge branch 'master' of rsync://rsync.denx.de/git/u-boot
[platform/kernel/u-boot.git] / lib_m68k / board.c
1 /*
2  * (C) Copyright 2003
3  * Josef Baumgartner <josef.baumgartner@telex.de>
4  *
5  * (C) Copyright 2000-2002
6  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26
27 #include <common.h>
28 #include <watchdog.h>
29 #include <command.h>
30 #include <malloc.h>
31 #include <devices.h>
32
33 #ifdef  CONFIG_M5272
34 #include <asm/immap_5272.h>
35 #endif
36
37 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
38 #include <ide.h>
39 #endif
40 #if (CONFIG_COMMANDS & CFG_CMD_SCSI)
41 #include <scsi.h>
42 #endif
43 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
44 #include <kgdb.h>
45 #endif
46 #ifdef CONFIG_STATUS_LED
47 #include <status_led.h>
48 #endif
49 #include <net.h>
50 #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
51 #include <cmd_bedbug.h>
52 #endif
53 #ifdef CFG_ALLOC_DPRAM
54 #include <commproc.h>
55 #endif
56 #include <version.h>
57
58 #if defined(CONFIG_HARD_I2C) || \
59     defined(CONFIG_SOFT_I2C)
60 #include <i2c.h>
61 #endif
62
63 DECLARE_GLOBAL_DATA_PTR;
64
65 static char *failed = "*** failed ***\n";
66
67 #ifdef  CONFIG_PCU_E
68 extern flash_info_t flash_info[];
69 #endif
70
71 #include <environment.h>
72
73 #if ( ((CFG_ENV_ADDR+CFG_ENV_SIZE) < CFG_MONITOR_BASE) || \
74       (CFG_ENV_ADDR >= (CFG_MONITOR_BASE + CFG_MONITOR_LEN)) ) || \
75     defined(CFG_ENV_IS_IN_NVRAM)
76 #define TOTAL_MALLOC_LEN        (CFG_MALLOC_LEN + CFG_ENV_SIZE)
77 #else
78 #define TOTAL_MALLOC_LEN        CFG_MALLOC_LEN
79 #endif
80
81 extern ulong __init_end;
82 extern ulong _end;
83
84 extern  void timer_init(void);
85
86 #if defined(CONFIG_WATCHDOG)
87 # define INIT_FUNC_WATCHDOG_INIT        watchdog_init,
88 # define WATCHDOG_DISABLE               watchdog_disable
89
90 extern int watchdog_init(void);
91 extern int watchdog_disable(void);
92 #else
93 # define INIT_FUNC_WATCHDOG_INIT        /* undef */
94 # define WATCHDOG_DISABLE               /* undef */
95 #endif /* CONFIG_WATCHDOG */
96
97 ulong monitor_flash_len;
98
99 /*
100  * Begin and End of memory area for malloc(), and current "brk"
101  */
102 static  ulong   mem_malloc_start = 0;
103 static  ulong   mem_malloc_end   = 0;
104 static  ulong   mem_malloc_brk   = 0;
105
106 /************************************************************************
107  * Utilities                                                            *
108  ************************************************************************
109  */
110
111 /*
112  * The Malloc area is immediately below the monitor copy in DRAM
113  */
114 static void mem_malloc_init (void)
115 {
116         ulong dest_addr = CFG_MONITOR_BASE + gd->reloc_off;
117
118         mem_malloc_end = dest_addr;
119         mem_malloc_start = dest_addr - TOTAL_MALLOC_LEN;
120         mem_malloc_brk = mem_malloc_start;
121
122         memset ((void *) mem_malloc_start,
123                 0,
124                 mem_malloc_end - mem_malloc_start);
125 }
126
127 void *sbrk (ptrdiff_t increment)
128 {
129         ulong old = mem_malloc_brk;
130         ulong new = old + increment;
131
132         if ((new < mem_malloc_start) ||
133             (new > mem_malloc_end) ) {
134                 return (NULL);
135         }
136         mem_malloc_brk = new;
137         return ((void *)old);
138 }
139
140 char *strmhz(char *buf, long hz)
141 {
142     long l, n;
143     long m;
144
145     n = hz / 1000000L;
146
147     l = sprintf (buf, "%ld", n);
148
149     m = (hz % 1000000L) / 1000L;
150
151     if (m != 0)
152         sprintf (buf+l, ".%03ld", m);
153
154     return (buf);
155 }
156
157 /*
158  * All attempts to come up with a "common" initialization sequence
159  * that works for all boards and architectures failed: some of the
160  * requirements are just _too_ different. To get rid of the resulting
161  * mess of board dependend #ifdef'ed code we now make the whole
162  * initialization sequence configurable to the user.
163  *
164  * The requirements for any new initalization function is simple: it
165  * receives a pointer to the "global data" structure as it's only
166  * argument, and returns an integer return code, where 0 means
167  * "continue" and != 0 means "fatal error, hang the system".
168  */
169 typedef int (init_fnc_t) (void);
170
171 /************************************************************************
172  * Init Utilities                                                       *
173  ************************************************************************
174  * Some of this code should be moved into the core functions,
175  * but let's get it working (again) first...
176  */
177
178 static int init_baudrate (void)
179 {
180         uchar tmp[64];  /* long enough for environment variables */
181         int i = getenv_r ("baudrate", tmp, sizeof (tmp));
182
183         gd->baudrate = (i > 0)
184                         ? (int) simple_strtoul (tmp, NULL, 10)
185                         : CONFIG_BAUDRATE;
186         return (0);
187 }
188
189 /***********************************************************************/
190
191 static int init_func_ram (void)
192 {
193         int board_type = 0;     /* use dummy arg */
194         puts ("DRAM:  ");
195
196         if ((gd->ram_size = initdram (board_type)) > 0) {
197                 print_size (gd->ram_size, "\n");
198                 return (0);
199         }
200         puts (failed);
201         return (1);
202 }
203
204 /***********************************************************************/
205
206 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
207 static int init_func_i2c (void)
208 {
209         puts ("I2C:   ");
210         i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
211         puts ("ready\n");
212         return (0);
213 }
214 #endif
215
216 /***********************************************************************/
217
218 /************************************************************************
219  * Initialization sequence                                              *
220  ************************************************************************
221  */
222
223 init_fnc_t *init_sequence[] = {
224         env_init,
225         init_baudrate,
226         serial_init,
227         console_init_f,
228         display_options,
229         checkcpu,
230         checkboard,
231 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
232         init_func_i2c,
233 #endif
234         init_func_ram,
235 #if defined(CFG_DRAM_TEST)
236         testdram,
237 #endif /* CFG_DRAM_TEST */
238         INIT_FUNC_WATCHDOG_INIT
239         NULL,                   /* Terminate this list */
240 };
241
242
243 /************************************************************************
244  *
245  * This is the first part of the initialization sequence that is
246  * implemented in C, but still running from ROM.
247  *
248  * The main purpose is to provide a (serial) console interface as
249  * soon as possible (so we can see any error messages), and to
250  * initialize the RAM so that we can relocate the monitor code to
251  * RAM.
252  *
253  * Be aware of the restrictions: global data is read-only, BSS is not
254  * initialized, and stack space is limited to a few kB.
255  *
256  ************************************************************************
257  */
258
259 void
260 board_init_f (ulong bootflag)
261 {
262         bd_t *bd;
263         ulong len, addr, addr_sp;
264         gd_t *id;
265         init_fnc_t **init_fnc_ptr;
266 #ifdef CONFIG_PRAM
267         int i;
268         ulong reg;
269         uchar tmp[64];          /* long enough for environment variables */
270 #endif
271
272         /* Pointer is writable since we allocated a register for it */
273         gd = (gd_t *) (CFG_INIT_RAM_ADDR + CFG_GBL_DATA_OFFSET);
274         /* compiler optimization barrier needed for GCC >= 3.4 */
275         __asm__ __volatile__("": : :"memory");
276
277         /* Clear initial global data */
278         memset ((void *) gd, 0, sizeof (gd_t));
279
280         for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
281                 if ((*init_fnc_ptr)() != 0) {
282                         hang ();
283                 }
284         }
285
286         /*
287          * Now that we have DRAM mapped and working, we can
288          * relocate the code and continue running from DRAM.
289          *
290          * Reserve memory at end of RAM for (top down in that order):
291          *      - protected RAM
292          *      - LCD framebuffer
293          *      - monitor code
294          *      - board info struct
295          */
296         len = (ulong)&_end - CFG_MONITOR_BASE;
297
298         addr = CFG_SDRAM_BASE + gd->ram_size;
299
300 #ifdef CONFIG_LOGBUFFER
301         /* reserve kernel log buffer */
302         addr -= (LOGBUFF_RESERVE);
303         debug ("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN, addr);
304 #endif
305
306 #ifdef CONFIG_PRAM
307         /*
308          * reserve protected RAM
309          */
310         i = getenv_r ("pram", tmp, sizeof (tmp));
311         reg = (i > 0) ? simple_strtoul (tmp, NULL, 10) : CONFIG_PRAM;
312         addr -= (reg << 10);            /* size is in kB */
313         debug ("Reserving %ldk for protected RAM at %08lx\n", reg, addr);
314 #endif /* CONFIG_PRAM */
315
316         /*
317          * reserve memory for U-Boot code, data & bss
318          * round down to next 4 kB limit
319          */
320         addr -= len;
321         addr &= ~(4096 - 1);
322
323         debug ("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
324
325         /*
326          * reserve memory for malloc() arena
327          */
328         addr_sp = addr - TOTAL_MALLOC_LEN;
329         debug ("Reserving %dk for malloc() at: %08lx\n",
330                         TOTAL_MALLOC_LEN >> 10, addr_sp);
331
332         /*
333          * (permanently) allocate a Board Info struct
334          * and a permanent copy of the "global" data
335          */
336         addr_sp -= sizeof (bd_t);
337         bd = (bd_t *) addr_sp;
338         gd->bd = bd;
339         debug ("Reserving %d Bytes for Board Info at: %08lx\n",
340                         sizeof (bd_t), addr_sp);
341         addr_sp -= sizeof (gd_t);
342         id = (gd_t *) addr_sp;
343         debug ("Reserving %d Bytes for Global Data at: %08lx\n",
344                         sizeof (gd_t), addr_sp);
345
346         /* Reserve memory for boot params. */
347         addr_sp -= CFG_BOOTPARAMS_LEN;
348         bd->bi_boot_params = addr_sp;
349         debug ("Reserving %dk for boot parameters at: %08lx\n",
350                         CFG_BOOTPARAMS_LEN >> 10, addr_sp);
351
352         /*
353          * Finally, we set up a new (bigger) stack.
354          *
355          * Leave some safety gap for SP, force alignment on 16 byte boundary
356          * Clear initial stack frame
357          */
358         addr_sp -= 16;
359         addr_sp &= ~0xF;
360         *((ulong *) addr_sp)-- = 0;
361         *((ulong *) addr_sp)-- = 0;
362         debug ("Stack Pointer at: %08lx\n", addr_sp);
363
364         /*
365          * Save local variables to board info struct
366          */
367         bd->bi_memstart  = CFG_SDRAM_BASE;      /* start of  DRAM memory      */
368         bd->bi_memsize   = gd->ram_size;        /* size  of  DRAM memory in bytes */
369         bd->bi_mbar_base = CFG_MBAR;            /* base of internal registers */
370
371         bd->bi_bootflags = bootflag;            /* boot / reboot flag (for LynxOS)    */
372
373         WATCHDOG_RESET ();
374         bd->bi_intfreq = gd->cpu_clk;   /* Internal Freq, in Hz */
375         bd->bi_busfreq = gd->bus_clk;   /* Bus Freq,      in Hz */
376         bd->bi_baudrate = gd->baudrate; /* Console Baudrate     */
377
378 #ifdef CFG_EXTBDINFO
379         strncpy (bd->bi_s_version, "1.2", sizeof (bd->bi_s_version));
380         strncpy (bd->bi_r_version, U_BOOT_VERSION, sizeof (bd->bi_r_version));
381 #endif
382
383         WATCHDOG_RESET ();
384
385 #ifdef CONFIG_POST
386         post_bootmode_init();
387         post_run (NULL, POST_ROM | post_bootmode_get(0));
388 #endif
389
390         WATCHDOG_RESET();
391
392         memcpy (id, (void *)gd, sizeof (gd_t));
393
394         debug ("Start relocate of code from %08x to %08lx\n", CFG_MONITOR_BASE, addr);
395         relocate_code (addr_sp, id, addr);
396
397         /* NOTREACHED - jump_to_ram() does not return */
398 }
399
400 /************************************************************************
401  *
402  * This is the next part if the initialization sequence: we are now
403  * running from RAM and have a "normal" C environment, i. e. global
404  * data can be written, BSS has been cleared, the stack size in not
405  * that critical any more, etc.
406  *
407  ************************************************************************
408  */
409 void board_init_r (gd_t *id, ulong dest_addr)
410 {
411         cmd_tbl_t *cmdtp;
412         char *s, *e;
413         bd_t *bd;
414         int i;
415         extern void malloc_bin_reloc (void);
416
417 #ifndef CFG_ENV_IS_NOWHERE
418         extern char * env_name_spec;
419 #endif
420 #ifndef CFG_NO_FLASH
421         ulong flash_size;
422 #endif
423         gd = id;                /* initialize RAM version of global data */
424         bd = gd->bd;
425
426         gd->flags |= GD_FLG_RELOC;      /* tell others: relocation done */
427
428         debug ("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
429
430         WATCHDOG_RESET ();
431
432         gd->reloc_off =  dest_addr - CFG_MONITOR_BASE;
433
434         monitor_flash_len = (ulong)&__init_end - dest_addr;
435
436         /*
437          * We have to relocate the command table manually
438          */
439         for (cmdtp = &__u_boot_cmd_start; cmdtp !=  &__u_boot_cmd_end; cmdtp++) {
440                 ulong addr;
441                 addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
442 #if 0
443                 printf ("Command \"%s\": 0x%08lx => 0x%08lx\n",
444                                 cmdtp->name, (ulong) (cmdtp->cmd), addr);
445 #endif
446                 cmdtp->cmd =
447                         (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;
448
449                 addr = (ulong)(cmdtp->name) + gd->reloc_off;
450                 cmdtp->name = (char *)addr;
451
452                 if (cmdtp->usage) {
453                         addr = (ulong)(cmdtp->usage) + gd->reloc_off;
454                         cmdtp->usage = (char *)addr;
455                 }
456 #ifdef  CFG_LONGHELP
457                 if (cmdtp->help) {
458                         addr = (ulong)(cmdtp->help) + gd->reloc_off;
459                         cmdtp->help = (char *)addr;
460                 }
461 #endif
462         }
463         /* there are some other pointer constants we must deal with */
464 #ifndef CFG_ENV_IS_NOWHERE
465         env_name_spec += gd->reloc_off;
466 #endif
467
468         WATCHDOG_RESET ();
469
470 #ifdef CONFIG_LOGBUFFER
471         logbuff_init_ptrs ();
472 #endif
473 #ifdef CONFIG_POST
474         post_output_backlog ();
475         post_reloc ();
476 #endif
477         WATCHDOG_RESET();
478
479 #if 0
480         /* instruction cache enabled in cpu_init_f() for faster relocation */
481         icache_enable ();       /* it's time to enable the instruction cache */
482 #endif
483
484         /*
485          * Setup trap handlers
486          */
487         trap_init (0);
488
489 #if !defined(CFG_NO_FLASH)
490         puts ("FLASH: ");
491
492         if ((flash_size = flash_init ()) > 0) {
493 # ifdef CFG_FLASH_CHECKSUM
494                 print_size (flash_size, "");
495                 /*
496                  * Compute and print flash CRC if flashchecksum is set to 'y'
497                  *
498                  * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
499                  */
500                 s = getenv ("flashchecksum");
501                 if (s && (*s == 'y')) {
502                         printf ("  CRC: %08lX",
503                                         crc32 (0,
504                                                    (const unsigned char *) CFG_FLASH_BASE,
505                                                    flash_size)
506                                         );
507                 }
508                 putc ('\n');
509 # else  /* !CFG_FLASH_CHECKSUM */
510                 print_size (flash_size, "\n");
511 # endif /* CFG_FLASH_CHECKSUM */
512         } else {
513                 puts (failed);
514                 hang ();
515         }
516
517         bd->bi_flashstart = CFG_FLASH_BASE;     /* update start of FLASH memory    */
518         bd->bi_flashsize = flash_size;  /* size of FLASH memory (final value) */
519         bd->bi_flashoffset = 0;
520 #else   /* CFG_NO_FLASH */
521         bd->bi_flashsize = 0;
522         bd->bi_flashstart = 0;
523         bd->bi_flashoffset = 0;
524 #endif /* !CFG_NO_FLASH */
525
526         WATCHDOG_RESET ();
527
528         /* initialize higher level parts of CPU like time base and timers */
529         cpu_init_r ();
530
531         WATCHDOG_RESET ();
532
533         /* initialize malloc() area */
534         mem_malloc_init ();
535         malloc_bin_reloc ();
536
537 #ifdef CONFIG_SPI
538 # if !defined(CFG_ENV_IS_IN_EEPROM)
539         spi_init_f ();
540 # endif
541         spi_init_r ();
542 #endif
543
544         /* relocate environment function pointers etc. */
545         env_relocate ();
546
547         /*
548          * Fill in missing fields of bd_info.
549          * We do this here, where we have "normal" access to the
550          * environment; we used to do this still running from ROM,
551          * where had to use getenv_r(), which can be pretty slow when
552          * the environment is in EEPROM.
553          */
554         s = getenv ("ethaddr");
555         for (i = 0; i < 6; ++i) {
556                 bd->bi_enetaddr[i] = s ? simple_strtoul (s, &e, 16) : 0;
557                 if (s)
558                         s = (*e) ? e + 1 : e;
559         }
560
561         /* IP Address */
562         bd->bi_ip_addr = getenv_IPaddr ("ipaddr");
563
564         WATCHDOG_RESET ();
565
566
567         /** leave this here (after malloc(), environment and PCI are working) **/
568         /* Initialize devices */
569         devices_init ();
570
571         /* Initialize the jump table for applications */
572         jumptable_init ();
573
574         /* Initialize the console (after the relocation and devices init) */
575         console_init_r ();
576
577 #if defined(CONFIG_MISC_INIT_R)
578         /* miscellaneous platform dependent initialisations */
579         misc_init_r ();
580 #endif
581
582 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
583         WATCHDOG_RESET ();
584         puts ("KGDB:  ");
585         kgdb_init ();
586 #endif
587
588         debug ("U-Boot relocated to %08lx\n", dest_addr);
589
590         /*
591          * Enable Interrupts
592          */
593         interrupt_init ();
594
595         /* Must happen after interrupts are initialized since
596          * an irq handler gets installed
597          */
598         timer_init();
599
600 #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
601         serial_buffered_init();
602 #endif
603
604 #ifdef CONFIG_STATUS_LED
605         status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
606 #endif
607
608         udelay (20);
609
610         set_timer (0);
611
612         /* Insert function pointers now that we have relocated the code */
613
614         /* Initialize from environment */
615         if ((s = getenv ("loadaddr")) != NULL) {
616                 load_addr = simple_strtoul (s, NULL, 16);
617         }
618 #if (CONFIG_COMMANDS & CFG_CMD_NET)
619         if ((s = getenv ("bootfile")) != NULL) {
620                 copy_filename (BootFile, s, sizeof (BootFile));
621         }
622 #endif /* CFG_CMD_NET */
623
624         WATCHDOG_RESET ();
625
626 #if (CONFIG_COMMANDS & CFG_CMD_DOC)
627         WATCHDOG_RESET ();
628         puts ("DOC:   ");
629         doc_init ();
630 #endif
631
632 #if (CONFIG_COMMANDS & CFG_CMD_NAND)
633         WATCHDOG_RESET ();
634         puts ("NAND:");
635         nand_init();            /* go init the NAND */
636 #endif
637
638 #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(FEC_ENET)
639         WATCHDOG_RESET();
640         eth_init(bd);
641 #endif
642
643 #ifdef CONFIG_POST
644         post_run (NULL, POST_RAM | post_bootmode_get(0));
645 #endif
646
647 #ifdef CONFIG_LAST_STAGE_INIT
648         WATCHDOG_RESET ();
649         /*
650          * Some parts can be only initialized if all others (like
651          * Interrupts) are up and running (i.e. the PC-style ISA
652          * keyboard).
653          */
654         last_stage_init ();
655 #endif
656
657 #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
658         WATCHDOG_RESET ();
659         bedbug_init ();
660 #endif
661
662 #if defined(CONFIG_PRAM) || defined(CONFIG_LOGBUFFER)
663         /*
664          * Export available size of memory for Linux,
665          * taking into account the protected RAM at top of memory
666          */
667         {
668                 ulong pram;
669                 uchar memsz[32];
670 #ifdef CONFIG_PRAM
671                 char *s;
672
673                 if ((s = getenv ("pram")) != NULL) {
674                         pram = simple_strtoul (s, NULL, 10);
675                 } else {
676                         pram = CONFIG_PRAM;
677                 }
678 #else
679                 pram=0;
680 #endif
681 #ifdef CONFIG_LOGBUFFER
682                 /* Also take the logbuffer into account (pram is in kB) */
683                 pram += (LOGBUFF_LEN+LOGBUFF_OVERHEAD)/1024;
684 #endif
685                 sprintf (memsz, "%ldk", (bd->bi_memsize / 1024) - pram);
686                 setenv ("mem", memsz);
687         }
688 #endif
689
690 #ifdef CONFIG_MODEM_SUPPORT
691  {
692          extern int do_mdm_init;
693          do_mdm_init = gd->do_mdm_init;
694  }
695 #endif
696
697 #ifdef CONFIG_WATCHDOG
698         /* disable watchdog if environment is set */
699         if ((s = getenv ("watchdog")) != NULL) {
700                 if (strncmp (s, "off", 3) == 0) {
701                         WATCHDOG_DISABLE ();
702                 }
703         }
704 #endif /* CONFIG_WATCHDOG*/
705
706
707         /* Initialization complete - start the monitor */
708
709         /* main_loop() can return to retry autoboot, if so just run it again. */
710         for (;;) {
711                 WATCHDOG_RESET ();
712                 main_loop ();
713         }
714
715         /* NOTREACHED - no way out of command loop except booting */
716 }
717
718
719 void hang(void)
720 {
721         puts ("### ERROR ### Please RESET the board ###\n");
722         for (;;);
723 }