blob: 7a374bbfcc32c4b60ff9e09515417bdb0c1cf80d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Christian Riesch32b11272011-12-09 09:47:35 +00002/*
3 * Copyright (C) 2011 OMICRON electronics GmbH
4 *
Miquel Raynala430fa02018-08-16 17:30:07 +02005 * based on drivers/mtd/nand/raw/nand_spl_load.c
Christian Riesch32b11272011-12-09 09:47:35 +00006 *
7 * Copyright (C) 2011
8 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
Christian Riesch32b11272011-12-09 09:47:35 +00009 */
10
11#include <common.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060012#include <image.h>
Simon Glassff0960f2014-10-13 23:42:04 -060013#include <spi.h>
Christian Riesch32b11272011-12-09 09:47:35 +000014#include <spi_flash.h>
Nikita Kiryanov36afd452015-11-08 17:11:49 +020015#include <errno.h>
Tom Rinia4cc1c42012-08-14 14:34:10 -070016#include <spl.h>
Christian Riesch32b11272011-12-09 09:47:35 +000017
Philipp Tomsich2bac55b2017-04-17 17:45:11 +020018DECLARE_GLOBAL_DATA_PTR;
19
Tom Rinifa1a73f2014-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 Glass2a2ee2a2016-09-24 18:20:13 -060025static int spi_load_image_os(struct spl_image_info *spl_image,
26 struct spi_flash *flash,
Tom Rinifa1a73f2014-04-03 07:52:55 -040027 struct image_header *header)
28{
Marek Vasut7e0f2262016-04-29 00:44:54 +020029 int err;
30
Tom Rinifa1a73f2014-04-03 07:52:55 -040031 /* Read for a header, parse or error out. */
Michal Simek51c12312018-10-04 09:30:20 +020032 spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS, sizeof(*header),
Tom Rinifa1a73f2014-04-03 07:52:55 -040033 (void *)header);
34
35 if (image_get_magic(header) != IH_MAGIC)
36 return -1;
37
Simon Glass2a2ee2a2016-09-24 18:20:13 -060038 err = spl_parse_image_header(spl_image, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +020039 if (err)
40 return err;
Tom Rinifa1a73f2014-04-03 07:52:55 -040041
42 spi_flash_read(flash, CONFIG_SYS_SPI_KERNEL_OFFS,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060043 spl_image->size, (void *)spl_image->load_addr);
Tom Rinifa1a73f2014-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 Vutla00d55952016-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}
Peng Fanec649332019-09-23 10:18:41 +080066
67unsigned int __weak spl_spi_get_uboot_offs(struct spi_flash *flash)
68{
69 return CONFIG_SYS_SPI_U_BOOT_OFFS;
70}
71
Christian Riesch32b11272011-12-09 09:47:35 +000072/*
73 * The main entry for SPI booting. It's necessary that SDRAM is already
74 * configured and available since this code loads the main U-Boot image
75 * from SPI into SDRAM and starts it from there.
76 */
Simon Glass2a2ee2a2016-09-24 18:20:13 -060077static int spl_spi_load_image(struct spl_image_info *spl_image,
78 struct spl_boot_device *bootdev)
Christian Riesch32b11272011-12-09 09:47:35 +000079{
Nikita Kiryanov36afd452015-11-08 17:11:49 +020080 int err = 0;
Peng Fanec649332019-09-23 10:18:41 +080081 unsigned int payload_offs;
Christian Riesch32b11272011-12-09 09:47:35 +000082 struct spi_flash *flash;
Tom Rinia4cc1c42012-08-14 14:34:10 -070083 struct image_header *header;
Christian Riesch32b11272011-12-09 09:47:35 +000084
85 /*
86 * Load U-Boot image from SPI flash into RAM
Patrick Delaunayb0cc1b82019-02-27 15:36:44 +010087 * In DM mode: defaults speed and mode will be
88 * taken from DT when available
Christian Riesch32b11272011-12-09 09:47:35 +000089 */
90
Nikita Kiryanov88e34e52014-08-20 15:08:48 +030091 flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
92 CONFIG_SF_DEFAULT_CS,
93 CONFIG_SF_DEFAULT_SPEED,
94 CONFIG_SF_DEFAULT_MODE);
Christian Riesch32b11272011-12-09 09:47:35 +000095 if (!flash) {
Tom Rinia4cc1c42012-08-14 14:34:10 -070096 puts("SPI probe failed.\n");
Nikita Kiryanov36afd452015-11-08 17:11:49 +020097 return -ENODEV;
Christian Riesch32b11272011-12-09 09:47:35 +000098 }
99
Peng Fanec649332019-09-23 10:18:41 +0800100 payload_offs = spl_spi_get_uboot_offs(flash);
101
Michal Simek51c12312018-10-04 09:30:20 +0200102 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
Christian Riesch32b11272011-12-09 09:47:35 +0000103
Philipp Tomsich2bac55b2017-04-17 17:45:11 +0200104#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
105 payload_offs = fdtdec_get_config_int(gd->fdt_blob,
106 "u-boot,spl-payload-offset",
107 payload_offs);
108#endif
109
Tom Rinifa1a73f2014-04-03 07:52:55 -0400110#ifdef CONFIG_SPL_OS_BOOT
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600111 if (spl_start_uboot() || spi_load_image_os(spl_image, flash, header))
Tom Rinifa1a73f2014-04-03 07:52:55 -0400112#endif
113 {
114 /* Load u-boot, mkimage header is 64 bytes. */
Michal Simek51c12312018-10-04 09:30:20 +0200115 err = spi_flash_read(flash, payload_offs, sizeof(*header),
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200116 (void *)header);
Simon Glassa7044902017-01-16 07:03:27 -0700117 if (err) {
118 debug("%s: Failed to read from SPI flash (err=%d)\n",
119 __func__, err);
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200120 return err;
Simon Glassa7044902017-01-16 07:03:27 -0700121 }
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200122
Marek Vasut26ad6482018-05-31 17:59:29 +0200123 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
124 image_get_magic(header) == FDT_MAGIC) {
125 err = spi_flash_read(flash, payload_offs,
126 roundup(fdt_totalsize(header), 4),
127 (void *)CONFIG_SYS_LOAD_ADDR);
128 if (err)
129 return err;
130 err = spl_parse_image_header(spl_image,
131 (struct image_header *)CONFIG_SYS_LOAD_ADDR);
132 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
133 image_get_magic(header) == FDT_MAGIC) {
Lokesh Vutla00d55952016-05-24 10:34:40 +0530134 struct spl_load_info load;
135
136 debug("Found FIT\n");
137 load.dev = flash;
138 load.priv = NULL;
139 load.filename = NULL;
140 load.bl_len = 1;
141 load.read = spl_spi_fit_read;
Simon Glassf4d7d852016-09-24 18:20:16 -0600142 err = spl_load_simple_fit(spl_image, &load,
Philipp Tomsich2bac55b2017-04-17 17:45:11 +0200143 payload_offs,
Lokesh Vutla00d55952016-05-24 10:34:40 +0530144 header);
Peng Fand9bd2f42019-09-23 10:18:47 +0800145 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
146 struct spl_load_info load;
147
148 load.dev = flash;
149 load.priv = NULL;
150 load.filename = NULL;
151 load.bl_len = 1;
152 load.read = spl_spi_fit_read;
153
154 err = spl_load_imx_container(spl_image, &load,
155 payload_offs);
Lokesh Vutla00d55952016-05-24 10:34:40 +0530156 } else {
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600157 err = spl_parse_image_header(spl_image, header);
Lokesh Vutla00d55952016-05-24 10:34:40 +0530158 if (err)
159 return err;
Philipp Tomsich2bac55b2017-04-17 17:45:11 +0200160 err = spi_flash_read(flash, payload_offs,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600161 spl_image->size,
162 (void *)spl_image->load_addr);
Lokesh Vutla00d55952016-05-24 10:34:40 +0530163 }
Tom Rinifa1a73f2014-04-03 07:52:55 -0400164 }
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200165
166 return err;
Christian Riesch32b11272011-12-09 09:47:35 +0000167}
Simon Glass139db7a2016-09-24 18:20:09 -0600168/* Use priorty 1 so that boards can override this */
Simon Glassebc4ef62016-11-30 15:30:50 -0700169SPL_LOAD_IMAGE_METHOD("SPI", 1, BOOT_DEVICE_SPI, spl_spi_load_image);