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