xref: /rk3399_rockchip-uboot/common/spl/spl.c (revision ae83d882f5fdf7aa7c5aec09cfafb593153c25d6)
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*ae83d882SStefano Babic #ifndef CONFIG_SYS_MONITOR_LEN
39*ae83d882SStefano Babic #define CONFIG_SYS_MONITOR_LEN	(200 * 1024)
40*ae83d882SStefano Babic #endif
41*ae83d882SStefano Babic 
4247f7bcaeSTom Rini u32 *boot_params_ptr = NULL;
4347f7bcaeSTom Rini struct spl_image_info spl_image;
4447f7bcaeSTom Rini 
456507f133STom Rini /* Define board data structure */
4647f7bcaeSTom Rini static bd_t bdata __attribute__ ((section(".data")));
4747f7bcaeSTom Rini 
4847f7bcaeSTom Rini inline void hang(void)
4947f7bcaeSTom Rini {
5047f7bcaeSTom Rini 	puts("### ERROR ### Please RESET the board ###\n");
5147f7bcaeSTom Rini 	for (;;)
5247f7bcaeSTom Rini 		;
5347f7bcaeSTom Rini }
5447f7bcaeSTom Rini 
5547f7bcaeSTom Rini /*
5647f7bcaeSTom Rini  * Default function to determine if u-boot or the OS should
5747f7bcaeSTom Rini  * be started. This implementation always returns 1.
5847f7bcaeSTom Rini  *
5947f7bcaeSTom Rini  * Please implement your own board specific funcion to do this.
6047f7bcaeSTom Rini  *
6147f7bcaeSTom Rini  * RETURN
6247f7bcaeSTom Rini  * 0 to not start u-boot
6347f7bcaeSTom Rini  * positive if u-boot should start
6447f7bcaeSTom Rini  */
6547f7bcaeSTom Rini #ifdef CONFIG_SPL_OS_BOOT
6647f7bcaeSTom Rini __weak int spl_start_uboot(void)
6747f7bcaeSTom Rini {
6847f7bcaeSTom Rini 	puts("SPL: Please implement spl_start_uboot() for your board\n");
6947f7bcaeSTom Rini 	puts("SPL: Direct Linux boot not active!\n");
7047f7bcaeSTom Rini 	return 1;
7147f7bcaeSTom Rini }
7247f7bcaeSTom Rini #endif
7347f7bcaeSTom Rini 
7447f7bcaeSTom Rini void spl_parse_image_header(const struct image_header *header)
7547f7bcaeSTom Rini {
7647f7bcaeSTom Rini 	u32 header_size = sizeof(struct image_header);
7747f7bcaeSTom Rini 
7847f7bcaeSTom Rini 	if (__be32_to_cpu(header->ih_magic) == IH_MAGIC) {
7947f7bcaeSTom Rini 		spl_image.size = __be32_to_cpu(header->ih_size) + header_size;
8047f7bcaeSTom Rini 		spl_image.entry_point = __be32_to_cpu(header->ih_load);
8147f7bcaeSTom Rini 		/* Load including the header */
8247f7bcaeSTom Rini 		spl_image.load_addr = spl_image.entry_point - header_size;
8347f7bcaeSTom Rini 		spl_image.os = header->ih_os;
8447f7bcaeSTom Rini 		spl_image.name = (const char *)&header->ih_name;
8547f7bcaeSTom Rini 		debug("spl: payload image: %s load addr: 0x%x size: %d\n",
8647f7bcaeSTom Rini 			spl_image.name, spl_image.load_addr, spl_image.size);
8747f7bcaeSTom Rini 	} else {
8847f7bcaeSTom Rini 		/* Signature not found - assume u-boot.bin */
8947f7bcaeSTom Rini 		puts("mkimage signature not found, assuming u-boot.bin ..\n");
9047f7bcaeSTom Rini 		debug("mkimage signature not found - ih_magic = %x\n",
9147f7bcaeSTom Rini 			header->ih_magic);
9247f7bcaeSTom Rini 		/* Let's assume U-Boot will not be more than 200 KB */
93*ae83d882SStefano Babic 		spl_image.size = CONFIG_SYS_MONITOR_LEN;
9447f7bcaeSTom Rini 		spl_image.entry_point = CONFIG_SYS_TEXT_BASE;
9547f7bcaeSTom Rini 		spl_image.load_addr = CONFIG_SYS_TEXT_BASE;
9647f7bcaeSTom Rini 		spl_image.os = IH_OS_U_BOOT;
9747f7bcaeSTom Rini 		spl_image.name = "U-Boot";
9847f7bcaeSTom Rini 	}
9947f7bcaeSTom Rini }
10047f7bcaeSTom Rini 
10147f7bcaeSTom Rini static void __noreturn jump_to_image_no_args(void)
10247f7bcaeSTom Rini {
10347f7bcaeSTom Rini 	typedef void __noreturn (*image_entry_noargs_t)(u32 *);
10447f7bcaeSTom Rini 	image_entry_noargs_t image_entry =
10547f7bcaeSTom Rini 			(image_entry_noargs_t) spl_image.entry_point;
10647f7bcaeSTom Rini 
10747f7bcaeSTom Rini 	debug("image entry point: 0x%X\n", spl_image.entry_point);
10847f7bcaeSTom Rini 	/* Pass the saved boot_params from rom code */
10947f7bcaeSTom Rini #if defined(CONFIG_VIRTIO) || defined(CONFIG_ZEBU)
11047f7bcaeSTom Rini 	image_entry = (image_entry_noargs_t)0x80100000;
11147f7bcaeSTom Rini #endif
11247f7bcaeSTom Rini 	u32 boot_params_ptr_addr = (u32)&boot_params_ptr;
11347f7bcaeSTom Rini 	image_entry((u32 *)boot_params_ptr_addr);
11447f7bcaeSTom Rini }
11547f7bcaeSTom Rini 
1166507f133STom Rini void board_init_r(gd_t *dummy1, ulong dummy2)
11747f7bcaeSTom Rini {
11847f7bcaeSTom Rini 	u32 boot_device;
11947f7bcaeSTom Rini 	debug(">>spl:board_init_r()\n");
12047f7bcaeSTom Rini 
12147f7bcaeSTom Rini #ifdef CONFIG_SYS_SPL_MALLOC_START
12247f7bcaeSTom Rini 	mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
12347f7bcaeSTom Rini 			CONFIG_SYS_SPL_MALLOC_SIZE);
12447f7bcaeSTom Rini #endif
12547f7bcaeSTom Rini 
12647f7bcaeSTom Rini #ifdef CONFIG_SPL_BOARD_INIT
12747f7bcaeSTom Rini 	spl_board_init();
12847f7bcaeSTom Rini #endif
12947f7bcaeSTom Rini 
13047f7bcaeSTom Rini 	boot_device = spl_boot_device();
13147f7bcaeSTom Rini 	debug("boot device - %d\n", boot_device);
13247f7bcaeSTom Rini 	switch (boot_device) {
13347f7bcaeSTom Rini #ifdef CONFIG_SPL_MMC_SUPPORT
13447f7bcaeSTom Rini 	case BOOT_DEVICE_MMC1:
13547f7bcaeSTom Rini 	case BOOT_DEVICE_MMC2:
13647f7bcaeSTom Rini 	case BOOT_DEVICE_MMC2_2:
13747f7bcaeSTom Rini 		spl_mmc_load_image();
13847f7bcaeSTom Rini 		break;
13947f7bcaeSTom Rini #endif
14047f7bcaeSTom Rini #ifdef CONFIG_SPL_NAND_SUPPORT
14147f7bcaeSTom Rini 	case BOOT_DEVICE_NAND:
14247f7bcaeSTom Rini 		spl_nand_load_image();
14347f7bcaeSTom Rini 		break;
14447f7bcaeSTom Rini #endif
14547f7bcaeSTom Rini #ifdef CONFIG_SPL_YMODEM_SUPPORT
14647f7bcaeSTom Rini 	case BOOT_DEVICE_UART:
14747f7bcaeSTom Rini 		spl_ymodem_load_image();
14847f7bcaeSTom Rini 		break;
14947f7bcaeSTom Rini #endif
15047f7bcaeSTom Rini #ifdef CONFIG_SPL_SPI_SUPPORT
15147f7bcaeSTom Rini 	case BOOT_DEVICE_SPI:
15247f7bcaeSTom Rini 		spi_boot();
15347f7bcaeSTom Rini 	break;
15447f7bcaeSTom Rini #endif
15547f7bcaeSTom Rini 	default:
15647f7bcaeSTom Rini 		puts("SPL: Un-supported Boot Device\n");
15747f7bcaeSTom Rini 		debug("Found: %d\n", boot_device);
15847f7bcaeSTom Rini 		hang();
15947f7bcaeSTom Rini 		break;
16047f7bcaeSTom Rini 	}
16147f7bcaeSTom Rini 
16247f7bcaeSTom Rini 	switch (spl_image.os) {
16347f7bcaeSTom Rini 	case IH_OS_U_BOOT:
16447f7bcaeSTom Rini 		debug("Jumping to U-Boot\n");
16547f7bcaeSTom Rini 		jump_to_image_no_args();
16647f7bcaeSTom Rini 		break;
16747f7bcaeSTom Rini #ifdef CONFIG_SPL_OS_BOOT
16847f7bcaeSTom Rini 	case IH_OS_LINUX:
16947f7bcaeSTom Rini 		debug("Jumping to Linux\n");
17047f7bcaeSTom Rini 		spl_board_prepare_for_linux();
17147f7bcaeSTom Rini 		jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
17247f7bcaeSTom Rini 		break;
17347f7bcaeSTom Rini #endif
17447f7bcaeSTom Rini 	default:
17547f7bcaeSTom Rini 		puts("Unsupported OS image.. Jumping nevertheless..\n");
17647f7bcaeSTom Rini 		jump_to_image_no_args();
17747f7bcaeSTom Rini 	}
17847f7bcaeSTom Rini }
17947f7bcaeSTom Rini 
1806507f133STom Rini /*
1816507f133STom Rini  * This requires UART clocks to be enabled.  In order for this to work the
1826507f133STom Rini  * caller must ensure that the gd pointer is valid.
1836507f133STom Rini  */
18447f7bcaeSTom Rini void preloader_console_init(void)
18547f7bcaeSTom Rini {
18647f7bcaeSTom Rini 	gd->bd = &bdata;
18747f7bcaeSTom Rini 	gd->flags |= GD_FLG_RELOC;
18847f7bcaeSTom Rini 	gd->baudrate = CONFIG_BAUDRATE;
18947f7bcaeSTom Rini 
19047f7bcaeSTom Rini 	serial_init();		/* serial communications setup */
19147f7bcaeSTom Rini 
19247f7bcaeSTom Rini 	gd->have_console = 1;
19347f7bcaeSTom Rini 
19447f7bcaeSTom Rini 	puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
19547f7bcaeSTom Rini 			U_BOOT_TIME ")\n");
19647f7bcaeSTom Rini #ifdef CONFIG_SPL_DISPLAY_PRINT
19747f7bcaeSTom Rini 	spl_display_print();
19847f7bcaeSTom Rini #endif
19947f7bcaeSTom Rini }
200