xref: /rk3399_rockchip-uboot/common/spl/spl.c (revision ea8256f072ccc04c83fa10030673cdd4cd01cbd9)
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 
77*ea8256f0SStefan Roese /*
78*ea8256f0SStefan Roese  * Weak default function for board specific cleanup/preparation before
79*ea8256f0SStefan Roese  * Linux boot. Some boards/platforms might not need it, so just provide
80*ea8256f0SStefan Roese  * an empty stub here.
81*ea8256f0SStefan Roese  */
82*ea8256f0SStefan Roese __weak void spl_board_prepare_for_linux(void)
83*ea8256f0SStefan Roese {
84*ea8256f0SStefan Roese 	/* Nothing to do! */
85*ea8256f0SStefan Roese }
86*ea8256f0SStefan Roese 
8747f7bcaeSTom Rini void spl_parse_image_header(const struct image_header *header)
8847f7bcaeSTom Rini {
8947f7bcaeSTom Rini 	u32 header_size = sizeof(struct image_header);
9047f7bcaeSTom Rini 
9177552b06SStefan Roese 	if (image_get_magic(header) == IH_MAGIC) {
92022b4975SStefan Roese 		if (spl_image.flags & SPL_COPY_PAYLOAD_ONLY) {
93022b4975SStefan Roese 			/*
94022b4975SStefan Roese 			 * On some system (e.g. powerpc), the load-address and
95022b4975SStefan Roese 			 * entry-point is located at address 0. We can't load
96022b4975SStefan Roese 			 * to 0-0x40. So skip header in this case.
97022b4975SStefan Roese 			 */
98022b4975SStefan Roese 			spl_image.load_addr = image_get_load(header);
99022b4975SStefan Roese 			spl_image.entry_point = image_get_ep(header);
100022b4975SStefan Roese 			spl_image.size = image_get_data_size(header);
101022b4975SStefan Roese 		} else {
10277552b06SStefan Roese 			spl_image.entry_point = image_get_load(header);
10347f7bcaeSTom Rini 			/* Load including the header */
104022b4975SStefan Roese 			spl_image.load_addr = spl_image.entry_point -
105022b4975SStefan Roese 				header_size;
106022b4975SStefan Roese 			spl_image.size = image_get_data_size(header) +
107022b4975SStefan Roese 				header_size;
108022b4975SStefan Roese 		}
10977552b06SStefan Roese 		spl_image.os = image_get_os(header);
11077552b06SStefan Roese 		spl_image.name = image_get_name(header);
11147f7bcaeSTom Rini 		debug("spl: payload image: %s load addr: 0x%x size: %d\n",
11247f7bcaeSTom Rini 			spl_image.name, spl_image.load_addr, spl_image.size);
11347f7bcaeSTom Rini 	} else {
11447f7bcaeSTom Rini 		/* Signature not found - assume u-boot.bin */
11547f7bcaeSTom Rini 		debug("mkimage signature not found - ih_magic = %x\n",
11647f7bcaeSTom Rini 			header->ih_magic);
11747f7bcaeSTom Rini 		/* Let's assume U-Boot will not be more than 200 KB */
118ae83d882SStefano Babic 		spl_image.size = CONFIG_SYS_MONITOR_LEN;
1193c6f8a0dSStefan Roese 		spl_image.entry_point = CONFIG_SYS_UBOOT_START;
12047f7bcaeSTom Rini 		spl_image.load_addr = CONFIG_SYS_TEXT_BASE;
12147f7bcaeSTom Rini 		spl_image.os = IH_OS_U_BOOT;
12247f7bcaeSTom Rini 		spl_image.name = "U-Boot";
12347f7bcaeSTom Rini 	}
12447f7bcaeSTom Rini }
12547f7bcaeSTom Rini 
126a759f1e0SAllen Martin __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
12747f7bcaeSTom Rini {
12847f7bcaeSTom Rini 	typedef void __noreturn (*image_entry_noargs_t)(u32 *);
12947f7bcaeSTom Rini 	image_entry_noargs_t image_entry =
130a759f1e0SAllen Martin 			(image_entry_noargs_t) spl_image->entry_point;
13147f7bcaeSTom Rini 
132a759f1e0SAllen Martin 	debug("image entry point: 0x%X\n", spl_image->entry_point);
13347f7bcaeSTom Rini 	/* Pass the saved boot_params from rom code */
13447f7bcaeSTom Rini #if defined(CONFIG_VIRTIO) || defined(CONFIG_ZEBU)
13547f7bcaeSTom Rini 	image_entry = (image_entry_noargs_t)0x80100000;
13647f7bcaeSTom Rini #endif
13747f7bcaeSTom Rini 	u32 boot_params_ptr_addr = (u32)&boot_params_ptr;
13847f7bcaeSTom Rini 	image_entry((u32 *)boot_params_ptr_addr);
13947f7bcaeSTom Rini }
14047f7bcaeSTom Rini 
141c57b953dSPavel Machek #ifdef CONFIG_SPL_RAM_DEVICE
142c57b953dSPavel Machek static void spl_ram_load_image(void)
143c57b953dSPavel Machek {
144c57b953dSPavel Machek 	const struct image_header *header;
145c57b953dSPavel Machek 
146c57b953dSPavel Machek 	/*
147c57b953dSPavel Machek 	 * Get the header.  It will point to an address defined by handoff
148c57b953dSPavel Machek 	 * which will tell where the image located inside the flash. For
149c57b953dSPavel Machek 	 * now, it will temporary fixed to address pointed by U-Boot.
150c57b953dSPavel Machek 	 */
151c57b953dSPavel Machek 	header = (struct image_header *)
152c57b953dSPavel Machek 		(CONFIG_SYS_TEXT_BASE -	sizeof(struct image_header));
153c57b953dSPavel Machek 
154c57b953dSPavel Machek 	spl_parse_image_header(header);
155c57b953dSPavel Machek }
156c57b953dSPavel Machek #endif
157c57b953dSPavel Machek 
1586507f133STom Rini void board_init_r(gd_t *dummy1, ulong dummy2)
15947f7bcaeSTom Rini {
16047f7bcaeSTom Rini 	u32 boot_device;
16147f7bcaeSTom Rini 	debug(">>spl:board_init_r()\n");
16247f7bcaeSTom Rini 
16347f7bcaeSTom Rini #ifdef CONFIG_SYS_SPL_MALLOC_START
16447f7bcaeSTom Rini 	mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
16547f7bcaeSTom Rini 			CONFIG_SYS_SPL_MALLOC_SIZE);
16647f7bcaeSTom Rini #endif
16747f7bcaeSTom Rini 
168*ea8256f0SStefan Roese #ifndef CONFIG_PPC
169*ea8256f0SStefan Roese 	/*
170*ea8256f0SStefan Roese 	 * timer_init() does not exist on PPC systems. The timer is initialized
171*ea8256f0SStefan Roese 	 * and enabled (decrementer) in interrupt_init() here.
172*ea8256f0SStefan Roese 	 */
1734063c77dSIlya Yanok 	timer_init();
174*ea8256f0SStefan Roese #endif
1754063c77dSIlya Yanok 
17647f7bcaeSTom Rini #ifdef CONFIG_SPL_BOARD_INIT
17747f7bcaeSTom Rini 	spl_board_init();
17847f7bcaeSTom Rini #endif
17947f7bcaeSTom Rini 
18047f7bcaeSTom Rini 	boot_device = spl_boot_device();
18147f7bcaeSTom Rini 	debug("boot device - %d\n", boot_device);
18247f7bcaeSTom Rini 	switch (boot_device) {
183c57b953dSPavel Machek #ifdef CONFIG_SPL_RAM_DEVICE
184c57b953dSPavel Machek 	case BOOT_DEVICE_RAM:
185c57b953dSPavel Machek 		spl_ram_load_image();
186c57b953dSPavel Machek 		break;
187c57b953dSPavel Machek #endif
18847f7bcaeSTom Rini #ifdef CONFIG_SPL_MMC_SUPPORT
18947f7bcaeSTom Rini 	case BOOT_DEVICE_MMC1:
19047f7bcaeSTom Rini 	case BOOT_DEVICE_MMC2:
19147f7bcaeSTom Rini 	case BOOT_DEVICE_MMC2_2:
19247f7bcaeSTom Rini 		spl_mmc_load_image();
19347f7bcaeSTom Rini 		break;
19447f7bcaeSTom Rini #endif
19547f7bcaeSTom Rini #ifdef CONFIG_SPL_NAND_SUPPORT
19647f7bcaeSTom Rini 	case BOOT_DEVICE_NAND:
19747f7bcaeSTom Rini 		spl_nand_load_image();
19847f7bcaeSTom Rini 		break;
19947f7bcaeSTom Rini #endif
20033d34646SStefan Roese #ifdef CONFIG_SPL_NOR_SUPPORT
20133d34646SStefan Roese 	case BOOT_DEVICE_NOR:
20233d34646SStefan Roese 		spl_nor_load_image();
20333d34646SStefan Roese 		break;
20433d34646SStefan Roese #endif
20547f7bcaeSTom Rini #ifdef CONFIG_SPL_YMODEM_SUPPORT
20647f7bcaeSTom Rini 	case BOOT_DEVICE_UART:
20747f7bcaeSTom Rini 		spl_ymodem_load_image();
20847f7bcaeSTom Rini 		break;
20947f7bcaeSTom Rini #endif
21047f7bcaeSTom Rini #ifdef CONFIG_SPL_SPI_SUPPORT
21147f7bcaeSTom Rini 	case BOOT_DEVICE_SPI:
212a4cc1c48STom Rini 		spl_spi_load_image();
21347f7bcaeSTom Rini 		break;
21447f7bcaeSTom Rini #endif
2157ac2fe2dSIlya Yanok #ifdef CONFIG_SPL_ETH_SUPPORT
2167ac2fe2dSIlya Yanok 	case BOOT_DEVICE_CPGMAC:
2177ac2fe2dSIlya Yanok #ifdef CONFIG_SPL_ETH_DEVICE
2187ac2fe2dSIlya Yanok 		spl_net_load_image(CONFIG_SPL_ETH_DEVICE);
2197ac2fe2dSIlya Yanok #else
2207ac2fe2dSIlya Yanok 		spl_net_load_image(NULL);
2217ac2fe2dSIlya Yanok #endif
2227ac2fe2dSIlya Yanok 		break;
2237ac2fe2dSIlya Yanok #endif
22447f7bcaeSTom Rini 	default:
2251292eaf3STom Rini 		debug("SPL: Un-supported Boot Device\n");
22647f7bcaeSTom Rini 		hang();
22747f7bcaeSTom Rini 	}
22847f7bcaeSTom Rini 
22947f7bcaeSTom Rini 	switch (spl_image.os) {
23047f7bcaeSTom Rini 	case IH_OS_U_BOOT:
23147f7bcaeSTom Rini 		debug("Jumping to U-Boot\n");
23247f7bcaeSTom Rini 		break;
23347f7bcaeSTom Rini #ifdef CONFIG_SPL_OS_BOOT
23447f7bcaeSTom Rini 	case IH_OS_LINUX:
23547f7bcaeSTom Rini 		debug("Jumping to Linux\n");
23647f7bcaeSTom Rini 		spl_board_prepare_for_linux();
23747f7bcaeSTom Rini 		jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
23847f7bcaeSTom Rini #endif
23947f7bcaeSTom Rini 	default:
24042120981STom Rini 		debug("Unsupported OS image.. Jumping nevertheless..\n");
24147f7bcaeSTom Rini 	}
242a759f1e0SAllen Martin 	jump_to_image_no_args(&spl_image);
24347f7bcaeSTom Rini }
24447f7bcaeSTom Rini 
2456507f133STom Rini /*
2466507f133STom Rini  * This requires UART clocks to be enabled.  In order for this to work the
2476507f133STom Rini  * caller must ensure that the gd pointer is valid.
2486507f133STom Rini  */
24947f7bcaeSTom Rini void preloader_console_init(void)
25047f7bcaeSTom Rini {
25147f7bcaeSTom Rini 	gd->bd = &bdata;
25247f7bcaeSTom Rini 	gd->baudrate = CONFIG_BAUDRATE;
25347f7bcaeSTom Rini 
25447f7bcaeSTom Rini 	serial_init();		/* serial communications setup */
25547f7bcaeSTom Rini 
25647f7bcaeSTom Rini 	gd->have_console = 1;
25747f7bcaeSTom Rini 
25847f7bcaeSTom Rini 	puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
25947f7bcaeSTom Rini 			U_BOOT_TIME ")\n");
26047f7bcaeSTom Rini #ifdef CONFIG_SPL_DISPLAY_PRINT
26147f7bcaeSTom Rini 	spl_display_print();
26247f7bcaeSTom Rini #endif
26347f7bcaeSTom Rini }
264