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+
27 #include <fdt_support.h>
30 #include <asm/global_data.h>
31 #include <dm/device_compat.h>
32 #include <linux/bitops.h>
34 #include <asm/bitops.h>
37 #include <linux/iopoll.h>
39 DECLARE_GLOBAL_DATA_PTR;
41 /* sun4i spi registers */
42 #define SUN4I_RXDATA_REG 0x00
43 #define SUN4I_TXDATA_REG 0x04
44 #define SUN4I_CTL_REG 0x08
45 #define SUN4I_CLK_CTL_REG 0x1c
46 #define SUN4I_BURST_CNT_REG 0x20
47 #define SUN4I_XMIT_CNT_REG 0x24
48 #define SUN4I_FIFO_STA_REG 0x28
50 /* sun6i spi registers */
51 #define SUN6I_GBL_CTL_REG 0x04
52 #define SUN6I_TFR_CTL_REG 0x08
53 #define SUN6I_FIFO_CTL_REG 0x18
54 #define SUN6I_FIFO_STA_REG 0x1c
55 #define SUN6I_CLK_CTL_REG 0x24
56 #define SUN6I_BURST_CNT_REG 0x30
57 #define SUN6I_XMIT_CNT_REG 0x34
58 #define SUN6I_BURST_CTL_REG 0x38
59 #define SUN6I_TXDATA_REG 0x200
60 #define SUN6I_RXDATA_REG 0x300
63 #define SUN4I_CTL_ENABLE BIT(0)
64 #define SUN4I_CTL_MASTER BIT(1)
65 #define SUN4I_CLK_CTL_CDR2_MASK 0xff
66 #define SUN4I_CLK_CTL_CDR2(div) ((div) & SUN4I_CLK_CTL_CDR2_MASK)
67 #define SUN4I_CLK_CTL_CDR1_MASK 0xf
68 #define SUN4I_CLK_CTL_CDR1(div) (((div) & SUN4I_CLK_CTL_CDR1_MASK) << 8)
69 #define SUN4I_CLK_CTL_DRS BIT(12)
70 #define SUN4I_MAX_XFER_SIZE 0xffffff
71 #define SUN4I_BURST_CNT(cnt) ((cnt) & SUN4I_MAX_XFER_SIZE)
72 #define SUN4I_XMIT_CNT(cnt) ((cnt) & SUN4I_MAX_XFER_SIZE)
73 #define SUN4I_FIFO_STA_RF_CNT_BITS 0
75 #define SUN4I_SPI_MAX_RATE 24000000
76 #define SUN4I_SPI_MIN_RATE 3000
77 #define SUN4I_SPI_DEFAULT_RATE 1000000
78 #define SUN4I_SPI_TIMEOUT_MS 1000
80 #define SPI_REG(priv, reg) ((priv)->base + \
81 (priv)->variant->regs[reg])
82 #define SPI_BIT(priv, bit) ((priv)->variant->bits[bit])
83 #define SPI_CS(priv, cs) (((cs) << SPI_BIT(priv, SPI_TCR_CS_SEL)) & \
84 SPI_BIT(priv, SPI_TCR_CS_MASK))
86 /* sun spi register set */
100 /* sun spi register bits */
101 enum sun4i_spi_bits {
106 SPI_TCR_CS_ACTIVE_LOW,
117 struct sun4i_spi_variant {
118 const unsigned long *regs;
125 struct sun4i_spi_plat {
126 struct sun4i_spi_variant *variant;
131 struct sun4i_spi_priv {
132 struct sun4i_spi_variant *variant;
133 struct clk clk_ahb, clk_mod;
134 struct reset_ctl reset;
143 static inline void sun4i_spi_drain_fifo(struct sun4i_spi_priv *priv, int len)
148 byte = readb(SPI_REG(priv, SPI_RXD));
150 *priv->rx_buf++ = byte;
154 static inline void sun4i_spi_fill_fifo(struct sun4i_spi_priv *priv, int len)
159 byte = priv->tx_buf ? *priv->tx_buf++ : 0;
160 writeb(byte, SPI_REG(priv, SPI_TXD));
164 static void sun4i_spi_set_cs(struct udevice *bus, u8 cs, bool enable)
166 struct sun4i_spi_priv *priv = dev_get_priv(bus);
169 reg = readl(SPI_REG(priv, SPI_TCR));
171 reg &= ~SPI_BIT(priv, SPI_TCR_CS_MASK);
172 reg |= SPI_CS(priv, cs);
175 reg &= ~SPI_BIT(priv, SPI_TCR_CS_LEVEL);
177 reg |= SPI_BIT(priv, SPI_TCR_CS_LEVEL);
179 writel(reg, SPI_REG(priv, SPI_TCR));
182 static inline int sun4i_spi_set_clock(struct udevice *dev, bool enable)
184 struct sun4i_spi_priv *priv = dev_get_priv(dev);
188 clk_disable(&priv->clk_ahb);
189 clk_disable(&priv->clk_mod);
190 if (reset_valid(&priv->reset))
191 reset_assert(&priv->reset);
195 ret = clk_enable(&priv->clk_ahb);
197 dev_err(dev, "failed to enable ahb clock (ret=%d)\n", ret);
201 ret = clk_enable(&priv->clk_mod);
203 dev_err(dev, "failed to enable mod clock (ret=%d)\n", ret);
207 if (reset_valid(&priv->reset)) {
208 ret = reset_deassert(&priv->reset);
210 dev_err(dev, "failed to deassert reset\n");
218 clk_disable(&priv->clk_mod);
220 clk_disable(&priv->clk_ahb);
224 static void sun4i_spi_set_speed_mode(struct udevice *dev)
226 struct sun4i_spi_priv *priv = dev_get_priv(dev);
231 * Setup clock divider.
233 * We have two choices there. Either we can use the clock
234 * divide rate 1, which is calculated thanks to this formula:
235 * SPI_CLK = MOD_CLK / (2 ^ (cdr + 1))
236 * Or we can use CDR2, which is calculated with the formula:
237 * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
238 * Whether we use the former or the latter is set through the
241 * First try CDR2, and if we can't reach the expected
242 * frequency, fall back to CDR1.
245 div = SUN4I_SPI_MAX_RATE / (2 * priv->freq);
246 reg = readl(SPI_REG(priv, SPI_CCR));
248 if (div <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
252 reg &= ~(SUN4I_CLK_CTL_CDR2_MASK | SUN4I_CLK_CTL_DRS);
253 reg |= SUN4I_CLK_CTL_CDR2(div) | SUN4I_CLK_CTL_DRS;
255 div = __ilog2(SUN4I_SPI_MAX_RATE) - __ilog2(priv->freq);
256 reg &= ~((SUN4I_CLK_CTL_CDR1_MASK << 8) | SUN4I_CLK_CTL_DRS);
257 reg |= SUN4I_CLK_CTL_CDR1(div);
260 writel(reg, SPI_REG(priv, SPI_CCR));
262 reg = readl(SPI_REG(priv, SPI_TCR));
263 reg &= ~(SPI_BIT(priv, SPI_TCR_CPOL) | SPI_BIT(priv, SPI_TCR_CPHA));
265 if (priv->mode & SPI_CPOL)
266 reg |= SPI_BIT(priv, SPI_TCR_CPOL);
268 if (priv->mode & SPI_CPHA)
269 reg |= SPI_BIT(priv, SPI_TCR_CPHA);
271 writel(reg, SPI_REG(priv, SPI_TCR));
274 static int sun4i_spi_claim_bus(struct udevice *dev)
276 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
279 ret = sun4i_spi_set_clock(dev->parent, true);
283 setbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE |
284 SUN4I_CTL_MASTER | SPI_BIT(priv, SPI_GCR_TP));
286 if (priv->variant->has_soft_reset)
287 setbits_le32(SPI_REG(priv, SPI_GCR),
288 SPI_BIT(priv, SPI_GCR_SRST));
290 setbits_le32(SPI_REG(priv, SPI_TCR), SPI_BIT(priv, SPI_TCR_CS_MANUAL) |
291 SPI_BIT(priv, SPI_TCR_CS_ACTIVE_LOW));
293 sun4i_spi_set_speed_mode(dev->parent);
298 static int sun4i_spi_release_bus(struct udevice *dev)
300 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
302 clrbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE);
304 sun4i_spi_set_clock(dev->parent, false);
309 static int sun4i_spi_xfer(struct udevice *dev, unsigned int bitlen,
310 const void *dout, void *din, unsigned long flags)
312 struct udevice *bus = dev->parent;
313 struct sun4i_spi_priv *priv = dev_get_priv(bus);
314 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
316 u32 len = bitlen / 8;
324 debug("%s: non byte-aligned SPI transfer.\n", __func__);
328 if (flags & SPI_XFER_BEGIN)
329 sun4i_spi_set_cs(bus, slave_plat->cs, true);
332 setbits_le32(SPI_REG(priv, SPI_FCR), SPI_BIT(priv, SPI_FCR_RF_RST) |
333 SPI_BIT(priv, SPI_FCR_TF_RST));
336 /* Setup the transfer now... */
337 nbytes = min(len, (priv->variant->fifo_depth - 1));
339 /* Setup the counters */
340 writel(SUN4I_BURST_CNT(nbytes), SPI_REG(priv, SPI_BC));
341 writel(SUN4I_XMIT_CNT(nbytes), SPI_REG(priv, SPI_TC));
343 if (priv->variant->has_burst_ctl)
344 writel(SUN4I_BURST_CNT(nbytes),
345 SPI_REG(priv, SPI_BCTL));
347 /* Fill the TX FIFO */
348 sun4i_spi_fill_fifo(priv, nbytes);
350 /* Start the transfer */
351 setbits_le32(SPI_REG(priv, SPI_TCR),
352 SPI_BIT(priv, SPI_TCR_XCH));
354 /* Wait for the transfer to be done */
355 ret = wait_for_bit_le32((const void *)SPI_REG(priv, SPI_TCR),
356 SPI_BIT(priv, SPI_TCR_XCH),
357 false, SUN4I_SPI_TIMEOUT_MS, false);
359 printf("ERROR: sun4i_spi: Timeout transferring data\n");
360 sun4i_spi_set_cs(bus, slave_plat->cs, false);
364 /* Drain the RX FIFO */
365 sun4i_spi_drain_fifo(priv, nbytes);
370 if (flags & SPI_XFER_END)
371 sun4i_spi_set_cs(bus, slave_plat->cs, false);
376 static int sun4i_spi_set_speed(struct udevice *dev, uint speed)
378 struct sun4i_spi_plat *plat = dev_get_plat(dev);
379 struct sun4i_spi_priv *priv = dev_get_priv(dev);
381 if (speed > plat->max_hz)
382 speed = plat->max_hz;
384 if (speed < SUN4I_SPI_MIN_RATE)
385 speed = SUN4I_SPI_MIN_RATE;
392 static int sun4i_spi_set_mode(struct udevice *dev, uint mode)
394 struct sun4i_spi_priv *priv = dev_get_priv(dev);
401 static const struct dm_spi_ops sun4i_spi_ops = {
402 .claim_bus = sun4i_spi_claim_bus,
403 .release_bus = sun4i_spi_release_bus,
404 .xfer = sun4i_spi_xfer,
405 .set_speed = sun4i_spi_set_speed,
406 .set_mode = sun4i_spi_set_mode,
409 static int sun4i_spi_probe(struct udevice *bus)
411 struct sun4i_spi_plat *plat = dev_get_plat(bus);
412 struct sun4i_spi_priv *priv = dev_get_priv(bus);
415 ret = clk_get_by_name(bus, "ahb", &priv->clk_ahb);
417 dev_err(bus, "failed to get ahb clock\n");
421 ret = clk_get_by_name(bus, "mod", &priv->clk_mod);
423 dev_err(bus, "failed to get mod clock\n");
427 ret = reset_get_by_index(bus, 0, &priv->reset);
428 if (ret && ret != -ENOENT) {
429 dev_err(bus, "failed to get reset\n");
433 priv->variant = plat->variant;
434 priv->base = plat->base;
435 priv->freq = plat->max_hz;
440 static int sun4i_spi_of_to_plat(struct udevice *bus)
442 struct sun4i_spi_plat *plat = dev_get_plat(bus);
443 int node = dev_of_offset(bus);
445 plat->base = dev_read_addr(bus);
446 plat->variant = (struct sun4i_spi_variant *)dev_get_driver_data(bus);
447 plat->max_hz = fdtdec_get_int(gd->fdt_blob, node,
449 SUN4I_SPI_DEFAULT_RATE);
451 if (plat->max_hz > SUN4I_SPI_MAX_RATE)
452 plat->max_hz = SUN4I_SPI_MAX_RATE;
457 static const unsigned long sun4i_spi_regs[] = {
458 [SPI_GCR] = SUN4I_CTL_REG,
459 [SPI_TCR] = SUN4I_CTL_REG,
460 [SPI_FCR] = SUN4I_CTL_REG,
461 [SPI_FSR] = SUN4I_FIFO_STA_REG,
462 [SPI_CCR] = SUN4I_CLK_CTL_REG,
463 [SPI_BC] = SUN4I_BURST_CNT_REG,
464 [SPI_TC] = SUN4I_XMIT_CNT_REG,
465 [SPI_TXD] = SUN4I_TXDATA_REG,
466 [SPI_RXD] = SUN4I_RXDATA_REG,
469 static const u32 sun4i_spi_bits[] = {
470 [SPI_GCR_TP] = BIT(18),
471 [SPI_TCR_CPHA] = BIT(2),
472 [SPI_TCR_CPOL] = BIT(3),
473 [SPI_TCR_CS_ACTIVE_LOW] = BIT(4),
474 [SPI_TCR_XCH] = BIT(10),
475 [SPI_TCR_CS_SEL] = 12,
476 [SPI_TCR_CS_MASK] = 0x3000,
477 [SPI_TCR_CS_MANUAL] = BIT(16),
478 [SPI_TCR_CS_LEVEL] = BIT(17),
479 [SPI_FCR_TF_RST] = BIT(8),
480 [SPI_FCR_RF_RST] = BIT(9),
481 [SPI_FSR_RF_CNT_MASK] = GENMASK(6, 0),
484 static const unsigned long sun6i_spi_regs[] = {
485 [SPI_GCR] = SUN6I_GBL_CTL_REG,
486 [SPI_TCR] = SUN6I_TFR_CTL_REG,
487 [SPI_FCR] = SUN6I_FIFO_CTL_REG,
488 [SPI_FSR] = SUN6I_FIFO_STA_REG,
489 [SPI_CCR] = SUN6I_CLK_CTL_REG,
490 [SPI_BC] = SUN6I_BURST_CNT_REG,
491 [SPI_TC] = SUN6I_XMIT_CNT_REG,
492 [SPI_BCTL] = SUN6I_BURST_CTL_REG,
493 [SPI_TXD] = SUN6I_TXDATA_REG,
494 [SPI_RXD] = SUN6I_RXDATA_REG,
497 static const u32 sun6i_spi_bits[] = {
498 [SPI_GCR_TP] = BIT(7),
499 [SPI_GCR_SRST] = BIT(31),
500 [SPI_TCR_CPHA] = BIT(0),
501 [SPI_TCR_CPOL] = BIT(1),
502 [SPI_TCR_CS_ACTIVE_LOW] = BIT(2),
503 [SPI_TCR_CS_SEL] = 4,
504 [SPI_TCR_CS_MASK] = 0x30,
505 [SPI_TCR_CS_MANUAL] = BIT(6),
506 [SPI_TCR_CS_LEVEL] = BIT(7),
507 [SPI_TCR_XCH] = BIT(31),
508 [SPI_FCR_RF_RST] = BIT(15),
509 [SPI_FCR_TF_RST] = BIT(31),
510 [SPI_FSR_RF_CNT_MASK] = GENMASK(7, 0),
513 static const struct sun4i_spi_variant sun4i_a10_spi_variant = {
514 .regs = sun4i_spi_regs,
515 .bits = sun4i_spi_bits,
519 static const struct sun4i_spi_variant sun6i_a31_spi_variant = {
520 .regs = sun6i_spi_regs,
521 .bits = sun6i_spi_bits,
523 .has_soft_reset = true,
524 .has_burst_ctl = true,
527 static const struct sun4i_spi_variant sun8i_h3_spi_variant = {
528 .regs = sun6i_spi_regs,
529 .bits = sun6i_spi_bits,
531 .has_soft_reset = true,
532 .has_burst_ctl = true,
535 static const struct udevice_id sun4i_spi_ids[] = {
537 .compatible = "allwinner,sun4i-a10-spi",
538 .data = (ulong)&sun4i_a10_spi_variant,
541 .compatible = "allwinner,sun6i-a31-spi",
542 .data = (ulong)&sun6i_a31_spi_variant,
545 .compatible = "allwinner,sun8i-h3-spi",
546 .data = (ulong)&sun8i_h3_spi_variant,
551 U_BOOT_DRIVER(sun4i_spi) = {
554 .of_match = sun4i_spi_ids,
555 .ops = &sun4i_spi_ops,
556 .of_to_plat = sun4i_spi_of_to_plat,
557 .plat_auto = sizeof(struct sun4i_spi_plat),
558 .priv_auto = sizeof(struct sun4i_spi_priv),
559 .probe = sun4i_spi_probe,