2 * (C) Copyright 2017 Whitebox Systems / Northend Systems B.V.
6 * (C) Copyright 2017 Olimex Ltd..
9 * Based on linux spi driver. Original copyright follows:
10 * linux/drivers/spi/spi-sun4i.c
12 * Copyright (C) 2012 - 2014 Allwinner Tech
15 * Copyright (C) 2014 Maxime Ripard
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 #ifdef CONFIG_MACH_SUNIV
76 /* the AHB clock, which we programmed to be 1/3 of PLL_PERIPH@600MHz */
77 #define SUNXI_INPUT_CLOCK 200000000 /* 200 MHz */
78 #define SUN4I_SPI_MAX_RATE (SUNXI_INPUT_CLOCK / 2)
80 /* the SPI mod clock, defaulting to be 1/1 of the HOSC@24MHz */
81 #define SUNXI_INPUT_CLOCK 24000000 /* 24 MHz */
82 #define SUN4I_SPI_MAX_RATE SUNXI_INPUT_CLOCK
84 #define SUN4I_SPI_MIN_RATE 3000
85 #define SUN4I_SPI_DEFAULT_RATE 1000000
86 #define SUN4I_SPI_TIMEOUT_MS 1000
88 #define SPI_REG(priv, reg) ((priv)->base + \
89 (priv)->variant->regs[reg])
90 #define SPI_BIT(priv, bit) ((priv)->variant->bits[bit])
91 #define SPI_CS(priv, cs) (((cs) << SPI_BIT(priv, SPI_TCR_CS_SEL)) & \
92 SPI_BIT(priv, SPI_TCR_CS_MASK))
94 /* sun spi register set */
108 /* sun spi register bits */
109 enum sun4i_spi_bits {
114 SPI_TCR_CS_ACTIVE_LOW,
125 struct sun4i_spi_variant {
126 const unsigned long *regs;
133 struct sun4i_spi_plat {
134 struct sun4i_spi_variant *variant;
139 struct sun4i_spi_priv {
140 struct sun4i_spi_variant *variant;
141 struct clk clk_ahb, clk_mod;
142 struct reset_ctl reset;
151 static inline void sun4i_spi_drain_fifo(struct sun4i_spi_priv *priv, int len)
156 byte = readb(SPI_REG(priv, SPI_RXD));
158 *priv->rx_buf++ = byte;
162 static inline void sun4i_spi_fill_fifo(struct sun4i_spi_priv *priv, int len)
167 byte = priv->tx_buf ? *priv->tx_buf++ : 0;
168 writeb(byte, SPI_REG(priv, SPI_TXD));
172 static void sun4i_spi_set_cs(struct udevice *bus, u8 cs, bool enable)
174 struct sun4i_spi_priv *priv = dev_get_priv(bus);
177 reg = readl(SPI_REG(priv, SPI_TCR));
179 reg &= ~SPI_BIT(priv, SPI_TCR_CS_MASK);
180 reg |= SPI_CS(priv, cs);
183 reg &= ~SPI_BIT(priv, SPI_TCR_CS_LEVEL);
185 reg |= SPI_BIT(priv, SPI_TCR_CS_LEVEL);
187 writel(reg, SPI_REG(priv, SPI_TCR));
190 static inline int sun4i_spi_set_clock(struct udevice *dev, bool enable)
192 struct sun4i_spi_priv *priv = dev_get_priv(dev);
196 clk_disable(&priv->clk_ahb);
197 clk_disable(&priv->clk_mod);
198 if (reset_valid(&priv->reset))
199 reset_assert(&priv->reset);
203 ret = clk_enable(&priv->clk_ahb);
205 dev_err(dev, "failed to enable ahb clock (ret=%d)\n", ret);
209 ret = clk_enable(&priv->clk_mod);
211 dev_err(dev, "failed to enable mod clock (ret=%d)\n", ret);
215 if (reset_valid(&priv->reset)) {
216 ret = reset_deassert(&priv->reset);
218 dev_err(dev, "failed to deassert reset\n");
226 clk_disable(&priv->clk_mod);
228 clk_disable(&priv->clk_ahb);
232 static void sun4i_spi_set_speed_mode(struct udevice *dev)
234 struct sun4i_spi_priv *priv = dev_get_priv(dev);
239 * Setup clock divider.
241 * We have two choices there. Either we can use the clock
242 * divide rate 1, which is calculated thanks to this formula:
243 * SPI_CLK = MOD_CLK / (2 ^ (cdr + 1))
244 * Or we can use CDR2, which is calculated with the formula:
245 * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
246 * Whether we use the former or the latter is set through the
249 * First try CDR2, and if we can't reach the expected
250 * frequency, fall back to CDR1.
253 div = DIV_ROUND_UP(SUNXI_INPUT_CLOCK, priv->freq);
254 reg = readl(SPI_REG(priv, SPI_CCR));
256 if ((div / 2) <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
261 reg &= ~(SUN4I_CLK_CTL_CDR2_MASK | SUN4I_CLK_CTL_DRS);
262 reg |= SUN4I_CLK_CTL_CDR2(div) | SUN4I_CLK_CTL_DRS;
265 /* The F1C100s encodes the divider as 2^(n+1) */
266 if (IS_ENABLED(CONFIG_MACH_SUNIV))
268 reg &= ~((SUN4I_CLK_CTL_CDR1_MASK << 8) | SUN4I_CLK_CTL_DRS);
269 reg |= SUN4I_CLK_CTL_CDR1(div);
272 writel(reg, SPI_REG(priv, SPI_CCR));
274 reg = readl(SPI_REG(priv, SPI_TCR));
275 reg &= ~(SPI_BIT(priv, SPI_TCR_CPOL) | SPI_BIT(priv, SPI_TCR_CPHA));
277 if (priv->mode & SPI_CPOL)
278 reg |= SPI_BIT(priv, SPI_TCR_CPOL);
280 if (priv->mode & SPI_CPHA)
281 reg |= SPI_BIT(priv, SPI_TCR_CPHA);
283 writel(reg, SPI_REG(priv, SPI_TCR));
286 static int sun4i_spi_claim_bus(struct udevice *dev)
288 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
291 ret = sun4i_spi_set_clock(dev->parent, true);
295 setbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE |
296 SUN4I_CTL_MASTER | SPI_BIT(priv, SPI_GCR_TP));
298 if (priv->variant->has_soft_reset)
299 setbits_le32(SPI_REG(priv, SPI_GCR),
300 SPI_BIT(priv, SPI_GCR_SRST));
302 setbits_le32(SPI_REG(priv, SPI_TCR), SPI_BIT(priv, SPI_TCR_CS_MANUAL) |
303 SPI_BIT(priv, SPI_TCR_CS_ACTIVE_LOW));
305 sun4i_spi_set_speed_mode(dev->parent);
310 static int sun4i_spi_release_bus(struct udevice *dev)
312 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
314 clrbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE);
316 sun4i_spi_set_clock(dev->parent, false);
321 static int sun4i_spi_xfer(struct udevice *dev, unsigned int bitlen,
322 const void *dout, void *din, unsigned long flags)
324 struct udevice *bus = dev->parent;
325 struct sun4i_spi_priv *priv = dev_get_priv(bus);
326 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
328 u32 len = bitlen / 8;
336 debug("%s: non byte-aligned SPI transfer.\n", __func__);
340 if (flags & SPI_XFER_BEGIN)
341 sun4i_spi_set_cs(bus, slave_plat->cs, true);
344 setbits_le32(SPI_REG(priv, SPI_FCR), SPI_BIT(priv, SPI_FCR_RF_RST) |
345 SPI_BIT(priv, SPI_FCR_TF_RST));
348 /* Setup the transfer now... */
349 nbytes = min(len, (priv->variant->fifo_depth - 1));
351 /* Setup the counters */
352 writel(SUN4I_BURST_CNT(nbytes), SPI_REG(priv, SPI_BC));
353 writel(SUN4I_XMIT_CNT(nbytes), SPI_REG(priv, SPI_TC));
355 if (priv->variant->has_burst_ctl)
356 writel(SUN4I_BURST_CNT(nbytes),
357 SPI_REG(priv, SPI_BCTL));
359 /* Fill the TX FIFO */
360 sun4i_spi_fill_fifo(priv, nbytes);
362 /* Start the transfer */
363 setbits_le32(SPI_REG(priv, SPI_TCR),
364 SPI_BIT(priv, SPI_TCR_XCH));
366 /* Wait for the transfer to be done */
367 ret = wait_for_bit_le32((const void *)SPI_REG(priv, SPI_TCR),
368 SPI_BIT(priv, SPI_TCR_XCH),
369 false, SUN4I_SPI_TIMEOUT_MS, false);
371 printf("ERROR: sun4i_spi: Timeout transferring data\n");
372 sun4i_spi_set_cs(bus, slave_plat->cs, false);
376 /* Drain the RX FIFO */
377 sun4i_spi_drain_fifo(priv, nbytes);
382 if (flags & SPI_XFER_END)
383 sun4i_spi_set_cs(bus, slave_plat->cs, false);
388 static int sun4i_spi_set_speed(struct udevice *dev, uint speed)
390 struct sun4i_spi_plat *plat = dev_get_plat(dev);
391 struct sun4i_spi_priv *priv = dev_get_priv(dev);
393 if (speed > plat->max_hz)
394 speed = plat->max_hz;
396 if (speed < SUN4I_SPI_MIN_RATE)
397 speed = SUN4I_SPI_MIN_RATE;
404 static int sun4i_spi_set_mode(struct udevice *dev, uint mode)
406 struct sun4i_spi_priv *priv = dev_get_priv(dev);
413 static const struct dm_spi_ops sun4i_spi_ops = {
414 .claim_bus = sun4i_spi_claim_bus,
415 .release_bus = sun4i_spi_release_bus,
416 .xfer = sun4i_spi_xfer,
417 .set_speed = sun4i_spi_set_speed,
418 .set_mode = sun4i_spi_set_mode,
421 static int sun4i_spi_probe(struct udevice *bus)
423 struct sun4i_spi_plat *plat = dev_get_plat(bus);
424 struct sun4i_spi_priv *priv = dev_get_priv(bus);
427 ret = clk_get_by_name(bus, "ahb", &priv->clk_ahb);
429 dev_err(bus, "failed to get ahb clock\n");
433 ret = clk_get_by_name(bus, "mod", &priv->clk_mod);
435 dev_err(bus, "failed to get mod clock\n");
439 ret = reset_get_by_index(bus, 0, &priv->reset);
440 if (ret && ret != -ENOENT) {
441 dev_err(bus, "failed to get reset\n");
445 priv->variant = plat->variant;
446 priv->base = plat->base;
447 priv->freq = plat->max_hz;
452 static int sun4i_spi_of_to_plat(struct udevice *bus)
454 struct sun4i_spi_plat *plat = dev_get_plat(bus);
455 int node = dev_of_offset(bus);
457 plat->base = dev_read_addr(bus);
458 plat->variant = (struct sun4i_spi_variant *)dev_get_driver_data(bus);
459 plat->max_hz = fdtdec_get_int(gd->fdt_blob, node,
461 SUN4I_SPI_DEFAULT_RATE);
463 if (plat->max_hz > SUN4I_SPI_MAX_RATE)
464 plat->max_hz = SUN4I_SPI_MAX_RATE;
469 static const unsigned long sun4i_spi_regs[] = {
470 [SPI_GCR] = SUN4I_CTL_REG,
471 [SPI_TCR] = SUN4I_CTL_REG,
472 [SPI_FCR] = SUN4I_CTL_REG,
473 [SPI_FSR] = SUN4I_FIFO_STA_REG,
474 [SPI_CCR] = SUN4I_CLK_CTL_REG,
475 [SPI_BC] = SUN4I_BURST_CNT_REG,
476 [SPI_TC] = SUN4I_XMIT_CNT_REG,
477 [SPI_TXD] = SUN4I_TXDATA_REG,
478 [SPI_RXD] = SUN4I_RXDATA_REG,
481 static const u32 sun4i_spi_bits[] = {
482 [SPI_GCR_TP] = BIT(18),
483 [SPI_TCR_CPHA] = BIT(2),
484 [SPI_TCR_CPOL] = BIT(3),
485 [SPI_TCR_CS_ACTIVE_LOW] = BIT(4),
486 [SPI_TCR_XCH] = BIT(10),
487 [SPI_TCR_CS_SEL] = 12,
488 [SPI_TCR_CS_MASK] = 0x3000,
489 [SPI_TCR_CS_MANUAL] = BIT(16),
490 [SPI_TCR_CS_LEVEL] = BIT(17),
491 [SPI_FCR_TF_RST] = BIT(8),
492 [SPI_FCR_RF_RST] = BIT(9),
493 [SPI_FSR_RF_CNT_MASK] = GENMASK(6, 0),
496 static const unsigned long sun6i_spi_regs[] = {
497 [SPI_GCR] = SUN6I_GBL_CTL_REG,
498 [SPI_TCR] = SUN6I_TFR_CTL_REG,
499 [SPI_FCR] = SUN6I_FIFO_CTL_REG,
500 [SPI_FSR] = SUN6I_FIFO_STA_REG,
501 [SPI_CCR] = SUN6I_CLK_CTL_REG,
502 [SPI_BC] = SUN6I_BURST_CNT_REG,
503 [SPI_TC] = SUN6I_XMIT_CNT_REG,
504 [SPI_BCTL] = SUN6I_BURST_CTL_REG,
505 [SPI_TXD] = SUN6I_TXDATA_REG,
506 [SPI_RXD] = SUN6I_RXDATA_REG,
509 static const u32 sun6i_spi_bits[] = {
510 [SPI_GCR_TP] = BIT(7),
511 [SPI_GCR_SRST] = BIT(31),
512 [SPI_TCR_CPHA] = BIT(0),
513 [SPI_TCR_CPOL] = BIT(1),
514 [SPI_TCR_CS_ACTIVE_LOW] = BIT(2),
515 [SPI_TCR_CS_SEL] = 4,
516 [SPI_TCR_CS_MASK] = 0x30,
517 [SPI_TCR_CS_MANUAL] = BIT(6),
518 [SPI_TCR_CS_LEVEL] = BIT(7),
519 [SPI_TCR_XCH] = BIT(31),
520 [SPI_FCR_RF_RST] = BIT(15),
521 [SPI_FCR_TF_RST] = BIT(31),
522 [SPI_FSR_RF_CNT_MASK] = GENMASK(7, 0),
525 static const struct sun4i_spi_variant sun4i_a10_spi_variant = {
526 .regs = sun4i_spi_regs,
527 .bits = sun4i_spi_bits,
531 static const struct sun4i_spi_variant sun6i_a31_spi_variant = {
532 .regs = sun6i_spi_regs,
533 .bits = sun6i_spi_bits,
535 .has_soft_reset = true,
536 .has_burst_ctl = true,
539 static const struct sun4i_spi_variant sun8i_h3_spi_variant = {
540 .regs = sun6i_spi_regs,
541 .bits = sun6i_spi_bits,
543 .has_soft_reset = true,
544 .has_burst_ctl = true,
547 static const struct udevice_id sun4i_spi_ids[] = {
549 .compatible = "allwinner,sun4i-a10-spi",
550 .data = (ulong)&sun4i_a10_spi_variant,
553 .compatible = "allwinner,sun6i-a31-spi",
554 .data = (ulong)&sun6i_a31_spi_variant,
557 .compatible = "allwinner,sun8i-h3-spi",
558 .data = (ulong)&sun8i_h3_spi_variant,
563 U_BOOT_DRIVER(sun4i_spi) = {
566 .of_match = sun4i_spi_ids,
567 .ops = &sun4i_spi_ops,
568 .of_to_plat = sun4i_spi_of_to_plat,
569 .plat_auto = sizeof(struct sun4i_spi_plat),
570 .priv_auto = sizeof(struct sun4i_spi_priv),
571 .probe = sun4i_spi_probe,