]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/board_f.c
6a8cc3b52961e5a4d33bc263992b4a255e861272
[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         arch_setup_gd(gd->new_gd);
726         board_init_f_r_trampoline(gd->start_addr_sp);
727 #else
728         relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
729 #endif
730
731         return 0;
732 }
733 #endif
734
735 /* Record the board_init_f() bootstage (after arch_cpu_init()) */
736 static int mark_bootstage(void)
737 {
738         bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
739
740         return 0;
741 }
742
743 static int initf_dm(void)
744 {
745 #if defined(CONFIG_DM) && defined(CONFIG_SYS_MALLOC_F_LEN)
746         int ret;
747
748         ret = dm_init_and_scan(true);
749         if (ret)
750                 return ret;
751 #endif
752
753         return 0;
754 }
755
756 /* Architecture-specific memory reservation */
757 __weak int reserve_arch(void)
758 {
759         return 0;
760 }
761
762 __weak int arch_cpu_init_dm(void)
763 {
764         return 0;
765 }
766
767 static init_fnc_t init_sequence_f[] = {
768 #ifdef CONFIG_SANDBOX
769         setup_ram_buf,
770 #endif
771         setup_mon_len,
772 #ifdef CONFIG_OF_CONTROL
773         fdtdec_setup,
774 #endif
775 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
776         x86_fsp_init,
777 #endif
778 #ifdef CONFIG_TRACE
779         trace_early_init,
780 #endif
781         initf_malloc,
782 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
783         /* TODO: can this go into arch_cpu_init()? */
784         probecpu,
785 #endif
786         arch_cpu_init,          /* basic arch cpu dependent setup */
787         mark_bootstage,
788         initf_dm,
789         arch_cpu_init_dm,
790 #if defined(CONFIG_BOARD_EARLY_INIT_F)
791         board_early_init_f,
792 #endif
793         /* TODO: can any of this go into arch_cpu_init()? */
794 #if defined(CONFIG_PPC) && !defined(CONFIG_8xx_CPUCLK_DEFAULT)
795         get_clocks,             /* get CPU and bus clocks (etc.) */
796 #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
797                 && !defined(CONFIG_TQM885D)
798         adjust_sdram_tbs_8xx,
799 #endif
800         /* TODO: can we rename this to timer_init()? */
801         init_timebase,
802 #endif
803 #if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || defined(CONFIG_BLACKFIN)
804         timer_init,             /* initialize timer */
805 #endif
806 #ifdef CONFIG_SYS_ALLOC_DPRAM
807 #if !defined(CONFIG_CPM2)
808         dpram_init,
809 #endif
810 #endif
811 #if defined(CONFIG_BOARD_POSTCLK_INIT)
812         board_postclk_init,
813 #endif
814 #ifdef CONFIG_FSL_ESDHC
815         get_clocks,
816 #endif
817 #ifdef CONFIG_M68K
818         get_clocks,
819 #endif
820         env_init,               /* initialize environment */
821 #if defined(CONFIG_8xx_CPUCLK_DEFAULT)
822         /* get CPU and bus clocks according to the environment variable */
823         get_clocks_866,
824         /* adjust sdram refresh rate according to the new clock */
825         sdram_adjust_866,
826         init_timebase,
827 #endif
828         init_baud_rate,         /* initialze baudrate settings */
829         serial_init,            /* serial communications setup */
830         console_init_f,         /* stage 1 init of console */
831 #ifdef CONFIG_SANDBOX
832         sandbox_early_getopt_check,
833 #endif
834 #ifdef CONFIG_OF_CONTROL
835         fdtdec_prepare_fdt,
836 #endif
837         display_options,        /* say that we are here */
838         display_text_info,      /* show debugging info if required */
839 #if defined(CONFIG_MPC8260)
840         prt_8260_rsr,
841         prt_8260_clks,
842 #endif /* CONFIG_MPC8260 */
843 #if defined(CONFIG_MPC83xx)
844         prt_83xx_rsr,
845 #endif
846 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
847         checkcpu,
848 #endif
849         print_cpuinfo,          /* display cpu info (and speed) */
850 #if defined(CONFIG_MPC5xxx)
851         prt_mpc5xxx_clks,
852 #endif /* CONFIG_MPC5xxx */
853 #if defined(CONFIG_DISPLAY_BOARDINFO)
854         show_board_info,
855 #endif
856         INIT_FUNC_WATCHDOG_INIT
857 #if defined(CONFIG_MISC_INIT_F)
858         misc_init_f,
859 #endif
860         INIT_FUNC_WATCHDOG_RESET
861 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
862         init_func_i2c,
863 #endif
864 #if defined(CONFIG_HARD_SPI)
865         init_func_spi,
866 #endif
867         announce_dram_init,
868         /* TODO: unify all these dram functions? */
869 #if defined(CONFIG_ARM) || defined(CONFIG_X86) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_AVR32)
870         dram_init,              /* configure available RAM banks */
871 #endif
872 #if defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_M68K)
873         init_func_ram,
874 #endif
875 #ifdef CONFIG_POST
876         post_init_f,
877 #endif
878         INIT_FUNC_WATCHDOG_RESET
879 #if defined(CONFIG_SYS_DRAM_TEST)
880         testdram,
881 #endif /* CONFIG_SYS_DRAM_TEST */
882         INIT_FUNC_WATCHDOG_RESET
883
884 #ifdef CONFIG_POST
885         init_post,
886 #endif
887         INIT_FUNC_WATCHDOG_RESET
888         /*
889          * Now that we have DRAM mapped and working, we can
890          * relocate the code and continue running from DRAM.
891          *
892          * Reserve memory at end of RAM for (top down in that order):
893          *  - area that won't get touched by U-Boot and Linux (optional)
894          *  - kernel log buffer
895          *  - protected RAM
896          *  - LCD framebuffer
897          *  - monitor code
898          *  - board info struct
899          */
900         setup_dest_addr,
901 #if defined(CONFIG_BLACKFIN) || defined(CONFIG_NIOS2)
902         /* Blackfin u-boot monitor should be on top of the ram */
903         reserve_uboot,
904 #endif
905 #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
906         reserve_logbuffer,
907 #endif
908 #ifdef CONFIG_PRAM
909         reserve_pram,
910 #endif
911         reserve_round_4k,
912 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
913                 defined(CONFIG_ARM)
914         reserve_mmu,
915 #endif
916 #ifdef CONFIG_LCD
917         reserve_lcd,
918 #endif
919         reserve_trace,
920         /* TODO: Why the dependency on CONFIG_8xx? */
921 #if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
922                 !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
923                 !defined(CONFIG_BLACKFIN) && !defined(CONFIG_M68K)
924         reserve_video,
925 #endif
926 #if !defined(CONFIG_BLACKFIN) && !defined(CONFIG_NIOS2)
927         reserve_uboot,
928 #endif
929 #ifndef CONFIG_SPL_BUILD
930         reserve_malloc,
931         reserve_board,
932 #endif
933         setup_machine,
934         reserve_global_data,
935         reserve_fdt,
936         reserve_arch,
937         reserve_stacks,
938         setup_dram_config,
939         show_dram_config,
940 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
941         setup_board_part1,
942         INIT_FUNC_WATCHDOG_RESET
943         setup_board_part2,
944 #endif
945         display_new_sp,
946 #ifdef CONFIG_SYS_EXTBDINFO
947         setup_board_extra,
948 #endif
949         INIT_FUNC_WATCHDOG_RESET
950         reloc_fdt,
951         setup_reloc,
952 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
953         copy_uboot_to_ram,
954         clear_bss,
955         do_elf_reloc_fixups,
956 #endif
957 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
958         jump_to_copy,
959 #endif
960         NULL,
961 };
962
963 void board_init_f(ulong boot_flags)
964 {
965 #ifdef CONFIG_SYS_GENERIC_GLOBAL_DATA
966         /*
967          * For some archtectures, global data is initialized and used before
968          * calling this function. The data should be preserved. For others,
969          * CONFIG_SYS_GENERIC_GLOBAL_DATA should be defined and use the stack
970          * here to host global data until relocation.
971          */
972         gd_t data;
973
974         gd = &data;
975
976         /*
977          * Clear global data before it is accessed at debug print
978          * in initcall_run_list. Otherwise the debug print probably
979          * get the wrong vaule of gd->have_console.
980          */
981         zero_global_data();
982 #endif
983
984         gd->flags = boot_flags;
985         gd->have_console = 0;
986
987         if (initcall_run_list(init_sequence_f))
988                 hang();
989
990 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
991                 !defined(CONFIG_EFI_APP)
992         /* NOTREACHED - jump_to_copy() does not return */
993         hang();
994 #endif
995 }
996
997 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
998 /*
999  * For now this code is only used on x86.
1000  *
1001  * init_sequence_f_r is the list of init functions which are run when
1002  * U-Boot is executing from Flash with a semi-limited 'C' environment.
1003  * The following limitations must be considered when implementing an
1004  * '_f_r' function:
1005  *  - 'static' variables are read-only
1006  *  - Global Data (gd->xxx) is read/write
1007  *
1008  * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
1009  * supported).  It _should_, if possible, copy global data to RAM and
1010  * initialise the CPU caches (to speed up the relocation process)
1011  *
1012  * NOTE: At present only x86 uses this route, but it is intended that
1013  * all archs will move to this when generic relocation is implemented.
1014  */
1015 static init_fnc_t init_sequence_f_r[] = {
1016         init_cache_f_r,
1017
1018         NULL,
1019 };
1020
1021 void board_init_f_r(void)
1022 {
1023         if (initcall_run_list(init_sequence_f_r))
1024                 hang();
1025
1026         /*
1027          * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
1028          * Transfer execution from Flash to RAM by calculating the address
1029          * of the in-RAM copy of board_init_r() and calling it
1030          */
1031         (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
1032
1033         /* NOTREACHED - board_init_r() does not return */
1034         hang();
1035 }
1036 #endif /* CONFIG_X86 */
1037
1038 /* Unfortunately x86 can't compile this code as gd cannot be assigned */
1039 #ifndef CONFIG_X86
1040 __weak void arch_setup_gd(struct global_data *gd_ptr)
1041 {
1042         gd = gd_ptr;
1043 }
1044 #endif /* !CONFIG_X86 */
1045
1046 ulong board_init_f_mem(ulong top)
1047 {
1048         struct global_data *gd_ptr;
1049
1050         /* Leave space for the stack we are running with now */
1051         top -= 0x40;
1052
1053         top -= sizeof(struct global_data);
1054         top = ALIGN(top, 16);
1055         gd_ptr = (struct global_data *)top;
1056         memset(gd_ptr, '\0', sizeof(*gd));
1057         arch_setup_gd(gd_ptr);
1058
1059 #ifdef CONFIG_SYS_MALLOC_F_LEN
1060         top -= CONFIG_SYS_MALLOC_F_LEN;
1061         gd->malloc_base = top;
1062 #endif
1063
1064         return top;
1065 }