GNU Linux-libre 5.10.217-gnu1
[releases.git] / drivers / dma / xilinx / xilinx_dpdma.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Xilinx ZynqMP DPDMA Engine driver
4  *
5  * Copyright (C) 2015 - 2020 Xilinx, Inc.
6  *
7  * Author: Hyun Woo Kwon <hyun.kwon@xilinx.com>
8  */
9
10 #include <linux/bitfield.h>
11 #include <linux/bits.h>
12 #include <linux/clk.h>
13 #include <linux/debugfs.h>
14 #include <linux/delay.h>
15 #include <linux/dmaengine.h>
16 #include <linux/dmapool.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_dma.h>
21 #include <linux/platform_device.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/wait.h>
26
27 #include <dt-bindings/dma/xlnx-zynqmp-dpdma.h>
28
29 #include "../dmaengine.h"
30 #include "../virt-dma.h"
31
32 /* DPDMA registers */
33 #define XILINX_DPDMA_ERR_CTRL                           0x000
34 #define XILINX_DPDMA_ISR                                0x004
35 #define XILINX_DPDMA_IMR                                0x008
36 #define XILINX_DPDMA_IEN                                0x00c
37 #define XILINX_DPDMA_IDS                                0x010
38 #define XILINX_DPDMA_INTR_DESC_DONE(n)                  BIT((n) + 0)
39 #define XILINX_DPDMA_INTR_DESC_DONE_MASK                GENMASK(5, 0)
40 #define XILINX_DPDMA_INTR_NO_OSTAND(n)                  BIT((n) + 6)
41 #define XILINX_DPDMA_INTR_NO_OSTAND_MASK                GENMASK(11, 6)
42 #define XILINX_DPDMA_INTR_AXI_ERR(n)                    BIT((n) + 12)
43 #define XILINX_DPDMA_INTR_AXI_ERR_MASK                  GENMASK(17, 12)
44 #define XILINX_DPDMA_INTR_DESC_ERR(n)                   BIT((n) + 16)
45 #define XILINX_DPDMA_INTR_DESC_ERR_MASK                 GENMASK(23, 18)
46 #define XILINX_DPDMA_INTR_WR_CMD_FIFO_FULL              BIT(24)
47 #define XILINX_DPDMA_INTR_WR_DATA_FIFO_FULL             BIT(25)
48 #define XILINX_DPDMA_INTR_AXI_4K_CROSS                  BIT(26)
49 #define XILINX_DPDMA_INTR_VSYNC                         BIT(27)
50 #define XILINX_DPDMA_INTR_CHAN_ERR_MASK                 0x00041000
51 #define XILINX_DPDMA_INTR_CHAN_ERR                      0x00fff000
52 #define XILINX_DPDMA_INTR_GLOBAL_ERR                    0x07000000
53 #define XILINX_DPDMA_INTR_ERR_ALL                       0x07fff000
54 #define XILINX_DPDMA_INTR_CHAN_MASK                     0x00041041
55 #define XILINX_DPDMA_INTR_GLOBAL_MASK                   0x0f000000
56 #define XILINX_DPDMA_INTR_ALL                           0x0fffffff
57 #define XILINX_DPDMA_EISR                               0x014
58 #define XILINX_DPDMA_EIMR                               0x018
59 #define XILINX_DPDMA_EIEN                               0x01c
60 #define XILINX_DPDMA_EIDS                               0x020
61 #define XILINX_DPDMA_EINTR_INV_APB                      BIT(0)
62 #define XILINX_DPDMA_EINTR_RD_AXI_ERR(n)                BIT((n) + 1)
63 #define XILINX_DPDMA_EINTR_RD_AXI_ERR_MASK              GENMASK(6, 1)
64 #define XILINX_DPDMA_EINTR_PRE_ERR(n)                   BIT((n) + 7)
65 #define XILINX_DPDMA_EINTR_PRE_ERR_MASK                 GENMASK(12, 7)
66 #define XILINX_DPDMA_EINTR_CRC_ERR(n)                   BIT((n) + 13)
67 #define XILINX_DPDMA_EINTR_CRC_ERR_MASK                 GENMASK(18, 13)
68 #define XILINX_DPDMA_EINTR_WR_AXI_ERR(n)                BIT((n) + 19)
69 #define XILINX_DPDMA_EINTR_WR_AXI_ERR_MASK              GENMASK(24, 19)
70 #define XILINX_DPDMA_EINTR_DESC_DONE_ERR(n)             BIT((n) + 25)
71 #define XILINX_DPDMA_EINTR_DESC_DONE_ERR_MASK           GENMASK(30, 25)
72 #define XILINX_DPDMA_EINTR_RD_CMD_FIFO_FULL             BIT(32)
73 #define XILINX_DPDMA_EINTR_CHAN_ERR_MASK                0x02082082
74 #define XILINX_DPDMA_EINTR_CHAN_ERR                     0x7ffffffe
75 #define XILINX_DPDMA_EINTR_GLOBAL_ERR                   0x80000001
76 #define XILINX_DPDMA_EINTR_ALL                          0xffffffff
77 #define XILINX_DPDMA_CNTL                               0x100
78 #define XILINX_DPDMA_GBL                                0x104
79 #define XILINX_DPDMA_GBL_TRIG_MASK(n)                   ((n) << 0)
80 #define XILINX_DPDMA_GBL_RETRIG_MASK(n)                 ((n) << 6)
81 #define XILINX_DPDMA_ALC0_CNTL                          0x108
82 #define XILINX_DPDMA_ALC0_STATUS                        0x10c
83 #define XILINX_DPDMA_ALC0_MAX                           0x110
84 #define XILINX_DPDMA_ALC0_MIN                           0x114
85 #define XILINX_DPDMA_ALC0_ACC                           0x118
86 #define XILINX_DPDMA_ALC0_ACC_TRAN                      0x11c
87 #define XILINX_DPDMA_ALC1_CNTL                          0x120
88 #define XILINX_DPDMA_ALC1_STATUS                        0x124
89 #define XILINX_DPDMA_ALC1_MAX                           0x128
90 #define XILINX_DPDMA_ALC1_MIN                           0x12c
91 #define XILINX_DPDMA_ALC1_ACC                           0x130
92 #define XILINX_DPDMA_ALC1_ACC_TRAN                      0x134
93
94 /* Channel register */
95 #define XILINX_DPDMA_CH_BASE                            0x200
96 #define XILINX_DPDMA_CH_OFFSET                          0x100
97 #define XILINX_DPDMA_CH_DESC_START_ADDRE                0x000
98 #define XILINX_DPDMA_CH_DESC_START_ADDRE_MASK           GENMASK(15, 0)
99 #define XILINX_DPDMA_CH_DESC_START_ADDR                 0x004
100 #define XILINX_DPDMA_CH_DESC_NEXT_ADDRE                 0x008
101 #define XILINX_DPDMA_CH_DESC_NEXT_ADDR                  0x00c
102 #define XILINX_DPDMA_CH_PYLD_CUR_ADDRE                  0x010
103 #define XILINX_DPDMA_CH_PYLD_CUR_ADDR                   0x014
104 #define XILINX_DPDMA_CH_CNTL                            0x018
105 #define XILINX_DPDMA_CH_CNTL_ENABLE                     BIT(0)
106 #define XILINX_DPDMA_CH_CNTL_PAUSE                      BIT(1)
107 #define XILINX_DPDMA_CH_CNTL_QOS_DSCR_WR_MASK           GENMASK(5, 2)
108 #define XILINX_DPDMA_CH_CNTL_QOS_DSCR_RD_MASK           GENMASK(9, 6)
109 #define XILINX_DPDMA_CH_CNTL_QOS_DATA_RD_MASK           GENMASK(13, 10)
110 #define XILINX_DPDMA_CH_CNTL_QOS_VID_CLASS              11
111 #define XILINX_DPDMA_CH_STATUS                          0x01c
112 #define XILINX_DPDMA_CH_STATUS_OTRAN_CNT_MASK           GENMASK(24, 21)
113 #define XILINX_DPDMA_CH_VDO                             0x020
114 #define XILINX_DPDMA_CH_PYLD_SZ                         0x024
115 #define XILINX_DPDMA_CH_DESC_ID                         0x028
116 #define XILINX_DPDMA_CH_DESC_ID_MASK                    GENMASK(15, 0)
117
118 /* DPDMA descriptor fields */
119 #define XILINX_DPDMA_DESC_CONTROL_PREEMBLE              0xa5
120 #define XILINX_DPDMA_DESC_CONTROL_COMPLETE_INTR         BIT(8)
121 #define XILINX_DPDMA_DESC_CONTROL_DESC_UPDATE           BIT(9)
122 #define XILINX_DPDMA_DESC_CONTROL_IGNORE_DONE           BIT(10)
123 #define XILINX_DPDMA_DESC_CONTROL_FRAG_MODE             BIT(18)
124 #define XILINX_DPDMA_DESC_CONTROL_LAST                  BIT(19)
125 #define XILINX_DPDMA_DESC_CONTROL_ENABLE_CRC            BIT(20)
126 #define XILINX_DPDMA_DESC_CONTROL_LAST_OF_FRAME         BIT(21)
127 #define XILINX_DPDMA_DESC_ID_MASK                       GENMASK(15, 0)
128 #define XILINX_DPDMA_DESC_HSIZE_STRIDE_HSIZE_MASK       GENMASK(17, 0)
129 #define XILINX_DPDMA_DESC_HSIZE_STRIDE_STRIDE_MASK      GENMASK(31, 18)
130 #define XILINX_DPDMA_DESC_ADDR_EXT_NEXT_ADDR_MASK       GENMASK(15, 0)
131 #define XILINX_DPDMA_DESC_ADDR_EXT_SRC_ADDR_MASK        GENMASK(31, 16)
132
133 #define XILINX_DPDMA_ALIGN_BYTES                        256
134 #define XILINX_DPDMA_LINESIZE_ALIGN_BITS                128
135
136 #define XILINX_DPDMA_NUM_CHAN                           6
137
138 struct xilinx_dpdma_chan;
139
140 /**
141  * struct xilinx_dpdma_hw_desc - DPDMA hardware descriptor
142  * @control: control configuration field
143  * @desc_id: descriptor ID
144  * @xfer_size: transfer size
145  * @hsize_stride: horizontal size and stride
146  * @timestamp_lsb: LSB of time stamp
147  * @timestamp_msb: MSB of time stamp
148  * @addr_ext: upper 16 bit of 48 bit address (next_desc and src_addr)
149  * @next_desc: next descriptor 32 bit address
150  * @src_addr: payload source address (1st page, 32 LSB)
151  * @addr_ext_23: payload source address (3nd and 3rd pages, 16 LSBs)
152  * @addr_ext_45: payload source address (4th and 5th pages, 16 LSBs)
153  * @src_addr2: payload source address (2nd page, 32 LSB)
154  * @src_addr3: payload source address (3rd page, 32 LSB)
155  * @src_addr4: payload source address (4th page, 32 LSB)
156  * @src_addr5: payload source address (5th page, 32 LSB)
157  * @crc: descriptor CRC
158  */
159 struct xilinx_dpdma_hw_desc {
160         u32 control;
161         u32 desc_id;
162         u32 xfer_size;
163         u32 hsize_stride;
164         u32 timestamp_lsb;
165         u32 timestamp_msb;
166         u32 addr_ext;
167         u32 next_desc;
168         u32 src_addr;
169         u32 addr_ext_23;
170         u32 addr_ext_45;
171         u32 src_addr2;
172         u32 src_addr3;
173         u32 src_addr4;
174         u32 src_addr5;
175         u32 crc;
176 } __aligned(XILINX_DPDMA_ALIGN_BYTES);
177
178 /**
179  * struct xilinx_dpdma_sw_desc - DPDMA software descriptor
180  * @hw: DPDMA hardware descriptor
181  * @node: list node for software descriptors
182  * @dma_addr: DMA address of the software descriptor
183  */
184 struct xilinx_dpdma_sw_desc {
185         struct xilinx_dpdma_hw_desc hw;
186         struct list_head node;
187         dma_addr_t dma_addr;
188 };
189
190 /**
191  * struct xilinx_dpdma_tx_desc - DPDMA transaction descriptor
192  * @vdesc: virtual DMA descriptor
193  * @chan: DMA channel
194  * @descriptors: list of software descriptors
195  * @error: an error has been detected with this descriptor
196  */
197 struct xilinx_dpdma_tx_desc {
198         struct virt_dma_desc vdesc;
199         struct xilinx_dpdma_chan *chan;
200         struct list_head descriptors;
201         bool error;
202 };
203
204 #define to_dpdma_tx_desc(_desc) \
205         container_of(_desc, struct xilinx_dpdma_tx_desc, vdesc)
206
207 /**
208  * struct xilinx_dpdma_chan - DPDMA channel
209  * @vchan: virtual DMA channel
210  * @reg: register base address
211  * @id: channel ID
212  * @wait_to_stop: queue to wait for outstanding transacitons before stopping
213  * @running: true if the channel is running
214  * @first_frame: flag for the first frame of stream
215  * @video_group: flag if multi-channel operation is needed for video channels
216  * @lock: lock to access struct xilinx_dpdma_chan. Must be taken before
217  *        @vchan.lock, if both are to be held.
218  * @desc_pool: descriptor allocation pool
219  * @err_task: error IRQ bottom half handler
220  * @desc: References to descriptors being processed
221  * @desc.pending: Descriptor schedule to the hardware, pending execution
222  * @desc.active: Descriptor being executed by the hardware
223  * @xdev: DPDMA device
224  */
225 struct xilinx_dpdma_chan {
226         struct virt_dma_chan vchan;
227         void __iomem *reg;
228         unsigned int id;
229
230         wait_queue_head_t wait_to_stop;
231         bool running;
232         bool first_frame;
233         bool video_group;
234
235         spinlock_t lock; /* lock to access struct xilinx_dpdma_chan */
236         struct dma_pool *desc_pool;
237         struct tasklet_struct err_task;
238
239         struct {
240                 struct xilinx_dpdma_tx_desc *pending;
241                 struct xilinx_dpdma_tx_desc *active;
242         } desc;
243
244         struct xilinx_dpdma_device *xdev;
245 };
246
247 #define to_xilinx_chan(_chan) \
248         container_of(_chan, struct xilinx_dpdma_chan, vchan.chan)
249
250 /**
251  * struct xilinx_dpdma_device - DPDMA device
252  * @common: generic dma device structure
253  * @reg: register base address
254  * @dev: generic device structure
255  * @irq: the interrupt number
256  * @axi_clk: axi clock
257  * @chan: DPDMA channels
258  * @ext_addr: flag for 64 bit system (48 bit addressing)
259  */
260 struct xilinx_dpdma_device {
261         struct dma_device common;
262         void __iomem *reg;
263         struct device *dev;
264         int irq;
265
266         struct clk *axi_clk;
267         struct xilinx_dpdma_chan *chan[XILINX_DPDMA_NUM_CHAN];
268
269         bool ext_addr;
270 };
271
272 /* -----------------------------------------------------------------------------
273  * DebugFS
274  */
275
276 #ifdef CONFIG_DEBUG_FS
277
278 #define XILINX_DPDMA_DEBUGFS_READ_MAX_SIZE      32
279 #define XILINX_DPDMA_DEBUGFS_UINT16_MAX_STR     "65535"
280
281 /* Match xilinx_dpdma_testcases vs dpdma_debugfs_reqs[] entry */
282 enum xilinx_dpdma_testcases {
283         DPDMA_TC_INTR_DONE,
284         DPDMA_TC_NONE
285 };
286
287 struct xilinx_dpdma_debugfs {
288         enum xilinx_dpdma_testcases testcase;
289         u16 xilinx_dpdma_irq_done_count;
290         unsigned int chan_id;
291 };
292
293 static struct xilinx_dpdma_debugfs dpdma_debugfs;
294 struct xilinx_dpdma_debugfs_request {
295         const char *name;
296         enum xilinx_dpdma_testcases tc;
297         ssize_t (*read)(char *buf);
298         int (*write)(char *args);
299 };
300
301 static void xilinx_dpdma_debugfs_desc_done_irq(struct xilinx_dpdma_chan *chan)
302 {
303         if (chan->id == dpdma_debugfs.chan_id)
304                 dpdma_debugfs.xilinx_dpdma_irq_done_count++;
305 }
306
307 static ssize_t xilinx_dpdma_debugfs_desc_done_irq_read(char *buf)
308 {
309         size_t out_str_len;
310
311         dpdma_debugfs.testcase = DPDMA_TC_NONE;
312
313         out_str_len = strlen(XILINX_DPDMA_DEBUGFS_UINT16_MAX_STR);
314         out_str_len = min_t(size_t, XILINX_DPDMA_DEBUGFS_READ_MAX_SIZE,
315                             out_str_len);
316         snprintf(buf, out_str_len, "%d",
317                  dpdma_debugfs.xilinx_dpdma_irq_done_count);
318
319         return 0;
320 }
321
322 static int xilinx_dpdma_debugfs_desc_done_irq_write(char *args)
323 {
324         char *arg;
325         int ret;
326         u32 id;
327
328         arg = strsep(&args, " ");
329         if (!arg || strncasecmp(arg, "start", 5))
330                 return -EINVAL;
331
332         arg = strsep(&args, " ");
333         if (!arg)
334                 return -EINVAL;
335
336         ret = kstrtou32(arg, 0, &id);
337         if (ret < 0)
338                 return ret;
339
340         if (id < ZYNQMP_DPDMA_VIDEO0 || id > ZYNQMP_DPDMA_AUDIO1)
341                 return -EINVAL;
342
343         dpdma_debugfs.testcase = DPDMA_TC_INTR_DONE;
344         dpdma_debugfs.xilinx_dpdma_irq_done_count = 0;
345         dpdma_debugfs.chan_id = id;
346
347         return 0;
348 }
349
350 /* Match xilinx_dpdma_testcases vs dpdma_debugfs_reqs[] entry */
351 static struct xilinx_dpdma_debugfs_request dpdma_debugfs_reqs[] = {
352         {
353                 .name = "DESCRIPTOR_DONE_INTR",
354                 .tc = DPDMA_TC_INTR_DONE,
355                 .read = xilinx_dpdma_debugfs_desc_done_irq_read,
356                 .write = xilinx_dpdma_debugfs_desc_done_irq_write,
357         },
358 };
359
360 static ssize_t xilinx_dpdma_debugfs_read(struct file *f, char __user *buf,
361                                          size_t size, loff_t *pos)
362 {
363         enum xilinx_dpdma_testcases testcase;
364         char *kern_buff;
365         int ret = 0;
366
367         if (*pos != 0 || size <= 0)
368                 return -EINVAL;
369
370         kern_buff = kzalloc(XILINX_DPDMA_DEBUGFS_READ_MAX_SIZE, GFP_KERNEL);
371         if (!kern_buff) {
372                 dpdma_debugfs.testcase = DPDMA_TC_NONE;
373                 return -ENOMEM;
374         }
375
376         testcase = READ_ONCE(dpdma_debugfs.testcase);
377         if (testcase != DPDMA_TC_NONE) {
378                 ret = dpdma_debugfs_reqs[testcase].read(kern_buff);
379                 if (ret < 0)
380                         goto done;
381         } else {
382                 strlcpy(kern_buff, "No testcase executed",
383                         XILINX_DPDMA_DEBUGFS_READ_MAX_SIZE);
384         }
385
386         size = min(size, strlen(kern_buff));
387         if (copy_to_user(buf, kern_buff, size))
388                 ret = -EFAULT;
389
390 done:
391         kfree(kern_buff);
392         if (ret)
393                 return ret;
394
395         *pos = size + 1;
396         return size;
397 }
398
399 static ssize_t xilinx_dpdma_debugfs_write(struct file *f,
400                                           const char __user *buf, size_t size,
401                                           loff_t *pos)
402 {
403         char *kern_buff, *kern_buff_start;
404         char *testcase;
405         unsigned int i;
406         int ret;
407
408         if (*pos != 0 || size <= 0)
409                 return -EINVAL;
410
411         /* Supporting single instance of test as of now. */
412         if (dpdma_debugfs.testcase != DPDMA_TC_NONE)
413                 return -EBUSY;
414
415         kern_buff = kzalloc(size, GFP_KERNEL);
416         if (!kern_buff)
417                 return -ENOMEM;
418         kern_buff_start = kern_buff;
419
420         ret = strncpy_from_user(kern_buff, buf, size);
421         if (ret < 0)
422                 goto done;
423
424         /* Read the testcase name from a user request. */
425         testcase = strsep(&kern_buff, " ");
426
427         for (i = 0; i < ARRAY_SIZE(dpdma_debugfs_reqs); i++) {
428                 if (!strcasecmp(testcase, dpdma_debugfs_reqs[i].name))
429                         break;
430         }
431
432         if (i == ARRAY_SIZE(dpdma_debugfs_reqs)) {
433                 ret = -EINVAL;
434                 goto done;
435         }
436
437         ret = dpdma_debugfs_reqs[i].write(kern_buff);
438         if (ret < 0)
439                 goto done;
440
441         ret = size;
442
443 done:
444         kfree(kern_buff_start);
445         return ret;
446 }
447
448 static const struct file_operations fops_xilinx_dpdma_dbgfs = {
449         .owner = THIS_MODULE,
450         .read = xilinx_dpdma_debugfs_read,
451         .write = xilinx_dpdma_debugfs_write,
452 };
453
454 static void xilinx_dpdma_debugfs_init(struct xilinx_dpdma_device *xdev)
455 {
456         struct dentry *dent;
457
458         dpdma_debugfs.testcase = DPDMA_TC_NONE;
459
460         dent = debugfs_create_file("testcase", 0444, xdev->common.dbg_dev_root,
461                                    NULL, &fops_xilinx_dpdma_dbgfs);
462         if (IS_ERR(dent))
463                 dev_err(xdev->dev, "Failed to create debugfs testcase file\n");
464 }
465
466 #else
467 static void xilinx_dpdma_debugfs_init(struct xilinx_dpdma_device *xdev)
468 {
469 }
470
471 static void xilinx_dpdma_debugfs_desc_done_irq(struct xilinx_dpdma_chan *chan)
472 {
473 }
474 #endif /* CONFIG_DEBUG_FS */
475
476 /* -----------------------------------------------------------------------------
477  * I/O Accessors
478  */
479
480 static inline u32 dpdma_read(void __iomem *base, u32 offset)
481 {
482         return ioread32(base + offset);
483 }
484
485 static inline void dpdma_write(void __iomem *base, u32 offset, u32 val)
486 {
487         iowrite32(val, base + offset);
488 }
489
490 static inline void dpdma_clr(void __iomem *base, u32 offset, u32 clr)
491 {
492         dpdma_write(base, offset, dpdma_read(base, offset) & ~clr);
493 }
494
495 static inline void dpdma_set(void __iomem *base, u32 offset, u32 set)
496 {
497         dpdma_write(base, offset, dpdma_read(base, offset) | set);
498 }
499
500 /* -----------------------------------------------------------------------------
501  * Descriptor Operations
502  */
503
504 /**
505  * xilinx_dpdma_sw_desc_set_dma_addrs - Set DMA addresses in the descriptor
506  * @xdev: DPDMA device
507  * @sw_desc: The software descriptor in which to set DMA addresses
508  * @prev: The previous descriptor
509  * @dma_addr: array of dma addresses
510  * @num_src_addr: number of addresses in @dma_addr
511  *
512  * Set all the DMA addresses in the hardware descriptor corresponding to @dev
513  * from @dma_addr. If a previous descriptor is specified in @prev, its next
514  * descriptor DMA address is set to the DMA address of @sw_desc. @prev may be
515  * identical to @sw_desc for cyclic transfers.
516  */
517 static void xilinx_dpdma_sw_desc_set_dma_addrs(struct xilinx_dpdma_device *xdev,
518                                                struct xilinx_dpdma_sw_desc *sw_desc,
519                                                struct xilinx_dpdma_sw_desc *prev,
520                                                dma_addr_t dma_addr[],
521                                                unsigned int num_src_addr)
522 {
523         struct xilinx_dpdma_hw_desc *hw_desc = &sw_desc->hw;
524         unsigned int i;
525
526         hw_desc->src_addr = lower_32_bits(dma_addr[0]);
527         if (xdev->ext_addr)
528                 hw_desc->addr_ext |=
529                         FIELD_PREP(XILINX_DPDMA_DESC_ADDR_EXT_SRC_ADDR_MASK,
530                                    upper_32_bits(dma_addr[0]));
531
532         for (i = 1; i < num_src_addr; i++) {
533                 u32 *addr = &hw_desc->src_addr2;
534
535                 addr[i-1] = lower_32_bits(dma_addr[i]);
536
537                 if (xdev->ext_addr) {
538                         u32 *addr_ext = &hw_desc->addr_ext_23;
539                         u32 addr_msb;
540
541                         addr_msb = upper_32_bits(dma_addr[i]) & GENMASK(15, 0);
542                         addr_msb <<= 16 * ((i - 1) % 2);
543                         addr_ext[(i - 1) / 2] |= addr_msb;
544                 }
545         }
546
547         if (!prev)
548                 return;
549
550         prev->hw.next_desc = lower_32_bits(sw_desc->dma_addr);
551         if (xdev->ext_addr)
552                 prev->hw.addr_ext |=
553                         FIELD_PREP(XILINX_DPDMA_DESC_ADDR_EXT_NEXT_ADDR_MASK,
554                                    upper_32_bits(sw_desc->dma_addr));
555 }
556
557 /**
558  * xilinx_dpdma_chan_alloc_sw_desc - Allocate a software descriptor
559  * @chan: DPDMA channel
560  *
561  * Allocate a software descriptor from the channel's descriptor pool.
562  *
563  * Return: a software descriptor or NULL.
564  */
565 static struct xilinx_dpdma_sw_desc *
566 xilinx_dpdma_chan_alloc_sw_desc(struct xilinx_dpdma_chan *chan)
567 {
568         struct xilinx_dpdma_sw_desc *sw_desc;
569         dma_addr_t dma_addr;
570
571         sw_desc = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &dma_addr);
572         if (!sw_desc)
573                 return NULL;
574
575         sw_desc->dma_addr = dma_addr;
576
577         return sw_desc;
578 }
579
580 /**
581  * xilinx_dpdma_chan_free_sw_desc - Free a software descriptor
582  * @chan: DPDMA channel
583  * @sw_desc: software descriptor to free
584  *
585  * Free a software descriptor from the channel's descriptor pool.
586  */
587 static void
588 xilinx_dpdma_chan_free_sw_desc(struct xilinx_dpdma_chan *chan,
589                                struct xilinx_dpdma_sw_desc *sw_desc)
590 {
591         dma_pool_free(chan->desc_pool, sw_desc, sw_desc->dma_addr);
592 }
593
594 /**
595  * xilinx_dpdma_chan_dump_tx_desc - Dump a tx descriptor
596  * @chan: DPDMA channel
597  * @tx_desc: tx descriptor to dump
598  *
599  * Dump contents of a tx descriptor
600  */
601 static void xilinx_dpdma_chan_dump_tx_desc(struct xilinx_dpdma_chan *chan,
602                                            struct xilinx_dpdma_tx_desc *tx_desc)
603 {
604         struct xilinx_dpdma_sw_desc *sw_desc;
605         struct device *dev = chan->xdev->dev;
606         unsigned int i = 0;
607
608         dev_dbg(dev, "------- TX descriptor dump start -------\n");
609         dev_dbg(dev, "------- channel ID = %d -------\n", chan->id);
610
611         list_for_each_entry(sw_desc, &tx_desc->descriptors, node) {
612                 struct xilinx_dpdma_hw_desc *hw_desc = &sw_desc->hw;
613
614                 dev_dbg(dev, "------- HW descriptor %d -------\n", i++);
615                 dev_dbg(dev, "descriptor DMA addr: %pad\n", &sw_desc->dma_addr);
616                 dev_dbg(dev, "control: 0x%08x\n", hw_desc->control);
617                 dev_dbg(dev, "desc_id: 0x%08x\n", hw_desc->desc_id);
618                 dev_dbg(dev, "xfer_size: 0x%08x\n", hw_desc->xfer_size);
619                 dev_dbg(dev, "hsize_stride: 0x%08x\n", hw_desc->hsize_stride);
620                 dev_dbg(dev, "timestamp_lsb: 0x%08x\n", hw_desc->timestamp_lsb);
621                 dev_dbg(dev, "timestamp_msb: 0x%08x\n", hw_desc->timestamp_msb);
622                 dev_dbg(dev, "addr_ext: 0x%08x\n", hw_desc->addr_ext);
623                 dev_dbg(dev, "next_desc: 0x%08x\n", hw_desc->next_desc);
624                 dev_dbg(dev, "src_addr: 0x%08x\n", hw_desc->src_addr);
625                 dev_dbg(dev, "addr_ext_23: 0x%08x\n", hw_desc->addr_ext_23);
626                 dev_dbg(dev, "addr_ext_45: 0x%08x\n", hw_desc->addr_ext_45);
627                 dev_dbg(dev, "src_addr2: 0x%08x\n", hw_desc->src_addr2);
628                 dev_dbg(dev, "src_addr3: 0x%08x\n", hw_desc->src_addr3);
629                 dev_dbg(dev, "src_addr4: 0x%08x\n", hw_desc->src_addr4);
630                 dev_dbg(dev, "src_addr5: 0x%08x\n", hw_desc->src_addr5);
631                 dev_dbg(dev, "crc: 0x%08x\n", hw_desc->crc);
632         }
633
634         dev_dbg(dev, "------- TX descriptor dump end -------\n");
635 }
636
637 /**
638  * xilinx_dpdma_chan_alloc_tx_desc - Allocate a transaction descriptor
639  * @chan: DPDMA channel
640  *
641  * Allocate a tx descriptor.
642  *
643  * Return: a tx descriptor or NULL.
644  */
645 static struct xilinx_dpdma_tx_desc *
646 xilinx_dpdma_chan_alloc_tx_desc(struct xilinx_dpdma_chan *chan)
647 {
648         struct xilinx_dpdma_tx_desc *tx_desc;
649
650         tx_desc = kzalloc(sizeof(*tx_desc), GFP_NOWAIT);
651         if (!tx_desc)
652                 return NULL;
653
654         INIT_LIST_HEAD(&tx_desc->descriptors);
655         tx_desc->chan = chan;
656         tx_desc->error = false;
657
658         return tx_desc;
659 }
660
661 /**
662  * xilinx_dpdma_chan_free_tx_desc - Free a virtual DMA descriptor
663  * @vdesc: virtual DMA descriptor
664  *
665  * Free the virtual DMA descriptor @vdesc including its software descriptors.
666  */
667 static void xilinx_dpdma_chan_free_tx_desc(struct virt_dma_desc *vdesc)
668 {
669         struct xilinx_dpdma_sw_desc *sw_desc, *next;
670         struct xilinx_dpdma_tx_desc *desc;
671
672         if (!vdesc)
673                 return;
674
675         desc = to_dpdma_tx_desc(vdesc);
676
677         list_for_each_entry_safe(sw_desc, next, &desc->descriptors, node) {
678                 list_del(&sw_desc->node);
679                 xilinx_dpdma_chan_free_sw_desc(desc->chan, sw_desc);
680         }
681
682         kfree(desc);
683 }
684
685 /**
686  * xilinx_dpdma_chan_prep_interleaved_dma - Prepare an interleaved dma
687  *                                          descriptor
688  * @chan: DPDMA channel
689  * @xt: dma interleaved template
690  *
691  * Prepare a tx descriptor including internal software/hardware descriptors
692  * based on @xt.
693  *
694  * Return: A DPDMA TX descriptor on success, or NULL.
695  */
696 static struct xilinx_dpdma_tx_desc *
697 xilinx_dpdma_chan_prep_interleaved_dma(struct xilinx_dpdma_chan *chan,
698                                        struct dma_interleaved_template *xt)
699 {
700         struct xilinx_dpdma_tx_desc *tx_desc;
701         struct xilinx_dpdma_sw_desc *sw_desc;
702         struct xilinx_dpdma_hw_desc *hw_desc;
703         size_t hsize = xt->sgl[0].size;
704         size_t stride = hsize + xt->sgl[0].icg;
705
706         if (!IS_ALIGNED(xt->src_start, XILINX_DPDMA_ALIGN_BYTES)) {
707                 dev_err(chan->xdev->dev, "buffer should be aligned at %d B\n",
708                         XILINX_DPDMA_ALIGN_BYTES);
709                 return NULL;
710         }
711
712         tx_desc = xilinx_dpdma_chan_alloc_tx_desc(chan);
713         if (!tx_desc)
714                 return NULL;
715
716         sw_desc = xilinx_dpdma_chan_alloc_sw_desc(chan);
717         if (!sw_desc) {
718                 xilinx_dpdma_chan_free_tx_desc(&tx_desc->vdesc);
719                 return NULL;
720         }
721
722         xilinx_dpdma_sw_desc_set_dma_addrs(chan->xdev, sw_desc, sw_desc,
723                                            &xt->src_start, 1);
724
725         hw_desc = &sw_desc->hw;
726         hsize = ALIGN(hsize, XILINX_DPDMA_LINESIZE_ALIGN_BITS / 8);
727         hw_desc->xfer_size = hsize * xt->numf;
728         hw_desc->hsize_stride =
729                 FIELD_PREP(XILINX_DPDMA_DESC_HSIZE_STRIDE_HSIZE_MASK, hsize) |
730                 FIELD_PREP(XILINX_DPDMA_DESC_HSIZE_STRIDE_STRIDE_MASK,
731                            stride / 16);
732         hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_PREEMBLE;
733         hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_COMPLETE_INTR;
734         hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_IGNORE_DONE;
735         hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_LAST_OF_FRAME;
736
737         list_add_tail(&sw_desc->node, &tx_desc->descriptors);
738
739         return tx_desc;
740 }
741
742 /* -----------------------------------------------------------------------------
743  * DPDMA Channel Operations
744  */
745
746 /**
747  * xilinx_dpdma_chan_enable - Enable the channel
748  * @chan: DPDMA channel
749  *
750  * Enable the channel and its interrupts. Set the QoS values for video class.
751  */
752 static void xilinx_dpdma_chan_enable(struct xilinx_dpdma_chan *chan)
753 {
754         u32 reg;
755
756         reg = (XILINX_DPDMA_INTR_CHAN_MASK << chan->id)
757             | XILINX_DPDMA_INTR_GLOBAL_MASK;
758         dpdma_write(chan->xdev->reg, XILINX_DPDMA_IEN, reg);
759         reg = (XILINX_DPDMA_EINTR_CHAN_ERR_MASK << chan->id)
760             | XILINX_DPDMA_INTR_GLOBAL_ERR;
761         dpdma_write(chan->xdev->reg, XILINX_DPDMA_EIEN, reg);
762
763         reg = XILINX_DPDMA_CH_CNTL_ENABLE
764             | FIELD_PREP(XILINX_DPDMA_CH_CNTL_QOS_DSCR_WR_MASK,
765                          XILINX_DPDMA_CH_CNTL_QOS_VID_CLASS)
766             | FIELD_PREP(XILINX_DPDMA_CH_CNTL_QOS_DSCR_RD_MASK,
767                          XILINX_DPDMA_CH_CNTL_QOS_VID_CLASS)
768             | FIELD_PREP(XILINX_DPDMA_CH_CNTL_QOS_DATA_RD_MASK,
769                          XILINX_DPDMA_CH_CNTL_QOS_VID_CLASS);
770         dpdma_set(chan->reg, XILINX_DPDMA_CH_CNTL, reg);
771 }
772
773 /**
774  * xilinx_dpdma_chan_disable - Disable the channel
775  * @chan: DPDMA channel
776  *
777  * Disable the channel and its interrupts.
778  */
779 static void xilinx_dpdma_chan_disable(struct xilinx_dpdma_chan *chan)
780 {
781         u32 reg;
782
783         reg = XILINX_DPDMA_INTR_CHAN_MASK << chan->id;
784         dpdma_write(chan->xdev->reg, XILINX_DPDMA_IEN, reg);
785         reg = XILINX_DPDMA_EINTR_CHAN_ERR_MASK << chan->id;
786         dpdma_write(chan->xdev->reg, XILINX_DPDMA_EIEN, reg);
787
788         dpdma_clr(chan->reg, XILINX_DPDMA_CH_CNTL, XILINX_DPDMA_CH_CNTL_ENABLE);
789 }
790
791 /**
792  * xilinx_dpdma_chan_pause - Pause the channel
793  * @chan: DPDMA channel
794  *
795  * Pause the channel.
796  */
797 static void xilinx_dpdma_chan_pause(struct xilinx_dpdma_chan *chan)
798 {
799         dpdma_set(chan->reg, XILINX_DPDMA_CH_CNTL, XILINX_DPDMA_CH_CNTL_PAUSE);
800 }
801
802 /**
803  * xilinx_dpdma_chan_unpause - Unpause the channel
804  * @chan: DPDMA channel
805  *
806  * Unpause the channel.
807  */
808 static void xilinx_dpdma_chan_unpause(struct xilinx_dpdma_chan *chan)
809 {
810         dpdma_clr(chan->reg, XILINX_DPDMA_CH_CNTL, XILINX_DPDMA_CH_CNTL_PAUSE);
811 }
812
813 static u32 xilinx_dpdma_chan_video_group_ready(struct xilinx_dpdma_chan *chan)
814 {
815         struct xilinx_dpdma_device *xdev = chan->xdev;
816         u32 channels = 0;
817         unsigned int i;
818
819         for (i = ZYNQMP_DPDMA_VIDEO0; i <= ZYNQMP_DPDMA_VIDEO2; i++) {
820                 if (xdev->chan[i]->video_group && !xdev->chan[i]->running)
821                         return 0;
822
823                 if (xdev->chan[i]->video_group)
824                         channels |= BIT(i);
825         }
826
827         return channels;
828 }
829
830 /**
831  * xilinx_dpdma_chan_queue_transfer - Queue the next transfer
832  * @chan: DPDMA channel
833  *
834  * Queue the next descriptor, if any, to the hardware. If the channel is
835  * stopped, start it first. Otherwise retrigger it with the next descriptor.
836  */
837 static void xilinx_dpdma_chan_queue_transfer(struct xilinx_dpdma_chan *chan)
838 {
839         struct xilinx_dpdma_device *xdev = chan->xdev;
840         struct xilinx_dpdma_sw_desc *sw_desc;
841         struct xilinx_dpdma_tx_desc *desc;
842         struct virt_dma_desc *vdesc;
843         u32 reg, channels;
844         bool first_frame;
845
846         lockdep_assert_held(&chan->lock);
847
848         if (chan->desc.pending)
849                 return;
850
851         if (!chan->running) {
852                 xilinx_dpdma_chan_unpause(chan);
853                 xilinx_dpdma_chan_enable(chan);
854                 chan->first_frame = true;
855                 chan->running = true;
856         }
857
858         vdesc = vchan_next_desc(&chan->vchan);
859         if (!vdesc)
860                 return;
861
862         desc = to_dpdma_tx_desc(vdesc);
863         chan->desc.pending = desc;
864         list_del(&desc->vdesc.node);
865
866         /*
867          * Assign the cookie to descriptors in this transaction. Only 16 bit
868          * will be used, but it should be enough.
869          */
870         list_for_each_entry(sw_desc, &desc->descriptors, node)
871                 sw_desc->hw.desc_id = desc->vdesc.tx.cookie
872                                     & XILINX_DPDMA_CH_DESC_ID_MASK;
873
874         sw_desc = list_first_entry(&desc->descriptors,
875                                    struct xilinx_dpdma_sw_desc, node);
876         dpdma_write(chan->reg, XILINX_DPDMA_CH_DESC_START_ADDR,
877                     lower_32_bits(sw_desc->dma_addr));
878         if (xdev->ext_addr)
879                 dpdma_write(chan->reg, XILINX_DPDMA_CH_DESC_START_ADDRE,
880                             FIELD_PREP(XILINX_DPDMA_CH_DESC_START_ADDRE_MASK,
881                                        upper_32_bits(sw_desc->dma_addr)));
882
883         first_frame = chan->first_frame;
884         chan->first_frame = false;
885
886         if (chan->video_group) {
887                 channels = xilinx_dpdma_chan_video_group_ready(chan);
888                 /*
889                  * Trigger the transfer only when all channels in the group are
890                  * ready.
891                  */
892                 if (!channels)
893                         return;
894         } else {
895                 channels = BIT(chan->id);
896         }
897
898         if (first_frame)
899                 reg = XILINX_DPDMA_GBL_TRIG_MASK(channels);
900         else
901                 reg = XILINX_DPDMA_GBL_RETRIG_MASK(channels);
902
903         dpdma_write(xdev->reg, XILINX_DPDMA_GBL, reg);
904 }
905
906 /**
907  * xilinx_dpdma_chan_ostand - Number of outstanding transactions
908  * @chan: DPDMA channel
909  *
910  * Read and return the number of outstanding transactions from register.
911  *
912  * Return: Number of outstanding transactions from the status register.
913  */
914 static u32 xilinx_dpdma_chan_ostand(struct xilinx_dpdma_chan *chan)
915 {
916         return FIELD_GET(XILINX_DPDMA_CH_STATUS_OTRAN_CNT_MASK,
917                          dpdma_read(chan->reg, XILINX_DPDMA_CH_STATUS));
918 }
919
920 /**
921  * xilinx_dpdma_chan_no_ostand - Notify no outstanding transaction event
922  * @chan: DPDMA channel
923  *
924  * Notify waiters for no outstanding event, so waiters can stop the channel
925  * safely. This function is supposed to be called when 'no outstanding'
926  * interrupt is generated. The 'no outstanding' interrupt is disabled and
927  * should be re-enabled when this event is handled. If the channel status
928  * register still shows some number of outstanding transactions, the interrupt
929  * remains enabled.
930  *
931  * Return: 0 on success. On failure, -EWOULDBLOCK if there's still outstanding
932  * transaction(s).
933  */
934 static int xilinx_dpdma_chan_notify_no_ostand(struct xilinx_dpdma_chan *chan)
935 {
936         u32 cnt;
937
938         cnt = xilinx_dpdma_chan_ostand(chan);
939         if (cnt) {
940                 dev_dbg(chan->xdev->dev, "%d outstanding transactions\n", cnt);
941                 return -EWOULDBLOCK;
942         }
943
944         /* Disable 'no outstanding' interrupt */
945         dpdma_write(chan->xdev->reg, XILINX_DPDMA_IDS,
946                     XILINX_DPDMA_INTR_NO_OSTAND(chan->id));
947         wake_up(&chan->wait_to_stop);
948
949         return 0;
950 }
951
952 /**
953  * xilinx_dpdma_chan_wait_no_ostand - Wait for the no outstanding irq
954  * @chan: DPDMA channel
955  *
956  * Wait for the no outstanding transaction interrupt. This functions can sleep
957  * for 50ms.
958  *
959  * Return: 0 on success. On failure, -ETIMEOUT for time out, or the error code
960  * from wait_event_interruptible_timeout().
961  */
962 static int xilinx_dpdma_chan_wait_no_ostand(struct xilinx_dpdma_chan *chan)
963 {
964         int ret;
965
966         /* Wait for a no outstanding transaction interrupt upto 50msec */
967         ret = wait_event_interruptible_timeout(chan->wait_to_stop,
968                                                !xilinx_dpdma_chan_ostand(chan),
969                                                msecs_to_jiffies(50));
970         if (ret > 0) {
971                 dpdma_write(chan->xdev->reg, XILINX_DPDMA_IEN,
972                             XILINX_DPDMA_INTR_NO_OSTAND(chan->id));
973                 return 0;
974         }
975
976         dev_err(chan->xdev->dev, "not ready to stop: %d trans\n",
977                 xilinx_dpdma_chan_ostand(chan));
978
979         if (ret == 0)
980                 return -ETIMEDOUT;
981
982         return ret;
983 }
984
985 /**
986  * xilinx_dpdma_chan_poll_no_ostand - Poll the outstanding transaction status
987  * @chan: DPDMA channel
988  *
989  * Poll the outstanding transaction status, and return when there's no
990  * outstanding transaction. This functions can be used in the interrupt context
991  * or where the atomicity is required. Calling thread may wait more than 50ms.
992  *
993  * Return: 0 on success, or -ETIMEDOUT.
994  */
995 static int xilinx_dpdma_chan_poll_no_ostand(struct xilinx_dpdma_chan *chan)
996 {
997         u32 cnt, loop = 50000;
998
999         /* Poll at least for 50ms (20 fps). */
1000         do {
1001                 cnt = xilinx_dpdma_chan_ostand(chan);
1002                 udelay(1);
1003         } while (loop-- > 0 && cnt);
1004
1005         if (loop) {
1006                 dpdma_write(chan->xdev->reg, XILINX_DPDMA_IEN,
1007                             XILINX_DPDMA_INTR_NO_OSTAND(chan->id));
1008                 return 0;
1009         }
1010
1011         dev_err(chan->xdev->dev, "not ready to stop: %d trans\n",
1012                 xilinx_dpdma_chan_ostand(chan));
1013
1014         return -ETIMEDOUT;
1015 }
1016
1017 /**
1018  * xilinx_dpdma_chan_stop - Stop the channel
1019  * @chan: DPDMA channel
1020  *
1021  * Stop a previously paused channel by first waiting for completion of all
1022  * outstanding transaction and then disabling the channel.
1023  *
1024  * Return: 0 on success, or -ETIMEDOUT if the channel failed to stop.
1025  */
1026 static int xilinx_dpdma_chan_stop(struct xilinx_dpdma_chan *chan)
1027 {
1028         unsigned long flags;
1029         int ret;
1030
1031         ret = xilinx_dpdma_chan_wait_no_ostand(chan);
1032         if (ret)
1033                 return ret;
1034
1035         spin_lock_irqsave(&chan->lock, flags);
1036         xilinx_dpdma_chan_disable(chan);
1037         chan->running = false;
1038         spin_unlock_irqrestore(&chan->lock, flags);
1039
1040         return 0;
1041 }
1042
1043 /**
1044  * xilinx_dpdma_chan_done_irq - Handle hardware descriptor completion
1045  * @chan: DPDMA channel
1046  *
1047  * Handle completion of the currently active descriptor (@chan->desc.active). As
1048  * we currently support cyclic transfers only, this just invokes the cyclic
1049  * callback. The descriptor will be completed at the VSYNC interrupt when a new
1050  * descriptor replaces it.
1051  */
1052 static void xilinx_dpdma_chan_done_irq(struct xilinx_dpdma_chan *chan)
1053 {
1054         struct xilinx_dpdma_tx_desc *active;
1055         unsigned long flags;
1056
1057         spin_lock_irqsave(&chan->lock, flags);
1058
1059         xilinx_dpdma_debugfs_desc_done_irq(chan);
1060
1061         active = chan->desc.active;
1062         if (active)
1063                 vchan_cyclic_callback(&active->vdesc);
1064         else
1065                 dev_warn(chan->xdev->dev,
1066                          "DONE IRQ with no active descriptor!\n");
1067
1068         spin_unlock_irqrestore(&chan->lock, flags);
1069 }
1070
1071 /**
1072  * xilinx_dpdma_chan_vsync_irq - Handle hardware descriptor scheduling
1073  * @chan: DPDMA channel
1074  *
1075  * At VSYNC the active descriptor may have been replaced by the pending
1076  * descriptor. Detect this through the DESC_ID and perform appropriate
1077  * bookkeeping.
1078  */
1079 static void xilinx_dpdma_chan_vsync_irq(struct  xilinx_dpdma_chan *chan)
1080 {
1081         struct xilinx_dpdma_tx_desc *pending;
1082         struct xilinx_dpdma_sw_desc *sw_desc;
1083         unsigned long flags;
1084         u32 desc_id;
1085
1086         spin_lock_irqsave(&chan->lock, flags);
1087
1088         pending = chan->desc.pending;
1089         if (!chan->running || !pending)
1090                 goto out;
1091
1092         desc_id = dpdma_read(chan->reg, XILINX_DPDMA_CH_DESC_ID)
1093                 & XILINX_DPDMA_CH_DESC_ID_MASK;
1094
1095         /* If the retrigger raced with vsync, retry at the next frame. */
1096         sw_desc = list_first_entry(&pending->descriptors,
1097                                    struct xilinx_dpdma_sw_desc, node);
1098         if (sw_desc->hw.desc_id != desc_id)
1099                 goto out;
1100
1101         /*
1102          * Complete the active descriptor, if any, promote the pending
1103          * descriptor to active, and queue the next transfer, if any.
1104          */
1105         spin_lock(&chan->vchan.lock);
1106         if (chan->desc.active)
1107                 vchan_cookie_complete(&chan->desc.active->vdesc);
1108         chan->desc.active = pending;
1109         chan->desc.pending = NULL;
1110
1111         xilinx_dpdma_chan_queue_transfer(chan);
1112         spin_unlock(&chan->vchan.lock);
1113
1114 out:
1115         spin_unlock_irqrestore(&chan->lock, flags);
1116 }
1117
1118 /**
1119  * xilinx_dpdma_chan_err - Detect any channel error
1120  * @chan: DPDMA channel
1121  * @isr: masked Interrupt Status Register
1122  * @eisr: Error Interrupt Status Register
1123  *
1124  * Return: true if any channel error occurs, or false otherwise.
1125  */
1126 static bool
1127 xilinx_dpdma_chan_err(struct xilinx_dpdma_chan *chan, u32 isr, u32 eisr)
1128 {
1129         if (!chan)
1130                 return false;
1131
1132         if (chan->running &&
1133             ((isr & (XILINX_DPDMA_INTR_CHAN_ERR_MASK << chan->id)) ||
1134             (eisr & (XILINX_DPDMA_EINTR_CHAN_ERR_MASK << chan->id))))
1135                 return true;
1136
1137         return false;
1138 }
1139
1140 /**
1141  * xilinx_dpdma_chan_handle_err - DPDMA channel error handling
1142  * @chan: DPDMA channel
1143  *
1144  * This function is called when any channel error or any global error occurs.
1145  * The function disables the paused channel by errors and determines
1146  * if the current active descriptor can be rescheduled depending on
1147  * the descriptor status.
1148  */
1149 static void xilinx_dpdma_chan_handle_err(struct xilinx_dpdma_chan *chan)
1150 {
1151         struct xilinx_dpdma_device *xdev = chan->xdev;
1152         struct xilinx_dpdma_tx_desc *active;
1153         unsigned long flags;
1154
1155         spin_lock_irqsave(&chan->lock, flags);
1156
1157         dev_dbg(xdev->dev, "cur desc addr = 0x%04x%08x\n",
1158                 dpdma_read(chan->reg, XILINX_DPDMA_CH_DESC_START_ADDRE),
1159                 dpdma_read(chan->reg, XILINX_DPDMA_CH_DESC_START_ADDR));
1160         dev_dbg(xdev->dev, "cur payload addr = 0x%04x%08x\n",
1161                 dpdma_read(chan->reg, XILINX_DPDMA_CH_PYLD_CUR_ADDRE),
1162                 dpdma_read(chan->reg, XILINX_DPDMA_CH_PYLD_CUR_ADDR));
1163
1164         xilinx_dpdma_chan_disable(chan);
1165         chan->running = false;
1166
1167         if (!chan->desc.active)
1168                 goto out_unlock;
1169
1170         active = chan->desc.active;
1171         chan->desc.active = NULL;
1172
1173         xilinx_dpdma_chan_dump_tx_desc(chan, active);
1174
1175         if (active->error)
1176                 dev_dbg(xdev->dev, "repeated error on desc\n");
1177
1178         /* Reschedule if there's no new descriptor */
1179         if (!chan->desc.pending &&
1180             list_empty(&chan->vchan.desc_issued)) {
1181                 active->error = true;
1182                 list_add_tail(&active->vdesc.node,
1183                               &chan->vchan.desc_issued);
1184         } else {
1185                 xilinx_dpdma_chan_free_tx_desc(&active->vdesc);
1186         }
1187
1188 out_unlock:
1189         spin_unlock_irqrestore(&chan->lock, flags);
1190 }
1191
1192 /* -----------------------------------------------------------------------------
1193  * DMA Engine Operations
1194  */
1195
1196 static struct dma_async_tx_descriptor *
1197 xilinx_dpdma_prep_interleaved_dma(struct dma_chan *dchan,
1198                                   struct dma_interleaved_template *xt,
1199                                   unsigned long flags)
1200 {
1201         struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
1202         struct xilinx_dpdma_tx_desc *desc;
1203
1204         if (xt->dir != DMA_MEM_TO_DEV)
1205                 return NULL;
1206
1207         if (!xt->numf || !xt->sgl[0].size)
1208                 return NULL;
1209
1210         if (!(flags & DMA_PREP_REPEAT) || !(flags & DMA_PREP_LOAD_EOT))
1211                 return NULL;
1212
1213         desc = xilinx_dpdma_chan_prep_interleaved_dma(chan, xt);
1214         if (!desc)
1215                 return NULL;
1216
1217         vchan_tx_prep(&chan->vchan, &desc->vdesc, flags | DMA_CTRL_ACK);
1218
1219         return &desc->vdesc.tx;
1220 }
1221
1222 /**
1223  * xilinx_dpdma_alloc_chan_resources - Allocate resources for the channel
1224  * @dchan: DMA channel
1225  *
1226  * Allocate a descriptor pool for the channel.
1227  *
1228  * Return: 0 on success, or -ENOMEM if failed to allocate a pool.
1229  */
1230 static int xilinx_dpdma_alloc_chan_resources(struct dma_chan *dchan)
1231 {
1232         struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
1233         size_t align = __alignof__(struct xilinx_dpdma_sw_desc);
1234
1235         chan->desc_pool = dma_pool_create(dev_name(chan->xdev->dev),
1236                                           chan->xdev->dev,
1237                                           sizeof(struct xilinx_dpdma_sw_desc),
1238                                           align, 0);
1239         if (!chan->desc_pool) {
1240                 dev_err(chan->xdev->dev,
1241                         "failed to allocate a descriptor pool\n");
1242                 return -ENOMEM;
1243         }
1244
1245         return 0;
1246 }
1247
1248 /**
1249  * xilinx_dpdma_free_chan_resources - Free all resources for the channel
1250  * @dchan: DMA channel
1251  *
1252  * Free resources associated with the virtual DMA channel, and destroy the
1253  * descriptor pool.
1254  */
1255 static void xilinx_dpdma_free_chan_resources(struct dma_chan *dchan)
1256 {
1257         struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
1258
1259         vchan_free_chan_resources(&chan->vchan);
1260
1261         dma_pool_destroy(chan->desc_pool);
1262         chan->desc_pool = NULL;
1263 }
1264
1265 static void xilinx_dpdma_issue_pending(struct dma_chan *dchan)
1266 {
1267         struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
1268         unsigned long flags;
1269
1270         spin_lock_irqsave(&chan->lock, flags);
1271         spin_lock(&chan->vchan.lock);
1272         if (vchan_issue_pending(&chan->vchan))
1273                 xilinx_dpdma_chan_queue_transfer(chan);
1274         spin_unlock(&chan->vchan.lock);
1275         spin_unlock_irqrestore(&chan->lock, flags);
1276 }
1277
1278 static int xilinx_dpdma_config(struct dma_chan *dchan,
1279                                struct dma_slave_config *config)
1280 {
1281         struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
1282         unsigned long flags;
1283
1284         /*
1285          * The destination address doesn't need to be specified as the DPDMA is
1286          * hardwired to the destination (the DP controller). The transfer
1287          * width, burst size and port window size are thus meaningless, they're
1288          * fixed both on the DPDMA side and on the DP controller side.
1289          */
1290
1291         spin_lock_irqsave(&chan->lock, flags);
1292
1293         /*
1294          * Abuse the slave_id to indicate that the channel is part of a video
1295          * group.
1296          */
1297         if (chan->id <= ZYNQMP_DPDMA_VIDEO2)
1298                 chan->video_group = config->slave_id != 0;
1299
1300         spin_unlock_irqrestore(&chan->lock, flags);
1301
1302         return 0;
1303 }
1304
1305 static int xilinx_dpdma_pause(struct dma_chan *dchan)
1306 {
1307         xilinx_dpdma_chan_pause(to_xilinx_chan(dchan));
1308
1309         return 0;
1310 }
1311
1312 static int xilinx_dpdma_resume(struct dma_chan *dchan)
1313 {
1314         xilinx_dpdma_chan_unpause(to_xilinx_chan(dchan));
1315
1316         return 0;
1317 }
1318
1319 /**
1320  * xilinx_dpdma_terminate_all - Terminate the channel and descriptors
1321  * @dchan: DMA channel
1322  *
1323  * Pause the channel without waiting for ongoing transfers to complete. Waiting
1324  * for completion is performed by xilinx_dpdma_synchronize() that will disable
1325  * the channel to complete the stop.
1326  *
1327  * All the descriptors associated with the channel that are guaranteed not to
1328  * be touched by the hardware. The pending and active descriptor are not
1329  * touched, and will be freed either upon completion, or by
1330  * xilinx_dpdma_synchronize().
1331  *
1332  * Return: 0 on success, or -ETIMEDOUT if the channel failed to stop.
1333  */
1334 static int xilinx_dpdma_terminate_all(struct dma_chan *dchan)
1335 {
1336         struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
1337         struct xilinx_dpdma_device *xdev = chan->xdev;
1338         LIST_HEAD(descriptors);
1339         unsigned long flags;
1340         unsigned int i;
1341
1342         /* Pause the channel (including the whole video group if applicable). */
1343         if (chan->video_group) {
1344                 for (i = ZYNQMP_DPDMA_VIDEO0; i <= ZYNQMP_DPDMA_VIDEO2; i++) {
1345                         if (xdev->chan[i]->video_group &&
1346                             xdev->chan[i]->running) {
1347                                 xilinx_dpdma_chan_pause(xdev->chan[i]);
1348                                 xdev->chan[i]->video_group = false;
1349                         }
1350                 }
1351         } else {
1352                 xilinx_dpdma_chan_pause(chan);
1353         }
1354
1355         /* Gather all the descriptors we can free and free them. */
1356         spin_lock_irqsave(&chan->vchan.lock, flags);
1357         vchan_get_all_descriptors(&chan->vchan, &descriptors);
1358         spin_unlock_irqrestore(&chan->vchan.lock, flags);
1359
1360         vchan_dma_desc_free_list(&chan->vchan, &descriptors);
1361
1362         return 0;
1363 }
1364
1365 /**
1366  * xilinx_dpdma_synchronize - Synchronize callback execution
1367  * @dchan: DMA channel
1368  *
1369  * Synchronizing callback execution ensures that all previously issued
1370  * transfers have completed and all associated callbacks have been called and
1371  * have returned.
1372  *
1373  * This function waits for the DMA channel to stop. It assumes it has been
1374  * paused by a previous call to dmaengine_terminate_async(), and that no new
1375  * pending descriptors have been issued with dma_async_issue_pending(). The
1376  * behaviour is undefined otherwise.
1377  */
1378 static void xilinx_dpdma_synchronize(struct dma_chan *dchan)
1379 {
1380         struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
1381         unsigned long flags;
1382
1383         xilinx_dpdma_chan_stop(chan);
1384
1385         spin_lock_irqsave(&chan->vchan.lock, flags);
1386         if (chan->desc.pending) {
1387                 vchan_terminate_vdesc(&chan->desc.pending->vdesc);
1388                 chan->desc.pending = NULL;
1389         }
1390         if (chan->desc.active) {
1391                 vchan_terminate_vdesc(&chan->desc.active->vdesc);
1392                 chan->desc.active = NULL;
1393         }
1394         spin_unlock_irqrestore(&chan->vchan.lock, flags);
1395
1396         vchan_synchronize(&chan->vchan);
1397 }
1398
1399 /* -----------------------------------------------------------------------------
1400  * Interrupt and Tasklet Handling
1401  */
1402
1403 /**
1404  * xilinx_dpdma_err - Detect any global error
1405  * @isr: Interrupt Status Register
1406  * @eisr: Error Interrupt Status Register
1407  *
1408  * Return: True if any global error occurs, or false otherwise.
1409  */
1410 static bool xilinx_dpdma_err(u32 isr, u32 eisr)
1411 {
1412         if (isr & XILINX_DPDMA_INTR_GLOBAL_ERR ||
1413             eisr & XILINX_DPDMA_EINTR_GLOBAL_ERR)
1414                 return true;
1415
1416         return false;
1417 }
1418
1419 /**
1420  * xilinx_dpdma_handle_err_irq - Handle DPDMA error interrupt
1421  * @xdev: DPDMA device
1422  * @isr: masked Interrupt Status Register
1423  * @eisr: Error Interrupt Status Register
1424  *
1425  * Handle if any error occurs based on @isr and @eisr. This function disables
1426  * corresponding error interrupts, and those should be re-enabled once handling
1427  * is done.
1428  */
1429 static void xilinx_dpdma_handle_err_irq(struct xilinx_dpdma_device *xdev,
1430                                         u32 isr, u32 eisr)
1431 {
1432         bool err = xilinx_dpdma_err(isr, eisr);
1433         unsigned int i;
1434
1435         dev_dbg_ratelimited(xdev->dev,
1436                             "error irq: isr = 0x%08x, eisr = 0x%08x\n",
1437                             isr, eisr);
1438
1439         /* Disable channel error interrupts until errors are handled. */
1440         dpdma_write(xdev->reg, XILINX_DPDMA_IDS,
1441                     isr & ~XILINX_DPDMA_INTR_GLOBAL_ERR);
1442         dpdma_write(xdev->reg, XILINX_DPDMA_EIDS,
1443                     eisr & ~XILINX_DPDMA_EINTR_GLOBAL_ERR);
1444
1445         for (i = 0; i < ARRAY_SIZE(xdev->chan); i++)
1446                 if (err || xilinx_dpdma_chan_err(xdev->chan[i], isr, eisr))
1447                         tasklet_schedule(&xdev->chan[i]->err_task);
1448 }
1449
1450 /**
1451  * xilinx_dpdma_enable_irq - Enable interrupts
1452  * @xdev: DPDMA device
1453  *
1454  * Enable interrupts.
1455  */
1456 static void xilinx_dpdma_enable_irq(struct xilinx_dpdma_device *xdev)
1457 {
1458         dpdma_write(xdev->reg, XILINX_DPDMA_IEN, XILINX_DPDMA_INTR_ALL);
1459         dpdma_write(xdev->reg, XILINX_DPDMA_EIEN, XILINX_DPDMA_EINTR_ALL);
1460 }
1461
1462 /**
1463  * xilinx_dpdma_disable_irq - Disable interrupts
1464  * @xdev: DPDMA device
1465  *
1466  * Disable interrupts.
1467  */
1468 static void xilinx_dpdma_disable_irq(struct xilinx_dpdma_device *xdev)
1469 {
1470         dpdma_write(xdev->reg, XILINX_DPDMA_IDS, XILINX_DPDMA_INTR_ALL);
1471         dpdma_write(xdev->reg, XILINX_DPDMA_EIDS, XILINX_DPDMA_EINTR_ALL);
1472 }
1473
1474 /**
1475  * xilinx_dpdma_chan_err_task - Per channel tasklet for error handling
1476  * @t: pointer to the tasklet associated with this handler
1477  *
1478  * Per channel error handling tasklet. This function waits for the outstanding
1479  * transaction to complete and triggers error handling. After error handling,
1480  * re-enable channel error interrupts, and restart the channel if needed.
1481  */
1482 static void xilinx_dpdma_chan_err_task(struct tasklet_struct *t)
1483 {
1484         struct xilinx_dpdma_chan *chan = from_tasklet(chan, t, err_task);
1485         struct xilinx_dpdma_device *xdev = chan->xdev;
1486         unsigned long flags;
1487
1488         /* Proceed error handling even when polling fails. */
1489         xilinx_dpdma_chan_poll_no_ostand(chan);
1490
1491         xilinx_dpdma_chan_handle_err(chan);
1492
1493         dpdma_write(xdev->reg, XILINX_DPDMA_IEN,
1494                     XILINX_DPDMA_INTR_CHAN_ERR_MASK << chan->id);
1495         dpdma_write(xdev->reg, XILINX_DPDMA_EIEN,
1496                     XILINX_DPDMA_EINTR_CHAN_ERR_MASK << chan->id);
1497
1498         spin_lock_irqsave(&chan->lock, flags);
1499         spin_lock(&chan->vchan.lock);
1500         xilinx_dpdma_chan_queue_transfer(chan);
1501         spin_unlock(&chan->vchan.lock);
1502         spin_unlock_irqrestore(&chan->lock, flags);
1503 }
1504
1505 static irqreturn_t xilinx_dpdma_irq_handler(int irq, void *data)
1506 {
1507         struct xilinx_dpdma_device *xdev = data;
1508         unsigned long mask;
1509         unsigned int i;
1510         u32 status;
1511         u32 error;
1512
1513         status = dpdma_read(xdev->reg, XILINX_DPDMA_ISR);
1514         error = dpdma_read(xdev->reg, XILINX_DPDMA_EISR);
1515         if (!status && !error)
1516                 return IRQ_NONE;
1517
1518         dpdma_write(xdev->reg, XILINX_DPDMA_ISR, status);
1519         dpdma_write(xdev->reg, XILINX_DPDMA_EISR, error);
1520
1521         if (status & XILINX_DPDMA_INTR_VSYNC) {
1522                 /*
1523                  * There's a single VSYNC interrupt that needs to be processed
1524                  * by each running channel to update the active descriptor.
1525                  */
1526                 for (i = 0; i < ARRAY_SIZE(xdev->chan); i++) {
1527                         struct xilinx_dpdma_chan *chan = xdev->chan[i];
1528
1529                         if (chan)
1530                                 xilinx_dpdma_chan_vsync_irq(chan);
1531                 }
1532         }
1533
1534         mask = FIELD_GET(XILINX_DPDMA_INTR_DESC_DONE_MASK, status);
1535         if (mask) {
1536                 for_each_set_bit(i, &mask, ARRAY_SIZE(xdev->chan))
1537                         xilinx_dpdma_chan_done_irq(xdev->chan[i]);
1538         }
1539
1540         mask = FIELD_GET(XILINX_DPDMA_INTR_NO_OSTAND_MASK, status);
1541         if (mask) {
1542                 for_each_set_bit(i, &mask, ARRAY_SIZE(xdev->chan))
1543                         xilinx_dpdma_chan_notify_no_ostand(xdev->chan[i]);
1544         }
1545
1546         mask = status & XILINX_DPDMA_INTR_ERR_ALL;
1547         if (mask || error)
1548                 xilinx_dpdma_handle_err_irq(xdev, mask, error);
1549
1550         return IRQ_HANDLED;
1551 }
1552
1553 /* -----------------------------------------------------------------------------
1554  * Initialization & Cleanup
1555  */
1556
1557 static int xilinx_dpdma_chan_init(struct xilinx_dpdma_device *xdev,
1558                                   unsigned int chan_id)
1559 {
1560         struct xilinx_dpdma_chan *chan;
1561
1562         chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
1563         if (!chan)
1564                 return -ENOMEM;
1565
1566         chan->id = chan_id;
1567         chan->reg = xdev->reg + XILINX_DPDMA_CH_BASE
1568                   + XILINX_DPDMA_CH_OFFSET * chan->id;
1569         chan->running = false;
1570         chan->xdev = xdev;
1571
1572         spin_lock_init(&chan->lock);
1573         init_waitqueue_head(&chan->wait_to_stop);
1574
1575         tasklet_setup(&chan->err_task, xilinx_dpdma_chan_err_task);
1576
1577         chan->vchan.desc_free = xilinx_dpdma_chan_free_tx_desc;
1578         vchan_init(&chan->vchan, &xdev->common);
1579
1580         xdev->chan[chan->id] = chan;
1581
1582         return 0;
1583 }
1584
1585 static void xilinx_dpdma_chan_remove(struct xilinx_dpdma_chan *chan)
1586 {
1587         if (!chan)
1588                 return;
1589
1590         tasklet_kill(&chan->err_task);
1591         list_del(&chan->vchan.chan.device_node);
1592 }
1593
1594 static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
1595                                             struct of_dma *ofdma)
1596 {
1597         struct xilinx_dpdma_device *xdev = ofdma->of_dma_data;
1598         uint32_t chan_id = dma_spec->args[0];
1599
1600         if (chan_id >= ARRAY_SIZE(xdev->chan))
1601                 return NULL;
1602
1603         if (!xdev->chan[chan_id])
1604                 return NULL;
1605
1606         return dma_get_slave_channel(&xdev->chan[chan_id]->vchan.chan);
1607 }
1608
1609 static void dpdma_hw_init(struct xilinx_dpdma_device *xdev)
1610 {
1611         unsigned int i;
1612         void __iomem *reg;
1613
1614         /* Disable all interrupts */
1615         xilinx_dpdma_disable_irq(xdev);
1616
1617         /* Stop all channels */
1618         for (i = 0; i < ARRAY_SIZE(xdev->chan); i++) {
1619                 reg = xdev->reg + XILINX_DPDMA_CH_BASE
1620                                 + XILINX_DPDMA_CH_OFFSET * i;
1621                 dpdma_clr(reg, XILINX_DPDMA_CH_CNTL, XILINX_DPDMA_CH_CNTL_ENABLE);
1622         }
1623
1624         /* Clear the interrupt status registers */
1625         dpdma_write(xdev->reg, XILINX_DPDMA_ISR, XILINX_DPDMA_INTR_ALL);
1626         dpdma_write(xdev->reg, XILINX_DPDMA_EISR, XILINX_DPDMA_EINTR_ALL);
1627 }
1628
1629 static int xilinx_dpdma_probe(struct platform_device *pdev)
1630 {
1631         struct xilinx_dpdma_device *xdev;
1632         struct dma_device *ddev;
1633         unsigned int i;
1634         int ret;
1635
1636         xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
1637         if (!xdev)
1638                 return -ENOMEM;
1639
1640         xdev->dev = &pdev->dev;
1641         xdev->ext_addr = sizeof(dma_addr_t) > 4;
1642
1643         INIT_LIST_HEAD(&xdev->common.channels);
1644
1645         platform_set_drvdata(pdev, xdev);
1646
1647         xdev->axi_clk = devm_clk_get(xdev->dev, "axi_clk");
1648         if (IS_ERR(xdev->axi_clk))
1649                 return PTR_ERR(xdev->axi_clk);
1650
1651         xdev->reg = devm_platform_ioremap_resource(pdev, 0);
1652         if (IS_ERR(xdev->reg))
1653                 return PTR_ERR(xdev->reg);
1654
1655         dpdma_hw_init(xdev);
1656
1657         xdev->irq = platform_get_irq(pdev, 0);
1658         if (xdev->irq < 0) {
1659                 dev_err(xdev->dev, "failed to get platform irq\n");
1660                 return xdev->irq;
1661         }
1662
1663         ret = request_irq(xdev->irq, xilinx_dpdma_irq_handler, IRQF_SHARED,
1664                           dev_name(xdev->dev), xdev);
1665         if (ret) {
1666                 dev_err(xdev->dev, "failed to request IRQ\n");
1667                 return ret;
1668         }
1669
1670         ddev = &xdev->common;
1671         ddev->dev = &pdev->dev;
1672
1673         dma_cap_set(DMA_SLAVE, ddev->cap_mask);
1674         dma_cap_set(DMA_PRIVATE, ddev->cap_mask);
1675         dma_cap_set(DMA_INTERLEAVE, ddev->cap_mask);
1676         dma_cap_set(DMA_REPEAT, ddev->cap_mask);
1677         dma_cap_set(DMA_LOAD_EOT, ddev->cap_mask);
1678         ddev->copy_align = fls(XILINX_DPDMA_ALIGN_BYTES - 1);
1679
1680         ddev->device_alloc_chan_resources = xilinx_dpdma_alloc_chan_resources;
1681         ddev->device_free_chan_resources = xilinx_dpdma_free_chan_resources;
1682         ddev->device_prep_interleaved_dma = xilinx_dpdma_prep_interleaved_dma;
1683         /* TODO: Can we achieve better granularity ? */
1684         ddev->device_tx_status = dma_cookie_status;
1685         ddev->device_issue_pending = xilinx_dpdma_issue_pending;
1686         ddev->device_config = xilinx_dpdma_config;
1687         ddev->device_pause = xilinx_dpdma_pause;
1688         ddev->device_resume = xilinx_dpdma_resume;
1689         ddev->device_terminate_all = xilinx_dpdma_terminate_all;
1690         ddev->device_synchronize = xilinx_dpdma_synchronize;
1691         ddev->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED);
1692         ddev->directions = BIT(DMA_MEM_TO_DEV);
1693         ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
1694
1695         for (i = 0; i < ARRAY_SIZE(xdev->chan); ++i) {
1696                 ret = xilinx_dpdma_chan_init(xdev, i);
1697                 if (ret < 0) {
1698                         dev_err(xdev->dev, "failed to initialize channel %u\n",
1699                                 i);
1700                         goto error;
1701                 }
1702         }
1703
1704         ret = clk_prepare_enable(xdev->axi_clk);
1705         if (ret) {
1706                 dev_err(xdev->dev, "failed to enable the axi clock\n");
1707                 goto error;
1708         }
1709
1710         ret = dma_async_device_register(ddev);
1711         if (ret) {
1712                 dev_err(xdev->dev, "failed to register the dma device\n");
1713                 goto error_dma_async;
1714         }
1715
1716         ret = of_dma_controller_register(xdev->dev->of_node,
1717                                          of_dma_xilinx_xlate, ddev);
1718         if (ret) {
1719                 dev_err(xdev->dev, "failed to register DMA to DT DMA helper\n");
1720                 goto error_of_dma;
1721         }
1722
1723         xilinx_dpdma_enable_irq(xdev);
1724
1725         xilinx_dpdma_debugfs_init(xdev);
1726
1727         dev_info(&pdev->dev, "Xilinx DPDMA engine is probed\n");
1728
1729         return 0;
1730
1731 error_of_dma:
1732         dma_async_device_unregister(ddev);
1733 error_dma_async:
1734         clk_disable_unprepare(xdev->axi_clk);
1735 error:
1736         for (i = 0; i < ARRAY_SIZE(xdev->chan); i++)
1737                 xilinx_dpdma_chan_remove(xdev->chan[i]);
1738
1739         free_irq(xdev->irq, xdev);
1740
1741         return ret;
1742 }
1743
1744 static int xilinx_dpdma_remove(struct platform_device *pdev)
1745 {
1746         struct xilinx_dpdma_device *xdev = platform_get_drvdata(pdev);
1747         unsigned int i;
1748
1749         /* Start by disabling the IRQ to avoid races during cleanup. */
1750         free_irq(xdev->irq, xdev);
1751
1752         xilinx_dpdma_disable_irq(xdev);
1753         of_dma_controller_free(pdev->dev.of_node);
1754         dma_async_device_unregister(&xdev->common);
1755         clk_disable_unprepare(xdev->axi_clk);
1756
1757         for (i = 0; i < ARRAY_SIZE(xdev->chan); i++)
1758                 xilinx_dpdma_chan_remove(xdev->chan[i]);
1759
1760         return 0;
1761 }
1762
1763 static const struct of_device_id xilinx_dpdma_of_match[] = {
1764         { .compatible = "xlnx,zynqmp-dpdma",},
1765         { /* end of table */ },
1766 };
1767 MODULE_DEVICE_TABLE(of, xilinx_dpdma_of_match);
1768
1769 static struct platform_driver xilinx_dpdma_driver = {
1770         .probe                  = xilinx_dpdma_probe,
1771         .remove                 = xilinx_dpdma_remove,
1772         .driver                 = {
1773                 .name           = "xilinx-zynqmp-dpdma",
1774                 .of_match_table = xilinx_dpdma_of_match,
1775         },
1776 };
1777
1778 module_platform_driver(xilinx_dpdma_driver);
1779
1780 MODULE_AUTHOR("Xilinx, Inc.");
1781 MODULE_DESCRIPTION("Xilinx ZynqMP DPDMA driver");
1782 MODULE_LICENSE("GPL v2");