Standardize mem_malloc_init() implementation
[platform/kernel/u-boot.git] / lib_mips / 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 <timestamp.h>
29 #include <version.h>
30 #include <net.h>
31 #include <environment.h>
32 #include <nand.h>
33 #include <onenand_uboot.h>
34 #include <spi.h>
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 #if ( ((CONFIG_ENV_ADDR+CONFIG_ENV_SIZE) < CONFIG_SYS_MONITOR_BASE) || \
39       (CONFIG_ENV_ADDR >= (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)) ) || \
40     defined(CONFIG_ENV_IS_IN_NVRAM)
41 #define TOTAL_MALLOC_LEN        (CONFIG_SYS_MALLOC_LEN + CONFIG_ENV_SIZE)
42 #else
43 #define TOTAL_MALLOC_LEN        CONFIG_SYS_MALLOC_LEN
44 #endif
45
46 #undef DEBUG
47
48 extern int timer_init(void);
49
50 extern int incaip_set_cpuclk(void);
51
52 extern ulong uboot_end_data;
53 extern ulong uboot_end;
54
55 ulong monitor_flash_len;
56
57 const char version_string[] =
58         U_BOOT_VERSION" (" U_BOOT_DATE " - " U_BOOT_TIME ")";
59
60 static char *failed = "*** failed ***\n";
61
62 /*
63  * mips_io_port_base is the begin of the address space to which x86 style
64  * I/O ports are mapped.
65  */
66 unsigned long mips_io_port_base = -1;
67
68 int __board_early_init_f(void)
69 {
70         /*
71          * Nothing to do in this dummy implementation
72          */
73         return 0;
74 }
75 int board_early_init_f(void) __attribute__((weak, alias("__board_early_init_f")));
76
77 /*
78  * The Malloc area is immediately below the monitor copy in DRAM
79  */
80 static void mem_malloc_init(ulong start, ulong size)
81 {
82         mem_malloc_start = start;
83         mem_malloc_end = start + size;
84         mem_malloc_brk = start;
85
86         memset ((void *)mem_malloc_start, 0, size);
87 }
88
89
90 static int init_func_ram (void)
91 {
92 #ifdef  CONFIG_BOARD_TYPES
93         int board_type = gd->board_type;
94 #else
95         int board_type = 0;     /* use dummy arg */
96 #endif
97         puts ("DRAM:  ");
98
99         if ((gd->ram_size = initdram (board_type)) > 0) {
100                 print_size (gd->ram_size, "\n");
101                 return (0);
102         }
103         puts (failed);
104         return (1);
105 }
106
107 static int display_banner(void)
108 {
109
110         printf ("\n\n%s\n\n", version_string);
111         return (0);
112 }
113
114 #ifndef CONFIG_SYS_NO_FLASH
115 static void display_flash_config(ulong size)
116 {
117         puts ("Flash: ");
118         print_size (size, "\n");
119 }
120 #endif
121
122 static int init_baudrate (void)
123 {
124         char tmp[64];   /* long enough for environment variables */
125         int i = getenv_r ("baudrate", tmp, sizeof (tmp));
126
127         gd->baudrate = (i > 0)
128                         ? (int) simple_strtoul (tmp, NULL, 10)
129                         : CONFIG_BAUDRATE;
130
131         return (0);
132 }
133
134
135 /*
136  * Breath some life into the board...
137  *
138  * The first part of initialization is running from Flash memory;
139  * its main purpose is to initialize the RAM so that we
140  * can relocate the monitor code to RAM.
141  */
142
143 /*
144  * All attempts to come up with a "common" initialization sequence
145  * that works for all boards and architectures failed: some of the
146  * requirements are just _too_ different. To get rid of the resulting
147  * mess of board dependend #ifdef'ed code we now make the whole
148  * initialization sequence configurable to the user.
149  *
150  * The requirements for any new initalization function is simple: it
151  * receives a pointer to the "global data" structure as it's only
152  * argument, and returns an integer return code, where 0 means
153  * "continue" and != 0 means "fatal error, hang the system".
154  */
155 typedef int (init_fnc_t) (void);
156
157 init_fnc_t *init_sequence[] = {
158         board_early_init_f,
159         timer_init,
160         env_init,               /* initialize environment */
161 #ifdef CONFIG_INCA_IP
162         incaip_set_cpuclk,      /* set cpu clock according to environment variable */
163 #endif
164         init_baudrate,          /* initialze baudrate settings */
165         serial_init,            /* serial communications setup */
166         console_init_f,
167         display_banner,         /* say that we are here */
168         checkboard,
169         init_func_ram,
170         NULL,
171 };
172
173
174 void board_init_f(ulong bootflag)
175 {
176         gd_t gd_data, *id;
177         bd_t *bd;
178         init_fnc_t **init_fnc_ptr;
179         ulong addr, addr_sp, len = (ulong)&uboot_end - CONFIG_SYS_MONITOR_BASE;
180         ulong *s;
181 #ifdef CONFIG_PURPLE
182         void copy_code (ulong);
183 #endif
184
185         /* Pointer is writable since we allocated a register for it.
186          */
187         gd = &gd_data;
188         /* compiler optimization barrier needed for GCC >= 3.4 */
189         __asm__ __volatile__("": : :"memory");
190
191         memset ((void *)gd, 0, sizeof (gd_t));
192
193         for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
194                 if ((*init_fnc_ptr)() != 0) {
195                         hang ();
196                 }
197         }
198
199         /*
200          * Now that we have DRAM mapped and working, we can
201          * relocate the code and continue running from DRAM.
202          */
203         addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
204
205         /* We can reserve some RAM "on top" here.
206          */
207
208         /* round down to next 4 kB limit.
209          */
210         addr &= ~(4096 - 1);
211         debug ("Top of RAM usable for U-Boot at: %08lx\n", addr);
212
213         /* Reserve memory for U-Boot code, data & bss
214          * round down to next 16 kB limit
215          */
216         addr -= len;
217         addr &= ~(16 * 1024 - 1);
218
219         debug ("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
220
221          /* Reserve memory for malloc() arena.
222          */
223         addr_sp = addr - TOTAL_MALLOC_LEN;
224         debug ("Reserving %dk for malloc() at: %08lx\n",
225                         TOTAL_MALLOC_LEN >> 10, addr_sp);
226
227         /*
228          * (permanently) allocate a Board Info struct
229          * and a permanent copy of the "global" data
230          */
231         addr_sp -= sizeof(bd_t);
232         bd = (bd_t *)addr_sp;
233         gd->bd = bd;
234         debug ("Reserving %zu Bytes for Board Info at: %08lx\n",
235                         sizeof(bd_t), addr_sp);
236
237         addr_sp -= sizeof(gd_t);
238         id = (gd_t *)addr_sp;
239         debug ("Reserving %zu Bytes for Global Data at: %08lx\n",
240                         sizeof (gd_t), addr_sp);
241
242         /* Reserve memory for boot params.
243          */
244         addr_sp -= CONFIG_SYS_BOOTPARAMS_LEN;
245         bd->bi_boot_params = addr_sp;
246         debug ("Reserving %dk for boot params() at: %08lx\n",
247                         CONFIG_SYS_BOOTPARAMS_LEN >> 10, addr_sp);
248
249         /*
250          * Finally, we set up a new (bigger) stack.
251          *
252          * Leave some safety gap for SP, force alignment on 16 byte boundary
253          * Clear initial stack frame
254          */
255         addr_sp -= 16;
256         addr_sp &= ~0xF;
257         s = (ulong *)addr_sp;
258         *s-- = 0;
259         *s-- = 0;
260         addr_sp = (ulong)s;
261         debug ("Stack Pointer at: %08lx\n", addr_sp);
262
263         /*
264          * Save local variables to board info struct
265          */
266         bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;        /* start of  DRAM memory */
267         bd->bi_memsize  = gd->ram_size;         /* size  of  DRAM memory in bytes */
268         bd->bi_baudrate = gd->baudrate;         /* Console Baudrate */
269
270         memcpy (id, (void *)gd, sizeof (gd_t));
271
272         /* On the purple board we copy the code in a special way
273          * in order to solve flash problems
274          */
275 #ifdef CONFIG_PURPLE
276         copy_code(addr);
277 #endif
278
279         relocate_code (addr_sp, id, addr);
280
281         /* NOTREACHED - relocate_code() does not return */
282 }
283 /************************************************************************
284  *
285  * This is the next part if the initialization sequence: we are now
286  * running from RAM and have a "normal" C environment, i. e. global
287  * data can be written, BSS has been cleared, the stack size in not
288  * that critical any more, etc.
289  *
290  ************************************************************************
291  */
292
293 void board_init_r (gd_t *id, ulong dest_addr)
294 {
295         cmd_tbl_t *cmdtp;
296 #ifndef CONFIG_SYS_NO_FLASH
297         ulong size;
298 #endif
299         extern void malloc_bin_reloc (void);
300 #ifndef CONFIG_ENV_IS_NOWHERE
301         extern char * env_name_spec;
302 #endif
303         char *s;
304         bd_t *bd;
305
306         gd = id;
307         gd->flags |= GD_FLG_RELOC;      /* tell others: relocation done */
308
309         debug ("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
310
311         gd->reloc_off = dest_addr - CONFIG_SYS_MONITOR_BASE;
312
313         monitor_flash_len = (ulong)&uboot_end_data - dest_addr;
314
315         /*
316          * We have to relocate the command table manually
317          */
318         for (cmdtp = &__u_boot_cmd_start; cmdtp !=  &__u_boot_cmd_end; cmdtp++) {
319                 ulong addr;
320
321                 addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
322 #if 0
323                 printf ("Command \"%s\": 0x%08lx => 0x%08lx\n",
324                                 cmdtp->name, (ulong) (cmdtp->cmd), addr);
325 #endif
326                 cmdtp->cmd =
327                         (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;
328
329                 addr = (ulong)(cmdtp->name) + gd->reloc_off;
330                 cmdtp->name = (char *)addr;
331
332                 if (cmdtp->usage) {
333                         addr = (ulong)(cmdtp->usage) + gd->reloc_off;
334                         cmdtp->usage = (char *)addr;
335                 }
336 #ifdef  CONFIG_SYS_LONGHELP
337                 if (cmdtp->help) {
338                         addr = (ulong)(cmdtp->help) + gd->reloc_off;
339                         cmdtp->help = (char *)addr;
340                 }
341 #endif
342         }
343         /* there are some other pointer constants we must deal with */
344 #ifndef CONFIG_ENV_IS_NOWHERE
345         env_name_spec += gd->reloc_off;
346 #endif
347
348         bd = gd->bd;
349
350         /* initialize malloc() area */
351         mem_malloc_init(CONFIG_SYS_MONITOR_BASE + gd->reloc_off -
352                         TOTAL_MALLOC_LEN, TOTAL_MALLOC_LEN);
353         malloc_bin_reloc();
354
355 #ifndef CONFIG_SYS_NO_FLASH
356         /* configure available FLASH banks */
357         size = flash_init();
358         display_flash_config (size);
359         bd->bi_flashsize = size;
360 #endif
361
362         bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
363 #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
364         bd->bi_flashoffset = monitor_flash_len; /* reserved area for U-Boot */
365 #else
366         bd->bi_flashoffset = 0;
367 #endif
368
369 #ifdef CONFIG_CMD_NAND
370         puts ("NAND:  ");
371         nand_init ();           /* go init the NAND */
372 #endif
373
374 #if defined(CONFIG_CMD_ONENAND)
375         onenand_init();
376 #endif
377
378         /* relocate environment function pointers etc. */
379         env_relocate();
380
381         /* IP Address */
382         bd->bi_ip_addr = getenv_IPaddr("ipaddr");
383
384 #if defined(CONFIG_PCI)
385         /*
386          * Do pci configuration
387          */
388         pci_init();
389 #endif
390
391 /** leave this here (after malloc(), environment and PCI are working) **/
392         /* Initialize stdio devices */
393         stdio_init ();
394
395         jumptable_init ();
396
397         /* Initialize the console (after the relocation and devices init) */
398         console_init_r ();
399 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
400
401         /* Initialize from environment */
402         if ((s = getenv ("loadaddr")) != NULL) {
403                 load_addr = simple_strtoul (s, NULL, 16);
404         }
405 #if defined(CONFIG_CMD_NET)
406         if ((s = getenv ("bootfile")) != NULL) {
407                 copy_filename (BootFile, s, sizeof (BootFile));
408         }
409 #endif
410
411 #ifdef CONFIG_CMD_SPI
412         puts ("SPI:   ");
413         spi_init ();            /* go init the SPI */
414         puts ("ready\n");
415 #endif
416
417 #if defined(CONFIG_MISC_INIT_R)
418         /* miscellaneous platform dependent initialisations */
419         misc_init_r ();
420 #endif
421
422 #if defined(CONFIG_CMD_NET)
423 #if defined(CONFIG_NET_MULTI)
424         puts ("Net:   ");
425 #endif
426         eth_initialize(gd->bd);
427 #endif
428
429         /* main_loop() can return to retry autoboot, if so just run it again. */
430         for (;;) {
431                 main_loop ();
432         }
433
434         /* NOTREACHED - no way out of command loop except booting */
435 }
436
437 void hang (void)
438 {
439         puts ("### ERROR ### Please RESET the board ###\n");
440         for (;;);
441 }