1 // SPDX-License-Identifier: GPL-2.0+
3 // Actions Semi Owl SoCs DMA driver
5 // Copyright (c) 2014 Actions Semi Inc.
8 // Copyright (c) 2018 Linaro Ltd.
11 #include <linux/bitops.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/dmaengine.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/dmapool.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/slab.h>
27 #define OWL_DMA_FRAME_MAX_LENGTH 0xfffff
29 /* Global DMA Controller Registers */
30 #define OWL_DMA_IRQ_PD0 0x00
31 #define OWL_DMA_IRQ_PD1 0x04
32 #define OWL_DMA_IRQ_PD2 0x08
33 #define OWL_DMA_IRQ_PD3 0x0C
34 #define OWL_DMA_IRQ_EN0 0x10
35 #define OWL_DMA_IRQ_EN1 0x14
36 #define OWL_DMA_IRQ_EN2 0x18
37 #define OWL_DMA_IRQ_EN3 0x1C
38 #define OWL_DMA_SECURE_ACCESS_CTL 0x20
39 #define OWL_DMA_NIC_QOS 0x24
40 #define OWL_DMA_DBGSEL 0x28
41 #define OWL_DMA_IDLE_STAT 0x2C
43 /* Channel Registers */
44 #define OWL_DMA_CHAN_BASE(i) (0x100 + (i) * 0x100)
45 #define OWL_DMAX_MODE 0x00
46 #define OWL_DMAX_SOURCE 0x04
47 #define OWL_DMAX_DESTINATION 0x08
48 #define OWL_DMAX_FRAME_LEN 0x0C
49 #define OWL_DMAX_FRAME_CNT 0x10
50 #define OWL_DMAX_REMAIN_FRAME_CNT 0x14
51 #define OWL_DMAX_REMAIN_CNT 0x18
52 #define OWL_DMAX_SOURCE_STRIDE 0x1C
53 #define OWL_DMAX_DESTINATION_STRIDE 0x20
54 #define OWL_DMAX_START 0x24
55 #define OWL_DMAX_PAUSE 0x28
56 #define OWL_DMAX_CHAINED_CTL 0x2C
57 #define OWL_DMAX_CONSTANT 0x30
58 #define OWL_DMAX_LINKLIST_CTL 0x34
59 #define OWL_DMAX_NEXT_DESCRIPTOR 0x38
60 #define OWL_DMAX_CURRENT_DESCRIPTOR_NUM 0x3C
61 #define OWL_DMAX_INT_CTL 0x40
62 #define OWL_DMAX_INT_STATUS 0x44
63 #define OWL_DMAX_CURRENT_SOURCE_POINTER 0x48
64 #define OWL_DMAX_CURRENT_DESTINATION_POINTER 0x4C
66 /* OWL_DMAX_MODE Bits */
67 #define OWL_DMA_MODE_TS(x) (((x) & GENMASK(5, 0)) << 0)
68 #define OWL_DMA_MODE_ST(x) (((x) & GENMASK(1, 0)) << 8)
69 #define OWL_DMA_MODE_ST_DEV OWL_DMA_MODE_ST(0)
70 #define OWL_DMA_MODE_ST_DCU OWL_DMA_MODE_ST(2)
71 #define OWL_DMA_MODE_ST_SRAM OWL_DMA_MODE_ST(3)
72 #define OWL_DMA_MODE_DT(x) (((x) & GENMASK(1, 0)) << 10)
73 #define OWL_DMA_MODE_DT_DEV OWL_DMA_MODE_DT(0)
74 #define OWL_DMA_MODE_DT_DCU OWL_DMA_MODE_DT(2)
75 #define OWL_DMA_MODE_DT_SRAM OWL_DMA_MODE_DT(3)
76 #define OWL_DMA_MODE_SAM(x) (((x) & GENMASK(1, 0)) << 16)
77 #define OWL_DMA_MODE_SAM_CONST OWL_DMA_MODE_SAM(0)
78 #define OWL_DMA_MODE_SAM_INC OWL_DMA_MODE_SAM(1)
79 #define OWL_DMA_MODE_SAM_STRIDE OWL_DMA_MODE_SAM(2)
80 #define OWL_DMA_MODE_DAM(x) (((x) & GENMASK(1, 0)) << 18)
81 #define OWL_DMA_MODE_DAM_CONST OWL_DMA_MODE_DAM(0)
82 #define OWL_DMA_MODE_DAM_INC OWL_DMA_MODE_DAM(1)
83 #define OWL_DMA_MODE_DAM_STRIDE OWL_DMA_MODE_DAM(2)
84 #define OWL_DMA_MODE_PW(x) (((x) & GENMASK(2, 0)) << 20)
85 #define OWL_DMA_MODE_CB BIT(23)
86 #define OWL_DMA_MODE_NDDBW(x) (((x) & 0x1) << 28)
87 #define OWL_DMA_MODE_NDDBW_32BIT OWL_DMA_MODE_NDDBW(0)
88 #define OWL_DMA_MODE_NDDBW_8BIT OWL_DMA_MODE_NDDBW(1)
89 #define OWL_DMA_MODE_CFE BIT(29)
90 #define OWL_DMA_MODE_LME BIT(30)
91 #define OWL_DMA_MODE_CME BIT(31)
93 /* OWL_DMAX_LINKLIST_CTL Bits */
94 #define OWL_DMA_LLC_SAV(x) (((x) & GENMASK(1, 0)) << 8)
95 #define OWL_DMA_LLC_SAV_INC OWL_DMA_LLC_SAV(0)
96 #define OWL_DMA_LLC_SAV_LOAD_NEXT OWL_DMA_LLC_SAV(1)
97 #define OWL_DMA_LLC_SAV_LOAD_PREV OWL_DMA_LLC_SAV(2)
98 #define OWL_DMA_LLC_DAV(x) (((x) & GENMASK(1, 0)) << 10)
99 #define OWL_DMA_LLC_DAV_INC OWL_DMA_LLC_DAV(0)
100 #define OWL_DMA_LLC_DAV_LOAD_NEXT OWL_DMA_LLC_DAV(1)
101 #define OWL_DMA_LLC_DAV_LOAD_PREV OWL_DMA_LLC_DAV(2)
102 #define OWL_DMA_LLC_SUSPEND BIT(16)
104 /* OWL_DMAX_INT_CTL Bits */
105 #define OWL_DMA_INTCTL_BLOCK BIT(0)
106 #define OWL_DMA_INTCTL_SUPER_BLOCK BIT(1)
107 #define OWL_DMA_INTCTL_FRAME BIT(2)
108 #define OWL_DMA_INTCTL_HALF_FRAME BIT(3)
109 #define OWL_DMA_INTCTL_LAST_FRAME BIT(4)
111 /* OWL_DMAX_INT_STATUS Bits */
112 #define OWL_DMA_INTSTAT_BLOCK BIT(0)
113 #define OWL_DMA_INTSTAT_SUPER_BLOCK BIT(1)
114 #define OWL_DMA_INTSTAT_FRAME BIT(2)
115 #define OWL_DMA_INTSTAT_HALF_FRAME BIT(3)
116 #define OWL_DMA_INTSTAT_LAST_FRAME BIT(4)
118 /* Pack shift and newshift in a single word */
119 #define BIT_FIELD(val, width, shift, newshift) \
120 ((((val) >> (shift)) & ((BIT(width)) - 1)) << (newshift))
123 * struct owl_dma_lli_hw - Hardware link list for dma transfer
124 * @next_lli: physical address of the next link list
125 * @saddr: source physical address
126 * @daddr: destination physical address
127 * @flen: frame length
129 * @src_stride: source stride
130 * @dst_stride: destination stride
131 * @ctrla: dma_mode and linklist ctrl config
132 * @ctrlb: interrupt config
133 * @const_num: data for constant fill
135 struct owl_dma_lli_hw {
149 * struct owl_dma_lli - Link list for dma transfer
150 * @hw: hardware link list
151 * @phys: physical address of hardware link list
152 * @node: node for txd's lli_list
155 struct owl_dma_lli_hw hw;
157 struct list_head node;
161 * struct owl_dma_txd - Wrapper for struct dma_async_tx_descriptor
162 * @vd: virtual DMA descriptor
163 * @lli_list: link list of lli nodes
166 struct virt_dma_desc vd;
167 struct list_head lli_list;
171 * struct owl_dma_pchan - Holder for the physical channels
172 * @id: physical index to this channel
173 * @base: virtual memory base for the dma channel
174 * @vchan: the virtual channel currently being served by this physical channel
175 * @lock: a lock to use when altering an instance of this struct
177 struct owl_dma_pchan {
180 struct owl_dma_vchan *vchan;
185 * struct owl_dma_pchan - Wrapper for DMA ENGINE channel
186 * @vc: wrappped virtual channel
187 * @pchan: the physical channel utilized by this channel
188 * @txd: active transaction on this channel
190 struct owl_dma_vchan {
191 struct virt_dma_chan vc;
192 struct owl_dma_pchan *pchan;
193 struct owl_dma_txd *txd;
197 * struct owl_dma - Holder for the Owl DMA controller
198 * @dma: dma engine for this instance
199 * @base: virtual memory base for the DMA controller
200 * @clk: clock for the DMA controller
201 * @lock: a lock to use when change DMA controller global register
202 * @lli_pool: a pool for the LLI descriptors
203 * @nr_pchans: the number of physical channels
204 * @pchans: array of data for the physical channels
205 * @nr_vchans: the number of physical channels
206 * @vchans: array of data for the physical channels
209 struct dma_device dma;
213 struct dma_pool *lli_pool;
216 unsigned int nr_pchans;
217 struct owl_dma_pchan *pchans;
219 unsigned int nr_vchans;
220 struct owl_dma_vchan *vchans;
223 static void pchan_update(struct owl_dma_pchan *pchan, u32 reg,
228 regval = readl(pchan->base + reg);
235 writel(val, pchan->base + reg);
238 static void pchan_writel(struct owl_dma_pchan *pchan, u32 reg, u32 data)
240 writel(data, pchan->base + reg);
243 static u32 pchan_readl(struct owl_dma_pchan *pchan, u32 reg)
245 return readl(pchan->base + reg);
248 static void dma_update(struct owl_dma *od, u32 reg, u32 val, bool state)
252 regval = readl(od->base + reg);
259 writel(val, od->base + reg);
262 static void dma_writel(struct owl_dma *od, u32 reg, u32 data)
264 writel(data, od->base + reg);
267 static u32 dma_readl(struct owl_dma *od, u32 reg)
269 return readl(od->base + reg);
272 static inline struct owl_dma *to_owl_dma(struct dma_device *dd)
274 return container_of(dd, struct owl_dma, dma);
277 static struct device *chan2dev(struct dma_chan *chan)
279 return &chan->dev->device;
282 static inline struct owl_dma_vchan *to_owl_vchan(struct dma_chan *chan)
284 return container_of(chan, struct owl_dma_vchan, vc.chan);
287 static inline struct owl_dma_txd *to_owl_txd(struct dma_async_tx_descriptor *tx)
289 return container_of(tx, struct owl_dma_txd, vd.tx);
292 static inline u32 llc_hw_ctrla(u32 mode, u32 llc_ctl)
296 ctl = BIT_FIELD(mode, 4, 28, 28) |
297 BIT_FIELD(mode, 8, 16, 20) |
298 BIT_FIELD(mode, 4, 8, 16) |
299 BIT_FIELD(mode, 6, 0, 10) |
300 BIT_FIELD(llc_ctl, 2, 10, 8) |
301 BIT_FIELD(llc_ctl, 2, 8, 6);
306 static inline u32 llc_hw_ctrlb(u32 int_ctl)
310 ctl = BIT_FIELD(int_ctl, 7, 0, 18);
315 static void owl_dma_free_lli(struct owl_dma *od,
316 struct owl_dma_lli *lli)
318 list_del(&lli->node);
319 dma_pool_free(od->lli_pool, lli, lli->phys);
322 static struct owl_dma_lli *owl_dma_alloc_lli(struct owl_dma *od)
324 struct owl_dma_lli *lli;
327 lli = dma_pool_alloc(od->lli_pool, GFP_NOWAIT, &phys);
331 INIT_LIST_HEAD(&lli->node);
337 static struct owl_dma_lli *owl_dma_add_lli(struct owl_dma_txd *txd,
338 struct owl_dma_lli *prev,
339 struct owl_dma_lli *next)
341 list_add_tail(&next->node, &txd->lli_list);
344 prev->hw.next_lli = next->phys;
345 prev->hw.ctrla |= llc_hw_ctrla(OWL_DMA_MODE_LME, 0);
351 static inline int owl_dma_cfg_lli(struct owl_dma_vchan *vchan,
352 struct owl_dma_lli *lli,
353 dma_addr_t src, dma_addr_t dst,
354 u32 len, enum dma_transfer_direction dir)
356 struct owl_dma_lli_hw *hw = &lli->hw;
359 mode = OWL_DMA_MODE_PW(0);
363 mode |= OWL_DMA_MODE_TS(0) | OWL_DMA_MODE_ST_DCU |
364 OWL_DMA_MODE_DT_DCU | OWL_DMA_MODE_SAM_INC |
365 OWL_DMA_MODE_DAM_INC;
372 hw->next_lli = 0; /* One link list by default */
376 hw->fcnt = 1; /* Frame count fixed as 1 */
377 hw->flen = len; /* Max frame length is 1MB */
380 hw->ctrla = llc_hw_ctrla(mode,
381 OWL_DMA_LLC_SAV_LOAD_NEXT |
382 OWL_DMA_LLC_DAV_LOAD_NEXT);
384 hw->ctrlb = llc_hw_ctrlb(OWL_DMA_INTCTL_SUPER_BLOCK);
389 static struct owl_dma_pchan *owl_dma_get_pchan(struct owl_dma *od,
390 struct owl_dma_vchan *vchan)
392 struct owl_dma_pchan *pchan = NULL;
396 for (i = 0; i < od->nr_pchans; i++) {
397 pchan = &od->pchans[i];
399 spin_lock_irqsave(&pchan->lock, flags);
401 pchan->vchan = vchan;
402 spin_unlock_irqrestore(&pchan->lock, flags);
406 spin_unlock_irqrestore(&pchan->lock, flags);
412 static int owl_dma_pchan_busy(struct owl_dma *od, struct owl_dma_pchan *pchan)
416 val = dma_readl(od, OWL_DMA_IDLE_STAT);
418 return !(val & (1 << pchan->id));
421 static void owl_dma_terminate_pchan(struct owl_dma *od,
422 struct owl_dma_pchan *pchan)
427 pchan_writel(pchan, OWL_DMAX_START, 0);
428 pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
430 spin_lock_irqsave(&od->lock, flags);
431 dma_update(od, OWL_DMA_IRQ_EN0, (1 << pchan->id), false);
433 irq_pd = dma_readl(od, OWL_DMA_IRQ_PD0);
434 if (irq_pd & (1 << pchan->id)) {
435 dev_warn(od->dma.dev,
436 "terminating pchan %d that still has pending irq\n",
438 dma_writel(od, OWL_DMA_IRQ_PD0, (1 << pchan->id));
443 spin_unlock_irqrestore(&od->lock, flags);
446 static int owl_dma_start_next_txd(struct owl_dma_vchan *vchan)
448 struct owl_dma *od = to_owl_dma(vchan->vc.chan.device);
449 struct virt_dma_desc *vd = vchan_next_desc(&vchan->vc);
450 struct owl_dma_pchan *pchan = vchan->pchan;
451 struct owl_dma_txd *txd = to_owl_txd(&vd->tx);
452 struct owl_dma_lli *lli;
460 /* Wait for channel inactive */
461 while (owl_dma_pchan_busy(od, pchan))
464 lli = list_first_entry(&txd->lli_list,
465 struct owl_dma_lli, node);
467 int_ctl = OWL_DMA_INTCTL_SUPER_BLOCK;
469 pchan_writel(pchan, OWL_DMAX_MODE, OWL_DMA_MODE_LME);
470 pchan_writel(pchan, OWL_DMAX_LINKLIST_CTL,
471 OWL_DMA_LLC_SAV_LOAD_NEXT | OWL_DMA_LLC_DAV_LOAD_NEXT);
472 pchan_writel(pchan, OWL_DMAX_NEXT_DESCRIPTOR, lli->phys);
473 pchan_writel(pchan, OWL_DMAX_INT_CTL, int_ctl);
475 /* Clear IRQ status for this pchan */
476 pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
478 spin_lock_irqsave(&od->lock, flags);
480 dma_update(od, OWL_DMA_IRQ_EN0, (1 << pchan->id), true);
482 spin_unlock_irqrestore(&od->lock, flags);
484 dev_dbg(chan2dev(&vchan->vc.chan), "starting pchan %d\n", pchan->id);
486 /* Start DMA transfer for this pchan */
487 pchan_writel(pchan, OWL_DMAX_START, 0x1);
492 static void owl_dma_phy_free(struct owl_dma *od, struct owl_dma_vchan *vchan)
494 /* Ensure that the physical channel is stopped */
495 owl_dma_terminate_pchan(od, vchan->pchan);
500 static irqreturn_t owl_dma_interrupt(int irq, void *dev_id)
502 struct owl_dma *od = dev_id;
503 struct owl_dma_vchan *vchan;
504 struct owl_dma_pchan *pchan;
505 unsigned long pending;
507 unsigned int global_irq_pending, chan_irq_pending;
509 spin_lock(&od->lock);
511 pending = dma_readl(od, OWL_DMA_IRQ_PD0);
513 /* Clear IRQ status for each pchan */
514 for_each_set_bit(i, &pending, od->nr_pchans) {
515 pchan = &od->pchans[i];
516 pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
519 /* Clear pending IRQ */
520 dma_writel(od, OWL_DMA_IRQ_PD0, pending);
522 /* Check missed pending IRQ */
523 for (i = 0; i < od->nr_pchans; i++) {
524 pchan = &od->pchans[i];
525 chan_irq_pending = pchan_readl(pchan, OWL_DMAX_INT_CTL) &
526 pchan_readl(pchan, OWL_DMAX_INT_STATUS);
528 /* Dummy read to ensure OWL_DMA_IRQ_PD0 value is updated */
529 dma_readl(od, OWL_DMA_IRQ_PD0);
531 global_irq_pending = dma_readl(od, OWL_DMA_IRQ_PD0);
533 if (chan_irq_pending && !(global_irq_pending & BIT(i))) {
535 "global and channel IRQ pending match err\n");
537 /* Clear IRQ status for this pchan */
538 pchan_update(pchan, OWL_DMAX_INT_STATUS,
541 /* Update global IRQ pending */
546 spin_unlock(&od->lock);
548 for_each_set_bit(i, &pending, od->nr_pchans) {
549 struct owl_dma_txd *txd;
551 pchan = &od->pchans[i];
553 vchan = pchan->vchan;
555 dev_warn(od->dma.dev, "no vchan attached on pchan %d\n",
560 spin_lock(&vchan->vc.lock);
566 vchan_cookie_complete(&txd->vd);
569 * Start the next descriptor (if any),
570 * otherwise free this channel.
572 if (vchan_next_desc(&vchan->vc))
573 owl_dma_start_next_txd(vchan);
575 owl_dma_phy_free(od, vchan);
578 spin_unlock(&vchan->vc.lock);
584 static void owl_dma_free_txd(struct owl_dma *od, struct owl_dma_txd *txd)
586 struct owl_dma_lli *lli, *_lli;
591 list_for_each_entry_safe(lli, _lli, &txd->lli_list, node)
592 owl_dma_free_lli(od, lli);
597 static void owl_dma_desc_free(struct virt_dma_desc *vd)
599 struct owl_dma *od = to_owl_dma(vd->tx.chan->device);
600 struct owl_dma_txd *txd = to_owl_txd(&vd->tx);
602 owl_dma_free_txd(od, txd);
605 static int owl_dma_terminate_all(struct dma_chan *chan)
607 struct owl_dma *od = to_owl_dma(chan->device);
608 struct owl_dma_vchan *vchan = to_owl_vchan(chan);
612 spin_lock_irqsave(&vchan->vc.lock, flags);
615 owl_dma_phy_free(od, vchan);
618 owl_dma_desc_free(&vchan->txd->vd);
622 vchan_get_all_descriptors(&vchan->vc, &head);
623 vchan_dma_desc_free_list(&vchan->vc, &head);
625 spin_unlock_irqrestore(&vchan->vc.lock, flags);
630 static u32 owl_dma_getbytes_chan(struct owl_dma_vchan *vchan)
632 struct owl_dma_pchan *pchan;
633 struct owl_dma_txd *txd;
634 struct owl_dma_lli *lli;
635 unsigned int next_lli_phy;
638 pchan = vchan->pchan;
644 /* Get remain count of current node in link list */
645 bytes = pchan_readl(pchan, OWL_DMAX_REMAIN_CNT);
647 /* Loop through the preceding nodes to get total remaining bytes */
648 if (pchan_readl(pchan, OWL_DMAX_MODE) & OWL_DMA_MODE_LME) {
649 next_lli_phy = pchan_readl(pchan, OWL_DMAX_NEXT_DESCRIPTOR);
650 list_for_each_entry(lli, &txd->lli_list, node) {
651 /* Start from the next active node */
652 if (lli->phys == next_lli_phy) {
653 list_for_each_entry(lli, &txd->lli_list, node)
654 bytes += lli->hw.flen;
663 static enum dma_status owl_dma_tx_status(struct dma_chan *chan,
665 struct dma_tx_state *state)
667 struct owl_dma_vchan *vchan = to_owl_vchan(chan);
668 struct owl_dma_lli *lli;
669 struct virt_dma_desc *vd;
670 struct owl_dma_txd *txd;
675 ret = dma_cookie_status(chan, cookie, state);
676 if (ret == DMA_COMPLETE || !state)
679 spin_lock_irqsave(&vchan->vc.lock, flags);
681 vd = vchan_find_desc(&vchan->vc, cookie);
683 txd = to_owl_txd(&vd->tx);
684 list_for_each_entry(lli, &txd->lli_list, node)
685 bytes += lli->hw.flen;
687 bytes = owl_dma_getbytes_chan(vchan);
690 spin_unlock_irqrestore(&vchan->vc.lock, flags);
692 dma_set_residue(state, bytes);
697 static void owl_dma_phy_alloc_and_start(struct owl_dma_vchan *vchan)
699 struct owl_dma *od = to_owl_dma(vchan->vc.chan.device);
700 struct owl_dma_pchan *pchan;
702 pchan = owl_dma_get_pchan(od, vchan);
706 dev_dbg(od->dma.dev, "allocated pchan %d\n", pchan->id);
708 vchan->pchan = pchan;
709 owl_dma_start_next_txd(vchan);
712 static void owl_dma_issue_pending(struct dma_chan *chan)
714 struct owl_dma_vchan *vchan = to_owl_vchan(chan);
717 spin_lock_irqsave(&vchan->vc.lock, flags);
718 if (vchan_issue_pending(&vchan->vc)) {
720 owl_dma_phy_alloc_and_start(vchan);
722 spin_unlock_irqrestore(&vchan->vc.lock, flags);
725 static struct dma_async_tx_descriptor
726 *owl_dma_prep_memcpy(struct dma_chan *chan,
727 dma_addr_t dst, dma_addr_t src,
728 size_t len, unsigned long flags)
730 struct owl_dma *od = to_owl_dma(chan->device);
731 struct owl_dma_vchan *vchan = to_owl_vchan(chan);
732 struct owl_dma_txd *txd;
733 struct owl_dma_lli *lli, *prev = NULL;
734 size_t offset, bytes;
740 txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
744 INIT_LIST_HEAD(&txd->lli_list);
746 /* Process the transfer as frame by frame */
747 for (offset = 0; offset < len; offset += bytes) {
748 lli = owl_dma_alloc_lli(od);
750 dev_warn(chan2dev(chan), "failed to allocate lli\n");
754 bytes = min_t(size_t, (len - offset), OWL_DMA_FRAME_MAX_LENGTH);
756 ret = owl_dma_cfg_lli(vchan, lli, src + offset, dst + offset,
757 bytes, DMA_MEM_TO_MEM);
759 dev_warn(chan2dev(chan), "failed to config lli\n");
763 prev = owl_dma_add_lli(txd, prev, lli);
766 return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
769 owl_dma_free_txd(od, txd);
773 static void owl_dma_free_chan_resources(struct dma_chan *chan)
775 struct owl_dma_vchan *vchan = to_owl_vchan(chan);
777 /* Ensure all queued descriptors are freed */
778 vchan_free_chan_resources(&vchan->vc);
781 static inline void owl_dma_free(struct owl_dma *od)
783 struct owl_dma_vchan *vchan = NULL;
784 struct owl_dma_vchan *next;
786 list_for_each_entry_safe(vchan,
787 next, &od->dma.channels, vc.chan.device_node) {
788 list_del(&vchan->vc.chan.device_node);
789 tasklet_kill(&vchan->vc.task);
793 static int owl_dma_probe(struct platform_device *pdev)
795 struct device_node *np = pdev->dev.of_node;
797 struct resource *res;
798 int ret, i, nr_channels, nr_requests;
800 od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
804 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
808 od->base = devm_ioremap_resource(&pdev->dev, res);
809 if (IS_ERR(od->base))
810 return PTR_ERR(od->base);
812 ret = of_property_read_u32(np, "dma-channels", &nr_channels);
814 dev_err(&pdev->dev, "can't get dma-channels\n");
818 ret = of_property_read_u32(np, "dma-requests", &nr_requests);
820 dev_err(&pdev->dev, "can't get dma-requests\n");
824 dev_info(&pdev->dev, "dma-channels %d, dma-requests %d\n",
825 nr_channels, nr_requests);
827 od->nr_pchans = nr_channels;
828 od->nr_vchans = nr_requests;
830 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
832 platform_set_drvdata(pdev, od);
833 spin_lock_init(&od->lock);
835 dma_cap_set(DMA_MEMCPY, od->dma.cap_mask);
837 od->dma.dev = &pdev->dev;
838 od->dma.device_free_chan_resources = owl_dma_free_chan_resources;
839 od->dma.device_tx_status = owl_dma_tx_status;
840 od->dma.device_issue_pending = owl_dma_issue_pending;
841 od->dma.device_prep_dma_memcpy = owl_dma_prep_memcpy;
842 od->dma.device_terminate_all = owl_dma_terminate_all;
843 od->dma.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
844 od->dma.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
845 od->dma.directions = BIT(DMA_MEM_TO_MEM);
846 od->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
848 INIT_LIST_HEAD(&od->dma.channels);
850 od->clk = devm_clk_get(&pdev->dev, NULL);
851 if (IS_ERR(od->clk)) {
852 dev_err(&pdev->dev, "unable to get clock\n");
853 return PTR_ERR(od->clk);
857 * Eventhough the DMA controller is capable of generating 4
858 * IRQ's for DMA priority feature, we only use 1 IRQ for
861 od->irq = platform_get_irq(pdev, 0);
862 ret = devm_request_irq(&pdev->dev, od->irq, owl_dma_interrupt, 0,
863 dev_name(&pdev->dev), od);
865 dev_err(&pdev->dev, "unable to request IRQ\n");
869 /* Init physical channel */
870 od->pchans = devm_kcalloc(&pdev->dev, od->nr_pchans,
871 sizeof(struct owl_dma_pchan), GFP_KERNEL);
875 for (i = 0; i < od->nr_pchans; i++) {
876 struct owl_dma_pchan *pchan = &od->pchans[i];
879 pchan->base = od->base + OWL_DMA_CHAN_BASE(i);
882 /* Init virtual channel */
883 od->vchans = devm_kcalloc(&pdev->dev, od->nr_vchans,
884 sizeof(struct owl_dma_vchan), GFP_KERNEL);
888 for (i = 0; i < od->nr_vchans; i++) {
889 struct owl_dma_vchan *vchan = &od->vchans[i];
891 vchan->vc.desc_free = owl_dma_desc_free;
892 vchan_init(&vchan->vc, &od->dma);
895 /* Create a pool of consistent memory blocks for hardware descriptors */
896 od->lli_pool = dma_pool_create(dev_name(od->dma.dev), od->dma.dev,
897 sizeof(struct owl_dma_lli),
898 __alignof__(struct owl_dma_lli),
901 dev_err(&pdev->dev, "unable to allocate DMA descriptor pool\n");
905 clk_prepare_enable(od->clk);
907 ret = dma_async_device_register(&od->dma);
909 dev_err(&pdev->dev, "failed to register DMA engine device\n");
916 clk_disable_unprepare(od->clk);
917 dma_pool_destroy(od->lli_pool);
922 static int owl_dma_remove(struct platform_device *pdev)
924 struct owl_dma *od = platform_get_drvdata(pdev);
926 dma_async_device_unregister(&od->dma);
928 /* Mask all interrupts for this execution environment */
929 dma_writel(od, OWL_DMA_IRQ_EN0, 0x0);
931 /* Make sure we won't have any further interrupts */
932 devm_free_irq(od->dma.dev, od->irq, od);
936 clk_disable_unprepare(od->clk);
941 static const struct of_device_id owl_dma_match[] = {
942 { .compatible = "actions,s900-dma", },
945 MODULE_DEVICE_TABLE(of, owl_dma_match);
947 static struct platform_driver owl_dma_driver = {
948 .probe = owl_dma_probe,
949 .remove = owl_dma_remove,
952 .of_match_table = of_match_ptr(owl_dma_match),
956 static int owl_dma_init(void)
958 return platform_driver_register(&owl_dma_driver);
960 subsys_initcall(owl_dma_init);
962 static void __exit owl_dma_exit(void)
964 platform_driver_unregister(&owl_dma_driver);
966 module_exit(owl_dma_exit);
970 MODULE_DESCRIPTION("Actions Semi Owl SoCs DMA driver");
971 MODULE_LICENSE("GPL");