Merge remote branch 'u-boot-avr32/master'
[oweals/u-boot.git] / lib_arm / board.c
1 /*
2  * (C) Copyright 2002-2006
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * (C) Copyright 2002
6  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7  * Marius Groeger <mgroeger@sysgo.de>
8  *
9  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27
28 /*
29  * To match the U-Boot user interface on ARM platforms to the U-Boot
30  * standard (as on PPC platforms), some messages with debug character
31  * are removed from the default U-Boot build.
32  *
33  * Define DEBUG here if you want additional info as shown below
34  * printed upon startup:
35  *
36  * U-Boot code: 00F00000 -> 00F3C774  BSS: -> 00FC3274
37  * IRQ Stack: 00ebff7c
38  * FIQ Stack: 00ebef7c
39  */
40
41 #include <common.h>
42 #include <command.h>
43 #include <malloc.h>
44 #include <devices.h>
45 #include <version.h>
46 #include <net.h>
47 #include <serial.h>
48 #include <nand.h>
49 #include <onenand_uboot.h>
50
51 #ifdef CONFIG_DRIVER_SMC91111
52 #include "../drivers/net/smc91111.h"
53 #endif
54 #ifdef CONFIG_DRIVER_LAN91C96
55 #include "../drivers/net/lan91c96.h"
56 #endif
57
58 DECLARE_GLOBAL_DATA_PTR;
59
60 ulong monitor_flash_len;
61
62 #ifdef CONFIG_HAS_DATAFLASH
63 extern int  AT91F_DataflashInit(void);
64 extern void dataflash_print_info(void);
65 #endif
66
67 #ifndef CONFIG_IDENT_STRING
68 #define CONFIG_IDENT_STRING ""
69 #endif
70
71 const char version_string[] =
72         U_BOOT_VERSION" (" __DATE__ " - " __TIME__ ")"CONFIG_IDENT_STRING;
73
74 #ifdef CONFIG_DRIVER_CS8900
75 extern void cs8900_get_enetaddr (uchar * addr);
76 #endif
77
78 #ifdef CONFIG_DRIVER_RTL8019
79 extern void rtl8019_get_enetaddr (uchar * addr);
80 #endif
81
82 #if defined(CONFIG_HARD_I2C) || \
83     defined(CONFIG_SOFT_I2C)
84 #include <i2c.h>
85 #endif
86
87 /*
88  * Begin and End of memory area for malloc(), and current "brk"
89  */
90 static ulong mem_malloc_start = 0;
91 static ulong mem_malloc_end = 0;
92 static ulong mem_malloc_brk = 0;
93
94 static
95 void mem_malloc_init (ulong dest_addr)
96 {
97         mem_malloc_start = dest_addr;
98         mem_malloc_end = dest_addr + CFG_MALLOC_LEN;
99         mem_malloc_brk = mem_malloc_start;
100
101         memset ((void *) mem_malloc_start, 0,
102                         mem_malloc_end - mem_malloc_start);
103 }
104
105 void *sbrk (ptrdiff_t increment)
106 {
107         ulong old = mem_malloc_brk;
108         ulong new = old + increment;
109
110         if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
111                 return (NULL);
112         }
113         mem_malloc_brk = new;
114
115         return ((void *) old);
116 }
117
118 /************************************************************************
119  * Coloured LED functionality
120  ************************************************************************
121  * May be supplied by boards if desired
122  */
123 void inline __coloured_LED_init (void) {}
124 void inline coloured_LED_init (void) __attribute__((weak, alias("__coloured_LED_init")));
125 void inline __red_LED_on (void) {}
126 void inline red_LED_on (void) __attribute__((weak, alias("__red_LED_on")));
127 void inline __red_LED_off(void) {}
128 void inline red_LED_off(void)        __attribute__((weak, alias("__red_LED_off")));
129 void inline __green_LED_on(void) {}
130 void inline green_LED_on(void) __attribute__((weak, alias("__green_LED_on")));
131 void inline __green_LED_off(void) {}
132 void inline green_LED_off(void)__attribute__((weak, alias("__green_LED_off")));
133 void inline __yellow_LED_on(void) {}
134 void inline yellow_LED_on(void)__attribute__((weak, alias("__yellow_LED_on")));
135 void inline __yellow_LED_off(void) {}
136 void inline yellow_LED_off(void)__attribute__((weak, alias("__yellow_LED_off")));
137
138 /************************************************************************
139  * Init Utilities                                                       *
140  ************************************************************************
141  * Some of this code should be moved into the core functions,
142  * or dropped completely,
143  * but let's get it working (again) first...
144  */
145
146 static int init_baudrate (void)
147 {
148         char tmp[64];   /* long enough for environment variables */
149         int i = getenv_r ("baudrate", tmp, sizeof (tmp));
150         gd->bd->bi_baudrate = gd->baudrate = (i > 0)
151                         ? (int) simple_strtoul (tmp, NULL, 10)
152                         : CONFIG_BAUDRATE;
153
154         return (0);
155 }
156
157 static int display_banner (void)
158 {
159         printf ("\n\n%s\n\n", version_string);
160         debug ("U-Boot code: %08lX -> %08lX  BSS: -> %08lX\n",
161                _armboot_start, _bss_start, _bss_end);
162 #ifdef CONFIG_MODEM_SUPPORT
163         debug ("Modem Support enabled\n");
164 #endif
165 #ifdef CONFIG_USE_IRQ
166         debug ("IRQ Stack: %08lx\n", IRQ_STACK_START);
167         debug ("FIQ Stack: %08lx\n", FIQ_STACK_START);
168 #endif
169
170         return (0);
171 }
172
173 /*
174  * WARNING: this code looks "cleaner" than the PowerPC version, but
175  * has the disadvantage that you either get nothing, or everything.
176  * On PowerPC, you might see "DRAM: " before the system hangs - which
177  * gives a simple yet clear indication which part of the
178  * initialization if failing.
179  */
180 static int display_dram_config (void)
181 {
182         int i;
183
184 #ifdef DEBUG
185         puts ("RAM Configuration:\n");
186
187         for(i=0; i<CONFIG_NR_DRAM_BANKS; i++) {
188                 printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
189                 print_size (gd->bd->bi_dram[i].size, "\n");
190         }
191 #else
192         ulong size = 0;
193
194         for (i=0; i<CONFIG_NR_DRAM_BANKS; i++) {
195                 size += gd->bd->bi_dram[i].size;
196         }
197         puts("DRAM:  ");
198         print_size(size, "\n");
199 #endif
200
201         return (0);
202 }
203
204 #ifndef CFG_NO_FLASH
205 static void display_flash_config (ulong size)
206 {
207         puts ("Flash: ");
208         print_size (size, "\n");
209 }
210 #endif /* CFG_NO_FLASH */
211
212 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
213 static int init_func_i2c (void)
214 {
215         puts ("I2C:   ");
216         i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
217         puts ("ready\n");
218         return (0);
219 }
220 #endif
221
222 /*
223  * Breathe some life into the board...
224  *
225  * Initialize a serial port as console, and carry out some hardware
226  * tests.
227  *
228  * The first part of initialization is running from Flash memory;
229  * its main purpose is to initialize the RAM so that we
230  * can relocate the monitor code to RAM.
231  */
232
233 /*
234  * All attempts to come up with a "common" initialization sequence
235  * that works for all boards and architectures failed: some of the
236  * requirements are just _too_ different. To get rid of the resulting
237  * mess of board dependent #ifdef'ed code we now make the whole
238  * initialization sequence configurable to the user.
239  *
240  * The requirements for any new initalization function is simple: it
241  * receives a pointer to the "global data" structure as it's only
242  * argument, and returns an integer return code, where 0 means
243  * "continue" and != 0 means "fatal error, hang the system".
244  */
245 typedef int (init_fnc_t) (void);
246
247 int print_cpuinfo (void); /* test-only */
248
249 init_fnc_t *init_sequence[] = {
250         cpu_init,               /* basic cpu dependent setup */
251         board_init,             /* basic board dependent setup */
252         interrupt_init,         /* set up exceptions */
253         env_init,               /* initialize environment */
254         init_baudrate,          /* initialze baudrate settings */
255         serial_init,            /* serial communications setup */
256         console_init_f,         /* stage 1 init of console */
257         display_banner,         /* say that we are here */
258 #if defined(CONFIG_DISPLAY_CPUINFO)
259         print_cpuinfo,          /* display cpu info (and speed) */
260 #endif
261 #if defined(CONFIG_DISPLAY_BOARDINFO)
262         checkboard,             /* display board info */
263 #endif
264 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
265         init_func_i2c,
266 #endif
267         dram_init,              /* configure available RAM banks */
268         display_dram_config,
269         NULL,
270 };
271
272 void start_armboot (void)
273 {
274         init_fnc_t **init_fnc_ptr;
275         char *s;
276 #ifndef CFG_NO_FLASH
277         ulong size;
278 #endif
279 #if defined(CONFIG_VFD) || defined(CONFIG_LCD)
280         unsigned long addr;
281 #endif
282
283         /* Pointer is writable since we allocated a register for it */
284         gd = (gd_t*)(_armboot_start - CFG_MALLOC_LEN - sizeof(gd_t));
285         /* compiler optimization barrier needed for GCC >= 3.4 */
286         __asm__ __volatile__("": : :"memory");
287
288         memset ((void*)gd, 0, sizeof (gd_t));
289         gd->bd = (bd_t*)((char*)gd - sizeof(bd_t));
290         memset (gd->bd, 0, sizeof (bd_t));
291
292         monitor_flash_len = _bss_start - _armboot_start;
293
294         for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
295                 if ((*init_fnc_ptr)() != 0) {
296                         hang ();
297                 }
298         }
299
300 #ifndef CFG_NO_FLASH
301         /* configure available FLASH banks */
302         size = flash_init ();
303         display_flash_config (size);
304 #endif /* CFG_NO_FLASH */
305
306 #ifdef CONFIG_VFD
307 #       ifndef PAGE_SIZE
308 #         define PAGE_SIZE 4096
309 #       endif
310         /*
311          * reserve memory for VFD display (always full pages)
312          */
313         /* bss_end is defined in the board-specific linker script */
314         addr = (_bss_end + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
315         size = vfd_setmem (addr);
316         gd->fb_base = addr;
317 #endif /* CONFIG_VFD */
318
319 #ifdef CONFIG_LCD
320 #       ifndef PAGE_SIZE
321 #         define PAGE_SIZE 4096
322 #       endif
323         /*
324          * reserve memory for LCD display (always full pages)
325          */
326         /* bss_end is defined in the board-specific linker script */
327         addr = (_bss_end + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
328         size = lcd_setmem (addr);
329         gd->fb_base = addr;
330 #endif /* CONFIG_LCD */
331
332         /* armboot_start is defined in the board-specific linker script */
333         mem_malloc_init (_armboot_start - CFG_MALLOC_LEN);
334
335 #if defined(CONFIG_CMD_NAND)
336         puts ("NAND:  ");
337         nand_init();            /* go init the NAND */
338 #endif
339
340 #if defined(CONFIG_CMD_ONENAND)
341         onenand_init();
342 #endif
343
344 #ifdef CONFIG_HAS_DATAFLASH
345         AT91F_DataflashInit();
346         dataflash_print_info();
347 #endif
348
349         /* initialize environment */
350         env_relocate ();
351
352 #ifdef CONFIG_VFD
353         /* must do this after the framebuffer is allocated */
354         drv_vfd_init();
355 #endif /* CONFIG_VFD */
356
357 #ifdef CONFIG_SERIAL_MULTI
358         serial_initialize();
359 #endif
360
361         /* IP Address */
362         gd->bd->bi_ip_addr = getenv_IPaddr ("ipaddr");
363
364         /* MAC Address */
365         {
366                 int i;
367                 ulong reg;
368                 char *s, *e;
369                 char tmp[64];
370
371                 i = getenv_r ("ethaddr", tmp, sizeof (tmp));
372                 s = (i > 0) ? tmp : NULL;
373
374                 for (reg = 0; reg < 6; ++reg) {
375                         gd->bd->bi_enetaddr[reg] = s ? simple_strtoul (s, &e, 16) : 0;
376                         if (s)
377                                 s = (*e) ? e + 1 : e;
378                 }
379
380 #ifdef CONFIG_HAS_ETH1
381                 i = getenv_r ("eth1addr", tmp, sizeof (tmp));
382                 s = (i > 0) ? tmp : NULL;
383
384                 for (reg = 0; reg < 6; ++reg) {
385                         gd->bd->bi_enet1addr[reg] = s ? simple_strtoul (s, &e, 16) : 0;
386                         if (s)
387                                 s = (*e) ? e + 1 : e;
388                 }
389 #endif
390         }
391
392         devices_init ();        /* get the devices list going. */
393
394 #ifdef CONFIG_CMC_PU2
395         load_sernum_ethaddr ();
396 #endif /* CONFIG_CMC_PU2 */
397
398         jumptable_init ();
399
400         console_init_r ();      /* fully init console as a device */
401
402 #if defined(CONFIG_MISC_INIT_R)
403         /* miscellaneous platform dependent initialisations */
404         misc_init_r ();
405 #endif
406
407         /* enable exceptions */
408         enable_interrupts ();
409
410         /* Perform network card initialisation if necessary */
411 #ifdef CONFIG_DRIVER_TI_EMAC
412 extern void dm644x_eth_set_mac_addr (const u_int8_t *addr);
413         if (getenv ("ethaddr")) {
414                 dm644x_eth_set_mac_addr(gd->bd->bi_enetaddr);
415         }
416 #endif
417
418 #ifdef CONFIG_DRIVER_CS8900
419         cs8900_get_enetaddr (gd->bd->bi_enetaddr);
420 #endif
421
422 #if defined(CONFIG_DRIVER_SMC91111) || defined (CONFIG_DRIVER_LAN91C96)
423         if (getenv ("ethaddr")) {
424                 smc_set_mac_addr(gd->bd->bi_enetaddr);
425         }
426 #endif /* CONFIG_DRIVER_SMC91111 || CONFIG_DRIVER_LAN91C96 */
427
428         /* Initialize from environment */
429         if ((s = getenv ("loadaddr")) != NULL) {
430                 load_addr = simple_strtoul (s, NULL, 16);
431         }
432 #if defined(CONFIG_CMD_NET)
433         if ((s = getenv ("bootfile")) != NULL) {
434                 copy_filename (BootFile, s, sizeof (BootFile));
435         }
436 #endif
437
438 #ifdef BOARD_LATE_INIT
439         board_late_init ();
440 #endif
441 #if defined(CONFIG_CMD_NET)
442 #if defined(CONFIG_NET_MULTI)
443         puts ("Net:   ");
444 #endif
445         eth_initialize(gd->bd);
446 #if defined(CONFIG_RESET_PHY_R)
447         debug ("Reset Ethernet PHY\n");
448         reset_phy();
449 #endif
450 #endif
451         /* main_loop() can return to retry autoboot, if so just run it again. */
452         for (;;) {
453                 main_loop ();
454         }
455
456         /* NOTREACHED - no way out of command loop except booting */
457 }
458
459 void hang (void)
460 {
461         puts ("### ERROR ### Please RESET the board ###\n");
462         for (;;);
463 }
464
465 #ifdef CONFIG_MODEM_SUPPORT
466 static inline void mdm_readline(char *buf, int bufsiz);
467
468 /* called from main loop (common/main.c) */
469 extern void  dbg(const char *fmt, ...);
470 int mdm_init (void)
471 {
472         char env_str[16];
473         char *init_str;
474         int i;
475         extern char console_buffer[];
476         extern void enable_putc(void);
477         extern int hwflow_onoff(int);
478
479         enable_putc(); /* enable serial_putc() */
480
481 #ifdef CONFIG_HWFLOW
482         init_str = getenv("mdm_flow_control");
483         if (init_str && (strcmp(init_str, "rts/cts") == 0))
484                 hwflow_onoff (1);
485         else
486                 hwflow_onoff(-1);
487 #endif
488
489         for (i = 1;;i++) {
490                 sprintf(env_str, "mdm_init%d", i);
491                 if ((init_str = getenv(env_str)) != NULL) {
492                         serial_puts(init_str);
493                         serial_puts("\n");
494                         for(;;) {
495                                 mdm_readline(console_buffer, CFG_CBSIZE);
496                                 dbg("ini%d: [%s]", i, console_buffer);
497
498                                 if ((strcmp(console_buffer, "OK") == 0) ||
499                                         (strcmp(console_buffer, "ERROR") == 0)) {
500                                         dbg("ini%d: cmd done", i);
501                                         break;
502                                 } else /* in case we are originating call ... */
503                                         if (strncmp(console_buffer, "CONNECT", 7) == 0) {
504                                                 dbg("ini%d: connect", i);
505                                                 return 0;
506                                         }
507                         }
508                 } else
509                         break; /* no init string - stop modem init */
510
511                 udelay(100000);
512         }
513
514         udelay(100000);
515
516         /* final stage - wait for connect */
517         for(;i > 1;) { /* if 'i' > 1 - wait for connection
518                                   message from modem */
519                 mdm_readline(console_buffer, CFG_CBSIZE);
520                 dbg("ini_f: [%s]", console_buffer);
521                 if (strncmp(console_buffer, "CONNECT", 7) == 0) {
522                         dbg("ini_f: connected");
523                         return 0;
524                 }
525         }
526
527         return 0;
528 }
529
530 /* 'inline' - We have to do it fast */
531 static inline void mdm_readline(char *buf, int bufsiz)
532 {
533         char c;
534         char *p;
535         int n;
536
537         n = 0;
538         p = buf;
539         for(;;) {
540                 c = serial_getc();
541
542                 /*              dbg("(%c)", c); */
543
544                 switch(c) {
545                 case '\r':
546                         break;
547                 case '\n':
548                         *p = '\0';
549                         return;
550
551                 default:
552                         if(n++ > bufsiz) {
553                                 *p = '\0';
554                                 return; /* sanity check */
555                         }
556                         *p = c;
557                         p++;
558                         break;
559                 }
560         }
561 }
562 #endif  /* CONFIG_MODEM_SUPPORT */