2 * Copyright (c) 2006 ARM Ltd.
3 * Copyright (c) 2010 ST-Ericsson SA
5 * Author: Peter Pearse <peter.pearse@arm.com>
6 * Author: Linus Walleij <linus.walleij@stericsson.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 59
20 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * The full GNU General Public License is in this distribution in the
23 * file called COPYING.
25 * Documentation: ARM DDI 0196G == PL080
26 * Documentation: ARM DDI 0218E == PL081
28 * PL080 & PL081 both have 16 sets of DMA signals that can be routed to
31 * The PL080 has 8 channels available for simultaneous use, and the PL081
32 * has only two channels. So on these DMA controllers the number of channels
33 * and the number of incoming DMA signals are two totally different things.
34 * It is usually not possible to theoretically handle all physical signals,
35 * so a multiplexing scheme with possible denial of use is necessary.
37 * The PL080 has a dual bus master, PL081 has a single master.
39 * Memory to peripheral transfer may be visualized as
40 * Get data from memory to DMAC
42 * On burst request from peripheral
43 * Destination burst from DMAC to peripheral
45 * Raise terminal count interrupt
47 * For peripherals with a FIFO:
48 * Source burst size == half the depth of the peripheral FIFO
49 * Destination burst size == the depth of the peripheral FIFO
51 * (Bursts are irrelevant for mem to mem transfers - there are no burst
52 * signals, the DMA controller will simply facilitate its AHB master.)
54 * ASSUMES default (little) endianness for DMA transfers
56 * The PL08x has two flow control settings:
57 * - DMAC flow control: the transfer size defines the number of transfers
58 * which occur for the current LLI entry, and the DMAC raises TC at the
59 * end of every LLI entry. Observed behaviour shows the DMAC listening
60 * to both the BREQ and SREQ signals (contrary to documented),
61 * transferring data if either is active. The LBREQ and LSREQ signals
64 * - Peripheral flow control: the transfer size is ignored (and should be
65 * zero). The data is transferred from the current LLI entry, until
66 * after the final transfer signalled by LBREQ or LSREQ. The DMAC
67 * will then move to the next LLI entry.
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.
75 * - Break out common code from arch/arm/mach-s3c64xx and share
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/dmapool.h>
83 #include <linux/dmaengine.h>
84 #include <linux/amba/bus.h>
85 #include <linux/amba/pl08x.h>
86 #include <linux/debugfs.h>
87 #include <linux/seq_file.h>
89 #include <asm/hardware/pl080.h>
91 #define DRIVER_NAME "pl08xdmac"
94 * struct vendor_data - vendor-specific config parameters
95 * for PL08x derivatives
96 * @channels: the number of channels available in this variant
97 * @dualmaster: whether this version supports dual AHB masters
106 * PL08X private data structures
107 * An LLI struct - see PL08x TRM. Note that next uses bit[0] as a bus bit,
108 * start & end do not - their bus bit info is in cctl. Also note that these
109 * are fixed 32-bit quantities.
119 * struct pl08x_driver_data - the local state holder for the PL08x
120 * @slave: slave engine for this instance
121 * @memcpy: memcpy engine for this instance
122 * @base: virtual memory base (remapped) for the PL08x
123 * @adev: the corresponding AMBA (PrimeCell) bus entry
124 * @vd: vendor data for this PL08x variant
125 * @pd: platform data passed in from the platform/machine
126 * @phy_chans: array of data for the physical channels
127 * @pool: a pool for the LLI descriptors
128 * @pool_ctr: counter of LLIs in the pool
129 * @lock: a spinlock for this struct
131 struct pl08x_driver_data {
132 struct dma_device slave;
133 struct dma_device memcpy;
135 struct amba_device *adev;
136 const struct vendor_data *vd;
137 struct pl08x_platform_data *pd;
138 struct pl08x_phy_chan *phy_chans;
139 struct dma_pool *pool;
145 * PL08X specific defines
149 * Memory boundaries: the manual for PL08x says that the controller
150 * cannot read past a 1KiB boundary, so these defines are used to
151 * create transfer LLIs that do not cross such boundaries.
153 #define PL08X_BOUNDARY_SHIFT (10) /* 1KB 0x400 */
154 #define PL08X_BOUNDARY_SIZE (1 << PL08X_BOUNDARY_SHIFT)
156 /* Minimum period between work queue runs */
157 #define PL08X_WQ_PERIODMIN 20
159 /* Size (bytes) of each LLI buffer allocated for one transfer */
160 # define PL08X_LLI_TSFR_SIZE 0x2000
162 /* Maximum times we call dma_pool_alloc on this pool without freeing */
163 #define PL08X_MAX_ALLOCS 0x40
164 #define MAX_NUM_TSFR_LLIS (PL08X_LLI_TSFR_SIZE/sizeof(struct pl08x_lli))
165 #define PL08X_ALIGN 8
167 static inline struct pl08x_dma_chan *to_pl08x_chan(struct dma_chan *chan)
169 return container_of(chan, struct pl08x_dma_chan, chan);
173 * Physical channel handling
176 /* Whether a certain channel is busy or not */
177 static int pl08x_phy_channel_busy(struct pl08x_phy_chan *ch)
181 val = readl(ch->base + PL080_CH_CONFIG);
182 return val & PL080_CONFIG_ACTIVE;
186 * Set the initial DMA register values i.e. those for the first LLI
187 * The next LLI pointer and the configuration interrupt bit have
188 * been set when the LLIs were constructed. Poke them into the hardware
189 * and start the transfer.
191 static void pl08x_start_txd(struct pl08x_dma_chan *plchan,
192 struct pl08x_txd *txd)
194 struct pl08x_driver_data *pl08x = plchan->host;
195 struct pl08x_phy_chan *phychan = plchan->phychan;
196 struct pl08x_lli *lli = &txd->llis_va[0];
201 /* Wait for channel inactive */
202 while (pl08x_phy_channel_busy(phychan))
205 dev_vdbg(&pl08x->adev->dev,
206 "WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
207 "clli=0x%08x, cctl=0x%08x, ccfg=0x%08x\n",
208 phychan->id, lli->src, lli->dst, lli->lli, lli->cctl,
211 writel(lli->src, phychan->base + PL080_CH_SRC_ADDR);
212 writel(lli->dst, phychan->base + PL080_CH_DST_ADDR);
213 writel(lli->lli, phychan->base + PL080_CH_LLI);
214 writel(lli->cctl, phychan->base + PL080_CH_CONTROL);
215 writel(txd->ccfg, phychan->base + PL080_CH_CONFIG);
217 /* Enable the DMA channel */
218 /* Do not access config register until channel shows as disabled */
219 while (readl(pl08x->base + PL080_EN_CHAN) & (1 << phychan->id))
222 /* Do not access config register until channel shows as inactive */
223 val = readl(phychan->base + PL080_CH_CONFIG);
224 while ((val & PL080_CONFIG_ACTIVE) || (val & PL080_CONFIG_ENABLE))
225 val = readl(phychan->base + PL080_CH_CONFIG);
227 writel(val | PL080_CONFIG_ENABLE, phychan->base + PL080_CH_CONFIG);
231 * Overall DMAC remains enabled always.
233 * Disabling individual channels could lose data.
235 * Disable the peripheral DMA after disabling the DMAC
236 * in order to allow the DMAC FIFO to drain, and
237 * hence allow the channel to show inactive
240 static void pl08x_pause_phy_chan(struct pl08x_phy_chan *ch)
244 /* Set the HALT bit and wait for the FIFO to drain */
245 val = readl(ch->base + PL080_CH_CONFIG);
246 val |= PL080_CONFIG_HALT;
247 writel(val, ch->base + PL080_CH_CONFIG);
249 /* Wait for channel inactive */
250 while (pl08x_phy_channel_busy(ch))
254 static void pl08x_resume_phy_chan(struct pl08x_phy_chan *ch)
258 /* Clear the HALT bit */
259 val = readl(ch->base + PL080_CH_CONFIG);
260 val &= ~PL080_CONFIG_HALT;
261 writel(val, ch->base + PL080_CH_CONFIG);
265 /* Stops the channel */
266 static void pl08x_stop_phy_chan(struct pl08x_phy_chan *ch)
270 pl08x_pause_phy_chan(ch);
272 /* Disable channel */
273 val = readl(ch->base + PL080_CH_CONFIG);
274 val &= ~PL080_CONFIG_ENABLE;
275 val &= ~PL080_CONFIG_ERR_IRQ_MASK;
276 val &= ~PL080_CONFIG_TC_IRQ_MASK;
277 writel(val, ch->base + PL080_CH_CONFIG);
280 static inline u32 get_bytes_in_cctl(u32 cctl)
282 /* The source width defines the number of bytes */
283 u32 bytes = cctl & PL080_CONTROL_TRANSFER_SIZE_MASK;
285 switch (cctl >> PL080_CONTROL_SWIDTH_SHIFT) {
286 case PL080_WIDTH_8BIT:
288 case PL080_WIDTH_16BIT:
291 case PL080_WIDTH_32BIT:
298 /* The channel should be paused when calling this */
299 static u32 pl08x_getbytes_chan(struct pl08x_dma_chan *plchan)
301 struct pl08x_phy_chan *ch;
302 struct pl08x_txd *txd;
306 spin_lock_irqsave(&plchan->lock, flags);
307 ch = plchan->phychan;
311 * Follow the LLIs to get the number of remaining
312 * bytes in the currently active transaction.
315 u32 clli = readl(ch->base + PL080_CH_LLI) & ~PL080_LLI_LM_AHB2;
317 /* First get the remaining bytes in the active transfer */
318 bytes = get_bytes_in_cctl(readl(ch->base + PL080_CH_CONTROL));
321 struct pl08x_lli *llis_va = txd->llis_va;
322 dma_addr_t llis_bus = txd->llis_bus;
325 BUG_ON(clli < llis_bus || clli >= llis_bus +
326 sizeof(struct pl08x_lli) * MAX_NUM_TSFR_LLIS);
329 * Locate the next LLI - as this is an array,
330 * it's simple maths to find.
332 index = (clli - llis_bus) / sizeof(struct pl08x_lli);
334 for (; index < MAX_NUM_TSFR_LLIS; index++) {
335 bytes += get_bytes_in_cctl(llis_va[index].cctl);
338 * A LLI pointer of 0 terminates the LLI list
340 if (!llis_va[index].lli)
346 /* Sum up all queued transactions */
347 if (!list_empty(&plchan->desc_list)) {
348 struct pl08x_txd *txdi;
349 list_for_each_entry(txdi, &plchan->desc_list, node) {
354 spin_unlock_irqrestore(&plchan->lock, flags);
360 * Allocate a physical channel for a virtual channel
362 static struct pl08x_phy_chan *
363 pl08x_get_phy_channel(struct pl08x_driver_data *pl08x,
364 struct pl08x_dma_chan *virt_chan)
366 struct pl08x_phy_chan *ch = NULL;
371 * Try to locate a physical channel to be used for
372 * this transfer. If all are taken return NULL and
373 * the requester will have to cope by using some fallback
374 * PIO mode or retrying later.
376 for (i = 0; i < pl08x->vd->channels; i++) {
377 ch = &pl08x->phy_chans[i];
379 spin_lock_irqsave(&ch->lock, flags);
382 ch->serving = virt_chan;
384 spin_unlock_irqrestore(&ch->lock, flags);
388 spin_unlock_irqrestore(&ch->lock, flags);
391 if (i == pl08x->vd->channels) {
392 /* No physical channel available, cope with it */
399 static inline void pl08x_put_phy_channel(struct pl08x_driver_data *pl08x,
400 struct pl08x_phy_chan *ch)
404 /* Stop the channel and clear its interrupts */
405 pl08x_stop_phy_chan(ch);
406 writel((1 << ch->id), pl08x->base + PL080_ERR_CLEAR);
407 writel((1 << ch->id), pl08x->base + PL080_TC_CLEAR);
409 /* Mark it as free */
410 spin_lock_irqsave(&ch->lock, flags);
412 spin_unlock_irqrestore(&ch->lock, flags);
419 static inline unsigned int pl08x_get_bytes_for_cctl(unsigned int coded)
422 case PL080_WIDTH_8BIT:
424 case PL080_WIDTH_16BIT:
426 case PL080_WIDTH_32BIT:
435 static inline u32 pl08x_cctl_bits(u32 cctl, u8 srcwidth, u8 dstwidth,
440 /* Remove all src, dst and transfer size bits */
441 retbits &= ~PL080_CONTROL_DWIDTH_MASK;
442 retbits &= ~PL080_CONTROL_SWIDTH_MASK;
443 retbits &= ~PL080_CONTROL_TRANSFER_SIZE_MASK;
445 /* Then set the bits according to the parameters */
448 retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT;
451 retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT;
454 retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT;
463 retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT;
466 retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT;
469 retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT;
476 retbits |= tsize << PL080_CONTROL_TRANSFER_SIZE_SHIFT;
481 * Autoselect a master bus to use for the transfer
482 * this prefers the destination bus if both available
483 * if fixed address on one bus the other will be chosen
485 static void pl08x_choose_master_bus(struct pl08x_bus_data *src_bus,
486 struct pl08x_bus_data *dst_bus, struct pl08x_bus_data **mbus,
487 struct pl08x_bus_data **sbus, u32 cctl)
489 if (!(cctl & PL080_CONTROL_DST_INCR)) {
492 } else if (!(cctl & PL080_CONTROL_SRC_INCR)) {
496 if (dst_bus->buswidth == 4) {
499 } else if (src_bus->buswidth == 4) {
502 } else if (dst_bus->buswidth == 2) {
505 } else if (src_bus->buswidth == 2) {
509 /* src_bus->buswidth == 1 */
517 * Fills in one LLI for a certain transfer descriptor
518 * and advance the counter
520 static int pl08x_fill_lli_for_desc(struct pl08x_driver_data *pl08x,
521 struct pl08x_txd *txd, int num_llis, int len,
522 u32 cctl, u32 *remainder)
524 struct pl08x_lli *llis_va = txd->llis_va;
525 dma_addr_t llis_bus = txd->llis_bus;
527 BUG_ON(num_llis >= MAX_NUM_TSFR_LLIS);
529 llis_va[num_llis].cctl = cctl;
530 llis_va[num_llis].src = txd->srcbus.addr;
531 llis_va[num_llis].dst = txd->dstbus.addr;
534 * On versions with dual masters, you can optionally AND on
535 * PL080_LLI_LM_AHB2 to the LLI to tell the hardware to read
536 * in new LLIs with that controller, but we always try to
537 * choose AHB1 to point into memory. The idea is to have AHB2
538 * fixed on the peripheral and AHB1 messing around in the
539 * memory. So we don't manipulate this bit currently.
542 llis_va[num_llis].lli = llis_bus + (num_llis + 1) * sizeof(struct pl08x_lli);
544 if (cctl & PL080_CONTROL_SRC_INCR)
545 txd->srcbus.addr += len;
546 if (cctl & PL080_CONTROL_DST_INCR)
547 txd->dstbus.addr += len;
549 BUG_ON(*remainder < len);
557 * Return number of bytes to fill to boundary, or len
559 static inline size_t pl08x_pre_boundary(u32 addr, size_t len)
563 boundary = ((addr >> PL08X_BOUNDARY_SHIFT) + 1)
564 << PL08X_BOUNDARY_SHIFT;
566 if (boundary < addr + len)
567 return boundary - addr;
573 * This fills in the table of LLIs for the transfer descriptor
574 * Note that we assume we never have to change the burst sizes
577 static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
578 struct pl08x_txd *txd)
580 struct pl08x_bus_data *mbus, *sbus;
584 size_t max_bytes_per_lli;
585 size_t total_bytes = 0;
586 struct pl08x_lli *llis_va;
588 txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT,
591 dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
597 /* Get the default CCTL */
600 /* Find maximum width of the source bus */
601 txd->srcbus.maxwidth =
602 pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_SWIDTH_MASK) >>
603 PL080_CONTROL_SWIDTH_SHIFT);
605 /* Find maximum width of the destination bus */
606 txd->dstbus.maxwidth =
607 pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_DWIDTH_MASK) >>
608 PL080_CONTROL_DWIDTH_SHIFT);
610 /* Set up the bus widths to the maximum */
611 txd->srcbus.buswidth = txd->srcbus.maxwidth;
612 txd->dstbus.buswidth = txd->dstbus.maxwidth;
613 dev_vdbg(&pl08x->adev->dev,
614 "%s source bus is %d bytes wide, dest bus is %d bytes wide\n",
615 __func__, txd->srcbus.buswidth, txd->dstbus.buswidth);
619 * Bytes transferred == tsize * MIN(buswidths), not max(buswidths)
621 max_bytes_per_lli = min(txd->srcbus.buswidth, txd->dstbus.buswidth) *
622 PL080_CONTROL_TRANSFER_SIZE_MASK;
623 dev_vdbg(&pl08x->adev->dev,
624 "%s max bytes per lli = %zu\n",
625 __func__, max_bytes_per_lli);
627 /* We need to count this down to zero */
628 remainder = txd->len;
629 dev_vdbg(&pl08x->adev->dev,
630 "%s remainder = %zu\n",
631 __func__, remainder);
634 * Choose bus to align to
635 * - prefers destination bus if both available
636 * - if fixed address on one bus chooses other
637 * - modifies cctl to choose an appropriate master
639 pl08x_choose_master_bus(&txd->srcbus, &txd->dstbus,
644 * The lowest bit of the LLI register
645 * is also used to indicate which master to
646 * use for reading the LLIs.
649 if (txd->len < mbus->buswidth) {
651 * Less than a bus width available
652 * - send as single bytes
655 dev_vdbg(&pl08x->adev->dev,
656 "%s single byte LLIs for a transfer of "
657 "less than a bus width (remain 0x%08x)\n",
658 __func__, remainder);
659 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
661 pl08x_fill_lli_for_desc(pl08x, txd, num_llis, 1,
667 * Make one byte LLIs until master bus is aligned
668 * - slave will then be aligned also
670 while ((mbus->addr) % (mbus->buswidth)) {
671 dev_vdbg(&pl08x->adev->dev,
672 "%s adjustment lli for less than bus width "
674 __func__, remainder);
675 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
676 num_llis = pl08x_fill_lli_for_desc
677 (pl08x, txd, num_llis, 1, cctl, &remainder);
683 * - if slave is not then we must set its width down
685 if (sbus->addr % sbus->buswidth) {
686 dev_dbg(&pl08x->adev->dev,
687 "%s set down bus width to one byte\n",
694 * Make largest possible LLIs until less than one bus
697 while (remainder > (mbus->buswidth - 1)) {
698 size_t lli_len, target_len, tsize, odd_bytes;
701 * If enough left try to send max possible,
702 * otherwise try to send the remainder
704 target_len = remainder;
705 if (remainder > max_bytes_per_lli)
706 target_len = max_bytes_per_lli;
709 * Set bus lengths for incrementing buses
710 * to number of bytes which fill to next memory
713 if (cctl & PL080_CONTROL_SRC_INCR)
714 txd->srcbus.fill_bytes =
719 txd->srcbus.fill_bytes =
722 if (cctl & PL080_CONTROL_DST_INCR)
723 txd->dstbus.fill_bytes =
728 txd->dstbus.fill_bytes =
734 lli_len = min(txd->srcbus.fill_bytes,
735 txd->dstbus.fill_bytes);
737 BUG_ON(lli_len > remainder);
740 dev_err(&pl08x->adev->dev,
741 "%s lli_len is %zu, <= 0\n",
746 if (lli_len == target_len) {
748 * Can send what we wanted
753 lli_len = (lli_len/mbus->buswidth) *
758 * So now we know how many bytes to transfer
759 * to get to the nearest boundary
760 * The next LLI will past the boundary
761 * - however we may be working to a boundary
763 * We need to ensure the master stays aligned
765 odd_bytes = lli_len % mbus->buswidth;
767 * - and that we are working in multiples
770 lli_len -= odd_bytes;
776 * Check against minimum bus alignment:
777 * Calculate actual transfer size in relation
778 * to bus width an get a maximum remainder of
779 * the smallest bus width - 1
781 /* FIXME: use round_down()? */
782 tsize = lli_len / min(mbus->buswidth,
784 lli_len = tsize * min(mbus->buswidth,
787 if (target_len != lli_len) {
788 dev_vdbg(&pl08x->adev->dev,
789 "%s can't send what we want. Desired 0x%08zx, lli of 0x%08zx bytes in txd of 0x%08zx\n",
790 __func__, target_len, lli_len, txd->len);
793 cctl = pl08x_cctl_bits(cctl,
794 txd->srcbus.buswidth,
795 txd->dstbus.buswidth,
798 dev_vdbg(&pl08x->adev->dev,
799 "%s fill lli with single lli chunk of size 0x%08zx (remainder 0x%08zx)\n",
800 __func__, lli_len, remainder);
801 num_llis = pl08x_fill_lli_for_desc(pl08x, txd,
802 num_llis, lli_len, cctl,
804 total_bytes += lli_len;
810 * Creep past the boundary,
811 * maintaining master alignment
814 for (j = 0; (j < mbus->buswidth)
815 && (remainder); j++) {
816 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
817 dev_vdbg(&pl08x->adev->dev,
818 "%s align with boundary, single byte (remain 0x%08zx)\n",
819 __func__, remainder);
821 pl08x_fill_lli_for_desc(pl08x,
833 cctl = pl08x_cctl_bits(cctl, 1, 1, 1);
834 dev_vdbg(&pl08x->adev->dev,
835 "%s align with boundary, single odd byte (remain %zu)\n",
836 __func__, remainder);
837 num_llis = pl08x_fill_lli_for_desc(pl08x, txd, num_llis,
838 1, cctl, &remainder);
842 if (total_bytes != txd->len) {
843 dev_err(&pl08x->adev->dev,
844 "%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n",
845 __func__, total_bytes, txd->len);
849 if (num_llis >= MAX_NUM_TSFR_LLIS) {
850 dev_err(&pl08x->adev->dev,
851 "%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
852 __func__, (u32) MAX_NUM_TSFR_LLIS);
856 llis_va = txd->llis_va;
858 * The final LLI terminates the LLI.
860 llis_va[num_llis - 1].lli = 0;
862 * The final LLI element shall also fire an interrupt
864 llis_va[num_llis - 1].cctl |= PL080_CONTROL_TC_IRQ_EN;
870 for (i = 0; i < num_llis; i++) {
871 dev_vdbg(&pl08x->adev->dev,
872 "lli %d @%p: csrc=0x%08x, cdst=0x%08x, cctl=0x%08x, clli=0x%08x\n",
887 /* You should call this with the struct pl08x lock held */
888 static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
889 struct pl08x_txd *txd)
892 dma_pool_free(pl08x->pool, txd->llis_va, txd->llis_bus);
899 static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
900 struct pl08x_dma_chan *plchan)
902 struct pl08x_txd *txdi = NULL;
903 struct pl08x_txd *next;
905 if (!list_empty(&plchan->desc_list)) {
906 list_for_each_entry_safe(txdi,
907 next, &plchan->desc_list, node) {
908 list_del(&txdi->node);
909 pl08x_free_txd(pl08x, txdi);
918 static int pl08x_alloc_chan_resources(struct dma_chan *chan)
923 static void pl08x_free_chan_resources(struct dma_chan *chan)
928 * This should be called with the channel plchan->lock held
930 static int prep_phy_channel(struct pl08x_dma_chan *plchan,
931 struct pl08x_txd *txd)
933 struct pl08x_driver_data *pl08x = plchan->host;
934 struct pl08x_phy_chan *ch;
937 /* Check if we already have a channel */
941 ch = pl08x_get_phy_channel(pl08x, plchan);
943 /* No physical channel available, cope with it */
944 dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
949 * OK we have a physical channel: for memcpy() this is all we
950 * need, but for slaves the physical signals may be muxed!
951 * Can the platform allow us to use this channel?
955 pl08x->pd->get_signal) {
956 ret = pl08x->pd->get_signal(plchan);
958 dev_dbg(&pl08x->adev->dev,
959 "unable to use physical channel %d for transfer on %s due to platform restrictions\n",
960 ch->id, plchan->name);
961 /* Release physical channel & return */
962 pl08x_put_phy_channel(pl08x, ch);
967 /* Assign the flow control signal to this channel */
968 if (txd->direction == DMA_TO_DEVICE)
969 txd->ccfg |= ch->signal << PL080_CONFIG_DST_SEL_SHIFT;
970 else if (txd->direction == DMA_FROM_DEVICE)
971 txd->ccfg |= ch->signal << PL080_CONFIG_SRC_SEL_SHIFT;
974 dev_dbg(&pl08x->adev->dev, "allocated physical channel %d and signal %d for xfer on %s\n",
979 plchan->phychan = ch;
984 static void release_phy_channel(struct pl08x_dma_chan *plchan)
986 struct pl08x_driver_data *pl08x = plchan->host;
988 if ((plchan->phychan->signal >= 0) && pl08x->pd->put_signal) {
989 pl08x->pd->put_signal(plchan);
990 plchan->phychan->signal = -1;
992 pl08x_put_phy_channel(pl08x, plchan->phychan);
993 plchan->phychan = NULL;
996 static dma_cookie_t pl08x_tx_submit(struct dma_async_tx_descriptor *tx)
998 struct pl08x_dma_chan *plchan = to_pl08x_chan(tx->chan);
1000 plchan->chan.cookie += 1;
1001 if (plchan->chan.cookie < 0)
1002 plchan->chan.cookie = 1;
1003 tx->cookie = plchan->chan.cookie;
1004 /* This unlock follows the lock in the prep() function */
1005 spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
1010 static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
1011 struct dma_chan *chan, unsigned long flags)
1013 struct dma_async_tx_descriptor *retval = NULL;
1019 * Code accessing dma_async_is_complete() in a tight loop
1020 * may give problems - could schedule where indicated.
1021 * If slaves are relying on interrupts to signal completion this
1022 * function must not be called with interrupts disabled
1024 static enum dma_status
1025 pl08x_dma_tx_status(struct dma_chan *chan,
1026 dma_cookie_t cookie,
1027 struct dma_tx_state *txstate)
1029 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1030 dma_cookie_t last_used;
1031 dma_cookie_t last_complete;
1032 enum dma_status ret;
1035 last_used = plchan->chan.cookie;
1036 last_complete = plchan->lc;
1038 ret = dma_async_is_complete(cookie, last_complete, last_used);
1039 if (ret == DMA_SUCCESS) {
1040 dma_set_tx_state(txstate, last_complete, last_used, 0);
1045 * schedule(); could be inserted here
1049 * This cookie not complete yet
1051 last_used = plchan->chan.cookie;
1052 last_complete = plchan->lc;
1054 /* Get number of bytes left in the active transactions and queue */
1055 bytesleft = pl08x_getbytes_chan(plchan);
1057 dma_set_tx_state(txstate, last_complete, last_used,
1060 if (plchan->state == PL08X_CHAN_PAUSED)
1063 /* Whether waiting or running, we're in progress */
1064 return DMA_IN_PROGRESS;
1067 /* PrimeCell DMA extension */
1068 struct burst_table {
1073 static const struct burst_table burst_sizes[] = {
1076 .reg = (PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT) |
1077 (PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT),
1081 .reg = (PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT) |
1082 (PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT),
1086 .reg = (PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT) |
1087 (PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT),
1091 .reg = (PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT) |
1092 (PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT),
1096 .reg = (PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT) |
1097 (PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT),
1101 .reg = (PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT) |
1102 (PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT),
1106 .reg = (PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT) |
1107 (PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT),
1111 .reg = (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
1112 (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT),
1116 static void dma_set_runtime_config(struct dma_chan *chan,
1117 struct dma_slave_config *config)
1119 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1120 struct pl08x_driver_data *pl08x = plchan->host;
1121 struct pl08x_channel_data *cd = plchan->cd;
1122 enum dma_slave_buswidth addr_width;
1127 /* Transfer direction */
1128 plchan->runtime_direction = config->direction;
1129 if (config->direction == DMA_TO_DEVICE) {
1130 plchan->runtime_addr = config->dst_addr;
1131 addr_width = config->dst_addr_width;
1132 maxburst = config->dst_maxburst;
1133 } else if (config->direction == DMA_FROM_DEVICE) {
1134 plchan->runtime_addr = config->src_addr;
1135 addr_width = config->src_addr_width;
1136 maxburst = config->src_maxburst;
1138 dev_err(&pl08x->adev->dev,
1139 "bad runtime_config: alien transfer direction\n");
1143 switch (addr_width) {
1144 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1145 cctl |= (PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1146 (PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT);
1148 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1149 cctl |= (PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1150 (PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT);
1152 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1153 cctl |= (PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT) |
1154 (PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT);
1157 dev_err(&pl08x->adev->dev,
1158 "bad runtime_config: alien address width\n");
1163 * Now decide on a maxburst:
1164 * If this channel will only request single transfers, set this
1165 * down to ONE element. Also select one element if no maxburst
1168 if (plchan->cd->single || maxburst == 0) {
1169 cctl |= (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |
1170 (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT);
1172 for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
1173 if (burst_sizes[i].burstwords <= maxburst)
1175 cctl |= burst_sizes[i].reg;
1178 /* Modify the default channel data to fit PrimeCell request */
1181 dev_dbg(&pl08x->adev->dev,
1182 "configured channel %s (%s) for %s, data width %d, "
1183 "maxburst %d words, LE, CCTL=0x%08x\n",
1184 dma_chan_name(chan), plchan->name,
1185 (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
1192 * Slave transactions callback to the slave device to allow
1193 * synchronization of slave DMA signals with the DMAC enable
1195 static void pl08x_issue_pending(struct dma_chan *chan)
1197 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1198 unsigned long flags;
1200 spin_lock_irqsave(&plchan->lock, flags);
1201 /* Something is already active, or we're waiting for a channel... */
1202 if (plchan->at || plchan->state == PL08X_CHAN_WAITING) {
1203 spin_unlock_irqrestore(&plchan->lock, flags);
1207 /* Take the first element in the queue and execute it */
1208 if (!list_empty(&plchan->desc_list)) {
1209 struct pl08x_txd *next;
1211 next = list_first_entry(&plchan->desc_list,
1214 list_del(&next->node);
1215 plchan->state = PL08X_CHAN_RUNNING;
1217 pl08x_start_txd(plchan, next);
1220 spin_unlock_irqrestore(&plchan->lock, flags);
1223 static int pl08x_prep_channel_resources(struct pl08x_dma_chan *plchan,
1224 struct pl08x_txd *txd)
1227 struct pl08x_driver_data *pl08x = plchan->host;
1230 num_llis = pl08x_fill_llis_for_desc(pl08x, txd);
1236 spin_lock_irqsave(&plchan->lock, plchan->lockflags);
1238 list_add_tail(&txd->node, &plchan->desc_list);
1241 * See if we already have a physical channel allocated,
1242 * else this is the time to try to get one.
1244 ret = prep_phy_channel(plchan, txd);
1247 * No physical channel available, we will
1248 * stack up the memcpy channels until there is a channel
1249 * available to handle it whereas slave transfers may
1250 * have been denied due to platform channel muxing restrictions
1251 * and since there is no guarantee that this will ever be
1252 * resolved, and since the signal must be acquired AFTER
1253 * acquiring the physical channel, we will let them be NACK:ed
1254 * with -EBUSY here. The drivers can alway retry the prep()
1255 * call if they are eager on doing this using DMA.
1257 if (plchan->slave) {
1258 pl08x_free_txd_list(pl08x, plchan);
1259 spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);
1262 /* Do this memcpy whenever there is a channel ready */
1263 plchan->state = PL08X_CHAN_WAITING;
1264 plchan->waiting = txd;
1267 * Else we're all set, paused and ready to roll,
1268 * status will switch to PL08X_CHAN_RUNNING when
1269 * we call issue_pending(). If there is something
1270 * running on the channel already we don't change
1273 if (plchan->state == PL08X_CHAN_IDLE)
1274 plchan->state = PL08X_CHAN_PAUSED;
1277 * Notice that we leave plchan->lock locked on purpose:
1278 * it will be unlocked in the subsequent tx_submit()
1279 * call. This is a consequence of the current API.
1285 static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan)
1287 struct pl08x_txd *txd = kzalloc(sizeof(struct pl08x_txd), GFP_NOWAIT);
1290 dma_async_tx_descriptor_init(&txd->tx, &plchan->chan);
1291 txd->tx.tx_submit = pl08x_tx_submit;
1292 INIT_LIST_HEAD(&txd->node);
1294 /* Always enable error and terminal interrupts */
1295 txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
1296 PL080_CONFIG_TC_IRQ_MASK;
1302 * Initialize a descriptor to be used by memcpy submit
1304 static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
1305 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1306 size_t len, unsigned long flags)
1308 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1309 struct pl08x_driver_data *pl08x = plchan->host;
1310 struct pl08x_txd *txd;
1313 txd = pl08x_get_txd(plchan);
1315 dev_err(&pl08x->adev->dev,
1316 "%s no memory for descriptor\n", __func__);
1320 txd->direction = DMA_NONE;
1321 txd->srcbus.addr = src;
1322 txd->dstbus.addr = dest;
1325 /* Set platform data for m2m */
1326 txd->ccfg |= PL080_FLOW_MEM2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1327 txd->cctl = pl08x->pd->memcpy_channel.cctl &
1328 ~(PL080_CONTROL_DST_AHB2 | PL080_CONTROL_SRC_AHB2);
1330 /* Both to be incremented or the code will break */
1331 txd->cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
1334 * On the PL080 we have two bus masters and we should select one for
1335 * source and one for destination. We try to use AHB2 for the bus
1336 * which does not increment (typically the peripheral) else we just
1339 if (pl08x->vd->dualmaster)
1340 /* Source increments, use AHB2 for destination */
1341 txd->cctl |= PL080_CONTROL_DST_AHB2;
1343 ret = pl08x_prep_channel_resources(plchan, txd);
1347 * NB: the channel lock is held at this point so tx_submit()
1348 * must be called in direct succession.
1354 static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
1355 struct dma_chan *chan, struct scatterlist *sgl,
1356 unsigned int sg_len, enum dma_data_direction direction,
1357 unsigned long flags)
1359 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1360 struct pl08x_driver_data *pl08x = plchan->host;
1361 struct pl08x_txd *txd;
1365 * Current implementation ASSUMES only one sg
1368 dev_err(&pl08x->adev->dev, "%s prepared too long sglist\n",
1373 dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
1374 __func__, sgl->length, plchan->name);
1376 txd = pl08x_get_txd(plchan);
1378 dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
1382 if (direction != plchan->runtime_direction)
1383 dev_err(&pl08x->adev->dev, "%s DMA setup does not match "
1384 "the direction configured for the PrimeCell\n",
1388 * Set up addresses, the PrimeCell configured address
1389 * will take precedence since this may configure the
1390 * channel target address dynamically at runtime.
1392 txd->direction = direction;
1393 txd->len = sgl->length;
1395 txd->cctl = plchan->cd->cctl &
1396 ~(PL080_CONTROL_SRC_AHB2 | PL080_CONTROL_DST_AHB2 |
1397 PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR |
1398 PL080_CONTROL_PROT_MASK);
1400 /* Access the cell in privileged mode, non-bufferable, non-cacheable */
1401 txd->cctl |= PL080_CONTROL_PROT_SYS;
1403 if (direction == DMA_TO_DEVICE) {
1404 txd->ccfg |= PL080_FLOW_MEM2PER << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1405 txd->cctl |= PL080_CONTROL_SRC_INCR;
1406 if (pl08x->vd->dualmaster)
1407 /* Source increments, use AHB2 for destination */
1408 txd->cctl |= PL080_CONTROL_DST_AHB2;
1409 txd->srcbus.addr = sgl->dma_address;
1410 if (plchan->runtime_addr)
1411 txd->dstbus.addr = plchan->runtime_addr;
1413 txd->dstbus.addr = plchan->cd->addr;
1414 } else if (direction == DMA_FROM_DEVICE) {
1415 txd->ccfg |= PL080_FLOW_PER2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1416 txd->cctl |= PL080_CONTROL_DST_INCR;
1417 if (pl08x->vd->dualmaster)
1418 /* Destination increments, use AHB2 for source */
1419 txd->cctl |= PL080_CONTROL_SRC_AHB2;
1420 if (plchan->runtime_addr)
1421 txd->srcbus.addr = plchan->runtime_addr;
1423 txd->srcbus.addr = plchan->cd->addr;
1424 txd->dstbus.addr = sgl->dma_address;
1426 dev_err(&pl08x->adev->dev,
1427 "%s direction unsupported\n", __func__);
1431 ret = pl08x_prep_channel_resources(plchan, txd);
1435 * NB: the channel lock is held at this point so tx_submit()
1436 * must be called in direct succession.
1442 static int pl08x_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1445 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1446 struct pl08x_driver_data *pl08x = plchan->host;
1447 unsigned long flags;
1450 /* Controls applicable to inactive channels */
1451 if (cmd == DMA_SLAVE_CONFIG) {
1452 dma_set_runtime_config(chan,
1453 (struct dma_slave_config *)
1459 * Anything succeeds on channels with no physical allocation and
1460 * no queued transfers.
1462 spin_lock_irqsave(&plchan->lock, flags);
1463 if (!plchan->phychan && !plchan->at) {
1464 spin_unlock_irqrestore(&plchan->lock, flags);
1469 case DMA_TERMINATE_ALL:
1470 plchan->state = PL08X_CHAN_IDLE;
1472 if (plchan->phychan) {
1473 pl08x_stop_phy_chan(plchan->phychan);
1476 * Mark physical channel as free and free any slave
1479 release_phy_channel(plchan);
1481 /* Dequeue jobs and free LLIs */
1483 pl08x_free_txd(pl08x, plchan->at);
1486 /* Dequeue jobs not yet fired as well */
1487 pl08x_free_txd_list(pl08x, plchan);
1490 pl08x_pause_phy_chan(plchan->phychan);
1491 plchan->state = PL08X_CHAN_PAUSED;
1494 pl08x_resume_phy_chan(plchan->phychan);
1495 plchan->state = PL08X_CHAN_RUNNING;
1498 /* Unknown command */
1503 spin_unlock_irqrestore(&plchan->lock, flags);
1508 bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
1510 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1511 char *name = chan_id;
1513 /* Check that the channel is not taken! */
1514 if (!strcmp(plchan->name, name))
1521 * Just check that the device is there and active
1522 * TODO: turn this bit on/off depending on the number of
1523 * physical channels actually used, if it is zero... well
1524 * shut it off. That will save some power. Cut the clock
1527 static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
1531 val = readl(pl08x->base + PL080_CONFIG);
1532 val &= ~(PL080_CONFIG_M2_BE | PL080_CONFIG_M1_BE | PL080_CONFIG_ENABLE);
1533 /* We implicitly clear bit 1 and that means little-endian mode */
1534 val |= PL080_CONFIG_ENABLE;
1535 writel(val, pl08x->base + PL080_CONFIG);
1538 static void pl08x_tasklet(unsigned long data)
1540 struct pl08x_dma_chan *plchan = (struct pl08x_dma_chan *) data;
1541 struct pl08x_driver_data *pl08x = plchan->host;
1542 unsigned long flags;
1544 spin_lock_irqsave(&plchan->lock, flags);
1547 dma_async_tx_callback callback =
1548 plchan->at->tx.callback;
1549 void *callback_param =
1550 plchan->at->tx.callback_param;
1553 * Update last completed
1555 plchan->lc = plchan->at->tx.cookie;
1558 * Callback to signal completion
1561 callback(callback_param);
1564 * Free the descriptor
1566 pl08x_free_txd(pl08x, plchan->at);
1570 * If a new descriptor is queued, set it up
1571 * plchan->at is NULL here
1573 if (!list_empty(&plchan->desc_list)) {
1574 struct pl08x_txd *next;
1576 next = list_first_entry(&plchan->desc_list,
1579 list_del(&next->node);
1581 pl08x_start_txd(plchan, next);
1583 struct pl08x_dma_chan *waiting = NULL;
1586 * No more jobs, so free up the physical channel
1587 * Free any allocated signal on slave transfers too
1589 release_phy_channel(plchan);
1590 plchan->state = PL08X_CHAN_IDLE;
1593 * And NOW before anyone else can grab that free:d
1594 * up physical channel, see if there is some memcpy
1595 * pending that seriously needs to start because of
1596 * being stacked up while we were choking the
1597 * physical channels with data.
1599 list_for_each_entry(waiting, &pl08x->memcpy.channels,
1601 if (waiting->state == PL08X_CHAN_WAITING &&
1602 waiting->waiting != NULL) {
1605 /* This should REALLY not fail now */
1606 ret = prep_phy_channel(waiting,
1609 waiting->state = PL08X_CHAN_RUNNING;
1610 waiting->waiting = NULL;
1611 pl08x_issue_pending(&waiting->chan);
1617 spin_unlock_irqrestore(&plchan->lock, flags);
1620 static irqreturn_t pl08x_irq(int irq, void *dev)
1622 struct pl08x_driver_data *pl08x = dev;
1627 val = readl(pl08x->base + PL080_ERR_STATUS);
1630 * An error interrupt (on one or more channels)
1632 dev_err(&pl08x->adev->dev,
1633 "%s error interrupt, register value 0x%08x\n",
1636 * Simply clear ALL PL08X error interrupts,
1637 * regardless of channel and cause
1638 * FIXME: should be 0x00000003 on PL081 really.
1640 writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
1642 val = readl(pl08x->base + PL080_INT_STATUS);
1643 for (i = 0; i < pl08x->vd->channels; i++) {
1644 if ((1 << i) & val) {
1645 /* Locate physical channel */
1646 struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
1647 struct pl08x_dma_chan *plchan = phychan->serving;
1649 /* Schedule tasklet on this channel */
1650 tasklet_schedule(&plchan->tasklet);
1656 * Clear only the terminal interrupts on channels we processed
1658 writel(mask, pl08x->base + PL080_TC_CLEAR);
1660 return mask ? IRQ_HANDLED : IRQ_NONE;
1664 * Initialise the DMAC memcpy/slave channels.
1665 * Make a local wrapper to hold required data
1667 static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
1668 struct dma_device *dmadev,
1669 unsigned int channels,
1672 struct pl08x_dma_chan *chan;
1675 INIT_LIST_HEAD(&dmadev->channels);
1677 * Register as many many memcpy as we have physical channels,
1678 * we won't always be able to use all but the code will have
1679 * to cope with that situation.
1681 for (i = 0; i < channels; i++) {
1682 chan = kzalloc(sizeof(struct pl08x_dma_chan), GFP_KERNEL);
1684 dev_err(&pl08x->adev->dev,
1685 "%s no memory for channel\n", __func__);
1690 chan->state = PL08X_CHAN_IDLE;
1694 chan->name = pl08x->pd->slave_channels[i].bus_id;
1695 chan->cd = &pl08x->pd->slave_channels[i];
1697 chan->cd = &pl08x->pd->memcpy_channel;
1698 chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
1704 if (chan->cd->circular_buffer) {
1705 dev_err(&pl08x->adev->dev,
1706 "channel %s: circular buffers not supported\n",
1711 dev_info(&pl08x->adev->dev,
1712 "initialize virtual channel \"%s\"\n",
1715 chan->chan.device = dmadev;
1716 chan->chan.cookie = 0;
1719 spin_lock_init(&chan->lock);
1720 INIT_LIST_HEAD(&chan->desc_list);
1721 tasklet_init(&chan->tasklet, pl08x_tasklet,
1722 (unsigned long) chan);
1724 list_add_tail(&chan->chan.device_node, &dmadev->channels);
1726 dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
1727 i, slave ? "slave" : "memcpy");
1731 static void pl08x_free_virtual_channels(struct dma_device *dmadev)
1733 struct pl08x_dma_chan *chan = NULL;
1734 struct pl08x_dma_chan *next;
1736 list_for_each_entry_safe(chan,
1737 next, &dmadev->channels, chan.device_node) {
1738 list_del(&chan->chan.device_node);
1743 #ifdef CONFIG_DEBUG_FS
1744 static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
1747 case PL08X_CHAN_IDLE:
1749 case PL08X_CHAN_RUNNING:
1751 case PL08X_CHAN_PAUSED:
1753 case PL08X_CHAN_WAITING:
1758 return "UNKNOWN STATE";
1761 static int pl08x_debugfs_show(struct seq_file *s, void *data)
1763 struct pl08x_driver_data *pl08x = s->private;
1764 struct pl08x_dma_chan *chan;
1765 struct pl08x_phy_chan *ch;
1766 unsigned long flags;
1769 seq_printf(s, "PL08x physical channels:\n");
1770 seq_printf(s, "CHANNEL:\tUSER:\n");
1771 seq_printf(s, "--------\t-----\n");
1772 for (i = 0; i < pl08x->vd->channels; i++) {
1773 struct pl08x_dma_chan *virt_chan;
1775 ch = &pl08x->phy_chans[i];
1777 spin_lock_irqsave(&ch->lock, flags);
1778 virt_chan = ch->serving;
1780 seq_printf(s, "%d\t\t%s\n",
1781 ch->id, virt_chan ? virt_chan->name : "(none)");
1783 spin_unlock_irqrestore(&ch->lock, flags);
1786 seq_printf(s, "\nPL08x virtual memcpy channels:\n");
1787 seq_printf(s, "CHANNEL:\tSTATE:\n");
1788 seq_printf(s, "--------\t------\n");
1789 list_for_each_entry(chan, &pl08x->memcpy.channels, chan.device_node) {
1790 seq_printf(s, "%s\t\t%s\n", chan->name,
1791 pl08x_state_str(chan->state));
1794 seq_printf(s, "\nPL08x virtual slave channels:\n");
1795 seq_printf(s, "CHANNEL:\tSTATE:\n");
1796 seq_printf(s, "--------\t------\n");
1797 list_for_each_entry(chan, &pl08x->slave.channels, chan.device_node) {
1798 seq_printf(s, "%s\t\t%s\n", chan->name,
1799 pl08x_state_str(chan->state));
1805 static int pl08x_debugfs_open(struct inode *inode, struct file *file)
1807 return single_open(file, pl08x_debugfs_show, inode->i_private);
1810 static const struct file_operations pl08x_debugfs_operations = {
1811 .open = pl08x_debugfs_open,
1813 .llseek = seq_lseek,
1814 .release = single_release,
1817 static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
1819 /* Expose a simple debugfs interface to view all clocks */
1820 (void) debugfs_create_file(dev_name(&pl08x->adev->dev), S_IFREG | S_IRUGO,
1822 &pl08x_debugfs_operations);
1826 static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
1831 static int pl08x_probe(struct amba_device *adev, struct amba_id *id)
1833 struct pl08x_driver_data *pl08x;
1834 const struct vendor_data *vd = id->data;
1838 ret = amba_request_regions(adev, NULL);
1842 /* Create the driver state holder */
1843 pl08x = kzalloc(sizeof(struct pl08x_driver_data), GFP_KERNEL);
1849 /* Initialize memcpy engine */
1850 dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
1851 pl08x->memcpy.dev = &adev->dev;
1852 pl08x->memcpy.device_alloc_chan_resources = pl08x_alloc_chan_resources;
1853 pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
1854 pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
1855 pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
1856 pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
1857 pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
1858 pl08x->memcpy.device_control = pl08x_control;
1860 /* Initialize slave engine */
1861 dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
1862 pl08x->slave.dev = &adev->dev;
1863 pl08x->slave.device_alloc_chan_resources = pl08x_alloc_chan_resources;
1864 pl08x->slave.device_free_chan_resources = pl08x_free_chan_resources;
1865 pl08x->slave.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
1866 pl08x->slave.device_tx_status = pl08x_dma_tx_status;
1867 pl08x->slave.device_issue_pending = pl08x_issue_pending;
1868 pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
1869 pl08x->slave.device_control = pl08x_control;
1871 /* Get the platform data */
1872 pl08x->pd = dev_get_platdata(&adev->dev);
1874 dev_err(&adev->dev, "no platform data supplied\n");
1875 goto out_no_platdata;
1878 /* Assign useful pointers to the driver state */
1882 /* A DMA memory pool for LLIs, align on 1-byte boundary */
1883 pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
1884 PL08X_LLI_TSFR_SIZE, PL08X_ALIGN, 0);
1887 goto out_no_lli_pool;
1890 spin_lock_init(&pl08x->lock);
1892 pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
1895 goto out_no_ioremap;
1898 /* Turn on the PL08x */
1899 pl08x_ensure_on(pl08x);
1902 * Attach the interrupt handler
1904 writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
1905 writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
1907 ret = request_irq(adev->irq[0], pl08x_irq, IRQF_DISABLED,
1908 DRIVER_NAME, pl08x);
1910 dev_err(&adev->dev, "%s failed to request interrupt %d\n",
1911 __func__, adev->irq[0]);
1915 /* Initialize physical channels */
1916 pl08x->phy_chans = kmalloc((vd->channels * sizeof(struct pl08x_phy_chan)),
1918 if (!pl08x->phy_chans) {
1919 dev_err(&adev->dev, "%s failed to allocate "
1920 "physical channel holders\n",
1922 goto out_no_phychans;
1925 for (i = 0; i < vd->channels; i++) {
1926 struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
1929 ch->base = pl08x->base + PL080_Cx_BASE(i);
1930 spin_lock_init(&ch->lock);
1933 dev_info(&adev->dev,
1934 "physical channel %d is %s\n", i,
1935 pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
1938 /* Register as many memcpy channels as there are physical channels */
1939 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
1940 pl08x->vd->channels, false);
1942 dev_warn(&pl08x->adev->dev,
1943 "%s failed to enumerate memcpy channels - %d\n",
1947 pl08x->memcpy.chancnt = ret;
1949 /* Register slave channels */
1950 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
1951 pl08x->pd->num_slave_channels,
1954 dev_warn(&pl08x->adev->dev,
1955 "%s failed to enumerate slave channels - %d\n",
1959 pl08x->slave.chancnt = ret;
1961 ret = dma_async_device_register(&pl08x->memcpy);
1963 dev_warn(&pl08x->adev->dev,
1964 "%s failed to register memcpy as an async device - %d\n",
1966 goto out_no_memcpy_reg;
1969 ret = dma_async_device_register(&pl08x->slave);
1971 dev_warn(&pl08x->adev->dev,
1972 "%s failed to register slave as an async device - %d\n",
1974 goto out_no_slave_reg;
1977 amba_set_drvdata(adev, pl08x);
1978 init_pl08x_debugfs(pl08x);
1979 dev_info(&pl08x->adev->dev, "DMA: PL%03x rev%u at 0x%08llx irq %d\n",
1980 amba_part(adev), amba_rev(adev),
1981 (unsigned long long)adev->res.start, adev->irq[0]);
1985 dma_async_device_unregister(&pl08x->memcpy);
1987 pl08x_free_virtual_channels(&pl08x->slave);
1989 pl08x_free_virtual_channels(&pl08x->memcpy);
1991 kfree(pl08x->phy_chans);
1993 free_irq(adev->irq[0], pl08x);
1995 iounmap(pl08x->base);
1997 dma_pool_destroy(pl08x->pool);
2002 amba_release_regions(adev);
2006 /* PL080 has 8 channels and the PL080 have just 2 */
2007 static struct vendor_data vendor_pl080 = {
2012 static struct vendor_data vendor_pl081 = {
2014 .dualmaster = false,
2017 static struct amba_id pl08x_ids[] = {
2022 .data = &vendor_pl080,
2028 .data = &vendor_pl081,
2030 /* Nomadik 8815 PL080 variant */
2034 .data = &vendor_pl080,
2039 static struct amba_driver pl08x_amba_driver = {
2040 .drv.name = DRIVER_NAME,
2041 .id_table = pl08x_ids,
2042 .probe = pl08x_probe,
2045 static int __init pl08x_init(void)
2048 retval = amba_driver_register(&pl08x_amba_driver);
2050 printk(KERN_WARNING DRIVER_NAME
2051 "failed to register as an AMBA device (%d)\n",
2055 subsys_initcall(pl08x_init);