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