xref: /linux/drivers/dma/owl-dma.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Actions Semi Owl SoCs DMA driver
4 //
5 // Copyright (c) 2014 Actions Semi Inc.
6 // Author: David Liu <liuwei@actions-semi.com>
7 //
8 // Copyright (c) 2018 Linaro Ltd.
9 // Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
10 
11 #include <linux/bitops.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/dmaengine.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/dmapool.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/mm.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/of_dma.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include "virt-dma.h"
28 
29 #define OWL_DMA_FRAME_MAX_LENGTH		0xfffff
30 
31 /* Global DMA Controller Registers */
32 #define OWL_DMA_IRQ_PD0				0x00
33 #define OWL_DMA_IRQ_PD1				0x04
34 #define OWL_DMA_IRQ_PD2				0x08
35 #define OWL_DMA_IRQ_PD3				0x0C
36 #define OWL_DMA_IRQ_EN0				0x10
37 #define OWL_DMA_IRQ_EN1				0x14
38 #define OWL_DMA_IRQ_EN2				0x18
39 #define OWL_DMA_IRQ_EN3				0x1C
40 #define OWL_DMA_SECURE_ACCESS_CTL		0x20
41 #define OWL_DMA_NIC_QOS				0x24
42 #define OWL_DMA_DBGSEL				0x28
43 #define OWL_DMA_IDLE_STAT			0x2C
44 
45 /* Channel Registers */
46 #define OWL_DMA_CHAN_BASE(i)			(0x100 + (i) * 0x100)
47 #define OWL_DMAX_MODE				0x00
48 #define OWL_DMAX_SOURCE				0x04
49 #define OWL_DMAX_DESTINATION			0x08
50 #define OWL_DMAX_FRAME_LEN			0x0C
51 #define OWL_DMAX_FRAME_CNT			0x10
52 #define OWL_DMAX_REMAIN_FRAME_CNT		0x14
53 #define OWL_DMAX_REMAIN_CNT			0x18
54 #define OWL_DMAX_SOURCE_STRIDE			0x1C
55 #define OWL_DMAX_DESTINATION_STRIDE		0x20
56 #define OWL_DMAX_START				0x24
57 #define OWL_DMAX_PAUSE				0x28
58 #define OWL_DMAX_CHAINED_CTL			0x2C
59 #define OWL_DMAX_CONSTANT			0x30
60 #define OWL_DMAX_LINKLIST_CTL			0x34
61 #define OWL_DMAX_NEXT_DESCRIPTOR		0x38
62 #define OWL_DMAX_CURRENT_DESCRIPTOR_NUM		0x3C
63 #define OWL_DMAX_INT_CTL			0x40
64 #define OWL_DMAX_INT_STATUS			0x44
65 #define OWL_DMAX_CURRENT_SOURCE_POINTER		0x48
66 #define OWL_DMAX_CURRENT_DESTINATION_POINTER	0x4C
67 
68 /* OWL_DMAX_MODE Bits */
69 #define OWL_DMA_MODE_TS(x)			(((x) & GENMASK(5, 0)) << 0)
70 #define OWL_DMA_MODE_ST(x)			(((x) & GENMASK(1, 0)) << 8)
71 #define	OWL_DMA_MODE_ST_DEV			OWL_DMA_MODE_ST(0)
72 #define	OWL_DMA_MODE_ST_DCU			OWL_DMA_MODE_ST(2)
73 #define	OWL_DMA_MODE_ST_SRAM			OWL_DMA_MODE_ST(3)
74 #define OWL_DMA_MODE_DT(x)			(((x) & GENMASK(1, 0)) << 10)
75 #define	OWL_DMA_MODE_DT_DEV			OWL_DMA_MODE_DT(0)
76 #define	OWL_DMA_MODE_DT_DCU			OWL_DMA_MODE_DT(2)
77 #define	OWL_DMA_MODE_DT_SRAM			OWL_DMA_MODE_DT(3)
78 #define OWL_DMA_MODE_SAM(x)			(((x) & GENMASK(1, 0)) << 16)
79 #define	OWL_DMA_MODE_SAM_CONST			OWL_DMA_MODE_SAM(0)
80 #define	OWL_DMA_MODE_SAM_INC			OWL_DMA_MODE_SAM(1)
81 #define	OWL_DMA_MODE_SAM_STRIDE			OWL_DMA_MODE_SAM(2)
82 #define OWL_DMA_MODE_DAM(x)			(((x) & GENMASK(1, 0)) << 18)
83 #define	OWL_DMA_MODE_DAM_CONST			OWL_DMA_MODE_DAM(0)
84 #define	OWL_DMA_MODE_DAM_INC			OWL_DMA_MODE_DAM(1)
85 #define	OWL_DMA_MODE_DAM_STRIDE			OWL_DMA_MODE_DAM(2)
86 #define OWL_DMA_MODE_PW(x)			(((x) & GENMASK(2, 0)) << 20)
87 #define OWL_DMA_MODE_CB				BIT(23)
88 #define OWL_DMA_MODE_NDDBW(x)			(((x) & 0x1) << 28)
89 #define	OWL_DMA_MODE_NDDBW_32BIT		OWL_DMA_MODE_NDDBW(0)
90 #define	OWL_DMA_MODE_NDDBW_8BIT			OWL_DMA_MODE_NDDBW(1)
91 #define OWL_DMA_MODE_CFE			BIT(29)
92 #define OWL_DMA_MODE_LME			BIT(30)
93 #define OWL_DMA_MODE_CME			BIT(31)
94 
95 /* OWL_DMAX_LINKLIST_CTL Bits */
96 #define OWL_DMA_LLC_SAV(x)			(((x) & GENMASK(1, 0)) << 8)
97 #define	OWL_DMA_LLC_SAV_INC			OWL_DMA_LLC_SAV(0)
98 #define	OWL_DMA_LLC_SAV_LOAD_NEXT		OWL_DMA_LLC_SAV(1)
99 #define	OWL_DMA_LLC_SAV_LOAD_PREV		OWL_DMA_LLC_SAV(2)
100 #define OWL_DMA_LLC_DAV(x)			(((x) & GENMASK(1, 0)) << 10)
101 #define	OWL_DMA_LLC_DAV_INC			OWL_DMA_LLC_DAV(0)
102 #define	OWL_DMA_LLC_DAV_LOAD_NEXT		OWL_DMA_LLC_DAV(1)
103 #define	OWL_DMA_LLC_DAV_LOAD_PREV		OWL_DMA_LLC_DAV(2)
104 #define OWL_DMA_LLC_SUSPEND			BIT(16)
105 
106 /* OWL_DMAX_INT_CTL Bits */
107 #define OWL_DMA_INTCTL_BLOCK			BIT(0)
108 #define OWL_DMA_INTCTL_SUPER_BLOCK		BIT(1)
109 #define OWL_DMA_INTCTL_FRAME			BIT(2)
110 #define OWL_DMA_INTCTL_HALF_FRAME		BIT(3)
111 #define OWL_DMA_INTCTL_LAST_FRAME		BIT(4)
112 
113 /* OWL_DMAX_INT_STATUS Bits */
114 #define OWL_DMA_INTSTAT_BLOCK			BIT(0)
115 #define OWL_DMA_INTSTAT_SUPER_BLOCK		BIT(1)
116 #define OWL_DMA_INTSTAT_FRAME			BIT(2)
117 #define OWL_DMA_INTSTAT_HALF_FRAME		BIT(3)
118 #define OWL_DMA_INTSTAT_LAST_FRAME		BIT(4)
119 
120 /* Pack shift and newshift in a single word */
121 #define BIT_FIELD(val, width, shift, newshift)	\
122 		((((val) >> (shift)) & ((BIT(width)) - 1)) << (newshift))
123 
124 /* Frame count value is fixed as 1 */
125 #define FCNT_VAL				0x1
126 
127 /**
128  * enum owl_dmadesc_offsets - Describe DMA descriptor, hardware link
129  * list for dma transfer
130  * @OWL_DMADESC_NEXT_LLI: physical address of the next link list
131  * @OWL_DMADESC_SADDR: source physical address
132  * @OWL_DMADESC_DADDR: destination physical address
133  * @OWL_DMADESC_FLEN: frame length
134  * @OWL_DMADESC_SRC_STRIDE: source stride
135  * @OWL_DMADESC_DST_STRIDE: destination stride
136  * @OWL_DMADESC_CTRLA: dma_mode and linklist ctrl config
137  * @OWL_DMADESC_CTRLB: interrupt config
138  * @OWL_DMADESC_CONST_NUM: data for constant fill
139  * @OWL_DMADESC_SIZE: max size of this enum
140  */
141 enum owl_dmadesc_offsets {
142 	OWL_DMADESC_NEXT_LLI = 0,
143 	OWL_DMADESC_SADDR,
144 	OWL_DMADESC_DADDR,
145 	OWL_DMADESC_FLEN,
146 	OWL_DMADESC_SRC_STRIDE,
147 	OWL_DMADESC_DST_STRIDE,
148 	OWL_DMADESC_CTRLA,
149 	OWL_DMADESC_CTRLB,
150 	OWL_DMADESC_CONST_NUM,
151 	OWL_DMADESC_SIZE
152 };
153 
154 enum owl_dma_id {
155 	S900_DMA,
156 	S700_DMA,
157 };
158 
159 /**
160  * struct owl_dma_lli - Link list for dma transfer
161  * @hw: hardware link list
162  * @phys: physical address of hardware link list
163  * @node: node for txd's lli_list
164  */
165 struct owl_dma_lli {
166 	u32			hw[OWL_DMADESC_SIZE];
167 	dma_addr_t		phys;
168 	struct list_head	node;
169 };
170 
171 /**
172  * struct owl_dma_txd - Wrapper for struct dma_async_tx_descriptor
173  * @vd: virtual DMA descriptor
174  * @lli_list: link list of lli nodes
175  * @cyclic: flag to indicate cyclic transfers
176  */
177 struct owl_dma_txd {
178 	struct virt_dma_desc	vd;
179 	struct list_head	lli_list;
180 	bool			cyclic;
181 };
182 
183 /**
184  * struct owl_dma_pchan - Holder for the physical channels
185  * @id: physical index to this channel
186  * @base: virtual memory base for the dma channel
187  * @vchan: the virtual channel currently being served by this physical channel
188  */
189 struct owl_dma_pchan {
190 	u32			id;
191 	void __iomem		*base;
192 	struct owl_dma_vchan	*vchan;
193 };
194 
195 /**
196  * struct owl_dma_vchan - Wrapper for DMA ENGINE channel
197  * @vc: wrapped virtual channel
198  * @pchan: the physical channel utilized by this channel
199  * @txd: active transaction on this channel
200  * @cfg: slave configuration for this channel
201  * @drq: physical DMA request ID for this channel
202  */
203 struct owl_dma_vchan {
204 	struct virt_dma_chan	vc;
205 	struct owl_dma_pchan	*pchan;
206 	struct owl_dma_txd	*txd;
207 	struct dma_slave_config cfg;
208 	u8			drq;
209 };
210 
211 /**
212  * struct owl_dma - Holder for the Owl DMA controller
213  * @dma: dma engine for this instance
214  * @base: virtual memory base for the DMA controller
215  * @clk: clock for the DMA controller
216  * @lock: a lock to use when change DMA controller global register
217  * @lli_pool: a pool for the LLI descriptors
218  * @irq: interrupt ID for the DMA controller
219  * @nr_pchans: the number of physical channels
220  * @pchans: array of data for the physical channels
221  * @nr_vchans: the number of physical channels
222  * @vchans: array of data for the physical channels
223  * @devid: device id based on OWL SoC
224  */
225 struct owl_dma {
226 	struct dma_device	dma;
227 	void __iomem		*base;
228 	struct clk		*clk;
229 	spinlock_t		lock;
230 	struct dma_pool		*lli_pool;
231 	int			irq;
232 
233 	unsigned int		nr_pchans;
234 	struct owl_dma_pchan	*pchans;
235 
236 	unsigned int		nr_vchans;
237 	struct owl_dma_vchan	*vchans;
238 	enum owl_dma_id		devid;
239 };
240 
241 static void pchan_update(struct owl_dma_pchan *pchan, u32 reg,
242 			 u32 val, bool state)
243 {
244 	u32 regval;
245 
246 	regval = readl(pchan->base + reg);
247 
248 	if (state)
249 		regval |= val;
250 	else
251 		regval &= ~val;
252 
253 	writel(regval, pchan->base + reg);
254 }
255 
256 static void pchan_writel(struct owl_dma_pchan *pchan, u32 reg, u32 data)
257 {
258 	writel(data, pchan->base + reg);
259 }
260 
261 static u32 pchan_readl(struct owl_dma_pchan *pchan, u32 reg)
262 {
263 	return readl(pchan->base + reg);
264 }
265 
266 static void dma_update(struct owl_dma *od, u32 reg, u32 val, bool state)
267 {
268 	u32 regval;
269 
270 	regval = readl(od->base + reg);
271 
272 	if (state)
273 		regval |= val;
274 	else
275 		regval &= ~val;
276 
277 	writel(regval, od->base + reg);
278 }
279 
280 static void dma_writel(struct owl_dma *od, u32 reg, u32 data)
281 {
282 	writel(data, od->base + reg);
283 }
284 
285 static u32 dma_readl(struct owl_dma *od, u32 reg)
286 {
287 	return readl(od->base + reg);
288 }
289 
290 static inline struct owl_dma *to_owl_dma(struct dma_device *dd)
291 {
292 	return container_of(dd, struct owl_dma, dma);
293 }
294 
295 static inline struct owl_dma_vchan *to_owl_vchan(struct dma_chan *chan)
296 {
297 	return container_of(chan, struct owl_dma_vchan, vc.chan);
298 }
299 
300 static inline struct owl_dma_txd *to_owl_txd(struct dma_async_tx_descriptor *tx)
301 {
302 	return container_of(tx, struct owl_dma_txd, vd.tx);
303 }
304 
305 static inline u32 llc_hw_ctrla(u32 mode, u32 llc_ctl)
306 {
307 	u32 ctl;
308 
309 	ctl = BIT_FIELD(mode, 4, 28, 28) |
310 	      BIT_FIELD(mode, 8, 16, 20) |
311 	      BIT_FIELD(mode, 4, 8, 16) |
312 	      BIT_FIELD(mode, 6, 0, 10) |
313 	      BIT_FIELD(llc_ctl, 2, 10, 8) |
314 	      BIT_FIELD(llc_ctl, 2, 8, 6);
315 
316 	return ctl;
317 }
318 
319 static inline u32 llc_hw_ctrlb(u32 int_ctl)
320 {
321 	u32 ctl;
322 
323 	/*
324 	 * Irrespective of the SoC, ctrlb value starts filling from
325 	 * bit 18.
326 	 */
327 	ctl = BIT_FIELD(int_ctl, 7, 0, 18);
328 
329 	return ctl;
330 }
331 
332 static u32 llc_hw_flen(struct owl_dma_lli *lli)
333 {
334 	return lli->hw[OWL_DMADESC_FLEN] & GENMASK(19, 0);
335 }
336 
337 static void owl_dma_free_lli(struct owl_dma *od,
338 			     struct owl_dma_lli *lli)
339 {
340 	list_del(&lli->node);
341 	dma_pool_free(od->lli_pool, lli, lli->phys);
342 }
343 
344 static struct owl_dma_lli *owl_dma_alloc_lli(struct owl_dma *od)
345 {
346 	struct owl_dma_lli *lli;
347 	dma_addr_t phys;
348 
349 	lli = dma_pool_alloc(od->lli_pool, GFP_NOWAIT, &phys);
350 	if (!lli)
351 		return NULL;
352 
353 	INIT_LIST_HEAD(&lli->node);
354 	lli->phys = phys;
355 
356 	return lli;
357 }
358 
359 static struct owl_dma_lli *owl_dma_add_lli(struct owl_dma_txd *txd,
360 					   struct owl_dma_lli *prev,
361 					   struct owl_dma_lli *next,
362 					   bool is_cyclic)
363 {
364 	if (!is_cyclic)
365 		list_add_tail(&next->node, &txd->lli_list);
366 
367 	if (prev) {
368 		prev->hw[OWL_DMADESC_NEXT_LLI] = next->phys;
369 		prev->hw[OWL_DMADESC_CTRLA] |=
370 					llc_hw_ctrla(OWL_DMA_MODE_LME, 0);
371 	}
372 
373 	return next;
374 }
375 
376 static inline int owl_dma_cfg_lli(struct owl_dma_vchan *vchan,
377 				  struct owl_dma_lli *lli,
378 				  dma_addr_t src, dma_addr_t dst,
379 				  u32 len, enum dma_transfer_direction dir,
380 				  struct dma_slave_config *sconfig,
381 				  bool is_cyclic)
382 {
383 	struct owl_dma *od = to_owl_dma(vchan->vc.chan.device);
384 	u32 mode, ctrlb;
385 
386 	mode = OWL_DMA_MODE_PW(0);
387 
388 	switch (dir) {
389 	case DMA_MEM_TO_MEM:
390 		mode |= OWL_DMA_MODE_TS(0) | OWL_DMA_MODE_ST_DCU |
391 			OWL_DMA_MODE_DT_DCU | OWL_DMA_MODE_SAM_INC |
392 			OWL_DMA_MODE_DAM_INC;
393 
394 		break;
395 	case DMA_MEM_TO_DEV:
396 		mode |= OWL_DMA_MODE_TS(vchan->drq)
397 			| OWL_DMA_MODE_ST_DCU | OWL_DMA_MODE_DT_DEV
398 			| OWL_DMA_MODE_SAM_INC | OWL_DMA_MODE_DAM_CONST;
399 
400 		/*
401 		 * Hardware only supports 32bit and 8bit buswidth. Since the
402 		 * default is 32bit, select 8bit only when requested.
403 		 */
404 		if (sconfig->dst_addr_width == DMA_SLAVE_BUSWIDTH_1_BYTE)
405 			mode |= OWL_DMA_MODE_NDDBW_8BIT;
406 
407 		break;
408 	case DMA_DEV_TO_MEM:
409 		 mode |= OWL_DMA_MODE_TS(vchan->drq)
410 			| OWL_DMA_MODE_ST_DEV | OWL_DMA_MODE_DT_DCU
411 			| OWL_DMA_MODE_SAM_CONST | OWL_DMA_MODE_DAM_INC;
412 
413 		/*
414 		 * Hardware only supports 32bit and 8bit buswidth. Since the
415 		 * default is 32bit, select 8bit only when requested.
416 		 */
417 		if (sconfig->src_addr_width == DMA_SLAVE_BUSWIDTH_1_BYTE)
418 			mode |= OWL_DMA_MODE_NDDBW_8BIT;
419 
420 		break;
421 	default:
422 		return -EINVAL;
423 	}
424 
425 	lli->hw[OWL_DMADESC_CTRLA] = llc_hw_ctrla(mode,
426 						  OWL_DMA_LLC_SAV_LOAD_NEXT |
427 						  OWL_DMA_LLC_DAV_LOAD_NEXT);
428 
429 	if (is_cyclic)
430 		ctrlb = llc_hw_ctrlb(OWL_DMA_INTCTL_BLOCK);
431 	else
432 		ctrlb = llc_hw_ctrlb(OWL_DMA_INTCTL_SUPER_BLOCK);
433 
434 	lli->hw[OWL_DMADESC_NEXT_LLI] = 0; /* One link list by default */
435 	lli->hw[OWL_DMADESC_SADDR] = src;
436 	lli->hw[OWL_DMADESC_DADDR] = dst;
437 	lli->hw[OWL_DMADESC_SRC_STRIDE] = 0;
438 	lli->hw[OWL_DMADESC_DST_STRIDE] = 0;
439 
440 	if (od->devid == S700_DMA) {
441 		/* Max frame length is 1MB */
442 		lli->hw[OWL_DMADESC_FLEN] = len;
443 		/*
444 		 * On S700, word starts from offset 0x1C is shared between
445 		 * frame count and ctrlb, where first 12 bits are for frame
446 		 * count and rest of 20 bits are for ctrlb.
447 		 */
448 		lli->hw[OWL_DMADESC_CTRLB] = FCNT_VAL | ctrlb;
449 	} else {
450 		/*
451 		 * On S900, word starts from offset 0xC is shared between
452 		 * frame length (max frame length is 1MB) and frame count,
453 		 * where first 20 bits are for frame length and rest of
454 		 * 12 bits are for frame count.
455 		 */
456 		lli->hw[OWL_DMADESC_FLEN] = len | FCNT_VAL << 20;
457 		lli->hw[OWL_DMADESC_CTRLB] = ctrlb;
458 	}
459 
460 	return 0;
461 }
462 
463 static struct owl_dma_pchan *owl_dma_get_pchan(struct owl_dma *od,
464 					       struct owl_dma_vchan *vchan)
465 {
466 	struct owl_dma_pchan *pchan = NULL;
467 	unsigned long flags;
468 	int i;
469 
470 	for (i = 0; i < od->nr_pchans; i++) {
471 		pchan = &od->pchans[i];
472 
473 		spin_lock_irqsave(&od->lock, flags);
474 		if (!pchan->vchan) {
475 			pchan->vchan = vchan;
476 			spin_unlock_irqrestore(&od->lock, flags);
477 			break;
478 		}
479 
480 		spin_unlock_irqrestore(&od->lock, flags);
481 	}
482 
483 	return pchan;
484 }
485 
486 static int owl_dma_pchan_busy(struct owl_dma *od, struct owl_dma_pchan *pchan)
487 {
488 	unsigned int val;
489 
490 	val = dma_readl(od, OWL_DMA_IDLE_STAT);
491 
492 	return !(val & (1 << pchan->id));
493 }
494 
495 static void owl_dma_terminate_pchan(struct owl_dma *od,
496 				    struct owl_dma_pchan *pchan)
497 {
498 	unsigned long flags;
499 	u32 irq_pd;
500 
501 	pchan_writel(pchan, OWL_DMAX_START, 0);
502 	pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
503 
504 	spin_lock_irqsave(&od->lock, flags);
505 	dma_update(od, OWL_DMA_IRQ_EN0, (1 << pchan->id), false);
506 
507 	irq_pd = dma_readl(od, OWL_DMA_IRQ_PD0);
508 	if (irq_pd & (1 << pchan->id)) {
509 		dev_warn(od->dma.dev,
510 			 "terminating pchan %d that still has pending irq\n",
511 			 pchan->id);
512 		dma_writel(od, OWL_DMA_IRQ_PD0, (1 << pchan->id));
513 	}
514 
515 	pchan->vchan = NULL;
516 
517 	spin_unlock_irqrestore(&od->lock, flags);
518 }
519 
520 static void owl_dma_pause_pchan(struct owl_dma_pchan *pchan)
521 {
522 	pchan_writel(pchan, 1, OWL_DMAX_PAUSE);
523 }
524 
525 static void owl_dma_resume_pchan(struct owl_dma_pchan *pchan)
526 {
527 	pchan_writel(pchan, 0, OWL_DMAX_PAUSE);
528 }
529 
530 static int owl_dma_start_next_txd(struct owl_dma_vchan *vchan)
531 {
532 	struct owl_dma *od = to_owl_dma(vchan->vc.chan.device);
533 	struct virt_dma_desc *vd = vchan_next_desc(&vchan->vc);
534 	struct owl_dma_pchan *pchan = vchan->pchan;
535 	struct owl_dma_txd *txd = to_owl_txd(&vd->tx);
536 	struct owl_dma_lli *lli;
537 	unsigned long flags;
538 	u32 int_ctl;
539 
540 	list_del(&vd->node);
541 
542 	vchan->txd = txd;
543 
544 	/* Wait for channel inactive */
545 	while (owl_dma_pchan_busy(od, pchan))
546 		cpu_relax();
547 
548 	lli = list_first_entry(&txd->lli_list,
549 			       struct owl_dma_lli, node);
550 
551 	if (txd->cyclic)
552 		int_ctl = OWL_DMA_INTCTL_BLOCK;
553 	else
554 		int_ctl = OWL_DMA_INTCTL_SUPER_BLOCK;
555 
556 	pchan_writel(pchan, OWL_DMAX_MODE, OWL_DMA_MODE_LME);
557 	pchan_writel(pchan, OWL_DMAX_LINKLIST_CTL,
558 		     OWL_DMA_LLC_SAV_LOAD_NEXT | OWL_DMA_LLC_DAV_LOAD_NEXT);
559 	pchan_writel(pchan, OWL_DMAX_NEXT_DESCRIPTOR, lli->phys);
560 	pchan_writel(pchan, OWL_DMAX_INT_CTL, int_ctl);
561 
562 	/* Clear IRQ status for this pchan */
563 	pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
564 
565 	spin_lock_irqsave(&od->lock, flags);
566 
567 	dma_update(od, OWL_DMA_IRQ_EN0, (1 << pchan->id), true);
568 
569 	spin_unlock_irqrestore(&od->lock, flags);
570 
571 	dev_dbg(vchan_chan_dev(&vchan->vc), "starting pchan %d\n", pchan->id);
572 
573 	/* Start DMA transfer for this pchan */
574 	pchan_writel(pchan, OWL_DMAX_START, 0x1);
575 
576 	return 0;
577 }
578 
579 static void owl_dma_phy_free(struct owl_dma *od, struct owl_dma_vchan *vchan)
580 {
581 	/* Ensure that the physical channel is stopped */
582 	owl_dma_terminate_pchan(od, vchan->pchan);
583 
584 	vchan->pchan = NULL;
585 }
586 
587 static irqreturn_t owl_dma_interrupt(int irq, void *dev_id)
588 {
589 	struct owl_dma *od = dev_id;
590 	struct owl_dma_vchan *vchan;
591 	struct owl_dma_pchan *pchan;
592 	unsigned long pending;
593 	int i;
594 	unsigned int global_irq_pending, chan_irq_pending;
595 
596 	spin_lock(&od->lock);
597 
598 	pending = dma_readl(od, OWL_DMA_IRQ_PD0);
599 
600 	/* Clear IRQ status for each pchan */
601 	for_each_set_bit(i, &pending, od->nr_pchans) {
602 		pchan = &od->pchans[i];
603 		pchan_update(pchan, OWL_DMAX_INT_STATUS, 0xff, false);
604 	}
605 
606 	/* Clear pending IRQ */
607 	dma_writel(od, OWL_DMA_IRQ_PD0, pending);
608 
609 	/* Check missed pending IRQ */
610 	for (i = 0; i < od->nr_pchans; i++) {
611 		pchan = &od->pchans[i];
612 		chan_irq_pending = pchan_readl(pchan, OWL_DMAX_INT_CTL) &
613 				   pchan_readl(pchan, OWL_DMAX_INT_STATUS);
614 
615 		/* Dummy read to ensure OWL_DMA_IRQ_PD0 value is updated */
616 		dma_readl(od, OWL_DMA_IRQ_PD0);
617 
618 		global_irq_pending = dma_readl(od, OWL_DMA_IRQ_PD0);
619 
620 		if (chan_irq_pending && !(global_irq_pending & BIT(i))) {
621 			dev_dbg(od->dma.dev,
622 				"global and channel IRQ pending match err\n");
623 
624 			/* Clear IRQ status for this pchan */
625 			pchan_update(pchan, OWL_DMAX_INT_STATUS,
626 				     0xff, false);
627 
628 			/* Update global IRQ pending */
629 			pending |= BIT(i);
630 		}
631 	}
632 
633 	spin_unlock(&od->lock);
634 
635 	for_each_set_bit(i, &pending, od->nr_pchans) {
636 		struct owl_dma_txd *txd;
637 
638 		pchan = &od->pchans[i];
639 
640 		vchan = pchan->vchan;
641 		if (!vchan) {
642 			dev_warn(od->dma.dev, "no vchan attached on pchan %d\n",
643 				 pchan->id);
644 			continue;
645 		}
646 
647 		spin_lock(&vchan->vc.lock);
648 
649 		txd = vchan->txd;
650 		if (txd) {
651 			vchan->txd = NULL;
652 
653 			vchan_cookie_complete(&txd->vd);
654 
655 			/*
656 			 * Start the next descriptor (if any),
657 			 * otherwise free this channel.
658 			 */
659 			if (vchan_next_desc(&vchan->vc))
660 				owl_dma_start_next_txd(vchan);
661 			else
662 				owl_dma_phy_free(od, vchan);
663 		}
664 
665 		spin_unlock(&vchan->vc.lock);
666 	}
667 
668 	return IRQ_HANDLED;
669 }
670 
671 static void owl_dma_free_txd(struct owl_dma *od, struct owl_dma_txd *txd)
672 {
673 	struct owl_dma_lli *lli, *_lli;
674 
675 	if (unlikely(!txd))
676 		return;
677 
678 	list_for_each_entry_safe(lli, _lli, &txd->lli_list, node)
679 		owl_dma_free_lli(od, lli);
680 
681 	kfree(txd);
682 }
683 
684 static void owl_dma_desc_free(struct virt_dma_desc *vd)
685 {
686 	struct owl_dma *od = to_owl_dma(vd->tx.chan->device);
687 	struct owl_dma_txd *txd = to_owl_txd(&vd->tx);
688 
689 	owl_dma_free_txd(od, txd);
690 }
691 
692 static int owl_dma_terminate_all(struct dma_chan *chan)
693 {
694 	struct owl_dma *od = to_owl_dma(chan->device);
695 	struct owl_dma_vchan *vchan = to_owl_vchan(chan);
696 	unsigned long flags;
697 	LIST_HEAD(head);
698 
699 	spin_lock_irqsave(&vchan->vc.lock, flags);
700 
701 	if (vchan->pchan)
702 		owl_dma_phy_free(od, vchan);
703 
704 	if (vchan->txd) {
705 		owl_dma_desc_free(&vchan->txd->vd);
706 		vchan->txd = NULL;
707 	}
708 
709 	vchan_get_all_descriptors(&vchan->vc, &head);
710 
711 	spin_unlock_irqrestore(&vchan->vc.lock, flags);
712 
713 	vchan_dma_desc_free_list(&vchan->vc, &head);
714 
715 	return 0;
716 }
717 
718 static int owl_dma_config(struct dma_chan *chan,
719 			  struct dma_slave_config *config)
720 {
721 	struct owl_dma_vchan *vchan = to_owl_vchan(chan);
722 
723 	/* Reject definitely invalid configurations */
724 	if (config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
725 	    config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
726 		return -EINVAL;
727 
728 	memcpy(&vchan->cfg, config, sizeof(struct dma_slave_config));
729 
730 	return 0;
731 }
732 
733 static int owl_dma_pause(struct dma_chan *chan)
734 {
735 	struct owl_dma_vchan *vchan = to_owl_vchan(chan);
736 	unsigned long flags;
737 
738 	spin_lock_irqsave(&vchan->vc.lock, flags);
739 
740 	owl_dma_pause_pchan(vchan->pchan);
741 
742 	spin_unlock_irqrestore(&vchan->vc.lock, flags);
743 
744 	return 0;
745 }
746 
747 static int owl_dma_resume(struct dma_chan *chan)
748 {
749 	struct owl_dma_vchan *vchan = to_owl_vchan(chan);
750 	unsigned long flags;
751 
752 	if (!vchan->pchan && !vchan->txd)
753 		return 0;
754 
755 	dev_dbg(dmaengine_chan_dev(chan), "vchan %p: resume\n", &vchan->vc);
756 
757 	spin_lock_irqsave(&vchan->vc.lock, flags);
758 
759 	owl_dma_resume_pchan(vchan->pchan);
760 
761 	spin_unlock_irqrestore(&vchan->vc.lock, flags);
762 
763 	return 0;
764 }
765 
766 static u32 owl_dma_getbytes_chan(struct owl_dma_vchan *vchan)
767 {
768 	struct owl_dma_pchan *pchan;
769 	struct owl_dma_txd *txd;
770 	struct owl_dma_lli *lli;
771 	unsigned int next_lli_phy;
772 	size_t bytes;
773 
774 	pchan = vchan->pchan;
775 	txd = vchan->txd;
776 
777 	if (!pchan || !txd)
778 		return 0;
779 
780 	/* Get remain count of current node in link list */
781 	bytes = pchan_readl(pchan, OWL_DMAX_REMAIN_CNT);
782 
783 	/* Loop through the preceding nodes to get total remaining bytes */
784 	if (pchan_readl(pchan, OWL_DMAX_MODE) & OWL_DMA_MODE_LME) {
785 		next_lli_phy = pchan_readl(pchan, OWL_DMAX_NEXT_DESCRIPTOR);
786 		list_for_each_entry(lli, &txd->lli_list, node) {
787 			/* Start from the next active node */
788 			if (lli->phys == next_lli_phy) {
789 				list_for_each_entry(lli, &txd->lli_list, node)
790 					bytes += llc_hw_flen(lli);
791 				break;
792 			}
793 		}
794 	}
795 
796 	return bytes;
797 }
798 
799 static enum dma_status owl_dma_tx_status(struct dma_chan *chan,
800 					 dma_cookie_t cookie,
801 					 struct dma_tx_state *state)
802 {
803 	struct owl_dma_vchan *vchan = to_owl_vchan(chan);
804 	struct owl_dma_lli *lli;
805 	struct virt_dma_desc *vd;
806 	struct owl_dma_txd *txd;
807 	enum dma_status ret;
808 	unsigned long flags;
809 	size_t bytes = 0;
810 
811 	ret = dma_cookie_status(chan, cookie, state);
812 	if (ret == DMA_COMPLETE || !state)
813 		return ret;
814 
815 	spin_lock_irqsave(&vchan->vc.lock, flags);
816 
817 	vd = vchan_find_desc(&vchan->vc, cookie);
818 	if (vd) {
819 		txd = to_owl_txd(&vd->tx);
820 		list_for_each_entry(lli, &txd->lli_list, node)
821 			bytes += llc_hw_flen(lli);
822 	} else {
823 		bytes = owl_dma_getbytes_chan(vchan);
824 	}
825 
826 	spin_unlock_irqrestore(&vchan->vc.lock, flags);
827 
828 	dma_set_residue(state, bytes);
829 
830 	return ret;
831 }
832 
833 static void owl_dma_phy_alloc_and_start(struct owl_dma_vchan *vchan)
834 {
835 	struct owl_dma *od = to_owl_dma(vchan->vc.chan.device);
836 	struct owl_dma_pchan *pchan;
837 
838 	pchan = owl_dma_get_pchan(od, vchan);
839 	if (!pchan)
840 		return;
841 
842 	dev_dbg(od->dma.dev, "allocated pchan %d\n", pchan->id);
843 
844 	vchan->pchan = pchan;
845 	owl_dma_start_next_txd(vchan);
846 }
847 
848 static void owl_dma_issue_pending(struct dma_chan *chan)
849 {
850 	struct owl_dma_vchan *vchan = to_owl_vchan(chan);
851 	unsigned long flags;
852 
853 	spin_lock_irqsave(&vchan->vc.lock, flags);
854 	if (vchan_issue_pending(&vchan->vc)) {
855 		if (!vchan->pchan)
856 			owl_dma_phy_alloc_and_start(vchan);
857 	}
858 	spin_unlock_irqrestore(&vchan->vc.lock, flags);
859 }
860 
861 static struct dma_async_tx_descriptor
862 		*owl_dma_prep_memcpy(struct dma_chan *chan,
863 				     dma_addr_t dst, dma_addr_t src,
864 				     size_t len, unsigned long flags)
865 {
866 	struct owl_dma *od = to_owl_dma(chan->device);
867 	struct owl_dma_vchan *vchan = to_owl_vchan(chan);
868 	struct owl_dma_txd *txd;
869 	struct owl_dma_lli *lli, *prev = NULL;
870 	size_t offset, bytes;
871 	int ret;
872 
873 	if (!len)
874 		return NULL;
875 
876 	txd = kzalloc_obj(*txd, GFP_NOWAIT);
877 	if (!txd)
878 		return NULL;
879 
880 	INIT_LIST_HEAD(&txd->lli_list);
881 
882 	/* Process the transfer as frame by frame */
883 	for (offset = 0; offset < len; offset += bytes) {
884 		lli = owl_dma_alloc_lli(od);
885 		if (!lli) {
886 			dev_warn(dmaengine_chan_dev(chan), "failed to allocate lli\n");
887 			goto err_txd_free;
888 		}
889 
890 		bytes = min_t(size_t, (len - offset), OWL_DMA_FRAME_MAX_LENGTH);
891 
892 		ret = owl_dma_cfg_lli(vchan, lli, src + offset, dst + offset,
893 				      bytes, DMA_MEM_TO_MEM,
894 				      &vchan->cfg, txd->cyclic);
895 		if (ret) {
896 			dev_warn(dmaengine_chan_dev(chan), "failed to config lli\n");
897 			goto err_txd_free;
898 		}
899 
900 		prev = owl_dma_add_lli(txd, prev, lli, false);
901 	}
902 
903 	return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
904 
905 err_txd_free:
906 	owl_dma_free_txd(od, txd);
907 	return NULL;
908 }
909 
910 static struct dma_async_tx_descriptor
911 		*owl_dma_prep_slave_sg(struct dma_chan *chan,
912 				       struct scatterlist *sgl,
913 				       unsigned int sg_len,
914 				       enum dma_transfer_direction dir,
915 				       unsigned long flags, void *context)
916 {
917 	struct owl_dma *od = to_owl_dma(chan->device);
918 	struct owl_dma_vchan *vchan = to_owl_vchan(chan);
919 	struct dma_slave_config *sconfig = &vchan->cfg;
920 	struct owl_dma_txd *txd;
921 	struct owl_dma_lli *lli, *prev = NULL;
922 	struct scatterlist *sg;
923 	dma_addr_t addr, src = 0, dst = 0;
924 	size_t len;
925 	int ret, i;
926 
927 	txd = kzalloc_obj(*txd, GFP_NOWAIT);
928 	if (!txd)
929 		return NULL;
930 
931 	INIT_LIST_HEAD(&txd->lli_list);
932 
933 	for_each_sg(sgl, sg, sg_len, i) {
934 		addr = sg_dma_address(sg);
935 		len = sg_dma_len(sg);
936 
937 		if (len > OWL_DMA_FRAME_MAX_LENGTH) {
938 			dev_err(od->dma.dev,
939 				"frame length exceeds max supported length");
940 			goto err_txd_free;
941 		}
942 
943 		lli = owl_dma_alloc_lli(od);
944 		if (!lli) {
945 			dev_err(dmaengine_chan_dev(chan), "failed to allocate lli");
946 			goto err_txd_free;
947 		}
948 
949 		if (dir == DMA_MEM_TO_DEV) {
950 			src = addr;
951 			dst = sconfig->dst_addr;
952 		} else {
953 			src = sconfig->src_addr;
954 			dst = addr;
955 		}
956 
957 		ret = owl_dma_cfg_lli(vchan, lli, src, dst, len, dir, sconfig,
958 				      txd->cyclic);
959 		if (ret) {
960 			dev_warn(dmaengine_chan_dev(chan), "failed to config lli");
961 			goto err_txd_free;
962 		}
963 
964 		prev = owl_dma_add_lli(txd, prev, lli, false);
965 	}
966 
967 	return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
968 
969 err_txd_free:
970 	owl_dma_free_txd(od, txd);
971 
972 	return NULL;
973 }
974 
975 static struct dma_async_tx_descriptor
976 		*owl_prep_dma_cyclic(struct dma_chan *chan,
977 				     dma_addr_t buf_addr, size_t buf_len,
978 				     size_t period_len,
979 				     enum dma_transfer_direction dir,
980 				     unsigned long flags)
981 {
982 	struct owl_dma *od = to_owl_dma(chan->device);
983 	struct owl_dma_vchan *vchan = to_owl_vchan(chan);
984 	struct dma_slave_config *sconfig = &vchan->cfg;
985 	struct owl_dma_txd *txd;
986 	struct owl_dma_lli *lli, *prev = NULL, *first = NULL;
987 	dma_addr_t src = 0, dst = 0;
988 	unsigned int periods = buf_len / period_len;
989 	int ret, i;
990 
991 	txd = kzalloc_obj(*txd, GFP_NOWAIT);
992 	if (!txd)
993 		return NULL;
994 
995 	INIT_LIST_HEAD(&txd->lli_list);
996 	txd->cyclic = true;
997 
998 	for (i = 0; i < periods; i++) {
999 		lli = owl_dma_alloc_lli(od);
1000 		if (!lli) {
1001 			dev_warn(dmaengine_chan_dev(chan), "failed to allocate lli");
1002 			goto err_txd_free;
1003 		}
1004 
1005 		if (dir == DMA_MEM_TO_DEV) {
1006 			src = buf_addr + (period_len * i);
1007 			dst = sconfig->dst_addr;
1008 		} else if (dir == DMA_DEV_TO_MEM) {
1009 			src = sconfig->src_addr;
1010 			dst = buf_addr + (period_len * i);
1011 		}
1012 
1013 		ret = owl_dma_cfg_lli(vchan, lli, src, dst, period_len,
1014 				      dir, sconfig, txd->cyclic);
1015 		if (ret) {
1016 			dev_warn(dmaengine_chan_dev(chan), "failed to config lli");
1017 			goto err_txd_free;
1018 		}
1019 
1020 		if (!first)
1021 			first = lli;
1022 
1023 		prev = owl_dma_add_lli(txd, prev, lli, false);
1024 	}
1025 
1026 	/* close the cyclic list */
1027 	owl_dma_add_lli(txd, prev, first, true);
1028 
1029 	return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
1030 
1031 err_txd_free:
1032 	owl_dma_free_txd(od, txd);
1033 
1034 	return NULL;
1035 }
1036 
1037 static void owl_dma_free_chan_resources(struct dma_chan *chan)
1038 {
1039 	struct owl_dma_vchan *vchan = to_owl_vchan(chan);
1040 
1041 	/* Ensure all queued descriptors are freed */
1042 	vchan_free_chan_resources(&vchan->vc);
1043 }
1044 
1045 static inline void owl_dma_free(struct owl_dma *od)
1046 {
1047 	struct owl_dma_vchan *vchan = NULL;
1048 	struct owl_dma_vchan *next;
1049 
1050 	list_for_each_entry_safe(vchan,
1051 				 next, &od->dma.channels, vc.chan.device_node) {
1052 		list_del(&vchan->vc.chan.device_node);
1053 		tasklet_kill(&vchan->vc.task);
1054 	}
1055 }
1056 
1057 static struct dma_chan *owl_dma_of_xlate(struct of_phandle_args *dma_spec,
1058 					 struct of_dma *ofdma)
1059 {
1060 	struct owl_dma *od = ofdma->of_dma_data;
1061 	struct owl_dma_vchan *vchan;
1062 	struct dma_chan *chan;
1063 	u8 drq = dma_spec->args[0];
1064 
1065 	if (drq > od->nr_vchans)
1066 		return NULL;
1067 
1068 	chan = dma_get_any_slave_channel(&od->dma);
1069 	if (!chan)
1070 		return NULL;
1071 
1072 	vchan = to_owl_vchan(chan);
1073 	vchan->drq = drq;
1074 
1075 	return chan;
1076 }
1077 
1078 static const struct of_device_id owl_dma_match[] = {
1079 	{ .compatible = "actions,s500-dma", .data = (void *)S900_DMA,},
1080 	{ .compatible = "actions,s700-dma", .data = (void *)S700_DMA,},
1081 	{ .compatible = "actions,s900-dma", .data = (void *)S900_DMA,},
1082 	{ /* sentinel */ },
1083 };
1084 MODULE_DEVICE_TABLE(of, owl_dma_match);
1085 
1086 static int owl_dma_probe(struct platform_device *pdev)
1087 {
1088 	struct device_node *np = pdev->dev.of_node;
1089 	struct owl_dma *od;
1090 	int ret, i, nr_channels, nr_requests;
1091 
1092 	od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
1093 	if (!od)
1094 		return -ENOMEM;
1095 
1096 	od->base = devm_platform_ioremap_resource(pdev, 0);
1097 	if (IS_ERR(od->base))
1098 		return PTR_ERR(od->base);
1099 
1100 	ret = of_property_read_u32(np, "dma-channels", &nr_channels);
1101 	if (ret) {
1102 		dev_err(&pdev->dev, "can't get dma-channels\n");
1103 		return ret;
1104 	}
1105 
1106 	ret = of_property_read_u32(np, "dma-requests", &nr_requests);
1107 	if (ret) {
1108 		dev_err(&pdev->dev, "can't get dma-requests\n");
1109 		return ret;
1110 	}
1111 
1112 	dev_info(&pdev->dev, "dma-channels %d, dma-requests %d\n",
1113 		 nr_channels, nr_requests);
1114 
1115 	od->devid = (uintptr_t)of_device_get_match_data(&pdev->dev);
1116 
1117 	od->nr_pchans = nr_channels;
1118 	od->nr_vchans = nr_requests;
1119 
1120 	pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
1121 
1122 	platform_set_drvdata(pdev, od);
1123 	spin_lock_init(&od->lock);
1124 
1125 	dma_cap_set(DMA_MEMCPY, od->dma.cap_mask);
1126 	dma_cap_set(DMA_SLAVE, od->dma.cap_mask);
1127 	dma_cap_set(DMA_CYCLIC, od->dma.cap_mask);
1128 
1129 	od->dma.dev = &pdev->dev;
1130 	od->dma.device_free_chan_resources = owl_dma_free_chan_resources;
1131 	od->dma.device_tx_status = owl_dma_tx_status;
1132 	od->dma.device_issue_pending = owl_dma_issue_pending;
1133 	od->dma.device_prep_dma_memcpy = owl_dma_prep_memcpy;
1134 	od->dma.device_prep_slave_sg = owl_dma_prep_slave_sg;
1135 	od->dma.device_prep_dma_cyclic = owl_prep_dma_cyclic;
1136 	od->dma.device_config = owl_dma_config;
1137 	od->dma.device_pause = owl_dma_pause;
1138 	od->dma.device_resume = owl_dma_resume;
1139 	od->dma.device_terminate_all = owl_dma_terminate_all;
1140 	od->dma.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1141 	od->dma.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1142 	od->dma.directions = BIT(DMA_MEM_TO_MEM);
1143 	od->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1144 
1145 	INIT_LIST_HEAD(&od->dma.channels);
1146 
1147 	od->clk = devm_clk_get(&pdev->dev, NULL);
1148 	if (IS_ERR(od->clk)) {
1149 		dev_err(&pdev->dev, "unable to get clock\n");
1150 		return PTR_ERR(od->clk);
1151 	}
1152 
1153 	/*
1154 	 * Even though the DMA controller is capable of generating 4
1155 	 * IRQ's for DMA priority feature, we only use 1 IRQ for
1156 	 * simplification.
1157 	 */
1158 	od->irq = platform_get_irq(pdev, 0);
1159 	ret = devm_request_irq(&pdev->dev, od->irq, owl_dma_interrupt, 0,
1160 			       dev_name(&pdev->dev), od);
1161 	if (ret) {
1162 		dev_err(&pdev->dev, "unable to request IRQ\n");
1163 		return ret;
1164 	}
1165 
1166 	/* Init physical channel */
1167 	od->pchans = devm_kcalloc(&pdev->dev, od->nr_pchans,
1168 				  sizeof(struct owl_dma_pchan), GFP_KERNEL);
1169 	if (!od->pchans)
1170 		return -ENOMEM;
1171 
1172 	for (i = 0; i < od->nr_pchans; i++) {
1173 		struct owl_dma_pchan *pchan = &od->pchans[i];
1174 
1175 		pchan->id = i;
1176 		pchan->base = od->base + OWL_DMA_CHAN_BASE(i);
1177 	}
1178 
1179 	/* Init virtual channel */
1180 	od->vchans = devm_kcalloc(&pdev->dev, od->nr_vchans,
1181 				  sizeof(struct owl_dma_vchan), GFP_KERNEL);
1182 	if (!od->vchans)
1183 		return -ENOMEM;
1184 
1185 	for (i = 0; i < od->nr_vchans; i++) {
1186 		struct owl_dma_vchan *vchan = &od->vchans[i];
1187 
1188 		vchan->vc.desc_free = owl_dma_desc_free;
1189 		vchan_init(&vchan->vc, &od->dma);
1190 	}
1191 
1192 	/* Create a pool of consistent memory blocks for hardware descriptors */
1193 	od->lli_pool = dma_pool_create(dev_name(od->dma.dev), od->dma.dev,
1194 				       sizeof(struct owl_dma_lli),
1195 				       __alignof__(struct owl_dma_lli),
1196 				       0);
1197 	if (!od->lli_pool) {
1198 		dev_err(&pdev->dev, "unable to allocate DMA descriptor pool\n");
1199 		return -ENOMEM;
1200 	}
1201 
1202 	clk_prepare_enable(od->clk);
1203 
1204 	ret = dma_async_device_register(&od->dma);
1205 	if (ret) {
1206 		dev_err(&pdev->dev, "failed to register DMA engine device\n");
1207 		goto err_pool_free;
1208 	}
1209 
1210 	/* Device-tree DMA controller registration */
1211 	ret = of_dma_controller_register(pdev->dev.of_node,
1212 					 owl_dma_of_xlate, od);
1213 	if (ret) {
1214 		dev_err(&pdev->dev, "of_dma_controller_register failed\n");
1215 		goto err_dma_unregister;
1216 	}
1217 
1218 	return 0;
1219 
1220 err_dma_unregister:
1221 	dma_async_device_unregister(&od->dma);
1222 err_pool_free:
1223 	clk_disable_unprepare(od->clk);
1224 	dma_pool_destroy(od->lli_pool);
1225 
1226 	return ret;
1227 }
1228 
1229 static void owl_dma_remove(struct platform_device *pdev)
1230 {
1231 	struct owl_dma *od = platform_get_drvdata(pdev);
1232 
1233 	of_dma_controller_free(pdev->dev.of_node);
1234 	dma_async_device_unregister(&od->dma);
1235 
1236 	/* Mask all interrupts for this execution environment */
1237 	dma_writel(od, OWL_DMA_IRQ_EN0, 0x0);
1238 
1239 	/* Make sure we won't have any further interrupts */
1240 	devm_free_irq(od->dma.dev, od->irq, od);
1241 
1242 	owl_dma_free(od);
1243 
1244 	clk_disable_unprepare(od->clk);
1245 	dma_pool_destroy(od->lli_pool);
1246 }
1247 
1248 static struct platform_driver owl_dma_driver = {
1249 	.probe	= owl_dma_probe,
1250 	.remove = owl_dma_remove,
1251 	.driver = {
1252 		.name = "dma-owl",
1253 		.of_match_table = of_match_ptr(owl_dma_match),
1254 	},
1255 };
1256 
1257 static int owl_dma_init(void)
1258 {
1259 	return platform_driver_register(&owl_dma_driver);
1260 }
1261 subsys_initcall(owl_dma_init);
1262 
1263 static void __exit owl_dma_exit(void)
1264 {
1265 	platform_driver_unregister(&owl_dma_driver);
1266 }
1267 module_exit(owl_dma_exit);
1268 
1269 MODULE_AUTHOR("David Liu <liuwei@actions-semi.com>");
1270 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
1271 MODULE_DESCRIPTION("Actions Semi Owl SoCs DMA driver");
1272 MODULE_LICENSE("GPL");
1273