blob: 691a431a9262dd0de653320a924f1d538e04debb [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
Tom Rini03de3052024-05-20 13:35:03 -060011#include <config.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060012#include <image.h>
Sean Andersonab121792023-10-14 16:47:44 -040013#include <imx_container.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glassff0960f2014-10-13 23:42:04 -060015#include <spi.h>
Christian Riesch32b11272011-12-09 09:47:35 +000016#include <spi_flash.h>
Nikita Kiryanov36afd452015-11-08 17:11:49 +020017#include <errno.h>
Tom Rinia4cc1c42012-08-14 14:34:10 -070018#include <spl.h>
Sean Andersona04d5f62023-11-08 11:48:56 -050019#include <spl_load.h>
Simon Glass401d1c42020-10-30 21:38:53 -060020#include <asm/global_data.h>
Sean Andersonb02c4e92023-10-14 16:47:55 -040021#include <asm/io.h>
Simon Glass7de8bd02021-08-07 07:24:01 -060022#include <dm/ofnode.h>
Philipp Tomsich2bac55b2017-04-17 17:45:11 +020023
Lokesh Vutla00d55952016-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 Anderson0c6c83e2023-11-08 11:48:38 -050027 struct spi_flash *flash = load->priv;
Lokesh Vutla00d55952016-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 Fanec649332019-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 Achath6dd18a62022-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 Riesch32b11272011-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 Glass2a2ee2a2016-09-24 18:20:13 -060057static int spl_spi_load_image(struct spl_image_info *spl_image,
58 struct spl_boot_device *bootdev)
Christian Riesch32b11272011-12-09 09:47:35 +000059{
Nikita Kiryanov36afd452015-11-08 17:11:49 +020060 int err = 0;
Peng Fanec649332019-09-23 10:18:41 +080061 unsigned int payload_offs;
Christian Riesch32b11272011-12-09 09:47:35 +000062 struct spi_flash *flash;
Vaishnav Achath6dd18a62022-06-03 11:32:15 +053063 unsigned int sf_bus = spl_spi_boot_bus();
64 unsigned int sf_cs = spl_spi_boot_cs();
Sean Andersona04d5f62023-11-08 11:48:56 -050065 struct spl_load_info load;
Christian Riesch32b11272011-12-09 09:47:35 +000066
67 /*
68 * Load U-Boot image from SPI flash into RAM
Patrick Delaunayb0cc1b82019-02-27 15:36:44 +010069 * In DM mode: defaults speed and mode will be
70 * taken from DT when available
Christian Riesch32b11272011-12-09 09:47:35 +000071 */
Vaishnav Achath6dd18a62022-06-03 11:32:15 +053072 flash = spi_flash_probe(sf_bus, sf_cs,
Nikita Kiryanov88e34e52014-08-20 15:08:48 +030073 CONFIG_SF_DEFAULT_SPEED,
74 CONFIG_SF_DEFAULT_MODE);
Christian Riesch32b11272011-12-09 09:47:35 +000075 if (!flash) {
Tom Rinia4cc1c42012-08-14 14:34:10 -070076 puts("SPI probe failed.\n");
Nikita Kiryanov36afd452015-11-08 17:11:49 +020077 return -ENODEV;
Christian Riesch32b11272011-12-09 09:47:35 +000078 }
79
Simon Glass3fd11272024-08-22 07:55:02 -060080 spl_load_init(&load, spl_spi_fit_read, flash, 1);
Sean Anderson14509a22023-11-08 11:48:57 -050081
Sean Andersona04d5f62023-11-08 11:48:56 -050082#if CONFIG_IS_ENABLED(OS_BOOT)
Sean Anderson14509a22023-11-08 11:48:57 -050083 if (spl_start_uboot()) {
84 int err = spl_load(spl_image, bootdev, &load, 0,
85 CFG_SYS_SPI_KERNEL_OFFS);
86
87 if (!err)
88 /* Read device tree. */
89 return spi_flash_read(flash, CFG_SYS_SPI_ARGS_OFFS,
90 CFG_SYS_SPI_ARGS_SIZE,
91 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
92 }
Sean Andersona04d5f62023-11-08 11:48:56 -050093#endif
94
Peng Fanec649332019-09-23 10:18:41 +080095 payload_offs = spl_spi_get_uboot_offs(flash);
Simon Glass414cc152021-08-07 07:24:03 -060096 if (CONFIG_IS_ENABLED(OF_REAL)) {
97 payload_offs = ofnode_conf_read_int("u-boot,spl-payload-offset",
98 payload_offs);
99 }
Philipp Tomsich2bac55b2017-04-17 17:45:11 +0200100
Sean Andersona04d5f62023-11-08 11:48:56 -0500101 err = spl_load(spl_image, bootdev, &load, 0, payload_offs);
102 if (IS_ENABLED(CONFIG_SPI_FLASH_SOFT_RESET))
103 err = spi_nor_remove(flash);
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200104 return err;
Christian Riesch32b11272011-12-09 09:47:35 +0000105}
Simon Glass139db7a2016-09-24 18:20:09 -0600106/* Use priorty 1 so that boards can override this */
Simon Glassebc4ef62016-11-30 15:30:50 -0700107SPL_LOAD_IMAGE_METHOD("SPI", 1, BOOT_DEVICE_SPI, spl_spi_load_image);