1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2011 OMICRON electronics GmbH
4 *
5 * based on drivers/mtd/nand/raw/nand_spl_load.c
6 *
7 * Copyright (C) 2011
8 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
9 */
10
11 #include <config.h>
12 #include <image.h>
13 #include <imx_container.h>
14 #include <log.h>
15 #include <spi.h>
16 #include <spi_flash.h>
17 #include <errno.h>
18 #include <spl.h>
19 #include <spl_load.h>
20 #include <asm/global_data.h>
21 #include <asm/io.h>
22 #include <dm/ofnode.h>
23
spl_spi_fit_read(struct spl_load_info * load,ulong sector,ulong count,void * buf)24 static ulong spl_spi_fit_read(struct spl_load_info *load, ulong sector,
25 ulong count, void *buf)
26 {
27 struct spi_flash *flash = load->priv;
28 ulong ret;
29
30 ret = spi_flash_read(flash, sector, count, buf);
31 if (!ret)
32 return count;
33 else
34 return 0;
35 }
36
spl_spi_get_uboot_offs(struct spi_flash * flash)37 unsigned int __weak spl_spi_get_uboot_offs(struct spi_flash *flash)
38 {
39 return CONFIG_SYS_SPI_U_BOOT_OFFS;
40 }
41
spl_spi_boot_bus(void)42 u32 __weak spl_spi_boot_bus(void)
43 {
44 return CONFIG_SF_DEFAULT_BUS;
45 }
46
spl_spi_boot_cs(void)47 u32 __weak spl_spi_boot_cs(void)
48 {
49 return CONFIG_SF_DEFAULT_CS;
50 }
51
52 /*
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 */
spl_spi_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)57 static int spl_spi_load_image(struct spl_image_info *spl_image,
58 struct spl_boot_device *bootdev)
59 {
60 int err = 0;
61 unsigned int payload_offs;
62 struct spi_flash *flash;
63 unsigned int sf_bus = spl_spi_boot_bus();
64 unsigned int sf_cs = spl_spi_boot_cs();
65 struct spl_load_info load;
66
67 /*
68 * Load U-Boot image from SPI flash into RAM
69 * In DM mode: defaults speed and mode will be
70 * taken from DT when available
71 */
72 flash = spi_flash_probe(sf_bus, sf_cs,
73 CONFIG_SF_DEFAULT_SPEED,
74 CONFIG_SF_DEFAULT_MODE);
75 if (!flash) {
76 puts("SPI probe failed.\n");
77 return -ENODEV;
78 }
79
80 spl_load_init(&load, spl_spi_fit_read, flash, 1);
81
82 #if CONFIG_IS_ENABLED(OS_BOOT)
83 if (spl_start_uboot()) {
84 int err = spl_load(spl_image, bootdev, &load, 0,
85 CONFIG_SYS_SPI_KERNEL_OFFS);
86
87 if (!err)
88 /* Read device tree. */
89 return spi_flash_read(
90 flash, CONFIG_SYS_SPI_ARGS_OFFS,
91 CONFIG_SYS_SPI_ARGS_SIZE,
92 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
93 }
94 #endif
95
96 payload_offs = spl_spi_get_uboot_offs(flash);
97 if (CONFIG_IS_ENABLED(OF_REAL)) {
98 payload_offs = ofnode_conf_read_int("u-boot,spl-payload-offset",
99 payload_offs);
100 }
101
102 err = spl_load(spl_image, bootdev, &load, 0, payload_offs);
103 if (IS_ENABLED(CONFIG_SPI_FLASH_SOFT_RESET))
104 err = spi_nor_remove(flash);
105 return err;
106 }
107 /* Use priorty 1 so that boards can override this */
108 SPL_LOAD_IMAGE_METHOD("SPI", 1, BOOT_DEVICE_SPI, spl_spi_load_image);
109