blob: 86450b3442f2f229519c6f1e0f82e960feaa68d1 [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
Ludovic Desroches62971b22013-06-13 10:39:39 +020017#include <dt-bindings/dma/at91.h>
Nicolas Ferredc78baa2009-07-03 19:24:33 +020018#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>
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +000028#include <linux/of_dma.h>
Nicolas Ferredc78baa2009-07-03 19:24:33 +020029
30#include "at_hdmac_regs.h"
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000031#include "dmaengine.h"
Nicolas Ferredc78baa2009-07-03 19:24:33 +020032
33/*
34 * Glossary
35 * --------
36 *
37 * at_hdmac : Name of the ATmel AHB DMA Controller
38 * at_dma_ / atdma : ATmel DMA controller entity related
39 * atc_ / atchan : ATmel DMA Channel entity related
40 */
41
42#define ATC_DEFAULT_CFG (ATC_FIFOCFG_HALFFIFO)
Nicolas Ferreae14d4b2011-04-30 16:57:49 +020043#define ATC_DEFAULT_CTRLB (ATC_SIF(AT_DMA_MEM_IF) \
44 |ATC_DIF(AT_DMA_MEM_IF))
Nicolas Ferredc78baa2009-07-03 19:24:33 +020045
46/*
47 * Initial number of descriptors to allocate for each channel. This could
48 * be increased during dma usage.
49 */
50static unsigned int init_nr_desc_per_channel = 64;
51module_param(init_nr_desc_per_channel, uint, 0644);
52MODULE_PARM_DESC(init_nr_desc_per_channel,
53 "initial descriptors per channel (default: 64)");
54
55
56/* prototypes */
57static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx);
Elen Songd48de6f2013-05-10 11:01:46 +080058static void atc_issue_pending(struct dma_chan *chan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +020059
60
61/*----------------------------------------------------------------------*/
62
63static struct at_desc *atc_first_active(struct at_dma_chan *atchan)
64{
65 return list_first_entry(&atchan->active_list,
66 struct at_desc, desc_node);
67}
68
69static struct at_desc *atc_first_queued(struct at_dma_chan *atchan)
70{
71 return list_first_entry(&atchan->queue,
72 struct at_desc, desc_node);
73}
74
75/**
Uwe Kleine-König421f91d2010-06-11 12:17:00 +020076 * atc_alloc_descriptor - allocate and return an initialized descriptor
Nicolas Ferredc78baa2009-07-03 19:24:33 +020077 * @chan: the channel to allocate descriptors for
78 * @gfp_flags: GFP allocation flags
79 *
80 * Note: The ack-bit is positioned in the descriptor flag at creation time
81 * to make initial allocation more convenient. This bit will be cleared
82 * and control will be given to client at usage time (during
83 * preparation functions).
84 */
85static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan,
86 gfp_t gfp_flags)
87{
88 struct at_desc *desc = NULL;
89 struct at_dma *atdma = to_at_dma(chan->device);
90 dma_addr_t phys;
91
92 desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys);
93 if (desc) {
94 memset(desc, 0, sizeof(struct at_desc));
Dan Williams285a3c72009-09-08 17:53:03 -070095 INIT_LIST_HEAD(&desc->tx_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +020096 dma_async_tx_descriptor_init(&desc->txd, chan);
97 /* txd.flags will be overwritten in prep functions */
98 desc->txd.flags = DMA_CTRL_ACK;
99 desc->txd.tx_submit = atc_tx_submit;
100 desc->txd.phys = phys;
101 }
102
103 return desc;
104}
105
106/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200107 * atc_desc_get - get an unused descriptor from free_list
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200108 * @atchan: channel we want a new descriptor for
109 */
110static struct at_desc *atc_desc_get(struct at_dma_chan *atchan)
111{
112 struct at_desc *desc, *_desc;
113 struct at_desc *ret = NULL;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000114 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200115 unsigned int i = 0;
116 LIST_HEAD(tmp_list);
117
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000118 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200119 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
120 i++;
121 if (async_tx_test_ack(&desc->txd)) {
122 list_del(&desc->desc_node);
123 ret = desc;
124 break;
125 }
126 dev_dbg(chan2dev(&atchan->chan_common),
127 "desc %p not ACKed\n", desc);
128 }
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000129 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200130 dev_vdbg(chan2dev(&atchan->chan_common),
131 "scanned %u descriptors on freelist\n", i);
132
133 /* no more descriptor available in initial pool: create one more */
134 if (!ret) {
135 ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC);
136 if (ret) {
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000137 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200138 atchan->descs_allocated++;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000139 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200140 } else {
141 dev_err(chan2dev(&atchan->chan_common),
142 "not enough descriptors available\n");
143 }
144 }
145
146 return ret;
147}
148
149/**
150 * atc_desc_put - move a descriptor, including any children, to the free list
151 * @atchan: channel we work on
152 * @desc: descriptor, at the head of a chain, to move to free list
153 */
154static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc)
155{
156 if (desc) {
157 struct at_desc *child;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000158 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200159
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000160 spin_lock_irqsave(&atchan->lock, flags);
Dan Williams285a3c72009-09-08 17:53:03 -0700161 list_for_each_entry(child, &desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200162 dev_vdbg(chan2dev(&atchan->chan_common),
163 "moving child desc %p to freelist\n",
164 child);
Dan Williams285a3c72009-09-08 17:53:03 -0700165 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200166 dev_vdbg(chan2dev(&atchan->chan_common),
167 "moving desc %p to freelist\n", desc);
168 list_add(&desc->desc_node, &atchan->free_list);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000169 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200170 }
171}
172
173/**
Masanari Iidad73111c2012-08-04 23:37:53 +0900174 * atc_desc_chain - build chain adding a descriptor
175 * @first: address of first descriptor of the chain
176 * @prev: address of previous descriptor of the chain
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200177 * @desc: descriptor to queue
178 *
179 * Called from prep_* functions
180 */
181static void atc_desc_chain(struct at_desc **first, struct at_desc **prev,
182 struct at_desc *desc)
183{
184 if (!(*first)) {
185 *first = desc;
186 } else {
187 /* inform the HW lli about chaining */
188 (*prev)->lli.dscr = desc->txd.phys;
189 /* insert the link descriptor to the LD ring */
190 list_add_tail(&desc->desc_node,
191 &(*first)->tx_list);
192 }
193 *prev = desc;
194}
195
196/**
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200197 * atc_dostart - starts the DMA engine for real
198 * @atchan: the channel we want to start
199 * @first: first descriptor in the list we want to begin with
200 *
201 * Called with atchan->lock held and bh disabled
202 */
203static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
204{
205 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
206
207 /* ASSERT: channel is idle */
208 if (atc_chan_is_enabled(atchan)) {
209 dev_err(chan2dev(&atchan->chan_common),
210 "BUG: Attempted to start non-idle channel\n");
211 dev_err(chan2dev(&atchan->chan_common),
212 " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
213 channel_readl(atchan, SADDR),
214 channel_readl(atchan, DADDR),
215 channel_readl(atchan, CTRLA),
216 channel_readl(atchan, CTRLB),
217 channel_readl(atchan, DSCR));
218
219 /* The tasklet will hopefully advance the queue... */
220 return;
221 }
222
223 vdbg_dump_regs(atchan);
224
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200225 channel_writel(atchan, SADDR, 0);
226 channel_writel(atchan, DADDR, 0);
227 channel_writel(atchan, CTRLA, 0);
228 channel_writel(atchan, CTRLB, 0);
229 channel_writel(atchan, DSCR, first->txd.phys);
230 dma_writel(atdma, CHER, atchan->mask);
231
232 vdbg_dump_regs(atchan);
233}
234
Elen Songd48de6f2013-05-10 11:01:46 +0800235/*
236 * atc_get_current_descriptors -
237 * locate the descriptor which equal to physical address in DSCR
238 * @atchan: the channel we want to start
239 * @dscr_addr: physical descriptor address in DSCR
240 */
241static struct at_desc *atc_get_current_descriptors(struct at_dma_chan *atchan,
242 u32 dscr_addr)
243{
244 struct at_desc *desc, *_desc, *child, *desc_cur = NULL;
245
246 list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
247 if (desc->lli.dscr == dscr_addr) {
248 desc_cur = desc;
249 break;
250 }
251
252 list_for_each_entry(child, &desc->tx_list, desc_node) {
253 if (child->lli.dscr == dscr_addr) {
254 desc_cur = child;
255 break;
256 }
257 }
258 }
259
260 return desc_cur;
261}
262
263/*
264 * atc_get_bytes_left -
265 * Get the number of bytes residue in dma buffer,
266 * @chan: the channel we want to start
267 */
268static int atc_get_bytes_left(struct dma_chan *chan)
269{
270 struct at_dma_chan *atchan = to_at_dma_chan(chan);
271 struct at_dma *atdma = to_at_dma(chan->device);
272 int chan_id = atchan->chan_common.chan_id;
273 struct at_desc *desc_first = atc_first_active(atchan);
274 struct at_desc *desc_cur;
275 int ret = 0, count = 0;
276
277 /*
278 * Initialize necessary values in the first time.
279 * remain_desc record remain desc length.
280 */
281 if (atchan->remain_desc == 0)
282 /* First descriptor embedds the transaction length */
283 atchan->remain_desc = desc_first->len;
284
285 /*
286 * This happens when current descriptor transfer complete.
287 * The residual buffer size should reduce current descriptor length.
288 */
289 if (unlikely(test_bit(ATC_IS_BTC, &atchan->status))) {
290 clear_bit(ATC_IS_BTC, &atchan->status);
291 desc_cur = atc_get_current_descriptors(atchan,
292 channel_readl(atchan, DSCR));
293 if (!desc_cur) {
294 ret = -EINVAL;
295 goto out;
296 }
Alexandre Belloni6758dda2014-08-01 18:51:46 +0200297
298 count = (desc_cur->lli.ctrla & ATC_BTSIZE_MAX)
299 << desc_first->tx_width;
300 if (atchan->remain_desc < count) {
Elen Songd48de6f2013-05-10 11:01:46 +0800301 ret = -EINVAL;
302 goto out;
Nicolas Ferrec3dbc602013-06-07 17:26:14 +0200303 }
Alexandre Belloni6758dda2014-08-01 18:51:46 +0200304
305 atchan->remain_desc -= count;
306 ret = atchan->remain_desc;
Elen Songd48de6f2013-05-10 11:01:46 +0800307 } else {
308 /*
309 * Get residual bytes when current
310 * descriptor transfer in progress.
311 */
312 count = (channel_readl(atchan, CTRLA) & ATC_BTSIZE_MAX)
313 << (desc_first->tx_width);
314 ret = atchan->remain_desc - count;
315 }
316 /*
317 * Check fifo empty.
318 */
319 if (!(dma_readl(atdma, CHSR) & AT_DMA_EMPT(chan_id)))
320 atc_issue_pending(chan);
321
322out:
323 return ret;
324}
325
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200326/**
327 * atc_chain_complete - finish work for one transaction chain
328 * @atchan: channel we work on
329 * @desc: descriptor at the head of the chain we want do complete
330 *
331 * Called with atchan->lock held and bh disabled */
332static void
333atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
334{
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200335 struct dma_async_tx_descriptor *txd = &desc->txd;
336
337 dev_vdbg(chan2dev(&atchan->chan_common),
338 "descriptor %u complete\n", txd->cookie);
339
Vinod Kould4116052012-05-11 11:48:21 +0530340 /* mark the descriptor as complete for non cyclic cases only */
341 if (!atc_chan_is_cyclic(atchan))
342 dma_cookie_complete(txd);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200343
344 /* move children to free_list */
Dan Williams285a3c72009-09-08 17:53:03 -0700345 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200346 /* move myself to free_list */
347 list_move(&desc->desc_node, &atchan->free_list);
348
Dan Williamsd38a8c62013-10-18 19:35:23 +0200349 dma_descriptor_unmap(txd);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200350 /* for cyclic transfers,
351 * no need to replay callback function while stopping */
Nicolas Ferre3c477482011-07-25 21:09:23 +0000352 if (!atc_chan_is_cyclic(atchan)) {
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200353 dma_async_tx_callback callback = txd->callback;
354 void *param = txd->callback_param;
355
356 /*
357 * The API requires that no submissions are done from a
358 * callback, so we don't need to drop the lock here
359 */
360 if (callback)
361 callback(param);
362 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200363
364 dma_run_dependencies(txd);
365}
366
367/**
368 * atc_complete_all - finish work for all transactions
369 * @atchan: channel to complete transactions for
370 *
371 * Eventually submit queued descriptors if any
372 *
373 * Assume channel is idle while calling this function
374 * Called with atchan->lock held and bh disabled
375 */
376static void atc_complete_all(struct at_dma_chan *atchan)
377{
378 struct at_desc *desc, *_desc;
379 LIST_HEAD(list);
380
381 dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
382
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200383 /*
384 * Submit queued descriptors ASAP, i.e. before we go through
385 * the completed ones.
386 */
387 if (!list_empty(&atchan->queue))
388 atc_dostart(atchan, atc_first_queued(atchan));
389 /* empty active_list now it is completed */
390 list_splice_init(&atchan->active_list, &list);
391 /* empty queue list by moving descriptors (if any) to active_list */
392 list_splice_init(&atchan->queue, &atchan->active_list);
393
394 list_for_each_entry_safe(desc, _desc, &list, desc_node)
395 atc_chain_complete(atchan, desc);
396}
397
398/**
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200399 * atc_advance_work - at the end of a transaction, move forward
400 * @atchan: channel where the transaction ended
401 *
402 * Called with atchan->lock held and bh disabled
403 */
404static void atc_advance_work(struct at_dma_chan *atchan)
405{
406 dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
407
Ludovic Desrochesd202f052013-04-18 09:52:59 +0200408 if (atc_chan_is_enabled(atchan))
409 return;
410
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200411 if (list_empty(&atchan->active_list) ||
412 list_is_singular(&atchan->active_list)) {
413 atc_complete_all(atchan);
414 } else {
415 atc_chain_complete(atchan, atc_first_active(atchan));
416 /* advance work */
417 atc_dostart(atchan, atc_first_active(atchan));
418 }
419}
420
421
422/**
423 * atc_handle_error - handle errors reported by DMA controller
424 * @atchan: channel where error occurs
425 *
426 * Called with atchan->lock held and bh disabled
427 */
428static void atc_handle_error(struct at_dma_chan *atchan)
429{
430 struct at_desc *bad_desc;
431 struct at_desc *child;
432
433 /*
434 * The descriptor currently at the head of the active list is
435 * broked. Since we don't have any way to report errors, we'll
436 * just have to scream loudly and try to carry on.
437 */
438 bad_desc = atc_first_active(atchan);
439 list_del_init(&bad_desc->desc_node);
440
441 /* As we are stopped, take advantage to push queued descriptors
442 * in active_list */
443 list_splice_init(&atchan->queue, atchan->active_list.prev);
444
445 /* Try to restart the controller */
446 if (!list_empty(&atchan->active_list))
447 atc_dostart(atchan, atc_first_active(atchan));
448
449 /*
450 * KERN_CRITICAL may seem harsh, but since this only happens
451 * when someone submits a bad physical address in a
452 * descriptor, we should consider ourselves lucky that the
453 * controller flagged an error instead of scribbling over
454 * random memory locations.
455 */
456 dev_crit(chan2dev(&atchan->chan_common),
457 "Bad descriptor submitted for DMA!\n");
458 dev_crit(chan2dev(&atchan->chan_common),
459 " cookie: %d\n", bad_desc->txd.cookie);
460 atc_dump_lli(atchan, &bad_desc->lli);
Dan Williams285a3c72009-09-08 17:53:03 -0700461 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200462 atc_dump_lli(atchan, &child->lli);
463
464 /* Pretend the descriptor completed successfully */
465 atc_chain_complete(atchan, bad_desc);
466}
467
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200468/**
469 * atc_handle_cyclic - at the end of a period, run callback function
470 * @atchan: channel used for cyclic operations
471 *
472 * Called with atchan->lock held and bh disabled
473 */
474static void atc_handle_cyclic(struct at_dma_chan *atchan)
475{
476 struct at_desc *first = atc_first_active(atchan);
477 struct dma_async_tx_descriptor *txd = &first->txd;
478 dma_async_tx_callback callback = txd->callback;
479 void *param = txd->callback_param;
480
481 dev_vdbg(chan2dev(&atchan->chan_common),
482 "new cyclic period llp 0x%08x\n",
483 channel_readl(atchan, DSCR));
484
485 if (callback)
486 callback(param);
487}
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200488
489/*-- IRQ & Tasklet ---------------------------------------------------*/
490
491static void atc_tasklet(unsigned long data)
492{
493 struct at_dma_chan *atchan = (struct at_dma_chan *)data;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000494 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200495
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000496 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200497 if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200498 atc_handle_error(atchan);
Nicolas Ferre3c477482011-07-25 21:09:23 +0000499 else if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200500 atc_handle_cyclic(atchan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200501 else
502 atc_advance_work(atchan);
503
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000504 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200505}
506
507static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
508{
509 struct at_dma *atdma = (struct at_dma *)dev_id;
510 struct at_dma_chan *atchan;
511 int i;
512 u32 status, pending, imr;
513 int ret = IRQ_NONE;
514
515 do {
516 imr = dma_readl(atdma, EBCIMR);
517 status = dma_readl(atdma, EBCISR);
518 pending = status & imr;
519
520 if (!pending)
521 break;
522
523 dev_vdbg(atdma->dma_common.dev,
524 "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
525 status, imr, pending);
526
527 for (i = 0; i < atdma->dma_common.chancnt; i++) {
528 atchan = &atdma->chan[i];
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200529 if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200530 if (pending & AT_DMA_ERR(i)) {
531 /* Disable channel on AHB error */
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200532 dma_writel(atdma, CHDR,
533 AT_DMA_RES(i) | atchan->mask);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200534 /* Give information to tasklet */
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200535 set_bit(ATC_IS_ERROR, &atchan->status);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200536 }
Elen Songd48de6f2013-05-10 11:01:46 +0800537 if (pending & AT_DMA_BTC(i))
538 set_bit(ATC_IS_BTC, &atchan->status);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200539 tasklet_schedule(&atchan->tasklet);
540 ret = IRQ_HANDLED;
541 }
542 }
543
544 } while (pending);
545
546 return ret;
547}
548
549
550/*-- DMA Engine API --------------------------------------------------*/
551
552/**
553 * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
554 * @desc: descriptor at the head of the transaction chain
555 *
556 * Queue chain if DMA engine is working already
557 *
558 * Cookie increment and adding to active_list or queue must be atomic
559 */
560static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
561{
562 struct at_desc *desc = txd_to_at_desc(tx);
563 struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
564 dma_cookie_t cookie;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000565 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200566
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000567 spin_lock_irqsave(&atchan->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000568 cookie = dma_cookie_assign(tx);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200569
570 if (list_empty(&atchan->active_list)) {
571 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
572 desc->txd.cookie);
573 atc_dostart(atchan, desc);
574 list_add_tail(&desc->desc_node, &atchan->active_list);
575 } else {
576 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
577 desc->txd.cookie);
578 list_add_tail(&desc->desc_node, &atchan->queue);
579 }
580
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000581 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200582
583 return cookie;
584}
585
586/**
587 * atc_prep_dma_memcpy - prepare a memcpy operation
588 * @chan: the channel to prepare operation on
589 * @dest: operation virtual destination address
590 * @src: operation virtual source address
591 * @len: operation length
592 * @flags: tx descriptor status flags
593 */
594static struct dma_async_tx_descriptor *
595atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
596 size_t len, unsigned long flags)
597{
598 struct at_dma_chan *atchan = to_at_dma_chan(chan);
599 struct at_desc *desc = NULL;
600 struct at_desc *first = NULL;
601 struct at_desc *prev = NULL;
602 size_t xfer_count;
603 size_t offset;
604 unsigned int src_width;
605 unsigned int dst_width;
606 u32 ctrla;
607 u32 ctrlb;
608
609 dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
610 dest, src, len, flags);
611
612 if (unlikely(!len)) {
613 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
614 return NULL;
615 }
616
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200617 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200618 | ATC_SRC_ADDR_MODE_INCR
619 | ATC_DST_ADDR_MODE_INCR
620 | ATC_FC_MEM2MEM;
621
622 /*
623 * We can be a lot more clever here, but this should take care
624 * of the most common optimization.
625 */
626 if (!((src | dest | len) & 3)) {
Nicolas Ferreb409ebf2012-05-10 12:17:40 +0200627 ctrla = ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200628 src_width = dst_width = 2;
629 } else if (!((src | dest | len) & 1)) {
Nicolas Ferreb409ebf2012-05-10 12:17:40 +0200630 ctrla = ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200631 src_width = dst_width = 1;
632 } else {
Nicolas Ferreb409ebf2012-05-10 12:17:40 +0200633 ctrla = ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200634 src_width = dst_width = 0;
635 }
636
637 for (offset = 0; offset < len; offset += xfer_count << src_width) {
638 xfer_count = min_t(size_t, (len - offset) >> src_width,
639 ATC_BTSIZE_MAX);
640
641 desc = atc_desc_get(atchan);
642 if (!desc)
643 goto err_desc_get;
644
645 desc->lli.saddr = src + offset;
646 desc->lli.daddr = dest + offset;
647 desc->lli.ctrla = ctrla | xfer_count;
648 desc->lli.ctrlb = ctrlb;
649
650 desc->txd.cookie = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200651
Nicolas Ferree257e152011-05-06 19:56:53 +0200652 atc_desc_chain(&first, &prev, desc);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200653 }
654
655 /* First descriptor of the chain embedds additional information */
656 first->txd.cookie = -EBUSY;
657 first->len = len;
Elen Songd088c332013-05-10 11:00:50 +0800658 first->tx_width = src_width;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200659
660 /* set end-of-link to the last link descriptor of list*/
661 set_desc_eol(desc);
662
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100663 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200664
665 return &first->txd;
666
667err_desc_get:
668 atc_desc_put(atchan, first);
669 return NULL;
670}
671
Nicolas Ferre808347f2009-07-22 20:04:45 +0200672
673/**
674 * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
675 * @chan: DMA channel
676 * @sgl: scatterlist to transfer to/from
677 * @sg_len: number of entries in @scatterlist
678 * @direction: DMA direction
679 * @flags: tx descriptor status flags
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500680 * @context: transaction context (ignored)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200681 */
682static struct dma_async_tx_descriptor *
683atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530684 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500685 unsigned long flags, void *context)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200686{
687 struct at_dma_chan *atchan = to_at_dma_chan(chan);
688 struct at_dma_slave *atslave = chan->private;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100689 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200690 struct at_desc *first = NULL;
691 struct at_desc *prev = NULL;
692 u32 ctrla;
693 u32 ctrlb;
694 dma_addr_t reg;
695 unsigned int reg_width;
696 unsigned int mem_width;
697 unsigned int i;
698 struct scatterlist *sg;
699 size_t total_len = 0;
700
Nicolas Ferrecc52a102011-04-30 16:57:47 +0200701 dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
702 sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530703 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre808347f2009-07-22 20:04:45 +0200704 flags);
705
706 if (unlikely(!atslave || !sg_len)) {
Nicolas Ferrec618a9b2012-09-11 17:21:44 +0200707 dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n");
Nicolas Ferre808347f2009-07-22 20:04:45 +0200708 return NULL;
709 }
710
Nicolas Ferre1dd1ea82012-05-10 12:17:41 +0200711 ctrla = ATC_SCSIZE(sconfig->src_maxburst)
712 | ATC_DCSIZE(sconfig->dst_maxburst);
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200713 ctrlb = ATC_IEN;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200714
715 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530716 case DMA_MEM_TO_DEV:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100717 reg_width = convert_buswidth(sconfig->dst_addr_width);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200718 ctrla |= ATC_DST_WIDTH(reg_width);
719 ctrlb |= ATC_DST_ADDR_MODE_FIXED
720 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200721 | ATC_FC_MEM2PER
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +0000722 | ATC_SIF(atchan->mem_if) | ATC_DIF(atchan->per_if);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100723 reg = sconfig->dst_addr;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200724 for_each_sg(sgl, sg, sg_len, i) {
725 struct at_desc *desc;
726 u32 len;
727 u32 mem;
728
729 desc = atc_desc_get(atchan);
730 if (!desc)
731 goto err_desc_get;
732
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100733 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200734 len = sg_dma_len(sg);
Nicolas Ferrec4567972012-09-11 17:21:45 +0200735 if (unlikely(!len)) {
736 dev_dbg(chan2dev(chan),
737 "prep_slave_sg: sg(%d) data length is zero\n", i);
738 goto err;
739 }
Nicolas Ferre808347f2009-07-22 20:04:45 +0200740 mem_width = 2;
741 if (unlikely(mem & 3 || len & 3))
742 mem_width = 0;
743
744 desc->lli.saddr = mem;
745 desc->lli.daddr = reg;
746 desc->lli.ctrla = ctrla
747 | ATC_SRC_WIDTH(mem_width)
748 | len >> mem_width;
749 desc->lli.ctrlb = ctrlb;
750
Nicolas Ferree257e152011-05-06 19:56:53 +0200751 atc_desc_chain(&first, &prev, desc);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200752 total_len += len;
753 }
754 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530755 case DMA_DEV_TO_MEM:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100756 reg_width = convert_buswidth(sconfig->src_addr_width);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200757 ctrla |= ATC_SRC_WIDTH(reg_width);
758 ctrlb |= ATC_DST_ADDR_MODE_INCR
759 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200760 | ATC_FC_PER2MEM
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +0000761 | ATC_SIF(atchan->per_if) | ATC_DIF(atchan->mem_if);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200762
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100763 reg = sconfig->src_addr;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200764 for_each_sg(sgl, sg, sg_len, i) {
765 struct at_desc *desc;
766 u32 len;
767 u32 mem;
768
769 desc = atc_desc_get(atchan);
770 if (!desc)
771 goto err_desc_get;
772
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100773 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200774 len = sg_dma_len(sg);
Nicolas Ferrec4567972012-09-11 17:21:45 +0200775 if (unlikely(!len)) {
776 dev_dbg(chan2dev(chan),
777 "prep_slave_sg: sg(%d) data length is zero\n", i);
778 goto err;
779 }
Nicolas Ferre808347f2009-07-22 20:04:45 +0200780 mem_width = 2;
781 if (unlikely(mem & 3 || len & 3))
782 mem_width = 0;
783
784 desc->lli.saddr = reg;
785 desc->lli.daddr = mem;
786 desc->lli.ctrla = ctrla
787 | ATC_DST_WIDTH(mem_width)
Nicolas Ferre59a609d2010-12-13 13:48:41 +0100788 | len >> reg_width;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200789 desc->lli.ctrlb = ctrlb;
790
Nicolas Ferree257e152011-05-06 19:56:53 +0200791 atc_desc_chain(&first, &prev, desc);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200792 total_len += len;
793 }
794 break;
795 default:
796 return NULL;
797 }
798
799 /* set end-of-link to the last link descriptor of list*/
800 set_desc_eol(prev);
801
802 /* First descriptor of the chain embedds additional information */
803 first->txd.cookie = -EBUSY;
804 first->len = total_len;
Elen Songd088c332013-05-10 11:00:50 +0800805 first->tx_width = reg_width;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200806
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100807 /* first link descriptor of list is responsible of flags */
808 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferre808347f2009-07-22 20:04:45 +0200809
810 return &first->txd;
811
812err_desc_get:
813 dev_err(chan2dev(chan), "not enough descriptors available\n");
Nicolas Ferrec4567972012-09-11 17:21:45 +0200814err:
Nicolas Ferre808347f2009-07-22 20:04:45 +0200815 atc_desc_put(atchan, first);
816 return NULL;
817}
818
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200819/**
820 * atc_dma_cyclic_check_values
821 * Check for too big/unaligned periods and unaligned DMA buffer
822 */
823static int
824atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
Andy Shevchenko0e7264c2013-01-10 10:52:57 +0200825 size_t period_len)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200826{
827 if (period_len > (ATC_BTSIZE_MAX << reg_width))
828 goto err_out;
829 if (unlikely(period_len & ((1 << reg_width) - 1)))
830 goto err_out;
831 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
832 goto err_out;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200833
834 return 0;
835
836err_out:
837 return -EINVAL;
838}
839
840/**
Masanari Iidad73111c2012-08-04 23:37:53 +0900841 * atc_dma_cyclic_fill_desc - Fill one period descriptor
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200842 */
843static int
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100844atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200845 unsigned int period_index, dma_addr_t buf_addr,
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100846 unsigned int reg_width, size_t period_len,
847 enum dma_transfer_direction direction)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200848{
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100849 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100850 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
851 u32 ctrla;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200852
853 /* prepare common CRTLA value */
Nicolas Ferre1dd1ea82012-05-10 12:17:41 +0200854 ctrla = ATC_SCSIZE(sconfig->src_maxburst)
855 | ATC_DCSIZE(sconfig->dst_maxburst)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200856 | ATC_DST_WIDTH(reg_width)
857 | ATC_SRC_WIDTH(reg_width)
858 | period_len >> reg_width;
859
860 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530861 case DMA_MEM_TO_DEV:
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200862 desc->lli.saddr = buf_addr + (period_len * period_index);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100863 desc->lli.daddr = sconfig->dst_addr;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200864 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200865 desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200866 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200867 | ATC_FC_MEM2PER
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +0000868 | ATC_SIF(atchan->mem_if)
869 | ATC_DIF(atchan->per_if);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200870 break;
871
Vinod Kouldb8196d2011-10-13 22:34:23 +0530872 case DMA_DEV_TO_MEM:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100873 desc->lli.saddr = sconfig->src_addr;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200874 desc->lli.daddr = buf_addr + (period_len * period_index);
875 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200876 desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200877 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200878 | ATC_FC_PER2MEM
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +0000879 | ATC_SIF(atchan->per_if)
880 | ATC_DIF(atchan->mem_if);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200881 break;
882
883 default:
884 return -EINVAL;
885 }
886
887 return 0;
888}
889
890/**
891 * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
892 * @chan: the DMA channel to prepare
893 * @buf_addr: physical DMA address where the buffer starts
894 * @buf_len: total number of bytes for the entire buffer
895 * @period_len: number of bytes for each period
896 * @direction: transfer direction, to or from device
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +0300897 * @flags: tx descriptor status flags
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200898 */
899static struct dma_async_tx_descriptor *
900atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500901 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +0200902 unsigned long flags)
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200903{
904 struct at_dma_chan *atchan = to_at_dma_chan(chan);
905 struct at_dma_slave *atslave = chan->private;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100906 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200907 struct at_desc *first = NULL;
908 struct at_desc *prev = NULL;
909 unsigned long was_cyclic;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100910 unsigned int reg_width;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200911 unsigned int periods = buf_len / period_len;
912 unsigned int i;
913
914 dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
Vinod Kouldb8196d2011-10-13 22:34:23 +0530915 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200916 buf_addr,
917 periods, buf_len, period_len);
918
919 if (unlikely(!atslave || !buf_len || !period_len)) {
920 dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
921 return NULL;
922 }
923
924 was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
925 if (was_cyclic) {
926 dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
927 return NULL;
928 }
929
Andy Shevchenko0e7264c2013-01-10 10:52:57 +0200930 if (unlikely(!is_slave_direction(direction)))
931 goto err_out;
932
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100933 if (sconfig->direction == DMA_MEM_TO_DEV)
934 reg_width = convert_buswidth(sconfig->dst_addr_width);
935 else
936 reg_width = convert_buswidth(sconfig->src_addr_width);
937
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200938 /* Check for too big/unaligned periods and unaligned DMA buffer */
Andy Shevchenko0e7264c2013-01-10 10:52:57 +0200939 if (atc_dma_cyclic_check_values(reg_width, buf_addr, period_len))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200940 goto err_out;
941
942 /* build cyclic linked list */
943 for (i = 0; i < periods; i++) {
944 struct at_desc *desc;
945
946 desc = atc_desc_get(atchan);
947 if (!desc)
948 goto err_desc_get;
949
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100950 if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
951 reg_width, period_len, direction))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200952 goto err_desc_get;
953
954 atc_desc_chain(&first, &prev, desc);
955 }
956
957 /* lets make a cyclic list */
958 prev->lli.dscr = first->txd.phys;
959
960 /* First descriptor of the chain embedds additional information */
961 first->txd.cookie = -EBUSY;
962 first->len = buf_len;
Elen Songd088c332013-05-10 11:00:50 +0800963 first->tx_width = reg_width;
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200964
965 return &first->txd;
966
967err_desc_get:
968 dev_err(chan2dev(chan), "not enough descriptors available\n");
969 atc_desc_put(atchan, first);
970err_out:
971 clear_bit(ATC_IS_CYCLIC, &atchan->status);
972 return NULL;
973}
974
Maxime Ripard4facfe72014-11-17 14:42:06 +0100975static int atc_config(struct dma_chan *chan,
976 struct dma_slave_config *sconfig)
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100977{
978 struct at_dma_chan *atchan = to_at_dma_chan(chan);
979
Maxime Ripard4facfe72014-11-17 14:42:06 +0100980 dev_vdbg(chan2dev(chan), "%s\n", __func__);
981
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100982 /* Check if it is chan is configured for slave transfers */
983 if (!chan->private)
984 return -EINVAL;
985
986 memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
987
988 convert_burst(&atchan->dma_sconfig.src_maxburst);
989 convert_burst(&atchan->dma_sconfig.dst_maxburst);
990
991 return 0;
992}
993
Maxime Ripard4facfe72014-11-17 14:42:06 +0100994static int atc_pause(struct dma_chan *chan)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200995{
996 struct at_dma_chan *atchan = to_at_dma_chan(chan);
997 struct at_dma *atdma = to_at_dma(chan->device);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200998 int chan_id = atchan->chan_common.chan_id;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000999 unsigned long flags;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001000
Nicolas Ferre808347f2009-07-22 20:04:45 +02001001 LIST_HEAD(list);
1002
Maxime Ripard4facfe72014-11-17 14:42:06 +01001003 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001004
Maxime Ripard4facfe72014-11-17 14:42:06 +01001005 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001006
Maxime Ripard4facfe72014-11-17 14:42:06 +01001007 dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
1008 set_bit(ATC_IS_PAUSED, &atchan->status);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001009
Maxime Ripard4facfe72014-11-17 14:42:06 +01001010 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001011
Maxime Ripard4facfe72014-11-17 14:42:06 +01001012 return 0;
1013}
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001014
Maxime Ripard4facfe72014-11-17 14:42:06 +01001015static int atc_resume(struct dma_chan *chan)
1016{
1017 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1018 struct at_dma *atdma = to_at_dma(chan->device);
1019 int chan_id = atchan->chan_common.chan_id;
1020 unsigned long flags;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001021
Maxime Ripard4facfe72014-11-17 14:42:06 +01001022 LIST_HEAD(list);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001023
Maxime Ripard4facfe72014-11-17 14:42:06 +01001024 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001025
Maxime Ripard4facfe72014-11-17 14:42:06 +01001026 if (!atc_chan_is_paused(atchan))
1027 return 0;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001028
Maxime Ripard4facfe72014-11-17 14:42:06 +01001029 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001030
Maxime Ripard4facfe72014-11-17 14:42:06 +01001031 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
1032 clear_bit(ATC_IS_PAUSED, &atchan->status);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001033
Maxime Ripard4facfe72014-11-17 14:42:06 +01001034 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001035
Maxime Ripard4facfe72014-11-17 14:42:06 +01001036 return 0;
1037}
1038
1039static int atc_terminate_all(struct dma_chan *chan)
1040{
1041 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1042 struct at_dma *atdma = to_at_dma(chan->device);
1043 int chan_id = atchan->chan_common.chan_id;
1044 struct at_desc *desc, *_desc;
1045 unsigned long flags;
1046
1047 LIST_HEAD(list);
1048
1049 dev_vdbg(chan2dev(chan), "%s\n", __func__);
1050
1051 /*
1052 * This is only called when something went wrong elsewhere, so
1053 * we don't really care about the data. Just disable the
1054 * channel. We still have to poll the channel enable bit due
1055 * to AHB/HSB limitations.
1056 */
1057 spin_lock_irqsave(&atchan->lock, flags);
1058
1059 /* disabling channel: must also remove suspend state */
1060 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
1061
1062 /* confirm that this channel is disabled */
1063 while (dma_readl(atdma, CHSR) & atchan->mask)
1064 cpu_relax();
1065
1066 /* active_list entries will end up before queued entries */
1067 list_splice_init(&atchan->queue, &list);
1068 list_splice_init(&atchan->active_list, &list);
1069
1070 /* Flush all pending and queued descriptors */
1071 list_for_each_entry_safe(desc, _desc, &list, desc_node)
1072 atc_chain_complete(atchan, desc);
1073
1074 clear_bit(ATC_IS_PAUSED, &atchan->status);
1075 /* if channel dedicated to cyclic operations, free it */
1076 clear_bit(ATC_IS_CYCLIC, &atchan->status);
1077
1078 spin_unlock_irqrestore(&atchan->lock, flags);
Yong Wangb0ebeb92010-08-05 10:40:08 +08001079
Linus Walleijc3635c72010-03-26 16:44:01 -07001080 return 0;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001081}
1082
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001083/**
Linus Walleij07934482010-03-26 16:50:49 -07001084 * atc_tx_status - poll for transaction completion
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001085 * @chan: DMA channel
1086 * @cookie: transaction identifier to check status of
Linus Walleij07934482010-03-26 16:50:49 -07001087 * @txstate: if not %NULL updated with transaction state
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001088 *
Linus Walleij07934482010-03-26 16:50:49 -07001089 * If @txstate is passed in, upon return it reflect the driver
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001090 * internal state and can be used with dma_async_is_complete() to check
1091 * the status of multiple cookies without re-checking hardware state.
1092 */
1093static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -07001094atc_tx_status(struct dma_chan *chan,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001095 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -07001096 struct dma_tx_state *txstate)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001097{
1098 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001099 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001100 enum dma_status ret;
Elen Songd48de6f2013-05-10 11:01:46 +08001101 int bytes = 0;
1102
1103 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul6d203d12013-10-16 13:34:35 +05301104 if (ret == DMA_COMPLETE)
Elen Songd48de6f2013-05-10 11:01:46 +08001105 return ret;
1106 /*
1107 * There's no point calculating the residue if there's
1108 * no txstate to store the value.
1109 */
1110 if (!txstate)
1111 return DMA_ERROR;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001112
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001113 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001114
Elen Songd48de6f2013-05-10 11:01:46 +08001115 /* Get number of bytes left in the active transactions */
1116 bytes = atc_get_bytes_left(chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001117
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001118 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001119
Elen Songd48de6f2013-05-10 11:01:46 +08001120 if (unlikely(bytes < 0)) {
1121 dev_vdbg(chan2dev(chan), "get residual bytes error\n");
1122 return DMA_ERROR;
Nicolas Ferrec3dbc602013-06-07 17:26:14 +02001123 } else {
Elen Songd48de6f2013-05-10 11:01:46 +08001124 dma_set_residue(txstate, bytes);
Nicolas Ferrec3dbc602013-06-07 17:26:14 +02001125 }
Nicolas Ferre543aabc2011-05-06 19:56:51 +02001126
Elen Songd48de6f2013-05-10 11:01:46 +08001127 dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d residue = %d\n",
1128 ret, cookie, bytes);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001129
1130 return ret;
1131}
1132
1133/**
1134 * atc_issue_pending - try to finish work
1135 * @chan: target DMA channel
1136 */
1137static void atc_issue_pending(struct dma_chan *chan)
1138{
1139 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001140 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001141
1142 dev_vdbg(chan2dev(chan), "issue_pending\n");
1143
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001144 /* Not needed for cyclic transfers */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001145 if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001146 return;
1147
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001148 spin_lock_irqsave(&atchan->lock, flags);
Ludovic Desrochesd202f052013-04-18 09:52:59 +02001149 atc_advance_work(atchan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001150 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001151}
1152
1153/**
1154 * atc_alloc_chan_resources - allocate resources for DMA channel
1155 * @chan: allocate descriptor resources for this channel
1156 * @client: current client requesting the channel be ready for requests
1157 *
1158 * return - the number of allocated descriptors
1159 */
1160static int atc_alloc_chan_resources(struct dma_chan *chan)
1161{
1162 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1163 struct at_dma *atdma = to_at_dma(chan->device);
1164 struct at_desc *desc;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001165 struct at_dma_slave *atslave;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001166 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001167 int i;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001168 u32 cfg;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001169 LIST_HEAD(tmp_list);
1170
1171 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
1172
1173 /* ASSERT: channel is idle */
1174 if (atc_chan_is_enabled(atchan)) {
1175 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
1176 return -EIO;
1177 }
1178
Nicolas Ferre808347f2009-07-22 20:04:45 +02001179 cfg = ATC_DEFAULT_CFG;
1180
1181 atslave = chan->private;
1182 if (atslave) {
1183 /*
1184 * We need controller-specific data to set up slave
1185 * transfers.
1186 */
1187 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
1188
Nicolas Ferreea7e7902013-05-10 15:19:13 +02001189 /* if cfg configuration specified take it instead of default */
Nicolas Ferre808347f2009-07-22 20:04:45 +02001190 if (atslave->cfg)
1191 cfg = atslave->cfg;
1192 }
1193
1194 /* have we already been set up?
1195 * reconfigure channel but no need to reallocate descriptors */
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001196 if (!list_empty(&atchan->free_list))
1197 return atchan->descs_allocated;
1198
1199 /* Allocate initial pool of descriptors */
1200 for (i = 0; i < init_nr_desc_per_channel; i++) {
1201 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
1202 if (!desc) {
1203 dev_err(atdma->dma_common.dev,
1204 "Only %d initial descriptors\n", i);
1205 break;
1206 }
1207 list_add_tail(&desc->desc_node, &tmp_list);
1208 }
1209
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001210 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001211 atchan->descs_allocated = i;
Elen Songd48de6f2013-05-10 11:01:46 +08001212 atchan->remain_desc = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001213 list_splice(&tmp_list, &atchan->free_list);
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001214 dma_cookie_init(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001215 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001216
1217 /* channel parameters */
Nicolas Ferre808347f2009-07-22 20:04:45 +02001218 channel_writel(atchan, CFG, cfg);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001219
1220 dev_dbg(chan2dev(chan),
1221 "alloc_chan_resources: allocated %d descriptors\n",
1222 atchan->descs_allocated);
1223
1224 return atchan->descs_allocated;
1225}
1226
1227/**
1228 * atc_free_chan_resources - free all channel resources
1229 * @chan: DMA channel
1230 */
1231static void atc_free_chan_resources(struct dma_chan *chan)
1232{
1233 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1234 struct at_dma *atdma = to_at_dma(chan->device);
1235 struct at_desc *desc, *_desc;
1236 LIST_HEAD(list);
1237
1238 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
1239 atchan->descs_allocated);
1240
1241 /* ASSERT: channel is idle */
1242 BUG_ON(!list_empty(&atchan->active_list));
1243 BUG_ON(!list_empty(&atchan->queue));
1244 BUG_ON(atc_chan_is_enabled(atchan));
1245
1246 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
1247 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1248 list_del(&desc->desc_node);
1249 /* free link descriptor */
1250 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
1251 }
1252 list_splice_init(&atchan->free_list, &list);
1253 atchan->descs_allocated = 0;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001254 atchan->status = 0;
Elen Songd48de6f2013-05-10 11:01:46 +08001255 atchan->remain_desc = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001256
1257 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
1258}
1259
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001260#ifdef CONFIG_OF
1261static bool at_dma_filter(struct dma_chan *chan, void *slave)
1262{
1263 struct at_dma_slave *atslave = slave;
1264
1265 if (atslave->dma_dev == chan->device->dev) {
1266 chan->private = atslave;
1267 return true;
1268 } else {
1269 return false;
1270 }
1271}
1272
1273static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
1274 struct of_dma *of_dma)
1275{
1276 struct dma_chan *chan;
1277 struct at_dma_chan *atchan;
1278 struct at_dma_slave *atslave;
1279 dma_cap_mask_t mask;
1280 unsigned int per_id;
1281 struct platform_device *dmac_pdev;
1282
1283 if (dma_spec->args_count != 2)
1284 return NULL;
1285
1286 dmac_pdev = of_find_device_by_node(dma_spec->np);
1287
1288 dma_cap_zero(mask);
1289 dma_cap_set(DMA_SLAVE, mask);
1290
1291 atslave = devm_kzalloc(&dmac_pdev->dev, sizeof(*atslave), GFP_KERNEL);
1292 if (!atslave)
1293 return NULL;
Ludovic Desroches62971b22013-06-13 10:39:39 +02001294
1295 atslave->cfg = ATC_DST_H2SEL_HW | ATC_SRC_H2SEL_HW;
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001296 /*
1297 * We can fill both SRC_PER and DST_PER, one of these fields will be
1298 * ignored depending on DMA transfer direction.
1299 */
Ludovic Desroches62971b22013-06-13 10:39:39 +02001300 per_id = dma_spec->args[1] & AT91_DMA_CFG_PER_ID_MASK;
1301 atslave->cfg |= ATC_DST_PER_MSB(per_id) | ATC_DST_PER(per_id)
Nicolas Ferre6c227702013-05-10 15:19:15 +02001302 | ATC_SRC_PER_MSB(per_id) | ATC_SRC_PER(per_id);
Ludovic Desroches62971b22013-06-13 10:39:39 +02001303 /*
1304 * We have to translate the value we get from the device tree since
1305 * the half FIFO configuration value had to be 0 to keep backward
1306 * compatibility.
1307 */
1308 switch (dma_spec->args[1] & AT91_DMA_CFG_FIFOCFG_MASK) {
1309 case AT91_DMA_CFG_FIFOCFG_ALAP:
1310 atslave->cfg |= ATC_FIFOCFG_LARGESTBURST;
1311 break;
1312 case AT91_DMA_CFG_FIFOCFG_ASAP:
1313 atslave->cfg |= ATC_FIFOCFG_ENOUGHSPACE;
1314 break;
1315 case AT91_DMA_CFG_FIFOCFG_HALF:
1316 default:
1317 atslave->cfg |= ATC_FIFOCFG_HALFFIFO;
1318 }
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001319 atslave->dma_dev = &dmac_pdev->dev;
1320
1321 chan = dma_request_channel(mask, at_dma_filter, atslave);
1322 if (!chan)
1323 return NULL;
1324
1325 atchan = to_at_dma_chan(chan);
1326 atchan->per_if = dma_spec->args[0] & 0xff;
1327 atchan->mem_if = (dma_spec->args[0] >> 16) & 0xff;
1328
1329 return chan;
1330}
1331#else
1332static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
1333 struct of_dma *of_dma)
1334{
1335 return NULL;
1336}
1337#endif
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001338
1339/*-- Module Management -----------------------------------------------*/
1340
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001341/* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
1342static struct at_dma_platform_data at91sam9rl_config = {
1343 .nr_channels = 2,
1344};
1345static struct at_dma_platform_data at91sam9g45_config = {
1346 .nr_channels = 8,
1347};
1348
Nicolas Ferrec5115952011-10-17 14:56:41 +02001349#if defined(CONFIG_OF)
1350static const struct of_device_id atmel_dma_dt_ids[] = {
1351 {
1352 .compatible = "atmel,at91sam9rl-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001353 .data = &at91sam9rl_config,
Nicolas Ferrec5115952011-10-17 14:56:41 +02001354 }, {
1355 .compatible = "atmel,at91sam9g45-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001356 .data = &at91sam9g45_config,
Nicolas Ferredcc81732011-11-22 11:55:53 +01001357 }, {
1358 /* sentinel */
1359 }
Nicolas Ferrec5115952011-10-17 14:56:41 +02001360};
1361
1362MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
1363#endif
1364
Nicolas Ferre0ab88a02011-11-22 11:55:52 +01001365static const struct platform_device_id atdma_devtypes[] = {
Nicolas Ferre67348452011-10-17 14:56:40 +02001366 {
1367 .name = "at91sam9rl_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001368 .driver_data = (unsigned long) &at91sam9rl_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001369 }, {
1370 .name = "at91sam9g45_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001371 .driver_data = (unsigned long) &at91sam9g45_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001372 }, {
1373 /* sentinel */
1374 }
1375};
1376
Uwe Kleine-König7fd63cc2012-07-13 14:32:10 +02001377static inline const struct at_dma_platform_data * __init at_dma_get_driver_data(
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001378 struct platform_device *pdev)
Nicolas Ferrec5115952011-10-17 14:56:41 +02001379{
1380 if (pdev->dev.of_node) {
1381 const struct of_device_id *match;
1382 match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
1383 if (match == NULL)
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001384 return NULL;
1385 return match->data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001386 }
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001387 return (struct at_dma_platform_data *)
1388 platform_get_device_id(pdev)->driver_data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001389}
1390
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001391/**
1392 * at_dma_off - disable DMA controller
1393 * @atdma: the Atmel HDAMC device
1394 */
1395static void at_dma_off(struct at_dma *atdma)
1396{
1397 dma_writel(atdma, EN, 0);
1398
1399 /* disable all interrupts */
1400 dma_writel(atdma, EBCIDR, -1L);
1401
1402 /* confirm that all channels are disabled */
1403 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
1404 cpu_relax();
1405}
1406
1407static int __init at_dma_probe(struct platform_device *pdev)
1408{
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001409 struct resource *io;
1410 struct at_dma *atdma;
1411 size_t size;
1412 int irq;
1413 int err;
1414 int i;
Uwe Kleine-König7fd63cc2012-07-13 14:32:10 +02001415 const struct at_dma_platform_data *plat_dat;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001416
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001417 /* setup platform data for each SoC */
1418 dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
1419 dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
1420 dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
Nicolas Ferre67348452011-10-17 14:56:40 +02001421
1422 /* get DMA parameters from controller type */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001423 plat_dat = at_dma_get_driver_data(pdev);
1424 if (!plat_dat)
1425 return -ENODEV;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001426
1427 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1428 if (!io)
1429 return -EINVAL;
1430
1431 irq = platform_get_irq(pdev, 0);
1432 if (irq < 0)
1433 return irq;
1434
1435 size = sizeof(struct at_dma);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001436 size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001437 atdma = kzalloc(size, GFP_KERNEL);
1438 if (!atdma)
1439 return -ENOMEM;
1440
Nicolas Ferre67348452011-10-17 14:56:40 +02001441 /* discover transaction capabilities */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001442 atdma->dma_common.cap_mask = plat_dat->cap_mask;
1443 atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001444
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07001445 size = resource_size(io);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001446 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1447 err = -EBUSY;
1448 goto err_kfree;
1449 }
1450
1451 atdma->regs = ioremap(io->start, size);
1452 if (!atdma->regs) {
1453 err = -ENOMEM;
1454 goto err_release_r;
1455 }
1456
1457 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1458 if (IS_ERR(atdma->clk)) {
1459 err = PTR_ERR(atdma->clk);
1460 goto err_clk;
1461 }
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001462 err = clk_prepare_enable(atdma->clk);
1463 if (err)
1464 goto err_clk_prepare;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001465
1466 /* force dma off, just in case */
1467 at_dma_off(atdma);
1468
1469 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1470 if (err)
1471 goto err_irq;
1472
1473 platform_set_drvdata(pdev, atdma);
1474
1475 /* create a pool of consistent memory blocks for hardware descriptors */
1476 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1477 &pdev->dev, sizeof(struct at_desc),
1478 4 /* word alignment */, 0);
1479 if (!atdma->dma_desc_pool) {
1480 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1481 err = -ENOMEM;
1482 goto err_pool_create;
1483 }
1484
1485 /* clear any pending interrupt */
1486 while (dma_readl(atdma, EBCISR))
1487 cpu_relax();
1488
1489 /* initialize channels related values */
1490 INIT_LIST_HEAD(&atdma->dma_common.channels);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001491 for (i = 0; i < plat_dat->nr_channels; i++) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001492 struct at_dma_chan *atchan = &atdma->chan[i];
1493
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001494 atchan->mem_if = AT_DMA_MEM_IF;
1495 atchan->per_if = AT_DMA_PER_IF;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001496 atchan->chan_common.device = &atdma->dma_common;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001497 dma_cookie_init(&atchan->chan_common);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001498 list_add_tail(&atchan->chan_common.device_node,
1499 &atdma->dma_common.channels);
1500
1501 atchan->ch_regs = atdma->regs + ch_regs(i);
1502 spin_lock_init(&atchan->lock);
1503 atchan->mask = 1 << i;
1504
1505 INIT_LIST_HEAD(&atchan->active_list);
1506 INIT_LIST_HEAD(&atchan->queue);
1507 INIT_LIST_HEAD(&atchan->free_list);
1508
1509 tasklet_init(&atchan->tasklet, atc_tasklet,
1510 (unsigned long)atchan);
Nikolaus Vossbda3a472012-01-17 10:28:33 +01001511 atc_enable_chan_irq(atdma, i);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001512 }
1513
1514 /* set base routines */
1515 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1516 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
Linus Walleij07934482010-03-26 16:50:49 -07001517 atdma->dma_common.device_tx_status = atc_tx_status;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001518 atdma->dma_common.device_issue_pending = atc_issue_pending;
1519 atdma->dma_common.dev = &pdev->dev;
1520
1521 /* set prep routines based on capability */
1522 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1523 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1524
Nicolas Ferred7db8082011-08-05 11:43:44 +00001525 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
Nicolas Ferre808347f2009-07-22 20:04:45 +02001526 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001527 /* controller can do slave DMA: can trigger cyclic transfers */
1528 dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001529 atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
Maxime Ripard4facfe72014-11-17 14:42:06 +01001530 atdma->dma_common.device_config = atc_config;
1531 atdma->dma_common.device_pause = atc_pause;
1532 atdma->dma_common.device_resume = atc_resume;
1533 atdma->dma_common.device_terminate_all = atc_terminate_all;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001534 }
Nicolas Ferre808347f2009-07-22 20:04:45 +02001535
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001536 dma_writel(atdma, EN, AT_DMA_ENABLE);
1537
1538 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
1539 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1540 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001541 plat_dat->nr_channels);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001542
1543 dma_async_device_register(&atdma->dma_common);
1544
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001545 /*
1546 * Do not return an error if the dmac node is not present in order to
1547 * not break the existing way of requesting channel with
1548 * dma_request_channel().
1549 */
1550 if (pdev->dev.of_node) {
1551 err = of_dma_controller_register(pdev->dev.of_node,
1552 at_dma_xlate, atdma);
1553 if (err) {
1554 dev_err(&pdev->dev, "could not register of_dma_controller\n");
1555 goto err_of_dma_controller_register;
1556 }
1557 }
1558
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001559 return 0;
1560
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001561err_of_dma_controller_register:
1562 dma_async_device_unregister(&atdma->dma_common);
1563 dma_pool_destroy(atdma->dma_desc_pool);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001564err_pool_create:
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001565 free_irq(platform_get_irq(pdev, 0), atdma);
1566err_irq:
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001567 clk_disable_unprepare(atdma->clk);
1568err_clk_prepare:
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001569 clk_put(atdma->clk);
1570err_clk:
1571 iounmap(atdma->regs);
1572 atdma->regs = NULL;
1573err_release_r:
1574 release_mem_region(io->start, size);
1575err_kfree:
1576 kfree(atdma);
1577 return err;
1578}
1579
Maxin B. John1d1bbd32013-02-20 02:07:04 +02001580static int at_dma_remove(struct platform_device *pdev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001581{
1582 struct at_dma *atdma = platform_get_drvdata(pdev);
1583 struct dma_chan *chan, *_chan;
1584 struct resource *io;
1585
1586 at_dma_off(atdma);
1587 dma_async_device_unregister(&atdma->dma_common);
1588
1589 dma_pool_destroy(atdma->dma_desc_pool);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001590 free_irq(platform_get_irq(pdev, 0), atdma);
1591
1592 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1593 device_node) {
1594 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1595
1596 /* Disable interrupts */
Nikolaus Vossbda3a472012-01-17 10:28:33 +01001597 atc_disable_chan_irq(atdma, chan->chan_id);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001598
1599 tasklet_kill(&atchan->tasklet);
1600 list_del(&chan->device_node);
1601 }
1602
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001603 clk_disable_unprepare(atdma->clk);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001604 clk_put(atdma->clk);
1605
1606 iounmap(atdma->regs);
1607 atdma->regs = NULL;
1608
1609 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07001610 release_mem_region(io->start, resource_size(io));
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001611
1612 kfree(atdma);
1613
1614 return 0;
1615}
1616
1617static void at_dma_shutdown(struct platform_device *pdev)
1618{
1619 struct at_dma *atdma = platform_get_drvdata(pdev);
1620
1621 at_dma_off(platform_get_drvdata(pdev));
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001622 clk_disable_unprepare(atdma->clk);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001623}
1624
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001625static int at_dma_prepare(struct device *dev)
1626{
1627 struct platform_device *pdev = to_platform_device(dev);
1628 struct at_dma *atdma = platform_get_drvdata(pdev);
1629 struct dma_chan *chan, *_chan;
1630
1631 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1632 device_node) {
1633 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1634 /* wait for transaction completion (except in cyclic case) */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001635 if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001636 return -EAGAIN;
1637 }
1638 return 0;
1639}
1640
1641static void atc_suspend_cyclic(struct at_dma_chan *atchan)
1642{
1643 struct dma_chan *chan = &atchan->chan_common;
1644
1645 /* Channel should be paused by user
1646 * do it anyway even if it is not done already */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001647 if (!atc_chan_is_paused(atchan)) {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001648 dev_warn(chan2dev(chan),
1649 "cyclic channel not paused, should be done by channel user\n");
Maxime Ripard4facfe72014-11-17 14:42:06 +01001650 atc_pause(chan);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001651 }
1652
1653 /* now preserve additional data for cyclic operations */
1654 /* next descriptor address in the cyclic list */
1655 atchan->save_dscr = channel_readl(atchan, DSCR);
1656
1657 vdbg_dump_regs(atchan);
1658}
1659
Dan Williams33f82d12009-09-10 00:06:44 +02001660static int at_dma_suspend_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001661{
Dan Williams33f82d12009-09-10 00:06:44 +02001662 struct platform_device *pdev = to_platform_device(dev);
1663 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001664 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001665
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001666 /* preserve data */
1667 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1668 device_node) {
1669 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1670
Nicolas Ferre3c477482011-07-25 21:09:23 +00001671 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001672 atc_suspend_cyclic(atchan);
1673 atchan->save_cfg = channel_readl(atchan, CFG);
1674 }
1675 atdma->save_imr = dma_readl(atdma, EBCIMR);
1676
1677 /* disable DMA controller */
1678 at_dma_off(atdma);
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001679 clk_disable_unprepare(atdma->clk);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001680 return 0;
1681}
1682
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001683static void atc_resume_cyclic(struct at_dma_chan *atchan)
1684{
1685 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
1686
1687 /* restore channel status for cyclic descriptors list:
1688 * next descriptor in the cyclic list at the time of suspend */
1689 channel_writel(atchan, SADDR, 0);
1690 channel_writel(atchan, DADDR, 0);
1691 channel_writel(atchan, CTRLA, 0);
1692 channel_writel(atchan, CTRLB, 0);
1693 channel_writel(atchan, DSCR, atchan->save_dscr);
1694 dma_writel(atdma, CHER, atchan->mask);
1695
1696 /* channel pause status should be removed by channel user
1697 * We cannot take the initiative to do it here */
1698
1699 vdbg_dump_regs(atchan);
1700}
1701
Dan Williams33f82d12009-09-10 00:06:44 +02001702static int at_dma_resume_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001703{
Dan Williams33f82d12009-09-10 00:06:44 +02001704 struct platform_device *pdev = to_platform_device(dev);
1705 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001706 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001707
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001708 /* bring back DMA controller */
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001709 clk_prepare_enable(atdma->clk);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001710 dma_writel(atdma, EN, AT_DMA_ENABLE);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001711
1712 /* clear any pending interrupt */
1713 while (dma_readl(atdma, EBCISR))
1714 cpu_relax();
1715
1716 /* restore saved data */
1717 dma_writel(atdma, EBCIER, atdma->save_imr);
1718 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1719 device_node) {
1720 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1721
1722 channel_writel(atchan, CFG, atchan->save_cfg);
Nicolas Ferre3c477482011-07-25 21:09:23 +00001723 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001724 atc_resume_cyclic(atchan);
1725 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001726 return 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001727}
1728
Alexey Dobriyan47145212009-12-14 18:00:08 -08001729static const struct dev_pm_ops at_dma_dev_pm_ops = {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001730 .prepare = at_dma_prepare,
Dan Williams33f82d12009-09-10 00:06:44 +02001731 .suspend_noirq = at_dma_suspend_noirq,
1732 .resume_noirq = at_dma_resume_noirq,
1733};
1734
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001735static struct platform_driver at_dma_driver = {
Maxin B. John1d1bbd32013-02-20 02:07:04 +02001736 .remove = at_dma_remove,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001737 .shutdown = at_dma_shutdown,
Nicolas Ferre67348452011-10-17 14:56:40 +02001738 .id_table = atdma_devtypes,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001739 .driver = {
1740 .name = "at_hdmac",
Dan Williams33f82d12009-09-10 00:06:44 +02001741 .pm = &at_dma_dev_pm_ops,
Nicolas Ferrec5115952011-10-17 14:56:41 +02001742 .of_match_table = of_match_ptr(atmel_dma_dt_ids),
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001743 },
1744};
1745
1746static int __init at_dma_init(void)
1747{
1748 return platform_driver_probe(&at_dma_driver, at_dma_probe);
1749}
Eric Xu93d0bec2011-01-12 15:39:08 +01001750subsys_initcall(at_dma_init);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001751
1752static void __exit at_dma_exit(void)
1753{
1754 platform_driver_unregister(&at_dma_driver);
1755}
1756module_exit(at_dma_exit);
1757
1758MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1759MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1760MODULE_LICENSE("GPL");
1761MODULE_ALIAS("platform:at_hdmac");