2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2011-12-09 09:47:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 OMICRON electronics GmbH
|
|
|
|
*
|
2018-08-16 15:30:07 +00:00
|
|
|
* based on drivers/mtd/nand/raw/nand_spl_load.c
|
2011-12-09 09:47:35 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2011
|
|
|
|
* Heiko Schocher, DENX Software Engineering, hs@denx.de.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-05-10 17:40:01 +00:00
|
|
|
#include <image.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.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>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.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. */
|
2018-10-04 07:30:20 +00:00
|
|
|
spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS, sizeof(*header),
|
2014-04-03 11:52:55 +00:00
|
|
|
(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;
|
|
|
|
}
|
2019-09-23 02:18:41 +00:00
|
|
|
|
|
|
|
unsigned int __weak spl_spi_get_uboot_offs(struct spi_flash *flash)
|
|
|
|
{
|
|
|
|
return CONFIG_SYS_SPI_U_BOOT_OFFS;
|
|
|
|
}
|
|
|
|
|
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;
|
2019-09-23 02:18:41 +00:00
|
|
|
unsigned int payload_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
|
2019-02-27 14:36:44 +00:00
|
|
|
* In DM mode: defaults speed and mode will be
|
|
|
|
* taken from DT when available
|
2011-12-09 09:47:35 +00:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-23 02:18:41 +00:00
|
|
|
payload_offs = spl_spi_get_uboot_offs(flash);
|
|
|
|
|
2018-10-04 07:30:20 +00:00
|
|
|
header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
|
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. */
|
2018-10-04 07:30:20 +00:00
|
|
|
err = spi_flash_read(flash, payload_offs, sizeof(*header),
|
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
|
|
|
|
2018-05-31 15:59:29 +00:00
|
|
|
if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
|
|
|
|
image_get_magic(header) == FDT_MAGIC) {
|
|
|
|
err = spi_flash_read(flash, payload_offs,
|
|
|
|
roundup(fdt_totalsize(header), 4),
|
|
|
|
(void *)CONFIG_SYS_LOAD_ADDR);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
err = spl_parse_image_header(spl_image,
|
|
|
|
(struct image_header *)CONFIG_SYS_LOAD_ADDR);
|
|
|
|
} else 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);
|
2019-09-23 02:18:47 +00:00
|
|
|
} else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
|
|
|
|
struct spl_load_info load;
|
|
|
|
|
|
|
|
load.dev = flash;
|
|
|
|
load.priv = NULL;
|
|
|
|
load.filename = NULL;
|
|
|
|
load.bl_len = 1;
|
|
|
|
load.read = spl_spi_fit_read;
|
|
|
|
|
|
|
|
err = spl_load_imx_container(spl_image, &load,
|
|
|
|
payload_offs);
|
2016-05-24 05:04:40 +00:00
|
|
|
} 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);
|