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