1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018-2019 Synopsys, Inc. and/or its affiliates.
4 * Synopsys DesignWare eDMA core driver
6 * Author: Gustavo Pimentel <gustavo.pimentel@synopsys.com>
9 #include <linux/module.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/dmaengine.h>
14 #include <linux/err.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/dma/edma.h>
18 #include <linux/dma-mapping.h>
20 #include "dw-edma-core.h"
21 #include "dw-edma-v0-core.h"
22 #include "../dmaengine.h"
23 #include "../virt-dma.h"
26 struct device *dchan2dev(struct dma_chan *dchan)
28 return &dchan->dev->device;
32 struct device *chan2dev(struct dw_edma_chan *chan)
34 return &chan->vc.chan.dev->device;
38 struct dw_edma_desc *vd2dw_edma_desc(struct virt_dma_desc *vd)
40 return container_of(vd, struct dw_edma_desc, vd);
43 static struct dw_edma_burst *dw_edma_alloc_burst(struct dw_edma_chunk *chunk)
45 struct dw_edma_burst *burst;
47 burst = kzalloc(sizeof(*burst), GFP_NOWAIT);
51 INIT_LIST_HEAD(&burst->list);
53 /* Create and add new element into the linked list */
54 chunk->bursts_alloc++;
55 list_add_tail(&burst->list, &chunk->burst->list);
58 chunk->bursts_alloc = 0;
65 static struct dw_edma_chunk *dw_edma_alloc_chunk(struct dw_edma_desc *desc)
67 struct dw_edma_chan *chan = desc->chan;
68 struct dw_edma *dw = chan->chip->dw;
69 struct dw_edma_chunk *chunk;
71 chunk = kzalloc(sizeof(*chunk), GFP_NOWAIT);
75 INIT_LIST_HEAD(&chunk->list);
77 /* Toggling change bit (CB) in each chunk, this is a mechanism to
78 * inform the eDMA HW block that this is a new linked list ready
80 * - Odd chunks originate CB equal to 0
81 * - Even chunks originate CB equal to 1
83 chunk->cb = !(desc->chunks_alloc % 2);
84 if (chan->dir == EDMA_DIR_WRITE) {
85 chunk->ll_region.paddr = dw->ll_region_wr[chan->id].paddr;
86 chunk->ll_region.vaddr = dw->ll_region_wr[chan->id].vaddr;
88 chunk->ll_region.paddr = dw->ll_region_rd[chan->id].paddr;
89 chunk->ll_region.vaddr = dw->ll_region_rd[chan->id].vaddr;
93 /* Create and add new element into the linked list */
94 if (!dw_edma_alloc_burst(chunk)) {
99 list_add_tail(&chunk->list, &desc->chunk->list);
103 desc->chunks_alloc = 0;
110 static struct dw_edma_desc *dw_edma_alloc_desc(struct dw_edma_chan *chan)
112 struct dw_edma_desc *desc;
114 desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
119 if (!dw_edma_alloc_chunk(desc)) {
127 static void dw_edma_free_burst(struct dw_edma_chunk *chunk)
129 struct dw_edma_burst *child, *_next;
131 /* Remove all the list elements */
132 list_for_each_entry_safe(child, _next, &chunk->burst->list, list) {
133 list_del(&child->list);
135 chunk->bursts_alloc--;
138 /* Remove the list head */
143 static void dw_edma_free_chunk(struct dw_edma_desc *desc)
145 struct dw_edma_chunk *child, *_next;
150 /* Remove all the list elements */
151 list_for_each_entry_safe(child, _next, &desc->chunk->list, list) {
152 dw_edma_free_burst(child);
153 list_del(&child->list);
155 desc->chunks_alloc--;
158 /* Remove the list head */
163 static void dw_edma_free_desc(struct dw_edma_desc *desc)
165 dw_edma_free_chunk(desc);
169 static void vchan_free_desc(struct virt_dma_desc *vdesc)
171 dw_edma_free_desc(vd2dw_edma_desc(vdesc));
174 static void dw_edma_start_transfer(struct dw_edma_chan *chan)
176 struct dw_edma_chunk *child;
177 struct dw_edma_desc *desc;
178 struct virt_dma_desc *vd;
180 vd = vchan_next_desc(&chan->vc);
184 desc = vd2dw_edma_desc(vd);
188 child = list_first_entry_or_null(&desc->chunk->list,
189 struct dw_edma_chunk, list);
193 dw_edma_v0_core_start(child, !desc->xfer_sz);
194 desc->xfer_sz += child->ll_region.sz;
195 dw_edma_free_burst(child);
196 list_del(&child->list);
198 desc->chunks_alloc--;
201 static int dw_edma_device_config(struct dma_chan *dchan,
202 struct dma_slave_config *config)
204 struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
206 memcpy(&chan->config, config, sizeof(*config));
207 chan->configured = true;
212 static int dw_edma_device_pause(struct dma_chan *dchan)
214 struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
217 if (!chan->configured)
219 else if (chan->status != EDMA_ST_BUSY)
221 else if (chan->request != EDMA_REQ_NONE)
224 chan->request = EDMA_REQ_PAUSE;
229 static int dw_edma_device_resume(struct dma_chan *dchan)
231 struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
234 if (!chan->configured) {
236 } else if (chan->status != EDMA_ST_PAUSE) {
238 } else if (chan->request != EDMA_REQ_NONE) {
241 chan->status = EDMA_ST_BUSY;
242 dw_edma_start_transfer(chan);
248 static int dw_edma_device_terminate_all(struct dma_chan *dchan)
250 struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
254 if (!chan->configured) {
256 } else if (chan->status == EDMA_ST_PAUSE) {
257 chan->status = EDMA_ST_IDLE;
258 chan->configured = false;
259 } else if (chan->status == EDMA_ST_IDLE) {
260 chan->configured = false;
261 } else if (dw_edma_v0_core_ch_status(chan) == DMA_COMPLETE) {
263 * The channel is in a false BUSY state, probably didn't
264 * receive or lost an interrupt
266 chan->status = EDMA_ST_IDLE;
267 chan->configured = false;
268 } else if (chan->request > EDMA_REQ_PAUSE) {
271 chan->request = EDMA_REQ_STOP;
277 static void dw_edma_device_issue_pending(struct dma_chan *dchan)
279 struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
282 spin_lock_irqsave(&chan->vc.lock, flags);
283 if (chan->configured && chan->request == EDMA_REQ_NONE &&
284 chan->status == EDMA_ST_IDLE && vchan_issue_pending(&chan->vc)) {
285 chan->status = EDMA_ST_BUSY;
286 dw_edma_start_transfer(chan);
288 spin_unlock_irqrestore(&chan->vc.lock, flags);
291 static enum dma_status
292 dw_edma_device_tx_status(struct dma_chan *dchan, dma_cookie_t cookie,
293 struct dma_tx_state *txstate)
295 struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
296 struct dw_edma_desc *desc;
297 struct virt_dma_desc *vd;
302 ret = dma_cookie_status(dchan, cookie, txstate);
303 if (ret == DMA_COMPLETE)
306 if (ret == DMA_IN_PROGRESS && chan->status == EDMA_ST_PAUSE)
312 spin_lock_irqsave(&chan->vc.lock, flags);
313 vd = vchan_find_desc(&chan->vc, cookie);
315 desc = vd2dw_edma_desc(vd);
317 residue = desc->alloc_sz - desc->xfer_sz;
319 spin_unlock_irqrestore(&chan->vc.lock, flags);
322 dma_set_residue(txstate, residue);
327 static struct dma_async_tx_descriptor *
328 dw_edma_device_transfer(struct dw_edma_transfer *xfer)
330 struct dw_edma_chan *chan = dchan2dw_edma_chan(xfer->dchan);
331 enum dma_transfer_direction dir = xfer->direction;
332 phys_addr_t src_addr, dst_addr;
333 struct scatterlist *sg = NULL;
334 struct dw_edma_chunk *chunk;
335 struct dw_edma_burst *burst;
336 struct dw_edma_desc *desc;
340 if (!chan->configured)
343 switch (chan->config.direction) {
344 case DMA_DEV_TO_MEM: /* local DMA */
345 if (dir == DMA_DEV_TO_MEM && chan->dir == EDMA_DIR_READ)
348 case DMA_MEM_TO_DEV: /* local DMA */
349 if (dir == DMA_MEM_TO_DEV && chan->dir == EDMA_DIR_WRITE)
352 default: /* remote DMA */
353 if (dir == DMA_MEM_TO_DEV && chan->dir == EDMA_DIR_READ)
355 if (dir == DMA_DEV_TO_MEM && chan->dir == EDMA_DIR_WRITE)
360 if (xfer->type == EDMA_XFER_CYCLIC) {
361 if (!xfer->xfer.cyclic.len || !xfer->xfer.cyclic.cnt)
363 } else if (xfer->type == EDMA_XFER_SCATTER_GATHER) {
364 if (xfer->xfer.sg.len < 1)
366 } else if (xfer->type == EDMA_XFER_INTERLEAVED) {
367 if (!xfer->xfer.il->numf)
369 if (xfer->xfer.il->numf > 0 && xfer->xfer.il->frame_size > 0)
375 desc = dw_edma_alloc_desc(chan);
379 chunk = dw_edma_alloc_chunk(desc);
380 if (unlikely(!chunk))
383 if (xfer->type == EDMA_XFER_INTERLEAVED) {
384 src_addr = xfer->xfer.il->src_start;
385 dst_addr = xfer->xfer.il->dst_start;
387 src_addr = chan->config.src_addr;
388 dst_addr = chan->config.dst_addr;
391 if (xfer->type == EDMA_XFER_CYCLIC) {
392 cnt = xfer->xfer.cyclic.cnt;
393 } else if (xfer->type == EDMA_XFER_SCATTER_GATHER) {
394 cnt = xfer->xfer.sg.len;
395 sg = xfer->xfer.sg.sgl;
396 } else if (xfer->type == EDMA_XFER_INTERLEAVED) {
397 if (xfer->xfer.il->numf > 0)
398 cnt = xfer->xfer.il->numf;
400 cnt = xfer->xfer.il->frame_size;
403 for (i = 0; i < cnt; i++) {
404 if (xfer->type == EDMA_XFER_SCATTER_GATHER && !sg)
407 if (chunk->bursts_alloc == chan->ll_max) {
408 chunk = dw_edma_alloc_chunk(desc);
409 if (unlikely(!chunk))
413 burst = dw_edma_alloc_burst(chunk);
414 if (unlikely(!burst))
417 if (xfer->type == EDMA_XFER_CYCLIC)
418 burst->sz = xfer->xfer.cyclic.len;
419 else if (xfer->type == EDMA_XFER_SCATTER_GATHER)
420 burst->sz = sg_dma_len(sg);
421 else if (xfer->type == EDMA_XFER_INTERLEAVED)
422 burst->sz = xfer->xfer.il->sgl[i].size;
424 chunk->ll_region.sz += burst->sz;
425 desc->alloc_sz += burst->sz;
427 if (chan->dir == EDMA_DIR_WRITE) {
428 burst->sar = src_addr;
429 if (xfer->type == EDMA_XFER_CYCLIC) {
430 burst->dar = xfer->xfer.cyclic.paddr;
431 } else if (xfer->type == EDMA_XFER_SCATTER_GATHER) {
432 src_addr += sg_dma_len(sg);
433 burst->dar = sg_dma_address(sg);
434 /* Unlike the typical assumption by other
435 * drivers/IPs the peripheral memory isn't
436 * a FIFO memory, in this case, it's a
437 * linear memory and that why the source
438 * and destination addresses are increased
439 * by the same portion (data length)
443 burst->dar = dst_addr;
444 if (xfer->type == EDMA_XFER_CYCLIC) {
445 burst->sar = xfer->xfer.cyclic.paddr;
446 } else if (xfer->type == EDMA_XFER_SCATTER_GATHER) {
447 dst_addr += sg_dma_len(sg);
448 burst->sar = sg_dma_address(sg);
449 /* Unlike the typical assumption by other
450 * drivers/IPs the peripheral memory isn't
451 * a FIFO memory, in this case, it's a
452 * linear memory and that why the source
453 * and destination addresses are increased
454 * by the same portion (data length)
459 if (xfer->type == EDMA_XFER_SCATTER_GATHER) {
461 } else if (xfer->type == EDMA_XFER_INTERLEAVED &&
462 xfer->xfer.il->frame_size > 0) {
463 struct dma_interleaved_template *il = xfer->xfer.il;
464 struct data_chunk *dc = &il->sgl[i];
467 src_addr += burst->sz;
468 src_addr += dmaengine_get_src_icg(il, dc);
472 dst_addr += burst->sz;
473 dst_addr += dmaengine_get_dst_icg(il, dc);
478 return vchan_tx_prep(&chan->vc, &desc->vd, xfer->flags);
482 dw_edma_free_desc(desc);
487 static struct dma_async_tx_descriptor *
488 dw_edma_device_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
490 enum dma_transfer_direction direction,
491 unsigned long flags, void *context)
493 struct dw_edma_transfer xfer;
496 xfer.direction = direction;
497 xfer.xfer.sg.sgl = sgl;
498 xfer.xfer.sg.len = len;
500 xfer.type = EDMA_XFER_SCATTER_GATHER;
502 return dw_edma_device_transfer(&xfer);
505 static struct dma_async_tx_descriptor *
506 dw_edma_device_prep_dma_cyclic(struct dma_chan *dchan, dma_addr_t paddr,
507 size_t len, size_t count,
508 enum dma_transfer_direction direction,
511 struct dw_edma_transfer xfer;
514 xfer.direction = direction;
515 xfer.xfer.cyclic.paddr = paddr;
516 xfer.xfer.cyclic.len = len;
517 xfer.xfer.cyclic.cnt = count;
519 xfer.type = EDMA_XFER_CYCLIC;
521 return dw_edma_device_transfer(&xfer);
524 static struct dma_async_tx_descriptor *
525 dw_edma_device_prep_interleaved_dma(struct dma_chan *dchan,
526 struct dma_interleaved_template *ilt,
529 struct dw_edma_transfer xfer;
532 xfer.direction = ilt->dir;
535 xfer.type = EDMA_XFER_INTERLEAVED;
537 return dw_edma_device_transfer(&xfer);
540 static void dw_edma_done_interrupt(struct dw_edma_chan *chan)
542 struct dw_edma_desc *desc;
543 struct virt_dma_desc *vd;
546 dw_edma_v0_core_clear_done_int(chan);
548 spin_lock_irqsave(&chan->vc.lock, flags);
549 vd = vchan_next_desc(&chan->vc);
551 switch (chan->request) {
553 desc = vd2dw_edma_desc(vd);
554 if (desc->chunks_alloc) {
555 chan->status = EDMA_ST_BUSY;
556 dw_edma_start_transfer(chan);
559 vchan_cookie_complete(vd);
560 chan->status = EDMA_ST_IDLE;
566 vchan_cookie_complete(vd);
567 chan->request = EDMA_REQ_NONE;
568 chan->status = EDMA_ST_IDLE;
572 chan->request = EDMA_REQ_NONE;
573 chan->status = EDMA_ST_PAUSE;
580 spin_unlock_irqrestore(&chan->vc.lock, flags);
583 static void dw_edma_abort_interrupt(struct dw_edma_chan *chan)
585 struct virt_dma_desc *vd;
588 dw_edma_v0_core_clear_abort_int(chan);
590 spin_lock_irqsave(&chan->vc.lock, flags);
591 vd = vchan_next_desc(&chan->vc);
594 vchan_cookie_complete(vd);
596 spin_unlock_irqrestore(&chan->vc.lock, flags);
597 chan->request = EDMA_REQ_NONE;
598 chan->status = EDMA_ST_IDLE;
601 static irqreturn_t dw_edma_interrupt(int irq, void *data, bool write)
603 struct dw_edma_irq *dw_irq = data;
604 struct dw_edma *dw = dw_irq->dw;
605 unsigned long total, pos, val;
610 total = dw->wr_ch_cnt;
612 mask = dw_irq->wr_mask;
614 total = dw->rd_ch_cnt;
616 mask = dw_irq->rd_mask;
619 val = dw_edma_v0_core_status_done_int(dw, write ?
623 for_each_set_bit(pos, &val, total) {
624 struct dw_edma_chan *chan = &dw->chan[pos + off];
626 dw_edma_done_interrupt(chan);
629 val = dw_edma_v0_core_status_abort_int(dw, write ?
633 for_each_set_bit(pos, &val, total) {
634 struct dw_edma_chan *chan = &dw->chan[pos + off];
636 dw_edma_abort_interrupt(chan);
642 static inline irqreturn_t dw_edma_interrupt_write(int irq, void *data)
644 return dw_edma_interrupt(irq, data, true);
647 static inline irqreturn_t dw_edma_interrupt_read(int irq, void *data)
649 return dw_edma_interrupt(irq, data, false);
652 static irqreturn_t dw_edma_interrupt_common(int irq, void *data)
654 dw_edma_interrupt(irq, data, true);
655 dw_edma_interrupt(irq, data, false);
660 static int dw_edma_alloc_chan_resources(struct dma_chan *dchan)
662 struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
664 if (chan->status != EDMA_ST_IDLE)
667 pm_runtime_get(chan->chip->dev);
672 static void dw_edma_free_chan_resources(struct dma_chan *dchan)
674 unsigned long timeout = jiffies + msecs_to_jiffies(5000);
675 struct dw_edma_chan *chan = dchan2dw_edma_chan(dchan);
678 while (time_before(jiffies, timeout)) {
679 ret = dw_edma_device_terminate_all(dchan);
683 if (time_after_eq(jiffies, timeout))
689 pm_runtime_put(chan->chip->dev);
692 static int dw_edma_channel_setup(struct dw_edma_chip *chip, bool write,
693 u32 wr_alloc, u32 rd_alloc)
695 struct dw_edma_region *dt_region;
696 struct device *dev = chip->dev;
697 struct dw_edma *dw = chip->dw;
698 struct dw_edma_chan *chan;
699 struct dw_edma_irq *irq;
700 struct dma_device *dma;
701 u32 alloc, off_alloc;
717 off_alloc = wr_alloc;
720 INIT_LIST_HEAD(&dma->channels);
721 for (j = 0; (alloc || dw->nr_irqs == 1) && j < cnt; j++, i++) {
724 dt_region = devm_kzalloc(dev, sizeof(*dt_region), GFP_KERNEL);
728 chan->vc.chan.private = dt_region;
732 chan->dir = write ? EDMA_DIR_WRITE : EDMA_DIR_READ;
733 chan->configured = false;
734 chan->request = EDMA_REQ_NONE;
735 chan->status = EDMA_ST_IDLE;
738 chan->ll_max = (dw->ll_region_wr[j].sz / EDMA_LL_SZ);
740 chan->ll_max = (dw->ll_region_rd[j].sz / EDMA_LL_SZ);
743 dev_vdbg(dev, "L. List:\tChannel %s[%u] max_cnt=%u\n",
744 write ? "write" : "read", j, chan->ll_max);
746 if (dw->nr_irqs == 1)
749 pos = off_alloc + (j % alloc);
754 irq->wr_mask |= BIT(j);
756 irq->rd_mask |= BIT(j);
759 memcpy(&chan->msi, &irq->msi, sizeof(chan->msi));
761 dev_vdbg(dev, "MSI:\t\tChannel %s[%u] addr=0x%.8x%.8x, data=0x%.8x\n",
762 write ? "write" : "read", j,
763 chan->msi.address_hi, chan->msi.address_lo,
766 chan->vc.desc_free = vchan_free_desc;
767 vchan_init(&chan->vc, dma);
770 dt_region->paddr = dw->dt_region_wr[j].paddr;
771 dt_region->vaddr = dw->dt_region_wr[j].vaddr;
772 dt_region->sz = dw->dt_region_wr[j].sz;
774 dt_region->paddr = dw->dt_region_rd[j].paddr;
775 dt_region->vaddr = dw->dt_region_rd[j].vaddr;
776 dt_region->sz = dw->dt_region_rd[j].sz;
779 dw_edma_v0_core_device_config(chan);
782 /* Set DMA channel capabilities */
783 dma_cap_zero(dma->cap_mask);
784 dma_cap_set(DMA_SLAVE, dma->cap_mask);
785 dma_cap_set(DMA_CYCLIC, dma->cap_mask);
786 dma_cap_set(DMA_PRIVATE, dma->cap_mask);
787 dma_cap_set(DMA_INTERLEAVE, dma->cap_mask);
788 dma->directions = BIT(write ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV);
789 dma->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
790 dma->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
791 dma->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
794 /* Set DMA channel callbacks */
795 dma->dev = chip->dev;
796 dma->device_alloc_chan_resources = dw_edma_alloc_chan_resources;
797 dma->device_free_chan_resources = dw_edma_free_chan_resources;
798 dma->device_config = dw_edma_device_config;
799 dma->device_pause = dw_edma_device_pause;
800 dma->device_resume = dw_edma_device_resume;
801 dma->device_terminate_all = dw_edma_device_terminate_all;
802 dma->device_issue_pending = dw_edma_device_issue_pending;
803 dma->device_tx_status = dw_edma_device_tx_status;
804 dma->device_prep_slave_sg = dw_edma_device_prep_slave_sg;
805 dma->device_prep_dma_cyclic = dw_edma_device_prep_dma_cyclic;
806 dma->device_prep_interleaved_dma = dw_edma_device_prep_interleaved_dma;
808 dma_set_max_seg_size(dma->dev, U32_MAX);
810 /* Register DMA device */
811 err = dma_async_device_register(dma);
816 static inline void dw_edma_dec_irq_alloc(int *nr_irqs, u32 *alloc, u16 cnt)
818 if (*nr_irqs && *alloc < cnt) {
824 static inline void dw_edma_add_irq_mask(u32 *mask, u32 alloc, u16 cnt)
826 while (*mask * alloc < cnt)
830 static int dw_edma_irq_request(struct dw_edma_chip *chip,
831 u32 *wr_alloc, u32 *rd_alloc)
833 struct device *dev = chip->dev;
834 struct dw_edma *dw = chip->dw;
841 ch_cnt = dw->wr_ch_cnt + dw->rd_ch_cnt;
846 if (dw->nr_irqs == 1) {
847 /* Common IRQ shared among all channels */
848 irq = dw->ops->irq_vector(dev, 0);
849 err = request_irq(irq, dw_edma_interrupt_common,
850 IRQF_SHARED, dw->name, &dw->irq[0]);
856 if (irq_get_msi_desc(irq))
857 get_cached_msi_msg(irq, &dw->irq[0].msi);
859 /* Distribute IRQs equally among all channels */
860 int tmp = dw->nr_irqs;
862 while (tmp && (*wr_alloc + *rd_alloc) < ch_cnt) {
863 dw_edma_dec_irq_alloc(&tmp, wr_alloc, dw->wr_ch_cnt);
864 dw_edma_dec_irq_alloc(&tmp, rd_alloc, dw->rd_ch_cnt);
867 dw_edma_add_irq_mask(&wr_mask, *wr_alloc, dw->wr_ch_cnt);
868 dw_edma_add_irq_mask(&rd_mask, *rd_alloc, dw->rd_ch_cnt);
870 for (i = 0; i < (*wr_alloc + *rd_alloc); i++) {
871 irq = dw->ops->irq_vector(dev, i);
872 err = request_irq(irq,
874 dw_edma_interrupt_write :
875 dw_edma_interrupt_read,
876 IRQF_SHARED, dw->name,
883 if (irq_get_msi_desc(irq))
884 get_cached_msi_msg(irq, &dw->irq[i].msi);
893 int dw_edma_probe(struct dw_edma_chip *chip)
909 if (!dw || !dw->irq || !dw->ops || !dw->ops->irq_vector)
912 raw_spin_lock_init(&dw->lock);
914 dw->wr_ch_cnt = min_t(u16, dw->wr_ch_cnt,
915 dw_edma_v0_core_ch_count(dw, EDMA_DIR_WRITE));
916 dw->wr_ch_cnt = min_t(u16, dw->wr_ch_cnt, EDMA_MAX_WR_CH);
918 dw->rd_ch_cnt = min_t(u16, dw->rd_ch_cnt,
919 dw_edma_v0_core_ch_count(dw, EDMA_DIR_READ));
920 dw->rd_ch_cnt = min_t(u16, dw->rd_ch_cnt, EDMA_MAX_RD_CH);
922 if (!dw->wr_ch_cnt && !dw->rd_ch_cnt)
925 dev_vdbg(dev, "Channels:\twrite=%d, read=%d\n",
926 dw->wr_ch_cnt, dw->rd_ch_cnt);
928 /* Allocate channels */
929 dw->chan = devm_kcalloc(dev, dw->wr_ch_cnt + dw->rd_ch_cnt,
930 sizeof(*dw->chan), GFP_KERNEL);
934 snprintf(dw->name, sizeof(dw->name), "dw-edma-core:%d", chip->id);
936 /* Disable eDMA, only to establish the ideal initial conditions */
937 dw_edma_v0_core_off(dw);
940 err = dw_edma_irq_request(chip, &wr_alloc, &rd_alloc);
944 /* Setup write channels */
945 err = dw_edma_channel_setup(chip, true, wr_alloc, rd_alloc);
949 /* Setup read channels */
950 err = dw_edma_channel_setup(chip, false, wr_alloc, rd_alloc);
954 /* Power management */
955 pm_runtime_enable(dev);
957 /* Turn debugfs on */
958 dw_edma_v0_core_debugfs_on(chip);
963 for (i = (dw->nr_irqs - 1); i >= 0; i--)
964 free_irq(dw->ops->irq_vector(dev, i), &dw->irq[i]);
970 EXPORT_SYMBOL_GPL(dw_edma_probe);
972 int dw_edma_remove(struct dw_edma_chip *chip)
974 struct dw_edma_chan *chan, *_chan;
975 struct device *dev = chip->dev;
976 struct dw_edma *dw = chip->dw;
980 dw_edma_v0_core_off(dw);
983 for (i = (dw->nr_irqs - 1); i >= 0; i--)
984 free_irq(dw->ops->irq_vector(dev, i), &dw->irq[i]);
986 /* Power management */
987 pm_runtime_disable(dev);
989 /* Deregister eDMA device */
990 dma_async_device_unregister(&dw->wr_edma);
991 list_for_each_entry_safe(chan, _chan, &dw->wr_edma.channels,
992 vc.chan.device_node) {
993 tasklet_kill(&chan->vc.task);
994 list_del(&chan->vc.chan.device_node);
997 dma_async_device_unregister(&dw->rd_edma);
998 list_for_each_entry_safe(chan, _chan, &dw->rd_edma.channels,
999 vc.chan.device_node) {
1000 tasklet_kill(&chan->vc.task);
1001 list_del(&chan->vc.chan.device_node);
1004 /* Turn debugfs off */
1005 dw_edma_v0_core_debugfs_off(chip);
1009 EXPORT_SYMBOL_GPL(dw_edma_remove);
1011 MODULE_LICENSE("GPL v2");
1012 MODULE_DESCRIPTION("Synopsys DesignWare eDMA controller core driver");
1013 MODULE_AUTHOR("Gustavo Pimentel <gustavo.pimentel@synopsys.com>");