xref: /openbmc/linux/drivers/dma/ti/edma.c (revision 26b32974)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TI EDMA DMA engine driver
4  *
5  * Copyright 2012 Texas Instruments
6  */
7 
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>
19 #include <linux/of.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>
25 
26 #include <linux/platform_data/edma.h>
27 
28 #include "../dmaengine.h"
29 #include "../virt-dma.h"
30 
31 /* Offsets matching "struct edmacc_param" */
32 #define PARM_OPT		0x00
33 #define PARM_SRC		0x04
34 #define PARM_A_B_CNT		0x08
35 #define PARM_DST		0x0c
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
40 
41 #define PARM_SIZE		0x20
42 
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 */
58 #define SH_IEVAL		0x78
59 #define SH_QER			0x80
60 #define SH_QEER			0x84
61 #define SH_QEECR		0x88
62 #define SH_QEESR		0x8c
63 #define SH_QSER			0x90
64 #define SH_QSECR		0x94
65 #define SH_SIZE			0x200
66 
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
89 
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 */
95 
96 #define PARM_OFFSET(param_no)	(EDMA_PARM + ((param_no) << 5))
97 
98 #define EDMA_DCHMAP		0x0100  /* 64 registers */
99 
100 /* CCCFG register */
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)
107 
108 /* CCSTAT register */
109 #define EDMA_CCSTAT_ACTV	BIT(4)
110 
111 /*
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.
117  */
118 #define MAX_NR_SG		20
119 #define EDMA_MAX_SLOTS		MAX_NR_SG
120 #define EDMA_DESCRIPTORS	16
121 
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
127 
128 /*
129  * 64bit array registers are split into two 32bit registers:
130  * reg0: channel/event 0-31
131  * reg1: channel/event 32-63
132  *
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
135  */
136 #define EDMA_REG_ARRAY_INDEX(channel)	((channel) >> 5)
137 #define EDMA_CHANNEL_BIT(channel)	(BIT((channel) & 0x1f))
138 
139 /* PaRAM slots are laid out like this */
140 struct edmacc_param {
141 	u32 opt;
142 	u32 src;
143 	u32 a_b_cnt;
144 	u32 dst;
145 	u32 src_dst_bidx;
146 	u32 link_bcntrld;
147 	u32 src_dst_cidx;
148 	u32 ccnt;
149 } __packed;
150 
151 /* fields in edmacc_param.opt */
152 #define SAM		BIT(0)
153 #define DAM		BIT(1)
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)
163 
164 struct edma_pset {
165 	u32				len;
166 	dma_addr_t			addr;
167 	struct edmacc_param		param;
168 };
169 
170 struct edma_desc {
171 	struct virt_dma_desc		vdesc;
172 	struct list_head		node;
173 	enum dma_transfer_direction	direction;
174 	int				cyclic;
175 	bool				polled;
176 	int				absync;
177 	int				pset_nr;
178 	struct edma_chan		*echan;
179 	int				processed;
180 
181 	/*
182 	 * The following 4 elements are used for residue accounting.
183 	 *
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.
190 	 *
191 	 * - residue: The amount of bytes we have left to transfer for this desc
192 	 *
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.
196 	 *
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.
200 	 */
201 	int				processed_stat;
202 	u32				sg_len;
203 	u32				residue;
204 	u32				residue_stat;
205 
206 	struct edma_pset		pset[];
207 };
208 
209 struct edma_cc;
210 
211 struct edma_tc {
212 	struct device_node		*node;
213 	u16				id;
214 };
215 
216 struct edma_chan {
217 	struct virt_dma_chan		vchan;
218 	struct list_head		node;
219 	struct edma_desc		*edesc;
220 	struct edma_cc			*ecc;
221 	struct edma_tc			*tc;
222 	int				ch_num;
223 	bool				alloced;
224 	bool				hw_triggered;
225 	int				slot[EDMA_MAX_SLOTS];
226 	int				missed;
227 	struct dma_slave_config		cfg;
228 };
229 
230 struct edma_cc {
231 	struct device			*dev;
232 	struct edma_soc_info		*info;
233 	void __iomem			*base;
234 	int				id;
235 	bool				legacy_mode;
236 
237 	/* eDMA3 resource information */
238 	unsigned			num_channels;
239 	unsigned			num_qchannels;
240 	unsigned			num_region;
241 	unsigned			num_slots;
242 	unsigned			num_tc;
243 	bool				chmap_exist;
244 	enum dma_event_q		default_queue;
245 
246 	unsigned int			ccint;
247 	unsigned int			ccerrint;
248 
249 	/*
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.
252 	 */
253 	unsigned long *slot_inuse;
254 
255 	/*
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.
259 	 */
260 	unsigned long *channels_mask;
261 
262 	struct dma_device		dma_slave;
263 	struct dma_device		*dma_memcpy;
264 	struct edma_chan		*slave_chans;
265 	struct edma_tc			*tc_list;
266 	int				dummy_slot;
267 };
268 
269 /* dummy param set used to (re)initialize parameter RAM slots */
270 static const struct edmacc_param dummy_paramset = {
271 	.link_bcntrld = 0xffff,
272 	.ccnt = 1,
273 };
274 
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,
280 };
281 
282 static const struct of_device_id edma_of_ids[] = {
283 	{
284 		.compatible = "ti,edma3",
285 		.data = &edma_binding_type[EDMA_BINDING_LEGACY],
286 	},
287 	{
288 		.compatible = "ti,edma3-tpcc",
289 		.data = &edma_binding_type[EDMA_BINDING_TPCC],
290 	},
291 	{}
292 };
293 MODULE_DEVICE_TABLE(of, edma_of_ids);
294 
295 static const struct of_device_id edma_tptc_of_ids[] = {
296 	{ .compatible = "ti,edma3-tptc", },
297 	{}
298 };
299 MODULE_DEVICE_TABLE(of, edma_tptc_of_ids);
300 
301 static inline unsigned int edma_read(struct edma_cc *ecc, int offset)
302 {
303 	return (unsigned int)__raw_readl(ecc->base + offset);
304 }
305 
306 static inline void edma_write(struct edma_cc *ecc, int offset, int val)
307 {
308 	__raw_writel(val, ecc->base + offset);
309 }
310 
311 static inline void edma_modify(struct edma_cc *ecc, int offset, unsigned and,
312 			       unsigned or)
313 {
314 	unsigned val = edma_read(ecc, offset);
315 
316 	val &= and;
317 	val |= or;
318 	edma_write(ecc, offset, val);
319 }
320 
321 static inline void edma_or(struct edma_cc *ecc, int offset, unsigned or)
322 {
323 	unsigned val = edma_read(ecc, offset);
324 
325 	val |= or;
326 	edma_write(ecc, offset, val);
327 }
328 
329 static inline unsigned int edma_read_array(struct edma_cc *ecc, int offset,
330 					   int i)
331 {
332 	return edma_read(ecc, offset + (i << 2));
333 }
334 
335 static inline void edma_write_array(struct edma_cc *ecc, int offset, int i,
336 				    unsigned val)
337 {
338 	edma_write(ecc, offset + (i << 2), val);
339 }
340 
341 static inline void edma_modify_array(struct edma_cc *ecc, int offset, int i,
342 				     unsigned and, unsigned or)
343 {
344 	edma_modify(ecc, offset + (i << 2), and, or);
345 }
346 
347 static inline void edma_or_array2(struct edma_cc *ecc, int offset, int i, int j,
348 				  unsigned or)
349 {
350 	edma_or(ecc, offset + ((i * 2 + j) << 2), or);
351 }
352 
353 static inline void edma_write_array2(struct edma_cc *ecc, int offset, int i,
354 				     int j, unsigned val)
355 {
356 	edma_write(ecc, offset + ((i * 2 + j) << 2), val);
357 }
358 
359 static inline unsigned int edma_shadow0_read_array(struct edma_cc *ecc,
360 						   int offset, int i)
361 {
362 	return edma_read(ecc, EDMA_SHADOW0 + offset + (i << 2));
363 }
364 
365 static inline void edma_shadow0_write(struct edma_cc *ecc, int offset,
366 				      unsigned val)
367 {
368 	edma_write(ecc, EDMA_SHADOW0 + offset, val);
369 }
370 
371 static inline void edma_shadow0_write_array(struct edma_cc *ecc, int offset,
372 					    int i, unsigned val)
373 {
374 	edma_write(ecc, EDMA_SHADOW0 + offset + (i << 2), val);
375 }
376 
377 static inline void edma_param_modify(struct edma_cc *ecc, int offset,
378 				     int param_no, unsigned and, unsigned or)
379 {
380 	edma_modify(ecc, EDMA_PARM + offset + (param_no << 5), and, or);
381 }
382 
383 static void edma_assign_priority_to_queue(struct edma_cc *ecc, int queue_no,
384 					  int priority)
385 {
386 	int bit = queue_no * 4;
387 
388 	edma_modify(ecc, EDMA_QUEPRI, ~(0x7 << bit), ((priority & 0x7) << bit));
389 }
390 
391 static void edma_set_chmap(struct edma_chan *echan, int slot)
392 {
393 	struct edma_cc *ecc = echan->ecc;
394 	int channel = EDMA_CHAN_SLOT(echan->ch_num);
395 
396 	if (ecc->chmap_exist) {
397 		slot = EDMA_CHAN_SLOT(slot);
398 		edma_write_array(ecc, EDMA_DCHMAP, channel, (slot << 5));
399 	}
400 }
401 
402 static void edma_setup_interrupt(struct edma_chan *echan, bool enable)
403 {
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);
408 
409 	if (enable) {
410 		edma_shadow0_write_array(ecc, SH_ICR, idx, ch_bit);
411 		edma_shadow0_write_array(ecc, SH_IESR, idx, ch_bit);
412 	} else {
413 		edma_shadow0_write_array(ecc, SH_IECR, idx, ch_bit);
414 	}
415 }
416 
417 /*
418  * paRAM slot management functions
419  */
420 static void edma_write_slot(struct edma_cc *ecc, unsigned slot,
421 			    const struct edmacc_param *param)
422 {
423 	slot = EDMA_CHAN_SLOT(slot);
424 	if (slot >= ecc->num_slots)
425 		return;
426 	memcpy_toio(ecc->base + PARM_OFFSET(slot), param, PARM_SIZE);
427 }
428 
429 static int edma_read_slot(struct edma_cc *ecc, unsigned slot,
430 			   struct edmacc_param *param)
431 {
432 	slot = EDMA_CHAN_SLOT(slot);
433 	if (slot >= ecc->num_slots)
434 		return -EINVAL;
435 	memcpy_fromio(param, ecc->base + PARM_OFFSET(slot), PARM_SIZE);
436 
437 	return 0;
438 }
439 
440 /**
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"
444  *
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.
449  *
450  * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
451  * slots may be allocated on behalf of DSP firmware.
452  *
453  * Returns the number of the slot, else negative errno.
454  */
455 static int edma_alloc_slot(struct edma_cc *ecc, int slot)
456 {
457 	if (slot >= 0) {
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;
462 	}
463 
464 	if (slot < 0) {
465 		if (ecc->chmap_exist)
466 			slot = 0;
467 		else
468 			slot = ecc->num_channels;
469 		for (;;) {
470 			slot = find_next_zero_bit(ecc->slot_inuse,
471 						  ecc->num_slots,
472 						  slot);
473 			if (slot == ecc->num_slots)
474 				return -ENOMEM;
475 			if (!test_and_set_bit(slot, ecc->slot_inuse))
476 				break;
477 		}
478 	} else if (slot >= ecc->num_slots) {
479 		return -EINVAL;
480 	} else if (test_and_set_bit(slot, ecc->slot_inuse)) {
481 		return -EBUSY;
482 	}
483 
484 	edma_write_slot(ecc, slot, &dummy_paramset);
485 
486 	return EDMA_CTLR_CHAN(ecc->id, slot);
487 }
488 
489 static void edma_free_slot(struct edma_cc *ecc, unsigned slot)
490 {
491 	slot = EDMA_CHAN_SLOT(slot);
492 	if (slot >= ecc->num_slots)
493 		return;
494 
495 	edma_write_slot(ecc, slot, &dummy_paramset);
496 	clear_bit(slot, ecc->slot_inuse);
497 }
498 
499 /**
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
504  *
505  * The originating slot should not be part of any active DMA transfer.
506  */
507 static void edma_link(struct edma_cc *ecc, unsigned from, unsigned to)
508 {
509 	if (unlikely(EDMA_CTLR(from) != EDMA_CTLR(to)))
510 		dev_warn(ecc->dev, "Ignoring eDMA instance for linking\n");
511 
512 	from = EDMA_CHAN_SLOT(from);
513 	to = EDMA_CHAN_SLOT(to);
514 	if (from >= ecc->num_slots || to >= ecc->num_slots)
515 		return;
516 
517 	edma_param_modify(ecc, PARM_LINK_BCNTRLD, from, 0xffff0000,
518 			  PARM_OFFSET(to));
519 }
520 
521 /**
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
526  *
527  * Returns the position of the current active slot
528  */
529 static dma_addr_t edma_get_position(struct edma_cc *ecc, unsigned slot,
530 				    bool dst)
531 {
532 	u32 offs;
533 
534 	slot = EDMA_CHAN_SLOT(slot);
535 	offs = PARM_OFFSET(slot);
536 	offs += dst ? PARM_DST : PARM_SRC;
537 
538 	return edma_read(ecc, offs);
539 }
540 
541 /*
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.)
546  */
547 static void edma_start(struct edma_chan *echan)
548 {
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);
553 
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);
559 	} else {
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);
566 		/* Clear any SER */
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));
571 	}
572 }
573 
574 static void edma_stop(struct edma_chan *echan)
575 {
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);
580 
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);
585 
586 	/* clear possibly pending completion interrupt */
587 	edma_shadow0_write_array(ecc, SH_ICR, idx, ch_bit);
588 
589 	dev_dbg(ecc->dev, "EER%d %08x\n", idx,
590 		edma_shadow0_read_array(ecc, SH_EER, idx));
591 
592 	/* REVISIT:  consider guarding against inappropriate event
593 	 * chaining by overwriting with dummy_paramset.
594 	 */
595 }
596 
597 /*
598  * Temporarily disable EDMA hardware events on the specified channel,
599  * preventing them from triggering new transfers
600  */
601 static void edma_pause(struct edma_chan *echan)
602 {
603 	int channel = EDMA_CHAN_SLOT(echan->ch_num);
604 
605 	edma_shadow0_write_array(echan->ecc, SH_EECR,
606 				 EDMA_REG_ARRAY_INDEX(channel),
607 				 EDMA_CHANNEL_BIT(channel));
608 }
609 
610 /* Re-enable EDMA hardware events on the specified channel.  */
611 static void edma_resume(struct edma_chan *echan)
612 {
613 	int channel = EDMA_CHAN_SLOT(echan->ch_num);
614 
615 	edma_shadow0_write_array(echan->ecc, SH_EESR,
616 				 EDMA_REG_ARRAY_INDEX(channel),
617 				 EDMA_CHANNEL_BIT(channel));
618 }
619 
620 static void edma_trigger_channel(struct edma_chan *echan)
621 {
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);
626 
627 	edma_shadow0_write_array(ecc, SH_ESR, idx, ch_bit);
628 
629 	dev_dbg(ecc->dev, "ESR%d %08x\n", idx,
630 		edma_shadow0_read_array(ecc, SH_ESR, idx));
631 }
632 
633 static void edma_clean_channel(struct edma_chan *echan)
634 {
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);
639 
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);
645 	/* Clear any SER */
646 	edma_shadow0_write_array(ecc, SH_SECR, idx, ch_bit);
647 	edma_write(ecc, EDMA_CCERRCLR, BIT(16) | BIT(1) | BIT(0));
648 }
649 
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)
653 {
654 	struct edma_cc *ecc = echan->ecc;
655 	int channel = EDMA_CHAN_SLOT(echan->ch_num);
656 	int bit = (channel & 0x7) * 4;
657 
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)
662 		return;
663 
664 	eventq_no &= 7;
665 	edma_modify_array(ecc, EDMA_DMAQNUM, (channel >> 3), ~(0x7 << bit),
666 			  eventq_no << bit);
667 }
668 
669 static int edma_alloc_channel(struct edma_chan *echan,
670 			      enum dma_event_q eventq_no)
671 {
672 	struct edma_cc *ecc = echan->ecc;
673 	int channel = EDMA_CHAN_SLOT(echan->ch_num);
674 
675 	if (!test_bit(echan->ch_num, ecc->channels_mask)) {
676 		dev_err(ecc->dev, "Channel%d is reserved, can not be used!\n",
677 			echan->ch_num);
678 		return -EINVAL;
679 	}
680 
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));
684 
685 	/* ensure no events are pending */
686 	edma_stop(echan);
687 
688 	edma_setup_interrupt(echan, true);
689 
690 	edma_assign_channel_eventq(echan, eventq_no);
691 
692 	return 0;
693 }
694 
695 static void edma_free_channel(struct edma_chan *echan)
696 {
697 	/* ensure no events are pending */
698 	edma_stop(echan);
699 	/* REVISIT should probably take out of shadow region 0 */
700 	edma_setup_interrupt(echan, false);
701 }
702 
703 static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
704 {
705 	return container_of(c, struct edma_chan, vchan.chan);
706 }
707 
708 static inline struct edma_desc *to_edma_desc(struct dma_async_tx_descriptor *tx)
709 {
710 	return container_of(tx, struct edma_desc, vdesc.tx);
711 }
712 
713 static void edma_desc_free(struct virt_dma_desc *vdesc)
714 {
715 	kfree(container_of(vdesc, struct edma_desc, vdesc));
716 }
717 
718 /* Dispatch a queued descriptor to the controller (caller holds lock) */
719 static void edma_execute(struct edma_chan *echan)
720 {
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;
726 
727 	if (!echan->edesc) {
728 		/* Setup is needed for the first transfer */
729 		vdesc = vchan_next_desc(&echan->vchan);
730 		if (!vdesc)
731 			return;
732 		list_del(&vdesc->node);
733 		echan->edesc = to_edma_desc(&vdesc->tx);
734 	}
735 
736 	edesc = echan->edesc;
737 
738 	/* Find out how many left */
739 	left = edesc->pset_nr - edesc->processed;
740 	nslots = min(MAX_NR_SG, left);
741 	edesc->sg_len = 0;
742 
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;
748 		dev_vdbg(dev,
749 			 "\n pset[%d]:\n"
750 			 "  chnum\t%d\n"
751 			 "  slot\t%d\n"
752 			 "  opt\t%08x\n"
753 			 "  src\t%08x\n"
754 			 "  dst\t%08x\n"
755 			 "  abcnt\t%08x\n"
756 			 "  ccnt\t%08x\n"
757 			 "  bidx\t%08x\n"
758 			 "  cidx\t%08x\n"
759 			 "  lkrld\t%08x\n",
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]);
772 	}
773 
774 	edesc->processed += nslots;
775 
776 	/*
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
780 	 */
781 	if (edesc->processed == edesc->pset_nr) {
782 		if (edesc->cyclic)
783 			edma_link(ecc, echan->slot[nslots - 1], echan->slot[1]);
784 		else
785 			edma_link(ecc, echan->slot[nslots - 1],
786 				  echan->ecc->dummy_slot);
787 	}
788 
789 	if (echan->missed) {
790 		/*
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
794 		 */
795 		dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
796 		edma_clean_channel(echan);
797 		edma_stop(echan);
798 		edma_start(echan);
799 		edma_trigger_channel(echan);
800 		echan->missed = 0;
801 	} else if (edesc->processed <= MAX_NR_SG) {
802 		dev_dbg(dev, "first transfer starting on channel %d\n",
803 			echan->ch_num);
804 		edma_start(echan);
805 	} else {
806 		dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
807 			echan->ch_num, edesc->processed);
808 		edma_resume(echan);
809 	}
810 }
811 
812 static int edma_terminate_all(struct dma_chan *chan)
813 {
814 	struct edma_chan *echan = to_edma_chan(chan);
815 	unsigned long flags;
816 	LIST_HEAD(head);
817 
818 	spin_lock_irqsave(&echan->vchan.lock, flags);
819 
820 	/*
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.)
824 	 */
825 	if (echan->edesc) {
826 		edma_stop(echan);
827 		/* Move the cyclic channel back to default queue */
828 		if (!echan->tc && echan->edesc->cyclic)
829 			edma_assign_channel_eventq(echan, EVENTQ_DEFAULT);
830 
831 		vchan_terminate_vdesc(&echan->edesc->vdesc);
832 		echan->edesc = NULL;
833 	}
834 
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);
838 
839 	return 0;
840 }
841 
842 static void edma_synchronize(struct dma_chan *chan)
843 {
844 	struct edma_chan *echan = to_edma_chan(chan);
845 
846 	vchan_synchronize(&echan->vchan);
847 }
848 
849 static int edma_slave_config(struct dma_chan *chan,
850 	struct dma_slave_config *cfg)
851 {
852 	struct edma_chan *echan = to_edma_chan(chan);
853 
854 	if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
855 	    cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
856 		return -EINVAL;
857 
858 	if (cfg->src_maxburst > chan->device->max_burst ||
859 	    cfg->dst_maxburst > chan->device->max_burst)
860 		return -EINVAL;
861 
862 	memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
863 
864 	return 0;
865 }
866 
867 static int edma_dma_pause(struct dma_chan *chan)
868 {
869 	struct edma_chan *echan = to_edma_chan(chan);
870 
871 	if (!echan->edesc)
872 		return -EINVAL;
873 
874 	edma_pause(echan);
875 	return 0;
876 }
877 
878 static int edma_dma_resume(struct dma_chan *chan)
879 {
880 	struct edma_chan *echan = to_edma_chan(chan);
881 
882 	edma_resume(echan);
883 	return 0;
884 }
885 
886 /*
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
896  */
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)
901 {
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;
907 	int absync;
908 
909 	/* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
910 	if (!burst)
911 		burst = 1;
912 	/*
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.
916 	 */
917 	if (burst == 1) {
918 		/*
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.
929 		 */
930 		absync = false;
931 		ccnt = dma_length / acnt / (SZ_64K - 1);
932 		bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
933 		/*
934 		 * If bcnt is non-zero, we have a remainder and hence an
935 		 * extra frame to transfer, so increment ccnt.
936 		 */
937 		if (bcnt)
938 			ccnt++;
939 		else
940 			bcnt = SZ_64K - 1;
941 		cidx = acnt;
942 	} else {
943 		/*
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.
951 		 */
952 		absync = true;
953 		bcnt = burst;
954 		ccnt = dma_length / (acnt * bcnt);
955 		if (ccnt > (SZ_64K - 1)) {
956 			dev_err(dev, "Exceeded max SG segment size\n");
957 			return -EINVAL;
958 		}
959 		cidx = acnt * bcnt;
960 	}
961 
962 	epset->len = dma_length;
963 
964 	if (direction == DMA_MEM_TO_DEV) {
965 		src_bidx = acnt;
966 		src_cidx = cidx;
967 		dst_bidx = 0;
968 		dst_cidx = 0;
969 		epset->addr = src_addr;
970 	} else if (direction == DMA_DEV_TO_MEM)  {
971 		src_bidx = 0;
972 		src_cidx = 0;
973 		dst_bidx = acnt;
974 		dst_cidx = cidx;
975 		epset->addr = dst_addr;
976 	} else if (direction == DMA_MEM_TO_MEM)  {
977 		src_bidx = acnt;
978 		src_cidx = cidx;
979 		dst_bidx = acnt;
980 		dst_cidx = cidx;
981 		epset->addr = src_addr;
982 	} else {
983 		dev_err(dev, "%s: direction not implemented yet\n", __func__);
984 		return -EINVAL;
985 	}
986 
987 	param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
988 	/* Configure A or AB synchronized transfers */
989 	if (absync)
990 		param->opt |= SYNCDIM;
991 
992 	param->src = src_addr;
993 	param->dst = dst_addr;
994 
995 	param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
996 	param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
997 
998 	param->a_b_cnt = bcnt << 16 | acnt;
999 	param->ccnt = ccnt;
1000 	/*
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.
1005 	 */
1006 	param->link_bcntrld = 0xffffffff;
1007 	return absync;
1008 }
1009 
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)
1014 {
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;
1020 	u32 burst;
1021 	struct scatterlist *sg;
1022 	int i, nslots, ret;
1023 
1024 	if (unlikely(!echan || !sgl || !sg_len))
1025 		return NULL;
1026 
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;
1035 	} else {
1036 		dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
1037 		return NULL;
1038 	}
1039 
1040 	if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
1041 		dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
1042 		return NULL;
1043 	}
1044 
1045 	edesc = kzalloc(struct_size(edesc, pset, sg_len), GFP_ATOMIC);
1046 	if (!edesc)
1047 		return NULL;
1048 
1049 	edesc->pset_nr = sg_len;
1050 	edesc->residue = 0;
1051 	edesc->direction = direction;
1052 	edesc->echan = echan;
1053 
1054 	/* Allocate a PaRAM slot, if needed */
1055 	nslots = min_t(unsigned, MAX_NR_SG, sg_len);
1056 
1057 	for (i = 0; i < nslots; i++) {
1058 		if (echan->slot[i] < 0) {
1059 			echan->slot[i] =
1060 				edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
1061 			if (echan->slot[i] < 0) {
1062 				kfree(edesc);
1063 				dev_err(dev, "%s: Failed to allocate slot\n",
1064 					__func__);
1065 				return NULL;
1066 			}
1067 		}
1068 	}
1069 
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);
1075 		else
1076 			src_addr = sg_dma_address(sg);
1077 
1078 		ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1079 				       dst_addr, burst, dev_width,
1080 				       sg_dma_len(sg), direction);
1081 		if (ret < 0) {
1082 			kfree(edesc);
1083 			return NULL;
1084 		}
1085 
1086 		edesc->absync = ret;
1087 		edesc->residue += sg_dma_len(sg);
1088 
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))
1093 			/*
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.
1098 			 */
1099 			edesc->pset[i].param.opt |= (TCINTEN | TCCMODE);
1100 	}
1101 	edesc->residue_stat = edesc->residue;
1102 
1103 	return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1104 }
1105 
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)
1109 {
1110 	int ret, nslots;
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;
1115 
1116 	if (unlikely(!echan || !len))
1117 		return NULL;
1118 
1119 	/* Align the array size (acnt block) with the transfer properties */
1120 	switch (__ffs((src | dest | len))) {
1121 	case 0:
1122 		array_size = SZ_32K - 1;
1123 		break;
1124 	case 1:
1125 		array_size = SZ_32K - 2;
1126 		break;
1127 	default:
1128 		array_size = SZ_32K - 4;
1129 		break;
1130 	}
1131 
1132 	if (len < SZ_64K) {
1133 		/*
1134 		 * Transfer size less than 64K can be handled with one paRAM
1135 		 * slot and with one burst.
1136 		 * ACNT = length
1137 		 */
1138 		width = len;
1139 		pset_len = len;
1140 		nslots = 1;
1141 	} else {
1142 		/*
1143 		 * Transfer size bigger than 64K will be handled with maximum of
1144 		 * two paRAM slots.
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
1149 		 *
1150 		 * When the full_length is a multiple of 32767 one slot can be
1151 		 * used to complete the transfer.
1152 		 */
1153 		width = array_size;
1154 		pset_len = rounddown(len, width);
1155 		/* One slot is enough for lengths multiple of (SZ_32K -1) */
1156 		if (unlikely(pset_len == len))
1157 			nslots = 1;
1158 		else
1159 			nslots = 2;
1160 	}
1161 
1162 	edesc = kzalloc(struct_size(edesc, pset, nslots), GFP_ATOMIC);
1163 	if (!edesc)
1164 		return NULL;
1165 
1166 	edesc->pset_nr = nslots;
1167 	edesc->residue = edesc->residue_stat = len;
1168 	edesc->direction = DMA_MEM_TO_MEM;
1169 	edesc->echan = echan;
1170 
1171 	ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
1172 			       width, pset_len, DMA_MEM_TO_MEM);
1173 	if (ret < 0) {
1174 		kfree(edesc);
1175 		return NULL;
1176 	}
1177 
1178 	edesc->absync = ret;
1179 
1180 	edesc->pset[0].param.opt |= ITCCHEN;
1181 	if (nslots == 1) {
1182 		/* Enable transfer complete interrupt if requested */
1183 		if (tx_flags & DMA_PREP_INTERRUPT)
1184 			edesc->pset[0].param.opt |= TCINTEN;
1185 	} else {
1186 		/* Enable transfer complete chaining for the first slot */
1187 		edesc->pset[0].param.opt |= TCCHEN;
1188 
1189 		if (echan->slot[1] < 0) {
1190 			echan->slot[1] = edma_alloc_slot(echan->ecc,
1191 							 EDMA_SLOT_ANY);
1192 			if (echan->slot[1] < 0) {
1193 				kfree(edesc);
1194 				dev_err(dev, "%s: Failed to allocate slot\n",
1195 					__func__);
1196 				return NULL;
1197 			}
1198 		}
1199 		dest += pset_len;
1200 		src += pset_len;
1201 		pset_len = width = len % array_size;
1202 
1203 		ret = edma_config_pset(chan, &edesc->pset[1], src, dest, 1,
1204 				       width, pset_len, DMA_MEM_TO_MEM);
1205 		if (ret < 0) {
1206 			kfree(edesc);
1207 			return NULL;
1208 		}
1209 
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;
1214 	}
1215 
1216 	if (!(tx_flags & DMA_PREP_INTERRUPT))
1217 		edesc->polled = true;
1218 
1219 	return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1220 }
1221 
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)
1226 {
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;
1233 
1234 	/* Slave mode is not supported */
1235 	if (is_slave_direction(xt->dir))
1236 		return NULL;
1237 
1238 	if (xt->frame_size != 1 || xt->numf == 0)
1239 		return NULL;
1240 
1241 	if (xt->sgl[0].size > SZ_64K || xt->numf > SZ_64K)
1242 		return NULL;
1243 
1244 	src_icg = dmaengine_get_src_icg(xt, &xt->sgl[0]);
1245 	if (src_icg) {
1246 		src_bidx = src_icg + xt->sgl[0].size;
1247 	} else if (xt->src_inc) {
1248 		src_bidx = xt->sgl[0].size;
1249 	} else {
1250 		dev_err(dev, "%s: SRC constant addressing is not supported\n",
1251 			__func__);
1252 		return NULL;
1253 	}
1254 
1255 	dst_icg = dmaengine_get_dst_icg(xt, &xt->sgl[0]);
1256 	if (dst_icg) {
1257 		dst_bidx = dst_icg + xt->sgl[0].size;
1258 	} else if (xt->dst_inc) {
1259 		dst_bidx = xt->sgl[0].size;
1260 	} else {
1261 		dev_err(dev, "%s: DST constant addressing is not supported\n",
1262 			__func__);
1263 		return NULL;
1264 	}
1265 
1266 	if (src_bidx > SZ_64K || dst_bidx > SZ_64K)
1267 		return NULL;
1268 
1269 	edesc = kzalloc(struct_size(edesc, pset, 1), GFP_ATOMIC);
1270 	if (!edesc)
1271 		return NULL;
1272 
1273 	edesc->direction = DMA_MEM_TO_MEM;
1274 	edesc->echan = echan;
1275 	edesc->pset_nr = 1;
1276 
1277 	param = &edesc->pset[0].param;
1278 
1279 	param->src = xt->src_start;
1280 	param->dst = xt->dst_start;
1281 	param->a_b_cnt = xt->numf << 16 | xt->sgl[0].size;
1282 	param->ccnt = 1;
1283 	param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
1284 	param->src_dst_cidx = 0;
1285 
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;
1291 	else
1292 		edesc->polled = true;
1293 
1294 	return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1295 }
1296 
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)
1301 {
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;
1308 	u32 burst;
1309 	int i, ret, nslots;
1310 
1311 	if (unlikely(!echan || !buf_len || !period_len))
1312 		return NULL;
1313 
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;
1324 	} else {
1325 		dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
1326 		return NULL;
1327 	}
1328 
1329 	if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
1330 		dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
1331 		return NULL;
1332 	}
1333 
1334 	if (unlikely(buf_len % period_len)) {
1335 		dev_err(dev, "Period should be multiple of Buffer length\n");
1336 		return NULL;
1337 	}
1338 
1339 	nslots = (buf_len / period_len) + 1;
1340 
1341 	/*
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.
1348 	 */
1349 	if (nslots > MAX_NR_SG) {
1350 		/*
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.
1355 		 */
1356 		if (burst == period_len) {
1357 			period_len = buf_len;
1358 			nslots = 2;
1359 			use_intermediate = true;
1360 		} else {
1361 			return NULL;
1362 		}
1363 	}
1364 
1365 	edesc = kzalloc(struct_size(edesc, pset, nslots), GFP_ATOMIC);
1366 	if (!edesc)
1367 		return NULL;
1368 
1369 	edesc->cyclic = 1;
1370 	edesc->pset_nr = nslots;
1371 	edesc->residue = edesc->residue_stat = buf_len;
1372 	edesc->direction = direction;
1373 	edesc->echan = echan;
1374 
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);
1377 
1378 	for (i = 0; i < nslots; i++) {
1379 		/* Allocate a PaRAM slot, if needed */
1380 		if (echan->slot[i] < 0) {
1381 			echan->slot[i] =
1382 				edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
1383 			if (echan->slot[i] < 0) {
1384 				kfree(edesc);
1385 				dev_err(dev, "%s: Failed to allocate slot\n",
1386 					__func__);
1387 				return NULL;
1388 			}
1389 		}
1390 
1391 		if (i == nslots - 1) {
1392 			memcpy(&edesc->pset[i], &edesc->pset[0],
1393 			       sizeof(edesc->pset[0]));
1394 			break;
1395 		}
1396 
1397 		ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
1398 				       dst_addr, burst, dev_width, period_len,
1399 				       direction);
1400 		if (ret < 0) {
1401 			kfree(edesc);
1402 			return NULL;
1403 		}
1404 
1405 		if (direction == DMA_DEV_TO_MEM)
1406 			dst_addr += period_len;
1407 		else
1408 			src_addr += period_len;
1409 
1410 		dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
1411 		dev_vdbg(dev,
1412 			"\n pset[%d]:\n"
1413 			"  chnum\t%d\n"
1414 			"  slot\t%d\n"
1415 			"  opt\t%08x\n"
1416 			"  src\t%08x\n"
1417 			"  dst\t%08x\n"
1418 			"  abcnt\t%08x\n"
1419 			"  ccnt\t%08x\n"
1420 			"  bidx\t%08x\n"
1421 			"  cidx\t%08x\n"
1422 			"  lkrld\t%08x\n",
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);
1432 
1433 		edesc->absync = ret;
1434 
1435 		/*
1436 		 * Enable period interrupt only if it is requested
1437 		 */
1438 		if (tx_flags & DMA_PREP_INTERRUPT) {
1439 			edesc->pset[i].param.opt |= TCINTEN;
1440 
1441 			/* Also enable intermediate interrupts if necessary */
1442 			if (use_intermediate)
1443 				edesc->pset[i].param.opt |= ITCINTEN;
1444 		}
1445 	}
1446 
1447 	/* Place the cyclic channel to highest priority queue */
1448 	if (!echan->tc)
1449 		edma_assign_channel_eventq(echan, EVENTQ_0);
1450 
1451 	return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
1452 }
1453 
1454 static void edma_completion_handler(struct edma_chan *echan)
1455 {
1456 	struct device *dev = echan->vchan.chan.device->dev;
1457 	struct edma_desc *edesc;
1458 
1459 	spin_lock(&echan->vchan.lock);
1460 	edesc = echan->edesc;
1461 	if (edesc) {
1462 		if (edesc->cyclic) {
1463 			vchan_cyclic_callback(&edesc->vdesc);
1464 			spin_unlock(&echan->vchan.lock);
1465 			return;
1466 		} else if (edesc->processed == edesc->pset_nr) {
1467 			edesc->residue = 0;
1468 			edma_stop(echan);
1469 			vchan_cookie_complete(&edesc->vdesc);
1470 			echan->edesc = NULL;
1471 
1472 			dev_dbg(dev, "Transfer completed on channel %d\n",
1473 				echan->ch_num);
1474 		} else {
1475 			dev_dbg(dev, "Sub transfer completed on channel %d\n",
1476 				echan->ch_num);
1477 
1478 			edma_pause(echan);
1479 
1480 			/* Update statistics for tx_status */
1481 			edesc->residue -= edesc->sg_len;
1482 			edesc->residue_stat = edesc->residue;
1483 			edesc->processed_stat = edesc->processed;
1484 		}
1485 		edma_execute(echan);
1486 	}
1487 
1488 	spin_unlock(&echan->vchan.lock);
1489 }
1490 
1491 /* eDMA interrupt handler */
1492 static irqreturn_t dma_irq_handler(int irq, void *data)
1493 {
1494 	struct edma_cc *ecc = data;
1495 	int ctlr;
1496 	u32 sh_ier;
1497 	u32 sh_ipr;
1498 	u32 bank;
1499 
1500 	ctlr = ecc->id;
1501 	if (ctlr < 0)
1502 		return IRQ_NONE;
1503 
1504 	dev_vdbg(ecc->dev, "dma_irq_handler\n");
1505 
1506 	sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 0);
1507 	if (!sh_ipr) {
1508 		sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 1);
1509 		if (!sh_ipr)
1510 			return IRQ_NONE;
1511 		sh_ier = edma_shadow0_read_array(ecc, SH_IER, 1);
1512 		bank = 1;
1513 	} else {
1514 		sh_ier = edma_shadow0_read_array(ecc, SH_IER, 0);
1515 		bank = 0;
1516 	}
1517 
1518 	do {
1519 		u32 slot;
1520 		u32 channel;
1521 
1522 		slot = __ffs(sh_ipr);
1523 		sh_ipr &= ~(BIT(slot));
1524 
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]);
1530 		}
1531 	} while (sh_ipr);
1532 
1533 	edma_shadow0_write(ecc, SH_IEVAL, 1);
1534 	return IRQ_HANDLED;
1535 }
1536 
1537 static void edma_error_handler(struct edma_chan *echan)
1538 {
1539 	struct edma_cc *ecc = echan->ecc;
1540 	struct device *dev = echan->vchan.chan.device->dev;
1541 	struct edmacc_param p;
1542 	int err;
1543 
1544 	if (!echan->edesc)
1545 		return;
1546 
1547 	spin_lock(&echan->vchan.lock);
1548 
1549 	err = edma_read_slot(ecc, echan->slot[0], &p);
1550 
1551 	/*
1552 	 * Issue later based on missed flag which will be sure
1553 	 * to happen as:
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.
1558 	 *
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.
1562 	 */
1563 	if (err || (p.a_b_cnt == 0 && p.ccnt == 0)) {
1564 		dev_dbg(dev, "Error on null slot, setting miss\n");
1565 		echan->missed = 1;
1566 	} else {
1567 		/*
1568 		 * The slot is already programmed but the event got
1569 		 * missed, so its safe to issue it here.
1570 		 */
1571 		dev_dbg(dev, "Missed event, TRIGGERING\n");
1572 		edma_clean_channel(echan);
1573 		edma_stop(echan);
1574 		edma_start(echan);
1575 		edma_trigger_channel(echan);
1576 	}
1577 	spin_unlock(&echan->vchan.lock);
1578 }
1579 
1580 static inline bool edma_error_pending(struct edma_cc *ecc)
1581 {
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))
1585 		return true;
1586 
1587 	return false;
1588 }
1589 
1590 /* eDMA error interrupt handler */
1591 static irqreturn_t dma_ccerr_handler(int irq, void *data)
1592 {
1593 	struct edma_cc *ecc = data;
1594 	int i, j;
1595 	int ctlr;
1596 	unsigned int cnt = 0;
1597 	unsigned int val;
1598 
1599 	ctlr = ecc->id;
1600 	if (ctlr < 0)
1601 		return IRQ_NONE;
1602 
1603 	dev_vdbg(ecc->dev, "dma_ccerr_handler\n");
1604 
1605 	if (!edma_error_pending(ecc)) {
1606 		/*
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.
1610 		 */
1611 		dev_err(ecc->dev, "%s: Error interrupt without error event!\n",
1612 			__func__);
1613 		edma_write(ecc, EDMA_EEVAL, 1);
1614 		return IRQ_NONE;
1615 	}
1616 
1617 	while (1) {
1618 		/* Event missed register(s) */
1619 		for (j = 0; j < 2; j++) {
1620 			unsigned long emr;
1621 
1622 			val = edma_read_array(ecc, EDMA_EMR, j);
1623 			if (!val)
1624 				continue;
1625 
1626 			dev_dbg(ecc->dev, "EMR%d 0x%08x\n", j, val);
1627 			emr = val;
1628 			for_each_set_bit(i, &emr, 32) {
1629 				int k = (j << 5) + i;
1630 
1631 				/* Clear the corresponding EMR bits */
1632 				edma_write_array(ecc, EDMA_EMCR, j, BIT(i));
1633 				/* Clear any SER */
1634 				edma_shadow0_write_array(ecc, SH_SECR, j,
1635 							 BIT(i));
1636 				edma_error_handler(&ecc->slave_chans[k]);
1637 			}
1638 		}
1639 
1640 		val = edma_read(ecc, EDMA_QEMR);
1641 		if (val) {
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);
1646 		}
1647 
1648 		val = edma_read(ecc, EDMA_CCERR);
1649 		if (val) {
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);
1653 		}
1654 
1655 		if (!edma_error_pending(ecc))
1656 			break;
1657 		cnt++;
1658 		if (cnt > 10)
1659 			break;
1660 	}
1661 	edma_write(ecc, EDMA_EEVAL, 1);
1662 	return IRQ_HANDLED;
1663 }
1664 
1665 /* Alloc channel resources */
1666 static int edma_alloc_chan_resources(struct dma_chan *chan)
1667 {
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;
1672 	int ret;
1673 
1674 	if (echan->tc) {
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;
1680 	}
1681 
1682 	ret = edma_alloc_channel(echan, eventq_no);
1683 	if (ret)
1684 		return ret;
1685 
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];
1691 		goto err_slot;
1692 	}
1693 
1694 	/* Set up channel -> slot mapping for the entry slot */
1695 	edma_set_chmap(echan, echan->slot[0]);
1696 	echan->alloced = true;
1697 
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");
1701 
1702 	return 0;
1703 
1704 err_slot:
1705 	edma_free_channel(echan);
1706 	return ret;
1707 }
1708 
1709 /* Free channel resources */
1710 static void edma_free_chan_resources(struct dma_chan *chan)
1711 {
1712 	struct edma_chan *echan = to_edma_chan(chan);
1713 	struct device *dev = echan->ecc->dev;
1714 	int i;
1715 
1716 	/* Terminate transfers */
1717 	edma_stop(echan);
1718 
1719 	vchan_free_chan_resources(&echan->vchan);
1720 
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;
1726 		}
1727 	}
1728 
1729 	/* Set entry slot to the dummy slot */
1730 	edma_set_chmap(echan, echan->ecc->dummy_slot);
1731 
1732 	/* Free EDMA channel */
1733 	if (echan->alloced) {
1734 		edma_free_channel(echan);
1735 		echan->alloced = false;
1736 	}
1737 
1738 	echan->tc = NULL;
1739 	echan->hw_triggered = false;
1740 
1741 	dev_dbg(dev, "Free eDMA channel %d for virt channel %d\n",
1742 		EDMA_CHAN_SLOT(echan->ch_num), chan->chan_id);
1743 }
1744 
1745 /* Send pending descriptor to hardware */
1746 static void edma_issue_pending(struct dma_chan *chan)
1747 {
1748 	struct edma_chan *echan = to_edma_chan(chan);
1749 	unsigned long flags;
1750 
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);
1755 }
1756 
1757 /*
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.
1763  */
1764 #define EDMA_MAX_TR_WAIT_LOOPS 1000
1765 
1766 static u32 edma_residue(struct edma_desc *edesc)
1767 {
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);
1776 	int event_reg;
1777 	int i;
1778 
1779 	/*
1780 	 * We always read the dst/src position from the first RamPar
1781 	 * pset. That's the one which is active now.
1782 	 */
1783 	pos = edma_get_position(echan->ecc, echan->slot[0], dst);
1784 
1785 	/*
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
1792 	 */
1793 	if (is_slave_direction(edesc->direction))
1794 		event_reg = SH_ER;
1795 	else
1796 		event_reg = SH_ESR;
1797 
1798 	pos_old = pos;
1799 	while (edma_shadow0_read_array(echan->ecc, event_reg, idx) & ch_bit) {
1800 		pos = edma_get_position(echan->ecc, echan->slot[0], dst);
1801 		if (pos != pos_old)
1802 			break;
1803 
1804 		if (!--loop_count) {
1805 			dev_dbg_ratelimited(echan->vchan.chan.device->dev,
1806 				"%s: timeout waiting for PaRAM update\n",
1807 				__func__);
1808 			break;
1809 		}
1810 
1811 		cpu_relax();
1812 	}
1813 
1814 	/*
1815 	 * Cyclic is simple. Just subtract pset[0].addr from pos.
1816 	 *
1817 	 * We never update edesc->residue in the cyclic case, so we
1818 	 * can tell the remaining room to the end of the circular
1819 	 * buffer.
1820 	 */
1821 	if (edesc->cyclic) {
1822 		done = pos - pset->addr;
1823 		edesc->residue_stat = edesc->residue - done;
1824 		return edesc->residue_stat;
1825 	}
1826 
1827 	/*
1828 	 * If the position is 0, then EDMA loaded the closing dummy slot, the
1829 	 * transfer is completed
1830 	 */
1831 	if (!pos)
1832 		return 0;
1833 	/*
1834 	 * For SG operation we catch up with the last processed
1835 	 * status.
1836 	 */
1837 	pset += edesc->processed_stat;
1838 
1839 	for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
1840 		/*
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.
1844 		 */
1845 		if (pos >= pset->addr && pos < pset->addr + pset->len)
1846 			return edesc->residue_stat - (pos - pset->addr);
1847 
1848 		/* Otherwise mark it done and update residue_stat. */
1849 		edesc->processed_stat++;
1850 		edesc->residue_stat -= pset->len;
1851 	}
1852 	return edesc->residue_stat;
1853 }
1854 
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)
1859 {
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;
1864 
1865 	ret = dma_cookie_status(chan, cookie, txstate);
1866 
1867 	if (ret == DMA_COMPLETE)
1868 		return ret;
1869 
1870 	/* Provide a dummy dma_tx_state for completion checking */
1871 	if (!txstate)
1872 		txstate = &txstate_tmp;
1873 
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);
1877 	} else {
1878 		struct virt_dma_desc *vdesc = vchan_find_desc(&echan->vchan,
1879 							      cookie);
1880 
1881 		if (vdesc)
1882 			txstate->residue = to_edma_desc(&vdesc->tx)->residue;
1883 		else
1884 			txstate->residue = 0;
1885 	}
1886 
1887 	/*
1888 	 * Mark the cookie completed if the residue is 0 for non cyclic
1889 	 * transfers
1890 	 */
1891 	if (ret != DMA_COMPLETE && !txstate->residue &&
1892 	    echan->edesc && echan->edesc->polled &&
1893 	    echan->edesc->vdesc.tx.cookie == cookie) {
1894 		edma_stop(echan);
1895 		vchan_cookie_complete(&echan->edesc->vdesc);
1896 		echan->edesc = NULL;
1897 		edma_execute(echan);
1898 		ret = DMA_COMPLETE;
1899 	}
1900 
1901 	spin_unlock_irqrestore(&echan->vchan.lock, flags);
1902 
1903 	return ret;
1904 }
1905 
1906 static bool edma_is_memcpy_channel(int ch_num, s32 *memcpy_channels)
1907 {
1908 	if (!memcpy_channels)
1909 		return false;
1910 	while (*memcpy_channels != -1) {
1911 		if (*memcpy_channels == ch_num)
1912 			return true;
1913 		memcpy_channels++;
1914 	}
1915 	return false;
1916 }
1917 
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))
1922 
1923 static void edma_dma_init(struct edma_cc *ecc, bool legacy_mode)
1924 {
1925 	struct dma_device *s_ddev = &ecc->dma_slave;
1926 	struct dma_device *m_ddev = NULL;
1927 	s32 *memcpy_channels = ecc->info->memcpy_channels;
1928 	int i, j;
1929 
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) {
1934 		dev_warn(ecc->dev,
1935 			 "Legacy memcpy is enabled, things might not work\n");
1936 
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);
1942 	}
1943 
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;
1955 
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 */
1961 
1962 	s_ddev->dev = ecc->dev;
1963 	INIT_LIST_HEAD(&s_ddev->channels);
1964 
1965 	if (memcpy_channels) {
1966 		m_ddev = devm_kzalloc(ecc->dev, sizeof(*m_ddev), GFP_KERNEL);
1967 		if (!m_ddev) {
1968 			dev_warn(ecc->dev, "memcpy is disabled due to OoM\n");
1969 			memcpy_channels = NULL;
1970 			goto ch_setup;
1971 		}
1972 		ecc->dma_memcpy = m_ddev;
1973 
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);
1977 
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;
1989 
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;
1994 
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");
1999 	}
2000 
2001 ch_setup:
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);
2005 		echan->ecc = ecc;
2006 		echan->vchan.desc_free = edma_desc_free;
2007 
2008 		if (m_ddev && edma_is_memcpy_channel(i, memcpy_channels))
2009 			vchan_init(&echan->vchan, m_ddev);
2010 		else
2011 			vchan_init(&echan->vchan, s_ddev);
2012 
2013 		INIT_LIST_HEAD(&echan->node);
2014 		for (j = 0; j < EDMA_MAX_SLOTS; j++)
2015 			echan->slot[j] = -1;
2016 	}
2017 }
2018 
2019 static int edma_setup_from_hw(struct device *dev, struct edma_soc_info *pdata,
2020 			      struct edma_cc *ecc)
2021 {
2022 	int i;
2023 	u32 value, cccfg;
2024 	s8 (*queue_priority_map)[2];
2025 
2026 	/* Decode the eDMA3 configuration from CCCFG register */
2027 	cccfg = edma_read(ecc, EDMA_CCCFG);
2028 
2029 	value = GET_NUM_REGN(cccfg);
2030 	ecc->num_region = BIT(value);
2031 
2032 	value = GET_NUM_DMACH(cccfg);
2033 	ecc->num_channels = BIT(value + 1);
2034 
2035 	value = GET_NUM_QDMACH(cccfg);
2036 	ecc->num_qchannels = value * 2;
2037 
2038 	value = GET_NUM_PAENTRY(cccfg);
2039 	ecc->num_slots = BIT(value + 4);
2040 
2041 	value = GET_NUM_EVQUE(cccfg);
2042 	ecc->num_tc = value + 1;
2043 
2044 	ecc->chmap_exist = (cccfg & CHMAP_EXIST) ? true : false;
2045 
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");
2053 
2054 	/* Nothing need to be done if queue priority is provided */
2055 	if (pdata->queue_priority_mapping)
2056 		return 0;
2057 
2058 	/*
2059 	 * Configure TC/queue priority as follows:
2060 	 * Q0 - priority 0
2061 	 * Q1 - priority 1
2062 	 * Q2 - priority 2
2063 	 * ...
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.
2067 	 */
2068 	queue_priority_map = devm_kcalloc(dev, ecc->num_tc + 1, sizeof(s8),
2069 					  GFP_KERNEL);
2070 	if (!queue_priority_map)
2071 		return -ENOMEM;
2072 
2073 	for (i = 0; i < ecc->num_tc; i++) {
2074 		queue_priority_map[i][0] = i;
2075 		queue_priority_map[i][1] = i;
2076 	}
2077 	queue_priority_map[i][0] = -1;
2078 	queue_priority_map[i][1] = -1;
2079 
2080 	pdata->queue_priority_mapping = queue_priority_map;
2081 	/* Default queue has the lowest priority */
2082 	pdata->default_queue = i - 1;
2083 
2084 	return 0;
2085 }
2086 
2087 #if IS_ENABLED(CONFIG_OF)
2088 static int edma_xbar_event_map(struct device *dev, struct edma_soc_info *pdata,
2089 			       size_t sz)
2090 {
2091 	const char pname[] = "ti,edma-xbar-event-map";
2092 	struct resource res;
2093 	void __iomem *xbar;
2094 	s16 (*xbar_chans)[2];
2095 	size_t nelm = sz / sizeof(s16);
2096 	u32 shift, offset, mux;
2097 	int ret, i;
2098 
2099 	xbar_chans = devm_kcalloc(dev, nelm + 2, sizeof(s16), GFP_KERNEL);
2100 	if (!xbar_chans)
2101 		return -ENOMEM;
2102 
2103 	ret = of_address_to_resource(dev->of_node, 1, &res);
2104 	if (ret)
2105 		return -ENOMEM;
2106 
2107 	xbar = devm_ioremap(dev, res.start, resource_size(&res));
2108 	if (!xbar)
2109 		return -ENOMEM;
2110 
2111 	ret = of_property_read_u16_array(dev->of_node, pname, (u16 *)xbar_chans,
2112 					 nelm);
2113 	if (ret)
2114 		return -EIO;
2115 
2116 	/* Invalidate last entry for the other user of this mess */
2117 	nelm >>= 1;
2118 	xbar_chans[nelm][0] = -1;
2119 	xbar_chans[nelm][1] = -1;
2120 
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));
2128 	}
2129 
2130 	pdata->xbar_chans = (const s16 (*)[2]) xbar_chans;
2131 	return 0;
2132 }
2133 
2134 static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
2135 						     bool legacy_mode)
2136 {
2137 	struct edma_soc_info *info;
2138 	struct property *prop;
2139 	int sz, ret;
2140 
2141 	info = devm_kzalloc(dev, sizeof(struct edma_soc_info), GFP_KERNEL);
2142 	if (!info)
2143 		return ERR_PTR(-ENOMEM);
2144 
2145 	if (legacy_mode) {
2146 		prop = of_find_property(dev->of_node, "ti,edma-xbar-event-map",
2147 					&sz);
2148 		if (prop) {
2149 			ret = edma_xbar_event_map(dev, info, sz);
2150 			if (ret)
2151 				return ERR_PTR(ret);
2152 		}
2153 		return info;
2154 	}
2155 
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);
2158 	if (prop) {
2159 		const char pname[] = "ti,edma-memcpy-channels";
2160 		size_t nelm = sz / sizeof(s32);
2161 		s32 *memcpy_ch;
2162 
2163 		memcpy_ch = devm_kcalloc(dev, nelm + 1, sizeof(s32),
2164 					 GFP_KERNEL);
2165 		if (!memcpy_ch)
2166 			return ERR_PTR(-ENOMEM);
2167 
2168 		ret = of_property_read_u32_array(dev->of_node, pname,
2169 						 (u32 *)memcpy_ch, nelm);
2170 		if (ret)
2171 			return ERR_PTR(ret);
2172 
2173 		memcpy_ch[nelm] = -1;
2174 		info->memcpy_channels = memcpy_ch;
2175 	}
2176 
2177 	prop = of_find_property(dev->of_node, "ti,edma-reserved-slot-ranges",
2178 				&sz);
2179 	if (prop) {
2180 		const char pname[] = "ti,edma-reserved-slot-ranges";
2181 		u32 (*tmp)[2];
2182 		s16 (*rsv_slots)[2];
2183 		size_t nelm = sz / sizeof(*tmp);
2184 		struct edma_rsv_info *rsv_info;
2185 		int i;
2186 
2187 		if (!nelm)
2188 			return info;
2189 
2190 		tmp = kcalloc(nelm, sizeof(*tmp), GFP_KERNEL);
2191 		if (!tmp)
2192 			return ERR_PTR(-ENOMEM);
2193 
2194 		rsv_info = devm_kzalloc(dev, sizeof(*rsv_info), GFP_KERNEL);
2195 		if (!rsv_info) {
2196 			kfree(tmp);
2197 			return ERR_PTR(-ENOMEM);
2198 		}
2199 
2200 		rsv_slots = devm_kcalloc(dev, nelm + 1, sizeof(*rsv_slots),
2201 					 GFP_KERNEL);
2202 		if (!rsv_slots) {
2203 			kfree(tmp);
2204 			return ERR_PTR(-ENOMEM);
2205 		}
2206 
2207 		ret = of_property_read_u32_array(dev->of_node, pname,
2208 						 (u32 *)tmp, nelm * 2);
2209 		if (ret) {
2210 			kfree(tmp);
2211 			return ERR_PTR(ret);
2212 		}
2213 
2214 		for (i = 0; i < nelm; i++) {
2215 			rsv_slots[i][0] = tmp[i][0];
2216 			rsv_slots[i][1] = tmp[i][1];
2217 		}
2218 		rsv_slots[nelm][0] = -1;
2219 		rsv_slots[nelm][1] = -1;
2220 
2221 		info->rsv = rsv_info;
2222 		info->rsv->rsv_slots = (const s16 (*)[2])rsv_slots;
2223 
2224 		kfree(tmp);
2225 	}
2226 
2227 	return info;
2228 }
2229 
2230 static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
2231 				      struct of_dma *ofdma)
2232 {
2233 	struct edma_cc *ecc = ofdma->of_dma_data;
2234 	struct dma_chan *chan = NULL;
2235 	struct edma_chan *echan;
2236 	int i;
2237 
2238 	if (!ecc || dma_spec->args_count < 1)
2239 		return NULL;
2240 
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;
2245 			break;
2246 		}
2247 	}
2248 
2249 	if (!chan)
2250 		return NULL;
2251 
2252 	if (echan->ecc->legacy_mode && dma_spec->args_count == 1)
2253 		goto out;
2254 
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]];
2258 		goto out;
2259 	}
2260 
2261 	return NULL;
2262 out:
2263 	/* The channel is going to be used as HW synchronized */
2264 	echan->hw_triggered = true;
2265 	return dma_get_slave_channel(chan);
2266 }
2267 #else
2268 static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
2269 						     bool legacy_mode)
2270 {
2271 	return ERR_PTR(-EINVAL);
2272 }
2273 
2274 static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
2275 				      struct of_dma *ofdma)
2276 {
2277 	return NULL;
2278 }
2279 #endif
2280 
2281 static bool edma_filter_fn(struct dma_chan *chan, void *param);
2282 
2283 static int edma_probe(struct platform_device *pdev)
2284 {
2285 	struct edma_soc_info	*info = pdev->dev.platform_data;
2286 	s8			(*queue_priority_mapping)[2];
2287 	const s16		(*reserved)[2];
2288 	int			i, irq;
2289 	char			*irq_name;
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;
2295 	int ret;
2296 
2297 	if (node) {
2298 		const struct of_device_id *match;
2299 
2300 		match = of_match_node(edma_of_ids, node);
2301 		if (match && (*(u32 *)match->data) == EDMA_BINDING_TPCC)
2302 			legacy_mode = false;
2303 
2304 		info = edma_setup_info_from_dt(dev, legacy_mode);
2305 		if (IS_ERR(info)) {
2306 			dev_err(dev, "failed to get DT data\n");
2307 			return PTR_ERR(info);
2308 		}
2309 	}
2310 
2311 	if (!info)
2312 		return -ENODEV;
2313 
2314 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2315 	if (ret)
2316 		return ret;
2317 
2318 	ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
2319 	if (!ecc)
2320 		return -ENOMEM;
2321 
2322 	ecc->dev = dev;
2323 	ecc->id = pdev->id;
2324 	ecc->legacy_mode = legacy_mode;
2325 	/* When booting with DT the pdev->id is -1 */
2326 	if (ecc->id < 0)
2327 		ecc->id = 0;
2328 
2329 	mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "edma3_cc");
2330 	if (!mem) {
2331 		dev_dbg(dev, "mem resource not found, using index 0\n");
2332 		mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2333 		if (!mem) {
2334 			dev_err(dev, "no mem resource?\n");
2335 			return -ENODEV;
2336 		}
2337 	}
2338 	ecc->base = devm_ioremap_resource(dev, mem);
2339 	if (IS_ERR(ecc->base))
2340 		return PTR_ERR(ecc->base);
2341 
2342 	platform_set_drvdata(pdev, ecc);
2343 
2344 	pm_runtime_enable(dev);
2345 	ret = pm_runtime_get_sync(dev);
2346 	if (ret < 0) {
2347 		dev_err(dev, "pm_runtime_get_sync() failed\n");
2348 		pm_runtime_disable(dev);
2349 		return ret;
2350 	}
2351 
2352 	/* Get eDMA3 configuration from IP */
2353 	ret = edma_setup_from_hw(dev, info, ecc);
2354 	if (ret)
2355 		goto err_disable_pm;
2356 
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);
2360 
2361 	ecc->slot_inuse = devm_kcalloc(dev, BITS_TO_LONGS(ecc->num_slots),
2362 				       sizeof(unsigned long), GFP_KERNEL);
2363 
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) {
2368 		ret = -ENOMEM;
2369 		goto err_disable_pm;
2370 	}
2371 
2372 	/* Mark all channels available initially */
2373 	bitmap_fill(ecc->channels_mask, ecc->num_channels);
2374 
2375 	ecc->default_queue = info->default_queue;
2376 
2377 	if (info->rsv) {
2378 		/* Set the reserved slots in inuse list */
2379 		reserved = info->rsv->rsv_slots;
2380 		if (reserved) {
2381 			for (i = 0; reserved[i][0] != -1; i++)
2382 				bitmap_set(ecc->slot_inuse, reserved[i][0],
2383 					   reserved[i][1]);
2384 		}
2385 
2386 		/* Clear channels not usable for Linux */
2387 		reserved = info->rsv->rsv_chans;
2388 		if (reserved) {
2389 			for (i = 0; reserved[i][0] != -1; i++)
2390 				bitmap_clear(ecc->channels_mask, reserved[i][0],
2391 					     reserved[i][1]);
2392 		}
2393 	}
2394 
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);
2399 	}
2400 
2401 	irq = platform_get_irq_byname(pdev, "edma3_ccint");
2402 	if (irq < 0 && node)
2403 		irq = irq_of_parse_and_map(node, 0);
2404 
2405 	if (irq >= 0) {
2406 		irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccint",
2407 					  dev_name(dev));
2408 		ret = devm_request_irq(dev, irq, dma_irq_handler, 0, irq_name,
2409 				       ecc);
2410 		if (ret) {
2411 			dev_err(dev, "CCINT (%d) failed --> %d\n", irq, ret);
2412 			goto err_disable_pm;
2413 		}
2414 		ecc->ccint = irq;
2415 	}
2416 
2417 	irq = platform_get_irq_byname(pdev, "edma3_ccerrint");
2418 	if (irq < 0 && node)
2419 		irq = irq_of_parse_and_map(node, 2);
2420 
2421 	if (irq >= 0) {
2422 		irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccerrint",
2423 					  dev_name(dev));
2424 		ret = devm_request_irq(dev, irq, dma_ccerr_handler, 0, irq_name,
2425 				       ecc);
2426 		if (ret) {
2427 			dev_err(dev, "CCERRINT (%d) failed --> %d\n", irq, ret);
2428 			goto err_disable_pm;
2429 		}
2430 		ecc->ccerrint = irq;
2431 	}
2432 
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;
2438 	}
2439 
2440 	queue_priority_mapping = info->queue_priority_mapping;
2441 
2442 	if (!ecc->legacy_mode) {
2443 		int lowest_priority = 0;
2444 		unsigned int array_max;
2445 		struct of_phandle_args tc_args;
2446 
2447 		ecc->tc_list = devm_kcalloc(dev, ecc->num_tc,
2448 					    sizeof(*ecc->tc_list), GFP_KERNEL);
2449 		if (!ecc->tc_list) {
2450 			ret = -ENOMEM;
2451 			goto err_reg1;
2452 		}
2453 
2454 		for (i = 0;; i++) {
2455 			ret = of_parse_phandle_with_fixed_args(node, "ti,tptcs",
2456 							       1, i, &tc_args);
2457 			if (ret || i == ecc->num_tc)
2458 				break;
2459 
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;
2466 			}
2467 		}
2468 
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,
2472 						"dma-channel-mask",
2473 						(u32 *)ecc->channels_mask,
2474 						1, array_max);
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)
2478 			dev_warn(dev,
2479 				 "dma-channel-mask is out of range or empty\n");
2480 	}
2481 
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]);
2486 
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);
2490 
2491 	ecc->info = info;
2492 
2493 	/* Init the dma device and channels */
2494 	edma_dma_init(ecc, legacy_mode);
2495 
2496 	for (i = 0; i < ecc->num_channels; i++) {
2497 		/* Do not touch reserved channels */
2498 		if (!test_bit(i, ecc->channels_mask))
2499 			continue;
2500 
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);
2506 	}
2507 
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;
2511 
2512 	ret = dma_async_device_register(&ecc->dma_slave);
2513 	if (ret) {
2514 		dev_err(dev, "slave ddev registration failed (%d)\n", ret);
2515 		goto err_reg1;
2516 	}
2517 
2518 	if (ecc->dma_memcpy) {
2519 		ret = dma_async_device_register(ecc->dma_memcpy);
2520 		if (ret) {
2521 			dev_err(dev, "memcpy ddev registration failed (%d)\n",
2522 				ret);
2523 			dma_async_device_unregister(&ecc->dma_slave);
2524 			goto err_reg1;
2525 		}
2526 	}
2527 
2528 	if (node)
2529 		of_dma_controller_register(node, of_edma_xlate, ecc);
2530 
2531 	dev_info(dev, "TI EDMA DMA engine driver\n");
2532 
2533 	return 0;
2534 
2535 err_reg1:
2536 	edma_free_slot(ecc, ecc->dummy_slot);
2537 err_disable_pm:
2538 	pm_runtime_put_sync(dev);
2539 	pm_runtime_disable(dev);
2540 	return ret;
2541 }
2542 
2543 static void edma_cleanupp_vchan(struct dma_device *dmadev)
2544 {
2545 	struct edma_chan *echan, *_echan;
2546 
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);
2551 	}
2552 }
2553 
2554 static int edma_remove(struct platform_device *pdev)
2555 {
2556 	struct device *dev = &pdev->dev;
2557 	struct edma_cc *ecc = dev_get_drvdata(dev);
2558 
2559 	devm_free_irq(dev, ecc->ccint, ecc);
2560 	devm_free_irq(dev, ecc->ccerrint, ecc);
2561 
2562 	edma_cleanupp_vchan(&ecc->dma_slave);
2563 
2564 	if (dev->of_node)
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);
2572 
2573 	return 0;
2574 }
2575 
2576 #ifdef CONFIG_PM_SLEEP
2577 static int edma_pm_suspend(struct device *dev)
2578 {
2579 	struct edma_cc *ecc = dev_get_drvdata(dev);
2580 	struct edma_chan *echan = ecc->slave_chans;
2581 	int i;
2582 
2583 	for (i = 0; i < ecc->num_channels; i++) {
2584 		if (echan[i].alloced)
2585 			edma_setup_interrupt(&echan[i], false);
2586 	}
2587 
2588 	return 0;
2589 }
2590 
2591 static int edma_pm_resume(struct device *dev)
2592 {
2593 	struct edma_cc *ecc = dev_get_drvdata(dev);
2594 	struct edma_chan *echan = ecc->slave_chans;
2595 	int i;
2596 	s8 (*queue_priority_mapping)[2];
2597 
2598 	/* re initialize dummy slot to dummy param set */
2599 	edma_write_slot(ecc, ecc->dummy_slot, &dummy_paramset);
2600 
2601 	queue_priority_mapping = ecc->info->queue_priority_mapping;
2602 
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]);
2607 
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));
2614 
2615 			edma_setup_interrupt(&echan[i], true);
2616 
2617 			/* Set up channel -> slot mapping for the entry slot */
2618 			edma_set_chmap(&echan[i], echan[i].slot[0]);
2619 		}
2620 	}
2621 
2622 	return 0;
2623 }
2624 #endif
2625 
2626 static const struct dev_pm_ops edma_pm_ops = {
2627 	SET_LATE_SYSTEM_SLEEP_PM_OPS(edma_pm_suspend, edma_pm_resume)
2628 };
2629 
2630 static struct platform_driver edma_driver = {
2631 	.probe		= edma_probe,
2632 	.remove		= edma_remove,
2633 	.driver = {
2634 		.name	= "edma",
2635 		.pm	= &edma_pm_ops,
2636 		.of_match_table = edma_of_ids,
2637 	},
2638 };
2639 
2640 static int edma_tptc_probe(struct platform_device *pdev)
2641 {
2642 	pm_runtime_enable(&pdev->dev);
2643 	return pm_runtime_get_sync(&pdev->dev);
2644 }
2645 
2646 static struct platform_driver edma_tptc_driver = {
2647 	.probe		= edma_tptc_probe,
2648 	.driver = {
2649 		.name	= "edma3-tptc",
2650 		.of_match_table = edma_tptc_of_ids,
2651 	},
2652 };
2653 
2654 static bool edma_filter_fn(struct dma_chan *chan, void *param)
2655 {
2656 	bool match = false;
2657 
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;
2664 			match = true;
2665 		}
2666 	}
2667 	return match;
2668 }
2669 
2670 static int edma_init(void)
2671 {
2672 	int ret;
2673 
2674 	ret = platform_driver_register(&edma_tptc_driver);
2675 	if (ret)
2676 		return ret;
2677 
2678 	return platform_driver_register(&edma_driver);
2679 }
2680 subsys_initcall(edma_init);
2681 
2682 static void __exit edma_exit(void)
2683 {
2684 	platform_driver_unregister(&edma_driver);
2685 	platform_driver_unregister(&edma_tptc_driver);
2686 }
2687 module_exit(edma_exit);
2688 
2689 MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
2690 MODULE_DESCRIPTION("TI EDMA DMA engine driver");
2691 MODULE_LICENSE("GPL v2");
2692