Merge branch 'fixes' into cleanups
[platform/kernel/u-boot.git] / lib_avr32 / board.c
1 /*
2  * Copyright (C) 2004-2006 Atmel Corporation
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22 #include <common.h>
23 #include <command.h>
24 #include <malloc.h>
25 #include <devices.h>
26 #include <timestamp.h>
27 #include <version.h>
28 #include <net.h>
29
30 #include <asm/initcalls.h>
31 #include <asm/sections.h>
32
33 #ifndef CONFIG_IDENT_STRING
34 #define CONFIG_IDENT_STRING ""
35 #endif
36
37 DECLARE_GLOBAL_DATA_PTR;
38
39 const char version_string[] =
40         U_BOOT_VERSION " ("U_BOOT_DATE" - "U_BOOT_TIME") " CONFIG_IDENT_STRING;
41
42 unsigned long monitor_flash_len;
43
44 /*
45  * Begin and end of memory area for malloc(), and current "brk"
46  */
47 static unsigned long mem_malloc_start = 0;
48 static unsigned long mem_malloc_end = 0;
49 static unsigned long mem_malloc_brk = 0;
50
51 /* Weak aliases for optional board functions */
52 static int __do_nothing(void)
53 {
54         return 0;
55 }
56 int board_postclk_init(void) __attribute__((weak, alias("__do_nothing")));
57 int board_early_init_r(void) __attribute__((weak, alias("__do_nothing")));
58
59 /* The malloc area is right below the monitor image in RAM */
60 static void mem_malloc_init(void)
61 {
62         unsigned long monitor_addr;
63
64         monitor_addr = CONFIG_SYS_MONITOR_BASE + gd->reloc_off;
65         mem_malloc_end = monitor_addr;
66         mem_malloc_start = mem_malloc_end - CONFIG_SYS_MALLOC_LEN;
67         mem_malloc_brk = mem_malloc_start;
68
69         printf("malloc: Using memory from 0x%08lx to 0x%08lx\n",
70                mem_malloc_start, mem_malloc_end);
71
72         memset ((void *)mem_malloc_start, 0,
73                 mem_malloc_end - mem_malloc_start);
74 }
75
76 void *sbrk(ptrdiff_t increment)
77 {
78         unsigned long old = mem_malloc_brk;
79         unsigned long new = old + increment;
80
81         if ((new < mem_malloc_start) || (new > mem_malloc_end))
82                 return NULL;
83
84         mem_malloc_brk = new;
85         return ((void *)old);
86 }
87
88 #ifdef CONFIG_SYS_DMA_ALLOC_LEN
89 #include <asm/cacheflush.h>
90 #include <asm/io.h>
91
92 static unsigned long dma_alloc_start;
93 static unsigned long dma_alloc_end;
94 static unsigned long dma_alloc_brk;
95
96 static void dma_alloc_init(void)
97 {
98         unsigned long monitor_addr;
99
100         monitor_addr = CONFIG_SYS_MONITOR_BASE + gd->reloc_off;
101         dma_alloc_end = monitor_addr - CONFIG_SYS_MALLOC_LEN;
102         dma_alloc_start = dma_alloc_end - CONFIG_SYS_DMA_ALLOC_LEN;
103         dma_alloc_brk = dma_alloc_start;
104
105         printf("DMA: Using memory from 0x%08lx to 0x%08lx\n",
106                dma_alloc_start, dma_alloc_end);
107
108         dcache_invalidate_range(cached(dma_alloc_start),
109                                 dma_alloc_end - dma_alloc_start);
110 }
111
112 void *dma_alloc_coherent(size_t len, unsigned long *handle)
113 {
114         unsigned long paddr = dma_alloc_brk;
115
116         if (dma_alloc_brk + len > dma_alloc_end)
117                 return NULL;
118
119         dma_alloc_brk = ((paddr + len + CONFIG_SYS_DCACHE_LINESZ - 1)
120                          & ~(CONFIG_SYS_DCACHE_LINESZ - 1));
121
122         *handle = paddr;
123         return uncached(paddr);
124 }
125 #else
126 static inline void dma_alloc_init(void)
127 {
128
129 }
130 #endif
131
132 static int init_baudrate(void)
133 {
134         char tmp[64];
135         int i;
136
137         i = getenv_r("baudrate", tmp, sizeof(tmp));
138         if (i > 0) {
139                 gd->baudrate = simple_strtoul(tmp, NULL, 10);
140         } else {
141                 gd->baudrate = CONFIG_BAUDRATE;
142         }
143         return 0;
144 }
145
146
147 static int display_banner (void)
148 {
149         printf ("\n\n%s\n\n", version_string);
150         printf ("U-Boot code: %p -> %p  data: %p -> %p\n",
151                 _text, _etext, _data, _end);
152         return 0;
153 }
154
155 void hang(void)
156 {
157         for (;;) ;
158 }
159
160 static int display_dram_config (void)
161 {
162         int i;
163
164         puts ("DRAM Configuration:\n");
165
166         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
167                 printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
168                 print_size (gd->bd->bi_dram[i].size, "\n");
169         }
170
171         return 0;
172 }
173
174 static void display_flash_config (void)
175 {
176         puts ("Flash: ");
177         print_size(gd->bd->bi_flashsize, " ");
178         printf("at address 0x%08lx\n", gd->bd->bi_flashstart);
179 }
180
181 void board_init_f(ulong board_type)
182 {
183         gd_t gd_data;
184         gd_t *new_gd;
185         bd_t *bd;
186         unsigned long *new_sp;
187         unsigned long monitor_len;
188         unsigned long monitor_addr;
189         unsigned long addr;
190         long sdram_size;
191
192         /* Initialize the global data pointer */
193         memset(&gd_data, 0, sizeof(gd_data));
194         gd = &gd_data;
195
196         /* Perform initialization sequence */
197         board_early_init_f();
198         cpu_init();
199         board_postclk_init();
200         env_init();
201         init_baudrate();
202         serial_init();
203         console_init_f();
204         display_banner();
205         sdram_size = initdram(board_type);
206
207         /* If we have no SDRAM, we can't go on */
208         if (sdram_size <= 0)
209                 panic("No working SDRAM available\n");
210
211         /*
212          * Now that we have DRAM mapped and working, we can
213          * relocate the code and continue running from DRAM.
214          *
215          * Reserve memory at end of RAM for (top down in that order):
216          *  - u-boot image
217          *  - heap for malloc()
218          *  - board info struct
219          *  - global data struct
220          *  - stack
221          */
222         addr = CONFIG_SYS_SDRAM_BASE + sdram_size;
223         monitor_len = _end - _text;
224
225         /*
226          * Reserve memory for u-boot code, data and bss.
227          * Round down to next 4 kB limit.
228          */
229         addr -= monitor_len;
230         addr &= ~(4096UL - 1);
231         monitor_addr = addr;
232
233         /* Reserve memory for malloc() */
234         addr -= CONFIG_SYS_MALLOC_LEN;
235
236 #ifdef CONFIG_SYS_DMA_ALLOC_LEN
237         /* Reserve DMA memory (must be cache aligned) */
238         addr &= ~(CONFIG_SYS_DCACHE_LINESZ - 1);
239         addr -= CONFIG_SYS_DMA_ALLOC_LEN;
240 #endif
241
242         /* Allocate a Board Info struct on a word boundary */
243         addr -= sizeof(bd_t);
244         addr &= ~3UL;
245         gd->bd = bd = (bd_t *)addr;
246
247         /* Allocate a new global data copy on a 8-byte boundary. */
248         addr -= sizeof(gd_t);
249         addr &= ~7UL;
250         new_gd = (gd_t *)addr;
251
252         /* And finally, a new, bigger stack. */
253         new_sp = (unsigned long *)addr;
254         gd->stack_end = addr;
255         *(--new_sp) = 0;
256         *(--new_sp) = 0;
257
258         /*
259          * Initialize the board information struct with the
260          * information we have.
261          */
262         bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
263         bd->bi_dram[0].size = sdram_size;
264         bd->bi_baudrate = gd->baudrate;
265
266         memcpy(new_gd, gd, sizeof(gd_t));
267
268         relocate_code((unsigned long)new_sp, new_gd, monitor_addr);
269 }
270
271 void board_init_r(gd_t *new_gd, ulong dest_addr)
272 {
273         extern void malloc_bin_reloc (void);
274 #ifndef CONFIG_ENV_IS_NOWHERE
275         extern char * env_name_spec;
276 #endif
277         char *s;
278         cmd_tbl_t *cmdtp;
279         bd_t *bd;
280
281         gd = new_gd;
282         bd = gd->bd;
283
284         gd->flags |= GD_FLG_RELOC;
285         gd->reloc_off = dest_addr - CONFIG_SYS_MONITOR_BASE;
286
287         board_early_init_r();
288
289         monitor_flash_len = _edata - _text;
290
291         /*
292          * We have to relocate the command table manually
293          */
294         for (cmdtp = &__u_boot_cmd_start;
295              cmdtp !=  &__u_boot_cmd_end; cmdtp++) {
296                 unsigned long addr;
297
298                 addr = (unsigned long)cmdtp->cmd + gd->reloc_off;
299                 cmdtp->cmd = (typeof(cmdtp->cmd))addr;
300
301                 addr = (unsigned long)cmdtp->name + gd->reloc_off;
302                 cmdtp->name = (typeof(cmdtp->name))addr;
303
304                 if (cmdtp->usage) {
305                         addr = (unsigned long)cmdtp->usage + gd->reloc_off;
306                         cmdtp->usage = (typeof(cmdtp->usage))addr;
307                 }
308 #ifdef CONFIG_SYS_LONGHELP
309                 if (cmdtp->help) {
310                         addr = (unsigned long)cmdtp->help + gd->reloc_off;
311                         cmdtp->help = (typeof(cmdtp->help))addr;
312                 }
313 #endif
314         }
315
316         /* there are some other pointer constants we must deal with */
317 #ifndef CONFIG_ENV_IS_NOWHERE
318         env_name_spec += gd->reloc_off;
319 #endif
320
321         timer_init();
322         mem_malloc_init();
323         malloc_bin_reloc();
324         dma_alloc_init();
325
326         enable_interrupts();
327
328         bd->bi_flashstart = 0;
329         bd->bi_flashsize = 0;
330         bd->bi_flashoffset = 0;
331
332 #ifndef CONFIG_SYS_NO_FLASH
333         bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
334         bd->bi_flashsize = flash_init();
335         bd->bi_flashoffset = (unsigned long)_edata - (unsigned long)_text;
336
337         if (bd->bi_flashsize)
338                 display_flash_config();
339 #endif
340
341         if (bd->bi_dram[0].size)
342                 display_dram_config();
343
344         gd->bd->bi_boot_params = malloc(CONFIG_SYS_BOOTPARAMS_LEN);
345         if (!gd->bd->bi_boot_params)
346                 puts("WARNING: Cannot allocate space for boot parameters\n");
347
348         /* initialize environment */
349         env_relocate();
350
351         bd->bi_ip_addr = getenv_IPaddr ("ipaddr");
352
353         devices_init();
354         jumptable_init();
355         console_init_r();
356
357         s = getenv("loadaddr");
358         if (s)
359                 load_addr = simple_strtoul(s, NULL, 16);
360
361 #if defined(CONFIG_CMD_NET)
362         s = getenv("bootfile");
363         if (s)
364                 copy_filename(BootFile, s, sizeof(BootFile));
365 #if defined(CONFIG_NET_MULTI)
366         puts("Net:   ");
367 #endif
368         eth_initialize(gd->bd);
369 #endif
370
371         for (;;) {
372                 main_loop();
373         }
374 }