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