dmaengine: at_hdmac: add cyclic DMA operation support
[pandora-kernel.git] / drivers / dma / at_hdmac.c
1 /*
2  * Driver for the Atmel AHB DMA Controller (aka HDMA or DMAC on AT91 systems)
3  *
4  * Copyright (C) 2008 Atmel Corporation
5  *
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.
10  *
11  *
12  * This supports the Atmel AHB DMA Controller,
13  *
14  * The driver has currently been tested with the Atmel AT91SAM9RL
15  * and AT91SAM9G45 series.
16  */
17
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
27 #include "at_hdmac_regs.h"
28
29 /*
30  * Glossary
31  * --------
32  *
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
36  */
37
38 #define ATC_DEFAULT_CFG         (ATC_FIFOCFG_HALFFIFO)
39 #define ATC_DEFAULT_CTRLA       (0)
40 #define ATC_DEFAULT_CTRLB       (ATC_SIF(0)     \
41                                 |ATC_DIF(1))
42
43 /*
44  * Initial number of descriptors to allocate for each channel. This could
45  * be increased during dma usage.
46  */
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)");
51
52
53 /* prototypes */
54 static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx);
55
56
57 /*----------------------------------------------------------------------*/
58
59 static struct at_desc *atc_first_active(struct at_dma_chan *atchan)
60 {
61         return list_first_entry(&atchan->active_list,
62                                 struct at_desc, desc_node);
63 }
64
65 static struct at_desc *atc_first_queued(struct at_dma_chan *atchan)
66 {
67         return list_first_entry(&atchan->queue,
68                                 struct at_desc, desc_node);
69 }
70
71 /**
72  * atc_alloc_descriptor - allocate and return an initialized descriptor
73  * @chan: the channel to allocate descriptors for
74  * @gfp_flags: GFP allocation flags
75  *
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).
80  */
81 static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan,
82                                             gfp_t gfp_flags)
83 {
84         struct at_desc  *desc = NULL;
85         struct at_dma   *atdma = to_at_dma(chan->device);
86         dma_addr_t phys;
87
88         desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys);
89         if (desc) {
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;
97         }
98
99         return desc;
100 }
101
102 /**
103  * atc_desc_get - get an unused descriptor from free_list
104  * @atchan: channel we want a new descriptor for
105  */
106 static struct at_desc *atc_desc_get(struct at_dma_chan *atchan)
107 {
108         struct at_desc *desc, *_desc;
109         struct at_desc *ret = NULL;
110         unsigned int i = 0;
111         LIST_HEAD(tmp_list);
112
113         spin_lock_bh(&atchan->lock);
114         list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
115                 i++;
116                 if (async_tx_test_ack(&desc->txd)) {
117                         list_del(&desc->desc_node);
118                         ret = desc;
119                         break;
120                 }
121                 dev_dbg(chan2dev(&atchan->chan_common),
122                                 "desc %p not ACKed\n", desc);
123         }
124         spin_unlock_bh(&atchan->lock);
125         dev_vdbg(chan2dev(&atchan->chan_common),
126                 "scanned %u descriptors on freelist\n", i);
127
128         /* no more descriptor available in initial pool: create one more */
129         if (!ret) {
130                 ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC);
131                 if (ret) {
132                         spin_lock_bh(&atchan->lock);
133                         atchan->descs_allocated++;
134                         spin_unlock_bh(&atchan->lock);
135                 } else {
136                         dev_err(chan2dev(&atchan->chan_common),
137                                         "not enough descriptors available\n");
138                 }
139         }
140
141         return ret;
142 }
143
144 /**
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
148  */
149 static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc)
150 {
151         if (desc) {
152                 struct at_desc *child;
153
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",
158                                         child);
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);
164         }
165 }
166
167 /**
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
172  *
173  * Called from prep_* functions
174  */
175 static void atc_desc_chain(struct at_desc **first, struct at_desc **prev,
176                            struct at_desc *desc)
177 {
178         if (!(*first)) {
179                 *first = desc;
180         } else {
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,
185                                 &(*first)->tx_list);
186         }
187         *prev = desc;
188 }
189
190 /**
191  * atc_assign_cookie - compute and assign new cookie
192  * @atchan: channel we work on
193  * @desc: descriptor to asign cookie for
194  *
195  * Called with atchan->lock held and bh disabled
196  */
197 static dma_cookie_t
198 atc_assign_cookie(struct at_dma_chan *atchan, struct at_desc *desc)
199 {
200         dma_cookie_t cookie = atchan->chan_common.cookie;
201
202         if (++cookie < 0)
203                 cookie = 1;
204
205         atchan->chan_common.cookie = cookie;
206         desc->txd.cookie = cookie;
207
208         return cookie;
209 }
210
211 /**
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
215  *
216  * Called with atchan->lock held and bh disabled
217  */
218 static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
219 {
220         struct at_dma   *atdma = to_at_dma(atchan->chan_common.device);
221
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));
233
234                 /* The tasklet will hopefully advance the queue... */
235                 return;
236         }
237
238         vdbg_dump_regs(atchan);
239
240         /* clear any pending interrupt */
241         while (dma_readl(atdma, EBCISR))
242                 cpu_relax();
243
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);
250
251         vdbg_dump_regs(atchan);
252 }
253
254 /**
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
258  *
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)
262 {
263         struct dma_async_tx_descriptor  *txd = &desc->txd;
264
265         dev_vdbg(chan2dev(&atchan->chan_common),
266                 "descriptor %u complete\n", txd->cookie);
267
268         atchan->completed_cookie = txd->cookie;
269
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);
274
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);
287                 }
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);
297                 }
298         }
299
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;
305
306                 /*
307                  * The API requires that no submissions are done from a
308                  * callback, so we don't need to drop the lock here
309                  */
310                 if (callback)
311                         callback(param);
312         }
313
314         dma_run_dependencies(txd);
315 }
316
317 /**
318  * atc_complete_all - finish work for all transactions
319  * @atchan: channel to complete transactions for
320  *
321  * Eventually submit queued descriptors if any
322  *
323  * Assume channel is idle while calling this function
324  * Called with atchan->lock held and bh disabled
325  */
326 static void atc_complete_all(struct at_dma_chan *atchan)
327 {
328         struct at_desc *desc, *_desc;
329         LIST_HEAD(list);
330
331         dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
332
333         BUG_ON(atc_chan_is_enabled(atchan));
334
335         /*
336          * Submit queued descriptors ASAP, i.e. before we go through
337          * the completed ones.
338          */
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);
345
346         list_for_each_entry_safe(desc, _desc, &list, desc_node)
347                 atc_chain_complete(atchan, desc);
348 }
349
350 /**
351  * atc_cleanup_descriptors - cleanup up finished descriptors in active_list
352  * @atchan: channel to be cleaned up
353  *
354  * Called with atchan->lock held and bh disabled
355  */
356 static void atc_cleanup_descriptors(struct at_dma_chan *atchan)
357 {
358         struct at_desc  *desc, *_desc;
359         struct at_desc  *child;
360
361         dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n");
362
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;
367
368                 list_for_each_entry(child, &desc->tx_list, desc_node)
369                         if (!(child->lli.ctrla & ATC_DONE))
370                                 /* Currently in progress */
371                                 return;
372
373                 /*
374                  * No descriptors so far seem to be in progress, i.e.
375                  * this chain must be done.
376                  */
377                 atc_chain_complete(atchan, desc);
378         }
379 }
380
381 /**
382  * atc_advance_work - at the end of a transaction, move forward
383  * @atchan: channel where the transaction ended
384  *
385  * Called with atchan->lock held and bh disabled
386  */
387 static void atc_advance_work(struct at_dma_chan *atchan)
388 {
389         dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
390
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));
398         }
399 }
400
401
402 /**
403  * atc_handle_error - handle errors reported by DMA controller
404  * @atchan: channel where error occurs
405  *
406  * Called with atchan->lock held and bh disabled
407  */
408 static void atc_handle_error(struct at_dma_chan *atchan)
409 {
410         struct at_desc *bad_desc;
411         struct at_desc *child;
412
413         /*
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.
417          */
418         bad_desc = atc_first_active(atchan);
419         list_del_init(&bad_desc->desc_node);
420
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);
424
425         /* Try to restart the controller */
426         if (!list_empty(&atchan->active_list))
427                 atc_dostart(atchan, atc_first_active(atchan));
428
429         /*
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.
435          */
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);
443
444         /* Pretend the descriptor completed successfully */
445         atc_chain_complete(atchan, bad_desc);
446 }
447
448 /**
449  * atc_handle_cyclic - at the end of a period, run callback function
450  * @atchan: channel used for cyclic operations
451  *
452  * Called with atchan->lock held and bh disabled
453  */
454 static void atc_handle_cyclic(struct at_dma_chan *atchan)
455 {
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;
460
461         dev_vdbg(chan2dev(&atchan->chan_common),
462                         "new cyclic period llp 0x%08x\n",
463                         channel_readl(atchan, DSCR));
464
465         if (callback)
466                 callback(param);
467 }
468
469 /*--  IRQ & Tasklet  ---------------------------------------------------*/
470
471 static void atc_tasklet(unsigned long data)
472 {
473         struct at_dma_chan *atchan = (struct at_dma_chan *)data;
474
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");
479                 return;
480         }
481
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);
487         else
488                 atc_advance_work(atchan);
489
490         spin_unlock(&atchan->lock);
491 }
492
493 static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
494 {
495         struct at_dma           *atdma = (struct at_dma *)dev_id;
496         struct at_dma_chan      *atchan;
497         int                     i;
498         u32                     status, pending, imr;
499         int                     ret = IRQ_NONE;
500
501         do {
502                 imr = dma_readl(atdma, EBCIMR);
503                 status = dma_readl(atdma, EBCISR);
504                 pending = status & imr;
505
506                 if (!pending)
507                         break;
508
509                 dev_vdbg(atdma->dma_common.dev,
510                         "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
511                          status, imr, pending);
512
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);
521                                 }
522                                 tasklet_schedule(&atchan->tasklet);
523                                 ret = IRQ_HANDLED;
524                         }
525                 }
526
527         } while (pending);
528
529         return ret;
530 }
531
532
533 /*--  DMA Engine API  --------------------------------------------------*/
534
535 /**
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
538  *
539  * Queue chain if DMA engine is working already
540  *
541  * Cookie increment and adding to active_list or queue must be atomic
542  */
543 static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
544 {
545         struct at_desc          *desc = txd_to_at_desc(tx);
546         struct at_dma_chan      *atchan = to_at_dma_chan(tx->chan);
547         dma_cookie_t            cookie;
548
549         spin_lock_bh(&atchan->lock);
550         cookie = atc_assign_cookie(atchan, desc);
551
552         if (list_empty(&atchan->active_list)) {
553                 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
554                                 desc->txd.cookie);
555                 atc_dostart(atchan, desc);
556                 list_add_tail(&desc->desc_node, &atchan->active_list);
557         } else {
558                 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
559                                 desc->txd.cookie);
560                 list_add_tail(&desc->desc_node, &atchan->queue);
561         }
562
563         spin_unlock_bh(&atchan->lock);
564
565         return cookie;
566 }
567
568 /**
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
575  */
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)
579 {
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;
584         size_t                  xfer_count;
585         size_t                  offset;
586         unsigned int            src_width;
587         unsigned int            dst_width;
588         u32                     ctrla;
589         u32                     ctrlb;
590
591         dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
592                         dest, src, len, flags);
593
594         if (unlikely(!len)) {
595                 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
596                 return NULL;
597         }
598
599         ctrla =   ATC_DEFAULT_CTRLA;
600         ctrlb =   ATC_DEFAULT_CTRLB | ATC_IEN
601                 | ATC_SRC_ADDR_MODE_INCR
602                 | ATC_DST_ADDR_MODE_INCR
603                 | ATC_FC_MEM2MEM;
604
605         /*
606          * We can be a lot more clever here, but this should take care
607          * of the most common optimization.
608          */
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;
615         } else {
616                 ctrla |= ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
617                 src_width = dst_width = 0;
618         }
619
620         for (offset = 0; offset < len; offset += xfer_count << src_width) {
621                 xfer_count = min_t(size_t, (len - offset) >> src_width,
622                                 ATC_BTSIZE_MAX);
623
624                 desc = atc_desc_get(atchan);
625                 if (!desc)
626                         goto err_desc_get;
627
628                 desc->lli.saddr = src + offset;
629                 desc->lli.daddr = dest + offset;
630                 desc->lli.ctrla = ctrla | xfer_count;
631                 desc->lli.ctrlb = ctrlb;
632
633                 desc->txd.cookie = 0;
634
635                 if (!first) {
636                         first = desc;
637                 } else {
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,
642                                         &first->tx_list);
643                 }
644                 prev = desc;
645         }
646
647         /* First descriptor of the chain embedds additional information */
648         first->txd.cookie = -EBUSY;
649         first->len = len;
650
651         /* set end-of-link to the last link descriptor of list*/
652         set_desc_eol(desc);
653
654         first->txd.flags = flags; /* client is in control of this ack */
655
656         return &first->txd;
657
658 err_desc_get:
659         atc_desc_put(atchan, first);
660         return NULL;
661 }
662
663
664 /**
665  * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
666  * @chan: DMA channel
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
671  */
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,
675                 unsigned long flags)
676 {
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;
681         u32                     ctrla;
682         u32                     ctrlb;
683         dma_addr_t              reg;
684         unsigned int            reg_width;
685         unsigned int            mem_width;
686         unsigned int            i;
687         struct scatterlist      *sg;
688         size_t                  total_len = 0;
689
690         dev_vdbg(chan2dev(chan), "prep_slave_sg: %s f0x%lx\n",
691                         direction == DMA_TO_DEVICE ? "TO DEVICE" : "FROM DEVICE",
692                         flags);
693
694         if (unlikely(!atslave || !sg_len)) {
695                 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
696                 return NULL;
697         }
698
699         reg_width = atslave->reg_width;
700
701         ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla;
702         ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN;
703
704         switch (direction) {
705         case DMA_TO_DEVICE:
706                 ctrla |=  ATC_DST_WIDTH(reg_width);
707                 ctrlb |=  ATC_DST_ADDR_MODE_FIXED
708                         | ATC_SRC_ADDR_MODE_INCR
709                         | ATC_FC_MEM2PER;
710                 reg = atslave->tx_reg;
711                 for_each_sg(sgl, sg, sg_len, i) {
712                         struct at_desc  *desc;
713                         u32             len;
714                         u32             mem;
715
716                         desc = atc_desc_get(atchan);
717                         if (!desc)
718                                 goto err_desc_get;
719
720                         mem = sg_dma_address(sg);
721                         len = sg_dma_len(sg);
722                         mem_width = 2;
723                         if (unlikely(mem & 3 || len & 3))
724                                 mem_width = 0;
725
726                         desc->lli.saddr = mem;
727                         desc->lli.daddr = reg;
728                         desc->lli.ctrla = ctrla
729                                         | ATC_SRC_WIDTH(mem_width)
730                                         | len >> mem_width;
731                         desc->lli.ctrlb = ctrlb;
732
733                         if (!first) {
734                                 first = desc;
735                         } else {
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,
740                                                 &first->tx_list);
741                         }
742                         prev = desc;
743                         total_len += len;
744                 }
745                 break;
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
750                         | ATC_FC_PER2MEM;
751
752                 reg = atslave->rx_reg;
753                 for_each_sg(sgl, sg, sg_len, i) {
754                         struct at_desc  *desc;
755                         u32             len;
756                         u32             mem;
757
758                         desc = atc_desc_get(atchan);
759                         if (!desc)
760                                 goto err_desc_get;
761
762                         mem = sg_dma_address(sg);
763                         len = sg_dma_len(sg);
764                         mem_width = 2;
765                         if (unlikely(mem & 3 || len & 3))
766                                 mem_width = 0;
767
768                         desc->lli.saddr = reg;
769                         desc->lli.daddr = mem;
770                         desc->lli.ctrla = ctrla
771                                         | ATC_DST_WIDTH(mem_width)
772                                         | len >> reg_width;
773                         desc->lli.ctrlb = ctrlb;
774
775                         if (!first) {
776                                 first = desc;
777                         } else {
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,
782                                                 &first->tx_list);
783                         }
784                         prev = desc;
785                         total_len += len;
786                 }
787                 break;
788         default:
789                 return NULL;
790         }
791
792         /* set end-of-link to the last link descriptor of list*/
793         set_desc_eol(prev);
794
795         /* First descriptor of the chain embedds additional information */
796         first->txd.cookie = -EBUSY;
797         first->len = total_len;
798
799         /* first link descriptor of list is responsible of flags */
800         first->txd.flags = flags; /* client is in control of this ack */
801
802         return &first->txd;
803
804 err_desc_get:
805         dev_err(chan2dev(chan), "not enough descriptors available\n");
806         atc_desc_put(atchan, first);
807         return NULL;
808 }
809
810 /**
811  * atc_dma_cyclic_check_values
812  * Check for too big/unaligned periods and unaligned DMA buffer
813  */
814 static int
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)
817 {
818         if (period_len > (ATC_BTSIZE_MAX << reg_width))
819                 goto err_out;
820         if (unlikely(period_len & ((1 << reg_width) - 1)))
821                 goto err_out;
822         if (unlikely(buf_addr & ((1 << reg_width) - 1)))
823                 goto err_out;
824         if (unlikely(!(direction & (DMA_TO_DEVICE | DMA_FROM_DEVICE))))
825                 goto err_out;
826
827         return 0;
828
829 err_out:
830         return -EINVAL;
831 }
832
833 /**
834  * atc_dma_cyclic_fill_desc - Fill one period decriptor
835  */
836 static int
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)
840 {
841         u32             ctrla;
842         unsigned int    reg_width = atslave->reg_width;
843
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;
849
850         switch (direction) {
851         case DMA_TO_DEVICE:
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
858                                 | ATC_FC_MEM2PER;
859                 break;
860
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
868                                 | ATC_FC_PER2MEM;
869                 break;
870
871         default:
872                 return -EINVAL;
873         }
874
875         return 0;
876 }
877
878 /**
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
885  */
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)
889 {
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;
896         unsigned int            i;
897
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",
900                         buf_addr,
901                         periods, buf_len, period_len);
902
903         if (unlikely(!atslave || !buf_len || !period_len)) {
904                 dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
905                 return NULL;
906         }
907
908         was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
909         if (was_cyclic) {
910                 dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
911                 return NULL;
912         }
913
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))
917                 goto err_out;
918
919         /* build cyclic linked list */
920         for (i = 0; i < periods; i++) {
921                 struct at_desc  *desc;
922
923                 desc = atc_desc_get(atchan);
924                 if (!desc)
925                         goto err_desc_get;
926
927                 if (atc_dma_cyclic_fill_desc(atslave, desc, i, buf_addr,
928                                                 period_len, direction))
929                         goto err_desc_get;
930
931                 atc_desc_chain(&first, &prev, desc);
932         }
933
934         /* lets make a cyclic list */
935         prev->lli.dscr = first->txd.phys;
936
937         /* First descriptor of the chain embedds additional information */
938         first->txd.cookie = -EBUSY;
939         first->len = buf_len;
940
941         return &first->txd;
942
943 err_desc_get:
944         dev_err(chan2dev(chan), "not enough descriptors available\n");
945         atc_desc_put(atchan, first);
946 err_out:
947         clear_bit(ATC_IS_CYCLIC, &atchan->status);
948         return NULL;
949 }
950
951
952 static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
953                        unsigned long arg)
954 {
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;
958         LIST_HEAD(list);
959
960         /* Only supports DMA_TERMINATE_ALL */
961         if (cmd != DMA_TERMINATE_ALL)
962                 return -ENXIO;
963
964         /*
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.
969          */
970         spin_lock_bh(&atchan->lock);
971
972         dma_writel(atdma, CHDR, atchan->mask);
973
974         /* confirm that this channel is disabled */
975         while (dma_readl(atdma, CHSR) & atchan->mask)
976                 cpu_relax();
977
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);
981
982         /* Flush all pending and queued descriptors */
983         list_for_each_entry_safe(desc, _desc, &list, desc_node)
984                 atc_chain_complete(atchan, desc);
985
986         /* if channel dedicated to cyclic operations, free it */
987         clear_bit(ATC_IS_CYCLIC, &atchan->status);
988
989         spin_unlock_bh(&atchan->lock);
990
991         return 0;
992 }
993
994 /**
995  * atc_tx_status - poll for transaction completion
996  * @chan: DMA channel
997  * @cookie: transaction identifier to check status of
998  * @txstate: if not %NULL updated with transaction state
999  *
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.
1003  */
1004 static enum dma_status
1005 atc_tx_status(struct dma_chan *chan,
1006                 dma_cookie_t cookie,
1007                 struct dma_tx_state *txstate)
1008 {
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;
1013
1014         spin_lock_bh(&atchan->lock);
1015
1016         last_complete = atchan->completed_cookie;
1017         last_used = chan->cookie;
1018
1019         ret = dma_async_is_complete(cookie, last_complete, last_used);
1020         if (ret != DMA_SUCCESS) {
1021                 atc_cleanup_descriptors(atchan);
1022
1023                 last_complete = atchan->completed_cookie;
1024                 last_used = chan->cookie;
1025
1026                 ret = dma_async_is_complete(cookie, last_complete, last_used);
1027         }
1028
1029         spin_unlock_bh(&atchan->lock);
1030
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);
1035
1036         return ret;
1037 }
1038
1039 /**
1040  * atc_issue_pending - try to finish work
1041  * @chan: target DMA channel
1042  */
1043 static void atc_issue_pending(struct dma_chan *chan)
1044 {
1045         struct at_dma_chan      *atchan = to_at_dma_chan(chan);
1046
1047         dev_vdbg(chan2dev(chan), "issue_pending\n");
1048
1049         /* Not needed for cyclic transfers */
1050         if (test_bit(ATC_IS_CYCLIC, &atchan->status))
1051                 return;
1052
1053         spin_lock_bh(&atchan->lock);
1054         if (!atc_chan_is_enabled(atchan)) {
1055                 atc_advance_work(atchan);
1056         }
1057         spin_unlock_bh(&atchan->lock);
1058 }
1059
1060 /**
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
1064  *
1065  * return - the number of allocated descriptors
1066  */
1067 static int atc_alloc_chan_resources(struct dma_chan *chan)
1068 {
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;
1073         int                     i;
1074         u32                     cfg;
1075         LIST_HEAD(tmp_list);
1076
1077         dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
1078
1079         /* ASSERT:  channel is idle */
1080         if (atc_chan_is_enabled(atchan)) {
1081                 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
1082                 return -EIO;
1083         }
1084
1085         cfg = ATC_DEFAULT_CFG;
1086
1087         atslave = chan->private;
1088         if (atslave) {
1089                 /*
1090                  * We need controller-specific data to set up slave
1091                  * transfers.
1092                  */
1093                 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
1094
1095                 /* if cfg configuration specified take it instad of default */
1096                 if (atslave->cfg)
1097                         cfg = atslave->cfg;
1098         }
1099
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;
1104
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);
1108                 if (!desc) {
1109                         dev_err(atdma->dma_common.dev,
1110                                 "Only %d initial descriptors\n", i);
1111                         break;
1112                 }
1113                 list_add_tail(&desc->desc_node, &tmp_list);
1114         }
1115
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);
1121
1122         /* channel parameters */
1123         channel_writel(atchan, CFG, cfg);
1124
1125         dev_dbg(chan2dev(chan),
1126                 "alloc_chan_resources: allocated %d descriptors\n",
1127                 atchan->descs_allocated);
1128
1129         return atchan->descs_allocated;
1130 }
1131
1132 /**
1133  * atc_free_chan_resources - free all channel resources
1134  * @chan: DMA channel
1135  */
1136 static void atc_free_chan_resources(struct dma_chan *chan)
1137 {
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;
1141         LIST_HEAD(list);
1142
1143         dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
1144                 atchan->descs_allocated);
1145
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));
1150
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);
1156         }
1157         list_splice_init(&atchan->free_list, &list);
1158         atchan->descs_allocated = 0;
1159         atchan->status = 0;
1160
1161         dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
1162 }
1163
1164
1165 /*--  Module Management  -----------------------------------------------*/
1166
1167 /**
1168  * at_dma_off - disable DMA controller
1169  * @atdma: the Atmel HDAMC device
1170  */
1171 static void at_dma_off(struct at_dma *atdma)
1172 {
1173         dma_writel(atdma, EN, 0);
1174
1175         /* disable all interrupts */
1176         dma_writel(atdma, EBCIDR, -1L);
1177
1178         /* confirm that all channels are disabled */
1179         while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
1180                 cpu_relax();
1181 }
1182
1183 static int __init at_dma_probe(struct platform_device *pdev)
1184 {
1185         struct at_dma_platform_data *pdata;
1186         struct resource         *io;
1187         struct at_dma           *atdma;
1188         size_t                  size;
1189         int                     irq;
1190         int                     err;
1191         int                     i;
1192
1193         /* get DMA Controller parameters from platform */
1194         pdata = pdev->dev.platform_data;
1195         if (!pdata || pdata->nr_channels > AT_DMA_MAX_NR_CHANNELS)
1196                 return -EINVAL;
1197
1198         io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1199         if (!io)
1200                 return -EINVAL;
1201
1202         irq = platform_get_irq(pdev, 0);
1203         if (irq < 0)
1204                 return irq;
1205
1206         size = sizeof(struct at_dma);
1207         size += pdata->nr_channels * sizeof(struct at_dma_chan);
1208         atdma = kzalloc(size, GFP_KERNEL);
1209         if (!atdma)
1210                 return -ENOMEM;
1211
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;
1215
1216         size = io->end - io->start + 1;
1217         if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1218                 err = -EBUSY;
1219                 goto err_kfree;
1220         }
1221
1222         atdma->regs = ioremap(io->start, size);
1223         if (!atdma->regs) {
1224                 err = -ENOMEM;
1225                 goto err_release_r;
1226         }
1227
1228         atdma->clk = clk_get(&pdev->dev, "dma_clk");
1229         if (IS_ERR(atdma->clk)) {
1230                 err = PTR_ERR(atdma->clk);
1231                 goto err_clk;
1232         }
1233         clk_enable(atdma->clk);
1234
1235         /* force dma off, just in case */
1236         at_dma_off(atdma);
1237
1238         err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1239         if (err)
1240                 goto err_irq;
1241
1242         platform_set_drvdata(pdev, atdma);
1243
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");
1250                 err = -ENOMEM;
1251                 goto err_pool_create;
1252         }
1253
1254         /* clear any pending interrupt */
1255         while (dma_readl(atdma, EBCISR))
1256                 cpu_relax();
1257
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];
1262
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);
1268
1269                 atchan->ch_regs = atdma->regs + ch_regs(i);
1270                 spin_lock_init(&atchan->lock);
1271                 atchan->mask = 1 << i;
1272
1273                 INIT_LIST_HEAD(&atchan->active_list);
1274                 INIT_LIST_HEAD(&atchan->queue);
1275                 INIT_LIST_HEAD(&atchan->free_list);
1276
1277                 tasklet_init(&atchan->tasklet, atc_tasklet,
1278                                 (unsigned long)atchan);
1279                 atc_enable_irq(atchan);
1280         }
1281
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;
1288
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;
1292
1293         if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask))
1294                 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
1295
1296         if (dma_has_cap(DMA_CYCLIC, atdma->dma_common.cap_mask))
1297                 atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
1298
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;
1302
1303         dma_writel(atdma, EN, AT_DMA_ENABLE);
1304
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);
1309
1310         dma_async_device_register(&atdma->dma_common);
1311
1312         return 0;
1313
1314 err_pool_create:
1315         platform_set_drvdata(pdev, NULL);
1316         free_irq(platform_get_irq(pdev, 0), atdma);
1317 err_irq:
1318         clk_disable(atdma->clk);
1319         clk_put(atdma->clk);
1320 err_clk:
1321         iounmap(atdma->regs);
1322         atdma->regs = NULL;
1323 err_release_r:
1324         release_mem_region(io->start, size);
1325 err_kfree:
1326         kfree(atdma);
1327         return err;
1328 }
1329
1330 static int __exit at_dma_remove(struct platform_device *pdev)
1331 {
1332         struct at_dma           *atdma = platform_get_drvdata(pdev);
1333         struct dma_chan         *chan, *_chan;
1334         struct resource         *io;
1335
1336         at_dma_off(atdma);
1337         dma_async_device_unregister(&atdma->dma_common);
1338
1339         dma_pool_destroy(atdma->dma_desc_pool);
1340         platform_set_drvdata(pdev, NULL);
1341         free_irq(platform_get_irq(pdev, 0), atdma);
1342
1343         list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1344                         device_node) {
1345                 struct at_dma_chan      *atchan = to_at_dma_chan(chan);
1346
1347                 /* Disable interrupts */
1348                 atc_disable_irq(atchan);
1349                 tasklet_disable(&atchan->tasklet);
1350
1351                 tasklet_kill(&atchan->tasklet);
1352                 list_del(&chan->device_node);
1353         }
1354
1355         clk_disable(atdma->clk);
1356         clk_put(atdma->clk);
1357
1358         iounmap(atdma->regs);
1359         atdma->regs = NULL;
1360
1361         io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1362         release_mem_region(io->start, io->end - io->start + 1);
1363
1364         kfree(atdma);
1365
1366         return 0;
1367 }
1368
1369 static void at_dma_shutdown(struct platform_device *pdev)
1370 {
1371         struct at_dma   *atdma = platform_get_drvdata(pdev);
1372
1373         at_dma_off(platform_get_drvdata(pdev));
1374         clk_disable(atdma->clk);
1375 }
1376
1377 static int at_dma_suspend_noirq(struct device *dev)
1378 {
1379         struct platform_device *pdev = to_platform_device(dev);
1380         struct at_dma *atdma = platform_get_drvdata(pdev);
1381
1382         at_dma_off(platform_get_drvdata(pdev));
1383         clk_disable(atdma->clk);
1384         return 0;
1385 }
1386
1387 static int at_dma_resume_noirq(struct device *dev)
1388 {
1389         struct platform_device *pdev = to_platform_device(dev);
1390         struct at_dma *atdma = platform_get_drvdata(pdev);
1391
1392         clk_enable(atdma->clk);
1393         dma_writel(atdma, EN, AT_DMA_ENABLE);
1394         return 0;
1395 }
1396
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,
1400 };
1401
1402 static struct platform_driver at_dma_driver = {
1403         .remove         = __exit_p(at_dma_remove),
1404         .shutdown       = at_dma_shutdown,
1405         .driver = {
1406                 .name   = "at_hdmac",
1407                 .pm     = &at_dma_dev_pm_ops,
1408         },
1409 };
1410
1411 static int __init at_dma_init(void)
1412 {
1413         return platform_driver_probe(&at_dma_driver, at_dma_probe);
1414 }
1415 subsys_initcall(at_dma_init);
1416
1417 static void __exit at_dma_exit(void)
1418 {
1419         platform_driver_unregister(&at_dma_driver);
1420 }
1421 module_exit(at_dma_exit);
1422
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");