2011-12-09 09:47:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 OMICRON electronics GmbH
|
|
|
|
*
|
|
|
|
* based on drivers/mtd/nand/nand_spl_load.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011
|
|
|
|
* Heiko Schocher, DENX Software Engineering, hs@denx.de.
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2011-12-09 09:47:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2014-10-14 05:42:04 +00:00
|
|
|
#include <spi.h>
|
2011-12-09 09:47:35 +00:00
|
|
|
#include <spi_flash.h>
|
2015-11-08 15:11:49 +00:00
|
|
|
#include <errno.h>
|
2012-08-14 21:34:10 +00:00
|
|
|
#include <spl.h>
|
2011-12-09 09:47:35 +00:00
|
|
|
|
2017-04-17 15:45:11 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2014-04-03 11:52:55 +00:00
|
|
|
#ifdef CONFIG_SPL_OS_BOOT
|
|
|
|
/*
|
|
|
|
* Load the kernel, check for a valid header we can parse, and if found load
|
|
|
|
* the kernel and then device tree.
|
|
|
|
*/
|
2016-09-25 00:20:13 +00:00
|
|
|
static int spi_load_image_os(struct spl_image_info *spl_image,
|
|
|
|
struct spi_flash *flash,
|
2014-04-03 11:52:55 +00:00
|
|
|
struct image_header *header)
|
|
|
|
{
|
2016-04-28 22:44:54 +00:00
|
|
|
int err;
|
|
|
|
|
2014-04-03 11:52:55 +00:00
|
|
|
/* Read for a header, parse or error out. */
|
|
|
|
spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS, 0x40,
|
|
|
|
(void *)header);
|
|
|
|
|
|
|
|
if (image_get_magic(header) != IH_MAGIC)
|
|
|
|
return -1;
|
|
|
|
|
2016-09-25 00:20:13 +00:00
|
|
|
err = spl_parse_image_header(spl_image, header);
|
2016-04-28 22:44:54 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
2014-04-03 11:52:55 +00:00
|
|
|
|
|
|
|
spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS,
|
2016-09-25 00:20:13 +00:00
|
|
|
spl_image->size, (void *)spl_image->load_addr);
|
2014-04-03 11:52:55 +00:00
|
|
|
|
|
|
|
/* Read device tree. */
|
|
|
|
spi_flash_read(flash, CONFIG_SYS_SPI_ARGS_OFFS,
|
|
|
|
CONFIG_SYS_SPI_ARGS_SIZE,
|
|
|
|
(void *)CONFIG_SYS_SPL_ARGS_ADDR);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-05-24 05:04:40 +00:00
|
|
|
static ulong spl_spi_fit_read(struct spl_load_info *load, ulong sector,
|
|
|
|
ulong count, void *buf)
|
|
|
|
{
|
|
|
|
struct spi_flash *flash = load->dev;
|
|
|
|
ulong ret;
|
|
|
|
|
|
|
|
ret = spi_flash_read(flash, sector, count, buf);
|
|
|
|
if (!ret)
|
|
|
|
return count;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2011-12-09 09:47:35 +00:00
|
|
|
/*
|
|
|
|
* The main entry for SPI booting. It's necessary that SDRAM is already
|
|
|
|
* configured and available since this code loads the main U-Boot image
|
|
|
|
* from SPI into SDRAM and starts it from there.
|
|
|
|
*/
|
2016-09-25 00:20:13 +00:00
|
|
|
static int spl_spi_load_image(struct spl_image_info *spl_image,
|
|
|
|
struct spl_boot_device *bootdev)
|
2011-12-09 09:47:35 +00:00
|
|
|
{
|
2015-11-08 15:11:49 +00:00
|
|
|
int err = 0;
|
2017-04-17 15:45:11 +00:00
|
|
|
unsigned payload_offs = CONFIG_SYS_SPI_U_BOOT_OFFS;
|
2011-12-09 09:47:35 +00:00
|
|
|
struct spi_flash *flash;
|
2012-08-14 21:34:10 +00:00
|
|
|
struct image_header *header;
|
2011-12-09 09:47:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Load U-Boot image from SPI flash into RAM
|
|
|
|
*/
|
|
|
|
|
2014-08-20 12:08:48 +00:00
|
|
|
flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
|
|
|
|
CONFIG_SF_DEFAULT_CS,
|
|
|
|
CONFIG_SF_DEFAULT_SPEED,
|
|
|
|
CONFIG_SF_DEFAULT_MODE);
|
2011-12-09 09:47:35 +00:00
|
|
|
if (!flash) {
|
2012-08-14 21:34:10 +00:00
|
|
|
puts("SPI probe failed.\n");
|
2015-11-08 15:11:49 +00:00
|
|
|
return -ENODEV;
|
2011-12-09 09:47:35 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 21:34:10 +00:00
|
|
|
/* use CONFIG_SYS_TEXT_BASE as temporary storage area */
|
|
|
|
header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
|
2011-12-09 09:47:35 +00:00
|
|
|
|
2017-04-17 15:45:11 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
|
|
|
|
payload_offs = fdtdec_get_config_int(gd->fdt_blob,
|
|
|
|
"u-boot,spl-payload-offset",
|
|
|
|
payload_offs);
|
|
|
|
#endif
|
|
|
|
|
2014-04-03 11:52:55 +00:00
|
|
|
#ifdef CONFIG_SPL_OS_BOOT
|
2016-09-25 00:20:13 +00:00
|
|
|
if (spl_start_uboot() || spi_load_image_os(spl_image, flash, header))
|
2014-04-03 11:52:55 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Load u-boot, mkimage header is 64 bytes. */
|
2017-04-17 15:45:11 +00:00
|
|
|
err = spi_flash_read(flash, payload_offs, 0x40,
|
2015-11-08 15:11:49 +00:00
|
|
|
(void *)header);
|
2017-01-16 14:03:27 +00:00
|
|
|
if (err) {
|
|
|
|
debug("%s: Failed to read from SPI flash (err=%d)\n",
|
|
|
|
__func__, err);
|
2015-11-08 15:11:49 +00:00
|
|
|
return err;
|
2017-01-16 14:03:27 +00:00
|
|
|
}
|
2015-11-08 15:11:49 +00:00
|
|
|
|
2016-11-16 10:54:39 +00:00
|
|
|
if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
|
|
|
|
image_get_magic(header) == FDT_MAGIC) {
|
2016-05-24 05:04:40 +00:00
|
|
|
struct spl_load_info load;
|
|
|
|
|
|
|
|
debug("Found FIT\n");
|
|
|
|
load.dev = flash;
|
|
|
|
load.priv = NULL;
|
|
|
|
load.filename = NULL;
|
|
|
|
load.bl_len = 1;
|
|
|
|
load.read = spl_spi_fit_read;
|
2016-09-25 00:20:16 +00:00
|
|
|
err = spl_load_simple_fit(spl_image, &load,
|
2017-04-17 15:45:11 +00:00
|
|
|
payload_offs,
|
2016-05-24 05:04:40 +00:00
|
|
|
header);
|
|
|
|
} else {
|
2016-09-25 00:20:13 +00:00
|
|
|
err = spl_parse_image_header(spl_image, header);
|
2016-05-24 05:04:40 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
2017-04-17 15:45:11 +00:00
|
|
|
err = spi_flash_read(flash, payload_offs,
|
2016-09-25 00:20:13 +00:00
|
|
|
spl_image->size,
|
|
|
|
(void *)spl_image->load_addr);
|
2016-05-24 05:04:40 +00:00
|
|
|
}
|
2014-04-03 11:52:55 +00:00
|
|
|
}
|
2015-11-08 15:11:49 +00:00
|
|
|
|
|
|
|
return err;
|
2011-12-09 09:47:35 +00:00
|
|
|
}
|
2016-09-25 00:20:09 +00:00
|
|
|
/* Use priorty 1 so that boards can override this */
|
2016-11-30 22:30:50 +00:00
|
|
|
SPL_LOAD_IMAGE_METHOD("SPI", 1, BOOT_DEVICE_SPI, spl_spi_load_image);
|