xref: /openbmc/u-boot/arch/x86/lib/init_helpers.c (revision 86cfb6bd)
1d47ab0ecSGraeme Russ /*
2d47ab0ecSGraeme Russ  * (C) Copyright 2011
3d47ab0ecSGraeme Russ  * Graeme Russ, <graeme.russ@gmail.com>
4d47ab0ecSGraeme Russ  *
5d47ab0ecSGraeme Russ  * See file CREDITS for list of people who contributed to this
6d47ab0ecSGraeme Russ  * project.
7d47ab0ecSGraeme Russ  *
8d47ab0ecSGraeme Russ  * This program is free software; you can redistribute it and/or
9d47ab0ecSGraeme Russ  * modify it under the terms of the GNU General Public License as
10d47ab0ecSGraeme Russ  * published by the Free Software Foundation; either version 2 of
11d47ab0ecSGraeme Russ  * the License, or (at your option) any later version.
12d47ab0ecSGraeme Russ  *
13d47ab0ecSGraeme Russ  * This program is distributed in the hope that it will be useful,
14d47ab0ecSGraeme Russ  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15d47ab0ecSGraeme Russ  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16d47ab0ecSGraeme Russ  * GNU General Public License for more details.
17d47ab0ecSGraeme Russ  *
18d47ab0ecSGraeme Russ  * You should have received a copy of the GNU General Public License
19d47ab0ecSGraeme Russ  * along with this program; if not, write to the Free Software
20d47ab0ecSGraeme Russ  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21d47ab0ecSGraeme Russ  * MA 02111-1307 USA
22d47ab0ecSGraeme Russ  */
23d47ab0ecSGraeme Russ #include <common.h>
24d47ab0ecSGraeme Russ #include <command.h>
25f697d528SSimon Glass #include <fdtdec.h>
26d47ab0ecSGraeme Russ #include <stdio_dev.h>
27d47ab0ecSGraeme Russ #include <version.h>
28d47ab0ecSGraeme Russ #include <malloc.h>
29d47ab0ecSGraeme Russ #include <net.h>
30d47ab0ecSGraeme Russ #include <ide.h>
31d47ab0ecSGraeme Russ #include <serial.h>
328313315bSGabe Black #include <spi.h>
33d47ab0ecSGraeme Russ #include <status_led.h>
34a1d57b7aSGraeme Russ #include <asm/processor.h>
35*86cfb6bdSSimon Glass #include <asm/sections.h>
36d47ab0ecSGraeme Russ #include <asm/u-boot-x86.h>
37d65297b6SGabe Black #include <linux/compiler.h>
38d47ab0ecSGraeme Russ 
39d47ab0ecSGraeme Russ #include <asm/init_helpers.h>
40d47ab0ecSGraeme Russ 
41d47ab0ecSGraeme Russ DECLARE_GLOBAL_DATA_PTR;
42d47ab0ecSGraeme Russ 
43d47ab0ecSGraeme Russ /************************************************************************
44d47ab0ecSGraeme Russ  * Init Utilities							*
45d47ab0ecSGraeme Russ  ************************************************************************
46d47ab0ecSGraeme Russ  * Some of this code should be moved into the core functions,
47d47ab0ecSGraeme Russ  * or dropped completely,
48d47ab0ecSGraeme Russ  * but let's get it working (again) first...
49d47ab0ecSGraeme Russ  */
50d47ab0ecSGraeme Russ 
51d47ab0ecSGraeme Russ int display_banner(void)
52d47ab0ecSGraeme Russ {
53d47ab0ecSGraeme Russ 	printf("\n\n%s\n\n", version_string);
54d47ab0ecSGraeme Russ 
55d47ab0ecSGraeme Russ 	return 0;
56d47ab0ecSGraeme Russ }
57d47ab0ecSGraeme Russ 
58d47ab0ecSGraeme Russ int display_dram_config(void)
59d47ab0ecSGraeme Russ {
60d47ab0ecSGraeme Russ 	int i;
61d47ab0ecSGraeme Russ 
62d47ab0ecSGraeme Russ 	puts("DRAM Configuration:\n");
63d47ab0ecSGraeme Russ 
64d47ab0ecSGraeme Russ 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
65d47ab0ecSGraeme Russ 		printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
66d47ab0ecSGraeme Russ 		print_size(gd->bd->bi_dram[i].size, "\n");
67d47ab0ecSGraeme Russ 	}
68d47ab0ecSGraeme Russ 
69d47ab0ecSGraeme Russ 	return 0;
70d47ab0ecSGraeme Russ }
71d47ab0ecSGraeme Russ 
72d47ab0ecSGraeme Russ int init_baudrate_f(void)
73d47ab0ecSGraeme Russ {
74d47ab0ecSGraeme Russ 	gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
75d47ab0ecSGraeme Russ 	return 0;
76d47ab0ecSGraeme Russ }
77d47ab0ecSGraeme Russ 
785e98947fSSimon Glass /* Get the top of usable RAM */
795e98947fSSimon Glass __weak ulong board_get_usable_ram_top(ulong total_size)
80a1d57b7aSGraeme Russ {
815e98947fSSimon Glass 	return gd->ram_size;
825e98947fSSimon Glass }
835e98947fSSimon Glass 
845e98947fSSimon Glass int calculate_relocation_address(void)
855e98947fSSimon Glass {
865e98947fSSimon Glass 	const ulong uboot_size = (uintptr_t)&__bss_end -
875e98947fSSimon Glass 			(uintptr_t)&__text_start;
885e98947fSSimon Glass 	ulong total_size;
89a1d57b7aSGraeme Russ 	ulong dest_addr;
90f697d528SSimon Glass 	ulong fdt_size = 0;
91a1d57b7aSGraeme Russ 
92f697d528SSimon Glass #if defined(CONFIG_OF_SEPARATE) && defined(CONFIG_OF_CONTROL)
93f697d528SSimon Glass 	if (gd->fdt_blob)
94f697d528SSimon Glass 		fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
95f697d528SSimon Glass #endif
965e98947fSSimon Glass 	total_size = ALIGN(uboot_size, 1 << 12) + CONFIG_SYS_MALLOC_LEN +
97f697d528SSimon Glass 		CONFIG_SYS_STACK_SIZE + fdt_size;
985e98947fSSimon Glass 
99f697d528SSimon Glass 	dest_addr = board_get_usable_ram_top(total_size);
100a1d57b7aSGraeme Russ 	/*
101a1d57b7aSGraeme Russ 	 * NOTE: All destination address are rounded down to 16-byte
102a1d57b7aSGraeme Russ 	 *       boundary to satisfy various worst-case alignment
103a1d57b7aSGraeme Russ 	 *       requirements
104a1d57b7aSGraeme Russ 	 */
105f697d528SSimon Glass 	dest_addr &= ~15;
106a1d57b7aSGraeme Russ 
107f697d528SSimon Glass #if defined(CONFIG_OF_SEPARATE) && defined(CONFIG_OF_CONTROL)
108f697d528SSimon Glass 	/*
109f697d528SSimon Glass 	 * If the device tree is sitting immediate above our image then we
110f697d528SSimon Glass 	 * must relocate it. If it is embedded in the data section, then it
111f697d528SSimon Glass 	 * will be relocated with other data.
112f697d528SSimon Glass 	 */
113f697d528SSimon Glass 	if (gd->fdt_blob) {
114f697d528SSimon Glass 		dest_addr -= fdt_size;
1151938f4a5SSimon Glass 		gd->new_fdt = (void *)dest_addr;
116f697d528SSimon Glass 		dest_addr &= ~15;
117f697d528SSimon Glass 	}
118f697d528SSimon Glass #endif
1195e98947fSSimon Glass 	/* U-Boot is below the FDT */
1205e98947fSSimon Glass 	dest_addr -= uboot_size;
1215e98947fSSimon Glass 	dest_addr &= ~((1 << 12) - 1);
122a1d57b7aSGraeme Russ 	gd->relocaddr = dest_addr;
1235e98947fSSimon Glass 	gd->reloc_off = dest_addr - (uintptr_t)&__text_start;
124a1d57b7aSGraeme Russ 
12532f98735SGabe Black 	/* Stack is at the bottom, so it can grow down */
12632f98735SGabe Black 	gd->start_addr_sp = dest_addr - CONFIG_SYS_MALLOC_LEN;
12732f98735SGabe Black 
128a1d57b7aSGraeme Russ 	return 0;
129a1d57b7aSGraeme Russ }
130a1d57b7aSGraeme Russ 
131a1d57b7aSGraeme Russ int init_cache_f_r(void)
132a1d57b7aSGraeme Russ {
133a1d57b7aSGraeme Russ 	/* Initialise the CPU cache(s) */
134a1d57b7aSGraeme Russ 	return init_cache();
135a1d57b7aSGraeme Russ }
136a1d57b7aSGraeme Russ 
137a1d57b7aSGraeme Russ int set_reloc_flag_r(void)
138a1d57b7aSGraeme Russ {
139a1d57b7aSGraeme Russ 	gd->flags = GD_FLG_RELOC;
140a1d57b7aSGraeme Russ 
141a1d57b7aSGraeme Russ 	return 0;
142a1d57b7aSGraeme Russ }
143a1d57b7aSGraeme Russ 
144d47ab0ecSGraeme Russ int mem_malloc_init_r(void)
145d47ab0ecSGraeme Russ {
146d47ab0ecSGraeme Russ 	mem_malloc_init(((gd->relocaddr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
147d47ab0ecSGraeme Russ 			CONFIG_SYS_MALLOC_LEN);
148d47ab0ecSGraeme Russ 
149d47ab0ecSGraeme Russ 	return 0;
150d47ab0ecSGraeme Russ }
151d47ab0ecSGraeme Russ 
152d47ab0ecSGraeme Russ bd_t bd_data;
153d47ab0ecSGraeme Russ 
154d47ab0ecSGraeme Russ int init_bd_struct_r(void)
155d47ab0ecSGraeme Russ {
156d47ab0ecSGraeme Russ 	gd->bd = &bd_data;
157d47ab0ecSGraeme Russ 	memset(gd->bd, 0, sizeof(bd_t));
158d47ab0ecSGraeme Russ 
159d47ab0ecSGraeme Russ 	return 0;
160d47ab0ecSGraeme Russ }
161d47ab0ecSGraeme Russ 
162d47ab0ecSGraeme Russ #ifndef CONFIG_SYS_NO_FLASH
163d47ab0ecSGraeme Russ int flash_init_r(void)
164d47ab0ecSGraeme Russ {
165d47ab0ecSGraeme Russ 	ulong size;
166d47ab0ecSGraeme Russ 
167d47ab0ecSGraeme Russ 	puts("Flash: ");
168d47ab0ecSGraeme Russ 
169d47ab0ecSGraeme Russ 	/* configure available FLASH banks */
170d47ab0ecSGraeme Russ 	size = flash_init();
171d47ab0ecSGraeme Russ 
172d47ab0ecSGraeme Russ 	print_size(size, "\n");
173d47ab0ecSGraeme Russ 
174d47ab0ecSGraeme Russ 	return 0;
175d47ab0ecSGraeme Russ }
176d47ab0ecSGraeme Russ #endif
177d47ab0ecSGraeme Russ 
178d47ab0ecSGraeme Russ #ifdef CONFIG_STATUS_LED
179d47ab0ecSGraeme Russ int status_led_set_r(void)
180d47ab0ecSGraeme Russ {
181d47ab0ecSGraeme Russ 	status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
182d47ab0ecSGraeme Russ 
183d47ab0ecSGraeme Russ 	return 0;
184d47ab0ecSGraeme Russ }
185d47ab0ecSGraeme Russ #endif
186d47ab0ecSGraeme Russ 
187d47ab0ecSGraeme Russ int set_load_addr_r(void)
188d47ab0ecSGraeme Russ {
189d47ab0ecSGraeme Russ 	/* Initialize from environment */
190d47ab0ecSGraeme Russ 	load_addr = getenv_ulong("loadaddr", 16, load_addr);
191d47ab0ecSGraeme Russ 
192d47ab0ecSGraeme Russ 	return 0;
193d47ab0ecSGraeme Russ }
1948313315bSGabe Black 
1958313315bSGabe Black int init_func_spi(void)
1968313315bSGabe Black {
1978313315bSGabe Black 	puts("SPI:   ");
1988313315bSGabe Black 	spi_init();
1998313315bSGabe Black 	puts("ready\n");
2008313315bSGabe Black 	return 0;
2018313315bSGabe Black }
202b208c7f1SGabe Black 
203b208c7f1SGabe Black #ifdef CONFIG_OF_CONTROL
204b208c7f1SGabe Black int find_fdt(void)
205b208c7f1SGabe Black {
206b208c7f1SGabe Black #ifdef CONFIG_OF_EMBED
207b208c7f1SGabe Black 	/* Get a pointer to the FDT */
208b208c7f1SGabe Black 	gd->fdt_blob = _binary_dt_dtb_start;
209b208c7f1SGabe Black #elif defined CONFIG_OF_SEPARATE
210b208c7f1SGabe Black 	/* FDT is at end of image */
2114b491b8dSSimon Glass 	gd->fdt_blob = (ulong *)&_end;
212b208c7f1SGabe Black #endif
213b208c7f1SGabe Black 	/* Allow the early environment to override the fdt address */
214b208c7f1SGabe Black 	gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
215b208c7f1SGabe Black 						(uintptr_t)gd->fdt_blob);
216b208c7f1SGabe Black 
217b208c7f1SGabe Black 	return 0;
218b208c7f1SGabe Black }
219b208c7f1SGabe Black 
220b208c7f1SGabe Black int prepare_fdt(void)
221b208c7f1SGabe Black {
222b208c7f1SGabe Black 	/* For now, put this check after the console is ready */
223b208c7f1SGabe Black 	if (fdtdec_prepare_fdt()) {
224b208c7f1SGabe Black 		panic("** CONFIG_OF_CONTROL defined but no FDT - please see "
225b208c7f1SGabe Black 			"doc/README.fdt-control");
226b208c7f1SGabe Black 	}
227b208c7f1SGabe Black 
228b208c7f1SGabe Black 	return 0;
229b208c7f1SGabe Black }
230b208c7f1SGabe Black #endif
231