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_MAX_DESC_BYTES DCMD_LENGTH
77 struct mmp_pdma_desc_hw {
78 u32 ddadr; /* Points to the next descriptor + flags */
79 u32 dsadr; /* DSADR value for the current transfer */
80 u32 dtadr; /* DTADR value for the current transfer */
81 u32 dcmd; /* DCMD value for the current transfer */
84 struct mmp_pdma_desc_sw {
85 struct mmp_pdma_desc_hw desc;
86 struct list_head node;
87 struct list_head tx_list;
88 struct dma_async_tx_descriptor async_tx;
93 struct mmp_pdma_chan {
96 struct dma_async_tx_descriptor desc;
97 struct mmp_pdma_phy *phy;
98 enum dma_transfer_direction dir;
100 struct mmp_pdma_desc_sw *cyclic_first; /* first desc_sw if channel
101 * is in cyclic mode */
103 /* channel's basic info */
104 struct tasklet_struct tasklet;
110 spinlock_t desc_lock; /* Descriptor list lock */
111 struct list_head chain_pending; /* Link descriptors queue for pending */
112 struct list_head chain_running; /* Link descriptors queue for running */
113 bool idle; /* channel statue machine */
116 struct dma_pool *desc_pool; /* Descriptors pool */
119 struct mmp_pdma_phy {
122 struct mmp_pdma_chan *vchan;
125 struct mmp_pdma_device {
129 struct dma_device device;
130 struct mmp_pdma_phy *phy;
131 spinlock_t phy_lock; /* protect alloc/free phy channels */
134 #define tx_to_mmp_pdma_desc(tx) \
135 container_of(tx, struct mmp_pdma_desc_sw, async_tx)
136 #define to_mmp_pdma_desc(lh) \
137 container_of(lh, struct mmp_pdma_desc_sw, node)
138 #define to_mmp_pdma_chan(dchan) \
139 container_of(dchan, struct mmp_pdma_chan, chan)
140 #define to_mmp_pdma_dev(dmadev) \
141 container_of(dmadev, struct mmp_pdma_device, device)
143 static void set_desc(struct mmp_pdma_phy *phy, dma_addr_t addr)
145 u32 reg = (phy->idx << 4) + DDADR;
147 writel(addr, phy->base + reg);
150 static void enable_chan(struct mmp_pdma_phy *phy)
157 reg = DRCMR(phy->vchan->drcmr);
158 writel(DRCMR_MAPVLD | phy->idx, phy->base + reg);
160 dalgn = readl(phy->base + DALGN);
161 if (phy->vchan->byte_align)
162 dalgn |= 1 << phy->idx;
164 dalgn &= ~(1 << phy->idx);
165 writel(dalgn, phy->base + DALGN);
167 reg = (phy->idx << 2) + DCSR;
168 writel(readl(phy->base + reg) | DCSR_RUN, phy->base + reg);
171 static void disable_chan(struct mmp_pdma_phy *phy)
178 reg = (phy->idx << 2) + DCSR;
179 writel(readl(phy->base + reg) & ~DCSR_RUN, phy->base + reg);
182 static int clear_chan_irq(struct mmp_pdma_phy *phy)
185 u32 dint = readl(phy->base + DINT);
186 u32 reg = (phy->idx << 2) + DCSR;
188 if (!(dint & BIT(phy->idx)))
192 dcsr = readl(phy->base + reg);
193 writel(dcsr, phy->base + reg);
194 if ((dcsr & DCSR_BUSERR) && (phy->vchan))
195 dev_warn(phy->vchan->dev, "DCSR_BUSERR\n");
200 static irqreturn_t mmp_pdma_chan_handler(int irq, void *dev_id)
202 struct mmp_pdma_phy *phy = dev_id;
204 if (clear_chan_irq(phy) != 0)
207 tasklet_schedule(&phy->vchan->tasklet);
211 static irqreturn_t mmp_pdma_int_handler(int irq, void *dev_id)
213 struct mmp_pdma_device *pdev = dev_id;
214 struct mmp_pdma_phy *phy;
215 u32 dint = readl(pdev->base + DINT);
221 /* only handle interrupts belonging to pdma driver*/
222 if (i >= pdev->dma_channels)
226 ret = mmp_pdma_chan_handler(irq, phy);
227 if (ret == IRQ_HANDLED)
237 /* lookup free phy channel as descending priority */
238 static struct mmp_pdma_phy *lookup_phy(struct mmp_pdma_chan *pchan)
241 struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
242 struct mmp_pdma_phy *phy, *found = NULL;
246 * dma channel priorities
247 * ch 0 - 3, 16 - 19 <--> (0)
248 * ch 4 - 7, 20 - 23 <--> (1)
249 * ch 8 - 11, 24 - 27 <--> (2)
250 * ch 12 - 15, 28 - 31 <--> (3)
253 spin_lock_irqsave(&pdev->phy_lock, flags);
254 for (prio = 0; prio <= ((pdev->dma_channels - 1) & 0xf) >> 2; prio++) {
255 for (i = 0; i < pdev->dma_channels; i++) {
256 if (prio != (i & 0xf) >> 2)
268 spin_unlock_irqrestore(&pdev->phy_lock, flags);
272 static void mmp_pdma_free_phy(struct mmp_pdma_chan *pchan)
274 struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
281 /* clear the channel mapping in DRCMR */
282 reg = DRCMR(pchan->drcmr);
283 writel(0, pchan->phy->base + reg);
285 spin_lock_irqsave(&pdev->phy_lock, flags);
286 pchan->phy->vchan = NULL;
288 spin_unlock_irqrestore(&pdev->phy_lock, flags);
292 * start_pending_queue - transfer any pending transactions
293 * pending list ==> running list
295 static void start_pending_queue(struct mmp_pdma_chan *chan)
297 struct mmp_pdma_desc_sw *desc;
299 /* still in running, irq will start the pending list */
301 dev_dbg(chan->dev, "DMA controller still busy\n");
305 if (list_empty(&chan->chain_pending)) {
306 /* chance to re-fetch phy channel with higher prio */
307 mmp_pdma_free_phy(chan);
308 dev_dbg(chan->dev, "no pending list\n");
313 chan->phy = lookup_phy(chan);
315 dev_dbg(chan->dev, "no free dma channel\n");
322 * reintilize pending list
324 desc = list_first_entry(&chan->chain_pending,
325 struct mmp_pdma_desc_sw, node);
326 list_splice_tail_init(&chan->chain_pending, &chan->chain_running);
329 * Program the descriptor's address into the DMA controller,
330 * then start the DMA transaction
332 set_desc(chan->phy, desc->async_tx.phys);
333 enable_chan(chan->phy);
338 /* desc->tx_list ==> pending list */
339 static dma_cookie_t mmp_pdma_tx_submit(struct dma_async_tx_descriptor *tx)
341 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(tx->chan);
342 struct mmp_pdma_desc_sw *desc = tx_to_mmp_pdma_desc(tx);
343 struct mmp_pdma_desc_sw *child;
345 dma_cookie_t cookie = -EBUSY;
347 spin_lock_irqsave(&chan->desc_lock, flags);
349 list_for_each_entry(child, &desc->tx_list, node) {
350 cookie = dma_cookie_assign(&child->async_tx);
353 /* softly link to pending list - desc->tx_list ==> pending list */
354 list_splice_tail_init(&desc->tx_list, &chan->chain_pending);
356 spin_unlock_irqrestore(&chan->desc_lock, flags);
361 static struct mmp_pdma_desc_sw *
362 mmp_pdma_alloc_descriptor(struct mmp_pdma_chan *chan)
364 struct mmp_pdma_desc_sw *desc;
367 desc = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
369 dev_err(chan->dev, "out of memory for link descriptor\n");
373 INIT_LIST_HEAD(&desc->tx_list);
374 dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan);
375 /* each desc has submit */
376 desc->async_tx.tx_submit = mmp_pdma_tx_submit;
377 desc->async_tx.phys = pdesc;
383 * mmp_pdma_alloc_chan_resources - Allocate resources for DMA channel.
385 * This function will create a dma pool for descriptor allocation.
386 * Request irq only when channel is requested
387 * Return - The number of allocated descriptors.
390 static int mmp_pdma_alloc_chan_resources(struct dma_chan *dchan)
392 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
397 chan->desc_pool = dma_pool_create(dev_name(&dchan->dev->device),
399 sizeof(struct mmp_pdma_desc_sw),
400 __alignof__(struct mmp_pdma_desc_sw),
402 if (!chan->desc_pool) {
403 dev_err(chan->dev, "unable to allocate descriptor pool\n");
407 mmp_pdma_free_phy(chan);
413 static void mmp_pdma_free_desc_list(struct mmp_pdma_chan *chan,
414 struct list_head *list)
416 struct mmp_pdma_desc_sw *desc, *_desc;
418 list_for_each_entry_safe(desc, _desc, list, node) {
419 list_del(&desc->node);
420 dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
424 static void mmp_pdma_free_chan_resources(struct dma_chan *dchan)
426 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
429 spin_lock_irqsave(&chan->desc_lock, flags);
430 mmp_pdma_free_desc_list(chan, &chan->chain_pending);
431 mmp_pdma_free_desc_list(chan, &chan->chain_running);
432 spin_unlock_irqrestore(&chan->desc_lock, flags);
434 dma_pool_destroy(chan->desc_pool);
435 chan->desc_pool = NULL;
438 mmp_pdma_free_phy(chan);
442 static struct dma_async_tx_descriptor *
443 mmp_pdma_prep_memcpy(struct dma_chan *dchan,
444 dma_addr_t dma_dst, dma_addr_t dma_src,
445 size_t len, unsigned long flags)
447 struct mmp_pdma_chan *chan;
448 struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
457 chan = to_mmp_pdma_chan(dchan);
458 chan->byte_align = false;
461 chan->dir = DMA_MEM_TO_MEM;
462 chan->dcmd = DCMD_INCTRGADDR | DCMD_INCSRCADDR;
463 chan->dcmd |= DCMD_BURST32;
467 /* Allocate the link descriptor from DMA pool */
468 new = mmp_pdma_alloc_descriptor(chan);
470 dev_err(chan->dev, "no memory for desc\n");
474 copy = min_t(size_t, len, PDMA_MAX_DESC_BYTES);
475 if (dma_src & 0x7 || dma_dst & 0x7)
476 chan->byte_align = true;
478 new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & copy);
479 new->desc.dsadr = dma_src;
480 new->desc.dtadr = dma_dst;
485 prev->desc.ddadr = new->async_tx.phys;
487 new->async_tx.cookie = 0;
488 async_tx_ack(&new->async_tx);
493 if (chan->dir == DMA_MEM_TO_DEV) {
495 } else if (chan->dir == DMA_DEV_TO_MEM) {
497 } else if (chan->dir == DMA_MEM_TO_MEM) {
502 /* Insert the link descriptor to the LD ring */
503 list_add_tail(&new->node, &first->tx_list);
506 first->async_tx.flags = flags; /* client is in control of this ack */
507 first->async_tx.cookie = -EBUSY;
509 /* last desc and fire IRQ */
510 new->desc.ddadr = DDADR_STOP;
511 new->desc.dcmd |= DCMD_ENDIRQEN;
513 chan->cyclic_first = NULL;
515 return &first->async_tx;
519 mmp_pdma_free_desc_list(chan, &first->tx_list);
523 static struct dma_async_tx_descriptor *
524 mmp_pdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
525 unsigned int sg_len, enum dma_transfer_direction dir,
526 unsigned long flags, void *context)
528 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
529 struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new = NULL;
531 struct scatterlist *sg;
535 if ((sgl == NULL) || (sg_len == 0))
538 chan->byte_align = false;
540 for_each_sg(sgl, sg, sg_len, i) {
541 addr = sg_dma_address(sg);
542 avail = sg_dma_len(sgl);
545 len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES);
547 chan->byte_align = true;
549 /* allocate and populate the descriptor */
550 new = mmp_pdma_alloc_descriptor(chan);
552 dev_err(chan->dev, "no memory for desc\n");
556 new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & len);
557 if (dir == DMA_MEM_TO_DEV) {
558 new->desc.dsadr = addr;
559 new->desc.dtadr = chan->dev_addr;
561 new->desc.dsadr = chan->dev_addr;
562 new->desc.dtadr = addr;
568 prev->desc.ddadr = new->async_tx.phys;
570 new->async_tx.cookie = 0;
571 async_tx_ack(&new->async_tx);
574 /* Insert the link descriptor to the LD ring */
575 list_add_tail(&new->node, &first->tx_list);
577 /* update metadata */
583 first->async_tx.cookie = -EBUSY;
584 first->async_tx.flags = flags;
586 /* last desc and fire IRQ */
587 new->desc.ddadr = DDADR_STOP;
588 new->desc.dcmd |= DCMD_ENDIRQEN;
591 chan->cyclic_first = NULL;
593 return &first->async_tx;
597 mmp_pdma_free_desc_list(chan, &first->tx_list);
601 static struct dma_async_tx_descriptor *
602 mmp_pdma_prep_dma_cyclic(struct dma_chan *dchan,
603 dma_addr_t buf_addr, size_t len, size_t period_len,
604 enum dma_transfer_direction direction,
607 struct mmp_pdma_chan *chan;
608 struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
609 dma_addr_t dma_src, dma_dst;
611 if (!dchan || !len || !period_len)
614 /* the buffer length must be a multiple of period_len */
615 if (len % period_len != 0)
618 if (period_len > PDMA_MAX_DESC_BYTES)
621 chan = to_mmp_pdma_chan(dchan);
626 dma_dst = chan->dev_addr;
630 dma_src = chan->dev_addr;
633 dev_err(chan->dev, "Unsupported direction for cyclic DMA\n");
637 chan->dir = direction;
640 /* Allocate the link descriptor from DMA pool */
641 new = mmp_pdma_alloc_descriptor(chan);
643 dev_err(chan->dev, "no memory for desc\n");
647 new->desc.dcmd = (chan->dcmd | DCMD_ENDIRQEN |
648 (DCMD_LENGTH & period_len));
649 new->desc.dsadr = dma_src;
650 new->desc.dtadr = dma_dst;
655 prev->desc.ddadr = new->async_tx.phys;
657 new->async_tx.cookie = 0;
658 async_tx_ack(&new->async_tx);
663 if (chan->dir == DMA_MEM_TO_DEV)
664 dma_src += period_len;
666 dma_dst += period_len;
668 /* Insert the link descriptor to the LD ring */
669 list_add_tail(&new->node, &first->tx_list);
672 first->async_tx.flags = flags; /* client is in control of this ack */
673 first->async_tx.cookie = -EBUSY;
675 /* make the cyclic link */
676 new->desc.ddadr = first->async_tx.phys;
677 chan->cyclic_first = first;
679 return &first->async_tx;
683 mmp_pdma_free_desc_list(chan, &first->tx_list);
687 static int mmp_pdma_config(struct dma_chan *dchan,
688 struct dma_slave_config *cfg)
690 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
691 u32 maxburst = 0, addr = 0;
692 enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
697 if (cfg->direction == DMA_DEV_TO_MEM) {
698 chan->dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC;
699 maxburst = cfg->src_maxburst;
700 width = cfg->src_addr_width;
701 addr = cfg->src_addr;
702 } else if (cfg->direction == DMA_MEM_TO_DEV) {
703 chan->dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG;
704 maxburst = cfg->dst_maxburst;
705 width = cfg->dst_addr_width;
706 addr = cfg->dst_addr;
709 if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
710 chan->dcmd |= DCMD_WIDTH1;
711 else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
712 chan->dcmd |= DCMD_WIDTH2;
713 else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES)
714 chan->dcmd |= DCMD_WIDTH4;
717 chan->dcmd |= DCMD_BURST8;
718 else if (maxburst == 16)
719 chan->dcmd |= DCMD_BURST16;
720 else if (maxburst == 32)
721 chan->dcmd |= DCMD_BURST32;
723 chan->dir = cfg->direction;
724 chan->dev_addr = addr;
725 /* FIXME: drivers should be ported over to use the filter
726 * function. Once that's done, the following two lines can
730 chan->drcmr = cfg->slave_id;
735 static int mmp_pdma_terminate_all(struct dma_chan *dchan)
737 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
743 disable_chan(chan->phy);
744 mmp_pdma_free_phy(chan);
745 spin_lock_irqsave(&chan->desc_lock, flags);
746 mmp_pdma_free_desc_list(chan, &chan->chain_pending);
747 mmp_pdma_free_desc_list(chan, &chan->chain_running);
748 spin_unlock_irqrestore(&chan->desc_lock, flags);
754 static unsigned int mmp_pdma_residue(struct mmp_pdma_chan *chan,
757 struct mmp_pdma_desc_sw *sw;
758 u32 curr, residue = 0;
760 bool cyclic = chan->cyclic_first != NULL;
763 * If the channel does not have a phy pointer anymore, it has already
764 * been completed. Therefore, its residue is 0.
769 if (chan->dir == DMA_DEV_TO_MEM)
770 curr = readl(chan->phy->base + DTADR(chan->phy->idx));
772 curr = readl(chan->phy->base + DSADR(chan->phy->idx));
774 list_for_each_entry(sw, &chan->chain_running, node) {
777 if (chan->dir == DMA_DEV_TO_MEM)
778 start = sw->desc.dtadr;
780 start = sw->desc.dsadr;
782 len = sw->desc.dcmd & DCMD_LENGTH;
786 * 'passed' will be latched once we found the descriptor which
787 * lies inside the boundaries of the curr pointer. All
788 * descriptors that occur in the list _after_ we found that
789 * partially handled descriptor are still to be processed and
790 * are hence added to the residual bytes counter.
795 } else if (curr >= start && curr <= end) {
796 residue += end - curr;
801 * Descriptors that have the ENDIRQEN bit set mark the end of a
802 * transaction chain, and the cookie assigned with it has been
803 * returned previously from mmp_pdma_tx_submit().
805 * In case we have multiple transactions in the running chain,
806 * and the cookie does not match the one the user asked us
807 * about, reset the state variables and start over.
809 * This logic does not apply to cyclic transactions, where all
810 * descriptors have the ENDIRQEN bit set, and for which we
811 * can't have multiple transactions on one channel anyway.
813 if (cyclic || !(sw->desc.dcmd & DCMD_ENDIRQEN))
816 if (sw->async_tx.cookie == cookie) {
824 /* We should only get here in case of cyclic transactions */
828 static enum dma_status mmp_pdma_tx_status(struct dma_chan *dchan,
830 struct dma_tx_state *txstate)
832 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
835 ret = dma_cookie_status(dchan, cookie, txstate);
836 if (likely(ret != DMA_ERROR))
837 dma_set_residue(txstate, mmp_pdma_residue(chan, cookie));
843 * mmp_pdma_issue_pending - Issue the DMA start command
844 * pending list ==> running list
846 static void mmp_pdma_issue_pending(struct dma_chan *dchan)
848 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
851 spin_lock_irqsave(&chan->desc_lock, flags);
852 start_pending_queue(chan);
853 spin_unlock_irqrestore(&chan->desc_lock, flags);
861 static void dma_do_tasklet(unsigned long data)
863 struct mmp_pdma_chan *chan = (struct mmp_pdma_chan *)data;
864 struct mmp_pdma_desc_sw *desc, *_desc;
865 LIST_HEAD(chain_cleanup);
867 struct dmaengine_desc_callback cb;
869 if (chan->cyclic_first) {
870 spin_lock_irqsave(&chan->desc_lock, flags);
871 desc = chan->cyclic_first;
872 dmaengine_desc_get_callback(&desc->async_tx, &cb);
873 spin_unlock_irqrestore(&chan->desc_lock, flags);
875 dmaengine_desc_callback_invoke(&cb, NULL);
880 /* submit pending list; callback for each desc; free desc */
881 spin_lock_irqsave(&chan->desc_lock, flags);
883 list_for_each_entry_safe(desc, _desc, &chan->chain_running, node) {
885 * move the descriptors to a temporary list so we can drop
886 * the lock during the entire cleanup operation
888 list_move(&desc->node, &chain_cleanup);
891 * Look for the first list entry which has the ENDIRQEN flag
892 * set. That is the descriptor we got an interrupt for, so
893 * complete that transaction and its cookie.
895 if (desc->desc.dcmd & DCMD_ENDIRQEN) {
896 dma_cookie_t cookie = desc->async_tx.cookie;
897 dma_cookie_complete(&desc->async_tx);
898 dev_dbg(chan->dev, "completed_cookie=%d\n", cookie);
904 * The hardware is idle and ready for more when the
905 * chain_running list is empty.
907 chan->idle = list_empty(&chan->chain_running);
909 /* Start any pending transactions automatically */
910 start_pending_queue(chan);
911 spin_unlock_irqrestore(&chan->desc_lock, flags);
913 /* Run the callback for each descriptor, in order */
914 list_for_each_entry_safe(desc, _desc, &chain_cleanup, node) {
915 struct dma_async_tx_descriptor *txd = &desc->async_tx;
917 /* Remove from the list of transactions */
918 list_del(&desc->node);
919 /* Run the link descriptor callback function */
920 dmaengine_desc_get_callback(txd, &cb);
921 dmaengine_desc_callback_invoke(&cb, NULL);
923 dma_pool_free(chan->desc_pool, desc, txd->phys);
927 static int mmp_pdma_remove(struct platform_device *op)
929 struct mmp_pdma_device *pdev = platform_get_drvdata(op);
930 struct mmp_pdma_phy *phy;
931 int i, irq = 0, irq_num = 0;
934 for (i = 0; i < pdev->dma_channels; i++) {
935 if (platform_get_irq(op, i) > 0)
939 if (irq_num != pdev->dma_channels) {
940 irq = platform_get_irq(op, 0);
941 devm_free_irq(&op->dev, irq, pdev);
943 for (i = 0; i < pdev->dma_channels; i++) {
945 irq = platform_get_irq(op, i);
946 devm_free_irq(&op->dev, irq, phy);
950 dma_async_device_unregister(&pdev->device);
954 static int mmp_pdma_chan_init(struct mmp_pdma_device *pdev, int idx, int irq)
956 struct mmp_pdma_phy *phy = &pdev->phy[idx];
957 struct mmp_pdma_chan *chan;
960 chan = devm_kzalloc(pdev->dev, sizeof(*chan), GFP_KERNEL);
965 phy->base = pdev->base;
968 ret = devm_request_irq(pdev->dev, irq, mmp_pdma_chan_handler,
969 IRQF_SHARED, "pdma", phy);
971 dev_err(pdev->dev, "channel request irq fail!\n");
976 spin_lock_init(&chan->desc_lock);
977 chan->dev = pdev->dev;
978 chan->chan.device = &pdev->device;
979 tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
980 INIT_LIST_HEAD(&chan->chain_pending);
981 INIT_LIST_HEAD(&chan->chain_running);
983 /* register virt channel to dma engine */
984 list_add_tail(&chan->chan.device_node, &pdev->device.channels);
989 static const struct of_device_id mmp_pdma_dt_ids[] = {
990 { .compatible = "marvell,pdma-1.0", },
993 MODULE_DEVICE_TABLE(of, mmp_pdma_dt_ids);
995 static struct dma_chan *mmp_pdma_dma_xlate(struct of_phandle_args *dma_spec,
996 struct of_dma *ofdma)
998 struct mmp_pdma_device *d = ofdma->of_dma_data;
999 struct dma_chan *chan;
1001 chan = dma_get_any_slave_channel(&d->device);
1005 to_mmp_pdma_chan(chan)->drcmr = dma_spec->args[0];
1010 static int mmp_pdma_probe(struct platform_device *op)
1012 struct mmp_pdma_device *pdev;
1013 const struct of_device_id *of_id;
1014 struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
1015 struct resource *iores;
1016 int i, ret, irq = 0;
1017 int dma_channels = 0, irq_num = 0;
1018 const enum dma_slave_buswidth widths =
1019 DMA_SLAVE_BUSWIDTH_1_BYTE | DMA_SLAVE_BUSWIDTH_2_BYTES |
1020 DMA_SLAVE_BUSWIDTH_4_BYTES;
1022 pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
1026 pdev->dev = &op->dev;
1028 spin_lock_init(&pdev->phy_lock);
1030 iores = platform_get_resource(op, IORESOURCE_MEM, 0);
1031 pdev->base = devm_ioremap_resource(pdev->dev, iores);
1032 if (IS_ERR(pdev->base))
1033 return PTR_ERR(pdev->base);
1035 of_id = of_match_device(mmp_pdma_dt_ids, pdev->dev);
1037 of_property_read_u32(pdev->dev->of_node, "#dma-channels",
1039 else if (pdata && pdata->dma_channels)
1040 dma_channels = pdata->dma_channels;
1042 dma_channels = 32; /* default 32 channel */
1043 pdev->dma_channels = dma_channels;
1045 for (i = 0; i < dma_channels; i++) {
1046 if (platform_get_irq(op, i) > 0)
1050 pdev->phy = devm_kcalloc(pdev->dev, dma_channels, sizeof(*pdev->phy),
1052 if (pdev->phy == NULL)
1055 INIT_LIST_HEAD(&pdev->device.channels);
1057 if (irq_num != dma_channels) {
1058 /* all chan share one irq, demux inside */
1059 irq = platform_get_irq(op, 0);
1060 ret = devm_request_irq(pdev->dev, irq, mmp_pdma_int_handler,
1061 IRQF_SHARED, "pdma", pdev);
1066 for (i = 0; i < dma_channels; i++) {
1067 irq = (irq_num != dma_channels) ? 0 : platform_get_irq(op, i);
1068 ret = mmp_pdma_chan_init(pdev, i, irq);
1073 dma_cap_set(DMA_SLAVE, pdev->device.cap_mask);
1074 dma_cap_set(DMA_MEMCPY, pdev->device.cap_mask);
1075 dma_cap_set(DMA_CYCLIC, pdev->device.cap_mask);
1076 dma_cap_set(DMA_PRIVATE, pdev->device.cap_mask);
1077 pdev->device.dev = &op->dev;
1078 pdev->device.device_alloc_chan_resources = mmp_pdma_alloc_chan_resources;
1079 pdev->device.device_free_chan_resources = mmp_pdma_free_chan_resources;
1080 pdev->device.device_tx_status = mmp_pdma_tx_status;
1081 pdev->device.device_prep_dma_memcpy = mmp_pdma_prep_memcpy;
1082 pdev->device.device_prep_slave_sg = mmp_pdma_prep_slave_sg;
1083 pdev->device.device_prep_dma_cyclic = mmp_pdma_prep_dma_cyclic;
1084 pdev->device.device_issue_pending = mmp_pdma_issue_pending;
1085 pdev->device.device_config = mmp_pdma_config;
1086 pdev->device.device_terminate_all = mmp_pdma_terminate_all;
1087 pdev->device.copy_align = DMAENGINE_ALIGN_8_BYTES;
1088 pdev->device.src_addr_widths = widths;
1089 pdev->device.dst_addr_widths = widths;
1090 pdev->device.directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
1091 pdev->device.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
1093 if (pdev->dev->coherent_dma_mask)
1094 dma_set_mask(pdev->dev, pdev->dev->coherent_dma_mask);
1096 dma_set_mask(pdev->dev, DMA_BIT_MASK(64));
1098 ret = dma_async_device_register(&pdev->device);
1100 dev_err(pdev->device.dev, "unable to register\n");
1104 if (op->dev.of_node) {
1105 /* Device-tree DMA controller registration */
1106 ret = of_dma_controller_register(op->dev.of_node,
1107 mmp_pdma_dma_xlate, pdev);
1109 dev_err(&op->dev, "of_dma_controller_register failed\n");
1114 platform_set_drvdata(op, pdev);
1115 dev_info(pdev->device.dev, "initialized %d channels\n", dma_channels);
1119 static const struct platform_device_id mmp_pdma_id_table[] = {
1124 static struct platform_driver mmp_pdma_driver = {
1127 .of_match_table = mmp_pdma_dt_ids,
1129 .id_table = mmp_pdma_id_table,
1130 .probe = mmp_pdma_probe,
1131 .remove = mmp_pdma_remove,
1134 bool mmp_pdma_filter_fn(struct dma_chan *chan, void *param)
1136 struct mmp_pdma_chan *c = to_mmp_pdma_chan(chan);
1138 if (chan->device->dev->driver != &mmp_pdma_driver.driver)
1141 c->drcmr = *(unsigned int *)param;
1145 EXPORT_SYMBOL_GPL(mmp_pdma_filter_fn);
1147 module_platform_driver(mmp_pdma_driver);
1149 MODULE_DESCRIPTION("MARVELL MMP Peripheral DMA Driver");
1150 MODULE_AUTHOR("Marvell International Ltd.");
1151 MODULE_LICENSE("GPL v2");