xref: /rk3399_rockchip-uboot/common/spl/spl.c (revision e76caa627b9f4ebe061208c48f3c5ca71913e23c)
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  *
71a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
847f7bcaeSTom Rini  */
947f7bcaeSTom Rini #include <common.h>
1047f7bcaeSTom Rini #include <spl.h>
1147f7bcaeSTom Rini #include <asm/u-boot.h>
1247f7bcaeSTom Rini #include <nand.h>
1347f7bcaeSTom Rini #include <fat.h>
1447f7bcaeSTom Rini #include <version.h>
1547f7bcaeSTom Rini #include <i2c.h>
1647f7bcaeSTom Rini #include <image.h>
1747f7bcaeSTom Rini #include <malloc.h>
1847f7bcaeSTom Rini #include <linux/compiler.h>
1947f7bcaeSTom Rini 
2047f7bcaeSTom Rini DECLARE_GLOBAL_DATA_PTR;
2147f7bcaeSTom Rini 
223c6f8a0dSStefan Roese #ifndef CONFIG_SYS_UBOOT_START
233c6f8a0dSStefan Roese #define CONFIG_SYS_UBOOT_START	CONFIG_SYS_TEXT_BASE
243c6f8a0dSStefan Roese #endif
25ae83d882SStefano Babic #ifndef CONFIG_SYS_MONITOR_LEN
26*e76caa62SAndreas Bießmann /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
27ae83d882SStefano Babic #define CONFIG_SYS_MONITOR_LEN	(200 * 1024)
28ae83d882SStefano Babic #endif
29ae83d882SStefano Babic 
3047f7bcaeSTom Rini u32 *boot_params_ptr = NULL;
3147f7bcaeSTom Rini struct spl_image_info spl_image;
3247f7bcaeSTom Rini 
336507f133STom Rini /* Define board data structure */
3447f7bcaeSTom Rini static bd_t bdata __attribute__ ((section(".data")));
3547f7bcaeSTom Rini 
3647f7bcaeSTom Rini /*
3747f7bcaeSTom Rini  * Default function to determine if u-boot or the OS should
3847f7bcaeSTom Rini  * be started. This implementation always returns 1.
3947f7bcaeSTom Rini  *
4047f7bcaeSTom Rini  * Please implement your own board specific funcion to do this.
4147f7bcaeSTom Rini  *
4247f7bcaeSTom Rini  * RETURN
4347f7bcaeSTom Rini  * 0 to not start u-boot
4447f7bcaeSTom Rini  * positive if u-boot should start
4547f7bcaeSTom Rini  */
4647f7bcaeSTom Rini #ifdef CONFIG_SPL_OS_BOOT
4747f7bcaeSTom Rini __weak int spl_start_uboot(void)
4847f7bcaeSTom Rini {
4947f7bcaeSTom Rini 	puts("SPL: Please implement spl_start_uboot() for your board\n");
5047f7bcaeSTom Rini 	puts("SPL: Direct Linux boot not active!\n");
5147f7bcaeSTom Rini 	return 1;
5247f7bcaeSTom Rini }
5347f7bcaeSTom Rini #endif
5447f7bcaeSTom Rini 
55ea8256f0SStefan Roese /*
56ea8256f0SStefan Roese  * Weak default function for board specific cleanup/preparation before
57ea8256f0SStefan Roese  * Linux boot. Some boards/platforms might not need it, so just provide
58ea8256f0SStefan Roese  * an empty stub here.
59ea8256f0SStefan Roese  */
60ea8256f0SStefan Roese __weak void spl_board_prepare_for_linux(void)
61ea8256f0SStefan Roese {
62ea8256f0SStefan Roese 	/* Nothing to do! */
63ea8256f0SStefan Roese }
64ea8256f0SStefan Roese 
6547f7bcaeSTom Rini void spl_parse_image_header(const struct image_header *header)
6647f7bcaeSTom Rini {
6747f7bcaeSTom Rini 	u32 header_size = sizeof(struct image_header);
6847f7bcaeSTom Rini 
6977552b06SStefan Roese 	if (image_get_magic(header) == IH_MAGIC) {
70022b4975SStefan Roese 		if (spl_image.flags & SPL_COPY_PAYLOAD_ONLY) {
71022b4975SStefan Roese 			/*
72022b4975SStefan Roese 			 * On some system (e.g. powerpc), the load-address and
73022b4975SStefan Roese 			 * entry-point is located at address 0. We can't load
74022b4975SStefan Roese 			 * to 0-0x40. So skip header in this case.
75022b4975SStefan Roese 			 */
76022b4975SStefan Roese 			spl_image.load_addr = image_get_load(header);
77022b4975SStefan Roese 			spl_image.entry_point = image_get_ep(header);
78022b4975SStefan Roese 			spl_image.size = image_get_data_size(header);
79022b4975SStefan Roese 		} else {
8077552b06SStefan Roese 			spl_image.entry_point = image_get_load(header);
8147f7bcaeSTom Rini 			/* Load including the header */
82022b4975SStefan Roese 			spl_image.load_addr = spl_image.entry_point -
83022b4975SStefan Roese 				header_size;
84022b4975SStefan Roese 			spl_image.size = image_get_data_size(header) +
85022b4975SStefan Roese 				header_size;
86022b4975SStefan Roese 		}
8777552b06SStefan Roese 		spl_image.os = image_get_os(header);
8877552b06SStefan Roese 		spl_image.name = image_get_name(header);
8962cf11c0STaras Kondratiuk 		debug("spl: payload image: %.*s load addr: 0x%x size: %d\n",
905d69a5d1SVasili Galka 			(int)sizeof(spl_image.name), spl_image.name,
9162cf11c0STaras Kondratiuk 			spl_image.load_addr, spl_image.size);
9247f7bcaeSTom Rini 	} else {
9347f7bcaeSTom Rini 		/* Signature not found - assume u-boot.bin */
9447f7bcaeSTom Rini 		debug("mkimage signature not found - ih_magic = %x\n",
9547f7bcaeSTom Rini 			header->ih_magic);
96ae83d882SStefano Babic 		spl_image.size = CONFIG_SYS_MONITOR_LEN;
973c6f8a0dSStefan Roese 		spl_image.entry_point = CONFIG_SYS_UBOOT_START;
9847f7bcaeSTom Rini 		spl_image.load_addr = CONFIG_SYS_TEXT_BASE;
9947f7bcaeSTom Rini 		spl_image.os = IH_OS_U_BOOT;
10047f7bcaeSTom Rini 		spl_image.name = "U-Boot";
10147f7bcaeSTom Rini 	}
10247f7bcaeSTom Rini }
10347f7bcaeSTom Rini 
104a759f1e0SAllen Martin __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
10547f7bcaeSTom Rini {
1064a0eb757SSRICHARAN R 	typedef void __noreturn (*image_entry_noargs_t)(void);
1074a0eb757SSRICHARAN R 
10847f7bcaeSTom Rini 	image_entry_noargs_t image_entry =
109a759f1e0SAllen Martin 			(image_entry_noargs_t) spl_image->entry_point;
11047f7bcaeSTom Rini 
111a759f1e0SAllen Martin 	debug("image entry point: 0x%X\n", spl_image->entry_point);
1124a0eb757SSRICHARAN R 	image_entry();
11347f7bcaeSTom Rini }
11447f7bcaeSTom Rini 
115c57b953dSPavel Machek #ifdef CONFIG_SPL_RAM_DEVICE
116c57b953dSPavel Machek static void spl_ram_load_image(void)
117c57b953dSPavel Machek {
118c57b953dSPavel Machek 	const struct image_header *header;
119c57b953dSPavel Machek 
120c57b953dSPavel Machek 	/*
121c57b953dSPavel Machek 	 * Get the header.  It will point to an address defined by handoff
122c57b953dSPavel Machek 	 * which will tell where the image located inside the flash. For
123c57b953dSPavel Machek 	 * now, it will temporary fixed to address pointed by U-Boot.
124c57b953dSPavel Machek 	 */
125c57b953dSPavel Machek 	header = (struct image_header *)
126c57b953dSPavel Machek 		(CONFIG_SYS_TEXT_BASE -	sizeof(struct image_header));
127c57b953dSPavel Machek 
128c57b953dSPavel Machek 	spl_parse_image_header(header);
129c57b953dSPavel Machek }
130c57b953dSPavel Machek #endif
131c57b953dSPavel Machek 
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 
142ea8256f0SStefan Roese #ifndef CONFIG_PPC
143ea8256f0SStefan Roese 	/*
144ea8256f0SStefan Roese 	 * timer_init() does not exist on PPC systems. The timer is initialized
145ea8256f0SStefan Roese 	 * and enabled (decrementer) in interrupt_init() here.
146ea8256f0SStefan Roese 	 */
1474063c77dSIlya Yanok 	timer_init();
148ea8256f0SStefan Roese #endif
1494063c77dSIlya Yanok 
15047f7bcaeSTom Rini #ifdef CONFIG_SPL_BOARD_INIT
15147f7bcaeSTom Rini 	spl_board_init();
15247f7bcaeSTom Rini #endif
15347f7bcaeSTom Rini 
15447f7bcaeSTom Rini 	boot_device = spl_boot_device();
15547f7bcaeSTom Rini 	debug("boot device - %d\n", boot_device);
15647f7bcaeSTom Rini 	switch (boot_device) {
157c57b953dSPavel Machek #ifdef CONFIG_SPL_RAM_DEVICE
158c57b953dSPavel Machek 	case BOOT_DEVICE_RAM:
159c57b953dSPavel Machek 		spl_ram_load_image();
160c57b953dSPavel Machek 		break;
161c57b953dSPavel Machek #endif
16247f7bcaeSTom Rini #ifdef CONFIG_SPL_MMC_SUPPORT
16347f7bcaeSTom Rini 	case BOOT_DEVICE_MMC1:
16447f7bcaeSTom Rini 	case BOOT_DEVICE_MMC2:
16547f7bcaeSTom Rini 	case BOOT_DEVICE_MMC2_2:
16647f7bcaeSTom Rini 		spl_mmc_load_image();
16747f7bcaeSTom Rini 		break;
16847f7bcaeSTom Rini #endif
16947f7bcaeSTom Rini #ifdef CONFIG_SPL_NAND_SUPPORT
17047f7bcaeSTom Rini 	case BOOT_DEVICE_NAND:
17147f7bcaeSTom Rini 		spl_nand_load_image();
17247f7bcaeSTom Rini 		break;
17347f7bcaeSTom Rini #endif
1746000992eSEnric Balletbo i Serra #ifdef CONFIG_SPL_ONENAND_SUPPORT
1756000992eSEnric Balletbo i Serra 	case BOOT_DEVICE_ONENAND:
1766000992eSEnric Balletbo i Serra 		spl_onenand_load_image();
1776000992eSEnric Balletbo i Serra 		break;
1786000992eSEnric Balletbo i Serra #endif
17933d34646SStefan Roese #ifdef CONFIG_SPL_NOR_SUPPORT
18033d34646SStefan Roese 	case BOOT_DEVICE_NOR:
18133d34646SStefan Roese 		spl_nor_load_image();
18233d34646SStefan Roese 		break;
18333d34646SStefan Roese #endif
18447f7bcaeSTom Rini #ifdef CONFIG_SPL_YMODEM_SUPPORT
18547f7bcaeSTom Rini 	case BOOT_DEVICE_UART:
18647f7bcaeSTom Rini 		spl_ymodem_load_image();
18747f7bcaeSTom Rini 		break;
18847f7bcaeSTom Rini #endif
18947f7bcaeSTom Rini #ifdef CONFIG_SPL_SPI_SUPPORT
19047f7bcaeSTom Rini 	case BOOT_DEVICE_SPI:
191a4cc1c48STom Rini 		spl_spi_load_image();
19247f7bcaeSTom Rini 		break;
19347f7bcaeSTom Rini #endif
1947ac2fe2dSIlya Yanok #ifdef CONFIG_SPL_ETH_SUPPORT
1957ac2fe2dSIlya Yanok 	case BOOT_DEVICE_CPGMAC:
1967ac2fe2dSIlya Yanok #ifdef CONFIG_SPL_ETH_DEVICE
1977ac2fe2dSIlya Yanok 		spl_net_load_image(CONFIG_SPL_ETH_DEVICE);
1987ac2fe2dSIlya Yanok #else
1997ac2fe2dSIlya Yanok 		spl_net_load_image(NULL);
2007ac2fe2dSIlya Yanok #endif
2017ac2fe2dSIlya Yanok 		break;
2027ac2fe2dSIlya Yanok #endif
20362a81431SIlya Yanok #ifdef CONFIG_SPL_USBETH_SUPPORT
20462a81431SIlya Yanok 	case BOOT_DEVICE_USBETH:
20562a81431SIlya Yanok 		spl_net_load_image("usb_ether");
20662a81431SIlya Yanok 		break;
20762a81431SIlya Yanok #endif
2088cffe5bdSDan Murphy #ifdef CONFIG_SPL_USB_SUPPORT
2098cffe5bdSDan Murphy 	case BOOT_DEVICE_USB:
2108cffe5bdSDan Murphy 		spl_usb_load_image();
2118cffe5bdSDan Murphy 		break;
2128cffe5bdSDan Murphy #endif
213fff40a7eSDan Murphy #ifdef CONFIG_SPL_SATA_SUPPORT
214fff40a7eSDan Murphy 	case BOOT_DEVICE_SATA:
215fff40a7eSDan Murphy 		spl_sata_load_image();
216fff40a7eSDan Murphy 		break;
217fff40a7eSDan Murphy #endif
21847f7bcaeSTom Rini 	default:
2191292eaf3STom Rini 		debug("SPL: Un-supported Boot Device\n");
22047f7bcaeSTom Rini 		hang();
22147f7bcaeSTom Rini 	}
22247f7bcaeSTom Rini 
22347f7bcaeSTom Rini 	switch (spl_image.os) {
22447f7bcaeSTom Rini 	case IH_OS_U_BOOT:
22547f7bcaeSTom Rini 		debug("Jumping to U-Boot\n");
22647f7bcaeSTom Rini 		break;
22747f7bcaeSTom Rini #ifdef CONFIG_SPL_OS_BOOT
22847f7bcaeSTom Rini 	case IH_OS_LINUX:
22947f7bcaeSTom Rini 		debug("Jumping to Linux\n");
23047f7bcaeSTom Rini 		spl_board_prepare_for_linux();
23147f7bcaeSTom Rini 		jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
23247f7bcaeSTom Rini #endif
23347f7bcaeSTom Rini 	default:
23442120981STom Rini 		debug("Unsupported OS image.. Jumping nevertheless..\n");
23547f7bcaeSTom Rini 	}
236a759f1e0SAllen Martin 	jump_to_image_no_args(&spl_image);
23747f7bcaeSTom Rini }
23847f7bcaeSTom Rini 
2396507f133STom Rini /*
2406507f133STom Rini  * This requires UART clocks to be enabled.  In order for this to work the
2416507f133STom Rini  * caller must ensure that the gd pointer is valid.
2426507f133STom Rini  */
24347f7bcaeSTom Rini void preloader_console_init(void)
24447f7bcaeSTom Rini {
24547f7bcaeSTom Rini 	gd->bd = &bdata;
24647f7bcaeSTom Rini 	gd->baudrate = CONFIG_BAUDRATE;
24747f7bcaeSTom Rini 
24847f7bcaeSTom Rini 	serial_init();		/* serial communications setup */
24947f7bcaeSTom Rini 
25047f7bcaeSTom Rini 	gd->have_console = 1;
25147f7bcaeSTom Rini 
25247f7bcaeSTom Rini 	puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
25347f7bcaeSTom Rini 			U_BOOT_TIME ")\n");
25447f7bcaeSTom Rini #ifdef CONFIG_SPL_DISPLAY_PRINT
25547f7bcaeSTom Rini 	spl_display_print();
25647f7bcaeSTom Rini #endif
25747f7bcaeSTom Rini }
258