Linux 3.18.139
[linux/fpc-iii.git] / drivers / dma / edma.c
blobabff66c1843241e824843b757b04df6b319273a0
1 /*
2 * TI EDMA DMA engine driver
4 * Copyright 2012 Texas Instruments
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/dmaengine.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/of.h>
28 #include <linux/platform_data/edma.h>
30 #include "dmaengine.h"
31 #include "virt-dma.h"
34 * This will go away when the private EDMA API is folded
35 * into this driver and the platform device(s) are
36 * instantiated in the arch code. We can only get away
37 * with this simplification because DA8XX may not be built
38 * in the same kernel image with other DaVinci parts. This
39 * avoids having to sprinkle dmaengine driver platform devices
40 * and data throughout all the existing board files.
42 #ifdef CONFIG_ARCH_DAVINCI_DA8XX
43 #define EDMA_CTLRS 2
44 #define EDMA_CHANS 32
45 #else
46 #define EDMA_CTLRS 1
47 #define EDMA_CHANS 64
48 #endif /* CONFIG_ARCH_DAVINCI_DA8XX */
51 * Max of 20 segments per channel to conserve PaRAM slots
52 * Also note that MAX_NR_SG should be atleast the no.of periods
53 * that are required for ASoC, otherwise DMA prep calls will
54 * fail. Today davinci-pcm is the only user of this driver and
55 * requires atleast 17 slots, so we setup the default to 20.
57 #define MAX_NR_SG 20
58 #define EDMA_MAX_SLOTS MAX_NR_SG
59 #define EDMA_DESCRIPTORS 16
61 struct edma_pset {
62 u32 len;
63 dma_addr_t addr;
64 struct edmacc_param param;
67 struct edma_desc {
68 struct virt_dma_desc vdesc;
69 struct list_head node;
70 enum dma_transfer_direction direction;
71 int cyclic;
72 int absync;
73 int pset_nr;
74 struct edma_chan *echan;
75 int processed;
78 * The following 4 elements are used for residue accounting.
80 * - processed_stat: the number of SG elements we have traversed
81 * so far to cover accounting. This is updated directly to processed
82 * during edma_callback and is always <= processed, because processed
83 * refers to the number of pending transfer (programmed to EDMA
84 * controller), where as processed_stat tracks number of transfers
85 * accounted for so far.
87 * - residue: The amount of bytes we have left to transfer for this desc
89 * - residue_stat: The residue in bytes of data we have covered
90 * so far for accounting. This is updated directly to residue
91 * during callbacks to keep it current.
93 * - sg_len: Tracks the length of the current intermediate transfer,
94 * this is required to update the residue during intermediate transfer
95 * completion callback.
97 int processed_stat;
98 u32 sg_len;
99 u32 residue;
100 u32 residue_stat;
102 struct edma_pset pset[0];
105 struct edma_cc;
107 struct edma_chan {
108 struct virt_dma_chan vchan;
109 struct list_head node;
110 struct edma_desc *edesc;
111 struct edma_cc *ecc;
112 int ch_num;
113 bool alloced;
114 int slot[EDMA_MAX_SLOTS];
115 int missed;
116 struct dma_slave_config cfg;
119 struct edma_cc {
120 int ctlr;
121 struct dma_device dma_slave;
122 struct edma_chan slave_chans[EDMA_CHANS];
123 int num_slave_chans;
124 int dummy_slot;
127 static inline struct edma_cc *to_edma_cc(struct dma_device *d)
129 return container_of(d, struct edma_cc, dma_slave);
132 static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
134 return container_of(c, struct edma_chan, vchan.chan);
137 static inline struct edma_desc
138 *to_edma_desc(struct dma_async_tx_descriptor *tx)
140 return container_of(tx, struct edma_desc, vdesc.tx);
143 static void edma_desc_free(struct virt_dma_desc *vdesc)
145 kfree(container_of(vdesc, struct edma_desc, vdesc));
148 /* Dispatch a queued descriptor to the controller (caller holds lock) */
149 static void edma_execute(struct edma_chan *echan)
151 struct virt_dma_desc *vdesc;
152 struct edma_desc *edesc;
153 struct device *dev = echan->vchan.chan.device->dev;
154 int i, j, left, nslots;
156 /* If either we processed all psets or we're still not started */
157 if (!echan->edesc ||
158 echan->edesc->pset_nr == echan->edesc->processed) {
159 /* Get next vdesc */
160 vdesc = vchan_next_desc(&echan->vchan);
161 if (!vdesc) {
162 echan->edesc = NULL;
163 return;
165 list_del(&vdesc->node);
166 echan->edesc = to_edma_desc(&vdesc->tx);
169 edesc = echan->edesc;
171 /* Find out how many left */
172 left = edesc->pset_nr - edesc->processed;
173 nslots = min(MAX_NR_SG, left);
174 edesc->sg_len = 0;
176 /* Write descriptor PaRAM set(s) */
177 for (i = 0; i < nslots; i++) {
178 j = i + edesc->processed;
179 edma_write_slot(echan->slot[i], &edesc->pset[j].param);
180 edesc->sg_len += edesc->pset[j].len;
181 dev_vdbg(echan->vchan.chan.device->dev,
182 "\n pset[%d]:\n"
183 " chnum\t%d\n"
184 " slot\t%d\n"
185 " opt\t%08x\n"
186 " src\t%08x\n"
187 " dst\t%08x\n"
188 " abcnt\t%08x\n"
189 " ccnt\t%08x\n"
190 " bidx\t%08x\n"
191 " cidx\t%08x\n"
192 " lkrld\t%08x\n",
193 j, echan->ch_num, echan->slot[i],
194 edesc->pset[j].param.opt,
195 edesc->pset[j].param.src,
196 edesc->pset[j].param.dst,
197 edesc->pset[j].param.a_b_cnt,
198 edesc->pset[j].param.ccnt,
199 edesc->pset[j].param.src_dst_bidx,
200 edesc->pset[j].param.src_dst_cidx,
201 edesc->pset[j].param.link_bcntrld);
202 /* Link to the previous slot if not the last set */
203 if (i != (nslots - 1))
204 edma_link(echan->slot[i], echan->slot[i+1]);
207 edesc->processed += nslots;
210 * If this is either the last set in a set of SG-list transactions
211 * then setup a link to the dummy slot, this results in all future
212 * events being absorbed and that's OK because we're done
214 if (edesc->processed == edesc->pset_nr) {
215 if (edesc->cyclic)
216 edma_link(echan->slot[nslots-1], echan->slot[1]);
217 else
218 edma_link(echan->slot[nslots-1],
219 echan->ecc->dummy_slot);
222 if (edesc->processed <= MAX_NR_SG) {
223 dev_dbg(dev, "first transfer starting on channel %d\n",
224 echan->ch_num);
225 edma_start(echan->ch_num);
226 } else {
227 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
228 echan->ch_num, edesc->processed);
229 edma_resume(echan->ch_num);
233 * This happens due to setup times between intermediate transfers
234 * in long SG lists which have to be broken up into transfers of
235 * MAX_NR_SG
237 if (echan->missed) {
238 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
239 edma_clean_channel(echan->ch_num);
240 edma_stop(echan->ch_num);
241 edma_start(echan->ch_num);
242 edma_trigger_channel(echan->ch_num);
243 echan->missed = 0;
247 static int edma_terminate_all(struct edma_chan *echan)
249 unsigned long flags;
250 LIST_HEAD(head);
252 spin_lock_irqsave(&echan->vchan.lock, flags);
255 * Stop DMA activity: we assume the callback will not be called
256 * after edma_dma() returns (even if it does, it will see
257 * echan->edesc is NULL and exit.)
259 if (echan->edesc) {
260 int cyclic = echan->edesc->cyclic;
263 * free the running request descriptor
264 * since it is not in any of the vdesc lists
266 edma_desc_free(&echan->edesc->vdesc);
268 echan->edesc = NULL;
269 edma_stop(echan->ch_num);
270 /* Move the cyclic channel back to default queue */
271 if (cyclic)
272 edma_assign_channel_eventq(echan->ch_num,
273 EVENTQ_DEFAULT);
276 vchan_get_all_descriptors(&echan->vchan, &head);
277 spin_unlock_irqrestore(&echan->vchan.lock, flags);
278 vchan_dma_desc_free_list(&echan->vchan, &head);
280 return 0;
283 static int edma_slave_config(struct edma_chan *echan,
284 struct dma_slave_config *cfg)
286 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
287 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
288 return -EINVAL;
290 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
292 return 0;
295 static int edma_dma_pause(struct edma_chan *echan)
297 /* Pause/Resume only allowed with cyclic mode */
298 if (!echan->edesc || !echan->edesc->cyclic)
299 return -EINVAL;
301 edma_pause(echan->ch_num);
302 return 0;
305 static int edma_dma_resume(struct edma_chan *echan)
307 /* Pause/Resume only allowed with cyclic mode */
308 if (!echan->edesc->cyclic)
309 return -EINVAL;
311 edma_resume(echan->ch_num);
312 return 0;
315 static int edma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
316 unsigned long arg)
318 int ret = 0;
319 struct dma_slave_config *config;
320 struct edma_chan *echan = to_edma_chan(chan);
322 switch (cmd) {
323 case DMA_TERMINATE_ALL:
324 edma_terminate_all(echan);
325 break;
326 case DMA_SLAVE_CONFIG:
327 config = (struct dma_slave_config *)arg;
328 ret = edma_slave_config(echan, config);
329 break;
330 case DMA_PAUSE:
331 ret = edma_dma_pause(echan);
332 break;
334 case DMA_RESUME:
335 ret = edma_dma_resume(echan);
336 break;
338 default:
339 ret = -ENOSYS;
342 return ret;
346 * A PaRAM set configuration abstraction used by other modes
347 * @chan: Channel who's PaRAM set we're configuring
348 * @pset: PaRAM set to initialize and setup.
349 * @src_addr: Source address of the DMA
350 * @dst_addr: Destination address of the DMA
351 * @burst: In units of dev_width, how much to send
352 * @dev_width: How much is the dev_width
353 * @dma_length: Total length of the DMA transfer
354 * @direction: Direction of the transfer
356 static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
357 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
358 enum dma_slave_buswidth dev_width, unsigned int dma_length,
359 enum dma_transfer_direction direction)
361 struct edma_chan *echan = to_edma_chan(chan);
362 struct device *dev = chan->device->dev;
363 struct edmacc_param *param = &epset->param;
364 int acnt, bcnt, ccnt, cidx;
365 int src_bidx, dst_bidx, src_cidx, dst_cidx;
366 int absync;
368 acnt = dev_width;
370 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
371 if (!burst)
372 burst = 1;
374 * If the maxburst is equal to the fifo width, use
375 * A-synced transfers. This allows for large contiguous
376 * buffer transfers using only one PaRAM set.
378 if (burst == 1) {
380 * For the A-sync case, bcnt and ccnt are the remainder
381 * and quotient respectively of the division of:
382 * (dma_length / acnt) by (SZ_64K -1). This is so
383 * that in case bcnt over flows, we have ccnt to use.
384 * Note: In A-sync tranfer only, bcntrld is used, but it
385 * only applies for sg_dma_len(sg) >= SZ_64K.
386 * In this case, the best way adopted is- bccnt for the
387 * first frame will be the remainder below. Then for
388 * every successive frame, bcnt will be SZ_64K-1. This
389 * is assured as bcntrld = 0xffff in end of function.
391 absync = false;
392 ccnt = dma_length / acnt / (SZ_64K - 1);
393 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
395 * If bcnt is non-zero, we have a remainder and hence an
396 * extra frame to transfer, so increment ccnt.
398 if (bcnt)
399 ccnt++;
400 else
401 bcnt = SZ_64K - 1;
402 cidx = acnt;
403 } else {
405 * If maxburst is greater than the fifo address_width,
406 * use AB-synced transfers where A count is the fifo
407 * address_width and B count is the maxburst. In this
408 * case, we are limited to transfers of C count frames
409 * of (address_width * maxburst) where C count is limited
410 * to SZ_64K-1. This places an upper bound on the length
411 * of an SG segment that can be handled.
413 absync = true;
414 bcnt = burst;
415 ccnt = dma_length / (acnt * bcnt);
416 if (ccnt > (SZ_64K - 1)) {
417 dev_err(dev, "Exceeded max SG segment size\n");
418 return -EINVAL;
420 cidx = acnt * bcnt;
423 epset->len = dma_length;
425 if (direction == DMA_MEM_TO_DEV) {
426 src_bidx = acnt;
427 src_cidx = cidx;
428 dst_bidx = 0;
429 dst_cidx = 0;
430 epset->addr = src_addr;
431 } else if (direction == DMA_DEV_TO_MEM) {
432 src_bidx = 0;
433 src_cidx = 0;
434 dst_bidx = acnt;
435 dst_cidx = cidx;
436 epset->addr = dst_addr;
437 } else if (direction == DMA_MEM_TO_MEM) {
438 src_bidx = acnt;
439 src_cidx = cidx;
440 dst_bidx = acnt;
441 dst_cidx = cidx;
442 } else {
443 dev_err(dev, "%s: direction not implemented yet\n", __func__);
444 return -EINVAL;
447 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
448 /* Configure A or AB synchronized transfers */
449 if (absync)
450 param->opt |= SYNCDIM;
452 param->src = src_addr;
453 param->dst = dst_addr;
455 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
456 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
458 param->a_b_cnt = bcnt << 16 | acnt;
459 param->ccnt = ccnt;
461 * Only time when (bcntrld) auto reload is required is for
462 * A-sync case, and in this case, a requirement of reload value
463 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
464 * and then later will be populated by edma_execute.
466 param->link_bcntrld = 0xffffffff;
467 return absync;
470 static struct dma_async_tx_descriptor *edma_prep_slave_sg(
471 struct dma_chan *chan, struct scatterlist *sgl,
472 unsigned int sg_len, enum dma_transfer_direction direction,
473 unsigned long tx_flags, void *context)
475 struct edma_chan *echan = to_edma_chan(chan);
476 struct device *dev = chan->device->dev;
477 struct edma_desc *edesc;
478 dma_addr_t src_addr = 0, dst_addr = 0;
479 enum dma_slave_buswidth dev_width;
480 u32 burst;
481 struct scatterlist *sg;
482 int i, nslots, ret;
484 if (unlikely(!echan || !sgl || !sg_len))
485 return NULL;
487 if (direction == DMA_DEV_TO_MEM) {
488 src_addr = echan->cfg.src_addr;
489 dev_width = echan->cfg.src_addr_width;
490 burst = echan->cfg.src_maxburst;
491 } else if (direction == DMA_MEM_TO_DEV) {
492 dst_addr = echan->cfg.dst_addr;
493 dev_width = echan->cfg.dst_addr_width;
494 burst = echan->cfg.dst_maxburst;
495 } else {
496 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
497 return NULL;
500 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
501 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
502 return NULL;
505 edesc = kzalloc(sizeof(*edesc) + sg_len *
506 sizeof(edesc->pset[0]), GFP_ATOMIC);
507 if (!edesc) {
508 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
509 return NULL;
512 edesc->pset_nr = sg_len;
513 edesc->residue = 0;
514 edesc->direction = direction;
515 edesc->echan = echan;
517 /* Allocate a PaRAM slot, if needed */
518 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
520 for (i = 0; i < nslots; i++) {
521 if (echan->slot[i] < 0) {
522 echan->slot[i] =
523 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
524 EDMA_SLOT_ANY);
525 if (echan->slot[i] < 0) {
526 kfree(edesc);
527 dev_err(dev, "%s: Failed to allocate slot\n",
528 __func__);
529 return NULL;
534 /* Configure PaRAM sets for each SG */
535 for_each_sg(sgl, sg, sg_len, i) {
536 /* Get address for each SG */
537 if (direction == DMA_DEV_TO_MEM)
538 dst_addr = sg_dma_address(sg);
539 else
540 src_addr = sg_dma_address(sg);
542 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
543 dst_addr, burst, dev_width,
544 sg_dma_len(sg), direction);
545 if (ret < 0) {
546 kfree(edesc);
547 return NULL;
550 edesc->absync = ret;
551 edesc->residue += sg_dma_len(sg);
553 /* If this is the last in a current SG set of transactions,
554 enable interrupts so that next set is processed */
555 if (!((i+1) % MAX_NR_SG))
556 edesc->pset[i].param.opt |= TCINTEN;
558 /* If this is the last set, enable completion interrupt flag */
559 if (i == sg_len - 1)
560 edesc->pset[i].param.opt |= TCINTEN;
562 edesc->residue_stat = edesc->residue;
564 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
567 struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
568 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
569 size_t len, unsigned long tx_flags)
571 int ret;
572 struct edma_desc *edesc;
573 struct device *dev = chan->device->dev;
574 struct edma_chan *echan = to_edma_chan(chan);
576 if (unlikely(!echan || !len))
577 return NULL;
579 edesc = kzalloc(sizeof(*edesc) + sizeof(edesc->pset[0]), GFP_ATOMIC);
580 if (!edesc) {
581 dev_dbg(dev, "Failed to allocate a descriptor\n");
582 return NULL;
585 edesc->pset_nr = 1;
587 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
588 DMA_SLAVE_BUSWIDTH_4_BYTES, len, DMA_MEM_TO_MEM);
589 if (ret < 0)
590 return NULL;
592 edesc->absync = ret;
595 * Enable intermediate transfer chaining to re-trigger channel
596 * on completion of every TR, and enable transfer-completion
597 * interrupt on completion of the whole transfer.
599 edesc->pset[0].param.opt |= ITCCHEN;
600 edesc->pset[0].param.opt |= TCINTEN;
602 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
605 static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
606 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
607 size_t period_len, enum dma_transfer_direction direction,
608 unsigned long tx_flags)
610 struct edma_chan *echan = to_edma_chan(chan);
611 struct device *dev = chan->device->dev;
612 struct edma_desc *edesc;
613 dma_addr_t src_addr, dst_addr;
614 enum dma_slave_buswidth dev_width;
615 u32 burst;
616 int i, ret, nslots;
618 if (unlikely(!echan || !buf_len || !period_len))
619 return NULL;
621 if (direction == DMA_DEV_TO_MEM) {
622 src_addr = echan->cfg.src_addr;
623 dst_addr = buf_addr;
624 dev_width = echan->cfg.src_addr_width;
625 burst = echan->cfg.src_maxburst;
626 } else if (direction == DMA_MEM_TO_DEV) {
627 src_addr = buf_addr;
628 dst_addr = echan->cfg.dst_addr;
629 dev_width = echan->cfg.dst_addr_width;
630 burst = echan->cfg.dst_maxburst;
631 } else {
632 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
633 return NULL;
636 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
637 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
638 return NULL;
641 if (unlikely(buf_len % period_len)) {
642 dev_err(dev, "Period should be multiple of Buffer length\n");
643 return NULL;
646 nslots = (buf_len / period_len) + 1;
649 * Cyclic DMA users such as audio cannot tolerate delays introduced
650 * by cases where the number of periods is more than the maximum
651 * number of SGs the EDMA driver can handle at a time. For DMA types
652 * such as Slave SGs, such delays are tolerable and synchronized,
653 * but the synchronization is difficult to achieve with Cyclic and
654 * cannot be guaranteed, so we error out early.
656 if (nslots > MAX_NR_SG)
657 return NULL;
659 edesc = kzalloc(sizeof(*edesc) + nslots *
660 sizeof(edesc->pset[0]), GFP_ATOMIC);
661 if (!edesc) {
662 dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
663 return NULL;
666 edesc->cyclic = 1;
667 edesc->pset_nr = nslots;
668 edesc->residue = edesc->residue_stat = buf_len;
669 edesc->direction = direction;
670 edesc->echan = echan;
672 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
673 __func__, echan->ch_num, nslots, period_len, buf_len);
675 for (i = 0; i < nslots; i++) {
676 /* Allocate a PaRAM slot, if needed */
677 if (echan->slot[i] < 0) {
678 echan->slot[i] =
679 edma_alloc_slot(EDMA_CTLR(echan->ch_num),
680 EDMA_SLOT_ANY);
681 if (echan->slot[i] < 0) {
682 kfree(edesc);
683 dev_err(dev, "%s: Failed to allocate slot\n",
684 __func__);
685 return NULL;
689 if (i == nslots - 1) {
690 memcpy(&edesc->pset[i], &edesc->pset[0],
691 sizeof(edesc->pset[0]));
692 break;
695 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
696 dst_addr, burst, dev_width, period_len,
697 direction);
698 if (ret < 0) {
699 kfree(edesc);
700 return NULL;
703 if (direction == DMA_DEV_TO_MEM)
704 dst_addr += period_len;
705 else
706 src_addr += period_len;
708 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
709 dev_vdbg(dev,
710 "\n pset[%d]:\n"
711 " chnum\t%d\n"
712 " slot\t%d\n"
713 " opt\t%08x\n"
714 " src\t%08x\n"
715 " dst\t%08x\n"
716 " abcnt\t%08x\n"
717 " ccnt\t%08x\n"
718 " bidx\t%08x\n"
719 " cidx\t%08x\n"
720 " lkrld\t%08x\n",
721 i, echan->ch_num, echan->slot[i],
722 edesc->pset[i].param.opt,
723 edesc->pset[i].param.src,
724 edesc->pset[i].param.dst,
725 edesc->pset[i].param.a_b_cnt,
726 edesc->pset[i].param.ccnt,
727 edesc->pset[i].param.src_dst_bidx,
728 edesc->pset[i].param.src_dst_cidx,
729 edesc->pset[i].param.link_bcntrld);
731 edesc->absync = ret;
734 * Enable period interrupt only if it is requested
736 if (tx_flags & DMA_PREP_INTERRUPT)
737 edesc->pset[i].param.opt |= TCINTEN;
740 /* Place the cyclic channel to highest priority queue */
741 edma_assign_channel_eventq(echan->ch_num, EVENTQ_0);
743 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
746 static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
748 struct edma_chan *echan = data;
749 struct device *dev = echan->vchan.chan.device->dev;
750 struct edma_desc *edesc;
751 struct edmacc_param p;
753 edesc = echan->edesc;
755 /* Pause the channel for non-cyclic */
756 if (!edesc || (edesc && !edesc->cyclic))
757 edma_pause(echan->ch_num);
759 switch (ch_status) {
760 case EDMA_DMA_COMPLETE:
761 spin_lock(&echan->vchan.lock);
763 if (edesc) {
764 if (edesc->cyclic) {
765 vchan_cyclic_callback(&edesc->vdesc);
766 } else if (edesc->processed == edesc->pset_nr) {
767 dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
768 edesc->residue = 0;
769 edma_stop(echan->ch_num);
770 vchan_cookie_complete(&edesc->vdesc);
771 edma_execute(echan);
772 } else {
773 dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
775 /* Update statistics for tx_status */
776 edesc->residue -= edesc->sg_len;
777 edesc->residue_stat = edesc->residue;
778 edesc->processed_stat = edesc->processed;
780 edma_execute(echan);
784 spin_unlock(&echan->vchan.lock);
786 break;
787 case EDMA_DMA_CC_ERROR:
788 spin_lock(&echan->vchan.lock);
790 edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
793 * Issue later based on missed flag which will be sure
794 * to happen as:
795 * (1) we finished transmitting an intermediate slot and
796 * edma_execute is coming up.
797 * (2) or we finished current transfer and issue will
798 * call edma_execute.
800 * Important note: issuing can be dangerous here and
801 * lead to some nasty recursion when we are in a NULL
802 * slot. So we avoid doing so and set the missed flag.
804 if (p.a_b_cnt == 0 && p.ccnt == 0) {
805 dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
806 echan->missed = 1;
807 } else {
809 * The slot is already programmed but the event got
810 * missed, so its safe to issue it here.
812 dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
813 edma_clean_channel(echan->ch_num);
814 edma_stop(echan->ch_num);
815 edma_start(echan->ch_num);
816 edma_trigger_channel(echan->ch_num);
819 spin_unlock(&echan->vchan.lock);
821 break;
822 default:
823 break;
827 /* Alloc channel resources */
828 static int edma_alloc_chan_resources(struct dma_chan *chan)
830 struct edma_chan *echan = to_edma_chan(chan);
831 struct device *dev = chan->device->dev;
832 int ret;
833 int a_ch_num;
834 LIST_HEAD(descs);
836 a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
837 chan, EVENTQ_DEFAULT);
839 if (a_ch_num < 0) {
840 ret = -ENODEV;
841 goto err_no_chan;
844 if (a_ch_num != echan->ch_num) {
845 dev_err(dev, "failed to allocate requested channel %u:%u\n",
846 EDMA_CTLR(echan->ch_num),
847 EDMA_CHAN_SLOT(echan->ch_num));
848 ret = -ENODEV;
849 goto err_wrong_chan;
852 echan->alloced = true;
853 echan->slot[0] = echan->ch_num;
855 dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
856 EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
858 return 0;
860 err_wrong_chan:
861 edma_free_channel(a_ch_num);
862 err_no_chan:
863 return ret;
866 /* Free channel resources */
867 static void edma_free_chan_resources(struct dma_chan *chan)
869 struct edma_chan *echan = to_edma_chan(chan);
870 struct device *dev = chan->device->dev;
871 int i;
873 /* Terminate transfers */
874 edma_stop(echan->ch_num);
876 vchan_free_chan_resources(&echan->vchan);
878 /* Free EDMA PaRAM slots */
879 for (i = 1; i < EDMA_MAX_SLOTS; i++) {
880 if (echan->slot[i] >= 0) {
881 edma_free_slot(echan->slot[i]);
882 echan->slot[i] = -1;
886 /* Free EDMA channel */
887 if (echan->alloced) {
888 edma_free_channel(echan->ch_num);
889 echan->alloced = false;
892 dev_dbg(dev, "freeing channel for %u\n", echan->ch_num);
895 /* Send pending descriptor to hardware */
896 static void edma_issue_pending(struct dma_chan *chan)
898 struct edma_chan *echan = to_edma_chan(chan);
899 unsigned long flags;
901 spin_lock_irqsave(&echan->vchan.lock, flags);
902 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
903 edma_execute(echan);
904 spin_unlock_irqrestore(&echan->vchan.lock, flags);
907 static u32 edma_residue(struct edma_desc *edesc)
909 bool dst = edesc->direction == DMA_DEV_TO_MEM;
910 struct edma_pset *pset = edesc->pset;
911 dma_addr_t done, pos;
912 int i;
915 * We always read the dst/src position from the first RamPar
916 * pset. That's the one which is active now.
918 pos = edma_get_position(edesc->echan->slot[0], dst);
921 * Cyclic is simple. Just subtract pset[0].addr from pos.
923 * We never update edesc->residue in the cyclic case, so we
924 * can tell the remaining room to the end of the circular
925 * buffer.
927 if (edesc->cyclic) {
928 done = pos - pset->addr;
929 edesc->residue_stat = edesc->residue - done;
930 return edesc->residue_stat;
934 * For SG operation we catch up with the last processed
935 * status.
937 pset += edesc->processed_stat;
939 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
941 * If we are inside this pset address range, we know
942 * this is the active one. Get the current delta and
943 * stop walking the psets.
945 if (pos >= pset->addr && pos < pset->addr + pset->len)
946 return edesc->residue_stat - (pos - pset->addr);
948 /* Otherwise mark it done and update residue_stat. */
949 edesc->processed_stat++;
950 edesc->residue_stat -= pset->len;
952 return edesc->residue_stat;
955 /* Check request completion status */
956 static enum dma_status edma_tx_status(struct dma_chan *chan,
957 dma_cookie_t cookie,
958 struct dma_tx_state *txstate)
960 struct edma_chan *echan = to_edma_chan(chan);
961 struct virt_dma_desc *vdesc;
962 enum dma_status ret;
963 unsigned long flags;
965 ret = dma_cookie_status(chan, cookie, txstate);
966 if (ret == DMA_COMPLETE || !txstate)
967 return ret;
969 spin_lock_irqsave(&echan->vchan.lock, flags);
970 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
971 txstate->residue = edma_residue(echan->edesc);
972 else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
973 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
974 spin_unlock_irqrestore(&echan->vchan.lock, flags);
976 return ret;
979 static void __init edma_chan_init(struct edma_cc *ecc,
980 struct dma_device *dma,
981 struct edma_chan *echans)
983 int i, j;
985 for (i = 0; i < EDMA_CHANS; i++) {
986 struct edma_chan *echan = &echans[i];
987 echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
988 echan->ecc = ecc;
989 echan->vchan.desc_free = edma_desc_free;
991 vchan_init(&echan->vchan, dma);
993 INIT_LIST_HEAD(&echan->node);
994 for (j = 0; j < EDMA_MAX_SLOTS; j++)
995 echan->slot[j] = -1;
999 #define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1000 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
1001 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
1002 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1004 static int edma_dma_device_slave_caps(struct dma_chan *dchan,
1005 struct dma_slave_caps *caps)
1007 caps->src_addr_widths = EDMA_DMA_BUSWIDTHS;
1008 caps->dstn_addr_widths = EDMA_DMA_BUSWIDTHS;
1009 caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1010 caps->cmd_pause = true;
1011 caps->cmd_terminate = true;
1012 caps->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1014 return 0;
1017 static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
1018 struct device *dev)
1020 dma->device_prep_slave_sg = edma_prep_slave_sg;
1021 dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
1022 dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
1023 dma->device_alloc_chan_resources = edma_alloc_chan_resources;
1024 dma->device_free_chan_resources = edma_free_chan_resources;
1025 dma->device_issue_pending = edma_issue_pending;
1026 dma->device_tx_status = edma_tx_status;
1027 dma->device_control = edma_control;
1028 dma->device_slave_caps = edma_dma_device_slave_caps;
1029 dma->dev = dev;
1032 * code using dma memcpy must make sure alignment of
1033 * length is at dma->copy_align boundary.
1035 dma->copy_align = DMA_SLAVE_BUSWIDTH_4_BYTES;
1037 INIT_LIST_HEAD(&dma->channels);
1040 static int edma_probe(struct platform_device *pdev)
1042 struct edma_cc *ecc;
1043 int ret;
1045 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1046 if (ret)
1047 return ret;
1049 ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
1050 if (!ecc) {
1051 dev_err(&pdev->dev, "Can't allocate controller\n");
1052 return -ENOMEM;
1055 ecc->ctlr = pdev->id;
1056 ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
1057 if (ecc->dummy_slot < 0) {
1058 dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
1059 return ecc->dummy_slot;
1062 dma_cap_zero(ecc->dma_slave.cap_mask);
1063 dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
1064 dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
1065 dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
1067 edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
1069 edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
1071 ret = dma_async_device_register(&ecc->dma_slave);
1072 if (ret)
1073 goto err_reg1;
1075 platform_set_drvdata(pdev, ecc);
1077 dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
1079 return 0;
1081 err_reg1:
1082 edma_free_slot(ecc->dummy_slot);
1083 return ret;
1086 static int edma_remove(struct platform_device *pdev)
1088 struct device *dev = &pdev->dev;
1089 struct edma_cc *ecc = dev_get_drvdata(dev);
1091 dma_async_device_unregister(&ecc->dma_slave);
1092 edma_free_slot(ecc->dummy_slot);
1094 return 0;
1097 static struct platform_driver edma_driver = {
1098 .probe = edma_probe,
1099 .remove = edma_remove,
1100 .driver = {
1101 .name = "edma-dma-engine",
1102 .owner = THIS_MODULE,
1106 bool edma_filter_fn(struct dma_chan *chan, void *param)
1108 if (chan->device->dev->driver == &edma_driver.driver) {
1109 struct edma_chan *echan = to_edma_chan(chan);
1110 unsigned ch_req = *(unsigned *)param;
1111 return ch_req == echan->ch_num;
1113 return false;
1115 EXPORT_SYMBOL(edma_filter_fn);
1117 static int edma_init(void)
1119 return platform_driver_register(&edma_driver);
1121 subsys_initcall(edma_init);
1123 static void __exit edma_exit(void)
1125 platform_driver_unregister(&edma_driver);
1127 module_exit(edma_exit);
1129 MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
1130 MODULE_DESCRIPTION("TI EDMA DMA engine driver");
1131 MODULE_LICENSE("GPL v2");