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