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