]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - common/board_f.c
fdt: sandbox: Move setup code from board_f to fdtdec
[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 <mapmem.h>
27
28 /* TODO: Can we move these into arch/ headers? */
29 #ifdef CONFIG_8xx
30 #include <mpc8xx.h>
31 #endif
32 #ifdef CONFIG_5xx
33 #include <mpc5xx.h>
34 #endif
35 #ifdef CONFIG_MPC5xxx
36 #include <mpc5xxx.h>
37 #endif
38 #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
39 #include <asm/mp.h>
40 #endif
41
42 #include <os.h>
43 #include <post.h>
44 #include <spi.h>
45 #include <status_led.h>
46 #include <trace.h>
47 #include <watchdog.h>
48 #include <asm/errno.h>
49 #include <asm/io.h>
50 #include <asm/sections.h>
51 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
52 #include <asm/init_helpers.h>
53 #include <asm/relocate.h>
54 #endif
55 #ifdef CONFIG_SANDBOX
56 #include <asm/state.h>
57 #endif
58 #include <dm/root.h>
59 #include <linux/compiler.h>
60
61 /*
62  * Pointer to initial global data area
63  *
64  * Here we initialize it if needed.
65  */
66 #ifdef XTRN_DECLARE_GLOBAL_DATA_PTR
67 #undef  XTRN_DECLARE_GLOBAL_DATA_PTR
68 #define XTRN_DECLARE_GLOBAL_DATA_PTR    /* empty = allocate here */
69 DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
70 #else
71 DECLARE_GLOBAL_DATA_PTR;
72 #endif
73
74 /*
75  * sjg: IMO this code should be
76  * refactored to a single function, something like:
77  *
78  * void led_set_state(enum led_colour_t colour, int on);
79  */
80 /************************************************************************
81  * Coloured LED functionality
82  ************************************************************************
83  * May be supplied by boards if desired
84  */
85 __weak void coloured_LED_init(void) {}
86 __weak void red_led_on(void) {}
87 __weak void red_led_off(void) {}
88 __weak void green_led_on(void) {}
89 __weak void green_led_off(void) {}
90 __weak void yellow_led_on(void) {}
91 __weak void yellow_led_off(void) {}
92 __weak void blue_led_on(void) {}
93 __weak void blue_led_off(void) {}
94
95 /*
96  * Why is gd allocated a register? Prior to reloc it might be better to
97  * just pass it around to each function in this file?
98  *
99  * After reloc one could argue that it is hardly used and doesn't need
100  * to be in a register. Or if it is it should perhaps hold pointers to all
101  * global data for all modules, so that post-reloc we can avoid the massive
102  * literal pool we get on ARM. Or perhaps just encourage each module to use
103  * a structure...
104  */
105
106 /*
107  * Could the CONFIG_SPL_BUILD infection become a flag in gd?
108  */
109
110 #if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
111 static int init_func_watchdog_init(void)
112 {
113 # if defined(CONFIG_HW_WATCHDOG) && (defined(CONFIG_BLACKFIN) || \
114         defined(CONFIG_M68K) || defined(CONFIG_MICROBLAZE) || \
115         defined(CONFIG_SH) || defined(CONFIG_AT91SAM9_WATCHDOG) || \
116         defined(CONFIG_IMX_WATCHDOG))
117         hw_watchdog_init();
118 # endif
119         puts("       Watchdog enabled\n");
120         WATCHDOG_RESET();
121
122         return 0;
123 }
124
125 int init_func_watchdog_reset(void)
126 {
127         WATCHDOG_RESET();
128
129         return 0;
130 }
131 #endif /* CONFIG_WATCHDOG */
132
133 __weak void board_add_ram_info(int use_default)
134 {
135         /* please define platform specific board_add_ram_info() */
136 }
137
138 static int init_baud_rate(void)
139 {
140         gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
141         return 0;
142 }
143
144 static int display_text_info(void)
145 {
146 #ifndef CONFIG_SANDBOX
147         ulong bss_start, bss_end, text_base;
148
149         bss_start = (ulong)&__bss_start;
150         bss_end = (ulong)&__bss_end;
151
152 #ifdef CONFIG_SYS_TEXT_BASE
153         text_base = CONFIG_SYS_TEXT_BASE;
154 #else
155         text_base = CONFIG_SYS_MONITOR_BASE;
156 #endif
157
158         debug("U-Boot code: %08lX -> %08lX  BSS: -> %08lX\n",
159                 text_base, bss_start, bss_end);
160 #endif
161
162 #ifdef CONFIG_MODEM_SUPPORT
163         debug("Modem Support enabled\n");
164 #endif
165 #ifdef CONFIG_USE_IRQ
166         debug("IRQ Stack: %08lx\n", IRQ_STACK_START);
167         debug("FIQ Stack: %08lx\n", FIQ_STACK_START);
168 #endif
169
170         return 0;
171 }
172
173 static int announce_dram_init(void)
174 {
175         puts("DRAM:  ");
176         return 0;
177 }
178
179 #if defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_M68K)
180 static int init_func_ram(void)
181 {
182 #ifdef  CONFIG_BOARD_TYPES
183         int board_type = gd->board_type;
184 #else
185         int board_type = 0;     /* use dummy arg */
186 #endif
187
188         gd->ram_size = initdram(board_type);
189
190         if (gd->ram_size > 0)
191                 return 0;
192
193         puts("*** failed ***\n");
194         return 1;
195 }
196 #endif
197
198 static int show_dram_config(void)
199 {
200         unsigned long long size;
201
202 #ifdef CONFIG_NR_DRAM_BANKS
203         int i;
204
205         debug("\nRAM Configuration:\n");
206         for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
207                 size += gd->bd->bi_dram[i].size;
208                 debug("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
209 #ifdef DEBUG
210                 print_size(gd->bd->bi_dram[i].size, "\n");
211 #endif
212         }
213         debug("\nDRAM:  ");
214 #else
215         size = gd->ram_size;
216 #endif
217
218         print_size(size, "");
219         board_add_ram_info(0);
220         putc('\n');
221
222         return 0;
223 }
224
225 __weak void dram_init_banksize(void)
226 {
227 #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
228         gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
229         gd->bd->bi_dram[0].size = get_effective_memsize();
230 #endif
231 }
232
233 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
234 static int init_func_i2c(void)
235 {
236         puts("I2C:   ");
237 #ifdef CONFIG_SYS_I2C
238         i2c_init_all();
239 #else
240         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
241 #endif
242         puts("ready\n");
243         return 0;
244 }
245 #endif
246
247 #if defined(CONFIG_HARD_SPI)
248 static int init_func_spi(void)
249 {
250         puts("SPI:   ");
251         spi_init();
252         puts("ready\n");
253         return 0;
254 }
255 #endif
256
257 __maybe_unused
258 static int zero_global_data(void)
259 {
260         memset((void *)gd, '\0', sizeof(gd_t));
261
262         return 0;
263 }
264
265 static int setup_mon_len(void)
266 {
267 #if defined(__ARM__) || defined(__MICROBLAZE__)
268         gd->mon_len = (ulong)&__bss_end - (ulong)_start;
269 #elif defined(CONFIG_SANDBOX)
270         gd->mon_len = (ulong)&_end - (ulong)_init;
271 #elif defined(CONFIG_BLACKFIN) || defined(CONFIG_NIOS2)
272         gd->mon_len = CONFIG_SYS_MONITOR_LEN;
273 #else
274         /* TODO: use (ulong)&__bss_end - (ulong)&__text_start; ? */
275         gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
276 #endif
277         return 0;
278 }
279
280 __weak int arch_cpu_init(void)
281 {
282         return 0;
283 }
284
285 __weak unsigned long get_timer_masked(void)
286 {
287         return get_timer(0);
288 }
289
290 #ifdef CONFIG_SANDBOX
291 static int setup_ram_buf(void)
292 {
293         struct sandbox_state *state = state_get_current();
294
295         gd->arch.ram_buf = state->ram_buf;
296         gd->ram_size = state->ram_size;
297
298         return 0;
299 }
300 #endif
301
302 /* Get the top of usable RAM */
303 __weak ulong board_get_usable_ram_top(ulong total_size)
304 {
305 #ifdef CONFIG_SYS_SDRAM_BASE
306         /*
307          * Detect whether we have so much RAM it goes past the end of our
308          * 32-bit address space. If so, clip the usable RAM so it doesn't.
309          */
310         if (gd->ram_top < CONFIG_SYS_SDRAM_BASE)
311                 /*
312                  * Will wrap back to top of 32-bit space when reservations
313                  * are made.
314                  */
315                 return 0;
316 #endif
317         return gd->ram_top;
318 }
319
320 static int setup_dest_addr(void)
321 {
322         debug("Monitor len: %08lX\n", gd->mon_len);
323         /*
324          * Ram is setup, size stored in gd !!
325          */
326         debug("Ram size: %08lX\n", (ulong)gd->ram_size);
327 #if defined(CONFIG_SYS_MEM_TOP_HIDE)
328         /*
329          * Subtract specified amount of memory to hide so that it won't
330          * get "touched" at all by U-Boot. By fixing up gd->ram_size
331          * the Linux kernel should now get passed the now "corrected"
332          * memory size and won't touch it either. This should work
333          * for arch/ppc and arch/powerpc. Only Linux board ports in
334          * arch/powerpc with bootwrapper support, that recalculate the
335          * memory size from the SDRAM controller setup will have to
336          * get fixed.
337          */
338         gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
339 #endif
340 #ifdef CONFIG_SYS_SDRAM_BASE
341         gd->ram_top = CONFIG_SYS_SDRAM_BASE;
342 #endif
343         gd->ram_top += get_effective_memsize();
344         gd->ram_top = board_get_usable_ram_top(gd->mon_len);
345         gd->relocaddr = gd->ram_top;
346         debug("Ram top: %08lX\n", (ulong)gd->ram_top);
347 #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
348         /*
349          * We need to make sure the location we intend to put secondary core
350          * boot code is reserved and not used by any part of u-boot
351          */
352         if (gd->relocaddr > determine_mp_bootpg(NULL)) {
353                 gd->relocaddr = determine_mp_bootpg(NULL);
354                 debug("Reserving MP boot page to %08lx\n", gd->relocaddr);
355         }
356 #endif
357         return 0;
358 }
359
360 #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
361 static int reserve_logbuffer(void)
362 {
363         /* reserve kernel log buffer */
364         gd->relocaddr -= LOGBUFF_RESERVE;
365         debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
366                 gd->relocaddr);
367         return 0;
368 }
369 #endif
370
371 #ifdef CONFIG_PRAM
372 /* reserve protected RAM */
373 static int reserve_pram(void)
374 {
375         ulong reg;
376
377         reg = getenv_ulong("pram", 10, CONFIG_PRAM);
378         gd->relocaddr -= (reg << 10);           /* size is in kB */
379         debug("Reserving %ldk for protected RAM at %08lx\n", reg,
380               gd->relocaddr);
381         return 0;
382 }
383 #endif /* CONFIG_PRAM */
384
385 /* Round memory pointer down to next 4 kB limit */
386 static int reserve_round_4k(void)
387 {
388         gd->relocaddr &= ~(4096 - 1);
389         return 0;
390 }
391
392 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
393                 defined(CONFIG_ARM)
394 static int reserve_mmu(void)
395 {
396         /* reserve TLB table */
397         gd->arch.tlb_size = PGTABLE_SIZE;
398         gd->relocaddr -= gd->arch.tlb_size;
399
400         /* round down to next 64 kB limit */
401         gd->relocaddr &= ~(0x10000 - 1);
402
403         gd->arch.tlb_addr = gd->relocaddr;
404         debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
405               gd->arch.tlb_addr + gd->arch.tlb_size);
406         return 0;
407 }
408 #endif
409
410 #ifdef CONFIG_LCD
411 static int reserve_lcd(void)
412 {
413 #ifdef CONFIG_FB_ADDR
414         gd->fb_base = CONFIG_FB_ADDR;
415 #else
416         /* reserve memory for LCD display (always full pages) */
417         gd->relocaddr = lcd_setmem(gd->relocaddr);
418         gd->fb_base = gd->relocaddr;
419 #endif /* CONFIG_FB_ADDR */
420         return 0;
421 }
422 #endif /* CONFIG_LCD */
423
424 static int reserve_trace(void)
425 {
426 #ifdef CONFIG_TRACE
427         gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
428         gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
429         debug("Reserving %dk for trace data at: %08lx\n",
430               CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
431 #endif
432
433         return 0;
434 }
435
436 #if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
437                 !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
438                 !defined(CONFIG_BLACKFIN) && !defined(CONFIG_M68K)
439 static int reserve_video(void)
440 {
441         /* reserve memory for video display (always full pages) */
442         gd->relocaddr = video_setmem(gd->relocaddr);
443         gd->fb_base = gd->relocaddr;
444
445         return 0;
446 }
447 #endif
448
449 static int reserve_uboot(void)
450 {
451         /*
452          * reserve memory for U-Boot code, data & bss
453          * round down to next 4 kB limit
454          */
455         gd->relocaddr -= gd->mon_len;
456         gd->relocaddr &= ~(4096 - 1);
457 #ifdef CONFIG_E500
458         /* round down to next 64 kB limit so that IVPR stays aligned */
459         gd->relocaddr &= ~(65536 - 1);
460 #endif
461
462         debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
463               gd->relocaddr);
464
465         gd->start_addr_sp = gd->relocaddr;
466
467         return 0;
468 }
469
470 #ifndef CONFIG_SPL_BUILD
471 /* reserve memory for malloc() area */
472 static int reserve_malloc(void)
473 {
474         gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
475         debug("Reserving %dk for malloc() at: %08lx\n",
476                         TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
477         return 0;
478 }
479
480 /* (permanently) allocate a Board Info struct */
481 static int reserve_board(void)
482 {
483         if (!gd->bd) {
484                 gd->start_addr_sp -= sizeof(bd_t);
485                 gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
486                 memset(gd->bd, '\0', sizeof(bd_t));
487                 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
488                       sizeof(bd_t), gd->start_addr_sp);
489         }
490         return 0;
491 }
492 #endif
493
494 static int setup_machine(void)
495 {
496 #ifdef CONFIG_MACH_TYPE
497         gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
498 #endif
499         return 0;
500 }
501
502 static int reserve_global_data(void)
503 {
504         gd->start_addr_sp -= sizeof(gd_t);
505         gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
506         debug("Reserving %zu Bytes for Global Data at: %08lx\n",
507                         sizeof(gd_t), gd->start_addr_sp);
508         return 0;
509 }
510
511 static int reserve_fdt(void)
512 {
513         /*
514          * If the device tree is sitting immediate above our image then we
515          * must relocate it. If it is embedded in the data section, then it
516          * will be relocated with other data.
517          */
518         if (gd->fdt_blob) {
519                 gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
520
521                 gd->start_addr_sp -= gd->fdt_size;
522                 gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
523                 debug("Reserving %lu Bytes for FDT at: %08lx\n",
524                       gd->fdt_size, gd->start_addr_sp);
525         }
526
527         return 0;
528 }
529
530 int arch_reserve_stacks(void)
531 {
532         return 0;
533 }
534
535 static int reserve_stacks(void)
536 {
537         /* make stack pointer 16-byte aligned */
538         gd->start_addr_sp -= 16;
539         gd->start_addr_sp &= ~0xf;
540
541         /*
542          * let the architecture specific code tailor gd->start_addr_sp and
543          * gd->irq_sp
544          */
545         return arch_reserve_stacks();
546 }
547
548 static int display_new_sp(void)
549 {
550         debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
551
552         return 0;
553 }
554
555 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
556 static int setup_board_part1(void)
557 {
558         bd_t *bd = gd->bd;
559
560         /*
561          * Save local variables to board info struct
562          */
563
564         bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;        /* start of memory */
565         bd->bi_memsize = gd->ram_size;                  /* size in bytes */
566
567 #ifdef CONFIG_SYS_SRAM_BASE
568         bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;        /* start of SRAM */
569         bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;         /* size  of SRAM */
570 #endif
571
572 #if defined(CONFIG_8xx) || defined(CONFIG_MPC8260) || defined(CONFIG_5xx) || \
573                 defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
574         bd->bi_immr_base = CONFIG_SYS_IMMR;     /* base  of IMMR register     */
575 #endif
576 #if defined(CONFIG_MPC5xxx) || defined(CONFIG_M68K)
577         bd->bi_mbar_base = CONFIG_SYS_MBAR;     /* base of internal registers */
578 #endif
579 #if defined(CONFIG_MPC83xx)
580         bd->bi_immrbar = CONFIG_SYS_IMMR;
581 #endif
582
583         return 0;
584 }
585
586 static int setup_board_part2(void)
587 {
588         bd_t *bd = gd->bd;
589
590         bd->bi_intfreq = gd->cpu_clk;   /* Internal Freq, in Hz */
591         bd->bi_busfreq = gd->bus_clk;   /* Bus Freq,      in Hz */
592 #if defined(CONFIG_CPM2)
593         bd->bi_cpmfreq = gd->arch.cpm_clk;
594         bd->bi_brgfreq = gd->arch.brg_clk;
595         bd->bi_sccfreq = gd->arch.scc_clk;
596         bd->bi_vco = gd->arch.vco_out;
597 #endif /* CONFIG_CPM2 */
598 #if defined(CONFIG_MPC512X)
599         bd->bi_ipsfreq = gd->arch.ips_clk;
600 #endif /* CONFIG_MPC512X */
601 #if defined(CONFIG_MPC5xxx)
602         bd->bi_ipbfreq = gd->arch.ipb_clk;
603         bd->bi_pcifreq = gd->pci_clk;
604 #endif /* CONFIG_MPC5xxx */
605 #if defined(CONFIG_M68K) && defined(CONFIG_PCI)
606         bd->bi_pcifreq = gd->pci_clk;
607 #endif
608 #if defined(CONFIG_EXTRA_CLOCK)
609         bd->bi_inpfreq = gd->arch.inp_clk;      /* input Freq in Hz */
610         bd->bi_vcofreq = gd->arch.vco_clk;      /* vco Freq in Hz */
611         bd->bi_flbfreq = gd->arch.flb_clk;      /* flexbus Freq in Hz */
612 #endif
613
614         return 0;
615 }
616 #endif
617
618 #ifdef CONFIG_SYS_EXTBDINFO
619 static int setup_board_extra(void)
620 {
621         bd_t *bd = gd->bd;
622
623         strncpy((char *) bd->bi_s_version, "1.2", sizeof(bd->bi_s_version));
624         strncpy((char *) bd->bi_r_version, U_BOOT_VERSION,
625                 sizeof(bd->bi_r_version));
626
627         bd->bi_procfreq = gd->cpu_clk;  /* Processor Speed, In Hz */
628         bd->bi_plb_busfreq = gd->bus_clk;
629 #if defined(CONFIG_405GP) || defined(CONFIG_405EP) || \
630                 defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
631                 defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
632         bd->bi_pci_busfreq = get_PCI_freq();
633         bd->bi_opbfreq = get_OPB_freq();
634 #elif defined(CONFIG_XILINX_405)
635         bd->bi_pci_busfreq = get_PCI_freq();
636 #endif
637
638         return 0;
639 }
640 #endif
641
642 #ifdef CONFIG_POST
643 static int init_post(void)
644 {
645         post_bootmode_init();
646         post_run(NULL, POST_ROM | post_bootmode_get(0));
647
648         return 0;
649 }
650 #endif
651
652 static int setup_dram_config(void)
653 {
654         /* Ram is board specific, so move it to board code ... */
655         dram_init_banksize();
656
657         return 0;
658 }
659
660 static int reloc_fdt(void)
661 {
662         if (gd->new_fdt) {
663                 memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
664                 gd->fdt_blob = gd->new_fdt;
665         }
666
667         return 0;
668 }
669
670 static int setup_reloc(void)
671 {
672 #ifdef CONFIG_SYS_TEXT_BASE
673 #if defined(CONFIG_M68K)
674         /*
675          * On all ColdFire arch cpu, monitor code starts always
676          * just after the default vector table location, so at 0x400
677          */
678         gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
679 #elif defined(CONFIG_ARM)
680         gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
681 #else
682         gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
683 #endif
684 #endif
685         memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
686
687         debug("Relocation Offset is: %08lx\n", gd->reloc_off);
688         debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
689               gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
690               gd->start_addr_sp);
691
692         return 0;
693 }
694
695 /* ARM calls relocate_code from its crt0.S */
696 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
697
698 static int jump_to_copy(void)
699 {
700         /*
701          * x86 is special, but in a nice way. It uses a trampoline which
702          * enables the dcache if possible.
703          *
704          * For now, other archs use relocate_code(), which is implemented
705          * similarly for all archs. When we do generic relocation, hopefully
706          * we can make all archs enable the dcache prior to relocation.
707          */
708 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
709         /*
710          * SDRAM and console are now initialised. The final stack can now
711          * be setup in SDRAM. Code execution will continue in Flash, but
712          * with the stack in SDRAM and Global Data in temporary memory
713          * (CPU cache)
714          */
715         board_init_f_r_trampoline(gd->start_addr_sp);
716 #else
717         relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
718 #endif
719
720         return 0;
721 }
722 #endif
723
724 /* Record the board_init_f() bootstage (after arch_cpu_init()) */
725 static int mark_bootstage(void)
726 {
727         bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
728
729         return 0;
730 }
731
732 static int initf_malloc(void)
733 {
734 #ifdef CONFIG_SYS_MALLOC_F_LEN
735         assert(gd->malloc_base);        /* Set up by crt0.S */
736         gd->malloc_limit = gd->malloc_base + CONFIG_SYS_MALLOC_F_LEN;
737         gd->malloc_ptr = 0;
738 #endif
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         setup_fdt,
774 #endif
775 #ifdef CONFIG_TRACE
776         trace_early_init,
777 #endif
778         initf_malloc,
779 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
780         /* TODO: can this go into arch_cpu_init()? */
781         probecpu,
782 #endif
783         arch_cpu_init,          /* basic arch cpu dependent setup */
784         mark_bootstage,
785 #ifdef CONFIG_OF_CONTROL
786         fdtdec_check_fdt,
787 #endif
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         /* 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 */