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