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