blob: 6c82d496b58e84c04236c0a219535bf2b9fd9c28 [file] [log] [blame]
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001/*
2 * PXA2xx SPI private DMA support.
3 *
4 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
Mika Westerbergcd7bed02013-01-22 12:26:28 +020021#include <linux/delay.h>
22#include <linux/device.h>
23#include <linux/dma-mapping.h>
24#include <linux/pxa2xx_ssp.h>
25#include <linux/spi/spi.h>
26#include <linux/spi/pxa2xx_spi.h>
27
28#include "spi-pxa2xx.h"
29
30#define DMA_INT_MASK (DCSR_ENDINTR | DCSR_STARTINTR | DCSR_BUSERR)
31#define RESET_DMA_CHANNEL (DCSR_NODESC | DMA_INT_MASK)
32
33bool pxa2xx_spi_dma_is_possible(size_t len)
34{
35 /* Try to map dma buffer and do a dma transfer if successful, but
36 * only if the length is non-zero and less than MAX_DMA_LEN.
37 *
38 * Zero-length non-descriptor DMA is illegal on PXA2xx; force use
39 * of PIO instead. Care is needed above because the transfer may
40 * have have been passed with buffers that are already dma mapped.
41 * A zero-length transfer in PIO mode will not try to write/read
42 * to/from the buffers
43 *
44 * REVISIT large transfers are exactly where we most want to be
45 * using DMA. If this happens much, split those transfers into
46 * multiple DMA segments rather than forcing PIO.
47 */
48 return len > 0 && len <= MAX_DMA_LEN;
49}
50
51int pxa2xx_spi_map_dma_buffers(struct driver_data *drv_data)
52{
53 struct spi_message *msg = drv_data->cur_msg;
54 struct device *dev = &msg->spi->dev;
55
56 if (!drv_data->cur_chip->enable_dma)
57 return 0;
58
59 if (msg->is_dma_mapped)
60 return drv_data->rx_dma && drv_data->tx_dma;
61
62 if (!IS_DMA_ALIGNED(drv_data->rx) || !IS_DMA_ALIGNED(drv_data->tx))
63 return 0;
64
65 /* Modify setup if rx buffer is null */
66 if (drv_data->rx == NULL) {
67 *drv_data->null_dma_buf = 0;
68 drv_data->rx = drv_data->null_dma_buf;
69 drv_data->rx_map_len = 4;
70 } else
71 drv_data->rx_map_len = drv_data->len;
72
73
74 /* Modify setup if tx buffer is null */
75 if (drv_data->tx == NULL) {
76 *drv_data->null_dma_buf = 0;
77 drv_data->tx = drv_data->null_dma_buf;
78 drv_data->tx_map_len = 4;
79 } else
80 drv_data->tx_map_len = drv_data->len;
81
82 /* Stream map the tx buffer. Always do DMA_TO_DEVICE first
83 * so we flush the cache *before* invalidating it, in case
84 * the tx and rx buffers overlap.
85 */
86 drv_data->tx_dma = dma_map_single(dev, drv_data->tx,
87 drv_data->tx_map_len, DMA_TO_DEVICE);
88 if (dma_mapping_error(dev, drv_data->tx_dma))
89 return 0;
90
91 /* Stream map the rx buffer */
92 drv_data->rx_dma = dma_map_single(dev, drv_data->rx,
93 drv_data->rx_map_len, DMA_FROM_DEVICE);
94 if (dma_mapping_error(dev, drv_data->rx_dma)) {
95 dma_unmap_single(dev, drv_data->tx_dma,
96 drv_data->tx_map_len, DMA_TO_DEVICE);
97 return 0;
98 }
99
100 return 1;
101}
102
103static void pxa2xx_spi_unmap_dma_buffers(struct driver_data *drv_data)
104{
105 struct device *dev;
106
107 if (!drv_data->dma_mapped)
108 return;
109
110 if (!drv_data->cur_msg->is_dma_mapped) {
111 dev = &drv_data->cur_msg->spi->dev;
112 dma_unmap_single(dev, drv_data->rx_dma,
113 drv_data->rx_map_len, DMA_FROM_DEVICE);
114 dma_unmap_single(dev, drv_data->tx_dma,
115 drv_data->tx_map_len, DMA_TO_DEVICE);
116 }
117
118 drv_data->dma_mapped = 0;
119}
120
Jarkko Nikula8e8dd9f2014-12-18 15:04:22 +0200121static int wait_ssp_rx_stall(struct driver_data *drv_data)
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200122{
123 unsigned long limit = loops_per_jiffy << 1;
124
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200125 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit)
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200126 cpu_relax();
127
128 return limit;
129}
130
131static int wait_dma_channel_stop(int channel)
132{
133 unsigned long limit = loops_per_jiffy << 1;
134
135 while (!(DCSR(channel) & DCSR_STOPSTATE) && --limit)
136 cpu_relax();
137
138 return limit;
139}
140
141static void pxa2xx_spi_dma_error_stop(struct driver_data *drv_data,
142 const char *msg)
143{
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200144 /* Stop and reset */
145 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
146 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
147 write_SSSR_CS(drv_data, drv_data->clear_sr);
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200148 pxa2xx_spi_write(drv_data, SSCR1,
149 pxa2xx_spi_read(drv_data, SSCR1)
150 & ~drv_data->dma_cr1);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200151 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200152 pxa2xx_spi_write(drv_data, SSTO, 0);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200153 pxa2xx_spi_flush(drv_data);
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200154 pxa2xx_spi_write(drv_data, SSCR0,
155 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200156
157 pxa2xx_spi_unmap_dma_buffers(drv_data);
158
159 dev_err(&drv_data->pdev->dev, "%s\n", msg);
160
161 drv_data->cur_msg->state = ERROR_STATE;
162 tasklet_schedule(&drv_data->pump_transfers);
163}
164
165static void pxa2xx_spi_dma_transfer_complete(struct driver_data *drv_data)
166{
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200167 struct spi_message *msg = drv_data->cur_msg;
168
169 /* Clear and disable interrupts on SSP and DMA channels*/
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200170 pxa2xx_spi_write(drv_data, SSCR1,
171 pxa2xx_spi_read(drv_data, SSCR1)
172 & ~drv_data->dma_cr1);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200173 write_SSSR_CS(drv_data, drv_data->clear_sr);
174 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
175 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
176
177 if (wait_dma_channel_stop(drv_data->rx_channel) == 0)
178 dev_err(&drv_data->pdev->dev,
179 "dma_handler: dma rx channel stop failed\n");
180
181 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
182 dev_err(&drv_data->pdev->dev,
183 "dma_transfer: ssp rx stall failed\n");
184
185 pxa2xx_spi_unmap_dma_buffers(drv_data);
186
187 /* update the buffer pointer for the amount completed in dma */
188 drv_data->rx += drv_data->len -
189 (DCMD(drv_data->rx_channel) & DCMD_LENGTH);
190
191 /* read trailing data from fifo, it does not matter how many
192 * bytes are in the fifo just read until buffer is full
193 * or fifo is empty, which ever occurs first */
194 drv_data->read(drv_data);
195
196 /* return count of what was actually read */
197 msg->actual_length += drv_data->len -
198 (drv_data->rx_end - drv_data->rx);
199
200 /* Transfer delays and chip select release are
201 * handled in pump_transfers or giveback
202 */
203
204 /* Move to next transfer */
205 msg->state = pxa2xx_spi_next_transfer(drv_data);
206
207 /* Schedule transfer tasklet */
208 tasklet_schedule(&drv_data->pump_transfers);
209}
210
211void pxa2xx_spi_dma_handler(int channel, void *data)
212{
213 struct driver_data *drv_data = data;
214 u32 irq_status = DCSR(channel) & DMA_INT_MASK;
215
216 if (irq_status & DCSR_BUSERR) {
217
218 if (channel == drv_data->tx_channel)
219 pxa2xx_spi_dma_error_stop(drv_data,
220 "dma_handler: bad bus address on tx channel");
221 else
222 pxa2xx_spi_dma_error_stop(drv_data,
223 "dma_handler: bad bus address on rx channel");
224 return;
225 }
226
227 /* PXA255x_SSP has no timeout interrupt, wait for tailing bytes */
228 if ((channel == drv_data->tx_channel)
229 && (irq_status & DCSR_ENDINTR)
230 && (drv_data->ssp_type == PXA25x_SSP)) {
231
232 /* Wait for rx to stall */
Jarkko Nikula8e8dd9f2014-12-18 15:04:22 +0200233 if (wait_ssp_rx_stall(drv_data) == 0)
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200234 dev_err(&drv_data->pdev->dev,
235 "dma_handler: ssp rx stall failed\n");
236
237 /* finish this transfer, start the next */
238 pxa2xx_spi_dma_transfer_complete(drv_data);
239 }
240}
241
242irqreturn_t pxa2xx_spi_dma_transfer(struct driver_data *drv_data)
243{
244 u32 irq_status;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200245
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200246 irq_status = pxa2xx_spi_read(drv_data, SSSR) & drv_data->mask_sr;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200247 if (irq_status & SSSR_ROR) {
248 pxa2xx_spi_dma_error_stop(drv_data,
249 "dma_transfer: fifo overrun");
250 return IRQ_HANDLED;
251 }
252
253 /* Check for false positive timeout */
254 if ((irq_status & SSSR_TINT)
255 && (DCSR(drv_data->tx_channel) & DCSR_RUN)) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200256 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200257 return IRQ_HANDLED;
258 }
259
260 if (irq_status & SSSR_TINT || drv_data->rx == drv_data->rx_end) {
261
262 /* Clear and disable timeout interrupt, do the rest in
263 * dma_transfer_complete */
264 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200265 pxa2xx_spi_write(drv_data, SSTO, 0);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200266
267 /* finish this transfer, start the next */
268 pxa2xx_spi_dma_transfer_complete(drv_data);
269
270 return IRQ_HANDLED;
271 }
272
273 /* Opps problem detected */
274 return IRQ_NONE;
275}
276
277int pxa2xx_spi_dma_prepare(struct driver_data *drv_data, u32 dma_burst)
278{
279 u32 dma_width;
280
281 switch (drv_data->n_bytes) {
282 case 1:
283 dma_width = DCMD_WIDTH1;
284 break;
285 case 2:
286 dma_width = DCMD_WIDTH2;
287 break;
288 default:
289 dma_width = DCMD_WIDTH4;
290 break;
291 }
292
293 /* Setup rx DMA Channel */
294 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
295 DSADR(drv_data->rx_channel) = drv_data->ssdr_physical;
296 DTADR(drv_data->rx_channel) = drv_data->rx_dma;
297 if (drv_data->rx == drv_data->null_dma_buf)
298 /* No target address increment */
299 DCMD(drv_data->rx_channel) = DCMD_FLOWSRC
300 | dma_width
301 | dma_burst
302 | drv_data->len;
303 else
304 DCMD(drv_data->rx_channel) = DCMD_INCTRGADDR
305 | DCMD_FLOWSRC
306 | dma_width
307 | dma_burst
308 | drv_data->len;
309
310 /* Setup tx DMA Channel */
311 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
312 DSADR(drv_data->tx_channel) = drv_data->tx_dma;
313 DTADR(drv_data->tx_channel) = drv_data->ssdr_physical;
314 if (drv_data->tx == drv_data->null_dma_buf)
315 /* No source address increment */
316 DCMD(drv_data->tx_channel) = DCMD_FLOWTRG
317 | dma_width
318 | dma_burst
319 | drv_data->len;
320 else
321 DCMD(drv_data->tx_channel) = DCMD_INCSRCADDR
322 | DCMD_FLOWTRG
323 | dma_width
324 | dma_burst
325 | drv_data->len;
326
327 /* Enable dma end irqs on SSP to detect end of transfer */
328 if (drv_data->ssp_type == PXA25x_SSP)
329 DCMD(drv_data->tx_channel) |= DCMD_ENDIRQEN;
330
331 return 0;
332}
333
334void pxa2xx_spi_dma_start(struct driver_data *drv_data)
335{
336 DCSR(drv_data->rx_channel) |= DCSR_RUN;
337 DCSR(drv_data->tx_channel) |= DCSR_RUN;
338}
339
340int pxa2xx_spi_dma_setup(struct driver_data *drv_data)
341{
342 struct device *dev = &drv_data->pdev->dev;
343 struct ssp_device *ssp = drv_data->ssp;
344
345 /* Get two DMA channels (rx and tx) */
346 drv_data->rx_channel = pxa_request_dma("pxa2xx_spi_ssp_rx",
347 DMA_PRIO_HIGH,
348 pxa2xx_spi_dma_handler,
349 drv_data);
350 if (drv_data->rx_channel < 0) {
351 dev_err(dev, "problem (%d) requesting rx channel\n",
352 drv_data->rx_channel);
353 return -ENODEV;
354 }
355 drv_data->tx_channel = pxa_request_dma("pxa2xx_spi_ssp_tx",
356 DMA_PRIO_MEDIUM,
357 pxa2xx_spi_dma_handler,
358 drv_data);
359 if (drv_data->tx_channel < 0) {
360 dev_err(dev, "problem (%d) requesting tx channel\n",
361 drv_data->tx_channel);
362 pxa_free_dma(drv_data->rx_channel);
363 return -ENODEV;
364 }
365
366 DRCMR(ssp->drcmr_rx) = DRCMR_MAPVLD | drv_data->rx_channel;
367 DRCMR(ssp->drcmr_tx) = DRCMR_MAPVLD | drv_data->tx_channel;
368
369 return 0;
370}
371
372void pxa2xx_spi_dma_release(struct driver_data *drv_data)
373{
374 struct ssp_device *ssp = drv_data->ssp;
375
376 DRCMR(ssp->drcmr_rx) = 0;
377 DRCMR(ssp->drcmr_tx) = 0;
378
379 if (drv_data->tx_channel != 0)
380 pxa_free_dma(drv_data->tx_channel);
381 if (drv_data->rx_channel != 0)
382 pxa_free_dma(drv_data->rx_channel);
383}
384
385void pxa2xx_spi_dma_resume(struct driver_data *drv_data)
386{
387 if (drv_data->rx_channel != -1)
388 DRCMR(drv_data->ssp->drcmr_rx) =
389 DRCMR_MAPVLD | drv_data->rx_channel;
390 if (drv_data->tx_channel != -1)
391 DRCMR(drv_data->ssp->drcmr_tx) =
392 DRCMR_MAPVLD | drv_data->tx_channel;
393}
394
395int pxa2xx_spi_set_dma_burst_and_threshold(struct chip_data *chip,
396 struct spi_device *spi,
397 u8 bits_per_word, u32 *burst_code,
398 u32 *threshold)
399{
400 struct pxa2xx_spi_chip *chip_info =
401 (struct pxa2xx_spi_chip *)spi->controller_data;
402 int bytes_per_word;
403 int burst_bytes;
404 int thresh_words;
405 int req_burst_size;
406 int retval = 0;
407
408 /* Set the threshold (in registers) to equal the same amount of data
409 * as represented by burst size (in bytes). The computation below
410 * is (burst_size rounded up to nearest 8 byte, word or long word)
411 * divided by (bytes/register); the tx threshold is the inverse of
412 * the rx, so that there will always be enough data in the rx fifo
413 * to satisfy a burst, and there will always be enough space in the
414 * tx fifo to accept a burst (a tx burst will overwrite the fifo if
415 * there is not enough space), there must always remain enough empty
416 * space in the rx fifo for any data loaded to the tx fifo.
417 * Whenever burst_size (in bytes) equals bits/word, the fifo threshold
418 * will be 8, or half the fifo;
419 * The threshold can only be set to 2, 4 or 8, but not 16, because
420 * to burst 16 to the tx fifo, the fifo would have to be empty;
421 * however, the minimum fifo trigger level is 1, and the tx will
422 * request service when the fifo is at this level, with only 15 spaces.
423 */
424
425 /* find bytes/word */
426 if (bits_per_word <= 8)
427 bytes_per_word = 1;
428 else if (bits_per_word <= 16)
429 bytes_per_word = 2;
430 else
431 bytes_per_word = 4;
432
433 /* use struct pxa2xx_spi_chip->dma_burst_size if available */
434 if (chip_info)
435 req_burst_size = chip_info->dma_burst_size;
436 else {
437 switch (chip->dma_burst_size) {
438 default:
439 /* if the default burst size is not set,
440 * do it now */
441 chip->dma_burst_size = DCMD_BURST8;
442 case DCMD_BURST8:
443 req_burst_size = 8;
444 break;
445 case DCMD_BURST16:
446 req_burst_size = 16;
447 break;
448 case DCMD_BURST32:
449 req_burst_size = 32;
450 break;
451 }
452 }
453 if (req_burst_size <= 8) {
454 *burst_code = DCMD_BURST8;
455 burst_bytes = 8;
456 } else if (req_burst_size <= 16) {
457 if (bytes_per_word == 1) {
458 /* don't burst more than 1/2 the fifo */
459 *burst_code = DCMD_BURST8;
460 burst_bytes = 8;
461 retval = 1;
462 } else {
463 *burst_code = DCMD_BURST16;
464 burst_bytes = 16;
465 }
466 } else {
467 if (bytes_per_word == 1) {
468 /* don't burst more than 1/2 the fifo */
469 *burst_code = DCMD_BURST8;
470 burst_bytes = 8;
471 retval = 1;
472 } else if (bytes_per_word == 2) {
473 /* don't burst more than 1/2 the fifo */
474 *burst_code = DCMD_BURST16;
475 burst_bytes = 16;
476 retval = 1;
477 } else {
478 *burst_code = DCMD_BURST32;
479 burst_bytes = 32;
480 }
481 }
482
483 thresh_words = burst_bytes / bytes_per_word;
484
485 /* thresh_words will be between 2 and 8 */
486 *threshold = (SSCR1_RxTresh(thresh_words) & SSCR1_RFT)
487 | (SSCR1_TxTresh(16-thresh_words) & SSCR1_TFT);
488
489 return retval;
490}