1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
4 * Author: Addy Ke <addy.ke@rock-chips.com>
8 #include <linux/dmaengine.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
12 #include <linux/pinctrl/consumer.h>
13 #include <linux/platform_device.h>
14 #include <linux/spi/spi.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/scatterlist.h>
18 #define DRIVER_NAME "rockchip-spi"
20 #define ROCKCHIP_SPI_CLR_BITS(reg, bits) \
21 writel_relaxed(readl_relaxed(reg) & ~(bits), reg)
22 #define ROCKCHIP_SPI_SET_BITS(reg, bits) \
23 writel_relaxed(readl_relaxed(reg) | (bits), reg)
25 /* SPI register offsets */
26 #define ROCKCHIP_SPI_CTRLR0 0x0000
27 #define ROCKCHIP_SPI_CTRLR1 0x0004
28 #define ROCKCHIP_SPI_SSIENR 0x0008
29 #define ROCKCHIP_SPI_SER 0x000c
30 #define ROCKCHIP_SPI_BAUDR 0x0010
31 #define ROCKCHIP_SPI_TXFTLR 0x0014
32 #define ROCKCHIP_SPI_RXFTLR 0x0018
33 #define ROCKCHIP_SPI_TXFLR 0x001c
34 #define ROCKCHIP_SPI_RXFLR 0x0020
35 #define ROCKCHIP_SPI_SR 0x0024
36 #define ROCKCHIP_SPI_IPR 0x0028
37 #define ROCKCHIP_SPI_IMR 0x002c
38 #define ROCKCHIP_SPI_ISR 0x0030
39 #define ROCKCHIP_SPI_RISR 0x0034
40 #define ROCKCHIP_SPI_ICR 0x0038
41 #define ROCKCHIP_SPI_DMACR 0x003c
42 #define ROCKCHIP_SPI_DMATDLR 0x0040
43 #define ROCKCHIP_SPI_DMARDLR 0x0044
44 #define ROCKCHIP_SPI_VERSION 0x0048
45 #define ROCKCHIP_SPI_TXDR 0x0400
46 #define ROCKCHIP_SPI_RXDR 0x0800
48 /* Bit fields in CTRLR0 */
49 #define CR0_DFS_OFFSET 0
50 #define CR0_DFS_4BIT 0x0
51 #define CR0_DFS_8BIT 0x1
52 #define CR0_DFS_16BIT 0x2
54 #define CR0_CFS_OFFSET 2
56 #define CR0_SCPH_OFFSET 6
58 #define CR0_SCPOL_OFFSET 7
60 #define CR0_CSM_OFFSET 8
61 #define CR0_CSM_KEEP 0x0
62 /* ss_n be high for half sclk_out cycles */
63 #define CR0_CSM_HALF 0X1
64 /* ss_n be high for one sclk_out cycle */
65 #define CR0_CSM_ONE 0x2
67 /* ss_n to sclk_out delay */
68 #define CR0_SSD_OFFSET 10
70 * The period between ss_n active and
71 * sclk_out active is half sclk_out cycles
73 #define CR0_SSD_HALF 0x0
75 * The period between ss_n active and
76 * sclk_out active is one sclk_out cycle
78 #define CR0_SSD_ONE 0x1
80 #define CR0_EM_OFFSET 11
81 #define CR0_EM_LITTLE 0x0
82 #define CR0_EM_BIG 0x1
84 #define CR0_FBM_OFFSET 12
85 #define CR0_FBM_MSB 0x0
86 #define CR0_FBM_LSB 0x1
88 #define CR0_BHT_OFFSET 13
89 #define CR0_BHT_16BIT 0x0
90 #define CR0_BHT_8BIT 0x1
92 #define CR0_RSD_OFFSET 14
93 #define CR0_RSD_MAX 0x3
95 #define CR0_FRF_OFFSET 16
96 #define CR0_FRF_SPI 0x0
97 #define CR0_FRF_SSP 0x1
98 #define CR0_FRF_MICROWIRE 0x2
100 #define CR0_XFM_OFFSET 18
101 #define CR0_XFM_MASK (0x03 << SPI_XFM_OFFSET)
102 #define CR0_XFM_TR 0x0
103 #define CR0_XFM_TO 0x1
104 #define CR0_XFM_RO 0x2
106 #define CR0_OPM_OFFSET 20
107 #define CR0_OPM_MASTER 0x0
108 #define CR0_OPM_SLAVE 0x1
110 #define CR0_SOI_OFFSET 23
112 #define CR0_MTM_OFFSET 0x21
114 /* Bit fields in SER, 2bit */
117 /* Bit fields in BAUDR */
118 #define BAUDR_SCKDV_MIN 2
119 #define BAUDR_SCKDV_MAX 65534
121 /* Bit fields in SR, 6bit */
123 #define SR_BUSY (1 << 0)
124 #define SR_TF_FULL (1 << 1)
125 #define SR_TF_EMPTY (1 << 2)
126 #define SR_RF_EMPTY (1 << 3)
127 #define SR_RF_FULL (1 << 4)
128 #define SR_SLAVE_TX_BUSY (1 << 5)
130 /* Bit fields in ISR, IMR, ISR, RISR, 5bit */
131 #define INT_MASK 0x1f
132 #define INT_TF_EMPTY (1 << 0)
133 #define INT_TF_OVERFLOW (1 << 1)
134 #define INT_RF_UNDERFLOW (1 << 2)
135 #define INT_RF_OVERFLOW (1 << 3)
136 #define INT_RF_FULL (1 << 4)
137 #define INT_CS_INACTIVE (1 << 6)
139 /* Bit fields in ICR, 4bit */
140 #define ICR_MASK 0x0f
141 #define ICR_ALL (1 << 0)
142 #define ICR_RF_UNDERFLOW (1 << 1)
143 #define ICR_RF_OVERFLOW (1 << 2)
144 #define ICR_TF_OVERFLOW (1 << 3)
146 /* Bit fields in DMACR */
147 #define RF_DMA_EN (1 << 0)
148 #define TF_DMA_EN (1 << 1)
150 /* Driver state flags */
151 #define RXDMA (1 << 0)
152 #define TXDMA (1 << 1)
154 /* sclk_out: spi master internal logic in rk3x can support 50Mhz */
155 #define MAX_SCLK_OUT 50000000U
158 * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However,
159 * the controller seems to hang when given 0x10000, so stick with this for now.
161 #define ROCKCHIP_SPI_MAX_TRANLEN 0xffff
163 /* 2 for native cs, 2 for cs-gpio */
164 #define ROCKCHIP_SPI_MAX_CS_NUM 4
165 #define ROCKCHIP_SPI_VER2_TYPE1 0x05EC0002
166 #define ROCKCHIP_SPI_VER2_TYPE2 0x00110002
168 #define ROCKCHIP_AUTOSUSPEND_TIMEOUT 2000
170 struct rockchip_spi {
174 struct clk *apb_pclk;
177 dma_addr_t dma_addr_rx;
178 dma_addr_t dma_addr_tx;
182 unsigned int tx_left;
183 unsigned int rx_left;
187 /*depth of the FIFO buffer */
189 /* frequency of spiclk */
195 bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];
198 bool cs_inactive; /* spi slave tansmition stop when cs inactive */
199 struct spi_transfer *xfer; /* Store xfer temporarily */
202 static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)
204 writel_relaxed((enable ? 1U : 0U), rs->regs + ROCKCHIP_SPI_SSIENR);
207 static inline void wait_for_tx_idle(struct rockchip_spi *rs, bool slave_mode)
209 unsigned long timeout = jiffies + msecs_to_jiffies(5);
213 if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_SLAVE_TX_BUSY) &&
214 !((readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY)))
217 if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
220 } while (!time_after(jiffies, timeout));
222 dev_warn(rs->dev, "spi controller is in busy state!\n");
225 static u32 get_fifo_len(struct rockchip_spi *rs)
229 ver = readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION);
232 case ROCKCHIP_SPI_VER2_TYPE1:
233 case ROCKCHIP_SPI_VER2_TYPE2:
240 static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
242 struct spi_controller *ctlr = spi->controller;
243 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
244 bool cs_asserted = spi->mode & SPI_CS_HIGH ? enable : !enable;
246 /* Return immediately for no-op */
247 if (cs_asserted == rs->cs_asserted[spi->chip_select])
251 /* Keep things powered as long as CS is asserted */
252 pm_runtime_get_sync(rs->dev);
255 ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, 1);
257 ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select));
260 ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, 1);
262 ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select));
264 /* Drop reference from when we first asserted CS */
265 pm_runtime_put(rs->dev);
268 rs->cs_asserted[spi->chip_select] = cs_asserted;
271 static void rockchip_spi_handle_err(struct spi_controller *ctlr,
272 struct spi_message *msg)
274 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
276 /* stop running spi transfer
277 * this also flushes both rx and tx fifos
279 spi_enable_chip(rs, false);
281 /* make sure all interrupts are masked */
282 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
284 if (atomic_read(&rs->state) & TXDMA)
285 dmaengine_terminate_async(ctlr->dma_tx);
287 if (atomic_read(&rs->state) & RXDMA)
288 dmaengine_terminate_async(ctlr->dma_rx);
291 static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
293 u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
294 u32 words = min(rs->tx_left, tx_free);
296 rs->tx_left -= words;
297 for (; words; words--) {
300 if (rs->n_bytes == 1)
303 txw = *(u16 *)rs->tx;
305 writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
306 rs->tx += rs->n_bytes;
310 static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
312 u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
313 u32 rx_left = (rs->rx_left > words) ? rs->rx_left - words : 0;
315 /* the hardware doesn't allow us to change fifo threshold
316 * level while spi is enabled, so instead make sure to leave
317 * enough words in the rx fifo to get the last interrupt
318 * exactly when all words have been received
321 u32 ftl = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFTLR) + 1;
325 words = rs->rx_left - rx_left;
329 rs->rx_left = rx_left;
330 for (; words; words--) {
331 u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
336 if (rs->n_bytes == 1)
337 *(u8 *)rs->rx = (u8)rxw;
339 *(u16 *)rs->rx = (u16)rxw;
340 rs->rx += rs->n_bytes;
344 static irqreturn_t rockchip_spi_isr(int irq, void *dev_id)
346 struct spi_controller *ctlr = dev_id;
347 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
349 /* When int_cs_inactive comes, spi slave abort */
350 if (rs->cs_inactive && readl_relaxed(rs->regs + ROCKCHIP_SPI_IMR) & INT_CS_INACTIVE) {
351 ctlr->slave_abort(ctlr);
352 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
353 writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR);
359 rockchip_spi_pio_writer(rs);
361 rockchip_spi_pio_reader(rs);
363 spi_enable_chip(rs, false);
364 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
365 writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR);
366 spi_finalize_current_transfer(ctlr);
372 static int rockchip_spi_prepare_irq(struct rockchip_spi *rs,
373 struct spi_controller *ctlr,
374 struct spi_transfer *xfer)
376 rs->tx = xfer->tx_buf;
377 rs->rx = xfer->rx_buf;
378 rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0;
379 rs->rx_left = xfer->len / rs->n_bytes;
382 writel_relaxed(INT_RF_FULL | INT_CS_INACTIVE, rs->regs + ROCKCHIP_SPI_IMR);
384 writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR);
385 spi_enable_chip(rs, true);
388 rockchip_spi_pio_writer(rs);
390 /* 1 means the transfer is in progress */
394 static void rockchip_spi_dma_rxcb(void *data)
396 struct spi_controller *ctlr = data;
397 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
398 int state = atomic_fetch_andnot(RXDMA, &rs->state);
400 if (state & TXDMA && !rs->slave_abort)
404 writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
406 spi_enable_chip(rs, false);
407 spi_finalize_current_transfer(ctlr);
410 static void rockchip_spi_dma_txcb(void *data)
412 struct spi_controller *ctlr = data;
413 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
414 int state = atomic_fetch_andnot(TXDMA, &rs->state);
416 if (state & RXDMA && !rs->slave_abort)
419 /* Wait until the FIFO data completely. */
420 wait_for_tx_idle(rs, ctlr->slave);
422 spi_enable_chip(rs, false);
423 spi_finalize_current_transfer(ctlr);
426 static u32 rockchip_spi_calc_burst_size(u32 data_len)
430 /* burst size: 1, 2, 4, 8 */
431 for (i = 1; i < 8; i <<= 1) {
439 static int rockchip_spi_prepare_dma(struct rockchip_spi *rs,
440 struct spi_controller *ctlr, struct spi_transfer *xfer)
442 struct dma_async_tx_descriptor *rxdesc, *txdesc;
444 atomic_set(&rs->state, 0);
446 rs->tx = xfer->tx_buf;
447 rs->rx = xfer->rx_buf;
451 struct dma_slave_config rxconf = {
452 .direction = DMA_DEV_TO_MEM,
453 .src_addr = rs->dma_addr_rx,
454 .src_addr_width = rs->n_bytes,
455 .src_maxburst = rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes),
458 dmaengine_slave_config(ctlr->dma_rx, &rxconf);
460 rxdesc = dmaengine_prep_slave_sg(
462 xfer->rx_sg.sgl, xfer->rx_sg.nents,
463 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
467 rxdesc->callback = rockchip_spi_dma_rxcb;
468 rxdesc->callback_param = ctlr;
473 struct dma_slave_config txconf = {
474 .direction = DMA_MEM_TO_DEV,
475 .dst_addr = rs->dma_addr_tx,
476 .dst_addr_width = rs->n_bytes,
477 .dst_maxburst = rs->fifo_len / 4,
480 dmaengine_slave_config(ctlr->dma_tx, &txconf);
482 txdesc = dmaengine_prep_slave_sg(
484 xfer->tx_sg.sgl, xfer->tx_sg.nents,
485 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
488 dmaengine_terminate_sync(ctlr->dma_rx);
492 txdesc->callback = rockchip_spi_dma_txcb;
493 txdesc->callback_param = ctlr;
496 /* rx must be started before tx due to spi instinct */
498 atomic_or(RXDMA, &rs->state);
499 ctlr->dma_rx->cookie = dmaengine_submit(rxdesc);
500 dma_async_issue_pending(ctlr->dma_rx);
504 writel_relaxed(INT_CS_INACTIVE, rs->regs + ROCKCHIP_SPI_IMR);
506 spi_enable_chip(rs, true);
509 atomic_or(TXDMA, &rs->state);
510 dmaengine_submit(txdesc);
511 dma_async_issue_pending(ctlr->dma_tx);
514 /* 1 means the transfer is in progress */
518 static int rockchip_spi_config(struct rockchip_spi *rs,
519 struct spi_device *spi, struct spi_transfer *xfer,
520 bool use_dma, bool slave_mode)
522 u32 cr0 = CR0_FRF_SPI << CR0_FRF_OFFSET
523 | CR0_BHT_8BIT << CR0_BHT_OFFSET
524 | CR0_SSD_ONE << CR0_SSD_OFFSET
525 | CR0_EM_BIG << CR0_EM_OFFSET;
530 cr0 |= CR0_OPM_SLAVE << CR0_OPM_OFFSET;
531 rs->slave_abort = false;
533 cr0 |= rs->rsd << CR0_RSD_OFFSET;
534 cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET;
535 if (spi->mode & SPI_LSB_FIRST)
536 cr0 |= CR0_FBM_LSB << CR0_FBM_OFFSET;
537 if (spi->mode & SPI_CS_HIGH)
538 cr0 |= BIT(spi->chip_select) << CR0_SOI_OFFSET;
540 if (xfer->rx_buf && xfer->tx_buf)
541 cr0 |= CR0_XFM_TR << CR0_XFM_OFFSET;
542 else if (xfer->rx_buf)
543 cr0 |= CR0_XFM_RO << CR0_XFM_OFFSET;
545 cr0 |= CR0_XFM_TO << CR0_XFM_OFFSET;
547 switch (xfer->bits_per_word) {
549 cr0 |= CR0_DFS_4BIT << CR0_DFS_OFFSET;
553 cr0 |= CR0_DFS_8BIT << CR0_DFS_OFFSET;
557 cr0 |= CR0_DFS_16BIT << CR0_DFS_OFFSET;
558 cr1 = xfer->len / 2 - 1;
561 /* we only whitelist 4, 8 and 16 bit words in
562 * ctlr->bits_per_word_mask, so this shouldn't
565 dev_err(rs->dev, "unknown bits per word: %d\n",
566 xfer->bits_per_word);
577 writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
578 writel_relaxed(cr1, rs->regs + ROCKCHIP_SPI_CTRLR1);
580 /* unfortunately setting the fifo threshold level to generate an
581 * interrupt exactly when the fifo is full doesn't seem to work,
582 * so we need the strict inequality here
584 if ((xfer->len / rs->n_bytes) < rs->fifo_len)
585 writel_relaxed(xfer->len / rs->n_bytes - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
587 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
589 writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_DMATDLR);
590 writel_relaxed(rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes) - 1,
591 rs->regs + ROCKCHIP_SPI_DMARDLR);
592 writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
594 /* the hardware only supports an even clock divisor, so
595 * round divisor = spiclk / speed up to nearest even number
596 * so that the resulting speed is <= the requested speed
598 writel_relaxed(2 * DIV_ROUND_UP(rs->freq, 2 * xfer->speed_hz),
599 rs->regs + ROCKCHIP_SPI_BAUDR);
604 static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
606 return ROCKCHIP_SPI_MAX_TRANLEN;
609 static int rockchip_spi_slave_abort(struct spi_controller *ctlr)
611 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
613 struct dma_tx_state state;
614 enum dma_status status;
616 /* Get current dma rx point */
617 if (atomic_read(&rs->state) & RXDMA) {
618 dmaengine_pause(ctlr->dma_rx);
619 status = dmaengine_tx_status(ctlr->dma_rx, ctlr->dma_rx->cookie, &state);
620 if (status == DMA_ERROR) {
621 rs->rx = rs->xfer->rx_buf;
623 rx_fifo_left = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
624 for (; rx_fifo_left; rx_fifo_left--)
625 readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
628 rs->rx += rs->xfer->len - rs->n_bytes * state.residue;
632 /* Get the valid data left in rx fifo and set rs->xfer->len real rx size */
634 rx_fifo_left = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
635 for (; rx_fifo_left; rx_fifo_left--) {
636 u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
638 if (rs->n_bytes == 1)
639 *(u8 *)rs->rx = (u8)rxw;
641 *(u16 *)rs->rx = (u16)rxw;
642 rs->rx += rs->n_bytes;
644 rs->xfer->len = (unsigned int)(rs->rx - rs->xfer->rx_buf);
648 if (atomic_read(&rs->state) & RXDMA)
649 dmaengine_terminate_sync(ctlr->dma_rx);
650 if (atomic_read(&rs->state) & TXDMA)
651 dmaengine_terminate_sync(ctlr->dma_tx);
652 atomic_set(&rs->state, 0);
653 spi_enable_chip(rs, false);
654 rs->slave_abort = true;
655 spi_finalize_current_transfer(ctlr);
660 static int rockchip_spi_transfer_one(
661 struct spi_controller *ctlr,
662 struct spi_device *spi,
663 struct spi_transfer *xfer)
665 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
669 /* Zero length transfers won't trigger an interrupt on completion */
671 spi_finalize_current_transfer(ctlr);
675 WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
676 (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
678 if (!xfer->tx_buf && !xfer->rx_buf) {
679 dev_err(rs->dev, "No buffer for transfer\n");
683 if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) {
684 dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len);
688 rs->n_bytes = xfer->bits_per_word <= 8 ? 1 : 2;
690 use_dma = ctlr->can_dma ? ctlr->can_dma(ctlr, spi, xfer) : false;
692 ret = rockchip_spi_config(rs, spi, xfer, use_dma, ctlr->slave);
697 return rockchip_spi_prepare_dma(rs, ctlr, xfer);
699 return rockchip_spi_prepare_irq(rs, ctlr, xfer);
702 static bool rockchip_spi_can_dma(struct spi_controller *ctlr,
703 struct spi_device *spi,
704 struct spi_transfer *xfer)
706 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
707 unsigned int bytes_per_word = xfer->bits_per_word <= 8 ? 1 : 2;
709 /* if the numbor of spi words to transfer is less than the fifo
710 * length we can just fill the fifo and wait for a single irq,
711 * so don't bother setting up dma
713 return xfer->len / bytes_per_word >= rs->fifo_len;
716 static int rockchip_spi_setup(struct spi_device *spi)
718 struct rockchip_spi *rs = spi_controller_get_devdata(spi->controller);
721 pm_runtime_get_sync(rs->dev);
723 cr0 = readl_relaxed(rs->regs + ROCKCHIP_SPI_CTRLR0);
725 cr0 &= ~(0x3 << CR0_SCPH_OFFSET);
726 cr0 |= ((spi->mode & 0x3) << CR0_SCPH_OFFSET);
727 if (spi->mode & SPI_CS_HIGH && spi->chip_select <= 1)
728 cr0 |= BIT(spi->chip_select) << CR0_SOI_OFFSET;
729 else if (spi->chip_select <= 1)
730 cr0 &= ~(BIT(spi->chip_select) << CR0_SOI_OFFSET);
732 writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
734 pm_runtime_put(rs->dev);
739 static int rockchip_spi_probe(struct platform_device *pdev)
742 struct rockchip_spi *rs;
743 struct spi_controller *ctlr;
744 struct resource *mem;
745 struct device_node *np = pdev->dev.of_node;
746 u32 rsd_nsecs, num_cs;
749 slave_mode = of_property_read_bool(np, "spi-slave");
752 ctlr = spi_alloc_slave(&pdev->dev,
753 sizeof(struct rockchip_spi));
755 ctlr = spi_alloc_master(&pdev->dev,
756 sizeof(struct rockchip_spi));
761 platform_set_drvdata(pdev, ctlr);
763 rs = spi_controller_get_devdata(ctlr);
764 ctlr->slave = slave_mode;
766 /* Get basic io resource and map it */
767 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
768 rs->regs = devm_ioremap_resource(&pdev->dev, mem);
769 if (IS_ERR(rs->regs)) {
770 ret = PTR_ERR(rs->regs);
774 rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
775 if (IS_ERR(rs->apb_pclk)) {
776 dev_err(&pdev->dev, "Failed to get apb_pclk\n");
777 ret = PTR_ERR(rs->apb_pclk);
781 rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
782 if (IS_ERR(rs->spiclk)) {
783 dev_err(&pdev->dev, "Failed to get spi_pclk\n");
784 ret = PTR_ERR(rs->spiclk);
788 ret = clk_prepare_enable(rs->apb_pclk);
790 dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
794 ret = clk_prepare_enable(rs->spiclk);
796 dev_err(&pdev->dev, "Failed to enable spi_clk\n");
797 goto err_disable_apbclk;
800 spi_enable_chip(rs, false);
802 ret = platform_get_irq(pdev, 0);
804 goto err_disable_spiclk;
806 ret = devm_request_threaded_irq(&pdev->dev, ret, rockchip_spi_isr, NULL,
807 IRQF_ONESHOT, dev_name(&pdev->dev), ctlr);
809 goto err_disable_spiclk;
811 rs->dev = &pdev->dev;
812 rs->freq = clk_get_rate(rs->spiclk);
814 if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
816 /* rx sample delay is expressed in parent clock cycles (max 3) */
817 u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8),
820 dev_warn(rs->dev, "%u Hz are too slow to express %u ns delay\n",
821 rs->freq, rsd_nsecs);
822 } else if (rsd > CR0_RSD_MAX) {
824 dev_warn(rs->dev, "%u Hz are too fast to express %u ns delay, clamping at %u ns\n",
826 CR0_RSD_MAX * 1000000000U / rs->freq);
831 rs->fifo_len = get_fifo_len(rs);
833 dev_err(&pdev->dev, "Failed to get fifo length\n");
835 goto err_disable_spiclk;
838 pm_runtime_set_autosuspend_delay(&pdev->dev, ROCKCHIP_AUTOSUSPEND_TIMEOUT);
839 pm_runtime_use_autosuspend(&pdev->dev);
840 pm_runtime_set_active(&pdev->dev);
841 pm_runtime_enable(&pdev->dev);
843 ctlr->auto_runtime_pm = true;
844 ctlr->bus_num = pdev->id;
845 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST;
847 ctlr->mode_bits |= SPI_NO_CS;
848 ctlr->slave_abort = rockchip_spi_slave_abort;
850 ctlr->flags = SPI_MASTER_GPIO_SS;
851 ctlr->max_native_cs = ROCKCHIP_SPI_MAX_CS_NUM;
853 * rk spi0 has two native cs, spi1..5 one cs only
854 * if num-cs is missing in the dts, default to 1
856 if (of_property_read_u32(np, "num-cs", &num_cs))
858 ctlr->num_chipselect = num_cs;
859 ctlr->use_gpio_descriptors = true;
861 ctlr->dev.of_node = pdev->dev.of_node;
862 ctlr->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8) | SPI_BPW_MASK(4);
863 ctlr->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX;
864 ctlr->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT);
866 ctlr->setup = rockchip_spi_setup;
867 ctlr->set_cs = rockchip_spi_set_cs;
868 ctlr->transfer_one = rockchip_spi_transfer_one;
869 ctlr->max_transfer_size = rockchip_spi_max_transfer_size;
870 ctlr->handle_err = rockchip_spi_handle_err;
872 ctlr->dma_tx = dma_request_chan(rs->dev, "tx");
873 if (IS_ERR(ctlr->dma_tx)) {
874 /* Check tx to see if we need defer probing driver */
875 if (PTR_ERR(ctlr->dma_tx) == -EPROBE_DEFER) {
877 goto err_disable_pm_runtime;
879 dev_warn(rs->dev, "Failed to request TX DMA channel\n");
883 ctlr->dma_rx = dma_request_chan(rs->dev, "rx");
884 if (IS_ERR(ctlr->dma_rx)) {
885 if (PTR_ERR(ctlr->dma_rx) == -EPROBE_DEFER) {
887 goto err_free_dma_tx;
889 dev_warn(rs->dev, "Failed to request RX DMA channel\n");
893 if (ctlr->dma_tx && ctlr->dma_rx) {
894 rs->dma_addr_tx = mem->start + ROCKCHIP_SPI_TXDR;
895 rs->dma_addr_rx = mem->start + ROCKCHIP_SPI_RXDR;
896 ctlr->can_dma = rockchip_spi_can_dma;
899 switch (readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION)) {
900 case ROCKCHIP_SPI_VER2_TYPE2:
901 ctlr->mode_bits |= SPI_CS_HIGH;
902 if (ctlr->can_dma && slave_mode)
903 rs->cs_inactive = true;
905 rs->cs_inactive = false;
908 rs->cs_inactive = false;
912 ret = devm_spi_register_controller(&pdev->dev, ctlr);
914 dev_err(&pdev->dev, "Failed to register controller\n");
915 goto err_free_dma_rx;
922 dma_release_channel(ctlr->dma_rx);
925 dma_release_channel(ctlr->dma_tx);
926 err_disable_pm_runtime:
927 pm_runtime_disable(&pdev->dev);
929 clk_disable_unprepare(rs->spiclk);
931 clk_disable_unprepare(rs->apb_pclk);
933 spi_controller_put(ctlr);
938 static int rockchip_spi_remove(struct platform_device *pdev)
940 struct spi_controller *ctlr = spi_controller_get(platform_get_drvdata(pdev));
941 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
943 pm_runtime_get_sync(&pdev->dev);
945 clk_disable_unprepare(rs->spiclk);
946 clk_disable_unprepare(rs->apb_pclk);
948 pm_runtime_put_noidle(&pdev->dev);
949 pm_runtime_disable(&pdev->dev);
950 pm_runtime_set_suspended(&pdev->dev);
953 dma_release_channel(ctlr->dma_tx);
955 dma_release_channel(ctlr->dma_rx);
957 spi_controller_put(ctlr);
962 #ifdef CONFIG_PM_SLEEP
963 static int rockchip_spi_suspend(struct device *dev)
966 struct spi_controller *ctlr = dev_get_drvdata(dev);
968 ret = spi_controller_suspend(ctlr);
972 ret = pm_runtime_force_suspend(dev);
976 pinctrl_pm_select_sleep_state(dev);
981 static int rockchip_spi_resume(struct device *dev)
984 struct spi_controller *ctlr = dev_get_drvdata(dev);
985 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
987 pinctrl_pm_select_default_state(dev);
989 ret = pm_runtime_force_resume(dev);
993 ret = spi_controller_resume(ctlr);
995 clk_disable_unprepare(rs->spiclk);
996 clk_disable_unprepare(rs->apb_pclk);
1001 #endif /* CONFIG_PM_SLEEP */
1004 static int rockchip_spi_runtime_suspend(struct device *dev)
1006 struct spi_controller *ctlr = dev_get_drvdata(dev);
1007 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
1009 clk_disable_unprepare(rs->spiclk);
1010 clk_disable_unprepare(rs->apb_pclk);
1015 static int rockchip_spi_runtime_resume(struct device *dev)
1018 struct spi_controller *ctlr = dev_get_drvdata(dev);
1019 struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
1021 ret = clk_prepare_enable(rs->apb_pclk);
1025 ret = clk_prepare_enable(rs->spiclk);
1027 clk_disable_unprepare(rs->apb_pclk);
1031 #endif /* CONFIG_PM */
1033 static const struct dev_pm_ops rockchip_spi_pm = {
1034 SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
1035 SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
1036 rockchip_spi_runtime_resume, NULL)
1039 static const struct of_device_id rockchip_spi_dt_match[] = {
1040 { .compatible = "rockchip,px30-spi", },
1041 { .compatible = "rockchip,rk3036-spi", },
1042 { .compatible = "rockchip,rk3066-spi", },
1043 { .compatible = "rockchip,rk3188-spi", },
1044 { .compatible = "rockchip,rk3228-spi", },
1045 { .compatible = "rockchip,rk3288-spi", },
1046 { .compatible = "rockchip,rk3308-spi", },
1047 { .compatible = "rockchip,rk3328-spi", },
1048 { .compatible = "rockchip,rk3368-spi", },
1049 { .compatible = "rockchip,rk3399-spi", },
1050 { .compatible = "rockchip,rv1108-spi", },
1051 { .compatible = "rockchip,rv1126-spi", },
1054 MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
1056 static struct platform_driver rockchip_spi_driver = {
1058 .name = DRIVER_NAME,
1059 .pm = &rockchip_spi_pm,
1060 .of_match_table = of_match_ptr(rockchip_spi_dt_match),
1062 .probe = rockchip_spi_probe,
1063 .remove = rockchip_spi_remove,
1066 module_platform_driver(rockchip_spi_driver);
1068 MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
1069 MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
1070 MODULE_LICENSE("GPL v2");