1 // SPDX-License-Identifier: GPL-2.0-only
3 * IMG Multi-threaded DMA Controller (MDC)
5 * Copyright (C) 2009,2012,2013 Imagination Technologies Ltd.
6 * Copyright (C) 2014 Google, Inc.
10 #include <linux/dma-mapping.h>
11 #include <linux/dmaengine.h>
12 #include <linux/dmapool.h>
13 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/kernel.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/of_dma.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/regmap.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
28 #include "dmaengine.h"
31 #define MDC_MAX_DMA_CHANNELS 32
33 #define MDC_GENERAL_CONFIG 0x000
34 #define MDC_GENERAL_CONFIG_LIST_IEN BIT(31)
35 #define MDC_GENERAL_CONFIG_IEN BIT(29)
36 #define MDC_GENERAL_CONFIG_LEVEL_INT BIT(28)
37 #define MDC_GENERAL_CONFIG_INC_W BIT(12)
38 #define MDC_GENERAL_CONFIG_INC_R BIT(8)
39 #define MDC_GENERAL_CONFIG_PHYSICAL_W BIT(7)
40 #define MDC_GENERAL_CONFIG_WIDTH_W_SHIFT 4
41 #define MDC_GENERAL_CONFIG_WIDTH_W_MASK 0x7
42 #define MDC_GENERAL_CONFIG_PHYSICAL_R BIT(3)
43 #define MDC_GENERAL_CONFIG_WIDTH_R_SHIFT 0
44 #define MDC_GENERAL_CONFIG_WIDTH_R_MASK 0x7
46 #define MDC_READ_PORT_CONFIG 0x004
47 #define MDC_READ_PORT_CONFIG_STHREAD_SHIFT 28
48 #define MDC_READ_PORT_CONFIG_STHREAD_MASK 0xf
49 #define MDC_READ_PORT_CONFIG_RTHREAD_SHIFT 24
50 #define MDC_READ_PORT_CONFIG_RTHREAD_MASK 0xf
51 #define MDC_READ_PORT_CONFIG_WTHREAD_SHIFT 16
52 #define MDC_READ_PORT_CONFIG_WTHREAD_MASK 0xf
53 #define MDC_READ_PORT_CONFIG_BURST_SIZE_SHIFT 4
54 #define MDC_READ_PORT_CONFIG_BURST_SIZE_MASK 0xff
55 #define MDC_READ_PORT_CONFIG_DREQ_ENABLE BIT(1)
57 #define MDC_READ_ADDRESS 0x008
59 #define MDC_WRITE_ADDRESS 0x00c
61 #define MDC_TRANSFER_SIZE 0x010
62 #define MDC_TRANSFER_SIZE_MASK 0xffffff
64 #define MDC_LIST_NODE_ADDRESS 0x014
66 #define MDC_CMDS_PROCESSED 0x018
67 #define MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT 16
68 #define MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK 0x3f
69 #define MDC_CMDS_PROCESSED_INT_ACTIVE BIT(8)
70 #define MDC_CMDS_PROCESSED_CMDS_DONE_SHIFT 0
71 #define MDC_CMDS_PROCESSED_CMDS_DONE_MASK 0x3f
73 #define MDC_CONTROL_AND_STATUS 0x01c
74 #define MDC_CONTROL_AND_STATUS_CANCEL BIT(20)
75 #define MDC_CONTROL_AND_STATUS_LIST_EN BIT(4)
76 #define MDC_CONTROL_AND_STATUS_EN BIT(0)
78 #define MDC_ACTIVE_TRANSFER_SIZE 0x030
80 #define MDC_GLOBAL_CONFIG_A 0x900
81 #define MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_SHIFT 16
82 #define MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_MASK 0xff
83 #define MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_SHIFT 8
84 #define MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_MASK 0xff
85 #define MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_SHIFT 0
86 #define MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_MASK 0xff
88 struct mdc_hw_list_desc {
98 * Not part of the list descriptor, but instead used by the CPU to
101 struct mdc_hw_list_desc *next_desc;
105 struct mdc_chan *chan;
106 struct virt_dma_desc vd;
107 dma_addr_t list_phys;
108 struct mdc_hw_list_desc *list;
111 unsigned int list_len;
112 unsigned int list_period_len;
113 size_t list_xfer_size;
114 unsigned int list_cmds_done;
118 struct mdc_dma *mdma;
119 struct virt_dma_chan vc;
120 struct dma_slave_config config;
121 struct mdc_tx_desc *desc;
125 unsigned int chan_nr;
128 struct mdc_dma_soc_data {
129 void (*enable_chan)(struct mdc_chan *mchan);
130 void (*disable_chan)(struct mdc_chan *mchan);
134 struct dma_device dma_dev;
137 struct dma_pool *desc_pool;
138 struct regmap *periph_regs;
140 unsigned int nr_threads;
141 unsigned int nr_channels;
142 unsigned int bus_width;
143 unsigned int max_burst_mult;
144 unsigned int max_xfer_size;
145 const struct mdc_dma_soc_data *soc;
146 struct mdc_chan channels[MDC_MAX_DMA_CHANNELS];
149 static inline u32 mdc_readl(struct mdc_dma *mdma, u32 reg)
151 return readl(mdma->regs + reg);
154 static inline void mdc_writel(struct mdc_dma *mdma, u32 val, u32 reg)
156 writel(val, mdma->regs + reg);
159 static inline u32 mdc_chan_readl(struct mdc_chan *mchan, u32 reg)
161 return mdc_readl(mchan->mdma, mchan->chan_nr * 0x040 + reg);
164 static inline void mdc_chan_writel(struct mdc_chan *mchan, u32 val, u32 reg)
166 mdc_writel(mchan->mdma, val, mchan->chan_nr * 0x040 + reg);
169 static inline struct mdc_chan *to_mdc_chan(struct dma_chan *c)
171 return container_of(to_virt_chan(c), struct mdc_chan, vc);
174 static inline struct mdc_tx_desc *to_mdc_desc(struct dma_async_tx_descriptor *t)
176 struct virt_dma_desc *vdesc = container_of(t, struct virt_dma_desc, tx);
178 return container_of(vdesc, struct mdc_tx_desc, vd);
181 static inline struct device *mdma2dev(struct mdc_dma *mdma)
183 return mdma->dma_dev.dev;
186 static inline unsigned int to_mdc_width(unsigned int bytes)
188 return ffs(bytes) - 1;
191 static inline void mdc_set_read_width(struct mdc_hw_list_desc *ldesc,
194 ldesc->gen_conf |= to_mdc_width(bytes) <<
195 MDC_GENERAL_CONFIG_WIDTH_R_SHIFT;
198 static inline void mdc_set_write_width(struct mdc_hw_list_desc *ldesc,
201 ldesc->gen_conf |= to_mdc_width(bytes) <<
202 MDC_GENERAL_CONFIG_WIDTH_W_SHIFT;
205 static void mdc_list_desc_config(struct mdc_chan *mchan,
206 struct mdc_hw_list_desc *ldesc,
207 enum dma_transfer_direction dir,
208 dma_addr_t src, dma_addr_t dst, size_t len)
210 struct mdc_dma *mdma = mchan->mdma;
211 unsigned int max_burst, burst_size;
213 ldesc->gen_conf = MDC_GENERAL_CONFIG_IEN | MDC_GENERAL_CONFIG_LIST_IEN |
214 MDC_GENERAL_CONFIG_LEVEL_INT | MDC_GENERAL_CONFIG_PHYSICAL_W |
215 MDC_GENERAL_CONFIG_PHYSICAL_R;
216 ldesc->readport_conf =
217 (mchan->thread << MDC_READ_PORT_CONFIG_STHREAD_SHIFT) |
218 (mchan->thread << MDC_READ_PORT_CONFIG_RTHREAD_SHIFT) |
219 (mchan->thread << MDC_READ_PORT_CONFIG_WTHREAD_SHIFT);
220 ldesc->read_addr = src;
221 ldesc->write_addr = dst;
222 ldesc->xfer_size = len - 1;
223 ldesc->node_addr = 0;
224 ldesc->cmds_done = 0;
225 ldesc->ctrl_status = MDC_CONTROL_AND_STATUS_LIST_EN |
226 MDC_CONTROL_AND_STATUS_EN;
227 ldesc->next_desc = NULL;
229 if (IS_ALIGNED(dst, mdma->bus_width) &&
230 IS_ALIGNED(src, mdma->bus_width))
231 max_burst = mdma->bus_width * mdma->max_burst_mult;
233 max_burst = mdma->bus_width * (mdma->max_burst_mult - 1);
235 if (dir == DMA_MEM_TO_DEV) {
236 ldesc->gen_conf |= MDC_GENERAL_CONFIG_INC_R;
237 ldesc->readport_conf |= MDC_READ_PORT_CONFIG_DREQ_ENABLE;
238 mdc_set_read_width(ldesc, mdma->bus_width);
239 mdc_set_write_width(ldesc, mchan->config.dst_addr_width);
240 burst_size = min(max_burst, mchan->config.dst_maxburst *
241 mchan->config.dst_addr_width);
242 } else if (dir == DMA_DEV_TO_MEM) {
243 ldesc->gen_conf |= MDC_GENERAL_CONFIG_INC_W;
244 ldesc->readport_conf |= MDC_READ_PORT_CONFIG_DREQ_ENABLE;
245 mdc_set_read_width(ldesc, mchan->config.src_addr_width);
246 mdc_set_write_width(ldesc, mdma->bus_width);
247 burst_size = min(max_burst, mchan->config.src_maxburst *
248 mchan->config.src_addr_width);
250 ldesc->gen_conf |= MDC_GENERAL_CONFIG_INC_R |
251 MDC_GENERAL_CONFIG_INC_W;
252 mdc_set_read_width(ldesc, mdma->bus_width);
253 mdc_set_write_width(ldesc, mdma->bus_width);
254 burst_size = max_burst;
256 ldesc->readport_conf |= (burst_size - 1) <<
257 MDC_READ_PORT_CONFIG_BURST_SIZE_SHIFT;
260 static void mdc_list_desc_free(struct mdc_tx_desc *mdesc)
262 struct mdc_dma *mdma = mdesc->chan->mdma;
263 struct mdc_hw_list_desc *curr, *next;
264 dma_addr_t curr_phys, next_phys;
267 curr_phys = mdesc->list_phys;
269 next = curr->next_desc;
270 next_phys = curr->node_addr;
271 dma_pool_free(mdma->desc_pool, curr, curr_phys);
273 curr_phys = next_phys;
277 static void mdc_desc_free(struct virt_dma_desc *vd)
279 struct mdc_tx_desc *mdesc = to_mdc_desc(&vd->tx);
281 mdc_list_desc_free(mdesc);
285 static struct dma_async_tx_descriptor *mdc_prep_dma_memcpy(
286 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, size_t len,
289 struct mdc_chan *mchan = to_mdc_chan(chan);
290 struct mdc_dma *mdma = mchan->mdma;
291 struct mdc_tx_desc *mdesc;
292 struct mdc_hw_list_desc *curr, *prev = NULL;
293 dma_addr_t curr_phys;
298 mdesc = kzalloc(sizeof(*mdesc), GFP_NOWAIT);
302 mdesc->list_xfer_size = len;
307 curr = dma_pool_alloc(mdma->desc_pool, GFP_NOWAIT, &curr_phys);
312 prev->node_addr = curr_phys;
313 prev->next_desc = curr;
315 mdesc->list_phys = curr_phys;
319 xfer_size = min_t(size_t, mdma->max_xfer_size, len);
321 mdc_list_desc_config(mchan, curr, DMA_MEM_TO_MEM, src, dest,
332 return vchan_tx_prep(&mchan->vc, &mdesc->vd, flags);
335 mdc_desc_free(&mdesc->vd);
340 static int mdc_check_slave_width(struct mdc_chan *mchan,
341 enum dma_transfer_direction dir)
343 enum dma_slave_buswidth width;
345 if (dir == DMA_MEM_TO_DEV)
346 width = mchan->config.dst_addr_width;
348 width = mchan->config.src_addr_width;
351 case DMA_SLAVE_BUSWIDTH_1_BYTE:
352 case DMA_SLAVE_BUSWIDTH_2_BYTES:
353 case DMA_SLAVE_BUSWIDTH_4_BYTES:
354 case DMA_SLAVE_BUSWIDTH_8_BYTES:
360 if (width > mchan->mdma->bus_width)
366 static struct dma_async_tx_descriptor *mdc_prep_dma_cyclic(
367 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
368 size_t period_len, enum dma_transfer_direction dir,
371 struct mdc_chan *mchan = to_mdc_chan(chan);
372 struct mdc_dma *mdma = mchan->mdma;
373 struct mdc_tx_desc *mdesc;
374 struct mdc_hw_list_desc *curr, *prev = NULL;
375 dma_addr_t curr_phys;
377 if (!buf_len && !period_len)
380 if (!is_slave_direction(dir))
383 if (mdc_check_slave_width(mchan, dir) < 0)
386 mdesc = kzalloc(sizeof(*mdesc), GFP_NOWAIT);
390 mdesc->cyclic = true;
391 mdesc->list_xfer_size = buf_len;
392 mdesc->list_period_len = DIV_ROUND_UP(period_len,
393 mdma->max_xfer_size);
395 while (buf_len > 0) {
396 size_t remainder = min(period_len, buf_len);
398 while (remainder > 0) {
401 curr = dma_pool_alloc(mdma->desc_pool, GFP_NOWAIT,
407 mdesc->list_phys = curr_phys;
410 prev->node_addr = curr_phys;
411 prev->next_desc = curr;
414 xfer_size = min_t(size_t, mdma->max_xfer_size,
417 if (dir == DMA_MEM_TO_DEV) {
418 mdc_list_desc_config(mchan, curr, dir,
420 mchan->config.dst_addr,
423 mdc_list_desc_config(mchan, curr, dir,
424 mchan->config.src_addr,
432 buf_addr += xfer_size;
433 buf_len -= xfer_size;
434 remainder -= xfer_size;
437 prev->node_addr = mdesc->list_phys;
439 return vchan_tx_prep(&mchan->vc, &mdesc->vd, flags);
442 mdc_desc_free(&mdesc->vd);
447 static struct dma_async_tx_descriptor *mdc_prep_slave_sg(
448 struct dma_chan *chan, struct scatterlist *sgl,
449 unsigned int sg_len, enum dma_transfer_direction dir,
450 unsigned long flags, void *context)
452 struct mdc_chan *mchan = to_mdc_chan(chan);
453 struct mdc_dma *mdma = mchan->mdma;
454 struct mdc_tx_desc *mdesc;
455 struct scatterlist *sg;
456 struct mdc_hw_list_desc *curr, *prev = NULL;
457 dma_addr_t curr_phys;
463 if (!is_slave_direction(dir))
466 if (mdc_check_slave_width(mchan, dir) < 0)
469 mdesc = kzalloc(sizeof(*mdesc), GFP_NOWAIT);
474 for_each_sg(sgl, sg, sg_len, i) {
475 dma_addr_t buf = sg_dma_address(sg);
476 size_t buf_len = sg_dma_len(sg);
478 while (buf_len > 0) {
481 curr = dma_pool_alloc(mdma->desc_pool, GFP_NOWAIT,
487 mdesc->list_phys = curr_phys;
490 prev->node_addr = curr_phys;
491 prev->next_desc = curr;
494 xfer_size = min_t(size_t, mdma->max_xfer_size,
497 if (dir == DMA_MEM_TO_DEV) {
498 mdc_list_desc_config(mchan, curr, dir, buf,
499 mchan->config.dst_addr,
502 mdc_list_desc_config(mchan, curr, dir,
503 mchan->config.src_addr,
510 mdesc->list_xfer_size += xfer_size;
512 buf_len -= xfer_size;
516 return vchan_tx_prep(&mchan->vc, &mdesc->vd, flags);
519 mdc_desc_free(&mdesc->vd);
524 static void mdc_issue_desc(struct mdc_chan *mchan)
526 struct mdc_dma *mdma = mchan->mdma;
527 struct virt_dma_desc *vd;
528 struct mdc_tx_desc *mdesc;
531 vd = vchan_next_desc(&mchan->vc);
537 mdesc = to_mdc_desc(&vd->tx);
540 dev_dbg(mdma2dev(mdma), "Issuing descriptor on channel %d\n",
543 mdma->soc->enable_chan(mchan);
545 val = mdc_chan_readl(mchan, MDC_GENERAL_CONFIG);
546 val |= MDC_GENERAL_CONFIG_LIST_IEN | MDC_GENERAL_CONFIG_IEN |
547 MDC_GENERAL_CONFIG_LEVEL_INT | MDC_GENERAL_CONFIG_PHYSICAL_W |
548 MDC_GENERAL_CONFIG_PHYSICAL_R;
549 mdc_chan_writel(mchan, val, MDC_GENERAL_CONFIG);
550 val = (mchan->thread << MDC_READ_PORT_CONFIG_STHREAD_SHIFT) |
551 (mchan->thread << MDC_READ_PORT_CONFIG_RTHREAD_SHIFT) |
552 (mchan->thread << MDC_READ_PORT_CONFIG_WTHREAD_SHIFT);
553 mdc_chan_writel(mchan, val, MDC_READ_PORT_CONFIG);
554 mdc_chan_writel(mchan, mdesc->list_phys, MDC_LIST_NODE_ADDRESS);
555 val = mdc_chan_readl(mchan, MDC_CONTROL_AND_STATUS);
556 val |= MDC_CONTROL_AND_STATUS_LIST_EN;
557 mdc_chan_writel(mchan, val, MDC_CONTROL_AND_STATUS);
560 static void mdc_issue_pending(struct dma_chan *chan)
562 struct mdc_chan *mchan = to_mdc_chan(chan);
565 spin_lock_irqsave(&mchan->vc.lock, flags);
566 if (vchan_issue_pending(&mchan->vc) && !mchan->desc)
567 mdc_issue_desc(mchan);
568 spin_unlock_irqrestore(&mchan->vc.lock, flags);
571 static enum dma_status mdc_tx_status(struct dma_chan *chan,
572 dma_cookie_t cookie, struct dma_tx_state *txstate)
574 struct mdc_chan *mchan = to_mdc_chan(chan);
575 struct mdc_tx_desc *mdesc;
576 struct virt_dma_desc *vd;
581 ret = dma_cookie_status(chan, cookie, txstate);
582 if (ret == DMA_COMPLETE)
588 spin_lock_irqsave(&mchan->vc.lock, flags);
589 vd = vchan_find_desc(&mchan->vc, cookie);
591 mdesc = to_mdc_desc(&vd->tx);
592 bytes = mdesc->list_xfer_size;
593 } else if (mchan->desc && mchan->desc->vd.tx.cookie == cookie) {
594 struct mdc_hw_list_desc *ldesc;
595 u32 val1, val2, done, processed, residue;
601 * Determine the number of commands that haven't been
602 * processed (handled by the IRQ handler) yet.
605 val1 = mdc_chan_readl(mchan, MDC_CMDS_PROCESSED) &
606 ~MDC_CMDS_PROCESSED_INT_ACTIVE;
607 residue = mdc_chan_readl(mchan,
608 MDC_ACTIVE_TRANSFER_SIZE);
609 val2 = mdc_chan_readl(mchan, MDC_CMDS_PROCESSED) &
610 ~MDC_CMDS_PROCESSED_INT_ACTIVE;
611 } while (val1 != val2);
613 done = (val1 >> MDC_CMDS_PROCESSED_CMDS_DONE_SHIFT) &
614 MDC_CMDS_PROCESSED_CMDS_DONE_MASK;
615 processed = (val1 >> MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT) &
616 MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK;
617 cmds = (done - processed) %
618 (MDC_CMDS_PROCESSED_CMDS_DONE_MASK + 1);
621 * If the command loaded event hasn't been processed yet, then
622 * the difference above includes an extra command.
624 if (!mdesc->cmd_loaded)
627 cmds += mdesc->list_cmds_done;
629 bytes = mdesc->list_xfer_size;
631 for (i = 0; i < cmds; i++) {
632 bytes -= ldesc->xfer_size + 1;
633 ldesc = ldesc->next_desc;
636 if (residue != MDC_TRANSFER_SIZE_MASK)
637 bytes -= ldesc->xfer_size - residue;
639 bytes -= ldesc->xfer_size + 1;
642 spin_unlock_irqrestore(&mchan->vc.lock, flags);
644 dma_set_residue(txstate, bytes);
649 static unsigned int mdc_get_new_events(struct mdc_chan *mchan)
651 u32 val, processed, done1, done2;
654 val = mdc_chan_readl(mchan, MDC_CMDS_PROCESSED);
655 processed = (val >> MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT) &
656 MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK;
658 * CMDS_DONE may have incremented between reading CMDS_PROCESSED
659 * and clearing INT_ACTIVE. Re-read CMDS_PROCESSED to ensure we
660 * didn't miss a command completion.
663 val = mdc_chan_readl(mchan, MDC_CMDS_PROCESSED);
665 done1 = (val >> MDC_CMDS_PROCESSED_CMDS_DONE_SHIFT) &
666 MDC_CMDS_PROCESSED_CMDS_DONE_MASK;
668 val &= ~((MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK <<
669 MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT) |
670 MDC_CMDS_PROCESSED_INT_ACTIVE);
672 val |= done1 << MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT;
674 mdc_chan_writel(mchan, val, MDC_CMDS_PROCESSED);
676 val = mdc_chan_readl(mchan, MDC_CMDS_PROCESSED);
678 done2 = (val >> MDC_CMDS_PROCESSED_CMDS_DONE_SHIFT) &
679 MDC_CMDS_PROCESSED_CMDS_DONE_MASK;
680 } while (done1 != done2);
682 if (done1 >= processed)
683 ret = done1 - processed;
685 ret = ((MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK + 1) -
691 static int mdc_terminate_all(struct dma_chan *chan)
693 struct mdc_chan *mchan = to_mdc_chan(chan);
697 spin_lock_irqsave(&mchan->vc.lock, flags);
699 mdc_chan_writel(mchan, MDC_CONTROL_AND_STATUS_CANCEL,
700 MDC_CONTROL_AND_STATUS);
703 vchan_terminate_vdesc(&mchan->desc->vd);
706 vchan_get_all_descriptors(&mchan->vc, &head);
708 mdc_get_new_events(mchan);
710 spin_unlock_irqrestore(&mchan->vc.lock, flags);
712 vchan_dma_desc_free_list(&mchan->vc, &head);
717 static void mdc_synchronize(struct dma_chan *chan)
719 struct mdc_chan *mchan = to_mdc_chan(chan);
721 vchan_synchronize(&mchan->vc);
724 static int mdc_slave_config(struct dma_chan *chan,
725 struct dma_slave_config *config)
727 struct mdc_chan *mchan = to_mdc_chan(chan);
730 spin_lock_irqsave(&mchan->vc.lock, flags);
731 mchan->config = *config;
732 spin_unlock_irqrestore(&mchan->vc.lock, flags);
737 static int mdc_alloc_chan_resources(struct dma_chan *chan)
739 struct mdc_chan *mchan = to_mdc_chan(chan);
740 struct device *dev = mdma2dev(mchan->mdma);
742 return pm_runtime_get_sync(dev);
745 static void mdc_free_chan_resources(struct dma_chan *chan)
747 struct mdc_chan *mchan = to_mdc_chan(chan);
748 struct mdc_dma *mdma = mchan->mdma;
749 struct device *dev = mdma2dev(mdma);
751 mdc_terminate_all(chan);
752 mdma->soc->disable_chan(mchan);
756 static irqreturn_t mdc_chan_irq(int irq, void *dev_id)
758 struct mdc_chan *mchan = (struct mdc_chan *)dev_id;
759 struct mdc_tx_desc *mdesc;
760 unsigned int i, new_events;
762 spin_lock(&mchan->vc.lock);
764 dev_dbg(mdma2dev(mchan->mdma), "IRQ on channel %d\n", mchan->chan_nr);
766 new_events = mdc_get_new_events(mchan);
773 dev_warn(mdma2dev(mchan->mdma),
774 "IRQ with no active descriptor on channel %d\n",
779 for (i = 0; i < new_events; i++) {
781 * The first interrupt in a transfer indicates that the
782 * command list has been loaded, not that a command has
785 if (!mdesc->cmd_loaded) {
786 mdesc->cmd_loaded = true;
790 mdesc->list_cmds_done++;
792 mdesc->list_cmds_done %= mdesc->list_len;
793 if (mdesc->list_cmds_done % mdesc->list_period_len == 0)
794 vchan_cyclic_callback(&mdesc->vd);
795 } else if (mdesc->list_cmds_done == mdesc->list_len) {
797 vchan_cookie_complete(&mdesc->vd);
798 mdc_issue_desc(mchan);
803 spin_unlock(&mchan->vc.lock);
808 static struct dma_chan *mdc_of_xlate(struct of_phandle_args *dma_spec,
809 struct of_dma *ofdma)
811 struct mdc_dma *mdma = ofdma->of_dma_data;
812 struct dma_chan *chan;
814 if (dma_spec->args_count != 3)
817 list_for_each_entry(chan, &mdma->dma_dev.channels, device_node) {
818 struct mdc_chan *mchan = to_mdc_chan(chan);
820 if (!(dma_spec->args[1] & BIT(mchan->chan_nr)))
822 if (dma_get_slave_channel(chan)) {
823 mchan->periph = dma_spec->args[0];
824 mchan->thread = dma_spec->args[2];
832 #define PISTACHIO_CR_PERIPH_DMA_ROUTE(ch) (0x120 + 0x4 * ((ch) / 4))
833 #define PISTACHIO_CR_PERIPH_DMA_ROUTE_SHIFT(ch) (8 * ((ch) % 4))
834 #define PISTACHIO_CR_PERIPH_DMA_ROUTE_MASK 0x3f
836 static void pistachio_mdc_enable_chan(struct mdc_chan *mchan)
838 struct mdc_dma *mdma = mchan->mdma;
840 regmap_update_bits(mdma->periph_regs,
841 PISTACHIO_CR_PERIPH_DMA_ROUTE(mchan->chan_nr),
842 PISTACHIO_CR_PERIPH_DMA_ROUTE_MASK <<
843 PISTACHIO_CR_PERIPH_DMA_ROUTE_SHIFT(mchan->chan_nr),
845 PISTACHIO_CR_PERIPH_DMA_ROUTE_SHIFT(mchan->chan_nr));
848 static void pistachio_mdc_disable_chan(struct mdc_chan *mchan)
850 struct mdc_dma *mdma = mchan->mdma;
852 regmap_update_bits(mdma->periph_regs,
853 PISTACHIO_CR_PERIPH_DMA_ROUTE(mchan->chan_nr),
854 PISTACHIO_CR_PERIPH_DMA_ROUTE_MASK <<
855 PISTACHIO_CR_PERIPH_DMA_ROUTE_SHIFT(mchan->chan_nr),
859 static const struct mdc_dma_soc_data pistachio_mdc_data = {
860 .enable_chan = pistachio_mdc_enable_chan,
861 .disable_chan = pistachio_mdc_disable_chan,
864 static const struct of_device_id mdc_dma_of_match[] = {
865 { .compatible = "img,pistachio-mdc-dma", .data = &pistachio_mdc_data, },
868 MODULE_DEVICE_TABLE(of, mdc_dma_of_match);
870 static int img_mdc_runtime_suspend(struct device *dev)
872 struct mdc_dma *mdma = dev_get_drvdata(dev);
874 clk_disable_unprepare(mdma->clk);
879 static int img_mdc_runtime_resume(struct device *dev)
881 struct mdc_dma *mdma = dev_get_drvdata(dev);
883 return clk_prepare_enable(mdma->clk);
886 static int mdc_dma_probe(struct platform_device *pdev)
888 struct mdc_dma *mdma;
889 struct resource *res;
894 mdma = devm_kzalloc(&pdev->dev, sizeof(*mdma), GFP_KERNEL);
897 platform_set_drvdata(pdev, mdma);
899 mdma->soc = of_device_get_match_data(&pdev->dev);
901 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
902 mdma->regs = devm_ioremap_resource(&pdev->dev, res);
903 if (IS_ERR(mdma->regs))
904 return PTR_ERR(mdma->regs);
906 mdma->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
908 if (IS_ERR(mdma->periph_regs))
909 return PTR_ERR(mdma->periph_regs);
911 mdma->clk = devm_clk_get(&pdev->dev, "sys");
912 if (IS_ERR(mdma->clk))
913 return PTR_ERR(mdma->clk);
915 dma_cap_zero(mdma->dma_dev.cap_mask);
916 dma_cap_set(DMA_SLAVE, mdma->dma_dev.cap_mask);
917 dma_cap_set(DMA_PRIVATE, mdma->dma_dev.cap_mask);
918 dma_cap_set(DMA_CYCLIC, mdma->dma_dev.cap_mask);
919 dma_cap_set(DMA_MEMCPY, mdma->dma_dev.cap_mask);
921 val = mdc_readl(mdma, MDC_GLOBAL_CONFIG_A);
922 mdma->nr_channels = (val >> MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_SHIFT) &
923 MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_MASK;
925 1 << ((val >> MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_SHIFT) &
926 MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_MASK);
928 (1 << ((val >> MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_SHIFT) &
929 MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_MASK)) / 8;
931 * Although transfer sizes of up to MDC_TRANSFER_SIZE_MASK + 1 bytes
932 * are supported, this makes it possible for the value reported in
933 * MDC_ACTIVE_TRANSFER_SIZE to be ambiguous - an active transfer size
934 * of MDC_TRANSFER_SIZE_MASK may indicate either that 0 bytes or
935 * MDC_TRANSFER_SIZE_MASK + 1 bytes are remaining. To eliminate this
936 * ambiguity, restrict transfer sizes to one bus-width less than the
939 mdma->max_xfer_size = MDC_TRANSFER_SIZE_MASK + 1 - mdma->bus_width;
941 of_property_read_u32(pdev->dev.of_node, "dma-channels",
943 ret = of_property_read_u32(pdev->dev.of_node,
944 "img,max-burst-multiplier",
945 &mdma->max_burst_mult);
949 mdma->dma_dev.dev = &pdev->dev;
950 mdma->dma_dev.device_prep_slave_sg = mdc_prep_slave_sg;
951 mdma->dma_dev.device_prep_dma_cyclic = mdc_prep_dma_cyclic;
952 mdma->dma_dev.device_prep_dma_memcpy = mdc_prep_dma_memcpy;
953 mdma->dma_dev.device_alloc_chan_resources = mdc_alloc_chan_resources;
954 mdma->dma_dev.device_free_chan_resources = mdc_free_chan_resources;
955 mdma->dma_dev.device_tx_status = mdc_tx_status;
956 mdma->dma_dev.device_issue_pending = mdc_issue_pending;
957 mdma->dma_dev.device_terminate_all = mdc_terminate_all;
958 mdma->dma_dev.device_synchronize = mdc_synchronize;
959 mdma->dma_dev.device_config = mdc_slave_config;
961 mdma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
962 mdma->dma_dev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
963 for (i = 1; i <= mdma->bus_width; i <<= 1) {
964 mdma->dma_dev.src_addr_widths |= BIT(i);
965 mdma->dma_dev.dst_addr_widths |= BIT(i);
968 INIT_LIST_HEAD(&mdma->dma_dev.channels);
969 for (i = 0; i < mdma->nr_channels; i++) {
970 struct mdc_chan *mchan = &mdma->channels[i];
974 mchan->irq = platform_get_irq(pdev, i);
978 ret = devm_request_irq(&pdev->dev, mchan->irq, mdc_chan_irq,
980 dev_name(&pdev->dev), mchan);
984 mchan->vc.desc_free = mdc_desc_free;
985 vchan_init(&mchan->vc, &mdma->dma_dev);
988 mdma->desc_pool = dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
989 sizeof(struct mdc_hw_list_desc),
991 if (!mdma->desc_pool)
994 pm_runtime_enable(&pdev->dev);
995 if (!pm_runtime_enabled(&pdev->dev)) {
996 ret = img_mdc_runtime_resume(&pdev->dev);
1001 ret = dma_async_device_register(&mdma->dma_dev);
1005 ret = of_dma_controller_register(pdev->dev.of_node, mdc_of_xlate, mdma);
1009 dev_info(&pdev->dev, "MDC with %u channels and %u threads\n",
1010 mdma->nr_channels, mdma->nr_threads);
1015 dma_async_device_unregister(&mdma->dma_dev);
1017 if (!pm_runtime_enabled(&pdev->dev))
1018 img_mdc_runtime_suspend(&pdev->dev);
1019 pm_runtime_disable(&pdev->dev);
1023 static int mdc_dma_remove(struct platform_device *pdev)
1025 struct mdc_dma *mdma = platform_get_drvdata(pdev);
1026 struct mdc_chan *mchan, *next;
1028 of_dma_controller_free(pdev->dev.of_node);
1029 dma_async_device_unregister(&mdma->dma_dev);
1031 list_for_each_entry_safe(mchan, next, &mdma->dma_dev.channels,
1032 vc.chan.device_node) {
1033 list_del(&mchan->vc.chan.device_node);
1035 devm_free_irq(&pdev->dev, mchan->irq, mchan);
1037 tasklet_kill(&mchan->vc.task);
1040 pm_runtime_disable(&pdev->dev);
1041 if (!pm_runtime_status_suspended(&pdev->dev))
1042 img_mdc_runtime_suspend(&pdev->dev);
1047 #ifdef CONFIG_PM_SLEEP
1048 static int img_mdc_suspend_late(struct device *dev)
1050 struct mdc_dma *mdma = dev_get_drvdata(dev);
1053 /* Check that all channels are idle */
1054 for (i = 0; i < mdma->nr_channels; i++) {
1055 struct mdc_chan *mchan = &mdma->channels[i];
1057 if (unlikely(mchan->desc))
1061 return pm_runtime_force_suspend(dev);
1064 static int img_mdc_resume_early(struct device *dev)
1066 return pm_runtime_force_resume(dev);
1068 #endif /* CONFIG_PM_SLEEP */
1070 static const struct dev_pm_ops img_mdc_pm_ops = {
1071 SET_RUNTIME_PM_OPS(img_mdc_runtime_suspend,
1072 img_mdc_runtime_resume, NULL)
1073 SET_LATE_SYSTEM_SLEEP_PM_OPS(img_mdc_suspend_late,
1074 img_mdc_resume_early)
1077 static struct platform_driver mdc_dma_driver = {
1079 .name = "img-mdc-dma",
1080 .pm = &img_mdc_pm_ops,
1081 .of_match_table = of_match_ptr(mdc_dma_of_match),
1083 .probe = mdc_dma_probe,
1084 .remove = mdc_dma_remove,
1086 module_platform_driver(mdc_dma_driver);
1088 MODULE_DESCRIPTION("IMG Multi-threaded DMA Controller (MDC) driver");
1089 MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
1090 MODULE_LICENSE("GPL v2");