blob: 9ec3943b12327566c6c8a39927788d74eecd5a88 [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_dostart - starts the DMA engine for real
197 * @atchan: the channel we want to start
198 * @first: first descriptor in the list we want to begin with
199 *
200 * Called with atchan->lock held and bh disabled
201 */
202static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
203{
204 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
205
206 /* ASSERT: channel is idle */
207 if (atc_chan_is_enabled(atchan)) {
208 dev_err(chan2dev(&atchan->chan_common),
209 "BUG: Attempted to start non-idle channel\n");
210 dev_err(chan2dev(&atchan->chan_common),
211 " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
212 channel_readl(atchan, SADDR),
213 channel_readl(atchan, DADDR),
214 channel_readl(atchan, CTRLA),
215 channel_readl(atchan, CTRLB),
216 channel_readl(atchan, DSCR));
217
218 /* The tasklet will hopefully advance the queue... */
219 return;
220 }
221
222 vdbg_dump_regs(atchan);
223
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200224 channel_writel(atchan, SADDR, 0);
225 channel_writel(atchan, DADDR, 0);
226 channel_writel(atchan, CTRLA, 0);
227 channel_writel(atchan, CTRLB, 0);
228 channel_writel(atchan, DSCR, first->txd.phys);
229 dma_writel(atdma, CHER, atchan->mask);
230
231 vdbg_dump_regs(atchan);
232}
233
234/**
235 * atc_chain_complete - finish work for one transaction chain
236 * @atchan: channel we work on
237 * @desc: descriptor at the head of the chain we want do complete
238 *
239 * Called with atchan->lock held and bh disabled */
240static void
241atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
242{
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200243 struct dma_async_tx_descriptor *txd = &desc->txd;
244
245 dev_vdbg(chan2dev(&atchan->chan_common),
246 "descriptor %u complete\n", txd->cookie);
247
Vinod Kould4116052012-05-11 11:48:21 +0530248 /* mark the descriptor as complete for non cyclic cases only */
249 if (!atc_chan_is_cyclic(atchan))
250 dma_cookie_complete(txd);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200251
252 /* move children to free_list */
Dan Williams285a3c72009-09-08 17:53:03 -0700253 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200254 /* move myself to free_list */
255 list_move(&desc->desc_node, &atchan->free_list);
256
Nicolas Ferreebcf9b82011-01-12 15:39:06 +0100257 /* unmap dma addresses (not on slave channels) */
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700258 if (!atchan->chan_common.private) {
259 struct device *parent = chan2parent(&atchan->chan_common);
260 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
261 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
262 dma_unmap_single(parent,
263 desc->lli.daddr,
264 desc->len, DMA_FROM_DEVICE);
265 else
266 dma_unmap_page(parent,
267 desc->lli.daddr,
268 desc->len, DMA_FROM_DEVICE);
269 }
270 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
271 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
272 dma_unmap_single(parent,
273 desc->lli.saddr,
274 desc->len, DMA_TO_DEVICE);
275 else
276 dma_unmap_page(parent,
277 desc->lli.saddr,
278 desc->len, DMA_TO_DEVICE);
279 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200280 }
281
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200282 /* for cyclic transfers,
283 * no need to replay callback function while stopping */
Nicolas Ferre3c477482011-07-25 21:09:23 +0000284 if (!atc_chan_is_cyclic(atchan)) {
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200285 dma_async_tx_callback callback = txd->callback;
286 void *param = txd->callback_param;
287
288 /*
289 * The API requires that no submissions are done from a
290 * callback, so we don't need to drop the lock here
291 */
292 if (callback)
293 callback(param);
294 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200295
296 dma_run_dependencies(txd);
297}
298
299/**
300 * atc_complete_all - finish work for all transactions
301 * @atchan: channel to complete transactions for
302 *
303 * Eventually submit queued descriptors if any
304 *
305 * Assume channel is idle while calling this function
306 * Called with atchan->lock held and bh disabled
307 */
308static void atc_complete_all(struct at_dma_chan *atchan)
309{
310 struct at_desc *desc, *_desc;
311 LIST_HEAD(list);
312
313 dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
314
315 BUG_ON(atc_chan_is_enabled(atchan));
316
317 /*
318 * Submit queued descriptors ASAP, i.e. before we go through
319 * the completed ones.
320 */
321 if (!list_empty(&atchan->queue))
322 atc_dostart(atchan, atc_first_queued(atchan));
323 /* empty active_list now it is completed */
324 list_splice_init(&atchan->active_list, &list);
325 /* empty queue list by moving descriptors (if any) to active_list */
326 list_splice_init(&atchan->queue, &atchan->active_list);
327
328 list_for_each_entry_safe(desc, _desc, &list, desc_node)
329 atc_chain_complete(atchan, desc);
330}
331
332/**
333 * atc_cleanup_descriptors - cleanup up finished descriptors in active_list
334 * @atchan: channel to be cleaned up
335 *
336 * Called with atchan->lock held and bh disabled
337 */
338static void atc_cleanup_descriptors(struct at_dma_chan *atchan)
339{
340 struct at_desc *desc, *_desc;
341 struct at_desc *child;
342
343 dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n");
344
345 list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
346 if (!(desc->lli.ctrla & ATC_DONE))
347 /* This one is currently in progress */
348 return;
349
Dan Williams285a3c72009-09-08 17:53:03 -0700350 list_for_each_entry(child, &desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200351 if (!(child->lli.ctrla & ATC_DONE))
352 /* Currently in progress */
353 return;
354
355 /*
356 * No descriptors so far seem to be in progress, i.e.
357 * this chain must be done.
358 */
359 atc_chain_complete(atchan, desc);
360 }
361}
362
363/**
364 * atc_advance_work - at the end of a transaction, move forward
365 * @atchan: channel where the transaction ended
366 *
367 * Called with atchan->lock held and bh disabled
368 */
369static void atc_advance_work(struct at_dma_chan *atchan)
370{
371 dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
372
373 if (list_empty(&atchan->active_list) ||
374 list_is_singular(&atchan->active_list)) {
375 atc_complete_all(atchan);
376 } else {
377 atc_chain_complete(atchan, atc_first_active(atchan));
378 /* advance work */
379 atc_dostart(atchan, atc_first_active(atchan));
380 }
381}
382
383
384/**
385 * atc_handle_error - handle errors reported by DMA controller
386 * @atchan: channel where error occurs
387 *
388 * Called with atchan->lock held and bh disabled
389 */
390static void atc_handle_error(struct at_dma_chan *atchan)
391{
392 struct at_desc *bad_desc;
393 struct at_desc *child;
394
395 /*
396 * The descriptor currently at the head of the active list is
397 * broked. Since we don't have any way to report errors, we'll
398 * just have to scream loudly and try to carry on.
399 */
400 bad_desc = atc_first_active(atchan);
401 list_del_init(&bad_desc->desc_node);
402
403 /* As we are stopped, take advantage to push queued descriptors
404 * in active_list */
405 list_splice_init(&atchan->queue, atchan->active_list.prev);
406
407 /* Try to restart the controller */
408 if (!list_empty(&atchan->active_list))
409 atc_dostart(atchan, atc_first_active(atchan));
410
411 /*
412 * KERN_CRITICAL may seem harsh, but since this only happens
413 * when someone submits a bad physical address in a
414 * descriptor, we should consider ourselves lucky that the
415 * controller flagged an error instead of scribbling over
416 * random memory locations.
417 */
418 dev_crit(chan2dev(&atchan->chan_common),
419 "Bad descriptor submitted for DMA!\n");
420 dev_crit(chan2dev(&atchan->chan_common),
421 " cookie: %d\n", bad_desc->txd.cookie);
422 atc_dump_lli(atchan, &bad_desc->lli);
Dan Williams285a3c72009-09-08 17:53:03 -0700423 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200424 atc_dump_lli(atchan, &child->lli);
425
426 /* Pretend the descriptor completed successfully */
427 atc_chain_complete(atchan, bad_desc);
428}
429
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200430/**
431 * atc_handle_cyclic - at the end of a period, run callback function
432 * @atchan: channel used for cyclic operations
433 *
434 * Called with atchan->lock held and bh disabled
435 */
436static void atc_handle_cyclic(struct at_dma_chan *atchan)
437{
438 struct at_desc *first = atc_first_active(atchan);
439 struct dma_async_tx_descriptor *txd = &first->txd;
440 dma_async_tx_callback callback = txd->callback;
441 void *param = txd->callback_param;
442
443 dev_vdbg(chan2dev(&atchan->chan_common),
444 "new cyclic period llp 0x%08x\n",
445 channel_readl(atchan, DSCR));
446
447 if (callback)
448 callback(param);
449}
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200450
451/*-- IRQ & Tasklet ---------------------------------------------------*/
452
453static void atc_tasklet(unsigned long data)
454{
455 struct at_dma_chan *atchan = (struct at_dma_chan *)data;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000456 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200457
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000458 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200459 if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200460 atc_handle_error(atchan);
Nicolas Ferre3c477482011-07-25 21:09:23 +0000461 else if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200462 atc_handle_cyclic(atchan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200463 else
464 atc_advance_work(atchan);
465
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000466 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200467}
468
469static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
470{
471 struct at_dma *atdma = (struct at_dma *)dev_id;
472 struct at_dma_chan *atchan;
473 int i;
474 u32 status, pending, imr;
475 int ret = IRQ_NONE;
476
477 do {
478 imr = dma_readl(atdma, EBCIMR);
479 status = dma_readl(atdma, EBCISR);
480 pending = status & imr;
481
482 if (!pending)
483 break;
484
485 dev_vdbg(atdma->dma_common.dev,
486 "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
487 status, imr, pending);
488
489 for (i = 0; i < atdma->dma_common.chancnt; i++) {
490 atchan = &atdma->chan[i];
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200491 if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200492 if (pending & AT_DMA_ERR(i)) {
493 /* Disable channel on AHB error */
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200494 dma_writel(atdma, CHDR,
495 AT_DMA_RES(i) | atchan->mask);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200496 /* Give information to tasklet */
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200497 set_bit(ATC_IS_ERROR, &atchan->status);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200498 }
499 tasklet_schedule(&atchan->tasklet);
500 ret = IRQ_HANDLED;
501 }
502 }
503
504 } while (pending);
505
506 return ret;
507}
508
509
510/*-- DMA Engine API --------------------------------------------------*/
511
512/**
513 * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
514 * @desc: descriptor at the head of the transaction chain
515 *
516 * Queue chain if DMA engine is working already
517 *
518 * Cookie increment and adding to active_list or queue must be atomic
519 */
520static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
521{
522 struct at_desc *desc = txd_to_at_desc(tx);
523 struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
524 dma_cookie_t cookie;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000525 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200526
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000527 spin_lock_irqsave(&atchan->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000528 cookie = dma_cookie_assign(tx);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200529
530 if (list_empty(&atchan->active_list)) {
531 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
532 desc->txd.cookie);
533 atc_dostart(atchan, desc);
534 list_add_tail(&desc->desc_node, &atchan->active_list);
535 } else {
536 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
537 desc->txd.cookie);
538 list_add_tail(&desc->desc_node, &atchan->queue);
539 }
540
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000541 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200542
543 return cookie;
544}
545
546/**
547 * atc_prep_dma_memcpy - prepare a memcpy operation
548 * @chan: the channel to prepare operation on
549 * @dest: operation virtual destination address
550 * @src: operation virtual source address
551 * @len: operation length
552 * @flags: tx descriptor status flags
553 */
554static struct dma_async_tx_descriptor *
555atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
556 size_t len, unsigned long flags)
557{
558 struct at_dma_chan *atchan = to_at_dma_chan(chan);
559 struct at_desc *desc = NULL;
560 struct at_desc *first = NULL;
561 struct at_desc *prev = NULL;
562 size_t xfer_count;
563 size_t offset;
564 unsigned int src_width;
565 unsigned int dst_width;
566 u32 ctrla;
567 u32 ctrlb;
568
569 dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
570 dest, src, len, flags);
571
572 if (unlikely(!len)) {
573 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
574 return NULL;
575 }
576
577 ctrla = ATC_DEFAULT_CTRLA;
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200578 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200579 | ATC_SRC_ADDR_MODE_INCR
580 | ATC_DST_ADDR_MODE_INCR
581 | ATC_FC_MEM2MEM;
582
583 /*
584 * We can be a lot more clever here, but this should take care
585 * of the most common optimization.
586 */
587 if (!((src | dest | len) & 3)) {
588 ctrla |= ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
589 src_width = dst_width = 2;
590 } else if (!((src | dest | len) & 1)) {
591 ctrla |= ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
592 src_width = dst_width = 1;
593 } else {
594 ctrla |= ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
595 src_width = dst_width = 0;
596 }
597
598 for (offset = 0; offset < len; offset += xfer_count << src_width) {
599 xfer_count = min_t(size_t, (len - offset) >> src_width,
600 ATC_BTSIZE_MAX);
601
602 desc = atc_desc_get(atchan);
603 if (!desc)
604 goto err_desc_get;
605
606 desc->lli.saddr = src + offset;
607 desc->lli.daddr = dest + offset;
608 desc->lli.ctrla = ctrla | xfer_count;
609 desc->lli.ctrlb = ctrlb;
610
611 desc->txd.cookie = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200612
Nicolas Ferree257e152011-05-06 19:56:53 +0200613 atc_desc_chain(&first, &prev, desc);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200614 }
615
616 /* First descriptor of the chain embedds additional information */
617 first->txd.cookie = -EBUSY;
618 first->len = len;
619
620 /* set end-of-link to the last link descriptor of list*/
621 set_desc_eol(desc);
622
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100623 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200624
625 return &first->txd;
626
627err_desc_get:
628 atc_desc_put(atchan, first);
629 return NULL;
630}
631
Nicolas Ferre808347f2009-07-22 20:04:45 +0200632
633/**
634 * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
635 * @chan: DMA channel
636 * @sgl: scatterlist to transfer to/from
637 * @sg_len: number of entries in @scatterlist
638 * @direction: DMA direction
639 * @flags: tx descriptor status flags
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500640 * @context: transaction context (ignored)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200641 */
642static struct dma_async_tx_descriptor *
643atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530644 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500645 unsigned long flags, void *context)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200646{
647 struct at_dma_chan *atchan = to_at_dma_chan(chan);
648 struct at_dma_slave *atslave = chan->private;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100649 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200650 struct at_desc *first = NULL;
651 struct at_desc *prev = NULL;
652 u32 ctrla;
653 u32 ctrlb;
654 dma_addr_t reg;
655 unsigned int reg_width;
656 unsigned int mem_width;
657 unsigned int i;
658 struct scatterlist *sg;
659 size_t total_len = 0;
660
Nicolas Ferrecc52a102011-04-30 16:57:47 +0200661 dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
662 sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530663 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre808347f2009-07-22 20:04:45 +0200664 flags);
665
666 if (unlikely(!atslave || !sg_len)) {
Nicolas Ferre643fc242012-09-11 17:21:44 +0200667 dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n");
Nicolas Ferre808347f2009-07-22 20:04:45 +0200668 return NULL;
669 }
670
Nicolas Ferre808347f2009-07-22 20:04:45 +0200671 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200672 ctrlb = ATC_IEN;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200673
674 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530675 case DMA_MEM_TO_DEV:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100676 reg_width = convert_buswidth(sconfig->dst_addr_width);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200677 ctrla |= ATC_DST_WIDTH(reg_width);
678 ctrlb |= ATC_DST_ADDR_MODE_FIXED
679 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200680 | ATC_FC_MEM2PER
681 | ATC_SIF(AT_DMA_MEM_IF) | ATC_DIF(AT_DMA_PER_IF);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100682 reg = sconfig->dst_addr;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200683 for_each_sg(sgl, sg, sg_len, i) {
684 struct at_desc *desc;
685 u32 len;
686 u32 mem;
687
688 desc = atc_desc_get(atchan);
689 if (!desc)
690 goto err_desc_get;
691
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100692 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200693 len = sg_dma_len(sg);
Nicolas Ferred7b24352012-09-11 17:21:45 +0200694 if (unlikely(!len)) {
695 dev_dbg(chan2dev(chan),
696 "prep_slave_sg: sg(%d) data length is zero\n", i);
697 goto err;
698 }
Nicolas Ferre808347f2009-07-22 20:04:45 +0200699 mem_width = 2;
700 if (unlikely(mem & 3 || len & 3))
701 mem_width = 0;
702
703 desc->lli.saddr = mem;
704 desc->lli.daddr = reg;
705 desc->lli.ctrla = ctrla
706 | ATC_SRC_WIDTH(mem_width)
707 | len >> mem_width;
708 desc->lli.ctrlb = ctrlb;
709
Nicolas Ferree257e152011-05-06 19:56:53 +0200710 atc_desc_chain(&first, &prev, desc);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200711 total_len += len;
712 }
713 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530714 case DMA_DEV_TO_MEM:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100715 reg_width = convert_buswidth(sconfig->src_addr_width);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200716 ctrla |= ATC_SRC_WIDTH(reg_width);
717 ctrlb |= ATC_DST_ADDR_MODE_INCR
718 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200719 | ATC_FC_PER2MEM
720 | ATC_SIF(AT_DMA_PER_IF) | ATC_DIF(AT_DMA_MEM_IF);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200721
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100722 reg = sconfig->src_addr;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200723 for_each_sg(sgl, sg, sg_len, i) {
724 struct at_desc *desc;
725 u32 len;
726 u32 mem;
727
728 desc = atc_desc_get(atchan);
729 if (!desc)
730 goto err_desc_get;
731
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100732 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200733 len = sg_dma_len(sg);
Nicolas Ferred7b24352012-09-11 17:21:45 +0200734 if (unlikely(!len)) {
735 dev_dbg(chan2dev(chan),
736 "prep_slave_sg: sg(%d) data length is zero\n", i);
737 goto err;
738 }
Nicolas Ferre808347f2009-07-22 20:04:45 +0200739 mem_width = 2;
740 if (unlikely(mem & 3 || len & 3))
741 mem_width = 0;
742
743 desc->lli.saddr = reg;
744 desc->lli.daddr = mem;
745 desc->lli.ctrla = ctrla
746 | ATC_DST_WIDTH(mem_width)
Nicolas Ferre59a609d2010-12-13 13:48:41 +0100747 | len >> reg_width;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200748 desc->lli.ctrlb = ctrlb;
749
Nicolas Ferree257e152011-05-06 19:56:53 +0200750 atc_desc_chain(&first, &prev, desc);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200751 total_len += len;
752 }
753 break;
754 default:
755 return NULL;
756 }
757
758 /* set end-of-link to the last link descriptor of list*/
759 set_desc_eol(prev);
760
761 /* First descriptor of the chain embedds additional information */
762 first->txd.cookie = -EBUSY;
763 first->len = total_len;
764
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100765 /* first link descriptor of list is responsible of flags */
766 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferre808347f2009-07-22 20:04:45 +0200767
768 return &first->txd;
769
770err_desc_get:
771 dev_err(chan2dev(chan), "not enough descriptors available\n");
Nicolas Ferred7b24352012-09-11 17:21:45 +0200772err:
Nicolas Ferre808347f2009-07-22 20:04:45 +0200773 atc_desc_put(atchan, first);
774 return NULL;
775}
776
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200777/**
778 * atc_dma_cyclic_check_values
779 * Check for too big/unaligned periods and unaligned DMA buffer
780 */
781static int
782atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530783 size_t period_len, enum dma_transfer_direction direction)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200784{
785 if (period_len > (ATC_BTSIZE_MAX << reg_width))
786 goto err_out;
787 if (unlikely(period_len & ((1 << reg_width) - 1)))
788 goto err_out;
789 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
790 goto err_out;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530791 if (unlikely(!(direction & (DMA_DEV_TO_MEM | DMA_MEM_TO_DEV))))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200792 goto err_out;
793
794 return 0;
795
796err_out:
797 return -EINVAL;
798}
799
800/**
801 * atc_dma_cyclic_fill_desc - Fill one period decriptor
802 */
803static int
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100804atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200805 unsigned int period_index, dma_addr_t buf_addr,
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100806 unsigned int reg_width, size_t period_len,
807 enum dma_transfer_direction direction)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200808{
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100809 struct at_dma_chan *atchan = to_at_dma_chan(chan);
810 struct at_dma_slave *atslave = chan->private;
811 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
812 u32 ctrla;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200813
814 /* prepare common CRTLA value */
815 ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla
816 | ATC_DST_WIDTH(reg_width)
817 | ATC_SRC_WIDTH(reg_width)
818 | period_len >> reg_width;
819
820 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530821 case DMA_MEM_TO_DEV:
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200822 desc->lli.saddr = buf_addr + (period_len * period_index);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100823 desc->lli.daddr = sconfig->dst_addr;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200824 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200825 desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200826 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200827 | ATC_FC_MEM2PER
828 | ATC_SIF(AT_DMA_MEM_IF)
829 | ATC_DIF(AT_DMA_PER_IF);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200830 break;
831
Vinod Kouldb8196d2011-10-13 22:34:23 +0530832 case DMA_DEV_TO_MEM:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100833 desc->lli.saddr = sconfig->src_addr;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200834 desc->lli.daddr = buf_addr + (period_len * period_index);
835 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200836 desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200837 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200838 | ATC_FC_PER2MEM
839 | ATC_SIF(AT_DMA_PER_IF)
840 | ATC_DIF(AT_DMA_MEM_IF);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200841 break;
842
843 default:
844 return -EINVAL;
845 }
846
847 return 0;
848}
849
850/**
851 * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
852 * @chan: the DMA channel to prepare
853 * @buf_addr: physical DMA address where the buffer starts
854 * @buf_len: total number of bytes for the entire buffer
855 * @period_len: number of bytes for each period
856 * @direction: transfer direction, to or from device
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500857 * @context: transfer context (ignored)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200858 */
859static struct dma_async_tx_descriptor *
860atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500861 size_t period_len, enum dma_transfer_direction direction,
862 void *context)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200863{
864 struct at_dma_chan *atchan = to_at_dma_chan(chan);
865 struct at_dma_slave *atslave = chan->private;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100866 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200867 struct at_desc *first = NULL;
868 struct at_desc *prev = NULL;
869 unsigned long was_cyclic;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100870 unsigned int reg_width;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200871 unsigned int periods = buf_len / period_len;
872 unsigned int i;
873
874 dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
Vinod Kouldb8196d2011-10-13 22:34:23 +0530875 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200876 buf_addr,
877 periods, buf_len, period_len);
878
879 if (unlikely(!atslave || !buf_len || !period_len)) {
880 dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
881 return NULL;
882 }
883
884 was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
885 if (was_cyclic) {
886 dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
887 return NULL;
888 }
889
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100890 if (sconfig->direction == DMA_MEM_TO_DEV)
891 reg_width = convert_buswidth(sconfig->dst_addr_width);
892 else
893 reg_width = convert_buswidth(sconfig->src_addr_width);
894
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200895 /* Check for too big/unaligned periods and unaligned DMA buffer */
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100896 if (atc_dma_cyclic_check_values(reg_width, buf_addr,
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200897 period_len, direction))
898 goto err_out;
899
900 /* build cyclic linked list */
901 for (i = 0; i < periods; i++) {
902 struct at_desc *desc;
903
904 desc = atc_desc_get(atchan);
905 if (!desc)
906 goto err_desc_get;
907
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100908 if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
909 reg_width, period_len, direction))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200910 goto err_desc_get;
911
912 atc_desc_chain(&first, &prev, desc);
913 }
914
915 /* lets make a cyclic list */
916 prev->lli.dscr = first->txd.phys;
917
918 /* First descriptor of the chain embedds additional information */
919 first->txd.cookie = -EBUSY;
920 first->len = buf_len;
921
922 return &first->txd;
923
924err_desc_get:
925 dev_err(chan2dev(chan), "not enough descriptors available\n");
926 atc_desc_put(atchan, first);
927err_out:
928 clear_bit(ATC_IS_CYCLIC, &atchan->status);
929 return NULL;
930}
931
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100932static int set_runtime_config(struct dma_chan *chan,
933 struct dma_slave_config *sconfig)
934{
935 struct at_dma_chan *atchan = to_at_dma_chan(chan);
936
937 /* Check if it is chan is configured for slave transfers */
938 if (!chan->private)
939 return -EINVAL;
940
941 memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
942
943 convert_burst(&atchan->dma_sconfig.src_maxburst);
944 convert_burst(&atchan->dma_sconfig.dst_maxburst);
945
946 return 0;
947}
948
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200949
Linus Walleij05827632010-05-17 16:30:42 -0700950static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
951 unsigned long arg)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200952{
953 struct at_dma_chan *atchan = to_at_dma_chan(chan);
954 struct at_dma *atdma = to_at_dma(chan->device);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200955 int chan_id = atchan->chan_common.chan_id;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000956 unsigned long flags;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200957
Nicolas Ferre808347f2009-07-22 20:04:45 +0200958 LIST_HEAD(list);
959
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200960 dev_vdbg(chan2dev(chan), "atc_control (%d)\n", cmd);
961
962 if (cmd == DMA_PAUSE) {
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000963 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200964
965 dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200966 set_bit(ATC_IS_PAUSED, &atchan->status);
967
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000968 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200969 } else if (cmd == DMA_RESUME) {
Nicolas Ferre3c477482011-07-25 21:09:23 +0000970 if (!atc_chan_is_paused(atchan))
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200971 return 0;
972
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000973 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200974
975 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
976 clear_bit(ATC_IS_PAUSED, &atchan->status);
977
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000978 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200979 } else if (cmd == DMA_TERMINATE_ALL) {
980 struct at_desc *desc, *_desc;
981 /*
982 * This is only called when something went wrong elsewhere, so
983 * we don't really care about the data. Just disable the
984 * channel. We still have to poll the channel enable bit due
985 * to AHB/HSB limitations.
986 */
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000987 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200988
989 /* disabling channel: must also remove suspend state */
990 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
991
992 /* confirm that this channel is disabled */
993 while (dma_readl(atdma, CHSR) & atchan->mask)
994 cpu_relax();
995
996 /* active_list entries will end up before queued entries */
997 list_splice_init(&atchan->queue, &list);
998 list_splice_init(&atchan->active_list, &list);
999
1000 /* Flush all pending and queued descriptors */
1001 list_for_each_entry_safe(desc, _desc, &list, desc_node)
1002 atc_chain_complete(atchan, desc);
1003
1004 clear_bit(ATC_IS_PAUSED, &atchan->status);
1005 /* if channel dedicated to cyclic operations, free it */
1006 clear_bit(ATC_IS_CYCLIC, &atchan->status);
1007
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001008 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001009 } else if (cmd == DMA_SLAVE_CONFIG) {
1010 return set_runtime_config(chan, (struct dma_slave_config *)arg);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001011 } else {
Linus Walleijc3635c72010-03-26 16:44:01 -07001012 return -ENXIO;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001013 }
Yong Wangb0ebeb92010-08-05 10:40:08 +08001014
Linus Walleijc3635c72010-03-26 16:44:01 -07001015 return 0;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001016}
1017
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001018/**
Linus Walleij07934482010-03-26 16:50:49 -07001019 * atc_tx_status - poll for transaction completion
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001020 * @chan: DMA channel
1021 * @cookie: transaction identifier to check status of
Linus Walleij07934482010-03-26 16:50:49 -07001022 * @txstate: if not %NULL updated with transaction state
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001023 *
Linus Walleij07934482010-03-26 16:50:49 -07001024 * If @txstate is passed in, upon return it reflect the driver
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001025 * internal state and can be used with dma_async_is_complete() to check
1026 * the status of multiple cookies without re-checking hardware state.
1027 */
1028static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -07001029atc_tx_status(struct dma_chan *chan,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001030 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -07001031 struct dma_tx_state *txstate)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001032{
1033 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1034 dma_cookie_t last_used;
1035 dma_cookie_t last_complete;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001036 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001037 enum dma_status ret;
1038
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001039 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001040
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001041 ret = dma_cookie_status(chan, cookie, txstate);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001042 if (ret != DMA_SUCCESS) {
1043 atc_cleanup_descriptors(atchan);
1044
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001045 ret = dma_cookie_status(chan, cookie, txstate);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001046 }
1047
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001048 last_complete = chan->completed_cookie;
1049 last_used = chan->cookie;
1050
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001051 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001052
Nicolas Ferre543aabc2011-05-06 19:56:51 +02001053 if (ret != DMA_SUCCESS)
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001054 dma_set_residue(txstate, atc_first_active(atchan)->len);
Nicolas Ferre543aabc2011-05-06 19:56:51 +02001055
Nicolas Ferre3c477482011-07-25 21:09:23 +00001056 if (atc_chan_is_paused(atchan))
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001057 ret = DMA_PAUSED;
1058
1059 dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d (d%d, u%d)\n",
1060 ret, cookie, last_complete ? last_complete : 0,
Linus Walleij07934482010-03-26 16:50:49 -07001061 last_used ? last_used : 0);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001062
1063 return ret;
1064}
1065
1066/**
1067 * atc_issue_pending - try to finish work
1068 * @chan: target DMA channel
1069 */
1070static void atc_issue_pending(struct dma_chan *chan)
1071{
1072 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001073 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001074
1075 dev_vdbg(chan2dev(chan), "issue_pending\n");
1076
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001077 /* Not needed for cyclic transfers */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001078 if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001079 return;
1080
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001081 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001082 if (!atc_chan_is_enabled(atchan)) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001083 atc_advance_work(atchan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001084 }
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001085 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001086}
1087
1088/**
1089 * atc_alloc_chan_resources - allocate resources for DMA channel
1090 * @chan: allocate descriptor resources for this channel
1091 * @client: current client requesting the channel be ready for requests
1092 *
1093 * return - the number of allocated descriptors
1094 */
1095static int atc_alloc_chan_resources(struct dma_chan *chan)
1096{
1097 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1098 struct at_dma *atdma = to_at_dma(chan->device);
1099 struct at_desc *desc;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001100 struct at_dma_slave *atslave;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001101 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001102 int i;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001103 u32 cfg;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001104 LIST_HEAD(tmp_list);
1105
1106 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
1107
1108 /* ASSERT: channel is idle */
1109 if (atc_chan_is_enabled(atchan)) {
1110 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
1111 return -EIO;
1112 }
1113
Nicolas Ferre808347f2009-07-22 20:04:45 +02001114 cfg = ATC_DEFAULT_CFG;
1115
1116 atslave = chan->private;
1117 if (atslave) {
1118 /*
1119 * We need controller-specific data to set up slave
1120 * transfers.
1121 */
1122 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
1123
1124 /* if cfg configuration specified take it instad of default */
1125 if (atslave->cfg)
1126 cfg = atslave->cfg;
1127 }
1128
1129 /* have we already been set up?
1130 * reconfigure channel but no need to reallocate descriptors */
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001131 if (!list_empty(&atchan->free_list))
1132 return atchan->descs_allocated;
1133
1134 /* Allocate initial pool of descriptors */
1135 for (i = 0; i < init_nr_desc_per_channel; i++) {
1136 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
1137 if (!desc) {
1138 dev_err(atdma->dma_common.dev,
1139 "Only %d initial descriptors\n", i);
1140 break;
1141 }
1142 list_add_tail(&desc->desc_node, &tmp_list);
1143 }
1144
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001145 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001146 atchan->descs_allocated = i;
1147 list_splice(&tmp_list, &atchan->free_list);
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001148 dma_cookie_init(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001149 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001150
1151 /* channel parameters */
Nicolas Ferre808347f2009-07-22 20:04:45 +02001152 channel_writel(atchan, CFG, cfg);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001153
1154 dev_dbg(chan2dev(chan),
1155 "alloc_chan_resources: allocated %d descriptors\n",
1156 atchan->descs_allocated);
1157
1158 return atchan->descs_allocated;
1159}
1160
1161/**
1162 * atc_free_chan_resources - free all channel resources
1163 * @chan: DMA channel
1164 */
1165static void atc_free_chan_resources(struct dma_chan *chan)
1166{
1167 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1168 struct at_dma *atdma = to_at_dma(chan->device);
1169 struct at_desc *desc, *_desc;
1170 LIST_HEAD(list);
1171
1172 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
1173 atchan->descs_allocated);
1174
1175 /* ASSERT: channel is idle */
1176 BUG_ON(!list_empty(&atchan->active_list));
1177 BUG_ON(!list_empty(&atchan->queue));
1178 BUG_ON(atc_chan_is_enabled(atchan));
1179
1180 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
1181 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1182 list_del(&desc->desc_node);
1183 /* free link descriptor */
1184 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
1185 }
1186 list_splice_init(&atchan->free_list, &list);
1187 atchan->descs_allocated = 0;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001188 atchan->status = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001189
1190 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
1191}
1192
1193
1194/*-- Module Management -----------------------------------------------*/
1195
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001196/* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
1197static struct at_dma_platform_data at91sam9rl_config = {
1198 .nr_channels = 2,
1199};
1200static struct at_dma_platform_data at91sam9g45_config = {
1201 .nr_channels = 8,
1202};
1203
Nicolas Ferrec5115952011-10-17 14:56:41 +02001204#if defined(CONFIG_OF)
1205static const struct of_device_id atmel_dma_dt_ids[] = {
1206 {
1207 .compatible = "atmel,at91sam9rl-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001208 .data = &at91sam9rl_config,
Nicolas Ferrec5115952011-10-17 14:56:41 +02001209 }, {
1210 .compatible = "atmel,at91sam9g45-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001211 .data = &at91sam9g45_config,
Nicolas Ferredcc81732011-11-22 11:55:53 +01001212 }, {
1213 /* sentinel */
1214 }
Nicolas Ferrec5115952011-10-17 14:56:41 +02001215};
1216
1217MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
1218#endif
1219
Nicolas Ferre0ab88a02011-11-22 11:55:52 +01001220static const struct platform_device_id atdma_devtypes[] = {
Nicolas Ferre67348452011-10-17 14:56:40 +02001221 {
1222 .name = "at91sam9rl_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001223 .driver_data = (unsigned long) &at91sam9rl_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001224 }, {
1225 .name = "at91sam9g45_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001226 .driver_data = (unsigned long) &at91sam9g45_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001227 }, {
1228 /* sentinel */
1229 }
1230};
1231
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001232static inline struct at_dma_platform_data * __init at_dma_get_driver_data(
1233 struct platform_device *pdev)
Nicolas Ferrec5115952011-10-17 14:56:41 +02001234{
1235 if (pdev->dev.of_node) {
1236 const struct of_device_id *match;
1237 match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
1238 if (match == NULL)
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001239 return NULL;
1240 return match->data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001241 }
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001242 return (struct at_dma_platform_data *)
1243 platform_get_device_id(pdev)->driver_data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001244}
1245
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001246/**
1247 * at_dma_off - disable DMA controller
1248 * @atdma: the Atmel HDAMC device
1249 */
1250static void at_dma_off(struct at_dma *atdma)
1251{
1252 dma_writel(atdma, EN, 0);
1253
1254 /* disable all interrupts */
1255 dma_writel(atdma, EBCIDR, -1L);
1256
1257 /* confirm that all channels are disabled */
1258 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
1259 cpu_relax();
1260}
1261
1262static int __init at_dma_probe(struct platform_device *pdev)
1263{
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001264 struct resource *io;
1265 struct at_dma *atdma;
1266 size_t size;
1267 int irq;
1268 int err;
1269 int i;
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001270 struct at_dma_platform_data *plat_dat;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001271
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001272 /* setup platform data for each SoC */
1273 dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
1274 dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
1275 dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
Nicolas Ferre67348452011-10-17 14:56:40 +02001276
1277 /* get DMA parameters from controller type */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001278 plat_dat = at_dma_get_driver_data(pdev);
1279 if (!plat_dat)
1280 return -ENODEV;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001281
1282 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1283 if (!io)
1284 return -EINVAL;
1285
1286 irq = platform_get_irq(pdev, 0);
1287 if (irq < 0)
1288 return irq;
1289
1290 size = sizeof(struct at_dma);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001291 size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001292 atdma = kzalloc(size, GFP_KERNEL);
1293 if (!atdma)
1294 return -ENOMEM;
1295
Nicolas Ferre67348452011-10-17 14:56:40 +02001296 /* discover transaction capabilities */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001297 atdma->dma_common.cap_mask = plat_dat->cap_mask;
1298 atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001299
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07001300 size = resource_size(io);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001301 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1302 err = -EBUSY;
1303 goto err_kfree;
1304 }
1305
1306 atdma->regs = ioremap(io->start, size);
1307 if (!atdma->regs) {
1308 err = -ENOMEM;
1309 goto err_release_r;
1310 }
1311
1312 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1313 if (IS_ERR(atdma->clk)) {
1314 err = PTR_ERR(atdma->clk);
1315 goto err_clk;
1316 }
1317 clk_enable(atdma->clk);
1318
1319 /* force dma off, just in case */
1320 at_dma_off(atdma);
1321
1322 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1323 if (err)
1324 goto err_irq;
1325
1326 platform_set_drvdata(pdev, atdma);
1327
1328 /* create a pool of consistent memory blocks for hardware descriptors */
1329 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1330 &pdev->dev, sizeof(struct at_desc),
1331 4 /* word alignment */, 0);
1332 if (!atdma->dma_desc_pool) {
1333 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1334 err = -ENOMEM;
1335 goto err_pool_create;
1336 }
1337
1338 /* clear any pending interrupt */
1339 while (dma_readl(atdma, EBCISR))
1340 cpu_relax();
1341
1342 /* initialize channels related values */
1343 INIT_LIST_HEAD(&atdma->dma_common.channels);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001344 for (i = 0; i < plat_dat->nr_channels; i++) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001345 struct at_dma_chan *atchan = &atdma->chan[i];
1346
1347 atchan->chan_common.device = &atdma->dma_common;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001348 dma_cookie_init(&atchan->chan_common);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001349 list_add_tail(&atchan->chan_common.device_node,
1350 &atdma->dma_common.channels);
1351
1352 atchan->ch_regs = atdma->regs + ch_regs(i);
1353 spin_lock_init(&atchan->lock);
1354 atchan->mask = 1 << i;
1355
1356 INIT_LIST_HEAD(&atchan->active_list);
1357 INIT_LIST_HEAD(&atchan->queue);
1358 INIT_LIST_HEAD(&atchan->free_list);
1359
1360 tasklet_init(&atchan->tasklet, atc_tasklet,
1361 (unsigned long)atchan);
Nikolaus Vossbda3a472012-01-17 10:28:33 +01001362 atc_enable_chan_irq(atdma, i);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001363 }
1364
1365 /* set base routines */
1366 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1367 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
Linus Walleij07934482010-03-26 16:50:49 -07001368 atdma->dma_common.device_tx_status = atc_tx_status;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001369 atdma->dma_common.device_issue_pending = atc_issue_pending;
1370 atdma->dma_common.dev = &pdev->dev;
1371
1372 /* set prep routines based on capability */
1373 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1374 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1375
Nicolas Ferred7db8082011-08-05 11:43:44 +00001376 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
Nicolas Ferre808347f2009-07-22 20:04:45 +02001377 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001378 /* controller can do slave DMA: can trigger cyclic transfers */
1379 dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001380 atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
Linus Walleijc3635c72010-03-26 16:44:01 -07001381 atdma->dma_common.device_control = atc_control;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001382 }
Nicolas Ferre808347f2009-07-22 20:04:45 +02001383
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001384 dma_writel(atdma, EN, AT_DMA_ENABLE);
1385
1386 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
1387 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1388 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001389 plat_dat->nr_channels);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001390
1391 dma_async_device_register(&atdma->dma_common);
1392
1393 return 0;
1394
1395err_pool_create:
1396 platform_set_drvdata(pdev, NULL);
1397 free_irq(platform_get_irq(pdev, 0), atdma);
1398err_irq:
1399 clk_disable(atdma->clk);
1400 clk_put(atdma->clk);
1401err_clk:
1402 iounmap(atdma->regs);
1403 atdma->regs = NULL;
1404err_release_r:
1405 release_mem_region(io->start, size);
1406err_kfree:
1407 kfree(atdma);
1408 return err;
1409}
1410
1411static int __exit at_dma_remove(struct platform_device *pdev)
1412{
1413 struct at_dma *atdma = platform_get_drvdata(pdev);
1414 struct dma_chan *chan, *_chan;
1415 struct resource *io;
1416
1417 at_dma_off(atdma);
1418 dma_async_device_unregister(&atdma->dma_common);
1419
1420 dma_pool_destroy(atdma->dma_desc_pool);
1421 platform_set_drvdata(pdev, NULL);
1422 free_irq(platform_get_irq(pdev, 0), atdma);
1423
1424 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1425 device_node) {
1426 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1427
1428 /* Disable interrupts */
Nikolaus Vossbda3a472012-01-17 10:28:33 +01001429 atc_disable_chan_irq(atdma, chan->chan_id);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001430 tasklet_disable(&atchan->tasklet);
1431
1432 tasklet_kill(&atchan->tasklet);
1433 list_del(&chan->device_node);
1434 }
1435
1436 clk_disable(atdma->clk);
1437 clk_put(atdma->clk);
1438
1439 iounmap(atdma->regs);
1440 atdma->regs = NULL;
1441
1442 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07001443 release_mem_region(io->start, resource_size(io));
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001444
1445 kfree(atdma);
1446
1447 return 0;
1448}
1449
1450static void at_dma_shutdown(struct platform_device *pdev)
1451{
1452 struct at_dma *atdma = platform_get_drvdata(pdev);
1453
1454 at_dma_off(platform_get_drvdata(pdev));
1455 clk_disable(atdma->clk);
1456}
1457
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001458static int at_dma_prepare(struct device *dev)
1459{
1460 struct platform_device *pdev = to_platform_device(dev);
1461 struct at_dma *atdma = platform_get_drvdata(pdev);
1462 struct dma_chan *chan, *_chan;
1463
1464 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1465 device_node) {
1466 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1467 /* wait for transaction completion (except in cyclic case) */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001468 if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001469 return -EAGAIN;
1470 }
1471 return 0;
1472}
1473
1474static void atc_suspend_cyclic(struct at_dma_chan *atchan)
1475{
1476 struct dma_chan *chan = &atchan->chan_common;
1477
1478 /* Channel should be paused by user
1479 * do it anyway even if it is not done already */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001480 if (!atc_chan_is_paused(atchan)) {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001481 dev_warn(chan2dev(chan),
1482 "cyclic channel not paused, should be done by channel user\n");
1483 atc_control(chan, DMA_PAUSE, 0);
1484 }
1485
1486 /* now preserve additional data for cyclic operations */
1487 /* next descriptor address in the cyclic list */
1488 atchan->save_dscr = channel_readl(atchan, DSCR);
1489
1490 vdbg_dump_regs(atchan);
1491}
1492
Dan Williams33f82d12009-09-10 00:06:44 +02001493static int at_dma_suspend_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001494{
Dan Williams33f82d12009-09-10 00:06:44 +02001495 struct platform_device *pdev = to_platform_device(dev);
1496 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001497 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001498
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001499 /* preserve data */
1500 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1501 device_node) {
1502 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1503
Nicolas Ferre3c477482011-07-25 21:09:23 +00001504 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001505 atc_suspend_cyclic(atchan);
1506 atchan->save_cfg = channel_readl(atchan, CFG);
1507 }
1508 atdma->save_imr = dma_readl(atdma, EBCIMR);
1509
1510 /* disable DMA controller */
1511 at_dma_off(atdma);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001512 clk_disable(atdma->clk);
1513 return 0;
1514}
1515
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001516static void atc_resume_cyclic(struct at_dma_chan *atchan)
1517{
1518 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
1519
1520 /* restore channel status for cyclic descriptors list:
1521 * next descriptor in the cyclic list at the time of suspend */
1522 channel_writel(atchan, SADDR, 0);
1523 channel_writel(atchan, DADDR, 0);
1524 channel_writel(atchan, CTRLA, 0);
1525 channel_writel(atchan, CTRLB, 0);
1526 channel_writel(atchan, DSCR, atchan->save_dscr);
1527 dma_writel(atdma, CHER, atchan->mask);
1528
1529 /* channel pause status should be removed by channel user
1530 * We cannot take the initiative to do it here */
1531
1532 vdbg_dump_regs(atchan);
1533}
1534
Dan Williams33f82d12009-09-10 00:06:44 +02001535static int at_dma_resume_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001536{
Dan Williams33f82d12009-09-10 00:06:44 +02001537 struct platform_device *pdev = to_platform_device(dev);
1538 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001539 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001540
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001541 /* bring back DMA controller */
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001542 clk_enable(atdma->clk);
1543 dma_writel(atdma, EN, AT_DMA_ENABLE);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001544
1545 /* clear any pending interrupt */
1546 while (dma_readl(atdma, EBCISR))
1547 cpu_relax();
1548
1549 /* restore saved data */
1550 dma_writel(atdma, EBCIER, atdma->save_imr);
1551 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1552 device_node) {
1553 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1554
1555 channel_writel(atchan, CFG, atchan->save_cfg);
Nicolas Ferre3c477482011-07-25 21:09:23 +00001556 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001557 atc_resume_cyclic(atchan);
1558 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001559 return 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001560}
1561
Alexey Dobriyan47145212009-12-14 18:00:08 -08001562static const struct dev_pm_ops at_dma_dev_pm_ops = {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001563 .prepare = at_dma_prepare,
Dan Williams33f82d12009-09-10 00:06:44 +02001564 .suspend_noirq = at_dma_suspend_noirq,
1565 .resume_noirq = at_dma_resume_noirq,
1566};
1567
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001568static struct platform_driver at_dma_driver = {
1569 .remove = __exit_p(at_dma_remove),
1570 .shutdown = at_dma_shutdown,
Nicolas Ferre67348452011-10-17 14:56:40 +02001571 .id_table = atdma_devtypes,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001572 .driver = {
1573 .name = "at_hdmac",
Dan Williams33f82d12009-09-10 00:06:44 +02001574 .pm = &at_dma_dev_pm_ops,
Nicolas Ferrec5115952011-10-17 14:56:41 +02001575 .of_match_table = of_match_ptr(atmel_dma_dt_ids),
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001576 },
1577};
1578
1579static int __init at_dma_init(void)
1580{
1581 return platform_driver_probe(&at_dma_driver, at_dma_probe);
1582}
Eric Xu93d0bec2011-01-12 15:39:08 +01001583subsys_initcall(at_dma_init);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001584
1585static void __exit at_dma_exit(void)
1586{
1587 platform_driver_unregister(&at_dma_driver);
1588}
1589module_exit(at_dma_exit);
1590
1591MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1592MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1593MODULE_LICENSE("GPL");
1594MODULE_ALIAS("platform:at_hdmac");