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