AVR32: Relocate u-boot to SDRAM
[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 <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 unused)
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
138         /* Initialize the global data pointer */
139         memset(&gd_data, 0, sizeof(gd_data));
140         gd = &gd_data;
141
142         /* Perform initialization sequence */
143         board_early_init_f();
144         cpu_init();
145         env_init();
146         init_baudrate();
147         serial_init();
148         console_init_f();
149         display_banner();
150         board_init_memories();
151
152         /* If we have no SDRAM, we can't go on */
153         if (!gd->sdram_size)
154                 panic("No working SDRAM available\n");
155
156         /*
157          * Now that we have DRAM mapped and working, we can
158          * relocate the code and continue running from DRAM.
159          *
160          * Reserve memory at end of RAM for (top down in that order):
161          *  - u-boot image
162          *  - heap for malloc()
163          *  - board info struct
164          *  - global data struct
165          *  - stack
166          */
167         addr = CFG_SDRAM_BASE + gd->sdram_size;
168         monitor_len = _end - _text;
169
170         /*
171          * Reserve memory for u-boot code, data and bss.
172          * Round down to next 4 kB limit.
173          */
174         addr -= monitor_len;
175         addr &= ~(4096UL - 1);
176         monitor_addr = addr;
177
178         /* Reserve memory for malloc() */
179         addr -= CFG_MALLOC_LEN;
180
181         /* Allocate a Board Info struct on a word boundary */
182         addr -= sizeof(bd_t);
183         addr &= ~3UL;
184         gd->bd = bd = (bd_t *)addr;
185
186         /* Allocate a new global data copy on a 8-byte boundary. */
187         addr -= sizeof(gd_t);
188         addr &= ~7UL;
189         new_gd = (gd_t *)addr;
190
191         /* And finally, a new, bigger stack. */
192         new_sp = (unsigned long *)addr;
193         gd->stack_end = addr;
194         *(--new_sp) = 0;
195         *(--new_sp) = 0;
196
197         /*
198          * Initialize the board information struct with the
199          * information we have.
200          */
201         bd->bi_dram[0].start = CFG_SDRAM_BASE;
202         bd->bi_dram[0].size = gd->sdram_size;
203         bd->bi_baudrate = gd->baudrate;
204
205         memcpy(new_gd, gd, sizeof(gd_t));
206
207         relocate_code((unsigned long)new_sp, new_gd, monitor_addr);
208 }
209
210 void board_init_r(gd_t *new_gd, ulong dest_addr)
211 {
212         extern void malloc_bin_reloc (void);
213 #ifndef CFG_ENV_IS_NOWHERE
214         extern char * env_name_spec;
215 #endif
216         cmd_tbl_t *cmdtp;
217         bd_t *bd;
218
219         gd = new_gd;
220         bd = gd->bd;
221
222         gd->flags |= GD_FLG_RELOC;
223         gd->reloc_off = dest_addr - CFG_MONITOR_BASE;
224
225         monitor_flash_len = _edata - _text;
226
227         /*
228          * We have to relocate the command table manually
229          */
230         for (cmdtp = &__u_boot_cmd_start;
231              cmdtp !=  &__u_boot_cmd_end; cmdtp++) {
232                 unsigned long addr;
233
234                 addr = (unsigned long)cmdtp->cmd + gd->reloc_off;
235                 cmdtp->cmd = (typeof(cmdtp->cmd))addr;
236
237                 addr = (unsigned long)cmdtp->name + gd->reloc_off;
238                 cmdtp->name = (typeof(cmdtp->name))addr;
239
240                 if (cmdtp->usage) {
241                         addr = (unsigned long)cmdtp->usage + gd->reloc_off;
242                         cmdtp->usage = (typeof(cmdtp->usage))addr;
243                 }
244 #ifdef CFG_LONGHELP
245                 if (cmdtp->help) {
246                         addr = (unsigned long)cmdtp->help + gd->reloc_off;
247                         cmdtp->help = (typeof(cmdtp->help))addr;
248                 }
249 #endif
250         }
251
252         /* there are some other pointer constants we must deal with */
253 #ifndef CFG_ENV_IS_NOWHERE
254         env_name_spec += gd->reloc_off;
255 #endif
256
257         timer_init();
258         mem_malloc_init();
259         malloc_bin_reloc();
260         board_init_info();
261         flash_init();
262
263         if (bd->bi_flashsize)
264                 display_flash_config();
265         if (bd->bi_dram[0].size)
266                 display_dram_config();
267
268         gd->bd->bi_boot_params = malloc(CFG_BOOTPARAMS_LEN);
269         if (!gd->bd->bi_boot_params)
270                 puts("WARNING: Cannot allocate space for boot parameters\n");
271
272         /* initialize environment */
273         env_relocate();
274
275         devices_init();
276         jumptable_init();
277         console_init_r();
278
279         for (;;) {
280                 main_loop();
281         }
282 }