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