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