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