blob: 42880d56b91046b08dfd790c5c1275e7b81469e6 [file] [log] [blame]
Christian Riesch23f16a82011-12-09 09:47:35 +00001/*
2 * Copyright (C) 2011 OMICRON electronics GmbH
3 *
4 * based on drivers/mtd/nand/nand_spl_load.c
5 *
6 * Copyright (C) 2011
7 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
8 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Christian Riesch23f16a82011-12-09 09:47:35 +000010 */
11
12#include <common.h>
Simon Glassd34b4562014-10-13 23:42:04 -060013#include <spi.h>
Christian Riesch23f16a82011-12-09 09:47:35 +000014#include <spi_flash.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020015#include <errno.h>
Tom Rini1e2abf92012-08-14 14:34:10 -070016#include <spl.h>
Christian Riesch23f16a82011-12-09 09:47:35 +000017
Philipp Tomsiche8eba3f2017-04-17 17:45:11 +020018DECLARE_GLOBAL_DATA_PTR;
19
Tom Rinid4cdb7a2014-04-03 07:52:55 -040020#ifdef CONFIG_SPL_OS_BOOT
21/*
22 * Load the kernel, check for a valid header we can parse, and if found load
23 * the kernel and then device tree.
24 */
Simon Glassee306792016-09-24 18:20:13 -060025static int spi_load_image_os(struct spl_image_info *spl_image,
26 struct spi_flash *flash,
Tom Rinid4cdb7a2014-04-03 07:52:55 -040027 struct image_header *header)
28{
Marek Vasut02266e22016-04-29 00:44:54 +020029 int err;
30
Tom Rinid4cdb7a2014-04-03 07:52:55 -040031 /* Read for a header, parse or error out. */
32 spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS, 0x40,
33 (void *)header);
34
35 if (image_get_magic(header) != IH_MAGIC)
36 return -1;
37
Simon Glassee306792016-09-24 18:20:13 -060038 err = spl_parse_image_header(spl_image, header);
Marek Vasut02266e22016-04-29 00:44:54 +020039 if (err)
40 return err;
Tom Rinid4cdb7a2014-04-03 07:52:55 -040041
42 spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS,
Simon Glassee306792016-09-24 18:20:13 -060043 spl_image->size, (void *)spl_image->load_addr);
Tom Rinid4cdb7a2014-04-03 07:52:55 -040044
45 /* Read device tree. */
46 spi_flash_read(flash, CONFIG_SYS_SPI_ARGS_OFFS,
47 CONFIG_SYS_SPI_ARGS_SIZE,
48 (void *)CONFIG_SYS_SPL_ARGS_ADDR);
49
50 return 0;
51}
52#endif
53
Lokesh Vutla255c92a2016-05-24 10:34:40 +053054static ulong spl_spi_fit_read(struct spl_load_info *load, ulong sector,
55 ulong count, void *buf)
56{
57 struct spi_flash *flash = load->dev;
58 ulong ret;
59
60 ret = spi_flash_read(flash, sector, count, buf);
61 if (!ret)
62 return count;
63 else
64 return 0;
65}
Christian Riesch23f16a82011-12-09 09:47:35 +000066/*
67 * The main entry for SPI booting. It's necessary that SDRAM is already
68 * configured and available since this code loads the main U-Boot image
69 * from SPI into SDRAM and starts it from there.
70 */
Simon Glassee306792016-09-24 18:20:13 -060071static int spl_spi_load_image(struct spl_image_info *spl_image,
72 struct spl_boot_device *bootdev)
Christian Riesch23f16a82011-12-09 09:47:35 +000073{
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020074 int err = 0;
Philipp Tomsiche8eba3f2017-04-17 17:45:11 +020075 unsigned payload_offs = CONFIG_SYS_SPI_U_BOOT_OFFS;
Christian Riesch23f16a82011-12-09 09:47:35 +000076 struct spi_flash *flash;
Tom Rini1e2abf92012-08-14 14:34:10 -070077 struct image_header *header;
Christian Riesch23f16a82011-12-09 09:47:35 +000078
79 /*
80 * Load U-Boot image from SPI flash into RAM
81 */
82
Nikita Kiryanov6c6ccdf2014-08-20 15:08:48 +030083 flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
84 CONFIG_SF_DEFAULT_CS,
85 CONFIG_SF_DEFAULT_SPEED,
86 CONFIG_SF_DEFAULT_MODE);
Christian Riesch23f16a82011-12-09 09:47:35 +000087 if (!flash) {
Tom Rini1e2abf92012-08-14 14:34:10 -070088 puts("SPI probe failed.\n");
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020089 return -ENODEV;
Christian Riesch23f16a82011-12-09 09:47:35 +000090 }
91
Tom Rini1e2abf92012-08-14 14:34:10 -070092 /* use CONFIG_SYS_TEXT_BASE as temporary storage area */
93 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
Christian Riesch23f16a82011-12-09 09:47:35 +000094
Philipp Tomsiche8eba3f2017-04-17 17:45:11 +020095#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
96 payload_offs = fdtdec_get_config_int(gd->fdt_blob,
97 "u-boot,spl-payload-offset",
98 payload_offs);
99#endif
100
Tom Rinid4cdb7a2014-04-03 07:52:55 -0400101#ifdef CONFIG_SPL_OS_BOOT
Simon Glassee306792016-09-24 18:20:13 -0600102 if (spl_start_uboot() || spi_load_image_os(spl_image, flash, header))
Tom Rinid4cdb7a2014-04-03 07:52:55 -0400103#endif
104 {
105 /* Load u-boot, mkimage header is 64 bytes. */
Philipp Tomsiche8eba3f2017-04-17 17:45:11 +0200106 err = spi_flash_read(flash, payload_offs, 0x40,
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200107 (void *)header);
Simon Glass2c066d62017-01-16 07:03:27 -0700108 if (err) {
109 debug("%s: Failed to read from SPI flash (err=%d)\n",
110 __func__, err);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200111 return err;
Simon Glass2c066d62017-01-16 07:03:27 -0700112 }
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200113
tomas.melin@vaisala.come379f592016-11-16 12:54:39 +0200114 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
115 image_get_magic(header) == FDT_MAGIC) {
Lokesh Vutla255c92a2016-05-24 10:34:40 +0530116 struct spl_load_info load;
117
118 debug("Found FIT\n");
119 load.dev = flash;
120 load.priv = NULL;
121 load.filename = NULL;
122 load.bl_len = 1;
123 load.read = spl_spi_fit_read;
Simon Glass43a734f2016-09-24 18:20:16 -0600124 err = spl_load_simple_fit(spl_image, &load,
Philipp Tomsiche8eba3f2017-04-17 17:45:11 +0200125 payload_offs,
Lokesh Vutla255c92a2016-05-24 10:34:40 +0530126 header);
127 } else {
Simon Glassee306792016-09-24 18:20:13 -0600128 err = spl_parse_image_header(spl_image, header);
Lokesh Vutla255c92a2016-05-24 10:34:40 +0530129 if (err)
130 return err;
Philipp Tomsiche8eba3f2017-04-17 17:45:11 +0200131 err = spi_flash_read(flash, payload_offs,
Simon Glassee306792016-09-24 18:20:13 -0600132 spl_image->size,
133 (void *)spl_image->load_addr);
Lokesh Vutla255c92a2016-05-24 10:34:40 +0530134 }
Tom Rinid4cdb7a2014-04-03 07:52:55 -0400135 }
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200136
137 return err;
Christian Riesch23f16a82011-12-09 09:47:35 +0000138}
Simon Glassb9f6d892016-09-24 18:20:09 -0600139/* Use priorty 1 so that boards can override this */
Simon Glass4fc1f252016-11-30 15:30:50 -0700140SPL_LOAD_IMAGE_METHOD("SPI", 1, BOOT_DEVICE_SPI, spl_spi_load_image);