]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/board_f.c
Introduce generic pre-relocation board_f.c
[karo-tx-uboot.git] / common / board_f.c
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * (C) Copyright 2002-2006
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * (C) Copyright 2002
7  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8  * Marius Groeger <mgroeger@sysgo.de>
9  *
10  * See file CREDITS for list of people who contributed to this
11  * project.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation; either version 2 of
16  * the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26  * MA 02111-1307 USA
27  */
28
29 #include <common.h>
30 #include <linux/compiler.h>
31 #include <version.h>
32 #include <environment.h>
33 #include <fdtdec.h>
34 #include <initcall.h>
35 #include <logbuff.h>
36 #include <post.h>
37 #include <asm/io.h>
38 #include <asm/sections.h>
39 #include <linux/compiler.h>
40
41 /*
42  * Pointer to initial global data area
43  *
44  * Here we initialize it if needed.
45  */
46 #ifdef XTRN_DECLARE_GLOBAL_DATA_PTR
47 #undef  XTRN_DECLARE_GLOBAL_DATA_PTR
48 #define XTRN_DECLARE_GLOBAL_DATA_PTR    /* empty = allocate here */
49 DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
50 #else
51 DECLARE_GLOBAL_DATA_PTR;
52 #endif
53
54 /*
55  * sjg: IMO this code should be
56  * refactored to a single function, something like:
57  *
58  * void led_set_state(enum led_colour_t colour, int on);
59  */
60 /************************************************************************
61  * Coloured LED functionality
62  ************************************************************************
63  * May be supplied by boards if desired
64  */
65 inline void __coloured_LED_init(void) {}
66 void coloured_LED_init(void)
67         __attribute__((weak, alias("__coloured_LED_init")));
68 inline void __red_led_on(void) {}
69 void red_led_on(void) __attribute__((weak, alias("__red_led_on")));
70 inline void __red_led_off(void) {}
71 void red_led_off(void) __attribute__((weak, alias("__red_led_off")));
72 inline void __green_led_on(void) {}
73 void green_led_on(void) __attribute__((weak, alias("__green_led_on")));
74 inline void __green_led_off(void) {}
75 void green_led_off(void) __attribute__((weak, alias("__green_led_off")));
76 inline void __yellow_led_on(void) {}
77 void yellow_led_on(void) __attribute__((weak, alias("__yellow_led_on")));
78 inline void __yellow_led_off(void) {}
79 void yellow_led_off(void) __attribute__((weak, alias("__yellow_led_off")));
80 inline void __blue_led_on(void) {}
81 void blue_led_on(void) __attribute__((weak, alias("__blue_led_on")));
82 inline void __blue_led_off(void) {}
83 void blue_led_off(void) __attribute__((weak, alias("__blue_led_off")));
84
85 /*
86  * Why is gd allocated a register? Prior to reloc it might be better to
87  * just pass it around to each function in this file?
88  *
89  * After reloc one could argue that it is hardly used and doesn't need
90  * to be in a register. Or if it is it should perhaps hold pointers to all
91  * global data for all modules, so that post-reloc we can avoid the massive
92  * literal pool we get on ARM. Or perhaps just encourage each module to use
93  * a structure...
94  */
95
96 /*
97  * Could the CONFIG_SPL_BUILD infection become a flag in gd?
98  */
99
100 static int init_baud_rate(void)
101 {
102         gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
103         return 0;
104 }
105
106 static int display_text_info(void)
107 {
108         ulong bss_start, bss_end;
109
110         bss_start = _bss_start_ofs + _TEXT_BASE;
111         bss_end = _bss_end_ofs + _TEXT_BASE;
112         debug("U-Boot code: %08X -> %08lX  BSS: -> %08lX\n",
113               CONFIG_SYS_TEXT_BASE, bss_start, bss_end);
114
115 #ifdef CONFIG_MODEM_SUPPORT
116         debug("Modem Support enabled\n");
117 #endif
118 #ifdef CONFIG_USE_IRQ
119         debug("IRQ Stack: %08lx\n", IRQ_STACK_START);
120         debug("FIQ Stack: %08lx\n", FIQ_STACK_START);
121 #endif
122
123         return 0;
124 }
125
126 static int announce_dram_init(void)
127 {
128         puts("DRAM:  ");
129         return 0;
130 }
131
132 static int show_dram_config(void)
133 {
134         ulong size;
135
136 #ifdef CONFIG_NR_DRAM_BANKS
137         int i;
138
139         debug("\nRAM Configuration:\n");
140         for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
141                 size += gd->bd->bi_dram[i].size;
142                 debug("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
143 #ifdef DEBUG
144                 print_size(gd->bd->bi_dram[i].size, "\n");
145 #endif
146         }
147         debug("\nDRAM:  ");
148 #else
149         size = gd->ram_size;
150 #endif
151
152         print_size(size, "\n");
153
154         return 0;
155 }
156
157 void __dram_init_banksize(void)
158 {
159 #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
160         gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
161         gd->bd->bi_dram[0].size = get_effective_memsize();
162 #endif
163 }
164
165 void dram_init_banksize(void)
166         __attribute__((weak, alias("__dram_init_banksize")));
167
168 static int zero_global_data(void)
169 {
170         memset((void *)gd, '\0', sizeof(gd_t));
171
172         return 0;
173 }
174
175 static int setup_mon_len(void)
176 {
177         gd->mon_len = _bss_end_ofs;
178         return 0;
179 }
180
181 __weak int arch_cpu_init(void)
182 {
183         return 0;
184 }
185
186 static int setup_fdt(void)
187 {
188 #ifdef CONFIG_OF_EMBED
189         /* Get a pointer to the FDT */
190         gd->fdt_blob = _binary_dt_dtb_start;
191 #elif defined CONFIG_OF_SEPARATE
192         /* FDT is at end of image */
193         gd->fdt_blob = (void *)(_end_ofs + CONFIG_SYS_TEXT_BASE);
194 #endif
195         /* Allow the early environment to override the fdt address */
196         gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
197                                                 (uintptr_t)gd->fdt_blob);
198         return 0;
199 }
200
201 /* Get the top of usable RAM */
202 __weak ulong board_get_usable_ram_top(ulong total_size)
203 {
204         return gd->ram_top;
205 }
206
207 static int setup_dest_addr(void)
208 {
209         debug("Monitor len: %08lX\n", gd->mon_len);
210         /*
211          * Ram is setup, size stored in gd !!
212          */
213         debug("Ram size: %08lX\n", (ulong)gd->ram_size);
214 #if defined(CONFIG_SYS_MEM_TOP_HIDE)
215         /*
216          * Subtract specified amount of memory to hide so that it won't
217          * get "touched" at all by U-Boot. By fixing up gd->ram_size
218          * the Linux kernel should now get passed the now "corrected"
219          * memory size and won't touch it either. This should work
220          * for arch/ppc and arch/powerpc. Only Linux board ports in
221          * arch/powerpc with bootwrapper support, that recalculate the
222          * memory size from the SDRAM controller setup will have to
223          * get fixed.
224          */
225         gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
226 #endif
227 #ifdef CONFIG_SYS_SDRAM_BASE
228         gd->ram_top = CONFIG_SYS_SDRAM_BASE;
229 #endif
230         gd->ram_top = board_get_usable_ram_top(gd->mon_len);
231         gd->dest_addr = gd->ram_top;
232         debug("Ram top: %08lX\n", (ulong)gd->ram_top);
233         gd->dest_addr_sp = gd->dest_addr;
234         return 0;
235 }
236
237 #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
238 static int reserve_logbuffer(void)
239 {
240         /* reserve kernel log buffer */
241         gd->dest_addr -= LOGBUFF_RESERVE;
242         debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
243                 gd->dest_addr);
244         return 0;
245 }
246 #endif
247
248 #ifdef CONFIG_PRAM
249 /* reserve protected RAM */
250 static int reserve_pram(void)
251 {
252         ulong reg;
253
254         reg = getenv_ulong("pram", 10, CONFIG_PRAM);
255         gd->dest_addr -= (reg << 10);           /* size is in kB */
256         debug("Reserving %ldk for protected RAM at %08lx\n", reg,
257               gd->dest_addr);
258         return 0;
259 }
260 #endif /* CONFIG_PRAM */
261
262 /* Round memory pointer down to next 4 kB limit */
263 static int reserve_round_4k(void)
264 {
265         gd->dest_addr &= ~(4096 - 1);
266         return 0;
267 }
268
269 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
270                 defined(CONFIG_ARM)
271 static int reserve_mmu(void)
272 {
273         /* reserve TLB table */
274         gd->arch.tlb_size = 4096 * 4;
275         gd->dest_addr -= gd->arch.tlb_size;
276
277         /* round down to next 64 kB limit */
278         gd->dest_addr &= ~(0x10000 - 1);
279
280         gd->arch.tlb_addr = gd->dest_addr;
281         debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
282               gd->arch.tlb_addr + gd->arch.tlb_size);
283         return 0;
284 }
285 #endif
286
287 #ifdef CONFIG_LCD
288 static int reserve_lcd(void)
289 {
290 #ifdef CONFIG_FB_ADDR
291         gd->fb_base = CONFIG_FB_ADDR;
292 #else
293         /* reserve memory for LCD display (always full pages) */
294         gd->dest_addr = lcd_setmem(gd->dest_addr);
295         gd->fb_base = gd->dest_addr;
296 #endif /* CONFIG_FB_ADDR */
297         return 0;
298 }
299 #endif /* CONFIG_LCD */
300
301 static int reserve_uboot(void)
302 {
303         /*
304          * reserve memory for U-Boot code, data & bss
305          * round down to next 4 kB limit
306          */
307         gd->dest_addr -= gd->mon_len;
308         gd->dest_addr &= ~(4096 - 1);
309
310         debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
311               gd->dest_addr);
312         return 0;
313 }
314
315 /* reserve memory for malloc() area */
316 static int reserve_malloc(void)
317 {
318         gd->dest_addr_sp = gd->dest_addr - TOTAL_MALLOC_LEN;
319         debug("Reserving %dk for malloc() at: %08lx\n",
320                         TOTAL_MALLOC_LEN >> 10, gd->dest_addr_sp);
321         return 0;
322 }
323
324 /* (permanently) allocate a Board Info struct */
325 static int reserve_board(void)
326 {
327         gd->dest_addr_sp -= sizeof(bd_t);
328         gd->bd = (bd_t *)gd->dest_addr_sp;
329         memset(gd->bd, '\0', sizeof(bd_t));
330         debug("Reserving %zu Bytes for Board Info at: %08lx\n",
331                         sizeof(bd_t), gd->dest_addr_sp);
332         return 0;
333 }
334
335 static int setup_machine(void)
336 {
337 #ifdef CONFIG_MACH_TYPE
338         gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
339 #endif
340         return 0;
341 }
342
343 static int reserve_global_data(void)
344 {
345         gd->dest_addr_sp -= sizeof(gd_t);
346         gd->new_gd = (gd_t *)gd->dest_addr_sp;
347         debug("Reserving %zu Bytes for Global Data at: %08lx\n",
348                         sizeof(gd_t), gd->dest_addr_sp);
349         return 0;
350 }
351
352 static int reserve_fdt(void)
353 {
354         /*
355          * If the device tree is sitting immediate above our image then we
356          * must relocate it. If it is embedded in the data section, then it
357          * will be relocated with other data.
358          */
359         if (gd->fdt_blob) {
360                 gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
361
362                 gd->dest_addr_sp -= gd->fdt_size;
363                 gd->new_fdt = (void *)gd->dest_addr_sp;
364                 debug("Reserving %lu Bytes for FDT at: %p\n",
365                       gd->fdt_size, gd->new_fdt);
366         }
367
368         return 0;
369 }
370
371 static int reserve_stacks(void)
372 {
373         /* setup stack pointer for exceptions */
374         gd->dest_addr_sp -= 16;
375         gd->dest_addr_sp &= ~0xf;
376         gd->irq_sp = gd->dest_addr_sp;
377
378         /*
379          * Handle architecture-specific things here
380          * TODO(sjg@chromium.org): Perhaps create arch_reserve_stack()
381          * to handle this and put in arch/xxx/lib/stack.c
382          */
383 # ifdef CONFIG_ARM
384 #  ifdef CONFIG_USE_IRQ
385         gd->dest_addr_sp -= (CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ);
386         debug("Reserving %zu Bytes for IRQ stack at: %08lx\n",
387                 CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ, gd->dest_addr_sp);
388
389         /* 8-byte alignment for ARM ABI compliance */
390         gd->dest_addr_sp &= ~0x07;
391 #  endif
392         /* leave 3 words for abort-stack, plus 1 for alignment */
393         gd->dest_addr_sp -= 16;
394
395         return 0;
396 }
397
398 static int display_new_sp(void)
399 {
400         debug("New Stack Pointer is: %08lx\n", gd->dest_addr_sp);
401
402         return 0;
403 }
404
405 #ifdef CONFIG_POST
406 static int init_post(void)
407 {
408         post_bootmode_init();
409         post_run(NULL, POST_ROM | post_bootmode_get(0));
410
411         return 0;
412 }
413 #endif
414
415 static int setup_baud_rate(void)
416 {
417         /* Ick, can we get rid of this line? */
418         gd->bd->bi_baudrate = gd->baudrate;
419
420         return 0;
421 }
422
423 static int setup_dram_config(void)
424 {
425         /* Ram is board specific, so move it to board code ... */
426         dram_init_banksize();
427
428         return 0;
429 }
430
431 static int reloc_fdt(void)
432 {
433         if (gd->new_fdt) {
434                 memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
435                 gd->fdt_blob = gd->new_fdt;
436         }
437
438         return 0;
439 }
440
441 static int setup_reloc(void)
442 {
443         gd->relocaddr = gd->dest_addr;
444         gd->start_addr_sp = gd->dest_addr_sp;
445         gd->reloc_off = gd->dest_addr - CONFIG_SYS_TEXT_BASE;
446         memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
447
448         debug("Relocation Offset is: %08lx\n", gd->reloc_off);
449         debug("Relocating to %08lx, new gd at %p, sp at %08lx\n",
450               gd->dest_addr, gd->new_gd, gd->dest_addr_sp);
451
452         return 0;
453 }
454
455 /* ARM calls relocate_code from its crt0.S */
456 #if !defined(CONFIG_ARM)
457
458 static int jump_to_copy(void)
459 {
460         relocate_code(gd->dest_addr_sp, gd->new_gd, gd->dest_addr);
461
462         return 0;
463 }
464 #endif
465
466 /* Record the board_init_f() bootstage (after arch_cpu_init()) */
467 static int mark_bootstage(void)
468 {
469         bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
470
471         return 0;
472 }
473
474 static init_fnc_t init_sequence_f[] = {
475         setup_fdt,
476         setup_mon_len,
477         arch_cpu_init,          /* basic arch cpu dependent setup */
478         mark_bootstage,
479 #ifdef CONFIG_OF_CONTROL
480         fdtdec_check_fdt,
481 #endif
482 #if defined(CONFIG_BOARD_EARLY_INIT_F)
483         board_early_init_f,
484 #endif
485         timer_init,             /* initialize timer */
486 #ifdef CONFIG_BOARD_POSTCLK_INIT
487         board_postclk_init,
488 #endif
489 #ifdef CONFIG_FSL_ESDHC
490         get_clocks,
491 #endif
492         env_init,               /* initialize environment */
493         init_baud_rate,         /* initialze baudrate settings */
494         serial_init,            /* serial communications setup */
495         console_init_f,         /* stage 1 init of console */
496         display_options,        /* say that we are here */
497         display_text_info,      /* show debugging info if required */
498 #if defined(CONFIG_DISPLAY_CPUINFO)
499         print_cpuinfo,          /* display cpu info (and speed) */
500 #endif
501 #if defined(CONFIG_DISPLAY_BOARDINFO)
502         checkboard,             /* display board info */
503 #endif
504         announce_dram_init,
505         /* TODO: unify all these dram functions? */
506 #ifdef CONFIG_ARM
507         dram_init,              /* configure available RAM banks */
508 #endif
509 #ifdef CONFIG_POST
510         init_post,
511 #endif
512         /*
513          * Now that we have DRAM mapped and working, we can
514          * relocate the code and continue running from DRAM.
515          *
516          * Reserve memory at end of RAM for (top down in that order):
517          *  - area that won't get touched by U-Boot and Linux (optional)
518          *  - kernel log buffer
519          *  - protected RAM
520          *  - LCD framebuffer
521          *  - monitor code
522          *  - board info struct
523          */
524         setup_dest_addr,
525 #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
526         reserve_logbuffer,
527 #endif
528 #ifdef CONFIG_PRAM
529         reserve_pram,
530 #endif
531         reserve_round_4k,
532 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
533                 defined(CONFIG_ARM)
534         reserve_mmu,
535 #endif
536 #ifdef CONFIG_LCD
537         reserve_lcd,
538 #endif
539         reserve_uboot,
540         reserve_malloc,
541         reserve_board,
542         setup_machine,
543         reserve_global_data,
544         reserve_fdt,
545         reserve_stacks,
546         setup_dram_config,
547         show_dram_config,
548         setup_baud_rate,
549         display_new_sp,
550         reloc_fdt,
551         setup_reloc,
552 #ifndef CONFIG_ARM
553         jump_to_copy,
554 #endif
555         NULL,
556 };
557
558 void board_init_f(ulong boot_flags)
559 {
560         gd_t data;
561
562         gd = &data;
563
564         gd->flags = boot_flags;
565
566         if (initcall_run_list(init_sequence_f))
567                 hang();
568
569 #ifndef CONFIG_ARM
570         /* NOTREACHED - jump_to_copy() does not return */
571         hang();
572 #endif
573 }
574
575 void hang(void)
576 {
577         puts("### ERROR ### Please RESET the board ###\n");
578         for (;;);
579 }