blob: a3997b25901d906813c1f58bea7b46a8a428cdd6 [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>
7#include <spl.h>
8#include <asm/gpio.h>
9#include <asm/io.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030011
12#ifdef CONFIG_SPL_OS_BOOT
13#error CONFIG_SPL_OS_BOOT is not supported yet
14#endif
15
16/*
17 * This is a very simple U-Boot image loading implementation, trying to
18 * replicate what the boot ROM is doing when loading the SPL. Because we
19 * know the exact pins where the SPI Flash is connected and also know
20 * that the Read Data Bytes (03h) command is supported, the hardware
21 * configuration is very simple and we don't need the extra flexibility
22 * of the SPI framework. Moreover, we rely on the default settings of
23 * the SPI controler hardware registers and only adjust what needs to
24 * be changed. This is good for the code size and this implementation
25 * adds less than 400 bytes to the SPL.
26 *
27 * There are two variants of the SPI controller in Allwinner SoCs:
28 * A10/A13/A20 (sun4i variant) and everything else (sun6i variant).
29 * Both of them are supported.
30 *
31 * The pin mixing part is SoC specific and only A10/A13/A20/H3/A64 are
32 * supported at the moment.
33 */
34
35/*****************************************************************************/
36/* SUN4I variant of the SPI controller */
37/*****************************************************************************/
38
Andre Przywara5c7624d2020-01-28 00:46:40 +000039#define SUN4I_SPI0_CCTL 0x1C
40#define SUN4I_SPI0_CTL 0x08
41#define SUN4I_SPI0_RX 0x00
42#define SUN4I_SPI0_TX 0x04
43#define SUN4I_SPI0_FIFO_STA 0x28
44#define SUN4I_SPI0_BC 0x20
45#define SUN4I_SPI0_TC 0x24
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030046
47#define SUN4I_CTL_ENABLE BIT(0)
48#define SUN4I_CTL_MASTER BIT(1)
49#define SUN4I_CTL_TF_RST BIT(8)
50#define SUN4I_CTL_RF_RST BIT(9)
51#define SUN4I_CTL_XCH BIT(10)
52
53/*****************************************************************************/
54/* SUN6I variant of the SPI controller */
55/*****************************************************************************/
56
Andre Przywara5c7624d2020-01-28 00:46:40 +000057#define SUN6I_SPI0_CCTL 0x24
58#define SUN6I_SPI0_GCR 0x04
59#define SUN6I_SPI0_TCR 0x08
60#define SUN6I_SPI0_FIFO_STA 0x1C
61#define SUN6I_SPI0_MBC 0x30
62#define SUN6I_SPI0_MTC 0x34
63#define SUN6I_SPI0_BCC 0x38
64#define SUN6I_SPI0_TXD 0x200
65#define SUN6I_SPI0_RXD 0x300
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030066
67#define SUN6I_CTL_ENABLE BIT(0)
68#define SUN6I_CTL_MASTER BIT(1)
69#define SUN6I_CTL_SRST BIT(31)
70#define SUN6I_TCR_XCH BIT(31)
71
72/*****************************************************************************/
73
74#define CCM_AHB_GATING0 (0x01C20000 + 0x60)
Andre Przywara0c882df2020-01-28 00:46:43 +000075#define CCM_H6_SPI_BGR_REG (0x03001000 + 0x96c)
76#ifdef CONFIG_MACH_SUN50I_H6
77#define CCM_SPI0_CLK (0x03001000 + 0x940)
78#else
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030079#define CCM_SPI0_CLK (0x01C20000 + 0xA0)
Andre Przywara0c882df2020-01-28 00:46:43 +000080#endif
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030081#define SUN6I_BUS_SOFT_RST_REG0 (0x01C20000 + 0x2C0)
82
83#define AHB_RESET_SPI0_SHIFT 20
84#define AHB_GATE_OFFSET_SPI0 20
85
86#define SPI0_CLK_DIV_BY_2 0x1000
87#define SPI0_CLK_DIV_BY_4 0x1001
88
89/*****************************************************************************/
90
91/*
92 * Allwinner A10/A20 SoCs were using pins PC0,PC1,PC2,PC23 for booting
93 * from SPI Flash, everything else is using pins PC0,PC1,PC2,PC3.
Andre Przywara0c882df2020-01-28 00:46:43 +000094 * The H6 uses PC0, PC2, PC3, PC5.
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030095 */
96static void spi0_pinmux_setup(unsigned int pin_function)
97{
Andre Przywara0c882df2020-01-28 00:46:43 +000098 /* All chips use PC0 and PC2. */
99 sunxi_gpio_set_cfgpin(SUNXI_GPC(0), pin_function);
100 sunxi_gpio_set_cfgpin(SUNXI_GPC(2), pin_function);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300101
Andre Przywara0c882df2020-01-28 00:46:43 +0000102 /* All chips except H6 use PC1, and only H6 uses PC5. */
103 if (!IS_ENABLED(CONFIG_MACH_SUN50I_H6))
104 sunxi_gpio_set_cfgpin(SUNXI_GPC(1), pin_function);
105 else
106 sunxi_gpio_set_cfgpin(SUNXI_GPC(5), pin_function);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300107
Andre Przywara0c882df2020-01-28 00:46:43 +0000108 /* Older generations use PC23 for CS, newer ones use PC3. */
Andre Przywarada3bd452020-01-28 00:46:42 +0000109 if (IS_ENABLED(CONFIG_MACH_SUN4I) || IS_ENABLED(CONFIG_MACH_SUN7I) ||
110 IS_ENABLED(CONFIG_MACH_SUN8I_R40))
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300111 sunxi_gpio_set_cfgpin(SUNXI_GPC(23), pin_function);
112 else
113 sunxi_gpio_set_cfgpin(SUNXI_GPC(3), pin_function);
114}
115
Andre Przywara382dab22020-01-28 00:46:41 +0000116static bool is_sun6i_gen_spi(void)
117{
Andre Przywara0c882df2020-01-28 00:46:43 +0000118 return IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I) ||
119 IS_ENABLED(CONFIG_MACH_SUN50I_H6);
Andre Przywara382dab22020-01-28 00:46:41 +0000120}
121
Andre Przywara5c7624d2020-01-28 00:46:40 +0000122static uintptr_t spi0_base_address(void)
123{
Andre Przywarada3bd452020-01-28 00:46:42 +0000124 if (IS_ENABLED(CONFIG_MACH_SUN8I_R40))
125 return 0x01C05000;
126
Andre Przywara0c882df2020-01-28 00:46:43 +0000127 if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
128 return 0x05010000;
129
Andre Przywara382dab22020-01-28 00:46:41 +0000130 if (!is_sun6i_gen_spi())
Andre Przywara5c7624d2020-01-28 00:46:40 +0000131 return 0x01C05000;
132
133 return 0x01C68000;
134}
135
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300136/*
137 * Setup 6 MHz from OSC24M (because the BROM is doing the same).
138 */
139static void spi0_enable_clock(void)
140{
Andre Przywara5c7624d2020-01-28 00:46:40 +0000141 uintptr_t base = spi0_base_address();
142
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300143 /* Deassert SPI0 reset on SUN6I */
Andre Przywara0c882df2020-01-28 00:46:43 +0000144 if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
145 setbits_le32(CCM_H6_SPI_BGR_REG, (1U << 16) | 0x1);
146 else if (is_sun6i_gen_spi())
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300147 setbits_le32(SUN6I_BUS_SOFT_RST_REG0,
148 (1 << AHB_RESET_SPI0_SHIFT));
149
150 /* Open the SPI0 gate */
Andre Przywara0c882df2020-01-28 00:46:43 +0000151 if (!IS_ENABLED(CONFIG_MACH_SUN50I_H6))
152 setbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0));
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300153
154 /* Divide by 4 */
Andre Przywara382dab22020-01-28 00:46:41 +0000155 writel(SPI0_CLK_DIV_BY_4, base + (is_sun6i_gen_spi() ?
Andre Przywara5c7624d2020-01-28 00:46:40 +0000156 SUN6I_SPI0_CCTL : SUN4I_SPI0_CCTL));
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300157 /* 24MHz from OSC24M */
158 writel((1 << 31), CCM_SPI0_CLK);
159
Andre Przywara382dab22020-01-28 00:46:41 +0000160 if (is_sun6i_gen_spi()) {
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300161 /* Enable SPI in the master mode and do a soft reset */
Andre Przywara5c7624d2020-01-28 00:46:40 +0000162 setbits_le32(base + SUN6I_SPI0_GCR, SUN6I_CTL_MASTER |
163 SUN6I_CTL_ENABLE | SUN6I_CTL_SRST);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300164 /* Wait for completion */
Andre Przywara5c7624d2020-01-28 00:46:40 +0000165 while (readl(base + SUN6I_SPI0_GCR) & SUN6I_CTL_SRST)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300166 ;
167 } else {
168 /* Enable SPI in the master mode and reset FIFO */
Andre Przywara5c7624d2020-01-28 00:46:40 +0000169 setbits_le32(base + SUN4I_SPI0_CTL, SUN4I_CTL_MASTER |
170 SUN4I_CTL_ENABLE |
171 SUN4I_CTL_TF_RST |
172 SUN4I_CTL_RF_RST);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300173 }
174}
175
176static void spi0_disable_clock(void)
177{
Andre Przywara5c7624d2020-01-28 00:46:40 +0000178 uintptr_t base = spi0_base_address();
179
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300180 /* Disable the SPI0 controller */
Andre Przywara382dab22020-01-28 00:46:41 +0000181 if (is_sun6i_gen_spi())
Andre Przywara5c7624d2020-01-28 00:46:40 +0000182 clrbits_le32(base + SUN6I_SPI0_GCR, SUN6I_CTL_MASTER |
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300183 SUN6I_CTL_ENABLE);
184 else
Andre Przywara5c7624d2020-01-28 00:46:40 +0000185 clrbits_le32(base + SUN4I_SPI0_CTL, SUN4I_CTL_MASTER |
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300186 SUN4I_CTL_ENABLE);
187
188 /* Disable the SPI0 clock */
189 writel(0, CCM_SPI0_CLK);
190
191 /* Close the SPI0 gate */
Andre Przywara0c882df2020-01-28 00:46:43 +0000192 if (!IS_ENABLED(CONFIG_MACH_SUN50I_H6))
193 clrbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0));
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300194
195 /* Assert SPI0 reset on SUN6I */
Andre Przywara0c882df2020-01-28 00:46:43 +0000196 if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
197 clrbits_le32(CCM_H6_SPI_BGR_REG, (1U << 16) | 0x1);
198 else if (is_sun6i_gen_spi())
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300199 clrbits_le32(SUN6I_BUS_SOFT_RST_REG0,
200 (1 << AHB_RESET_SPI0_SHIFT));
201}
202
Andre Przywara90895f62016-11-20 14:56:55 +0000203static void spi0_init(void)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300204{
205 unsigned int pin_function = SUNXI_GPC_SPI0;
Andre Przywara90895f62016-11-20 14:56:55 +0000206
Andre Przywara0c882df2020-01-28 00:46:43 +0000207 if (IS_ENABLED(CONFIG_MACH_SUN50I) ||
208 IS_ENABLED(CONFIG_MACH_SUN50I_H6))
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300209 pin_function = SUN50I_GPC_SPI0;
210
211 spi0_pinmux_setup(pin_function);
212 spi0_enable_clock();
213}
214
215static void spi0_deinit(void)
216{
217 /* New SoCs can disable pins, older could only set them as input */
218 unsigned int pin_function = SUNXI_GPIO_INPUT;
Andre Przywara382dab22020-01-28 00:46:41 +0000219
220 if (is_sun6i_gen_spi())
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300221 pin_function = SUNXI_GPIO_DISABLE;
222
223 spi0_disable_clock();
224 spi0_pinmux_setup(pin_function);
225}
226
227/*****************************************************************************/
228
229#define SPI_READ_MAX_SIZE 60 /* FIFO size, minus 4 bytes of the header */
230
231static void sunxi_spi0_read_data(u8 *buf, u32 addr, u32 bufsize,
Andre Przywarac10848d2017-02-16 01:20:25 +0000232 ulong spi_ctl_reg,
233 ulong spi_ctl_xch_bitmask,
234 ulong spi_fifo_reg,
235 ulong spi_tx_reg,
236 ulong spi_rx_reg,
237 ulong spi_bc_reg,
238 ulong spi_tc_reg,
239 ulong spi_bcc_reg)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300240{
241 writel(4 + bufsize, spi_bc_reg); /* Burst counter (total bytes) */
242 writel(4, spi_tc_reg); /* Transfer counter (bytes to send) */
243 if (spi_bcc_reg)
244 writel(4, spi_bcc_reg); /* SUN6I also needs this */
245
246 /* Send the Read Data Bytes (03h) command header */
247 writeb(0x03, spi_tx_reg);
248 writeb((u8)(addr >> 16), spi_tx_reg);
249 writeb((u8)(addr >> 8), spi_tx_reg);
250 writeb((u8)(addr), spi_tx_reg);
251
252 /* Start the data transfer */
253 setbits_le32(spi_ctl_reg, spi_ctl_xch_bitmask);
254
255 /* Wait until everything is received in the RX FIFO */
256 while ((readl(spi_fifo_reg) & 0x7F) < 4 + bufsize)
257 ;
258
259 /* Skip 4 bytes */
260 readl(spi_rx_reg);
261
262 /* Read the data */
263 while (bufsize-- > 0)
264 *buf++ = readb(spi_rx_reg);
265
266 /* tSHSL time is up to 100 ns in various SPI flash datasheets */
267 udelay(1);
268}
269
270static void spi0_read_data(void *buf, u32 addr, u32 len)
271{
272 u8 *buf8 = buf;
273 u32 chunk_len;
Andre Przywara5c7624d2020-01-28 00:46:40 +0000274 uintptr_t base = spi0_base_address();
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300275
276 while (len > 0) {
277 chunk_len = len;
278 if (chunk_len > SPI_READ_MAX_SIZE)
279 chunk_len = SPI_READ_MAX_SIZE;
280
Andre Przywara382dab22020-01-28 00:46:41 +0000281 if (is_sun6i_gen_spi()) {
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300282 sunxi_spi0_read_data(buf8, addr, chunk_len,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000283 base + SUN6I_SPI0_TCR,
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300284 SUN6I_TCR_XCH,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000285 base + SUN6I_SPI0_FIFO_STA,
286 base + SUN6I_SPI0_TXD,
287 base + SUN6I_SPI0_RXD,
288 base + SUN6I_SPI0_MBC,
289 base + SUN6I_SPI0_MTC,
290 base + SUN6I_SPI0_BCC);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300291 } else {
292 sunxi_spi0_read_data(buf8, addr, chunk_len,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000293 base + SUN4I_SPI0_CTL,
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300294 SUN4I_CTL_XCH,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000295 base + SUN4I_SPI0_FIFO_STA,
296 base + SUN4I_SPI0_TX,
297 base + SUN4I_SPI0_RX,
298 base + SUN4I_SPI0_BC,
299 base + SUN4I_SPI0_TC,
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300300 0);
301 }
302
303 len -= chunk_len;
304 buf8 += chunk_len;
305 addr += chunk_len;
306 }
307}
308
Andre Przywara230fed72017-09-22 22:57:22 +0100309static ulong spi_load_read(struct spl_load_info *load, ulong sector,
310 ulong count, void *buf)
311{
312 spi0_read_data(buf, sector, count);
313
314 return count;
315}
316
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300317/*****************************************************************************/
318
Simon Glass0649e912016-09-24 18:20:14 -0600319static int spl_spi_load_image(struct spl_image_info *spl_image,
320 struct spl_boot_device *bootdev)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300321{
Andre Przywara230fed72017-09-22 22:57:22 +0100322 int ret = 0;
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300323 struct image_header *header;
324 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
325
326 spi0_init();
327
328 spi0_read_data((void *)header, CONFIG_SYS_SPI_U_BOOT_OFFS, 0x40);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300329
Andre Przywara230fed72017-09-22 22:57:22 +0100330 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
331 image_get_magic(header) == FDT_MAGIC) {
332 struct spl_load_info load;
333
334 debug("Found FIT image\n");
335 load.dev = NULL;
336 load.priv = NULL;
337 load.filename = NULL;
338 load.bl_len = 1;
339 load.read = spi_load_read;
340 ret = spl_load_simple_fit(spl_image, &load,
341 CONFIG_SYS_SPI_U_BOOT_OFFS, header);
342 } else {
343 ret = spl_parse_image_header(spl_image, header);
344 if (ret)
345 return ret;
346
347 spi0_read_data((void *)spl_image->load_addr,
348 CONFIG_SYS_SPI_U_BOOT_OFFS, spl_image->size);
349 }
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300350
351 spi0_deinit();
Andre Przywara230fed72017-09-22 22:57:22 +0100352
353 return ret;
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300354}
Simon Glassb9f6d892016-09-24 18:20:09 -0600355/* Use priorty 0 to override the default if it happens to be linked in */
Priit Laes19d39fc2017-01-02 20:24:50 +0200356SPL_LOAD_IMAGE_METHOD("sunxi SPI", 0, BOOT_DEVICE_SPI, spl_spi_load_image);