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