diff options
Diffstat (limited to 'drivers/spi/spi-atmel.c')
-rw-r--r-- | drivers/spi/spi-atmel.c | 728 |
1 files changed, 660 insertions, 68 deletions
diff --git a/drivers/spi/spi-atmel.c b/drivers/spi/spi-atmel.c index 656d137db253..787bd2c22bca 100644 --- a/drivers/spi/spi-atmel.c +++ b/drivers/spi/spi-atmel.c | |||
@@ -15,16 +15,17 @@ | |||
15 | #include <linux/platform_device.h> | 15 | #include <linux/platform_device.h> |
16 | #include <linux/delay.h> | 16 | #include <linux/delay.h> |
17 | #include <linux/dma-mapping.h> | 17 | #include <linux/dma-mapping.h> |
18 | #include <linux/dmaengine.h> | ||
18 | #include <linux/err.h> | 19 | #include <linux/err.h> |
19 | #include <linux/interrupt.h> | 20 | #include <linux/interrupt.h> |
20 | #include <linux/spi/spi.h> | 21 | #include <linux/spi/spi.h> |
21 | #include <linux/slab.h> | 22 | #include <linux/slab.h> |
22 | #include <linux/platform_data/atmel.h> | 23 | #include <linux/platform_data/atmel.h> |
24 | #include <linux/platform_data/dma-atmel.h> | ||
23 | #include <linux/of.h> | 25 | #include <linux/of.h> |
24 | 26 | ||
25 | #include <asm/io.h> | 27 | #include <linux/io.h> |
26 | #include <asm/gpio.h> | 28 | #include <linux/gpio.h> |
27 | #include <mach/cpu.h> | ||
28 | 29 | ||
29 | /* SPI register offsets */ | 30 | /* SPI register offsets */ |
30 | #define SPI_CR 0x0000 | 31 | #define SPI_CR 0x0000 |
@@ -39,6 +40,7 @@ | |||
39 | #define SPI_CSR1 0x0034 | 40 | #define SPI_CSR1 0x0034 |
40 | #define SPI_CSR2 0x0038 | 41 | #define SPI_CSR2 0x0038 |
41 | #define SPI_CSR3 0x003c | 42 | #define SPI_CSR3 0x003c |
43 | #define SPI_VERSION 0x00fc | ||
42 | #define SPI_RPR 0x0100 | 44 | #define SPI_RPR 0x0100 |
43 | #define SPI_RCR 0x0104 | 45 | #define SPI_RCR 0x0104 |
44 | #define SPI_TPR 0x0108 | 46 | #define SPI_TPR 0x0108 |
@@ -71,6 +73,8 @@ | |||
71 | #define SPI_FDIV_SIZE 1 | 73 | #define SPI_FDIV_SIZE 1 |
72 | #define SPI_MODFDIS_OFFSET 4 | 74 | #define SPI_MODFDIS_OFFSET 4 |
73 | #define SPI_MODFDIS_SIZE 1 | 75 | #define SPI_MODFDIS_SIZE 1 |
76 | #define SPI_WDRBT_OFFSET 5 | ||
77 | #define SPI_WDRBT_SIZE 1 | ||
74 | #define SPI_LLB_OFFSET 7 | 78 | #define SPI_LLB_OFFSET 7 |
75 | #define SPI_LLB_SIZE 1 | 79 | #define SPI_LLB_SIZE 1 |
76 | #define SPI_PCS_OFFSET 16 | 80 | #define SPI_PCS_OFFSET 16 |
@@ -180,6 +184,27 @@ | |||
180 | #define spi_writel(port,reg,value) \ | 184 | #define spi_writel(port,reg,value) \ |
181 | __raw_writel((value), (port)->regs + SPI_##reg) | 185 | __raw_writel((value), (port)->regs + SPI_##reg) |
182 | 186 | ||
187 | /* use PIO for small transfers, avoiding DMA setup/teardown overhead and | ||
188 | * cache operations; better heuristics consider wordsize and bitrate. | ||
189 | */ | ||
190 | #define DMA_MIN_BYTES 16 | ||
191 | |||
192 | struct atmel_spi_dma { | ||
193 | struct dma_chan *chan_rx; | ||
194 | struct dma_chan *chan_tx; | ||
195 | struct scatterlist sgrx; | ||
196 | struct scatterlist sgtx; | ||
197 | struct dma_async_tx_descriptor *data_desc_rx; | ||
198 | struct dma_async_tx_descriptor *data_desc_tx; | ||
199 | |||
200 | struct at_dma_slave dma_slave; | ||
201 | }; | ||
202 | |||
203 | struct atmel_spi_caps { | ||
204 | bool is_spi2; | ||
205 | bool has_wdrbt; | ||
206 | bool has_dma_support; | ||
207 | }; | ||
183 | 208 | ||
184 | /* | 209 | /* |
185 | * The core SPI transfer engine just talks to a register bank to set up | 210 | * The core SPI transfer engine just talks to a register bank to set up |
@@ -188,7 +213,9 @@ | |||
188 | */ | 213 | */ |
189 | struct atmel_spi { | 214 | struct atmel_spi { |
190 | spinlock_t lock; | 215 | spinlock_t lock; |
216 | unsigned long flags; | ||
191 | 217 | ||
218 | phys_addr_t phybase; | ||
192 | void __iomem *regs; | 219 | void __iomem *regs; |
193 | int irq; | 220 | int irq; |
194 | struct clk *clk; | 221 | struct clk *clk; |
@@ -197,13 +224,23 @@ struct atmel_spi { | |||
197 | 224 | ||
198 | u8 stopping; | 225 | u8 stopping; |
199 | struct list_head queue; | 226 | struct list_head queue; |
227 | struct tasklet_struct tasklet; | ||
200 | struct spi_transfer *current_transfer; | 228 | struct spi_transfer *current_transfer; |
201 | unsigned long current_remaining_bytes; | 229 | unsigned long current_remaining_bytes; |
202 | struct spi_transfer *next_transfer; | 230 | struct spi_transfer *next_transfer; |
203 | unsigned long next_remaining_bytes; | 231 | unsigned long next_remaining_bytes; |
232 | int done_status; | ||
204 | 233 | ||
234 | /* scratch buffer */ | ||
205 | void *buffer; | 235 | void *buffer; |
206 | dma_addr_t buffer_dma; | 236 | dma_addr_t buffer_dma; |
237 | |||
238 | struct atmel_spi_caps caps; | ||
239 | |||
240 | bool use_dma; | ||
241 | bool use_pdc; | ||
242 | /* dmaengine data */ | ||
243 | struct atmel_spi_dma dma; | ||
207 | }; | 244 | }; |
208 | 245 | ||
209 | /* Controller-specific per-slave state */ | 246 | /* Controller-specific per-slave state */ |
@@ -222,14 +259,10 @@ struct atmel_spi_device { | |||
222 | * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs) | 259 | * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs) |
223 | * - SPI_CSRx.CSAAT | 260 | * - SPI_CSRx.CSAAT |
224 | * - SPI_CSRx.SBCR allows faster clocking | 261 | * - SPI_CSRx.SBCR allows faster clocking |
225 | * | ||
226 | * We can determine the controller version by reading the VERSION | ||
227 | * register, but I haven't checked that it exists on all chips, and | ||
228 | * this is cheaper anyway. | ||
229 | */ | 262 | */ |
230 | static bool atmel_spi_is_v2(void) | 263 | static bool atmel_spi_is_v2(struct atmel_spi *as) |
231 | { | 264 | { |
232 | return !cpu_is_at91rm9200(); | 265 | return as->caps.is_spi2; |
233 | } | 266 | } |
234 | 267 | ||
235 | /* | 268 | /* |
@@ -250,11 +283,6 @@ static bool atmel_spi_is_v2(void) | |||
250 | * Master on Chip Select 0.") No workaround exists for that ... so for | 283 | * Master on Chip Select 0.") No workaround exists for that ... so for |
251 | * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH, | 284 | * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH, |
252 | * and (c) will trigger that first erratum in some cases. | 285 | * and (c) will trigger that first erratum in some cases. |
253 | * | ||
254 | * TODO: Test if the atmel_spi_is_v2() branch below works on | ||
255 | * AT91RM9200 if we use some other register than CSR0. However, don't | ||
256 | * do this unconditionally since AP7000 has an errata where the BITS | ||
257 | * field in CSR0 overrides all other CSRs. | ||
258 | */ | 286 | */ |
259 | 287 | ||
260 | static void cs_activate(struct atmel_spi *as, struct spi_device *spi) | 288 | static void cs_activate(struct atmel_spi *as, struct spi_device *spi) |
@@ -263,15 +291,25 @@ static void cs_activate(struct atmel_spi *as, struct spi_device *spi) | |||
263 | unsigned active = spi->mode & SPI_CS_HIGH; | 291 | unsigned active = spi->mode & SPI_CS_HIGH; |
264 | u32 mr; | 292 | u32 mr; |
265 | 293 | ||
266 | if (atmel_spi_is_v2()) { | 294 | if (atmel_spi_is_v2(as)) { |
267 | /* | 295 | spi_writel(as, CSR0 + 4 * spi->chip_select, asd->csr); |
268 | * Always use CSR0. This ensures that the clock | 296 | /* For the low SPI version, there is a issue that PDC transfer |
269 | * switches to the correct idle polarity before we | 297 | * on CS1,2,3 needs SPI_CSR0.BITS config as SPI_CSR1,2,3.BITS |
270 | * toggle the CS. | ||
271 | */ | 298 | */ |
272 | spi_writel(as, CSR0, asd->csr); | 299 | spi_writel(as, CSR0, asd->csr); |
273 | spi_writel(as, MR, SPI_BF(PCS, 0x0e) | SPI_BIT(MODFDIS) | 300 | if (as->caps.has_wdrbt) { |
274 | | SPI_BIT(MSTR)); | 301 | spi_writel(as, MR, |
302 | SPI_BF(PCS, ~(0x01 << spi->chip_select)) | ||
303 | | SPI_BIT(WDRBT) | ||
304 | | SPI_BIT(MODFDIS) | ||
305 | | SPI_BIT(MSTR)); | ||
306 | } else { | ||
307 | spi_writel(as, MR, | ||
308 | SPI_BF(PCS, ~(0x01 << spi->chip_select)) | ||
309 | | SPI_BIT(MODFDIS) | ||
310 | | SPI_BIT(MSTR)); | ||
311 | } | ||
312 | |||
275 | mr = spi_readl(as, MR); | 313 | mr = spi_readl(as, MR); |
276 | gpio_set_value(asd->npcs_pin, active); | 314 | gpio_set_value(asd->npcs_pin, active); |
277 | } else { | 315 | } else { |
@@ -318,10 +356,26 @@ static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi) | |||
318 | asd->npcs_pin, active ? " (low)" : "", | 356 | asd->npcs_pin, active ? " (low)" : "", |
319 | mr); | 357 | mr); |
320 | 358 | ||
321 | if (atmel_spi_is_v2() || spi->chip_select != 0) | 359 | if (atmel_spi_is_v2(as) || spi->chip_select != 0) |
322 | gpio_set_value(asd->npcs_pin, !active); | 360 | gpio_set_value(asd->npcs_pin, !active); |
323 | } | 361 | } |
324 | 362 | ||
363 | static void atmel_spi_lock(struct atmel_spi *as) | ||
364 | { | ||
365 | spin_lock_irqsave(&as->lock, as->flags); | ||
366 | } | ||
367 | |||
368 | static void atmel_spi_unlock(struct atmel_spi *as) | ||
369 | { | ||
370 | spin_unlock_irqrestore(&as->lock, as->flags); | ||
371 | } | ||
372 | |||
373 | static inline bool atmel_spi_use_dma(struct atmel_spi *as, | ||
374 | struct spi_transfer *xfer) | ||
375 | { | ||
376 | return as->use_dma && xfer->len >= DMA_MIN_BYTES; | ||
377 | } | ||
378 | |||
325 | static inline int atmel_spi_xfer_is_last(struct spi_message *msg, | 379 | static inline int atmel_spi_xfer_is_last(struct spi_message *msg, |
326 | struct spi_transfer *xfer) | 380 | struct spi_transfer *xfer) |
327 | { | 381 | { |
@@ -333,6 +387,265 @@ static inline int atmel_spi_xfer_can_be_chained(struct spi_transfer *xfer) | |||
333 | return xfer->delay_usecs == 0 && !xfer->cs_change; | 387 | return xfer->delay_usecs == 0 && !xfer->cs_change; |
334 | } | 388 | } |
335 | 389 | ||
390 | static int atmel_spi_dma_slave_config(struct atmel_spi *as, | ||
391 | struct dma_slave_config *slave_config, | ||
392 | u8 bits_per_word) | ||
393 | { | ||
394 | int err = 0; | ||
395 | |||
396 | if (bits_per_word > 8) { | ||
397 | slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; | ||
398 | slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; | ||
399 | } else { | ||
400 | slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; | ||
401 | slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; | ||
402 | } | ||
403 | |||
404 | slave_config->dst_addr = (dma_addr_t)as->phybase + SPI_TDR; | ||
405 | slave_config->src_addr = (dma_addr_t)as->phybase + SPI_RDR; | ||
406 | slave_config->src_maxburst = 1; | ||
407 | slave_config->dst_maxburst = 1; | ||
408 | slave_config->device_fc = false; | ||
409 | |||
410 | slave_config->direction = DMA_MEM_TO_DEV; | ||
411 | if (dmaengine_slave_config(as->dma.chan_tx, slave_config)) { | ||
412 | dev_err(&as->pdev->dev, | ||
413 | "failed to configure tx dma channel\n"); | ||
414 | err = -EINVAL; | ||
415 | } | ||
416 | |||
417 | slave_config->direction = DMA_DEV_TO_MEM; | ||
418 | if (dmaengine_slave_config(as->dma.chan_rx, slave_config)) { | ||
419 | dev_err(&as->pdev->dev, | ||
420 | "failed to configure rx dma channel\n"); | ||
421 | err = -EINVAL; | ||
422 | } | ||
423 | |||
424 | return err; | ||
425 | } | ||
426 | |||
427 | static bool filter(struct dma_chan *chan, void *slave) | ||
428 | { | ||
429 | struct at_dma_slave *sl = slave; | ||
430 | |||
431 | if (sl->dma_dev == chan->device->dev) { | ||
432 | chan->private = sl; | ||
433 | return true; | ||
434 | } else { | ||
435 | return false; | ||
436 | } | ||
437 | } | ||
438 | |||
439 | static int atmel_spi_configure_dma(struct atmel_spi *as) | ||
440 | { | ||
441 | struct at_dma_slave *sdata = &as->dma.dma_slave; | ||
442 | struct dma_slave_config slave_config; | ||
443 | int err; | ||
444 | |||
445 | if (sdata && sdata->dma_dev) { | ||
446 | dma_cap_mask_t mask; | ||
447 | |||
448 | /* Try to grab two DMA channels */ | ||
449 | dma_cap_zero(mask); | ||
450 | dma_cap_set(DMA_SLAVE, mask); | ||
451 | as->dma.chan_tx = dma_request_channel(mask, filter, sdata); | ||
452 | if (as->dma.chan_tx) | ||
453 | as->dma.chan_rx = | ||
454 | dma_request_channel(mask, filter, sdata); | ||
455 | } | ||
456 | if (!as->dma.chan_rx || !as->dma.chan_tx) { | ||
457 | dev_err(&as->pdev->dev, | ||
458 | "DMA channel not available, SPI unable to use DMA\n"); | ||
459 | err = -EBUSY; | ||
460 | goto error; | ||
461 | } | ||
462 | |||
463 | err = atmel_spi_dma_slave_config(as, &slave_config, 8); | ||
464 | if (err) | ||
465 | goto error; | ||
466 | |||
467 | dev_info(&as->pdev->dev, | ||
468 | "Using %s (tx) and %s (rx) for DMA transfers\n", | ||
469 | dma_chan_name(as->dma.chan_tx), | ||
470 | dma_chan_name(as->dma.chan_rx)); | ||
471 | return 0; | ||
472 | error: | ||
473 | if (as->dma.chan_rx) | ||
474 | dma_release_channel(as->dma.chan_rx); | ||
475 | if (as->dma.chan_tx) | ||
476 | dma_release_channel(as->dma.chan_tx); | ||
477 | return err; | ||
478 | } | ||
479 | |||
480 | static void atmel_spi_stop_dma(struct atmel_spi *as) | ||
481 | { | ||
482 | if (as->dma.chan_rx) | ||
483 | as->dma.chan_rx->device->device_control(as->dma.chan_rx, | ||
484 | DMA_TERMINATE_ALL, 0); | ||
485 | if (as->dma.chan_tx) | ||
486 | as->dma.chan_tx->device->device_control(as->dma.chan_tx, | ||
487 | DMA_TERMINATE_ALL, 0); | ||
488 | } | ||
489 | |||
490 | static void atmel_spi_release_dma(struct atmel_spi *as) | ||
491 | { | ||
492 | if (as->dma.chan_rx) | ||
493 | dma_release_channel(as->dma.chan_rx); | ||
494 | if (as->dma.chan_tx) | ||
495 | dma_release_channel(as->dma.chan_tx); | ||
496 | } | ||
497 | |||
498 | /* This function is called by the DMA driver from tasklet context */ | ||
499 | static void dma_callback(void *data) | ||
500 | { | ||
501 | struct spi_master *master = data; | ||
502 | struct atmel_spi *as = spi_master_get_devdata(master); | ||
503 | |||
504 | /* trigger SPI tasklet */ | ||
505 | tasklet_schedule(&as->tasklet); | ||
506 | } | ||
507 | |||
508 | /* | ||
509 | * Next transfer using PIO. | ||
510 | * lock is held, spi tasklet is blocked | ||
511 | */ | ||
512 | static void atmel_spi_next_xfer_pio(struct spi_master *master, | ||
513 | struct spi_transfer *xfer) | ||
514 | { | ||
515 | struct atmel_spi *as = spi_master_get_devdata(master); | ||
516 | |||
517 | dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_pio\n"); | ||
518 | |||
519 | as->current_remaining_bytes = xfer->len; | ||
520 | |||
521 | /* Make sure data is not remaining in RDR */ | ||
522 | spi_readl(as, RDR); | ||
523 | while (spi_readl(as, SR) & SPI_BIT(RDRF)) { | ||
524 | spi_readl(as, RDR); | ||
525 | cpu_relax(); | ||
526 | } | ||
527 | |||
528 | if (xfer->tx_buf) | ||
529 | spi_writel(as, TDR, *(u8 *)(xfer->tx_buf)); | ||
530 | else | ||
531 | spi_writel(as, TDR, 0); | ||
532 | |||
533 | dev_dbg(master->dev.parent, | ||
534 | " start pio xfer %p: len %u tx %p rx %p\n", | ||
535 | xfer, xfer->len, xfer->tx_buf, xfer->rx_buf); | ||
536 | |||
537 | /* Enable relevant interrupts */ | ||
538 | spi_writel(as, IER, SPI_BIT(RDRF) | SPI_BIT(OVRES)); | ||
539 | } | ||
540 | |||
541 | /* | ||
542 | * Submit next transfer for DMA. | ||
543 | * lock is held, spi tasklet is blocked | ||
544 | */ | ||
545 | static int atmel_spi_next_xfer_dma_submit(struct spi_master *master, | ||
546 | struct spi_transfer *xfer, | ||
547 | u32 *plen) | ||
548 | { | ||
549 | struct atmel_spi *as = spi_master_get_devdata(master); | ||
550 | struct dma_chan *rxchan = as->dma.chan_rx; | ||
551 | struct dma_chan *txchan = as->dma.chan_tx; | ||
552 | struct dma_async_tx_descriptor *rxdesc; | ||
553 | struct dma_async_tx_descriptor *txdesc; | ||
554 | struct dma_slave_config slave_config; | ||
555 | dma_cookie_t cookie; | ||
556 | u32 len = *plen; | ||
557 | |||
558 | dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_dma_submit\n"); | ||
559 | |||
560 | /* Check that the channels are available */ | ||
561 | if (!rxchan || !txchan) | ||
562 | return -ENODEV; | ||
563 | |||
564 | /* release lock for DMA operations */ | ||
565 | atmel_spi_unlock(as); | ||
566 | |||
567 | /* prepare the RX dma transfer */ | ||
568 | sg_init_table(&as->dma.sgrx, 1); | ||
569 | if (xfer->rx_buf) { | ||
570 | as->dma.sgrx.dma_address = xfer->rx_dma + xfer->len - *plen; | ||
571 | } else { | ||
572 | as->dma.sgrx.dma_address = as->buffer_dma; | ||
573 | if (len > BUFFER_SIZE) | ||
574 | len = BUFFER_SIZE; | ||
575 | } | ||
576 | |||
577 | /* prepare the TX dma transfer */ | ||
578 | sg_init_table(&as->dma.sgtx, 1); | ||
579 | if (xfer->tx_buf) { | ||
580 | as->dma.sgtx.dma_address = xfer->tx_dma + xfer->len - *plen; | ||
581 | } else { | ||
582 | as->dma.sgtx.dma_address = as->buffer_dma; | ||
583 | if (len > BUFFER_SIZE) | ||
584 | len = BUFFER_SIZE; | ||
585 | memset(as->buffer, 0, len); | ||
586 | } | ||
587 | |||
588 | sg_dma_len(&as->dma.sgtx) = len; | ||
589 | sg_dma_len(&as->dma.sgrx) = len; | ||
590 | |||
591 | *plen = len; | ||
592 | |||
593 | if (atmel_spi_dma_slave_config(as, &slave_config, 8)) | ||
594 | goto err_exit; | ||
595 | |||
596 | /* Send both scatterlists */ | ||
597 | rxdesc = rxchan->device->device_prep_slave_sg(rxchan, | ||
598 | &as->dma.sgrx, | ||
599 | 1, | ||
600 | DMA_FROM_DEVICE, | ||
601 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK, | ||
602 | NULL); | ||
603 | if (!rxdesc) | ||
604 | goto err_dma; | ||
605 | |||
606 | txdesc = txchan->device->device_prep_slave_sg(txchan, | ||
607 | &as->dma.sgtx, | ||
608 | 1, | ||
609 | DMA_TO_DEVICE, | ||
610 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK, | ||
611 | NULL); | ||
612 | if (!txdesc) | ||
613 | goto err_dma; | ||
614 | |||
615 | dev_dbg(master->dev.parent, | ||
616 | " start dma xfer %p: len %u tx %p/%08x rx %p/%08x\n", | ||
617 | xfer, xfer->len, xfer->tx_buf, xfer->tx_dma, | ||
618 | xfer->rx_buf, xfer->rx_dma); | ||
619 | |||
620 | /* Enable relevant interrupts */ | ||
621 | spi_writel(as, IER, SPI_BIT(OVRES)); | ||
622 | |||
623 | /* Put the callback on the RX transfer only, that should finish last */ | ||
624 | rxdesc->callback = dma_callback; | ||
625 | rxdesc->callback_param = master; | ||
626 | |||
627 | /* Submit and fire RX and TX with TX last so we're ready to read! */ | ||
628 | cookie = rxdesc->tx_submit(rxdesc); | ||
629 | if (dma_submit_error(cookie)) | ||
630 | goto err_dma; | ||
631 | cookie = txdesc->tx_submit(txdesc); | ||
632 | if (dma_submit_error(cookie)) | ||
633 | goto err_dma; | ||
634 | rxchan->device->device_issue_pending(rxchan); | ||
635 | txchan->device->device_issue_pending(txchan); | ||
636 | |||
637 | /* take back lock */ | ||
638 | atmel_spi_lock(as); | ||
639 | return 0; | ||
640 | |||
641 | err_dma: | ||
642 | spi_writel(as, IDR, SPI_BIT(OVRES)); | ||
643 | atmel_spi_stop_dma(as); | ||
644 | err_exit: | ||
645 | atmel_spi_lock(as); | ||
646 | return -ENOMEM; | ||
647 | } | ||
648 | |||
336 | static void atmel_spi_next_xfer_data(struct spi_master *master, | 649 | static void atmel_spi_next_xfer_data(struct spi_master *master, |
337 | struct spi_transfer *xfer, | 650 | struct spi_transfer *xfer, |
338 | dma_addr_t *tx_dma, | 651 | dma_addr_t *tx_dma, |
@@ -350,6 +663,7 @@ static void atmel_spi_next_xfer_data(struct spi_master *master, | |||
350 | if (len > BUFFER_SIZE) | 663 | if (len > BUFFER_SIZE) |
351 | len = BUFFER_SIZE; | 664 | len = BUFFER_SIZE; |
352 | } | 665 | } |
666 | |||
353 | if (xfer->tx_buf) | 667 | if (xfer->tx_buf) |
354 | *tx_dma = xfer->tx_dma + xfer->len - *plen; | 668 | *tx_dma = xfer->tx_dma + xfer->len - *plen; |
355 | else { | 669 | else { |
@@ -365,10 +679,10 @@ static void atmel_spi_next_xfer_data(struct spi_master *master, | |||
365 | } | 679 | } |
366 | 680 | ||
367 | /* | 681 | /* |
368 | * Submit next transfer for DMA. | 682 | * Submit next transfer for PDC. |
369 | * lock is held, spi irq is blocked | 683 | * lock is held, spi irq is blocked |
370 | */ | 684 | */ |
371 | static void atmel_spi_next_xfer(struct spi_master *master, | 685 | static void atmel_spi_pdc_next_xfer(struct spi_master *master, |
372 | struct spi_message *msg) | 686 | struct spi_message *msg) |
373 | { | 687 | { |
374 | struct atmel_spi *as = spi_master_get_devdata(master); | 688 | struct atmel_spi *as = spi_master_get_devdata(master); |
@@ -465,6 +779,48 @@ static void atmel_spi_next_xfer(struct spi_master *master, | |||
465 | spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN)); | 779 | spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN)); |
466 | } | 780 | } |
467 | 781 | ||
782 | /* | ||
783 | * Choose way to submit next transfer and start it. | ||
784 | * lock is held, spi tasklet is blocked | ||
785 | */ | ||
786 | static void atmel_spi_dma_next_xfer(struct spi_master *master, | ||
787 | struct spi_message *msg) | ||
788 | { | ||
789 | struct atmel_spi *as = spi_master_get_devdata(master); | ||
790 | struct spi_transfer *xfer; | ||
791 | u32 remaining, len; | ||
792 | |||
793 | remaining = as->current_remaining_bytes; | ||
794 | if (remaining) { | ||
795 | xfer = as->current_transfer; | ||
796 | len = remaining; | ||
797 | } else { | ||
798 | if (!as->current_transfer) | ||
799 | xfer = list_entry(msg->transfers.next, | ||
800 | struct spi_transfer, transfer_list); | ||
801 | else | ||
802 | xfer = list_entry( | ||
803 | as->current_transfer->transfer_list.next, | ||
804 | struct spi_transfer, transfer_list); | ||
805 | |||
806 | as->current_transfer = xfer; | ||
807 | len = xfer->len; | ||
808 | } | ||
809 | |||
810 | if (atmel_spi_use_dma(as, xfer)) { | ||
811 | u32 total = len; | ||
812 | if (!atmel_spi_next_xfer_dma_submit(master, xfer, &len)) { | ||
813 | as->current_remaining_bytes = total - len; | ||
814 | return; | ||
815 | } else { | ||
816 | dev_err(&msg->spi->dev, "unable to use DMA, fallback to PIO\n"); | ||
817 | } | ||
818 | } | ||
819 | |||
820 | /* use PIO if error appened using DMA */ | ||
821 | atmel_spi_next_xfer_pio(master, xfer); | ||
822 | } | ||
823 | |||
468 | static void atmel_spi_next_message(struct spi_master *master) | 824 | static void atmel_spi_next_message(struct spi_master *master) |
469 | { | 825 | { |
470 | struct atmel_spi *as = spi_master_get_devdata(master); | 826 | struct atmel_spi *as = spi_master_get_devdata(master); |
@@ -489,7 +845,10 @@ static void atmel_spi_next_message(struct spi_master *master) | |||
489 | } else | 845 | } else |
490 | cs_activate(as, spi); | 846 | cs_activate(as, spi); |
491 | 847 | ||
492 | atmel_spi_next_xfer(master, msg); | 848 | if (as->use_pdc) |
849 | atmel_spi_pdc_next_xfer(master, msg); | ||
850 | else | ||
851 | atmel_spi_dma_next_xfer(master, msg); | ||
493 | } | 852 | } |
494 | 853 | ||
495 | /* | 854 | /* |
@@ -542,38 +901,213 @@ static void atmel_spi_dma_unmap_xfer(struct spi_master *master, | |||
542 | xfer->len, DMA_FROM_DEVICE); | 901 | xfer->len, DMA_FROM_DEVICE); |
543 | } | 902 | } |
544 | 903 | ||
904 | static void atmel_spi_disable_pdc_transfer(struct atmel_spi *as) | ||
905 | { | ||
906 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); | ||
907 | } | ||
908 | |||
545 | static void | 909 | static void |
546 | atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as, | 910 | atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as, |
547 | struct spi_message *msg, int status, int stay) | 911 | struct spi_message *msg, int stay) |
548 | { | 912 | { |
549 | if (!stay || status < 0) | 913 | if (!stay || as->done_status < 0) |
550 | cs_deactivate(as, msg->spi); | 914 | cs_deactivate(as, msg->spi); |
551 | else | 915 | else |
552 | as->stay = msg->spi; | 916 | as->stay = msg->spi; |
553 | 917 | ||
554 | list_del(&msg->queue); | 918 | list_del(&msg->queue); |
555 | msg->status = status; | 919 | msg->status = as->done_status; |
556 | 920 | ||
557 | dev_dbg(master->dev.parent, | 921 | dev_dbg(master->dev.parent, |
558 | "xfer complete: %u bytes transferred\n", | 922 | "xfer complete: %u bytes transferred\n", |
559 | msg->actual_length); | 923 | msg->actual_length); |
560 | 924 | ||
561 | spin_unlock(&as->lock); | 925 | atmel_spi_unlock(as); |
562 | msg->complete(msg->context); | 926 | msg->complete(msg->context); |
563 | spin_lock(&as->lock); | 927 | atmel_spi_lock(as); |
564 | 928 | ||
565 | as->current_transfer = NULL; | 929 | as->current_transfer = NULL; |
566 | as->next_transfer = NULL; | 930 | as->next_transfer = NULL; |
931 | as->done_status = 0; | ||
567 | 932 | ||
568 | /* continue if needed */ | 933 | /* continue if needed */ |
569 | if (list_empty(&as->queue) || as->stopping) | 934 | if (list_empty(&as->queue) || as->stopping) { |
570 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); | 935 | if (as->use_pdc) |
571 | else | 936 | atmel_spi_disable_pdc_transfer(as); |
937 | } else { | ||
572 | atmel_spi_next_message(master); | 938 | atmel_spi_next_message(master); |
939 | } | ||
940 | } | ||
941 | |||
942 | /* Called from IRQ | ||
943 | * lock is held | ||
944 | * | ||
945 | * Must update "current_remaining_bytes" to keep track of data | ||
946 | * to transfer. | ||
947 | */ | ||
948 | static void | ||
949 | atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer) | ||
950 | { | ||
951 | u8 *txp; | ||
952 | u8 *rxp; | ||
953 | unsigned long xfer_pos = xfer->len - as->current_remaining_bytes; | ||
954 | |||
955 | if (xfer->rx_buf) { | ||
956 | rxp = ((u8 *)xfer->rx_buf) + xfer_pos; | ||
957 | *rxp = spi_readl(as, RDR); | ||
958 | } else { | ||
959 | spi_readl(as, RDR); | ||
960 | } | ||
961 | |||
962 | as->current_remaining_bytes--; | ||
963 | |||
964 | if (as->current_remaining_bytes) { | ||
965 | if (xfer->tx_buf) { | ||
966 | txp = ((u8 *)xfer->tx_buf) + xfer_pos + 1; | ||
967 | spi_writel(as, TDR, *txp); | ||
968 | } else { | ||
969 | spi_writel(as, TDR, 0); | ||
970 | } | ||
971 | } | ||
972 | } | ||
973 | |||
974 | /* Tasklet | ||
975 | * Called from DMA callback + pio transfer and overrun IRQ. | ||
976 | */ | ||
977 | static void atmel_spi_tasklet_func(unsigned long data) | ||
978 | { | ||
979 | struct spi_master *master = (struct spi_master *)data; | ||
980 | struct atmel_spi *as = spi_master_get_devdata(master); | ||
981 | struct spi_message *msg; | ||
982 | struct spi_transfer *xfer; | ||
983 | |||
984 | dev_vdbg(master->dev.parent, "atmel_spi_tasklet_func\n"); | ||
985 | |||
986 | atmel_spi_lock(as); | ||
987 | |||
988 | xfer = as->current_transfer; | ||
989 | |||
990 | if (xfer == NULL) | ||
991 | /* already been there */ | ||
992 | goto tasklet_out; | ||
993 | |||
994 | msg = list_entry(as->queue.next, struct spi_message, queue); | ||
995 | |||
996 | if (as->current_remaining_bytes == 0) { | ||
997 | if (as->done_status < 0) { | ||
998 | /* error happened (overrun) */ | ||
999 | if (atmel_spi_use_dma(as, xfer)) | ||
1000 | atmel_spi_stop_dma(as); | ||
1001 | } else { | ||
1002 | /* only update length if no error */ | ||
1003 | msg->actual_length += xfer->len; | ||
1004 | } | ||
1005 | |||
1006 | if (atmel_spi_use_dma(as, xfer)) | ||
1007 | if (!msg->is_dma_mapped) | ||
1008 | atmel_spi_dma_unmap_xfer(master, xfer); | ||
1009 | |||
1010 | if (xfer->delay_usecs) | ||
1011 | udelay(xfer->delay_usecs); | ||
1012 | |||
1013 | if (atmel_spi_xfer_is_last(msg, xfer) || as->done_status < 0) { | ||
1014 | /* report completed (or erroneous) message */ | ||
1015 | atmel_spi_msg_done(master, as, msg, xfer->cs_change); | ||
1016 | } else { | ||
1017 | if (xfer->cs_change) { | ||
1018 | cs_deactivate(as, msg->spi); | ||
1019 | udelay(1); | ||
1020 | cs_activate(as, msg->spi); | ||
1021 | } | ||
1022 | |||
1023 | /* | ||
1024 | * Not done yet. Submit the next transfer. | ||
1025 | * | ||
1026 | * FIXME handle protocol options for xfer | ||
1027 | */ | ||
1028 | atmel_spi_dma_next_xfer(master, msg); | ||
1029 | } | ||
1030 | } else { | ||
1031 | /* | ||
1032 | * Keep going, we still have data to send in | ||
1033 | * the current transfer. | ||
1034 | */ | ||
1035 | atmel_spi_dma_next_xfer(master, msg); | ||
1036 | } | ||
1037 | |||
1038 | tasklet_out: | ||
1039 | atmel_spi_unlock(as); | ||
573 | } | 1040 | } |
574 | 1041 | ||
1042 | /* Interrupt | ||
1043 | * | ||
1044 | * No need for locking in this Interrupt handler: done_status is the | ||
1045 | * only information modified. What we need is the update of this field | ||
1046 | * before tasklet runs. This is ensured by using barrier. | ||
1047 | */ | ||
575 | static irqreturn_t | 1048 | static irqreturn_t |
576 | atmel_spi_interrupt(int irq, void *dev_id) | 1049 | atmel_spi_pio_interrupt(int irq, void *dev_id) |
1050 | { | ||
1051 | struct spi_master *master = dev_id; | ||
1052 | struct atmel_spi *as = spi_master_get_devdata(master); | ||
1053 | u32 status, pending, imr; | ||
1054 | struct spi_transfer *xfer; | ||
1055 | int ret = IRQ_NONE; | ||
1056 | |||
1057 | imr = spi_readl(as, IMR); | ||
1058 | status = spi_readl(as, SR); | ||
1059 | pending = status & imr; | ||
1060 | |||
1061 | if (pending & SPI_BIT(OVRES)) { | ||
1062 | ret = IRQ_HANDLED; | ||
1063 | spi_writel(as, IDR, SPI_BIT(OVRES)); | ||
1064 | dev_warn(master->dev.parent, "overrun\n"); | ||
1065 | |||
1066 | /* | ||
1067 | * When we get an overrun, we disregard the current | ||
1068 | * transfer. Data will not be copied back from any | ||
1069 | * bounce buffer and msg->actual_len will not be | ||
1070 | * updated with the last xfer. | ||
1071 | * | ||
1072 | * We will also not process any remaning transfers in | ||
1073 | * the message. | ||
1074 | * | ||
1075 | * All actions are done in tasklet with done_status indication | ||
1076 | */ | ||
1077 | as->done_status = -EIO; | ||
1078 | smp_wmb(); | ||
1079 | |||
1080 | /* Clear any overrun happening while cleaning up */ | ||
1081 | spi_readl(as, SR); | ||
1082 | |||
1083 | tasklet_schedule(&as->tasklet); | ||
1084 | |||
1085 | } else if (pending & SPI_BIT(RDRF)) { | ||
1086 | atmel_spi_lock(as); | ||
1087 | |||
1088 | if (as->current_remaining_bytes) { | ||
1089 | ret = IRQ_HANDLED; | ||
1090 | xfer = as->current_transfer; | ||
1091 | atmel_spi_pump_pio_data(as, xfer); | ||
1092 | if (!as->current_remaining_bytes) { | ||
1093 | /* no more data to xfer, kick tasklet */ | ||
1094 | spi_writel(as, IDR, pending); | ||
1095 | tasklet_schedule(&as->tasklet); | ||
1096 | } | ||
1097 | } | ||
1098 | |||
1099 | atmel_spi_unlock(as); | ||
1100 | } else { | ||
1101 | WARN_ONCE(pending, "IRQ not handled, pending = %x\n", pending); | ||
1102 | ret = IRQ_HANDLED; | ||
1103 | spi_writel(as, IDR, pending); | ||
1104 | } | ||
1105 | |||
1106 | return ret; | ||
1107 | } | ||
1108 | |||
1109 | static irqreturn_t | ||
1110 | atmel_spi_pdc_interrupt(int irq, void *dev_id) | ||
577 | { | 1111 | { |
578 | struct spi_master *master = dev_id; | 1112 | struct spi_master *master = dev_id; |
579 | struct atmel_spi *as = spi_master_get_devdata(master); | 1113 | struct atmel_spi *as = spi_master_get_devdata(master); |
@@ -582,7 +1116,7 @@ atmel_spi_interrupt(int irq, void *dev_id) | |||
582 | u32 status, pending, imr; | 1116 | u32 status, pending, imr; |
583 | int ret = IRQ_NONE; | 1117 | int ret = IRQ_NONE; |
584 | 1118 | ||
585 | spin_lock(&as->lock); | 1119 | atmel_spi_lock(as); |
586 | 1120 | ||
587 | xfer = as->current_transfer; | 1121 | xfer = as->current_transfer; |
588 | msg = list_entry(as->queue.next, struct spi_message, queue); | 1122 | msg = list_entry(as->queue.next, struct spi_message, queue); |
@@ -641,7 +1175,8 @@ atmel_spi_interrupt(int irq, void *dev_id) | |||
641 | /* Clear any overrun happening while cleaning up */ | 1175 | /* Clear any overrun happening while cleaning up */ |
642 | spi_readl(as, SR); | 1176 | spi_readl(as, SR); |
643 | 1177 | ||
644 | atmel_spi_msg_done(master, as, msg, -EIO, 0); | 1178 | as->done_status = -EIO; |
1179 | atmel_spi_msg_done(master, as, msg, 0); | ||
645 | } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) { | 1180 | } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) { |
646 | ret = IRQ_HANDLED; | 1181 | ret = IRQ_HANDLED; |
647 | 1182 | ||
@@ -659,7 +1194,7 @@ atmel_spi_interrupt(int irq, void *dev_id) | |||
659 | 1194 | ||
660 | if (atmel_spi_xfer_is_last(msg, xfer)) { | 1195 | if (atmel_spi_xfer_is_last(msg, xfer)) { |
661 | /* report completed message */ | 1196 | /* report completed message */ |
662 | atmel_spi_msg_done(master, as, msg, 0, | 1197 | atmel_spi_msg_done(master, as, msg, |
663 | xfer->cs_change); | 1198 | xfer->cs_change); |
664 | } else { | 1199 | } else { |
665 | if (xfer->cs_change) { | 1200 | if (xfer->cs_change) { |
@@ -673,18 +1208,18 @@ atmel_spi_interrupt(int irq, void *dev_id) | |||
673 | * | 1208 | * |
674 | * FIXME handle protocol options for xfer | 1209 | * FIXME handle protocol options for xfer |
675 | */ | 1210 | */ |
676 | atmel_spi_next_xfer(master, msg); | 1211 | atmel_spi_pdc_next_xfer(master, msg); |
677 | } | 1212 | } |
678 | } else { | 1213 | } else { |
679 | /* | 1214 | /* |
680 | * Keep going, we still have data to send in | 1215 | * Keep going, we still have data to send in |
681 | * the current transfer. | 1216 | * the current transfer. |
682 | */ | 1217 | */ |
683 | atmel_spi_next_xfer(master, msg); | 1218 | atmel_spi_pdc_next_xfer(master, msg); |
684 | } | 1219 | } |
685 | } | 1220 | } |
686 | 1221 | ||
687 | spin_unlock(&as->lock); | 1222 | atmel_spi_unlock(as); |
688 | 1223 | ||
689 | return ret; | 1224 | return ret; |
690 | } | 1225 | } |
@@ -719,7 +1254,7 @@ static int atmel_spi_setup(struct spi_device *spi) | |||
719 | } | 1254 | } |
720 | 1255 | ||
721 | /* see notes above re chipselect */ | 1256 | /* see notes above re chipselect */ |
722 | if (!atmel_spi_is_v2() | 1257 | if (!atmel_spi_is_v2(as) |
723 | && spi->chip_select == 0 | 1258 | && spi->chip_select == 0 |
724 | && (spi->mode & SPI_CS_HIGH)) { | 1259 | && (spi->mode & SPI_CS_HIGH)) { |
725 | dev_dbg(&spi->dev, "setup: can't be active-high\n"); | 1260 | dev_dbg(&spi->dev, "setup: can't be active-high\n"); |
@@ -728,7 +1263,7 @@ static int atmel_spi_setup(struct spi_device *spi) | |||
728 | 1263 | ||
729 | /* v1 chips start out at half the peripheral bus speed. */ | 1264 | /* v1 chips start out at half the peripheral bus speed. */ |
730 | bus_hz = clk_get_rate(as->clk); | 1265 | bus_hz = clk_get_rate(as->clk); |
731 | if (!atmel_spi_is_v2()) | 1266 | if (!atmel_spi_is_v2(as)) |
732 | bus_hz /= 2; | 1267 | bus_hz /= 2; |
733 | 1268 | ||
734 | if (spi->max_speed_hz) { | 1269 | if (spi->max_speed_hz) { |
@@ -789,13 +1324,11 @@ static int atmel_spi_setup(struct spi_device *spi) | |||
789 | spi->controller_state = asd; | 1324 | spi->controller_state = asd; |
790 | gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH)); | 1325 | gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH)); |
791 | } else { | 1326 | } else { |
792 | unsigned long flags; | 1327 | atmel_spi_lock(as); |
793 | |||
794 | spin_lock_irqsave(&as->lock, flags); | ||
795 | if (as->stay == spi) | 1328 | if (as->stay == spi) |
796 | as->stay = NULL; | 1329 | as->stay = NULL; |
797 | cs_deactivate(as, spi); | 1330 | cs_deactivate(as, spi); |
798 | spin_unlock_irqrestore(&as->lock, flags); | 1331 | atmel_spi_unlock(as); |
799 | } | 1332 | } |
800 | 1333 | ||
801 | asd->csr = csr; | 1334 | asd->csr = csr; |
@@ -804,7 +1337,7 @@ static int atmel_spi_setup(struct spi_device *spi) | |||
804 | "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n", | 1337 | "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n", |
805 | bus_hz / scbr, bits, spi->mode, spi->chip_select, csr); | 1338 | bus_hz / scbr, bits, spi->mode, spi->chip_select, csr); |
806 | 1339 | ||
807 | if (!atmel_spi_is_v2()) | 1340 | if (!atmel_spi_is_v2(as)) |
808 | spi_writel(as, CSR0 + 4 * spi->chip_select, csr); | 1341 | spi_writel(as, CSR0 + 4 * spi->chip_select, csr); |
809 | 1342 | ||
810 | return 0; | 1343 | return 0; |
@@ -814,7 +1347,6 @@ static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg) | |||
814 | { | 1347 | { |
815 | struct atmel_spi *as; | 1348 | struct atmel_spi *as; |
816 | struct spi_transfer *xfer; | 1349 | struct spi_transfer *xfer; |
817 | unsigned long flags; | ||
818 | struct device *controller = spi->master->dev.parent; | 1350 | struct device *controller = spi->master->dev.parent; |
819 | u8 bits; | 1351 | u8 bits; |
820 | struct atmel_spi_device *asd; | 1352 | struct atmel_spi_device *asd; |
@@ -854,13 +1386,10 @@ static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg) | |||
854 | 1386 | ||
855 | /* | 1387 | /* |
856 | * DMA map early, for performance (empties dcache ASAP) and | 1388 | * DMA map early, for performance (empties dcache ASAP) and |
857 | * better fault reporting. This is a DMA-only driver. | 1389 | * better fault reporting. |
858 | * | ||
859 | * NOTE that if dma_unmap_single() ever starts to do work on | ||
860 | * platforms supported by this driver, we would need to clean | ||
861 | * up mappings for previously-mapped transfers. | ||
862 | */ | 1390 | */ |
863 | if (!msg->is_dma_mapped) { | 1391 | if ((!msg->is_dma_mapped) && (atmel_spi_use_dma(as, xfer) |
1392 | || as->use_pdc)) { | ||
864 | if (atmel_spi_dma_map_xfer(as, xfer) < 0) | 1393 | if (atmel_spi_dma_map_xfer(as, xfer) < 0) |
865 | return -ENOMEM; | 1394 | return -ENOMEM; |
866 | } | 1395 | } |
@@ -879,11 +1408,11 @@ static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg) | |||
879 | msg->status = -EINPROGRESS; | 1408 | msg->status = -EINPROGRESS; |
880 | msg->actual_length = 0; | 1409 | msg->actual_length = 0; |
881 | 1410 | ||
882 | spin_lock_irqsave(&as->lock, flags); | 1411 | atmel_spi_lock(as); |
883 | list_add_tail(&msg->queue, &as->queue); | 1412 | list_add_tail(&msg->queue, &as->queue); |
884 | if (!as->current_transfer) | 1413 | if (!as->current_transfer) |
885 | atmel_spi_next_message(spi->master); | 1414 | atmel_spi_next_message(spi->master); |
886 | spin_unlock_irqrestore(&as->lock, flags); | 1415 | atmel_spi_unlock(as); |
887 | 1416 | ||
888 | return 0; | 1417 | return 0; |
889 | } | 1418 | } |
@@ -893,23 +1422,39 @@ static void atmel_spi_cleanup(struct spi_device *spi) | |||
893 | struct atmel_spi *as = spi_master_get_devdata(spi->master); | 1422 | struct atmel_spi *as = spi_master_get_devdata(spi->master); |
894 | struct atmel_spi_device *asd = spi->controller_state; | 1423 | struct atmel_spi_device *asd = spi->controller_state; |
895 | unsigned gpio = (unsigned) spi->controller_data; | 1424 | unsigned gpio = (unsigned) spi->controller_data; |
896 | unsigned long flags; | ||
897 | 1425 | ||
898 | if (!asd) | 1426 | if (!asd) |
899 | return; | 1427 | return; |
900 | 1428 | ||
901 | spin_lock_irqsave(&as->lock, flags); | 1429 | atmel_spi_lock(as); |
902 | if (as->stay == spi) { | 1430 | if (as->stay == spi) { |
903 | as->stay = NULL; | 1431 | as->stay = NULL; |
904 | cs_deactivate(as, spi); | 1432 | cs_deactivate(as, spi); |
905 | } | 1433 | } |
906 | spin_unlock_irqrestore(&as->lock, flags); | 1434 | atmel_spi_unlock(as); |
907 | 1435 | ||
908 | spi->controller_state = NULL; | 1436 | spi->controller_state = NULL; |
909 | gpio_free(gpio); | 1437 | gpio_free(gpio); |
910 | kfree(asd); | 1438 | kfree(asd); |
911 | } | 1439 | } |
912 | 1440 | ||
1441 | static inline unsigned int atmel_get_version(struct atmel_spi *as) | ||
1442 | { | ||
1443 | return spi_readl(as, VERSION) & 0x00000fff; | ||
1444 | } | ||
1445 | |||
1446 | static void atmel_get_caps(struct atmel_spi *as) | ||
1447 | { | ||
1448 | unsigned int version; | ||
1449 | |||
1450 | version = atmel_get_version(as); | ||
1451 | dev_info(&as->pdev->dev, "version: 0x%x\n", version); | ||
1452 | |||
1453 | as->caps.is_spi2 = version > 0x121; | ||
1454 | as->caps.has_wdrbt = version >= 0x210; | ||
1455 | as->caps.has_dma_support = version >= 0x212; | ||
1456 | } | ||
1457 | |||
913 | /*-------------------------------------------------------------------------*/ | 1458 | /*-------------------------------------------------------------------------*/ |
914 | 1459 | ||
915 | static int atmel_spi_probe(struct platform_device *pdev) | 1460 | static int atmel_spi_probe(struct platform_device *pdev) |
@@ -963,15 +1508,39 @@ static int atmel_spi_probe(struct platform_device *pdev) | |||
963 | 1508 | ||
964 | spin_lock_init(&as->lock); | 1509 | spin_lock_init(&as->lock); |
965 | INIT_LIST_HEAD(&as->queue); | 1510 | INIT_LIST_HEAD(&as->queue); |
1511 | |||
966 | as->pdev = pdev; | 1512 | as->pdev = pdev; |
967 | as->regs = ioremap(regs->start, resource_size(regs)); | 1513 | as->regs = ioremap(regs->start, resource_size(regs)); |
968 | if (!as->regs) | 1514 | if (!as->regs) |
969 | goto out_free_buffer; | 1515 | goto out_free_buffer; |
1516 | as->phybase = regs->start; | ||
970 | as->irq = irq; | 1517 | as->irq = irq; |
971 | as->clk = clk; | 1518 | as->clk = clk; |
972 | 1519 | ||
973 | ret = request_irq(irq, atmel_spi_interrupt, 0, | 1520 | atmel_get_caps(as); |
974 | dev_name(&pdev->dev), master); | 1521 | |
1522 | as->use_dma = false; | ||
1523 | as->use_pdc = false; | ||
1524 | if (as->caps.has_dma_support) { | ||
1525 | if (atmel_spi_configure_dma(as) == 0) | ||
1526 | as->use_dma = true; | ||
1527 | } else { | ||
1528 | as->use_pdc = true; | ||
1529 | } | ||
1530 | |||
1531 | if (as->caps.has_dma_support && !as->use_dma) | ||
1532 | dev_info(&pdev->dev, "Atmel SPI Controller using PIO only\n"); | ||
1533 | |||
1534 | if (as->use_pdc) { | ||
1535 | ret = request_irq(irq, atmel_spi_pdc_interrupt, 0, | ||
1536 | dev_name(&pdev->dev), master); | ||
1537 | } else { | ||
1538 | tasklet_init(&as->tasklet, atmel_spi_tasklet_func, | ||
1539 | (unsigned long)master); | ||
1540 | |||
1541 | ret = request_irq(irq, atmel_spi_pio_interrupt, 0, | ||
1542 | dev_name(&pdev->dev), master); | ||
1543 | } | ||
975 | if (ret) | 1544 | if (ret) |
976 | goto out_unmap_regs; | 1545 | goto out_unmap_regs; |
977 | 1546 | ||
@@ -979,8 +1548,15 @@ static int atmel_spi_probe(struct platform_device *pdev) | |||
979 | clk_enable(clk); | 1548 | clk_enable(clk); |
980 | spi_writel(as, CR, SPI_BIT(SWRST)); | 1549 | spi_writel(as, CR, SPI_BIT(SWRST)); |
981 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ | 1550 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ |
982 | spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS)); | 1551 | if (as->caps.has_wdrbt) { |
983 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); | 1552 | spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS) |
1553 | | SPI_BIT(MSTR)); | ||
1554 | } else { | ||
1555 | spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS)); | ||
1556 | } | ||
1557 | |||
1558 | if (as->use_pdc) | ||
1559 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); | ||
984 | spi_writel(as, CR, SPI_BIT(SPIEN)); | 1560 | spi_writel(as, CR, SPI_BIT(SPIEN)); |
985 | 1561 | ||
986 | /* go! */ | 1562 | /* go! */ |
@@ -989,11 +1565,14 @@ static int atmel_spi_probe(struct platform_device *pdev) | |||
989 | 1565 | ||
990 | ret = spi_register_master(master); | 1566 | ret = spi_register_master(master); |
991 | if (ret) | 1567 | if (ret) |
992 | goto out_reset_hw; | 1568 | goto out_free_dma; |
993 | 1569 | ||
994 | return 0; | 1570 | return 0; |
995 | 1571 | ||
996 | out_reset_hw: | 1572 | out_free_dma: |
1573 | if (as->use_dma) | ||
1574 | atmel_spi_release_dma(as); | ||
1575 | |||
997 | spi_writel(as, CR, SPI_BIT(SWRST)); | 1576 | spi_writel(as, CR, SPI_BIT(SWRST)); |
998 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ | 1577 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ |
999 | clk_disable(clk); | 1578 | clk_disable(clk); |
@@ -1001,6 +1580,8 @@ out_reset_hw: | |||
1001 | out_unmap_regs: | 1580 | out_unmap_regs: |
1002 | iounmap(as->regs); | 1581 | iounmap(as->regs); |
1003 | out_free_buffer: | 1582 | out_free_buffer: |
1583 | if (!as->use_pdc) | ||
1584 | tasklet_kill(&as->tasklet); | ||
1004 | dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer, | 1585 | dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer, |
1005 | as->buffer_dma); | 1586 | as->buffer_dma); |
1006 | out_free: | 1587 | out_free: |
@@ -1014,10 +1595,16 @@ static int atmel_spi_remove(struct platform_device *pdev) | |||
1014 | struct spi_master *master = platform_get_drvdata(pdev); | 1595 | struct spi_master *master = platform_get_drvdata(pdev); |
1015 | struct atmel_spi *as = spi_master_get_devdata(master); | 1596 | struct atmel_spi *as = spi_master_get_devdata(master); |
1016 | struct spi_message *msg; | 1597 | struct spi_message *msg; |
1598 | struct spi_transfer *xfer; | ||
1017 | 1599 | ||
1018 | /* reset the hardware and block queue progress */ | 1600 | /* reset the hardware and block queue progress */ |
1019 | spin_lock_irq(&as->lock); | 1601 | spin_lock_irq(&as->lock); |
1020 | as->stopping = 1; | 1602 | as->stopping = 1; |
1603 | if (as->use_dma) { | ||
1604 | atmel_spi_stop_dma(as); | ||
1605 | atmel_spi_release_dma(as); | ||
1606 | } | ||
1607 | |||
1021 | spi_writel(as, CR, SPI_BIT(SWRST)); | 1608 | spi_writel(as, CR, SPI_BIT(SWRST)); |
1022 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ | 1609 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ |
1023 | spi_readl(as, SR); | 1610 | spi_readl(as, SR); |
@@ -1025,13 +1612,18 @@ static int atmel_spi_remove(struct platform_device *pdev) | |||
1025 | 1612 | ||
1026 | /* Terminate remaining queued transfers */ | 1613 | /* Terminate remaining queued transfers */ |
1027 | list_for_each_entry(msg, &as->queue, queue) { | 1614 | list_for_each_entry(msg, &as->queue, queue) { |
1028 | /* REVISIT unmapping the dma is a NOP on ARM and AVR32 | 1615 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
1029 | * but we shouldn't depend on that... | 1616 | if (!msg->is_dma_mapped |
1030 | */ | 1617 | && (atmel_spi_use_dma(as, xfer) |
1618 | || as->use_pdc)) | ||
1619 | atmel_spi_dma_unmap_xfer(master, xfer); | ||
1620 | } | ||
1031 | msg->status = -ESHUTDOWN; | 1621 | msg->status = -ESHUTDOWN; |
1032 | msg->complete(msg->context); | 1622 | msg->complete(msg->context); |
1033 | } | 1623 | } |
1034 | 1624 | ||
1625 | if (!as->use_pdc) | ||
1626 | tasklet_kill(&as->tasklet); | ||
1035 | dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer, | 1627 | dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer, |
1036 | as->buffer_dma); | 1628 | as->buffer_dma); |
1037 | 1629 | ||