Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

spi/ep93xx: add DMA support

This patch adds DMA support for the EP93xx SPI driver. By default the DMA is
not enabled but it can be enabled by setting ep93xx_spi_info.use_dma to true
in board configuration file.

Note that the SPI driver still uses PIO for small transfers (<= 8 bytes) for
performance reasons.

Signed-off-by: Mika Westerberg <mika.westerberg@iki.fi>
Acked-by: H Hartley Sweeten <hsweeten@visionengravers.com>
Acked-by: Vinod Koul <vinod.koul@intel.com>
Signed-off-by: Grant Likely <grant.likely@secretlab.ca>

authored by

Mika Westerberg and committed by
Grant Likely
626a96db 71cebd70

+308 -13
+10
Documentation/spi/ep93xx_spi
··· 88 88 ARRAY_SIZE(ts72xx_spi_devices)); 89 89 } 90 90 91 + The driver can use DMA for the transfers also. In this case ts72xx_spi_info 92 + becomes: 93 + 94 + static struct ep93xx_spi_info ts72xx_spi_info = { 95 + .num_chipselect = ARRAY_SIZE(ts72xx_spi_devices), 96 + .use_dma = true; 97 + }; 98 + 99 + Note that CONFIG_EP93XX_DMA should be enabled as well. 100 + 91 101 Thanks to 92 102 ========= 93 103 Martin Guy, H. Hartley Sweeten and others who helped me during development of
+5 -1
arch/arm/mach-ep93xx/core.c
··· 488 488 }, 489 489 }; 490 490 491 + static u64 ep93xx_spi_dma_mask = DMA_BIT_MASK(32); 492 + 491 493 static struct platform_device ep93xx_spi_device = { 492 494 .name = "ep93xx-spi", 493 495 .id = 0, 494 496 .dev = { 495 - .platform_data = &ep93xx_spi_master_data, 497 + .platform_data = &ep93xx_spi_master_data, 498 + .coherent_dma_mask = DMA_BIT_MASK(32), 499 + .dma_mask = &ep93xx_spi_dma_mask, 496 500 }, 497 501 .num_resources = ARRAY_SIZE(ep93xx_spi_resources), 498 502 .resource = ep93xx_spi_resources,
+2
arch/arm/mach-ep93xx/include/mach/ep93xx_spi.h
··· 7 7 * struct ep93xx_spi_info - EP93xx specific SPI descriptor 8 8 * @num_chipselect: number of chip selects on this board, must be 9 9 * at least one 10 + * @use_dma: use DMA for the transfers 10 11 */ 11 12 struct ep93xx_spi_info { 12 13 int num_chipselect; 14 + bool use_dma; 13 15 }; 14 16 15 17 /**
+291 -12
drivers/spi/ep93xx_spi.c
··· 1 1 /* 2 2 * Driver for Cirrus Logic EP93xx SPI controller. 3 3 * 4 - * Copyright (c) 2010 Mika Westerberg 4 + * Copyright (C) 2010-2011 Mika Westerberg 5 5 * 6 6 * Explicit FIFO handling code was inspired by amba-pl022 driver. 7 7 * ··· 21 21 #include <linux/err.h> 22 22 #include <linux/delay.h> 23 23 #include <linux/device.h> 24 + #include <linux/dmaengine.h> 24 25 #include <linux/bitops.h> 25 26 #include <linux/interrupt.h> 26 27 #include <linux/platform_device.h> 27 28 #include <linux/workqueue.h> 28 29 #include <linux/sched.h> 30 + #include <linux/scatterlist.h> 29 31 #include <linux/spi/spi.h> 30 32 33 + #include <mach/dma.h> 31 34 #include <mach/ep93xx_spi.h> 32 35 33 36 #define SSPCR0 0x0000 ··· 74 71 * @pdev: pointer to platform device 75 72 * @clk: clock for the controller 76 73 * @regs_base: pointer to ioremap()'d registers 74 + * @sspdr_phys: physical address of the SSPDR register 77 75 * @irq: IRQ number used by the driver 78 76 * @min_rate: minimum clock rate (in Hz) supported by the controller 79 77 * @max_rate: maximum clock rate (in Hz) supported by the controller ··· 88 84 * @rx: current byte in transfer to receive 89 85 * @fifo_level: how full is FIFO (%0..%SPI_FIFO_SIZE - %1). Receiving one 90 86 * frame decreases this level and sending one frame increases it. 87 + * @dma_rx: RX DMA channel 88 + * @dma_tx: TX DMA channel 89 + * @dma_rx_data: RX parameters passed to the DMA engine 90 + * @dma_tx_data: TX parameters passed to the DMA engine 91 + * @rx_sgt: sg table for RX transfers 92 + * @tx_sgt: sg table for TX transfers 93 + * @zeropage: dummy page used as RX buffer when only TX buffer is passed in by 94 + * the client 91 95 * 92 96 * This structure holds EP93xx SPI controller specific information. When 93 97 * @running is %true, driver accepts transfer requests from protocol drivers. ··· 112 100 const struct platform_device *pdev; 113 101 struct clk *clk; 114 102 void __iomem *regs_base; 103 + unsigned long sspdr_phys; 115 104 int irq; 116 105 unsigned long min_rate; 117 106 unsigned long max_rate; ··· 125 112 size_t tx; 126 113 size_t rx; 127 114 size_t fifo_level; 115 + struct dma_chan *dma_rx; 116 + struct dma_chan *dma_tx; 117 + struct ep93xx_dma_data dma_rx_data; 118 + struct ep93xx_dma_data dma_tx_data; 119 + struct sg_table rx_sgt; 120 + struct sg_table tx_sgt; 121 + void *zeropage; 128 122 }; 129 123 130 124 /** ··· 516 496 espi->fifo_level++; 517 497 } 518 498 519 - if (espi->rx == t->len) { 520 - msg->actual_length += t->len; 499 + if (espi->rx == t->len) 521 500 return 0; 522 - } 523 501 524 502 return -EINPROGRESS; 503 + } 504 + 505 + static void ep93xx_spi_pio_transfer(struct ep93xx_spi *espi) 506 + { 507 + /* 508 + * Now everything is set up for the current transfer. We prime the TX 509 + * FIFO, enable interrupts, and wait for the transfer to complete. 510 + */ 511 + if (ep93xx_spi_read_write(espi)) { 512 + ep93xx_spi_enable_interrupts(espi); 513 + wait_for_completion(&espi->wait); 514 + } 515 + } 516 + 517 + /** 518 + * ep93xx_spi_dma_prepare() - prepares a DMA transfer 519 + * @espi: ep93xx SPI controller struct 520 + * @dir: DMA transfer direction 521 + * 522 + * Function configures the DMA, maps the buffer and prepares the DMA 523 + * descriptor. Returns a valid DMA descriptor in case of success and ERR_PTR 524 + * in case of failure. 525 + */ 526 + static struct dma_async_tx_descriptor * 527 + ep93xx_spi_dma_prepare(struct ep93xx_spi *espi, enum dma_data_direction dir) 528 + { 529 + struct spi_transfer *t = espi->current_msg->state; 530 + struct dma_async_tx_descriptor *txd; 531 + enum dma_slave_buswidth buswidth; 532 + struct dma_slave_config conf; 533 + struct scatterlist *sg; 534 + struct sg_table *sgt; 535 + struct dma_chan *chan; 536 + const void *buf, *pbuf; 537 + size_t len = t->len; 538 + int i, ret, nents; 539 + 540 + if (bits_per_word(espi) > 8) 541 + buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES; 542 + else 543 + buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE; 544 + 545 + memset(&conf, 0, sizeof(conf)); 546 + conf.direction = dir; 547 + 548 + if (dir == DMA_FROM_DEVICE) { 549 + chan = espi->dma_rx; 550 + buf = t->rx_buf; 551 + sgt = &espi->rx_sgt; 552 + 553 + conf.src_addr = espi->sspdr_phys; 554 + conf.src_addr_width = buswidth; 555 + } else { 556 + chan = espi->dma_tx; 557 + buf = t->tx_buf; 558 + sgt = &espi->tx_sgt; 559 + 560 + conf.dst_addr = espi->sspdr_phys; 561 + conf.dst_addr_width = buswidth; 562 + } 563 + 564 + ret = dmaengine_slave_config(chan, &conf); 565 + if (ret) 566 + return ERR_PTR(ret); 567 + 568 + /* 569 + * We need to split the transfer into PAGE_SIZE'd chunks. This is 570 + * because we are using @espi->zeropage to provide a zero RX buffer 571 + * for the TX transfers and we have only allocated one page for that. 572 + * 573 + * For performance reasons we allocate a new sg_table only when 574 + * needed. Otherwise we will re-use the current one. Eventually the 575 + * last sg_table is released in ep93xx_spi_release_dma(). 576 + */ 577 + 578 + nents = DIV_ROUND_UP(len, PAGE_SIZE); 579 + if (nents != sgt->nents) { 580 + sg_free_table(sgt); 581 + 582 + ret = sg_alloc_table(sgt, nents, GFP_KERNEL); 583 + if (ret) 584 + return ERR_PTR(ret); 585 + } 586 + 587 + pbuf = buf; 588 + for_each_sg(sgt->sgl, sg, sgt->nents, i) { 589 + size_t bytes = min_t(size_t, len, PAGE_SIZE); 590 + 591 + if (buf) { 592 + sg_set_page(sg, virt_to_page(pbuf), bytes, 593 + offset_in_page(pbuf)); 594 + } else { 595 + sg_set_page(sg, virt_to_page(espi->zeropage), 596 + bytes, 0); 597 + } 598 + 599 + pbuf += bytes; 600 + len -= bytes; 601 + } 602 + 603 + if (WARN_ON(len)) { 604 + dev_warn(&espi->pdev->dev, "len = %d expected 0!", len); 605 + return ERR_PTR(-EINVAL); 606 + } 607 + 608 + nents = dma_map_sg(chan->device->dev, sgt->sgl, sgt->nents, dir); 609 + if (!nents) 610 + return ERR_PTR(-ENOMEM); 611 + 612 + txd = chan->device->device_prep_slave_sg(chan, sgt->sgl, nents, 613 + dir, DMA_CTRL_ACK); 614 + if (!txd) { 615 + dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir); 616 + return ERR_PTR(-ENOMEM); 617 + } 618 + return txd; 619 + } 620 + 621 + /** 622 + * ep93xx_spi_dma_finish() - finishes with a DMA transfer 623 + * @espi: ep93xx SPI controller struct 624 + * @dir: DMA transfer direction 625 + * 626 + * Function finishes with the DMA transfer. After this, the DMA buffer is 627 + * unmapped. 628 + */ 629 + static void ep93xx_spi_dma_finish(struct ep93xx_spi *espi, 630 + enum dma_data_direction dir) 631 + { 632 + struct dma_chan *chan; 633 + struct sg_table *sgt; 634 + 635 + if (dir == DMA_FROM_DEVICE) { 636 + chan = espi->dma_rx; 637 + sgt = &espi->rx_sgt; 638 + } else { 639 + chan = espi->dma_tx; 640 + sgt = &espi->tx_sgt; 641 + } 642 + 643 + dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir); 644 + } 645 + 646 + static void ep93xx_spi_dma_callback(void *callback_param) 647 + { 648 + complete(callback_param); 649 + } 650 + 651 + static void ep93xx_spi_dma_transfer(struct ep93xx_spi *espi) 652 + { 653 + struct spi_message *msg = espi->current_msg; 654 + struct dma_async_tx_descriptor *rxd, *txd; 655 + 656 + rxd = ep93xx_spi_dma_prepare(espi, DMA_FROM_DEVICE); 657 + if (IS_ERR(rxd)) { 658 + dev_err(&espi->pdev->dev, "DMA RX failed: %ld\n", PTR_ERR(rxd)); 659 + msg->status = PTR_ERR(rxd); 660 + return; 661 + } 662 + 663 + txd = ep93xx_spi_dma_prepare(espi, DMA_TO_DEVICE); 664 + if (IS_ERR(txd)) { 665 + ep93xx_spi_dma_finish(espi, DMA_FROM_DEVICE); 666 + dev_err(&espi->pdev->dev, "DMA TX failed: %ld\n", PTR_ERR(rxd)); 667 + msg->status = PTR_ERR(txd); 668 + return; 669 + } 670 + 671 + /* We are ready when RX is done */ 672 + rxd->callback = ep93xx_spi_dma_callback; 673 + rxd->callback_param = &espi->wait; 674 + 675 + /* Now submit both descriptors and wait while they finish */ 676 + dmaengine_submit(rxd); 677 + dmaengine_submit(txd); 678 + 679 + dma_async_issue_pending(espi->dma_rx); 680 + dma_async_issue_pending(espi->dma_tx); 681 + 682 + wait_for_completion(&espi->wait); 683 + 684 + ep93xx_spi_dma_finish(espi, DMA_TO_DEVICE); 685 + ep93xx_spi_dma_finish(espi, DMA_FROM_DEVICE); 525 686 } 526 687 527 688 /** ··· 757 556 espi->tx = 0; 758 557 759 558 /* 760 - * Now everything is set up for the current transfer. We prime the TX 761 - * FIFO, enable interrupts, and wait for the transfer to complete. 559 + * There is no point of setting up DMA for the transfers which will 560 + * fit into the FIFO and can be transferred with a single interrupt. 561 + * So in these cases we will be using PIO and don't bother for DMA. 762 562 */ 763 - if (ep93xx_spi_read_write(espi)) { 764 - ep93xx_spi_enable_interrupts(espi); 765 - wait_for_completion(&espi->wait); 766 - } 563 + if (espi->dma_rx && t->len > SPI_FIFO_SIZE) 564 + ep93xx_spi_dma_transfer(espi); 565 + else 566 + ep93xx_spi_pio_transfer(espi); 767 567 768 568 /* 769 569 * In case of error during transmit, we bail out from processing ··· 772 570 */ 773 571 if (msg->status) 774 572 return; 573 + 574 + msg->actual_length += t->len; 775 575 776 576 /* 777 577 * After this transfer is finished, perform any possible ··· 956 752 return IRQ_HANDLED; 957 753 } 958 754 755 + static bool ep93xx_spi_dma_filter(struct dma_chan *chan, void *filter_param) 756 + { 757 + if (ep93xx_dma_chan_is_m2p(chan)) 758 + return false; 759 + 760 + chan->private = filter_param; 761 + return true; 762 + } 763 + 764 + static int ep93xx_spi_setup_dma(struct ep93xx_spi *espi) 765 + { 766 + dma_cap_mask_t mask; 767 + int ret; 768 + 769 + espi->zeropage = (void *)get_zeroed_page(GFP_KERNEL); 770 + if (!espi->zeropage) 771 + return -ENOMEM; 772 + 773 + dma_cap_zero(mask); 774 + dma_cap_set(DMA_SLAVE, mask); 775 + 776 + espi->dma_rx_data.port = EP93XX_DMA_SSP; 777 + espi->dma_rx_data.direction = DMA_FROM_DEVICE; 778 + espi->dma_rx_data.name = "ep93xx-spi-rx"; 779 + 780 + espi->dma_rx = dma_request_channel(mask, ep93xx_spi_dma_filter, 781 + &espi->dma_rx_data); 782 + if (!espi->dma_rx) { 783 + ret = -ENODEV; 784 + goto fail_free_page; 785 + } 786 + 787 + espi->dma_tx_data.port = EP93XX_DMA_SSP; 788 + espi->dma_tx_data.direction = DMA_TO_DEVICE; 789 + espi->dma_tx_data.name = "ep93xx-spi-tx"; 790 + 791 + espi->dma_tx = dma_request_channel(mask, ep93xx_spi_dma_filter, 792 + &espi->dma_tx_data); 793 + if (!espi->dma_tx) { 794 + ret = -ENODEV; 795 + goto fail_release_rx; 796 + } 797 + 798 + return 0; 799 + 800 + fail_release_rx: 801 + dma_release_channel(espi->dma_rx); 802 + espi->dma_rx = NULL; 803 + fail_free_page: 804 + free_page((unsigned long)espi->zeropage); 805 + 806 + return ret; 807 + } 808 + 809 + static void ep93xx_spi_release_dma(struct ep93xx_spi *espi) 810 + { 811 + if (espi->dma_rx) { 812 + dma_release_channel(espi->dma_rx); 813 + sg_free_table(&espi->rx_sgt); 814 + } 815 + if (espi->dma_tx) { 816 + dma_release_channel(espi->dma_tx); 817 + sg_free_table(&espi->tx_sgt); 818 + } 819 + 820 + if (espi->zeropage) 821 + free_page((unsigned long)espi->zeropage); 822 + } 823 + 959 824 static int __init ep93xx_spi_probe(struct platform_device *pdev) 960 825 { 961 826 struct spi_master *master; ··· 1091 818 goto fail_put_clock; 1092 819 } 1093 820 821 + espi->sspdr_phys = res->start + SSPDR; 1094 822 espi->regs_base = ioremap(res->start, resource_size(res)); 1095 823 if (!espi->regs_base) { 1096 824 dev_err(&pdev->dev, "failed to map resources\n"); ··· 1106 832 goto fail_unmap_regs; 1107 833 } 1108 834 835 + if (info->use_dma && ep93xx_spi_setup_dma(espi)) 836 + dev_warn(&pdev->dev, "DMA setup failed. Falling back to PIO\n"); 837 + 1109 838 espi->wq = create_singlethread_workqueue("ep93xx_spid"); 1110 839 if (!espi->wq) { 1111 840 dev_err(&pdev->dev, "unable to create workqueue\n"); 1112 - goto fail_free_irq; 841 + goto fail_free_dma; 1113 842 } 1114 843 INIT_WORK(&espi->msg_work, ep93xx_spi_work); 1115 844 INIT_LIST_HEAD(&espi->msg_queue); ··· 1134 857 1135 858 fail_free_queue: 1136 859 destroy_workqueue(espi->wq); 1137 - fail_free_irq: 860 + fail_free_dma: 861 + ep93xx_spi_release_dma(espi); 1138 862 free_irq(espi->irq, espi); 1139 863 fail_unmap_regs: 1140 864 iounmap(espi->regs_base); ··· 1179 901 } 1180 902 spin_unlock_irq(&espi->lock); 1181 903 904 + ep93xx_spi_release_dma(espi); 1182 905 free_irq(espi->irq, espi); 1183 906 iounmap(espi->regs_base); 1184 907 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);