]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - lib_avr32/board.c
AVR32: Use initdram() instead of board_init_memories()
[karo-tx-uboot.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 <version.h>
27 #include <net.h>
28
29 #include <asm/initcalls.h>
30 #include <asm/sections.h>
31
32 #ifndef CONFIG_IDENT_STRING
33 #define CONFIG_IDENT_STRING ""
34 #endif
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 const char version_string[] =
39         U_BOOT_VERSION " (" __DATE__ " - " __TIME__ ") " CONFIG_IDENT_STRING;
40
41 unsigned long monitor_flash_len;
42
43 /*
44  * Begin and end of memory area for malloc(), and current "brk"
45  */
46 static unsigned long mem_malloc_start = 0;
47 static unsigned long mem_malloc_end = 0;
48 static unsigned long mem_malloc_brk = 0;
49
50 /* The malloc area is right below the monitor image in RAM */
51 static void mem_malloc_init(void)
52 {
53         unsigned long monitor_addr;
54
55         monitor_addr = CFG_MONITOR_BASE + gd->reloc_off;
56         mem_malloc_end = monitor_addr;
57         mem_malloc_start = mem_malloc_end - CFG_MALLOC_LEN;
58         mem_malloc_brk = mem_malloc_start;
59
60         printf("malloc: Using memory from 0x%08lx to 0x%08lx\n",
61                mem_malloc_start, mem_malloc_end);
62
63         memset ((void *)mem_malloc_start, 0,
64                 mem_malloc_end - mem_malloc_start);
65 }
66
67 void *sbrk(ptrdiff_t increment)
68 {
69         unsigned long old = mem_malloc_brk;
70         unsigned long new = old + increment;
71
72         if ((new < mem_malloc_start) || (new > mem_malloc_end))
73                 return NULL;
74
75         mem_malloc_brk = new;
76         return ((void *)old);
77 }
78
79 static int init_baudrate(void)
80 {
81         char tmp[64];
82         int i;
83
84         i = getenv_r("baudrate", tmp, sizeof(tmp));
85         if (i > 0) {
86                 gd->baudrate = simple_strtoul(tmp, NULL, 10);
87         } else {
88                 gd->baudrate = CONFIG_BAUDRATE;
89         }
90         return 0;
91 }
92
93
94 static int display_banner (void)
95 {
96         printf ("\n\n%s\n\n", version_string);
97         printf ("U-Boot code: %p -> %p  data: %p -> %p\n",
98                 _text, _etext, _data, _end);
99         return 0;
100 }
101
102 void hang(void)
103 {
104         for (;;) ;
105 }
106
107 static int display_dram_config (void)
108 {
109         int i;
110
111         puts ("DRAM Configuration:\n");
112
113         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
114                 printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
115                 print_size (gd->bd->bi_dram[i].size, "\n");
116         }
117
118         return 0;
119 }
120
121 static void display_flash_config (void)
122 {
123         puts ("Flash: ");
124         print_size(gd->bd->bi_flashsize, " ");
125         printf("at address 0x%08lx\n", gd->bd->bi_flashstart);
126 }
127
128 void board_init_f(ulong board_type)
129 {
130         gd_t gd_data;
131         gd_t *new_gd;
132         bd_t *bd;
133         unsigned long *new_sp;
134         unsigned long monitor_len;
135         unsigned long monitor_addr;
136         unsigned long addr;
137         long sdram_size;
138
139         /* Initialize the global data pointer */
140         memset(&gd_data, 0, sizeof(gd_data));
141         gd = &gd_data;
142
143         /* Perform initialization sequence */
144         board_early_init_f();
145         cpu_init();
146         env_init();
147         init_baudrate();
148         serial_init();
149         console_init_f();
150         display_banner();
151         sdram_size = initdram(board_type);
152
153         /* If we have no SDRAM, we can't go on */
154         if (sdram_size <= 0)
155                 panic("No working SDRAM available\n");
156
157         /*
158          * Now that we have DRAM mapped and working, we can
159          * relocate the code and continue running from DRAM.
160          *
161          * Reserve memory at end of RAM for (top down in that order):
162          *  - u-boot image
163          *  - heap for malloc()
164          *  - board info struct
165          *  - global data struct
166          *  - stack
167          */
168         addr = CFG_SDRAM_BASE + sdram_size;
169         monitor_len = _end - _text;
170
171         /*
172          * Reserve memory for u-boot code, data and bss.
173          * Round down to next 4 kB limit.
174          */
175         addr -= monitor_len;
176         addr &= ~(4096UL - 1);
177         monitor_addr = addr;
178
179         /* Reserve memory for malloc() */
180         addr -= CFG_MALLOC_LEN;
181
182         /* Allocate a Board Info struct on a word boundary */
183         addr -= sizeof(bd_t);
184         addr &= ~3UL;
185         gd->bd = bd = (bd_t *)addr;
186
187         /* Allocate a new global data copy on a 8-byte boundary. */
188         addr -= sizeof(gd_t);
189         addr &= ~7UL;
190         new_gd = (gd_t *)addr;
191
192         /* And finally, a new, bigger stack. */
193         new_sp = (unsigned long *)addr;
194         gd->stack_end = addr;
195         *(--new_sp) = 0;
196         *(--new_sp) = 0;
197
198         /*
199          * Initialize the board information struct with the
200          * information we have.
201          */
202         bd->bi_dram[0].start = CFG_SDRAM_BASE;
203         bd->bi_dram[0].size = sdram_size;
204         bd->bi_baudrate = gd->baudrate;
205
206         memcpy(new_gd, gd, sizeof(gd_t));
207
208         relocate_code((unsigned long)new_sp, new_gd, monitor_addr);
209 }
210
211 void board_init_r(gd_t *new_gd, ulong dest_addr)
212 {
213         extern void malloc_bin_reloc (void);
214 #ifndef CFG_ENV_IS_NOWHERE
215         extern char * env_name_spec;
216 #endif
217         cmd_tbl_t *cmdtp;
218         bd_t *bd;
219
220         gd = new_gd;
221         bd = gd->bd;
222
223         gd->flags |= GD_FLG_RELOC;
224         gd->reloc_off = dest_addr - CFG_MONITOR_BASE;
225
226         monitor_flash_len = _edata - _text;
227
228         /*
229          * We have to relocate the command table manually
230          */
231         for (cmdtp = &__u_boot_cmd_start;
232              cmdtp !=  &__u_boot_cmd_end; cmdtp++) {
233                 unsigned long addr;
234
235                 addr = (unsigned long)cmdtp->cmd + gd->reloc_off;
236                 cmdtp->cmd = (typeof(cmdtp->cmd))addr;
237
238                 addr = (unsigned long)cmdtp->name + gd->reloc_off;
239                 cmdtp->name = (typeof(cmdtp->name))addr;
240
241                 if (cmdtp->usage) {
242                         addr = (unsigned long)cmdtp->usage + gd->reloc_off;
243                         cmdtp->usage = (typeof(cmdtp->usage))addr;
244                 }
245 #ifdef CFG_LONGHELP
246                 if (cmdtp->help) {
247                         addr = (unsigned long)cmdtp->help + gd->reloc_off;
248                         cmdtp->help = (typeof(cmdtp->help))addr;
249                 }
250 #endif
251         }
252
253         /* there are some other pointer constants we must deal with */
254 #ifndef CFG_ENV_IS_NOWHERE
255         env_name_spec += gd->reloc_off;
256 #endif
257
258         timer_init();
259         mem_malloc_init();
260         malloc_bin_reloc();
261         board_init_info();
262         flash_init();
263
264         if (bd->bi_flashsize)
265                 display_flash_config();
266         if (bd->bi_dram[0].size)
267                 display_dram_config();
268
269         gd->bd->bi_boot_params = malloc(CFG_BOOTPARAMS_LEN);
270         if (!gd->bd->bi_boot_params)
271                 puts("WARNING: Cannot allocate space for boot parameters\n");
272
273         /* initialize environment */
274         env_relocate();
275
276         devices_init();
277         jumptable_init();
278         console_init_r();
279
280         for (;;) {
281                 main_loop();
282         }
283 }