]> git.sur5r.net Git - u-boot/blob - lib_sparc/board.c
Standardize mem_malloc_init() implementation
[u-boot] / lib_sparc / board.c
1 /* SPARC Board initialization
2  *
3  * (C) Copyright 2000-2006
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * (C) Copyright 2007
7  * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com.
8  *
9  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27
28 #include <common.h>
29 #include <command.h>
30 #include <malloc.h>
31 #include <stdio_dev.h>
32 #include <config.h>
33 #if defined(CONFIG_CMD_IDE)
34 #include <ide.h>
35 #endif
36 #ifdef CONFIG_STATUS_LED
37 #include <status_led.h>
38 #endif
39 #include <net.h>
40 #include <serial.h>
41 #include <version.h>
42 #if defined(CONFIG_POST)
43 #include <post.h>
44 #endif
45 #ifdef CONFIG_PS2KBD
46 #include <keyboard.h>
47 #endif
48 #ifdef CONFIG_CMD_AMBAPP
49 #include <ambapp.h>
50 #endif
51
52 DECLARE_GLOBAL_DATA_PTR;
53
54 /* Debug options
55 #define DEBUG_INIT_SEQUENCE
56 #define DEBUG_MEM_LAYOUT
57 #define DEBUG_COMMANDS
58 */
59
60 extern void timer_interrupt_init(void);
61 extern void malloc_bin_reloc(void);
62 extern int do_ambapp_print(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]);
63 extern int prom_init(void);
64
65 #if defined(CONFIG__CMD_DOC)
66 void doc_init(void);
67 #endif
68
69 #if !defined(CONFIG_SYS_NO_FLASH)
70 static char *failed = "*** failed ***\n";
71 #endif
72
73 #include <environment.h>
74
75 ulong monitor_flash_len;
76
77 /************************************************************************
78  * Utilities                                                            *
79  ************************************************************************
80  */
81
82 /*
83  * The Malloc area is immediately below the monitor copy in RAM
84  */
85 static void mem_malloc_init(ulong start, ulong size)
86 {
87         mem_malloc_start = start;
88         mem_malloc_end = start + size;
89         mem_malloc_brk = start
90
91         memset((void *)mem_malloc_start, 0, size);
92 }
93
94 /***********************************************************************/
95
96 /************************************************************************
97  * Init Utilities                                                       *
98  ************************************************************************
99  * Some of this code should be moved into the core functions,
100  * but let's get it working (again) first...
101  */
102
103 static int init_baudrate(void)
104 {
105         char tmp[64];           /* long enough for environment variables */
106         int i = getenv_r("baudrate", tmp, sizeof(tmp));
107
108         gd->baudrate = (i > 0)
109             ? (int)simple_strtoul(tmp, NULL, 10)
110             : CONFIG_BAUDRATE;
111         return (0);
112 }
113
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 #define WATCHDOG_RESET(x)
131
132 /************************************************************************
133  * Initialization sequence                                              *
134  ************************************************************************
135  */
136
137 init_fnc_t *init_sequence[] = {
138
139 #if defined(CONFIG_BOARD_EARLY_INIT_F)
140         board_early_init_f,
141 #endif
142         serial_init,
143
144         init_timebase,
145
146 #if defined(CONFIG_CMD_AMBAPP)
147         ambapp_init_reloc,
148 #endif
149
150         env_init,
151
152         init_baudrate,
153
154         console_init_f,
155         display_options,
156
157         checkcpu,
158         checkboard,
159 #if defined(CONFIG_MISC_INIT_F)
160         misc_init_f,
161 #endif
162
163 #ifdef CONFIG_POST
164         post_init_f,
165 #endif
166
167         NULL,                   /* Terminate this list,
168                                  * beware: this list will be relocated
169                                  * which means that NULL will become
170                                  * NULL+RELOC_OFFSET. We simply make
171                                  * NULL be -RELOC_OFFSET instead.
172                                  */
173 };
174
175 /************************************************************************
176  *
177  * This is the SPARC board initialization routine, running from RAM.
178  *
179  ************************************************************************
180  */
181 #ifdef DEBUG_INIT_SEQUENCE
182 char *str_init_seq = "INIT_SEQ 00\n";
183 char *str_init_seq_done = "\n\rInit sequence done...\r\n\r\n";
184 #endif
185
186 void board_init_f(ulong bootflag)
187 {
188         cmd_tbl_t *cmdtp;
189         bd_t *bd;
190         unsigned char *s;
191         init_fnc_t **init_fnc_ptr;
192         int j;
193         int i;
194         char *e;
195
196 #ifndef CONFIG_SYS_NO_FLASH
197         ulong flash_size;
198 #endif
199
200         gd = (gd_t *) (CONFIG_SYS_GBL_DATA_OFFSET);
201
202         /* Clear initial global data */
203         memset((void *)gd, 0, sizeof(gd_t));
204
205         gd->bd = (bd_t *) (gd + 1);     /* At end of global data */
206         gd->baudrate = CONFIG_BAUDRATE;
207         gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
208
209         bd = gd->bd;
210         bd->bi_memstart = CONFIG_SYS_RAM_BASE;
211         bd->bi_memsize = CONFIG_SYS_RAM_SIZE;
212         bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
213 #if     defined(CONFIG_SYS_SRAM_BASE) && defined(CONFIG_SYS_SRAM_SIZE)
214         bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;
215         bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;
216 #endif
217         bd->bi_baudrate = CONFIG_BAUDRATE;
218         bd->bi_bootflags = bootflag;    /* boot / reboot flag (for LynxOS)    */
219
220         gd->flags |= GD_FLG_RELOC;      /* tell others: relocation done */
221         gd->reloc_off = CONFIG_SYS_RELOC_MONITOR_BASE - CONFIG_SYS_MONITOR_BASE;
222
223         for (init_fnc_ptr = init_sequence, j = 0; *init_fnc_ptr;
224              ++init_fnc_ptr, j++) {
225 #ifdef DEBUG_INIT_SEQUENCE
226                 if (j > 9)
227                         str_init_seq[9] = '0' + (j / 10);
228                 str_init_seq[10] = '0' + (j - (j / 10) * 10);
229                 serial_puts(str_init_seq);
230 #endif
231                 if ((*init_fnc_ptr + gd->reloc_off) () != 0) {
232                         hang();
233                 }
234         }
235 #ifdef DEBUG_INIT_SEQUENCE
236         serial_puts(str_init_seq_done);
237 #endif
238
239         /*
240          * Now that we have DRAM mapped and working, we can
241          * relocate the code and continue running from DRAM.
242          *
243          * Reserve memory at end of RAM for (top down in that order):
244          *  - kernel log buffer
245          *  - protected RAM
246          *  - LCD framebuffer
247          *  - monitor code
248          *  - board info struct
249          */
250 #ifdef DEBUG_MEM_LAYOUT
251         printf("CONFIG_SYS_MONITOR_BASE:       0x%lx\n", CONFIG_SYS_MONITOR_BASE);
252         printf("CONFIG_ENV_ADDR:           0x%lx\n", CONFIG_ENV_ADDR);
253         printf("CONFIG_SYS_RELOC_MONITOR_BASE: 0x%lx (%d)\n", CONFIG_SYS_RELOC_MONITOR_BASE,
254                CONFIG_SYS_MONITOR_LEN);
255         printf("CONFIG_SYS_MALLOC_BASE:        0x%lx (%d)\n", CONFIG_SYS_MALLOC_BASE,
256                CONFIG_SYS_MALLOC_LEN);
257         printf("CONFIG_SYS_INIT_SP_OFFSET:     0x%lx (%d)\n", CONFIG_SYS_INIT_SP_OFFSET,
258                CONFIG_SYS_STACK_SIZE);
259         printf("CONFIG_SYS_PROM_OFFSET:        0x%lx (%d)\n", CONFIG_SYS_PROM_OFFSET,
260                CONFIG_SYS_PROM_SIZE);
261         printf("CONFIG_SYS_GBL_DATA_OFFSET:    0x%lx (%d)\n", CONFIG_SYS_GBL_DATA_OFFSET,
262                CONFIG_SYS_GBL_DATA_SIZE);
263 #endif
264
265 #ifdef CONFIG_POST
266         post_bootmode_init();
267         post_run(NULL, POST_ROM | post_bootmode_get(0));
268 #endif
269
270         /*
271          * We have to relocate the command table manually
272          */
273         for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
274                 ulong addr;
275                 addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
276 #if DEBUG_COMMANDS
277                 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
278                        cmdtp->name, (ulong) (cmdtp->cmd), addr);
279 #endif
280                 cmdtp->cmd =
281                     (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;
282
283                 addr = (ulong) (cmdtp->name) + gd->reloc_off;
284                 cmdtp->name = (char *)addr;
285
286                 if (cmdtp->usage) {
287                         addr = (ulong) (cmdtp->usage) + gd->reloc_off;
288                         cmdtp->usage = (char *)addr;
289                 }
290 #ifdef  CONFIG_SYS_LONGHELP
291                 if (cmdtp->help) {
292                         addr = (ulong) (cmdtp->help) + gd->reloc_off;
293                         cmdtp->help = (char *)addr;
294                 }
295 #endif
296         }
297
298 #if defined(CONFIG_CMD_AMBAPP) && defined(CONFIG_SYS_AMBAPP_PRINT_ON_STARTUP)
299         puts("AMBA:\n");
300         do_ambapp_print(NULL, 0, 0, NULL);
301 #endif
302
303         /* initialize higher level parts of CPU like time base and timers */
304         cpu_init_r();
305
306         /* start timer */
307         timer_interrupt_init();
308
309         /*
310          * Enable Interrupts before any calls to udelay,
311          * the flash driver may use udelay resulting in
312          * a hang if not timer0 IRQ is enabled.
313          */
314         interrupt_init();
315
316         /* initialize malloc() area */
317         mem_malloc_init(CONFIG_SYS_MALLOC_BASE,
318                         CONFIG_SYS_MALLOC_END - CONFIG_SYS_MALLOC_BASE);
319         malloc_bin_reloc();
320
321 #if !defined(CONFIG_SYS_NO_FLASH)
322         puts("FLASH: ");
323
324         if ((flash_size = flash_init()) > 0) {
325 # ifdef CONFIG_SYS_FLASH_CHECKSUM
326                 print_size(flash_size, "");
327                 /*
328                  * Compute and print flash CRC if flashchecksum is set to 'y'
329                  *
330                  * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
331                  */
332                 s = getenv("flashchecksum");
333                 if (s && (*s == 'y')) {
334                         printf("  CRC: %08lX",
335                                crc32(0, (const unsigned char *)CONFIG_SYS_FLASH_BASE,
336                                      flash_size)
337                             );
338                 }
339                 putc('\n');
340 # else                          /* !CONFIG_SYS_FLASH_CHECKSUM */
341                 print_size(flash_size, "\n");
342 # endif                         /* CONFIG_SYS_FLASH_CHECKSUM */
343         } else {
344                 puts(failed);
345                 hang();
346         }
347
348         bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;      /* update start of FLASH memory    */
349         bd->bi_flashsize = flash_size;  /* size of FLASH memory (final value) */
350 #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
351         bd->bi_flashoffset = monitor_flash_len; /* reserved area for startup monitor  */
352 #else
353         bd->bi_flashoffset = 0;
354 #endif
355 #else                           /* CONFIG_SYS_NO_FLASH */
356         bd->bi_flashsize = 0;
357         bd->bi_flashstart = 0;
358         bd->bi_flashoffset = 0;
359 #endif                          /* !CONFIG_SYS_NO_FLASH */
360
361 #ifdef CONFIG_SPI
362 # if !defined(CONFIG_ENV_IS_IN_EEPROM)
363         spi_init_f();
364 # endif
365         spi_init_r();
366 #endif
367
368         /* relocate environment function pointers etc. */
369         env_relocate();
370
371 #if defined(CONFIG_BOARD_LATE_INIT)
372         board_late_init();
373 #endif
374
375 #ifdef CONFIG_ID_EEPROM
376         mac_read_from_eeprom();
377 #endif
378
379         /* IP Address */
380         bd->bi_ip_addr = getenv_IPaddr("ipaddr");
381 #if defined(CONFIG_PCI)
382         /*
383          * Do pci configuration
384          */
385         pci_init();
386 #endif
387
388         /* Initialize stdio devices */
389         stdio_init();
390
391         /* Initialize the jump table for applications */
392         jumptable_init();
393
394         /* Initialize the console (after the relocation and devices init) */
395         console_init_r();
396
397 #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
398         serial_buffered_init();
399 #endif
400
401 #ifdef CONFIG_STATUS_LED
402         status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
403 #endif
404
405         udelay(20);
406
407         set_timer(0);
408
409         /* Initialize from environment */
410         if ((s = getenv("loadaddr")) != NULL) {
411                 load_addr = simple_strtoul(s, NULL, 16);
412         }
413 #if defined(CONFIG_CMD_NET)
414         if ((s = getenv("bootfile")) != NULL) {
415                 copy_filename(BootFile, s, sizeof(BootFile));
416         }
417 #endif /* CONFIG_CMD_NET */
418
419         WATCHDOG_RESET();
420
421 #if defined(CONFIG_CMD_DOC)
422         WATCHDOG_RESET();
423         puts("DOC:   ");
424         doc_init();
425 #endif
426
427 #if defined(CONFIG_CMD_NET)
428 #if defined(CONFIG_NET_MULTI)
429         WATCHDOG_RESET();
430         puts("Net:   ");
431 #endif
432         eth_initialize(bd);
433 #endif
434
435 #if defined(CONFIG_CMD_NET) && defined(CONFIG_RESET_PHY_R)
436         WATCHDOG_RESET();
437         debug("Reset Ethernet PHY\n");
438         reset_phy();
439 #endif
440
441 #ifdef CONFIG_POST
442         post_run(NULL, POST_RAM | post_bootmode_get(0));
443 #endif
444
445 #if defined(CONFIG_CMD_IDE)
446         WATCHDOG_RESET();
447         puts("IDE:   ");
448         ide_init();
449 #endif /* CONFIG_CMD_IDE */
450
451 #ifdef CONFIG_LAST_STAGE_INIT
452         WATCHDOG_RESET();
453         /*
454          * Some parts can be only initialized if all others (like
455          * Interrupts) are up and running (i.e. the PC-style ISA
456          * keyboard).
457          */
458         last_stage_init();
459 #endif
460
461 #ifdef CONFIG_PS2KBD
462         puts("PS/2:  ");
463         kbd_init();
464 #endif
465         prom_init();
466
467         /* main_loop */
468         for (;;) {
469                 WATCHDOG_RESET();
470                 main_loop();
471         }
472
473 }
474
475 void hang(void)
476 {
477         puts("### ERROR ### Please RESET the board ###\n");
478 #ifdef CONFIG_SHOW_BOOT_PROGRESS
479         show_boot_progress(-30);
480 #endif
481         for (;;) ;
482 }
483
484 /************************************************************************/