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