xref: /openbmc/u-boot/arch/x86/lib/init_helpers.c (revision 86bedaeb)
1 /*
2  * (C) Copyright 2011
3  * Graeme Russ, <graeme.russ@gmail.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 #include <common.h>
8 #include <fdtdec.h>
9 #include <spi.h>
10 #include <asm/errno.h>
11 #include <asm/mtrr.h>
12 #include <asm/sections.h>
13 
14 DECLARE_GLOBAL_DATA_PTR;
15 
16 /* Get the top of usable RAM */
17 __weak ulong board_get_usable_ram_top(ulong total_size)
18 {
19 	return gd->ram_size;
20 }
21 
22 int calculate_relocation_address(void)
23 {
24 	const ulong uboot_size = (uintptr_t)&__bss_end -
25 			(uintptr_t)&__text_start;
26 	ulong total_size;
27 	ulong dest_addr;
28 	ulong fdt_size = 0;
29 
30 #if defined(CONFIG_OF_SEPARATE) && defined(CONFIG_OF_CONTROL)
31 	if (gd->fdt_blob)
32 		fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
33 #endif
34 	total_size = ALIGN(uboot_size, 1 << 12) + CONFIG_SYS_MALLOC_LEN +
35 		CONFIG_SYS_STACK_SIZE + fdt_size;
36 
37 	dest_addr = board_get_usable_ram_top(total_size);
38 	/*
39 	 * NOTE: All destination address are rounded down to 16-byte
40 	 *       boundary to satisfy various worst-case alignment
41 	 *       requirements
42 	 */
43 	dest_addr &= ~15;
44 
45 #if defined(CONFIG_OF_SEPARATE) && defined(CONFIG_OF_CONTROL)
46 	/*
47 	 * If the device tree is sitting immediate above our image then we
48 	 * must relocate it. If it is embedded in the data section, then it
49 	 * will be relocated with other data.
50 	 */
51 	if (gd->fdt_blob) {
52 		dest_addr -= fdt_size;
53 		gd->new_fdt = (void *)dest_addr;
54 		dest_addr &= ~15;
55 	}
56 #endif
57 	/* U-Boot is below the FDT */
58 	dest_addr -= uboot_size;
59 	dest_addr &= ~((1 << 12) - 1);
60 	gd->relocaddr = dest_addr;
61 	gd->reloc_off = dest_addr - (uintptr_t)&__text_start;
62 
63 	/* Stack is at the bottom, so it can grow down */
64 	gd->start_addr_sp = dest_addr - CONFIG_SYS_MALLOC_LEN;
65 
66 	return 0;
67 }
68 
69 int init_cache_f_r(void)
70 {
71 #if defined(CONFIG_X86_RESET_VECTOR) & !defined(CONFIG_HAVE_FSP)
72 	int ret;
73 
74 	ret = mtrr_commit(false);
75 	/* If MTRR MSR is not implemented by the processor, just ignore it */
76 	if (ret && ret != -ENOSYS)
77 		return ret;
78 #endif
79 	/* Initialise the CPU cache(s) */
80 	return init_cache();
81 }
82 
83 bd_t bd_data;
84 
85 int init_bd_struct_r(void)
86 {
87 	gd->bd = &bd_data;
88 	memset(gd->bd, 0, sizeof(bd_t));
89 
90 	return 0;
91 }
92 
93 int init_func_spi(void)
94 {
95 	puts("SPI:   ");
96 	spi_init();
97 	puts("ready\n");
98 	return 0;
99 }
100