2 * Copyright 2012 Marvell International Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/types.h>
13 #include <linux/interrupt.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/slab.h>
16 #include <linux/dmaengine.h>
17 #include <linux/platform_device.h>
18 #include <linux/device.h>
19 #include <linux/platform_data/mmp_dma.h>
20 #include <linux/dmapool.h>
21 #include <linux/of_device.h>
22 #include <linux/of_dma.h>
24 #include <linux/dma/mmp-pdma.h>
26 #include "dmaengine.h"
32 #define DSADR(n) (0x0204 + ((n) << 4))
33 #define DTADR(n) (0x0208 + ((n) << 4))
36 #define DCSR_RUN BIT(31) /* Run Bit (read / write) */
37 #define DCSR_NODESC BIT(30) /* No-Descriptor Fetch (read / write) */
38 #define DCSR_STOPIRQEN BIT(29) /* Stop Interrupt Enable (read / write) */
39 #define DCSR_REQPEND BIT(8) /* Request Pending (read-only) */
40 #define DCSR_STOPSTATE BIT(3) /* Stop State (read-only) */
41 #define DCSR_ENDINTR BIT(2) /* End Interrupt (read / write) */
42 #define DCSR_STARTINTR BIT(1) /* Start Interrupt (read / write) */
43 #define DCSR_BUSERR BIT(0) /* Bus Error Interrupt (read / write) */
45 #define DCSR_EORIRQEN BIT(28) /* End of Receive Interrupt Enable (R/W) */
46 #define DCSR_EORJMPEN BIT(27) /* Jump to next descriptor on EOR */
47 #define DCSR_EORSTOPEN BIT(26) /* STOP on an EOR */
48 #define DCSR_SETCMPST BIT(25) /* Set Descriptor Compare Status */
49 #define DCSR_CLRCMPST BIT(24) /* Clear Descriptor Compare Status */
50 #define DCSR_CMPST BIT(10) /* The Descriptor Compare Status */
51 #define DCSR_EORINTR BIT(9) /* The end of Receive */
53 #define DRCMR(n) ((((n) < 64) ? 0x0100 : 0x1100) + (((n) & 0x3f) << 2))
54 #define DRCMR_MAPVLD BIT(7) /* Map Valid (read / write) */
55 #define DRCMR_CHLNUM 0x1f /* mask for Channel Number (read / write) */
57 #define DDADR_DESCADDR 0xfffffff0 /* Address of next descriptor (mask) */
58 #define DDADR_STOP BIT(0) /* Stop (read / write) */
60 #define DCMD_INCSRCADDR BIT(31) /* Source Address Increment Setting. */
61 #define DCMD_INCTRGADDR BIT(30) /* Target Address Increment Setting. */
62 #define DCMD_FLOWSRC BIT(29) /* Flow Control by the source. */
63 #define DCMD_FLOWTRG BIT(28) /* Flow Control by the target. */
64 #define DCMD_STARTIRQEN BIT(22) /* Start Interrupt Enable */
65 #define DCMD_ENDIRQEN BIT(21) /* End Interrupt Enable */
66 #define DCMD_ENDIAN BIT(18) /* Device Endian-ness. */
67 #define DCMD_BURST8 (1 << 16) /* 8 byte burst */
68 #define DCMD_BURST16 (2 << 16) /* 16 byte burst */
69 #define DCMD_BURST32 (3 << 16) /* 32 byte burst */
70 #define DCMD_WIDTH1 (1 << 14) /* 1 byte width */
71 #define DCMD_WIDTH2 (2 << 14) /* 2 byte width (HalfWord) */
72 #define DCMD_WIDTH4 (3 << 14) /* 4 byte width (Word) */
73 #define DCMD_LENGTH 0x01fff /* length mask (max = 8K - 1) */
75 #define PDMA_ALIGNMENT 3
76 #define PDMA_MAX_DESC_BYTES DCMD_LENGTH
78 struct mmp_pdma_desc_hw {
79 u32 ddadr; /* Points to the next descriptor + flags */
80 u32 dsadr; /* DSADR value for the current transfer */
81 u32 dtadr; /* DTADR value for the current transfer */
82 u32 dcmd; /* DCMD value for the current transfer */
85 struct mmp_pdma_desc_sw {
86 struct mmp_pdma_desc_hw desc;
87 struct list_head node;
88 struct list_head tx_list;
89 struct dma_async_tx_descriptor async_tx;
94 struct mmp_pdma_chan {
97 struct dma_async_tx_descriptor desc;
98 struct mmp_pdma_phy *phy;
99 enum dma_transfer_direction dir;
101 struct mmp_pdma_desc_sw *cyclic_first; /* first desc_sw if channel
102 * is in cyclic mode */
104 /* channel's basic info */
105 struct tasklet_struct tasklet;
111 spinlock_t desc_lock; /* Descriptor list lock */
112 struct list_head chain_pending; /* Link descriptors queue for pending */
113 struct list_head chain_running; /* Link descriptors queue for running */
114 bool idle; /* channel statue machine */
117 struct dma_pool *desc_pool; /* Descriptors pool */
120 struct mmp_pdma_phy {
123 struct mmp_pdma_chan *vchan;
126 struct mmp_pdma_device {
130 struct dma_device device;
131 struct mmp_pdma_phy *phy;
132 spinlock_t phy_lock; /* protect alloc/free phy channels */
135 #define tx_to_mmp_pdma_desc(tx) \
136 container_of(tx, struct mmp_pdma_desc_sw, async_tx)
137 #define to_mmp_pdma_desc(lh) \
138 container_of(lh, struct mmp_pdma_desc_sw, node)
139 #define to_mmp_pdma_chan(dchan) \
140 container_of(dchan, struct mmp_pdma_chan, chan)
141 #define to_mmp_pdma_dev(dmadev) \
142 container_of(dmadev, struct mmp_pdma_device, device)
144 static void set_desc(struct mmp_pdma_phy *phy, dma_addr_t addr)
146 u32 reg = (phy->idx << 4) + DDADR;
148 writel(addr, phy->base + reg);
151 static void enable_chan(struct mmp_pdma_phy *phy)
158 reg = DRCMR(phy->vchan->drcmr);
159 writel(DRCMR_MAPVLD | phy->idx, phy->base + reg);
161 dalgn = readl(phy->base + DALGN);
162 if (phy->vchan->byte_align)
163 dalgn |= 1 << phy->idx;
165 dalgn &= ~(1 << phy->idx);
166 writel(dalgn, phy->base + DALGN);
168 reg = (phy->idx << 2) + DCSR;
169 writel(readl(phy->base + reg) | DCSR_RUN, phy->base + reg);
172 static void disable_chan(struct mmp_pdma_phy *phy)
179 reg = (phy->idx << 2) + DCSR;
180 writel(readl(phy->base + reg) & ~DCSR_RUN, phy->base + reg);
183 static int clear_chan_irq(struct mmp_pdma_phy *phy)
186 u32 dint = readl(phy->base + DINT);
187 u32 reg = (phy->idx << 2) + DCSR;
189 if (!(dint & BIT(phy->idx)))
193 dcsr = readl(phy->base + reg);
194 writel(dcsr, phy->base + reg);
195 if ((dcsr & DCSR_BUSERR) && (phy->vchan))
196 dev_warn(phy->vchan->dev, "DCSR_BUSERR\n");
201 static irqreturn_t mmp_pdma_chan_handler(int irq, void *dev_id)
203 struct mmp_pdma_phy *phy = dev_id;
205 if (clear_chan_irq(phy) != 0)
208 tasklet_schedule(&phy->vchan->tasklet);
212 static irqreturn_t mmp_pdma_int_handler(int irq, void *dev_id)
214 struct mmp_pdma_device *pdev = dev_id;
215 struct mmp_pdma_phy *phy;
216 u32 dint = readl(pdev->base + DINT);
224 ret = mmp_pdma_chan_handler(irq, phy);
225 if (ret == IRQ_HANDLED)
235 /* lookup free phy channel as descending priority */
236 static struct mmp_pdma_phy *lookup_phy(struct mmp_pdma_chan *pchan)
239 struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
240 struct mmp_pdma_phy *phy, *found = NULL;
244 * dma channel priorities
245 * ch 0 - 3, 16 - 19 <--> (0)
246 * ch 4 - 7, 20 - 23 <--> (1)
247 * ch 8 - 11, 24 - 27 <--> (2)
248 * ch 12 - 15, 28 - 31 <--> (3)
251 spin_lock_irqsave(&pdev->phy_lock, flags);
252 for (prio = 0; prio <= ((pdev->dma_channels - 1) & 0xf) >> 2; prio++) {
253 for (i = 0; i < pdev->dma_channels; i++) {
254 if (prio != (i & 0xf) >> 2)
266 spin_unlock_irqrestore(&pdev->phy_lock, flags);
270 static void mmp_pdma_free_phy(struct mmp_pdma_chan *pchan)
272 struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
279 /* clear the channel mapping in DRCMR */
280 reg = DRCMR(pchan->drcmr);
281 writel(0, pchan->phy->base + reg);
283 spin_lock_irqsave(&pdev->phy_lock, flags);
284 pchan->phy->vchan = NULL;
286 spin_unlock_irqrestore(&pdev->phy_lock, flags);
290 * start_pending_queue - transfer any pending transactions
291 * pending list ==> running list
293 static void start_pending_queue(struct mmp_pdma_chan *chan)
295 struct mmp_pdma_desc_sw *desc;
297 /* still in running, irq will start the pending list */
299 dev_dbg(chan->dev, "DMA controller still busy\n");
303 if (list_empty(&chan->chain_pending)) {
304 /* chance to re-fetch phy channel with higher prio */
305 mmp_pdma_free_phy(chan);
306 dev_dbg(chan->dev, "no pending list\n");
311 chan->phy = lookup_phy(chan);
313 dev_dbg(chan->dev, "no free dma channel\n");
320 * reintilize pending list
322 desc = list_first_entry(&chan->chain_pending,
323 struct mmp_pdma_desc_sw, node);
324 list_splice_tail_init(&chan->chain_pending, &chan->chain_running);
327 * Program the descriptor's address into the DMA controller,
328 * then start the DMA transaction
330 set_desc(chan->phy, desc->async_tx.phys);
331 enable_chan(chan->phy);
336 /* desc->tx_list ==> pending list */
337 static dma_cookie_t mmp_pdma_tx_submit(struct dma_async_tx_descriptor *tx)
339 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(tx->chan);
340 struct mmp_pdma_desc_sw *desc = tx_to_mmp_pdma_desc(tx);
341 struct mmp_pdma_desc_sw *child;
343 dma_cookie_t cookie = -EBUSY;
345 spin_lock_irqsave(&chan->desc_lock, flags);
347 list_for_each_entry(child, &desc->tx_list, node) {
348 cookie = dma_cookie_assign(&child->async_tx);
351 /* softly link to pending list - desc->tx_list ==> pending list */
352 list_splice_tail_init(&desc->tx_list, &chan->chain_pending);
354 spin_unlock_irqrestore(&chan->desc_lock, flags);
359 static struct mmp_pdma_desc_sw *
360 mmp_pdma_alloc_descriptor(struct mmp_pdma_chan *chan)
362 struct mmp_pdma_desc_sw *desc;
365 desc = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
367 dev_err(chan->dev, "out of memory for link descriptor\n");
371 memset(desc, 0, sizeof(*desc));
372 INIT_LIST_HEAD(&desc->tx_list);
373 dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan);
374 /* each desc has submit */
375 desc->async_tx.tx_submit = mmp_pdma_tx_submit;
376 desc->async_tx.phys = pdesc;
382 * mmp_pdma_alloc_chan_resources - Allocate resources for DMA channel.
384 * This function will create a dma pool for descriptor allocation.
385 * Request irq only when channel is requested
386 * Return - The number of allocated descriptors.
389 static int mmp_pdma_alloc_chan_resources(struct dma_chan *dchan)
391 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
396 chan->desc_pool = dma_pool_create(dev_name(&dchan->dev->device),
398 sizeof(struct mmp_pdma_desc_sw),
399 __alignof__(struct mmp_pdma_desc_sw),
401 if (!chan->desc_pool) {
402 dev_err(chan->dev, "unable to allocate descriptor pool\n");
406 mmp_pdma_free_phy(chan);
412 static void mmp_pdma_free_desc_list(struct mmp_pdma_chan *chan,
413 struct list_head *list)
415 struct mmp_pdma_desc_sw *desc, *_desc;
417 list_for_each_entry_safe(desc, _desc, list, node) {
418 list_del(&desc->node);
419 dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
423 static void mmp_pdma_free_chan_resources(struct dma_chan *dchan)
425 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
428 spin_lock_irqsave(&chan->desc_lock, flags);
429 mmp_pdma_free_desc_list(chan, &chan->chain_pending);
430 mmp_pdma_free_desc_list(chan, &chan->chain_running);
431 spin_unlock_irqrestore(&chan->desc_lock, flags);
433 dma_pool_destroy(chan->desc_pool);
434 chan->desc_pool = NULL;
437 mmp_pdma_free_phy(chan);
441 static struct dma_async_tx_descriptor *
442 mmp_pdma_prep_memcpy(struct dma_chan *dchan,
443 dma_addr_t dma_dst, dma_addr_t dma_src,
444 size_t len, unsigned long flags)
446 struct mmp_pdma_chan *chan;
447 struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
456 chan = to_mmp_pdma_chan(dchan);
457 chan->byte_align = false;
460 chan->dir = DMA_MEM_TO_MEM;
461 chan->dcmd = DCMD_INCTRGADDR | DCMD_INCSRCADDR;
462 chan->dcmd |= DCMD_BURST32;
466 /* Allocate the link descriptor from DMA pool */
467 new = mmp_pdma_alloc_descriptor(chan);
469 dev_err(chan->dev, "no memory for desc\n");
473 copy = min_t(size_t, len, PDMA_MAX_DESC_BYTES);
474 if (dma_src & 0x7 || dma_dst & 0x7)
475 chan->byte_align = true;
477 new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & copy);
478 new->desc.dsadr = dma_src;
479 new->desc.dtadr = dma_dst;
484 prev->desc.ddadr = new->async_tx.phys;
486 new->async_tx.cookie = 0;
487 async_tx_ack(&new->async_tx);
492 if (chan->dir == DMA_MEM_TO_DEV) {
494 } else if (chan->dir == DMA_DEV_TO_MEM) {
496 } else if (chan->dir == DMA_MEM_TO_MEM) {
501 /* Insert the link descriptor to the LD ring */
502 list_add_tail(&new->node, &first->tx_list);
505 first->async_tx.flags = flags; /* client is in control of this ack */
506 first->async_tx.cookie = -EBUSY;
508 /* last desc and fire IRQ */
509 new->desc.ddadr = DDADR_STOP;
510 new->desc.dcmd |= DCMD_ENDIRQEN;
512 chan->cyclic_first = NULL;
514 return &first->async_tx;
518 mmp_pdma_free_desc_list(chan, &first->tx_list);
522 static struct dma_async_tx_descriptor *
523 mmp_pdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
524 unsigned int sg_len, enum dma_transfer_direction dir,
525 unsigned long flags, void *context)
527 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
528 struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new = NULL;
530 struct scatterlist *sg;
534 if ((sgl == NULL) || (sg_len == 0))
537 chan->byte_align = false;
539 for_each_sg(sgl, sg, sg_len, i) {
540 addr = sg_dma_address(sg);
541 avail = sg_dma_len(sgl);
544 len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES);
546 chan->byte_align = true;
548 /* allocate and populate the descriptor */
549 new = mmp_pdma_alloc_descriptor(chan);
551 dev_err(chan->dev, "no memory for desc\n");
555 new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & len);
556 if (dir == DMA_MEM_TO_DEV) {
557 new->desc.dsadr = addr;
558 new->desc.dtadr = chan->dev_addr;
560 new->desc.dsadr = chan->dev_addr;
561 new->desc.dtadr = addr;
567 prev->desc.ddadr = new->async_tx.phys;
569 new->async_tx.cookie = 0;
570 async_tx_ack(&new->async_tx);
573 /* Insert the link descriptor to the LD ring */
574 list_add_tail(&new->node, &first->tx_list);
576 /* update metadata */
582 first->async_tx.cookie = -EBUSY;
583 first->async_tx.flags = flags;
585 /* last desc and fire IRQ */
586 new->desc.ddadr = DDADR_STOP;
587 new->desc.dcmd |= DCMD_ENDIRQEN;
590 chan->cyclic_first = NULL;
592 return &first->async_tx;
596 mmp_pdma_free_desc_list(chan, &first->tx_list);
600 static struct dma_async_tx_descriptor *
601 mmp_pdma_prep_dma_cyclic(struct dma_chan *dchan,
602 dma_addr_t buf_addr, size_t len, size_t period_len,
603 enum dma_transfer_direction direction,
606 struct mmp_pdma_chan *chan;
607 struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
608 dma_addr_t dma_src, dma_dst;
610 if (!dchan || !len || !period_len)
613 /* the buffer length must be a multiple of period_len */
614 if (len % period_len != 0)
617 if (period_len > PDMA_MAX_DESC_BYTES)
620 chan = to_mmp_pdma_chan(dchan);
625 dma_dst = chan->dev_addr;
629 dma_src = chan->dev_addr;
632 dev_err(chan->dev, "Unsupported direction for cyclic DMA\n");
636 chan->dir = direction;
639 /* Allocate the link descriptor from DMA pool */
640 new = mmp_pdma_alloc_descriptor(chan);
642 dev_err(chan->dev, "no memory for desc\n");
646 new->desc.dcmd = (chan->dcmd | DCMD_ENDIRQEN |
647 (DCMD_LENGTH & period_len));
648 new->desc.dsadr = dma_src;
649 new->desc.dtadr = dma_dst;
654 prev->desc.ddadr = new->async_tx.phys;
656 new->async_tx.cookie = 0;
657 async_tx_ack(&new->async_tx);
662 if (chan->dir == DMA_MEM_TO_DEV)
663 dma_src += period_len;
665 dma_dst += period_len;
667 /* Insert the link descriptor to the LD ring */
668 list_add_tail(&new->node, &first->tx_list);
671 first->async_tx.flags = flags; /* client is in control of this ack */
672 first->async_tx.cookie = -EBUSY;
674 /* make the cyclic link */
675 new->desc.ddadr = first->async_tx.phys;
676 chan->cyclic_first = first;
678 return &first->async_tx;
682 mmp_pdma_free_desc_list(chan, &first->tx_list);
686 static int mmp_pdma_control(struct dma_chan *dchan, enum dma_ctrl_cmd cmd,
689 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
690 struct dma_slave_config *cfg = (void *)arg;
692 u32 maxburst = 0, addr = 0;
693 enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
699 case DMA_TERMINATE_ALL:
700 disable_chan(chan->phy);
701 mmp_pdma_free_phy(chan);
702 spin_lock_irqsave(&chan->desc_lock, flags);
703 mmp_pdma_free_desc_list(chan, &chan->chain_pending);
704 mmp_pdma_free_desc_list(chan, &chan->chain_running);
705 spin_unlock_irqrestore(&chan->desc_lock, flags);
708 case DMA_SLAVE_CONFIG:
709 if (cfg->direction == DMA_DEV_TO_MEM) {
710 chan->dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC;
711 maxburst = cfg->src_maxburst;
712 width = cfg->src_addr_width;
713 addr = cfg->src_addr;
714 } else if (cfg->direction == DMA_MEM_TO_DEV) {
715 chan->dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG;
716 maxburst = cfg->dst_maxburst;
717 width = cfg->dst_addr_width;
718 addr = cfg->dst_addr;
721 if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
722 chan->dcmd |= DCMD_WIDTH1;
723 else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
724 chan->dcmd |= DCMD_WIDTH2;
725 else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES)
726 chan->dcmd |= DCMD_WIDTH4;
729 chan->dcmd |= DCMD_BURST8;
730 else if (maxburst == 16)
731 chan->dcmd |= DCMD_BURST16;
732 else if (maxburst == 32)
733 chan->dcmd |= DCMD_BURST32;
735 chan->dir = cfg->direction;
736 chan->dev_addr = addr;
737 /* FIXME: drivers should be ported over to use the filter
738 * function. Once that's done, the following two lines can
742 chan->drcmr = cfg->slave_id;
751 static unsigned int mmp_pdma_residue(struct mmp_pdma_chan *chan,
754 struct mmp_pdma_desc_sw *sw;
755 u32 curr, residue = 0;
757 bool cyclic = chan->cyclic_first != NULL;
760 * If the channel does not have a phy pointer anymore, it has already
761 * been completed. Therefore, its residue is 0.
766 if (chan->dir == DMA_DEV_TO_MEM)
767 curr = readl(chan->phy->base + DTADR(chan->phy->idx));
769 curr = readl(chan->phy->base + DSADR(chan->phy->idx));
771 list_for_each_entry(sw, &chan->chain_running, node) {
774 if (chan->dir == DMA_DEV_TO_MEM)
775 start = sw->desc.dtadr;
777 start = sw->desc.dsadr;
779 len = sw->desc.dcmd & DCMD_LENGTH;
783 * 'passed' will be latched once we found the descriptor which
784 * lies inside the boundaries of the curr pointer. All
785 * descriptors that occur in the list _after_ we found that
786 * partially handled descriptor are still to be processed and
787 * are hence added to the residual bytes counter.
792 } else if (curr >= start && curr <= end) {
793 residue += end - curr;
798 * Descriptors that have the ENDIRQEN bit set mark the end of a
799 * transaction chain, and the cookie assigned with it has been
800 * returned previously from mmp_pdma_tx_submit().
802 * In case we have multiple transactions in the running chain,
803 * and the cookie does not match the one the user asked us
804 * about, reset the state variables and start over.
806 * This logic does not apply to cyclic transactions, where all
807 * descriptors have the ENDIRQEN bit set, and for which we
808 * can't have multiple transactions on one channel anyway.
810 if (cyclic || !(sw->desc.dcmd & DCMD_ENDIRQEN))
813 if (sw->async_tx.cookie == cookie) {
821 /* We should only get here in case of cyclic transactions */
825 static enum dma_status mmp_pdma_tx_status(struct dma_chan *dchan,
827 struct dma_tx_state *txstate)
829 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
832 ret = dma_cookie_status(dchan, cookie, txstate);
833 if (likely(ret != DMA_ERROR))
834 dma_set_residue(txstate, mmp_pdma_residue(chan, cookie));
840 * mmp_pdma_issue_pending - Issue the DMA start command
841 * pending list ==> running list
843 static void mmp_pdma_issue_pending(struct dma_chan *dchan)
845 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
848 spin_lock_irqsave(&chan->desc_lock, flags);
849 start_pending_queue(chan);
850 spin_unlock_irqrestore(&chan->desc_lock, flags);
858 static void dma_do_tasklet(unsigned long data)
860 struct mmp_pdma_chan *chan = (struct mmp_pdma_chan *)data;
861 struct mmp_pdma_desc_sw *desc, *_desc;
862 LIST_HEAD(chain_cleanup);
865 if (chan->cyclic_first) {
866 dma_async_tx_callback cb = NULL;
867 void *cb_data = NULL;
869 spin_lock_irqsave(&chan->desc_lock, flags);
870 desc = chan->cyclic_first;
871 cb = desc->async_tx.callback;
872 cb_data = desc->async_tx.callback_param;
873 spin_unlock_irqrestore(&chan->desc_lock, flags);
881 /* submit pending list; callback for each desc; free desc */
882 spin_lock_irqsave(&chan->desc_lock, flags);
884 list_for_each_entry_safe(desc, _desc, &chan->chain_running, node) {
886 * move the descriptors to a temporary list so we can drop
887 * the lock during the entire cleanup operation
889 list_move(&desc->node, &chain_cleanup);
892 * Look for the first list entry which has the ENDIRQEN flag
893 * set. That is the descriptor we got an interrupt for, so
894 * complete that transaction and its cookie.
896 if (desc->desc.dcmd & DCMD_ENDIRQEN) {
897 dma_cookie_t cookie = desc->async_tx.cookie;
898 dma_cookie_complete(&desc->async_tx);
899 dev_dbg(chan->dev, "completed_cookie=%d\n", cookie);
905 * The hardware is idle and ready for more when the
906 * chain_running list is empty.
908 chan->idle = list_empty(&chan->chain_running);
910 /* Start any pending transactions automatically */
911 start_pending_queue(chan);
912 spin_unlock_irqrestore(&chan->desc_lock, flags);
914 /* Run the callback for each descriptor, in order */
915 list_for_each_entry_safe(desc, _desc, &chain_cleanup, node) {
916 struct dma_async_tx_descriptor *txd = &desc->async_tx;
918 /* Remove from the list of transactions */
919 list_del(&desc->node);
920 /* Run the link descriptor callback function */
922 txd->callback(txd->callback_param);
924 dma_pool_free(chan->desc_pool, desc, txd->phys);
928 static int mmp_pdma_remove(struct platform_device *op)
930 struct mmp_pdma_device *pdev = platform_get_drvdata(op);
932 dma_async_device_unregister(&pdev->device);
936 static int mmp_pdma_chan_init(struct mmp_pdma_device *pdev, int idx, int irq)
938 struct mmp_pdma_phy *phy = &pdev->phy[idx];
939 struct mmp_pdma_chan *chan;
942 chan = devm_kzalloc(pdev->dev, sizeof(*chan), GFP_KERNEL);
947 phy->base = pdev->base;
950 ret = devm_request_irq(pdev->dev, irq, mmp_pdma_chan_handler,
951 IRQF_SHARED, "pdma", phy);
953 dev_err(pdev->dev, "channel request irq fail!\n");
958 spin_lock_init(&chan->desc_lock);
959 chan->dev = pdev->dev;
960 chan->chan.device = &pdev->device;
961 tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
962 INIT_LIST_HEAD(&chan->chain_pending);
963 INIT_LIST_HEAD(&chan->chain_running);
965 /* register virt channel to dma engine */
966 list_add_tail(&chan->chan.device_node, &pdev->device.channels);
971 static struct of_device_id mmp_pdma_dt_ids[] = {
972 { .compatible = "marvell,pdma-1.0", },
975 MODULE_DEVICE_TABLE(of, mmp_pdma_dt_ids);
977 static struct dma_chan *mmp_pdma_dma_xlate(struct of_phandle_args *dma_spec,
978 struct of_dma *ofdma)
980 struct mmp_pdma_device *d = ofdma->of_dma_data;
981 struct dma_chan *chan;
983 chan = dma_get_any_slave_channel(&d->device);
987 to_mmp_pdma_chan(chan)->drcmr = dma_spec->args[0];
992 static int mmp_pdma_probe(struct platform_device *op)
994 struct mmp_pdma_device *pdev;
995 const struct of_device_id *of_id;
996 struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
997 struct resource *iores;
999 int dma_channels = 0, irq_num = 0;
1001 pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
1005 pdev->dev = &op->dev;
1007 spin_lock_init(&pdev->phy_lock);
1009 iores = platform_get_resource(op, IORESOURCE_MEM, 0);
1010 pdev->base = devm_ioremap_resource(pdev->dev, iores);
1011 if (IS_ERR(pdev->base))
1012 return PTR_ERR(pdev->base);
1014 of_id = of_match_device(mmp_pdma_dt_ids, pdev->dev);
1016 of_property_read_u32(pdev->dev->of_node, "#dma-channels",
1018 else if (pdata && pdata->dma_channels)
1019 dma_channels = pdata->dma_channels;
1021 dma_channels = 32; /* default 32 channel */
1022 pdev->dma_channels = dma_channels;
1024 for (i = 0; i < dma_channels; i++) {
1025 if (platform_get_irq(op, i) > 0)
1029 pdev->phy = devm_kcalloc(pdev->dev, dma_channels, sizeof(*pdev->phy),
1031 if (pdev->phy == NULL)
1034 INIT_LIST_HEAD(&pdev->device.channels);
1036 if (irq_num != dma_channels) {
1037 /* all chan share one irq, demux inside */
1038 irq = platform_get_irq(op, 0);
1039 ret = devm_request_irq(pdev->dev, irq, mmp_pdma_int_handler,
1040 IRQF_SHARED, "pdma", pdev);
1045 for (i = 0; i < dma_channels; i++) {
1046 irq = (irq_num != dma_channels) ? 0 : platform_get_irq(op, i);
1047 ret = mmp_pdma_chan_init(pdev, i, irq);
1052 dma_cap_set(DMA_SLAVE, pdev->device.cap_mask);
1053 dma_cap_set(DMA_MEMCPY, pdev->device.cap_mask);
1054 dma_cap_set(DMA_CYCLIC, pdev->device.cap_mask);
1055 dma_cap_set(DMA_PRIVATE, pdev->device.cap_mask);
1056 pdev->device.dev = &op->dev;
1057 pdev->device.device_alloc_chan_resources = mmp_pdma_alloc_chan_resources;
1058 pdev->device.device_free_chan_resources = mmp_pdma_free_chan_resources;
1059 pdev->device.device_tx_status = mmp_pdma_tx_status;
1060 pdev->device.device_prep_dma_memcpy = mmp_pdma_prep_memcpy;
1061 pdev->device.device_prep_slave_sg = mmp_pdma_prep_slave_sg;
1062 pdev->device.device_prep_dma_cyclic = mmp_pdma_prep_dma_cyclic;
1063 pdev->device.device_issue_pending = mmp_pdma_issue_pending;
1064 pdev->device.device_control = mmp_pdma_control;
1065 pdev->device.copy_align = PDMA_ALIGNMENT;
1067 if (pdev->dev->coherent_dma_mask)
1068 dma_set_mask(pdev->dev, pdev->dev->coherent_dma_mask);
1070 dma_set_mask(pdev->dev, DMA_BIT_MASK(64));
1072 ret = dma_async_device_register(&pdev->device);
1074 dev_err(pdev->device.dev, "unable to register\n");
1078 if (op->dev.of_node) {
1079 /* Device-tree DMA controller registration */
1080 ret = of_dma_controller_register(op->dev.of_node,
1081 mmp_pdma_dma_xlate, pdev);
1083 dev_err(&op->dev, "of_dma_controller_register failed\n");
1088 platform_set_drvdata(op, pdev);
1089 dev_info(pdev->device.dev, "initialized %d channels\n", dma_channels);
1093 static const struct platform_device_id mmp_pdma_id_table[] = {
1098 static struct platform_driver mmp_pdma_driver = {
1101 .owner = THIS_MODULE,
1102 .of_match_table = mmp_pdma_dt_ids,
1104 .id_table = mmp_pdma_id_table,
1105 .probe = mmp_pdma_probe,
1106 .remove = mmp_pdma_remove,
1109 bool mmp_pdma_filter_fn(struct dma_chan *chan, void *param)
1111 struct mmp_pdma_chan *c = to_mmp_pdma_chan(chan);
1113 if (chan->device->dev->driver != &mmp_pdma_driver.driver)
1116 c->drcmr = *(unsigned int *)param;
1120 EXPORT_SYMBOL_GPL(mmp_pdma_filter_fn);
1122 module_platform_driver(mmp_pdma_driver);
1124 MODULE_DESCRIPTION("MARVELL MMP Peripheral DMA Driver");
1125 MODULE_AUTHOR("Marvell International Ltd.");
1126 MODULE_LICENSE("GPL v2");