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