]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/dma/at_hdmac.c
DMAENGINE: generic channel status v2
[net-next-2.6.git] / drivers / dma / at_hdmac.c
CommitLineData
dc78baa2
NF
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
26#include "at_hdmac_regs.h"
27
28/*
29 * Glossary
30 * --------
31 *
32 * at_hdmac : Name of the ATmel AHB DMA Controller
33 * at_dma_ / atdma : ATmel DMA controller entity related
34 * atc_ / atchan : ATmel DMA Channel entity related
35 */
36
37#define ATC_DEFAULT_CFG (ATC_FIFOCFG_HALFFIFO)
38#define ATC_DEFAULT_CTRLA (0)
39#define ATC_DEFAULT_CTRLB (ATC_SIF(0) \
40 |ATC_DIF(1))
41
42/*
43 * Initial number of descriptors to allocate for each channel. This could
44 * be increased during dma usage.
45 */
46static unsigned int init_nr_desc_per_channel = 64;
47module_param(init_nr_desc_per_channel, uint, 0644);
48MODULE_PARM_DESC(init_nr_desc_per_channel,
49 "initial descriptors per channel (default: 64)");
50
51
52/* prototypes */
53static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx);
54
55
56/*----------------------------------------------------------------------*/
57
58static struct at_desc *atc_first_active(struct at_dma_chan *atchan)
59{
60 return list_first_entry(&atchan->active_list,
61 struct at_desc, desc_node);
62}
63
64static struct at_desc *atc_first_queued(struct at_dma_chan *atchan)
65{
66 return list_first_entry(&atchan->queue,
67 struct at_desc, desc_node);
68}
69
70/**
71 * atc_alloc_descriptor - allocate and return an initilized descriptor
72 * @chan: the channel to allocate descriptors for
73 * @gfp_flags: GFP allocation flags
74 *
75 * Note: The ack-bit is positioned in the descriptor flag at creation time
76 * to make initial allocation more convenient. This bit will be cleared
77 * and control will be given to client at usage time (during
78 * preparation functions).
79 */
80static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan,
81 gfp_t gfp_flags)
82{
83 struct at_desc *desc = NULL;
84 struct at_dma *atdma = to_at_dma(chan->device);
85 dma_addr_t phys;
86
87 desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys);
88 if (desc) {
89 memset(desc, 0, sizeof(struct at_desc));
285a3c71 90 INIT_LIST_HEAD(&desc->tx_list);
dc78baa2
NF
91 dma_async_tx_descriptor_init(&desc->txd, chan);
92 /* txd.flags will be overwritten in prep functions */
93 desc->txd.flags = DMA_CTRL_ACK;
94 desc->txd.tx_submit = atc_tx_submit;
95 desc->txd.phys = phys;
96 }
97
98 return desc;
99}
100
101/**
af901ca1 102 * atc_desc_get - get an unused descriptor from free_list
dc78baa2
NF
103 * @atchan: channel we want a new descriptor for
104 */
105static struct at_desc *atc_desc_get(struct at_dma_chan *atchan)
106{
107 struct at_desc *desc, *_desc;
108 struct at_desc *ret = NULL;
109 unsigned int i = 0;
110 LIST_HEAD(tmp_list);
111
112 spin_lock_bh(&atchan->lock);
113 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
114 i++;
115 if (async_tx_test_ack(&desc->txd)) {
116 list_del(&desc->desc_node);
117 ret = desc;
118 break;
119 }
120 dev_dbg(chan2dev(&atchan->chan_common),
121 "desc %p not ACKed\n", desc);
122 }
123 spin_unlock_bh(&atchan->lock);
124 dev_vdbg(chan2dev(&atchan->chan_common),
125 "scanned %u descriptors on freelist\n", i);
126
127 /* no more descriptor available in initial pool: create one more */
128 if (!ret) {
129 ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC);
130 if (ret) {
131 spin_lock_bh(&atchan->lock);
132 atchan->descs_allocated++;
133 spin_unlock_bh(&atchan->lock);
134 } else {
135 dev_err(chan2dev(&atchan->chan_common),
136 "not enough descriptors available\n");
137 }
138 }
139
140 return ret;
141}
142
143/**
144 * atc_desc_put - move a descriptor, including any children, to the free list
145 * @atchan: channel we work on
146 * @desc: descriptor, at the head of a chain, to move to free list
147 */
148static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc)
149{
150 if (desc) {
151 struct at_desc *child;
152
153 spin_lock_bh(&atchan->lock);
285a3c71 154 list_for_each_entry(child, &desc->tx_list, desc_node)
dc78baa2
NF
155 dev_vdbg(chan2dev(&atchan->chan_common),
156 "moving child desc %p to freelist\n",
157 child);
285a3c71 158 list_splice_init(&desc->tx_list, &atchan->free_list);
dc78baa2
NF
159 dev_vdbg(chan2dev(&atchan->chan_common),
160 "moving desc %p to freelist\n", desc);
161 list_add(&desc->desc_node, &atchan->free_list);
162 spin_unlock_bh(&atchan->lock);
163 }
164}
165
166/**
167 * atc_assign_cookie - compute and assign new cookie
168 * @atchan: channel we work on
169 * @desc: descriptor to asign cookie for
170 *
171 * Called with atchan->lock held and bh disabled
172 */
173static dma_cookie_t
174atc_assign_cookie(struct at_dma_chan *atchan, struct at_desc *desc)
175{
176 dma_cookie_t cookie = atchan->chan_common.cookie;
177
178 if (++cookie < 0)
179 cookie = 1;
180
181 atchan->chan_common.cookie = cookie;
182 desc->txd.cookie = cookie;
183
184 return cookie;
185}
186
187/**
188 * atc_dostart - starts the DMA engine for real
189 * @atchan: the channel we want to start
190 * @first: first descriptor in the list we want to begin with
191 *
192 * Called with atchan->lock held and bh disabled
193 */
194static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
195{
196 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
197
198 /* ASSERT: channel is idle */
199 if (atc_chan_is_enabled(atchan)) {
200 dev_err(chan2dev(&atchan->chan_common),
201 "BUG: Attempted to start non-idle channel\n");
202 dev_err(chan2dev(&atchan->chan_common),
203 " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
204 channel_readl(atchan, SADDR),
205 channel_readl(atchan, DADDR),
206 channel_readl(atchan, CTRLA),
207 channel_readl(atchan, CTRLB),
208 channel_readl(atchan, DSCR));
209
210 /* The tasklet will hopefully advance the queue... */
211 return;
212 }
213
214 vdbg_dump_regs(atchan);
215
216 /* clear any pending interrupt */
217 while (dma_readl(atdma, EBCISR))
218 cpu_relax();
219
220 channel_writel(atchan, SADDR, 0);
221 channel_writel(atchan, DADDR, 0);
222 channel_writel(atchan, CTRLA, 0);
223 channel_writel(atchan, CTRLB, 0);
224 channel_writel(atchan, DSCR, first->txd.phys);
225 dma_writel(atdma, CHER, atchan->mask);
226
227 vdbg_dump_regs(atchan);
228}
229
230/**
231 * atc_chain_complete - finish work for one transaction chain
232 * @atchan: channel we work on
233 * @desc: descriptor at the head of the chain we want do complete
234 *
235 * Called with atchan->lock held and bh disabled */
236static void
237atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
238{
239 dma_async_tx_callback callback;
240 void *param;
241 struct dma_async_tx_descriptor *txd = &desc->txd;
242
243 dev_vdbg(chan2dev(&atchan->chan_common),
244 "descriptor %u complete\n", txd->cookie);
245
246 atchan->completed_cookie = txd->cookie;
247 callback = txd->callback;
248 param = txd->callback_param;
249
250 /* move children to free_list */
285a3c71 251 list_splice_init(&desc->tx_list, &atchan->free_list);
dc78baa2
NF
252 /* move myself to free_list */
253 list_move(&desc->desc_node, &atchan->free_list);
254
255 /* unmap dma addresses */
657a77fa
AN
256 if (!atchan->chan_common.private) {
257 struct device *parent = chan2parent(&atchan->chan_common);
258 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
259 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
260 dma_unmap_single(parent,
261 desc->lli.daddr,
262 desc->len, DMA_FROM_DEVICE);
263 else
264 dma_unmap_page(parent,
265 desc->lli.daddr,
266 desc->len, DMA_FROM_DEVICE);
267 }
268 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
269 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
270 dma_unmap_single(parent,
271 desc->lli.saddr,
272 desc->len, DMA_TO_DEVICE);
273 else
274 dma_unmap_page(parent,
275 desc->lli.saddr,
276 desc->len, DMA_TO_DEVICE);
277 }
dc78baa2
NF
278 }
279
280 /*
281 * The API requires that no submissions are done from a
282 * callback, so we don't need to drop the lock here
283 */
284 if (callback)
285 callback(param);
286
287 dma_run_dependencies(txd);
288}
289
290/**
291 * atc_complete_all - finish work for all transactions
292 * @atchan: channel to complete transactions for
293 *
294 * Eventually submit queued descriptors if any
295 *
296 * Assume channel is idle while calling this function
297 * Called with atchan->lock held and bh disabled
298 */
299static void atc_complete_all(struct at_dma_chan *atchan)
300{
301 struct at_desc *desc, *_desc;
302 LIST_HEAD(list);
303
304 dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
305
306 BUG_ON(atc_chan_is_enabled(atchan));
307
308 /*
309 * Submit queued descriptors ASAP, i.e. before we go through
310 * the completed ones.
311 */
312 if (!list_empty(&atchan->queue))
313 atc_dostart(atchan, atc_first_queued(atchan));
314 /* empty active_list now it is completed */
315 list_splice_init(&atchan->active_list, &list);
316 /* empty queue list by moving descriptors (if any) to active_list */
317 list_splice_init(&atchan->queue, &atchan->active_list);
318
319 list_for_each_entry_safe(desc, _desc, &list, desc_node)
320 atc_chain_complete(atchan, desc);
321}
322
323/**
324 * atc_cleanup_descriptors - cleanup up finished descriptors in active_list
325 * @atchan: channel to be cleaned up
326 *
327 * Called with atchan->lock held and bh disabled
328 */
329static void atc_cleanup_descriptors(struct at_dma_chan *atchan)
330{
331 struct at_desc *desc, *_desc;
332 struct at_desc *child;
333
334 dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n");
335
336 list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
337 if (!(desc->lli.ctrla & ATC_DONE))
338 /* This one is currently in progress */
339 return;
340
285a3c71 341 list_for_each_entry(child, &desc->tx_list, desc_node)
dc78baa2
NF
342 if (!(child->lli.ctrla & ATC_DONE))
343 /* Currently in progress */
344 return;
345
346 /*
347 * No descriptors so far seem to be in progress, i.e.
348 * this chain must be done.
349 */
350 atc_chain_complete(atchan, desc);
351 }
352}
353
354/**
355 * atc_advance_work - at the end of a transaction, move forward
356 * @atchan: channel where the transaction ended
357 *
358 * Called with atchan->lock held and bh disabled
359 */
360static void atc_advance_work(struct at_dma_chan *atchan)
361{
362 dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
363
364 if (list_empty(&atchan->active_list) ||
365 list_is_singular(&atchan->active_list)) {
366 atc_complete_all(atchan);
367 } else {
368 atc_chain_complete(atchan, atc_first_active(atchan));
369 /* advance work */
370 atc_dostart(atchan, atc_first_active(atchan));
371 }
372}
373
374
375/**
376 * atc_handle_error - handle errors reported by DMA controller
377 * @atchan: channel where error occurs
378 *
379 * Called with atchan->lock held and bh disabled
380 */
381static void atc_handle_error(struct at_dma_chan *atchan)
382{
383 struct at_desc *bad_desc;
384 struct at_desc *child;
385
386 /*
387 * The descriptor currently at the head of the active list is
388 * broked. Since we don't have any way to report errors, we'll
389 * just have to scream loudly and try to carry on.
390 */
391 bad_desc = atc_first_active(atchan);
392 list_del_init(&bad_desc->desc_node);
393
394 /* As we are stopped, take advantage to push queued descriptors
395 * in active_list */
396 list_splice_init(&atchan->queue, atchan->active_list.prev);
397
398 /* Try to restart the controller */
399 if (!list_empty(&atchan->active_list))
400 atc_dostart(atchan, atc_first_active(atchan));
401
402 /*
403 * KERN_CRITICAL may seem harsh, but since this only happens
404 * when someone submits a bad physical address in a
405 * descriptor, we should consider ourselves lucky that the
406 * controller flagged an error instead of scribbling over
407 * random memory locations.
408 */
409 dev_crit(chan2dev(&atchan->chan_common),
410 "Bad descriptor submitted for DMA!\n");
411 dev_crit(chan2dev(&atchan->chan_common),
412 " cookie: %d\n", bad_desc->txd.cookie);
413 atc_dump_lli(atchan, &bad_desc->lli);
285a3c71 414 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
dc78baa2
NF
415 atc_dump_lli(atchan, &child->lli);
416
417 /* Pretend the descriptor completed successfully */
418 atc_chain_complete(atchan, bad_desc);
419}
420
421
422/*-- IRQ & Tasklet ---------------------------------------------------*/
423
424static void atc_tasklet(unsigned long data)
425{
426 struct at_dma_chan *atchan = (struct at_dma_chan *)data;
427
428 /* Channel cannot be enabled here */
429 if (atc_chan_is_enabled(atchan)) {
430 dev_err(chan2dev(&atchan->chan_common),
431 "BUG: channel enabled in tasklet\n");
432 return;
433 }
434
435 spin_lock(&atchan->lock);
436 if (test_and_clear_bit(0, &atchan->error_status))
437 atc_handle_error(atchan);
438 else
439 atc_advance_work(atchan);
440
441 spin_unlock(&atchan->lock);
442}
443
444static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
445{
446 struct at_dma *atdma = (struct at_dma *)dev_id;
447 struct at_dma_chan *atchan;
448 int i;
449 u32 status, pending, imr;
450 int ret = IRQ_NONE;
451
452 do {
453 imr = dma_readl(atdma, EBCIMR);
454 status = dma_readl(atdma, EBCISR);
455 pending = status & imr;
456
457 if (!pending)
458 break;
459
460 dev_vdbg(atdma->dma_common.dev,
461 "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
462 status, imr, pending);
463
464 for (i = 0; i < atdma->dma_common.chancnt; i++) {
465 atchan = &atdma->chan[i];
466 if (pending & (AT_DMA_CBTC(i) | AT_DMA_ERR(i))) {
467 if (pending & AT_DMA_ERR(i)) {
468 /* Disable channel on AHB error */
469 dma_writel(atdma, CHDR, atchan->mask);
470 /* Give information to tasklet */
471 set_bit(0, &atchan->error_status);
472 }
473 tasklet_schedule(&atchan->tasklet);
474 ret = IRQ_HANDLED;
475 }
476 }
477
478 } while (pending);
479
480 return ret;
481}
482
483
484/*-- DMA Engine API --------------------------------------------------*/
485
486/**
487 * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
488 * @desc: descriptor at the head of the transaction chain
489 *
490 * Queue chain if DMA engine is working already
491 *
492 * Cookie increment and adding to active_list or queue must be atomic
493 */
494static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
495{
496 struct at_desc *desc = txd_to_at_desc(tx);
497 struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
498 dma_cookie_t cookie;
499
500 spin_lock_bh(&atchan->lock);
501 cookie = atc_assign_cookie(atchan, desc);
502
503 if (list_empty(&atchan->active_list)) {
504 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
505 desc->txd.cookie);
506 atc_dostart(atchan, desc);
507 list_add_tail(&desc->desc_node, &atchan->active_list);
508 } else {
509 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
510 desc->txd.cookie);
511 list_add_tail(&desc->desc_node, &atchan->queue);
512 }
513
514 spin_unlock_bh(&atchan->lock);
515
516 return cookie;
517}
518
519/**
520 * atc_prep_dma_memcpy - prepare a memcpy operation
521 * @chan: the channel to prepare operation on
522 * @dest: operation virtual destination address
523 * @src: operation virtual source address
524 * @len: operation length
525 * @flags: tx descriptor status flags
526 */
527static struct dma_async_tx_descriptor *
528atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
529 size_t len, unsigned long flags)
530{
531 struct at_dma_chan *atchan = to_at_dma_chan(chan);
532 struct at_desc *desc = NULL;
533 struct at_desc *first = NULL;
534 struct at_desc *prev = NULL;
535 size_t xfer_count;
536 size_t offset;
537 unsigned int src_width;
538 unsigned int dst_width;
539 u32 ctrla;
540 u32 ctrlb;
541
542 dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
543 dest, src, len, flags);
544
545 if (unlikely(!len)) {
546 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
547 return NULL;
548 }
549
550 ctrla = ATC_DEFAULT_CTRLA;
551 ctrlb = ATC_DEFAULT_CTRLB
552 | ATC_SRC_ADDR_MODE_INCR
553 | ATC_DST_ADDR_MODE_INCR
554 | ATC_FC_MEM2MEM;
555
556 /*
557 * We can be a lot more clever here, but this should take care
558 * of the most common optimization.
559 */
560 if (!((src | dest | len) & 3)) {
561 ctrla |= ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
562 src_width = dst_width = 2;
563 } else if (!((src | dest | len) & 1)) {
564 ctrla |= ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
565 src_width = dst_width = 1;
566 } else {
567 ctrla |= ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
568 src_width = dst_width = 0;
569 }
570
571 for (offset = 0; offset < len; offset += xfer_count << src_width) {
572 xfer_count = min_t(size_t, (len - offset) >> src_width,
573 ATC_BTSIZE_MAX);
574
575 desc = atc_desc_get(atchan);
576 if (!desc)
577 goto err_desc_get;
578
579 desc->lli.saddr = src + offset;
580 desc->lli.daddr = dest + offset;
581 desc->lli.ctrla = ctrla | xfer_count;
582 desc->lli.ctrlb = ctrlb;
583
584 desc->txd.cookie = 0;
585 async_tx_ack(&desc->txd);
586
587 if (!first) {
588 first = desc;
589 } else {
590 /* inform the HW lli about chaining */
591 prev->lli.dscr = desc->txd.phys;
592 /* insert the link descriptor to the LD ring */
593 list_add_tail(&desc->desc_node,
285a3c71 594 &first->tx_list);
dc78baa2
NF
595 }
596 prev = desc;
597 }
598
599 /* First descriptor of the chain embedds additional information */
600 first->txd.cookie = -EBUSY;
601 first->len = len;
602
603 /* set end-of-link to the last link descriptor of list*/
604 set_desc_eol(desc);
605
606 desc->txd.flags = flags; /* client is in control of this ack */
607
608 return &first->txd;
609
610err_desc_get:
611 atc_desc_put(atchan, first);
612 return NULL;
613}
614
808347f6
NF
615
616/**
617 * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
618 * @chan: DMA channel
619 * @sgl: scatterlist to transfer to/from
620 * @sg_len: number of entries in @scatterlist
621 * @direction: DMA direction
622 * @flags: tx descriptor status flags
623 */
624static struct dma_async_tx_descriptor *
625atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
626 unsigned int sg_len, enum dma_data_direction direction,
627 unsigned long flags)
628{
629 struct at_dma_chan *atchan = to_at_dma_chan(chan);
630 struct at_dma_slave *atslave = chan->private;
631 struct at_desc *first = NULL;
632 struct at_desc *prev = NULL;
633 u32 ctrla;
634 u32 ctrlb;
635 dma_addr_t reg;
636 unsigned int reg_width;
637 unsigned int mem_width;
638 unsigned int i;
639 struct scatterlist *sg;
640 size_t total_len = 0;
641
642 dev_vdbg(chan2dev(chan), "prep_slave_sg: %s f0x%lx\n",
643 direction == DMA_TO_DEVICE ? "TO DEVICE" : "FROM DEVICE",
644 flags);
645
646 if (unlikely(!atslave || !sg_len)) {
647 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
648 return NULL;
649 }
650
651 reg_width = atslave->reg_width;
652
808347f6
NF
653 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla;
654 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN;
655
656 switch (direction) {
657 case DMA_TO_DEVICE:
658 ctrla |= ATC_DST_WIDTH(reg_width);
659 ctrlb |= ATC_DST_ADDR_MODE_FIXED
660 | ATC_SRC_ADDR_MODE_INCR
661 | ATC_FC_MEM2PER;
662 reg = atslave->tx_reg;
663 for_each_sg(sgl, sg, sg_len, i) {
664 struct at_desc *desc;
665 u32 len;
666 u32 mem;
667
668 desc = atc_desc_get(atchan);
669 if (!desc)
670 goto err_desc_get;
671
672 mem = sg_phys(sg);
673 len = sg_dma_len(sg);
674 mem_width = 2;
675 if (unlikely(mem & 3 || len & 3))
676 mem_width = 0;
677
678 desc->lli.saddr = mem;
679 desc->lli.daddr = reg;
680 desc->lli.ctrla = ctrla
681 | ATC_SRC_WIDTH(mem_width)
682 | len >> mem_width;
683 desc->lli.ctrlb = ctrlb;
684
685 if (!first) {
686 first = desc;
687 } else {
688 /* inform the HW lli about chaining */
689 prev->lli.dscr = desc->txd.phys;
690 /* insert the link descriptor to the LD ring */
691 list_add_tail(&desc->desc_node,
285a3c71 692 &first->tx_list);
808347f6
NF
693 }
694 prev = desc;
695 total_len += len;
696 }
697 break;
698 case DMA_FROM_DEVICE:
699 ctrla |= ATC_SRC_WIDTH(reg_width);
700 ctrlb |= ATC_DST_ADDR_MODE_INCR
701 | ATC_SRC_ADDR_MODE_FIXED
702 | ATC_FC_PER2MEM;
703
704 reg = atslave->rx_reg;
705 for_each_sg(sgl, sg, sg_len, i) {
706 struct at_desc *desc;
707 u32 len;
708 u32 mem;
709
710 desc = atc_desc_get(atchan);
711 if (!desc)
712 goto err_desc_get;
713
714 mem = sg_phys(sg);
715 len = sg_dma_len(sg);
716 mem_width = 2;
717 if (unlikely(mem & 3 || len & 3))
718 mem_width = 0;
719
720 desc->lli.saddr = reg;
721 desc->lli.daddr = mem;
722 desc->lli.ctrla = ctrla
723 | ATC_DST_WIDTH(mem_width)
724 | len >> mem_width;
725 desc->lli.ctrlb = ctrlb;
726
727 if (!first) {
728 first = desc;
729 } else {
730 /* inform the HW lli about chaining */
731 prev->lli.dscr = desc->txd.phys;
732 /* insert the link descriptor to the LD ring */
733 list_add_tail(&desc->desc_node,
285a3c71 734 &first->tx_list);
808347f6
NF
735 }
736 prev = desc;
737 total_len += len;
738 }
739 break;
740 default:
741 return NULL;
742 }
743
744 /* set end-of-link to the last link descriptor of list*/
745 set_desc_eol(prev);
746
747 /* First descriptor of the chain embedds additional information */
748 first->txd.cookie = -EBUSY;
749 first->len = total_len;
750
751 /* last link descriptor of list is responsible of flags */
752 prev->txd.flags = flags; /* client is in control of this ack */
753
754 return &first->txd;
755
756err_desc_get:
757 dev_err(chan2dev(chan), "not enough descriptors available\n");
758 atc_desc_put(atchan, first);
759 return NULL;
760}
761
c3635c78 762static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd)
808347f6
NF
763{
764 struct at_dma_chan *atchan = to_at_dma_chan(chan);
765 struct at_dma *atdma = to_at_dma(chan->device);
766 struct at_desc *desc, *_desc;
767 LIST_HEAD(list);
768
c3635c78
LW
769 /* Only supports DMA_TERMINATE_ALL */
770 if (cmd != DMA_TERMINATE_ALL)
771 return -ENXIO;
772
808347f6
NF
773 /*
774 * This is only called when something went wrong elsewhere, so
775 * we don't really care about the data. Just disable the
776 * channel. We still have to poll the channel enable bit due
777 * to AHB/HSB limitations.
778 */
779 spin_lock_bh(&atchan->lock);
780
781 dma_writel(atdma, CHDR, atchan->mask);
782
783 /* confirm that this channel is disabled */
784 while (dma_readl(atdma, CHSR) & atchan->mask)
785 cpu_relax();
786
787 /* active_list entries will end up before queued entries */
788 list_splice_init(&atchan->queue, &list);
789 list_splice_init(&atchan->active_list, &list);
790
791 spin_unlock_bh(&atchan->lock);
792
793 /* Flush all pending and queued descriptors */
794 list_for_each_entry_safe(desc, _desc, &list, desc_node)
795 atc_chain_complete(atchan, desc);
c3635c78
LW
796
797 return 0;
808347f6
NF
798}
799
dc78baa2 800/**
07934481 801 * atc_tx_status - poll for transaction completion
dc78baa2
NF
802 * @chan: DMA channel
803 * @cookie: transaction identifier to check status of
07934481 804 * @txstate: if not %NULL updated with transaction state
dc78baa2 805 *
07934481 806 * If @txstate is passed in, upon return it reflect the driver
dc78baa2
NF
807 * internal state and can be used with dma_async_is_complete() to check
808 * the status of multiple cookies without re-checking hardware state.
809 */
810static enum dma_status
07934481 811atc_tx_status(struct dma_chan *chan,
dc78baa2 812 dma_cookie_t cookie,
07934481 813 struct dma_tx_state *txstate)
dc78baa2
NF
814{
815 struct at_dma_chan *atchan = to_at_dma_chan(chan);
816 dma_cookie_t last_used;
817 dma_cookie_t last_complete;
818 enum dma_status ret;
819
4297a462 820 spin_lock_bh(&atchan->lock);
dc78baa2
NF
821
822 last_complete = atchan->completed_cookie;
823 last_used = chan->cookie;
824
825 ret = dma_async_is_complete(cookie, last_complete, last_used);
826 if (ret != DMA_SUCCESS) {
827 atc_cleanup_descriptors(atchan);
828
829 last_complete = atchan->completed_cookie;
830 last_used = chan->cookie;
831
832 ret = dma_async_is_complete(cookie, last_complete, last_used);
833 }
834
4297a462 835 spin_unlock_bh(&atchan->lock);
dc78baa2 836
07934481
LW
837 if (txstate) {
838 txstate->last = last_complete;
839 txstate->used = last_used;
840 txstate->residue = 0;
841 }
842
843 dev_vdbg(chan2dev(chan), "tx_status: %d (d%d, u%d)\n",
844 cookie, last_complete ? last_complete : 0,
845 last_used ? last_used : 0);
dc78baa2
NF
846
847 return ret;
848}
849
850/**
851 * atc_issue_pending - try to finish work
852 * @chan: target DMA channel
853 */
854static void atc_issue_pending(struct dma_chan *chan)
855{
856 struct at_dma_chan *atchan = to_at_dma_chan(chan);
857
858 dev_vdbg(chan2dev(chan), "issue_pending\n");
859
860 if (!atc_chan_is_enabled(atchan)) {
861 spin_lock_bh(&atchan->lock);
862 atc_advance_work(atchan);
863 spin_unlock_bh(&atchan->lock);
864 }
865}
866
867/**
868 * atc_alloc_chan_resources - allocate resources for DMA channel
869 * @chan: allocate descriptor resources for this channel
870 * @client: current client requesting the channel be ready for requests
871 *
872 * return - the number of allocated descriptors
873 */
874static int atc_alloc_chan_resources(struct dma_chan *chan)
875{
876 struct at_dma_chan *atchan = to_at_dma_chan(chan);
877 struct at_dma *atdma = to_at_dma(chan->device);
878 struct at_desc *desc;
808347f6 879 struct at_dma_slave *atslave;
dc78baa2 880 int i;
808347f6 881 u32 cfg;
dc78baa2
NF
882 LIST_HEAD(tmp_list);
883
884 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
885
886 /* ASSERT: channel is idle */
887 if (atc_chan_is_enabled(atchan)) {
888 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
889 return -EIO;
890 }
891
808347f6
NF
892 cfg = ATC_DEFAULT_CFG;
893
894 atslave = chan->private;
895 if (atslave) {
896 /*
897 * We need controller-specific data to set up slave
898 * transfers.
899 */
900 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
901
902 /* if cfg configuration specified take it instad of default */
903 if (atslave->cfg)
904 cfg = atslave->cfg;
905 }
906
907 /* have we already been set up?
908 * reconfigure channel but no need to reallocate descriptors */
dc78baa2
NF
909 if (!list_empty(&atchan->free_list))
910 return atchan->descs_allocated;
911
912 /* Allocate initial pool of descriptors */
913 for (i = 0; i < init_nr_desc_per_channel; i++) {
914 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
915 if (!desc) {
916 dev_err(atdma->dma_common.dev,
917 "Only %d initial descriptors\n", i);
918 break;
919 }
920 list_add_tail(&desc->desc_node, &tmp_list);
921 }
922
923 spin_lock_bh(&atchan->lock);
924 atchan->descs_allocated = i;
925 list_splice(&tmp_list, &atchan->free_list);
926 atchan->completed_cookie = chan->cookie = 1;
927 spin_unlock_bh(&atchan->lock);
928
929 /* channel parameters */
808347f6 930 channel_writel(atchan, CFG, cfg);
dc78baa2
NF
931
932 dev_dbg(chan2dev(chan),
933 "alloc_chan_resources: allocated %d descriptors\n",
934 atchan->descs_allocated);
935
936 return atchan->descs_allocated;
937}
938
939/**
940 * atc_free_chan_resources - free all channel resources
941 * @chan: DMA channel
942 */
943static void atc_free_chan_resources(struct dma_chan *chan)
944{
945 struct at_dma_chan *atchan = to_at_dma_chan(chan);
946 struct at_dma *atdma = to_at_dma(chan->device);
947 struct at_desc *desc, *_desc;
948 LIST_HEAD(list);
949
950 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
951 atchan->descs_allocated);
952
953 /* ASSERT: channel is idle */
954 BUG_ON(!list_empty(&atchan->active_list));
955 BUG_ON(!list_empty(&atchan->queue));
956 BUG_ON(atc_chan_is_enabled(atchan));
957
958 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
959 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
960 list_del(&desc->desc_node);
961 /* free link descriptor */
962 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
963 }
964 list_splice_init(&atchan->free_list, &list);
965 atchan->descs_allocated = 0;
966
967 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
968}
969
970
971/*-- Module Management -----------------------------------------------*/
972
973/**
974 * at_dma_off - disable DMA controller
975 * @atdma: the Atmel HDAMC device
976 */
977static void at_dma_off(struct at_dma *atdma)
978{
979 dma_writel(atdma, EN, 0);
980
981 /* disable all interrupts */
982 dma_writel(atdma, EBCIDR, -1L);
983
984 /* confirm that all channels are disabled */
985 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
986 cpu_relax();
987}
988
989static int __init at_dma_probe(struct platform_device *pdev)
990{
991 struct at_dma_platform_data *pdata;
992 struct resource *io;
993 struct at_dma *atdma;
994 size_t size;
995 int irq;
996 int err;
997 int i;
998
999 /* get DMA Controller parameters from platform */
1000 pdata = pdev->dev.platform_data;
1001 if (!pdata || pdata->nr_channels > AT_DMA_MAX_NR_CHANNELS)
1002 return -EINVAL;
1003
1004 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1005 if (!io)
1006 return -EINVAL;
1007
1008 irq = platform_get_irq(pdev, 0);
1009 if (irq < 0)
1010 return irq;
1011
1012 size = sizeof(struct at_dma);
1013 size += pdata->nr_channels * sizeof(struct at_dma_chan);
1014 atdma = kzalloc(size, GFP_KERNEL);
1015 if (!atdma)
1016 return -ENOMEM;
1017
1018 /* discover transaction capabilites from the platform data */
1019 atdma->dma_common.cap_mask = pdata->cap_mask;
1020 atdma->all_chan_mask = (1 << pdata->nr_channels) - 1;
1021
1022 size = io->end - io->start + 1;
1023 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1024 err = -EBUSY;
1025 goto err_kfree;
1026 }
1027
1028 atdma->regs = ioremap(io->start, size);
1029 if (!atdma->regs) {
1030 err = -ENOMEM;
1031 goto err_release_r;
1032 }
1033
1034 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1035 if (IS_ERR(atdma->clk)) {
1036 err = PTR_ERR(atdma->clk);
1037 goto err_clk;
1038 }
1039 clk_enable(atdma->clk);
1040
1041 /* force dma off, just in case */
1042 at_dma_off(atdma);
1043
1044 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1045 if (err)
1046 goto err_irq;
1047
1048 platform_set_drvdata(pdev, atdma);
1049
1050 /* create a pool of consistent memory blocks for hardware descriptors */
1051 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1052 &pdev->dev, sizeof(struct at_desc),
1053 4 /* word alignment */, 0);
1054 if (!atdma->dma_desc_pool) {
1055 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1056 err = -ENOMEM;
1057 goto err_pool_create;
1058 }
1059
1060 /* clear any pending interrupt */
1061 while (dma_readl(atdma, EBCISR))
1062 cpu_relax();
1063
1064 /* initialize channels related values */
1065 INIT_LIST_HEAD(&atdma->dma_common.channels);
1066 for (i = 0; i < pdata->nr_channels; i++, atdma->dma_common.chancnt++) {
1067 struct at_dma_chan *atchan = &atdma->chan[i];
1068
1069 atchan->chan_common.device = &atdma->dma_common;
1070 atchan->chan_common.cookie = atchan->completed_cookie = 1;
1071 atchan->chan_common.chan_id = i;
1072 list_add_tail(&atchan->chan_common.device_node,
1073 &atdma->dma_common.channels);
1074
1075 atchan->ch_regs = atdma->regs + ch_regs(i);
1076 spin_lock_init(&atchan->lock);
1077 atchan->mask = 1 << i;
1078
1079 INIT_LIST_HEAD(&atchan->active_list);
1080 INIT_LIST_HEAD(&atchan->queue);
1081 INIT_LIST_HEAD(&atchan->free_list);
1082
1083 tasklet_init(&atchan->tasklet, atc_tasklet,
1084 (unsigned long)atchan);
1085 atc_enable_irq(atchan);
1086 }
1087
1088 /* set base routines */
1089 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1090 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
07934481 1091 atdma->dma_common.device_tx_status = atc_tx_status;
dc78baa2
NF
1092 atdma->dma_common.device_issue_pending = atc_issue_pending;
1093 atdma->dma_common.dev = &pdev->dev;
1094
1095 /* set prep routines based on capability */
1096 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1097 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1098
808347f6
NF
1099 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
1100 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
c3635c78 1101 atdma->dma_common.device_control = atc_control;
808347f6
NF
1102 }
1103
dc78baa2
NF
1104 dma_writel(atdma, EN, AT_DMA_ENABLE);
1105
1106 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
1107 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1108 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
1109 atdma->dma_common.chancnt);
1110
1111 dma_async_device_register(&atdma->dma_common);
1112
1113 return 0;
1114
1115err_pool_create:
1116 platform_set_drvdata(pdev, NULL);
1117 free_irq(platform_get_irq(pdev, 0), atdma);
1118err_irq:
1119 clk_disable(atdma->clk);
1120 clk_put(atdma->clk);
1121err_clk:
1122 iounmap(atdma->regs);
1123 atdma->regs = NULL;
1124err_release_r:
1125 release_mem_region(io->start, size);
1126err_kfree:
1127 kfree(atdma);
1128 return err;
1129}
1130
1131static int __exit at_dma_remove(struct platform_device *pdev)
1132{
1133 struct at_dma *atdma = platform_get_drvdata(pdev);
1134 struct dma_chan *chan, *_chan;
1135 struct resource *io;
1136
1137 at_dma_off(atdma);
1138 dma_async_device_unregister(&atdma->dma_common);
1139
1140 dma_pool_destroy(atdma->dma_desc_pool);
1141 platform_set_drvdata(pdev, NULL);
1142 free_irq(platform_get_irq(pdev, 0), atdma);
1143
1144 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1145 device_node) {
1146 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1147
1148 /* Disable interrupts */
1149 atc_disable_irq(atchan);
1150 tasklet_disable(&atchan->tasklet);
1151
1152 tasklet_kill(&atchan->tasklet);
1153 list_del(&chan->device_node);
1154 }
1155
1156 clk_disable(atdma->clk);
1157 clk_put(atdma->clk);
1158
1159 iounmap(atdma->regs);
1160 atdma->regs = NULL;
1161
1162 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1163 release_mem_region(io->start, io->end - io->start + 1);
1164
1165 kfree(atdma);
1166
1167 return 0;
1168}
1169
1170static void at_dma_shutdown(struct platform_device *pdev)
1171{
1172 struct at_dma *atdma = platform_get_drvdata(pdev);
1173
1174 at_dma_off(platform_get_drvdata(pdev));
1175 clk_disable(atdma->clk);
1176}
1177
33f82d14 1178static int at_dma_suspend_noirq(struct device *dev)
dc78baa2 1179{
33f82d14
DW
1180 struct platform_device *pdev = to_platform_device(dev);
1181 struct at_dma *atdma = platform_get_drvdata(pdev);
dc78baa2
NF
1182
1183 at_dma_off(platform_get_drvdata(pdev));
1184 clk_disable(atdma->clk);
1185 return 0;
1186}
1187
33f82d14 1188static int at_dma_resume_noirq(struct device *dev)
dc78baa2 1189{
33f82d14
DW
1190 struct platform_device *pdev = to_platform_device(dev);
1191 struct at_dma *atdma = platform_get_drvdata(pdev);
dc78baa2
NF
1192
1193 clk_enable(atdma->clk);
1194 dma_writel(atdma, EN, AT_DMA_ENABLE);
1195 return 0;
dc78baa2
NF
1196}
1197
47145210 1198static const struct dev_pm_ops at_dma_dev_pm_ops = {
33f82d14
DW
1199 .suspend_noirq = at_dma_suspend_noirq,
1200 .resume_noirq = at_dma_resume_noirq,
1201};
1202
dc78baa2
NF
1203static struct platform_driver at_dma_driver = {
1204 .remove = __exit_p(at_dma_remove),
1205 .shutdown = at_dma_shutdown,
dc78baa2
NF
1206 .driver = {
1207 .name = "at_hdmac",
33f82d14 1208 .pm = &at_dma_dev_pm_ops,
dc78baa2
NF
1209 },
1210};
1211
1212static int __init at_dma_init(void)
1213{
1214 return platform_driver_probe(&at_dma_driver, at_dma_probe);
1215}
1216module_init(at_dma_init);
1217
1218static void __exit at_dma_exit(void)
1219{
1220 platform_driver_unregister(&at_dma_driver);
1221}
1222module_exit(at_dma_exit);
1223
1224MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1225MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1226MODULE_LICENSE("GPL");
1227MODULE_ALIAS("platform:at_hdmac");