]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - common/board_f.c
x86: Switch to using generic global_data setup
[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->start_addr_sp &= ~0xf;
508         gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
509         debug("Reserving %zu Bytes for Global Data at: %08lx\n",
510                         sizeof(gd_t), gd->start_addr_sp);
511         return 0;
512 }
513
514 static int reserve_fdt(void)
515 {
516         /*
517          * If the device tree is sitting immediately above our image then we
518          * must relocate it. If it is embedded in the data section, then it
519          * will be relocated with other data.
520          */
521         if (gd->fdt_blob) {
522                 gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
523
524                 gd->start_addr_sp -= gd->fdt_size;
525                 gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
526                 debug("Reserving %lu Bytes for FDT at: %08lx\n",
527                       gd->fdt_size, gd->start_addr_sp);
528         }
529
530         return 0;
531 }
532
533 int arch_reserve_stacks(void)
534 {
535         return 0;
536 }
537
538 static int reserve_stacks(void)
539 {
540         /* make stack pointer 16-byte aligned */
541         gd->start_addr_sp -= 16;
542         gd->start_addr_sp &= ~0xf;
543
544         /*
545          * let the architecture-specific code tailor gd->start_addr_sp and
546          * gd->irq_sp
547          */
548         return arch_reserve_stacks();
549 }
550
551 static int display_new_sp(void)
552 {
553         debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
554
555         return 0;
556 }
557
558 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
559 static int setup_board_part1(void)
560 {
561         bd_t *bd = gd->bd;
562
563         /*
564          * Save local variables to board info struct
565          */
566         bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;        /* start of memory */
567         bd->bi_memsize = gd->ram_size;                  /* size in bytes */
568
569 #ifdef CONFIG_SYS_SRAM_BASE
570         bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;        /* start of SRAM */
571         bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;         /* size  of SRAM */
572 #endif
573
574 #if defined(CONFIG_8xx) || defined(CONFIG_MPC8260) || defined(CONFIG_5xx) || \
575                 defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
576         bd->bi_immr_base = CONFIG_SYS_IMMR;     /* base  of IMMR register     */
577 #endif
578 #if defined(CONFIG_MPC5xxx) || defined(CONFIG_M68K)
579         bd->bi_mbar_base = CONFIG_SYS_MBAR;     /* base of internal registers */
580 #endif
581 #if defined(CONFIG_MPC83xx)
582         bd->bi_immrbar = CONFIG_SYS_IMMR;
583 #endif
584
585         return 0;
586 }
587
588 static int setup_board_part2(void)
589 {
590         bd_t *bd = gd->bd;
591
592         bd->bi_intfreq = gd->cpu_clk;   /* Internal Freq, in Hz */
593         bd->bi_busfreq = gd->bus_clk;   /* Bus Freq,      in Hz */
594 #if defined(CONFIG_CPM2)
595         bd->bi_cpmfreq = gd->arch.cpm_clk;
596         bd->bi_brgfreq = gd->arch.brg_clk;
597         bd->bi_sccfreq = gd->arch.scc_clk;
598         bd->bi_vco = gd->arch.vco_out;
599 #endif /* CONFIG_CPM2 */
600 #if defined(CONFIG_MPC512X)
601         bd->bi_ipsfreq = gd->arch.ips_clk;
602 #endif /* CONFIG_MPC512X */
603 #if defined(CONFIG_MPC5xxx)
604         bd->bi_ipbfreq = gd->arch.ipb_clk;
605         bd->bi_pcifreq = gd->pci_clk;
606 #endif /* CONFIG_MPC5xxx */
607 #if defined(CONFIG_M68K) && defined(CONFIG_PCI)
608         bd->bi_pcifreq = gd->pci_clk;
609 #endif
610 #if defined(CONFIG_EXTRA_CLOCK)
611         bd->bi_inpfreq = gd->arch.inp_clk;      /* input Freq in Hz */
612         bd->bi_vcofreq = gd->arch.vco_clk;      /* vco Freq in Hz */
613         bd->bi_flbfreq = gd->arch.flb_clk;      /* flexbus Freq in Hz */
614 #endif
615
616         return 0;
617 }
618 #endif
619
620 #ifdef CONFIG_SYS_EXTBDINFO
621 static int setup_board_extra(void)
622 {
623         bd_t *bd = gd->bd;
624
625         strncpy((char *) bd->bi_s_version, "1.2", sizeof(bd->bi_s_version));
626         strncpy((char *) bd->bi_r_version, U_BOOT_VERSION,
627                 sizeof(bd->bi_r_version));
628
629         bd->bi_procfreq = gd->cpu_clk;  /* Processor Speed, In Hz */
630         bd->bi_plb_busfreq = gd->bus_clk;
631 #if defined(CONFIG_405GP) || defined(CONFIG_405EP) || \
632                 defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
633                 defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
634         bd->bi_pci_busfreq = get_PCI_freq();
635         bd->bi_opbfreq = get_OPB_freq();
636 #elif defined(CONFIG_XILINX_405)
637         bd->bi_pci_busfreq = get_PCI_freq();
638 #endif
639
640         return 0;
641 }
642 #endif
643
644 #ifdef CONFIG_POST
645 static int init_post(void)
646 {
647         post_bootmode_init();
648         post_run(NULL, POST_ROM | post_bootmode_get(0));
649
650         return 0;
651 }
652 #endif
653
654 static int setup_dram_config(void)
655 {
656         /* Ram is board specific, so move it to board code ... */
657         dram_init_banksize();
658
659         return 0;
660 }
661
662 static int reloc_fdt(void)
663 {
664         if (gd->flags & GD_FLG_SKIP_RELOC)
665                 return 0;
666         if (gd->new_fdt) {
667                 memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
668                 gd->fdt_blob = gd->new_fdt;
669         }
670
671         return 0;
672 }
673
674 static int setup_reloc(void)
675 {
676         if (gd->flags & GD_FLG_SKIP_RELOC) {
677                 debug("Skipping relocation due to flag\n");
678                 return 0;
679         }
680
681 #ifdef CONFIG_SYS_TEXT_BASE
682 #if defined(CONFIG_M68K)
683         /*
684          * On all ColdFire arch cpu, monitor code starts always
685          * just after the default vector table location, so at 0x400
686          */
687         gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
688 #elif defined(CONFIG_ARM)
689         gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
690 #else
691         gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
692 #endif
693 #endif
694         memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
695
696         debug("Relocation Offset is: %08lx\n", gd->reloc_off);
697         debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
698               gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
699               gd->start_addr_sp);
700
701         return 0;
702 }
703
704 /* ARM calls relocate_code from its crt0.S */
705 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
706
707 static int jump_to_copy(void)
708 {
709         if (gd->flags & GD_FLG_SKIP_RELOC)
710                 return 0;
711         /*
712          * x86 is special, but in a nice way. It uses a trampoline which
713          * enables the dcache if possible.
714          *
715          * For now, other archs use relocate_code(), which is implemented
716          * similarly for all archs. When we do generic relocation, hopefully
717          * we can make all archs enable the dcache prior to relocation.
718          */
719 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
720         /*
721          * SDRAM and console are now initialised. The final stack can now
722          * be setup in SDRAM. Code execution will continue in Flash, but
723          * with the stack in SDRAM and Global Data in temporary memory
724          * (CPU cache)
725          */
726         arch_setup_gd(gd->new_gd);
727         board_init_f_r_trampoline(gd->start_addr_sp);
728 #else
729         relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
730 #endif
731
732         return 0;
733 }
734 #endif
735
736 /* Record the board_init_f() bootstage (after arch_cpu_init()) */
737 static int mark_bootstage(void)
738 {
739         bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
740
741         return 0;
742 }
743
744 static int initf_dm(void)
745 {
746 #if defined(CONFIG_DM) && defined(CONFIG_SYS_MALLOC_F_LEN)
747         int ret;
748
749         ret = dm_init_and_scan(true);
750         if (ret)
751                 return ret;
752 #endif
753
754         return 0;
755 }
756
757 /* Architecture-specific memory reservation */
758 __weak int reserve_arch(void)
759 {
760         return 0;
761 }
762
763 __weak int arch_cpu_init_dm(void)
764 {
765         return 0;
766 }
767
768 static init_fnc_t init_sequence_f[] = {
769 #ifdef CONFIG_SANDBOX
770         setup_ram_buf,
771 #endif
772         setup_mon_len,
773 #ifdef CONFIG_OF_CONTROL
774         fdtdec_setup,
775 #endif
776 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
777         x86_fsp_init,
778 #endif
779 #ifdef CONFIG_TRACE
780         trace_early_init,
781 #endif
782         initf_malloc,
783 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
784         /* TODO: can this go into arch_cpu_init()? */
785         probecpu,
786 #endif
787         arch_cpu_init,          /* basic arch cpu dependent setup */
788         mark_bootstage,
789         initf_dm,
790         arch_cpu_init_dm,
791 #if defined(CONFIG_BOARD_EARLY_INIT_F)
792         board_early_init_f,
793 #endif
794         /* TODO: can any of this go into arch_cpu_init()? */
795 #if defined(CONFIG_PPC) && !defined(CONFIG_8xx_CPUCLK_DEFAULT)
796         get_clocks,             /* get CPU and bus clocks (etc.) */
797 #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
798                 && !defined(CONFIG_TQM885D)
799         adjust_sdram_tbs_8xx,
800 #endif
801         /* TODO: can we rename this to timer_init()? */
802         init_timebase,
803 #endif
804 #if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || defined(CONFIG_BLACKFIN)
805         timer_init,             /* initialize timer */
806 #endif
807 #ifdef CONFIG_SYS_ALLOC_DPRAM
808 #if !defined(CONFIG_CPM2)
809         dpram_init,
810 #endif
811 #endif
812 #if defined(CONFIG_BOARD_POSTCLK_INIT)
813         board_postclk_init,
814 #endif
815 #ifdef CONFIG_FSL_ESDHC
816         get_clocks,
817 #endif
818 #ifdef CONFIG_M68K
819         get_clocks,
820 #endif
821         env_init,               /* initialize environment */
822 #if defined(CONFIG_8xx_CPUCLK_DEFAULT)
823         /* get CPU and bus clocks according to the environment variable */
824         get_clocks_866,
825         /* adjust sdram refresh rate according to the new clock */
826         sdram_adjust_866,
827         init_timebase,
828 #endif
829         init_baud_rate,         /* initialze baudrate settings */
830         serial_init,            /* serial communications setup */
831         console_init_f,         /* stage 1 init of console */
832 #ifdef CONFIG_SANDBOX
833         sandbox_early_getopt_check,
834 #endif
835 #ifdef CONFIG_OF_CONTROL
836         fdtdec_prepare_fdt,
837 #endif
838         display_options,        /* say that we are here */
839         display_text_info,      /* show debugging info if required */
840 #if defined(CONFIG_MPC8260)
841         prt_8260_rsr,
842         prt_8260_clks,
843 #endif /* CONFIG_MPC8260 */
844 #if defined(CONFIG_MPC83xx)
845         prt_83xx_rsr,
846 #endif
847 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
848         checkcpu,
849 #endif
850         print_cpuinfo,          /* display cpu info (and speed) */
851 #if defined(CONFIG_MPC5xxx)
852         prt_mpc5xxx_clks,
853 #endif /* CONFIG_MPC5xxx */
854 #if defined(CONFIG_DISPLAY_BOARDINFO)
855         show_board_info,
856 #endif
857         INIT_FUNC_WATCHDOG_INIT
858 #if defined(CONFIG_MISC_INIT_F)
859         misc_init_f,
860 #endif
861         INIT_FUNC_WATCHDOG_RESET
862 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
863         init_func_i2c,
864 #endif
865 #if defined(CONFIG_HARD_SPI)
866         init_func_spi,
867 #endif
868         announce_dram_init,
869         /* TODO: unify all these dram functions? */
870 #if defined(CONFIG_ARM) || defined(CONFIG_X86) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_AVR32)
871         dram_init,              /* configure available RAM banks */
872 #endif
873 #if defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_M68K)
874         init_func_ram,
875 #endif
876 #ifdef CONFIG_POST
877         post_init_f,
878 #endif
879         INIT_FUNC_WATCHDOG_RESET
880 #if defined(CONFIG_SYS_DRAM_TEST)
881         testdram,
882 #endif /* CONFIG_SYS_DRAM_TEST */
883         INIT_FUNC_WATCHDOG_RESET
884
885 #ifdef CONFIG_POST
886         init_post,
887 #endif
888         INIT_FUNC_WATCHDOG_RESET
889         /*
890          * Now that we have DRAM mapped and working, we can
891          * relocate the code and continue running from DRAM.
892          *
893          * Reserve memory at end of RAM for (top down in that order):
894          *  - area that won't get touched by U-Boot and Linux (optional)
895          *  - kernel log buffer
896          *  - protected RAM
897          *  - LCD framebuffer
898          *  - monitor code
899          *  - board info struct
900          */
901         setup_dest_addr,
902 #if defined(CONFIG_BLACKFIN) || defined(CONFIG_NIOS2)
903         /* Blackfin u-boot monitor should be on top of the ram */
904         reserve_uboot,
905 #endif
906 #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
907         reserve_logbuffer,
908 #endif
909 #ifdef CONFIG_PRAM
910         reserve_pram,
911 #endif
912         reserve_round_4k,
913 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
914                 defined(CONFIG_ARM)
915         reserve_mmu,
916 #endif
917 #ifdef CONFIG_LCD
918         reserve_lcd,
919 #endif
920         reserve_trace,
921         /* TODO: Why the dependency on CONFIG_8xx? */
922 #if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
923                 !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
924                 !defined(CONFIG_BLACKFIN) && !defined(CONFIG_M68K)
925         reserve_video,
926 #endif
927 #if !defined(CONFIG_BLACKFIN) && !defined(CONFIG_NIOS2)
928         reserve_uboot,
929 #endif
930 #ifndef CONFIG_SPL_BUILD
931         reserve_malloc,
932         reserve_board,
933 #endif
934         setup_machine,
935         reserve_global_data,
936         reserve_fdt,
937         reserve_arch,
938         reserve_stacks,
939         setup_dram_config,
940         show_dram_config,
941 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
942         setup_board_part1,
943         INIT_FUNC_WATCHDOG_RESET
944         setup_board_part2,
945 #endif
946         display_new_sp,
947 #ifdef CONFIG_SYS_EXTBDINFO
948         setup_board_extra,
949 #endif
950         INIT_FUNC_WATCHDOG_RESET
951         reloc_fdt,
952         setup_reloc,
953 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
954         copy_uboot_to_ram,
955         clear_bss,
956         do_elf_reloc_fixups,
957 #endif
958 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
959         jump_to_copy,
960 #endif
961         NULL,
962 };
963
964 void board_init_f(ulong boot_flags)
965 {
966 #ifdef CONFIG_SYS_GENERIC_GLOBAL_DATA
967         /*
968          * For some archtectures, global data is initialized and used before
969          * calling this function. The data should be preserved. For others,
970          * CONFIG_SYS_GENERIC_GLOBAL_DATA should be defined and use the stack
971          * here to host global data until relocation.
972          */
973         gd_t data;
974
975         gd = &data;
976
977         /*
978          * Clear global data before it is accessed at debug print
979          * in initcall_run_list. Otherwise the debug print probably
980          * get the wrong vaule of gd->have_console.
981          */
982         zero_global_data();
983 #endif
984
985         gd->flags = boot_flags;
986         gd->have_console = 0;
987
988         if (initcall_run_list(init_sequence_f))
989                 hang();
990
991 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
992                 !defined(CONFIG_EFI_APP)
993         /* NOTREACHED - jump_to_copy() does not return */
994         hang();
995 #endif
996 }
997
998 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
999 /*
1000  * For now this code is only used on x86.
1001  *
1002  * init_sequence_f_r is the list of init functions which are run when
1003  * U-Boot is executing from Flash with a semi-limited 'C' environment.
1004  * The following limitations must be considered when implementing an
1005  * '_f_r' function:
1006  *  - 'static' variables are read-only
1007  *  - Global Data (gd->xxx) is read/write
1008  *
1009  * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
1010  * supported).  It _should_, if possible, copy global data to RAM and
1011  * initialise the CPU caches (to speed up the relocation process)
1012  *
1013  * NOTE: At present only x86 uses this route, but it is intended that
1014  * all archs will move to this when generic relocation is implemented.
1015  */
1016 static init_fnc_t init_sequence_f_r[] = {
1017         init_cache_f_r,
1018
1019         NULL,
1020 };
1021
1022 void board_init_f_r(void)
1023 {
1024         if (initcall_run_list(init_sequence_f_r))
1025                 hang();
1026
1027         /*
1028          * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
1029          * Transfer execution from Flash to RAM by calculating the address
1030          * of the in-RAM copy of board_init_r() and calling it
1031          */
1032         (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
1033
1034         /* NOTREACHED - board_init_r() does not return */
1035         hang();
1036 }
1037 #endif /* CONFIG_X86 */
1038
1039 /* Unfortunately x86 can't compile this code as gd cannot be assigned */
1040 #ifndef CONFIG_X86
1041 __weak void arch_setup_gd(struct global_data *gd_ptr)
1042 {
1043         gd = gd_ptr;
1044 }
1045 #endif /* !CONFIG_X86 */
1046
1047 ulong board_init_f_mem(ulong top)
1048 {
1049         struct global_data *gd_ptr;
1050
1051         /* Leave space for the stack we are running with now */
1052         top -= 0x40;
1053
1054         top -= sizeof(struct global_data);
1055         top = ALIGN(top, 16);
1056         gd_ptr = (struct global_data *)top;
1057         memset(gd_ptr, '\0', sizeof(*gd));
1058         arch_setup_gd(gd_ptr);
1059
1060 #ifdef CONFIG_SYS_MALLOC_F_LEN
1061         top -= CONFIG_SYS_MALLOC_F_LEN;
1062         gd->malloc_base = top;
1063 #endif
1064
1065         return top;
1066 }