aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/dma/amba-pl08x.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/dma/amba-pl08x.c')
-rw-r--r--drivers/dma/amba-pl08x.c455
1 files changed, 197 insertions, 258 deletions
diff --git a/drivers/dma/amba-pl08x.c b/drivers/dma/amba-pl08x.c
index be21e3f138a8..cd8df7f5b5c8 100644
--- a/drivers/dma/amba-pl08x.c
+++ b/drivers/dma/amba-pl08x.c
@@ -66,32 +66,29 @@
66 * after the final transfer signalled by LBREQ or LSREQ. The DMAC 66 * after the final transfer signalled by LBREQ or LSREQ. The DMAC
67 * will then move to the next LLI entry. 67 * will then move to the next LLI entry.
68 * 68 *
69 * Only the former works sanely with scatter lists, so we only implement
70 * the DMAC flow control method. However, peripherals which use the LBREQ
71 * and LSREQ signals (eg, MMCI) are unable to use this mode, which through
72 * these hardware restrictions prevents them from using scatter DMA.
73 *
74 * Global TODO: 69 * Global TODO:
75 * - Break out common code from arch/arm/mach-s3c64xx and share 70 * - Break out common code from arch/arm/mach-s3c64xx and share
76 */ 71 */
77#include <linux/device.h>
78#include <linux/init.h>
79#include <linux/module.h>
80#include <linux/interrupt.h>
81#include <linux/slab.h>
82#include <linux/delay.h>
83#include <linux/dma-mapping.h>
84#include <linux/dmapool.h>
85#include <linux/dmaengine.h>
86#include <linux/amba/bus.h> 72#include <linux/amba/bus.h>
87#include <linux/amba/pl08x.h> 73#include <linux/amba/pl08x.h>
88#include <linux/debugfs.h> 74#include <linux/debugfs.h>
75#include <linux/delay.h>
76#include <linux/device.h>
77#include <linux/dmaengine.h>
78#include <linux/dmapool.h>
79#include <linux/dma-mapping.h>
80#include <linux/init.h>
81#include <linux/interrupt.h>
82#include <linux/module.h>
83#include <linux/pm_runtime.h>
89#include <linux/seq_file.h> 84#include <linux/seq_file.h>
90 85#include <linux/slab.h>
91#include <asm/hardware/pl080.h> 86#include <asm/hardware/pl080.h>
92 87
93#define DRIVER_NAME "pl08xdmac" 88#define DRIVER_NAME "pl08xdmac"
94 89
90static struct amba_driver pl08x_amba_driver;
91
95/** 92/**
96 * struct vendor_data - vendor-specific config parameters for PL08x derivatives 93 * struct vendor_data - vendor-specific config parameters for PL08x derivatives
97 * @channels: the number of channels available in this variant 94 * @channels: the number of channels available in this variant
@@ -126,7 +123,8 @@ struct pl08x_lli {
126 * @phy_chans: array of data for the physical channels 123 * @phy_chans: array of data for the physical channels
127 * @pool: a pool for the LLI descriptors 124 * @pool: a pool for the LLI descriptors
128 * @pool_ctr: counter of LLIs in the pool 125 * @pool_ctr: counter of LLIs in the pool
129 * @lli_buses: bitmask to or in to LLI pointer selecting AHB port for LLI fetches 126 * @lli_buses: bitmask to or in to LLI pointer selecting AHB port for LLI
127 * fetches
130 * @mem_buses: set to indicate memory transfers on AHB2. 128 * @mem_buses: set to indicate memory transfers on AHB2.
131 * @lock: a spinlock for this struct 129 * @lock: a spinlock for this struct
132 */ 130 */
@@ -149,14 +147,6 @@ struct pl08x_driver_data {
149 * PL08X specific defines 147 * PL08X specific defines
150 */ 148 */
151 149
152/*
153 * Memory boundaries: the manual for PL08x says that the controller
154 * cannot read past a 1KiB boundary, so these defines are used to
155 * create transfer LLIs that do not cross such boundaries.
156 */
157#define PL08X_BOUNDARY_SHIFT (10) /* 1KB 0x400 */
158#define PL08X_BOUNDARY_SIZE (1 << PL08X_BOUNDARY_SHIFT)
159
160/* Size (bytes) of each LLI buffer allocated for one transfer */ 150/* Size (bytes) of each LLI buffer allocated for one transfer */
161# define PL08X_LLI_TSFR_SIZE 0x2000 151# define PL08X_LLI_TSFR_SIZE 0x2000
162 152
@@ -272,7 +262,6 @@ static void pl08x_resume_phy_chan(struct pl08x_phy_chan *ch)
272 writel(val, ch->base + PL080_CH_CONFIG); 262 writel(val, ch->base + PL080_CH_CONFIG);
273} 263}
274 264
275
276/* 265/*
277 * pl08x_terminate_phy_chan() stops the channel, clears the FIFO and 266 * pl08x_terminate_phy_chan() stops the channel, clears the FIFO and
278 * clears any pending interrupt status. This should not be used for 267 * clears any pending interrupt status. This should not be used for
@@ -407,6 +396,7 @@ pl08x_get_phy_channel(struct pl08x_driver_data *pl08x,
407 return NULL; 396 return NULL;
408 } 397 }
409 398
399 pm_runtime_get_sync(&pl08x->adev->dev);
410 return ch; 400 return ch;
411} 401}
412 402
@@ -420,6 +410,8 @@ static inline void pl08x_put_phy_channel(struct pl08x_driver_data *pl08x,
420 /* Stop the channel and clear its interrupts */ 410 /* Stop the channel and clear its interrupts */
421 pl08x_terminate_phy_chan(pl08x, ch); 411 pl08x_terminate_phy_chan(pl08x, ch);
422 412
413 pm_runtime_put(&pl08x->adev->dev);
414
423 /* Mark it as free */ 415 /* Mark it as free */
424 ch->serving = NULL; 416 ch->serving = NULL;
425 spin_unlock_irqrestore(&ch->lock, flags); 417 spin_unlock_irqrestore(&ch->lock, flags);
@@ -499,36 +491,30 @@ struct pl08x_lli_build_data {
499}; 491};
500 492
501/* 493/*
502 * Autoselect a master bus to use for the transfer this prefers the 494 * Autoselect a master bus to use for the transfer. Slave will be the chosen as
503 * destination bus if both available if fixed address on one bus the 495 * victim in case src & dest are not similarly aligned. i.e. If after aligning
504 * other will be chosen 496 * masters address with width requirements of transfer (by sending few byte by
497 * byte data), slave is still not aligned, then its width will be reduced to
498 * BYTE.
499 * - prefers the destination bus if both available
500 * - prefers bus with fixed address (i.e. peripheral)
505 */ 501 */
506static void pl08x_choose_master_bus(struct pl08x_lli_build_data *bd, 502static void pl08x_choose_master_bus(struct pl08x_lli_build_data *bd,
507 struct pl08x_bus_data **mbus, struct pl08x_bus_data **sbus, u32 cctl) 503 struct pl08x_bus_data **mbus, struct pl08x_bus_data **sbus, u32 cctl)
508{ 504{
509 if (!(cctl & PL080_CONTROL_DST_INCR)) { 505 if (!(cctl & PL080_CONTROL_DST_INCR)) {
510 *mbus = &bd->srcbus;
511 *sbus = &bd->dstbus;
512 } else if (!(cctl & PL080_CONTROL_SRC_INCR)) {
513 *mbus = &bd->dstbus; 506 *mbus = &bd->dstbus;
514 *sbus = &bd->srcbus; 507 *sbus = &bd->srcbus;
508 } else if (!(cctl & PL080_CONTROL_SRC_INCR)) {
509 *mbus = &bd->srcbus;
510 *sbus = &bd->dstbus;
515 } else { 511 } else {
516 if (bd->dstbus.buswidth == 4) { 512 if (bd->dstbus.buswidth >= bd->srcbus.buswidth) {
517 *mbus = &bd->dstbus;
518 *sbus = &bd->srcbus;
519 } else if (bd->srcbus.buswidth == 4) {
520 *mbus = &bd->srcbus;
521 *sbus = &bd->dstbus;
522 } else if (bd->dstbus.buswidth == 2) {
523 *mbus = &bd->dstbus; 513 *mbus = &bd->dstbus;
524 *sbus = &bd->srcbus; 514 *sbus = &bd->srcbus;
525 } else if (bd->srcbus.buswidth == 2) { 515 } else {
526 *mbus = &bd->srcbus; 516 *mbus = &bd->srcbus;
527 *sbus = &bd->dstbus; 517 *sbus = &bd->dstbus;
528 } else {
529 /* bd->srcbus.buswidth == 1 */
530 *mbus = &bd->dstbus;
531 *sbus = &bd->srcbus;
532 } 518 }
533 } 519 }
534} 520}
@@ -547,7 +533,8 @@ static void pl08x_fill_lli_for_desc(struct pl08x_lli_build_data *bd,
547 llis_va[num_llis].cctl = cctl; 533 llis_va[num_llis].cctl = cctl;
548 llis_va[num_llis].src = bd->srcbus.addr; 534 llis_va[num_llis].src = bd->srcbus.addr;
549 llis_va[num_llis].dst = bd->dstbus.addr; 535 llis_va[num_llis].dst = bd->dstbus.addr;
550 llis_va[num_llis].lli = llis_bus + (num_llis + 1) * sizeof(struct pl08x_lli); 536 llis_va[num_llis].lli = llis_bus + (num_llis + 1) *
537 sizeof(struct pl08x_lli);
551 llis_va[num_llis].lli |= bd->lli_bus; 538 llis_va[num_llis].lli |= bd->lli_bus;
552 539
553 if (cctl & PL080_CONTROL_SRC_INCR) 540 if (cctl & PL080_CONTROL_SRC_INCR)
@@ -560,16 +547,12 @@ static void pl08x_fill_lli_for_desc(struct pl08x_lli_build_data *bd,
560 bd->remainder -= len; 547 bd->remainder -= len;
561} 548}
562 549
563/* 550static inline void prep_byte_width_lli(struct pl08x_lli_build_data *bd,
564 * Return number of bytes to fill to boundary, or len. 551 u32 *cctl, u32 len, int num_llis, size_t *total_bytes)
565 * This calculation works for any value of addr.
566 */
567static inline size_t pl08x_pre_boundary(u32 addr, size_t len)
568{ 552{
569 size_t boundary_len = PL08X_BOUNDARY_SIZE - 553 *cctl = pl08x_cctl_bits(*cctl, 1, 1, len);
570 (addr & (PL08X_BOUNDARY_SIZE - 1)); 554 pl08x_fill_lli_for_desc(bd, num_llis, len, *cctl);
571 555 (*total_bytes) += len;
572 return min(boundary_len, len);
573} 556}
574 557
575/* 558/*
@@ -583,13 +566,11 @@ static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
583 struct pl08x_bus_data *mbus, *sbus; 566 struct pl08x_bus_data *mbus, *sbus;
584 struct pl08x_lli_build_data bd; 567 struct pl08x_lli_build_data bd;
585 int num_llis = 0; 568 int num_llis = 0;
586 u32 cctl; 569 u32 cctl, early_bytes = 0;
587 size_t max_bytes_per_lli; 570 size_t max_bytes_per_lli, total_bytes = 0;
588 size_t total_bytes = 0;
589 struct pl08x_lli *llis_va; 571 struct pl08x_lli *llis_va;
590 572
591 txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT, 573 txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT, &txd->llis_bus);
592 &txd->llis_bus);
593 if (!txd->llis_va) { 574 if (!txd->llis_va) {
594 dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__); 575 dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
595 return 0; 576 return 0;
@@ -619,55 +600,85 @@ static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
619 bd.srcbus.buswidth = bd.srcbus.maxwidth; 600 bd.srcbus.buswidth = bd.srcbus.maxwidth;
620 bd.dstbus.buswidth = bd.dstbus.maxwidth; 601 bd.dstbus.buswidth = bd.dstbus.maxwidth;
621 602
622 /*
623 * Bytes transferred == tsize * MIN(buswidths), not max(buswidths)
624 */
625 max_bytes_per_lli = min(bd.srcbus.buswidth, bd.dstbus.buswidth) *
626 PL080_CONTROL_TRANSFER_SIZE_MASK;
627
628 /* We need to count this down to zero */ 603 /* We need to count this down to zero */
629 bd.remainder = txd->len; 604 bd.remainder = txd->len;
630 605
631 /*
632 * Choose bus to align to
633 * - prefers destination bus if both available
634 * - if fixed address on one bus chooses other
635 */
636 pl08x_choose_master_bus(&bd, &mbus, &sbus, cctl); 606 pl08x_choose_master_bus(&bd, &mbus, &sbus, cctl);
637 607
638 dev_vdbg(&pl08x->adev->dev, "src=0x%08x%s/%u dst=0x%08x%s/%u len=%zu llimax=%zu\n", 608 dev_vdbg(&pl08x->adev->dev, "src=0x%08x%s/%u dst=0x%08x%s/%u len=%zu\n",
639 bd.srcbus.addr, cctl & PL080_CONTROL_SRC_INCR ? "+" : "", 609 bd.srcbus.addr, cctl & PL080_CONTROL_SRC_INCR ? "+" : "",
640 bd.srcbus.buswidth, 610 bd.srcbus.buswidth,
641 bd.dstbus.addr, cctl & PL080_CONTROL_DST_INCR ? "+" : "", 611 bd.dstbus.addr, cctl & PL080_CONTROL_DST_INCR ? "+" : "",
642 bd.dstbus.buswidth, 612 bd.dstbus.buswidth,
643 bd.remainder, max_bytes_per_lli); 613 bd.remainder);
644 dev_vdbg(&pl08x->adev->dev, "mbus=%s sbus=%s\n", 614 dev_vdbg(&pl08x->adev->dev, "mbus=%s sbus=%s\n",
645 mbus == &bd.srcbus ? "src" : "dst", 615 mbus == &bd.srcbus ? "src" : "dst",
646 sbus == &bd.srcbus ? "src" : "dst"); 616 sbus == &bd.srcbus ? "src" : "dst");
647 617
648 if (txd->len < mbus->buswidth) { 618 /*
649 /* Less than a bus width available - send as single bytes */ 619 * Zero length is only allowed if all these requirements are met:
650 while (bd.remainder) { 620 * - flow controller is peripheral.
651 dev_vdbg(&pl08x->adev->dev, 621 * - src.addr is aligned to src.width
652 "%s single byte LLIs for a transfer of " 622 * - dst.addr is aligned to dst.width
653 "less than a bus width (remain 0x%08x)\n", 623 *
654 __func__, bd.remainder); 624 * sg_len == 1 should be true, as there can be two cases here:
655 cctl = pl08x_cctl_bits(cctl, 1, 1, 1); 625 * - Memory addresses are contiguous and are not scattered. Here, Only
656 pl08x_fill_lli_for_desc(&bd, num_llis++, 1, cctl); 626 * one sg will be passed by user driver, with memory address and zero
657 total_bytes++; 627 * length. We pass this to controller and after the transfer it will
628 * receive the last burst request from peripheral and so transfer
629 * finishes.
630 *
631 * - Memory addresses are scattered and are not contiguous. Here,
632 * Obviously as DMA controller doesn't know when a lli's transfer gets
633 * over, it can't load next lli. So in this case, there has to be an
634 * assumption that only one lli is supported. Thus, we can't have
635 * scattered addresses.
636 */
637 if (!bd.remainder) {
638 u32 fc = (txd->ccfg & PL080_CONFIG_FLOW_CONTROL_MASK) >>
639 PL080_CONFIG_FLOW_CONTROL_SHIFT;
640 if (!((fc >= PL080_FLOW_SRC2DST_DST) &&
641 (fc <= PL080_FLOW_SRC2DST_SRC))) {
642 dev_err(&pl08x->adev->dev, "%s sg len can't be zero",
643 __func__);
644 return 0;
658 } 645 }
659 } else { 646
660 /* Make one byte LLIs until master bus is aligned */ 647 if ((bd.srcbus.addr % bd.srcbus.buswidth) ||
661 while ((mbus->addr) % (mbus->buswidth)) { 648 (bd.srcbus.addr % bd.srcbus.buswidth)) {
662 dev_vdbg(&pl08x->adev->dev, 649 dev_err(&pl08x->adev->dev,
663 "%s adjustment lli for less than bus width " 650 "%s src & dst address must be aligned to src"
664 "(remain 0x%08x)\n", 651 " & dst width if peripheral is flow controller",
665 __func__, bd.remainder); 652 __func__);
666 cctl = pl08x_cctl_bits(cctl, 1, 1, 1); 653 return 0;
667 pl08x_fill_lli_for_desc(&bd, num_llis++, 1, cctl);
668 total_bytes++;
669 } 654 }
670 655
656 cctl = pl08x_cctl_bits(cctl, bd.srcbus.buswidth,
657 bd.dstbus.buswidth, 0);
658 pl08x_fill_lli_for_desc(&bd, num_llis++, 0, cctl);
659 }
660
661 /*
662 * Send byte by byte for following cases
663 * - Less than a bus width available
664 * - until master bus is aligned
665 */
666 if (bd.remainder < mbus->buswidth)
667 early_bytes = bd.remainder;
668 else if ((mbus->addr) % (mbus->buswidth)) {
669 early_bytes = mbus->buswidth - (mbus->addr) % (mbus->buswidth);
670 if ((bd.remainder - early_bytes) < mbus->buswidth)
671 early_bytes = bd.remainder;
672 }
673
674 if (early_bytes) {
675 dev_vdbg(&pl08x->adev->dev, "%s byte width LLIs "
676 "(remain 0x%08x)\n", __func__, bd.remainder);
677 prep_byte_width_lli(&bd, &cctl, early_bytes, num_llis++,
678 &total_bytes);
679 }
680
681 if (bd.remainder) {
671 /* 682 /*
672 * Master now aligned 683 * Master now aligned
673 * - if slave is not then we must set its width down 684 * - if slave is not then we must set its width down
@@ -680,138 +691,55 @@ static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
680 sbus->buswidth = 1; 691 sbus->buswidth = 1;
681 } 692 }
682 693
694 /* Bytes transferred = tsize * src width, not MIN(buswidths) */
695 max_bytes_per_lli = bd.srcbus.buswidth *
696 PL080_CONTROL_TRANSFER_SIZE_MASK;
697
683 /* 698 /*
684 * Make largest possible LLIs until less than one bus 699 * Make largest possible LLIs until less than one bus
685 * width left 700 * width left
686 */ 701 */
687 while (bd.remainder > (mbus->buswidth - 1)) { 702 while (bd.remainder > (mbus->buswidth - 1)) {
688 size_t lli_len, target_len, tsize, odd_bytes; 703 size_t lli_len, tsize, width;
689 704
690 /* 705 /*
691 * If enough left try to send max possible, 706 * If enough left try to send max possible,
692 * otherwise try to send the remainder 707 * otherwise try to send the remainder
693 */ 708 */
694 target_len = min(bd.remainder, max_bytes_per_lli); 709 lli_len = min(bd.remainder, max_bytes_per_lli);
695 710
696 /* 711 /*
697 * Set bus lengths for incrementing buses to the 712 * Check against maximum bus alignment: Calculate actual
698 * number of bytes which fill to next memory boundary, 713 * transfer size in relation to bus width and get a
699 * limiting on the target length calculated above. 714 * maximum remainder of the highest bus width - 1
700 */ 715 */
701 if (cctl & PL080_CONTROL_SRC_INCR) 716 width = max(mbus->buswidth, sbus->buswidth);
702 bd.srcbus.fill_bytes = 717 lli_len = (lli_len / width) * width;
703 pl08x_pre_boundary(bd.srcbus.addr, 718 tsize = lli_len / bd.srcbus.buswidth;
704 target_len);
705 else
706 bd.srcbus.fill_bytes = target_len;
707
708 if (cctl & PL080_CONTROL_DST_INCR)
709 bd.dstbus.fill_bytes =
710 pl08x_pre_boundary(bd.dstbus.addr,
711 target_len);
712 else
713 bd.dstbus.fill_bytes = target_len;
714
715 /* Find the nearest */
716 lli_len = min(bd.srcbus.fill_bytes,
717 bd.dstbus.fill_bytes);
718
719 BUG_ON(lli_len > bd.remainder);
720
721 if (lli_len <= 0) {
722 dev_err(&pl08x->adev->dev,
723 "%s lli_len is %zu, <= 0\n",
724 __func__, lli_len);
725 return 0;
726 }
727
728 if (lli_len == target_len) {
729 /*
730 * Can send what we wanted.
731 * Maintain alignment
732 */
733 lli_len = (lli_len/mbus->buswidth) *
734 mbus->buswidth;
735 odd_bytes = 0;
736 } else {
737 /*
738 * So now we know how many bytes to transfer
739 * to get to the nearest boundary. The next
740 * LLI will past the boundary. However, we
741 * may be working to a boundary on the slave
742 * bus. We need to ensure the master stays
743 * aligned, and that we are working in
744 * multiples of the bus widths.
745 */
746 odd_bytes = lli_len % mbus->buswidth;
747 lli_len -= odd_bytes;
748
749 }
750
751 if (lli_len) {
752 /*
753 * Check against minimum bus alignment:
754 * Calculate actual transfer size in relation
755 * to bus width an get a maximum remainder of
756 * the smallest bus width - 1
757 */
758 /* FIXME: use round_down()? */
759 tsize = lli_len / min(mbus->buswidth,
760 sbus->buswidth);
761 lli_len = tsize * min(mbus->buswidth,
762 sbus->buswidth);
763
764 if (target_len != lli_len) {
765 dev_vdbg(&pl08x->adev->dev,
766 "%s can't send what we want. Desired 0x%08zx, lli of 0x%08zx bytes in txd of 0x%08zx\n",
767 __func__, target_len, lli_len, txd->len);
768 }
769
770 cctl = pl08x_cctl_bits(cctl,
771 bd.srcbus.buswidth,
772 bd.dstbus.buswidth,
773 tsize);
774
775 dev_vdbg(&pl08x->adev->dev,
776 "%s fill lli with single lli chunk of size 0x%08zx (remainder 0x%08zx)\n",
777 __func__, lli_len, bd.remainder);
778 pl08x_fill_lli_for_desc(&bd, num_llis++,
779 lli_len, cctl);
780 total_bytes += lli_len;
781 }
782 719
783 720 dev_vdbg(&pl08x->adev->dev,
784 if (odd_bytes) { 721 "%s fill lli with single lli chunk of "
785 /* 722 "size 0x%08zx (remainder 0x%08zx)\n",
786 * Creep past the boundary, maintaining 723 __func__, lli_len, bd.remainder);
787 * master alignment 724
788 */ 725 cctl = pl08x_cctl_bits(cctl, bd.srcbus.buswidth,
789 int j; 726 bd.dstbus.buswidth, tsize);
790 for (j = 0; (j < mbus->buswidth) 727 pl08x_fill_lli_for_desc(&bd, num_llis++, lli_len, cctl);
791 && (bd.remainder); j++) { 728 total_bytes += lli_len;
792 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
793 dev_vdbg(&pl08x->adev->dev,
794 "%s align with boundary, single byte (remain 0x%08zx)\n",
795 __func__, bd.remainder);
796 pl08x_fill_lli_for_desc(&bd,
797 num_llis++, 1, cctl);
798 total_bytes++;
799 }
800 }
801 } 729 }
802 730
803 /* 731 /*
804 * Send any odd bytes 732 * Send any odd bytes
805 */ 733 */
806 while (bd.remainder) { 734 if (bd.remainder) {
807 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
808 dev_vdbg(&pl08x->adev->dev, 735 dev_vdbg(&pl08x->adev->dev,
809 "%s align with boundary, single odd byte (remain %zu)\n", 736 "%s align with boundary, send odd bytes (remain %zu)\n",
810 __func__, bd.remainder); 737 __func__, bd.remainder);
811 pl08x_fill_lli_for_desc(&bd, num_llis++, 1, cctl); 738 prep_byte_width_lli(&bd, &cctl, bd.remainder,
812 total_bytes++; 739 num_llis++, &total_bytes);
813 } 740 }
814 } 741 }
742
815 if (total_bytes != txd->len) { 743 if (total_bytes != txd->len) {
816 dev_err(&pl08x->adev->dev, 744 dev_err(&pl08x->adev->dev,
817 "%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n", 745 "%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n",
@@ -917,9 +845,7 @@ static int prep_phy_channel(struct pl08x_dma_chan *plchan,
917 * need, but for slaves the physical signals may be muxed! 845 * need, but for slaves the physical signals may be muxed!
918 * Can the platform allow us to use this channel? 846 * Can the platform allow us to use this channel?
919 */ 847 */
920 if (plchan->slave && 848 if (plchan->slave && pl08x->pd->get_signal) {
921 ch->signal < 0 &&
922 pl08x->pd->get_signal) {
923 ret = pl08x->pd->get_signal(plchan); 849 ret = pl08x->pd->get_signal(plchan);
924 if (ret < 0) { 850 if (ret < 0) {
925 dev_dbg(&pl08x->adev->dev, 851 dev_dbg(&pl08x->adev->dev,
@@ -1008,10 +934,8 @@ static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
1008 * If slaves are relying on interrupts to signal completion this function 934 * If slaves are relying on interrupts to signal completion this function
1009 * must not be called with interrupts disabled. 935 * must not be called with interrupts disabled.
1010 */ 936 */
1011static enum dma_status 937static enum dma_status pl08x_dma_tx_status(struct dma_chan *chan,
1012pl08x_dma_tx_status(struct dma_chan *chan, 938 dma_cookie_t cookie, struct dma_tx_state *txstate)
1013 dma_cookie_t cookie,
1014 struct dma_tx_state *txstate)
1015{ 939{
1016 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan); 940 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1017 dma_cookie_t last_used; 941 dma_cookie_t last_used;
@@ -1253,7 +1177,9 @@ static int pl08x_prep_channel_resources(struct pl08x_dma_chan *plchan,
1253 1177
1254 num_llis = pl08x_fill_llis_for_desc(pl08x, txd); 1178 num_llis = pl08x_fill_llis_for_desc(pl08x, txd);
1255 if (!num_llis) { 1179 if (!num_llis) {
1256 kfree(txd); 1180 spin_lock_irqsave(&plchan->lock, flags);
1181 pl08x_free_txd(pl08x, txd);
1182 spin_unlock_irqrestore(&plchan->lock, flags);
1257 return -EINVAL; 1183 return -EINVAL;
1258 } 1184 }
1259 1185
@@ -1301,7 +1227,7 @@ static int pl08x_prep_channel_resources(struct pl08x_dma_chan *plchan,
1301static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan, 1227static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan,
1302 unsigned long flags) 1228 unsigned long flags)
1303{ 1229{
1304 struct pl08x_txd *txd = kzalloc(sizeof(struct pl08x_txd), GFP_NOWAIT); 1230 struct pl08x_txd *txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
1305 1231
1306 if (txd) { 1232 if (txd) {
1307 dma_async_tx_descriptor_init(&txd->tx, &plchan->chan); 1233 dma_async_tx_descriptor_init(&txd->tx, &plchan->chan);
@@ -1367,7 +1293,7 @@ static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
1367 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan); 1293 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1368 struct pl08x_driver_data *pl08x = plchan->host; 1294 struct pl08x_driver_data *pl08x = plchan->host;
1369 struct pl08x_txd *txd; 1295 struct pl08x_txd *txd;
1370 int ret; 1296 int ret, tmp;
1371 1297
1372 /* 1298 /*
1373 * Current implementation ASSUMES only one sg 1299 * Current implementation ASSUMES only one sg
@@ -1401,12 +1327,10 @@ static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
1401 txd->len = sgl->length; 1327 txd->len = sgl->length;
1402 1328
1403 if (direction == DMA_TO_DEVICE) { 1329 if (direction == DMA_TO_DEVICE) {
1404 txd->ccfg |= PL080_FLOW_MEM2PER << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1405 txd->cctl = plchan->dst_cctl; 1330 txd->cctl = plchan->dst_cctl;
1406 txd->src_addr = sgl->dma_address; 1331 txd->src_addr = sgl->dma_address;
1407 txd->dst_addr = plchan->dst_addr; 1332 txd->dst_addr = plchan->dst_addr;
1408 } else if (direction == DMA_FROM_DEVICE) { 1333 } else if (direction == DMA_FROM_DEVICE) {
1409 txd->ccfg |= PL080_FLOW_PER2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1410 txd->cctl = plchan->src_cctl; 1334 txd->cctl = plchan->src_cctl;
1411 txd->src_addr = plchan->src_addr; 1335 txd->src_addr = plchan->src_addr;
1412 txd->dst_addr = sgl->dma_address; 1336 txd->dst_addr = sgl->dma_address;
@@ -1416,6 +1340,15 @@ static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
1416 return NULL; 1340 return NULL;
1417 } 1341 }
1418 1342
1343 if (plchan->cd->device_fc)
1344 tmp = (direction == DMA_TO_DEVICE) ? PL080_FLOW_MEM2PER_PER :
1345 PL080_FLOW_PER2MEM_PER;
1346 else
1347 tmp = (direction == DMA_TO_DEVICE) ? PL080_FLOW_MEM2PER :
1348 PL080_FLOW_PER2MEM;
1349
1350 txd->ccfg |= tmp << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1351
1419 ret = pl08x_prep_channel_resources(plchan, txd); 1352 ret = pl08x_prep_channel_resources(plchan, txd);
1420 if (ret) 1353 if (ret)
1421 return NULL; 1354 return NULL;
@@ -1489,9 +1422,15 @@ static int pl08x_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1489 1422
1490bool pl08x_filter_id(struct dma_chan *chan, void *chan_id) 1423bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
1491{ 1424{
1492 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan); 1425 struct pl08x_dma_chan *plchan;
1493 char *name = chan_id; 1426 char *name = chan_id;
1494 1427
1428 /* Reject channels for devices not bound to this driver */
1429 if (chan->device->dev->driver != &pl08x_amba_driver.drv)
1430 return false;
1431
1432 plchan = to_pl08x_chan(chan);
1433
1495 /* Check that the channel is not taken! */ 1434 /* Check that the channel is not taken! */
1496 if (!strcmp(plchan->name, name)) 1435 if (!strcmp(plchan->name, name))
1497 return true; 1436 return true;
@@ -1507,13 +1446,7 @@ bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
1507 */ 1446 */
1508static void pl08x_ensure_on(struct pl08x_driver_data *pl08x) 1447static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
1509{ 1448{
1510 u32 val; 1449 writel(PL080_CONFIG_ENABLE, pl08x->base + PL080_CONFIG);
1511
1512 val = readl(pl08x->base + PL080_CONFIG);
1513 val &= ~(PL080_CONFIG_M2_BE | PL080_CONFIG_M1_BE | PL080_CONFIG_ENABLE);
1514 /* We implicitly clear bit 1 and that means little-endian mode */
1515 val |= PL080_CONFIG_ENABLE;
1516 writel(val, pl08x->base + PL080_CONFIG);
1517} 1450}
1518 1451
1519static void pl08x_unmap_buffers(struct pl08x_txd *txd) 1452static void pl08x_unmap_buffers(struct pl08x_txd *txd)
@@ -1589,8 +1522,8 @@ static void pl08x_tasklet(unsigned long data)
1589 */ 1522 */
1590 list_for_each_entry(waiting, &pl08x->memcpy.channels, 1523 list_for_each_entry(waiting, &pl08x->memcpy.channels,
1591 chan.device_node) { 1524 chan.device_node) {
1592 if (waiting->state == PL08X_CHAN_WAITING && 1525 if (waiting->state == PL08X_CHAN_WAITING &&
1593 waiting->waiting != NULL) { 1526 waiting->waiting != NULL) {
1594 int ret; 1527 int ret;
1595 1528
1596 /* This should REALLY not fail now */ 1529 /* This should REALLY not fail now */
@@ -1630,38 +1563,40 @@ static void pl08x_tasklet(unsigned long data)
1630static irqreturn_t pl08x_irq(int irq, void *dev) 1563static irqreturn_t pl08x_irq(int irq, void *dev)
1631{ 1564{
1632 struct pl08x_driver_data *pl08x = dev; 1565 struct pl08x_driver_data *pl08x = dev;
1633 u32 mask = 0; 1566 u32 mask = 0, err, tc, i;
1634 u32 val; 1567
1635 int i; 1568 /* check & clear - ERR & TC interrupts */
1636 1569 err = readl(pl08x->base + PL080_ERR_STATUS);
1637 val = readl(pl08x->base + PL080_ERR_STATUS); 1570 if (err) {
1638 if (val) { 1571 dev_err(&pl08x->adev->dev, "%s error interrupt, register value 0x%08x\n",
1639 /* An error interrupt (on one or more channels) */ 1572 __func__, err);
1640 dev_err(&pl08x->adev->dev, 1573 writel(err, pl08x->base + PL080_ERR_CLEAR);
1641 "%s error interrupt, register value 0x%08x\n",
1642 __func__, val);
1643 /*
1644 * Simply clear ALL PL08X error interrupts,
1645 * regardless of channel and cause
1646 * FIXME: should be 0x00000003 on PL081 really.
1647 */
1648 writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
1649 } 1574 }
1650 val = readl(pl08x->base + PL080_INT_STATUS); 1575 tc = readl(pl08x->base + PL080_INT_STATUS);
1576 if (tc)
1577 writel(tc, pl08x->base + PL080_TC_CLEAR);
1578
1579 if (!err && !tc)
1580 return IRQ_NONE;
1581
1651 for (i = 0; i < pl08x->vd->channels; i++) { 1582 for (i = 0; i < pl08x->vd->channels; i++) {
1652 if ((1 << i) & val) { 1583 if (((1 << i) & err) || ((1 << i) & tc)) {
1653 /* Locate physical channel */ 1584 /* Locate physical channel */
1654 struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i]; 1585 struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
1655 struct pl08x_dma_chan *plchan = phychan->serving; 1586 struct pl08x_dma_chan *plchan = phychan->serving;
1656 1587
1588 if (!plchan) {
1589 dev_err(&pl08x->adev->dev,
1590 "%s Error TC interrupt on unused channel: 0x%08x\n",
1591 __func__, i);
1592 continue;
1593 }
1594
1657 /* Schedule tasklet on this channel */ 1595 /* Schedule tasklet on this channel */
1658 tasklet_schedule(&plchan->tasklet); 1596 tasklet_schedule(&plchan->tasklet);
1659
1660 mask |= (1 << i); 1597 mask |= (1 << i);
1661 } 1598 }
1662 } 1599 }
1663 /* Clear only the terminal interrupts on channels we processed */
1664 writel(mask, pl08x->base + PL080_TC_CLEAR);
1665 1600
1666 return mask ? IRQ_HANDLED : IRQ_NONE; 1601 return mask ? IRQ_HANDLED : IRQ_NONE;
1667} 1602}
@@ -1685,9 +1620,7 @@ static void pl08x_dma_slave_init(struct pl08x_dma_chan *chan)
1685 * Make a local wrapper to hold required data 1620 * Make a local wrapper to hold required data
1686 */ 1621 */
1687static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x, 1622static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
1688 struct dma_device *dmadev, 1623 struct dma_device *dmadev, unsigned int channels, bool slave)
1689 unsigned int channels,
1690 bool slave)
1691{ 1624{
1692 struct pl08x_dma_chan *chan; 1625 struct pl08x_dma_chan *chan;
1693 int i; 1626 int i;
@@ -1700,7 +1633,7 @@ static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
1700 * to cope with that situation. 1633 * to cope with that situation.
1701 */ 1634 */
1702 for (i = 0; i < channels; i++) { 1635 for (i = 0; i < channels; i++) {
1703 chan = kzalloc(sizeof(struct pl08x_dma_chan), GFP_KERNEL); 1636 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
1704 if (!chan) { 1637 if (!chan) {
1705 dev_err(&pl08x->adev->dev, 1638 dev_err(&pl08x->adev->dev,
1706 "%s no memory for channel\n", __func__); 1639 "%s no memory for channel\n", __func__);
@@ -1728,7 +1661,7 @@ static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
1728 kfree(chan); 1661 kfree(chan);
1729 continue; 1662 continue;
1730 } 1663 }
1731 dev_info(&pl08x->adev->dev, 1664 dev_dbg(&pl08x->adev->dev,
1732 "initialize virtual channel \"%s\"\n", 1665 "initialize virtual channel \"%s\"\n",
1733 chan->name); 1666 chan->name);
1734 1667
@@ -1837,9 +1770,9 @@ static const struct file_operations pl08x_debugfs_operations = {
1837static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x) 1770static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
1838{ 1771{
1839 /* Expose a simple debugfs interface to view all clocks */ 1772 /* Expose a simple debugfs interface to view all clocks */
1840 (void) debugfs_create_file(dev_name(&pl08x->adev->dev), S_IFREG | S_IRUGO, 1773 (void) debugfs_create_file(dev_name(&pl08x->adev->dev),
1841 NULL, pl08x, 1774 S_IFREG | S_IRUGO, NULL, pl08x,
1842 &pl08x_debugfs_operations); 1775 &pl08x_debugfs_operations);
1843} 1776}
1844 1777
1845#else 1778#else
@@ -1860,12 +1793,15 @@ static int pl08x_probe(struct amba_device *adev, const struct amba_id *id)
1860 return ret; 1793 return ret;
1861 1794
1862 /* Create the driver state holder */ 1795 /* Create the driver state holder */
1863 pl08x = kzalloc(sizeof(struct pl08x_driver_data), GFP_KERNEL); 1796 pl08x = kzalloc(sizeof(*pl08x), GFP_KERNEL);
1864 if (!pl08x) { 1797 if (!pl08x) {
1865 ret = -ENOMEM; 1798 ret = -ENOMEM;
1866 goto out_no_pl08x; 1799 goto out_no_pl08x;
1867 } 1800 }
1868 1801
1802 pm_runtime_set_active(&adev->dev);
1803 pm_runtime_enable(&adev->dev);
1804
1869 /* Initialize memcpy engine */ 1805 /* Initialize memcpy engine */
1870 dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask); 1806 dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
1871 pl08x->memcpy.dev = &adev->dev; 1807 pl08x->memcpy.dev = &adev->dev;
@@ -1939,7 +1875,7 @@ static int pl08x_probe(struct amba_device *adev, const struct amba_id *id)
1939 } 1875 }
1940 1876
1941 /* Initialize physical channels */ 1877 /* Initialize physical channels */
1942 pl08x->phy_chans = kmalloc((vd->channels * sizeof(struct pl08x_phy_chan)), 1878 pl08x->phy_chans = kmalloc((vd->channels * sizeof(*pl08x->phy_chans)),
1943 GFP_KERNEL); 1879 GFP_KERNEL);
1944 if (!pl08x->phy_chans) { 1880 if (!pl08x->phy_chans) {
1945 dev_err(&adev->dev, "%s failed to allocate " 1881 dev_err(&adev->dev, "%s failed to allocate "
@@ -1956,9 +1892,8 @@ static int pl08x_probe(struct amba_device *adev, const struct amba_id *id)
1956 spin_lock_init(&ch->lock); 1892 spin_lock_init(&ch->lock);
1957 ch->serving = NULL; 1893 ch->serving = NULL;
1958 ch->signal = -1; 1894 ch->signal = -1;
1959 dev_info(&adev->dev, 1895 dev_dbg(&adev->dev, "physical channel %d is %s\n",
1960 "physical channel %d is %s\n", i, 1896 i, pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
1961 pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
1962 } 1897 }
1963 1898
1964 /* Register as many memcpy channels as there are physical channels */ 1899 /* Register as many memcpy channels as there are physical channels */
@@ -1974,8 +1909,7 @@ static int pl08x_probe(struct amba_device *adev, const struct amba_id *id)
1974 1909
1975 /* Register slave channels */ 1910 /* Register slave channels */
1976 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave, 1911 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
1977 pl08x->pd->num_slave_channels, 1912 pl08x->pd->num_slave_channels, true);
1978 true);
1979 if (ret <= 0) { 1913 if (ret <= 0) {
1980 dev_warn(&pl08x->adev->dev, 1914 dev_warn(&pl08x->adev->dev,
1981 "%s failed to enumerate slave channels - %d\n", 1915 "%s failed to enumerate slave channels - %d\n",
@@ -2005,6 +1939,8 @@ static int pl08x_probe(struct amba_device *adev, const struct amba_id *id)
2005 dev_info(&pl08x->adev->dev, "DMA: PL%03x rev%u at 0x%08llx irq %d\n", 1939 dev_info(&pl08x->adev->dev, "DMA: PL%03x rev%u at 0x%08llx irq %d\n",
2006 amba_part(adev), amba_rev(adev), 1940 amba_part(adev), amba_rev(adev),
2007 (unsigned long long)adev->res.start, adev->irq[0]); 1941 (unsigned long long)adev->res.start, adev->irq[0]);
1942
1943 pm_runtime_put(&adev->dev);
2008 return 0; 1944 return 0;
2009 1945
2010out_no_slave_reg: 1946out_no_slave_reg:
@@ -2023,6 +1959,9 @@ out_no_ioremap:
2023 dma_pool_destroy(pl08x->pool); 1959 dma_pool_destroy(pl08x->pool);
2024out_no_lli_pool: 1960out_no_lli_pool:
2025out_no_platdata: 1961out_no_platdata:
1962 pm_runtime_put(&adev->dev);
1963 pm_runtime_disable(&adev->dev);
1964
2026 kfree(pl08x); 1965 kfree(pl08x);
2027out_no_pl08x: 1966out_no_pl08x:
2028 amba_release_regions(adev); 1967 amba_release_regions(adev);