2 * Driver for the Atmel AHB DMA Controller (aka HDMA or DMAC on AT91 systems)
4 * Copyright (C) 2008 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 * This supports the Atmel AHB DMA Controller,
14 * The driver has currently been tested with the Atmel AT91SAM9RL
15 * and AT91SAM9G45 series.
18 #include <linux/clk.h>
19 #include <linux/dmaengine.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/dmapool.h>
22 #include <linux/interrupt.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
27 #include "at_hdmac_regs.h"
33 * at_hdmac : Name of the ATmel AHB DMA Controller
34 * at_dma_ / atdma : ATmel DMA controller entity related
35 * atc_ / atchan : ATmel DMA Channel entity related
38 #define ATC_DEFAULT_CFG (ATC_FIFOCFG_HALFFIFO)
39 #define ATC_DEFAULT_CTRLA (0)
40 #define ATC_DEFAULT_CTRLB (ATC_SIF(AT_DMA_MEM_IF) \
41 |ATC_DIF(AT_DMA_MEM_IF))
44 * Initial number of descriptors to allocate for each channel. This could
45 * be increased during dma usage.
47 static unsigned int init_nr_desc_per_channel = 64;
48 module_param(init_nr_desc_per_channel, uint, 0644);
49 MODULE_PARM_DESC(init_nr_desc_per_channel,
50 "initial descriptors per channel (default: 64)");
54 static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx);
57 /*----------------------------------------------------------------------*/
59 static struct at_desc *atc_first_active(struct at_dma_chan *atchan)
61 return list_first_entry(&atchan->active_list,
62 struct at_desc, desc_node);
65 static struct at_desc *atc_first_queued(struct at_dma_chan *atchan)
67 return list_first_entry(&atchan->queue,
68 struct at_desc, desc_node);
72 * atc_alloc_descriptor - allocate and return an initialized descriptor
73 * @chan: the channel to allocate descriptors for
74 * @gfp_flags: GFP allocation flags
76 * Note: The ack-bit is positioned in the descriptor flag at creation time
77 * to make initial allocation more convenient. This bit will be cleared
78 * and control will be given to client at usage time (during
79 * preparation functions).
81 static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan,
84 struct at_desc *desc = NULL;
85 struct at_dma *atdma = to_at_dma(chan->device);
88 desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys);
90 memset(desc, 0, sizeof(struct at_desc));
91 INIT_LIST_HEAD(&desc->tx_list);
92 dma_async_tx_descriptor_init(&desc->txd, chan);
93 /* txd.flags will be overwritten in prep functions */
94 desc->txd.flags = DMA_CTRL_ACK;
95 desc->txd.tx_submit = atc_tx_submit;
96 desc->txd.phys = phys;
103 * atc_desc_get - get an unused descriptor from free_list
104 * @atchan: channel we want a new descriptor for
106 static struct at_desc *atc_desc_get(struct at_dma_chan *atchan)
108 struct at_desc *desc, *_desc;
109 struct at_desc *ret = NULL;
113 spin_lock_bh(&atchan->lock);
114 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
116 if (async_tx_test_ack(&desc->txd)) {
117 list_del(&desc->desc_node);
121 dev_dbg(chan2dev(&atchan->chan_common),
122 "desc %p not ACKed\n", desc);
124 spin_unlock_bh(&atchan->lock);
125 dev_vdbg(chan2dev(&atchan->chan_common),
126 "scanned %u descriptors on freelist\n", i);
128 /* no more descriptor available in initial pool: create one more */
130 ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC);
132 spin_lock_bh(&atchan->lock);
133 atchan->descs_allocated++;
134 spin_unlock_bh(&atchan->lock);
136 dev_err(chan2dev(&atchan->chan_common),
137 "not enough descriptors available\n");
145 * atc_desc_put - move a descriptor, including any children, to the free list
146 * @atchan: channel we work on
147 * @desc: descriptor, at the head of a chain, to move to free list
149 static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc)
152 struct at_desc *child;
154 spin_lock_bh(&atchan->lock);
155 list_for_each_entry(child, &desc->tx_list, desc_node)
156 dev_vdbg(chan2dev(&atchan->chan_common),
157 "moving child desc %p to freelist\n",
159 list_splice_init(&desc->tx_list, &atchan->free_list);
160 dev_vdbg(chan2dev(&atchan->chan_common),
161 "moving desc %p to freelist\n", desc);
162 list_add(&desc->desc_node, &atchan->free_list);
163 spin_unlock_bh(&atchan->lock);
168 * atc_desc_chain - build chain adding a descripor
169 * @first: address of first descripor of the chain
170 * @prev: address of previous descripor of the chain
171 * @desc: descriptor to queue
173 * Called from prep_* functions
175 static void atc_desc_chain(struct at_desc **first, struct at_desc **prev,
176 struct at_desc *desc)
181 /* inform the HW lli about chaining */
182 (*prev)->lli.dscr = desc->txd.phys;
183 /* insert the link descriptor to the LD ring */
184 list_add_tail(&desc->desc_node,
191 * atc_assign_cookie - compute and assign new cookie
192 * @atchan: channel we work on
193 * @desc: descriptor to asign cookie for
195 * Called with atchan->lock held and bh disabled
198 atc_assign_cookie(struct at_dma_chan *atchan, struct at_desc *desc)
200 dma_cookie_t cookie = atchan->chan_common.cookie;
205 atchan->chan_common.cookie = cookie;
206 desc->txd.cookie = cookie;
212 * atc_dostart - starts the DMA engine for real
213 * @atchan: the channel we want to start
214 * @first: first descriptor in the list we want to begin with
216 * Called with atchan->lock held and bh disabled
218 static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
220 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
222 /* ASSERT: channel is idle */
223 if (atc_chan_is_enabled(atchan)) {
224 dev_err(chan2dev(&atchan->chan_common),
225 "BUG: Attempted to start non-idle channel\n");
226 dev_err(chan2dev(&atchan->chan_common),
227 " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
228 channel_readl(atchan, SADDR),
229 channel_readl(atchan, DADDR),
230 channel_readl(atchan, CTRLA),
231 channel_readl(atchan, CTRLB),
232 channel_readl(atchan, DSCR));
234 /* The tasklet will hopefully advance the queue... */
238 vdbg_dump_regs(atchan);
240 /* clear any pending interrupt */
241 while (dma_readl(atdma, EBCISR))
244 channel_writel(atchan, SADDR, 0);
245 channel_writel(atchan, DADDR, 0);
246 channel_writel(atchan, CTRLA, 0);
247 channel_writel(atchan, CTRLB, 0);
248 channel_writel(atchan, DSCR, first->txd.phys);
249 dma_writel(atdma, CHER, atchan->mask);
251 vdbg_dump_regs(atchan);
255 * atc_chain_complete - finish work for one transaction chain
256 * @atchan: channel we work on
257 * @desc: descriptor at the head of the chain we want do complete
259 * Called with atchan->lock held and bh disabled */
261 atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
263 struct dma_async_tx_descriptor *txd = &desc->txd;
265 dev_vdbg(chan2dev(&atchan->chan_common),
266 "descriptor %u complete\n", txd->cookie);
268 atchan->completed_cookie = txd->cookie;
270 /* move children to free_list */
271 list_splice_init(&desc->tx_list, &atchan->free_list);
272 /* move myself to free_list */
273 list_move(&desc->desc_node, &atchan->free_list);
275 /* unmap dma addresses (not on slave channels) */
276 if (!atchan->chan_common.private) {
277 struct device *parent = chan2parent(&atchan->chan_common);
278 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
279 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
280 dma_unmap_single(parent,
282 desc->len, DMA_FROM_DEVICE);
284 dma_unmap_page(parent,
286 desc->len, DMA_FROM_DEVICE);
288 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
289 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
290 dma_unmap_single(parent,
292 desc->len, DMA_TO_DEVICE);
294 dma_unmap_page(parent,
296 desc->len, DMA_TO_DEVICE);
300 /* for cyclic transfers,
301 * no need to replay callback function while stopping */
302 if (!test_bit(ATC_IS_CYCLIC, &atchan->status)) {
303 dma_async_tx_callback callback = txd->callback;
304 void *param = txd->callback_param;
307 * The API requires that no submissions are done from a
308 * callback, so we don't need to drop the lock here
314 dma_run_dependencies(txd);
318 * atc_complete_all - finish work for all transactions
319 * @atchan: channel to complete transactions for
321 * Eventually submit queued descriptors if any
323 * Assume channel is idle while calling this function
324 * Called with atchan->lock held and bh disabled
326 static void atc_complete_all(struct at_dma_chan *atchan)
328 struct at_desc *desc, *_desc;
331 dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
333 BUG_ON(atc_chan_is_enabled(atchan));
336 * Submit queued descriptors ASAP, i.e. before we go through
337 * the completed ones.
339 if (!list_empty(&atchan->queue))
340 atc_dostart(atchan, atc_first_queued(atchan));
341 /* empty active_list now it is completed */
342 list_splice_init(&atchan->active_list, &list);
343 /* empty queue list by moving descriptors (if any) to active_list */
344 list_splice_init(&atchan->queue, &atchan->active_list);
346 list_for_each_entry_safe(desc, _desc, &list, desc_node)
347 atc_chain_complete(atchan, desc);
351 * atc_cleanup_descriptors - cleanup up finished descriptors in active_list
352 * @atchan: channel to be cleaned up
354 * Called with atchan->lock held and bh disabled
356 static void atc_cleanup_descriptors(struct at_dma_chan *atchan)
358 struct at_desc *desc, *_desc;
359 struct at_desc *child;
361 dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n");
363 list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
364 if (!(desc->lli.ctrla & ATC_DONE))
365 /* This one is currently in progress */
368 list_for_each_entry(child, &desc->tx_list, desc_node)
369 if (!(child->lli.ctrla & ATC_DONE))
370 /* Currently in progress */
374 * No descriptors so far seem to be in progress, i.e.
375 * this chain must be done.
377 atc_chain_complete(atchan, desc);
382 * atc_advance_work - at the end of a transaction, move forward
383 * @atchan: channel where the transaction ended
385 * Called with atchan->lock held and bh disabled
387 static void atc_advance_work(struct at_dma_chan *atchan)
389 dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
391 if (list_empty(&atchan->active_list) ||
392 list_is_singular(&atchan->active_list)) {
393 atc_complete_all(atchan);
395 atc_chain_complete(atchan, atc_first_active(atchan));
397 atc_dostart(atchan, atc_first_active(atchan));
403 * atc_handle_error - handle errors reported by DMA controller
404 * @atchan: channel where error occurs
406 * Called with atchan->lock held and bh disabled
408 static void atc_handle_error(struct at_dma_chan *atchan)
410 struct at_desc *bad_desc;
411 struct at_desc *child;
414 * The descriptor currently at the head of the active list is
415 * broked. Since we don't have any way to report errors, we'll
416 * just have to scream loudly and try to carry on.
418 bad_desc = atc_first_active(atchan);
419 list_del_init(&bad_desc->desc_node);
421 /* As we are stopped, take advantage to push queued descriptors
423 list_splice_init(&atchan->queue, atchan->active_list.prev);
425 /* Try to restart the controller */
426 if (!list_empty(&atchan->active_list))
427 atc_dostart(atchan, atc_first_active(atchan));
430 * KERN_CRITICAL may seem harsh, but since this only happens
431 * when someone submits a bad physical address in a
432 * descriptor, we should consider ourselves lucky that the
433 * controller flagged an error instead of scribbling over
434 * random memory locations.
436 dev_crit(chan2dev(&atchan->chan_common),
437 "Bad descriptor submitted for DMA!\n");
438 dev_crit(chan2dev(&atchan->chan_common),
439 " cookie: %d\n", bad_desc->txd.cookie);
440 atc_dump_lli(atchan, &bad_desc->lli);
441 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
442 atc_dump_lli(atchan, &child->lli);
444 /* Pretend the descriptor completed successfully */
445 atc_chain_complete(atchan, bad_desc);
449 * atc_handle_cyclic - at the end of a period, run callback function
450 * @atchan: channel used for cyclic operations
452 * Called with atchan->lock held and bh disabled
454 static void atc_handle_cyclic(struct at_dma_chan *atchan)
456 struct at_desc *first = atc_first_active(atchan);
457 struct dma_async_tx_descriptor *txd = &first->txd;
458 dma_async_tx_callback callback = txd->callback;
459 void *param = txd->callback_param;
461 dev_vdbg(chan2dev(&atchan->chan_common),
462 "new cyclic period llp 0x%08x\n",
463 channel_readl(atchan, DSCR));
469 /*-- IRQ & Tasklet ---------------------------------------------------*/
471 static void atc_tasklet(unsigned long data)
473 struct at_dma_chan *atchan = (struct at_dma_chan *)data;
475 spin_lock(&atchan->lock);
476 if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
477 atc_handle_error(atchan);
478 else if (test_bit(ATC_IS_CYCLIC, &atchan->status))
479 atc_handle_cyclic(atchan);
481 atc_advance_work(atchan);
483 spin_unlock(&atchan->lock);
486 static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
488 struct at_dma *atdma = (struct at_dma *)dev_id;
489 struct at_dma_chan *atchan;
491 u32 status, pending, imr;
495 imr = dma_readl(atdma, EBCIMR);
496 status = dma_readl(atdma, EBCISR);
497 pending = status & imr;
502 dev_vdbg(atdma->dma_common.dev,
503 "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
504 status, imr, pending);
506 for (i = 0; i < atdma->dma_common.chancnt; i++) {
507 atchan = &atdma->chan[i];
508 if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
509 if (pending & AT_DMA_ERR(i)) {
510 /* Disable channel on AHB error */
511 dma_writel(atdma, CHDR,
512 AT_DMA_RES(i) | atchan->mask);
513 /* Give information to tasklet */
514 set_bit(ATC_IS_ERROR, &atchan->status);
516 tasklet_schedule(&atchan->tasklet);
527 /*-- DMA Engine API --------------------------------------------------*/
530 * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
531 * @desc: descriptor at the head of the transaction chain
533 * Queue chain if DMA engine is working already
535 * Cookie increment and adding to active_list or queue must be atomic
537 static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
539 struct at_desc *desc = txd_to_at_desc(tx);
540 struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
543 spin_lock_bh(&atchan->lock);
544 cookie = atc_assign_cookie(atchan, desc);
546 if (list_empty(&atchan->active_list)) {
547 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
549 atc_dostart(atchan, desc);
550 list_add_tail(&desc->desc_node, &atchan->active_list);
552 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
554 list_add_tail(&desc->desc_node, &atchan->queue);
557 spin_unlock_bh(&atchan->lock);
563 * atc_prep_dma_memcpy - prepare a memcpy operation
564 * @chan: the channel to prepare operation on
565 * @dest: operation virtual destination address
566 * @src: operation virtual source address
567 * @len: operation length
568 * @flags: tx descriptor status flags
570 static struct dma_async_tx_descriptor *
571 atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
572 size_t len, unsigned long flags)
574 struct at_dma_chan *atchan = to_at_dma_chan(chan);
575 struct at_desc *desc = NULL;
576 struct at_desc *first = NULL;
577 struct at_desc *prev = NULL;
580 unsigned int src_width;
581 unsigned int dst_width;
585 dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
586 dest, src, len, flags);
588 if (unlikely(!len)) {
589 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
593 ctrla = ATC_DEFAULT_CTRLA;
594 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
595 | ATC_SRC_ADDR_MODE_INCR
596 | ATC_DST_ADDR_MODE_INCR
600 * We can be a lot more clever here, but this should take care
601 * of the most common optimization.
603 if (!((src | dest | len) & 3)) {
604 ctrla |= ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
605 src_width = dst_width = 2;
606 } else if (!((src | dest | len) & 1)) {
607 ctrla |= ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
608 src_width = dst_width = 1;
610 ctrla |= ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
611 src_width = dst_width = 0;
614 for (offset = 0; offset < len; offset += xfer_count << src_width) {
615 xfer_count = min_t(size_t, (len - offset) >> src_width,
618 desc = atc_desc_get(atchan);
622 desc->lli.saddr = src + offset;
623 desc->lli.daddr = dest + offset;
624 desc->lli.ctrla = ctrla | xfer_count;
625 desc->lli.ctrlb = ctrlb;
627 desc->txd.cookie = 0;
632 /* inform the HW lli about chaining */
633 prev->lli.dscr = desc->txd.phys;
634 /* insert the link descriptor to the LD ring */
635 list_add_tail(&desc->desc_node,
641 /* First descriptor of the chain embedds additional information */
642 first->txd.cookie = -EBUSY;
645 /* set end-of-link to the last link descriptor of list*/
648 first->txd.flags = flags; /* client is in control of this ack */
653 atc_desc_put(atchan, first);
659 * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
661 * @sgl: scatterlist to transfer to/from
662 * @sg_len: number of entries in @scatterlist
663 * @direction: DMA direction
664 * @flags: tx descriptor status flags
666 static struct dma_async_tx_descriptor *
667 atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
668 unsigned int sg_len, enum dma_data_direction direction,
671 struct at_dma_chan *atchan = to_at_dma_chan(chan);
672 struct at_dma_slave *atslave = chan->private;
673 struct at_desc *first = NULL;
674 struct at_desc *prev = NULL;
678 unsigned int reg_width;
679 unsigned int mem_width;
681 struct scatterlist *sg;
682 size_t total_len = 0;
684 dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
686 direction == DMA_TO_DEVICE ? "TO DEVICE" : "FROM DEVICE",
689 if (unlikely(!atslave || !sg_len)) {
690 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
694 reg_width = atslave->reg_width;
696 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla;
701 ctrla |= ATC_DST_WIDTH(reg_width);
702 ctrlb |= ATC_DST_ADDR_MODE_FIXED
703 | ATC_SRC_ADDR_MODE_INCR
705 | ATC_SIF(AT_DMA_MEM_IF) | ATC_DIF(AT_DMA_PER_IF);
706 reg = atslave->tx_reg;
707 for_each_sg(sgl, sg, sg_len, i) {
708 struct at_desc *desc;
712 desc = atc_desc_get(atchan);
716 mem = sg_dma_address(sg);
717 len = sg_dma_len(sg);
719 if (unlikely(mem & 3 || len & 3))
722 desc->lli.saddr = mem;
723 desc->lli.daddr = reg;
724 desc->lli.ctrla = ctrla
725 | ATC_SRC_WIDTH(mem_width)
727 desc->lli.ctrlb = ctrlb;
732 /* inform the HW lli about chaining */
733 prev->lli.dscr = desc->txd.phys;
734 /* insert the link descriptor to the LD ring */
735 list_add_tail(&desc->desc_node,
742 case DMA_FROM_DEVICE:
743 ctrla |= ATC_SRC_WIDTH(reg_width);
744 ctrlb |= ATC_DST_ADDR_MODE_INCR
745 | ATC_SRC_ADDR_MODE_FIXED
747 | ATC_SIF(AT_DMA_PER_IF) | ATC_DIF(AT_DMA_MEM_IF);
749 reg = atslave->rx_reg;
750 for_each_sg(sgl, sg, sg_len, i) {
751 struct at_desc *desc;
755 desc = atc_desc_get(atchan);
759 mem = sg_dma_address(sg);
760 len = sg_dma_len(sg);
762 if (unlikely(mem & 3 || len & 3))
765 desc->lli.saddr = reg;
766 desc->lli.daddr = mem;
767 desc->lli.ctrla = ctrla
768 | ATC_DST_WIDTH(mem_width)
770 desc->lli.ctrlb = ctrlb;
775 /* inform the HW lli about chaining */
776 prev->lli.dscr = desc->txd.phys;
777 /* insert the link descriptor to the LD ring */
778 list_add_tail(&desc->desc_node,
789 /* set end-of-link to the last link descriptor of list*/
792 /* First descriptor of the chain embedds additional information */
793 first->txd.cookie = -EBUSY;
794 first->len = total_len;
796 /* first link descriptor of list is responsible of flags */
797 first->txd.flags = flags; /* client is in control of this ack */
802 dev_err(chan2dev(chan), "not enough descriptors available\n");
803 atc_desc_put(atchan, first);
808 * atc_dma_cyclic_check_values
809 * Check for too big/unaligned periods and unaligned DMA buffer
812 atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
813 size_t period_len, enum dma_data_direction direction)
815 if (period_len > (ATC_BTSIZE_MAX << reg_width))
817 if (unlikely(period_len & ((1 << reg_width) - 1)))
819 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
821 if (unlikely(!(direction & (DMA_TO_DEVICE | DMA_FROM_DEVICE))))
831 * atc_dma_cyclic_fill_desc - Fill one period decriptor
834 atc_dma_cyclic_fill_desc(struct at_dma_slave *atslave, struct at_desc *desc,
835 unsigned int period_index, dma_addr_t buf_addr,
836 size_t period_len, enum dma_data_direction direction)
839 unsigned int reg_width = atslave->reg_width;
841 /* prepare common CRTLA value */
842 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla
843 | ATC_DST_WIDTH(reg_width)
844 | ATC_SRC_WIDTH(reg_width)
845 | period_len >> reg_width;
849 desc->lli.saddr = buf_addr + (period_len * period_index);
850 desc->lli.daddr = atslave->tx_reg;
851 desc->lli.ctrla = ctrla;
852 desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
853 | ATC_SRC_ADDR_MODE_INCR
855 | ATC_SIF(AT_DMA_MEM_IF)
856 | ATC_DIF(AT_DMA_PER_IF);
859 case DMA_FROM_DEVICE:
860 desc->lli.saddr = atslave->rx_reg;
861 desc->lli.daddr = buf_addr + (period_len * period_index);
862 desc->lli.ctrla = ctrla;
863 desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
864 | ATC_SRC_ADDR_MODE_FIXED
866 | ATC_SIF(AT_DMA_PER_IF)
867 | ATC_DIF(AT_DMA_MEM_IF);
878 * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
879 * @chan: the DMA channel to prepare
880 * @buf_addr: physical DMA address where the buffer starts
881 * @buf_len: total number of bytes for the entire buffer
882 * @period_len: number of bytes for each period
883 * @direction: transfer direction, to or from device
885 static struct dma_async_tx_descriptor *
886 atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
887 size_t period_len, enum dma_data_direction direction)
889 struct at_dma_chan *atchan = to_at_dma_chan(chan);
890 struct at_dma_slave *atslave = chan->private;
891 struct at_desc *first = NULL;
892 struct at_desc *prev = NULL;
893 unsigned long was_cyclic;
894 unsigned int periods = buf_len / period_len;
897 dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
898 direction == DMA_TO_DEVICE ? "TO DEVICE" : "FROM DEVICE",
900 periods, buf_len, period_len);
902 if (unlikely(!atslave || !buf_len || !period_len)) {
903 dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
907 was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
909 dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
913 /* Check for too big/unaligned periods and unaligned DMA buffer */
914 if (atc_dma_cyclic_check_values(atslave->reg_width, buf_addr,
915 period_len, direction))
918 /* build cyclic linked list */
919 for (i = 0; i < periods; i++) {
920 struct at_desc *desc;
922 desc = atc_desc_get(atchan);
926 if (atc_dma_cyclic_fill_desc(atslave, desc, i, buf_addr,
927 period_len, direction))
930 atc_desc_chain(&first, &prev, desc);
933 /* lets make a cyclic list */
934 prev->lli.dscr = first->txd.phys;
936 /* First descriptor of the chain embedds additional information */
937 first->txd.cookie = -EBUSY;
938 first->len = buf_len;
943 dev_err(chan2dev(chan), "not enough descriptors available\n");
944 atc_desc_put(atchan, first);
946 clear_bit(ATC_IS_CYCLIC, &atchan->status);
951 static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
954 struct at_dma_chan *atchan = to_at_dma_chan(chan);
955 struct at_dma *atdma = to_at_dma(chan->device);
956 int chan_id = atchan->chan_common.chan_id;
960 dev_vdbg(chan2dev(chan), "atc_control (%d)\n", cmd);
962 if (cmd == DMA_PAUSE) {
963 int pause_timeout = 1000;
965 spin_lock_bh(&atchan->lock);
967 dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
969 /* wait for FIFO to be empty */
970 while (!(dma_readl(atdma, CHSR) & AT_DMA_EMPT(chan_id))) {
971 if (pause_timeout-- > 0) {
972 /* the FIFO can only drain if the peripheral
973 * is still requesting data:
974 * -> timeout if it is not the case. */
975 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
976 spin_unlock_bh(&atchan->lock);
982 set_bit(ATC_IS_PAUSED, &atchan->status);
984 spin_unlock_bh(&atchan->lock);
985 } else if (cmd == DMA_RESUME) {
986 if (!test_bit(ATC_IS_PAUSED, &atchan->status))
989 spin_lock_bh(&atchan->lock);
991 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
992 clear_bit(ATC_IS_PAUSED, &atchan->status);
994 spin_unlock_bh(&atchan->lock);
995 } else if (cmd == DMA_TERMINATE_ALL) {
996 struct at_desc *desc, *_desc;
998 * This is only called when something went wrong elsewhere, so
999 * we don't really care about the data. Just disable the
1000 * channel. We still have to poll the channel enable bit due
1001 * to AHB/HSB limitations.
1003 spin_lock_bh(&atchan->lock);
1005 /* disabling channel: must also remove suspend state */
1006 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
1008 /* confirm that this channel is disabled */
1009 while (dma_readl(atdma, CHSR) & atchan->mask)
1012 /* active_list entries will end up before queued entries */
1013 list_splice_init(&atchan->queue, &list);
1014 list_splice_init(&atchan->active_list, &list);
1016 /* Flush all pending and queued descriptors */
1017 list_for_each_entry_safe(desc, _desc, &list, desc_node)
1018 atc_chain_complete(atchan, desc);
1020 clear_bit(ATC_IS_PAUSED, &atchan->status);
1021 /* if channel dedicated to cyclic operations, free it */
1022 clear_bit(ATC_IS_CYCLIC, &atchan->status);
1024 spin_unlock_bh(&atchan->lock);
1033 * atc_tx_status - poll for transaction completion
1034 * @chan: DMA channel
1035 * @cookie: transaction identifier to check status of
1036 * @txstate: if not %NULL updated with transaction state
1038 * If @txstate is passed in, upon return it reflect the driver
1039 * internal state and can be used with dma_async_is_complete() to check
1040 * the status of multiple cookies without re-checking hardware state.
1042 static enum dma_status
1043 atc_tx_status(struct dma_chan *chan,
1044 dma_cookie_t cookie,
1045 struct dma_tx_state *txstate)
1047 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1048 dma_cookie_t last_used;
1049 dma_cookie_t last_complete;
1050 enum dma_status ret;
1052 spin_lock_bh(&atchan->lock);
1054 last_complete = atchan->completed_cookie;
1055 last_used = chan->cookie;
1057 ret = dma_async_is_complete(cookie, last_complete, last_used);
1058 if (ret != DMA_SUCCESS) {
1059 atc_cleanup_descriptors(atchan);
1061 last_complete = atchan->completed_cookie;
1062 last_used = chan->cookie;
1064 ret = dma_async_is_complete(cookie, last_complete, last_used);
1067 spin_unlock_bh(&atchan->lock);
1069 if (ret != DMA_SUCCESS)
1070 dma_set_tx_state(txstate, last_complete, last_used,
1071 atc_first_active(atchan)->len);
1073 dma_set_tx_state(txstate, last_complete, last_used, 0);
1075 if (test_bit(ATC_IS_PAUSED, &atchan->status))
1078 dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d (d%d, u%d)\n",
1079 ret, cookie, last_complete ? last_complete : 0,
1080 last_used ? last_used : 0);
1086 * atc_issue_pending - try to finish work
1087 * @chan: target DMA channel
1089 static void atc_issue_pending(struct dma_chan *chan)
1091 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1093 dev_vdbg(chan2dev(chan), "issue_pending\n");
1095 /* Not needed for cyclic transfers */
1096 if (test_bit(ATC_IS_CYCLIC, &atchan->status))
1099 spin_lock_bh(&atchan->lock);
1100 if (!atc_chan_is_enabled(atchan)) {
1101 atc_advance_work(atchan);
1103 spin_unlock_bh(&atchan->lock);
1107 * atc_alloc_chan_resources - allocate resources for DMA channel
1108 * @chan: allocate descriptor resources for this channel
1109 * @client: current client requesting the channel be ready for requests
1111 * return - the number of allocated descriptors
1113 static int atc_alloc_chan_resources(struct dma_chan *chan)
1115 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1116 struct at_dma *atdma = to_at_dma(chan->device);
1117 struct at_desc *desc;
1118 struct at_dma_slave *atslave;
1121 LIST_HEAD(tmp_list);
1123 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
1125 /* ASSERT: channel is idle */
1126 if (atc_chan_is_enabled(atchan)) {
1127 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
1131 cfg = ATC_DEFAULT_CFG;
1133 atslave = chan->private;
1136 * We need controller-specific data to set up slave
1139 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
1141 /* if cfg configuration specified take it instad of default */
1146 /* have we already been set up?
1147 * reconfigure channel but no need to reallocate descriptors */
1148 if (!list_empty(&atchan->free_list))
1149 return atchan->descs_allocated;
1151 /* Allocate initial pool of descriptors */
1152 for (i = 0; i < init_nr_desc_per_channel; i++) {
1153 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
1155 dev_err(atdma->dma_common.dev,
1156 "Only %d initial descriptors\n", i);
1159 list_add_tail(&desc->desc_node, &tmp_list);
1162 spin_lock_bh(&atchan->lock);
1163 atchan->descs_allocated = i;
1164 list_splice(&tmp_list, &atchan->free_list);
1165 atchan->completed_cookie = chan->cookie = 1;
1166 spin_unlock_bh(&atchan->lock);
1168 /* channel parameters */
1169 channel_writel(atchan, CFG, cfg);
1171 dev_dbg(chan2dev(chan),
1172 "alloc_chan_resources: allocated %d descriptors\n",
1173 atchan->descs_allocated);
1175 return atchan->descs_allocated;
1179 * atc_free_chan_resources - free all channel resources
1180 * @chan: DMA channel
1182 static void atc_free_chan_resources(struct dma_chan *chan)
1184 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1185 struct at_dma *atdma = to_at_dma(chan->device);
1186 struct at_desc *desc, *_desc;
1189 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
1190 atchan->descs_allocated);
1192 /* ASSERT: channel is idle */
1193 BUG_ON(!list_empty(&atchan->active_list));
1194 BUG_ON(!list_empty(&atchan->queue));
1195 BUG_ON(atc_chan_is_enabled(atchan));
1197 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
1198 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1199 list_del(&desc->desc_node);
1200 /* free link descriptor */
1201 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
1203 list_splice_init(&atchan->free_list, &list);
1204 atchan->descs_allocated = 0;
1207 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
1211 /*-- Module Management -----------------------------------------------*/
1214 * at_dma_off - disable DMA controller
1215 * @atdma: the Atmel HDAMC device
1217 static void at_dma_off(struct at_dma *atdma)
1219 dma_writel(atdma, EN, 0);
1221 /* disable all interrupts */
1222 dma_writel(atdma, EBCIDR, -1L);
1224 /* confirm that all channels are disabled */
1225 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
1229 static int __init at_dma_probe(struct platform_device *pdev)
1231 struct at_dma_platform_data *pdata;
1232 struct resource *io;
1233 struct at_dma *atdma;
1239 /* get DMA Controller parameters from platform */
1240 pdata = pdev->dev.platform_data;
1241 if (!pdata || pdata->nr_channels > AT_DMA_MAX_NR_CHANNELS)
1244 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1248 irq = platform_get_irq(pdev, 0);
1252 size = sizeof(struct at_dma);
1253 size += pdata->nr_channels * sizeof(struct at_dma_chan);
1254 atdma = kzalloc(size, GFP_KERNEL);
1258 /* discover transaction capabilites from the platform data */
1259 atdma->dma_common.cap_mask = pdata->cap_mask;
1260 atdma->all_chan_mask = (1 << pdata->nr_channels) - 1;
1262 size = io->end - io->start + 1;
1263 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1268 atdma->regs = ioremap(io->start, size);
1274 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1275 if (IS_ERR(atdma->clk)) {
1276 err = PTR_ERR(atdma->clk);
1279 clk_enable(atdma->clk);
1281 /* force dma off, just in case */
1284 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1288 platform_set_drvdata(pdev, atdma);
1290 /* create a pool of consistent memory blocks for hardware descriptors */
1291 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1292 &pdev->dev, sizeof(struct at_desc),
1293 4 /* word alignment */, 0);
1294 if (!atdma->dma_desc_pool) {
1295 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1297 goto err_pool_create;
1300 /* clear any pending interrupt */
1301 while (dma_readl(atdma, EBCISR))
1304 /* initialize channels related values */
1305 INIT_LIST_HEAD(&atdma->dma_common.channels);
1306 for (i = 0; i < pdata->nr_channels; i++, atdma->dma_common.chancnt++) {
1307 struct at_dma_chan *atchan = &atdma->chan[i];
1309 atchan->chan_common.device = &atdma->dma_common;
1310 atchan->chan_common.cookie = atchan->completed_cookie = 1;
1311 atchan->chan_common.chan_id = i;
1312 list_add_tail(&atchan->chan_common.device_node,
1313 &atdma->dma_common.channels);
1315 atchan->ch_regs = atdma->regs + ch_regs(i);
1316 spin_lock_init(&atchan->lock);
1317 atchan->mask = 1 << i;
1319 INIT_LIST_HEAD(&atchan->active_list);
1320 INIT_LIST_HEAD(&atchan->queue);
1321 INIT_LIST_HEAD(&atchan->free_list);
1323 tasklet_init(&atchan->tasklet, atc_tasklet,
1324 (unsigned long)atchan);
1325 atc_enable_irq(atchan);
1328 /* set base routines */
1329 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1330 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
1331 atdma->dma_common.device_tx_status = atc_tx_status;
1332 atdma->dma_common.device_issue_pending = atc_issue_pending;
1333 atdma->dma_common.dev = &pdev->dev;
1335 /* set prep routines based on capability */
1336 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1337 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1339 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask))
1340 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
1342 if (dma_has_cap(DMA_CYCLIC, atdma->dma_common.cap_mask))
1343 atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
1345 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ||
1346 dma_has_cap(DMA_CYCLIC, atdma->dma_common.cap_mask))
1347 atdma->dma_common.device_control = atc_control;
1349 dma_writel(atdma, EN, AT_DMA_ENABLE);
1351 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
1352 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1353 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
1354 atdma->dma_common.chancnt);
1356 dma_async_device_register(&atdma->dma_common);
1361 platform_set_drvdata(pdev, NULL);
1362 free_irq(platform_get_irq(pdev, 0), atdma);
1364 clk_disable(atdma->clk);
1365 clk_put(atdma->clk);
1367 iounmap(atdma->regs);
1370 release_mem_region(io->start, size);
1376 static int __exit at_dma_remove(struct platform_device *pdev)
1378 struct at_dma *atdma = platform_get_drvdata(pdev);
1379 struct dma_chan *chan, *_chan;
1380 struct resource *io;
1383 dma_async_device_unregister(&atdma->dma_common);
1385 dma_pool_destroy(atdma->dma_desc_pool);
1386 platform_set_drvdata(pdev, NULL);
1387 free_irq(platform_get_irq(pdev, 0), atdma);
1389 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1391 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1393 /* Disable interrupts */
1394 atc_disable_irq(atchan);
1395 tasklet_disable(&atchan->tasklet);
1397 tasklet_kill(&atchan->tasklet);
1398 list_del(&chan->device_node);
1401 clk_disable(atdma->clk);
1402 clk_put(atdma->clk);
1404 iounmap(atdma->regs);
1407 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1408 release_mem_region(io->start, io->end - io->start + 1);
1415 static void at_dma_shutdown(struct platform_device *pdev)
1417 struct at_dma *atdma = platform_get_drvdata(pdev);
1419 at_dma_off(platform_get_drvdata(pdev));
1420 clk_disable(atdma->clk);
1423 static int at_dma_suspend_noirq(struct device *dev)
1425 struct platform_device *pdev = to_platform_device(dev);
1426 struct at_dma *atdma = platform_get_drvdata(pdev);
1428 at_dma_off(platform_get_drvdata(pdev));
1429 clk_disable(atdma->clk);
1433 static int at_dma_resume_noirq(struct device *dev)
1435 struct platform_device *pdev = to_platform_device(dev);
1436 struct at_dma *atdma = platform_get_drvdata(pdev);
1438 clk_enable(atdma->clk);
1439 dma_writel(atdma, EN, AT_DMA_ENABLE);
1443 static const struct dev_pm_ops at_dma_dev_pm_ops = {
1444 .suspend_noirq = at_dma_suspend_noirq,
1445 .resume_noirq = at_dma_resume_noirq,
1448 static struct platform_driver at_dma_driver = {
1449 .remove = __exit_p(at_dma_remove),
1450 .shutdown = at_dma_shutdown,
1453 .pm = &at_dma_dev_pm_ops,
1457 static int __init at_dma_init(void)
1459 return platform_driver_probe(&at_dma_driver, at_dma_probe);
1461 subsys_initcall(at_dma_init);
1463 static void __exit at_dma_exit(void)
1465 platform_driver_unregister(&at_dma_driver);
1467 module_exit(at_dma_exit);
1469 MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1470 MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1471 MODULE_LICENSE("GPL");
1472 MODULE_ALIAS("platform:at_hdmac");