Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / spi / spi-uniphier.c
blob6a9ef8ee3cc9028f73c3bcf0a1f4ab3a8252373e
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>
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/dmaengine.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/spi/spi.h>
18 #include <asm/unaligned.h>
20 #define SSI_TIMEOUT_MS 2000
21 #define SSI_POLL_TIMEOUT_US 200
22 #define SSI_MAX_CLK_DIVIDER 254
23 #define SSI_MIN_CLK_DIVIDER 4
25 struct uniphier_spi_priv {
26 void __iomem *base;
27 dma_addr_t base_dma_addr;
28 struct clk *clk;
29 struct spi_master *master;
30 struct completion xfer_done;
32 int error;
33 unsigned int tx_bytes;
34 unsigned int rx_bytes;
35 const u8 *tx_buf;
36 u8 *rx_buf;
37 atomic_t dma_busy;
39 bool is_save_param;
40 u8 bits_per_word;
41 u16 mode;
42 u32 speed_hz;
45 #define SSI_CTL 0x00
46 #define SSI_CTL_EN BIT(0)
48 #define SSI_CKS 0x04
49 #define SSI_CKS_CKRAT_MASK GENMASK(7, 0)
50 #define SSI_CKS_CKPHS BIT(14)
51 #define SSI_CKS_CKINIT BIT(13)
52 #define SSI_CKS_CKDLY BIT(12)
54 #define SSI_TXWDS 0x08
55 #define SSI_TXWDS_WDLEN_MASK GENMASK(13, 8)
56 #define SSI_TXWDS_TDTF_MASK GENMASK(7, 6)
57 #define SSI_TXWDS_DTLEN_MASK GENMASK(5, 0)
59 #define SSI_RXWDS 0x0c
60 #define SSI_RXWDS_DTLEN_MASK GENMASK(5, 0)
62 #define SSI_FPS 0x10
63 #define SSI_FPS_FSPOL BIT(15)
64 #define SSI_FPS_FSTRT BIT(14)
66 #define SSI_SR 0x14
67 #define SSI_SR_BUSY BIT(7)
68 #define SSI_SR_RNE BIT(0)
70 #define SSI_IE 0x18
71 #define SSI_IE_TCIE BIT(4)
72 #define SSI_IE_RCIE BIT(3)
73 #define SSI_IE_TXRE BIT(2)
74 #define SSI_IE_RXRE BIT(1)
75 #define SSI_IE_RORIE BIT(0)
76 #define SSI_IE_ALL_MASK GENMASK(4, 0)
78 #define SSI_IS 0x1c
79 #define SSI_IS_RXRS BIT(9)
80 #define SSI_IS_RCID BIT(3)
81 #define SSI_IS_RORID BIT(0)
83 #define SSI_IC 0x1c
84 #define SSI_IC_TCIC BIT(4)
85 #define SSI_IC_RCIC BIT(3)
86 #define SSI_IC_RORIC BIT(0)
88 #define SSI_FC 0x20
89 #define SSI_FC_TXFFL BIT(12)
90 #define SSI_FC_TXFTH_MASK GENMASK(11, 8)
91 #define SSI_FC_RXFFL BIT(4)
92 #define SSI_FC_RXFTH_MASK GENMASK(3, 0)
94 #define SSI_TXDR 0x24
95 #define SSI_RXDR 0x24
97 #define SSI_FIFO_DEPTH 8U
98 #define SSI_FIFO_BURST_NUM 1
100 #define SSI_DMA_RX_BUSY BIT(1)
101 #define SSI_DMA_TX_BUSY BIT(0)
103 static inline unsigned int bytes_per_word(unsigned int bits)
105 return bits <= 8 ? 1 : (bits <= 16 ? 2 : 4);
108 static inline void uniphier_spi_irq_enable(struct uniphier_spi_priv *priv,
109 u32 mask)
111 u32 val;
113 val = readl(priv->base + SSI_IE);
114 val |= mask;
115 writel(val, priv->base + SSI_IE);
118 static inline void uniphier_spi_irq_disable(struct uniphier_spi_priv *priv,
119 u32 mask)
121 u32 val;
123 val = readl(priv->base + SSI_IE);
124 val &= ~mask;
125 writel(val, priv->base + SSI_IE);
128 static void uniphier_spi_set_mode(struct spi_device *spi)
130 struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
131 u32 val1, val2;
134 * clock setting
135 * CKPHS capture timing. 0:rising edge, 1:falling edge
136 * CKINIT clock initial level. 0:low, 1:high
137 * CKDLY clock delay. 0:no delay, 1:delay depending on FSTRT
138 * (FSTRT=0: 1 clock, FSTRT=1: 0.5 clock)
140 * frame setting
141 * FSPOL frame signal porarity. 0: low, 1: high
142 * FSTRT start frame timing
143 * 0: rising edge of clock, 1: falling edge of clock
145 switch (spi->mode & (SPI_CPOL | SPI_CPHA)) {
146 case SPI_MODE_0:
147 /* CKPHS=1, CKINIT=0, CKDLY=1, FSTRT=0 */
148 val1 = SSI_CKS_CKPHS | SSI_CKS_CKDLY;
149 val2 = 0;
150 break;
151 case SPI_MODE_1:
152 /* CKPHS=0, CKINIT=0, CKDLY=0, FSTRT=1 */
153 val1 = 0;
154 val2 = SSI_FPS_FSTRT;
155 break;
156 case SPI_MODE_2:
157 /* CKPHS=0, CKINIT=1, CKDLY=1, FSTRT=1 */
158 val1 = SSI_CKS_CKINIT | SSI_CKS_CKDLY;
159 val2 = SSI_FPS_FSTRT;
160 break;
161 case SPI_MODE_3:
162 /* CKPHS=1, CKINIT=1, CKDLY=0, FSTRT=0 */
163 val1 = SSI_CKS_CKPHS | SSI_CKS_CKINIT;
164 val2 = 0;
165 break;
168 if (!(spi->mode & SPI_CS_HIGH))
169 val2 |= SSI_FPS_FSPOL;
171 writel(val1, priv->base + SSI_CKS);
172 writel(val2, priv->base + SSI_FPS);
174 val1 = 0;
175 if (spi->mode & SPI_LSB_FIRST)
176 val1 |= FIELD_PREP(SSI_TXWDS_TDTF_MASK, 1);
177 writel(val1, priv->base + SSI_TXWDS);
178 writel(val1, priv->base + SSI_RXWDS);
181 static void uniphier_spi_set_transfer_size(struct spi_device *spi, int size)
183 struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
184 u32 val;
186 val = readl(priv->base + SSI_TXWDS);
187 val &= ~(SSI_TXWDS_WDLEN_MASK | SSI_TXWDS_DTLEN_MASK);
188 val |= FIELD_PREP(SSI_TXWDS_WDLEN_MASK, size);
189 val |= FIELD_PREP(SSI_TXWDS_DTLEN_MASK, size);
190 writel(val, priv->base + SSI_TXWDS);
192 val = readl(priv->base + SSI_RXWDS);
193 val &= ~SSI_RXWDS_DTLEN_MASK;
194 val |= FIELD_PREP(SSI_RXWDS_DTLEN_MASK, size);
195 writel(val, priv->base + SSI_RXWDS);
198 static void uniphier_spi_set_baudrate(struct spi_device *spi,
199 unsigned int speed)
201 struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
202 u32 val, ckdiv;
205 * the supported rates are even numbers from 4 to 254. (4,6,8...254)
206 * round up as we look for equal or less speed
208 ckdiv = DIV_ROUND_UP(clk_get_rate(priv->clk), speed);
209 ckdiv = round_up(ckdiv, 2);
211 val = readl(priv->base + SSI_CKS);
212 val &= ~SSI_CKS_CKRAT_MASK;
213 val |= ckdiv & SSI_CKS_CKRAT_MASK;
214 writel(val, priv->base + SSI_CKS);
217 static void uniphier_spi_setup_transfer(struct spi_device *spi,
218 struct spi_transfer *t)
220 struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
221 u32 val;
223 priv->error = 0;
224 priv->tx_buf = t->tx_buf;
225 priv->rx_buf = t->rx_buf;
226 priv->tx_bytes = priv->rx_bytes = t->len;
228 if (!priv->is_save_param || priv->mode != spi->mode) {
229 uniphier_spi_set_mode(spi);
230 priv->mode = spi->mode;
231 priv->is_save_param = false;
234 if (!priv->is_save_param || priv->bits_per_word != t->bits_per_word) {
235 uniphier_spi_set_transfer_size(spi, t->bits_per_word);
236 priv->bits_per_word = t->bits_per_word;
239 if (!priv->is_save_param || priv->speed_hz != t->speed_hz) {
240 uniphier_spi_set_baudrate(spi, t->speed_hz);
241 priv->speed_hz = t->speed_hz;
244 priv->is_save_param = true;
246 /* reset FIFOs */
247 val = SSI_FC_TXFFL | SSI_FC_RXFFL;
248 writel(val, priv->base + SSI_FC);
251 static void uniphier_spi_send(struct uniphier_spi_priv *priv)
253 int wsize;
254 u32 val = 0;
256 wsize = min(bytes_per_word(priv->bits_per_word), priv->tx_bytes);
257 priv->tx_bytes -= wsize;
259 if (priv->tx_buf) {
260 switch (wsize) {
261 case 1:
262 val = *priv->tx_buf;
263 break;
264 case 2:
265 val = get_unaligned_le16(priv->tx_buf);
266 break;
267 case 4:
268 val = get_unaligned_le32(priv->tx_buf);
269 break;
272 priv->tx_buf += wsize;
275 writel(val, priv->base + SSI_TXDR);
278 static void uniphier_spi_recv(struct uniphier_spi_priv *priv)
280 int rsize;
281 u32 val;
283 rsize = min(bytes_per_word(priv->bits_per_word), priv->rx_bytes);
284 priv->rx_bytes -= rsize;
286 val = readl(priv->base + SSI_RXDR);
288 if (priv->rx_buf) {
289 switch (rsize) {
290 case 1:
291 *priv->rx_buf = val;
292 break;
293 case 2:
294 put_unaligned_le16(val, priv->rx_buf);
295 break;
296 case 4:
297 put_unaligned_le32(val, priv->rx_buf);
298 break;
301 priv->rx_buf += rsize;
305 static void uniphier_spi_set_fifo_threshold(struct uniphier_spi_priv *priv,
306 unsigned int threshold)
308 u32 val;
310 val = readl(priv->base + SSI_FC);
311 val &= ~(SSI_FC_TXFTH_MASK | SSI_FC_RXFTH_MASK);
312 val |= FIELD_PREP(SSI_FC_TXFTH_MASK, SSI_FIFO_DEPTH - threshold);
313 val |= FIELD_PREP(SSI_FC_RXFTH_MASK, threshold);
314 writel(val, priv->base + SSI_FC);
317 static void uniphier_spi_fill_tx_fifo(struct uniphier_spi_priv *priv)
319 unsigned int fifo_threshold, fill_words;
320 unsigned int bpw = bytes_per_word(priv->bits_per_word);
322 fifo_threshold = DIV_ROUND_UP(priv->rx_bytes, bpw);
323 fifo_threshold = min(fifo_threshold, SSI_FIFO_DEPTH);
325 uniphier_spi_set_fifo_threshold(priv, fifo_threshold);
327 fill_words = fifo_threshold -
328 DIV_ROUND_UP(priv->rx_bytes - priv->tx_bytes, bpw);
330 while (fill_words--)
331 uniphier_spi_send(priv);
334 static void uniphier_spi_set_cs(struct spi_device *spi, bool enable)
336 struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
337 u32 val;
339 val = readl(priv->base + SSI_FPS);
341 if (enable)
342 val |= SSI_FPS_FSPOL;
343 else
344 val &= ~SSI_FPS_FSPOL;
346 writel(val, priv->base + SSI_FPS);
349 static bool uniphier_spi_can_dma(struct spi_master *master,
350 struct spi_device *spi,
351 struct spi_transfer *t)
353 struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
354 unsigned int bpw = bytes_per_word(priv->bits_per_word);
356 if ((!master->dma_tx && !master->dma_rx)
357 || (!master->dma_tx && t->tx_buf)
358 || (!master->dma_rx && t->rx_buf))
359 return false;
361 return DIV_ROUND_UP(t->len, bpw) > SSI_FIFO_DEPTH;
364 static void uniphier_spi_dma_rxcb(void *data)
366 struct spi_master *master = data;
367 struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
368 int state = atomic_fetch_andnot(SSI_DMA_RX_BUSY, &priv->dma_busy);
370 uniphier_spi_irq_disable(priv, SSI_IE_RXRE);
372 if (!(state & SSI_DMA_TX_BUSY))
373 spi_finalize_current_transfer(master);
376 static void uniphier_spi_dma_txcb(void *data)
378 struct spi_master *master = data;
379 struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
380 int state = atomic_fetch_andnot(SSI_DMA_TX_BUSY, &priv->dma_busy);
382 uniphier_spi_irq_disable(priv, SSI_IE_TXRE);
384 if (!(state & SSI_DMA_RX_BUSY))
385 spi_finalize_current_transfer(master);
388 static int uniphier_spi_transfer_one_dma(struct spi_master *master,
389 struct spi_device *spi,
390 struct spi_transfer *t)
392 struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
393 struct dma_async_tx_descriptor *rxdesc = NULL, *txdesc = NULL;
394 int buswidth;
396 atomic_set(&priv->dma_busy, 0);
398 uniphier_spi_set_fifo_threshold(priv, SSI_FIFO_BURST_NUM);
400 if (priv->bits_per_word <= 8)
401 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
402 else if (priv->bits_per_word <= 16)
403 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
404 else
405 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
407 if (priv->rx_buf) {
408 struct dma_slave_config rxconf = {
409 .direction = DMA_DEV_TO_MEM,
410 .src_addr = priv->base_dma_addr + SSI_RXDR,
411 .src_addr_width = buswidth,
412 .src_maxburst = SSI_FIFO_BURST_NUM,
415 dmaengine_slave_config(master->dma_rx, &rxconf);
417 rxdesc = dmaengine_prep_slave_sg(
418 master->dma_rx,
419 t->rx_sg.sgl, t->rx_sg.nents,
420 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
421 if (!rxdesc)
422 goto out_err_prep;
424 rxdesc->callback = uniphier_spi_dma_rxcb;
425 rxdesc->callback_param = master;
427 uniphier_spi_irq_enable(priv, SSI_IE_RXRE);
428 atomic_or(SSI_DMA_RX_BUSY, &priv->dma_busy);
430 dmaengine_submit(rxdesc);
431 dma_async_issue_pending(master->dma_rx);
434 if (priv->tx_buf) {
435 struct dma_slave_config txconf = {
436 .direction = DMA_MEM_TO_DEV,
437 .dst_addr = priv->base_dma_addr + SSI_TXDR,
438 .dst_addr_width = buswidth,
439 .dst_maxburst = SSI_FIFO_BURST_NUM,
442 dmaengine_slave_config(master->dma_tx, &txconf);
444 txdesc = dmaengine_prep_slave_sg(
445 master->dma_tx,
446 t->tx_sg.sgl, t->tx_sg.nents,
447 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
448 if (!txdesc)
449 goto out_err_prep;
451 txdesc->callback = uniphier_spi_dma_txcb;
452 txdesc->callback_param = master;
454 uniphier_spi_irq_enable(priv, SSI_IE_TXRE);
455 atomic_or(SSI_DMA_TX_BUSY, &priv->dma_busy);
457 dmaengine_submit(txdesc);
458 dma_async_issue_pending(master->dma_tx);
461 /* signal that we need to wait for completion */
462 return (priv->tx_buf || priv->rx_buf);
464 out_err_prep:
465 if (rxdesc)
466 dmaengine_terminate_sync(master->dma_rx);
468 return -EINVAL;
471 static int uniphier_spi_transfer_one_irq(struct spi_master *master,
472 struct spi_device *spi,
473 struct spi_transfer *t)
475 struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
476 struct device *dev = master->dev.parent;
477 unsigned long time_left;
479 reinit_completion(&priv->xfer_done);
481 uniphier_spi_fill_tx_fifo(priv);
483 uniphier_spi_irq_enable(priv, SSI_IE_RCIE | SSI_IE_RORIE);
485 time_left = wait_for_completion_timeout(&priv->xfer_done,
486 msecs_to_jiffies(SSI_TIMEOUT_MS));
488 uniphier_spi_irq_disable(priv, SSI_IE_RCIE | SSI_IE_RORIE);
490 if (!time_left) {
491 dev_err(dev, "transfer timeout.\n");
492 return -ETIMEDOUT;
495 return priv->error;
498 static int uniphier_spi_transfer_one_poll(struct spi_master *master,
499 struct spi_device *spi,
500 struct spi_transfer *t)
502 struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
503 int loop = SSI_POLL_TIMEOUT_US * 10;
505 while (priv->tx_bytes) {
506 uniphier_spi_fill_tx_fifo(priv);
508 while ((priv->rx_bytes - priv->tx_bytes) > 0) {
509 while (!(readl(priv->base + SSI_SR) & SSI_SR_RNE)
510 && loop--)
511 ndelay(100);
513 if (loop == -1)
514 goto irq_transfer;
516 uniphier_spi_recv(priv);
520 return 0;
522 irq_transfer:
523 return uniphier_spi_transfer_one_irq(master, spi, t);
526 static int uniphier_spi_transfer_one(struct spi_master *master,
527 struct spi_device *spi,
528 struct spi_transfer *t)
530 struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
531 unsigned long threshold;
532 bool use_dma;
534 /* Terminate and return success for 0 byte length transfer */
535 if (!t->len)
536 return 0;
538 uniphier_spi_setup_transfer(spi, t);
540 use_dma = master->can_dma ? master->can_dma(master, spi, t) : false;
541 if (use_dma)
542 return uniphier_spi_transfer_one_dma(master, spi, t);
545 * If the transfer operation will take longer than
546 * SSI_POLL_TIMEOUT_US, it should use irq.
548 threshold = DIV_ROUND_UP(SSI_POLL_TIMEOUT_US * priv->speed_hz,
549 USEC_PER_SEC * BITS_PER_BYTE);
550 if (t->len > threshold)
551 return uniphier_spi_transfer_one_irq(master, spi, t);
552 else
553 return uniphier_spi_transfer_one_poll(master, spi, t);
556 static int uniphier_spi_prepare_transfer_hardware(struct spi_master *master)
558 struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
560 writel(SSI_CTL_EN, priv->base + SSI_CTL);
562 return 0;
565 static int uniphier_spi_unprepare_transfer_hardware(struct spi_master *master)
567 struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
569 writel(0, priv->base + SSI_CTL);
571 return 0;
574 static void uniphier_spi_handle_err(struct spi_master *master,
575 struct spi_message *msg)
577 struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
578 u32 val;
580 /* stop running spi transfer */
581 writel(0, priv->base + SSI_CTL);
583 /* reset FIFOs */
584 val = SSI_FC_TXFFL | SSI_FC_RXFFL;
585 writel(val, priv->base + SSI_FC);
587 uniphier_spi_irq_disable(priv, SSI_IE_ALL_MASK);
589 if (atomic_read(&priv->dma_busy) & SSI_DMA_TX_BUSY) {
590 dmaengine_terminate_async(master->dma_tx);
591 atomic_andnot(SSI_DMA_TX_BUSY, &priv->dma_busy);
594 if (atomic_read(&priv->dma_busy) & SSI_DMA_RX_BUSY) {
595 dmaengine_terminate_async(master->dma_rx);
596 atomic_andnot(SSI_DMA_RX_BUSY, &priv->dma_busy);
600 static irqreturn_t uniphier_spi_handler(int irq, void *dev_id)
602 struct uniphier_spi_priv *priv = dev_id;
603 u32 val, stat;
605 stat = readl(priv->base + SSI_IS);
606 val = SSI_IC_TCIC | SSI_IC_RCIC | SSI_IC_RORIC;
607 writel(val, priv->base + SSI_IC);
609 /* rx fifo overrun */
610 if (stat & SSI_IS_RORID) {
611 priv->error = -EIO;
612 goto done;
615 /* rx complete */
616 if ((stat & SSI_IS_RCID) && (stat & SSI_IS_RXRS)) {
617 while ((readl(priv->base + SSI_SR) & SSI_SR_RNE) &&
618 (priv->rx_bytes - priv->tx_bytes) > 0)
619 uniphier_spi_recv(priv);
621 if ((readl(priv->base + SSI_SR) & SSI_SR_RNE) ||
622 (priv->rx_bytes != priv->tx_bytes)) {
623 priv->error = -EIO;
624 goto done;
625 } else if (priv->rx_bytes == 0)
626 goto done;
628 /* next tx transfer */
629 uniphier_spi_fill_tx_fifo(priv);
631 return IRQ_HANDLED;
634 return IRQ_NONE;
636 done:
637 complete(&priv->xfer_done);
638 return IRQ_HANDLED;
641 static int uniphier_spi_probe(struct platform_device *pdev)
643 struct uniphier_spi_priv *priv;
644 struct spi_master *master;
645 struct resource *res;
646 struct dma_slave_caps caps;
647 u32 dma_tx_burst = 0, dma_rx_burst = 0;
648 unsigned long clk_rate;
649 int irq;
650 int ret;
652 master = spi_alloc_master(&pdev->dev, sizeof(*priv));
653 if (!master)
654 return -ENOMEM;
656 platform_set_drvdata(pdev, master);
658 priv = spi_master_get_devdata(master);
659 priv->master = master;
660 priv->is_save_param = false;
662 priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
663 if (IS_ERR(priv->base)) {
664 ret = PTR_ERR(priv->base);
665 goto out_master_put;
667 priv->base_dma_addr = res->start;
669 priv->clk = devm_clk_get(&pdev->dev, NULL);
670 if (IS_ERR(priv->clk)) {
671 dev_err(&pdev->dev, "failed to get clock\n");
672 ret = PTR_ERR(priv->clk);
673 goto out_master_put;
676 ret = clk_prepare_enable(priv->clk);
677 if (ret)
678 goto out_master_put;
680 irq = platform_get_irq(pdev, 0);
681 if (irq < 0) {
682 ret = irq;
683 goto out_disable_clk;
686 ret = devm_request_irq(&pdev->dev, irq, uniphier_spi_handler,
687 0, "uniphier-spi", priv);
688 if (ret) {
689 dev_err(&pdev->dev, "failed to request IRQ\n");
690 goto out_disable_clk;
693 init_completion(&priv->xfer_done);
695 clk_rate = clk_get_rate(priv->clk);
697 master->max_speed_hz = DIV_ROUND_UP(clk_rate, SSI_MIN_CLK_DIVIDER);
698 master->min_speed_hz = DIV_ROUND_UP(clk_rate, SSI_MAX_CLK_DIVIDER);
699 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
700 master->dev.of_node = pdev->dev.of_node;
701 master->bus_num = pdev->id;
702 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
704 master->set_cs = uniphier_spi_set_cs;
705 master->transfer_one = uniphier_spi_transfer_one;
706 master->prepare_transfer_hardware
707 = uniphier_spi_prepare_transfer_hardware;
708 master->unprepare_transfer_hardware
709 = uniphier_spi_unprepare_transfer_hardware;
710 master->handle_err = uniphier_spi_handle_err;
711 master->can_dma = uniphier_spi_can_dma;
713 master->num_chipselect = 1;
714 master->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
716 master->dma_tx = dma_request_chan(&pdev->dev, "tx");
717 if (IS_ERR_OR_NULL(master->dma_tx)) {
718 if (PTR_ERR(master->dma_tx) == -EPROBE_DEFER) {
719 ret = -EPROBE_DEFER;
720 goto out_disable_clk;
722 master->dma_tx = NULL;
723 dma_tx_burst = INT_MAX;
724 } else {
725 ret = dma_get_slave_caps(master->dma_tx, &caps);
726 if (ret) {
727 dev_err(&pdev->dev, "failed to get TX DMA capacities: %d\n",
728 ret);
729 goto out_disable_clk;
731 dma_tx_burst = caps.max_burst;
734 master->dma_rx = dma_request_chan(&pdev->dev, "rx");
735 if (IS_ERR_OR_NULL(master->dma_rx)) {
736 if (PTR_ERR(master->dma_rx) == -EPROBE_DEFER) {
737 ret = -EPROBE_DEFER;
738 goto out_disable_clk;
740 master->dma_rx = NULL;
741 dma_rx_burst = INT_MAX;
742 } else {
743 ret = dma_get_slave_caps(master->dma_rx, &caps);
744 if (ret) {
745 dev_err(&pdev->dev, "failed to get RX DMA capacities: %d\n",
746 ret);
747 goto out_disable_clk;
749 dma_rx_burst = caps.max_burst;
752 master->max_dma_len = min(dma_tx_burst, dma_rx_burst);
754 ret = devm_spi_register_master(&pdev->dev, master);
755 if (ret)
756 goto out_disable_clk;
758 return 0;
760 out_disable_clk:
761 clk_disable_unprepare(priv->clk);
763 out_master_put:
764 spi_master_put(master);
765 return ret;
768 static int uniphier_spi_remove(struct platform_device *pdev)
770 struct uniphier_spi_priv *priv = platform_get_drvdata(pdev);
772 if (priv->master->dma_tx)
773 dma_release_channel(priv->master->dma_tx);
774 if (priv->master->dma_rx)
775 dma_release_channel(priv->master->dma_rx);
777 clk_disable_unprepare(priv->clk);
779 return 0;
782 static const struct of_device_id uniphier_spi_match[] = {
783 { .compatible = "socionext,uniphier-scssi" },
784 { /* sentinel */ }
786 MODULE_DEVICE_TABLE(of, uniphier_spi_match);
788 static struct platform_driver uniphier_spi_driver = {
789 .probe = uniphier_spi_probe,
790 .remove = uniphier_spi_remove,
791 .driver = {
792 .name = "uniphier-spi",
793 .of_match_table = uniphier_spi_match,
796 module_platform_driver(uniphier_spi_driver);
798 MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
799 MODULE_AUTHOR("Keiji Hayashibara <hayashibara.keiji@socionext.com>");
800 MODULE_DESCRIPTION("Socionext UniPhier SPI controller driver");
801 MODULE_LICENSE("GPL v2");