1 // SPDX-License-Identifier: GPL-2.0
2 // spi-uniphier.c - Socionext UniPhier SPI controller driver
3 // Copyright 2012 Panasonic Corporation
4 // Copyright 2016-2018 Socionext Inc.
6 #include <linux/kernel.h>
7 #include <linux/bitfield.h>
8 #include <linux/bitops.h>
10 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/spi/spi.h>
16 #include <asm/unaligned.h>
18 #define SSI_TIMEOUT_MS 2000
19 #define SSI_MAX_CLK_DIVIDER 254
20 #define SSI_MIN_CLK_DIVIDER 4
22 struct uniphier_spi_priv {
25 struct spi_master *master;
26 struct completion xfer_done;
29 unsigned int tx_bytes;
30 unsigned int rx_bytes;
41 #define SSI_CTL_EN BIT(0)
44 #define SSI_CKS_CKRAT_MASK GENMASK(7, 0)
45 #define SSI_CKS_CKPHS BIT(14)
46 #define SSI_CKS_CKINIT BIT(13)
47 #define SSI_CKS_CKDLY BIT(12)
49 #define SSI_TXWDS 0x08
50 #define SSI_TXWDS_WDLEN_MASK GENMASK(13, 8)
51 #define SSI_TXWDS_TDTF_MASK GENMASK(7, 6)
52 #define SSI_TXWDS_DTLEN_MASK GENMASK(5, 0)
54 #define SSI_RXWDS 0x0c
55 #define SSI_RXWDS_DTLEN_MASK GENMASK(5, 0)
58 #define SSI_FPS_FSPOL BIT(15)
59 #define SSI_FPS_FSTRT BIT(14)
62 #define SSI_SR_RNE BIT(0)
65 #define SSI_IE_RCIE BIT(3)
66 #define SSI_IE_RORIE BIT(0)
69 #define SSI_IS_RXRS BIT(9)
70 #define SSI_IS_RCID BIT(3)
71 #define SSI_IS_RORID BIT(0)
74 #define SSI_IC_TCIC BIT(4)
75 #define SSI_IC_RCIC BIT(3)
76 #define SSI_IC_RORIC BIT(0)
79 #define SSI_FC_TXFFL BIT(12)
80 #define SSI_FC_TXFTH_MASK GENMASK(11, 8)
81 #define SSI_FC_RXFFL BIT(4)
82 #define SSI_FC_RXFTH_MASK GENMASK(3, 0)
87 #define SSI_FIFO_DEPTH 8U
89 static inline unsigned int bytes_per_word(unsigned int bits)
91 return bits <= 8 ? 1 : (bits <= 16 ? 2 : 4);
94 static inline void uniphier_spi_irq_enable(struct spi_device *spi, u32 mask)
96 struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
99 val = readl(priv->base + SSI_IE);
101 writel(val, priv->base + SSI_IE);
104 static inline void uniphier_spi_irq_disable(struct spi_device *spi, u32 mask)
106 struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
109 val = readl(priv->base + SSI_IE);
111 writel(val, priv->base + SSI_IE);
114 static void uniphier_spi_set_mode(struct spi_device *spi)
116 struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
121 * CKPHS capture timing. 0:rising edge, 1:falling edge
122 * CKINIT clock initial level. 0:low, 1:high
123 * CKDLY clock delay. 0:no delay, 1:delay depending on FSTRT
124 * (FSTRT=0: 1 clock, FSTRT=1: 0.5 clock)
127 * FSPOL frame signal porarity. 0: low, 1: high
128 * FSTRT start frame timing
129 * 0: rising edge of clock, 1: falling edge of clock
131 switch (spi->mode & (SPI_CPOL | SPI_CPHA)) {
133 /* CKPHS=1, CKINIT=0, CKDLY=1, FSTRT=0 */
134 val1 = SSI_CKS_CKPHS | SSI_CKS_CKDLY;
138 /* CKPHS=0, CKINIT=0, CKDLY=0, FSTRT=1 */
140 val2 = SSI_FPS_FSTRT;
143 /* CKPHS=0, CKINIT=1, CKDLY=1, FSTRT=1 */
144 val1 = SSI_CKS_CKINIT | SSI_CKS_CKDLY;
145 val2 = SSI_FPS_FSTRT;
148 /* CKPHS=1, CKINIT=1, CKDLY=0, FSTRT=0 */
149 val1 = SSI_CKS_CKPHS | SSI_CKS_CKINIT;
154 if (!(spi->mode & SPI_CS_HIGH))
155 val2 |= SSI_FPS_FSPOL;
157 writel(val1, priv->base + SSI_CKS);
158 writel(val2, priv->base + SSI_FPS);
161 if (spi->mode & SPI_LSB_FIRST)
162 val1 |= FIELD_PREP(SSI_TXWDS_TDTF_MASK, 1);
163 writel(val1, priv->base + SSI_TXWDS);
164 writel(val1, priv->base + SSI_RXWDS);
167 static void uniphier_spi_set_transfer_size(struct spi_device *spi, int size)
169 struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
172 val = readl(priv->base + SSI_TXWDS);
173 val &= ~(SSI_TXWDS_WDLEN_MASK | SSI_TXWDS_DTLEN_MASK);
174 val |= FIELD_PREP(SSI_TXWDS_WDLEN_MASK, size);
175 val |= FIELD_PREP(SSI_TXWDS_DTLEN_MASK, size);
176 writel(val, priv->base + SSI_TXWDS);
178 val = readl(priv->base + SSI_RXWDS);
179 val &= ~SSI_RXWDS_DTLEN_MASK;
180 val |= FIELD_PREP(SSI_RXWDS_DTLEN_MASK, size);
181 writel(val, priv->base + SSI_RXWDS);
184 static void uniphier_spi_set_baudrate(struct spi_device *spi,
187 struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
191 * the supported rates are even numbers from 4 to 254. (4,6,8...254)
192 * round up as we look for equal or less speed
194 ckdiv = DIV_ROUND_UP(clk_get_rate(priv->clk), speed);
195 ckdiv = round_up(ckdiv, 2);
197 val = readl(priv->base + SSI_CKS);
198 val &= ~SSI_CKS_CKRAT_MASK;
199 val |= ckdiv & SSI_CKS_CKRAT_MASK;
200 writel(val, priv->base + SSI_CKS);
203 static void uniphier_spi_setup_transfer(struct spi_device *spi,
204 struct spi_transfer *t)
206 struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
210 priv->tx_buf = t->tx_buf;
211 priv->rx_buf = t->rx_buf;
212 priv->tx_bytes = priv->rx_bytes = t->len;
214 if (!priv->is_save_param || priv->mode != spi->mode) {
215 uniphier_spi_set_mode(spi);
216 priv->mode = spi->mode;
219 if (!priv->is_save_param || priv->bits_per_word != t->bits_per_word) {
220 uniphier_spi_set_transfer_size(spi, t->bits_per_word);
221 priv->bits_per_word = t->bits_per_word;
224 if (!priv->is_save_param || priv->speed_hz != t->speed_hz) {
225 uniphier_spi_set_baudrate(spi, t->speed_hz);
226 priv->speed_hz = t->speed_hz;
229 if (!priv->is_save_param)
230 priv->is_save_param = true;
233 val = SSI_FC_TXFFL | SSI_FC_RXFFL;
234 writel(val, priv->base + SSI_FC);
237 static void uniphier_spi_send(struct uniphier_spi_priv *priv)
242 wsize = min(bytes_per_word(priv->bits_per_word), priv->tx_bytes);
243 priv->tx_bytes -= wsize;
251 val = get_unaligned_le16(priv->tx_buf);
254 val = get_unaligned_le32(priv->tx_buf);
258 priv->tx_buf += wsize;
261 writel(val, priv->base + SSI_TXDR);
264 static void uniphier_spi_recv(struct uniphier_spi_priv *priv)
269 rsize = min(bytes_per_word(priv->bits_per_word), priv->rx_bytes);
270 priv->rx_bytes -= rsize;
272 val = readl(priv->base + SSI_RXDR);
280 put_unaligned_le16(val, priv->rx_buf);
283 put_unaligned_le32(val, priv->rx_buf);
287 priv->rx_buf += rsize;
291 static void uniphier_spi_fill_tx_fifo(struct uniphier_spi_priv *priv)
293 unsigned int tx_count;
296 tx_count = DIV_ROUND_UP(priv->tx_bytes,
297 bytes_per_word(priv->bits_per_word));
298 tx_count = min(tx_count, SSI_FIFO_DEPTH);
300 /* set fifo threshold */
301 val = readl(priv->base + SSI_FC);
302 val &= ~(SSI_FC_TXFTH_MASK | SSI_FC_RXFTH_MASK);
303 val |= FIELD_PREP(SSI_FC_TXFTH_MASK, tx_count);
304 val |= FIELD_PREP(SSI_FC_RXFTH_MASK, tx_count);
305 writel(val, priv->base + SSI_FC);
308 uniphier_spi_send(priv);
311 static void uniphier_spi_set_cs(struct spi_device *spi, bool enable)
313 struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
316 val = readl(priv->base + SSI_FPS);
319 val |= SSI_FPS_FSPOL;
321 val &= ~SSI_FPS_FSPOL;
323 writel(val, priv->base + SSI_FPS);
326 static int uniphier_spi_transfer_one(struct spi_master *master,
327 struct spi_device *spi,
328 struct spi_transfer *t)
330 struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
331 struct device *dev = master->dev.parent;
332 unsigned long time_left;
334 /* Terminate and return success for 0 byte length transfer */
338 uniphier_spi_setup_transfer(spi, t);
340 reinit_completion(&priv->xfer_done);
342 uniphier_spi_fill_tx_fifo(priv);
344 uniphier_spi_irq_enable(spi, SSI_IE_RCIE | SSI_IE_RORIE);
346 time_left = wait_for_completion_timeout(&priv->xfer_done,
347 msecs_to_jiffies(SSI_TIMEOUT_MS));
349 uniphier_spi_irq_disable(spi, SSI_IE_RCIE | SSI_IE_RORIE);
352 dev_err(dev, "transfer timeout.\n");
359 static int uniphier_spi_prepare_transfer_hardware(struct spi_master *master)
361 struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
363 writel(SSI_CTL_EN, priv->base + SSI_CTL);
368 static int uniphier_spi_unprepare_transfer_hardware(struct spi_master *master)
370 struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
372 writel(0, priv->base + SSI_CTL);
377 static irqreturn_t uniphier_spi_handler(int irq, void *dev_id)
379 struct uniphier_spi_priv *priv = dev_id;
382 stat = readl(priv->base + SSI_IS);
383 val = SSI_IC_TCIC | SSI_IC_RCIC | SSI_IC_RORIC;
384 writel(val, priv->base + SSI_IC);
386 /* rx fifo overrun */
387 if (stat & SSI_IS_RORID) {
393 if ((stat & SSI_IS_RCID) && (stat & SSI_IS_RXRS)) {
394 while ((readl(priv->base + SSI_SR) & SSI_SR_RNE) &&
395 (priv->rx_bytes - priv->tx_bytes) > 0)
396 uniphier_spi_recv(priv);
398 if ((readl(priv->base + SSI_SR) & SSI_SR_RNE) ||
399 (priv->rx_bytes != priv->tx_bytes)) {
402 } else if (priv->rx_bytes == 0)
405 /* next tx transfer */
406 uniphier_spi_fill_tx_fifo(priv);
414 complete(&priv->xfer_done);
418 static int uniphier_spi_probe(struct platform_device *pdev)
420 struct uniphier_spi_priv *priv;
421 struct spi_master *master;
422 struct resource *res;
423 unsigned long clk_rate;
427 master = spi_alloc_master(&pdev->dev, sizeof(*priv));
431 platform_set_drvdata(pdev, master);
433 priv = spi_master_get_devdata(master);
434 priv->master = master;
435 priv->is_save_param = false;
437 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
438 priv->base = devm_ioremap_resource(&pdev->dev, res);
439 if (IS_ERR(priv->base)) {
440 ret = PTR_ERR(priv->base);
444 priv->clk = devm_clk_get(&pdev->dev, NULL);
445 if (IS_ERR(priv->clk)) {
446 dev_err(&pdev->dev, "failed to get clock\n");
447 ret = PTR_ERR(priv->clk);
451 ret = clk_prepare_enable(priv->clk);
455 irq = platform_get_irq(pdev, 0);
457 dev_err(&pdev->dev, "failed to get IRQ\n");
459 goto out_disable_clk;
462 ret = devm_request_irq(&pdev->dev, irq, uniphier_spi_handler,
463 0, "uniphier-spi", priv);
465 dev_err(&pdev->dev, "failed to request IRQ\n");
466 goto out_disable_clk;
469 init_completion(&priv->xfer_done);
471 clk_rate = clk_get_rate(priv->clk);
473 master->max_speed_hz = DIV_ROUND_UP(clk_rate, SSI_MIN_CLK_DIVIDER);
474 master->min_speed_hz = DIV_ROUND_UP(clk_rate, SSI_MAX_CLK_DIVIDER);
475 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
476 master->dev.of_node = pdev->dev.of_node;
477 master->bus_num = pdev->id;
478 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
480 master->set_cs = uniphier_spi_set_cs;
481 master->transfer_one = uniphier_spi_transfer_one;
482 master->prepare_transfer_hardware
483 = uniphier_spi_prepare_transfer_hardware;
484 master->unprepare_transfer_hardware
485 = uniphier_spi_unprepare_transfer_hardware;
486 master->num_chipselect = 1;
488 ret = devm_spi_register_master(&pdev->dev, master);
490 goto out_disable_clk;
495 clk_disable_unprepare(priv->clk);
498 spi_master_put(master);
502 static int uniphier_spi_remove(struct platform_device *pdev)
504 struct uniphier_spi_priv *priv = platform_get_drvdata(pdev);
506 clk_disable_unprepare(priv->clk);
511 static const struct of_device_id uniphier_spi_match[] = {
512 { .compatible = "socionext,uniphier-scssi" },
515 MODULE_DEVICE_TABLE(of, uniphier_spi_match);
517 static struct platform_driver uniphier_spi_driver = {
518 .probe = uniphier_spi_probe,
519 .remove = uniphier_spi_remove,
521 .name = "uniphier-spi",
522 .of_match_table = uniphier_spi_match,
525 module_platform_driver(uniphier_spi_driver);
527 MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
528 MODULE_AUTHOR("Keiji Hayashibara <hayashibara.keiji@socionext.com>");
529 MODULE_DESCRIPTION("Socionext UniPhier SPI controller driver");
530 MODULE_LICENSE("GPL v2");