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