1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2014, The Linux foundation. All rights reserved.
7 #include <linux/delay.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/dmaengine.h>
10 #include <linux/err.h>
11 #include <linux/interconnect.h>
12 #include <linux/interrupt.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_opp.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/spi/spi.h>
21 #include "internals.h"
23 #define QUP_CONFIG 0x0000
24 #define QUP_STATE 0x0004
25 #define QUP_IO_M_MODES 0x0008
26 #define QUP_SW_RESET 0x000c
27 #define QUP_OPERATIONAL 0x0018
28 #define QUP_ERROR_FLAGS 0x001c
29 #define QUP_ERROR_FLAGS_EN 0x0020
30 #define QUP_OPERATIONAL_MASK 0x0028
31 #define QUP_HW_VERSION 0x0030
32 #define QUP_MX_OUTPUT_CNT 0x0100
33 #define QUP_OUTPUT_FIFO 0x0110
34 #define QUP_MX_WRITE_CNT 0x0150
35 #define QUP_MX_INPUT_CNT 0x0200
36 #define QUP_MX_READ_CNT 0x0208
37 #define QUP_INPUT_FIFO 0x0218
39 #define SPI_CONFIG 0x0300
40 #define SPI_IO_CONTROL 0x0304
41 #define SPI_ERROR_FLAGS 0x0308
42 #define SPI_ERROR_FLAGS_EN 0x030c
44 /* QUP_CONFIG fields */
45 #define QUP_CONFIG_SPI_MODE (1 << 8)
46 #define QUP_CONFIG_CLOCK_AUTO_GATE BIT(13)
47 #define QUP_CONFIG_NO_INPUT BIT(7)
48 #define QUP_CONFIG_NO_OUTPUT BIT(6)
49 #define QUP_CONFIG_N 0x001f
51 /* QUP_STATE fields */
52 #define QUP_STATE_VALID BIT(2)
53 #define QUP_STATE_RESET 0
54 #define QUP_STATE_RUN 1
55 #define QUP_STATE_PAUSE 3
56 #define QUP_STATE_MASK 3
57 #define QUP_STATE_CLEAR 2
59 #define QUP_HW_VERSION_2_1_1 0x20010001
61 /* QUP_IO_M_MODES fields */
62 #define QUP_IO_M_PACK_EN BIT(15)
63 #define QUP_IO_M_UNPACK_EN BIT(14)
64 #define QUP_IO_M_INPUT_MODE_MASK_SHIFT 12
65 #define QUP_IO_M_OUTPUT_MODE_MASK_SHIFT 10
66 #define QUP_IO_M_INPUT_MODE_MASK (3 << QUP_IO_M_INPUT_MODE_MASK_SHIFT)
67 #define QUP_IO_M_OUTPUT_MODE_MASK (3 << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT)
69 #define QUP_IO_M_OUTPUT_BLOCK_SIZE(x) (((x) & (0x03 << 0)) >> 0)
70 #define QUP_IO_M_OUTPUT_FIFO_SIZE(x) (((x) & (0x07 << 2)) >> 2)
71 #define QUP_IO_M_INPUT_BLOCK_SIZE(x) (((x) & (0x03 << 5)) >> 5)
72 #define QUP_IO_M_INPUT_FIFO_SIZE(x) (((x) & (0x07 << 7)) >> 7)
74 #define QUP_IO_M_MODE_FIFO 0
75 #define QUP_IO_M_MODE_BLOCK 1
76 #define QUP_IO_M_MODE_DMOV 2
77 #define QUP_IO_M_MODE_BAM 3
79 /* QUP_OPERATIONAL fields */
80 #define QUP_OP_IN_BLOCK_READ_REQ BIT(13)
81 #define QUP_OP_OUT_BLOCK_WRITE_REQ BIT(12)
82 #define QUP_OP_MAX_INPUT_DONE_FLAG BIT(11)
83 #define QUP_OP_MAX_OUTPUT_DONE_FLAG BIT(10)
84 #define QUP_OP_IN_SERVICE_FLAG BIT(9)
85 #define QUP_OP_OUT_SERVICE_FLAG BIT(8)
86 #define QUP_OP_IN_FIFO_FULL BIT(7)
87 #define QUP_OP_OUT_FIFO_FULL BIT(6)
88 #define QUP_OP_IN_FIFO_NOT_EMPTY BIT(5)
89 #define QUP_OP_OUT_FIFO_NOT_EMPTY BIT(4)
91 /* QUP_ERROR_FLAGS and QUP_ERROR_FLAGS_EN fields */
92 #define QUP_ERROR_OUTPUT_OVER_RUN BIT(5)
93 #define QUP_ERROR_INPUT_UNDER_RUN BIT(4)
94 #define QUP_ERROR_OUTPUT_UNDER_RUN BIT(3)
95 #define QUP_ERROR_INPUT_OVER_RUN BIT(2)
97 /* SPI_CONFIG fields */
98 #define SPI_CONFIG_HS_MODE BIT(10)
99 #define SPI_CONFIG_INPUT_FIRST BIT(9)
100 #define SPI_CONFIG_LOOPBACK BIT(8)
102 /* SPI_IO_CONTROL fields */
103 #define SPI_IO_C_FORCE_CS BIT(11)
104 #define SPI_IO_C_CLK_IDLE_HIGH BIT(10)
105 #define SPI_IO_C_MX_CS_MODE BIT(8)
106 #define SPI_IO_C_CS_N_POLARITY_0 BIT(4)
107 #define SPI_IO_C_CS_SELECT(x) (((x) & 3) << 2)
108 #define SPI_IO_C_CS_SELECT_MASK 0x000c
109 #define SPI_IO_C_TRISTATE_CS BIT(1)
110 #define SPI_IO_C_NO_TRI_STATE BIT(0)
112 /* SPI_ERROR_FLAGS and SPI_ERROR_FLAGS_EN fields */
113 #define SPI_ERROR_CLK_OVER_RUN BIT(1)
114 #define SPI_ERROR_CLK_UNDER_RUN BIT(0)
116 #define SPI_NUM_CHIPSELECTS 4
118 #define SPI_MAX_XFER (SZ_64K - 64)
120 /* high speed mode is when bus rate is greater then 26MHz */
121 #define SPI_HS_MIN_RATE 26000000
122 #define SPI_MAX_RATE 50000000
124 #define SPI_DELAY_THRESHOLD 1
125 #define SPI_DELAY_RETRY 10
127 #define SPI_BUS_WIDTH 8
132 struct clk
*cclk
; /* core clock */
133 struct clk
*iclk
; /* interface clock */
134 struct icc_path
*icc_path
; /* interconnect to RAM */
143 struct spi_transfer
*xfer
;
144 struct completion done
;
146 int w_size
; /* bytes per SPI word */
155 struct dma_slave_config rx_conf
;
156 struct dma_slave_config tx_conf
;
161 static int spi_qup_io_config(struct spi_device
*spi
, struct spi_transfer
*xfer
);
163 static inline bool spi_qup_is_flag_set(struct spi_qup
*controller
, u32 flag
)
165 u32 opflag
= readl_relaxed(controller
->base
+ QUP_OPERATIONAL
);
167 return (opflag
& flag
) != 0;
170 static inline bool spi_qup_is_dma_xfer(int mode
)
172 if (mode
== QUP_IO_M_MODE_DMOV
|| mode
== QUP_IO_M_MODE_BAM
)
178 /* get's the transaction size length */
179 static inline unsigned int spi_qup_len(struct spi_qup
*controller
)
181 return controller
->n_words
* controller
->w_size
;
184 static inline bool spi_qup_is_valid_state(struct spi_qup
*controller
)
186 u32 opstate
= readl_relaxed(controller
->base
+ QUP_STATE
);
188 return opstate
& QUP_STATE_VALID
;
191 static int spi_qup_vote_bw(struct spi_qup
*controller
, u32 speed_hz
)
196 if (controller
->bw_speed_hz
== speed_hz
)
199 needed_peak_bw
= Bps_to_icc(speed_hz
* SPI_BUS_WIDTH
);
200 ret
= icc_set_bw(controller
->icc_path
, 0, needed_peak_bw
);
204 controller
->bw_speed_hz
= speed_hz
;
208 static int spi_qup_set_state(struct spi_qup
*controller
, u32 state
)
214 while (!spi_qup_is_valid_state(controller
)) {
216 usleep_range(SPI_DELAY_THRESHOLD
, SPI_DELAY_THRESHOLD
* 2);
218 if (++loop
> SPI_DELAY_RETRY
)
223 dev_dbg(controller
->dev
, "invalid state for %ld,us %d\n",
226 cur_state
= readl_relaxed(controller
->base
+ QUP_STATE
);
228 * Per spec: for PAUSE_STATE to RESET_STATE, two writes
229 * of (b10) are required
231 if (((cur_state
& QUP_STATE_MASK
) == QUP_STATE_PAUSE
) &&
232 (state
== QUP_STATE_RESET
)) {
233 writel_relaxed(QUP_STATE_CLEAR
, controller
->base
+ QUP_STATE
);
234 writel_relaxed(QUP_STATE_CLEAR
, controller
->base
+ QUP_STATE
);
236 cur_state
&= ~QUP_STATE_MASK
;
238 writel_relaxed(cur_state
, controller
->base
+ QUP_STATE
);
242 while (!spi_qup_is_valid_state(controller
)) {
244 usleep_range(SPI_DELAY_THRESHOLD
, SPI_DELAY_THRESHOLD
* 2);
246 if (++loop
> SPI_DELAY_RETRY
)
253 static void spi_qup_read_from_fifo(struct spi_qup
*controller
, u32 num_words
)
255 u8
*rx_buf
= controller
->rx_buf
;
256 int i
, shift
, num_bytes
;
259 for (; num_words
; num_words
--) {
261 word
= readl_relaxed(controller
->base
+ QUP_INPUT_FIFO
);
263 num_bytes
= min_t(int, spi_qup_len(controller
) -
264 controller
->rx_bytes
,
268 controller
->rx_bytes
+= num_bytes
;
272 for (i
= 0; i
< num_bytes
; i
++, controller
->rx_bytes
++) {
274 * The data format depends on bytes per SPI word:
275 * 4 bytes: 0x12345678
276 * 2 bytes: 0x00001234
277 * 1 byte : 0x00000012
279 shift
= BITS_PER_BYTE
;
280 shift
*= (controller
->w_size
- i
- 1);
281 rx_buf
[controller
->rx_bytes
] = word
>> shift
;
286 static void spi_qup_read(struct spi_qup
*controller
, u32
*opflags
)
288 u32 remainder
, words_per_block
, num_words
;
289 bool is_block_mode
= controller
->mode
== QUP_IO_M_MODE_BLOCK
;
291 remainder
= DIV_ROUND_UP(spi_qup_len(controller
) - controller
->rx_bytes
,
293 words_per_block
= controller
->in_blk_sz
>> 2;
296 /* ACK by clearing service flag */
297 writel_relaxed(QUP_OP_IN_SERVICE_FLAG
,
298 controller
->base
+ QUP_OPERATIONAL
);
304 num_words
= (remainder
> words_per_block
) ?
305 words_per_block
: remainder
;
307 if (!spi_qup_is_flag_set(controller
,
308 QUP_OP_IN_FIFO_NOT_EMPTY
))
314 /* read up to the maximum transfer size available */
315 spi_qup_read_from_fifo(controller
, num_words
);
317 remainder
-= num_words
;
319 /* if block mode, check to see if next block is available */
320 if (is_block_mode
&& !spi_qup_is_flag_set(controller
,
321 QUP_OP_IN_BLOCK_READ_REQ
))
327 * Due to extra stickiness of the QUP_OP_IN_SERVICE_FLAG during block
328 * reads, it has to be cleared again at the very end. However, be sure
329 * to refresh opflags value because MAX_INPUT_DONE_FLAG may now be
330 * present and this is used to determine if transaction is complete
334 *opflags
= readl_relaxed(controller
->base
+ QUP_OPERATIONAL
);
335 if (is_block_mode
&& *opflags
& QUP_OP_MAX_INPUT_DONE_FLAG
)
336 writel_relaxed(QUP_OP_IN_SERVICE_FLAG
,
337 controller
->base
+ QUP_OPERATIONAL
);
341 static void spi_qup_write_to_fifo(struct spi_qup
*controller
, u32 num_words
)
343 const u8
*tx_buf
= controller
->tx_buf
;
347 for (; num_words
; num_words
--) {
350 num_bytes
= min_t(int, spi_qup_len(controller
) -
351 controller
->tx_bytes
,
354 for (i
= 0; i
< num_bytes
; i
++) {
355 data
= tx_buf
[controller
->tx_bytes
+ i
];
356 word
|= data
<< (BITS_PER_BYTE
* (3 - i
));
359 controller
->tx_bytes
+= num_bytes
;
361 writel_relaxed(word
, controller
->base
+ QUP_OUTPUT_FIFO
);
365 static void spi_qup_dma_done(void *data
)
367 struct spi_qup
*qup
= data
;
369 complete(&qup
->done
);
372 static void spi_qup_write(struct spi_qup
*controller
)
374 bool is_block_mode
= controller
->mode
== QUP_IO_M_MODE_BLOCK
;
375 u32 remainder
, words_per_block
, num_words
;
377 remainder
= DIV_ROUND_UP(spi_qup_len(controller
) - controller
->tx_bytes
,
379 words_per_block
= controller
->out_blk_sz
>> 2;
382 /* ACK by clearing service flag */
383 writel_relaxed(QUP_OP_OUT_SERVICE_FLAG
,
384 controller
->base
+ QUP_OPERATIONAL
);
386 /* make sure the interrupt is valid */
391 num_words
= (remainder
> words_per_block
) ?
392 words_per_block
: remainder
;
394 if (spi_qup_is_flag_set(controller
,
395 QUP_OP_OUT_FIFO_FULL
))
401 spi_qup_write_to_fifo(controller
, num_words
);
403 remainder
-= num_words
;
405 /* if block mode, check to see if next block is available */
406 if (is_block_mode
&& !spi_qup_is_flag_set(controller
,
407 QUP_OP_OUT_BLOCK_WRITE_REQ
))
413 static int spi_qup_prep_sg(struct spi_controller
*host
, struct scatterlist
*sgl
,
414 unsigned int nents
, enum dma_transfer_direction dir
,
415 dma_async_tx_callback callback
)
417 struct spi_qup
*qup
= spi_controller_get_devdata(host
);
418 unsigned long flags
= DMA_PREP_INTERRUPT
| DMA_PREP_FENCE
;
419 struct dma_async_tx_descriptor
*desc
;
420 struct dma_chan
*chan
;
423 if (dir
== DMA_MEM_TO_DEV
)
428 desc
= dmaengine_prep_slave_sg(chan
, sgl
, nents
, dir
, flags
);
429 if (IS_ERR_OR_NULL(desc
))
430 return desc
? PTR_ERR(desc
) : -EINVAL
;
432 desc
->callback
= callback
;
433 desc
->callback_param
= qup
;
435 cookie
= dmaengine_submit(desc
);
437 return dma_submit_error(cookie
);
440 static void spi_qup_dma_terminate(struct spi_controller
*host
,
441 struct spi_transfer
*xfer
)
444 dmaengine_terminate_all(host
->dma_tx
);
446 dmaengine_terminate_all(host
->dma_rx
);
449 static u32
spi_qup_sgl_get_nents_len(struct scatterlist
*sgl
, u32 max
,
452 struct scatterlist
*sg
;
455 for (sg
= sgl
; sg
; sg
= sg_next(sg
)) {
456 unsigned int len
= sg_dma_len(sg
);
458 /* check for overflow as well as limit */
459 if (((total
+ len
) < total
) || ((total
+ len
) > max
))
469 static int spi_qup_do_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
,
470 unsigned long timeout
)
472 dma_async_tx_callback rx_done
= NULL
, tx_done
= NULL
;
473 struct spi_controller
*host
= spi
->controller
;
474 struct spi_qup
*qup
= spi_controller_get_devdata(host
);
475 struct scatterlist
*tx_sgl
, *rx_sgl
;
478 ret
= spi_qup_vote_bw(qup
, xfer
->speed_hz
);
480 dev_err(qup
->dev
, "fail to vote for ICC bandwidth: %d\n", ret
);
485 rx_done
= spi_qup_dma_done
;
486 else if (xfer
->tx_buf
)
487 tx_done
= spi_qup_dma_done
;
489 rx_sgl
= xfer
->rx_sg
.sgl
;
490 tx_sgl
= xfer
->tx_sg
.sgl
;
493 u32 rx_nents
= 0, tx_nents
= 0;
496 qup
->n_words
= spi_qup_sgl_get_nents_len(rx_sgl
,
497 SPI_MAX_XFER
, &rx_nents
) / qup
->w_size
;
499 qup
->n_words
= spi_qup_sgl_get_nents_len(tx_sgl
,
500 SPI_MAX_XFER
, &tx_nents
) / qup
->w_size
;
504 ret
= spi_qup_io_config(spi
, xfer
);
508 /* before issuing the descriptors, set the QUP to run */
509 ret
= spi_qup_set_state(qup
, QUP_STATE_RUN
);
511 dev_warn(qup
->dev
, "cannot set RUN state\n");
515 ret
= spi_qup_prep_sg(host
, rx_sgl
, rx_nents
,
516 DMA_DEV_TO_MEM
, rx_done
);
519 dma_async_issue_pending(host
->dma_rx
);
523 ret
= spi_qup_prep_sg(host
, tx_sgl
, tx_nents
,
524 DMA_MEM_TO_DEV
, tx_done
);
528 dma_async_issue_pending(host
->dma_tx
);
531 if (!wait_for_completion_timeout(&qup
->done
, timeout
))
534 for (; rx_sgl
&& rx_nents
--; rx_sgl
= sg_next(rx_sgl
))
536 for (; tx_sgl
&& tx_nents
--; tx_sgl
= sg_next(tx_sgl
))
539 } while (rx_sgl
|| tx_sgl
);
544 static int spi_qup_do_pio(struct spi_device
*spi
, struct spi_transfer
*xfer
,
545 unsigned long timeout
)
547 struct spi_controller
*host
= spi
->controller
;
548 struct spi_qup
*qup
= spi_controller_get_devdata(host
);
549 int ret
, n_words
, iterations
, offset
= 0;
551 n_words
= qup
->n_words
;
552 iterations
= n_words
/ SPI_MAX_XFER
; /* round down */
553 qup
->rx_buf
= xfer
->rx_buf
;
554 qup
->tx_buf
= xfer
->tx_buf
;
558 qup
->n_words
= SPI_MAX_XFER
;
560 qup
->n_words
= n_words
% SPI_MAX_XFER
;
562 if (qup
->tx_buf
&& offset
)
563 qup
->tx_buf
= xfer
->tx_buf
+ offset
* SPI_MAX_XFER
;
565 if (qup
->rx_buf
&& offset
)
566 qup
->rx_buf
= xfer
->rx_buf
+ offset
* SPI_MAX_XFER
;
569 * if the transaction is small enough, we need
570 * to fallback to FIFO mode
572 if (qup
->n_words
<= (qup
->in_fifo_sz
/ sizeof(u32
)))
573 qup
->mode
= QUP_IO_M_MODE_FIFO
;
575 ret
= spi_qup_io_config(spi
, xfer
);
579 ret
= spi_qup_set_state(qup
, QUP_STATE_RUN
);
581 dev_warn(qup
->dev
, "cannot set RUN state\n");
585 ret
= spi_qup_set_state(qup
, QUP_STATE_PAUSE
);
587 dev_warn(qup
->dev
, "cannot set PAUSE state\n");
591 if (qup
->mode
== QUP_IO_M_MODE_FIFO
)
594 ret
= spi_qup_set_state(qup
, QUP_STATE_RUN
);
596 dev_warn(qup
->dev
, "cannot set RUN state\n");
600 if (!wait_for_completion_timeout(&qup
->done
, timeout
))
604 } while (iterations
--);
609 static bool spi_qup_data_pending(struct spi_qup
*controller
)
611 unsigned int remainder_tx
, remainder_rx
;
613 remainder_tx
= DIV_ROUND_UP(spi_qup_len(controller
) -
614 controller
->tx_bytes
, controller
->w_size
);
616 remainder_rx
= DIV_ROUND_UP(spi_qup_len(controller
) -
617 controller
->rx_bytes
, controller
->w_size
);
619 return remainder_tx
|| remainder_rx
;
622 static irqreturn_t
spi_qup_qup_irq(int irq
, void *dev_id
)
624 struct spi_qup
*controller
= dev_id
;
625 u32 opflags
, qup_err
, spi_err
;
628 qup_err
= readl_relaxed(controller
->base
+ QUP_ERROR_FLAGS
);
629 spi_err
= readl_relaxed(controller
->base
+ SPI_ERROR_FLAGS
);
630 opflags
= readl_relaxed(controller
->base
+ QUP_OPERATIONAL
);
632 writel_relaxed(qup_err
, controller
->base
+ QUP_ERROR_FLAGS
);
633 writel_relaxed(spi_err
, controller
->base
+ SPI_ERROR_FLAGS
);
636 if (qup_err
& QUP_ERROR_OUTPUT_OVER_RUN
)
637 dev_warn(controller
->dev
, "OUTPUT_OVER_RUN\n");
638 if (qup_err
& QUP_ERROR_INPUT_UNDER_RUN
)
639 dev_warn(controller
->dev
, "INPUT_UNDER_RUN\n");
640 if (qup_err
& QUP_ERROR_OUTPUT_UNDER_RUN
)
641 dev_warn(controller
->dev
, "OUTPUT_UNDER_RUN\n");
642 if (qup_err
& QUP_ERROR_INPUT_OVER_RUN
)
643 dev_warn(controller
->dev
, "INPUT_OVER_RUN\n");
649 if (spi_err
& SPI_ERROR_CLK_OVER_RUN
)
650 dev_warn(controller
->dev
, "CLK_OVER_RUN\n");
651 if (spi_err
& SPI_ERROR_CLK_UNDER_RUN
)
652 dev_warn(controller
->dev
, "CLK_UNDER_RUN\n");
657 spin_lock(&controller
->lock
);
658 if (!controller
->error
)
659 controller
->error
= error
;
660 spin_unlock(&controller
->lock
);
662 if (spi_qup_is_dma_xfer(controller
->mode
)) {
663 writel_relaxed(opflags
, controller
->base
+ QUP_OPERATIONAL
);
665 if (opflags
& QUP_OP_IN_SERVICE_FLAG
)
666 spi_qup_read(controller
, &opflags
);
668 if (opflags
& QUP_OP_OUT_SERVICE_FLAG
)
669 spi_qup_write(controller
);
671 if (!spi_qup_data_pending(controller
))
672 complete(&controller
->done
);
676 complete(&controller
->done
);
678 if (opflags
& QUP_OP_MAX_INPUT_DONE_FLAG
) {
679 if (!spi_qup_is_dma_xfer(controller
->mode
)) {
680 if (spi_qup_data_pending(controller
))
683 complete(&controller
->done
);
689 /* set clock freq ... bits per word, determine mode */
690 static int spi_qup_io_prep(struct spi_device
*spi
, struct spi_transfer
*xfer
)
692 struct spi_qup
*controller
= spi_controller_get_devdata(spi
->controller
);
695 if (spi
->mode
& SPI_LOOP
&& xfer
->len
> controller
->in_fifo_sz
) {
696 dev_err(controller
->dev
, "too big size for loopback %d > %d\n",
697 xfer
->len
, controller
->in_fifo_sz
);
701 ret
= dev_pm_opp_set_rate(controller
->dev
, xfer
->speed_hz
);
703 dev_err(controller
->dev
, "fail to set frequency %d",
708 controller
->w_size
= DIV_ROUND_UP(xfer
->bits_per_word
, 8);
709 controller
->n_words
= xfer
->len
/ controller
->w_size
;
711 if (controller
->n_words
<= (controller
->in_fifo_sz
/ sizeof(u32
)))
712 controller
->mode
= QUP_IO_M_MODE_FIFO
;
713 else if (spi_xfer_is_dma_mapped(spi
->controller
, spi
, xfer
))
714 controller
->mode
= QUP_IO_M_MODE_BAM
;
716 controller
->mode
= QUP_IO_M_MODE_BLOCK
;
721 /* prep qup for another spi transaction of specific type */
722 static int spi_qup_io_config(struct spi_device
*spi
, struct spi_transfer
*xfer
)
724 struct spi_qup
*controller
= spi_controller_get_devdata(spi
->controller
);
725 u32 config
, iomode
, control
;
728 spin_lock_irqsave(&controller
->lock
, flags
);
729 controller
->xfer
= xfer
;
730 controller
->error
= 0;
731 controller
->rx_bytes
= 0;
732 controller
->tx_bytes
= 0;
733 spin_unlock_irqrestore(&controller
->lock
, flags
);
736 if (spi_qup_set_state(controller
, QUP_STATE_RESET
)) {
737 dev_err(controller
->dev
, "cannot set RESET state\n");
741 switch (controller
->mode
) {
742 case QUP_IO_M_MODE_FIFO
:
743 writel_relaxed(controller
->n_words
,
744 controller
->base
+ QUP_MX_READ_CNT
);
745 writel_relaxed(controller
->n_words
,
746 controller
->base
+ QUP_MX_WRITE_CNT
);
747 /* must be zero for FIFO */
748 writel_relaxed(0, controller
->base
+ QUP_MX_INPUT_CNT
);
749 writel_relaxed(0, controller
->base
+ QUP_MX_OUTPUT_CNT
);
751 case QUP_IO_M_MODE_BAM
:
752 writel_relaxed(controller
->n_words
,
753 controller
->base
+ QUP_MX_INPUT_CNT
);
754 writel_relaxed(controller
->n_words
,
755 controller
->base
+ QUP_MX_OUTPUT_CNT
);
756 /* must be zero for BLOCK and BAM */
757 writel_relaxed(0, controller
->base
+ QUP_MX_READ_CNT
);
758 writel_relaxed(0, controller
->base
+ QUP_MX_WRITE_CNT
);
760 if (!controller
->qup_v1
) {
761 void __iomem
*input_cnt
;
763 input_cnt
= controller
->base
+ QUP_MX_INPUT_CNT
;
765 * for DMA transfers, both QUP_MX_INPUT_CNT and
766 * QUP_MX_OUTPUT_CNT must be zero to all cases but one.
767 * That case is a non-balanced transfer when there is
771 writel_relaxed(0, input_cnt
);
773 writel_relaxed(controller
->n_words
, input_cnt
);
775 writel_relaxed(0, controller
->base
+ QUP_MX_OUTPUT_CNT
);
778 case QUP_IO_M_MODE_BLOCK
:
779 reinit_completion(&controller
->done
);
780 writel_relaxed(controller
->n_words
,
781 controller
->base
+ QUP_MX_INPUT_CNT
);
782 writel_relaxed(controller
->n_words
,
783 controller
->base
+ QUP_MX_OUTPUT_CNT
);
784 /* must be zero for BLOCK and BAM */
785 writel_relaxed(0, controller
->base
+ QUP_MX_READ_CNT
);
786 writel_relaxed(0, controller
->base
+ QUP_MX_WRITE_CNT
);
789 dev_err(controller
->dev
, "unknown mode = %d\n",
794 iomode
= readl_relaxed(controller
->base
+ QUP_IO_M_MODES
);
795 /* Set input and output transfer mode */
796 iomode
&= ~(QUP_IO_M_INPUT_MODE_MASK
| QUP_IO_M_OUTPUT_MODE_MASK
);
798 if (!spi_qup_is_dma_xfer(controller
->mode
))
799 iomode
&= ~(QUP_IO_M_PACK_EN
| QUP_IO_M_UNPACK_EN
);
801 iomode
|= QUP_IO_M_PACK_EN
| QUP_IO_M_UNPACK_EN
;
803 iomode
|= (controller
->mode
<< QUP_IO_M_OUTPUT_MODE_MASK_SHIFT
);
804 iomode
|= (controller
->mode
<< QUP_IO_M_INPUT_MODE_MASK_SHIFT
);
806 writel_relaxed(iomode
, controller
->base
+ QUP_IO_M_MODES
);
808 control
= readl_relaxed(controller
->base
+ SPI_IO_CONTROL
);
810 if (spi
->mode
& SPI_CPOL
)
811 control
|= SPI_IO_C_CLK_IDLE_HIGH
;
813 control
&= ~SPI_IO_C_CLK_IDLE_HIGH
;
815 writel_relaxed(control
, controller
->base
+ SPI_IO_CONTROL
);
817 config
= readl_relaxed(controller
->base
+ SPI_CONFIG
);
819 if (spi
->mode
& SPI_LOOP
)
820 config
|= SPI_CONFIG_LOOPBACK
;
822 config
&= ~SPI_CONFIG_LOOPBACK
;
824 if (spi
->mode
& SPI_CPHA
)
825 config
&= ~SPI_CONFIG_INPUT_FIRST
;
827 config
|= SPI_CONFIG_INPUT_FIRST
;
830 * HS_MODE improves signal stability for spi-clk high rates,
831 * but is invalid in loop back mode.
833 if ((xfer
->speed_hz
>= SPI_HS_MIN_RATE
) && !(spi
->mode
& SPI_LOOP
))
834 config
|= SPI_CONFIG_HS_MODE
;
836 config
&= ~SPI_CONFIG_HS_MODE
;
838 writel_relaxed(config
, controller
->base
+ SPI_CONFIG
);
840 config
= readl_relaxed(controller
->base
+ QUP_CONFIG
);
841 config
&= ~(QUP_CONFIG_NO_INPUT
| QUP_CONFIG_NO_OUTPUT
| QUP_CONFIG_N
);
842 config
|= xfer
->bits_per_word
- 1;
843 config
|= QUP_CONFIG_SPI_MODE
;
845 if (spi_qup_is_dma_xfer(controller
->mode
)) {
847 config
|= QUP_CONFIG_NO_OUTPUT
;
849 config
|= QUP_CONFIG_NO_INPUT
;
852 writel_relaxed(config
, controller
->base
+ QUP_CONFIG
);
854 /* only write to OPERATIONAL_MASK when register is present */
855 if (!controller
->qup_v1
) {
859 * mask INPUT and OUTPUT service flags to prevent IRQs on FIFO
860 * status change in BAM mode
863 if (spi_qup_is_dma_xfer(controller
->mode
))
864 mask
= QUP_OP_IN_SERVICE_FLAG
| QUP_OP_OUT_SERVICE_FLAG
;
866 writel_relaxed(mask
, controller
->base
+ QUP_OPERATIONAL_MASK
);
872 static int spi_qup_transfer_one(struct spi_controller
*host
,
873 struct spi_device
*spi
,
874 struct spi_transfer
*xfer
)
876 struct spi_qup
*controller
= spi_controller_get_devdata(host
);
877 unsigned long timeout
, flags
;
880 ret
= spi_qup_io_prep(spi
, xfer
);
884 timeout
= DIV_ROUND_UP(xfer
->speed_hz
, MSEC_PER_SEC
);
885 timeout
= DIV_ROUND_UP(min_t(unsigned long, SPI_MAX_XFER
,
886 xfer
->len
) * 8, timeout
);
887 timeout
= 100 * msecs_to_jiffies(timeout
);
889 reinit_completion(&controller
->done
);
891 spin_lock_irqsave(&controller
->lock
, flags
);
892 controller
->xfer
= xfer
;
893 controller
->error
= 0;
894 controller
->rx_bytes
= 0;
895 controller
->tx_bytes
= 0;
896 spin_unlock_irqrestore(&controller
->lock
, flags
);
898 if (spi_qup_is_dma_xfer(controller
->mode
))
899 ret
= spi_qup_do_dma(spi
, xfer
, timeout
);
901 ret
= spi_qup_do_pio(spi
, xfer
, timeout
);
903 spi_qup_set_state(controller
, QUP_STATE_RESET
);
904 spin_lock_irqsave(&controller
->lock
, flags
);
906 ret
= controller
->error
;
907 spin_unlock_irqrestore(&controller
->lock
, flags
);
909 if (ret
&& spi_qup_is_dma_xfer(controller
->mode
))
910 spi_qup_dma_terminate(host
, xfer
);
915 static bool spi_qup_can_dma(struct spi_controller
*host
, struct spi_device
*spi
,
916 struct spi_transfer
*xfer
)
918 struct spi_qup
*qup
= spi_controller_get_devdata(host
);
919 size_t dma_align
= dma_get_cache_alignment();
923 if (!IS_ALIGNED((size_t)xfer
->rx_buf
, dma_align
) ||
924 IS_ERR_OR_NULL(host
->dma_rx
))
926 if (qup
->qup_v1
&& (xfer
->len
% qup
->in_blk_sz
))
931 if (!IS_ALIGNED((size_t)xfer
->tx_buf
, dma_align
) ||
932 IS_ERR_OR_NULL(host
->dma_tx
))
934 if (qup
->qup_v1
&& (xfer
->len
% qup
->out_blk_sz
))
938 n_words
= xfer
->len
/ DIV_ROUND_UP(xfer
->bits_per_word
, 8);
939 if (n_words
<= (qup
->in_fifo_sz
/ sizeof(u32
)))
945 static void spi_qup_release_dma(struct spi_controller
*host
)
947 if (!IS_ERR_OR_NULL(host
->dma_rx
))
948 dma_release_channel(host
->dma_rx
);
949 if (!IS_ERR_OR_NULL(host
->dma_tx
))
950 dma_release_channel(host
->dma_tx
);
953 static int spi_qup_init_dma(struct spi_controller
*host
, resource_size_t base
)
955 struct spi_qup
*spi
= spi_controller_get_devdata(host
);
956 struct dma_slave_config
*rx_conf
= &spi
->rx_conf
,
957 *tx_conf
= &spi
->tx_conf
;
958 struct device
*dev
= spi
->dev
;
961 /* allocate dma resources, if available */
962 host
->dma_rx
= dma_request_chan(dev
, "rx");
963 if (IS_ERR(host
->dma_rx
))
964 return PTR_ERR(host
->dma_rx
);
966 host
->dma_tx
= dma_request_chan(dev
, "tx");
967 if (IS_ERR(host
->dma_tx
)) {
968 ret
= PTR_ERR(host
->dma_tx
);
972 /* set DMA parameters */
973 rx_conf
->direction
= DMA_DEV_TO_MEM
;
974 rx_conf
->device_fc
= 1;
975 rx_conf
->src_addr
= base
+ QUP_INPUT_FIFO
;
976 rx_conf
->src_maxburst
= spi
->in_blk_sz
;
978 tx_conf
->direction
= DMA_MEM_TO_DEV
;
979 tx_conf
->device_fc
= 1;
980 tx_conf
->dst_addr
= base
+ QUP_OUTPUT_FIFO
;
981 tx_conf
->dst_maxburst
= spi
->out_blk_sz
;
983 ret
= dmaengine_slave_config(host
->dma_rx
, rx_conf
);
985 dev_err(dev
, "failed to configure RX channel\n");
989 ret
= dmaengine_slave_config(host
->dma_tx
, tx_conf
);
991 dev_err(dev
, "failed to configure TX channel\n");
998 dma_release_channel(host
->dma_tx
);
1000 dma_release_channel(host
->dma_rx
);
1004 static void spi_qup_set_cs(struct spi_device
*spi
, bool val
)
1006 struct spi_qup
*controller
;
1010 controller
= spi_controller_get_devdata(spi
->controller
);
1011 spi_ioc
= readl_relaxed(controller
->base
+ SPI_IO_CONTROL
);
1012 spi_ioc_orig
= spi_ioc
;
1014 spi_ioc
|= SPI_IO_C_FORCE_CS
;
1016 spi_ioc
&= ~SPI_IO_C_FORCE_CS
;
1018 if (spi_ioc
!= spi_ioc_orig
)
1019 writel_relaxed(spi_ioc
, controller
->base
+ SPI_IO_CONTROL
);
1022 static int spi_qup_probe(struct platform_device
*pdev
)
1024 struct spi_controller
*host
;
1025 struct icc_path
*icc_path
;
1026 struct clk
*iclk
, *cclk
;
1027 struct spi_qup
*controller
;
1028 struct resource
*res
;
1031 u32 max_freq
, iomode
, num_cs
;
1035 base
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
1037 return PTR_ERR(base
);
1039 irq
= platform_get_irq(pdev
, 0);
1043 cclk
= devm_clk_get(dev
, "core");
1045 return PTR_ERR(cclk
);
1047 iclk
= devm_clk_get(dev
, "iface");
1049 return PTR_ERR(iclk
);
1051 icc_path
= devm_of_icc_get(dev
, NULL
);
1052 if (IS_ERR(icc_path
))
1053 return dev_err_probe(dev
, PTR_ERR(icc_path
),
1054 "failed to get interconnect path\n");
1056 /* This is optional parameter */
1057 if (of_property_read_u32(dev
->of_node
, "spi-max-frequency", &max_freq
))
1058 max_freq
= SPI_MAX_RATE
;
1060 if (!max_freq
|| max_freq
> SPI_MAX_RATE
) {
1061 dev_err(dev
, "invalid clock frequency %d\n", max_freq
);
1065 ret
= devm_pm_opp_set_clkname(dev
, "core");
1069 /* OPP table is optional */
1070 ret
= devm_pm_opp_of_add_table(dev
);
1071 if (ret
&& ret
!= -ENODEV
)
1072 return dev_err_probe(dev
, ret
, "invalid OPP table\n");
1074 host
= spi_alloc_host(dev
, sizeof(struct spi_qup
));
1076 dev_err(dev
, "cannot allocate host\n");
1080 /* use num-cs unless not present or out of range */
1081 if (of_property_read_u32(dev
->of_node
, "num-cs", &num_cs
) ||
1082 num_cs
> SPI_NUM_CHIPSELECTS
)
1083 host
->num_chipselect
= SPI_NUM_CHIPSELECTS
;
1085 host
->num_chipselect
= num_cs
;
1087 host
->use_gpio_descriptors
= true;
1088 host
->max_native_cs
= SPI_NUM_CHIPSELECTS
;
1089 host
->bus_num
= pdev
->id
;
1090 host
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
| SPI_LOOP
;
1091 host
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 32);
1092 host
->max_speed_hz
= max_freq
;
1093 host
->transfer_one
= spi_qup_transfer_one
;
1094 host
->dev
.of_node
= pdev
->dev
.of_node
;
1095 host
->auto_runtime_pm
= true;
1096 host
->dma_alignment
= dma_get_cache_alignment();
1097 host
->max_dma_len
= SPI_MAX_XFER
;
1099 platform_set_drvdata(pdev
, host
);
1101 controller
= spi_controller_get_devdata(host
);
1103 controller
->dev
= dev
;
1104 controller
->base
= base
;
1105 controller
->iclk
= iclk
;
1106 controller
->cclk
= cclk
;
1107 controller
->icc_path
= icc_path
;
1108 controller
->irq
= irq
;
1110 ret
= spi_qup_init_dma(host
, res
->start
);
1111 if (ret
== -EPROBE_DEFER
)
1114 host
->can_dma
= spi_qup_can_dma
;
1116 controller
->qup_v1
= (uintptr_t)of_device_get_match_data(dev
);
1118 if (!controller
->qup_v1
)
1119 host
->set_cs
= spi_qup_set_cs
;
1121 spin_lock_init(&controller
->lock
);
1122 init_completion(&controller
->done
);
1124 ret
= clk_prepare_enable(cclk
);
1126 dev_err(dev
, "cannot enable core clock\n");
1130 ret
= clk_prepare_enable(iclk
);
1132 clk_disable_unprepare(cclk
);
1133 dev_err(dev
, "cannot enable iface clock\n");
1137 iomode
= readl_relaxed(base
+ QUP_IO_M_MODES
);
1139 size
= QUP_IO_M_OUTPUT_BLOCK_SIZE(iomode
);
1141 controller
->out_blk_sz
= size
* 16;
1143 controller
->out_blk_sz
= 4;
1145 size
= QUP_IO_M_INPUT_BLOCK_SIZE(iomode
);
1147 controller
->in_blk_sz
= size
* 16;
1149 controller
->in_blk_sz
= 4;
1151 size
= QUP_IO_M_OUTPUT_FIFO_SIZE(iomode
);
1152 controller
->out_fifo_sz
= controller
->out_blk_sz
* (2 << size
);
1154 size
= QUP_IO_M_INPUT_FIFO_SIZE(iomode
);
1155 controller
->in_fifo_sz
= controller
->in_blk_sz
* (2 << size
);
1157 dev_info(dev
, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
1158 controller
->in_blk_sz
, controller
->in_fifo_sz
,
1159 controller
->out_blk_sz
, controller
->out_fifo_sz
);
1161 writel_relaxed(1, base
+ QUP_SW_RESET
);
1163 ret
= spi_qup_set_state(controller
, QUP_STATE_RESET
);
1165 dev_err(dev
, "cannot set RESET state\n");
1169 writel_relaxed(0, base
+ QUP_OPERATIONAL
);
1170 writel_relaxed(0, base
+ QUP_IO_M_MODES
);
1172 if (!controller
->qup_v1
)
1173 writel_relaxed(0, base
+ QUP_OPERATIONAL_MASK
);
1175 writel_relaxed(SPI_ERROR_CLK_UNDER_RUN
| SPI_ERROR_CLK_OVER_RUN
,
1176 base
+ SPI_ERROR_FLAGS_EN
);
1178 /* if earlier version of the QUP, disable INPUT_OVERRUN */
1179 if (controller
->qup_v1
)
1180 writel_relaxed(QUP_ERROR_OUTPUT_OVER_RUN
|
1181 QUP_ERROR_INPUT_UNDER_RUN
| QUP_ERROR_OUTPUT_UNDER_RUN
,
1182 base
+ QUP_ERROR_FLAGS_EN
);
1184 writel_relaxed(0, base
+ SPI_CONFIG
);
1185 writel_relaxed(SPI_IO_C_NO_TRI_STATE
, base
+ SPI_IO_CONTROL
);
1187 ret
= devm_request_irq(dev
, irq
, spi_qup_qup_irq
,
1188 IRQF_TRIGGER_HIGH
, pdev
->name
, controller
);
1192 pm_runtime_set_autosuspend_delay(dev
, MSEC_PER_SEC
);
1193 pm_runtime_use_autosuspend(dev
);
1194 pm_runtime_set_active(dev
);
1195 pm_runtime_enable(dev
);
1197 ret
= devm_spi_register_controller(dev
, host
);
1204 pm_runtime_disable(&pdev
->dev
);
1206 clk_disable_unprepare(cclk
);
1207 clk_disable_unprepare(iclk
);
1209 spi_qup_release_dma(host
);
1211 spi_controller_put(host
);
1216 static int spi_qup_pm_suspend_runtime(struct device
*device
)
1218 struct spi_controller
*host
= dev_get_drvdata(device
);
1219 struct spi_qup
*controller
= spi_controller_get_devdata(host
);
1222 /* Enable clocks auto gaiting */
1223 config
= readl(controller
->base
+ QUP_CONFIG
);
1224 config
|= QUP_CONFIG_CLOCK_AUTO_GATE
;
1225 writel_relaxed(config
, controller
->base
+ QUP_CONFIG
);
1227 clk_disable_unprepare(controller
->cclk
);
1228 spi_qup_vote_bw(controller
, 0);
1229 clk_disable_unprepare(controller
->iclk
);
1234 static int spi_qup_pm_resume_runtime(struct device
*device
)
1236 struct spi_controller
*host
= dev_get_drvdata(device
);
1237 struct spi_qup
*controller
= spi_controller_get_devdata(host
);
1241 ret
= clk_prepare_enable(controller
->iclk
);
1245 ret
= clk_prepare_enable(controller
->cclk
);
1247 clk_disable_unprepare(controller
->iclk
);
1251 /* Disable clocks auto gaiting */
1252 config
= readl_relaxed(controller
->base
+ QUP_CONFIG
);
1253 config
&= ~QUP_CONFIG_CLOCK_AUTO_GATE
;
1254 writel_relaxed(config
, controller
->base
+ QUP_CONFIG
);
1257 #endif /* CONFIG_PM */
1259 #ifdef CONFIG_PM_SLEEP
1260 static int spi_qup_suspend(struct device
*device
)
1262 struct spi_controller
*host
= dev_get_drvdata(device
);
1263 struct spi_qup
*controller
= spi_controller_get_devdata(host
);
1266 if (pm_runtime_suspended(device
)) {
1267 ret
= spi_qup_pm_resume_runtime(device
);
1271 ret
= spi_controller_suspend(host
);
1275 ret
= spi_qup_set_state(controller
, QUP_STATE_RESET
);
1279 clk_disable_unprepare(controller
->cclk
);
1280 spi_qup_vote_bw(controller
, 0);
1281 clk_disable_unprepare(controller
->iclk
);
1285 static int spi_qup_resume(struct device
*device
)
1287 struct spi_controller
*host
= dev_get_drvdata(device
);
1288 struct spi_qup
*controller
= spi_controller_get_devdata(host
);
1291 ret
= clk_prepare_enable(controller
->iclk
);
1295 ret
= clk_prepare_enable(controller
->cclk
);
1297 clk_disable_unprepare(controller
->iclk
);
1301 ret
= spi_qup_set_state(controller
, QUP_STATE_RESET
);
1305 ret
= spi_controller_resume(host
);
1312 clk_disable_unprepare(controller
->cclk
);
1313 clk_disable_unprepare(controller
->iclk
);
1316 #endif /* CONFIG_PM_SLEEP */
1318 static void spi_qup_remove(struct platform_device
*pdev
)
1320 struct spi_controller
*host
= dev_get_drvdata(&pdev
->dev
);
1321 struct spi_qup
*controller
= spi_controller_get_devdata(host
);
1324 ret
= pm_runtime_get_sync(&pdev
->dev
);
1327 ret
= spi_qup_set_state(controller
, QUP_STATE_RESET
);
1329 dev_warn(&pdev
->dev
, "failed to reset controller (%pe)\n",
1332 clk_disable_unprepare(controller
->cclk
);
1333 clk_disable_unprepare(controller
->iclk
);
1335 dev_warn(&pdev
->dev
, "failed to resume, skip hw disable (%pe)\n",
1339 spi_qup_release_dma(host
);
1341 pm_runtime_put_noidle(&pdev
->dev
);
1342 pm_runtime_disable(&pdev
->dev
);
1345 static const struct of_device_id spi_qup_dt_match
[] = {
1346 { .compatible
= "qcom,spi-qup-v1.1.1", .data
= (void *)1, },
1347 { .compatible
= "qcom,spi-qup-v2.1.1", },
1348 { .compatible
= "qcom,spi-qup-v2.2.1", },
1351 MODULE_DEVICE_TABLE(of
, spi_qup_dt_match
);
1353 static const struct dev_pm_ops spi_qup_dev_pm_ops
= {
1354 SET_SYSTEM_SLEEP_PM_OPS(spi_qup_suspend
, spi_qup_resume
)
1355 SET_RUNTIME_PM_OPS(spi_qup_pm_suspend_runtime
,
1356 spi_qup_pm_resume_runtime
,
1360 static struct platform_driver spi_qup_driver
= {
1363 .pm
= &spi_qup_dev_pm_ops
,
1364 .of_match_table
= spi_qup_dt_match
,
1366 .probe
= spi_qup_probe
,
1367 .remove
= spi_qup_remove
,
1369 module_platform_driver(spi_qup_driver
);
1371 MODULE_DESCRIPTION("Qualcomm SPI controller with QUP interface");
1372 MODULE_LICENSE("GPL v2");
1373 MODULE_ALIAS("platform:spi_qup");