xref: /openbmc/linux/drivers/dma/xilinx/zynqmp_dma.c (revision 9145554d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * DMA driver for Xilinx ZynqMP DMA Engine
4  *
5  * Copyright (C) 2016 Xilinx, Inc. All rights reserved.
6  */
7 
8 #include <linux/bitops.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/io.h>
13 #include <linux/module.h>
14 #include <linux/of_dma.h>
15 #include <linux/of_platform.h>
16 #include <linux/slab.h>
17 #include <linux/clk.h>
18 #include <linux/io-64-nonatomic-lo-hi.h>
19 #include <linux/pm_runtime.h>
20 
21 #include "../dmaengine.h"
22 
23 /* Register Offsets */
24 #define ZYNQMP_DMA_ISR			0x100
25 #define ZYNQMP_DMA_IMR			0x104
26 #define ZYNQMP_DMA_IER			0x108
27 #define ZYNQMP_DMA_IDS			0x10C
28 #define ZYNQMP_DMA_CTRL0		0x110
29 #define ZYNQMP_DMA_CTRL1		0x114
30 #define ZYNQMP_DMA_DATA_ATTR		0x120
31 #define ZYNQMP_DMA_DSCR_ATTR		0x124
32 #define ZYNQMP_DMA_SRC_DSCR_WRD0	0x128
33 #define ZYNQMP_DMA_SRC_DSCR_WRD1	0x12C
34 #define ZYNQMP_DMA_SRC_DSCR_WRD2	0x130
35 #define ZYNQMP_DMA_SRC_DSCR_WRD3	0x134
36 #define ZYNQMP_DMA_DST_DSCR_WRD0	0x138
37 #define ZYNQMP_DMA_DST_DSCR_WRD1	0x13C
38 #define ZYNQMP_DMA_DST_DSCR_WRD2	0x140
39 #define ZYNQMP_DMA_DST_DSCR_WRD3	0x144
40 #define ZYNQMP_DMA_SRC_START_LSB	0x158
41 #define ZYNQMP_DMA_SRC_START_MSB	0x15C
42 #define ZYNQMP_DMA_DST_START_LSB	0x160
43 #define ZYNQMP_DMA_DST_START_MSB	0x164
44 #define ZYNQMP_DMA_TOTAL_BYTE		0x188
45 #define ZYNQMP_DMA_RATE_CTRL		0x18C
46 #define ZYNQMP_DMA_IRQ_SRC_ACCT		0x190
47 #define ZYNQMP_DMA_IRQ_DST_ACCT		0x194
48 #define ZYNQMP_DMA_CTRL2		0x200
49 
50 /* Interrupt registers bit field definitions */
51 #define ZYNQMP_DMA_DONE			BIT(10)
52 #define ZYNQMP_DMA_AXI_WR_DATA		BIT(9)
53 #define ZYNQMP_DMA_AXI_RD_DATA		BIT(8)
54 #define ZYNQMP_DMA_AXI_RD_DST_DSCR	BIT(7)
55 #define ZYNQMP_DMA_AXI_RD_SRC_DSCR	BIT(6)
56 #define ZYNQMP_DMA_IRQ_DST_ACCT_ERR	BIT(5)
57 #define ZYNQMP_DMA_IRQ_SRC_ACCT_ERR	BIT(4)
58 #define ZYNQMP_DMA_BYTE_CNT_OVRFL	BIT(3)
59 #define ZYNQMP_DMA_DST_DSCR_DONE	BIT(2)
60 #define ZYNQMP_DMA_INV_APB		BIT(0)
61 
62 /* Control 0 register bit field definitions */
63 #define ZYNQMP_DMA_OVR_FETCH		BIT(7)
64 #define ZYNQMP_DMA_POINT_TYPE_SG	BIT(6)
65 #define ZYNQMP_DMA_RATE_CTRL_EN		BIT(3)
66 
67 /* Control 1 register bit field definitions */
68 #define ZYNQMP_DMA_SRC_ISSUE		GENMASK(4, 0)
69 
70 /* Data Attribute register bit field definitions */
71 #define ZYNQMP_DMA_ARBURST		GENMASK(27, 26)
72 #define ZYNQMP_DMA_ARCACHE		GENMASK(25, 22)
73 #define ZYNQMP_DMA_ARCACHE_OFST		22
74 #define ZYNQMP_DMA_ARQOS		GENMASK(21, 18)
75 #define ZYNQMP_DMA_ARQOS_OFST		18
76 #define ZYNQMP_DMA_ARLEN		GENMASK(17, 14)
77 #define ZYNQMP_DMA_ARLEN_OFST		14
78 #define ZYNQMP_DMA_AWBURST		GENMASK(13, 12)
79 #define ZYNQMP_DMA_AWCACHE		GENMASK(11, 8)
80 #define ZYNQMP_DMA_AWCACHE_OFST		8
81 #define ZYNQMP_DMA_AWQOS		GENMASK(7, 4)
82 #define ZYNQMP_DMA_AWQOS_OFST		4
83 #define ZYNQMP_DMA_AWLEN		GENMASK(3, 0)
84 #define ZYNQMP_DMA_AWLEN_OFST		0
85 
86 /* Descriptor Attribute register bit field definitions */
87 #define ZYNQMP_DMA_AXCOHRNT		BIT(8)
88 #define ZYNQMP_DMA_AXCACHE		GENMASK(7, 4)
89 #define ZYNQMP_DMA_AXCACHE_OFST		4
90 #define ZYNQMP_DMA_AXQOS		GENMASK(3, 0)
91 #define ZYNQMP_DMA_AXQOS_OFST		0
92 
93 /* Control register 2 bit field definitions */
94 #define ZYNQMP_DMA_ENABLE		BIT(0)
95 
96 /* Buffer Descriptor definitions */
97 #define ZYNQMP_DMA_DESC_CTRL_STOP	0x10
98 #define ZYNQMP_DMA_DESC_CTRL_COMP_INT	0x4
99 #define ZYNQMP_DMA_DESC_CTRL_SIZE_256	0x2
100 #define ZYNQMP_DMA_DESC_CTRL_COHRNT	0x1
101 
102 /* Interrupt Mask specific definitions */
103 #define ZYNQMP_DMA_INT_ERR	(ZYNQMP_DMA_AXI_RD_DATA | \
104 				ZYNQMP_DMA_AXI_WR_DATA | \
105 				ZYNQMP_DMA_AXI_RD_DST_DSCR | \
106 				ZYNQMP_DMA_AXI_RD_SRC_DSCR | \
107 				ZYNQMP_DMA_INV_APB)
108 #define ZYNQMP_DMA_INT_OVRFL	(ZYNQMP_DMA_BYTE_CNT_OVRFL | \
109 				ZYNQMP_DMA_IRQ_SRC_ACCT_ERR | \
110 				ZYNQMP_DMA_IRQ_DST_ACCT_ERR)
111 #define ZYNQMP_DMA_INT_DONE	(ZYNQMP_DMA_DONE | ZYNQMP_DMA_DST_DSCR_DONE)
112 #define ZYNQMP_DMA_INT_EN_DEFAULT_MASK	(ZYNQMP_DMA_INT_DONE | \
113 					ZYNQMP_DMA_INT_ERR | \
114 					ZYNQMP_DMA_INT_OVRFL | \
115 					ZYNQMP_DMA_DST_DSCR_DONE)
116 
117 /* Max number of descriptors per channel */
118 #define ZYNQMP_DMA_NUM_DESCS	32
119 
120 /* Max transfer size per descriptor */
121 #define ZYNQMP_DMA_MAX_TRANS_LEN	0x40000000
122 
123 /* Max burst lengths */
124 #define ZYNQMP_DMA_MAX_DST_BURST_LEN    32768U
125 #define ZYNQMP_DMA_MAX_SRC_BURST_LEN    32768U
126 
127 /* Reset values for data attributes */
128 #define ZYNQMP_DMA_AXCACHE_VAL		0xF
129 
130 #define ZYNQMP_DMA_SRC_ISSUE_RST_VAL	0x1F
131 
132 #define ZYNQMP_DMA_IDS_DEFAULT_MASK	0xFFF
133 
134 /* Bus width in bits */
135 #define ZYNQMP_DMA_BUS_WIDTH_64		64
136 #define ZYNQMP_DMA_BUS_WIDTH_128	128
137 
138 #define ZDMA_PM_TIMEOUT			100
139 
140 #define ZYNQMP_DMA_DESC_SIZE(chan)	(chan->desc_size)
141 
142 #define to_chan(chan)		container_of(chan, struct zynqmp_dma_chan, \
143 					     common)
144 #define tx_to_desc(tx)		container_of(tx, struct zynqmp_dma_desc_sw, \
145 					     async_tx)
146 
147 /**
148  * struct zynqmp_dma_desc_ll - Hw linked list descriptor
149  * @addr: Buffer address
150  * @size: Size of the buffer
151  * @ctrl: Control word
152  * @nxtdscraddr: Next descriptor base address
153  * @rsvd: Reserved field and for Hw internal use.
154  */
155 struct zynqmp_dma_desc_ll {
156 	u64 addr;
157 	u32 size;
158 	u32 ctrl;
159 	u64 nxtdscraddr;
160 	u64 rsvd;
161 };
162 
163 /**
164  * struct zynqmp_dma_desc_sw - Per Transaction structure
165  * @src: Source address for simple mode dma
166  * @dst: Destination address for simple mode dma
167  * @len: Transfer length for simple mode dma
168  * @node: Node in the channel descriptor list
169  * @tx_list: List head for the current transfer
170  * @async_tx: Async transaction descriptor
171  * @src_v: Virtual address of the src descriptor
172  * @src_p: Physical address of the src descriptor
173  * @dst_v: Virtual address of the dst descriptor
174  * @dst_p: Physical address of the dst descriptor
175  */
176 struct zynqmp_dma_desc_sw {
177 	u64 src;
178 	u64 dst;
179 	u32 len;
180 	struct list_head node;
181 	struct list_head tx_list;
182 	struct dma_async_tx_descriptor async_tx;
183 	struct zynqmp_dma_desc_ll *src_v;
184 	dma_addr_t src_p;
185 	struct zynqmp_dma_desc_ll *dst_v;
186 	dma_addr_t dst_p;
187 };
188 
189 /**
190  * struct zynqmp_dma_chan - Driver specific DMA channel structure
191  * @zdev: Driver specific device structure
192  * @regs: Control registers offset
193  * @lock: Descriptor operation lock
194  * @pending_list: Descriptors waiting
195  * @free_list: Descriptors free
196  * @active_list: Descriptors active
197  * @sw_desc_pool: SW descriptor pool
198  * @done_list: Complete descriptors
199  * @common: DMA common channel
200  * @desc_pool_v: Statically allocated descriptor base
201  * @desc_pool_p: Physical allocated descriptor base
202  * @desc_free_cnt: Descriptor available count
203  * @dev: The dma device
204  * @irq: Channel IRQ
205  * @is_dmacoherent: Tells whether dma operations are coherent or not
206  * @tasklet: Cleanup work after irq
207  * @idle : Channel status;
208  * @desc_size: Size of the low level descriptor
209  * @err: Channel has errors
210  * @bus_width: Bus width
211  * @src_burst_len: Source burst length
212  * @dst_burst_len: Dest burst length
213  */
214 struct zynqmp_dma_chan {
215 	struct zynqmp_dma_device *zdev;
216 	void __iomem *regs;
217 	spinlock_t lock;
218 	struct list_head pending_list;
219 	struct list_head free_list;
220 	struct list_head active_list;
221 	struct zynqmp_dma_desc_sw *sw_desc_pool;
222 	struct list_head done_list;
223 	struct dma_chan common;
224 	void *desc_pool_v;
225 	dma_addr_t desc_pool_p;
226 	u32 desc_free_cnt;
227 	struct device *dev;
228 	int irq;
229 	bool is_dmacoherent;
230 	struct tasklet_struct tasklet;
231 	bool idle;
232 	u32 desc_size;
233 	bool err;
234 	u32 bus_width;
235 	u32 src_burst_len;
236 	u32 dst_burst_len;
237 };
238 
239 /**
240  * struct zynqmp_dma_device - DMA device structure
241  * @dev: Device Structure
242  * @common: DMA device structure
243  * @chan: Driver specific DMA channel
244  * @clk_main: Pointer to main clock
245  * @clk_apb: Pointer to apb clock
246  */
247 struct zynqmp_dma_device {
248 	struct device *dev;
249 	struct dma_device common;
250 	struct zynqmp_dma_chan *chan;
251 	struct clk *clk_main;
252 	struct clk *clk_apb;
253 };
254 
255 static inline void zynqmp_dma_writeq(struct zynqmp_dma_chan *chan, u32 reg,
256 				     u64 value)
257 {
258 	lo_hi_writeq(value, chan->regs + reg);
259 }
260 
261 /**
262  * zynqmp_dma_update_desc_to_ctrlr - Updates descriptor to the controller
263  * @chan: ZynqMP DMA DMA channel pointer
264  * @desc: Transaction descriptor pointer
265  */
266 static void zynqmp_dma_update_desc_to_ctrlr(struct zynqmp_dma_chan *chan,
267 				      struct zynqmp_dma_desc_sw *desc)
268 {
269 	dma_addr_t addr;
270 
271 	addr = desc->src_p;
272 	zynqmp_dma_writeq(chan, ZYNQMP_DMA_SRC_START_LSB, addr);
273 	addr = desc->dst_p;
274 	zynqmp_dma_writeq(chan, ZYNQMP_DMA_DST_START_LSB, addr);
275 }
276 
277 /**
278  * zynqmp_dma_desc_config_eod - Mark the descriptor as end descriptor
279  * @chan: ZynqMP DMA channel pointer
280  * @desc: Hw descriptor pointer
281  */
282 static void zynqmp_dma_desc_config_eod(struct zynqmp_dma_chan *chan,
283 				       void *desc)
284 {
285 	struct zynqmp_dma_desc_ll *hw = (struct zynqmp_dma_desc_ll *)desc;
286 
287 	hw->ctrl |= ZYNQMP_DMA_DESC_CTRL_STOP;
288 	hw++;
289 	hw->ctrl |= ZYNQMP_DMA_DESC_CTRL_COMP_INT | ZYNQMP_DMA_DESC_CTRL_STOP;
290 }
291 
292 /**
293  * zynqmp_dma_config_sg_ll_desc - Configure the linked list descriptor
294  * @chan: ZynqMP DMA channel pointer
295  * @sdesc: Hw descriptor pointer
296  * @src: Source buffer address
297  * @dst: Destination buffer address
298  * @len: Transfer length
299  * @prev: Previous hw descriptor pointer
300  */
301 static void zynqmp_dma_config_sg_ll_desc(struct zynqmp_dma_chan *chan,
302 				   struct zynqmp_dma_desc_ll *sdesc,
303 				   dma_addr_t src, dma_addr_t dst, size_t len,
304 				   struct zynqmp_dma_desc_ll *prev)
305 {
306 	struct zynqmp_dma_desc_ll *ddesc = sdesc + 1;
307 
308 	sdesc->size = ddesc->size = len;
309 	sdesc->addr = src;
310 	ddesc->addr = dst;
311 
312 	sdesc->ctrl = ddesc->ctrl = ZYNQMP_DMA_DESC_CTRL_SIZE_256;
313 	if (chan->is_dmacoherent) {
314 		sdesc->ctrl |= ZYNQMP_DMA_DESC_CTRL_COHRNT;
315 		ddesc->ctrl |= ZYNQMP_DMA_DESC_CTRL_COHRNT;
316 	}
317 
318 	if (prev) {
319 		dma_addr_t addr = chan->desc_pool_p +
320 			    ((uintptr_t)sdesc - (uintptr_t)chan->desc_pool_v);
321 		ddesc = prev + 1;
322 		prev->nxtdscraddr = addr;
323 		ddesc->nxtdscraddr = addr + ZYNQMP_DMA_DESC_SIZE(chan);
324 	}
325 }
326 
327 /**
328  * zynqmp_dma_init - Initialize the channel
329  * @chan: ZynqMP DMA channel pointer
330  */
331 static void zynqmp_dma_init(struct zynqmp_dma_chan *chan)
332 {
333 	u32 val;
334 
335 	writel(ZYNQMP_DMA_IDS_DEFAULT_MASK, chan->regs + ZYNQMP_DMA_IDS);
336 	val = readl(chan->regs + ZYNQMP_DMA_ISR);
337 	writel(val, chan->regs + ZYNQMP_DMA_ISR);
338 
339 	if (chan->is_dmacoherent) {
340 		val = ZYNQMP_DMA_AXCOHRNT;
341 		val = (val & ~ZYNQMP_DMA_AXCACHE) |
342 			(ZYNQMP_DMA_AXCACHE_VAL << ZYNQMP_DMA_AXCACHE_OFST);
343 		writel(val, chan->regs + ZYNQMP_DMA_DSCR_ATTR);
344 	}
345 
346 	val = readl(chan->regs + ZYNQMP_DMA_DATA_ATTR);
347 	if (chan->is_dmacoherent) {
348 		val = (val & ~ZYNQMP_DMA_ARCACHE) |
349 			(ZYNQMP_DMA_AXCACHE_VAL << ZYNQMP_DMA_ARCACHE_OFST);
350 		val = (val & ~ZYNQMP_DMA_AWCACHE) |
351 			(ZYNQMP_DMA_AXCACHE_VAL << ZYNQMP_DMA_AWCACHE_OFST);
352 	}
353 	writel(val, chan->regs + ZYNQMP_DMA_DATA_ATTR);
354 
355 	/* Clearing the interrupt account rgisters */
356 	val = readl(chan->regs + ZYNQMP_DMA_IRQ_SRC_ACCT);
357 	val = readl(chan->regs + ZYNQMP_DMA_IRQ_DST_ACCT);
358 
359 	chan->idle = true;
360 }
361 
362 /**
363  * zynqmp_dma_tx_submit - Submit DMA transaction
364  * @tx: Async transaction descriptor pointer
365  *
366  * Return: cookie value
367  */
368 static dma_cookie_t zynqmp_dma_tx_submit(struct dma_async_tx_descriptor *tx)
369 {
370 	struct zynqmp_dma_chan *chan = to_chan(tx->chan);
371 	struct zynqmp_dma_desc_sw *desc, *new;
372 	dma_cookie_t cookie;
373 	unsigned long irqflags;
374 
375 	new = tx_to_desc(tx);
376 	spin_lock_irqsave(&chan->lock, irqflags);
377 	cookie = dma_cookie_assign(tx);
378 
379 	if (!list_empty(&chan->pending_list)) {
380 		desc = list_last_entry(&chan->pending_list,
381 				     struct zynqmp_dma_desc_sw, node);
382 		if (!list_empty(&desc->tx_list))
383 			desc = list_last_entry(&desc->tx_list,
384 					       struct zynqmp_dma_desc_sw, node);
385 		desc->src_v->nxtdscraddr = new->src_p;
386 		desc->src_v->ctrl &= ~ZYNQMP_DMA_DESC_CTRL_STOP;
387 		desc->dst_v->nxtdscraddr = new->dst_p;
388 		desc->dst_v->ctrl &= ~ZYNQMP_DMA_DESC_CTRL_STOP;
389 	}
390 
391 	list_add_tail(&new->node, &chan->pending_list);
392 	spin_unlock_irqrestore(&chan->lock, irqflags);
393 
394 	return cookie;
395 }
396 
397 /**
398  * zynqmp_dma_get_descriptor - Get the sw descriptor from the pool
399  * @chan: ZynqMP DMA channel pointer
400  *
401  * Return: The sw descriptor
402  */
403 static struct zynqmp_dma_desc_sw *
404 zynqmp_dma_get_descriptor(struct zynqmp_dma_chan *chan)
405 {
406 	struct zynqmp_dma_desc_sw *desc;
407 	unsigned long irqflags;
408 
409 	spin_lock_irqsave(&chan->lock, irqflags);
410 	desc = list_first_entry(&chan->free_list,
411 				struct zynqmp_dma_desc_sw, node);
412 	list_del(&desc->node);
413 	spin_unlock_irqrestore(&chan->lock, irqflags);
414 
415 	INIT_LIST_HEAD(&desc->tx_list);
416 	/* Clear the src and dst descriptor memory */
417 	memset((void *)desc->src_v, 0, ZYNQMP_DMA_DESC_SIZE(chan));
418 	memset((void *)desc->dst_v, 0, ZYNQMP_DMA_DESC_SIZE(chan));
419 
420 	return desc;
421 }
422 
423 /**
424  * zynqmp_dma_free_descriptor - Issue pending transactions
425  * @chan: ZynqMP DMA channel pointer
426  * @sdesc: Transaction descriptor pointer
427  */
428 static void zynqmp_dma_free_descriptor(struct zynqmp_dma_chan *chan,
429 				 struct zynqmp_dma_desc_sw *sdesc)
430 {
431 	struct zynqmp_dma_desc_sw *child, *next;
432 
433 	chan->desc_free_cnt++;
434 	list_move_tail(&sdesc->node, &chan->free_list);
435 	list_for_each_entry_safe(child, next, &sdesc->tx_list, node) {
436 		chan->desc_free_cnt++;
437 		list_move_tail(&child->node, &chan->free_list);
438 	}
439 }
440 
441 /**
442  * zynqmp_dma_free_desc_list - Free descriptors list
443  * @chan: ZynqMP DMA channel pointer
444  * @list: List to parse and delete the descriptor
445  */
446 static void zynqmp_dma_free_desc_list(struct zynqmp_dma_chan *chan,
447 				      struct list_head *list)
448 {
449 	struct zynqmp_dma_desc_sw *desc, *next;
450 
451 	list_for_each_entry_safe(desc, next, list, node)
452 		zynqmp_dma_free_descriptor(chan, desc);
453 }
454 
455 /**
456  * zynqmp_dma_alloc_chan_resources - Allocate channel resources
457  * @dchan: DMA channel
458  *
459  * Return: Number of descriptors on success and failure value on error
460  */
461 static int zynqmp_dma_alloc_chan_resources(struct dma_chan *dchan)
462 {
463 	struct zynqmp_dma_chan *chan = to_chan(dchan);
464 	struct zynqmp_dma_desc_sw *desc;
465 	int i, ret;
466 
467 	ret = pm_runtime_resume_and_get(chan->dev);
468 	if (ret < 0)
469 		return ret;
470 
471 	chan->sw_desc_pool = kcalloc(ZYNQMP_DMA_NUM_DESCS, sizeof(*desc),
472 				     GFP_KERNEL);
473 	if (!chan->sw_desc_pool)
474 		return -ENOMEM;
475 
476 	chan->idle = true;
477 	chan->desc_free_cnt = ZYNQMP_DMA_NUM_DESCS;
478 
479 	INIT_LIST_HEAD(&chan->free_list);
480 
481 	for (i = 0; i < ZYNQMP_DMA_NUM_DESCS; i++) {
482 		desc = chan->sw_desc_pool + i;
483 		dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
484 		desc->async_tx.tx_submit = zynqmp_dma_tx_submit;
485 		list_add_tail(&desc->node, &chan->free_list);
486 	}
487 
488 	chan->desc_pool_v = dma_alloc_coherent(chan->dev,
489 					       (2 * chan->desc_size * ZYNQMP_DMA_NUM_DESCS),
490 					       &chan->desc_pool_p, GFP_KERNEL);
491 	if (!chan->desc_pool_v)
492 		return -ENOMEM;
493 
494 	for (i = 0; i < ZYNQMP_DMA_NUM_DESCS; i++) {
495 		desc = chan->sw_desc_pool + i;
496 		desc->src_v = (struct zynqmp_dma_desc_ll *) (chan->desc_pool_v +
497 					(i * ZYNQMP_DMA_DESC_SIZE(chan) * 2));
498 		desc->dst_v = (struct zynqmp_dma_desc_ll *) (desc->src_v + 1);
499 		desc->src_p = chan->desc_pool_p +
500 				(i * ZYNQMP_DMA_DESC_SIZE(chan) * 2);
501 		desc->dst_p = desc->src_p + ZYNQMP_DMA_DESC_SIZE(chan);
502 	}
503 
504 	return ZYNQMP_DMA_NUM_DESCS;
505 }
506 
507 /**
508  * zynqmp_dma_start - Start DMA channel
509  * @chan: ZynqMP DMA channel pointer
510  */
511 static void zynqmp_dma_start(struct zynqmp_dma_chan *chan)
512 {
513 	writel(ZYNQMP_DMA_INT_EN_DEFAULT_MASK, chan->regs + ZYNQMP_DMA_IER);
514 	writel(0, chan->regs + ZYNQMP_DMA_TOTAL_BYTE);
515 	chan->idle = false;
516 	writel(ZYNQMP_DMA_ENABLE, chan->regs + ZYNQMP_DMA_CTRL2);
517 }
518 
519 /**
520  * zynqmp_dma_handle_ovfl_int - Process the overflow interrupt
521  * @chan: ZynqMP DMA channel pointer
522  * @status: Interrupt status value
523  */
524 static void zynqmp_dma_handle_ovfl_int(struct zynqmp_dma_chan *chan, u32 status)
525 {
526 	if (status & ZYNQMP_DMA_BYTE_CNT_OVRFL)
527 		writel(0, chan->regs + ZYNQMP_DMA_TOTAL_BYTE);
528 	if (status & ZYNQMP_DMA_IRQ_DST_ACCT_ERR)
529 		readl(chan->regs + ZYNQMP_DMA_IRQ_DST_ACCT);
530 	if (status & ZYNQMP_DMA_IRQ_SRC_ACCT_ERR)
531 		readl(chan->regs + ZYNQMP_DMA_IRQ_SRC_ACCT);
532 }
533 
534 static void zynqmp_dma_config(struct zynqmp_dma_chan *chan)
535 {
536 	u32 val, burst_val;
537 
538 	val = readl(chan->regs + ZYNQMP_DMA_CTRL0);
539 	val |= ZYNQMP_DMA_POINT_TYPE_SG;
540 	writel(val, chan->regs + ZYNQMP_DMA_CTRL0);
541 
542 	val = readl(chan->regs + ZYNQMP_DMA_DATA_ATTR);
543 	burst_val = __ilog2_u32(chan->src_burst_len);
544 	val = (val & ~ZYNQMP_DMA_ARLEN) |
545 		((burst_val << ZYNQMP_DMA_ARLEN_OFST) & ZYNQMP_DMA_ARLEN);
546 	burst_val = __ilog2_u32(chan->dst_burst_len);
547 	val = (val & ~ZYNQMP_DMA_AWLEN) |
548 		((burst_val << ZYNQMP_DMA_AWLEN_OFST) & ZYNQMP_DMA_AWLEN);
549 	writel(val, chan->regs + ZYNQMP_DMA_DATA_ATTR);
550 }
551 
552 /**
553  * zynqmp_dma_device_config - Zynqmp dma device configuration
554  * @dchan: DMA channel
555  * @config: DMA device config
556  *
557  * Return: 0 always
558  */
559 static int zynqmp_dma_device_config(struct dma_chan *dchan,
560 				    struct dma_slave_config *config)
561 {
562 	struct zynqmp_dma_chan *chan = to_chan(dchan);
563 
564 	chan->src_burst_len = clamp(config->src_maxburst, 1U,
565 		ZYNQMP_DMA_MAX_SRC_BURST_LEN);
566 	chan->dst_burst_len = clamp(config->dst_maxburst, 1U,
567 		ZYNQMP_DMA_MAX_DST_BURST_LEN);
568 
569 	return 0;
570 }
571 
572 /**
573  * zynqmp_dma_start_transfer - Initiate the new transfer
574  * @chan: ZynqMP DMA channel pointer
575  */
576 static void zynqmp_dma_start_transfer(struct zynqmp_dma_chan *chan)
577 {
578 	struct zynqmp_dma_desc_sw *desc;
579 
580 	if (!chan->idle)
581 		return;
582 
583 	zynqmp_dma_config(chan);
584 
585 	desc = list_first_entry_or_null(&chan->pending_list,
586 					struct zynqmp_dma_desc_sw, node);
587 	if (!desc)
588 		return;
589 
590 	list_splice_tail_init(&chan->pending_list, &chan->active_list);
591 	zynqmp_dma_update_desc_to_ctrlr(chan, desc);
592 	zynqmp_dma_start(chan);
593 }
594 
595 
596 /**
597  * zynqmp_dma_chan_desc_cleanup - Cleanup the completed descriptors
598  * @chan: ZynqMP DMA channel
599  */
600 static void zynqmp_dma_chan_desc_cleanup(struct zynqmp_dma_chan *chan)
601 {
602 	struct zynqmp_dma_desc_sw *desc, *next;
603 	unsigned long irqflags;
604 
605 	spin_lock_irqsave(&chan->lock, irqflags);
606 
607 	list_for_each_entry_safe(desc, next, &chan->done_list, node) {
608 		struct dmaengine_desc_callback cb;
609 
610 		dmaengine_desc_get_callback(&desc->async_tx, &cb);
611 		if (dmaengine_desc_callback_valid(&cb)) {
612 			spin_unlock_irqrestore(&chan->lock, irqflags);
613 			dmaengine_desc_callback_invoke(&cb, NULL);
614 			spin_lock_irqsave(&chan->lock, irqflags);
615 		}
616 
617 		/* Run any dependencies, then free the descriptor */
618 		zynqmp_dma_free_descriptor(chan, desc);
619 	}
620 
621 	spin_unlock_irqrestore(&chan->lock, irqflags);
622 }
623 
624 /**
625  * zynqmp_dma_complete_descriptor - Mark the active descriptor as complete
626  * @chan: ZynqMP DMA channel pointer
627  */
628 static void zynqmp_dma_complete_descriptor(struct zynqmp_dma_chan *chan)
629 {
630 	struct zynqmp_dma_desc_sw *desc;
631 
632 	desc = list_first_entry_or_null(&chan->active_list,
633 					struct zynqmp_dma_desc_sw, node);
634 	if (!desc)
635 		return;
636 	list_del(&desc->node);
637 	dma_cookie_complete(&desc->async_tx);
638 	list_add_tail(&desc->node, &chan->done_list);
639 }
640 
641 /**
642  * zynqmp_dma_issue_pending - Issue pending transactions
643  * @dchan: DMA channel pointer
644  */
645 static void zynqmp_dma_issue_pending(struct dma_chan *dchan)
646 {
647 	struct zynqmp_dma_chan *chan = to_chan(dchan);
648 	unsigned long irqflags;
649 
650 	spin_lock_irqsave(&chan->lock, irqflags);
651 	zynqmp_dma_start_transfer(chan);
652 	spin_unlock_irqrestore(&chan->lock, irqflags);
653 }
654 
655 /**
656  * zynqmp_dma_free_descriptors - Free channel descriptors
657  * @chan: ZynqMP DMA channel pointer
658  */
659 static void zynqmp_dma_free_descriptors(struct zynqmp_dma_chan *chan)
660 {
661 	unsigned long irqflags;
662 
663 	spin_lock_irqsave(&chan->lock, irqflags);
664 	zynqmp_dma_free_desc_list(chan, &chan->active_list);
665 	zynqmp_dma_free_desc_list(chan, &chan->pending_list);
666 	zynqmp_dma_free_desc_list(chan, &chan->done_list);
667 	spin_unlock_irqrestore(&chan->lock, irqflags);
668 }
669 
670 /**
671  * zynqmp_dma_free_chan_resources - Free channel resources
672  * @dchan: DMA channel pointer
673  */
674 static void zynqmp_dma_free_chan_resources(struct dma_chan *dchan)
675 {
676 	struct zynqmp_dma_chan *chan = to_chan(dchan);
677 
678 	zynqmp_dma_free_descriptors(chan);
679 	dma_free_coherent(chan->dev,
680 		(2 * ZYNQMP_DMA_DESC_SIZE(chan) * ZYNQMP_DMA_NUM_DESCS),
681 		chan->desc_pool_v, chan->desc_pool_p);
682 	kfree(chan->sw_desc_pool);
683 	pm_runtime_mark_last_busy(chan->dev);
684 	pm_runtime_put_autosuspend(chan->dev);
685 }
686 
687 /**
688  * zynqmp_dma_reset - Reset the channel
689  * @chan: ZynqMP DMA channel pointer
690  */
691 static void zynqmp_dma_reset(struct zynqmp_dma_chan *chan)
692 {
693 	unsigned long irqflags;
694 
695 	writel(ZYNQMP_DMA_IDS_DEFAULT_MASK, chan->regs + ZYNQMP_DMA_IDS);
696 
697 	spin_lock_irqsave(&chan->lock, irqflags);
698 	zynqmp_dma_complete_descriptor(chan);
699 	spin_unlock_irqrestore(&chan->lock, irqflags);
700 	zynqmp_dma_chan_desc_cleanup(chan);
701 	zynqmp_dma_free_descriptors(chan);
702 
703 	zynqmp_dma_init(chan);
704 }
705 
706 /**
707  * zynqmp_dma_irq_handler - ZynqMP DMA Interrupt handler
708  * @irq: IRQ number
709  * @data: Pointer to the ZynqMP DMA channel structure
710  *
711  * Return: IRQ_HANDLED/IRQ_NONE
712  */
713 static irqreturn_t zynqmp_dma_irq_handler(int irq, void *data)
714 {
715 	struct zynqmp_dma_chan *chan = (struct zynqmp_dma_chan *)data;
716 	u32 isr, imr, status;
717 	irqreturn_t ret = IRQ_NONE;
718 
719 	isr = readl(chan->regs + ZYNQMP_DMA_ISR);
720 	imr = readl(chan->regs + ZYNQMP_DMA_IMR);
721 	status = isr & ~imr;
722 
723 	writel(isr, chan->regs + ZYNQMP_DMA_ISR);
724 	if (status & ZYNQMP_DMA_INT_DONE) {
725 		tasklet_schedule(&chan->tasklet);
726 		ret = IRQ_HANDLED;
727 	}
728 
729 	if (status & ZYNQMP_DMA_DONE)
730 		chan->idle = true;
731 
732 	if (status & ZYNQMP_DMA_INT_ERR) {
733 		chan->err = true;
734 		tasklet_schedule(&chan->tasklet);
735 		dev_err(chan->dev, "Channel %p has errors\n", chan);
736 		ret = IRQ_HANDLED;
737 	}
738 
739 	if (status & ZYNQMP_DMA_INT_OVRFL) {
740 		zynqmp_dma_handle_ovfl_int(chan, status);
741 		dev_dbg(chan->dev, "Channel %p overflow interrupt\n", chan);
742 		ret = IRQ_HANDLED;
743 	}
744 
745 	return ret;
746 }
747 
748 /**
749  * zynqmp_dma_do_tasklet - Schedule completion tasklet
750  * @t: Pointer to the ZynqMP DMA channel structure
751  */
752 static void zynqmp_dma_do_tasklet(struct tasklet_struct *t)
753 {
754 	struct zynqmp_dma_chan *chan = from_tasklet(chan, t, tasklet);
755 	u32 count;
756 	unsigned long irqflags;
757 
758 	if (chan->err) {
759 		zynqmp_dma_reset(chan);
760 		chan->err = false;
761 		return;
762 	}
763 
764 	spin_lock_irqsave(&chan->lock, irqflags);
765 	count = readl(chan->regs + ZYNQMP_DMA_IRQ_DST_ACCT);
766 	while (count) {
767 		zynqmp_dma_complete_descriptor(chan);
768 		count--;
769 	}
770 	spin_unlock_irqrestore(&chan->lock, irqflags);
771 
772 	zynqmp_dma_chan_desc_cleanup(chan);
773 
774 	if (chan->idle) {
775 		spin_lock_irqsave(&chan->lock, irqflags);
776 		zynqmp_dma_start_transfer(chan);
777 		spin_unlock_irqrestore(&chan->lock, irqflags);
778 	}
779 }
780 
781 /**
782  * zynqmp_dma_device_terminate_all - Aborts all transfers on a channel
783  * @dchan: DMA channel pointer
784  *
785  * Return: Always '0'
786  */
787 static int zynqmp_dma_device_terminate_all(struct dma_chan *dchan)
788 {
789 	struct zynqmp_dma_chan *chan = to_chan(dchan);
790 
791 	writel(ZYNQMP_DMA_IDS_DEFAULT_MASK, chan->regs + ZYNQMP_DMA_IDS);
792 	zynqmp_dma_free_descriptors(chan);
793 
794 	return 0;
795 }
796 
797 /**
798  * zynqmp_dma_prep_memcpy - prepare descriptors for memcpy transaction
799  * @dchan: DMA channel
800  * @dma_dst: Destination buffer address
801  * @dma_src: Source buffer address
802  * @len: Transfer length
803  * @flags: transfer ack flags
804  *
805  * Return: Async transaction descriptor on success and NULL on failure
806  */
807 static struct dma_async_tx_descriptor *zynqmp_dma_prep_memcpy(
808 				struct dma_chan *dchan, dma_addr_t dma_dst,
809 				dma_addr_t dma_src, size_t len, ulong flags)
810 {
811 	struct zynqmp_dma_chan *chan;
812 	struct zynqmp_dma_desc_sw *new, *first = NULL;
813 	void *desc = NULL, *prev = NULL;
814 	size_t copy;
815 	u32 desc_cnt;
816 	unsigned long irqflags;
817 
818 	chan = to_chan(dchan);
819 
820 	desc_cnt = DIV_ROUND_UP(len, ZYNQMP_DMA_MAX_TRANS_LEN);
821 
822 	spin_lock_irqsave(&chan->lock, irqflags);
823 	if (desc_cnt > chan->desc_free_cnt) {
824 		spin_unlock_irqrestore(&chan->lock, irqflags);
825 		dev_dbg(chan->dev, "chan %p descs are not available\n", chan);
826 		return NULL;
827 	}
828 	chan->desc_free_cnt = chan->desc_free_cnt - desc_cnt;
829 	spin_unlock_irqrestore(&chan->lock, irqflags);
830 
831 	do {
832 		/* Allocate and populate the descriptor */
833 		new = zynqmp_dma_get_descriptor(chan);
834 
835 		copy = min_t(size_t, len, ZYNQMP_DMA_MAX_TRANS_LEN);
836 		desc = (struct zynqmp_dma_desc_ll *)new->src_v;
837 		zynqmp_dma_config_sg_ll_desc(chan, desc, dma_src,
838 					     dma_dst, copy, prev);
839 		prev = desc;
840 		len -= copy;
841 		dma_src += copy;
842 		dma_dst += copy;
843 		if (!first)
844 			first = new;
845 		else
846 			list_add_tail(&new->node, &first->tx_list);
847 	} while (len);
848 
849 	zynqmp_dma_desc_config_eod(chan, desc);
850 	async_tx_ack(&first->async_tx);
851 	first->async_tx.flags = flags;
852 	return &first->async_tx;
853 }
854 
855 /**
856  * zynqmp_dma_chan_remove - Channel remove function
857  * @chan: ZynqMP DMA channel pointer
858  */
859 static void zynqmp_dma_chan_remove(struct zynqmp_dma_chan *chan)
860 {
861 	if (!chan)
862 		return;
863 
864 	if (chan->irq)
865 		devm_free_irq(chan->zdev->dev, chan->irq, chan);
866 	tasklet_kill(&chan->tasklet);
867 	list_del(&chan->common.device_node);
868 }
869 
870 /**
871  * zynqmp_dma_chan_probe - Per Channel Probing
872  * @zdev: Driver specific device structure
873  * @pdev: Pointer to the platform_device structure
874  *
875  * Return: '0' on success and failure value on error
876  */
877 static int zynqmp_dma_chan_probe(struct zynqmp_dma_device *zdev,
878 			   struct platform_device *pdev)
879 {
880 	struct zynqmp_dma_chan *chan;
881 	struct resource *res;
882 	struct device_node *node = pdev->dev.of_node;
883 	int err;
884 
885 	chan = devm_kzalloc(zdev->dev, sizeof(*chan), GFP_KERNEL);
886 	if (!chan)
887 		return -ENOMEM;
888 	chan->dev = zdev->dev;
889 	chan->zdev = zdev;
890 
891 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
892 	chan->regs = devm_ioremap_resource(&pdev->dev, res);
893 	if (IS_ERR(chan->regs))
894 		return PTR_ERR(chan->regs);
895 
896 	chan->bus_width = ZYNQMP_DMA_BUS_WIDTH_64;
897 	chan->dst_burst_len = ZYNQMP_DMA_MAX_DST_BURST_LEN;
898 	chan->src_burst_len = ZYNQMP_DMA_MAX_SRC_BURST_LEN;
899 	err = of_property_read_u32(node, "xlnx,bus-width", &chan->bus_width);
900 	if (err < 0) {
901 		dev_err(&pdev->dev, "missing xlnx,bus-width property\n");
902 		return err;
903 	}
904 
905 	if (chan->bus_width != ZYNQMP_DMA_BUS_WIDTH_64 &&
906 	    chan->bus_width != ZYNQMP_DMA_BUS_WIDTH_128) {
907 		dev_err(zdev->dev, "invalid bus-width value");
908 		return -EINVAL;
909 	}
910 
911 	chan->is_dmacoherent =  of_property_read_bool(node, "dma-coherent");
912 	zdev->chan = chan;
913 	tasklet_setup(&chan->tasklet, zynqmp_dma_do_tasklet);
914 	spin_lock_init(&chan->lock);
915 	INIT_LIST_HEAD(&chan->active_list);
916 	INIT_LIST_HEAD(&chan->pending_list);
917 	INIT_LIST_HEAD(&chan->done_list);
918 	INIT_LIST_HEAD(&chan->free_list);
919 
920 	dma_cookie_init(&chan->common);
921 	chan->common.device = &zdev->common;
922 	list_add_tail(&chan->common.device_node, &zdev->common.channels);
923 
924 	zynqmp_dma_init(chan);
925 	chan->irq = platform_get_irq(pdev, 0);
926 	if (chan->irq < 0)
927 		return -ENXIO;
928 	err = devm_request_irq(&pdev->dev, chan->irq, zynqmp_dma_irq_handler, 0,
929 			       "zynqmp-dma", chan);
930 	if (err)
931 		return err;
932 
933 	chan->desc_size = sizeof(struct zynqmp_dma_desc_ll);
934 	chan->idle = true;
935 	return 0;
936 }
937 
938 /**
939  * of_zynqmp_dma_xlate - Translation function
940  * @dma_spec: Pointer to DMA specifier as found in the device tree
941  * @ofdma: Pointer to DMA controller data
942  *
943  * Return: DMA channel pointer on success and NULL on error
944  */
945 static struct dma_chan *of_zynqmp_dma_xlate(struct of_phandle_args *dma_spec,
946 					    struct of_dma *ofdma)
947 {
948 	struct zynqmp_dma_device *zdev = ofdma->of_dma_data;
949 
950 	return dma_get_slave_channel(&zdev->chan->common);
951 }
952 
953 /**
954  * zynqmp_dma_suspend - Suspend method for the driver
955  * @dev:	Address of the device structure
956  *
957  * Put the driver into low power mode.
958  * Return: 0 on success and failure value on error
959  */
960 static int __maybe_unused zynqmp_dma_suspend(struct device *dev)
961 {
962 	if (!device_may_wakeup(dev))
963 		return pm_runtime_force_suspend(dev);
964 
965 	return 0;
966 }
967 
968 /**
969  * zynqmp_dma_resume - Resume from suspend
970  * @dev:	Address of the device structure
971  *
972  * Resume operation after suspend.
973  * Return: 0 on success and failure value on error
974  */
975 static int __maybe_unused zynqmp_dma_resume(struct device *dev)
976 {
977 	if (!device_may_wakeup(dev))
978 		return pm_runtime_force_resume(dev);
979 
980 	return 0;
981 }
982 
983 /**
984  * zynqmp_dma_runtime_suspend - Runtime suspend method for the driver
985  * @dev:	Address of the device structure
986  *
987  * Put the driver into low power mode.
988  * Return: 0 always
989  */
990 static int __maybe_unused zynqmp_dma_runtime_suspend(struct device *dev)
991 {
992 	struct zynqmp_dma_device *zdev = dev_get_drvdata(dev);
993 
994 	clk_disable_unprepare(zdev->clk_main);
995 	clk_disable_unprepare(zdev->clk_apb);
996 
997 	return 0;
998 }
999 
1000 /**
1001  * zynqmp_dma_runtime_resume - Runtime suspend method for the driver
1002  * @dev:	Address of the device structure
1003  *
1004  * Put the driver into low power mode.
1005  * Return: 0 always
1006  */
1007 static int __maybe_unused zynqmp_dma_runtime_resume(struct device *dev)
1008 {
1009 	struct zynqmp_dma_device *zdev = dev_get_drvdata(dev);
1010 	int err;
1011 
1012 	err = clk_prepare_enable(zdev->clk_main);
1013 	if (err) {
1014 		dev_err(dev, "Unable to enable main clock.\n");
1015 		return err;
1016 	}
1017 
1018 	err = clk_prepare_enable(zdev->clk_apb);
1019 	if (err) {
1020 		dev_err(dev, "Unable to enable apb clock.\n");
1021 		clk_disable_unprepare(zdev->clk_main);
1022 		return err;
1023 	}
1024 
1025 	return 0;
1026 }
1027 
1028 static const struct dev_pm_ops zynqmp_dma_dev_pm_ops = {
1029 	SET_SYSTEM_SLEEP_PM_OPS(zynqmp_dma_suspend, zynqmp_dma_resume)
1030 	SET_RUNTIME_PM_OPS(zynqmp_dma_runtime_suspend,
1031 			   zynqmp_dma_runtime_resume, NULL)
1032 };
1033 
1034 /**
1035  * zynqmp_dma_probe - Driver probe function
1036  * @pdev: Pointer to the platform_device structure
1037  *
1038  * Return: '0' on success and failure value on error
1039  */
1040 static int zynqmp_dma_probe(struct platform_device *pdev)
1041 {
1042 	struct zynqmp_dma_device *zdev;
1043 	struct dma_device *p;
1044 	int ret;
1045 
1046 	zdev = devm_kzalloc(&pdev->dev, sizeof(*zdev), GFP_KERNEL);
1047 	if (!zdev)
1048 		return -ENOMEM;
1049 
1050 	zdev->dev = &pdev->dev;
1051 	INIT_LIST_HEAD(&zdev->common.channels);
1052 
1053 	dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
1054 	dma_cap_set(DMA_MEMCPY, zdev->common.cap_mask);
1055 
1056 	p = &zdev->common;
1057 	p->device_prep_dma_memcpy = zynqmp_dma_prep_memcpy;
1058 	p->device_terminate_all = zynqmp_dma_device_terminate_all;
1059 	p->device_issue_pending = zynqmp_dma_issue_pending;
1060 	p->device_alloc_chan_resources = zynqmp_dma_alloc_chan_resources;
1061 	p->device_free_chan_resources = zynqmp_dma_free_chan_resources;
1062 	p->device_tx_status = dma_cookie_status;
1063 	p->device_config = zynqmp_dma_device_config;
1064 	p->dev = &pdev->dev;
1065 
1066 	zdev->clk_main = devm_clk_get(&pdev->dev, "clk_main");
1067 	if (IS_ERR(zdev->clk_main))
1068 		return dev_err_probe(&pdev->dev, PTR_ERR(zdev->clk_main),
1069 				     "main clock not found.\n");
1070 
1071 	zdev->clk_apb = devm_clk_get(&pdev->dev, "clk_apb");
1072 	if (IS_ERR(zdev->clk_apb))
1073 		return dev_err_probe(&pdev->dev, PTR_ERR(zdev->clk_apb),
1074 				     "apb clock not found.\n");
1075 
1076 	platform_set_drvdata(pdev, zdev);
1077 	pm_runtime_set_autosuspend_delay(zdev->dev, ZDMA_PM_TIMEOUT);
1078 	pm_runtime_use_autosuspend(zdev->dev);
1079 	pm_runtime_enable(zdev->dev);
1080 	pm_runtime_get_sync(zdev->dev);
1081 	if (!pm_runtime_enabled(zdev->dev)) {
1082 		ret = zynqmp_dma_runtime_resume(zdev->dev);
1083 		if (ret)
1084 			return ret;
1085 	}
1086 
1087 	ret = zynqmp_dma_chan_probe(zdev, pdev);
1088 	if (ret) {
1089 		dev_err_probe(&pdev->dev, ret, "Probing channel failed\n");
1090 		goto err_disable_pm;
1091 	}
1092 
1093 	p->dst_addr_widths = BIT(zdev->chan->bus_width / 8);
1094 	p->src_addr_widths = BIT(zdev->chan->bus_width / 8);
1095 
1096 	dma_async_device_register(&zdev->common);
1097 
1098 	ret = of_dma_controller_register(pdev->dev.of_node,
1099 					 of_zynqmp_dma_xlate, zdev);
1100 	if (ret) {
1101 		dev_err_probe(&pdev->dev, ret, "Unable to register DMA to DT\n");
1102 		dma_async_device_unregister(&zdev->common);
1103 		goto free_chan_resources;
1104 	}
1105 
1106 	pm_runtime_mark_last_busy(zdev->dev);
1107 	pm_runtime_put_sync_autosuspend(zdev->dev);
1108 
1109 	return 0;
1110 
1111 free_chan_resources:
1112 	zynqmp_dma_chan_remove(zdev->chan);
1113 err_disable_pm:
1114 	if (!pm_runtime_enabled(zdev->dev))
1115 		zynqmp_dma_runtime_suspend(zdev->dev);
1116 	pm_runtime_disable(zdev->dev);
1117 	return ret;
1118 }
1119 
1120 /**
1121  * zynqmp_dma_remove - Driver remove function
1122  * @pdev: Pointer to the platform_device structure
1123  *
1124  * Return: Always '0'
1125  */
1126 static int zynqmp_dma_remove(struct platform_device *pdev)
1127 {
1128 	struct zynqmp_dma_device *zdev = platform_get_drvdata(pdev);
1129 
1130 	of_dma_controller_free(pdev->dev.of_node);
1131 	dma_async_device_unregister(&zdev->common);
1132 
1133 	zynqmp_dma_chan_remove(zdev->chan);
1134 	pm_runtime_disable(zdev->dev);
1135 	if (!pm_runtime_enabled(zdev->dev))
1136 		zynqmp_dma_runtime_suspend(zdev->dev);
1137 
1138 	return 0;
1139 }
1140 
1141 static const struct of_device_id zynqmp_dma_of_match[] = {
1142 	{ .compatible = "xlnx,zynqmp-dma-1.0", },
1143 	{}
1144 };
1145 MODULE_DEVICE_TABLE(of, zynqmp_dma_of_match);
1146 
1147 static struct platform_driver zynqmp_dma_driver = {
1148 	.driver = {
1149 		.name = "xilinx-zynqmp-dma",
1150 		.of_match_table = zynqmp_dma_of_match,
1151 		.pm = &zynqmp_dma_dev_pm_ops,
1152 	},
1153 	.probe = zynqmp_dma_probe,
1154 	.remove = zynqmp_dma_remove,
1155 };
1156 
1157 module_platform_driver(zynqmp_dma_driver);
1158 
1159 MODULE_LICENSE("GPL");
1160 MODULE_AUTHOR("Xilinx, Inc.");
1161 MODULE_DESCRIPTION("Xilinx ZynqMP DMA driver");
1162