d6c02d8a4ced882676116e7f88b0e801152b95eb
[oweals/u-boot.git] / lib_nios / board.c
1 /*
2  * (C) Copyright 2003, Psyent Corporation <www.psyent.com>
3  * Scott McNutt <smcnutt@psyent.com>
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 <devices.h>
29 #include <watchdog.h>
30 #include <net.h>
31 #ifdef CONFIG_STATUS_LED
32 #include <status_led.h>
33 #endif
34
35
36 /*
37  * All attempts to come up with a "common" initialization sequence
38  * that works for all boards and architectures failed: some of the
39  * requirements are just _too_ different. To get rid of the resulting
40  * mess of board dependend #ifdef'ed code we now make the whole
41  * initialization sequence configurable to the user.
42  *
43  * The requirements for any new initalization function is simple: it
44  * receives a pointer to the "global data" structure as it's only
45  * argument, and returns an integer return code, where 0 means
46  * "continue" and != 0 means "fatal error, hang the system".
47  */
48
49
50 extern void malloc_bin_reloc (void);
51 typedef int (init_fnc_t) (void);
52 extern unsigned _vectors[];
53
54 /*
55  * Begin and End of memory area for malloc(), and current "brk"
56  */
57 static  ulong   mem_malloc_start = 0;
58 static  ulong   mem_malloc_end   = 0;
59 static  ulong   mem_malloc_brk   = 0;
60
61 /*
62  * The Malloc area is immediately below the monitor copy in RAM
63  */
64 static void mem_malloc_init (void)
65 {
66         mem_malloc_start = CFG_MALLOC_BASE;
67         mem_malloc_end = mem_malloc_start + CFG_MALLOC_LEN;
68         mem_malloc_brk = mem_malloc_start;
69         memset ((void *) mem_malloc_start,
70                 0,
71                 mem_malloc_end - mem_malloc_start);
72 }
73
74 void *sbrk (ptrdiff_t increment)
75 {
76         ulong old = mem_malloc_brk;
77         ulong new = old + increment;
78
79         if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
80                 return (NULL);
81         }
82         mem_malloc_brk = new;
83         return ((void *) old);
84 }
85
86
87 /************************************************************************
88  * Initialization sequence                                              *
89  ***********************************************************************/
90
91 init_fnc_t *init_sequence[] = {
92
93 #if defined(CONFIG_BOARD_EARLY_INIT_F)
94         board_early_init_f,     /* Call board-specific init code early.*/
95 #endif
96
97         env_init,
98         serial_init,
99         console_init_f,
100         display_options,
101         checkcpu,
102         checkboard,
103         NULL,                   /* Terminate this list */
104 };
105
106
107 /***********************************************************************/
108 void board_init (void)
109 {
110         DECLARE_GLOBAL_DATA_PTR;
111
112         bd_t *bd;
113         init_fnc_t **init_fnc_ptr;
114         char *s, *e;
115         int i;
116
117         /* Pointer is writable since we allocated a register for it.
118          * Nios treats CFG_GBL_DATA_OFFSET as an address.
119          */
120         gd = (gd_t *)CFG_GBL_DATA_OFFSET;
121         memset( gd, 0, CFG_GBL_DATA_SIZE );
122
123         /* Copy exception vectors to the correct location.
124          */
125         memcpy( (void *)CFG_VECT_BASE, _vectors, 256 );
126
127         gd->bd = (bd_t *)(gd+1);        /* At end of global data */
128         gd->baudrate = CONFIG_BAUDRATE;
129         gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
130
131         bd = gd->bd;
132         bd->bi_memstart = CFG_SDRAM_BASE;
133         bd->bi_memsize = CFG_SDRAM_SIZE;
134         bd->bi_flashstart = CFG_FLASH_BASE;
135 #if     defined(CFG_SRAM_BASE) && defined(CFG_SRAM_SIZE)
136         bd->bi_sramstart= CFG_SRAM_BASE;
137         bd->bi_sramsize = CFG_SRAM_SIZE;
138 #endif
139         bd->bi_baudrate = CONFIG_BAUDRATE;
140
141         for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
142                 WATCHDOG_RESET ();
143                 if ((*init_fnc_ptr) () != 0) {
144                         hang ();
145                 }
146         }
147
148         WATCHDOG_RESET ();
149         bd->bi_flashsize = flash_init();
150
151         WATCHDOG_RESET ();
152         mem_malloc_init();
153         malloc_bin_reloc();
154         env_relocate();
155
156         bd->bi_ip_addr = getenv_IPaddr ("ipaddr");
157         s = getenv ("ethaddr");
158         for (i = 0; i < 6; ++i) {
159                 bd->bi_enetaddr[i] = s ? simple_strtoul (s, &e, 16) : 0;
160                 if (s) s = (*e) ? e + 1 : e;
161         }
162
163         WATCHDOG_RESET ();
164         devices_init();
165         jumptable_init();
166         console_init_r();
167         /*
168          */
169
170         WATCHDOG_RESET ();
171         interrupt_init ();
172
173 #ifdef CONFIG_STATUS_LED
174         status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
175 #endif
176
177         /* main_loop */
178         for (;;) {
179                 WATCHDOG_RESET ();
180                 main_loop ();
181         }
182 }
183
184
185 /***********************************************************************/
186
187 void hang (void)
188 {
189 #ifdef CONFIG_STATUS_LED
190         status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
191         status_led_set(STATUS_LED_RED, STATUS_LED_BLINKING);
192 #endif
193         puts("### ERROR ### Please reset board ###\n");
194         for (;;);
195 }