blob: 89de73c726cd294db3922bf1173b0ab677306e2b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Christian Riesch23f16a82011-12-09 09:47:35 +00002/*
3 * Copyright (C) 2011 OMICRON electronics GmbH
4 *
Miquel Raynal1f1ae152018-08-16 17:30:07 +02005 * based on drivers/mtd/nand/raw/nand_spl_load.c
Christian Riesch23f16a82011-12-09 09:47:35 +00006 *
7 * Copyright (C) 2011
8 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
Christian Riesch23f16a82011-12-09 09:47:35 +00009 */
10
11#include <common.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060012#include <image.h>
Sean Anderson952ed672023-10-14 16:47:44 -040013#include <imx_container.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glassd34b4562014-10-13 23:42:04 -060015#include <spi.h>
Christian Riesch23f16a82011-12-09 09:47:35 +000016#include <spi_flash.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020017#include <errno.h>
Tom Rini1e2abf92012-08-14 14:34:10 -070018#include <spl.h>
Sean Anderson38e4f622023-11-08 11:48:56 -050019#include <spl_load.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.h>
Sean Anderson5ff77722023-10-14 16:47:55 -040021#include <asm/io.h>
Simon Glass0034d962021-08-07 07:24:01 -060022#include <dm/ofnode.h>
Philipp Tomsiche8eba3f2017-04-17 17:45:11 +020023
Lokesh Vutla255c92a2016-05-24 10:34:40 +053024static ulong spl_spi_fit_read(struct spl_load_info *load, ulong sector,
25 ulong count, void *buf)
26{
Sean Anderson2b5f9c82023-11-08 11:48:38 -050027 struct spi_flash *flash = load->priv;
Lokesh Vutla255c92a2016-05-24 10:34:40 +053028 ulong ret;
29
30 ret = spi_flash_read(flash, sector, count, buf);
31 if (!ret)
32 return count;
33 else
34 return 0;
35}
Peng Fan3a571692019-09-23 10:18:41 +080036
37unsigned int __weak spl_spi_get_uboot_offs(struct spi_flash *flash)
38{
39 return CONFIG_SYS_SPI_U_BOOT_OFFS;
40}
41
Vaishnav Achath58a0cdb2022-06-03 11:32:15 +053042u32 __weak spl_spi_boot_bus(void)
43{
44 return CONFIG_SF_DEFAULT_BUS;
45}
46
47u32 __weak spl_spi_boot_cs(void)
48{
49 return CONFIG_SF_DEFAULT_CS;
50}
51
Christian Riesch23f16a82011-12-09 09:47:35 +000052/*
53 * The main entry for SPI booting. It's necessary that SDRAM is already
54 * configured and available since this code loads the main U-Boot image
55 * from SPI into SDRAM and starts it from there.
56 */
Simon Glassee306792016-09-24 18:20:13 -060057static int spl_spi_load_image(struct spl_image_info *spl_image,
58 struct spl_boot_device *bootdev)
Christian Riesch23f16a82011-12-09 09:47:35 +000059{
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020060 int err = 0;
Peng Fan3a571692019-09-23 10:18:41 +080061 unsigned int payload_offs;
Christian Riesch23f16a82011-12-09 09:47:35 +000062 struct spi_flash *flash;
Vaishnav Achath58a0cdb2022-06-03 11:32:15 +053063 unsigned int sf_bus = spl_spi_boot_bus();
64 unsigned int sf_cs = spl_spi_boot_cs();
Sean Anderson38e4f622023-11-08 11:48:56 -050065 struct spl_load_info load;
Christian Riesch23f16a82011-12-09 09:47:35 +000066
67 /*
68 * Load U-Boot image from SPI flash into RAM
Patrick Delaunayfa19c652019-02-27 15:36:44 +010069 * In DM mode: defaults speed and mode will be
70 * taken from DT when available
Christian Riesch23f16a82011-12-09 09:47:35 +000071 */
Vaishnav Achath58a0cdb2022-06-03 11:32:15 +053072 flash = spi_flash_probe(sf_bus, sf_cs,
Nikita Kiryanov6c6ccdf2014-08-20 15:08:48 +030073 CONFIG_SF_DEFAULT_SPEED,
74 CONFIG_SF_DEFAULT_MODE);
Christian Riesch23f16a82011-12-09 09:47:35 +000075 if (!flash) {
Tom Rini1e2abf92012-08-14 14:34:10 -070076 puts("SPI probe failed.\n");
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020077 return -ENODEV;
Christian Riesch23f16a82011-12-09 09:47:35 +000078 }
79
Sean Andersonb664e672023-11-08 11:48:57 -050080 load.priv = flash;
81 spl_set_bl_len(&load, 1);
82 load.read = spl_spi_fit_read;
83
Sean Anderson38e4f622023-11-08 11:48:56 -050084#if CONFIG_IS_ENABLED(OS_BOOT)
Sean Andersonb664e672023-11-08 11:48:57 -050085 if (spl_start_uboot()) {
86 int err = spl_load(spl_image, bootdev, &load, 0,
87 CFG_SYS_SPI_KERNEL_OFFS);
88
89 if (!err)
90 /* Read device tree. */
91 return spi_flash_read(flash, CFG_SYS_SPI_ARGS_OFFS,
92 CFG_SYS_SPI_ARGS_SIZE,
93 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
94 }
Sean Anderson38e4f622023-11-08 11:48:56 -050095#endif
Christian Riesch23f16a82011-12-09 09:47:35 +000096
Sean Anderson38e4f622023-11-08 11:48:56 -050097 payload_offs = spl_spi_get_uboot_offs(flash);
Simon Glass3580f6d2021-08-07 07:24:03 -060098 if (CONFIG_IS_ENABLED(OF_REAL)) {
99 payload_offs = ofnode_conf_read_int("u-boot,spl-payload-offset",
100 payload_offs);
101 }
Philipp Tomsiche8eba3f2017-04-17 17:45:11 +0200102
Sean Anderson38e4f622023-11-08 11:48:56 -0500103 err = spl_load(spl_image, bootdev, &load, 0, payload_offs);
104 if (IS_ENABLED(CONFIG_SPI_FLASH_SOFT_RESET))
105 err = spi_nor_remove(flash);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200106 return err;
Christian Riesch23f16a82011-12-09 09:47:35 +0000107}
Simon Glassb9f6d892016-09-24 18:20:09 -0600108/* Use priorty 1 so that boards can override this */
Simon Glass4fc1f252016-11-30 15:30:50 -0700109SPL_LOAD_IMAGE_METHOD("SPI", 1, BOOT_DEVICE_SPI, spl_spi_load_image);