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 #ifdef CONFIG_SYS_ALLOC_DPRAM
39 #if defined(CONFIG_HARD_I2C) || \
40 defined(CONFIG_SYS_I2C)
48 #ifdef CONFIG_BITBANGMII
54 DECLARE_GLOBAL_DATA_PTR;
56 static char *failed = "*** failed ***\n";
58 #include <environment.h>
60 extern ulong __init_end;
61 extern ulong __bss_end;
63 #if defined(CONFIG_WATCHDOG)
64 # undef INIT_FUNC_WATCHDOG_INIT
65 # define INIT_FUNC_WATCHDOG_INIT watchdog_init,
66 # define WATCHDOG_DISABLE watchdog_disable
68 extern int watchdog_init(void);
69 extern int watchdog_disable(void);
71 # define INIT_FUNC_WATCHDOG_INIT /* undef */
72 # define WATCHDOG_DISABLE /* undef */
73 #endif /* CONFIG_WATCHDOG */
75 ulong monitor_flash_len;
77 /************************************************************************
79 ************************************************************************
83 * All attempts to come up with a "common" initialization sequence
84 * that works for all boards and architectures failed: some of the
85 * requirements are just _too_ different. To get rid of the resulting
86 * mess of board dependend #ifdef'ed code we now make the whole
87 * initialization sequence configurable to the user.
89 * The requirements for any new initalization function is simple: it
90 * receives a pointer to the "global data" structure as it's only
91 * argument, and returns an integer return code, where 0 means
92 * "continue" and != 0 means "fatal error, hang the system".
94 typedef int (init_fnc_t) (void);
96 /************************************************************************
98 ************************************************************************
99 * Some of this code should be moved into the core functions,
100 * but let's get it working (again) first...
103 static int init_baudrate (void)
105 gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
109 /***********************************************************************/
111 static int init_func_ram (void)
113 int board_type = 0; /* use dummy arg */
116 if ((gd->ram_size = initdram (board_type)) > 0) {
117 print_size (gd->ram_size, "\n");
124 /***********************************************************************/
126 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
127 static int init_func_i2c (void)
130 #ifdef CONFIG_SYS_I2C
133 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
140 #if defined(CONFIG_HARD_SPI)
141 static int init_func_spi (void)
150 /***********************************************************************/
152 /************************************************************************
153 * Initialization sequence *
154 ************************************************************************
157 init_fnc_t *init_sequence[] = {
166 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
169 #if defined(CONFIG_HARD_SPI)
173 #if defined(CONFIG_SYS_DRAM_TEST)
175 #endif /* CONFIG_SYS_DRAM_TEST */
176 INIT_FUNC_WATCHDOG_INIT
177 NULL, /* Terminate this list */
181 /************************************************************************
183 * This is the first part of the initialization sequence that is
184 * implemented in C, but still running from ROM.
186 * The main purpose is to provide a (serial) console interface as
187 * soon as possible (so we can see any error messages), and to
188 * initialize the RAM so that we can relocate the monitor code to
191 * Be aware of the restrictions: global data is read-only, BSS is not
192 * initialized, and stack space is limited to a few kB.
194 ************************************************************************
198 board_init_f (ulong bootflag)
201 ulong len, addr, addr_sp;
204 init_fnc_t **init_fnc_ptr;
209 /* Pointer is writable since we allocated a register for it */
210 gd = (gd_t *) (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET);
211 /* compiler optimization barrier needed for GCC >= 3.4 */
212 __asm__ __volatile__("": : :"memory");
214 /* Clear initial global data */
215 memset ((void *) gd, 0, sizeof (gd_t));
217 for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
218 if ((*init_fnc_ptr)() != 0) {
224 * Now that we have DRAM mapped and working, we can
225 * relocate the code and continue running from DRAM.
227 * Reserve memory at end of RAM for (top down in that order):
231 * - board info struct
233 len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
235 addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
237 #ifdef CONFIG_LOGBUFFER
238 /* reserve kernel log buffer */
239 addr -= (LOGBUFF_RESERVE);
240 debug ("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN, addr);
245 * reserve protected RAM
247 reg = getenv_ulong("pram", 10, CONFIG_PRAM);
248 addr -= (reg << 10); /* size is in kB */
249 debug ("Reserving %ldk for protected RAM at %08lx\n", reg, addr);
250 #endif /* CONFIG_PRAM */
252 /* round down to next 4 kB limit */
254 debug ("Top of RAM usable for U-Boot at: %08lx\n", addr);
257 #ifdef CONFIG_FB_ADDR
258 gd->fb_base = CONFIG_FB_ADDR;
260 /* reserve memory for LCD display (always full pages) */
261 addr = lcd_setmem (addr);
263 #endif /* CONFIG_FB_ADDR */
264 #endif /* CONFIG_LCD */
267 * reserve memory for U-Boot code, data & bss
268 * round down to next 4 kB limit
273 debug ("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
276 * reserve memory for malloc() arena
278 addr_sp = addr - TOTAL_MALLOC_LEN;
279 debug ("Reserving %dk for malloc() at: %08lx\n",
280 TOTAL_MALLOC_LEN >> 10, addr_sp);
283 * (permanently) allocate a Board Info struct
284 * and a permanent copy of the "global" data
286 addr_sp -= sizeof (bd_t);
287 bd = (bd_t *) addr_sp;
289 debug ("Reserving %zu Bytes for Board Info at: %08lx\n",
290 sizeof (bd_t), addr_sp);
291 addr_sp -= sizeof (gd_t);
292 id = (gd_t *) addr_sp;
293 debug ("Reserving %zu Bytes for Global Data at: %08lx\n",
294 sizeof (gd_t), addr_sp);
296 /* Reserve memory for boot params. */
297 addr_sp -= CONFIG_SYS_BOOTPARAMS_LEN;
298 bd->bi_boot_params = addr_sp;
299 debug ("Reserving %dk for boot parameters at: %08lx\n",
300 CONFIG_SYS_BOOTPARAMS_LEN >> 10, addr_sp);
303 * Finally, we set up a new (bigger) stack.
305 * Leave some safety gap for SP, force alignment on 16 byte boundary
306 * Clear initial stack frame
311 paddr = (ulong *)addr_sp;
314 addr_sp = (ulong)paddr;
316 debug ("Stack Pointer at: %08lx\n", addr_sp);
319 * Save local variables to board info struct
321 bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of DRAM memory */
322 bd->bi_memsize = gd->ram_size; /* size of DRAM memory in bytes */
323 #ifdef CONFIG_SYS_INIT_RAM_ADDR
324 bd->bi_sramstart = CONFIG_SYS_INIT_RAM_ADDR; /* start of SRAM memory */
325 bd->bi_sramsize = CONFIG_SYS_INIT_RAM_SIZE; /* size of SRAM memory */
327 bd->bi_mbar_base = CONFIG_SYS_MBAR; /* base of internal registers */
329 bd->bi_bootflags = bootflag; /* boot / reboot flag (for LynxOS) */
332 bd->bi_intfreq = gd->cpu_clk; /* Internal Freq, in Hz */
333 bd->bi_busfreq = gd->bus_clk; /* Bus Freq, in Hz */
335 bd->bi_pcifreq = gd->pci_clk; /* PCI Freq in Hz */
337 #ifdef CONFIG_EXTRA_CLOCK
338 bd->bi_inpfreq = gd->arch.inp_clk; /* input Freq in Hz */
339 bd->bi_vcofreq = gd->arch.vco_clk; /* vco Freq in Hz */
340 bd->bi_flbfreq = gd->arch.flb_clk; /* flexbus Freq in Hz */
343 #ifdef CONFIG_SYS_EXTBDINFO
344 strncpy (bd->bi_s_version, "1.2", sizeof (bd->bi_s_version));
345 strncpy (bd->bi_r_version, U_BOOT_VERSION, sizeof (bd->bi_r_version));
351 post_bootmode_init();
352 post_run (NULL, POST_ROM | post_bootmode_get(0));
357 memcpy (id, (void *)gd, sizeof (gd_t));
359 debug ("Start relocate of code from %08x to %08lx\n", CONFIG_SYS_MONITOR_BASE, addr);
360 relocate_code (addr_sp, id, addr);
362 /* NOTREACHED - jump_to_ram() does not return */
365 /************************************************************************
367 * This is the next part if the initialization sequence: we are now
368 * running from RAM and have a "normal" C environment, i. e. global
369 * data can be written, BSS has been cleared, the stack size in not
370 * that critical any more, etc.
372 ************************************************************************
374 void board_init_r (gd_t *id, ulong dest_addr)
376 char *s __maybe_unused;
379 #ifndef CONFIG_ENV_IS_NOWHERE
380 extern char * env_name_spec;
382 #ifndef CONFIG_SYS_NO_FLASH
385 gd = id; /* initialize RAM version of global data */
388 gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
392 gd->reloc_off = dest_addr - CONFIG_SYS_MONITOR_BASE;
396 debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
398 monitor_flash_len = (ulong)&__init_end - dest_addr;
400 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
402 * We have to relocate the command table manually
404 fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd),
405 ll_entry_count(cmd_tbl_t, cmd));
406 #endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
408 /* there are some other pointer constants we must deal with */
409 #ifndef CONFIG_ENV_IS_NOWHERE
410 env_name_spec += gd->reloc_off;
415 #ifdef CONFIG_LOGBUFFER
416 logbuff_init_ptrs ();
419 post_output_backlog ();
425 /* instruction cache enabled in cpu_init_f() for faster relocation */
426 icache_enable (); /* it's time to enable the instruction cache */
430 * Setup trap handlers
432 trap_init (CONFIG_SYS_SDRAM_BASE);
434 /* The Malloc area is immediately below the monitor copy in DRAM */
435 mem_malloc_init (CONFIG_SYS_MONITOR_BASE + gd->reloc_off -
436 TOTAL_MALLOC_LEN, TOTAL_MALLOC_LEN);
438 #if !defined(CONFIG_SYS_NO_FLASH)
441 if ((flash_size = flash_init ()) > 0) {
442 # ifdef CONFIG_SYS_FLASH_CHECKSUM
443 print_size (flash_size, "");
445 * Compute and print flash CRC if flashchecksum is set to 'y'
447 * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
449 if (getenv_yesno("flashchecksum") == 1) {
450 printf (" CRC: %08X",
452 (const unsigned char *) CONFIG_SYS_FLASH_BASE,
457 # else /* !CONFIG_SYS_FLASH_CHECKSUM */
458 print_size (flash_size, "\n");
459 # endif /* CONFIG_SYS_FLASH_CHECKSUM */
465 bd->bi_flashstart = CONFIG_SYS_FLASH_BASE; /* update start of FLASH memory */
466 bd->bi_flashsize = flash_size; /* size of FLASH memory (final value) */
467 bd->bi_flashoffset = 0;
468 #else /* CONFIG_SYS_NO_FLASH */
469 bd->bi_flashsize = 0;
470 bd->bi_flashstart = 0;
471 bd->bi_flashoffset = 0;
472 #endif /* !CONFIG_SYS_NO_FLASH */
476 /* initialize higher level parts of CPU like time base and timers */
482 # if !defined(CONFIG_ENV_IS_IN_EEPROM)
488 #if defined(CONFIG_SYS_I2C)
489 /* Adjust I2C subsystem pointers after relocation */
493 /* relocate environment function pointers etc. */
498 #if defined(CONFIG_PCI)
500 * Do pci configuration
505 /** leave this here (after malloc(), environment and PCI are working) **/
506 /* Initialize stdio devices */
509 /* Initialize the jump table for applications */
512 /* Initialize the console (after the relocation and devices init) */
515 #if defined(CONFIG_MISC_INIT_R)
516 /* miscellaneous platform dependent initialisations */
520 #if defined(CONFIG_CMD_KGDB)
526 debug ("U-Boot relocated to %08lx\n", dest_addr);
533 /* Must happen after interrupts are initialized since
534 * an irq handler gets installed
538 #ifdef CONFIG_STATUS_LED
539 status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
544 /* Insert function pointers now that we have relocated the code */
546 /* Initialize from environment */
547 load_addr = getenv_ulong("loadaddr", 16, load_addr);
551 #if defined(CONFIG_CMD_DOC)
557 #if defined(CONFIG_CMD_NAND)
560 nand_init(); /* go init the NAND */
563 #ifdef CONFIG_BITBANGMII
566 #if defined(CONFIG_CMD_NET)
568 #if defined(FEC_ENET)
576 post_run (NULL, POST_RAM | post_bootmode_get(0));
579 #if defined(CONFIG_CMD_PCMCIA) \
580 && !defined(CONFIG_CMD_IDE)
586 #if defined(CONFIG_CMD_IDE)
592 #ifdef CONFIG_LAST_STAGE_INIT
595 * Some parts can be only initialized if all others (like
596 * Interrupts) are up and running (i.e. the PC-style ISA
602 #if defined(CONFIG_PRAM) || defined(CONFIG_LOGBUFFER)
604 * Export available size of memory for Linux,
605 * taking into account the protected RAM at top of memory
612 pram = getenv_ulong("pram", 10, CONFIG_PRAM);
614 #ifdef CONFIG_LOGBUFFER
615 /* Also take the logbuffer into account (pram is in kB) */
616 pram += (LOGBUFF_LEN+LOGBUFF_OVERHEAD)/1024;
618 sprintf (memsz, "%ldk", (bd->bi_memsize / 1024) - pram);
619 setenv ("mem", memsz);
623 #ifdef CONFIG_WATCHDOG
624 /* disable watchdog if environment is set */
625 if ((s = getenv ("watchdog")) != NULL) {
626 if (strncmp (s, "off", 3) == 0) {
630 #endif /* CONFIG_WATCHDOG*/
633 /* Initialization complete - start the monitor */
635 /* main_loop() can return to retry autoboot, if so just run it again. */
641 /* NOTREACHED - no way out of command loop except booting */