1 From 05a08cc5620df0fcf8e260feee04b9671705723e Mon Sep 17 00:00:00 2001
2 From: Matthew McClintock <mmcclint@codeaurora.org>
3 Date: Tue, 26 Apr 2016 15:46:24 -0500
4 Subject: [PATCH 14/37] spi: qup: allow block mode to generate multiple
7 This let's you write more to the SPI bus than 64K-1 which is important
8 if the block size of a SPI device is >= 64K or some other device wants
11 This has the benefit of completly removing spi_message from the spi-qup
14 Signed-off-by: Matthew McClintock <mmcclint@codeaurora.org>
16 drivers/spi/spi-qup.c | 120 ++++++++++++++++++++++++++++++-------------------
17 1 file changed, 75 insertions(+), 45 deletions(-)
19 --- a/drivers/spi/spi-qup.c
20 +++ b/drivers/spi/spi-qup.c
23 #define SPI_NUM_CHIPSELECTS 4
25 -#define SPI_MAX_DMA_XFER (SZ_64K - 64)
26 +#define SPI_MAX_XFER (SZ_64K - 64)
28 /* high speed mode is when bus rate is greater then 26MHz */
29 #define SPI_HS_MIN_RATE 26000000
30 @@ -150,6 +150,8 @@ struct spi_qup {
39 @@ -172,6 +174,12 @@ static inline bool spi_qup_is_dma_xfer(i
43 +/* get's the transaction size length */
44 +static inline unsigned spi_qup_len(struct spi_qup *controller)
46 + return controller->n_words * controller->w_size;
49 static inline bool spi_qup_is_valid_state(struct spi_qup *controller)
51 u32 opstate = readl_relaxed(controller->base + QUP_STATE);
52 @@ -224,10 +232,9 @@ static int spi_qup_set_state(struct spi_
56 -static void spi_qup_read_from_fifo(struct spi_qup *controller,
57 - struct spi_transfer *xfer, u32 num_words)
58 +static void spi_qup_read_from_fifo(struct spi_qup *controller, u32 num_words)
60 - u8 *rx_buf = xfer->rx_buf;
61 + u8 *rx_buf = controller->rx_buf;
62 int i, shift, num_bytes;
65 @@ -235,7 +242,7 @@ static void spi_qup_read_from_fifo(struc
67 word = readl_relaxed(controller->base + QUP_INPUT_FIFO);
69 - num_bytes = min_t(int, xfer->len - controller->rx_bytes,
70 + num_bytes = min_t(int, spi_qup_len(controller) - controller->rx_bytes,
74 @@ -257,13 +264,12 @@ static void spi_qup_read_from_fifo(struc
78 -static void spi_qup_read(struct spi_qup *controller,
79 - struct spi_transfer *xfer)
80 +static void spi_qup_read(struct spi_qup *controller)
82 u32 remainder, words_per_block, num_words;
83 bool is_block_mode = controller->mode == QUP_IO_M_MODE_BLOCK;
85 - remainder = DIV_ROUND_UP(xfer->len - controller->rx_bytes,
86 + remainder = DIV_ROUND_UP(spi_qup_len(controller) - controller->rx_bytes,
88 words_per_block = controller->in_blk_sz >> 2;
90 @@ -284,7 +290,7 @@ static void spi_qup_read(struct spi_qup
93 /* read up to the maximum transfer size available */
94 - spi_qup_read_from_fifo(controller, xfer, num_words);
95 + spi_qup_read_from_fifo(controller, num_words);
97 remainder -= num_words;
99 @@ -306,17 +312,16 @@ static void spi_qup_read(struct spi_qup
103 -static void spi_qup_write_to_fifo(struct spi_qup *controller,
104 - struct spi_transfer *xfer, u32 num_words)
105 +static void spi_qup_write_to_fifo(struct spi_qup *controller, u32 num_words)
107 - const u8 *tx_buf = xfer->tx_buf;
108 + const u8 *tx_buf = controller->tx_buf;
112 for (; num_words; num_words--) {
115 - num_bytes = min_t(int, xfer->len - controller->tx_bytes,
116 + num_bytes = min_t(int, spi_qup_len(controller) - controller->tx_bytes,
119 for (i = 0; i < num_bytes; i++) {
120 @@ -337,13 +342,12 @@ static void spi_qup_dma_done(void *data)
124 -static void spi_qup_write(struct spi_qup *controller,
125 - struct spi_transfer *xfer)
126 +static void spi_qup_write(struct spi_qup *controller)
128 bool is_block_mode = controller->mode == QUP_IO_M_MODE_BLOCK;
129 u32 remainder, words_per_block, num_words;
131 - remainder = DIV_ROUND_UP(xfer->len - controller->tx_bytes,
132 + remainder = DIV_ROUND_UP(spi_qup_len(controller) - controller->tx_bytes,
134 words_per_block = controller->out_blk_sz >> 2;
136 @@ -363,7 +367,7 @@ static void spi_qup_write(struct spi_qup
140 - spi_qup_write_to_fifo(controller, xfer, num_words);
141 + spi_qup_write_to_fifo(controller, num_words);
143 remainder -= num_words;
145 @@ -629,35 +633,61 @@ static int spi_qup_do_pio(struct spi_dev
147 struct spi_master *master = spi->master;
148 struct spi_qup *qup = spi_master_get_devdata(master);
150 + int ret, n_words, iterations, offset = 0;
152 - ret = spi_qup_io_config(spi, xfer);
155 + n_words = qup->n_words;
156 + iterations = n_words / SPI_MAX_XFER; /* round down */
158 - ret = spi_qup_set_state(qup, QUP_STATE_RUN);
160 - dev_warn(qup->dev, "cannot set RUN state\n");
163 + qup->rx_buf = xfer->rx_buf;
164 + qup->tx_buf = xfer->tx_buf;
166 - ret = spi_qup_set_state(qup, QUP_STATE_PAUSE);
168 - dev_warn(qup->dev, "cannot set PAUSE state\n");
173 + qup->n_words = SPI_MAX_XFER;
175 + qup->n_words = n_words % SPI_MAX_XFER;
177 + if (qup->tx_buf && offset)
178 + qup->tx_buf = xfer->tx_buf + offset * SPI_MAX_XFER;
180 + if (qup->rx_buf && offset)
181 + qup->rx_buf = xfer->rx_buf + offset * SPI_MAX_XFER;
183 + /* if the transaction is small enough, we need
184 + * to fallback to FIFO mode */
185 + if (qup->n_words <= (qup->in_fifo_sz / sizeof(u32)))
186 + qup->mode = QUP_IO_M_MODE_FIFO;
188 - if (qup->mode == QUP_IO_M_MODE_FIFO)
189 - spi_qup_write(qup, xfer);
190 + ret = spi_qup_io_config(spi, xfer);
194 - ret = spi_qup_set_state(qup, QUP_STATE_RUN);
196 - dev_warn(qup->dev, "cannot set RUN state\n");
199 + ret = spi_qup_set_state(qup, QUP_STATE_RUN);
201 + dev_warn(qup->dev, "cannot set RUN state\n");
205 - if (!wait_for_completion_timeout(&qup->done, timeout))
207 + ret = spi_qup_set_state(qup, QUP_STATE_PAUSE);
209 + dev_warn(qup->dev, "cannot set PAUSE state\n");
213 + if (qup->mode == QUP_IO_M_MODE_FIFO)
214 + spi_qup_write(qup);
216 + ret = spi_qup_set_state(qup, QUP_STATE_RUN);
218 + dev_warn(qup->dev, "cannot set RUN state\n");
222 + if (!wait_for_completion_timeout(&qup->done, timeout))
226 + } while (iterations--);
230 @@ -722,17 +752,17 @@ static irqreturn_t spi_qup_qup_irq(int i
231 complete(&controller->dma_tx_done);
233 if (opflags & QUP_OP_IN_SERVICE_FLAG)
234 - spi_qup_read(controller, xfer);
235 + spi_qup_read(controller);
237 if (opflags & QUP_OP_OUT_SERVICE_FLAG)
238 - spi_qup_write(controller, xfer);
239 + spi_qup_write(controller);
242 /* re-read opflags as flags may have changed due to actions above */
243 if (opflags & QUP_OP_OUT_SERVICE_FLAG)
244 opflags = readl_relaxed(controller->base + QUP_OPERATIONAL);
246 - if ((controller->rx_bytes == xfer->len &&
247 + if ((controller->rx_bytes == spi_qup_len(controller) &&
248 (opflags & QUP_OP_MAX_INPUT_DONE_FLAG)) || error)
251 @@ -794,7 +824,7 @@ static int spi_qup_transfer_one(struct s
254 timeout = DIV_ROUND_UP(xfer->speed_hz, MSEC_PER_SEC);
255 - timeout = DIV_ROUND_UP(xfer->len * 8, timeout);
256 + timeout = DIV_ROUND_UP(min_t(unsigned long, SPI_MAX_XFER, xfer->len) * 8, timeout);
257 timeout = 100 * msecs_to_jiffies(timeout);
259 if (spi_qup_is_dma_xfer(controller->mode))
260 @@ -983,7 +1013,7 @@ static int spi_qup_probe(struct platform
261 master->dev.of_node = pdev->dev.of_node;
262 master->auto_runtime_pm = true;
263 master->dma_alignment = dma_get_cache_alignment();
264 - master->max_dma_len = SPI_MAX_DMA_XFER;
265 + master->max_dma_len = SPI_MAX_XFER;
267 platform_set_drvdata(pdev, master);