Merge 'next' branch
[platform/kernel/u-boot.git] / lib_sh / board.c
1 /*
2  * Copyright (C) 2007,2008
3  * Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA
19  */
20
21 #include <common.h>
22 #include <command.h>
23 #include <malloc.h>
24 #include <devices.h>
25 #include <version.h>
26 #include <watchdog.h>
27 #include <net.h>
28 #include <environment.h>
29
30 extern void malloc_bin_reloc (void);
31 extern int cpu_init(void);
32 extern int board_init(void);
33 extern int dram_init(void);
34 extern int timer_init(void);
35
36 const char version_string[] = U_BOOT_VERSION" (" __DATE__ " - " __TIME__ ")";
37
38 unsigned long monitor_flash_len = CONFIG_SYS_MONITOR_LEN;
39
40 static unsigned long mem_malloc_start;
41 static unsigned long mem_malloc_end;
42 static unsigned long mem_malloc_brk;
43
44 static void mem_malloc_init(void)
45 {
46
47         mem_malloc_start = (TEXT_BASE - CONFIG_SYS_GBL_DATA_SIZE - CONFIG_SYS_MALLOC_LEN);
48         mem_malloc_end = (mem_malloc_start + CONFIG_SYS_MALLOC_LEN - 16);
49         mem_malloc_brk = mem_malloc_start;
50         memset((void *) mem_malloc_start, 0,
51                 (mem_malloc_end - mem_malloc_start));
52 }
53
54 void *sbrk(ptrdiff_t increment)
55 {
56         unsigned long old = mem_malloc_brk;
57         unsigned long new = old + increment;
58
59         if ((new < mem_malloc_start) ||
60             (new > mem_malloc_end)) {
61                 return NULL;
62         }
63
64         mem_malloc_brk = new;
65         return (void *) old;
66 }
67
68 static int sh_flash_init(void)
69 {
70         DECLARE_GLOBAL_DATA_PTR;
71
72         gd->bd->bi_flashsize = flash_init();
73         printf("FLASH: %ldMB\n", gd->bd->bi_flashsize / (1024*1024));
74
75         return 0;
76 }
77
78 #if defined(CONFIG_CMD_NAND)
79 # include <nand.h>
80 # define INIT_FUNC_NAND_INIT nand_init,
81 #else
82 # define INIT_FUNC_NAND_INIT
83 #endif /* CONFIG_CMD_NAND */
84
85 #if defined(CONFIG_WATCHDOG)
86 extern int watchdog_init(void);
87 extern int watchdog_disable(void);
88 # define INIT_FUNC_WATCHDOG_INIT        watchdog_init,
89 # define WATCHDOG_DISABLE               watchdog_disable
90 #else
91 # define INIT_FUNC_WATCHDOG_INIT
92 # define WATCHDOG_DISABLE
93 #endif /* CONFIG_WATCHDOG */
94
95 #if defined(CONFIG_CMD_IDE)
96 # include <ide.h>
97 # define INIT_FUNC_IDE_INIT     ide_init,
98 #else
99 # define INIT_FUNC_IDE_INIT
100 #endif /* CONFIG_CMD_IDE */
101
102 #if defined(CONFIG_PCI)
103 #include <pci.h>
104 static int sh_pci_init(void)
105 {
106         pci_init();
107         return 0;
108 }
109 # define INIT_FUNC_PCI_INIT sh_pci_init,
110 #else
111 # define INIT_FUNC_PCI_INIT
112 #endif /* CONFIG_PCI */
113
114 static int sh_mem_env_init(void)
115 {
116         mem_malloc_init();
117         malloc_bin_reloc();
118         env_relocate();
119         jumptable_init();
120         return 0;
121 }
122
123 #if defined(CONFIG_CMD_NET)
124 static int sh_net_init(void)
125 {
126         DECLARE_GLOBAL_DATA_PTR;
127         char *s, *e;
128         int i;
129
130         gd->bd->bi_ip_addr = getenv_IPaddr("ipaddr");
131         s = getenv("ethaddr");
132         for (i = 0; i < 6; ++i) {
133                 gd->bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
134                 if (s)
135                         s = (*e) ? e + 1 : e;
136         }
137
138         return 0;
139 }
140 #endif
141
142 typedef int (init_fnc_t) (void);
143
144 init_fnc_t *init_sequence[] =
145 {
146         cpu_init,               /* basic cpu dependent setup */
147         board_init,             /* basic board dependent setup */
148         interrupt_init, /* set up exceptions */
149         env_init,               /* event init */
150         serial_init,    /* SCIF init */
151         INIT_FUNC_WATCHDOG_INIT /* watchdog init */
152         console_init_f,
153         display_options,
154         checkcpu,
155         checkboard,             /* Check support board */
156         dram_init,              /* SDRAM init */
157         timer_init,             /* SuperH Timer (TCNT0 only) init */
158         sh_flash_init,  /* Flash memory(NOR) init*/
159         sh_mem_env_init,
160         INIT_FUNC_NAND_INIT/* Flash memory (NAND) init */
161         INIT_FUNC_PCI_INIT      /* PCI init */
162         devices_init,
163         console_init_r,
164         interrupt_init,
165 #ifdef BOARD_LATE_INIT
166         board_late_init,
167 #endif
168 #if defined(CONFIG_CMD_NET)
169         sh_net_init,            /* SH specific eth init */
170 #endif
171         NULL                    /* Terminate this list */
172 };
173
174 void sh_generic_init(void)
175 {
176         DECLARE_GLOBAL_DATA_PTR;
177
178         bd_t *bd;
179         init_fnc_t **init_fnc_ptr;
180
181         memset(gd, 0, CONFIG_SYS_GBL_DATA_SIZE);
182
183         gd->flags |= GD_FLG_RELOC;      /* tell others: relocation done */
184
185         gd->bd = (bd_t *)(gd + 1);      /* At end of global data */
186         gd->baudrate = CONFIG_BAUDRATE;
187
188         gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
189
190         bd = gd->bd;
191         bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;
192         bd->bi_memsize = CONFIG_SYS_SDRAM_SIZE;
193         bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
194 #if defined(CONFIG_SYS_SRAM_BASE) && defined(CONFIG_SYS_SRAM_SIZE)
195         bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;
196         bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;
197 #endif
198         bd->bi_baudrate = CONFIG_BAUDRATE;
199
200         for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
201                 WATCHDOG_RESET();
202                 if ((*init_fnc_ptr) () != 0)
203                         hang();
204         }
205
206 #ifdef CONFIG_WATCHDOG
207         /* disable watchdog if environment is set */
208         {
209                 char *s = getenv("watchdog");
210                 if (s != NULL)
211                         if (strncmp(s, "off", 3) == 0)
212                                 WATCHDOG_DISABLE();
213         }
214 #endif /* CONFIG_WATCHDOG*/
215
216
217 #if defined(CONFIG_CMD_NET)
218         {
219                 char *s;
220                 puts("Net:   ");
221                 eth_initialize(gd->bd);
222
223                 s = getenv("bootfile");
224                 if (s != NULL)
225                         copy_filename(BootFile, s, sizeof(BootFile));
226         }
227 #endif /* CONFIG_CMD_NET */
228
229         while (1) {
230                 WATCHDOG_RESET();
231                 main_loop();
232         }
233 }
234
235 /***********************************************************************/
236
237 void hang(void)
238 {
239         puts("Board ERROR\n");
240         for (;;)
241                 ;
242 }