Consolidate arch-specific sbrk() implementations
[platform/kernel/u-boot.git] / 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(void)
86 {
87         mem_malloc_start = CONFIG_SYS_MALLOC_BASE;
88         mem_malloc_end = CONFIG_SYS_MALLOC_END;
89         mem_malloc_brk = mem_malloc_start;
90         memset((void *)mem_malloc_start, 0, mem_malloc_end - mem_malloc_start);
91 }
92
93 /***********************************************************************/
94
95 /************************************************************************
96  * Init Utilities                                                       *
97  ************************************************************************
98  * Some of this code should be moved into the core functions,
99  * but let's get it working (again) first...
100  */
101
102 static int init_baudrate(void)
103 {
104         char tmp[64];           /* long enough for environment variables */
105         int i = getenv_r("baudrate", tmp, sizeof(tmp));
106
107         gd->baudrate = (i > 0)
108             ? (int)simple_strtoul(tmp, NULL, 10)
109             : CONFIG_BAUDRATE;
110         return (0);
111 }
112
113 /***********************************************************************/
114
115 /*
116  * All attempts to come up with a "common" initialization sequence
117  * that works for all boards and architectures failed: some of the
118  * requirements are just _too_ different. To get rid of the resulting
119  * mess of board dependend #ifdef'ed code we now make the whole
120  * initialization sequence configurable to the user.
121  *
122  * The requirements for any new initalization function is simple: it
123  * receives a pointer to the "global data" structure as it's only
124  * argument, and returns an integer return code, where 0 means
125  * "continue" and != 0 means "fatal error, hang the system".
126  */
127 typedef int (init_fnc_t) (void);
128
129 #define WATCHDOG_RESET(x)
130
131 /************************************************************************
132  * Initialization sequence                                              *
133  ************************************************************************
134  */
135
136 init_fnc_t *init_sequence[] = {
137
138 #if defined(CONFIG_BOARD_EARLY_INIT_F)
139         board_early_init_f,
140 #endif
141         serial_init,
142
143         init_timebase,
144
145 #if defined(CONFIG_CMD_AMBAPP)
146         ambapp_init_reloc,
147 #endif
148
149         env_init,
150
151         init_baudrate,
152
153         console_init_f,
154         display_options,
155
156         checkcpu,
157         checkboard,
158 #if defined(CONFIG_MISC_INIT_F)
159         misc_init_f,
160 #endif
161
162 #ifdef CONFIG_POST
163         post_init_f,
164 #endif
165
166         NULL,                   /* Terminate this list,
167                                  * beware: this list will be relocated
168                                  * which means that NULL will become
169                                  * NULL+RELOC_OFFSET. We simply make
170                                  * NULL be -RELOC_OFFSET instead.
171                                  */
172 };
173
174 /************************************************************************
175  *
176  * This is the SPARC board initialization routine, running from RAM.
177  *
178  ************************************************************************
179  */
180 #ifdef DEBUG_INIT_SEQUENCE
181 char *str_init_seq = "INIT_SEQ 00\n";
182 char *str_init_seq_done = "\n\rInit sequence done...\r\n\r\n";
183 #endif
184
185 void board_init_f(ulong bootflag)
186 {
187         cmd_tbl_t *cmdtp;
188         bd_t *bd;
189         unsigned char *s;
190         init_fnc_t **init_fnc_ptr;
191         int j;
192         int i;
193         char *e;
194
195 #ifndef CONFIG_SYS_NO_FLASH
196         ulong flash_size;
197 #endif
198
199         gd = (gd_t *) (CONFIG_SYS_GBL_DATA_OFFSET);
200
201         /* Clear initial global data */
202         memset((void *)gd, 0, sizeof(gd_t));
203
204         gd->bd = (bd_t *) (gd + 1);     /* At end of global data */
205         gd->baudrate = CONFIG_BAUDRATE;
206         gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
207
208         bd = gd->bd;
209         bd->bi_memstart = CONFIG_SYS_RAM_BASE;
210         bd->bi_memsize = CONFIG_SYS_RAM_SIZE;
211         bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
212 #if     defined(CONFIG_SYS_SRAM_BASE) && defined(CONFIG_SYS_SRAM_SIZE)
213         bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;
214         bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;
215 #endif
216         bd->bi_baudrate = CONFIG_BAUDRATE;
217         bd->bi_bootflags = bootflag;    /* boot / reboot flag (for LynxOS)    */
218
219         gd->flags |= GD_FLG_RELOC;      /* tell others: relocation done */
220         gd->reloc_off = CONFIG_SYS_RELOC_MONITOR_BASE - CONFIG_SYS_MONITOR_BASE;
221
222         for (init_fnc_ptr = init_sequence, j = 0; *init_fnc_ptr;
223              ++init_fnc_ptr, j++) {
224 #ifdef DEBUG_INIT_SEQUENCE
225                 if (j > 9)
226                         str_init_seq[9] = '0' + (j / 10);
227                 str_init_seq[10] = '0' + (j - (j / 10) * 10);
228                 serial_puts(str_init_seq);
229 #endif
230                 if ((*init_fnc_ptr + gd->reloc_off) () != 0) {
231                         hang();
232                 }
233         }
234 #ifdef DEBUG_INIT_SEQUENCE
235         serial_puts(str_init_seq_done);
236 #endif
237
238         /*
239          * Now that we have DRAM mapped and working, we can
240          * relocate the code and continue running from DRAM.
241          *
242          * Reserve memory at end of RAM for (top down in that order):
243          *  - kernel log buffer
244          *  - protected RAM
245          *  - LCD framebuffer
246          *  - monitor code
247          *  - board info struct
248          */
249 #ifdef DEBUG_MEM_LAYOUT
250         printf("CONFIG_SYS_MONITOR_BASE:       0x%lx\n", CONFIG_SYS_MONITOR_BASE);
251         printf("CONFIG_ENV_ADDR:           0x%lx\n", CONFIG_ENV_ADDR);
252         printf("CONFIG_SYS_RELOC_MONITOR_BASE: 0x%lx (%d)\n", CONFIG_SYS_RELOC_MONITOR_BASE,
253                CONFIG_SYS_MONITOR_LEN);
254         printf("CONFIG_SYS_MALLOC_BASE:        0x%lx (%d)\n", CONFIG_SYS_MALLOC_BASE,
255                CONFIG_SYS_MALLOC_LEN);
256         printf("CONFIG_SYS_INIT_SP_OFFSET:     0x%lx (%d)\n", CONFIG_SYS_INIT_SP_OFFSET,
257                CONFIG_SYS_STACK_SIZE);
258         printf("CONFIG_SYS_PROM_OFFSET:        0x%lx (%d)\n", CONFIG_SYS_PROM_OFFSET,
259                CONFIG_SYS_PROM_SIZE);
260         printf("CONFIG_SYS_GBL_DATA_OFFSET:    0x%lx (%d)\n", CONFIG_SYS_GBL_DATA_OFFSET,
261                CONFIG_SYS_GBL_DATA_SIZE);
262 #endif
263
264 #ifdef CONFIG_POST
265         post_bootmode_init();
266         post_run(NULL, POST_ROM | post_bootmode_get(0));
267 #endif
268
269         /*
270          * We have to relocate the command table manually
271          */
272         for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
273                 ulong addr;
274                 addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
275 #if DEBUG_COMMANDS
276                 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
277                        cmdtp->name, (ulong) (cmdtp->cmd), addr);
278 #endif
279                 cmdtp->cmd =
280                     (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;
281
282                 addr = (ulong) (cmdtp->name) + gd->reloc_off;
283                 cmdtp->name = (char *)addr;
284
285                 if (cmdtp->usage) {
286                         addr = (ulong) (cmdtp->usage) + gd->reloc_off;
287                         cmdtp->usage = (char *)addr;
288                 }
289 #ifdef  CONFIG_SYS_LONGHELP
290                 if (cmdtp->help) {
291                         addr = (ulong) (cmdtp->help) + gd->reloc_off;
292                         cmdtp->help = (char *)addr;
293                 }
294 #endif
295         }
296
297 #if defined(CONFIG_CMD_AMBAPP) && defined(CONFIG_SYS_AMBAPP_PRINT_ON_STARTUP)
298         puts("AMBA:\n");
299         do_ambapp_print(NULL, 0, 0, NULL);
300 #endif
301
302         /* initialize higher level parts of CPU like time base and timers */
303         cpu_init_r();
304
305         /* start timer */
306         timer_interrupt_init();
307
308         /*
309          * Enable Interrupts before any calls to udelay,
310          * the flash driver may use udelay resulting in
311          * a hang if not timer0 IRQ is enabled.
312          */
313         interrupt_init();
314
315         /* initialize malloc() area */
316         mem_malloc_init();
317         malloc_bin_reloc();
318
319 #if !defined(CONFIG_SYS_NO_FLASH)
320         puts("FLASH: ");
321
322         if ((flash_size = flash_init()) > 0) {
323 # ifdef CONFIG_SYS_FLASH_CHECKSUM
324                 print_size(flash_size, "");
325                 /*
326                  * Compute and print flash CRC if flashchecksum is set to 'y'
327                  *
328                  * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
329                  */
330                 s = getenv("flashchecksum");
331                 if (s && (*s == 'y')) {
332                         printf("  CRC: %08lX",
333                                crc32(0, (const unsigned char *)CONFIG_SYS_FLASH_BASE,
334                                      flash_size)
335                             );
336                 }
337                 putc('\n');
338 # else                          /* !CONFIG_SYS_FLASH_CHECKSUM */
339                 print_size(flash_size, "\n");
340 # endif                         /* CONFIG_SYS_FLASH_CHECKSUM */
341         } else {
342                 puts(failed);
343                 hang();
344         }
345
346         bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;      /* update start of FLASH memory    */
347         bd->bi_flashsize = flash_size;  /* size of FLASH memory (final value) */
348 #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
349         bd->bi_flashoffset = monitor_flash_len; /* reserved area for startup monitor  */
350 #else
351         bd->bi_flashoffset = 0;
352 #endif
353 #else                           /* CONFIG_SYS_NO_FLASH */
354         bd->bi_flashsize = 0;
355         bd->bi_flashstart = 0;
356         bd->bi_flashoffset = 0;
357 #endif                          /* !CONFIG_SYS_NO_FLASH */
358
359 #ifdef CONFIG_SPI
360 # if !defined(CONFIG_ENV_IS_IN_EEPROM)
361         spi_init_f();
362 # endif
363         spi_init_r();
364 #endif
365
366         /* relocate environment function pointers etc. */
367         env_relocate();
368
369 #if defined(CONFIG_BOARD_LATE_INIT)
370         board_late_init();
371 #endif
372
373 #ifdef CONFIG_ID_EEPROM
374         mac_read_from_eeprom();
375 #endif
376
377         /* IP Address */
378         bd->bi_ip_addr = getenv_IPaddr("ipaddr");
379 #if defined(CONFIG_PCI)
380         /*
381          * Do pci configuration
382          */
383         pci_init();
384 #endif
385
386         /* Initialize stdio devices */
387         stdio_init();
388
389         /* Initialize the jump table for applications */
390         jumptable_init();
391
392         /* Initialize the console (after the relocation and devices init) */
393         console_init_r();
394
395 #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
396         serial_buffered_init();
397 #endif
398
399 #ifdef CONFIG_STATUS_LED
400         status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
401 #endif
402
403         udelay(20);
404
405         set_timer(0);
406
407         /* Initialize from environment */
408         if ((s = getenv("loadaddr")) != NULL) {
409                 load_addr = simple_strtoul(s, NULL, 16);
410         }
411 #if defined(CONFIG_CMD_NET)
412         if ((s = getenv("bootfile")) != NULL) {
413                 copy_filename(BootFile, s, sizeof(BootFile));
414         }
415 #endif /* CONFIG_CMD_NET */
416
417         WATCHDOG_RESET();
418
419 #if defined(CONFIG_CMD_DOC)
420         WATCHDOG_RESET();
421         puts("DOC:   ");
422         doc_init();
423 #endif
424
425 #if defined(CONFIG_CMD_NET)
426 #if defined(CONFIG_NET_MULTI)
427         WATCHDOG_RESET();
428         puts("Net:   ");
429 #endif
430         eth_initialize(bd);
431 #endif
432
433 #if defined(CONFIG_CMD_NET) && defined(CONFIG_RESET_PHY_R)
434         WATCHDOG_RESET();
435         debug("Reset Ethernet PHY\n");
436         reset_phy();
437 #endif
438
439 #ifdef CONFIG_POST
440         post_run(NULL, POST_RAM | post_bootmode_get(0));
441 #endif
442
443 #if defined(CONFIG_CMD_IDE)
444         WATCHDOG_RESET();
445         puts("IDE:   ");
446         ide_init();
447 #endif /* CONFIG_CMD_IDE */
448
449 #ifdef CONFIG_LAST_STAGE_INIT
450         WATCHDOG_RESET();
451         /*
452          * Some parts can be only initialized if all others (like
453          * Interrupts) are up and running (i.e. the PC-style ISA
454          * keyboard).
455          */
456         last_stage_init();
457 #endif
458
459 #ifdef CONFIG_PS2KBD
460         puts("PS/2:  ");
461         kbd_init();
462 #endif
463         prom_init();
464
465         /* main_loop */
466         for (;;) {
467                 WATCHDOG_RESET();
468                 main_loop();
469         }
470
471 }
472
473 void hang(void)
474 {
475         puts("### ERROR ### Please RESET the board ###\n");
476 #ifdef CONFIG_SHOW_BOOT_PROGRESS
477         show_boot_progress(-30);
478 #endif
479         for (;;) ;
480 }
481
482 /************************************************************************/