blob: 7292aa87b2dd3fb54a4797f7c676d74a6a047713 [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)
Nicolas Ferreae14d4b2011-04-30 16:57:49 +020042#define ATC_DEFAULT_CTRLB (ATC_SIF(AT_DMA_MEM_IF) \
43 |ATC_DIF(AT_DMA_MEM_IF))
Nicolas Ferredc78baa2009-07-03 19:24:33 +020044
45/*
46 * Initial number of descriptors to allocate for each channel. This could
47 * be increased during dma usage.
48 */
49static unsigned int init_nr_desc_per_channel = 64;
50module_param(init_nr_desc_per_channel, uint, 0644);
51MODULE_PARM_DESC(init_nr_desc_per_channel,
52 "initial descriptors per channel (default: 64)");
53
54
55/* prototypes */
56static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx);
57
58
59/*----------------------------------------------------------------------*/
60
61static struct at_desc *atc_first_active(struct at_dma_chan *atchan)
62{
63 return list_first_entry(&atchan->active_list,
64 struct at_desc, desc_node);
65}
66
67static struct at_desc *atc_first_queued(struct at_dma_chan *atchan)
68{
69 return list_first_entry(&atchan->queue,
70 struct at_desc, desc_node);
71}
72
73/**
Uwe Kleine-König421f91d2010-06-11 12:17:00 +020074 * atc_alloc_descriptor - allocate and return an initialized descriptor
Nicolas Ferredc78baa2009-07-03 19:24:33 +020075 * @chan: the channel to allocate descriptors for
76 * @gfp_flags: GFP allocation flags
77 *
78 * Note: The ack-bit is positioned in the descriptor flag at creation time
79 * to make initial allocation more convenient. This bit will be cleared
80 * and control will be given to client at usage time (during
81 * preparation functions).
82 */
83static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan,
84 gfp_t gfp_flags)
85{
86 struct at_desc *desc = NULL;
87 struct at_dma *atdma = to_at_dma(chan->device);
88 dma_addr_t phys;
89
90 desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys);
91 if (desc) {
92 memset(desc, 0, sizeof(struct at_desc));
Dan Williams285a3c72009-09-08 17:53:03 -070093 INIT_LIST_HEAD(&desc->tx_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +020094 dma_async_tx_descriptor_init(&desc->txd, chan);
95 /* txd.flags will be overwritten in prep functions */
96 desc->txd.flags = DMA_CTRL_ACK;
97 desc->txd.tx_submit = atc_tx_submit;
98 desc->txd.phys = phys;
99 }
100
101 return desc;
102}
103
104/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200105 * atc_desc_get - get an unused descriptor from free_list
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200106 * @atchan: channel we want a new descriptor for
107 */
108static struct at_desc *atc_desc_get(struct at_dma_chan *atchan)
109{
110 struct at_desc *desc, *_desc;
111 struct at_desc *ret = NULL;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000112 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200113 unsigned int i = 0;
114 LIST_HEAD(tmp_list);
115
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000116 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200117 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
118 i++;
119 if (async_tx_test_ack(&desc->txd)) {
120 list_del(&desc->desc_node);
121 ret = desc;
122 break;
123 }
124 dev_dbg(chan2dev(&atchan->chan_common),
125 "desc %p not ACKed\n", desc);
126 }
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000127 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200128 dev_vdbg(chan2dev(&atchan->chan_common),
129 "scanned %u descriptors on freelist\n", i);
130
131 /* no more descriptor available in initial pool: create one more */
132 if (!ret) {
133 ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC);
134 if (ret) {
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000135 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200136 atchan->descs_allocated++;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000137 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200138 } else {
139 dev_err(chan2dev(&atchan->chan_common),
140 "not enough descriptors available\n");
141 }
142 }
143
144 return ret;
145}
146
147/**
148 * atc_desc_put - move a descriptor, including any children, to the free list
149 * @atchan: channel we work on
150 * @desc: descriptor, at the head of a chain, to move to free list
151 */
152static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc)
153{
154 if (desc) {
155 struct at_desc *child;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000156 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200157
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000158 spin_lock_irqsave(&atchan->lock, flags);
Dan Williams285a3c72009-09-08 17:53:03 -0700159 list_for_each_entry(child, &desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200160 dev_vdbg(chan2dev(&atchan->chan_common),
161 "moving child desc %p to freelist\n",
162 child);
Dan Williams285a3c72009-09-08 17:53:03 -0700163 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200164 dev_vdbg(chan2dev(&atchan->chan_common),
165 "moving desc %p to freelist\n", desc);
166 list_add(&desc->desc_node, &atchan->free_list);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000167 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200168 }
169}
170
171/**
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200172 * atc_desc_chain - build chain adding a descripor
173 * @first: address of first descripor of the chain
174 * @prev: address of previous descripor of the chain
175 * @desc: descriptor to queue
176 *
177 * Called from prep_* functions
178 */
179static void atc_desc_chain(struct at_desc **first, struct at_desc **prev,
180 struct at_desc *desc)
181{
182 if (!(*first)) {
183 *first = desc;
184 } else {
185 /* inform the HW lli about chaining */
186 (*prev)->lli.dscr = desc->txd.phys;
187 /* insert the link descriptor to the LD ring */
188 list_add_tail(&desc->desc_node,
189 &(*first)->tx_list);
190 }
191 *prev = desc;
192}
193
194/**
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200195 * atc_dostart - starts the DMA engine for real
196 * @atchan: the channel we want to start
197 * @first: first descriptor in the list we want to begin with
198 *
199 * Called with atchan->lock held and bh disabled
200 */
201static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
202{
203 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
204
205 /* ASSERT: channel is idle */
206 if (atc_chan_is_enabled(atchan)) {
207 dev_err(chan2dev(&atchan->chan_common),
208 "BUG: Attempted to start non-idle channel\n");
209 dev_err(chan2dev(&atchan->chan_common),
210 " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
211 channel_readl(atchan, SADDR),
212 channel_readl(atchan, DADDR),
213 channel_readl(atchan, CTRLA),
214 channel_readl(atchan, CTRLB),
215 channel_readl(atchan, DSCR));
216
217 /* The tasklet will hopefully advance the queue... */
218 return;
219 }
220
221 vdbg_dump_regs(atchan);
222
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200223 channel_writel(atchan, SADDR, 0);
224 channel_writel(atchan, DADDR, 0);
225 channel_writel(atchan, CTRLA, 0);
226 channel_writel(atchan, CTRLB, 0);
227 channel_writel(atchan, DSCR, first->txd.phys);
228 dma_writel(atdma, CHER, atchan->mask);
229
230 vdbg_dump_regs(atchan);
231}
232
233/**
234 * atc_chain_complete - finish work for one transaction chain
235 * @atchan: channel we work on
236 * @desc: descriptor at the head of the chain we want do complete
237 *
238 * Called with atchan->lock held and bh disabled */
239static void
240atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
241{
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200242 struct dma_async_tx_descriptor *txd = &desc->txd;
243
244 dev_vdbg(chan2dev(&atchan->chan_common),
245 "descriptor %u complete\n", txd->cookie);
246
Vinod Kould4116052012-05-11 11:48:21 +0530247 /* mark the descriptor as complete for non cyclic cases only */
248 if (!atc_chan_is_cyclic(atchan))
249 dma_cookie_complete(txd);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200250
251 /* move children to free_list */
Dan Williams285a3c72009-09-08 17:53:03 -0700252 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200253 /* move myself to free_list */
254 list_move(&desc->desc_node, &atchan->free_list);
255
Nicolas Ferreebcf9b82011-01-12 15:39:06 +0100256 /* unmap dma addresses (not on slave channels) */
Atsushi Nemoto657a77fa2009-09-08 17:53:05 -0700257 if (!atchan->chan_common.private) {
258 struct device *parent = chan2parent(&atchan->chan_common);
259 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
260 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
261 dma_unmap_single(parent,
262 desc->lli.daddr,
263 desc->len, DMA_FROM_DEVICE);
264 else
265 dma_unmap_page(parent,
266 desc->lli.daddr,
267 desc->len, DMA_FROM_DEVICE);
268 }
269 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
270 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
271 dma_unmap_single(parent,
272 desc->lli.saddr,
273 desc->len, DMA_TO_DEVICE);
274 else
275 dma_unmap_page(parent,
276 desc->lli.saddr,
277 desc->len, DMA_TO_DEVICE);
278 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200279 }
280
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200281 /* for cyclic transfers,
282 * no need to replay callback function while stopping */
Nicolas Ferre3c477482011-07-25 21:09:23 +0000283 if (!atc_chan_is_cyclic(atchan)) {
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200284 dma_async_tx_callback callback = txd->callback;
285 void *param = txd->callback_param;
286
287 /*
288 * The API requires that no submissions are done from a
289 * callback, so we don't need to drop the lock here
290 */
291 if (callback)
292 callback(param);
293 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200294
295 dma_run_dependencies(txd);
296}
297
298/**
299 * atc_complete_all - finish work for all transactions
300 * @atchan: channel to complete transactions for
301 *
302 * Eventually submit queued descriptors if any
303 *
304 * Assume channel is idle while calling this function
305 * Called with atchan->lock held and bh disabled
306 */
307static void atc_complete_all(struct at_dma_chan *atchan)
308{
309 struct at_desc *desc, *_desc;
310 LIST_HEAD(list);
311
312 dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
313
314 BUG_ON(atc_chan_is_enabled(atchan));
315
316 /*
317 * Submit queued descriptors ASAP, i.e. before we go through
318 * the completed ones.
319 */
320 if (!list_empty(&atchan->queue))
321 atc_dostart(atchan, atc_first_queued(atchan));
322 /* empty active_list now it is completed */
323 list_splice_init(&atchan->active_list, &list);
324 /* empty queue list by moving descriptors (if any) to active_list */
325 list_splice_init(&atchan->queue, &atchan->active_list);
326
327 list_for_each_entry_safe(desc, _desc, &list, desc_node)
328 atc_chain_complete(atchan, desc);
329}
330
331/**
332 * atc_cleanup_descriptors - cleanup up finished descriptors in active_list
333 * @atchan: channel to be cleaned up
334 *
335 * Called with atchan->lock held and bh disabled
336 */
337static void atc_cleanup_descriptors(struct at_dma_chan *atchan)
338{
339 struct at_desc *desc, *_desc;
340 struct at_desc *child;
341
342 dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n");
343
344 list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
345 if (!(desc->lli.ctrla & ATC_DONE))
346 /* This one is currently in progress */
347 return;
348
Dan Williams285a3c72009-09-08 17:53:03 -0700349 list_for_each_entry(child, &desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200350 if (!(child->lli.ctrla & ATC_DONE))
351 /* Currently in progress */
352 return;
353
354 /*
355 * No descriptors so far seem to be in progress, i.e.
356 * this chain must be done.
357 */
358 atc_chain_complete(atchan, desc);
359 }
360}
361
362/**
363 * atc_advance_work - at the end of a transaction, move forward
364 * @atchan: channel where the transaction ended
365 *
366 * Called with atchan->lock held and bh disabled
367 */
368static void atc_advance_work(struct at_dma_chan *atchan)
369{
370 dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
371
372 if (list_empty(&atchan->active_list) ||
373 list_is_singular(&atchan->active_list)) {
374 atc_complete_all(atchan);
375 } else {
376 atc_chain_complete(atchan, atc_first_active(atchan));
377 /* advance work */
378 atc_dostart(atchan, atc_first_active(atchan));
379 }
380}
381
382
383/**
384 * atc_handle_error - handle errors reported by DMA controller
385 * @atchan: channel where error occurs
386 *
387 * Called with atchan->lock held and bh disabled
388 */
389static void atc_handle_error(struct at_dma_chan *atchan)
390{
391 struct at_desc *bad_desc;
392 struct at_desc *child;
393
394 /*
395 * The descriptor currently at the head of the active list is
396 * broked. Since we don't have any way to report errors, we'll
397 * just have to scream loudly and try to carry on.
398 */
399 bad_desc = atc_first_active(atchan);
400 list_del_init(&bad_desc->desc_node);
401
402 /* As we are stopped, take advantage to push queued descriptors
403 * in active_list */
404 list_splice_init(&atchan->queue, atchan->active_list.prev);
405
406 /* Try to restart the controller */
407 if (!list_empty(&atchan->active_list))
408 atc_dostart(atchan, atc_first_active(atchan));
409
410 /*
411 * KERN_CRITICAL may seem harsh, but since this only happens
412 * when someone submits a bad physical address in a
413 * descriptor, we should consider ourselves lucky that the
414 * controller flagged an error instead of scribbling over
415 * random memory locations.
416 */
417 dev_crit(chan2dev(&atchan->chan_common),
418 "Bad descriptor submitted for DMA!\n");
419 dev_crit(chan2dev(&atchan->chan_common),
420 " cookie: %d\n", bad_desc->txd.cookie);
421 atc_dump_lli(atchan, &bad_desc->lli);
Dan Williams285a3c72009-09-08 17:53:03 -0700422 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200423 atc_dump_lli(atchan, &child->lli);
424
425 /* Pretend the descriptor completed successfully */
426 atc_chain_complete(atchan, bad_desc);
427}
428
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200429/**
430 * atc_handle_cyclic - at the end of a period, run callback function
431 * @atchan: channel used for cyclic operations
432 *
433 * Called with atchan->lock held and bh disabled
434 */
435static void atc_handle_cyclic(struct at_dma_chan *atchan)
436{
437 struct at_desc *first = atc_first_active(atchan);
438 struct dma_async_tx_descriptor *txd = &first->txd;
439 dma_async_tx_callback callback = txd->callback;
440 void *param = txd->callback_param;
441
442 dev_vdbg(chan2dev(&atchan->chan_common),
443 "new cyclic period llp 0x%08x\n",
444 channel_readl(atchan, DSCR));
445
446 if (callback)
447 callback(param);
448}
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200449
450/*-- IRQ & Tasklet ---------------------------------------------------*/
451
452static void atc_tasklet(unsigned long data)
453{
454 struct at_dma_chan *atchan = (struct at_dma_chan *)data;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000455 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200456
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000457 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200458 if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200459 atc_handle_error(atchan);
Nicolas Ferre3c477482011-07-25 21:09:23 +0000460 else if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200461 atc_handle_cyclic(atchan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200462 else
463 atc_advance_work(atchan);
464
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000465 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200466}
467
468static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
469{
470 struct at_dma *atdma = (struct at_dma *)dev_id;
471 struct at_dma_chan *atchan;
472 int i;
473 u32 status, pending, imr;
474 int ret = IRQ_NONE;
475
476 do {
477 imr = dma_readl(atdma, EBCIMR);
478 status = dma_readl(atdma, EBCISR);
479 pending = status & imr;
480
481 if (!pending)
482 break;
483
484 dev_vdbg(atdma->dma_common.dev,
485 "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
486 status, imr, pending);
487
488 for (i = 0; i < atdma->dma_common.chancnt; i++) {
489 atchan = &atdma->chan[i];
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200490 if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200491 if (pending & AT_DMA_ERR(i)) {
492 /* Disable channel on AHB error */
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200493 dma_writel(atdma, CHDR,
494 AT_DMA_RES(i) | atchan->mask);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200495 /* Give information to tasklet */
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200496 set_bit(ATC_IS_ERROR, &atchan->status);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200497 }
498 tasklet_schedule(&atchan->tasklet);
499 ret = IRQ_HANDLED;
500 }
501 }
502
503 } while (pending);
504
505 return ret;
506}
507
508
509/*-- DMA Engine API --------------------------------------------------*/
510
511/**
512 * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
513 * @desc: descriptor at the head of the transaction chain
514 *
515 * Queue chain if DMA engine is working already
516 *
517 * Cookie increment and adding to active_list or queue must be atomic
518 */
519static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
520{
521 struct at_desc *desc = txd_to_at_desc(tx);
522 struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
523 dma_cookie_t cookie;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000524 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200525
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000526 spin_lock_irqsave(&atchan->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000527 cookie = dma_cookie_assign(tx);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200528
529 if (list_empty(&atchan->active_list)) {
530 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
531 desc->txd.cookie);
532 atc_dostart(atchan, desc);
533 list_add_tail(&desc->desc_node, &atchan->active_list);
534 } else {
535 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
536 desc->txd.cookie);
537 list_add_tail(&desc->desc_node, &atchan->queue);
538 }
539
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000540 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200541
542 return cookie;
543}
544
545/**
546 * atc_prep_dma_memcpy - prepare a memcpy operation
547 * @chan: the channel to prepare operation on
548 * @dest: operation virtual destination address
549 * @src: operation virtual source address
550 * @len: operation length
551 * @flags: tx descriptor status flags
552 */
553static struct dma_async_tx_descriptor *
554atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
555 size_t len, unsigned long flags)
556{
557 struct at_dma_chan *atchan = to_at_dma_chan(chan);
558 struct at_desc *desc = NULL;
559 struct at_desc *first = NULL;
560 struct at_desc *prev = NULL;
561 size_t xfer_count;
562 size_t offset;
563 unsigned int src_width;
564 unsigned int dst_width;
565 u32 ctrla;
566 u32 ctrlb;
567
568 dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
569 dest, src, len, flags);
570
571 if (unlikely(!len)) {
572 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
573 return NULL;
574 }
575
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200576 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200577 | ATC_SRC_ADDR_MODE_INCR
578 | ATC_DST_ADDR_MODE_INCR
579 | ATC_FC_MEM2MEM;
580
581 /*
582 * We can be a lot more clever here, but this should take care
583 * of the most common optimization.
584 */
585 if (!((src | dest | len) & 3)) {
Nicolas Ferreb409ebf2012-05-10 12:17:40 +0200586 ctrla = ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200587 src_width = dst_width = 2;
588 } else if (!((src | dest | len) & 1)) {
Nicolas Ferreb409ebf2012-05-10 12:17:40 +0200589 ctrla = ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200590 src_width = dst_width = 1;
591 } else {
Nicolas Ferreb409ebf2012-05-10 12:17:40 +0200592 ctrla = ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200593 src_width = dst_width = 0;
594 }
595
596 for (offset = 0; offset < len; offset += xfer_count << src_width) {
597 xfer_count = min_t(size_t, (len - offset) >> src_width,
598 ATC_BTSIZE_MAX);
599
600 desc = atc_desc_get(atchan);
601 if (!desc)
602 goto err_desc_get;
603
604 desc->lli.saddr = src + offset;
605 desc->lli.daddr = dest + offset;
606 desc->lli.ctrla = ctrla | xfer_count;
607 desc->lli.ctrlb = ctrlb;
608
609 desc->txd.cookie = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200610
Nicolas Ferree257e152011-05-06 19:56:53 +0200611 atc_desc_chain(&first, &prev, desc);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200612 }
613
614 /* First descriptor of the chain embedds additional information */
615 first->txd.cookie = -EBUSY;
616 first->len = len;
617
618 /* set end-of-link to the last link descriptor of list*/
619 set_desc_eol(desc);
620
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100621 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200622
623 return &first->txd;
624
625err_desc_get:
626 atc_desc_put(atchan, first);
627 return NULL;
628}
629
Nicolas Ferre808347f2009-07-22 20:04:45 +0200630
631/**
632 * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
633 * @chan: DMA channel
634 * @sgl: scatterlist to transfer to/from
635 * @sg_len: number of entries in @scatterlist
636 * @direction: DMA direction
637 * @flags: tx descriptor status flags
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500638 * @context: transaction context (ignored)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200639 */
640static struct dma_async_tx_descriptor *
641atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530642 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500643 unsigned long flags, void *context)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200644{
645 struct at_dma_chan *atchan = to_at_dma_chan(chan);
646 struct at_dma_slave *atslave = chan->private;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100647 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200648 struct at_desc *first = NULL;
649 struct at_desc *prev = NULL;
650 u32 ctrla;
651 u32 ctrlb;
652 dma_addr_t reg;
653 unsigned int reg_width;
654 unsigned int mem_width;
655 unsigned int i;
656 struct scatterlist *sg;
657 size_t total_len = 0;
658
Nicolas Ferrecc52a102011-04-30 16:57:47 +0200659 dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
660 sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530661 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre808347f2009-07-22 20:04:45 +0200662 flags);
663
664 if (unlikely(!atslave || !sg_len)) {
665 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
666 return NULL;
667 }
668
Nicolas Ferre1dd1ea82012-05-10 12:17:41 +0200669 ctrla = ATC_SCSIZE(sconfig->src_maxburst)
670 | ATC_DCSIZE(sconfig->dst_maxburst);
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200671 ctrlb = ATC_IEN;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200672
673 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530674 case DMA_MEM_TO_DEV:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100675 reg_width = convert_buswidth(sconfig->dst_addr_width);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200676 ctrla |= ATC_DST_WIDTH(reg_width);
677 ctrlb |= ATC_DST_ADDR_MODE_FIXED
678 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200679 | ATC_FC_MEM2PER
680 | ATC_SIF(AT_DMA_MEM_IF) | ATC_DIF(AT_DMA_PER_IF);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100681 reg = sconfig->dst_addr;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200682 for_each_sg(sgl, sg, sg_len, i) {
683 struct at_desc *desc;
684 u32 len;
685 u32 mem;
686
687 desc = atc_desc_get(atchan);
688 if (!desc)
689 goto err_desc_get;
690
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100691 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200692 len = sg_dma_len(sg);
693 mem_width = 2;
694 if (unlikely(mem & 3 || len & 3))
695 mem_width = 0;
696
697 desc->lli.saddr = mem;
698 desc->lli.daddr = reg;
699 desc->lli.ctrla = ctrla
700 | ATC_SRC_WIDTH(mem_width)
701 | len >> mem_width;
702 desc->lli.ctrlb = ctrlb;
703
Nicolas Ferree257e152011-05-06 19:56:53 +0200704 atc_desc_chain(&first, &prev, desc);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200705 total_len += len;
706 }
707 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530708 case DMA_DEV_TO_MEM:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100709 reg_width = convert_buswidth(sconfig->src_addr_width);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200710 ctrla |= ATC_SRC_WIDTH(reg_width);
711 ctrlb |= ATC_DST_ADDR_MODE_INCR
712 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200713 | ATC_FC_PER2MEM
714 | ATC_SIF(AT_DMA_PER_IF) | ATC_DIF(AT_DMA_MEM_IF);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200715
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100716 reg = sconfig->src_addr;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200717 for_each_sg(sgl, sg, sg_len, i) {
718 struct at_desc *desc;
719 u32 len;
720 u32 mem;
721
722 desc = atc_desc_get(atchan);
723 if (!desc)
724 goto err_desc_get;
725
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100726 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200727 len = sg_dma_len(sg);
728 mem_width = 2;
729 if (unlikely(mem & 3 || len & 3))
730 mem_width = 0;
731
732 desc->lli.saddr = reg;
733 desc->lli.daddr = mem;
734 desc->lli.ctrla = ctrla
735 | ATC_DST_WIDTH(mem_width)
Nicolas Ferre59a609d2010-12-13 13:48:41 +0100736 | len >> reg_width;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200737 desc->lli.ctrlb = ctrlb;
738
Nicolas Ferree257e152011-05-06 19:56:53 +0200739 atc_desc_chain(&first, &prev, desc);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200740 total_len += len;
741 }
742 break;
743 default:
744 return NULL;
745 }
746
747 /* set end-of-link to the last link descriptor of list*/
748 set_desc_eol(prev);
749
750 /* First descriptor of the chain embedds additional information */
751 first->txd.cookie = -EBUSY;
752 first->len = total_len;
753
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100754 /* first link descriptor of list is responsible of flags */
755 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferre808347f2009-07-22 20:04:45 +0200756
757 return &first->txd;
758
759err_desc_get:
760 dev_err(chan2dev(chan), "not enough descriptors available\n");
761 atc_desc_put(atchan, first);
762 return NULL;
763}
764
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200765/**
766 * atc_dma_cyclic_check_values
767 * Check for too big/unaligned periods and unaligned DMA buffer
768 */
769static int
770atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530771 size_t period_len, enum dma_transfer_direction direction)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200772{
773 if (period_len > (ATC_BTSIZE_MAX << reg_width))
774 goto err_out;
775 if (unlikely(period_len & ((1 << reg_width) - 1)))
776 goto err_out;
777 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
778 goto err_out;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530779 if (unlikely(!(direction & (DMA_DEV_TO_MEM | DMA_MEM_TO_DEV))))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200780 goto err_out;
781
782 return 0;
783
784err_out:
785 return -EINVAL;
786}
787
788/**
789 * atc_dma_cyclic_fill_desc - Fill one period decriptor
790 */
791static int
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100792atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200793 unsigned int period_index, dma_addr_t buf_addr,
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100794 unsigned int reg_width, size_t period_len,
795 enum dma_transfer_direction direction)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200796{
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100797 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100798 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
799 u32 ctrla;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200800
801 /* prepare common CRTLA value */
Nicolas Ferre1dd1ea82012-05-10 12:17:41 +0200802 ctrla = ATC_SCSIZE(sconfig->src_maxburst)
803 | ATC_DCSIZE(sconfig->dst_maxburst)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200804 | ATC_DST_WIDTH(reg_width)
805 | ATC_SRC_WIDTH(reg_width)
806 | period_len >> reg_width;
807
808 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530809 case DMA_MEM_TO_DEV:
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200810 desc->lli.saddr = buf_addr + (period_len * period_index);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100811 desc->lli.daddr = sconfig->dst_addr;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200812 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200813 desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200814 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200815 | ATC_FC_MEM2PER
816 | ATC_SIF(AT_DMA_MEM_IF)
817 | ATC_DIF(AT_DMA_PER_IF);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200818 break;
819
Vinod Kouldb8196d2011-10-13 22:34:23 +0530820 case DMA_DEV_TO_MEM:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100821 desc->lli.saddr = sconfig->src_addr;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200822 desc->lli.daddr = buf_addr + (period_len * period_index);
823 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200824 desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200825 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200826 | ATC_FC_PER2MEM
827 | ATC_SIF(AT_DMA_PER_IF)
828 | ATC_DIF(AT_DMA_MEM_IF);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200829 break;
830
831 default:
832 return -EINVAL;
833 }
834
835 return 0;
836}
837
838/**
839 * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
840 * @chan: the DMA channel to prepare
841 * @buf_addr: physical DMA address where the buffer starts
842 * @buf_len: total number of bytes for the entire buffer
843 * @period_len: number of bytes for each period
844 * @direction: transfer direction, to or from device
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500845 * @context: transfer context (ignored)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200846 */
847static struct dma_async_tx_descriptor *
848atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500849 size_t period_len, enum dma_transfer_direction direction,
850 void *context)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200851{
852 struct at_dma_chan *atchan = to_at_dma_chan(chan);
853 struct at_dma_slave *atslave = chan->private;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100854 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200855 struct at_desc *first = NULL;
856 struct at_desc *prev = NULL;
857 unsigned long was_cyclic;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100858 unsigned int reg_width;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200859 unsigned int periods = buf_len / period_len;
860 unsigned int i;
861
862 dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
Vinod Kouldb8196d2011-10-13 22:34:23 +0530863 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200864 buf_addr,
865 periods, buf_len, period_len);
866
867 if (unlikely(!atslave || !buf_len || !period_len)) {
868 dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
869 return NULL;
870 }
871
872 was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
873 if (was_cyclic) {
874 dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
875 return NULL;
876 }
877
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100878 if (sconfig->direction == DMA_MEM_TO_DEV)
879 reg_width = convert_buswidth(sconfig->dst_addr_width);
880 else
881 reg_width = convert_buswidth(sconfig->src_addr_width);
882
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200883 /* Check for too big/unaligned periods and unaligned DMA buffer */
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100884 if (atc_dma_cyclic_check_values(reg_width, buf_addr,
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200885 period_len, direction))
886 goto err_out;
887
888 /* build cyclic linked list */
889 for (i = 0; i < periods; i++) {
890 struct at_desc *desc;
891
892 desc = atc_desc_get(atchan);
893 if (!desc)
894 goto err_desc_get;
895
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100896 if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
897 reg_width, period_len, direction))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200898 goto err_desc_get;
899
900 atc_desc_chain(&first, &prev, desc);
901 }
902
903 /* lets make a cyclic list */
904 prev->lli.dscr = first->txd.phys;
905
906 /* First descriptor of the chain embedds additional information */
907 first->txd.cookie = -EBUSY;
908 first->len = buf_len;
909
910 return &first->txd;
911
912err_desc_get:
913 dev_err(chan2dev(chan), "not enough descriptors available\n");
914 atc_desc_put(atchan, first);
915err_out:
916 clear_bit(ATC_IS_CYCLIC, &atchan->status);
917 return NULL;
918}
919
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100920static int set_runtime_config(struct dma_chan *chan,
921 struct dma_slave_config *sconfig)
922{
923 struct at_dma_chan *atchan = to_at_dma_chan(chan);
924
925 /* Check if it is chan is configured for slave transfers */
926 if (!chan->private)
927 return -EINVAL;
928
929 memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
930
931 convert_burst(&atchan->dma_sconfig.src_maxburst);
932 convert_burst(&atchan->dma_sconfig.dst_maxburst);
933
934 return 0;
935}
936
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200937
Linus Walleij05827632010-05-17 16:30:42 -0700938static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
939 unsigned long arg)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200940{
941 struct at_dma_chan *atchan = to_at_dma_chan(chan);
942 struct at_dma *atdma = to_at_dma(chan->device);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200943 int chan_id = atchan->chan_common.chan_id;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000944 unsigned long flags;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200945
Nicolas Ferre808347f2009-07-22 20:04:45 +0200946 LIST_HEAD(list);
947
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200948 dev_vdbg(chan2dev(chan), "atc_control (%d)\n", cmd);
949
950 if (cmd == DMA_PAUSE) {
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000951 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200952
953 dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200954 set_bit(ATC_IS_PAUSED, &atchan->status);
955
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000956 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200957 } else if (cmd == DMA_RESUME) {
Nicolas Ferre3c477482011-07-25 21:09:23 +0000958 if (!atc_chan_is_paused(atchan))
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200959 return 0;
960
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000961 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200962
963 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
964 clear_bit(ATC_IS_PAUSED, &atchan->status);
965
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000966 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200967 } else if (cmd == DMA_TERMINATE_ALL) {
968 struct at_desc *desc, *_desc;
969 /*
970 * This is only called when something went wrong elsewhere, so
971 * we don't really care about the data. Just disable the
972 * channel. We still have to poll the channel enable bit due
973 * to AHB/HSB limitations.
974 */
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000975 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200976
977 /* disabling channel: must also remove suspend state */
978 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
979
980 /* confirm that this channel is disabled */
981 while (dma_readl(atdma, CHSR) & atchan->mask)
982 cpu_relax();
983
984 /* active_list entries will end up before queued entries */
985 list_splice_init(&atchan->queue, &list);
986 list_splice_init(&atchan->active_list, &list);
987
988 /* Flush all pending and queued descriptors */
989 list_for_each_entry_safe(desc, _desc, &list, desc_node)
990 atc_chain_complete(atchan, desc);
991
992 clear_bit(ATC_IS_PAUSED, &atchan->status);
993 /* if channel dedicated to cyclic operations, free it */
994 clear_bit(ATC_IS_CYCLIC, &atchan->status);
995
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000996 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100997 } else if (cmd == DMA_SLAVE_CONFIG) {
998 return set_runtime_config(chan, (struct dma_slave_config *)arg);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200999 } else {
Linus Walleijc3635c72010-03-26 16:44:01 -07001000 return -ENXIO;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001001 }
Yong Wangb0ebeb92010-08-05 10:40:08 +08001002
Linus Walleijc3635c72010-03-26 16:44:01 -07001003 return 0;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001004}
1005
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001006/**
Linus Walleij07934482010-03-26 16:50:49 -07001007 * atc_tx_status - poll for transaction completion
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001008 * @chan: DMA channel
1009 * @cookie: transaction identifier to check status of
Linus Walleij07934482010-03-26 16:50:49 -07001010 * @txstate: if not %NULL updated with transaction state
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001011 *
Linus Walleij07934482010-03-26 16:50:49 -07001012 * If @txstate is passed in, upon return it reflect the driver
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001013 * internal state and can be used with dma_async_is_complete() to check
1014 * the status of multiple cookies without re-checking hardware state.
1015 */
1016static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -07001017atc_tx_status(struct dma_chan *chan,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001018 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -07001019 struct dma_tx_state *txstate)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001020{
1021 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1022 dma_cookie_t last_used;
1023 dma_cookie_t last_complete;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001024 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001025 enum dma_status ret;
1026
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001027 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001028
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001029 ret = dma_cookie_status(chan, cookie, txstate);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001030 if (ret != DMA_SUCCESS) {
1031 atc_cleanup_descriptors(atchan);
1032
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001033 ret = dma_cookie_status(chan, cookie, txstate);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001034 }
1035
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001036 last_complete = chan->completed_cookie;
1037 last_used = chan->cookie;
1038
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001039 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001040
Nicolas Ferre543aabc2011-05-06 19:56:51 +02001041 if (ret != DMA_SUCCESS)
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001042 dma_set_residue(txstate, atc_first_active(atchan)->len);
Nicolas Ferre543aabc2011-05-06 19:56:51 +02001043
Nicolas Ferre3c477482011-07-25 21:09:23 +00001044 if (atc_chan_is_paused(atchan))
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001045 ret = DMA_PAUSED;
1046
1047 dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d (d%d, u%d)\n",
1048 ret, cookie, last_complete ? last_complete : 0,
Linus Walleij07934482010-03-26 16:50:49 -07001049 last_used ? last_used : 0);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001050
1051 return ret;
1052}
1053
1054/**
1055 * atc_issue_pending - try to finish work
1056 * @chan: target DMA channel
1057 */
1058static void atc_issue_pending(struct dma_chan *chan)
1059{
1060 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001061 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001062
1063 dev_vdbg(chan2dev(chan), "issue_pending\n");
1064
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001065 /* Not needed for cyclic transfers */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001066 if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001067 return;
1068
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001069 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001070 if (!atc_chan_is_enabled(atchan)) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001071 atc_advance_work(atchan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001072 }
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001073 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001074}
1075
1076/**
1077 * atc_alloc_chan_resources - allocate resources for DMA channel
1078 * @chan: allocate descriptor resources for this channel
1079 * @client: current client requesting the channel be ready for requests
1080 *
1081 * return - the number of allocated descriptors
1082 */
1083static int atc_alloc_chan_resources(struct dma_chan *chan)
1084{
1085 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1086 struct at_dma *atdma = to_at_dma(chan->device);
1087 struct at_desc *desc;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001088 struct at_dma_slave *atslave;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001089 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001090 int i;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001091 u32 cfg;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001092 LIST_HEAD(tmp_list);
1093
1094 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
1095
1096 /* ASSERT: channel is idle */
1097 if (atc_chan_is_enabled(atchan)) {
1098 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
1099 return -EIO;
1100 }
1101
Nicolas Ferre808347f2009-07-22 20:04:45 +02001102 cfg = ATC_DEFAULT_CFG;
1103
1104 atslave = chan->private;
1105 if (atslave) {
1106 /*
1107 * We need controller-specific data to set up slave
1108 * transfers.
1109 */
1110 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
1111
1112 /* if cfg configuration specified take it instad of default */
1113 if (atslave->cfg)
1114 cfg = atslave->cfg;
1115 }
1116
1117 /* have we already been set up?
1118 * reconfigure channel but no need to reallocate descriptors */
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001119 if (!list_empty(&atchan->free_list))
1120 return atchan->descs_allocated;
1121
1122 /* Allocate initial pool of descriptors */
1123 for (i = 0; i < init_nr_desc_per_channel; i++) {
1124 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
1125 if (!desc) {
1126 dev_err(atdma->dma_common.dev,
1127 "Only %d initial descriptors\n", i);
1128 break;
1129 }
1130 list_add_tail(&desc->desc_node, &tmp_list);
1131 }
1132
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001133 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001134 atchan->descs_allocated = i;
1135 list_splice(&tmp_list, &atchan->free_list);
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001136 dma_cookie_init(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001137 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001138
1139 /* channel parameters */
Nicolas Ferre808347f2009-07-22 20:04:45 +02001140 channel_writel(atchan, CFG, cfg);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001141
1142 dev_dbg(chan2dev(chan),
1143 "alloc_chan_resources: allocated %d descriptors\n",
1144 atchan->descs_allocated);
1145
1146 return atchan->descs_allocated;
1147}
1148
1149/**
1150 * atc_free_chan_resources - free all channel resources
1151 * @chan: DMA channel
1152 */
1153static void atc_free_chan_resources(struct dma_chan *chan)
1154{
1155 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1156 struct at_dma *atdma = to_at_dma(chan->device);
1157 struct at_desc *desc, *_desc;
1158 LIST_HEAD(list);
1159
1160 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
1161 atchan->descs_allocated);
1162
1163 /* ASSERT: channel is idle */
1164 BUG_ON(!list_empty(&atchan->active_list));
1165 BUG_ON(!list_empty(&atchan->queue));
1166 BUG_ON(atc_chan_is_enabled(atchan));
1167
1168 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
1169 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1170 list_del(&desc->desc_node);
1171 /* free link descriptor */
1172 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
1173 }
1174 list_splice_init(&atchan->free_list, &list);
1175 atchan->descs_allocated = 0;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001176 atchan->status = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001177
1178 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
1179}
1180
1181
1182/*-- Module Management -----------------------------------------------*/
1183
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001184/* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
1185static struct at_dma_platform_data at91sam9rl_config = {
1186 .nr_channels = 2,
1187};
1188static struct at_dma_platform_data at91sam9g45_config = {
1189 .nr_channels = 8,
1190};
1191
Nicolas Ferrec5115952011-10-17 14:56:41 +02001192#if defined(CONFIG_OF)
1193static const struct of_device_id atmel_dma_dt_ids[] = {
1194 {
1195 .compatible = "atmel,at91sam9rl-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001196 .data = &at91sam9rl_config,
Nicolas Ferrec5115952011-10-17 14:56:41 +02001197 }, {
1198 .compatible = "atmel,at91sam9g45-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001199 .data = &at91sam9g45_config,
Nicolas Ferredcc81732011-11-22 11:55:53 +01001200 }, {
1201 /* sentinel */
1202 }
Nicolas Ferrec5115952011-10-17 14:56:41 +02001203};
1204
1205MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
1206#endif
1207
Nicolas Ferre0ab88a02011-11-22 11:55:52 +01001208static const struct platform_device_id atdma_devtypes[] = {
Nicolas Ferre67348452011-10-17 14:56:40 +02001209 {
1210 .name = "at91sam9rl_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001211 .driver_data = (unsigned long) &at91sam9rl_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001212 }, {
1213 .name = "at91sam9g45_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001214 .driver_data = (unsigned long) &at91sam9g45_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001215 }, {
1216 /* sentinel */
1217 }
1218};
1219
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001220static inline struct at_dma_platform_data * __init at_dma_get_driver_data(
1221 struct platform_device *pdev)
Nicolas Ferrec5115952011-10-17 14:56:41 +02001222{
1223 if (pdev->dev.of_node) {
1224 const struct of_device_id *match;
1225 match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
1226 if (match == NULL)
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001227 return NULL;
1228 return match->data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001229 }
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001230 return (struct at_dma_platform_data *)
1231 platform_get_device_id(pdev)->driver_data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001232}
1233
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001234/**
1235 * at_dma_off - disable DMA controller
1236 * @atdma: the Atmel HDAMC device
1237 */
1238static void at_dma_off(struct at_dma *atdma)
1239{
1240 dma_writel(atdma, EN, 0);
1241
1242 /* disable all interrupts */
1243 dma_writel(atdma, EBCIDR, -1L);
1244
1245 /* confirm that all channels are disabled */
1246 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
1247 cpu_relax();
1248}
1249
1250static int __init at_dma_probe(struct platform_device *pdev)
1251{
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001252 struct resource *io;
1253 struct at_dma *atdma;
1254 size_t size;
1255 int irq;
1256 int err;
1257 int i;
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001258 struct at_dma_platform_data *plat_dat;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001259
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001260 /* setup platform data for each SoC */
1261 dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
1262 dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
1263 dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
Nicolas Ferre67348452011-10-17 14:56:40 +02001264
1265 /* get DMA parameters from controller type */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001266 plat_dat = at_dma_get_driver_data(pdev);
1267 if (!plat_dat)
1268 return -ENODEV;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001269
1270 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1271 if (!io)
1272 return -EINVAL;
1273
1274 irq = platform_get_irq(pdev, 0);
1275 if (irq < 0)
1276 return irq;
1277
1278 size = sizeof(struct at_dma);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001279 size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001280 atdma = kzalloc(size, GFP_KERNEL);
1281 if (!atdma)
1282 return -ENOMEM;
1283
Nicolas Ferre67348452011-10-17 14:56:40 +02001284 /* discover transaction capabilities */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001285 atdma->dma_common.cap_mask = plat_dat->cap_mask;
1286 atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001287
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07001288 size = resource_size(io);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001289 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1290 err = -EBUSY;
1291 goto err_kfree;
1292 }
1293
1294 atdma->regs = ioremap(io->start, size);
1295 if (!atdma->regs) {
1296 err = -ENOMEM;
1297 goto err_release_r;
1298 }
1299
1300 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1301 if (IS_ERR(atdma->clk)) {
1302 err = PTR_ERR(atdma->clk);
1303 goto err_clk;
1304 }
1305 clk_enable(atdma->clk);
1306
1307 /* force dma off, just in case */
1308 at_dma_off(atdma);
1309
1310 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1311 if (err)
1312 goto err_irq;
1313
1314 platform_set_drvdata(pdev, atdma);
1315
1316 /* create a pool of consistent memory blocks for hardware descriptors */
1317 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1318 &pdev->dev, sizeof(struct at_desc),
1319 4 /* word alignment */, 0);
1320 if (!atdma->dma_desc_pool) {
1321 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1322 err = -ENOMEM;
1323 goto err_pool_create;
1324 }
1325
1326 /* clear any pending interrupt */
1327 while (dma_readl(atdma, EBCISR))
1328 cpu_relax();
1329
1330 /* initialize channels related values */
1331 INIT_LIST_HEAD(&atdma->dma_common.channels);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001332 for (i = 0; i < plat_dat->nr_channels; i++) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001333 struct at_dma_chan *atchan = &atdma->chan[i];
1334
1335 atchan->chan_common.device = &atdma->dma_common;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001336 dma_cookie_init(&atchan->chan_common);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001337 list_add_tail(&atchan->chan_common.device_node,
1338 &atdma->dma_common.channels);
1339
1340 atchan->ch_regs = atdma->regs + ch_regs(i);
1341 spin_lock_init(&atchan->lock);
1342 atchan->mask = 1 << i;
1343
1344 INIT_LIST_HEAD(&atchan->active_list);
1345 INIT_LIST_HEAD(&atchan->queue);
1346 INIT_LIST_HEAD(&atchan->free_list);
1347
1348 tasklet_init(&atchan->tasklet, atc_tasklet,
1349 (unsigned long)atchan);
Nikolaus Vossbda3a472012-01-17 10:28:33 +01001350 atc_enable_chan_irq(atdma, i);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001351 }
1352
1353 /* set base routines */
1354 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1355 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
Linus Walleij07934482010-03-26 16:50:49 -07001356 atdma->dma_common.device_tx_status = atc_tx_status;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001357 atdma->dma_common.device_issue_pending = atc_issue_pending;
1358 atdma->dma_common.dev = &pdev->dev;
1359
1360 /* set prep routines based on capability */
1361 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1362 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1363
Nicolas Ferred7db8082011-08-05 11:43:44 +00001364 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
Nicolas Ferre808347f2009-07-22 20:04:45 +02001365 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001366 /* controller can do slave DMA: can trigger cyclic transfers */
1367 dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001368 atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
Linus Walleijc3635c72010-03-26 16:44:01 -07001369 atdma->dma_common.device_control = atc_control;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001370 }
Nicolas Ferre808347f2009-07-22 20:04:45 +02001371
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001372 dma_writel(atdma, EN, AT_DMA_ENABLE);
1373
1374 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
1375 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1376 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001377 plat_dat->nr_channels);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001378
1379 dma_async_device_register(&atdma->dma_common);
1380
1381 return 0;
1382
1383err_pool_create:
1384 platform_set_drvdata(pdev, NULL);
1385 free_irq(platform_get_irq(pdev, 0), atdma);
1386err_irq:
1387 clk_disable(atdma->clk);
1388 clk_put(atdma->clk);
1389err_clk:
1390 iounmap(atdma->regs);
1391 atdma->regs = NULL;
1392err_release_r:
1393 release_mem_region(io->start, size);
1394err_kfree:
1395 kfree(atdma);
1396 return err;
1397}
1398
1399static int __exit at_dma_remove(struct platform_device *pdev)
1400{
1401 struct at_dma *atdma = platform_get_drvdata(pdev);
1402 struct dma_chan *chan, *_chan;
1403 struct resource *io;
1404
1405 at_dma_off(atdma);
1406 dma_async_device_unregister(&atdma->dma_common);
1407
1408 dma_pool_destroy(atdma->dma_desc_pool);
1409 platform_set_drvdata(pdev, NULL);
1410 free_irq(platform_get_irq(pdev, 0), atdma);
1411
1412 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1413 device_node) {
1414 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1415
1416 /* Disable interrupts */
Nikolaus Vossbda3a472012-01-17 10:28:33 +01001417 atc_disable_chan_irq(atdma, chan->chan_id);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001418 tasklet_disable(&atchan->tasklet);
1419
1420 tasklet_kill(&atchan->tasklet);
1421 list_del(&chan->device_node);
1422 }
1423
1424 clk_disable(atdma->clk);
1425 clk_put(atdma->clk);
1426
1427 iounmap(atdma->regs);
1428 atdma->regs = NULL;
1429
1430 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07001431 release_mem_region(io->start, resource_size(io));
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001432
1433 kfree(atdma);
1434
1435 return 0;
1436}
1437
1438static void at_dma_shutdown(struct platform_device *pdev)
1439{
1440 struct at_dma *atdma = platform_get_drvdata(pdev);
1441
1442 at_dma_off(platform_get_drvdata(pdev));
1443 clk_disable(atdma->clk);
1444}
1445
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001446static int at_dma_prepare(struct device *dev)
1447{
1448 struct platform_device *pdev = to_platform_device(dev);
1449 struct at_dma *atdma = platform_get_drvdata(pdev);
1450 struct dma_chan *chan, *_chan;
1451
1452 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1453 device_node) {
1454 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1455 /* wait for transaction completion (except in cyclic case) */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001456 if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001457 return -EAGAIN;
1458 }
1459 return 0;
1460}
1461
1462static void atc_suspend_cyclic(struct at_dma_chan *atchan)
1463{
1464 struct dma_chan *chan = &atchan->chan_common;
1465
1466 /* Channel should be paused by user
1467 * do it anyway even if it is not done already */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001468 if (!atc_chan_is_paused(atchan)) {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001469 dev_warn(chan2dev(chan),
1470 "cyclic channel not paused, should be done by channel user\n");
1471 atc_control(chan, DMA_PAUSE, 0);
1472 }
1473
1474 /* now preserve additional data for cyclic operations */
1475 /* next descriptor address in the cyclic list */
1476 atchan->save_dscr = channel_readl(atchan, DSCR);
1477
1478 vdbg_dump_regs(atchan);
1479}
1480
Dan Williams33f82d12009-09-10 00:06:44 +02001481static int at_dma_suspend_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001482{
Dan Williams33f82d12009-09-10 00:06:44 +02001483 struct platform_device *pdev = to_platform_device(dev);
1484 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001485 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001486
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001487 /* preserve data */
1488 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1489 device_node) {
1490 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1491
Nicolas Ferre3c477482011-07-25 21:09:23 +00001492 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001493 atc_suspend_cyclic(atchan);
1494 atchan->save_cfg = channel_readl(atchan, CFG);
1495 }
1496 atdma->save_imr = dma_readl(atdma, EBCIMR);
1497
1498 /* disable DMA controller */
1499 at_dma_off(atdma);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001500 clk_disable(atdma->clk);
1501 return 0;
1502}
1503
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001504static void atc_resume_cyclic(struct at_dma_chan *atchan)
1505{
1506 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
1507
1508 /* restore channel status for cyclic descriptors list:
1509 * next descriptor in the cyclic list at the time of suspend */
1510 channel_writel(atchan, SADDR, 0);
1511 channel_writel(atchan, DADDR, 0);
1512 channel_writel(atchan, CTRLA, 0);
1513 channel_writel(atchan, CTRLB, 0);
1514 channel_writel(atchan, DSCR, atchan->save_dscr);
1515 dma_writel(atdma, CHER, atchan->mask);
1516
1517 /* channel pause status should be removed by channel user
1518 * We cannot take the initiative to do it here */
1519
1520 vdbg_dump_regs(atchan);
1521}
1522
Dan Williams33f82d12009-09-10 00:06:44 +02001523static int at_dma_resume_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001524{
Dan Williams33f82d12009-09-10 00:06:44 +02001525 struct platform_device *pdev = to_platform_device(dev);
1526 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001527 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001528
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001529 /* bring back DMA controller */
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001530 clk_enable(atdma->clk);
1531 dma_writel(atdma, EN, AT_DMA_ENABLE);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001532
1533 /* clear any pending interrupt */
1534 while (dma_readl(atdma, EBCISR))
1535 cpu_relax();
1536
1537 /* restore saved data */
1538 dma_writel(atdma, EBCIER, atdma->save_imr);
1539 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1540 device_node) {
1541 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1542
1543 channel_writel(atchan, CFG, atchan->save_cfg);
Nicolas Ferre3c477482011-07-25 21:09:23 +00001544 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001545 atc_resume_cyclic(atchan);
1546 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001547 return 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001548}
1549
Alexey Dobriyan47145212009-12-14 18:00:08 -08001550static const struct dev_pm_ops at_dma_dev_pm_ops = {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001551 .prepare = at_dma_prepare,
Dan Williams33f82d12009-09-10 00:06:44 +02001552 .suspend_noirq = at_dma_suspend_noirq,
1553 .resume_noirq = at_dma_resume_noirq,
1554};
1555
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001556static struct platform_driver at_dma_driver = {
1557 .remove = __exit_p(at_dma_remove),
1558 .shutdown = at_dma_shutdown,
Nicolas Ferre67348452011-10-17 14:56:40 +02001559 .id_table = atdma_devtypes,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001560 .driver = {
1561 .name = "at_hdmac",
Dan Williams33f82d12009-09-10 00:06:44 +02001562 .pm = &at_dma_dev_pm_ops,
Nicolas Ferrec5115952011-10-17 14:56:41 +02001563 .of_match_table = of_match_ptr(atmel_dma_dt_ids),
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001564 },
1565};
1566
1567static int __init at_dma_init(void)
1568{
1569 return platform_driver_probe(&at_dma_driver, at_dma_probe);
1570}
Eric Xu93d0bec2011-01-12 15:39:08 +01001571subsys_initcall(at_dma_init);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001572
1573static void __exit at_dma_exit(void)
1574{
1575 platform_driver_unregister(&at_dma_driver);
1576}
1577module_exit(at_dma_exit);
1578
1579MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1580MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1581MODULE_LICENSE("GPL");
1582MODULE_ALIAS("platform:at_hdmac");