blob: 910e80501617e5d326f9a3123d2277de00f2db5b [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
6#include <common.h>
Simon Glass2dc9c342020-05-10 11:40:01 -06007#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +03009#include <spl.h>
Andre Przywara05ebd892021-07-06 00:04:43 +010010#include <asm/arch/spl.h>
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030011#include <asm/gpio.h>
12#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060013#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090015#include <linux/libfdt.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)
75#define SUN6I_TCR_XCH BIT(31)
76
77/*****************************************************************************/
78
79#define CCM_AHB_GATING0 (0x01C20000 + 0x60)
Andre Przywara0c882df2020-01-28 00:46:43 +000080#define CCM_H6_SPI_BGR_REG (0x03001000 + 0x96c)
81#ifdef CONFIG_MACH_SUN50I_H6
82#define CCM_SPI0_CLK (0x03001000 + 0x940)
83#else
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030084#define CCM_SPI0_CLK (0x01C20000 + 0xA0)
Andre Przywara0c882df2020-01-28 00:46:43 +000085#endif
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030086#define SUN6I_BUS_SOFT_RST_REG0 (0x01C20000 + 0x2C0)
87
88#define AHB_RESET_SPI0_SHIFT 20
89#define AHB_GATE_OFFSET_SPI0 20
90
91#define SPI0_CLK_DIV_BY_2 0x1000
92#define SPI0_CLK_DIV_BY_4 0x1001
93
94/*****************************************************************************/
95
96/*
97 * Allwinner A10/A20 SoCs were using pins PC0,PC1,PC2,PC23 for booting
98 * from SPI Flash, everything else is using pins PC0,PC1,PC2,PC3.
Andre Przywara0c882df2020-01-28 00:46:43 +000099 * The H6 uses PC0, PC2, PC3, PC5.
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300100 */
101static void spi0_pinmux_setup(unsigned int pin_function)
102{
Andre Przywara0c882df2020-01-28 00:46:43 +0000103 /* All chips use PC0 and PC2. */
104 sunxi_gpio_set_cfgpin(SUNXI_GPC(0), pin_function);
105 sunxi_gpio_set_cfgpin(SUNXI_GPC(2), pin_function);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300106
Andre Przywara0c882df2020-01-28 00:46:43 +0000107 /* All chips except H6 use PC1, and only H6 uses PC5. */
108 if (!IS_ENABLED(CONFIG_MACH_SUN50I_H6))
109 sunxi_gpio_set_cfgpin(SUNXI_GPC(1), pin_function);
110 else
111 sunxi_gpio_set_cfgpin(SUNXI_GPC(5), pin_function);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300112
Andre Przywara0c882df2020-01-28 00:46:43 +0000113 /* Older generations use PC23 for CS, newer ones use PC3. */
Andre Przywarada3bd452020-01-28 00:46:42 +0000114 if (IS_ENABLED(CONFIG_MACH_SUN4I) || IS_ENABLED(CONFIG_MACH_SUN7I) ||
115 IS_ENABLED(CONFIG_MACH_SUN8I_R40))
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300116 sunxi_gpio_set_cfgpin(SUNXI_GPC(23), pin_function);
117 else
118 sunxi_gpio_set_cfgpin(SUNXI_GPC(3), pin_function);
119}
120
Andre Przywara382dab22020-01-28 00:46:41 +0000121static bool is_sun6i_gen_spi(void)
122{
Andre Przywara0c882df2020-01-28 00:46:43 +0000123 return IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I) ||
124 IS_ENABLED(CONFIG_MACH_SUN50I_H6);
Andre Przywara382dab22020-01-28 00:46:41 +0000125}
126
Andre Przywara5c7624d2020-01-28 00:46:40 +0000127static uintptr_t spi0_base_address(void)
128{
Andre Przywarada3bd452020-01-28 00:46:42 +0000129 if (IS_ENABLED(CONFIG_MACH_SUN8I_R40))
130 return 0x01C05000;
131
Andre Przywara0c882df2020-01-28 00:46:43 +0000132 if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
133 return 0x05010000;
134
Andre Przywara382dab22020-01-28 00:46:41 +0000135 if (!is_sun6i_gen_spi())
Andre Przywara5c7624d2020-01-28 00:46:40 +0000136 return 0x01C05000;
137
138 return 0x01C68000;
139}
140
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300141/*
142 * Setup 6 MHz from OSC24M (because the BROM is doing the same).
143 */
144static void spi0_enable_clock(void)
145{
Andre Przywara5c7624d2020-01-28 00:46:40 +0000146 uintptr_t base = spi0_base_address();
147
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300148 /* Deassert SPI0 reset on SUN6I */
Andre Przywara0c882df2020-01-28 00:46:43 +0000149 if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
150 setbits_le32(CCM_H6_SPI_BGR_REG, (1U << 16) | 0x1);
151 else if (is_sun6i_gen_spi())
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300152 setbits_le32(SUN6I_BUS_SOFT_RST_REG0,
153 (1 << AHB_RESET_SPI0_SHIFT));
154
155 /* Open the SPI0 gate */
Andre Przywara0c882df2020-01-28 00:46:43 +0000156 if (!IS_ENABLED(CONFIG_MACH_SUN50I_H6))
157 setbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0));
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300158
159 /* Divide by 4 */
Andre Przywara382dab22020-01-28 00:46:41 +0000160 writel(SPI0_CLK_DIV_BY_4, base + (is_sun6i_gen_spi() ?
Andre Przywara5c7624d2020-01-28 00:46:40 +0000161 SUN6I_SPI0_CCTL : SUN4I_SPI0_CCTL));
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300162 /* 24MHz from OSC24M */
163 writel((1 << 31), CCM_SPI0_CLK);
164
Andre Przywara382dab22020-01-28 00:46:41 +0000165 if (is_sun6i_gen_spi()) {
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300166 /* Enable SPI in the master mode and do a soft reset */
Andre Przywara5c7624d2020-01-28 00:46:40 +0000167 setbits_le32(base + SUN6I_SPI0_GCR, SUN6I_CTL_MASTER |
168 SUN6I_CTL_ENABLE | SUN6I_CTL_SRST);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300169 /* Wait for completion */
Andre Przywara5c7624d2020-01-28 00:46:40 +0000170 while (readl(base + SUN6I_SPI0_GCR) & SUN6I_CTL_SRST)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300171 ;
172 } else {
173 /* Enable SPI in the master mode and reset FIFO */
Andre Przywara5c7624d2020-01-28 00:46:40 +0000174 setbits_le32(base + SUN4I_SPI0_CTL, SUN4I_CTL_MASTER |
175 SUN4I_CTL_ENABLE |
176 SUN4I_CTL_TF_RST |
177 SUN4I_CTL_RF_RST);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300178 }
179}
180
181static void spi0_disable_clock(void)
182{
Andre Przywara5c7624d2020-01-28 00:46:40 +0000183 uintptr_t base = spi0_base_address();
184
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300185 /* Disable the SPI0 controller */
Andre Przywara382dab22020-01-28 00:46:41 +0000186 if (is_sun6i_gen_spi())
Andre Przywara5c7624d2020-01-28 00:46:40 +0000187 clrbits_le32(base + SUN6I_SPI0_GCR, SUN6I_CTL_MASTER |
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300188 SUN6I_CTL_ENABLE);
189 else
Andre Przywara5c7624d2020-01-28 00:46:40 +0000190 clrbits_le32(base + SUN4I_SPI0_CTL, SUN4I_CTL_MASTER |
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300191 SUN4I_CTL_ENABLE);
192
193 /* Disable the SPI0 clock */
194 writel(0, CCM_SPI0_CLK);
195
196 /* Close the SPI0 gate */
Andre Przywara0c882df2020-01-28 00:46:43 +0000197 if (!IS_ENABLED(CONFIG_MACH_SUN50I_H6))
198 clrbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0));
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300199
200 /* Assert SPI0 reset on SUN6I */
Andre Przywara0c882df2020-01-28 00:46:43 +0000201 if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
202 clrbits_le32(CCM_H6_SPI_BGR_REG, (1U << 16) | 0x1);
203 else if (is_sun6i_gen_spi())
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300204 clrbits_le32(SUN6I_BUS_SOFT_RST_REG0,
205 (1 << AHB_RESET_SPI0_SHIFT));
206}
207
Andre Przywara90895f62016-11-20 14:56:55 +0000208static void spi0_init(void)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300209{
210 unsigned int pin_function = SUNXI_GPC_SPI0;
Andre Przywara90895f62016-11-20 14:56:55 +0000211
Andre Przywara0c882df2020-01-28 00:46:43 +0000212 if (IS_ENABLED(CONFIG_MACH_SUN50I) ||
213 IS_ENABLED(CONFIG_MACH_SUN50I_H6))
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300214 pin_function = SUN50I_GPC_SPI0;
215
216 spi0_pinmux_setup(pin_function);
217 spi0_enable_clock();
218}
219
220static void spi0_deinit(void)
221{
222 /* New SoCs can disable pins, older could only set them as input */
223 unsigned int pin_function = SUNXI_GPIO_INPUT;
Andre Przywara382dab22020-01-28 00:46:41 +0000224
225 if (is_sun6i_gen_spi())
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300226 pin_function = SUNXI_GPIO_DISABLE;
227
228 spi0_disable_clock();
229 spi0_pinmux_setup(pin_function);
230}
231
232/*****************************************************************************/
233
234#define SPI_READ_MAX_SIZE 60 /* FIFO size, minus 4 bytes of the header */
235
236static void sunxi_spi0_read_data(u8 *buf, u32 addr, u32 bufsize,
Andre Przywarac10848d2017-02-16 01:20:25 +0000237 ulong spi_ctl_reg,
238 ulong spi_ctl_xch_bitmask,
239 ulong spi_fifo_reg,
240 ulong spi_tx_reg,
241 ulong spi_rx_reg,
242 ulong spi_bc_reg,
243 ulong spi_tc_reg,
244 ulong spi_bcc_reg)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300245{
246 writel(4 + bufsize, spi_bc_reg); /* Burst counter (total bytes) */
247 writel(4, spi_tc_reg); /* Transfer counter (bytes to send) */
248 if (spi_bcc_reg)
249 writel(4, spi_bcc_reg); /* SUN6I also needs this */
250
251 /* Send the Read Data Bytes (03h) command header */
252 writeb(0x03, spi_tx_reg);
253 writeb((u8)(addr >> 16), spi_tx_reg);
254 writeb((u8)(addr >> 8), spi_tx_reg);
255 writeb((u8)(addr), spi_tx_reg);
256
257 /* Start the data transfer */
258 setbits_le32(spi_ctl_reg, spi_ctl_xch_bitmask);
259
260 /* Wait until everything is received in the RX FIFO */
261 while ((readl(spi_fifo_reg) & 0x7F) < 4 + bufsize)
262 ;
263
264 /* Skip 4 bytes */
265 readl(spi_rx_reg);
266
267 /* Read the data */
268 while (bufsize-- > 0)
269 *buf++ = readb(spi_rx_reg);
270
271 /* tSHSL time is up to 100 ns in various SPI flash datasheets */
272 udelay(1);
273}
274
275static void spi0_read_data(void *buf, u32 addr, u32 len)
276{
277 u8 *buf8 = buf;
278 u32 chunk_len;
Andre Przywara5c7624d2020-01-28 00:46:40 +0000279 uintptr_t base = spi0_base_address();
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300280
281 while (len > 0) {
282 chunk_len = len;
283 if (chunk_len > SPI_READ_MAX_SIZE)
284 chunk_len = SPI_READ_MAX_SIZE;
285
Andre Przywara382dab22020-01-28 00:46:41 +0000286 if (is_sun6i_gen_spi()) {
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300287 sunxi_spi0_read_data(buf8, addr, chunk_len,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000288 base + SUN6I_SPI0_TCR,
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300289 SUN6I_TCR_XCH,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000290 base + SUN6I_SPI0_FIFO_STA,
291 base + SUN6I_SPI0_TXD,
292 base + SUN6I_SPI0_RXD,
293 base + SUN6I_SPI0_MBC,
294 base + SUN6I_SPI0_MTC,
295 base + SUN6I_SPI0_BCC);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300296 } else {
297 sunxi_spi0_read_data(buf8, addr, chunk_len,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000298 base + SUN4I_SPI0_CTL,
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300299 SUN4I_CTL_XCH,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000300 base + SUN4I_SPI0_FIFO_STA,
301 base + SUN4I_SPI0_TX,
302 base + SUN4I_SPI0_RX,
303 base + SUN4I_SPI0_BC,
304 base + SUN4I_SPI0_TC,
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300305 0);
306 }
307
308 len -= chunk_len;
309 buf8 += chunk_len;
310 addr += chunk_len;
311 }
312}
313
Andre Przywara230fed72017-09-22 22:57:22 +0100314static ulong spi_load_read(struct spl_load_info *load, ulong sector,
315 ulong count, void *buf)
316{
317 spi0_read_data(buf, sector, count);
318
319 return count;
320}
321
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300322/*****************************************************************************/
323
Simon Glass0649e912016-09-24 18:20:14 -0600324static int spl_spi_load_image(struct spl_image_info *spl_image,
325 struct spl_boot_device *bootdev)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300326{
Andre Przywara230fed72017-09-22 22:57:22 +0100327 int ret = 0;
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300328 struct image_header *header;
329 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
Andre Przywara05ebd892021-07-06 00:04:43 +0100330 int load_offset = readl(SPL_ADDR + 0x10);
331
332 load_offset = max(load_offset, CONFIG_SYS_SPI_U_BOOT_OFFS);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300333
334 spi0_init();
335
Andre Przywara05ebd892021-07-06 00:04:43 +0100336 spi0_read_data((void *)header, load_offset, 0x40);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300337
Andre Przywara230fed72017-09-22 22:57:22 +0100338 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
339 image_get_magic(header) == FDT_MAGIC) {
340 struct spl_load_info load;
341
342 debug("Found FIT image\n");
343 load.dev = NULL;
344 load.priv = NULL;
345 load.filename = NULL;
346 load.bl_len = 1;
347 load.read = spi_load_read;
348 ret = spl_load_simple_fit(spl_image, &load,
Andre Przywara05ebd892021-07-06 00:04:43 +0100349 load_offset, header);
Andre Przywara230fed72017-09-22 22:57:22 +0100350 } else {
Pali Rohárdda8f882022-01-14 14:31:38 +0100351 ret = spl_parse_image_header(spl_image, bootdev, header);
Andre Przywara230fed72017-09-22 22:57:22 +0100352 if (ret)
353 return ret;
354
355 spi0_read_data((void *)spl_image->load_addr,
Andre Przywara05ebd892021-07-06 00:04:43 +0100356 load_offset, spl_image->size);
Andre Przywara230fed72017-09-22 22:57:22 +0100357 }
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300358
359 spi0_deinit();
Andre Przywara230fed72017-09-22 22:57:22 +0100360
361 return ret;
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300362}
Simon Glassb9f6d892016-09-24 18:20:09 -0600363/* Use priorty 0 to override the default if it happens to be linked in */
Priit Laes19d39fc2017-01-02 20:24:50 +0200364SPL_LOAD_IMAGE_METHOD("sunxi SPI", 0, BOOT_DEVICE_SPI, spl_spi_load_image);