2 * (C) Copyright 2000-2003
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
30 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
33 #if (CONFIG_COMMANDS & CFG_CMD_SCSI)
36 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
39 #ifdef CONFIG_STATUS_LED
40 #include <status_led.h>
43 #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
44 #include <cmd_bedbug.h>
46 #ifdef CFG_ALLOC_DPRAM
51 static char *failed = "*** failed ***\n";
54 extern flash_info_t flash_info[];
57 #if defined(CFG_ENV_IS_IN_FLASH)
59 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
62 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
64 # if (CFG_ENV_ADDR >= CFG_MONITOR_BASE) && \
65 (CFG_ENV_ADDR+CFG_ENV_SIZE) < (CFG_MONITOR_BASE + CFG_MONITOR_LEN)
66 # define ENV_IS_EMBEDDED
68 #endif /* CFG_ENV_IS_IN_FLASH */
69 #if ( ((CFG_ENV_ADDR+CFG_ENV_SIZE) < CFG_MONITOR_BASE) || \
70 (CFG_ENV_ADDR >= (CFG_MONITOR_BASE + CFG_MONITOR_LEN)) ) || \
71 defined(CFG_ENV_IS_IN_NVRAM)
72 #define TOTAL_MALLOC_LEN (CFG_MALLOC_LEN + CFG_ENV_SIZE)
74 #define TOTAL_MALLOC_LEN CFG_MALLOC_LEN
78 * Begin and End of memory area for malloc(), and current "brk"
80 static ulong mem_malloc_start = 0;
81 static ulong mem_malloc_end = 0;
82 static ulong mem_malloc_brk = 0;
84 /************************************************************************
86 ************************************************************************
90 * The Malloc area is immediately below the monitor copy in DRAM
92 static void mem_malloc_init (ulong dest_addr)
94 mem_malloc_end = dest_addr;
95 mem_malloc_start = dest_addr - TOTAL_MALLOC_LEN;
96 mem_malloc_brk = mem_malloc_start;
98 memset ((void *) mem_malloc_start, 0,
99 mem_malloc_end - mem_malloc_start);
102 void *sbrk (ptrdiff_t increment)
104 ulong old = mem_malloc_brk;
105 ulong new = old + increment;
107 if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
110 mem_malloc_brk = new;
111 return ((void *) old);
114 char *strmhz (char *buf, long hz)
121 l = sprintf (buf, "%ld", n);
123 m = (hz % 1000000L) / 1000L;
126 sprintf (buf + l, ".%03ld", m);
131 static void syscalls_init (int reloc_off)
135 addr = (ulong *) syscall_tbl;
136 syscall_tbl[SYSCALL_MALLOC] = (void *) malloc;
137 syscall_tbl[SYSCALL_FREE] = (void *) free;
139 syscall_tbl[SYSCALL_INSTALL_HDLR] = (void *) irq_install_handler;
140 syscall_tbl[SYSCALL_FREE_HDLR] = (void *) irq_free_handler;
142 addr = (ulong *) 0xc00; /* syscall ISR addr */
145 *addr++ |= (ulong) syscall_tbl >> 16;
146 *addr++ |= (ulong) syscall_tbl & 0xFFFF;
147 *addr++ |= NR_SYSCALLS >> 16;
148 *addr++ |= NR_SYSCALLS & 0xFFFF;
151 /************************************************************************
153 * This is the first part of the initialization sequence that is
154 * implemented in C, but still running from ROM.
156 * The main purpose is to provide a (serial) console interface as
157 * soon as possible (so we can see any error messages), and to
158 * initialize the RAM so that we can relocate the monitor code to
161 * Be aware of the restrictions: global data is read-only, BSS is not
162 * initialized, and stack space is limited to a few kB.
164 ************************************************************************
172 void board_init_f (ulong bootflag)
174 DECLARE_GLOBAL_DATA_PTR;
177 ulong reg, len, addr, addr_sp, dram_size;
178 int i, baudrate, board_type;
180 uchar tmp[64]; /* long enough for environment variables */
182 /* Pointer to initial global data area */
183 gd = global_data = &gdata;
184 bd = gd->bd = &bdata;
189 i = getenv_r ("baudrate", tmp, sizeof (tmp));
191 (i > 0) ? (int) simple_strtoul (tmp, NULL,
192 10) : CONFIG_BAUDRATE;
193 bd->bi_baudrate = baudrate; /* Console Baudrate */
195 /* set up serial port */
198 /* Initialize the console (before the relocation) */
202 if (sizeof (init_data_t) > CFG_INIT_DATA_SIZE) {
203 printf ("PANIC: sizeof(init_data_t)=%d > CFG_INIT_DATA_SIZE=%d\n", sizeof (init_data_t), CFG_INIT_DATA_SIZE);
208 /* now we can use standard printf/puts/getc/tstc functions */
211 puts ("CPU: "); /* Check CPU */
212 if (checkcpu () < 0) {
217 puts ("Board: "); /* Check Board */
218 if ((board_type = checkboard ()) < 0) {
224 if ((dram_size = initdram (board_type)) > 0) {
225 printf ("%2ld MB\n", dram_size >> 20);
231 #if defined(CFG_DRAM_TEST)
232 if (testdram () != 0) {
235 #endif /* CFG_DRAM_TEST */
238 * Now that we have DRAM mapped and working, we can
239 * relocate the code and continue running from DRAM.
241 * Reserve memory at end of RAM for (top down in that order):
245 * - board info struct
247 len = get_endaddr () - CFG_MONITOR_BASE;
249 if (len > CFG_MONITOR_LEN) {
250 printf ("*** u-boot size %ld > reserved memory (%d)\n",
251 len, CFG_MONITOR_LEN);
255 if (CFG_MONITOR_LEN > len)
256 len = CFG_MONITOR_LEN;
258 addr = CFG_SDRAM_BASE + dram_size;
262 * Save local variables to board info struct
264 bd->bi_memstart = CFG_SDRAM_BASE; /* start of DRAM memory */
265 bd->bi_memsize = dram_size; /* size of DRAM memory in bytes */
266 bd->bi_bootflags = bootflag; /* boot / reboot flag (for LynxOS) */
268 i = getenv_r ("ethaddr", tmp, sizeof (tmp));
269 s = (i > 0) ? tmp : NULL;
271 for (reg = 0; reg < 6; ++reg) {
272 bd->bi_enetaddr[reg] = s ? simple_strtoul (s, &e, 16) : 0;
274 s = (*e) ? e + 1 : e;
277 bd->bi_intfreq = get_gclk_freq (); /* Internal Freq, in Hz */
278 bd->bi_busfreq = get_bus_freq (get_gclk_freq ()); /* Bus Freq, in Hz */
281 strncpy (bd->bi_s_version, "1.2", sizeof (bd->bi_s_version));
282 strncpy (bd->bi_r_version, PPCBOOT_VERSION,
283 sizeof (bd->bi_r_version));
285 bd->bi_procfreq = get_gclk_freq (); /* Processor Speed, In Hz */
286 bd->bi_plb_busfreq = bd->bi_busfreq;
289 board_init_final (addr);
294 /************************************************************************
296 * This is the next part if the initialization sequence: we are now
297 * running from RAM and have a "normal" C environment, i. e. global
298 * data can be written, BSS has been cleared, the stack size in not
299 * that critical any more, etc.
301 ************************************************************************
304 void board_init_final (ulong dest_addr)
306 DECLARE_GLOBAL_DATA_PTR;
313 /* icache_enable(); /XX* it's time to enable the instruction cache */
316 * Setup trap handlers
318 trap_init (dest_addr);
322 if ((flash_size = flash_init ()) > 0) {
323 #ifdef CFG_FLASH_CHECKSUM
324 if (flash_size >= (1 << 20)) {
325 printf ("%2ld MB", flash_size >> 20);
327 printf ("%2ld kB", flash_size >> 10);
330 * Compute and print flash CRC if flashchecksum is set to 'y'
332 * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
334 s = getenv ("flashchecksum");
335 if (s && (*s == 'y')) {
336 printf (" CRC: %08lX",
338 (const unsigned char *) CFG_FLASH_BASE,
344 if (flash_size >= (1 << 20)) {
345 printf ("%2ld MB\n", flash_size >> 20);
347 printf ("%2ld kB\n", flash_size >> 10);
349 #endif /* CFG_FLASH_CHECKSUM */
355 bd->bi_flashstart = CFG_FLASH_BASE; /* update start of FLASH memory */
356 bd->bi_flashsize = flash_size; /* size of FLASH memory (final value) */
357 bd->bi_flashoffset = 0x10000; /* reserved area for startup monitor */
361 /* initialize higher level parts of CPU like time base and timers */
366 /* initialize malloc() area */
367 mem_malloc_init (dest_addr);
370 # if !defined(CFG_ENV_IS_IN_EEPROM)
376 /* relocate environment function pointers etc. */
380 bd->bi_ip_addr = getenv_IPaddr ("ipaddr");
384 /* allocate syscalls table (console_init_r will fill it in */
385 syscall_tbl = (void **) malloc (NR_SYSCALLS * sizeof (void *));
387 /* Initialize the console (after the relocation and devices init) */
390 #if (CONFIG_COMMANDS & CFG_CMD_NET) && ( \
391 defined(CONFIG_CCM) || \
392 defined(CONFIG_EP8260) || \
393 defined(CONFIG_IP860) || \
394 defined(CONFIG_IVML24) || \
395 defined(CONFIG_IVMS8) || \
396 defined(CONFIG_LWMON) || \
397 defined(CONFIG_MPC8260ADS) || \
398 defined(CONFIG_PCU_E) || \
399 defined(CONFIG_RPXSUPER) || \
400 defined(CONFIG_SPD823TS) )
404 puts ("Reset Ethernet PHY\n");
410 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
423 /* Insert function pointers now that we have relocated the code */
425 /* Initialize from environment */
426 if ((s = getenv ("loadaddr")) != NULL) {
427 load_addr = simple_strtoul (s, NULL, 16);
429 #if (CONFIG_COMMANDS & CFG_CMD_NET)
430 if ((s = getenv ("bootfile")) != NULL) {
431 copy_filename (BootFile, s, sizeof (BootFile));
433 #endif /* CFG_CMD_NET */
437 #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI)
443 #ifdef CONFIG_LAST_STAGE_INIT
446 * Some parts can be only initialized if all others (like
447 * Interrupts) are up and running (i.e. the PC-style ISA
453 #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
460 * Export available size of memory for Linux,
461 * taking into account the protected RAM at top of memory
468 if ((s = getenv ("pram")) != NULL) {
469 pram = simple_strtoul (s, NULL, 10);
473 sprintf (memsz, "%ldk", (bd->bi_memsize / 1024) - pram);
474 setenv ("mem", memsz);
478 /* Initialization complete - start the monitor */
480 /* main_loop() can return to retry autoboot, if so just run it again. */
486 /* NOTREACHED - no way out of command loop except booting */
491 puts ("### ERROR ### Please RESET the board ###\n");