xref: /rk3399_rockchip-uboot/common/spl/spl.c (revision a759f1e0db7be05ab562e060cf024af262920426)
147f7bcaeSTom Rini /*
247f7bcaeSTom Rini  * (C) Copyright 2010
347f7bcaeSTom Rini  * Texas Instruments, <www.ti.com>
447f7bcaeSTom Rini  *
547f7bcaeSTom Rini  * Aneesh V <aneesh@ti.com>
647f7bcaeSTom Rini  *
747f7bcaeSTom Rini  * See file CREDITS for list of people who contributed to this
847f7bcaeSTom Rini  * project.
947f7bcaeSTom Rini  *
1047f7bcaeSTom Rini  * This program is free software; you can redistribute it and/or
1147f7bcaeSTom Rini  * modify it under the terms of the GNU General Public License as
1247f7bcaeSTom Rini  * published by the Free Software Foundation; either version 2 of
1347f7bcaeSTom Rini  * the License, or (at your option) any later version.
1447f7bcaeSTom Rini  *
1547f7bcaeSTom Rini  * This program is distributed in the hope that it will be useful,
1647f7bcaeSTom Rini  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1747f7bcaeSTom Rini  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1847f7bcaeSTom Rini  * GNU General Public License for more details.
1947f7bcaeSTom Rini  *
2047f7bcaeSTom Rini  * You should have received a copy of the GNU General Public License
2147f7bcaeSTom Rini  * along with this program; if not, write to the Free Software
2247f7bcaeSTom Rini  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
2347f7bcaeSTom Rini  * MA 02111-1307 USA
2447f7bcaeSTom Rini  */
2547f7bcaeSTom Rini #include <common.h>
2647f7bcaeSTom Rini #include <spl.h>
2747f7bcaeSTom Rini #include <asm/u-boot.h>
2847f7bcaeSTom Rini #include <nand.h>
2947f7bcaeSTom Rini #include <fat.h>
3047f7bcaeSTom Rini #include <version.h>
3147f7bcaeSTom Rini #include <i2c.h>
3247f7bcaeSTom Rini #include <image.h>
3347f7bcaeSTom Rini #include <malloc.h>
3447f7bcaeSTom Rini #include <linux/compiler.h>
3547f7bcaeSTom Rini 
3647f7bcaeSTom Rini DECLARE_GLOBAL_DATA_PTR;
3747f7bcaeSTom Rini 
383c6f8a0dSStefan Roese #ifndef CONFIG_SYS_UBOOT_START
393c6f8a0dSStefan Roese #define CONFIG_SYS_UBOOT_START	CONFIG_SYS_TEXT_BASE
403c6f8a0dSStefan Roese #endif
41ae83d882SStefano Babic #ifndef CONFIG_SYS_MONITOR_LEN
42ae83d882SStefano Babic #define CONFIG_SYS_MONITOR_LEN	(200 * 1024)
43ae83d882SStefano Babic #endif
44ae83d882SStefano Babic 
4547f7bcaeSTom Rini u32 *boot_params_ptr = NULL;
4647f7bcaeSTom Rini struct spl_image_info spl_image;
4747f7bcaeSTom Rini 
486507f133STom Rini /* Define board data structure */
4947f7bcaeSTom Rini static bd_t bdata __attribute__ ((section(".data")));
5047f7bcaeSTom Rini 
5147f7bcaeSTom Rini inline void hang(void)
5247f7bcaeSTom Rini {
5347f7bcaeSTom Rini 	puts("### ERROR ### Please RESET the board ###\n");
5447f7bcaeSTom Rini 	for (;;)
5547f7bcaeSTom Rini 		;
5647f7bcaeSTom Rini }
5747f7bcaeSTom Rini 
5847f7bcaeSTom Rini /*
5947f7bcaeSTom Rini  * Default function to determine if u-boot or the OS should
6047f7bcaeSTom Rini  * be started. This implementation always returns 1.
6147f7bcaeSTom Rini  *
6247f7bcaeSTom Rini  * Please implement your own board specific funcion to do this.
6347f7bcaeSTom Rini  *
6447f7bcaeSTom Rini  * RETURN
6547f7bcaeSTom Rini  * 0 to not start u-boot
6647f7bcaeSTom Rini  * positive if u-boot should start
6747f7bcaeSTom Rini  */
6847f7bcaeSTom Rini #ifdef CONFIG_SPL_OS_BOOT
6947f7bcaeSTom Rini __weak int spl_start_uboot(void)
7047f7bcaeSTom Rini {
7147f7bcaeSTom Rini 	puts("SPL: Please implement spl_start_uboot() for your board\n");
7247f7bcaeSTom Rini 	puts("SPL: Direct Linux boot not active!\n");
7347f7bcaeSTom Rini 	return 1;
7447f7bcaeSTom Rini }
7547f7bcaeSTom Rini #endif
7647f7bcaeSTom Rini 
7747f7bcaeSTom Rini void spl_parse_image_header(const struct image_header *header)
7847f7bcaeSTom Rini {
7947f7bcaeSTom Rini 	u32 header_size = sizeof(struct image_header);
8047f7bcaeSTom Rini 
8177552b06SStefan Roese 	if (image_get_magic(header) == IH_MAGIC) {
82022b4975SStefan Roese 		if (spl_image.flags & SPL_COPY_PAYLOAD_ONLY) {
83022b4975SStefan Roese 			/*
84022b4975SStefan Roese 			 * On some system (e.g. powerpc), the load-address and
85022b4975SStefan Roese 			 * entry-point is located at address 0. We can't load
86022b4975SStefan Roese 			 * to 0-0x40. So skip header in this case.
87022b4975SStefan Roese 			 */
88022b4975SStefan Roese 			spl_image.load_addr = image_get_load(header);
89022b4975SStefan Roese 			spl_image.entry_point = image_get_ep(header);
90022b4975SStefan Roese 			spl_image.size = image_get_data_size(header);
91022b4975SStefan Roese 		} else {
9277552b06SStefan Roese 			spl_image.entry_point = image_get_load(header);
9347f7bcaeSTom Rini 			/* Load including the header */
94022b4975SStefan Roese 			spl_image.load_addr = spl_image.entry_point -
95022b4975SStefan Roese 				header_size;
96022b4975SStefan Roese 			spl_image.size = image_get_data_size(header) +
97022b4975SStefan Roese 				header_size;
98022b4975SStefan Roese 		}
9977552b06SStefan Roese 		spl_image.os = image_get_os(header);
10077552b06SStefan Roese 		spl_image.name = image_get_name(header);
10147f7bcaeSTom Rini 		debug("spl: payload image: %s load addr: 0x%x size: %d\n",
10247f7bcaeSTom Rini 			spl_image.name, spl_image.load_addr, spl_image.size);
10347f7bcaeSTom Rini 	} else {
10447f7bcaeSTom Rini 		/* Signature not found - assume u-boot.bin */
10547f7bcaeSTom Rini 		debug("mkimage signature not found - ih_magic = %x\n",
10647f7bcaeSTom Rini 			header->ih_magic);
10747f7bcaeSTom Rini 		/* Let's assume U-Boot will not be more than 200 KB */
108ae83d882SStefano Babic 		spl_image.size = CONFIG_SYS_MONITOR_LEN;
1093c6f8a0dSStefan Roese 		spl_image.entry_point = CONFIG_SYS_UBOOT_START;
11047f7bcaeSTom Rini 		spl_image.load_addr = CONFIG_SYS_TEXT_BASE;
11147f7bcaeSTom Rini 		spl_image.os = IH_OS_U_BOOT;
11247f7bcaeSTom Rini 		spl_image.name = "U-Boot";
11347f7bcaeSTom Rini 	}
11447f7bcaeSTom Rini }
11547f7bcaeSTom Rini 
116*a759f1e0SAllen Martin __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
11747f7bcaeSTom Rini {
11847f7bcaeSTom Rini 	typedef void __noreturn (*image_entry_noargs_t)(u32 *);
11947f7bcaeSTom Rini 	image_entry_noargs_t image_entry =
120*a759f1e0SAllen Martin 			(image_entry_noargs_t) spl_image->entry_point;
12147f7bcaeSTom Rini 
122*a759f1e0SAllen Martin 	debug("image entry point: 0x%X\n", spl_image->entry_point);
12347f7bcaeSTom Rini 	/* Pass the saved boot_params from rom code */
12447f7bcaeSTom Rini #if defined(CONFIG_VIRTIO) || defined(CONFIG_ZEBU)
12547f7bcaeSTom Rini 	image_entry = (image_entry_noargs_t)0x80100000;
12647f7bcaeSTom Rini #endif
12747f7bcaeSTom Rini 	u32 boot_params_ptr_addr = (u32)&boot_params_ptr;
12847f7bcaeSTom Rini 	image_entry((u32 *)boot_params_ptr_addr);
12947f7bcaeSTom Rini }
13047f7bcaeSTom Rini 
131c57b953dSPavel Machek #ifdef CONFIG_SPL_RAM_DEVICE
132c57b953dSPavel Machek static void spl_ram_load_image(void)
133c57b953dSPavel Machek {
134c57b953dSPavel Machek 	const struct image_header *header;
135c57b953dSPavel Machek 
136c57b953dSPavel Machek 	/*
137c57b953dSPavel Machek 	 * Get the header.  It will point to an address defined by handoff
138c57b953dSPavel Machek 	 * which will tell where the image located inside the flash. For
139c57b953dSPavel Machek 	 * now, it will temporary fixed to address pointed by U-Boot.
140c57b953dSPavel Machek 	 */
141c57b953dSPavel Machek 	header = (struct image_header *)
142c57b953dSPavel Machek 		(CONFIG_SYS_TEXT_BASE -	sizeof(struct image_header));
143c57b953dSPavel Machek 
144c57b953dSPavel Machek 	spl_parse_image_header(header);
145c57b953dSPavel Machek }
146c57b953dSPavel Machek #endif
147c57b953dSPavel Machek 
1486507f133STom Rini void board_init_r(gd_t *dummy1, ulong dummy2)
14947f7bcaeSTom Rini {
15047f7bcaeSTom Rini 	u32 boot_device;
15147f7bcaeSTom Rini 	debug(">>spl:board_init_r()\n");
15247f7bcaeSTom Rini 
15347f7bcaeSTom Rini #ifdef CONFIG_SYS_SPL_MALLOC_START
15447f7bcaeSTom Rini 	mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
15547f7bcaeSTom Rini 			CONFIG_SYS_SPL_MALLOC_SIZE);
15647f7bcaeSTom Rini #endif
15747f7bcaeSTom Rini 
1584063c77dSIlya Yanok 	timer_init();
1594063c77dSIlya Yanok 
16047f7bcaeSTom Rini #ifdef CONFIG_SPL_BOARD_INIT
16147f7bcaeSTom Rini 	spl_board_init();
16247f7bcaeSTom Rini #endif
16347f7bcaeSTom Rini 
16447f7bcaeSTom Rini 	boot_device = spl_boot_device();
16547f7bcaeSTom Rini 	debug("boot device - %d\n", boot_device);
16647f7bcaeSTom Rini 	switch (boot_device) {
167c57b953dSPavel Machek #ifdef CONFIG_SPL_RAM_DEVICE
168c57b953dSPavel Machek 	case BOOT_DEVICE_RAM:
169c57b953dSPavel Machek 		spl_ram_load_image();
170c57b953dSPavel Machek 		break;
171c57b953dSPavel Machek #endif
17247f7bcaeSTom Rini #ifdef CONFIG_SPL_MMC_SUPPORT
17347f7bcaeSTom Rini 	case BOOT_DEVICE_MMC1:
17447f7bcaeSTom Rini 	case BOOT_DEVICE_MMC2:
17547f7bcaeSTom Rini 	case BOOT_DEVICE_MMC2_2:
17647f7bcaeSTom Rini 		spl_mmc_load_image();
17747f7bcaeSTom Rini 		break;
17847f7bcaeSTom Rini #endif
17947f7bcaeSTom Rini #ifdef CONFIG_SPL_NAND_SUPPORT
18047f7bcaeSTom Rini 	case BOOT_DEVICE_NAND:
18147f7bcaeSTom Rini 		spl_nand_load_image();
18247f7bcaeSTom Rini 		break;
18347f7bcaeSTom Rini #endif
18433d34646SStefan Roese #ifdef CONFIG_SPL_NOR_SUPPORT
18533d34646SStefan Roese 	case BOOT_DEVICE_NOR:
18633d34646SStefan Roese 		spl_nor_load_image();
18733d34646SStefan Roese 		break;
18833d34646SStefan Roese #endif
18947f7bcaeSTom Rini #ifdef CONFIG_SPL_YMODEM_SUPPORT
19047f7bcaeSTom Rini 	case BOOT_DEVICE_UART:
19147f7bcaeSTom Rini 		spl_ymodem_load_image();
19247f7bcaeSTom Rini 		break;
19347f7bcaeSTom Rini #endif
19447f7bcaeSTom Rini #ifdef CONFIG_SPL_SPI_SUPPORT
19547f7bcaeSTom Rini 	case BOOT_DEVICE_SPI:
196a4cc1c48STom Rini 		spl_spi_load_image();
19747f7bcaeSTom Rini 		break;
19847f7bcaeSTom Rini #endif
1997ac2fe2dSIlya Yanok #ifdef CONFIG_SPL_ETH_SUPPORT
2007ac2fe2dSIlya Yanok 	case BOOT_DEVICE_CPGMAC:
2017ac2fe2dSIlya Yanok #ifdef CONFIG_SPL_ETH_DEVICE
2027ac2fe2dSIlya Yanok 		spl_net_load_image(CONFIG_SPL_ETH_DEVICE);
2037ac2fe2dSIlya Yanok #else
2047ac2fe2dSIlya Yanok 		spl_net_load_image(NULL);
2057ac2fe2dSIlya Yanok #endif
2067ac2fe2dSIlya Yanok 		break;
2077ac2fe2dSIlya Yanok #endif
20847f7bcaeSTom Rini 	default:
2091292eaf3STom Rini 		debug("SPL: Un-supported Boot Device\n");
21047f7bcaeSTom Rini 		hang();
21147f7bcaeSTom Rini 	}
21247f7bcaeSTom Rini 
21347f7bcaeSTom Rini 	switch (spl_image.os) {
21447f7bcaeSTom Rini 	case IH_OS_U_BOOT:
21547f7bcaeSTom Rini 		debug("Jumping to U-Boot\n");
21647f7bcaeSTom Rini 		break;
21747f7bcaeSTom Rini #ifdef CONFIG_SPL_OS_BOOT
21847f7bcaeSTom Rini 	case IH_OS_LINUX:
21947f7bcaeSTom Rini 		debug("Jumping to Linux\n");
22047f7bcaeSTom Rini 		spl_board_prepare_for_linux();
22147f7bcaeSTom Rini 		jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
22247f7bcaeSTom Rini #endif
22347f7bcaeSTom Rini 	default:
22442120981STom Rini 		debug("Unsupported OS image.. Jumping nevertheless..\n");
22547f7bcaeSTom Rini 	}
226*a759f1e0SAllen Martin 	jump_to_image_no_args(&spl_image);
22747f7bcaeSTom Rini }
22847f7bcaeSTom Rini 
2296507f133STom Rini /*
2306507f133STom Rini  * This requires UART clocks to be enabled.  In order for this to work the
2316507f133STom Rini  * caller must ensure that the gd pointer is valid.
2326507f133STom Rini  */
23347f7bcaeSTom Rini void preloader_console_init(void)
23447f7bcaeSTom Rini {
23547f7bcaeSTom Rini 	gd->bd = &bdata;
23647f7bcaeSTom Rini 	gd->baudrate = CONFIG_BAUDRATE;
23747f7bcaeSTom Rini 
23847f7bcaeSTom Rini 	serial_init();		/* serial communications setup */
23947f7bcaeSTom Rini 
24047f7bcaeSTom Rini 	gd->have_console = 1;
24147f7bcaeSTom Rini 
24247f7bcaeSTom Rini 	puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
24347f7bcaeSTom Rini 			U_BOOT_TIME ")\n");
24447f7bcaeSTom Rini #ifdef CONFIG_SPL_DISPLAY_PRINT
24547f7bcaeSTom Rini 	spl_display_print();
24647f7bcaeSTom Rini #endif
24747f7bcaeSTom Rini }
248