blob: ce26ba3811443b4f1390d05ff0f8c69af8175ee0 [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 Ferrec5115952011-10-17 14:56:41 +020026#include <linux/of.h>
27#include <linux/of_device.h>
Nicolas Ferredc78baa2009-07-03 19:24:33 +020028
29#include "at_hdmac_regs.h"
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000030#include "dmaengine.h"
Nicolas Ferredc78baa2009-07-03 19:24:33 +020031
32/*
33 * Glossary
34 * --------
35 *
36 * at_hdmac : Name of the ATmel AHB DMA Controller
37 * at_dma_ / atdma : ATmel DMA controller entity related
38 * atc_ / atchan : ATmel DMA Channel entity related
39 */
40
41#define ATC_DEFAULT_CFG (ATC_FIFOCFG_HALFFIFO)
42#define ATC_DEFAULT_CTRLA (0)
Nicolas Ferreae14d4b2011-04-30 16:57:49 +020043#define ATC_DEFAULT_CTRLB (ATC_SIF(AT_DMA_MEM_IF) \
44 |ATC_DIF(AT_DMA_MEM_IF))
Nicolas Ferredc78baa2009-07-03 19:24:33 +020045
46/*
47 * Initial number of descriptors to allocate for each channel. This could
48 * be increased during dma usage.
49 */
50static unsigned int init_nr_desc_per_channel = 64;
51module_param(init_nr_desc_per_channel, uint, 0644);
52MODULE_PARM_DESC(init_nr_desc_per_channel,
53 "initial descriptors per channel (default: 64)");
54
55
56/* prototypes */
57static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx);
58
59
60/*----------------------------------------------------------------------*/
61
62static struct at_desc *atc_first_active(struct at_dma_chan *atchan)
63{
64 return list_first_entry(&atchan->active_list,
65 struct at_desc, desc_node);
66}
67
68static struct at_desc *atc_first_queued(struct at_dma_chan *atchan)
69{
70 return list_first_entry(&atchan->queue,
71 struct at_desc, desc_node);
72}
73
74/**
Uwe Kleine-König421f91d2010-06-11 12:17:00 +020075 * atc_alloc_descriptor - allocate and return an initialized descriptor
Nicolas Ferredc78baa2009-07-03 19:24:33 +020076 * @chan: the channel to allocate descriptors for
77 * @gfp_flags: GFP allocation flags
78 *
79 * Note: The ack-bit is positioned in the descriptor flag at creation time
80 * to make initial allocation more convenient. This bit will be cleared
81 * and control will be given to client at usage time (during
82 * preparation functions).
83 */
84static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan,
85 gfp_t gfp_flags)
86{
87 struct at_desc *desc = NULL;
88 struct at_dma *atdma = to_at_dma(chan->device);
89 dma_addr_t phys;
90
91 desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys);
92 if (desc) {
93 memset(desc, 0, sizeof(struct at_desc));
Dan Williams285a3c72009-09-08 17:53:03 -070094 INIT_LIST_HEAD(&desc->tx_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +020095 dma_async_tx_descriptor_init(&desc->txd, chan);
96 /* txd.flags will be overwritten in prep functions */
97 desc->txd.flags = DMA_CTRL_ACK;
98 desc->txd.tx_submit = atc_tx_submit;
99 desc->txd.phys = phys;
100 }
101
102 return desc;
103}
104
105/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200106 * atc_desc_get - get an unused descriptor from free_list
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200107 * @atchan: channel we want a new descriptor for
108 */
109static struct at_desc *atc_desc_get(struct at_dma_chan *atchan)
110{
111 struct at_desc *desc, *_desc;
112 struct at_desc *ret = NULL;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000113 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200114 unsigned int i = 0;
115 LIST_HEAD(tmp_list);
116
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000117 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200118 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
119 i++;
120 if (async_tx_test_ack(&desc->txd)) {
121 list_del(&desc->desc_node);
122 ret = desc;
123 break;
124 }
125 dev_dbg(chan2dev(&atchan->chan_common),
126 "desc %p not ACKed\n", desc);
127 }
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000128 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200129 dev_vdbg(chan2dev(&atchan->chan_common),
130 "scanned %u descriptors on freelist\n", i);
131
132 /* no more descriptor available in initial pool: create one more */
133 if (!ret) {
134 ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC);
135 if (ret) {
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000136 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200137 atchan->descs_allocated++;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000138 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200139 } else {
140 dev_err(chan2dev(&atchan->chan_common),
141 "not enough descriptors available\n");
142 }
143 }
144
145 return ret;
146}
147
148/**
149 * atc_desc_put - move a descriptor, including any children, to the free list
150 * @atchan: channel we work on
151 * @desc: descriptor, at the head of a chain, to move to free list
152 */
153static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc)
154{
155 if (desc) {
156 struct at_desc *child;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000157 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200158
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000159 spin_lock_irqsave(&atchan->lock, flags);
Dan Williams285a3c72009-09-08 17:53:03 -0700160 list_for_each_entry(child, &desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200161 dev_vdbg(chan2dev(&atchan->chan_common),
162 "moving child desc %p to freelist\n",
163 child);
Dan Williams285a3c72009-09-08 17:53:03 -0700164 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200165 dev_vdbg(chan2dev(&atchan->chan_common),
166 "moving desc %p to freelist\n", desc);
167 list_add(&desc->desc_node, &atchan->free_list);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000168 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200169 }
170}
171
172/**
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200173 * atc_desc_chain - build chain adding a descripor
174 * @first: address of first descripor of the chain
175 * @prev: address of previous descripor of the chain
176 * @desc: descriptor to queue
177 *
178 * Called from prep_* functions
179 */
180static void atc_desc_chain(struct at_desc **first, struct at_desc **prev,
181 struct at_desc *desc)
182{
183 if (!(*first)) {
184 *first = desc;
185 } else {
186 /* inform the HW lli about chaining */
187 (*prev)->lli.dscr = desc->txd.phys;
188 /* insert the link descriptor to the LD ring */
189 list_add_tail(&desc->desc_node,
190 &(*first)->tx_list);
191 }
192 *prev = desc;
193}
194
195/**
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200196 * atc_assign_cookie - compute and assign new cookie
197 * @atchan: channel we work on
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300198 * @desc: descriptor to assign cookie for
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200199 *
200 * Called with atchan->lock held and bh disabled
201 */
202static dma_cookie_t
203atc_assign_cookie(struct at_dma_chan *atchan, struct at_desc *desc)
204{
205 dma_cookie_t cookie = atchan->chan_common.cookie;
206
207 if (++cookie < 0)
208 cookie = 1;
209
210 atchan->chan_common.cookie = cookie;
211 desc->txd.cookie = cookie;
212
213 return cookie;
214}
215
216/**
217 * atc_dostart - starts the DMA engine for real
218 * @atchan: the channel we want to start
219 * @first: first descriptor in the list we want to begin with
220 *
221 * Called with atchan->lock held and bh disabled
222 */
223static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
224{
225 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
226
227 /* ASSERT: channel is idle */
228 if (atc_chan_is_enabled(atchan)) {
229 dev_err(chan2dev(&atchan->chan_common),
230 "BUG: Attempted to start non-idle channel\n");
231 dev_err(chan2dev(&atchan->chan_common),
232 " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
233 channel_readl(atchan, SADDR),
234 channel_readl(atchan, DADDR),
235 channel_readl(atchan, CTRLA),
236 channel_readl(atchan, CTRLB),
237 channel_readl(atchan, DSCR));
238
239 /* The tasklet will hopefully advance the queue... */
240 return;
241 }
242
243 vdbg_dump_regs(atchan);
244
245 /* clear any pending interrupt */
246 while (dma_readl(atdma, EBCISR))
247 cpu_relax();
248
249 channel_writel(atchan, SADDR, 0);
250 channel_writel(atchan, DADDR, 0);
251 channel_writel(atchan, CTRLA, 0);
252 channel_writel(atchan, CTRLB, 0);
253 channel_writel(atchan, DSCR, first->txd.phys);
254 dma_writel(atdma, CHER, atchan->mask);
255
256 vdbg_dump_regs(atchan);
257}
258
259/**
260 * atc_chain_complete - finish work for one transaction chain
261 * @atchan: channel we work on
262 * @desc: descriptor at the head of the chain we want do complete
263 *
264 * Called with atchan->lock held and bh disabled */
265static void
266atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
267{
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200268 struct dma_async_tx_descriptor *txd = &desc->txd;
269
270 dev_vdbg(chan2dev(&atchan->chan_common),
271 "descriptor %u complete\n", txd->cookie);
272
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000273 atchan->chan_common.completed_cookie = txd->cookie;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200274
275 /* move children to free_list */
Dan Williams285a3c72009-09-08 17:53:03 -0700276 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200277 /* move myself to free_list */
278 list_move(&desc->desc_node, &atchan->free_list);
279
Nicolas Ferreebcf9b82011-01-12 15:39:06 +0100280 /* unmap dma addresses (not on slave channels) */
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700281 if (!atchan->chan_common.private) {
282 struct device *parent = chan2parent(&atchan->chan_common);
283 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
284 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
285 dma_unmap_single(parent,
286 desc->lli.daddr,
287 desc->len, DMA_FROM_DEVICE);
288 else
289 dma_unmap_page(parent,
290 desc->lli.daddr,
291 desc->len, DMA_FROM_DEVICE);
292 }
293 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
294 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
295 dma_unmap_single(parent,
296 desc->lli.saddr,
297 desc->len, DMA_TO_DEVICE);
298 else
299 dma_unmap_page(parent,
300 desc->lli.saddr,
301 desc->len, DMA_TO_DEVICE);
302 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200303 }
304
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200305 /* for cyclic transfers,
306 * no need to replay callback function while stopping */
Nicolas Ferre3c477482011-07-25 21:09:23 +0000307 if (!atc_chan_is_cyclic(atchan)) {
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200308 dma_async_tx_callback callback = txd->callback;
309 void *param = txd->callback_param;
310
311 /*
312 * The API requires that no submissions are done from a
313 * callback, so we don't need to drop the lock here
314 */
315 if (callback)
316 callback(param);
317 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200318
319 dma_run_dependencies(txd);
320}
321
322/**
323 * atc_complete_all - finish work for all transactions
324 * @atchan: channel to complete transactions for
325 *
326 * Eventually submit queued descriptors if any
327 *
328 * Assume channel is idle while calling this function
329 * Called with atchan->lock held and bh disabled
330 */
331static void atc_complete_all(struct at_dma_chan *atchan)
332{
333 struct at_desc *desc, *_desc;
334 LIST_HEAD(list);
335
336 dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
337
338 BUG_ON(atc_chan_is_enabled(atchan));
339
340 /*
341 * Submit queued descriptors ASAP, i.e. before we go through
342 * the completed ones.
343 */
344 if (!list_empty(&atchan->queue))
345 atc_dostart(atchan, atc_first_queued(atchan));
346 /* empty active_list now it is completed */
347 list_splice_init(&atchan->active_list, &list);
348 /* empty queue list by moving descriptors (if any) to active_list */
349 list_splice_init(&atchan->queue, &atchan->active_list);
350
351 list_for_each_entry_safe(desc, _desc, &list, desc_node)
352 atc_chain_complete(atchan, desc);
353}
354
355/**
356 * atc_cleanup_descriptors - cleanup up finished descriptors in active_list
357 * @atchan: channel to be cleaned up
358 *
359 * Called with atchan->lock held and bh disabled
360 */
361static void atc_cleanup_descriptors(struct at_dma_chan *atchan)
362{
363 struct at_desc *desc, *_desc;
364 struct at_desc *child;
365
366 dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n");
367
368 list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
369 if (!(desc->lli.ctrla & ATC_DONE))
370 /* This one is currently in progress */
371 return;
372
Dan Williams285a3c72009-09-08 17:53:03 -0700373 list_for_each_entry(child, &desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200374 if (!(child->lli.ctrla & ATC_DONE))
375 /* Currently in progress */
376 return;
377
378 /*
379 * No descriptors so far seem to be in progress, i.e.
380 * this chain must be done.
381 */
382 atc_chain_complete(atchan, desc);
383 }
384}
385
386/**
387 * atc_advance_work - at the end of a transaction, move forward
388 * @atchan: channel where the transaction ended
389 *
390 * Called with atchan->lock held and bh disabled
391 */
392static void atc_advance_work(struct at_dma_chan *atchan)
393{
394 dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
395
396 if (list_empty(&atchan->active_list) ||
397 list_is_singular(&atchan->active_list)) {
398 atc_complete_all(atchan);
399 } else {
400 atc_chain_complete(atchan, atc_first_active(atchan));
401 /* advance work */
402 atc_dostart(atchan, atc_first_active(atchan));
403 }
404}
405
406
407/**
408 * atc_handle_error - handle errors reported by DMA controller
409 * @atchan: channel where error occurs
410 *
411 * Called with atchan->lock held and bh disabled
412 */
413static void atc_handle_error(struct at_dma_chan *atchan)
414{
415 struct at_desc *bad_desc;
416 struct at_desc *child;
417
418 /*
419 * The descriptor currently at the head of the active list is
420 * broked. Since we don't have any way to report errors, we'll
421 * just have to scream loudly and try to carry on.
422 */
423 bad_desc = atc_first_active(atchan);
424 list_del_init(&bad_desc->desc_node);
425
426 /* As we are stopped, take advantage to push queued descriptors
427 * in active_list */
428 list_splice_init(&atchan->queue, atchan->active_list.prev);
429
430 /* Try to restart the controller */
431 if (!list_empty(&atchan->active_list))
432 atc_dostart(atchan, atc_first_active(atchan));
433
434 /*
435 * KERN_CRITICAL may seem harsh, but since this only happens
436 * when someone submits a bad physical address in a
437 * descriptor, we should consider ourselves lucky that the
438 * controller flagged an error instead of scribbling over
439 * random memory locations.
440 */
441 dev_crit(chan2dev(&atchan->chan_common),
442 "Bad descriptor submitted for DMA!\n");
443 dev_crit(chan2dev(&atchan->chan_common),
444 " cookie: %d\n", bad_desc->txd.cookie);
445 atc_dump_lli(atchan, &bad_desc->lli);
Dan Williams285a3c72009-09-08 17:53:03 -0700446 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200447 atc_dump_lli(atchan, &child->lli);
448
449 /* Pretend the descriptor completed successfully */
450 atc_chain_complete(atchan, bad_desc);
451}
452
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200453/**
454 * atc_handle_cyclic - at the end of a period, run callback function
455 * @atchan: channel used for cyclic operations
456 *
457 * Called with atchan->lock held and bh disabled
458 */
459static void atc_handle_cyclic(struct at_dma_chan *atchan)
460{
461 struct at_desc *first = atc_first_active(atchan);
462 struct dma_async_tx_descriptor *txd = &first->txd;
463 dma_async_tx_callback callback = txd->callback;
464 void *param = txd->callback_param;
465
466 dev_vdbg(chan2dev(&atchan->chan_common),
467 "new cyclic period llp 0x%08x\n",
468 channel_readl(atchan, DSCR));
469
470 if (callback)
471 callback(param);
472}
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200473
474/*-- IRQ & Tasklet ---------------------------------------------------*/
475
476static void atc_tasklet(unsigned long data)
477{
478 struct at_dma_chan *atchan = (struct at_dma_chan *)data;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000479 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200480
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000481 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200482 if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200483 atc_handle_error(atchan);
Nicolas Ferre3c477482011-07-25 21:09:23 +0000484 else if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200485 atc_handle_cyclic(atchan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200486 else
487 atc_advance_work(atchan);
488
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000489 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200490}
491
492static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
493{
494 struct at_dma *atdma = (struct at_dma *)dev_id;
495 struct at_dma_chan *atchan;
496 int i;
497 u32 status, pending, imr;
498 int ret = IRQ_NONE;
499
500 do {
501 imr = dma_readl(atdma, EBCIMR);
502 status = dma_readl(atdma, EBCISR);
503 pending = status & imr;
504
505 if (!pending)
506 break;
507
508 dev_vdbg(atdma->dma_common.dev,
509 "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
510 status, imr, pending);
511
512 for (i = 0; i < atdma->dma_common.chancnt; i++) {
513 atchan = &atdma->chan[i];
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200514 if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200515 if (pending & AT_DMA_ERR(i)) {
516 /* Disable channel on AHB error */
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200517 dma_writel(atdma, CHDR,
518 AT_DMA_RES(i) | atchan->mask);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200519 /* 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;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000548 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200549
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000550 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200551 cookie = atc_assign_cookie(atchan, desc);
552
553 if (list_empty(&atchan->active_list)) {
554 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
555 desc->txd.cookie);
556 atc_dostart(atchan, desc);
557 list_add_tail(&desc->desc_node, &atchan->active_list);
558 } else {
559 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
560 desc->txd.cookie);
561 list_add_tail(&desc->desc_node, &atchan->queue);
562 }
563
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000564 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200565
566 return cookie;
567}
568
569/**
570 * atc_prep_dma_memcpy - prepare a memcpy operation
571 * @chan: the channel to prepare operation on
572 * @dest: operation virtual destination address
573 * @src: operation virtual source address
574 * @len: operation length
575 * @flags: tx descriptor status flags
576 */
577static struct dma_async_tx_descriptor *
578atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
579 size_t len, unsigned long flags)
580{
581 struct at_dma_chan *atchan = to_at_dma_chan(chan);
582 struct at_desc *desc = NULL;
583 struct at_desc *first = NULL;
584 struct at_desc *prev = NULL;
585 size_t xfer_count;
586 size_t offset;
587 unsigned int src_width;
588 unsigned int dst_width;
589 u32 ctrla;
590 u32 ctrlb;
591
592 dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
593 dest, src, len, flags);
594
595 if (unlikely(!len)) {
596 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
597 return NULL;
598 }
599
600 ctrla = ATC_DEFAULT_CTRLA;
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200601 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200602 | ATC_SRC_ADDR_MODE_INCR
603 | ATC_DST_ADDR_MODE_INCR
604 | ATC_FC_MEM2MEM;
605
606 /*
607 * We can be a lot more clever here, but this should take care
608 * of the most common optimization.
609 */
610 if (!((src | dest | len) & 3)) {
611 ctrla |= ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
612 src_width = dst_width = 2;
613 } else if (!((src | dest | len) & 1)) {
614 ctrla |= ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
615 src_width = dst_width = 1;
616 } else {
617 ctrla |= ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
618 src_width = dst_width = 0;
619 }
620
621 for (offset = 0; offset < len; offset += xfer_count << src_width) {
622 xfer_count = min_t(size_t, (len - offset) >> src_width,
623 ATC_BTSIZE_MAX);
624
625 desc = atc_desc_get(atchan);
626 if (!desc)
627 goto err_desc_get;
628
629 desc->lli.saddr = src + offset;
630 desc->lli.daddr = dest + offset;
631 desc->lli.ctrla = ctrla | xfer_count;
632 desc->lli.ctrlb = ctrlb;
633
634 desc->txd.cookie = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200635
Nicolas Ferree257e152011-05-06 19:56:53 +0200636 atc_desc_chain(&first, &prev, desc);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200637 }
638
639 /* First descriptor of the chain embedds additional information */
640 first->txd.cookie = -EBUSY;
641 first->len = len;
642
643 /* set end-of-link to the last link descriptor of list*/
644 set_desc_eol(desc);
645
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100646 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200647
648 return &first->txd;
649
650err_desc_get:
651 atc_desc_put(atchan, first);
652 return NULL;
653}
654
Nicolas Ferre808347f2009-07-22 20:04:45 +0200655
656/**
657 * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
658 * @chan: DMA channel
659 * @sgl: scatterlist to transfer to/from
660 * @sg_len: number of entries in @scatterlist
661 * @direction: DMA direction
662 * @flags: tx descriptor status flags
663 */
664static struct dma_async_tx_descriptor *
665atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530666 unsigned int sg_len, enum dma_transfer_direction direction,
Nicolas Ferre808347f2009-07-22 20:04:45 +0200667 unsigned long flags)
668{
669 struct at_dma_chan *atchan = to_at_dma_chan(chan);
670 struct at_dma_slave *atslave = chan->private;
671 struct at_desc *first = NULL;
672 struct at_desc *prev = NULL;
673 u32 ctrla;
674 u32 ctrlb;
675 dma_addr_t reg;
676 unsigned int reg_width;
677 unsigned int mem_width;
678 unsigned int i;
679 struct scatterlist *sg;
680 size_t total_len = 0;
681
Nicolas Ferrecc52a102011-04-30 16:57:47 +0200682 dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
683 sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530684 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre808347f2009-07-22 20:04:45 +0200685 flags);
686
687 if (unlikely(!atslave || !sg_len)) {
688 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
689 return NULL;
690 }
691
692 reg_width = atslave->reg_width;
693
Nicolas Ferre808347f2009-07-22 20:04:45 +0200694 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200695 ctrlb = ATC_IEN;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200696
697 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530698 case DMA_MEM_TO_DEV:
Nicolas Ferre808347f2009-07-22 20:04:45 +0200699 ctrla |= ATC_DST_WIDTH(reg_width);
700 ctrlb |= ATC_DST_ADDR_MODE_FIXED
701 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200702 | ATC_FC_MEM2PER
703 | ATC_SIF(AT_DMA_MEM_IF) | ATC_DIF(AT_DMA_PER_IF);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200704 reg = atslave->tx_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
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100714 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200715 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 = mem;
721 desc->lli.daddr = reg;
722 desc->lli.ctrla = ctrla
723 | ATC_SRC_WIDTH(mem_width)
724 | len >> mem_width;
725 desc->lli.ctrlb = ctrlb;
726
Nicolas Ferree257e152011-05-06 19:56:53 +0200727 atc_desc_chain(&first, &prev, desc);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200728 total_len += len;
729 }
730 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530731 case DMA_DEV_TO_MEM:
Nicolas Ferre808347f2009-07-22 20:04:45 +0200732 ctrla |= ATC_SRC_WIDTH(reg_width);
733 ctrlb |= ATC_DST_ADDR_MODE_INCR
734 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200735 | ATC_FC_PER2MEM
736 | ATC_SIF(AT_DMA_PER_IF) | ATC_DIF(AT_DMA_MEM_IF);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200737
738 reg = atslave->rx_reg;
739 for_each_sg(sgl, sg, sg_len, i) {
740 struct at_desc *desc;
741 u32 len;
742 u32 mem;
743
744 desc = atc_desc_get(atchan);
745 if (!desc)
746 goto err_desc_get;
747
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100748 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200749 len = sg_dma_len(sg);
750 mem_width = 2;
751 if (unlikely(mem & 3 || len & 3))
752 mem_width = 0;
753
754 desc->lli.saddr = reg;
755 desc->lli.daddr = mem;
756 desc->lli.ctrla = ctrla
757 | ATC_DST_WIDTH(mem_width)
Nicolas Ferre59a609d2010-12-13 13:48:41 +0100758 | len >> reg_width;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200759 desc->lli.ctrlb = ctrlb;
760
Nicolas Ferree257e152011-05-06 19:56:53 +0200761 atc_desc_chain(&first, &prev, desc);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200762 total_len += len;
763 }
764 break;
765 default:
766 return NULL;
767 }
768
769 /* set end-of-link to the last link descriptor of list*/
770 set_desc_eol(prev);
771
772 /* First descriptor of the chain embedds additional information */
773 first->txd.cookie = -EBUSY;
774 first->len = total_len;
775
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100776 /* first link descriptor of list is responsible of flags */
777 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferre808347f2009-07-22 20:04:45 +0200778
779 return &first->txd;
780
781err_desc_get:
782 dev_err(chan2dev(chan), "not enough descriptors available\n");
783 atc_desc_put(atchan, first);
784 return NULL;
785}
786
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200787/**
788 * atc_dma_cyclic_check_values
789 * Check for too big/unaligned periods and unaligned DMA buffer
790 */
791static int
792atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530793 size_t period_len, enum dma_transfer_direction direction)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200794{
795 if (period_len > (ATC_BTSIZE_MAX << reg_width))
796 goto err_out;
797 if (unlikely(period_len & ((1 << reg_width) - 1)))
798 goto err_out;
799 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
800 goto err_out;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530801 if (unlikely(!(direction & (DMA_DEV_TO_MEM | DMA_MEM_TO_DEV))))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200802 goto err_out;
803
804 return 0;
805
806err_out:
807 return -EINVAL;
808}
809
810/**
811 * atc_dma_cyclic_fill_desc - Fill one period decriptor
812 */
813static int
814atc_dma_cyclic_fill_desc(struct at_dma_slave *atslave, struct at_desc *desc,
815 unsigned int period_index, dma_addr_t buf_addr,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530816 size_t period_len, enum dma_transfer_direction direction)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200817{
818 u32 ctrla;
819 unsigned int reg_width = atslave->reg_width;
820
821 /* prepare common CRTLA value */
822 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla
823 | ATC_DST_WIDTH(reg_width)
824 | ATC_SRC_WIDTH(reg_width)
825 | period_len >> reg_width;
826
827 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530828 case DMA_MEM_TO_DEV:
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200829 desc->lli.saddr = buf_addr + (period_len * period_index);
830 desc->lli.daddr = atslave->tx_reg;
831 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200832 desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200833 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200834 | ATC_FC_MEM2PER
835 | ATC_SIF(AT_DMA_MEM_IF)
836 | ATC_DIF(AT_DMA_PER_IF);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200837 break;
838
Vinod Kouldb8196d2011-10-13 22:34:23 +0530839 case DMA_DEV_TO_MEM:
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200840 desc->lli.saddr = atslave->rx_reg;
841 desc->lli.daddr = buf_addr + (period_len * period_index);
842 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200843 desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200844 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200845 | ATC_FC_PER2MEM
846 | ATC_SIF(AT_DMA_PER_IF)
847 | ATC_DIF(AT_DMA_MEM_IF);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200848 break;
849
850 default:
851 return -EINVAL;
852 }
853
854 return 0;
855}
856
857/**
858 * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
859 * @chan: the DMA channel to prepare
860 * @buf_addr: physical DMA address where the buffer starts
861 * @buf_len: total number of bytes for the entire buffer
862 * @period_len: number of bytes for each period
863 * @direction: transfer direction, to or from device
864 */
865static struct dma_async_tx_descriptor *
866atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530867 size_t period_len, enum dma_transfer_direction direction)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200868{
869 struct at_dma_chan *atchan = to_at_dma_chan(chan);
870 struct at_dma_slave *atslave = chan->private;
871 struct at_desc *first = NULL;
872 struct at_desc *prev = NULL;
873 unsigned long was_cyclic;
874 unsigned int periods = buf_len / period_len;
875 unsigned int i;
876
877 dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
Vinod Kouldb8196d2011-10-13 22:34:23 +0530878 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200879 buf_addr,
880 periods, buf_len, period_len);
881
882 if (unlikely(!atslave || !buf_len || !period_len)) {
883 dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
884 return NULL;
885 }
886
887 was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
888 if (was_cyclic) {
889 dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
890 return NULL;
891 }
892
893 /* Check for too big/unaligned periods and unaligned DMA buffer */
894 if (atc_dma_cyclic_check_values(atslave->reg_width, buf_addr,
895 period_len, direction))
896 goto err_out;
897
898 /* build cyclic linked list */
899 for (i = 0; i < periods; i++) {
900 struct at_desc *desc;
901
902 desc = atc_desc_get(atchan);
903 if (!desc)
904 goto err_desc_get;
905
906 if (atc_dma_cyclic_fill_desc(atslave, desc, i, buf_addr,
907 period_len, direction))
908 goto err_desc_get;
909
910 atc_desc_chain(&first, &prev, desc);
911 }
912
913 /* lets make a cyclic list */
914 prev->lli.dscr = first->txd.phys;
915
916 /* First descriptor of the chain embedds additional information */
917 first->txd.cookie = -EBUSY;
918 first->len = buf_len;
919
920 return &first->txd;
921
922err_desc_get:
923 dev_err(chan2dev(chan), "not enough descriptors available\n");
924 atc_desc_put(atchan, first);
925err_out:
926 clear_bit(ATC_IS_CYCLIC, &atchan->status);
927 return NULL;
928}
929
930
Linus Walleij05827632010-05-17 16:30:42 -0700931static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
932 unsigned long arg)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200933{
934 struct at_dma_chan *atchan = to_at_dma_chan(chan);
935 struct at_dma *atdma = to_at_dma(chan->device);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200936 int chan_id = atchan->chan_common.chan_id;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000937 unsigned long flags;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200938
Nicolas Ferre808347f2009-07-22 20:04:45 +0200939 LIST_HEAD(list);
940
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200941 dev_vdbg(chan2dev(chan), "atc_control (%d)\n", cmd);
942
943 if (cmd == DMA_PAUSE) {
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000944 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200945
946 dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200947 set_bit(ATC_IS_PAUSED, &atchan->status);
948
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000949 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200950 } else if (cmd == DMA_RESUME) {
Nicolas Ferre3c477482011-07-25 21:09:23 +0000951 if (!atc_chan_is_paused(atchan))
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200952 return 0;
953
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000954 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200955
956 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
957 clear_bit(ATC_IS_PAUSED, &atchan->status);
958
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000959 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200960 } else if (cmd == DMA_TERMINATE_ALL) {
961 struct at_desc *desc, *_desc;
962 /*
963 * This is only called when something went wrong elsewhere, so
964 * we don't really care about the data. Just disable the
965 * channel. We still have to poll the channel enable bit due
966 * to AHB/HSB limitations.
967 */
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000968 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200969
970 /* disabling channel: must also remove suspend state */
971 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
972
973 /* confirm that this channel is disabled */
974 while (dma_readl(atdma, CHSR) & atchan->mask)
975 cpu_relax();
976
977 /* active_list entries will end up before queued entries */
978 list_splice_init(&atchan->queue, &list);
979 list_splice_init(&atchan->active_list, &list);
980
981 /* Flush all pending and queued descriptors */
982 list_for_each_entry_safe(desc, _desc, &list, desc_node)
983 atc_chain_complete(atchan, desc);
984
985 clear_bit(ATC_IS_PAUSED, &atchan->status);
986 /* if channel dedicated to cyclic operations, free it */
987 clear_bit(ATC_IS_CYCLIC, &atchan->status);
988
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000989 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200990 } else {
Linus Walleijc3635c72010-03-26 16:44:01 -0700991 return -ENXIO;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200992 }
Yong Wangb0ebeb92010-08-05 10:40:08 +0800993
Linus Walleijc3635c72010-03-26 16:44:01 -0700994 return 0;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200995}
996
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200997/**
Linus Walleij07934482010-03-26 16:50:49 -0700998 * atc_tx_status - poll for transaction completion
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200999 * @chan: DMA channel
1000 * @cookie: transaction identifier to check status of
Linus Walleij07934482010-03-26 16:50:49 -07001001 * @txstate: if not %NULL updated with transaction state
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001002 *
Linus Walleij07934482010-03-26 16:50:49 -07001003 * If @txstate is passed in, upon return it reflect the driver
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001004 * internal state and can be used with dma_async_is_complete() to check
1005 * the status of multiple cookies without re-checking hardware state.
1006 */
1007static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -07001008atc_tx_status(struct dma_chan *chan,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001009 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -07001010 struct dma_tx_state *txstate)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001011{
1012 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1013 dma_cookie_t last_used;
1014 dma_cookie_t last_complete;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001015 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001016 enum dma_status ret;
1017
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001018 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001019
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +00001020 last_complete = chan->completed_cookie;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001021 last_used = chan->cookie;
1022
1023 ret = dma_async_is_complete(cookie, last_complete, last_used);
1024 if (ret != DMA_SUCCESS) {
1025 atc_cleanup_descriptors(atchan);
1026
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +00001027 last_complete = chan->completed_cookie;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001028 last_used = chan->cookie;
1029
1030 ret = dma_async_is_complete(cookie, last_complete, last_used);
1031 }
1032
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001033 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001034
Nicolas Ferre543aabc2011-05-06 19:56:51 +02001035 if (ret != DMA_SUCCESS)
1036 dma_set_tx_state(txstate, last_complete, last_used,
1037 atc_first_active(atchan)->len);
1038 else
1039 dma_set_tx_state(txstate, last_complete, last_used, 0);
1040
Nicolas Ferre3c477482011-07-25 21:09:23 +00001041 if (atc_chan_is_paused(atchan))
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001042 ret = DMA_PAUSED;
1043
1044 dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d (d%d, u%d)\n",
1045 ret, cookie, last_complete ? last_complete : 0,
Linus Walleij07934482010-03-26 16:50:49 -07001046 last_used ? last_used : 0);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001047
1048 return ret;
1049}
1050
1051/**
1052 * atc_issue_pending - try to finish work
1053 * @chan: target DMA channel
1054 */
1055static void atc_issue_pending(struct dma_chan *chan)
1056{
1057 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001058 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001059
1060 dev_vdbg(chan2dev(chan), "issue_pending\n");
1061
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001062 /* Not needed for cyclic transfers */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001063 if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001064 return;
1065
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001066 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001067 if (!atc_chan_is_enabled(atchan)) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001068 atc_advance_work(atchan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001069 }
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001070 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001071}
1072
1073/**
1074 * atc_alloc_chan_resources - allocate resources for DMA channel
1075 * @chan: allocate descriptor resources for this channel
1076 * @client: current client requesting the channel be ready for requests
1077 *
1078 * return - the number of allocated descriptors
1079 */
1080static int atc_alloc_chan_resources(struct dma_chan *chan)
1081{
1082 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1083 struct at_dma *atdma = to_at_dma(chan->device);
1084 struct at_desc *desc;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001085 struct at_dma_slave *atslave;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001086 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001087 int i;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001088 u32 cfg;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001089 LIST_HEAD(tmp_list);
1090
1091 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
1092
1093 /* ASSERT: channel is idle */
1094 if (atc_chan_is_enabled(atchan)) {
1095 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
1096 return -EIO;
1097 }
1098
Nicolas Ferre808347f2009-07-22 20:04:45 +02001099 cfg = ATC_DEFAULT_CFG;
1100
1101 atslave = chan->private;
1102 if (atslave) {
1103 /*
1104 * We need controller-specific data to set up slave
1105 * transfers.
1106 */
1107 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
1108
1109 /* if cfg configuration specified take it instad of default */
1110 if (atslave->cfg)
1111 cfg = atslave->cfg;
1112 }
1113
1114 /* have we already been set up?
1115 * reconfigure channel but no need to reallocate descriptors */
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001116 if (!list_empty(&atchan->free_list))
1117 return atchan->descs_allocated;
1118
1119 /* Allocate initial pool of descriptors */
1120 for (i = 0; i < init_nr_desc_per_channel; i++) {
1121 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
1122 if (!desc) {
1123 dev_err(atdma->dma_common.dev,
1124 "Only %d initial descriptors\n", i);
1125 break;
1126 }
1127 list_add_tail(&desc->desc_node, &tmp_list);
1128 }
1129
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001130 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001131 atchan->descs_allocated = i;
1132 list_splice(&tmp_list, &atchan->free_list);
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +00001133 chan->completed_cookie = chan->cookie = 1;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001134 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001135
1136 /* channel parameters */
Nicolas Ferre808347f2009-07-22 20:04:45 +02001137 channel_writel(atchan, CFG, cfg);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001138
1139 dev_dbg(chan2dev(chan),
1140 "alloc_chan_resources: allocated %d descriptors\n",
1141 atchan->descs_allocated);
1142
1143 return atchan->descs_allocated;
1144}
1145
1146/**
1147 * atc_free_chan_resources - free all channel resources
1148 * @chan: DMA channel
1149 */
1150static void atc_free_chan_resources(struct dma_chan *chan)
1151{
1152 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1153 struct at_dma *atdma = to_at_dma(chan->device);
1154 struct at_desc *desc, *_desc;
1155 LIST_HEAD(list);
1156
1157 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
1158 atchan->descs_allocated);
1159
1160 /* ASSERT: channel is idle */
1161 BUG_ON(!list_empty(&atchan->active_list));
1162 BUG_ON(!list_empty(&atchan->queue));
1163 BUG_ON(atc_chan_is_enabled(atchan));
1164
1165 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
1166 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1167 list_del(&desc->desc_node);
1168 /* free link descriptor */
1169 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
1170 }
1171 list_splice_init(&atchan->free_list, &list);
1172 atchan->descs_allocated = 0;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001173 atchan->status = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001174
1175 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
1176}
1177
1178
1179/*-- Module Management -----------------------------------------------*/
1180
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001181/* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
1182static struct at_dma_platform_data at91sam9rl_config = {
1183 .nr_channels = 2,
1184};
1185static struct at_dma_platform_data at91sam9g45_config = {
1186 .nr_channels = 8,
1187};
1188
Nicolas Ferrec5115952011-10-17 14:56:41 +02001189#if defined(CONFIG_OF)
1190static const struct of_device_id atmel_dma_dt_ids[] = {
1191 {
1192 .compatible = "atmel,at91sam9rl-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001193 .data = &at91sam9rl_config,
Nicolas Ferrec5115952011-10-17 14:56:41 +02001194 }, {
1195 .compatible = "atmel,at91sam9g45-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001196 .data = &at91sam9g45_config,
Nicolas Ferredcc81732011-11-22 11:55:53 +01001197 }, {
1198 /* sentinel */
1199 }
Nicolas Ferrec5115952011-10-17 14:56:41 +02001200};
1201
1202MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
1203#endif
1204
Nicolas Ferre0ab88a02011-11-22 11:55:52 +01001205static const struct platform_device_id atdma_devtypes[] = {
Nicolas Ferre67348452011-10-17 14:56:40 +02001206 {
1207 .name = "at91sam9rl_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001208 .driver_data = (unsigned long) &at91sam9rl_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001209 }, {
1210 .name = "at91sam9g45_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001211 .driver_data = (unsigned long) &at91sam9g45_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001212 }, {
1213 /* sentinel */
1214 }
1215};
1216
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001217static inline struct at_dma_platform_data * __init at_dma_get_driver_data(
1218 struct platform_device *pdev)
Nicolas Ferrec5115952011-10-17 14:56:41 +02001219{
1220 if (pdev->dev.of_node) {
1221 const struct of_device_id *match;
1222 match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
1223 if (match == NULL)
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001224 return NULL;
1225 return match->data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001226 }
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001227 return (struct at_dma_platform_data *)
1228 platform_get_device_id(pdev)->driver_data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001229}
1230
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001231/**
1232 * at_dma_off - disable DMA controller
1233 * @atdma: the Atmel HDAMC device
1234 */
1235static void at_dma_off(struct at_dma *atdma)
1236{
1237 dma_writel(atdma, EN, 0);
1238
1239 /* disable all interrupts */
1240 dma_writel(atdma, EBCIDR, -1L);
1241
1242 /* confirm that all channels are disabled */
1243 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
1244 cpu_relax();
1245}
1246
1247static int __init at_dma_probe(struct platform_device *pdev)
1248{
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001249 struct resource *io;
1250 struct at_dma *atdma;
1251 size_t size;
1252 int irq;
1253 int err;
1254 int i;
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001255 struct at_dma_platform_data *plat_dat;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001256
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001257 /* setup platform data for each SoC */
1258 dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
1259 dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
1260 dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
Nicolas Ferre67348452011-10-17 14:56:40 +02001261
1262 /* get DMA parameters from controller type */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001263 plat_dat = at_dma_get_driver_data(pdev);
1264 if (!plat_dat)
1265 return -ENODEV;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001266
1267 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1268 if (!io)
1269 return -EINVAL;
1270
1271 irq = platform_get_irq(pdev, 0);
1272 if (irq < 0)
1273 return irq;
1274
1275 size = sizeof(struct at_dma);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001276 size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001277 atdma = kzalloc(size, GFP_KERNEL);
1278 if (!atdma)
1279 return -ENOMEM;
1280
Nicolas Ferre67348452011-10-17 14:56:40 +02001281 /* discover transaction capabilities */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001282 atdma->dma_common.cap_mask = plat_dat->cap_mask;
1283 atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001284
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07001285 size = resource_size(io);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001286 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1287 err = -EBUSY;
1288 goto err_kfree;
1289 }
1290
1291 atdma->regs = ioremap(io->start, size);
1292 if (!atdma->regs) {
1293 err = -ENOMEM;
1294 goto err_release_r;
1295 }
1296
1297 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1298 if (IS_ERR(atdma->clk)) {
1299 err = PTR_ERR(atdma->clk);
1300 goto err_clk;
1301 }
1302 clk_enable(atdma->clk);
1303
1304 /* force dma off, just in case */
1305 at_dma_off(atdma);
1306
1307 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1308 if (err)
1309 goto err_irq;
1310
1311 platform_set_drvdata(pdev, atdma);
1312
1313 /* create a pool of consistent memory blocks for hardware descriptors */
1314 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1315 &pdev->dev, sizeof(struct at_desc),
1316 4 /* word alignment */, 0);
1317 if (!atdma->dma_desc_pool) {
1318 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1319 err = -ENOMEM;
1320 goto err_pool_create;
1321 }
1322
1323 /* clear any pending interrupt */
1324 while (dma_readl(atdma, EBCISR))
1325 cpu_relax();
1326
1327 /* initialize channels related values */
1328 INIT_LIST_HEAD(&atdma->dma_common.channels);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001329 for (i = 0; i < plat_dat->nr_channels; i++) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001330 struct at_dma_chan *atchan = &atdma->chan[i];
1331
1332 atchan->chan_common.device = &atdma->dma_common;
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +00001333 atchan->chan_common.cookie = atchan->chan_common.completed_cookie = 1;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001334 list_add_tail(&atchan->chan_common.device_node,
1335 &atdma->dma_common.channels);
1336
1337 atchan->ch_regs = atdma->regs + ch_regs(i);
1338 spin_lock_init(&atchan->lock);
1339 atchan->mask = 1 << i;
1340
1341 INIT_LIST_HEAD(&atchan->active_list);
1342 INIT_LIST_HEAD(&atchan->queue);
1343 INIT_LIST_HEAD(&atchan->free_list);
1344
1345 tasklet_init(&atchan->tasklet, atc_tasklet,
1346 (unsigned long)atchan);
Nikolaus Vossbda3a472012-01-17 10:28:33 +01001347 atc_enable_chan_irq(atdma, i);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001348 }
1349
1350 /* set base routines */
1351 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1352 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
Linus Walleij07934482010-03-26 16:50:49 -07001353 atdma->dma_common.device_tx_status = atc_tx_status;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001354 atdma->dma_common.device_issue_pending = atc_issue_pending;
1355 atdma->dma_common.dev = &pdev->dev;
1356
1357 /* set prep routines based on capability */
1358 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1359 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1360
Nicolas Ferred7db8082011-08-05 11:43:44 +00001361 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
Nicolas Ferre808347f2009-07-22 20:04:45 +02001362 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001363 /* controller can do slave DMA: can trigger cyclic transfers */
1364 dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001365 atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
Linus Walleijc3635c72010-03-26 16:44:01 -07001366 atdma->dma_common.device_control = atc_control;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001367 }
Nicolas Ferre808347f2009-07-22 20:04:45 +02001368
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001369 dma_writel(atdma, EN, AT_DMA_ENABLE);
1370
1371 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
1372 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1373 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001374 plat_dat->nr_channels);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001375
1376 dma_async_device_register(&atdma->dma_common);
1377
1378 return 0;
1379
1380err_pool_create:
1381 platform_set_drvdata(pdev, NULL);
1382 free_irq(platform_get_irq(pdev, 0), atdma);
1383err_irq:
1384 clk_disable(atdma->clk);
1385 clk_put(atdma->clk);
1386err_clk:
1387 iounmap(atdma->regs);
1388 atdma->regs = NULL;
1389err_release_r:
1390 release_mem_region(io->start, size);
1391err_kfree:
1392 kfree(atdma);
1393 return err;
1394}
1395
1396static int __exit at_dma_remove(struct platform_device *pdev)
1397{
1398 struct at_dma *atdma = platform_get_drvdata(pdev);
1399 struct dma_chan *chan, *_chan;
1400 struct resource *io;
1401
1402 at_dma_off(atdma);
1403 dma_async_device_unregister(&atdma->dma_common);
1404
1405 dma_pool_destroy(atdma->dma_desc_pool);
1406 platform_set_drvdata(pdev, NULL);
1407 free_irq(platform_get_irq(pdev, 0), atdma);
1408
1409 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1410 device_node) {
1411 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1412
1413 /* Disable interrupts */
Nikolaus Vossbda3a472012-01-17 10:28:33 +01001414 atc_disable_chan_irq(atdma, chan->chan_id);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001415 tasklet_disable(&atchan->tasklet);
1416
1417 tasklet_kill(&atchan->tasklet);
1418 list_del(&chan->device_node);
1419 }
1420
1421 clk_disable(atdma->clk);
1422 clk_put(atdma->clk);
1423
1424 iounmap(atdma->regs);
1425 atdma->regs = NULL;
1426
1427 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07001428 release_mem_region(io->start, resource_size(io));
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001429
1430 kfree(atdma);
1431
1432 return 0;
1433}
1434
1435static void at_dma_shutdown(struct platform_device *pdev)
1436{
1437 struct at_dma *atdma = platform_get_drvdata(pdev);
1438
1439 at_dma_off(platform_get_drvdata(pdev));
1440 clk_disable(atdma->clk);
1441}
1442
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001443static int at_dma_prepare(struct device *dev)
1444{
1445 struct platform_device *pdev = to_platform_device(dev);
1446 struct at_dma *atdma = platform_get_drvdata(pdev);
1447 struct dma_chan *chan, *_chan;
1448
1449 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1450 device_node) {
1451 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1452 /* wait for transaction completion (except in cyclic case) */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001453 if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001454 return -EAGAIN;
1455 }
1456 return 0;
1457}
1458
1459static void atc_suspend_cyclic(struct at_dma_chan *atchan)
1460{
1461 struct dma_chan *chan = &atchan->chan_common;
1462
1463 /* Channel should be paused by user
1464 * do it anyway even if it is not done already */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001465 if (!atc_chan_is_paused(atchan)) {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001466 dev_warn(chan2dev(chan),
1467 "cyclic channel not paused, should be done by channel user\n");
1468 atc_control(chan, DMA_PAUSE, 0);
1469 }
1470
1471 /* now preserve additional data for cyclic operations */
1472 /* next descriptor address in the cyclic list */
1473 atchan->save_dscr = channel_readl(atchan, DSCR);
1474
1475 vdbg_dump_regs(atchan);
1476}
1477
Dan Williams33f82d12009-09-10 00:06:44 +02001478static int at_dma_suspend_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001479{
Dan Williams33f82d12009-09-10 00:06:44 +02001480 struct platform_device *pdev = to_platform_device(dev);
1481 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001482 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001483
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001484 /* preserve data */
1485 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1486 device_node) {
1487 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1488
Nicolas Ferre3c477482011-07-25 21:09:23 +00001489 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001490 atc_suspend_cyclic(atchan);
1491 atchan->save_cfg = channel_readl(atchan, CFG);
1492 }
1493 atdma->save_imr = dma_readl(atdma, EBCIMR);
1494
1495 /* disable DMA controller */
1496 at_dma_off(atdma);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001497 clk_disable(atdma->clk);
1498 return 0;
1499}
1500
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001501static void atc_resume_cyclic(struct at_dma_chan *atchan)
1502{
1503 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
1504
1505 /* restore channel status for cyclic descriptors list:
1506 * next descriptor in the cyclic list at the time of suspend */
1507 channel_writel(atchan, SADDR, 0);
1508 channel_writel(atchan, DADDR, 0);
1509 channel_writel(atchan, CTRLA, 0);
1510 channel_writel(atchan, CTRLB, 0);
1511 channel_writel(atchan, DSCR, atchan->save_dscr);
1512 dma_writel(atdma, CHER, atchan->mask);
1513
1514 /* channel pause status should be removed by channel user
1515 * We cannot take the initiative to do it here */
1516
1517 vdbg_dump_regs(atchan);
1518}
1519
Dan Williams33f82d12009-09-10 00:06:44 +02001520static int at_dma_resume_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001521{
Dan Williams33f82d12009-09-10 00:06:44 +02001522 struct platform_device *pdev = to_platform_device(dev);
1523 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001524 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001525
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001526 /* bring back DMA controller */
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001527 clk_enable(atdma->clk);
1528 dma_writel(atdma, EN, AT_DMA_ENABLE);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001529
1530 /* clear any pending interrupt */
1531 while (dma_readl(atdma, EBCISR))
1532 cpu_relax();
1533
1534 /* restore saved data */
1535 dma_writel(atdma, EBCIER, atdma->save_imr);
1536 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1537 device_node) {
1538 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1539
1540 channel_writel(atchan, CFG, atchan->save_cfg);
Nicolas Ferre3c477482011-07-25 21:09:23 +00001541 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001542 atc_resume_cyclic(atchan);
1543 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001544 return 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001545}
1546
Alexey Dobriyan47145212009-12-14 18:00:08 -08001547static const struct dev_pm_ops at_dma_dev_pm_ops = {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001548 .prepare = at_dma_prepare,
Dan Williams33f82d12009-09-10 00:06:44 +02001549 .suspend_noirq = at_dma_suspend_noirq,
1550 .resume_noirq = at_dma_resume_noirq,
1551};
1552
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001553static struct platform_driver at_dma_driver = {
1554 .remove = __exit_p(at_dma_remove),
1555 .shutdown = at_dma_shutdown,
Nicolas Ferre67348452011-10-17 14:56:40 +02001556 .id_table = atdma_devtypes,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001557 .driver = {
1558 .name = "at_hdmac",
Dan Williams33f82d12009-09-10 00:06:44 +02001559 .pm = &at_dma_dev_pm_ops,
Nicolas Ferrec5115952011-10-17 14:56:41 +02001560 .of_match_table = of_match_ptr(atmel_dma_dt_ids),
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001561 },
1562};
1563
1564static int __init at_dma_init(void)
1565{
1566 return platform_driver_probe(&at_dma_driver, at_dma_probe);
1567}
Eric Xu93d0bec2011-01-12 15:39:08 +01001568subsys_initcall(at_dma_init);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001569
1570static void __exit at_dma_exit(void)
1571{
1572 platform_driver_unregister(&at_dma_driver);
1573}
1574module_exit(at_dma_exit);
1575
1576MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1577MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1578MODULE_LICENSE("GPL");
1579MODULE_ALIAS("platform:at_hdmac");