2 * Copyright (C) 2016 Siarhei Siamashka <siarhei.siamashka@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0+
13 #ifdef CONFIG_SPL_OS_BOOT
14 #error CONFIG_SPL_OS_BOOT is not supported yet
18 * This is a very simple U-Boot image loading implementation, trying to
19 * replicate what the boot ROM is doing when loading the SPL. Because we
20 * know the exact pins where the SPI Flash is connected and also know
21 * that the Read Data Bytes (03h) command is supported, the hardware
22 * configuration is very simple and we don't need the extra flexibility
23 * of the SPI framework. Moreover, we rely on the default settings of
24 * the SPI controler hardware registers and only adjust what needs to
25 * be changed. This is good for the code size and this implementation
26 * adds less than 400 bytes to the SPL.
28 * There are two variants of the SPI controller in Allwinner SoCs:
29 * A10/A13/A20 (sun4i variant) and everything else (sun6i variant).
30 * Both of them are supported.
32 * The pin mixing part is SoC specific and only A10/A13/A20/H3/A64 are
33 * supported at the moment.
36 /*****************************************************************************/
37 /* SUN4I variant of the SPI controller */
38 /*****************************************************************************/
40 #define SUN4I_SPI0_CCTL (0x01C05000 + 0x1C)
41 #define SUN4I_SPI0_CTL (0x01C05000 + 0x08)
42 #define SUN4I_SPI0_RX (0x01C05000 + 0x00)
43 #define SUN4I_SPI0_TX (0x01C05000 + 0x04)
44 #define SUN4I_SPI0_FIFO_STA (0x01C05000 + 0x28)
45 #define SUN4I_SPI0_BC (0x01C05000 + 0x20)
46 #define SUN4I_SPI0_TC (0x01C05000 + 0x24)
48 #define SUN4I_CTL_ENABLE BIT(0)
49 #define SUN4I_CTL_MASTER BIT(1)
50 #define SUN4I_CTL_TF_RST BIT(8)
51 #define SUN4I_CTL_RF_RST BIT(9)
52 #define SUN4I_CTL_XCH BIT(10)
54 /*****************************************************************************/
55 /* SUN6I variant of the SPI controller */
56 /*****************************************************************************/
58 #define SUN6I_SPI0_CCTL (0x01C68000 + 0x24)
59 #define SUN6I_SPI0_GCR (0x01C68000 + 0x04)
60 #define SUN6I_SPI0_TCR (0x01C68000 + 0x08)
61 #define SUN6I_SPI0_FIFO_STA (0x01C68000 + 0x1C)
62 #define SUN6I_SPI0_MBC (0x01C68000 + 0x30)
63 #define SUN6I_SPI0_MTC (0x01C68000 + 0x34)
64 #define SUN6I_SPI0_BCC (0x01C68000 + 0x38)
65 #define SUN6I_SPI0_TXD (0x01C68000 + 0x200)
66 #define SUN6I_SPI0_RXD (0x01C68000 + 0x300)
68 #define SUN6I_CTL_ENABLE BIT(0)
69 #define SUN6I_CTL_MASTER BIT(1)
70 #define SUN6I_CTL_SRST BIT(31)
71 #define SUN6I_TCR_XCH BIT(31)
73 /*****************************************************************************/
75 #define CCM_AHB_GATING0 (0x01C20000 + 0x60)
76 #define CCM_SPI0_CLK (0x01C20000 + 0xA0)
77 #define SUN6I_BUS_SOFT_RST_REG0 (0x01C20000 + 0x2C0)
79 #define AHB_RESET_SPI0_SHIFT 20
80 #define AHB_GATE_OFFSET_SPI0 20
82 #define SPI0_CLK_DIV_BY_2 0x1000
83 #define SPI0_CLK_DIV_BY_4 0x1001
85 /*****************************************************************************/
88 * Allwinner A10/A20 SoCs were using pins PC0,PC1,PC2,PC23 for booting
89 * from SPI Flash, everything else is using pins PC0,PC1,PC2,PC3.
91 static void spi0_pinmux_setup(unsigned int pin_function)
95 for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(2); pin++)
96 sunxi_gpio_set_cfgpin(pin, pin_function);
98 if (IS_ENABLED(CONFIG_MACH_SUN4I) || IS_ENABLED(CONFIG_MACH_SUN7I))
99 sunxi_gpio_set_cfgpin(SUNXI_GPC(23), pin_function);
101 sunxi_gpio_set_cfgpin(SUNXI_GPC(3), pin_function);
105 * Setup 6 MHz from OSC24M (because the BROM is doing the same).
107 static void spi0_enable_clock(void)
109 /* Deassert SPI0 reset on SUN6I */
110 if (IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I))
111 setbits_le32(SUN6I_BUS_SOFT_RST_REG0,
112 (1 << AHB_RESET_SPI0_SHIFT));
114 /* Open the SPI0 gate */
115 setbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0));
118 writel(SPI0_CLK_DIV_BY_4, IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I) ?
119 SUN6I_SPI0_CCTL : SUN4I_SPI0_CCTL);
120 /* 24MHz from OSC24M */
121 writel((1 << 31), CCM_SPI0_CLK);
123 if (IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I)) {
124 /* Enable SPI in the master mode and do a soft reset */
125 setbits_le32(SUN6I_SPI0_GCR, SUN6I_CTL_MASTER |
128 /* Wait for completion */
129 while (readl(SUN6I_SPI0_GCR) & SUN6I_CTL_SRST)
132 /* Enable SPI in the master mode and reset FIFO */
133 setbits_le32(SUN4I_SPI0_CTL, SUN4I_CTL_MASTER |
140 static void spi0_disable_clock(void)
142 /* Disable the SPI0 controller */
143 if (IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I))
144 clrbits_le32(SUN6I_SPI0_GCR, SUN6I_CTL_MASTER |
147 clrbits_le32(SUN4I_SPI0_CTL, SUN4I_CTL_MASTER |
150 /* Disable the SPI0 clock */
151 writel(0, CCM_SPI0_CLK);
153 /* Close the SPI0 gate */
154 clrbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0));
156 /* Assert SPI0 reset on SUN6I */
157 if (IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I))
158 clrbits_le32(SUN6I_BUS_SOFT_RST_REG0,
159 (1 << AHB_RESET_SPI0_SHIFT));
162 static void spi0_init(void)
164 unsigned int pin_function = SUNXI_GPC_SPI0;
166 if (IS_ENABLED(CONFIG_MACH_SUN50I))
167 pin_function = SUN50I_GPC_SPI0;
169 spi0_pinmux_setup(pin_function);
173 static void spi0_deinit(void)
175 /* New SoCs can disable pins, older could only set them as input */
176 unsigned int pin_function = SUNXI_GPIO_INPUT;
177 if (IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I))
178 pin_function = SUNXI_GPIO_DISABLE;
180 spi0_disable_clock();
181 spi0_pinmux_setup(pin_function);
184 /*****************************************************************************/
186 #define SPI_READ_MAX_SIZE 60 /* FIFO size, minus 4 bytes of the header */
188 static void sunxi_spi0_read_data(u8 *buf, u32 addr, u32 bufsize,
190 ulong spi_ctl_xch_bitmask,
198 writel(4 + bufsize, spi_bc_reg); /* Burst counter (total bytes) */
199 writel(4, spi_tc_reg); /* Transfer counter (bytes to send) */
201 writel(4, spi_bcc_reg); /* SUN6I also needs this */
203 /* Send the Read Data Bytes (03h) command header */
204 writeb(0x03, spi_tx_reg);
205 writeb((u8)(addr >> 16), spi_tx_reg);
206 writeb((u8)(addr >> 8), spi_tx_reg);
207 writeb((u8)(addr), spi_tx_reg);
209 /* Start the data transfer */
210 setbits_le32(spi_ctl_reg, spi_ctl_xch_bitmask);
212 /* Wait until everything is received in the RX FIFO */
213 while ((readl(spi_fifo_reg) & 0x7F) < 4 + bufsize)
220 while (bufsize-- > 0)
221 *buf++ = readb(spi_rx_reg);
223 /* tSHSL time is up to 100 ns in various SPI flash datasheets */
227 static void spi0_read_data(void *buf, u32 addr, u32 len)
234 if (chunk_len > SPI_READ_MAX_SIZE)
235 chunk_len = SPI_READ_MAX_SIZE;
237 if (IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I)) {
238 sunxi_spi0_read_data(buf8, addr, chunk_len,
248 sunxi_spi0_read_data(buf8, addr, chunk_len,
265 static ulong spi_load_read(struct spl_load_info *load, ulong sector,
266 ulong count, void *buf)
268 spi0_read_data(buf, sector, count);
273 /*****************************************************************************/
275 static int spl_spi_load_image(struct spl_image_info *spl_image,
276 struct spl_boot_device *bootdev)
279 struct image_header *header;
280 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
284 spi0_read_data((void *)header, CONFIG_SYS_SPI_U_BOOT_OFFS, 0x40);
286 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
287 image_get_magic(header) == FDT_MAGIC) {
288 struct spl_load_info load;
290 debug("Found FIT image\n");
293 load.filename = NULL;
295 load.read = spi_load_read;
296 ret = spl_load_simple_fit(spl_image, &load,
297 CONFIG_SYS_SPI_U_BOOT_OFFS, header);
299 ret = spl_parse_image_header(spl_image, header);
303 spi0_read_data((void *)spl_image->load_addr,
304 CONFIG_SYS_SPI_U_BOOT_OFFS, spl_image->size);
311 /* Use priorty 0 to override the default if it happens to be linked in */
312 SPL_LOAD_IMAGE_METHOD("sunxi SPI", 0, BOOT_DEVICE_SPI, spl_spi_load_image);