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