blob: 1538da3f29a6f6a0317e9ff7d75d23322baf0161 [file] [log] [blame]
Wolfgang Denk6cb142f2006-03-12 02:12:27 +01001/*
2 * U-boot - board.c First C file to be called contains init routines
3 *
Aubrey Li155fd762007-04-05 18:31:18 +08004 * Copyright (c) 2005-2007 Analog Devices Inc.
Wolfgang Denk6cb142f2006-03-12 02:12:27 +01005 *
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
Aubrey Li155fd762007-04-05 18:31:18 +080024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
25 * MA 02110-1301 USA
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010026 */
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>
Aubrey.Li3f0606a2007-03-09 13:38:44 +080035#include <i2c.h>
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010036#include "blackfin_board.h"
Aubrey.Li3f0606a2007-03-09 13:38:44 +080037#include <asm/cplb.h>
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010038#include "../drivers/smc91111.h"
39
Aubrey.Li3f0606a2007-03-09 13:38:44 +080040#if defined(CONFIG_BF537)&&defined(CONFIG_POST)
41#include <post.h>
42int post_flag;
43#endif
Wolfgang Denkd87080b2006-03-31 18:32:53 +020044
Aubrey.Li3f0606a2007-03-09 13:38:44 +080045#ifndef CFG_NO_FLASH
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010046extern flash_info_t flash_info[];
Aubrey.Li3f0606a2007-03-09 13:38:44 +080047#endif
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010048
Aubrey.Li3f0606a2007-03-09 13:38:44 +080049static 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*/
65u_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 */
80u_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}
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010091
92static 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;
Aubrey.Li3f0606a2007-03-09 13:38:44 +080097 memset((void *)mem_malloc_start, 0, mem_malloc_end - mem_malloc_start);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010098}
99
100void *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
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800110 return ((void *)old);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100111}
112
113static 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
120static void display_flash_config(ulong size)
121{
122 puts("FLASH: ");
123 print_size(size, "\n");
124 return;
125}
126
127static int init_baudrate(void)
128{
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800129 DECLARE_GLOBAL_DATA_PTR;
130
131 char tmp[64];
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100132 int i = getenv_r("baudrate", tmp, sizeof(tmp));
133 gd->bd->bi_baudrate = gd->baudrate = (i > 0)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800134 ? (int)simple_strtoul(tmp, NULL, 10)
135 : CONFIG_BAUDRATE;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100136 return (0);
137}
138
139#ifdef DEBUG
140static void display_global_data(void)
141{
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800142 DECLARE_GLOBAL_DATA_PTR;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100143 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",
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800157 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]);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100161 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
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800172/* we cover everything with 4 meg pages, and need an extra for L1 */
173unsigned int icplb_table[page_descriptor_table_size][2];
174unsigned int dcplb_table[page_descriptor_table_size][2];
175
176void 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
Aubrey Li7b7e30a2007-04-05 18:33:04 +0800185 for (i = 0; i < CONFIG_MEM_SIZE / 4; i++) {
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800186 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)
Aubrey Li7b7e30a2007-04-05 18:33:04 +0800196 /* MAC space */
197 icplb_table[j][0] = 0x2C000000;
198 icplb_table[j][1] = SDRAM_INON_CHBL;
199 j++;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800200 /* Async Memory space */
201 for (i = 0; i < 3; i++) {
Aubrey Li7b7e30a2007-04-05 18:33:04 +0800202 icplb_table[j][0] = 0x20000000 + i * 4 * 1024 * 1024;
203 icplb_table[j][1] = SDRAM_INON_CHBL;
204 j++;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800205 }
206#else
207 icplb_table[j][0] = 0x20000000;
Aubrey Li7b7e30a2007-04-05 18:33:04 +0800208 icplb_table[j][1] = SDRAM_INON_CHBL;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800209#endif
210 j = 0;
211 dcplb_table[j][0] = 0xFF800000;
212 dcplb_table[j][1] = L1_DMEMORY;
213 j++;
214
215 for (i = 0; i < CONFIG_MEM_SIZE / 4; i++) {
216 dcplb_table[j][0] = (i * 4 * 1024 * 1024);
217 if (i * 4 * 1024 * 1024 <= CFG_MONITOR_BASE
218 && (i + 1) * 4 * 1024 * 1024 >= CFG_MONITOR_BASE) {
219 dcplb_table[j][1] = SDRAM_DKERNEL;
220 } else {
221 dcplb_table[j][1] = SDRAM_DGENERIC;
222 }
223 j++;
224 }
225
226#if defined(CONFIG_BF561)
227 /* MAC space */
Aubrey Li7b7e30a2007-04-05 18:33:04 +0800228 dcplb_table[j][0] = 0x2C000000;
229 dcplb_table[j][1] = SDRAM_EBIU;
230 j++;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800231
232 /* Flash space */
Aubrey Li7b7e30a2007-04-05 18:33:04 +0800233 for (i = 0; i < 3; i++) {
234 dcplb_table[j][0] = 0x20000000 + i * 4 * 1024 * 1024;
235 dcplb_table[j][1] = SDRAM_EBIU;
236 j++;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800237 }
238#else
239 dcplb_table[j][0] = 0x20000000;
240 dcplb_table[j][1] = SDRAM_EBIU;
241#endif
242}
243
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100244/*
245 * All attempts to come up with a "common" initialization sequence
246 * that works for all boards and architectures failed: some of the
247 * requirements are just _too_ different. To get rid of the resulting
248 * mess of board dependend #ifdef'ed code we now make the whole
249 * initialization sequence configurable to the user.
250 *
251 * The requirements for any new initalization function is simple: it
252 * receives a pointer to the "global data" structure as it's only
253 * argument, and returns an integer return code, where 0 means
254 * "continue" and != 0 means "fatal error, hang the system".
255 */
256
257void board_init_f(ulong bootflag)
258{
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800259 DECLARE_GLOBAL_DATA_PTR;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100260 ulong addr;
261 bd_t *bd;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800262 int i;
263
264 init_cplbtables();
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100265
266 gd = (gd_t *) (CFG_GBL_DATA_ADDR);
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800267 memset((void *)gd, 0, sizeof(gd_t));
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100268
269 /* Board data initialization */
270 addr = (CFG_GBL_DATA_ADDR + sizeof(gd_t));
271
272 /* Align to 4 byte boundary */
Wolfgang Denk8e7b7032006-03-12 02:55:22 +0100273 addr &= ~(4 - 1);
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800274 bd = (bd_t *) addr;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100275 gd->bd = bd;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800276 memset((void *)bd, 0, sizeof(bd_t));
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100277
278 /* Initialize */
279 init_IRQ();
280 env_init(); /* initialize environment */
281 init_baudrate(); /* initialze baudrate settings */
282 serial_init(); /* serial communications setup */
283 console_init_f();
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800284#ifdef CONFIG_ICACHE_ON
285 icache_enable();
286#endif
287#ifdef CONFIG_DCACHE_ON
288 dcache_enable();
289#endif
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100290 display_banner(); /* say that we are here */
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800291
292 for (i = 0; i < page_descriptor_table_size; i++) {
Aubrey Li8440bb12007-03-12 00:25:14 +0800293 debug
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800294 ("data (%02i)= 0x%08x : 0x%08x intr = 0x%08x : 0x%08x\n",
295 i, dcplb_table[i][0], dcplb_table[i][1], icplb_table[i][0],
296 icplb_table[i][1]);
297 }
298
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100299 checkboard();
300#if defined(CONFIG_RTC_BF533) && (CONFIG_COMMANDS & CFG_CMD_DATE)
301 rtc_init();
302#endif
303 timer_init();
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800304 printf("Clock: VCO: %lu MHz, Core: %lu MHz, System: %lu MHz\n",
305 get_vco() / 1000000, get_cclk() / 1000000, get_sclk() / 1000000);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100306 printf("SDRAM: ");
307 print_size(initdram(0), "\n");
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800308#if defined(CONFIG_BF537)&&defined(CONFIG_POST)
309 post_init_f();
310 post_bootmode_init();
311 post_run(NULL, POST_ROM | post_bootmode_get(0));
312#endif
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100313 board_init_r((gd_t *) gd, 0x20000010);
314}
315
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800316#if defined(CONFIG_SOFT_I2C) || defined(CONFIG_HARD_I2C)
317static int init_func_i2c(void)
318{
319 puts("I2C: ");
320 i2c_init(CFG_I2C_SPEED, CFG_I2C_SLAVE);
321 puts("ready\n");
322 return (0);
323}
324#endif
325
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100326void board_init_r(gd_t * id, ulong dest_addr)
327{
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800328 DECLARE_GLOBAL_DATA_PTR;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100329 ulong size;
330 extern void malloc_bin_reloc(void);
331 char *s, *e;
332 bd_t *bd;
333 int i;
334 gd = id;
335 gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
336 bd = gd->bd;
337
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800338#if defined(CONFIG_BF537) && defined(CONFIG_POST)
339 post_output_backlog();
340 post_reloc();
341#endif
342
343#if (CONFIG_STAMP || CONFIG_BF537 || CONFIG_EZKIT561) && !defined(CFG_NO_FLASH)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100344 /* There are some other pointer constants we must deal with */
345 /* configure available FLASH banks */
346 size = flash_init();
347 display_flash_config(size);
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800348 flash_protect(FLAG_PROTECT_SET, CFG_FLASH_BASE,
349 CFG_FLASH_BASE + 0x1ffff, &flash_info[0]);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100350 bd->bi_flashstart = CFG_FLASH_BASE;
351 bd->bi_flashsize = size;
352 bd->bi_flashoffset = 0;
353#else
354 bd->bi_flashstart = 0;
355 bd->bi_flashsize = 0;
356 bd->bi_flashoffset = 0;
357#endif
358 /* initialize malloc() area */
359 mem_malloc_init();
360 malloc_bin_reloc();
361
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800362#ifdef CONFIG_SPI
363# if ! defined(CFG_ENV_IS_IN_EEPROM)
364 spi_init_f();
365# endif
366 spi_init_r();
367#endif
368
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100369 /* relocate environment function pointers etc. */
370 env_relocate();
371
372 /* board MAC address */
373 s = getenv("ethaddr");
374 for (i = 0; i < 6; ++i) {
375 bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
376 if (s)
377 s = (*e) ? e + 1 : e;
378 }
379
380 /* IP Address */
381 bd->bi_ip_addr = getenv_IPaddr("ipaddr");
382
383 /* Initialize devices */
384 devices_init();
385 jumptable_init();
386
387 /* Initialize the console (after the relocation and devices init) */
388 console_init_r();
389
390 /* Initialize from environment */
391 if ((s = getenv("loadaddr")) != NULL) {
392 load_addr = simple_strtoul(s, NULL, 16);
393 }
394#if (CONFIG_COMMANDS & CFG_CMD_NET)
395 if ((s = getenv("bootfile")) != NULL) {
396 copy_filename(BootFile, s, sizeof(BootFile));
397 }
398#endif
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800399
400#if (CONFIG_COMMANDS & CFG_CMD_NAND)
401 puts("NAND: ");
402 nand_init(); /* go init the NAND */
403#endif
404
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100405#if defined(CONFIG_MISC_INIT_R)
406 /* miscellaneous platform dependent initialisations */
407 misc_init_r();
408#endif
409
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800410#if ((BFIN_CPU == ADSP_BF537) || (BFIN_CPU == ADSP_BF536))
411 printf("Net: ");
412 eth_initialize(bd);
413#endif
414
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100415#ifdef CONFIG_DRIVER_SMC91111
416#ifdef SHARED_RESOURCES
417 /* Switch to Ethernet */
418 swap_to(ETHERNET);
419#endif
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800420 if ((SMC_inw(BANK_SELECT) & UPPER_BYTE_MASK) != SMC_IDENT) {
421 printf("ERROR: Can't find SMC91111 at address %x\n",
422 SMC_BASE_ADDRESS);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100423 } else {
424 printf("Net: SMC91111 at 0x%08X\n", SMC_BASE_ADDRESS);
425 }
426
427#ifdef SHARED_RESOURCES
428 swap_to(FLASH);
429#endif
430#endif
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800431#if defined(CONFIG_SOFT_I2C) || defined(CONFIG_HARD_I2C)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100432 init_func_i2c();
433#endif
434
435#ifdef DEBUG
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800436 display_global_data();
437#endif
438
439#if defined(CONFIG_BF537) && defined(CONFIG_POST)
440 if (post_flag)
441 post_run(NULL, POST_RAM | post_bootmode_get(0));
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100442#endif
443
444 /* main_loop() can return to retry autoboot, if so just run it again. */
445 for (;;) {
446 main_loop();
447 }
448}
449
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100450void hang(void)
451{
452 puts("### ERROR ### Please RESET the board ###\n");
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800453 for (;;) ;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100454}