blob: da4e8b710a9b1e75f7e647a4b767c40d71d32193 [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>
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));
Dan Williams285a3c72009-09-08 17:53:03 -070090 INIT_LIST_HEAD(&desc->tx_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +020091 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/**
102 * atc_desc_get - get a unsused descriptor from free_list
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);
Dan Williams285a3c72009-09-08 17:53:03 -0700154 list_for_each_entry(child, &desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200155 dev_vdbg(chan2dev(&atchan->chan_common),
156 "moving child desc %p to freelist\n",
157 child);
Dan Williams285a3c72009-09-08 17:53:03 -0700158 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200159 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 */
Dan Williams285a3c72009-09-08 17:53:03 -0700251 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200252 /* move myself to free_list */
253 list_move(&desc->desc_node, &atchan->free_list);
254
255 /* unmap dma addresses */
256 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
257 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
258 dma_unmap_single(chan2parent(&atchan->chan_common),
259 desc->lli.daddr,
260 desc->len, DMA_FROM_DEVICE);
261 else
262 dma_unmap_page(chan2parent(&atchan->chan_common),
263 desc->lli.daddr,
264 desc->len, DMA_FROM_DEVICE);
265 }
266 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
267 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
268 dma_unmap_single(chan2parent(&atchan->chan_common),
269 desc->lli.saddr,
270 desc->len, DMA_TO_DEVICE);
271 else
272 dma_unmap_page(chan2parent(&atchan->chan_common),
273 desc->lli.saddr,
274 desc->len, DMA_TO_DEVICE);
275 }
276
277 /*
278 * The API requires that no submissions are done from a
279 * callback, so we don't need to drop the lock here
280 */
281 if (callback)
282 callback(param);
283
284 dma_run_dependencies(txd);
285}
286
287/**
288 * atc_complete_all - finish work for all transactions
289 * @atchan: channel to complete transactions for
290 *
291 * Eventually submit queued descriptors if any
292 *
293 * Assume channel is idle while calling this function
294 * Called with atchan->lock held and bh disabled
295 */
296static void atc_complete_all(struct at_dma_chan *atchan)
297{
298 struct at_desc *desc, *_desc;
299 LIST_HEAD(list);
300
301 dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
302
303 BUG_ON(atc_chan_is_enabled(atchan));
304
305 /*
306 * Submit queued descriptors ASAP, i.e. before we go through
307 * the completed ones.
308 */
309 if (!list_empty(&atchan->queue))
310 atc_dostart(atchan, atc_first_queued(atchan));
311 /* empty active_list now it is completed */
312 list_splice_init(&atchan->active_list, &list);
313 /* empty queue list by moving descriptors (if any) to active_list */
314 list_splice_init(&atchan->queue, &atchan->active_list);
315
316 list_for_each_entry_safe(desc, _desc, &list, desc_node)
317 atc_chain_complete(atchan, desc);
318}
319
320/**
321 * atc_cleanup_descriptors - cleanup up finished descriptors in active_list
322 * @atchan: channel to be cleaned up
323 *
324 * Called with atchan->lock held and bh disabled
325 */
326static void atc_cleanup_descriptors(struct at_dma_chan *atchan)
327{
328 struct at_desc *desc, *_desc;
329 struct at_desc *child;
330
331 dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n");
332
333 list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
334 if (!(desc->lli.ctrla & ATC_DONE))
335 /* This one is currently in progress */
336 return;
337
Dan Williams285a3c72009-09-08 17:53:03 -0700338 list_for_each_entry(child, &desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200339 if (!(child->lli.ctrla & ATC_DONE))
340 /* Currently in progress */
341 return;
342
343 /*
344 * No descriptors so far seem to be in progress, i.e.
345 * this chain must be done.
346 */
347 atc_chain_complete(atchan, desc);
348 }
349}
350
351/**
352 * atc_advance_work - at the end of a transaction, move forward
353 * @atchan: channel where the transaction ended
354 *
355 * Called with atchan->lock held and bh disabled
356 */
357static void atc_advance_work(struct at_dma_chan *atchan)
358{
359 dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
360
361 if (list_empty(&atchan->active_list) ||
362 list_is_singular(&atchan->active_list)) {
363 atc_complete_all(atchan);
364 } else {
365 atc_chain_complete(atchan, atc_first_active(atchan));
366 /* advance work */
367 atc_dostart(atchan, atc_first_active(atchan));
368 }
369}
370
371
372/**
373 * atc_handle_error - handle errors reported by DMA controller
374 * @atchan: channel where error occurs
375 *
376 * Called with atchan->lock held and bh disabled
377 */
378static void atc_handle_error(struct at_dma_chan *atchan)
379{
380 struct at_desc *bad_desc;
381 struct at_desc *child;
382
383 /*
384 * The descriptor currently at the head of the active list is
385 * broked. Since we don't have any way to report errors, we'll
386 * just have to scream loudly and try to carry on.
387 */
388 bad_desc = atc_first_active(atchan);
389 list_del_init(&bad_desc->desc_node);
390
391 /* As we are stopped, take advantage to push queued descriptors
392 * in active_list */
393 list_splice_init(&atchan->queue, atchan->active_list.prev);
394
395 /* Try to restart the controller */
396 if (!list_empty(&atchan->active_list))
397 atc_dostart(atchan, atc_first_active(atchan));
398
399 /*
400 * KERN_CRITICAL may seem harsh, but since this only happens
401 * when someone submits a bad physical address in a
402 * descriptor, we should consider ourselves lucky that the
403 * controller flagged an error instead of scribbling over
404 * random memory locations.
405 */
406 dev_crit(chan2dev(&atchan->chan_common),
407 "Bad descriptor submitted for DMA!\n");
408 dev_crit(chan2dev(&atchan->chan_common),
409 " cookie: %d\n", bad_desc->txd.cookie);
410 atc_dump_lli(atchan, &bad_desc->lli);
Dan Williams285a3c72009-09-08 17:53:03 -0700411 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200412 atc_dump_lli(atchan, &child->lli);
413
414 /* Pretend the descriptor completed successfully */
415 atc_chain_complete(atchan, bad_desc);
416}
417
418
419/*-- IRQ & Tasklet ---------------------------------------------------*/
420
421static void atc_tasklet(unsigned long data)
422{
423 struct at_dma_chan *atchan = (struct at_dma_chan *)data;
424
425 /* Channel cannot be enabled here */
426 if (atc_chan_is_enabled(atchan)) {
427 dev_err(chan2dev(&atchan->chan_common),
428 "BUG: channel enabled in tasklet\n");
429 return;
430 }
431
432 spin_lock(&atchan->lock);
433 if (test_and_clear_bit(0, &atchan->error_status))
434 atc_handle_error(atchan);
435 else
436 atc_advance_work(atchan);
437
438 spin_unlock(&atchan->lock);
439}
440
441static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
442{
443 struct at_dma *atdma = (struct at_dma *)dev_id;
444 struct at_dma_chan *atchan;
445 int i;
446 u32 status, pending, imr;
447 int ret = IRQ_NONE;
448
449 do {
450 imr = dma_readl(atdma, EBCIMR);
451 status = dma_readl(atdma, EBCISR);
452 pending = status & imr;
453
454 if (!pending)
455 break;
456
457 dev_vdbg(atdma->dma_common.dev,
458 "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
459 status, imr, pending);
460
461 for (i = 0; i < atdma->dma_common.chancnt; i++) {
462 atchan = &atdma->chan[i];
463 if (pending & (AT_DMA_CBTC(i) | AT_DMA_ERR(i))) {
464 if (pending & AT_DMA_ERR(i)) {
465 /* Disable channel on AHB error */
466 dma_writel(atdma, CHDR, atchan->mask);
467 /* Give information to tasklet */
468 set_bit(0, &atchan->error_status);
469 }
470 tasklet_schedule(&atchan->tasklet);
471 ret = IRQ_HANDLED;
472 }
473 }
474
475 } while (pending);
476
477 return ret;
478}
479
480
481/*-- DMA Engine API --------------------------------------------------*/
482
483/**
484 * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
485 * @desc: descriptor at the head of the transaction chain
486 *
487 * Queue chain if DMA engine is working already
488 *
489 * Cookie increment and adding to active_list or queue must be atomic
490 */
491static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
492{
493 struct at_desc *desc = txd_to_at_desc(tx);
494 struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
495 dma_cookie_t cookie;
496
497 spin_lock_bh(&atchan->lock);
498 cookie = atc_assign_cookie(atchan, desc);
499
500 if (list_empty(&atchan->active_list)) {
501 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
502 desc->txd.cookie);
503 atc_dostart(atchan, desc);
504 list_add_tail(&desc->desc_node, &atchan->active_list);
505 } else {
506 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
507 desc->txd.cookie);
508 list_add_tail(&desc->desc_node, &atchan->queue);
509 }
510
511 spin_unlock_bh(&atchan->lock);
512
513 return cookie;
514}
515
516/**
517 * atc_prep_dma_memcpy - prepare a memcpy operation
518 * @chan: the channel to prepare operation on
519 * @dest: operation virtual destination address
520 * @src: operation virtual source address
521 * @len: operation length
522 * @flags: tx descriptor status flags
523 */
524static struct dma_async_tx_descriptor *
525atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
526 size_t len, unsigned long flags)
527{
528 struct at_dma_chan *atchan = to_at_dma_chan(chan);
529 struct at_desc *desc = NULL;
530 struct at_desc *first = NULL;
531 struct at_desc *prev = NULL;
532 size_t xfer_count;
533 size_t offset;
534 unsigned int src_width;
535 unsigned int dst_width;
536 u32 ctrla;
537 u32 ctrlb;
538
539 dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
540 dest, src, len, flags);
541
542 if (unlikely(!len)) {
543 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
544 return NULL;
545 }
546
547 ctrla = ATC_DEFAULT_CTRLA;
548 ctrlb = ATC_DEFAULT_CTRLB
549 | ATC_SRC_ADDR_MODE_INCR
550 | ATC_DST_ADDR_MODE_INCR
551 | ATC_FC_MEM2MEM;
552
553 /*
554 * We can be a lot more clever here, but this should take care
555 * of the most common optimization.
556 */
557 if (!((src | dest | len) & 3)) {
558 ctrla |= ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
559 src_width = dst_width = 2;
560 } else if (!((src | dest | len) & 1)) {
561 ctrla |= ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
562 src_width = dst_width = 1;
563 } else {
564 ctrla |= ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
565 src_width = dst_width = 0;
566 }
567
568 for (offset = 0; offset < len; offset += xfer_count << src_width) {
569 xfer_count = min_t(size_t, (len - offset) >> src_width,
570 ATC_BTSIZE_MAX);
571
572 desc = atc_desc_get(atchan);
573 if (!desc)
574 goto err_desc_get;
575
576 desc->lli.saddr = src + offset;
577 desc->lli.daddr = dest + offset;
578 desc->lli.ctrla = ctrla | xfer_count;
579 desc->lli.ctrlb = ctrlb;
580
581 desc->txd.cookie = 0;
582 async_tx_ack(&desc->txd);
583
584 if (!first) {
585 first = desc;
586 } else {
587 /* inform the HW lli about chaining */
588 prev->lli.dscr = desc->txd.phys;
589 /* insert the link descriptor to the LD ring */
590 list_add_tail(&desc->desc_node,
Dan Williams285a3c72009-09-08 17:53:03 -0700591 &first->tx_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200592 }
593 prev = desc;
594 }
595
596 /* First descriptor of the chain embedds additional information */
597 first->txd.cookie = -EBUSY;
598 first->len = len;
599
600 /* set end-of-link to the last link descriptor of list*/
601 set_desc_eol(desc);
602
603 desc->txd.flags = flags; /* client is in control of this ack */
604
605 return &first->txd;
606
607err_desc_get:
608 atc_desc_put(atchan, first);
609 return NULL;
610}
611
Nicolas Ferre808347f2009-07-22 20:04:45 +0200612
613/**
614 * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
615 * @chan: DMA channel
616 * @sgl: scatterlist to transfer to/from
617 * @sg_len: number of entries in @scatterlist
618 * @direction: DMA direction
619 * @flags: tx descriptor status flags
620 */
621static struct dma_async_tx_descriptor *
622atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
623 unsigned int sg_len, enum dma_data_direction direction,
624 unsigned long flags)
625{
626 struct at_dma_chan *atchan = to_at_dma_chan(chan);
627 struct at_dma_slave *atslave = chan->private;
628 struct at_desc *first = NULL;
629 struct at_desc *prev = NULL;
630 u32 ctrla;
631 u32 ctrlb;
632 dma_addr_t reg;
633 unsigned int reg_width;
634 unsigned int mem_width;
635 unsigned int i;
636 struct scatterlist *sg;
637 size_t total_len = 0;
638
639 dev_vdbg(chan2dev(chan), "prep_slave_sg: %s f0x%lx\n",
640 direction == DMA_TO_DEVICE ? "TO DEVICE" : "FROM DEVICE",
641 flags);
642
643 if (unlikely(!atslave || !sg_len)) {
644 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
645 return NULL;
646 }
647
648 reg_width = atslave->reg_width;
649
650 sg_len = dma_map_sg(chan2parent(chan), sgl, sg_len, direction);
651
652 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla;
653 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN;
654
655 switch (direction) {
656 case DMA_TO_DEVICE:
657 ctrla |= ATC_DST_WIDTH(reg_width);
658 ctrlb |= ATC_DST_ADDR_MODE_FIXED
659 | ATC_SRC_ADDR_MODE_INCR
660 | ATC_FC_MEM2PER;
661 reg = atslave->tx_reg;
662 for_each_sg(sgl, sg, sg_len, i) {
663 struct at_desc *desc;
664 u32 len;
665 u32 mem;
666
667 desc = atc_desc_get(atchan);
668 if (!desc)
669 goto err_desc_get;
670
671 mem = sg_phys(sg);
672 len = sg_dma_len(sg);
673 mem_width = 2;
674 if (unlikely(mem & 3 || len & 3))
675 mem_width = 0;
676
677 desc->lli.saddr = mem;
678 desc->lli.daddr = reg;
679 desc->lli.ctrla = ctrla
680 | ATC_SRC_WIDTH(mem_width)
681 | len >> mem_width;
682 desc->lli.ctrlb = ctrlb;
683
684 if (!first) {
685 first = desc;
686 } else {
687 /* inform the HW lli about chaining */
688 prev->lli.dscr = desc->txd.phys;
689 /* insert the link descriptor to the LD ring */
690 list_add_tail(&desc->desc_node,
Dan Williams285a3c72009-09-08 17:53:03 -0700691 &first->tx_list);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200692 }
693 prev = desc;
694 total_len += len;
695 }
696 break;
697 case DMA_FROM_DEVICE:
698 ctrla |= ATC_SRC_WIDTH(reg_width);
699 ctrlb |= ATC_DST_ADDR_MODE_INCR
700 | ATC_SRC_ADDR_MODE_FIXED
701 | ATC_FC_PER2MEM;
702
703 reg = atslave->rx_reg;
704 for_each_sg(sgl, sg, sg_len, i) {
705 struct at_desc *desc;
706 u32 len;
707 u32 mem;
708
709 desc = atc_desc_get(atchan);
710 if (!desc)
711 goto err_desc_get;
712
713 mem = sg_phys(sg);
714 len = sg_dma_len(sg);
715 mem_width = 2;
716 if (unlikely(mem & 3 || len & 3))
717 mem_width = 0;
718
719 desc->lli.saddr = reg;
720 desc->lli.daddr = mem;
721 desc->lli.ctrla = ctrla
722 | ATC_DST_WIDTH(mem_width)
723 | len >> mem_width;
724 desc->lli.ctrlb = ctrlb;
725
726 if (!first) {
727 first = desc;
728 } else {
729 /* inform the HW lli about chaining */
730 prev->lli.dscr = desc->txd.phys;
731 /* insert the link descriptor to the LD ring */
732 list_add_tail(&desc->desc_node,
Dan Williams285a3c72009-09-08 17:53:03 -0700733 &first->tx_list);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200734 }
735 prev = desc;
736 total_len += len;
737 }
738 break;
739 default:
740 return NULL;
741 }
742
743 /* set end-of-link to the last link descriptor of list*/
744 set_desc_eol(prev);
745
746 /* First descriptor of the chain embedds additional information */
747 first->txd.cookie = -EBUSY;
748 first->len = total_len;
749
750 /* last link descriptor of list is responsible of flags */
751 prev->txd.flags = flags; /* client is in control of this ack */
752
753 return &first->txd;
754
755err_desc_get:
756 dev_err(chan2dev(chan), "not enough descriptors available\n");
757 atc_desc_put(atchan, first);
758 return NULL;
759}
760
761static void atc_terminate_all(struct dma_chan *chan)
762{
763 struct at_dma_chan *atchan = to_at_dma_chan(chan);
764 struct at_dma *atdma = to_at_dma(chan->device);
765 struct at_desc *desc, *_desc;
766 LIST_HEAD(list);
767
768 /*
769 * This is only called when something went wrong elsewhere, so
770 * we don't really care about the data. Just disable the
771 * channel. We still have to poll the channel enable bit due
772 * to AHB/HSB limitations.
773 */
774 spin_lock_bh(&atchan->lock);
775
776 dma_writel(atdma, CHDR, atchan->mask);
777
778 /* confirm that this channel is disabled */
779 while (dma_readl(atdma, CHSR) & atchan->mask)
780 cpu_relax();
781
782 /* active_list entries will end up before queued entries */
783 list_splice_init(&atchan->queue, &list);
784 list_splice_init(&atchan->active_list, &list);
785
786 spin_unlock_bh(&atchan->lock);
787
788 /* Flush all pending and queued descriptors */
789 list_for_each_entry_safe(desc, _desc, &list, desc_node)
790 atc_chain_complete(atchan, desc);
791}
792
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200793/**
794 * atc_is_tx_complete - poll for transaction completion
795 * @chan: DMA channel
796 * @cookie: transaction identifier to check status of
797 * @done: if not %NULL, updated with last completed transaction
798 * @used: if not %NULL, updated with last used transaction
799 *
800 * If @done and @used are passed in, upon return they reflect the driver
801 * internal state and can be used with dma_async_is_complete() to check
802 * the status of multiple cookies without re-checking hardware state.
803 */
804static enum dma_status
805atc_is_tx_complete(struct dma_chan *chan,
806 dma_cookie_t cookie,
807 dma_cookie_t *done, dma_cookie_t *used)
808{
809 struct at_dma_chan *atchan = to_at_dma_chan(chan);
810 dma_cookie_t last_used;
811 dma_cookie_t last_complete;
812 enum dma_status ret;
813
814 dev_vdbg(chan2dev(chan), "is_tx_complete: %d (d%d, u%d)\n",
815 cookie, done ? *done : 0, used ? *used : 0);
816
817 spin_lock_bh(atchan->lock);
818
819 last_complete = atchan->completed_cookie;
820 last_used = chan->cookie;
821
822 ret = dma_async_is_complete(cookie, last_complete, last_used);
823 if (ret != DMA_SUCCESS) {
824 atc_cleanup_descriptors(atchan);
825
826 last_complete = atchan->completed_cookie;
827 last_used = chan->cookie;
828
829 ret = dma_async_is_complete(cookie, last_complete, last_used);
830 }
831
832 spin_unlock_bh(atchan->lock);
833
834 if (done)
835 *done = last_complete;
836 if (used)
837 *used = last_used;
838
839 return ret;
840}
841
842/**
843 * atc_issue_pending - try to finish work
844 * @chan: target DMA channel
845 */
846static void atc_issue_pending(struct dma_chan *chan)
847{
848 struct at_dma_chan *atchan = to_at_dma_chan(chan);
849
850 dev_vdbg(chan2dev(chan), "issue_pending\n");
851
852 if (!atc_chan_is_enabled(atchan)) {
853 spin_lock_bh(&atchan->lock);
854 atc_advance_work(atchan);
855 spin_unlock_bh(&atchan->lock);
856 }
857}
858
859/**
860 * atc_alloc_chan_resources - allocate resources for DMA channel
861 * @chan: allocate descriptor resources for this channel
862 * @client: current client requesting the channel be ready for requests
863 *
864 * return - the number of allocated descriptors
865 */
866static int atc_alloc_chan_resources(struct dma_chan *chan)
867{
868 struct at_dma_chan *atchan = to_at_dma_chan(chan);
869 struct at_dma *atdma = to_at_dma(chan->device);
870 struct at_desc *desc;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200871 struct at_dma_slave *atslave;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200872 int i;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200873 u32 cfg;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200874 LIST_HEAD(tmp_list);
875
876 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
877
878 /* ASSERT: channel is idle */
879 if (atc_chan_is_enabled(atchan)) {
880 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
881 return -EIO;
882 }
883
Nicolas Ferre808347f2009-07-22 20:04:45 +0200884 cfg = ATC_DEFAULT_CFG;
885
886 atslave = chan->private;
887 if (atslave) {
888 /*
889 * We need controller-specific data to set up slave
890 * transfers.
891 */
892 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
893
894 /* if cfg configuration specified take it instad of default */
895 if (atslave->cfg)
896 cfg = atslave->cfg;
897 }
898
899 /* have we already been set up?
900 * reconfigure channel but no need to reallocate descriptors */
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200901 if (!list_empty(&atchan->free_list))
902 return atchan->descs_allocated;
903
904 /* Allocate initial pool of descriptors */
905 for (i = 0; i < init_nr_desc_per_channel; i++) {
906 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
907 if (!desc) {
908 dev_err(atdma->dma_common.dev,
909 "Only %d initial descriptors\n", i);
910 break;
911 }
912 list_add_tail(&desc->desc_node, &tmp_list);
913 }
914
915 spin_lock_bh(&atchan->lock);
916 atchan->descs_allocated = i;
917 list_splice(&tmp_list, &atchan->free_list);
918 atchan->completed_cookie = chan->cookie = 1;
919 spin_unlock_bh(&atchan->lock);
920
921 /* channel parameters */
Nicolas Ferre808347f2009-07-22 20:04:45 +0200922 channel_writel(atchan, CFG, cfg);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200923
924 dev_dbg(chan2dev(chan),
925 "alloc_chan_resources: allocated %d descriptors\n",
926 atchan->descs_allocated);
927
928 return atchan->descs_allocated;
929}
930
931/**
932 * atc_free_chan_resources - free all channel resources
933 * @chan: DMA channel
934 */
935static void atc_free_chan_resources(struct dma_chan *chan)
936{
937 struct at_dma_chan *atchan = to_at_dma_chan(chan);
938 struct at_dma *atdma = to_at_dma(chan->device);
939 struct at_desc *desc, *_desc;
940 LIST_HEAD(list);
941
942 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
943 atchan->descs_allocated);
944
945 /* ASSERT: channel is idle */
946 BUG_ON(!list_empty(&atchan->active_list));
947 BUG_ON(!list_empty(&atchan->queue));
948 BUG_ON(atc_chan_is_enabled(atchan));
949
950 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
951 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
952 list_del(&desc->desc_node);
953 /* free link descriptor */
954 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
955 }
956 list_splice_init(&atchan->free_list, &list);
957 atchan->descs_allocated = 0;
958
959 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
960}
961
962
963/*-- Module Management -----------------------------------------------*/
964
965/**
966 * at_dma_off - disable DMA controller
967 * @atdma: the Atmel HDAMC device
968 */
969static void at_dma_off(struct at_dma *atdma)
970{
971 dma_writel(atdma, EN, 0);
972
973 /* disable all interrupts */
974 dma_writel(atdma, EBCIDR, -1L);
975
976 /* confirm that all channels are disabled */
977 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
978 cpu_relax();
979}
980
981static int __init at_dma_probe(struct platform_device *pdev)
982{
983 struct at_dma_platform_data *pdata;
984 struct resource *io;
985 struct at_dma *atdma;
986 size_t size;
987 int irq;
988 int err;
989 int i;
990
991 /* get DMA Controller parameters from platform */
992 pdata = pdev->dev.platform_data;
993 if (!pdata || pdata->nr_channels > AT_DMA_MAX_NR_CHANNELS)
994 return -EINVAL;
995
996 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
997 if (!io)
998 return -EINVAL;
999
1000 irq = platform_get_irq(pdev, 0);
1001 if (irq < 0)
1002 return irq;
1003
1004 size = sizeof(struct at_dma);
1005 size += pdata->nr_channels * sizeof(struct at_dma_chan);
1006 atdma = kzalloc(size, GFP_KERNEL);
1007 if (!atdma)
1008 return -ENOMEM;
1009
1010 /* discover transaction capabilites from the platform data */
1011 atdma->dma_common.cap_mask = pdata->cap_mask;
1012 atdma->all_chan_mask = (1 << pdata->nr_channels) - 1;
1013
1014 size = io->end - io->start + 1;
1015 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1016 err = -EBUSY;
1017 goto err_kfree;
1018 }
1019
1020 atdma->regs = ioremap(io->start, size);
1021 if (!atdma->regs) {
1022 err = -ENOMEM;
1023 goto err_release_r;
1024 }
1025
1026 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1027 if (IS_ERR(atdma->clk)) {
1028 err = PTR_ERR(atdma->clk);
1029 goto err_clk;
1030 }
1031 clk_enable(atdma->clk);
1032
1033 /* force dma off, just in case */
1034 at_dma_off(atdma);
1035
1036 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1037 if (err)
1038 goto err_irq;
1039
1040 platform_set_drvdata(pdev, atdma);
1041
1042 /* create a pool of consistent memory blocks for hardware descriptors */
1043 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1044 &pdev->dev, sizeof(struct at_desc),
1045 4 /* word alignment */, 0);
1046 if (!atdma->dma_desc_pool) {
1047 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1048 err = -ENOMEM;
1049 goto err_pool_create;
1050 }
1051
1052 /* clear any pending interrupt */
1053 while (dma_readl(atdma, EBCISR))
1054 cpu_relax();
1055
1056 /* initialize channels related values */
1057 INIT_LIST_HEAD(&atdma->dma_common.channels);
1058 for (i = 0; i < pdata->nr_channels; i++, atdma->dma_common.chancnt++) {
1059 struct at_dma_chan *atchan = &atdma->chan[i];
1060
1061 atchan->chan_common.device = &atdma->dma_common;
1062 atchan->chan_common.cookie = atchan->completed_cookie = 1;
1063 atchan->chan_common.chan_id = i;
1064 list_add_tail(&atchan->chan_common.device_node,
1065 &atdma->dma_common.channels);
1066
1067 atchan->ch_regs = atdma->regs + ch_regs(i);
1068 spin_lock_init(&atchan->lock);
1069 atchan->mask = 1 << i;
1070
1071 INIT_LIST_HEAD(&atchan->active_list);
1072 INIT_LIST_HEAD(&atchan->queue);
1073 INIT_LIST_HEAD(&atchan->free_list);
1074
1075 tasklet_init(&atchan->tasklet, atc_tasklet,
1076 (unsigned long)atchan);
1077 atc_enable_irq(atchan);
1078 }
1079
1080 /* set base routines */
1081 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1082 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
1083 atdma->dma_common.device_is_tx_complete = atc_is_tx_complete;
1084 atdma->dma_common.device_issue_pending = atc_issue_pending;
1085 atdma->dma_common.dev = &pdev->dev;
1086
1087 /* set prep routines based on capability */
1088 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1089 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1090
Nicolas Ferre808347f2009-07-22 20:04:45 +02001091 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
1092 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
1093 atdma->dma_common.device_terminate_all = atc_terminate_all;
1094 }
1095
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001096 dma_writel(atdma, EN, AT_DMA_ENABLE);
1097
1098 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
1099 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1100 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
1101 atdma->dma_common.chancnt);
1102
1103 dma_async_device_register(&atdma->dma_common);
1104
1105 return 0;
1106
1107err_pool_create:
1108 platform_set_drvdata(pdev, NULL);
1109 free_irq(platform_get_irq(pdev, 0), atdma);
1110err_irq:
1111 clk_disable(atdma->clk);
1112 clk_put(atdma->clk);
1113err_clk:
1114 iounmap(atdma->regs);
1115 atdma->regs = NULL;
1116err_release_r:
1117 release_mem_region(io->start, size);
1118err_kfree:
1119 kfree(atdma);
1120 return err;
1121}
1122
1123static int __exit at_dma_remove(struct platform_device *pdev)
1124{
1125 struct at_dma *atdma = platform_get_drvdata(pdev);
1126 struct dma_chan *chan, *_chan;
1127 struct resource *io;
1128
1129 at_dma_off(atdma);
1130 dma_async_device_unregister(&atdma->dma_common);
1131
1132 dma_pool_destroy(atdma->dma_desc_pool);
1133 platform_set_drvdata(pdev, NULL);
1134 free_irq(platform_get_irq(pdev, 0), atdma);
1135
1136 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1137 device_node) {
1138 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1139
1140 /* Disable interrupts */
1141 atc_disable_irq(atchan);
1142 tasklet_disable(&atchan->tasklet);
1143
1144 tasklet_kill(&atchan->tasklet);
1145 list_del(&chan->device_node);
1146 }
1147
1148 clk_disable(atdma->clk);
1149 clk_put(atdma->clk);
1150
1151 iounmap(atdma->regs);
1152 atdma->regs = NULL;
1153
1154 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1155 release_mem_region(io->start, io->end - io->start + 1);
1156
1157 kfree(atdma);
1158
1159 return 0;
1160}
1161
1162static void at_dma_shutdown(struct platform_device *pdev)
1163{
1164 struct at_dma *atdma = platform_get_drvdata(pdev);
1165
1166 at_dma_off(platform_get_drvdata(pdev));
1167 clk_disable(atdma->clk);
1168}
1169
1170static int at_dma_suspend_late(struct platform_device *pdev, pm_message_t mesg)
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 return 0;
1177}
1178
1179static int at_dma_resume_early(struct platform_device *pdev)
1180{
1181 struct at_dma *atdma = platform_get_drvdata(pdev);
1182
1183 clk_enable(atdma->clk);
1184 dma_writel(atdma, EN, AT_DMA_ENABLE);
1185 return 0;
1186
1187}
1188
1189static struct platform_driver at_dma_driver = {
1190 .remove = __exit_p(at_dma_remove),
1191 .shutdown = at_dma_shutdown,
1192 .suspend_late = at_dma_suspend_late,
1193 .resume_early = at_dma_resume_early,
1194 .driver = {
1195 .name = "at_hdmac",
1196 },
1197};
1198
1199static int __init at_dma_init(void)
1200{
1201 return platform_driver_probe(&at_dma_driver, at_dma_probe);
1202}
1203module_init(at_dma_init);
1204
1205static void __exit at_dma_exit(void)
1206{
1207 platform_driver_unregister(&at_dma_driver);
1208}
1209module_exit(at_dma_exit);
1210
1211MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1212MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1213MODULE_LICENSE("GPL");
1214MODULE_ALIAS("platform:at_hdmac");