xref: /openbmc/linux/drivers/dma/dw/core.c (revision ee21014b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Core driver for the Synopsys DesignWare DMA Controller
4  *
5  * Copyright (C) 2007-2008 Atmel Corporation
6  * Copyright (C) 2010-2011 ST Microelectronics
7  * Copyright (C) 2013 Intel Corporation
8  */
9 
10 #include <linux/bitops.h>
11 #include <linux/delay.h>
12 #include <linux/dmaengine.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/dmapool.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/pm_runtime.h>
23 
24 #include "../dmaengine.h"
25 #include "internal.h"
26 
27 /*
28  * This supports the Synopsys "DesignWare AHB Central DMA Controller",
29  * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
30  * of which use ARM any more).  See the "Databook" from Synopsys for
31  * information beyond what licensees probably provide.
32  *
33  * The driver has been tested with the Atmel AT32AP7000, which does not
34  * support descriptor writeback.
35  */
36 
37 /* The set of bus widths supported by the DMA controller */
38 #define DW_DMA_BUSWIDTHS			  \
39 	BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED)	| \
40 	BIT(DMA_SLAVE_BUSWIDTH_1_BYTE)		| \
41 	BIT(DMA_SLAVE_BUSWIDTH_2_BYTES)		| \
42 	BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
43 
44 /*----------------------------------------------------------------------*/
45 
46 static struct device *chan2dev(struct dma_chan *chan)
47 {
48 	return &chan->dev->device;
49 }
50 
51 static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
52 {
53 	return to_dw_desc(dwc->active_list.next);
54 }
55 
56 static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
57 {
58 	struct dw_desc		*desc = txd_to_dw_desc(tx);
59 	struct dw_dma_chan	*dwc = to_dw_dma_chan(tx->chan);
60 	dma_cookie_t		cookie;
61 	unsigned long		flags;
62 
63 	spin_lock_irqsave(&dwc->lock, flags);
64 	cookie = dma_cookie_assign(tx);
65 
66 	/*
67 	 * REVISIT: We should attempt to chain as many descriptors as
68 	 * possible, perhaps even appending to those already submitted
69 	 * for DMA. But this is hard to do in a race-free manner.
70 	 */
71 
72 	list_add_tail(&desc->desc_node, &dwc->queue);
73 	spin_unlock_irqrestore(&dwc->lock, flags);
74 	dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n",
75 		 __func__, desc->txd.cookie);
76 
77 	return cookie;
78 }
79 
80 static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
81 {
82 	struct dw_dma *dw = to_dw_dma(dwc->chan.device);
83 	struct dw_desc *desc;
84 	dma_addr_t phys;
85 
86 	desc = dma_pool_zalloc(dw->desc_pool, GFP_ATOMIC, &phys);
87 	if (!desc)
88 		return NULL;
89 
90 	dwc->descs_allocated++;
91 	INIT_LIST_HEAD(&desc->tx_list);
92 	dma_async_tx_descriptor_init(&desc->txd, &dwc->chan);
93 	desc->txd.tx_submit = dwc_tx_submit;
94 	desc->txd.flags = DMA_CTRL_ACK;
95 	desc->txd.phys = phys;
96 	return desc;
97 }
98 
99 static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
100 {
101 	struct dw_dma *dw = to_dw_dma(dwc->chan.device);
102 	struct dw_desc *child, *_next;
103 
104 	if (unlikely(!desc))
105 		return;
106 
107 	list_for_each_entry_safe(child, _next, &desc->tx_list, desc_node) {
108 		list_del(&child->desc_node);
109 		dma_pool_free(dw->desc_pool, child, child->txd.phys);
110 		dwc->descs_allocated--;
111 	}
112 
113 	dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
114 	dwc->descs_allocated--;
115 }
116 
117 static void dwc_initialize(struct dw_dma_chan *dwc)
118 {
119 	struct dw_dma *dw = to_dw_dma(dwc->chan.device);
120 
121 	dw->initialize_chan(dwc);
122 
123 	/* Enable interrupts */
124 	channel_set_bit(dw, MASK.XFER, dwc->mask);
125 	channel_set_bit(dw, MASK.ERROR, dwc->mask);
126 }
127 
128 /*----------------------------------------------------------------------*/
129 
130 static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
131 {
132 	dev_err(chan2dev(&dwc->chan),
133 		"  SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
134 		channel_readl(dwc, SAR),
135 		channel_readl(dwc, DAR),
136 		channel_readl(dwc, LLP),
137 		channel_readl(dwc, CTL_HI),
138 		channel_readl(dwc, CTL_LO));
139 }
140 
141 static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
142 {
143 	channel_clear_bit(dw, CH_EN, dwc->mask);
144 	while (dma_readl(dw, CH_EN) & dwc->mask)
145 		cpu_relax();
146 }
147 
148 /*----------------------------------------------------------------------*/
149 
150 /* Perform single block transfer */
151 static inline void dwc_do_single_block(struct dw_dma_chan *dwc,
152 				       struct dw_desc *desc)
153 {
154 	struct dw_dma	*dw = to_dw_dma(dwc->chan.device);
155 	u32		ctllo;
156 
157 	/*
158 	 * Software emulation of LLP mode relies on interrupts to continue
159 	 * multi block transfer.
160 	 */
161 	ctllo = lli_read(desc, ctllo) | DWC_CTLL_INT_EN;
162 
163 	channel_writel(dwc, SAR, lli_read(desc, sar));
164 	channel_writel(dwc, DAR, lli_read(desc, dar));
165 	channel_writel(dwc, CTL_LO, ctllo);
166 	channel_writel(dwc, CTL_HI, lli_read(desc, ctlhi));
167 	channel_set_bit(dw, CH_EN, dwc->mask);
168 
169 	/* Move pointer to next descriptor */
170 	dwc->tx_node_active = dwc->tx_node_active->next;
171 }
172 
173 /* Called with dwc->lock held and bh disabled */
174 static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
175 {
176 	struct dw_dma	*dw = to_dw_dma(dwc->chan.device);
177 	u8		lms = DWC_LLP_LMS(dwc->dws.m_master);
178 	unsigned long	was_soft_llp;
179 
180 	/* ASSERT:  channel is idle */
181 	if (dma_readl(dw, CH_EN) & dwc->mask) {
182 		dev_err(chan2dev(&dwc->chan),
183 			"%s: BUG: Attempted to start non-idle channel\n",
184 			__func__);
185 		dwc_dump_chan_regs(dwc);
186 
187 		/* The tasklet will hopefully advance the queue... */
188 		return;
189 	}
190 
191 	if (dwc->nollp) {
192 		was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP,
193 						&dwc->flags);
194 		if (was_soft_llp) {
195 			dev_err(chan2dev(&dwc->chan),
196 				"BUG: Attempted to start new LLP transfer inside ongoing one\n");
197 			return;
198 		}
199 
200 		dwc_initialize(dwc);
201 
202 		first->residue = first->total_len;
203 		dwc->tx_node_active = &first->tx_list;
204 
205 		/* Submit first block */
206 		dwc_do_single_block(dwc, first);
207 
208 		return;
209 	}
210 
211 	dwc_initialize(dwc);
212 
213 	channel_writel(dwc, LLP, first->txd.phys | lms);
214 	channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
215 	channel_writel(dwc, CTL_HI, 0);
216 	channel_set_bit(dw, CH_EN, dwc->mask);
217 }
218 
219 static void dwc_dostart_first_queued(struct dw_dma_chan *dwc)
220 {
221 	struct dw_desc *desc;
222 
223 	if (list_empty(&dwc->queue))
224 		return;
225 
226 	list_move(dwc->queue.next, &dwc->active_list);
227 	desc = dwc_first_active(dwc);
228 	dev_vdbg(chan2dev(&dwc->chan), "%s: started %u\n", __func__, desc->txd.cookie);
229 	dwc_dostart(dwc, desc);
230 }
231 
232 /*----------------------------------------------------------------------*/
233 
234 static void
235 dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
236 		bool callback_required)
237 {
238 	struct dma_async_tx_descriptor	*txd = &desc->txd;
239 	struct dw_desc			*child;
240 	unsigned long			flags;
241 	struct dmaengine_desc_callback	cb;
242 
243 	dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
244 
245 	spin_lock_irqsave(&dwc->lock, flags);
246 	dma_cookie_complete(txd);
247 	if (callback_required)
248 		dmaengine_desc_get_callback(txd, &cb);
249 	else
250 		memset(&cb, 0, sizeof(cb));
251 
252 	/* async_tx_ack */
253 	list_for_each_entry(child, &desc->tx_list, desc_node)
254 		async_tx_ack(&child->txd);
255 	async_tx_ack(&desc->txd);
256 	dwc_desc_put(dwc, desc);
257 	spin_unlock_irqrestore(&dwc->lock, flags);
258 
259 	dmaengine_desc_callback_invoke(&cb, NULL);
260 }
261 
262 static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
263 {
264 	struct dw_desc *desc, *_desc;
265 	LIST_HEAD(list);
266 	unsigned long flags;
267 
268 	spin_lock_irqsave(&dwc->lock, flags);
269 	if (dma_readl(dw, CH_EN) & dwc->mask) {
270 		dev_err(chan2dev(&dwc->chan),
271 			"BUG: XFER bit set, but channel not idle!\n");
272 
273 		/* Try to continue after resetting the channel... */
274 		dwc_chan_disable(dw, dwc);
275 	}
276 
277 	/*
278 	 * Submit queued descriptors ASAP, i.e. before we go through
279 	 * the completed ones.
280 	 */
281 	list_splice_init(&dwc->active_list, &list);
282 	dwc_dostart_first_queued(dwc);
283 
284 	spin_unlock_irqrestore(&dwc->lock, flags);
285 
286 	list_for_each_entry_safe(desc, _desc, &list, desc_node)
287 		dwc_descriptor_complete(dwc, desc, true);
288 }
289 
290 /* Returns how many bytes were already received from source */
291 static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
292 {
293 	struct dw_dma *dw = to_dw_dma(dwc->chan.device);
294 	u32 ctlhi = channel_readl(dwc, CTL_HI);
295 	u32 ctllo = channel_readl(dwc, CTL_LO);
296 
297 	return dw->block2bytes(dwc, ctlhi, ctllo >> 4 & 7);
298 }
299 
300 static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
301 {
302 	dma_addr_t llp;
303 	struct dw_desc *desc, *_desc;
304 	struct dw_desc *child;
305 	u32 status_xfer;
306 	unsigned long flags;
307 
308 	spin_lock_irqsave(&dwc->lock, flags);
309 	llp = channel_readl(dwc, LLP);
310 	status_xfer = dma_readl(dw, RAW.XFER);
311 
312 	if (status_xfer & dwc->mask) {
313 		/* Everything we've submitted is done */
314 		dma_writel(dw, CLEAR.XFER, dwc->mask);
315 
316 		if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
317 			struct list_head *head, *active = dwc->tx_node_active;
318 
319 			/*
320 			 * We are inside first active descriptor.
321 			 * Otherwise something is really wrong.
322 			 */
323 			desc = dwc_first_active(dwc);
324 
325 			head = &desc->tx_list;
326 			if (active != head) {
327 				/* Update residue to reflect last sent descriptor */
328 				if (active == head->next)
329 					desc->residue -= desc->len;
330 				else
331 					desc->residue -= to_dw_desc(active->prev)->len;
332 
333 				child = to_dw_desc(active);
334 
335 				/* Submit next block */
336 				dwc_do_single_block(dwc, child);
337 
338 				spin_unlock_irqrestore(&dwc->lock, flags);
339 				return;
340 			}
341 
342 			/* We are done here */
343 			clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
344 		}
345 
346 		spin_unlock_irqrestore(&dwc->lock, flags);
347 
348 		dwc_complete_all(dw, dwc);
349 		return;
350 	}
351 
352 	if (list_empty(&dwc->active_list)) {
353 		spin_unlock_irqrestore(&dwc->lock, flags);
354 		return;
355 	}
356 
357 	if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
358 		dev_vdbg(chan2dev(&dwc->chan), "%s: soft LLP mode\n", __func__);
359 		spin_unlock_irqrestore(&dwc->lock, flags);
360 		return;
361 	}
362 
363 	dev_vdbg(chan2dev(&dwc->chan), "%s: llp=%pad\n", __func__, &llp);
364 
365 	list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
366 		/* Initial residue value */
367 		desc->residue = desc->total_len;
368 
369 		/* Check first descriptors addr */
370 		if (desc->txd.phys == DWC_LLP_LOC(llp)) {
371 			spin_unlock_irqrestore(&dwc->lock, flags);
372 			return;
373 		}
374 
375 		/* Check first descriptors llp */
376 		if (lli_read(desc, llp) == llp) {
377 			/* This one is currently in progress */
378 			desc->residue -= dwc_get_sent(dwc);
379 			spin_unlock_irqrestore(&dwc->lock, flags);
380 			return;
381 		}
382 
383 		desc->residue -= desc->len;
384 		list_for_each_entry(child, &desc->tx_list, desc_node) {
385 			if (lli_read(child, llp) == llp) {
386 				/* Currently in progress */
387 				desc->residue -= dwc_get_sent(dwc);
388 				spin_unlock_irqrestore(&dwc->lock, flags);
389 				return;
390 			}
391 			desc->residue -= child->len;
392 		}
393 
394 		/*
395 		 * No descriptors so far seem to be in progress, i.e.
396 		 * this one must be done.
397 		 */
398 		spin_unlock_irqrestore(&dwc->lock, flags);
399 		dwc_descriptor_complete(dwc, desc, true);
400 		spin_lock_irqsave(&dwc->lock, flags);
401 	}
402 
403 	dev_err(chan2dev(&dwc->chan),
404 		"BUG: All descriptors done, but channel not idle!\n");
405 
406 	/* Try to continue after resetting the channel... */
407 	dwc_chan_disable(dw, dwc);
408 
409 	dwc_dostart_first_queued(dwc);
410 	spin_unlock_irqrestore(&dwc->lock, flags);
411 }
412 
413 static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_desc *desc)
414 {
415 	dev_crit(chan2dev(&dwc->chan), "  desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
416 		 lli_read(desc, sar),
417 		 lli_read(desc, dar),
418 		 lli_read(desc, llp),
419 		 lli_read(desc, ctlhi),
420 		 lli_read(desc, ctllo));
421 }
422 
423 static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
424 {
425 	struct dw_desc *bad_desc;
426 	struct dw_desc *child;
427 	unsigned long flags;
428 
429 	dwc_scan_descriptors(dw, dwc);
430 
431 	spin_lock_irqsave(&dwc->lock, flags);
432 
433 	/*
434 	 * The descriptor currently at the head of the active list is
435 	 * borked. Since we don't have any way to report errors, we'll
436 	 * just have to scream loudly and try to carry on.
437 	 */
438 	bad_desc = dwc_first_active(dwc);
439 	list_del_init(&bad_desc->desc_node);
440 	list_move(dwc->queue.next, dwc->active_list.prev);
441 
442 	/* Clear the error flag and try to restart the controller */
443 	dma_writel(dw, CLEAR.ERROR, dwc->mask);
444 	if (!list_empty(&dwc->active_list))
445 		dwc_dostart(dwc, dwc_first_active(dwc));
446 
447 	/*
448 	 * WARN may seem harsh, but since this only happens
449 	 * when someone submits a bad physical address in a
450 	 * descriptor, we should consider ourselves lucky that the
451 	 * controller flagged an error instead of scribbling over
452 	 * random memory locations.
453 	 */
454 	dev_WARN(chan2dev(&dwc->chan), "Bad descriptor submitted for DMA!\n"
455 				       "  cookie: %d\n", bad_desc->txd.cookie);
456 	dwc_dump_lli(dwc, bad_desc);
457 	list_for_each_entry(child, &bad_desc->tx_list, desc_node)
458 		dwc_dump_lli(dwc, child);
459 
460 	spin_unlock_irqrestore(&dwc->lock, flags);
461 
462 	/* Pretend the descriptor completed successfully */
463 	dwc_descriptor_complete(dwc, bad_desc, true);
464 }
465 
466 static void dw_dma_tasklet(unsigned long data)
467 {
468 	struct dw_dma *dw = (struct dw_dma *)data;
469 	struct dw_dma_chan *dwc;
470 	u32 status_xfer;
471 	u32 status_err;
472 	unsigned int i;
473 
474 	status_xfer = dma_readl(dw, RAW.XFER);
475 	status_err = dma_readl(dw, RAW.ERROR);
476 
477 	dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
478 
479 	for (i = 0; i < dw->dma.chancnt; i++) {
480 		dwc = &dw->chan[i];
481 		if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
482 			dev_vdbg(dw->dma.dev, "Cyclic xfer is not implemented\n");
483 		else if (status_err & (1 << i))
484 			dwc_handle_error(dw, dwc);
485 		else if (status_xfer & (1 << i))
486 			dwc_scan_descriptors(dw, dwc);
487 	}
488 
489 	/* Re-enable interrupts */
490 	channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
491 	channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
492 }
493 
494 static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
495 {
496 	struct dw_dma *dw = dev_id;
497 	u32 status;
498 
499 	/* Check if we have any interrupt from the DMAC which is not in use */
500 	if (!dw->in_use)
501 		return IRQ_NONE;
502 
503 	status = dma_readl(dw, STATUS_INT);
504 	dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__, status);
505 
506 	/* Check if we have any interrupt from the DMAC */
507 	if (!status)
508 		return IRQ_NONE;
509 
510 	/*
511 	 * Just disable the interrupts. We'll turn them back on in the
512 	 * softirq handler.
513 	 */
514 	channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
515 	channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
516 	channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
517 
518 	status = dma_readl(dw, STATUS_INT);
519 	if (status) {
520 		dev_err(dw->dma.dev,
521 			"BUG: Unexpected interrupts pending: 0x%x\n",
522 			status);
523 
524 		/* Try to recover */
525 		channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
526 		channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
527 		channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
528 		channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
529 		channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
530 	}
531 
532 	tasklet_schedule(&dw->tasklet);
533 
534 	return IRQ_HANDLED;
535 }
536 
537 /*----------------------------------------------------------------------*/
538 
539 static struct dma_async_tx_descriptor *
540 dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
541 		size_t len, unsigned long flags)
542 {
543 	struct dw_dma_chan	*dwc = to_dw_dma_chan(chan);
544 	struct dw_dma		*dw = to_dw_dma(chan->device);
545 	struct dw_desc		*desc;
546 	struct dw_desc		*first;
547 	struct dw_desc		*prev;
548 	size_t			xfer_count;
549 	size_t			offset;
550 	u8			m_master = dwc->dws.m_master;
551 	unsigned int		src_width;
552 	unsigned int		dst_width;
553 	unsigned int		data_width = dw->pdata->data_width[m_master];
554 	u32			ctllo, ctlhi;
555 	u8			lms = DWC_LLP_LMS(m_master);
556 
557 	dev_vdbg(chan2dev(chan),
558 			"%s: d%pad s%pad l0x%zx f0x%lx\n", __func__,
559 			&dest, &src, len, flags);
560 
561 	if (unlikely(!len)) {
562 		dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
563 		return NULL;
564 	}
565 
566 	dwc->direction = DMA_MEM_TO_MEM;
567 
568 	src_width = dst_width = __ffs(data_width | src | dest | len);
569 
570 	ctllo = dw->prepare_ctllo(dwc)
571 			| DWC_CTLL_DST_WIDTH(dst_width)
572 			| DWC_CTLL_SRC_WIDTH(src_width)
573 			| DWC_CTLL_DST_INC
574 			| DWC_CTLL_SRC_INC
575 			| DWC_CTLL_FC_M2M;
576 	prev = first = NULL;
577 
578 	for (offset = 0; offset < len; offset += xfer_count) {
579 		desc = dwc_desc_get(dwc);
580 		if (!desc)
581 			goto err_desc_get;
582 
583 		ctlhi = dw->bytes2block(dwc, len - offset, src_width, &xfer_count);
584 
585 		lli_write(desc, sar, src + offset);
586 		lli_write(desc, dar, dest + offset);
587 		lli_write(desc, ctllo, ctllo);
588 		lli_write(desc, ctlhi, ctlhi);
589 		desc->len = xfer_count;
590 
591 		if (!first) {
592 			first = desc;
593 		} else {
594 			lli_write(prev, llp, desc->txd.phys | lms);
595 			list_add_tail(&desc->desc_node, &first->tx_list);
596 		}
597 		prev = desc;
598 	}
599 
600 	if (flags & DMA_PREP_INTERRUPT)
601 		/* Trigger interrupt after last block */
602 		lli_set(prev, ctllo, DWC_CTLL_INT_EN);
603 
604 	prev->lli.llp = 0;
605 	lli_clear(prev, ctllo, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
606 	first->txd.flags = flags;
607 	first->total_len = len;
608 
609 	return &first->txd;
610 
611 err_desc_get:
612 	dwc_desc_put(dwc, first);
613 	return NULL;
614 }
615 
616 static struct dma_async_tx_descriptor *
617 dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
618 		unsigned int sg_len, enum dma_transfer_direction direction,
619 		unsigned long flags, void *context)
620 {
621 	struct dw_dma_chan	*dwc = to_dw_dma_chan(chan);
622 	struct dw_dma		*dw = to_dw_dma(chan->device);
623 	struct dma_slave_config	*sconfig = &dwc->dma_sconfig;
624 	struct dw_desc		*prev;
625 	struct dw_desc		*first;
626 	u32			ctllo, ctlhi;
627 	u8			m_master = dwc->dws.m_master;
628 	u8			lms = DWC_LLP_LMS(m_master);
629 	dma_addr_t		reg;
630 	unsigned int		reg_width;
631 	unsigned int		mem_width;
632 	unsigned int		data_width = dw->pdata->data_width[m_master];
633 	unsigned int		i;
634 	struct scatterlist	*sg;
635 	size_t			total_len = 0;
636 
637 	dev_vdbg(chan2dev(chan), "%s\n", __func__);
638 
639 	if (unlikely(!is_slave_direction(direction) || !sg_len))
640 		return NULL;
641 
642 	dwc->direction = direction;
643 
644 	prev = first = NULL;
645 
646 	switch (direction) {
647 	case DMA_MEM_TO_DEV:
648 		reg_width = __ffs(sconfig->dst_addr_width);
649 		reg = sconfig->dst_addr;
650 		ctllo = dw->prepare_ctllo(dwc)
651 				| DWC_CTLL_DST_WIDTH(reg_width)
652 				| DWC_CTLL_DST_FIX
653 				| DWC_CTLL_SRC_INC;
654 
655 		ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
656 			DWC_CTLL_FC(DW_DMA_FC_D_M2P);
657 
658 		for_each_sg(sgl, sg, sg_len, i) {
659 			struct dw_desc	*desc;
660 			u32		len, mem;
661 			size_t		dlen;
662 
663 			mem = sg_dma_address(sg);
664 			len = sg_dma_len(sg);
665 
666 			mem_width = __ffs(data_width | mem | len);
667 
668 slave_sg_todev_fill_desc:
669 			desc = dwc_desc_get(dwc);
670 			if (!desc)
671 				goto err_desc_get;
672 
673 			ctlhi = dw->bytes2block(dwc, len, mem_width, &dlen);
674 
675 			lli_write(desc, sar, mem);
676 			lli_write(desc, dar, reg);
677 			lli_write(desc, ctlhi, ctlhi);
678 			lli_write(desc, ctllo, ctllo | DWC_CTLL_SRC_WIDTH(mem_width));
679 			desc->len = dlen;
680 
681 			if (!first) {
682 				first = desc;
683 			} else {
684 				lli_write(prev, llp, desc->txd.phys | lms);
685 				list_add_tail(&desc->desc_node, &first->tx_list);
686 			}
687 			prev = desc;
688 
689 			mem += dlen;
690 			len -= dlen;
691 			total_len += dlen;
692 
693 			if (len)
694 				goto slave_sg_todev_fill_desc;
695 		}
696 		break;
697 	case DMA_DEV_TO_MEM:
698 		reg_width = __ffs(sconfig->src_addr_width);
699 		reg = sconfig->src_addr;
700 		ctllo = dw->prepare_ctllo(dwc)
701 				| DWC_CTLL_SRC_WIDTH(reg_width)
702 				| DWC_CTLL_DST_INC
703 				| DWC_CTLL_SRC_FIX;
704 
705 		ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
706 			DWC_CTLL_FC(DW_DMA_FC_D_P2M);
707 
708 		for_each_sg(sgl, sg, sg_len, i) {
709 			struct dw_desc	*desc;
710 			u32		len, mem;
711 			size_t		dlen;
712 
713 			mem = sg_dma_address(sg);
714 			len = sg_dma_len(sg);
715 
716 slave_sg_fromdev_fill_desc:
717 			desc = dwc_desc_get(dwc);
718 			if (!desc)
719 				goto err_desc_get;
720 
721 			ctlhi = dw->bytes2block(dwc, len, reg_width, &dlen);
722 
723 			lli_write(desc, sar, reg);
724 			lli_write(desc, dar, mem);
725 			lli_write(desc, ctlhi, ctlhi);
726 			mem_width = __ffs(data_width | mem | dlen);
727 			lli_write(desc, ctllo, ctllo | DWC_CTLL_DST_WIDTH(mem_width));
728 			desc->len = dlen;
729 
730 			if (!first) {
731 				first = desc;
732 			} else {
733 				lli_write(prev, llp, desc->txd.phys | lms);
734 				list_add_tail(&desc->desc_node, &first->tx_list);
735 			}
736 			prev = desc;
737 
738 			mem += dlen;
739 			len -= dlen;
740 			total_len += dlen;
741 
742 			if (len)
743 				goto slave_sg_fromdev_fill_desc;
744 		}
745 		break;
746 	default:
747 		return NULL;
748 	}
749 
750 	if (flags & DMA_PREP_INTERRUPT)
751 		/* Trigger interrupt after last block */
752 		lli_set(prev, ctllo, DWC_CTLL_INT_EN);
753 
754 	prev->lli.llp = 0;
755 	lli_clear(prev, ctllo, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
756 	first->total_len = total_len;
757 
758 	return &first->txd;
759 
760 err_desc_get:
761 	dev_err(chan2dev(chan),
762 		"not enough descriptors available. Direction %d\n", direction);
763 	dwc_desc_put(dwc, first);
764 	return NULL;
765 }
766 
767 bool dw_dma_filter(struct dma_chan *chan, void *param)
768 {
769 	struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
770 	struct dw_dma_slave *dws = param;
771 
772 	if (dws->dma_dev != chan->device->dev)
773 		return false;
774 
775 	/* We have to copy data since dws can be temporary storage */
776 	memcpy(&dwc->dws, dws, sizeof(struct dw_dma_slave));
777 
778 	return true;
779 }
780 EXPORT_SYMBOL_GPL(dw_dma_filter);
781 
782 static int dwc_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
783 {
784 	struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
785 	struct dw_dma *dw = to_dw_dma(chan->device);
786 
787 	memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
788 
789 	dwc->dma_sconfig.src_maxburst =
790 		clamp(dwc->dma_sconfig.src_maxburst, 0U, dwc->max_burst);
791 	dwc->dma_sconfig.dst_maxburst =
792 		clamp(dwc->dma_sconfig.dst_maxburst, 0U, dwc->max_burst);
793 
794 	dw->encode_maxburst(dwc, &dwc->dma_sconfig.src_maxburst);
795 	dw->encode_maxburst(dwc, &dwc->dma_sconfig.dst_maxburst);
796 
797 	return 0;
798 }
799 
800 static void dwc_chan_pause(struct dw_dma_chan *dwc, bool drain)
801 {
802 	struct dw_dma *dw = to_dw_dma(dwc->chan.device);
803 	unsigned int		count = 20;	/* timeout iterations */
804 
805 	dw->suspend_chan(dwc, drain);
806 
807 	while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY) && count--)
808 		udelay(2);
809 
810 	set_bit(DW_DMA_IS_PAUSED, &dwc->flags);
811 }
812 
813 static int dwc_pause(struct dma_chan *chan)
814 {
815 	struct dw_dma_chan	*dwc = to_dw_dma_chan(chan);
816 	unsigned long		flags;
817 
818 	spin_lock_irqsave(&dwc->lock, flags);
819 	dwc_chan_pause(dwc, false);
820 	spin_unlock_irqrestore(&dwc->lock, flags);
821 
822 	return 0;
823 }
824 
825 static inline void dwc_chan_resume(struct dw_dma_chan *dwc, bool drain)
826 {
827 	struct dw_dma *dw = to_dw_dma(dwc->chan.device);
828 
829 	dw->resume_chan(dwc, drain);
830 
831 	clear_bit(DW_DMA_IS_PAUSED, &dwc->flags);
832 }
833 
834 static int dwc_resume(struct dma_chan *chan)
835 {
836 	struct dw_dma_chan	*dwc = to_dw_dma_chan(chan);
837 	unsigned long		flags;
838 
839 	spin_lock_irqsave(&dwc->lock, flags);
840 
841 	if (test_bit(DW_DMA_IS_PAUSED, &dwc->flags))
842 		dwc_chan_resume(dwc, false);
843 
844 	spin_unlock_irqrestore(&dwc->lock, flags);
845 
846 	return 0;
847 }
848 
849 static int dwc_terminate_all(struct dma_chan *chan)
850 {
851 	struct dw_dma_chan	*dwc = to_dw_dma_chan(chan);
852 	struct dw_dma		*dw = to_dw_dma(chan->device);
853 	struct dw_desc		*desc, *_desc;
854 	unsigned long		flags;
855 	LIST_HEAD(list);
856 
857 	spin_lock_irqsave(&dwc->lock, flags);
858 
859 	clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
860 
861 	dwc_chan_pause(dwc, true);
862 
863 	dwc_chan_disable(dw, dwc);
864 
865 	dwc_chan_resume(dwc, true);
866 
867 	/* active_list entries will end up before queued entries */
868 	list_splice_init(&dwc->queue, &list);
869 	list_splice_init(&dwc->active_list, &list);
870 
871 	spin_unlock_irqrestore(&dwc->lock, flags);
872 
873 	/* Flush all pending and queued descriptors */
874 	list_for_each_entry_safe(desc, _desc, &list, desc_node)
875 		dwc_descriptor_complete(dwc, desc, false);
876 
877 	return 0;
878 }
879 
880 static struct dw_desc *dwc_find_desc(struct dw_dma_chan *dwc, dma_cookie_t c)
881 {
882 	struct dw_desc *desc;
883 
884 	list_for_each_entry(desc, &dwc->active_list, desc_node)
885 		if (desc->txd.cookie == c)
886 			return desc;
887 
888 	return NULL;
889 }
890 
891 static u32 dwc_get_residue(struct dw_dma_chan *dwc, dma_cookie_t cookie)
892 {
893 	struct dw_desc *desc;
894 	unsigned long flags;
895 	u32 residue;
896 
897 	spin_lock_irqsave(&dwc->lock, flags);
898 
899 	desc = dwc_find_desc(dwc, cookie);
900 	if (desc) {
901 		if (desc == dwc_first_active(dwc)) {
902 			residue = desc->residue;
903 			if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue)
904 				residue -= dwc_get_sent(dwc);
905 		} else {
906 			residue = desc->total_len;
907 		}
908 	} else {
909 		residue = 0;
910 	}
911 
912 	spin_unlock_irqrestore(&dwc->lock, flags);
913 	return residue;
914 }
915 
916 static enum dma_status
917 dwc_tx_status(struct dma_chan *chan,
918 	      dma_cookie_t cookie,
919 	      struct dma_tx_state *txstate)
920 {
921 	struct dw_dma_chan	*dwc = to_dw_dma_chan(chan);
922 	enum dma_status		ret;
923 
924 	ret = dma_cookie_status(chan, cookie, txstate);
925 	if (ret == DMA_COMPLETE)
926 		return ret;
927 
928 	dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
929 
930 	ret = dma_cookie_status(chan, cookie, txstate);
931 	if (ret == DMA_COMPLETE)
932 		return ret;
933 
934 	dma_set_residue(txstate, dwc_get_residue(dwc, cookie));
935 
936 	if (test_bit(DW_DMA_IS_PAUSED, &dwc->flags) && ret == DMA_IN_PROGRESS)
937 		return DMA_PAUSED;
938 
939 	return ret;
940 }
941 
942 static void dwc_issue_pending(struct dma_chan *chan)
943 {
944 	struct dw_dma_chan	*dwc = to_dw_dma_chan(chan);
945 	unsigned long		flags;
946 
947 	spin_lock_irqsave(&dwc->lock, flags);
948 	if (list_empty(&dwc->active_list))
949 		dwc_dostart_first_queued(dwc);
950 	spin_unlock_irqrestore(&dwc->lock, flags);
951 }
952 
953 /*----------------------------------------------------------------------*/
954 
955 void do_dw_dma_off(struct dw_dma *dw)
956 {
957 	dma_writel(dw, CFG, 0);
958 
959 	channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
960 	channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
961 	channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
962 	channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
963 	channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
964 
965 	while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
966 		cpu_relax();
967 }
968 
969 void do_dw_dma_on(struct dw_dma *dw)
970 {
971 	dma_writel(dw, CFG, DW_CFG_DMA_EN);
972 }
973 
974 static int dwc_alloc_chan_resources(struct dma_chan *chan)
975 {
976 	struct dw_dma_chan	*dwc = to_dw_dma_chan(chan);
977 	struct dw_dma		*dw = to_dw_dma(chan->device);
978 
979 	dev_vdbg(chan2dev(chan), "%s\n", __func__);
980 
981 	/* ASSERT:  channel is idle */
982 	if (dma_readl(dw, CH_EN) & dwc->mask) {
983 		dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
984 		return -EIO;
985 	}
986 
987 	dma_cookie_init(chan);
988 
989 	/*
990 	 * NOTE: some controllers may have additional features that we
991 	 * need to initialize here, like "scatter-gather" (which
992 	 * doesn't mean what you think it means), and status writeback.
993 	 */
994 
995 	/*
996 	 * We need controller-specific data to set up slave transfers.
997 	 */
998 	if (chan->private && !dw_dma_filter(chan, chan->private)) {
999 		dev_warn(chan2dev(chan), "Wrong controller-specific data\n");
1000 		return -EINVAL;
1001 	}
1002 
1003 	/* Enable controller here if needed */
1004 	if (!dw->in_use)
1005 		do_dw_dma_on(dw);
1006 	dw->in_use |= dwc->mask;
1007 
1008 	return 0;
1009 }
1010 
1011 static void dwc_free_chan_resources(struct dma_chan *chan)
1012 {
1013 	struct dw_dma_chan	*dwc = to_dw_dma_chan(chan);
1014 	struct dw_dma		*dw = to_dw_dma(chan->device);
1015 	unsigned long		flags;
1016 
1017 	dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
1018 			dwc->descs_allocated);
1019 
1020 	/* ASSERT:  channel is idle */
1021 	BUG_ON(!list_empty(&dwc->active_list));
1022 	BUG_ON(!list_empty(&dwc->queue));
1023 	BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
1024 
1025 	spin_lock_irqsave(&dwc->lock, flags);
1026 
1027 	/* Clear custom channel configuration */
1028 	memset(&dwc->dws, 0, sizeof(struct dw_dma_slave));
1029 
1030 	/* Disable interrupts */
1031 	channel_clear_bit(dw, MASK.XFER, dwc->mask);
1032 	channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
1033 	channel_clear_bit(dw, MASK.ERROR, dwc->mask);
1034 
1035 	spin_unlock_irqrestore(&dwc->lock, flags);
1036 
1037 	/* Disable controller in case it was a last user */
1038 	dw->in_use &= ~dwc->mask;
1039 	if (!dw->in_use)
1040 		do_dw_dma_off(dw);
1041 
1042 	dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
1043 }
1044 
1045 static void dwc_caps(struct dma_chan *chan, struct dma_slave_caps *caps)
1046 {
1047 	struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1048 
1049 	caps->max_burst = dwc->max_burst;
1050 
1051 	/*
1052 	 * It might be crucial for some devices to have the hardware
1053 	 * accelerated multi-block transfers supported, aka LLPs in DW DMAC
1054 	 * notation. So if LLPs are supported then max_sg_burst is set to
1055 	 * zero which means unlimited number of SG entries can be handled in a
1056 	 * single DMA transaction, otherwise it's just one SG entry.
1057 	 */
1058 	if (dwc->nollp)
1059 		caps->max_sg_burst = 1;
1060 	else
1061 		caps->max_sg_burst = 0;
1062 }
1063 
1064 int do_dma_probe(struct dw_dma_chip *chip)
1065 {
1066 	struct dw_dma *dw = chip->dw;
1067 	struct dw_dma_platform_data *pdata;
1068 	bool			autocfg = false;
1069 	unsigned int		dw_params;
1070 	unsigned int		i;
1071 	int			err;
1072 
1073 	dw->pdata = devm_kzalloc(chip->dev, sizeof(*dw->pdata), GFP_KERNEL);
1074 	if (!dw->pdata)
1075 		return -ENOMEM;
1076 
1077 	dw->regs = chip->regs;
1078 
1079 	pm_runtime_get_sync(chip->dev);
1080 
1081 	if (!chip->pdata) {
1082 		dw_params = dma_readl(dw, DW_PARAMS);
1083 		dev_dbg(chip->dev, "DW_PARAMS: 0x%08x\n", dw_params);
1084 
1085 		autocfg = dw_params >> DW_PARAMS_EN & 1;
1086 		if (!autocfg) {
1087 			err = -EINVAL;
1088 			goto err_pdata;
1089 		}
1090 
1091 		/* Reassign the platform data pointer */
1092 		pdata = dw->pdata;
1093 
1094 		/* Get hardware configuration parameters */
1095 		pdata->nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 7) + 1;
1096 		pdata->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1;
1097 		for (i = 0; i < pdata->nr_masters; i++) {
1098 			pdata->data_width[i] =
1099 				4 << (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3);
1100 		}
1101 		pdata->block_size = dma_readl(dw, MAX_BLK_SIZE);
1102 
1103 		/* Fill platform data with the default values */
1104 		pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING;
1105 		pdata->chan_priority = CHAN_PRIORITY_ASCENDING;
1106 	} else if (chip->pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS) {
1107 		err = -EINVAL;
1108 		goto err_pdata;
1109 	} else {
1110 		memcpy(dw->pdata, chip->pdata, sizeof(*dw->pdata));
1111 
1112 		/* Reassign the platform data pointer */
1113 		pdata = dw->pdata;
1114 	}
1115 
1116 	dw->chan = devm_kcalloc(chip->dev, pdata->nr_channels, sizeof(*dw->chan),
1117 				GFP_KERNEL);
1118 	if (!dw->chan) {
1119 		err = -ENOMEM;
1120 		goto err_pdata;
1121 	}
1122 
1123 	/* Calculate all channel mask before DMA setup */
1124 	dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
1125 
1126 	/* Force dma off, just in case */
1127 	dw->disable(dw);
1128 
1129 	/* Device and instance ID for IRQ and DMA pool */
1130 	dw->set_device_name(dw, chip->id);
1131 
1132 	/* Create a pool of consistent memory blocks for hardware descriptors */
1133 	dw->desc_pool = dmam_pool_create(dw->name, chip->dev,
1134 					 sizeof(struct dw_desc), 4, 0);
1135 	if (!dw->desc_pool) {
1136 		dev_err(chip->dev, "No memory for descriptors dma pool\n");
1137 		err = -ENOMEM;
1138 		goto err_pdata;
1139 	}
1140 
1141 	tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1142 
1143 	err = request_irq(chip->irq, dw_dma_interrupt, IRQF_SHARED,
1144 			  dw->name, dw);
1145 	if (err)
1146 		goto err_pdata;
1147 
1148 	INIT_LIST_HEAD(&dw->dma.channels);
1149 	for (i = 0; i < pdata->nr_channels; i++) {
1150 		struct dw_dma_chan	*dwc = &dw->chan[i];
1151 
1152 		dwc->chan.device = &dw->dma;
1153 		dma_cookie_init(&dwc->chan);
1154 		if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
1155 			list_add_tail(&dwc->chan.device_node,
1156 					&dw->dma.channels);
1157 		else
1158 			list_add(&dwc->chan.device_node, &dw->dma.channels);
1159 
1160 		/* 7 is highest priority & 0 is lowest. */
1161 		if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
1162 			dwc->priority = pdata->nr_channels - i - 1;
1163 		else
1164 			dwc->priority = i;
1165 
1166 		dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1167 		spin_lock_init(&dwc->lock);
1168 		dwc->mask = 1 << i;
1169 
1170 		INIT_LIST_HEAD(&dwc->active_list);
1171 		INIT_LIST_HEAD(&dwc->queue);
1172 
1173 		channel_clear_bit(dw, CH_EN, dwc->mask);
1174 
1175 		dwc->direction = DMA_TRANS_NONE;
1176 
1177 		/* Hardware configuration */
1178 		if (autocfg) {
1179 			unsigned int r = DW_DMA_MAX_NR_CHANNELS - i - 1;
1180 			void __iomem *addr = &__dw_regs(dw)->DWC_PARAMS[r];
1181 			unsigned int dwc_params = readl(addr);
1182 
1183 			dev_dbg(chip->dev, "DWC_PARAMS[%d]: 0x%08x\n", i,
1184 					   dwc_params);
1185 
1186 			/*
1187 			 * Decode maximum block size for given channel. The
1188 			 * stored 4 bit value represents blocks from 0x00 for 3
1189 			 * up to 0x0a for 4095.
1190 			 */
1191 			dwc->block_size =
1192 				(4 << ((pdata->block_size >> 4 * i) & 0xf)) - 1;
1193 
1194 			/*
1195 			 * According to the DW DMA databook the true scatter-
1196 			 * gether LLPs aren't available if either multi-block
1197 			 * config is disabled (CHx_MULTI_BLK_EN == 0) or the
1198 			 * LLP register is hard-coded to zeros
1199 			 * (CHx_HC_LLP == 1).
1200 			 */
1201 			dwc->nollp =
1202 				(dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0 ||
1203 				(dwc_params >> DWC_PARAMS_HC_LLP & 0x1) == 1;
1204 			dwc->max_burst =
1205 				(0x4 << (dwc_params >> DWC_PARAMS_MSIZE & 0x7));
1206 		} else {
1207 			dwc->block_size = pdata->block_size;
1208 			dwc->nollp = !pdata->multi_block[i];
1209 			dwc->max_burst = pdata->max_burst[i] ?: DW_DMA_MAX_BURST;
1210 		}
1211 	}
1212 
1213 	/* Clear all interrupts on all channels. */
1214 	dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
1215 	dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
1216 	dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1217 	dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1218 	dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1219 
1220 	/* Set capabilities */
1221 	dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
1222 	dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
1223 	dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1224 
1225 	dw->dma.dev = chip->dev;
1226 	dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1227 	dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1228 
1229 	dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
1230 	dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
1231 
1232 	dw->dma.device_caps = dwc_caps;
1233 	dw->dma.device_config = dwc_config;
1234 	dw->dma.device_pause = dwc_pause;
1235 	dw->dma.device_resume = dwc_resume;
1236 	dw->dma.device_terminate_all = dwc_terminate_all;
1237 
1238 	dw->dma.device_tx_status = dwc_tx_status;
1239 	dw->dma.device_issue_pending = dwc_issue_pending;
1240 
1241 	/* DMA capabilities */
1242 	dw->dma.min_burst = DW_DMA_MIN_BURST;
1243 	dw->dma.max_burst = DW_DMA_MAX_BURST;
1244 	dw->dma.src_addr_widths = DW_DMA_BUSWIDTHS;
1245 	dw->dma.dst_addr_widths = DW_DMA_BUSWIDTHS;
1246 	dw->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
1247 			     BIT(DMA_MEM_TO_MEM);
1248 	dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1249 
1250 	/*
1251 	 * For now there is no hardware with non uniform maximum block size
1252 	 * across all of the device channels, so we set the maximum segment
1253 	 * size as the block size found for the very first channel.
1254 	 */
1255 	dma_set_max_seg_size(dw->dma.dev, dw->chan[0].block_size);
1256 
1257 	err = dma_async_device_register(&dw->dma);
1258 	if (err)
1259 		goto err_dma_register;
1260 
1261 	dev_info(chip->dev, "DesignWare DMA Controller, %d channels\n",
1262 		 pdata->nr_channels);
1263 
1264 	pm_runtime_put_sync_suspend(chip->dev);
1265 
1266 	return 0;
1267 
1268 err_dma_register:
1269 	free_irq(chip->irq, dw);
1270 err_pdata:
1271 	pm_runtime_put_sync_suspend(chip->dev);
1272 	return err;
1273 }
1274 
1275 int do_dma_remove(struct dw_dma_chip *chip)
1276 {
1277 	struct dw_dma		*dw = chip->dw;
1278 	struct dw_dma_chan	*dwc, *_dwc;
1279 
1280 	pm_runtime_get_sync(chip->dev);
1281 
1282 	do_dw_dma_off(dw);
1283 	dma_async_device_unregister(&dw->dma);
1284 
1285 	free_irq(chip->irq, dw);
1286 	tasklet_kill(&dw->tasklet);
1287 
1288 	list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1289 			chan.device_node) {
1290 		list_del(&dwc->chan.device_node);
1291 		channel_clear_bit(dw, CH_EN, dwc->mask);
1292 	}
1293 
1294 	pm_runtime_put_sync_suspend(chip->dev);
1295 	return 0;
1296 }
1297 
1298 int do_dw_dma_disable(struct dw_dma_chip *chip)
1299 {
1300 	struct dw_dma *dw = chip->dw;
1301 
1302 	dw->disable(dw);
1303 	return 0;
1304 }
1305 EXPORT_SYMBOL_GPL(do_dw_dma_disable);
1306 
1307 int do_dw_dma_enable(struct dw_dma_chip *chip)
1308 {
1309 	struct dw_dma *dw = chip->dw;
1310 
1311 	dw->enable(dw);
1312 	return 0;
1313 }
1314 EXPORT_SYMBOL_GPL(do_dw_dma_enable);
1315 
1316 MODULE_LICENSE("GPL v2");
1317 MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver");
1318 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1319 MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
1320