1 /*
2  * (C) Copyright 2000-2010
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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,
21  * MA 02111-1307 USA
22  */
23 
24 #include <common.h>
25 #include <watchdog.h>
26 #include <command.h>
27 #include <malloc.h>
28 #include <stdio_dev.h>
29 #ifdef CONFIG_8xx
30 #include <mpc8xx.h>
31 #endif
32 #ifdef CONFIG_5xx
33 #include <mpc5xx.h>
34 #endif
35 #ifdef CONFIG_MPC5xxx
36 #include <mpc5xxx.h>
37 #endif
38 #if defined(CONFIG_CMD_IDE)
39 #include <ide.h>
40 #endif
41 #if defined(CONFIG_CMD_SCSI)
42 #include <scsi.h>
43 #endif
44 #if defined(CONFIG_CMD_KGDB)
45 #include <kgdb.h>
46 #endif
47 #ifdef CONFIG_STATUS_LED
48 #include <status_led.h>
49 #endif
50 #include <net.h>
51 #ifdef CONFIG_GENERIC_MMC
52 #include <mmc.h>
53 #endif
54 #include <serial.h>
55 #ifdef CONFIG_SYS_ALLOC_DPRAM
56 #if !defined(CONFIG_CPM2)
57 #include <commproc.h>
58 #endif
59 #endif
60 #include <version.h>
61 #if defined(CONFIG_BAB7xx)
62 #include <w83c553f.h>
63 #endif
64 #include <dtt.h>
65 #if defined(CONFIG_POST)
66 #include <post.h>
67 #endif
68 #if defined(CONFIG_LOGBUFFER)
69 #include <logbuff.h>
70 #endif
71 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
72 #include <asm/cache.h>
73 #endif
74 #ifdef CONFIG_PS2KBD
75 #include <keyboard.h>
76 #endif
77 
78 #ifdef CONFIG_ADDR_MAP
79 #include <asm/mmu.h>
80 #endif
81 
82 #ifdef CONFIG_MP
83 #include <asm/mp.h>
84 #endif
85 
86 #ifdef CONFIG_BITBANGMII
87 #include <miiphy.h>
88 #endif
89 
90 #ifdef CONFIG_SYS_UPDATE_FLASH_SIZE
91 extern int update_flash_size (int flash_size);
92 #endif
93 
94 #if defined(CONFIG_SC3)
95 extern void sc3_read_eeprom(void);
96 #endif
97 
98 #if defined(CONFIG_CMD_DOC)
99 void doc_init (void);
100 #endif
101 #if defined(CONFIG_HARD_I2C) || \
102     defined(CONFIG_SOFT_I2C)
103 #include <i2c.h>
104 #endif
105 #include <spi.h>
106 #include <nand.h>
107 
108 #ifdef CONFIG_SAM460EX
109 #include <asm/io.h>
110 #endif
111 
112 static char *failed = "*** failed ***\n";
113 
114 #if defined(CONFIG_OXC) || defined(CONFIG_PCU_E) || defined(CONFIG_RMU)
115 extern flash_info_t flash_info[];
116 #endif
117 
118 #if defined(CONFIG_START_IDE)
119 extern int board_start_ide(void);
120 #endif
121 #include <environment.h>
122 
123 DECLARE_GLOBAL_DATA_PTR;
124 
125 #if defined(CONFIG_ENV_IS_EMBEDDED)
126 #define TOTAL_MALLOC_LEN	CONFIG_SYS_MALLOC_LEN
127 #elif ( ((CONFIG_ENV_ADDR+CONFIG_ENV_SIZE) < CONFIG_SYS_MONITOR_BASE) || \
128 	(CONFIG_ENV_ADDR >= (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)) ) || \
129       defined(CONFIG_ENV_IS_IN_NVRAM)
130 #define	TOTAL_MALLOC_LEN	(CONFIG_SYS_MALLOC_LEN + CONFIG_ENV_SIZE)
131 #else
132 #define	TOTAL_MALLOC_LEN	CONFIG_SYS_MALLOC_LEN
133 #endif
134 
135 #if !defined(CONFIG_SYS_MEM_TOP_HIDE)
136 #define CONFIG_SYS_MEM_TOP_HIDE	0
137 #endif
138 
139 extern ulong __init_end;
140 extern ulong _end;
141 ulong monitor_flash_len;
142 
143 #if defined(CONFIG_CMD_BEDBUG)
144 #include <bedbug/type.h>
145 #endif
146 
147 /************************************************************************
148  * Utilities								*
149  ************************************************************************
150  */
151 
152 /*
153  * All attempts to come up with a "common" initialization sequence
154  * that works for all boards and architectures failed: some of the
155  * requirements are just _too_ different. To get rid of the resulting
156  * mess of board dependend #ifdef'ed code we now make the whole
157  * initialization sequence configurable to the user.
158  *
159  * The requirements for any new initalization function is simple: it
160  * receives a pointer to the "global data" structure as it's only
161  * argument, and returns an integer return code, where 0 means
162  * "continue" and != 0 means "fatal error, hang the system".
163  */
164 typedef int (init_fnc_t) (void);
165 
166 /************************************************************************
167  * Init Utilities							*
168  ************************************************************************
169  * Some of this code should be moved into the core functions,
170  * but let's get it working (again) first...
171  */
172 
init_baudrate(void)173 static int init_baudrate (void)
174 {
175 	char tmp[64];	/* long enough for environment variables */
176 	int i = getenv_r ("baudrate", tmp, sizeof (tmp));
177 
178 	gd->baudrate = (i > 0)
179 			? (int) simple_strtoul (tmp, NULL, 10)
180 			: CONFIG_BAUDRATE;
181 	return (0);
182 }
183 
184 /***********************************************************************/
185 
__board_add_ram_info(int use_default)186 void __board_add_ram_info(int use_default)
187 {
188 	/* please define platform specific board_add_ram_info() */
189 }
190 void board_add_ram_info(int) __attribute__((weak, alias("__board_add_ram_info")));
191 
192 
init_func_ram(void)193 static int init_func_ram (void)
194 {
195 #ifdef	CONFIG_BOARD_TYPES
196 	int board_type = gd->board_type;
197 #else
198 	int board_type = 0;	/* use dummy arg */
199 #endif
200 	puts ("DRAM:  ");
201 
202 #ifdef CONFIG_SAM460EX
203     initdram (board_type);
204 #endif
205 
206 	if ((gd->ram_size = initdram (board_type)) > 0) {
207 		print_size (gd->ram_size, "");
208 		board_add_ram_info(0);
209 		putc('\n');
210 		return (0);
211 	}
212 	puts (failed);
213 	return (1);
214 }
215 
216 /***********************************************************************/
217 
218 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
init_func_i2c(void)219 static int init_func_i2c (void)
220 {
221 	puts ("I2C:   ");
222 	i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
223 	puts ("ready\n");
224 	return (0);
225 }
226 #endif
227 
228 #if defined(CONFIG_HARD_SPI)
init_func_spi(void)229 static int init_func_spi (void)
230 {
231 	puts ("SPI:   ");
232 	spi_init ();
233 	puts ("ready\n");
234 	return (0);
235 }
236 #endif
237 
238 /***********************************************************************/
239 
240 #if defined(CONFIG_WATCHDOG)
init_func_watchdog_init(void)241 static int init_func_watchdog_init (void)
242 {
243 	puts ("       Watchdog enabled\n");
244 	WATCHDOG_RESET ();
245 	return (0);
246 }
247 # define INIT_FUNC_WATCHDOG_INIT	init_func_watchdog_init,
248 
init_func_watchdog_reset(void)249 static int init_func_watchdog_reset (void)
250 {
251 	WATCHDOG_RESET ();
252 	return (0);
253 }
254 # define INIT_FUNC_WATCHDOG_RESET	init_func_watchdog_reset,
255 #else
256 # define INIT_FUNC_WATCHDOG_INIT	/* undef */
257 # define INIT_FUNC_WATCHDOG_RESET	/* undef */
258 #endif /* CONFIG_WATCHDOG */
259 
260 /************************************************************************
261  * Initialization sequence						*
262  ************************************************************************
263  */
264 
265 init_fnc_t *init_sequence[] = {
266 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
267 	probecpu,
268 #endif
269 #if defined(CONFIG_BOARD_EARLY_INIT_F)
270 	board_early_init_f,
271 #endif
272 #if !defined(CONFIG_8xx_CPUCLK_DEFAULT)
273 	get_clocks,		/* get CPU and bus clocks (etc.) */
274 #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
275     && !defined(CONFIG_TQM885D)
276 	adjust_sdram_tbs_8xx,
277 #endif
278 	init_timebase,
279 #endif
280 #ifdef CONFIG_SYS_ALLOC_DPRAM
281 #if !defined(CONFIG_CPM2)
282 	dpram_init,
283 #endif
284 #endif
285 #if defined(CONFIG_BOARD_POSTCLK_INIT)
286 	board_postclk_init,
287 #endif
288 	env_init,
289 #if defined(CONFIG_8xx_CPUCLK_DEFAULT)
290 	get_clocks_866,		/* get CPU and bus clocks according to the environment variable */
291 	sdram_adjust_866,	/* adjust sdram refresh rate according to the new clock */
292 	init_timebase,
293 #endif
294 	init_baudrate,
295 	serial_init,
296 	console_init_f,
297 	display_options,
298 #if defined(CONFIG_8260)
299 	prt_8260_rsr,
300 	prt_8260_clks,
301 #endif /* CONFIG_8260 */
302 #if defined(CONFIG_MPC83xx)
303 	prt_83xx_rsr,
304 #endif
305 	checkcpu,
306 #if defined(CONFIG_MPC5xxx)
307 	prt_mpc5xxx_clks,
308 #endif /* CONFIG_MPC5xxx */
309 #if defined(CONFIG_MPC8220)
310 	prt_mpc8220_clks,
311 #endif
312 	checkboard,
313 	INIT_FUNC_WATCHDOG_INIT
314 #if defined(CONFIG_MISC_INIT_F)
315 	misc_init_f,
316 #endif
317 	INIT_FUNC_WATCHDOG_RESET
318 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
319 	init_func_i2c,
320 #endif
321 #if defined(CONFIG_HARD_SPI)
322 	init_func_spi,
323 #endif
324 #ifdef CONFIG_POST
325 	post_init_f,
326 #endif
327 	INIT_FUNC_WATCHDOG_RESET
328 	init_func_ram,
329 #if defined(CONFIG_SYS_DRAM_TEST)
330 	testdram,
331 #endif /* CONFIG_SYS_DRAM_TEST */
332 	INIT_FUNC_WATCHDOG_RESET
333 
334 	NULL,			/* Terminate this list */
335 };
336 
get_effective_memsize(void)337 ulong get_effective_memsize(void)
338 {
339 #ifndef	CONFIG_VERY_BIG_RAM
340 	return gd->ram_size;
341 #else
342 	/* limit stack to what we can reasonable map */
343 	return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ?
344 		 CONFIG_MAX_MEM_MAPPED : gd->ram_size);
345 #endif
346 }
347 
348 /************************************************************************
349  *
350  * This is the first part of the initialization sequence that is
351  * implemented in C, but still running from ROM.
352  *
353  * The main purpose is to provide a (serial) console interface as
354  * soon as possible (so we can see any error messages), and to
355  * initialize the RAM so that we can relocate the monitor code to
356  * RAM.
357  *
358  * Be aware of the restrictions: global data is read-only, BSS is not
359  * initialized, and stack space is limited to a few kB.
360  *
361  ************************************************************************
362  */
363 
364 #ifdef CONFIG_LOGBUFFER
logbuffer_base(void)365 unsigned long logbuffer_base(void)
366 {
367 	return CONFIG_SYS_SDRAM_BASE + get_effective_memsize() - LOGBUFF_LEN;
368 }
369 #endif
370 
board_init_f(ulong bootflag)371 void board_init_f (ulong bootflag)
372 {
373 	bd_t *bd;
374 	ulong len, addr, addr_sp;
375 	ulong *s;
376 	gd_t *id;
377 	init_fnc_t **init_fnc_ptr;
378 #ifdef CONFIG_PRAM
379 	int i;
380 	ulong reg;
381 	uchar tmp[64];		/* long enough for environment variables */
382 #endif
383 
384 	/* Pointer is writable since we allocated a register for it */
385 	gd = (gd_t *) (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET);
386 	/* compiler optimization barrier needed for GCC >= 3.4 */
387 	__asm__ __volatile__("": : :"memory");
388 
389 #if !defined(CONFIG_CPM2) && !defined(CONFIG_MPC512X) && \
390     !defined(CONFIG_MPC83xx) && !defined(CONFIG_MPC85xx) && \
391     !defined(CONFIG_MPC86xx)
392 	/* Clear initial global data */
393 	memset ((void *) gd, 0, sizeof (gd_t));
394 #endif
395 
396 	for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
397 		if ((*init_fnc_ptr) () != 0) {
398 			hang ();
399 		}
400 	}
401 
402 	/*
403 	 * Now that we have DRAM mapped and working, we can
404 	 * relocate the code and continue running from DRAM.
405 	 *
406 	 * Reserve memory at end of RAM for (top down in that order):
407 	 *  - area that won't get touched by U-Boot and Linux (optional)
408 	 *  - kernel log buffer
409 	 *  - protected RAM
410 	 *  - LCD framebuffer
411 	 *  - monitor code
412 	 *  - board info struct
413 	 */
414 	len = (ulong)&_end - CONFIG_SYS_MONITOR_BASE;
415 
416 	/*
417 	 * Subtract specified amount of memory to hide so that it won't
418 	 * get "touched" at all by U-Boot. By fixing up gd->ram_size
419 	 * the Linux kernel should now get passed the now "corrected"
420 	 * memory size and won't touch it either. This should work
421 	 * for arch/ppc and arch/powerpc. Only Linux board ports in
422 	 * arch/powerpc with bootwrapper support, that recalculate the
423 	 * memory size from the SDRAM controller setup will have to
424 	 * get fixed.
425 	 */
426 	gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
427 
428 	addr = CONFIG_SYS_SDRAM_BASE + get_effective_memsize();
429 
430 #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
431 	/*
432 	 * We need to make sure the location we intend to put secondary core
433 	 * boot code is reserved and not used by any part of u-boot
434 	 */
435 	if (addr > determine_mp_bootpg()) {
436 		addr = determine_mp_bootpg();
437 		debug ("Reserving MP boot page to %08lx\n", addr);
438 	}
439 #endif
440 
441 #ifdef CONFIG_LOGBUFFER
442 #ifndef CONFIG_ALT_LB_ADDR
443 	/* reserve kernel log buffer */
444 	addr -= (LOGBUFF_RESERVE);
445 	debug ("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN, addr);
446 #endif
447 #endif
448 
449 #ifdef CONFIG_PRAM
450 	/*
451 	 * reserve protected RAM
452 	 */
453 	i = getenv_r ("pram", (char *)tmp, sizeof (tmp));
454 	reg = (i > 0) ? simple_strtoul ((const char *)tmp, NULL, 10) : CONFIG_PRAM;
455 	addr -= (reg << 10);		/* size is in kB */
456 	debug ("Reserving %ldk for protected RAM at %08lx\n", reg, addr);
457 #endif /* CONFIG_PRAM */
458 
459 	/* round down to next 4 kB limit */
460 	addr &= ~(4096 - 1);
461 	debug ("Top of RAM usable for U-Boot at: %08lx\n", addr);
462 
463 #ifdef CONFIG_LCD
464 	/* reserve memory for LCD display (always full pages) */
465 	addr = lcd_setmem (addr);
466 	gd->fb_base = addr;
467 #endif /* CONFIG_LCD */
468 
469 #if defined(CONFIG_VIDEO) && defined(CONFIG_8xx)
470 	/* reserve memory for video display (always full pages) */
471 	addr = video_setmem (addr);
472 	gd->fb_base = addr;
473 #endif /* CONFIG_VIDEO  */
474 
475 	/*
476 	 * reserve memory for U-Boot code, data & bss
477 	 * round down to next 4 kB limit
478 	 */
479 	addr -= len;
480 	addr &= ~(4096 - 1);
481 #ifdef CONFIG_E500
482 	/* round down to next 64 kB limit so that IVPR stays aligned */
483 	addr &= ~(65536 - 1);
484 #endif
485 
486 	debug ("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
487 
488 	/*
489 	 * reserve memory for malloc() arena
490 	 */
491 	addr_sp = addr - TOTAL_MALLOC_LEN;
492 	debug ("Reserving %dk for malloc() at: %08lx\n",
493 			TOTAL_MALLOC_LEN >> 10, addr_sp);
494 
495 	/*
496 	 * (permanently) allocate a Board Info struct
497 	 * and a permanent copy of the "global" data
498 	 */
499 	addr_sp -= sizeof (bd_t);
500 	bd = (bd_t *) addr_sp;
501 	gd->bd = bd;
502 	debug ("Reserving %zu Bytes for Board Info at: %08lx\n",
503 			sizeof (bd_t), addr_sp);
504 	addr_sp -= sizeof (gd_t);
505 	id = (gd_t *) addr_sp;
506 	debug ("Reserving %zu Bytes for Global Data at: %08lx\n",
507 			sizeof (gd_t), addr_sp);
508 
509 	/*
510 	 * Finally, we set up a new (bigger) stack.
511 	 *
512 	 * Leave some safety gap for SP, force alignment on 16 byte boundary
513 	 * Clear initial stack frame
514 	 */
515 	addr_sp -= 16;
516 	addr_sp &= ~0xF;
517 	s = (ulong *)addr_sp;
518 	*s-- = 0;
519 	*s-- = 0;
520 	addr_sp = (ulong)s;
521 	debug ("Stack Pointer at: %08lx\n", addr_sp);
522 
523 	/*
524 	 * Save local variables to board info struct
525 	 */
526 
527 	bd->bi_memstart  = CONFIG_SYS_SDRAM_BASE;	/* start of  DRAM memory	*/
528 	bd->bi_memsize   = gd->ram_size;	/* size  of  DRAM memory in bytes */
529 
530 #ifdef CONFIG_IP860
531 	bd->bi_sramstart = SRAM_BASE;	/* start of  SRAM memory	*/
532 	bd->bi_sramsize  = SRAM_SIZE;	/* size  of  SRAM memory	*/
533 #elif defined CONFIG_MPC8220
534 	bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;	/* start of  SRAM memory	*/
535 	bd->bi_sramsize  = CONFIG_SYS_SRAM_SIZE;	/* size  of  SRAM memory	*/
536 #else
537 	bd->bi_sramstart = 0;		/* FIXME */ /* start of  SRAM memory	*/
538 	bd->bi_sramsize  = 0;		/* FIXME */ /* size  of  SRAM memory	*/
539 #endif
540 
541 #if defined(CONFIG_8xx) || defined(CONFIG_8260) || defined(CONFIG_5xx) || \
542     defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
543 	bd->bi_immr_base = CONFIG_SYS_IMMR;	/* base  of IMMR register     */
544 #endif
545 #if defined(CONFIG_MPC5xxx)
546 	bd->bi_mbar_base = CONFIG_SYS_MBAR;	/* base of internal registers */
547 #endif
548 #if defined(CONFIG_MPC83xx)
549 	bd->bi_immrbar = CONFIG_SYS_IMMR;
550 #endif
551 #if defined(CONFIG_MPC8220)
552 	bd->bi_mbar_base = CONFIG_SYS_MBAR;	/* base of internal registers */
553 	bd->bi_inpfreq   = gd->inp_clk;
554 	bd->bi_pcifreq   = gd->pci_clk;
555 	bd->bi_vcofreq   = gd->vco_clk;
556 	bd->bi_pevfreq   = gd->pev_clk;
557 	bd->bi_flbfreq   = gd->flb_clk;
558 
559 	/* store bootparam to sram (backward compatible), here? */
560 	{
561 		u32 *sram = (u32 *)CONFIG_SYS_SRAM_BASE;
562 		*sram++ = gd->ram_size;
563 		*sram++ = gd->bus_clk;
564 		*sram++ = gd->inp_clk;
565 		*sram++ = gd->cpu_clk;
566 		*sram++ = gd->vco_clk;
567 		*sram++ = gd->flb_clk;
568 		*sram++ = 0xb8c3ba11;  /* boot signature */
569 	}
570 #endif
571 
572 	bd->bi_bootflags = bootflag;	/* boot / reboot flag (for LynxOS)    */
573 
574 	WATCHDOG_RESET ();
575 	bd->bi_intfreq = gd->cpu_clk;	/* Internal Freq, in Hz */
576 	bd->bi_busfreq = gd->bus_clk;	/* Bus Freq,      in Hz */
577 #if defined(CONFIG_CPM2)
578 	bd->bi_cpmfreq = gd->cpm_clk;
579 	bd->bi_brgfreq = gd->brg_clk;
580 	bd->bi_sccfreq = gd->scc_clk;
581 	bd->bi_vco     = gd->vco_out;
582 #endif /* CONFIG_CPM2 */
583 #if defined(CONFIG_MPC512X)
584 	bd->bi_ipsfreq = gd->ips_clk;
585 #endif /* CONFIG_MPC512X */
586 #if defined(CONFIG_MPC5xxx)
587 	bd->bi_ipbfreq = gd->ipb_clk;
588 	bd->bi_pcifreq = gd->pci_clk;
589 #endif /* CONFIG_MPC5xxx */
590 	bd->bi_baudrate = gd->baudrate;	/* Console Baudrate     */
591 
592 #ifdef CONFIG_SYS_EXTBDINFO
593 	strncpy ((char *)bd->bi_s_version, "1.2", sizeof (bd->bi_s_version));
594 	strncpy ((char *)bd->bi_r_version, U_BOOT_VERSION, sizeof (bd->bi_r_version));
595 
596 	bd->bi_procfreq = gd->cpu_clk;	/* Processor Speed, In Hz */
597 	bd->bi_plb_busfreq = gd->bus_clk;
598 #if defined(CONFIG_405GP) || defined(CONFIG_405EP) || \
599     defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
600     defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
601 	bd->bi_pci_busfreq = get_PCI_freq ();
602 	bd->bi_opbfreq = get_OPB_freq ();
603 #elif defined(CONFIG_XILINX_405)
604 	bd->bi_pci_busfreq = get_PCI_freq ();
605 #endif
606 #endif
607 
608 	debug ("New Stack Pointer is: %08lx\n", addr_sp);
609 
610 	WATCHDOG_RESET ();
611 
612 #ifdef CONFIG_POST
613 	post_bootmode_init();
614 	post_run (NULL, POST_ROM | post_bootmode_get(0));
615 #endif
616 
617 	WATCHDOG_RESET();
618 
619 	gd->relocaddr = addr; /* Record relocation address, useful for debug */
620 
621 	memcpy (id, (void *)gd, sizeof (gd_t));
622 
623 	relocate_code (addr_sp, id, addr);
624 
625 	/* NOTREACHED - relocate_code() does not return */
626 }
627 
628 /************************************************************************
629  *
630  * This is the next part if the initialization sequence: we are now
631  * running from RAM and have a "normal" C environment, i. e. global
632  * data can be written, BSS has been cleared, the stack size in not
633  * that critical any more, etc.
634  *
635  ************************************************************************
636  */
board_init_r(gd_t * id,ulong dest_addr)637 void board_init_r (gd_t *id, ulong dest_addr)
638 {
639 	char *s;
640 	bd_t *bd;
641 	ulong malloc_start;
642 
643 #ifndef CONFIG_SYS_NO_FLASH
644 	ulong flash_size;
645 #endif
646 
647 	gd = id;		/* initialize RAM version of global data */
648 	bd = gd->bd;
649 
650 	gd->flags |= GD_FLG_RELOC;	/* tell others: relocation done */
651 
652 	/* The Malloc area is immediately below the monitor copy in DRAM */
653 	malloc_start = dest_addr - TOTAL_MALLOC_LEN;
654 
655 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
656 	/*
657 	 * The gd->cpu pointer is set to an address in flash before relocation.
658 	 * We need to update it to point to the same CPU entry in RAM.
659 	 */
660 	gd->cpu += dest_addr - CONFIG_SYS_MONITOR_BASE;
661 #endif
662 
663 #ifdef CONFIG_SERIAL_MULTI
664 	serial_initialize();
665 #endif
666 
667 	debug ("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
668 
669 	WATCHDOG_RESET ();
670 
671 	/*
672 	 * Setup trap handlers
673 	 */
674 	trap_init (dest_addr);
675 
676 #ifdef CONFIG_ADDR_MAP
677 	init_addr_map();
678 #endif
679 
680 #if defined(CONFIG_BOARD_EARLY_INIT_R)
681 	board_early_init_r ();
682 #endif
683 
684 	monitor_flash_len = (ulong)&__init_end - dest_addr;
685 
686 	WATCHDOG_RESET ();
687 
688 #ifdef CONFIG_LOGBUFFER
689 	logbuff_init_ptrs ();
690 #endif
691 #ifdef CONFIG_POST
692 	post_output_backlog ();
693 #endif
694 
695 	WATCHDOG_RESET();
696 
697 #if defined(CONFIG_SYS_DELAYED_ICACHE)
698 	icache_enable ();	/* it's time to enable the instruction cache */
699 #endif
700 
701 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && defined(CONFIG_E500)
702 	unlock_ram_in_cache();	/* it's time to unlock D-cache in e500 */
703 #endif
704 
705 #if defined(CONFIG_BAB7xx) || defined(CONFIG_CPC45)
706 	/*
707 	 * Do PCI configuration on BAB7xx and CPC45 _before_ the flash
708 	 * gets initialised, because we need the ISA resp. PCI_to_LOCAL bus
709 	 * bridge there.
710 	 */
711 	pci_init ();
712 #endif
713 #if defined(CONFIG_BAB7xx)
714 	/*
715 	 * Initialise the ISA bridge
716 	 */
717 	initialise_w83c553f ();
718 #endif
719 
720 	asm ("sync ; isync");
721 
722 	mem_malloc_init (malloc_start, TOTAL_MALLOC_LEN);
723 
724 #ifdef CONFIG_SAM460EX
725     u16 fpga_val;
726 
727 	// Green, Yellow, Ambra, Red LEDs ON -------------------------
728 	fpga_val = in_be16((void *)CONFIG_SYS_FPGA_BASE + 0x2E);
729 	fpga_val |= 0x000f;
730 	out_be16((void *)CONFIG_SYS_FPGA_BASE + 0x2E, fpga_val);
731 
732 	int ii=0;
733     for (ii=0;ii<10;ii++) udelay(10000);
734 #endif
735 
736 #if !defined(CONFIG_SYS_NO_FLASH)
737 	puts ("FLASH: ");
738 
739 	if ((flash_size = flash_init ()) > 0) {
740 # ifdef CONFIG_SYS_FLASH_CHECKSUM
741 		print_size (flash_size, "");
742 		/*
743 		 * Compute and print flash CRC if flashchecksum is set to 'y'
744 		 *
745 		 * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
746 		 */
747 		s = getenv ("flashchecksum");
748 		if (s && (*s == 'y')) {
749 			printf ("  CRC: %08X",
750 				crc32 (0, (const unsigned char *) CONFIG_SYS_FLASH_BASE, flash_size)
751 			);
752 		}
753 		putc ('\n');
754 # else	/* !CONFIG_SYS_FLASH_CHECKSUM */
755 		print_size (flash_size, "\n");
756 # endif /* CONFIG_SYS_FLASH_CHECKSUM */
757 	} else {
758 		puts (failed);
759 		hang ();
760 	}
761 
762 	bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;	/* update start of FLASH memory    */
763 	bd->bi_flashsize = flash_size;	/* size of FLASH memory (final value) */
764 
765 #if defined(CONFIG_SYS_UPDATE_FLASH_SIZE)
766 	/* Make a update of the Memctrl. */
767 	update_flash_size (flash_size);
768 #endif
769 
770 
771 # if defined(CONFIG_PCU_E) || defined(CONFIG_OXC) || defined(CONFIG_RMU)
772 	/* flash mapped at end of memory map */
773 	bd->bi_flashoffset = TEXT_BASE + flash_size;
774 # elif CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
775 	bd->bi_flashoffset = monitor_flash_len;	/* reserved area for startup monitor  */
776 # else
777 	bd->bi_flashoffset = 0;
778 # endif
779 #else	/* CONFIG_SYS_NO_FLASH */
780 
781 	bd->bi_flashsize = 0;
782 	bd->bi_flashstart = 0;
783 	bd->bi_flashoffset = 0;
784 #endif /* !CONFIG_SYS_NO_FLASH */
785 
786 	WATCHDOG_RESET ();
787 
788 	/* initialize higher level parts of CPU like time base and timers */
789 	cpu_init_r ();
790 
791 	WATCHDOG_RESET ();
792 
793 #ifdef CONFIG_SPI
794 # if !defined(CONFIG_ENV_IS_IN_EEPROM)
795 	spi_init_f ();
796 # endif
797 	spi_init_r ();
798 #endif
799 
800 #if defined(CONFIG_CMD_NAND)
801 	WATCHDOG_RESET ();
802 	puts ("NAND:  ");
803 	nand_init();		/* go init the NAND */
804 #endif
805 
806 	/* relocate environment function pointers etc. */
807 	env_relocate ();
808 
809 	/*
810 	 * Fill in missing fields of bd_info.
811 	 * We do this here, where we have "normal" access to the
812 	 * environment; we used to do this still running from ROM,
813 	 * where had to use getenv_r(), which can be pretty slow when
814 	 * the environment is in EEPROM.
815 	 */
816 
817 #if defined(CONFIG_SYS_EXTBDINFO)
818 #if defined(CONFIG_405GP) || defined(CONFIG_405EP)
819 #if defined(CONFIG_I2CFAST)
820 	/*
821 	 * set bi_iic_fast for linux taking environment variable
822 	 * "i2cfast" into account
823 	 */
824 	{
825 		char *s = getenv ("i2cfast");
826 		if (s && ((*s == 'y') || (*s == 'Y'))) {
827 			bd->bi_iic_fast[0] = 1;
828 			bd->bi_iic_fast[1] = 1;
829 		} else {
830 			bd->bi_iic_fast[0] = 0;
831 			bd->bi_iic_fast[1] = 0;
832 		}
833 	}
834 #else
835 	bd->bi_iic_fast[0] = 0;
836 	bd->bi_iic_fast[1] = 0;
837 #endif	/* CONFIG_I2CFAST */
838 #endif	/* CONFIG_405GP, CONFIG_405EP */
839 #endif	/* CONFIG_SYS_EXTBDINFO */
840 
841 #if defined(CONFIG_SC3)
842 	sc3_read_eeprom();
843 #endif
844 
845 #if defined (CONFIG_ID_EEPROM) || defined (CONFIG_SYS_I2C_MAC_OFFSET)
846 	mac_read_from_eeprom();
847 #endif
848 
849 #ifdef	CONFIG_HERMES
850 	if ((gd->board_type >> 16) == 2)
851 		bd->bi_ethspeed = gd->board_type & 0xFFFF;
852 	else
853 		bd->bi_ethspeed = 0xFFFF;
854 #endif
855 
856 #ifdef CONFIG_CMD_NET
857 	/* kept around for legacy kernels only ... ignore the next section */
858 	eth_getenv_enetaddr("ethaddr", bd->bi_enetaddr);
859 #ifdef CONFIG_HAS_ETH1
860 	eth_getenv_enetaddr("eth1addr", bd->bi_enet1addr);
861 #endif
862 #ifdef CONFIG_HAS_ETH2
863 	eth_getenv_enetaddr("eth2addr", bd->bi_enet2addr);
864 #endif
865 #ifdef CONFIG_HAS_ETH3
866 	eth_getenv_enetaddr("eth3addr", bd->bi_enet3addr);
867 #endif
868 #ifdef CONFIG_HAS_ETH4
869 	eth_getenv_enetaddr("eth4addr", bd->bi_enet4addr);
870 #endif
871 #ifdef CONFIG_HAS_ETH5
872 	eth_getenv_enetaddr("eth5addr", bd->bi_enet5addr);
873 #endif
874 #endif /* CONFIG_CMD_NET */
875 
876 	/* IP Address */
877 	bd->bi_ip_addr = getenv_IPaddr ("ipaddr");
878 
879 	WATCHDOG_RESET ();
880 
881 #if defined(CONFIG_PCI) && !defined(CONFIG_BAB7xx) && !defined(CONFIG_CPC45)
882 	/*
883 	 * Do pci configuration
884 	 */
885 	pci_init ();
886 #endif
887 
888 /** leave this here (after malloc(), environment and PCI are working) **/
889 	/* Initialize stdio devices */
890 	stdio_init ();
891 
892 	/* Initialize the jump table for applications */
893 	jumptable_init ();
894 
895 #if defined(CONFIG_API)
896 	/* Initialize API */
897 	api_init ();
898 #endif
899 
900 	/* Initialize the console (after the relocation and devices init) */
901 	console_init_r ();
902 
903 #if defined(CONFIG_MISC_INIT_R)
904 	/* miscellaneous platform dependent initialisations */
905 	misc_init_r ();
906 #endif
907 
908 #ifdef	CONFIG_HERMES
909 	if (bd->bi_ethspeed != 0xFFFF)
910 		hermes_start_lxt980 ((int) bd->bi_ethspeed);
911 #endif
912 
913 #if defined(CONFIG_CMD_KGDB)
914 	WATCHDOG_RESET ();
915 	puts ("KGDB:  ");
916 	kgdb_init ();
917 #endif
918 
919 	debug ("U-Boot relocated to %08lx\n", dest_addr);
920 
921 	/*
922 	 * Enable Interrupts
923 	 */
924 	interrupt_init ();
925 
926 	/* Must happen after interrupts are initialized since
927 	 * an irq handler gets installed
928 	 */
929 #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
930 	serial_buffered_init();
931 #endif
932 
933 #if defined(CONFIG_STATUS_LED) && defined(STATUS_LED_BOOT)
934 	status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
935 #endif
936 
937 	udelay (20);
938 
939 	set_timer (0);
940 
941 	/* Initialize from environment */
942 	if ((s = getenv ("loadaddr")) != NULL) {
943 		load_addr = simple_strtoul (s, NULL, 16);
944 	}
945 #if defined(CONFIG_CMD_NET)
946 	if ((s = getenv ("bootfile")) != NULL) {
947 		copy_filename (BootFile, s, sizeof (BootFile));
948 	}
949 #endif
950 
951 	WATCHDOG_RESET ();
952 
953 #if defined(CONFIG_DTT)		/* Digital Thermometers and Thermostats */
954 	dtt_init ();
955 #endif
956 #if defined(CONFIG_CMD_SCSI)
957 	WATCHDOG_RESET ();
958 	puts ("SCSI:  ");
959 	scsi_init ();
960 #endif
961 
962 #ifdef CONFIG_GENERIC_MMC
963 	WATCHDOG_RESET ();
964 	puts ("MMC:  ");
965 	mmc_initialize (bd);
966 #endif
967 
968 #if defined(CONFIG_CMD_DOC)
969 	WATCHDOG_RESET ();
970 	puts ("DOC:   ");
971 	doc_init ();
972 #endif
973 
974 #ifdef CONFIG_BITBANGMII
975 	bb_miiphy_init();
976 #endif
977 #if defined(CONFIG_CMD_NET)
978 #if defined(CONFIG_NET_MULTI)
979 	WATCHDOG_RESET ();
980 	puts ("Net:   ");
981 #endif
982 	eth_initialize (bd);
983 #endif
984 
985 #if defined(CONFIG_CMD_NET) && defined(CONFIG_RESET_PHY_R)
986 	WATCHDOG_RESET ();
987 	debug ("Reset Ethernet PHY\n");
988 	reset_phy ();
989 #endif
990 
991 #ifdef CONFIG_POST
992 	post_run (NULL, POST_RAM | post_bootmode_get(0));
993 #endif
994 
995 #if defined(CONFIG_CMD_PCMCIA) \
996     && !defined(CONFIG_CMD_IDE)
997 	WATCHDOG_RESET ();
998 	puts ("PCMCIA:");
999 	pcmcia_init ();
1000 #endif
1001 
1002 #if defined(CONFIG_CMD_IDE)
1003 	WATCHDOG_RESET ();
1004 # ifdef	CONFIG_IDE_8xx_PCCARD
1005 	puts ("PCMCIA:");
1006 # else
1007 	puts ("IDE:   ");
1008 #endif
1009 #if defined(CONFIG_START_IDE)
1010 	if (board_start_ide())
1011 		ide_init ();
1012 #else
1013 	ide_init ();
1014 #endif
1015 #endif
1016 
1017 #ifdef CONFIG_LAST_STAGE_INIT
1018 	WATCHDOG_RESET ();
1019 	/*
1020 	 * Some parts can be only initialized if all others (like
1021 	 * Interrupts) are up and running (i.e. the PC-style ISA
1022 	 * keyboard).
1023 	 */
1024 	last_stage_init ();
1025 #endif
1026 
1027 #if defined(CONFIG_CMD_BEDBUG)
1028 	WATCHDOG_RESET ();
1029 	bedbug_init ();
1030 #endif
1031 
1032 #if defined(CONFIG_PRAM) || defined(CONFIG_LOGBUFFER)
1033 	/*
1034 	 * Export available size of memory for Linux,
1035 	 * taking into account the protected RAM at top of memory
1036 	 */
1037 	{
1038 		ulong pram;
1039 		uchar memsz[32];
1040 #ifdef CONFIG_PRAM
1041 		char *s;
1042 
1043 		if ((s = getenv ("pram")) != NULL) {
1044 			pram = simple_strtoul (s, NULL, 10);
1045 		} else {
1046 			pram = CONFIG_PRAM;
1047 		}
1048 #else
1049 		pram=0;
1050 #endif
1051 #ifdef CONFIG_LOGBUFFER
1052 #ifndef CONFIG_ALT_LB_ADDR
1053 		/* Also take the logbuffer into account (pram is in kB) */
1054 		pram += (LOGBUFF_LEN+LOGBUFF_OVERHEAD)/1024;
1055 #endif
1056 #endif
1057 		sprintf ((char *)memsz, "%ldk", (bd->bi_memsize / 1024) - pram);
1058 		setenv ("mem", (char *)memsz);
1059 	}
1060 #endif
1061 
1062 #ifdef CONFIG_PS2KBD
1063 	puts ("PS/2:  ");
1064 	kbd_init();
1065 #endif
1066 
1067 #ifdef CONFIG_MODEM_SUPPORT
1068  {
1069 	 extern int do_mdm_init;
1070 	 do_mdm_init = gd->do_mdm_init;
1071  }
1072 #endif
1073 
1074 	/* Initialization complete - start the monitor */
1075 
1076 	/* main_loop() can return to retry autoboot, if so just run it again. */
1077 	for (;;) {
1078 		WATCHDOG_RESET ();
1079 		main_loop ();
1080 	}
1081 
1082 	/* NOTREACHED - no way out of command loop except booting */
1083 }
1084 
hang(void)1085 void hang (void)
1086 {
1087 	puts ("### ERROR ### Please RESET the board ###\n");
1088 	show_boot_progress(-30);
1089 	for (;;);
1090 }
1091 
1092 
1093 #if 0 /* We could use plain global data, but the resulting code is bigger */
1094 /*
1095  * Pointer to initial global data area
1096  *
1097  * Here we initialize it.
1098  */
1099 #undef	XTRN_DECLARE_GLOBAL_DATA_PTR
1100 #define XTRN_DECLARE_GLOBAL_DATA_PTR	/* empty = allocate here */
1101 DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET);
1102 #endif  /* 0 */
1103 
1104 /************************************************************************/
1105