AVR32: Split start_u_boot into board_init_f and board_init_r
[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 wherever the board wants it to be */
51 static void mem_malloc_init(void)
52 {
53         mem_malloc_start = CFG_MALLOC_START;
54         mem_malloc_end = CFG_MALLOC_END;
55         mem_malloc_brk = mem_malloc_start;
56
57         printf("malloc: Using memory from 0x%08lx to 0x%08lx\n",
58                mem_malloc_start, mem_malloc_end);
59
60         memset ((void *)mem_malloc_start, 0,
61                 mem_malloc_end - mem_malloc_start);
62 }
63
64 void *sbrk(ptrdiff_t increment)
65 {
66         unsigned long old = mem_malloc_brk;
67         unsigned long new = old + increment;
68
69         if ((new < mem_malloc_start) || (new > mem_malloc_end))
70                 return NULL;
71
72         mem_malloc_brk = new;
73         return ((void *)old);
74 }
75
76 static int init_baudrate(void)
77 {
78         char tmp[64];
79         int i;
80
81         i = getenv_r("baudrate", tmp, sizeof(tmp));
82         if (i > 0) {
83                 gd->baudrate = simple_strtoul(tmp, NULL, 10);
84         } else {
85                 gd->baudrate = CONFIG_BAUDRATE;
86         }
87         return 0;
88 }
89
90
91 static int display_banner (void)
92 {
93         printf ("\n\n%s\n\n", version_string);
94         printf ("U-Boot code: %p -> %p  data: %p -> %p\n",
95                 _text, _etext, _data, _end);
96         return 0;
97 }
98
99 void hang(void)
100 {
101         for (;;) ;
102 }
103
104 static int display_dram_config (void)
105 {
106         int i;
107
108         puts ("DRAM Configuration:\n");
109
110         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
111                 printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
112                 print_size (gd->bd->bi_dram[i].size, "\n");
113         }
114
115         return 0;
116 }
117
118 static void display_flash_config (void)
119 {
120         puts ("Flash: ");
121         print_size(gd->bd->bi_flashsize, " ");
122         printf("at address 0x%08lx\n", gd->bd->bi_flashstart);
123 }
124
125 void board_init_f(ulong unused)
126 {
127         gd_t gd_data;
128
129         /* Initialize the global data pointer */
130         memset(&gd_data, 0, sizeof(gd_data));
131         gd = &gd_data;
132
133         /* Perform initialization sequence */
134         cpu_init();
135         timer_init();
136         env_init();
137         init_baudrate();
138         serial_init();
139         console_init_f();
140         display_banner();
141         board_init_memories();
142
143         board_init_r(gd, CFG_MONITOR_BASE);
144 }
145
146 void board_init_r(gd_t *new_gd, ulong dest_addr)
147 {
148         gd = new_gd;
149
150         monitor_flash_len = _edata - _text;
151
152         mem_malloc_init();
153         gd->bd = malloc(sizeof(bd_t));
154         memset(gd->bd, 0, sizeof(bd_t));
155         gd->bd->bi_baudrate = gd->baudrate;
156         gd->bd->bi_dram[0].start = CFG_SDRAM_BASE;
157         gd->bd->bi_dram[0].size = gd->sdram_size;
158
159         board_init_info();
160         flash_init();
161
162         if (gd->bd->bi_flashsize)
163                 display_flash_config();
164         if (gd->bd->bi_dram[0].size)
165                 display_dram_config();
166
167         gd->bd->bi_boot_params = malloc(CFG_BOOTPARAMS_LEN);
168         if (!gd->bd->bi_boot_params)
169                 puts("WARNING: Cannot allocate space for boot parameters\n");
170
171         /* initialize environment */
172         env_relocate();
173
174         devices_init();
175         jumptable_init();
176         console_init_r();
177
178         for (;;) {
179                 main_loop();
180         }
181 }