GNU Linux-libre 4.14.262-gnu1
[releases.git] / drivers / dma / xilinx / xilinx_dma.c
1 /*
2  * DMA driver for Xilinx Video DMA Engine
3  *
4  * Copyright (C) 2010-2014 Xilinx, Inc. All rights reserved.
5  *
6  * Based on the Freescale DMA driver.
7  *
8  * Description:
9  * The AXI Video Direct Memory Access (AXI VDMA) core is a soft Xilinx IP
10  * core that provides high-bandwidth direct memory access between memory
11  * and AXI4-Stream type video target peripherals. The core provides efficient
12  * two dimensional DMA operations with independent asynchronous read (S2MM)
13  * and write (MM2S) channel operation. It can be configured to have either
14  * one channel or two channels. If configured as two channels, one is to
15  * transmit to the video device (MM2S) and another is to receive from the
16  * video device (S2MM). Initialization, status, interrupt and management
17  * registers are accessed through an AXI4-Lite slave interface.
18  *
19  * The AXI Direct Memory Access (AXI DMA) core is a soft Xilinx IP core that
20  * provides high-bandwidth one dimensional direct memory access between memory
21  * and AXI4-Stream target peripherals. It supports one receive and one
22  * transmit channel, both of them optional at synthesis time.
23  *
24  * The AXI CDMA, is a soft IP, which provides high-bandwidth Direct Memory
25  * Access (DMA) between a memory-mapped source address and a memory-mapped
26  * destination address.
27  *
28  * This program is free software: you can redistribute it and/or modify
29  * it under the terms of the GNU General Public License as published by
30  * the Free Software Foundation, either version 2 of the License, or
31  * (at your option) any later version.
32  */
33
34 #include <linux/bitops.h>
35 #include <linux/dmapool.h>
36 #include <linux/dma/xilinx_dma.h>
37 #include <linux/init.h>
38 #include <linux/interrupt.h>
39 #include <linux/io.h>
40 #include <linux/iopoll.h>
41 #include <linux/module.h>
42 #include <linux/of_address.h>
43 #include <linux/of_dma.h>
44 #include <linux/of_platform.h>
45 #include <linux/of_irq.h>
46 #include <linux/slab.h>
47 #include <linux/clk.h>
48 #include <linux/io-64-nonatomic-lo-hi.h>
49
50 #include "../dmaengine.h"
51
52 /* Register/Descriptor Offsets */
53 #define XILINX_DMA_MM2S_CTRL_OFFSET             0x0000
54 #define XILINX_DMA_S2MM_CTRL_OFFSET             0x0030
55 #define XILINX_VDMA_MM2S_DESC_OFFSET            0x0050
56 #define XILINX_VDMA_S2MM_DESC_OFFSET            0x00a0
57
58 /* Control Registers */
59 #define XILINX_DMA_REG_DMACR                    0x0000
60 #define XILINX_DMA_DMACR_DELAY_MAX              0xff
61 #define XILINX_DMA_DMACR_DELAY_SHIFT            24
62 #define XILINX_DMA_DMACR_FRAME_COUNT_MAX        0xff
63 #define XILINX_DMA_DMACR_FRAME_COUNT_SHIFT      16
64 #define XILINX_DMA_DMACR_ERR_IRQ                BIT(14)
65 #define XILINX_DMA_DMACR_DLY_CNT_IRQ            BIT(13)
66 #define XILINX_DMA_DMACR_FRM_CNT_IRQ            BIT(12)
67 #define XILINX_DMA_DMACR_MASTER_SHIFT           8
68 #define XILINX_DMA_DMACR_FSYNCSRC_SHIFT 5
69 #define XILINX_DMA_DMACR_FRAMECNT_EN            BIT(4)
70 #define XILINX_DMA_DMACR_GENLOCK_EN             BIT(3)
71 #define XILINX_DMA_DMACR_RESET                  BIT(2)
72 #define XILINX_DMA_DMACR_CIRC_EN                BIT(1)
73 #define XILINX_DMA_DMACR_RUNSTOP                BIT(0)
74 #define XILINX_DMA_DMACR_FSYNCSRC_MASK          GENMASK(6, 5)
75 #define XILINX_DMA_DMACR_DELAY_MASK             GENMASK(31, 24)
76 #define XILINX_DMA_DMACR_FRAME_COUNT_MASK       GENMASK(23, 16)
77 #define XILINX_DMA_DMACR_MASTER_MASK            GENMASK(11, 8)
78
79 #define XILINX_DMA_REG_DMASR                    0x0004
80 #define XILINX_DMA_DMASR_EOL_LATE_ERR           BIT(15)
81 #define XILINX_DMA_DMASR_ERR_IRQ                BIT(14)
82 #define XILINX_DMA_DMASR_DLY_CNT_IRQ            BIT(13)
83 #define XILINX_DMA_DMASR_FRM_CNT_IRQ            BIT(12)
84 #define XILINX_DMA_DMASR_SOF_LATE_ERR           BIT(11)
85 #define XILINX_DMA_DMASR_SG_DEC_ERR             BIT(10)
86 #define XILINX_DMA_DMASR_SG_SLV_ERR             BIT(9)
87 #define XILINX_DMA_DMASR_EOF_EARLY_ERR          BIT(8)
88 #define XILINX_DMA_DMASR_SOF_EARLY_ERR          BIT(7)
89 #define XILINX_DMA_DMASR_DMA_DEC_ERR            BIT(6)
90 #define XILINX_DMA_DMASR_DMA_SLAVE_ERR          BIT(5)
91 #define XILINX_DMA_DMASR_DMA_INT_ERR            BIT(4)
92 #define XILINX_DMA_DMASR_IDLE                   BIT(1)
93 #define XILINX_DMA_DMASR_HALTED         BIT(0)
94 #define XILINX_DMA_DMASR_DELAY_MASK             GENMASK(31, 24)
95 #define XILINX_DMA_DMASR_FRAME_COUNT_MASK       GENMASK(23, 16)
96
97 #define XILINX_DMA_REG_CURDESC                  0x0008
98 #define XILINX_DMA_REG_TAILDESC         0x0010
99 #define XILINX_DMA_REG_REG_INDEX                0x0014
100 #define XILINX_DMA_REG_FRMSTORE         0x0018
101 #define XILINX_DMA_REG_THRESHOLD                0x001c
102 #define XILINX_DMA_REG_FRMPTR_STS               0x0024
103 #define XILINX_DMA_REG_PARK_PTR         0x0028
104 #define XILINX_DMA_PARK_PTR_WR_REF_SHIFT        8
105 #define XILINX_DMA_PARK_PTR_RD_REF_SHIFT        0
106 #define XILINX_DMA_REG_VDMA_VERSION             0x002c
107
108 /* Register Direct Mode Registers */
109 #define XILINX_DMA_REG_VSIZE                    0x0000
110 #define XILINX_DMA_REG_HSIZE                    0x0004
111
112 #define XILINX_DMA_REG_FRMDLY_STRIDE            0x0008
113 #define XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT   24
114 #define XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT   0
115
116 #define XILINX_VDMA_REG_START_ADDRESS(n)        (0x000c + 4 * (n))
117 #define XILINX_VDMA_REG_START_ADDRESS_64(n)     (0x000c + 8 * (n))
118
119 /* HW specific definitions */
120 #define XILINX_DMA_MAX_CHANS_PER_DEVICE 0x20
121
122 #define XILINX_DMA_DMAXR_ALL_IRQ_MASK   \
123                 (XILINX_DMA_DMASR_FRM_CNT_IRQ | \
124                  XILINX_DMA_DMASR_DLY_CNT_IRQ | \
125                  XILINX_DMA_DMASR_ERR_IRQ)
126
127 #define XILINX_DMA_DMASR_ALL_ERR_MASK   \
128                 (XILINX_DMA_DMASR_EOL_LATE_ERR | \
129                  XILINX_DMA_DMASR_SOF_LATE_ERR | \
130                  XILINX_DMA_DMASR_SG_DEC_ERR | \
131                  XILINX_DMA_DMASR_SG_SLV_ERR | \
132                  XILINX_DMA_DMASR_EOF_EARLY_ERR | \
133                  XILINX_DMA_DMASR_SOF_EARLY_ERR | \
134                  XILINX_DMA_DMASR_DMA_DEC_ERR | \
135                  XILINX_DMA_DMASR_DMA_SLAVE_ERR | \
136                  XILINX_DMA_DMASR_DMA_INT_ERR)
137
138 /*
139  * Recoverable errors are DMA Internal error, SOF Early, EOF Early
140  * and SOF Late. They are only recoverable when C_FLUSH_ON_FSYNC
141  * is enabled in the h/w system.
142  */
143 #define XILINX_DMA_DMASR_ERR_RECOVER_MASK       \
144                 (XILINX_DMA_DMASR_SOF_LATE_ERR | \
145                  XILINX_DMA_DMASR_EOF_EARLY_ERR | \
146                  XILINX_DMA_DMASR_SOF_EARLY_ERR | \
147                  XILINX_DMA_DMASR_DMA_INT_ERR)
148
149 /* Axi VDMA Flush on Fsync bits */
150 #define XILINX_DMA_FLUSH_S2MM           3
151 #define XILINX_DMA_FLUSH_MM2S           2
152 #define XILINX_DMA_FLUSH_BOTH           1
153
154 /* Delay loop counter to prevent hardware failure */
155 #define XILINX_DMA_LOOP_COUNT           1000000
156
157 /* AXI DMA Specific Registers/Offsets */
158 #define XILINX_DMA_REG_SRCDSTADDR       0x18
159 #define XILINX_DMA_REG_BTT              0x28
160
161 /* AXI DMA Specific Masks/Bit fields */
162 #define XILINX_DMA_MAX_TRANS_LEN        GENMASK(22, 0)
163 #define XILINX_DMA_CR_COALESCE_MAX      GENMASK(23, 16)
164 #define XILINX_DMA_CR_CYCLIC_BD_EN_MASK BIT(4)
165 #define XILINX_DMA_CR_COALESCE_SHIFT    16
166 #define XILINX_DMA_BD_SOP               BIT(27)
167 #define XILINX_DMA_BD_EOP               BIT(26)
168 #define XILINX_DMA_COALESCE_MAX         255
169 #define XILINX_DMA_NUM_APP_WORDS        5
170
171 /* Multi-Channel DMA Descriptor offsets*/
172 #define XILINX_DMA_MCRX_CDESC(x)        (0x40 + (x-1) * 0x20)
173 #define XILINX_DMA_MCRX_TDESC(x)        (0x48 + (x-1) * 0x20)
174
175 /* Multi-Channel DMA Masks/Shifts */
176 #define XILINX_DMA_BD_HSIZE_MASK        GENMASK(15, 0)
177 #define XILINX_DMA_BD_STRIDE_MASK       GENMASK(15, 0)
178 #define XILINX_DMA_BD_VSIZE_MASK        GENMASK(31, 19)
179 #define XILINX_DMA_BD_TDEST_MASK        GENMASK(4, 0)
180 #define XILINX_DMA_BD_STRIDE_SHIFT      0
181 #define XILINX_DMA_BD_VSIZE_SHIFT       19
182
183 /* AXI CDMA Specific Registers/Offsets */
184 #define XILINX_CDMA_REG_SRCADDR         0x18
185 #define XILINX_CDMA_REG_DSTADDR         0x20
186
187 /* AXI CDMA Specific Masks */
188 #define XILINX_CDMA_CR_SGMODE          BIT(3)
189
190 /**
191  * struct xilinx_vdma_desc_hw - Hardware Descriptor
192  * @next_desc: Next Descriptor Pointer @0x00
193  * @pad1: Reserved @0x04
194  * @buf_addr: Buffer address @0x08
195  * @buf_addr_msb: MSB of Buffer address @0x0C
196  * @vsize: Vertical Size @0x10
197  * @hsize: Horizontal Size @0x14
198  * @stride: Number of bytes between the first
199  *          pixels of each horizontal line @0x18
200  */
201 struct xilinx_vdma_desc_hw {
202         u32 next_desc;
203         u32 pad1;
204         u32 buf_addr;
205         u32 buf_addr_msb;
206         u32 vsize;
207         u32 hsize;
208         u32 stride;
209 } __aligned(64);
210
211 /**
212  * struct xilinx_axidma_desc_hw - Hardware Descriptor for AXI DMA
213  * @next_desc: Next Descriptor Pointer @0x00
214  * @next_desc_msb: MSB of Next Descriptor Pointer @0x04
215  * @buf_addr: Buffer address @0x08
216  * @buf_addr_msb: MSB of Buffer address @0x0C
217  * @pad1: Reserved @0x10
218  * @pad2: Reserved @0x14
219  * @control: Control field @0x18
220  * @status: Status field @0x1C
221  * @app: APP Fields @0x20 - 0x30
222  */
223 struct xilinx_axidma_desc_hw {
224         u32 next_desc;
225         u32 next_desc_msb;
226         u32 buf_addr;
227         u32 buf_addr_msb;
228         u32 mcdma_control;
229         u32 vsize_stride;
230         u32 control;
231         u32 status;
232         u32 app[XILINX_DMA_NUM_APP_WORDS];
233 } __aligned(64);
234
235 /**
236  * struct xilinx_cdma_desc_hw - Hardware Descriptor
237  * @next_desc: Next Descriptor Pointer @0x00
238  * @next_descmsb: Next Descriptor Pointer MSB @0x04
239  * @src_addr: Source address @0x08
240  * @src_addrmsb: Source address MSB @0x0C
241  * @dest_addr: Destination address @0x10
242  * @dest_addrmsb: Destination address MSB @0x14
243  * @control: Control field @0x18
244  * @status: Status field @0x1C
245  */
246 struct xilinx_cdma_desc_hw {
247         u32 next_desc;
248         u32 next_desc_msb;
249         u32 src_addr;
250         u32 src_addr_msb;
251         u32 dest_addr;
252         u32 dest_addr_msb;
253         u32 control;
254         u32 status;
255 } __aligned(64);
256
257 /**
258  * struct xilinx_vdma_tx_segment - Descriptor segment
259  * @hw: Hardware descriptor
260  * @node: Node in the descriptor segments list
261  * @phys: Physical address of segment
262  */
263 struct xilinx_vdma_tx_segment {
264         struct xilinx_vdma_desc_hw hw;
265         struct list_head node;
266         dma_addr_t phys;
267 } __aligned(64);
268
269 /**
270  * struct xilinx_axidma_tx_segment - Descriptor segment
271  * @hw: Hardware descriptor
272  * @node: Node in the descriptor segments list
273  * @phys: Physical address of segment
274  */
275 struct xilinx_axidma_tx_segment {
276         struct xilinx_axidma_desc_hw hw;
277         struct list_head node;
278         dma_addr_t phys;
279 } __aligned(64);
280
281 /**
282  * struct xilinx_cdma_tx_segment - Descriptor segment
283  * @hw: Hardware descriptor
284  * @node: Node in the descriptor segments list
285  * @phys: Physical address of segment
286  */
287 struct xilinx_cdma_tx_segment {
288         struct xilinx_cdma_desc_hw hw;
289         struct list_head node;
290         dma_addr_t phys;
291 } __aligned(64);
292
293 /**
294  * struct xilinx_dma_tx_descriptor - Per Transaction structure
295  * @async_tx: Async transaction descriptor
296  * @segments: TX segments list
297  * @node: Node in the channel descriptors list
298  * @cyclic: Check for cyclic transfers.
299  */
300 struct xilinx_dma_tx_descriptor {
301         struct dma_async_tx_descriptor async_tx;
302         struct list_head segments;
303         struct list_head node;
304         bool cyclic;
305 };
306
307 /**
308  * struct xilinx_dma_chan - Driver specific DMA channel structure
309  * @xdev: Driver specific device structure
310  * @ctrl_offset: Control registers offset
311  * @desc_offset: TX descriptor registers offset
312  * @lock: Descriptor operation lock
313  * @pending_list: Descriptors waiting
314  * @active_list: Descriptors ready to submit
315  * @done_list: Complete descriptors
316  * @common: DMA common channel
317  * @desc_pool: Descriptors pool
318  * @dev: The dma device
319  * @irq: Channel IRQ
320  * @id: Channel ID
321  * @direction: Transfer direction
322  * @num_frms: Number of frames
323  * @has_sg: Support scatter transfers
324  * @cyclic: Check for cyclic transfers.
325  * @genlock: Support genlock mode
326  * @err: Channel has errors
327  * @tasklet: Cleanup work after irq
328  * @config: Device configuration info
329  * @flush_on_fsync: Flush on Frame sync
330  * @desc_pendingcount: Descriptor pending count
331  * @ext_addr: Indicates 64 bit addressing is supported by dma channel
332  * @desc_submitcount: Descriptor h/w submitted count
333  * @residue: Residue for AXI DMA
334  * @seg_v: Statically allocated segments base
335  * @cyclic_seg_v: Statically allocated segment base for cyclic transfers
336  * @start_transfer: Differentiate b/w DMA IP's transfer
337  * @stop_transfer: Differentiate b/w DMA IP's quiesce
338  */
339 struct xilinx_dma_chan {
340         struct xilinx_dma_device *xdev;
341         u32 ctrl_offset;
342         u32 desc_offset;
343         spinlock_t lock;
344         struct list_head pending_list;
345         struct list_head active_list;
346         struct list_head done_list;
347         struct dma_chan common;
348         struct dma_pool *desc_pool;
349         struct device *dev;
350         int irq;
351         int id;
352         enum dma_transfer_direction direction;
353         int num_frms;
354         bool has_sg;
355         bool cyclic;
356         bool genlock;
357         bool err;
358         struct tasklet_struct tasklet;
359         struct xilinx_vdma_config config;
360         bool flush_on_fsync;
361         u32 desc_pendingcount;
362         bool ext_addr;
363         u32 desc_submitcount;
364         u32 residue;
365         struct xilinx_axidma_tx_segment *seg_v;
366         struct xilinx_axidma_tx_segment *cyclic_seg_v;
367         void (*start_transfer)(struct xilinx_dma_chan *chan);
368         int (*stop_transfer)(struct xilinx_dma_chan *chan);
369         u16 tdest;
370 };
371
372 struct xilinx_dma_config {
373         enum xdma_ip_type dmatype;
374         int (*clk_init)(struct platform_device *pdev, struct clk **axi_clk,
375                         struct clk **tx_clk, struct clk **txs_clk,
376                         struct clk **rx_clk, struct clk **rxs_clk);
377 };
378
379 /**
380  * struct xilinx_dma_device - DMA device structure
381  * @regs: I/O mapped base address
382  * @dev: Device Structure
383  * @common: DMA device structure
384  * @chan: Driver specific DMA channel
385  * @has_sg: Specifies whether Scatter-Gather is present or not
386  * @mcdma: Specifies whether Multi-Channel is present or not
387  * @flush_on_fsync: Flush on frame sync
388  * @ext_addr: Indicates 64 bit addressing is supported by dma device
389  * @pdev: Platform device structure pointer
390  * @dma_config: DMA config structure
391  * @axi_clk: DMA Axi4-lite interace clock
392  * @tx_clk: DMA mm2s clock
393  * @txs_clk: DMA mm2s stream clock
394  * @rx_clk: DMA s2mm clock
395  * @rxs_clk: DMA s2mm stream clock
396  * @nr_channels: Number of channels DMA device supports
397  * @chan_id: DMA channel identifier
398  */
399 struct xilinx_dma_device {
400         void __iomem *regs;
401         struct device *dev;
402         struct dma_device common;
403         struct xilinx_dma_chan *chan[XILINX_DMA_MAX_CHANS_PER_DEVICE];
404         bool has_sg;
405         bool mcdma;
406         u32 flush_on_fsync;
407         bool ext_addr;
408         struct platform_device  *pdev;
409         const struct xilinx_dma_config *dma_config;
410         struct clk *axi_clk;
411         struct clk *tx_clk;
412         struct clk *txs_clk;
413         struct clk *rx_clk;
414         struct clk *rxs_clk;
415         u32 nr_channels;
416         u32 chan_id;
417 };
418
419 /* Macros */
420 #define to_xilinx_chan(chan) \
421         container_of(chan, struct xilinx_dma_chan, common)
422 #define to_dma_tx_descriptor(tx) \
423         container_of(tx, struct xilinx_dma_tx_descriptor, async_tx)
424 #define xilinx_dma_poll_timeout(chan, reg, val, cond, delay_us, timeout_us) \
425         readl_poll_timeout_atomic(chan->xdev->regs + chan->ctrl_offset + reg, \
426                                   val, cond, delay_us, timeout_us)
427
428 /* IO accessors */
429 static inline u32 dma_read(struct xilinx_dma_chan *chan, u32 reg)
430 {
431         return ioread32(chan->xdev->regs + reg);
432 }
433
434 static inline void dma_write(struct xilinx_dma_chan *chan, u32 reg, u32 value)
435 {
436         iowrite32(value, chan->xdev->regs + reg);
437 }
438
439 static inline void vdma_desc_write(struct xilinx_dma_chan *chan, u32 reg,
440                                    u32 value)
441 {
442         dma_write(chan, chan->desc_offset + reg, value);
443 }
444
445 static inline u32 dma_ctrl_read(struct xilinx_dma_chan *chan, u32 reg)
446 {
447         return dma_read(chan, chan->ctrl_offset + reg);
448 }
449
450 static inline void dma_ctrl_write(struct xilinx_dma_chan *chan, u32 reg,
451                                    u32 value)
452 {
453         dma_write(chan, chan->ctrl_offset + reg, value);
454 }
455
456 static inline void dma_ctrl_clr(struct xilinx_dma_chan *chan, u32 reg,
457                                  u32 clr)
458 {
459         dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) & ~clr);
460 }
461
462 static inline void dma_ctrl_set(struct xilinx_dma_chan *chan, u32 reg,
463                                  u32 set)
464 {
465         dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) | set);
466 }
467
468 /**
469  * vdma_desc_write_64 - 64-bit descriptor write
470  * @chan: Driver specific VDMA channel
471  * @reg: Register to write
472  * @value_lsb: lower address of the descriptor.
473  * @value_msb: upper address of the descriptor.
474  *
475  * Since vdma driver is trying to write to a register offset which is not a
476  * multiple of 64 bits(ex : 0x5c), we are writing as two separate 32 bits
477  * instead of a single 64 bit register write.
478  */
479 static inline void vdma_desc_write_64(struct xilinx_dma_chan *chan, u32 reg,
480                                       u32 value_lsb, u32 value_msb)
481 {
482         /* Write the lsb 32 bits*/
483         writel(value_lsb, chan->xdev->regs + chan->desc_offset + reg);
484
485         /* Write the msb 32 bits */
486         writel(value_msb, chan->xdev->regs + chan->desc_offset + reg + 4);
487 }
488
489 static inline void dma_writeq(struct xilinx_dma_chan *chan, u32 reg, u64 value)
490 {
491         lo_hi_writeq(value, chan->xdev->regs + chan->ctrl_offset + reg);
492 }
493
494 static inline void xilinx_write(struct xilinx_dma_chan *chan, u32 reg,
495                                 dma_addr_t addr)
496 {
497         if (chan->ext_addr)
498                 dma_writeq(chan, reg, addr);
499         else
500                 dma_ctrl_write(chan, reg, addr);
501 }
502
503 static inline void xilinx_axidma_buf(struct xilinx_dma_chan *chan,
504                                      struct xilinx_axidma_desc_hw *hw,
505                                      dma_addr_t buf_addr, size_t sg_used,
506                                      size_t period_len)
507 {
508         if (chan->ext_addr) {
509                 hw->buf_addr = lower_32_bits(buf_addr + sg_used + period_len);
510                 hw->buf_addr_msb = upper_32_bits(buf_addr + sg_used +
511                                                  period_len);
512         } else {
513                 hw->buf_addr = buf_addr + sg_used + period_len;
514         }
515 }
516
517 /* -----------------------------------------------------------------------------
518  * Descriptors and segments alloc and free
519  */
520
521 /**
522  * xilinx_vdma_alloc_tx_segment - Allocate transaction segment
523  * @chan: Driver specific DMA channel
524  *
525  * Return: The allocated segment on success and NULL on failure.
526  */
527 static struct xilinx_vdma_tx_segment *
528 xilinx_vdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
529 {
530         struct xilinx_vdma_tx_segment *segment;
531         dma_addr_t phys;
532
533         segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
534         if (!segment)
535                 return NULL;
536
537         segment->phys = phys;
538
539         return segment;
540 }
541
542 /**
543  * xilinx_cdma_alloc_tx_segment - Allocate transaction segment
544  * @chan: Driver specific DMA channel
545  *
546  * Return: The allocated segment on success and NULL on failure.
547  */
548 static struct xilinx_cdma_tx_segment *
549 xilinx_cdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
550 {
551         struct xilinx_cdma_tx_segment *segment;
552         dma_addr_t phys;
553
554         segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
555         if (!segment)
556                 return NULL;
557
558         segment->phys = phys;
559
560         return segment;
561 }
562
563 /**
564  * xilinx_axidma_alloc_tx_segment - Allocate transaction segment
565  * @chan: Driver specific DMA channel
566  *
567  * Return: The allocated segment on success and NULL on failure.
568  */
569 static struct xilinx_axidma_tx_segment *
570 xilinx_axidma_alloc_tx_segment(struct xilinx_dma_chan *chan)
571 {
572         struct xilinx_axidma_tx_segment *segment;
573         dma_addr_t phys;
574
575         segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
576         if (!segment)
577                 return NULL;
578
579         segment->phys = phys;
580
581         return segment;
582 }
583
584 /**
585  * xilinx_dma_free_tx_segment - Free transaction segment
586  * @chan: Driver specific DMA channel
587  * @segment: DMA transaction segment
588  */
589 static void xilinx_dma_free_tx_segment(struct xilinx_dma_chan *chan,
590                                 struct xilinx_axidma_tx_segment *segment)
591 {
592         dma_pool_free(chan->desc_pool, segment, segment->phys);
593 }
594
595 /**
596  * xilinx_cdma_free_tx_segment - Free transaction segment
597  * @chan: Driver specific DMA channel
598  * @segment: DMA transaction segment
599  */
600 static void xilinx_cdma_free_tx_segment(struct xilinx_dma_chan *chan,
601                                 struct xilinx_cdma_tx_segment *segment)
602 {
603         dma_pool_free(chan->desc_pool, segment, segment->phys);
604 }
605
606 /**
607  * xilinx_vdma_free_tx_segment - Free transaction segment
608  * @chan: Driver specific DMA channel
609  * @segment: DMA transaction segment
610  */
611 static void xilinx_vdma_free_tx_segment(struct xilinx_dma_chan *chan,
612                                         struct xilinx_vdma_tx_segment *segment)
613 {
614         dma_pool_free(chan->desc_pool, segment, segment->phys);
615 }
616
617 /**
618  * xilinx_dma_tx_descriptor - Allocate transaction descriptor
619  * @chan: Driver specific DMA channel
620  *
621  * Return: The allocated descriptor on success and NULL on failure.
622  */
623 static struct xilinx_dma_tx_descriptor *
624 xilinx_dma_alloc_tx_descriptor(struct xilinx_dma_chan *chan)
625 {
626         struct xilinx_dma_tx_descriptor *desc;
627
628         desc = kzalloc(sizeof(*desc), GFP_KERNEL);
629         if (!desc)
630                 return NULL;
631
632         INIT_LIST_HEAD(&desc->segments);
633
634         return desc;
635 }
636
637 /**
638  * xilinx_dma_free_tx_descriptor - Free transaction descriptor
639  * @chan: Driver specific DMA channel
640  * @desc: DMA transaction descriptor
641  */
642 static void
643 xilinx_dma_free_tx_descriptor(struct xilinx_dma_chan *chan,
644                                struct xilinx_dma_tx_descriptor *desc)
645 {
646         struct xilinx_vdma_tx_segment *segment, *next;
647         struct xilinx_cdma_tx_segment *cdma_segment, *cdma_next;
648         struct xilinx_axidma_tx_segment *axidma_segment, *axidma_next;
649
650         if (!desc)
651                 return;
652
653         if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
654                 list_for_each_entry_safe(segment, next, &desc->segments, node) {
655                         list_del(&segment->node);
656                         xilinx_vdma_free_tx_segment(chan, segment);
657                 }
658         } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
659                 list_for_each_entry_safe(cdma_segment, cdma_next,
660                                          &desc->segments, node) {
661                         list_del(&cdma_segment->node);
662                         xilinx_cdma_free_tx_segment(chan, cdma_segment);
663                 }
664         } else {
665                 list_for_each_entry_safe(axidma_segment, axidma_next,
666                                          &desc->segments, node) {
667                         list_del(&axidma_segment->node);
668                         xilinx_dma_free_tx_segment(chan, axidma_segment);
669                 }
670         }
671
672         kfree(desc);
673 }
674
675 /* Required functions */
676
677 /**
678  * xilinx_dma_free_desc_list - Free descriptors list
679  * @chan: Driver specific DMA channel
680  * @list: List to parse and delete the descriptor
681  */
682 static void xilinx_dma_free_desc_list(struct xilinx_dma_chan *chan,
683                                         struct list_head *list)
684 {
685         struct xilinx_dma_tx_descriptor *desc, *next;
686
687         list_for_each_entry_safe(desc, next, list, node) {
688                 list_del(&desc->node);
689                 xilinx_dma_free_tx_descriptor(chan, desc);
690         }
691 }
692
693 /**
694  * xilinx_dma_free_descriptors - Free channel descriptors
695  * @chan: Driver specific DMA channel
696  */
697 static void xilinx_dma_free_descriptors(struct xilinx_dma_chan *chan)
698 {
699         unsigned long flags;
700
701         spin_lock_irqsave(&chan->lock, flags);
702
703         xilinx_dma_free_desc_list(chan, &chan->pending_list);
704         xilinx_dma_free_desc_list(chan, &chan->done_list);
705         xilinx_dma_free_desc_list(chan, &chan->active_list);
706
707         spin_unlock_irqrestore(&chan->lock, flags);
708 }
709
710 /**
711  * xilinx_dma_free_chan_resources - Free channel resources
712  * @dchan: DMA channel
713  */
714 static void xilinx_dma_free_chan_resources(struct dma_chan *dchan)
715 {
716         struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
717
718         dev_dbg(chan->dev, "Free all channel resources.\n");
719
720         xilinx_dma_free_descriptors(chan);
721         if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
722                 xilinx_dma_free_tx_segment(chan, chan->cyclic_seg_v);
723                 xilinx_dma_free_tx_segment(chan, chan->seg_v);
724         }
725         dma_pool_destroy(chan->desc_pool);
726         chan->desc_pool = NULL;
727 }
728
729 /**
730  * xilinx_dma_chan_handle_cyclic - Cyclic dma callback
731  * @chan: Driver specific dma channel
732  * @desc: dma transaction descriptor
733  * @flags: flags for spin lock
734  */
735 static void xilinx_dma_chan_handle_cyclic(struct xilinx_dma_chan *chan,
736                                           struct xilinx_dma_tx_descriptor *desc,
737                                           unsigned long *flags)
738 {
739         dma_async_tx_callback callback;
740         void *callback_param;
741
742         callback = desc->async_tx.callback;
743         callback_param = desc->async_tx.callback_param;
744         if (callback) {
745                 spin_unlock_irqrestore(&chan->lock, *flags);
746                 callback(callback_param);
747                 spin_lock_irqsave(&chan->lock, *flags);
748         }
749 }
750
751 /**
752  * xilinx_dma_chan_desc_cleanup - Clean channel descriptors
753  * @chan: Driver specific DMA channel
754  */
755 static void xilinx_dma_chan_desc_cleanup(struct xilinx_dma_chan *chan)
756 {
757         struct xilinx_dma_tx_descriptor *desc, *next;
758         unsigned long flags;
759
760         spin_lock_irqsave(&chan->lock, flags);
761
762         list_for_each_entry_safe(desc, next, &chan->done_list, node) {
763                 struct dmaengine_desc_callback cb;
764
765                 if (desc->cyclic) {
766                         xilinx_dma_chan_handle_cyclic(chan, desc, &flags);
767                         break;
768                 }
769
770                 /* Remove from the list of running transactions */
771                 list_del(&desc->node);
772
773                 /* Run the link descriptor callback function */
774                 dmaengine_desc_get_callback(&desc->async_tx, &cb);
775                 if (dmaengine_desc_callback_valid(&cb)) {
776                         spin_unlock_irqrestore(&chan->lock, flags);
777                         dmaengine_desc_callback_invoke(&cb, NULL);
778                         spin_lock_irqsave(&chan->lock, flags);
779                 }
780
781                 /* Run any dependencies, then free the descriptor */
782                 dma_run_dependencies(&desc->async_tx);
783                 xilinx_dma_free_tx_descriptor(chan, desc);
784         }
785
786         spin_unlock_irqrestore(&chan->lock, flags);
787 }
788
789 /**
790  * xilinx_dma_do_tasklet - Schedule completion tasklet
791  * @data: Pointer to the Xilinx DMA channel structure
792  */
793 static void xilinx_dma_do_tasklet(unsigned long data)
794 {
795         struct xilinx_dma_chan *chan = (struct xilinx_dma_chan *)data;
796
797         xilinx_dma_chan_desc_cleanup(chan);
798 }
799
800 /**
801  * xilinx_dma_alloc_chan_resources - Allocate channel resources
802  * @dchan: DMA channel
803  *
804  * Return: '0' on success and failure value on error
805  */
806 static int xilinx_dma_alloc_chan_resources(struct dma_chan *dchan)
807 {
808         struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
809
810         /* Has this channel already been allocated? */
811         if (chan->desc_pool)
812                 return 0;
813
814         /*
815          * We need the descriptor to be aligned to 64bytes
816          * for meeting Xilinx VDMA specification requirement.
817          */
818         if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
819                 chan->desc_pool = dma_pool_create("xilinx_dma_desc_pool",
820                                    chan->dev,
821                                    sizeof(struct xilinx_axidma_tx_segment),
822                                    __alignof__(struct xilinx_axidma_tx_segment),
823                                    0);
824         } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
825                 chan->desc_pool = dma_pool_create("xilinx_cdma_desc_pool",
826                                    chan->dev,
827                                    sizeof(struct xilinx_cdma_tx_segment),
828                                    __alignof__(struct xilinx_cdma_tx_segment),
829                                    0);
830         } else {
831                 chan->desc_pool = dma_pool_create("xilinx_vdma_desc_pool",
832                                      chan->dev,
833                                      sizeof(struct xilinx_vdma_tx_segment),
834                                      __alignof__(struct xilinx_vdma_tx_segment),
835                                      0);
836         }
837
838         if (!chan->desc_pool) {
839                 dev_err(chan->dev,
840                         "unable to allocate channel %d descriptor pool\n",
841                         chan->id);
842                 return -ENOMEM;
843         }
844
845         if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
846                 /*
847                  * For AXI DMA case after submitting a pending_list, keep
848                  * an extra segment allocated so that the "next descriptor"
849                  * pointer on the tail descriptor always points to a
850                  * valid descriptor, even when paused after reaching taildesc.
851                  * This way, it is possible to issue additional
852                  * transfers without halting and restarting the channel.
853                  */
854                 chan->seg_v = xilinx_axidma_alloc_tx_segment(chan);
855
856                 /*
857                  * For cyclic DMA mode we need to program the tail Descriptor
858                  * register with a value which is not a part of the BD chain
859                  * so allocating a desc segment during channel allocation for
860                  * programming tail descriptor.
861                  */
862                 chan->cyclic_seg_v = xilinx_axidma_alloc_tx_segment(chan);
863         }
864
865         dma_cookie_init(dchan);
866
867         if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
868                 /* For AXI DMA resetting once channel will reset the
869                  * other channel as well so enable the interrupts here.
870                  */
871                 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
872                               XILINX_DMA_DMAXR_ALL_IRQ_MASK);
873         }
874
875         if ((chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) && chan->has_sg)
876                 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
877                              XILINX_CDMA_CR_SGMODE);
878
879         return 0;
880 }
881
882 /**
883  * xilinx_dma_tx_status - Get DMA transaction status
884  * @dchan: DMA channel
885  * @cookie: Transaction identifier
886  * @txstate: Transaction state
887  *
888  * Return: DMA transaction status
889  */
890 static enum dma_status xilinx_dma_tx_status(struct dma_chan *dchan,
891                                         dma_cookie_t cookie,
892                                         struct dma_tx_state *txstate)
893 {
894         struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
895         struct xilinx_dma_tx_descriptor *desc;
896         struct xilinx_axidma_tx_segment *segment;
897         struct xilinx_axidma_desc_hw *hw;
898         enum dma_status ret;
899         unsigned long flags;
900         u32 residue = 0;
901
902         ret = dma_cookie_status(dchan, cookie, txstate);
903         if (ret == DMA_COMPLETE || !txstate)
904                 return ret;
905
906         if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
907                 spin_lock_irqsave(&chan->lock, flags);
908
909                 desc = list_last_entry(&chan->active_list,
910                                        struct xilinx_dma_tx_descriptor, node);
911                 if (chan->has_sg) {
912                         list_for_each_entry(segment, &desc->segments, node) {
913                                 hw = &segment->hw;
914                                 residue += (hw->control - hw->status) &
915                                            XILINX_DMA_MAX_TRANS_LEN;
916                         }
917                 }
918                 spin_unlock_irqrestore(&chan->lock, flags);
919
920                 chan->residue = residue;
921                 dma_set_residue(txstate, chan->residue);
922         }
923
924         return ret;
925 }
926
927 /**
928  * xilinx_dma_is_running - Check if DMA channel is running
929  * @chan: Driver specific DMA channel
930  *
931  * Return: '1' if running, '0' if not.
932  */
933 static bool xilinx_dma_is_running(struct xilinx_dma_chan *chan)
934 {
935         return !(dma_ctrl_read(chan, XILINX_DMA_REG_DMASR) &
936                  XILINX_DMA_DMASR_HALTED) &&
937                 (dma_ctrl_read(chan, XILINX_DMA_REG_DMACR) &
938                  XILINX_DMA_DMACR_RUNSTOP);
939 }
940
941 /**
942  * xilinx_dma_is_idle - Check if DMA channel is idle
943  * @chan: Driver specific DMA channel
944  *
945  * Return: '1' if idle, '0' if not.
946  */
947 static bool xilinx_dma_is_idle(struct xilinx_dma_chan *chan)
948 {
949         return dma_ctrl_read(chan, XILINX_DMA_REG_DMASR) &
950                 XILINX_DMA_DMASR_IDLE;
951 }
952
953 /**
954  * xilinx_dma_stop_transfer - Halt DMA channel
955  * @chan: Driver specific DMA channel
956  */
957 static int xilinx_dma_stop_transfer(struct xilinx_dma_chan *chan)
958 {
959         u32 val;
960
961         dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
962
963         /* Wait for the hardware to halt */
964         return xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
965                                        val & XILINX_DMA_DMASR_HALTED, 0,
966                                        XILINX_DMA_LOOP_COUNT);
967 }
968
969 /**
970  * xilinx_cdma_stop_transfer - Wait for the current transfer to complete
971  * @chan: Driver specific DMA channel
972  */
973 static int xilinx_cdma_stop_transfer(struct xilinx_dma_chan *chan)
974 {
975         u32 val;
976
977         return xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
978                                        val & XILINX_DMA_DMASR_IDLE, 0,
979                                        XILINX_DMA_LOOP_COUNT);
980 }
981
982 /**
983  * xilinx_dma_start - Start DMA channel
984  * @chan: Driver specific DMA channel
985  */
986 static void xilinx_dma_start(struct xilinx_dma_chan *chan)
987 {
988         int err;
989         u32 val;
990
991         dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
992
993         /* Wait for the hardware to start */
994         err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
995                                       !(val & XILINX_DMA_DMASR_HALTED), 0,
996                                       XILINX_DMA_LOOP_COUNT);
997
998         if (err) {
999                 dev_err(chan->dev, "Cannot start channel %p: %x\n",
1000                         chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
1001
1002                 chan->err = true;
1003         }
1004 }
1005
1006 /**
1007  * xilinx_vdma_start_transfer - Starts VDMA transfer
1008  * @chan: Driver specific channel struct pointer
1009  */
1010 static void xilinx_vdma_start_transfer(struct xilinx_dma_chan *chan)
1011 {
1012         struct xilinx_vdma_config *config = &chan->config;
1013         struct xilinx_dma_tx_descriptor *desc, *tail_desc;
1014         u32 reg;
1015         struct xilinx_vdma_tx_segment *tail_segment;
1016
1017         /* This function was invoked with lock held */
1018         if (chan->err)
1019                 return;
1020
1021         if (list_empty(&chan->pending_list))
1022                 return;
1023
1024         desc = list_first_entry(&chan->pending_list,
1025                                 struct xilinx_dma_tx_descriptor, node);
1026         tail_desc = list_last_entry(&chan->pending_list,
1027                                     struct xilinx_dma_tx_descriptor, node);
1028
1029         tail_segment = list_last_entry(&tail_desc->segments,
1030                                        struct xilinx_vdma_tx_segment, node);
1031
1032         /* If it is SG mode and hardware is busy, cannot submit */
1033         if (chan->has_sg && xilinx_dma_is_running(chan) &&
1034             !xilinx_dma_is_idle(chan)) {
1035                 dev_dbg(chan->dev, "DMA controller still busy\n");
1036                 return;
1037         }
1038
1039         /*
1040          * If hardware is idle, then all descriptors on the running lists are
1041          * done, start new transfers
1042          */
1043         if (chan->has_sg)
1044                 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
1045                                 desc->async_tx.phys);
1046
1047         /* Configure the hardware using info in the config structure */
1048         reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1049
1050         if (config->frm_cnt_en)
1051                 reg |= XILINX_DMA_DMACR_FRAMECNT_EN;
1052         else
1053                 reg &= ~XILINX_DMA_DMACR_FRAMECNT_EN;
1054
1055         /* Configure channel to allow number frame buffers */
1056         dma_ctrl_write(chan, XILINX_DMA_REG_FRMSTORE,
1057                         chan->desc_pendingcount);
1058
1059         /*
1060          * With SG, start with circular mode, so that BDs can be fetched.
1061          * In direct register mode, if not parking, enable circular mode
1062          */
1063         if (chan->has_sg || !config->park)
1064                 reg |= XILINX_DMA_DMACR_CIRC_EN;
1065
1066         if (config->park)
1067                 reg &= ~XILINX_DMA_DMACR_CIRC_EN;
1068
1069         dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1070
1071         if (config->park && (config->park_frm >= 0) &&
1072                         (config->park_frm < chan->num_frms)) {
1073                 if (chan->direction == DMA_MEM_TO_DEV)
1074                         dma_write(chan, XILINX_DMA_REG_PARK_PTR,
1075                                 config->park_frm <<
1076                                         XILINX_DMA_PARK_PTR_RD_REF_SHIFT);
1077                 else
1078                         dma_write(chan, XILINX_DMA_REG_PARK_PTR,
1079                                 config->park_frm <<
1080                                         XILINX_DMA_PARK_PTR_WR_REF_SHIFT);
1081         }
1082
1083         /* Start the hardware */
1084         xilinx_dma_start(chan);
1085
1086         if (chan->err)
1087                 return;
1088
1089         /* Start the transfer */
1090         if (chan->has_sg) {
1091                 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
1092                                 tail_segment->phys);
1093         } else {
1094                 struct xilinx_vdma_tx_segment *segment, *last = NULL;
1095                 int i = 0;
1096
1097                 if (chan->desc_submitcount < chan->num_frms)
1098                         i = chan->desc_submitcount;
1099
1100                 list_for_each_entry(segment, &desc->segments, node) {
1101                         if (chan->ext_addr)
1102                                 vdma_desc_write_64(chan,
1103                                         XILINX_VDMA_REG_START_ADDRESS_64(i++),
1104                                         segment->hw.buf_addr,
1105                                         segment->hw.buf_addr_msb);
1106                         else
1107                                 vdma_desc_write(chan,
1108                                         XILINX_VDMA_REG_START_ADDRESS(i++),
1109                                         segment->hw.buf_addr);
1110
1111                         last = segment;
1112                 }
1113
1114                 if (!last)
1115                         return;
1116
1117                 /* HW expects these parameters to be same for one transaction */
1118                 vdma_desc_write(chan, XILINX_DMA_REG_HSIZE, last->hw.hsize);
1119                 vdma_desc_write(chan, XILINX_DMA_REG_FRMDLY_STRIDE,
1120                                 last->hw.stride);
1121                 vdma_desc_write(chan, XILINX_DMA_REG_VSIZE, last->hw.vsize);
1122         }
1123
1124         if (!chan->has_sg) {
1125                 list_del(&desc->node);
1126                 list_add_tail(&desc->node, &chan->active_list);
1127                 chan->desc_submitcount++;
1128                 chan->desc_pendingcount--;
1129                 if (chan->desc_submitcount == chan->num_frms)
1130                         chan->desc_submitcount = 0;
1131         } else {
1132                 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1133                 chan->desc_pendingcount = 0;
1134         }
1135 }
1136
1137 /**
1138  * xilinx_cdma_start_transfer - Starts cdma transfer
1139  * @chan: Driver specific channel struct pointer
1140  */
1141 static void xilinx_cdma_start_transfer(struct xilinx_dma_chan *chan)
1142 {
1143         struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1144         struct xilinx_cdma_tx_segment *tail_segment;
1145         u32 ctrl_reg = dma_read(chan, XILINX_DMA_REG_DMACR);
1146
1147         if (chan->err)
1148                 return;
1149
1150         if (list_empty(&chan->pending_list))
1151                 return;
1152
1153         head_desc = list_first_entry(&chan->pending_list,
1154                                      struct xilinx_dma_tx_descriptor, node);
1155         tail_desc = list_last_entry(&chan->pending_list,
1156                                     struct xilinx_dma_tx_descriptor, node);
1157         tail_segment = list_last_entry(&tail_desc->segments,
1158                                        struct xilinx_cdma_tx_segment, node);
1159
1160         if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1161                 ctrl_reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1162                 ctrl_reg |= chan->desc_pendingcount <<
1163                                 XILINX_DMA_CR_COALESCE_SHIFT;
1164                 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, ctrl_reg);
1165         }
1166
1167         if (chan->has_sg) {
1168                 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1169                              head_desc->async_tx.phys);
1170
1171                 /* Update tail ptr register which will start the transfer */
1172                 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1173                              tail_segment->phys);
1174         } else {
1175                 /* In simple mode */
1176                 struct xilinx_cdma_tx_segment *segment;
1177                 struct xilinx_cdma_desc_hw *hw;
1178
1179                 segment = list_first_entry(&head_desc->segments,
1180                                            struct xilinx_cdma_tx_segment,
1181                                            node);
1182
1183                 hw = &segment->hw;
1184
1185                 xilinx_write(chan, XILINX_CDMA_REG_SRCADDR, hw->src_addr);
1186                 xilinx_write(chan, XILINX_CDMA_REG_DSTADDR, hw->dest_addr);
1187
1188                 /* Start the transfer */
1189                 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1190                                 hw->control & XILINX_DMA_MAX_TRANS_LEN);
1191         }
1192
1193         list_splice_tail_init(&chan->pending_list, &chan->active_list);
1194         chan->desc_pendingcount = 0;
1195 }
1196
1197 /**
1198  * xilinx_dma_start_transfer - Starts DMA transfer
1199  * @chan: Driver specific channel struct pointer
1200  */
1201 static void xilinx_dma_start_transfer(struct xilinx_dma_chan *chan)
1202 {
1203         struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1204         struct xilinx_axidma_tx_segment *tail_segment, *old_head, *new_head;
1205         u32 reg;
1206
1207         if (chan->err)
1208                 return;
1209
1210         if (list_empty(&chan->pending_list))
1211                 return;
1212
1213         /* If it is SG mode and hardware is busy, cannot submit */
1214         if (chan->has_sg && xilinx_dma_is_running(chan) &&
1215             !xilinx_dma_is_idle(chan)) {
1216                 dev_dbg(chan->dev, "DMA controller still busy\n");
1217                 return;
1218         }
1219
1220         head_desc = list_first_entry(&chan->pending_list,
1221                                      struct xilinx_dma_tx_descriptor, node);
1222         tail_desc = list_last_entry(&chan->pending_list,
1223                                     struct xilinx_dma_tx_descriptor, node);
1224         tail_segment = list_last_entry(&tail_desc->segments,
1225                                        struct xilinx_axidma_tx_segment, node);
1226
1227         if (chan->has_sg && !chan->xdev->mcdma) {
1228                 old_head = list_first_entry(&head_desc->segments,
1229                                         struct xilinx_axidma_tx_segment, node);
1230                 new_head = chan->seg_v;
1231                 /* Copy Buffer Descriptor fields. */
1232                 new_head->hw = old_head->hw;
1233
1234                 /* Swap and save new reserve */
1235                 list_replace_init(&old_head->node, &new_head->node);
1236                 chan->seg_v = old_head;
1237
1238                 tail_segment->hw.next_desc = chan->seg_v->phys;
1239                 head_desc->async_tx.phys = new_head->phys;
1240         }
1241
1242         reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1243
1244         if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1245                 reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1246                 reg |= chan->desc_pendingcount <<
1247                                   XILINX_DMA_CR_COALESCE_SHIFT;
1248                 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1249         }
1250
1251         if (chan->has_sg && !chan->xdev->mcdma)
1252                 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1253                              head_desc->async_tx.phys);
1254
1255         if (chan->has_sg && chan->xdev->mcdma) {
1256                 if (chan->direction == DMA_MEM_TO_DEV) {
1257                         dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
1258                                        head_desc->async_tx.phys);
1259                 } else {
1260                         if (!chan->tdest) {
1261                                 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
1262                                        head_desc->async_tx.phys);
1263                         } else {
1264                                 dma_ctrl_write(chan,
1265                                         XILINX_DMA_MCRX_CDESC(chan->tdest),
1266                                        head_desc->async_tx.phys);
1267                         }
1268                 }
1269         }
1270
1271         xilinx_dma_start(chan);
1272
1273         if (chan->err)
1274                 return;
1275
1276         /* Start the transfer */
1277         if (chan->has_sg && !chan->xdev->mcdma) {
1278                 if (chan->cyclic)
1279                         xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1280                                      chan->cyclic_seg_v->phys);
1281                 else
1282                         xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1283                                      tail_segment->phys);
1284         } else if (chan->has_sg && chan->xdev->mcdma) {
1285                 if (chan->direction == DMA_MEM_TO_DEV) {
1286                         dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
1287                                tail_segment->phys);
1288                 } else {
1289                         if (!chan->tdest) {
1290                                 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
1291                                                tail_segment->phys);
1292                         } else {
1293                                 dma_ctrl_write(chan,
1294                                         XILINX_DMA_MCRX_TDESC(chan->tdest),
1295                                         tail_segment->phys);
1296                         }
1297                 }
1298         } else {
1299                 struct xilinx_axidma_tx_segment *segment;
1300                 struct xilinx_axidma_desc_hw *hw;
1301
1302                 segment = list_first_entry(&head_desc->segments,
1303                                            struct xilinx_axidma_tx_segment,
1304                                            node);
1305                 hw = &segment->hw;
1306
1307                 xilinx_write(chan, XILINX_DMA_REG_SRCDSTADDR, hw->buf_addr);
1308
1309                 /* Start the transfer */
1310                 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1311                                hw->control & XILINX_DMA_MAX_TRANS_LEN);
1312         }
1313
1314         list_splice_tail_init(&chan->pending_list, &chan->active_list);
1315         chan->desc_pendingcount = 0;
1316 }
1317
1318 /**
1319  * xilinx_dma_issue_pending - Issue pending transactions
1320  * @dchan: DMA channel
1321  */
1322 static void xilinx_dma_issue_pending(struct dma_chan *dchan)
1323 {
1324         struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1325         unsigned long flags;
1326
1327         spin_lock_irqsave(&chan->lock, flags);
1328         chan->start_transfer(chan);
1329         spin_unlock_irqrestore(&chan->lock, flags);
1330 }
1331
1332 /**
1333  * xilinx_dma_complete_descriptor - Mark the active descriptor as complete
1334  * @chan : xilinx DMA channel
1335  *
1336  * CONTEXT: hardirq
1337  */
1338 static void xilinx_dma_complete_descriptor(struct xilinx_dma_chan *chan)
1339 {
1340         struct xilinx_dma_tx_descriptor *desc, *next;
1341
1342         /* This function was invoked with lock held */
1343         if (list_empty(&chan->active_list))
1344                 return;
1345
1346         list_for_each_entry_safe(desc, next, &chan->active_list, node) {
1347                 list_del(&desc->node);
1348                 if (!desc->cyclic)
1349                         dma_cookie_complete(&desc->async_tx);
1350                 list_add_tail(&desc->node, &chan->done_list);
1351         }
1352 }
1353
1354 /**
1355  * xilinx_dma_reset - Reset DMA channel
1356  * @chan: Driver specific DMA channel
1357  *
1358  * Return: '0' on success and failure value on error
1359  */
1360 static int xilinx_dma_reset(struct xilinx_dma_chan *chan)
1361 {
1362         int err;
1363         u32 tmp;
1364
1365         dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RESET);
1366
1367         /* Wait for the hardware to finish reset */
1368         err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMACR, tmp,
1369                                       !(tmp & XILINX_DMA_DMACR_RESET), 0,
1370                                       XILINX_DMA_LOOP_COUNT);
1371
1372         if (err) {
1373                 dev_err(chan->dev, "reset timeout, cr %x, sr %x\n",
1374                         dma_ctrl_read(chan, XILINX_DMA_REG_DMACR),
1375                         dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
1376                 return -ETIMEDOUT;
1377         }
1378
1379         chan->err = false;
1380
1381         return err;
1382 }
1383
1384 /**
1385  * xilinx_dma_chan_reset - Reset DMA channel and enable interrupts
1386  * @chan: Driver specific DMA channel
1387  *
1388  * Return: '0' on success and failure value on error
1389  */
1390 static int xilinx_dma_chan_reset(struct xilinx_dma_chan *chan)
1391 {
1392         int err;
1393
1394         /* Reset VDMA */
1395         err = xilinx_dma_reset(chan);
1396         if (err)
1397                 return err;
1398
1399         /* Enable interrupts */
1400         dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
1401                       XILINX_DMA_DMAXR_ALL_IRQ_MASK);
1402
1403         return 0;
1404 }
1405
1406 /**
1407  * xilinx_dma_irq_handler - DMA Interrupt handler
1408  * @irq: IRQ number
1409  * @data: Pointer to the Xilinx DMA channel structure
1410  *
1411  * Return: IRQ_HANDLED/IRQ_NONE
1412  */
1413 static irqreturn_t xilinx_dma_irq_handler(int irq, void *data)
1414 {
1415         struct xilinx_dma_chan *chan = data;
1416         u32 status;
1417
1418         /* Read the status and ack the interrupts. */
1419         status = dma_ctrl_read(chan, XILINX_DMA_REG_DMASR);
1420         if (!(status & XILINX_DMA_DMAXR_ALL_IRQ_MASK))
1421                 return IRQ_NONE;
1422
1423         dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1424                         status & XILINX_DMA_DMAXR_ALL_IRQ_MASK);
1425
1426         if (status & XILINX_DMA_DMASR_ERR_IRQ) {
1427                 /*
1428                  * An error occurred. If C_FLUSH_ON_FSYNC is enabled and the
1429                  * error is recoverable, ignore it. Otherwise flag the error.
1430                  *
1431                  * Only recoverable errors can be cleared in the DMASR register,
1432                  * make sure not to write to other error bits to 1.
1433                  */
1434                 u32 errors = status & XILINX_DMA_DMASR_ALL_ERR_MASK;
1435
1436                 dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1437                                 errors & XILINX_DMA_DMASR_ERR_RECOVER_MASK);
1438
1439                 if (!chan->flush_on_fsync ||
1440                     (errors & ~XILINX_DMA_DMASR_ERR_RECOVER_MASK)) {
1441                         dev_err(chan->dev,
1442                                 "Channel %p has errors %x, cdr %x tdr %x\n",
1443                                 chan, errors,
1444                                 dma_ctrl_read(chan, XILINX_DMA_REG_CURDESC),
1445                                 dma_ctrl_read(chan, XILINX_DMA_REG_TAILDESC));
1446                         chan->err = true;
1447                 }
1448         }
1449
1450         if (status & XILINX_DMA_DMASR_DLY_CNT_IRQ) {
1451                 /*
1452                  * Device takes too long to do the transfer when user requires
1453                  * responsiveness.
1454                  */
1455                 dev_dbg(chan->dev, "Inter-packet latency too long\n");
1456         }
1457
1458         if (status & XILINX_DMA_DMASR_FRM_CNT_IRQ) {
1459                 spin_lock(&chan->lock);
1460                 xilinx_dma_complete_descriptor(chan);
1461                 chan->start_transfer(chan);
1462                 spin_unlock(&chan->lock);
1463         }
1464
1465         tasklet_schedule(&chan->tasklet);
1466         return IRQ_HANDLED;
1467 }
1468
1469 /**
1470  * append_desc_queue - Queuing descriptor
1471  * @chan: Driver specific dma channel
1472  * @desc: dma transaction descriptor
1473  */
1474 static void append_desc_queue(struct xilinx_dma_chan *chan,
1475                               struct xilinx_dma_tx_descriptor *desc)
1476 {
1477         struct xilinx_vdma_tx_segment *tail_segment;
1478         struct xilinx_dma_tx_descriptor *tail_desc;
1479         struct xilinx_axidma_tx_segment *axidma_tail_segment;
1480         struct xilinx_cdma_tx_segment *cdma_tail_segment;
1481
1482         if (list_empty(&chan->pending_list))
1483                 goto append;
1484
1485         /*
1486          * Add the hardware descriptor to the chain of hardware descriptors
1487          * that already exists in memory.
1488          */
1489         tail_desc = list_last_entry(&chan->pending_list,
1490                                     struct xilinx_dma_tx_descriptor, node);
1491         if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
1492                 tail_segment = list_last_entry(&tail_desc->segments,
1493                                                struct xilinx_vdma_tx_segment,
1494                                                node);
1495                 tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1496         } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
1497                 cdma_tail_segment = list_last_entry(&tail_desc->segments,
1498                                                 struct xilinx_cdma_tx_segment,
1499                                                 node);
1500                 cdma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1501         } else {
1502                 axidma_tail_segment = list_last_entry(&tail_desc->segments,
1503                                                struct xilinx_axidma_tx_segment,
1504                                                node);
1505                 axidma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1506         }
1507
1508         /*
1509          * Add the software descriptor and all children to the list
1510          * of pending transactions
1511          */
1512 append:
1513         list_add_tail(&desc->node, &chan->pending_list);
1514         chan->desc_pendingcount++;
1515
1516         if (chan->has_sg && (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA)
1517             && unlikely(chan->desc_pendingcount > chan->num_frms)) {
1518                 dev_dbg(chan->dev, "desc pendingcount is too high\n");
1519                 chan->desc_pendingcount = chan->num_frms;
1520         }
1521 }
1522
1523 /**
1524  * xilinx_dma_tx_submit - Submit DMA transaction
1525  * @tx: Async transaction descriptor
1526  *
1527  * Return: cookie value on success and failure value on error
1528  */
1529 static dma_cookie_t xilinx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
1530 {
1531         struct xilinx_dma_tx_descriptor *desc = to_dma_tx_descriptor(tx);
1532         struct xilinx_dma_chan *chan = to_xilinx_chan(tx->chan);
1533         dma_cookie_t cookie;
1534         unsigned long flags;
1535         int err;
1536
1537         if (chan->cyclic) {
1538                 xilinx_dma_free_tx_descriptor(chan, desc);
1539                 return -EBUSY;
1540         }
1541
1542         if (chan->err) {
1543                 /*
1544                  * If reset fails, need to hard reset the system.
1545                  * Channel is no longer functional
1546                  */
1547                 err = xilinx_dma_chan_reset(chan);
1548                 if (err < 0)
1549                         return err;
1550         }
1551
1552         spin_lock_irqsave(&chan->lock, flags);
1553
1554         cookie = dma_cookie_assign(tx);
1555
1556         /* Put this transaction onto the tail of the pending queue */
1557         append_desc_queue(chan, desc);
1558
1559         if (desc->cyclic)
1560                 chan->cyclic = true;
1561
1562         spin_unlock_irqrestore(&chan->lock, flags);
1563
1564         return cookie;
1565 }
1566
1567 /**
1568  * xilinx_vdma_dma_prep_interleaved - prepare a descriptor for a
1569  *      DMA_SLAVE transaction
1570  * @dchan: DMA channel
1571  * @xt: Interleaved template pointer
1572  * @flags: transfer ack flags
1573  *
1574  * Return: Async transaction descriptor on success and NULL on failure
1575  */
1576 static struct dma_async_tx_descriptor *
1577 xilinx_vdma_dma_prep_interleaved(struct dma_chan *dchan,
1578                                  struct dma_interleaved_template *xt,
1579                                  unsigned long flags)
1580 {
1581         struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1582         struct xilinx_dma_tx_descriptor *desc;
1583         struct xilinx_vdma_tx_segment *segment, *prev = NULL;
1584         struct xilinx_vdma_desc_hw *hw;
1585
1586         if (!is_slave_direction(xt->dir))
1587                 return NULL;
1588
1589         if (!xt->numf || !xt->sgl[0].size)
1590                 return NULL;
1591
1592         if (xt->frame_size != 1)
1593                 return NULL;
1594
1595         /* Allocate a transaction descriptor. */
1596         desc = xilinx_dma_alloc_tx_descriptor(chan);
1597         if (!desc)
1598                 return NULL;
1599
1600         dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1601         desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1602         async_tx_ack(&desc->async_tx);
1603
1604         /* Allocate the link descriptor from DMA pool */
1605         segment = xilinx_vdma_alloc_tx_segment(chan);
1606         if (!segment)
1607                 goto error;
1608
1609         /* Fill in the hardware descriptor */
1610         hw = &segment->hw;
1611         hw->vsize = xt->numf;
1612         hw->hsize = xt->sgl[0].size;
1613         hw->stride = (xt->sgl[0].icg + xt->sgl[0].size) <<
1614                         XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT;
1615         hw->stride |= chan->config.frm_dly <<
1616                         XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT;
1617
1618         if (xt->dir != DMA_MEM_TO_DEV) {
1619                 if (chan->ext_addr) {
1620                         hw->buf_addr = lower_32_bits(xt->dst_start);
1621                         hw->buf_addr_msb = upper_32_bits(xt->dst_start);
1622                 } else {
1623                         hw->buf_addr = xt->dst_start;
1624                 }
1625         } else {
1626                 if (chan->ext_addr) {
1627                         hw->buf_addr = lower_32_bits(xt->src_start);
1628                         hw->buf_addr_msb = upper_32_bits(xt->src_start);
1629                 } else {
1630                         hw->buf_addr = xt->src_start;
1631                 }
1632         }
1633
1634         /* Insert the segment into the descriptor segments list. */
1635         list_add_tail(&segment->node, &desc->segments);
1636
1637         prev = segment;
1638
1639         /* Link the last hardware descriptor with the first. */
1640         segment = list_first_entry(&desc->segments,
1641                                    struct xilinx_vdma_tx_segment, node);
1642         desc->async_tx.phys = segment->phys;
1643
1644         return &desc->async_tx;
1645
1646 error:
1647         xilinx_dma_free_tx_descriptor(chan, desc);
1648         return NULL;
1649 }
1650
1651 /**
1652  * xilinx_cdma_prep_memcpy - prepare descriptors for a memcpy transaction
1653  * @dchan: DMA channel
1654  * @dma_dst: destination address
1655  * @dma_src: source address
1656  * @len: transfer length
1657  * @flags: transfer ack flags
1658  *
1659  * Return: Async transaction descriptor on success and NULL on failure
1660  */
1661 static struct dma_async_tx_descriptor *
1662 xilinx_cdma_prep_memcpy(struct dma_chan *dchan, dma_addr_t dma_dst,
1663                         dma_addr_t dma_src, size_t len, unsigned long flags)
1664 {
1665         struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1666         struct xilinx_dma_tx_descriptor *desc;
1667         struct xilinx_cdma_tx_segment *segment;
1668         struct xilinx_cdma_desc_hw *hw;
1669
1670         if (!len || len > XILINX_DMA_MAX_TRANS_LEN)
1671                 return NULL;
1672
1673         desc = xilinx_dma_alloc_tx_descriptor(chan);
1674         if (!desc)
1675                 return NULL;
1676
1677         dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1678         desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1679
1680         /* Allocate the link descriptor from DMA pool */
1681         segment = xilinx_cdma_alloc_tx_segment(chan);
1682         if (!segment)
1683                 goto error;
1684
1685         hw = &segment->hw;
1686         hw->control = len;
1687         hw->src_addr = dma_src;
1688         hw->dest_addr = dma_dst;
1689         if (chan->ext_addr) {
1690                 hw->src_addr_msb = upper_32_bits(dma_src);
1691                 hw->dest_addr_msb = upper_32_bits(dma_dst);
1692         }
1693
1694         /* Insert the segment into the descriptor segments list. */
1695         list_add_tail(&segment->node, &desc->segments);
1696
1697         desc->async_tx.phys = segment->phys;
1698         hw->next_desc = segment->phys;
1699
1700         return &desc->async_tx;
1701
1702 error:
1703         xilinx_dma_free_tx_descriptor(chan, desc);
1704         return NULL;
1705 }
1706
1707 /**
1708  * xilinx_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
1709  * @dchan: DMA channel
1710  * @sgl: scatterlist to transfer to/from
1711  * @sg_len: number of entries in @scatterlist
1712  * @direction: DMA direction
1713  * @flags: transfer ack flags
1714  * @context: APP words of the descriptor
1715  *
1716  * Return: Async transaction descriptor on success and NULL on failure
1717  */
1718 static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
1719         struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
1720         enum dma_transfer_direction direction, unsigned long flags,
1721         void *context)
1722 {
1723         struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1724         struct xilinx_dma_tx_descriptor *desc;
1725         struct xilinx_axidma_tx_segment *segment = NULL, *prev = NULL;
1726         u32 *app_w = (u32 *)context;
1727         struct scatterlist *sg;
1728         size_t copy;
1729         size_t sg_used;
1730         unsigned int i;
1731
1732         if (!is_slave_direction(direction))
1733                 return NULL;
1734
1735         /* Allocate a transaction descriptor. */
1736         desc = xilinx_dma_alloc_tx_descriptor(chan);
1737         if (!desc)
1738                 return NULL;
1739
1740         dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1741         desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1742
1743         /* Build transactions using information in the scatter gather list */
1744         for_each_sg(sgl, sg, sg_len, i) {
1745                 sg_used = 0;
1746
1747                 /* Loop until the entire scatterlist entry is used */
1748                 while (sg_used < sg_dma_len(sg)) {
1749                         struct xilinx_axidma_desc_hw *hw;
1750
1751                         /* Get a free segment */
1752                         segment = xilinx_axidma_alloc_tx_segment(chan);
1753                         if (!segment)
1754                                 goto error;
1755
1756                         /*
1757                          * Calculate the maximum number of bytes to transfer,
1758                          * making sure it is less than the hw limit
1759                          */
1760                         copy = min_t(size_t, sg_dma_len(sg) - sg_used,
1761                                      XILINX_DMA_MAX_TRANS_LEN);
1762                         hw = &segment->hw;
1763
1764                         /* Fill in the descriptor */
1765                         xilinx_axidma_buf(chan, hw, sg_dma_address(sg),
1766                                           sg_used, 0);
1767
1768                         hw->control = copy;
1769
1770                         if (chan->direction == DMA_MEM_TO_DEV) {
1771                                 if (app_w)
1772                                         memcpy(hw->app, app_w, sizeof(u32) *
1773                                                XILINX_DMA_NUM_APP_WORDS);
1774                         }
1775
1776                         if (prev)
1777                                 prev->hw.next_desc = segment->phys;
1778
1779                         prev = segment;
1780                         sg_used += copy;
1781
1782                         /*
1783                          * Insert the segment into the descriptor segments
1784                          * list.
1785                          */
1786                         list_add_tail(&segment->node, &desc->segments);
1787                 }
1788         }
1789
1790         segment = list_first_entry(&desc->segments,
1791                                    struct xilinx_axidma_tx_segment, node);
1792         desc->async_tx.phys = segment->phys;
1793         prev->hw.next_desc = segment->phys;
1794
1795         /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1796         if (chan->direction == DMA_MEM_TO_DEV) {
1797                 segment->hw.control |= XILINX_DMA_BD_SOP;
1798                 segment = list_last_entry(&desc->segments,
1799                                           struct xilinx_axidma_tx_segment,
1800                                           node);
1801                 segment->hw.control |= XILINX_DMA_BD_EOP;
1802         }
1803
1804         return &desc->async_tx;
1805
1806 error:
1807         xilinx_dma_free_tx_descriptor(chan, desc);
1808         return NULL;
1809 }
1810
1811 /**
1812  * xilinx_dma_prep_dma_cyclic - prepare descriptors for a DMA_SLAVE transaction
1813  * @chan: DMA channel
1814  * @sgl: scatterlist to transfer to/from
1815  * @sg_len: number of entries in @scatterlist
1816  * @direction: DMA direction
1817  * @flags: transfer ack flags
1818  */
1819 static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
1820         struct dma_chan *dchan, dma_addr_t buf_addr, size_t buf_len,
1821         size_t period_len, enum dma_transfer_direction direction,
1822         unsigned long flags)
1823 {
1824         struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1825         struct xilinx_dma_tx_descriptor *desc;
1826         struct xilinx_axidma_tx_segment *segment, *head_segment, *prev = NULL;
1827         size_t copy, sg_used;
1828         unsigned int num_periods;
1829         int i;
1830         u32 reg;
1831
1832         if (!period_len)
1833                 return NULL;
1834
1835         num_periods = buf_len / period_len;
1836
1837         if (!num_periods)
1838                 return NULL;
1839
1840         if (!is_slave_direction(direction))
1841                 return NULL;
1842
1843         /* Allocate a transaction descriptor. */
1844         desc = xilinx_dma_alloc_tx_descriptor(chan);
1845         if (!desc)
1846                 return NULL;
1847
1848         chan->direction = direction;
1849         dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1850         desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1851
1852         for (i = 0; i < num_periods; ++i) {
1853                 sg_used = 0;
1854
1855                 while (sg_used < period_len) {
1856                         struct xilinx_axidma_desc_hw *hw;
1857
1858                         /* Get a free segment */
1859                         segment = xilinx_axidma_alloc_tx_segment(chan);
1860                         if (!segment)
1861                                 goto error;
1862
1863                         /*
1864                          * Calculate the maximum number of bytes to transfer,
1865                          * making sure it is less than the hw limit
1866                          */
1867                         copy = min_t(size_t, period_len - sg_used,
1868                                      XILINX_DMA_MAX_TRANS_LEN);
1869                         hw = &segment->hw;
1870                         xilinx_axidma_buf(chan, hw, buf_addr, sg_used,
1871                                           period_len * i);
1872                         hw->control = copy;
1873
1874                         if (prev)
1875                                 prev->hw.next_desc = segment->phys;
1876
1877                         prev = segment;
1878                         sg_used += copy;
1879
1880                         /*
1881                          * Insert the segment into the descriptor segments
1882                          * list.
1883                          */
1884                         list_add_tail(&segment->node, &desc->segments);
1885                 }
1886         }
1887
1888         head_segment = list_first_entry(&desc->segments,
1889                                    struct xilinx_axidma_tx_segment, node);
1890         desc->async_tx.phys = head_segment->phys;
1891
1892         desc->cyclic = true;
1893         reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1894         reg |= XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
1895         dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1896
1897         segment = list_last_entry(&desc->segments,
1898                                   struct xilinx_axidma_tx_segment,
1899                                   node);
1900         segment->hw.next_desc = (u32) head_segment->phys;
1901
1902         /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1903         if (direction == DMA_MEM_TO_DEV) {
1904                 head_segment->hw.control |= XILINX_DMA_BD_SOP;
1905                 segment->hw.control |= XILINX_DMA_BD_EOP;
1906         }
1907
1908         return &desc->async_tx;
1909
1910 error:
1911         xilinx_dma_free_tx_descriptor(chan, desc);
1912         return NULL;
1913 }
1914
1915 /**
1916  * xilinx_dma_prep_interleaved - prepare a descriptor for a
1917  *      DMA_SLAVE transaction
1918  * @dchan: DMA channel
1919  * @xt: Interleaved template pointer
1920  * @flags: transfer ack flags
1921  *
1922  * Return: Async transaction descriptor on success and NULL on failure
1923  */
1924 static struct dma_async_tx_descriptor *
1925 xilinx_dma_prep_interleaved(struct dma_chan *dchan,
1926                                  struct dma_interleaved_template *xt,
1927                                  unsigned long flags)
1928 {
1929         struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1930         struct xilinx_dma_tx_descriptor *desc;
1931         struct xilinx_axidma_tx_segment *segment;
1932         struct xilinx_axidma_desc_hw *hw;
1933
1934         if (!is_slave_direction(xt->dir))
1935                 return NULL;
1936
1937         if (!xt->numf || !xt->sgl[0].size)
1938                 return NULL;
1939
1940         if (xt->frame_size != 1)
1941                 return NULL;
1942
1943         /* Allocate a transaction descriptor. */
1944         desc = xilinx_dma_alloc_tx_descriptor(chan);
1945         if (!desc)
1946                 return NULL;
1947
1948         chan->direction = xt->dir;
1949         dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1950         desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1951
1952         /* Get a free segment */
1953         segment = xilinx_axidma_alloc_tx_segment(chan);
1954         if (!segment)
1955                 goto error;
1956
1957         hw = &segment->hw;
1958
1959         /* Fill in the descriptor */
1960         if (xt->dir != DMA_MEM_TO_DEV)
1961                 hw->buf_addr = xt->dst_start;
1962         else
1963                 hw->buf_addr = xt->src_start;
1964
1965         hw->mcdma_control = chan->tdest & XILINX_DMA_BD_TDEST_MASK;
1966         hw->vsize_stride = (xt->numf << XILINX_DMA_BD_VSIZE_SHIFT) &
1967                             XILINX_DMA_BD_VSIZE_MASK;
1968         hw->vsize_stride |= (xt->sgl[0].icg + xt->sgl[0].size) &
1969                             XILINX_DMA_BD_STRIDE_MASK;
1970         hw->control = xt->sgl[0].size & XILINX_DMA_BD_HSIZE_MASK;
1971
1972         /*
1973          * Insert the segment into the descriptor segments
1974          * list.
1975          */
1976         list_add_tail(&segment->node, &desc->segments);
1977
1978
1979         segment = list_first_entry(&desc->segments,
1980                                    struct xilinx_axidma_tx_segment, node);
1981         desc->async_tx.phys = segment->phys;
1982
1983         /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1984         if (xt->dir == DMA_MEM_TO_DEV) {
1985                 segment->hw.control |= XILINX_DMA_BD_SOP;
1986                 segment = list_last_entry(&desc->segments,
1987                                           struct xilinx_axidma_tx_segment,
1988                                           node);
1989                 segment->hw.control |= XILINX_DMA_BD_EOP;
1990         }
1991
1992         return &desc->async_tx;
1993
1994 error:
1995         xilinx_dma_free_tx_descriptor(chan, desc);
1996         return NULL;
1997 }
1998
1999 /**
2000  * xilinx_dma_terminate_all - Halt the channel and free descriptors
2001  * @chan: Driver specific DMA Channel pointer
2002  */
2003 static int xilinx_dma_terminate_all(struct dma_chan *dchan)
2004 {
2005         struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
2006         u32 reg;
2007         int err;
2008
2009         if (chan->cyclic)
2010                 xilinx_dma_chan_reset(chan);
2011
2012         err = chan->stop_transfer(chan);
2013         if (err) {
2014                 dev_err(chan->dev, "Cannot stop channel %p: %x\n",
2015                         chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
2016                 chan->err = true;
2017         }
2018
2019         /* Remove and free all of the descriptors in the lists */
2020         xilinx_dma_free_descriptors(chan);
2021
2022         if (chan->cyclic) {
2023                 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
2024                 reg &= ~XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
2025                 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
2026                 chan->cyclic = false;
2027         }
2028
2029         return 0;
2030 }
2031
2032 /**
2033  * xilinx_dma_channel_set_config - Configure VDMA channel
2034  * Run-time configuration for Axi VDMA, supports:
2035  * . halt the channel
2036  * . configure interrupt coalescing and inter-packet delay threshold
2037  * . start/stop parking
2038  * . enable genlock
2039  *
2040  * @dchan: DMA channel
2041  * @cfg: VDMA device configuration pointer
2042  *
2043  * Return: '0' on success and failure value on error
2044  */
2045 int xilinx_vdma_channel_set_config(struct dma_chan *dchan,
2046                                         struct xilinx_vdma_config *cfg)
2047 {
2048         struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
2049         u32 dmacr;
2050
2051         if (cfg->reset)
2052                 return xilinx_dma_chan_reset(chan);
2053
2054         dmacr = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
2055
2056         chan->config.frm_dly = cfg->frm_dly;
2057         chan->config.park = cfg->park;
2058
2059         /* genlock settings */
2060         chan->config.gen_lock = cfg->gen_lock;
2061         chan->config.master = cfg->master;
2062
2063         dmacr &= ~XILINX_DMA_DMACR_GENLOCK_EN;
2064         if (cfg->gen_lock && chan->genlock) {
2065                 dmacr |= XILINX_DMA_DMACR_GENLOCK_EN;
2066                 dmacr &= ~XILINX_DMA_DMACR_MASTER_MASK;
2067                 dmacr |= cfg->master << XILINX_DMA_DMACR_MASTER_SHIFT;
2068         }
2069
2070         chan->config.frm_cnt_en = cfg->frm_cnt_en;
2071         if (cfg->park)
2072                 chan->config.park_frm = cfg->park_frm;
2073         else
2074                 chan->config.park_frm = -1;
2075
2076         chan->config.coalesc = cfg->coalesc;
2077         chan->config.delay = cfg->delay;
2078
2079         if (cfg->coalesc <= XILINX_DMA_DMACR_FRAME_COUNT_MAX) {
2080                 dmacr &= ~XILINX_DMA_DMACR_FRAME_COUNT_MASK;
2081                 dmacr |= cfg->coalesc << XILINX_DMA_DMACR_FRAME_COUNT_SHIFT;
2082                 chan->config.coalesc = cfg->coalesc;
2083         }
2084
2085         if (cfg->delay <= XILINX_DMA_DMACR_DELAY_MAX) {
2086                 dmacr &= ~XILINX_DMA_DMACR_DELAY_MASK;
2087                 dmacr |= cfg->delay << XILINX_DMA_DMACR_DELAY_SHIFT;
2088                 chan->config.delay = cfg->delay;
2089         }
2090
2091         /* FSync Source selection */
2092         dmacr &= ~XILINX_DMA_DMACR_FSYNCSRC_MASK;
2093         dmacr |= cfg->ext_fsync << XILINX_DMA_DMACR_FSYNCSRC_SHIFT;
2094
2095         dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, dmacr);
2096
2097         return 0;
2098 }
2099 EXPORT_SYMBOL(xilinx_vdma_channel_set_config);
2100
2101 /* -----------------------------------------------------------------------------
2102  * Probe and remove
2103  */
2104
2105 /**
2106  * xilinx_dma_chan_remove - Per Channel remove function
2107  * @chan: Driver specific DMA channel
2108  */
2109 static void xilinx_dma_chan_remove(struct xilinx_dma_chan *chan)
2110 {
2111         /* Disable all interrupts */
2112         dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
2113                       XILINX_DMA_DMAXR_ALL_IRQ_MASK);
2114
2115         if (chan->irq > 0)
2116                 free_irq(chan->irq, chan);
2117
2118         tasklet_kill(&chan->tasklet);
2119
2120         list_del(&chan->common.device_node);
2121 }
2122
2123 static int axidma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2124                             struct clk **tx_clk, struct clk **rx_clk,
2125                             struct clk **sg_clk, struct clk **tmp_clk)
2126 {
2127         int err;
2128
2129         *tmp_clk = NULL;
2130
2131         *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2132         if (IS_ERR(*axi_clk)) {
2133                 err = PTR_ERR(*axi_clk);
2134                 dev_err(&pdev->dev, "failed to get axi_aclk (%d)\n", err);
2135                 return err;
2136         }
2137
2138         *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2139         if (IS_ERR(*tx_clk))
2140                 *tx_clk = NULL;
2141
2142         *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2143         if (IS_ERR(*rx_clk))
2144                 *rx_clk = NULL;
2145
2146         *sg_clk = devm_clk_get(&pdev->dev, "m_axi_sg_aclk");
2147         if (IS_ERR(*sg_clk))
2148                 *sg_clk = NULL;
2149
2150         err = clk_prepare_enable(*axi_clk);
2151         if (err) {
2152                 dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n", err);
2153                 return err;
2154         }
2155
2156         err = clk_prepare_enable(*tx_clk);
2157         if (err) {
2158                 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
2159                 goto err_disable_axiclk;
2160         }
2161
2162         err = clk_prepare_enable(*rx_clk);
2163         if (err) {
2164                 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
2165                 goto err_disable_txclk;
2166         }
2167
2168         err = clk_prepare_enable(*sg_clk);
2169         if (err) {
2170                 dev_err(&pdev->dev, "failed to enable sg_clk (%d)\n", err);
2171                 goto err_disable_rxclk;
2172         }
2173
2174         return 0;
2175
2176 err_disable_rxclk:
2177         clk_disable_unprepare(*rx_clk);
2178 err_disable_txclk:
2179         clk_disable_unprepare(*tx_clk);
2180 err_disable_axiclk:
2181         clk_disable_unprepare(*axi_clk);
2182
2183         return err;
2184 }
2185
2186 static int axicdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2187                             struct clk **dev_clk, struct clk **tmp_clk,
2188                             struct clk **tmp1_clk, struct clk **tmp2_clk)
2189 {
2190         int err;
2191
2192         *tmp_clk = NULL;
2193         *tmp1_clk = NULL;
2194         *tmp2_clk = NULL;
2195
2196         *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2197         if (IS_ERR(*axi_clk)) {
2198                 err = PTR_ERR(*axi_clk);
2199                 dev_err(&pdev->dev, "failed to get axi_clk (%d)\n", err);
2200                 return err;
2201         }
2202
2203         *dev_clk = devm_clk_get(&pdev->dev, "m_axi_aclk");
2204         if (IS_ERR(*dev_clk)) {
2205                 err = PTR_ERR(*dev_clk);
2206                 dev_err(&pdev->dev, "failed to get dev_clk (%d)\n", err);
2207                 return err;
2208         }
2209
2210         err = clk_prepare_enable(*axi_clk);
2211         if (err) {
2212                 dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n", err);
2213                 return err;
2214         }
2215
2216         err = clk_prepare_enable(*dev_clk);
2217         if (err) {
2218                 dev_err(&pdev->dev, "failed to enable dev_clk (%d)\n", err);
2219                 goto err_disable_axiclk;
2220         }
2221
2222         return 0;
2223
2224 err_disable_axiclk:
2225         clk_disable_unprepare(*axi_clk);
2226
2227         return err;
2228 }
2229
2230 static int axivdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2231                             struct clk **tx_clk, struct clk **txs_clk,
2232                             struct clk **rx_clk, struct clk **rxs_clk)
2233 {
2234         int err;
2235
2236         *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2237         if (IS_ERR(*axi_clk)) {
2238                 err = PTR_ERR(*axi_clk);
2239                 dev_err(&pdev->dev, "failed to get axi_aclk (%d)\n", err);
2240                 return err;
2241         }
2242
2243         *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2244         if (IS_ERR(*tx_clk))
2245                 *tx_clk = NULL;
2246
2247         *txs_clk = devm_clk_get(&pdev->dev, "m_axis_mm2s_aclk");
2248         if (IS_ERR(*txs_clk))
2249                 *txs_clk = NULL;
2250
2251         *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2252         if (IS_ERR(*rx_clk))
2253                 *rx_clk = NULL;
2254
2255         *rxs_clk = devm_clk_get(&pdev->dev, "s_axis_s2mm_aclk");
2256         if (IS_ERR(*rxs_clk))
2257                 *rxs_clk = NULL;
2258
2259         err = clk_prepare_enable(*axi_clk);
2260         if (err) {
2261                 dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n", err);
2262                 return err;
2263         }
2264
2265         err = clk_prepare_enable(*tx_clk);
2266         if (err) {
2267                 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
2268                 goto err_disable_axiclk;
2269         }
2270
2271         err = clk_prepare_enable(*txs_clk);
2272         if (err) {
2273                 dev_err(&pdev->dev, "failed to enable txs_clk (%d)\n", err);
2274                 goto err_disable_txclk;
2275         }
2276
2277         err = clk_prepare_enable(*rx_clk);
2278         if (err) {
2279                 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
2280                 goto err_disable_txsclk;
2281         }
2282
2283         err = clk_prepare_enable(*rxs_clk);
2284         if (err) {
2285                 dev_err(&pdev->dev, "failed to enable rxs_clk (%d)\n", err);
2286                 goto err_disable_rxclk;
2287         }
2288
2289         return 0;
2290
2291 err_disable_rxclk:
2292         clk_disable_unprepare(*rx_clk);
2293 err_disable_txsclk:
2294         clk_disable_unprepare(*txs_clk);
2295 err_disable_txclk:
2296         clk_disable_unprepare(*tx_clk);
2297 err_disable_axiclk:
2298         clk_disable_unprepare(*axi_clk);
2299
2300         return err;
2301 }
2302
2303 static void xdma_disable_allclks(struct xilinx_dma_device *xdev)
2304 {
2305         clk_disable_unprepare(xdev->rxs_clk);
2306         clk_disable_unprepare(xdev->rx_clk);
2307         clk_disable_unprepare(xdev->txs_clk);
2308         clk_disable_unprepare(xdev->tx_clk);
2309         clk_disable_unprepare(xdev->axi_clk);
2310 }
2311
2312 /**
2313  * xilinx_dma_chan_probe - Per Channel Probing
2314  * It get channel features from the device tree entry and
2315  * initialize special channel handling routines
2316  *
2317  * @xdev: Driver specific device structure
2318  * @node: Device node
2319  *
2320  * Return: '0' on success and failure value on error
2321  */
2322 static int xilinx_dma_chan_probe(struct xilinx_dma_device *xdev,
2323                                   struct device_node *node, int chan_id)
2324 {
2325         struct xilinx_dma_chan *chan;
2326         bool has_dre = false;
2327         u32 value, width;
2328         int err;
2329
2330         /* Allocate and initialize the channel structure */
2331         chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
2332         if (!chan)
2333                 return -ENOMEM;
2334
2335         chan->dev = xdev->dev;
2336         chan->xdev = xdev;
2337         chan->has_sg = xdev->has_sg;
2338         chan->desc_pendingcount = 0x0;
2339         chan->ext_addr = xdev->ext_addr;
2340
2341         spin_lock_init(&chan->lock);
2342         INIT_LIST_HEAD(&chan->pending_list);
2343         INIT_LIST_HEAD(&chan->done_list);
2344         INIT_LIST_HEAD(&chan->active_list);
2345
2346         /* Retrieve the channel properties from the device tree */
2347         has_dre = of_property_read_bool(node, "xlnx,include-dre");
2348
2349         chan->genlock = of_property_read_bool(node, "xlnx,genlock-mode");
2350
2351         err = of_property_read_u32(node, "xlnx,datawidth", &value);
2352         if (err) {
2353                 dev_err(xdev->dev, "missing xlnx,datawidth property\n");
2354                 return err;
2355         }
2356         width = value >> 3; /* Convert bits to bytes */
2357
2358         /* If data width is greater than 8 bytes, DRE is not in hw */
2359         if (width > 8)
2360                 has_dre = false;
2361
2362         if (!has_dre)
2363                 xdev->common.copy_align = (enum dmaengine_alignment)fls(width - 1);
2364
2365         if (of_device_is_compatible(node, "xlnx,axi-vdma-mm2s-channel") ||
2366             of_device_is_compatible(node, "xlnx,axi-dma-mm2s-channel") ||
2367             of_device_is_compatible(node, "xlnx,axi-cdma-channel")) {
2368                 chan->direction = DMA_MEM_TO_DEV;
2369                 chan->id = chan_id;
2370                 chan->tdest = chan_id;
2371
2372                 chan->ctrl_offset = XILINX_DMA_MM2S_CTRL_OFFSET;
2373                 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
2374                         chan->desc_offset = XILINX_VDMA_MM2S_DESC_OFFSET;
2375
2376                         if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2377                             xdev->flush_on_fsync == XILINX_DMA_FLUSH_MM2S)
2378                                 chan->flush_on_fsync = true;
2379                 }
2380         } else if (of_device_is_compatible(node,
2381                                            "xlnx,axi-vdma-s2mm-channel") ||
2382                    of_device_is_compatible(node,
2383                                            "xlnx,axi-dma-s2mm-channel")) {
2384                 chan->direction = DMA_DEV_TO_MEM;
2385                 chan->id = chan_id;
2386                 chan->tdest = chan_id - xdev->nr_channels;
2387
2388                 chan->ctrl_offset = XILINX_DMA_S2MM_CTRL_OFFSET;
2389                 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
2390                         chan->desc_offset = XILINX_VDMA_S2MM_DESC_OFFSET;
2391
2392                         if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2393                             xdev->flush_on_fsync == XILINX_DMA_FLUSH_S2MM)
2394                                 chan->flush_on_fsync = true;
2395                 }
2396         } else {
2397                 dev_err(xdev->dev, "Invalid channel compatible node\n");
2398                 return -EINVAL;
2399         }
2400
2401         /* Request the interrupt */
2402         chan->irq = irq_of_parse_and_map(node, 0);
2403         err = request_irq(chan->irq, xilinx_dma_irq_handler, IRQF_SHARED,
2404                           "xilinx-dma-controller", chan);
2405         if (err) {
2406                 dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq);
2407                 return err;
2408         }
2409
2410         if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
2411                 chan->start_transfer = xilinx_dma_start_transfer;
2412                 chan->stop_transfer = xilinx_dma_stop_transfer;
2413         } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
2414                 chan->start_transfer = xilinx_cdma_start_transfer;
2415                 chan->stop_transfer = xilinx_cdma_stop_transfer;
2416         } else {
2417                 chan->start_transfer = xilinx_vdma_start_transfer;
2418                 chan->stop_transfer = xilinx_dma_stop_transfer;
2419         }
2420
2421         /* Initialize the tasklet */
2422         tasklet_init(&chan->tasklet, xilinx_dma_do_tasklet,
2423                         (unsigned long)chan);
2424
2425         /*
2426          * Initialize the DMA channel and add it to the DMA engine channels
2427          * list.
2428          */
2429         chan->common.device = &xdev->common;
2430
2431         list_add_tail(&chan->common.device_node, &xdev->common.channels);
2432         xdev->chan[chan->id] = chan;
2433
2434         /* Reset the channel */
2435         err = xilinx_dma_chan_reset(chan);
2436         if (err < 0) {
2437                 dev_err(xdev->dev, "Reset channel failed\n");
2438                 return err;
2439         }
2440
2441         return 0;
2442 }
2443
2444 /**
2445  * xilinx_dma_child_probe - Per child node probe
2446  * It get number of dma-channels per child node from
2447  * device-tree and initializes all the channels.
2448  *
2449  * @xdev: Driver specific device structure
2450  * @node: Device node
2451  *
2452  * Return: 0 always.
2453  */
2454 static int xilinx_dma_child_probe(struct xilinx_dma_device *xdev,
2455                                     struct device_node *node) {
2456         int ret, i, nr_channels = 1;
2457
2458         ret = of_property_read_u32(node, "dma-channels", &nr_channels);
2459         if ((ret < 0) && xdev->mcdma)
2460                 dev_warn(xdev->dev, "missing dma-channels property\n");
2461
2462         for (i = 0; i < nr_channels; i++)
2463                 xilinx_dma_chan_probe(xdev, node, xdev->chan_id++);
2464
2465         xdev->nr_channels += nr_channels;
2466
2467         return 0;
2468 }
2469
2470 /**
2471  * of_dma_xilinx_xlate - Translation function
2472  * @dma_spec: Pointer to DMA specifier as found in the device tree
2473  * @ofdma: Pointer to DMA controller data
2474  *
2475  * Return: DMA channel pointer on success and NULL on error
2476  */
2477 static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
2478                                                 struct of_dma *ofdma)
2479 {
2480         struct xilinx_dma_device *xdev = ofdma->of_dma_data;
2481         int chan_id = dma_spec->args[0];
2482
2483         if (chan_id >= xdev->nr_channels || !xdev->chan[chan_id])
2484                 return NULL;
2485
2486         return dma_get_slave_channel(&xdev->chan[chan_id]->common);
2487 }
2488
2489 static const struct xilinx_dma_config axidma_config = {
2490         .dmatype = XDMA_TYPE_AXIDMA,
2491         .clk_init = axidma_clk_init,
2492 };
2493
2494 static const struct xilinx_dma_config axicdma_config = {
2495         .dmatype = XDMA_TYPE_CDMA,
2496         .clk_init = axicdma_clk_init,
2497 };
2498
2499 static const struct xilinx_dma_config axivdma_config = {
2500         .dmatype = XDMA_TYPE_VDMA,
2501         .clk_init = axivdma_clk_init,
2502 };
2503
2504 static const struct of_device_id xilinx_dma_of_ids[] = {
2505         { .compatible = "xlnx,axi-dma-1.00.a", .data = &axidma_config },
2506         { .compatible = "xlnx,axi-cdma-1.00.a", .data = &axicdma_config },
2507         { .compatible = "xlnx,axi-vdma-1.00.a", .data = &axivdma_config },
2508         {}
2509 };
2510 MODULE_DEVICE_TABLE(of, xilinx_dma_of_ids);
2511
2512 /**
2513  * xilinx_dma_probe - Driver probe function
2514  * @pdev: Pointer to the platform_device structure
2515  *
2516  * Return: '0' on success and failure value on error
2517  */
2518 static int xilinx_dma_probe(struct platform_device *pdev)
2519 {
2520         int (*clk_init)(struct platform_device *, struct clk **, struct clk **,
2521                         struct clk **, struct clk **, struct clk **)
2522                                         = axivdma_clk_init;
2523         struct device_node *node = pdev->dev.of_node;
2524         struct xilinx_dma_device *xdev;
2525         struct device_node *child, *np = pdev->dev.of_node;
2526         struct resource *io;
2527         u32 num_frames, addr_width;
2528         int i, err;
2529
2530         /* Allocate and initialize the DMA engine structure */
2531         xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
2532         if (!xdev)
2533                 return -ENOMEM;
2534
2535         xdev->dev = &pdev->dev;
2536         if (np) {
2537                 const struct of_device_id *match;
2538
2539                 match = of_match_node(xilinx_dma_of_ids, np);
2540                 if (match && match->data) {
2541                         xdev->dma_config = match->data;
2542                         clk_init = xdev->dma_config->clk_init;
2543                 }
2544         }
2545
2546         err = clk_init(pdev, &xdev->axi_clk, &xdev->tx_clk, &xdev->txs_clk,
2547                        &xdev->rx_clk, &xdev->rxs_clk);
2548         if (err)
2549                 return err;
2550
2551         /* Request and map I/O memory */
2552         io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2553         xdev->regs = devm_ioremap_resource(&pdev->dev, io);
2554         if (IS_ERR(xdev->regs))
2555                 return PTR_ERR(xdev->regs);
2556
2557         /* Retrieve the DMA engine properties from the device tree */
2558         xdev->has_sg = of_property_read_bool(node, "xlnx,include-sg");
2559         if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA)
2560                 xdev->mcdma = of_property_read_bool(node, "xlnx,mcdma");
2561
2562         if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
2563                 err = of_property_read_u32(node, "xlnx,num-fstores",
2564                                            &num_frames);
2565                 if (err < 0) {
2566                         dev_err(xdev->dev,
2567                                 "missing xlnx,num-fstores property\n");
2568                         return err;
2569                 }
2570
2571                 err = of_property_read_u32(node, "xlnx,flush-fsync",
2572                                            &xdev->flush_on_fsync);
2573                 if (err < 0)
2574                         dev_warn(xdev->dev,
2575                                  "missing xlnx,flush-fsync property\n");
2576         }
2577
2578         err = of_property_read_u32(node, "xlnx,addrwidth", &addr_width);
2579         if (err < 0)
2580                 dev_warn(xdev->dev, "missing xlnx,addrwidth property\n");
2581
2582         if (addr_width > 32)
2583                 xdev->ext_addr = true;
2584         else
2585                 xdev->ext_addr = false;
2586
2587         /* Set the dma mask bits */
2588         dma_set_mask_and_coherent(xdev->dev, DMA_BIT_MASK(addr_width));
2589
2590         /* Initialize the DMA engine */
2591         xdev->common.dev = &pdev->dev;
2592
2593         INIT_LIST_HEAD(&xdev->common.channels);
2594         if (!(xdev->dma_config->dmatype == XDMA_TYPE_CDMA)) {
2595                 dma_cap_set(DMA_SLAVE, xdev->common.cap_mask);
2596                 dma_cap_set(DMA_PRIVATE, xdev->common.cap_mask);
2597         }
2598
2599         xdev->common.device_alloc_chan_resources =
2600                                 xilinx_dma_alloc_chan_resources;
2601         xdev->common.device_free_chan_resources =
2602                                 xilinx_dma_free_chan_resources;
2603         xdev->common.device_terminate_all = xilinx_dma_terminate_all;
2604         xdev->common.device_tx_status = xilinx_dma_tx_status;
2605         xdev->common.device_issue_pending = xilinx_dma_issue_pending;
2606         if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
2607                 dma_cap_set(DMA_CYCLIC, xdev->common.cap_mask);
2608                 xdev->common.device_prep_slave_sg = xilinx_dma_prep_slave_sg;
2609                 xdev->common.device_prep_dma_cyclic =
2610                                           xilinx_dma_prep_dma_cyclic;
2611                 xdev->common.device_prep_interleaved_dma =
2612                                         xilinx_dma_prep_interleaved;
2613                 /* Residue calculation is supported by only AXI DMA */
2614                 xdev->common.residue_granularity =
2615                                           DMA_RESIDUE_GRANULARITY_SEGMENT;
2616         } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
2617                 dma_cap_set(DMA_MEMCPY, xdev->common.cap_mask);
2618                 xdev->common.device_prep_dma_memcpy = xilinx_cdma_prep_memcpy;
2619         } else {
2620                 xdev->common.device_prep_interleaved_dma =
2621                                 xilinx_vdma_dma_prep_interleaved;
2622         }
2623
2624         platform_set_drvdata(pdev, xdev);
2625
2626         /* Initialize the channels */
2627         for_each_child_of_node(node, child) {
2628                 err = xilinx_dma_child_probe(xdev, child);
2629                 if (err < 0)
2630                         goto disable_clks;
2631         }
2632
2633         if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
2634                 for (i = 0; i < xdev->nr_channels; i++)
2635                         if (xdev->chan[i])
2636                                 xdev->chan[i]->num_frms = num_frames;
2637         }
2638
2639         /* Register the DMA engine with the core */
2640         err = dma_async_device_register(&xdev->common);
2641         if (err) {
2642                 dev_err(xdev->dev, "failed to register the dma device\n");
2643                 goto error;
2644         }
2645
2646         err = of_dma_controller_register(node, of_dma_xilinx_xlate,
2647                                          xdev);
2648         if (err < 0) {
2649                 dev_err(&pdev->dev, "Unable to register DMA to DT\n");
2650                 dma_async_device_unregister(&xdev->common);
2651                 goto error;
2652         }
2653
2654         dev_info(&pdev->dev, "Xilinx AXI VDMA Engine Driver Probed!!\n");
2655
2656         return 0;
2657
2658 disable_clks:
2659         xdma_disable_allclks(xdev);
2660 error:
2661         for (i = 0; i < xdev->nr_channels; i++)
2662                 if (xdev->chan[i])
2663                         xilinx_dma_chan_remove(xdev->chan[i]);
2664
2665         return err;
2666 }
2667
2668 /**
2669  * xilinx_dma_remove - Driver remove function
2670  * @pdev: Pointer to the platform_device structure
2671  *
2672  * Return: Always '0'
2673  */
2674 static int xilinx_dma_remove(struct platform_device *pdev)
2675 {
2676         struct xilinx_dma_device *xdev = platform_get_drvdata(pdev);
2677         int i;
2678
2679         of_dma_controller_free(pdev->dev.of_node);
2680
2681         dma_async_device_unregister(&xdev->common);
2682
2683         for (i = 0; i < xdev->nr_channels; i++)
2684                 if (xdev->chan[i])
2685                         xilinx_dma_chan_remove(xdev->chan[i]);
2686
2687         xdma_disable_allclks(xdev);
2688
2689         return 0;
2690 }
2691
2692 static struct platform_driver xilinx_vdma_driver = {
2693         .driver = {
2694                 .name = "xilinx-vdma",
2695                 .of_match_table = xilinx_dma_of_ids,
2696         },
2697         .probe = xilinx_dma_probe,
2698         .remove = xilinx_dma_remove,
2699 };
2700
2701 module_platform_driver(xilinx_vdma_driver);
2702
2703 MODULE_AUTHOR("Xilinx, Inc.");
2704 MODULE_DESCRIPTION("Xilinx VDMA driver");
2705 MODULE_LICENSE("GPL v2");