blob: 7c6a1e93c6d5d3105f834eaf2ffb98cfc7e5f60a [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 *
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>
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#ifdef DEBUG
46#define pr_debug(fmt,arg...) printf(fmt,##arg)
47#else
48static inline int
49 __attribute__ ((format(printf, 1, 2))) pr_debug(const char *fmt, ...)
50{
51 return 0;
52}
53#endif
54
55#ifndef CFG_NO_FLASH
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010056extern flash_info_t flash_info[];
Aubrey.Li3f0606a2007-03-09 13:38:44 +080057#endif
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010058
Aubrey.Li3f0606a2007-03-09 13:38:44 +080059static inline u_long get_vco(void)
60{
61 u_long msel;
62 u_long vco;
63
64 msel = (*pPLL_CTL >> 9) & 0x3F;
65 if (0 == msel)
66 msel = 64;
67
68 vco = CONFIG_CLKIN_HZ;
69 vco >>= (1 & *pPLL_CTL); /* DF bit */
70 vco = msel * vco;
71 return vco;
72}
73
74/*Get the Core clock*/
75u_long get_cclk(void)
76{
77 u_long csel, ssel;
78 if (*pPLL_STAT & 0x1)
79 return CONFIG_CLKIN_HZ;
80
81 ssel = *pPLL_DIV;
82 csel = ((ssel >> 4) & 0x03);
83 ssel &= 0xf;
84 if (ssel && ssel < (1 << csel)) /* SCLK > CCLK */
85 return get_vco() / ssel;
86 return get_vco() >> csel;
87}
88
89/* Get the System clock */
90u_long get_sclk(void)
91{
92 u_long ssel;
93
94 if (*pPLL_STAT & 0x1)
95 return CONFIG_CLKIN_HZ;
96
97 ssel = (*pPLL_DIV & 0xf);
98
99 return get_vco() / ssel;
100}
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100101
102static void mem_malloc_init(void)
103{
104 mem_malloc_start = CFG_MALLOC_BASE;
105 mem_malloc_end = (CFG_MALLOC_BASE + CFG_MALLOC_LEN);
106 mem_malloc_brk = mem_malloc_start;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800107 memset((void *)mem_malloc_start, 0, mem_malloc_end - mem_malloc_start);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100108}
109
110void *sbrk(ptrdiff_t increment)
111{
112 ulong old = mem_malloc_brk;
113 ulong new = old + increment;
114
115 if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
116 return (NULL);
117 }
118 mem_malloc_brk = new;
119
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800120 return ((void *)old);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100121}
122
123static int display_banner(void)
124{
125 sprintf(version_string, VERSION_STRING_FORMAT, VERSION_STRING);
126 printf("%s\n", version_string);
127 return (0);
128}
129
130static void display_flash_config(ulong size)
131{
132 puts("FLASH: ");
133 print_size(size, "\n");
134 return;
135}
136
137static int init_baudrate(void)
138{
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800139 DECLARE_GLOBAL_DATA_PTR;
140
141 char tmp[64];
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100142 int i = getenv_r("baudrate", tmp, sizeof(tmp));
143 gd->bd->bi_baudrate = gd->baudrate = (i > 0)
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800144 ? (int)simple_strtoul(tmp, NULL, 10)
145 : CONFIG_BAUDRATE;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100146 return (0);
147}
148
149#ifdef DEBUG
150static void display_global_data(void)
151{
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800152 DECLARE_GLOBAL_DATA_PTR;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100153 bd_t *bd;
154 bd = gd->bd;
155 printf("--flags:%x\n", gd->flags);
156 printf("--board_type:%x\n", gd->board_type);
157 printf("--baudrate:%x\n", gd->baudrate);
158 printf("--have_console:%x\n", gd->have_console);
159 printf("--ram_size:%x\n", gd->ram_size);
160 printf("--reloc_off:%x\n", gd->reloc_off);
161 printf("--env_addr:%x\n", gd->env_addr);
162 printf("--env_valid:%x\n", gd->env_valid);
163 printf("--bd:%x %x\n", gd->bd, bd);
164 printf("---bi_baudrate:%x\n", bd->bi_baudrate);
165 printf("---bi_ip_addr:%x\n", bd->bi_ip_addr);
166 printf("---bi_enetaddr:%x %x %x %x %x %x\n",
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800167 bd->bi_enetaddr[0],
168 bd->bi_enetaddr[1],
169 bd->bi_enetaddr[2],
170 bd->bi_enetaddr[3], bd->bi_enetaddr[4], bd->bi_enetaddr[5]);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100171 printf("---bi_arch_number:%x\n", bd->bi_arch_number);
172 printf("---bi_boot_params:%x\n", bd->bi_boot_params);
173 printf("---bi_memstart:%x\n", bd->bi_memstart);
174 printf("---bi_memsize:%x\n", bd->bi_memsize);
175 printf("---bi_flashstart:%x\n", bd->bi_flashstart);
176 printf("---bi_flashsize:%x\n", bd->bi_flashsize);
177 printf("---bi_flashoffset:%x\n", bd->bi_flashoffset);
178 printf("--jt:%x *:%x\n", gd->jt, *(gd->jt));
179}
180#endif
181
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800182/* we cover everything with 4 meg pages, and need an extra for L1 */
183unsigned int icplb_table[page_descriptor_table_size][2];
184unsigned int dcplb_table[page_descriptor_table_size][2];
185
186void init_cplbtables(void)
187{
188 int i, j;
189
190 j = 0;
191 icplb_table[j][0] = 0xFFA00000;
192 icplb_table[j][1] = L1_IMEMORY;
193 j++;
194
195 for (i = 0; i <= CONFIG_MEM_SIZE / 4; i++) {
196 icplb_table[j][0] = (i * 4 * 1024 * 1024);
197 if (i * 4 * 1024 * 1024 <= CFG_MONITOR_BASE
198 && (i + 1) * 4 * 1024 * 1024 >= CFG_MONITOR_BASE) {
199 icplb_table[j][1] = SDRAM_IKERNEL;
200 } else {
201 icplb_table[j][1] = SDRAM_IGENERIC;
202 }
203 j++;
204 }
205#if defined(CONFIG_BF561)
206 /* Async Memory space */
207 for (i = 0; i < 3; i++) {
208 icplb_table[j++][0] = 0x20000000 + i * 4 * 1024 * 1024;
209 icplb_table[j++][1] = SDRAM_IGENERIC;
210 }
211#else
212 icplb_table[j][0] = 0x20000000;
213 icplb_table[j][1] = SDRAM_IGENERIC;
214#endif
215 j = 0;
216 dcplb_table[j][0] = 0xFF800000;
217 dcplb_table[j][1] = L1_DMEMORY;
218 j++;
219
220 for (i = 0; i < CONFIG_MEM_SIZE / 4; i++) {
221 dcplb_table[j][0] = (i * 4 * 1024 * 1024);
222 if (i * 4 * 1024 * 1024 <= CFG_MONITOR_BASE
223 && (i + 1) * 4 * 1024 * 1024 >= CFG_MONITOR_BASE) {
224 dcplb_table[j][1] = SDRAM_DKERNEL;
225 } else {
226 dcplb_table[j][1] = SDRAM_DGENERIC;
227 }
228 j++;
229 }
230
231#if defined(CONFIG_BF561)
232 /* MAC space */
233 dcplb_table[j++][0] = CONFIG_ASYNC_EBIU_BASE;
234 dcplb_table[j++][1] = SDRAM_EBIU;
235
236 /* Flash space */
237 for (i = 0; i < 2; i++) {
238 dcplb_table[j++][0] = 0x20000000 + i * 4 * 1024 * 1024;
239 dcplb_table[j++][1] = SDRAM_EBIU;
240 }
241#else
242 dcplb_table[j][0] = 0x20000000;
243 dcplb_table[j][1] = SDRAM_EBIU;
244#endif
245}
246
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100247/*
248 * All attempts to come up with a "common" initialization sequence
249 * that works for all boards and architectures failed: some of the
250 * requirements are just _too_ different. To get rid of the resulting
251 * mess of board dependend #ifdef'ed code we now make the whole
252 * initialization sequence configurable to the user.
253 *
254 * The requirements for any new initalization function is simple: it
255 * receives a pointer to the "global data" structure as it's only
256 * argument, and returns an integer return code, where 0 means
257 * "continue" and != 0 means "fatal error, hang the system".
258 */
259
260void board_init_f(ulong bootflag)
261{
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800262 DECLARE_GLOBAL_DATA_PTR;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100263 ulong addr;
264 bd_t *bd;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800265 int i;
266
267 init_cplbtables();
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100268
269 gd = (gd_t *) (CFG_GBL_DATA_ADDR);
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800270 memset((void *)gd, 0, sizeof(gd_t));
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100271
272 /* Board data initialization */
273 addr = (CFG_GBL_DATA_ADDR + sizeof(gd_t));
274
275 /* Align to 4 byte boundary */
Wolfgang Denk8e7b7032006-03-12 02:55:22 +0100276 addr &= ~(4 - 1);
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800277 bd = (bd_t *) addr;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100278 gd->bd = bd;
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800279 memset((void *)bd, 0, sizeof(bd_t));
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100280
281 /* Initialize */
282 init_IRQ();
283 env_init(); /* initialize environment */
284 init_baudrate(); /* initialze baudrate settings */
285 serial_init(); /* serial communications setup */
286 console_init_f();
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800287#ifdef CONFIG_ICACHE_ON
288 icache_enable();
289#endif
290#ifdef CONFIG_DCACHE_ON
291 dcache_enable();
292#endif
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100293 display_banner(); /* say that we are here */
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800294
295 for (i = 0; i < page_descriptor_table_size; i++) {
296 pr_debug
297 ("data (%02i)= 0x%08x : 0x%08x intr = 0x%08x : 0x%08x\n",
298 i, dcplb_table[i][0], dcplb_table[i][1], icplb_table[i][0],
299 icplb_table[i][1]);
300 }
301
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100302 checkboard();
303#if defined(CONFIG_RTC_BF533) && (CONFIG_COMMANDS & CFG_CMD_DATE)
304 rtc_init();
305#endif
306 timer_init();
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800307 printf("Clock: VCO: %lu MHz, Core: %lu MHz, System: %lu MHz\n",
308 get_vco() / 1000000, get_cclk() / 1000000, get_sclk() / 1000000);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100309 printf("SDRAM: ");
310 print_size(initdram(0), "\n");
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800311#if defined(CONFIG_BF537)&&defined(CONFIG_POST)
312 post_init_f();
313 post_bootmode_init();
314 post_run(NULL, POST_ROM | post_bootmode_get(0));
315#endif
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100316 board_init_r((gd_t *) gd, 0x20000010);
317}
318
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800319#if defined(CONFIG_SOFT_I2C) || defined(CONFIG_HARD_I2C)
320static int init_func_i2c(void)
321{
322 puts("I2C: ");
323 i2c_init(CFG_I2C_SPEED, CFG_I2C_SLAVE);
324 puts("ready\n");
325 return (0);
326}
327#endif
328
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100329void board_init_r(gd_t * id, ulong dest_addr)
330{
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800331 DECLARE_GLOBAL_DATA_PTR;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100332 ulong size;
333 extern void malloc_bin_reloc(void);
334 char *s, *e;
335 bd_t *bd;
336 int i;
337 gd = id;
338 gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
339 bd = gd->bd;
340
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800341#if defined(CONFIG_BF537) && defined(CONFIG_POST)
342 post_output_backlog();
343 post_reloc();
344#endif
345
346#if (CONFIG_STAMP || CONFIG_BF537 || CONFIG_EZKIT561) && !defined(CFG_NO_FLASH)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100347 /* There are some other pointer constants we must deal with */
348 /* configure available FLASH banks */
349 size = flash_init();
350 display_flash_config(size);
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800351 flash_protect(FLAG_PROTECT_SET, CFG_FLASH_BASE,
352 CFG_FLASH_BASE + 0x1ffff, &flash_info[0]);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100353 bd->bi_flashstart = CFG_FLASH_BASE;
354 bd->bi_flashsize = size;
355 bd->bi_flashoffset = 0;
356#else
357 bd->bi_flashstart = 0;
358 bd->bi_flashsize = 0;
359 bd->bi_flashoffset = 0;
360#endif
361 /* initialize malloc() area */
362 mem_malloc_init();
363 malloc_bin_reloc();
364
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800365#ifdef CONFIG_SPI
366# if ! defined(CFG_ENV_IS_IN_EEPROM)
367 spi_init_f();
368# endif
369 spi_init_r();
370#endif
371
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100372 /* relocate environment function pointers etc. */
373 env_relocate();
374
375 /* board MAC address */
376 s = getenv("ethaddr");
377 for (i = 0; i < 6; ++i) {
378 bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
379 if (s)
380 s = (*e) ? e + 1 : e;
381 }
382
383 /* IP Address */
384 bd->bi_ip_addr = getenv_IPaddr("ipaddr");
385
386 /* Initialize devices */
387 devices_init();
388 jumptable_init();
389
390 /* Initialize the console (after the relocation and devices init) */
391 console_init_r();
392
393 /* Initialize from environment */
394 if ((s = getenv("loadaddr")) != NULL) {
395 load_addr = simple_strtoul(s, NULL, 16);
396 }
397#if (CONFIG_COMMANDS & CFG_CMD_NET)
398 if ((s = getenv("bootfile")) != NULL) {
399 copy_filename(BootFile, s, sizeof(BootFile));
400 }
401#endif
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800402
403#if (CONFIG_COMMANDS & CFG_CMD_NAND)
404 puts("NAND: ");
405 nand_init(); /* go init the NAND */
406#endif
407
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100408#if defined(CONFIG_MISC_INIT_R)
409 /* miscellaneous platform dependent initialisations */
410 misc_init_r();
411#endif
412
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800413#if ((BFIN_CPU == ADSP_BF537) || (BFIN_CPU == ADSP_BF536))
414 printf("Net: ");
415 eth_initialize(bd);
416#endif
417
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100418#ifdef CONFIG_DRIVER_SMC91111
419#ifdef SHARED_RESOURCES
420 /* Switch to Ethernet */
421 swap_to(ETHERNET);
422#endif
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800423 if ((SMC_inw(BANK_SELECT) & UPPER_BYTE_MASK) != SMC_IDENT) {
424 printf("ERROR: Can't find SMC91111 at address %x\n",
425 SMC_BASE_ADDRESS);
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100426 } else {
427 printf("Net: SMC91111 at 0x%08X\n", SMC_BASE_ADDRESS);
428 }
429
430#ifdef SHARED_RESOURCES
431 swap_to(FLASH);
432#endif
433#endif
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800434#if defined(CONFIG_SOFT_I2C) || defined(CONFIG_HARD_I2C)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100435 init_func_i2c();
436#endif
437
438#ifdef DEBUG
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800439 display_global_data();
440#endif
441
442#if defined(CONFIG_BF537) && defined(CONFIG_POST)
443 if (post_flag)
444 post_run(NULL, POST_RAM | post_bootmode_get(0));
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100445#endif
446
447 /* main_loop() can return to retry autoboot, if so just run it again. */
448 for (;;) {
449 main_loop();
450 }
451}
452
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100453void hang(void)
454{
455 puts("### ERROR ### Please RESET the board ###\n");
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800456 for (;;) ;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100457}