Commit | Line | Data |
---|---|---|
64ff247a II |
1 | /* |
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> | |
22 | #include <linux/platform_device.h> | |
23 | #include <linux/pm_runtime.h> | |
24 | #include <linux/spi/spi.h> | |
612762e8 AG |
25 | #include <linux/dmaengine.h> |
26 | #include <linux/dma-mapping.h> | |
64ff247a II |
27 | |
28 | #define QUP_CONFIG 0x0000 | |
29 | #define QUP_STATE 0x0004 | |
30 | #define QUP_IO_M_MODES 0x0008 | |
31 | #define QUP_SW_RESET 0x000c | |
32 | #define QUP_OPERATIONAL 0x0018 | |
33 | #define QUP_ERROR_FLAGS 0x001c | |
34 | #define QUP_ERROR_FLAGS_EN 0x0020 | |
35 | #define QUP_OPERATIONAL_MASK 0x0028 | |
36 | #define QUP_HW_VERSION 0x0030 | |
37 | #define QUP_MX_OUTPUT_CNT 0x0100 | |
38 | #define QUP_OUTPUT_FIFO 0x0110 | |
39 | #define QUP_MX_WRITE_CNT 0x0150 | |
40 | #define QUP_MX_INPUT_CNT 0x0200 | |
41 | #define QUP_MX_READ_CNT 0x0208 | |
42 | #define QUP_INPUT_FIFO 0x0218 | |
43 | ||
44 | #define SPI_CONFIG 0x0300 | |
45 | #define SPI_IO_CONTROL 0x0304 | |
46 | #define SPI_ERROR_FLAGS 0x0308 | |
47 | #define SPI_ERROR_FLAGS_EN 0x030c | |
48 | ||
49 | /* QUP_CONFIG fields */ | |
50 | #define QUP_CONFIG_SPI_MODE (1 << 8) | |
51 | #define QUP_CONFIG_CLOCK_AUTO_GATE BIT(13) | |
52 | #define QUP_CONFIG_NO_INPUT BIT(7) | |
53 | #define QUP_CONFIG_NO_OUTPUT BIT(6) | |
54 | #define QUP_CONFIG_N 0x001f | |
55 | ||
56 | /* QUP_STATE fields */ | |
57 | #define QUP_STATE_VALID BIT(2) | |
58 | #define QUP_STATE_RESET 0 | |
59 | #define QUP_STATE_RUN 1 | |
60 | #define QUP_STATE_PAUSE 3 | |
61 | #define QUP_STATE_MASK 3 | |
62 | #define QUP_STATE_CLEAR 2 | |
63 | ||
64 | #define QUP_HW_VERSION_2_1_1 0x20010001 | |
65 | ||
66 | /* QUP_IO_M_MODES fields */ | |
67 | #define QUP_IO_M_PACK_EN BIT(15) | |
68 | #define QUP_IO_M_UNPACK_EN BIT(14) | |
69 | #define QUP_IO_M_INPUT_MODE_MASK_SHIFT 12 | |
70 | #define QUP_IO_M_OUTPUT_MODE_MASK_SHIFT 10 | |
71 | #define QUP_IO_M_INPUT_MODE_MASK (3 << QUP_IO_M_INPUT_MODE_MASK_SHIFT) | |
72 | #define QUP_IO_M_OUTPUT_MODE_MASK (3 << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT) | |
73 | ||
74 | #define QUP_IO_M_OUTPUT_BLOCK_SIZE(x) (((x) & (0x03 << 0)) >> 0) | |
75 | #define QUP_IO_M_OUTPUT_FIFO_SIZE(x) (((x) & (0x07 << 2)) >> 2) | |
76 | #define QUP_IO_M_INPUT_BLOCK_SIZE(x) (((x) & (0x03 << 5)) >> 5) | |
77 | #define QUP_IO_M_INPUT_FIFO_SIZE(x) (((x) & (0x07 << 7)) >> 7) | |
78 | ||
79 | #define QUP_IO_M_MODE_FIFO 0 | |
80 | #define QUP_IO_M_MODE_BLOCK 1 | |
81 | #define QUP_IO_M_MODE_DMOV 2 | |
82 | #define QUP_IO_M_MODE_BAM 3 | |
83 | ||
84 | /* QUP_OPERATIONAL fields */ | |
7538726f VN |
85 | #define QUP_OP_IN_BLOCK_READ_REQ BIT(13) |
86 | #define QUP_OP_OUT_BLOCK_WRITE_REQ BIT(12) | |
64ff247a II |
87 | #define QUP_OP_MAX_INPUT_DONE_FLAG BIT(11) |
88 | #define QUP_OP_MAX_OUTPUT_DONE_FLAG BIT(10) | |
89 | #define QUP_OP_IN_SERVICE_FLAG BIT(9) | |
90 | #define QUP_OP_OUT_SERVICE_FLAG BIT(8) | |
91 | #define QUP_OP_IN_FIFO_FULL BIT(7) | |
92 | #define QUP_OP_OUT_FIFO_FULL BIT(6) | |
93 | #define QUP_OP_IN_FIFO_NOT_EMPTY BIT(5) | |
94 | #define QUP_OP_OUT_FIFO_NOT_EMPTY BIT(4) | |
95 | ||
96 | /* QUP_ERROR_FLAGS and QUP_ERROR_FLAGS_EN fields */ | |
97 | #define QUP_ERROR_OUTPUT_OVER_RUN BIT(5) | |
98 | #define QUP_ERROR_INPUT_UNDER_RUN BIT(4) | |
99 | #define QUP_ERROR_OUTPUT_UNDER_RUN BIT(3) | |
100 | #define QUP_ERROR_INPUT_OVER_RUN BIT(2) | |
101 | ||
102 | /* SPI_CONFIG fields */ | |
103 | #define SPI_CONFIG_HS_MODE BIT(10) | |
104 | #define SPI_CONFIG_INPUT_FIRST BIT(9) | |
105 | #define SPI_CONFIG_LOOPBACK BIT(8) | |
106 | ||
107 | /* SPI_IO_CONTROL fields */ | |
108 | #define SPI_IO_C_FORCE_CS BIT(11) | |
109 | #define SPI_IO_C_CLK_IDLE_HIGH BIT(10) | |
110 | #define SPI_IO_C_MX_CS_MODE BIT(8) | |
111 | #define SPI_IO_C_CS_N_POLARITY_0 BIT(4) | |
112 | #define SPI_IO_C_CS_SELECT(x) (((x) & 3) << 2) | |
113 | #define SPI_IO_C_CS_SELECT_MASK 0x000c | |
114 | #define SPI_IO_C_TRISTATE_CS BIT(1) | |
115 | #define SPI_IO_C_NO_TRI_STATE BIT(0) | |
116 | ||
117 | /* SPI_ERROR_FLAGS and SPI_ERROR_FLAGS_EN fields */ | |
118 | #define SPI_ERROR_CLK_OVER_RUN BIT(1) | |
119 | #define SPI_ERROR_CLK_UNDER_RUN BIT(0) | |
120 | ||
121 | #define SPI_NUM_CHIPSELECTS 4 | |
122 | ||
612762e8 AG |
123 | #define SPI_MAX_DMA_XFER (SZ_64K - 64) |
124 | ||
64ff247a II |
125 | /* high speed mode is when bus rate is greater then 26MHz */ |
126 | #define SPI_HS_MIN_RATE 26000000 | |
127 | #define SPI_MAX_RATE 50000000 | |
128 | ||
129 | #define SPI_DELAY_THRESHOLD 1 | |
130 | #define SPI_DELAY_RETRY 10 | |
131 | ||
64ff247a II |
132 | struct spi_qup { |
133 | void __iomem *base; | |
134 | struct device *dev; | |
135 | struct clk *cclk; /* core clock */ | |
136 | struct clk *iclk; /* interface clock */ | |
137 | int irq; | |
64ff247a II |
138 | spinlock_t lock; |
139 | ||
140 | int in_fifo_sz; | |
141 | int out_fifo_sz; | |
142 | int in_blk_sz; | |
143 | int out_blk_sz; | |
144 | ||
145 | struct spi_transfer *xfer; | |
146 | struct completion done; | |
147 | int error; | |
148 | int w_size; /* bytes per SPI word */ | |
612762e8 | 149 | int n_words; |
64ff247a II |
150 | int tx_bytes; |
151 | int rx_bytes; | |
70cea0a9 | 152 | int qup_v1; |
612762e8 | 153 | |
32ecab99 | 154 | int mode; |
612762e8 AG |
155 | struct dma_slave_config rx_conf; |
156 | struct dma_slave_config tx_conf; | |
64ff247a II |
157 | }; |
158 | ||
7538726f VN |
159 | static inline bool spi_qup_is_flag_set(struct spi_qup *controller, u32 flag) |
160 | { | |
161 | u32 opflag = readl_relaxed(controller->base + QUP_OPERATIONAL); | |
162 | ||
163 | return (opflag & flag) != 0; | |
164 | } | |
165 | ||
32ecab99 VN |
166 | static inline bool spi_qup_is_dma_xfer(int mode) |
167 | { | |
168 | if (mode == QUP_IO_M_MODE_DMOV || mode == QUP_IO_M_MODE_BAM) | |
169 | return true; | |
170 | ||
171 | return false; | |
172 | } | |
64ff247a II |
173 | |
174 | static inline bool spi_qup_is_valid_state(struct spi_qup *controller) | |
175 | { | |
176 | u32 opstate = readl_relaxed(controller->base + QUP_STATE); | |
177 | ||
178 | return opstate & QUP_STATE_VALID; | |
179 | } | |
180 | ||
181 | static int spi_qup_set_state(struct spi_qup *controller, u32 state) | |
182 | { | |
183 | unsigned long loop; | |
184 | u32 cur_state; | |
185 | ||
186 | loop = 0; | |
187 | while (!spi_qup_is_valid_state(controller)) { | |
188 | ||
189 | usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2); | |
190 | ||
191 | if (++loop > SPI_DELAY_RETRY) | |
192 | return -EIO; | |
193 | } | |
194 | ||
195 | if (loop) | |
196 | dev_dbg(controller->dev, "invalid state for %ld,us %d\n", | |
197 | loop, state); | |
198 | ||
199 | cur_state = readl_relaxed(controller->base + QUP_STATE); | |
200 | /* | |
201 | * Per spec: for PAUSE_STATE to RESET_STATE, two writes | |
202 | * of (b10) are required | |
203 | */ | |
204 | if (((cur_state & QUP_STATE_MASK) == QUP_STATE_PAUSE) && | |
205 | (state == QUP_STATE_RESET)) { | |
206 | writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE); | |
207 | writel_relaxed(QUP_STATE_CLEAR, controller->base + QUP_STATE); | |
208 | } else { | |
209 | cur_state &= ~QUP_STATE_MASK; | |
210 | cur_state |= state; | |
211 | writel_relaxed(cur_state, controller->base + QUP_STATE); | |
212 | } | |
213 | ||
214 | loop = 0; | |
215 | while (!spi_qup_is_valid_state(controller)) { | |
216 | ||
217 | usleep_range(SPI_DELAY_THRESHOLD, SPI_DELAY_THRESHOLD * 2); | |
218 | ||
219 | if (++loop > SPI_DELAY_RETRY) | |
220 | return -EIO; | |
221 | } | |
222 | ||
223 | return 0; | |
224 | } | |
225 | ||
7538726f VN |
226 | static void spi_qup_read_from_fifo(struct spi_qup *controller, |
227 | struct spi_transfer *xfer, u32 num_words) | |
64ff247a II |
228 | { |
229 | u8 *rx_buf = xfer->rx_buf; | |
7538726f VN |
230 | int i, shift, num_bytes; |
231 | u32 word; | |
64ff247a | 232 | |
7538726f | 233 | for (; num_words; num_words--) { |
64ff247a II |
234 | |
235 | word = readl_relaxed(controller->base + QUP_INPUT_FIFO); | |
236 | ||
7538726f VN |
237 | num_bytes = min_t(int, xfer->len - controller->rx_bytes, |
238 | controller->w_size); | |
239 | ||
64ff247a | 240 | if (!rx_buf) { |
7538726f | 241 | controller->rx_bytes += num_bytes; |
64ff247a II |
242 | continue; |
243 | } | |
244 | ||
7538726f | 245 | for (i = 0; i < num_bytes; i++, controller->rx_bytes++) { |
64ff247a II |
246 | /* |
247 | * The data format depends on bytes per SPI word: | |
248 | * 4 bytes: 0x12345678 | |
249 | * 2 bytes: 0x00001234 | |
250 | * 1 byte : 0x00000012 | |
251 | */ | |
252 | shift = BITS_PER_BYTE; | |
7538726f | 253 | shift *= (controller->w_size - i - 1); |
64ff247a II |
254 | rx_buf[controller->rx_bytes] = word >> shift; |
255 | } | |
256 | } | |
257 | } | |
258 | ||
7538726f | 259 | static void spi_qup_read(struct spi_qup *controller, |
64ff247a II |
260 | struct spi_transfer *xfer) |
261 | { | |
7538726f VN |
262 | u32 remainder, words_per_block, num_words; |
263 | bool is_block_mode = controller->mode == QUP_IO_M_MODE_BLOCK; | |
264 | ||
265 | remainder = DIV_ROUND_UP(xfer->len - controller->rx_bytes, | |
266 | controller->w_size); | |
267 | words_per_block = controller->in_blk_sz >> 2; | |
268 | ||
269 | do { | |
270 | /* ACK by clearing service flag */ | |
271 | writel_relaxed(QUP_OP_IN_SERVICE_FLAG, | |
272 | controller->base + QUP_OPERATIONAL); | |
273 | ||
274 | if (is_block_mode) { | |
275 | num_words = (remainder > words_per_block) ? | |
276 | words_per_block : remainder; | |
277 | } else { | |
278 | if (!spi_qup_is_flag_set(controller, | |
279 | QUP_OP_IN_FIFO_NOT_EMPTY)) | |
280 | break; | |
64ff247a | 281 | |
7538726f VN |
282 | num_words = 1; |
283 | } | |
64ff247a | 284 | |
7538726f VN |
285 | /* read up to the maximum transfer size available */ |
286 | spi_qup_read_from_fifo(controller, xfer, num_words); | |
64ff247a | 287 | |
7538726f VN |
288 | remainder -= num_words; |
289 | ||
290 | /* if block mode, check to see if next block is available */ | |
291 | if (is_block_mode && !spi_qup_is_flag_set(controller, | |
292 | QUP_OP_IN_BLOCK_READ_REQ)) | |
64ff247a II |
293 | break; |
294 | ||
7538726f VN |
295 | } while (remainder); |
296 | ||
297 | /* | |
298 | * Due to extra stickiness of the QUP_OP_IN_SERVICE_FLAG during block | |
299 | * mode reads, it has to be cleared again at the very end | |
300 | */ | |
301 | if (is_block_mode && spi_qup_is_flag_set(controller, | |
302 | QUP_OP_MAX_INPUT_DONE_FLAG)) | |
303 | writel_relaxed(QUP_OP_IN_SERVICE_FLAG, | |
304 | controller->base + QUP_OPERATIONAL); | |
305 | ||
306 | } | |
307 | ||
308 | static void spi_qup_write_to_fifo(struct spi_qup *controller, | |
309 | struct spi_transfer *xfer, u32 num_words) | |
310 | { | |
311 | const u8 *tx_buf = xfer->tx_buf; | |
312 | int i, num_bytes; | |
313 | u32 word, data; | |
314 | ||
315 | for (; num_words; num_words--) { | |
64ff247a | 316 | word = 0; |
64ff247a | 317 | |
7538726f VN |
318 | num_bytes = min_t(int, xfer->len - controller->tx_bytes, |
319 | controller->w_size); | |
320 | if (tx_buf) | |
321 | for (i = 0; i < num_bytes; i++) { | |
322 | data = tx_buf[controller->tx_bytes + i]; | |
323 | word |= data << (BITS_PER_BYTE * (3 - i)); | |
64ff247a II |
324 | } |
325 | ||
7538726f | 326 | controller->tx_bytes += num_bytes; |
64ff247a II |
327 | |
328 | writel_relaxed(word, controller->base + QUP_OUTPUT_FIFO); | |
329 | } | |
330 | } | |
331 | ||
612762e8 AG |
332 | static void spi_qup_dma_done(void *data) |
333 | { | |
334 | struct spi_qup *qup = data; | |
335 | ||
336 | complete(&qup->done); | |
337 | } | |
338 | ||
7538726f VN |
339 | static void spi_qup_write(struct spi_qup *controller, |
340 | struct spi_transfer *xfer) | |
341 | { | |
342 | bool is_block_mode = controller->mode == QUP_IO_M_MODE_BLOCK; | |
343 | u32 remainder, words_per_block, num_words; | |
344 | ||
345 | remainder = DIV_ROUND_UP(xfer->len - controller->tx_bytes, | |
346 | controller->w_size); | |
347 | words_per_block = controller->out_blk_sz >> 2; | |
348 | ||
349 | do { | |
350 | /* ACK by clearing service flag */ | |
351 | writel_relaxed(QUP_OP_OUT_SERVICE_FLAG, | |
352 | controller->base + QUP_OPERATIONAL); | |
353 | ||
354 | if (is_block_mode) { | |
355 | num_words = (remainder > words_per_block) ? | |
356 | words_per_block : remainder; | |
357 | } else { | |
358 | if (spi_qup_is_flag_set(controller, | |
359 | QUP_OP_OUT_FIFO_FULL)) | |
360 | break; | |
361 | ||
362 | num_words = 1; | |
363 | } | |
364 | ||
365 | spi_qup_write_to_fifo(controller, xfer, num_words); | |
366 | ||
367 | remainder -= num_words; | |
368 | ||
369 | /* if block mode, check to see if next block is available */ | |
370 | if (is_block_mode && !spi_qup_is_flag_set(controller, | |
371 | QUP_OP_OUT_BLOCK_WRITE_REQ)) | |
372 | break; | |
373 | ||
374 | } while (remainder); | |
375 | } | |
376 | ||
612762e8 AG |
377 | static int spi_qup_prep_sg(struct spi_master *master, struct spi_transfer *xfer, |
378 | enum dma_transfer_direction dir, | |
379 | dma_async_tx_callback callback) | |
380 | { | |
381 | struct spi_qup *qup = spi_master_get_devdata(master); | |
382 | unsigned long flags = DMA_PREP_INTERRUPT | DMA_PREP_FENCE; | |
383 | struct dma_async_tx_descriptor *desc; | |
384 | struct scatterlist *sgl; | |
385 | struct dma_chan *chan; | |
386 | dma_cookie_t cookie; | |
387 | unsigned int nents; | |
388 | ||
389 | if (dir == DMA_MEM_TO_DEV) { | |
390 | chan = master->dma_tx; | |
391 | nents = xfer->tx_sg.nents; | |
392 | sgl = xfer->tx_sg.sgl; | |
393 | } else { | |
394 | chan = master->dma_rx; | |
395 | nents = xfer->rx_sg.nents; | |
396 | sgl = xfer->rx_sg.sgl; | |
397 | } | |
398 | ||
399 | desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags); | |
d9a09a6c VN |
400 | if (IS_ERR_OR_NULL(desc)) |
401 | return desc ? PTR_ERR(desc) : -EINVAL; | |
612762e8 AG |
402 | |
403 | desc->callback = callback; | |
404 | desc->callback_param = qup; | |
405 | ||
406 | cookie = dmaengine_submit(desc); | |
407 | ||
408 | return dma_submit_error(cookie); | |
409 | } | |
410 | ||
411 | static void spi_qup_dma_terminate(struct spi_master *master, | |
412 | struct spi_transfer *xfer) | |
413 | { | |
414 | if (xfer->tx_buf) | |
415 | dmaengine_terminate_all(master->dma_tx); | |
416 | if (xfer->rx_buf) | |
417 | dmaengine_terminate_all(master->dma_rx); | |
418 | } | |
419 | ||
5f13fd60 VN |
420 | static int spi_qup_do_dma(struct spi_master *master, struct spi_transfer *xfer, |
421 | unsigned long timeout) | |
612762e8 | 422 | { |
5f13fd60 | 423 | struct spi_qup *qup = spi_master_get_devdata(master); |
612762e8 AG |
424 | dma_async_tx_callback rx_done = NULL, tx_done = NULL; |
425 | int ret; | |
426 | ||
427 | if (xfer->rx_buf) | |
428 | rx_done = spi_qup_dma_done; | |
429 | else if (xfer->tx_buf) | |
430 | tx_done = spi_qup_dma_done; | |
431 | ||
ce00bab3 VN |
432 | /* before issuing the descriptors, set the QUP to run */ |
433 | ret = spi_qup_set_state(qup, QUP_STATE_RUN); | |
434 | if (ret) { | |
435 | dev_warn(qup->dev, "%s(%d): cannot set RUN state\n", | |
436 | __func__, __LINE__); | |
437 | return ret; | |
438 | } | |
439 | ||
612762e8 AG |
440 | if (xfer->rx_buf) { |
441 | ret = spi_qup_prep_sg(master, xfer, DMA_DEV_TO_MEM, rx_done); | |
442 | if (ret) | |
443 | return ret; | |
444 | ||
445 | dma_async_issue_pending(master->dma_rx); | |
446 | } | |
447 | ||
448 | if (xfer->tx_buf) { | |
449 | ret = spi_qup_prep_sg(master, xfer, DMA_MEM_TO_DEV, tx_done); | |
450 | if (ret) | |
451 | return ret; | |
452 | ||
453 | dma_async_issue_pending(master->dma_tx); | |
454 | } | |
455 | ||
5f13fd60 VN |
456 | if (!wait_for_completion_timeout(&qup->done, timeout)) |
457 | return -ETIMEDOUT; | |
458 | ||
612762e8 AG |
459 | return 0; |
460 | } | |
461 | ||
5f13fd60 VN |
462 | static int spi_qup_do_pio(struct spi_master *master, struct spi_transfer *xfer, |
463 | unsigned long timeout) | |
612762e8 AG |
464 | { |
465 | struct spi_qup *qup = spi_master_get_devdata(master); | |
466 | int ret; | |
467 | ||
468 | ret = spi_qup_set_state(qup, QUP_STATE_RUN); | |
469 | if (ret) { | |
470 | dev_warn(qup->dev, "cannot set RUN state\n"); | |
471 | return ret; | |
472 | } | |
473 | ||
474 | ret = spi_qup_set_state(qup, QUP_STATE_PAUSE); | |
475 | if (ret) { | |
476 | dev_warn(qup->dev, "cannot set PAUSE state\n"); | |
477 | return ret; | |
478 | } | |
479 | ||
7538726f VN |
480 | if (qup->mode == QUP_IO_M_MODE_FIFO) |
481 | spi_qup_write(qup, xfer); | |
612762e8 | 482 | |
ce00bab3 VN |
483 | ret = spi_qup_set_state(qup, QUP_STATE_RUN); |
484 | if (ret) { | |
485 | dev_warn(qup->dev, "%s(%d): cannot set RUN state\n", | |
486 | __func__, __LINE__); | |
487 | return ret; | |
488 | } | |
489 | ||
5f13fd60 VN |
490 | if (!wait_for_completion_timeout(&qup->done, timeout)) |
491 | return -ETIMEDOUT; | |
492 | ||
612762e8 AG |
493 | return 0; |
494 | } | |
495 | ||
64ff247a II |
496 | static irqreturn_t spi_qup_qup_irq(int irq, void *dev_id) |
497 | { | |
498 | struct spi_qup *controller = dev_id; | |
ce7dfc71 | 499 | struct spi_transfer *xfer = controller->xfer; |
64ff247a | 500 | u32 opflags, qup_err, spi_err; |
64ff247a II |
501 | int error = 0; |
502 | ||
64ff247a II |
503 | qup_err = readl_relaxed(controller->base + QUP_ERROR_FLAGS); |
504 | spi_err = readl_relaxed(controller->base + SPI_ERROR_FLAGS); | |
505 | opflags = readl_relaxed(controller->base + QUP_OPERATIONAL); | |
506 | ||
507 | writel_relaxed(qup_err, controller->base + QUP_ERROR_FLAGS); | |
508 | writel_relaxed(spi_err, controller->base + SPI_ERROR_FLAGS); | |
64ff247a II |
509 | |
510 | if (qup_err) { | |
511 | if (qup_err & QUP_ERROR_OUTPUT_OVER_RUN) | |
512 | dev_warn(controller->dev, "OUTPUT_OVER_RUN\n"); | |
513 | if (qup_err & QUP_ERROR_INPUT_UNDER_RUN) | |
514 | dev_warn(controller->dev, "INPUT_UNDER_RUN\n"); | |
515 | if (qup_err & QUP_ERROR_OUTPUT_UNDER_RUN) | |
516 | dev_warn(controller->dev, "OUTPUT_UNDER_RUN\n"); | |
517 | if (qup_err & QUP_ERROR_INPUT_OVER_RUN) | |
518 | dev_warn(controller->dev, "INPUT_OVER_RUN\n"); | |
519 | ||
520 | error = -EIO; | |
521 | } | |
522 | ||
523 | if (spi_err) { | |
524 | if (spi_err & SPI_ERROR_CLK_OVER_RUN) | |
525 | dev_warn(controller->dev, "CLK_OVER_RUN\n"); | |
526 | if (spi_err & SPI_ERROR_CLK_UNDER_RUN) | |
527 | dev_warn(controller->dev, "CLK_UNDER_RUN\n"); | |
528 | ||
529 | error = -EIO; | |
530 | } | |
531 | ||
ce7dfc71 VN |
532 | if (spi_qup_is_dma_xfer(controller->mode)) { |
533 | writel_relaxed(opflags, controller->base + QUP_OPERATIONAL); | |
534 | } else { | |
612762e8 | 535 | if (opflags & QUP_OP_IN_SERVICE_FLAG) |
7538726f | 536 | spi_qup_read(controller, xfer); |
64ff247a | 537 | |
612762e8 | 538 | if (opflags & QUP_OP_OUT_SERVICE_FLAG) |
7538726f | 539 | spi_qup_write(controller, xfer); |
612762e8 | 540 | } |
64ff247a | 541 | |
ce7dfc71 | 542 | if ((opflags & QUP_OP_MAX_INPUT_DONE_FLAG) || error) |
64ff247a II |
543 | complete(&controller->done); |
544 | ||
545 | return IRQ_HANDLED; | |
546 | } | |
547 | ||
64ff247a | 548 | /* set clock freq ... bits per word */ |
00cce74d | 549 | static int spi_qup_io_config(struct spi_device *spi, struct spi_transfer *xfer) |
64ff247a | 550 | { |
00cce74d | 551 | struct spi_qup *controller = spi_master_get_devdata(spi->master); |
32ecab99 | 552 | u32 config, iomode, control; |
612762e8 | 553 | int ret, n_words; |
64ff247a | 554 | |
00cce74d | 555 | if (spi->mode & SPI_LOOP && xfer->len > controller->in_fifo_sz) { |
64ff247a II |
556 | dev_err(controller->dev, "too big size for loopback %d > %d\n", |
557 | xfer->len, controller->in_fifo_sz); | |
558 | return -EIO; | |
559 | } | |
560 | ||
561 | ret = clk_set_rate(controller->cclk, xfer->speed_hz); | |
562 | if (ret) { | |
563 | dev_err(controller->dev, "fail to set frequency %d", | |
564 | xfer->speed_hz); | |
565 | return -EIO; | |
566 | } | |
567 | ||
568 | if (spi_qup_set_state(controller, QUP_STATE_RESET)) { | |
569 | dev_err(controller->dev, "cannot set RESET state\n"); | |
570 | return -EIO; | |
571 | } | |
572 | ||
32ecab99 VN |
573 | controller->w_size = DIV_ROUND_UP(xfer->bits_per_word, 8); |
574 | controller->n_words = xfer->len / controller->w_size; | |
612762e8 | 575 | n_words = controller->n_words; |
64ff247a | 576 | |
32ecab99 VN |
577 | if (n_words <= (controller->in_fifo_sz / sizeof(u32))) { |
578 | ||
579 | controller->mode = QUP_IO_M_MODE_FIFO; | |
580 | ||
64ff247a II |
581 | writel_relaxed(n_words, controller->base + QUP_MX_READ_CNT); |
582 | writel_relaxed(n_words, controller->base + QUP_MX_WRITE_CNT); | |
583 | /* must be zero for FIFO */ | |
584 | writel_relaxed(0, controller->base + QUP_MX_INPUT_CNT); | |
585 | writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT); | |
32ecab99 VN |
586 | } else if (spi->master->can_dma && |
587 | spi->master->can_dma(spi->master, spi, xfer) && | |
588 | spi->master->cur_msg_mapped) { | |
589 | ||
590 | controller->mode = QUP_IO_M_MODE_BAM; | |
591 | ||
64ff247a II |
592 | writel_relaxed(n_words, controller->base + QUP_MX_INPUT_CNT); |
593 | writel_relaxed(n_words, controller->base + QUP_MX_OUTPUT_CNT); | |
594 | /* must be zero for BLOCK and BAM */ | |
595 | writel_relaxed(0, controller->base + QUP_MX_READ_CNT); | |
596 | writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT); | |
612762e8 AG |
597 | |
598 | if (!controller->qup_v1) { | |
599 | void __iomem *input_cnt; | |
600 | ||
601 | input_cnt = controller->base + QUP_MX_INPUT_CNT; | |
602 | /* | |
603 | * for DMA transfers, both QUP_MX_INPUT_CNT and | |
604 | * QUP_MX_OUTPUT_CNT must be zero to all cases but one. | |
605 | * That case is a non-balanced transfer when there is | |
606 | * only a rx_buf. | |
607 | */ | |
608 | if (xfer->tx_buf) | |
609 | writel_relaxed(0, input_cnt); | |
610 | else | |
611 | writel_relaxed(n_words, input_cnt); | |
612 | ||
613 | writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT); | |
614 | } | |
32ecab99 VN |
615 | } else { |
616 | ||
617 | controller->mode = QUP_IO_M_MODE_BLOCK; | |
618 | ||
619 | writel_relaxed(n_words, controller->base + QUP_MX_INPUT_CNT); | |
620 | writel_relaxed(n_words, controller->base + QUP_MX_OUTPUT_CNT); | |
621 | /* must be zero for BLOCK and BAM */ | |
622 | writel_relaxed(0, controller->base + QUP_MX_READ_CNT); | |
623 | writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT); | |
64ff247a II |
624 | } |
625 | ||
626 | iomode = readl_relaxed(controller->base + QUP_IO_M_MODES); | |
627 | /* Set input and output transfer mode */ | |
628 | iomode &= ~(QUP_IO_M_INPUT_MODE_MASK | QUP_IO_M_OUTPUT_MODE_MASK); | |
612762e8 | 629 | |
32ecab99 | 630 | if (!spi_qup_is_dma_xfer(controller->mode)) |
612762e8 AG |
631 | iomode &= ~(QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN); |
632 | else | |
633 | iomode |= QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN; | |
634 | ||
32ecab99 VN |
635 | iomode |= (controller->mode << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT); |
636 | iomode |= (controller->mode << QUP_IO_M_INPUT_MODE_MASK_SHIFT); | |
64ff247a II |
637 | |
638 | writel_relaxed(iomode, controller->base + QUP_IO_M_MODES); | |
639 | ||
0667dd5f II |
640 | control = readl_relaxed(controller->base + SPI_IO_CONTROL); |
641 | ||
642 | if (spi->mode & SPI_CPOL) | |
643 | control |= SPI_IO_C_CLK_IDLE_HIGH; | |
644 | else | |
645 | control &= ~SPI_IO_C_CLK_IDLE_HIGH; | |
646 | ||
647 | writel_relaxed(control, controller->base + SPI_IO_CONTROL); | |
648 | ||
64ff247a II |
649 | config = readl_relaxed(controller->base + SPI_CONFIG); |
650 | ||
00cce74d | 651 | if (spi->mode & SPI_LOOP) |
64ff247a II |
652 | config |= SPI_CONFIG_LOOPBACK; |
653 | else | |
654 | config &= ~SPI_CONFIG_LOOPBACK; | |
655 | ||
00cce74d | 656 | if (spi->mode & SPI_CPHA) |
64ff247a II |
657 | config &= ~SPI_CONFIG_INPUT_FIRST; |
658 | else | |
659 | config |= SPI_CONFIG_INPUT_FIRST; | |
660 | ||
661 | /* | |
662 | * HS_MODE improves signal stability for spi-clk high rates, | |
663 | * but is invalid in loop back mode. | |
664 | */ | |
00cce74d | 665 | if ((xfer->speed_hz >= SPI_HS_MIN_RATE) && !(spi->mode & SPI_LOOP)) |
64ff247a II |
666 | config |= SPI_CONFIG_HS_MODE; |
667 | else | |
668 | config &= ~SPI_CONFIG_HS_MODE; | |
669 | ||
670 | writel_relaxed(config, controller->base + SPI_CONFIG); | |
671 | ||
672 | config = readl_relaxed(controller->base + QUP_CONFIG); | |
673 | config &= ~(QUP_CONFIG_NO_INPUT | QUP_CONFIG_NO_OUTPUT | QUP_CONFIG_N); | |
674 | config |= xfer->bits_per_word - 1; | |
675 | config |= QUP_CONFIG_SPI_MODE; | |
612762e8 | 676 | |
32ecab99 | 677 | if (spi_qup_is_dma_xfer(controller->mode)) { |
612762e8 AG |
678 | if (!xfer->tx_buf) |
679 | config |= QUP_CONFIG_NO_OUTPUT; | |
680 | if (!xfer->rx_buf) | |
681 | config |= QUP_CONFIG_NO_INPUT; | |
682 | } | |
683 | ||
64ff247a II |
684 | writel_relaxed(config, controller->base + QUP_CONFIG); |
685 | ||
70cea0a9 | 686 | /* only write to OPERATIONAL_MASK when register is present */ |
612762e8 AG |
687 | if (!controller->qup_v1) { |
688 | u32 mask = 0; | |
689 | ||
690 | /* | |
691 | * mask INPUT and OUTPUT service flags to prevent IRQs on FIFO | |
692 | * status change in BAM mode | |
693 | */ | |
694 | ||
32ecab99 | 695 | if (spi_qup_is_dma_xfer(controller->mode)) |
612762e8 AG |
696 | mask = QUP_OP_IN_SERVICE_FLAG | QUP_OP_OUT_SERVICE_FLAG; |
697 | ||
698 | writel_relaxed(mask, controller->base + QUP_OPERATIONAL_MASK); | |
699 | } | |
700 | ||
64ff247a II |
701 | return 0; |
702 | } | |
703 | ||
64ff247a II |
704 | static int spi_qup_transfer_one(struct spi_master *master, |
705 | struct spi_device *spi, | |
706 | struct spi_transfer *xfer) | |
707 | { | |
708 | struct spi_qup *controller = spi_master_get_devdata(master); | |
64ff247a II |
709 | unsigned long timeout, flags; |
710 | int ret = -EIO; | |
711 | ||
00cce74d | 712 | ret = spi_qup_io_config(spi, xfer); |
64ff247a II |
713 | if (ret) |
714 | return ret; | |
715 | ||
716 | timeout = DIV_ROUND_UP(xfer->speed_hz, MSEC_PER_SEC); | |
717 | timeout = DIV_ROUND_UP(xfer->len * 8, timeout); | |
718 | timeout = 100 * msecs_to_jiffies(timeout); | |
719 | ||
720 | reinit_completion(&controller->done); | |
721 | ||
722 | spin_lock_irqsave(&controller->lock, flags); | |
723 | controller->xfer = xfer; | |
724 | controller->error = 0; | |
725 | controller->rx_bytes = 0; | |
726 | controller->tx_bytes = 0; | |
727 | spin_unlock_irqrestore(&controller->lock, flags); | |
728 | ||
32ecab99 | 729 | if (spi_qup_is_dma_xfer(controller->mode)) |
5f13fd60 | 730 | ret = spi_qup_do_dma(master, xfer, timeout); |
612762e8 | 731 | else |
5f13fd60 | 732 | ret = spi_qup_do_pio(master, xfer, timeout); |
64ff247a | 733 | |
612762e8 | 734 | if (ret) |
64ff247a | 735 | goto exit; |
64ff247a | 736 | |
64ff247a II |
737 | exit: |
738 | spi_qup_set_state(controller, QUP_STATE_RESET); | |
739 | spin_lock_irqsave(&controller->lock, flags); | |
64ff247a II |
740 | if (!ret) |
741 | ret = controller->error; | |
742 | spin_unlock_irqrestore(&controller->lock, flags); | |
612762e8 | 743 | |
32ecab99 | 744 | if (ret && spi_qup_is_dma_xfer(controller->mode)) |
612762e8 AG |
745 | spi_qup_dma_terminate(master, xfer); |
746 | ||
747 | return ret; | |
748 | } | |
749 | ||
750 | static bool spi_qup_can_dma(struct spi_master *master, struct spi_device *spi, | |
751 | struct spi_transfer *xfer) | |
752 | { | |
753 | struct spi_qup *qup = spi_master_get_devdata(master); | |
754 | size_t dma_align = dma_get_cache_alignment(); | |
32ecab99 | 755 | int n_words; |
612762e8 | 756 | |
32ecab99 VN |
757 | if (xfer->rx_buf) { |
758 | if (!IS_ALIGNED((size_t)xfer->rx_buf, dma_align) || | |
759 | IS_ERR_OR_NULL(master->dma_rx)) | |
760 | return false; | |
761 | if (qup->qup_v1 && (xfer->len % qup->in_blk_sz)) | |
762 | return false; | |
763 | } | |
612762e8 | 764 | |
32ecab99 VN |
765 | if (xfer->tx_buf) { |
766 | if (!IS_ALIGNED((size_t)xfer->tx_buf, dma_align) || | |
767 | IS_ERR_OR_NULL(master->dma_tx)) | |
768 | return false; | |
769 | if (qup->qup_v1 && (xfer->len % qup->out_blk_sz)) | |
770 | return false; | |
771 | } | |
612762e8 | 772 | |
32ecab99 VN |
773 | n_words = xfer->len / DIV_ROUND_UP(xfer->bits_per_word, 8); |
774 | if (n_words <= (qup->in_fifo_sz / sizeof(u32))) | |
612762e8 AG |
775 | return false; |
776 | ||
612762e8 AG |
777 | return true; |
778 | } | |
779 | ||
780 | static void spi_qup_release_dma(struct spi_master *master) | |
781 | { | |
782 | if (!IS_ERR_OR_NULL(master->dma_rx)) | |
783 | dma_release_channel(master->dma_rx); | |
784 | if (!IS_ERR_OR_NULL(master->dma_tx)) | |
785 | dma_release_channel(master->dma_tx); | |
786 | } | |
787 | ||
788 | static int spi_qup_init_dma(struct spi_master *master, resource_size_t base) | |
789 | { | |
790 | struct spi_qup *spi = spi_master_get_devdata(master); | |
791 | struct dma_slave_config *rx_conf = &spi->rx_conf, | |
792 | *tx_conf = &spi->tx_conf; | |
793 | struct device *dev = spi->dev; | |
794 | int ret; | |
795 | ||
796 | /* allocate dma resources, if available */ | |
797 | master->dma_rx = dma_request_slave_channel_reason(dev, "rx"); | |
798 | if (IS_ERR(master->dma_rx)) | |
799 | return PTR_ERR(master->dma_rx); | |
800 | ||
801 | master->dma_tx = dma_request_slave_channel_reason(dev, "tx"); | |
802 | if (IS_ERR(master->dma_tx)) { | |
803 | ret = PTR_ERR(master->dma_tx); | |
804 | goto err_tx; | |
805 | } | |
806 | ||
807 | /* set DMA parameters */ | |
808 | rx_conf->direction = DMA_DEV_TO_MEM; | |
809 | rx_conf->device_fc = 1; | |
810 | rx_conf->src_addr = base + QUP_INPUT_FIFO; | |
811 | rx_conf->src_maxburst = spi->in_blk_sz; | |
812 | ||
813 | tx_conf->direction = DMA_MEM_TO_DEV; | |
814 | tx_conf->device_fc = 1; | |
815 | tx_conf->dst_addr = base + QUP_OUTPUT_FIFO; | |
816 | tx_conf->dst_maxburst = spi->out_blk_sz; | |
817 | ||
818 | ret = dmaengine_slave_config(master->dma_rx, rx_conf); | |
819 | if (ret) { | |
820 | dev_err(dev, "failed to configure RX channel\n"); | |
821 | goto err; | |
822 | } | |
823 | ||
824 | ret = dmaengine_slave_config(master->dma_tx, tx_conf); | |
825 | if (ret) { | |
826 | dev_err(dev, "failed to configure TX channel\n"); | |
827 | goto err; | |
828 | } | |
829 | ||
830 | return 0; | |
831 | ||
832 | err: | |
833 | dma_release_channel(master->dma_tx); | |
834 | err_tx: | |
835 | dma_release_channel(master->dma_rx); | |
64ff247a II |
836 | return ret; |
837 | } | |
838 | ||
b702b9fb VN |
839 | static void spi_qup_set_cs(struct spi_device *spi, bool val) |
840 | { | |
841 | struct spi_qup *controller; | |
842 | u32 spi_ioc; | |
843 | u32 spi_ioc_orig; | |
844 | ||
845 | controller = spi_master_get_devdata(spi->master); | |
846 | spi_ioc = readl_relaxed(controller->base + SPI_IO_CONTROL); | |
847 | spi_ioc_orig = spi_ioc; | |
848 | if (!val) | |
849 | spi_ioc |= SPI_IO_C_FORCE_CS; | |
850 | else | |
851 | spi_ioc &= ~SPI_IO_C_FORCE_CS; | |
852 | ||
853 | if (spi_ioc != spi_ioc_orig) | |
854 | writel_relaxed(spi_ioc, controller->base + SPI_IO_CONTROL); | |
855 | } | |
856 | ||
64ff247a II |
857 | static int spi_qup_probe(struct platform_device *pdev) |
858 | { | |
859 | struct spi_master *master; | |
860 | struct clk *iclk, *cclk; | |
861 | struct spi_qup *controller; | |
862 | struct resource *res; | |
863 | struct device *dev; | |
864 | void __iomem *base; | |
12cb89e3 | 865 | u32 max_freq, iomode, num_cs; |
64ff247a II |
866 | int ret, irq, size; |
867 | ||
868 | dev = &pdev->dev; | |
869 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
870 | base = devm_ioremap_resource(dev, res); | |
871 | if (IS_ERR(base)) | |
872 | return PTR_ERR(base); | |
873 | ||
874 | irq = platform_get_irq(pdev, 0); | |
64ff247a II |
875 | if (irq < 0) |
876 | return irq; | |
877 | ||
878 | cclk = devm_clk_get(dev, "core"); | |
879 | if (IS_ERR(cclk)) | |
880 | return PTR_ERR(cclk); | |
881 | ||
882 | iclk = devm_clk_get(dev, "iface"); | |
883 | if (IS_ERR(iclk)) | |
884 | return PTR_ERR(iclk); | |
885 | ||
886 | /* This is optional parameter */ | |
887 | if (of_property_read_u32(dev->of_node, "spi-max-frequency", &max_freq)) | |
888 | max_freq = SPI_MAX_RATE; | |
889 | ||
890 | if (!max_freq || max_freq > SPI_MAX_RATE) { | |
891 | dev_err(dev, "invalid clock frequency %d\n", max_freq); | |
892 | return -ENXIO; | |
893 | } | |
894 | ||
895 | ret = clk_prepare_enable(cclk); | |
896 | if (ret) { | |
897 | dev_err(dev, "cannot enable core clock\n"); | |
898 | return ret; | |
899 | } | |
900 | ||
901 | ret = clk_prepare_enable(iclk); | |
902 | if (ret) { | |
903 | clk_disable_unprepare(cclk); | |
904 | dev_err(dev, "cannot enable iface clock\n"); | |
905 | return ret; | |
906 | } | |
907 | ||
64ff247a II |
908 | master = spi_alloc_master(dev, sizeof(struct spi_qup)); |
909 | if (!master) { | |
910 | clk_disable_unprepare(cclk); | |
911 | clk_disable_unprepare(iclk); | |
912 | dev_err(dev, "cannot allocate master\n"); | |
913 | return -ENOMEM; | |
914 | } | |
915 | ||
4a8573ab | 916 | /* use num-cs unless not present or out of range */ |
12cb89e3 II |
917 | if (of_property_read_u32(dev->of_node, "num-cs", &num_cs) || |
918 | num_cs > SPI_NUM_CHIPSELECTS) | |
4a8573ab | 919 | master->num_chipselect = SPI_NUM_CHIPSELECTS; |
12cb89e3 II |
920 | else |
921 | master->num_chipselect = num_cs; | |
4a8573ab | 922 | |
64ff247a II |
923 | master->bus_num = pdev->id; |
924 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP; | |
64ff247a | 925 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); |
cb64ca54 | 926 | master->max_speed_hz = max_freq; |
64ff247a II |
927 | master->transfer_one = spi_qup_transfer_one; |
928 | master->dev.of_node = pdev->dev.of_node; | |
929 | master->auto_runtime_pm = true; | |
612762e8 AG |
930 | master->dma_alignment = dma_get_cache_alignment(); |
931 | master->max_dma_len = SPI_MAX_DMA_XFER; | |
64ff247a II |
932 | |
933 | platform_set_drvdata(pdev, master); | |
934 | ||
935 | controller = spi_master_get_devdata(master); | |
936 | ||
937 | controller->dev = dev; | |
938 | controller->base = base; | |
939 | controller->iclk = iclk; | |
940 | controller->cclk = cclk; | |
941 | controller->irq = irq; | |
64ff247a | 942 | |
612762e8 AG |
943 | ret = spi_qup_init_dma(master, res->start); |
944 | if (ret == -EPROBE_DEFER) | |
945 | goto error; | |
946 | else if (!ret) | |
947 | master->can_dma = spi_qup_can_dma; | |
948 | ||
70cea0a9 AG |
949 | /* set v1 flag if device is version 1 */ |
950 | if (of_device_is_compatible(dev->of_node, "qcom,spi-qup-v1.1.1")) | |
951 | controller->qup_v1 = 1; | |
952 | ||
b702b9fb VN |
953 | if (!controller->qup_v1) |
954 | master->set_cs = spi_qup_set_cs; | |
955 | ||
64ff247a II |
956 | spin_lock_init(&controller->lock); |
957 | init_completion(&controller->done); | |
958 | ||
959 | iomode = readl_relaxed(base + QUP_IO_M_MODES); | |
960 | ||
961 | size = QUP_IO_M_OUTPUT_BLOCK_SIZE(iomode); | |
962 | if (size) | |
963 | controller->out_blk_sz = size * 16; | |
964 | else | |
965 | controller->out_blk_sz = 4; | |
966 | ||
967 | size = QUP_IO_M_INPUT_BLOCK_SIZE(iomode); | |
968 | if (size) | |
969 | controller->in_blk_sz = size * 16; | |
970 | else | |
971 | controller->in_blk_sz = 4; | |
972 | ||
973 | size = QUP_IO_M_OUTPUT_FIFO_SIZE(iomode); | |
974 | controller->out_fifo_sz = controller->out_blk_sz * (2 << size); | |
975 | ||
976 | size = QUP_IO_M_INPUT_FIFO_SIZE(iomode); | |
977 | controller->in_fifo_sz = controller->in_blk_sz * (2 << size); | |
978 | ||
70cea0a9 AG |
979 | dev_info(dev, "IN:block:%d, fifo:%d, OUT:block:%d, fifo:%d\n", |
980 | controller->in_blk_sz, controller->in_fifo_sz, | |
64ff247a II |
981 | controller->out_blk_sz, controller->out_fifo_sz); |
982 | ||
983 | writel_relaxed(1, base + QUP_SW_RESET); | |
984 | ||
985 | ret = spi_qup_set_state(controller, QUP_STATE_RESET); | |
986 | if (ret) { | |
987 | dev_err(dev, "cannot set RESET state\n"); | |
612762e8 | 988 | goto error_dma; |
64ff247a II |
989 | } |
990 | ||
991 | writel_relaxed(0, base + QUP_OPERATIONAL); | |
992 | writel_relaxed(0, base + QUP_IO_M_MODES); | |
70cea0a9 AG |
993 | |
994 | if (!controller->qup_v1) | |
995 | writel_relaxed(0, base + QUP_OPERATIONAL_MASK); | |
996 | ||
64ff247a II |
997 | writel_relaxed(SPI_ERROR_CLK_UNDER_RUN | SPI_ERROR_CLK_OVER_RUN, |
998 | base + SPI_ERROR_FLAGS_EN); | |
999 | ||
70cea0a9 AG |
1000 | /* if earlier version of the QUP, disable INPUT_OVERRUN */ |
1001 | if (controller->qup_v1) | |
1002 | writel_relaxed(QUP_ERROR_OUTPUT_OVER_RUN | | |
1003 | QUP_ERROR_INPUT_UNDER_RUN | QUP_ERROR_OUTPUT_UNDER_RUN, | |
1004 | base + QUP_ERROR_FLAGS_EN); | |
1005 | ||
64ff247a II |
1006 | writel_relaxed(0, base + SPI_CONFIG); |
1007 | writel_relaxed(SPI_IO_C_NO_TRI_STATE, base + SPI_IO_CONTROL); | |
1008 | ||
1009 | ret = devm_request_irq(dev, irq, spi_qup_qup_irq, | |
1010 | IRQF_TRIGGER_HIGH, pdev->name, controller); | |
1011 | if (ret) | |
612762e8 | 1012 | goto error_dma; |
64ff247a | 1013 | |
64ff247a II |
1014 | pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC); |
1015 | pm_runtime_use_autosuspend(dev); | |
1016 | pm_runtime_set_active(dev); | |
1017 | pm_runtime_enable(dev); | |
045c243a AG |
1018 | |
1019 | ret = devm_spi_register_master(dev, master); | |
1020 | if (ret) | |
1021 | goto disable_pm; | |
1022 | ||
64ff247a II |
1023 | return 0; |
1024 | ||
045c243a AG |
1025 | disable_pm: |
1026 | pm_runtime_disable(&pdev->dev); | |
612762e8 AG |
1027 | error_dma: |
1028 | spi_qup_release_dma(master); | |
64ff247a II |
1029 | error: |
1030 | clk_disable_unprepare(cclk); | |
1031 | clk_disable_unprepare(iclk); | |
1032 | spi_master_put(master); | |
1033 | return ret; | |
1034 | } | |
1035 | ||
ec833050 | 1036 | #ifdef CONFIG_PM |
64ff247a II |
1037 | static int spi_qup_pm_suspend_runtime(struct device *device) |
1038 | { | |
1039 | struct spi_master *master = dev_get_drvdata(device); | |
1040 | struct spi_qup *controller = spi_master_get_devdata(master); | |
1041 | u32 config; | |
1042 | ||
1043 | /* Enable clocks auto gaiting */ | |
1044 | config = readl(controller->base + QUP_CONFIG); | |
f0ceb114 | 1045 | config |= QUP_CONFIG_CLOCK_AUTO_GATE; |
64ff247a | 1046 | writel_relaxed(config, controller->base + QUP_CONFIG); |
dae1a770 PG |
1047 | |
1048 | clk_disable_unprepare(controller->cclk); | |
1049 | clk_disable_unprepare(controller->iclk); | |
1050 | ||
64ff247a II |
1051 | return 0; |
1052 | } | |
1053 | ||
1054 | static int spi_qup_pm_resume_runtime(struct device *device) | |
1055 | { | |
1056 | struct spi_master *master = dev_get_drvdata(device); | |
1057 | struct spi_qup *controller = spi_master_get_devdata(master); | |
1058 | u32 config; | |
dae1a770 PG |
1059 | int ret; |
1060 | ||
1061 | ret = clk_prepare_enable(controller->iclk); | |
1062 | if (ret) | |
1063 | return ret; | |
1064 | ||
1065 | ret = clk_prepare_enable(controller->cclk); | |
1066 | if (ret) | |
1067 | return ret; | |
64ff247a II |
1068 | |
1069 | /* Disable clocks auto gaiting */ | |
1070 | config = readl_relaxed(controller->base + QUP_CONFIG); | |
f0ceb114 | 1071 | config &= ~QUP_CONFIG_CLOCK_AUTO_GATE; |
64ff247a II |
1072 | writel_relaxed(config, controller->base + QUP_CONFIG); |
1073 | return 0; | |
1074 | } | |
ec833050 | 1075 | #endif /* CONFIG_PM */ |
64ff247a II |
1076 | |
1077 | #ifdef CONFIG_PM_SLEEP | |
1078 | static int spi_qup_suspend(struct device *device) | |
1079 | { | |
1080 | struct spi_master *master = dev_get_drvdata(device); | |
1081 | struct spi_qup *controller = spi_master_get_devdata(master); | |
1082 | int ret; | |
1083 | ||
1084 | ret = spi_master_suspend(master); | |
1085 | if (ret) | |
1086 | return ret; | |
1087 | ||
1088 | ret = spi_qup_set_state(controller, QUP_STATE_RESET); | |
1089 | if (ret) | |
1090 | return ret; | |
1091 | ||
9d04d8bc SH |
1092 | if (!pm_runtime_suspended(device)) { |
1093 | clk_disable_unprepare(controller->cclk); | |
1094 | clk_disable_unprepare(controller->iclk); | |
1095 | } | |
64ff247a II |
1096 | return 0; |
1097 | } | |
1098 | ||
1099 | static int spi_qup_resume(struct device *device) | |
1100 | { | |
1101 | struct spi_master *master = dev_get_drvdata(device); | |
1102 | struct spi_qup *controller = spi_master_get_devdata(master); | |
1103 | int ret; | |
1104 | ||
1105 | ret = clk_prepare_enable(controller->iclk); | |
1106 | if (ret) | |
1107 | return ret; | |
1108 | ||
1109 | ret = clk_prepare_enable(controller->cclk); | |
1110 | if (ret) | |
1111 | return ret; | |
1112 | ||
1113 | ret = spi_qup_set_state(controller, QUP_STATE_RESET); | |
1114 | if (ret) | |
1115 | return ret; | |
1116 | ||
1117 | return spi_master_resume(master); | |
1118 | } | |
1119 | #endif /* CONFIG_PM_SLEEP */ | |
1120 | ||
1121 | static int spi_qup_remove(struct platform_device *pdev) | |
1122 | { | |
1123 | struct spi_master *master = dev_get_drvdata(&pdev->dev); | |
1124 | struct spi_qup *controller = spi_master_get_devdata(master); | |
1125 | int ret; | |
1126 | ||
1127 | ret = pm_runtime_get_sync(&pdev->dev); | |
3d89e141 | 1128 | if (ret < 0) |
64ff247a II |
1129 | return ret; |
1130 | ||
1131 | ret = spi_qup_set_state(controller, QUP_STATE_RESET); | |
1132 | if (ret) | |
1133 | return ret; | |
1134 | ||
612762e8 AG |
1135 | spi_qup_release_dma(master); |
1136 | ||
64ff247a II |
1137 | clk_disable_unprepare(controller->cclk); |
1138 | clk_disable_unprepare(controller->iclk); | |
1139 | ||
1140 | pm_runtime_put_noidle(&pdev->dev); | |
1141 | pm_runtime_disable(&pdev->dev); | |
d2442287 | 1142 | |
64ff247a II |
1143 | return 0; |
1144 | } | |
1145 | ||
113b1a07 | 1146 | static const struct of_device_id spi_qup_dt_match[] = { |
70cea0a9 | 1147 | { .compatible = "qcom,spi-qup-v1.1.1", }, |
64ff247a II |
1148 | { .compatible = "qcom,spi-qup-v2.1.1", }, |
1149 | { .compatible = "qcom,spi-qup-v2.2.1", }, | |
1150 | { } | |
1151 | }; | |
1152 | MODULE_DEVICE_TABLE(of, spi_qup_dt_match); | |
1153 | ||
1154 | static const struct dev_pm_ops spi_qup_dev_pm_ops = { | |
1155 | SET_SYSTEM_SLEEP_PM_OPS(spi_qup_suspend, spi_qup_resume) | |
1156 | SET_RUNTIME_PM_OPS(spi_qup_pm_suspend_runtime, | |
1157 | spi_qup_pm_resume_runtime, | |
1158 | NULL) | |
1159 | }; | |
1160 | ||
1161 | static struct platform_driver spi_qup_driver = { | |
1162 | .driver = { | |
1163 | .name = "spi_qup", | |
64ff247a II |
1164 | .pm = &spi_qup_dev_pm_ops, |
1165 | .of_match_table = spi_qup_dt_match, | |
1166 | }, | |
1167 | .probe = spi_qup_probe, | |
1168 | .remove = spi_qup_remove, | |
1169 | }; | |
1170 | module_platform_driver(spi_qup_driver); | |
1171 | ||
1172 | MODULE_LICENSE("GPL v2"); | |
64ff247a | 1173 | MODULE_ALIAS("platform:spi_qup"); |