xref: /rk3399_rockchip-uboot/common/spl/spl.c (revision 3c6f8a0d19c0c8e2cee3dae933dbc5a75e29906d)
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 
38*3c6f8a0dSStefan Roese #ifndef CONFIG_SYS_UBOOT_START
39*3c6f8a0dSStefan Roese #define CONFIG_SYS_UBOOT_START	CONFIG_SYS_TEXT_BASE
40*3c6f8a0dSStefan 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 		puts("mkimage signature not found, assuming u-boot.bin ..\n");
10647f7bcaeSTom Rini 		debug("mkimage signature not found - ih_magic = %x\n",
10747f7bcaeSTom Rini 			header->ih_magic);
10847f7bcaeSTom Rini 		/* Let's assume U-Boot will not be more than 200 KB */
109ae83d882SStefano Babic 		spl_image.size = CONFIG_SYS_MONITOR_LEN;
110*3c6f8a0dSStefan Roese 		spl_image.entry_point = CONFIG_SYS_UBOOT_START;
11147f7bcaeSTom Rini 		spl_image.load_addr = CONFIG_SYS_TEXT_BASE;
11247f7bcaeSTom Rini 		spl_image.os = IH_OS_U_BOOT;
11347f7bcaeSTom Rini 		spl_image.name = "U-Boot";
11447f7bcaeSTom Rini 	}
11547f7bcaeSTom Rini }
11647f7bcaeSTom Rini 
11747f7bcaeSTom Rini static void __noreturn jump_to_image_no_args(void)
11847f7bcaeSTom Rini {
11947f7bcaeSTom Rini 	typedef void __noreturn (*image_entry_noargs_t)(u32 *);
12047f7bcaeSTom Rini 	image_entry_noargs_t image_entry =
12147f7bcaeSTom Rini 			(image_entry_noargs_t) spl_image.entry_point;
12247f7bcaeSTom Rini 
12347f7bcaeSTom Rini 	debug("image entry point: 0x%X\n", spl_image.entry_point);
12447f7bcaeSTom Rini 	/* Pass the saved boot_params from rom code */
12547f7bcaeSTom Rini #if defined(CONFIG_VIRTIO) || defined(CONFIG_ZEBU)
12647f7bcaeSTom Rini 	image_entry = (image_entry_noargs_t)0x80100000;
12747f7bcaeSTom Rini #endif
12847f7bcaeSTom Rini 	u32 boot_params_ptr_addr = (u32)&boot_params_ptr;
12947f7bcaeSTom Rini 	image_entry((u32 *)boot_params_ptr_addr);
13047f7bcaeSTom Rini }
13147f7bcaeSTom Rini 
1326507f133STom Rini void board_init_r(gd_t *dummy1, ulong dummy2)
13347f7bcaeSTom Rini {
13447f7bcaeSTom Rini 	u32 boot_device;
13547f7bcaeSTom Rini 	debug(">>spl:board_init_r()\n");
13647f7bcaeSTom Rini 
13747f7bcaeSTom Rini #ifdef CONFIG_SYS_SPL_MALLOC_START
13847f7bcaeSTom Rini 	mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
13947f7bcaeSTom Rini 			CONFIG_SYS_SPL_MALLOC_SIZE);
14047f7bcaeSTom Rini #endif
14147f7bcaeSTom Rini 
14247f7bcaeSTom Rini #ifdef CONFIG_SPL_BOARD_INIT
14347f7bcaeSTom Rini 	spl_board_init();
14447f7bcaeSTom Rini #endif
14547f7bcaeSTom Rini 
14647f7bcaeSTom Rini 	boot_device = spl_boot_device();
14747f7bcaeSTom Rini 	debug("boot device - %d\n", boot_device);
14847f7bcaeSTom Rini 	switch (boot_device) {
14947f7bcaeSTom Rini #ifdef CONFIG_SPL_MMC_SUPPORT
15047f7bcaeSTom Rini 	case BOOT_DEVICE_MMC1:
15147f7bcaeSTom Rini 	case BOOT_DEVICE_MMC2:
15247f7bcaeSTom Rini 	case BOOT_DEVICE_MMC2_2:
15347f7bcaeSTom Rini 		spl_mmc_load_image();
15447f7bcaeSTom Rini 		break;
15547f7bcaeSTom Rini #endif
15647f7bcaeSTom Rini #ifdef CONFIG_SPL_NAND_SUPPORT
15747f7bcaeSTom Rini 	case BOOT_DEVICE_NAND:
15847f7bcaeSTom Rini 		spl_nand_load_image();
15947f7bcaeSTom Rini 		break;
16047f7bcaeSTom Rini #endif
16133d34646SStefan Roese #ifdef CONFIG_SPL_NOR_SUPPORT
16233d34646SStefan Roese 	case BOOT_DEVICE_NOR:
16333d34646SStefan Roese 		spl_nor_load_image();
16433d34646SStefan Roese 		break;
16533d34646SStefan Roese #endif
16647f7bcaeSTom Rini #ifdef CONFIG_SPL_YMODEM_SUPPORT
16747f7bcaeSTom Rini 	case BOOT_DEVICE_UART:
16847f7bcaeSTom Rini 		spl_ymodem_load_image();
16947f7bcaeSTom Rini 		break;
17047f7bcaeSTom Rini #endif
17147f7bcaeSTom Rini #ifdef CONFIG_SPL_SPI_SUPPORT
17247f7bcaeSTom Rini 	case BOOT_DEVICE_SPI:
173a4cc1c48STom Rini 		spl_spi_load_image();
17447f7bcaeSTom Rini 		break;
17547f7bcaeSTom Rini #endif
17647f7bcaeSTom Rini 	default:
17747f7bcaeSTom Rini 		puts("SPL: Un-supported Boot Device\n");
17847f7bcaeSTom Rini 		debug("Found: %d\n", boot_device);
17947f7bcaeSTom Rini 		hang();
18047f7bcaeSTom Rini 		break;
18147f7bcaeSTom Rini 	}
18247f7bcaeSTom Rini 
18347f7bcaeSTom Rini 	switch (spl_image.os) {
18447f7bcaeSTom Rini 	case IH_OS_U_BOOT:
18547f7bcaeSTom Rini 		debug("Jumping to U-Boot\n");
18647f7bcaeSTom Rini 		jump_to_image_no_args();
18747f7bcaeSTom Rini 		break;
18847f7bcaeSTom Rini #ifdef CONFIG_SPL_OS_BOOT
18947f7bcaeSTom Rini 	case IH_OS_LINUX:
19047f7bcaeSTom Rini 		debug("Jumping to Linux\n");
19147f7bcaeSTom Rini 		spl_board_prepare_for_linux();
19247f7bcaeSTom Rini 		jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
19347f7bcaeSTom Rini 		break;
19447f7bcaeSTom Rini #endif
19547f7bcaeSTom Rini 	default:
19647f7bcaeSTom Rini 		puts("Unsupported OS image.. Jumping nevertheless..\n");
19747f7bcaeSTom Rini 		jump_to_image_no_args();
19847f7bcaeSTom Rini 	}
19947f7bcaeSTom Rini }
20047f7bcaeSTom Rini 
2016507f133STom Rini /*
2026507f133STom Rini  * This requires UART clocks to be enabled.  In order for this to work the
2036507f133STom Rini  * caller must ensure that the gd pointer is valid.
2046507f133STom Rini  */
20547f7bcaeSTom Rini void preloader_console_init(void)
20647f7bcaeSTom Rini {
20747f7bcaeSTom Rini 	gd->bd = &bdata;
20847f7bcaeSTom Rini 	gd->flags |= GD_FLG_RELOC;
20947f7bcaeSTom Rini 	gd->baudrate = CONFIG_BAUDRATE;
21047f7bcaeSTom Rini 
21147f7bcaeSTom Rini 	serial_init();		/* serial communications setup */
21247f7bcaeSTom Rini 
21347f7bcaeSTom Rini 	gd->have_console = 1;
21447f7bcaeSTom Rini 
21547f7bcaeSTom Rini 	puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
21647f7bcaeSTom Rini 			U_BOOT_TIME ")\n");
21747f7bcaeSTom Rini #ifdef CONFIG_SPL_DISPLAY_PRINT
21847f7bcaeSTom Rini 	spl_display_print();
21947f7bcaeSTom Rini #endif
22047f7bcaeSTom Rini }
221