blob: 8f50a0fb79e7b5e646f4596a7bc8b9ac10a21871 [file] [log] [blame]
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Nicolas Ferredc78baa2009-07-03 19:24:33 +020026
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 */
47static unsigned int init_nr_desc_per_channel = 64;
48module_param(init_nr_desc_per_channel, uint, 0644);
49MODULE_PARM_DESC(init_nr_desc_per_channel,
50 "initial descriptors per channel (default: 64)");
51
52
53/* prototypes */
54static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx);
55
56
57/*----------------------------------------------------------------------*/
58
59static 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
65static 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/**
Uwe Kleine-König421f91d2010-06-11 12:17:00 +020072 * atc_alloc_descriptor - allocate and return an initialized descriptor
Nicolas Ferredc78baa2009-07-03 19:24:33 +020073 * @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 */
81static 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));
Dan Williams285a3c72009-09-08 17:53:03 -070091 INIT_LIST_HEAD(&desc->tx_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +020092 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/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200103 * atc_desc_get - get an unused descriptor from free_list
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200104 * @atchan: channel we want a new descriptor for
105 */
106static 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 */
149static 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);
Dan Williams285a3c72009-09-08 17:53:03 -0700155 list_for_each_entry(child, &desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200156 dev_vdbg(chan2dev(&atchan->chan_common),
157 "moving child desc %p to freelist\n",
158 child);
Dan Williams285a3c72009-09-08 17:53:03 -0700159 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200160 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/**
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200168 * 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 */
175static 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/**
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200191 * 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 */
197static dma_cookie_t
198atc_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 */
218static 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 */
260static void
261atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
262{
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200263 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;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200269
270 /* move children to free_list */
Dan Williams285a3c72009-09-08 17:53:03 -0700271 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200272 /* move myself to free_list */
273 list_move(&desc->desc_node, &atchan->free_list);
274
Nicolas Ferreebcf9b82011-01-12 15:39:06 +0100275 /* unmap dma addresses (not on slave channels) */
Atsushi Nemoto657a77fa2009-09-08 17:53:05 -0700276 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 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200298 }
299
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200300 /* 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 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200313
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 */
326static 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 */
356static 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
Dan Williams285a3c72009-09-08 17:53:03 -0700368 list_for_each_entry(child, &desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200369 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 */
387static 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 */
408static 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);
Dan Williams285a3c72009-09-08 17:53:03 -0700441 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200442 atc_dump_lli(atchan, &child->lli);
443
444 /* Pretend the descriptor completed successfully */
445 atc_chain_complete(atchan, bad_desc);
446}
447
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200448/**
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 */
454static 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}
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200468
469/*-- IRQ & Tasklet ---------------------------------------------------*/
470
471static 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);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200483 if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200484 atc_handle_error(atchan);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200485 else if (test_bit(ATC_IS_CYCLIC, &atchan->status))
486 atc_handle_cyclic(atchan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200487 else
488 atc_advance_work(atchan);
489
490 spin_unlock(&atchan->lock);
491}
492
493static 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];
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200515 if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200516 if (pending & AT_DMA_ERR(i)) {
517 /* Disable channel on AHB error */
518 dma_writel(atdma, CHDR, atchan->mask);
519 /* Give information to tasklet */
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200520 set_bit(ATC_IS_ERROR, &atchan->status);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200521 }
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 */
543static 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 */
576static struct dma_async_tx_descriptor *
577atc_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;
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200600 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200601 | 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;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200634
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,
Dan Williams285a3c72009-09-08 17:53:03 -0700642 &first->tx_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200643 }
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
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100654 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200655
656 return &first->txd;
657
658err_desc_get:
659 atc_desc_put(atchan, first);
660 return NULL;
661}
662
Nicolas Ferre808347f2009-07-22 20:04:45 +0200663
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 */
672static struct dma_async_tx_descriptor *
673atc_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
Nicolas Ferrecc52a102011-04-30 16:57:47 +0200690 dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
691 sg_len,
Nicolas Ferre808347f2009-07-22 20:04:45 +0200692 direction == DMA_TO_DEVICE ? "TO DEVICE" : "FROM DEVICE",
693 flags);
694
695 if (unlikely(!atslave || !sg_len)) {
696 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
697 return NULL;
698 }
699
700 reg_width = atslave->reg_width;
701
Nicolas Ferre808347f2009-07-22 20:04:45 +0200702 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla;
703 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN;
704
705 switch (direction) {
706 case DMA_TO_DEVICE:
707 ctrla |= ATC_DST_WIDTH(reg_width);
708 ctrlb |= ATC_DST_ADDR_MODE_FIXED
709 | ATC_SRC_ADDR_MODE_INCR
710 | ATC_FC_MEM2PER;
711 reg = atslave->tx_reg;
712 for_each_sg(sgl, sg, sg_len, i) {
713 struct at_desc *desc;
714 u32 len;
715 u32 mem;
716
717 desc = atc_desc_get(atchan);
718 if (!desc)
719 goto err_desc_get;
720
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100721 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200722 len = sg_dma_len(sg);
723 mem_width = 2;
724 if (unlikely(mem & 3 || len & 3))
725 mem_width = 0;
726
727 desc->lli.saddr = mem;
728 desc->lli.daddr = reg;
729 desc->lli.ctrla = ctrla
730 | ATC_SRC_WIDTH(mem_width)
731 | len >> mem_width;
732 desc->lli.ctrlb = ctrlb;
733
734 if (!first) {
735 first = desc;
736 } else {
737 /* inform the HW lli about chaining */
738 prev->lli.dscr = desc->txd.phys;
739 /* insert the link descriptor to the LD ring */
740 list_add_tail(&desc->desc_node,
Dan Williams285a3c72009-09-08 17:53:03 -0700741 &first->tx_list);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200742 }
743 prev = desc;
744 total_len += len;
745 }
746 break;
747 case DMA_FROM_DEVICE:
748 ctrla |= ATC_SRC_WIDTH(reg_width);
749 ctrlb |= ATC_DST_ADDR_MODE_INCR
750 | ATC_SRC_ADDR_MODE_FIXED
751 | ATC_FC_PER2MEM;
752
753 reg = atslave->rx_reg;
754 for_each_sg(sgl, sg, sg_len, i) {
755 struct at_desc *desc;
756 u32 len;
757 u32 mem;
758
759 desc = atc_desc_get(atchan);
760 if (!desc)
761 goto err_desc_get;
762
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100763 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200764 len = sg_dma_len(sg);
765 mem_width = 2;
766 if (unlikely(mem & 3 || len & 3))
767 mem_width = 0;
768
769 desc->lli.saddr = reg;
770 desc->lli.daddr = mem;
771 desc->lli.ctrla = ctrla
772 | ATC_DST_WIDTH(mem_width)
Nicolas Ferre59a609d2010-12-13 13:48:41 +0100773 | len >> reg_width;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200774 desc->lli.ctrlb = ctrlb;
775
776 if (!first) {
777 first = desc;
778 } else {
779 /* inform the HW lli about chaining */
780 prev->lli.dscr = desc->txd.phys;
781 /* insert the link descriptor to the LD ring */
782 list_add_tail(&desc->desc_node,
Dan Williams285a3c72009-09-08 17:53:03 -0700783 &first->tx_list);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200784 }
785 prev = desc;
786 total_len += len;
787 }
788 break;
789 default:
790 return NULL;
791 }
792
793 /* set end-of-link to the last link descriptor of list*/
794 set_desc_eol(prev);
795
796 /* First descriptor of the chain embedds additional information */
797 first->txd.cookie = -EBUSY;
798 first->len = total_len;
799
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100800 /* first link descriptor of list is responsible of flags */
801 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferre808347f2009-07-22 20:04:45 +0200802
803 return &first->txd;
804
805err_desc_get:
806 dev_err(chan2dev(chan), "not enough descriptors available\n");
807 atc_desc_put(atchan, first);
808 return NULL;
809}
810
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200811/**
812 * atc_dma_cyclic_check_values
813 * Check for too big/unaligned periods and unaligned DMA buffer
814 */
815static int
816atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
817 size_t period_len, enum dma_data_direction direction)
818{
819 if (period_len > (ATC_BTSIZE_MAX << reg_width))
820 goto err_out;
821 if (unlikely(period_len & ((1 << reg_width) - 1)))
822 goto err_out;
823 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
824 goto err_out;
825 if (unlikely(!(direction & (DMA_TO_DEVICE | DMA_FROM_DEVICE))))
826 goto err_out;
827
828 return 0;
829
830err_out:
831 return -EINVAL;
832}
833
834/**
835 * atc_dma_cyclic_fill_desc - Fill one period decriptor
836 */
837static int
838atc_dma_cyclic_fill_desc(struct at_dma_slave *atslave, struct at_desc *desc,
839 unsigned int period_index, dma_addr_t buf_addr,
840 size_t period_len, enum dma_data_direction direction)
841{
842 u32 ctrla;
843 unsigned int reg_width = atslave->reg_width;
844
845 /* prepare common CRTLA value */
846 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla
847 | ATC_DST_WIDTH(reg_width)
848 | ATC_SRC_WIDTH(reg_width)
849 | period_len >> reg_width;
850
851 switch (direction) {
852 case DMA_TO_DEVICE:
853 desc->lli.saddr = buf_addr + (period_len * period_index);
854 desc->lli.daddr = atslave->tx_reg;
855 desc->lli.ctrla = ctrla;
856 desc->lli.ctrlb = ATC_DEFAULT_CTRLB
857 | ATC_DST_ADDR_MODE_FIXED
858 | ATC_SRC_ADDR_MODE_INCR
859 | ATC_FC_MEM2PER;
860 break;
861
862 case DMA_FROM_DEVICE:
863 desc->lli.saddr = atslave->rx_reg;
864 desc->lli.daddr = buf_addr + (period_len * period_index);
865 desc->lli.ctrla = ctrla;
866 desc->lli.ctrlb = ATC_DEFAULT_CTRLB
867 | ATC_DST_ADDR_MODE_INCR
868 | ATC_SRC_ADDR_MODE_FIXED
869 | ATC_FC_PER2MEM;
870 break;
871
872 default:
873 return -EINVAL;
874 }
875
876 return 0;
877}
878
879/**
880 * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
881 * @chan: the DMA channel to prepare
882 * @buf_addr: physical DMA address where the buffer starts
883 * @buf_len: total number of bytes for the entire buffer
884 * @period_len: number of bytes for each period
885 * @direction: transfer direction, to or from device
886 */
887static struct dma_async_tx_descriptor *
888atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
889 size_t period_len, enum dma_data_direction direction)
890{
891 struct at_dma_chan *atchan = to_at_dma_chan(chan);
892 struct at_dma_slave *atslave = chan->private;
893 struct at_desc *first = NULL;
894 struct at_desc *prev = NULL;
895 unsigned long was_cyclic;
896 unsigned int periods = buf_len / period_len;
897 unsigned int i;
898
899 dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
900 direction == DMA_TO_DEVICE ? "TO DEVICE" : "FROM DEVICE",
901 buf_addr,
902 periods, buf_len, period_len);
903
904 if (unlikely(!atslave || !buf_len || !period_len)) {
905 dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
906 return NULL;
907 }
908
909 was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
910 if (was_cyclic) {
911 dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
912 return NULL;
913 }
914
915 /* Check for too big/unaligned periods and unaligned DMA buffer */
916 if (atc_dma_cyclic_check_values(atslave->reg_width, buf_addr,
917 period_len, direction))
918 goto err_out;
919
920 /* build cyclic linked list */
921 for (i = 0; i < periods; i++) {
922 struct at_desc *desc;
923
924 desc = atc_desc_get(atchan);
925 if (!desc)
926 goto err_desc_get;
927
928 if (atc_dma_cyclic_fill_desc(atslave, desc, i, buf_addr,
929 period_len, direction))
930 goto err_desc_get;
931
932 atc_desc_chain(&first, &prev, desc);
933 }
934
935 /* lets make a cyclic list */
936 prev->lli.dscr = first->txd.phys;
937
938 /* First descriptor of the chain embedds additional information */
939 first->txd.cookie = -EBUSY;
940 first->len = buf_len;
941
942 return &first->txd;
943
944err_desc_get:
945 dev_err(chan2dev(chan), "not enough descriptors available\n");
946 atc_desc_put(atchan, first);
947err_out:
948 clear_bit(ATC_IS_CYCLIC, &atchan->status);
949 return NULL;
950}
951
952
Linus Walleij05827632010-05-17 16:30:42 -0700953static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
954 unsigned long arg)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200955{
956 struct at_dma_chan *atchan = to_at_dma_chan(chan);
957 struct at_dma *atdma = to_at_dma(chan->device);
958 struct at_desc *desc, *_desc;
959 LIST_HEAD(list);
960
Linus Walleijc3635c72010-03-26 16:44:01 -0700961 /* Only supports DMA_TERMINATE_ALL */
962 if (cmd != DMA_TERMINATE_ALL)
963 return -ENXIO;
964
Nicolas Ferre808347f2009-07-22 20:04:45 +0200965 /*
966 * This is only called when something went wrong elsewhere, so
967 * we don't really care about the data. Just disable the
968 * channel. We still have to poll the channel enable bit due
969 * to AHB/HSB limitations.
970 */
971 spin_lock_bh(&atchan->lock);
972
973 dma_writel(atdma, CHDR, atchan->mask);
974
975 /* confirm that this channel is disabled */
976 while (dma_readl(atdma, CHSR) & atchan->mask)
977 cpu_relax();
978
979 /* active_list entries will end up before queued entries */
980 list_splice_init(&atchan->queue, &list);
981 list_splice_init(&atchan->active_list, &list);
982
Nicolas Ferre808347f2009-07-22 20:04:45 +0200983 /* Flush all pending and queued descriptors */
984 list_for_each_entry_safe(desc, _desc, &list, desc_node)
985 atc_chain_complete(atchan, desc);
Linus Walleijc3635c72010-03-26 16:44:01 -0700986
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200987 /* if channel dedicated to cyclic operations, free it */
988 clear_bit(ATC_IS_CYCLIC, &atchan->status);
989
Yong Wangb0ebeb92010-08-05 10:40:08 +0800990 spin_unlock_bh(&atchan->lock);
991
Linus Walleijc3635c72010-03-26 16:44:01 -0700992 return 0;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200993}
994
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200995/**
Linus Walleij07934482010-03-26 16:50:49 -0700996 * atc_tx_status - poll for transaction completion
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200997 * @chan: DMA channel
998 * @cookie: transaction identifier to check status of
Linus Walleij07934482010-03-26 16:50:49 -0700999 * @txstate: if not %NULL updated with transaction state
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001000 *
Linus Walleij07934482010-03-26 16:50:49 -07001001 * If @txstate is passed in, upon return it reflect the driver
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001002 * internal state and can be used with dma_async_is_complete() to check
1003 * the status of multiple cookies without re-checking hardware state.
1004 */
1005static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -07001006atc_tx_status(struct dma_chan *chan,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001007 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -07001008 struct dma_tx_state *txstate)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001009{
1010 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1011 dma_cookie_t last_used;
1012 dma_cookie_t last_complete;
1013 enum dma_status ret;
1014
Nicolas Ferre4297a462009-12-16 16:28:03 +01001015 spin_lock_bh(&atchan->lock);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001016
1017 last_complete = atchan->completed_cookie;
1018 last_used = chan->cookie;
1019
1020 ret = dma_async_is_complete(cookie, last_complete, last_used);
1021 if (ret != DMA_SUCCESS) {
1022 atc_cleanup_descriptors(atchan);
1023
1024 last_complete = atchan->completed_cookie;
1025 last_used = chan->cookie;
1026
1027 ret = dma_async_is_complete(cookie, last_complete, last_used);
1028 }
1029
Nicolas Ferre4297a462009-12-16 16:28:03 +01001030 spin_unlock_bh(&atchan->lock);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001031
Dan Williamsbca34692010-03-26 16:52:10 -07001032 dma_set_tx_state(txstate, last_complete, last_used, 0);
Linus Walleij07934482010-03-26 16:50:49 -07001033 dev_vdbg(chan2dev(chan), "tx_status: %d (d%d, u%d)\n",
1034 cookie, last_complete ? last_complete : 0,
1035 last_used ? last_used : 0);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001036
1037 return ret;
1038}
1039
1040/**
1041 * atc_issue_pending - try to finish work
1042 * @chan: target DMA channel
1043 */
1044static void atc_issue_pending(struct dma_chan *chan)
1045{
1046 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1047
1048 dev_vdbg(chan2dev(chan), "issue_pending\n");
1049
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001050 /* Not needed for cyclic transfers */
1051 if (test_bit(ATC_IS_CYCLIC, &atchan->status))
1052 return;
1053
Nicolas Ferredda36f92011-01-12 15:39:10 +01001054 spin_lock_bh(&atchan->lock);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001055 if (!atc_chan_is_enabled(atchan)) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001056 atc_advance_work(atchan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001057 }
Nicolas Ferredda36f92011-01-12 15:39:10 +01001058 spin_unlock_bh(&atchan->lock);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001059}
1060
1061/**
1062 * atc_alloc_chan_resources - allocate resources for DMA channel
1063 * @chan: allocate descriptor resources for this channel
1064 * @client: current client requesting the channel be ready for requests
1065 *
1066 * return - the number of allocated descriptors
1067 */
1068static int atc_alloc_chan_resources(struct dma_chan *chan)
1069{
1070 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1071 struct at_dma *atdma = to_at_dma(chan->device);
1072 struct at_desc *desc;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001073 struct at_dma_slave *atslave;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001074 int i;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001075 u32 cfg;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001076 LIST_HEAD(tmp_list);
1077
1078 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
1079
1080 /* ASSERT: channel is idle */
1081 if (atc_chan_is_enabled(atchan)) {
1082 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
1083 return -EIO;
1084 }
1085
Nicolas Ferre808347f2009-07-22 20:04:45 +02001086 cfg = ATC_DEFAULT_CFG;
1087
1088 atslave = chan->private;
1089 if (atslave) {
1090 /*
1091 * We need controller-specific data to set up slave
1092 * transfers.
1093 */
1094 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
1095
1096 /* if cfg configuration specified take it instad of default */
1097 if (atslave->cfg)
1098 cfg = atslave->cfg;
1099 }
1100
1101 /* have we already been set up?
1102 * reconfigure channel but no need to reallocate descriptors */
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001103 if (!list_empty(&atchan->free_list))
1104 return atchan->descs_allocated;
1105
1106 /* Allocate initial pool of descriptors */
1107 for (i = 0; i < init_nr_desc_per_channel; i++) {
1108 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
1109 if (!desc) {
1110 dev_err(atdma->dma_common.dev,
1111 "Only %d initial descriptors\n", i);
1112 break;
1113 }
1114 list_add_tail(&desc->desc_node, &tmp_list);
1115 }
1116
1117 spin_lock_bh(&atchan->lock);
1118 atchan->descs_allocated = i;
1119 list_splice(&tmp_list, &atchan->free_list);
1120 atchan->completed_cookie = chan->cookie = 1;
1121 spin_unlock_bh(&atchan->lock);
1122
1123 /* channel parameters */
Nicolas Ferre808347f2009-07-22 20:04:45 +02001124 channel_writel(atchan, CFG, cfg);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001125
1126 dev_dbg(chan2dev(chan),
1127 "alloc_chan_resources: allocated %d descriptors\n",
1128 atchan->descs_allocated);
1129
1130 return atchan->descs_allocated;
1131}
1132
1133/**
1134 * atc_free_chan_resources - free all channel resources
1135 * @chan: DMA channel
1136 */
1137static void atc_free_chan_resources(struct dma_chan *chan)
1138{
1139 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1140 struct at_dma *atdma = to_at_dma(chan->device);
1141 struct at_desc *desc, *_desc;
1142 LIST_HEAD(list);
1143
1144 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
1145 atchan->descs_allocated);
1146
1147 /* ASSERT: channel is idle */
1148 BUG_ON(!list_empty(&atchan->active_list));
1149 BUG_ON(!list_empty(&atchan->queue));
1150 BUG_ON(atc_chan_is_enabled(atchan));
1151
1152 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
1153 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1154 list_del(&desc->desc_node);
1155 /* free link descriptor */
1156 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
1157 }
1158 list_splice_init(&atchan->free_list, &list);
1159 atchan->descs_allocated = 0;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001160 atchan->status = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001161
1162 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
1163}
1164
1165
1166/*-- Module Management -----------------------------------------------*/
1167
1168/**
1169 * at_dma_off - disable DMA controller
1170 * @atdma: the Atmel HDAMC device
1171 */
1172static void at_dma_off(struct at_dma *atdma)
1173{
1174 dma_writel(atdma, EN, 0);
1175
1176 /* disable all interrupts */
1177 dma_writel(atdma, EBCIDR, -1L);
1178
1179 /* confirm that all channels are disabled */
1180 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
1181 cpu_relax();
1182}
1183
1184static int __init at_dma_probe(struct platform_device *pdev)
1185{
1186 struct at_dma_platform_data *pdata;
1187 struct resource *io;
1188 struct at_dma *atdma;
1189 size_t size;
1190 int irq;
1191 int err;
1192 int i;
1193
1194 /* get DMA Controller parameters from platform */
1195 pdata = pdev->dev.platform_data;
1196 if (!pdata || pdata->nr_channels > AT_DMA_MAX_NR_CHANNELS)
1197 return -EINVAL;
1198
1199 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1200 if (!io)
1201 return -EINVAL;
1202
1203 irq = platform_get_irq(pdev, 0);
1204 if (irq < 0)
1205 return irq;
1206
1207 size = sizeof(struct at_dma);
1208 size += pdata->nr_channels * sizeof(struct at_dma_chan);
1209 atdma = kzalloc(size, GFP_KERNEL);
1210 if (!atdma)
1211 return -ENOMEM;
1212
1213 /* discover transaction capabilites from the platform data */
1214 atdma->dma_common.cap_mask = pdata->cap_mask;
1215 atdma->all_chan_mask = (1 << pdata->nr_channels) - 1;
1216
1217 size = io->end - io->start + 1;
1218 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1219 err = -EBUSY;
1220 goto err_kfree;
1221 }
1222
1223 atdma->regs = ioremap(io->start, size);
1224 if (!atdma->regs) {
1225 err = -ENOMEM;
1226 goto err_release_r;
1227 }
1228
1229 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1230 if (IS_ERR(atdma->clk)) {
1231 err = PTR_ERR(atdma->clk);
1232 goto err_clk;
1233 }
1234 clk_enable(atdma->clk);
1235
1236 /* force dma off, just in case */
1237 at_dma_off(atdma);
1238
1239 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1240 if (err)
1241 goto err_irq;
1242
1243 platform_set_drvdata(pdev, atdma);
1244
1245 /* create a pool of consistent memory blocks for hardware descriptors */
1246 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1247 &pdev->dev, sizeof(struct at_desc),
1248 4 /* word alignment */, 0);
1249 if (!atdma->dma_desc_pool) {
1250 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1251 err = -ENOMEM;
1252 goto err_pool_create;
1253 }
1254
1255 /* clear any pending interrupt */
1256 while (dma_readl(atdma, EBCISR))
1257 cpu_relax();
1258
1259 /* initialize channels related values */
1260 INIT_LIST_HEAD(&atdma->dma_common.channels);
1261 for (i = 0; i < pdata->nr_channels; i++, atdma->dma_common.chancnt++) {
1262 struct at_dma_chan *atchan = &atdma->chan[i];
1263
1264 atchan->chan_common.device = &atdma->dma_common;
1265 atchan->chan_common.cookie = atchan->completed_cookie = 1;
1266 atchan->chan_common.chan_id = i;
1267 list_add_tail(&atchan->chan_common.device_node,
1268 &atdma->dma_common.channels);
1269
1270 atchan->ch_regs = atdma->regs + ch_regs(i);
1271 spin_lock_init(&atchan->lock);
1272 atchan->mask = 1 << i;
1273
1274 INIT_LIST_HEAD(&atchan->active_list);
1275 INIT_LIST_HEAD(&atchan->queue);
1276 INIT_LIST_HEAD(&atchan->free_list);
1277
1278 tasklet_init(&atchan->tasklet, atc_tasklet,
1279 (unsigned long)atchan);
1280 atc_enable_irq(atchan);
1281 }
1282
1283 /* set base routines */
1284 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1285 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
Linus Walleij07934482010-03-26 16:50:49 -07001286 atdma->dma_common.device_tx_status = atc_tx_status;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001287 atdma->dma_common.device_issue_pending = atc_issue_pending;
1288 atdma->dma_common.dev = &pdev->dev;
1289
1290 /* set prep routines based on capability */
1291 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1292 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1293
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001294 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask))
Nicolas Ferre808347f2009-07-22 20:04:45 +02001295 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001296
1297 if (dma_has_cap(DMA_CYCLIC, atdma->dma_common.cap_mask))
1298 atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
1299
1300 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ||
1301 dma_has_cap(DMA_CYCLIC, atdma->dma_common.cap_mask))
Linus Walleijc3635c72010-03-26 16:44:01 -07001302 atdma->dma_common.device_control = atc_control;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001303
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001304 dma_writel(atdma, EN, AT_DMA_ENABLE);
1305
1306 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
1307 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1308 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
1309 atdma->dma_common.chancnt);
1310
1311 dma_async_device_register(&atdma->dma_common);
1312
1313 return 0;
1314
1315err_pool_create:
1316 platform_set_drvdata(pdev, NULL);
1317 free_irq(platform_get_irq(pdev, 0), atdma);
1318err_irq:
1319 clk_disable(atdma->clk);
1320 clk_put(atdma->clk);
1321err_clk:
1322 iounmap(atdma->regs);
1323 atdma->regs = NULL;
1324err_release_r:
1325 release_mem_region(io->start, size);
1326err_kfree:
1327 kfree(atdma);
1328 return err;
1329}
1330
1331static int __exit at_dma_remove(struct platform_device *pdev)
1332{
1333 struct at_dma *atdma = platform_get_drvdata(pdev);
1334 struct dma_chan *chan, *_chan;
1335 struct resource *io;
1336
1337 at_dma_off(atdma);
1338 dma_async_device_unregister(&atdma->dma_common);
1339
1340 dma_pool_destroy(atdma->dma_desc_pool);
1341 platform_set_drvdata(pdev, NULL);
1342 free_irq(platform_get_irq(pdev, 0), atdma);
1343
1344 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1345 device_node) {
1346 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1347
1348 /* Disable interrupts */
1349 atc_disable_irq(atchan);
1350 tasklet_disable(&atchan->tasklet);
1351
1352 tasklet_kill(&atchan->tasklet);
1353 list_del(&chan->device_node);
1354 }
1355
1356 clk_disable(atdma->clk);
1357 clk_put(atdma->clk);
1358
1359 iounmap(atdma->regs);
1360 atdma->regs = NULL;
1361
1362 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1363 release_mem_region(io->start, io->end - io->start + 1);
1364
1365 kfree(atdma);
1366
1367 return 0;
1368}
1369
1370static void at_dma_shutdown(struct platform_device *pdev)
1371{
1372 struct at_dma *atdma = platform_get_drvdata(pdev);
1373
1374 at_dma_off(platform_get_drvdata(pdev));
1375 clk_disable(atdma->clk);
1376}
1377
Dan Williams33f82d12009-09-10 00:06:44 +02001378static int at_dma_suspend_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001379{
Dan Williams33f82d12009-09-10 00:06:44 +02001380 struct platform_device *pdev = to_platform_device(dev);
1381 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001382
1383 at_dma_off(platform_get_drvdata(pdev));
1384 clk_disable(atdma->clk);
1385 return 0;
1386}
1387
Dan Williams33f82d12009-09-10 00:06:44 +02001388static int at_dma_resume_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001389{
Dan Williams33f82d12009-09-10 00:06:44 +02001390 struct platform_device *pdev = to_platform_device(dev);
1391 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001392
1393 clk_enable(atdma->clk);
1394 dma_writel(atdma, EN, AT_DMA_ENABLE);
1395 return 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001396}
1397
Alexey Dobriyan47145212009-12-14 18:00:08 -08001398static const struct dev_pm_ops at_dma_dev_pm_ops = {
Dan Williams33f82d12009-09-10 00:06:44 +02001399 .suspend_noirq = at_dma_suspend_noirq,
1400 .resume_noirq = at_dma_resume_noirq,
1401};
1402
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001403static struct platform_driver at_dma_driver = {
1404 .remove = __exit_p(at_dma_remove),
1405 .shutdown = at_dma_shutdown,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001406 .driver = {
1407 .name = "at_hdmac",
Dan Williams33f82d12009-09-10 00:06:44 +02001408 .pm = &at_dma_dev_pm_ops,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001409 },
1410};
1411
1412static int __init at_dma_init(void)
1413{
1414 return platform_driver_probe(&at_dma_driver, at_dma_probe);
1415}
Eric Xu93d0bec2011-01-12 15:39:08 +01001416subsys_initcall(at_dma_init);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001417
1418static void __exit at_dma_exit(void)
1419{
1420 platform_driver_unregister(&at_dma_driver);
1421}
1422module_exit(at_dma_exit);
1423
1424MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1425MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1426MODULE_LICENSE("GPL");
1427MODULE_ALIAS("platform:at_hdmac");