blob: 5ce89368a8db28a76d1b1e593298da6b9bfbdf23 [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 *
Nicolas Ferre9102d872012-06-12 10:44:55 +020012 * This supports the Atmel AHB DMA Controller found in several Atmel SoCs.
13 * The only Atmel DMA Controller that is not covered by this driver is the one
14 * found on AT91SAM9263.
Nicolas Ferredc78baa2009-07-03 19:24:33 +020015 */
16
17#include <linux/clk.h>
18#include <linux/dmaengine.h>
19#include <linux/dma-mapping.h>
20#include <linux/dmapool.h>
21#include <linux/interrupt.h>
22#include <linux/module.h>
23#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Nicolas Ferrec5115952011-10-17 14:56:41 +020025#include <linux/of.h>
26#include <linux/of_device.h>
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +000027#include <linux/of_dma.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);
Elen Songd48de6f2013-05-10 11:01:46 +080057static void atc_issue_pending(struct dma_chan *chan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +020058
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/**
Masanari Iidad73111c2012-08-04 23:37:53 +0900173 * atc_desc_chain - build chain adding a descriptor
174 * @first: address of first descriptor of the chain
175 * @prev: address of previous descriptor of the chain
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200176 * @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
Elen Songd48de6f2013-05-10 11:01:46 +0800234/*
235 * atc_get_current_descriptors -
236 * locate the descriptor which equal to physical address in DSCR
237 * @atchan: the channel we want to start
238 * @dscr_addr: physical descriptor address in DSCR
239 */
240static struct at_desc *atc_get_current_descriptors(struct at_dma_chan *atchan,
241 u32 dscr_addr)
242{
243 struct at_desc *desc, *_desc, *child, *desc_cur = NULL;
244
245 list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
246 if (desc->lli.dscr == dscr_addr) {
247 desc_cur = desc;
248 break;
249 }
250
251 list_for_each_entry(child, &desc->tx_list, desc_node) {
252 if (child->lli.dscr == dscr_addr) {
253 desc_cur = child;
254 break;
255 }
256 }
257 }
258
259 return desc_cur;
260}
261
262/*
263 * atc_get_bytes_left -
264 * Get the number of bytes residue in dma buffer,
265 * @chan: the channel we want to start
266 */
267static int atc_get_bytes_left(struct dma_chan *chan)
268{
269 struct at_dma_chan *atchan = to_at_dma_chan(chan);
270 struct at_dma *atdma = to_at_dma(chan->device);
271 int chan_id = atchan->chan_common.chan_id;
272 struct at_desc *desc_first = atc_first_active(atchan);
273 struct at_desc *desc_cur;
274 int ret = 0, count = 0;
275
276 /*
277 * Initialize necessary values in the first time.
278 * remain_desc record remain desc length.
279 */
280 if (atchan->remain_desc == 0)
281 /* First descriptor embedds the transaction length */
282 atchan->remain_desc = desc_first->len;
283
284 /*
285 * This happens when current descriptor transfer complete.
286 * The residual buffer size should reduce current descriptor length.
287 */
288 if (unlikely(test_bit(ATC_IS_BTC, &atchan->status))) {
289 clear_bit(ATC_IS_BTC, &atchan->status);
290 desc_cur = atc_get_current_descriptors(atchan,
291 channel_readl(atchan, DSCR));
292 if (!desc_cur) {
293 ret = -EINVAL;
294 goto out;
295 }
296 atchan->remain_desc -= (desc_cur->lli.ctrla & ATC_BTSIZE_MAX)
297 << (desc_first->tx_width);
298 if (atchan->remain_desc < 0) {
299 ret = -EINVAL;
300 goto out;
301 } else
302 ret = atchan->remain_desc;
303 } else {
304 /*
305 * Get residual bytes when current
306 * descriptor transfer in progress.
307 */
308 count = (channel_readl(atchan, CTRLA) & ATC_BTSIZE_MAX)
309 << (desc_first->tx_width);
310 ret = atchan->remain_desc - count;
311 }
312 /*
313 * Check fifo empty.
314 */
315 if (!(dma_readl(atdma, CHSR) & AT_DMA_EMPT(chan_id)))
316 atc_issue_pending(chan);
317
318out:
319 return ret;
320}
321
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200322/**
323 * atc_chain_complete - finish work for one transaction chain
324 * @atchan: channel we work on
325 * @desc: descriptor at the head of the chain we want do complete
326 *
327 * Called with atchan->lock held and bh disabled */
328static void
329atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
330{
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200331 struct dma_async_tx_descriptor *txd = &desc->txd;
332
333 dev_vdbg(chan2dev(&atchan->chan_common),
334 "descriptor %u complete\n", txd->cookie);
335
Vinod Kould4116052012-05-11 11:48:21 +0530336 /* mark the descriptor as complete for non cyclic cases only */
337 if (!atc_chan_is_cyclic(atchan))
338 dma_cookie_complete(txd);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200339
340 /* move children to free_list */
Dan Williams285a3c72009-09-08 17:53:03 -0700341 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200342 /* move myself to free_list */
343 list_move(&desc->desc_node, &atchan->free_list);
344
Nicolas Ferreebcf9b82011-01-12 15:39:06 +0100345 /* unmap dma addresses (not on slave channels) */
Atsushi Nemoto657a77fa2009-09-08 17:53:05 -0700346 if (!atchan->chan_common.private) {
347 struct device *parent = chan2parent(&atchan->chan_common);
348 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
349 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
350 dma_unmap_single(parent,
351 desc->lli.daddr,
352 desc->len, DMA_FROM_DEVICE);
353 else
354 dma_unmap_page(parent,
355 desc->lli.daddr,
356 desc->len, DMA_FROM_DEVICE);
357 }
358 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
359 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
360 dma_unmap_single(parent,
361 desc->lli.saddr,
362 desc->len, DMA_TO_DEVICE);
363 else
364 dma_unmap_page(parent,
365 desc->lli.saddr,
366 desc->len, DMA_TO_DEVICE);
367 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200368 }
369
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200370 /* for cyclic transfers,
371 * no need to replay callback function while stopping */
Nicolas Ferre3c477482011-07-25 21:09:23 +0000372 if (!atc_chan_is_cyclic(atchan)) {
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200373 dma_async_tx_callback callback = txd->callback;
374 void *param = txd->callback_param;
375
376 /*
377 * The API requires that no submissions are done from a
378 * callback, so we don't need to drop the lock here
379 */
380 if (callback)
381 callback(param);
382 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200383
384 dma_run_dependencies(txd);
385}
386
387/**
388 * atc_complete_all - finish work for all transactions
389 * @atchan: channel to complete transactions for
390 *
391 * Eventually submit queued descriptors if any
392 *
393 * Assume channel is idle while calling this function
394 * Called with atchan->lock held and bh disabled
395 */
396static void atc_complete_all(struct at_dma_chan *atchan)
397{
398 struct at_desc *desc, *_desc;
399 LIST_HEAD(list);
400
401 dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
402
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200403 /*
404 * Submit queued descriptors ASAP, i.e. before we go through
405 * the completed ones.
406 */
407 if (!list_empty(&atchan->queue))
408 atc_dostart(atchan, atc_first_queued(atchan));
409 /* empty active_list now it is completed */
410 list_splice_init(&atchan->active_list, &list);
411 /* empty queue list by moving descriptors (if any) to active_list */
412 list_splice_init(&atchan->queue, &atchan->active_list);
413
414 list_for_each_entry_safe(desc, _desc, &list, desc_node)
415 atc_chain_complete(atchan, desc);
416}
417
418/**
419 * atc_cleanup_descriptors - cleanup up finished descriptors in active_list
420 * @atchan: channel to be cleaned up
421 *
422 * Called with atchan->lock held and bh disabled
423 */
424static void atc_cleanup_descriptors(struct at_dma_chan *atchan)
425{
426 struct at_desc *desc, *_desc;
427 struct at_desc *child;
428
429 dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n");
430
431 list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
432 if (!(desc->lli.ctrla & ATC_DONE))
433 /* This one is currently in progress */
434 return;
435
Dan Williams285a3c72009-09-08 17:53:03 -0700436 list_for_each_entry(child, &desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200437 if (!(child->lli.ctrla & ATC_DONE))
438 /* Currently in progress */
439 return;
440
441 /*
442 * No descriptors so far seem to be in progress, i.e.
443 * this chain must be done.
444 */
445 atc_chain_complete(atchan, desc);
446 }
447}
448
449/**
450 * atc_advance_work - at the end of a transaction, move forward
451 * @atchan: channel where the transaction ended
452 *
453 * Called with atchan->lock held and bh disabled
454 */
455static void atc_advance_work(struct at_dma_chan *atchan)
456{
457 dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
458
Ludovic Desrochesd202f052013-04-18 09:52:59 +0200459 if (atc_chan_is_enabled(atchan))
460 return;
461
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200462 if (list_empty(&atchan->active_list) ||
463 list_is_singular(&atchan->active_list)) {
464 atc_complete_all(atchan);
465 } else {
466 atc_chain_complete(atchan, atc_first_active(atchan));
467 /* advance work */
468 atc_dostart(atchan, atc_first_active(atchan));
469 }
470}
471
472
473/**
474 * atc_handle_error - handle errors reported by DMA controller
475 * @atchan: channel where error occurs
476 *
477 * Called with atchan->lock held and bh disabled
478 */
479static void atc_handle_error(struct at_dma_chan *atchan)
480{
481 struct at_desc *bad_desc;
482 struct at_desc *child;
483
484 /*
485 * The descriptor currently at the head of the active list is
486 * broked. Since we don't have any way to report errors, we'll
487 * just have to scream loudly and try to carry on.
488 */
489 bad_desc = atc_first_active(atchan);
490 list_del_init(&bad_desc->desc_node);
491
492 /* As we are stopped, take advantage to push queued descriptors
493 * in active_list */
494 list_splice_init(&atchan->queue, atchan->active_list.prev);
495
496 /* Try to restart the controller */
497 if (!list_empty(&atchan->active_list))
498 atc_dostart(atchan, atc_first_active(atchan));
499
500 /*
501 * KERN_CRITICAL may seem harsh, but since this only happens
502 * when someone submits a bad physical address in a
503 * descriptor, we should consider ourselves lucky that the
504 * controller flagged an error instead of scribbling over
505 * random memory locations.
506 */
507 dev_crit(chan2dev(&atchan->chan_common),
508 "Bad descriptor submitted for DMA!\n");
509 dev_crit(chan2dev(&atchan->chan_common),
510 " cookie: %d\n", bad_desc->txd.cookie);
511 atc_dump_lli(atchan, &bad_desc->lli);
Dan Williams285a3c72009-09-08 17:53:03 -0700512 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200513 atc_dump_lli(atchan, &child->lli);
514
515 /* Pretend the descriptor completed successfully */
516 atc_chain_complete(atchan, bad_desc);
517}
518
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200519/**
520 * atc_handle_cyclic - at the end of a period, run callback function
521 * @atchan: channel used for cyclic operations
522 *
523 * Called with atchan->lock held and bh disabled
524 */
525static void atc_handle_cyclic(struct at_dma_chan *atchan)
526{
527 struct at_desc *first = atc_first_active(atchan);
528 struct dma_async_tx_descriptor *txd = &first->txd;
529 dma_async_tx_callback callback = txd->callback;
530 void *param = txd->callback_param;
531
532 dev_vdbg(chan2dev(&atchan->chan_common),
533 "new cyclic period llp 0x%08x\n",
534 channel_readl(atchan, DSCR));
535
536 if (callback)
537 callback(param);
538}
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200539
540/*-- IRQ & Tasklet ---------------------------------------------------*/
541
542static void atc_tasklet(unsigned long data)
543{
544 struct at_dma_chan *atchan = (struct at_dma_chan *)data;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000545 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200546
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000547 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200548 if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200549 atc_handle_error(atchan);
Nicolas Ferre3c477482011-07-25 21:09:23 +0000550 else if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200551 atc_handle_cyclic(atchan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200552 else
553 atc_advance_work(atchan);
554
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000555 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200556}
557
558static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
559{
560 struct at_dma *atdma = (struct at_dma *)dev_id;
561 struct at_dma_chan *atchan;
562 int i;
563 u32 status, pending, imr;
564 int ret = IRQ_NONE;
565
566 do {
567 imr = dma_readl(atdma, EBCIMR);
568 status = dma_readl(atdma, EBCISR);
569 pending = status & imr;
570
571 if (!pending)
572 break;
573
574 dev_vdbg(atdma->dma_common.dev,
575 "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
576 status, imr, pending);
577
578 for (i = 0; i < atdma->dma_common.chancnt; i++) {
579 atchan = &atdma->chan[i];
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200580 if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200581 if (pending & AT_DMA_ERR(i)) {
582 /* Disable channel on AHB error */
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200583 dma_writel(atdma, CHDR,
584 AT_DMA_RES(i) | atchan->mask);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200585 /* Give information to tasklet */
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200586 set_bit(ATC_IS_ERROR, &atchan->status);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200587 }
Elen Songd48de6f2013-05-10 11:01:46 +0800588 if (pending & AT_DMA_BTC(i))
589 set_bit(ATC_IS_BTC, &atchan->status);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200590 tasklet_schedule(&atchan->tasklet);
591 ret = IRQ_HANDLED;
592 }
593 }
594
595 } while (pending);
596
597 return ret;
598}
599
600
601/*-- DMA Engine API --------------------------------------------------*/
602
603/**
604 * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
605 * @desc: descriptor at the head of the transaction chain
606 *
607 * Queue chain if DMA engine is working already
608 *
609 * Cookie increment and adding to active_list or queue must be atomic
610 */
611static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
612{
613 struct at_desc *desc = txd_to_at_desc(tx);
614 struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
615 dma_cookie_t cookie;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000616 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200617
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000618 spin_lock_irqsave(&atchan->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000619 cookie = dma_cookie_assign(tx);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200620
621 if (list_empty(&atchan->active_list)) {
622 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
623 desc->txd.cookie);
624 atc_dostart(atchan, desc);
625 list_add_tail(&desc->desc_node, &atchan->active_list);
626 } else {
627 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
628 desc->txd.cookie);
629 list_add_tail(&desc->desc_node, &atchan->queue);
630 }
631
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000632 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200633
634 return cookie;
635}
636
637/**
638 * atc_prep_dma_memcpy - prepare a memcpy operation
639 * @chan: the channel to prepare operation on
640 * @dest: operation virtual destination address
641 * @src: operation virtual source address
642 * @len: operation length
643 * @flags: tx descriptor status flags
644 */
645static struct dma_async_tx_descriptor *
646atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
647 size_t len, unsigned long flags)
648{
649 struct at_dma_chan *atchan = to_at_dma_chan(chan);
650 struct at_desc *desc = NULL;
651 struct at_desc *first = NULL;
652 struct at_desc *prev = NULL;
653 size_t xfer_count;
654 size_t offset;
655 unsigned int src_width;
656 unsigned int dst_width;
657 u32 ctrla;
658 u32 ctrlb;
659
660 dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
661 dest, src, len, flags);
662
663 if (unlikely(!len)) {
664 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
665 return NULL;
666 }
667
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200668 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200669 | ATC_SRC_ADDR_MODE_INCR
670 | ATC_DST_ADDR_MODE_INCR
671 | ATC_FC_MEM2MEM;
672
673 /*
674 * We can be a lot more clever here, but this should take care
675 * of the most common optimization.
676 */
677 if (!((src | dest | len) & 3)) {
Nicolas Ferreb409ebf2012-05-10 12:17:40 +0200678 ctrla = ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200679 src_width = dst_width = 2;
680 } else if (!((src | dest | len) & 1)) {
Nicolas Ferreb409ebf2012-05-10 12:17:40 +0200681 ctrla = ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200682 src_width = dst_width = 1;
683 } else {
Nicolas Ferreb409ebf2012-05-10 12:17:40 +0200684 ctrla = ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200685 src_width = dst_width = 0;
686 }
687
688 for (offset = 0; offset < len; offset += xfer_count << src_width) {
689 xfer_count = min_t(size_t, (len - offset) >> src_width,
690 ATC_BTSIZE_MAX);
691
692 desc = atc_desc_get(atchan);
693 if (!desc)
694 goto err_desc_get;
695
696 desc->lli.saddr = src + offset;
697 desc->lli.daddr = dest + offset;
698 desc->lli.ctrla = ctrla | xfer_count;
699 desc->lli.ctrlb = ctrlb;
700
701 desc->txd.cookie = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200702
Nicolas Ferree257e152011-05-06 19:56:53 +0200703 atc_desc_chain(&first, &prev, desc);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200704 }
705
706 /* First descriptor of the chain embedds additional information */
707 first->txd.cookie = -EBUSY;
708 first->len = len;
Elen Songd088c332013-05-10 11:00:50 +0800709 first->tx_width = src_width;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200710
711 /* set end-of-link to the last link descriptor of list*/
712 set_desc_eol(desc);
713
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100714 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200715
716 return &first->txd;
717
718err_desc_get:
719 atc_desc_put(atchan, first);
720 return NULL;
721}
722
Nicolas Ferre808347f2009-07-22 20:04:45 +0200723
724/**
725 * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
726 * @chan: DMA channel
727 * @sgl: scatterlist to transfer to/from
728 * @sg_len: number of entries in @scatterlist
729 * @direction: DMA direction
730 * @flags: tx descriptor status flags
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500731 * @context: transaction context (ignored)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200732 */
733static struct dma_async_tx_descriptor *
734atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530735 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500736 unsigned long flags, void *context)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200737{
738 struct at_dma_chan *atchan = to_at_dma_chan(chan);
739 struct at_dma_slave *atslave = chan->private;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100740 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200741 struct at_desc *first = NULL;
742 struct at_desc *prev = NULL;
743 u32 ctrla;
744 u32 ctrlb;
745 dma_addr_t reg;
746 unsigned int reg_width;
747 unsigned int mem_width;
748 unsigned int i;
749 struct scatterlist *sg;
750 size_t total_len = 0;
751
Nicolas Ferrecc52a102011-04-30 16:57:47 +0200752 dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
753 sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530754 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre808347f2009-07-22 20:04:45 +0200755 flags);
756
757 if (unlikely(!atslave || !sg_len)) {
Nicolas Ferrec618a9b2012-09-11 17:21:44 +0200758 dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n");
Nicolas Ferre808347f2009-07-22 20:04:45 +0200759 return NULL;
760 }
761
Nicolas Ferre1dd1ea82012-05-10 12:17:41 +0200762 ctrla = ATC_SCSIZE(sconfig->src_maxburst)
763 | ATC_DCSIZE(sconfig->dst_maxburst);
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200764 ctrlb = ATC_IEN;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200765
766 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530767 case DMA_MEM_TO_DEV:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100768 reg_width = convert_buswidth(sconfig->dst_addr_width);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200769 ctrla |= ATC_DST_WIDTH(reg_width);
770 ctrlb |= ATC_DST_ADDR_MODE_FIXED
771 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200772 | ATC_FC_MEM2PER
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +0000773 | ATC_SIF(atchan->mem_if) | ATC_DIF(atchan->per_if);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100774 reg = sconfig->dst_addr;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200775 for_each_sg(sgl, sg, sg_len, i) {
776 struct at_desc *desc;
777 u32 len;
778 u32 mem;
779
780 desc = atc_desc_get(atchan);
781 if (!desc)
782 goto err_desc_get;
783
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100784 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200785 len = sg_dma_len(sg);
Nicolas Ferrec4567972012-09-11 17:21:45 +0200786 if (unlikely(!len)) {
787 dev_dbg(chan2dev(chan),
788 "prep_slave_sg: sg(%d) data length is zero\n", i);
789 goto err;
790 }
Nicolas Ferre808347f2009-07-22 20:04:45 +0200791 mem_width = 2;
792 if (unlikely(mem & 3 || len & 3))
793 mem_width = 0;
794
795 desc->lli.saddr = mem;
796 desc->lli.daddr = reg;
797 desc->lli.ctrla = ctrla
798 | ATC_SRC_WIDTH(mem_width)
799 | len >> mem_width;
800 desc->lli.ctrlb = ctrlb;
801
Nicolas Ferree257e152011-05-06 19:56:53 +0200802 atc_desc_chain(&first, &prev, desc);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200803 total_len += len;
804 }
805 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530806 case DMA_DEV_TO_MEM:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100807 reg_width = convert_buswidth(sconfig->src_addr_width);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200808 ctrla |= ATC_SRC_WIDTH(reg_width);
809 ctrlb |= ATC_DST_ADDR_MODE_INCR
810 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200811 | ATC_FC_PER2MEM
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +0000812 | ATC_SIF(atchan->per_if) | ATC_DIF(atchan->mem_if);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200813
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100814 reg = sconfig->src_addr;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200815 for_each_sg(sgl, sg, sg_len, i) {
816 struct at_desc *desc;
817 u32 len;
818 u32 mem;
819
820 desc = atc_desc_get(atchan);
821 if (!desc)
822 goto err_desc_get;
823
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100824 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200825 len = sg_dma_len(sg);
Nicolas Ferrec4567972012-09-11 17:21:45 +0200826 if (unlikely(!len)) {
827 dev_dbg(chan2dev(chan),
828 "prep_slave_sg: sg(%d) data length is zero\n", i);
829 goto err;
830 }
Nicolas Ferre808347f2009-07-22 20:04:45 +0200831 mem_width = 2;
832 if (unlikely(mem & 3 || len & 3))
833 mem_width = 0;
834
835 desc->lli.saddr = reg;
836 desc->lli.daddr = mem;
837 desc->lli.ctrla = ctrla
838 | ATC_DST_WIDTH(mem_width)
Nicolas Ferre59a609d2010-12-13 13:48:41 +0100839 | len >> reg_width;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200840 desc->lli.ctrlb = ctrlb;
841
Nicolas Ferree257e152011-05-06 19:56:53 +0200842 atc_desc_chain(&first, &prev, desc);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200843 total_len += len;
844 }
845 break;
846 default:
847 return NULL;
848 }
849
850 /* set end-of-link to the last link descriptor of list*/
851 set_desc_eol(prev);
852
853 /* First descriptor of the chain embedds additional information */
854 first->txd.cookie = -EBUSY;
855 first->len = total_len;
Elen Songd088c332013-05-10 11:00:50 +0800856 first->tx_width = reg_width;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200857
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100858 /* first link descriptor of list is responsible of flags */
859 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferre808347f2009-07-22 20:04:45 +0200860
861 return &first->txd;
862
863err_desc_get:
864 dev_err(chan2dev(chan), "not enough descriptors available\n");
Nicolas Ferrec4567972012-09-11 17:21:45 +0200865err:
Nicolas Ferre808347f2009-07-22 20:04:45 +0200866 atc_desc_put(atchan, first);
867 return NULL;
868}
869
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200870/**
871 * atc_dma_cyclic_check_values
872 * Check for too big/unaligned periods and unaligned DMA buffer
873 */
874static int
875atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
Andy Shevchenko0e7264c2013-01-10 10:52:57 +0200876 size_t period_len)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200877{
878 if (period_len > (ATC_BTSIZE_MAX << reg_width))
879 goto err_out;
880 if (unlikely(period_len & ((1 << reg_width) - 1)))
881 goto err_out;
882 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
883 goto err_out;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200884
885 return 0;
886
887err_out:
888 return -EINVAL;
889}
890
891/**
Masanari Iidad73111c2012-08-04 23:37:53 +0900892 * atc_dma_cyclic_fill_desc - Fill one period descriptor
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200893 */
894static int
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100895atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200896 unsigned int period_index, dma_addr_t buf_addr,
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100897 unsigned int reg_width, size_t period_len,
898 enum dma_transfer_direction direction)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200899{
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100900 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100901 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
902 u32 ctrla;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200903
904 /* prepare common CRTLA value */
Nicolas Ferre1dd1ea82012-05-10 12:17:41 +0200905 ctrla = ATC_SCSIZE(sconfig->src_maxburst)
906 | ATC_DCSIZE(sconfig->dst_maxburst)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200907 | ATC_DST_WIDTH(reg_width)
908 | ATC_SRC_WIDTH(reg_width)
909 | period_len >> reg_width;
910
911 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530912 case DMA_MEM_TO_DEV:
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200913 desc->lli.saddr = buf_addr + (period_len * period_index);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100914 desc->lli.daddr = sconfig->dst_addr;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200915 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200916 desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200917 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200918 | ATC_FC_MEM2PER
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +0000919 | ATC_SIF(atchan->mem_if)
920 | ATC_DIF(atchan->per_if);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200921 break;
922
Vinod Kouldb8196d2011-10-13 22:34:23 +0530923 case DMA_DEV_TO_MEM:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100924 desc->lli.saddr = sconfig->src_addr;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200925 desc->lli.daddr = buf_addr + (period_len * period_index);
926 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200927 desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200928 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200929 | ATC_FC_PER2MEM
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +0000930 | ATC_SIF(atchan->per_if)
931 | ATC_DIF(atchan->mem_if);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200932 break;
933
934 default:
935 return -EINVAL;
936 }
937
938 return 0;
939}
940
941/**
942 * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
943 * @chan: the DMA channel to prepare
944 * @buf_addr: physical DMA address where the buffer starts
945 * @buf_len: total number of bytes for the entire buffer
946 * @period_len: number of bytes for each period
947 * @direction: transfer direction, to or from device
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +0300948 * @flags: tx descriptor status flags
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500949 * @context: transfer context (ignored)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200950 */
951static struct dma_async_tx_descriptor *
952atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500953 size_t period_len, enum dma_transfer_direction direction,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +0300954 unsigned long flags, void *context)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200955{
956 struct at_dma_chan *atchan = to_at_dma_chan(chan);
957 struct at_dma_slave *atslave = chan->private;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100958 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200959 struct at_desc *first = NULL;
960 struct at_desc *prev = NULL;
961 unsigned long was_cyclic;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100962 unsigned int reg_width;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200963 unsigned int periods = buf_len / period_len;
964 unsigned int i;
965
966 dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
Vinod Kouldb8196d2011-10-13 22:34:23 +0530967 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200968 buf_addr,
969 periods, buf_len, period_len);
970
971 if (unlikely(!atslave || !buf_len || !period_len)) {
972 dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
973 return NULL;
974 }
975
976 was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
977 if (was_cyclic) {
978 dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
979 return NULL;
980 }
981
Andy Shevchenko0e7264c2013-01-10 10:52:57 +0200982 if (unlikely(!is_slave_direction(direction)))
983 goto err_out;
984
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100985 if (sconfig->direction == DMA_MEM_TO_DEV)
986 reg_width = convert_buswidth(sconfig->dst_addr_width);
987 else
988 reg_width = convert_buswidth(sconfig->src_addr_width);
989
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200990 /* Check for too big/unaligned periods and unaligned DMA buffer */
Andy Shevchenko0e7264c2013-01-10 10:52:57 +0200991 if (atc_dma_cyclic_check_values(reg_width, buf_addr, period_len))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200992 goto err_out;
993
994 /* build cyclic linked list */
995 for (i = 0; i < periods; i++) {
996 struct at_desc *desc;
997
998 desc = atc_desc_get(atchan);
999 if (!desc)
1000 goto err_desc_get;
1001
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001002 if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
1003 reg_width, period_len, direction))
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001004 goto err_desc_get;
1005
1006 atc_desc_chain(&first, &prev, desc);
1007 }
1008
1009 /* lets make a cyclic list */
1010 prev->lli.dscr = first->txd.phys;
1011
1012 /* First descriptor of the chain embedds additional information */
1013 first->txd.cookie = -EBUSY;
1014 first->len = buf_len;
Elen Songd088c332013-05-10 11:00:50 +08001015 first->tx_width = reg_width;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001016
1017 return &first->txd;
1018
1019err_desc_get:
1020 dev_err(chan2dev(chan), "not enough descriptors available\n");
1021 atc_desc_put(atchan, first);
1022err_out:
1023 clear_bit(ATC_IS_CYCLIC, &atchan->status);
1024 return NULL;
1025}
1026
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001027static int set_runtime_config(struct dma_chan *chan,
1028 struct dma_slave_config *sconfig)
1029{
1030 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1031
1032 /* Check if it is chan is configured for slave transfers */
1033 if (!chan->private)
1034 return -EINVAL;
1035
1036 memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
1037
1038 convert_burst(&atchan->dma_sconfig.src_maxburst);
1039 convert_burst(&atchan->dma_sconfig.dst_maxburst);
1040
1041 return 0;
1042}
1043
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001044
Linus Walleij05827632010-05-17 16:30:42 -07001045static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1046 unsigned long arg)
Nicolas Ferre808347f2009-07-22 20:04:45 +02001047{
1048 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1049 struct at_dma *atdma = to_at_dma(chan->device);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001050 int chan_id = atchan->chan_common.chan_id;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001051 unsigned long flags;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001052
Nicolas Ferre808347f2009-07-22 20:04:45 +02001053 LIST_HEAD(list);
1054
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001055 dev_vdbg(chan2dev(chan), "atc_control (%d)\n", cmd);
1056
1057 if (cmd == DMA_PAUSE) {
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001058 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001059
1060 dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001061 set_bit(ATC_IS_PAUSED, &atchan->status);
1062
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001063 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001064 } else if (cmd == DMA_RESUME) {
Nicolas Ferre3c477482011-07-25 21:09:23 +00001065 if (!atc_chan_is_paused(atchan))
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001066 return 0;
1067
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001068 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001069
1070 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
1071 clear_bit(ATC_IS_PAUSED, &atchan->status);
1072
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001073 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001074 } else if (cmd == DMA_TERMINATE_ALL) {
1075 struct at_desc *desc, *_desc;
1076 /*
1077 * This is only called when something went wrong elsewhere, so
1078 * we don't really care about the data. Just disable the
1079 * channel. We still have to poll the channel enable bit due
1080 * to AHB/HSB limitations.
1081 */
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001082 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001083
1084 /* disabling channel: must also remove suspend state */
1085 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
1086
1087 /* confirm that this channel is disabled */
1088 while (dma_readl(atdma, CHSR) & atchan->mask)
1089 cpu_relax();
1090
1091 /* active_list entries will end up before queued entries */
1092 list_splice_init(&atchan->queue, &list);
1093 list_splice_init(&atchan->active_list, &list);
1094
1095 /* Flush all pending and queued descriptors */
1096 list_for_each_entry_safe(desc, _desc, &list, desc_node)
1097 atc_chain_complete(atchan, desc);
1098
1099 clear_bit(ATC_IS_PAUSED, &atchan->status);
1100 /* if channel dedicated to cyclic operations, free it */
1101 clear_bit(ATC_IS_CYCLIC, &atchan->status);
1102
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001103 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001104 } else if (cmd == DMA_SLAVE_CONFIG) {
1105 return set_runtime_config(chan, (struct dma_slave_config *)arg);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001106 } else {
Linus Walleijc3635c72010-03-26 16:44:01 -07001107 return -ENXIO;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001108 }
Yong Wangb0ebeb92010-08-05 10:40:08 +08001109
Linus Walleijc3635c72010-03-26 16:44:01 -07001110 return 0;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001111}
1112
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001113/**
Linus Walleij07934482010-03-26 16:50:49 -07001114 * atc_tx_status - poll for transaction completion
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001115 * @chan: DMA channel
1116 * @cookie: transaction identifier to check status of
Linus Walleij07934482010-03-26 16:50:49 -07001117 * @txstate: if not %NULL updated with transaction state
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001118 *
Linus Walleij07934482010-03-26 16:50:49 -07001119 * If @txstate is passed in, upon return it reflect the driver
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001120 * internal state and can be used with dma_async_is_complete() to check
1121 * the status of multiple cookies without re-checking hardware state.
1122 */
1123static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -07001124atc_tx_status(struct dma_chan *chan,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001125 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -07001126 struct dma_tx_state *txstate)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001127{
1128 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001129 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001130 enum dma_status ret;
Elen Songd48de6f2013-05-10 11:01:46 +08001131 int bytes = 0;
1132
1133 ret = dma_cookie_status(chan, cookie, txstate);
1134 if (ret == DMA_SUCCESS)
1135 return ret;
1136 /*
1137 * There's no point calculating the residue if there's
1138 * no txstate to store the value.
1139 */
1140 if (!txstate)
1141 return DMA_ERROR;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001142
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001143 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001144
Elen Songd48de6f2013-05-10 11:01:46 +08001145 /* Get number of bytes left in the active transactions */
1146 bytes = atc_get_bytes_left(chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001147
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001148 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001149
Elen Songd48de6f2013-05-10 11:01:46 +08001150 if (unlikely(bytes < 0)) {
1151 dev_vdbg(chan2dev(chan), "get residual bytes error\n");
1152 return DMA_ERROR;
1153 } else
1154 dma_set_residue(txstate, bytes);
Nicolas Ferre543aabc2011-05-06 19:56:51 +02001155
Elen Songd48de6f2013-05-10 11:01:46 +08001156 dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d residue = %d\n",
1157 ret, cookie, bytes);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001158
1159 return ret;
1160}
1161
1162/**
1163 * atc_issue_pending - try to finish work
1164 * @chan: target DMA channel
1165 */
1166static void atc_issue_pending(struct dma_chan *chan)
1167{
1168 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001169 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001170
1171 dev_vdbg(chan2dev(chan), "issue_pending\n");
1172
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001173 /* Not needed for cyclic transfers */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001174 if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001175 return;
1176
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001177 spin_lock_irqsave(&atchan->lock, flags);
Ludovic Desrochesd202f052013-04-18 09:52:59 +02001178 atc_advance_work(atchan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001179 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001180}
1181
1182/**
1183 * atc_alloc_chan_resources - allocate resources for DMA channel
1184 * @chan: allocate descriptor resources for this channel
1185 * @client: current client requesting the channel be ready for requests
1186 *
1187 * return - the number of allocated descriptors
1188 */
1189static int atc_alloc_chan_resources(struct dma_chan *chan)
1190{
1191 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1192 struct at_dma *atdma = to_at_dma(chan->device);
1193 struct at_desc *desc;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001194 struct at_dma_slave *atslave;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001195 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001196 int i;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001197 u32 cfg;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001198 LIST_HEAD(tmp_list);
1199
1200 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
1201
1202 /* ASSERT: channel is idle */
1203 if (atc_chan_is_enabled(atchan)) {
1204 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
1205 return -EIO;
1206 }
1207
Nicolas Ferre808347f2009-07-22 20:04:45 +02001208 cfg = ATC_DEFAULT_CFG;
1209
1210 atslave = chan->private;
1211 if (atslave) {
1212 /*
1213 * We need controller-specific data to set up slave
1214 * transfers.
1215 */
1216 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
1217
Nicolas Ferreea7e7902013-05-10 15:19:13 +02001218 /* if cfg configuration specified take it instead of default */
Nicolas Ferre808347f2009-07-22 20:04:45 +02001219 if (atslave->cfg)
1220 cfg = atslave->cfg;
1221 }
1222
1223 /* have we already been set up?
1224 * reconfigure channel but no need to reallocate descriptors */
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001225 if (!list_empty(&atchan->free_list))
1226 return atchan->descs_allocated;
1227
1228 /* Allocate initial pool of descriptors */
1229 for (i = 0; i < init_nr_desc_per_channel; i++) {
1230 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
1231 if (!desc) {
1232 dev_err(atdma->dma_common.dev,
1233 "Only %d initial descriptors\n", i);
1234 break;
1235 }
1236 list_add_tail(&desc->desc_node, &tmp_list);
1237 }
1238
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001239 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001240 atchan->descs_allocated = i;
Elen Songd48de6f2013-05-10 11:01:46 +08001241 atchan->remain_desc = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001242 list_splice(&tmp_list, &atchan->free_list);
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001243 dma_cookie_init(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001244 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001245
1246 /* channel parameters */
Nicolas Ferre808347f2009-07-22 20:04:45 +02001247 channel_writel(atchan, CFG, cfg);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001248
1249 dev_dbg(chan2dev(chan),
1250 "alloc_chan_resources: allocated %d descriptors\n",
1251 atchan->descs_allocated);
1252
1253 return atchan->descs_allocated;
1254}
1255
1256/**
1257 * atc_free_chan_resources - free all channel resources
1258 * @chan: DMA channel
1259 */
1260static void atc_free_chan_resources(struct dma_chan *chan)
1261{
1262 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1263 struct at_dma *atdma = to_at_dma(chan->device);
1264 struct at_desc *desc, *_desc;
1265 LIST_HEAD(list);
1266
1267 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
1268 atchan->descs_allocated);
1269
1270 /* ASSERT: channel is idle */
1271 BUG_ON(!list_empty(&atchan->active_list));
1272 BUG_ON(!list_empty(&atchan->queue));
1273 BUG_ON(atc_chan_is_enabled(atchan));
1274
1275 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
1276 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1277 list_del(&desc->desc_node);
1278 /* free link descriptor */
1279 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
1280 }
1281 list_splice_init(&atchan->free_list, &list);
1282 atchan->descs_allocated = 0;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001283 atchan->status = 0;
Elen Songd48de6f2013-05-10 11:01:46 +08001284 atchan->remain_desc = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001285
1286 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
1287}
1288
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001289#ifdef CONFIG_OF
1290static bool at_dma_filter(struct dma_chan *chan, void *slave)
1291{
1292 struct at_dma_slave *atslave = slave;
1293
1294 if (atslave->dma_dev == chan->device->dev) {
1295 chan->private = atslave;
1296 return true;
1297 } else {
1298 return false;
1299 }
1300}
1301
1302static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
1303 struct of_dma *of_dma)
1304{
1305 struct dma_chan *chan;
1306 struct at_dma_chan *atchan;
1307 struct at_dma_slave *atslave;
1308 dma_cap_mask_t mask;
1309 unsigned int per_id;
1310 struct platform_device *dmac_pdev;
1311
1312 if (dma_spec->args_count != 2)
1313 return NULL;
1314
1315 dmac_pdev = of_find_device_by_node(dma_spec->np);
1316
1317 dma_cap_zero(mask);
1318 dma_cap_set(DMA_SLAVE, mask);
1319
1320 atslave = devm_kzalloc(&dmac_pdev->dev, sizeof(*atslave), GFP_KERNEL);
1321 if (!atslave)
1322 return NULL;
1323 /*
1324 * We can fill both SRC_PER and DST_PER, one of these fields will be
1325 * ignored depending on DMA transfer direction.
1326 */
1327 per_id = dma_spec->args[1];
Nicolas Ferre6c227702013-05-10 15:19:15 +02001328 atslave->cfg = ATC_FIFOCFG_HALFFIFO
1329 | ATC_DST_H2SEL_HW | ATC_SRC_H2SEL_HW
1330 | ATC_DST_PER_MSB(per_id) | ATC_DST_PER(per_id)
1331 | ATC_SRC_PER_MSB(per_id) | ATC_SRC_PER(per_id);
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001332 atslave->dma_dev = &dmac_pdev->dev;
1333
1334 chan = dma_request_channel(mask, at_dma_filter, atslave);
1335 if (!chan)
1336 return NULL;
1337
1338 atchan = to_at_dma_chan(chan);
1339 atchan->per_if = dma_spec->args[0] & 0xff;
1340 atchan->mem_if = (dma_spec->args[0] >> 16) & 0xff;
1341
1342 return chan;
1343}
1344#else
1345static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
1346 struct of_dma *of_dma)
1347{
1348 return NULL;
1349}
1350#endif
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001351
1352/*-- Module Management -----------------------------------------------*/
1353
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001354/* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
1355static struct at_dma_platform_data at91sam9rl_config = {
1356 .nr_channels = 2,
1357};
1358static struct at_dma_platform_data at91sam9g45_config = {
1359 .nr_channels = 8,
1360};
1361
Nicolas Ferrec5115952011-10-17 14:56:41 +02001362#if defined(CONFIG_OF)
1363static const struct of_device_id atmel_dma_dt_ids[] = {
1364 {
1365 .compatible = "atmel,at91sam9rl-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001366 .data = &at91sam9rl_config,
Nicolas Ferrec5115952011-10-17 14:56:41 +02001367 }, {
1368 .compatible = "atmel,at91sam9g45-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001369 .data = &at91sam9g45_config,
Nicolas Ferredcc81732011-11-22 11:55:53 +01001370 }, {
1371 /* sentinel */
1372 }
Nicolas Ferrec5115952011-10-17 14:56:41 +02001373};
1374
1375MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
1376#endif
1377
Nicolas Ferre0ab88a02011-11-22 11:55:52 +01001378static const struct platform_device_id atdma_devtypes[] = {
Nicolas Ferre67348452011-10-17 14:56:40 +02001379 {
1380 .name = "at91sam9rl_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001381 .driver_data = (unsigned long) &at91sam9rl_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001382 }, {
1383 .name = "at91sam9g45_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001384 .driver_data = (unsigned long) &at91sam9g45_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001385 }, {
1386 /* sentinel */
1387 }
1388};
1389
Uwe Kleine-König7fd63cc2012-07-13 14:32:10 +02001390static inline const struct at_dma_platform_data * __init at_dma_get_driver_data(
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001391 struct platform_device *pdev)
Nicolas Ferrec5115952011-10-17 14:56:41 +02001392{
1393 if (pdev->dev.of_node) {
1394 const struct of_device_id *match;
1395 match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
1396 if (match == NULL)
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001397 return NULL;
1398 return match->data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001399 }
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001400 return (struct at_dma_platform_data *)
1401 platform_get_device_id(pdev)->driver_data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001402}
1403
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001404/**
1405 * at_dma_off - disable DMA controller
1406 * @atdma: the Atmel HDAMC device
1407 */
1408static void at_dma_off(struct at_dma *atdma)
1409{
1410 dma_writel(atdma, EN, 0);
1411
1412 /* disable all interrupts */
1413 dma_writel(atdma, EBCIDR, -1L);
1414
1415 /* confirm that all channels are disabled */
1416 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
1417 cpu_relax();
1418}
1419
1420static int __init at_dma_probe(struct platform_device *pdev)
1421{
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001422 struct resource *io;
1423 struct at_dma *atdma;
1424 size_t size;
1425 int irq;
1426 int err;
1427 int i;
Uwe Kleine-König7fd63cc2012-07-13 14:32:10 +02001428 const struct at_dma_platform_data *plat_dat;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001429
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001430 /* setup platform data for each SoC */
1431 dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
1432 dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
1433 dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
Nicolas Ferre67348452011-10-17 14:56:40 +02001434
1435 /* get DMA parameters from controller type */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001436 plat_dat = at_dma_get_driver_data(pdev);
1437 if (!plat_dat)
1438 return -ENODEV;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001439
1440 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1441 if (!io)
1442 return -EINVAL;
1443
1444 irq = platform_get_irq(pdev, 0);
1445 if (irq < 0)
1446 return irq;
1447
1448 size = sizeof(struct at_dma);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001449 size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001450 atdma = kzalloc(size, GFP_KERNEL);
1451 if (!atdma)
1452 return -ENOMEM;
1453
Nicolas Ferre67348452011-10-17 14:56:40 +02001454 /* discover transaction capabilities */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001455 atdma->dma_common.cap_mask = plat_dat->cap_mask;
1456 atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001457
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07001458 size = resource_size(io);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001459 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1460 err = -EBUSY;
1461 goto err_kfree;
1462 }
1463
1464 atdma->regs = ioremap(io->start, size);
1465 if (!atdma->regs) {
1466 err = -ENOMEM;
1467 goto err_release_r;
1468 }
1469
1470 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1471 if (IS_ERR(atdma->clk)) {
1472 err = PTR_ERR(atdma->clk);
1473 goto err_clk;
1474 }
1475 clk_enable(atdma->clk);
1476
1477 /* force dma off, just in case */
1478 at_dma_off(atdma);
1479
1480 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1481 if (err)
1482 goto err_irq;
1483
1484 platform_set_drvdata(pdev, atdma);
1485
1486 /* create a pool of consistent memory blocks for hardware descriptors */
1487 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1488 &pdev->dev, sizeof(struct at_desc),
1489 4 /* word alignment */, 0);
1490 if (!atdma->dma_desc_pool) {
1491 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1492 err = -ENOMEM;
1493 goto err_pool_create;
1494 }
1495
1496 /* clear any pending interrupt */
1497 while (dma_readl(atdma, EBCISR))
1498 cpu_relax();
1499
1500 /* initialize channels related values */
1501 INIT_LIST_HEAD(&atdma->dma_common.channels);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001502 for (i = 0; i < plat_dat->nr_channels; i++) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001503 struct at_dma_chan *atchan = &atdma->chan[i];
1504
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001505 atchan->mem_if = AT_DMA_MEM_IF;
1506 atchan->per_if = AT_DMA_PER_IF;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001507 atchan->chan_common.device = &atdma->dma_common;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001508 dma_cookie_init(&atchan->chan_common);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001509 list_add_tail(&atchan->chan_common.device_node,
1510 &atdma->dma_common.channels);
1511
1512 atchan->ch_regs = atdma->regs + ch_regs(i);
1513 spin_lock_init(&atchan->lock);
1514 atchan->mask = 1 << i;
1515
1516 INIT_LIST_HEAD(&atchan->active_list);
1517 INIT_LIST_HEAD(&atchan->queue);
1518 INIT_LIST_HEAD(&atchan->free_list);
1519
1520 tasklet_init(&atchan->tasklet, atc_tasklet,
1521 (unsigned long)atchan);
Nikolaus Vossbda3a472012-01-17 10:28:33 +01001522 atc_enable_chan_irq(atdma, i);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001523 }
1524
1525 /* set base routines */
1526 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1527 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
Linus Walleij07934482010-03-26 16:50:49 -07001528 atdma->dma_common.device_tx_status = atc_tx_status;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001529 atdma->dma_common.device_issue_pending = atc_issue_pending;
1530 atdma->dma_common.dev = &pdev->dev;
1531
1532 /* set prep routines based on capability */
1533 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1534 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1535
Nicolas Ferred7db8082011-08-05 11:43:44 +00001536 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
Nicolas Ferre808347f2009-07-22 20:04:45 +02001537 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001538 /* controller can do slave DMA: can trigger cyclic transfers */
1539 dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001540 atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
Linus Walleijc3635c72010-03-26 16:44:01 -07001541 atdma->dma_common.device_control = atc_control;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001542 }
Nicolas Ferre808347f2009-07-22 20:04:45 +02001543
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001544 dma_writel(atdma, EN, AT_DMA_ENABLE);
1545
1546 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
1547 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1548 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001549 plat_dat->nr_channels);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001550
1551 dma_async_device_register(&atdma->dma_common);
1552
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001553 /*
1554 * Do not return an error if the dmac node is not present in order to
1555 * not break the existing way of requesting channel with
1556 * dma_request_channel().
1557 */
1558 if (pdev->dev.of_node) {
1559 err = of_dma_controller_register(pdev->dev.of_node,
1560 at_dma_xlate, atdma);
1561 if (err) {
1562 dev_err(&pdev->dev, "could not register of_dma_controller\n");
1563 goto err_of_dma_controller_register;
1564 }
1565 }
1566
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001567 return 0;
1568
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001569err_of_dma_controller_register:
1570 dma_async_device_unregister(&atdma->dma_common);
1571 dma_pool_destroy(atdma->dma_desc_pool);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001572err_pool_create:
1573 platform_set_drvdata(pdev, NULL);
1574 free_irq(platform_get_irq(pdev, 0), atdma);
1575err_irq:
1576 clk_disable(atdma->clk);
1577 clk_put(atdma->clk);
1578err_clk:
1579 iounmap(atdma->regs);
1580 atdma->regs = NULL;
1581err_release_r:
1582 release_mem_region(io->start, size);
1583err_kfree:
1584 kfree(atdma);
1585 return err;
1586}
1587
Maxin B. John1d1bbd32013-02-20 02:07:04 +02001588static int at_dma_remove(struct platform_device *pdev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001589{
1590 struct at_dma *atdma = platform_get_drvdata(pdev);
1591 struct dma_chan *chan, *_chan;
1592 struct resource *io;
1593
1594 at_dma_off(atdma);
1595 dma_async_device_unregister(&atdma->dma_common);
1596
1597 dma_pool_destroy(atdma->dma_desc_pool);
1598 platform_set_drvdata(pdev, NULL);
1599 free_irq(platform_get_irq(pdev, 0), atdma);
1600
1601 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1602 device_node) {
1603 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1604
1605 /* Disable interrupts */
Nikolaus Vossbda3a472012-01-17 10:28:33 +01001606 atc_disable_chan_irq(atdma, chan->chan_id);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001607 tasklet_disable(&atchan->tasklet);
1608
1609 tasklet_kill(&atchan->tasklet);
1610 list_del(&chan->device_node);
1611 }
1612
1613 clk_disable(atdma->clk);
1614 clk_put(atdma->clk);
1615
1616 iounmap(atdma->regs);
1617 atdma->regs = NULL;
1618
1619 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07001620 release_mem_region(io->start, resource_size(io));
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001621
1622 kfree(atdma);
1623
1624 return 0;
1625}
1626
1627static void at_dma_shutdown(struct platform_device *pdev)
1628{
1629 struct at_dma *atdma = platform_get_drvdata(pdev);
1630
1631 at_dma_off(platform_get_drvdata(pdev));
1632 clk_disable(atdma->clk);
1633}
1634
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001635static int at_dma_prepare(struct device *dev)
1636{
1637 struct platform_device *pdev = to_platform_device(dev);
1638 struct at_dma *atdma = platform_get_drvdata(pdev);
1639 struct dma_chan *chan, *_chan;
1640
1641 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1642 device_node) {
1643 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1644 /* wait for transaction completion (except in cyclic case) */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001645 if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001646 return -EAGAIN;
1647 }
1648 return 0;
1649}
1650
1651static void atc_suspend_cyclic(struct at_dma_chan *atchan)
1652{
1653 struct dma_chan *chan = &atchan->chan_common;
1654
1655 /* Channel should be paused by user
1656 * do it anyway even if it is not done already */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001657 if (!atc_chan_is_paused(atchan)) {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001658 dev_warn(chan2dev(chan),
1659 "cyclic channel not paused, should be done by channel user\n");
1660 atc_control(chan, DMA_PAUSE, 0);
1661 }
1662
1663 /* now preserve additional data for cyclic operations */
1664 /* next descriptor address in the cyclic list */
1665 atchan->save_dscr = channel_readl(atchan, DSCR);
1666
1667 vdbg_dump_regs(atchan);
1668}
1669
Dan Williams33f82d12009-09-10 00:06:44 +02001670static int at_dma_suspend_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001671{
Dan Williams33f82d12009-09-10 00:06:44 +02001672 struct platform_device *pdev = to_platform_device(dev);
1673 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001674 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001675
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001676 /* preserve data */
1677 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1678 device_node) {
1679 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1680
Nicolas Ferre3c477482011-07-25 21:09:23 +00001681 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001682 atc_suspend_cyclic(atchan);
1683 atchan->save_cfg = channel_readl(atchan, CFG);
1684 }
1685 atdma->save_imr = dma_readl(atdma, EBCIMR);
1686
1687 /* disable DMA controller */
1688 at_dma_off(atdma);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001689 clk_disable(atdma->clk);
1690 return 0;
1691}
1692
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001693static void atc_resume_cyclic(struct at_dma_chan *atchan)
1694{
1695 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
1696
1697 /* restore channel status for cyclic descriptors list:
1698 * next descriptor in the cyclic list at the time of suspend */
1699 channel_writel(atchan, SADDR, 0);
1700 channel_writel(atchan, DADDR, 0);
1701 channel_writel(atchan, CTRLA, 0);
1702 channel_writel(atchan, CTRLB, 0);
1703 channel_writel(atchan, DSCR, atchan->save_dscr);
1704 dma_writel(atdma, CHER, atchan->mask);
1705
1706 /* channel pause status should be removed by channel user
1707 * We cannot take the initiative to do it here */
1708
1709 vdbg_dump_regs(atchan);
1710}
1711
Dan Williams33f82d12009-09-10 00:06:44 +02001712static int at_dma_resume_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001713{
Dan Williams33f82d12009-09-10 00:06:44 +02001714 struct platform_device *pdev = to_platform_device(dev);
1715 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001716 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001717
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001718 /* bring back DMA controller */
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001719 clk_enable(atdma->clk);
1720 dma_writel(atdma, EN, AT_DMA_ENABLE);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001721
1722 /* clear any pending interrupt */
1723 while (dma_readl(atdma, EBCISR))
1724 cpu_relax();
1725
1726 /* restore saved data */
1727 dma_writel(atdma, EBCIER, atdma->save_imr);
1728 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1729 device_node) {
1730 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1731
1732 channel_writel(atchan, CFG, atchan->save_cfg);
Nicolas Ferre3c477482011-07-25 21:09:23 +00001733 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001734 atc_resume_cyclic(atchan);
1735 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001736 return 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001737}
1738
Alexey Dobriyan47145212009-12-14 18:00:08 -08001739static const struct dev_pm_ops at_dma_dev_pm_ops = {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001740 .prepare = at_dma_prepare,
Dan Williams33f82d12009-09-10 00:06:44 +02001741 .suspend_noirq = at_dma_suspend_noirq,
1742 .resume_noirq = at_dma_resume_noirq,
1743};
1744
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001745static struct platform_driver at_dma_driver = {
Maxin B. John1d1bbd32013-02-20 02:07:04 +02001746 .remove = at_dma_remove,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001747 .shutdown = at_dma_shutdown,
Nicolas Ferre67348452011-10-17 14:56:40 +02001748 .id_table = atdma_devtypes,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001749 .driver = {
1750 .name = "at_hdmac",
Dan Williams33f82d12009-09-10 00:06:44 +02001751 .pm = &at_dma_dev_pm_ops,
Nicolas Ferrec5115952011-10-17 14:56:41 +02001752 .of_match_table = of_match_ptr(atmel_dma_dt_ids),
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001753 },
1754};
1755
1756static int __init at_dma_init(void)
1757{
1758 return platform_driver_probe(&at_dma_driver, at_dma_probe);
1759}
Eric Xu93d0bec2011-01-12 15:39:08 +01001760subsys_initcall(at_dma_init);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001761
1762static void __exit at_dma_exit(void)
1763{
1764 platform_driver_unregister(&at_dma_driver);
1765}
1766module_exit(at_dma_exit);
1767
1768MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1769MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1770MODULE_LICENSE("GPL");
1771MODULE_ALIAS("platform:at_hdmac");