xref: /rk3399_rockchip-uboot/common/spl/spl.c (revision 47f7bcae8c0de8b2a8af7ca309744f041a6d1424)
1*47f7bcaeSTom Rini /*
2*47f7bcaeSTom Rini  * (C) Copyright 2010
3*47f7bcaeSTom Rini  * Texas Instruments, <www.ti.com>
4*47f7bcaeSTom Rini  *
5*47f7bcaeSTom Rini  * Aneesh V <aneesh@ti.com>
6*47f7bcaeSTom Rini  *
7*47f7bcaeSTom Rini  * See file CREDITS for list of people who contributed to this
8*47f7bcaeSTom Rini  * project.
9*47f7bcaeSTom Rini  *
10*47f7bcaeSTom Rini  * This program is free software; you can redistribute it and/or
11*47f7bcaeSTom Rini  * modify it under the terms of the GNU General Public License as
12*47f7bcaeSTom Rini  * published by the Free Software Foundation; either version 2 of
13*47f7bcaeSTom Rini  * the License, or (at your option) any later version.
14*47f7bcaeSTom Rini  *
15*47f7bcaeSTom Rini  * This program is distributed in the hope that it will be useful,
16*47f7bcaeSTom Rini  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*47f7bcaeSTom Rini  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18*47f7bcaeSTom Rini  * GNU General Public License for more details.
19*47f7bcaeSTom Rini  *
20*47f7bcaeSTom Rini  * You should have received a copy of the GNU General Public License
21*47f7bcaeSTom Rini  * along with this program; if not, write to the Free Software
22*47f7bcaeSTom Rini  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23*47f7bcaeSTom Rini  * MA 02111-1307 USA
24*47f7bcaeSTom Rini  */
25*47f7bcaeSTom Rini #include <common.h>
26*47f7bcaeSTom Rini #include <spl.h>
27*47f7bcaeSTom Rini #include <asm/u-boot.h>
28*47f7bcaeSTom Rini #include <nand.h>
29*47f7bcaeSTom Rini #include <fat.h>
30*47f7bcaeSTom Rini #include <version.h>
31*47f7bcaeSTom Rini #include <i2c.h>
32*47f7bcaeSTom Rini #include <image.h>
33*47f7bcaeSTom Rini #include <malloc.h>
34*47f7bcaeSTom Rini #include <linux/compiler.h>
35*47f7bcaeSTom Rini 
36*47f7bcaeSTom Rini DECLARE_GLOBAL_DATA_PTR;
37*47f7bcaeSTom Rini 
38*47f7bcaeSTom Rini u32 *boot_params_ptr = NULL;
39*47f7bcaeSTom Rini struct spl_image_info spl_image;
40*47f7bcaeSTom Rini 
41*47f7bcaeSTom Rini /* Define global data structure pointer to it*/
42*47f7bcaeSTom Rini static gd_t gdata __attribute__ ((section(".data")));
43*47f7bcaeSTom Rini static bd_t bdata __attribute__ ((section(".data")));
44*47f7bcaeSTom Rini 
45*47f7bcaeSTom Rini inline void hang(void)
46*47f7bcaeSTom Rini {
47*47f7bcaeSTom Rini 	puts("### ERROR ### Please RESET the board ###\n");
48*47f7bcaeSTom Rini 	for (;;)
49*47f7bcaeSTom Rini 		;
50*47f7bcaeSTom Rini }
51*47f7bcaeSTom Rini 
52*47f7bcaeSTom Rini void board_init_f(ulong dummy)
53*47f7bcaeSTom Rini {
54*47f7bcaeSTom Rini 	/*
55*47f7bcaeSTom Rini 	 * We call relocate_code() with relocation target same as the
56*47f7bcaeSTom Rini 	 * CONFIG_SYS_SPL_TEXT_BASE. This will result in relocation getting
57*47f7bcaeSTom Rini 	 * skipped. Instead, only .bss initialization will happen. That's
58*47f7bcaeSTom Rini 	 * all we need
59*47f7bcaeSTom Rini 	 */
60*47f7bcaeSTom Rini 	debug(">>board_init_f()\n");
61*47f7bcaeSTom Rini 	relocate_code(CONFIG_SPL_STACK, &gdata, CONFIG_SPL_TEXT_BASE);
62*47f7bcaeSTom Rini }
63*47f7bcaeSTom Rini 
64*47f7bcaeSTom Rini /*
65*47f7bcaeSTom Rini  * Default function to determine if u-boot or the OS should
66*47f7bcaeSTom Rini  * be started. This implementation always returns 1.
67*47f7bcaeSTom Rini  *
68*47f7bcaeSTom Rini  * Please implement your own board specific funcion to do this.
69*47f7bcaeSTom Rini  *
70*47f7bcaeSTom Rini  * RETURN
71*47f7bcaeSTom Rini  * 0 to not start u-boot
72*47f7bcaeSTom Rini  * positive if u-boot should start
73*47f7bcaeSTom Rini  */
74*47f7bcaeSTom Rini #ifdef CONFIG_SPL_OS_BOOT
75*47f7bcaeSTom Rini __weak int spl_start_uboot(void)
76*47f7bcaeSTom Rini {
77*47f7bcaeSTom Rini 	puts("SPL: Please implement spl_start_uboot() for your board\n");
78*47f7bcaeSTom Rini 	puts("SPL: Direct Linux boot not active!\n");
79*47f7bcaeSTom Rini 	return 1;
80*47f7bcaeSTom Rini }
81*47f7bcaeSTom Rini #endif
82*47f7bcaeSTom Rini 
83*47f7bcaeSTom Rini void spl_parse_image_header(const struct image_header *header)
84*47f7bcaeSTom Rini {
85*47f7bcaeSTom Rini 	u32 header_size = sizeof(struct image_header);
86*47f7bcaeSTom Rini 
87*47f7bcaeSTom Rini 	if (__be32_to_cpu(header->ih_magic) == IH_MAGIC) {
88*47f7bcaeSTom Rini 		spl_image.size = __be32_to_cpu(header->ih_size) + header_size;
89*47f7bcaeSTom Rini 		spl_image.entry_point = __be32_to_cpu(header->ih_load);
90*47f7bcaeSTom Rini 		/* Load including the header */
91*47f7bcaeSTom Rini 		spl_image.load_addr = spl_image.entry_point - header_size;
92*47f7bcaeSTom Rini 		spl_image.os = header->ih_os;
93*47f7bcaeSTom Rini 		spl_image.name = (const char *)&header->ih_name;
94*47f7bcaeSTom Rini 		debug("spl: payload image: %s load addr: 0x%x size: %d\n",
95*47f7bcaeSTom Rini 			spl_image.name, spl_image.load_addr, spl_image.size);
96*47f7bcaeSTom Rini 	} else {
97*47f7bcaeSTom Rini 		/* Signature not found - assume u-boot.bin */
98*47f7bcaeSTom Rini 		puts("mkimage signature not found, assuming u-boot.bin ..\n");
99*47f7bcaeSTom Rini 		debug("mkimage signature not found - ih_magic = %x\n",
100*47f7bcaeSTom Rini 			header->ih_magic);
101*47f7bcaeSTom Rini 		/* Let's assume U-Boot will not be more than 200 KB */
102*47f7bcaeSTom Rini 		spl_image.size = 200 * 1024;
103*47f7bcaeSTom Rini 		spl_image.entry_point = CONFIG_SYS_TEXT_BASE;
104*47f7bcaeSTom Rini 		spl_image.load_addr = CONFIG_SYS_TEXT_BASE;
105*47f7bcaeSTom Rini 		spl_image.os = IH_OS_U_BOOT;
106*47f7bcaeSTom Rini 		spl_image.name = "U-Boot";
107*47f7bcaeSTom Rini 	}
108*47f7bcaeSTom Rini }
109*47f7bcaeSTom Rini 
110*47f7bcaeSTom Rini /*
111*47f7bcaeSTom Rini  * This function jumps to an image with argument. Normally an FDT or ATAGS
112*47f7bcaeSTom Rini  * image.
113*47f7bcaeSTom Rini  * arg: Pointer to paramter image in RAM
114*47f7bcaeSTom Rini  */
115*47f7bcaeSTom Rini #ifdef CONFIG_SPL_OS_BOOT
116*47f7bcaeSTom Rini static void __noreturn jump_to_image_linux(void *arg)
117*47f7bcaeSTom Rini {
118*47f7bcaeSTom Rini 	debug("Entering kernel arg pointer: 0x%p\n", arg);
119*47f7bcaeSTom Rini 	typedef void (*image_entry_arg_t)(int, int, void *)
120*47f7bcaeSTom Rini 		__attribute__ ((noreturn));
121*47f7bcaeSTom Rini 	image_entry_arg_t image_entry =
122*47f7bcaeSTom Rini 		(image_entry_arg_t) spl_image.entry_point;
123*47f7bcaeSTom Rini 	cleanup_before_linux();
124*47f7bcaeSTom Rini 	image_entry(0, CONFIG_MACH_TYPE, arg);
125*47f7bcaeSTom Rini }
126*47f7bcaeSTom Rini #endif
127*47f7bcaeSTom Rini 
128*47f7bcaeSTom Rini static void __noreturn jump_to_image_no_args(void)
129*47f7bcaeSTom Rini {
130*47f7bcaeSTom Rini 	typedef void __noreturn (*image_entry_noargs_t)(u32 *);
131*47f7bcaeSTom Rini 	image_entry_noargs_t image_entry =
132*47f7bcaeSTom Rini 			(image_entry_noargs_t) spl_image.entry_point;
133*47f7bcaeSTom Rini 
134*47f7bcaeSTom Rini 	debug("image entry point: 0x%X\n", spl_image.entry_point);
135*47f7bcaeSTom Rini 	/* Pass the saved boot_params from rom code */
136*47f7bcaeSTom Rini #if defined(CONFIG_VIRTIO) || defined(CONFIG_ZEBU)
137*47f7bcaeSTom Rini 	image_entry = (image_entry_noargs_t)0x80100000;
138*47f7bcaeSTom Rini #endif
139*47f7bcaeSTom Rini 	u32 boot_params_ptr_addr = (u32)&boot_params_ptr;
140*47f7bcaeSTom Rini 	image_entry((u32 *)boot_params_ptr_addr);
141*47f7bcaeSTom Rini }
142*47f7bcaeSTom Rini 
143*47f7bcaeSTom Rini void board_init_r(gd_t *id, ulong dummy)
144*47f7bcaeSTom Rini {
145*47f7bcaeSTom Rini 	u32 boot_device;
146*47f7bcaeSTom Rini 	debug(">>spl:board_init_r()\n");
147*47f7bcaeSTom Rini 
148*47f7bcaeSTom Rini #ifdef CONFIG_SYS_SPL_MALLOC_START
149*47f7bcaeSTom Rini 	mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
150*47f7bcaeSTom Rini 			CONFIG_SYS_SPL_MALLOC_SIZE);
151*47f7bcaeSTom Rini #endif
152*47f7bcaeSTom Rini 
153*47f7bcaeSTom Rini #ifdef CONFIG_SPL_BOARD_INIT
154*47f7bcaeSTom Rini 	spl_board_init();
155*47f7bcaeSTom Rini #endif
156*47f7bcaeSTom Rini 
157*47f7bcaeSTom Rini 	boot_device = spl_boot_device();
158*47f7bcaeSTom Rini 	debug("boot device - %d\n", boot_device);
159*47f7bcaeSTom Rini 	switch (boot_device) {
160*47f7bcaeSTom Rini #ifdef CONFIG_SPL_MMC_SUPPORT
161*47f7bcaeSTom Rini 	case BOOT_DEVICE_MMC1:
162*47f7bcaeSTom Rini 	case BOOT_DEVICE_MMC2:
163*47f7bcaeSTom Rini 	case BOOT_DEVICE_MMC2_2:
164*47f7bcaeSTom Rini 		spl_mmc_load_image();
165*47f7bcaeSTom Rini 		break;
166*47f7bcaeSTom Rini #endif
167*47f7bcaeSTom Rini #ifdef CONFIG_SPL_NAND_SUPPORT
168*47f7bcaeSTom Rini 	case BOOT_DEVICE_NAND:
169*47f7bcaeSTom Rini 		spl_nand_load_image();
170*47f7bcaeSTom Rini 		break;
171*47f7bcaeSTom Rini #endif
172*47f7bcaeSTom Rini #ifdef CONFIG_SPL_YMODEM_SUPPORT
173*47f7bcaeSTom Rini 	case BOOT_DEVICE_UART:
174*47f7bcaeSTom Rini 		spl_ymodem_load_image();
175*47f7bcaeSTom Rini 		break;
176*47f7bcaeSTom Rini #endif
177*47f7bcaeSTom Rini #ifdef CONFIG_SPL_SPI_SUPPORT
178*47f7bcaeSTom Rini 	case BOOT_DEVICE_SPI:
179*47f7bcaeSTom Rini 		spi_boot();
180*47f7bcaeSTom Rini 	break;
181*47f7bcaeSTom Rini #endif
182*47f7bcaeSTom Rini 	default:
183*47f7bcaeSTom Rini 		puts("SPL: Un-supported Boot Device\n");
184*47f7bcaeSTom Rini 		debug("Found: %d\n", boot_device);
185*47f7bcaeSTom Rini 		hang();
186*47f7bcaeSTom Rini 		break;
187*47f7bcaeSTom Rini 	}
188*47f7bcaeSTom Rini 
189*47f7bcaeSTom Rini 	switch (spl_image.os) {
190*47f7bcaeSTom Rini 	case IH_OS_U_BOOT:
191*47f7bcaeSTom Rini 		debug("Jumping to U-Boot\n");
192*47f7bcaeSTom Rini 		jump_to_image_no_args();
193*47f7bcaeSTom Rini 		break;
194*47f7bcaeSTom Rini #ifdef CONFIG_SPL_OS_BOOT
195*47f7bcaeSTom Rini 	case IH_OS_LINUX:
196*47f7bcaeSTom Rini 		debug("Jumping to Linux\n");
197*47f7bcaeSTom Rini 		spl_board_prepare_for_linux();
198*47f7bcaeSTom Rini 		jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
199*47f7bcaeSTom Rini 		break;
200*47f7bcaeSTom Rini #endif
201*47f7bcaeSTom Rini 	default:
202*47f7bcaeSTom Rini 		puts("Unsupported OS image.. Jumping nevertheless..\n");
203*47f7bcaeSTom Rini 		jump_to_image_no_args();
204*47f7bcaeSTom Rini 	}
205*47f7bcaeSTom Rini }
206*47f7bcaeSTom Rini 
207*47f7bcaeSTom Rini /* This requires UART clocks to be enabled */
208*47f7bcaeSTom Rini void preloader_console_init(void)
209*47f7bcaeSTom Rini {
210*47f7bcaeSTom Rini 	gd = &gdata;
211*47f7bcaeSTom Rini 	gd->bd = &bdata;
212*47f7bcaeSTom Rini 	gd->flags |= GD_FLG_RELOC;
213*47f7bcaeSTom Rini 	gd->baudrate = CONFIG_BAUDRATE;
214*47f7bcaeSTom Rini 
215*47f7bcaeSTom Rini 	serial_init();		/* serial communications setup */
216*47f7bcaeSTom Rini 
217*47f7bcaeSTom Rini 	gd->have_console = 1;
218*47f7bcaeSTom Rini 
219*47f7bcaeSTom Rini 	puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
220*47f7bcaeSTom Rini 			U_BOOT_TIME ")\n");
221*47f7bcaeSTom Rini #ifdef CONFIG_SPL_DISPLAY_PRINT
222*47f7bcaeSTom Rini 	spl_display_print();
223*47f7bcaeSTom Rini #endif
224*47f7bcaeSTom Rini }
225