]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - lib_blackfin/board.c
[Blackfin][PATCH] code cleanup
[karo-tx-uboot.git] / lib_blackfin / board.c
1 /*
2  * U-boot - board.c First C file to be called contains init routines
3  *
4  * Copyright (c) 2005 blackfin.uclinux.org
5  *
6  * (C) Copyright 2000-2004
7  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8  *
9  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27
28 #include <common.h>
29 #include <command.h>
30 #include <malloc.h>
31 #include <devices.h>
32 #include <version.h>
33 #include <net.h>
34 #include <environment.h>
35 #include <i2c.h>
36 #include "blackfin_board.h"
37 #include <asm/cplb.h>
38 #include "../drivers/smc91111.h"
39
40 #if defined(CONFIG_BF537)&&defined(CONFIG_POST)
41 #include <post.h>
42 int post_flag;
43 #endif
44
45 #ifndef CFG_NO_FLASH
46 extern flash_info_t flash_info[];
47 #endif
48
49 static inline u_long get_vco(void)
50 {
51         u_long msel;
52         u_long vco;
53
54         msel = (*pPLL_CTL >> 9) & 0x3F;
55         if (0 == msel)
56                 msel = 64;
57
58         vco = CONFIG_CLKIN_HZ;
59         vco >>= (1 & *pPLL_CTL);        /* DF bit */
60         vco = msel * vco;
61         return vco;
62 }
63
64 /*Get the Core clock*/
65 u_long get_cclk(void)
66 {
67         u_long csel, ssel;
68         if (*pPLL_STAT & 0x1)
69                 return CONFIG_CLKIN_HZ;
70
71         ssel = *pPLL_DIV;
72         csel = ((ssel >> 4) & 0x03);
73         ssel &= 0xf;
74         if (ssel && ssel < (1 << csel)) /* SCLK > CCLK */
75                 return get_vco() / ssel;
76         return get_vco() >> csel;
77 }
78
79 /* Get the System clock */
80 u_long get_sclk(void)
81 {
82         u_long ssel;
83
84         if (*pPLL_STAT & 0x1)
85                 return CONFIG_CLKIN_HZ;
86
87         ssel = (*pPLL_DIV & 0xf);
88
89         return get_vco() / ssel;
90 }
91
92 static void mem_malloc_init(void)
93 {
94         mem_malloc_start = CFG_MALLOC_BASE;
95         mem_malloc_end = (CFG_MALLOC_BASE + CFG_MALLOC_LEN);
96         mem_malloc_brk = mem_malloc_start;
97         memset((void *)mem_malloc_start, 0, mem_malloc_end - mem_malloc_start);
98 }
99
100 void *sbrk(ptrdiff_t increment)
101 {
102         ulong old = mem_malloc_brk;
103         ulong new = old + increment;
104
105         if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
106                 return (NULL);
107         }
108         mem_malloc_brk = new;
109
110         return ((void *)old);
111 }
112
113 static int display_banner(void)
114 {
115         sprintf(version_string, VERSION_STRING_FORMAT, VERSION_STRING);
116         printf("%s\n", version_string);
117         return (0);
118 }
119
120 static void display_flash_config(ulong size)
121 {
122         puts("FLASH:  ");
123         print_size(size, "\n");
124         return;
125 }
126
127 static int init_baudrate(void)
128 {
129         DECLARE_GLOBAL_DATA_PTR;
130
131         char tmp[64];
132         int i = getenv_r("baudrate", tmp, sizeof(tmp));
133         gd->bd->bi_baudrate = gd->baudrate = (i > 0)
134             ? (int)simple_strtoul(tmp, NULL, 10)
135             : CONFIG_BAUDRATE;
136         return (0);
137 }
138
139 #ifdef DEBUG
140 static void display_global_data(void)
141 {
142         DECLARE_GLOBAL_DATA_PTR;
143         bd_t *bd;
144         bd = gd->bd;
145         printf("--flags:%x\n", gd->flags);
146         printf("--board_type:%x\n", gd->board_type);
147         printf("--baudrate:%x\n", gd->baudrate);
148         printf("--have_console:%x\n", gd->have_console);
149         printf("--ram_size:%x\n", gd->ram_size);
150         printf("--reloc_off:%x\n", gd->reloc_off);
151         printf("--env_addr:%x\n", gd->env_addr);
152         printf("--env_valid:%x\n", gd->env_valid);
153         printf("--bd:%x %x\n", gd->bd, bd);
154         printf("---bi_baudrate:%x\n", bd->bi_baudrate);
155         printf("---bi_ip_addr:%x\n", bd->bi_ip_addr);
156         printf("---bi_enetaddr:%x %x %x %x %x %x\n",
157                bd->bi_enetaddr[0],
158                bd->bi_enetaddr[1],
159                bd->bi_enetaddr[2],
160                bd->bi_enetaddr[3], bd->bi_enetaddr[4], bd->bi_enetaddr[5]);
161         printf("---bi_arch_number:%x\n", bd->bi_arch_number);
162         printf("---bi_boot_params:%x\n", bd->bi_boot_params);
163         printf("---bi_memstart:%x\n", bd->bi_memstart);
164         printf("---bi_memsize:%x\n", bd->bi_memsize);
165         printf("---bi_flashstart:%x\n", bd->bi_flashstart);
166         printf("---bi_flashsize:%x\n", bd->bi_flashsize);
167         printf("---bi_flashoffset:%x\n", bd->bi_flashoffset);
168         printf("--jt:%x *:%x\n", gd->jt, *(gd->jt));
169 }
170 #endif
171
172 /* we cover everything with 4 meg pages, and need an extra for L1 */
173 unsigned int icplb_table[page_descriptor_table_size][2];
174 unsigned int dcplb_table[page_descriptor_table_size][2];
175
176 void init_cplbtables(void)
177 {
178         int i, j;
179
180         j = 0;
181         icplb_table[j][0] = 0xFFA00000;
182         icplb_table[j][1] = L1_IMEMORY;
183         j++;
184
185         for (i = 0; i <= CONFIG_MEM_SIZE / 4; i++) {
186                 icplb_table[j][0] = (i * 4 * 1024 * 1024);
187                 if (i * 4 * 1024 * 1024 <= CFG_MONITOR_BASE
188                     && (i + 1) * 4 * 1024 * 1024 >= CFG_MONITOR_BASE) {
189                         icplb_table[j][1] = SDRAM_IKERNEL;
190                 } else {
191                         icplb_table[j][1] = SDRAM_IGENERIC;
192                 }
193                 j++;
194         }
195 #if defined(CONFIG_BF561)
196         /* Async Memory space */
197         for (i = 0; i < 3; i++) {
198                 icplb_table[j++][0] = 0x20000000 + i * 4 * 1024 * 1024;
199                 icplb_table[j++][1] = SDRAM_IGENERIC;
200         }
201 #else
202         icplb_table[j][0] = 0x20000000;
203         icplb_table[j][1] = SDRAM_IGENERIC;
204 #endif
205         j = 0;
206         dcplb_table[j][0] = 0xFF800000;
207         dcplb_table[j][1] = L1_DMEMORY;
208         j++;
209
210         for (i = 0; i < CONFIG_MEM_SIZE / 4; i++) {
211                 dcplb_table[j][0] = (i * 4 * 1024 * 1024);
212                 if (i * 4 * 1024 * 1024 <= CFG_MONITOR_BASE
213                     && (i + 1) * 4 * 1024 * 1024 >= CFG_MONITOR_BASE) {
214                         dcplb_table[j][1] = SDRAM_DKERNEL;
215                 } else {
216                         dcplb_table[j][1] = SDRAM_DGENERIC;
217                 }
218                 j++;
219         }
220
221 #if defined(CONFIG_BF561)
222         /* MAC space */
223         dcplb_table[j++][0] = CONFIG_ASYNC_EBIU_BASE;
224         dcplb_table[j++][1] = SDRAM_EBIU;
225
226         /* Flash space */
227         for (i = 0; i < 2; i++) {
228                 dcplb_table[j++][0] = 0x20000000 + i * 4 * 1024 * 1024;
229                 dcplb_table[j++][1] = SDRAM_EBIU;
230         }
231 #else
232         dcplb_table[j][0] = 0x20000000;
233         dcplb_table[j][1] = SDRAM_EBIU;
234 #endif
235 }
236
237 /*
238  * All attempts to come up with a "common" initialization sequence
239  * that works for all boards and architectures failed: some of the
240  * requirements are just _too_ different. To get rid of the resulting
241  * mess of board dependend #ifdef'ed code we now make the whole
242  * initialization sequence configurable to the user.
243  *
244  * The requirements for any new initalization function is simple: it
245  * receives a pointer to the "global data" structure as it's only
246  * argument, and returns an integer return code, where 0 means
247  * "continue" and != 0 means "fatal error, hang the system".
248  */
249
250 void board_init_f(ulong bootflag)
251 {
252         DECLARE_GLOBAL_DATA_PTR;
253         ulong addr;
254         bd_t *bd;
255         int i;
256
257         init_cplbtables();
258
259         gd = (gd_t *) (CFG_GBL_DATA_ADDR);
260         memset((void *)gd, 0, sizeof(gd_t));
261
262         /* Board data initialization */
263         addr = (CFG_GBL_DATA_ADDR + sizeof(gd_t));
264
265         /* Align to 4 byte boundary */
266         addr &= ~(4 - 1);
267         bd = (bd_t *) addr;
268         gd->bd = bd;
269         memset((void *)bd, 0, sizeof(bd_t));
270
271         /* Initialize */
272         init_IRQ();
273         env_init();             /* initialize environment */
274         init_baudrate();        /* initialze baudrate settings */
275         serial_init();          /* serial communications setup */
276         console_init_f();
277 #ifdef CONFIG_ICACHE_ON
278         icache_enable();
279 #endif
280 #ifdef CONFIG_DCACHE_ON
281         dcache_enable();
282 #endif
283         display_banner();       /* say that we are here */
284
285         for (i = 0; i < page_descriptor_table_size; i++) {
286                 debug
287                     ("data (%02i)= 0x%08x : 0x%08x    intr = 0x%08x : 0x%08x\n",
288                      i, dcplb_table[i][0], dcplb_table[i][1], icplb_table[i][0],
289                      icplb_table[i][1]);
290         }
291
292         checkboard();
293 #if defined(CONFIG_RTC_BF533) && (CONFIG_COMMANDS & CFG_CMD_DATE)
294         rtc_init();
295 #endif
296         timer_init();
297         printf("Clock: VCO: %lu MHz, Core: %lu MHz, System: %lu MHz\n",
298                get_vco() / 1000000, get_cclk() / 1000000, get_sclk() / 1000000);
299         printf("SDRAM: ");
300         print_size(initdram(0), "\n");
301 #if defined(CONFIG_BF537)&&defined(CONFIG_POST)
302         post_init_f();
303         post_bootmode_init();
304         post_run(NULL, POST_ROM | post_bootmode_get(0));
305 #endif
306         board_init_r((gd_t *) gd, 0x20000010);
307 }
308
309 #if defined(CONFIG_SOFT_I2C) || defined(CONFIG_HARD_I2C)
310 static int init_func_i2c(void)
311 {
312         puts("I2C:   ");
313         i2c_init(CFG_I2C_SPEED, CFG_I2C_SLAVE);
314         puts("ready\n");
315         return (0);
316 }
317 #endif
318
319 void board_init_r(gd_t * id, ulong dest_addr)
320 {
321         DECLARE_GLOBAL_DATA_PTR;
322         ulong size;
323         extern void malloc_bin_reloc(void);
324         char *s, *e;
325         bd_t *bd;
326         int i;
327         gd = id;
328         gd->flags |= GD_FLG_RELOC;      /* tell others: relocation done */
329         bd = gd->bd;
330
331 #if    defined(CONFIG_BF537) && defined(CONFIG_POST)
332         post_output_backlog();
333         post_reloc();
334 #endif
335
336 #if     (CONFIG_STAMP || CONFIG_BF537 || CONFIG_EZKIT561) && !defined(CFG_NO_FLASH)
337         /* There are some other pointer constants we must deal with */
338         /* configure available FLASH banks */
339         size = flash_init();
340         display_flash_config(size);
341         flash_protect(FLAG_PROTECT_SET, CFG_FLASH_BASE,
342                       CFG_FLASH_BASE + 0x1ffff, &flash_info[0]);
343         bd->bi_flashstart = CFG_FLASH_BASE;
344         bd->bi_flashsize = size;
345         bd->bi_flashoffset = 0;
346 #else
347         bd->bi_flashstart = 0;
348         bd->bi_flashsize = 0;
349         bd->bi_flashoffset = 0;
350 #endif
351         /* initialize malloc() area */
352         mem_malloc_init();
353         malloc_bin_reloc();
354
355 #ifdef CONFIG_SPI
356 # if ! defined(CFG_ENV_IS_IN_EEPROM)
357         spi_init_f();
358 # endif
359         spi_init_r();
360 #endif
361
362         /* relocate environment function pointers etc. */
363         env_relocate();
364
365         /* board MAC address */
366         s = getenv("ethaddr");
367         for (i = 0; i < 6; ++i) {
368                 bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
369                 if (s)
370                         s = (*e) ? e + 1 : e;
371         }
372
373         /* IP Address */
374         bd->bi_ip_addr = getenv_IPaddr("ipaddr");
375
376         /* Initialize devices */
377         devices_init();
378         jumptable_init();
379
380         /* Initialize the console (after the relocation and devices init) */
381         console_init_r();
382
383         /* Initialize from environment */
384         if ((s = getenv("loadaddr")) != NULL) {
385                 load_addr = simple_strtoul(s, NULL, 16);
386         }
387 #if (CONFIG_COMMANDS & CFG_CMD_NET)
388         if ((s = getenv("bootfile")) != NULL) {
389                 copy_filename(BootFile, s, sizeof(BootFile));
390         }
391 #endif
392
393 #if (CONFIG_COMMANDS & CFG_CMD_NAND)
394         puts("NAND:  ");
395         nand_init();            /* go init the NAND */
396 #endif
397
398 #if defined(CONFIG_MISC_INIT_R)
399         /* miscellaneous platform dependent initialisations */
400         misc_init_r();
401 #endif
402
403 #if ((BFIN_CPU == ADSP_BF537) || (BFIN_CPU == ADSP_BF536))
404         printf("Net:    ");
405         eth_initialize(bd);
406 #endif
407
408 #ifdef CONFIG_DRIVER_SMC91111
409 #ifdef SHARED_RESOURCES
410         /* Switch to Ethernet */
411         swap_to(ETHERNET);
412 #endif
413         if ((SMC_inw(BANK_SELECT) & UPPER_BYTE_MASK) != SMC_IDENT) {
414                 printf("ERROR: Can't find SMC91111 at address %x\n",
415                        SMC_BASE_ADDRESS);
416         } else {
417                 printf("Net:   SMC91111 at 0x%08X\n", SMC_BASE_ADDRESS);
418         }
419
420 #ifdef SHARED_RESOURCES
421         swap_to(FLASH);
422 #endif
423 #endif
424 #if defined(CONFIG_SOFT_I2C) || defined(CONFIG_HARD_I2C)
425         init_func_i2c();
426 #endif
427
428 #ifdef DEBUG
429         display_global_data();
430 #endif
431
432 #if defined(CONFIG_BF537) && defined(CONFIG_POST)
433         if (post_flag)
434                 post_run(NULL, POST_RAM | post_bootmode_get(0));
435 #endif
436
437         /* main_loop() can return to retry autoboot, if so just run it again. */
438         for (;;) {
439                 main_loop();
440         }
441 }
442
443 void hang(void)
444 {
445         puts("### ERROR ### Please RESET the board ###\n");
446         for (;;) ;
447 }