]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - drivers/dma/xilinx/xilinx_dpdma.c
74c705fb4cbe421968c16f6d4b1a5fa8a8ef5086
[zynq/linux.git] / drivers / dma / xilinx / xilinx_dpdma.c
1 /*
2  * Xilinx DPDMA Engine driver
3  *
4  *  Copyright (C) 2015 Xilinx, Inc.
5  *
6  *  Author: Hyun Woo Kwon <hyun.kwon@xilinx.com>
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/bitops.h>
19 #include <linux/clk.h>
20 #include <linux/debugfs.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/dmaengine.h>
24 #include <linux/dmapool.h>
25 #include <linux/gfp.h>
26 #include <linux/interrupt.h>
27 #include <linux/irqreturn.h>
28 #include <linux/module.h>
29 #include <linux/of.h>
30 #include <linux/of_dma.h>
31 #include <linux/platform_device.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
35 #include <linux/types.h>
36 #include <linux/uaccess.h>
37 #include <linux/wait.h>
38
39 #include "../dmaengine.h"
40
41 /* DPDMA registers */
42 #define XILINX_DPDMA_ERR_CTRL                           0x0
43 #define XILINX_DPDMA_ISR                                0x4
44 #define XILINX_DPDMA_IMR                                0x8
45 #define XILINX_DPDMA_IEN                                0xc
46 #define XILINX_DPDMA_IDS                                0x10
47 #define XILINX_DPDMA_INTR_DESC_DONE_MASK                (0x3f << 0)
48 #define XILINX_DPDMA_INTR_DESC_DONE_SHIFT               0
49 #define XILINX_DPDMA_INTR_NO_OSTAND_MASK                (0x3f << 6)
50 #define XILINX_DPDMA_INTR_NO_OSTAND_SHIFT               6
51 #define XILINX_DPDMA_INTR_AXI_ERR_MASK                  (0x3f << 12)
52 #define XILINX_DPDMA_INTR_AXI_ERR_SHIFT                 12
53 #define XILINX_DPDMA_INTR_DESC_ERR_MASK                 (0x3f << 18)
54 #define XILINX_DPDMA_INTR_DESC_ERR_SHIFT                16
55 #define XILINX_DPDMA_INTR_WR_CMD_FIFO_FULL              BIT(24)
56 #define XILINX_DPDMA_INTR_WR_DATA_FIFO_FULL             BIT(25)
57 #define XILINX_DPDMA_INTR_AXI_4K_CROSS                  BIT(26)
58 #define XILINX_DPDMA_INTR_VSYNC                         BIT(27)
59 #define XILINX_DPDMA_INTR_CHAN_ERR_MASK                 0x41000
60 #define XILINX_DPDMA_INTR_CHAN_ERR                      0xfff000
61 #define XILINX_DPDMA_INTR_GLOBAL_ERR                    0x7000000
62 #define XILINX_DPDMA_INTR_ERR_ALL                       0x7fff000
63 #define XILINX_DPDMA_INTR_CHAN_MASK                     0x41041
64 #define XILINX_DPDMA_INTR_GLOBAL_MASK                   0xf000000
65 #define XILINX_DPDMA_INTR_ALL                           0xfffffff
66 #define XILINX_DPDMA_EISR                               0x14
67 #define XILINX_DPDMA_EIMR                               0x18
68 #define XILINX_DPDMA_EIEN                               0x1c
69 #define XILINX_DPDMA_EIDS                               0x20
70 #define XILINX_DPDMA_EINTR_INV_APB                      BIT(0)
71 #define XILINX_DPDMA_EINTR_RD_AXI_ERR_MASK              (0x3f << 1)
72 #define XILINX_DPDMA_EINTR_RD_AXI_ERR_SHIFT             1
73 #define XILINX_DPDMA_EINTR_PRE_ERR_MASK                 (0x3f << 7)
74 #define XILINX_DPDMA_EINTR_PRE_ERR_SHIFT                7
75 #define XILINX_DPDMA_EINTR_CRC_ERR_MASK                 (0x3f << 13)
76 #define XILINX_DPDMA_EINTR_CRC_ERR_SHIFT                13
77 #define XILINX_DPDMA_EINTR_WR_AXI_ERR_MASK              (0x3f << 19)
78 #define XILINX_DPDMA_EINTR_WR_AXI_ERR_SHIFT             19
79 #define XILINX_DPDMA_EINTR_DESC_DONE_ERR_MASK           (0x3f << 25)
80 #define XILINX_DPDMA_EINTR_DESC_DONE_ERR_SHIFT          25
81 #define XILINX_DPDMA_EINTR_RD_CMD_FIFO_FULL             BIT(32)
82 #define XILINX_DPDMA_EINTR_CHAN_ERR_MASK                0x2082082
83 #define XILINX_DPDMA_EINTR_CHAN_ERR                     0x7ffffffe
84 #define XILINX_DPDMA_EINTR_GLOBAL_ERR                   0x80000001
85 #define XILINX_DPDMA_EINTR_ALL                          0xffffffff
86 #define XILINX_DPDMA_CNTL                               0x100
87 #define XILINX_DPDMA_GBL                                0x104
88 #define XILINX_DPDMA_GBL_TRIG_SHIFT                     0
89 #define XILINX_DPDMA_GBL_RETRIG_SHIFT                   6
90 #define XILINX_DPDMA_ALC0_CNTL                          0x108
91 #define XILINX_DPDMA_ALC0_STATUS                        0x10c
92 #define XILINX_DPDMA_ALC0_MAX                           0x110
93 #define XILINX_DPDMA_ALC0_MIN                           0x114
94 #define XILINX_DPDMA_ALC0_ACC                           0x118
95 #define XILINX_DPDMA_ALC0_ACC_TRAN                      0x11c
96 #define XILINX_DPDMA_ALC1_CNTL                          0x120
97 #define XILINX_DPDMA_ALC1_STATUS                        0x124
98 #define XILINX_DPDMA_ALC1_MAX                           0x128
99 #define XILINX_DPDMA_ALC1_MIN                           0x12c
100 #define XILINX_DPDMA_ALC1_ACC                           0x130
101 #define XILINX_DPDMA_ALC1_ACC_TRAN                      0x134
102
103 /* Channel register */
104 #define XILINX_DPDMA_CH_BASE                            0x200
105 #define XILINX_DPDMA_CH_OFFSET                          0x100
106 #define XILINX_DPDMA_CH_DESC_START_ADDRE                0x0
107 #define XILINX_DPDMA_CH_DESC_START_ADDR                 0x4
108 #define XILINX_DPDMA_CH_DESC_NEXT_ADDRE                 0x8
109 #define XILINX_DPDMA_CH_DESC_NEXT_ADDR                  0xc
110 #define XILINX_DPDMA_CH_PYLD_CUR_ADDRE                  0x10
111 #define XILINX_DPDMA_CH_PYLD_CUR_ADDR                   0x14
112 #define XILINX_DPDMA_CH_CNTL                            0x18
113 #define XILINX_DPDMA_CH_CNTL_ENABLE                     BIT(0)
114 #define XILINX_DPDMA_CH_CNTL_PAUSE                      BIT(1)
115 #define XILINX_DPDMA_CH_CNTL_QOS_DSCR_WR_SHIFT          2
116 #define XILINX_DPDMA_CH_CNTL_QOS_DSCR_RD_SHIFT          6
117 #define XILINX_DPDMA_CH_CNTL_QOS_DATA_RD_SHIFT          10
118 #define XILINX_DPDMA_CH_CNTL_QOS_VID_CLASS              11
119 #define XILINX_DPDMA_CH_STATUS                          0x1c
120 #define XILINX_DPDMA_CH_STATUS_OTRAN_CNT_MASK           (0xf << 21)
121 #define XILINX_DPDMA_CH_STATUS_OTRAN_CNT_SHIFT          21
122 #define XILINX_DPDMA_CH_VDO                             0x20
123 #define XILINX_DPDMA_CH_PYLD_SZ                         0x24
124 #define XILINX_DPDMA_CH_DESC_ID                         0x28
125
126 /* DPDMA descriptor fields */
127 #define XILINX_DPDMA_DESC_CONTROL_PREEMBLE              (0xa5)
128 #define XILINX_DPDMA_DESC_CONTROL_COMPLETE_INTR         BIT(8)
129 #define XILINX_DPDMA_DESC_CONTROL_DESC_UPDATE           BIT(9)
130 #define XILINX_DPDMA_DESC_CONTROL_IGNORE_DONE           BIT(10)
131 #define XILINX_DPDMA_DESC_CONTROL_FRAG_MODE             BIT(18)
132 #define XILINX_DPDMA_DESC_CONTROL_LAST                  BIT(19)
133 #define XILINX_DPDMA_DESC_CONTROL_ENABLE_CRC            BIT(20)
134 #define XILINX_DPDMA_DESC_CONTROL_LAST_OF_FRAME         BIT(21)
135 #define XILINX_DPDMA_DESC_ID_MASK                       (0xffff << 0)
136 #define XILINX_DPDMA_DESC_ID_SHIFT                      (0)
137 #define XILINX_DPDMA_DESC_HSIZE_STRIDE_HSIZE_MASK       (0x3ffff << 0)
138 #define XILINX_DPDMA_DESC_HSIZE_STRIDE_HSIZE_SHIFT      (0)
139 #define XILINX_DPDMA_DESC_HSIZE_STRIDE_STRIDE_MASK      (0x3fff << 18)
140 #define XILINX_DPDMA_DESC_HSIZE_STRIDE_STRIDE_SHIFT     (18)
141 #define XILINX_DPDMA_DESC_ADDR_EXT_ADDR_MASK            (0xfff)
142 #define XILINX_DPDMA_DESC_ADDR_EXT_ADDR_SHIFT           (16)
143
144 #define XILINX_DPDMA_ALIGN_BYTES                        256
145
146 #define XILINX_DPDMA_NUM_CHAN                           6
147 #define XILINX_DPDMA_PAGE_MASK                          ((1 << 12) - 1)
148 #define XILINX_DPDMA_PAGE_SHIFT                         12
149
150 /**
151  * struct xilinx_dpdma_hw_desc - DPDMA hardware descriptor
152  * @control: control configuration field
153  * @desc_id: descriptor ID
154  * @xfer_size: transfer size
155  * @hsize_stride: horizontal size and stride
156  * @timestamp_lsb: LSB of time stamp
157  * @timestamp_msb: MSB of time stamp
158  * @addr_ext: upper 16 bit of 48 bit address (next_desc and src_addr)
159  * @next_desc: next descriptor 32 bit address
160  * @src_addr: payload source address (lower 32 bit of 1st 4KB page)
161  * @addr_ext_23: upper 16 bit of 48 bit address (src_addr2 and src_addr3)
162  * @addr_ext_45: upper 16 bit of 48 bit address (src_addr4 and src_addr5)
163  * @src_addr2: payload source address (lower 32 bit of 2nd 4KB page)
164  * @src_addr3: payload source address (lower 32 bit of 3rd 4KB page)
165  * @src_addr4: payload source address (lower 32 bit of 4th 4KB page)
166  * @src_addr5: payload source address (lower 32 bit of 5th 4KB page)
167  * @crc: descriptor CRC
168  */
169 struct xilinx_dpdma_hw_desc {
170         u32 control;
171         u32 desc_id;
172         u32 xfer_size;
173         u32 hsize_stride;
174         u32 timestamp_lsb;
175         u32 timestamp_msb;
176         u32 addr_ext;
177         u32 next_desc;
178         u32 src_addr;
179         u32 addr_ext_23;
180         u32 addr_ext_45;
181         u32 src_addr2;
182         u32 src_addr3;
183         u32 src_addr4;
184         u32 src_addr5;
185         u32 crc;
186 } __aligned(XILINX_DPDMA_ALIGN_BYTES);
187
188 /**
189  * struct xilinx_dpdma_sw_desc - DPDMA software descriptor
190  * @hw: DPDMA hardware descriptor
191  * @node: list node for software descriptors
192  * @phys: physical address of the software descriptor
193  */
194 struct xilinx_dpdma_sw_desc {
195         struct xilinx_dpdma_hw_desc hw;
196         struct list_head node;
197         dma_addr_t phys;
198 };
199
200 /**
201  * enum xilinx_dpdma_tx_desc_status - DPDMA tx descriptor status
202  * @PREPARED: descriptor is prepared for transaction
203  * @ACTIVE: transaction is (being) done successfully
204  * @ERRORED: descriptor generates some errors
205  */
206 enum xilinx_dpdma_tx_desc_status {
207         PREPARED,
208         ACTIVE,
209         ERRORED
210 };
211
212 /**
213  * struct xilinx_dpdma_tx_desc - DPDMA transaction descriptor
214  * @async_tx: DMA async transaction descriptor
215  * @descriptors: list of software descriptors
216  * @node: list node for transaction descriptors
217  * @status: tx descriptor status
218  * @done_cnt: number of complete notification to deliver
219  */
220 struct xilinx_dpdma_tx_desc {
221         struct dma_async_tx_descriptor async_tx;
222         struct list_head descriptors;
223         struct list_head node;
224         enum xilinx_dpdma_tx_desc_status status;
225         unsigned int done_cnt;
226 };
227
228 /**
229  * enum xilinx_dpdma_chan_id - DPDMA channel ID
230  * @VIDEO0: video 1st channel
231  * @VIDEO1: video 2nd channel for multi plane yuv formats
232  * @VIDEO2: video 3rd channel for multi plane yuv formats
233  * @GRAPHICS: graphics channel
234  * @AUDIO0: 1st audio channel
235  * @AUDIO1: 2nd audio channel
236  */
237 enum xilinx_dpdma_chan_id {
238         VIDEO0,
239         VIDEO1,
240         VIDEO2,
241         GRAPHICS,
242         AUDIO0,
243         AUDIO1
244 };
245
246 /**
247  * enum xilinx_dpdma_chan_status - DPDMA channel status
248  * @IDLE: idle state
249  * @STREAMING: actively streaming state
250  */
251 enum xilinx_dpdma_chan_status {
252         IDLE,
253         STREAMING
254 };
255
256 /*
257  * DPDMA descriptor placement
258  * --------------------------
259  * DPDMA descritpor life time is described with following placements:
260  *
261  * allocated_desc -> submitted_desc -> pending_desc -> active_desc -> done_list
262  *
263  * Transition is triggered as following:
264  *
265  * -> allocated_desc : a descriptor allocation
266  * allocated_desc -> submitted_desc: a descriptorsubmission
267  * submitted_desc -> pending_desc: request to issue pending a descriptor
268  * pending_desc -> active_desc: VSYNC intr when a desc is scheduled to DPDMA
269  * active_desc -> done_list: VSYNC intr when DPDMA switches to a new desc
270  */
271
272 /**
273  * struct xilinx_dpdma_chan - DPDMA channel
274  * @common: generic dma channel structure
275  * @reg: register base address
276  * @id: channel ID
277  * @wait_to_stop: queue to wait for outstanding transacitons before stopping
278  * @status: channel status
279  * @first_frame: flag for the first frame of stream
280  * @video_group: flag if multi-channel operation is needed for video channels
281  * @lock: lock to access struct xilinx_dpdma_chan
282  * @desc_pool: descriptor allocation pool
283  * @done_task: done IRQ bottom half handler
284  * @err_task: error IRQ bottom half handler
285  * @allocated_desc: allocated descriptor
286  * @submitted_desc: submitted descriptor
287  * @pending_desc: pending descriptor to be scheduled in next period
288  * @active_desc: descriptor that the DPDMA channel is active on
289  * @done_list: done descriptor list
290  * @xdev: DPDMA device
291  */
292 struct xilinx_dpdma_chan {
293         struct dma_chan common;
294         void __iomem *reg;
295         enum xilinx_dpdma_chan_id id;
296
297         wait_queue_head_t wait_to_stop;
298         enum xilinx_dpdma_chan_status status;
299         bool first_frame;
300         bool video_group;
301
302         spinlock_t lock;
303         struct dma_pool *desc_pool;
304         struct tasklet_struct done_task;
305         struct tasklet_struct err_task;
306
307         struct xilinx_dpdma_tx_desc *allocated_desc;
308         struct xilinx_dpdma_tx_desc *submitted_desc;
309         struct xilinx_dpdma_tx_desc *pending_desc;
310         struct xilinx_dpdma_tx_desc *active_desc;
311         struct list_head done_list;
312
313         struct xilinx_dpdma_device *xdev;
314 };
315
316 /**
317  * struct xilinx_dpdma_device - DPDMA device
318  * @common: generic dma device structure
319  * @reg: register base address
320  * @dev: generic device structure
321  * @axi_clk: axi clock
322  * @chan: DPDMA channels
323  * @ext_addr: flag for 64 bit system (48 bit addressing)
324  * @desc_addr: descriptor addressing callback (32 bit vs 64 bit)
325  */
326 struct xilinx_dpdma_device {
327         struct dma_device common;
328         void __iomem *reg;
329         struct device *dev;
330
331         struct clk *axi_clk;
332         struct xilinx_dpdma_chan *chan[XILINX_DPDMA_NUM_CHAN];
333
334         bool ext_addr;
335         void (*desc_addr)(struct xilinx_dpdma_sw_desc *sw_desc,
336                           struct xilinx_dpdma_sw_desc *prev,
337                           dma_addr_t dma_addr[], unsigned int num_src_addr);
338 };
339
340 #ifdef CONFIG_XILINX_DPDMA_DEBUG_FS
341 #define XILINX_DPDMA_DEBUGFS_READ_MAX_SIZE      32
342 #define XILINX_DPDMA_DEBUGFS_UINT16_MAX_STR     "65535"
343 #define IN_RANGE(x, min, max) ((x) >= (min) && (x) <= (max))
344
345 /* Match xilinx_dpdma_testcases vs dpdma_debugfs_reqs[] entry */
346 enum xilinx_dpdma_testcases {
347         DPDMA_TC_INTR_DONE,
348         DPDMA_TC_NONE
349 };
350
351 struct xilinx_dpdma_debugfs {
352         enum xilinx_dpdma_testcases testcase;
353         u16 xilinx_dpdma_intr_done_count;
354         enum xilinx_dpdma_chan_id chan_id;
355 };
356
357 static struct xilinx_dpdma_debugfs dpdma_debugfs;
358 struct xilinx_dpdma_debugfs_request {
359         const char *req;
360         enum xilinx_dpdma_testcases tc;
361         ssize_t (*read_handler)(char **kern_buff);
362         ssize_t (*write_handler)(char **cmd);
363 };
364
365 static void xilinx_dpdma_debugfs_intr_done_count_incr(int chan_id)
366 {
367         if (chan_id == dpdma_debugfs.chan_id)
368                 dpdma_debugfs.xilinx_dpdma_intr_done_count++;
369 }
370
371 static s64 xilinx_dpdma_debugfs_argument_value(char *arg)
372 {
373         s64 value;
374
375         if (!arg)
376                 return -1;
377
378         if (!kstrtos64(arg, 0, &value))
379                 return value;
380
381         return -1;
382 }
383
384 static ssize_t
385 xilinx_dpdma_debugfs_desc_done_intr_write(char **dpdma_test_arg)
386 {
387         char *arg;
388         char *arg_chan_id;
389         s64 id;
390
391         arg = strsep(dpdma_test_arg, " ");
392         if (strncasecmp(arg, "start", 5) != 0)
393                 return -EINVAL;
394
395         arg_chan_id = strsep(dpdma_test_arg, " ");
396         id = xilinx_dpdma_debugfs_argument_value(arg_chan_id);
397
398         if (id < 0 || !IN_RANGE(id, VIDEO0, AUDIO1))
399                 return -EINVAL;
400
401         dpdma_debugfs.testcase = DPDMA_TC_INTR_DONE;
402         dpdma_debugfs.xilinx_dpdma_intr_done_count = 0;
403         dpdma_debugfs.chan_id = id;
404
405         return 0;
406 }
407
408 static ssize_t xilinx_dpdma_debugfs_desc_done_intr_read(char **kern_buff)
409 {
410         size_t out_str_len;
411
412         dpdma_debugfs.testcase = DPDMA_TC_NONE;
413
414         out_str_len = strlen(XILINX_DPDMA_DEBUGFS_UINT16_MAX_STR);
415         out_str_len = min_t(size_t, XILINX_DPDMA_DEBUGFS_READ_MAX_SIZE,
416                             out_str_len);
417         snprintf(*kern_buff, out_str_len, "%d",
418                  dpdma_debugfs.xilinx_dpdma_intr_done_count);
419
420         return 0;
421 }
422
423 /* Match xilinx_dpdma_testcases vs dpdma_debugfs_reqs[] entry */
424 struct xilinx_dpdma_debugfs_request dpdma_debugfs_reqs[] = {
425         {"DESCRIPTOR_DONE_INTR", DPDMA_TC_INTR_DONE,
426                         xilinx_dpdma_debugfs_desc_done_intr_read,
427                         xilinx_dpdma_debugfs_desc_done_intr_write},
428 };
429
430 static ssize_t xilinx_dpdma_debugfs_write(struct file *f, const char __user
431                                                *buf, size_t size, loff_t *pos)
432 {
433         char *kern_buff;
434         char *dpdma_test_req;
435         int ret;
436         int i;
437
438         if (*pos != 0 || size <= 0)
439                 return -EINVAL;
440
441         /* Supporting single instance of test as of now*/
442         if (dpdma_debugfs.testcase != DPDMA_TC_NONE)
443                 return -EBUSY;
444
445         kern_buff = kzalloc(size, GFP_KERNEL);
446         if (!kern_buff)
447                 return -ENOMEM;
448
449         ret = strncpy_from_user(kern_buff, buf, size);
450         if (ret < 0) {
451                 kfree(kern_buff);
452                 return ret;
453         }
454
455         /* Read the testcase name from an user request */
456         dpdma_test_req = strsep(&kern_buff, " ");
457
458         for (i = 0; i < ARRAY_SIZE(dpdma_debugfs_reqs); i++) {
459                 if (!strcasecmp(dpdma_test_req, dpdma_debugfs_reqs[i].req)) {
460                         if (!dpdma_debugfs_reqs[i].write_handler(&kern_buff)) {
461                                 kfree(kern_buff);
462                                 return size;
463                         }
464                         break;
465                 }
466         }
467         kfree(kern_buff);
468         return -EINVAL;
469 }
470
471 static ssize_t xilinx_dpdma_debugfs_read(struct file *f, char __user *buf,
472                                          size_t size, loff_t *pos)
473 {
474         char *kern_buff = NULL;
475         size_t kern_buff_len, out_str_len;
476         int ret;
477
478         if (size <= 0)
479                 return -EINVAL;
480
481         if (*pos != 0)
482                 return 0;
483
484         kern_buff = kzalloc(XILINX_DPDMA_DEBUGFS_READ_MAX_SIZE, GFP_KERNEL);
485         if (!kern_buff) {
486                 dpdma_debugfs.testcase = DPDMA_TC_NONE;
487                 return -ENOMEM;
488         }
489
490         if (dpdma_debugfs.testcase == DPDMA_TC_NONE) {
491                 out_str_len = strlen("No testcase executed");
492                 out_str_len = min_t(size_t, XILINX_DPDMA_DEBUGFS_READ_MAX_SIZE,
493                                     out_str_len);
494                 snprintf(kern_buff, out_str_len, "%s", "No testcase executed");
495         } else {
496                 ret = dpdma_debugfs_reqs[dpdma_debugfs.testcase].read_handler(
497                                 &kern_buff);
498                 if (ret) {
499                         kfree(kern_buff);
500                         return ret;
501                 }
502         }
503
504         kern_buff_len = strlen(kern_buff);
505         size = min(size, kern_buff_len);
506
507         ret = copy_to_user(buf, kern_buff, size);
508
509         kfree(kern_buff);
510         if (ret)
511                 return ret;
512
513         *pos = size + 1;
514         return size;
515 }
516
517 static const struct file_operations fops_xilinx_dpdma_dbgfs = {
518         .owner = THIS_MODULE,
519         .read = xilinx_dpdma_debugfs_read,
520         .write = xilinx_dpdma_debugfs_write,
521 };
522
523 static int xilinx_dpdma_debugfs_init(struct device *dev)
524 {
525         int err;
526         struct dentry *xilinx_dpdma_debugfs_dir, *xilinx_dpdma_debugfs_file;
527
528         dpdma_debugfs.testcase = DPDMA_TC_NONE;
529
530         xilinx_dpdma_debugfs_dir = debugfs_create_dir("dpdma", NULL);
531         if (!xilinx_dpdma_debugfs_dir) {
532                 dev_err(dev, "debugfs_create_dir failed\n");
533                 return -ENODEV;
534         }
535
536         xilinx_dpdma_debugfs_file =
537                 debugfs_create_file("testcase", 0444,
538                                     xilinx_dpdma_debugfs_dir, NULL,
539                                     &fops_xilinx_dpdma_dbgfs);
540         if (!xilinx_dpdma_debugfs_file) {
541                 dev_err(dev, "debugfs_create_file testcase failed\n");
542                 err = -ENODEV;
543                 goto err_dbgfs;
544         }
545         return 0;
546
547 err_dbgfs:
548         debugfs_remove_recursive(xilinx_dpdma_debugfs_dir);
549         xilinx_dpdma_debugfs_dir = NULL;
550         return err;
551 }
552
553 #else
554 static int xilinx_dpdma_debugfs_init(struct device *dev)
555 {
556         return 0;
557 }
558
559 static void xilinx_dpdma_debugfs_intr_done_count_incr(int chan_id)
560 {
561 }
562 #endif /* CONFIG_XILINX_DPDMA_DEBUG_FS */
563
564 #define to_dpdma_tx_desc(tx) \
565         container_of(tx, struct xilinx_dpdma_tx_desc, async_tx)
566
567 #define to_xilinx_chan(chan) \
568         container_of(chan, struct xilinx_dpdma_chan, common)
569
570 /* IO operations */
571
572 static inline u32 dpdma_read(void __iomem *base, u32 offset)
573 {
574         return ioread32(base + offset);
575 }
576
577 static inline void dpdma_write(void __iomem *base, u32 offset, u32 val)
578 {
579         iowrite32(val, base + offset);
580 }
581
582 static inline void dpdma_clr(void __iomem *base, u32 offset, u32 clr)
583 {
584         dpdma_write(base, offset, dpdma_read(base, offset) & ~clr);
585 }
586
587 static inline void dpdma_set(void __iomem *base, u32 offset, u32 set)
588 {
589         dpdma_write(base, offset, dpdma_read(base, offset) | set);
590 }
591
592 /* Xilinx DPDMA descriptor operations */
593
594 /**
595  * xilinx_dpdma_sw_desc_next_32 - Set 32 bit address of a next sw descriptor
596  * @sw_desc: current software descriptor
597  * @next: next descriptor
598  *
599  * Update the current sw descriptor @sw_desc with 32 bit address of the next
600  * descriptor @next.
601  */
602 static inline void
603 xilinx_dpdma_sw_desc_next_32(struct xilinx_dpdma_sw_desc *sw_desc,
604                              struct xilinx_dpdma_sw_desc *next)
605 {
606         sw_desc->hw.next_desc = next->phys;
607 }
608
609 /**
610  * xilinx_dpdma_sw_desc_addr_32 - Update the sw descriptor with 32 bit address
611  * @sw_desc: software descriptor
612  * @prev: previous descriptor
613  * @dma_addr: array of dma addresses
614  * @num_src_addr: number of addresses in @dma_addr
615  *
616  * Update the descriptor @sw_desc with 32 bit address.
617  */
618 static void xilinx_dpdma_sw_desc_addr_32(struct xilinx_dpdma_sw_desc *sw_desc,
619                                          struct xilinx_dpdma_sw_desc *prev,
620                                          dma_addr_t dma_addr[],
621                                          unsigned int num_src_addr)
622 {
623         struct xilinx_dpdma_hw_desc *hw_desc = &sw_desc->hw;
624         unsigned int i;
625
626         hw_desc->src_addr = dma_addr[0];
627
628         if (prev)
629                 xilinx_dpdma_sw_desc_next_32(prev, sw_desc);
630
631         for (i = 1; i < num_src_addr; i++) {
632                 u32 *addr = &hw_desc->src_addr2;
633                 u32 frag_addr;
634
635                 frag_addr = dma_addr[i];
636                 addr[i - 1] = frag_addr;
637         }
638 }
639
640 /**
641  * xilinx_dpdma_sw_desc_next_64 - Set 64 bit address of a next sw descriptor
642  * @sw_desc: current software descriptor
643  * @next: next descriptor
644  *
645  * Update the current sw descriptor @sw_desc with 64 bit address of the next
646  * descriptor @next.
647  */
648 static inline void
649 xilinx_dpdma_sw_desc_next_64(struct xilinx_dpdma_sw_desc *sw_desc,
650                              struct xilinx_dpdma_sw_desc *next)
651 {
652         sw_desc->hw.next_desc = (u32)next->phys;
653         sw_desc->hw.addr_ext |= ((u64)next->phys >> 32) &
654                                 XILINX_DPDMA_DESC_ADDR_EXT_ADDR_MASK;
655 }
656
657 /**
658  * xilinx_dpdma_sw_desc_addr_64 - Update the sw descriptor with 64 bit address
659  * @sw_desc: software descriptor
660  * @prev: previous descriptor
661  * @dma_addr: array of dma addresses
662  * @num_src_addr: number of addresses in @dma_addr
663  *
664  * Update the descriptor @sw_desc with 64 bit address.
665  */
666 static void xilinx_dpdma_sw_desc_addr_64(struct xilinx_dpdma_sw_desc *sw_desc,
667                                          struct xilinx_dpdma_sw_desc *prev,
668                                          dma_addr_t dma_addr[],
669                                          unsigned int num_src_addr)
670 {
671         struct xilinx_dpdma_hw_desc *hw_desc = &sw_desc->hw;
672         unsigned int i;
673
674         hw_desc->src_addr = (u32)dma_addr[0];
675         hw_desc->addr_ext |=
676                 ((u64)dma_addr[0] >> 32) & XILINX_DPDMA_DESC_ADDR_EXT_ADDR_MASK;
677
678         if (prev)
679                 xilinx_dpdma_sw_desc_next_64(prev, sw_desc);
680
681         for (i = 1; i < num_src_addr; i++) {
682                 u32 *addr = &hw_desc->src_addr2;
683                 u32 *addr_ext = &hw_desc->addr_ext_23;
684                 u64 frag_addr;
685
686                 frag_addr = dma_addr[i];
687                 addr[i] = (u32)frag_addr;
688
689                 frag_addr >>= 32;
690                 frag_addr &= XILINX_DPDMA_DESC_ADDR_EXT_ADDR_MASK;
691                 frag_addr <<= XILINX_DPDMA_DESC_ADDR_EXT_ADDR_SHIFT * (i % 2);
692                 addr_ext[i / 2] = frag_addr;
693         }
694 }
695
696 /* Xilinx DPDMA channel descriptor operations */
697
698 /**
699  * xilinx_dpdma_chan_alloc_sw_desc - Allocate a software descriptor
700  * @chan: DPDMA channel
701  *
702  * Allocate a software descriptor from the channel's descriptor pool.
703  *
704  * Return: a software descriptor or NULL.
705  */
706 static struct xilinx_dpdma_sw_desc *
707 xilinx_dpdma_chan_alloc_sw_desc(struct xilinx_dpdma_chan *chan)
708 {
709         struct xilinx_dpdma_sw_desc *sw_desc;
710         dma_addr_t phys;
711
712         sw_desc = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &phys);
713         if (!sw_desc)
714                 return NULL;
715
716         memset(sw_desc, 0, sizeof(*sw_desc));
717         sw_desc->phys = phys;
718
719         return sw_desc;
720 }
721
722 /**
723  * xilinx_dpdma_chan_free_sw_desc - Free a software descriptor
724  * @chan: DPDMA channel
725  * @sw_desc: software descriptor to free
726  *
727  * Free a software descriptor from the channel's descriptor pool.
728  */
729 static void
730 xilinx_dpdma_chan_free_sw_desc(struct xilinx_dpdma_chan *chan,
731                                struct xilinx_dpdma_sw_desc *sw_desc)
732 {
733         dma_pool_free(chan->desc_pool, sw_desc, sw_desc->phys);
734 }
735
736 /**
737  * xilinx_dpdma_chan_dump_tx_desc - Dump a tx descriptor
738  * @chan: DPDMA channel
739  * @tx_desc: tx descriptor to dump
740  *
741  * Dump contents of a tx descriptor
742  */
743 static void xilinx_dpdma_chan_dump_tx_desc(struct xilinx_dpdma_chan *chan,
744                                            struct xilinx_dpdma_tx_desc *tx_desc)
745 {
746         struct xilinx_dpdma_sw_desc *sw_desc;
747         struct device *dev = chan->xdev->dev;
748         unsigned int i = 0;
749
750         dev_dbg(dev, "------- TX descriptor dump start -------\n");
751         dev_dbg(dev, "------- channel ID = %d -------\n", chan->id);
752
753         list_for_each_entry(sw_desc, &tx_desc->descriptors, node) {
754                 struct xilinx_dpdma_hw_desc *hw_desc = &sw_desc->hw;
755
756                 dev_dbg(dev, "------- HW descriptor %d -------\n", i++);
757                 dev_dbg(dev, "descriptor phys: %pad\n", &sw_desc->phys);
758                 dev_dbg(dev, "control: 0x%08x\n", hw_desc->control);
759                 dev_dbg(dev, "desc_id: 0x%08x\n", hw_desc->desc_id);
760                 dev_dbg(dev, "xfer_size: 0x%08x\n", hw_desc->xfer_size);
761                 dev_dbg(dev, "hsize_stride: 0x%08x\n", hw_desc->hsize_stride);
762                 dev_dbg(dev, "timestamp_lsb: 0x%08x\n", hw_desc->timestamp_lsb);
763                 dev_dbg(dev, "timestamp_msb: 0x%08x\n", hw_desc->timestamp_msb);
764                 dev_dbg(dev, "addr_ext: 0x%08x\n", hw_desc->addr_ext);
765                 dev_dbg(dev, "next_desc: 0x%08x\n", hw_desc->next_desc);
766                 dev_dbg(dev, "src_addr: 0x%08x\n", hw_desc->src_addr);
767                 dev_dbg(dev, "addr_ext_23: 0x%08x\n", hw_desc->addr_ext_23);
768                 dev_dbg(dev, "addr_ext_45: 0x%08x\n", hw_desc->addr_ext_45);
769                 dev_dbg(dev, "src_addr2: 0x%08x\n", hw_desc->src_addr2);
770                 dev_dbg(dev, "src_addr3: 0x%08x\n", hw_desc->src_addr3);
771                 dev_dbg(dev, "src_addr4: 0x%08x\n", hw_desc->src_addr4);
772                 dev_dbg(dev, "src_addr5: 0x%08x\n", hw_desc->src_addr5);
773                 dev_dbg(dev, "crc: 0x%08x\n", hw_desc->crc);
774         }
775
776         dev_dbg(dev, "------- TX descriptor dump end -------\n");
777 }
778
779 /**
780  * xilinx_dpdma_chan_alloc_tx_desc - Allocate a transaction descriptor
781  * @chan: DPDMA channel
782  *
783  * Allocate a tx descriptor.
784  *
785  * Return: a tx descriptor or NULL.
786  */
787 static struct xilinx_dpdma_tx_desc *
788 xilinx_dpdma_chan_alloc_tx_desc(struct xilinx_dpdma_chan *chan)
789 {
790         struct xilinx_dpdma_tx_desc *tx_desc;
791
792         tx_desc = kzalloc(sizeof(*tx_desc), GFP_KERNEL);
793         if (!tx_desc)
794                 return NULL;
795
796         INIT_LIST_HEAD(&tx_desc->descriptors);
797         tx_desc->status = PREPARED;
798
799         return tx_desc;
800 }
801
802 /**
803  * xilinx_dpdma_chan_free_tx_desc - Free a transaction descriptor
804  * @chan: DPDMA channel
805  * @tx_desc: tx descriptor
806  *
807  * Free the tx descriptor @tx_desc including its software descriptors.
808  */
809 static void
810 xilinx_dpdma_chan_free_tx_desc(struct xilinx_dpdma_chan *chan,
811                                struct xilinx_dpdma_tx_desc *tx_desc)
812 {
813         struct xilinx_dpdma_sw_desc *sw_desc, *next;
814
815         if (!tx_desc)
816                 return;
817
818         list_for_each_entry_safe(sw_desc, next, &tx_desc->descriptors, node) {
819                 list_del(&sw_desc->node);
820                 xilinx_dpdma_chan_free_sw_desc(chan, sw_desc);
821         }
822
823         kfree(tx_desc);
824 }
825
826 /**
827  * xilinx_dpdma_chan_submit_tx_desc - Submit a transaction descriptor
828  * @chan: DPDMA channel
829  * @tx_desc: tx descriptor
830  *
831  * Submit the tx descriptor @tx_desc to the channel @chan.
832  *
833  * Return: a cookie assigned to the tx descriptor
834  */
835 static dma_cookie_t
836 xilinx_dpdma_chan_submit_tx_desc(struct xilinx_dpdma_chan *chan,
837                                  struct xilinx_dpdma_tx_desc *tx_desc)
838 {
839         struct xilinx_dpdma_sw_desc *sw_desc;
840         dma_cookie_t cookie;
841         unsigned long flags;
842
843         spin_lock_irqsave(&chan->lock, flags);
844
845         if (chan->submitted_desc) {
846                 cookie = chan->submitted_desc->async_tx.cookie;
847                 goto out_unlock;
848         }
849
850         cookie = dma_cookie_assign(&tx_desc->async_tx);
851
852         /* Assign the cookie to descriptors in this transaction */
853         /* Only 16 bit will be used, but it should be enough */
854         list_for_each_entry(sw_desc, &tx_desc->descriptors, node)
855                 sw_desc->hw.desc_id = cookie;
856
857         if (tx_desc != chan->allocated_desc)
858                 dev_err(chan->xdev->dev, "desc != allocated_desc\n");
859         else
860                 chan->allocated_desc = NULL;
861         chan->submitted_desc = tx_desc;
862
863         if (chan->id == VIDEO1 || chan->id == VIDEO2) {
864                 chan->video_group = true;
865                 chan->xdev->chan[VIDEO0]->video_group = true;
866         }
867
868 out_unlock:
869         spin_unlock_irqrestore(&chan->lock, flags);
870
871         return cookie;
872 }
873
874 /**
875  * xilinx_dpdma_chan_free_desc_list - Free a descriptor list
876  * @chan: DPDMA channel
877  * @list: tx descriptor list
878  *
879  * Free tx descriptors in the list @list.
880  */
881 static void xilinx_dpdma_chan_free_desc_list(struct xilinx_dpdma_chan *chan,
882                                              struct list_head *list)
883 {
884         struct xilinx_dpdma_tx_desc *tx_desc, *next;
885
886         list_for_each_entry_safe(tx_desc, next, list, node) {
887                 list_del(&tx_desc->node);
888                 xilinx_dpdma_chan_free_tx_desc(chan, tx_desc);
889         }
890 }
891
892 /**
893  * xilinx_dpdma_chan_free_all_desc - Free all descriptors of the channel
894  * @chan: DPDMA channel
895  *
896  * Free all descriptors associated with the channel. The channel should be
897  * disabled before this function is called, otherwise, this function may
898  * result in misbehavior of the system due to remaining outstanding
899  * transactions.
900  */
901 static void xilinx_dpdma_chan_free_all_desc(struct xilinx_dpdma_chan *chan)
902 {
903         unsigned long flags;
904
905         spin_lock_irqsave(&chan->lock, flags);
906
907         dev_dbg(chan->xdev->dev, "chan->status = %s\n",
908                 chan->status == STREAMING ? "STREAMING" : "IDLE");
909
910         xilinx_dpdma_chan_free_tx_desc(chan, chan->allocated_desc);
911         chan->allocated_desc = NULL;
912         xilinx_dpdma_chan_free_tx_desc(chan, chan->submitted_desc);
913         chan->submitted_desc = NULL;
914         xilinx_dpdma_chan_free_tx_desc(chan, chan->pending_desc);
915         chan->pending_desc = NULL;
916         xilinx_dpdma_chan_free_tx_desc(chan, chan->active_desc);
917         chan->active_desc = NULL;
918         xilinx_dpdma_chan_free_desc_list(chan, &chan->done_list);
919
920         spin_unlock_irqrestore(&chan->lock, flags);
921 }
922
923 /**
924  * xilinx_dpdma_chan_cleanup_desc - Clean up descriptors
925  * @chan: DPDMA channel
926  *
927  * Trigger the complete callbacks of descriptors with finished transactions.
928  * Free descriptors which are no longer in use.
929  */
930 static void xilinx_dpdma_chan_cleanup_desc(struct xilinx_dpdma_chan *chan)
931 {
932         struct xilinx_dpdma_tx_desc *desc;
933         dma_async_tx_callback callback;
934         void *callback_param;
935         unsigned long flags;
936         unsigned int cnt, i;
937
938         spin_lock_irqsave(&chan->lock, flags);
939
940         while (!list_empty(&chan->done_list)) {
941                 desc = list_first_entry(&chan->done_list,
942                                         struct xilinx_dpdma_tx_desc, node);
943                 list_del(&desc->node);
944
945                 cnt = desc->done_cnt;
946                 desc->done_cnt = 0;
947                 callback = desc->async_tx.callback;
948                 callback_param = desc->async_tx.callback_param;
949                 if (callback) {
950                         spin_unlock_irqrestore(&chan->lock, flags);
951                         for (i = 0; i < cnt; i++)
952                                 callback(callback_param);
953                         spin_lock_irqsave(&chan->lock, flags);
954                 }
955
956                 xilinx_dpdma_chan_free_tx_desc(chan, desc);
957         }
958
959         if (chan->active_desc) {
960                 cnt = chan->active_desc->done_cnt;
961                 chan->active_desc->done_cnt = 0;
962                 callback = chan->active_desc->async_tx.callback;
963                 callback_param = chan->active_desc->async_tx.callback_param;
964                 if (callback) {
965                         spin_unlock_irqrestore(&chan->lock, flags);
966                         for (i = 0; i < cnt; i++)
967                                 callback(callback_param);
968                         spin_lock_irqsave(&chan->lock, flags);
969                 }
970         }
971
972         spin_unlock_irqrestore(&chan->lock, flags);
973 }
974
975 /**
976  * xilinx_dpdma_chan_desc_active - Set the descriptor as active
977  * @chan: DPDMA channel
978  *
979  * Make the pending descriptor @chan->pending_desc as active. This function
980  * should be called when the channel starts operating on the pending descriptor.
981  */
982 static void xilinx_dpdma_chan_desc_active(struct xilinx_dpdma_chan *chan)
983 {
984         unsigned long flags;
985
986         spin_lock_irqsave(&chan->lock, flags);
987
988         if (!chan->pending_desc)
989                 goto out_unlock;
990
991         if (chan->active_desc)
992                 list_add_tail(&chan->active_desc->node, &chan->done_list);
993
994         chan->active_desc = chan->pending_desc;
995         chan->pending_desc = NULL;
996
997 out_unlock:
998         spin_unlock_irqrestore(&chan->lock, flags);
999 }
1000
1001 /**
1002  * xilinx_dpdma_chan_desc_done_intr - Mark the current descriptor as 'done'
1003  * @chan: DPDMA channel
1004  *
1005  * Mark the current active descriptor @chan->active_desc as 'done'. This
1006  * function should be called to mark completion of the currently active
1007  * descriptor.
1008  */
1009 static void xilinx_dpdma_chan_desc_done_intr(struct xilinx_dpdma_chan *chan)
1010 {
1011         unsigned long flags;
1012
1013         spin_lock_irqsave(&chan->lock, flags);
1014
1015         xilinx_dpdma_debugfs_intr_done_count_incr(chan->id);
1016
1017         if (!chan->active_desc) {
1018                 dev_dbg(chan->xdev->dev, "done intr with no active desc\n");
1019                 goto out_unlock;
1020         }
1021
1022         chan->active_desc->done_cnt++;
1023         if (chan->active_desc->status ==  PREPARED) {
1024                 dma_cookie_complete(&chan->active_desc->async_tx);
1025                 chan->active_desc->status = ACTIVE;
1026         }
1027
1028 out_unlock:
1029         spin_unlock_irqrestore(&chan->lock, flags);
1030         tasklet_schedule(&chan->done_task);
1031 }
1032
1033 /**
1034  * xilinx_dpdma_chan_prep_slave_sg - Prepare a scatter-gather dma descriptor
1035  * @chan: DPDMA channel
1036  * @sgl: scatter-gather list
1037  *
1038  * Prepare a tx descriptor incudling internal software/hardware descriptors
1039  * for the given scatter-gather transaction.
1040  *
1041  * Return: A dma async tx descriptor on success, or NULL.
1042  */
1043 static struct dma_async_tx_descriptor *
1044 xilinx_dpdma_chan_prep_slave_sg(struct xilinx_dpdma_chan *chan,
1045                                 struct scatterlist *sgl)
1046 {
1047         struct xilinx_dpdma_tx_desc *tx_desc;
1048         struct xilinx_dpdma_sw_desc *sw_desc, *last = NULL;
1049         struct scatterlist *iter = sgl;
1050         u32 line_size = 0;
1051
1052         if (chan->allocated_desc)
1053                 return &chan->allocated_desc->async_tx;
1054
1055         tx_desc = xilinx_dpdma_chan_alloc_tx_desc(chan);
1056         if (!tx_desc)
1057                 return NULL;
1058
1059         while (!sg_is_chain(iter))
1060                 line_size += sg_dma_len(iter++);
1061
1062         while (sgl) {
1063                 struct xilinx_dpdma_hw_desc *hw_desc;
1064                 dma_addr_t dma_addr[4];
1065                 unsigned int num_pages = 0;
1066
1067                 sw_desc = xilinx_dpdma_chan_alloc_sw_desc(chan);
1068                 if (!sw_desc)
1069                         goto error;
1070
1071                 while (!sg_is_chain(sgl) && !sg_is_last(sgl)) {
1072                         dma_addr[num_pages] = sg_dma_address(sgl++);
1073                         if (!IS_ALIGNED(dma_addr[num_pages++],
1074                                         XILINX_DPDMA_ALIGN_BYTES)) {
1075                                 dev_err(chan->xdev->dev,
1076                                         "buffer should be aligned at %d B\n",
1077                                         XILINX_DPDMA_ALIGN_BYTES);
1078                                 goto error;
1079                         }
1080                 }
1081
1082                 chan->xdev->desc_addr(sw_desc, last, dma_addr, num_pages);
1083                 hw_desc = &sw_desc->hw;
1084                 hw_desc->xfer_size = line_size;
1085                 hw_desc->hsize_stride =
1086                         line_size << XILINX_DPDMA_DESC_HSIZE_STRIDE_HSIZE_SHIFT;
1087                 hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_PREEMBLE;
1088                 hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_FRAG_MODE;
1089                 hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_IGNORE_DONE;
1090
1091                 list_add_tail(&sw_desc->node, &tx_desc->descriptors);
1092                 last = sw_desc;
1093                 if (sg_is_last(sgl))
1094                         break;
1095                 sgl = sg_chain_ptr(sgl);
1096         }
1097
1098         sw_desc = list_first_entry(&tx_desc->descriptors,
1099                                    struct xilinx_dpdma_sw_desc, node);
1100         if (chan->xdev->ext_addr)
1101                 xilinx_dpdma_sw_desc_next_64(last, sw_desc);
1102         else
1103                 xilinx_dpdma_sw_desc_next_32(last, sw_desc);
1104         last->hw.control |= XILINX_DPDMA_DESC_CONTROL_COMPLETE_INTR;
1105         last->hw.control |= XILINX_DPDMA_DESC_CONTROL_LAST_OF_FRAME;
1106
1107         chan->allocated_desc = tx_desc;
1108
1109         return &tx_desc->async_tx;
1110
1111 error:
1112         xilinx_dpdma_chan_free_tx_desc(chan, tx_desc);
1113
1114         return NULL;
1115 }
1116
1117 /**
1118  * xilinx_dpdma_chan_prep_cyclic - Prepare a cyclic dma descriptor
1119  * @chan: DPDMA channel
1120  * @buf_addr: buffer address
1121  * @buf_len: buffer length
1122  * @period_len: number of periods
1123  *
1124  * Prepare a tx descriptor incudling internal software/hardware descriptors
1125  * for the given cyclic transaction.
1126  *
1127  * Return: A dma async tx descriptor on success, or NULL.
1128  */
1129 static struct dma_async_tx_descriptor *
1130 xilinx_dpdma_chan_prep_cyclic(struct xilinx_dpdma_chan *chan,
1131                               dma_addr_t buf_addr, size_t buf_len,
1132                               size_t period_len)
1133 {
1134         struct xilinx_dpdma_tx_desc *tx_desc;
1135         struct xilinx_dpdma_sw_desc *sw_desc, *last = NULL;
1136         unsigned int periods = buf_len / period_len;
1137         unsigned int i;
1138
1139         if (chan->allocated_desc)
1140                 return &chan->allocated_desc->async_tx;
1141
1142         tx_desc = xilinx_dpdma_chan_alloc_tx_desc(chan);
1143         if (!tx_desc)
1144                 return NULL;
1145
1146         for (i = 0; i < periods; i++) {
1147                 struct xilinx_dpdma_hw_desc *hw_desc;
1148
1149                 if (!IS_ALIGNED(buf_addr, XILINX_DPDMA_ALIGN_BYTES)) {
1150                         dev_err(chan->xdev->dev,
1151                                 "buffer should be aligned at %d B\n",
1152                                 XILINX_DPDMA_ALIGN_BYTES);
1153                         goto error;
1154                 }
1155
1156                 sw_desc = xilinx_dpdma_chan_alloc_sw_desc(chan);
1157                 if (!sw_desc)
1158                         goto error;
1159
1160                 chan->xdev->desc_addr(sw_desc, last, &buf_addr, 1);
1161                 hw_desc = &sw_desc->hw;
1162                 hw_desc->xfer_size = period_len;
1163                 hw_desc->hsize_stride =
1164                         period_len <<
1165                         XILINX_DPDMA_DESC_HSIZE_STRIDE_HSIZE_SHIFT;
1166                 hw_desc->hsize_stride |=
1167                         period_len <<
1168                         XILINX_DPDMA_DESC_HSIZE_STRIDE_STRIDE_SHIFT;
1169                 hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_PREEMBLE;
1170                 hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_IGNORE_DONE;
1171                 hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_COMPLETE_INTR;
1172
1173                 list_add_tail(&sw_desc->node, &tx_desc->descriptors);
1174
1175                 buf_addr += period_len;
1176                 last = sw_desc;
1177         }
1178
1179         sw_desc = list_first_entry(&tx_desc->descriptors,
1180                                    struct xilinx_dpdma_sw_desc, node);
1181         if (chan->xdev->ext_addr)
1182                 xilinx_dpdma_sw_desc_next_64(last, sw_desc);
1183         else
1184                 xilinx_dpdma_sw_desc_next_32(last, sw_desc);
1185         last->hw.control |= XILINX_DPDMA_DESC_CONTROL_LAST_OF_FRAME;
1186
1187         chan->allocated_desc = tx_desc;
1188
1189         return &tx_desc->async_tx;
1190
1191 error:
1192         xilinx_dpdma_chan_free_tx_desc(chan, tx_desc);
1193
1194         return NULL;
1195 }
1196
1197 /**
1198  * xilinx_dpdma_chan_prep_interleaved - Prepare a interleaved dma descriptor
1199  * @chan: DPDMA channel
1200  * @xt: dma interleaved template
1201  *
1202  * Prepare a tx descriptor incudling internal software/hardware descriptors
1203  * based on @xt.
1204  *
1205  * Return: A dma async tx descriptor on success, or NULL.
1206  */
1207 static struct dma_async_tx_descriptor *
1208 xilinx_dpdma_chan_prep_interleaved(struct xilinx_dpdma_chan *chan,
1209                                    struct dma_interleaved_template *xt)
1210 {
1211         struct xilinx_dpdma_tx_desc *tx_desc;
1212         struct xilinx_dpdma_sw_desc *sw_desc;
1213         struct xilinx_dpdma_hw_desc *hw_desc;
1214         size_t hsize = xt->sgl[0].size;
1215         size_t stride = hsize + xt->sgl[0].icg;
1216
1217         if (!IS_ALIGNED(xt->src_start, XILINX_DPDMA_ALIGN_BYTES)) {
1218                 dev_err(chan->xdev->dev, "buffer should be aligned at %d B\n",
1219                         XILINX_DPDMA_ALIGN_BYTES);
1220                 return NULL;
1221         }
1222
1223         if (chan->allocated_desc)
1224                 return &chan->allocated_desc->async_tx;
1225
1226         tx_desc = xilinx_dpdma_chan_alloc_tx_desc(chan);
1227         if (!tx_desc)
1228                 return NULL;
1229
1230         sw_desc = xilinx_dpdma_chan_alloc_sw_desc(chan);
1231         if (!sw_desc)
1232                 goto error;
1233
1234         chan->xdev->desc_addr(sw_desc, sw_desc, &xt->src_start, 1);
1235         hw_desc = &sw_desc->hw;
1236         hw_desc->xfer_size = hsize * xt->numf;
1237         hw_desc->hsize_stride = hsize <<
1238                                 XILINX_DPDMA_DESC_HSIZE_STRIDE_HSIZE_SHIFT;
1239         hw_desc->hsize_stride |= (stride / 16) <<
1240                                  XILINX_DPDMA_DESC_HSIZE_STRIDE_STRIDE_SHIFT;
1241         hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_PREEMBLE;
1242         hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_COMPLETE_INTR;
1243         hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_IGNORE_DONE;
1244         hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_LAST_OF_FRAME;
1245
1246         list_add_tail(&sw_desc->node, &tx_desc->descriptors);
1247         chan->allocated_desc = tx_desc;
1248
1249         return &tx_desc->async_tx;
1250
1251 error:
1252         xilinx_dpdma_chan_free_tx_desc(chan, tx_desc);
1253
1254         return NULL;
1255 }
1256
1257 /* Xilinx DPDMA channel operations */
1258
1259 /**
1260  * xilinx_dpdma_chan_enable - Enable the channel
1261  * @chan: DPDMA channel
1262  *
1263  * Enable the channel and its interrupts. Set the QoS values for video class.
1264  */
1265 static inline void xilinx_dpdma_chan_enable(struct xilinx_dpdma_chan *chan)
1266 {
1267         u32 reg;
1268
1269         reg = XILINX_DPDMA_INTR_CHAN_MASK << chan->id;
1270         reg |= XILINX_DPDMA_INTR_GLOBAL_MASK;
1271         dpdma_write(chan->xdev->reg, XILINX_DPDMA_IEN, reg);
1272         reg = XILINX_DPDMA_EINTR_CHAN_ERR_MASK << chan->id;
1273         reg |= XILINX_DPDMA_INTR_GLOBAL_ERR;
1274         dpdma_write(chan->xdev->reg, XILINX_DPDMA_EIEN, reg);
1275
1276         reg = XILINX_DPDMA_CH_CNTL_ENABLE;
1277         reg |= XILINX_DPDMA_CH_CNTL_QOS_VID_CLASS <<
1278                XILINX_DPDMA_CH_CNTL_QOS_DSCR_WR_SHIFT;
1279         reg |= XILINX_DPDMA_CH_CNTL_QOS_VID_CLASS <<
1280                XILINX_DPDMA_CH_CNTL_QOS_DSCR_RD_SHIFT;
1281         reg |= XILINX_DPDMA_CH_CNTL_QOS_VID_CLASS <<
1282                XILINX_DPDMA_CH_CNTL_QOS_DATA_RD_SHIFT;
1283         dpdma_set(chan->reg, XILINX_DPDMA_CH_CNTL, reg);
1284 }
1285
1286 /**
1287  * xilinx_dpdma_chan_disable - Disable the channel
1288  * @chan: DPDMA channel
1289  *
1290  * Disable the channel and its interrupts.
1291  */
1292 static inline void xilinx_dpdma_chan_disable(struct xilinx_dpdma_chan *chan)
1293 {
1294         u32 reg;
1295
1296         reg = XILINX_DPDMA_INTR_CHAN_MASK << chan->id;
1297         dpdma_write(chan->xdev->reg, XILINX_DPDMA_IEN, reg);
1298         reg = XILINX_DPDMA_EINTR_CHAN_ERR_MASK << chan->id;
1299         dpdma_write(chan->xdev->reg, XILINX_DPDMA_EIEN, reg);
1300
1301         dpdma_clr(chan->reg, XILINX_DPDMA_CH_CNTL, XILINX_DPDMA_CH_CNTL_ENABLE);
1302 }
1303
1304 /**
1305  * xilinx_dpdma_chan_pause - Pause the channel
1306  * @chan: DPDMA channel
1307  *
1308  * Pause the channel.
1309  */
1310 static inline void xilinx_dpdma_chan_pause(struct xilinx_dpdma_chan *chan)
1311 {
1312         dpdma_set(chan->reg, XILINX_DPDMA_CH_CNTL, XILINX_DPDMA_CH_CNTL_PAUSE);
1313 }
1314
1315 /**
1316  * xilinx_dpdma_chan_unpause - Unpause the channel
1317  * @chan: DPDMA channel
1318  *
1319  * Unpause the channel.
1320  */
1321 static inline void xilinx_dpdma_chan_unpause(struct xilinx_dpdma_chan *chan)
1322 {
1323         dpdma_clr(chan->reg, XILINX_DPDMA_CH_CNTL, XILINX_DPDMA_CH_CNTL_PAUSE);
1324 }
1325
1326 static u32
1327 xilinx_dpdma_chan_video_group_ready(struct xilinx_dpdma_chan *chan)
1328 {
1329         struct xilinx_dpdma_device *xdev = chan->xdev;
1330         u32 i = 0, ret = 0;
1331
1332         for (i = VIDEO0; i < GRAPHICS; i++) {
1333                 if (xdev->chan[i]->video_group &&
1334                     xdev->chan[i]->status != STREAMING)
1335                         return 0;
1336
1337                 if (xdev->chan[i]->video_group)
1338                         ret |= BIT(i);
1339         }
1340
1341         return ret;
1342 }
1343
1344 /**
1345  * xilinx_dpdma_chan_issue_pending - Issue the pending descriptor
1346  * @chan: DPDMA channel
1347  *
1348  * Issue the first pending descriptor from @chan->submitted_desc. If the channel
1349  * is already streaming, the channel is re-triggered with the pending
1350  * descriptor.
1351  */
1352 static void xilinx_dpdma_chan_issue_pending(struct xilinx_dpdma_chan *chan)
1353 {
1354         struct xilinx_dpdma_device *xdev = chan->xdev;
1355         struct xilinx_dpdma_sw_desc *sw_desc;
1356         unsigned long flags;
1357         u32 reg, channels;
1358
1359         spin_lock_irqsave(&chan->lock, flags);
1360
1361         if (!chan->submitted_desc || chan->pending_desc)
1362                 goto out_unlock;
1363
1364         chan->pending_desc = chan->submitted_desc;
1365         chan->submitted_desc = NULL;
1366
1367         sw_desc = list_first_entry(&chan->pending_desc->descriptors,
1368                                    struct xilinx_dpdma_sw_desc, node);
1369         dpdma_write(chan->reg, XILINX_DPDMA_CH_DESC_START_ADDR,
1370                     (u32)sw_desc->phys);
1371         if (xdev->ext_addr)
1372                 dpdma_write(chan->reg, XILINX_DPDMA_CH_DESC_START_ADDRE,
1373                             ((u64)sw_desc->phys >> 32) &
1374                             XILINX_DPDMA_DESC_ADDR_EXT_ADDR_MASK);
1375
1376         if (chan->first_frame) {
1377                 chan->first_frame = false;
1378                 if (chan->video_group) {
1379                         channels = xilinx_dpdma_chan_video_group_ready(chan);
1380                         if (!channels)
1381                                 goto out_unlock;
1382                         reg = channels << XILINX_DPDMA_GBL_TRIG_SHIFT;
1383                 } else {
1384                         reg = 1 << (XILINX_DPDMA_GBL_TRIG_SHIFT + chan->id);
1385                 }
1386         } else {
1387                 if (chan->video_group) {
1388                         channels = xilinx_dpdma_chan_video_group_ready(chan);
1389                         if (!channels)
1390                                 goto out_unlock;
1391                         reg = channels << XILINX_DPDMA_GBL_RETRIG_SHIFT;
1392                 } else {
1393                         reg = 1 << (XILINX_DPDMA_GBL_RETRIG_SHIFT + chan->id);
1394                 }
1395         }
1396
1397         dpdma_write(xdev->reg, XILINX_DPDMA_GBL, reg);
1398
1399 out_unlock:
1400         spin_unlock_irqrestore(&chan->lock, flags);
1401 }
1402
1403 /**
1404  * xilinx_dpdma_chan_start - Start the channel
1405  * @chan: DPDMA channel
1406  *
1407  * Start the channel by enabling interrupts and triggering the channel.
1408  * If the channel is enabled already or there's no pending descriptor, this
1409  * function won't do anything on the channel.
1410  */
1411 static void xilinx_dpdma_chan_start(struct xilinx_dpdma_chan *chan)
1412 {
1413         unsigned long flags;
1414
1415         spin_lock_irqsave(&chan->lock, flags);
1416
1417         if (!chan->submitted_desc || chan->status == STREAMING)
1418                 goto out_unlock;
1419
1420         xilinx_dpdma_chan_unpause(chan);
1421         xilinx_dpdma_chan_enable(chan);
1422         chan->first_frame = true;
1423         chan->status = STREAMING;
1424
1425 out_unlock:
1426         spin_unlock_irqrestore(&chan->lock, flags);
1427 }
1428
1429 /**
1430  * xilinx_dpdma_chan_ostand - Number of outstanding transactions
1431  * @chan: DPDMA channel
1432  *
1433  * Read and return the number of outstanding transactions from register.
1434  *
1435  * Return: Number of outstanding transactions from the status register.
1436  */
1437 static inline u32 xilinx_dpdma_chan_ostand(struct xilinx_dpdma_chan *chan)
1438 {
1439         return dpdma_read(chan->reg, XILINX_DPDMA_CH_STATUS) >>
1440                XILINX_DPDMA_CH_STATUS_OTRAN_CNT_SHIFT &
1441                XILINX_DPDMA_CH_STATUS_OTRAN_CNT_MASK;
1442 }
1443
1444 /**
1445  * xilinx_dpdma_chan_no_ostand - Notify no outstanding transaction event
1446  * @chan: DPDMA channel
1447  *
1448  * Notify waiters for no outstanding event, so waiters can stop the channel
1449  * safely. This function is supposed to be called when 'no oustanding' interrupt
1450  * is generated. The 'no outstanding' interrupt is disabled and should be
1451  * re-enabled when this event is handled. If the channel status register still
1452  * shows some number of outstanding transactions, the interrupt remains enabled.
1453  *
1454  * Return: 0 on success. On failure, -EWOULDBLOCK if there's still outstanding
1455  * transaction(s).
1456  */
1457 static int xilinx_dpdma_chan_notify_no_ostand(struct xilinx_dpdma_chan *chan)
1458 {
1459         u32 cnt;
1460
1461         cnt = xilinx_dpdma_chan_ostand(chan);
1462         if (cnt) {
1463                 dev_dbg(chan->xdev->dev, "%d outstanding transactions\n", cnt);
1464                 return -EWOULDBLOCK;
1465         }
1466
1467         /* Disable 'no oustanding' interrupt */
1468         dpdma_write(chan->xdev->reg, XILINX_DPDMA_IDS,
1469                     1 << (XILINX_DPDMA_INTR_NO_OSTAND_SHIFT + chan->id));
1470         wake_up(&chan->wait_to_stop);
1471
1472         return 0;
1473 }
1474
1475 /**
1476  * xilinx_dpdma_chan_wait_no_ostand - Wait for the oustanding transaction intr
1477  * @chan: DPDMA channel
1478  *
1479  * Wait for the no outstanding transaction interrupt. This functions can sleep
1480  * for 50ms.
1481  *
1482  * Return: 0 on success. On failure, -ETIMEOUT for time out, or the error code
1483  * from wait_event_interruptible_timeout().
1484  */
1485 static int xilinx_dpdma_chan_wait_no_ostand(struct xilinx_dpdma_chan *chan)
1486 {
1487         int ret;
1488
1489         /* Wait for a no outstanding transaction interrupt upto 50msec */
1490         ret = wait_event_interruptible_timeout(chan->wait_to_stop,
1491                                                !xilinx_dpdma_chan_ostand(chan),
1492                                                msecs_to_jiffies(50));
1493         if (ret > 0) {
1494                 dpdma_write(chan->xdev->reg, XILINX_DPDMA_IEN,
1495                             1 <<
1496                             (XILINX_DPDMA_INTR_NO_OSTAND_SHIFT + chan->id));
1497                 return 0;
1498         }
1499
1500         dev_err(chan->xdev->dev, "not ready to stop: %d trans\n",
1501                 xilinx_dpdma_chan_ostand(chan));
1502
1503         if (ret == 0)
1504                 return -ETIMEDOUT;
1505
1506         return ret;
1507 }
1508
1509 /**
1510  * xilinx_dpdma_chan_poll_no_ostand - Poll the oustanding transaction status reg
1511  * @chan: DPDMA channel
1512  *
1513  * Poll the outstanding transaction status, and return when there's no
1514  * outstanding transaction. This functions can be used in the interrupt context
1515  * or where the atomicity is required. Calling thread may wait more than 50ms.
1516  *
1517  * Return: 0 on success, or -ETIMEDOUT.
1518  */
1519 static int xilinx_dpdma_chan_poll_no_ostand(struct xilinx_dpdma_chan *chan)
1520 {
1521         u32 cnt, loop = 50000;
1522
1523         /* Poll at least for 50ms (20 fps). */
1524         do {
1525                 cnt = xilinx_dpdma_chan_ostand(chan);
1526                 udelay(1);
1527         } while (loop-- > 0 && cnt);
1528
1529         if (loop) {
1530                 dpdma_write(chan->xdev->reg, XILINX_DPDMA_IEN,
1531                             1 <<
1532                             (XILINX_DPDMA_INTR_NO_OSTAND_SHIFT + chan->id));
1533                 return 0;
1534         }
1535
1536         dev_err(chan->xdev->dev, "not ready to stop: %d trans\n",
1537                 xilinx_dpdma_chan_ostand(chan));
1538
1539         return -ETIMEDOUT;
1540 }
1541
1542 /**
1543  * xilinx_dpdma_chan_stop - Stop the channel
1544  * @chan: DPDMA channel
1545  *
1546  * Stop the channel with the following sequence: 1. Pause, 2. Wait (sleep) for
1547  * no outstanding transaction interrupt, 3. Disable the channel.
1548  *
1549  * Return: 0 on success, or error code from xilinx_dpdma_chan_wait_no_ostand().
1550  */
1551 static int xilinx_dpdma_chan_stop(struct xilinx_dpdma_chan *chan)
1552 {
1553         unsigned long flags;
1554         bool ret;
1555
1556         xilinx_dpdma_chan_pause(chan);
1557         ret = xilinx_dpdma_chan_wait_no_ostand(chan);
1558         if (ret)
1559                 return ret;
1560
1561         spin_lock_irqsave(&chan->lock, flags);
1562         xilinx_dpdma_chan_disable(chan);
1563         chan->status = IDLE;
1564         spin_unlock_irqrestore(&chan->lock, flags);
1565
1566         return 0;
1567 }
1568
1569 /**
1570  * xilinx_dpdma_chan_alloc_resources - Allocate resources for the channel
1571  * @chan: DPDMA channel
1572  *
1573  * Allocate a descriptor pool for the channel.
1574  *
1575  * Return: 0 on success, or -ENOMEM if failed to allocate a pool.
1576  */
1577 static int xilinx_dpdma_chan_alloc_resources(struct xilinx_dpdma_chan *chan)
1578 {
1579         chan->desc_pool = dma_pool_create(dev_name(chan->xdev->dev),
1580                                 chan->xdev->dev,
1581                                 sizeof(struct xilinx_dpdma_sw_desc),
1582                                 __alignof__(struct xilinx_dpdma_sw_desc), 0);
1583         if (!chan->desc_pool) {
1584                 dev_err(chan->xdev->dev,
1585                         "failed to allocate a descriptor pool\n");
1586                 return -ENOMEM;
1587         }
1588
1589         return 0;
1590 }
1591
1592 /**
1593  * xilinx_dpdma_chan_free_resources - Free all resources for the channel
1594  * @chan: DPDMA channel
1595  *
1596  * Free all descriptors and the descriptor pool for the channel.
1597  */
1598 static void xilinx_dpdma_chan_free_resources(struct xilinx_dpdma_chan *chan)
1599 {
1600         xilinx_dpdma_chan_free_all_desc(chan);
1601         dma_pool_destroy(chan->desc_pool);
1602         chan->desc_pool = NULL;
1603 }
1604
1605 /**
1606  * xilinx_dpdma_chan_terminate_all - Terminate the channel and descriptors
1607  * @chan: DPDMA channel
1608  *
1609  * Stop the channel and free all associated descriptors.
1610  *
1611  * Return: 0 on success, or the error code from xilinx_dpdma_chan_stop().
1612  */
1613 static int xilinx_dpdma_chan_terminate_all(struct xilinx_dpdma_chan *chan)
1614 {
1615         struct xilinx_dpdma_device *xdev = chan->xdev;
1616         int ret;
1617         unsigned int i;
1618
1619         if (chan->video_group) {
1620                 for (i = VIDEO0; i < GRAPHICS; i++) {
1621                         if (xdev->chan[i]->video_group &&
1622                             xdev->chan[i]->status == STREAMING) {
1623                                 xilinx_dpdma_chan_pause(xdev->chan[i]);
1624                                 xdev->chan[i]->video_group = false;
1625                         }
1626                 }
1627         }
1628
1629         ret = xilinx_dpdma_chan_stop(chan);
1630         if (ret)
1631                 return ret;
1632
1633         xilinx_dpdma_chan_free_all_desc(chan);
1634
1635         return 0;
1636 }
1637
1638 /**
1639  * xilinx_dpdma_chan_err - Detect any channel error
1640  * @chan: DPDMA channel
1641  * @isr: masked Interrupt Status Register
1642  * @eisr: Error Interrupt Status Register
1643  *
1644  * Return: true if any channel error occurs, or false otherwise.
1645  */
1646 static bool
1647 xilinx_dpdma_chan_err(struct xilinx_dpdma_chan *chan, u32 isr, u32 eisr)
1648 {
1649         if (!chan)
1650                 return false;
1651
1652         if (chan->status == STREAMING &&
1653             ((isr & (XILINX_DPDMA_INTR_CHAN_ERR_MASK << chan->id)) ||
1654             (eisr & (XILINX_DPDMA_EINTR_CHAN_ERR_MASK << chan->id))))
1655                 return true;
1656
1657         return false;
1658 }
1659
1660 /**
1661  * xilinx_dpdma_chan_handle_err - DPDMA channel error handling
1662  * @chan: DPDMA channel
1663  *
1664  * This function is called when any channel error or any global error occurs.
1665  * The function disables the paused channel by errors and determines
1666  * if the current active descriptor can be rescheduled depending on
1667  * the descriptor status.
1668  */
1669 static void xilinx_dpdma_chan_handle_err(struct xilinx_dpdma_chan *chan)
1670 {
1671         struct xilinx_dpdma_device *xdev = chan->xdev;
1672         struct device *dev = xdev->dev;
1673         unsigned long flags;
1674
1675         spin_lock_irqsave(&chan->lock, flags);
1676
1677         dev_dbg(dev, "cur desc addr = 0x%04x%08x\n",
1678                 dpdma_read(chan->reg, XILINX_DPDMA_CH_DESC_START_ADDRE),
1679                 dpdma_read(chan->reg, XILINX_DPDMA_CH_DESC_START_ADDR));
1680         dev_dbg(dev, "cur payload addr = 0x%04x%08x\n",
1681                 dpdma_read(chan->reg, XILINX_DPDMA_CH_PYLD_CUR_ADDRE),
1682                 dpdma_read(chan->reg, XILINX_DPDMA_CH_PYLD_CUR_ADDR));
1683
1684         xilinx_dpdma_chan_disable(chan);
1685         chan->status = IDLE;
1686
1687         /* Decide if the current descriptor can be rescheduled */
1688         if (chan->active_desc) {
1689                 switch (chan->active_desc->status) {
1690                 case ACTIVE:
1691                 case PREPARED:
1692                         xilinx_dpdma_chan_free_tx_desc(chan,
1693                                                        chan->submitted_desc);
1694                         chan->submitted_desc = NULL;
1695                         xilinx_dpdma_chan_free_tx_desc(chan,
1696                                                        chan->pending_desc);
1697                         chan->pending_desc = NULL;
1698                         chan->active_desc->status = ERRORED;
1699                         chan->submitted_desc = chan->active_desc;
1700                         break;
1701                 case ERRORED:
1702                         dev_err(dev, "desc is dropped by unrecoverable err\n");
1703                         xilinx_dpdma_chan_dump_tx_desc(chan, chan->active_desc);
1704                         xilinx_dpdma_chan_free_tx_desc(chan, chan->active_desc);
1705                         break;
1706                 default:
1707                         break;
1708                 }
1709                 chan->active_desc = NULL;
1710         }
1711
1712         spin_unlock_irqrestore(&chan->lock, flags);
1713 }
1714
1715 /* DMA tx descriptor */
1716
1717 static dma_cookie_t xilinx_dpdma_tx_submit(struct dma_async_tx_descriptor *tx)
1718 {
1719         struct xilinx_dpdma_chan *chan = to_xilinx_chan(tx->chan);
1720         struct xilinx_dpdma_tx_desc *tx_desc = to_dpdma_tx_desc(tx);
1721
1722         return xilinx_dpdma_chan_submit_tx_desc(chan, tx_desc);
1723 }
1724
1725 /* DMA channel operations */
1726
1727 static struct dma_async_tx_descriptor *
1728 xilinx_dpdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
1729                            unsigned int sg_len,
1730                            enum dma_transfer_direction direction,
1731                            unsigned long flags, void *context)
1732 {
1733         struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
1734         struct dma_async_tx_descriptor *async_tx;
1735
1736         if (direction != DMA_MEM_TO_DEV)
1737                 return NULL;
1738
1739         if (!sgl || sg_len < 2)
1740                 return NULL;
1741
1742         async_tx = xilinx_dpdma_chan_prep_slave_sg(chan, sgl);
1743         if (!async_tx)
1744                 return NULL;
1745
1746         dma_async_tx_descriptor_init(async_tx, dchan);
1747         async_tx->tx_submit = xilinx_dpdma_tx_submit;
1748         async_tx->flags = flags;
1749         async_tx_ack(async_tx);
1750
1751         return async_tx;
1752 }
1753
1754 static struct dma_async_tx_descriptor *
1755 xilinx_dpdma_prep_dma_cyclic(struct dma_chan *dchan, dma_addr_t buf_addr,
1756                              size_t buf_len, size_t period_len,
1757                              enum dma_transfer_direction direction,
1758                              unsigned long flags)
1759 {
1760         struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
1761         struct dma_async_tx_descriptor *async_tx;
1762
1763         if (direction != DMA_MEM_TO_DEV)
1764                 return NULL;
1765
1766         if (buf_len % period_len)
1767                 return NULL;
1768
1769         async_tx = xilinx_dpdma_chan_prep_cyclic(chan, buf_addr, buf_len,
1770                                                  period_len);
1771         if (!async_tx)
1772                 return NULL;
1773
1774         dma_async_tx_descriptor_init(async_tx, dchan);
1775         async_tx->tx_submit = xilinx_dpdma_tx_submit;
1776         async_tx->flags = flags;
1777         async_tx_ack(async_tx);
1778
1779         return async_tx;
1780 }
1781
1782 static struct dma_async_tx_descriptor *
1783 xilinx_dpdma_prep_interleaved_dma(struct dma_chan *dchan,
1784                                   struct dma_interleaved_template *xt,
1785                                   unsigned long flags)
1786 {
1787         struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
1788         struct dma_async_tx_descriptor *async_tx;
1789
1790         if (xt->dir != DMA_MEM_TO_DEV)
1791                 return NULL;
1792
1793         if (!xt->numf || !xt->sgl[0].size)
1794                 return NULL;
1795
1796         async_tx = xilinx_dpdma_chan_prep_interleaved(chan, xt);
1797         if (!async_tx)
1798                 return NULL;
1799
1800         dma_async_tx_descriptor_init(async_tx, dchan);
1801         async_tx->tx_submit = xilinx_dpdma_tx_submit;
1802         async_tx->flags = flags;
1803         async_tx_ack(async_tx);
1804
1805         return async_tx;
1806 }
1807
1808 static int xilinx_dpdma_alloc_chan_resources(struct dma_chan *dchan)
1809 {
1810         struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
1811
1812         dma_cookie_init(dchan);
1813
1814         return xilinx_dpdma_chan_alloc_resources(chan);
1815 }
1816
1817 static void xilinx_dpdma_free_chan_resources(struct dma_chan *dchan)
1818 {
1819         struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
1820
1821         xilinx_dpdma_chan_free_resources(chan);
1822 }
1823
1824 static enum dma_status xilinx_dpdma_tx_status(struct dma_chan *dchan,
1825                                               dma_cookie_t cookie,
1826                                               struct dma_tx_state *txstate)
1827 {
1828         return dma_cookie_status(dchan, cookie, txstate);
1829 }
1830
1831 static void xilinx_dpdma_issue_pending(struct dma_chan *dchan)
1832 {
1833         struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
1834
1835         xilinx_dpdma_chan_start(chan);
1836         xilinx_dpdma_chan_issue_pending(chan);
1837 }
1838
1839 static int xilinx_dpdma_config(struct dma_chan *dchan,
1840                                struct dma_slave_config *config)
1841 {
1842         if (config->direction != DMA_MEM_TO_DEV)
1843                 return -EINVAL;
1844
1845         return 0;
1846 }
1847
1848 static int xilinx_dpdma_pause(struct dma_chan *dchan)
1849 {
1850         xilinx_dpdma_chan_pause(to_xilinx_chan(dchan));
1851
1852         return 0;
1853 }
1854
1855 static int xilinx_dpdma_resume(struct dma_chan *dchan)
1856 {
1857         xilinx_dpdma_chan_unpause(to_xilinx_chan(dchan));
1858
1859         return 0;
1860 }
1861
1862 static int xilinx_dpdma_terminate_all(struct dma_chan *dchan)
1863 {
1864         return xilinx_dpdma_chan_terminate_all(to_xilinx_chan(dchan));
1865 }
1866
1867 /* Xilinx DPDMA device operations */
1868
1869 /**
1870  * xilinx_dpdma_err - Detect any global error
1871  * @isr: Interrupt Status Register
1872  * @eisr: Error Interrupt Status Register
1873  *
1874  * Return: True if any global error occurs, or false otherwise.
1875  */
1876 static bool xilinx_dpdma_err(u32 isr, u32 eisr)
1877 {
1878         if ((isr & XILINX_DPDMA_INTR_GLOBAL_ERR ||
1879             eisr & XILINX_DPDMA_EINTR_GLOBAL_ERR))
1880                 return true;
1881
1882         return false;
1883 }
1884
1885 /**
1886  * xilinx_dpdma_handle_err_intr - Handle DPDMA error interrupt
1887  * @xdev: DPDMA device
1888  * @isr: masked Interrupt Status Register
1889  * @eisr: Error Interrupt Status Register
1890  *
1891  * Handle if any error occurs based on @isr and @eisr. This function disables
1892  * corresponding error interrupts, and those should be re-enabled once handling
1893  * is done.
1894  */
1895 static void xilinx_dpdma_handle_err_intr(struct xilinx_dpdma_device *xdev,
1896                                          u32 isr, u32 eisr)
1897 {
1898         bool err = xilinx_dpdma_err(isr, eisr);
1899         unsigned int i;
1900
1901         dev_err(xdev->dev, "error intr: isr = 0x%08x, eisr = 0x%08x\n",
1902                 isr, eisr);
1903
1904         /* Disable channel error interrupts until errors are handled. */
1905         dpdma_write(xdev->reg, XILINX_DPDMA_IDS,
1906                     isr & ~XILINX_DPDMA_INTR_GLOBAL_ERR);
1907         dpdma_write(xdev->reg, XILINX_DPDMA_EIDS,
1908                     eisr & ~XILINX_DPDMA_EINTR_GLOBAL_ERR);
1909
1910         for (i = 0; i < XILINX_DPDMA_NUM_CHAN; i++)
1911                 if (err || xilinx_dpdma_chan_err(xdev->chan[i], isr, eisr))
1912                         tasklet_schedule(&xdev->chan[i]->err_task);
1913 }
1914
1915 /**
1916  * xilinx_dpdma_handle_vsync_intr - Handle the VSYNC interrupt
1917  * @xdev: DPDMA device
1918  *
1919  * Handle the VSYNC event. At this point, the current frame becomes active,
1920  * which means the DPDMA actually starts fetching, and the next frame can be
1921  * scheduled.
1922  */
1923 static void xilinx_dpdma_handle_vsync_intr(struct xilinx_dpdma_device *xdev)
1924 {
1925         unsigned int i;
1926
1927         for (i = 0; i < XILINX_DPDMA_NUM_CHAN; i++) {
1928                 if (xdev->chan[i] &&
1929                     xdev->chan[i]->status == STREAMING) {
1930                         xilinx_dpdma_chan_desc_active(xdev->chan[i]);
1931                         xilinx_dpdma_chan_issue_pending(xdev->chan[i]);
1932                 }
1933         }
1934 }
1935
1936 /**
1937  * xilinx_dpdma_enable_intr - Enable interrupts
1938  * @xdev: DPDMA device
1939  *
1940  * Enable interrupts.
1941  */
1942 static void xilinx_dpdma_enable_intr(struct xilinx_dpdma_device *xdev)
1943 {
1944         dpdma_write(xdev->reg, XILINX_DPDMA_IEN, XILINX_DPDMA_INTR_ALL);
1945         dpdma_write(xdev->reg, XILINX_DPDMA_EIEN, XILINX_DPDMA_EINTR_ALL);
1946 }
1947
1948 /**
1949  * xilinx_dpdma_disable_intr - Disable interrupts
1950  * @xdev: DPDMA device
1951  *
1952  * Disable interrupts.
1953  */
1954 static void xilinx_dpdma_disable_intr(struct xilinx_dpdma_device *xdev)
1955 {
1956         dpdma_write(xdev->reg, XILINX_DPDMA_IDS, XILINX_DPDMA_INTR_ERR_ALL);
1957         dpdma_write(xdev->reg, XILINX_DPDMA_EIDS, XILINX_DPDMA_EINTR_ALL);
1958 }
1959
1960 /* Interrupt handling operations*/
1961
1962 /**
1963  * xilinx_dpdma_chan_err_task - Per channel tasklet for error handling
1964  * @data: tasklet data to be casted to DPDMA channel structure
1965  *
1966  * Per channel error handling tasklet. This function waits for the outstanding
1967  * transaction to complete and triggers error handling. After error handling,
1968  * re-enable channel error interrupts, and restart the channel if needed.
1969  */
1970 static void xilinx_dpdma_chan_err_task(unsigned long data)
1971 {
1972         struct xilinx_dpdma_chan *chan = (struct xilinx_dpdma_chan *)data;
1973         struct xilinx_dpdma_device *xdev = chan->xdev;
1974
1975         /* Proceed error handling even when polling fails. */
1976         xilinx_dpdma_chan_poll_no_ostand(chan);
1977
1978         xilinx_dpdma_chan_handle_err(chan);
1979
1980         dpdma_write(xdev->reg, XILINX_DPDMA_IEN,
1981                     XILINX_DPDMA_INTR_CHAN_ERR_MASK << chan->id);
1982         dpdma_write(xdev->reg, XILINX_DPDMA_EIEN,
1983                     XILINX_DPDMA_EINTR_CHAN_ERR_MASK << chan->id);
1984
1985         xilinx_dpdma_chan_start(chan);
1986         xilinx_dpdma_chan_issue_pending(chan);
1987 }
1988
1989 /**
1990  * xilinx_dpdma_chan_done_task - Per channel tasklet for done interrupt handling
1991  * @data: tasklet data to be casted to DPDMA channel structure
1992  *
1993  * Per channel done interrupt handling tasklet.
1994  */
1995 static void xilinx_dpdma_chan_done_task(unsigned long data)
1996 {
1997         struct xilinx_dpdma_chan *chan = (struct xilinx_dpdma_chan *)data;
1998
1999         xilinx_dpdma_chan_cleanup_desc(chan);
2000 }
2001
2002 static irqreturn_t xilinx_dpdma_irq_handler(int irq, void *data)
2003 {
2004         struct xilinx_dpdma_device *xdev = data;
2005         u32 status, error, i;
2006         unsigned long masked;
2007
2008         status = dpdma_read(xdev->reg, XILINX_DPDMA_ISR);
2009         error = dpdma_read(xdev->reg, XILINX_DPDMA_EISR);
2010         if (!status && !error)
2011                 return IRQ_NONE;
2012
2013         dpdma_write(xdev->reg, XILINX_DPDMA_ISR, status);
2014         dpdma_write(xdev->reg, XILINX_DPDMA_EISR, error);
2015
2016         if (status & XILINX_DPDMA_INTR_VSYNC)
2017                 xilinx_dpdma_handle_vsync_intr(xdev);
2018
2019         masked = (status & XILINX_DPDMA_INTR_DESC_DONE_MASK) >>
2020                  XILINX_DPDMA_INTR_DESC_DONE_SHIFT;
2021         if (masked)
2022                 for_each_set_bit(i, &masked, XILINX_DPDMA_NUM_CHAN)
2023                         xilinx_dpdma_chan_desc_done_intr(xdev->chan[i]);
2024
2025         masked = (status & XILINX_DPDMA_INTR_NO_OSTAND_MASK) >>
2026                  XILINX_DPDMA_INTR_NO_OSTAND_SHIFT;
2027         if (masked)
2028                 for_each_set_bit(i, &masked, XILINX_DPDMA_NUM_CHAN)
2029                         xilinx_dpdma_chan_notify_no_ostand(xdev->chan[i]);
2030
2031         masked = status & XILINX_DPDMA_INTR_ERR_ALL;
2032         if (masked || error)
2033                 xilinx_dpdma_handle_err_intr(xdev, masked, error);
2034
2035         return IRQ_HANDLED;
2036 }
2037
2038 /* Initialization operations */
2039
2040 static struct xilinx_dpdma_chan *
2041 xilinx_dpdma_chan_probe(struct device_node *node,
2042                         struct xilinx_dpdma_device *xdev)
2043 {
2044         struct xilinx_dpdma_chan *chan;
2045
2046         chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
2047         if (!chan)
2048                 return ERR_PTR(-ENOMEM);
2049
2050         if (of_device_is_compatible(node, "xlnx,video0")) {
2051                 chan->id = VIDEO0;
2052         } else if (of_device_is_compatible(node, "xlnx,video1")) {
2053                 chan->id = VIDEO1;
2054         } else if (of_device_is_compatible(node, "xlnx,video2")) {
2055                 chan->id = VIDEO2;
2056         } else if (of_device_is_compatible(node, "xlnx,graphics")) {
2057                 chan->id = GRAPHICS;
2058         } else if (of_device_is_compatible(node, "xlnx,audio0")) {
2059                 chan->id = AUDIO0;
2060         } else if (of_device_is_compatible(node, "xlnx,audio1")) {
2061                 chan->id = AUDIO1;
2062         } else {
2063                 dev_err(xdev->dev, "invalid channel compatible string in DT\n");
2064                 return ERR_PTR(-EINVAL);
2065         }
2066
2067         chan->reg = xdev->reg + XILINX_DPDMA_CH_BASE + XILINX_DPDMA_CH_OFFSET *
2068                     chan->id;
2069         chan->status = IDLE;
2070
2071         spin_lock_init(&chan->lock);
2072         INIT_LIST_HEAD(&chan->done_list);
2073         init_waitqueue_head(&chan->wait_to_stop);
2074
2075         tasklet_init(&chan->done_task, xilinx_dpdma_chan_done_task,
2076                      (unsigned long)chan);
2077         tasklet_init(&chan->err_task, xilinx_dpdma_chan_err_task,
2078                      (unsigned long)chan);
2079
2080         chan->common.device = &xdev->common;
2081         chan->xdev = xdev;
2082
2083         list_add_tail(&chan->common.device_node, &xdev->common.channels);
2084         xdev->chan[chan->id] = chan;
2085
2086         return chan;
2087 }
2088
2089 static void xilinx_dpdma_chan_remove(struct xilinx_dpdma_chan *chan)
2090 {
2091         tasklet_kill(&chan->err_task);
2092         tasklet_kill(&chan->done_task);
2093         list_del(&chan->common.device_node);
2094 }
2095
2096 static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
2097                                             struct of_dma *ofdma)
2098 {
2099         struct xilinx_dpdma_device *xdev = ofdma->of_dma_data;
2100         uint32_t chan_id = dma_spec->args[0];
2101
2102         if (chan_id >= XILINX_DPDMA_NUM_CHAN)
2103                 return NULL;
2104
2105         if (!xdev->chan[chan_id])
2106                 return NULL;
2107
2108         return dma_get_slave_channel(&xdev->chan[chan_id]->common);
2109 }
2110
2111 static int xilinx_dpdma_probe(struct platform_device *pdev)
2112 {
2113         struct xilinx_dpdma_device *xdev;
2114         struct xilinx_dpdma_chan *chan;
2115         struct dma_device *ddev;
2116         struct resource *res;
2117         struct device_node *node, *child;
2118         u32 i;
2119         int irq, ret;
2120
2121         xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
2122         if (!xdev)
2123                 return -ENOMEM;
2124
2125         xdev->dev = &pdev->dev;
2126         ddev = &xdev->common;
2127         ddev->dev = &pdev->dev;
2128         node = xdev->dev->of_node;
2129
2130         xdev->axi_clk = devm_clk_get(xdev->dev, "axi_clk");
2131         if (IS_ERR(xdev->axi_clk))
2132                 return PTR_ERR(xdev->axi_clk);
2133
2134         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2135         xdev->reg = devm_ioremap_resource(&pdev->dev, res);
2136         if (IS_ERR(xdev->reg))
2137                 return PTR_ERR(xdev->reg);
2138
2139         irq = platform_get_irq(pdev, 0);
2140         if (irq < 0) {
2141                 dev_err(xdev->dev, "failed to get platform irq\n");
2142                 return irq;
2143         }
2144
2145         ret = devm_request_irq(xdev->dev, irq, xilinx_dpdma_irq_handler,
2146                                IRQF_SHARED, dev_name(xdev->dev), xdev);
2147         if (ret) {
2148                 dev_err(xdev->dev, "failed to request IRQ\n");
2149                 return ret;
2150         }
2151
2152         INIT_LIST_HEAD(&xdev->common.channels);
2153         dma_cap_set(DMA_SLAVE, ddev->cap_mask);
2154         dma_cap_set(DMA_PRIVATE, ddev->cap_mask);
2155         dma_cap_set(DMA_CYCLIC, ddev->cap_mask);
2156         dma_cap_set(DMA_INTERLEAVE, ddev->cap_mask);
2157         ddev->copy_align = fls(XILINX_DPDMA_ALIGN_BYTES - 1);
2158
2159         ddev->device_alloc_chan_resources = xilinx_dpdma_alloc_chan_resources;
2160         ddev->device_free_chan_resources = xilinx_dpdma_free_chan_resources;
2161         ddev->device_prep_slave_sg = xilinx_dpdma_prep_slave_sg;
2162         ddev->device_prep_dma_cyclic = xilinx_dpdma_prep_dma_cyclic;
2163         ddev->device_prep_interleaved_dma = xilinx_dpdma_prep_interleaved_dma;
2164         ddev->device_tx_status = xilinx_dpdma_tx_status;
2165         ddev->device_issue_pending = xilinx_dpdma_issue_pending;
2166         ddev->device_config = xilinx_dpdma_config;
2167         ddev->device_pause = xilinx_dpdma_pause;
2168         ddev->device_resume = xilinx_dpdma_resume;
2169         ddev->device_terminate_all = xilinx_dpdma_terminate_all;
2170         ddev->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED);
2171         ddev->directions = BIT(DMA_MEM_TO_DEV);
2172         ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
2173
2174         for_each_child_of_node(node, child) {
2175                 chan = xilinx_dpdma_chan_probe(child, xdev);
2176                 if (IS_ERR(chan)) {
2177                         dev_err(xdev->dev, "failed to probe a channel\n");
2178                         ret = PTR_ERR(chan);
2179                         goto error;
2180                 }
2181         }
2182
2183         xdev->ext_addr = sizeof(dma_addr_t) > 4;
2184         if (xdev->ext_addr)
2185                 xdev->desc_addr = xilinx_dpdma_sw_desc_addr_64;
2186         else
2187                 xdev->desc_addr = xilinx_dpdma_sw_desc_addr_32;
2188
2189         ret = clk_prepare_enable(xdev->axi_clk);
2190         if (ret) {
2191                 dev_err(xdev->dev, "failed to enable the axi clock\n");
2192                 goto error;
2193         }
2194
2195         ret = dma_async_device_register(ddev);
2196         if (ret) {
2197                 dev_err(xdev->dev, "failed to enable the axi clock\n");
2198                 goto error_dma_async;
2199         }
2200
2201         ret = of_dma_controller_register(xdev->dev->of_node,
2202                                          of_dma_xilinx_xlate, ddev);
2203         if (ret) {
2204                 dev_err(xdev->dev, "failed to register DMA to DT DMA helper\n");
2205                 goto error_of_dma;
2206         }
2207
2208         xilinx_dpdma_enable_intr(xdev);
2209
2210         xilinx_dpdma_debugfs_init(&pdev->dev);
2211
2212         dev_info(&pdev->dev, "Xilinx DPDMA engine is probed\n");
2213
2214         return 0;
2215
2216 error_of_dma:
2217         dma_async_device_unregister(ddev);
2218 error_dma_async:
2219         clk_disable_unprepare(xdev->axi_clk);
2220 error:
2221         for (i = 0; i < XILINX_DPDMA_NUM_CHAN; i++)
2222                 if (xdev->chan[i])
2223                         xilinx_dpdma_chan_remove(xdev->chan[i]);
2224
2225         return ret;
2226 }
2227
2228 static int xilinx_dpdma_remove(struct platform_device *pdev)
2229 {
2230         struct xilinx_dpdma_device *xdev;
2231         unsigned int i;
2232
2233         xdev = platform_get_drvdata(pdev);
2234
2235         xilinx_dpdma_disable_intr(xdev);
2236         of_dma_controller_free(pdev->dev.of_node);
2237         dma_async_device_unregister(&xdev->common);
2238         clk_disable_unprepare(xdev->axi_clk);
2239
2240         for (i = 0; i < XILINX_DPDMA_NUM_CHAN; i++)
2241                 if (xdev->chan[i])
2242                         xilinx_dpdma_chan_remove(xdev->chan[i]);
2243
2244         return 0;
2245 }
2246
2247 static const struct of_device_id xilinx_dpdma_of_match[] = {
2248         { .compatible = "xlnx,dpdma",},
2249         { /* end of table */ },
2250 };
2251 MODULE_DEVICE_TABLE(of, xilinx_dpdma_of_match);
2252
2253 static struct platform_driver xilinx_dpdma_driver = {
2254         .probe                  = xilinx_dpdma_probe,
2255         .remove                 = xilinx_dpdma_remove,
2256         .driver                 = {
2257                 .name           = "xilinx-dpdma",
2258                 .of_match_table = xilinx_dpdma_of_match,
2259         },
2260 };
2261
2262 module_platform_driver(xilinx_dpdma_driver);
2263
2264 MODULE_AUTHOR("Xilinx, Inc.");
2265 MODULE_DESCRIPTION("Xilinx DPDMA driver");
2266 MODULE_LICENSE("GPL v2");