]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/dma/at_hdmac.c
dmaengine: Remove the context argument to the prep_dma_cyclic operation
[mirror_ubuntu-artful-kernel.git] / drivers / dma / at_hdmac.c
CommitLineData
dc78baa2
NF
1/*
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 *
9102d871
NF
12 * 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.
dc78baa2
NF
15 */
16
62971b29 17#include <dt-bindings/dma/at91.h>
dc78baa2
NF
18#include <linux/clk.h>
19#include <linux/dmaengine.h>
20#include <linux/dma-mapping.h>
21#include <linux/dmapool.h>
22#include <linux/interrupt.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
5a0e3ad6 25#include <linux/slab.h>
c5115953
NF
26#include <linux/of.h>
27#include <linux/of_device.h>
bbe89c8e 28#include <linux/of_dma.h>
dc78baa2
NF
29
30#include "at_hdmac_regs.h"
d2ebfb33 31#include "dmaengine.h"
dc78baa2
NF
32
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)
ae14d4b5
NF
43#define ATC_DEFAULT_CTRLB (ATC_SIF(AT_DMA_MEM_IF) \
44 |ATC_DIF(AT_DMA_MEM_IF))
dc78baa2
NF
45
46/*
47 * Initial number of descriptors to allocate for each channel. This could
48 * be increased during dma usage.
49 */
50static unsigned int init_nr_desc_per_channel = 64;
51module_param(init_nr_desc_per_channel, uint, 0644);
52MODULE_PARM_DESC(init_nr_desc_per_channel,
53 "initial descriptors per channel (default: 64)");
54
55
56/* prototypes */
57static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx);
d48de6f1 58static void atc_issue_pending(struct dma_chan *chan);
dc78baa2
NF
59
60
61/*----------------------------------------------------------------------*/
62
63static struct at_desc *atc_first_active(struct at_dma_chan *atchan)
64{
65 return list_first_entry(&atchan->active_list,
66 struct at_desc, desc_node);
67}
68
69static struct at_desc *atc_first_queued(struct at_dma_chan *atchan)
70{
71 return list_first_entry(&atchan->queue,
72 struct at_desc, desc_node);
73}
74
75/**
421f91d2 76 * atc_alloc_descriptor - allocate and return an initialized descriptor
dc78baa2
NF
77 * @chan: the channel to allocate descriptors for
78 * @gfp_flags: GFP allocation flags
79 *
80 * Note: The ack-bit is positioned in the descriptor flag at creation time
81 * to make initial allocation more convenient. This bit will be cleared
82 * and control will be given to client at usage time (during
83 * preparation functions).
84 */
85static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan,
86 gfp_t gfp_flags)
87{
88 struct at_desc *desc = NULL;
89 struct at_dma *atdma = to_at_dma(chan->device);
90 dma_addr_t phys;
91
92 desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys);
93 if (desc) {
94 memset(desc, 0, sizeof(struct at_desc));
285a3c71 95 INIT_LIST_HEAD(&desc->tx_list);
dc78baa2
NF
96 dma_async_tx_descriptor_init(&desc->txd, chan);
97 /* txd.flags will be overwritten in prep functions */
98 desc->txd.flags = DMA_CTRL_ACK;
99 desc->txd.tx_submit = atc_tx_submit;
100 desc->txd.phys = phys;
101 }
102
103 return desc;
104}
105
106/**
af901ca1 107 * atc_desc_get - get an unused descriptor from free_list
dc78baa2
NF
108 * @atchan: channel we want a new descriptor for
109 */
110static struct at_desc *atc_desc_get(struct at_dma_chan *atchan)
111{
112 struct at_desc *desc, *_desc;
113 struct at_desc *ret = NULL;
d8cb04b0 114 unsigned long flags;
dc78baa2
NF
115 unsigned int i = 0;
116 LIST_HEAD(tmp_list);
117
d8cb04b0 118 spin_lock_irqsave(&atchan->lock, flags);
dc78baa2
NF
119 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
120 i++;
121 if (async_tx_test_ack(&desc->txd)) {
122 list_del(&desc->desc_node);
123 ret = desc;
124 break;
125 }
126 dev_dbg(chan2dev(&atchan->chan_common),
127 "desc %p not ACKed\n", desc);
128 }
d8cb04b0 129 spin_unlock_irqrestore(&atchan->lock, flags);
dc78baa2
NF
130 dev_vdbg(chan2dev(&atchan->chan_common),
131 "scanned %u descriptors on freelist\n", i);
132
133 /* no more descriptor available in initial pool: create one more */
134 if (!ret) {
135 ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC);
136 if (ret) {
d8cb04b0 137 spin_lock_irqsave(&atchan->lock, flags);
dc78baa2 138 atchan->descs_allocated++;
d8cb04b0 139 spin_unlock_irqrestore(&atchan->lock, flags);
dc78baa2
NF
140 } else {
141 dev_err(chan2dev(&atchan->chan_common),
142 "not enough descriptors available\n");
143 }
144 }
145
146 return ret;
147}
148
149/**
150 * atc_desc_put - move a descriptor, including any children, to the free list
151 * @atchan: channel we work on
152 * @desc: descriptor, at the head of a chain, to move to free list
153 */
154static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc)
155{
156 if (desc) {
157 struct at_desc *child;
d8cb04b0 158 unsigned long flags;
dc78baa2 159
d8cb04b0 160 spin_lock_irqsave(&atchan->lock, flags);
285a3c71 161 list_for_each_entry(child, &desc->tx_list, desc_node)
dc78baa2
NF
162 dev_vdbg(chan2dev(&atchan->chan_common),
163 "moving child desc %p to freelist\n",
164 child);
285a3c71 165 list_splice_init(&desc->tx_list, &atchan->free_list);
dc78baa2
NF
166 dev_vdbg(chan2dev(&atchan->chan_common),
167 "moving desc %p to freelist\n", desc);
168 list_add(&desc->desc_node, &atchan->free_list);
d8cb04b0 169 spin_unlock_irqrestore(&atchan->lock, flags);
dc78baa2
NF
170 }
171}
172
53830cc7 173/**
d73111c6
MI
174 * atc_desc_chain - build chain adding a descriptor
175 * @first: address of first descriptor of the chain
176 * @prev: address of previous descriptor of the chain
53830cc7
NF
177 * @desc: descriptor to queue
178 *
179 * Called from prep_* functions
180 */
181static void atc_desc_chain(struct at_desc **first, struct at_desc **prev,
182 struct at_desc *desc)
183{
184 if (!(*first)) {
185 *first = desc;
186 } else {
187 /* inform the HW lli about chaining */
188 (*prev)->lli.dscr = desc->txd.phys;
189 /* insert the link descriptor to the LD ring */
190 list_add_tail(&desc->desc_node,
191 &(*first)->tx_list);
192 }
193 *prev = desc;
194}
195
dc78baa2
NF
196/**
197 * atc_dostart - starts the DMA engine for real
198 * @atchan: the channel we want to start
199 * @first: first descriptor in the list we want to begin with
200 *
201 * Called with atchan->lock held and bh disabled
202 */
203static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
204{
205 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
206
207 /* ASSERT: channel is idle */
208 if (atc_chan_is_enabled(atchan)) {
209 dev_err(chan2dev(&atchan->chan_common),
210 "BUG: Attempted to start non-idle channel\n");
211 dev_err(chan2dev(&atchan->chan_common),
212 " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
213 channel_readl(atchan, SADDR),
214 channel_readl(atchan, DADDR),
215 channel_readl(atchan, CTRLA),
216 channel_readl(atchan, CTRLB),
217 channel_readl(atchan, DSCR));
218
219 /* The tasklet will hopefully advance the queue... */
220 return;
221 }
222
223 vdbg_dump_regs(atchan);
224
dc78baa2
NF
225 channel_writel(atchan, SADDR, 0);
226 channel_writel(atchan, DADDR, 0);
227 channel_writel(atchan, CTRLA, 0);
228 channel_writel(atchan, CTRLB, 0);
229 channel_writel(atchan, DSCR, first->txd.phys);
230 dma_writel(atdma, CHER, atchan->mask);
231
232 vdbg_dump_regs(atchan);
233}
234
d48de6f1
ES
235/*
236 * atc_get_current_descriptors -
237 * locate the descriptor which equal to physical address in DSCR
238 * @atchan: the channel we want to start
239 * @dscr_addr: physical descriptor address in DSCR
240 */
241static struct at_desc *atc_get_current_descriptors(struct at_dma_chan *atchan,
242 u32 dscr_addr)
243{
244 struct at_desc *desc, *_desc, *child, *desc_cur = NULL;
245
246 list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
247 if (desc->lli.dscr == dscr_addr) {
248 desc_cur = desc;
249 break;
250 }
251
252 list_for_each_entry(child, &desc->tx_list, desc_node) {
253 if (child->lli.dscr == dscr_addr) {
254 desc_cur = child;
255 break;
256 }
257 }
258 }
259
260 return desc_cur;
261}
262
263/*
264 * atc_get_bytes_left -
265 * Get the number of bytes residue in dma buffer,
266 * @chan: the channel we want to start
267 */
268static int atc_get_bytes_left(struct dma_chan *chan)
269{
270 struct at_dma_chan *atchan = to_at_dma_chan(chan);
271 struct at_dma *atdma = to_at_dma(chan->device);
272 int chan_id = atchan->chan_common.chan_id;
273 struct at_desc *desc_first = atc_first_active(atchan);
274 struct at_desc *desc_cur;
275 int ret = 0, count = 0;
276
277 /*
278 * Initialize necessary values in the first time.
279 * remain_desc record remain desc length.
280 */
281 if (atchan->remain_desc == 0)
282 /* First descriptor embedds the transaction length */
283 atchan->remain_desc = desc_first->len;
284
285 /*
286 * This happens when current descriptor transfer complete.
287 * The residual buffer size should reduce current descriptor length.
288 */
289 if (unlikely(test_bit(ATC_IS_BTC, &atchan->status))) {
290 clear_bit(ATC_IS_BTC, &atchan->status);
291 desc_cur = atc_get_current_descriptors(atchan,
292 channel_readl(atchan, DSCR));
293 if (!desc_cur) {
294 ret = -EINVAL;
295 goto out;
296 }
297 atchan->remain_desc -= (desc_cur->lli.ctrla & ATC_BTSIZE_MAX)
298 << (desc_first->tx_width);
299 if (atchan->remain_desc < 0) {
300 ret = -EINVAL;
301 goto out;
c3dbc60c 302 } else {
d48de6f1 303 ret = atchan->remain_desc;
c3dbc60c 304 }
d48de6f1
ES
305 } else {
306 /*
307 * Get residual bytes when current
308 * descriptor transfer in progress.
309 */
310 count = (channel_readl(atchan, CTRLA) & ATC_BTSIZE_MAX)
311 << (desc_first->tx_width);
312 ret = atchan->remain_desc - count;
313 }
314 /*
315 * Check fifo empty.
316 */
317 if (!(dma_readl(atdma, CHSR) & AT_DMA_EMPT(chan_id)))
318 atc_issue_pending(chan);
319
320out:
321 return ret;
322}
323
dc78baa2
NF
324/**
325 * atc_chain_complete - finish work for one transaction chain
326 * @atchan: channel we work on
327 * @desc: descriptor at the head of the chain we want do complete
328 *
329 * Called with atchan->lock held and bh disabled */
330static void
331atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
332{
dc78baa2
NF
333 struct dma_async_tx_descriptor *txd = &desc->txd;
334
335 dev_vdbg(chan2dev(&atchan->chan_common),
336 "descriptor %u complete\n", txd->cookie);
337
d4116052
VK
338 /* mark the descriptor as complete for non cyclic cases only */
339 if (!atc_chan_is_cyclic(atchan))
340 dma_cookie_complete(txd);
dc78baa2
NF
341
342 /* move children to free_list */
285a3c71 343 list_splice_init(&desc->tx_list, &atchan->free_list);
dc78baa2
NF
344 /* move myself to free_list */
345 list_move(&desc->desc_node, &atchan->free_list);
346
d38a8c62 347 dma_descriptor_unmap(txd);
53830cc7
NF
348 /* for cyclic transfers,
349 * no need to replay callback function while stopping */
3c477482 350 if (!atc_chan_is_cyclic(atchan)) {
53830cc7
NF
351 dma_async_tx_callback callback = txd->callback;
352 void *param = txd->callback_param;
353
354 /*
355 * The API requires that no submissions are done from a
356 * callback, so we don't need to drop the lock here
357 */
358 if (callback)
359 callback(param);
360 }
dc78baa2
NF
361
362 dma_run_dependencies(txd);
363}
364
365/**
366 * atc_complete_all - finish work for all transactions
367 * @atchan: channel to complete transactions for
368 *
369 * Eventually submit queued descriptors if any
370 *
371 * Assume channel is idle while calling this function
372 * Called with atchan->lock held and bh disabled
373 */
374static void atc_complete_all(struct at_dma_chan *atchan)
375{
376 struct at_desc *desc, *_desc;
377 LIST_HEAD(list);
378
379 dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
380
dc78baa2
NF
381 /*
382 * Submit queued descriptors ASAP, i.e. before we go through
383 * the completed ones.
384 */
385 if (!list_empty(&atchan->queue))
386 atc_dostart(atchan, atc_first_queued(atchan));
387 /* empty active_list now it is completed */
388 list_splice_init(&atchan->active_list, &list);
389 /* empty queue list by moving descriptors (if any) to active_list */
390 list_splice_init(&atchan->queue, &atchan->active_list);
391
392 list_for_each_entry_safe(desc, _desc, &list, desc_node)
393 atc_chain_complete(atchan, desc);
394}
395
dc78baa2
NF
396/**
397 * atc_advance_work - at the end of a transaction, move forward
398 * @atchan: channel where the transaction ended
399 *
400 * Called with atchan->lock held and bh disabled
401 */
402static void atc_advance_work(struct at_dma_chan *atchan)
403{
404 dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
405
d202f051
LD
406 if (atc_chan_is_enabled(atchan))
407 return;
408
dc78baa2
NF
409 if (list_empty(&atchan->active_list) ||
410 list_is_singular(&atchan->active_list)) {
411 atc_complete_all(atchan);
412 } else {
413 atc_chain_complete(atchan, atc_first_active(atchan));
414 /* advance work */
415 atc_dostart(atchan, atc_first_active(atchan));
416 }
417}
418
419
420/**
421 * atc_handle_error - handle errors reported by DMA controller
422 * @atchan: channel where error occurs
423 *
424 * Called with atchan->lock held and bh disabled
425 */
426static void atc_handle_error(struct at_dma_chan *atchan)
427{
428 struct at_desc *bad_desc;
429 struct at_desc *child;
430
431 /*
432 * The descriptor currently at the head of the active list is
433 * broked. Since we don't have any way to report errors, we'll
434 * just have to scream loudly and try to carry on.
435 */
436 bad_desc = atc_first_active(atchan);
437 list_del_init(&bad_desc->desc_node);
438
439 /* As we are stopped, take advantage to push queued descriptors
440 * in active_list */
441 list_splice_init(&atchan->queue, atchan->active_list.prev);
442
443 /* Try to restart the controller */
444 if (!list_empty(&atchan->active_list))
445 atc_dostart(atchan, atc_first_active(atchan));
446
447 /*
448 * KERN_CRITICAL may seem harsh, but since this only happens
449 * when someone submits a bad physical address in a
450 * descriptor, we should consider ourselves lucky that the
451 * controller flagged an error instead of scribbling over
452 * random memory locations.
453 */
454 dev_crit(chan2dev(&atchan->chan_common),
455 "Bad descriptor submitted for DMA!\n");
456 dev_crit(chan2dev(&atchan->chan_common),
457 " cookie: %d\n", bad_desc->txd.cookie);
458 atc_dump_lli(atchan, &bad_desc->lli);
285a3c71 459 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
dc78baa2
NF
460 atc_dump_lli(atchan, &child->lli);
461
462 /* Pretend the descriptor completed successfully */
463 atc_chain_complete(atchan, bad_desc);
464}
465
53830cc7
NF
466/**
467 * atc_handle_cyclic - at the end of a period, run callback function
468 * @atchan: channel used for cyclic operations
469 *
470 * Called with atchan->lock held and bh disabled
471 */
472static void atc_handle_cyclic(struct at_dma_chan *atchan)
473{
474 struct at_desc *first = atc_first_active(atchan);
475 struct dma_async_tx_descriptor *txd = &first->txd;
476 dma_async_tx_callback callback = txd->callback;
477 void *param = txd->callback_param;
478
479 dev_vdbg(chan2dev(&atchan->chan_common),
480 "new cyclic period llp 0x%08x\n",
481 channel_readl(atchan, DSCR));
482
483 if (callback)
484 callback(param);
485}
dc78baa2
NF
486
487/*-- IRQ & Tasklet ---------------------------------------------------*/
488
489static void atc_tasklet(unsigned long data)
490{
491 struct at_dma_chan *atchan = (struct at_dma_chan *)data;
d8cb04b0 492 unsigned long flags;
dc78baa2 493
d8cb04b0 494 spin_lock_irqsave(&atchan->lock, flags);
53830cc7 495 if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
dc78baa2 496 atc_handle_error(atchan);
3c477482 497 else if (atc_chan_is_cyclic(atchan))
53830cc7 498 atc_handle_cyclic(atchan);
dc78baa2
NF
499 else
500 atc_advance_work(atchan);
501
d8cb04b0 502 spin_unlock_irqrestore(&atchan->lock, flags);
dc78baa2
NF
503}
504
505static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
506{
507 struct at_dma *atdma = (struct at_dma *)dev_id;
508 struct at_dma_chan *atchan;
509 int i;
510 u32 status, pending, imr;
511 int ret = IRQ_NONE;
512
513 do {
514 imr = dma_readl(atdma, EBCIMR);
515 status = dma_readl(atdma, EBCISR);
516 pending = status & imr;
517
518 if (!pending)
519 break;
520
521 dev_vdbg(atdma->dma_common.dev,
522 "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
523 status, imr, pending);
524
525 for (i = 0; i < atdma->dma_common.chancnt; i++) {
526 atchan = &atdma->chan[i];
9b3aa589 527 if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
dc78baa2
NF
528 if (pending & AT_DMA_ERR(i)) {
529 /* Disable channel on AHB error */
23b5e3ad
NF
530 dma_writel(atdma, CHDR,
531 AT_DMA_RES(i) | atchan->mask);
dc78baa2 532 /* Give information to tasklet */
53830cc7 533 set_bit(ATC_IS_ERROR, &atchan->status);
dc78baa2 534 }
d48de6f1
ES
535 if (pending & AT_DMA_BTC(i))
536 set_bit(ATC_IS_BTC, &atchan->status);
dc78baa2
NF
537 tasklet_schedule(&atchan->tasklet);
538 ret = IRQ_HANDLED;
539 }
540 }
541
542 } while (pending);
543
544 return ret;
545}
546
547
548/*-- DMA Engine API --------------------------------------------------*/
549
550/**
551 * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
552 * @desc: descriptor at the head of the transaction chain
553 *
554 * Queue chain if DMA engine is working already
555 *
556 * Cookie increment and adding to active_list or queue must be atomic
557 */
558static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
559{
560 struct at_desc *desc = txd_to_at_desc(tx);
561 struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
562 dma_cookie_t cookie;
d8cb04b0 563 unsigned long flags;
dc78baa2 564
d8cb04b0 565 spin_lock_irqsave(&atchan->lock, flags);
884485e1 566 cookie = dma_cookie_assign(tx);
dc78baa2
NF
567
568 if (list_empty(&atchan->active_list)) {
569 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
570 desc->txd.cookie);
571 atc_dostart(atchan, desc);
572 list_add_tail(&desc->desc_node, &atchan->active_list);
573 } else {
574 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
575 desc->txd.cookie);
576 list_add_tail(&desc->desc_node, &atchan->queue);
577 }
578
d8cb04b0 579 spin_unlock_irqrestore(&atchan->lock, flags);
dc78baa2
NF
580
581 return cookie;
582}
583
584/**
585 * atc_prep_dma_memcpy - prepare a memcpy operation
586 * @chan: the channel to prepare operation on
587 * @dest: operation virtual destination address
588 * @src: operation virtual source address
589 * @len: operation length
590 * @flags: tx descriptor status flags
591 */
592static struct dma_async_tx_descriptor *
593atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
594 size_t len, unsigned long flags)
595{
596 struct at_dma_chan *atchan = to_at_dma_chan(chan);
597 struct at_desc *desc = NULL;
598 struct at_desc *first = NULL;
599 struct at_desc *prev = NULL;
600 size_t xfer_count;
601 size_t offset;
602 unsigned int src_width;
603 unsigned int dst_width;
604 u32 ctrla;
605 u32 ctrlb;
606
607 dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
608 dest, src, len, flags);
609
610 if (unlikely(!len)) {
611 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
612 return NULL;
613 }
614
9b3aa589 615 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
dc78baa2
NF
616 | ATC_SRC_ADDR_MODE_INCR
617 | ATC_DST_ADDR_MODE_INCR
618 | ATC_FC_MEM2MEM;
619
620 /*
621 * We can be a lot more clever here, but this should take care
622 * of the most common optimization.
623 */
624 if (!((src | dest | len) & 3)) {
b409ebfb 625 ctrla = ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
dc78baa2
NF
626 src_width = dst_width = 2;
627 } else if (!((src | dest | len) & 1)) {
b409ebfb 628 ctrla = ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
dc78baa2
NF
629 src_width = dst_width = 1;
630 } else {
b409ebfb 631 ctrla = ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
dc78baa2
NF
632 src_width = dst_width = 0;
633 }
634
635 for (offset = 0; offset < len; offset += xfer_count << src_width) {
636 xfer_count = min_t(size_t, (len - offset) >> src_width,
637 ATC_BTSIZE_MAX);
638
639 desc = atc_desc_get(atchan);
640 if (!desc)
641 goto err_desc_get;
642
643 desc->lli.saddr = src + offset;
644 desc->lli.daddr = dest + offset;
645 desc->lli.ctrla = ctrla | xfer_count;
646 desc->lli.ctrlb = ctrlb;
647
648 desc->txd.cookie = 0;
dc78baa2 649
e257e156 650 atc_desc_chain(&first, &prev, desc);
dc78baa2
NF
651 }
652
653 /* First descriptor of the chain embedds additional information */
654 first->txd.cookie = -EBUSY;
655 first->len = len;
d088c33b 656 first->tx_width = src_width;
dc78baa2
NF
657
658 /* set end-of-link to the last link descriptor of list*/
659 set_desc_eol(desc);
660
568f7f0c 661 first->txd.flags = flags; /* client is in control of this ack */
dc78baa2
NF
662
663 return &first->txd;
664
665err_desc_get:
666 atc_desc_put(atchan, first);
667 return NULL;
668}
669
808347f6
NF
670
671/**
672 * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
673 * @chan: DMA channel
674 * @sgl: scatterlist to transfer to/from
675 * @sg_len: number of entries in @scatterlist
676 * @direction: DMA direction
677 * @flags: tx descriptor status flags
185ecb5f 678 * @context: transaction context (ignored)
808347f6
NF
679 */
680static struct dma_async_tx_descriptor *
681atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
db8196df 682 unsigned int sg_len, enum dma_transfer_direction direction,
185ecb5f 683 unsigned long flags, void *context)
808347f6
NF
684{
685 struct at_dma_chan *atchan = to_at_dma_chan(chan);
686 struct at_dma_slave *atslave = chan->private;
beeaa103 687 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
808347f6
NF
688 struct at_desc *first = NULL;
689 struct at_desc *prev = NULL;
690 u32 ctrla;
691 u32 ctrlb;
692 dma_addr_t reg;
693 unsigned int reg_width;
694 unsigned int mem_width;
695 unsigned int i;
696 struct scatterlist *sg;
697 size_t total_len = 0;
698
cc52a10a
NF
699 dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
700 sg_len,
db8196df 701 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
808347f6
NF
702 flags);
703
704 if (unlikely(!atslave || !sg_len)) {
c618a9be 705 dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n");
808347f6
NF
706 return NULL;
707 }
708
1dd1ea8e
NF
709 ctrla = ATC_SCSIZE(sconfig->src_maxburst)
710 | ATC_DCSIZE(sconfig->dst_maxburst);
ae14d4b5 711 ctrlb = ATC_IEN;
808347f6
NF
712
713 switch (direction) {
db8196df 714 case DMA_MEM_TO_DEV:
beeaa103 715 reg_width = convert_buswidth(sconfig->dst_addr_width);
808347f6
NF
716 ctrla |= ATC_DST_WIDTH(reg_width);
717 ctrlb |= ATC_DST_ADDR_MODE_FIXED
718 | ATC_SRC_ADDR_MODE_INCR
ae14d4b5 719 | ATC_FC_MEM2PER
bbe89c8e 720 | ATC_SIF(atchan->mem_if) | ATC_DIF(atchan->per_if);
beeaa103 721 reg = sconfig->dst_addr;
808347f6
NF
722 for_each_sg(sgl, sg, sg_len, i) {
723 struct at_desc *desc;
724 u32 len;
725 u32 mem;
726
727 desc = atc_desc_get(atchan);
728 if (!desc)
729 goto err_desc_get;
730
0f70e8ce 731 mem = sg_dma_address(sg);
808347f6 732 len = sg_dma_len(sg);
c4567976
NF
733 if (unlikely(!len)) {
734 dev_dbg(chan2dev(chan),
735 "prep_slave_sg: sg(%d) data length is zero\n", i);
736 goto err;
737 }
808347f6
NF
738 mem_width = 2;
739 if (unlikely(mem & 3 || len & 3))
740 mem_width = 0;
741
742 desc->lli.saddr = mem;
743 desc->lli.daddr = reg;
744 desc->lli.ctrla = ctrla
745 | ATC_SRC_WIDTH(mem_width)
746 | len >> mem_width;
747 desc->lli.ctrlb = ctrlb;
748
e257e156 749 atc_desc_chain(&first, &prev, desc);
808347f6
NF
750 total_len += len;
751 }
752 break;
db8196df 753 case DMA_DEV_TO_MEM:
beeaa103 754 reg_width = convert_buswidth(sconfig->src_addr_width);
808347f6
NF
755 ctrla |= ATC_SRC_WIDTH(reg_width);
756 ctrlb |= ATC_DST_ADDR_MODE_INCR
757 | ATC_SRC_ADDR_MODE_FIXED
ae14d4b5 758 | ATC_FC_PER2MEM
bbe89c8e 759 | ATC_SIF(atchan->per_if) | ATC_DIF(atchan->mem_if);
808347f6 760
beeaa103 761 reg = sconfig->src_addr;
808347f6
NF
762 for_each_sg(sgl, sg, sg_len, i) {
763 struct at_desc *desc;
764 u32 len;
765 u32 mem;
766
767 desc = atc_desc_get(atchan);
768 if (!desc)
769 goto err_desc_get;
770
0f70e8ce 771 mem = sg_dma_address(sg);
808347f6 772 len = sg_dma_len(sg);
c4567976
NF
773 if (unlikely(!len)) {
774 dev_dbg(chan2dev(chan),
775 "prep_slave_sg: sg(%d) data length is zero\n", i);
776 goto err;
777 }
808347f6
NF
778 mem_width = 2;
779 if (unlikely(mem & 3 || len & 3))
780 mem_width = 0;
781
782 desc->lli.saddr = reg;
783 desc->lli.daddr = mem;
784 desc->lli.ctrla = ctrla
785 | ATC_DST_WIDTH(mem_width)
59a609d9 786 | len >> reg_width;
808347f6
NF
787 desc->lli.ctrlb = ctrlb;
788
e257e156 789 atc_desc_chain(&first, &prev, desc);
808347f6
NF
790 total_len += len;
791 }
792 break;
793 default:
794 return NULL;
795 }
796
797 /* set end-of-link to the last link descriptor of list*/
798 set_desc_eol(prev);
799
800 /* First descriptor of the chain embedds additional information */
801 first->txd.cookie = -EBUSY;
802 first->len = total_len;
d088c33b 803 first->tx_width = reg_width;
808347f6 804
568f7f0c
NF
805 /* first link descriptor of list is responsible of flags */
806 first->txd.flags = flags; /* client is in control of this ack */
808347f6
NF
807
808 return &first->txd;
809
810err_desc_get:
811 dev_err(chan2dev(chan), "not enough descriptors available\n");
c4567976 812err:
808347f6
NF
813 atc_desc_put(atchan, first);
814 return NULL;
815}
816
53830cc7
NF
817/**
818 * atc_dma_cyclic_check_values
819 * Check for too big/unaligned periods and unaligned DMA buffer
820 */
821static int
822atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
0e7264cc 823 size_t period_len)
53830cc7
NF
824{
825 if (period_len > (ATC_BTSIZE_MAX << reg_width))
826 goto err_out;
827 if (unlikely(period_len & ((1 << reg_width) - 1)))
828 goto err_out;
829 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
830 goto err_out;
53830cc7
NF
831
832 return 0;
833
834err_out:
835 return -EINVAL;
836}
837
838/**
d73111c6 839 * atc_dma_cyclic_fill_desc - Fill one period descriptor
53830cc7
NF
840 */
841static int
beeaa103 842atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
53830cc7 843 unsigned int period_index, dma_addr_t buf_addr,
beeaa103
NF
844 unsigned int reg_width, size_t period_len,
845 enum dma_transfer_direction direction)
53830cc7 846{
beeaa103 847 struct at_dma_chan *atchan = to_at_dma_chan(chan);
beeaa103
NF
848 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
849 u32 ctrla;
53830cc7
NF
850
851 /* prepare common CRTLA value */
1dd1ea8e
NF
852 ctrla = ATC_SCSIZE(sconfig->src_maxburst)
853 | ATC_DCSIZE(sconfig->dst_maxburst)
53830cc7
NF
854 | ATC_DST_WIDTH(reg_width)
855 | ATC_SRC_WIDTH(reg_width)
856 | period_len >> reg_width;
857
858 switch (direction) {
db8196df 859 case DMA_MEM_TO_DEV:
53830cc7 860 desc->lli.saddr = buf_addr + (period_len * period_index);
beeaa103 861 desc->lli.daddr = sconfig->dst_addr;
53830cc7 862 desc->lli.ctrla = ctrla;
ae14d4b5 863 desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
53830cc7 864 | ATC_SRC_ADDR_MODE_INCR
ae14d4b5 865 | ATC_FC_MEM2PER
bbe89c8e
LD
866 | ATC_SIF(atchan->mem_if)
867 | ATC_DIF(atchan->per_if);
53830cc7
NF
868 break;
869
db8196df 870 case DMA_DEV_TO_MEM:
beeaa103 871 desc->lli.saddr = sconfig->src_addr;
53830cc7
NF
872 desc->lli.daddr = buf_addr + (period_len * period_index);
873 desc->lli.ctrla = ctrla;
ae14d4b5 874 desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
53830cc7 875 | ATC_SRC_ADDR_MODE_FIXED
ae14d4b5 876 | ATC_FC_PER2MEM
bbe89c8e
LD
877 | ATC_SIF(atchan->per_if)
878 | ATC_DIF(atchan->mem_if);
53830cc7
NF
879 break;
880
881 default:
882 return -EINVAL;
883 }
884
885 return 0;
886}
887
888/**
889 * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
890 * @chan: the DMA channel to prepare
891 * @buf_addr: physical DMA address where the buffer starts
892 * @buf_len: total number of bytes for the entire buffer
893 * @period_len: number of bytes for each period
894 * @direction: transfer direction, to or from device
ec8b5e48 895 * @flags: tx descriptor status flags
53830cc7
NF
896 */
897static struct dma_async_tx_descriptor *
898atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
185ecb5f 899 size_t period_len, enum dma_transfer_direction direction,
31c1e5a1 900 unsigned long flags)
53830cc7
NF
901{
902 struct at_dma_chan *atchan = to_at_dma_chan(chan);
903 struct at_dma_slave *atslave = chan->private;
beeaa103 904 struct dma_slave_config *sconfig = &atchan->dma_sconfig;
53830cc7
NF
905 struct at_desc *first = NULL;
906 struct at_desc *prev = NULL;
907 unsigned long was_cyclic;
beeaa103 908 unsigned int reg_width;
53830cc7
NF
909 unsigned int periods = buf_len / period_len;
910 unsigned int i;
911
912 dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
db8196df 913 direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
53830cc7
NF
914 buf_addr,
915 periods, buf_len, period_len);
916
917 if (unlikely(!atslave || !buf_len || !period_len)) {
918 dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
919 return NULL;
920 }
921
922 was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
923 if (was_cyclic) {
924 dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
925 return NULL;
926 }
927
0e7264cc
AS
928 if (unlikely(!is_slave_direction(direction)))
929 goto err_out;
930
beeaa103
NF
931 if (sconfig->direction == DMA_MEM_TO_DEV)
932 reg_width = convert_buswidth(sconfig->dst_addr_width);
933 else
934 reg_width = convert_buswidth(sconfig->src_addr_width);
935
53830cc7 936 /* Check for too big/unaligned periods and unaligned DMA buffer */
0e7264cc 937 if (atc_dma_cyclic_check_values(reg_width, buf_addr, period_len))
53830cc7
NF
938 goto err_out;
939
940 /* build cyclic linked list */
941 for (i = 0; i < periods; i++) {
942 struct at_desc *desc;
943
944 desc = atc_desc_get(atchan);
945 if (!desc)
946 goto err_desc_get;
947
beeaa103
NF
948 if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
949 reg_width, period_len, direction))
53830cc7
NF
950 goto err_desc_get;
951
952 atc_desc_chain(&first, &prev, desc);
953 }
954
955 /* lets make a cyclic list */
956 prev->lli.dscr = first->txd.phys;
957
958 /* First descriptor of the chain embedds additional information */
959 first->txd.cookie = -EBUSY;
960 first->len = buf_len;
d088c33b 961 first->tx_width = reg_width;
53830cc7
NF
962
963 return &first->txd;
964
965err_desc_get:
966 dev_err(chan2dev(chan), "not enough descriptors available\n");
967 atc_desc_put(atchan, first);
968err_out:
969 clear_bit(ATC_IS_CYCLIC, &atchan->status);
970 return NULL;
971}
972
beeaa103
NF
973static int set_runtime_config(struct dma_chan *chan,
974 struct dma_slave_config *sconfig)
975{
976 struct at_dma_chan *atchan = to_at_dma_chan(chan);
977
978 /* Check if it is chan is configured for slave transfers */
979 if (!chan->private)
980 return -EINVAL;
981
982 memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
983
984 convert_burst(&atchan->dma_sconfig.src_maxburst);
985 convert_burst(&atchan->dma_sconfig.dst_maxburst);
986
987 return 0;
988}
989
53830cc7 990
05827630
LW
991static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
992 unsigned long arg)
808347f6
NF
993{
994 struct at_dma_chan *atchan = to_at_dma_chan(chan);
995 struct at_dma *atdma = to_at_dma(chan->device);
23b5e3ad 996 int chan_id = atchan->chan_common.chan_id;
d8cb04b0 997 unsigned long flags;
23b5e3ad 998
808347f6
NF
999 LIST_HEAD(list);
1000
23b5e3ad 1001 dev_vdbg(chan2dev(chan), "atc_control (%d)\n", cmd);
c3635c78 1002
23b5e3ad 1003 if (cmd == DMA_PAUSE) {
d8cb04b0 1004 spin_lock_irqsave(&atchan->lock, flags);
808347f6 1005
23b5e3ad 1006 dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
23b5e3ad 1007 set_bit(ATC_IS_PAUSED, &atchan->status);
808347f6 1008
d8cb04b0 1009 spin_unlock_irqrestore(&atchan->lock, flags);
23b5e3ad 1010 } else if (cmd == DMA_RESUME) {
3c477482 1011 if (!atc_chan_is_paused(atchan))
23b5e3ad 1012 return 0;
808347f6 1013
d8cb04b0 1014 spin_lock_irqsave(&atchan->lock, flags);
808347f6 1015
23b5e3ad
NF
1016 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
1017 clear_bit(ATC_IS_PAUSED, &atchan->status);
c3635c78 1018
d8cb04b0 1019 spin_unlock_irqrestore(&atchan->lock, flags);
23b5e3ad
NF
1020 } else if (cmd == DMA_TERMINATE_ALL) {
1021 struct at_desc *desc, *_desc;
1022 /*
1023 * This is only called when something went wrong elsewhere, so
1024 * we don't really care about the data. Just disable the
1025 * channel. We still have to poll the channel enable bit due
1026 * to AHB/HSB limitations.
1027 */
d8cb04b0 1028 spin_lock_irqsave(&atchan->lock, flags);
23b5e3ad
NF
1029
1030 /* disabling channel: must also remove suspend state */
1031 dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
1032
1033 /* confirm that this channel is disabled */
1034 while (dma_readl(atdma, CHSR) & atchan->mask)
1035 cpu_relax();
1036
1037 /* active_list entries will end up before queued entries */
1038 list_splice_init(&atchan->queue, &list);
1039 list_splice_init(&atchan->active_list, &list);
1040
1041 /* Flush all pending and queued descriptors */
1042 list_for_each_entry_safe(desc, _desc, &list, desc_node)
1043 atc_chain_complete(atchan, desc);
1044
1045 clear_bit(ATC_IS_PAUSED, &atchan->status);
1046 /* if channel dedicated to cyclic operations, free it */
1047 clear_bit(ATC_IS_CYCLIC, &atchan->status);
1048
d8cb04b0 1049 spin_unlock_irqrestore(&atchan->lock, flags);
beeaa103
NF
1050 } else if (cmd == DMA_SLAVE_CONFIG) {
1051 return set_runtime_config(chan, (struct dma_slave_config *)arg);
23b5e3ad
NF
1052 } else {
1053 return -ENXIO;
1054 }
b0ebeb9c 1055
c3635c78 1056 return 0;
808347f6
NF
1057}
1058
dc78baa2 1059/**
07934481 1060 * atc_tx_status - poll for transaction completion
dc78baa2
NF
1061 * @chan: DMA channel
1062 * @cookie: transaction identifier to check status of
07934481 1063 * @txstate: if not %NULL updated with transaction state
dc78baa2 1064 *
07934481 1065 * If @txstate is passed in, upon return it reflect the driver
dc78baa2
NF
1066 * internal state and can be used with dma_async_is_complete() to check
1067 * the status of multiple cookies without re-checking hardware state.
1068 */
1069static enum dma_status
07934481 1070atc_tx_status(struct dma_chan *chan,
dc78baa2 1071 dma_cookie_t cookie,
07934481 1072 struct dma_tx_state *txstate)
dc78baa2
NF
1073{
1074 struct at_dma_chan *atchan = to_at_dma_chan(chan);
d8cb04b0 1075 unsigned long flags;
dc78baa2 1076 enum dma_status ret;
d48de6f1 1077 int bytes = 0;
dc78baa2 1078
96a2af41 1079 ret = dma_cookie_status(chan, cookie, txstate);
6d203d1e 1080 if (ret == DMA_COMPLETE)
d48de6f1
ES
1081 return ret;
1082 /*
1083 * There's no point calculating the residue if there's
1084 * no txstate to store the value.
1085 */
1086 if (!txstate)
1087 return DMA_ERROR;
dc78baa2 1088
d48de6f1 1089 spin_lock_irqsave(&atchan->lock, flags);
dc78baa2 1090
d48de6f1
ES
1091 /* Get number of bytes left in the active transactions */
1092 bytes = atc_get_bytes_left(chan);
96a2af41 1093
d8cb04b0 1094 spin_unlock_irqrestore(&atchan->lock, flags);
dc78baa2 1095
d48de6f1
ES
1096 if (unlikely(bytes < 0)) {
1097 dev_vdbg(chan2dev(chan), "get residual bytes error\n");
1098 return DMA_ERROR;
c3dbc60c 1099 } else {
d48de6f1 1100 dma_set_residue(txstate, bytes);
c3dbc60c 1101 }
23b5e3ad 1102
d48de6f1
ES
1103 dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d residue = %d\n",
1104 ret, cookie, bytes);
dc78baa2
NF
1105
1106 return ret;
1107}
1108
1109/**
1110 * atc_issue_pending - try to finish work
1111 * @chan: target DMA channel
1112 */
1113static void atc_issue_pending(struct dma_chan *chan)
1114{
1115 struct at_dma_chan *atchan = to_at_dma_chan(chan);
d8cb04b0 1116 unsigned long flags;
dc78baa2
NF
1117
1118 dev_vdbg(chan2dev(chan), "issue_pending\n");
1119
53830cc7 1120 /* Not needed for cyclic transfers */
3c477482 1121 if (atc_chan_is_cyclic(atchan))
53830cc7
NF
1122 return;
1123
d8cb04b0 1124 spin_lock_irqsave(&atchan->lock, flags);
d202f051 1125 atc_advance_work(atchan);
d8cb04b0 1126 spin_unlock_irqrestore(&atchan->lock, flags);
dc78baa2
NF
1127}
1128
1129/**
1130 * atc_alloc_chan_resources - allocate resources for DMA channel
1131 * @chan: allocate descriptor resources for this channel
1132 * @client: current client requesting the channel be ready for requests
1133 *
1134 * return - the number of allocated descriptors
1135 */
1136static int atc_alloc_chan_resources(struct dma_chan *chan)
1137{
1138 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1139 struct at_dma *atdma = to_at_dma(chan->device);
1140 struct at_desc *desc;
808347f6 1141 struct at_dma_slave *atslave;
d8cb04b0 1142 unsigned long flags;
dc78baa2 1143 int i;
808347f6 1144 u32 cfg;
dc78baa2
NF
1145 LIST_HEAD(tmp_list);
1146
1147 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
1148
1149 /* ASSERT: channel is idle */
1150 if (atc_chan_is_enabled(atchan)) {
1151 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
1152 return -EIO;
1153 }
1154
808347f6
NF
1155 cfg = ATC_DEFAULT_CFG;
1156
1157 atslave = chan->private;
1158 if (atslave) {
1159 /*
1160 * We need controller-specific data to set up slave
1161 * transfers.
1162 */
1163 BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
1164
ea7e7906 1165 /* if cfg configuration specified take it instead of default */
808347f6
NF
1166 if (atslave->cfg)
1167 cfg = atslave->cfg;
1168 }
1169
1170 /* have we already been set up?
1171 * reconfigure channel but no need to reallocate descriptors */
dc78baa2
NF
1172 if (!list_empty(&atchan->free_list))
1173 return atchan->descs_allocated;
1174
1175 /* Allocate initial pool of descriptors */
1176 for (i = 0; i < init_nr_desc_per_channel; i++) {
1177 desc = atc_alloc_descriptor(chan, GFP_KERNEL);
1178 if (!desc) {
1179 dev_err(atdma->dma_common.dev,
1180 "Only %d initial descriptors\n", i);
1181 break;
1182 }
1183 list_add_tail(&desc->desc_node, &tmp_list);
1184 }
1185
d8cb04b0 1186 spin_lock_irqsave(&atchan->lock, flags);
dc78baa2 1187 atchan->descs_allocated = i;
d48de6f1 1188 atchan->remain_desc = 0;
dc78baa2 1189 list_splice(&tmp_list, &atchan->free_list);
d3ee98cd 1190 dma_cookie_init(chan);
d8cb04b0 1191 spin_unlock_irqrestore(&atchan->lock, flags);
dc78baa2
NF
1192
1193 /* channel parameters */
808347f6 1194 channel_writel(atchan, CFG, cfg);
dc78baa2
NF
1195
1196 dev_dbg(chan2dev(chan),
1197 "alloc_chan_resources: allocated %d descriptors\n",
1198 atchan->descs_allocated);
1199
1200 return atchan->descs_allocated;
1201}
1202
1203/**
1204 * atc_free_chan_resources - free all channel resources
1205 * @chan: DMA channel
1206 */
1207static void atc_free_chan_resources(struct dma_chan *chan)
1208{
1209 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1210 struct at_dma *atdma = to_at_dma(chan->device);
1211 struct at_desc *desc, *_desc;
1212 LIST_HEAD(list);
1213
1214 dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
1215 atchan->descs_allocated);
1216
1217 /* ASSERT: channel is idle */
1218 BUG_ON(!list_empty(&atchan->active_list));
1219 BUG_ON(!list_empty(&atchan->queue));
1220 BUG_ON(atc_chan_is_enabled(atchan));
1221
1222 list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
1223 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1224 list_del(&desc->desc_node);
1225 /* free link descriptor */
1226 dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
1227 }
1228 list_splice_init(&atchan->free_list, &list);
1229 atchan->descs_allocated = 0;
53830cc7 1230 atchan->status = 0;
d48de6f1 1231 atchan->remain_desc = 0;
dc78baa2
NF
1232
1233 dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
1234}
1235
bbe89c8e
LD
1236#ifdef CONFIG_OF
1237static bool at_dma_filter(struct dma_chan *chan, void *slave)
1238{
1239 struct at_dma_slave *atslave = slave;
1240
1241 if (atslave->dma_dev == chan->device->dev) {
1242 chan->private = atslave;
1243 return true;
1244 } else {
1245 return false;
1246 }
1247}
1248
1249static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
1250 struct of_dma *of_dma)
1251{
1252 struct dma_chan *chan;
1253 struct at_dma_chan *atchan;
1254 struct at_dma_slave *atslave;
1255 dma_cap_mask_t mask;
1256 unsigned int per_id;
1257 struct platform_device *dmac_pdev;
1258
1259 if (dma_spec->args_count != 2)
1260 return NULL;
1261
1262 dmac_pdev = of_find_device_by_node(dma_spec->np);
1263
1264 dma_cap_zero(mask);
1265 dma_cap_set(DMA_SLAVE, mask);
1266
1267 atslave = devm_kzalloc(&dmac_pdev->dev, sizeof(*atslave), GFP_KERNEL);
1268 if (!atslave)
1269 return NULL;
62971b29
LD
1270
1271 atslave->cfg = ATC_DST_H2SEL_HW | ATC_SRC_H2SEL_HW;
bbe89c8e
LD
1272 /*
1273 * We can fill both SRC_PER and DST_PER, one of these fields will be
1274 * ignored depending on DMA transfer direction.
1275 */
62971b29
LD
1276 per_id = dma_spec->args[1] & AT91_DMA_CFG_PER_ID_MASK;
1277 atslave->cfg |= ATC_DST_PER_MSB(per_id) | ATC_DST_PER(per_id)
6c22770f 1278 | ATC_SRC_PER_MSB(per_id) | ATC_SRC_PER(per_id);
62971b29
LD
1279 /*
1280 * We have to translate the value we get from the device tree since
1281 * the half FIFO configuration value had to be 0 to keep backward
1282 * compatibility.
1283 */
1284 switch (dma_spec->args[1] & AT91_DMA_CFG_FIFOCFG_MASK) {
1285 case AT91_DMA_CFG_FIFOCFG_ALAP:
1286 atslave->cfg |= ATC_FIFOCFG_LARGESTBURST;
1287 break;
1288 case AT91_DMA_CFG_FIFOCFG_ASAP:
1289 atslave->cfg |= ATC_FIFOCFG_ENOUGHSPACE;
1290 break;
1291 case AT91_DMA_CFG_FIFOCFG_HALF:
1292 default:
1293 atslave->cfg |= ATC_FIFOCFG_HALFFIFO;
1294 }
bbe89c8e
LD
1295 atslave->dma_dev = &dmac_pdev->dev;
1296
1297 chan = dma_request_channel(mask, at_dma_filter, atslave);
1298 if (!chan)
1299 return NULL;
1300
1301 atchan = to_at_dma_chan(chan);
1302 atchan->per_if = dma_spec->args[0] & 0xff;
1303 atchan->mem_if = (dma_spec->args[0] >> 16) & 0xff;
1304
1305 return chan;
1306}
1307#else
1308static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
1309 struct of_dma *of_dma)
1310{
1311 return NULL;
1312}
1313#endif
dc78baa2
NF
1314
1315/*-- Module Management -----------------------------------------------*/
1316
02f88be9
NF
1317/* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
1318static struct at_dma_platform_data at91sam9rl_config = {
1319 .nr_channels = 2,
1320};
1321static struct at_dma_platform_data at91sam9g45_config = {
1322 .nr_channels = 8,
1323};
1324
c5115953
NF
1325#if defined(CONFIG_OF)
1326static const struct of_device_id atmel_dma_dt_ids[] = {
1327 {
1328 .compatible = "atmel,at91sam9rl-dma",
02f88be9 1329 .data = &at91sam9rl_config,
c5115953
NF
1330 }, {
1331 .compatible = "atmel,at91sam9g45-dma",
02f88be9 1332 .data = &at91sam9g45_config,
dcc81734
NF
1333 }, {
1334 /* sentinel */
1335 }
c5115953
NF
1336};
1337
1338MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
1339#endif
1340
0ab88a01 1341static const struct platform_device_id atdma_devtypes[] = {
67348450
NF
1342 {
1343 .name = "at91sam9rl_dma",
02f88be9 1344 .driver_data = (unsigned long) &at91sam9rl_config,
67348450
NF
1345 }, {
1346 .name = "at91sam9g45_dma",
02f88be9 1347 .driver_data = (unsigned long) &at91sam9g45_config,
67348450
NF
1348 }, {
1349 /* sentinel */
1350 }
1351};
1352
7fd63ccd 1353static inline const struct at_dma_platform_data * __init at_dma_get_driver_data(
02f88be9 1354 struct platform_device *pdev)
c5115953
NF
1355{
1356 if (pdev->dev.of_node) {
1357 const struct of_device_id *match;
1358 match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
1359 if (match == NULL)
02f88be9
NF
1360 return NULL;
1361 return match->data;
c5115953 1362 }
02f88be9
NF
1363 return (struct at_dma_platform_data *)
1364 platform_get_device_id(pdev)->driver_data;
c5115953
NF
1365}
1366
dc78baa2
NF
1367/**
1368 * at_dma_off - disable DMA controller
1369 * @atdma: the Atmel HDAMC device
1370 */
1371static void at_dma_off(struct at_dma *atdma)
1372{
1373 dma_writel(atdma, EN, 0);
1374
1375 /* disable all interrupts */
1376 dma_writel(atdma, EBCIDR, -1L);
1377
1378 /* confirm that all channels are disabled */
1379 while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
1380 cpu_relax();
1381}
1382
1383static int __init at_dma_probe(struct platform_device *pdev)
1384{
dc78baa2
NF
1385 struct resource *io;
1386 struct at_dma *atdma;
1387 size_t size;
1388 int irq;
1389 int err;
1390 int i;
7fd63ccd 1391 const struct at_dma_platform_data *plat_dat;
67348450 1392
02f88be9
NF
1393 /* setup platform data for each SoC */
1394 dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
1395 dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
1396 dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
67348450
NF
1397
1398 /* get DMA parameters from controller type */
02f88be9
NF
1399 plat_dat = at_dma_get_driver_data(pdev);
1400 if (!plat_dat)
1401 return -ENODEV;
dc78baa2
NF
1402
1403 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1404 if (!io)
1405 return -EINVAL;
1406
1407 irq = platform_get_irq(pdev, 0);
1408 if (irq < 0)
1409 return irq;
1410
1411 size = sizeof(struct at_dma);
02f88be9 1412 size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
dc78baa2
NF
1413 atdma = kzalloc(size, GFP_KERNEL);
1414 if (!atdma)
1415 return -ENOMEM;
1416
67348450 1417 /* discover transaction capabilities */
02f88be9
NF
1418 atdma->dma_common.cap_mask = plat_dat->cap_mask;
1419 atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
dc78baa2 1420
114df7d6 1421 size = resource_size(io);
dc78baa2
NF
1422 if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1423 err = -EBUSY;
1424 goto err_kfree;
1425 }
1426
1427 atdma->regs = ioremap(io->start, size);
1428 if (!atdma->regs) {
1429 err = -ENOMEM;
1430 goto err_release_r;
1431 }
1432
1433 atdma->clk = clk_get(&pdev->dev, "dma_clk");
1434 if (IS_ERR(atdma->clk)) {
1435 err = PTR_ERR(atdma->clk);
1436 goto err_clk;
1437 }
f784d9c9
BB
1438 err = clk_prepare_enable(atdma->clk);
1439 if (err)
1440 goto err_clk_prepare;
dc78baa2
NF
1441
1442 /* force dma off, just in case */
1443 at_dma_off(atdma);
1444
1445 err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1446 if (err)
1447 goto err_irq;
1448
1449 platform_set_drvdata(pdev, atdma);
1450
1451 /* create a pool of consistent memory blocks for hardware descriptors */
1452 atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1453 &pdev->dev, sizeof(struct at_desc),
1454 4 /* word alignment */, 0);
1455 if (!atdma->dma_desc_pool) {
1456 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1457 err = -ENOMEM;
1458 goto err_pool_create;
1459 }
1460
1461 /* clear any pending interrupt */
1462 while (dma_readl(atdma, EBCISR))
1463 cpu_relax();
1464
1465 /* initialize channels related values */
1466 INIT_LIST_HEAD(&atdma->dma_common.channels);
02f88be9 1467 for (i = 0; i < plat_dat->nr_channels; i++) {
dc78baa2
NF
1468 struct at_dma_chan *atchan = &atdma->chan[i];
1469
bbe89c8e
LD
1470 atchan->mem_if = AT_DMA_MEM_IF;
1471 atchan->per_if = AT_DMA_PER_IF;
dc78baa2 1472 atchan->chan_common.device = &atdma->dma_common;
d3ee98cd 1473 dma_cookie_init(&atchan->chan_common);
dc78baa2
NF
1474 list_add_tail(&atchan->chan_common.device_node,
1475 &atdma->dma_common.channels);
1476
1477 atchan->ch_regs = atdma->regs + ch_regs(i);
1478 spin_lock_init(&atchan->lock);
1479 atchan->mask = 1 << i;
1480
1481 INIT_LIST_HEAD(&atchan->active_list);
1482 INIT_LIST_HEAD(&atchan->queue);
1483 INIT_LIST_HEAD(&atchan->free_list);
1484
1485 tasklet_init(&atchan->tasklet, atc_tasklet,
1486 (unsigned long)atchan);
bda3a47c 1487 atc_enable_chan_irq(atdma, i);
dc78baa2
NF
1488 }
1489
1490 /* set base routines */
1491 atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1492 atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
07934481 1493 atdma->dma_common.device_tx_status = atc_tx_status;
dc78baa2
NF
1494 atdma->dma_common.device_issue_pending = atc_issue_pending;
1495 atdma->dma_common.dev = &pdev->dev;
1496
1497 /* set prep routines based on capability */
1498 if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1499 atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1500
d7db8080 1501 if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
808347f6 1502 atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
d7db8080
NF
1503 /* controller can do slave DMA: can trigger cyclic transfers */
1504 dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
53830cc7 1505 atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
c3635c78 1506 atdma->dma_common.device_control = atc_control;
d7db8080 1507 }
808347f6 1508
dc78baa2
NF
1509 dma_writel(atdma, EN, AT_DMA_ENABLE);
1510
1511 dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
1512 dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1513 dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
02f88be9 1514 plat_dat->nr_channels);
dc78baa2
NF
1515
1516 dma_async_device_register(&atdma->dma_common);
1517
bbe89c8e
LD
1518 /*
1519 * Do not return an error if the dmac node is not present in order to
1520 * not break the existing way of requesting channel with
1521 * dma_request_channel().
1522 */
1523 if (pdev->dev.of_node) {
1524 err = of_dma_controller_register(pdev->dev.of_node,
1525 at_dma_xlate, atdma);
1526 if (err) {
1527 dev_err(&pdev->dev, "could not register of_dma_controller\n");
1528 goto err_of_dma_controller_register;
1529 }
1530 }
1531
dc78baa2
NF
1532 return 0;
1533
bbe89c8e
LD
1534err_of_dma_controller_register:
1535 dma_async_device_unregister(&atdma->dma_common);
1536 dma_pool_destroy(atdma->dma_desc_pool);
dc78baa2 1537err_pool_create:
dc78baa2
NF
1538 free_irq(platform_get_irq(pdev, 0), atdma);
1539err_irq:
f784d9c9
BB
1540 clk_disable_unprepare(atdma->clk);
1541err_clk_prepare:
dc78baa2
NF
1542 clk_put(atdma->clk);
1543err_clk:
1544 iounmap(atdma->regs);
1545 atdma->regs = NULL;
1546err_release_r:
1547 release_mem_region(io->start, size);
1548err_kfree:
1549 kfree(atdma);
1550 return err;
1551}
1552
1d1bbd30 1553static int at_dma_remove(struct platform_device *pdev)
dc78baa2
NF
1554{
1555 struct at_dma *atdma = platform_get_drvdata(pdev);
1556 struct dma_chan *chan, *_chan;
1557 struct resource *io;
1558
1559 at_dma_off(atdma);
1560 dma_async_device_unregister(&atdma->dma_common);
1561
1562 dma_pool_destroy(atdma->dma_desc_pool);
dc78baa2
NF
1563 free_irq(platform_get_irq(pdev, 0), atdma);
1564
1565 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1566 device_node) {
1567 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1568
1569 /* Disable interrupts */
bda3a47c 1570 atc_disable_chan_irq(atdma, chan->chan_id);
dc78baa2
NF
1571
1572 tasklet_kill(&atchan->tasklet);
1573 list_del(&chan->device_node);
1574 }
1575
f784d9c9 1576 clk_disable_unprepare(atdma->clk);
dc78baa2
NF
1577 clk_put(atdma->clk);
1578
1579 iounmap(atdma->regs);
1580 atdma->regs = NULL;
1581
1582 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
114df7d6 1583 release_mem_region(io->start, resource_size(io));
dc78baa2
NF
1584
1585 kfree(atdma);
1586
1587 return 0;
1588}
1589
1590static void at_dma_shutdown(struct platform_device *pdev)
1591{
1592 struct at_dma *atdma = platform_get_drvdata(pdev);
1593
1594 at_dma_off(platform_get_drvdata(pdev));
f784d9c9 1595 clk_disable_unprepare(atdma->clk);
dc78baa2
NF
1596}
1597
c0ba5947
NF
1598static int at_dma_prepare(struct device *dev)
1599{
1600 struct platform_device *pdev = to_platform_device(dev);
1601 struct at_dma *atdma = platform_get_drvdata(pdev);
1602 struct dma_chan *chan, *_chan;
1603
1604 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1605 device_node) {
1606 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1607 /* wait for transaction completion (except in cyclic case) */
3c477482 1608 if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
c0ba5947
NF
1609 return -EAGAIN;
1610 }
1611 return 0;
1612}
1613
1614static void atc_suspend_cyclic(struct at_dma_chan *atchan)
1615{
1616 struct dma_chan *chan = &atchan->chan_common;
1617
1618 /* Channel should be paused by user
1619 * do it anyway even if it is not done already */
3c477482 1620 if (!atc_chan_is_paused(atchan)) {
c0ba5947
NF
1621 dev_warn(chan2dev(chan),
1622 "cyclic channel not paused, should be done by channel user\n");
1623 atc_control(chan, DMA_PAUSE, 0);
1624 }
1625
1626 /* now preserve additional data for cyclic operations */
1627 /* next descriptor address in the cyclic list */
1628 atchan->save_dscr = channel_readl(atchan, DSCR);
1629
1630 vdbg_dump_regs(atchan);
1631}
1632
33f82d14 1633static int at_dma_suspend_noirq(struct device *dev)
dc78baa2 1634{
33f82d14
DW
1635 struct platform_device *pdev = to_platform_device(dev);
1636 struct at_dma *atdma = platform_get_drvdata(pdev);
c0ba5947 1637 struct dma_chan *chan, *_chan;
dc78baa2 1638
c0ba5947
NF
1639 /* preserve data */
1640 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1641 device_node) {
1642 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1643
3c477482 1644 if (atc_chan_is_cyclic(atchan))
c0ba5947
NF
1645 atc_suspend_cyclic(atchan);
1646 atchan->save_cfg = channel_readl(atchan, CFG);
1647 }
1648 atdma->save_imr = dma_readl(atdma, EBCIMR);
1649
1650 /* disable DMA controller */
1651 at_dma_off(atdma);
f784d9c9 1652 clk_disable_unprepare(atdma->clk);
dc78baa2
NF
1653 return 0;
1654}
1655
c0ba5947
NF
1656static void atc_resume_cyclic(struct at_dma_chan *atchan)
1657{
1658 struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
1659
1660 /* restore channel status for cyclic descriptors list:
1661 * next descriptor in the cyclic list at the time of suspend */
1662 channel_writel(atchan, SADDR, 0);
1663 channel_writel(atchan, DADDR, 0);
1664 channel_writel(atchan, CTRLA, 0);
1665 channel_writel(atchan, CTRLB, 0);
1666 channel_writel(atchan, DSCR, atchan->save_dscr);
1667 dma_writel(atdma, CHER, atchan->mask);
1668
1669 /* channel pause status should be removed by channel user
1670 * We cannot take the initiative to do it here */
1671
1672 vdbg_dump_regs(atchan);
1673}
1674
33f82d14 1675static int at_dma_resume_noirq(struct device *dev)
dc78baa2 1676{
33f82d14
DW
1677 struct platform_device *pdev = to_platform_device(dev);
1678 struct at_dma *atdma = platform_get_drvdata(pdev);
c0ba5947 1679 struct dma_chan *chan, *_chan;
dc78baa2 1680
c0ba5947 1681 /* bring back DMA controller */
f784d9c9 1682 clk_prepare_enable(atdma->clk);
dc78baa2 1683 dma_writel(atdma, EN, AT_DMA_ENABLE);
c0ba5947
NF
1684
1685 /* clear any pending interrupt */
1686 while (dma_readl(atdma, EBCISR))
1687 cpu_relax();
1688
1689 /* restore saved data */
1690 dma_writel(atdma, EBCIER, atdma->save_imr);
1691 list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1692 device_node) {
1693 struct at_dma_chan *atchan = to_at_dma_chan(chan);
1694
1695 channel_writel(atchan, CFG, atchan->save_cfg);
3c477482 1696 if (atc_chan_is_cyclic(atchan))
c0ba5947
NF
1697 atc_resume_cyclic(atchan);
1698 }
dc78baa2 1699 return 0;
dc78baa2
NF
1700}
1701
47145210 1702static const struct dev_pm_ops at_dma_dev_pm_ops = {
c0ba5947 1703 .prepare = at_dma_prepare,
33f82d14
DW
1704 .suspend_noirq = at_dma_suspend_noirq,
1705 .resume_noirq = at_dma_resume_noirq,
1706};
1707
dc78baa2 1708static struct platform_driver at_dma_driver = {
1d1bbd30 1709 .remove = at_dma_remove,
dc78baa2 1710 .shutdown = at_dma_shutdown,
67348450 1711 .id_table = atdma_devtypes,
dc78baa2
NF
1712 .driver = {
1713 .name = "at_hdmac",
33f82d14 1714 .pm = &at_dma_dev_pm_ops,
c5115953 1715 .of_match_table = of_match_ptr(atmel_dma_dt_ids),
dc78baa2
NF
1716 },
1717};
1718
1719static int __init at_dma_init(void)
1720{
1721 return platform_driver_probe(&at_dma_driver, at_dma_probe);
1722}
93d0bec2 1723subsys_initcall(at_dma_init);
dc78baa2
NF
1724
1725static void __exit at_dma_exit(void)
1726{
1727 platform_driver_unregister(&at_dma_driver);
1728}
1729module_exit(at_dma_exit);
1730
1731MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1732MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1733MODULE_LICENSE("GPL");
1734MODULE_ALIAS("platform:at_hdmac");