3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 #include <asm/mipsregs.h>
27 #include <asm/addrspace.h>
32 #include <environment.h>
34 DECLARE_GLOBAL_DATA_PTR;
36 /*#define BOARD_DEBUG*/
38 #if (((CFG_ENV_ADDR + CFG_ENV_SIZE) < CFG_MONITOR_BASE) ||\
39 (CFG_ENV_ADDR >= (CFG_MONITOR_BASE + CFG_MONITOR_LEN))) ||\
40 defined(CFG_ENV_IS_IN_NVRAM)
41 #define TOTAL_MALLOC_LEN (CFG_MALLOC_LEN + CFG_ENV_SIZE)
43 #define TOTAL_MALLOC_LEN CFG_MALLOC_LEN
46 extern ulong uboot_end_data;
47 extern ulong uboot_end;
49 ulong monitor_flash_len;
51 const char version_string[] = U_BOOT_VERSION;
53 /* Begin and end of memory area for malloc(), and current "brk" */
54 static ulong mem_malloc_start;
55 static ulong mem_malloc_end;
56 static ulong mem_malloc_brk;
58 /* The Malloc area is immediately below the monitor copy in DRAM */
59 static void mem_malloc_init(void)
61 ulong dest_addr = CFG_MONITOR_BASE + gd->reloc_off;
63 mem_malloc_end = dest_addr;
64 mem_malloc_start = dest_addr - TOTAL_MALLOC_LEN;
65 mem_malloc_brk = mem_malloc_start;
67 memset((void *)mem_malloc_start,
68 0, mem_malloc_end - mem_malloc_start);
71 void *sbrk(ptrdiff_t increment)
73 ulong old = mem_malloc_brk;
74 ulong new = old + increment;
76 if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
77 printf_err("sbrk: out of memory "
78 "(%d requested > %d available)\n",
79 increment, mem_malloc_end - old);
81 return (void*)MORECORE_FAILURE;
89 static int display_banner(void)
93 #if defined(CONFIG_SKIP_LOWLEVEL_INIT)
94 puts("\n***************************************"
100 printf("\n***************************************"
103 MK_STR(CONFIG_BUILD_DATE_UTC) " *"
104 "\n***************************************\n\n",
110 static int baudrate_init(void)
114 if ((s = getenv("baudrate")) != NULL)
115 gd->baudrate = simple_strtoul(s, NULL, 10);
117 gd->baudrate = CONFIG_BAUDRATE;
122 #if !defined(COMPRESSED_UBOOT)
123 static int init_func_ram(void)
125 if ((gd->ram_size = dram_init()) > 0)
128 printf_err("on RAM initialization!\n");
134 * Breath some life into the board...
136 * The first part of initialization is running from Flash memory;
137 * its main purpose is to initialize the RAM so that we
138 * can relocate the monitor code to RAM.
142 * All attempts to come up with a "common" initialization sequence
143 * that works for all boards and architectures failed: some of the
144 * requirements are just _too_ different. To get rid of the resulting
145 * mess of board dependend #ifdef'ed code we now make the whole
146 * initialization sequence configurable to the user.
148 * The requirements for any new initalization function is simple: it
149 * receives a pointer to the "global data" structure as it's only
150 * argument, and returns an integer return code, where 0 means
151 * "continue" and != 0 means "fatal error, hang the system".
153 typedef int(init_fnc_t)(void);
155 #if !defined(COMPRESSED_UBOOT)
156 init_fnc_t *init_sequence[] = { timer_init,
165 init_fnc_t *init_sequence[] = { timer_init,
176 * BOARD INITIALIZATION
179 void board_init_f(ulong bootflag)
181 ulong len = (ulong)&uboot_end - CFG_MONITOR_BASE;
182 init_fnc_t **init_fnc_ptr;
183 ulong addr, addr_sp, *s;
187 /* Pointer is writable since we allocated a register for it */
190 /* Compiler optimization barrier needed for GCC >= 3.4 */
191 __asm__ __volatile__("": : :"memory");
193 memset((void *)gd, 0, sizeof(gd_t));
195 /* Loop trough init_sequence */
196 for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
197 if ((*init_fnc_ptr)() != 0)
201 #if defined(COMPRESSED_UBOOT)
202 gd->ram_size = bootflag;
206 * Now that we have DRAM mapped and working, we can
207 * relocate the code and continue running from DRAM
209 addr = CFG_SDRAM_BASE + gd->ram_size;
212 * We can reserve some RAM "on top" here,
213 * round down to next 4 kB limit
217 #if defined(BOARD_DEBUG)
218 printf("Top of RAM usable for U-Boot at: %08lX\n", addr);
222 * Reserve memory for U-Boot code, data & bss,
223 * round down to next 16 kB limit
226 addr &= ~(16 * 1024 - 1);
228 #if defined(BOARD_DEBUG)
229 printf("Reserving %ldk for U-Boot at: %08lX\n", len >> 10, addr);
232 /* Reserve memory for malloc() arena */
233 addr_sp = addr - TOTAL_MALLOC_LEN;
235 #if defined(BOARD_DEBUG)
236 printf("Reserving %dk for malloc() at: %08lX\n",
237 TOTAL_MALLOC_LEN >> 10, addr_sp);
241 * (permanently) allocate a Board Info struct
242 * and a permanent copy of the "global" data
244 addr_sp -= sizeof(bd_t);
245 bd = (bd_t *)addr_sp;
248 #if defined(BOARD_DEBUG)
249 printf("Reserving %d Bytes for Board Info at: %08lX\n",
250 sizeof(bd_t), addr_sp);
253 addr_sp -= sizeof(gd_t);
254 id = (gd_t *)addr_sp;
256 #if defined(BOARD_DEBUG)
257 printf("Reserving %d Bytes for Global Data at: %08lX\n",
258 sizeof(gd_t), addr_sp);
261 /* Reserve memory for boot params */
262 addr_sp -= CFG_BOOTPARAMS_LEN;
263 bd->bi_boot_params = addr_sp;
265 #if defined(BOARD_DEBUG)
266 printf("Reserving %dk for boot params() at: %08lX\n",
267 CFG_BOOTPARAMS_LEN >> 10, addr_sp);
271 * Finally, we set up a new (bigger) stack
273 * Leave some safety gap for SP, force alignment on 16 byte boundary,
274 * clear initial stack frame
278 s = (ulong *)addr_sp;
283 #if defined(BOARD_DEBUG)
284 printf("Stack Pointer at: %08lX\n", addr_sp);
288 * Save local variables to board info struct:
289 * - start of DRAM memory
290 * - size of DRAM memory in bytes
293 bd->bi_memstart = CFG_SDRAM_BASE;
294 bd->bi_memsize = gd->ram_size;
295 bd->bi_baudrate = gd->baudrate;
297 memcpy(id, (void *)gd, sizeof(gd_t));
299 relocate_code(addr_sp, id, addr);
300 /* NOTREACHED - relocate_code() does not return */
303 /************************************************************************
305 * This is the next part if the initialization sequence: we are now
306 * running from RAM and have a "normal" C environment, i. e. global
307 * data can be written, BSS has been cleared, the stack size in not
308 * that critical any more, etc.
310 ************************************************************************
312 void board_init_r(gd_t *id, ulong dest_addr)
314 extern void malloc_bin_reloc(void);
315 #if !defined(CFG_ENV_IS_NOWHERE)
316 extern char *env_name_spec;
326 /* Tell others: relocation done */
327 gd->flags |= GD_FLG_RELOC;
329 /* bd -> board data */
332 #if defined(BOARD_DEBUG)
333 printf("Now running in RAM - U-Boot at: %08lX\n", dest_addr);
336 /* We need (half of the) main CPU clock for udelay */
337 bd->bi_cfg_hz = (u32)(main_cpu_clk() >> 1);
339 gd->reloc_off = dest_addr - CFG_MONITOR_BASE;
341 monitor_flash_len = (ulong)&uboot_end_data - dest_addr;
343 /* We have to relocate the command table manually */
344 for (cmdtp = &__u_boot_cmd_start;
345 cmdtp != &__u_boot_cmd_end; cmdtp++) {
348 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
351 (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;
353 addr = (ulong)(cmdtp->name) + gd->reloc_off;
354 cmdtp->name = (char *)addr;
357 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
358 cmdtp->usage = (char *)addr;
361 #if defined(CFG_LONGHELP)
363 addr = (ulong)(cmdtp->help) + gd->reloc_off;
364 cmdtp->help = (char *)addr;
369 /* There are some other pointer constants we must deal with */
370 #if !defined(CFG_ENV_IS_NOWHERE)
371 env_name_spec += gd->reloc_off;
374 /* Configure available FLASH banks */
375 bd->bi_flashstart = CFG_FLASH_BASE;
376 bd->bi_flashsize = flash_init();
378 #if (CFG_MONITOR_BASE == CFG_FLASH_BASE)
379 /* Reserved area for U-Boot */
380 bd->bi_flashoffset = monitor_flash_len;
382 bd->bi_flashoffset = 0;
385 /* Initialize malloc() area */
389 /* Relocate environment function pointers etc. */
392 /* Local device IP address */
393 bd->bi_ip_addr = getenv_IPaddr("ipaddr");
395 #if defined(CONFIG_PCI)
396 /* Do pci configuration */
400 #if defined(CONFIG_USB)
404 /* Leave this here (after malloc(), environment and PCI are working) */
405 /* Initialize devices */
410 /* Initialize the console (after the relocation and devices init) */
413 /* Initialize from environment */
414 if ((s = getenv("loadaddr")) != NULL)
415 load_addr = simple_strtoul(s, NULL, 16);
417 #if defined(CONFIG_CMD_NET)
418 if ((s = getenv("bootfile")) != NULL)
419 copy_filename(BootFile, s, sizeof(BootFile));
422 /* Init MAC address in board data info */
423 if ((s = getenv("ethaddr")) != NULL && strlen(s) == 17) {
424 /* Use the one from env */
425 for (i = 0; i < 6; i++)
427 simple_strtoul((char *)(s + (i * 3)), NULL, 16);
429 /* Use the one provided by board func */
430 macaddr_init(bd->bi_enetaddr);
432 /* And revrite it to env variable */
433 for (i = 0; i < 6; i++)
434 sprintf((char *)(buf + (i * 3)), "%02X%c",
435 bd->bi_enetaddr[i], i < 5 ? ':' : '\0');
437 setenv("ethaddr", buf);
440 /* Print some information about board */
443 #if defined(CONFIG_CMD_NET)
445 eth_initialize(gd->bd);
449 /* main_loop() can return to retry autoboot, if so just run it again */
453 /* NOTREACHED - no way out of command loop except booting */
458 puts("## ERROR ##\n");