PLL/clocks settings in FLASH and O/C recovery for AR9331, other minor changes, AR9344...
[oweals/u-boot_mod.git] / u-boot / lib_mips / board.c
1 /*
2  * (C) Copyright 2003
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <command.h>
26 #include <asm/mipsregs.h>
27 #include <asm/addrspace.h>
28 #include <malloc.h>
29 #include <devices.h>
30 #include <version.h>
31 #include <net.h>
32 #include <environment.h>
33 #include "ar7240_soc.h"
34 #include "../board/ar7240/common/ar7240_flash.h"
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 //#define       BOARD_DEBUG
39
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)
42 #else
43 #define TOTAL_MALLOC_LEN        CFG_MALLOC_LEN
44 #endif
45
46 extern ulong uboot_end_data;
47 extern ulong uboot_end;
48
49 extern int timer_init(void);
50
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);
55
56 ulong monitor_flash_len;
57
58 const char version_string[] = U_BOOT_VERSION"  (" __DATE__ ")";
59
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;
64
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;
68
69         mem_malloc_end = dest_addr;
70         mem_malloc_start = dest_addr - TOTAL_MALLOC_LEN;
71         mem_malloc_brk = mem_malloc_start;
72
73         memset((void *)mem_malloc_start, 0, mem_malloc_end - mem_malloc_start);
74 }
75
76 void *sbrk(ptrdiff_t increment){
77         ulong old = mem_malloc_brk;
78         ulong new = old + increment;
79
80         if((new < mem_malloc_start) || (new > mem_malloc_end)){
81                 return(NULL);
82         }
83         mem_malloc_brk = new;
84         return((void *)old);
85 }
86
87 static int display_banner(void){
88         printf("\n\n*********************************************\n*        %s        *\n*********************************************\n\n", version_string);
89         return(0);
90 }
91
92 static int init_baudrate(void){
93         gd->baudrate = CONFIG_BAUDRATE;
94         return(0);
95 }
96
97 #ifndef COMPRESSED_UBOOT
98 static int init_func_ram(void){
99         puts("DRAM:   ");
100
101         if((gd->ram_size = initdram()) > 0){
102                 print_size(gd->ram_size, print_mem_type());
103                 puts("\n");
104                 return(0);
105         }
106
107         puts("## Error on RAM initialization!\n");
108         return(1);
109 }
110 #endif
111
112 /*
113  * Breath some life into the board...
114  *
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.
118  */
119
120 /*
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.
126  *
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".
131  */
132 typedef int(init_fnc_t)(void);
133
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 */
139                                 console_init_f,
140                                 display_banner, /* say that we are here */
141                                 checkboard,
142                                 init_func_ram,
143                                 NULL, };
144 #else
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 */
149                                                                 NULL, };
150 #endif
151
152 /*
153  *
154  * BOARD INITIALIZATION
155  *
156  */
157 void board_init_f(ulong bootflag){
158         gd_t gd_data, *id;
159         bd_t *bd;
160         init_fnc_t **init_fnc_ptr;
161         ulong addr, addr_sp, len = (ulong)&uboot_end - CFG_MONITOR_BASE;
162         ulong *s;
163
164         // Pointer is writable since we allocated a register for it.
165         gd = &gd_data;
166
167         /* compiler optimization barrier needed for GCC >= 3.4 */
168         __asm__ __volatile__("": : :"memory");
169
170         memset((void *)gd, 0, sizeof(gd_t));
171
172         // loop trough init_sequence
173         for(init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr){
174                 if((*init_fnc_ptr)() != 0){
175                         hang();
176                 }
177         }
178
179 #ifdef COMPRESSED_UBOOT
180         // print BOARD_CUSTOM_STRING
181         puts(BOARD_CUSTOM_STRING"\n\n");
182
183         // count ram size and print it
184         gd->ram_size = bootflag;
185         puts("DRAM:   ");
186         print_size(gd->ram_size, print_mem_type());
187         puts("\n");
188 #endif
189
190         /*
191          * Now that we have DRAM mapped and working, we can
192          * relocate the code and continue running from DRAM.
193          */
194         addr = CFG_SDRAM_BASE + gd->ram_size;
195
196         /*
197          * We can reserve some RAM "on top" here.
198          */
199
200         /*
201          * round down to next 4 kB limit.
202          */
203         addr &= ~(4096 - 1);
204
205 #ifdef BOARD_DEBUG
206         printf("Top of RAM usable for U-Boot at: %08lX\n", addr);
207 #endif
208
209         /*
210          * Reserve memory for U-Boot code, data & bss
211          * round down to next 16 kB limit
212          */
213         addr -= len;
214         addr &= ~(16 * 1024 - 1);
215
216 #ifdef BOARD_DEBUG
217         printf("Reserving %ldk for U-Boot at: %08lX\n", len >> 10, addr);
218 #endif
219
220         /*
221          * Reserve memory for malloc() arena.
222          */
223         addr_sp = addr - TOTAL_MALLOC_LEN;
224
225 #ifdef BOARD_DEBUG
226         printf("Reserving %dk for malloc() at: %08lX\n", TOTAL_MALLOC_LEN >> 10, addr_sp);
227 #endif
228
229         /*
230          * (permanently) allocate a Board Info struct
231          * and a permanent copy of the "global" data
232          */
233         addr_sp -= sizeof(bd_t);
234         bd = (bd_t *)addr_sp;
235         gd->bd = bd;
236
237 #ifdef BOARD_DEBUG
238         printf("Reserving %d Bytes for Board Info at: %08lX\n", sizeof(bd_t), addr_sp);
239 #endif
240
241         addr_sp -= sizeof(gd_t);
242         id = (gd_t *)addr_sp;
243
244 #ifdef BOARD_DEBUG
245         printf("Reserving %d Bytes for Global Data at: %08lX\n", sizeof(gd_t), addr_sp);
246 #endif
247
248         /* Reserve memory for boot params.
249          */
250         addr_sp -= CFG_BOOTPARAMS_LEN;
251         bd->bi_boot_params = addr_sp;
252
253 #ifdef BOARD_DEBUG
254         printf("Reserving %dk for boot params() at: %08lX\n", CFG_BOOTPARAMS_LEN >> 10, addr_sp);
255 #endif
256
257         /*
258          * Finally, we set up a new (bigger) stack.
259          *
260          * Leave some safety gap for SP, force alignment on 16 byte boundary
261          * Clear initial stack frame
262          */
263         addr_sp -= 16;
264         addr_sp &= ~0xF;
265         s = (ulong *)addr_sp;
266         *s-- = 0;
267         *s-- = 0;
268         addr_sp = (ulong)s;
269
270 #ifdef BOARD_DEBUG
271         printf("Stack Pointer at: %08lX\n", addr_sp);
272 #endif
273
274         /*
275          * Save local variables to board info struct
276          */
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 */
280
281         memcpy(id, (void *)gd, sizeof(gd_t));
282
283         relocate_code(addr_sp, id, addr);
284         /* NOTREACHED - relocate_code() does not return */
285 }
286
287 /************************************************************************
288  *
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.
293  *
294  ************************************************************************
295  */
296 void board_init_r(gd_t *id, ulong dest_addr){
297         cmd_tbl_t *cmdtp;
298         ulong size;
299         extern void malloc_bin_reloc(void);
300 #ifndef CFG_ENV_IS_NOWHERE
301         extern char * env_name_spec;
302 #endif
303         bd_t *bd;
304         int i;
305         char *s;
306         unsigned char buffer[6];
307         unsigned int ahb_freq, ddr_freq, cpu_freq, spi_freq;
308
309         gd = id;
310         gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
311
312         /* bd -> board data */
313         bd = gd->bd;
314
315         /* get CPU/RAM/AHB clocks */
316         ar7240_sys_frequency(&cpu_freq, &ddr_freq, &ahb_freq);
317
318         /* set bi_cfg_hz */
319         bd->bi_cfg_hz = (unsigned long)(cpu_freq >> 1);
320
321 #ifdef BOARD_DEBUG
322         printf("Now running in RAM - U-Boot at: %08lX\n", dest_addr);
323 #endif
324
325         gd->reloc_off = dest_addr - CFG_MONITOR_BASE;
326
327         monitor_flash_len = (ulong)&uboot_end_data - dest_addr;
328
329         /*
330          * We have to relocate the command table manually
331          */
332         for(cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++){
333                 ulong addr;
334
335                 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
336
337                 cmdtp->cmd = (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;
338
339                 addr = (ulong)(cmdtp->name) + gd->reloc_off;
340                 cmdtp->name = (char *)addr;
341
342                 if(cmdtp->usage){
343                         addr = (ulong)(cmdtp->usage) + gd->reloc_off;
344                         cmdtp->usage = (char *)addr;
345                 }
346 #ifdef  CFG_LONGHELP
347                 if(cmdtp->help){
348                         addr = (ulong)(cmdtp->help) + gd->reloc_off;
349                         cmdtp->help = (char *)addr;
350                 }
351 #endif
352         }
353
354         /* there are some other pointer constants we must deal with */
355 #ifndef CFG_ENV_IS_NOWHERE
356         env_name_spec += gd->reloc_off;
357 #endif
358
359         /* configure available FLASH banks */
360         size = flash_init();
361
362         bd->bi_flashstart = CFG_FLASH_BASE;
363         bd->bi_flashsize = size;
364
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);
369
370         // make MHz from Hz
371         cpu_freq /= 1000000;
372         ddr_freq /= 1000000;
373         ahb_freq /= 1000000;
374         spi_freq /= 1000000;
375
376         printf("CLOCKS: %d/%d/%d/%d MHz (CPU/RAM/AHB/SPI)\n", cpu_freq, ddr_freq, ahb_freq, spi_freq);
377         puts("\n");
378
379 #if CFG_MONITOR_BASE == CFG_FLASH_BASE
380         bd->bi_flashoffset = monitor_flash_len; /* reserved area for U-Boot */
381 #else
382         bd->bi_flashoffset = 0;
383 #endif
384
385         /* initialize malloc() area */
386         mem_malloc_init();
387         malloc_bin_reloc();
388
389         /* relocate environment function pointers etc. */
390         env_relocate();
391
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);
395 #else
396         // fake MAC
397         // 00-03-7F (Atheros Communications, Inc.)
398         buffer[0] = 0x00;
399         buffer[1] = 0x03;
400         buffer[2] = 0x7f;
401         buffer[3] = 0x09;
402         buffer[4] = 0x0b;
403         buffer[5] = 0xad;
404 #endif
405
406         for(i = 0; i < 6; ++i){
407                 bd->bi_enetaddr[i] = buffer[i];
408         }
409
410         /* IP Address */
411         bd->bi_ip_addr = getenv_IPaddr("ipaddr");
412
413 #if defined(CONFIG_PCI)
414         /*
415          * Do pci configuration
416          */
417         pci_init();
418 #endif
419
420         /** leave this here (after malloc(), environment and PCI are working) **/
421         /* Initialize devices */
422         devices_init();
423
424         jumptable_init();
425
426         /* Initialize the console (after the relocation and devices init) */
427         console_init_r();
428         /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
429
430         /* Initialize from environment */
431         if((s = getenv("loadaddr")) != NULL){
432                 load_addr = simple_strtoul(s, NULL, 16);
433         }
434
435 #if (CONFIG_COMMANDS & CFG_CMD_NET)
436         if((s = getenv("bootfile")) != NULL){
437                 copy_filename(BootFile, s, sizeof(BootFile));
438         }
439 #endif  /* CFG_CMD_NET */
440
441         /* blink all available LEDs */
442         printf("LED on during eth initialization...\n\n");
443         all_led_on();
444
445 #if (CONFIG_COMMANDS & CFG_CMD_NET)
446         eth_initialize(gd->bd);
447 #endif
448
449         all_led_off();
450
451         /* main_loop() can return to retry autoboot, if so just run it again. */
452         for(;;){
453                 main_loop();
454         }
455
456         /* NOTREACHED - no way out of command loop except booting */
457 }
458
459 void hang(void){
460         puts("## ERROR ##\n");
461         for(;;);
462 }