2 * (C) Copyright 2002-2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Copyright (C) 2011 Andes Technology Corporation
6 * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
7 * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
9 * SPDX-License-Identifier: GPL-2.0+
15 #include <stdio_dev.h>
16 #include <timestamp.h>
21 #include <onenand_uboot.h>
23 #include <asm/sections.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 ulong monitor_flash_len;
33 #if !defined(CONFIG_BAUDRATE)
34 #define CONFIG_BAUDRATE 38400
36 static int init_baudrate(void)
38 gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
43 * WARNING: this code looks "cleaner" than the PowerPC version, but
44 * has the disadvantage that you either get nothing, or everything.
45 * On PowerPC, you might see "DRAM: " before the system hangs - which
46 * gives a simple yet clear indication which part of the
47 * initialization if failing.
49 static int display_dram_config(void)
54 puts("RAM Configuration:\n");
56 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
57 printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
58 print_size(gd->bd->bi_dram[i].size, "\n");
63 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
64 size += gd->bd->bi_dram[i].size;
67 print_size(size, "\n");
73 #ifndef CONFIG_SYS_NO_FLASH
74 static void display_flash_config(ulong size)
77 print_size(size, "\n");
79 #endif /* CONFIG_SYS_NO_FLASH */
81 #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI)
83 static int nds32_pci_init(void)
88 #endif /* CONFIG_CMD_PCI || CONFIG_PCI */
90 #if defined(CONFIG_PMU) || defined(CONFIG_PCU)
91 #ifndef CONFIG_SKIP_LOWLEVEL_INIT
92 static int pmu_init(void)
94 #if defined(CONFIG_FTPMU010_POWER)
95 #ifdef __NDS32_N1213_43U1H__ /* AG101: internal definition in toolchain */
96 ftpmu010_sdram_clk_disable(CONFIG_SYS_FTPMU010_PDLLCR0_HCLKOUTDIS);
97 ftpmu010_mfpsr_select_dev(FTPMU010_MFPSR_AC97CLKSEL);
98 ftpmu010_sdramhtc_set(CONFIG_SYS_FTPMU010_SDRAMHTC);
99 #endif /* __NDS32_N1213_43U1H__ */
107 * Breathe some life into the board...
109 * Initialize a serial port as console, and carry out some hardware
112 * The first part of initialization is running from Flash memory;
113 * its main purpose is to initialize the RAM so that we
114 * can relocate the monitor code to RAM.
118 * All attempts to come up with a "common" initialization sequence
119 * that works for all boards and architectures failed: some of the
120 * requirements are just _too_ different. To get rid of the resulting
121 * mess of board dependent #ifdef'ed code we now make the whole
122 * initialization sequence configurable to the user.
124 * The requirements for any new initalization function is simple: it
125 * receives a pointer to the "global data" structure as it's only
126 * argument, and returns an integer return code, where 0 means
127 * "continue" and != 0 means "fatal error, hang the system".
129 typedef int (init_fnc_t)(void);
131 void __dram_init_banksize(void)
133 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
134 gd->bd->bi_dram[0].size = gd->ram_size;
136 void dram_init_banksize(void)
137 __attribute__((weak, alias("__dram_init_banksize")));
139 init_fnc_t *init_sequence[] = {
140 #if defined(CONFIG_ARCH_CPU_INIT)
141 arch_cpu_init, /* basic arch cpu dependent setup */
143 #if defined(CONFIG_PMU) || defined(CONFIG_PCU)
144 #ifndef CONFIG_SKIP_LOWLEVEL_INIT
148 board_init, /* basic board dependent setup */
149 #if defined(CONFIG_USE_IRQ)
150 interrupt_init, /* set up exceptions */
152 timer_init, /* initialize timer */
153 env_init, /* initialize environment */
154 init_baudrate, /* initialze baudrate settings */
155 serial_init, /* serial communications setup */
156 console_init_f, /* stage 1 init of console */
157 #if defined(CONFIG_DISPLAY_BOARDINFO)
158 checkboard, /* display board info */
160 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
163 dram_init, /* configure available RAM banks */
168 void board_init_f(ulong bootflag)
171 init_fnc_t **init_fnc_ptr;
175 /* Pointer is writable since we allocated a register for it */
176 gd = (gd_t *) ((CONFIG_SYS_INIT_SP_ADDR) & ~0x07);
178 memset((void *)gd, 0, GENERATED_GBL_DATA_SIZE);
180 gd->mon_len = (unsigned int)(&__bss_end) - (unsigned int)(&_start);
182 for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
183 if ((*init_fnc_ptr)() != 0)
187 debug("monitor len: %08lX\n", gd->mon_len);
189 * Ram is setup, size stored in gd !!
191 debug("ramsize: %08lX\n", gd->ram_size);
193 addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
195 /* round down to next 4 kB limit */
197 debug("Top of RAM usable for U-Boot at: %08lx\n", addr);
200 #ifdef CONFIG_FB_ADDR
201 gd->fb_base = CONFIG_FB_ADDR;
203 /* reserve memory for LCD display (always full pages) */
204 addr = lcd_setmem(addr);
206 #endif /* CONFIG_FB_ADDR */
207 #endif /* CONFIG_LCD */
210 * reserve memory for U-Boot code, data & bss
211 * round down to next 4 kB limit
216 debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10, addr);
219 * reserve memory for malloc() arena
221 addr_sp = addr - TOTAL_MALLOC_LEN;
222 debug("Reserving %dk for malloc() at: %08lx\n",
223 TOTAL_MALLOC_LEN >> 10, addr_sp);
225 * (permanently) allocate a Board Info struct
226 * and a permanent copy of the "global" data
228 addr_sp -= GENERATED_BD_INFO_SIZE;
229 bd = (bd_t *) addr_sp;
231 memset((void *)bd, 0, GENERATED_BD_INFO_SIZE);
232 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
233 GENERATED_BD_INFO_SIZE, addr_sp);
235 addr_sp -= GENERATED_GBL_DATA_SIZE;
236 id = (gd_t *) addr_sp;
237 debug("Reserving %zu Bytes for Global Data at: %08lx\n",
238 GENERATED_GBL_DATA_SIZE, addr_sp);
240 /* setup stackpointer for exeptions */
241 gd->irq_sp = addr_sp;
242 #ifdef CONFIG_USE_IRQ
243 addr_sp -= (CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ);
244 debug("Reserving %zu Bytes for IRQ stack at: %08lx\n",
245 CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ, addr_sp);
247 /* leave 3 words for abort-stack */
250 /* 8-byte alignment for ABI compliance */
252 debug("New Stack Pointer is: %08lx\n", addr_sp);
254 gd->bd->bi_baudrate = gd->baudrate;
255 /* Ram isn't board specific, so move it to board code ... */
256 dram_init_banksize();
257 display_dram_config(); /* and display it */
259 gd->relocaddr = addr;
260 gd->start_addr_sp = addr_sp;
262 gd->reloc_off = addr - _TEXT_BASE;
264 debug("relocation Offset is: %08lx\n", gd->reloc_off);
265 memcpy(id, (void *)gd, GENERATED_GBL_DATA_SIZE);
267 relocate_code(addr_sp, id, addr);
269 /* NOTREACHED - relocate_code() does not return */
273 * This is the next part if the initialization sequence: we are now
274 * running from RAM and have a "normal" C environment, i. e. global
275 * data can be written, BSS has been cleared, the stack size in not
276 * that critical any more, etc.
278 void board_init_r(gd_t *id, ulong dest_addr)
286 gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
288 monitor_flash_len = (ulong)&_end - (ulong)&_start;
289 debug("monitor flash len: %08lX\n", monitor_flash_len);
291 board_init(); /* Setup chipselects */
293 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
295 * We have to relocate the command table manually
297 fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd),
298 ll_entry_count(cmd_tbl_t, cmd));
299 #endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
303 debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
305 /* The Malloc area is immediately below the monitor copy in DRAM */
306 malloc_start = dest_addr - TOTAL_MALLOC_LEN;
307 mem_malloc_init(malloc_start, TOTAL_MALLOC_LEN);
309 #ifndef CONFIG_SYS_NO_FLASH
310 /* configure available FLASH banks */
311 gd->bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
312 gd->bd->bi_flashsize = flash_init();
313 gd->bd->bi_flashoffset = CONFIG_SYS_FLASH_BASE + gd->bd->bi_flashsize;
315 if (gd->bd->bi_flashsize)
316 display_flash_config(gd->bd->bi_flashsize);
317 #endif /* CONFIG_SYS_NO_FLASH */
319 #if defined(CONFIG_CMD_NAND)
321 nand_init(); /* go init the NAND */
324 #if defined(CONFIG_CMD_IDE)
329 #ifdef CONFIG_GENERIC_MMC
331 mmc_initialize(gd->bd);
334 /* initialize environment */
337 #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI)
342 stdio_init(); /* get the devices list going. */
346 #if defined(CONFIG_API)
351 console_init_r(); /* fully init console as a device */
353 #if defined(CONFIG_ARCH_MISC_INIT)
354 /* miscellaneous arch dependent initialisations */
357 #if defined(CONFIG_MISC_INIT_R)
358 /* miscellaneous platform dependent initialisations */
362 #if defined(CONFIG_USE_IRQ)
363 /* set up exceptions */
365 /* enable exceptions */
369 /* Initialize from environment */
370 load_addr = getenv_ulong("loadaddr", 16, load_addr);
372 #ifdef CONFIG_BOARD_LATE_INIT
376 #if defined(CONFIG_CMD_NET)
379 eth_initialize(gd->bd);
380 #if defined(CONFIG_RESET_PHY_R)
381 debug("Reset Ethernet PHY\n");
386 /* main_loop() can return to retry autoboot, if so just run it again. */
390 /* NOTREACHED - no way out of command loop except booting */