2 * (C) Copyright 2017 Whitebox Systems / Northend Systems B.V.
3 * S.J.R. van Schaik <stephan@whiteboxsystems.nl>
4 * M.B.W. Wajer <merlijn@whiteboxsystems.nl>
6 * (C) Copyright 2017 Olimex Ltd..
7 * Stefan Mavrodiev <stefan@olimex.com>
9 * Based on linux spi driver. Original copyright follows:
10 * linux/drivers/spi/spi-sun4i.c
12 * Copyright (C) 2012 - 2014 Allwinner Tech
13 * Pan Nan <pannan@allwinnertech.com>
15 * Copyright (C) 2014 Maxime Ripard
16 * Maxime Ripard <maxime.ripard@free-electrons.com>
18 * SPDX-License-Identifier: GPL-2.0+
25 #include <fdt_support.h>
28 #include <asm/bitops.h>
32 #include <asm/arch/clock.h>
34 #define SUN4I_FIFO_DEPTH 64
36 #define SUN4I_RXDATA_REG 0x00
38 #define SUN4I_TXDATA_REG 0x04
40 #define SUN4I_CTL_REG 0x08
41 #define SUN4I_CTL_ENABLE BIT(0)
42 #define SUN4I_CTL_MASTER BIT(1)
43 #define SUN4I_CTL_CPHA BIT(2)
44 #define SUN4I_CTL_CPOL BIT(3)
45 #define SUN4I_CTL_CS_ACTIVE_LOW BIT(4)
46 #define SUN4I_CTL_LMTF BIT(6)
47 #define SUN4I_CTL_TF_RST BIT(8)
48 #define SUN4I_CTL_RF_RST BIT(9)
49 #define SUN4I_CTL_XCH_MASK 0x0400
50 #define SUN4I_CTL_XCH BIT(10)
51 #define SUN4I_CTL_CS_MASK 0x3000
52 #define SUN4I_CTL_CS(cs) (((cs) << 12) & SUN4I_CTL_CS_MASK)
53 #define SUN4I_CTL_DHB BIT(15)
54 #define SUN4I_CTL_CS_MANUAL BIT(16)
55 #define SUN4I_CTL_CS_LEVEL BIT(17)
56 #define SUN4I_CTL_TP BIT(18)
58 #define SUN4I_INT_CTL_REG 0x0c
59 #define SUN4I_INT_CTL_RF_F34 BIT(4)
60 #define SUN4I_INT_CTL_TF_E34 BIT(12)
61 #define SUN4I_INT_CTL_TC BIT(16)
63 #define SUN4I_INT_STA_REG 0x10
65 #define SUN4I_DMA_CTL_REG 0x14
67 #define SUN4I_WAIT_REG 0x18
69 #define SUN4I_CLK_CTL_REG 0x1c
70 #define SUN4I_CLK_CTL_CDR2_MASK 0xff
71 #define SUN4I_CLK_CTL_CDR2(div) ((div) & SUN4I_CLK_CTL_CDR2_MASK)
72 #define SUN4I_CLK_CTL_CDR1_MASK 0xf
73 #define SUN4I_CLK_CTL_CDR1(div) (((div) & SUN4I_CLK_CTL_CDR1_MASK) << 8)
74 #define SUN4I_CLK_CTL_DRS BIT(12)
76 #define SUN4I_MAX_XFER_SIZE 0xffffff
78 #define SUN4I_BURST_CNT_REG 0x20
79 #define SUN4I_BURST_CNT(cnt) ((cnt) & SUN4I_MAX_XFER_SIZE)
81 #define SUN4I_XMIT_CNT_REG 0x24
82 #define SUN4I_XMIT_CNT(cnt) ((cnt) & SUN4I_MAX_XFER_SIZE)
84 #define SUN4I_FIFO_STA_REG 0x28
85 #define SUN4I_FIFO_STA_RF_CNT_MASK 0x7f
86 #define SUN4I_FIFO_STA_RF_CNT_BITS 0
87 #define SUN4I_FIFO_STA_TF_CNT_MASK 0x7f
88 #define SUN4I_FIFO_STA_TF_CNT_BITS 16
90 #define SUN4I_SPI_MAX_RATE 24000000
91 #define SUN4I_SPI_MIN_RATE 3000
92 #define SUN4I_SPI_DEFAULT_RATE 1000000
93 #define SUN4I_SPI_TIMEOUT_US 1000000
95 /* sun4i spi register set */
96 struct sun4i_spi_regs {
110 struct sun4i_spi_platdata {
115 struct sun4i_spi_priv {
116 struct sun4i_spi_regs *regs;
124 DECLARE_GLOBAL_DATA_PTR;
126 static inline void sun4i_spi_drain_fifo(struct sun4i_spi_priv *priv, int len)
131 byte = readb(&priv->regs->rxdata);
132 *priv->rx_buf++ = byte;
136 static inline void sun4i_spi_fill_fifo(struct sun4i_spi_priv *priv, int len)
141 byte = priv->tx_buf ? *priv->tx_buf++ : 0;
142 writeb(byte, &priv->regs->txdata);
146 static void sun4i_spi_set_cs(struct udevice *bus, u8 cs, bool enable)
148 struct sun4i_spi_priv *priv = dev_get_priv(bus);
151 reg = readl(&priv->regs->ctl);
153 reg &= ~SUN4I_CTL_CS_MASK;
154 reg |= SUN4I_CTL_CS(cs);
157 reg &= ~SUN4I_CTL_CS_LEVEL;
159 reg |= SUN4I_CTL_CS_LEVEL;
161 writel(reg, &priv->regs->ctl);
164 static int sun4i_spi_parse_pins(struct udevice *dev)
166 const void *fdt = gd->fdt_blob;
167 const char *pin_name;
170 int drive, pull = 0, pin, i;
174 list = fdt_getprop(fdt, dev_of_offset(dev), "pinctrl-0", &size);
176 printf("WARNING: sun4i_spi: cannot find pinctrl-0 node\n");
181 phandle = fdt32_to_cpu(*list++);
182 size -= sizeof(*list);
184 offset = fdt_node_offset_by_phandle(fdt, phandle);
188 drive = fdt_getprop_u32_default_node(fdt, offset, 0,
189 "drive-strength", 0);
193 else if (drive <= 20)
195 else if (drive <= 30)
200 drive = fdt_getprop_u32_default_node(fdt, offset, 0,
203 drive = min(drive, 3);
206 if (fdt_get_property(fdt, offset, "bias-disable", NULL))
208 else if (fdt_get_property(fdt, offset, "bias-pull-up", NULL))
210 else if (fdt_get_property(fdt, offset, "bias-pull-down", NULL))
213 pull = fdt_getprop_u32_default_node(fdt, offset, 0,
219 pin_name = fdt_stringlist_get(fdt, offset,
222 pin_name = fdt_stringlist_get(fdt, offset,
229 pin = name_to_gpio(pin_name);
233 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SPI0);
234 sunxi_gpio_set_drv(pin, drive);
235 sunxi_gpio_set_pull(pin, pull);
241 static inline void sun4i_spi_enable_clock(void)
243 struct sunxi_ccm_reg *const ccm =
244 (struct sunxi_ccm_reg *const)SUNXI_CCM_BASE;
246 setbits_le32(&ccm->ahb_gate0, (1 << AHB_GATE_OFFSET_SPI0));
247 writel((1 << 31), &ccm->spi0_clk_cfg);
250 static int sun4i_spi_ofdata_to_platdata(struct udevice *bus)
252 struct sun4i_spi_platdata *plat = dev_get_platdata(bus);
253 int node = dev_of_offset(bus);
255 plat->base_addr = devfdt_get_addr(bus);
256 plat->max_hz = fdtdec_get_int(gd->fdt_blob, node,
258 SUN4I_SPI_DEFAULT_RATE);
260 if (plat->max_hz > SUN4I_SPI_MAX_RATE)
261 plat->max_hz = SUN4I_SPI_MAX_RATE;
266 static int sun4i_spi_probe(struct udevice *bus)
268 struct sun4i_spi_platdata *plat = dev_get_platdata(bus);
269 struct sun4i_spi_priv *priv = dev_get_priv(bus);
271 sun4i_spi_enable_clock();
272 sun4i_spi_parse_pins(bus);
274 priv->regs = (struct sun4i_spi_regs *)(uintptr_t)plat->base_addr;
275 priv->freq = plat->max_hz;
280 static int sun4i_spi_claim_bus(struct udevice *dev)
282 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
284 writel(SUN4I_CTL_ENABLE | SUN4I_CTL_MASTER | SUN4I_CTL_TP |
285 SUN4I_CTL_CS_MANUAL | SUN4I_CTL_CS_ACTIVE_LOW,
290 static int sun4i_spi_release_bus(struct udevice *dev)
292 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
295 reg = readl(&priv->regs->ctl);
296 reg &= ~SUN4I_CTL_ENABLE;
297 writel(reg, &priv->regs->ctl);
302 static int sun4i_spi_xfer(struct udevice *dev, unsigned int bitlen,
303 const void *dout, void *din, unsigned long flags)
305 struct udevice *bus = dev->parent;
306 struct sun4i_spi_priv *priv = dev_get_priv(bus);
307 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
309 u32 len = bitlen / 8;
318 debug("%s: non byte-aligned SPI transfer.\n", __func__);
322 if (flags & SPI_XFER_BEGIN)
323 sun4i_spi_set_cs(bus, slave_plat->cs, true);
325 reg = readl(&priv->regs->ctl);
328 writel(reg | SUN4I_CTL_RF_RST | SUN4I_CTL_TF_RST, &priv->regs->ctl);
331 /* Setup the transfer now... */
332 nbytes = min(len, (u32)(SUN4I_FIFO_DEPTH - 1));
334 /* Setup the counters */
335 writel(SUN4I_BURST_CNT(nbytes), &priv->regs->bc);
336 writel(SUN4I_XMIT_CNT(nbytes), &priv->regs->tc);
338 /* Fill the TX FIFO */
339 sun4i_spi_fill_fifo(priv, nbytes);
341 /* Start the transfer */
342 reg = readl(&priv->regs->ctl);
343 writel(reg | SUN4I_CTL_XCH, &priv->regs->ctl);
345 /* Wait transfer to complete */
346 ret = wait_for_bit_le32(&priv->regs->ctl, SUN4I_CTL_XCH_MASK,
347 false, SUN4I_SPI_TIMEOUT_US, false);
349 printf("ERROR: sun4i_spi: Timeout transferring data\n");
350 sun4i_spi_set_cs(bus, slave_plat->cs, false);
354 /* Drain the RX FIFO */
355 sun4i_spi_drain_fifo(priv, nbytes);
360 if (flags & SPI_XFER_END)
361 sun4i_spi_set_cs(bus, slave_plat->cs, false);
366 static int sun4i_spi_set_speed(struct udevice *dev, uint speed)
368 struct sun4i_spi_platdata *plat = dev_get_platdata(dev);
369 struct sun4i_spi_priv *priv = dev_get_priv(dev);
373 if (speed > plat->max_hz)
374 speed = plat->max_hz;
376 if (speed < SUN4I_SPI_MIN_RATE)
377 speed = SUN4I_SPI_MIN_RATE;
379 * Setup clock divider.
381 * We have two choices there. Either we can use the clock
382 * divide rate 1, which is calculated thanks to this formula:
383 * SPI_CLK = MOD_CLK / (2 ^ (cdr + 1))
384 * Or we can use CDR2, which is calculated with the formula:
385 * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
386 * Whether we use the former or the latter is set through the
389 * First try CDR2, and if we can't reach the expected
390 * frequency, fall back to CDR1.
393 div = SUN4I_SPI_MAX_RATE / (2 * speed);
394 reg = readl(&priv->regs->cctl);
396 if (div <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
400 reg &= ~(SUN4I_CLK_CTL_CDR2_MASK | SUN4I_CLK_CTL_DRS);
401 reg |= SUN4I_CLK_CTL_CDR2(div) | SUN4I_CLK_CTL_DRS;
403 div = __ilog2(SUN4I_SPI_MAX_RATE) - __ilog2(speed);
404 reg &= ~((SUN4I_CLK_CTL_CDR1_MASK << 8) | SUN4I_CLK_CTL_DRS);
405 reg |= SUN4I_CLK_CTL_CDR1(div);
409 writel(reg, &priv->regs->cctl);
414 static int sun4i_spi_set_mode(struct udevice *dev, uint mode)
416 struct sun4i_spi_priv *priv = dev_get_priv(dev);
419 reg = readl(&priv->regs->ctl);
420 reg &= ~(SUN4I_CTL_CPOL | SUN4I_CTL_CPHA);
423 reg |= SUN4I_CTL_CPOL;
426 reg |= SUN4I_CTL_CPHA;
429 writel(reg, &priv->regs->ctl);
434 static const struct dm_spi_ops sun4i_spi_ops = {
435 .claim_bus = sun4i_spi_claim_bus,
436 .release_bus = sun4i_spi_release_bus,
437 .xfer = sun4i_spi_xfer,
438 .set_speed = sun4i_spi_set_speed,
439 .set_mode = sun4i_spi_set_mode,
442 static const struct udevice_id sun4i_spi_ids[] = {
443 { .compatible = "allwinner,sun4i-a10-spi" },
447 U_BOOT_DRIVER(sun4i_spi) = {
450 .of_match = sun4i_spi_ids,
451 .ops = &sun4i_spi_ops,
452 .ofdata_to_platdata = sun4i_spi_ofdata_to_platdata,
453 .platdata_auto_alloc_size = sizeof(struct sun4i_spi_platdata),
454 .priv_auto_alloc_size = sizeof(struct sun4i_spi_priv),
455 .probe = sun4i_spi_probe,