blob: a407bcad14d7ab637cca5dfa71432ade6cfea97d [file] [log] [blame]
Wolfgang Denk7b64fef2006-10-24 14:21:16 +02001/*
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
36DECLARE_GLOBAL_DATA_PTR;
37
38const char version_string[] =
39 U_BOOT_VERSION " (" __DATE__ " - " __TIME__ ") " CONFIG_IDENT_STRING;
40
41unsigned long monitor_flash_len;
42
43/*
44 * Begin and end of memory area for malloc(), and current "brk"
45 */
46static unsigned long mem_malloc_start = 0;
47static unsigned long mem_malloc_end = 0;
48static unsigned long mem_malloc_brk = 0;
49
Haavard Skinnemoen1f4f2122006-11-20 15:53:10 +010050/* The malloc area is right below the monitor image in RAM */
Wolfgang Denk7b64fef2006-10-24 14:21:16 +020051static void mem_malloc_init(void)
52{
Haavard Skinnemoen1f4f2122006-11-20 15:53:10 +010053 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;
Wolfgang Denk7b64fef2006-10-24 14:21:16 +020058 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
67void *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
79static 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
94static 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
102void hang(void)
103{
104 for (;;) ;
105}
106
107static 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
121static 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
Haavard Skinnemoen12f099c2006-12-17 14:46:06 +0100128void board_init_f(ulong board_type)
Wolfgang Denk7b64fef2006-10-24 14:21:16 +0200129{
130 gd_t gd_data;
Haavard Skinnemoen1f4f2122006-11-20 15:53:10 +0100131 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;
Haavard Skinnemoen12f099c2006-12-17 14:46:06 +0100137 long sdram_size;
Wolfgang Denk7b64fef2006-10-24 14:21:16 +0200138
139 /* Initialize the global data pointer */
140 memset(&gd_data, 0, sizeof(gd_data));
141 gd = &gd_data;
142
Wolfgang Denk7b64fef2006-10-24 14:21:16 +0200143 /* Perform initialization sequence */
Haavard Skinnemoendf548d32006-11-19 18:06:53 +0100144 board_early_init_f();
Wolfgang Denk7b64fef2006-10-24 14:21:16 +0200145 cpu_init();
Wolfgang Denk7b64fef2006-10-24 14:21:16 +0200146 env_init();
147 init_baudrate();
148 serial_init();
149 console_init_f();
150 display_banner();
Haavard Skinnemoen12f099c2006-12-17 14:46:06 +0100151 sdram_size = initdram(board_type);
Wolfgang Denk7b64fef2006-10-24 14:21:16 +0200152
Haavard Skinnemoen1f4f2122006-11-20 15:53:10 +0100153 /* If we have no SDRAM, we can't go on */
Haavard Skinnemoen12f099c2006-12-17 14:46:06 +0100154 if (sdram_size <= 0)
Haavard Skinnemoen1f4f2122006-11-20 15:53:10 +0100155 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 */
Haavard Skinnemoen12f099c2006-12-17 14:46:06 +0100168 addr = CFG_SDRAM_BASE + sdram_size;
Haavard Skinnemoen1f4f2122006-11-20 15:53:10 +0100169 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;
Haavard Skinnemoen12f099c2006-12-17 14:46:06 +0100203 bd->bi_dram[0].size = sdram_size;
Haavard Skinnemoen1f4f2122006-11-20 15:53:10 +0100204 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);
Haavard Skinnemoenc841bee2006-11-18 17:15:30 +0100209}
210
211void board_init_r(gd_t *new_gd, ulong dest_addr)
212{
Haavard Skinnemoen1f4f2122006-11-20 15:53:10 +0100213 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
Haavard Skinnemoenc841bee2006-11-18 17:15:30 +0100220 gd = new_gd;
Haavard Skinnemoen1f4f2122006-11-20 15:53:10 +0100221 bd = gd->bd;
222
223 gd->flags |= GD_FLG_RELOC;
224 gd->reloc_off = dest_addr - CFG_MONITOR_BASE;
Haavard Skinnemoenc841bee2006-11-18 17:15:30 +0100225
226 monitor_flash_len = _edata - _text;
227
Haavard Skinnemoen1f4f2122006-11-20 15:53:10 +0100228 /*
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;
Wolfgang Denk7b64fef2006-10-24 14:21:16 +0200234
Haavard Skinnemoen1f4f2122006-11-20 15:53:10 +0100235 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();
Wolfgang Denk7b64fef2006-10-24 14:21:16 +0200261 board_init_info();
262 flash_init();
263
Haavard Skinnemoen1f4f2122006-11-20 15:53:10 +0100264 if (bd->bi_flashsize)
Wolfgang Denk7b64fef2006-10-24 14:21:16 +0200265 display_flash_config();
Haavard Skinnemoen1f4f2122006-11-20 15:53:10 +0100266 if (bd->bi_dram[0].size)
Wolfgang Denk7b64fef2006-10-24 14:21:16 +0200267 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}