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>
33 #include "ar7240_soc.h"
34 #include "../board/ar7240/common/ar7240_flash.h"
36 DECLARE_GLOBAL_DATA_PTR;
40 #if ( ((CFG_ENV_ADDR+CFG_ENV_SIZE) < CFG_MONITOR_BASE) || (CFG_ENV_ADDR >= (CFG_MONITOR_BASE + CFG_MONITOR_LEN)) ) || 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 extern int timer_init(void);
51 extern void all_led_on(void);
52 extern void all_led_off(void);
53 extern const char* print_mem_type(void);
54 extern void ar7240_sys_frequency(u32 *cpu_freq, u32 *ddr_freq, u32 *ahb_freq);
56 ulong monitor_flash_len;
58 const char version_string[] = U_BOOT_VERSION" (" __DATE__ ")";
60 // Begin and End of memory area for malloc(), and current "brk"
61 static ulong mem_malloc_start;
62 static ulong mem_malloc_end;
63 static ulong mem_malloc_brk;
65 // The Malloc area is immediately below the monitor copy in DRAM
66 static void mem_malloc_init(void){
67 ulong dest_addr = CFG_MONITOR_BASE + gd->reloc_off;
69 mem_malloc_end = dest_addr;
70 mem_malloc_start = dest_addr - TOTAL_MALLOC_LEN;
71 mem_malloc_brk = mem_malloc_start;
73 memset((void *)mem_malloc_start, 0, mem_malloc_end - mem_malloc_start);
76 void *sbrk(ptrdiff_t increment){
77 ulong old = mem_malloc_brk;
78 ulong new = old + increment;
80 if((new < mem_malloc_start) || (new > mem_malloc_end)){
87 static int display_banner(void){
88 printf("\n\n*********************************************\n* %s *\n*********************************************\n\n", version_string);
92 static int init_baudrate(void){
93 gd->baudrate = CONFIG_BAUDRATE;
97 #ifndef COMPRESSED_UBOOT
98 static int init_func_ram(void){
101 if((gd->ram_size = initdram()) > 0){
102 print_size(gd->ram_size, print_mem_type());
107 puts("## Error on RAM initialization!\n");
113 * Breath some life into the board...
115 * The first part of initialization is running from Flash memory;
116 * its main purpose is to initialize the RAM so that we
117 * can relocate the monitor code to RAM.
121 * All attempts to come up with a "common" initialization sequence
122 * that works for all boards and architectures failed: some of the
123 * requirements are just _too_ different. To get rid of the resulting
124 * mess of board dependend #ifdef'ed code we now make the whole
125 * initialization sequence configurable to the user.
127 * The requirements for any new initalization function is simple: it
128 * receives a pointer to the "global data" structure as it's only
129 * argument, and returns an integer return code, where 0 means
130 * "continue" and != 0 means "fatal error, hang the system".
132 typedef int(init_fnc_t)(void);
134 #ifndef COMPRESSED_UBOOT
135 init_fnc_t *init_sequence[] = { timer_init,
136 env_init, /* initialize environment */
137 init_baudrate, /* initialze baudrate settings */
138 serial_init, /* serial communications setup */
140 display_banner, /* say that we are here */
145 init_fnc_t *init_sequence[] = { env_init, /* initialize environment */
146 init_baudrate, /* initialze baudrate settings */
147 console_init_f, /* initialize console */
148 display_banner, /* say that we are here -> print baner */
154 * BOARD INITIALIZATION
157 void board_init_f(ulong bootflag){
160 init_fnc_t **init_fnc_ptr;
161 ulong addr, addr_sp, len = (ulong)&uboot_end - CFG_MONITOR_BASE;
164 // Pointer is writable since we allocated a register for it.
167 /* compiler optimization barrier needed for GCC >= 3.4 */
168 __asm__ __volatile__("": : :"memory");
170 memset((void *)gd, 0, sizeof(gd_t));
172 // loop trough init_sequence
173 for(init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr){
174 if((*init_fnc_ptr)() != 0){
179 #ifdef COMPRESSED_UBOOT
180 // print BOARD_CUSTOM_STRING
181 puts(BOARD_CUSTOM_STRING"\n\n");
183 // count ram size and print it
184 gd->ram_size = bootflag;
186 print_size(gd->ram_size, print_mem_type());
191 * Now that we have DRAM mapped and working, we can
192 * relocate the code and continue running from DRAM.
194 addr = CFG_SDRAM_BASE + gd->ram_size;
197 * We can reserve some RAM "on top" here.
201 * round down to next 4 kB limit.
206 printf("Top of RAM usable for U-Boot at: %08lX\n", addr);
210 * Reserve memory for U-Boot code, data & bss
211 * round down to next 16 kB limit
214 addr &= ~(16 * 1024 - 1);
217 printf("Reserving %ldk for U-Boot at: %08lX\n", len >> 10, addr);
221 * Reserve memory for malloc() arena.
223 addr_sp = addr - TOTAL_MALLOC_LEN;
226 printf("Reserving %dk for malloc() at: %08lX\n", TOTAL_MALLOC_LEN >> 10, addr_sp);
230 * (permanently) allocate a Board Info struct
231 * and a permanent copy of the "global" data
233 addr_sp -= sizeof(bd_t);
234 bd = (bd_t *)addr_sp;
238 printf("Reserving %d Bytes for Board Info at: %08lX\n", sizeof(bd_t), addr_sp);
241 addr_sp -= sizeof(gd_t);
242 id = (gd_t *)addr_sp;
245 printf("Reserving %d Bytes for Global Data at: %08lX\n", sizeof(gd_t), addr_sp);
248 /* Reserve memory for boot params.
250 addr_sp -= CFG_BOOTPARAMS_LEN;
251 bd->bi_boot_params = addr_sp;
254 printf("Reserving %dk for boot params() at: %08lX\n", CFG_BOOTPARAMS_LEN >> 10, addr_sp);
258 * Finally, we set up a new (bigger) stack.
260 * Leave some safety gap for SP, force alignment on 16 byte boundary
261 * Clear initial stack frame
265 s = (ulong *)addr_sp;
271 printf("Stack Pointer at: %08lX\n", addr_sp);
275 * Save local variables to board info struct
277 bd->bi_memstart = CFG_SDRAM_BASE; /* start of DRAM memory */
278 bd->bi_memsize = gd->ram_size; /* size of DRAM memory in bytes */
279 bd->bi_baudrate = gd->baudrate; /* Console Baudrate */
281 memcpy(id, (void *)gd, sizeof(gd_t));
283 relocate_code(addr_sp, id, addr);
284 /* NOTREACHED - relocate_code() does not return */
287 /************************************************************************
289 * This is the next part if the initialization sequence: we are now
290 * running from RAM and have a "normal" C environment, i. e. global
291 * data can be written, BSS has been cleared, the stack size in not
292 * that critical any more, etc.
294 ************************************************************************
296 void board_init_r(gd_t *id, ulong dest_addr){
299 extern void malloc_bin_reloc(void);
300 #ifndef CFG_ENV_IS_NOWHERE
301 extern char * env_name_spec;
306 unsigned char buffer[6];
307 unsigned int ahb_freq, ddr_freq, cpu_freq, spi_freq;
310 gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
312 /* bd -> board data */
315 /* get CPU/RAM/AHB clocks */
316 ar7240_sys_frequency(&cpu_freq, &ddr_freq, &ahb_freq);
319 bd->bi_cfg_hz = (unsigned long)(cpu_freq >> 1);
322 printf("Now running in RAM - U-Boot at: %08lX\n", dest_addr);
325 gd->reloc_off = dest_addr - CFG_MONITOR_BASE;
327 monitor_flash_len = (ulong)&uboot_end_data - dest_addr;
330 * We have to relocate the command table manually
332 for(cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++){
335 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
337 cmdtp->cmd = (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;
339 addr = (ulong)(cmdtp->name) + gd->reloc_off;
340 cmdtp->name = (char *)addr;
343 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
344 cmdtp->usage = (char *)addr;
348 addr = (ulong)(cmdtp->help) + gd->reloc_off;
349 cmdtp->help = (char *)addr;
354 /* there are some other pointer constants we must deal with */
355 #ifndef CFG_ENV_IS_NOWHERE
356 env_name_spec += gd->reloc_off;
359 /* configure available FLASH banks */
362 bd->bi_flashstart = CFG_FLASH_BASE;
363 bd->bi_flashsize = size;
365 // calculate SPI clock (we need to set bit 0 to 1 in SPI_FUNC_SELECT to access SPI registers)
366 ar7240_reg_wr(AR7240_SPI_FS, 0x01);
367 spi_freq = ahb_freq / (((ar7240_reg_rd(AR7240_SPI_CLOCK) & 0x3F) + 1) * 2);
368 ar7240_reg_wr(AR7240_SPI_FS, 0x0);
376 printf("CLOCKS: %d/%d/%d/%d MHz (CPU/RAM/AHB/SPI)\n", cpu_freq, ddr_freq, ahb_freq, spi_freq);
379 #if CFG_MONITOR_BASE == CFG_FLASH_BASE
380 bd->bi_flashoffset = monitor_flash_len; /* reserved area for U-Boot */
382 bd->bi_flashoffset = 0;
385 /* initialize malloc() area */
389 /* relocate environment function pointers etc. */
392 /* board MAC address */
393 #if defined(OFFSET_MAC_ADDRESS)
394 memcpy(buffer, (void *)(CFG_FLASH_BASE + OFFSET_MAC_DATA_BLOCK + OFFSET_MAC_ADDRESS), 6);
397 // 00-03-7F (Atheros Communications, Inc.)
406 for(i = 0; i < 6; ++i){
407 bd->bi_enetaddr[i] = buffer[i];
411 bd->bi_ip_addr = getenv_IPaddr("ipaddr");
413 #if defined(CONFIG_PCI)
415 * Do pci configuration
420 /** leave this here (after malloc(), environment and PCI are working) **/
421 /* Initialize devices */
426 /* Initialize the console (after the relocation and devices init) */
428 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
430 /* Initialize from environment */
431 if((s = getenv("loadaddr")) != NULL){
432 load_addr = simple_strtoul(s, NULL, 16);
435 #if (CONFIG_COMMANDS & CFG_CMD_NET)
436 if((s = getenv("bootfile")) != NULL){
437 copy_filename(BootFile, s, sizeof(BootFile));
439 #endif /* CFG_CMD_NET */
441 /* blink all available LEDs */
442 printf("LED on during eth initialization...\n\n");
445 #if (CONFIG_COMMANDS & CFG_CMD_NET)
446 eth_initialize(gd->bd);
451 /* main_loop() can return to retry autoboot, if so just run it again. */
456 /* NOTREACHED - no way out of command loop except booting */
460 puts("## ERROR ##\n");