]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/board_f.c
arm: mx6: fix NFC clock get/set routines for i.MX6QP,i.MX6UL
[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 #elif defined(CONFIG_NDS32)
276         gd->mon_len = (ulong)(&__bss_end) - (ulong)(&_start);
277 #else
278         /* TODO: use (ulong)&__bss_end - (ulong)&__text_start; ? */
279         gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
280 #endif
281         return 0;
282 }
283
284 __weak int arch_cpu_init(void)
285 {
286         return 0;
287 }
288
289 __weak unsigned long get_timer_masked(void)
290 {
291         return get_timer(0);
292 }
293
294 #ifdef CONFIG_SANDBOX
295 static int setup_ram_buf(void)
296 {
297         struct sandbox_state *state = state_get_current();
298
299         gd->arch.ram_buf = state->ram_buf;
300         gd->ram_size = state->ram_size;
301
302         return 0;
303 }
304 #endif
305
306 /* Get the top of usable RAM */
307 __weak ulong board_get_usable_ram_top(ulong total_size)
308 {
309 #ifdef CONFIG_SYS_SDRAM_BASE
310         /*
311          * Detect whether we have so much RAM that it goes past the end of our
312          * 32-bit address space. If so, clip the usable RAM so it doesn't.
313          */
314         if (gd->ram_top < CONFIG_SYS_SDRAM_BASE)
315                 /*
316                  * Will wrap back to top of 32-bit space when reservations
317                  * are made.
318                  */
319                 return 0;
320 #endif
321         return gd->ram_top;
322 }
323
324 static int setup_dest_addr(void)
325 {
326         debug("Monitor len: %08lX\n", gd->mon_len);
327         /*
328          * Ram is setup, size stored in gd !!
329          */
330         debug("Ram size: %08lX\n", (ulong)gd->ram_size);
331 #if defined(CONFIG_SYS_MEM_TOP_HIDE)
332         /*
333          * Subtract specified amount of memory to hide so that it won't
334          * get "touched" at all by U-Boot. By fixing up gd->ram_size
335          * the Linux kernel should now get passed the now "corrected"
336          * memory size and won't touch it either. This should work
337          * for arch/ppc and arch/powerpc. Only Linux board ports in
338          * arch/powerpc with bootwrapper support, that recalculate the
339          * memory size from the SDRAM controller setup will have to
340          * get fixed.
341          */
342         gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
343 #endif
344 #ifdef CONFIG_SYS_SDRAM_BASE
345         gd->ram_top = CONFIG_SYS_SDRAM_BASE;
346 #endif
347         gd->ram_top += get_effective_memsize();
348         gd->ram_top = board_get_usable_ram_top(gd->mon_len);
349         gd->relocaddr = gd->ram_top;
350         debug("Ram top: %08lX\n", (ulong)gd->ram_top);
351 #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
352         /*
353          * We need to make sure the location we intend to put secondary core
354          * boot code is reserved and not used by any part of u-boot
355          */
356         if (gd->relocaddr > determine_mp_bootpg(NULL)) {
357                 gd->relocaddr = determine_mp_bootpg(NULL);
358                 debug("Reserving MP boot page to %08lx\n", gd->relocaddr);
359         }
360 #endif
361         return 0;
362 }
363
364 #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
365 static int reserve_logbuffer(void)
366 {
367         /* reserve kernel log buffer */
368         gd->relocaddr -= LOGBUFF_RESERVE;
369         debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
370                 gd->relocaddr);
371         return 0;
372 }
373 #endif
374
375 #ifdef CONFIG_PRAM
376 /* reserve protected RAM */
377 static int reserve_pram(void)
378 {
379         ulong reg;
380
381         reg = getenv_ulong("pram", 10, CONFIG_PRAM);
382         gd->relocaddr -= (reg << 10);           /* size is in kB */
383         debug("Reserving %ldk for protected RAM at %08lx\n", reg,
384               gd->relocaddr);
385         return 0;
386 }
387 #endif /* CONFIG_PRAM */
388
389 /* Round memory pointer down to next 4 kB limit */
390 static int reserve_round_4k(void)
391 {
392         gd->relocaddr &= ~(4096 - 1);
393         return 0;
394 }
395
396 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
397                 defined(CONFIG_ARM)
398 static int reserve_mmu(void)
399 {
400         /* reserve TLB table */
401         gd->arch.tlb_size = PGTABLE_SIZE;
402         gd->relocaddr -= gd->arch.tlb_size;
403
404         /* round down to next 64 kB limit */
405         gd->relocaddr &= ~(0x10000 - 1);
406
407         gd->arch.tlb_addr = gd->relocaddr;
408         debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
409               gd->arch.tlb_addr + gd->arch.tlb_size);
410         return 0;
411 }
412 #endif
413
414 #ifdef CONFIG_LCD
415 static int reserve_lcd(void)
416 {
417 #ifdef CONFIG_FB_ADDR
418         gd->fb_base = CONFIG_FB_ADDR;
419 #else
420         /* reserve memory for LCD display (always full pages) */
421         gd->relocaddr = lcd_setmem(gd->relocaddr);
422         gd->fb_base = gd->relocaddr;
423 #endif /* CONFIG_FB_ADDR */
424         return 0;
425 }
426 #endif /* CONFIG_LCD */
427
428 static int reserve_trace(void)
429 {
430 #ifdef CONFIG_TRACE
431         gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
432         gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
433         debug("Reserving %dk for trace data at: %08lx\n",
434               CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
435 #endif
436
437         return 0;
438 }
439
440 #if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
441                 !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
442                 !defined(CONFIG_BLACKFIN) && !defined(CONFIG_M68K)
443 static int reserve_video(void)
444 {
445         /* reserve memory for video display (always full pages) */
446         gd->relocaddr = video_setmem(gd->relocaddr);
447         gd->fb_base = gd->relocaddr;
448
449         return 0;
450 }
451 #endif
452
453 static int reserve_uboot(void)
454 {
455         /*
456          * reserve memory for U-Boot code, data & bss
457          * round down to next 4 kB limit
458          */
459         gd->relocaddr -= gd->mon_len;
460         gd->relocaddr &= ~(4096 - 1);
461 #ifdef CONFIG_E500
462         /* round down to next 64 kB limit so that IVPR stays aligned */
463         gd->relocaddr &= ~(65536 - 1);
464 #endif
465
466         debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
467               gd->relocaddr);
468
469         gd->start_addr_sp = gd->relocaddr;
470
471         return 0;
472 }
473
474 #ifndef CONFIG_SPL_BUILD
475 /* reserve memory for malloc() area */
476 static int reserve_malloc(void)
477 {
478         gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
479         debug("Reserving %dk for malloc() at: %08lx\n",
480                         TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
481         return 0;
482 }
483
484 /* (permanently) allocate a Board Info struct */
485 static int reserve_board(void)
486 {
487         if (!gd->bd) {
488                 gd->start_addr_sp -= sizeof(bd_t);
489                 gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
490                 memset(gd->bd, '\0', sizeof(bd_t));
491                 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
492                       sizeof(bd_t), gd->start_addr_sp);
493         }
494         return 0;
495 }
496 #endif
497
498 static int setup_machine(void)
499 {
500 #ifdef CONFIG_MACH_TYPE
501         gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
502 #endif
503         return 0;
504 }
505
506 static int reserve_global_data(void)
507 {
508         gd->start_addr_sp -= sizeof(gd_t);
509         gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
510         debug("Reserving %zu Bytes for Global Data at: %08lx\n",
511                         sizeof(gd_t), gd->start_addr_sp);
512         return 0;
513 }
514
515 static int reserve_fdt(void)
516 {
517         /*
518          * If the device tree is sitting immediately above our image then we
519          * must relocate it. If it is embedded in the data section, then it
520          * will be relocated with other data.
521          */
522         if (gd->fdt_blob) {
523                 gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
524
525                 gd->start_addr_sp -= gd->fdt_size;
526                 gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
527                 debug("Reserving %lu Bytes for FDT at: %08lx\n",
528                       gd->fdt_size, gd->start_addr_sp);
529         }
530
531         return 0;
532 }
533
534 int arch_reserve_stacks(void)
535 {
536         return 0;
537 }
538
539 static int reserve_stacks(void)
540 {
541         /* make stack pointer 16-byte aligned */
542         gd->start_addr_sp -= 16;
543         gd->start_addr_sp &= ~0xf;
544
545         /*
546          * let the architecture-specific code tailor gd->start_addr_sp and
547          * gd->irq_sp
548          */
549         return arch_reserve_stacks();
550 }
551
552 static int display_new_sp(void)
553 {
554         debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
555
556         return 0;
557 }
558
559 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
560 static int setup_board_part1(void)
561 {
562         bd_t *bd = gd->bd;
563
564         /*
565          * Save local variables to board info struct
566          */
567         bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;        /* start of memory */
568         bd->bi_memsize = gd->ram_size;                  /* size in bytes */
569
570 #ifdef CONFIG_SYS_SRAM_BASE
571         bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;        /* start of SRAM */
572         bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;         /* size  of SRAM */
573 #endif
574
575 #if defined(CONFIG_8xx) || defined(CONFIG_MPC8260) || defined(CONFIG_5xx) || \
576                 defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
577         bd->bi_immr_base = CONFIG_SYS_IMMR;     /* base  of IMMR register     */
578 #endif
579 #if defined(CONFIG_MPC5xxx) || defined(CONFIG_M68K)
580         bd->bi_mbar_base = CONFIG_SYS_MBAR;     /* base of internal registers */
581 #endif
582 #if defined(CONFIG_MPC83xx)
583         bd->bi_immrbar = CONFIG_SYS_IMMR;
584 #endif
585
586         return 0;
587 }
588
589 static int setup_board_part2(void)
590 {
591         bd_t *bd = gd->bd;
592
593         bd->bi_intfreq = gd->cpu_clk;   /* Internal Freq, in Hz */
594         bd->bi_busfreq = gd->bus_clk;   /* Bus Freq,      in Hz */
595 #if defined(CONFIG_CPM2)
596         bd->bi_cpmfreq = gd->arch.cpm_clk;
597         bd->bi_brgfreq = gd->arch.brg_clk;
598         bd->bi_sccfreq = gd->arch.scc_clk;
599         bd->bi_vco = gd->arch.vco_out;
600 #endif /* CONFIG_CPM2 */
601 #if defined(CONFIG_MPC512X)
602         bd->bi_ipsfreq = gd->arch.ips_clk;
603 #endif /* CONFIG_MPC512X */
604 #if defined(CONFIG_MPC5xxx)
605         bd->bi_ipbfreq = gd->arch.ipb_clk;
606         bd->bi_pcifreq = gd->pci_clk;
607 #endif /* CONFIG_MPC5xxx */
608 #if defined(CONFIG_M68K) && defined(CONFIG_PCI)
609         bd->bi_pcifreq = gd->pci_clk;
610 #endif
611 #if defined(CONFIG_EXTRA_CLOCK)
612         bd->bi_inpfreq = gd->arch.inp_clk;      /* input Freq in Hz */
613         bd->bi_vcofreq = gd->arch.vco_clk;      /* vco Freq in Hz */
614         bd->bi_flbfreq = gd->arch.flb_clk;      /* flexbus Freq in Hz */
615 #endif
616
617         return 0;
618 }
619 #endif
620
621 #ifdef CONFIG_SYS_EXTBDINFO
622 static int setup_board_extra(void)
623 {
624         bd_t *bd = gd->bd;
625
626         strncpy((char *) bd->bi_s_version, "1.2", sizeof(bd->bi_s_version));
627         strncpy((char *) bd->bi_r_version, U_BOOT_VERSION,
628                 sizeof(bd->bi_r_version));
629
630         bd->bi_procfreq = gd->cpu_clk;  /* Processor Speed, In Hz */
631         bd->bi_plb_busfreq = gd->bus_clk;
632 #if defined(CONFIG_405GP) || defined(CONFIG_405EP) || \
633                 defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
634                 defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
635         bd->bi_pci_busfreq = get_PCI_freq();
636         bd->bi_opbfreq = get_OPB_freq();
637 #elif defined(CONFIG_XILINX_405)
638         bd->bi_pci_busfreq = get_PCI_freq();
639 #endif
640
641         return 0;
642 }
643 #endif
644
645 #ifdef CONFIG_POST
646 static int init_post(void)
647 {
648         post_bootmode_init();
649         post_run(NULL, POST_ROM | post_bootmode_get(0));
650
651         return 0;
652 }
653 #endif
654
655 static int setup_dram_config(void)
656 {
657         /* Ram is board specific, so move it to board code ... */
658         dram_init_banksize();
659
660         return 0;
661 }
662
663 static int reloc_fdt(void)
664 {
665         if (gd->flags & GD_FLG_SKIP_RELOC)
666                 return 0;
667         if (gd->new_fdt) {
668                 memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
669                 gd->fdt_blob = gd->new_fdt;
670         }
671
672         return 0;
673 }
674
675 static int setup_reloc(void)
676 {
677         if (gd->flags & GD_FLG_SKIP_RELOC) {
678                 debug("Skipping relocation due to flag\n");
679                 return 0;
680         }
681
682 #ifdef CONFIG_SYS_TEXT_BASE
683 #if defined(CONFIG_M68K)
684         /*
685          * On all ColdFire arch cpu, monitor code starts always
686          * just after the default vector table location, so at 0x400
687          */
688         gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
689 #elif defined(CONFIG_ARM)
690         gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
691 #else
692         gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
693 #endif
694 #endif
695         memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
696
697         debug("Relocation Offset is: %08lx\n", gd->reloc_off);
698         debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
699               gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
700               gd->start_addr_sp);
701
702         return 0;
703 }
704
705 /* ARM calls relocate_code from its crt0.S */
706 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
707
708 static int jump_to_copy(void)
709 {
710         if (gd->flags & GD_FLG_SKIP_RELOC)
711                 return 0;
712         /*
713          * x86 is special, but in a nice way. It uses a trampoline which
714          * enables the dcache if possible.
715          *
716          * For now, other archs use relocate_code(), which is implemented
717          * similarly for all archs. When we do generic relocation, hopefully
718          * we can make all archs enable the dcache prior to relocation.
719          */
720 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
721         /*
722          * SDRAM and console are now initialised. The final stack can now
723          * be setup in SDRAM. Code execution will continue in Flash, but
724          * with the stack in SDRAM and Global Data in temporary memory
725          * (CPU cache)
726          */
727         arch_setup_gd(gd->new_gd);
728         board_init_f_r_trampoline(gd->start_addr_sp);
729 #else
730         relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
731 #endif
732
733         return 0;
734 }
735 #endif
736
737 /* Record the board_init_f() bootstage (after arch_cpu_init()) */
738 static int mark_bootstage(void)
739 {
740         bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
741
742         return 0;
743 }
744
745 static int initf_dm(void)
746 {
747 #if defined(CONFIG_DM) && defined(CONFIG_SYS_MALLOC_F_LEN)
748         int ret;
749
750         ret = dm_init_and_scan(true);
751         if (ret)
752                 return ret;
753 #endif
754
755         return 0;
756 }
757
758 /* Architecture-specific memory reservation */
759 __weak int reserve_arch(void)
760 {
761         return 0;
762 }
763
764 __weak int arch_cpu_init_dm(void)
765 {
766         return 0;
767 }
768
769 static init_fnc_t init_sequence_f[] = {
770 #ifdef CONFIG_SANDBOX
771         setup_ram_buf,
772 #endif
773         setup_mon_len,
774 #ifdef CONFIG_OF_CONTROL
775         fdtdec_setup,
776 #endif
777 #ifdef CONFIG_TRACE
778         trace_early_init,
779 #endif
780         initf_malloc,
781 #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
782         /* TODO: can this go into arch_cpu_init()? */
783         probecpu,
784 #endif
785 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
786         x86_fsp_init,
787 #endif
788         arch_cpu_init,          /* basic arch cpu dependent setup */
789         mark_bootstage,
790         initf_dm,
791         arch_cpu_init_dm,
792 #if defined(CONFIG_BOARD_EARLY_INIT_F)
793         board_early_init_f,
794 #endif
795         /* TODO: can any of this go into arch_cpu_init()? */
796 #if defined(CONFIG_PPC) && !defined(CONFIG_8xx_CPUCLK_DEFAULT)
797         get_clocks,             /* get CPU and bus clocks (etc.) */
798 #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
799                 && !defined(CONFIG_TQM885D)
800         adjust_sdram_tbs_8xx,
801 #endif
802         /* TODO: can we rename this to timer_init()? */
803         init_timebase,
804 #endif
805 #if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || \
806                 defined(CONFIG_BLACKFIN) || defined(CONFIG_NDS32)
807         timer_init,             /* initialize timer */
808 #endif
809 #ifdef CONFIG_SYS_ALLOC_DPRAM
810 #if !defined(CONFIG_CPM2)
811         dpram_init,
812 #endif
813 #endif
814 #if defined(CONFIG_BOARD_POSTCLK_INIT)
815         board_postclk_init,
816 #endif
817 #ifdef CONFIG_FSL_ESDHC
818         get_clocks,
819 #endif
820 #ifdef CONFIG_M68K
821         get_clocks,
822 #endif
823         env_init,               /* initialize environment */
824 #if defined(CONFIG_8xx_CPUCLK_DEFAULT)
825         /* get CPU and bus clocks according to the environment variable */
826         get_clocks_866,
827         /* adjust sdram refresh rate according to the new clock */
828         sdram_adjust_866,
829         init_timebase,
830 #endif
831         init_baud_rate,         /* initialze baudrate settings */
832         serial_init,            /* serial communications setup */
833         console_init_f,         /* stage 1 init of console */
834 #ifdef CONFIG_SANDBOX
835         sandbox_early_getopt_check,
836 #endif
837 #ifdef CONFIG_OF_CONTROL
838         fdtdec_prepare_fdt,
839 #endif
840         display_options,        /* say that we are here */
841         display_text_info,      /* show debugging info if required */
842 #if defined(CONFIG_MPC8260)
843         prt_8260_rsr,
844         prt_8260_clks,
845 #endif /* CONFIG_MPC8260 */
846 #if defined(CONFIG_MPC83xx)
847         prt_83xx_rsr,
848 #endif
849 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
850         checkcpu,
851 #endif
852         print_cpuinfo,          /* display cpu info (and speed) */
853 #if defined(CONFIG_MPC5xxx)
854         prt_mpc5xxx_clks,
855 #endif /* CONFIG_MPC5xxx */
856 #if defined(CONFIG_DISPLAY_BOARDINFO)
857         show_board_info,
858 #endif
859         INIT_FUNC_WATCHDOG_INIT
860 #if defined(CONFIG_MISC_INIT_F)
861         misc_init_f,
862 #endif
863         INIT_FUNC_WATCHDOG_RESET
864 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
865         init_func_i2c,
866 #endif
867 #if defined(CONFIG_HARD_SPI)
868         init_func_spi,
869 #endif
870         announce_dram_init,
871         /* TODO: unify all these dram functions? */
872 #if defined(CONFIG_ARM) || defined(CONFIG_X86) || defined(CONFIG_NDS32) || \
873                 defined(CONFIG_MICROBLAZE) || defined(CONFIG_AVR32)
874         dram_init,              /* configure available RAM banks */
875 #endif
876 #if defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_M68K)
877         init_func_ram,
878 #endif
879 #ifdef CONFIG_POST
880         post_init_f,
881 #endif
882         INIT_FUNC_WATCHDOG_RESET
883 #if defined(CONFIG_SYS_DRAM_TEST)
884         testdram,
885 #endif /* CONFIG_SYS_DRAM_TEST */
886         INIT_FUNC_WATCHDOG_RESET
887
888 #ifdef CONFIG_POST
889         init_post,
890 #endif
891         INIT_FUNC_WATCHDOG_RESET
892         /*
893          * Now that we have DRAM mapped and working, we can
894          * relocate the code and continue running from DRAM.
895          *
896          * Reserve memory at end of RAM for (top down in that order):
897          *  - area that won't get touched by U-Boot and Linux (optional)
898          *  - kernel log buffer
899          *  - protected RAM
900          *  - LCD framebuffer
901          *  - monitor code
902          *  - board info struct
903          */
904         setup_dest_addr,
905 #if defined(CONFIG_BLACKFIN) || defined(CONFIG_NIOS2)
906         /* Blackfin u-boot monitor should be on top of the ram */
907         reserve_uboot,
908 #endif
909 #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
910         reserve_logbuffer,
911 #endif
912 #ifdef CONFIG_PRAM
913         reserve_pram,
914 #endif
915         reserve_round_4k,
916 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
917                 defined(CONFIG_ARM)
918         reserve_mmu,
919 #endif
920 #ifdef CONFIG_LCD
921         reserve_lcd,
922 #endif
923         reserve_trace,
924         /* TODO: Why the dependency on CONFIG_8xx? */
925 #if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
926                 !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
927                 !defined(CONFIG_BLACKFIN) && !defined(CONFIG_M68K)
928         reserve_video,
929 #endif
930 #if !defined(CONFIG_BLACKFIN) && !defined(CONFIG_NIOS2)
931         reserve_uboot,
932 #endif
933 #ifndef CONFIG_SPL_BUILD
934         reserve_malloc,
935         reserve_board,
936 #endif
937         setup_machine,
938         reserve_global_data,
939         reserve_fdt,
940         reserve_arch,
941         reserve_stacks,
942         setup_dram_config,
943         show_dram_config,
944 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
945         setup_board_part1,
946         INIT_FUNC_WATCHDOG_RESET
947         setup_board_part2,
948 #endif
949         display_new_sp,
950 #ifdef CONFIG_SYS_EXTBDINFO
951         setup_board_extra,
952 #endif
953         INIT_FUNC_WATCHDOG_RESET
954         reloc_fdt,
955         setup_reloc,
956 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
957         copy_uboot_to_ram,
958         clear_bss,
959         do_elf_reloc_fixups,
960 #endif
961 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
962         jump_to_copy,
963 #endif
964         NULL,
965 };
966
967 void board_init_f(ulong boot_flags)
968 {
969 #ifdef CONFIG_SYS_GENERIC_GLOBAL_DATA
970         /*
971          * For some archtectures, global data is initialized and used before
972          * calling this function. The data should be preserved. For others,
973          * CONFIG_SYS_GENERIC_GLOBAL_DATA should be defined and use the stack
974          * here to host global data until relocation.
975          */
976         gd_t data;
977
978         gd = &data;
979
980         /*
981          * Clear global data before it is accessed at debug print
982          * in initcall_run_list. Otherwise the debug print probably
983          * get the wrong vaule of gd->have_console.
984          */
985         zero_global_data();
986 #endif
987
988         gd->flags = boot_flags;
989         gd->have_console = 0;
990
991         if (initcall_run_list(init_sequence_f))
992                 hang();
993
994 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
995                 !defined(CONFIG_EFI_APP)
996         /* NOTREACHED - jump_to_copy() does not return */
997         hang();
998 #endif
999 }
1000
1001 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
1002 /*
1003  * For now this code is only used on x86.
1004  *
1005  * init_sequence_f_r is the list of init functions which are run when
1006  * U-Boot is executing from Flash with a semi-limited 'C' environment.
1007  * The following limitations must be considered when implementing an
1008  * '_f_r' function:
1009  *  - 'static' variables are read-only
1010  *  - Global Data (gd->xxx) is read/write
1011  *
1012  * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
1013  * supported).  It _should_, if possible, copy global data to RAM and
1014  * initialise the CPU caches (to speed up the relocation process)
1015  *
1016  * NOTE: At present only x86 uses this route, but it is intended that
1017  * all archs will move to this when generic relocation is implemented.
1018  */
1019 static init_fnc_t init_sequence_f_r[] = {
1020         init_cache_f_r,
1021
1022         NULL,
1023 };
1024
1025 void board_init_f_r(void)
1026 {
1027         if (initcall_run_list(init_sequence_f_r))
1028                 hang();
1029
1030         /*
1031          * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
1032          * Transfer execution from Flash to RAM by calculating the address
1033          * of the in-RAM copy of board_init_r() and calling it
1034          */
1035         (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
1036
1037         /* NOTREACHED - board_init_r() does not return */
1038         hang();
1039 }
1040 #endif /* CONFIG_X86 */
1041
1042 /* Unfortunately x86 can't compile this code as gd cannot be assigned */
1043 #ifndef CONFIG_X86
1044 __weak void arch_setup_gd(struct global_data *gd_ptr)
1045 {
1046         gd = gd_ptr;
1047 }
1048 #endif /* !CONFIG_X86 */
1049
1050 ulong board_init_f_mem(ulong top)
1051 {
1052         struct global_data *gd_ptr;
1053
1054         /* Leave space for the stack we are running with now */
1055         top -= 0x40;
1056
1057         top -= sizeof(struct global_data);
1058         top = ALIGN(top, 16);
1059         gd_ptr = (struct global_data *)top;
1060         memset(gd_ptr, '\0', sizeof(*gd));
1061         arch_setup_gd(gd_ptr);
1062
1063 #ifdef CONFIG_SYS_MALLOC_F_LEN
1064         top -= CONFIG_SYS_MALLOC_F_LEN;
1065         gd->malloc_base = top;
1066 #endif
1067
1068         return top;
1069 }