blob: 57b2141ddddc1abca862197b78f8bd2e1cb51f57 [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))
Ludovic Desroches816070e2015-01-06 17:36:26 +010045#define ATC_DMA_BUSWIDTHS\
46 (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) |\
47 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |\
48 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |\
49 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
Nicolas Ferredc78baa2009-07-03 19:24:33 +020050
51/*
52 * Initial number of descriptors to allocate for each channel. This could
53 * be increased during dma usage.
54 */
55static unsigned int init_nr_desc_per_channel = 64;
56module_param(init_nr_desc_per_channel, uint, 0644);
57MODULE_PARM_DESC(init_nr_desc_per_channel,
58 "initial descriptors per channel (default: 64)");
59
60
61/* prototypes */
62static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx);
Elen Songd48de6f2013-05-10 11:01:46 +080063static void atc_issue_pending(struct dma_chan *chan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +020064
65
66/*----------------------------------------------------------------------*/
67
Torsten Fleischer265567f2015-02-23 17:54:11 +010068static inline unsigned int atc_get_xfer_width(dma_addr_t src, dma_addr_t dst,
69 size_t len)
70{
71 unsigned int width;
72
73 if (!((src | dst | len) & 3))
74 width = 2;
75 else if (!((src | dst | len) & 1))
76 width = 1;
77 else
78 width = 0;
79
80 return width;
81}
82
Nicolas Ferredc78baa2009-07-03 19:24:33 +020083static struct at_desc *atc_first_active(struct at_dma_chan *atchan)
84{
85 return list_first_entry(&atchan->active_list,
86 struct at_desc, desc_node);
87}
88
89static struct at_desc *atc_first_queued(struct at_dma_chan *atchan)
90{
91 return list_first_entry(&atchan->queue,
92 struct at_desc, desc_node);
93}
94
95/**
Uwe Kleine-König421f91d2010-06-11 12:17:00 +020096 * atc_alloc_descriptor - allocate and return an initialized descriptor
Nicolas Ferredc78baa2009-07-03 19:24:33 +020097 * @chan: the channel to allocate descriptors for
98 * @gfp_flags: GFP allocation flags
99 *
100 * Note: The ack-bit is positioned in the descriptor flag at creation time
101 * to make initial allocation more convenient. This bit will be cleared
102 * and control will be given to client at usage time (during
103 * preparation functions).
104 */
105static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan,
106 gfp_t gfp_flags)
107{
108 struct at_desc *desc = NULL;
109 struct at_dma *atdma = to_at_dma(chan->device);
110 dma_addr_t phys;
111
112 desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys);
113 if (desc) {
114 memset(desc, 0, sizeof(struct at_desc));
Dan Williams285a3c72009-09-08 17:53:03 -0700115 INIT_LIST_HEAD(&desc->tx_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200116 dma_async_tx_descriptor_init(&desc->txd, chan);
117 /* txd.flags will be overwritten in prep functions */
118 desc->txd.flags = DMA_CTRL_ACK;
119 desc->txd.tx_submit = atc_tx_submit;
120 desc->txd.phys = phys;
121 }
122
123 return desc;
124}
125
126/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200127 * atc_desc_get - get an unused descriptor from free_list
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200128 * @atchan: channel we want a new descriptor for
129 */
130static struct at_desc *atc_desc_get(struct at_dma_chan *atchan)
131{
132 struct at_desc *desc, *_desc;
133 struct at_desc *ret = NULL;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000134 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200135 unsigned int i = 0;
136 LIST_HEAD(tmp_list);
137
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000138 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200139 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
140 i++;
141 if (async_tx_test_ack(&desc->txd)) {
142 list_del(&desc->desc_node);
143 ret = desc;
144 break;
145 }
146 dev_dbg(chan2dev(&atchan->chan_common),
147 "desc %p not ACKed\n", desc);
148 }
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000149 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200150 dev_vdbg(chan2dev(&atchan->chan_common),
151 "scanned %u descriptors on freelist\n", i);
152
153 /* no more descriptor available in initial pool: create one more */
154 if (!ret) {
155 ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC);
156 if (ret) {
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000157 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200158 atchan->descs_allocated++;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000159 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200160 } else {
161 dev_err(chan2dev(&atchan->chan_common),
162 "not enough descriptors available\n");
163 }
164 }
165
166 return ret;
167}
168
169/**
170 * atc_desc_put - move a descriptor, including any children, to the free list
171 * @atchan: channel we work on
172 * @desc: descriptor, at the head of a chain, to move to free list
173 */
174static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc)
175{
176 if (desc) {
177 struct at_desc *child;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000178 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200179
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000180 spin_lock_irqsave(&atchan->lock, flags);
Dan Williams285a3c72009-09-08 17:53:03 -0700181 list_for_each_entry(child, &desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200182 dev_vdbg(chan2dev(&atchan->chan_common),
183 "moving child desc %p to freelist\n",
184 child);
Dan Williams285a3c72009-09-08 17:53:03 -0700185 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200186 dev_vdbg(chan2dev(&atchan->chan_common),
187 "moving desc %p to freelist\n", desc);
188 list_add(&desc->desc_node, &atchan->free_list);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000189 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200190 }
191}
192
193/**
Masanari Iidad73111c2012-08-04 23:37:53 +0900194 * atc_desc_chain - build chain adding a descriptor
195 * @first: address of first descriptor of the chain
196 * @prev: address of previous descriptor of the chain
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200197 * @desc: descriptor to queue
198 *
199 * Called from prep_* functions
200 */
201static void atc_desc_chain(struct at_desc **first, struct at_desc **prev,
202 struct at_desc *desc)
203{
204 if (!(*first)) {
205 *first = desc;
206 } else {
207 /* inform the HW lli about chaining */
208 (*prev)->lli.dscr = desc->txd.phys;
209 /* insert the link descriptor to the LD ring */
210 list_add_tail(&desc->desc_node,
211 &(*first)->tx_list);
212 }
213 *prev = desc;
214}
215
216/**
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200217 * atc_dostart - starts the DMA engine for real
218 * @atchan: the channel we want to start
219 * @first: first descriptor in the list we want to begin with
220 *
221 * Called with atchan->lock held and bh disabled
222 */
223static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
224{
225 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
226
227 /* ASSERT: channel is idle */
228 if (atc_chan_is_enabled(atchan)) {
229 dev_err(chan2dev(&atchan->chan_common),
230 "BUG: Attempted to start non-idle channel\n");
231 dev_err(chan2dev(&atchan->chan_common),
232 " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
233 channel_readl(atchan, SADDR),
234 channel_readl(atchan, DADDR),
235 channel_readl(atchan, CTRLA),
236 channel_readl(atchan, CTRLB),
237 channel_readl(atchan, DSCR));
238
239 /* The tasklet will hopefully advance the queue... */
240 return;
241 }
242
243 vdbg_dump_regs(atchan);
244
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200245 channel_writel(atchan, SADDR, 0);
246 channel_writel(atchan, DADDR, 0);
247 channel_writel(atchan, CTRLA, 0);
248 channel_writel(atchan, CTRLB, 0);
249 channel_writel(atchan, DSCR, first->txd.phys);
250 dma_writel(atdma, CHER, atchan->mask);
251
252 vdbg_dump_regs(atchan);
253}
254
Elen Songd48de6f2013-05-10 11:01:46 +0800255/*
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100256 * atc_get_desc_by_cookie - get the descriptor of a cookie
257 * @atchan: the DMA channel
258 * @cookie: the cookie to get the descriptor for
Elen Songd48de6f2013-05-10 11:01:46 +0800259 */
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100260static struct at_desc *atc_get_desc_by_cookie(struct at_dma_chan *atchan,
261 dma_cookie_t cookie)
Elen Songd48de6f2013-05-10 11:01:46 +0800262{
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100263 struct at_desc *desc, *_desc;
264
265 list_for_each_entry_safe(desc, _desc, &atchan->queue, desc_node) {
266 if (desc->txd.cookie == cookie)
267 return desc;
268 }
Elen Songd48de6f2013-05-10 11:01:46 +0800269
270 list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100271 if (desc->txd.cookie == cookie)
272 return desc;
Elen Songd48de6f2013-05-10 11:01:46 +0800273 }
274
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100275 return NULL;
Elen Songd48de6f2013-05-10 11:01:46 +0800276}
277
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100278/**
279 * atc_calc_bytes_left - calculates the number of bytes left according to the
280 * value read from CTRLA.
281 *
282 * @current_len: the number of bytes left before reading CTRLA
283 * @ctrla: the value of CTRLA
284 * @desc: the descriptor containing the transfer width
Elen Songd48de6f2013-05-10 11:01:46 +0800285 */
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100286static inline int atc_calc_bytes_left(int current_len, u32 ctrla,
287 struct at_desc *desc)
288{
289 return current_len - ((ctrla & ATC_BTSIZE_MAX) << desc->tx_width);
290}
291
292/**
293 * atc_calc_bytes_left_from_reg - calculates the number of bytes left according
294 * to the current value of CTRLA.
295 *
296 * @current_len: the number of bytes left before reading CTRLA
297 * @atchan: the channel to read CTRLA for
298 * @desc: the descriptor containing the transfer width
299 */
300static inline int atc_calc_bytes_left_from_reg(int current_len,
301 struct at_dma_chan *atchan, struct at_desc *desc)
302{
303 u32 ctrla = channel_readl(atchan, CTRLA);
304
305 return atc_calc_bytes_left(current_len, ctrla, desc);
306}
307
308/**
309 * atc_get_bytes_left - get the number of bytes residue for a cookie
310 * @chan: DMA channel
311 * @cookie: transaction identifier to check status of
312 */
313static int atc_get_bytes_left(struct dma_chan *chan, dma_cookie_t cookie)
Elen Songd48de6f2013-05-10 11:01:46 +0800314{
315 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Elen Songd48de6f2013-05-10 11:01:46 +0800316 struct at_desc *desc_first = atc_first_active(atchan);
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100317 struct at_desc *desc;
318 int ret;
319 u32 ctrla, dscr;
Elen Songd48de6f2013-05-10 11:01:46 +0800320
321 /*
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100322 * If the cookie doesn't match to the currently running transfer then
323 * we can return the total length of the associated DMA transfer,
324 * because it is still queued.
Elen Songd48de6f2013-05-10 11:01:46 +0800325 */
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100326 desc = atc_get_desc_by_cookie(atchan, cookie);
327 if (desc == NULL)
328 return -EINVAL;
329 else if (desc != desc_first)
330 return desc->total_len;
Elen Songd48de6f2013-05-10 11:01:46 +0800331
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100332 /* cookie matches to the currently running transfer */
333 ret = desc_first->total_len;
Alexandre Belloni6758dda2014-08-01 18:51:46 +0200334
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100335 if (desc_first->lli.dscr) {
336 /* hardware linked list transfer */
Alexandre Belloni6758dda2014-08-01 18:51:46 +0200337
Elen Songd48de6f2013-05-10 11:01:46 +0800338 /*
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100339 * Calculate the residue by removing the length of the child
340 * descriptors already transferred from the total length.
341 * To get the current child descriptor we can use the value of
342 * the channel's DSCR register and compare it against the value
343 * of the hardware linked list structure of each child
344 * descriptor.
Elen Songd48de6f2013-05-10 11:01:46 +0800345 */
Elen Songd48de6f2013-05-10 11:01:46 +0800346
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100347 ctrla = channel_readl(atchan, CTRLA);
348 rmb(); /* ensure CTRLA is read before DSCR */
349 dscr = channel_readl(atchan, DSCR);
350
351 /* for the first descriptor we can be more accurate */
352 if (desc_first->lli.dscr == dscr)
353 return atc_calc_bytes_left(ret, ctrla, desc_first);
354
355 ret -= desc_first->len;
356 list_for_each_entry(desc, &desc_first->tx_list, desc_node) {
357 if (desc->lli.dscr == dscr)
358 break;
359
360 ret -= desc->len;
361 }
362
363 /*
364 * For the last descriptor in the chain we can calculate
365 * the remaining bytes using the channel's register.
366 * Note that the transfer width of the first and last
367 * descriptor may differ.
368 */
369 if (!desc->lli.dscr)
370 ret = atc_calc_bytes_left_from_reg(ret, atchan, desc);
371 } else {
372 /* single transfer */
373 ret = atc_calc_bytes_left_from_reg(ret, atchan, desc_first);
374 }
375
Elen Songd48de6f2013-05-10 11:01:46 +0800376 return ret;
377}
378
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200379/**
380 * atc_chain_complete - finish work for one transaction chain
381 * @atchan: channel we work on
382 * @desc: descriptor at the head of the chain we want do complete
383 *
384 * Called with atchan->lock held and bh disabled */
385static void
386atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
387{
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200388 struct dma_async_tx_descriptor *txd = &desc->txd;
389
390 dev_vdbg(chan2dev(&atchan->chan_common),
391 "descriptor %u complete\n", txd->cookie);
392
Vinod Kould4116052012-05-11 11:48:21 +0530393 /* mark the descriptor as complete for non cyclic cases only */
394 if (!atc_chan_is_cyclic(atchan))
395 dma_cookie_complete(txd);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200396
397 /* move children to free_list */
Dan Williams285a3c72009-09-08 17:53:03 -0700398 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200399 /* move myself to free_list */
400 list_move(&desc->desc_node, &atchan->free_list);
401
Dan Williamsd38a8c62013-10-18 19:35:23 +0200402 dma_descriptor_unmap(txd);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200403 /* for cyclic transfers,
404 * no need to replay callback function while stopping */
Nicolas Ferre3c477482011-07-25 21:09:23 +0000405 if (!atc_chan_is_cyclic(atchan)) {
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200406 dma_async_tx_callback callback = txd->callback;
407 void *param = txd->callback_param;
408
409 /*
410 * The API requires that no submissions are done from a
411 * callback, so we don't need to drop the lock here
412 */
413 if (callback)
414 callback(param);
415 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200416
417 dma_run_dependencies(txd);
418}
419
420/**
421 * atc_complete_all - finish work for all transactions
422 * @atchan: channel to complete transactions for
423 *
424 * Eventually submit queued descriptors if any
425 *
426 * Assume channel is idle while calling this function
427 * Called with atchan->lock held and bh disabled
428 */
429static void atc_complete_all(struct at_dma_chan *atchan)
430{
431 struct at_desc *desc, *_desc;
432 LIST_HEAD(list);
433
434 dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
435
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200436 /*
437 * Submit queued descriptors ASAP, i.e. before we go through
438 * the completed ones.
439 */
440 if (!list_empty(&atchan->queue))
441 atc_dostart(atchan, atc_first_queued(atchan));
442 /* empty active_list now it is completed */
443 list_splice_init(&atchan->active_list, &list);
444 /* empty queue list by moving descriptors (if any) to active_list */
445 list_splice_init(&atchan->queue, &atchan->active_list);
446
447 list_for_each_entry_safe(desc, _desc, &list, desc_node)
448 atc_chain_complete(atchan, desc);
449}
450
451/**
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200452 * atc_advance_work - at the end of a transaction, move forward
453 * @atchan: channel where the transaction ended
454 *
455 * Called with atchan->lock held and bh disabled
456 */
457static void atc_advance_work(struct at_dma_chan *atchan)
458{
459 dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
460
Ludovic Desrochesd202f052013-04-18 09:52:59 +0200461 if (atc_chan_is_enabled(atchan))
462 return;
463
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200464 if (list_empty(&atchan->active_list) ||
465 list_is_singular(&atchan->active_list)) {
466 atc_complete_all(atchan);
467 } else {
468 atc_chain_complete(atchan, atc_first_active(atchan));
469 /* advance work */
470 atc_dostart(atchan, atc_first_active(atchan));
471 }
472}
473
474
475/**
476 * atc_handle_error - handle errors reported by DMA controller
477 * @atchan: channel where error occurs
478 *
479 * Called with atchan->lock held and bh disabled
480 */
481static void atc_handle_error(struct at_dma_chan *atchan)
482{
483 struct at_desc *bad_desc;
484 struct at_desc *child;
485
486 /*
487 * The descriptor currently at the head of the active list is
488 * broked. Since we don't have any way to report errors, we'll
489 * just have to scream loudly and try to carry on.
490 */
491 bad_desc = atc_first_active(atchan);
492 list_del_init(&bad_desc->desc_node);
493
494 /* As we are stopped, take advantage to push queued descriptors
495 * in active_list */
496 list_splice_init(&atchan->queue, atchan->active_list.prev);
497
498 /* Try to restart the controller */
499 if (!list_empty(&atchan->active_list))
500 atc_dostart(atchan, atc_first_active(atchan));
501
502 /*
503 * KERN_CRITICAL may seem harsh, but since this only happens
504 * when someone submits a bad physical address in a
505 * descriptor, we should consider ourselves lucky that the
506 * controller flagged an error instead of scribbling over
507 * random memory locations.
508 */
509 dev_crit(chan2dev(&atchan->chan_common),
510 "Bad descriptor submitted for DMA!\n");
511 dev_crit(chan2dev(&atchan->chan_common),
512 " cookie: %d\n", bad_desc->txd.cookie);
513 atc_dump_lli(atchan, &bad_desc->lli);
Dan Williams285a3c72009-09-08 17:53:03 -0700514 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200515 atc_dump_lli(atchan, &child->lli);
516
517 /* Pretend the descriptor completed successfully */
518 atc_chain_complete(atchan, bad_desc);
519}
520
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200521/**
522 * atc_handle_cyclic - at the end of a period, run callback function
523 * @atchan: channel used for cyclic operations
524 *
525 * Called with atchan->lock held and bh disabled
526 */
527static void atc_handle_cyclic(struct at_dma_chan *atchan)
528{
529 struct at_desc *first = atc_first_active(atchan);
530 struct dma_async_tx_descriptor *txd = &first->txd;
531 dma_async_tx_callback callback = txd->callback;
532 void *param = txd->callback_param;
533
534 dev_vdbg(chan2dev(&atchan->chan_common),
535 "new cyclic period llp 0x%08x\n",
536 channel_readl(atchan, DSCR));
537
538 if (callback)
539 callback(param);
540}
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200541
542/*-- IRQ & Tasklet ---------------------------------------------------*/
543
544static void atc_tasklet(unsigned long data)
545{
546 struct at_dma_chan *atchan = (struct at_dma_chan *)data;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000547 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200548
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000549 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200550 if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200551 atc_handle_error(atchan);
Nicolas Ferre3c477482011-07-25 21:09:23 +0000552 else if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200553 atc_handle_cyclic(atchan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200554 else
555 atc_advance_work(atchan);
556
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000557 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200558}
559
560static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
561{
562 struct at_dma *atdma = (struct at_dma *)dev_id;
563 struct at_dma_chan *atchan;
564 int i;
565 u32 status, pending, imr;
566 int ret = IRQ_NONE;
567
568 do {
569 imr = dma_readl(atdma, EBCIMR);
570 status = dma_readl(atdma, EBCISR);
571 pending = status & imr;
572
573 if (!pending)
574 break;
575
576 dev_vdbg(atdma->dma_common.dev,
577 "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
578 status, imr, pending);
579
580 for (i = 0; i < atdma->dma_common.chancnt; i++) {
581 atchan = &atdma->chan[i];
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200582 if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200583 if (pending & AT_DMA_ERR(i)) {
584 /* Disable channel on AHB error */
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200585 dma_writel(atdma, CHDR,
586 AT_DMA_RES(i) | atchan->mask);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200587 /* Give information to tasklet */
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200588 set_bit(ATC_IS_ERROR, &atchan->status);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200589 }
590 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 */
Torsten Fleischer265567f2015-02-23 17:54:11 +0100677 src_width = dst_width = atc_get_xfer_width(src, dest, len);
678
679 ctrla = ATC_SRC_WIDTH(src_width) |
680 ATC_DST_WIDTH(dst_width);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200681
682 for (offset = 0; offset < len; offset += xfer_count << src_width) {
683 xfer_count = min_t(size_t, (len - offset) >> src_width,
684 ATC_BTSIZE_MAX);
685
686 desc = atc_desc_get(atchan);
687 if (!desc)
688 goto err_desc_get;
689
690 desc->lli.saddr = src + offset;
691 desc->lli.daddr = dest + offset;
692 desc->lli.ctrla = ctrla | xfer_count;
693 desc->lli.ctrlb = ctrlb;
694
695 desc->txd.cookie = 0;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100696 desc->len = xfer_count << src_width;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200697
Nicolas Ferree257e152011-05-06 19:56:53 +0200698 atc_desc_chain(&first, &prev, desc);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200699 }
700
701 /* First descriptor of the chain embedds additional information */
702 first->txd.cookie = -EBUSY;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100703 first->total_len = len;
704
705 /* set transfer width for the calculation of the residue */
Elen Songd088c332013-05-10 11:00:50 +0800706 first->tx_width = src_width;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100707 prev->tx_width = src_width;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200708
709 /* set end-of-link to the last link descriptor of list*/
710 set_desc_eol(desc);
711
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100712 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200713
714 return &first->txd;
715
716err_desc_get:
717 atc_desc_put(atchan, first);
718 return NULL;
719}
720
Nicolas Ferre808347f2009-07-22 20:04:45 +0200721
722/**
723 * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
724 * @chan: DMA channel
725 * @sgl: scatterlist to transfer to/from
726 * @sg_len: number of entries in @scatterlist
727 * @direction: DMA direction
728 * @flags: tx descriptor status flags
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500729 * @context: transaction context (ignored)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200730 */
731static struct dma_async_tx_descriptor *
732atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530733 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500734 unsigned long flags, void *context)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200735{
736 struct at_dma_chan *atchan = to_at_dma_chan(chan);
737 struct at_dma_slave *atslave = chan->private;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100738 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200739 struct at_desc *first = NULL;
740 struct at_desc *prev = NULL;
741 u32 ctrla;
742 u32 ctrlb;
743 dma_addr_t reg;
744 unsigned int reg_width;
745 unsigned int mem_width;
746 unsigned int i;
747 struct scatterlist *sg;
748 size_t total_len = 0;
749
Nicolas Ferrecc52a102011-04-30 16:57:47 +0200750 dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
751 sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530752 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre808347f2009-07-22 20:04:45 +0200753 flags);
754
755 if (unlikely(!atslave || !sg_len)) {
Nicolas Ferrec618a9b2012-09-11 17:21:44 +0200756 dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n");
Nicolas Ferre808347f2009-07-22 20:04:45 +0200757 return NULL;
758 }
759
Nicolas Ferre1dd1ea82012-05-10 12:17:41 +0200760 ctrla = ATC_SCSIZE(sconfig->src_maxburst)
761 | ATC_DCSIZE(sconfig->dst_maxburst);
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200762 ctrlb = ATC_IEN;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200763
764 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530765 case DMA_MEM_TO_DEV:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100766 reg_width = convert_buswidth(sconfig->dst_addr_width);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200767 ctrla |= ATC_DST_WIDTH(reg_width);
768 ctrlb |= ATC_DST_ADDR_MODE_FIXED
769 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200770 | ATC_FC_MEM2PER
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +0000771 | ATC_SIF(atchan->mem_if) | ATC_DIF(atchan->per_if);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100772 reg = sconfig->dst_addr;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200773 for_each_sg(sgl, sg, sg_len, i) {
774 struct at_desc *desc;
775 u32 len;
776 u32 mem;
777
778 desc = atc_desc_get(atchan);
779 if (!desc)
780 goto err_desc_get;
781
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100782 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200783 len = sg_dma_len(sg);
Nicolas Ferrec4567972012-09-11 17:21:45 +0200784 if (unlikely(!len)) {
785 dev_dbg(chan2dev(chan),
786 "prep_slave_sg: sg(%d) data length is zero\n", i);
787 goto err;
788 }
Nicolas Ferre808347f2009-07-22 20:04:45 +0200789 mem_width = 2;
790 if (unlikely(mem & 3 || len & 3))
791 mem_width = 0;
792
793 desc->lli.saddr = mem;
794 desc->lli.daddr = reg;
795 desc->lli.ctrla = ctrla
796 | ATC_SRC_WIDTH(mem_width)
797 | len >> mem_width;
798 desc->lli.ctrlb = ctrlb;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100799 desc->len = len;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200800
Nicolas Ferree257e152011-05-06 19:56:53 +0200801 atc_desc_chain(&first, &prev, desc);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200802 total_len += len;
803 }
804 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530805 case DMA_DEV_TO_MEM:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100806 reg_width = convert_buswidth(sconfig->src_addr_width);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200807 ctrla |= ATC_SRC_WIDTH(reg_width);
808 ctrlb |= ATC_DST_ADDR_MODE_INCR
809 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200810 | ATC_FC_PER2MEM
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +0000811 | ATC_SIF(atchan->per_if) | ATC_DIF(atchan->mem_if);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200812
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100813 reg = sconfig->src_addr;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200814 for_each_sg(sgl, sg, sg_len, i) {
815 struct at_desc *desc;
816 u32 len;
817 u32 mem;
818
819 desc = atc_desc_get(atchan);
820 if (!desc)
821 goto err_desc_get;
822
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100823 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200824 len = sg_dma_len(sg);
Nicolas Ferrec4567972012-09-11 17:21:45 +0200825 if (unlikely(!len)) {
826 dev_dbg(chan2dev(chan),
827 "prep_slave_sg: sg(%d) data length is zero\n", i);
828 goto err;
829 }
Nicolas Ferre808347f2009-07-22 20:04:45 +0200830 mem_width = 2;
831 if (unlikely(mem & 3 || len & 3))
832 mem_width = 0;
833
834 desc->lli.saddr = reg;
835 desc->lli.daddr = mem;
836 desc->lli.ctrla = ctrla
837 | ATC_DST_WIDTH(mem_width)
Nicolas Ferre59a609d2010-12-13 13:48:41 +0100838 | len >> reg_width;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200839 desc->lli.ctrlb = ctrlb;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100840 desc->len = len;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200841
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;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100855 first->total_len = total_len;
856
857 /* set transfer width for the calculation of the residue */
Elen Songd088c332013-05-10 11:00:50 +0800858 first->tx_width = reg_width;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100859 prev->tx_width = reg_width;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200860
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100861 /* first link descriptor of list is responsible of flags */
862 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferre808347f2009-07-22 20:04:45 +0200863
864 return &first->txd;
865
866err_desc_get:
867 dev_err(chan2dev(chan), "not enough descriptors available\n");
Nicolas Ferrec4567972012-09-11 17:21:45 +0200868err:
Nicolas Ferre808347f2009-07-22 20:04:45 +0200869 atc_desc_put(atchan, first);
870 return NULL;
871}
872
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200873/**
Torsten Fleischer265567f2015-02-23 17:54:11 +0100874 * atc_prep_dma_sg - prepare memory to memory scather-gather operation
875 * @chan: the channel to prepare operation on
876 * @dst_sg: destination scatterlist
877 * @dst_nents: number of destination scatterlist entries
878 * @src_sg: source scatterlist
879 * @src_nents: number of source scatterlist entries
880 * @flags: tx descriptor status flags
881 */
882static struct dma_async_tx_descriptor *
883atc_prep_dma_sg(struct dma_chan *chan,
884 struct scatterlist *dst_sg, unsigned int dst_nents,
885 struct scatterlist *src_sg, unsigned int src_nents,
886 unsigned long flags)
887{
888 struct at_dma_chan *atchan = to_at_dma_chan(chan);
889 struct at_desc *desc = NULL;
890 struct at_desc *first = NULL;
891 struct at_desc *prev = NULL;
892 unsigned int src_width;
893 unsigned int dst_width;
894 size_t xfer_count;
895 u32 ctrla;
896 u32 ctrlb;
897 size_t dst_len = 0, src_len = 0;
898 dma_addr_t dst = 0, src = 0;
899 size_t len = 0, total_len = 0;
900
901 if (unlikely(dst_nents == 0 || src_nents == 0))
902 return NULL;
903
904 if (unlikely(dst_sg == NULL || src_sg == NULL))
905 return NULL;
906
907 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
908 | ATC_SRC_ADDR_MODE_INCR
909 | ATC_DST_ADDR_MODE_INCR
910 | ATC_FC_MEM2MEM;
911
912 /*
913 * loop until there is either no more source or no more destination
914 * scatterlist entry
915 */
916 while (true) {
917
918 /* prepare the next transfer */
919 if (dst_len == 0) {
920
921 /* no more destination scatterlist entries */
922 if (!dst_sg || !dst_nents)
923 break;
924
925 dst = sg_dma_address(dst_sg);
926 dst_len = sg_dma_len(dst_sg);
927
928 dst_sg = sg_next(dst_sg);
929 dst_nents--;
930 }
931
932 if (src_len == 0) {
933
934 /* no more source scatterlist entries */
935 if (!src_sg || !src_nents)
936 break;
937
938 src = sg_dma_address(src_sg);
939 src_len = sg_dma_len(src_sg);
940
941 src_sg = sg_next(src_sg);
942 src_nents--;
943 }
944
945 len = min_t(size_t, src_len, dst_len);
946 if (len == 0)
947 continue;
948
949 /* take care for the alignment */
950 src_width = dst_width = atc_get_xfer_width(src, dst, len);
951
952 ctrla = ATC_SRC_WIDTH(src_width) |
953 ATC_DST_WIDTH(dst_width);
954
955 /*
956 * The number of transfers to set up refer to the source width
957 * that depends on the alignment.
958 */
959 xfer_count = len >> src_width;
960 if (xfer_count > ATC_BTSIZE_MAX) {
961 xfer_count = ATC_BTSIZE_MAX;
962 len = ATC_BTSIZE_MAX << src_width;
963 }
964
965 /* create the transfer */
966 desc = atc_desc_get(atchan);
967 if (!desc)
968 goto err_desc_get;
969
970 desc->lli.saddr = src;
971 desc->lli.daddr = dst;
972 desc->lli.ctrla = ctrla | xfer_count;
973 desc->lli.ctrlb = ctrlb;
974
975 desc->txd.cookie = 0;
976 desc->len = len;
977
978 /*
979 * Although we only need the transfer width for the first and
980 * the last descriptor, its easier to set it to all descriptors.
981 */
982 desc->tx_width = src_width;
983
984 atc_desc_chain(&first, &prev, desc);
985
986 /* update the lengths and addresses for the next loop cycle */
987 dst_len -= len;
988 src_len -= len;
989 dst += len;
990 src += len;
991
992 total_len += len;
993 }
994
995 /* First descriptor of the chain embedds additional information */
996 first->txd.cookie = -EBUSY;
997 first->total_len = total_len;
998
999 /* set end-of-link to the last link descriptor of list*/
1000 set_desc_eol(desc);
1001
1002 first->txd.flags = flags; /* client is in control of this ack */
1003
1004 return &first->txd;
1005
1006err_desc_get:
1007 atc_desc_put(atchan, first);
1008 return NULL;
1009}
1010
1011/**
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001012 * atc_dma_cyclic_check_values
1013 * Check for too big/unaligned periods and unaligned DMA buffer
1014 */
1015static int
1016atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
Andy Shevchenko0e7264c2013-01-10 10:52:57 +02001017 size_t period_len)
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001018{
1019 if (period_len > (ATC_BTSIZE_MAX << reg_width))
1020 goto err_out;
1021 if (unlikely(period_len & ((1 << reg_width) - 1)))
1022 goto err_out;
1023 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1024 goto err_out;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001025
1026 return 0;
1027
1028err_out:
1029 return -EINVAL;
1030}
1031
1032/**
Masanari Iidad73111c2012-08-04 23:37:53 +09001033 * atc_dma_cyclic_fill_desc - Fill one period descriptor
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001034 */
1035static int
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001036atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001037 unsigned int period_index, dma_addr_t buf_addr,
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001038 unsigned int reg_width, size_t period_len,
1039 enum dma_transfer_direction direction)
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001040{
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001041 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001042 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
1043 u32 ctrla;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001044
1045 /* prepare common CRTLA value */
Nicolas Ferre1dd1ea82012-05-10 12:17:41 +02001046 ctrla = ATC_SCSIZE(sconfig->src_maxburst)
1047 | ATC_DCSIZE(sconfig->dst_maxburst)
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001048 | ATC_DST_WIDTH(reg_width)
1049 | ATC_SRC_WIDTH(reg_width)
1050 | period_len >> reg_width;
1051
1052 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +05301053 case DMA_MEM_TO_DEV:
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001054 desc->lli.saddr = buf_addr + (period_len * period_index);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001055 desc->lli.daddr = sconfig->dst_addr;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001056 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +02001057 desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001058 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +02001059 | ATC_FC_MEM2PER
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001060 | ATC_SIF(atchan->mem_if)
1061 | ATC_DIF(atchan->per_if);
Torsten Fleischerbdf6c792015-02-23 17:54:10 +01001062 desc->len = period_len;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001063 break;
1064
Vinod Kouldb8196d2011-10-13 22:34:23 +05301065 case DMA_DEV_TO_MEM:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001066 desc->lli.saddr = sconfig->src_addr;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001067 desc->lli.daddr = buf_addr + (period_len * period_index);
1068 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +02001069 desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001070 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +02001071 | ATC_FC_PER2MEM
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001072 | ATC_SIF(atchan->per_if)
1073 | ATC_DIF(atchan->mem_if);
Torsten Fleischerbdf6c792015-02-23 17:54:10 +01001074 desc->len = period_len;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001075 break;
1076
1077 default:
1078 return -EINVAL;
1079 }
1080
1081 return 0;
1082}
1083
1084/**
1085 * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
1086 * @chan: the DMA channel to prepare
1087 * @buf_addr: physical DMA address where the buffer starts
1088 * @buf_len: total number of bytes for the entire buffer
1089 * @period_len: number of bytes for each period
1090 * @direction: transfer direction, to or from device
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03001091 * @flags: tx descriptor status flags
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001092 */
1093static struct dma_async_tx_descriptor *
1094atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05001095 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +02001096 unsigned long flags)
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001097{
1098 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1099 struct at_dma_slave *atslave = chan->private;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001100 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001101 struct at_desc *first = NULL;
1102 struct at_desc *prev = NULL;
1103 unsigned long was_cyclic;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001104 unsigned int reg_width;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001105 unsigned int periods = buf_len / period_len;
1106 unsigned int i;
1107
1108 dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
Vinod Kouldb8196d2011-10-13 22:34:23 +05301109 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001110 buf_addr,
1111 periods, buf_len, period_len);
1112
1113 if (unlikely(!atslave || !buf_len || !period_len)) {
1114 dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
1115 return NULL;
1116 }
1117
1118 was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
1119 if (was_cyclic) {
1120 dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
1121 return NULL;
1122 }
1123
Andy Shevchenko0e7264c2013-01-10 10:52:57 +02001124 if (unlikely(!is_slave_direction(direction)))
1125 goto err_out;
1126
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001127 if (sconfig->direction == DMA_MEM_TO_DEV)
1128 reg_width = convert_buswidth(sconfig->dst_addr_width);
1129 else
1130 reg_width = convert_buswidth(sconfig->src_addr_width);
1131
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001132 /* Check for too big/unaligned periods and unaligned DMA buffer */
Andy Shevchenko0e7264c2013-01-10 10:52:57 +02001133 if (atc_dma_cyclic_check_values(reg_width, buf_addr, period_len))
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001134 goto err_out;
1135
1136 /* build cyclic linked list */
1137 for (i = 0; i < periods; i++) {
1138 struct at_desc *desc;
1139
1140 desc = atc_desc_get(atchan);
1141 if (!desc)
1142 goto err_desc_get;
1143
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001144 if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
1145 reg_width, period_len, direction))
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001146 goto err_desc_get;
1147
1148 atc_desc_chain(&first, &prev, desc);
1149 }
1150
1151 /* lets make a cyclic list */
1152 prev->lli.dscr = first->txd.phys;
1153
1154 /* First descriptor of the chain embedds additional information */
1155 first->txd.cookie = -EBUSY;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +01001156 first->total_len = buf_len;
Elen Songd088c332013-05-10 11:00:50 +08001157 first->tx_width = reg_width;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001158
1159 return &first->txd;
1160
1161err_desc_get:
1162 dev_err(chan2dev(chan), "not enough descriptors available\n");
1163 atc_desc_put(atchan, first);
1164err_out:
1165 clear_bit(ATC_IS_CYCLIC, &atchan->status);
1166 return NULL;
1167}
1168
Maxime Ripard4facfe72014-11-17 14:42:06 +01001169static int atc_config(struct dma_chan *chan,
1170 struct dma_slave_config *sconfig)
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001171{
1172 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1173
Maxime Ripard4facfe72014-11-17 14:42:06 +01001174 dev_vdbg(chan2dev(chan), "%s\n", __func__);
1175
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001176 /* Check if it is chan is configured for slave transfers */
1177 if (!chan->private)
1178 return -EINVAL;
1179
1180 memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
1181
1182 convert_burst(&atchan->dma_sconfig.src_maxburst);
1183 convert_burst(&atchan->dma_sconfig.dst_maxburst);
1184
1185 return 0;
1186}
1187
Maxime Ripard4facfe72014-11-17 14:42:06 +01001188static int atc_pause(struct dma_chan *chan)
Nicolas Ferre808347f2009-07-22 20:04:45 +02001189{
1190 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1191 struct at_dma *atdma = to_at_dma(chan->device);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001192 int chan_id = atchan->chan_common.chan_id;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001193 unsigned long flags;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001194
Nicolas Ferre808347f2009-07-22 20:04:45 +02001195 LIST_HEAD(list);
1196
Maxime Ripard4facfe72014-11-17 14:42:06 +01001197 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001198
Maxime Ripard4facfe72014-11-17 14:42:06 +01001199 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001200
Maxime Ripard4facfe72014-11-17 14:42:06 +01001201 dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
1202 set_bit(ATC_IS_PAUSED, &atchan->status);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001203
Maxime Ripard4facfe72014-11-17 14:42:06 +01001204 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001205
Maxime Ripard4facfe72014-11-17 14:42:06 +01001206 return 0;
1207}
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001208
Maxime Ripard4facfe72014-11-17 14:42:06 +01001209static int atc_resume(struct dma_chan *chan)
1210{
1211 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1212 struct at_dma *atdma = to_at_dma(chan->device);
1213 int chan_id = atchan->chan_common.chan_id;
1214 unsigned long flags;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001215
Maxime Ripard4facfe72014-11-17 14:42:06 +01001216 LIST_HEAD(list);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001217
Maxime Ripard4facfe72014-11-17 14:42:06 +01001218 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001219
Maxime Ripard4facfe72014-11-17 14:42:06 +01001220 if (!atc_chan_is_paused(atchan))
1221 return 0;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001222
Maxime Ripard4facfe72014-11-17 14:42:06 +01001223 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001224
Maxime Ripard4facfe72014-11-17 14:42:06 +01001225 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
1226 clear_bit(ATC_IS_PAUSED, &atchan->status);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001227
Maxime Ripard4facfe72014-11-17 14:42:06 +01001228 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001229
Maxime Ripard4facfe72014-11-17 14:42:06 +01001230 return 0;
1231}
1232
1233static int atc_terminate_all(struct dma_chan *chan)
1234{
1235 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1236 struct at_dma *atdma = to_at_dma(chan->device);
1237 int chan_id = atchan->chan_common.chan_id;
1238 struct at_desc *desc, *_desc;
1239 unsigned long flags;
1240
1241 LIST_HEAD(list);
1242
1243 dev_vdbg(chan2dev(chan), "%s\n", __func__);
1244
1245 /*
1246 * This is only called when something went wrong elsewhere, so
1247 * we don't really care about the data. Just disable the
1248 * channel. We still have to poll the channel enable bit due
1249 * to AHB/HSB limitations.
1250 */
1251 spin_lock_irqsave(&atchan->lock, flags);
1252
1253 /* disabling channel: must also remove suspend state */
1254 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
1255
1256 /* confirm that this channel is disabled */
1257 while (dma_readl(atdma, CHSR) & atchan->mask)
1258 cpu_relax();
1259
1260 /* active_list entries will end up before queued entries */
1261 list_splice_init(&atchan->queue, &list);
1262 list_splice_init(&atchan->active_list, &list);
1263
1264 /* Flush all pending and queued descriptors */
1265 list_for_each_entry_safe(desc, _desc, &list, desc_node)
1266 atc_chain_complete(atchan, desc);
1267
1268 clear_bit(ATC_IS_PAUSED, &atchan->status);
1269 /* if channel dedicated to cyclic operations, free it */
1270 clear_bit(ATC_IS_CYCLIC, &atchan->status);
1271
1272 spin_unlock_irqrestore(&atchan->lock, flags);
Yong Wangb0ebeb92010-08-05 10:40:08 +08001273
Linus Walleijc3635c72010-03-26 16:44:01 -07001274 return 0;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001275}
1276
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001277/**
Linus Walleij07934482010-03-26 16:50:49 -07001278 * atc_tx_status - poll for transaction completion
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001279 * @chan: DMA channel
1280 * @cookie: transaction identifier to check status of
Linus Walleij07934482010-03-26 16:50:49 -07001281 * @txstate: if not %NULL updated with transaction state
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001282 *
Linus Walleij07934482010-03-26 16:50:49 -07001283 * If @txstate is passed in, upon return it reflect the driver
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001284 * internal state and can be used with dma_async_is_complete() to check
1285 * the status of multiple cookies without re-checking hardware state.
1286 */
1287static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -07001288atc_tx_status(struct dma_chan *chan,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001289 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -07001290 struct dma_tx_state *txstate)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001291{
1292 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001293 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001294 enum dma_status ret;
Elen Songd48de6f2013-05-10 11:01:46 +08001295 int bytes = 0;
1296
1297 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul6d203d12013-10-16 13:34:35 +05301298 if (ret == DMA_COMPLETE)
Elen Songd48de6f2013-05-10 11:01:46 +08001299 return ret;
1300 /*
1301 * There's no point calculating the residue if there's
1302 * no txstate to store the value.
1303 */
1304 if (!txstate)
1305 return DMA_ERROR;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001306
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001307 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001308
Elen Songd48de6f2013-05-10 11:01:46 +08001309 /* Get number of bytes left in the active transactions */
Torsten Fleischerbdf6c792015-02-23 17:54:10 +01001310 bytes = atc_get_bytes_left(chan, cookie);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001311
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001312 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001313
Elen Songd48de6f2013-05-10 11:01:46 +08001314 if (unlikely(bytes < 0)) {
1315 dev_vdbg(chan2dev(chan), "get residual bytes error\n");
1316 return DMA_ERROR;
Nicolas Ferrec3dbc602013-06-07 17:26:14 +02001317 } else {
Elen Songd48de6f2013-05-10 11:01:46 +08001318 dma_set_residue(txstate, bytes);
Nicolas Ferrec3dbc602013-06-07 17:26:14 +02001319 }
Nicolas Ferre543aabc2011-05-06 19:56:51 +02001320
Elen Songd48de6f2013-05-10 11:01:46 +08001321 dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d residue = %d\n",
1322 ret, cookie, bytes);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001323
1324 return ret;
1325}
1326
1327/**
1328 * atc_issue_pending - try to finish work
1329 * @chan: target DMA channel
1330 */
1331static void atc_issue_pending(struct dma_chan *chan)
1332{
1333 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001334 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001335
1336 dev_vdbg(chan2dev(chan), "issue_pending\n");
1337
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001338 /* Not needed for cyclic transfers */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001339 if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001340 return;
1341
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001342 spin_lock_irqsave(&atchan->lock, flags);
Ludovic Desrochesd202f052013-04-18 09:52:59 +02001343 atc_advance_work(atchan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001344 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001345}
1346
1347/**
1348 * atc_alloc_chan_resources - allocate resources for DMA channel
1349 * @chan: allocate descriptor resources for this channel
1350 * @client: current client requesting the channel be ready for requests
1351 *
1352 * return - the number of allocated descriptors
1353 */
1354static int atc_alloc_chan_resources(struct dma_chan *chan)
1355{
1356 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1357 struct at_dma *atdma = to_at_dma(chan->device);
1358 struct at_desc *desc;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001359 struct at_dma_slave *atslave;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001360 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001361 int i;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001362 u32 cfg;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001363 LIST_HEAD(tmp_list);
1364
1365 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
1366
1367 /* ASSERT: channel is idle */
1368 if (atc_chan_is_enabled(atchan)) {
1369 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
1370 return -EIO;
1371 }
1372
Nicolas Ferre808347f2009-07-22 20:04:45 +02001373 cfg = ATC_DEFAULT_CFG;
1374
1375 atslave = chan->private;
1376 if (atslave) {
1377 /*
1378 * We need controller-specific data to set up slave
1379 * transfers.
1380 */
1381 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
1382
Nicolas Ferreea7e7902013-05-10 15:19:13 +02001383 /* if cfg configuration specified take it instead of default */
Nicolas Ferre808347f2009-07-22 20:04:45 +02001384 if (atslave->cfg)
1385 cfg = atslave->cfg;
1386 }
1387
1388 /* have we already been set up?
1389 * reconfigure channel but no need to reallocate descriptors */
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001390 if (!list_empty(&atchan->free_list))
1391 return atchan->descs_allocated;
1392
1393 /* Allocate initial pool of descriptors */
1394 for (i = 0; i < init_nr_desc_per_channel; i++) {
1395 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
1396 if (!desc) {
1397 dev_err(atdma->dma_common.dev,
1398 "Only %d initial descriptors\n", i);
1399 break;
1400 }
1401 list_add_tail(&desc->desc_node, &tmp_list);
1402 }
1403
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001404 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001405 atchan->descs_allocated = i;
1406 list_splice(&tmp_list, &atchan->free_list);
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001407 dma_cookie_init(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001408 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001409
1410 /* channel parameters */
Nicolas Ferre808347f2009-07-22 20:04:45 +02001411 channel_writel(atchan, CFG, cfg);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001412
1413 dev_dbg(chan2dev(chan),
1414 "alloc_chan_resources: allocated %d descriptors\n",
1415 atchan->descs_allocated);
1416
1417 return atchan->descs_allocated;
1418}
1419
1420/**
1421 * atc_free_chan_resources - free all channel resources
1422 * @chan: DMA channel
1423 */
1424static void atc_free_chan_resources(struct dma_chan *chan)
1425{
1426 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1427 struct at_dma *atdma = to_at_dma(chan->device);
1428 struct at_desc *desc, *_desc;
1429 LIST_HEAD(list);
1430
1431 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
1432 atchan->descs_allocated);
1433
1434 /* ASSERT: channel is idle */
1435 BUG_ON(!list_empty(&atchan->active_list));
1436 BUG_ON(!list_empty(&atchan->queue));
1437 BUG_ON(atc_chan_is_enabled(atchan));
1438
1439 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
1440 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1441 list_del(&desc->desc_node);
1442 /* free link descriptor */
1443 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
1444 }
1445 list_splice_init(&atchan->free_list, &list);
1446 atchan->descs_allocated = 0;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001447 atchan->status = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001448
1449 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
1450}
1451
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001452#ifdef CONFIG_OF
1453static bool at_dma_filter(struct dma_chan *chan, void *slave)
1454{
1455 struct at_dma_slave *atslave = slave;
1456
1457 if (atslave->dma_dev == chan->device->dev) {
1458 chan->private = atslave;
1459 return true;
1460 } else {
1461 return false;
1462 }
1463}
1464
1465static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
1466 struct of_dma *of_dma)
1467{
1468 struct dma_chan *chan;
1469 struct at_dma_chan *atchan;
1470 struct at_dma_slave *atslave;
1471 dma_cap_mask_t mask;
1472 unsigned int per_id;
1473 struct platform_device *dmac_pdev;
1474
1475 if (dma_spec->args_count != 2)
1476 return NULL;
1477
1478 dmac_pdev = of_find_device_by_node(dma_spec->np);
1479
1480 dma_cap_zero(mask);
1481 dma_cap_set(DMA_SLAVE, mask);
1482
1483 atslave = devm_kzalloc(&dmac_pdev->dev, sizeof(*atslave), GFP_KERNEL);
1484 if (!atslave)
1485 return NULL;
Ludovic Desroches62971b22013-06-13 10:39:39 +02001486
1487 atslave->cfg = ATC_DST_H2SEL_HW | ATC_SRC_H2SEL_HW;
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001488 /*
1489 * We can fill both SRC_PER and DST_PER, one of these fields will be
1490 * ignored depending on DMA transfer direction.
1491 */
Ludovic Desroches62971b22013-06-13 10:39:39 +02001492 per_id = dma_spec->args[1] & AT91_DMA_CFG_PER_ID_MASK;
1493 atslave->cfg |= ATC_DST_PER_MSB(per_id) | ATC_DST_PER(per_id)
Nicolas Ferre6c227702013-05-10 15:19:15 +02001494 | ATC_SRC_PER_MSB(per_id) | ATC_SRC_PER(per_id);
Ludovic Desroches62971b22013-06-13 10:39:39 +02001495 /*
1496 * We have to translate the value we get from the device tree since
1497 * the half FIFO configuration value had to be 0 to keep backward
1498 * compatibility.
1499 */
1500 switch (dma_spec->args[1] & AT91_DMA_CFG_FIFOCFG_MASK) {
1501 case AT91_DMA_CFG_FIFOCFG_ALAP:
1502 atslave->cfg |= ATC_FIFOCFG_LARGESTBURST;
1503 break;
1504 case AT91_DMA_CFG_FIFOCFG_ASAP:
1505 atslave->cfg |= ATC_FIFOCFG_ENOUGHSPACE;
1506 break;
1507 case AT91_DMA_CFG_FIFOCFG_HALF:
1508 default:
1509 atslave->cfg |= ATC_FIFOCFG_HALFFIFO;
1510 }
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001511 atslave->dma_dev = &dmac_pdev->dev;
1512
1513 chan = dma_request_channel(mask, at_dma_filter, atslave);
1514 if (!chan)
1515 return NULL;
1516
1517 atchan = to_at_dma_chan(chan);
1518 atchan->per_if = dma_spec->args[0] & 0xff;
1519 atchan->mem_if = (dma_spec->args[0] >> 16) & 0xff;
1520
1521 return chan;
1522}
1523#else
1524static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
1525 struct of_dma *of_dma)
1526{
1527 return NULL;
1528}
1529#endif
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001530
1531/*-- Module Management -----------------------------------------------*/
1532
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001533/* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
1534static struct at_dma_platform_data at91sam9rl_config = {
1535 .nr_channels = 2,
1536};
1537static struct at_dma_platform_data at91sam9g45_config = {
1538 .nr_channels = 8,
1539};
1540
Nicolas Ferrec5115952011-10-17 14:56:41 +02001541#if defined(CONFIG_OF)
1542static const struct of_device_id atmel_dma_dt_ids[] = {
1543 {
1544 .compatible = "atmel,at91sam9rl-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001545 .data = &at91sam9rl_config,
Nicolas Ferrec5115952011-10-17 14:56:41 +02001546 }, {
1547 .compatible = "atmel,at91sam9g45-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001548 .data = &at91sam9g45_config,
Nicolas Ferredcc81732011-11-22 11:55:53 +01001549 }, {
1550 /* sentinel */
1551 }
Nicolas Ferrec5115952011-10-17 14:56:41 +02001552};
1553
1554MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
1555#endif
1556
Nicolas Ferre0ab88a02011-11-22 11:55:52 +01001557static const struct platform_device_id atdma_devtypes[] = {
Nicolas Ferre67348452011-10-17 14:56:40 +02001558 {
1559 .name = "at91sam9rl_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001560 .driver_data = (unsigned long) &at91sam9rl_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001561 }, {
1562 .name = "at91sam9g45_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001563 .driver_data = (unsigned long) &at91sam9g45_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001564 }, {
1565 /* sentinel */
1566 }
1567};
1568
Uwe Kleine-König7fd63cc2012-07-13 14:32:10 +02001569static inline const struct at_dma_platform_data * __init at_dma_get_driver_data(
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001570 struct platform_device *pdev)
Nicolas Ferrec5115952011-10-17 14:56:41 +02001571{
1572 if (pdev->dev.of_node) {
1573 const struct of_device_id *match;
1574 match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
1575 if (match == NULL)
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001576 return NULL;
1577 return match->data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001578 }
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001579 return (struct at_dma_platform_data *)
1580 platform_get_device_id(pdev)->driver_data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001581}
1582
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001583/**
1584 * at_dma_off - disable DMA controller
1585 * @atdma: the Atmel HDAMC device
1586 */
1587static void at_dma_off(struct at_dma *atdma)
1588{
1589 dma_writel(atdma, EN, 0);
1590
1591 /* disable all interrupts */
1592 dma_writel(atdma, EBCIDR, -1L);
1593
1594 /* confirm that all channels are disabled */
1595 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
1596 cpu_relax();
1597}
1598
1599static int __init at_dma_probe(struct platform_device *pdev)
1600{
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001601 struct resource *io;
1602 struct at_dma *atdma;
1603 size_t size;
1604 int irq;
1605 int err;
1606 int i;
Uwe Kleine-König7fd63cc2012-07-13 14:32:10 +02001607 const struct at_dma_platform_data *plat_dat;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001608
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001609 /* setup platform data for each SoC */
1610 dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
Torsten Fleischer265567f2015-02-23 17:54:11 +01001611 dma_cap_set(DMA_SG, at91sam9rl_config.cap_mask);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001612 dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
1613 dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
Torsten Fleischer265567f2015-02-23 17:54:11 +01001614 dma_cap_set(DMA_SG, at91sam9g45_config.cap_mask);
Nicolas Ferre67348452011-10-17 14:56:40 +02001615
1616 /* get DMA parameters from controller type */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001617 plat_dat = at_dma_get_driver_data(pdev);
1618 if (!plat_dat)
1619 return -ENODEV;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001620
1621 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1622 if (!io)
1623 return -EINVAL;
1624
1625 irq = platform_get_irq(pdev, 0);
1626 if (irq < 0)
1627 return irq;
1628
1629 size = sizeof(struct at_dma);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001630 size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001631 atdma = kzalloc(size, GFP_KERNEL);
1632 if (!atdma)
1633 return -ENOMEM;
1634
Nicolas Ferre67348452011-10-17 14:56:40 +02001635 /* discover transaction capabilities */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001636 atdma->dma_common.cap_mask = plat_dat->cap_mask;
1637 atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001638
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07001639 size = resource_size(io);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001640 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1641 err = -EBUSY;
1642 goto err_kfree;
1643 }
1644
1645 atdma->regs = ioremap(io->start, size);
1646 if (!atdma->regs) {
1647 err = -ENOMEM;
1648 goto err_release_r;
1649 }
1650
1651 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1652 if (IS_ERR(atdma->clk)) {
1653 err = PTR_ERR(atdma->clk);
1654 goto err_clk;
1655 }
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001656 err = clk_prepare_enable(atdma->clk);
1657 if (err)
1658 goto err_clk_prepare;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001659
1660 /* force dma off, just in case */
1661 at_dma_off(atdma);
1662
1663 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1664 if (err)
1665 goto err_irq;
1666
1667 platform_set_drvdata(pdev, atdma);
1668
1669 /* create a pool of consistent memory blocks for hardware descriptors */
1670 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1671 &pdev->dev, sizeof(struct at_desc),
1672 4 /* word alignment */, 0);
1673 if (!atdma->dma_desc_pool) {
1674 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1675 err = -ENOMEM;
1676 goto err_pool_create;
1677 }
1678
1679 /* clear any pending interrupt */
1680 while (dma_readl(atdma, EBCISR))
1681 cpu_relax();
1682
1683 /* initialize channels related values */
1684 INIT_LIST_HEAD(&atdma->dma_common.channels);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001685 for (i = 0; i < plat_dat->nr_channels; i++) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001686 struct at_dma_chan *atchan = &atdma->chan[i];
1687
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001688 atchan->mem_if = AT_DMA_MEM_IF;
1689 atchan->per_if = AT_DMA_PER_IF;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001690 atchan->chan_common.device = &atdma->dma_common;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001691 dma_cookie_init(&atchan->chan_common);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001692 list_add_tail(&atchan->chan_common.device_node,
1693 &atdma->dma_common.channels);
1694
1695 atchan->ch_regs = atdma->regs + ch_regs(i);
1696 spin_lock_init(&atchan->lock);
1697 atchan->mask = 1 << i;
1698
1699 INIT_LIST_HEAD(&atchan->active_list);
1700 INIT_LIST_HEAD(&atchan->queue);
1701 INIT_LIST_HEAD(&atchan->free_list);
1702
1703 tasklet_init(&atchan->tasklet, atc_tasklet,
1704 (unsigned long)atchan);
Nikolaus Vossbda3a472012-01-17 10:28:33 +01001705 atc_enable_chan_irq(atdma, i);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001706 }
1707
1708 /* set base routines */
1709 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1710 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
Linus Walleij07934482010-03-26 16:50:49 -07001711 atdma->dma_common.device_tx_status = atc_tx_status;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001712 atdma->dma_common.device_issue_pending = atc_issue_pending;
1713 atdma->dma_common.dev = &pdev->dev;
1714
1715 /* set prep routines based on capability */
1716 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1717 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1718
Nicolas Ferred7db8082011-08-05 11:43:44 +00001719 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
Nicolas Ferre808347f2009-07-22 20:04:45 +02001720 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001721 /* controller can do slave DMA: can trigger cyclic transfers */
1722 dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001723 atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
Maxime Ripard4facfe72014-11-17 14:42:06 +01001724 atdma->dma_common.device_config = atc_config;
1725 atdma->dma_common.device_pause = atc_pause;
1726 atdma->dma_common.device_resume = atc_resume;
1727 atdma->dma_common.device_terminate_all = atc_terminate_all;
Ludovic Desroches816070e2015-01-06 17:36:26 +01001728 atdma->dma_common.src_addr_widths = ATC_DMA_BUSWIDTHS;
1729 atdma->dma_common.dst_addr_widths = ATC_DMA_BUSWIDTHS;
1730 atdma->dma_common.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1731 atdma->dma_common.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001732 }
Nicolas Ferre808347f2009-07-22 20:04:45 +02001733
Torsten Fleischer265567f2015-02-23 17:54:11 +01001734 if (dma_has_cap(DMA_SG, atdma->dma_common.cap_mask))
1735 atdma->dma_common.device_prep_dma_sg = atc_prep_dma_sg;
1736
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001737 dma_writel(atdma, EN, AT_DMA_ENABLE);
1738
Torsten Fleischer265567f2015-02-23 17:54:11 +01001739 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s%s), %d channels\n",
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001740 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1741 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
Torsten Fleischer265567f2015-02-23 17:54:11 +01001742 dma_has_cap(DMA_SG, atdma->dma_common.cap_mask) ? "sg-cpy " : "",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001743 plat_dat->nr_channels);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001744
1745 dma_async_device_register(&atdma->dma_common);
1746
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001747 /*
1748 * Do not return an error if the dmac node is not present in order to
1749 * not break the existing way of requesting channel with
1750 * dma_request_channel().
1751 */
1752 if (pdev->dev.of_node) {
1753 err = of_dma_controller_register(pdev->dev.of_node,
1754 at_dma_xlate, atdma);
1755 if (err) {
1756 dev_err(&pdev->dev, "could not register of_dma_controller\n");
1757 goto err_of_dma_controller_register;
1758 }
1759 }
1760
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001761 return 0;
1762
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001763err_of_dma_controller_register:
1764 dma_async_device_unregister(&atdma->dma_common);
1765 dma_pool_destroy(atdma->dma_desc_pool);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001766err_pool_create:
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001767 free_irq(platform_get_irq(pdev, 0), atdma);
1768err_irq:
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001769 clk_disable_unprepare(atdma->clk);
1770err_clk_prepare:
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001771 clk_put(atdma->clk);
1772err_clk:
1773 iounmap(atdma->regs);
1774 atdma->regs = NULL;
1775err_release_r:
1776 release_mem_region(io->start, size);
1777err_kfree:
1778 kfree(atdma);
1779 return err;
1780}
1781
Maxin B. John1d1bbd32013-02-20 02:07:04 +02001782static int at_dma_remove(struct platform_device *pdev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001783{
1784 struct at_dma *atdma = platform_get_drvdata(pdev);
1785 struct dma_chan *chan, *_chan;
1786 struct resource *io;
1787
1788 at_dma_off(atdma);
1789 dma_async_device_unregister(&atdma->dma_common);
1790
1791 dma_pool_destroy(atdma->dma_desc_pool);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001792 free_irq(platform_get_irq(pdev, 0), atdma);
1793
1794 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1795 device_node) {
1796 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1797
1798 /* Disable interrupts */
Nikolaus Vossbda3a472012-01-17 10:28:33 +01001799 atc_disable_chan_irq(atdma, chan->chan_id);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001800
1801 tasklet_kill(&atchan->tasklet);
1802 list_del(&chan->device_node);
1803 }
1804
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001805 clk_disable_unprepare(atdma->clk);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001806 clk_put(atdma->clk);
1807
1808 iounmap(atdma->regs);
1809 atdma->regs = NULL;
1810
1811 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07001812 release_mem_region(io->start, resource_size(io));
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001813
1814 kfree(atdma);
1815
1816 return 0;
1817}
1818
1819static void at_dma_shutdown(struct platform_device *pdev)
1820{
1821 struct at_dma *atdma = platform_get_drvdata(pdev);
1822
1823 at_dma_off(platform_get_drvdata(pdev));
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001824 clk_disable_unprepare(atdma->clk);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001825}
1826
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001827static int at_dma_prepare(struct device *dev)
1828{
1829 struct platform_device *pdev = to_platform_device(dev);
1830 struct at_dma *atdma = platform_get_drvdata(pdev);
1831 struct dma_chan *chan, *_chan;
1832
1833 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1834 device_node) {
1835 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1836 /* wait for transaction completion (except in cyclic case) */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001837 if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001838 return -EAGAIN;
1839 }
1840 return 0;
1841}
1842
1843static void atc_suspend_cyclic(struct at_dma_chan *atchan)
1844{
1845 struct dma_chan *chan = &atchan->chan_common;
1846
1847 /* Channel should be paused by user
1848 * do it anyway even if it is not done already */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001849 if (!atc_chan_is_paused(atchan)) {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001850 dev_warn(chan2dev(chan),
1851 "cyclic channel not paused, should be done by channel user\n");
Maxime Ripard4facfe72014-11-17 14:42:06 +01001852 atc_pause(chan);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001853 }
1854
1855 /* now preserve additional data for cyclic operations */
1856 /* next descriptor address in the cyclic list */
1857 atchan->save_dscr = channel_readl(atchan, DSCR);
1858
1859 vdbg_dump_regs(atchan);
1860}
1861
Dan Williams33f82d12009-09-10 00:06:44 +02001862static int at_dma_suspend_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001863{
Dan Williams33f82d12009-09-10 00:06:44 +02001864 struct platform_device *pdev = to_platform_device(dev);
1865 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001866 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001867
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001868 /* preserve data */
1869 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1870 device_node) {
1871 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1872
Nicolas Ferre3c477482011-07-25 21:09:23 +00001873 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001874 atc_suspend_cyclic(atchan);
1875 atchan->save_cfg = channel_readl(atchan, CFG);
1876 }
1877 atdma->save_imr = dma_readl(atdma, EBCIMR);
1878
1879 /* disable DMA controller */
1880 at_dma_off(atdma);
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001881 clk_disable_unprepare(atdma->clk);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001882 return 0;
1883}
1884
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001885static void atc_resume_cyclic(struct at_dma_chan *atchan)
1886{
1887 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
1888
1889 /* restore channel status for cyclic descriptors list:
1890 * next descriptor in the cyclic list at the time of suspend */
1891 channel_writel(atchan, SADDR, 0);
1892 channel_writel(atchan, DADDR, 0);
1893 channel_writel(atchan, CTRLA, 0);
1894 channel_writel(atchan, CTRLB, 0);
1895 channel_writel(atchan, DSCR, atchan->save_dscr);
1896 dma_writel(atdma, CHER, atchan->mask);
1897
1898 /* channel pause status should be removed by channel user
1899 * We cannot take the initiative to do it here */
1900
1901 vdbg_dump_regs(atchan);
1902}
1903
Dan Williams33f82d12009-09-10 00:06:44 +02001904static int at_dma_resume_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001905{
Dan Williams33f82d12009-09-10 00:06:44 +02001906 struct platform_device *pdev = to_platform_device(dev);
1907 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001908 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001909
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001910 /* bring back DMA controller */
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001911 clk_prepare_enable(atdma->clk);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001912 dma_writel(atdma, EN, AT_DMA_ENABLE);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001913
1914 /* clear any pending interrupt */
1915 while (dma_readl(atdma, EBCISR))
1916 cpu_relax();
1917
1918 /* restore saved data */
1919 dma_writel(atdma, EBCIER, atdma->save_imr);
1920 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1921 device_node) {
1922 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1923
1924 channel_writel(atchan, CFG, atchan->save_cfg);
Nicolas Ferre3c477482011-07-25 21:09:23 +00001925 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001926 atc_resume_cyclic(atchan);
1927 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001928 return 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001929}
1930
Alexey Dobriyan47145212009-12-14 18:00:08 -08001931static const struct dev_pm_ops at_dma_dev_pm_ops = {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00001932 .prepare = at_dma_prepare,
Dan Williams33f82d12009-09-10 00:06:44 +02001933 .suspend_noirq = at_dma_suspend_noirq,
1934 .resume_noirq = at_dma_resume_noirq,
1935};
1936
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001937static struct platform_driver at_dma_driver = {
Maxin B. John1d1bbd32013-02-20 02:07:04 +02001938 .remove = at_dma_remove,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001939 .shutdown = at_dma_shutdown,
Nicolas Ferre67348452011-10-17 14:56:40 +02001940 .id_table = atdma_devtypes,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001941 .driver = {
1942 .name = "at_hdmac",
Dan Williams33f82d12009-09-10 00:06:44 +02001943 .pm = &at_dma_dev_pm_ops,
Nicolas Ferrec5115952011-10-17 14:56:41 +02001944 .of_match_table = of_match_ptr(atmel_dma_dt_ids),
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001945 },
1946};
1947
1948static int __init at_dma_init(void)
1949{
1950 return platform_driver_probe(&at_dma_driver, at_dma_probe);
1951}
Eric Xu93d0bec2011-01-12 15:39:08 +01001952subsys_initcall(at_dma_init);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001953
1954static void __exit at_dma_exit(void)
1955{
1956 platform_driver_unregister(&at_dma_driver);
1957}
1958module_exit(at_dma_exit);
1959
1960MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1961MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1962MODULE_LICENSE("GPL");
1963MODULE_ALIAS("platform:at_hdmac");