]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/blackfin/lib/board.c
Merge branch 'u-boot-imx/master' into 'u-boot-arm/master'
[karo-tx-uboot.git] / arch / blackfin / lib / board.c
1 /*
2  * U-boot - board.c First C file to be called contains init routines
3  *
4  * Copyright (c) 2005-2008 Analog Devices Inc.
5  *
6  * (C) Copyright 2000-2004
7  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8  *
9  * Licensed under the GPL-2 or later.
10  */
11
12 #include <common.h>
13 #include <command.h>
14 #include <stdio_dev.h>
15 #include <serial.h>
16 #include <environment.h>
17 #include <malloc.h>
18 #include <mmc.h>
19 #include <net.h>
20 #include <status_led.h>
21 #include <version.h>
22 #include <watchdog.h>
23
24 #include <asm/cplb.h>
25 #include <asm/mach-common/bits/mpu.h>
26 #include <kgdb.h>
27
28 #ifdef CONFIG_CMD_NAND
29 #include <nand.h>       /* cannot even include nand.h if it isnt configured */
30 #endif
31
32 #ifdef CONFIG_BITBANGMII
33 #include <miiphy.h>
34 #endif
35
36 #if defined(CONFIG_POST)
37 #include <post.h>
38 int post_flag;
39 #endif
40
41 #if defined(CONFIG_SYS_I2C)
42 #include <i2c.h>
43 #endif
44
45 DECLARE_GLOBAL_DATA_PTR;
46
47 __attribute__((always_inline))
48 static inline void serial_early_puts(const char *s)
49 {
50 #ifdef CONFIG_DEBUG_EARLY_SERIAL
51         serial_puts("Early: ");
52         serial_puts(s);
53 #endif
54 }
55
56 static int display_banner(void)
57 {
58         display_options();
59         printf("CPU:   ADSP %s "
60                 "(Detected Rev: 0.%d) "
61                 "(%s boot)\n",
62                 gd->bd->bi_cpu,
63                 bfin_revid(),
64                 get_bfin_boot_mode(CONFIG_BFIN_BOOT_MODE));
65         return 0;
66 }
67
68 static int init_baudrate(void)
69 {
70         gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
71         gd->bd->bi_baudrate = gd->baudrate;
72         return 0;
73 }
74
75 static void display_global_data(void)
76 {
77         bd_t *bd;
78
79 #ifndef CONFIG_DEBUG_EARLY_SERIAL
80         return;
81 #endif
82
83         bd = gd->bd;
84         printf(" gd: %p\n", gd);
85         printf(" |-flags: %lx\n", gd->flags);
86         printf(" |-board_type: %lx\n", gd->arch.board_type);
87         printf(" |-baudrate: %u\n", gd->baudrate);
88         printf(" |-have_console: %lx\n", gd->have_console);
89         printf(" |-ram_size: %lx\n", gd->ram_size);
90         printf(" |-env_addr: %lx\n", gd->env_addr);
91         printf(" |-env_valid: %lx\n", gd->env_valid);
92         printf(" |-jt(%p): %p\n", gd->jt, *(gd->jt));
93         printf(" \\-bd: %p\n", gd->bd);
94         printf("   |-bi_baudrate: %x\n", bd->bi_baudrate);
95         printf("   |-bi_boot_params: %lx\n", bd->bi_boot_params);
96         printf("   |-bi_memstart: %lx\n", bd->bi_memstart);
97         printf("   |-bi_memsize: %lx\n", bd->bi_memsize);
98         printf("   |-bi_flashstart: %lx\n", bd->bi_flashstart);
99         printf("   |-bi_flashsize: %lx\n", bd->bi_flashsize);
100         printf("   \\-bi_flashoffset: %lx\n", bd->bi_flashoffset);
101 }
102
103 #define CPLB_PAGE_SIZE (4 * 1024 * 1024)
104 #define CPLB_PAGE_MASK (~(CPLB_PAGE_SIZE - 1))
105 #if defined(__ADSPBF60x__)
106 #define CPLB_EX_PAGE_SIZE (16 * 1024 * 1024)
107 #define CPLB_EX_PAGE_MASK (~(CPLB_EX_PAGE_SIZE - 1))
108 #else
109 #define CPLB_EX_PAGE_SIZE CPLB_PAGE_SIZE
110 #define CPLB_EX_PAGE_MASK CPLB_PAGE_MASK
111 #endif
112 void init_cplbtables(void)
113 {
114         volatile uint32_t *ICPLB_ADDR, *ICPLB_DATA;
115         volatile uint32_t *DCPLB_ADDR, *DCPLB_DATA;
116         uint32_t extern_memory;
117         size_t i;
118
119         void icplb_add(uint32_t addr, uint32_t data)
120         {
121                 *(ICPLB_ADDR + i) = addr;
122                 *(ICPLB_DATA + i) = data;
123         }
124         void dcplb_add(uint32_t addr, uint32_t data)
125         {
126                 *(DCPLB_ADDR + i) = addr;
127                 *(DCPLB_DATA + i) = data;
128         }
129
130         /* populate a few common entries ... we'll let
131          * the memory map and cplb exception handler do
132          * the rest of the work.
133          */
134         i = 0;
135         ICPLB_ADDR = (uint32_t *)ICPLB_ADDR0;
136         ICPLB_DATA = (uint32_t *)ICPLB_DATA0;
137         DCPLB_ADDR = (uint32_t *)DCPLB_ADDR0;
138         DCPLB_DATA = (uint32_t *)DCPLB_DATA0;
139
140         icplb_add(0xFFA00000, L1_IMEMORY);
141         dcplb_add(0xFF800000, L1_DMEMORY);
142         ++i;
143 #if defined(__ADSPBF60x__)
144         icplb_add(0x0, 0x0);
145         dcplb_add(CONFIG_SYS_FLASH_BASE, PAGE_SIZE_16MB | CPLB_DIRTY |
146                 CPLB_SUPV_WR | CPLB_USER_WR | CPLB_USER_RD | CPLB_VALID);
147         ++i;
148 #endif
149
150         if (CONFIG_MEM_SIZE) {
151                 uint32_t mbase = CONFIG_SYS_MONITOR_BASE;
152                 uint32_t mend  = mbase + CONFIG_SYS_MONITOR_LEN;
153                 mbase &= CPLB_PAGE_MASK;
154                 mend &= CPLB_PAGE_MASK;
155
156                 icplb_add(mbase, SDRAM_IKERNEL);
157                 dcplb_add(mbase, SDRAM_DKERNEL);
158                 ++i;
159
160                 /*
161                  * If the monitor crosses a 4 meg boundary, we'll need
162                  * to lock two entries for it.  We assume it doesn't
163                  * cross two 4 meg boundaries ...
164                  */
165                 if (mbase != mend) {
166                         icplb_add(mend, SDRAM_IKERNEL);
167                         dcplb_add(mend, SDRAM_DKERNEL);
168                         ++i;
169                 }
170         }
171
172 #ifndef __ADSPBF60x__
173         icplb_add(0x20000000, SDRAM_INON_CHBL);
174         dcplb_add(0x20000000, SDRAM_EBIU);
175         ++i;
176 #endif
177
178         /* Add entries for the rest of external RAM up to the bootrom */
179         extern_memory = 0;
180
181 #ifdef CONFIG_DEBUG_NULL_PTR
182         icplb_add(extern_memory, (SDRAM_IKERNEL & ~PAGE_SIZE_MASK) | PAGE_SIZE_1KB);
183         dcplb_add(extern_memory, (SDRAM_DKERNEL & ~PAGE_SIZE_MASK) | PAGE_SIZE_1KB);
184         ++i;
185         icplb_add(extern_memory, SDRAM_IKERNEL);
186         dcplb_add(extern_memory, SDRAM_DKERNEL);
187         extern_memory += CPLB_PAGE_SIZE;
188         ++i;
189 #endif
190
191         while (i < 16 && extern_memory <
192                 (CONFIG_SYS_MONITOR_BASE & CPLB_EX_PAGE_MASK)) {
193                 icplb_add(extern_memory, SDRAM_IGENERIC);
194                 dcplb_add(extern_memory, SDRAM_DGENERIC);
195                 extern_memory += CPLB_EX_PAGE_SIZE;
196                 ++i;
197         }
198         while (i < 16) {
199                 icplb_add(0, 0);
200                 dcplb_add(0, 0);
201                 ++i;
202         }
203 }
204
205 static int global_board_data_init(void)
206 {
207 #ifndef CONFIG_SYS_GBL_DATA_ADDR
208 # define CONFIG_SYS_GBL_DATA_ADDR 0
209 #endif
210 #ifndef CONFIG_SYS_BD_INFO_ADDR
211 # define CONFIG_SYS_BD_INFO_ADDR 0
212 #endif
213
214         bd_t *bd;
215
216         if (CONFIG_SYS_GBL_DATA_ADDR) {
217                 gd = (gd_t *) (CONFIG_SYS_GBL_DATA_ADDR);
218                 memset((void *)gd, 0, GENERATED_GBL_DATA_SIZE);
219         } else {
220                 static gd_t _bfin_gd;
221                 gd = &_bfin_gd;
222         }
223
224         if (CONFIG_SYS_BD_INFO_ADDR) {
225                 bd = (bd_t *) (CONFIG_SYS_BD_INFO_ADDR);
226                 memset(bd, 0, GENERATED_BD_INFO_SIZE);
227         } else {
228                 static bd_t _bfin_bd;
229                 bd = &_bfin_bd;
230         }
231         gd->bd = bd;
232
233         bd->bi_r_version = version_string;
234         bd->bi_cpu = __stringify(CONFIG_BFIN_CPU);
235         bd->bi_board_name = BFIN_BOARD_NAME;
236         bd->bi_vco = get_vco();
237         bd->bi_cclk = get_cclk();
238         bd->bi_sclk = get_sclk();
239         bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;
240         bd->bi_memsize = CONFIG_SYS_MAX_RAM_SIZE;
241
242         return 0;
243 }
244
245 /*
246  * All attempts to come up with a "common" initialization sequence
247  * that works for all boards and architectures failed: some of the
248  * requirements are just _too_ different. To get rid of the resulting
249  * mess of board dependend #ifdef'ed code we now make the whole
250  * initialization sequence configurable to the user.
251  *
252  * The requirements for any new initalization function is simple: it
253  * receives a pointer to the "global data" structure as it's only
254  * argument, and returns an integer return code, where 0 means
255  * "continue" and != 0 means "fatal error, hang the system".
256  */
257
258 extern int watchdog_init(void);
259 extern int exception_init(void);
260 extern int irq_init(void);
261 extern int timer_init(void);
262
263 void board_init_f(ulong bootflag)
264 {
265         char buf[32];
266
267 #ifdef CONFIG_BOARD_EARLY_INIT_F
268         serial_early_puts("Board early init flash\n");
269         board_early_init_f();
270 #endif
271
272         serial_early_puts("Init CPLB tables\n");
273         init_cplbtables();
274
275         serial_early_puts("Exceptions setup\n");
276         exception_init();
277
278 #ifndef CONFIG_ICACHE_OFF
279         serial_early_puts("Turn on ICACHE\n");
280         icache_enable();
281 #endif
282 #ifndef CONFIG_DCACHE_OFF
283         serial_early_puts("Turn on DCACHE\n");
284         dcache_enable();
285 #endif
286
287 #ifdef CONFIG_HW_WATCHDOG
288         serial_early_puts("Setting up external watchdog\n");
289         hw_watchdog_init();
290 #endif
291
292 #ifdef DEBUG
293         if (GENERATED_GBL_DATA_SIZE < sizeof(*gd))
294                 hang();
295 #endif
296         serial_early_puts("Init global data\n");
297
298         global_board_data_init();
299
300         /* Initialize */
301         serial_early_puts("IRQ init\n");
302         irq_init();
303         serial_early_puts("Environment init\n");
304         env_init();
305         serial_early_puts("Baudrate init\n");
306         init_baudrate();
307         serial_early_puts("Serial init\n");
308         serial_init();
309         serial_initialize();
310         serial_early_puts("Console init flash\n");
311         console_init_f();
312         serial_early_puts("End of early debugging\n");
313         display_banner();
314
315         checkboard();
316         timer_init();
317
318         printf("Clock: VCO: %s MHz, ", strmhz(buf, get_vco()));
319         printf("Core: %s MHz, ", strmhz(buf, get_cclk()));
320 #if defined(__ADSPBF60x__)
321         printf("System0: %s MHz, ", strmhz(buf, get_sclk0()));
322         printf("System1: %s MHz, ", strmhz(buf, get_sclk1()));
323         printf("Dclk: %s MHz\n", strmhz(buf, get_dclk()));
324 #else
325         printf("System: %s MHz\n", strmhz(buf, get_sclk()));
326 #endif
327
328         if (CONFIG_MEM_SIZE) {
329                 printf("RAM:   ");
330                 print_size(gd->bd->bi_memsize, "\n");
331         }
332
333 #if defined(CONFIG_POST)
334         post_init_f();
335         post_bootmode_init();
336         post_run(NULL, POST_ROM | post_bootmode_get(0));
337 #endif
338
339         board_init_r((gd_t *) gd, 0x20000010);
340 }
341
342 static void board_net_init_r(bd_t *bd)
343 {
344 #ifdef CONFIG_BITBANGMII
345         bb_miiphy_init();
346 #endif
347 #ifdef CONFIG_CMD_NET
348         printf("Net:   ");
349         eth_initialize(bd);
350 #endif
351 }
352
353 void board_init_r(gd_t * id, ulong dest_addr)
354 {
355         bd_t *bd;
356         gd = id;
357         gd->flags |= GD_FLG_RELOC;      /* tell others: relocation done */
358         bd = gd->bd;
359
360 #if defined(CONFIG_POST)
361         post_output_backlog();
362 #endif
363
364         /* initialize malloc() area */
365         mem_malloc_init(CONFIG_SYS_MALLOC_BASE, CONFIG_SYS_MALLOC_LEN);
366
367 #if     !defined(CONFIG_SYS_NO_FLASH)
368         /* Initialize the flash and protect u-boot by default */
369         extern flash_info_t flash_info[];
370         puts("Flash: ");
371         ulong size = flash_init();
372         print_size(size, "\n");
373         flash_protect(FLAG_PROTECT_SET, CONFIG_SYS_FLASH_BASE,
374                 CONFIG_SYS_FLASH_BASE + CONFIG_SYS_MONITOR_LEN - 1,
375                 &flash_info[0]);
376         bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
377         bd->bi_flashsize = size;
378         bd->bi_flashoffset = 0;
379 #else
380         bd->bi_flashstart = 0;
381         bd->bi_flashsize = 0;
382         bd->bi_flashoffset = 0;
383 #endif
384
385 #ifdef CONFIG_CMD_NAND
386         puts("NAND:  ");
387         nand_init();            /* go init the NAND */
388 #endif
389
390 #ifdef CONFIG_GENERIC_MMC
391         puts("MMC:   ");
392         mmc_initialize(bd);
393 #endif
394
395 #if defined(CONFIG_SYS_I2C)
396         i2c_reloc_fixup();
397 #endif
398         /* relocate environment function pointers etc. */
399         env_relocate();
400
401         /* Initialize stdio devices */
402         stdio_init();
403         jumptable_init();
404
405         /* Initialize the console (after the relocation and devices init) */
406         console_init_r();
407
408 #ifdef CONFIG_CMD_KGDB
409         puts("KGDB:  ");
410         kgdb_init();
411 #endif
412
413 #ifdef CONFIG_STATUS_LED
414         status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
415         status_led_set(STATUS_LED_CRASH, STATUS_LED_OFF);
416 #endif
417
418         /* Initialize from environment */
419         load_addr = getenv_ulong("loadaddr", 16, load_addr);
420
421 #if defined(CONFIG_MISC_INIT_R)
422         /* miscellaneous platform dependent initialisations */
423         misc_init_r();
424 #endif
425
426         board_net_init_r(bd);
427
428         display_global_data();
429
430 #if defined(CONFIG_POST)
431         if (post_flag)
432                 post_run(NULL, POST_RAM | post_bootmode_get(0));
433 #endif
434
435         if (CONFIG_MEM_SIZE && bfin_os_log_check()) {
436                 puts("\nLog buffer from operating system:\n");
437                 bfin_os_log_dump();
438                 puts("\n");
439         }
440
441         /* main_loop() can return to retry autoboot, if so just run it again. */
442         for (;;)
443                 main_loop();
444 }