image: Rename load_addr, save_addr, save_size
[oweals/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 <clock_legacy.h>
15 #include <console.h>
16 #include <cpu.h>
17 #include <cpu_func.h>
18 #include <dm.h>
19 #include <env.h>
20 #include <env_internal.h>
21 #include <fdtdec.h>
22 #include <fs.h>
23 #include <i2c.h>
24 #include <init.h>
25 #include <initcall.h>
26 #include <lcd.h>
27 #include <malloc.h>
28 #include <mapmem.h>
29 #include <os.h>
30 #include <post.h>
31 #include <relocate.h>
32 #include <serial.h>
33 #ifdef CONFIG_SPL
34 #include <spl.h>
35 #endif
36 #include <status_led.h>
37 #include <sysreset.h>
38 #include <timer.h>
39 #include <trace.h>
40 #include <video.h>
41 #include <watchdog.h>
42 #ifdef CONFIG_MACH_TYPE
43 #include <asm/mach-types.h>
44 #endif
45 #if defined(CONFIG_MP) && defined(CONFIG_PPC)
46 #include <asm/mp.h>
47 #endif
48 #include <asm/io.h>
49 #include <asm/sections.h>
50 #include <dm/root.h>
51 #include <linux/errno.h>
52
53 /*
54  * Pointer to initial global data area
55  *
56  * Here we initialize it if needed.
57  */
58 #ifdef XTRN_DECLARE_GLOBAL_DATA_PTR
59 #undef  XTRN_DECLARE_GLOBAL_DATA_PTR
60 #define XTRN_DECLARE_GLOBAL_DATA_PTR    /* empty = allocate here */
61 DECLARE_GLOBAL_DATA_PTR = (gd_t *)(CONFIG_SYS_INIT_GD_ADDR);
62 #else
63 DECLARE_GLOBAL_DATA_PTR;
64 #endif
65
66 /*
67  * TODO(sjg@chromium.org): IMO this code should be
68  * refactored to a single function, something like:
69  *
70  * void led_set_state(enum led_colour_t colour, int on);
71  */
72 /************************************************************************
73  * Coloured LED functionality
74  ************************************************************************
75  * May be supplied by boards if desired
76  */
77 __weak void coloured_LED_init(void) {}
78 __weak void red_led_on(void) {}
79 __weak void red_led_off(void) {}
80 __weak void green_led_on(void) {}
81 __weak void green_led_off(void) {}
82 __weak void yellow_led_on(void) {}
83 __weak void yellow_led_off(void) {}
84 __weak void blue_led_on(void) {}
85 __weak void blue_led_off(void) {}
86
87 /*
88  * Why is gd allocated a register? Prior to reloc it might be better to
89  * just pass it around to each function in this file?
90  *
91  * After reloc one could argue that it is hardly used and doesn't need
92  * to be in a register. Or if it is it should perhaps hold pointers to all
93  * global data for all modules, so that post-reloc we can avoid the massive
94  * literal pool we get on ARM. Or perhaps just encourage each module to use
95  * a structure...
96  */
97
98 #if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
99 static int init_func_watchdog_init(void)
100 {
101 # if defined(CONFIG_HW_WATCHDOG) && \
102         (defined(CONFIG_M68K) || defined(CONFIG_MICROBLAZE) || \
103         defined(CONFIG_SH) || \
104         defined(CONFIG_DESIGNWARE_WATCHDOG) || \
105         defined(CONFIG_IMX_WATCHDOG))
106         hw_watchdog_init();
107         puts("       Watchdog enabled\n");
108 # endif
109         WATCHDOG_RESET();
110
111         return 0;
112 }
113
114 int init_func_watchdog_reset(void)
115 {
116         WATCHDOG_RESET();
117
118         return 0;
119 }
120 #endif /* CONFIG_WATCHDOG */
121
122 __weak void board_add_ram_info(int use_default)
123 {
124         /* please define platform specific board_add_ram_info() */
125 }
126
127 static int init_baud_rate(void)
128 {
129         gd->baudrate = env_get_ulong("baudrate", 10, CONFIG_BAUDRATE);
130         return 0;
131 }
132
133 static int display_text_info(void)
134 {
135 #if !defined(CONFIG_SANDBOX) && !defined(CONFIG_EFI_APP)
136         ulong bss_start, bss_end, text_base;
137
138         bss_start = (ulong)&__bss_start;
139         bss_end = (ulong)&__bss_end;
140
141 #ifdef CONFIG_SYS_TEXT_BASE
142         text_base = CONFIG_SYS_TEXT_BASE;
143 #else
144         text_base = CONFIG_SYS_MONITOR_BASE;
145 #endif
146
147         debug("U-Boot code: %08lX -> %08lX  BSS: -> %08lX\n",
148               text_base, bss_start, bss_end);
149 #endif
150
151         return 0;
152 }
153
154 #ifdef CONFIG_SYSRESET
155 static int print_resetinfo(void)
156 {
157         struct udevice *dev;
158         char status[256];
159         int ret;
160
161         ret = uclass_first_device_err(UCLASS_SYSRESET, &dev);
162         if (ret) {
163                 debug("%s: No sysreset device found (error: %d)\n",
164                       __func__, ret);
165                 /* Not all boards have sysreset drivers available during early
166                  * boot, so don't fail if one can't be found.
167                  */
168                 return 0;
169         }
170
171         if (!sysreset_get_status(dev, status, sizeof(status)))
172                 printf("%s", status);
173
174         return 0;
175 }
176 #endif
177
178 #if defined(CONFIG_DISPLAY_CPUINFO) && CONFIG_IS_ENABLED(CPU)
179 static int print_cpuinfo(void)
180 {
181         struct udevice *dev;
182         char desc[512];
183         int ret;
184
185         ret = uclass_first_device_err(UCLASS_CPU, &dev);
186         if (ret) {
187                 debug("%s: Could not get CPU device (err = %d)\n",
188                       __func__, ret);
189                 return ret;
190         }
191
192         ret = cpu_get_desc(dev, desc, sizeof(desc));
193         if (ret) {
194                 debug("%s: Could not get CPU description (err = %d)\n",
195                       dev->name, ret);
196                 return ret;
197         }
198
199         printf("CPU:   %s\n", desc);
200
201         return 0;
202 }
203 #endif
204
205 static int announce_dram_init(void)
206 {
207         puts("DRAM:  ");
208         return 0;
209 }
210
211 static int show_dram_config(void)
212 {
213         unsigned long long size;
214
215 #ifdef CONFIG_NR_DRAM_BANKS
216         int i;
217
218         debug("\nRAM Configuration:\n");
219         for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
220                 size += gd->bd->bi_dram[i].size;
221                 debug("Bank #%d: %llx ", i,
222                       (unsigned long long)(gd->bd->bi_dram[i].start));
223 #ifdef DEBUG
224                 print_size(gd->bd->bi_dram[i].size, "\n");
225 #endif
226         }
227         debug("\nDRAM:  ");
228 #else
229         size = gd->ram_size;
230 #endif
231
232         print_size(size, "");
233         board_add_ram_info(0);
234         putc('\n');
235
236         return 0;
237 }
238
239 __weak int dram_init_banksize(void)
240 {
241 #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
242         gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
243         gd->bd->bi_dram[0].size = get_effective_memsize();
244 #endif
245
246         return 0;
247 }
248
249 #if defined(CONFIG_SYS_I2C)
250 static int init_func_i2c(void)
251 {
252         puts("I2C:   ");
253 #ifdef CONFIG_SYS_I2C
254         i2c_init_all();
255 #else
256         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
257 #endif
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 #ifdef CONFIG_SYS_SDRAM_BASE
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 #ifdef CONFIG_ARM
388 __weak int reserve_mmu(void)
389 {
390 #if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
391         /* reserve TLB table */
392         gd->arch.tlb_size = PGTABLE_SIZE;
393         gd->relocaddr -= gd->arch.tlb_size;
394
395         /* round down to next 64 kB limit */
396         gd->relocaddr &= ~(0x10000 - 1);
397
398         gd->arch.tlb_addr = gd->relocaddr;
399         debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
400               gd->arch.tlb_addr + gd->arch.tlb_size);
401
402 #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
403         /*
404          * Record allocated tlb_addr in case gd->tlb_addr to be overwritten
405          * with location within secure ram.
406          */
407         gd->arch.tlb_allocated = gd->arch.tlb_addr;
408 #endif
409 #endif
410
411         return 0;
412 }
413 #endif
414
415 static int reserve_video(void)
416 {
417 #ifdef CONFIG_DM_VIDEO
418         ulong addr;
419         int ret;
420
421         addr = gd->relocaddr;
422         ret = video_reserve(&addr);
423         if (ret)
424                 return ret;
425         gd->relocaddr = addr;
426 #elif defined(CONFIG_LCD)
427 #  ifdef CONFIG_FB_ADDR
428         gd->fb_base = CONFIG_FB_ADDR;
429 #  else
430         /* reserve memory for LCD display (always full pages) */
431         gd->relocaddr = lcd_setmem(gd->relocaddr);
432         gd->fb_base = gd->relocaddr;
433 #  endif /* CONFIG_FB_ADDR */
434 #endif
435
436         return 0;
437 }
438
439 static int reserve_trace(void)
440 {
441 #ifdef CONFIG_TRACE
442         gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
443         gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
444         debug("Reserving %luk for trace data at: %08lx\n",
445               (unsigned long)CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
446 #endif
447
448         return 0;
449 }
450
451 static int reserve_uboot(void)
452 {
453         if (!(gd->flags & GD_FLG_SKIP_RELOC)) {
454                 /*
455                  * reserve memory for U-Boot code, data & bss
456                  * round down to next 4 kB limit
457                  */
458                 gd->relocaddr -= gd->mon_len;
459                 gd->relocaddr &= ~(4096 - 1);
460         #if defined(CONFIG_E500) || defined(CONFIG_MIPS)
461                 /* round down to next 64 kB limit so that IVPR stays aligned */
462                 gd->relocaddr &= ~(65536 - 1);
463         #endif
464
465                 debug("Reserving %ldk for U-Boot at: %08lx\n",
466                       gd->mon_len >> 10, gd->relocaddr);
467         }
468
469         gd->start_addr_sp = gd->relocaddr;
470
471         return 0;
472 }
473
474 #ifdef CONFIG_SYS_NONCACHED_MEMORY
475 static int reserve_noncached(void)
476 {
477         /*
478          * The value of gd->start_addr_sp must match the value of malloc_start
479          * calculated in boatrd_f.c:initr_malloc(), which is passed to
480          * board_r.c:mem_malloc_init() and then used by
481          * cache.c:noncached_init()
482          *
483          * These calculations must match the code in cache.c:noncached_init()
484          */
485         gd->start_addr_sp = ALIGN(gd->start_addr_sp, MMU_SECTION_SIZE) -
486                 MMU_SECTION_SIZE;
487         gd->start_addr_sp -= ALIGN(CONFIG_SYS_NONCACHED_MEMORY,
488                                    MMU_SECTION_SIZE);
489         debug("Reserving %dM for noncached_alloc() at: %08lx\n",
490               CONFIG_SYS_NONCACHED_MEMORY >> 20, gd->start_addr_sp);
491
492         return 0;
493 }
494 #endif
495
496 /* reserve memory for malloc() area */
497 static int reserve_malloc(void)
498 {
499         gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
500         debug("Reserving %dk for malloc() at: %08lx\n",
501               TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
502 #ifdef CONFIG_SYS_NONCACHED_MEMORY
503         reserve_noncached();
504 #endif
505
506         return 0;
507 }
508
509 /* (permanently) allocate a Board Info struct */
510 static int reserve_board(void)
511 {
512         if (!gd->bd) {
513                 gd->start_addr_sp -= sizeof(bd_t);
514                 gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
515                 memset(gd->bd, '\0', sizeof(bd_t));
516                 debug("Reserving %zu Bytes for Board Info at: %08lx\n",
517                       sizeof(bd_t), gd->start_addr_sp);
518         }
519         return 0;
520 }
521
522 static int setup_machine(void)
523 {
524 #ifdef CONFIG_MACH_TYPE
525         gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
526 #endif
527         return 0;
528 }
529
530 static int reserve_global_data(void)
531 {
532         gd->start_addr_sp -= sizeof(gd_t);
533         gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
534         debug("Reserving %zu Bytes for Global Data at: %08lx\n",
535               sizeof(gd_t), gd->start_addr_sp);
536         return 0;
537 }
538
539 static int reserve_fdt(void)
540 {
541 #ifndef CONFIG_OF_EMBED
542         /*
543          * If the device tree is sitting immediately above our image then we
544          * must relocate it. If it is embedded in the data section, then it
545          * will be relocated with other data.
546          */
547         if (gd->fdt_blob) {
548                 gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
549
550                 gd->start_addr_sp -= gd->fdt_size;
551                 gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
552                 debug("Reserving %lu Bytes for FDT at: %08lx\n",
553                       gd->fdt_size, gd->start_addr_sp);
554         }
555 #endif
556
557         return 0;
558 }
559
560 static int reserve_bootstage(void)
561 {
562 #ifdef CONFIG_BOOTSTAGE
563         int size = bootstage_get_size();
564
565         gd->start_addr_sp -= size;
566         gd->new_bootstage = map_sysmem(gd->start_addr_sp, size);
567         debug("Reserving %#x Bytes for bootstage at: %08lx\n", size,
568               gd->start_addr_sp);
569 #endif
570
571         return 0;
572 }
573
574 __weak int arch_reserve_stacks(void)
575 {
576         return 0;
577 }
578
579 static int reserve_stacks(void)
580 {
581         /* make stack pointer 16-byte aligned */
582         gd->start_addr_sp -= 16;
583         gd->start_addr_sp &= ~0xf;
584
585         /*
586          * let the architecture-specific code tailor gd->start_addr_sp and
587          * gd->irq_sp
588          */
589         return arch_reserve_stacks();
590 }
591
592 static int reserve_bloblist(void)
593 {
594 #ifdef CONFIG_BLOBLIST
595         gd->start_addr_sp &= ~0xf;
596         gd->start_addr_sp -= CONFIG_BLOBLIST_SIZE;
597         gd->new_bloblist = map_sysmem(gd->start_addr_sp, CONFIG_BLOBLIST_SIZE);
598 #endif
599
600         return 0;
601 }
602
603 static int display_new_sp(void)
604 {
605         debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
606
607         return 0;
608 }
609
610 #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
611         defined(CONFIG_SH)
612 static int setup_board_part1(void)
613 {
614         bd_t *bd = gd->bd;
615
616         /*
617          * Save local variables to board info struct
618          */
619         bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;        /* start of memory */
620         bd->bi_memsize = gd->ram_size;                  /* size in bytes */
621
622 #ifdef CONFIG_SYS_SRAM_BASE
623         bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;        /* start of SRAM */
624         bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;         /* size  of SRAM */
625 #endif
626
627 #if defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
628         bd->bi_immr_base = CONFIG_SYS_IMMR;     /* base  of IMMR register     */
629 #endif
630 #if defined(CONFIG_M68K)
631         bd->bi_mbar_base = CONFIG_SYS_MBAR;     /* base of internal registers */
632 #endif
633 #if defined(CONFIG_MPC83xx)
634         bd->bi_immrbar = CONFIG_SYS_IMMR;
635 #endif
636
637         return 0;
638 }
639 #endif
640
641 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
642 static int setup_board_part2(void)
643 {
644         bd_t *bd = gd->bd;
645
646         bd->bi_intfreq = gd->cpu_clk;   /* Internal Freq, in Hz */
647         bd->bi_busfreq = gd->bus_clk;   /* Bus Freq,      in Hz */
648 #if defined(CONFIG_CPM2)
649         bd->bi_cpmfreq = gd->arch.cpm_clk;
650         bd->bi_brgfreq = gd->arch.brg_clk;
651         bd->bi_sccfreq = gd->arch.scc_clk;
652         bd->bi_vco = gd->arch.vco_out;
653 #endif /* CONFIG_CPM2 */
654 #if defined(CONFIG_M68K) && defined(CONFIG_PCI)
655         bd->bi_pcifreq = gd->pci_clk;
656 #endif
657 #if defined(CONFIG_EXTRA_CLOCK)
658         bd->bi_inpfreq = gd->arch.inp_clk;      /* input Freq in Hz */
659         bd->bi_vcofreq = gd->arch.vco_clk;      /* vco Freq in Hz */
660         bd->bi_flbfreq = gd->arch.flb_clk;      /* flexbus Freq in Hz */
661 #endif
662
663         return 0;
664 }
665 #endif
666
667 #ifdef CONFIG_POST
668 static int init_post(void)
669 {
670         post_bootmode_init();
671         post_run(NULL, POST_ROM | post_bootmode_get(0));
672
673         return 0;
674 }
675 #endif
676
677 static int reloc_fdt(void)
678 {
679 #ifndef CONFIG_OF_EMBED
680         if (gd->flags & GD_FLG_SKIP_RELOC)
681                 return 0;
682         if (gd->new_fdt) {
683                 memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
684                 gd->fdt_blob = gd->new_fdt;
685         }
686 #endif
687
688         return 0;
689 }
690
691 static int reloc_bootstage(void)
692 {
693 #ifdef CONFIG_BOOTSTAGE
694         if (gd->flags & GD_FLG_SKIP_RELOC)
695                 return 0;
696         if (gd->new_bootstage) {
697                 int size = bootstage_get_size();
698
699                 debug("Copying bootstage from %p to %p, size %x\n",
700                       gd->bootstage, gd->new_bootstage, size);
701                 memcpy(gd->new_bootstage, gd->bootstage, size);
702                 gd->bootstage = gd->new_bootstage;
703                 bootstage_relocate();
704         }
705 #endif
706
707         return 0;
708 }
709
710 static int reloc_bloblist(void)
711 {
712 #ifdef CONFIG_BLOBLIST
713         if (gd->flags & GD_FLG_SKIP_RELOC)
714                 return 0;
715         if (gd->new_bloblist) {
716                 int size = CONFIG_BLOBLIST_SIZE;
717
718                 debug("Copying bloblist from %p to %p, size %x\n",
719                       gd->bloblist, gd->new_bloblist, size);
720                 memcpy(gd->new_bloblist, gd->bloblist, size);
721                 gd->bloblist = gd->new_bloblist;
722         }
723 #endif
724
725         return 0;
726 }
727
728 static int setup_reloc(void)
729 {
730         if (gd->flags & GD_FLG_SKIP_RELOC) {
731                 debug("Skipping relocation due to flag\n");
732                 return 0;
733         }
734
735 #ifdef CONFIG_SYS_TEXT_BASE
736 #ifdef ARM
737         gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
738 #elif defined(CONFIG_M68K)
739         /*
740          * On all ColdFire arch cpu, monitor code starts always
741          * just after the default vector table location, so at 0x400
742          */
743         gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
744 #elif !defined(CONFIG_SANDBOX)
745         gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
746 #endif
747 #endif
748         memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
749
750         debug("Relocation Offset is: %08lx\n", gd->reloc_off);
751         debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
752               gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
753               gd->start_addr_sp);
754
755         return 0;
756 }
757
758 #ifdef CONFIG_OF_BOARD_FIXUP
759 static int fix_fdt(void)
760 {
761         return board_fix_fdt((void *)gd->fdt_blob);
762 }
763 #endif
764
765 /* ARM calls relocate_code from its crt0.S */
766 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
767                 !CONFIG_IS_ENABLED(X86_64)
768
769 static int jump_to_copy(void)
770 {
771         if (gd->flags & GD_FLG_SKIP_RELOC)
772                 return 0;
773         /*
774          * x86 is special, but in a nice way. It uses a trampoline which
775          * enables the dcache if possible.
776          *
777          * For now, other archs use relocate_code(), which is implemented
778          * similarly for all archs. When we do generic relocation, hopefully
779          * we can make all archs enable the dcache prior to relocation.
780          */
781 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
782         /*
783          * SDRAM and console are now initialised. The final stack can now
784          * be setup in SDRAM. Code execution will continue in Flash, but
785          * with the stack in SDRAM and Global Data in temporary memory
786          * (CPU cache)
787          */
788         arch_setup_gd(gd->new_gd);
789         board_init_f_r_trampoline(gd->start_addr_sp);
790 #else
791         relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
792 #endif
793
794         return 0;
795 }
796 #endif
797
798 /* Record the board_init_f() bootstage (after arch_cpu_init()) */
799 static int initf_bootstage(void)
800 {
801         bool from_spl = IS_ENABLED(CONFIG_SPL_BOOTSTAGE) &&
802                         IS_ENABLED(CONFIG_BOOTSTAGE_STASH);
803         int ret;
804
805         ret = bootstage_init(!from_spl);
806         if (ret)
807                 return ret;
808         if (from_spl) {
809                 const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
810                                                CONFIG_BOOTSTAGE_STASH_SIZE);
811
812                 ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
813                 if (ret && ret != -ENOENT) {
814                         debug("Failed to unstash bootstage: err=%d\n", ret);
815                         return ret;
816                 }
817         }
818
819         bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
820
821         return 0;
822 }
823
824 static int initf_console_record(void)
825 {
826 #if defined(CONFIG_CONSOLE_RECORD) && CONFIG_VAL(SYS_MALLOC_F_LEN)
827         return console_record_init();
828 #else
829         return 0;
830 #endif
831 }
832
833 static int initf_dm(void)
834 {
835 #if defined(CONFIG_DM) && CONFIG_VAL(SYS_MALLOC_F_LEN)
836         int ret;
837
838         bootstage_start(BOOTSTATE_ID_ACCUM_DM_F, "dm_f");
839         ret = dm_init_and_scan(true);
840         bootstage_accum(BOOTSTATE_ID_ACCUM_DM_F);
841         if (ret)
842                 return ret;
843 #endif
844 #ifdef CONFIG_TIMER_EARLY
845         ret = dm_timer_init();
846         if (ret)
847                 return ret;
848 #endif
849
850         return 0;
851 }
852
853 /* Architecture-specific memory reservation */
854 __weak int reserve_arch(void)
855 {
856         return 0;
857 }
858
859 __weak int arch_cpu_init_dm(void)
860 {
861         return 0;
862 }
863
864 static const init_fnc_t init_sequence_f[] = {
865         setup_mon_len,
866 #ifdef CONFIG_OF_CONTROL
867         fdtdec_setup,
868 #endif
869 #ifdef CONFIG_TRACE_EARLY
870         trace_early_init,
871 #endif
872         initf_malloc,
873         log_init,
874         initf_bootstage,        /* uses its own timer, so does not need DM */
875 #ifdef CONFIG_BLOBLIST
876         bloblist_init,
877 #endif
878         setup_spl_handoff,
879         initf_console_record,
880 #if defined(CONFIG_HAVE_FSP)
881         arch_fsp_init,
882 #endif
883         arch_cpu_init,          /* basic arch cpu dependent setup */
884         mach_cpu_init,          /* SoC/machine dependent CPU setup */
885         initf_dm,
886         arch_cpu_init_dm,
887 #if defined(CONFIG_BOARD_EARLY_INIT_F)
888         board_early_init_f,
889 #endif
890 #if defined(CONFIG_PPC) || defined(CONFIG_SYS_FSL_CLK) || defined(CONFIG_M68K)
891         /* get CPU and bus clocks according to the environment variable */
892         get_clocks,             /* get CPU and bus clocks (etc.) */
893 #endif
894 #if !defined(CONFIG_M68K)
895         timer_init,             /* initialize timer */
896 #endif
897 #if defined(CONFIG_BOARD_POSTCLK_INIT)
898         board_postclk_init,
899 #endif
900         env_init,               /* initialize environment */
901         init_baud_rate,         /* initialze baudrate settings */
902         serial_init,            /* serial communications setup */
903         console_init_f,         /* stage 1 init of console */
904         display_options,        /* say that we are here */
905         display_text_info,      /* show debugging info if required */
906 #if defined(CONFIG_PPC) || defined(CONFIG_SH) || defined(CONFIG_X86)
907         checkcpu,
908 #endif
909 #if defined(CONFIG_SYSRESET)
910         print_resetinfo,
911 #endif
912 #if defined(CONFIG_DISPLAY_CPUINFO)
913         print_cpuinfo,          /* display cpu info (and speed) */
914 #endif
915 #if defined(CONFIG_DTB_RESELECT)
916         embedded_dtb_select,
917 #endif
918 #if defined(CONFIG_DISPLAY_BOARDINFO)
919         show_board_info,
920 #endif
921         INIT_FUNC_WATCHDOG_INIT
922 #if defined(CONFIG_MISC_INIT_F)
923         misc_init_f,
924 #endif
925         INIT_FUNC_WATCHDOG_RESET
926 #if defined(CONFIG_SYS_I2C)
927         init_func_i2c,
928 #endif
929 #if defined(CONFIG_VID) && !defined(CONFIG_SPL)
930         init_func_vid,
931 #endif
932         announce_dram_init,
933         dram_init,              /* configure available RAM banks */
934 #ifdef CONFIG_POST
935         post_init_f,
936 #endif
937         INIT_FUNC_WATCHDOG_RESET
938 #if defined(CONFIG_SYS_DRAM_TEST)
939         testdram,
940 #endif /* CONFIG_SYS_DRAM_TEST */
941         INIT_FUNC_WATCHDOG_RESET
942
943 #ifdef CONFIG_POST
944         init_post,
945 #endif
946         INIT_FUNC_WATCHDOG_RESET
947         /*
948          * Now that we have DRAM mapped and working, we can
949          * relocate the code and continue running from DRAM.
950          *
951          * Reserve memory at end of RAM for (top down in that order):
952          *  - area that won't get touched by U-Boot and Linux (optional)
953          *  - kernel log buffer
954          *  - protected RAM
955          *  - LCD framebuffer
956          *  - monitor code
957          *  - board info struct
958          */
959         setup_dest_addr,
960 #ifdef CONFIG_PRAM
961         reserve_pram,
962 #endif
963         reserve_round_4k,
964 #ifdef CONFIG_ARM
965         reserve_mmu,
966 #endif
967         reserve_video,
968         reserve_trace,
969         reserve_uboot,
970         reserve_malloc,
971         reserve_board,
972         setup_machine,
973         reserve_global_data,
974         reserve_fdt,
975         reserve_bootstage,
976         reserve_bloblist,
977         reserve_arch,
978         reserve_stacks,
979         dram_init_banksize,
980         show_dram_config,
981 #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
982         defined(CONFIG_SH)
983         setup_board_part1,
984 #endif
985 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
986         INIT_FUNC_WATCHDOG_RESET
987         setup_board_part2,
988 #endif
989         display_new_sp,
990 #ifdef CONFIG_OF_BOARD_FIXUP
991         fix_fdt,
992 #endif
993         INIT_FUNC_WATCHDOG_RESET
994         reloc_fdt,
995         reloc_bootstage,
996         reloc_bloblist,
997         setup_reloc,
998 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
999         copy_uboot_to_ram,
1000         do_elf_reloc_fixups,
1001         clear_bss,
1002 #endif
1003 #if defined(CONFIG_XTENSA)
1004         clear_bss,
1005 #endif
1006 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
1007                 !CONFIG_IS_ENABLED(X86_64)
1008         jump_to_copy,
1009 #endif
1010         NULL,
1011 };
1012
1013 void board_init_f(ulong boot_flags)
1014 {
1015         gd->flags = boot_flags;
1016         gd->have_console = 0;
1017
1018         if (initcall_run_list(init_sequence_f))
1019                 hang();
1020
1021 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
1022                 !defined(CONFIG_EFI_APP) && !CONFIG_IS_ENABLED(X86_64) && \
1023                 !defined(CONFIG_ARC)
1024         /* NOTREACHED - jump_to_copy() does not return */
1025         hang();
1026 #endif
1027 }
1028
1029 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
1030 /*
1031  * For now this code is only used on x86.
1032  *
1033  * init_sequence_f_r is the list of init functions which are run when
1034  * U-Boot is executing from Flash with a semi-limited 'C' environment.
1035  * The following limitations must be considered when implementing an
1036  * '_f_r' function:
1037  *  - 'static' variables are read-only
1038  *  - Global Data (gd->xxx) is read/write
1039  *
1040  * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
1041  * supported).  It _should_, if possible, copy global data to RAM and
1042  * initialise the CPU caches (to speed up the relocation process)
1043  *
1044  * NOTE: At present only x86 uses this route, but it is intended that
1045  * all archs will move to this when generic relocation is implemented.
1046  */
1047 static const init_fnc_t init_sequence_f_r[] = {
1048 #if !CONFIG_IS_ENABLED(X86_64)
1049         init_cache_f_r,
1050 #endif
1051
1052         NULL,
1053 };
1054
1055 void board_init_f_r(void)
1056 {
1057         if (initcall_run_list(init_sequence_f_r))
1058                 hang();
1059
1060         /*
1061          * The pre-relocation drivers may be using memory that has now gone
1062          * away. Mark serial as unavailable - this will fall back to the debug
1063          * UART if available.
1064          *
1065          * Do the same with log drivers since the memory may not be available.
1066          */
1067         gd->flags &= ~(GD_FLG_SERIAL_READY | GD_FLG_LOG_READY);
1068 #ifdef CONFIG_TIMER
1069         gd->timer = NULL;
1070 #endif
1071
1072         /*
1073          * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
1074          * Transfer execution from Flash to RAM by calculating the address
1075          * of the in-RAM copy of board_init_r() and calling it
1076          */
1077         (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
1078
1079         /* NOTREACHED - board_init_r() does not return */
1080         hang();
1081 }
1082 #endif /* CONFIG_X86 */