1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2014, The Linux foundation. All rights reserved.
7 #include <linux/delay.h>
9 #include <linux/interrupt.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/spi/spi.h>
17 #include <linux/dmaengine.h>
18 #include <linux/dma-mapping.h>
20 #define QUP_CONFIG 0x0000
21 #define QUP_STATE 0x0004
22 #define QUP_IO_M_MODES 0x0008
23 #define QUP_SW_RESET 0x000c
24 #define QUP_OPERATIONAL 0x0018
25 #define QUP_ERROR_FLAGS 0x001c
26 #define QUP_ERROR_FLAGS_EN 0x0020
27 #define QUP_OPERATIONAL_MASK 0x0028
28 #define QUP_HW_VERSION 0x0030
29 #define QUP_MX_OUTPUT_CNT 0x0100
30 #define QUP_OUTPUT_FIFO 0x0110
31 #define QUP_MX_WRITE_CNT 0x0150
32 #define QUP_MX_INPUT_CNT 0x0200
33 #define QUP_MX_READ_CNT 0x0208
34 #define QUP_INPUT_FIFO 0x0218
36 #define SPI_CONFIG 0x0300
37 #define SPI_IO_CONTROL 0x0304
38 #define SPI_ERROR_FLAGS 0x0308
39 #define SPI_ERROR_FLAGS_EN 0x030c
41 /* QUP_CONFIG fields */
42 #define QUP_CONFIG_SPI_MODE (1 << 8)
43 #define QUP_CONFIG_CLOCK_AUTO_GATE BIT(13)
44 #define QUP_CONFIG_NO_INPUT BIT(7)
45 #define QUP_CONFIG_NO_OUTPUT BIT(6)
46 #define QUP_CONFIG_N 0x001f
48 /* QUP_STATE fields */
49 #define QUP_STATE_VALID BIT(2)
50 #define QUP_STATE_RESET 0
51 #define QUP_STATE_RUN 1
52 #define QUP_STATE_PAUSE 3
53 #define QUP_STATE_MASK 3
54 #define QUP_STATE_CLEAR 2
56 #define QUP_HW_VERSION_2_1_1 0x20010001
58 /* QUP_IO_M_MODES fields */
59 #define QUP_IO_M_PACK_EN BIT(15)
60 #define QUP_IO_M_UNPACK_EN BIT(14)
61 #define QUP_IO_M_INPUT_MODE_MASK_SHIFT 12
62 #define QUP_IO_M_OUTPUT_MODE_MASK_SHIFT 10
63 #define QUP_IO_M_INPUT_MODE_MASK (3 << QUP_IO_M_INPUT_MODE_MASK_SHIFT)
64 #define QUP_IO_M_OUTPUT_MODE_MASK (3 << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT)
66 #define QUP_IO_M_OUTPUT_BLOCK_SIZE(x) (((x) & (0x03 << 0)) >> 0)
67 #define QUP_IO_M_OUTPUT_FIFO_SIZE(x) (((x) & (0x07 << 2)) >> 2)
68 #define QUP_IO_M_INPUT_BLOCK_SIZE(x) (((x) & (0x03 << 5)) >> 5)
69 #define QUP_IO_M_INPUT_FIFO_SIZE(x) (((x) & (0x07 << 7)) >> 7)
71 #define QUP_IO_M_MODE_FIFO 0
72 #define QUP_IO_M_MODE_BLOCK 1
73 #define QUP_IO_M_MODE_DMOV 2
74 #define QUP_IO_M_MODE_BAM 3
76 /* QUP_OPERATIONAL fields */
77 #define QUP_OP_IN_BLOCK_READ_REQ BIT(13)
78 #define QUP_OP_OUT_BLOCK_WRITE_REQ BIT(12)
79 #define QUP_OP_MAX_INPUT_DONE_FLAG BIT(11)
80 #define QUP_OP_MAX_OUTPUT_DONE_FLAG BIT(10)
81 #define QUP_OP_IN_SERVICE_FLAG BIT(9)
82 #define QUP_OP_OUT_SERVICE_FLAG BIT(8)
83 #define QUP_OP_IN_FIFO_FULL BIT(7)
84 #define QUP_OP_OUT_FIFO_FULL BIT(6)
85 #define QUP_OP_IN_FIFO_NOT_EMPTY BIT(5)
86 #define QUP_OP_OUT_FIFO_NOT_EMPTY BIT(4)
88 /* QUP_ERROR_FLAGS and QUP_ERROR_FLAGS_EN fields */
89 #define QUP_ERROR_OUTPUT_OVER_RUN BIT(5)
90 #define QUP_ERROR_INPUT_UNDER_RUN BIT(4)
91 #define QUP_ERROR_OUTPUT_UNDER_RUN BIT(3)
92 #define QUP_ERROR_INPUT_OVER_RUN BIT(2)
94 /* SPI_CONFIG fields */
95 #define SPI_CONFIG_HS_MODE BIT(10)
96 #define SPI_CONFIG_INPUT_FIRST BIT(9)
97 #define SPI_CONFIG_LOOPBACK BIT(8)
99 /* SPI_IO_CONTROL fields */
100 #define SPI_IO_C_FORCE_CS BIT(11)
101 #define SPI_IO_C_CLK_IDLE_HIGH BIT(10)
102 #define SPI_IO_C_MX_CS_MODE BIT(8)
103 #define SPI_IO_C_CS_N_POLARITY_0 BIT(4)
104 #define SPI_IO_C_CS_SELECT(x) (((x) & 3) << 2)
105 #define SPI_IO_C_CS_SELECT_MASK 0x000c
106 #define SPI_IO_C_TRISTATE_CS BIT(1)
107 #define SPI_IO_C_NO_TRI_STATE BIT(0)
109 /* SPI_ERROR_FLAGS and SPI_ERROR_FLAGS_EN fields */
110 #define SPI_ERROR_CLK_OVER_RUN BIT(1)
111 #define SPI_ERROR_CLK_UNDER_RUN BIT(0)
113 #define SPI_NUM_CHIPSELECTS 4
115 #define SPI_MAX_XFER (SZ_64K - 64)
117 /* high speed mode is when bus rate is greater then 26MHz */
118 #define SPI_HS_MIN_RATE 26000000
119 #define SPI_MAX_RATE 50000000
121 #define SPI_DELAY_THRESHOLD 1
122 #define SPI_DELAY_RETRY 10
127 struct clk *cclk; /* core clock */
128 struct clk *iclk; /* interface clock */
137 struct spi_transfer *xfer;
138 struct completion done;
140 int w_size; /* bytes per SPI word */
149 struct dma_slave_config rx_conf;
150 struct dma_slave_config tx_conf;
153 static int spi_qup_io_config(struct spi_device *spi, struct spi_transfer *xfer);
155 static inline bool spi_qup_is_flag_set(struct spi_qup *controller, u32 flag)
157 u32 opflag = readl_relaxed(controller->base + QUP_OPERATIONAL);
159 return (opflag & flag) != 0;
162 static inline bool spi_qup_is_dma_xfer(int mode)
164 if (mode == QUP_IO_M_MODE_DMOV || mode == QUP_IO_M_MODE_BAM)
170 /* get's the transaction size length */
171 static inline unsigned int spi_qup_len(struct spi_qup *controller)
173 return controller->n_words * controller->w_size;
176 static inline bool spi_qup_is_valid_state(struct spi_qup *controller)
178 u32 opstate = readl_relaxed(controller->base + QUP_STATE);
180 return opstate & QUP_STATE_VALID;
183 static int spi_qup_set_state(struct spi_qup *controller, u32 state)
189 while (!spi_qup_is_valid_state(controller)) {
191 usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2);
193 if (++loop > SPI_DELAY_RETRY)
198 dev_dbg(controller->dev, "invalid state for %ld,us %d\n",
201 cur_state = readl_relaxed(controller->base + QUP_STATE);
203 * Per spec: for PAUSE_STATE to RESET_STATE, two writes
204 * of (b10) are required
206 if (((cur_state & QUP_STATE_MASK) == QUP_STATE_PAUSE) &&
207 (state == QUP_STATE_RESET)) {
208 writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE);
209 writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE);
211 cur_state &= ~QUP_STATE_MASK;
213 writel_relaxed(cur_state, controller->base + QUP_STATE);
217 while (!spi_qup_is_valid_state(controller)) {
219 usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2);
221 if (++loop > SPI_DELAY_RETRY)
228 static void spi_qup_read_from_fifo(struct spi_qup *controller, u32 num_words)
230 u8 *rx_buf = controller->rx_buf;
231 int i, shift, num_bytes;
234 for (; num_words; num_words--) {
236 word = readl_relaxed(controller->base + QUP_INPUT_FIFO);
238 num_bytes = min_t(int, spi_qup_len(controller) -
239 controller->rx_bytes,
243 controller->rx_bytes += num_bytes;
247 for (i = 0; i < num_bytes; i++, controller->rx_bytes++) {
249 * The data format depends on bytes per SPI word:
250 * 4 bytes: 0x12345678
251 * 2 bytes: 0x00001234
252 * 1 byte : 0x00000012
254 shift = BITS_PER_BYTE;
255 shift *= (controller->w_size - i - 1);
256 rx_buf[controller->rx_bytes] = word >> shift;
261 static void spi_qup_read(struct spi_qup *controller, u32 *opflags)
263 u32 remainder, words_per_block, num_words;
264 bool is_block_mode = controller->mode == QUP_IO_M_MODE_BLOCK;
266 remainder = DIV_ROUND_UP(spi_qup_len(controller) - controller->rx_bytes,
268 words_per_block = controller->in_blk_sz >> 2;
271 /* ACK by clearing service flag */
272 writel_relaxed(QUP_OP_IN_SERVICE_FLAG,
273 controller->base + QUP_OPERATIONAL);
279 num_words = (remainder > words_per_block) ?
280 words_per_block : remainder;
282 if (!spi_qup_is_flag_set(controller,
283 QUP_OP_IN_FIFO_NOT_EMPTY))
289 /* read up to the maximum transfer size available */
290 spi_qup_read_from_fifo(controller, num_words);
292 remainder -= num_words;
294 /* if block mode, check to see if next block is available */
295 if (is_block_mode && !spi_qup_is_flag_set(controller,
296 QUP_OP_IN_BLOCK_READ_REQ))
302 * Due to extra stickiness of the QUP_OP_IN_SERVICE_FLAG during block
303 * reads, it has to be cleared again at the very end. However, be sure
304 * to refresh opflags value because MAX_INPUT_DONE_FLAG may now be
305 * present and this is used to determine if transaction is complete
309 *opflags = readl_relaxed(controller->base + QUP_OPERATIONAL);
310 if (is_block_mode && *opflags & QUP_OP_MAX_INPUT_DONE_FLAG)
311 writel_relaxed(QUP_OP_IN_SERVICE_FLAG,
312 controller->base + QUP_OPERATIONAL);
316 static void spi_qup_write_to_fifo(struct spi_qup *controller, u32 num_words)
318 const u8 *tx_buf = controller->tx_buf;
322 for (; num_words; num_words--) {
325 num_bytes = min_t(int, spi_qup_len(controller) -
326 controller->tx_bytes,
329 for (i = 0; i < num_bytes; i++) {
330 data = tx_buf[controller->tx_bytes + i];
331 word |= data << (BITS_PER_BYTE * (3 - i));
334 controller->tx_bytes += num_bytes;
336 writel_relaxed(word, controller->base + QUP_OUTPUT_FIFO);
340 static void spi_qup_dma_done(void *data)
342 struct spi_qup *qup = data;
344 complete(&qup->done);
347 static void spi_qup_write(struct spi_qup *controller)
349 bool is_block_mode = controller->mode == QUP_IO_M_MODE_BLOCK;
350 u32 remainder, words_per_block, num_words;
352 remainder = DIV_ROUND_UP(spi_qup_len(controller) - controller->tx_bytes,
354 words_per_block = controller->out_blk_sz >> 2;
357 /* ACK by clearing service flag */
358 writel_relaxed(QUP_OP_OUT_SERVICE_FLAG,
359 controller->base + QUP_OPERATIONAL);
361 /* make sure the interrupt is valid */
366 num_words = (remainder > words_per_block) ?
367 words_per_block : remainder;
369 if (spi_qup_is_flag_set(controller,
370 QUP_OP_OUT_FIFO_FULL))
376 spi_qup_write_to_fifo(controller, num_words);
378 remainder -= num_words;
380 /* if block mode, check to see if next block is available */
381 if (is_block_mode && !spi_qup_is_flag_set(controller,
382 QUP_OP_OUT_BLOCK_WRITE_REQ))
388 static int spi_qup_prep_sg(struct spi_controller *host, struct scatterlist *sgl,
389 unsigned int nents, enum dma_transfer_direction dir,
390 dma_async_tx_callback callback)
392 struct spi_qup *qup = spi_controller_get_devdata(host);
393 unsigned long flags = DMA_PREP_INTERRUPT | DMA_PREP_FENCE;
394 struct dma_async_tx_descriptor *desc;
395 struct dma_chan *chan;
398 if (dir == DMA_MEM_TO_DEV)
403 desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags);
404 if (IS_ERR_OR_NULL(desc))
405 return desc ? PTR_ERR(desc) : -EINVAL;
407 desc->callback = callback;
408 desc->callback_param = qup;
410 cookie = dmaengine_submit(desc);
412 return dma_submit_error(cookie);
415 static void spi_qup_dma_terminate(struct spi_controller *host,
416 struct spi_transfer *xfer)
419 dmaengine_terminate_all(host->dma_tx);
421 dmaengine_terminate_all(host->dma_rx);
424 static u32 spi_qup_sgl_get_nents_len(struct scatterlist *sgl, u32 max,
427 struct scatterlist *sg;
430 for (sg = sgl; sg; sg = sg_next(sg)) {
431 unsigned int len = sg_dma_len(sg);
433 /* check for overflow as well as limit */
434 if (((total + len) < total) || ((total + len) > max))
444 static int spi_qup_do_dma(struct spi_device *spi, struct spi_transfer *xfer,
445 unsigned long timeout)
447 dma_async_tx_callback rx_done = NULL, tx_done = NULL;
448 struct spi_controller *host = spi->controller;
449 struct spi_qup *qup = spi_controller_get_devdata(host);
450 struct scatterlist *tx_sgl, *rx_sgl;
454 rx_done = spi_qup_dma_done;
455 else if (xfer->tx_buf)
456 tx_done = spi_qup_dma_done;
458 rx_sgl = xfer->rx_sg.sgl;
459 tx_sgl = xfer->tx_sg.sgl;
462 u32 rx_nents = 0, tx_nents = 0;
465 qup->n_words = spi_qup_sgl_get_nents_len(rx_sgl,
466 SPI_MAX_XFER, &rx_nents) / qup->w_size;
468 qup->n_words = spi_qup_sgl_get_nents_len(tx_sgl,
469 SPI_MAX_XFER, &tx_nents) / qup->w_size;
473 ret = spi_qup_io_config(spi, xfer);
477 /* before issuing the descriptors, set the QUP to run */
478 ret = spi_qup_set_state(qup, QUP_STATE_RUN);
480 dev_warn(qup->dev, "cannot set RUN state\n");
484 ret = spi_qup_prep_sg(host, rx_sgl, rx_nents,
485 DMA_DEV_TO_MEM, rx_done);
488 dma_async_issue_pending(host->dma_rx);
492 ret = spi_qup_prep_sg(host, tx_sgl, tx_nents,
493 DMA_MEM_TO_DEV, tx_done);
497 dma_async_issue_pending(host->dma_tx);
500 if (!wait_for_completion_timeout(&qup->done, timeout))
503 for (; rx_sgl && rx_nents--; rx_sgl = sg_next(rx_sgl))
505 for (; tx_sgl && tx_nents--; tx_sgl = sg_next(tx_sgl))
508 } while (rx_sgl || tx_sgl);
513 static int spi_qup_do_pio(struct spi_device *spi, struct spi_transfer *xfer,
514 unsigned long timeout)
516 struct spi_controller *host = spi->controller;
517 struct spi_qup *qup = spi_controller_get_devdata(host);
518 int ret, n_words, iterations, offset = 0;
520 n_words = qup->n_words;
521 iterations = n_words / SPI_MAX_XFER; /* round down */
522 qup->rx_buf = xfer->rx_buf;
523 qup->tx_buf = xfer->tx_buf;
527 qup->n_words = SPI_MAX_XFER;
529 qup->n_words = n_words % SPI_MAX_XFER;
531 if (qup->tx_buf && offset)
532 qup->tx_buf = xfer->tx_buf + offset * SPI_MAX_XFER;
534 if (qup->rx_buf && offset)
535 qup->rx_buf = xfer->rx_buf + offset * SPI_MAX_XFER;
538 * if the transaction is small enough, we need
539 * to fallback to FIFO mode
541 if (qup->n_words <= (qup->in_fifo_sz / sizeof(u32)))
542 qup->mode = QUP_IO_M_MODE_FIFO;
544 ret = spi_qup_io_config(spi, xfer);
548 ret = spi_qup_set_state(qup, QUP_STATE_RUN);
550 dev_warn(qup->dev, "cannot set RUN state\n");
554 ret = spi_qup_set_state(qup, QUP_STATE_PAUSE);
556 dev_warn(qup->dev, "cannot set PAUSE state\n");
560 if (qup->mode == QUP_IO_M_MODE_FIFO)
563 ret = spi_qup_set_state(qup, QUP_STATE_RUN);
565 dev_warn(qup->dev, "cannot set RUN state\n");
569 if (!wait_for_completion_timeout(&qup->done, timeout))
573 } while (iterations--);
578 static bool spi_qup_data_pending(struct spi_qup *controller)
580 unsigned int remainder_tx, remainder_rx;
582 remainder_tx = DIV_ROUND_UP(spi_qup_len(controller) -
583 controller->tx_bytes, controller->w_size);
585 remainder_rx = DIV_ROUND_UP(spi_qup_len(controller) -
586 controller->rx_bytes, controller->w_size);
588 return remainder_tx || remainder_rx;
591 static irqreturn_t spi_qup_qup_irq(int irq, void *dev_id)
593 struct spi_qup *controller = dev_id;
594 u32 opflags, qup_err, spi_err;
597 qup_err = readl_relaxed(controller->base + QUP_ERROR_FLAGS);
598 spi_err = readl_relaxed(controller->base + SPI_ERROR_FLAGS);
599 opflags = readl_relaxed(controller->base + QUP_OPERATIONAL);
601 writel_relaxed(qup_err, controller->base + QUP_ERROR_FLAGS);
602 writel_relaxed(spi_err, controller->base + SPI_ERROR_FLAGS);
605 if (qup_err & QUP_ERROR_OUTPUT_OVER_RUN)
606 dev_warn(controller->dev, "OUTPUT_OVER_RUN\n");
607 if (qup_err & QUP_ERROR_INPUT_UNDER_RUN)
608 dev_warn(controller->dev, "INPUT_UNDER_RUN\n");
609 if (qup_err & QUP_ERROR_OUTPUT_UNDER_RUN)
610 dev_warn(controller->dev, "OUTPUT_UNDER_RUN\n");
611 if (qup_err & QUP_ERROR_INPUT_OVER_RUN)
612 dev_warn(controller->dev, "INPUT_OVER_RUN\n");
618 if (spi_err & SPI_ERROR_CLK_OVER_RUN)
619 dev_warn(controller->dev, "CLK_OVER_RUN\n");
620 if (spi_err & SPI_ERROR_CLK_UNDER_RUN)
621 dev_warn(controller->dev, "CLK_UNDER_RUN\n");
626 spin_lock(&controller->lock);
627 if (!controller->error)
628 controller->error = error;
629 spin_unlock(&controller->lock);
631 if (spi_qup_is_dma_xfer(controller->mode)) {
632 writel_relaxed(opflags, controller->base + QUP_OPERATIONAL);
634 if (opflags & QUP_OP_IN_SERVICE_FLAG)
635 spi_qup_read(controller, &opflags);
637 if (opflags & QUP_OP_OUT_SERVICE_FLAG)
638 spi_qup_write(controller);
640 if (!spi_qup_data_pending(controller))
641 complete(&controller->done);
645 complete(&controller->done);
647 if (opflags & QUP_OP_MAX_INPUT_DONE_FLAG) {
648 if (!spi_qup_is_dma_xfer(controller->mode)) {
649 if (spi_qup_data_pending(controller))
652 complete(&controller->done);
658 /* set clock freq ... bits per word, determine mode */
659 static int spi_qup_io_prep(struct spi_device *spi, struct spi_transfer *xfer)
661 struct spi_qup *controller = spi_controller_get_devdata(spi->controller);
664 if (spi->mode & SPI_LOOP && xfer->len > controller->in_fifo_sz) {
665 dev_err(controller->dev, "too big size for loopback %d > %d\n",
666 xfer->len, controller->in_fifo_sz);
670 ret = clk_set_rate(controller->cclk, xfer->speed_hz);
672 dev_err(controller->dev, "fail to set frequency %d",
677 controller->w_size = DIV_ROUND_UP(xfer->bits_per_word, 8);
678 controller->n_words = xfer->len / controller->w_size;
680 if (controller->n_words <= (controller->in_fifo_sz / sizeof(u32)))
681 controller->mode = QUP_IO_M_MODE_FIFO;
682 else if (spi->controller->can_dma &&
683 spi->controller->can_dma(spi->controller, spi, xfer) &&
684 spi->controller->cur_msg_mapped)
685 controller->mode = QUP_IO_M_MODE_BAM;
687 controller->mode = QUP_IO_M_MODE_BLOCK;
692 /* prep qup for another spi transaction of specific type */
693 static int spi_qup_io_config(struct spi_device *spi, struct spi_transfer *xfer)
695 struct spi_qup *controller = spi_controller_get_devdata(spi->controller);
696 u32 config, iomode, control;
699 spin_lock_irqsave(&controller->lock, flags);
700 controller->xfer = xfer;
701 controller->error = 0;
702 controller->rx_bytes = 0;
703 controller->tx_bytes = 0;
704 spin_unlock_irqrestore(&controller->lock, flags);
707 if (spi_qup_set_state(controller, QUP_STATE_RESET)) {
708 dev_err(controller->dev, "cannot set RESET state\n");
712 switch (controller->mode) {
713 case QUP_IO_M_MODE_FIFO:
714 writel_relaxed(controller->n_words,
715 controller->base + QUP_MX_READ_CNT);
716 writel_relaxed(controller->n_words,
717 controller->base + QUP_MX_WRITE_CNT);
718 /* must be zero for FIFO */
719 writel_relaxed(0, controller->base + QUP_MX_INPUT_CNT);
720 writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT);
722 case QUP_IO_M_MODE_BAM:
723 writel_relaxed(controller->n_words,
724 controller->base + QUP_MX_INPUT_CNT);
725 writel_relaxed(controller->n_words,
726 controller->base + QUP_MX_OUTPUT_CNT);
727 /* must be zero for BLOCK and BAM */
728 writel_relaxed(0, controller->base + QUP_MX_READ_CNT);
729 writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT);
731 if (!controller->qup_v1) {
732 void __iomem *input_cnt;
734 input_cnt = controller->base + QUP_MX_INPUT_CNT;
736 * for DMA transfers, both QUP_MX_INPUT_CNT and
737 * QUP_MX_OUTPUT_CNT must be zero to all cases but one.
738 * That case is a non-balanced transfer when there is
742 writel_relaxed(0, input_cnt);
744 writel_relaxed(controller->n_words, input_cnt);
746 writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT);
749 case QUP_IO_M_MODE_BLOCK:
750 reinit_completion(&controller->done);
751 writel_relaxed(controller->n_words,
752 controller->base + QUP_MX_INPUT_CNT);
753 writel_relaxed(controller->n_words,
754 controller->base + QUP_MX_OUTPUT_CNT);
755 /* must be zero for BLOCK and BAM */
756 writel_relaxed(0, controller->base + QUP_MX_READ_CNT);
757 writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT);
760 dev_err(controller->dev, "unknown mode = %d\n",
765 iomode = readl_relaxed(controller->base + QUP_IO_M_MODES);
766 /* Set input and output transfer mode */
767 iomode &= ~(QUP_IO_M_INPUT_MODE_MASK | QUP_IO_M_OUTPUT_MODE_MASK);
769 if (!spi_qup_is_dma_xfer(controller->mode))
770 iomode &= ~(QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN);
772 iomode |= QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN;
774 iomode |= (controller->mode << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT);
775 iomode |= (controller->mode << QUP_IO_M_INPUT_MODE_MASK_SHIFT);
777 writel_relaxed(iomode, controller->base + QUP_IO_M_MODES);
779 control = readl_relaxed(controller->base + SPI_IO_CONTROL);
781 if (spi->mode & SPI_CPOL)
782 control |= SPI_IO_C_CLK_IDLE_HIGH;
784 control &= ~SPI_IO_C_CLK_IDLE_HIGH;
786 writel_relaxed(control, controller->base + SPI_IO_CONTROL);
788 config = readl_relaxed(controller->base + SPI_CONFIG);
790 if (spi->mode & SPI_LOOP)
791 config |= SPI_CONFIG_LOOPBACK;
793 config &= ~SPI_CONFIG_LOOPBACK;
795 if (spi->mode & SPI_CPHA)
796 config &= ~SPI_CONFIG_INPUT_FIRST;
798 config |= SPI_CONFIG_INPUT_FIRST;
801 * HS_MODE improves signal stability for spi-clk high rates,
802 * but is invalid in loop back mode.
804 if ((xfer->speed_hz >= SPI_HS_MIN_RATE) && !(spi->mode & SPI_LOOP))
805 config |= SPI_CONFIG_HS_MODE;
807 config &= ~SPI_CONFIG_HS_MODE;
809 writel_relaxed(config, controller->base + SPI_CONFIG);
811 config = readl_relaxed(controller->base + QUP_CONFIG);
812 config &= ~(QUP_CONFIG_NO_INPUT | QUP_CONFIG_NO_OUTPUT | QUP_CONFIG_N);
813 config |= xfer->bits_per_word - 1;
814 config |= QUP_CONFIG_SPI_MODE;
816 if (spi_qup_is_dma_xfer(controller->mode)) {
818 config |= QUP_CONFIG_NO_OUTPUT;
820 config |= QUP_CONFIG_NO_INPUT;
823 writel_relaxed(config, controller->base + QUP_CONFIG);
825 /* only write to OPERATIONAL_MASK when register is present */
826 if (!controller->qup_v1) {
830 * mask INPUT and OUTPUT service flags to prevent IRQs on FIFO
831 * status change in BAM mode
834 if (spi_qup_is_dma_xfer(controller->mode))
835 mask = QUP_OP_IN_SERVICE_FLAG | QUP_OP_OUT_SERVICE_FLAG;
837 writel_relaxed(mask, controller->base + QUP_OPERATIONAL_MASK);
843 static int spi_qup_transfer_one(struct spi_controller *host,
844 struct spi_device *spi,
845 struct spi_transfer *xfer)
847 struct spi_qup *controller = spi_controller_get_devdata(host);
848 unsigned long timeout, flags;
851 ret = spi_qup_io_prep(spi, xfer);
855 timeout = DIV_ROUND_UP(xfer->speed_hz, MSEC_PER_SEC);
856 timeout = DIV_ROUND_UP(min_t(unsigned long, SPI_MAX_XFER,
857 xfer->len) * 8, timeout);
858 timeout = 100 * msecs_to_jiffies(timeout);
860 reinit_completion(&controller->done);
862 spin_lock_irqsave(&controller->lock, flags);
863 controller->xfer = xfer;
864 controller->error = 0;
865 controller->rx_bytes = 0;
866 controller->tx_bytes = 0;
867 spin_unlock_irqrestore(&controller->lock, flags);
869 if (spi_qup_is_dma_xfer(controller->mode))
870 ret = spi_qup_do_dma(spi, xfer, timeout);
872 ret = spi_qup_do_pio(spi, xfer, timeout);
874 spi_qup_set_state(controller, QUP_STATE_RESET);
875 spin_lock_irqsave(&controller->lock, flags);
877 ret = controller->error;
878 spin_unlock_irqrestore(&controller->lock, flags);
880 if (ret && spi_qup_is_dma_xfer(controller->mode))
881 spi_qup_dma_terminate(host, xfer);
886 static bool spi_qup_can_dma(struct spi_controller *host, struct spi_device *spi,
887 struct spi_transfer *xfer)
889 struct spi_qup *qup = spi_controller_get_devdata(host);
890 size_t dma_align = dma_get_cache_alignment();
894 if (!IS_ALIGNED((size_t)xfer->rx_buf, dma_align) ||
895 IS_ERR_OR_NULL(host->dma_rx))
897 if (qup->qup_v1 && (xfer->len % qup->in_blk_sz))
902 if (!IS_ALIGNED((size_t)xfer->tx_buf, dma_align) ||
903 IS_ERR_OR_NULL(host->dma_tx))
905 if (qup->qup_v1 && (xfer->len % qup->out_blk_sz))
909 n_words = xfer->len / DIV_ROUND_UP(xfer->bits_per_word, 8);
910 if (n_words <= (qup->in_fifo_sz / sizeof(u32)))
916 static void spi_qup_release_dma(struct spi_controller *host)
918 if (!IS_ERR_OR_NULL(host->dma_rx))
919 dma_release_channel(host->dma_rx);
920 if (!IS_ERR_OR_NULL(host->dma_tx))
921 dma_release_channel(host->dma_tx);
924 static int spi_qup_init_dma(struct spi_controller *host, resource_size_t base)
926 struct spi_qup *spi = spi_controller_get_devdata(host);
927 struct dma_slave_config *rx_conf = &spi->rx_conf,
928 *tx_conf = &spi->tx_conf;
929 struct device *dev = spi->dev;
932 /* allocate dma resources, if available */
933 host->dma_rx = dma_request_chan(dev, "rx");
934 if (IS_ERR(host->dma_rx))
935 return PTR_ERR(host->dma_rx);
937 host->dma_tx = dma_request_chan(dev, "tx");
938 if (IS_ERR(host->dma_tx)) {
939 ret = PTR_ERR(host->dma_tx);
943 /* set DMA parameters */
944 rx_conf->direction = DMA_DEV_TO_MEM;
945 rx_conf->device_fc = 1;
946 rx_conf->src_addr = base + QUP_INPUT_FIFO;
947 rx_conf->src_maxburst = spi->in_blk_sz;
949 tx_conf->direction = DMA_MEM_TO_DEV;
950 tx_conf->device_fc = 1;
951 tx_conf->dst_addr = base + QUP_OUTPUT_FIFO;
952 tx_conf->dst_maxburst = spi->out_blk_sz;
954 ret = dmaengine_slave_config(host->dma_rx, rx_conf);
956 dev_err(dev, "failed to configure RX channel\n");
960 ret = dmaengine_slave_config(host->dma_tx, tx_conf);
962 dev_err(dev, "failed to configure TX channel\n");
969 dma_release_channel(host->dma_tx);
971 dma_release_channel(host->dma_rx);
975 static void spi_qup_set_cs(struct spi_device *spi, bool val)
977 struct spi_qup *controller;
981 controller = spi_controller_get_devdata(spi->controller);
982 spi_ioc = readl_relaxed(controller->base + SPI_IO_CONTROL);
983 spi_ioc_orig = spi_ioc;
985 spi_ioc |= SPI_IO_C_FORCE_CS;
987 spi_ioc &= ~SPI_IO_C_FORCE_CS;
989 if (spi_ioc != spi_ioc_orig)
990 writel_relaxed(spi_ioc, controller->base + SPI_IO_CONTROL);
993 static int spi_qup_probe(struct platform_device *pdev)
995 struct spi_controller *host;
996 struct clk *iclk, *cclk;
997 struct spi_qup *controller;
998 struct resource *res;
1001 u32 max_freq, iomode, num_cs;
1005 base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1007 return PTR_ERR(base);
1009 irq = platform_get_irq(pdev, 0);
1013 cclk = devm_clk_get(dev, "core");
1015 return PTR_ERR(cclk);
1017 iclk = devm_clk_get(dev, "iface");
1019 return PTR_ERR(iclk);
1021 /* This is optional parameter */
1022 if (of_property_read_u32(dev->of_node, "spi-max-frequency", &max_freq))
1023 max_freq = SPI_MAX_RATE;
1025 if (!max_freq || max_freq > SPI_MAX_RATE) {
1026 dev_err(dev, "invalid clock frequency %d\n", max_freq);
1030 host = spi_alloc_host(dev, sizeof(struct spi_qup));
1032 dev_err(dev, "cannot allocate host\n");
1036 /* use num-cs unless not present or out of range */
1037 if (of_property_read_u32(dev->of_node, "num-cs", &num_cs) ||
1038 num_cs > SPI_NUM_CHIPSELECTS)
1039 host->num_chipselect = SPI_NUM_CHIPSELECTS;
1041 host->num_chipselect = num_cs;
1043 host->use_gpio_descriptors = true;
1044 host->max_native_cs = SPI_NUM_CHIPSELECTS;
1045 host->bus_num = pdev->id;
1046 host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1047 host->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1048 host->max_speed_hz = max_freq;
1049 host->transfer_one = spi_qup_transfer_one;
1050 host->dev.of_node = pdev->dev.of_node;
1051 host->auto_runtime_pm = true;
1052 host->dma_alignment = dma_get_cache_alignment();
1053 host->max_dma_len = SPI_MAX_XFER;
1055 platform_set_drvdata(pdev, host);
1057 controller = spi_controller_get_devdata(host);
1059 controller->dev = dev;
1060 controller->base = base;
1061 controller->iclk = iclk;
1062 controller->cclk = cclk;
1063 controller->irq = irq;
1065 ret = spi_qup_init_dma(host, res->start);
1066 if (ret == -EPROBE_DEFER)
1069 host->can_dma = spi_qup_can_dma;
1071 controller->qup_v1 = (uintptr_t)of_device_get_match_data(dev);
1073 if (!controller->qup_v1)
1074 host->set_cs = spi_qup_set_cs;
1076 spin_lock_init(&controller->lock);
1077 init_completion(&controller->done);
1079 ret = clk_prepare_enable(cclk);
1081 dev_err(dev, "cannot enable core clock\n");
1085 ret = clk_prepare_enable(iclk);
1087 clk_disable_unprepare(cclk);
1088 dev_err(dev, "cannot enable iface clock\n");
1092 iomode = readl_relaxed(base + QUP_IO_M_MODES);
1094 size = QUP_IO_M_OUTPUT_BLOCK_SIZE(iomode);
1096 controller->out_blk_sz = size * 16;
1098 controller->out_blk_sz = 4;
1100 size = QUP_IO_M_INPUT_BLOCK_SIZE(iomode);
1102 controller->in_blk_sz = size * 16;
1104 controller->in_blk_sz = 4;
1106 size = QUP_IO_M_OUTPUT_FIFO_SIZE(iomode);
1107 controller->out_fifo_sz = controller->out_blk_sz * (2 << size);
1109 size = QUP_IO_M_INPUT_FIFO_SIZE(iomode);
1110 controller->in_fifo_sz = controller->in_blk_sz * (2 << size);
1112 dev_info(dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
1113 controller->in_blk_sz, controller->in_fifo_sz,
1114 controller->out_blk_sz, controller->out_fifo_sz);
1116 writel_relaxed(1, base + QUP_SW_RESET);
1118 ret = spi_qup_set_state(controller, QUP_STATE_RESET);
1120 dev_err(dev, "cannot set RESET state\n");
1124 writel_relaxed(0, base + QUP_OPERATIONAL);
1125 writel_relaxed(0, base + QUP_IO_M_MODES);
1127 if (!controller->qup_v1)
1128 writel_relaxed(0, base + QUP_OPERATIONAL_MASK);
1130 writel_relaxed(SPI_ERROR_CLK_UNDER_RUN | SPI_ERROR_CLK_OVER_RUN,
1131 base + SPI_ERROR_FLAGS_EN);
1133 /* if earlier version of the QUP, disable INPUT_OVERRUN */
1134 if (controller->qup_v1)
1135 writel_relaxed(QUP_ERROR_OUTPUT_OVER_RUN |
1136 QUP_ERROR_INPUT_UNDER_RUN | QUP_ERROR_OUTPUT_UNDER_RUN,
1137 base + QUP_ERROR_FLAGS_EN);
1139 writel_relaxed(0, base + SPI_CONFIG);
1140 writel_relaxed(SPI_IO_C_NO_TRI_STATE, base + SPI_IO_CONTROL);
1142 ret = devm_request_irq(dev, irq, spi_qup_qup_irq,
1143 IRQF_TRIGGER_HIGH, pdev->name, controller);
1147 pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
1148 pm_runtime_use_autosuspend(dev);
1149 pm_runtime_set_active(dev);
1150 pm_runtime_enable(dev);
1152 ret = devm_spi_register_controller(dev, host);
1159 pm_runtime_disable(&pdev->dev);
1161 clk_disable_unprepare(cclk);
1162 clk_disable_unprepare(iclk);
1164 spi_qup_release_dma(host);
1166 spi_controller_put(host);
1171 static int spi_qup_pm_suspend_runtime(struct device *device)
1173 struct spi_controller *host = dev_get_drvdata(device);
1174 struct spi_qup *controller = spi_controller_get_devdata(host);
1177 /* Enable clocks auto gaiting */
1178 config = readl(controller->base + QUP_CONFIG);
1179 config |= QUP_CONFIG_CLOCK_AUTO_GATE;
1180 writel_relaxed(config, controller->base + QUP_CONFIG);
1182 clk_disable_unprepare(controller->cclk);
1183 clk_disable_unprepare(controller->iclk);
1188 static int spi_qup_pm_resume_runtime(struct device *device)
1190 struct spi_controller *host = dev_get_drvdata(device);
1191 struct spi_qup *controller = spi_controller_get_devdata(host);
1195 ret = clk_prepare_enable(controller->iclk);
1199 ret = clk_prepare_enable(controller->cclk);
1201 clk_disable_unprepare(controller->iclk);
1205 /* Disable clocks auto gaiting */
1206 config = readl_relaxed(controller->base + QUP_CONFIG);
1207 config &= ~QUP_CONFIG_CLOCK_AUTO_GATE;
1208 writel_relaxed(config, controller->base + QUP_CONFIG);
1211 #endif /* CONFIG_PM */
1213 #ifdef CONFIG_PM_SLEEP
1214 static int spi_qup_suspend(struct device *device)
1216 struct spi_controller *host = dev_get_drvdata(device);
1217 struct spi_qup *controller = spi_controller_get_devdata(host);
1220 if (pm_runtime_suspended(device)) {
1221 ret = spi_qup_pm_resume_runtime(device);
1225 ret = spi_controller_suspend(host);
1229 ret = spi_qup_set_state(controller, QUP_STATE_RESET);
1233 clk_disable_unprepare(controller->cclk);
1234 clk_disable_unprepare(controller->iclk);
1238 static int spi_qup_resume(struct device *device)
1240 struct spi_controller *host = dev_get_drvdata(device);
1241 struct spi_qup *controller = spi_controller_get_devdata(host);
1244 ret = clk_prepare_enable(controller->iclk);
1248 ret = clk_prepare_enable(controller->cclk);
1250 clk_disable_unprepare(controller->iclk);
1254 ret = spi_qup_set_state(controller, QUP_STATE_RESET);
1258 ret = spi_controller_resume(host);
1265 clk_disable_unprepare(controller->cclk);
1266 clk_disable_unprepare(controller->iclk);
1269 #endif /* CONFIG_PM_SLEEP */
1271 static void spi_qup_remove(struct platform_device *pdev)
1273 struct spi_controller *host = dev_get_drvdata(&pdev->dev);
1274 struct spi_qup *controller = spi_controller_get_devdata(host);
1277 ret = pm_runtime_get_sync(&pdev->dev);
1280 ret = spi_qup_set_state(controller, QUP_STATE_RESET);
1282 dev_warn(&pdev->dev, "failed to reset controller (%pe)\n",
1285 clk_disable_unprepare(controller->cclk);
1286 clk_disable_unprepare(controller->iclk);
1288 dev_warn(&pdev->dev, "failed to resume, skip hw disable (%pe)\n",
1292 spi_qup_release_dma(host);
1294 pm_runtime_put_noidle(&pdev->dev);
1295 pm_runtime_disable(&pdev->dev);
1298 static const struct of_device_id spi_qup_dt_match[] = {
1299 { .compatible = "qcom,spi-qup-v1.1.1", .data = (void *)1, },
1300 { .compatible = "qcom,spi-qup-v2.1.1", },
1301 { .compatible = "qcom,spi-qup-v2.2.1", },
1304 MODULE_DEVICE_TABLE(of, spi_qup_dt_match);
1306 static const struct dev_pm_ops spi_qup_dev_pm_ops = {
1307 SET_SYSTEM_SLEEP_PM_OPS(spi_qup_suspend, spi_qup_resume)
1308 SET_RUNTIME_PM_OPS(spi_qup_pm_suspend_runtime,
1309 spi_qup_pm_resume_runtime,
1313 static struct platform_driver spi_qup_driver = {
1316 .pm = &spi_qup_dev_pm_ops,
1317 .of_match_table = spi_qup_dt_match,
1319 .probe = spi_qup_probe,
1320 .remove_new = spi_qup_remove,
1322 module_platform_driver(spi_qup_driver);
1324 MODULE_LICENSE("GPL v2");
1325 MODULE_ALIAS("platform:spi_qup");