]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/mips/lib/board.c
MIPS: board.c: fix init of flash data in bd_info
[karo-tx-uboot.git] / arch / mips / lib / board.c
1 /*
2  * (C) Copyright 2003
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <command.h>
26 #include <malloc.h>
27 #include <stdio_dev.h>
28 #include <version.h>
29 #include <net.h>
30 #include <environment.h>
31 #include <nand.h>
32 #include <onenand_uboot.h>
33 #include <spi.h>
34
35 #ifdef CONFIG_BITBANGMII
36 #include <miiphy.h>
37 #endif
38
39 DECLARE_GLOBAL_DATA_PTR;
40
41 extern int timer_init(void);
42
43 extern int incaip_set_cpuclk(void);
44
45 extern ulong uboot_end_data;
46 extern ulong uboot_end;
47
48 ulong monitor_flash_len;
49
50 static char *failed = "*** failed ***\n";
51
52 /*
53  * mips_io_port_base is the begin of the address space to which x86 style
54  * I/O ports are mapped.
55  */
56 unsigned long mips_io_port_base = -1;
57
58 int __board_early_init_f(void)
59 {
60         /*
61          * Nothing to do in this dummy implementation
62          */
63         return 0;
64 }
65 int board_early_init_f(void)
66         __attribute__((weak, alias("__board_early_init_f")));
67
68 static int init_func_ram(void)
69 {
70 #ifdef  CONFIG_BOARD_TYPES
71         int board_type = gd->board_type;
72 #else
73         int board_type = 0;     /* use dummy arg */
74 #endif
75         puts("DRAM:  ");
76
77         gd->ram_size = initdram(board_type);
78         if (gd->ram_size > 0) {
79                 print_size(gd->ram_size, "\n");
80                 return 0;
81         }
82         puts(failed);
83         return 1;
84 }
85
86 static int display_banner(void)
87 {
88
89         printf("\n\n%s\n\n", version_string);
90         return 0;
91 }
92
93 #ifndef CONFIG_SYS_NO_FLASH
94 static void display_flash_config(ulong size)
95 {
96         puts("Flash: ");
97         print_size(size, "\n");
98 }
99 #endif
100
101 static int init_baudrate(void)
102 {
103         gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
104         return 0;
105 }
106
107
108 /*
109  * Breath some life into the board...
110  *
111  * The first part of initialization is running from Flash memory;
112  * its main purpose is to initialize the RAM so that we
113  * can relocate the monitor code to RAM.
114  */
115
116 /*
117  * All attempts to come up with a "common" initialization sequence
118  * that works for all boards and architectures failed: some of the
119  * requirements are just _too_ different. To get rid of the resulting
120  * mess of board dependend #ifdef'ed code we now make the whole
121  * initialization sequence configurable to the user.
122  *
123  * The requirements for any new initalization function is simple: it
124  * receives a pointer to the "global data" structure as it's only
125  * argument, and returns an integer return code, where 0 means
126  * "continue" and != 0 means "fatal error, hang the system".
127  */
128 typedef int (init_fnc_t)(void);
129
130 init_fnc_t *init_sequence[] = {
131         board_early_init_f,
132         timer_init,
133         env_init,               /* initialize environment */
134 #ifdef CONFIG_INCA_IP
135         incaip_set_cpuclk,      /* set cpu clock according to env. variable */
136 #endif
137         init_baudrate,          /* initialize baudrate settings */
138         serial_init,            /* serial communications setup */
139         console_init_f,
140         display_banner,         /* say that we are here */
141         checkboard,
142         init_func_ram,
143         NULL,
144 };
145
146
147 void board_init_f(ulong bootflag)
148 {
149         gd_t gd_data, *id;
150         bd_t *bd;
151         init_fnc_t **init_fnc_ptr;
152         ulong addr, addr_sp, len = (ulong)&uboot_end - CONFIG_SYS_MONITOR_BASE;
153         ulong *s;
154
155         /* Pointer is writable since we allocated a register for it.
156          */
157         gd = &gd_data;
158         /* compiler optimization barrier needed for GCC >= 3.4 */
159         __asm__ __volatile__("" : : : "memory");
160
161         memset((void *)gd, 0, sizeof(gd_t));
162
163         for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
164                 if ((*init_fnc_ptr)() != 0)
165                         hang();
166         }
167
168         /*
169          * Now that we have DRAM mapped and working, we can
170          * relocate the code and continue running from DRAM.
171          */
172         addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
173
174         /* We can reserve some RAM "on top" here.
175          */
176
177         /* round down to next 4 kB limit.
178          */
179         addr &= ~(4096 - 1);
180         debug("Top of RAM usable for U-Boot at: %08lx\n", addr);
181
182         /* Reserve memory for U-Boot code, data & bss
183          * round down to next 16 kB limit
184          */
185         addr -= len;
186         addr &= ~(16 * 1024 - 1);
187
188         debug("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
189
190          /* Reserve memory for malloc() arena.
191          */
192         addr_sp = addr - TOTAL_MALLOC_LEN;
193         debug("Reserving %dk for malloc() at: %08lx\n",
194                         TOTAL_MALLOC_LEN >> 10, addr_sp);
195
196         /*
197          * (permanently) allocate a Board Info struct
198          * and a permanent copy of the "global" data
199          */
200         addr_sp -= sizeof(bd_t);
201         bd = (bd_t *)addr_sp;
202         gd->bd = bd;
203         debug("Reserving %zu Bytes for Board Info at: %08lx\n",
204                         sizeof(bd_t), addr_sp);
205
206         addr_sp -= sizeof(gd_t);
207         id = (gd_t *)addr_sp;
208         debug("Reserving %zu Bytes for Global Data at: %08lx\n",
209                         sizeof(gd_t), addr_sp);
210
211         /* Reserve memory for boot params.
212          */
213         addr_sp -= CONFIG_SYS_BOOTPARAMS_LEN;
214         bd->bi_boot_params = addr_sp;
215         debug("Reserving %dk for boot params() at: %08lx\n",
216                         CONFIG_SYS_BOOTPARAMS_LEN >> 10, addr_sp);
217
218         /*
219          * Finally, we set up a new (bigger) stack.
220          *
221          * Leave some safety gap for SP, force alignment on 16 byte boundary
222          * Clear initial stack frame
223          */
224         addr_sp -= 16;
225         addr_sp &= ~0xF;
226         s = (ulong *)addr_sp;
227         *s-- = 0;
228         *s-- = 0;
229         addr_sp = (ulong)s;
230         debug("Stack Pointer at: %08lx\n", addr_sp);
231
232         /*
233          * Save local variables to board info struct
234          */
235         bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;        /* start of DRAM */
236         bd->bi_memsize  = gd->ram_size;         /* size of DRAM in bytes */
237         bd->bi_baudrate = gd->baudrate;         /* Console Baudrate */
238
239         memcpy(id, (void *)gd, sizeof(gd_t));
240
241         relocate_code(addr_sp, id, addr);
242
243         /* NOTREACHED - relocate_code() does not return */
244 }
245
246 /*
247  * This is the next part if the initialization sequence: we are now
248  * running from RAM and have a "normal" C environment, i. e. global
249  * data can be written, BSS has been cleared, the stack size in not
250  * that critical any more, etc.
251  */
252
253 void board_init_r(gd_t *id, ulong dest_addr)
254 {
255 #ifndef CONFIG_SYS_NO_FLASH
256         ulong size;
257 #endif
258         extern void malloc_bin_reloc(void);
259 #ifndef CONFIG_ENV_IS_NOWHERE
260         extern char *env_name_spec;
261 #endif
262         bd_t *bd;
263
264         gd = id;
265         gd->flags |= GD_FLG_RELOC;      /* tell others: relocation done */
266
267         debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
268
269         gd->reloc_off = dest_addr - CONFIG_SYS_MONITOR_BASE;
270
271         monitor_flash_len = (ulong)&uboot_end_data - dest_addr;
272
273 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
274         /*
275          * We have to relocate the command table manually
276          */
277         fixup_cmdtable(&__u_boot_cmd_start,
278                 (ulong)(&__u_boot_cmd_end - &__u_boot_cmd_start));
279 #endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
280
281         /* there are some other pointer constants we must deal with */
282 #ifndef CONFIG_ENV_IS_NOWHERE
283         env_name_spec += gd->reloc_off;
284 #endif
285
286         bd = gd->bd;
287
288         /* The Malloc area is immediately below the monitor copy in DRAM */
289         mem_malloc_init(CONFIG_SYS_MONITOR_BASE + gd->reloc_off -
290                         TOTAL_MALLOC_LEN, TOTAL_MALLOC_LEN);
291         malloc_bin_reloc();
292
293 #ifndef CONFIG_SYS_NO_FLASH
294         /* configure available FLASH banks */
295         size = flash_init();
296         display_flash_config(size);
297         bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
298         bd->bi_flashsize = size;
299
300 #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
301         bd->bi_flashoffset = monitor_flash_len; /* reserved area for U-Boot */
302 #else
303         bd->bi_flashoffset = 0;
304 #endif
305 #else
306         bd->bi_flashstart = 0;
307         bd->bi_flashsize = 0;
308         bd->bi_flashoffset = 0;
309 #endif
310
311 #ifdef CONFIG_CMD_NAND
312         puts("NAND:  ");
313         nand_init();            /* go init the NAND */
314 #endif
315
316 #if defined(CONFIG_CMD_ONENAND)
317         onenand_init();
318 #endif
319
320         /* relocate environment function pointers etc. */
321         env_relocate();
322
323         /* IP Address */
324         bd->bi_ip_addr = getenv_IPaddr("ipaddr");
325
326 #if defined(CONFIG_PCI)
327         /*
328          * Do pci configuration
329          */
330         pci_init();
331 #endif
332
333 /** leave this here (after malloc(), environment and PCI are working) **/
334         /* Initialize stdio devices */
335         stdio_init();
336
337         jumptable_init();
338
339         /* Initialize the console (after the relocation and devices init) */
340         console_init_r();
341 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
342
343         /* Initialize from environment */
344         load_addr = getenv_ulong("loadaddr", 16, load_addr);
345 #if defined(CONFIG_CMD_NET)
346         {
347                 char *s = getenv("bootfile");
348
349                 if (s != NULL)
350                         copy_filename(BootFile, s, sizeof(BootFile));
351         }
352 #endif
353
354 #ifdef CONFIG_CMD_SPI
355         puts("SPI:   ");
356         spi_init();             /* go init the SPI */
357         puts("ready\n");
358 #endif
359
360 #if defined(CONFIG_MISC_INIT_R)
361         /* miscellaneous platform dependent initialisations */
362         misc_init_r();
363 #endif
364
365 #ifdef CONFIG_BITBANGMII
366         bb_miiphy_init();
367 #endif
368 #if defined(CONFIG_CMD_NET)
369         puts("Net:   ");
370         eth_initialize(gd->bd);
371 #endif
372
373         /* main_loop() can return to retry autoboot, if so just run it again. */
374         for (;;)
375                 main_loop();
376
377         /* NOTREACHED - no way out of command loop except booting */
378 }
379
380 void hang(void)
381 {
382         puts("### ERROR ### Please RESET the board ###\n");
383         for (;;)
384                 ;
385 }