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);
133 *priv->rx_buf++ = byte;
137 static inline void sun4i_spi_fill_fifo(struct sun4i_spi_priv *priv, int len)
142 byte = priv->tx_buf ? *priv->tx_buf++ : 0;
143 writeb(byte, &priv->regs->txdata);
147 static void sun4i_spi_set_cs(struct udevice *bus, u8 cs, bool enable)
149 struct sun4i_spi_priv *priv = dev_get_priv(bus);
152 reg = readl(&priv->regs->ctl);
154 reg &= ~SUN4I_CTL_CS_MASK;
155 reg |= SUN4I_CTL_CS(cs);
158 reg &= ~SUN4I_CTL_CS_LEVEL;
160 reg |= SUN4I_CTL_CS_LEVEL;
162 writel(reg, &priv->regs->ctl);
165 static int sun4i_spi_parse_pins(struct udevice *dev)
167 const void *fdt = gd->fdt_blob;
168 const char *pin_name;
171 int drive, pull = 0, pin, i;
175 list = fdt_getprop(fdt, dev_of_offset(dev), "pinctrl-0", &size);
177 printf("WARNING: sun4i_spi: cannot find pinctrl-0 node\n");
182 phandle = fdt32_to_cpu(*list++);
183 size -= sizeof(*list);
185 offset = fdt_node_offset_by_phandle(fdt, phandle);
189 drive = fdt_getprop_u32_default_node(fdt, offset, 0,
190 "drive-strength", 0);
194 else if (drive <= 20)
196 else if (drive <= 30)
201 drive = fdt_getprop_u32_default_node(fdt, offset, 0,
204 drive = min(drive, 3);
207 if (fdt_get_property(fdt, offset, "bias-disable", NULL))
209 else if (fdt_get_property(fdt, offset, "bias-pull-up", NULL))
211 else if (fdt_get_property(fdt, offset, "bias-pull-down", NULL))
214 pull = fdt_getprop_u32_default_node(fdt, offset, 0,
220 pin_name = fdt_stringlist_get(fdt, offset,
223 pin_name = fdt_stringlist_get(fdt, offset,
230 pin = name_to_gpio(pin_name);
234 sunxi_gpio_set_cfgpin(pin, SUNXI_GPC_SPI0);
235 sunxi_gpio_set_drv(pin, drive);
236 sunxi_gpio_set_pull(pin, pull);
242 static inline void sun4i_spi_enable_clock(void)
244 struct sunxi_ccm_reg *const ccm =
245 (struct sunxi_ccm_reg *const)SUNXI_CCM_BASE;
247 setbits_le32(&ccm->ahb_gate0, (1 << AHB_GATE_OFFSET_SPI0));
248 writel((1 << 31), &ccm->spi0_clk_cfg);
251 static int sun4i_spi_ofdata_to_platdata(struct udevice *bus)
253 struct sun4i_spi_platdata *plat = dev_get_platdata(bus);
254 int node = dev_of_offset(bus);
256 plat->base_addr = devfdt_get_addr(bus);
257 plat->max_hz = fdtdec_get_int(gd->fdt_blob, node,
259 SUN4I_SPI_DEFAULT_RATE);
261 if (plat->max_hz > SUN4I_SPI_MAX_RATE)
262 plat->max_hz = SUN4I_SPI_MAX_RATE;
267 static int sun4i_spi_probe(struct udevice *bus)
269 struct sun4i_spi_platdata *plat = dev_get_platdata(bus);
270 struct sun4i_spi_priv *priv = dev_get_priv(bus);
272 sun4i_spi_enable_clock();
273 sun4i_spi_parse_pins(bus);
275 priv->regs = (struct sun4i_spi_regs *)(uintptr_t)plat->base_addr;
276 priv->freq = plat->max_hz;
281 static int sun4i_spi_claim_bus(struct udevice *dev)
283 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
285 writel(SUN4I_CTL_ENABLE | SUN4I_CTL_MASTER | SUN4I_CTL_TP |
286 SUN4I_CTL_CS_MANUAL | SUN4I_CTL_CS_ACTIVE_LOW,
291 static int sun4i_spi_release_bus(struct udevice *dev)
293 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
296 reg = readl(&priv->regs->ctl);
297 reg &= ~SUN4I_CTL_ENABLE;
298 writel(reg, &priv->regs->ctl);
303 static int sun4i_spi_xfer(struct udevice *dev, unsigned int bitlen,
304 const void *dout, void *din, unsigned long flags)
306 struct udevice *bus = dev->parent;
307 struct sun4i_spi_priv *priv = dev_get_priv(bus);
308 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
310 u32 len = bitlen / 8;
319 debug("%s: non byte-aligned SPI transfer.\n", __func__);
323 if (flags & SPI_XFER_BEGIN)
324 sun4i_spi_set_cs(bus, slave_plat->cs, true);
326 reg = readl(&priv->regs->ctl);
329 writel(reg | SUN4I_CTL_RF_RST | SUN4I_CTL_TF_RST, &priv->regs->ctl);
332 /* Setup the transfer now... */
333 nbytes = min(len, (u32)(SUN4I_FIFO_DEPTH - 1));
335 /* Setup the counters */
336 writel(SUN4I_BURST_CNT(nbytes), &priv->regs->bc);
337 writel(SUN4I_XMIT_CNT(nbytes), &priv->regs->tc);
339 /* Fill the TX FIFO */
340 sun4i_spi_fill_fifo(priv, nbytes);
342 /* Start the transfer */
343 reg = readl(&priv->regs->ctl);
344 writel(reg | SUN4I_CTL_XCH, &priv->regs->ctl);
346 /* Wait transfer to complete */
347 ret = wait_for_bit_le32(&priv->regs->ctl, SUN4I_CTL_XCH_MASK,
348 false, SUN4I_SPI_TIMEOUT_US, false);
350 printf("ERROR: sun4i_spi: Timeout transferring data\n");
351 sun4i_spi_set_cs(bus, slave_plat->cs, false);
355 /* Drain the RX FIFO */
356 sun4i_spi_drain_fifo(priv, nbytes);
361 if (flags & SPI_XFER_END)
362 sun4i_spi_set_cs(bus, slave_plat->cs, false);
367 static int sun4i_spi_set_speed(struct udevice *dev, uint speed)
369 struct sun4i_spi_platdata *plat = dev_get_platdata(dev);
370 struct sun4i_spi_priv *priv = dev_get_priv(dev);
374 if (speed > plat->max_hz)
375 speed = plat->max_hz;
377 if (speed < SUN4I_SPI_MIN_RATE)
378 speed = SUN4I_SPI_MIN_RATE;
380 * Setup clock divider.
382 * We have two choices there. Either we can use the clock
383 * divide rate 1, which is calculated thanks to this formula:
384 * SPI_CLK = MOD_CLK / (2 ^ (cdr + 1))
385 * Or we can use CDR2, which is calculated with the formula:
386 * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
387 * Whether we use the former or the latter is set through the
390 * First try CDR2, and if we can't reach the expected
391 * frequency, fall back to CDR1.
394 div = SUN4I_SPI_MAX_RATE / (2 * speed);
395 reg = readl(&priv->regs->cctl);
397 if (div <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
401 reg &= ~(SUN4I_CLK_CTL_CDR2_MASK | SUN4I_CLK_CTL_DRS);
402 reg |= SUN4I_CLK_CTL_CDR2(div) | SUN4I_CLK_CTL_DRS;
404 div = __ilog2(SUN4I_SPI_MAX_RATE) - __ilog2(speed);
405 reg &= ~((SUN4I_CLK_CTL_CDR1_MASK << 8) | SUN4I_CLK_CTL_DRS);
406 reg |= SUN4I_CLK_CTL_CDR1(div);
410 writel(reg, &priv->regs->cctl);
415 static int sun4i_spi_set_mode(struct udevice *dev, uint mode)
417 struct sun4i_spi_priv *priv = dev_get_priv(dev);
420 reg = readl(&priv->regs->ctl);
421 reg &= ~(SUN4I_CTL_CPOL | SUN4I_CTL_CPHA);
424 reg |= SUN4I_CTL_CPOL;
427 reg |= SUN4I_CTL_CPHA;
430 writel(reg, &priv->regs->ctl);
435 static const struct dm_spi_ops sun4i_spi_ops = {
436 .claim_bus = sun4i_spi_claim_bus,
437 .release_bus = sun4i_spi_release_bus,
438 .xfer = sun4i_spi_xfer,
439 .set_speed = sun4i_spi_set_speed,
440 .set_mode = sun4i_spi_set_mode,
443 static const struct udevice_id sun4i_spi_ids[] = {
444 { .compatible = "allwinner,sun4i-a10-spi" },
448 U_BOOT_DRIVER(sun4i_spi) = {
451 .of_match = sun4i_spi_ids,
452 .ops = &sun4i_spi_ops,
453 .ofdata_to_platdata = sun4i_spi_ofdata_to_platdata,
454 .platdata_auto_alloc_size = sizeof(struct sun4i_spi_platdata),
455 .priv_auto_alloc_size = sizeof(struct sun4i_spi_priv),
456 .probe = sun4i_spi_probe,