ed3cf6afbe08f8ba9de2a2c7a2bf371ba7a3279f
[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 #elif defined(CONFIG_VIDEO) && \
430                 (!defined(CONFIG_PPC)) && \
431                 !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
432                 !defined(CONFIG_M68K)
433         /* reserve memory for video display (always full pages) */
434         gd->relocaddr = video_setmem(gd->relocaddr);
435         gd->fb_base = gd->relocaddr;
436 #endif
437
438         return 0;
439 }
440
441 static int reserve_trace(void)
442 {
443 #ifdef CONFIG_TRACE
444         gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
445         gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
446         debug("Reserving %luk for trace data at: %08lx\n",
447               (unsigned long)CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
448 #endif
449
450         return 0;
451 }
452
453 static int reserve_uboot(void)
454 {
455         if (!(gd->flags & GD_FLG_SKIP_RELOC)) {
456                 /*
457                  * reserve memory for U-Boot code, data & bss
458                  * round down to next 4 kB limit
459                  */
460                 gd->relocaddr -= gd->mon_len;
461                 gd->relocaddr &= ~(4096 - 1);
462         #if defined(CONFIG_E500) || defined(CONFIG_MIPS)
463                 /* round down to next 64 kB limit so that IVPR stays aligned */
464                 gd->relocaddr &= ~(65536 - 1);
465         #endif
466
467                 debug("Reserving %ldk for U-Boot at: %08lx\n",
468                       gd->mon_len >> 10, gd->relocaddr);
469         }
470
471         gd->start_addr_sp = gd->relocaddr;
472
473         return 0;
474 }
475
476 /* reserve memory for malloc() area */
477 static int reserve_malloc(void)
478 {
479         gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
480         debug("Reserving %dk for malloc() at: %08lx\n",
481               TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
482         return 0;
483 }
484
485 /* (permanently) allocate a Board Info struct */
486 static int reserve_board(void)
487 {
488         if (!gd->bd) {
489                 gd->start_addr_sp -= sizeof(bd_t);
490                 gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
491                 memset(gd->bd, '\0', sizeof(bd_t));
492                 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
493                       sizeof(bd_t), gd->start_addr_sp);
494         }
495         return 0;
496 }
497
498 static int setup_machine(void)
499 {
500 #ifdef CONFIG_MACH_TYPE
501         gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
502 #endif
503         return 0;
504 }
505
506 static int reserve_global_data(void)
507 {
508         gd->start_addr_sp -= sizeof(gd_t);
509         gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
510         debug("Reserving %zu Bytes for Global Data at: %08lx\n",
511               sizeof(gd_t), gd->start_addr_sp);
512         return 0;
513 }
514
515 static int reserve_fdt(void)
516 {
517 #ifndef CONFIG_OF_EMBED
518         /*
519          * If the device tree is sitting immediately above our image then we
520          * must relocate it. If it is embedded in the data section, then it
521          * will be relocated with other data.
522          */
523         if (gd->fdt_blob) {
524                 gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
525
526                 gd->start_addr_sp -= gd->fdt_size;
527                 gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
528                 debug("Reserving %lu Bytes for FDT at: %08lx\n",
529                       gd->fdt_size, gd->start_addr_sp);
530         }
531 #endif
532
533         return 0;
534 }
535
536 static int reserve_bootstage(void)
537 {
538 #ifdef CONFIG_BOOTSTAGE
539         int size = bootstage_get_size();
540
541         gd->start_addr_sp -= size;
542         gd->new_bootstage = map_sysmem(gd->start_addr_sp, size);
543         debug("Reserving %#x Bytes for bootstage at: %08lx\n", size,
544               gd->start_addr_sp);
545 #endif
546
547         return 0;
548 }
549
550 __weak int arch_reserve_stacks(void)
551 {
552         return 0;
553 }
554
555 static int reserve_stacks(void)
556 {
557         /* make stack pointer 16-byte aligned */
558         gd->start_addr_sp -= 16;
559         gd->start_addr_sp &= ~0xf;
560
561         /*
562          * let the architecture-specific code tailor gd->start_addr_sp and
563          * gd->irq_sp
564          */
565         return arch_reserve_stacks();
566 }
567
568 static int reserve_bloblist(void)
569 {
570 #ifdef CONFIG_BLOBLIST
571         gd->start_addr_sp -= CONFIG_BLOBLIST_SIZE;
572         gd->new_bloblist = map_sysmem(gd->start_addr_sp, CONFIG_BLOBLIST_SIZE);
573 #endif
574
575         return 0;
576 }
577
578 static int display_new_sp(void)
579 {
580         debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
581
582         return 0;
583 }
584
585 #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
586         defined(CONFIG_SH)
587 static int setup_board_part1(void)
588 {
589         bd_t *bd = gd->bd;
590
591         /*
592          * Save local variables to board info struct
593          */
594         bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;        /* start of memory */
595         bd->bi_memsize = gd->ram_size;                  /* size in bytes */
596
597 #ifdef CONFIG_SYS_SRAM_BASE
598         bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;        /* start of SRAM */
599         bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;         /* size  of SRAM */
600 #endif
601
602 #if defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
603         bd->bi_immr_base = CONFIG_SYS_IMMR;     /* base  of IMMR register     */
604 #endif
605 #if defined(CONFIG_M68K)
606         bd->bi_mbar_base = CONFIG_SYS_MBAR;     /* base of internal registers */
607 #endif
608 #if defined(CONFIG_MPC83xx)
609         bd->bi_immrbar = CONFIG_SYS_IMMR;
610 #endif
611
612         return 0;
613 }
614 #endif
615
616 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
617 static int setup_board_part2(void)
618 {
619         bd_t *bd = gd->bd;
620
621         bd->bi_intfreq = gd->cpu_clk;   /* Internal Freq, in Hz */
622         bd->bi_busfreq = gd->bus_clk;   /* Bus Freq,      in Hz */
623 #if defined(CONFIG_CPM2)
624         bd->bi_cpmfreq = gd->arch.cpm_clk;
625         bd->bi_brgfreq = gd->arch.brg_clk;
626         bd->bi_sccfreq = gd->arch.scc_clk;
627         bd->bi_vco = gd->arch.vco_out;
628 #endif /* CONFIG_CPM2 */
629 #if defined(CONFIG_M68K) && defined(CONFIG_PCI)
630         bd->bi_pcifreq = gd->pci_clk;
631 #endif
632 #if defined(CONFIG_EXTRA_CLOCK)
633         bd->bi_inpfreq = gd->arch.inp_clk;      /* input Freq in Hz */
634         bd->bi_vcofreq = gd->arch.vco_clk;      /* vco Freq in Hz */
635         bd->bi_flbfreq = gd->arch.flb_clk;      /* flexbus Freq in Hz */
636 #endif
637
638         return 0;
639 }
640 #endif
641
642 #ifdef CONFIG_POST
643 static int init_post(void)
644 {
645         post_bootmode_init();
646         post_run(NULL, POST_ROM | post_bootmode_get(0));
647
648         return 0;
649 }
650 #endif
651
652 static int reloc_fdt(void)
653 {
654 #ifndef CONFIG_OF_EMBED
655         if (gd->flags & GD_FLG_SKIP_RELOC)
656                 return 0;
657         if (gd->new_fdt) {
658                 memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
659                 gd->fdt_blob = gd->new_fdt;
660         }
661 #endif
662
663         return 0;
664 }
665
666 static int reloc_bootstage(void)
667 {
668 #ifdef CONFIG_BOOTSTAGE
669         if (gd->flags & GD_FLG_SKIP_RELOC)
670                 return 0;
671         if (gd->new_bootstage) {
672                 int size = bootstage_get_size();
673
674                 debug("Copying bootstage from %p to %p, size %x\n",
675                       gd->bootstage, gd->new_bootstage, size);
676                 memcpy(gd->new_bootstage, gd->bootstage, size);
677                 gd->bootstage = gd->new_bootstage;
678         }
679 #endif
680
681         return 0;
682 }
683
684 static int reloc_bloblist(void)
685 {
686 #ifdef CONFIG_BLOBLIST
687         if (gd->flags & GD_FLG_SKIP_RELOC)
688                 return 0;
689         if (gd->new_bloblist) {
690                 int size = CONFIG_BLOBLIST_SIZE;
691
692                 debug("Copying bloblist from %p to %p, size %x\n",
693                       gd->bloblist, gd->new_bloblist, size);
694                 memcpy(gd->new_bloblist, gd->bloblist, size);
695                 gd->bloblist = gd->new_bloblist;
696         }
697 #endif
698
699         return 0;
700 }
701
702 static int setup_reloc(void)
703 {
704         if (gd->flags & GD_FLG_SKIP_RELOC) {
705                 debug("Skipping relocation due to flag\n");
706                 return 0;
707         }
708
709 #ifdef CONFIG_SYS_TEXT_BASE
710 #ifdef ARM
711         gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
712 #elif defined(CONFIG_M68K)
713         /*
714          * On all ColdFire arch cpu, monitor code starts always
715          * just after the default vector table location, so at 0x400
716          */
717         gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
718 #elif !defined(CONFIG_SANDBOX)
719         gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
720 #endif
721 #endif
722         memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
723
724         debug("Relocation Offset is: %08lx\n", gd->reloc_off);
725         debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
726               gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
727               gd->start_addr_sp);
728
729         return 0;
730 }
731
732 #ifdef CONFIG_OF_BOARD_FIXUP
733 static int fix_fdt(void)
734 {
735         return board_fix_fdt((void *)gd->fdt_blob);
736 }
737 #endif
738
739 /* ARM calls relocate_code from its crt0.S */
740 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
741                 !CONFIG_IS_ENABLED(X86_64)
742
743 static int jump_to_copy(void)
744 {
745         if (gd->flags & GD_FLG_SKIP_RELOC)
746                 return 0;
747         /*
748          * x86 is special, but in a nice way. It uses a trampoline which
749          * enables the dcache if possible.
750          *
751          * For now, other archs use relocate_code(), which is implemented
752          * similarly for all archs. When we do generic relocation, hopefully
753          * we can make all archs enable the dcache prior to relocation.
754          */
755 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
756         /*
757          * SDRAM and console are now initialised. The final stack can now
758          * be setup in SDRAM. Code execution will continue in Flash, but
759          * with the stack in SDRAM and Global Data in temporary memory
760          * (CPU cache)
761          */
762         arch_setup_gd(gd->new_gd);
763         board_init_f_r_trampoline(gd->start_addr_sp);
764 #else
765         relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
766 #endif
767
768         return 0;
769 }
770 #endif
771
772 /* Record the board_init_f() bootstage (after arch_cpu_init()) */
773 static int initf_bootstage(void)
774 {
775         bool from_spl = IS_ENABLED(CONFIG_SPL_BOOTSTAGE) &&
776                         IS_ENABLED(CONFIG_BOOTSTAGE_STASH);
777         int ret;
778
779         ret = bootstage_init(!from_spl);
780         if (ret)
781                 return ret;
782         if (from_spl) {
783                 const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
784                                                CONFIG_BOOTSTAGE_STASH_SIZE);
785
786                 ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
787                 if (ret && ret != -ENOENT) {
788                         debug("Failed to unstash bootstage: err=%d\n", ret);
789                         return ret;
790                 }
791         }
792
793         bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
794
795         return 0;
796 }
797
798 static int initf_console_record(void)
799 {
800 #if defined(CONFIG_CONSOLE_RECORD) && CONFIG_VAL(SYS_MALLOC_F_LEN)
801         return console_record_init();
802 #else
803         return 0;
804 #endif
805 }
806
807 static int initf_dm(void)
808 {
809 #if defined(CONFIG_DM) && CONFIG_VAL(SYS_MALLOC_F_LEN)
810         int ret;
811
812         bootstage_start(BOOTSTATE_ID_ACCUM_DM_F, "dm_f");
813         ret = dm_init_and_scan(true);
814         bootstage_accum(BOOTSTATE_ID_ACCUM_DM_F);
815         if (ret)
816                 return ret;
817 #endif
818 #ifdef CONFIG_TIMER_EARLY
819         ret = dm_timer_init();
820         if (ret)
821                 return ret;
822 #endif
823
824         return 0;
825 }
826
827 /* Architecture-specific memory reservation */
828 __weak int reserve_arch(void)
829 {
830         return 0;
831 }
832
833 __weak int arch_cpu_init_dm(void)
834 {
835         return 0;
836 }
837
838 static const init_fnc_t init_sequence_f[] = {
839         setup_mon_len,
840 #ifdef CONFIG_OF_CONTROL
841         fdtdec_setup,
842 #endif
843 #ifdef CONFIG_TRACE_EARLY
844         trace_early_init,
845 #endif
846         initf_malloc,
847         log_init,
848         initf_bootstage,        /* uses its own timer, so does not need DM */
849 #ifdef CONFIG_BLOBLIST
850         bloblist_init,
851 #endif
852         setup_spl_handoff,
853         initf_console_record,
854 #if defined(CONFIG_HAVE_FSP)
855         arch_fsp_init,
856 #endif
857         arch_cpu_init,          /* basic arch cpu dependent setup */
858         mach_cpu_init,          /* SoC/machine dependent CPU setup */
859         initf_dm,
860         arch_cpu_init_dm,
861 #if defined(CONFIG_BOARD_EARLY_INIT_F)
862         board_early_init_f,
863 #endif
864 #if defined(CONFIG_PPC) || defined(CONFIG_SYS_FSL_CLK) || defined(CONFIG_M68K)
865         /* get CPU and bus clocks according to the environment variable */
866         get_clocks,             /* get CPU and bus clocks (etc.) */
867 #endif
868 #if !defined(CONFIG_M68K)
869         timer_init,             /* initialize timer */
870 #endif
871 #if defined(CONFIG_BOARD_POSTCLK_INIT)
872         board_postclk_init,
873 #endif
874         env_init,               /* initialize environment */
875         init_baud_rate,         /* initialze baudrate settings */
876         serial_init,            /* serial communications setup */
877         console_init_f,         /* stage 1 init of console */
878         display_options,        /* say that we are here */
879         display_text_info,      /* show debugging info if required */
880 #if defined(CONFIG_PPC) || defined(CONFIG_SH) || defined(CONFIG_X86)
881         checkcpu,
882 #endif
883 #if defined(CONFIG_SYSRESET)
884         print_resetinfo,
885 #endif
886 #if defined(CONFIG_DISPLAY_CPUINFO)
887         print_cpuinfo,          /* display cpu info (and speed) */
888 #endif
889 #if defined(CONFIG_DTB_RESELECT)
890         embedded_dtb_select,
891 #endif
892 #if defined(CONFIG_DISPLAY_BOARDINFO)
893         show_board_info,
894 #endif
895         INIT_FUNC_WATCHDOG_INIT
896 #if defined(CONFIG_MISC_INIT_F)
897         misc_init_f,
898 #endif
899         INIT_FUNC_WATCHDOG_RESET
900 #if defined(CONFIG_SYS_I2C)
901         init_func_i2c,
902 #endif
903 #if defined(CONFIG_VID) && !defined(CONFIG_SPL)
904         init_func_vid,
905 #endif
906         announce_dram_init,
907         dram_init,              /* configure available RAM banks */
908 #ifdef CONFIG_POST
909         post_init_f,
910 #endif
911         INIT_FUNC_WATCHDOG_RESET
912 #if defined(CONFIG_SYS_DRAM_TEST)
913         testdram,
914 #endif /* CONFIG_SYS_DRAM_TEST */
915         INIT_FUNC_WATCHDOG_RESET
916
917 #ifdef CONFIG_POST
918         init_post,
919 #endif
920         INIT_FUNC_WATCHDOG_RESET
921         /*
922          * Now that we have DRAM mapped and working, we can
923          * relocate the code and continue running from DRAM.
924          *
925          * Reserve memory at end of RAM for (top down in that order):
926          *  - area that won't get touched by U-Boot and Linux (optional)
927          *  - kernel log buffer
928          *  - protected RAM
929          *  - LCD framebuffer
930          *  - monitor code
931          *  - board info struct
932          */
933         setup_dest_addr,
934 #ifdef CONFIG_PRAM
935         reserve_pram,
936 #endif
937         reserve_round_4k,
938 #ifdef CONFIG_ARM
939         reserve_mmu,
940 #endif
941         reserve_video,
942         reserve_trace,
943         reserve_uboot,
944         reserve_malloc,
945         reserve_board,
946         setup_machine,
947         reserve_global_data,
948         reserve_fdt,
949         reserve_bootstage,
950         reserve_bloblist,
951         reserve_arch,
952         reserve_stacks,
953         dram_init_banksize,
954         show_dram_config,
955 #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
956         defined(CONFIG_SH)
957         setup_board_part1,
958 #endif
959 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
960         INIT_FUNC_WATCHDOG_RESET
961         setup_board_part2,
962 #endif
963         display_new_sp,
964 #ifdef CONFIG_OF_BOARD_FIXUP
965         fix_fdt,
966 #endif
967         INIT_FUNC_WATCHDOG_RESET
968         reloc_fdt,
969         reloc_bootstage,
970         reloc_bloblist,
971         setup_reloc,
972 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
973         copy_uboot_to_ram,
974         do_elf_reloc_fixups,
975         clear_bss,
976 #endif
977 #if defined(CONFIG_XTENSA)
978         clear_bss,
979 #endif
980 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
981                 !CONFIG_IS_ENABLED(X86_64)
982         jump_to_copy,
983 #endif
984         NULL,
985 };
986
987 void board_init_f(ulong boot_flags)
988 {
989         gd->flags = boot_flags;
990         gd->have_console = 0;
991
992         if (initcall_run_list(init_sequence_f))
993                 hang();
994
995 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
996                 !defined(CONFIG_EFI_APP) && !CONFIG_IS_ENABLED(X86_64) && \
997                 !defined(CONFIG_ARC)
998         /* NOTREACHED - jump_to_copy() does not return */
999         hang();
1000 #endif
1001 }
1002
1003 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
1004 /*
1005  * For now this code is only used on x86.
1006  *
1007  * init_sequence_f_r is the list of init functions which are run when
1008  * U-Boot is executing from Flash with a semi-limited 'C' environment.
1009  * The following limitations must be considered when implementing an
1010  * '_f_r' function:
1011  *  - 'static' variables are read-only
1012  *  - Global Data (gd->xxx) is read/write
1013  *
1014  * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
1015  * supported).  It _should_, if possible, copy global data to RAM and
1016  * initialise the CPU caches (to speed up the relocation process)
1017  *
1018  * NOTE: At present only x86 uses this route, but it is intended that
1019  * all archs will move to this when generic relocation is implemented.
1020  */
1021 static const init_fnc_t init_sequence_f_r[] = {
1022 #if !CONFIG_IS_ENABLED(X86_64)
1023         init_cache_f_r,
1024 #endif
1025
1026         NULL,
1027 };
1028
1029 void board_init_f_r(void)
1030 {
1031         if (initcall_run_list(init_sequence_f_r))
1032                 hang();
1033
1034         /*
1035          * The pre-relocation drivers may be using memory that has now gone
1036          * away. Mark serial as unavailable - this will fall back to the debug
1037          * UART if available.
1038          *
1039          * Do the same with log drivers since the memory may not be available.
1040          */
1041         gd->flags &= ~(GD_FLG_SERIAL_READY | GD_FLG_LOG_READY);
1042 #ifdef CONFIG_TIMER
1043         gd->timer = NULL;
1044 #endif
1045
1046         /*
1047          * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
1048          * Transfer execution from Flash to RAM by calculating the address
1049          * of the in-RAM copy of board_init_r() and calling it
1050          */
1051         (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
1052
1053         /* NOTREACHED - board_init_r() does not return */
1054         hang();
1055 }
1056 #endif /* CONFIG_X86 */