2 * Freescale MPC85xx, MPC83xx DMA Engine support
4 * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
7 * Zhang Wei <wei.zhang@freescale.com>, Jul 2007
8 * Ebony Zhu <ebony.zhu@freescale.com>, May 2007
11 * DMA engine driver for Freescale MPC8540 DMA controller, which is
12 * also fit for MPC8560, MPC8555, MPC8548, MPC8641, and etc.
13 * The support for MPC8349 DMA controller is also added.
15 * This driver instructs the DMA controller to issue the PCI Read Multiple
16 * command for PCI read operations, instead of using the default PCI Read Line
17 * command. Please be aware that this setting may result in read pre-fetching
20 * This is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/dmaengine.h>
33 #include <linux/delay.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/dmapool.h>
36 #include <linux/of_platform.h>
38 #include <asm/fsldma.h>
41 static const char msg_ld_oom[] = "No free memory for link descriptor\n";
43 static void dma_init(struct fsldma_chan *chan)
45 /* Reset the channel */
46 DMA_OUT(chan, &chan->regs->mr, 0, 32);
48 switch (chan->feature & FSL_DMA_IP_MASK) {
50 /* Set the channel to below modes:
51 * EIE - Error interrupt enable
52 * EOSIE - End of segments interrupt enable (basic mode)
53 * EOLNIE - End of links interrupt enable
55 DMA_OUT(chan, &chan->regs->mr, FSL_DMA_MR_EIE
56 | FSL_DMA_MR_EOLNIE | FSL_DMA_MR_EOSIE, 32);
59 /* Set the channel to below modes:
60 * EOTIE - End-of-transfer interrupt enable
61 * PRC_RM - PCI read multiple
63 DMA_OUT(chan, &chan->regs->mr, FSL_DMA_MR_EOTIE
64 | FSL_DMA_MR_PRC_RM, 32);
69 static void set_sr(struct fsldma_chan *chan, u32 val)
71 DMA_OUT(chan, &chan->regs->sr, val, 32);
74 static u32 get_sr(struct fsldma_chan *chan)
76 return DMA_IN(chan, &chan->regs->sr, 32);
79 static void set_desc_cnt(struct fsldma_chan *chan,
80 struct fsl_dma_ld_hw *hw, u32 count)
82 hw->count = CPU_TO_DMA(chan, count, 32);
85 static void set_desc_src(struct fsldma_chan *chan,
86 struct fsl_dma_ld_hw *hw, dma_addr_t src)
90 snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
91 ? ((u64)FSL_DMA_SATR_SREADTYPE_SNOOP_READ << 32) : 0;
92 hw->src_addr = CPU_TO_DMA(chan, snoop_bits | src, 64);
95 static void set_desc_dst(struct fsldma_chan *chan,
96 struct fsl_dma_ld_hw *hw, dma_addr_t dst)
100 snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
101 ? ((u64)FSL_DMA_DATR_DWRITETYPE_SNOOP_WRITE << 32) : 0;
102 hw->dst_addr = CPU_TO_DMA(chan, snoop_bits | dst, 64);
105 static void set_desc_next(struct fsldma_chan *chan,
106 struct fsl_dma_ld_hw *hw, dma_addr_t next)
110 snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
112 hw->next_ln_addr = CPU_TO_DMA(chan, snoop_bits | next, 64);
115 static void set_cdar(struct fsldma_chan *chan, dma_addr_t addr)
117 DMA_OUT(chan, &chan->regs->cdar, addr | FSL_DMA_SNEN, 64);
120 static dma_addr_t get_cdar(struct fsldma_chan *chan)
122 return DMA_IN(chan, &chan->regs->cdar, 64) & ~FSL_DMA_SNEN;
125 static dma_addr_t get_ndar(struct fsldma_chan *chan)
127 return DMA_IN(chan, &chan->regs->ndar, 64);
130 static u32 get_bcr(struct fsldma_chan *chan)
132 return DMA_IN(chan, &chan->regs->bcr, 32);
135 static int dma_is_idle(struct fsldma_chan *chan)
137 u32 sr = get_sr(chan);
138 return (!(sr & FSL_DMA_SR_CB)) || (sr & FSL_DMA_SR_CH);
141 static void dma_start(struct fsldma_chan *chan)
145 mode = DMA_IN(chan, &chan->regs->mr, 32);
147 if ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX) {
148 if (chan->feature & FSL_DMA_CHAN_PAUSE_EXT) {
149 DMA_OUT(chan, &chan->regs->bcr, 0, 32);
150 mode |= FSL_DMA_MR_EMP_EN;
152 mode &= ~FSL_DMA_MR_EMP_EN;
156 if (chan->feature & FSL_DMA_CHAN_START_EXT)
157 mode |= FSL_DMA_MR_EMS_EN;
159 mode |= FSL_DMA_MR_CS;
161 DMA_OUT(chan, &chan->regs->mr, mode, 32);
164 static void dma_halt(struct fsldma_chan *chan)
169 mode = DMA_IN(chan, &chan->regs->mr, 32);
170 mode |= FSL_DMA_MR_CA;
171 DMA_OUT(chan, &chan->regs->mr, mode, 32);
173 mode &= ~(FSL_DMA_MR_CS | FSL_DMA_MR_EMS_EN | FSL_DMA_MR_CA);
174 DMA_OUT(chan, &chan->regs->mr, mode, 32);
176 for (i = 0; i < 100; i++) {
177 if (dma_is_idle(chan))
183 if (!dma_is_idle(chan))
184 dev_err(chan->dev, "DMA halt timeout!\n");
187 static void set_ld_eol(struct fsldma_chan *chan,
188 struct fsl_desc_sw *desc)
192 snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
195 desc->hw.next_ln_addr = CPU_TO_DMA(chan,
196 DMA_TO_CPU(chan, desc->hw.next_ln_addr, 64) | FSL_DMA_EOL
201 * fsl_chan_set_src_loop_size - Set source address hold transfer size
202 * @chan : Freescale DMA channel
203 * @size : Address loop size, 0 for disable loop
205 * The set source address hold transfer size. The source
206 * address hold or loop transfer size is when the DMA transfer
207 * data from source address (SA), if the loop size is 4, the DMA will
208 * read data from SA, SA + 1, SA + 2, SA + 3, then loop back to SA,
209 * SA + 1 ... and so on.
211 static void fsl_chan_set_src_loop_size(struct fsldma_chan *chan, int size)
215 mode = DMA_IN(chan, &chan->regs->mr, 32);
219 mode &= ~FSL_DMA_MR_SAHE;
225 mode |= FSL_DMA_MR_SAHE | (__ilog2(size) << 14);
229 DMA_OUT(chan, &chan->regs->mr, mode, 32);
233 * fsl_chan_set_dst_loop_size - Set destination address hold transfer size
234 * @chan : Freescale DMA channel
235 * @size : Address loop size, 0 for disable loop
237 * The set destination address hold transfer size. The destination
238 * address hold or loop transfer size is when the DMA transfer
239 * data to destination address (TA), if the loop size is 4, the DMA will
240 * write data to TA, TA + 1, TA + 2, TA + 3, then loop back to TA,
241 * TA + 1 ... and so on.
243 static void fsl_chan_set_dst_loop_size(struct fsldma_chan *chan, int size)
247 mode = DMA_IN(chan, &chan->regs->mr, 32);
251 mode &= ~FSL_DMA_MR_DAHE;
257 mode |= FSL_DMA_MR_DAHE | (__ilog2(size) << 16);
261 DMA_OUT(chan, &chan->regs->mr, mode, 32);
265 * fsl_chan_set_request_count - Set DMA Request Count for external control
266 * @chan : Freescale DMA channel
267 * @size : Number of bytes to transfer in a single request
269 * The Freescale DMA channel can be controlled by the external signal DREQ#.
270 * The DMA request count is how many bytes are allowed to transfer before
271 * pausing the channel, after which a new assertion of DREQ# resumes channel
274 * A size of 0 disables external pause control. The maximum size is 1024.
276 static void fsl_chan_set_request_count(struct fsldma_chan *chan, int size)
282 mode = DMA_IN(chan, &chan->regs->mr, 32);
283 mode |= (__ilog2(size) << 24) & 0x0f000000;
285 DMA_OUT(chan, &chan->regs->mr, mode, 32);
289 * fsl_chan_toggle_ext_pause - Toggle channel external pause status
290 * @chan : Freescale DMA channel
291 * @enable : 0 is disabled, 1 is enabled.
293 * The Freescale DMA channel can be controlled by the external signal DREQ#.
294 * The DMA Request Count feature should be used in addition to this feature
295 * to set the number of bytes to transfer before pausing the channel.
297 static void fsl_chan_toggle_ext_pause(struct fsldma_chan *chan, int enable)
300 chan->feature |= FSL_DMA_CHAN_PAUSE_EXT;
302 chan->feature &= ~FSL_DMA_CHAN_PAUSE_EXT;
306 * fsl_chan_toggle_ext_start - Toggle channel external start status
307 * @chan : Freescale DMA channel
308 * @enable : 0 is disabled, 1 is enabled.
310 * If enable the external start, the channel can be started by an
311 * external DMA start pin. So the dma_start() does not start the
312 * transfer immediately. The DMA channel will wait for the
313 * control pin asserted.
315 static void fsl_chan_toggle_ext_start(struct fsldma_chan *chan, int enable)
318 chan->feature |= FSL_DMA_CHAN_START_EXT;
320 chan->feature &= ~FSL_DMA_CHAN_START_EXT;
323 static void append_ld_queue(struct fsldma_chan *chan,
324 struct fsl_desc_sw *desc)
326 struct fsl_desc_sw *tail = to_fsl_desc(chan->ld_pending.prev);
328 if (list_empty(&chan->ld_pending))
332 * Add the hardware descriptor to the chain of hardware descriptors
333 * that already exists in memory.
335 * This will un-set the EOL bit of the existing transaction, and the
336 * last link in this transaction will become the EOL descriptor.
338 set_desc_next(chan, &tail->hw, desc->async_tx.phys);
341 * Add the software descriptor and all children to the list
342 * of pending transactions
345 list_splice_tail_init(&desc->tx_list, &chan->ld_pending);
348 static dma_cookie_t fsl_dma_tx_submit(struct dma_async_tx_descriptor *tx)
350 struct fsldma_chan *chan = to_fsl_chan(tx->chan);
351 struct fsl_desc_sw *desc = tx_to_fsl_desc(tx);
352 struct fsl_desc_sw *child;
356 spin_lock_irqsave(&chan->desc_lock, flags);
359 * assign cookies to all of the software descriptors
360 * that make up this transaction
362 cookie = chan->common.cookie;
363 list_for_each_entry(child, &desc->tx_list, node) {
368 child->async_tx.cookie = cookie;
371 chan->common.cookie = cookie;
373 /* put this transaction onto the tail of the pending queue */
374 append_ld_queue(chan, desc);
376 spin_unlock_irqrestore(&chan->desc_lock, flags);
382 * fsl_dma_alloc_descriptor - Allocate descriptor from channel's DMA pool.
383 * @chan : Freescale DMA channel
385 * Return - The descriptor allocated. NULL for failed.
387 static struct fsl_desc_sw *fsl_dma_alloc_descriptor(
388 struct fsldma_chan *chan)
390 struct fsl_desc_sw *desc;
393 desc = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
395 dev_dbg(chan->dev, "out of memory for link desc\n");
399 memset(desc, 0, sizeof(*desc));
400 INIT_LIST_HEAD(&desc->tx_list);
401 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
402 desc->async_tx.tx_submit = fsl_dma_tx_submit;
403 desc->async_tx.phys = pdesc;
410 * fsl_dma_alloc_chan_resources - Allocate resources for DMA channel.
411 * @chan : Freescale DMA channel
413 * This function will create a dma pool for descriptor allocation.
415 * Return - The number of descriptors allocated.
417 static int fsl_dma_alloc_chan_resources(struct dma_chan *dchan)
419 struct fsldma_chan *chan = to_fsl_chan(dchan);
421 /* Has this channel already been allocated? */
426 * We need the descriptor to be aligned to 32bytes
427 * for meeting FSL DMA specification requirement.
429 chan->desc_pool = dma_pool_create("fsl_dma_engine_desc_pool",
431 sizeof(struct fsl_desc_sw),
432 __alignof__(struct fsl_desc_sw), 0);
433 if (!chan->desc_pool) {
434 dev_err(chan->dev, "unable to allocate channel %d "
435 "descriptor pool\n", chan->id);
439 /* there is at least one descriptor free to be allocated */
444 * fsldma_free_desc_list - Free all descriptors in a queue
445 * @chan: Freescae DMA channel
446 * @list: the list to free
448 * LOCKING: must hold chan->desc_lock
450 static void fsldma_free_desc_list(struct fsldma_chan *chan,
451 struct list_head *list)
453 struct fsl_desc_sw *desc, *_desc;
455 list_for_each_entry_safe(desc, _desc, list, node) {
456 list_del(&desc->node);
457 dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
461 static void fsldma_free_desc_list_reverse(struct fsldma_chan *chan,
462 struct list_head *list)
464 struct fsl_desc_sw *desc, *_desc;
466 list_for_each_entry_safe_reverse(desc, _desc, list, node) {
467 list_del(&desc->node);
468 dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
473 * fsl_dma_free_chan_resources - Free all resources of the channel.
474 * @chan : Freescale DMA channel
476 static void fsl_dma_free_chan_resources(struct dma_chan *dchan)
478 struct fsldma_chan *chan = to_fsl_chan(dchan);
481 dev_dbg(chan->dev, "Free all channel resources.\n");
482 spin_lock_irqsave(&chan->desc_lock, flags);
483 fsldma_free_desc_list(chan, &chan->ld_pending);
484 fsldma_free_desc_list(chan, &chan->ld_running);
485 spin_unlock_irqrestore(&chan->desc_lock, flags);
487 dma_pool_destroy(chan->desc_pool);
488 chan->desc_pool = NULL;
491 static struct dma_async_tx_descriptor *
492 fsl_dma_prep_interrupt(struct dma_chan *dchan, unsigned long flags)
494 struct fsldma_chan *chan;
495 struct fsl_desc_sw *new;
500 chan = to_fsl_chan(dchan);
502 new = fsl_dma_alloc_descriptor(chan);
504 dev_err(chan->dev, msg_ld_oom);
508 new->async_tx.cookie = -EBUSY;
509 new->async_tx.flags = flags;
511 /* Insert the link descriptor to the LD ring */
512 list_add_tail(&new->node, &new->tx_list);
514 /* Set End-of-link to the last link descriptor of new list*/
515 set_ld_eol(chan, new);
517 return &new->async_tx;
520 static struct dma_async_tx_descriptor *fsl_dma_prep_memcpy(
521 struct dma_chan *dchan, dma_addr_t dma_dst, dma_addr_t dma_src,
522 size_t len, unsigned long flags)
524 struct fsldma_chan *chan;
525 struct fsl_desc_sw *first = NULL, *prev = NULL, *new;
534 chan = to_fsl_chan(dchan);
538 /* Allocate the link descriptor from DMA pool */
539 new = fsl_dma_alloc_descriptor(chan);
541 dev_err(chan->dev, msg_ld_oom);
544 #ifdef FSL_DMA_LD_DEBUG
545 dev_dbg(chan->dev, "new link desc alloc %p\n", new);
548 copy = min(len, (size_t)FSL_DMA_BCR_MAX_CNT);
550 set_desc_cnt(chan, &new->hw, copy);
551 set_desc_src(chan, &new->hw, dma_src);
552 set_desc_dst(chan, &new->hw, dma_dst);
557 set_desc_next(chan, &prev->hw, new->async_tx.phys);
559 new->async_tx.cookie = 0;
560 async_tx_ack(&new->async_tx);
567 /* Insert the link descriptor to the LD ring */
568 list_add_tail(&new->node, &first->tx_list);
571 new->async_tx.flags = flags; /* client is in control of this ack */
572 new->async_tx.cookie = -EBUSY;
574 /* Set End-of-link to the last link descriptor of new list*/
575 set_ld_eol(chan, new);
577 return &first->async_tx;
583 fsldma_free_desc_list_reverse(chan, &first->tx_list);
587 static struct dma_async_tx_descriptor *fsl_dma_prep_sg(struct dma_chan *dchan,
588 struct scatterlist *dst_sg, unsigned int dst_nents,
589 struct scatterlist *src_sg, unsigned int src_nents,
592 struct fsl_desc_sw *first = NULL, *prev = NULL, *new = NULL;
593 struct fsldma_chan *chan = to_fsl_chan(dchan);
594 size_t dst_avail, src_avail;
598 /* basic sanity checks */
599 if (dst_nents == 0 || src_nents == 0)
602 if (dst_sg == NULL || src_sg == NULL)
606 * TODO: should we check that both scatterlists have the same
607 * TODO: number of bytes in total? Is that really an error?
610 /* get prepared for the loop */
611 dst_avail = sg_dma_len(dst_sg);
612 src_avail = sg_dma_len(src_sg);
614 /* run until we are out of scatterlist entries */
617 /* create the largest transaction possible */
618 len = min_t(size_t, src_avail, dst_avail);
619 len = min_t(size_t, len, FSL_DMA_BCR_MAX_CNT);
623 dst = sg_dma_address(dst_sg) + sg_dma_len(dst_sg) - dst_avail;
624 src = sg_dma_address(src_sg) + sg_dma_len(src_sg) - src_avail;
626 /* allocate and populate the descriptor */
627 new = fsl_dma_alloc_descriptor(chan);
629 dev_err(chan->dev, msg_ld_oom);
632 #ifdef FSL_DMA_LD_DEBUG
633 dev_dbg(chan->dev, "new link desc alloc %p\n", new);
636 set_desc_cnt(chan, &new->hw, len);
637 set_desc_src(chan, &new->hw, src);
638 set_desc_dst(chan, &new->hw, dst);
643 set_desc_next(chan, &prev->hw, new->async_tx.phys);
645 new->async_tx.cookie = 0;
646 async_tx_ack(&new->async_tx);
649 /* Insert the link descriptor to the LD ring */
650 list_add_tail(&new->node, &first->tx_list);
652 /* update metadata */
657 /* fetch the next dst scatterlist entry */
658 if (dst_avail == 0) {
660 /* no more entries: we're done */
664 /* fetch the next entry: if there are no more: done */
665 dst_sg = sg_next(dst_sg);
670 dst_avail = sg_dma_len(dst_sg);
673 /* fetch the next src scatterlist entry */
674 if (src_avail == 0) {
676 /* no more entries: we're done */
680 /* fetch the next entry: if there are no more: done */
681 src_sg = sg_next(src_sg);
686 src_avail = sg_dma_len(src_sg);
690 new->async_tx.flags = flags; /* client is in control of this ack */
691 new->async_tx.cookie = -EBUSY;
693 /* Set End-of-link to the last link descriptor of new list */
694 set_ld_eol(chan, new);
696 return &first->async_tx;
702 fsldma_free_desc_list_reverse(chan, &first->tx_list);
707 * fsl_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
709 * @sgl: scatterlist to transfer to/from
710 * @sg_len: number of entries in @scatterlist
711 * @direction: DMA direction
712 * @flags: DMAEngine flags
714 * Prepare a set of descriptors for a DMA_SLAVE transaction. Following the
715 * DMA_SLAVE API, this gets the device-specific information from the
716 * chan->private variable.
718 static struct dma_async_tx_descriptor *fsl_dma_prep_slave_sg(
719 struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
720 enum dma_data_direction direction, unsigned long flags)
722 struct fsldma_chan *chan;
723 struct fsl_desc_sw *first = NULL, *prev = NULL, *new = NULL;
724 struct fsl_dma_slave *slave;
728 struct scatterlist *sg;
731 struct fsl_dma_hw_addr *hw;
732 dma_addr_t dma_dst, dma_src;
740 chan = to_fsl_chan(dchan);
741 slave = dchan->private;
743 if (list_empty(&slave->addresses))
746 hw = list_first_entry(&slave->addresses, struct fsl_dma_hw_addr, entry);
750 * Build the hardware transaction to copy from the scatterlist to
751 * the hardware, or from the hardware to the scatterlist
753 * If you are copying from the hardware to the scatterlist and it
754 * takes two hardware entries to fill an entire page, then both
755 * hardware entries will be coalesced into the same page
757 * If you are copying from the scatterlist to the hardware and a
758 * single page can fill two hardware entries, then the data will
759 * be read out of the page into the first hardware entry, and so on
761 for_each_sg(sgl, sg, sg_len, i) {
764 /* Loop until the entire scatterlist entry is used */
765 while (sg_used < sg_dma_len(sg)) {
768 * If we've used up the current hardware address/length
769 * pair, we need to load a new one
771 * This is done in a while loop so that descriptors with
772 * length == 0 will be skipped
774 while (hw_used >= hw->length) {
777 * If the current hardware entry is the last
778 * entry in the list, we're finished
780 if (list_is_last(&hw->entry, &slave->addresses))
783 /* Get the next hardware address/length pair */
784 hw = list_entry(hw->entry.next,
785 struct fsl_dma_hw_addr, entry);
789 /* Allocate the link descriptor from DMA pool */
790 new = fsl_dma_alloc_descriptor(chan);
792 dev_err(chan->dev, "No free memory for "
793 "link descriptor\n");
796 #ifdef FSL_DMA_LD_DEBUG
797 dev_dbg(chan->dev, "new link desc alloc %p\n", new);
801 * Calculate the maximum number of bytes to transfer,
802 * making sure it is less than the DMA controller limit
804 copy = min_t(size_t, sg_dma_len(sg) - sg_used,
805 hw->length - hw_used);
806 copy = min_t(size_t, copy, FSL_DMA_BCR_MAX_CNT);
810 * from the hardware to the scatterlist
813 * from the scatterlist to the hardware
815 if (direction == DMA_FROM_DEVICE) {
816 dma_src = hw->address + hw_used;
817 dma_dst = sg_dma_address(sg) + sg_used;
819 dma_src = sg_dma_address(sg) + sg_used;
820 dma_dst = hw->address + hw_used;
823 /* Fill in the descriptor */
824 set_desc_cnt(chan, &new->hw, copy);
825 set_desc_src(chan, &new->hw, dma_src);
826 set_desc_dst(chan, &new->hw, dma_dst);
829 * If this is not the first descriptor, chain the
830 * current descriptor after the previous descriptor
835 set_desc_next(chan, &prev->hw,
839 new->async_tx.cookie = 0;
840 async_tx_ack(&new->async_tx);
846 /* Insert the link descriptor into the LD ring */
847 list_add_tail(&new->node, &first->tx_list);
853 /* All of the hardware address/length pairs had length == 0 */
857 new->async_tx.flags = flags;
858 new->async_tx.cookie = -EBUSY;
860 /* Set End-of-link to the last link descriptor of new list */
861 set_ld_eol(chan, new);
863 /* Enable extra controller features */
864 if (chan->set_src_loop_size)
865 chan->set_src_loop_size(chan, slave->src_loop_size);
867 if (chan->set_dst_loop_size)
868 chan->set_dst_loop_size(chan, slave->dst_loop_size);
870 if (chan->toggle_ext_start)
871 chan->toggle_ext_start(chan, slave->external_start);
873 if (chan->toggle_ext_pause)
874 chan->toggle_ext_pause(chan, slave->external_pause);
876 if (chan->set_request_count)
877 chan->set_request_count(chan, slave->request_count);
879 return &first->async_tx;
882 /* If first was not set, then we failed to allocate the very first
883 * descriptor, and we're done */
888 * First is set, so all of the descriptors we allocated have been added
889 * to first->tx_list, INCLUDING "first" itself. Therefore we
890 * must traverse the list backwards freeing each descriptor in turn
892 * We're re-using variables for the loop, oh well
894 fsldma_free_desc_list_reverse(chan, &first->tx_list);
898 static int fsl_dma_device_control(struct dma_chan *dchan,
899 enum dma_ctrl_cmd cmd, unsigned long arg)
901 struct fsldma_chan *chan;
904 /* Only supports DMA_TERMINATE_ALL */
905 if (cmd != DMA_TERMINATE_ALL)
911 chan = to_fsl_chan(dchan);
913 /* Halt the DMA engine */
916 spin_lock_irqsave(&chan->desc_lock, flags);
918 /* Remove and free all of the descriptors in the LD queue */
919 fsldma_free_desc_list(chan, &chan->ld_pending);
920 fsldma_free_desc_list(chan, &chan->ld_running);
922 spin_unlock_irqrestore(&chan->desc_lock, flags);
928 * fsl_dma_update_completed_cookie - Update the completed cookie.
929 * @chan : Freescale DMA channel
933 static void fsl_dma_update_completed_cookie(struct fsldma_chan *chan)
935 struct fsl_desc_sw *desc;
939 spin_lock_irqsave(&chan->desc_lock, flags);
941 if (list_empty(&chan->ld_running)) {
942 dev_dbg(chan->dev, "no running descriptors\n");
946 /* Get the last descriptor, update the cookie to that */
947 desc = to_fsl_desc(chan->ld_running.prev);
948 if (dma_is_idle(chan))
949 cookie = desc->async_tx.cookie;
951 cookie = desc->async_tx.cookie - 1;
952 if (unlikely(cookie < DMA_MIN_COOKIE))
953 cookie = DMA_MAX_COOKIE;
956 chan->completed_cookie = cookie;
959 spin_unlock_irqrestore(&chan->desc_lock, flags);
963 * fsldma_desc_status - Check the status of a descriptor
964 * @chan: Freescale DMA channel
965 * @desc: DMA SW descriptor
967 * This function will return the status of the given descriptor
969 static enum dma_status fsldma_desc_status(struct fsldma_chan *chan,
970 struct fsl_desc_sw *desc)
972 return dma_async_is_complete(desc->async_tx.cookie,
973 chan->completed_cookie,
974 chan->common.cookie);
978 * fsl_chan_ld_cleanup - Clean up link descriptors
979 * @chan : Freescale DMA channel
981 * This function clean up the ld_queue of DMA channel.
983 static void fsl_chan_ld_cleanup(struct fsldma_chan *chan)
985 struct fsl_desc_sw *desc, *_desc;
988 spin_lock_irqsave(&chan->desc_lock, flags);
990 dev_dbg(chan->dev, "chan completed_cookie = %d\n", chan->completed_cookie);
991 list_for_each_entry_safe(desc, _desc, &chan->ld_running, node) {
992 dma_async_tx_callback callback;
993 void *callback_param;
995 if (fsldma_desc_status(chan, desc) == DMA_IN_PROGRESS)
998 /* Remove from the list of running transactions */
999 list_del(&desc->node);
1001 /* Run the link descriptor callback function */
1002 callback = desc->async_tx.callback;
1003 callback_param = desc->async_tx.callback_param;
1005 spin_unlock_irqrestore(&chan->desc_lock, flags);
1006 dev_dbg(chan->dev, "LD %p callback\n", desc);
1007 callback(callback_param);
1008 spin_lock_irqsave(&chan->desc_lock, flags);
1011 /* Run any dependencies, then free the descriptor */
1012 dma_run_dependencies(&desc->async_tx);
1013 dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
1016 spin_unlock_irqrestore(&chan->desc_lock, flags);
1020 * fsl_chan_xfer_ld_queue - transfer any pending transactions
1021 * @chan : Freescale DMA channel
1023 * This will make sure that any pending transactions will be run.
1024 * If the DMA controller is idle, it will be started. Otherwise,
1025 * the DMA controller's interrupt handler will start any pending
1026 * transactions when it becomes idle.
1028 static void fsl_chan_xfer_ld_queue(struct fsldma_chan *chan)
1030 struct fsl_desc_sw *desc;
1031 unsigned long flags;
1033 spin_lock_irqsave(&chan->desc_lock, flags);
1036 * If the list of pending descriptors is empty, then we
1037 * don't need to do any work at all
1039 if (list_empty(&chan->ld_pending)) {
1040 dev_dbg(chan->dev, "no pending LDs\n");
1045 * The DMA controller is not idle, which means the interrupt
1046 * handler will start any queued transactions when it runs
1047 * at the end of the current transaction
1049 if (!dma_is_idle(chan)) {
1050 dev_dbg(chan->dev, "DMA controller still busy\n");
1056 * make sure the dma_halt() function really un-wedges the
1057 * controller as much as possible
1062 * If there are some link descriptors which have not been
1063 * transferred, we need to start the controller
1067 * Move all elements from the queue of pending transactions
1068 * onto the list of running transactions
1070 desc = list_first_entry(&chan->ld_pending, struct fsl_desc_sw, node);
1071 list_splice_tail_init(&chan->ld_pending, &chan->ld_running);
1074 * Program the descriptor's address into the DMA controller,
1075 * then start the DMA transaction
1077 set_cdar(chan, desc->async_tx.phys);
1081 spin_unlock_irqrestore(&chan->desc_lock, flags);
1085 * fsl_dma_memcpy_issue_pending - Issue the DMA start command
1086 * @chan : Freescale DMA channel
1088 static void fsl_dma_memcpy_issue_pending(struct dma_chan *dchan)
1090 struct fsldma_chan *chan = to_fsl_chan(dchan);
1091 fsl_chan_xfer_ld_queue(chan);
1095 * fsl_tx_status - Determine the DMA status
1096 * @chan : Freescale DMA channel
1098 static enum dma_status fsl_tx_status(struct dma_chan *dchan,
1099 dma_cookie_t cookie,
1100 struct dma_tx_state *txstate)
1102 struct fsldma_chan *chan = to_fsl_chan(dchan);
1103 dma_cookie_t last_used;
1104 dma_cookie_t last_complete;
1106 fsl_chan_ld_cleanup(chan);
1108 last_used = dchan->cookie;
1109 last_complete = chan->completed_cookie;
1111 dma_set_tx_state(txstate, last_complete, last_used, 0);
1113 return dma_async_is_complete(cookie, last_complete, last_used);
1116 /*----------------------------------------------------------------------------*/
1117 /* Interrupt Handling */
1118 /*----------------------------------------------------------------------------*/
1120 static irqreturn_t fsldma_chan_irq(int irq, void *data)
1122 struct fsldma_chan *chan = data;
1123 int update_cookie = 0;
1127 /* save and clear the status register */
1128 stat = get_sr(chan);
1130 dev_dbg(chan->dev, "irq: channel %d, stat = 0x%x\n", chan->id, stat);
1132 stat &= ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);
1136 if (stat & FSL_DMA_SR_TE)
1137 dev_err(chan->dev, "Transfer Error!\n");
1141 * The DMA_INTERRUPT async_tx is a NULL transfer, which will
1142 * triger a PE interrupt.
1144 if (stat & FSL_DMA_SR_PE) {
1145 dev_dbg(chan->dev, "irq: Programming Error INT\n");
1146 if (get_bcr(chan) == 0) {
1147 /* BCR register is 0, this is a DMA_INTERRUPT async_tx.
1148 * Now, update the completed cookie, and continue the
1149 * next uncompleted transfer.
1154 stat &= ~FSL_DMA_SR_PE;
1158 * If the link descriptor segment transfer finishes,
1159 * we will recycle the used descriptor.
1161 if (stat & FSL_DMA_SR_EOSI) {
1162 dev_dbg(chan->dev, "irq: End-of-segments INT\n");
1163 dev_dbg(chan->dev, "irq: clndar 0x%llx, nlndar 0x%llx\n",
1164 (unsigned long long)get_cdar(chan),
1165 (unsigned long long)get_ndar(chan));
1166 stat &= ~FSL_DMA_SR_EOSI;
1171 * For MPC8349, EOCDI event need to update cookie
1172 * and start the next transfer if it exist.
1174 if (stat & FSL_DMA_SR_EOCDI) {
1175 dev_dbg(chan->dev, "irq: End-of-Chain link INT\n");
1176 stat &= ~FSL_DMA_SR_EOCDI;
1182 * If it current transfer is the end-of-transfer,
1183 * we should clear the Channel Start bit for
1184 * prepare next transfer.
1186 if (stat & FSL_DMA_SR_EOLNI) {
1187 dev_dbg(chan->dev, "irq: End-of-link INT\n");
1188 stat &= ~FSL_DMA_SR_EOLNI;
1193 fsl_dma_update_completed_cookie(chan);
1195 fsl_chan_xfer_ld_queue(chan);
1197 dev_dbg(chan->dev, "irq: unhandled sr 0x%02x\n", stat);
1199 dev_dbg(chan->dev, "irq: Exit\n");
1200 tasklet_schedule(&chan->tasklet);
1204 static void dma_do_tasklet(unsigned long data)
1206 struct fsldma_chan *chan = (struct fsldma_chan *)data;
1207 fsl_chan_ld_cleanup(chan);
1210 static irqreturn_t fsldma_ctrl_irq(int irq, void *data)
1212 struct fsldma_device *fdev = data;
1213 struct fsldma_chan *chan;
1214 unsigned int handled = 0;
1218 gsr = (fdev->feature & FSL_DMA_BIG_ENDIAN) ? in_be32(fdev->regs)
1219 : in_le32(fdev->regs);
1221 dev_dbg(fdev->dev, "IRQ: gsr 0x%.8x\n", gsr);
1223 for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
1224 chan = fdev->chan[i];
1229 dev_dbg(fdev->dev, "IRQ: chan %d\n", chan->id);
1230 fsldma_chan_irq(irq, chan);
1238 return IRQ_RETVAL(handled);
1241 static void fsldma_free_irqs(struct fsldma_device *fdev)
1243 struct fsldma_chan *chan;
1246 if (fdev->irq != NO_IRQ) {
1247 dev_dbg(fdev->dev, "free per-controller IRQ\n");
1248 free_irq(fdev->irq, fdev);
1252 for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
1253 chan = fdev->chan[i];
1254 if (chan && chan->irq != NO_IRQ) {
1255 dev_dbg(fdev->dev, "free channel %d IRQ\n", chan->id);
1256 free_irq(chan->irq, chan);
1261 static int fsldma_request_irqs(struct fsldma_device *fdev)
1263 struct fsldma_chan *chan;
1267 /* if we have a per-controller IRQ, use that */
1268 if (fdev->irq != NO_IRQ) {
1269 dev_dbg(fdev->dev, "request per-controller IRQ\n");
1270 ret = request_irq(fdev->irq, fsldma_ctrl_irq, IRQF_SHARED,
1271 "fsldma-controller", fdev);
1275 /* no per-controller IRQ, use the per-channel IRQs */
1276 for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
1277 chan = fdev->chan[i];
1281 if (chan->irq == NO_IRQ) {
1282 dev_err(fdev->dev, "no interrupts property defined for "
1283 "DMA channel %d. Please fix your "
1284 "device tree\n", chan->id);
1289 dev_dbg(fdev->dev, "request channel %d IRQ\n", chan->id);
1290 ret = request_irq(chan->irq, fsldma_chan_irq, IRQF_SHARED,
1291 "fsldma-chan", chan);
1293 dev_err(fdev->dev, "unable to request IRQ for DMA "
1294 "channel %d\n", chan->id);
1302 for (/* none */; i >= 0; i--) {
1303 chan = fdev->chan[i];
1307 if (chan->irq == NO_IRQ)
1310 free_irq(chan->irq, chan);
1316 /*----------------------------------------------------------------------------*/
1317 /* OpenFirmware Subsystem */
1318 /*----------------------------------------------------------------------------*/
1320 static int __devinit fsl_dma_chan_probe(struct fsldma_device *fdev,
1321 struct device_node *node, u32 feature, const char *compatible)
1323 struct fsldma_chan *chan;
1324 struct resource res;
1328 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
1330 dev_err(fdev->dev, "no free memory for DMA channels!\n");
1335 /* ioremap registers for use */
1336 chan->regs = of_iomap(node, 0);
1338 dev_err(fdev->dev, "unable to ioremap registers\n");
1343 err = of_address_to_resource(node, 0, &res);
1345 dev_err(fdev->dev, "unable to find 'reg' property\n");
1346 goto out_iounmap_regs;
1349 chan->feature = feature;
1351 fdev->feature = chan->feature;
1354 * If the DMA device's feature is different than the feature
1355 * of its channels, report the bug
1357 WARN_ON(fdev->feature != chan->feature);
1359 chan->dev = fdev->dev;
1360 chan->id = ((res.start - 0x100) & 0xfff) >> 7;
1361 if (chan->id >= FSL_DMA_MAX_CHANS_PER_DEVICE) {
1362 dev_err(fdev->dev, "too many channels for device\n");
1364 goto out_iounmap_regs;
1367 fdev->chan[chan->id] = chan;
1368 tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
1370 /* Initialize the channel */
1373 /* Clear cdar registers */
1376 switch (chan->feature & FSL_DMA_IP_MASK) {
1377 case FSL_DMA_IP_85XX:
1378 chan->toggle_ext_pause = fsl_chan_toggle_ext_pause;
1379 case FSL_DMA_IP_83XX:
1380 chan->toggle_ext_start = fsl_chan_toggle_ext_start;
1381 chan->set_src_loop_size = fsl_chan_set_src_loop_size;
1382 chan->set_dst_loop_size = fsl_chan_set_dst_loop_size;
1383 chan->set_request_count = fsl_chan_set_request_count;
1386 spin_lock_init(&chan->desc_lock);
1387 INIT_LIST_HEAD(&chan->ld_pending);
1388 INIT_LIST_HEAD(&chan->ld_running);
1390 chan->common.device = &fdev->common;
1392 /* find the IRQ line, if it exists in the device tree */
1393 chan->irq = irq_of_parse_and_map(node, 0);
1395 /* Add the channel to DMA device channel list */
1396 list_add_tail(&chan->common.device_node, &fdev->common.channels);
1397 fdev->common.chancnt++;
1399 dev_info(fdev->dev, "#%d (%s), irq %d\n", chan->id, compatible,
1400 chan->irq != NO_IRQ ? chan->irq : fdev->irq);
1405 iounmap(chan->regs);
1412 static void fsl_dma_chan_remove(struct fsldma_chan *chan)
1414 irq_dispose_mapping(chan->irq);
1415 list_del(&chan->common.device_node);
1416 iounmap(chan->regs);
1420 static int __devinit fsldma_of_probe(struct platform_device *op,
1421 const struct of_device_id *match)
1423 struct fsldma_device *fdev;
1424 struct device_node *child;
1427 fdev = kzalloc(sizeof(*fdev), GFP_KERNEL);
1429 dev_err(&op->dev, "No enough memory for 'priv'\n");
1434 fdev->dev = &op->dev;
1435 INIT_LIST_HEAD(&fdev->common.channels);
1437 /* ioremap the registers for use */
1438 fdev->regs = of_iomap(op->dev.of_node, 0);
1440 dev_err(&op->dev, "unable to ioremap registers\n");
1445 /* map the channel IRQ if it exists, but don't hookup the handler yet */
1446 fdev->irq = irq_of_parse_and_map(op->dev.of_node, 0);
1448 dma_cap_set(DMA_MEMCPY, fdev->common.cap_mask);
1449 dma_cap_set(DMA_INTERRUPT, fdev->common.cap_mask);
1450 dma_cap_set(DMA_SG, fdev->common.cap_mask);
1451 dma_cap_set(DMA_SLAVE, fdev->common.cap_mask);
1452 fdev->common.device_alloc_chan_resources = fsl_dma_alloc_chan_resources;
1453 fdev->common.device_free_chan_resources = fsl_dma_free_chan_resources;
1454 fdev->common.device_prep_dma_interrupt = fsl_dma_prep_interrupt;
1455 fdev->common.device_prep_dma_memcpy = fsl_dma_prep_memcpy;
1456 fdev->common.device_prep_dma_sg = fsl_dma_prep_sg;
1457 fdev->common.device_tx_status = fsl_tx_status;
1458 fdev->common.device_issue_pending = fsl_dma_memcpy_issue_pending;
1459 fdev->common.device_prep_slave_sg = fsl_dma_prep_slave_sg;
1460 fdev->common.device_control = fsl_dma_device_control;
1461 fdev->common.dev = &op->dev;
1463 dev_set_drvdata(&op->dev, fdev);
1466 * We cannot use of_platform_bus_probe() because there is no
1467 * of_platform_bus_remove(). Instead, we manually instantiate every DMA
1470 for_each_child_of_node(op->dev.of_node, child) {
1471 if (of_device_is_compatible(child, "fsl,eloplus-dma-channel")) {
1472 fsl_dma_chan_probe(fdev, child,
1473 FSL_DMA_IP_85XX | FSL_DMA_BIG_ENDIAN,
1474 "fsl,eloplus-dma-channel");
1477 if (of_device_is_compatible(child, "fsl,elo-dma-channel")) {
1478 fsl_dma_chan_probe(fdev, child,
1479 FSL_DMA_IP_83XX | FSL_DMA_LITTLE_ENDIAN,
1480 "fsl,elo-dma-channel");
1485 * Hookup the IRQ handler(s)
1487 * If we have a per-controller interrupt, we prefer that to the
1488 * per-channel interrupts to reduce the number of shared interrupt
1489 * handlers on the same IRQ line
1491 err = fsldma_request_irqs(fdev);
1493 dev_err(fdev->dev, "unable to request IRQs\n");
1497 dma_async_device_register(&fdev->common);
1501 irq_dispose_mapping(fdev->irq);
1507 static int fsldma_of_remove(struct platform_device *op)
1509 struct fsldma_device *fdev;
1512 fdev = dev_get_drvdata(&op->dev);
1513 dma_async_device_unregister(&fdev->common);
1515 fsldma_free_irqs(fdev);
1517 for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
1519 fsl_dma_chan_remove(fdev->chan[i]);
1522 iounmap(fdev->regs);
1523 dev_set_drvdata(&op->dev, NULL);
1529 static const struct of_device_id fsldma_of_ids[] = {
1530 { .compatible = "fsl,eloplus-dma", },
1531 { .compatible = "fsl,elo-dma", },
1535 static struct of_platform_driver fsldma_of_driver = {
1537 .name = "fsl-elo-dma",
1538 .owner = THIS_MODULE,
1539 .of_match_table = fsldma_of_ids,
1541 .probe = fsldma_of_probe,
1542 .remove = fsldma_of_remove,
1545 /*----------------------------------------------------------------------------*/
1546 /* Module Init / Exit */
1547 /*----------------------------------------------------------------------------*/
1549 static __init int fsldma_init(void)
1553 pr_info("Freescale Elo / Elo Plus DMA driver\n");
1555 ret = of_register_platform_driver(&fsldma_of_driver);
1557 pr_err("fsldma: failed to register platform driver\n");
1562 static void __exit fsldma_exit(void)
1564 of_unregister_platform_driver(&fsldma_of_driver);
1567 subsys_initcall(fsldma_init);
1568 module_exit(fsldma_exit);
1570 MODULE_DESCRIPTION("Freescale Elo / Elo Plus DMA driver");
1571 MODULE_LICENSE("GPL");