blob: d369df93d4a61663b7b2823b97c8021fc511857f [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>
10#include <asm/gpio.h>
11#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060012#include <linux/delay.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090013#include <linux/libfdt.h>
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030014
15#ifdef CONFIG_SPL_OS_BOOT
16#error CONFIG_SPL_OS_BOOT is not supported yet
17#endif
18
19/*
20 * This is a very simple U-Boot image loading implementation, trying to
21 * replicate what the boot ROM is doing when loading the SPL. Because we
22 * know the exact pins where the SPI Flash is connected and also know
23 * that the Read Data Bytes (03h) command is supported, the hardware
24 * configuration is very simple and we don't need the extra flexibility
25 * of the SPI framework. Moreover, we rely on the default settings of
26 * the SPI controler hardware registers and only adjust what needs to
27 * be changed. This is good for the code size and this implementation
28 * adds less than 400 bytes to the SPL.
29 *
30 * There are two variants of the SPI controller in Allwinner SoCs:
31 * A10/A13/A20 (sun4i variant) and everything else (sun6i variant).
32 * Both of them are supported.
33 *
34 * The pin mixing part is SoC specific and only A10/A13/A20/H3/A64 are
35 * supported at the moment.
36 */
37
38/*****************************************************************************/
39/* SUN4I variant of the SPI controller */
40/*****************************************************************************/
41
Andre Przywara5c7624d2020-01-28 00:46:40 +000042#define SUN4I_SPI0_CCTL 0x1C
43#define SUN4I_SPI0_CTL 0x08
44#define SUN4I_SPI0_RX 0x00
45#define SUN4I_SPI0_TX 0x04
46#define SUN4I_SPI0_FIFO_STA 0x28
47#define SUN4I_SPI0_BC 0x20
48#define SUN4I_SPI0_TC 0x24
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030049
50#define SUN4I_CTL_ENABLE BIT(0)
51#define SUN4I_CTL_MASTER BIT(1)
52#define SUN4I_CTL_TF_RST BIT(8)
53#define SUN4I_CTL_RF_RST BIT(9)
54#define SUN4I_CTL_XCH BIT(10)
55
56/*****************************************************************************/
57/* SUN6I variant of the SPI controller */
58/*****************************************************************************/
59
Andre Przywara5c7624d2020-01-28 00:46:40 +000060#define SUN6I_SPI0_CCTL 0x24
61#define SUN6I_SPI0_GCR 0x04
62#define SUN6I_SPI0_TCR 0x08
63#define SUN6I_SPI0_FIFO_STA 0x1C
64#define SUN6I_SPI0_MBC 0x30
65#define SUN6I_SPI0_MTC 0x34
66#define SUN6I_SPI0_BCC 0x38
67#define SUN6I_SPI0_TXD 0x200
68#define SUN6I_SPI0_RXD 0x300
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030069
70#define SUN6I_CTL_ENABLE BIT(0)
71#define SUN6I_CTL_MASTER BIT(1)
72#define SUN6I_CTL_SRST BIT(31)
73#define SUN6I_TCR_XCH BIT(31)
74
75/*****************************************************************************/
76
77#define CCM_AHB_GATING0 (0x01C20000 + 0x60)
Andre Przywara0c882df2020-01-28 00:46:43 +000078#define CCM_H6_SPI_BGR_REG (0x03001000 + 0x96c)
79#ifdef CONFIG_MACH_SUN50I_H6
80#define CCM_SPI0_CLK (0x03001000 + 0x940)
81#else
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030082#define CCM_SPI0_CLK (0x01C20000 + 0xA0)
Andre Przywara0c882df2020-01-28 00:46:43 +000083#endif
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030084#define SUN6I_BUS_SOFT_RST_REG0 (0x01C20000 + 0x2C0)
85
86#define AHB_RESET_SPI0_SHIFT 20
87#define AHB_GATE_OFFSET_SPI0 20
88
89#define SPI0_CLK_DIV_BY_2 0x1000
90#define SPI0_CLK_DIV_BY_4 0x1001
91
92/*****************************************************************************/
93
94/*
95 * Allwinner A10/A20 SoCs were using pins PC0,PC1,PC2,PC23 for booting
96 * from SPI Flash, everything else is using pins PC0,PC1,PC2,PC3.
Andre Przywara0c882df2020-01-28 00:46:43 +000097 * The H6 uses PC0, PC2, PC3, PC5.
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +030098 */
99static void spi0_pinmux_setup(unsigned int pin_function)
100{
Andre Przywara0c882df2020-01-28 00:46:43 +0000101 /* All chips use PC0 and PC2. */
102 sunxi_gpio_set_cfgpin(SUNXI_GPC(0), pin_function);
103 sunxi_gpio_set_cfgpin(SUNXI_GPC(2), pin_function);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300104
Andre Przywara0c882df2020-01-28 00:46:43 +0000105 /* All chips except H6 use PC1, and only H6 uses PC5. */
106 if (!IS_ENABLED(CONFIG_MACH_SUN50I_H6))
107 sunxi_gpio_set_cfgpin(SUNXI_GPC(1), pin_function);
108 else
109 sunxi_gpio_set_cfgpin(SUNXI_GPC(5), pin_function);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300110
Andre Przywara0c882df2020-01-28 00:46:43 +0000111 /* Older generations use PC23 for CS, newer ones use PC3. */
Andre Przywarada3bd452020-01-28 00:46:42 +0000112 if (IS_ENABLED(CONFIG_MACH_SUN4I) || IS_ENABLED(CONFIG_MACH_SUN7I) ||
113 IS_ENABLED(CONFIG_MACH_SUN8I_R40))
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300114 sunxi_gpio_set_cfgpin(SUNXI_GPC(23), pin_function);
115 else
116 sunxi_gpio_set_cfgpin(SUNXI_GPC(3), pin_function);
117}
118
Andre Przywara382dab22020-01-28 00:46:41 +0000119static bool is_sun6i_gen_spi(void)
120{
Andre Przywara0c882df2020-01-28 00:46:43 +0000121 return IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I) ||
122 IS_ENABLED(CONFIG_MACH_SUN50I_H6);
Andre Przywara382dab22020-01-28 00:46:41 +0000123}
124
Andre Przywara5c7624d2020-01-28 00:46:40 +0000125static uintptr_t spi0_base_address(void)
126{
Andre Przywarada3bd452020-01-28 00:46:42 +0000127 if (IS_ENABLED(CONFIG_MACH_SUN8I_R40))
128 return 0x01C05000;
129
Andre Przywara0c882df2020-01-28 00:46:43 +0000130 if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
131 return 0x05010000;
132
Andre Przywara382dab22020-01-28 00:46:41 +0000133 if (!is_sun6i_gen_spi())
Andre Przywara5c7624d2020-01-28 00:46:40 +0000134 return 0x01C05000;
135
136 return 0x01C68000;
137}
138
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300139/*
140 * Setup 6 MHz from OSC24M (because the BROM is doing the same).
141 */
142static void spi0_enable_clock(void)
143{
Andre Przywara5c7624d2020-01-28 00:46:40 +0000144 uintptr_t base = spi0_base_address();
145
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300146 /* Deassert SPI0 reset on SUN6I */
Andre Przywara0c882df2020-01-28 00:46:43 +0000147 if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
148 setbits_le32(CCM_H6_SPI_BGR_REG, (1U << 16) | 0x1);
149 else if (is_sun6i_gen_spi())
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300150 setbits_le32(SUN6I_BUS_SOFT_RST_REG0,
151 (1 << AHB_RESET_SPI0_SHIFT));
152
153 /* Open the SPI0 gate */
Andre Przywara0c882df2020-01-28 00:46:43 +0000154 if (!IS_ENABLED(CONFIG_MACH_SUN50I_H6))
155 setbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0));
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300156
157 /* Divide by 4 */
Andre Przywara382dab22020-01-28 00:46:41 +0000158 writel(SPI0_CLK_DIV_BY_4, base + (is_sun6i_gen_spi() ?
Andre Przywara5c7624d2020-01-28 00:46:40 +0000159 SUN6I_SPI0_CCTL : SUN4I_SPI0_CCTL));
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300160 /* 24MHz from OSC24M */
161 writel((1 << 31), CCM_SPI0_CLK);
162
Andre Przywara382dab22020-01-28 00:46:41 +0000163 if (is_sun6i_gen_spi()) {
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300164 /* Enable SPI in the master mode and do a soft reset */
Andre Przywara5c7624d2020-01-28 00:46:40 +0000165 setbits_le32(base + SUN6I_SPI0_GCR, SUN6I_CTL_MASTER |
166 SUN6I_CTL_ENABLE | SUN6I_CTL_SRST);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300167 /* Wait for completion */
Andre Przywara5c7624d2020-01-28 00:46:40 +0000168 while (readl(base + SUN6I_SPI0_GCR) & SUN6I_CTL_SRST)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300169 ;
170 } else {
171 /* Enable SPI in the master mode and reset FIFO */
Andre Przywara5c7624d2020-01-28 00:46:40 +0000172 setbits_le32(base + SUN4I_SPI0_CTL, SUN4I_CTL_MASTER |
173 SUN4I_CTL_ENABLE |
174 SUN4I_CTL_TF_RST |
175 SUN4I_CTL_RF_RST);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300176 }
177}
178
179static void spi0_disable_clock(void)
180{
Andre Przywara5c7624d2020-01-28 00:46:40 +0000181 uintptr_t base = spi0_base_address();
182
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300183 /* Disable the SPI0 controller */
Andre Przywara382dab22020-01-28 00:46:41 +0000184 if (is_sun6i_gen_spi())
Andre Przywara5c7624d2020-01-28 00:46:40 +0000185 clrbits_le32(base + SUN6I_SPI0_GCR, SUN6I_CTL_MASTER |
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300186 SUN6I_CTL_ENABLE);
187 else
Andre Przywara5c7624d2020-01-28 00:46:40 +0000188 clrbits_le32(base + SUN4I_SPI0_CTL, SUN4I_CTL_MASTER |
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300189 SUN4I_CTL_ENABLE);
190
191 /* Disable the SPI0 clock */
192 writel(0, CCM_SPI0_CLK);
193
194 /* Close the SPI0 gate */
Andre Przywara0c882df2020-01-28 00:46:43 +0000195 if (!IS_ENABLED(CONFIG_MACH_SUN50I_H6))
196 clrbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0));
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300197
198 /* Assert SPI0 reset on SUN6I */
Andre Przywara0c882df2020-01-28 00:46:43 +0000199 if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
200 clrbits_le32(CCM_H6_SPI_BGR_REG, (1U << 16) | 0x1);
201 else if (is_sun6i_gen_spi())
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300202 clrbits_le32(SUN6I_BUS_SOFT_RST_REG0,
203 (1 << AHB_RESET_SPI0_SHIFT));
204}
205
Andre Przywara90895f62016-11-20 14:56:55 +0000206static void spi0_init(void)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300207{
208 unsigned int pin_function = SUNXI_GPC_SPI0;
Andre Przywara90895f62016-11-20 14:56:55 +0000209
Andre Przywara0c882df2020-01-28 00:46:43 +0000210 if (IS_ENABLED(CONFIG_MACH_SUN50I) ||
211 IS_ENABLED(CONFIG_MACH_SUN50I_H6))
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300212 pin_function = SUN50I_GPC_SPI0;
213
214 spi0_pinmux_setup(pin_function);
215 spi0_enable_clock();
216}
217
218static void spi0_deinit(void)
219{
220 /* New SoCs can disable pins, older could only set them as input */
221 unsigned int pin_function = SUNXI_GPIO_INPUT;
Andre Przywara382dab22020-01-28 00:46:41 +0000222
223 if (is_sun6i_gen_spi())
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300224 pin_function = SUNXI_GPIO_DISABLE;
225
226 spi0_disable_clock();
227 spi0_pinmux_setup(pin_function);
228}
229
230/*****************************************************************************/
231
232#define SPI_READ_MAX_SIZE 60 /* FIFO size, minus 4 bytes of the header */
233
234static void sunxi_spi0_read_data(u8 *buf, u32 addr, u32 bufsize,
Andre Przywarac10848d2017-02-16 01:20:25 +0000235 ulong spi_ctl_reg,
236 ulong spi_ctl_xch_bitmask,
237 ulong spi_fifo_reg,
238 ulong spi_tx_reg,
239 ulong spi_rx_reg,
240 ulong spi_bc_reg,
241 ulong spi_tc_reg,
242 ulong spi_bcc_reg)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300243{
244 writel(4 + bufsize, spi_bc_reg); /* Burst counter (total bytes) */
245 writel(4, spi_tc_reg); /* Transfer counter (bytes to send) */
246 if (spi_bcc_reg)
247 writel(4, spi_bcc_reg); /* SUN6I also needs this */
248
249 /* Send the Read Data Bytes (03h) command header */
250 writeb(0x03, spi_tx_reg);
251 writeb((u8)(addr >> 16), spi_tx_reg);
252 writeb((u8)(addr >> 8), spi_tx_reg);
253 writeb((u8)(addr), spi_tx_reg);
254
255 /* Start the data transfer */
256 setbits_le32(spi_ctl_reg, spi_ctl_xch_bitmask);
257
258 /* Wait until everything is received in the RX FIFO */
259 while ((readl(spi_fifo_reg) & 0x7F) < 4 + bufsize)
260 ;
261
262 /* Skip 4 bytes */
263 readl(spi_rx_reg);
264
265 /* Read the data */
266 while (bufsize-- > 0)
267 *buf++ = readb(spi_rx_reg);
268
269 /* tSHSL time is up to 100 ns in various SPI flash datasheets */
270 udelay(1);
271}
272
273static void spi0_read_data(void *buf, u32 addr, u32 len)
274{
275 u8 *buf8 = buf;
276 u32 chunk_len;
Andre Przywara5c7624d2020-01-28 00:46:40 +0000277 uintptr_t base = spi0_base_address();
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300278
279 while (len > 0) {
280 chunk_len = len;
281 if (chunk_len > SPI_READ_MAX_SIZE)
282 chunk_len = SPI_READ_MAX_SIZE;
283
Andre Przywara382dab22020-01-28 00:46:41 +0000284 if (is_sun6i_gen_spi()) {
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300285 sunxi_spi0_read_data(buf8, addr, chunk_len,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000286 base + SUN6I_SPI0_TCR,
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300287 SUN6I_TCR_XCH,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000288 base + SUN6I_SPI0_FIFO_STA,
289 base + SUN6I_SPI0_TXD,
290 base + SUN6I_SPI0_RXD,
291 base + SUN6I_SPI0_MBC,
292 base + SUN6I_SPI0_MTC,
293 base + SUN6I_SPI0_BCC);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300294 } else {
295 sunxi_spi0_read_data(buf8, addr, chunk_len,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000296 base + SUN4I_SPI0_CTL,
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300297 SUN4I_CTL_XCH,
Andre Przywara5c7624d2020-01-28 00:46:40 +0000298 base + SUN4I_SPI0_FIFO_STA,
299 base + SUN4I_SPI0_TX,
300 base + SUN4I_SPI0_RX,
301 base + SUN4I_SPI0_BC,
302 base + SUN4I_SPI0_TC,
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300303 0);
304 }
305
306 len -= chunk_len;
307 buf8 += chunk_len;
308 addr += chunk_len;
309 }
310}
311
Andre Przywara230fed72017-09-22 22:57:22 +0100312static ulong spi_load_read(struct spl_load_info *load, ulong sector,
313 ulong count, void *buf)
314{
315 spi0_read_data(buf, sector, count);
316
317 return count;
318}
319
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300320/*****************************************************************************/
321
Simon Glass0649e912016-09-24 18:20:14 -0600322static int spl_spi_load_image(struct spl_image_info *spl_image,
323 struct spl_boot_device *bootdev)
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300324{
Andre Przywara230fed72017-09-22 22:57:22 +0100325 int ret = 0;
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300326 struct image_header *header;
327 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
328
329 spi0_init();
330
331 spi0_read_data((void *)header, CONFIG_SYS_SPI_U_BOOT_OFFS, 0x40);
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300332
Andre Przywara230fed72017-09-22 22:57:22 +0100333 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
334 image_get_magic(header) == FDT_MAGIC) {
335 struct spl_load_info load;
336
337 debug("Found FIT image\n");
338 load.dev = NULL;
339 load.priv = NULL;
340 load.filename = NULL;
341 load.bl_len = 1;
342 load.read = spi_load_read;
343 ret = spl_load_simple_fit(spl_image, &load,
344 CONFIG_SYS_SPI_U_BOOT_OFFS, header);
345 } else {
346 ret = spl_parse_image_header(spl_image, header);
347 if (ret)
348 return ret;
349
350 spi0_read_data((void *)spl_image->load_addr,
351 CONFIG_SYS_SPI_U_BOOT_OFFS, spl_image->size);
352 }
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300353
354 spi0_deinit();
Andre Przywara230fed72017-09-22 22:57:22 +0100355
356 return ret;
Siarhei Siamashka6f3ea202016-06-07 14:28:34 +0300357}
Simon Glassb9f6d892016-09-24 18:20:09 -0600358/* Use priorty 0 to override the default if it happens to be linked in */
Priit Laes19d39fc2017-01-02 20:24:50 +0200359SPL_LOAD_IMAGE_METHOD("sunxi SPI", 0, BOOT_DEVICE_SPI, spl_spi_load_image);