blob: 64db0e611cd1a5e1d1c5ddec6c4c23812fc02437 [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);
Maxime Ripard5abecfa2015-05-27 16:01:53 +0200250 channel_writel(atchan, SPIP, ATC_SPIP_HOLE(first->src_hole) |
251 ATC_SPIP_BOUNDARY(first->boundary));
252 channel_writel(atchan, DPIP, ATC_DPIP_HOLE(first->dst_hole) |
253 ATC_DPIP_BOUNDARY(first->boundary));
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200254 dma_writel(atdma, CHER, atchan->mask);
255
256 vdbg_dump_regs(atchan);
257}
258
Elen Songd48de6f2013-05-10 11:01:46 +0800259/*
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100260 * atc_get_desc_by_cookie - get the descriptor of a cookie
261 * @atchan: the DMA channel
262 * @cookie: the cookie to get the descriptor for
Elen Songd48de6f2013-05-10 11:01:46 +0800263 */
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100264static struct at_desc *atc_get_desc_by_cookie(struct at_dma_chan *atchan,
265 dma_cookie_t cookie)
Elen Songd48de6f2013-05-10 11:01:46 +0800266{
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100267 struct at_desc *desc, *_desc;
268
269 list_for_each_entry_safe(desc, _desc, &atchan->queue, desc_node) {
270 if (desc->txd.cookie == cookie)
271 return desc;
272 }
Elen Songd48de6f2013-05-10 11:01:46 +0800273
274 list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100275 if (desc->txd.cookie == cookie)
276 return desc;
Elen Songd48de6f2013-05-10 11:01:46 +0800277 }
278
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100279 return NULL;
Elen Songd48de6f2013-05-10 11:01:46 +0800280}
281
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100282/**
283 * atc_calc_bytes_left - calculates the number of bytes left according to the
284 * value read from CTRLA.
285 *
286 * @current_len: the number of bytes left before reading CTRLA
287 * @ctrla: the value of CTRLA
288 * @desc: the descriptor containing the transfer width
Elen Songd48de6f2013-05-10 11:01:46 +0800289 */
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100290static inline int atc_calc_bytes_left(int current_len, u32 ctrla,
291 struct at_desc *desc)
292{
293 return current_len - ((ctrla & ATC_BTSIZE_MAX) << desc->tx_width);
294}
295
296/**
297 * atc_calc_bytes_left_from_reg - calculates the number of bytes left according
298 * to the current value of CTRLA.
299 *
300 * @current_len: the number of bytes left before reading CTRLA
301 * @atchan: the channel to read CTRLA for
302 * @desc: the descriptor containing the transfer width
303 */
304static inline int atc_calc_bytes_left_from_reg(int current_len,
305 struct at_dma_chan *atchan, struct at_desc *desc)
306{
307 u32 ctrla = channel_readl(atchan, CTRLA);
308
309 return atc_calc_bytes_left(current_len, ctrla, desc);
310}
311
312/**
313 * atc_get_bytes_left - get the number of bytes residue for a cookie
314 * @chan: DMA channel
315 * @cookie: transaction identifier to check status of
316 */
317static int atc_get_bytes_left(struct dma_chan *chan, dma_cookie_t cookie)
Elen Songd48de6f2013-05-10 11:01:46 +0800318{
319 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Elen Songd48de6f2013-05-10 11:01:46 +0800320 struct at_desc *desc_first = atc_first_active(atchan);
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100321 struct at_desc *desc;
322 int ret;
323 u32 ctrla, dscr;
Elen Songd48de6f2013-05-10 11:01:46 +0800324
325 /*
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100326 * If the cookie doesn't match to the currently running transfer then
327 * we can return the total length of the associated DMA transfer,
328 * because it is still queued.
Elen Songd48de6f2013-05-10 11:01:46 +0800329 */
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100330 desc = atc_get_desc_by_cookie(atchan, cookie);
331 if (desc == NULL)
332 return -EINVAL;
333 else if (desc != desc_first)
334 return desc->total_len;
Elen Songd48de6f2013-05-10 11:01:46 +0800335
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100336 /* cookie matches to the currently running transfer */
337 ret = desc_first->total_len;
Alexandre Belloni6758dda2014-08-01 18:51:46 +0200338
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100339 if (desc_first->lli.dscr) {
340 /* hardware linked list transfer */
Alexandre Belloni6758dda2014-08-01 18:51:46 +0200341
Elen Songd48de6f2013-05-10 11:01:46 +0800342 /*
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100343 * Calculate the residue by removing the length of the child
344 * descriptors already transferred from the total length.
345 * To get the current child descriptor we can use the value of
346 * the channel's DSCR register and compare it against the value
347 * of the hardware linked list structure of each child
348 * descriptor.
Elen Songd48de6f2013-05-10 11:01:46 +0800349 */
Elen Songd48de6f2013-05-10 11:01:46 +0800350
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100351 ctrla = channel_readl(atchan, CTRLA);
352 rmb(); /* ensure CTRLA is read before DSCR */
353 dscr = channel_readl(atchan, DSCR);
354
355 /* for the first descriptor we can be more accurate */
356 if (desc_first->lli.dscr == dscr)
357 return atc_calc_bytes_left(ret, ctrla, desc_first);
358
359 ret -= desc_first->len;
360 list_for_each_entry(desc, &desc_first->tx_list, desc_node) {
361 if (desc->lli.dscr == dscr)
362 break;
363
364 ret -= desc->len;
365 }
366
367 /*
368 * For the last descriptor in the chain we can calculate
369 * the remaining bytes using the channel's register.
370 * Note that the transfer width of the first and last
371 * descriptor may differ.
372 */
373 if (!desc->lli.dscr)
374 ret = atc_calc_bytes_left_from_reg(ret, atchan, desc);
375 } else {
376 /* single transfer */
377 ret = atc_calc_bytes_left_from_reg(ret, atchan, desc_first);
378 }
379
Elen Songd48de6f2013-05-10 11:01:46 +0800380 return ret;
381}
382
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200383/**
384 * atc_chain_complete - finish work for one transaction chain
385 * @atchan: channel we work on
386 * @desc: descriptor at the head of the chain we want do complete
387 *
388 * Called with atchan->lock held and bh disabled */
389static void
390atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
391{
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200392 struct dma_async_tx_descriptor *txd = &desc->txd;
Maxime Ripard4d112422015-08-24 11:21:15 +0200393 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200394
395 dev_vdbg(chan2dev(&atchan->chan_common),
396 "descriptor %u complete\n", txd->cookie);
397
Vinod Kould4116052012-05-11 11:48:21 +0530398 /* mark the descriptor as complete for non cyclic cases only */
399 if (!atc_chan_is_cyclic(atchan))
400 dma_cookie_complete(txd);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200401
Maxime Ripard4d112422015-08-24 11:21:15 +0200402 /* If the transfer was a memset, free our temporary buffer */
403 if (desc->memset) {
404 dma_pool_free(atdma->memset_pool, desc->memset_vaddr,
405 desc->memset_paddr);
406 desc->memset = false;
407 }
408
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200409 /* move children to free_list */
Dan Williams285a3c72009-09-08 17:53:03 -0700410 list_splice_init(&desc->tx_list, &atchan->free_list);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200411 /* move myself to free_list */
412 list_move(&desc->desc_node, &atchan->free_list);
413
Dan Williamsd38a8c62013-10-18 19:35:23 +0200414 dma_descriptor_unmap(txd);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200415 /* for cyclic transfers,
416 * no need to replay callback function while stopping */
Nicolas Ferre3c477482011-07-25 21:09:23 +0000417 if (!atc_chan_is_cyclic(atchan)) {
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200418 dma_async_tx_callback callback = txd->callback;
419 void *param = txd->callback_param;
420
421 /*
422 * The API requires that no submissions are done from a
423 * callback, so we don't need to drop the lock here
424 */
425 if (callback)
426 callback(param);
427 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200428
429 dma_run_dependencies(txd);
430}
431
432/**
433 * atc_complete_all - finish work for all transactions
434 * @atchan: channel to complete transactions for
435 *
436 * Eventually submit queued descriptors if any
437 *
438 * Assume channel is idle while calling this function
439 * Called with atchan->lock held and bh disabled
440 */
441static void atc_complete_all(struct at_dma_chan *atchan)
442{
443 struct at_desc *desc, *_desc;
444 LIST_HEAD(list);
445
446 dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
447
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200448 /*
449 * Submit queued descriptors ASAP, i.e. before we go through
450 * the completed ones.
451 */
452 if (!list_empty(&atchan->queue))
453 atc_dostart(atchan, atc_first_queued(atchan));
454 /* empty active_list now it is completed */
455 list_splice_init(&atchan->active_list, &list);
456 /* empty queue list by moving descriptors (if any) to active_list */
457 list_splice_init(&atchan->queue, &atchan->active_list);
458
459 list_for_each_entry_safe(desc, _desc, &list, desc_node)
460 atc_chain_complete(atchan, desc);
461}
462
463/**
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200464 * atc_advance_work - at the end of a transaction, move forward
465 * @atchan: channel where the transaction ended
466 *
467 * Called with atchan->lock held and bh disabled
468 */
469static void atc_advance_work(struct at_dma_chan *atchan)
470{
471 dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
472
Ludovic Desrochesd202f052013-04-18 09:52:59 +0200473 if (atc_chan_is_enabled(atchan))
474 return;
475
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200476 if (list_empty(&atchan->active_list) ||
477 list_is_singular(&atchan->active_list)) {
478 atc_complete_all(atchan);
479 } else {
480 atc_chain_complete(atchan, atc_first_active(atchan));
481 /* advance work */
482 atc_dostart(atchan, atc_first_active(atchan));
483 }
484}
485
486
487/**
488 * atc_handle_error - handle errors reported by DMA controller
489 * @atchan: channel where error occurs
490 *
491 * Called with atchan->lock held and bh disabled
492 */
493static void atc_handle_error(struct at_dma_chan *atchan)
494{
495 struct at_desc *bad_desc;
496 struct at_desc *child;
497
498 /*
499 * The descriptor currently at the head of the active list is
500 * broked. Since we don't have any way to report errors, we'll
501 * just have to scream loudly and try to carry on.
502 */
503 bad_desc = atc_first_active(atchan);
504 list_del_init(&bad_desc->desc_node);
505
506 /* As we are stopped, take advantage to push queued descriptors
507 * in active_list */
508 list_splice_init(&atchan->queue, atchan->active_list.prev);
509
510 /* Try to restart the controller */
511 if (!list_empty(&atchan->active_list))
512 atc_dostart(atchan, atc_first_active(atchan));
513
514 /*
515 * KERN_CRITICAL may seem harsh, but since this only happens
516 * when someone submits a bad physical address in a
517 * descriptor, we should consider ourselves lucky that the
518 * controller flagged an error instead of scribbling over
519 * random memory locations.
520 */
521 dev_crit(chan2dev(&atchan->chan_common),
522 "Bad descriptor submitted for DMA!\n");
523 dev_crit(chan2dev(&atchan->chan_common),
524 " cookie: %d\n", bad_desc->txd.cookie);
525 atc_dump_lli(atchan, &bad_desc->lli);
Dan Williams285a3c72009-09-08 17:53:03 -0700526 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200527 atc_dump_lli(atchan, &child->lli);
528
529 /* Pretend the descriptor completed successfully */
530 atc_chain_complete(atchan, bad_desc);
531}
532
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200533/**
534 * atc_handle_cyclic - at the end of a period, run callback function
535 * @atchan: channel used for cyclic operations
536 *
537 * Called with atchan->lock held and bh disabled
538 */
539static void atc_handle_cyclic(struct at_dma_chan *atchan)
540{
541 struct at_desc *first = atc_first_active(atchan);
542 struct dma_async_tx_descriptor *txd = &first->txd;
543 dma_async_tx_callback callback = txd->callback;
544 void *param = txd->callback_param;
545
546 dev_vdbg(chan2dev(&atchan->chan_common),
547 "new cyclic period llp 0x%08x\n",
548 channel_readl(atchan, DSCR));
549
550 if (callback)
551 callback(param);
552}
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200553
554/*-- IRQ & Tasklet ---------------------------------------------------*/
555
556static void atc_tasklet(unsigned long data)
557{
558 struct at_dma_chan *atchan = (struct at_dma_chan *)data;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000559 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200560
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000561 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200562 if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200563 atc_handle_error(atchan);
Nicolas Ferre3c477482011-07-25 21:09:23 +0000564 else if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200565 atc_handle_cyclic(atchan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200566 else
567 atc_advance_work(atchan);
568
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000569 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200570}
571
572static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
573{
574 struct at_dma *atdma = (struct at_dma *)dev_id;
575 struct at_dma_chan *atchan;
576 int i;
577 u32 status, pending, imr;
578 int ret = IRQ_NONE;
579
580 do {
581 imr = dma_readl(atdma, EBCIMR);
582 status = dma_readl(atdma, EBCISR);
583 pending = status & imr;
584
585 if (!pending)
586 break;
587
588 dev_vdbg(atdma->dma_common.dev,
589 "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
590 status, imr, pending);
591
592 for (i = 0; i < atdma->dma_common.chancnt; i++) {
593 atchan = &atdma->chan[i];
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200594 if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200595 if (pending & AT_DMA_ERR(i)) {
596 /* Disable channel on AHB error */
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +0200597 dma_writel(atdma, CHDR,
598 AT_DMA_RES(i) | atchan->mask);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200599 /* Give information to tasklet */
Nicolas Ferre53830cc2011-04-30 16:57:46 +0200600 set_bit(ATC_IS_ERROR, &atchan->status);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200601 }
602 tasklet_schedule(&atchan->tasklet);
603 ret = IRQ_HANDLED;
604 }
605 }
606
607 } while (pending);
608
609 return ret;
610}
611
612
613/*-- DMA Engine API --------------------------------------------------*/
614
615/**
616 * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
617 * @desc: descriptor at the head of the transaction chain
618 *
619 * Queue chain if DMA engine is working already
620 *
621 * Cookie increment and adding to active_list or queue must be atomic
622 */
623static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
624{
625 struct at_desc *desc = txd_to_at_desc(tx);
626 struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
627 dma_cookie_t cookie;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000628 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200629
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000630 spin_lock_irqsave(&atchan->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000631 cookie = dma_cookie_assign(tx);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200632
633 if (list_empty(&atchan->active_list)) {
634 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
635 desc->txd.cookie);
636 atc_dostart(atchan, desc);
637 list_add_tail(&desc->desc_node, &atchan->active_list);
638 } else {
639 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
640 desc->txd.cookie);
641 list_add_tail(&desc->desc_node, &atchan->queue);
642 }
643
Nicolas Ferred8cb04b2011-07-27 12:21:28 +0000644 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200645
646 return cookie;
647}
648
649/**
Maxime Ripard5abecfa2015-05-27 16:01:53 +0200650 * atc_prep_dma_interleaved - prepare memory to memory interleaved operation
651 * @chan: the channel to prepare operation on
652 * @xt: Interleaved transfer template
653 * @flags: tx descriptor status flags
654 */
655static struct dma_async_tx_descriptor *
656atc_prep_dma_interleaved(struct dma_chan *chan,
657 struct dma_interleaved_template *xt,
658 unsigned long flags)
659{
660 struct at_dma_chan *atchan = to_at_dma_chan(chan);
661 struct data_chunk *first = xt->sgl;
662 struct at_desc *desc = NULL;
663 size_t xfer_count;
664 unsigned int dwidth;
665 u32 ctrla;
666 u32 ctrlb;
667 size_t len = 0;
668 int i;
669
Maninder Singh44833202015-06-26 16:04:48 +0530670 if (unlikely(!xt || xt->numf != 1 || !xt->frame_size))
671 return NULL;
672
Maxime Ripard5abecfa2015-05-27 16:01:53 +0200673 dev_info(chan2dev(chan),
674 "%s: src=0x%08x, dest=0x%08x, numf=%d, frame_size=%d, flags=0x%lx\n",
675 __func__, xt->src_start, xt->dst_start, xt->numf,
676 xt->frame_size, flags);
677
Maxime Ripard5abecfa2015-05-27 16:01:53 +0200678 /*
679 * The controller can only "skip" X bytes every Y bytes, so we
680 * need to make sure we are given a template that fit that
681 * description, ie a template with chunks that always have the
682 * same size, with the same ICGs.
683 */
684 for (i = 0; i < xt->frame_size; i++) {
685 struct data_chunk *chunk = xt->sgl + i;
686
687 if ((chunk->size != xt->sgl->size) ||
688 (dmaengine_get_dst_icg(xt, chunk) != dmaengine_get_dst_icg(xt, first)) ||
689 (dmaengine_get_src_icg(xt, chunk) != dmaengine_get_src_icg(xt, first))) {
690 dev_err(chan2dev(chan),
691 "%s: the controller can transfer only identical chunks\n",
692 __func__);
693 return NULL;
694 }
695
696 len += chunk->size;
697 }
698
699 dwidth = atc_get_xfer_width(xt->src_start,
700 xt->dst_start, len);
701
702 xfer_count = len >> dwidth;
703 if (xfer_count > ATC_BTSIZE_MAX) {
704 dev_err(chan2dev(chan), "%s: buffer is too big\n", __func__);
705 return NULL;
706 }
707
708 ctrla = ATC_SRC_WIDTH(dwidth) |
709 ATC_DST_WIDTH(dwidth);
710
711 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
712 | ATC_SRC_ADDR_MODE_INCR
713 | ATC_DST_ADDR_MODE_INCR
714 | ATC_SRC_PIP
715 | ATC_DST_PIP
716 | ATC_FC_MEM2MEM;
717
718 /* create the transfer */
719 desc = atc_desc_get(atchan);
720 if (!desc) {
721 dev_err(chan2dev(chan),
722 "%s: couldn't allocate our descriptor\n", __func__);
723 return NULL;
724 }
725
726 desc->lli.saddr = xt->src_start;
727 desc->lli.daddr = xt->dst_start;
728 desc->lli.ctrla = ctrla | xfer_count;
729 desc->lli.ctrlb = ctrlb;
730
731 desc->boundary = first->size >> dwidth;
732 desc->dst_hole = (dmaengine_get_dst_icg(xt, first) >> dwidth) + 1;
733 desc->src_hole = (dmaengine_get_src_icg(xt, first) >> dwidth) + 1;
734
735 desc->txd.cookie = -EBUSY;
736 desc->total_len = desc->len = len;
737 desc->tx_width = dwidth;
738
739 /* set end-of-link to the last link descriptor of list*/
740 set_desc_eol(desc);
741
742 desc->txd.flags = flags; /* client is in control of this ack */
743
744 return &desc->txd;
745}
746
747/**
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200748 * atc_prep_dma_memcpy - prepare a memcpy operation
749 * @chan: the channel to prepare operation on
750 * @dest: operation virtual destination address
751 * @src: operation virtual source address
752 * @len: operation length
753 * @flags: tx descriptor status flags
754 */
755static struct dma_async_tx_descriptor *
756atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
757 size_t len, unsigned long flags)
758{
759 struct at_dma_chan *atchan = to_at_dma_chan(chan);
760 struct at_desc *desc = NULL;
761 struct at_desc *first = NULL;
762 struct at_desc *prev = NULL;
763 size_t xfer_count;
764 size_t offset;
765 unsigned int src_width;
766 unsigned int dst_width;
767 u32 ctrla;
768 u32 ctrlb;
769
770 dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
771 dest, src, len, flags);
772
773 if (unlikely(!len)) {
774 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
775 return NULL;
776 }
777
Nicolas Ferre9b3aa582011-04-30 16:57:45 +0200778 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200779 | ATC_SRC_ADDR_MODE_INCR
780 | ATC_DST_ADDR_MODE_INCR
781 | ATC_FC_MEM2MEM;
782
783 /*
784 * We can be a lot more clever here, but this should take care
785 * of the most common optimization.
786 */
Torsten Fleischer265567f2015-02-23 17:54:11 +0100787 src_width = dst_width = atc_get_xfer_width(src, dest, len);
788
789 ctrla = ATC_SRC_WIDTH(src_width) |
790 ATC_DST_WIDTH(dst_width);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200791
792 for (offset = 0; offset < len; offset += xfer_count << src_width) {
793 xfer_count = min_t(size_t, (len - offset) >> src_width,
794 ATC_BTSIZE_MAX);
795
796 desc = atc_desc_get(atchan);
797 if (!desc)
798 goto err_desc_get;
799
800 desc->lli.saddr = src + offset;
801 desc->lli.daddr = dest + offset;
802 desc->lli.ctrla = ctrla | xfer_count;
803 desc->lli.ctrlb = ctrlb;
804
805 desc->txd.cookie = 0;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100806 desc->len = xfer_count << src_width;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200807
Nicolas Ferree257e152011-05-06 19:56:53 +0200808 atc_desc_chain(&first, &prev, desc);
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200809 }
810
811 /* First descriptor of the chain embedds additional information */
812 first->txd.cookie = -EBUSY;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100813 first->total_len = len;
814
815 /* set transfer width for the calculation of the residue */
Elen Songd088c332013-05-10 11:00:50 +0800816 first->tx_width = src_width;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100817 prev->tx_width = src_width;
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200818
819 /* set end-of-link to the last link descriptor of list*/
820 set_desc_eol(desc);
821
Nicolas Ferre568f7f02011-01-12 15:39:09 +0100822 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferredc78baa2009-07-03 19:24:33 +0200823
824 return &first->txd;
825
826err_desc_get:
827 atc_desc_put(atchan, first);
828 return NULL;
829}
830
Maxime Ripard4d112422015-08-24 11:21:15 +0200831/**
832 * atc_prep_dma_memset - prepare a memcpy operation
833 * @chan: the channel to prepare operation on
834 * @dest: operation virtual destination address
835 * @value: value to set memory buffer to
836 * @len: operation length
837 * @flags: tx descriptor status flags
838 */
839static struct dma_async_tx_descriptor *
840atc_prep_dma_memset(struct dma_chan *chan, dma_addr_t dest, int value,
841 size_t len, unsigned long flags)
842{
843 struct at_dma_chan *atchan = to_at_dma_chan(chan);
844 struct at_dma *atdma = to_at_dma(chan->device);
845 struct at_desc *desc = NULL;
846 size_t xfer_count;
847 u32 ctrla;
848 u32 ctrlb;
849
850 dev_vdbg(chan2dev(chan), "%s: d0x%x v0x%x l0x%zx f0x%lx\n", __func__,
851 dest, value, len, flags);
852
853 if (unlikely(!len)) {
854 dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
855 return NULL;
856 }
857
858 if (!is_dma_fill_aligned(chan->device, dest, 0, len)) {
859 dev_dbg(chan2dev(chan), "%s: buffer is not aligned\n",
860 __func__);
861 return NULL;
862 }
863
864 xfer_count = len >> 2;
865 if (xfer_count > ATC_BTSIZE_MAX) {
866 dev_err(chan2dev(chan), "%s: buffer is too big\n",
867 __func__);
868 return NULL;
869 }
870
871 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
872 | ATC_SRC_ADDR_MODE_FIXED
873 | ATC_DST_ADDR_MODE_INCR
874 | ATC_FC_MEM2MEM;
875
876 ctrla = ATC_SRC_WIDTH(2) |
877 ATC_DST_WIDTH(2);
878
879 desc = atc_desc_get(atchan);
880 if (!desc) {
881 dev_err(chan2dev(chan), "%s: can't get a descriptor\n",
882 __func__);
883 return NULL;
884 }
885
886 desc->memset_vaddr = dma_pool_alloc(atdma->memset_pool, GFP_ATOMIC,
887 &desc->memset_paddr);
888 if (!desc->memset_vaddr) {
889 dev_err(chan2dev(chan), "%s: couldn't allocate buffer\n",
890 __func__);
891 goto err_put_desc;
892 }
893
894 *desc->memset_vaddr = value;
895 desc->memset = true;
896
897 desc->lli.saddr = desc->memset_paddr;
898 desc->lli.daddr = dest;
899 desc->lli.ctrla = ctrla | xfer_count;
900 desc->lli.ctrlb = ctrlb;
901
902 desc->txd.cookie = -EBUSY;
903 desc->len = len;
904 desc->total_len = len;
905
906 /* set end-of-link on the descriptor */
907 set_desc_eol(desc);
908
909 desc->txd.flags = flags;
910
911 return &desc->txd;
912
913err_put_desc:
914 atc_desc_put(atchan, desc);
915 return NULL;
916}
917
Nicolas Ferre808347f2009-07-22 20:04:45 +0200918
919/**
920 * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
921 * @chan: DMA channel
922 * @sgl: scatterlist to transfer to/from
923 * @sg_len: number of entries in @scatterlist
924 * @direction: DMA direction
925 * @flags: tx descriptor status flags
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500926 * @context: transaction context (ignored)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200927 */
928static struct dma_async_tx_descriptor *
929atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530930 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500931 unsigned long flags, void *context)
Nicolas Ferre808347f2009-07-22 20:04:45 +0200932{
933 struct at_dma_chan *atchan = to_at_dma_chan(chan);
934 struct at_dma_slave *atslave = chan->private;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100935 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200936 struct at_desc *first = NULL;
937 struct at_desc *prev = NULL;
938 u32 ctrla;
939 u32 ctrlb;
940 dma_addr_t reg;
941 unsigned int reg_width;
942 unsigned int mem_width;
943 unsigned int i;
944 struct scatterlist *sg;
945 size_t total_len = 0;
946
Nicolas Ferrecc52a102011-04-30 16:57:47 +0200947 dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
948 sg_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530949 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre808347f2009-07-22 20:04:45 +0200950 flags);
951
952 if (unlikely(!atslave || !sg_len)) {
Nicolas Ferrec618a9b2012-09-11 17:21:44 +0200953 dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n");
Nicolas Ferre808347f2009-07-22 20:04:45 +0200954 return NULL;
955 }
956
Nicolas Ferre1dd1ea82012-05-10 12:17:41 +0200957 ctrla = ATC_SCSIZE(sconfig->src_maxburst)
958 | ATC_DCSIZE(sconfig->dst_maxburst);
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200959 ctrlb = ATC_IEN;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200960
961 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530962 case DMA_MEM_TO_DEV:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100963 reg_width = convert_buswidth(sconfig->dst_addr_width);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200964 ctrla |= ATC_DST_WIDTH(reg_width);
965 ctrlb |= ATC_DST_ADDR_MODE_FIXED
966 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +0200967 | ATC_FC_MEM2PER
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +0000968 | ATC_SIF(atchan->mem_if) | ATC_DIF(atchan->per_if);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +0100969 reg = sconfig->dst_addr;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200970 for_each_sg(sgl, sg, sg_len, i) {
971 struct at_desc *desc;
972 u32 len;
973 u32 mem;
974
975 desc = atc_desc_get(atchan);
976 if (!desc)
977 goto err_desc_get;
978
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +0100979 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200980 len = sg_dma_len(sg);
Nicolas Ferrec4567972012-09-11 17:21:45 +0200981 if (unlikely(!len)) {
982 dev_dbg(chan2dev(chan),
983 "prep_slave_sg: sg(%d) data length is zero\n", i);
984 goto err;
985 }
Nicolas Ferre808347f2009-07-22 20:04:45 +0200986 mem_width = 2;
987 if (unlikely(mem & 3 || len & 3))
988 mem_width = 0;
989
990 desc->lli.saddr = mem;
991 desc->lli.daddr = reg;
992 desc->lli.ctrla = ctrla
993 | ATC_SRC_WIDTH(mem_width)
994 | len >> mem_width;
995 desc->lli.ctrlb = ctrlb;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +0100996 desc->len = len;
Nicolas Ferre808347f2009-07-22 20:04:45 +0200997
Nicolas Ferree257e152011-05-06 19:56:53 +0200998 atc_desc_chain(&first, &prev, desc);
Nicolas Ferre808347f2009-07-22 20:04:45 +0200999 total_len += len;
1000 }
1001 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301002 case DMA_DEV_TO_MEM:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001003 reg_width = convert_buswidth(sconfig->src_addr_width);
Nicolas Ferre808347f2009-07-22 20:04:45 +02001004 ctrla |= ATC_SRC_WIDTH(reg_width);
1005 ctrlb |= ATC_DST_ADDR_MODE_INCR
1006 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +02001007 | ATC_FC_PER2MEM
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001008 | ATC_SIF(atchan->per_if) | ATC_DIF(atchan->mem_if);
Nicolas Ferre808347f2009-07-22 20:04:45 +02001009
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001010 reg = sconfig->src_addr;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001011 for_each_sg(sgl, sg, sg_len, i) {
1012 struct at_desc *desc;
1013 u32 len;
1014 u32 mem;
1015
1016 desc = atc_desc_get(atchan);
1017 if (!desc)
1018 goto err_desc_get;
1019
Nicolas Ferre0f70e8c2010-12-15 18:50:16 +01001020 mem = sg_dma_address(sg);
Nicolas Ferre808347f2009-07-22 20:04:45 +02001021 len = sg_dma_len(sg);
Nicolas Ferrec4567972012-09-11 17:21:45 +02001022 if (unlikely(!len)) {
1023 dev_dbg(chan2dev(chan),
1024 "prep_slave_sg: sg(%d) data length is zero\n", i);
1025 goto err;
1026 }
Nicolas Ferre808347f2009-07-22 20:04:45 +02001027 mem_width = 2;
1028 if (unlikely(mem & 3 || len & 3))
1029 mem_width = 0;
1030
1031 desc->lli.saddr = reg;
1032 desc->lli.daddr = mem;
1033 desc->lli.ctrla = ctrla
1034 | ATC_DST_WIDTH(mem_width)
Nicolas Ferre59a609d2010-12-13 13:48:41 +01001035 | len >> reg_width;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001036 desc->lli.ctrlb = ctrlb;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +01001037 desc->len = len;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001038
Nicolas Ferree257e152011-05-06 19:56:53 +02001039 atc_desc_chain(&first, &prev, desc);
Nicolas Ferre808347f2009-07-22 20:04:45 +02001040 total_len += len;
1041 }
1042 break;
1043 default:
1044 return NULL;
1045 }
1046
1047 /* set end-of-link to the last link descriptor of list*/
1048 set_desc_eol(prev);
1049
1050 /* First descriptor of the chain embedds additional information */
1051 first->txd.cookie = -EBUSY;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +01001052 first->total_len = total_len;
1053
1054 /* set transfer width for the calculation of the residue */
Elen Songd088c332013-05-10 11:00:50 +08001055 first->tx_width = reg_width;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +01001056 prev->tx_width = reg_width;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001057
Nicolas Ferre568f7f02011-01-12 15:39:09 +01001058 /* first link descriptor of list is responsible of flags */
1059 first->txd.flags = flags; /* client is in control of this ack */
Nicolas Ferre808347f2009-07-22 20:04:45 +02001060
1061 return &first->txd;
1062
1063err_desc_get:
1064 dev_err(chan2dev(chan), "not enough descriptors available\n");
Nicolas Ferrec4567972012-09-11 17:21:45 +02001065err:
Nicolas Ferre808347f2009-07-22 20:04:45 +02001066 atc_desc_put(atchan, first);
1067 return NULL;
1068}
1069
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001070/**
Torsten Fleischer265567f2015-02-23 17:54:11 +01001071 * atc_prep_dma_sg - prepare memory to memory scather-gather operation
1072 * @chan: the channel to prepare operation on
1073 * @dst_sg: destination scatterlist
1074 * @dst_nents: number of destination scatterlist entries
1075 * @src_sg: source scatterlist
1076 * @src_nents: number of source scatterlist entries
1077 * @flags: tx descriptor status flags
1078 */
1079static struct dma_async_tx_descriptor *
1080atc_prep_dma_sg(struct dma_chan *chan,
1081 struct scatterlist *dst_sg, unsigned int dst_nents,
1082 struct scatterlist *src_sg, unsigned int src_nents,
1083 unsigned long flags)
1084{
1085 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1086 struct at_desc *desc = NULL;
1087 struct at_desc *first = NULL;
1088 struct at_desc *prev = NULL;
1089 unsigned int src_width;
1090 unsigned int dst_width;
1091 size_t xfer_count;
1092 u32 ctrla;
1093 u32 ctrlb;
1094 size_t dst_len = 0, src_len = 0;
1095 dma_addr_t dst = 0, src = 0;
1096 size_t len = 0, total_len = 0;
1097
1098 if (unlikely(dst_nents == 0 || src_nents == 0))
1099 return NULL;
1100
1101 if (unlikely(dst_sg == NULL || src_sg == NULL))
1102 return NULL;
1103
1104 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
1105 | ATC_SRC_ADDR_MODE_INCR
1106 | ATC_DST_ADDR_MODE_INCR
1107 | ATC_FC_MEM2MEM;
1108
1109 /*
1110 * loop until there is either no more source or no more destination
1111 * scatterlist entry
1112 */
1113 while (true) {
1114
1115 /* prepare the next transfer */
1116 if (dst_len == 0) {
1117
1118 /* no more destination scatterlist entries */
1119 if (!dst_sg || !dst_nents)
1120 break;
1121
1122 dst = sg_dma_address(dst_sg);
1123 dst_len = sg_dma_len(dst_sg);
1124
1125 dst_sg = sg_next(dst_sg);
1126 dst_nents--;
1127 }
1128
1129 if (src_len == 0) {
1130
1131 /* no more source scatterlist entries */
1132 if (!src_sg || !src_nents)
1133 break;
1134
1135 src = sg_dma_address(src_sg);
1136 src_len = sg_dma_len(src_sg);
1137
1138 src_sg = sg_next(src_sg);
1139 src_nents--;
1140 }
1141
1142 len = min_t(size_t, src_len, dst_len);
1143 if (len == 0)
1144 continue;
1145
1146 /* take care for the alignment */
1147 src_width = dst_width = atc_get_xfer_width(src, dst, len);
1148
1149 ctrla = ATC_SRC_WIDTH(src_width) |
1150 ATC_DST_WIDTH(dst_width);
1151
1152 /*
1153 * The number of transfers to set up refer to the source width
1154 * that depends on the alignment.
1155 */
1156 xfer_count = len >> src_width;
1157 if (xfer_count > ATC_BTSIZE_MAX) {
1158 xfer_count = ATC_BTSIZE_MAX;
1159 len = ATC_BTSIZE_MAX << src_width;
1160 }
1161
1162 /* create the transfer */
1163 desc = atc_desc_get(atchan);
1164 if (!desc)
1165 goto err_desc_get;
1166
1167 desc->lli.saddr = src;
1168 desc->lli.daddr = dst;
1169 desc->lli.ctrla = ctrla | xfer_count;
1170 desc->lli.ctrlb = ctrlb;
1171
1172 desc->txd.cookie = 0;
1173 desc->len = len;
1174
1175 /*
1176 * Although we only need the transfer width for the first and
1177 * the last descriptor, its easier to set it to all descriptors.
1178 */
1179 desc->tx_width = src_width;
1180
1181 atc_desc_chain(&first, &prev, desc);
1182
1183 /* update the lengths and addresses for the next loop cycle */
1184 dst_len -= len;
1185 src_len -= len;
1186 dst += len;
1187 src += len;
1188
1189 total_len += len;
1190 }
1191
1192 /* First descriptor of the chain embedds additional information */
1193 first->txd.cookie = -EBUSY;
1194 first->total_len = total_len;
1195
1196 /* set end-of-link to the last link descriptor of list*/
1197 set_desc_eol(desc);
1198
1199 first->txd.flags = flags; /* client is in control of this ack */
1200
1201 return &first->txd;
1202
1203err_desc_get:
1204 atc_desc_put(atchan, first);
1205 return NULL;
1206}
1207
1208/**
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001209 * atc_dma_cyclic_check_values
1210 * Check for too big/unaligned periods and unaligned DMA buffer
1211 */
1212static int
1213atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
Andy Shevchenko0e7264c2013-01-10 10:52:57 +02001214 size_t period_len)
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001215{
1216 if (period_len > (ATC_BTSIZE_MAX << reg_width))
1217 goto err_out;
1218 if (unlikely(period_len & ((1 << reg_width) - 1)))
1219 goto err_out;
1220 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1221 goto err_out;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001222
1223 return 0;
1224
1225err_out:
1226 return -EINVAL;
1227}
1228
1229/**
Masanari Iidad73111c2012-08-04 23:37:53 +09001230 * atc_dma_cyclic_fill_desc - Fill one period descriptor
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001231 */
1232static int
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001233atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001234 unsigned int period_index, dma_addr_t buf_addr,
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001235 unsigned int reg_width, size_t period_len,
1236 enum dma_transfer_direction direction)
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001237{
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001238 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001239 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
1240 u32 ctrla;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001241
1242 /* prepare common CRTLA value */
Nicolas Ferre1dd1ea82012-05-10 12:17:41 +02001243 ctrla = ATC_SCSIZE(sconfig->src_maxburst)
1244 | ATC_DCSIZE(sconfig->dst_maxburst)
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001245 | ATC_DST_WIDTH(reg_width)
1246 | ATC_SRC_WIDTH(reg_width)
1247 | period_len >> reg_width;
1248
1249 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +05301250 case DMA_MEM_TO_DEV:
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001251 desc->lli.saddr = buf_addr + (period_len * period_index);
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001252 desc->lli.daddr = sconfig->dst_addr;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001253 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +02001254 desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001255 | ATC_SRC_ADDR_MODE_INCR
Nicolas Ferreae14d4b2011-04-30 16:57:49 +02001256 | ATC_FC_MEM2PER
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001257 | ATC_SIF(atchan->mem_if)
1258 | ATC_DIF(atchan->per_if);
Torsten Fleischerbdf6c792015-02-23 17:54:10 +01001259 desc->len = period_len;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001260 break;
1261
Vinod Kouldb8196d2011-10-13 22:34:23 +05301262 case DMA_DEV_TO_MEM:
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001263 desc->lli.saddr = sconfig->src_addr;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001264 desc->lli.daddr = buf_addr + (period_len * period_index);
1265 desc->lli.ctrla = ctrla;
Nicolas Ferreae14d4b2011-04-30 16:57:49 +02001266 desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001267 | ATC_SRC_ADDR_MODE_FIXED
Nicolas Ferreae14d4b2011-04-30 16:57:49 +02001268 | ATC_FC_PER2MEM
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001269 | ATC_SIF(atchan->per_if)
1270 | ATC_DIF(atchan->mem_if);
Torsten Fleischerbdf6c792015-02-23 17:54:10 +01001271 desc->len = period_len;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001272 break;
1273
1274 default:
1275 return -EINVAL;
1276 }
1277
1278 return 0;
1279}
1280
1281/**
1282 * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
1283 * @chan: the DMA channel to prepare
1284 * @buf_addr: physical DMA address where the buffer starts
1285 * @buf_len: total number of bytes for the entire buffer
1286 * @period_len: number of bytes for each period
1287 * @direction: transfer direction, to or from device
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +03001288 * @flags: tx descriptor status flags
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001289 */
1290static struct dma_async_tx_descriptor *
1291atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05001292 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +02001293 unsigned long flags)
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001294{
1295 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1296 struct at_dma_slave *atslave = chan->private;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001297 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001298 struct at_desc *first = NULL;
1299 struct at_desc *prev = NULL;
1300 unsigned long was_cyclic;
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001301 unsigned int reg_width;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001302 unsigned int periods = buf_len / period_len;
1303 unsigned int i;
1304
1305 dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
Vinod Kouldb8196d2011-10-13 22:34:23 +05301306 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001307 buf_addr,
1308 periods, buf_len, period_len);
1309
1310 if (unlikely(!atslave || !buf_len || !period_len)) {
1311 dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
1312 return NULL;
1313 }
1314
1315 was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
1316 if (was_cyclic) {
1317 dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
1318 return NULL;
1319 }
1320
Andy Shevchenko0e7264c2013-01-10 10:52:57 +02001321 if (unlikely(!is_slave_direction(direction)))
1322 goto err_out;
1323
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001324 if (sconfig->direction == DMA_MEM_TO_DEV)
1325 reg_width = convert_buswidth(sconfig->dst_addr_width);
1326 else
1327 reg_width = convert_buswidth(sconfig->src_addr_width);
1328
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001329 /* Check for too big/unaligned periods and unaligned DMA buffer */
Andy Shevchenko0e7264c2013-01-10 10:52:57 +02001330 if (atc_dma_cyclic_check_values(reg_width, buf_addr, period_len))
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001331 goto err_out;
1332
1333 /* build cyclic linked list */
1334 for (i = 0; i < periods; i++) {
1335 struct at_desc *desc;
1336
1337 desc = atc_desc_get(atchan);
1338 if (!desc)
1339 goto err_desc_get;
1340
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001341 if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
1342 reg_width, period_len, direction))
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001343 goto err_desc_get;
1344
1345 atc_desc_chain(&first, &prev, desc);
1346 }
1347
1348 /* lets make a cyclic list */
1349 prev->lli.dscr = first->txd.phys;
1350
1351 /* First descriptor of the chain embedds additional information */
1352 first->txd.cookie = -EBUSY;
Torsten Fleischerbdf6c792015-02-23 17:54:10 +01001353 first->total_len = buf_len;
Elen Songd088c332013-05-10 11:00:50 +08001354 first->tx_width = reg_width;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001355
1356 return &first->txd;
1357
1358err_desc_get:
1359 dev_err(chan2dev(chan), "not enough descriptors available\n");
1360 atc_desc_put(atchan, first);
1361err_out:
1362 clear_bit(ATC_IS_CYCLIC, &atchan->status);
1363 return NULL;
1364}
1365
Maxime Ripard4facfe72014-11-17 14:42:06 +01001366static int atc_config(struct dma_chan *chan,
1367 struct dma_slave_config *sconfig)
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001368{
1369 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1370
Maxime Ripard4facfe72014-11-17 14:42:06 +01001371 dev_vdbg(chan2dev(chan), "%s\n", __func__);
1372
Nicolas Ferrebeeaa102012-03-14 12:41:43 +01001373 /* Check if it is chan is configured for slave transfers */
1374 if (!chan->private)
1375 return -EINVAL;
1376
1377 memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
1378
1379 convert_burst(&atchan->dma_sconfig.src_maxburst);
1380 convert_burst(&atchan->dma_sconfig.dst_maxburst);
1381
1382 return 0;
1383}
1384
Maxime Ripard4facfe72014-11-17 14:42:06 +01001385static int atc_pause(struct dma_chan *chan)
Nicolas Ferre808347f2009-07-22 20:04:45 +02001386{
1387 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1388 struct at_dma *atdma = to_at_dma(chan->device);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001389 int chan_id = atchan->chan_common.chan_id;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001390 unsigned long flags;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001391
Nicolas Ferre808347f2009-07-22 20:04:45 +02001392 LIST_HEAD(list);
1393
Maxime Ripard4facfe72014-11-17 14:42:06 +01001394 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001395
Maxime Ripard4facfe72014-11-17 14:42:06 +01001396 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001397
Maxime Ripard4facfe72014-11-17 14:42:06 +01001398 dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
1399 set_bit(ATC_IS_PAUSED, &atchan->status);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001400
Maxime Ripard4facfe72014-11-17 14:42:06 +01001401 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001402
Maxime Ripard4facfe72014-11-17 14:42:06 +01001403 return 0;
1404}
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001405
Maxime Ripard4facfe72014-11-17 14:42:06 +01001406static int atc_resume(struct dma_chan *chan)
1407{
1408 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1409 struct at_dma *atdma = to_at_dma(chan->device);
1410 int chan_id = atchan->chan_common.chan_id;
1411 unsigned long flags;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001412
Maxime Ripard4facfe72014-11-17 14:42:06 +01001413 LIST_HEAD(list);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001414
Maxime Ripard4facfe72014-11-17 14:42:06 +01001415 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001416
Maxime Ripard4facfe72014-11-17 14:42:06 +01001417 if (!atc_chan_is_paused(atchan))
1418 return 0;
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001419
Maxime Ripard4facfe72014-11-17 14:42:06 +01001420 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001421
Maxime Ripard4facfe72014-11-17 14:42:06 +01001422 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
1423 clear_bit(ATC_IS_PAUSED, &atchan->status);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001424
Maxime Ripard4facfe72014-11-17 14:42:06 +01001425 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferre23b5e3a2011-05-06 19:56:52 +02001426
Maxime Ripard4facfe72014-11-17 14:42:06 +01001427 return 0;
1428}
1429
1430static int atc_terminate_all(struct dma_chan *chan)
1431{
1432 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1433 struct at_dma *atdma = to_at_dma(chan->device);
1434 int chan_id = atchan->chan_common.chan_id;
1435 struct at_desc *desc, *_desc;
1436 unsigned long flags;
1437
1438 LIST_HEAD(list);
1439
1440 dev_vdbg(chan2dev(chan), "%s\n", __func__);
1441
1442 /*
1443 * This is only called when something went wrong elsewhere, so
1444 * we don't really care about the data. Just disable the
1445 * channel. We still have to poll the channel enable bit due
1446 * to AHB/HSB limitations.
1447 */
1448 spin_lock_irqsave(&atchan->lock, flags);
1449
1450 /* disabling channel: must also remove suspend state */
1451 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
1452
1453 /* confirm that this channel is disabled */
1454 while (dma_readl(atdma, CHSR) & atchan->mask)
1455 cpu_relax();
1456
1457 /* active_list entries will end up before queued entries */
1458 list_splice_init(&atchan->queue, &list);
1459 list_splice_init(&atchan->active_list, &list);
1460
1461 /* Flush all pending and queued descriptors */
1462 list_for_each_entry_safe(desc, _desc, &list, desc_node)
1463 atc_chain_complete(atchan, desc);
1464
1465 clear_bit(ATC_IS_PAUSED, &atchan->status);
1466 /* if channel dedicated to cyclic operations, free it */
1467 clear_bit(ATC_IS_CYCLIC, &atchan->status);
1468
1469 spin_unlock_irqrestore(&atchan->lock, flags);
Yong Wangb0ebeb92010-08-05 10:40:08 +08001470
Linus Walleijc3635c72010-03-26 16:44:01 -07001471 return 0;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001472}
1473
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001474/**
Linus Walleij07934482010-03-26 16:50:49 -07001475 * atc_tx_status - poll for transaction completion
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001476 * @chan: DMA channel
1477 * @cookie: transaction identifier to check status of
Linus Walleij07934482010-03-26 16:50:49 -07001478 * @txstate: if not %NULL updated with transaction state
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001479 *
Linus Walleij07934482010-03-26 16:50:49 -07001480 * If @txstate is passed in, upon return it reflect the driver
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001481 * internal state and can be used with dma_async_is_complete() to check
1482 * the status of multiple cookies without re-checking hardware state.
1483 */
1484static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -07001485atc_tx_status(struct dma_chan *chan,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001486 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -07001487 struct dma_tx_state *txstate)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001488{
1489 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001490 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001491 enum dma_status ret;
Elen Songd48de6f2013-05-10 11:01:46 +08001492 int bytes = 0;
1493
1494 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul6d203d12013-10-16 13:34:35 +05301495 if (ret == DMA_COMPLETE)
Elen Songd48de6f2013-05-10 11:01:46 +08001496 return ret;
1497 /*
1498 * There's no point calculating the residue if there's
1499 * no txstate to store the value.
1500 */
1501 if (!txstate)
1502 return DMA_ERROR;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001503
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001504 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001505
Elen Songd48de6f2013-05-10 11:01:46 +08001506 /* Get number of bytes left in the active transactions */
Torsten Fleischerbdf6c792015-02-23 17:54:10 +01001507 bytes = atc_get_bytes_left(chan, cookie);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001508
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001509 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001510
Elen Songd48de6f2013-05-10 11:01:46 +08001511 if (unlikely(bytes < 0)) {
1512 dev_vdbg(chan2dev(chan), "get residual bytes error\n");
1513 return DMA_ERROR;
Nicolas Ferrec3dbc602013-06-07 17:26:14 +02001514 } else {
Elen Songd48de6f2013-05-10 11:01:46 +08001515 dma_set_residue(txstate, bytes);
Nicolas Ferrec3dbc602013-06-07 17:26:14 +02001516 }
Nicolas Ferre543aabc2011-05-06 19:56:51 +02001517
Elen Songd48de6f2013-05-10 11:01:46 +08001518 dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d residue = %d\n",
1519 ret, cookie, bytes);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001520
1521 return ret;
1522}
1523
1524/**
1525 * atc_issue_pending - try to finish work
1526 * @chan: target DMA channel
1527 */
1528static void atc_issue_pending(struct dma_chan *chan)
1529{
1530 struct at_dma_chan *atchan = to_at_dma_chan(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001531 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001532
1533 dev_vdbg(chan2dev(chan), "issue_pending\n");
1534
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001535 /* Not needed for cyclic transfers */
Nicolas Ferre3c477482011-07-25 21:09:23 +00001536 if (atc_chan_is_cyclic(atchan))
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001537 return;
1538
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001539 spin_lock_irqsave(&atchan->lock, flags);
Ludovic Desrochesd202f052013-04-18 09:52:59 +02001540 atc_advance_work(atchan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001541 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001542}
1543
1544/**
1545 * atc_alloc_chan_resources - allocate resources for DMA channel
1546 * @chan: allocate descriptor resources for this channel
1547 * @client: current client requesting the channel be ready for requests
1548 *
1549 * return - the number of allocated descriptors
1550 */
1551static int atc_alloc_chan_resources(struct dma_chan *chan)
1552{
1553 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1554 struct at_dma *atdma = to_at_dma(chan->device);
1555 struct at_desc *desc;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001556 struct at_dma_slave *atslave;
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001557 unsigned long flags;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001558 int i;
Nicolas Ferre808347f2009-07-22 20:04:45 +02001559 u32 cfg;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001560 LIST_HEAD(tmp_list);
1561
1562 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
1563
1564 /* ASSERT: channel is idle */
1565 if (atc_chan_is_enabled(atchan)) {
1566 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
1567 return -EIO;
1568 }
1569
Nicolas Ferre808347f2009-07-22 20:04:45 +02001570 cfg = ATC_DEFAULT_CFG;
1571
1572 atslave = chan->private;
1573 if (atslave) {
1574 /*
1575 * We need controller-specific data to set up slave
1576 * transfers.
1577 */
1578 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
1579
Nicolas Ferreea7e7902013-05-10 15:19:13 +02001580 /* if cfg configuration specified take it instead of default */
Nicolas Ferre808347f2009-07-22 20:04:45 +02001581 if (atslave->cfg)
1582 cfg = atslave->cfg;
1583 }
1584
1585 /* have we already been set up?
1586 * reconfigure channel but no need to reallocate descriptors */
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001587 if (!list_empty(&atchan->free_list))
1588 return atchan->descs_allocated;
1589
1590 /* Allocate initial pool of descriptors */
1591 for (i = 0; i < init_nr_desc_per_channel; i++) {
1592 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
1593 if (!desc) {
1594 dev_err(atdma->dma_common.dev,
1595 "Only %d initial descriptors\n", i);
1596 break;
1597 }
1598 list_add_tail(&desc->desc_node, &tmp_list);
1599 }
1600
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001601 spin_lock_irqsave(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001602 atchan->descs_allocated = i;
1603 list_splice(&tmp_list, &atchan->free_list);
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001604 dma_cookie_init(chan);
Nicolas Ferred8cb04b2011-07-27 12:21:28 +00001605 spin_unlock_irqrestore(&atchan->lock, flags);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001606
1607 /* channel parameters */
Nicolas Ferre808347f2009-07-22 20:04:45 +02001608 channel_writel(atchan, CFG, cfg);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001609
1610 dev_dbg(chan2dev(chan),
1611 "alloc_chan_resources: allocated %d descriptors\n",
1612 atchan->descs_allocated);
1613
1614 return atchan->descs_allocated;
1615}
1616
1617/**
1618 * atc_free_chan_resources - free all channel resources
1619 * @chan: DMA channel
1620 */
1621static void atc_free_chan_resources(struct dma_chan *chan)
1622{
1623 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1624 struct at_dma *atdma = to_at_dma(chan->device);
1625 struct at_desc *desc, *_desc;
1626 LIST_HEAD(list);
1627
1628 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
1629 atchan->descs_allocated);
1630
1631 /* ASSERT: channel is idle */
1632 BUG_ON(!list_empty(&atchan->active_list));
1633 BUG_ON(!list_empty(&atchan->queue));
1634 BUG_ON(atc_chan_is_enabled(atchan));
1635
1636 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
1637 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1638 list_del(&desc->desc_node);
1639 /* free link descriptor */
1640 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
1641 }
1642 list_splice_init(&atchan->free_list, &list);
1643 atchan->descs_allocated = 0;
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001644 atchan->status = 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001645
1646 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
1647}
1648
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001649#ifdef CONFIG_OF
1650static bool at_dma_filter(struct dma_chan *chan, void *slave)
1651{
1652 struct at_dma_slave *atslave = slave;
1653
1654 if (atslave->dma_dev == chan->device->dev) {
1655 chan->private = atslave;
1656 return true;
1657 } else {
1658 return false;
1659 }
1660}
1661
1662static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
1663 struct of_dma *of_dma)
1664{
1665 struct dma_chan *chan;
1666 struct at_dma_chan *atchan;
1667 struct at_dma_slave *atslave;
1668 dma_cap_mask_t mask;
1669 unsigned int per_id;
1670 struct platform_device *dmac_pdev;
1671
1672 if (dma_spec->args_count != 2)
1673 return NULL;
1674
1675 dmac_pdev = of_find_device_by_node(dma_spec->np);
1676
1677 dma_cap_zero(mask);
1678 dma_cap_set(DMA_SLAVE, mask);
1679
1680 atslave = devm_kzalloc(&dmac_pdev->dev, sizeof(*atslave), GFP_KERNEL);
1681 if (!atslave)
1682 return NULL;
Ludovic Desroches62971b22013-06-13 10:39:39 +02001683
1684 atslave->cfg = ATC_DST_H2SEL_HW | ATC_SRC_H2SEL_HW;
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001685 /*
1686 * We can fill both SRC_PER and DST_PER, one of these fields will be
1687 * ignored depending on DMA transfer direction.
1688 */
Ludovic Desroches62971b22013-06-13 10:39:39 +02001689 per_id = dma_spec->args[1] & AT91_DMA_CFG_PER_ID_MASK;
1690 atslave->cfg |= ATC_DST_PER_MSB(per_id) | ATC_DST_PER(per_id)
Nicolas Ferre6c227702013-05-10 15:19:15 +02001691 | ATC_SRC_PER_MSB(per_id) | ATC_SRC_PER(per_id);
Ludovic Desroches62971b22013-06-13 10:39:39 +02001692 /*
1693 * We have to translate the value we get from the device tree since
1694 * the half FIFO configuration value had to be 0 to keep backward
1695 * compatibility.
1696 */
1697 switch (dma_spec->args[1] & AT91_DMA_CFG_FIFOCFG_MASK) {
1698 case AT91_DMA_CFG_FIFOCFG_ALAP:
1699 atslave->cfg |= ATC_FIFOCFG_LARGESTBURST;
1700 break;
1701 case AT91_DMA_CFG_FIFOCFG_ASAP:
1702 atslave->cfg |= ATC_FIFOCFG_ENOUGHSPACE;
1703 break;
1704 case AT91_DMA_CFG_FIFOCFG_HALF:
1705 default:
1706 atslave->cfg |= ATC_FIFOCFG_HALFFIFO;
1707 }
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001708 atslave->dma_dev = &dmac_pdev->dev;
1709
1710 chan = dma_request_channel(mask, at_dma_filter, atslave);
1711 if (!chan)
1712 return NULL;
1713
1714 atchan = to_at_dma_chan(chan);
1715 atchan->per_if = dma_spec->args[0] & 0xff;
1716 atchan->mem_if = (dma_spec->args[0] >> 16) & 0xff;
1717
1718 return chan;
1719}
1720#else
1721static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
1722 struct of_dma *of_dma)
1723{
1724 return NULL;
1725}
1726#endif
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001727
1728/*-- Module Management -----------------------------------------------*/
1729
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001730/* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
1731static struct at_dma_platform_data at91sam9rl_config = {
1732 .nr_channels = 2,
1733};
1734static struct at_dma_platform_data at91sam9g45_config = {
1735 .nr_channels = 8,
1736};
1737
Nicolas Ferrec5115952011-10-17 14:56:41 +02001738#if defined(CONFIG_OF)
1739static const struct of_device_id atmel_dma_dt_ids[] = {
1740 {
1741 .compatible = "atmel,at91sam9rl-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001742 .data = &at91sam9rl_config,
Nicolas Ferrec5115952011-10-17 14:56:41 +02001743 }, {
1744 .compatible = "atmel,at91sam9g45-dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001745 .data = &at91sam9g45_config,
Nicolas Ferredcc81732011-11-22 11:55:53 +01001746 }, {
1747 /* sentinel */
1748 }
Nicolas Ferrec5115952011-10-17 14:56:41 +02001749};
1750
1751MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
1752#endif
1753
Nicolas Ferre0ab88a02011-11-22 11:55:52 +01001754static const struct platform_device_id atdma_devtypes[] = {
Nicolas Ferre67348452011-10-17 14:56:40 +02001755 {
1756 .name = "at91sam9rl_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001757 .driver_data = (unsigned long) &at91sam9rl_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001758 }, {
1759 .name = "at91sam9g45_dma",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001760 .driver_data = (unsigned long) &at91sam9g45_config,
Nicolas Ferre67348452011-10-17 14:56:40 +02001761 }, {
1762 /* sentinel */
1763 }
1764};
1765
Uwe Kleine-König7fd63cc2012-07-13 14:32:10 +02001766static inline const struct at_dma_platform_data * __init at_dma_get_driver_data(
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001767 struct platform_device *pdev)
Nicolas Ferrec5115952011-10-17 14:56:41 +02001768{
1769 if (pdev->dev.of_node) {
1770 const struct of_device_id *match;
1771 match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
1772 if (match == NULL)
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001773 return NULL;
1774 return match->data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001775 }
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001776 return (struct at_dma_platform_data *)
1777 platform_get_device_id(pdev)->driver_data;
Nicolas Ferrec5115952011-10-17 14:56:41 +02001778}
1779
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001780/**
1781 * at_dma_off - disable DMA controller
1782 * @atdma: the Atmel HDAMC device
1783 */
1784static void at_dma_off(struct at_dma *atdma)
1785{
1786 dma_writel(atdma, EN, 0);
1787
1788 /* disable all interrupts */
1789 dma_writel(atdma, EBCIDR, -1L);
1790
1791 /* confirm that all channels are disabled */
1792 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
1793 cpu_relax();
1794}
1795
1796static int __init at_dma_probe(struct platform_device *pdev)
1797{
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001798 struct resource *io;
1799 struct at_dma *atdma;
1800 size_t size;
1801 int irq;
1802 int err;
1803 int i;
Uwe Kleine-König7fd63cc2012-07-13 14:32:10 +02001804 const struct at_dma_platform_data *plat_dat;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001805
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001806 /* setup platform data for each SoC */
1807 dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
Torsten Fleischer265567f2015-02-23 17:54:11 +01001808 dma_cap_set(DMA_SG, at91sam9rl_config.cap_mask);
Maxime Ripard5abecfa2015-05-27 16:01:53 +02001809 dma_cap_set(DMA_INTERLEAVE, at91sam9g45_config.cap_mask);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001810 dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
Maxime Ripard4d112422015-08-24 11:21:15 +02001811 dma_cap_set(DMA_MEMSET, at91sam9g45_config.cap_mask);
1812 dma_cap_set(DMA_PRIVATE, at91sam9g45_config.cap_mask);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001813 dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
Torsten Fleischer265567f2015-02-23 17:54:11 +01001814 dma_cap_set(DMA_SG, at91sam9g45_config.cap_mask);
Nicolas Ferre67348452011-10-17 14:56:40 +02001815
1816 /* get DMA parameters from controller type */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001817 plat_dat = at_dma_get_driver_data(pdev);
1818 if (!plat_dat)
1819 return -ENODEV;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001820
1821 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1822 if (!io)
1823 return -EINVAL;
1824
1825 irq = platform_get_irq(pdev, 0);
1826 if (irq < 0)
1827 return irq;
1828
1829 size = sizeof(struct at_dma);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001830 size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001831 atdma = kzalloc(size, GFP_KERNEL);
1832 if (!atdma)
1833 return -ENOMEM;
1834
Nicolas Ferre67348452011-10-17 14:56:40 +02001835 /* discover transaction capabilities */
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001836 atdma->dma_common.cap_mask = plat_dat->cap_mask;
1837 atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001838
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07001839 size = resource_size(io);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001840 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1841 err = -EBUSY;
1842 goto err_kfree;
1843 }
1844
1845 atdma->regs = ioremap(io->start, size);
1846 if (!atdma->regs) {
1847 err = -ENOMEM;
1848 goto err_release_r;
1849 }
1850
1851 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1852 if (IS_ERR(atdma->clk)) {
1853 err = PTR_ERR(atdma->clk);
1854 goto err_clk;
1855 }
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001856 err = clk_prepare_enable(atdma->clk);
1857 if (err)
1858 goto err_clk_prepare;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001859
1860 /* force dma off, just in case */
1861 at_dma_off(atdma);
1862
1863 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1864 if (err)
1865 goto err_irq;
1866
1867 platform_set_drvdata(pdev, atdma);
1868
1869 /* create a pool of consistent memory blocks for hardware descriptors */
1870 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1871 &pdev->dev, sizeof(struct at_desc),
1872 4 /* word alignment */, 0);
1873 if (!atdma->dma_desc_pool) {
1874 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1875 err = -ENOMEM;
Maxime Ripard4d112422015-08-24 11:21:15 +02001876 goto err_desc_pool_create;
1877 }
1878
1879 /* create a pool of consistent memory blocks for memset blocks */
1880 atdma->memset_pool = dma_pool_create("at_hdmac_memset_pool",
1881 &pdev->dev, sizeof(int), 4, 0);
1882 if (!atdma->memset_pool) {
1883 dev_err(&pdev->dev, "No memory for memset dma pool\n");
1884 err = -ENOMEM;
1885 goto err_memset_pool_create;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001886 }
1887
1888 /* clear any pending interrupt */
1889 while (dma_readl(atdma, EBCISR))
1890 cpu_relax();
1891
1892 /* initialize channels related values */
1893 INIT_LIST_HEAD(&atdma->dma_common.channels);
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001894 for (i = 0; i < plat_dat->nr_channels; i++) {
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001895 struct at_dma_chan *atchan = &atdma->chan[i];
1896
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001897 atchan->mem_if = AT_DMA_MEM_IF;
1898 atchan->per_if = AT_DMA_PER_IF;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001899 atchan->chan_common.device = &atdma->dma_common;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001900 dma_cookie_init(&atchan->chan_common);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001901 list_add_tail(&atchan->chan_common.device_node,
1902 &atdma->dma_common.channels);
1903
1904 atchan->ch_regs = atdma->regs + ch_regs(i);
1905 spin_lock_init(&atchan->lock);
1906 atchan->mask = 1 << i;
1907
1908 INIT_LIST_HEAD(&atchan->active_list);
1909 INIT_LIST_HEAD(&atchan->queue);
1910 INIT_LIST_HEAD(&atchan->free_list);
1911
1912 tasklet_init(&atchan->tasklet, atc_tasklet,
1913 (unsigned long)atchan);
Nikolaus Vossbda3a472012-01-17 10:28:33 +01001914 atc_enable_chan_irq(atdma, i);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001915 }
1916
1917 /* set base routines */
1918 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1919 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
Linus Walleij07934482010-03-26 16:50:49 -07001920 atdma->dma_common.device_tx_status = atc_tx_status;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001921 atdma->dma_common.device_issue_pending = atc_issue_pending;
1922 atdma->dma_common.dev = &pdev->dev;
1923
1924 /* set prep routines based on capability */
Maxime Ripard5abecfa2015-05-27 16:01:53 +02001925 if (dma_has_cap(DMA_INTERLEAVE, atdma->dma_common.cap_mask))
1926 atdma->dma_common.device_prep_interleaved_dma = atc_prep_dma_interleaved;
1927
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001928 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1929 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1930
Maxime Ripard4d112422015-08-24 11:21:15 +02001931 if (dma_has_cap(DMA_MEMSET, atdma->dma_common.cap_mask)) {
1932 atdma->dma_common.device_prep_dma_memset = atc_prep_dma_memset;
1933 atdma->dma_common.fill_align = DMAENGINE_ALIGN_4_BYTES;
1934 }
1935
Nicolas Ferred7db8082011-08-05 11:43:44 +00001936 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
Nicolas Ferre808347f2009-07-22 20:04:45 +02001937 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001938 /* controller can do slave DMA: can trigger cyclic transfers */
1939 dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
Nicolas Ferre53830cc2011-04-30 16:57:46 +02001940 atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
Maxime Ripard4facfe72014-11-17 14:42:06 +01001941 atdma->dma_common.device_config = atc_config;
1942 atdma->dma_common.device_pause = atc_pause;
1943 atdma->dma_common.device_resume = atc_resume;
1944 atdma->dma_common.device_terminate_all = atc_terminate_all;
Ludovic Desroches816070e2015-01-06 17:36:26 +01001945 atdma->dma_common.src_addr_widths = ATC_DMA_BUSWIDTHS;
1946 atdma->dma_common.dst_addr_widths = ATC_DMA_BUSWIDTHS;
1947 atdma->dma_common.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1948 atdma->dma_common.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Nicolas Ferred7db8082011-08-05 11:43:44 +00001949 }
Nicolas Ferre808347f2009-07-22 20:04:45 +02001950
Torsten Fleischer265567f2015-02-23 17:54:11 +01001951 if (dma_has_cap(DMA_SG, atdma->dma_common.cap_mask))
1952 atdma->dma_common.device_prep_dma_sg = atc_prep_dma_sg;
1953
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001954 dma_writel(atdma, EN, AT_DMA_ENABLE);
1955
Maxime Ripard4d112422015-08-24 11:21:15 +02001956 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s%s%s), %d channels\n",
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001957 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
Maxime Ripard4d112422015-08-24 11:21:15 +02001958 dma_has_cap(DMA_MEMSET, atdma->dma_common.cap_mask) ? "set " : "",
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001959 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
Torsten Fleischer265567f2015-02-23 17:54:11 +01001960 dma_has_cap(DMA_SG, atdma->dma_common.cap_mask) ? "sg-cpy " : "",
Nicolas Ferre02f88be2011-11-22 11:55:54 +01001961 plat_dat->nr_channels);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001962
1963 dma_async_device_register(&atdma->dma_common);
1964
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001965 /*
1966 * Do not return an error if the dmac node is not present in order to
1967 * not break the existing way of requesting channel with
1968 * dma_request_channel().
1969 */
1970 if (pdev->dev.of_node) {
1971 err = of_dma_controller_register(pdev->dev.of_node,
1972 at_dma_xlate, atdma);
1973 if (err) {
1974 dev_err(&pdev->dev, "could not register of_dma_controller\n");
1975 goto err_of_dma_controller_register;
1976 }
1977 }
1978
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001979 return 0;
1980
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001981err_of_dma_controller_register:
1982 dma_async_device_unregister(&atdma->dma_common);
Maxime Ripard4d112422015-08-24 11:21:15 +02001983 dma_pool_destroy(atdma->memset_pool);
1984err_memset_pool_create:
Ludovic Desrochesbbe89c82013-04-19 09:11:18 +00001985 dma_pool_destroy(atdma->dma_desc_pool);
Maxime Ripard4d112422015-08-24 11:21:15 +02001986err_desc_pool_create:
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001987 free_irq(platform_get_irq(pdev, 0), atdma);
1988err_irq:
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02001989 clk_disable_unprepare(atdma->clk);
1990err_clk_prepare:
Nicolas Ferredc78baa2009-07-03 19:24:33 +02001991 clk_put(atdma->clk);
1992err_clk:
1993 iounmap(atdma->regs);
1994 atdma->regs = NULL;
1995err_release_r:
1996 release_mem_region(io->start, size);
1997err_kfree:
1998 kfree(atdma);
1999 return err;
2000}
2001
Maxin B. John1d1bbd32013-02-20 02:07:04 +02002002static int at_dma_remove(struct platform_device *pdev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002003{
2004 struct at_dma *atdma = platform_get_drvdata(pdev);
2005 struct dma_chan *chan, *_chan;
2006 struct resource *io;
2007
2008 at_dma_off(atdma);
2009 dma_async_device_unregister(&atdma->dma_common);
2010
Maxime Ripard4d112422015-08-24 11:21:15 +02002011 dma_pool_destroy(atdma->memset_pool);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002012 dma_pool_destroy(atdma->dma_desc_pool);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002013 free_irq(platform_get_irq(pdev, 0), atdma);
2014
2015 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
2016 device_node) {
2017 struct at_dma_chan *atchan = to_at_dma_chan(chan);
2018
2019 /* Disable interrupts */
Nikolaus Vossbda3a472012-01-17 10:28:33 +01002020 atc_disable_chan_irq(atdma, chan->chan_id);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002021
2022 tasklet_kill(&atchan->tasklet);
2023 list_del(&chan->device_node);
2024 }
2025
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02002026 clk_disable_unprepare(atdma->clk);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002027 clk_put(atdma->clk);
2028
2029 iounmap(atdma->regs);
2030 atdma->regs = NULL;
2031
2032 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
H Hartley Sweeten114df7d2011-06-01 15:16:09 -07002033 release_mem_region(io->start, resource_size(io));
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002034
2035 kfree(atdma);
2036
2037 return 0;
2038}
2039
2040static void at_dma_shutdown(struct platform_device *pdev)
2041{
2042 struct at_dma *atdma = platform_get_drvdata(pdev);
2043
2044 at_dma_off(platform_get_drvdata(pdev));
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02002045 clk_disable_unprepare(atdma->clk);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002046}
2047
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00002048static int at_dma_prepare(struct device *dev)
2049{
2050 struct platform_device *pdev = to_platform_device(dev);
2051 struct at_dma *atdma = platform_get_drvdata(pdev);
2052 struct dma_chan *chan, *_chan;
2053
2054 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
2055 device_node) {
2056 struct at_dma_chan *atchan = to_at_dma_chan(chan);
2057 /* wait for transaction completion (except in cyclic case) */
Nicolas Ferre3c477482011-07-25 21:09:23 +00002058 if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00002059 return -EAGAIN;
2060 }
2061 return 0;
2062}
2063
2064static void atc_suspend_cyclic(struct at_dma_chan *atchan)
2065{
2066 struct dma_chan *chan = &atchan->chan_common;
2067
2068 /* Channel should be paused by user
2069 * do it anyway even if it is not done already */
Nicolas Ferre3c477482011-07-25 21:09:23 +00002070 if (!atc_chan_is_paused(atchan)) {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00002071 dev_warn(chan2dev(chan),
2072 "cyclic channel not paused, should be done by channel user\n");
Maxime Ripard4facfe72014-11-17 14:42:06 +01002073 atc_pause(chan);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00002074 }
2075
2076 /* now preserve additional data for cyclic operations */
2077 /* next descriptor address in the cyclic list */
2078 atchan->save_dscr = channel_readl(atchan, DSCR);
2079
2080 vdbg_dump_regs(atchan);
2081}
2082
Dan Williams33f82d12009-09-10 00:06:44 +02002083static int at_dma_suspend_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002084{
Dan Williams33f82d12009-09-10 00:06:44 +02002085 struct platform_device *pdev = to_platform_device(dev);
2086 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00002087 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002088
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00002089 /* preserve data */
2090 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
2091 device_node) {
2092 struct at_dma_chan *atchan = to_at_dma_chan(chan);
2093
Nicolas Ferre3c477482011-07-25 21:09:23 +00002094 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00002095 atc_suspend_cyclic(atchan);
2096 atchan->save_cfg = channel_readl(atchan, CFG);
2097 }
2098 atdma->save_imr = dma_readl(atdma, EBCIMR);
2099
2100 /* disable DMA controller */
2101 at_dma_off(atdma);
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02002102 clk_disable_unprepare(atdma->clk);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002103 return 0;
2104}
2105
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00002106static void atc_resume_cyclic(struct at_dma_chan *atchan)
2107{
2108 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
2109
2110 /* restore channel status for cyclic descriptors list:
2111 * next descriptor in the cyclic list at the time of suspend */
2112 channel_writel(atchan, SADDR, 0);
2113 channel_writel(atchan, DADDR, 0);
2114 channel_writel(atchan, CTRLA, 0);
2115 channel_writel(atchan, CTRLB, 0);
2116 channel_writel(atchan, DSCR, atchan->save_dscr);
2117 dma_writel(atdma, CHER, atchan->mask);
2118
2119 /* channel pause status should be removed by channel user
2120 * We cannot take the initiative to do it here */
2121
2122 vdbg_dump_regs(atchan);
2123}
2124
Dan Williams33f82d12009-09-10 00:06:44 +02002125static int at_dma_resume_noirq(struct device *dev)
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002126{
Dan Williams33f82d12009-09-10 00:06:44 +02002127 struct platform_device *pdev = to_platform_device(dev);
2128 struct at_dma *atdma = platform_get_drvdata(pdev);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00002129 struct dma_chan *chan, *_chan;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002130
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00002131 /* bring back DMA controller */
Boris BREZILLONf784d9c2013-06-19 13:14:54 +02002132 clk_prepare_enable(atdma->clk);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002133 dma_writel(atdma, EN, AT_DMA_ENABLE);
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00002134
2135 /* clear any pending interrupt */
2136 while (dma_readl(atdma, EBCISR))
2137 cpu_relax();
2138
2139 /* restore saved data */
2140 dma_writel(atdma, EBCIER, atdma->save_imr);
2141 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
2142 device_node) {
2143 struct at_dma_chan *atchan = to_at_dma_chan(chan);
2144
2145 channel_writel(atchan, CFG, atchan->save_cfg);
Nicolas Ferre3c477482011-07-25 21:09:23 +00002146 if (atc_chan_is_cyclic(atchan))
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00002147 atc_resume_cyclic(atchan);
2148 }
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002149 return 0;
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002150}
2151
Alexey Dobriyan47145212009-12-14 18:00:08 -08002152static const struct dev_pm_ops at_dma_dev_pm_ops = {
Nicolas Ferrec0ba5942011-07-27 12:21:29 +00002153 .prepare = at_dma_prepare,
Dan Williams33f82d12009-09-10 00:06:44 +02002154 .suspend_noirq = at_dma_suspend_noirq,
2155 .resume_noirq = at_dma_resume_noirq,
2156};
2157
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002158static struct platform_driver at_dma_driver = {
Maxin B. John1d1bbd32013-02-20 02:07:04 +02002159 .remove = at_dma_remove,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002160 .shutdown = at_dma_shutdown,
Nicolas Ferre67348452011-10-17 14:56:40 +02002161 .id_table = atdma_devtypes,
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002162 .driver = {
2163 .name = "at_hdmac",
Dan Williams33f82d12009-09-10 00:06:44 +02002164 .pm = &at_dma_dev_pm_ops,
Nicolas Ferrec5115952011-10-17 14:56:41 +02002165 .of_match_table = of_match_ptr(atmel_dma_dt_ids),
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002166 },
2167};
2168
2169static int __init at_dma_init(void)
2170{
2171 return platform_driver_probe(&at_dma_driver, at_dma_probe);
2172}
Eric Xu93d0bec2011-01-12 15:39:08 +01002173subsys_initcall(at_dma_init);
Nicolas Ferredc78baa2009-07-03 19:24:33 +02002174
2175static void __exit at_dma_exit(void)
2176{
2177 platform_driver_unregister(&at_dma_driver);
2178}
2179module_exit(at_dma_exit);
2180
2181MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
2182MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
2183MODULE_LICENSE("GPL");
2184MODULE_ALIAS("platform:at_hdmac");