xref: /linux/drivers/dma/sun4i-dma.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2014 Emilio López
4  * Emilio López <emilio@elopez.com.ar>
5  */
6 
7 #include <linux/bitmap.h>
8 #include <linux/bitops.h>
9 #include <linux/clk.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/dmaengine.h>
12 #include <linux/dmapool.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/of_dma.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/reset.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 
22 #include "virt-dma.h"
23 
24 /** Common macros to normal and dedicated DMA registers **/
25 
26 #define SUN4I_DMA_CFG_LOADING			BIT(31)
27 #define SUN4I_DMA_CFG_DST_DATA_WIDTH(width)	((width) << 25)
28 #define SUN4I_DMA_CFG_DST_BURST_LENGTH(len)	((len) << 23)
29 #define SUN4I_DMA_CFG_DST_ADDR_MODE(mode)	((mode) << 21)
30 #define SUN4I_DMA_CFG_DST_DRQ_TYPE(type)	((type) << 16)
31 #define SUN4I_DMA_CFG_SRC_DATA_WIDTH(width)	((width) << 9)
32 #define SUN4I_DMA_CFG_SRC_BURST_LENGTH(len)	((len) << 7)
33 #define SUN4I_DMA_CFG_SRC_ADDR_MODE(mode)	((mode) << 5)
34 #define SUN4I_DMA_CFG_SRC_DRQ_TYPE(type)	(type)
35 
36 #define SUNIV_DMA_CFG_DST_DATA_WIDTH(width)	((width) << 24)
37 #define SUNIV_DMA_CFG_SRC_DATA_WIDTH(width)	((width) << 8)
38 
39 #define SUN4I_MAX_BURST	8
40 #define SUNIV_MAX_BURST	4
41 
42 /** Normal DMA register values **/
43 
44 /* Normal DMA source/destination data request type values */
45 #define SUN4I_NDMA_DRQ_TYPE_SDRAM		0x16
46 #define SUN4I_NDMA_DRQ_TYPE_LIMIT		(0x1F + 1)
47 
48 #define SUNIV_NDMA_DRQ_TYPE_SDRAM		0x11
49 #define SUNIV_NDMA_DRQ_TYPE_LIMIT		(0x17 + 1)
50 
51 /** Normal DMA register layout **/
52 
53 /* Dedicated DMA source/destination address mode values */
54 #define SUN4I_NDMA_ADDR_MODE_LINEAR		0
55 #define SUN4I_NDMA_ADDR_MODE_IO			1
56 
57 /* Normal DMA configuration register layout */
58 #define SUN4I_NDMA_CFG_CONT_MODE		BIT(30)
59 #define SUN4I_NDMA_CFG_WAIT_STATE(n)		((n) << 27)
60 #define SUN4I_NDMA_CFG_DST_NON_SECURE		BIT(22)
61 #define SUN4I_NDMA_CFG_BYTE_COUNT_MODE_REMAIN	BIT(15)
62 #define SUN4I_NDMA_CFG_SRC_NON_SECURE		BIT(6)
63 
64 #define SUNIV_NDMA_CFG_CONT_MODE		BIT(29)
65 #define SUNIV_NDMA_CFG_WAIT_STATE(n)		((n) << 26)
66 
67 /** Dedicated DMA register values **/
68 
69 /* Dedicated DMA source/destination address mode values */
70 #define SUN4I_DDMA_ADDR_MODE_LINEAR		0
71 #define SUN4I_DDMA_ADDR_MODE_IO			1
72 #define SUN4I_DDMA_ADDR_MODE_HORIZONTAL_PAGE	2
73 #define SUN4I_DDMA_ADDR_MODE_VERTICAL_PAGE	3
74 
75 /* Dedicated DMA source/destination data request type values */
76 #define SUN4I_DDMA_DRQ_TYPE_SDRAM		0x1
77 #define SUN4I_DDMA_DRQ_TYPE_LIMIT		(0x1F + 1)
78 
79 #define SUNIV_DDMA_DRQ_TYPE_SDRAM		0x1
80 #define SUNIV_DDMA_DRQ_TYPE_LIMIT		(0x9 + 1)
81 
82 /** Dedicated DMA register layout **/
83 
84 /* Dedicated DMA configuration register layout */
85 #define SUN4I_DDMA_CFG_BUSY			BIT(30)
86 #define SUN4I_DDMA_CFG_CONT_MODE		BIT(29)
87 #define SUN4I_DDMA_CFG_DST_NON_SECURE		BIT(28)
88 #define SUN4I_DDMA_CFG_BYTE_COUNT_MODE_REMAIN	BIT(15)
89 #define SUN4I_DDMA_CFG_SRC_NON_SECURE		BIT(12)
90 
91 /* Dedicated DMA parameter register layout */
92 #define SUN4I_DDMA_PARA_DST_DATA_BLK_SIZE(n)	(((n) - 1) << 24)
93 #define SUN4I_DDMA_PARA_DST_WAIT_CYCLES(n)	(((n) - 1) << 16)
94 #define SUN4I_DDMA_PARA_SRC_DATA_BLK_SIZE(n)	(((n) - 1) << 8)
95 #define SUN4I_DDMA_PARA_SRC_WAIT_CYCLES(n)	(((n) - 1) << 0)
96 
97 /** DMA register offsets **/
98 
99 /* General register offsets */
100 #define SUN4I_DMA_IRQ_ENABLE_REG		0x0
101 #define SUN4I_DMA_IRQ_PENDING_STATUS_REG	0x4
102 
103 /* Normal DMA register offsets */
104 #define SUN4I_NDMA_CHANNEL_REG_BASE(n)		(0x100 + (n) * 0x20)
105 #define SUN4I_NDMA_CFG_REG			0x0
106 #define SUN4I_NDMA_SRC_ADDR_REG			0x4
107 #define SUN4I_NDMA_DST_ADDR_REG		0x8
108 #define SUN4I_NDMA_BYTE_COUNT_REG		0xC
109 
110 /* Dedicated DMA register offsets */
111 #define SUN4I_DDMA_CHANNEL_REG_BASE(n)		(0x300 + (n) * 0x20)
112 #define SUN4I_DDMA_CFG_REG			0x0
113 #define SUN4I_DDMA_SRC_ADDR_REG			0x4
114 #define SUN4I_DDMA_DST_ADDR_REG		0x8
115 #define SUN4I_DDMA_BYTE_COUNT_REG		0xC
116 #define SUN4I_DDMA_PARA_REG			0x18
117 
118 /** DMA Driver **/
119 
120 /*
121  * Normal DMA has 8 channels, and Dedicated DMA has another 8, so
122  * that's 16 channels. As for endpoints, there's 29 and 21
123  * respectively. Given that the Normal DMA endpoints (other than
124  * SDRAM) can be used as tx/rx, we need 78 vchans in total
125  */
126 #define SUN4I_NDMA_NR_MAX_CHANNELS	8
127 #define SUN4I_DDMA_NR_MAX_CHANNELS	8
128 #define SUN4I_DMA_NR_MAX_CHANNELS					\
129 	(SUN4I_NDMA_NR_MAX_CHANNELS + SUN4I_DDMA_NR_MAX_CHANNELS)
130 #define SUN4I_NDMA_NR_MAX_VCHANS	(29 * 2 - 1)
131 #define SUN4I_DDMA_NR_MAX_VCHANS	21
132 #define SUN4I_DMA_NR_MAX_VCHANS						\
133 	(SUN4I_NDMA_NR_MAX_VCHANS + SUN4I_DDMA_NR_MAX_VCHANS)
134 
135 #define SUNIV_NDMA_NR_MAX_CHANNELS	4
136 #define SUNIV_DDMA_NR_MAX_CHANNELS	4
137 #define SUNIV_NDMA_NR_MAX_VCHANS	(24 * 2 - 1)
138 #define SUNIV_DDMA_NR_MAX_VCHANS	10
139 
140 /* This set of SUN4I_DDMA timing parameters were found experimentally while
141  * working with the SPI driver and seem to make it behave correctly */
142 #define SUN4I_DDMA_MAGIC_SPI_PARAMETERS \
143 	(SUN4I_DDMA_PARA_DST_DATA_BLK_SIZE(1) |			\
144 	 SUN4I_DDMA_PARA_SRC_DATA_BLK_SIZE(1) |				\
145 	 SUN4I_DDMA_PARA_DST_WAIT_CYCLES(2) |				\
146 	 SUN4I_DDMA_PARA_SRC_WAIT_CYCLES(2))
147 
148 /*
149  * Normal DMA supports individual transfers (segments) up to 128k.
150  * Dedicated DMA supports transfers up to 16M. We can only report
151  * one size limit, so we have to use the smaller value.
152  */
153 #define SUN4I_NDMA_MAX_SEG_SIZE		SZ_128K
154 #define SUN4I_DDMA_MAX_SEG_SIZE		SZ_16M
155 #define SUN4I_DMA_MAX_SEG_SIZE		SUN4I_NDMA_MAX_SEG_SIZE
156 
157 /*
158  * Hardware channels / ports representation
159  *
160  * The hardware is used in several SoCs, with differing numbers
161  * of channels and endpoints. This structure ties those numbers
162  * to a certain compatible string.
163  */
164 struct sun4i_dma_config {
165 	u32 ndma_nr_max_channels;
166 	u32 ndma_nr_max_vchans;
167 
168 	u32 ddma_nr_max_channels;
169 	u32 ddma_nr_max_vchans;
170 
171 	u32 dma_nr_max_channels;
172 
173 	void (*set_dst_data_width)(u32 *p_cfg, s8 data_width);
174 	void (*set_src_data_width)(u32 *p_cfg, s8 data_width);
175 	int (*convert_burst)(u32 maxburst);
176 
177 	u8 ndma_drq_sdram;
178 	u8 ddma_drq_sdram;
179 
180 	u8 max_burst;
181 	bool has_reset;
182 };
183 
184 struct sun4i_dma_pchan {
185 	/* Register base of channel */
186 	void __iomem			*base;
187 	/* vchan currently being serviced */
188 	struct sun4i_dma_vchan		*vchan;
189 	/* Is this a dedicated pchan? */
190 	int				is_dedicated;
191 };
192 
193 struct sun4i_dma_vchan {
194 	struct virt_dma_chan		vc;
195 	struct dma_slave_config		cfg;
196 	struct sun4i_dma_pchan		*pchan;
197 	struct sun4i_dma_promise	*processing;
198 	struct sun4i_dma_contract	*contract;
199 	u8				endpoint;
200 	int				is_dedicated;
201 };
202 
203 struct sun4i_dma_promise {
204 	u32				cfg;
205 	u32				para;
206 	dma_addr_t			src;
207 	dma_addr_t			dst;
208 	size_t				len;
209 	struct list_head		list;
210 };
211 
212 /* A contract is a set of promises */
213 struct sun4i_dma_contract {
214 	struct virt_dma_desc		vd;
215 	struct list_head		demands;
216 	struct list_head		completed_demands;
217 	bool				is_cyclic : 1;
218 	bool				use_half_int : 1;
219 };
220 
221 struct sun4i_dma_dev {
222 	unsigned long *pchans_used;
223 	struct dma_device		slave;
224 	struct sun4i_dma_pchan		*pchans;
225 	struct sun4i_dma_vchan		*vchans;
226 	void __iomem			*base;
227 	struct clk			*clk;
228 	int				irq;
229 	spinlock_t			lock;
230 	const struct sun4i_dma_config *cfg;
231 	struct reset_control *rst;
232 };
233 
234 static struct sun4i_dma_dev *to_sun4i_dma_dev(struct dma_device *dev)
235 {
236 	return container_of(dev, struct sun4i_dma_dev, slave);
237 }
238 
239 static struct sun4i_dma_vchan *to_sun4i_dma_vchan(struct dma_chan *chan)
240 {
241 	return container_of(chan, struct sun4i_dma_vchan, vc.chan);
242 }
243 
244 static struct sun4i_dma_contract *to_sun4i_dma_contract(struct virt_dma_desc *vd)
245 {
246 	return container_of(vd, struct sun4i_dma_contract, vd);
247 }
248 
249 static void set_dst_data_width_a10(u32 *p_cfg, s8 data_width)
250 {
251 	*p_cfg |= SUN4I_DMA_CFG_DST_DATA_WIDTH(data_width);
252 }
253 
254 static void set_src_data_width_a10(u32 *p_cfg, s8 data_width)
255 {
256 	*p_cfg |= SUN4I_DMA_CFG_SRC_DATA_WIDTH(data_width);
257 }
258 
259 static void set_dst_data_width_f1c100s(u32 *p_cfg, s8 data_width)
260 {
261 	*p_cfg |= SUNIV_DMA_CFG_DST_DATA_WIDTH(data_width);
262 }
263 
264 static void set_src_data_width_f1c100s(u32 *p_cfg, s8 data_width)
265 {
266 	*p_cfg |= SUNIV_DMA_CFG_SRC_DATA_WIDTH(data_width);
267 }
268 
269 static int convert_burst_a10(u32 maxburst)
270 {
271 	if (maxburst > 8)
272 		return -EINVAL;
273 
274 	/* 1 -> 0, 4 -> 1, 8 -> 2 */
275 	return (maxburst >> 2);
276 }
277 
278 static int convert_burst_f1c100s(u32 maxburst)
279 {
280 	if (maxburst > 4)
281 		return -EINVAL;
282 
283 	/* 1 -> 0, 4 -> 1 */
284 	return (maxburst >> 2);
285 }
286 
287 static int convert_buswidth(enum dma_slave_buswidth addr_width)
288 {
289 	if (addr_width > DMA_SLAVE_BUSWIDTH_4_BYTES)
290 		return -EINVAL;
291 
292 	/* 8 (1 byte) -> 0, 16 (2 bytes) -> 1, 32 (4 bytes) -> 2 */
293 	return (addr_width >> 1);
294 }
295 
296 static void sun4i_dma_free_chan_resources(struct dma_chan *chan)
297 {
298 	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
299 
300 	vchan_free_chan_resources(&vchan->vc);
301 }
302 
303 static struct sun4i_dma_pchan *find_and_use_pchan(struct sun4i_dma_dev *priv,
304 						  struct sun4i_dma_vchan *vchan)
305 {
306 	struct sun4i_dma_pchan *pchan = NULL, *pchans = priv->pchans;
307 	unsigned long flags;
308 	int i, max;
309 
310 	/*
311 	 * pchans 0-priv->cfg->ndma_nr_max_channels are normal, and
312 	 * priv->cfg->ndma_nr_max_channels+ are dedicated ones
313 	 */
314 	if (vchan->is_dedicated) {
315 		i = priv->cfg->ndma_nr_max_channels;
316 		max = priv->cfg->dma_nr_max_channels;
317 	} else {
318 		i = 0;
319 		max = priv->cfg->ndma_nr_max_channels;
320 	}
321 
322 	spin_lock_irqsave(&priv->lock, flags);
323 	for_each_clear_bit_from(i, priv->pchans_used, max) {
324 		pchan = &pchans[i];
325 		pchan->vchan = vchan;
326 		set_bit(i, priv->pchans_used);
327 		break;
328 	}
329 	spin_unlock_irqrestore(&priv->lock, flags);
330 
331 	return pchan;
332 }
333 
334 static void release_pchan(struct sun4i_dma_dev *priv,
335 			  struct sun4i_dma_pchan *pchan)
336 {
337 	unsigned long flags;
338 	int nr = pchan - priv->pchans;
339 
340 	spin_lock_irqsave(&priv->lock, flags);
341 
342 	pchan->vchan = NULL;
343 	clear_bit(nr, priv->pchans_used);
344 
345 	spin_unlock_irqrestore(&priv->lock, flags);
346 }
347 
348 static void configure_pchan(struct sun4i_dma_pchan *pchan,
349 			    struct sun4i_dma_promise *d)
350 {
351 	/*
352 	 * Configure addresses and misc parameters depending on type
353 	 * SUN4I_DDMA has an extra field with timing parameters
354 	 */
355 	if (pchan->is_dedicated) {
356 		writel_relaxed(d->src, pchan->base + SUN4I_DDMA_SRC_ADDR_REG);
357 		writel_relaxed(d->dst, pchan->base + SUN4I_DDMA_DST_ADDR_REG);
358 		writel_relaxed(d->len, pchan->base + SUN4I_DDMA_BYTE_COUNT_REG);
359 		writel_relaxed(d->para, pchan->base + SUN4I_DDMA_PARA_REG);
360 		writel_relaxed(d->cfg, pchan->base + SUN4I_DDMA_CFG_REG);
361 	} else {
362 		writel_relaxed(d->src, pchan->base + SUN4I_NDMA_SRC_ADDR_REG);
363 		writel_relaxed(d->dst, pchan->base + SUN4I_NDMA_DST_ADDR_REG);
364 		writel_relaxed(d->len, pchan->base + SUN4I_NDMA_BYTE_COUNT_REG);
365 		writel_relaxed(d->cfg, pchan->base + SUN4I_NDMA_CFG_REG);
366 	}
367 }
368 
369 static void set_pchan_interrupt(struct sun4i_dma_dev *priv,
370 				struct sun4i_dma_pchan *pchan,
371 				int half, int end)
372 {
373 	u32 reg;
374 	int pchan_number = pchan - priv->pchans;
375 	unsigned long flags;
376 
377 	spin_lock_irqsave(&priv->lock, flags);
378 
379 	reg = readl_relaxed(priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
380 
381 	if (half)
382 		reg |= BIT(pchan_number * 2);
383 	else
384 		reg &= ~BIT(pchan_number * 2);
385 
386 	if (end)
387 		reg |= BIT(pchan_number * 2 + 1);
388 	else
389 		reg &= ~BIT(pchan_number * 2 + 1);
390 
391 	writel_relaxed(reg, priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
392 
393 	spin_unlock_irqrestore(&priv->lock, flags);
394 }
395 
396 /*
397  * Execute pending operations on a vchan
398  *
399  * When given a vchan, this function will try to acquire a suitable
400  * pchan and, if successful, will configure it to fulfill a promise
401  * from the next pending contract.
402  *
403  * This function must be called with &vchan->vc.lock held.
404  */
405 static int __execute_vchan_pending(struct sun4i_dma_dev *priv,
406 				   struct sun4i_dma_vchan *vchan)
407 {
408 	struct sun4i_dma_promise *promise = NULL;
409 	struct sun4i_dma_contract *contract = NULL;
410 	struct sun4i_dma_pchan *pchan;
411 	struct virt_dma_desc *vd;
412 	int ret;
413 
414 	lockdep_assert_held(&vchan->vc.lock);
415 
416 	/* We need a pchan to do anything, so secure one if available */
417 	pchan = find_and_use_pchan(priv, vchan);
418 	if (!pchan)
419 		return -EBUSY;
420 
421 	/*
422 	 * Channel endpoints must not be repeated, so if this vchan
423 	 * has already submitted some work, we can't do anything else
424 	 */
425 	if (vchan->processing) {
426 		dev_dbg(vchan_chan_dev(&vchan->vc),
427 			"processing something to this endpoint already\n");
428 		ret = -EBUSY;
429 		goto release_pchan;
430 	}
431 
432 	do {
433 		/* Figure out which contract we're working with today */
434 		vd = vchan_next_desc(&vchan->vc);
435 		if (!vd) {
436 			dev_dbg(vchan_chan_dev(&vchan->vc),
437 				"No pending contract found");
438 			ret = 0;
439 			goto release_pchan;
440 		}
441 
442 		contract = to_sun4i_dma_contract(vd);
443 		if (list_empty(&contract->demands)) {
444 			/* The contract has been completed so mark it as such */
445 			list_del(&contract->vd.node);
446 			vchan_cookie_complete(&contract->vd);
447 			dev_dbg(vchan_chan_dev(&vchan->vc),
448 				"Empty contract found and marked complete");
449 		}
450 	} while (list_empty(&contract->demands));
451 
452 	/* Now find out what we need to do */
453 	promise = list_first_entry(&contract->demands,
454 				   struct sun4i_dma_promise, list);
455 	vchan->processing = promise;
456 
457 	/* ... and make it reality */
458 	if (promise) {
459 		vchan->contract = contract;
460 		vchan->pchan = pchan;
461 		set_pchan_interrupt(priv, pchan, contract->use_half_int, 1);
462 		configure_pchan(pchan, promise);
463 	}
464 
465 	return 0;
466 
467 release_pchan:
468 	release_pchan(priv, pchan);
469 	return ret;
470 }
471 
472 static int sanitize_config(struct dma_slave_config *sconfig,
473 			   enum dma_transfer_direction direction)
474 {
475 	switch (direction) {
476 	case DMA_MEM_TO_DEV:
477 		if ((sconfig->dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) ||
478 		    !sconfig->dst_maxburst)
479 			return -EINVAL;
480 
481 		if (sconfig->src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
482 			sconfig->src_addr_width = sconfig->dst_addr_width;
483 
484 		if (!sconfig->src_maxburst)
485 			sconfig->src_maxburst = sconfig->dst_maxburst;
486 
487 		break;
488 
489 	case DMA_DEV_TO_MEM:
490 		if ((sconfig->src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) ||
491 		    !sconfig->src_maxburst)
492 			return -EINVAL;
493 
494 		if (sconfig->dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
495 			sconfig->dst_addr_width = sconfig->src_addr_width;
496 
497 		if (!sconfig->dst_maxburst)
498 			sconfig->dst_maxburst = sconfig->src_maxburst;
499 
500 		break;
501 	default:
502 		return 0;
503 	}
504 
505 	return 0;
506 }
507 
508 /*
509  * Generate a promise, to be used in a normal DMA contract.
510  *
511  * A NDMA promise contains all the information required to program the
512  * normal part of the DMA Engine and get data copied. A non-executed
513  * promise will live in the demands list on a contract. Once it has been
514  * completed, it will be moved to the completed demands list for later freeing.
515  * All linked promises will be freed when the corresponding contract is freed
516  */
517 static struct sun4i_dma_promise *
518 generate_ndma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
519 		      size_t len, struct dma_slave_config *sconfig,
520 		      enum dma_transfer_direction direction)
521 {
522 	struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
523 	struct sun4i_dma_promise *promise;
524 	int ret;
525 
526 	ret = sanitize_config(sconfig, direction);
527 	if (ret)
528 		return NULL;
529 
530 	promise = kzalloc_obj(*promise, GFP_NOWAIT);
531 	if (!promise)
532 		return NULL;
533 
534 	promise->src = src;
535 	promise->dst = dest;
536 	promise->len = len;
537 	promise->cfg = SUN4I_DMA_CFG_LOADING |
538 		SUN4I_NDMA_CFG_BYTE_COUNT_MODE_REMAIN;
539 
540 	dev_dbg(dmaengine_chan_dev(chan),
541 		"src burst %d, dst burst %d, src buswidth %d, dst buswidth %d",
542 		sconfig->src_maxburst, sconfig->dst_maxburst,
543 		sconfig->src_addr_width, sconfig->dst_addr_width);
544 
545 	/* Source burst */
546 	ret = priv->cfg->convert_burst(sconfig->src_maxburst);
547 	if (ret < 0)
548 		goto fail;
549 	promise->cfg |= SUN4I_DMA_CFG_SRC_BURST_LENGTH(ret);
550 
551 	/* Destination burst */
552 	ret = priv->cfg->convert_burst(sconfig->dst_maxburst);
553 	if (ret < 0)
554 		goto fail;
555 	promise->cfg |= SUN4I_DMA_CFG_DST_BURST_LENGTH(ret);
556 
557 	/* Source bus width */
558 	ret = convert_buswidth(sconfig->src_addr_width);
559 	if (ret < 0)
560 		goto fail;
561 	priv->cfg->set_src_data_width(&promise->cfg, ret);
562 
563 	/* Destination bus width */
564 	ret = convert_buswidth(sconfig->dst_addr_width);
565 	if (ret < 0)
566 		goto fail;
567 	priv->cfg->set_dst_data_width(&promise->cfg, ret);
568 
569 	return promise;
570 
571 fail:
572 	kfree(promise);
573 	return NULL;
574 }
575 
576 /*
577  * Generate a promise, to be used in a dedicated DMA contract.
578  *
579  * A DDMA promise contains all the information required to program the
580  * Dedicated part of the DMA Engine and get data copied. A non-executed
581  * promise will live in the demands list on a contract. Once it has been
582  * completed, it will be moved to the completed demands list for later freeing.
583  * All linked promises will be freed when the corresponding contract is freed
584  */
585 static struct sun4i_dma_promise *
586 generate_ddma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
587 		      size_t len, struct dma_slave_config *sconfig)
588 {
589 	struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
590 	struct sun4i_dma_promise *promise;
591 	int ret;
592 
593 	promise = kzalloc_obj(*promise, GFP_NOWAIT);
594 	if (!promise)
595 		return NULL;
596 
597 	promise->src = src;
598 	promise->dst = dest;
599 	promise->len = len;
600 	promise->cfg = SUN4I_DMA_CFG_LOADING |
601 		SUN4I_DDMA_CFG_BYTE_COUNT_MODE_REMAIN;
602 
603 	/* Source burst */
604 	ret = priv->cfg->convert_burst(sconfig->src_maxburst);
605 	if (ret < 0)
606 		goto fail;
607 	promise->cfg |= SUN4I_DMA_CFG_SRC_BURST_LENGTH(ret);
608 
609 	/* Destination burst */
610 	ret = priv->cfg->convert_burst(sconfig->dst_maxburst);
611 	if (ret < 0)
612 		goto fail;
613 	promise->cfg |= SUN4I_DMA_CFG_DST_BURST_LENGTH(ret);
614 
615 	/* Source bus width */
616 	ret = convert_buswidth(sconfig->src_addr_width);
617 	if (ret < 0)
618 		goto fail;
619 	priv->cfg->set_src_data_width(&promise->cfg, ret);
620 
621 	/* Destination bus width */
622 	ret = convert_buswidth(sconfig->dst_addr_width);
623 	if (ret < 0)
624 		goto fail;
625 	priv->cfg->set_dst_data_width(&promise->cfg, ret);
626 
627 	return promise;
628 
629 fail:
630 	kfree(promise);
631 	return NULL;
632 }
633 
634 /*
635  * Generate a contract
636  *
637  * Contracts function as DMA descriptors. As our hardware does not support
638  * linked lists, we need to implement SG via software. We use a contract
639  * to hold all the pieces of the request and process them serially one
640  * after another. Each piece is represented as a promise.
641  */
642 static struct sun4i_dma_contract *generate_dma_contract(void)
643 {
644 	struct sun4i_dma_contract *contract;
645 
646 	contract = kzalloc_obj(*contract, GFP_NOWAIT);
647 	if (!contract)
648 		return NULL;
649 
650 	INIT_LIST_HEAD(&contract->demands);
651 	INIT_LIST_HEAD(&contract->completed_demands);
652 
653 	return contract;
654 }
655 
656 /*
657  * Get next promise on a cyclic transfer
658  *
659  * Cyclic contracts contain a series of promises which are executed on a
660  * loop. This function returns the next promise from a cyclic contract,
661  * so it can be programmed into the hardware.
662  */
663 static struct sun4i_dma_promise *
664 get_next_cyclic_promise(struct sun4i_dma_contract *contract)
665 {
666 	struct sun4i_dma_promise *promise;
667 
668 	promise = list_first_entry_or_null(&contract->demands,
669 					   struct sun4i_dma_promise, list);
670 	if (!promise) {
671 		list_splice_init(&contract->completed_demands,
672 				 &contract->demands);
673 		promise = list_first_entry(&contract->demands,
674 					   struct sun4i_dma_promise, list);
675 	}
676 
677 	return promise;
678 }
679 
680 /*
681  * Free a contract and all its associated promises
682  */
683 static void sun4i_dma_free_contract(struct virt_dma_desc *vd)
684 {
685 	struct sun4i_dma_contract *contract = to_sun4i_dma_contract(vd);
686 	struct sun4i_dma_promise *promise, *tmp;
687 
688 	/* Free all the demands and completed demands */
689 	list_for_each_entry_safe(promise, tmp, &contract->demands, list)
690 		kfree(promise);
691 
692 	list_for_each_entry_safe(promise, tmp, &contract->completed_demands, list)
693 		kfree(promise);
694 
695 	kfree(contract);
696 }
697 
698 static struct dma_async_tx_descriptor *
699 sun4i_dma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest,
700 			  dma_addr_t src, size_t len, unsigned long flags)
701 {
702 	struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
703 	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
704 	struct dma_slave_config *sconfig = &vchan->cfg;
705 	struct sun4i_dma_promise *promise;
706 	struct sun4i_dma_contract *contract;
707 
708 	contract = generate_dma_contract();
709 	if (!contract)
710 		return NULL;
711 
712 	/*
713 	 * We can only do the copy to bus aligned addresses, so
714 	 * choose the best one so we get decent performance. We also
715 	 * maximize the burst size for this same reason.
716 	 */
717 	sconfig->src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
718 	sconfig->dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
719 	sconfig->src_maxburst = priv->cfg->max_burst;
720 	sconfig->dst_maxburst = priv->cfg->max_burst;
721 
722 	if (vchan->is_dedicated)
723 		promise = generate_ddma_promise(chan, src, dest, len, sconfig);
724 	else
725 		promise = generate_ndma_promise(chan, src, dest, len, sconfig,
726 						DMA_MEM_TO_MEM);
727 
728 	if (!promise) {
729 		kfree(contract);
730 		return NULL;
731 	}
732 
733 	/* Configure memcpy mode */
734 	if (vchan->is_dedicated) {
735 		promise->cfg |=
736 			SUN4I_DMA_CFG_SRC_DRQ_TYPE(priv->cfg->ddma_drq_sdram) |
737 			SUN4I_DMA_CFG_DST_DRQ_TYPE(priv->cfg->ddma_drq_sdram);
738 	} else {
739 		promise->cfg |=
740 			SUN4I_DMA_CFG_SRC_DRQ_TYPE(priv->cfg->ndma_drq_sdram) |
741 			SUN4I_DMA_CFG_DST_DRQ_TYPE(priv->cfg->ndma_drq_sdram);
742 	}
743 
744 	/* Fill the contract with our only promise */
745 	list_add_tail(&promise->list, &contract->demands);
746 
747 	/* And add it to the vchan */
748 	return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
749 }
750 
751 static struct dma_async_tx_descriptor *
752 sun4i_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf, size_t len,
753 			  size_t period_len, enum dma_transfer_direction dir,
754 			  unsigned long flags)
755 {
756 	struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
757 	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
758 	struct dma_slave_config *sconfig = &vchan->cfg;
759 	struct sun4i_dma_promise *promise;
760 	struct sun4i_dma_contract *contract;
761 	dma_addr_t src, dest;
762 	u32 endpoints;
763 	int nr_periods, offset, plength, i;
764 	u8 ram_type, io_mode, linear_mode;
765 
766 	if (!is_slave_direction(dir)) {
767 		dev_err(dmaengine_chan_dev(chan), "Invalid DMA direction\n");
768 		return NULL;
769 	}
770 
771 	contract = generate_dma_contract();
772 	if (!contract)
773 		return NULL;
774 
775 	contract->is_cyclic = 1;
776 
777 	if (vchan->is_dedicated) {
778 		io_mode = SUN4I_DDMA_ADDR_MODE_IO;
779 		linear_mode = SUN4I_DDMA_ADDR_MODE_LINEAR;
780 		ram_type = priv->cfg->ddma_drq_sdram;
781 	} else {
782 		io_mode = SUN4I_NDMA_ADDR_MODE_IO;
783 		linear_mode = SUN4I_NDMA_ADDR_MODE_LINEAR;
784 		ram_type = priv->cfg->ndma_drq_sdram;
785 	}
786 
787 	if (dir == DMA_MEM_TO_DEV) {
788 		src = buf;
789 		dest = sconfig->dst_addr;
790 		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(vchan->endpoint) |
791 			    SUN4I_DMA_CFG_DST_ADDR_MODE(io_mode) |
792 			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(ram_type) |
793 			    SUN4I_DMA_CFG_SRC_ADDR_MODE(linear_mode);
794 	} else {
795 		src = sconfig->src_addr;
796 		dest = buf;
797 		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(ram_type) |
798 			    SUN4I_DMA_CFG_DST_ADDR_MODE(linear_mode) |
799 			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
800 			    SUN4I_DMA_CFG_SRC_ADDR_MODE(io_mode);
801 	}
802 
803 	/*
804 	 * We will be using half done interrupts to make two periods
805 	 * out of a promise, so we need to program the DMA engine less
806 	 * often
807 	 */
808 
809 	/*
810 	 * The engine can interrupt on half-transfer, so we can use
811 	 * this feature to program the engine half as often as if we
812 	 * didn't use it (keep in mind the hardware doesn't support
813 	 * linked lists).
814 	 *
815 	 * Say you have a set of periods (| marks the start/end, I for
816 	 * interrupt, P for programming the engine to do a new
817 	 * transfer), the easy but slow way would be to do
818 	 *
819 	 *  |---|---|---|---| (periods / promises)
820 	 *  P  I,P I,P I,P  I
821 	 *
822 	 * Using half transfer interrupts you can do
823 	 *
824 	 *  |-------|-------| (promises as configured on hw)
825 	 *  |---|---|---|---| (periods)
826 	 *  P   I  I,P  I   I
827 	 *
828 	 * Which requires half the engine programming for the same
829 	 * functionality.
830 	 *
831 	 * This only works if two periods fit in a single promise. That will
832 	 * always be the case for dedicated DMA, where the hardware has a much
833 	 * larger maximum transfer size than advertised to clients.
834 	 */
835 	if (vchan->is_dedicated || period_len <= SUN4I_NDMA_MAX_SEG_SIZE / 2) {
836 		period_len *= 2;
837 		contract->use_half_int = 1;
838 	}
839 
840 	nr_periods = DIV_ROUND_UP(len, period_len);
841 	for (i = 0; i < nr_periods; i++) {
842 		/* Calculate the offset in the buffer and the length needed */
843 		offset = i * period_len;
844 		plength = min((len - offset), period_len);
845 		if (dir == DMA_MEM_TO_DEV)
846 			src = buf + offset;
847 		else
848 			dest = buf + offset;
849 
850 		/* Make the promise */
851 		if (vchan->is_dedicated)
852 			promise = generate_ddma_promise(chan, src, dest,
853 							plength, sconfig);
854 		else
855 			promise = generate_ndma_promise(chan, src, dest,
856 							plength, sconfig, dir);
857 
858 		if (!promise) {
859 			/* TODO: should we free everything? */
860 			return NULL;
861 		}
862 		promise->cfg |= endpoints;
863 
864 		/* Then add it to the contract */
865 		list_add_tail(&promise->list, &contract->demands);
866 	}
867 
868 	/* And add it to the vchan */
869 	return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
870 }
871 
872 static struct dma_async_tx_descriptor *
873 sun4i_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
874 			unsigned int sg_len, enum dma_transfer_direction dir,
875 			unsigned long flags, void *context)
876 {
877 	struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
878 	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
879 	struct dma_slave_config *sconfig = &vchan->cfg;
880 	struct sun4i_dma_promise *promise;
881 	struct sun4i_dma_contract *contract;
882 	u8 ram_type, io_mode, linear_mode;
883 	struct scatterlist *sg;
884 	dma_addr_t srcaddr, dstaddr;
885 	u32 endpoints, para;
886 	int i;
887 
888 	if (!sgl)
889 		return NULL;
890 
891 	if (!is_slave_direction(dir)) {
892 		dev_err(dmaengine_chan_dev(chan), "Invalid DMA direction\n");
893 		return NULL;
894 	}
895 
896 	contract = generate_dma_contract();
897 	if (!contract)
898 		return NULL;
899 
900 	if (vchan->is_dedicated) {
901 		io_mode = SUN4I_DDMA_ADDR_MODE_IO;
902 		linear_mode = SUN4I_DDMA_ADDR_MODE_LINEAR;
903 		ram_type = priv->cfg->ddma_drq_sdram;
904 	} else {
905 		io_mode = SUN4I_NDMA_ADDR_MODE_IO;
906 		linear_mode = SUN4I_NDMA_ADDR_MODE_LINEAR;
907 		ram_type = priv->cfg->ndma_drq_sdram;
908 	}
909 
910 	if (dir == DMA_MEM_TO_DEV)
911 		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(vchan->endpoint) |
912 			    SUN4I_DMA_CFG_DST_ADDR_MODE(io_mode) |
913 			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(ram_type) |
914 			    SUN4I_DMA_CFG_SRC_ADDR_MODE(linear_mode);
915 	else
916 		endpoints = SUN4I_DMA_CFG_DST_DRQ_TYPE(ram_type) |
917 			    SUN4I_DMA_CFG_DST_ADDR_MODE(linear_mode) |
918 			    SUN4I_DMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
919 			    SUN4I_DMA_CFG_SRC_ADDR_MODE(io_mode);
920 
921 	for_each_sg(sgl, sg, sg_len, i) {
922 		/* Figure out addresses */
923 		if (dir == DMA_MEM_TO_DEV) {
924 			srcaddr = sg_dma_address(sg);
925 			dstaddr = sconfig->dst_addr;
926 		} else {
927 			srcaddr = sconfig->src_addr;
928 			dstaddr = sg_dma_address(sg);
929 		}
930 
931 		/*
932 		 * These are the magic DMA engine timings that keep SPI going.
933 		 * I haven't seen any interface on DMAEngine to configure
934 		 * timings, and so far they seem to work for everything we
935 		 * support, so I've kept them here. I don't know if other
936 		 * devices need different timings because, as usual, we only
937 		 * have the "para" bitfield meanings, but no comment on what
938 		 * the values should be when doing a certain operation :|
939 		 */
940 		para = SUN4I_DDMA_MAGIC_SPI_PARAMETERS;
941 
942 		/* And make a suitable promise */
943 		if (vchan->is_dedicated)
944 			promise = generate_ddma_promise(chan, srcaddr, dstaddr,
945 							sg_dma_len(sg),
946 							sconfig);
947 		else
948 			promise = generate_ndma_promise(chan, srcaddr, dstaddr,
949 							sg_dma_len(sg),
950 							sconfig, dir);
951 
952 		if (!promise)
953 			return NULL; /* TODO: should we free everything? */
954 
955 		promise->cfg |= endpoints;
956 		promise->para = para;
957 
958 		/* Then add it to the contract */
959 		list_add_tail(&promise->list, &contract->demands);
960 	}
961 
962 	/*
963 	 * Once we've got all the promises ready, add the contract
964 	 * to the pending list on the vchan
965 	 */
966 	return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
967 }
968 
969 static int sun4i_dma_terminate_all(struct dma_chan *chan)
970 {
971 	struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
972 	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
973 	struct sun4i_dma_pchan *pchan = vchan->pchan;
974 	LIST_HEAD(head);
975 	unsigned long flags;
976 
977 	spin_lock_irqsave(&vchan->vc.lock, flags);
978 	vchan_get_all_descriptors(&vchan->vc, &head);
979 	spin_unlock_irqrestore(&vchan->vc.lock, flags);
980 
981 	/*
982 	 * Clearing the configuration register will halt the pchan. Interrupts
983 	 * may still trigger, so don't forget to disable them.
984 	 */
985 	if (pchan) {
986 		if (pchan->is_dedicated)
987 			writel(0, pchan->base + SUN4I_DDMA_CFG_REG);
988 		else
989 			writel(0, pchan->base + SUN4I_NDMA_CFG_REG);
990 		set_pchan_interrupt(priv, pchan, 0, 0);
991 		release_pchan(priv, pchan);
992 	}
993 
994 	spin_lock_irqsave(&vchan->vc.lock, flags);
995 	/* Clear these so the vchan is usable again */
996 	vchan->processing = NULL;
997 	vchan->pchan = NULL;
998 	spin_unlock_irqrestore(&vchan->vc.lock, flags);
999 
1000 	vchan_dma_desc_free_list(&vchan->vc, &head);
1001 
1002 	return 0;
1003 }
1004 
1005 static int sun4i_dma_config(struct dma_chan *chan,
1006 			    struct dma_slave_config *config)
1007 {
1008 	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
1009 
1010 	memcpy(&vchan->cfg, config, sizeof(*config));
1011 
1012 	return 0;
1013 }
1014 
1015 static struct dma_chan *sun4i_dma_of_xlate(struct of_phandle_args *dma_spec,
1016 					   struct of_dma *ofdma)
1017 {
1018 	struct sun4i_dma_dev *priv = ofdma->of_dma_data;
1019 	struct sun4i_dma_vchan *vchan;
1020 	struct dma_chan *chan;
1021 	u8 is_dedicated = dma_spec->args[0];
1022 	u8 endpoint = dma_spec->args[1];
1023 
1024 	/* Check if type is Normal or Dedicated */
1025 	if (is_dedicated != 0 && is_dedicated != 1)
1026 		return NULL;
1027 
1028 	/* Make sure the endpoint looks sane */
1029 	if ((is_dedicated && endpoint >= SUN4I_DDMA_DRQ_TYPE_LIMIT) ||
1030 	    (!is_dedicated && endpoint >= SUN4I_NDMA_DRQ_TYPE_LIMIT))
1031 		return NULL;
1032 
1033 	chan = dma_get_any_slave_channel(&priv->slave);
1034 	if (!chan)
1035 		return NULL;
1036 
1037 	/* Assign the endpoint to the vchan */
1038 	vchan = to_sun4i_dma_vchan(chan);
1039 	vchan->is_dedicated = is_dedicated;
1040 	vchan->endpoint = endpoint;
1041 
1042 	return chan;
1043 }
1044 
1045 static enum dma_status sun4i_dma_tx_status(struct dma_chan *chan,
1046 					   dma_cookie_t cookie,
1047 					   struct dma_tx_state *state)
1048 {
1049 	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
1050 	struct sun4i_dma_pchan *pchan = vchan->pchan;
1051 	struct sun4i_dma_contract *contract;
1052 	struct sun4i_dma_promise *promise;
1053 	struct virt_dma_desc *vd;
1054 	unsigned long flags;
1055 	enum dma_status ret;
1056 	size_t bytes = 0;
1057 
1058 	ret = dma_cookie_status(chan, cookie, state);
1059 	if (!state || (ret == DMA_COMPLETE))
1060 		return ret;
1061 
1062 	spin_lock_irqsave(&vchan->vc.lock, flags);
1063 	vd = vchan_find_desc(&vchan->vc, cookie);
1064 	if (!vd)
1065 		goto exit;
1066 	contract = to_sun4i_dma_contract(vd);
1067 
1068 	list_for_each_entry(promise, &contract->demands, list)
1069 		bytes += promise->len;
1070 
1071 	/*
1072 	 * The hardware is configured to return the remaining byte
1073 	 * quantity. If possible, replace the first listed element's
1074 	 * full size with the actual remaining amount
1075 	 */
1076 	promise = list_first_entry_or_null(&contract->demands,
1077 					   struct sun4i_dma_promise, list);
1078 	if (promise && pchan) {
1079 		bytes -= promise->len;
1080 		if (pchan->is_dedicated)
1081 			bytes += readl(pchan->base + SUN4I_DDMA_BYTE_COUNT_REG);
1082 		else
1083 			bytes += readl(pchan->base + SUN4I_NDMA_BYTE_COUNT_REG);
1084 	}
1085 
1086 exit:
1087 
1088 	dma_set_residue(state, bytes);
1089 	spin_unlock_irqrestore(&vchan->vc.lock, flags);
1090 
1091 	return ret;
1092 }
1093 
1094 static void sun4i_dma_issue_pending(struct dma_chan *chan)
1095 {
1096 	struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
1097 	struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
1098 	unsigned long flags;
1099 
1100 	spin_lock_irqsave(&vchan->vc.lock, flags);
1101 
1102 	/*
1103 	 * If there are pending transactions for this vchan, push one of
1104 	 * them into the engine to get the ball rolling.
1105 	 */
1106 	if (vchan_issue_pending(&vchan->vc))
1107 		__execute_vchan_pending(priv, vchan);
1108 
1109 	spin_unlock_irqrestore(&vchan->vc.lock, flags);
1110 }
1111 
1112 static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)
1113 {
1114 	struct sun4i_dma_dev *priv = dev_id;
1115 	struct sun4i_dma_pchan *pchans = priv->pchans, *pchan;
1116 	struct sun4i_dma_vchan *vchan;
1117 	struct sun4i_dma_contract *contract;
1118 	struct sun4i_dma_promise *promise;
1119 	unsigned long pendirq, irqs, disableirqs;
1120 	int bit, i, free_room, allow_mitigation = 1;
1121 
1122 	pendirq = readl_relaxed(priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
1123 
1124 handle_pending:
1125 
1126 	disableirqs = 0;
1127 	free_room = 0;
1128 
1129 	for_each_set_bit(bit, &pendirq, 32) {
1130 		pchan = &pchans[bit >> 1];
1131 		vchan = pchan->vchan;
1132 		if (!vchan) /* a terminated channel may still interrupt */
1133 			continue;
1134 		contract = vchan->contract;
1135 
1136 		/*
1137 		 * Disable the IRQ and free the pchan if it's an end
1138 		 * interrupt (odd bit)
1139 		 */
1140 		if (bit & 1) {
1141 			spin_lock(&vchan->vc.lock);
1142 
1143 			/*
1144 			 * Move the promise into the completed list now that
1145 			 * we're done with it
1146 			 */
1147 			list_move_tail(&vchan->processing->list,
1148 				       &contract->completed_demands);
1149 
1150 			/*
1151 			 * Cyclic DMA transfers are special:
1152 			 * - There's always something we can dispatch
1153 			 * - We need to run the callback
1154 			 * - Latency is very important, as this is used by audio
1155 			 * We therefore just cycle through the list and dispatch
1156 			 * whatever we have here, reusing the pchan. There's
1157 			 * no need to run the thread after this.
1158 			 *
1159 			 * For non-cyclic transfers we need to look around,
1160 			 * so we can program some more work, or notify the
1161 			 * client that their transfers have been completed.
1162 			 */
1163 			if (contract->is_cyclic) {
1164 				promise = get_next_cyclic_promise(contract);
1165 				vchan->processing = promise;
1166 				configure_pchan(pchan, promise);
1167 				vchan_cyclic_callback(&contract->vd);
1168 			} else {
1169 				vchan->processing = NULL;
1170 				vchan->pchan = NULL;
1171 
1172 				free_room = 1;
1173 				disableirqs |= BIT(bit);
1174 				release_pchan(priv, pchan);
1175 			}
1176 
1177 			spin_unlock(&vchan->vc.lock);
1178 		} else {
1179 			/* Half done interrupt */
1180 			if (contract->is_cyclic)
1181 				vchan_cyclic_callback(&contract->vd);
1182 			else
1183 				disableirqs |= BIT(bit);
1184 		}
1185 	}
1186 
1187 	/* Disable the IRQs for events we handled */
1188 	spin_lock(&priv->lock);
1189 	irqs = readl_relaxed(priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
1190 	writel_relaxed(irqs & ~disableirqs,
1191 		       priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
1192 	spin_unlock(&priv->lock);
1193 
1194 	/* Writing 1 to the pending field will clear the pending interrupt */
1195 	writel_relaxed(pendirq, priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
1196 
1197 	/*
1198 	 * If a pchan was freed, we may be able to schedule something else,
1199 	 * so have a look around
1200 	 */
1201 	if (free_room) {
1202 		for (i = 0; i < SUN4I_DMA_NR_MAX_VCHANS; i++) {
1203 			vchan = &priv->vchans[i];
1204 			spin_lock(&vchan->vc.lock);
1205 			__execute_vchan_pending(priv, vchan);
1206 			spin_unlock(&vchan->vc.lock);
1207 		}
1208 	}
1209 
1210 	/*
1211 	 * Handle newer interrupts if some showed up, but only do it once
1212 	 * to avoid a too long a loop
1213 	 */
1214 	if (allow_mitigation) {
1215 		pendirq = readl_relaxed(priv->base +
1216 					SUN4I_DMA_IRQ_PENDING_STATUS_REG);
1217 		if (pendirq) {
1218 			allow_mitigation = 0;
1219 			goto handle_pending;
1220 		}
1221 	}
1222 
1223 	return IRQ_HANDLED;
1224 }
1225 
1226 static int sun4i_dma_probe(struct platform_device *pdev)
1227 {
1228 	struct sun4i_dma_dev *priv;
1229 	int i, j, ret;
1230 
1231 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1232 	if (!priv)
1233 		return -ENOMEM;
1234 
1235 	priv->cfg = of_device_get_match_data(&pdev->dev);
1236 	if (!priv->cfg)
1237 		return -ENODEV;
1238 
1239 	priv->base = devm_platform_ioremap_resource(pdev, 0);
1240 	if (IS_ERR(priv->base))
1241 		return PTR_ERR(priv->base);
1242 
1243 	priv->irq = platform_get_irq(pdev, 0);
1244 	if (priv->irq < 0)
1245 		return priv->irq;
1246 
1247 	priv->clk = devm_clk_get_enabled(&pdev->dev, NULL);
1248 	if (IS_ERR(priv->clk))
1249 		return dev_err_probe(&pdev->dev, PTR_ERR(priv->clk),
1250 				     "Couldn't start the clock\n");
1251 
1252 	if (priv->cfg->has_reset) {
1253 		priv->rst = devm_reset_control_get_exclusive_deasserted(&pdev->dev, NULL);
1254 		if (IS_ERR(priv->rst))
1255 			return dev_err_probe(&pdev->dev, PTR_ERR(priv->rst),
1256 					     "Failed to get reset control\n");
1257 	}
1258 
1259 	platform_set_drvdata(pdev, priv);
1260 	spin_lock_init(&priv->lock);
1261 
1262 	dma_set_max_seg_size(&pdev->dev, SUN4I_DMA_MAX_SEG_SIZE);
1263 
1264 	dma_cap_zero(priv->slave.cap_mask);
1265 	dma_cap_set(DMA_PRIVATE, priv->slave.cap_mask);
1266 	dma_cap_set(DMA_MEMCPY, priv->slave.cap_mask);
1267 	dma_cap_set(DMA_CYCLIC, priv->slave.cap_mask);
1268 	dma_cap_set(DMA_SLAVE, priv->slave.cap_mask);
1269 
1270 	INIT_LIST_HEAD(&priv->slave.channels);
1271 	priv->slave.device_free_chan_resources	= sun4i_dma_free_chan_resources;
1272 	priv->slave.device_tx_status		= sun4i_dma_tx_status;
1273 	priv->slave.device_issue_pending	= sun4i_dma_issue_pending;
1274 	priv->slave.device_prep_slave_sg	= sun4i_dma_prep_slave_sg;
1275 	priv->slave.device_prep_dma_memcpy	= sun4i_dma_prep_dma_memcpy;
1276 	priv->slave.device_prep_dma_cyclic	= sun4i_dma_prep_dma_cyclic;
1277 	priv->slave.device_config		= sun4i_dma_config;
1278 	priv->slave.device_terminate_all	= sun4i_dma_terminate_all;
1279 	priv->slave.copy_align			= 2;
1280 	priv->slave.src_addr_widths		= BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1281 						  BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1282 						  BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1283 	priv->slave.dst_addr_widths		= BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1284 						  BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1285 						  BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1286 	priv->slave.directions			= BIT(DMA_DEV_TO_MEM) |
1287 						  BIT(DMA_MEM_TO_DEV);
1288 	priv->slave.residue_granularity		= DMA_RESIDUE_GRANULARITY_BURST;
1289 
1290 	priv->slave.dev = &pdev->dev;
1291 
1292 	priv->pchans = devm_kcalloc(&pdev->dev, priv->cfg->dma_nr_max_channels,
1293 				    sizeof(struct sun4i_dma_pchan), GFP_KERNEL);
1294 	priv->vchans = devm_kcalloc(&pdev->dev, SUN4I_DMA_NR_MAX_VCHANS,
1295 				    sizeof(struct sun4i_dma_vchan), GFP_KERNEL);
1296 	priv->pchans_used = devm_kcalloc(&pdev->dev,
1297 					 BITS_TO_LONGS(priv->cfg->dma_nr_max_channels),
1298 					 sizeof(unsigned long), GFP_KERNEL);
1299 	if (!priv->vchans || !priv->pchans || !priv->pchans_used)
1300 		return -ENOMEM;
1301 
1302 	/*
1303 	 * [0..priv->cfg->ndma_nr_max_channels) are normal pchans, and
1304 	 * [priv->cfg->ndma_nr_max_channels..priv->cfg->dma_nr_max_channels) are
1305 	 * dedicated ones
1306 	 */
1307 	for (i = 0; i < priv->cfg->ndma_nr_max_channels; i++)
1308 		priv->pchans[i].base = priv->base +
1309 			SUN4I_NDMA_CHANNEL_REG_BASE(i);
1310 
1311 	for (j = 0; i < priv->cfg->dma_nr_max_channels; i++, j++) {
1312 		priv->pchans[i].base = priv->base +
1313 			SUN4I_DDMA_CHANNEL_REG_BASE(j);
1314 		priv->pchans[i].is_dedicated = 1;
1315 	}
1316 
1317 	for (i = 0; i < SUN4I_DMA_NR_MAX_VCHANS; i++) {
1318 		struct sun4i_dma_vchan *vchan = &priv->vchans[i];
1319 
1320 		spin_lock_init(&vchan->vc.lock);
1321 		vchan->vc.desc_free = sun4i_dma_free_contract;
1322 		vchan_init(&vchan->vc, &priv->slave);
1323 	}
1324 
1325 	/*
1326 	 * Make sure the IRQs are all disabled and accounted for. The bootloader
1327 	 * likes to leave these dirty
1328 	 */
1329 	writel(0, priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
1330 	writel(0xFFFFFFFF, priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
1331 
1332 	ret = devm_request_irq(&pdev->dev, priv->irq, sun4i_dma_interrupt,
1333 			       0, dev_name(&pdev->dev), priv);
1334 	if (ret)
1335 		return dev_err_probe(&pdev->dev, ret, "Cannot request IRQ\n");
1336 
1337 	ret = dmaenginem_async_device_register(&priv->slave);
1338 	if (ret)
1339 		return dev_err_probe(&pdev->dev, ret,
1340 				     "Failed to register DMA engine device\n");
1341 
1342 	ret = of_dma_controller_register(pdev->dev.of_node, sun4i_dma_of_xlate,
1343 					 priv);
1344 	if (ret)
1345 		return dev_err_probe(&pdev->dev, ret,
1346 				     "Failed to register translation function\n");
1347 
1348 	dev_dbg(&pdev->dev, "Successfully probed SUN4I_DMA\n");
1349 
1350 	return 0;
1351 }
1352 
1353 static void sun4i_dma_remove(struct platform_device *pdev)
1354 {
1355 	struct sun4i_dma_dev *priv = platform_get_drvdata(pdev);
1356 
1357 	/* Disable IRQ so no more work is scheduled */
1358 	disable_irq(priv->irq);
1359 
1360 	of_dma_controller_free(pdev->dev.of_node);
1361 }
1362 
1363 static struct sun4i_dma_config sun4i_a10_dma_cfg = {
1364 	.ndma_nr_max_channels	= SUN4I_NDMA_NR_MAX_CHANNELS,
1365 	.ndma_nr_max_vchans	= SUN4I_NDMA_NR_MAX_VCHANS,
1366 
1367 	.ddma_nr_max_channels	= SUN4I_DDMA_NR_MAX_CHANNELS,
1368 	.ddma_nr_max_vchans	= SUN4I_DDMA_NR_MAX_VCHANS,
1369 
1370 	.dma_nr_max_channels	= SUN4I_DMA_NR_MAX_CHANNELS,
1371 
1372 	.set_dst_data_width	= set_dst_data_width_a10,
1373 	.set_src_data_width	= set_src_data_width_a10,
1374 	.convert_burst		= convert_burst_a10,
1375 
1376 	.ndma_drq_sdram		= SUN4I_NDMA_DRQ_TYPE_SDRAM,
1377 	.ddma_drq_sdram		= SUN4I_DDMA_DRQ_TYPE_SDRAM,
1378 
1379 	.max_burst		= SUN4I_MAX_BURST,
1380 	.has_reset		= false,
1381 };
1382 
1383 static struct sun4i_dma_config suniv_f1c100s_dma_cfg = {
1384 	.ndma_nr_max_channels	= SUNIV_NDMA_NR_MAX_CHANNELS,
1385 	.ndma_nr_max_vchans	= SUNIV_NDMA_NR_MAX_VCHANS,
1386 
1387 	.ddma_nr_max_channels	= SUNIV_DDMA_NR_MAX_CHANNELS,
1388 	.ddma_nr_max_vchans	= SUNIV_DDMA_NR_MAX_VCHANS,
1389 
1390 	.dma_nr_max_channels	= SUNIV_NDMA_NR_MAX_CHANNELS +
1391 		SUNIV_DDMA_NR_MAX_CHANNELS,
1392 
1393 	.set_dst_data_width	= set_dst_data_width_f1c100s,
1394 	.set_src_data_width	= set_src_data_width_f1c100s,
1395 	.convert_burst		= convert_burst_f1c100s,
1396 
1397 	.ndma_drq_sdram		= SUNIV_NDMA_DRQ_TYPE_SDRAM,
1398 	.ddma_drq_sdram		= SUNIV_DDMA_DRQ_TYPE_SDRAM,
1399 
1400 	.max_burst		= SUNIV_MAX_BURST,
1401 	.has_reset		= true,
1402 };
1403 
1404 static const struct of_device_id sun4i_dma_match[] = {
1405 	{ .compatible = "allwinner,sun4i-a10-dma", .data = &sun4i_a10_dma_cfg },
1406 	{ .compatible = "allwinner,suniv-f1c100s-dma",
1407 		.data = &suniv_f1c100s_dma_cfg },
1408 	{ /* sentinel */ },
1409 };
1410 MODULE_DEVICE_TABLE(of, sun4i_dma_match);
1411 
1412 static struct platform_driver sun4i_dma_driver = {
1413 	.probe	= sun4i_dma_probe,
1414 	.remove = sun4i_dma_remove,
1415 	.driver	= {
1416 		.name		= "sun4i-dma",
1417 		.of_match_table	= sun4i_dma_match,
1418 	},
1419 };
1420 
1421 module_platform_driver(sun4i_dma_driver);
1422 
1423 MODULE_DESCRIPTION("Allwinner A10 Dedicated DMA Controller Driver");
1424 MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
1425 MODULE_LICENSE("GPL");
1426