spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / drivers / dma / at_hdmac.c
bloba342873526f889621286d2b3fc95480aeeb71db3
1 /*
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>
26 #include <linux/of.h>
27 #include <linux/of_device.h>
29 #include "at_hdmac_regs.h"
32 * Glossary
33 * --------
35 * at_hdmac : Name of the ATmel AHB DMA Controller
36 * at_dma_ / atdma : ATmel DMA controller entity related
37 * atc_ / atchan : ATmel DMA Channel entity related
40 #define ATC_DEFAULT_CFG (ATC_FIFOCFG_HALFFIFO)
41 #define ATC_DEFAULT_CTRLA (0)
42 #define ATC_DEFAULT_CTRLB (ATC_SIF(AT_DMA_MEM_IF) \
43 |ATC_DIF(AT_DMA_MEM_IF))
46 * Initial number of descriptors to allocate for each channel. This could
47 * be increased during dma usage.
49 static unsigned int init_nr_desc_per_channel = 64;
50 module_param(init_nr_desc_per_channel, uint, 0644);
51 MODULE_PARM_DESC(init_nr_desc_per_channel,
52 "initial descriptors per channel (default: 64)");
55 /* prototypes */
56 static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx);
59 /*----------------------------------------------------------------------*/
61 static struct at_desc *atc_first_active(struct at_dma_chan *atchan)
63 return list_first_entry(&atchan->active_list,
64 struct at_desc, desc_node);
67 static struct at_desc *atc_first_queued(struct at_dma_chan *atchan)
69 return list_first_entry(&atchan->queue,
70 struct at_desc, desc_node);
73 /**
74 * atc_alloc_descriptor - allocate and return an initialized descriptor
75 * @chan: the channel to allocate descriptors for
76 * @gfp_flags: GFP allocation flags
78 * Note: The ack-bit is positioned in the descriptor flag at creation time
79 * to make initial allocation more convenient. This bit will be cleared
80 * and control will be given to client at usage time (during
81 * preparation functions).
83 static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan,
84 gfp_t gfp_flags)
86 struct at_desc *desc = NULL;
87 struct at_dma *atdma = to_at_dma(chan->device);
88 dma_addr_t phys;
90 desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys);
91 if (desc) {
92 memset(desc, 0, sizeof(struct at_desc));
93 INIT_LIST_HEAD(&desc->tx_list);
94 dma_async_tx_descriptor_init(&desc->txd, chan);
95 /* txd.flags will be overwritten in prep functions */
96 desc->txd.flags = DMA_CTRL_ACK;
97 desc->txd.tx_submit = atc_tx_submit;
98 desc->txd.phys = phys;
101 return desc;
105 * atc_desc_get - get an unused descriptor from free_list
106 * @atchan: channel we want a new descriptor for
108 static struct at_desc *atc_desc_get(struct at_dma_chan *atchan)
110 struct at_desc *desc, *_desc;
111 struct at_desc *ret = NULL;
112 unsigned long flags;
113 unsigned int i = 0;
114 LIST_HEAD(tmp_list);
116 spin_lock_irqsave(&atchan->lock, flags);
117 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
118 i++;
119 if (async_tx_test_ack(&desc->txd)) {
120 list_del(&desc->desc_node);
121 ret = desc;
122 break;
124 dev_dbg(chan2dev(&atchan->chan_common),
125 "desc %p not ACKed\n", desc);
127 spin_unlock_irqrestore(&atchan->lock, flags);
128 dev_vdbg(chan2dev(&atchan->chan_common),
129 "scanned %u descriptors on freelist\n", i);
131 /* no more descriptor available in initial pool: create one more */
132 if (!ret) {
133 ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC);
134 if (ret) {
135 spin_lock_irqsave(&atchan->lock, flags);
136 atchan->descs_allocated++;
137 spin_unlock_irqrestore(&atchan->lock, flags);
138 } else {
139 dev_err(chan2dev(&atchan->chan_common),
140 "not enough descriptors available\n");
144 return ret;
148 * atc_desc_put - move a descriptor, including any children, to the free list
149 * @atchan: channel we work on
150 * @desc: descriptor, at the head of a chain, to move to free list
152 static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc)
154 if (desc) {
155 struct at_desc *child;
156 unsigned long flags;
158 spin_lock_irqsave(&atchan->lock, flags);
159 list_for_each_entry(child, &desc->tx_list, desc_node)
160 dev_vdbg(chan2dev(&atchan->chan_common),
161 "moving child desc %p to freelist\n",
162 child);
163 list_splice_init(&desc->tx_list, &atchan->free_list);
164 dev_vdbg(chan2dev(&atchan->chan_common),
165 "moving desc %p to freelist\n", desc);
166 list_add(&desc->desc_node, &atchan->free_list);
167 spin_unlock_irqrestore(&atchan->lock, flags);
172 * atc_desc_chain - build chain adding a descripor
173 * @first: address of first descripor of the chain
174 * @prev: address of previous descripor of the chain
175 * @desc: descriptor to queue
177 * Called from prep_* functions
179 static void atc_desc_chain(struct at_desc **first, struct at_desc **prev,
180 struct at_desc *desc)
182 if (!(*first)) {
183 *first = desc;
184 } else {
185 /* inform the HW lli about chaining */
186 (*prev)->lli.dscr = desc->txd.phys;
187 /* insert the link descriptor to the LD ring */
188 list_add_tail(&desc->desc_node,
189 &(*first)->tx_list);
191 *prev = desc;
195 * atc_assign_cookie - compute and assign new cookie
196 * @atchan: channel we work on
197 * @desc: descriptor to assign cookie for
199 * Called with atchan->lock held and bh disabled
201 static dma_cookie_t
202 atc_assign_cookie(struct at_dma_chan *atchan, struct at_desc *desc)
204 dma_cookie_t cookie = atchan->chan_common.cookie;
206 if (++cookie < 0)
207 cookie = 1;
209 atchan->chan_common.cookie = cookie;
210 desc->txd.cookie = cookie;
212 return cookie;
216 * atc_dostart - starts the DMA engine for real
217 * @atchan: the channel we want to start
218 * @first: first descriptor in the list we want to begin with
220 * Called with atchan->lock held and bh disabled
222 static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
224 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
226 /* ASSERT: channel is idle */
227 if (atc_chan_is_enabled(atchan)) {
228 dev_err(chan2dev(&atchan->chan_common),
229 "BUG: Attempted to start non-idle channel\n");
230 dev_err(chan2dev(&atchan->chan_common),
231 " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
232 channel_readl(atchan, SADDR),
233 channel_readl(atchan, DADDR),
234 channel_readl(atchan, CTRLA),
235 channel_readl(atchan, CTRLB),
236 channel_readl(atchan, DSCR));
238 /* The tasklet will hopefully advance the queue... */
239 return;
242 vdbg_dump_regs(atchan);
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 */
260 static void
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,
281 desc->lli.daddr,
282 desc->len, DMA_FROM_DEVICE);
283 else
284 dma_unmap_page(parent,
285 desc->lli.daddr,
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,
291 desc->lli.saddr,
292 desc->len, DMA_TO_DEVICE);
293 else
294 dma_unmap_page(parent,
295 desc->lli.saddr,
296 desc->len, DMA_TO_DEVICE);
300 /* for cyclic transfers,
301 * no need to replay callback function while stopping */
302 if (!atc_chan_is_cyclic(atchan)) {
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
310 if (callback)
311 callback(param);
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;
329 LIST_HEAD(list);
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 */
366 return;
368 list_for_each_entry(child, &desc->tx_list, desc_node)
369 if (!(child->lli.ctrla & ATC_DONE))
370 /* Currently in progress */
371 return;
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);
394 } else {
395 atc_chain_complete(atchan, atc_first_active(atchan));
396 /* advance work */
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
422 * in active_list */
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));
465 if (callback)
466 callback(param);
469 /*-- IRQ & Tasklet ---------------------------------------------------*/
471 static void atc_tasklet(unsigned long data)
473 struct at_dma_chan *atchan = (struct at_dma_chan *)data;
474 unsigned long flags;
476 spin_lock_irqsave(&atchan->lock, flags);
477 if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
478 atc_handle_error(atchan);
479 else if (atc_chan_is_cyclic(atchan))
480 atc_handle_cyclic(atchan);
481 else
482 atc_advance_work(atchan);
484 spin_unlock_irqrestore(&atchan->lock, flags);
487 static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
489 struct at_dma *atdma = (struct at_dma *)dev_id;
490 struct at_dma_chan *atchan;
491 int i;
492 u32 status, pending, imr;
493 int ret = IRQ_NONE;
495 do {
496 imr = dma_readl(atdma, EBCIMR);
497 status = dma_readl(atdma, EBCISR);
498 pending = status & imr;
500 if (!pending)
501 break;
503 dev_vdbg(atdma->dma_common.dev,
504 "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
505 status, imr, pending);
507 for (i = 0; i < atdma->dma_common.chancnt; i++) {
508 atchan = &atdma->chan[i];
509 if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
510 if (pending & AT_DMA_ERR(i)) {
511 /* Disable channel on AHB error */
512 dma_writel(atdma, CHDR,
513 AT_DMA_RES(i) | atchan->mask);
514 /* Give information to tasklet */
515 set_bit(ATC_IS_ERROR, &atchan->status);
517 tasklet_schedule(&atchan->tasklet);
518 ret = IRQ_HANDLED;
522 } while (pending);
524 return ret;
528 /*-- DMA Engine API --------------------------------------------------*/
531 * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
532 * @desc: descriptor at the head of the transaction chain
534 * Queue chain if DMA engine is working already
536 * Cookie increment and adding to active_list or queue must be atomic
538 static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
540 struct at_desc *desc = txd_to_at_desc(tx);
541 struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
542 dma_cookie_t cookie;
543 unsigned long flags;
545 spin_lock_irqsave(&atchan->lock, flags);
546 cookie = atc_assign_cookie(atchan, desc);
548 if (list_empty(&atchan->active_list)) {
549 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
550 desc->txd.cookie);
551 atc_dostart(atchan, desc);
552 list_add_tail(&desc->desc_node, &atchan->active_list);
553 } else {
554 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
555 desc->txd.cookie);
556 list_add_tail(&desc->desc_node, &atchan->queue);
559 spin_unlock_irqrestore(&atchan->lock, flags);
561 return cookie;
565 * atc_prep_dma_memcpy - prepare a memcpy operation
566 * @chan: the channel to prepare operation on
567 * @dest: operation virtual destination address
568 * @src: operation virtual source address
569 * @len: operation length
570 * @flags: tx descriptor status flags
572 static struct dma_async_tx_descriptor *
573 atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
574 size_t len, unsigned long flags)
576 struct at_dma_chan *atchan = to_at_dma_chan(chan);
577 struct at_desc *desc = NULL;
578 struct at_desc *first = NULL;
579 struct at_desc *prev = NULL;
580 size_t xfer_count;
581 size_t offset;
582 unsigned int src_width;
583 unsigned int dst_width;
584 u32 ctrla;
585 u32 ctrlb;
587 dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
588 dest, src, len, flags);
590 if (unlikely(!len)) {
591 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
592 return NULL;
595 ctrla = ATC_DEFAULT_CTRLA;
596 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
597 | ATC_SRC_ADDR_MODE_INCR
598 | ATC_DST_ADDR_MODE_INCR
599 | ATC_FC_MEM2MEM;
602 * We can be a lot more clever here, but this should take care
603 * of the most common optimization.
605 if (!((src | dest | len) & 3)) {
606 ctrla |= ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
607 src_width = dst_width = 2;
608 } else if (!((src | dest | len) & 1)) {
609 ctrla |= ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
610 src_width = dst_width = 1;
611 } else {
612 ctrla |= ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
613 src_width = dst_width = 0;
616 for (offset = 0; offset < len; offset += xfer_count << src_width) {
617 xfer_count = min_t(size_t, (len - offset) >> src_width,
618 ATC_BTSIZE_MAX);
620 desc = atc_desc_get(atchan);
621 if (!desc)
622 goto err_desc_get;
624 desc->lli.saddr = src + offset;
625 desc->lli.daddr = dest + offset;
626 desc->lli.ctrla = ctrla | xfer_count;
627 desc->lli.ctrlb = ctrlb;
629 desc->txd.cookie = 0;
631 atc_desc_chain(&first, &prev, desc);
634 /* First descriptor of the chain embedds additional information */
635 first->txd.cookie = -EBUSY;
636 first->len = len;
638 /* set end-of-link to the last link descriptor of list*/
639 set_desc_eol(desc);
641 first->txd.flags = flags; /* client is in control of this ack */
643 return &first->txd;
645 err_desc_get:
646 atc_desc_put(atchan, first);
647 return NULL;
652 * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
653 * @chan: DMA channel
654 * @sgl: scatterlist to transfer to/from
655 * @sg_len: number of entries in @scatterlist
656 * @direction: DMA direction
657 * @flags: tx descriptor status flags
659 static struct dma_async_tx_descriptor *
660 atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
661 unsigned int sg_len, enum dma_transfer_direction direction,
662 unsigned long flags)
664 struct at_dma_chan *atchan = to_at_dma_chan(chan);
665 struct at_dma_slave *atslave = chan->private;
666 struct at_desc *first = NULL;
667 struct at_desc *prev = NULL;
668 u32 ctrla;
669 u32 ctrlb;
670 dma_addr_t reg;
671 unsigned int reg_width;
672 unsigned int mem_width;
673 unsigned int i;
674 struct scatterlist *sg;
675 size_t total_len = 0;
677 dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
678 sg_len,
679 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
680 flags);
682 if (unlikely(!atslave || !sg_len)) {
683 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
684 return NULL;
687 reg_width = atslave->reg_width;
689 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla;
690 ctrlb = ATC_IEN;
692 switch (direction) {
693 case DMA_MEM_TO_DEV:
694 ctrla |= ATC_DST_WIDTH(reg_width);
695 ctrlb |= ATC_DST_ADDR_MODE_FIXED
696 | ATC_SRC_ADDR_MODE_INCR
697 | ATC_FC_MEM2PER
698 | ATC_SIF(AT_DMA_MEM_IF) | ATC_DIF(AT_DMA_PER_IF);
699 reg = atslave->tx_reg;
700 for_each_sg(sgl, sg, sg_len, i) {
701 struct at_desc *desc;
702 u32 len;
703 u32 mem;
705 desc = atc_desc_get(atchan);
706 if (!desc)
707 goto err_desc_get;
709 mem = sg_dma_address(sg);
710 len = sg_dma_len(sg);
711 mem_width = 2;
712 if (unlikely(mem & 3 || len & 3))
713 mem_width = 0;
715 desc->lli.saddr = mem;
716 desc->lli.daddr = reg;
717 desc->lli.ctrla = ctrla
718 | ATC_SRC_WIDTH(mem_width)
719 | len >> mem_width;
720 desc->lli.ctrlb = ctrlb;
722 atc_desc_chain(&first, &prev, desc);
723 total_len += len;
725 break;
726 case DMA_DEV_TO_MEM:
727 ctrla |= ATC_SRC_WIDTH(reg_width);
728 ctrlb |= ATC_DST_ADDR_MODE_INCR
729 | ATC_SRC_ADDR_MODE_FIXED
730 | ATC_FC_PER2MEM
731 | ATC_SIF(AT_DMA_PER_IF) | ATC_DIF(AT_DMA_MEM_IF);
733 reg = atslave->rx_reg;
734 for_each_sg(sgl, sg, sg_len, i) {
735 struct at_desc *desc;
736 u32 len;
737 u32 mem;
739 desc = atc_desc_get(atchan);
740 if (!desc)
741 goto err_desc_get;
743 mem = sg_dma_address(sg);
744 len = sg_dma_len(sg);
745 mem_width = 2;
746 if (unlikely(mem & 3 || len & 3))
747 mem_width = 0;
749 desc->lli.saddr = reg;
750 desc->lli.daddr = mem;
751 desc->lli.ctrla = ctrla
752 | ATC_DST_WIDTH(mem_width)
753 | len >> reg_width;
754 desc->lli.ctrlb = ctrlb;
756 atc_desc_chain(&first, &prev, desc);
757 total_len += len;
759 break;
760 default:
761 return NULL;
764 /* set end-of-link to the last link descriptor of list*/
765 set_desc_eol(prev);
767 /* First descriptor of the chain embedds additional information */
768 first->txd.cookie = -EBUSY;
769 first->len = total_len;
771 /* first link descriptor of list is responsible of flags */
772 first->txd.flags = flags; /* client is in control of this ack */
774 return &first->txd;
776 err_desc_get:
777 dev_err(chan2dev(chan), "not enough descriptors available\n");
778 atc_desc_put(atchan, first);
779 return NULL;
783 * atc_dma_cyclic_check_values
784 * Check for too big/unaligned periods and unaligned DMA buffer
786 static int
787 atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
788 size_t period_len, enum dma_transfer_direction direction)
790 if (period_len > (ATC_BTSIZE_MAX << reg_width))
791 goto err_out;
792 if (unlikely(period_len & ((1 << reg_width) - 1)))
793 goto err_out;
794 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
795 goto err_out;
796 if (unlikely(!(direction & (DMA_DEV_TO_MEM | DMA_MEM_TO_DEV))))
797 goto err_out;
799 return 0;
801 err_out:
802 return -EINVAL;
806 * atc_dma_cyclic_fill_desc - Fill one period decriptor
808 static int
809 atc_dma_cyclic_fill_desc(struct at_dma_slave *atslave, struct at_desc *desc,
810 unsigned int period_index, dma_addr_t buf_addr,
811 size_t period_len, enum dma_transfer_direction direction)
813 u32 ctrla;
814 unsigned int reg_width = atslave->reg_width;
816 /* prepare common CRTLA value */
817 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla
818 | ATC_DST_WIDTH(reg_width)
819 | ATC_SRC_WIDTH(reg_width)
820 | period_len >> reg_width;
822 switch (direction) {
823 case DMA_MEM_TO_DEV:
824 desc->lli.saddr = buf_addr + (period_len * period_index);
825 desc->lli.daddr = atslave->tx_reg;
826 desc->lli.ctrla = ctrla;
827 desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
828 | ATC_SRC_ADDR_MODE_INCR
829 | ATC_FC_MEM2PER
830 | ATC_SIF(AT_DMA_MEM_IF)
831 | ATC_DIF(AT_DMA_PER_IF);
832 break;
834 case DMA_DEV_TO_MEM:
835 desc->lli.saddr = atslave->rx_reg;
836 desc->lli.daddr = buf_addr + (period_len * period_index);
837 desc->lli.ctrla = ctrla;
838 desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
839 | ATC_SRC_ADDR_MODE_FIXED
840 | ATC_FC_PER2MEM
841 | ATC_SIF(AT_DMA_PER_IF)
842 | ATC_DIF(AT_DMA_MEM_IF);
843 break;
845 default:
846 return -EINVAL;
849 return 0;
853 * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
854 * @chan: the DMA channel to prepare
855 * @buf_addr: physical DMA address where the buffer starts
856 * @buf_len: total number of bytes for the entire buffer
857 * @period_len: number of bytes for each period
858 * @direction: transfer direction, to or from device
860 static struct dma_async_tx_descriptor *
861 atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
862 size_t period_len, enum dma_transfer_direction direction)
864 struct at_dma_chan *atchan = to_at_dma_chan(chan);
865 struct at_dma_slave *atslave = chan->private;
866 struct at_desc *first = NULL;
867 struct at_desc *prev = NULL;
868 unsigned long was_cyclic;
869 unsigned int periods = buf_len / period_len;
870 unsigned int i;
872 dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
873 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
874 buf_addr,
875 periods, buf_len, period_len);
877 if (unlikely(!atslave || !buf_len || !period_len)) {
878 dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
879 return NULL;
882 was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
883 if (was_cyclic) {
884 dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
885 return NULL;
888 /* Check for too big/unaligned periods and unaligned DMA buffer */
889 if (atc_dma_cyclic_check_values(atslave->reg_width, buf_addr,
890 period_len, direction))
891 goto err_out;
893 /* build cyclic linked list */
894 for (i = 0; i < periods; i++) {
895 struct at_desc *desc;
897 desc = atc_desc_get(atchan);
898 if (!desc)
899 goto err_desc_get;
901 if (atc_dma_cyclic_fill_desc(atslave, desc, i, buf_addr,
902 period_len, direction))
903 goto err_desc_get;
905 atc_desc_chain(&first, &prev, desc);
908 /* lets make a cyclic list */
909 prev->lli.dscr = first->txd.phys;
911 /* First descriptor of the chain embedds additional information */
912 first->txd.cookie = -EBUSY;
913 first->len = buf_len;
915 return &first->txd;
917 err_desc_get:
918 dev_err(chan2dev(chan), "not enough descriptors available\n");
919 atc_desc_put(atchan, first);
920 err_out:
921 clear_bit(ATC_IS_CYCLIC, &atchan->status);
922 return NULL;
926 static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
927 unsigned long arg)
929 struct at_dma_chan *atchan = to_at_dma_chan(chan);
930 struct at_dma *atdma = to_at_dma(chan->device);
931 int chan_id = atchan->chan_common.chan_id;
932 unsigned long flags;
934 LIST_HEAD(list);
936 dev_vdbg(chan2dev(chan), "atc_control (%d)\n", cmd);
938 if (cmd == DMA_PAUSE) {
939 spin_lock_irqsave(&atchan->lock, flags);
941 dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
942 set_bit(ATC_IS_PAUSED, &atchan->status);
944 spin_unlock_irqrestore(&atchan->lock, flags);
945 } else if (cmd == DMA_RESUME) {
946 if (!atc_chan_is_paused(atchan))
947 return 0;
949 spin_lock_irqsave(&atchan->lock, flags);
951 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
952 clear_bit(ATC_IS_PAUSED, &atchan->status);
954 spin_unlock_irqrestore(&atchan->lock, flags);
955 } else if (cmd == DMA_TERMINATE_ALL) {
956 struct at_desc *desc, *_desc;
958 * This is only called when something went wrong elsewhere, so
959 * we don't really care about the data. Just disable the
960 * channel. We still have to poll the channel enable bit due
961 * to AHB/HSB limitations.
963 spin_lock_irqsave(&atchan->lock, flags);
965 /* disabling channel: must also remove suspend state */
966 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
968 /* confirm that this channel is disabled */
969 while (dma_readl(atdma, CHSR) & atchan->mask)
970 cpu_relax();
972 /* active_list entries will end up before queued entries */
973 list_splice_init(&atchan->queue, &list);
974 list_splice_init(&atchan->active_list, &list);
976 /* Flush all pending and queued descriptors */
977 list_for_each_entry_safe(desc, _desc, &list, desc_node)
978 atc_chain_complete(atchan, desc);
980 clear_bit(ATC_IS_PAUSED, &atchan->status);
981 /* if channel dedicated to cyclic operations, free it */
982 clear_bit(ATC_IS_CYCLIC, &atchan->status);
984 spin_unlock_irqrestore(&atchan->lock, flags);
985 } else {
986 return -ENXIO;
989 return 0;
993 * atc_tx_status - poll for transaction completion
994 * @chan: DMA channel
995 * @cookie: transaction identifier to check status of
996 * @txstate: if not %NULL updated with transaction state
998 * If @txstate is passed in, upon return it reflect the driver
999 * internal state and can be used with dma_async_is_complete() to check
1000 * the status of multiple cookies without re-checking hardware state.
1002 static enum dma_status
1003 atc_tx_status(struct dma_chan *chan,
1004 dma_cookie_t cookie,
1005 struct dma_tx_state *txstate)
1007 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1008 dma_cookie_t last_used;
1009 dma_cookie_t last_complete;
1010 unsigned long flags;
1011 enum dma_status ret;
1013 spin_lock_irqsave(&atchan->lock, flags);
1015 last_complete = atchan->completed_cookie;
1016 last_used = chan->cookie;
1018 ret = dma_async_is_complete(cookie, last_complete, last_used);
1019 if (ret != DMA_SUCCESS) {
1020 atc_cleanup_descriptors(atchan);
1022 last_complete = atchan->completed_cookie;
1023 last_used = chan->cookie;
1025 ret = dma_async_is_complete(cookie, last_complete, last_used);
1028 spin_unlock_irqrestore(&atchan->lock, flags);
1030 if (ret != DMA_SUCCESS)
1031 dma_set_tx_state(txstate, last_complete, last_used,
1032 atc_first_active(atchan)->len);
1033 else
1034 dma_set_tx_state(txstate, last_complete, last_used, 0);
1036 if (atc_chan_is_paused(atchan))
1037 ret = DMA_PAUSED;
1039 dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d (d%d, u%d)\n",
1040 ret, cookie, last_complete ? last_complete : 0,
1041 last_used ? last_used : 0);
1043 return ret;
1047 * atc_issue_pending - try to finish work
1048 * @chan: target DMA channel
1050 static void atc_issue_pending(struct dma_chan *chan)
1052 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1053 unsigned long flags;
1055 dev_vdbg(chan2dev(chan), "issue_pending\n");
1057 /* Not needed for cyclic transfers */
1058 if (atc_chan_is_cyclic(atchan))
1059 return;
1061 spin_lock_irqsave(&atchan->lock, flags);
1062 if (!atc_chan_is_enabled(atchan)) {
1063 atc_advance_work(atchan);
1065 spin_unlock_irqrestore(&atchan->lock, flags);
1069 * atc_alloc_chan_resources - allocate resources for DMA channel
1070 * @chan: allocate descriptor resources for this channel
1071 * @client: current client requesting the channel be ready for requests
1073 * return - the number of allocated descriptors
1075 static int atc_alloc_chan_resources(struct dma_chan *chan)
1077 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1078 struct at_dma *atdma = to_at_dma(chan->device);
1079 struct at_desc *desc;
1080 struct at_dma_slave *atslave;
1081 unsigned long flags;
1082 int i;
1083 u32 cfg;
1084 LIST_HEAD(tmp_list);
1086 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
1088 /* ASSERT: channel is idle */
1089 if (atc_chan_is_enabled(atchan)) {
1090 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
1091 return -EIO;
1094 cfg = ATC_DEFAULT_CFG;
1096 atslave = chan->private;
1097 if (atslave) {
1099 * We need controller-specific data to set up slave
1100 * transfers.
1102 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
1104 /* if cfg configuration specified take it instad of default */
1105 if (atslave->cfg)
1106 cfg = atslave->cfg;
1109 /* have we already been set up?
1110 * reconfigure channel but no need to reallocate descriptors */
1111 if (!list_empty(&atchan->free_list))
1112 return atchan->descs_allocated;
1114 /* Allocate initial pool of descriptors */
1115 for (i = 0; i < init_nr_desc_per_channel; i++) {
1116 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
1117 if (!desc) {
1118 dev_err(atdma->dma_common.dev,
1119 "Only %d initial descriptors\n", i);
1120 break;
1122 list_add_tail(&desc->desc_node, &tmp_list);
1125 spin_lock_irqsave(&atchan->lock, flags);
1126 atchan->descs_allocated = i;
1127 list_splice(&tmp_list, &atchan->free_list);
1128 atchan->completed_cookie = chan->cookie = 1;
1129 spin_unlock_irqrestore(&atchan->lock, flags);
1131 /* channel parameters */
1132 channel_writel(atchan, CFG, cfg);
1134 dev_dbg(chan2dev(chan),
1135 "alloc_chan_resources: allocated %d descriptors\n",
1136 atchan->descs_allocated);
1138 return atchan->descs_allocated;
1142 * atc_free_chan_resources - free all channel resources
1143 * @chan: DMA channel
1145 static void atc_free_chan_resources(struct dma_chan *chan)
1147 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1148 struct at_dma *atdma = to_at_dma(chan->device);
1149 struct at_desc *desc, *_desc;
1150 LIST_HEAD(list);
1152 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
1153 atchan->descs_allocated);
1155 /* ASSERT: channel is idle */
1156 BUG_ON(!list_empty(&atchan->active_list));
1157 BUG_ON(!list_empty(&atchan->queue));
1158 BUG_ON(atc_chan_is_enabled(atchan));
1160 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
1161 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1162 list_del(&desc->desc_node);
1163 /* free link descriptor */
1164 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
1166 list_splice_init(&atchan->free_list, &list);
1167 atchan->descs_allocated = 0;
1168 atchan->status = 0;
1170 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
1174 /*-- Module Management -----------------------------------------------*/
1176 /* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
1177 static struct at_dma_platform_data at91sam9rl_config = {
1178 .nr_channels = 2,
1180 static struct at_dma_platform_data at91sam9g45_config = {
1181 .nr_channels = 8,
1184 #if defined(CONFIG_OF)
1185 static const struct of_device_id atmel_dma_dt_ids[] = {
1187 .compatible = "atmel,at91sam9rl-dma",
1188 .data = &at91sam9rl_config,
1189 }, {
1190 .compatible = "atmel,at91sam9g45-dma",
1191 .data = &at91sam9g45_config,
1192 }, {
1193 /* sentinel */
1197 MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
1198 #endif
1200 static const struct platform_device_id atdma_devtypes[] = {
1202 .name = "at91sam9rl_dma",
1203 .driver_data = (unsigned long) &at91sam9rl_config,
1204 }, {
1205 .name = "at91sam9g45_dma",
1206 .driver_data = (unsigned long) &at91sam9g45_config,
1207 }, {
1208 /* sentinel */
1212 static inline struct at_dma_platform_data * __init at_dma_get_driver_data(
1213 struct platform_device *pdev)
1215 if (pdev->dev.of_node) {
1216 const struct of_device_id *match;
1217 match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
1218 if (match == NULL)
1219 return NULL;
1220 return match->data;
1222 return (struct at_dma_platform_data *)
1223 platform_get_device_id(pdev)->driver_data;
1227 * at_dma_off - disable DMA controller
1228 * @atdma: the Atmel HDAMC device
1230 static void at_dma_off(struct at_dma *atdma)
1232 dma_writel(atdma, EN, 0);
1234 /* disable all interrupts */
1235 dma_writel(atdma, EBCIDR, -1L);
1237 /* confirm that all channels are disabled */
1238 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
1239 cpu_relax();
1242 static int __init at_dma_probe(struct platform_device *pdev)
1244 struct resource *io;
1245 struct at_dma *atdma;
1246 size_t size;
1247 int irq;
1248 int err;
1249 int i;
1250 struct at_dma_platform_data *plat_dat;
1252 /* setup platform data for each SoC */
1253 dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
1254 dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
1255 dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
1257 /* get DMA parameters from controller type */
1258 plat_dat = at_dma_get_driver_data(pdev);
1259 if (!plat_dat)
1260 return -ENODEV;
1262 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1263 if (!io)
1264 return -EINVAL;
1266 irq = platform_get_irq(pdev, 0);
1267 if (irq < 0)
1268 return irq;
1270 size = sizeof(struct at_dma);
1271 size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
1272 atdma = kzalloc(size, GFP_KERNEL);
1273 if (!atdma)
1274 return -ENOMEM;
1276 /* discover transaction capabilities */
1277 atdma->dma_common.cap_mask = plat_dat->cap_mask;
1278 atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
1280 size = resource_size(io);
1281 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1282 err = -EBUSY;
1283 goto err_kfree;
1286 atdma->regs = ioremap(io->start, size);
1287 if (!atdma->regs) {
1288 err = -ENOMEM;
1289 goto err_release_r;
1292 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1293 if (IS_ERR(atdma->clk)) {
1294 err = PTR_ERR(atdma->clk);
1295 goto err_clk;
1297 clk_enable(atdma->clk);
1299 /* force dma off, just in case */
1300 at_dma_off(atdma);
1302 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1303 if (err)
1304 goto err_irq;
1306 platform_set_drvdata(pdev, atdma);
1308 /* create a pool of consistent memory blocks for hardware descriptors */
1309 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1310 &pdev->dev, sizeof(struct at_desc),
1311 4 /* word alignment */, 0);
1312 if (!atdma->dma_desc_pool) {
1313 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1314 err = -ENOMEM;
1315 goto err_pool_create;
1318 /* clear any pending interrupt */
1319 while (dma_readl(atdma, EBCISR))
1320 cpu_relax();
1322 /* initialize channels related values */
1323 INIT_LIST_HEAD(&atdma->dma_common.channels);
1324 for (i = 0; i < plat_dat->nr_channels; i++) {
1325 struct at_dma_chan *atchan = &atdma->chan[i];
1327 atchan->chan_common.device = &atdma->dma_common;
1328 atchan->chan_common.cookie = atchan->completed_cookie = 1;
1329 list_add_tail(&atchan->chan_common.device_node,
1330 &atdma->dma_common.channels);
1332 atchan->ch_regs = atdma->regs + ch_regs(i);
1333 spin_lock_init(&atchan->lock);
1334 atchan->mask = 1 << i;
1336 INIT_LIST_HEAD(&atchan->active_list);
1337 INIT_LIST_HEAD(&atchan->queue);
1338 INIT_LIST_HEAD(&atchan->free_list);
1340 tasklet_init(&atchan->tasklet, atc_tasklet,
1341 (unsigned long)atchan);
1342 atc_enable_chan_irq(atdma, i);
1345 /* set base routines */
1346 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1347 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
1348 atdma->dma_common.device_tx_status = atc_tx_status;
1349 atdma->dma_common.device_issue_pending = atc_issue_pending;
1350 atdma->dma_common.dev = &pdev->dev;
1352 /* set prep routines based on capability */
1353 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1354 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1356 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
1357 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
1358 /* controller can do slave DMA: can trigger cyclic transfers */
1359 dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
1360 atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
1361 atdma->dma_common.device_control = atc_control;
1364 dma_writel(atdma, EN, AT_DMA_ENABLE);
1366 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
1367 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1368 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
1369 plat_dat->nr_channels);
1371 dma_async_device_register(&atdma->dma_common);
1373 return 0;
1375 err_pool_create:
1376 platform_set_drvdata(pdev, NULL);
1377 free_irq(platform_get_irq(pdev, 0), atdma);
1378 err_irq:
1379 clk_disable(atdma->clk);
1380 clk_put(atdma->clk);
1381 err_clk:
1382 iounmap(atdma->regs);
1383 atdma->regs = NULL;
1384 err_release_r:
1385 release_mem_region(io->start, size);
1386 err_kfree:
1387 kfree(atdma);
1388 return err;
1391 static int __exit at_dma_remove(struct platform_device *pdev)
1393 struct at_dma *atdma = platform_get_drvdata(pdev);
1394 struct dma_chan *chan, *_chan;
1395 struct resource *io;
1397 at_dma_off(atdma);
1398 dma_async_device_unregister(&atdma->dma_common);
1400 dma_pool_destroy(atdma->dma_desc_pool);
1401 platform_set_drvdata(pdev, NULL);
1402 free_irq(platform_get_irq(pdev, 0), atdma);
1404 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1405 device_node) {
1406 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1408 /* Disable interrupts */
1409 atc_disable_chan_irq(atdma, chan->chan_id);
1410 tasklet_disable(&atchan->tasklet);
1412 tasklet_kill(&atchan->tasklet);
1413 list_del(&chan->device_node);
1416 clk_disable(atdma->clk);
1417 clk_put(atdma->clk);
1419 iounmap(atdma->regs);
1420 atdma->regs = NULL;
1422 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1423 release_mem_region(io->start, resource_size(io));
1425 kfree(atdma);
1427 return 0;
1430 static void at_dma_shutdown(struct platform_device *pdev)
1432 struct at_dma *atdma = platform_get_drvdata(pdev);
1434 at_dma_off(platform_get_drvdata(pdev));
1435 clk_disable(atdma->clk);
1438 static int at_dma_prepare(struct device *dev)
1440 struct platform_device *pdev = to_platform_device(dev);
1441 struct at_dma *atdma = platform_get_drvdata(pdev);
1442 struct dma_chan *chan, *_chan;
1444 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1445 device_node) {
1446 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1447 /* wait for transaction completion (except in cyclic case) */
1448 if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
1449 return -EAGAIN;
1451 return 0;
1454 static void atc_suspend_cyclic(struct at_dma_chan *atchan)
1456 struct dma_chan *chan = &atchan->chan_common;
1458 /* Channel should be paused by user
1459 * do it anyway even if it is not done already */
1460 if (!atc_chan_is_paused(atchan)) {
1461 dev_warn(chan2dev(chan),
1462 "cyclic channel not paused, should be done by channel user\n");
1463 atc_control(chan, DMA_PAUSE, 0);
1466 /* now preserve additional data for cyclic operations */
1467 /* next descriptor address in the cyclic list */
1468 atchan->save_dscr = channel_readl(atchan, DSCR);
1470 vdbg_dump_regs(atchan);
1473 static int at_dma_suspend_noirq(struct device *dev)
1475 struct platform_device *pdev = to_platform_device(dev);
1476 struct at_dma *atdma = platform_get_drvdata(pdev);
1477 struct dma_chan *chan, *_chan;
1479 /* preserve data */
1480 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1481 device_node) {
1482 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1484 if (atc_chan_is_cyclic(atchan))
1485 atc_suspend_cyclic(atchan);
1486 atchan->save_cfg = channel_readl(atchan, CFG);
1488 atdma->save_imr = dma_readl(atdma, EBCIMR);
1490 /* disable DMA controller */
1491 at_dma_off(atdma);
1492 clk_disable(atdma->clk);
1493 return 0;
1496 static void atc_resume_cyclic(struct at_dma_chan *atchan)
1498 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
1500 /* restore channel status for cyclic descriptors list:
1501 * next descriptor in the cyclic list at the time of suspend */
1502 channel_writel(atchan, SADDR, 0);
1503 channel_writel(atchan, DADDR, 0);
1504 channel_writel(atchan, CTRLA, 0);
1505 channel_writel(atchan, CTRLB, 0);
1506 channel_writel(atchan, DSCR, atchan->save_dscr);
1507 dma_writel(atdma, CHER, atchan->mask);
1509 /* channel pause status should be removed by channel user
1510 * We cannot take the initiative to do it here */
1512 vdbg_dump_regs(atchan);
1515 static int at_dma_resume_noirq(struct device *dev)
1517 struct platform_device *pdev = to_platform_device(dev);
1518 struct at_dma *atdma = platform_get_drvdata(pdev);
1519 struct dma_chan *chan, *_chan;
1521 /* bring back DMA controller */
1522 clk_enable(atdma->clk);
1523 dma_writel(atdma, EN, AT_DMA_ENABLE);
1525 /* clear any pending interrupt */
1526 while (dma_readl(atdma, EBCISR))
1527 cpu_relax();
1529 /* restore saved data */
1530 dma_writel(atdma, EBCIER, atdma->save_imr);
1531 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1532 device_node) {
1533 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1535 channel_writel(atchan, CFG, atchan->save_cfg);
1536 if (atc_chan_is_cyclic(atchan))
1537 atc_resume_cyclic(atchan);
1539 return 0;
1542 static const struct dev_pm_ops at_dma_dev_pm_ops = {
1543 .prepare = at_dma_prepare,
1544 .suspend_noirq = at_dma_suspend_noirq,
1545 .resume_noirq = at_dma_resume_noirq,
1548 static struct platform_driver at_dma_driver = {
1549 .remove = __exit_p(at_dma_remove),
1550 .shutdown = at_dma_shutdown,
1551 .id_table = atdma_devtypes,
1552 .driver = {
1553 .name = "at_hdmac",
1554 .pm = &at_dma_dev_pm_ops,
1555 .of_match_table = of_match_ptr(atmel_dma_dt_ids),
1559 static int __init at_dma_init(void)
1561 return platform_driver_probe(&at_dma_driver, at_dma_probe);
1563 subsys_initcall(at_dma_init);
1565 static void __exit at_dma_exit(void)
1567 platform_driver_unregister(&at_dma_driver);
1569 module_exit(at_dma_exit);
1571 MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1572 MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1573 MODULE_LICENSE("GPL");
1574 MODULE_ALIAS("platform:at_hdmac");