blob: 5f72e809952b91443aaff75549ab49bda7300781 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +03002/*
3 * Copyright (C) 2016 Siarhei Siamashka <siarhei.siamashka@gmail.com>
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +03004 */
5
Simon Glass2dc9c342020-05-10 11:40:01 -06006#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +03008#include <spl.h>
Andre Przywara05ebd892021-07-06 00:04:43 +01009#include <asm/arch/spl.h>
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030010#include <asm/gpio.h>
11#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060012#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090014#include <linux/libfdt.h>
Andre Przywaraf944a612022-09-06 10:36:38 +010015#include <sunxi_gpio.h>
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030016
17#ifdef CONFIG_SPL_OS_BOOT
18#error CONFIG_SPL_OS_BOOT is not supported yet
19#endif
20
21/*
22 * This is a very simple U-Boot image loading implementation, trying to
23 * replicate what the boot ROM is doing when loading the SPL. Because we
24 * know the exact pins where the SPI Flash is connected and also know
25 * that the Read Data Bytes (03h) command is supported, the hardware
26 * configuration is very simple and we don't need the extra flexibility
27 * of the SPI framework. Moreover, we rely on the default settings of
28 * the SPI controler hardware registers and only adjust what needs to
29 * be changed. This is good for the code size and this implementation
30 * adds less than 400 bytes to the SPL.
31 *
32 * There are two variants of the SPI controller in Allwinner SoCs:
33 * A10/A13/A20 (sun4i variant) and everything else (sun6i variant).
34 * Both of them are supported.
35 *
36 * The pin mixing part is SoC specific and only A10/A13/A20/H3/A64 are
37 * supported at the moment.
38 */
39
40/*****************************************************************************/
41/* SUN4I variant of the SPI controller */
42/*****************************************************************************/
43
Andre Przywara5c7624d2020-01-28 00:46:40 +000044#define SUN4I_SPI0_CCTL 0x1C
45#define SUN4I_SPI0_CTL 0x08
46#define SUN4I_SPI0_RX 0x00
47#define SUN4I_SPI0_TX 0x04
48#define SUN4I_SPI0_FIFO_STA 0x28
49#define SUN4I_SPI0_BC 0x20
50#define SUN4I_SPI0_TC 0x24
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030051
52#define SUN4I_CTL_ENABLE BIT(0)
53#define SUN4I_CTL_MASTER BIT(1)
54#define SUN4I_CTL_TF_RST BIT(8)
55#define SUN4I_CTL_RF_RST BIT(9)
56#define SUN4I_CTL_XCH BIT(10)
57
58/*****************************************************************************/
59/* SUN6I variant of the SPI controller */
60/*****************************************************************************/
61
Andre Przywara5c7624d2020-01-28 00:46:40 +000062#define SUN6I_SPI0_CCTL 0x24
63#define SUN6I_SPI0_GCR 0x04
64#define SUN6I_SPI0_TCR 0x08
65#define SUN6I_SPI0_FIFO_STA 0x1C
66#define SUN6I_SPI0_MBC 0x30
67#define SUN6I_SPI0_MTC 0x34
68#define SUN6I_SPI0_BCC 0x38
69#define SUN6I_SPI0_TXD 0x200
70#define SUN6I_SPI0_RXD 0x300
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030071
72#define SUN6I_CTL_ENABLE BIT(0)
73#define SUN6I_CTL_MASTER BIT(1)
74#define SUN6I_CTL_SRST BIT(31)
Maksim Kiselev69bf17a2023-11-11 16:33:07 +030075#define SUN6I_TCR_SDM BIT(13)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030076#define SUN6I_TCR_XCH BIT(31)
77
78/*****************************************************************************/
79
Maksim Kiselev69bf17a2023-11-11 16:33:07 +030080#if IS_ENABLED(CONFIG_SUN50I_GEN_H6)
81#define CCM_BASE 0x03001000
82#elif IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2)
83#define CCM_BASE 0x02001000
Andre Przywara0c882df2020-01-28 00:46:43 +000084#else
Maksim Kiselev69bf17a2023-11-11 16:33:07 +030085#define CCM_BASE 0x01C20000
Andre Przywara0c882df2020-01-28 00:46:43 +000086#endif
Maksim Kiselev69bf17a2023-11-11 16:33:07 +030087
88#define CCM_AHB_GATING0 (CCM_BASE + 0x60)
89#define CCM_H6_SPI_BGR_REG (CCM_BASE + 0x96c)
90#if IS_ENABLED(CONFIG_SUN50I_GEN_H6) || IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2)
91#define CCM_SPI0_CLK (CCM_BASE + 0x940)
92#else
93#define CCM_SPI0_CLK (CCM_BASE + 0xA0)
94#endif
95#define SUN6I_BUS_SOFT_RST_REG0 (CCM_BASE + 0x2C0)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030096
97#define AHB_RESET_SPI0_SHIFT 20
98#define AHB_GATE_OFFSET_SPI0 20
99
100#define SPI0_CLK_DIV_BY_2 0x1000
101#define SPI0_CLK_DIV_BY_4 0x1001
Jesse Taubeea3cbc62022-02-11 19:32:34 -0500102#define SPI0_CLK_DIV_BY_32 0x100f
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300103
104/*****************************************************************************/
105
106/*
107 * Allwinner A10/A20 SoCs were using pins PC0,PC1,PC2,PC23 for booting
108 * from SPI Flash, everything else is using pins PC0,PC1,PC2,PC3.
Andre Przywarab2b4ff22020-12-13 20:19:43 +0000109 * The H6 uses PC0, PC2, PC3, PC5, the H616 PC0, PC2, PC3, PC4.
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300110 */
111static void spi0_pinmux_setup(unsigned int pin_function)
112{
Maksim Kiselev69bf17a2023-11-11 16:33:07 +0300113 /* All chips use PC2. And all chips use PC0, except R528/T113 */
114 if (!IS_ENABLED(CONFIG_MACH_SUN8I_R528))
115 sunxi_gpio_set_cfgpin(SUNXI_GPC(0), pin_function);
116
Andre Przywara0c882df2020-01-28 00:46:43 +0000117 sunxi_gpio_set_cfgpin(SUNXI_GPC(2), pin_function);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300118
Maksim Kiselev69bf17a2023-11-11 16:33:07 +0300119 /* All chips except H6/H616/R528/T113 use PC1. */
120 if (!IS_ENABLED(CONFIG_SUN50I_GEN_H6) &&
121 !IS_ENABLED(CONFIG_MACH_SUN8I_R528))
Andre Przywara0c882df2020-01-28 00:46:43 +0000122 sunxi_gpio_set_cfgpin(SUNXI_GPC(1), pin_function);
Andre Przywarab2b4ff22020-12-13 20:19:43 +0000123
Maksim Kiselev69bf17a2023-11-11 16:33:07 +0300124 if (IS_ENABLED(CONFIG_MACH_SUN50I_H6) ||
125 IS_ENABLED(CONFIG_MACH_SUN8I_R528))
Andre Przywara0c882df2020-01-28 00:46:43 +0000126 sunxi_gpio_set_cfgpin(SUNXI_GPC(5), pin_function);
Maksim Kiselev69bf17a2023-11-11 16:33:07 +0300127 if (IS_ENABLED(CONFIG_MACH_SUN50I_H616) ||
128 IS_ENABLED(CONFIG_MACH_SUN8I_R528))
Andre Przywarab2b4ff22020-12-13 20:19:43 +0000129 sunxi_gpio_set_cfgpin(SUNXI_GPC(4), pin_function);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300130
Andre Przywara0c882df2020-01-28 00:46:43 +0000131 /* Older generations use PC23 for CS, newer ones use PC3. */
Andre Przywarada3bd452020-01-28 00:46:42 +0000132 if (IS_ENABLED(CONFIG_MACH_SUN4I) || IS_ENABLED(CONFIG_MACH_SUN7I) ||
133 IS_ENABLED(CONFIG_MACH_SUN8I_R40))
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300134 sunxi_gpio_set_cfgpin(SUNXI_GPC(23), pin_function);
135 else
136 sunxi_gpio_set_cfgpin(SUNXI_GPC(3), pin_function);
137}
138
Andre Przywara382dab22020-01-28 00:46:41 +0000139static bool is_sun6i_gen_spi(void)
140{
Andre Przywara0c882df2020-01-28 00:46:43 +0000141 return IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I) ||
Maksim Kiselev69bf17a2023-11-11 16:33:07 +0300142 IS_ENABLED(CONFIG_SUN50I_GEN_H6) ||
Michael Walle3d7f1d82024-05-14 01:43:19 +0200143 IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2) ||
144 IS_ENABLED(CONFIG_MACH_SUN8I_V3S);
Andre Przywara382dab22020-01-28 00:46:41 +0000145}
146
Andre Przywara5c7624d2020-01-28 00:46:40 +0000147static uintptr_t spi0_base_address(void)
148{
Andre Przywarada3bd452020-01-28 00:46:42 +0000149 if (IS_ENABLED(CONFIG_MACH_SUN8I_R40))
150 return 0x01C05000;
151
Andre Przywarab2b4ff22020-12-13 20:19:43 +0000152 if (IS_ENABLED(CONFIG_SUN50I_GEN_H6))
Andre Przywara0c882df2020-01-28 00:46:43 +0000153 return 0x05010000;
154
Maksim Kiselev69bf17a2023-11-11 16:33:07 +0300155 if (IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2))
156 return 0x04025000;
157
Jesse Taubeea3cbc62022-02-11 19:32:34 -0500158 if (!is_sun6i_gen_spi() ||
159 IS_ENABLED(CONFIG_MACH_SUNIV))
Andre Przywara5c7624d2020-01-28 00:46:40 +0000160 return 0x01C05000;
161
162 return 0x01C68000;
163}
164
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300165/*
166 * Setup 6 MHz from OSC24M (because the BROM is doing the same).
167 */
168static void spi0_enable_clock(void)
169{
Andre Przywara5c7624d2020-01-28 00:46:40 +0000170 uintptr_t base = spi0_base_address();
171
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300172 /* Deassert SPI0 reset on SUN6I */
Maksim Kiselev69bf17a2023-11-11 16:33:07 +0300173 if (IS_ENABLED(CONFIG_SUN50I_GEN_H6) ||
174 IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2))
Andre Przywara0c882df2020-01-28 00:46:43 +0000175 setbits_le32(CCM_H6_SPI_BGR_REG, (1U << 16) | 0x1);
176 else if (is_sun6i_gen_spi())
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300177 setbits_le32(SUN6I_BUS_SOFT_RST_REG0,
178 (1 << AHB_RESET_SPI0_SHIFT));
179
180 /* Open the SPI0 gate */
Maksim Kiselev69bf17a2023-11-11 16:33:07 +0300181 if (!IS_ENABLED(CONFIG_SUN50I_GEN_H6) &&
182 !IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2))
Andre Przywara0c882df2020-01-28 00:46:43 +0000183 setbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0));
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300184
Jesse Taubeea3cbc62022-02-11 19:32:34 -0500185 if (IS_ENABLED(CONFIG_MACH_SUNIV)) {
186 /* Divide by 32, clock source is AHB clock 200MHz */
187 writel(SPI0_CLK_DIV_BY_32, base + SUN6I_SPI0_CCTL);
188 } else {
Maksim Kiselev69bf17a2023-11-11 16:33:07 +0300189 /* New SoCs do not have a clock divider inside */
190 if (!IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2)) {
191 /* Divide by 4 */
192 writel(SPI0_CLK_DIV_BY_4,
193 base + (is_sun6i_gen_spi() ? SUN6I_SPI0_CCTL :
194 SUN4I_SPI0_CCTL));
195 }
196
Jesse Taubeea3cbc62022-02-11 19:32:34 -0500197 /* 24MHz from OSC24M */
198 writel((1 << 31), CCM_SPI0_CLK);
199 }
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300200
Andre Przywara382dab22020-01-28 00:46:41 +0000201 if (is_sun6i_gen_spi()) {
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300202 /* Enable SPI in the master mode and do a soft reset */
Andre Przywara5c7624d2020-01-28 00:46:40 +0000203 setbits_le32(base + SUN6I_SPI0_GCR, SUN6I_CTL_MASTER |
204 SUN6I_CTL_ENABLE | SUN6I_CTL_SRST);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300205 /* Wait for completion */
Andre Przywara5c7624d2020-01-28 00:46:40 +0000206 while (readl(base + SUN6I_SPI0_GCR) & SUN6I_CTL_SRST)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300207 ;
Maksim Kiselev69bf17a2023-11-11 16:33:07 +0300208
209 /*
210 * For new SoCs we should configure sample mode depending on
211 * input clock. As 24MHz from OSC24M is used, we could use
212 * normal sample mode by setting SDM bit in the TCR register
213 */
214 if (IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2))
215 setbits_le32(base + SUN6I_SPI0_TCR, SUN6I_TCR_SDM);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300216 } else {
217 /* Enable SPI in the master mode and reset FIFO */
Andre Przywara5c7624d2020-01-28 00:46:40 +0000218 setbits_le32(base + SUN4I_SPI0_CTL, SUN4I_CTL_MASTER |
219 SUN4I_CTL_ENABLE |
220 SUN4I_CTL_TF_RST |
221 SUN4I_CTL_RF_RST);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300222 }
223}
224
225static void spi0_disable_clock(void)
226{
Andre Przywara5c7624d2020-01-28 00:46:40 +0000227 uintptr_t base = spi0_base_address();
228
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300229 /* Disable the SPI0 controller */
Andre Przywara382dab22020-01-28 00:46:41 +0000230 if (is_sun6i_gen_spi())
Andre Przywara5c7624d2020-01-28 00:46:40 +0000231 clrbits_le32(base + SUN6I_SPI0_GCR, SUN6I_CTL_MASTER |
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300232 SUN6I_CTL_ENABLE);
233 else
Andre Przywara5c7624d2020-01-28 00:46:40 +0000234 clrbits_le32(base + SUN4I_SPI0_CTL, SUN4I_CTL_MASTER |
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300235 SUN4I_CTL_ENABLE);
236
237 /* Disable the SPI0 clock */
Jesse Taubeea3cbc62022-02-11 19:32:34 -0500238 if (!IS_ENABLED(CONFIG_MACH_SUNIV))
239 writel(0, CCM_SPI0_CLK);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300240
241 /* Close the SPI0 gate */
Maksim Kiselev69bf17a2023-11-11 16:33:07 +0300242 if (!IS_ENABLED(CONFIG_SUN50I_GEN_H6) &&
243 !IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2))
Andre Przywara0c882df2020-01-28 00:46:43 +0000244 clrbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0));
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300245
246 /* Assert SPI0 reset on SUN6I */
Maksim Kiselev69bf17a2023-11-11 16:33:07 +0300247 if (IS_ENABLED(CONFIG_SUN50I_GEN_H6) ||
248 IS_ENABLED(CONFIG_SUNXI_GEN_NCAT2))
Andre Przywara0c882df2020-01-28 00:46:43 +0000249 clrbits_le32(CCM_H6_SPI_BGR_REG, (1U << 16) | 0x1);
250 else if (is_sun6i_gen_spi())
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300251 clrbits_le32(SUN6I_BUS_SOFT_RST_REG0,
252 (1 << AHB_RESET_SPI0_SHIFT));
253}
254
Andre Przywara90895f62016-11-20 14:56:55 +0000255static void spi0_init(void)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300256{
257 unsigned int pin_function = SUNXI_GPC_SPI0;
Andre Przywara90895f62016-11-20 14:56:55 +0000258
Andre Przywara0c882df2020-01-28 00:46:43 +0000259 if (IS_ENABLED(CONFIG_MACH_SUN50I) ||
Andre Przywarab2b4ff22020-12-13 20:19:43 +0000260 IS_ENABLED(CONFIG_SUN50I_GEN_H6))
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300261 pin_function = SUN50I_GPC_SPI0;
Maksim Kiselev69bf17a2023-11-11 16:33:07 +0300262 else if (IS_ENABLED(CONFIG_MACH_SUNIV) ||
263 IS_ENABLED(CONFIG_MACH_SUN8I_R528))
Jesse Taubeea3cbc62022-02-11 19:32:34 -0500264 pin_function = SUNIV_GPC_SPI0;
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300265
266 spi0_pinmux_setup(pin_function);
267 spi0_enable_clock();
268}
269
270static void spi0_deinit(void)
271{
272 /* New SoCs can disable pins, older could only set them as input */
273 unsigned int pin_function = SUNXI_GPIO_INPUT;
Andre Przywara382dab22020-01-28 00:46:41 +0000274
275 if (is_sun6i_gen_spi())
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300276 pin_function = SUNXI_GPIO_DISABLE;
277
278 spi0_disable_clock();
279 spi0_pinmux_setup(pin_function);
280}
281
282/*****************************************************************************/
283
284#define SPI_READ_MAX_SIZE 60 /* FIFO size, minus 4 bytes of the header */
285
286static void sunxi_spi0_read_data(u8 *buf, u32 addr, u32 bufsize,
Andre Przywarac10848d2017-02-16 01:20:25 +0000287 ulong spi_ctl_reg,
288 ulong spi_ctl_xch_bitmask,
289 ulong spi_fifo_reg,
290 ulong spi_tx_reg,
291 ulong spi_rx_reg,
292 ulong spi_bc_reg,
293 ulong spi_tc_reg,
294 ulong spi_bcc_reg)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300295{
296 writel(4 + bufsize, spi_bc_reg); /* Burst counter (total bytes) */
297 writel(4, spi_tc_reg); /* Transfer counter (bytes to send) */
298 if (spi_bcc_reg)
299 writel(4, spi_bcc_reg); /* SUN6I also needs this */
300
301 /* Send the Read Data Bytes (03h) command header */
302 writeb(0x03, spi_tx_reg);
303 writeb((u8)(addr >> 16), spi_tx_reg);
304 writeb((u8)(addr >> 8), spi_tx_reg);
305 writeb((u8)(addr), spi_tx_reg);
306
307 /* Start the data transfer */
308 setbits_le32(spi_ctl_reg, spi_ctl_xch_bitmask);
309
310 /* Wait until everything is received in the RX FIFO */
311 while ((readl(spi_fifo_reg) & 0x7F) < 4 + bufsize)
312 ;
313
314 /* Skip 4 bytes */
315 readl(spi_rx_reg);
316
317 /* Read the data */
318 while (bufsize-- > 0)
319 *buf++ = readb(spi_rx_reg);
320
321 /* tSHSL time is up to 100 ns in various SPI flash datasheets */
322 udelay(1);
323}
324
325static void spi0_read_data(void *buf, u32 addr, u32 len)
326{
327 u8 *buf8 = buf;
328 u32 chunk_len;
Andre Przywara5c7624d2020-01-28 00:46:40 +0000329 uintptr_t base = spi0_base_address();
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300330
331 while (len > 0) {
332 chunk_len = len;
333 if (chunk_len > SPI_READ_MAX_SIZE)
334 chunk_len = SPI_READ_MAX_SIZE;
335
Andre Przywara382dab22020-01-28 00:46:41 +0000336 if (is_sun6i_gen_spi()) {
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300337 sunxi_spi0_read_data(buf8, addr, chunk_len,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000338 base + SUN6I_SPI0_TCR,
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300339 SUN6I_TCR_XCH,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000340 base + SUN6I_SPI0_FIFO_STA,
341 base + SUN6I_SPI0_TXD,
342 base + SUN6I_SPI0_RXD,
343 base + SUN6I_SPI0_MBC,
344 base + SUN6I_SPI0_MTC,
345 base + SUN6I_SPI0_BCC);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300346 } else {
347 sunxi_spi0_read_data(buf8, addr, chunk_len,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000348 base + SUN4I_SPI0_CTL,
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300349 SUN4I_CTL_XCH,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000350 base + SUN4I_SPI0_FIFO_STA,
351 base + SUN4I_SPI0_TX,
352 base + SUN4I_SPI0_RX,
353 base + SUN4I_SPI0_BC,
354 base + SUN4I_SPI0_TC,
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300355 0);
356 }
357
358 len -= chunk_len;
359 buf8 += chunk_len;
360 addr += chunk_len;
361 }
362}
363
Andre Przywara230fed72017-09-22 22:57:22 +0100364static ulong spi_load_read(struct spl_load_info *load, ulong sector,
365 ulong count, void *buf)
366{
367 spi0_read_data(buf, sector, count);
368
369 return count;
370}
371
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300372/*****************************************************************************/
373
Simon Glass0649e912016-09-24 18:20:14 -0600374static int spl_spi_load_image(struct spl_image_info *spl_image,
375 struct spl_boot_device *bootdev)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300376{
Andre Przywara230fed72017-09-22 22:57:22 +0100377 int ret = 0;
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600378 struct legacy_img_hdr *header;
Samuel Holland784fcf62022-03-18 00:00:44 -0500379 uint32_t load_offset = sunxi_get_spl_size();
Andre Przywara05ebd892021-07-06 00:04:43 +0100380
Simon Glass72cc5382022-10-20 18:22:39 -0600381 header = (struct legacy_img_hdr *)CONFIG_TEXT_BASE;
Samuel Holland784fcf62022-03-18 00:00:44 -0500382 load_offset = max_t(uint32_t, load_offset, CONFIG_SYS_SPI_U_BOOT_OFFS);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300383
384 spi0_init();
385
Andre Przywara05ebd892021-07-06 00:04:43 +0100386 spi0_read_data((void *)header, load_offset, 0x40);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300387
Andre Przywara230fed72017-09-22 22:57:22 +0100388 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
389 image_get_magic(header) == FDT_MAGIC) {
390 struct spl_load_info load;
391
392 debug("Found FIT image\n");
Simon Glasse8066862024-08-22 07:55:02 -0600393 spl_load_init(&load, spi_load_read, NULL, 1);
Andre Przywara230fed72017-09-22 22:57:22 +0100394 ret = spl_load_simple_fit(spl_image, &load,
Andre Przywara05ebd892021-07-06 00:04:43 +0100395 load_offset, header);
Andre Przywara230fed72017-09-22 22:57:22 +0100396 } else {
Pali Rohárdda8f882022-01-14 14:31:38 +0100397 ret = spl_parse_image_header(spl_image, bootdev, header);
Andre Przywara230fed72017-09-22 22:57:22 +0100398 if (ret)
399 return ret;
400
401 spi0_read_data((void *)spl_image->load_addr,
Andre Przywara05ebd892021-07-06 00:04:43 +0100402 load_offset, spl_image->size);
Andre Przywara230fed72017-09-22 22:57:22 +0100403 }
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300404
405 spi0_deinit();
Andre Przywara230fed72017-09-22 22:57:22 +0100406
407 return ret;
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300408}
Simon Glassb9f6d892016-09-24 18:20:09 -0600409/* Use priorty 0 to override the default if it happens to be linked in */
Priit Laes19d39fc2017-01-02 20:24:50 +0200410SPL_LOAD_IMAGE_METHOD("sunxi SPI", 0, BOOT_DEVICE_SPI, spl_spi_load_image);