blob: d6b0e01075913547efafbf690d539112f106c9c5 [file] [log] [blame]
Aneesh Vb8e60b92011-07-21 09:10:21 -04001/*
2 * (C) Copyright 2010
3 * Texas Instruments, <www.ti.com>
4 *
5 * Aneesh V <aneesh@ti.com>
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Aneesh Vb8e60b92011-07-21 09:10:21 -04008 */
9#include <common.h>
Tom Rini28591df2012-08-13 12:03:19 -070010#include <spl.h>
Aneesh Vb8e60b92011-07-21 09:10:21 -040011#include <asm/u-boot.h>
Simon Schwarz992dcf72011-09-14 15:29:26 -040012#include <nand.h>
Aneesh V13a74c12011-07-21 09:10:27 -040013#include <fat.h>
Simon Glass50402412011-10-10 08:55:19 +000014#include <version.h>
Aneesh V13a74c12011-07-21 09:10:27 -040015#include <i2c.h>
16#include <image.h>
Aneesh V0560be22011-10-21 12:29:34 -040017#include <malloc.h>
Andreas Müller0cda7a42012-01-04 15:26:24 +000018#include <linux/compiler.h>
Aneesh Vb8e60b92011-07-21 09:10:21 -040019
20DECLARE_GLOBAL_DATA_PTR;
21
Stefan Roese5f169922012-08-28 10:50:59 +020022#ifndef CONFIG_SYS_UBOOT_START
23#define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE
24#endif
Stefano Babicfb5d62a2012-08-23 12:46:16 +020025#ifndef CONFIG_SYS_MONITOR_LEN
26#define CONFIG_SYS_MONITOR_LEN (200 * 1024)
27#endif
28
Tom Rini28591df2012-08-13 12:03:19 -070029u32 *boot_params_ptr = NULL;
Simon Schwarze21d2d82011-09-14 15:33:34 -040030struct spl_image_info spl_image;
31
Tom Rini31dfba42012-08-22 15:31:05 -070032/* Define board data structure */
Aneesh Vb8e60b92011-07-21 09:10:21 -040033static bd_t bdata __attribute__ ((section(".data")));
34
Simon Schwarz305bc4c2012-03-15 04:01:38 +000035/*
36 * Default function to determine if u-boot or the OS should
37 * be started. This implementation always returns 1.
38 *
39 * Please implement your own board specific funcion to do this.
40 *
41 * RETURN
42 * 0 to not start u-boot
43 * positive if u-boot should start
44 */
45#ifdef CONFIG_SPL_OS_BOOT
46__weak int spl_start_uboot(void)
47{
Tom Rini7b516862012-08-14 12:06:43 -070048 puts("SPL: Please implement spl_start_uboot() for your board\n");
49 puts("SPL: Direct Linux boot not active!\n");
Simon Schwarz305bc4c2012-03-15 04:01:38 +000050 return 1;
51}
52#endif
53
Stefan Roeseec90d342012-08-23 08:34:21 +020054/*
55 * Weak default function for board specific cleanup/preparation before
56 * Linux boot. Some boards/platforms might not need it, so just provide
57 * an empty stub here.
58 */
59__weak void spl_board_prepare_for_linux(void)
60{
61 /* Nothing to do! */
62}
63
Simon Schwarze21d2d82011-09-14 15:33:34 -040064void spl_parse_image_header(const struct image_header *header)
Aneesh V13a74c12011-07-21 09:10:27 -040065{
66 u32 header_size = sizeof(struct image_header);
67
Stefan Roese292db772012-08-27 12:50:57 +020068 if (image_get_magic(header) == IH_MAGIC) {
Stefan Roese00b57b32012-08-27 12:50:58 +020069 if (spl_image.flags & SPL_COPY_PAYLOAD_ONLY) {
70 /*
71 * On some system (e.g. powerpc), the load-address and
72 * entry-point is located at address 0. We can't load
73 * to 0-0x40. So skip header in this case.
74 */
75 spl_image.load_addr = image_get_load(header);
76 spl_image.entry_point = image_get_ep(header);
77 spl_image.size = image_get_data_size(header);
78 } else {
79 spl_image.entry_point = image_get_load(header);
80 /* Load including the header */
81 spl_image.load_addr = spl_image.entry_point -
82 header_size;
83 spl_image.size = image_get_data_size(header) +
84 header_size;
85 }
Stefan Roese292db772012-08-27 12:50:57 +020086 spl_image.os = image_get_os(header);
87 spl_image.name = image_get_name(header);
Aneesh V13a74c12011-07-21 09:10:27 -040088 debug("spl: payload image: %s load addr: 0x%x size: %d\n",
Simon Schwarze21d2d82011-09-14 15:33:34 -040089 spl_image.name, spl_image.load_addr, spl_image.size);
Aneesh V13a74c12011-07-21 09:10:27 -040090 } else {
91 /* Signature not found - assume u-boot.bin */
Tom Rini7b516862012-08-14 12:06:43 -070092 debug("mkimage signature not found - ih_magic = %x\n",
Aneesh V13a74c12011-07-21 09:10:27 -040093 header->ih_magic);
Aneesh V13a74c12011-07-21 09:10:27 -040094 /* Let's assume U-Boot will not be more than 200 KB */
Stefano Babicfb5d62a2012-08-23 12:46:16 +020095 spl_image.size = CONFIG_SYS_MONITOR_LEN;
Stefan Roese5f169922012-08-28 10:50:59 +020096 spl_image.entry_point = CONFIG_SYS_UBOOT_START;
Simon Schwarze21d2d82011-09-14 15:33:34 -040097 spl_image.load_addr = CONFIG_SYS_TEXT_BASE;
98 spl_image.os = IH_OS_U_BOOT;
99 spl_image.name = "U-Boot";
Aneesh V13a74c12011-07-21 09:10:27 -0400100 }
101}
102
Allen Martinc67e4852012-10-19 21:08:22 +0000103__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
Aneesh V13a74c12011-07-21 09:10:27 -0400104{
SRICHARAN R3f30b0a2013-04-24 00:41:24 +0000105 typedef void __noreturn (*image_entry_noargs_t)(void);
106
Aneesh V13a74c12011-07-21 09:10:27 -0400107 image_entry_noargs_t image_entry =
Allen Martinc67e4852012-10-19 21:08:22 +0000108 (image_entry_noargs_t) spl_image->entry_point;
Aneesh V13a74c12011-07-21 09:10:27 -0400109
Allen Martinc67e4852012-10-19 21:08:22 +0000110 debug("image entry point: 0x%X\n", spl_image->entry_point);
SRICHARAN R3f30b0a2013-04-24 00:41:24 +0000111 image_entry();
Aneesh V13a74c12011-07-21 09:10:27 -0400112}
113
Pavel Machekde997252012-08-30 22:42:11 +0200114#ifdef CONFIG_SPL_RAM_DEVICE
115static void spl_ram_load_image(void)
116{
117 const struct image_header *header;
118
119 /*
120 * Get the header. It will point to an address defined by handoff
121 * which will tell where the image located inside the flash. For
122 * now, it will temporary fixed to address pointed by U-Boot.
123 */
124 header = (struct image_header *)
125 (CONFIG_SYS_TEXT_BASE - sizeof(struct image_header));
126
127 spl_parse_image_header(header);
128}
129#endif
130
Tom Rini31dfba42012-08-22 15:31:05 -0700131void board_init_r(gd_t *dummy1, ulong dummy2)
Aneesh Vb8e60b92011-07-21 09:10:21 -0400132{
Aneesh V13a74c12011-07-21 09:10:27 -0400133 u32 boot_device;
134 debug(">>spl:board_init_r()\n");
135
Tom Rini730bae72012-08-13 14:13:07 -0700136#ifdef CONFIG_SYS_SPL_MALLOC_START
Aneesh V0560be22011-10-21 12:29:34 -0400137 mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
138 CONFIG_SYS_SPL_MALLOC_SIZE);
Tom Rini730bae72012-08-13 14:13:07 -0700139#endif
Aneesh V0560be22011-10-21 12:29:34 -0400140
Stefan Roeseec90d342012-08-23 08:34:21 +0200141#ifndef CONFIG_PPC
142 /*
143 * timer_init() does not exist on PPC systems. The timer is initialized
144 * and enabled (decrementer) in interrupt_init() here.
145 */
Ilya Yanoka84e3d92012-09-17 10:26:26 +0000146 timer_init();
Stefan Roeseec90d342012-08-23 08:34:21 +0200147#endif
Ilya Yanoka84e3d92012-09-17 10:26:26 +0000148
Tom Riniead66c12011-11-23 05:13:06 +0000149#ifdef CONFIG_SPL_BOARD_INIT
150 spl_board_init();
151#endif
152
Tom Rini0be93ff2012-08-13 12:53:23 -0700153 boot_device = spl_boot_device();
Aneesh V13a74c12011-07-21 09:10:27 -0400154 debug("boot device - %d\n", boot_device);
155 switch (boot_device) {
Pavel Machekde997252012-08-30 22:42:11 +0200156#ifdef CONFIG_SPL_RAM_DEVICE
157 case BOOT_DEVICE_RAM:
158 spl_ram_load_image();
159 break;
160#endif
Simon Schwarz992dcf72011-09-14 15:29:26 -0400161#ifdef CONFIG_SPL_MMC_SUPPORT
Aneesh V13a74c12011-07-21 09:10:27 -0400162 case BOOT_DEVICE_MMC1:
163 case BOOT_DEVICE_MMC2:
Balaji T Ke6c68892012-03-12 02:25:47 +0000164 case BOOT_DEVICE_MMC2_2:
Simon Schwarze21d2d82011-09-14 15:33:34 -0400165 spl_mmc_load_image();
Aneesh V13a74c12011-07-21 09:10:27 -0400166 break;
Simon Schwarz992dcf72011-09-14 15:29:26 -0400167#endif
168#ifdef CONFIG_SPL_NAND_SUPPORT
169 case BOOT_DEVICE_NAND:
Simon Schwarze21d2d82011-09-14 15:33:34 -0400170 spl_nand_load_image();
Simon Schwarz992dcf72011-09-14 15:29:26 -0400171 break;
172#endif
Enric Balletbo i Serrad6b5eed2013-02-07 23:14:48 +0000173#ifdef CONFIG_SPL_ONENAND_SUPPORT
174 case BOOT_DEVICE_ONENAND:
175 spl_onenand_load_image();
176 break;
177#endif
Stefan Roese900c1772012-08-27 12:50:59 +0200178#ifdef CONFIG_SPL_NOR_SUPPORT
179 case BOOT_DEVICE_NOR:
180 spl_nor_load_image();
181 break;
182#endif
Matt Porter79bfa732012-01-31 12:03:57 +0000183#ifdef CONFIG_SPL_YMODEM_SUPPORT
184 case BOOT_DEVICE_UART:
185 spl_ymodem_load_image();
186 break;
187#endif
Tom Riniff7a6052012-08-14 12:25:23 -0700188#ifdef CONFIG_SPL_SPI_SUPPORT
189 case BOOT_DEVICE_SPI:
Tom Rini1e2abf92012-08-14 14:34:10 -0700190 spl_spi_load_image();
191 break;
Tom Riniff7a6052012-08-14 12:25:23 -0700192#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000193#ifdef CONFIG_SPL_ETH_SUPPORT
194 case BOOT_DEVICE_CPGMAC:
195#ifdef CONFIG_SPL_ETH_DEVICE
196 spl_net_load_image(CONFIG_SPL_ETH_DEVICE);
197#else
198 spl_net_load_image(NULL);
199#endif
200 break;
201#endif
Ilya Yanokfc3a0f02013-02-05 11:36:24 +0000202#ifdef CONFIG_SPL_USBETH_SUPPORT
203 case BOOT_DEVICE_USBETH:
204 spl_net_load_image("usb_ether");
205 break;
206#endif
Aneesh V13a74c12011-07-21 09:10:27 -0400207 default:
Tom Rinid939ef02012-08-27 14:57:26 -0700208 debug("SPL: Un-supported Boot Device\n");
Aneesh V13a74c12011-07-21 09:10:27 -0400209 hang();
Aneesh V13a74c12011-07-21 09:10:27 -0400210 }
211
Simon Schwarze21d2d82011-09-14 15:33:34 -0400212 switch (spl_image.os) {
Aneesh V13a74c12011-07-21 09:10:27 -0400213 case IH_OS_U_BOOT:
214 debug("Jumping to U-Boot\n");
Aneesh V13a74c12011-07-21 09:10:27 -0400215 break;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000216#ifdef CONFIG_SPL_OS_BOOT
217 case IH_OS_LINUX:
218 debug("Jumping to Linux\n");
219 spl_board_prepare_for_linux();
220 jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000221#endif
Aneesh V13a74c12011-07-21 09:10:27 -0400222 default:
Tom Rini79178462012-08-27 14:58:28 -0700223 debug("Unsupported OS image.. Jumping nevertheless..\n");
Aneesh V13a74c12011-07-21 09:10:27 -0400224 }
Allen Martinc67e4852012-10-19 21:08:22 +0000225 jump_to_image_no_args(&spl_image);
Aneesh Vb8e60b92011-07-21 09:10:21 -0400226}
227
Tom Rini31dfba42012-08-22 15:31:05 -0700228/*
229 * This requires UART clocks to be enabled. In order for this to work the
230 * caller must ensure that the gd pointer is valid.
231 */
Aneesh Vb8e60b92011-07-21 09:10:21 -0400232void preloader_console_init(void)
233{
Aneesh Vb8e60b92011-07-21 09:10:21 -0400234 gd->bd = &bdata;
Aneesh Vb8e60b92011-07-21 09:10:21 -0400235 gd->baudrate = CONFIG_BAUDRATE;
236
Aneesh Vb8e60b92011-07-21 09:10:21 -0400237 serial_init(); /* serial communications setup */
238
Ilya Yanok74193c62011-11-01 13:16:03 +0000239 gd->have_console = 1;
240
Tom Rini7b516862012-08-14 12:06:43 -0700241 puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
242 U_BOOT_TIME ")\n");
Tom Rinife3b0c72012-08-13 11:37:56 -0700243#ifdef CONFIG_SPL_DISPLAY_PRINT
244 spl_display_print();
245#endif
Tom Rini09c797b2011-10-04 04:59:23 +0000246}