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