common/board_f: move CONFIG_MACH_TYPE logic to arch/arm/lib/bdinfo.c
[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 <bootstage.h>
15 #include <clock_legacy.h>
16 #include <console.h>
17 #include <cpu.h>
18 #include <cpu_func.h>
19 #include <cyclic.h>
20 #include <display_options.h>
21 #include <dm.h>
22 #include <env.h>
23 #include <env_internal.h>
24 #include <event.h>
25 #include <fdtdec.h>
26 #include <fs.h>
27 #include <hang.h>
28 #include <i2c.h>
29 #include <init.h>
30 #include <initcall.h>
31 #include <lcd.h>
32 #include <log.h>
33 #include <malloc.h>
34 #include <mapmem.h>
35 #include <os.h>
36 #include <post.h>
37 #include <relocate.h>
38 #include <serial.h>
39 #ifdef CONFIG_SPL
40 #include <spl.h>
41 #endif
42 #include <status_led.h>
43 #include <sysreset.h>
44 #include <timer.h>
45 #include <trace.h>
46 #include <video.h>
47 #include <watchdog.h>
48 #include <asm/cache.h>
49 #if defined(CONFIG_MP) && defined(CONFIG_PPC)
50 #include <asm/mp.h>
51 #endif
52 #include <asm/global_data.h>
53 #include <asm/io.h>
54 #include <asm/sections.h>
55 #include <dm/root.h>
56 #include <linux/errno.h>
57 #include <linux/log2.h>
58
59 DECLARE_GLOBAL_DATA_PTR;
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         schedule();
105
106         return 0;
107 }
108
109 int init_func_watchdog_reset(void)
110 {
111         schedule();
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         dev = cpu_get_current_dev();
181         if (!dev) {
182                 debug("%s: Could not get CPU device\n",
183                       __func__);
184                 return -ENODEV;
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 /*
207  * From input size calculate its nearest rounded unit scale (multiply of 2^10)
208  * and value in calculated unit scale multiplied by 10 (as fractional fixed
209  * point number with one decimal digit), which is human natural format,
210  * same what uses print_size() function for displaying. Mathematically it is:
211  * round_nearest(val * 2^scale) = size * 10; where: 10 <= val < 10240.
212  *
213  * For example for size=87654321 we calculate scale=20 and val=836 which means
214  * that input has natural human format 83.6 M (mega = 2^20).
215  */
216 #define compute_size_scale_val(size, scale, val) do { \
217         scale = ilog2(size) / 10 * 10; \
218         val = (10 * size + ((1ULL << scale) >> 1)) >> scale; \
219         if (val == 10240) { val = 10; scale += 10; } \
220 } while (0)
221
222 /*
223  * Check if the sizes in their natural units written in decimal format with
224  * one fraction number are same.
225  */
226 static int sizes_near(unsigned long long size1, unsigned long long size2)
227 {
228         unsigned int size1_scale, size1_val, size2_scale, size2_val;
229
230         compute_size_scale_val(size1, size1_scale, size1_val);
231         compute_size_scale_val(size2, size2_scale, size2_val);
232
233         return size1_scale == size2_scale && size1_val == size2_val;
234 }
235
236 static int show_dram_config(void)
237 {
238         unsigned long long size;
239         int i;
240
241         debug("\nRAM Configuration:\n");
242         for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
243                 size += gd->bd->bi_dram[i].size;
244                 debug("Bank #%d: %llx ", i,
245                       (unsigned long long)(gd->bd->bi_dram[i].start));
246 #ifdef DEBUG
247                 print_size(gd->bd->bi_dram[i].size, "\n");
248 #endif
249         }
250         debug("\nDRAM:  ");
251
252         print_size(gd->ram_size, "");
253         if (!sizes_near(gd->ram_size, size)) {
254                 printf(" (effective ");
255                 print_size(size, ")");
256         }
257         board_add_ram_info(0);
258         putc('\n');
259
260         return 0;
261 }
262
263 __weak int dram_init_banksize(void)
264 {
265         gd->bd->bi_dram[0].start = gd->ram_base;
266         gd->bd->bi_dram[0].size = get_effective_memsize();
267
268         return 0;
269 }
270
271 #if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
272 static int init_func_i2c(void)
273 {
274         puts("I2C:   ");
275         i2c_init_all();
276         puts("ready\n");
277         return 0;
278 }
279 #endif
280
281 #if defined(CONFIG_VID)
282 __weak int init_func_vid(void)
283 {
284         return 0;
285 }
286 #endif
287
288 static int setup_mon_len(void)
289 {
290 #if defined(__ARM__) || defined(__MICROBLAZE__)
291         gd->mon_len = (ulong)&__bss_end - (ulong)_start;
292 #elif defined(CONFIG_SANDBOX)
293         gd->mon_len = 0;
294 #elif defined(CONFIG_EFI_APP)
295         gd->mon_len = (ulong)&_end - (ulong)_init;
296 #elif defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
297         gd->mon_len = CONFIG_SYS_MONITOR_LEN;
298 #elif defined(CONFIG_SH) || defined(CONFIG_RISCV)
299         gd->mon_len = (ulong)(&__bss_end) - (ulong)(&_start);
300 #elif defined(CONFIG_SYS_MONITOR_BASE)
301         /* TODO: use (ulong)&__bss_end - (ulong)&__text_start; ? */
302         gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
303 #endif
304         return 0;
305 }
306
307 static int setup_spl_handoff(void)
308 {
309 #if CONFIG_IS_ENABLED(HANDOFF)
310         gd->spl_handoff = bloblist_find(BLOBLISTT_U_BOOT_SPL_HANDOFF,
311                                         sizeof(struct spl_handoff));
312         debug("Found SPL hand-off info %p\n", gd->spl_handoff);
313 #endif
314
315         return 0;
316 }
317
318 __weak int arch_cpu_init(void)
319 {
320         return 0;
321 }
322
323 __weak int mach_cpu_init(void)
324 {
325         return 0;
326 }
327
328 /* Get the top of usable RAM */
329 __weak phys_size_t board_get_usable_ram_top(phys_size_t total_size)
330 {
331 #if defined(CONFIG_SYS_SDRAM_BASE) && CONFIG_SYS_SDRAM_BASE > 0
332         /*
333          * Detect whether we have so much RAM that it goes past the end of our
334          * 32-bit address space. If so, clip the usable RAM so it doesn't.
335          */
336         if (gd->ram_top < CONFIG_SYS_SDRAM_BASE)
337                 /*
338                  * Will wrap back to top of 32-bit space when reservations
339                  * are made.
340                  */
341                 return 0;
342 #endif
343         return gd->ram_top;
344 }
345
346 static int setup_dest_addr(void)
347 {
348         debug("Monitor len: %08lX\n", gd->mon_len);
349         /*
350          * Ram is setup, size stored in gd !!
351          */
352         debug("Ram size: %08llX\n", (unsigned long long)gd->ram_size);
353 #if CONFIG_VAL(SYS_MEM_TOP_HIDE)
354         /*
355          * Subtract specified amount of memory to hide so that it won't
356          * get "touched" at all by U-Boot. By fixing up gd->ram_size
357          * the Linux kernel should now get passed the now "corrected"
358          * memory size and won't touch it either. This should work
359          * for arch/ppc and arch/powerpc. Only Linux board ports in
360          * arch/powerpc with bootwrapper support, that recalculate the
361          * memory size from the SDRAM controller setup will have to
362          * get fixed.
363          */
364         gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
365 #endif
366 #ifdef CONFIG_SYS_SDRAM_BASE
367         gd->ram_base = CONFIG_SYS_SDRAM_BASE;
368 #endif
369         gd->ram_top = gd->ram_base + get_effective_memsize();
370         gd->ram_top = board_get_usable_ram_top(gd->mon_len);
371         gd->relocaddr = gd->ram_top;
372         debug("Ram top: %08llX\n", (unsigned long long)gd->ram_top);
373 #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
374         /*
375          * We need to make sure the location we intend to put secondary core
376          * boot code is reserved and not used by any part of u-boot
377          */
378         if (gd->relocaddr > determine_mp_bootpg(NULL)) {
379                 gd->relocaddr = determine_mp_bootpg(NULL);
380                 debug("Reserving MP boot page to %08lx\n", gd->relocaddr);
381         }
382 #endif
383         return 0;
384 }
385
386 #ifdef CONFIG_PRAM
387 /* reserve protected RAM */
388 static int reserve_pram(void)
389 {
390         ulong reg;
391
392         reg = env_get_ulong("pram", 10, CONFIG_PRAM);
393         gd->relocaddr -= (reg << 10);           /* size is in kB */
394         debug("Reserving %ldk for protected RAM at %08lx\n", reg,
395               gd->relocaddr);
396         return 0;
397 }
398 #endif /* CONFIG_PRAM */
399
400 /* Round memory pointer down to next 4 kB limit */
401 static int reserve_round_4k(void)
402 {
403         gd->relocaddr &= ~(4096 - 1);
404         return 0;
405 }
406
407 __weak int arch_reserve_mmu(void)
408 {
409         return 0;
410 }
411
412 static int reserve_video(void)
413 {
414 #ifdef CONFIG_DM_VIDEO
415         ulong addr;
416         int ret;
417
418         addr = gd->relocaddr;
419         ret = video_reserve(&addr);
420         if (ret)
421                 return ret;
422         debug("Reserving %luk for video at: %08lx\n",
423               ((unsigned long)gd->relocaddr - addr) >> 10, addr);
424         gd->relocaddr = addr;
425 #elif defined(CONFIG_LCD)
426         /* reserve memory for LCD display (always full pages) */
427         gd->relocaddr = lcd_setmem(gd->relocaddr);
428         gd->fb_base = gd->relocaddr;
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 /*
470  * reserve after start_addr_sp the requested size and make the stack pointer
471  * 16-byte aligned, this alignment is needed for cast on the reserved memory
472  * ref = x86_64 ABI: https://reviews.llvm.org/D30049: 16 bytes
473  *     = ARMv8 Instruction Set Overview: quad word, 16 bytes
474  */
475 static unsigned long reserve_stack_aligned(size_t size)
476 {
477         return ALIGN_DOWN(gd->start_addr_sp - size, 16);
478 }
479
480 #ifdef CONFIG_SYS_NONCACHED_MEMORY
481 static int reserve_noncached(void)
482 {
483         /*
484          * The value of gd->start_addr_sp must match the value of malloc_start
485          * calculated in boatrd_f.c:initr_malloc(), which is passed to
486          * board_r.c:mem_malloc_init() and then used by
487          * cache.c:noncached_init()
488          *
489          * These calculations must match the code in cache.c:noncached_init()
490          */
491         gd->start_addr_sp = ALIGN(gd->start_addr_sp, MMU_SECTION_SIZE) -
492                 MMU_SECTION_SIZE;
493         gd->start_addr_sp -= ALIGN(CONFIG_SYS_NONCACHED_MEMORY,
494                                    MMU_SECTION_SIZE);
495         debug("Reserving %dM for noncached_alloc() at: %08lx\n",
496               CONFIG_SYS_NONCACHED_MEMORY >> 20, gd->start_addr_sp);
497
498         return 0;
499 }
500 #endif
501
502 /* reserve memory for malloc() area */
503 static int reserve_malloc(void)
504 {
505         gd->start_addr_sp = reserve_stack_aligned(TOTAL_MALLOC_LEN);
506         debug("Reserving %dk for malloc() at: %08lx\n",
507               TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
508 #ifdef CONFIG_SYS_NONCACHED_MEMORY
509         reserve_noncached();
510 #endif
511
512         return 0;
513 }
514
515 /* (permanently) allocate a Board Info struct */
516 static int reserve_board(void)
517 {
518         if (!gd->bd) {
519                 gd->start_addr_sp = reserve_stack_aligned(sizeof(struct bd_info));
520                 gd->bd = (struct bd_info *)map_sysmem(gd->start_addr_sp,
521                                                       sizeof(struct bd_info));
522                 memset(gd->bd, '\0', sizeof(struct bd_info));
523                 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
524                       sizeof(struct bd_info), gd->start_addr_sp);
525         }
526         return 0;
527 }
528
529 static int reserve_global_data(void)
530 {
531         gd->start_addr_sp = reserve_stack_aligned(sizeof(gd_t));
532         gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
533         debug("Reserving %zu Bytes for Global Data at: %08lx\n",
534               sizeof(gd_t), gd->start_addr_sp);
535         return 0;
536 }
537
538 static int reserve_fdt(void)
539 {
540         if (!IS_ENABLED(CONFIG_OF_EMBED)) {
541                 /*
542                  * If the device tree is sitting immediately above our image
543                  * then we must relocate it. If it is embedded in the data
544                  * section, then it will be relocated with other data.
545                  */
546                 if (gd->fdt_blob) {
547                         gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob), 32);
548
549                         gd->start_addr_sp = reserve_stack_aligned(gd->fdt_size);
550                         gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
551                         debug("Reserving %lu Bytes for FDT at: %08lx\n",
552                               gd->fdt_size, gd->start_addr_sp);
553                 }
554         }
555
556         return 0;
557 }
558
559 static int reserve_bootstage(void)
560 {
561 #ifdef CONFIG_BOOTSTAGE
562         int size = bootstage_get_size();
563
564         gd->start_addr_sp = reserve_stack_aligned(size);
565         gd->new_bootstage = map_sysmem(gd->start_addr_sp, size);
566         debug("Reserving %#x Bytes for bootstage at: %08lx\n", size,
567               gd->start_addr_sp);
568 #endif
569
570         return 0;
571 }
572
573 __weak int arch_reserve_stacks(void)
574 {
575         return 0;
576 }
577
578 static int reserve_stacks(void)
579 {
580         /* make stack pointer 16-byte aligned */
581         gd->start_addr_sp = reserve_stack_aligned(16);
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         /* Align to a 4KB boundary for easier reading of addresses */
594         gd->start_addr_sp = ALIGN_DOWN(gd->start_addr_sp -
595                                        CONFIG_BLOBLIST_SIZE_RELOC, 0x1000);
596         gd->new_bloblist = map_sysmem(gd->start_addr_sp,
597                                       CONFIG_BLOBLIST_SIZE_RELOC);
598 #endif
599
600         return 0;
601 }
602
603 static int display_new_sp(void)
604 {
605         debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
606
607         return 0;
608 }
609
610 __weak int arch_setup_bdinfo(void)
611 {
612         return 0;
613 }
614
615 int setup_bdinfo(void)
616 {
617         struct bd_info *bd = gd->bd;
618
619         if (IS_ENABLED(CONFIG_SYS_HAS_SRAM)) {
620                 bd->bi_sramstart = CONFIG_SYS_SRAM_BASE; /* start of SRAM */
621                 bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;  /* size  of SRAM */
622         }
623
624         return arch_setup_bdinfo();
625 }
626
627 #ifdef CONFIG_POST
628 static int init_post(void)
629 {
630         post_bootmode_init();
631         post_run(NULL, POST_ROM | post_bootmode_get(0));
632
633         return 0;
634 }
635 #endif
636
637 static int reloc_fdt(void)
638 {
639         if (!IS_ENABLED(CONFIG_OF_EMBED)) {
640                 if (gd->flags & GD_FLG_SKIP_RELOC)
641                         return 0;
642                 if (gd->new_fdt) {
643                         memcpy(gd->new_fdt, gd->fdt_blob,
644                                fdt_totalsize(gd->fdt_blob));
645                         gd->fdt_blob = gd->new_fdt;
646                 }
647         }
648
649         return 0;
650 }
651
652 static int reloc_bootstage(void)
653 {
654 #ifdef CONFIG_BOOTSTAGE
655         if (gd->flags & GD_FLG_SKIP_RELOC)
656                 return 0;
657         if (gd->new_bootstage) {
658                 int size = bootstage_get_size();
659
660                 debug("Copying bootstage from %p to %p, size %x\n",
661                       gd->bootstage, gd->new_bootstage, size);
662                 memcpy(gd->new_bootstage, gd->bootstage, size);
663                 gd->bootstage = gd->new_bootstage;
664                 bootstage_relocate();
665         }
666 #endif
667
668         return 0;
669 }
670
671 static int reloc_bloblist(void)
672 {
673 #ifdef CONFIG_BLOBLIST
674         /*
675          * Relocate only if we are supposed to send it
676          */
677         if ((gd->flags & GD_FLG_SKIP_RELOC) &&
678             CONFIG_BLOBLIST_SIZE == CONFIG_BLOBLIST_SIZE_RELOC) {
679                 debug("Not relocating bloblist\n");
680                 return 0;
681         }
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                 bloblist_reloc(gd->new_bloblist, CONFIG_BLOBLIST_SIZE_RELOC,
688                                gd->bloblist, size);
689                 gd->bloblist = gd->new_bloblist;
690         }
691 #endif
692
693         return 0;
694 }
695
696 static int setup_reloc(void)
697 {
698         if (!(gd->flags & GD_FLG_SKIP_RELOC)) {
699 #ifdef CONFIG_SYS_TEXT_BASE
700 #ifdef ARM
701                 gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
702 #elif defined(CONFIG_MICROBLAZE)
703                 gd->reloc_off = gd->relocaddr - (u32)_start;
704 #elif defined(CONFIG_M68K)
705                 /*
706                  * On all ColdFire arch cpu, monitor code starts always
707                  * just after the default vector table location, so at 0x400
708                  */
709                 gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
710 #elif !defined(CONFIG_SANDBOX)
711                 gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
712 #endif
713 #endif
714         }
715
716         memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
717
718         if (gd->flags & GD_FLG_SKIP_RELOC) {
719                 debug("Skipping relocation due to flag\n");
720         } else {
721                 debug("Relocation Offset is: %08lx\n", gd->reloc_off);
722                 debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
723                       gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
724                       gd->start_addr_sp);
725         }
726
727         return 0;
728 }
729
730 #ifdef CONFIG_OF_BOARD_FIXUP
731 static int fix_fdt(void)
732 {
733         return board_fix_fdt((void *)gd->fdt_blob);
734 }
735 #endif
736
737 /* ARM calls relocate_code from its crt0.S */
738 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
739                 !CONFIG_IS_ENABLED(X86_64)
740
741 static int jump_to_copy(void)
742 {
743         if (gd->flags & GD_FLG_SKIP_RELOC)
744                 return 0;
745         /*
746          * x86 is special, but in a nice way. It uses a trampoline which
747          * enables the dcache if possible.
748          *
749          * For now, other archs use relocate_code(), which is implemented
750          * similarly for all archs. When we do generic relocation, hopefully
751          * we can make all archs enable the dcache prior to relocation.
752          */
753 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
754         /*
755          * SDRAM and console are now initialised. The final stack can now
756          * be setup in SDRAM. Code execution will continue in Flash, but
757          * with the stack in SDRAM and Global Data in temporary memory
758          * (CPU cache)
759          */
760         arch_setup_gd(gd->new_gd);
761         board_init_f_r_trampoline(gd->start_addr_sp);
762 #else
763         relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
764 #endif
765
766         return 0;
767 }
768 #endif
769
770 /* Record the board_init_f() bootstage (after arch_cpu_init()) */
771 static int initf_bootstage(void)
772 {
773         bool from_spl = IS_ENABLED(CONFIG_SPL_BOOTSTAGE) &&
774                         IS_ENABLED(CONFIG_BOOTSTAGE_STASH);
775         int ret;
776
777         ret = bootstage_init(!from_spl);
778         if (ret)
779                 return ret;
780         if (from_spl) {
781                 const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
782                                                CONFIG_BOOTSTAGE_STASH_SIZE);
783
784                 ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
785                 if (ret && ret != -ENOENT) {
786                         debug("Failed to unstash bootstage: err=%d\n", ret);
787                         return ret;
788                 }
789         }
790
791         bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
792
793         return 0;
794 }
795
796 static int initf_dm(void)
797 {
798 #if defined(CONFIG_DM) && CONFIG_VAL(SYS_MALLOC_F_LEN)
799         int ret;
800
801         bootstage_start(BOOTSTAGE_ID_ACCUM_DM_F, "dm_f");
802         ret = dm_init_and_scan(true);
803         bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_F);
804         if (ret)
805                 return ret;
806
807         if (IS_ENABLED(CONFIG_TIMER_EARLY)) {
808                 ret = dm_timer_init();
809                 if (ret)
810                         return ret;
811         }
812 #endif
813
814         return 0;
815 }
816
817 /* Architecture-specific memory reservation */
818 __weak int reserve_arch(void)
819 {
820         return 0;
821 }
822
823 __weak int checkcpu(void)
824 {
825         return 0;
826 }
827
828 __weak int clear_bss(void)
829 {
830         return 0;
831 }
832
833 static int misc_init_f(void)
834 {
835         return event_notify_null(EVT_MISC_INIT_F);
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         cyclic_init,
850         event_init,
851 #ifdef CONFIG_BLOBLIST
852         bloblist_init,
853 #endif
854         setup_spl_handoff,
855 #if defined(CONFIG_CONSOLE_RECORD_INIT_F)
856         console_record_init,
857 #endif
858 #if defined(CONFIG_HAVE_FSP)
859         arch_fsp_init,
860 #endif
861         arch_cpu_init,          /* basic arch cpu dependent setup */
862         mach_cpu_init,          /* SoC/machine dependent CPU setup */
863         initf_dm,
864 #if defined(CONFIG_BOARD_EARLY_INIT_F)
865         board_early_init_f,
866 #endif
867 #if defined(CONFIG_PPC) || defined(CONFIG_SYS_FSL_CLK) || defined(CONFIG_M68K)
868         /* get CPU and bus clocks according to the environment variable */
869         get_clocks,             /* get CPU and bus clocks (etc.) */
870 #endif
871 #if !defined(CONFIG_M68K)
872         timer_init,             /* initialize timer */
873 #endif
874 #if defined(CONFIG_BOARD_POSTCLK_INIT)
875         board_postclk_init,
876 #endif
877         env_init,               /* initialize environment */
878         init_baud_rate,         /* initialze baudrate settings */
879         serial_init,            /* serial communications setup */
880         console_init_f,         /* stage 1 init of console */
881         display_options,        /* say that we are here */
882         display_text_info,      /* show debugging info if required */
883         checkcpu,
884 #if defined(CONFIG_SYSRESET)
885         print_resetinfo,
886 #endif
887 #if defined(CONFIG_DISPLAY_CPUINFO)
888         print_cpuinfo,          /* display cpu info (and speed) */
889 #endif
890 #if defined(CONFIG_DTB_RESELECT)
891         embedded_dtb_select,
892 #endif
893 #if defined(CONFIG_DISPLAY_BOARDINFO)
894         show_board_info,
895 #endif
896         INIT_FUNC_WATCHDOG_INIT
897         misc_init_f,
898         INIT_FUNC_WATCHDOG_RESET
899 #if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
900         init_func_i2c,
901 #endif
902 #if defined(CONFIG_VID) && !defined(CONFIG_SPL)
903         init_func_vid,
904 #endif
905         announce_dram_init,
906         dram_init,              /* configure available RAM banks */
907 #ifdef CONFIG_POST
908         post_init_f,
909 #endif
910         INIT_FUNC_WATCHDOG_RESET
911 #if defined(CONFIG_SYS_DRAM_TEST)
912         testdram,
913 #endif /* CONFIG_SYS_DRAM_TEST */
914         INIT_FUNC_WATCHDOG_RESET
915
916 #ifdef CONFIG_POST
917         init_post,
918 #endif
919         INIT_FUNC_WATCHDOG_RESET
920         /*
921          * Now that we have DRAM mapped and working, we can
922          * relocate the code and continue running from DRAM.
923          *
924          * Reserve memory at end of RAM for (top down in that order):
925          *  - area that won't get touched by U-Boot and Linux (optional)
926          *  - kernel log buffer
927          *  - protected RAM
928          *  - LCD framebuffer
929          *  - monitor code
930          *  - board info struct
931          */
932         setup_dest_addr,
933 #ifdef CONFIG_OF_BOARD_FIXUP
934         fix_fdt,
935 #endif
936 #ifdef CONFIG_PRAM
937         reserve_pram,
938 #endif
939         reserve_round_4k,
940         arch_reserve_mmu,
941         reserve_video,
942         reserve_trace,
943         reserve_uboot,
944         reserve_malloc,
945         reserve_board,
946         reserve_global_data,
947         reserve_fdt,
948         reserve_bootstage,
949         reserve_bloblist,
950         reserve_arch,
951         reserve_stacks,
952         dram_init_banksize,
953         show_dram_config,
954         INIT_FUNC_WATCHDOG_RESET
955         setup_bdinfo,
956         display_new_sp,
957         INIT_FUNC_WATCHDOG_RESET
958         reloc_fdt,
959         reloc_bootstage,
960         reloc_bloblist,
961         setup_reloc,
962 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
963         copy_uboot_to_ram,
964         do_elf_reloc_fixups,
965 #endif
966         clear_bss,
967 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
968                 !CONFIG_IS_ENABLED(X86_64)
969         jump_to_copy,
970 #endif
971         NULL,
972 };
973
974 void board_init_f(ulong boot_flags)
975 {
976         gd->flags = boot_flags;
977         gd->have_console = 0;
978
979         if (initcall_run_list(init_sequence_f))
980                 hang();
981
982 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
983                 !defined(CONFIG_EFI_APP) && !CONFIG_IS_ENABLED(X86_64) && \
984                 !defined(CONFIG_ARC)
985         /* NOTREACHED - jump_to_copy() does not return */
986         hang();
987 #endif
988 }
989
990 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
991 /*
992  * For now this code is only used on x86.
993  *
994  * init_sequence_f_r is the list of init functions which are run when
995  * U-Boot is executing from Flash with a semi-limited 'C' environment.
996  * The following limitations must be considered when implementing an
997  * '_f_r' function:
998  *  - 'static' variables are read-only
999  *  - Global Data (gd->xxx) is read/write
1000  *
1001  * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
1002  * supported).  It _should_, if possible, copy global data to RAM and
1003  * initialise the CPU caches (to speed up the relocation process)
1004  *
1005  * NOTE: At present only x86 uses this route, but it is intended that
1006  * all archs will move to this when generic relocation is implemented.
1007  */
1008 static const init_fnc_t init_sequence_f_r[] = {
1009 #if !CONFIG_IS_ENABLED(X86_64)
1010         init_cache_f_r,
1011 #endif
1012
1013         NULL,
1014 };
1015
1016 void board_init_f_r(void)
1017 {
1018         if (initcall_run_list(init_sequence_f_r))
1019                 hang();
1020
1021         /*
1022          * The pre-relocation drivers may be using memory that has now gone
1023          * away. Mark serial as unavailable - this will fall back to the debug
1024          * UART if available.
1025          *
1026          * Do the same with log drivers since the memory may not be available.
1027          */
1028         gd->flags &= ~(GD_FLG_SERIAL_READY | GD_FLG_LOG_READY);
1029 #ifdef CONFIG_TIMER
1030         gd->timer = NULL;
1031 #endif
1032
1033         /*
1034          * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
1035          * Transfer execution from Flash to RAM by calculating the address
1036          * of the in-RAM copy of board_init_r() and calling it
1037          */
1038         (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
1039
1040         /* NOTREACHED - board_init_r() does not return */
1041         hang();
1042 }
1043 #endif /* CONFIG_X86 */