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