1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DMA driver for Xilinx Video DMA Engine
5 * Copyright (C) 2010-2014 Xilinx, Inc. All rights reserved.
7 * Based on the Freescale DMA driver.
10 * The AXI Video Direct Memory Access (AXI VDMA) core is a soft Xilinx IP
11 * core that provides high-bandwidth direct memory access between memory
12 * and AXI4-Stream type video target peripherals. The core provides efficient
13 * two dimensional DMA operations with independent asynchronous read (S2MM)
14 * and write (MM2S) channel operation. It can be configured to have either
15 * one channel or two channels. If configured as two channels, one is to
16 * transmit to the video device (MM2S) and another is to receive from the
17 * video device (S2MM). Initialization, status, interrupt and management
18 * registers are accessed through an AXI4-Lite slave interface.
20 * The AXI Direct Memory Access (AXI DMA) core is a soft Xilinx IP core that
21 * provides high-bandwidth one dimensional direct memory access between memory
22 * and AXI4-Stream target peripherals. It supports one receive and one
23 * transmit channel, both of them optional at synthesis time.
25 * The AXI CDMA, is a soft IP, which provides high-bandwidth Direct Memory
26 * Access (DMA) between a memory-mapped source address and a memory-mapped
27 * destination address.
29 * The AXI Multichannel Direct Memory Access (AXI MCDMA) core is a soft
30 * Xilinx IP that provides high-bandwidth direct memory access between
31 * memory and AXI4-Stream target peripherals. It provides scatter gather
32 * (SG) interface with multiple channels independent configuration support.
36 #include <linux/bitops.h>
37 #include <linux/dmapool.h>
38 #include <linux/dma/xilinx_dma.h>
39 #include <linux/init.h>
40 #include <linux/interrupt.h>
42 #include <linux/iopoll.h>
43 #include <linux/module.h>
44 #include <linux/of_address.h>
45 #include <linux/of_dma.h>
46 #include <linux/of_platform.h>
47 #include <linux/of_irq.h>
48 #include <linux/slab.h>
49 #include <linux/clk.h>
50 #include <linux/io-64-nonatomic-lo-hi.h>
52 #include "../dmaengine.h"
54 /* Register/Descriptor Offsets */
55 #define XILINX_DMA_MM2S_CTRL_OFFSET 0x0000
56 #define XILINX_DMA_S2MM_CTRL_OFFSET 0x0030
57 #define XILINX_VDMA_MM2S_DESC_OFFSET 0x0050
58 #define XILINX_VDMA_S2MM_DESC_OFFSET 0x00a0
60 /* Control Registers */
61 #define XILINX_DMA_REG_DMACR 0x0000
62 #define XILINX_DMA_DMACR_DELAY_MAX 0xff
63 #define XILINX_DMA_DMACR_DELAY_SHIFT 24
64 #define XILINX_DMA_DMACR_FRAME_COUNT_MAX 0xff
65 #define XILINX_DMA_DMACR_FRAME_COUNT_SHIFT 16
66 #define XILINX_DMA_DMACR_ERR_IRQ BIT(14)
67 #define XILINX_DMA_DMACR_DLY_CNT_IRQ BIT(13)
68 #define XILINX_DMA_DMACR_FRM_CNT_IRQ BIT(12)
69 #define XILINX_DMA_DMACR_MASTER_SHIFT 8
70 #define XILINX_DMA_DMACR_FSYNCSRC_SHIFT 5
71 #define XILINX_DMA_DMACR_FRAMECNT_EN BIT(4)
72 #define XILINX_DMA_DMACR_GENLOCK_EN BIT(3)
73 #define XILINX_DMA_DMACR_RESET BIT(2)
74 #define XILINX_DMA_DMACR_CIRC_EN BIT(1)
75 #define XILINX_DMA_DMACR_RUNSTOP BIT(0)
76 #define XILINX_DMA_DMACR_FSYNCSRC_MASK GENMASK(6, 5)
77 #define XILINX_DMA_DMACR_DELAY_MASK GENMASK(31, 24)
78 #define XILINX_DMA_DMACR_FRAME_COUNT_MASK GENMASK(23, 16)
79 #define XILINX_DMA_DMACR_MASTER_MASK GENMASK(11, 8)
81 #define XILINX_DMA_REG_DMASR 0x0004
82 #define XILINX_DMA_DMASR_EOL_LATE_ERR BIT(15)
83 #define XILINX_DMA_DMASR_ERR_IRQ BIT(14)
84 #define XILINX_DMA_DMASR_DLY_CNT_IRQ BIT(13)
85 #define XILINX_DMA_DMASR_FRM_CNT_IRQ BIT(12)
86 #define XILINX_DMA_DMASR_SOF_LATE_ERR BIT(11)
87 #define XILINX_DMA_DMASR_SG_DEC_ERR BIT(10)
88 #define XILINX_DMA_DMASR_SG_SLV_ERR BIT(9)
89 #define XILINX_DMA_DMASR_EOF_EARLY_ERR BIT(8)
90 #define XILINX_DMA_DMASR_SOF_EARLY_ERR BIT(7)
91 #define XILINX_DMA_DMASR_DMA_DEC_ERR BIT(6)
92 #define XILINX_DMA_DMASR_DMA_SLAVE_ERR BIT(5)
93 #define XILINX_DMA_DMASR_DMA_INT_ERR BIT(4)
94 #define XILINX_DMA_DMASR_SG_MASK BIT(3)
95 #define XILINX_DMA_DMASR_IDLE BIT(1)
96 #define XILINX_DMA_DMASR_HALTED BIT(0)
97 #define XILINX_DMA_DMASR_DELAY_MASK GENMASK(31, 24)
98 #define XILINX_DMA_DMASR_FRAME_COUNT_MASK GENMASK(23, 16)
100 #define XILINX_DMA_REG_CURDESC 0x0008
101 #define XILINX_DMA_REG_TAILDESC 0x0010
102 #define XILINX_DMA_REG_REG_INDEX 0x0014
103 #define XILINX_DMA_REG_FRMSTORE 0x0018
104 #define XILINX_DMA_REG_THRESHOLD 0x001c
105 #define XILINX_DMA_REG_FRMPTR_STS 0x0024
106 #define XILINX_DMA_REG_PARK_PTR 0x0028
107 #define XILINX_DMA_PARK_PTR_WR_REF_SHIFT 8
108 #define XILINX_DMA_PARK_PTR_WR_REF_MASK GENMASK(12, 8)
109 #define XILINX_DMA_PARK_PTR_RD_REF_SHIFT 0
110 #define XILINX_DMA_PARK_PTR_RD_REF_MASK GENMASK(4, 0)
111 #define XILINX_DMA_REG_VDMA_VERSION 0x002c
113 /* Register Direct Mode Registers */
114 #define XILINX_DMA_REG_VSIZE 0x0000
115 #define XILINX_DMA_REG_HSIZE 0x0004
117 #define XILINX_DMA_REG_FRMDLY_STRIDE 0x0008
118 #define XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT 24
119 #define XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT 0
121 #define XILINX_VDMA_REG_START_ADDRESS(n) (0x000c + 4 * (n))
122 #define XILINX_VDMA_REG_START_ADDRESS_64(n) (0x000c + 8 * (n))
124 #define XILINX_VDMA_REG_ENABLE_VERTICAL_FLIP 0x00ec
125 #define XILINX_VDMA_ENABLE_VERTICAL_FLIP BIT(0)
127 /* HW specific definitions */
128 #define XILINX_MCDMA_MAX_CHANS_PER_DEVICE 0x20
129 #define XILINX_DMA_MAX_CHANS_PER_DEVICE 0x2
130 #define XILINX_CDMA_MAX_CHANS_PER_DEVICE 0x1
132 #define XILINX_DMA_DMAXR_ALL_IRQ_MASK \
133 (XILINX_DMA_DMASR_FRM_CNT_IRQ | \
134 XILINX_DMA_DMASR_DLY_CNT_IRQ | \
135 XILINX_DMA_DMASR_ERR_IRQ)
137 #define XILINX_DMA_DMASR_ALL_ERR_MASK \
138 (XILINX_DMA_DMASR_EOL_LATE_ERR | \
139 XILINX_DMA_DMASR_SOF_LATE_ERR | \
140 XILINX_DMA_DMASR_SG_DEC_ERR | \
141 XILINX_DMA_DMASR_SG_SLV_ERR | \
142 XILINX_DMA_DMASR_EOF_EARLY_ERR | \
143 XILINX_DMA_DMASR_SOF_EARLY_ERR | \
144 XILINX_DMA_DMASR_DMA_DEC_ERR | \
145 XILINX_DMA_DMASR_DMA_SLAVE_ERR | \
146 XILINX_DMA_DMASR_DMA_INT_ERR)
149 * Recoverable errors are DMA Internal error, SOF Early, EOF Early
150 * and SOF Late. They are only recoverable when C_FLUSH_ON_FSYNC
151 * is enabled in the h/w system.
153 #define XILINX_DMA_DMASR_ERR_RECOVER_MASK \
154 (XILINX_DMA_DMASR_SOF_LATE_ERR | \
155 XILINX_DMA_DMASR_EOF_EARLY_ERR | \
156 XILINX_DMA_DMASR_SOF_EARLY_ERR | \
157 XILINX_DMA_DMASR_DMA_INT_ERR)
159 /* Axi VDMA Flush on Fsync bits */
160 #define XILINX_DMA_FLUSH_S2MM 3
161 #define XILINX_DMA_FLUSH_MM2S 2
162 #define XILINX_DMA_FLUSH_BOTH 1
164 /* Delay loop counter to prevent hardware failure */
165 #define XILINX_DMA_LOOP_COUNT 1000000
167 /* AXI DMA Specific Registers/Offsets */
168 #define XILINX_DMA_REG_SRCDSTADDR 0x18
169 #define XILINX_DMA_REG_BTT 0x28
171 /* AXI DMA Specific Masks/Bit fields */
172 #define XILINX_DMA_MAX_TRANS_LEN_MIN 8
173 #define XILINX_DMA_MAX_TRANS_LEN_MAX 23
174 #define XILINX_DMA_V2_MAX_TRANS_LEN_MAX 26
175 #define XILINX_DMA_CR_COALESCE_MAX GENMASK(23, 16)
176 #define XILINX_DMA_CR_CYCLIC_BD_EN_MASK BIT(4)
177 #define XILINX_DMA_CR_COALESCE_SHIFT 16
178 #define XILINX_DMA_BD_SOP BIT(27)
179 #define XILINX_DMA_BD_EOP BIT(26)
180 #define XILINX_DMA_COALESCE_MAX 255
181 #define XILINX_DMA_NUM_DESCS 255
182 #define XILINX_DMA_NUM_APP_WORDS 5
184 /* AXI CDMA Specific Registers/Offsets */
185 #define XILINX_CDMA_REG_SRCADDR 0x18
186 #define XILINX_CDMA_REG_DSTADDR 0x20
188 /* AXI CDMA Specific Masks */
189 #define XILINX_CDMA_CR_SGMODE BIT(3)
191 #define xilinx_prep_dma_addr_t(addr) \
192 ((dma_addr_t)((u64)addr##_##msb << 32 | (addr)))
194 /* AXI MCDMA Specific Registers/Offsets */
195 #define XILINX_MCDMA_MM2S_CTRL_OFFSET 0x0000
196 #define XILINX_MCDMA_S2MM_CTRL_OFFSET 0x0500
197 #define XILINX_MCDMA_CHEN_OFFSET 0x0008
198 #define XILINX_MCDMA_CH_ERR_OFFSET 0x0010
199 #define XILINX_MCDMA_RXINT_SER_OFFSET 0x0020
200 #define XILINX_MCDMA_TXINT_SER_OFFSET 0x0028
201 #define XILINX_MCDMA_CHAN_CR_OFFSET(x) (0x40 + (x) * 0x40)
202 #define XILINX_MCDMA_CHAN_SR_OFFSET(x) (0x44 + (x) * 0x40)
203 #define XILINX_MCDMA_CHAN_CDESC_OFFSET(x) (0x48 + (x) * 0x40)
204 #define XILINX_MCDMA_CHAN_TDESC_OFFSET(x) (0x50 + (x) * 0x40)
206 /* AXI MCDMA Specific Masks/Shifts */
207 #define XILINX_MCDMA_COALESCE_SHIFT 16
208 #define XILINX_MCDMA_COALESCE_MAX 24
209 #define XILINX_MCDMA_IRQ_ALL_MASK GENMASK(7, 5)
210 #define XILINX_MCDMA_COALESCE_MASK GENMASK(23, 16)
211 #define XILINX_MCDMA_CR_RUNSTOP_MASK BIT(0)
212 #define XILINX_MCDMA_IRQ_IOC_MASK BIT(5)
213 #define XILINX_MCDMA_IRQ_DELAY_MASK BIT(6)
214 #define XILINX_MCDMA_IRQ_ERR_MASK BIT(7)
215 #define XILINX_MCDMA_BD_EOP BIT(30)
216 #define XILINX_MCDMA_BD_SOP BIT(31)
219 * struct xilinx_vdma_desc_hw - Hardware Descriptor
220 * @next_desc: Next Descriptor Pointer @0x00
221 * @pad1: Reserved @0x04
222 * @buf_addr: Buffer address @0x08
223 * @buf_addr_msb: MSB of Buffer address @0x0C
224 * @vsize: Vertical Size @0x10
225 * @hsize: Horizontal Size @0x14
226 * @stride: Number of bytes between the first
227 * pixels of each horizontal line @0x18
229 struct xilinx_vdma_desc_hw {
240 * struct xilinx_axidma_desc_hw - Hardware Descriptor for AXI DMA
241 * @next_desc: Next Descriptor Pointer @0x00
242 * @next_desc_msb: MSB of Next Descriptor Pointer @0x04
243 * @buf_addr: Buffer address @0x08
244 * @buf_addr_msb: MSB of Buffer address @0x0C
245 * @reserved1: Reserved @0x10
246 * @reserved2: Reserved @0x14
247 * @control: Control field @0x18
248 * @status: Status field @0x1C
249 * @app: APP Fields @0x20 - 0x30
251 struct xilinx_axidma_desc_hw {
260 u32 app[XILINX_DMA_NUM_APP_WORDS];
264 * struct xilinx_aximcdma_desc_hw - Hardware Descriptor for AXI MCDMA
265 * @next_desc: Next Descriptor Pointer @0x00
266 * @next_desc_msb: MSB of Next Descriptor Pointer @0x04
267 * @buf_addr: Buffer address @0x08
268 * @buf_addr_msb: MSB of Buffer address @0x0C
269 * @rsvd: Reserved field @0x10
270 * @control: Control Information field @0x14
271 * @status: Status field @0x18
272 * @sideband_status: Status of sideband signals @0x1C
273 * @app: APP Fields @0x20 - 0x30
275 struct xilinx_aximcdma_desc_hw {
284 u32 app[XILINX_DMA_NUM_APP_WORDS];
288 * struct xilinx_cdma_desc_hw - Hardware Descriptor
289 * @next_desc: Next Descriptor Pointer @0x00
290 * @next_desc_msb: Next Descriptor Pointer MSB @0x04
291 * @src_addr: Source address @0x08
292 * @src_addr_msb: Source address MSB @0x0C
293 * @dest_addr: Destination address @0x10
294 * @dest_addr_msb: Destination address MSB @0x14
295 * @control: Control field @0x18
296 * @status: Status field @0x1C
298 struct xilinx_cdma_desc_hw {
310 * struct xilinx_vdma_tx_segment - Descriptor segment
311 * @hw: Hardware descriptor
312 * @node: Node in the descriptor segments list
313 * @phys: Physical address of segment
315 struct xilinx_vdma_tx_segment {
316 struct xilinx_vdma_desc_hw hw;
317 struct list_head node;
322 * struct xilinx_axidma_tx_segment - Descriptor segment
323 * @hw: Hardware descriptor
324 * @node: Node in the descriptor segments list
325 * @phys: Physical address of segment
327 struct xilinx_axidma_tx_segment {
328 struct xilinx_axidma_desc_hw hw;
329 struct list_head node;
334 * struct xilinx_aximcdma_tx_segment - Descriptor segment
335 * @hw: Hardware descriptor
336 * @node: Node in the descriptor segments list
337 * @phys: Physical address of segment
339 struct xilinx_aximcdma_tx_segment {
340 struct xilinx_aximcdma_desc_hw hw;
341 struct list_head node;
346 * struct xilinx_cdma_tx_segment - Descriptor segment
347 * @hw: Hardware descriptor
348 * @node: Node in the descriptor segments list
349 * @phys: Physical address of segment
351 struct xilinx_cdma_tx_segment {
352 struct xilinx_cdma_desc_hw hw;
353 struct list_head node;
358 * struct xilinx_dma_tx_descriptor - Per Transaction structure
359 * @async_tx: Async transaction descriptor
360 * @segments: TX segments list
361 * @node: Node in the channel descriptors list
362 * @cyclic: Check for cyclic transfers.
363 * @err: Whether the descriptor has an error.
364 * @residue: Residue of the completed descriptor
366 struct xilinx_dma_tx_descriptor {
367 struct dma_async_tx_descriptor async_tx;
368 struct list_head segments;
369 struct list_head node;
376 * struct xilinx_dma_chan - Driver specific DMA channel structure
377 * @xdev: Driver specific device structure
378 * @ctrl_offset: Control registers offset
379 * @desc_offset: TX descriptor registers offset
380 * @lock: Descriptor operation lock
381 * @pending_list: Descriptors waiting
382 * @active_list: Descriptors ready to submit
383 * @done_list: Complete descriptors
384 * @free_seg_list: Free descriptors
385 * @common: DMA common channel
386 * @desc_pool: Descriptors pool
387 * @dev: The dma device
390 * @direction: Transfer direction
391 * @num_frms: Number of frames
392 * @has_sg: Support scatter transfers
393 * @cyclic: Check for cyclic transfers.
394 * @genlock: Support genlock mode
395 * @err: Channel has errors
396 * @idle: Check for channel idle
397 * @terminating: Check for channel being synchronized by user
398 * @tasklet: Cleanup work after irq
399 * @config: Device configuration info
400 * @flush_on_fsync: Flush on Frame sync
401 * @desc_pendingcount: Descriptor pending count
402 * @ext_addr: Indicates 64 bit addressing is supported by dma channel
403 * @desc_submitcount: Descriptor h/w submitted count
404 * @seg_v: Statically allocated segments base
405 * @seg_mv: Statically allocated segments base for MCDMA
406 * @seg_p: Physical allocated segments base
407 * @cyclic_seg_v: Statically allocated segment base for cyclic transfers
408 * @cyclic_seg_p: Physical allocated segments base for cyclic dma
409 * @start_transfer: Differentiate b/w DMA IP's transfer
410 * @stop_transfer: Differentiate b/w DMA IP's quiesce
411 * @tdest: TDEST value for mcdma
412 * @has_vflip: S2MM vertical flip
414 struct xilinx_dma_chan {
415 struct xilinx_dma_device *xdev;
419 struct list_head pending_list;
420 struct list_head active_list;
421 struct list_head done_list;
422 struct list_head free_seg_list;
423 struct dma_chan common;
424 struct dma_pool *desc_pool;
428 enum dma_transfer_direction direction;
436 struct tasklet_struct tasklet;
437 struct xilinx_vdma_config config;
439 u32 desc_pendingcount;
441 u32 desc_submitcount;
442 struct xilinx_axidma_tx_segment *seg_v;
443 struct xilinx_aximcdma_tx_segment *seg_mv;
445 struct xilinx_axidma_tx_segment *cyclic_seg_v;
446 dma_addr_t cyclic_seg_p;
447 void (*start_transfer)(struct xilinx_dma_chan *chan);
448 int (*stop_transfer)(struct xilinx_dma_chan *chan);
454 * enum xdma_ip_type - DMA IP type.
456 * @XDMA_TYPE_AXIDMA: Axi dma ip.
457 * @XDMA_TYPE_CDMA: Axi cdma ip.
458 * @XDMA_TYPE_VDMA: Axi vdma ip.
459 * @XDMA_TYPE_AXIMCDMA: Axi MCDMA ip.
463 XDMA_TYPE_AXIDMA = 0,
469 struct xilinx_dma_config {
470 enum xdma_ip_type dmatype;
471 int (*clk_init)(struct platform_device *pdev, struct clk **axi_clk,
472 struct clk **tx_clk, struct clk **txs_clk,
473 struct clk **rx_clk, struct clk **rxs_clk);
474 irqreturn_t (*irq_handler)(int irq, void *data);
475 const int max_channels;
479 * struct xilinx_dma_device - DMA device structure
480 * @regs: I/O mapped base address
481 * @dev: Device Structure
482 * @common: DMA device structure
483 * @chan: Driver specific DMA channel
484 * @flush_on_fsync: Flush on frame sync
485 * @ext_addr: Indicates 64 bit addressing is supported by dma device
486 * @pdev: Platform device structure pointer
487 * @dma_config: DMA config structure
488 * @axi_clk: DMA Axi4-lite interace clock
489 * @tx_clk: DMA mm2s clock
490 * @txs_clk: DMA mm2s stream clock
491 * @rx_clk: DMA s2mm clock
492 * @rxs_clk: DMA s2mm stream clock
493 * @s2mm_chan_id: DMA s2mm channel identifier
494 * @mm2s_chan_id: DMA mm2s channel identifier
495 * @max_buffer_len: Max buffer length
497 struct xilinx_dma_device {
500 struct dma_device common;
501 struct xilinx_dma_chan *chan[XILINX_MCDMA_MAX_CHANS_PER_DEVICE];
504 struct platform_device *pdev;
505 const struct xilinx_dma_config *dma_config;
517 #define to_xilinx_chan(chan) \
518 container_of(chan, struct xilinx_dma_chan, common)
519 #define to_dma_tx_descriptor(tx) \
520 container_of(tx, struct xilinx_dma_tx_descriptor, async_tx)
521 #define xilinx_dma_poll_timeout(chan, reg, val, cond, delay_us, timeout_us) \
522 readl_poll_timeout_atomic(chan->xdev->regs + chan->ctrl_offset + reg, \
523 val, cond, delay_us, timeout_us)
526 static inline u32 dma_read(struct xilinx_dma_chan *chan, u32 reg)
528 return ioread32(chan->xdev->regs + reg);
531 static inline void dma_write(struct xilinx_dma_chan *chan, u32 reg, u32 value)
533 iowrite32(value, chan->xdev->regs + reg);
536 static inline void vdma_desc_write(struct xilinx_dma_chan *chan, u32 reg,
539 dma_write(chan, chan->desc_offset + reg, value);
542 static inline u32 dma_ctrl_read(struct xilinx_dma_chan *chan, u32 reg)
544 return dma_read(chan, chan->ctrl_offset + reg);
547 static inline void dma_ctrl_write(struct xilinx_dma_chan *chan, u32 reg,
550 dma_write(chan, chan->ctrl_offset + reg, value);
553 static inline void dma_ctrl_clr(struct xilinx_dma_chan *chan, u32 reg,
556 dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) & ~clr);
559 static inline void dma_ctrl_set(struct xilinx_dma_chan *chan, u32 reg,
562 dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) | set);
566 * vdma_desc_write_64 - 64-bit descriptor write
567 * @chan: Driver specific VDMA channel
568 * @reg: Register to write
569 * @value_lsb: lower address of the descriptor.
570 * @value_msb: upper address of the descriptor.
572 * Since vdma driver is trying to write to a register offset which is not a
573 * multiple of 64 bits(ex : 0x5c), we are writing as two separate 32 bits
574 * instead of a single 64 bit register write.
576 static inline void vdma_desc_write_64(struct xilinx_dma_chan *chan, u32 reg,
577 u32 value_lsb, u32 value_msb)
579 /* Write the lsb 32 bits*/
580 writel(value_lsb, chan->xdev->regs + chan->desc_offset + reg);
582 /* Write the msb 32 bits */
583 writel(value_msb, chan->xdev->regs + chan->desc_offset + reg + 4);
586 static inline void dma_writeq(struct xilinx_dma_chan *chan, u32 reg, u64 value)
588 lo_hi_writeq(value, chan->xdev->regs + chan->ctrl_offset + reg);
591 static inline void xilinx_write(struct xilinx_dma_chan *chan, u32 reg,
595 dma_writeq(chan, reg, addr);
597 dma_ctrl_write(chan, reg, addr);
600 static inline void xilinx_axidma_buf(struct xilinx_dma_chan *chan,
601 struct xilinx_axidma_desc_hw *hw,
602 dma_addr_t buf_addr, size_t sg_used,
605 if (chan->ext_addr) {
606 hw->buf_addr = lower_32_bits(buf_addr + sg_used + period_len);
607 hw->buf_addr_msb = upper_32_bits(buf_addr + sg_used +
610 hw->buf_addr = buf_addr + sg_used + period_len;
614 static inline void xilinx_aximcdma_buf(struct xilinx_dma_chan *chan,
615 struct xilinx_aximcdma_desc_hw *hw,
616 dma_addr_t buf_addr, size_t sg_used)
618 if (chan->ext_addr) {
619 hw->buf_addr = lower_32_bits(buf_addr + sg_used);
620 hw->buf_addr_msb = upper_32_bits(buf_addr + sg_used);
622 hw->buf_addr = buf_addr + sg_used;
626 /* -----------------------------------------------------------------------------
627 * Descriptors and segments alloc and free
631 * xilinx_vdma_alloc_tx_segment - Allocate transaction segment
632 * @chan: Driver specific DMA channel
634 * Return: The allocated segment on success and NULL on failure.
636 static struct xilinx_vdma_tx_segment *
637 xilinx_vdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
639 struct xilinx_vdma_tx_segment *segment;
642 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
646 segment->phys = phys;
652 * xilinx_cdma_alloc_tx_segment - Allocate transaction segment
653 * @chan: Driver specific DMA channel
655 * Return: The allocated segment on success and NULL on failure.
657 static struct xilinx_cdma_tx_segment *
658 xilinx_cdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
660 struct xilinx_cdma_tx_segment *segment;
663 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
667 segment->phys = phys;
673 * xilinx_axidma_alloc_tx_segment - Allocate transaction segment
674 * @chan: Driver specific DMA channel
676 * Return: The allocated segment on success and NULL on failure.
678 static struct xilinx_axidma_tx_segment *
679 xilinx_axidma_alloc_tx_segment(struct xilinx_dma_chan *chan)
681 struct xilinx_axidma_tx_segment *segment = NULL;
684 spin_lock_irqsave(&chan->lock, flags);
685 if (!list_empty(&chan->free_seg_list)) {
686 segment = list_first_entry(&chan->free_seg_list,
687 struct xilinx_axidma_tx_segment,
689 list_del(&segment->node);
691 spin_unlock_irqrestore(&chan->lock, flags);
694 dev_dbg(chan->dev, "Could not find free tx segment\n");
700 * xilinx_aximcdma_alloc_tx_segment - Allocate transaction segment
701 * @chan: Driver specific DMA channel
703 * Return: The allocated segment on success and NULL on failure.
705 static struct xilinx_aximcdma_tx_segment *
706 xilinx_aximcdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
708 struct xilinx_aximcdma_tx_segment *segment = NULL;
711 spin_lock_irqsave(&chan->lock, flags);
712 if (!list_empty(&chan->free_seg_list)) {
713 segment = list_first_entry(&chan->free_seg_list,
714 struct xilinx_aximcdma_tx_segment,
716 list_del(&segment->node);
718 spin_unlock_irqrestore(&chan->lock, flags);
723 static void xilinx_dma_clean_hw_desc(struct xilinx_axidma_desc_hw *hw)
725 u32 next_desc = hw->next_desc;
726 u32 next_desc_msb = hw->next_desc_msb;
728 memset(hw, 0, sizeof(struct xilinx_axidma_desc_hw));
730 hw->next_desc = next_desc;
731 hw->next_desc_msb = next_desc_msb;
734 static void xilinx_mcdma_clean_hw_desc(struct xilinx_aximcdma_desc_hw *hw)
736 u32 next_desc = hw->next_desc;
737 u32 next_desc_msb = hw->next_desc_msb;
739 memset(hw, 0, sizeof(struct xilinx_aximcdma_desc_hw));
741 hw->next_desc = next_desc;
742 hw->next_desc_msb = next_desc_msb;
746 * xilinx_dma_free_tx_segment - Free transaction segment
747 * @chan: Driver specific DMA channel
748 * @segment: DMA transaction segment
750 static void xilinx_dma_free_tx_segment(struct xilinx_dma_chan *chan,
751 struct xilinx_axidma_tx_segment *segment)
753 xilinx_dma_clean_hw_desc(&segment->hw);
755 list_add_tail(&segment->node, &chan->free_seg_list);
759 * xilinx_mcdma_free_tx_segment - Free transaction segment
760 * @chan: Driver specific DMA channel
761 * @segment: DMA transaction segment
763 static void xilinx_mcdma_free_tx_segment(struct xilinx_dma_chan *chan,
764 struct xilinx_aximcdma_tx_segment *
767 xilinx_mcdma_clean_hw_desc(&segment->hw);
769 list_add_tail(&segment->node, &chan->free_seg_list);
773 * xilinx_cdma_free_tx_segment - Free transaction segment
774 * @chan: Driver specific DMA channel
775 * @segment: DMA transaction segment
777 static void xilinx_cdma_free_tx_segment(struct xilinx_dma_chan *chan,
778 struct xilinx_cdma_tx_segment *segment)
780 dma_pool_free(chan->desc_pool, segment, segment->phys);
784 * xilinx_vdma_free_tx_segment - Free transaction segment
785 * @chan: Driver specific DMA channel
786 * @segment: DMA transaction segment
788 static void xilinx_vdma_free_tx_segment(struct xilinx_dma_chan *chan,
789 struct xilinx_vdma_tx_segment *segment)
791 dma_pool_free(chan->desc_pool, segment, segment->phys);
795 * xilinx_dma_tx_descriptor - Allocate transaction descriptor
796 * @chan: Driver specific DMA channel
798 * Return: The allocated descriptor on success and NULL on failure.
800 static struct xilinx_dma_tx_descriptor *
801 xilinx_dma_alloc_tx_descriptor(struct xilinx_dma_chan *chan)
803 struct xilinx_dma_tx_descriptor *desc;
805 desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
809 INIT_LIST_HEAD(&desc->segments);
815 * xilinx_dma_free_tx_descriptor - Free transaction descriptor
816 * @chan: Driver specific DMA channel
817 * @desc: DMA transaction descriptor
820 xilinx_dma_free_tx_descriptor(struct xilinx_dma_chan *chan,
821 struct xilinx_dma_tx_descriptor *desc)
823 struct xilinx_vdma_tx_segment *segment, *next;
824 struct xilinx_cdma_tx_segment *cdma_segment, *cdma_next;
825 struct xilinx_axidma_tx_segment *axidma_segment, *axidma_next;
826 struct xilinx_aximcdma_tx_segment *aximcdma_segment, *aximcdma_next;
831 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
832 list_for_each_entry_safe(segment, next, &desc->segments, node) {
833 list_del(&segment->node);
834 xilinx_vdma_free_tx_segment(chan, segment);
836 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
837 list_for_each_entry_safe(cdma_segment, cdma_next,
838 &desc->segments, node) {
839 list_del(&cdma_segment->node);
840 xilinx_cdma_free_tx_segment(chan, cdma_segment);
842 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
843 list_for_each_entry_safe(axidma_segment, axidma_next,
844 &desc->segments, node) {
845 list_del(&axidma_segment->node);
846 xilinx_dma_free_tx_segment(chan, axidma_segment);
849 list_for_each_entry_safe(aximcdma_segment, aximcdma_next,
850 &desc->segments, node) {
851 list_del(&aximcdma_segment->node);
852 xilinx_mcdma_free_tx_segment(chan, aximcdma_segment);
859 /* Required functions */
862 * xilinx_dma_free_desc_list - Free descriptors list
863 * @chan: Driver specific DMA channel
864 * @list: List to parse and delete the descriptor
866 static void xilinx_dma_free_desc_list(struct xilinx_dma_chan *chan,
867 struct list_head *list)
869 struct xilinx_dma_tx_descriptor *desc, *next;
871 list_for_each_entry_safe(desc, next, list, node) {
872 list_del(&desc->node);
873 xilinx_dma_free_tx_descriptor(chan, desc);
878 * xilinx_dma_free_descriptors - Free channel descriptors
879 * @chan: Driver specific DMA channel
881 static void xilinx_dma_free_descriptors(struct xilinx_dma_chan *chan)
885 spin_lock_irqsave(&chan->lock, flags);
887 xilinx_dma_free_desc_list(chan, &chan->pending_list);
888 xilinx_dma_free_desc_list(chan, &chan->done_list);
889 xilinx_dma_free_desc_list(chan, &chan->active_list);
891 spin_unlock_irqrestore(&chan->lock, flags);
895 * xilinx_dma_free_chan_resources - Free channel resources
896 * @dchan: DMA channel
898 static void xilinx_dma_free_chan_resources(struct dma_chan *dchan)
900 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
903 dev_dbg(chan->dev, "Free all channel resources.\n");
905 xilinx_dma_free_descriptors(chan);
907 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
908 spin_lock_irqsave(&chan->lock, flags);
909 INIT_LIST_HEAD(&chan->free_seg_list);
910 spin_unlock_irqrestore(&chan->lock, flags);
912 /* Free memory that is allocated for BD */
913 dma_free_coherent(chan->dev, sizeof(*chan->seg_v) *
914 XILINX_DMA_NUM_DESCS, chan->seg_v,
917 /* Free Memory that is allocated for cyclic DMA Mode */
918 dma_free_coherent(chan->dev, sizeof(*chan->cyclic_seg_v),
919 chan->cyclic_seg_v, chan->cyclic_seg_p);
922 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA) {
923 spin_lock_irqsave(&chan->lock, flags);
924 INIT_LIST_HEAD(&chan->free_seg_list);
925 spin_unlock_irqrestore(&chan->lock, flags);
927 /* Free memory that is allocated for BD */
928 dma_free_coherent(chan->dev, sizeof(*chan->seg_mv) *
929 XILINX_DMA_NUM_DESCS, chan->seg_mv,
933 if (chan->xdev->dma_config->dmatype != XDMA_TYPE_AXIDMA &&
934 chan->xdev->dma_config->dmatype != XDMA_TYPE_AXIMCDMA) {
935 dma_pool_destroy(chan->desc_pool);
936 chan->desc_pool = NULL;
942 * xilinx_dma_get_residue - Compute residue for a given descriptor
943 * @chan: Driver specific dma channel
944 * @desc: dma transaction descriptor
946 * Return: The number of residue bytes for the descriptor.
948 static u32 xilinx_dma_get_residue(struct xilinx_dma_chan *chan,
949 struct xilinx_dma_tx_descriptor *desc)
951 struct xilinx_cdma_tx_segment *cdma_seg;
952 struct xilinx_axidma_tx_segment *axidma_seg;
953 struct xilinx_aximcdma_tx_segment *aximcdma_seg;
954 struct xilinx_cdma_desc_hw *cdma_hw;
955 struct xilinx_axidma_desc_hw *axidma_hw;
956 struct xilinx_aximcdma_desc_hw *aximcdma_hw;
957 struct list_head *entry;
960 list_for_each(entry, &desc->segments) {
961 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
962 cdma_seg = list_entry(entry,
963 struct xilinx_cdma_tx_segment,
965 cdma_hw = &cdma_seg->hw;
966 residue += (cdma_hw->control - cdma_hw->status) &
967 chan->xdev->max_buffer_len;
968 } else if (chan->xdev->dma_config->dmatype ==
970 axidma_seg = list_entry(entry,
971 struct xilinx_axidma_tx_segment,
973 axidma_hw = &axidma_seg->hw;
974 residue += (axidma_hw->control - axidma_hw->status) &
975 chan->xdev->max_buffer_len;
979 struct xilinx_aximcdma_tx_segment,
981 aximcdma_hw = &aximcdma_seg->hw;
983 (aximcdma_hw->control - aximcdma_hw->status) &
984 chan->xdev->max_buffer_len;
992 * xilinx_dma_chan_handle_cyclic - Cyclic dma callback
993 * @chan: Driver specific dma channel
994 * @desc: dma transaction descriptor
995 * @flags: flags for spin lock
997 static void xilinx_dma_chan_handle_cyclic(struct xilinx_dma_chan *chan,
998 struct xilinx_dma_tx_descriptor *desc,
999 unsigned long *flags)
1001 dma_async_tx_callback callback;
1002 void *callback_param;
1004 callback = desc->async_tx.callback;
1005 callback_param = desc->async_tx.callback_param;
1007 spin_unlock_irqrestore(&chan->lock, *flags);
1008 callback(callback_param);
1009 spin_lock_irqsave(&chan->lock, *flags);
1014 * xilinx_dma_chan_desc_cleanup - Clean channel descriptors
1015 * @chan: Driver specific DMA channel
1017 static void xilinx_dma_chan_desc_cleanup(struct xilinx_dma_chan *chan)
1019 struct xilinx_dma_tx_descriptor *desc, *next;
1020 unsigned long flags;
1022 spin_lock_irqsave(&chan->lock, flags);
1024 list_for_each_entry_safe(desc, next, &chan->done_list, node) {
1025 struct dmaengine_result result;
1028 xilinx_dma_chan_handle_cyclic(chan, desc, &flags);
1032 /* Remove from the list of running transactions */
1033 list_del(&desc->node);
1035 if (unlikely(desc->err)) {
1036 if (chan->direction == DMA_DEV_TO_MEM)
1037 result.result = DMA_TRANS_READ_FAILED;
1039 result.result = DMA_TRANS_WRITE_FAILED;
1041 result.result = DMA_TRANS_NOERROR;
1044 result.residue = desc->residue;
1046 /* Run the link descriptor callback function */
1047 spin_unlock_irqrestore(&chan->lock, flags);
1048 dmaengine_desc_get_callback_invoke(&desc->async_tx, &result);
1049 spin_lock_irqsave(&chan->lock, flags);
1051 /* Run any dependencies, then free the descriptor */
1052 dma_run_dependencies(&desc->async_tx);
1053 xilinx_dma_free_tx_descriptor(chan, desc);
1056 * While we ran a callback the user called a terminate function,
1057 * which takes care of cleaning up any remaining descriptors
1059 if (chan->terminating)
1063 spin_unlock_irqrestore(&chan->lock, flags);
1067 * xilinx_dma_do_tasklet - Schedule completion tasklet
1068 * @t: Pointer to the Xilinx DMA channel structure
1070 static void xilinx_dma_do_tasklet(struct tasklet_struct *t)
1072 struct xilinx_dma_chan *chan = from_tasklet(chan, t, tasklet);
1074 xilinx_dma_chan_desc_cleanup(chan);
1078 * xilinx_dma_alloc_chan_resources - Allocate channel resources
1079 * @dchan: DMA channel
1081 * Return: '0' on success and failure value on error
1083 static int xilinx_dma_alloc_chan_resources(struct dma_chan *dchan)
1085 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1088 /* Has this channel already been allocated? */
1089 if (chan->desc_pool)
1093 * We need the descriptor to be aligned to 64bytes
1094 * for meeting Xilinx VDMA specification requirement.
1096 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
1097 /* Allocate the buffer descriptors. */
1098 chan->seg_v = dma_alloc_coherent(chan->dev,
1099 sizeof(*chan->seg_v) * XILINX_DMA_NUM_DESCS,
1100 &chan->seg_p, GFP_KERNEL);
1103 "unable to allocate channel %d descriptors\n",
1108 * For cyclic DMA mode we need to program the tail Descriptor
1109 * register with a value which is not a part of the BD chain
1110 * so allocating a desc segment during channel allocation for
1111 * programming tail descriptor.
1113 chan->cyclic_seg_v = dma_alloc_coherent(chan->dev,
1114 sizeof(*chan->cyclic_seg_v),
1115 &chan->cyclic_seg_p,
1117 if (!chan->cyclic_seg_v) {
1119 "unable to allocate desc segment for cyclic DMA\n");
1120 dma_free_coherent(chan->dev, sizeof(*chan->seg_v) *
1121 XILINX_DMA_NUM_DESCS, chan->seg_v,
1125 chan->cyclic_seg_v->phys = chan->cyclic_seg_p;
1127 for (i = 0; i < XILINX_DMA_NUM_DESCS; i++) {
1128 chan->seg_v[i].hw.next_desc =
1129 lower_32_bits(chan->seg_p + sizeof(*chan->seg_v) *
1130 ((i + 1) % XILINX_DMA_NUM_DESCS));
1131 chan->seg_v[i].hw.next_desc_msb =
1132 upper_32_bits(chan->seg_p + sizeof(*chan->seg_v) *
1133 ((i + 1) % XILINX_DMA_NUM_DESCS));
1134 chan->seg_v[i].phys = chan->seg_p +
1135 sizeof(*chan->seg_v) * i;
1136 list_add_tail(&chan->seg_v[i].node,
1137 &chan->free_seg_list);
1139 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA) {
1140 /* Allocate the buffer descriptors. */
1141 chan->seg_mv = dma_alloc_coherent(chan->dev,
1142 sizeof(*chan->seg_mv) *
1143 XILINX_DMA_NUM_DESCS,
1144 &chan->seg_p, GFP_KERNEL);
1145 if (!chan->seg_mv) {
1147 "unable to allocate channel %d descriptors\n",
1151 for (i = 0; i < XILINX_DMA_NUM_DESCS; i++) {
1152 chan->seg_mv[i].hw.next_desc =
1153 lower_32_bits(chan->seg_p + sizeof(*chan->seg_mv) *
1154 ((i + 1) % XILINX_DMA_NUM_DESCS));
1155 chan->seg_mv[i].hw.next_desc_msb =
1156 upper_32_bits(chan->seg_p + sizeof(*chan->seg_mv) *
1157 ((i + 1) % XILINX_DMA_NUM_DESCS));
1158 chan->seg_mv[i].phys = chan->seg_p +
1159 sizeof(*chan->seg_mv) * i;
1160 list_add_tail(&chan->seg_mv[i].node,
1161 &chan->free_seg_list);
1163 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
1164 chan->desc_pool = dma_pool_create("xilinx_cdma_desc_pool",
1166 sizeof(struct xilinx_cdma_tx_segment),
1167 __alignof__(struct xilinx_cdma_tx_segment),
1170 chan->desc_pool = dma_pool_create("xilinx_vdma_desc_pool",
1172 sizeof(struct xilinx_vdma_tx_segment),
1173 __alignof__(struct xilinx_vdma_tx_segment),
1177 if (!chan->desc_pool &&
1178 ((chan->xdev->dma_config->dmatype != XDMA_TYPE_AXIDMA) &&
1179 chan->xdev->dma_config->dmatype != XDMA_TYPE_AXIMCDMA)) {
1181 "unable to allocate channel %d descriptor pool\n",
1186 dma_cookie_init(dchan);
1188 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
1189 /* For AXI DMA resetting once channel will reset the
1190 * other channel as well so enable the interrupts here.
1192 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
1193 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
1196 if ((chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) && chan->has_sg)
1197 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
1198 XILINX_CDMA_CR_SGMODE);
1204 * xilinx_dma_calc_copysize - Calculate the amount of data to copy
1205 * @chan: Driver specific DMA channel
1206 * @size: Total data that needs to be copied
1207 * @done: Amount of data that has been already copied
1209 * Return: Amount of data that has to be copied
1211 static int xilinx_dma_calc_copysize(struct xilinx_dma_chan *chan,
1216 copy = min_t(size_t, size - done,
1217 chan->xdev->max_buffer_len);
1219 if ((copy + done < size) &&
1220 chan->xdev->common.copy_align) {
1222 * If this is not the last descriptor, make sure
1223 * the next one will be properly aligned
1225 copy = rounddown(copy,
1226 (1 << chan->xdev->common.copy_align));
1232 * xilinx_dma_tx_status - Get DMA transaction status
1233 * @dchan: DMA channel
1234 * @cookie: Transaction identifier
1235 * @txstate: Transaction state
1237 * Return: DMA transaction status
1239 static enum dma_status xilinx_dma_tx_status(struct dma_chan *dchan,
1240 dma_cookie_t cookie,
1241 struct dma_tx_state *txstate)
1243 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1244 struct xilinx_dma_tx_descriptor *desc;
1245 enum dma_status ret;
1246 unsigned long flags;
1249 ret = dma_cookie_status(dchan, cookie, txstate);
1250 if (ret == DMA_COMPLETE || !txstate)
1253 spin_lock_irqsave(&chan->lock, flags);
1254 if (!list_empty(&chan->active_list)) {
1255 desc = list_last_entry(&chan->active_list,
1256 struct xilinx_dma_tx_descriptor, node);
1258 * VDMA and simple mode do not support residue reporting, so the
1259 * residue field will always be 0.
1261 if (chan->has_sg && chan->xdev->dma_config->dmatype != XDMA_TYPE_VDMA)
1262 residue = xilinx_dma_get_residue(chan, desc);
1264 spin_unlock_irqrestore(&chan->lock, flags);
1266 dma_set_residue(txstate, residue);
1272 * xilinx_dma_stop_transfer - Halt DMA channel
1273 * @chan: Driver specific DMA channel
1275 * Return: '0' on success and failure value on error
1277 static int xilinx_dma_stop_transfer(struct xilinx_dma_chan *chan)
1281 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
1283 /* Wait for the hardware to halt */
1284 return xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
1285 val & XILINX_DMA_DMASR_HALTED, 0,
1286 XILINX_DMA_LOOP_COUNT);
1290 * xilinx_cdma_stop_transfer - Wait for the current transfer to complete
1291 * @chan: Driver specific DMA channel
1293 * Return: '0' on success and failure value on error
1295 static int xilinx_cdma_stop_transfer(struct xilinx_dma_chan *chan)
1299 return xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
1300 val & XILINX_DMA_DMASR_IDLE, 0,
1301 XILINX_DMA_LOOP_COUNT);
1305 * xilinx_dma_start - Start DMA channel
1306 * @chan: Driver specific DMA channel
1308 static void xilinx_dma_start(struct xilinx_dma_chan *chan)
1313 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
1315 /* Wait for the hardware to start */
1316 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
1317 !(val & XILINX_DMA_DMASR_HALTED), 0,
1318 XILINX_DMA_LOOP_COUNT);
1321 dev_err(chan->dev, "Cannot start channel %p: %x\n",
1322 chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
1329 * xilinx_vdma_start_transfer - Starts VDMA transfer
1330 * @chan: Driver specific channel struct pointer
1332 static void xilinx_vdma_start_transfer(struct xilinx_dma_chan *chan)
1334 struct xilinx_vdma_config *config = &chan->config;
1335 struct xilinx_dma_tx_descriptor *desc;
1337 struct xilinx_vdma_tx_segment *segment, *last = NULL;
1340 /* This function was invoked with lock held */
1347 if (list_empty(&chan->pending_list))
1350 desc = list_first_entry(&chan->pending_list,
1351 struct xilinx_dma_tx_descriptor, node);
1353 /* Configure the hardware using info in the config structure */
1354 if (chan->has_vflip) {
1355 reg = dma_read(chan, XILINX_VDMA_REG_ENABLE_VERTICAL_FLIP);
1356 reg &= ~XILINX_VDMA_ENABLE_VERTICAL_FLIP;
1357 reg |= config->vflip_en;
1358 dma_write(chan, XILINX_VDMA_REG_ENABLE_VERTICAL_FLIP,
1362 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1364 if (config->frm_cnt_en)
1365 reg |= XILINX_DMA_DMACR_FRAMECNT_EN;
1367 reg &= ~XILINX_DMA_DMACR_FRAMECNT_EN;
1369 /* If not parking, enable circular mode */
1371 reg &= ~XILINX_DMA_DMACR_CIRC_EN;
1373 reg |= XILINX_DMA_DMACR_CIRC_EN;
1375 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1377 j = chan->desc_submitcount;
1378 reg = dma_read(chan, XILINX_DMA_REG_PARK_PTR);
1379 if (chan->direction == DMA_MEM_TO_DEV) {
1380 reg &= ~XILINX_DMA_PARK_PTR_RD_REF_MASK;
1381 reg |= j << XILINX_DMA_PARK_PTR_RD_REF_SHIFT;
1383 reg &= ~XILINX_DMA_PARK_PTR_WR_REF_MASK;
1384 reg |= j << XILINX_DMA_PARK_PTR_WR_REF_SHIFT;
1386 dma_write(chan, XILINX_DMA_REG_PARK_PTR, reg);
1388 /* Start the hardware */
1389 xilinx_dma_start(chan);
1394 /* Start the transfer */
1395 if (chan->desc_submitcount < chan->num_frms)
1396 i = chan->desc_submitcount;
1398 list_for_each_entry(segment, &desc->segments, node) {
1400 vdma_desc_write_64(chan,
1401 XILINX_VDMA_REG_START_ADDRESS_64(i++),
1402 segment->hw.buf_addr,
1403 segment->hw.buf_addr_msb);
1405 vdma_desc_write(chan,
1406 XILINX_VDMA_REG_START_ADDRESS(i++),
1407 segment->hw.buf_addr);
1415 /* HW expects these parameters to be same for one transaction */
1416 vdma_desc_write(chan, XILINX_DMA_REG_HSIZE, last->hw.hsize);
1417 vdma_desc_write(chan, XILINX_DMA_REG_FRMDLY_STRIDE,
1419 vdma_desc_write(chan, XILINX_DMA_REG_VSIZE, last->hw.vsize);
1421 chan->desc_submitcount++;
1422 chan->desc_pendingcount--;
1423 list_move_tail(&desc->node, &chan->active_list);
1424 if (chan->desc_submitcount == chan->num_frms)
1425 chan->desc_submitcount = 0;
1431 * xilinx_cdma_start_transfer - Starts cdma transfer
1432 * @chan: Driver specific channel struct pointer
1434 static void xilinx_cdma_start_transfer(struct xilinx_dma_chan *chan)
1436 struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1437 struct xilinx_cdma_tx_segment *tail_segment;
1438 u32 ctrl_reg = dma_read(chan, XILINX_DMA_REG_DMACR);
1446 if (list_empty(&chan->pending_list))
1449 head_desc = list_first_entry(&chan->pending_list,
1450 struct xilinx_dma_tx_descriptor, node);
1451 tail_desc = list_last_entry(&chan->pending_list,
1452 struct xilinx_dma_tx_descriptor, node);
1453 tail_segment = list_last_entry(&tail_desc->segments,
1454 struct xilinx_cdma_tx_segment, node);
1456 if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1457 ctrl_reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1458 ctrl_reg |= chan->desc_pendingcount <<
1459 XILINX_DMA_CR_COALESCE_SHIFT;
1460 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, ctrl_reg);
1464 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
1465 XILINX_CDMA_CR_SGMODE);
1467 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
1468 XILINX_CDMA_CR_SGMODE);
1470 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1471 head_desc->async_tx.phys);
1473 /* Update tail ptr register which will start the transfer */
1474 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1475 tail_segment->phys);
1477 /* In simple mode */
1478 struct xilinx_cdma_tx_segment *segment;
1479 struct xilinx_cdma_desc_hw *hw;
1481 segment = list_first_entry(&head_desc->segments,
1482 struct xilinx_cdma_tx_segment,
1487 xilinx_write(chan, XILINX_CDMA_REG_SRCADDR,
1488 xilinx_prep_dma_addr_t(hw->src_addr));
1489 xilinx_write(chan, XILINX_CDMA_REG_DSTADDR,
1490 xilinx_prep_dma_addr_t(hw->dest_addr));
1492 /* Start the transfer */
1493 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1494 hw->control & chan->xdev->max_buffer_len);
1497 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1498 chan->desc_pendingcount = 0;
1503 * xilinx_dma_start_transfer - Starts DMA transfer
1504 * @chan: Driver specific channel struct pointer
1506 static void xilinx_dma_start_transfer(struct xilinx_dma_chan *chan)
1508 struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1509 struct xilinx_axidma_tx_segment *tail_segment;
1515 if (list_empty(&chan->pending_list))
1521 head_desc = list_first_entry(&chan->pending_list,
1522 struct xilinx_dma_tx_descriptor, node);
1523 tail_desc = list_last_entry(&chan->pending_list,
1524 struct xilinx_dma_tx_descriptor, node);
1525 tail_segment = list_last_entry(&tail_desc->segments,
1526 struct xilinx_axidma_tx_segment, node);
1528 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1530 if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1531 reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1532 reg |= chan->desc_pendingcount <<
1533 XILINX_DMA_CR_COALESCE_SHIFT;
1534 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1538 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1539 head_desc->async_tx.phys);
1541 xilinx_dma_start(chan);
1546 /* Start the transfer */
1549 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1550 chan->cyclic_seg_v->phys);
1552 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1553 tail_segment->phys);
1555 struct xilinx_axidma_tx_segment *segment;
1556 struct xilinx_axidma_desc_hw *hw;
1558 segment = list_first_entry(&head_desc->segments,
1559 struct xilinx_axidma_tx_segment,
1563 xilinx_write(chan, XILINX_DMA_REG_SRCDSTADDR,
1564 xilinx_prep_dma_addr_t(hw->buf_addr));
1566 /* Start the transfer */
1567 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1568 hw->control & chan->xdev->max_buffer_len);
1571 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1572 chan->desc_pendingcount = 0;
1577 * xilinx_mcdma_start_transfer - Starts MCDMA transfer
1578 * @chan: Driver specific channel struct pointer
1580 static void xilinx_mcdma_start_transfer(struct xilinx_dma_chan *chan)
1582 struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1583 struct xilinx_aximcdma_tx_segment *tail_segment;
1587 * lock has been held by calling functions, so we don't need it
1588 * to take it here again.
1597 if (list_empty(&chan->pending_list))
1600 head_desc = list_first_entry(&chan->pending_list,
1601 struct xilinx_dma_tx_descriptor, node);
1602 tail_desc = list_last_entry(&chan->pending_list,
1603 struct xilinx_dma_tx_descriptor, node);
1604 tail_segment = list_last_entry(&tail_desc->segments,
1605 struct xilinx_aximcdma_tx_segment, node);
1607 reg = dma_ctrl_read(chan, XILINX_MCDMA_CHAN_CR_OFFSET(chan->tdest));
1609 if (chan->desc_pendingcount <= XILINX_MCDMA_COALESCE_MAX) {
1610 reg &= ~XILINX_MCDMA_COALESCE_MASK;
1611 reg |= chan->desc_pendingcount <<
1612 XILINX_MCDMA_COALESCE_SHIFT;
1615 reg |= XILINX_MCDMA_IRQ_ALL_MASK;
1616 dma_ctrl_write(chan, XILINX_MCDMA_CHAN_CR_OFFSET(chan->tdest), reg);
1618 /* Program current descriptor */
1619 xilinx_write(chan, XILINX_MCDMA_CHAN_CDESC_OFFSET(chan->tdest),
1620 head_desc->async_tx.phys);
1622 /* Program channel enable register */
1623 reg = dma_ctrl_read(chan, XILINX_MCDMA_CHEN_OFFSET);
1624 reg |= BIT(chan->tdest);
1625 dma_ctrl_write(chan, XILINX_MCDMA_CHEN_OFFSET, reg);
1627 /* Start the fetch of BDs for the channel */
1628 reg = dma_ctrl_read(chan, XILINX_MCDMA_CHAN_CR_OFFSET(chan->tdest));
1629 reg |= XILINX_MCDMA_CR_RUNSTOP_MASK;
1630 dma_ctrl_write(chan, XILINX_MCDMA_CHAN_CR_OFFSET(chan->tdest), reg);
1632 xilinx_dma_start(chan);
1637 /* Start the transfer */
1638 xilinx_write(chan, XILINX_MCDMA_CHAN_TDESC_OFFSET(chan->tdest),
1639 tail_segment->phys);
1641 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1642 chan->desc_pendingcount = 0;
1647 * xilinx_dma_issue_pending - Issue pending transactions
1648 * @dchan: DMA channel
1650 static void xilinx_dma_issue_pending(struct dma_chan *dchan)
1652 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1653 unsigned long flags;
1655 spin_lock_irqsave(&chan->lock, flags);
1656 chan->start_transfer(chan);
1657 spin_unlock_irqrestore(&chan->lock, flags);
1661 * xilinx_dma_device_config - Configure the DMA channel
1662 * @dchan: DMA channel
1663 * @config: channel configuration
1665 static int xilinx_dma_device_config(struct dma_chan *dchan,
1666 struct dma_slave_config *config)
1672 * xilinx_dma_complete_descriptor - Mark the active descriptor as complete
1673 * @chan : xilinx DMA channel
1677 static void xilinx_dma_complete_descriptor(struct xilinx_dma_chan *chan)
1679 struct xilinx_dma_tx_descriptor *desc, *next;
1681 /* This function was invoked with lock held */
1682 if (list_empty(&chan->active_list))
1685 list_for_each_entry_safe(desc, next, &chan->active_list, node) {
1686 if (chan->has_sg && chan->xdev->dma_config->dmatype !=
1688 desc->residue = xilinx_dma_get_residue(chan, desc);
1691 desc->err = chan->err;
1693 list_del(&desc->node);
1695 dma_cookie_complete(&desc->async_tx);
1696 list_add_tail(&desc->node, &chan->done_list);
1701 * xilinx_dma_reset - Reset DMA channel
1702 * @chan: Driver specific DMA channel
1704 * Return: '0' on success and failure value on error
1706 static int xilinx_dma_reset(struct xilinx_dma_chan *chan)
1711 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RESET);
1713 /* Wait for the hardware to finish reset */
1714 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMACR, tmp,
1715 !(tmp & XILINX_DMA_DMACR_RESET), 0,
1716 XILINX_DMA_LOOP_COUNT);
1719 dev_err(chan->dev, "reset timeout, cr %x, sr %x\n",
1720 dma_ctrl_read(chan, XILINX_DMA_REG_DMACR),
1721 dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
1727 chan->desc_pendingcount = 0;
1728 chan->desc_submitcount = 0;
1734 * xilinx_dma_chan_reset - Reset DMA channel and enable interrupts
1735 * @chan: Driver specific DMA channel
1737 * Return: '0' on success and failure value on error
1739 static int xilinx_dma_chan_reset(struct xilinx_dma_chan *chan)
1744 err = xilinx_dma_reset(chan);
1748 /* Enable interrupts */
1749 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
1750 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
1756 * xilinx_mcdma_irq_handler - MCDMA Interrupt handler
1758 * @data: Pointer to the Xilinx MCDMA channel structure
1760 * Return: IRQ_HANDLED/IRQ_NONE
1762 static irqreturn_t xilinx_mcdma_irq_handler(int irq, void *data)
1764 struct xilinx_dma_chan *chan = data;
1765 u32 status, ser_offset, chan_sermask, chan_offset = 0, chan_id;
1767 if (chan->direction == DMA_DEV_TO_MEM)
1768 ser_offset = XILINX_MCDMA_RXINT_SER_OFFSET;
1770 ser_offset = XILINX_MCDMA_TXINT_SER_OFFSET;
1772 /* Read the channel id raising the interrupt*/
1773 chan_sermask = dma_ctrl_read(chan, ser_offset);
1774 chan_id = ffs(chan_sermask);
1779 if (chan->direction == DMA_DEV_TO_MEM)
1780 chan_offset = chan->xdev->dma_config->max_channels / 2;
1782 chan_offset = chan_offset + (chan_id - 1);
1783 chan = chan->xdev->chan[chan_offset];
1784 /* Read the status and ack the interrupts. */
1785 status = dma_ctrl_read(chan, XILINX_MCDMA_CHAN_SR_OFFSET(chan->tdest));
1786 if (!(status & XILINX_MCDMA_IRQ_ALL_MASK))
1789 dma_ctrl_write(chan, XILINX_MCDMA_CHAN_SR_OFFSET(chan->tdest),
1790 status & XILINX_MCDMA_IRQ_ALL_MASK);
1792 if (status & XILINX_MCDMA_IRQ_ERR_MASK) {
1793 dev_err(chan->dev, "Channel %p has errors %x cdr %x tdr %x\n",
1795 dma_ctrl_read(chan, XILINX_MCDMA_CH_ERR_OFFSET),
1796 dma_ctrl_read(chan, XILINX_MCDMA_CHAN_CDESC_OFFSET
1798 dma_ctrl_read(chan, XILINX_MCDMA_CHAN_TDESC_OFFSET
1803 if (status & XILINX_MCDMA_IRQ_DELAY_MASK) {
1805 * Device takes too long to do the transfer when user requires
1808 dev_dbg(chan->dev, "Inter-packet latency too long\n");
1811 if (status & XILINX_MCDMA_IRQ_IOC_MASK) {
1812 spin_lock(&chan->lock);
1813 xilinx_dma_complete_descriptor(chan);
1815 chan->start_transfer(chan);
1816 spin_unlock(&chan->lock);
1819 tasklet_schedule(&chan->tasklet);
1824 * xilinx_dma_irq_handler - DMA Interrupt handler
1826 * @data: Pointer to the Xilinx DMA channel structure
1828 * Return: IRQ_HANDLED/IRQ_NONE
1830 static irqreturn_t xilinx_dma_irq_handler(int irq, void *data)
1832 struct xilinx_dma_chan *chan = data;
1835 /* Read the status and ack the interrupts. */
1836 status = dma_ctrl_read(chan, XILINX_DMA_REG_DMASR);
1837 if (!(status & XILINX_DMA_DMAXR_ALL_IRQ_MASK))
1840 dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1841 status & XILINX_DMA_DMAXR_ALL_IRQ_MASK);
1843 if (status & XILINX_DMA_DMASR_ERR_IRQ) {
1845 * An error occurred. If C_FLUSH_ON_FSYNC is enabled and the
1846 * error is recoverable, ignore it. Otherwise flag the error.
1848 * Only recoverable errors can be cleared in the DMASR register,
1849 * make sure not to write to other error bits to 1.
1851 u32 errors = status & XILINX_DMA_DMASR_ALL_ERR_MASK;
1853 dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1854 errors & XILINX_DMA_DMASR_ERR_RECOVER_MASK);
1856 if (!chan->flush_on_fsync ||
1857 (errors & ~XILINX_DMA_DMASR_ERR_RECOVER_MASK)) {
1859 "Channel %p has errors %x, cdr %x tdr %x\n",
1861 dma_ctrl_read(chan, XILINX_DMA_REG_CURDESC),
1862 dma_ctrl_read(chan, XILINX_DMA_REG_TAILDESC));
1867 if (status & XILINX_DMA_DMASR_DLY_CNT_IRQ) {
1869 * Device takes too long to do the transfer when user requires
1872 dev_dbg(chan->dev, "Inter-packet latency too long\n");
1875 if (status & XILINX_DMA_DMASR_FRM_CNT_IRQ) {
1876 spin_lock(&chan->lock);
1877 xilinx_dma_complete_descriptor(chan);
1879 chan->start_transfer(chan);
1880 spin_unlock(&chan->lock);
1883 tasklet_schedule(&chan->tasklet);
1888 * append_desc_queue - Queuing descriptor
1889 * @chan: Driver specific dma channel
1890 * @desc: dma transaction descriptor
1892 static void append_desc_queue(struct xilinx_dma_chan *chan,
1893 struct xilinx_dma_tx_descriptor *desc)
1895 struct xilinx_vdma_tx_segment *tail_segment;
1896 struct xilinx_dma_tx_descriptor *tail_desc;
1897 struct xilinx_axidma_tx_segment *axidma_tail_segment;
1898 struct xilinx_aximcdma_tx_segment *aximcdma_tail_segment;
1899 struct xilinx_cdma_tx_segment *cdma_tail_segment;
1901 if (list_empty(&chan->pending_list))
1905 * Add the hardware descriptor to the chain of hardware descriptors
1906 * that already exists in memory.
1908 tail_desc = list_last_entry(&chan->pending_list,
1909 struct xilinx_dma_tx_descriptor, node);
1910 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
1911 tail_segment = list_last_entry(&tail_desc->segments,
1912 struct xilinx_vdma_tx_segment,
1914 tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1915 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
1916 cdma_tail_segment = list_last_entry(&tail_desc->segments,
1917 struct xilinx_cdma_tx_segment,
1919 cdma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1920 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
1921 axidma_tail_segment = list_last_entry(&tail_desc->segments,
1922 struct xilinx_axidma_tx_segment,
1924 axidma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1926 aximcdma_tail_segment =
1927 list_last_entry(&tail_desc->segments,
1928 struct xilinx_aximcdma_tx_segment,
1930 aximcdma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1934 * Add the software descriptor and all children to the list
1935 * of pending transactions
1938 list_add_tail(&desc->node, &chan->pending_list);
1939 chan->desc_pendingcount++;
1941 if (chan->has_sg && (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA)
1942 && unlikely(chan->desc_pendingcount > chan->num_frms)) {
1943 dev_dbg(chan->dev, "desc pendingcount is too high\n");
1944 chan->desc_pendingcount = chan->num_frms;
1949 * xilinx_dma_tx_submit - Submit DMA transaction
1950 * @tx: Async transaction descriptor
1952 * Return: cookie value on success and failure value on error
1954 static dma_cookie_t xilinx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
1956 struct xilinx_dma_tx_descriptor *desc = to_dma_tx_descriptor(tx);
1957 struct xilinx_dma_chan *chan = to_xilinx_chan(tx->chan);
1958 dma_cookie_t cookie;
1959 unsigned long flags;
1963 xilinx_dma_free_tx_descriptor(chan, desc);
1969 * If reset fails, need to hard reset the system.
1970 * Channel is no longer functional
1972 err = xilinx_dma_chan_reset(chan);
1977 spin_lock_irqsave(&chan->lock, flags);
1979 cookie = dma_cookie_assign(tx);
1981 /* Put this transaction onto the tail of the pending queue */
1982 append_desc_queue(chan, desc);
1985 chan->cyclic = true;
1987 chan->terminating = false;
1989 spin_unlock_irqrestore(&chan->lock, flags);
1995 * xilinx_vdma_dma_prep_interleaved - prepare a descriptor for a
1996 * DMA_SLAVE transaction
1997 * @dchan: DMA channel
1998 * @xt: Interleaved template pointer
1999 * @flags: transfer ack flags
2001 * Return: Async transaction descriptor on success and NULL on failure
2003 static struct dma_async_tx_descriptor *
2004 xilinx_vdma_dma_prep_interleaved(struct dma_chan *dchan,
2005 struct dma_interleaved_template *xt,
2006 unsigned long flags)
2008 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
2009 struct xilinx_dma_tx_descriptor *desc;
2010 struct xilinx_vdma_tx_segment *segment;
2011 struct xilinx_vdma_desc_hw *hw;
2013 if (!is_slave_direction(xt->dir))
2016 if (!xt->numf || !xt->sgl[0].size)
2019 if (xt->frame_size != 1)
2022 /* Allocate a transaction descriptor. */
2023 desc = xilinx_dma_alloc_tx_descriptor(chan);
2027 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
2028 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
2029 async_tx_ack(&desc->async_tx);
2031 /* Allocate the link descriptor from DMA pool */
2032 segment = xilinx_vdma_alloc_tx_segment(chan);
2036 /* Fill in the hardware descriptor */
2038 hw->vsize = xt->numf;
2039 hw->hsize = xt->sgl[0].size;
2040 hw->stride = (xt->sgl[0].icg + xt->sgl[0].size) <<
2041 XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT;
2042 hw->stride |= chan->config.frm_dly <<
2043 XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT;
2045 if (xt->dir != DMA_MEM_TO_DEV) {
2046 if (chan->ext_addr) {
2047 hw->buf_addr = lower_32_bits(xt->dst_start);
2048 hw->buf_addr_msb = upper_32_bits(xt->dst_start);
2050 hw->buf_addr = xt->dst_start;
2053 if (chan->ext_addr) {
2054 hw->buf_addr = lower_32_bits(xt->src_start);
2055 hw->buf_addr_msb = upper_32_bits(xt->src_start);
2057 hw->buf_addr = xt->src_start;
2061 /* Insert the segment into the descriptor segments list. */
2062 list_add_tail(&segment->node, &desc->segments);
2064 /* Link the last hardware descriptor with the first. */
2065 segment = list_first_entry(&desc->segments,
2066 struct xilinx_vdma_tx_segment, node);
2067 desc->async_tx.phys = segment->phys;
2069 return &desc->async_tx;
2072 xilinx_dma_free_tx_descriptor(chan, desc);
2077 * xilinx_cdma_prep_memcpy - prepare descriptors for a memcpy transaction
2078 * @dchan: DMA channel
2079 * @dma_dst: destination address
2080 * @dma_src: source address
2081 * @len: transfer length
2082 * @flags: transfer ack flags
2084 * Return: Async transaction descriptor on success and NULL on failure
2086 static struct dma_async_tx_descriptor *
2087 xilinx_cdma_prep_memcpy(struct dma_chan *dchan, dma_addr_t dma_dst,
2088 dma_addr_t dma_src, size_t len, unsigned long flags)
2090 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
2091 struct xilinx_dma_tx_descriptor *desc;
2092 struct xilinx_cdma_tx_segment *segment;
2093 struct xilinx_cdma_desc_hw *hw;
2095 if (!len || len > chan->xdev->max_buffer_len)
2098 desc = xilinx_dma_alloc_tx_descriptor(chan);
2102 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
2103 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
2105 /* Allocate the link descriptor from DMA pool */
2106 segment = xilinx_cdma_alloc_tx_segment(chan);
2112 hw->src_addr = dma_src;
2113 hw->dest_addr = dma_dst;
2114 if (chan->ext_addr) {
2115 hw->src_addr_msb = upper_32_bits(dma_src);
2116 hw->dest_addr_msb = upper_32_bits(dma_dst);
2119 /* Insert the segment into the descriptor segments list. */
2120 list_add_tail(&segment->node, &desc->segments);
2122 desc->async_tx.phys = segment->phys;
2123 hw->next_desc = segment->phys;
2125 return &desc->async_tx;
2128 xilinx_dma_free_tx_descriptor(chan, desc);
2133 * xilinx_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
2134 * @dchan: DMA channel
2135 * @sgl: scatterlist to transfer to/from
2136 * @sg_len: number of entries in @scatterlist
2137 * @direction: DMA direction
2138 * @flags: transfer ack flags
2139 * @context: APP words of the descriptor
2141 * Return: Async transaction descriptor on success and NULL on failure
2143 static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
2144 struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
2145 enum dma_transfer_direction direction, unsigned long flags,
2148 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
2149 struct xilinx_dma_tx_descriptor *desc;
2150 struct xilinx_axidma_tx_segment *segment = NULL;
2151 u32 *app_w = (u32 *)context;
2152 struct scatterlist *sg;
2157 if (!is_slave_direction(direction))
2160 /* Allocate a transaction descriptor. */
2161 desc = xilinx_dma_alloc_tx_descriptor(chan);
2165 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
2166 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
2168 /* Build transactions using information in the scatter gather list */
2169 for_each_sg(sgl, sg, sg_len, i) {
2172 /* Loop until the entire scatterlist entry is used */
2173 while (sg_used < sg_dma_len(sg)) {
2174 struct xilinx_axidma_desc_hw *hw;
2176 /* Get a free segment */
2177 segment = xilinx_axidma_alloc_tx_segment(chan);
2182 * Calculate the maximum number of bytes to transfer,
2183 * making sure it is less than the hw limit
2185 copy = xilinx_dma_calc_copysize(chan, sg_dma_len(sg),
2189 /* Fill in the descriptor */
2190 xilinx_axidma_buf(chan, hw, sg_dma_address(sg),
2195 if (chan->direction == DMA_MEM_TO_DEV) {
2197 memcpy(hw->app, app_w, sizeof(u32) *
2198 XILINX_DMA_NUM_APP_WORDS);
2204 * Insert the segment into the descriptor segments
2207 list_add_tail(&segment->node, &desc->segments);
2211 segment = list_first_entry(&desc->segments,
2212 struct xilinx_axidma_tx_segment, node);
2213 desc->async_tx.phys = segment->phys;
2215 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
2216 if (chan->direction == DMA_MEM_TO_DEV) {
2217 segment->hw.control |= XILINX_DMA_BD_SOP;
2218 segment = list_last_entry(&desc->segments,
2219 struct xilinx_axidma_tx_segment,
2221 segment->hw.control |= XILINX_DMA_BD_EOP;
2224 return &desc->async_tx;
2227 xilinx_dma_free_tx_descriptor(chan, desc);
2232 * xilinx_dma_prep_dma_cyclic - prepare descriptors for a DMA_SLAVE transaction
2233 * @dchan: DMA channel
2234 * @buf_addr: Physical address of the buffer
2235 * @buf_len: Total length of the cyclic buffers
2236 * @period_len: length of individual cyclic buffer
2237 * @direction: DMA direction
2238 * @flags: transfer ack flags
2240 * Return: Async transaction descriptor on success and NULL on failure
2242 static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
2243 struct dma_chan *dchan, dma_addr_t buf_addr, size_t buf_len,
2244 size_t period_len, enum dma_transfer_direction direction,
2245 unsigned long flags)
2247 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
2248 struct xilinx_dma_tx_descriptor *desc;
2249 struct xilinx_axidma_tx_segment *segment, *head_segment, *prev = NULL;
2250 size_t copy, sg_used;
2251 unsigned int num_periods;
2258 num_periods = buf_len / period_len;
2263 if (!is_slave_direction(direction))
2266 /* Allocate a transaction descriptor. */
2267 desc = xilinx_dma_alloc_tx_descriptor(chan);
2271 chan->direction = direction;
2272 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
2273 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
2275 for (i = 0; i < num_periods; ++i) {
2278 while (sg_used < period_len) {
2279 struct xilinx_axidma_desc_hw *hw;
2281 /* Get a free segment */
2282 segment = xilinx_axidma_alloc_tx_segment(chan);
2287 * Calculate the maximum number of bytes to transfer,
2288 * making sure it is less than the hw limit
2290 copy = xilinx_dma_calc_copysize(chan, period_len,
2293 xilinx_axidma_buf(chan, hw, buf_addr, sg_used,
2298 prev->hw.next_desc = segment->phys;
2304 * Insert the segment into the descriptor segments
2307 list_add_tail(&segment->node, &desc->segments);
2311 head_segment = list_first_entry(&desc->segments,
2312 struct xilinx_axidma_tx_segment, node);
2313 desc->async_tx.phys = head_segment->phys;
2315 desc->cyclic = true;
2316 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
2317 reg |= XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
2318 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
2320 segment = list_last_entry(&desc->segments,
2321 struct xilinx_axidma_tx_segment,
2323 segment->hw.next_desc = (u32) head_segment->phys;
2325 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
2326 if (direction == DMA_MEM_TO_DEV) {
2327 head_segment->hw.control |= XILINX_DMA_BD_SOP;
2328 segment->hw.control |= XILINX_DMA_BD_EOP;
2331 return &desc->async_tx;
2334 xilinx_dma_free_tx_descriptor(chan, desc);
2339 * xilinx_mcdma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
2340 * @dchan: DMA channel
2341 * @sgl: scatterlist to transfer to/from
2342 * @sg_len: number of entries in @scatterlist
2343 * @direction: DMA direction
2344 * @flags: transfer ack flags
2345 * @context: APP words of the descriptor
2347 * Return: Async transaction descriptor on success and NULL on failure
2349 static struct dma_async_tx_descriptor *
2350 xilinx_mcdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
2351 unsigned int sg_len,
2352 enum dma_transfer_direction direction,
2353 unsigned long flags, void *context)
2355 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
2356 struct xilinx_dma_tx_descriptor *desc;
2357 struct xilinx_aximcdma_tx_segment *segment = NULL;
2358 u32 *app_w = (u32 *)context;
2359 struct scatterlist *sg;
2364 if (!is_slave_direction(direction))
2367 /* Allocate a transaction descriptor. */
2368 desc = xilinx_dma_alloc_tx_descriptor(chan);
2372 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
2373 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
2375 /* Build transactions using information in the scatter gather list */
2376 for_each_sg(sgl, sg, sg_len, i) {
2379 /* Loop until the entire scatterlist entry is used */
2380 while (sg_used < sg_dma_len(sg)) {
2381 struct xilinx_aximcdma_desc_hw *hw;
2383 /* Get a free segment */
2384 segment = xilinx_aximcdma_alloc_tx_segment(chan);
2389 * Calculate the maximum number of bytes to transfer,
2390 * making sure it is less than the hw limit
2392 copy = min_t(size_t, sg_dma_len(sg) - sg_used,
2393 chan->xdev->max_buffer_len);
2396 /* Fill in the descriptor */
2397 xilinx_aximcdma_buf(chan, hw, sg_dma_address(sg),
2401 if (chan->direction == DMA_MEM_TO_DEV && app_w) {
2402 memcpy(hw->app, app_w, sizeof(u32) *
2403 XILINX_DMA_NUM_APP_WORDS);
2408 * Insert the segment into the descriptor segments
2411 list_add_tail(&segment->node, &desc->segments);
2415 segment = list_first_entry(&desc->segments,
2416 struct xilinx_aximcdma_tx_segment, node);
2417 desc->async_tx.phys = segment->phys;
2419 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
2420 if (chan->direction == DMA_MEM_TO_DEV) {
2421 segment->hw.control |= XILINX_MCDMA_BD_SOP;
2422 segment = list_last_entry(&desc->segments,
2423 struct xilinx_aximcdma_tx_segment,
2425 segment->hw.control |= XILINX_MCDMA_BD_EOP;
2428 return &desc->async_tx;
2431 xilinx_dma_free_tx_descriptor(chan, desc);
2437 * xilinx_dma_terminate_all - Halt the channel and free descriptors
2438 * @dchan: Driver specific DMA Channel pointer
2440 * Return: '0' always.
2442 static int xilinx_dma_terminate_all(struct dma_chan *dchan)
2444 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
2448 if (!chan->cyclic) {
2449 err = chan->stop_transfer(chan);
2451 dev_err(chan->dev, "Cannot stop channel %p: %x\n",
2452 chan, dma_ctrl_read(chan,
2453 XILINX_DMA_REG_DMASR));
2458 xilinx_dma_chan_reset(chan);
2459 /* Remove and free all of the descriptors in the lists */
2460 chan->terminating = true;
2461 xilinx_dma_free_descriptors(chan);
2465 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
2466 reg &= ~XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
2467 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
2468 chan->cyclic = false;
2471 if ((chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) && chan->has_sg)
2472 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
2473 XILINX_CDMA_CR_SGMODE);
2478 static void xilinx_dma_synchronize(struct dma_chan *dchan)
2480 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
2482 tasklet_kill(&chan->tasklet);
2486 * xilinx_dma_channel_set_config - Configure VDMA channel
2487 * Run-time configuration for Axi VDMA, supports:
2488 * . halt the channel
2489 * . configure interrupt coalescing and inter-packet delay threshold
2490 * . start/stop parking
2493 * @dchan: DMA channel
2494 * @cfg: VDMA device configuration pointer
2496 * Return: '0' on success and failure value on error
2498 int xilinx_vdma_channel_set_config(struct dma_chan *dchan,
2499 struct xilinx_vdma_config *cfg)
2501 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
2505 return xilinx_dma_chan_reset(chan);
2507 dmacr = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
2509 chan->config.frm_dly = cfg->frm_dly;
2510 chan->config.park = cfg->park;
2512 /* genlock settings */
2513 chan->config.gen_lock = cfg->gen_lock;
2514 chan->config.master = cfg->master;
2516 dmacr &= ~XILINX_DMA_DMACR_GENLOCK_EN;
2517 if (cfg->gen_lock && chan->genlock) {
2518 dmacr |= XILINX_DMA_DMACR_GENLOCK_EN;
2519 dmacr &= ~XILINX_DMA_DMACR_MASTER_MASK;
2520 dmacr |= cfg->master << XILINX_DMA_DMACR_MASTER_SHIFT;
2523 chan->config.frm_cnt_en = cfg->frm_cnt_en;
2524 chan->config.vflip_en = cfg->vflip_en;
2527 chan->config.park_frm = cfg->park_frm;
2529 chan->config.park_frm = -1;
2531 chan->config.coalesc = cfg->coalesc;
2532 chan->config.delay = cfg->delay;
2534 if (cfg->coalesc <= XILINX_DMA_DMACR_FRAME_COUNT_MAX) {
2535 dmacr &= ~XILINX_DMA_DMACR_FRAME_COUNT_MASK;
2536 dmacr |= cfg->coalesc << XILINX_DMA_DMACR_FRAME_COUNT_SHIFT;
2537 chan->config.coalesc = cfg->coalesc;
2540 if (cfg->delay <= XILINX_DMA_DMACR_DELAY_MAX) {
2541 dmacr &= ~XILINX_DMA_DMACR_DELAY_MASK;
2542 dmacr |= cfg->delay << XILINX_DMA_DMACR_DELAY_SHIFT;
2543 chan->config.delay = cfg->delay;
2546 /* FSync Source selection */
2547 dmacr &= ~XILINX_DMA_DMACR_FSYNCSRC_MASK;
2548 dmacr |= cfg->ext_fsync << XILINX_DMA_DMACR_FSYNCSRC_SHIFT;
2550 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, dmacr);
2554 EXPORT_SYMBOL(xilinx_vdma_channel_set_config);
2556 /* -----------------------------------------------------------------------------
2561 * xilinx_dma_chan_remove - Per Channel remove function
2562 * @chan: Driver specific DMA channel
2564 static void xilinx_dma_chan_remove(struct xilinx_dma_chan *chan)
2566 /* Disable all interrupts */
2567 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
2568 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
2571 free_irq(chan->irq, chan);
2573 tasklet_kill(&chan->tasklet);
2575 list_del(&chan->common.device_node);
2578 static int axidma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2579 struct clk **tx_clk, struct clk **rx_clk,
2580 struct clk **sg_clk, struct clk **tmp_clk)
2586 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2587 if (IS_ERR(*axi_clk))
2588 return dev_err_probe(&pdev->dev, PTR_ERR(*axi_clk), "failed to get axi_aclk\n");
2590 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2591 if (IS_ERR(*tx_clk))
2594 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2595 if (IS_ERR(*rx_clk))
2598 *sg_clk = devm_clk_get(&pdev->dev, "m_axi_sg_aclk");
2599 if (IS_ERR(*sg_clk))
2602 err = clk_prepare_enable(*axi_clk);
2604 dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n", err);
2608 err = clk_prepare_enable(*tx_clk);
2610 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
2611 goto err_disable_axiclk;
2614 err = clk_prepare_enable(*rx_clk);
2616 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
2617 goto err_disable_txclk;
2620 err = clk_prepare_enable(*sg_clk);
2622 dev_err(&pdev->dev, "failed to enable sg_clk (%d)\n", err);
2623 goto err_disable_rxclk;
2629 clk_disable_unprepare(*rx_clk);
2631 clk_disable_unprepare(*tx_clk);
2633 clk_disable_unprepare(*axi_clk);
2638 static int axicdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2639 struct clk **dev_clk, struct clk **tmp_clk,
2640 struct clk **tmp1_clk, struct clk **tmp2_clk)
2648 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2649 if (IS_ERR(*axi_clk))
2650 return dev_err_probe(&pdev->dev, PTR_ERR(*axi_clk), "failed to get axi_aclk\n");
2652 *dev_clk = devm_clk_get(&pdev->dev, "m_axi_aclk");
2653 if (IS_ERR(*dev_clk))
2654 return dev_err_probe(&pdev->dev, PTR_ERR(*dev_clk), "failed to get dev_clk\n");
2656 err = clk_prepare_enable(*axi_clk);
2658 dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n", err);
2662 err = clk_prepare_enable(*dev_clk);
2664 dev_err(&pdev->dev, "failed to enable dev_clk (%d)\n", err);
2665 goto err_disable_axiclk;
2671 clk_disable_unprepare(*axi_clk);
2676 static int axivdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2677 struct clk **tx_clk, struct clk **txs_clk,
2678 struct clk **rx_clk, struct clk **rxs_clk)
2682 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2683 if (IS_ERR(*axi_clk))
2684 return dev_err_probe(&pdev->dev, PTR_ERR(*axi_clk), "failed to get axi_aclk\n");
2686 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2687 if (IS_ERR(*tx_clk))
2690 *txs_clk = devm_clk_get(&pdev->dev, "m_axis_mm2s_aclk");
2691 if (IS_ERR(*txs_clk))
2694 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2695 if (IS_ERR(*rx_clk))
2698 *rxs_clk = devm_clk_get(&pdev->dev, "s_axis_s2mm_aclk");
2699 if (IS_ERR(*rxs_clk))
2702 err = clk_prepare_enable(*axi_clk);
2704 dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n",
2709 err = clk_prepare_enable(*tx_clk);
2711 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
2712 goto err_disable_axiclk;
2715 err = clk_prepare_enable(*txs_clk);
2717 dev_err(&pdev->dev, "failed to enable txs_clk (%d)\n", err);
2718 goto err_disable_txclk;
2721 err = clk_prepare_enable(*rx_clk);
2723 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
2724 goto err_disable_txsclk;
2727 err = clk_prepare_enable(*rxs_clk);
2729 dev_err(&pdev->dev, "failed to enable rxs_clk (%d)\n", err);
2730 goto err_disable_rxclk;
2736 clk_disable_unprepare(*rx_clk);
2738 clk_disable_unprepare(*txs_clk);
2740 clk_disable_unprepare(*tx_clk);
2742 clk_disable_unprepare(*axi_clk);
2747 static void xdma_disable_allclks(struct xilinx_dma_device *xdev)
2749 clk_disable_unprepare(xdev->rxs_clk);
2750 clk_disable_unprepare(xdev->rx_clk);
2751 clk_disable_unprepare(xdev->txs_clk);
2752 clk_disable_unprepare(xdev->tx_clk);
2753 clk_disable_unprepare(xdev->axi_clk);
2757 * xilinx_dma_chan_probe - Per Channel Probing
2758 * It get channel features from the device tree entry and
2759 * initialize special channel handling routines
2761 * @xdev: Driver specific device structure
2762 * @node: Device node
2764 * Return: '0' on success and failure value on error
2766 static int xilinx_dma_chan_probe(struct xilinx_dma_device *xdev,
2767 struct device_node *node)
2769 struct xilinx_dma_chan *chan;
2770 bool has_dre = false;
2774 /* Allocate and initialize the channel structure */
2775 chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
2779 chan->dev = xdev->dev;
2781 chan->desc_pendingcount = 0x0;
2782 chan->ext_addr = xdev->ext_addr;
2783 /* This variable ensures that descriptors are not
2784 * Submitted when dma engine is in progress. This variable is
2785 * Added to avoid polling for a bit in the status register to
2786 * Know dma state in the driver hot path.
2790 spin_lock_init(&chan->lock);
2791 INIT_LIST_HEAD(&chan->pending_list);
2792 INIT_LIST_HEAD(&chan->done_list);
2793 INIT_LIST_HEAD(&chan->active_list);
2794 INIT_LIST_HEAD(&chan->free_seg_list);
2796 /* Retrieve the channel properties from the device tree */
2797 has_dre = of_property_read_bool(node, "xlnx,include-dre");
2799 chan->genlock = of_property_read_bool(node, "xlnx,genlock-mode");
2801 err = of_property_read_u32(node, "xlnx,datawidth", &value);
2803 dev_err(xdev->dev, "missing xlnx,datawidth property\n");
2806 width = value >> 3; /* Convert bits to bytes */
2808 /* If data width is greater than 8 bytes, DRE is not in hw */
2813 xdev->common.copy_align = (enum dmaengine_alignment)fls(width - 1);
2815 if (of_device_is_compatible(node, "xlnx,axi-vdma-mm2s-channel") ||
2816 of_device_is_compatible(node, "xlnx,axi-dma-mm2s-channel") ||
2817 of_device_is_compatible(node, "xlnx,axi-cdma-channel")) {
2818 chan->direction = DMA_MEM_TO_DEV;
2819 chan->id = xdev->mm2s_chan_id++;
2820 chan->tdest = chan->id;
2822 chan->ctrl_offset = XILINX_DMA_MM2S_CTRL_OFFSET;
2823 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
2824 chan->desc_offset = XILINX_VDMA_MM2S_DESC_OFFSET;
2825 chan->config.park = 1;
2827 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2828 xdev->flush_on_fsync == XILINX_DMA_FLUSH_MM2S)
2829 chan->flush_on_fsync = true;
2831 } else if (of_device_is_compatible(node,
2832 "xlnx,axi-vdma-s2mm-channel") ||
2833 of_device_is_compatible(node,
2834 "xlnx,axi-dma-s2mm-channel")) {
2835 chan->direction = DMA_DEV_TO_MEM;
2836 chan->id = xdev->s2mm_chan_id++;
2837 chan->tdest = chan->id - xdev->dma_config->max_channels / 2;
2838 chan->has_vflip = of_property_read_bool(node,
2839 "xlnx,enable-vert-flip");
2840 if (chan->has_vflip) {
2841 chan->config.vflip_en = dma_read(chan,
2842 XILINX_VDMA_REG_ENABLE_VERTICAL_FLIP) &
2843 XILINX_VDMA_ENABLE_VERTICAL_FLIP;
2846 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA)
2847 chan->ctrl_offset = XILINX_MCDMA_S2MM_CTRL_OFFSET;
2849 chan->ctrl_offset = XILINX_DMA_S2MM_CTRL_OFFSET;
2851 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
2852 chan->desc_offset = XILINX_VDMA_S2MM_DESC_OFFSET;
2853 chan->config.park = 1;
2855 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2856 xdev->flush_on_fsync == XILINX_DMA_FLUSH_S2MM)
2857 chan->flush_on_fsync = true;
2860 dev_err(xdev->dev, "Invalid channel compatible node\n");
2864 /* Request the interrupt */
2865 chan->irq = irq_of_parse_and_map(node, chan->tdest);
2866 err = request_irq(chan->irq, xdev->dma_config->irq_handler,
2867 IRQF_SHARED, "xilinx-dma-controller", chan);
2869 dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq);
2873 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
2874 chan->start_transfer = xilinx_dma_start_transfer;
2875 chan->stop_transfer = xilinx_dma_stop_transfer;
2876 } else if (xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA) {
2877 chan->start_transfer = xilinx_mcdma_start_transfer;
2878 chan->stop_transfer = xilinx_dma_stop_transfer;
2879 } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
2880 chan->start_transfer = xilinx_cdma_start_transfer;
2881 chan->stop_transfer = xilinx_cdma_stop_transfer;
2883 chan->start_transfer = xilinx_vdma_start_transfer;
2884 chan->stop_transfer = xilinx_dma_stop_transfer;
2887 /* check if SG is enabled (only for AXIDMA, AXIMCDMA, and CDMA) */
2888 if (xdev->dma_config->dmatype != XDMA_TYPE_VDMA) {
2889 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA ||
2890 dma_ctrl_read(chan, XILINX_DMA_REG_DMASR) &
2891 XILINX_DMA_DMASR_SG_MASK)
2892 chan->has_sg = true;
2893 dev_dbg(chan->dev, "ch %d: SG %s\n", chan->id,
2894 chan->has_sg ? "enabled" : "disabled");
2897 /* Initialize the tasklet */
2898 tasklet_setup(&chan->tasklet, xilinx_dma_do_tasklet);
2901 * Initialize the DMA channel and add it to the DMA engine channels
2904 chan->common.device = &xdev->common;
2906 list_add_tail(&chan->common.device_node, &xdev->common.channels);
2907 xdev->chan[chan->id] = chan;
2909 /* Reset the channel */
2910 err = xilinx_dma_chan_reset(chan);
2912 dev_err(xdev->dev, "Reset channel failed\n");
2920 * xilinx_dma_child_probe - Per child node probe
2921 * It get number of dma-channels per child node from
2922 * device-tree and initializes all the channels.
2924 * @xdev: Driver specific device structure
2925 * @node: Device node
2929 static int xilinx_dma_child_probe(struct xilinx_dma_device *xdev,
2930 struct device_node *node)
2933 u32 nr_channels = 1;
2935 ret = of_property_read_u32(node, "dma-channels", &nr_channels);
2936 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA && ret < 0)
2937 dev_warn(xdev->dev, "missing dma-channels property\n");
2939 for (i = 0; i < nr_channels; i++)
2940 xilinx_dma_chan_probe(xdev, node);
2946 * of_dma_xilinx_xlate - Translation function
2947 * @dma_spec: Pointer to DMA specifier as found in the device tree
2948 * @ofdma: Pointer to DMA controller data
2950 * Return: DMA channel pointer on success and NULL on error
2952 static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
2953 struct of_dma *ofdma)
2955 struct xilinx_dma_device *xdev = ofdma->of_dma_data;
2956 int chan_id = dma_spec->args[0];
2958 if (chan_id >= xdev->dma_config->max_channels || !xdev->chan[chan_id])
2961 return dma_get_slave_channel(&xdev->chan[chan_id]->common);
2964 static const struct xilinx_dma_config axidma_config = {
2965 .dmatype = XDMA_TYPE_AXIDMA,
2966 .clk_init = axidma_clk_init,
2967 .irq_handler = xilinx_dma_irq_handler,
2968 .max_channels = XILINX_DMA_MAX_CHANS_PER_DEVICE,
2971 static const struct xilinx_dma_config aximcdma_config = {
2972 .dmatype = XDMA_TYPE_AXIMCDMA,
2973 .clk_init = axidma_clk_init,
2974 .irq_handler = xilinx_mcdma_irq_handler,
2975 .max_channels = XILINX_MCDMA_MAX_CHANS_PER_DEVICE,
2977 static const struct xilinx_dma_config axicdma_config = {
2978 .dmatype = XDMA_TYPE_CDMA,
2979 .clk_init = axicdma_clk_init,
2980 .irq_handler = xilinx_dma_irq_handler,
2981 .max_channels = XILINX_CDMA_MAX_CHANS_PER_DEVICE,
2984 static const struct xilinx_dma_config axivdma_config = {
2985 .dmatype = XDMA_TYPE_VDMA,
2986 .clk_init = axivdma_clk_init,
2987 .irq_handler = xilinx_dma_irq_handler,
2988 .max_channels = XILINX_DMA_MAX_CHANS_PER_DEVICE,
2991 static const struct of_device_id xilinx_dma_of_ids[] = {
2992 { .compatible = "xlnx,axi-dma-1.00.a", .data = &axidma_config },
2993 { .compatible = "xlnx,axi-cdma-1.00.a", .data = &axicdma_config },
2994 { .compatible = "xlnx,axi-vdma-1.00.a", .data = &axivdma_config },
2995 { .compatible = "xlnx,axi-mcdma-1.00.a", .data = &aximcdma_config },
2998 MODULE_DEVICE_TABLE(of, xilinx_dma_of_ids);
3001 * xilinx_dma_probe - Driver probe function
3002 * @pdev: Pointer to the platform_device structure
3004 * Return: '0' on success and failure value on error
3006 static int xilinx_dma_probe(struct platform_device *pdev)
3008 int (*clk_init)(struct platform_device *, struct clk **, struct clk **,
3009 struct clk **, struct clk **, struct clk **)
3011 struct device_node *node = pdev->dev.of_node;
3012 struct xilinx_dma_device *xdev;
3013 struct device_node *child, *np = pdev->dev.of_node;
3014 u32 num_frames, addr_width, len_width;
3017 /* Allocate and initialize the DMA engine structure */
3018 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
3022 xdev->dev = &pdev->dev;
3024 const struct of_device_id *match;
3026 match = of_match_node(xilinx_dma_of_ids, np);
3027 if (match && match->data) {
3028 xdev->dma_config = match->data;
3029 clk_init = xdev->dma_config->clk_init;
3033 err = clk_init(pdev, &xdev->axi_clk, &xdev->tx_clk, &xdev->txs_clk,
3034 &xdev->rx_clk, &xdev->rxs_clk);
3038 /* Request and map I/O memory */
3039 xdev->regs = devm_platform_ioremap_resource(pdev, 0);
3040 if (IS_ERR(xdev->regs))
3041 return PTR_ERR(xdev->regs);
3043 /* Retrieve the DMA engine properties from the device tree */
3044 xdev->max_buffer_len = GENMASK(XILINX_DMA_MAX_TRANS_LEN_MAX - 1, 0);
3045 xdev->s2mm_chan_id = xdev->dma_config->max_channels / 2;
3047 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA ||
3048 xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA) {
3049 if (!of_property_read_u32(node, "xlnx,sg-length-width",
3051 if (len_width < XILINX_DMA_MAX_TRANS_LEN_MIN ||
3052 len_width > XILINX_DMA_V2_MAX_TRANS_LEN_MAX) {
3054 "invalid xlnx,sg-length-width property value. Using default width\n");
3056 if (len_width > XILINX_DMA_MAX_TRANS_LEN_MAX)
3057 dev_warn(xdev->dev, "Please ensure that IP supports buffer length > 23 bits\n");
3058 xdev->max_buffer_len =
3059 GENMASK(len_width - 1, 0);
3064 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
3065 err = of_property_read_u32(node, "xlnx,num-fstores",
3069 "missing xlnx,num-fstores property\n");
3073 err = of_property_read_u32(node, "xlnx,flush-fsync",
3074 &xdev->flush_on_fsync);
3077 "missing xlnx,flush-fsync property\n");
3080 err = of_property_read_u32(node, "xlnx,addrwidth", &addr_width);
3082 dev_warn(xdev->dev, "missing xlnx,addrwidth property\n");
3084 if (addr_width > 32)
3085 xdev->ext_addr = true;
3087 xdev->ext_addr = false;
3089 /* Set the dma mask bits */
3090 dma_set_mask_and_coherent(xdev->dev, DMA_BIT_MASK(addr_width));
3092 /* Initialize the DMA engine */
3093 xdev->common.dev = &pdev->dev;
3095 INIT_LIST_HEAD(&xdev->common.channels);
3096 if (!(xdev->dma_config->dmatype == XDMA_TYPE_CDMA)) {
3097 dma_cap_set(DMA_SLAVE, xdev->common.cap_mask);
3098 dma_cap_set(DMA_PRIVATE, xdev->common.cap_mask);
3101 xdev->common.device_alloc_chan_resources =
3102 xilinx_dma_alloc_chan_resources;
3103 xdev->common.device_free_chan_resources =
3104 xilinx_dma_free_chan_resources;
3105 xdev->common.device_terminate_all = xilinx_dma_terminate_all;
3106 xdev->common.device_synchronize = xilinx_dma_synchronize;
3107 xdev->common.device_tx_status = xilinx_dma_tx_status;
3108 xdev->common.device_issue_pending = xilinx_dma_issue_pending;
3109 xdev->common.device_config = xilinx_dma_device_config;
3110 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
3111 dma_cap_set(DMA_CYCLIC, xdev->common.cap_mask);
3112 xdev->common.device_prep_slave_sg = xilinx_dma_prep_slave_sg;
3113 xdev->common.device_prep_dma_cyclic =
3114 xilinx_dma_prep_dma_cyclic;
3115 /* Residue calculation is supported by only AXI DMA and CDMA */
3116 xdev->common.residue_granularity =
3117 DMA_RESIDUE_GRANULARITY_SEGMENT;
3118 } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
3119 dma_cap_set(DMA_MEMCPY, xdev->common.cap_mask);
3120 xdev->common.device_prep_dma_memcpy = xilinx_cdma_prep_memcpy;
3121 /* Residue calculation is supported by only AXI DMA and CDMA */
3122 xdev->common.residue_granularity =
3123 DMA_RESIDUE_GRANULARITY_SEGMENT;
3124 } else if (xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA) {
3125 xdev->common.device_prep_slave_sg = xilinx_mcdma_prep_slave_sg;
3127 xdev->common.device_prep_interleaved_dma =
3128 xilinx_vdma_dma_prep_interleaved;
3131 platform_set_drvdata(pdev, xdev);
3133 /* Initialize the channels */
3134 for_each_child_of_node(node, child) {
3135 err = xilinx_dma_child_probe(xdev, child);
3140 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
3141 for (i = 0; i < xdev->dma_config->max_channels; i++)
3143 xdev->chan[i]->num_frms = num_frames;
3146 /* Register the DMA engine with the core */
3147 err = dma_async_device_register(&xdev->common);
3149 dev_err(xdev->dev, "failed to register the dma device\n");
3153 err = of_dma_controller_register(node, of_dma_xilinx_xlate,
3156 dev_err(&pdev->dev, "Unable to register DMA to DT\n");
3157 dma_async_device_unregister(&xdev->common);
3161 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA)
3162 dev_info(&pdev->dev, "Xilinx AXI DMA Engine Driver Probed!!\n");
3163 else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA)
3164 dev_info(&pdev->dev, "Xilinx AXI CDMA Engine Driver Probed!!\n");
3165 else if (xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA)
3166 dev_info(&pdev->dev, "Xilinx AXI MCDMA Engine Driver Probed!!\n");
3168 dev_info(&pdev->dev, "Xilinx AXI VDMA Engine Driver Probed!!\n");
3173 xdma_disable_allclks(xdev);
3175 for (i = 0; i < xdev->dma_config->max_channels; i++)
3177 xilinx_dma_chan_remove(xdev->chan[i]);
3183 * xilinx_dma_remove - Driver remove function
3184 * @pdev: Pointer to the platform_device structure
3186 * Return: Always '0'
3188 static int xilinx_dma_remove(struct platform_device *pdev)
3190 struct xilinx_dma_device *xdev = platform_get_drvdata(pdev);
3193 of_dma_controller_free(pdev->dev.of_node);
3195 dma_async_device_unregister(&xdev->common);
3197 for (i = 0; i < xdev->dma_config->max_channels; i++)
3199 xilinx_dma_chan_remove(xdev->chan[i]);
3201 xdma_disable_allclks(xdev);
3206 static struct platform_driver xilinx_vdma_driver = {
3208 .name = "xilinx-vdma",
3209 .of_match_table = xilinx_dma_of_ids,
3211 .probe = xilinx_dma_probe,
3212 .remove = xilinx_dma_remove,
3215 module_platform_driver(xilinx_vdma_driver);
3217 MODULE_AUTHOR("Xilinx, Inc.");
3218 MODULE_DESCRIPTION("Xilinx VDMA driver");
3219 MODULE_LICENSE("GPL v2");