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