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(0) \
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 /* Channel cannot be enabled here */
476 if (atc_chan_is_enabled(atchan)) {
477 dev_err(chan2dev(&atchan->chan_common),
478 "BUG: channel enabled in tasklet\n");
482 spin_lock(&atchan->lock);
483 if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
484 atc_handle_error(atchan);
485 else if (test_bit(ATC_IS_CYCLIC, &atchan->status))
486 atc_handle_cyclic(atchan);
488 atc_advance_work(atchan);
490 spin_unlock(&atchan->lock);
493 static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
495 struct at_dma *atdma = (struct at_dma *)dev_id;
496 struct at_dma_chan *atchan;
498 u32 status, pending, imr;
502 imr = dma_readl(atdma, EBCIMR);
503 status = dma_readl(atdma, EBCISR);
504 pending = status & imr;
509 dev_vdbg(atdma->dma_common.dev,
510 "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
511 status, imr, pending);
513 for (i = 0; i < atdma->dma_common.chancnt; i++) {
514 atchan = &atdma->chan[i];
515 if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
516 if (pending & AT_DMA_ERR(i)) {
517 /* Disable channel on AHB error */
518 dma_writel(atdma, CHDR, atchan->mask);
519 /* Give information to tasklet */
520 set_bit(ATC_IS_ERROR, &atchan->status);
522 tasklet_schedule(&atchan->tasklet);
533 /*-- DMA Engine API --------------------------------------------------*/
536 * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
537 * @desc: descriptor at the head of the transaction chain
539 * Queue chain if DMA engine is working already
541 * Cookie increment and adding to active_list or queue must be atomic
543 static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
545 struct at_desc *desc = txd_to_at_desc(tx);
546 struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
549 spin_lock_bh(&atchan->lock);
550 cookie = atc_assign_cookie(atchan, desc);
552 if (list_empty(&atchan->active_list)) {
553 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
555 atc_dostart(atchan, desc);
556 list_add_tail(&desc->desc_node, &atchan->active_list);
558 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
560 list_add_tail(&desc->desc_node, &atchan->queue);
563 spin_unlock_bh(&atchan->lock);
569 * atc_prep_dma_memcpy - prepare a memcpy operation
570 * @chan: the channel to prepare operation on
571 * @dest: operation virtual destination address
572 * @src: operation virtual source address
573 * @len: operation length
574 * @flags: tx descriptor status flags
576 static struct dma_async_tx_descriptor *
577 atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
578 size_t len, unsigned long flags)
580 struct at_dma_chan *atchan = to_at_dma_chan(chan);
581 struct at_desc *desc = NULL;
582 struct at_desc *first = NULL;
583 struct at_desc *prev = NULL;
586 unsigned int src_width;
587 unsigned int dst_width;
591 dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
592 dest, src, len, flags);
594 if (unlikely(!len)) {
595 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
599 ctrla = ATC_DEFAULT_CTRLA;
600 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
601 | ATC_SRC_ADDR_MODE_INCR
602 | ATC_DST_ADDR_MODE_INCR
606 * We can be a lot more clever here, but this should take care
607 * of the most common optimization.
609 if (!((src | dest | len) & 3)) {
610 ctrla |= ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
611 src_width = dst_width = 2;
612 } else if (!((src | dest | len) & 1)) {
613 ctrla |= ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
614 src_width = dst_width = 1;
616 ctrla |= ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
617 src_width = dst_width = 0;
620 for (offset = 0; offset < len; offset += xfer_count << src_width) {
621 xfer_count = min_t(size_t, (len - offset) >> src_width,
624 desc = atc_desc_get(atchan);
628 desc->lli.saddr = src + offset;
629 desc->lli.daddr = dest + offset;
630 desc->lli.ctrla = ctrla | xfer_count;
631 desc->lli.ctrlb = ctrlb;
633 desc->txd.cookie = 0;
638 /* inform the HW lli about chaining */
639 prev->lli.dscr = desc->txd.phys;
640 /* insert the link descriptor to the LD ring */
641 list_add_tail(&desc->desc_node,
647 /* First descriptor of the chain embedds additional information */
648 first->txd.cookie = -EBUSY;
651 /* set end-of-link to the last link descriptor of list*/
654 first->txd.flags = flags; /* client is in control of this ack */
659 atc_desc_put(atchan, first);
665 * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
667 * @sgl: scatterlist to transfer to/from
668 * @sg_len: number of entries in @scatterlist
669 * @direction: DMA direction
670 * @flags: tx descriptor status flags
672 static struct dma_async_tx_descriptor *
673 atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
674 unsigned int sg_len, enum dma_data_direction direction,
677 struct at_dma_chan *atchan = to_at_dma_chan(chan);
678 struct at_dma_slave *atslave = chan->private;
679 struct at_desc *first = NULL;
680 struct at_desc *prev = NULL;
684 unsigned int reg_width;
685 unsigned int mem_width;
687 struct scatterlist *sg;
688 size_t total_len = 0;
690 dev_vdbg(chan2dev(chan), "prep_slave_sg: %s f0x%lx\n",
691 direction == DMA_TO_DEVICE ? "TO DEVICE" : "FROM DEVICE",
694 if (unlikely(!atslave || !sg_len)) {
695 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
699 reg_width = atslave->reg_width;
701 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla;
702 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN;
706 ctrla |= ATC_DST_WIDTH(reg_width);
707 ctrlb |= ATC_DST_ADDR_MODE_FIXED
708 | ATC_SRC_ADDR_MODE_INCR
710 reg = atslave->tx_reg;
711 for_each_sg(sgl, sg, sg_len, i) {
712 struct at_desc *desc;
716 desc = atc_desc_get(atchan);
720 mem = sg_dma_address(sg);
721 len = sg_dma_len(sg);
723 if (unlikely(mem & 3 || len & 3))
726 desc->lli.saddr = mem;
727 desc->lli.daddr = reg;
728 desc->lli.ctrla = ctrla
729 | ATC_SRC_WIDTH(mem_width)
731 desc->lli.ctrlb = ctrlb;
736 /* inform the HW lli about chaining */
737 prev->lli.dscr = desc->txd.phys;
738 /* insert the link descriptor to the LD ring */
739 list_add_tail(&desc->desc_node,
746 case DMA_FROM_DEVICE:
747 ctrla |= ATC_SRC_WIDTH(reg_width);
748 ctrlb |= ATC_DST_ADDR_MODE_INCR
749 | ATC_SRC_ADDR_MODE_FIXED
752 reg = atslave->rx_reg;
753 for_each_sg(sgl, sg, sg_len, i) {
754 struct at_desc *desc;
758 desc = atc_desc_get(atchan);
762 mem = sg_dma_address(sg);
763 len = sg_dma_len(sg);
765 if (unlikely(mem & 3 || len & 3))
768 desc->lli.saddr = reg;
769 desc->lli.daddr = mem;
770 desc->lli.ctrla = ctrla
771 | ATC_DST_WIDTH(mem_width)
773 desc->lli.ctrlb = ctrlb;
778 /* inform the HW lli about chaining */
779 prev->lli.dscr = desc->txd.phys;
780 /* insert the link descriptor to the LD ring */
781 list_add_tail(&desc->desc_node,
792 /* set end-of-link to the last link descriptor of list*/
795 /* First descriptor of the chain embedds additional information */
796 first->txd.cookie = -EBUSY;
797 first->len = total_len;
799 /* first link descriptor of list is responsible of flags */
800 first->txd.flags = flags; /* client is in control of this ack */
805 dev_err(chan2dev(chan), "not enough descriptors available\n");
806 atc_desc_put(atchan, first);
811 * atc_dma_cyclic_check_values
812 * Check for too big/unaligned periods and unaligned DMA buffer
815 atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
816 size_t period_len, enum dma_data_direction direction)
818 if (period_len > (ATC_BTSIZE_MAX << reg_width))
820 if (unlikely(period_len & ((1 << reg_width) - 1)))
822 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
824 if (unlikely(!(direction & (DMA_TO_DEVICE | DMA_FROM_DEVICE))))
834 * atc_dma_cyclic_fill_desc - Fill one period decriptor
837 atc_dma_cyclic_fill_desc(struct at_dma_slave *atslave, struct at_desc *desc,
838 unsigned int period_index, dma_addr_t buf_addr,
839 size_t period_len, enum dma_data_direction direction)
842 unsigned int reg_width = atslave->reg_width;
844 /* prepare common CRTLA value */
845 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla
846 | ATC_DST_WIDTH(reg_width)
847 | ATC_SRC_WIDTH(reg_width)
848 | period_len >> reg_width;
852 desc->lli.saddr = buf_addr + (period_len * period_index);
853 desc->lli.daddr = atslave->tx_reg;
854 desc->lli.ctrla = ctrla;
855 desc->lli.ctrlb = ATC_DEFAULT_CTRLB
856 | ATC_DST_ADDR_MODE_FIXED
857 | ATC_SRC_ADDR_MODE_INCR
861 case DMA_FROM_DEVICE:
862 desc->lli.saddr = atslave->rx_reg;
863 desc->lli.daddr = buf_addr + (period_len * period_index);
864 desc->lli.ctrla = ctrla;
865 desc->lli.ctrlb = ATC_DEFAULT_CTRLB
866 | ATC_DST_ADDR_MODE_INCR
867 | ATC_SRC_ADDR_MODE_FIXED
879 * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
880 * @chan: the DMA channel to prepare
881 * @buf_addr: physical DMA address where the buffer starts
882 * @buf_len: total number of bytes for the entire buffer
883 * @period_len: number of bytes for each period
884 * @direction: transfer direction, to or from device
886 static struct dma_async_tx_descriptor *
887 atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
888 size_t period_len, enum dma_data_direction direction)
890 struct at_dma_chan *atchan = to_at_dma_chan(chan);
891 struct at_dma_slave *atslave = chan->private;
892 struct at_desc *first = NULL;
893 struct at_desc *prev = NULL;
894 unsigned long was_cyclic;
895 unsigned int periods = buf_len / period_len;
898 dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
899 direction == DMA_TO_DEVICE ? "TO DEVICE" : "FROM DEVICE",
901 periods, buf_len, period_len);
903 if (unlikely(!atslave || !buf_len || !period_len)) {
904 dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
908 was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
910 dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
914 /* Check for too big/unaligned periods and unaligned DMA buffer */
915 if (atc_dma_cyclic_check_values(atslave->reg_width, buf_addr,
916 period_len, direction))
919 /* build cyclic linked list */
920 for (i = 0; i < periods; i++) {
921 struct at_desc *desc;
923 desc = atc_desc_get(atchan);
927 if (atc_dma_cyclic_fill_desc(atslave, desc, i, buf_addr,
928 period_len, direction))
931 atc_desc_chain(&first, &prev, desc);
934 /* lets make a cyclic list */
935 prev->lli.dscr = first->txd.phys;
937 /* First descriptor of the chain embedds additional information */
938 first->txd.cookie = -EBUSY;
939 first->len = buf_len;
944 dev_err(chan2dev(chan), "not enough descriptors available\n");
945 atc_desc_put(atchan, first);
947 clear_bit(ATC_IS_CYCLIC, &atchan->status);
952 static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
955 struct at_dma_chan *atchan = to_at_dma_chan(chan);
956 struct at_dma *atdma = to_at_dma(chan->device);
957 struct at_desc *desc, *_desc;
960 /* Only supports DMA_TERMINATE_ALL */
961 if (cmd != DMA_TERMINATE_ALL)
965 * This is only called when something went wrong elsewhere, so
966 * we don't really care about the data. Just disable the
967 * channel. We still have to poll the channel enable bit due
968 * to AHB/HSB limitations.
970 spin_lock_bh(&atchan->lock);
972 dma_writel(atdma, CHDR, atchan->mask);
974 /* confirm that this channel is disabled */
975 while (dma_readl(atdma, CHSR) & atchan->mask)
978 /* active_list entries will end up before queued entries */
979 list_splice_init(&atchan->queue, &list);
980 list_splice_init(&atchan->active_list, &list);
982 /* Flush all pending and queued descriptors */
983 list_for_each_entry_safe(desc, _desc, &list, desc_node)
984 atc_chain_complete(atchan, desc);
986 /* if channel dedicated to cyclic operations, free it */
987 clear_bit(ATC_IS_CYCLIC, &atchan->status);
989 spin_unlock_bh(&atchan->lock);
995 * atc_tx_status - poll for transaction completion
997 * @cookie: transaction identifier to check status of
998 * @txstate: if not %NULL updated with transaction state
1000 * If @txstate is passed in, upon return it reflect the driver
1001 * internal state and can be used with dma_async_is_complete() to check
1002 * the status of multiple cookies without re-checking hardware state.
1004 static enum dma_status
1005 atc_tx_status(struct dma_chan *chan,
1006 dma_cookie_t cookie,
1007 struct dma_tx_state *txstate)
1009 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1010 dma_cookie_t last_used;
1011 dma_cookie_t last_complete;
1012 enum dma_status ret;
1014 spin_lock_bh(&atchan->lock);
1016 last_complete = atchan->completed_cookie;
1017 last_used = chan->cookie;
1019 ret = dma_async_is_complete(cookie, last_complete, last_used);
1020 if (ret != DMA_SUCCESS) {
1021 atc_cleanup_descriptors(atchan);
1023 last_complete = atchan->completed_cookie;
1024 last_used = chan->cookie;
1026 ret = dma_async_is_complete(cookie, last_complete, last_used);
1029 spin_unlock_bh(&atchan->lock);
1031 dma_set_tx_state(txstate, last_complete, last_used, 0);
1032 dev_vdbg(chan2dev(chan), "tx_status: %d (d%d, u%d)\n",
1033 cookie, last_complete ? last_complete : 0,
1034 last_used ? last_used : 0);
1040 * atc_issue_pending - try to finish work
1041 * @chan: target DMA channel
1043 static void atc_issue_pending(struct dma_chan *chan)
1045 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1047 dev_vdbg(chan2dev(chan), "issue_pending\n");
1049 /* Not needed for cyclic transfers */
1050 if (test_bit(ATC_IS_CYCLIC, &atchan->status))
1053 spin_lock_bh(&atchan->lock);
1054 if (!atc_chan_is_enabled(atchan)) {
1055 atc_advance_work(atchan);
1057 spin_unlock_bh(&atchan->lock);
1061 * atc_alloc_chan_resources - allocate resources for DMA channel
1062 * @chan: allocate descriptor resources for this channel
1063 * @client: current client requesting the channel be ready for requests
1065 * return - the number of allocated descriptors
1067 static int atc_alloc_chan_resources(struct dma_chan *chan)
1069 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1070 struct at_dma *atdma = to_at_dma(chan->device);
1071 struct at_desc *desc;
1072 struct at_dma_slave *atslave;
1075 LIST_HEAD(tmp_list);
1077 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
1079 /* ASSERT: channel is idle */
1080 if (atc_chan_is_enabled(atchan)) {
1081 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
1085 cfg = ATC_DEFAULT_CFG;
1087 atslave = chan->private;
1090 * We need controller-specific data to set up slave
1093 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
1095 /* if cfg configuration specified take it instad of default */
1100 /* have we already been set up?
1101 * reconfigure channel but no need to reallocate descriptors */
1102 if (!list_empty(&atchan->free_list))
1103 return atchan->descs_allocated;
1105 /* Allocate initial pool of descriptors */
1106 for (i = 0; i < init_nr_desc_per_channel; i++) {
1107 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
1109 dev_err(atdma->dma_common.dev,
1110 "Only %d initial descriptors\n", i);
1113 list_add_tail(&desc->desc_node, &tmp_list);
1116 spin_lock_bh(&atchan->lock);
1117 atchan->descs_allocated = i;
1118 list_splice(&tmp_list, &atchan->free_list);
1119 atchan->completed_cookie = chan->cookie = 1;
1120 spin_unlock_bh(&atchan->lock);
1122 /* channel parameters */
1123 channel_writel(atchan, CFG, cfg);
1125 dev_dbg(chan2dev(chan),
1126 "alloc_chan_resources: allocated %d descriptors\n",
1127 atchan->descs_allocated);
1129 return atchan->descs_allocated;
1133 * atc_free_chan_resources - free all channel resources
1134 * @chan: DMA channel
1136 static void atc_free_chan_resources(struct dma_chan *chan)
1138 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1139 struct at_dma *atdma = to_at_dma(chan->device);
1140 struct at_desc *desc, *_desc;
1143 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
1144 atchan->descs_allocated);
1146 /* ASSERT: channel is idle */
1147 BUG_ON(!list_empty(&atchan->active_list));
1148 BUG_ON(!list_empty(&atchan->queue));
1149 BUG_ON(atc_chan_is_enabled(atchan));
1151 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
1152 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1153 list_del(&desc->desc_node);
1154 /* free link descriptor */
1155 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
1157 list_splice_init(&atchan->free_list, &list);
1158 atchan->descs_allocated = 0;
1161 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
1165 /*-- Module Management -----------------------------------------------*/
1168 * at_dma_off - disable DMA controller
1169 * @atdma: the Atmel HDAMC device
1171 static void at_dma_off(struct at_dma *atdma)
1173 dma_writel(atdma, EN, 0);
1175 /* disable all interrupts */
1176 dma_writel(atdma, EBCIDR, -1L);
1178 /* confirm that all channels are disabled */
1179 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
1183 static int __init at_dma_probe(struct platform_device *pdev)
1185 struct at_dma_platform_data *pdata;
1186 struct resource *io;
1187 struct at_dma *atdma;
1193 /* get DMA Controller parameters from platform */
1194 pdata = pdev->dev.platform_data;
1195 if (!pdata || pdata->nr_channels > AT_DMA_MAX_NR_CHANNELS)
1198 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1202 irq = platform_get_irq(pdev, 0);
1206 size = sizeof(struct at_dma);
1207 size += pdata->nr_channels * sizeof(struct at_dma_chan);
1208 atdma = kzalloc(size, GFP_KERNEL);
1212 /* discover transaction capabilites from the platform data */
1213 atdma->dma_common.cap_mask = pdata->cap_mask;
1214 atdma->all_chan_mask = (1 << pdata->nr_channels) - 1;
1216 size = io->end - io->start + 1;
1217 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1222 atdma->regs = ioremap(io->start, size);
1228 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1229 if (IS_ERR(atdma->clk)) {
1230 err = PTR_ERR(atdma->clk);
1233 clk_enable(atdma->clk);
1235 /* force dma off, just in case */
1238 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1242 platform_set_drvdata(pdev, atdma);
1244 /* create a pool of consistent memory blocks for hardware descriptors */
1245 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1246 &pdev->dev, sizeof(struct at_desc),
1247 4 /* word alignment */, 0);
1248 if (!atdma->dma_desc_pool) {
1249 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1251 goto err_pool_create;
1254 /* clear any pending interrupt */
1255 while (dma_readl(atdma, EBCISR))
1258 /* initialize channels related values */
1259 INIT_LIST_HEAD(&atdma->dma_common.channels);
1260 for (i = 0; i < pdata->nr_channels; i++, atdma->dma_common.chancnt++) {
1261 struct at_dma_chan *atchan = &atdma->chan[i];
1263 atchan->chan_common.device = &atdma->dma_common;
1264 atchan->chan_common.cookie = atchan->completed_cookie = 1;
1265 atchan->chan_common.chan_id = i;
1266 list_add_tail(&atchan->chan_common.device_node,
1267 &atdma->dma_common.channels);
1269 atchan->ch_regs = atdma->regs + ch_regs(i);
1270 spin_lock_init(&atchan->lock);
1271 atchan->mask = 1 << i;
1273 INIT_LIST_HEAD(&atchan->active_list);
1274 INIT_LIST_HEAD(&atchan->queue);
1275 INIT_LIST_HEAD(&atchan->free_list);
1277 tasklet_init(&atchan->tasklet, atc_tasklet,
1278 (unsigned long)atchan);
1279 atc_enable_irq(atchan);
1282 /* set base routines */
1283 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1284 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
1285 atdma->dma_common.device_tx_status = atc_tx_status;
1286 atdma->dma_common.device_issue_pending = atc_issue_pending;
1287 atdma->dma_common.dev = &pdev->dev;
1289 /* set prep routines based on capability */
1290 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1291 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1293 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask))
1294 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
1296 if (dma_has_cap(DMA_CYCLIC, atdma->dma_common.cap_mask))
1297 atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
1299 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ||
1300 dma_has_cap(DMA_CYCLIC, atdma->dma_common.cap_mask))
1301 atdma->dma_common.device_control = atc_control;
1303 dma_writel(atdma, EN, AT_DMA_ENABLE);
1305 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
1306 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1307 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
1308 atdma->dma_common.chancnt);
1310 dma_async_device_register(&atdma->dma_common);
1315 platform_set_drvdata(pdev, NULL);
1316 free_irq(platform_get_irq(pdev, 0), atdma);
1318 clk_disable(atdma->clk);
1319 clk_put(atdma->clk);
1321 iounmap(atdma->regs);
1324 release_mem_region(io->start, size);
1330 static int __exit at_dma_remove(struct platform_device *pdev)
1332 struct at_dma *atdma = platform_get_drvdata(pdev);
1333 struct dma_chan *chan, *_chan;
1334 struct resource *io;
1337 dma_async_device_unregister(&atdma->dma_common);
1339 dma_pool_destroy(atdma->dma_desc_pool);
1340 platform_set_drvdata(pdev, NULL);
1341 free_irq(platform_get_irq(pdev, 0), atdma);
1343 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1345 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1347 /* Disable interrupts */
1348 atc_disable_irq(atchan);
1349 tasklet_disable(&atchan->tasklet);
1351 tasklet_kill(&atchan->tasklet);
1352 list_del(&chan->device_node);
1355 clk_disable(atdma->clk);
1356 clk_put(atdma->clk);
1358 iounmap(atdma->regs);
1361 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1362 release_mem_region(io->start, io->end - io->start + 1);
1369 static void at_dma_shutdown(struct platform_device *pdev)
1371 struct at_dma *atdma = platform_get_drvdata(pdev);
1373 at_dma_off(platform_get_drvdata(pdev));
1374 clk_disable(atdma->clk);
1377 static int at_dma_suspend_noirq(struct device *dev)
1379 struct platform_device *pdev = to_platform_device(dev);
1380 struct at_dma *atdma = platform_get_drvdata(pdev);
1382 at_dma_off(platform_get_drvdata(pdev));
1383 clk_disable(atdma->clk);
1387 static int at_dma_resume_noirq(struct device *dev)
1389 struct platform_device *pdev = to_platform_device(dev);
1390 struct at_dma *atdma = platform_get_drvdata(pdev);
1392 clk_enable(atdma->clk);
1393 dma_writel(atdma, EN, AT_DMA_ENABLE);
1397 static const struct dev_pm_ops at_dma_dev_pm_ops = {
1398 .suspend_noirq = at_dma_suspend_noirq,
1399 .resume_noirq = at_dma_resume_noirq,
1402 static struct platform_driver at_dma_driver = {
1403 .remove = __exit_p(at_dma_remove),
1404 .shutdown = at_dma_shutdown,
1407 .pm = &at_dma_dev_pm_ops,
1411 static int __init at_dma_init(void)
1413 return platform_driver_probe(&at_dma_driver, at_dma_probe);
1415 subsys_initcall(at_dma_init);
1417 static void __exit at_dma_exit(void)
1419 platform_driver_unregister(&at_dma_driver);
1421 module_exit(at_dma_exit);
1423 MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1424 MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1425 MODULE_LICENSE("GPL");
1426 MODULE_ALIAS("platform:at_hdmac");