]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - common/board_f.c
common: Display >=4GiB memory bank size
[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  * SPDX-License-Identifier:     GPL-2.0+
11  */
12
13 #include <common.h>
14 #include <linux/compiler.h>
15 #include <version.h>
16 #include <environment.h>
17 #include <dm.h>
18 #include <fdtdec.h>
19 #include <fs.h>
20 #if defined(CONFIG_CMD_IDE)
21 #include <ide.h>
22 #endif
23 #include <i2c.h>
24 #include <initcall.h>
25 #include <logbuff.h>
26 #include <malloc.h>
27 #include <mapmem.h>
28
29 /* TODO: Can we move these into arch/ headers? */
30 #ifdef CONFIG_8xx
31 #include <mpc8xx.h>
32 #endif
33 #ifdef CONFIG_5xx
34 #include <mpc5xx.h>
35 #endif
36 #ifdef CONFIG_MPC5xxx
37 #include <mpc5xxx.h>
38 #endif
39 #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
40 #include <asm/mp.h>
41 #endif
42
43 #include <os.h>
44 #include <post.h>
45 #include <spi.h>
46 #include <status_led.h>
47 #include <trace.h>
48 #include <watchdog.h>
49 #include <asm/errno.h>
50 #include <asm/io.h>
51 #include <asm/sections.h>
52 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
53 #include <asm/init_helpers.h>
54 #include <asm/relocate.h>
55 #endif
56 #ifdef CONFIG_SANDBOX
57 #include <asm/state.h>
58 #endif
59 #include <dm/root.h>
60 #include <linux/compiler.h>
61
62 /*
63  * Pointer to initial global data area
64  *
65  * Here we initialize it if needed.
66  */
67 #ifdef XTRN_DECLARE_GLOBAL_DATA_PTR
68 #undef  XTRN_DECLARE_GLOBAL_DATA_PTR
69 #define XTRN_DECLARE_GLOBAL_DATA_PTR    /* empty = allocate here */
70 DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
71 #else
72 DECLARE_GLOBAL_DATA_PTR;
73 #endif
74
75 /*
76  * TODO(sjg@chromium.org): IMO this code should be
77  * refactored to a single function, something like:
78  *
79  * void led_set_state(enum led_colour_t colour, int on);
80  */
81 /************************************************************************
82  * Coloured LED functionality
83  ************************************************************************
84  * May be supplied by boards if desired
85  */
86 __weak void coloured_LED_init(void) {}
87 __weak void red_led_on(void) {}
88 __weak void red_led_off(void) {}
89 __weak void green_led_on(void) {}
90 __weak void green_led_off(void) {}
91 __weak void yellow_led_on(void) {}
92 __weak void yellow_led_off(void) {}
93 __weak void blue_led_on(void) {}
94 __weak void blue_led_off(void) {}
95
96 /*
97  * Why is gd allocated a register? Prior to reloc it might be better to
98  * just pass it around to each function in this file?
99  *
100  * After reloc one could argue that it is hardly used and doesn't need
101  * to be in a register. Or if it is it should perhaps hold pointers to all
102  * global data for all modules, so that post-reloc we can avoid the massive
103  * literal pool we get on ARM. Or perhaps just encourage each module to use
104  * a structure...
105  */
106
107 /*
108  * Could the CONFIG_SPL_BUILD infection become a flag in gd?
109  */
110
111 #if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
112 static int init_func_watchdog_init(void)
113 {
114 # if defined(CONFIG_HW_WATCHDOG) && (defined(CONFIG_BLACKFIN) || \
115         defined(CONFIG_M68K) || defined(CONFIG_MICROBLAZE) || \
116         defined(CONFIG_SH) || defined(CONFIG_AT91SAM9_WATCHDOG) || \
117         defined(CONFIG_IMX_WATCHDOG))
118         hw_watchdog_init();
119 # endif
120         puts("       Watchdog enabled\n");
121         WATCHDOG_RESET();
122
123         return 0;
124 }
125
126 int init_func_watchdog_reset(void)
127 {
128         WATCHDOG_RESET();
129
130         return 0;
131 }
132 #endif /* CONFIG_WATCHDOG */
133
134 __weak void board_add_ram_info(int use_default)
135 {
136         /* please define platform specific board_add_ram_info() */
137 }
138
139 static int init_baud_rate(void)
140 {
141         gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
142         return 0;
143 }
144
145 static int display_text_info(void)
146 {
147 #if !defined(CONFIG_SANDBOX) && !defined(CONFIG_EFI_APP)
148         ulong bss_start, bss_end, text_base;
149
150         bss_start = (ulong)&__bss_start;
151         bss_end = (ulong)&__bss_end;
152
153 #ifdef CONFIG_SYS_TEXT_BASE
154         text_base = CONFIG_SYS_TEXT_BASE;
155 #else
156         text_base = CONFIG_SYS_MONITOR_BASE;
157 #endif
158
159         debug("U-Boot code: %08lX -> %08lX  BSS: -> %08lX\n",
160                 text_base, bss_start, bss_end);
161 #endif
162
163 #ifdef CONFIG_MODEM_SUPPORT
164         debug("Modem Support enabled\n");
165 #endif
166 #ifdef CONFIG_USE_IRQ
167         debug("IRQ Stack: %08lx\n", IRQ_STACK_START);
168         debug("FIQ Stack: %08lx\n", FIQ_STACK_START);
169 #endif
170
171         return 0;
172 }
173
174 static int announce_dram_init(void)
175 {
176         puts("DRAM:  ");
177         return 0;
178 }
179
180 #if defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_M68K)
181 static int init_func_ram(void)
182 {
183 #ifdef  CONFIG_BOARD_TYPES
184         int board_type = gd->board_type;
185 #else
186         int board_type = 0;     /* use dummy arg */
187 #endif
188
189         gd->ram_size = initdram(board_type);
190
191         if (gd->ram_size > 0)
192                 return 0;
193
194         puts("*** failed ***\n");
195         return 1;
196 }
197 #endif
198
199 static int show_dram_config(void)
200 {
201         unsigned long long size;
202
203 #ifdef CONFIG_NR_DRAM_BANKS
204         int i;
205
206         debug("\nRAM Configuration:\n");
207         for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
208                 size += gd->bd->bi_dram[i].size;
209                 debug("Bank #%d: %llx ", i,
210                       (unsigned long long)(gd->bd->bi_dram[i].start));
211 #ifdef DEBUG
212                 print_size(gd->bd->bi_dram[i].size, "\n");
213 #endif
214         }
215         debug("\nDRAM:  ");
216 #else
217         size = gd->ram_size;
218 #endif
219
220         print_size(size, "");
221         board_add_ram_info(0);
222         putc('\n');
223
224         return 0;
225 }
226
227 __weak void dram_init_banksize(void)
228 {
229 #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
230         gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
231         gd->bd->bi_dram[0].size = get_effective_memsize();
232 #endif
233 }
234
235 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
236 static int init_func_i2c(void)
237 {
238         puts("I2C:   ");
239 #ifdef CONFIG_SYS_I2C
240         i2c_init_all();
241 #else
242         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
243 #endif
244         puts("ready\n");
245         return 0;
246 }
247 #endif
248
249 #if defined(CONFIG_HARD_SPI)
250 static int init_func_spi(void)
251 {
252         puts("SPI:   ");
253         spi_init();
254         puts("ready\n");
255         return 0;
256 }
257 #endif
258
259 __maybe_unused
260 static int zero_global_data(void)
261 {
262         memset((void *)gd, '\0', sizeof(gd_t));
263
264         return 0;
265 }
266
267 static int setup_mon_len(void)
268 {
269 #if defined(__ARM__) || defined(__MICROBLAZE__)
270         gd->mon_len = (ulong)&__bss_end - (ulong)_start;
271 #elif defined(CONFIG_SANDBOX) || defined(CONFIG_EFI_APP)
272         gd->mon_len = (ulong)&_end - (ulong)_init;
273 #elif defined(CONFIG_BLACKFIN) || defined(CONFIG_NIOS2)
274         gd->mon_len = CONFIG_SYS_MONITOR_LEN;
275 #else
276         /* TODO: use (ulong)&__bss_end - (ulong)&__text_start; ? */
277         gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
278 #endif
279         return 0;
280 }
281
282 __weak int arch_cpu_init(void)
283 {
284         return 0;
285 }
286
287 __weak unsigned long get_timer_masked(void)
288 {
289         return get_timer(0);
290 }
291
292 #ifdef CONFIG_SANDBOX
293 static int setup_ram_buf(void)
294 {
295         struct sandbox_state *state = state_get_current();
296
297         gd->arch.ram_buf = state->ram_buf;
298         gd->ram_size = state->ram_size;
299
300         return 0;
301 }
302 #endif
303
304 /* Get the top of usable RAM */
305 __weak ulong board_get_usable_ram_top(ulong total_size)
306 {
307 #ifdef CONFIG_SYS_SDRAM_BASE
308         /*
309          * Detect whether we have so much RAM that it goes past the end of our
310          * 32-bit address space. If so, clip the usable RAM so it doesn't.
311          */
312         if (gd->ram_top < CONFIG_SYS_SDRAM_BASE)
313                 /*
314                  * Will wrap back to top of 32-bit space when reservations
315                  * are made.
316                  */
317                 return 0;
318 #endif
319         return gd->ram_top;
320 }
321
322 static int setup_dest_addr(void)
323 {
324         debug("Monitor len: %08lX\n", gd->mon_len);
325         /*
326          * Ram is setup, size stored in gd !!
327          */
328         debug("Ram size: %08lX\n", (ulong)gd->ram_size);
329 #if defined(CONFIG_SYS_MEM_TOP_HIDE)
330         /*
331          * Subtract specified amount of memory to hide so that it won't
332          * get "touched" at all by U-Boot. By fixing up gd->ram_size
333          * the Linux kernel should now get passed the now "corrected"
334          * memory size and won't touch it either. This should work
335          * for arch/ppc and arch/powerpc. Only Linux board ports in
336          * arch/powerpc with bootwrapper support, that recalculate the
337          * memory size from the SDRAM controller setup will have to
338          * get fixed.
339          */
340         gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
341 #endif
342 #ifdef CONFIG_SYS_SDRAM_BASE
343         gd->ram_top = CONFIG_SYS_SDRAM_BASE;
344 #endif
345         gd->ram_top += get_effective_memsize();
346         gd->ram_top = board_get_usable_ram_top(gd->mon_len);
347         gd->relocaddr = gd->ram_top;
348         debug("Ram top: %08lX\n", (ulong)gd->ram_top);
349 #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
350         /*
351          * We need to make sure the location we intend to put secondary core
352          * boot code is reserved and not used by any part of u-boot
353          */
354         if (gd->relocaddr > determine_mp_bootpg(NULL)) {
355                 gd->relocaddr = determine_mp_bootpg(NULL);
356                 debug("Reserving MP boot page to %08lx\n", gd->relocaddr);
357         }
358 #endif
359         return 0;
360 }
361
362 #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
363 static int reserve_logbuffer(void)
364 {
365         /* reserve kernel log buffer */
366         gd->relocaddr -= LOGBUFF_RESERVE;
367         debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
368                 gd->relocaddr);
369         return 0;
370 }
371 #endif
372
373 #ifdef CONFIG_PRAM
374 /* reserve protected RAM */
375 static int reserve_pram(void)
376 {
377         ulong reg;
378
379         reg = getenv_ulong("pram", 10, CONFIG_PRAM);
380         gd->relocaddr -= (reg << 10);           /* size is in kB */
381         debug("Reserving %ldk for protected RAM at %08lx\n", reg,
382               gd->relocaddr);
383         return 0;
384 }
385 #endif /* CONFIG_PRAM */
386
387 /* Round memory pointer down to next 4 kB limit */
388 static int reserve_round_4k(void)
389 {
390         gd->relocaddr &= ~(4096 - 1);
391         return 0;
392 }
393
394 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
395                 defined(CONFIG_ARM)
396 static int reserve_mmu(void)
397 {
398         /* reserve TLB table */
399         gd->arch.tlb_size = PGTABLE_SIZE;
400         gd->relocaddr -= gd->arch.tlb_size;
401
402         /* round down to next 64 kB limit */
403         gd->relocaddr &= ~(0x10000 - 1);
404
405         gd->arch.tlb_addr = gd->relocaddr;
406         debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
407               gd->arch.tlb_addr + gd->arch.tlb_size);
408         return 0;
409 }
410 #endif
411
412 #ifdef CONFIG_LCD
413 static int reserve_lcd(void)
414 {
415 #ifdef CONFIG_FB_ADDR
416         gd->fb_base = CONFIG_FB_ADDR;
417 #else
418         /* reserve memory for LCD display (always full pages) */
419         gd->relocaddr = lcd_setmem(gd->relocaddr);
420         gd->fb_base = gd->relocaddr;
421 #endif /* CONFIG_FB_ADDR */
422         return 0;
423 }
424 #endif /* CONFIG_LCD */
425
426 static int reserve_trace(void)
427 {
428 #ifdef CONFIG_TRACE
429         gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
430         gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
431         debug("Reserving %dk for trace data at: %08lx\n",
432               CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
433 #endif
434
435         return 0;
436 }
437
438 #if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
439                 !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
440                 !defined(CONFIG_BLACKFIN) && !defined(CONFIG_M68K)
441 static int reserve_video(void)
442 {
443         /* reserve memory for video display (always full pages) */
444         gd->relocaddr = video_setmem(gd->relocaddr);
445         gd->fb_base = gd->relocaddr;
446
447         return 0;
448 }
449 #endif
450
451 static int reserve_uboot(void)
452 {
453         /*
454          * reserve memory for U-Boot code, data & bss
455          * round down to next 4 kB limit
456          */
457         gd->relocaddr -= gd->mon_len;
458         gd->relocaddr &= ~(4096 - 1);
459 #ifdef CONFIG_E500
460         /* round down to next 64 kB limit so that IVPR stays aligned */
461         gd->relocaddr &= ~(65536 - 1);
462 #endif
463
464         debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
465               gd->relocaddr);
466
467         gd->start_addr_sp = gd->relocaddr;
468
469         return 0;
470 }
471
472 #ifndef CONFIG_SPL_BUILD
473 /* reserve memory for malloc() area */
474 static int reserve_malloc(void)
475 {
476         gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
477         debug("Reserving %dk for malloc() at: %08lx\n",
478                         TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
479         return 0;
480 }
481
482 /* (permanently) allocate a Board Info struct */
483 static int reserve_board(void)
484 {
485         if (!gd->bd) {
486                 gd->start_addr_sp -= sizeof(bd_t);
487                 gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
488                 memset(gd->bd, '\0', sizeof(bd_t));
489                 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
490                       sizeof(bd_t), gd->start_addr_sp);
491         }
492         return 0;
493 }
494 #endif
495
496 static int setup_machine(void)
497 {
498 #ifdef CONFIG_MACH_TYPE
499         gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
500 #endif
501         return 0;
502 }
503
504 static int reserve_global_data(void)
505 {
506         gd->start_addr_sp -= sizeof(gd_t);
507         gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
508         debug("Reserving %zu Bytes for Global Data at: %08lx\n",
509                         sizeof(gd_t), gd->start_addr_sp);
510         return 0;
511 }
512
513 static int reserve_fdt(void)
514 {
515         /*
516          * If the device tree is sitting immediately above our image then we
517          * must relocate it. If it is embedded in the data section, then it
518          * will be relocated with other data.
519          */
520         if (gd->fdt_blob) {
521                 gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
522
523                 gd->start_addr_sp -= gd->fdt_size;
524                 gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
525                 debug("Reserving %lu Bytes for FDT at: %08lx\n",
526                       gd->fdt_size, gd->start_addr_sp);
527         }
528
529         return 0;
530 }
531
532 int arch_reserve_stacks(void)
533 {
534         return 0;
535 }
536
537 static int reserve_stacks(void)
538 {
539         /* make stack pointer 16-byte aligned */
540         gd->start_addr_sp -= 16;
541         gd->start_addr_sp &= ~0xf;
542
543         /*
544          * let the architecture-specific code tailor gd->start_addr_sp and
545          * gd->irq_sp
546          */
547         return arch_reserve_stacks();
548 }
549
550 static int display_new_sp(void)
551 {
552         debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
553
554         return 0;
555 }
556
557 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
558 static int setup_board_part1(void)
559 {
560         bd_t *bd = gd->bd;
561
562         /*
563          * Save local variables to board info struct
564          */
565         bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;        /* start of memory */
566         bd->bi_memsize = gd->ram_size;                  /* size in bytes */
567
568 #ifdef CONFIG_SYS_SRAM_BASE
569         bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;        /* start of SRAM */
570         bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;         /* size  of SRAM */
571 #endif
572
573 #if defined(CONFIG_8xx) || defined(CONFIG_MPC8260) || defined(CONFIG_5xx) || \
574                 defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
575         bd->bi_immr_base = CONFIG_SYS_IMMR;     /* base  of IMMR register     */
576 #endif
577 #if defined(CONFIG_MPC5xxx) || defined(CONFIG_M68K)
578         bd->bi_mbar_base = CONFIG_SYS_MBAR;     /* base of internal registers */
579 #endif
580 #if defined(CONFIG_MPC83xx)
581         bd->bi_immrbar = CONFIG_SYS_IMMR;
582 #endif
583
584         return 0;
585 }
586
587 static int setup_board_part2(void)
588 {
589         bd_t *bd = gd->bd;
590
591         bd->bi_intfreq = gd->cpu_clk;   /* Internal Freq, in Hz */
592         bd->bi_busfreq = gd->bus_clk;   /* Bus Freq,      in Hz */
593 #if defined(CONFIG_CPM2)
594         bd->bi_cpmfreq = gd->arch.cpm_clk;
595         bd->bi_brgfreq = gd->arch.brg_clk;
596         bd->bi_sccfreq = gd->arch.scc_clk;
597         bd->bi_vco = gd->arch.vco_out;
598 #endif /* CONFIG_CPM2 */
599 #if defined(CONFIG_MPC512X)
600         bd->bi_ipsfreq = gd->arch.ips_clk;
601 #endif /* CONFIG_MPC512X */
602 #if defined(CONFIG_MPC5xxx)
603         bd->bi_ipbfreq = gd->arch.ipb_clk;
604         bd->bi_pcifreq = gd->pci_clk;
605 #endif /* CONFIG_MPC5xxx */
606 #if defined(CONFIG_M68K) && defined(CONFIG_PCI)
607         bd->bi_pcifreq = gd->pci_clk;
608 #endif
609 #if defined(CONFIG_EXTRA_CLOCK)
610         bd->bi_inpfreq = gd->arch.inp_clk;      /* input Freq in Hz */
611         bd->bi_vcofreq = gd->arch.vco_clk;      /* vco Freq in Hz */
612         bd->bi_flbfreq = gd->arch.flb_clk;      /* flexbus Freq in Hz */
613 #endif
614
615         return 0;
616 }
617 #endif
618
619 #ifdef CONFIG_SYS_EXTBDINFO
620 static int setup_board_extra(void)
621 {
622         bd_t *bd = gd->bd;
623
624         strncpy((char *) bd->bi_s_version, "1.2", sizeof(bd->bi_s_version));
625         strncpy((char *) bd->bi_r_version, U_BOOT_VERSION,
626                 sizeof(bd->bi_r_version));
627
628         bd->bi_procfreq = gd->cpu_clk;  /* Processor Speed, In Hz */
629         bd->bi_plb_busfreq = gd->bus_clk;
630 #if defined(CONFIG_405GP) || defined(CONFIG_405EP) || \
631                 defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
632                 defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
633         bd->bi_pci_busfreq = get_PCI_freq();
634         bd->bi_opbfreq = get_OPB_freq();
635 #elif defined(CONFIG_XILINX_405)
636         bd->bi_pci_busfreq = get_PCI_freq();
637 #endif
638
639         return 0;
640 }
641 #endif
642
643 #ifdef CONFIG_POST
644 static int init_post(void)
645 {
646         post_bootmode_init();
647         post_run(NULL, POST_ROM | post_bootmode_get(0));
648
649         return 0;
650 }
651 #endif
652
653 static int setup_dram_config(void)
654 {
655         /* Ram is board specific, so move it to board code ... */
656         dram_init_banksize();
657
658         return 0;
659 }
660
661 static int reloc_fdt(void)
662 {
663         if (gd->flags & GD_FLG_SKIP_RELOC)
664                 return 0;
665         if (gd->new_fdt) {
666                 memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
667                 gd->fdt_blob = gd->new_fdt;
668         }
669
670         return 0;
671 }
672
673 static int setup_reloc(void)
674 {
675         if (gd->flags & GD_FLG_SKIP_RELOC) {
676                 debug("Skipping relocation due to flag\n");
677                 return 0;
678         }
679
680 #ifdef CONFIG_SYS_TEXT_BASE
681 #if defined(CONFIG_M68K)
682         /*
683          * On all ColdFire arch cpu, monitor code starts always
684          * just after the default vector table location, so at 0x400
685          */
686         gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
687 #elif defined(CONFIG_ARM)
688         gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
689 #else
690         gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
691 #endif
692 #endif
693         memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
694
695         debug("Relocation Offset is: %08lx\n", gd->reloc_off);
696         debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
697               gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
698               gd->start_addr_sp);
699
700         return 0;
701 }
702
703 /* ARM calls relocate_code from its crt0.S */
704 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
705
706 static int jump_to_copy(void)
707 {
708         if (gd->flags & GD_FLG_SKIP_RELOC)
709                 return 0;
710         /*
711          * x86 is special, but in a nice way. It uses a trampoline which
712          * enables the dcache if possible.
713          *
714          * For now, other archs use relocate_code(), which is implemented
715          * similarly for all archs. When we do generic relocation, hopefully
716          * we can make all archs enable the dcache prior to relocation.
717          */
718 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
719         /*
720          * SDRAM and console are now initialised. The final stack can now
721          * be setup in SDRAM. Code execution will continue in Flash, but
722          * with the stack in SDRAM and Global Data in temporary memory
723          * (CPU cache)
724          */
725         board_init_f_r_trampoline(gd->start_addr_sp);
726 #else
727         relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
728 #endif
729
730         return 0;
731 }
732 #endif
733
734 /* Record the board_init_f() bootstage (after arch_cpu_init()) */
735 static int mark_bootstage(void)
736 {
737         bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
738
739         return 0;
740 }
741
742 static int initf_dm(void)
743 {
744 #if defined(CONFIG_DM) && defined(CONFIG_SYS_MALLOC_F_LEN)
745         int ret;
746
747         ret = dm_init_and_scan(true);
748         if (ret)
749                 return ret;
750 #endif
751
752         return 0;
753 }
754
755 /* Architecture-specific memory reservation */
756 __weak int reserve_arch(void)
757 {
758         return 0;
759 }
760
761 __weak int arch_cpu_init_dm(void)
762 {
763         return 0;
764 }
765
766 static init_fnc_t init_sequence_f[] = {
767 #ifdef CONFIG_SANDBOX
768         setup_ram_buf,
769 #endif
770         setup_mon_len,
771 #ifdef CONFIG_OF_CONTROL
772         fdtdec_setup,
773 #endif
774 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
775         x86_fsp_init,
776 #endif
777 #ifdef CONFIG_TRACE
778         trace_early_init,
779 #endif
780         initf_malloc,
781 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
782         /* TODO: can this go into arch_cpu_init()? */
783         probecpu,
784 #endif
785         arch_cpu_init,          /* basic arch cpu dependent setup */
786         mark_bootstage,
787         initf_dm,
788         arch_cpu_init_dm,
789 #if defined(CONFIG_BOARD_EARLY_INIT_F)
790         board_early_init_f,
791 #endif
792         /* TODO: can any of this go into arch_cpu_init()? */
793 #if defined(CONFIG_PPC) && !defined(CONFIG_8xx_CPUCLK_DEFAULT)
794         get_clocks,             /* get CPU and bus clocks (etc.) */
795 #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
796                 && !defined(CONFIG_TQM885D)
797         adjust_sdram_tbs_8xx,
798 #endif
799         /* TODO: can we rename this to timer_init()? */
800         init_timebase,
801 #endif
802 #if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || defined(CONFIG_BLACKFIN)
803         timer_init,             /* initialize timer */
804 #endif
805 #ifdef CONFIG_SYS_ALLOC_DPRAM
806 #if !defined(CONFIG_CPM2)
807         dpram_init,
808 #endif
809 #endif
810 #if defined(CONFIG_BOARD_POSTCLK_INIT)
811         board_postclk_init,
812 #endif
813 #ifdef CONFIG_FSL_ESDHC
814         get_clocks,
815 #endif
816 #ifdef CONFIG_M68K
817         get_clocks,
818 #endif
819         env_init,               /* initialize environment */
820 #if defined(CONFIG_8xx_CPUCLK_DEFAULT)
821         /* get CPU and bus clocks according to the environment variable */
822         get_clocks_866,
823         /* adjust sdram refresh rate according to the new clock */
824         sdram_adjust_866,
825         init_timebase,
826 #endif
827         init_baud_rate,         /* initialze baudrate settings */
828         serial_init,            /* serial communications setup */
829         console_init_f,         /* stage 1 init of console */
830 #ifdef CONFIG_SANDBOX
831         sandbox_early_getopt_check,
832 #endif
833 #ifdef CONFIG_OF_CONTROL
834         fdtdec_prepare_fdt,
835 #endif
836         display_options,        /* say that we are here */
837         display_text_info,      /* show debugging info if required */
838 #if defined(CONFIG_MPC8260)
839         prt_8260_rsr,
840         prt_8260_clks,
841 #endif /* CONFIG_MPC8260 */
842 #if defined(CONFIG_MPC83xx)
843         prt_83xx_rsr,
844 #endif
845 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
846         checkcpu,
847 #endif
848         print_cpuinfo,          /* display cpu info (and speed) */
849 #if defined(CONFIG_MPC5xxx)
850         prt_mpc5xxx_clks,
851 #endif /* CONFIG_MPC5xxx */
852 #if defined(CONFIG_DISPLAY_BOARDINFO)
853         show_board_info,
854 #endif
855         INIT_FUNC_WATCHDOG_INIT
856 #if defined(CONFIG_MISC_INIT_F)
857         misc_init_f,
858 #endif
859         INIT_FUNC_WATCHDOG_RESET
860 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
861         init_func_i2c,
862 #endif
863 #if defined(CONFIG_HARD_SPI)
864         init_func_spi,
865 #endif
866         announce_dram_init,
867         /* TODO: unify all these dram functions? */
868 #if defined(CONFIG_ARM) || defined(CONFIG_X86) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_AVR32)
869         dram_init,              /* configure available RAM banks */
870 #endif
871 #if defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_M68K)
872         init_func_ram,
873 #endif
874 #ifdef CONFIG_POST
875         post_init_f,
876 #endif
877         INIT_FUNC_WATCHDOG_RESET
878 #if defined(CONFIG_SYS_DRAM_TEST)
879         testdram,
880 #endif /* CONFIG_SYS_DRAM_TEST */
881         INIT_FUNC_WATCHDOG_RESET
882
883 #ifdef CONFIG_POST
884         init_post,
885 #endif
886         INIT_FUNC_WATCHDOG_RESET
887         /*
888          * Now that we have DRAM mapped and working, we can
889          * relocate the code and continue running from DRAM.
890          *
891          * Reserve memory at end of RAM for (top down in that order):
892          *  - area that won't get touched by U-Boot and Linux (optional)
893          *  - kernel log buffer
894          *  - protected RAM
895          *  - LCD framebuffer
896          *  - monitor code
897          *  - board info struct
898          */
899         setup_dest_addr,
900 #if defined(CONFIG_BLACKFIN) || defined(CONFIG_NIOS2)
901         /* Blackfin u-boot monitor should be on top of the ram */
902         reserve_uboot,
903 #endif
904 #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
905         reserve_logbuffer,
906 #endif
907 #ifdef CONFIG_PRAM
908         reserve_pram,
909 #endif
910         reserve_round_4k,
911 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
912                 defined(CONFIG_ARM)
913         reserve_mmu,
914 #endif
915 #ifdef CONFIG_LCD
916         reserve_lcd,
917 #endif
918         reserve_trace,
919         /* TODO: Why the dependency on CONFIG_8xx? */
920 #if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
921                 !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
922                 !defined(CONFIG_BLACKFIN) && !defined(CONFIG_M68K)
923         reserve_video,
924 #endif
925 #if !defined(CONFIG_BLACKFIN) && !defined(CONFIG_NIOS2)
926         reserve_uboot,
927 #endif
928 #ifndef CONFIG_SPL_BUILD
929         reserve_malloc,
930         reserve_board,
931 #endif
932         setup_machine,
933         reserve_global_data,
934         reserve_fdt,
935         reserve_arch,
936         reserve_stacks,
937         setup_dram_config,
938         show_dram_config,
939 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
940         setup_board_part1,
941         INIT_FUNC_WATCHDOG_RESET
942         setup_board_part2,
943 #endif
944         display_new_sp,
945 #ifdef CONFIG_SYS_EXTBDINFO
946         setup_board_extra,
947 #endif
948         INIT_FUNC_WATCHDOG_RESET
949         reloc_fdt,
950         setup_reloc,
951 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
952         copy_uboot_to_ram,
953         clear_bss,
954         do_elf_reloc_fixups,
955 #endif
956 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
957         jump_to_copy,
958 #endif
959         NULL,
960 };
961
962 void board_init_f(ulong boot_flags)
963 {
964 #ifdef CONFIG_SYS_GENERIC_GLOBAL_DATA
965         /*
966          * For some archtectures, global data is initialized and used before
967          * calling this function. The data should be preserved. For others,
968          * CONFIG_SYS_GENERIC_GLOBAL_DATA should be defined and use the stack
969          * here to host global data until relocation.
970          */
971         gd_t data;
972
973         gd = &data;
974
975         /*
976          * Clear global data before it is accessed at debug print
977          * in initcall_run_list. Otherwise the debug print probably
978          * get the wrong vaule of gd->have_console.
979          */
980         zero_global_data();
981 #endif
982
983         gd->flags = boot_flags;
984         gd->have_console = 0;
985
986         if (initcall_run_list(init_sequence_f))
987                 hang();
988
989 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
990                 !defined(CONFIG_EFI_APP)
991         /* NOTREACHED - jump_to_copy() does not return */
992         hang();
993 #endif
994 }
995
996 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
997 /*
998  * For now this code is only used on x86.
999  *
1000  * init_sequence_f_r is the list of init functions which are run when
1001  * U-Boot is executing from Flash with a semi-limited 'C' environment.
1002  * The following limitations must be considered when implementing an
1003  * '_f_r' function:
1004  *  - 'static' variables are read-only
1005  *  - Global Data (gd->xxx) is read/write
1006  *
1007  * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
1008  * supported).  It _should_, if possible, copy global data to RAM and
1009  * initialise the CPU caches (to speed up the relocation process)
1010  *
1011  * NOTE: At present only x86 uses this route, but it is intended that
1012  * all archs will move to this when generic relocation is implemented.
1013  */
1014 static init_fnc_t init_sequence_f_r[] = {
1015         init_cache_f_r,
1016
1017         NULL,
1018 };
1019
1020 void board_init_f_r(void)
1021 {
1022         if (initcall_run_list(init_sequence_f_r))
1023                 hang();
1024
1025         /*
1026          * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
1027          * Transfer execution from Flash to RAM by calculating the address
1028          * of the in-RAM copy of board_init_r() and calling it
1029          */
1030         (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
1031
1032         /* NOTREACHED - board_init_r() does not return */
1033         hang();
1034 }
1035 #endif /* CONFIG_X86 */
1036
1037 #ifndef CONFIG_X86
1038 ulong board_init_f_mem(ulong top)
1039 {
1040         /* Leave space for the stack we are running with now */
1041         top -= 0x40;
1042
1043         top -= sizeof(struct global_data);
1044         top = ALIGN(top, 16);
1045         gd = (struct global_data *)top;
1046         memset((void *)gd, '\0', sizeof(*gd));
1047
1048 #ifdef CONFIG_SYS_MALLOC_F_LEN
1049         top -= CONFIG_SYS_MALLOC_F_LEN;
1050         gd->malloc_base = top;
1051 #endif
1052
1053         return top;
1054 }
1055 #endif /* !CONFIG_X86 */