3 * Josef Baumgartner <josef.baumgartner@telex.de>
5 * (C) Copyright 2000-2002
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * SPDX-License-Identifier: GPL-2.0+
15 #include <stdio_dev.h>
16 #include <linux/compiler.h>
18 #include <asm/immap.h>
20 #if defined(CONFIG_CMD_IDE)
23 #if defined(CONFIG_CMD_SCSI)
26 #if defined(CONFIG_CMD_KGDB)
29 #ifdef CONFIG_STATUS_LED
30 #include <status_led.h>
34 #if defined(CONFIG_CMD_BEDBUG)
35 #include <cmd_bedbug.h>
37 #ifdef CONFIG_SYS_ALLOC_DPRAM
42 #if defined(CONFIG_HARD_I2C) || \
43 defined(CONFIG_SYS_I2C)
51 #ifdef CONFIG_BITBANGMII
57 DECLARE_GLOBAL_DATA_PTR;
59 static char *failed = "*** failed ***\n";
61 #include <environment.h>
63 extern ulong __init_end;
64 extern ulong __bss_end;
66 #if defined(CONFIG_WATCHDOG)
67 # undef INIT_FUNC_WATCHDOG_INIT
68 # define INIT_FUNC_WATCHDOG_INIT watchdog_init,
69 # define WATCHDOG_DISABLE watchdog_disable
71 extern int watchdog_init(void);
72 extern int watchdog_disable(void);
74 # define INIT_FUNC_WATCHDOG_INIT /* undef */
75 # define WATCHDOG_DISABLE /* undef */
76 #endif /* CONFIG_WATCHDOG */
78 ulong monitor_flash_len;
80 /************************************************************************
82 ************************************************************************
86 * All attempts to come up with a "common" initialization sequence
87 * that works for all boards and architectures failed: some of the
88 * requirements are just _too_ different. To get rid of the resulting
89 * mess of board dependend #ifdef'ed code we now make the whole
90 * initialization sequence configurable to the user.
92 * The requirements for any new initalization function is simple: it
93 * receives a pointer to the "global data" structure as it's only
94 * argument, and returns an integer return code, where 0 means
95 * "continue" and != 0 means "fatal error, hang the system".
97 typedef int (init_fnc_t) (void);
99 /************************************************************************
101 ************************************************************************
102 * Some of this code should be moved into the core functions,
103 * but let's get it working (again) first...
106 static int init_baudrate (void)
108 gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
112 /***********************************************************************/
114 static int init_func_ram (void)
116 int board_type = 0; /* use dummy arg */
119 if ((gd->ram_size = initdram (board_type)) > 0) {
120 print_size (gd->ram_size, "\n");
127 /***********************************************************************/
129 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
130 static int init_func_i2c (void)
133 #ifdef CONFIG_SYS_I2C
136 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
143 #if defined(CONFIG_HARD_SPI)
144 static int init_func_spi (void)
153 /***********************************************************************/
155 /************************************************************************
156 * Initialization sequence *
157 ************************************************************************
160 init_fnc_t *init_sequence[] = {
169 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
172 #if defined(CONFIG_HARD_SPI)
176 #if defined(CONFIG_SYS_DRAM_TEST)
178 #endif /* CONFIG_SYS_DRAM_TEST */
179 INIT_FUNC_WATCHDOG_INIT
180 NULL, /* Terminate this list */
184 /************************************************************************
186 * This is the first part of the initialization sequence that is
187 * implemented in C, but still running from ROM.
189 * The main purpose is to provide a (serial) console interface as
190 * soon as possible (so we can see any error messages), and to
191 * initialize the RAM so that we can relocate the monitor code to
194 * Be aware of the restrictions: global data is read-only, BSS is not
195 * initialized, and stack space is limited to a few kB.
197 ************************************************************************
201 board_init_f (ulong bootflag)
204 ulong len, addr, addr_sp;
207 init_fnc_t **init_fnc_ptr;
212 /* Pointer is writable since we allocated a register for it */
213 gd = (gd_t *) (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET);
214 /* compiler optimization barrier needed for GCC >= 3.4 */
215 __asm__ __volatile__("": : :"memory");
217 /* Clear initial global data */
218 memset ((void *) gd, 0, sizeof (gd_t));
220 for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
221 if ((*init_fnc_ptr)() != 0) {
227 * Now that we have DRAM mapped and working, we can
228 * relocate the code and continue running from DRAM.
230 * Reserve memory at end of RAM for (top down in that order):
234 * - board info struct
236 len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
238 addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
240 #ifdef CONFIG_LOGBUFFER
241 /* reserve kernel log buffer */
242 addr -= (LOGBUFF_RESERVE);
243 debug ("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN, addr);
248 * reserve protected RAM
250 reg = getenv_ulong("pram", 10, CONFIG_PRAM);
251 addr -= (reg << 10); /* size is in kB */
252 debug ("Reserving %ldk for protected RAM at %08lx\n", reg, addr);
253 #endif /* CONFIG_PRAM */
255 /* round down to next 4 kB limit */
257 debug ("Top of RAM usable for U-Boot at: %08lx\n", addr);
260 #ifdef CONFIG_FB_ADDR
261 gd->fb_base = CONFIG_FB_ADDR;
263 /* reserve memory for LCD display (always full pages) */
264 addr = lcd_setmem (addr);
266 #endif /* CONFIG_FB_ADDR */
267 #endif /* CONFIG_LCD */
270 * reserve memory for U-Boot code, data & bss
271 * round down to next 4 kB limit
276 debug ("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
279 * reserve memory for malloc() arena
281 addr_sp = addr - TOTAL_MALLOC_LEN;
282 debug ("Reserving %dk for malloc() at: %08lx\n",
283 TOTAL_MALLOC_LEN >> 10, addr_sp);
286 * (permanently) allocate a Board Info struct
287 * and a permanent copy of the "global" data
289 addr_sp -= sizeof (bd_t);
290 bd = (bd_t *) addr_sp;
292 debug ("Reserving %zu Bytes for Board Info at: %08lx\n",
293 sizeof (bd_t), addr_sp);
294 addr_sp -= sizeof (gd_t);
295 id = (gd_t *) addr_sp;
296 debug ("Reserving %zu Bytes for Global Data at: %08lx\n",
297 sizeof (gd_t), addr_sp);
299 /* Reserve memory for boot params. */
300 addr_sp -= CONFIG_SYS_BOOTPARAMS_LEN;
301 bd->bi_boot_params = addr_sp;
302 debug ("Reserving %dk for boot parameters at: %08lx\n",
303 CONFIG_SYS_BOOTPARAMS_LEN >> 10, addr_sp);
306 * Finally, we set up a new (bigger) stack.
308 * Leave some safety gap for SP, force alignment on 16 byte boundary
309 * Clear initial stack frame
314 paddr = (ulong *)addr_sp;
317 addr_sp = (ulong)paddr;
319 debug ("Stack Pointer at: %08lx\n", addr_sp);
322 * Save local variables to board info struct
324 bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of DRAM memory */
325 bd->bi_memsize = gd->ram_size; /* size of DRAM memory in bytes */
326 #ifdef CONFIG_SYS_INIT_RAM_ADDR
327 bd->bi_sramstart = CONFIG_SYS_INIT_RAM_ADDR; /* start of SRAM memory */
328 bd->bi_sramsize = CONFIG_SYS_INIT_RAM_SIZE; /* size of SRAM memory */
330 bd->bi_mbar_base = CONFIG_SYS_MBAR; /* base of internal registers */
332 bd->bi_bootflags = bootflag; /* boot / reboot flag (for LynxOS) */
335 bd->bi_intfreq = gd->cpu_clk; /* Internal Freq, in Hz */
336 bd->bi_busfreq = gd->bus_clk; /* Bus Freq, in Hz */
338 bd->bi_pcifreq = gd->pci_clk; /* PCI Freq in Hz */
340 #ifdef CONFIG_EXTRA_CLOCK
341 bd->bi_inpfreq = gd->arch.inp_clk; /* input Freq in Hz */
342 bd->bi_vcofreq = gd->arch.vco_clk; /* vco Freq in Hz */
343 bd->bi_flbfreq = gd->arch.flb_clk; /* flexbus Freq in Hz */
346 #ifdef CONFIG_SYS_EXTBDINFO
347 strncpy (bd->bi_s_version, "1.2", sizeof (bd->bi_s_version));
348 strncpy (bd->bi_r_version, U_BOOT_VERSION, sizeof (bd->bi_r_version));
354 post_bootmode_init();
355 post_run (NULL, POST_ROM | post_bootmode_get(0));
360 memcpy (id, (void *)gd, sizeof (gd_t));
362 debug ("Start relocate of code from %08x to %08lx\n", CONFIG_SYS_MONITOR_BASE, addr);
363 relocate_code (addr_sp, id, addr);
365 /* NOTREACHED - jump_to_ram() does not return */
368 /************************************************************************
370 * This is the next part if the initialization sequence: we are now
371 * running from RAM and have a "normal" C environment, i. e. global
372 * data can be written, BSS has been cleared, the stack size in not
373 * that critical any more, etc.
375 ************************************************************************
377 void board_init_r (gd_t *id, ulong dest_addr)
379 char *s __maybe_unused;
382 #ifndef CONFIG_ENV_IS_NOWHERE
383 extern char * env_name_spec;
385 #ifndef CONFIG_SYS_NO_FLASH
388 gd = id; /* initialize RAM version of global data */
391 gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
395 gd->reloc_off = dest_addr - CONFIG_SYS_MONITOR_BASE;
399 debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
401 monitor_flash_len = (ulong)&__init_end - dest_addr;
403 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
405 * We have to relocate the command table manually
407 fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd),
408 ll_entry_count(cmd_tbl_t, cmd));
409 #endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
411 /* there are some other pointer constants we must deal with */
412 #ifndef CONFIG_ENV_IS_NOWHERE
413 env_name_spec += gd->reloc_off;
418 #ifdef CONFIG_LOGBUFFER
419 logbuff_init_ptrs ();
422 post_output_backlog ();
428 /* instruction cache enabled in cpu_init_f() for faster relocation */
429 icache_enable (); /* it's time to enable the instruction cache */
433 * Setup trap handlers
435 trap_init (CONFIG_SYS_SDRAM_BASE);
437 /* The Malloc area is immediately below the monitor copy in DRAM */
438 mem_malloc_init (CONFIG_SYS_MONITOR_BASE + gd->reloc_off -
439 TOTAL_MALLOC_LEN, TOTAL_MALLOC_LEN);
441 #if !defined(CONFIG_SYS_NO_FLASH)
444 if ((flash_size = flash_init ()) > 0) {
445 # ifdef CONFIG_SYS_FLASH_CHECKSUM
446 print_size (flash_size, "");
448 * Compute and print flash CRC if flashchecksum is set to 'y'
450 * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
452 if (getenv_yesno("flashchecksum") == 1) {
453 printf (" CRC: %08X",
455 (const unsigned char *) CONFIG_SYS_FLASH_BASE,
460 # else /* !CONFIG_SYS_FLASH_CHECKSUM */
461 print_size (flash_size, "\n");
462 # endif /* CONFIG_SYS_FLASH_CHECKSUM */
468 bd->bi_flashstart = CONFIG_SYS_FLASH_BASE; /* update start of FLASH memory */
469 bd->bi_flashsize = flash_size; /* size of FLASH memory (final value) */
470 bd->bi_flashoffset = 0;
471 #else /* CONFIG_SYS_NO_FLASH */
472 bd->bi_flashsize = 0;
473 bd->bi_flashstart = 0;
474 bd->bi_flashoffset = 0;
475 #endif /* !CONFIG_SYS_NO_FLASH */
479 /* initialize higher level parts of CPU like time base and timers */
485 # if !defined(CONFIG_ENV_IS_IN_EEPROM)
491 #if defined(CONFIG_SYS_I2C)
492 /* Adjust I2C subsystem pointers after relocation */
496 /* relocate environment function pointers etc. */
501 #if defined(CONFIG_PCI)
503 * Do pci configuration
508 /** leave this here (after malloc(), environment and PCI are working) **/
509 /* Initialize stdio devices */
512 /* Initialize the jump table for applications */
515 /* Initialize the console (after the relocation and devices init) */
518 #if defined(CONFIG_MISC_INIT_R)
519 /* miscellaneous platform dependent initialisations */
523 #if defined(CONFIG_CMD_KGDB)
529 debug ("U-Boot relocated to %08lx\n", dest_addr);
536 /* Must happen after interrupts are initialized since
537 * an irq handler gets installed
541 #ifdef CONFIG_STATUS_LED
542 status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
547 /* Insert function pointers now that we have relocated the code */
549 /* Initialize from environment */
550 load_addr = getenv_ulong("loadaddr", 16, load_addr);
554 #if defined(CONFIG_CMD_DOC)
560 #if defined(CONFIG_CMD_NAND)
563 nand_init(); /* go init the NAND */
566 #ifdef CONFIG_BITBANGMII
569 #if defined(CONFIG_CMD_NET)
571 #if defined(FEC_ENET)
579 post_run (NULL, POST_RAM | post_bootmode_get(0));
582 #if defined(CONFIG_CMD_PCMCIA) \
583 && !defined(CONFIG_CMD_IDE)
589 #if defined(CONFIG_CMD_IDE)
595 #ifdef CONFIG_LAST_STAGE_INIT
598 * Some parts can be only initialized if all others (like
599 * Interrupts) are up and running (i.e. the PC-style ISA
605 #if defined(CONFIG_CMD_BEDBUG)
610 #if defined(CONFIG_PRAM) || defined(CONFIG_LOGBUFFER)
612 * Export available size of memory for Linux,
613 * taking into account the protected RAM at top of memory
620 pram = getenv_ulong("pram", 10, CONFIG_PRAM);
622 #ifdef CONFIG_LOGBUFFER
623 /* Also take the logbuffer into account (pram is in kB) */
624 pram += (LOGBUFF_LEN+LOGBUFF_OVERHEAD)/1024;
626 sprintf (memsz, "%ldk", (bd->bi_memsize / 1024) - pram);
627 setenv ("mem", memsz);
631 #ifdef CONFIG_MODEM_SUPPORT
633 extern int do_mdm_init;
634 do_mdm_init = gd->do_mdm_init;
638 #ifdef CONFIG_WATCHDOG
639 /* disable watchdog if environment is set */
640 if ((s = getenv ("watchdog")) != NULL) {
641 if (strncmp (s, "off", 3) == 0) {
645 #endif /* CONFIG_WATCHDOG*/
648 /* Initialization complete - start the monitor */
650 /* main_loop() can return to retry autoboot, if so just run it again. */
656 /* NOTREACHED - no way out of command loop except booting */