1 // SPDX-License-Identifier: GPL-2.0+
3 * spi-synquacer.c - Socionext Synquacer SPI driver
4 * Copyright 2021 Linaro Ltd.
5 * Copyright 2021 Socionext, Inc.
13 #include <dm/device_compat.h>
14 #include <linux/bitfield.h>
15 #include <linux/bitops.h>
16 #include <linux/delay.h>
29 #define PCC(n) (PCC0 + (n) * 4)
40 #define CDRS_MASK 0x7f
72 #define RX_DATA_MASK 0x1f
73 #define RX_DATA_SHIFT 8
74 #define TX_DATA_MASK 0x1f
75 #define TX_DATA_SHIFT 16
84 #define RX_TRSHLD_MASK 0xf
85 #define RX_TRSHLD_SHIFT 0
86 #define TX_TRSHLD_MASK 0xf
87 #define TX_TRSHLD_SHIFT 4
95 #define RX_TRSHLD (FIFO_DEPTH - TX_TRSHLD)
100 DECLARE_GLOBAL_DATA_PTR;
102 struct synquacer_spi_plat {
107 struct synquacer_spi_priv {
110 int speed, cs, mode, rwflag;
113 unsigned int tx_words, rx_words;
116 static void read_fifo(struct synquacer_spi_priv *priv)
118 u32 len = readl(priv->base + DMSTATUS);
119 u8 *buf = priv->rx_buf;
122 len = (len >> RX_DATA_SHIFT) & RX_DATA_MASK;
123 len = min_t(unsigned int, len, priv->rx_words);
125 for (i = 0; i < len; i++)
126 *buf++ = readb(priv->base + RXFIFO);
129 priv->rx_words -= len;
132 static void write_fifo(struct synquacer_spi_priv *priv)
134 u32 len = readl(priv->base + DMSTATUS);
135 const u8 *buf = priv->tx_buf;
138 len = (len >> TX_DATA_SHIFT) & TX_DATA_MASK;
139 len = min_t(unsigned int, FIFO_DEPTH - len, priv->tx_words);
141 for (i = 0; i < len; i++)
142 writeb(*buf++, priv->base + TXFIFO);
145 priv->tx_words -= len;
148 static void synquacer_cs_set(struct synquacer_spi_priv *priv, bool active)
152 val = readl(priv->base + DMSTART);
153 val &= ~(CS_MASK << CS_SHIFT);
154 val |= priv->cs << CS_SHIFT;
157 writel(val, priv->base + DMSTART);
159 val = readl(priv->base + DMSTART);
161 writel(val, priv->base + DMSTART);
164 writel(val, priv->base + DMSTART);
176 static void synquacer_spi_config(struct udevice *dev, void *rx, const void *tx)
178 struct udevice *bus = dev->parent;
179 struct synquacer_spi_priv *priv = dev_get_priv(bus);
180 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
181 u32 val, div, bus_width;
184 rwflag = (rx ? 1 : 0) | (tx ? 2 : 0);
186 /* if nothing to do */
187 if (slave_plat->mode == priv->mode &&
188 rwflag == priv->rwflag &&
189 slave_plat->cs == priv->cs &&
190 slave_plat->max_hz == priv->speed)
193 priv->rwflag = rwflag;
194 priv->cs = slave_plat->cs;
195 priv->mode = slave_plat->mode;
196 priv->speed = slave_plat->max_hz;
198 if (priv->mode & SPI_TX_BYTE)
200 else if (priv->mode & SPI_TX_DUAL)
202 else if (priv->mode & SPI_TX_QUAD)
204 else if (priv->mode & SPI_TX_OCTAL)
207 div = DIV_ROUND_UP(125000000, priv->speed);
209 val = readl(priv->base + PCC(priv->cs));
212 val &= ~BIT(SAFESYNC);
213 if ((priv->mode & (SPI_TX_DUAL | SPI_RX_DUAL)) && div < 3)
214 val |= BIT(SAFESYNC);
215 if ((priv->mode & (SPI_TX_QUAD | SPI_RX_QUAD)) && div < 6)
216 val |= BIT(SAFESYNC);
218 if (priv->mode & SPI_CPHA)
223 if (priv->mode & SPI_CPOL)
228 if (priv->mode & SPI_CS_HIGH)
233 if (priv->mode & SPI_LSB_FIRST)
247 val &= ~(CDRS_MASK << CDRS_SHIFT);
248 val |= ((div >> 1) << CDRS_SHIFT);
250 writel(val, priv->base + PCC(priv->cs));
252 val = readl(priv->base + FIFOCFG);
253 val &= ~(BPW_MASK << BPW_SHIFT);
254 val |= (0 << BPW_SHIFT);
255 writel(val, priv->base + FIFOCFG);
257 val = readl(priv->base + DMSTART);
258 val &= ~(DATA_MASK << DATA_SHIFT);
261 val |= (DATA_TXRX << DATA_SHIFT);
263 val |= (DATA_RX << DATA_SHIFT);
265 val |= (DATA_TX << DATA_SHIFT);
267 val &= ~(3 << BUS_WIDTH);
268 val |= ((bus_width >> 1) << BUS_WIDTH);
269 writel(val, priv->base + DMSTART);
272 static int synquacer_spi_xfer(struct udevice *dev, unsigned int bitlen,
273 const void *tx_buf, void *rx_buf,
276 struct udevice *bus = dev->parent;
277 struct synquacer_spi_priv *priv = dev_get_priv(bus);
278 u32 val, words, busy;
280 val = readl(priv->base + FIFOCFG);
281 val |= (1 << RX_FLUSH);
282 val |= (1 << TX_FLUSH);
283 writel(val, priv->base + FIFOCFG);
285 synquacer_spi_config(dev, rx_buf, tx_buf);
287 priv->tx_buf = tx_buf;
288 priv->rx_buf = rx_buf;
294 priv->tx_words = words;
302 priv->rx_words = words;
308 if (flags & SPI_XFER_BEGIN)
309 synquacer_cs_set(priv, true);
315 val = readl(priv->base + FIFOCFG);
316 val &= ~(RX_TRSHLD_MASK << RX_TRSHLD_SHIFT);
317 val |= ((priv->rx_words > FIFO_DEPTH ?
318 RX_TRSHLD : priv->rx_words) << RX_TRSHLD_SHIFT);
319 writel(val, priv->base + FIFOCFG);
322 writel(~0, priv->base + TXC);
323 writel(~0, priv->base + RXC);
326 val = readl(priv->base + DMSTART);
328 writel(val, priv->base + DMSTART);
330 while (busy & (BIT(RXBIT) | BIT(TXBIT))) {
336 if (priv->tx_words) {
341 do { /* wait for shifter to empty out */
343 len = readl(priv->base + DMSTATUS);
344 len = (len >> TX_DATA_SHIFT) & TX_DATA_MASK;
345 } while (tx_buf && len);
350 if (flags & SPI_XFER_END)
351 synquacer_cs_set(priv, false);
356 static int synquacer_spi_set_speed(struct udevice *bus, uint speed)
361 static int synquacer_spi_set_mode(struct udevice *bus, uint mode)
366 static int synquacer_spi_claim_bus(struct udevice *dev)
371 static int synquacer_spi_release_bus(struct udevice *dev)
376 static void synquacer_spi_disable_module(struct synquacer_spi_priv *priv)
378 writel(0, priv->base + MCTRL);
379 while (readl(priv->base + MCTRL) & BIT(MES))
383 static void synquacer_spi_init(struct synquacer_spi_priv *priv)
387 synquacer_spi_disable_module(priv);
389 writel(0, priv->base + TXE);
390 writel(0, priv->base + RXE);
391 val = readl(priv->base + TXF);
392 writel(val, priv->base + TXC);
393 val = readl(priv->base + RXF);
394 writel(val, priv->base + RXC);
395 val = readl(priv->base + FAULTF);
396 writel(val, priv->base + FAULTC);
398 val = readl(priv->base + DMCFG);
400 val &= ~BIT(MSTARTEN);
401 writel(val, priv->base + DMCFG);
403 /* Enable module with direct mode */
404 val = readl(priv->base + MCTRL);
409 writel(val, priv->base + MCTRL);
412 static void synquacer_spi_exit(struct synquacer_spi_priv *priv)
416 synquacer_spi_disable_module(priv);
418 /* Enable module with command sequence mode */
419 val = readl(priv->base + MCTRL);
424 writel(val, priv->base + MCTRL);
426 while (!(readl(priv->base + MCTRL) & BIT(MES)))
430 static int synquacer_spi_probe(struct udevice *bus)
432 struct synquacer_spi_plat *plat = dev_get_plat(bus);
433 struct synquacer_spi_priv *priv = dev_get_priv(bus);
435 priv->base = plat->base;
436 priv->aces = plat->aces;
437 priv->rtm = plat->rtm;
439 synquacer_spi_init(priv);
443 static int synquacer_spi_remove(struct udevice *bus)
445 struct synquacer_spi_priv *priv = dev_get_priv(bus);
447 synquacer_spi_exit(priv);
451 static int synquacer_spi_of_to_plat(struct udevice *bus)
453 struct synquacer_spi_plat *plat = dev_get_plat(bus);
456 plat->base = dev_read_addr_ptr(bus);
458 plat->aces = dev_read_bool(bus, "socionext,set-aces");
459 plat->rtm = dev_read_bool(bus, "socionext,use-rtm");
461 clk_get_by_name(bus, "iHCLK", &clk);
467 static const struct dm_spi_ops synquacer_spi_ops = {
468 .claim_bus = synquacer_spi_claim_bus,
469 .release_bus = synquacer_spi_release_bus,
470 .xfer = synquacer_spi_xfer,
471 .set_speed = synquacer_spi_set_speed,
472 .set_mode = synquacer_spi_set_mode,
475 static const struct udevice_id synquacer_spi_ids[] = {
476 { .compatible = "socionext,synquacer-spi" },
480 U_BOOT_DRIVER(synquacer_spi) = {
481 .name = "synquacer_spi",
483 .of_match = synquacer_spi_ids,
484 .ops = &synquacer_spi_ops,
485 .of_to_plat = synquacer_spi_of_to_plat,
486 .plat_auto = sizeof(struct synquacer_spi_plat),
487 .priv_auto = sizeof(struct synquacer_spi_priv),
488 .probe = synquacer_spi_probe,
489 .flags = DM_FLAG_OS_PREPARE,
490 .remove = synquacer_spi_remove,