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