1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI EDMA DMA engine driver
5 * Copyright 2012 Texas Instruments
8 #include <linux/dmaengine.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/bitmap.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
20 #include <linux/of_dma.h>
21 #include <linux/of_irq.h>
22 #include <linux/of_address.h>
23 #include <linux/of_device.h>
24 #include <linux/pm_runtime.h>
26 #include <linux/platform_data/edma.h>
28 #include "../dmaengine.h"
29 #include "../virt-dma.h"
31 /* Offsets matching "struct edmacc_param" */
34 #define PARM_A_B_CNT 0x08
36 #define PARM_SRC_DST_BIDX 0x10
37 #define PARM_LINK_BCNTRLD 0x14
38 #define PARM_SRC_DST_CIDX 0x18
39 #define PARM_CCNT 0x1c
41 #define PARM_SIZE 0x20
43 /* Offsets for EDMA CC global channel registers and their shadows */
44 #define SH_ER 0x00 /* 64 bits */
45 #define SH_ECR 0x08 /* 64 bits */
46 #define SH_ESR 0x10 /* 64 bits */
47 #define SH_CER 0x18 /* 64 bits */
48 #define SH_EER 0x20 /* 64 bits */
49 #define SH_EECR 0x28 /* 64 bits */
50 #define SH_EESR 0x30 /* 64 bits */
51 #define SH_SER 0x38 /* 64 bits */
52 #define SH_SECR 0x40 /* 64 bits */
53 #define SH_IER 0x50 /* 64 bits */
54 #define SH_IECR 0x58 /* 64 bits */
55 #define SH_IESR 0x60 /* 64 bits */
56 #define SH_IPR 0x68 /* 64 bits */
57 #define SH_ICR 0x70 /* 64 bits */
67 /* Offsets for EDMA CC global registers */
68 #define EDMA_REV 0x0000
69 #define EDMA_CCCFG 0x0004
70 #define EDMA_QCHMAP 0x0200 /* 8 registers */
71 #define EDMA_DMAQNUM 0x0240 /* 8 registers (4 on OMAP-L1xx) */
72 #define EDMA_QDMAQNUM 0x0260
73 #define EDMA_QUETCMAP 0x0280
74 #define EDMA_QUEPRI 0x0284
75 #define EDMA_EMR 0x0300 /* 64 bits */
76 #define EDMA_EMCR 0x0308 /* 64 bits */
77 #define EDMA_QEMR 0x0310
78 #define EDMA_QEMCR 0x0314
79 #define EDMA_CCERR 0x0318
80 #define EDMA_CCERRCLR 0x031c
81 #define EDMA_EEVAL 0x0320
82 #define EDMA_DRAE 0x0340 /* 4 x 64 bits*/
83 #define EDMA_QRAE 0x0380 /* 4 registers */
84 #define EDMA_QUEEVTENTRY 0x0400 /* 2 x 16 registers */
85 #define EDMA_QSTAT 0x0600 /* 2 registers */
86 #define EDMA_QWMTHRA 0x0620
87 #define EDMA_QWMTHRB 0x0624
88 #define EDMA_CCSTAT 0x0640
90 #define EDMA_M 0x1000 /* global channel registers */
91 #define EDMA_ECR 0x1008
92 #define EDMA_ECRH 0x100C
93 #define EDMA_SHADOW0 0x2000 /* 4 shadow regions */
94 #define EDMA_PARM 0x4000 /* PaRAM entries */
96 #define PARM_OFFSET(param_no) (EDMA_PARM + ((param_no) << 5))
98 #define EDMA_DCHMAP 0x0100 /* 64 registers */
101 #define GET_NUM_DMACH(x) (x & 0x7) /* bits 0-2 */
102 #define GET_NUM_QDMACH(x) ((x & 0x70) >> 4) /* bits 4-6 */
103 #define GET_NUM_PAENTRY(x) ((x & 0x7000) >> 12) /* bits 12-14 */
104 #define GET_NUM_EVQUE(x) ((x & 0x70000) >> 16) /* bits 16-18 */
105 #define GET_NUM_REGN(x) ((x & 0x300000) >> 20) /* bits 20-21 */
106 #define CHMAP_EXIST BIT(24)
108 /* CCSTAT register */
109 #define EDMA_CCSTAT_ACTV BIT(4)
112 * Max of 20 segments per channel to conserve PaRAM slots
113 * Also note that MAX_NR_SG should be at least the no.of periods
114 * that are required for ASoC, otherwise DMA prep calls will
115 * fail. Today davinci-pcm is the only user of this driver and
116 * requires at least 17 slots, so we setup the default to 20.
119 #define EDMA_MAX_SLOTS MAX_NR_SG
120 #define EDMA_DESCRIPTORS 16
122 #define EDMA_CHANNEL_ANY -1 /* for edma_alloc_channel() */
123 #define EDMA_SLOT_ANY -1 /* for edma_alloc_slot() */
124 #define EDMA_CONT_PARAMS_ANY 1001
125 #define EDMA_CONT_PARAMS_FIXED_EXACT 1002
126 #define EDMA_CONT_PARAMS_FIXED_NOT_EXACT 1003
129 * 64bit array registers are split into two 32bit registers:
130 * reg0: channel/event 0-31
131 * reg1: channel/event 32-63
133 * bit 5 in the channel number tells the array index (0/1)
134 * bit 0-4 (0x1f) is the bit offset within the register
136 #define EDMA_REG_ARRAY_INDEX(channel) ((channel) >> 5)
137 #define EDMA_CHANNEL_BIT(channel) (BIT((channel) & 0x1f))
139 /* PaRAM slots are laid out like this */
140 struct edmacc_param {
151 /* fields in edmacc_param.opt */
154 #define SYNCDIM BIT(2)
155 #define STATIC BIT(3)
156 #define EDMA_FWID (0x07 << 8)
157 #define TCCMODE BIT(11)
158 #define EDMA_TCC(t) ((t) << 12)
159 #define TCINTEN BIT(20)
160 #define ITCINTEN BIT(21)
161 #define TCCHEN BIT(22)
162 #define ITCCHEN BIT(23)
167 struct edmacc_param param;
171 struct virt_dma_desc vdesc;
172 struct list_head node;
173 enum dma_transfer_direction direction;
178 struct edma_chan *echan;
182 * The following 4 elements are used for residue accounting.
184 * - processed_stat: the number of SG elements we have traversed
185 * so far to cover accounting. This is updated directly to processed
186 * during edma_callback and is always <= processed, because processed
187 * refers to the number of pending transfer (programmed to EDMA
188 * controller), where as processed_stat tracks number of transfers
189 * accounted for so far.
191 * - residue: The amount of bytes we have left to transfer for this desc
193 * - residue_stat: The residue in bytes of data we have covered
194 * so far for accounting. This is updated directly to residue
195 * during callbacks to keep it current.
197 * - sg_len: Tracks the length of the current intermediate transfer,
198 * this is required to update the residue during intermediate transfer
199 * completion callback.
206 struct edma_pset pset[];
212 struct device_node *node;
217 struct virt_dma_chan vchan;
218 struct list_head node;
219 struct edma_desc *edesc;
225 int slot[EDMA_MAX_SLOTS];
227 struct dma_slave_config cfg;
232 struct edma_soc_info *info;
237 /* eDMA3 resource information */
238 unsigned num_channels;
239 unsigned num_qchannels;
244 enum dma_event_q default_queue;
247 unsigned int ccerrint;
250 * The slot_inuse bit for each PaRAM slot is clear unless the slot is
251 * in use by Linux or if it is allocated to be used by DSP.
253 unsigned long *slot_inuse;
256 * For tracking reserved channels used by DSP.
257 * If the bit is cleared, the channel is allocated to be used by DSP
258 * and Linux must not touch it.
260 unsigned long *channels_mask;
262 struct dma_device dma_slave;
263 struct dma_device *dma_memcpy;
264 struct edma_chan *slave_chans;
265 struct edma_tc *tc_list;
269 /* dummy param set used to (re)initialize parameter RAM slots */
270 static const struct edmacc_param dummy_paramset = {
271 .link_bcntrld = 0xffff,
275 #define EDMA_BINDING_LEGACY 0
276 #define EDMA_BINDING_TPCC 1
277 static const u32 edma_binding_type[] = {
278 [EDMA_BINDING_LEGACY] = EDMA_BINDING_LEGACY,
279 [EDMA_BINDING_TPCC] = EDMA_BINDING_TPCC,
282 static const struct of_device_id edma_of_ids[] = {
284 .compatible = "ti,edma3",
285 .data = &edma_binding_type[EDMA_BINDING_LEGACY],
288 .compatible = "ti,edma3-tpcc",
289 .data = &edma_binding_type[EDMA_BINDING_TPCC],
293 MODULE_DEVICE_TABLE(of, edma_of_ids);
295 static const struct of_device_id edma_tptc_of_ids[] = {
296 { .compatible = "ti,edma3-tptc", },
299 MODULE_DEVICE_TABLE(of, edma_tptc_of_ids);
301 static inline unsigned int edma_read(struct edma_cc *ecc, int offset)
303 return (unsigned int)__raw_readl(ecc->base + offset);
306 static inline void edma_write(struct edma_cc *ecc, int offset, int val)
308 __raw_writel(val, ecc->base + offset);
311 static inline void edma_modify(struct edma_cc *ecc, int offset, unsigned and,
314 unsigned val = edma_read(ecc, offset);
318 edma_write(ecc, offset, val);
321 static inline void edma_or(struct edma_cc *ecc, int offset, unsigned or)
323 unsigned val = edma_read(ecc, offset);
326 edma_write(ecc, offset, val);
329 static inline unsigned int edma_read_array(struct edma_cc *ecc, int offset,
332 return edma_read(ecc, offset + (i << 2));
335 static inline void edma_write_array(struct edma_cc *ecc, int offset, int i,
338 edma_write(ecc, offset + (i << 2), val);
341 static inline void edma_modify_array(struct edma_cc *ecc, int offset, int i,
342 unsigned and, unsigned or)
344 edma_modify(ecc, offset + (i << 2), and, or);
347 static inline void edma_or_array2(struct edma_cc *ecc, int offset, int i, int j,
350 edma_or(ecc, offset + ((i * 2 + j) << 2), or);
353 static inline void edma_write_array2(struct edma_cc *ecc, int offset, int i,
356 edma_write(ecc, offset + ((i * 2 + j) << 2), val);
359 static inline unsigned int edma_shadow0_read_array(struct edma_cc *ecc,
362 return edma_read(ecc, EDMA_SHADOW0 + offset + (i << 2));
365 static inline void edma_shadow0_write(struct edma_cc *ecc, int offset,
368 edma_write(ecc, EDMA_SHADOW0 + offset, val);
371 static inline void edma_shadow0_write_array(struct edma_cc *ecc, int offset,
374 edma_write(ecc, EDMA_SHADOW0 + offset + (i << 2), val);
377 static inline void edma_param_modify(struct edma_cc *ecc, int offset,
378 int param_no, unsigned and, unsigned or)
380 edma_modify(ecc, EDMA_PARM + offset + (param_no << 5), and, or);
383 static void edma_assign_priority_to_queue(struct edma_cc *ecc, int queue_no,
386 int bit = queue_no * 4;
388 edma_modify(ecc, EDMA_QUEPRI, ~(0x7 << bit), ((priority & 0x7) << bit));
391 static void edma_set_chmap(struct edma_chan *echan, int slot)
393 struct edma_cc *ecc = echan->ecc;
394 int channel = EDMA_CHAN_SLOT(echan->ch_num);
396 if (ecc->chmap_exist) {
397 slot = EDMA_CHAN_SLOT(slot);
398 edma_write_array(ecc, EDMA_DCHMAP, channel, (slot << 5));
402 static void edma_setup_interrupt(struct edma_chan *echan, bool enable)
404 struct edma_cc *ecc = echan->ecc;
405 int channel = EDMA_CHAN_SLOT(echan->ch_num);
406 int idx = EDMA_REG_ARRAY_INDEX(channel);
407 int ch_bit = EDMA_CHANNEL_BIT(channel);
410 edma_shadow0_write_array(ecc, SH_ICR, idx, ch_bit);
411 edma_shadow0_write_array(ecc, SH_IESR, idx, ch_bit);
413 edma_shadow0_write_array(ecc, SH_IECR, idx, ch_bit);
418 * paRAM slot management functions
420 static void edma_write_slot(struct edma_cc *ecc, unsigned slot,
421 const struct edmacc_param *param)
423 slot = EDMA_CHAN_SLOT(slot);
424 if (slot >= ecc->num_slots)
426 memcpy_toio(ecc->base + PARM_OFFSET(slot), param, PARM_SIZE);
429 static int edma_read_slot(struct edma_cc *ecc, unsigned slot,
430 struct edmacc_param *param)
432 slot = EDMA_CHAN_SLOT(slot);
433 if (slot >= ecc->num_slots)
435 memcpy_fromio(param, ecc->base + PARM_OFFSET(slot), PARM_SIZE);
441 * edma_alloc_slot - allocate DMA parameter RAM
442 * @ecc: pointer to edma_cc struct
443 * @slot: specific slot to allocate; negative for "any unused slot"
445 * This allocates a parameter RAM slot, initializing it to hold a
446 * dummy transfer. Slots allocated using this routine have not been
447 * mapped to a hardware DMA channel, and will normally be used by
448 * linking to them from a slot associated with a DMA channel.
450 * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
451 * slots may be allocated on behalf of DSP firmware.
453 * Returns the number of the slot, else negative errno.
455 static int edma_alloc_slot(struct edma_cc *ecc, int slot)
458 slot = EDMA_CHAN_SLOT(slot);
459 /* Requesting entry paRAM slot for a HW triggered channel. */
460 if (ecc->chmap_exist && slot < ecc->num_channels)
461 slot = EDMA_SLOT_ANY;
465 if (ecc->chmap_exist)
468 slot = ecc->num_channels;
470 slot = find_next_zero_bit(ecc->slot_inuse,
473 if (slot == ecc->num_slots)
475 if (!test_and_set_bit(slot, ecc->slot_inuse))
478 } else if (slot >= ecc->num_slots) {
480 } else if (test_and_set_bit(slot, ecc->slot_inuse)) {
484 edma_write_slot(ecc, slot, &dummy_paramset);
486 return EDMA_CTLR_CHAN(ecc->id, slot);
489 static void edma_free_slot(struct edma_cc *ecc, unsigned slot)
491 slot = EDMA_CHAN_SLOT(slot);
492 if (slot >= ecc->num_slots)
495 edma_write_slot(ecc, slot, &dummy_paramset);
496 clear_bit(slot, ecc->slot_inuse);
500 * edma_link - link one parameter RAM slot to another
501 * @ecc: pointer to edma_cc struct
502 * @from: parameter RAM slot originating the link
503 * @to: parameter RAM slot which is the link target
505 * The originating slot should not be part of any active DMA transfer.
507 static void edma_link(struct edma_cc *ecc, unsigned from, unsigned to)
509 if (unlikely(EDMA_CTLR(from) != EDMA_CTLR(to)))
510 dev_warn(ecc->dev, "Ignoring eDMA instance for linking\n");
512 from = EDMA_CHAN_SLOT(from);
513 to = EDMA_CHAN_SLOT(to);
514 if (from >= ecc->num_slots || to >= ecc->num_slots)
517 edma_param_modify(ecc, PARM_LINK_BCNTRLD, from, 0xffff0000,
522 * edma_get_position - returns the current transfer point
523 * @ecc: pointer to edma_cc struct
524 * @slot: parameter RAM slot being examined
525 * @dst: true selects the dest position, false the source
527 * Returns the position of the current active slot
529 static dma_addr_t edma_get_position(struct edma_cc *ecc, unsigned slot,
534 slot = EDMA_CHAN_SLOT(slot);
535 offs = PARM_OFFSET(slot);
536 offs += dst ? PARM_DST : PARM_SRC;
538 return edma_read(ecc, offs);
542 * Channels with event associations will be triggered by their hardware
543 * events, and channels without such associations will be triggered by
544 * software. (At this writing there is no interface for using software
545 * triggers except with channels that don't support hardware triggers.)
547 static void edma_start(struct edma_chan *echan)
549 struct edma_cc *ecc = echan->ecc;
550 int channel = EDMA_CHAN_SLOT(echan->ch_num);
551 int idx = EDMA_REG_ARRAY_INDEX(channel);
552 int ch_bit = EDMA_CHANNEL_BIT(channel);
554 if (!echan->hw_triggered) {
555 /* EDMA channels without event association */
556 dev_dbg(ecc->dev, "ESR%d %08x\n", idx,
557 edma_shadow0_read_array(ecc, SH_ESR, idx));
558 edma_shadow0_write_array(ecc, SH_ESR, idx, ch_bit);
560 /* EDMA channel with event association */
561 dev_dbg(ecc->dev, "ER%d %08x\n", idx,
562 edma_shadow0_read_array(ecc, SH_ER, idx));
563 /* Clear any pending event or error */
564 edma_write_array(ecc, EDMA_ECR, idx, ch_bit);
565 edma_write_array(ecc, EDMA_EMCR, idx, ch_bit);
567 edma_shadow0_write_array(ecc, SH_SECR, idx, ch_bit);
568 edma_shadow0_write_array(ecc, SH_EESR, idx, ch_bit);
569 dev_dbg(ecc->dev, "EER%d %08x\n", idx,
570 edma_shadow0_read_array(ecc, SH_EER, idx));
574 static void edma_stop(struct edma_chan *echan)
576 struct edma_cc *ecc = echan->ecc;
577 int channel = EDMA_CHAN_SLOT(echan->ch_num);
578 int idx = EDMA_REG_ARRAY_INDEX(channel);
579 int ch_bit = EDMA_CHANNEL_BIT(channel);
581 edma_shadow0_write_array(ecc, SH_EECR, idx, ch_bit);
582 edma_shadow0_write_array(ecc, SH_ECR, idx, ch_bit);
583 edma_shadow0_write_array(ecc, SH_SECR, idx, ch_bit);
584 edma_write_array(ecc, EDMA_EMCR, idx, ch_bit);
586 /* clear possibly pending completion interrupt */
587 edma_shadow0_write_array(ecc, SH_ICR, idx, ch_bit);
589 dev_dbg(ecc->dev, "EER%d %08x\n", idx,
590 edma_shadow0_read_array(ecc, SH_EER, idx));
592 /* REVISIT: consider guarding against inappropriate event
593 * chaining by overwriting with dummy_paramset.
598 * Temporarily disable EDMA hardware events on the specified channel,
599 * preventing them from triggering new transfers
601 static void edma_pause(struct edma_chan *echan)
603 int channel = EDMA_CHAN_SLOT(echan->ch_num);
605 edma_shadow0_write_array(echan->ecc, SH_EECR,
606 EDMA_REG_ARRAY_INDEX(channel),
607 EDMA_CHANNEL_BIT(channel));
610 /* Re-enable EDMA hardware events on the specified channel. */
611 static void edma_resume(struct edma_chan *echan)
613 int channel = EDMA_CHAN_SLOT(echan->ch_num);
615 edma_shadow0_write_array(echan->ecc, SH_EESR,
616 EDMA_REG_ARRAY_INDEX(channel),
617 EDMA_CHANNEL_BIT(channel));
620 static void edma_trigger_channel(struct edma_chan *echan)
622 struct edma_cc *ecc = echan->ecc;
623 int channel = EDMA_CHAN_SLOT(echan->ch_num);
624 int idx = EDMA_REG_ARRAY_INDEX(channel);
625 int ch_bit = EDMA_CHANNEL_BIT(channel);
627 edma_shadow0_write_array(ecc, SH_ESR, idx, ch_bit);
629 dev_dbg(ecc->dev, "ESR%d %08x\n", idx,
630 edma_shadow0_read_array(ecc, SH_ESR, idx));
633 static void edma_clean_channel(struct edma_chan *echan)
635 struct edma_cc *ecc = echan->ecc;
636 int channel = EDMA_CHAN_SLOT(echan->ch_num);
637 int idx = EDMA_REG_ARRAY_INDEX(channel);
638 int ch_bit = EDMA_CHANNEL_BIT(channel);
640 dev_dbg(ecc->dev, "EMR%d %08x\n", idx,
641 edma_read_array(ecc, EDMA_EMR, idx));
642 edma_shadow0_write_array(ecc, SH_ECR, idx, ch_bit);
643 /* Clear the corresponding EMR bits */
644 edma_write_array(ecc, EDMA_EMCR, idx, ch_bit);
646 edma_shadow0_write_array(ecc, SH_SECR, idx, ch_bit);
647 edma_write(ecc, EDMA_CCERRCLR, BIT(16) | BIT(1) | BIT(0));
650 /* Move channel to a specific event queue */
651 static void edma_assign_channel_eventq(struct edma_chan *echan,
652 enum dma_event_q eventq_no)
654 struct edma_cc *ecc = echan->ecc;
655 int channel = EDMA_CHAN_SLOT(echan->ch_num);
656 int bit = (channel & 0x7) * 4;
658 /* default to low priority queue */
659 if (eventq_no == EVENTQ_DEFAULT)
660 eventq_no = ecc->default_queue;
661 if (eventq_no >= ecc->num_tc)
665 edma_modify_array(ecc, EDMA_DMAQNUM, (channel >> 3), ~(0x7 << bit),
669 static int edma_alloc_channel(struct edma_chan *echan,
670 enum dma_event_q eventq_no)
672 struct edma_cc *ecc = echan->ecc;
673 int channel = EDMA_CHAN_SLOT(echan->ch_num);
675 if (!test_bit(echan->ch_num, ecc->channels_mask)) {
676 dev_err(ecc->dev, "Channel%d is reserved, can not be used!\n",
681 /* ensure access through shadow region 0 */
682 edma_or_array2(ecc, EDMA_DRAE, 0, EDMA_REG_ARRAY_INDEX(channel),
683 EDMA_CHANNEL_BIT(channel));
685 /* ensure no events are pending */
688 edma_setup_interrupt(echan, true);
690 edma_assign_channel_eventq(echan, eventq_no);
695 static void edma_free_channel(struct edma_chan *echan)
697 /* ensure no events are pending */
699 /* REVISIT should probably take out of shadow region 0 */
700 edma_setup_interrupt(echan, false);
703 static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
705 return container_of(c, struct edma_chan, vchan.chan);
708 static inline struct edma_desc *to_edma_desc(struct dma_async_tx_descriptor *tx)
710 return container_of(tx, struct edma_desc, vdesc.tx);
713 static void edma_desc_free(struct virt_dma_desc *vdesc)
715 kfree(container_of(vdesc, struct edma_desc, vdesc));
718 /* Dispatch a queued descriptor to the controller (caller holds lock) */
719 static void edma_execute(struct edma_chan *echan)
721 struct edma_cc *ecc = echan->ecc;
722 struct virt_dma_desc *vdesc;
723 struct edma_desc *edesc;
724 struct device *dev = echan->vchan.chan.device->dev;
725 int i, j, left, nslots;
728 /* Setup is needed for the first transfer */
729 vdesc = vchan_next_desc(&echan->vchan);
732 list_del(&vdesc->node);
733 echan->edesc = to_edma_desc(&vdesc->tx);
736 edesc = echan->edesc;
738 /* Find out how many left */
739 left = edesc->pset_nr - edesc->processed;
740 nslots = min(MAX_NR_SG, left);
743 /* Write descriptor PaRAM set(s) */
744 for (i = 0; i < nslots; i++) {
745 j = i + edesc->processed;
746 edma_write_slot(ecc, echan->slot[i], &edesc->pset[j].param);
747 edesc->sg_len += edesc->pset[j].len;
760 j, echan->ch_num, echan->slot[i],
761 edesc->pset[j].param.opt,
762 edesc->pset[j].param.src,
763 edesc->pset[j].param.dst,
764 edesc->pset[j].param.a_b_cnt,
765 edesc->pset[j].param.ccnt,
766 edesc->pset[j].param.src_dst_bidx,
767 edesc->pset[j].param.src_dst_cidx,
768 edesc->pset[j].param.link_bcntrld);
769 /* Link to the previous slot if not the last set */
770 if (i != (nslots - 1))
771 edma_link(ecc, echan->slot[i], echan->slot[i + 1]);
774 edesc->processed += nslots;
777 * If this is either the last set in a set of SG-list transactions
778 * then setup a link to the dummy slot, this results in all future
779 * events being absorbed and that's OK because we're done
781 if (edesc->processed == edesc->pset_nr) {
783 edma_link(ecc, echan->slot[nslots - 1], echan->slot[1]);
785 edma_link(ecc, echan->slot[nslots - 1],
786 echan->ecc->dummy_slot);
791 * This happens due to setup times between intermediate
792 * transfers in long SG lists which have to be broken up into
793 * transfers of MAX_NR_SG
795 dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
796 edma_clean_channel(echan);
799 edma_trigger_channel(echan);
801 } else if (edesc->processed <= MAX_NR_SG) {
802 dev_dbg(dev, "first transfer starting on channel %d\n",
806 dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
807 echan->ch_num, edesc->processed);
812 static int edma_terminate_all(struct dma_chan *chan)
814 struct edma_chan *echan = to_edma_chan(chan);
818 spin_lock_irqsave(&echan->vchan.lock, flags);
821 * Stop DMA activity: we assume the callback will not be called
822 * after edma_dma() returns (even if it does, it will see
823 * echan->edesc is NULL and exit.)
827 /* Move the cyclic channel back to default queue */
828 if (!echan->tc && echan->edesc->cyclic)
829 edma_assign_channel_eventq(echan, EVENTQ_DEFAULT);
831 vchan_terminate_vdesc(&echan->edesc->vdesc);
835 vchan_get_all_descriptors(&echan->vchan, &head);
836 spin_unlock_irqrestore(&echan->vchan.lock, flags);
837 vchan_dma_desc_free_list(&echan->vchan, &head);
842 static void edma_synchronize(struct dma_chan *chan)
844 struct edma_chan *echan = to_edma_chan(chan);
846 vchan_synchronize(&echan->vchan);
849 static int edma_slave_config(struct dma_chan *chan,
850 struct dma_slave_config *cfg)
852 struct edma_chan *echan = to_edma_chan(chan);
854 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
855 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
858 if (cfg->src_maxburst > chan->device->max_burst ||
859 cfg->dst_maxburst > chan->device->max_burst)
862 memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
867 static int edma_dma_pause(struct dma_chan *chan)
869 struct edma_chan *echan = to_edma_chan(chan);
878 static int edma_dma_resume(struct dma_chan *chan)
880 struct edma_chan *echan = to_edma_chan(chan);
887 * A PaRAM set configuration abstraction used by other modes
888 * @chan: Channel who's PaRAM set we're configuring
889 * @pset: PaRAM set to initialize and setup.
890 * @src_addr: Source address of the DMA
891 * @dst_addr: Destination address of the DMA
892 * @burst: In units of dev_width, how much to send
893 * @dev_width: How much is the dev_width
894 * @dma_length: Total length of the DMA transfer
895 * @direction: Direction of the transfer
897 static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
898 dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
899 unsigned int acnt, unsigned int dma_length,
900 enum dma_transfer_direction direction)
902 struct edma_chan *echan = to_edma_chan(chan);
903 struct device *dev = chan->device->dev;
904 struct edmacc_param *param = &epset->param;
905 int bcnt, ccnt, cidx;
906 int src_bidx, dst_bidx, src_cidx, dst_cidx;
909 /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
913 * If the maxburst is equal to the fifo width, use
914 * A-synced transfers. This allows for large contiguous
915 * buffer transfers using only one PaRAM set.
919 * For the A-sync case, bcnt and ccnt are the remainder
920 * and quotient respectively of the division of:
921 * (dma_length / acnt) by (SZ_64K -1). This is so
922 * that in case bcnt over flows, we have ccnt to use.
923 * Note: In A-sync transfer only, bcntrld is used, but it
924 * only applies for sg_dma_len(sg) >= SZ_64K.
925 * In this case, the best way adopted is- bccnt for the
926 * first frame will be the remainder below. Then for
927 * every successive frame, bcnt will be SZ_64K-1. This
928 * is assured as bcntrld = 0xffff in end of function.
931 ccnt = dma_length / acnt / (SZ_64K - 1);
932 bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
934 * If bcnt is non-zero, we have a remainder and hence an
935 * extra frame to transfer, so increment ccnt.
944 * If maxburst is greater than the fifo address_width,
945 * use AB-synced transfers where A count is the fifo
946 * address_width and B count is the maxburst. In this
947 * case, we are limited to transfers of C count frames
948 * of (address_width * maxburst) where C count is limited
949 * to SZ_64K-1. This places an upper bound on the length
950 * of an SG segment that can be handled.
954 ccnt = dma_length / (acnt * bcnt);
955 if (ccnt > (SZ_64K - 1)) {
956 dev_err(dev, "Exceeded max SG segment size\n");
962 epset->len = dma_length;
964 if (direction == DMA_MEM_TO_DEV) {
969 epset->addr = src_addr;
970 } else if (direction == DMA_DEV_TO_MEM) {
975 epset->addr = dst_addr;
976 } else if (direction == DMA_MEM_TO_MEM) {
981 epset->addr = src_addr;
983 dev_err(dev, "%s: direction not implemented yet\n", __func__);
987 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
988 /* Configure A or AB synchronized transfers */
990 param->opt |= SYNCDIM;
992 param->src = src_addr;
993 param->dst = dst_addr;
995 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
996 param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
998 param->a_b_cnt = bcnt << 16 | acnt;
1001 * Only time when (bcntrld) auto reload is required is for
1002 * A-sync case, and in this case, a requirement of reload value
1003 * of SZ_64K-1 only is assured. 'link' is initially set to NULL
1004 * and then later will be populated by edma_execute.
1006 param->link_bcntrld = 0xffffffff;
1010 static struct dma_async_tx_descriptor *edma_prep_slave_sg(
1011 struct dma_chan *chan, struct scatterlist *sgl,
1012 unsigned int sg_len, enum dma_transfer_direction direction,
1013 unsigned long tx_flags, void *context)
1015 struct edma_chan *echan = to_edma_chan(chan);
1016 struct device *dev = chan->device->dev;
1017 struct edma_desc *edesc;
1018 dma_addr_t src_addr = 0, dst_addr = 0;
1019 enum dma_slave_buswidth dev_width;
1021 struct scatterlist *sg;
1024 if (unlikely(!echan || !sgl || !sg_len))
1027 if (direction == DMA_DEV_TO_MEM) {
1028 src_addr = echan->cfg.src_addr;
1029 dev_width = echan->cfg.src_addr_width;
1030 burst = echan->cfg.src_maxburst;
1031 } else if (direction == DMA_MEM_TO_DEV) {
1032 dst_addr = echan->cfg.dst_addr;
1033 dev_width = echan->cfg.dst_addr_width;
1034 burst = echan->cfg.dst_maxburst;
1036 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
1040 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
1041 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
1045 edesc = kzalloc(struct_size(edesc, pset, sg_len), GFP_ATOMIC);
1049 edesc->pset_nr = sg_len;
1051 edesc->direction = direction;
1052 edesc->echan = echan;
1054 /* Allocate a PaRAM slot, if needed */
1055 nslots = min_t(unsigned, MAX_NR_SG, sg_len);
1057 for (i = 0; i < nslots; i++) {
1058 if (echan->slot[i] < 0) {
1060 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
1061 if (echan->slot[i] < 0) {
1063 dev_err(dev, "%s: Failed to allocate slot\n",
1070 /* Configure PaRAM sets for each SG */
1071 for_each_sg(sgl, sg, sg_len, i) {
1072 /* Get address for each SG */
1073 if (direction == DMA_DEV_TO_MEM)
1074 dst_addr = sg_dma_address(sg);
1076 src_addr = sg_dma_address(sg);
1078 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1079 dst_addr, burst, dev_width,
1080 sg_dma_len(sg), direction);
1086 edesc->absync = ret;
1087 edesc->residue += sg_dma_len(sg);
1089 if (i == sg_len - 1)
1090 /* Enable completion interrupt */
1091 edesc->pset[i].param.opt |= TCINTEN;
1092 else if (!((i+1) % MAX_NR_SG))
1094 * Enable early completion interrupt for the
1095 * intermediateset. In this case the driver will be
1096 * notified when the paRAM set is submitted to TC. This
1097 * will allow more time to set up the next set of slots.
1099 edesc->pset[i].param.opt |= (TCINTEN | TCCMODE);
1101 edesc->residue_stat = edesc->residue;
1103 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1106 static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
1107 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1108 size_t len, unsigned long tx_flags)
1111 struct edma_desc *edesc;
1112 struct device *dev = chan->device->dev;
1113 struct edma_chan *echan = to_edma_chan(chan);
1114 unsigned int width, pset_len, array_size;
1116 if (unlikely(!echan || !len))
1119 /* Align the array size (acnt block) with the transfer properties */
1120 switch (__ffs((src | dest | len))) {
1122 array_size = SZ_32K - 1;
1125 array_size = SZ_32K - 2;
1128 array_size = SZ_32K - 4;
1134 * Transfer size less than 64K can be handled with one paRAM
1135 * slot and with one burst.
1143 * Transfer size bigger than 64K will be handled with maximum of
1145 * slot1: (full_length / 32767) times 32767 bytes bursts.
1146 * ACNT = 32767, length1: (full_length / 32767) * 32767
1147 * slot2: the remaining amount of data after slot1.
1148 * ACNT = full_length - length1, length2 = ACNT
1150 * When the full_length is a multiple of 32767 one slot can be
1151 * used to complete the transfer.
1154 pset_len = rounddown(len, width);
1155 /* One slot is enough for lengths multiple of (SZ_32K -1) */
1156 if (unlikely(pset_len == len))
1162 edesc = kzalloc(struct_size(edesc, pset, nslots), GFP_ATOMIC);
1166 edesc->pset_nr = nslots;
1167 edesc->residue = edesc->residue_stat = len;
1168 edesc->direction = DMA_MEM_TO_MEM;
1169 edesc->echan = echan;
1171 ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
1172 width, pset_len, DMA_MEM_TO_MEM);
1178 edesc->absync = ret;
1180 edesc->pset[0].param.opt |= ITCCHEN;
1182 /* Enable transfer complete interrupt if requested */
1183 if (tx_flags & DMA_PREP_INTERRUPT)
1184 edesc->pset[0].param.opt |= TCINTEN;
1186 /* Enable transfer complete chaining for the first slot */
1187 edesc->pset[0].param.opt |= TCCHEN;
1189 if (echan->slot[1] < 0) {
1190 echan->slot[1] = edma_alloc_slot(echan->ecc,
1192 if (echan->slot[1] < 0) {
1194 dev_err(dev, "%s: Failed to allocate slot\n",
1201 pset_len = width = len % array_size;
1203 ret = edma_config_pset(chan, &edesc->pset[1], src, dest, 1,
1204 width, pset_len, DMA_MEM_TO_MEM);
1210 edesc->pset[1].param.opt |= ITCCHEN;
1211 /* Enable transfer complete interrupt if requested */
1212 if (tx_flags & DMA_PREP_INTERRUPT)
1213 edesc->pset[1].param.opt |= TCINTEN;
1216 if (!(tx_flags & DMA_PREP_INTERRUPT))
1217 edesc->polled = true;
1219 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1222 static struct dma_async_tx_descriptor *
1223 edma_prep_dma_interleaved(struct dma_chan *chan,
1224 struct dma_interleaved_template *xt,
1225 unsigned long tx_flags)
1227 struct device *dev = chan->device->dev;
1228 struct edma_chan *echan = to_edma_chan(chan);
1229 struct edmacc_param *param;
1230 struct edma_desc *edesc;
1231 size_t src_icg, dst_icg;
1232 int src_bidx, dst_bidx;
1234 /* Slave mode is not supported */
1235 if (is_slave_direction(xt->dir))
1238 if (xt->frame_size != 1 || xt->numf == 0)
1241 if (xt->sgl[0].size > SZ_64K || xt->numf > SZ_64K)
1244 src_icg = dmaengine_get_src_icg(xt, &xt->sgl[0]);
1246 src_bidx = src_icg + xt->sgl[0].size;
1247 } else if (xt->src_inc) {
1248 src_bidx = xt->sgl[0].size;
1250 dev_err(dev, "%s: SRC constant addressing is not supported\n",
1255 dst_icg = dmaengine_get_dst_icg(xt, &xt->sgl[0]);
1257 dst_bidx = dst_icg + xt->sgl[0].size;
1258 } else if (xt->dst_inc) {
1259 dst_bidx = xt->sgl[0].size;
1261 dev_err(dev, "%s: DST constant addressing is not supported\n",
1266 if (src_bidx > SZ_64K || dst_bidx > SZ_64K)
1269 edesc = kzalloc(struct_size(edesc, pset, 1), GFP_ATOMIC);
1273 edesc->direction = DMA_MEM_TO_MEM;
1274 edesc->echan = echan;
1277 param = &edesc->pset[0].param;
1279 param->src = xt->src_start;
1280 param->dst = xt->dst_start;
1281 param->a_b_cnt = xt->numf << 16 | xt->sgl[0].size;
1283 param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
1284 param->src_dst_cidx = 0;
1286 param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
1287 param->opt |= ITCCHEN;
1288 /* Enable transfer complete interrupt if requested */
1289 if (tx_flags & DMA_PREP_INTERRUPT)
1290 param->opt |= TCINTEN;
1292 edesc->polled = true;
1294 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1297 static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
1298 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
1299 size_t period_len, enum dma_transfer_direction direction,
1300 unsigned long tx_flags)
1302 struct edma_chan *echan = to_edma_chan(chan);
1303 struct device *dev = chan->device->dev;
1304 struct edma_desc *edesc;
1305 dma_addr_t src_addr, dst_addr;
1306 enum dma_slave_buswidth dev_width;
1307 bool use_intermediate = false;
1311 if (unlikely(!echan || !buf_len || !period_len))
1314 if (direction == DMA_DEV_TO_MEM) {
1315 src_addr = echan->cfg.src_addr;
1316 dst_addr = buf_addr;
1317 dev_width = echan->cfg.src_addr_width;
1318 burst = echan->cfg.src_maxburst;
1319 } else if (direction == DMA_MEM_TO_DEV) {
1320 src_addr = buf_addr;
1321 dst_addr = echan->cfg.dst_addr;
1322 dev_width = echan->cfg.dst_addr_width;
1323 burst = echan->cfg.dst_maxburst;
1325 dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
1329 if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
1330 dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
1334 if (unlikely(buf_len % period_len)) {
1335 dev_err(dev, "Period should be multiple of Buffer length\n");
1339 nslots = (buf_len / period_len) + 1;
1342 * Cyclic DMA users such as audio cannot tolerate delays introduced
1343 * by cases where the number of periods is more than the maximum
1344 * number of SGs the EDMA driver can handle at a time. For DMA types
1345 * such as Slave SGs, such delays are tolerable and synchronized,
1346 * but the synchronization is difficult to achieve with Cyclic and
1347 * cannot be guaranteed, so we error out early.
1349 if (nslots > MAX_NR_SG) {
1351 * If the burst and period sizes are the same, we can put
1352 * the full buffer into a single period and activate
1353 * intermediate interrupts. This will produce interrupts
1354 * after each burst, which is also after each desired period.
1356 if (burst == period_len) {
1357 period_len = buf_len;
1359 use_intermediate = true;
1365 edesc = kzalloc(struct_size(edesc, pset, nslots), GFP_ATOMIC);
1370 edesc->pset_nr = nslots;
1371 edesc->residue = edesc->residue_stat = buf_len;
1372 edesc->direction = direction;
1373 edesc->echan = echan;
1375 dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
1376 __func__, echan->ch_num, nslots, period_len, buf_len);
1378 for (i = 0; i < nslots; i++) {
1379 /* Allocate a PaRAM slot, if needed */
1380 if (echan->slot[i] < 0) {
1382 edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
1383 if (echan->slot[i] < 0) {
1385 dev_err(dev, "%s: Failed to allocate slot\n",
1391 if (i == nslots - 1) {
1392 memcpy(&edesc->pset[i], &edesc->pset[0],
1393 sizeof(edesc->pset[0]));
1397 ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1398 dst_addr, burst, dev_width, period_len,
1405 if (direction == DMA_DEV_TO_MEM)
1406 dst_addr += period_len;
1408 src_addr += period_len;
1410 dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
1423 i, echan->ch_num, echan->slot[i],
1424 edesc->pset[i].param.opt,
1425 edesc->pset[i].param.src,
1426 edesc->pset[i].param.dst,
1427 edesc->pset[i].param.a_b_cnt,
1428 edesc->pset[i].param.ccnt,
1429 edesc->pset[i].param.src_dst_bidx,
1430 edesc->pset[i].param.src_dst_cidx,
1431 edesc->pset[i].param.link_bcntrld);
1433 edesc->absync = ret;
1436 * Enable period interrupt only if it is requested
1438 if (tx_flags & DMA_PREP_INTERRUPT) {
1439 edesc->pset[i].param.opt |= TCINTEN;
1441 /* Also enable intermediate interrupts if necessary */
1442 if (use_intermediate)
1443 edesc->pset[i].param.opt |= ITCINTEN;
1447 /* Place the cyclic channel to highest priority queue */
1449 edma_assign_channel_eventq(echan, EVENTQ_0);
1451 return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1454 static void edma_completion_handler(struct edma_chan *echan)
1456 struct device *dev = echan->vchan.chan.device->dev;
1457 struct edma_desc *edesc;
1459 spin_lock(&echan->vchan.lock);
1460 edesc = echan->edesc;
1462 if (edesc->cyclic) {
1463 vchan_cyclic_callback(&edesc->vdesc);
1464 spin_unlock(&echan->vchan.lock);
1466 } else if (edesc->processed == edesc->pset_nr) {
1469 vchan_cookie_complete(&edesc->vdesc);
1470 echan->edesc = NULL;
1472 dev_dbg(dev, "Transfer completed on channel %d\n",
1475 dev_dbg(dev, "Sub transfer completed on channel %d\n",
1480 /* Update statistics for tx_status */
1481 edesc->residue -= edesc->sg_len;
1482 edesc->residue_stat = edesc->residue;
1483 edesc->processed_stat = edesc->processed;
1485 edma_execute(echan);
1488 spin_unlock(&echan->vchan.lock);
1491 /* eDMA interrupt handler */
1492 static irqreturn_t dma_irq_handler(int irq, void *data)
1494 struct edma_cc *ecc = data;
1504 dev_vdbg(ecc->dev, "dma_irq_handler\n");
1506 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 0);
1508 sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 1);
1511 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 1);
1514 sh_ier = edma_shadow0_read_array(ecc, SH_IER, 0);
1522 slot = __ffs(sh_ipr);
1523 sh_ipr &= ~(BIT(slot));
1525 if (sh_ier & BIT(slot)) {
1526 channel = (bank << 5) | slot;
1527 /* Clear the corresponding IPR bits */
1528 edma_shadow0_write_array(ecc, SH_ICR, bank, BIT(slot));
1529 edma_completion_handler(&ecc->slave_chans[channel]);
1533 edma_shadow0_write(ecc, SH_IEVAL, 1);
1537 static void edma_error_handler(struct edma_chan *echan)
1539 struct edma_cc *ecc = echan->ecc;
1540 struct device *dev = echan->vchan.chan.device->dev;
1541 struct edmacc_param p;
1547 spin_lock(&echan->vchan.lock);
1549 err = edma_read_slot(ecc, echan->slot[0], &p);
1552 * Issue later based on missed flag which will be sure
1554 * (1) we finished transmitting an intermediate slot and
1555 * edma_execute is coming up.
1556 * (2) or we finished current transfer and issue will
1557 * call edma_execute.
1559 * Important note: issuing can be dangerous here and
1560 * lead to some nasty recursion when we are in a NULL
1561 * slot. So we avoid doing so and set the missed flag.
1563 if (err || (p.a_b_cnt == 0 && p.ccnt == 0)) {
1564 dev_dbg(dev, "Error on null slot, setting miss\n");
1568 * The slot is already programmed but the event got
1569 * missed, so its safe to issue it here.
1571 dev_dbg(dev, "Missed event, TRIGGERING\n");
1572 edma_clean_channel(echan);
1575 edma_trigger_channel(echan);
1577 spin_unlock(&echan->vchan.lock);
1580 static inline bool edma_error_pending(struct edma_cc *ecc)
1582 if (edma_read_array(ecc, EDMA_EMR, 0) ||
1583 edma_read_array(ecc, EDMA_EMR, 1) ||
1584 edma_read(ecc, EDMA_QEMR) || edma_read(ecc, EDMA_CCERR))
1590 /* eDMA error interrupt handler */
1591 static irqreturn_t dma_ccerr_handler(int irq, void *data)
1593 struct edma_cc *ecc = data;
1596 unsigned int cnt = 0;
1603 dev_vdbg(ecc->dev, "dma_ccerr_handler\n");
1605 if (!edma_error_pending(ecc)) {
1607 * The registers indicate no pending error event but the irq
1608 * handler has been called.
1609 * Ask eDMA to re-evaluate the error registers.
1611 dev_err(ecc->dev, "%s: Error interrupt without error event!\n",
1613 edma_write(ecc, EDMA_EEVAL, 1);
1618 /* Event missed register(s) */
1619 for (j = 0; j < 2; j++) {
1622 val = edma_read_array(ecc, EDMA_EMR, j);
1626 dev_dbg(ecc->dev, "EMR%d 0x%08x\n", j, val);
1628 for_each_set_bit(i, &emr, 32) {
1629 int k = (j << 5) + i;
1631 /* Clear the corresponding EMR bits */
1632 edma_write_array(ecc, EDMA_EMCR, j, BIT(i));
1634 edma_shadow0_write_array(ecc, SH_SECR, j,
1636 edma_error_handler(&ecc->slave_chans[k]);
1640 val = edma_read(ecc, EDMA_QEMR);
1642 dev_dbg(ecc->dev, "QEMR 0x%02x\n", val);
1643 /* Not reported, just clear the interrupt reason. */
1644 edma_write(ecc, EDMA_QEMCR, val);
1645 edma_shadow0_write(ecc, SH_QSECR, val);
1648 val = edma_read(ecc, EDMA_CCERR);
1650 dev_warn(ecc->dev, "CCERR 0x%08x\n", val);
1651 /* Not reported, just clear the interrupt reason. */
1652 edma_write(ecc, EDMA_CCERRCLR, val);
1655 if (!edma_error_pending(ecc))
1661 edma_write(ecc, EDMA_EEVAL, 1);
1665 /* Alloc channel resources */
1666 static int edma_alloc_chan_resources(struct dma_chan *chan)
1668 struct edma_chan *echan = to_edma_chan(chan);
1669 struct edma_cc *ecc = echan->ecc;
1670 struct device *dev = ecc->dev;
1671 enum dma_event_q eventq_no = EVENTQ_DEFAULT;
1675 eventq_no = echan->tc->id;
1676 } else if (ecc->tc_list) {
1677 /* memcpy channel */
1678 echan->tc = &ecc->tc_list[ecc->info->default_queue];
1679 eventq_no = echan->tc->id;
1682 ret = edma_alloc_channel(echan, eventq_no);
1686 echan->slot[0] = edma_alloc_slot(ecc, echan->ch_num);
1687 if (echan->slot[0] < 0) {
1688 dev_err(dev, "Entry slot allocation failed for channel %u\n",
1689 EDMA_CHAN_SLOT(echan->ch_num));
1690 ret = echan->slot[0];
1694 /* Set up channel -> slot mapping for the entry slot */
1695 edma_set_chmap(echan, echan->slot[0]);
1696 echan->alloced = true;
1698 dev_dbg(dev, "Got eDMA channel %d for virt channel %d (%s trigger)\n",
1699 EDMA_CHAN_SLOT(echan->ch_num), chan->chan_id,
1700 echan->hw_triggered ? "HW" : "SW");
1705 edma_free_channel(echan);
1709 /* Free channel resources */
1710 static void edma_free_chan_resources(struct dma_chan *chan)
1712 struct edma_chan *echan = to_edma_chan(chan);
1713 struct device *dev = echan->ecc->dev;
1716 /* Terminate transfers */
1719 vchan_free_chan_resources(&echan->vchan);
1721 /* Free EDMA PaRAM slots */
1722 for (i = 0; i < EDMA_MAX_SLOTS; i++) {
1723 if (echan->slot[i] >= 0) {
1724 edma_free_slot(echan->ecc, echan->slot[i]);
1725 echan->slot[i] = -1;
1729 /* Set entry slot to the dummy slot */
1730 edma_set_chmap(echan, echan->ecc->dummy_slot);
1732 /* Free EDMA channel */
1733 if (echan->alloced) {
1734 edma_free_channel(echan);
1735 echan->alloced = false;
1739 echan->hw_triggered = false;
1741 dev_dbg(dev, "Free eDMA channel %d for virt channel %d\n",
1742 EDMA_CHAN_SLOT(echan->ch_num), chan->chan_id);
1745 /* Send pending descriptor to hardware */
1746 static void edma_issue_pending(struct dma_chan *chan)
1748 struct edma_chan *echan = to_edma_chan(chan);
1749 unsigned long flags;
1751 spin_lock_irqsave(&echan->vchan.lock, flags);
1752 if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
1753 edma_execute(echan);
1754 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1758 * This limit exists to avoid a possible infinite loop when waiting for proof
1759 * that a particular transfer is completed. This limit can be hit if there
1760 * are large bursts to/from slow devices or the CPU is never able to catch
1761 * the DMA hardware idle. On an AM335x transferring 48 bytes from the UART
1762 * RX-FIFO, as many as 55 loops have been seen.
1764 #define EDMA_MAX_TR_WAIT_LOOPS 1000
1766 static u32 edma_residue(struct edma_desc *edesc)
1768 bool dst = edesc->direction == DMA_DEV_TO_MEM;
1769 int loop_count = EDMA_MAX_TR_WAIT_LOOPS;
1770 struct edma_chan *echan = edesc->echan;
1771 struct edma_pset *pset = edesc->pset;
1772 dma_addr_t done, pos, pos_old;
1773 int channel = EDMA_CHAN_SLOT(echan->ch_num);
1774 int idx = EDMA_REG_ARRAY_INDEX(channel);
1775 int ch_bit = EDMA_CHANNEL_BIT(channel);
1780 * We always read the dst/src position from the first RamPar
1781 * pset. That's the one which is active now.
1783 pos = edma_get_position(echan->ecc, echan->slot[0], dst);
1786 * "pos" may represent a transfer request that is still being
1787 * processed by the EDMACC or EDMATC. We will busy wait until
1788 * any one of the situations occurs:
1789 * 1. while and event is pending for the channel
1790 * 2. a position updated
1791 * 3. we hit the loop limit
1793 if (is_slave_direction(edesc->direction))
1799 while (edma_shadow0_read_array(echan->ecc, event_reg, idx) & ch_bit) {
1800 pos = edma_get_position(echan->ecc, echan->slot[0], dst);
1804 if (!--loop_count) {
1805 dev_dbg_ratelimited(echan->vchan.chan.device->dev,
1806 "%s: timeout waiting for PaRAM update\n",
1815 * Cyclic is simple. Just subtract pset[0].addr from pos.
1817 * We never update edesc->residue in the cyclic case, so we
1818 * can tell the remaining room to the end of the circular
1821 if (edesc->cyclic) {
1822 done = pos - pset->addr;
1823 edesc->residue_stat = edesc->residue - done;
1824 return edesc->residue_stat;
1828 * If the position is 0, then EDMA loaded the closing dummy slot, the
1829 * transfer is completed
1834 * For SG operation we catch up with the last processed
1837 pset += edesc->processed_stat;
1839 for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
1841 * If we are inside this pset address range, we know
1842 * this is the active one. Get the current delta and
1843 * stop walking the psets.
1845 if (pos >= pset->addr && pos < pset->addr + pset->len)
1846 return edesc->residue_stat - (pos - pset->addr);
1848 /* Otherwise mark it done and update residue_stat. */
1849 edesc->processed_stat++;
1850 edesc->residue_stat -= pset->len;
1852 return edesc->residue_stat;
1855 /* Check request completion status */
1856 static enum dma_status edma_tx_status(struct dma_chan *chan,
1857 dma_cookie_t cookie,
1858 struct dma_tx_state *txstate)
1860 struct edma_chan *echan = to_edma_chan(chan);
1861 struct dma_tx_state txstate_tmp;
1862 enum dma_status ret;
1863 unsigned long flags;
1865 ret = dma_cookie_status(chan, cookie, txstate);
1867 if (ret == DMA_COMPLETE)
1870 /* Provide a dummy dma_tx_state for completion checking */
1872 txstate = &txstate_tmp;
1874 spin_lock_irqsave(&echan->vchan.lock, flags);
1875 if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie) {
1876 txstate->residue = edma_residue(echan->edesc);
1878 struct virt_dma_desc *vdesc = vchan_find_desc(&echan->vchan,
1882 txstate->residue = to_edma_desc(&vdesc->tx)->residue;
1884 txstate->residue = 0;
1888 * Mark the cookie completed if the residue is 0 for non cyclic
1891 if (ret != DMA_COMPLETE && !txstate->residue &&
1892 echan->edesc && echan->edesc->polled &&
1893 echan->edesc->vdesc.tx.cookie == cookie) {
1895 vchan_cookie_complete(&echan->edesc->vdesc);
1896 echan->edesc = NULL;
1897 edma_execute(echan);
1901 spin_unlock_irqrestore(&echan->vchan.lock, flags);
1906 static bool edma_is_memcpy_channel(int ch_num, s32 *memcpy_channels)
1908 if (!memcpy_channels)
1910 while (*memcpy_channels != -1) {
1911 if (*memcpy_channels == ch_num)
1918 #define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1919 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
1920 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
1921 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1923 static void edma_dma_init(struct edma_cc *ecc, bool legacy_mode)
1925 struct dma_device *s_ddev = &ecc->dma_slave;
1926 struct dma_device *m_ddev = NULL;
1927 s32 *memcpy_channels = ecc->info->memcpy_channels;
1930 dma_cap_zero(s_ddev->cap_mask);
1931 dma_cap_set(DMA_SLAVE, s_ddev->cap_mask);
1932 dma_cap_set(DMA_CYCLIC, s_ddev->cap_mask);
1933 if (ecc->legacy_mode && !memcpy_channels) {
1935 "Legacy memcpy is enabled, things might not work\n");
1937 dma_cap_set(DMA_MEMCPY, s_ddev->cap_mask);
1938 dma_cap_set(DMA_INTERLEAVE, s_ddev->cap_mask);
1939 s_ddev->device_prep_dma_memcpy = edma_prep_dma_memcpy;
1940 s_ddev->device_prep_interleaved_dma = edma_prep_dma_interleaved;
1941 s_ddev->directions = BIT(DMA_MEM_TO_MEM);
1944 s_ddev->device_prep_slave_sg = edma_prep_slave_sg;
1945 s_ddev->device_prep_dma_cyclic = edma_prep_dma_cyclic;
1946 s_ddev->device_alloc_chan_resources = edma_alloc_chan_resources;
1947 s_ddev->device_free_chan_resources = edma_free_chan_resources;
1948 s_ddev->device_issue_pending = edma_issue_pending;
1949 s_ddev->device_tx_status = edma_tx_status;
1950 s_ddev->device_config = edma_slave_config;
1951 s_ddev->device_pause = edma_dma_pause;
1952 s_ddev->device_resume = edma_dma_resume;
1953 s_ddev->device_terminate_all = edma_terminate_all;
1954 s_ddev->device_synchronize = edma_synchronize;
1956 s_ddev->src_addr_widths = EDMA_DMA_BUSWIDTHS;
1957 s_ddev->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
1958 s_ddev->directions |= (BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV));
1959 s_ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1960 s_ddev->max_burst = SZ_32K - 1; /* CIDX: 16bit signed */
1962 s_ddev->dev = ecc->dev;
1963 INIT_LIST_HEAD(&s_ddev->channels);
1965 if (memcpy_channels) {
1966 m_ddev = devm_kzalloc(ecc->dev, sizeof(*m_ddev), GFP_KERNEL);
1968 dev_warn(ecc->dev, "memcpy is disabled due to OoM\n");
1969 memcpy_channels = NULL;
1972 ecc->dma_memcpy = m_ddev;
1974 dma_cap_zero(m_ddev->cap_mask);
1975 dma_cap_set(DMA_MEMCPY, m_ddev->cap_mask);
1976 dma_cap_set(DMA_INTERLEAVE, m_ddev->cap_mask);
1978 m_ddev->device_prep_dma_memcpy = edma_prep_dma_memcpy;
1979 m_ddev->device_prep_interleaved_dma = edma_prep_dma_interleaved;
1980 m_ddev->device_alloc_chan_resources = edma_alloc_chan_resources;
1981 m_ddev->device_free_chan_resources = edma_free_chan_resources;
1982 m_ddev->device_issue_pending = edma_issue_pending;
1983 m_ddev->device_tx_status = edma_tx_status;
1984 m_ddev->device_config = edma_slave_config;
1985 m_ddev->device_pause = edma_dma_pause;
1986 m_ddev->device_resume = edma_dma_resume;
1987 m_ddev->device_terminate_all = edma_terminate_all;
1988 m_ddev->device_synchronize = edma_synchronize;
1990 m_ddev->src_addr_widths = EDMA_DMA_BUSWIDTHS;
1991 m_ddev->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
1992 m_ddev->directions = BIT(DMA_MEM_TO_MEM);
1993 m_ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1995 m_ddev->dev = ecc->dev;
1996 INIT_LIST_HEAD(&m_ddev->channels);
1997 } else if (!ecc->legacy_mode) {
1998 dev_info(ecc->dev, "memcpy is disabled\n");
2002 for (i = 0; i < ecc->num_channels; i++) {
2003 struct edma_chan *echan = &ecc->slave_chans[i];
2004 echan->ch_num = EDMA_CTLR_CHAN(ecc->id, i);
2006 echan->vchan.desc_free = edma_desc_free;
2008 if (m_ddev && edma_is_memcpy_channel(i, memcpy_channels))
2009 vchan_init(&echan->vchan, m_ddev);
2011 vchan_init(&echan->vchan, s_ddev);
2013 INIT_LIST_HEAD(&echan->node);
2014 for (j = 0; j < EDMA_MAX_SLOTS; j++)
2015 echan->slot[j] = -1;
2019 static int edma_setup_from_hw(struct device *dev, struct edma_soc_info *pdata,
2020 struct edma_cc *ecc)
2024 s8 (*queue_priority_map)[2];
2026 /* Decode the eDMA3 configuration from CCCFG register */
2027 cccfg = edma_read(ecc, EDMA_CCCFG);
2029 value = GET_NUM_REGN(cccfg);
2030 ecc->num_region = BIT(value);
2032 value = GET_NUM_DMACH(cccfg);
2033 ecc->num_channels = BIT(value + 1);
2035 value = GET_NUM_QDMACH(cccfg);
2036 ecc->num_qchannels = value * 2;
2038 value = GET_NUM_PAENTRY(cccfg);
2039 ecc->num_slots = BIT(value + 4);
2041 value = GET_NUM_EVQUE(cccfg);
2042 ecc->num_tc = value + 1;
2044 ecc->chmap_exist = (cccfg & CHMAP_EXIST) ? true : false;
2046 dev_dbg(dev, "eDMA3 CC HW configuration (cccfg: 0x%08x):\n", cccfg);
2047 dev_dbg(dev, "num_region: %u\n", ecc->num_region);
2048 dev_dbg(dev, "num_channels: %u\n", ecc->num_channels);
2049 dev_dbg(dev, "num_qchannels: %u\n", ecc->num_qchannels);
2050 dev_dbg(dev, "num_slots: %u\n", ecc->num_slots);
2051 dev_dbg(dev, "num_tc: %u\n", ecc->num_tc);
2052 dev_dbg(dev, "chmap_exist: %s\n", ecc->chmap_exist ? "yes" : "no");
2054 /* Nothing need to be done if queue priority is provided */
2055 if (pdata->queue_priority_mapping)
2059 * Configure TC/queue priority as follows:
2064 * The meaning of priority numbers: 0 highest priority, 7 lowest
2065 * priority. So Q0 is the highest priority queue and the last queue has
2066 * the lowest priority.
2068 queue_priority_map = devm_kcalloc(dev, ecc->num_tc + 1, sizeof(s8),
2070 if (!queue_priority_map)
2073 for (i = 0; i < ecc->num_tc; i++) {
2074 queue_priority_map[i][0] = i;
2075 queue_priority_map[i][1] = i;
2077 queue_priority_map[i][0] = -1;
2078 queue_priority_map[i][1] = -1;
2080 pdata->queue_priority_mapping = queue_priority_map;
2081 /* Default queue has the lowest priority */
2082 pdata->default_queue = i - 1;
2087 #if IS_ENABLED(CONFIG_OF)
2088 static int edma_xbar_event_map(struct device *dev, struct edma_soc_info *pdata,
2091 const char pname[] = "ti,edma-xbar-event-map";
2092 struct resource res;
2094 s16 (*xbar_chans)[2];
2095 size_t nelm = sz / sizeof(s16);
2096 u32 shift, offset, mux;
2099 xbar_chans = devm_kcalloc(dev, nelm + 2, sizeof(s16), GFP_KERNEL);
2103 ret = of_address_to_resource(dev->of_node, 1, &res);
2107 xbar = devm_ioremap(dev, res.start, resource_size(&res));
2111 ret = of_property_read_u16_array(dev->of_node, pname, (u16 *)xbar_chans,
2116 /* Invalidate last entry for the other user of this mess */
2118 xbar_chans[nelm][0] = -1;
2119 xbar_chans[nelm][1] = -1;
2121 for (i = 0; i < nelm; i++) {
2122 shift = (xbar_chans[i][1] & 0x03) << 3;
2123 offset = xbar_chans[i][1] & 0xfffffffc;
2124 mux = readl(xbar + offset);
2125 mux &= ~(0xff << shift);
2126 mux |= xbar_chans[i][0] << shift;
2127 writel(mux, (xbar + offset));
2130 pdata->xbar_chans = (const s16 (*)[2]) xbar_chans;
2134 static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
2137 struct edma_soc_info *info;
2138 struct property *prop;
2141 info = devm_kzalloc(dev, sizeof(struct edma_soc_info), GFP_KERNEL);
2143 return ERR_PTR(-ENOMEM);
2146 prop = of_find_property(dev->of_node, "ti,edma-xbar-event-map",
2149 ret = edma_xbar_event_map(dev, info, sz);
2151 return ERR_PTR(ret);
2156 /* Get the list of channels allocated to be used for memcpy */
2157 prop = of_find_property(dev->of_node, "ti,edma-memcpy-channels", &sz);
2159 const char pname[] = "ti,edma-memcpy-channels";
2160 size_t nelm = sz / sizeof(s32);
2163 memcpy_ch = devm_kcalloc(dev, nelm + 1, sizeof(s32),
2166 return ERR_PTR(-ENOMEM);
2168 ret = of_property_read_u32_array(dev->of_node, pname,
2169 (u32 *)memcpy_ch, nelm);
2171 return ERR_PTR(ret);
2173 memcpy_ch[nelm] = -1;
2174 info->memcpy_channels = memcpy_ch;
2177 prop = of_find_property(dev->of_node, "ti,edma-reserved-slot-ranges",
2180 const char pname[] = "ti,edma-reserved-slot-ranges";
2182 s16 (*rsv_slots)[2];
2183 size_t nelm = sz / sizeof(*tmp);
2184 struct edma_rsv_info *rsv_info;
2190 tmp = kcalloc(nelm, sizeof(*tmp), GFP_KERNEL);
2192 return ERR_PTR(-ENOMEM);
2194 rsv_info = devm_kzalloc(dev, sizeof(*rsv_info), GFP_KERNEL);
2197 return ERR_PTR(-ENOMEM);
2200 rsv_slots = devm_kcalloc(dev, nelm + 1, sizeof(*rsv_slots),
2204 return ERR_PTR(-ENOMEM);
2207 ret = of_property_read_u32_array(dev->of_node, pname,
2208 (u32 *)tmp, nelm * 2);
2211 return ERR_PTR(ret);
2214 for (i = 0; i < nelm; i++) {
2215 rsv_slots[i][0] = tmp[i][0];
2216 rsv_slots[i][1] = tmp[i][1];
2218 rsv_slots[nelm][0] = -1;
2219 rsv_slots[nelm][1] = -1;
2221 info->rsv = rsv_info;
2222 info->rsv->rsv_slots = (const s16 (*)[2])rsv_slots;
2230 static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
2231 struct of_dma *ofdma)
2233 struct edma_cc *ecc = ofdma->of_dma_data;
2234 struct dma_chan *chan = NULL;
2235 struct edma_chan *echan;
2238 if (!ecc || dma_spec->args_count < 1)
2241 for (i = 0; i < ecc->num_channels; i++) {
2242 echan = &ecc->slave_chans[i];
2243 if (echan->ch_num == dma_spec->args[0]) {
2244 chan = &echan->vchan.chan;
2252 if (echan->ecc->legacy_mode && dma_spec->args_count == 1)
2255 if (!echan->ecc->legacy_mode && dma_spec->args_count == 2 &&
2256 dma_spec->args[1] < echan->ecc->num_tc) {
2257 echan->tc = &echan->ecc->tc_list[dma_spec->args[1]];
2263 /* The channel is going to be used as HW synchronized */
2264 echan->hw_triggered = true;
2265 return dma_get_slave_channel(chan);
2268 static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
2271 return ERR_PTR(-EINVAL);
2274 static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
2275 struct of_dma *ofdma)
2281 static bool edma_filter_fn(struct dma_chan *chan, void *param);
2283 static int edma_probe(struct platform_device *pdev)
2285 struct edma_soc_info *info = pdev->dev.platform_data;
2286 s8 (*queue_priority_mapping)[2];
2287 const s16 (*reserved)[2];
2290 struct resource *mem;
2291 struct device_node *node = pdev->dev.of_node;
2292 struct device *dev = &pdev->dev;
2293 struct edma_cc *ecc;
2294 bool legacy_mode = true;
2298 const struct of_device_id *match;
2300 match = of_match_node(edma_of_ids, node);
2301 if (match && (*(u32 *)match->data) == EDMA_BINDING_TPCC)
2302 legacy_mode = false;
2304 info = edma_setup_info_from_dt(dev, legacy_mode);
2306 dev_err(dev, "failed to get DT data\n");
2307 return PTR_ERR(info);
2314 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2318 ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
2324 ecc->legacy_mode = legacy_mode;
2325 /* When booting with DT the pdev->id is -1 */
2329 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "edma3_cc");
2331 dev_dbg(dev, "mem resource not found, using index 0\n");
2332 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2334 dev_err(dev, "no mem resource?\n");
2338 ecc->base = devm_ioremap_resource(dev, mem);
2339 if (IS_ERR(ecc->base))
2340 return PTR_ERR(ecc->base);
2342 platform_set_drvdata(pdev, ecc);
2344 pm_runtime_enable(dev);
2345 ret = pm_runtime_get_sync(dev);
2347 dev_err(dev, "pm_runtime_get_sync() failed\n");
2348 pm_runtime_disable(dev);
2352 /* Get eDMA3 configuration from IP */
2353 ret = edma_setup_from_hw(dev, info, ecc);
2355 goto err_disable_pm;
2357 /* Allocate memory based on the information we got from the IP */
2358 ecc->slave_chans = devm_kcalloc(dev, ecc->num_channels,
2359 sizeof(*ecc->slave_chans), GFP_KERNEL);
2361 ecc->slot_inuse = devm_kcalloc(dev, BITS_TO_LONGS(ecc->num_slots),
2362 sizeof(unsigned long), GFP_KERNEL);
2364 ecc->channels_mask = devm_kcalloc(dev,
2365 BITS_TO_LONGS(ecc->num_channels),
2366 sizeof(unsigned long), GFP_KERNEL);
2367 if (!ecc->slave_chans || !ecc->slot_inuse || !ecc->channels_mask) {
2369 goto err_disable_pm;
2372 /* Mark all channels available initially */
2373 bitmap_fill(ecc->channels_mask, ecc->num_channels);
2375 ecc->default_queue = info->default_queue;
2378 /* Set the reserved slots in inuse list */
2379 reserved = info->rsv->rsv_slots;
2381 for (i = 0; reserved[i][0] != -1; i++)
2382 bitmap_set(ecc->slot_inuse, reserved[i][0],
2386 /* Clear channels not usable for Linux */
2387 reserved = info->rsv->rsv_chans;
2389 for (i = 0; reserved[i][0] != -1; i++)
2390 bitmap_clear(ecc->channels_mask, reserved[i][0],
2395 for (i = 0; i < ecc->num_slots; i++) {
2396 /* Reset only unused - not reserved - paRAM slots */
2397 if (!test_bit(i, ecc->slot_inuse))
2398 edma_write_slot(ecc, i, &dummy_paramset);
2401 irq = platform_get_irq_byname(pdev, "edma3_ccint");
2402 if (irq < 0 && node)
2403 irq = irq_of_parse_and_map(node, 0);
2406 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccint",
2408 ret = devm_request_irq(dev, irq, dma_irq_handler, 0, irq_name,
2411 dev_err(dev, "CCINT (%d) failed --> %d\n", irq, ret);
2412 goto err_disable_pm;
2417 irq = platform_get_irq_byname(pdev, "edma3_ccerrint");
2418 if (irq < 0 && node)
2419 irq = irq_of_parse_and_map(node, 2);
2422 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccerrint",
2424 ret = devm_request_irq(dev, irq, dma_ccerr_handler, 0, irq_name,
2427 dev_err(dev, "CCERRINT (%d) failed --> %d\n", irq, ret);
2428 goto err_disable_pm;
2430 ecc->ccerrint = irq;
2433 ecc->dummy_slot = edma_alloc_slot(ecc, EDMA_SLOT_ANY);
2434 if (ecc->dummy_slot < 0) {
2435 dev_err(dev, "Can't allocate PaRAM dummy slot\n");
2436 ret = ecc->dummy_slot;
2437 goto err_disable_pm;
2440 queue_priority_mapping = info->queue_priority_mapping;
2442 if (!ecc->legacy_mode) {
2443 int lowest_priority = 0;
2444 unsigned int array_max;
2445 struct of_phandle_args tc_args;
2447 ecc->tc_list = devm_kcalloc(dev, ecc->num_tc,
2448 sizeof(*ecc->tc_list), GFP_KERNEL);
2449 if (!ecc->tc_list) {
2455 ret = of_parse_phandle_with_fixed_args(node, "ti,tptcs",
2457 if (ret || i == ecc->num_tc)
2460 ecc->tc_list[i].node = tc_args.np;
2461 ecc->tc_list[i].id = i;
2462 queue_priority_mapping[i][1] = tc_args.args[0];
2463 if (queue_priority_mapping[i][1] > lowest_priority) {
2464 lowest_priority = queue_priority_mapping[i][1];
2465 info->default_queue = i;
2469 /* See if we have optional dma-channel-mask array */
2470 array_max = DIV_ROUND_UP(ecc->num_channels, BITS_PER_TYPE(u32));
2471 ret = of_property_read_variable_u32_array(node,
2473 (u32 *)ecc->channels_mask,
2475 if (ret > 0 && ret != array_max)
2476 dev_warn(dev, "dma-channel-mask is not complete.\n");
2477 else if (ret == -EOVERFLOW || ret == -ENODATA)
2479 "dma-channel-mask is out of range or empty\n");
2482 /* Event queue priority mapping */
2483 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2484 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2485 queue_priority_mapping[i][1]);
2487 edma_write_array2(ecc, EDMA_DRAE, 0, 0, 0x0);
2488 edma_write_array2(ecc, EDMA_DRAE, 0, 1, 0x0);
2489 edma_write_array(ecc, EDMA_QRAE, 0, 0x0);
2493 /* Init the dma device and channels */
2494 edma_dma_init(ecc, legacy_mode);
2496 for (i = 0; i < ecc->num_channels; i++) {
2497 /* Do not touch reserved channels */
2498 if (!test_bit(i, ecc->channels_mask))
2501 /* Assign all channels to the default queue */
2502 edma_assign_channel_eventq(&ecc->slave_chans[i],
2503 info->default_queue);
2504 /* Set entry slot to the dummy slot */
2505 edma_set_chmap(&ecc->slave_chans[i], ecc->dummy_slot);
2508 ecc->dma_slave.filter.map = info->slave_map;
2509 ecc->dma_slave.filter.mapcnt = info->slavecnt;
2510 ecc->dma_slave.filter.fn = edma_filter_fn;
2512 ret = dma_async_device_register(&ecc->dma_slave);
2514 dev_err(dev, "slave ddev registration failed (%d)\n", ret);
2518 if (ecc->dma_memcpy) {
2519 ret = dma_async_device_register(ecc->dma_memcpy);
2521 dev_err(dev, "memcpy ddev registration failed (%d)\n",
2523 dma_async_device_unregister(&ecc->dma_slave);
2529 of_dma_controller_register(node, of_edma_xlate, ecc);
2531 dev_info(dev, "TI EDMA DMA engine driver\n");
2536 edma_free_slot(ecc, ecc->dummy_slot);
2538 pm_runtime_put_sync(dev);
2539 pm_runtime_disable(dev);
2543 static void edma_cleanupp_vchan(struct dma_device *dmadev)
2545 struct edma_chan *echan, *_echan;
2547 list_for_each_entry_safe(echan, _echan,
2548 &dmadev->channels, vchan.chan.device_node) {
2549 list_del(&echan->vchan.chan.device_node);
2550 tasklet_kill(&echan->vchan.task);
2554 static int edma_remove(struct platform_device *pdev)
2556 struct device *dev = &pdev->dev;
2557 struct edma_cc *ecc = dev_get_drvdata(dev);
2559 devm_free_irq(dev, ecc->ccint, ecc);
2560 devm_free_irq(dev, ecc->ccerrint, ecc);
2562 edma_cleanupp_vchan(&ecc->dma_slave);
2565 of_dma_controller_free(dev->of_node);
2566 dma_async_device_unregister(&ecc->dma_slave);
2567 if (ecc->dma_memcpy)
2568 dma_async_device_unregister(ecc->dma_memcpy);
2569 edma_free_slot(ecc, ecc->dummy_slot);
2570 pm_runtime_put_sync(dev);
2571 pm_runtime_disable(dev);
2576 #ifdef CONFIG_PM_SLEEP
2577 static int edma_pm_suspend(struct device *dev)
2579 struct edma_cc *ecc = dev_get_drvdata(dev);
2580 struct edma_chan *echan = ecc->slave_chans;
2583 for (i = 0; i < ecc->num_channels; i++) {
2584 if (echan[i].alloced)
2585 edma_setup_interrupt(&echan[i], false);
2591 static int edma_pm_resume(struct device *dev)
2593 struct edma_cc *ecc = dev_get_drvdata(dev);
2594 struct edma_chan *echan = ecc->slave_chans;
2596 s8 (*queue_priority_mapping)[2];
2598 /* re initialize dummy slot to dummy param set */
2599 edma_write_slot(ecc, ecc->dummy_slot, &dummy_paramset);
2601 queue_priority_mapping = ecc->info->queue_priority_mapping;
2603 /* Event queue priority mapping */
2604 for (i = 0; queue_priority_mapping[i][0] != -1; i++)
2605 edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
2606 queue_priority_mapping[i][1]);
2608 for (i = 0; i < ecc->num_channels; i++) {
2609 if (echan[i].alloced) {
2610 /* ensure access through shadow region 0 */
2611 edma_or_array2(ecc, EDMA_DRAE, 0,
2612 EDMA_REG_ARRAY_INDEX(i),
2613 EDMA_CHANNEL_BIT(i));
2615 edma_setup_interrupt(&echan[i], true);
2617 /* Set up channel -> slot mapping for the entry slot */
2618 edma_set_chmap(&echan[i], echan[i].slot[0]);
2626 static const struct dev_pm_ops edma_pm_ops = {
2627 SET_LATE_SYSTEM_SLEEP_PM_OPS(edma_pm_suspend, edma_pm_resume)
2630 static struct platform_driver edma_driver = {
2631 .probe = edma_probe,
2632 .remove = edma_remove,
2636 .of_match_table = edma_of_ids,
2640 static int edma_tptc_probe(struct platform_device *pdev)
2642 pm_runtime_enable(&pdev->dev);
2643 return pm_runtime_get_sync(&pdev->dev);
2646 static struct platform_driver edma_tptc_driver = {
2647 .probe = edma_tptc_probe,
2649 .name = "edma3-tptc",
2650 .of_match_table = edma_tptc_of_ids,
2654 static bool edma_filter_fn(struct dma_chan *chan, void *param)
2658 if (chan->device->dev->driver == &edma_driver.driver) {
2659 struct edma_chan *echan = to_edma_chan(chan);
2660 unsigned ch_req = *(unsigned *)param;
2661 if (ch_req == echan->ch_num) {
2662 /* The channel is going to be used as HW synchronized */
2663 echan->hw_triggered = true;
2670 static int edma_init(void)
2674 ret = platform_driver_register(&edma_tptc_driver);
2678 return platform_driver_register(&edma_driver);
2680 subsys_initcall(edma_init);
2682 static void __exit edma_exit(void)
2684 platform_driver_unregister(&edma_driver);
2685 platform_driver_unregister(&edma_tptc_driver);
2687 module_exit(edma_exit);
2689 MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
2690 MODULE_DESCRIPTION("TI EDMA DMA engine driver");
2691 MODULE_LICENSE("GPL v2");