3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
12 #include <stdio_dev.h>
15 #include <environment.h>
17 #include <onenand_uboot.h>
20 #ifdef CONFIG_BITBANGMII
24 DECLARE_GLOBAL_DATA_PTR;
26 ulong monitor_flash_len;
28 static char *failed = "*** failed ***\n";
30 int __board_early_init_f(void)
33 * Nothing to do in this dummy implementation
37 int board_early_init_f(void)
38 __attribute__((weak, alias("__board_early_init_f")));
40 static int init_func_ram(void)
42 #ifdef CONFIG_BOARD_TYPES
43 int board_type = gd->board_type;
45 int board_type = 0; /* use dummy arg */
49 gd->ram_size = initdram(board_type);
50 if (gd->ram_size > 0) {
51 print_size(gd->ram_size, "\n");
58 static int display_banner(void)
61 printf("\n\n%s\n\n", version_string);
65 #ifndef CONFIG_SYS_NO_FLASH
66 static void display_flash_config(ulong size)
69 print_size(size, "\n");
73 static int init_baudrate(void)
75 gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
81 * Breath some life into the board...
83 * The first part of initialization is running from Flash memory;
84 * its main purpose is to initialize the RAM so that we
85 * can relocate the monitor code to RAM.
89 * All attempts to come up with a "common" initialization sequence
90 * that works for all boards and architectures failed: some of the
91 * requirements are just _too_ different. To get rid of the resulting
92 * mess of board dependend #ifdef'ed code we now make the whole
93 * initialization sequence configurable to the user.
95 * The requirements for any new initalization function is simple: it
96 * receives a pointer to the "global data" structure as it's only
97 * argument, and returns an integer return code, where 0 means
98 * "continue" and != 0 means "fatal error, hang the system".
100 typedef int (init_fnc_t)(void);
102 init_fnc_t *init_sequence[] = {
105 env_init, /* initialize environment */
106 init_baudrate, /* initialize baudrate settings */
107 serial_init, /* serial communications setup */
109 display_banner, /* say that we are here */
116 void board_init_f(ulong bootflag)
120 init_fnc_t **init_fnc_ptr;
121 ulong addr, addr_sp, len;
124 /* Pointer is writable since we allocated a register for it.
127 /* compiler optimization barrier needed for GCC >= 3.4 */
128 __asm__ __volatile__("" : : : "memory");
130 memset((void *)gd, 0, sizeof(gd_t));
132 for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
133 if ((*init_fnc_ptr)() != 0)
138 * Now that we have DRAM mapped and working, we can
139 * relocate the code and continue running from DRAM.
141 addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
143 /* We can reserve some RAM "on top" here.
146 /* round down to next 4 kB limit.
149 debug("Top of RAM usable for U-Boot at: %08lx\n", addr);
151 /* Reserve memory for U-Boot code, data & bss
152 * round down to next 16 kB limit
154 len = bss_end() - CONFIG_SYS_MONITOR_BASE;
156 addr &= ~(16 * 1024 - 1);
158 debug("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
160 /* Reserve memory for malloc() arena.
162 addr_sp = addr - TOTAL_MALLOC_LEN;
163 debug("Reserving %dk for malloc() at: %08lx\n",
164 TOTAL_MALLOC_LEN >> 10, addr_sp);
167 * (permanently) allocate a Board Info struct
168 * and a permanent copy of the "global" data
170 addr_sp -= sizeof(bd_t);
171 bd = (bd_t *)addr_sp;
173 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
174 sizeof(bd_t), addr_sp);
176 addr_sp -= sizeof(gd_t);
177 id = (gd_t *)addr_sp;
178 debug("Reserving %zu Bytes for Global Data at: %08lx\n",
179 sizeof(gd_t), addr_sp);
181 /* Reserve memory for boot params.
183 addr_sp -= CONFIG_SYS_BOOTPARAMS_LEN;
184 bd->bi_boot_params = addr_sp;
185 debug("Reserving %dk for boot params() at: %08lx\n",
186 CONFIG_SYS_BOOTPARAMS_LEN >> 10, addr_sp);
189 * Finally, we set up a new (bigger) stack.
191 * Leave some safety gap for SP, force alignment on 16 byte boundary
192 * Clear initial stack frame
196 s = (ulong *)addr_sp;
200 debug("Stack Pointer at: %08lx\n", addr_sp);
203 * Save local variables to board info struct
205 bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of DRAM */
206 bd->bi_memsize = gd->ram_size; /* size of DRAM in bytes */
207 bd->bi_baudrate = gd->baudrate; /* Console Baudrate */
209 memcpy(id, (void *)gd, sizeof(gd_t));
211 relocate_code(addr_sp, id, addr);
213 /* NOTREACHED - relocate_code() does not return */
217 * This is the next part if the initialization sequence: we are now
218 * running from RAM and have a "normal" C environment, i. e. global
219 * data can be written, BSS has been cleared, the stack size in not
220 * that critical any more, etc.
223 void board_init_r(gd_t *id, ulong dest_addr)
225 #ifndef CONFIG_SYS_NO_FLASH
231 gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
233 debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
235 gd->reloc_off = dest_addr - CONFIG_SYS_MONITOR_BASE;
237 monitor_flash_len = image_copy_end() - dest_addr;
243 /* The Malloc area is immediately below the monitor copy in DRAM */
244 mem_malloc_init(CONFIG_SYS_MONITOR_BASE + gd->reloc_off -
245 TOTAL_MALLOC_LEN, TOTAL_MALLOC_LEN);
247 #ifndef CONFIG_SYS_NO_FLASH
248 /* configure available FLASH banks */
250 display_flash_config(size);
251 bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
252 bd->bi_flashsize = size;
254 #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
255 bd->bi_flashoffset = monitor_flash_len; /* reserved area for U-Boot */
257 bd->bi_flashoffset = 0;
260 bd->bi_flashstart = 0;
261 bd->bi_flashsize = 0;
262 bd->bi_flashoffset = 0;
265 #ifdef CONFIG_CMD_NAND
267 nand_init(); /* go init the NAND */
270 #if defined(CONFIG_CMD_ONENAND)
274 /* relocate environment function pointers etc. */
277 #if defined(CONFIG_PCI)
279 * Do pci configuration
284 /** leave this here (after malloc(), environment and PCI are working) **/
285 /* Initialize stdio devices */
290 /* Initialize the console (after the relocation and devices init) */
292 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
294 /* Initialize from environment */
295 load_addr = getenv_ulong("loadaddr", 16, load_addr);
297 #ifdef CONFIG_CMD_SPI
299 spi_init(); /* go init the SPI */
303 #if defined(CONFIG_MISC_INIT_R)
304 /* miscellaneous platform dependent initialisations */
308 #ifdef CONFIG_BITBANGMII
311 #if defined(CONFIG_CMD_NET)
313 eth_initialize(gd->bd);
316 /* main_loop() can return to retry autoboot, if so just run it again. */
320 /* NOTREACHED - no way out of command loop except booting */