blob: 1364516e87c29b0e4255b61bfdde045a7761f6bc [file] [log] [blame]
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001/*
2 * Copyright (c) 2008-2014, The Linux foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License rev 2 and
6 * only rev 2 as published by the free Software foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or fITNESS fOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/list.h>
20#include <linux/module.h>
21#include <linux/of.h>
Varadarajan Narayanan4d023732017-07-28 12:23:01 +053022#include <linux/of_device.h>
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +020023#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/spi/spi.h>
Andy Gross612762e2015-03-04 12:02:05 +020026#include <linux/dmaengine.h>
27#include <linux/dma-mapping.h>
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +020028
29#define QUP_CONFIG 0x0000
30#define QUP_STATE 0x0004
31#define QUP_IO_M_MODES 0x0008
32#define QUP_SW_RESET 0x000c
33#define QUP_OPERATIONAL 0x0018
34#define QUP_ERROR_FLAGS 0x001c
35#define QUP_ERROR_FLAGS_EN 0x0020
36#define QUP_OPERATIONAL_MASK 0x0028
37#define QUP_HW_VERSION 0x0030
38#define QUP_MX_OUTPUT_CNT 0x0100
39#define QUP_OUTPUT_FIFO 0x0110
40#define QUP_MX_WRITE_CNT 0x0150
41#define QUP_MX_INPUT_CNT 0x0200
42#define QUP_MX_READ_CNT 0x0208
43#define QUP_INPUT_FIFO 0x0218
44
45#define SPI_CONFIG 0x0300
46#define SPI_IO_CONTROL 0x0304
47#define SPI_ERROR_FLAGS 0x0308
48#define SPI_ERROR_FLAGS_EN 0x030c
49
50/* QUP_CONFIG fields */
51#define QUP_CONFIG_SPI_MODE (1 << 8)
52#define QUP_CONFIG_CLOCK_AUTO_GATE BIT(13)
53#define QUP_CONFIG_NO_INPUT BIT(7)
54#define QUP_CONFIG_NO_OUTPUT BIT(6)
55#define QUP_CONFIG_N 0x001f
56
57/* QUP_STATE fields */
58#define QUP_STATE_VALID BIT(2)
59#define QUP_STATE_RESET 0
60#define QUP_STATE_RUN 1
61#define QUP_STATE_PAUSE 3
62#define QUP_STATE_MASK 3
63#define QUP_STATE_CLEAR 2
64
65#define QUP_HW_VERSION_2_1_1 0x20010001
66
67/* QUP_IO_M_MODES fields */
68#define QUP_IO_M_PACK_EN BIT(15)
69#define QUP_IO_M_UNPACK_EN BIT(14)
70#define QUP_IO_M_INPUT_MODE_MASK_SHIFT 12
71#define QUP_IO_M_OUTPUT_MODE_MASK_SHIFT 10
72#define QUP_IO_M_INPUT_MODE_MASK (3 << QUP_IO_M_INPUT_MODE_MASK_SHIFT)
73#define QUP_IO_M_OUTPUT_MODE_MASK (3 << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT)
74
75#define QUP_IO_M_OUTPUT_BLOCK_SIZE(x) (((x) & (0x03 << 0)) >> 0)
76#define QUP_IO_M_OUTPUT_FIFO_SIZE(x) (((x) & (0x07 << 2)) >> 2)
77#define QUP_IO_M_INPUT_BLOCK_SIZE(x) (((x) & (0x03 << 5)) >> 5)
78#define QUP_IO_M_INPUT_FIFO_SIZE(x) (((x) & (0x07 << 7)) >> 7)
79
80#define QUP_IO_M_MODE_FIFO 0
81#define QUP_IO_M_MODE_BLOCK 1
82#define QUP_IO_M_MODE_DMOV 2
83#define QUP_IO_M_MODE_BAM 3
84
85/* QUP_OPERATIONAL fields */
Varadarajan Narayanan75387262017-07-28 12:22:54 +053086#define QUP_OP_IN_BLOCK_READ_REQ BIT(13)
87#define QUP_OP_OUT_BLOCK_WRITE_REQ BIT(12)
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +020088#define QUP_OP_MAX_INPUT_DONE_FLAG BIT(11)
89#define QUP_OP_MAX_OUTPUT_DONE_FLAG BIT(10)
90#define QUP_OP_IN_SERVICE_FLAG BIT(9)
91#define QUP_OP_OUT_SERVICE_FLAG BIT(8)
92#define QUP_OP_IN_FIFO_FULL BIT(7)
93#define QUP_OP_OUT_FIFO_FULL BIT(6)
94#define QUP_OP_IN_FIFO_NOT_EMPTY BIT(5)
95#define QUP_OP_OUT_FIFO_NOT_EMPTY BIT(4)
96
97/* QUP_ERROR_FLAGS and QUP_ERROR_FLAGS_EN fields */
98#define QUP_ERROR_OUTPUT_OVER_RUN BIT(5)
99#define QUP_ERROR_INPUT_UNDER_RUN BIT(4)
100#define QUP_ERROR_OUTPUT_UNDER_RUN BIT(3)
101#define QUP_ERROR_INPUT_OVER_RUN BIT(2)
102
103/* SPI_CONFIG fields */
104#define SPI_CONFIG_HS_MODE BIT(10)
105#define SPI_CONFIG_INPUT_FIRST BIT(9)
106#define SPI_CONFIG_LOOPBACK BIT(8)
107
108/* SPI_IO_CONTROL fields */
109#define SPI_IO_C_FORCE_CS BIT(11)
110#define SPI_IO_C_CLK_IDLE_HIGH BIT(10)
111#define SPI_IO_C_MX_CS_MODE BIT(8)
112#define SPI_IO_C_CS_N_POLARITY_0 BIT(4)
113#define SPI_IO_C_CS_SELECT(x) (((x) & 3) << 2)
114#define SPI_IO_C_CS_SELECT_MASK 0x000c
115#define SPI_IO_C_TRISTATE_CS BIT(1)
116#define SPI_IO_C_NO_TRI_STATE BIT(0)
117
118/* SPI_ERROR_FLAGS and SPI_ERROR_FLAGS_EN fields */
119#define SPI_ERROR_CLK_OVER_RUN BIT(1)
120#define SPI_ERROR_CLK_UNDER_RUN BIT(0)
121
122#define SPI_NUM_CHIPSELECTS 4
123
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530124#define SPI_MAX_XFER (SZ_64K - 64)
Andy Gross612762e2015-03-04 12:02:05 +0200125
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200126/* high speed mode is when bus rate is greater then 26MHz */
127#define SPI_HS_MIN_RATE 26000000
128#define SPI_MAX_RATE 50000000
129
130#define SPI_DELAY_THRESHOLD 1
131#define SPI_DELAY_RETRY 10
132
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200133struct spi_qup {
134 void __iomem *base;
135 struct device *dev;
136 struct clk *cclk; /* core clock */
137 struct clk *iclk; /* interface clock */
138 int irq;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200139 spinlock_t lock;
140
141 int in_fifo_sz;
142 int out_fifo_sz;
143 int in_blk_sz;
144 int out_blk_sz;
145
146 struct spi_transfer *xfer;
147 struct completion done;
148 int error;
149 int w_size; /* bytes per SPI word */
Andy Gross612762e2015-03-04 12:02:05 +0200150 int n_words;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200151 int tx_bytes;
152 int rx_bytes;
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530153 const u8 *tx_buf;
154 u8 *rx_buf;
Andy Gross70cea0a2014-06-12 14:34:12 -0500155 int qup_v1;
Andy Gross612762e2015-03-04 12:02:05 +0200156
Varadarajan Narayanan32ecab92017-07-28 12:22:49 +0530157 int mode;
Andy Gross612762e2015-03-04 12:02:05 +0200158 struct dma_slave_config rx_conf;
159 struct dma_slave_config tx_conf;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200160};
161
Varadarajan Narayanan3b5ea2c2017-07-28 12:22:56 +0530162static int spi_qup_io_config(struct spi_device *spi, struct spi_transfer *xfer);
163
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530164static inline bool spi_qup_is_flag_set(struct spi_qup *controller, u32 flag)
165{
166 u32 opflag = readl_relaxed(controller->base + QUP_OPERATIONAL);
167
168 return (opflag & flag) != 0;
169}
170
Varadarajan Narayanan32ecab92017-07-28 12:22:49 +0530171static inline bool spi_qup_is_dma_xfer(int mode)
172{
173 if (mode == QUP_IO_M_MODE_DMOV || mode == QUP_IO_M_MODE_BAM)
174 return true;
175
176 return false;
177}
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200178
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530179/* get's the transaction size length */
180static inline unsigned int spi_qup_len(struct spi_qup *controller)
181{
182 return controller->n_words * controller->w_size;
183}
184
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200185static inline bool spi_qup_is_valid_state(struct spi_qup *controller)
186{
187 u32 opstate = readl_relaxed(controller->base + QUP_STATE);
188
189 return opstate & QUP_STATE_VALID;
190}
191
192static int spi_qup_set_state(struct spi_qup *controller, u32 state)
193{
194 unsigned long loop;
195 u32 cur_state;
196
197 loop = 0;
198 while (!spi_qup_is_valid_state(controller)) {
199
200 usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2);
201
202 if (++loop > SPI_DELAY_RETRY)
203 return -EIO;
204 }
205
206 if (loop)
207 dev_dbg(controller->dev, "invalid state for %ld,us %d\n",
208 loop, state);
209
210 cur_state = readl_relaxed(controller->base + QUP_STATE);
211 /*
212 * Per spec: for PAUSE_STATE to RESET_STATE, two writes
213 * of (b10) are required
214 */
215 if (((cur_state & QUP_STATE_MASK) == QUP_STATE_PAUSE) &&
216 (state == QUP_STATE_RESET)) {
217 writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE);
218 writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE);
219 } else {
220 cur_state &= ~QUP_STATE_MASK;
221 cur_state |= state;
222 writel_relaxed(cur_state, controller->base + QUP_STATE);
223 }
224
225 loop = 0;
226 while (!spi_qup_is_valid_state(controller)) {
227
228 usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2);
229
230 if (++loop > SPI_DELAY_RETRY)
231 return -EIO;
232 }
233
234 return 0;
235}
236
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530237static void spi_qup_read_from_fifo(struct spi_qup *controller, u32 num_words)
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200238{
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530239 u8 *rx_buf = controller->rx_buf;
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530240 int i, shift, num_bytes;
241 u32 word;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200242
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530243 for (; num_words; num_words--) {
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200244
245 word = readl_relaxed(controller->base + QUP_INPUT_FIFO);
246
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530247 num_bytes = min_t(int, spi_qup_len(controller) -
248 controller->rx_bytes,
249 controller->w_size);
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530250
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200251 if (!rx_buf) {
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530252 controller->rx_bytes += num_bytes;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200253 continue;
254 }
255
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530256 for (i = 0; i < num_bytes; i++, controller->rx_bytes++) {
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200257 /*
258 * The data format depends on bytes per SPI word:
259 * 4 bytes: 0x12345678
260 * 2 bytes: 0x00001234
261 * 1 byte : 0x00000012
262 */
263 shift = BITS_PER_BYTE;
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530264 shift *= (controller->w_size - i - 1);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200265 rx_buf[controller->rx_bytes] = word >> shift;
266 }
267 }
268}
269
Varadarajan Narayanancd595b92017-07-28 12:23:00 +0530270static void spi_qup_read(struct spi_qup *controller, u32 *opflags)
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200271{
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530272 u32 remainder, words_per_block, num_words;
273 bool is_block_mode = controller->mode == QUP_IO_M_MODE_BLOCK;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200274
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530275 remainder = DIV_ROUND_UP(spi_qup_len(controller) - controller->rx_bytes,
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530276 controller->w_size);
277 words_per_block = controller->in_blk_sz >> 2;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200278
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530279 do {
280 /* ACK by clearing service flag */
281 writel_relaxed(QUP_OP_IN_SERVICE_FLAG,
282 controller->base + QUP_OPERATIONAL);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200283
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530284 if (is_block_mode) {
285 num_words = (remainder > words_per_block) ?
286 words_per_block : remainder;
287 } else {
288 if (!spi_qup_is_flag_set(controller,
289 QUP_OP_IN_FIFO_NOT_EMPTY))
290 break;
291
292 num_words = 1;
293 }
294
295 /* read up to the maximum transfer size available */
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530296 spi_qup_read_from_fifo(controller, num_words);
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530297
298 remainder -= num_words;
299
300 /* if block mode, check to see if next block is available */
301 if (is_block_mode && !spi_qup_is_flag_set(controller,
302 QUP_OP_IN_BLOCK_READ_REQ))
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200303 break;
304
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530305 } while (remainder);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200306
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530307 /*
308 * Due to extra stickiness of the QUP_OP_IN_SERVICE_FLAG during block
Varadarajan Narayanancd595b92017-07-28 12:23:00 +0530309 * reads, it has to be cleared again at the very end. However, be sure
310 * to refresh opflags value because MAX_INPUT_DONE_FLAG may now be
311 * present and this is used to determine if transaction is complete
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530312 */
Varadarajan Narayanancd595b92017-07-28 12:23:00 +0530313 *opflags = readl_relaxed(controller->base + QUP_OPERATIONAL);
314 if (is_block_mode && *opflags & QUP_OP_MAX_INPUT_DONE_FLAG)
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530315 writel_relaxed(QUP_OP_IN_SERVICE_FLAG,
316 controller->base + QUP_OPERATIONAL);
317
318}
319
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530320static void spi_qup_write_to_fifo(struct spi_qup *controller, u32 num_words)
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530321{
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530322 const u8 *tx_buf = controller->tx_buf;
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530323 int i, num_bytes;
324 u32 word, data;
325
326 for (; num_words; num_words--) {
327 word = 0;
328
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530329 num_bytes = min_t(int, spi_qup_len(controller) -
330 controller->tx_bytes,
331 controller->w_size);
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530332 if (tx_buf)
333 for (i = 0; i < num_bytes; i++) {
334 data = tx_buf[controller->tx_bytes + i];
335 word |= data << (BITS_PER_BYTE * (3 - i));
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200336 }
337
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530338 controller->tx_bytes += num_bytes;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200339
340 writel_relaxed(word, controller->base + QUP_OUTPUT_FIFO);
341 }
342}
343
Andy Gross612762e2015-03-04 12:02:05 +0200344static void spi_qup_dma_done(void *data)
345{
346 struct spi_qup *qup = data;
347
348 complete(&qup->done);
349}
350
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530351static void spi_qup_write(struct spi_qup *controller)
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530352{
353 bool is_block_mode = controller->mode == QUP_IO_M_MODE_BLOCK;
354 u32 remainder, words_per_block, num_words;
355
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530356 remainder = DIV_ROUND_UP(spi_qup_len(controller) - controller->tx_bytes,
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530357 controller->w_size);
358 words_per_block = controller->out_blk_sz >> 2;
359
360 do {
361 /* ACK by clearing service flag */
362 writel_relaxed(QUP_OP_OUT_SERVICE_FLAG,
363 controller->base + QUP_OPERATIONAL);
364
365 if (is_block_mode) {
366 num_words = (remainder > words_per_block) ?
367 words_per_block : remainder;
368 } else {
369 if (spi_qup_is_flag_set(controller,
370 QUP_OP_OUT_FIFO_FULL))
371 break;
372
373 num_words = 1;
374 }
375
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530376 spi_qup_write_to_fifo(controller, num_words);
Varadarajan Narayanan75387262017-07-28 12:22:54 +0530377
378 remainder -= num_words;
379
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))
383 break;
384
385 } while (remainder);
386}
387
Varadarajan Narayanana841b242017-07-28 12:22:58 +0530388static int spi_qup_prep_sg(struct spi_master *master, struct scatterlist *sgl,
389 unsigned int nents, enum dma_transfer_direction dir,
Andy Gross612762e2015-03-04 12:02:05 +0200390 dma_async_tx_callback callback)
391{
392 struct spi_qup *qup = spi_master_get_devdata(master);
393 unsigned long flags = DMA_PREP_INTERRUPT | DMA_PREP_FENCE;
394 struct dma_async_tx_descriptor *desc;
Andy Gross612762e2015-03-04 12:02:05 +0200395 struct dma_chan *chan;
396 dma_cookie_t cookie;
Andy Gross612762e2015-03-04 12:02:05 +0200397
Varadarajan Narayanana841b242017-07-28 12:22:58 +0530398 if (dir == DMA_MEM_TO_DEV)
Andy Gross612762e2015-03-04 12:02:05 +0200399 chan = master->dma_tx;
Varadarajan Narayanana841b242017-07-28 12:22:58 +0530400 else
Andy Gross612762e2015-03-04 12:02:05 +0200401 chan = master->dma_rx;
Andy Gross612762e2015-03-04 12:02:05 +0200402
403 desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags);
Varadarajan Narayanand9a09a62017-07-28 12:22:52 +0530404 if (IS_ERR_OR_NULL(desc))
405 return desc ? PTR_ERR(desc) : -EINVAL;
Andy Gross612762e2015-03-04 12:02:05 +0200406
407 desc->callback = callback;
408 desc->callback_param = qup;
409
410 cookie = dmaengine_submit(desc);
411
412 return dma_submit_error(cookie);
413}
414
415static void spi_qup_dma_terminate(struct spi_master *master,
416 struct spi_transfer *xfer)
417{
418 if (xfer->tx_buf)
419 dmaengine_terminate_all(master->dma_tx);
420 if (xfer->rx_buf)
421 dmaengine_terminate_all(master->dma_rx);
422}
423
Varadarajan Narayanan5884e172017-07-28 12:22:59 +0530424static u32 spi_qup_sgl_get_nents_len(struct scatterlist *sgl, u32 max,
425 u32 *nents)
426{
427 struct scatterlist *sg;
428 u32 total = 0;
429
430 *nents = 0;
431
432 for (sg = sgl; sg; sg = sg_next(sg)) {
433 unsigned int len = sg_dma_len(sg);
434
435 /* check for overflow as well as limit */
436 if (((total + len) < total) || ((total + len) > max))
437 break;
438
439 total += len;
440 (*nents)++;
441 }
442
443 return total;
444}
445
Varadarajan Narayanan3b5ea2c2017-07-28 12:22:56 +0530446static int spi_qup_do_dma(struct spi_device *spi, struct spi_transfer *xfer,
Varadarajan Narayanan5f13fd62017-07-28 12:22:50 +0530447 unsigned long timeout)
Andy Gross612762e2015-03-04 12:02:05 +0200448{
449 dma_async_tx_callback rx_done = NULL, tx_done = NULL;
Varadarajan Narayanan3b5ea2c2017-07-28 12:22:56 +0530450 struct spi_master *master = spi->master;
451 struct spi_qup *qup = spi_master_get_devdata(master);
Varadarajan Narayanan5884e172017-07-28 12:22:59 +0530452 struct scatterlist *tx_sgl, *rx_sgl;
Andy Gross612762e2015-03-04 12:02:05 +0200453 int ret;
454
455 if (xfer->rx_buf)
456 rx_done = spi_qup_dma_done;
457 else if (xfer->tx_buf)
458 tx_done = spi_qup_dma_done;
459
Varadarajan Narayanan5884e172017-07-28 12:22:59 +0530460 rx_sgl = xfer->rx_sg.sgl;
461 tx_sgl = xfer->tx_sg.sgl;
Varadarajan Narayanan3b5ea2c2017-07-28 12:22:56 +0530462
Varadarajan Narayanan5884e172017-07-28 12:22:59 +0530463 do {
464 u32 rx_nents, tx_nents;
Varadarajan Narayanance00bab2017-07-28 12:22:51 +0530465
Varadarajan Narayanan5884e172017-07-28 12:22:59 +0530466 if (rx_sgl)
467 qup->n_words = spi_qup_sgl_get_nents_len(rx_sgl,
468 SPI_MAX_XFER, &rx_nents) / qup->w_size;
469 if (tx_sgl)
470 qup->n_words = spi_qup_sgl_get_nents_len(tx_sgl,
471 SPI_MAX_XFER, &tx_nents) / qup->w_size;
472 if (!qup->n_words)
473 return -EIO;
474
475 ret = spi_qup_io_config(spi, xfer);
Andy Gross612762e2015-03-04 12:02:05 +0200476 if (ret)
477 return ret;
478
Varadarajan Narayanan5884e172017-07-28 12:22:59 +0530479 /* before issuing the descriptors, set the QUP to run */
480 ret = spi_qup_set_state(qup, QUP_STATE_RUN);
481 if (ret) {
482 dev_warn(qup->dev, "cannot set RUN state\n");
Andy Gross612762e2015-03-04 12:02:05 +0200483 return ret;
Varadarajan Narayanan5884e172017-07-28 12:22:59 +0530484 }
485 if (rx_sgl) {
486 ret = spi_qup_prep_sg(master, rx_sgl, rx_nents,
487 DMA_DEV_TO_MEM, rx_done);
488 if (ret)
489 return ret;
490 dma_async_issue_pending(master->dma_rx);
491 }
Andy Gross612762e2015-03-04 12:02:05 +0200492
Varadarajan Narayanan5884e172017-07-28 12:22:59 +0530493 if (tx_sgl) {
494 ret = spi_qup_prep_sg(master, tx_sgl, tx_nents,
495 DMA_MEM_TO_DEV, tx_done);
496 if (ret)
497 return ret;
Andy Gross612762e2015-03-04 12:02:05 +0200498
Varadarajan Narayanan5884e172017-07-28 12:22:59 +0530499 dma_async_issue_pending(master->dma_tx);
500 }
501
502 if (!wait_for_completion_timeout(&qup->done, timeout))
503 return -ETIMEDOUT;
504
505 for (; rx_sgl && rx_nents--; rx_sgl = sg_next(rx_sgl))
506 ;
507 for (; tx_sgl && tx_nents--; tx_sgl = sg_next(tx_sgl))
508 ;
509
510 } while (rx_sgl || tx_sgl);
Varadarajan Narayanan5f13fd62017-07-28 12:22:50 +0530511
Andy Gross612762e2015-03-04 12:02:05 +0200512 return 0;
513}
514
Varadarajan Narayanan3b5ea2c2017-07-28 12:22:56 +0530515static int spi_qup_do_pio(struct spi_device *spi, struct spi_transfer *xfer,
Varadarajan Narayanan5f13fd62017-07-28 12:22:50 +0530516 unsigned long timeout)
Andy Gross612762e2015-03-04 12:02:05 +0200517{
Varadarajan Narayanan3b5ea2c2017-07-28 12:22:56 +0530518 struct spi_master *master = spi->master;
Andy Gross612762e2015-03-04 12:02:05 +0200519 struct spi_qup *qup = spi_master_get_devdata(master);
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530520 int ret, n_words, iterations, offset = 0;
Andy Gross612762e2015-03-04 12:02:05 +0200521
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530522 n_words = qup->n_words;
523 iterations = n_words / SPI_MAX_XFER; /* round down */
524 qup->rx_buf = xfer->rx_buf;
525 qup->tx_buf = xfer->tx_buf;
Varadarajan Narayanan3b5ea2c2017-07-28 12:22:56 +0530526
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530527 do {
528 if (iterations)
529 qup->n_words = SPI_MAX_XFER;
530 else
531 qup->n_words = n_words % SPI_MAX_XFER;
Andy Gross612762e2015-03-04 12:02:05 +0200532
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530533 if (qup->tx_buf && offset)
534 qup->tx_buf = xfer->tx_buf + offset * SPI_MAX_XFER;
Andy Gross612762e2015-03-04 12:02:05 +0200535
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530536 if (qup->rx_buf && offset)
537 qup->rx_buf = xfer->rx_buf + offset * SPI_MAX_XFER;
Andy Gross612762e2015-03-04 12:02:05 +0200538
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530539 /*
540 * if the transaction is small enough, we need
541 * to fallback to FIFO mode
542 */
543 if (qup->n_words <= (qup->in_fifo_sz / sizeof(u32)))
544 qup->mode = QUP_IO_M_MODE_FIFO;
Varadarajan Narayanance00bab2017-07-28 12:22:51 +0530545
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530546 ret = spi_qup_io_config(spi, xfer);
547 if (ret)
548 return ret;
549
550 ret = spi_qup_set_state(qup, QUP_STATE_RUN);
551 if (ret) {
552 dev_warn(qup->dev, "cannot set RUN state\n");
553 return ret;
554 }
555
556 ret = spi_qup_set_state(qup, QUP_STATE_PAUSE);
557 if (ret) {
558 dev_warn(qup->dev, "cannot set PAUSE state\n");
559 return ret;
560 }
561
562 if (qup->mode == QUP_IO_M_MODE_FIFO)
563 spi_qup_write(qup);
564
565 ret = spi_qup_set_state(qup, QUP_STATE_RUN);
566 if (ret) {
567 dev_warn(qup->dev, "cannot set RUN state\n");
568 return ret;
569 }
570
571 if (!wait_for_completion_timeout(&qup->done, timeout))
572 return -ETIMEDOUT;
573
574 offset++;
575 } while (iterations--);
Varadarajan Narayanan5f13fd62017-07-28 12:22:50 +0530576
Andy Gross612762e2015-03-04 12:02:05 +0200577 return 0;
578}
579
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200580static irqreturn_t spi_qup_qup_irq(int irq, void *dev_id)
581{
582 struct spi_qup *controller = dev_id;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200583 u32 opflags, qup_err, spi_err;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200584 int error = 0;
585
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200586 qup_err = readl_relaxed(controller->base + QUP_ERROR_FLAGS);
587 spi_err = readl_relaxed(controller->base + SPI_ERROR_FLAGS);
588 opflags = readl_relaxed(controller->base + QUP_OPERATIONAL);
589
590 writel_relaxed(qup_err, controller->base + QUP_ERROR_FLAGS);
591 writel_relaxed(spi_err, controller->base + SPI_ERROR_FLAGS);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200592
593 if (qup_err) {
594 if (qup_err & QUP_ERROR_OUTPUT_OVER_RUN)
595 dev_warn(controller->dev, "OUTPUT_OVER_RUN\n");
596 if (qup_err & QUP_ERROR_INPUT_UNDER_RUN)
597 dev_warn(controller->dev, "INPUT_UNDER_RUN\n");
598 if (qup_err & QUP_ERROR_OUTPUT_UNDER_RUN)
599 dev_warn(controller->dev, "OUTPUT_UNDER_RUN\n");
600 if (qup_err & QUP_ERROR_INPUT_OVER_RUN)
601 dev_warn(controller->dev, "INPUT_OVER_RUN\n");
602
603 error = -EIO;
604 }
605
606 if (spi_err) {
607 if (spi_err & SPI_ERROR_CLK_OVER_RUN)
608 dev_warn(controller->dev, "CLK_OVER_RUN\n");
609 if (spi_err & SPI_ERROR_CLK_UNDER_RUN)
610 dev_warn(controller->dev, "CLK_UNDER_RUN\n");
611
612 error = -EIO;
613 }
614
Varadarajan Narayanance7dfc72017-07-28 12:22:53 +0530615 if (spi_qup_is_dma_xfer(controller->mode)) {
616 writel_relaxed(opflags, controller->base + QUP_OPERATIONAL);
617 } else {
Andy Gross612762e2015-03-04 12:02:05 +0200618 if (opflags & QUP_OP_IN_SERVICE_FLAG)
Varadarajan Narayanancd595b92017-07-28 12:23:00 +0530619 spi_qup_read(controller, &opflags);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200620
Andy Gross612762e2015-03-04 12:02:05 +0200621 if (opflags & QUP_OP_OUT_SERVICE_FLAG)
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530622 spi_qup_write(controller);
Andy Gross612762e2015-03-04 12:02:05 +0200623 }
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200624
Varadarajan Narayanance7dfc72017-07-28 12:22:53 +0530625 if ((opflags & QUP_OP_MAX_INPUT_DONE_FLAG) || error)
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200626 complete(&controller->done);
627
628 return IRQ_HANDLED;
629}
630
Varadarajan Narayanan94b91492017-07-28 12:22:55 +0530631/* set clock freq ... bits per word, determine mode */
632static int spi_qup_io_prep(struct spi_device *spi, struct spi_transfer *xfer)
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200633{
Axel Lin00cce742014-02-24 23:07:36 +0800634 struct spi_qup *controller = spi_master_get_devdata(spi->master);
Varadarajan Narayanan94b91492017-07-28 12:22:55 +0530635 int ret;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200636
Axel Lin00cce742014-02-24 23:07:36 +0800637 if (spi->mode & SPI_LOOP && xfer->len > controller->in_fifo_sz) {
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200638 dev_err(controller->dev, "too big size for loopback %d > %d\n",
639 xfer->len, controller->in_fifo_sz);
640 return -EIO;
641 }
642
643 ret = clk_set_rate(controller->cclk, xfer->speed_hz);
644 if (ret) {
645 dev_err(controller->dev, "fail to set frequency %d",
646 xfer->speed_hz);
647 return -EIO;
648 }
649
Varadarajan Narayanan94b91492017-07-28 12:22:55 +0530650 controller->w_size = DIV_ROUND_UP(xfer->bits_per_word, 8);
651 controller->n_words = xfer->len / controller->w_size;
652
653 if (controller->n_words <= (controller->in_fifo_sz / sizeof(u32)))
654 controller->mode = QUP_IO_M_MODE_FIFO;
655 else if (spi->master->can_dma &&
656 spi->master->can_dma(spi->master, spi, xfer) &&
657 spi->master->cur_msg_mapped)
658 controller->mode = QUP_IO_M_MODE_BAM;
659 else
660 controller->mode = QUP_IO_M_MODE_BLOCK;
661
662 return 0;
663}
664
665/* prep qup for another spi transaction of specific type */
666static int spi_qup_io_config(struct spi_device *spi, struct spi_transfer *xfer)
667{
668 struct spi_qup *controller = spi_master_get_devdata(spi->master);
669 u32 config, iomode, control;
670 unsigned long flags;
671
672 spin_lock_irqsave(&controller->lock, flags);
673 controller->xfer = xfer;
674 controller->error = 0;
675 controller->rx_bytes = 0;
676 controller->tx_bytes = 0;
677 spin_unlock_irqrestore(&controller->lock, flags);
678
679
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200680 if (spi_qup_set_state(controller, QUP_STATE_RESET)) {
681 dev_err(controller->dev, "cannot set RESET state\n");
682 return -EIO;
683 }
684
Varadarajan Narayanan94b91492017-07-28 12:22:55 +0530685 switch (controller->mode) {
686 case QUP_IO_M_MODE_FIFO:
687 writel_relaxed(controller->n_words,
688 controller->base + QUP_MX_READ_CNT);
689 writel_relaxed(controller->n_words,
690 controller->base + QUP_MX_WRITE_CNT);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200691 /* must be zero for FIFO */
692 writel_relaxed(0, controller->base + QUP_MX_INPUT_CNT);
693 writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT);
Varadarajan Narayanan94b91492017-07-28 12:22:55 +0530694 break;
695 case QUP_IO_M_MODE_BAM:
696 writel_relaxed(controller->n_words,
697 controller->base + QUP_MX_INPUT_CNT);
698 writel_relaxed(controller->n_words,
699 controller->base + QUP_MX_OUTPUT_CNT);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200700 /* must be zero for BLOCK and BAM */
701 writel_relaxed(0, controller->base + QUP_MX_READ_CNT);
702 writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT);
Andy Gross612762e2015-03-04 12:02:05 +0200703
704 if (!controller->qup_v1) {
705 void __iomem *input_cnt;
706
707 input_cnt = controller->base + QUP_MX_INPUT_CNT;
708 /*
709 * for DMA transfers, both QUP_MX_INPUT_CNT and
710 * QUP_MX_OUTPUT_CNT must be zero to all cases but one.
711 * That case is a non-balanced transfer when there is
712 * only a rx_buf.
713 */
714 if (xfer->tx_buf)
715 writel_relaxed(0, input_cnt);
716 else
Varadarajan Narayanan94b91492017-07-28 12:22:55 +0530717 writel_relaxed(controller->n_words, input_cnt);
Andy Gross612762e2015-03-04 12:02:05 +0200718
719 writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT);
720 }
Varadarajan Narayanan94b91492017-07-28 12:22:55 +0530721 break;
722 case QUP_IO_M_MODE_BLOCK:
723 reinit_completion(&controller->done);
724 writel_relaxed(controller->n_words,
725 controller->base + QUP_MX_INPUT_CNT);
726 writel_relaxed(controller->n_words,
727 controller->base + QUP_MX_OUTPUT_CNT);
Varadarajan Narayanan32ecab92017-07-28 12:22:49 +0530728 /* must be zero for BLOCK and BAM */
729 writel_relaxed(0, controller->base + QUP_MX_READ_CNT);
730 writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT);
Varadarajan Narayanan94b91492017-07-28 12:22:55 +0530731 break;
732 default:
733 dev_err(controller->dev, "unknown mode = %d\n",
734 controller->mode);
735 return -EIO;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200736 }
737
738 iomode = readl_relaxed(controller->base + QUP_IO_M_MODES);
739 /* Set input and output transfer mode */
740 iomode &= ~(QUP_IO_M_INPUT_MODE_MASK | QUP_IO_M_OUTPUT_MODE_MASK);
Andy Gross612762e2015-03-04 12:02:05 +0200741
Varadarajan Narayanan32ecab92017-07-28 12:22:49 +0530742 if (!spi_qup_is_dma_xfer(controller->mode))
Andy Gross612762e2015-03-04 12:02:05 +0200743 iomode &= ~(QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN);
744 else
745 iomode |= QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN;
746
Varadarajan Narayanan32ecab92017-07-28 12:22:49 +0530747 iomode |= (controller->mode << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT);
748 iomode |= (controller->mode << QUP_IO_M_INPUT_MODE_MASK_SHIFT);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200749
750 writel_relaxed(iomode, controller->base + QUP_IO_M_MODES);
751
Ivan T. Ivanov0667dd52014-12-16 12:21:55 +0200752 control = readl_relaxed(controller->base + SPI_IO_CONTROL);
753
754 if (spi->mode & SPI_CPOL)
755 control |= SPI_IO_C_CLK_IDLE_HIGH;
756 else
757 control &= ~SPI_IO_C_CLK_IDLE_HIGH;
758
759 writel_relaxed(control, controller->base + SPI_IO_CONTROL);
760
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200761 config = readl_relaxed(controller->base + SPI_CONFIG);
762
Axel Lin00cce742014-02-24 23:07:36 +0800763 if (spi->mode & SPI_LOOP)
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200764 config |= SPI_CONFIG_LOOPBACK;
765 else
766 config &= ~SPI_CONFIG_LOOPBACK;
767
Axel Lin00cce742014-02-24 23:07:36 +0800768 if (spi->mode & SPI_CPHA)
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200769 config &= ~SPI_CONFIG_INPUT_FIRST;
770 else
771 config |= SPI_CONFIG_INPUT_FIRST;
772
773 /*
774 * HS_MODE improves signal stability for spi-clk high rates,
775 * but is invalid in loop back mode.
776 */
Axel Lin00cce742014-02-24 23:07:36 +0800777 if ((xfer->speed_hz >= SPI_HS_MIN_RATE) && !(spi->mode & SPI_LOOP))
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200778 config |= SPI_CONFIG_HS_MODE;
779 else
780 config &= ~SPI_CONFIG_HS_MODE;
781
782 writel_relaxed(config, controller->base + SPI_CONFIG);
783
784 config = readl_relaxed(controller->base + QUP_CONFIG);
785 config &= ~(QUP_CONFIG_NO_INPUT | QUP_CONFIG_NO_OUTPUT | QUP_CONFIG_N);
786 config |= xfer->bits_per_word - 1;
787 config |= QUP_CONFIG_SPI_MODE;
Andy Gross612762e2015-03-04 12:02:05 +0200788
Varadarajan Narayanan32ecab92017-07-28 12:22:49 +0530789 if (spi_qup_is_dma_xfer(controller->mode)) {
Andy Gross612762e2015-03-04 12:02:05 +0200790 if (!xfer->tx_buf)
791 config |= QUP_CONFIG_NO_OUTPUT;
792 if (!xfer->rx_buf)
793 config |= QUP_CONFIG_NO_INPUT;
794 }
795
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200796 writel_relaxed(config, controller->base + QUP_CONFIG);
797
Andy Gross70cea0a2014-06-12 14:34:12 -0500798 /* only write to OPERATIONAL_MASK when register is present */
Andy Gross612762e2015-03-04 12:02:05 +0200799 if (!controller->qup_v1) {
800 u32 mask = 0;
801
802 /*
803 * mask INPUT and OUTPUT service flags to prevent IRQs on FIFO
804 * status change in BAM mode
805 */
806
Varadarajan Narayanan32ecab92017-07-28 12:22:49 +0530807 if (spi_qup_is_dma_xfer(controller->mode))
Andy Gross612762e2015-03-04 12:02:05 +0200808 mask = QUP_OP_IN_SERVICE_FLAG | QUP_OP_OUT_SERVICE_FLAG;
809
810 writel_relaxed(mask, controller->base + QUP_OPERATIONAL_MASK);
811 }
812
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200813 return 0;
814}
815
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200816static int spi_qup_transfer_one(struct spi_master *master,
817 struct spi_device *spi,
818 struct spi_transfer *xfer)
819{
820 struct spi_qup *controller = spi_master_get_devdata(master);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200821 unsigned long timeout, flags;
822 int ret = -EIO;
823
Varadarajan Narayanan94b91492017-07-28 12:22:55 +0530824 ret = spi_qup_io_prep(spi, xfer);
825 if (ret)
826 return ret;
827
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200828 timeout = DIV_ROUND_UP(xfer->speed_hz, MSEC_PER_SEC);
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +0530829 timeout = DIV_ROUND_UP(min_t(unsigned long, SPI_MAX_XFER,
830 xfer->len) * 8, timeout);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200831 timeout = 100 * msecs_to_jiffies(timeout);
832
833 reinit_completion(&controller->done);
834
835 spin_lock_irqsave(&controller->lock, flags);
836 controller->xfer = xfer;
837 controller->error = 0;
838 controller->rx_bytes = 0;
839 controller->tx_bytes = 0;
840 spin_unlock_irqrestore(&controller->lock, flags);
841
Varadarajan Narayanan32ecab92017-07-28 12:22:49 +0530842 if (spi_qup_is_dma_xfer(controller->mode))
Varadarajan Narayanan3b5ea2c2017-07-28 12:22:56 +0530843 ret = spi_qup_do_dma(spi, xfer, timeout);
Andy Gross612762e2015-03-04 12:02:05 +0200844 else
Varadarajan Narayanan3b5ea2c2017-07-28 12:22:56 +0530845 ret = spi_qup_do_pio(spi, xfer, timeout);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200846
Andy Gross612762e2015-03-04 12:02:05 +0200847 if (ret)
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200848 goto exit;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200849
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200850exit:
851 spi_qup_set_state(controller, QUP_STATE_RESET);
852 spin_lock_irqsave(&controller->lock, flags);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200853 if (!ret)
854 ret = controller->error;
855 spin_unlock_irqrestore(&controller->lock, flags);
Andy Gross612762e2015-03-04 12:02:05 +0200856
Varadarajan Narayanan32ecab92017-07-28 12:22:49 +0530857 if (ret && spi_qup_is_dma_xfer(controller->mode))
Andy Gross612762e2015-03-04 12:02:05 +0200858 spi_qup_dma_terminate(master, xfer);
859
860 return ret;
861}
862
863static bool spi_qup_can_dma(struct spi_master *master, struct spi_device *spi,
864 struct spi_transfer *xfer)
865{
866 struct spi_qup *qup = spi_master_get_devdata(master);
867 size_t dma_align = dma_get_cache_alignment();
Varadarajan Narayanan32ecab92017-07-28 12:22:49 +0530868 int n_words;
Andy Gross612762e2015-03-04 12:02:05 +0200869
Varadarajan Narayanan32ecab92017-07-28 12:22:49 +0530870 if (xfer->rx_buf) {
871 if (!IS_ALIGNED((size_t)xfer->rx_buf, dma_align) ||
872 IS_ERR_OR_NULL(master->dma_rx))
873 return false;
874 if (qup->qup_v1 && (xfer->len % qup->in_blk_sz))
875 return false;
876 }
Andy Gross612762e2015-03-04 12:02:05 +0200877
Varadarajan Narayanan32ecab92017-07-28 12:22:49 +0530878 if (xfer->tx_buf) {
879 if (!IS_ALIGNED((size_t)xfer->tx_buf, dma_align) ||
880 IS_ERR_OR_NULL(master->dma_tx))
881 return false;
882 if (qup->qup_v1 && (xfer->len % qup->out_blk_sz))
883 return false;
884 }
885
886 n_words = xfer->len / DIV_ROUND_UP(xfer->bits_per_word, 8);
887 if (n_words <= (qup->in_fifo_sz / sizeof(u32)))
Andy Gross612762e2015-03-04 12:02:05 +0200888 return false;
889
Andy Gross612762e2015-03-04 12:02:05 +0200890 return true;
891}
892
893static void spi_qup_release_dma(struct spi_master *master)
894{
895 if (!IS_ERR_OR_NULL(master->dma_rx))
896 dma_release_channel(master->dma_rx);
897 if (!IS_ERR_OR_NULL(master->dma_tx))
898 dma_release_channel(master->dma_tx);
899}
900
901static int spi_qup_init_dma(struct spi_master *master, resource_size_t base)
902{
903 struct spi_qup *spi = spi_master_get_devdata(master);
904 struct dma_slave_config *rx_conf = &spi->rx_conf,
905 *tx_conf = &spi->tx_conf;
906 struct device *dev = spi->dev;
907 int ret;
908
909 /* allocate dma resources, if available */
910 master->dma_rx = dma_request_slave_channel_reason(dev, "rx");
911 if (IS_ERR(master->dma_rx))
912 return PTR_ERR(master->dma_rx);
913
914 master->dma_tx = dma_request_slave_channel_reason(dev, "tx");
915 if (IS_ERR(master->dma_tx)) {
916 ret = PTR_ERR(master->dma_tx);
917 goto err_tx;
918 }
919
920 /* set DMA parameters */
921 rx_conf->direction = DMA_DEV_TO_MEM;
922 rx_conf->device_fc = 1;
923 rx_conf->src_addr = base + QUP_INPUT_FIFO;
924 rx_conf->src_maxburst = spi->in_blk_sz;
925
926 tx_conf->direction = DMA_MEM_TO_DEV;
927 tx_conf->device_fc = 1;
928 tx_conf->dst_addr = base + QUP_OUTPUT_FIFO;
929 tx_conf->dst_maxburst = spi->out_blk_sz;
930
931 ret = dmaengine_slave_config(master->dma_rx, rx_conf);
932 if (ret) {
933 dev_err(dev, "failed to configure RX channel\n");
934 goto err;
935 }
936
937 ret = dmaengine_slave_config(master->dma_tx, tx_conf);
938 if (ret) {
939 dev_err(dev, "failed to configure TX channel\n");
940 goto err;
941 }
942
943 return 0;
944
945err:
946 dma_release_channel(master->dma_tx);
947err_tx:
948 dma_release_channel(master->dma_rx);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200949 return ret;
950}
951
Varadarajan Narayananb702b9f2017-07-28 12:22:48 +0530952static void spi_qup_set_cs(struct spi_device *spi, bool val)
953{
954 struct spi_qup *controller;
955 u32 spi_ioc;
956 u32 spi_ioc_orig;
957
958 controller = spi_master_get_devdata(spi->master);
959 spi_ioc = readl_relaxed(controller->base + SPI_IO_CONTROL);
960 spi_ioc_orig = spi_ioc;
961 if (!val)
962 spi_ioc |= SPI_IO_C_FORCE_CS;
963 else
964 spi_ioc &= ~SPI_IO_C_FORCE_CS;
965
966 if (spi_ioc != spi_ioc_orig)
967 writel_relaxed(spi_ioc, controller->base + SPI_IO_CONTROL);
968}
969
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200970static int spi_qup_probe(struct platform_device *pdev)
971{
972 struct spi_master *master;
973 struct clk *iclk, *cclk;
974 struct spi_qup *controller;
975 struct resource *res;
976 struct device *dev;
977 void __iomem *base;
Ivan T. Ivanov12cb89e2015-03-06 17:26:17 +0200978 u32 max_freq, iomode, num_cs;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200979 int ret, irq, size;
980
981 dev = &pdev->dev;
982 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
983 base = devm_ioremap_resource(dev, res);
984 if (IS_ERR(base))
985 return PTR_ERR(base);
986
987 irq = platform_get_irq(pdev, 0);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +0200988 if (irq < 0)
989 return irq;
990
991 cclk = devm_clk_get(dev, "core");
992 if (IS_ERR(cclk))
993 return PTR_ERR(cclk);
994
995 iclk = devm_clk_get(dev, "iface");
996 if (IS_ERR(iclk))
997 return PTR_ERR(iclk);
998
999 /* This is optional parameter */
1000 if (of_property_read_u32(dev->of_node, "spi-max-frequency", &max_freq))
1001 max_freq = SPI_MAX_RATE;
1002
1003 if (!max_freq || max_freq > SPI_MAX_RATE) {
1004 dev_err(dev, "invalid clock frequency %d\n", max_freq);
1005 return -ENXIO;
1006 }
1007
1008 ret = clk_prepare_enable(cclk);
1009 if (ret) {
1010 dev_err(dev, "cannot enable core clock\n");
1011 return ret;
1012 }
1013
1014 ret = clk_prepare_enable(iclk);
1015 if (ret) {
1016 clk_disable_unprepare(cclk);
1017 dev_err(dev, "cannot enable iface clock\n");
1018 return ret;
1019 }
1020
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001021 master = spi_alloc_master(dev, sizeof(struct spi_qup));
1022 if (!master) {
1023 clk_disable_unprepare(cclk);
1024 clk_disable_unprepare(iclk);
1025 dev_err(dev, "cannot allocate master\n");
1026 return -ENOMEM;
1027 }
1028
Andy Gross4a8573a2014-06-12 14:34:10 -05001029 /* use num-cs unless not present or out of range */
Ivan T. Ivanov12cb89e2015-03-06 17:26:17 +02001030 if (of_property_read_u32(dev->of_node, "num-cs", &num_cs) ||
1031 num_cs > SPI_NUM_CHIPSELECTS)
Andy Gross4a8573a2014-06-12 14:34:10 -05001032 master->num_chipselect = SPI_NUM_CHIPSELECTS;
Ivan T. Ivanov12cb89e2015-03-06 17:26:17 +02001033 else
1034 master->num_chipselect = num_cs;
Andy Gross4a8573a2014-06-12 14:34:10 -05001035
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001036 master->bus_num = pdev->id;
1037 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001038 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Axel Lincb64ca52014-02-21 09:34:16 +08001039 master->max_speed_hz = max_freq;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001040 master->transfer_one = spi_qup_transfer_one;
1041 master->dev.of_node = pdev->dev.of_node;
1042 master->auto_runtime_pm = true;
Andy Gross612762e2015-03-04 12:02:05 +02001043 master->dma_alignment = dma_get_cache_alignment();
Varadarajan Narayanan5dc47fef2017-07-28 12:22:57 +05301044 master->max_dma_len = SPI_MAX_XFER;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001045
1046 platform_set_drvdata(pdev, master);
1047
1048 controller = spi_master_get_devdata(master);
1049
1050 controller->dev = dev;
1051 controller->base = base;
1052 controller->iclk = iclk;
1053 controller->cclk = cclk;
1054 controller->irq = irq;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001055
Andy Gross612762e2015-03-04 12:02:05 +02001056 ret = spi_qup_init_dma(master, res->start);
1057 if (ret == -EPROBE_DEFER)
1058 goto error;
1059 else if (!ret)
1060 master->can_dma = spi_qup_can_dma;
1061
Varadarajan Narayanan4d023732017-07-28 12:23:01 +05301062 controller->qup_v1 = (int)of_device_get_match_data(dev);
Andy Gross70cea0a2014-06-12 14:34:12 -05001063
Varadarajan Narayananb702b9f2017-07-28 12:22:48 +05301064 if (!controller->qup_v1)
1065 master->set_cs = spi_qup_set_cs;
1066
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001067 spin_lock_init(&controller->lock);
1068 init_completion(&controller->done);
1069
1070 iomode = readl_relaxed(base + QUP_IO_M_MODES);
1071
1072 size = QUP_IO_M_OUTPUT_BLOCK_SIZE(iomode);
1073 if (size)
1074 controller->out_blk_sz = size * 16;
1075 else
1076 controller->out_blk_sz = 4;
1077
1078 size = QUP_IO_M_INPUT_BLOCK_SIZE(iomode);
1079 if (size)
1080 controller->in_blk_sz = size * 16;
1081 else
1082 controller->in_blk_sz = 4;
1083
1084 size = QUP_IO_M_OUTPUT_FIFO_SIZE(iomode);
1085 controller->out_fifo_sz = controller->out_blk_sz * (2 << size);
1086
1087 size = QUP_IO_M_INPUT_FIFO_SIZE(iomode);
1088 controller->in_fifo_sz = controller->in_blk_sz * (2 << size);
1089
Andy Gross70cea0a2014-06-12 14:34:12 -05001090 dev_info(dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n",
1091 controller->in_blk_sz, controller->in_fifo_sz,
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001092 controller->out_blk_sz, controller->out_fifo_sz);
1093
1094 writel_relaxed(1, base + QUP_SW_RESET);
1095
1096 ret = spi_qup_set_state(controller, QUP_STATE_RESET);
1097 if (ret) {
1098 dev_err(dev, "cannot set RESET state\n");
Andy Gross612762e2015-03-04 12:02:05 +02001099 goto error_dma;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001100 }
1101
1102 writel_relaxed(0, base + QUP_OPERATIONAL);
1103 writel_relaxed(0, base + QUP_IO_M_MODES);
Andy Gross70cea0a2014-06-12 14:34:12 -05001104
1105 if (!controller->qup_v1)
1106 writel_relaxed(0, base + QUP_OPERATIONAL_MASK);
1107
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001108 writel_relaxed(SPI_ERROR_CLK_UNDER_RUN | SPI_ERROR_CLK_OVER_RUN,
1109 base + SPI_ERROR_FLAGS_EN);
1110
Andy Gross70cea0a2014-06-12 14:34:12 -05001111 /* if earlier version of the QUP, disable INPUT_OVERRUN */
1112 if (controller->qup_v1)
1113 writel_relaxed(QUP_ERROR_OUTPUT_OVER_RUN |
1114 QUP_ERROR_INPUT_UNDER_RUN | QUP_ERROR_OUTPUT_UNDER_RUN,
1115 base + QUP_ERROR_FLAGS_EN);
1116
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001117 writel_relaxed(0, base + SPI_CONFIG);
1118 writel_relaxed(SPI_IO_C_NO_TRI_STATE, base + SPI_IO_CONTROL);
1119
1120 ret = devm_request_irq(dev, irq, spi_qup_qup_irq,
1121 IRQF_TRIGGER_HIGH, pdev->name, controller);
1122 if (ret)
Andy Gross612762e2015-03-04 12:02:05 +02001123 goto error_dma;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001124
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001125 pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
1126 pm_runtime_use_autosuspend(dev);
1127 pm_runtime_set_active(dev);
1128 pm_runtime_enable(dev);
Andy Gross045c2432014-06-12 14:34:11 -05001129
1130 ret = devm_spi_register_master(dev, master);
1131 if (ret)
1132 goto disable_pm;
1133
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001134 return 0;
1135
Andy Gross045c2432014-06-12 14:34:11 -05001136disable_pm:
1137 pm_runtime_disable(&pdev->dev);
Andy Gross612762e2015-03-04 12:02:05 +02001138error_dma:
1139 spi_qup_release_dma(master);
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001140error:
1141 clk_disable_unprepare(cclk);
1142 clk_disable_unprepare(iclk);
1143 spi_master_put(master);
1144 return ret;
1145}
1146
Rafael J. Wysockiec833052014-12-13 00:41:15 +01001147#ifdef CONFIG_PM
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001148static int spi_qup_pm_suspend_runtime(struct device *device)
1149{
1150 struct spi_master *master = dev_get_drvdata(device);
1151 struct spi_qup *controller = spi_master_get_devdata(master);
1152 u32 config;
1153
1154 /* Enable clocks auto gaiting */
1155 config = readl(controller->base + QUP_CONFIG);
Axel Linf0ceb112014-02-23 13:27:16 +08001156 config |= QUP_CONFIG_CLOCK_AUTO_GATE;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001157 writel_relaxed(config, controller->base + QUP_CONFIG);
Pramod Guravdae1a772016-05-02 17:44:03 +05301158
1159 clk_disable_unprepare(controller->cclk);
1160 clk_disable_unprepare(controller->iclk);
1161
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001162 return 0;
1163}
1164
1165static int spi_qup_pm_resume_runtime(struct device *device)
1166{
1167 struct spi_master *master = dev_get_drvdata(device);
1168 struct spi_qup *controller = spi_master_get_devdata(master);
1169 u32 config;
Pramod Guravdae1a772016-05-02 17:44:03 +05301170 int ret;
1171
1172 ret = clk_prepare_enable(controller->iclk);
1173 if (ret)
1174 return ret;
1175
1176 ret = clk_prepare_enable(controller->cclk);
1177 if (ret)
1178 return ret;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001179
1180 /* Disable clocks auto gaiting */
1181 config = readl_relaxed(controller->base + QUP_CONFIG);
Axel Linf0ceb112014-02-23 13:27:16 +08001182 config &= ~QUP_CONFIG_CLOCK_AUTO_GATE;
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001183 writel_relaxed(config, controller->base + QUP_CONFIG);
1184 return 0;
1185}
Rafael J. Wysockiec833052014-12-13 00:41:15 +01001186#endif /* CONFIG_PM */
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001187
1188#ifdef CONFIG_PM_SLEEP
1189static int spi_qup_suspend(struct device *device)
1190{
1191 struct spi_master *master = dev_get_drvdata(device);
1192 struct spi_qup *controller = spi_master_get_devdata(master);
1193 int ret;
1194
1195 ret = spi_master_suspend(master);
1196 if (ret)
1197 return ret;
1198
1199 ret = spi_qup_set_state(controller, QUP_STATE_RESET);
1200 if (ret)
1201 return ret;
1202
Sudeep Holla9d04d8b2016-08-25 13:33:28 +01001203 if (!pm_runtime_suspended(device)) {
1204 clk_disable_unprepare(controller->cclk);
1205 clk_disable_unprepare(controller->iclk);
1206 }
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001207 return 0;
1208}
1209
1210static int spi_qup_resume(struct device *device)
1211{
1212 struct spi_master *master = dev_get_drvdata(device);
1213 struct spi_qup *controller = spi_master_get_devdata(master);
1214 int ret;
1215
1216 ret = clk_prepare_enable(controller->iclk);
1217 if (ret)
1218 return ret;
1219
1220 ret = clk_prepare_enable(controller->cclk);
1221 if (ret)
1222 return ret;
1223
1224 ret = spi_qup_set_state(controller, QUP_STATE_RESET);
1225 if (ret)
1226 return ret;
1227
1228 return spi_master_resume(master);
1229}
1230#endif /* CONFIG_PM_SLEEP */
1231
1232static int spi_qup_remove(struct platform_device *pdev)
1233{
1234 struct spi_master *master = dev_get_drvdata(&pdev->dev);
1235 struct spi_qup *controller = spi_master_get_devdata(master);
1236 int ret;
1237
1238 ret = pm_runtime_get_sync(&pdev->dev);
Axel Lin3d89e142014-05-03 10:57:57 +08001239 if (ret < 0)
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001240 return ret;
1241
1242 ret = spi_qup_set_state(controller, QUP_STATE_RESET);
1243 if (ret)
1244 return ret;
1245
Andy Gross612762e2015-03-04 12:02:05 +02001246 spi_qup_release_dma(master);
1247
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001248 clk_disable_unprepare(controller->cclk);
1249 clk_disable_unprepare(controller->iclk);
1250
1251 pm_runtime_put_noidle(&pdev->dev);
1252 pm_runtime_disable(&pdev->dev);
Pramod Guravd2442282016-05-02 17:44:04 +05301253
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001254 return 0;
1255}
1256
Jingoo Han113b1a02014-05-07 16:50:04 +09001257static const struct of_device_id spi_qup_dt_match[] = {
Varadarajan Narayanan4d023732017-07-28 12:23:01 +05301258 { .compatible = "qcom,spi-qup-v1.1.1", .data = (void *)1, },
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001259 { .compatible = "qcom,spi-qup-v2.1.1", },
1260 { .compatible = "qcom,spi-qup-v2.2.1", },
1261 { }
1262};
1263MODULE_DEVICE_TABLE(of, spi_qup_dt_match);
1264
1265static const struct dev_pm_ops spi_qup_dev_pm_ops = {
1266 SET_SYSTEM_SLEEP_PM_OPS(spi_qup_suspend, spi_qup_resume)
1267 SET_RUNTIME_PM_OPS(spi_qup_pm_suspend_runtime,
1268 spi_qup_pm_resume_runtime,
1269 NULL)
1270};
1271
1272static struct platform_driver spi_qup_driver = {
1273 .driver = {
1274 .name = "spi_qup",
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001275 .pm = &spi_qup_dev_pm_ops,
1276 .of_match_table = spi_qup_dt_match,
1277 },
1278 .probe = spi_qup_probe,
1279 .remove = spi_qup_remove,
1280};
1281module_platform_driver(spi_qup_driver);
1282
1283MODULE_LICENSE("GPL v2");
Ivan T. Ivanov64ff2472014-02-13 18:21:38 +02001284MODULE_ALIAS("platform:spi_qup");