2 * DMA driver for Xilinx Video DMA Engine
4 * Copyright (C) 2010-2014 Xilinx, Inc. All rights reserved.
6 * Based on the Freescale DMA driver.
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.
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.
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.
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.
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>
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>
50 #include "../dmaengine.h"
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
58 /* Control Registers */
59 #define XILINX_DMA_REG_DMACR 0x0000
60 #define XILINX_DMA_DMACR_DELAY_MAX 0xff
61 #define XILINX_DMA_DMACR_DELAY_SHIFT 24
62 #define XILINX_DMA_DMACR_FRAME_COUNT_MAX 0xff
63 #define XILINX_DMA_DMACR_FRAME_COUNT_SHIFT 16
64 #define XILINX_DMA_DMACR_ERR_IRQ BIT(14)
65 #define XILINX_DMA_DMACR_DLY_CNT_IRQ BIT(13)
66 #define XILINX_DMA_DMACR_FRM_CNT_IRQ BIT(12)
67 #define XILINX_DMA_DMACR_MASTER_SHIFT 8
68 #define XILINX_DMA_DMACR_FSYNCSRC_SHIFT 5
69 #define XILINX_DMA_DMACR_FRAMECNT_EN BIT(4)
70 #define XILINX_DMA_DMACR_GENLOCK_EN BIT(3)
71 #define XILINX_DMA_DMACR_RESET BIT(2)
72 #define XILINX_DMA_DMACR_CIRC_EN BIT(1)
73 #define XILINX_DMA_DMACR_RUNSTOP BIT(0)
74 #define XILINX_DMA_DMACR_FSYNCSRC_MASK GENMASK(6, 5)
75 #define XILINX_DMA_DMACR_DELAY_MASK GENMASK(31, 24)
76 #define XILINX_DMA_DMACR_FRAME_COUNT_MASK GENMASK(23, 16)
77 #define XILINX_DMA_DMACR_MASTER_MASK GENMASK(11, 8)
79 #define XILINX_DMA_REG_DMASR 0x0004
80 #define XILINX_DMA_DMASR_EOL_LATE_ERR BIT(15)
81 #define XILINX_DMA_DMASR_ERR_IRQ BIT(14)
82 #define XILINX_DMA_DMASR_DLY_CNT_IRQ BIT(13)
83 #define XILINX_DMA_DMASR_FRM_CNT_IRQ BIT(12)
84 #define XILINX_DMA_DMASR_SOF_LATE_ERR BIT(11)
85 #define XILINX_DMA_DMASR_SG_DEC_ERR BIT(10)
86 #define XILINX_DMA_DMASR_SG_SLV_ERR BIT(9)
87 #define XILINX_DMA_DMASR_EOF_EARLY_ERR BIT(8)
88 #define XILINX_DMA_DMASR_SOF_EARLY_ERR BIT(7)
89 #define XILINX_DMA_DMASR_DMA_DEC_ERR BIT(6)
90 #define XILINX_DMA_DMASR_DMA_SLAVE_ERR BIT(5)
91 #define XILINX_DMA_DMASR_DMA_INT_ERR BIT(4)
92 #define XILINX_DMA_DMASR_IDLE BIT(1)
93 #define XILINX_DMA_DMASR_HALTED BIT(0)
94 #define XILINX_DMA_DMASR_DELAY_MASK GENMASK(31, 24)
95 #define XILINX_DMA_DMASR_FRAME_COUNT_MASK GENMASK(23, 16)
97 #define XILINX_DMA_REG_CURDESC 0x0008
98 #define XILINX_DMA_REG_TAILDESC 0x0010
99 #define XILINX_DMA_REG_REG_INDEX 0x0014
100 #define XILINX_DMA_REG_FRMSTORE 0x0018
101 #define XILINX_DMA_REG_THRESHOLD 0x001c
102 #define XILINX_DMA_REG_FRMPTR_STS 0x0024
103 #define XILINX_DMA_REG_PARK_PTR 0x0028
104 #define XILINX_DMA_PARK_PTR_WR_REF_SHIFT 8
105 #define XILINX_DMA_PARK_PTR_RD_REF_SHIFT 0
106 #define XILINX_DMA_REG_VDMA_VERSION 0x002c
108 /* Register Direct Mode Registers */
109 #define XILINX_DMA_REG_VSIZE 0x0000
110 #define XILINX_DMA_REG_HSIZE 0x0004
112 #define XILINX_DMA_REG_FRMDLY_STRIDE 0x0008
113 #define XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT 24
114 #define XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT 0
116 #define XILINX_VDMA_REG_START_ADDRESS(n) (0x000c + 4 * (n))
117 #define XILINX_VDMA_REG_START_ADDRESS_64(n) (0x000c + 8 * (n))
119 /* HW specific definitions */
120 #define XILINX_DMA_MAX_CHANS_PER_DEVICE 0x20
122 #define XILINX_DMA_DMAXR_ALL_IRQ_MASK \
123 (XILINX_DMA_DMASR_FRM_CNT_IRQ | \
124 XILINX_DMA_DMASR_DLY_CNT_IRQ | \
125 XILINX_DMA_DMASR_ERR_IRQ)
127 #define XILINX_DMA_DMASR_ALL_ERR_MASK \
128 (XILINX_DMA_DMASR_EOL_LATE_ERR | \
129 XILINX_DMA_DMASR_SOF_LATE_ERR | \
130 XILINX_DMA_DMASR_SG_DEC_ERR | \
131 XILINX_DMA_DMASR_SG_SLV_ERR | \
132 XILINX_DMA_DMASR_EOF_EARLY_ERR | \
133 XILINX_DMA_DMASR_SOF_EARLY_ERR | \
134 XILINX_DMA_DMASR_DMA_DEC_ERR | \
135 XILINX_DMA_DMASR_DMA_SLAVE_ERR | \
136 XILINX_DMA_DMASR_DMA_INT_ERR)
139 * Recoverable errors are DMA Internal error, SOF Early, EOF Early
140 * and SOF Late. They are only recoverable when C_FLUSH_ON_FSYNC
141 * is enabled in the h/w system.
143 #define XILINX_DMA_DMASR_ERR_RECOVER_MASK \
144 (XILINX_DMA_DMASR_SOF_LATE_ERR | \
145 XILINX_DMA_DMASR_EOF_EARLY_ERR | \
146 XILINX_DMA_DMASR_SOF_EARLY_ERR | \
147 XILINX_DMA_DMASR_DMA_INT_ERR)
149 /* Axi VDMA Flush on Fsync bits */
150 #define XILINX_DMA_FLUSH_S2MM 3
151 #define XILINX_DMA_FLUSH_MM2S 2
152 #define XILINX_DMA_FLUSH_BOTH 1
154 /* Delay loop counter to prevent hardware failure */
155 #define XILINX_DMA_LOOP_COUNT 1000000
157 /* AXI DMA Specific Registers/Offsets */
158 #define XILINX_DMA_REG_SRCDSTADDR 0x18
159 #define XILINX_DMA_REG_BTT 0x28
161 /* AXI DMA Specific Masks/Bit fields */
162 #define XILINX_DMA_MAX_TRANS_LEN GENMASK(22, 0)
163 #define XILINX_DMA_CR_COALESCE_MAX GENMASK(23, 16)
164 #define XILINX_DMA_CR_CYCLIC_BD_EN_MASK BIT(4)
165 #define XILINX_DMA_CR_COALESCE_SHIFT 16
166 #define XILINX_DMA_BD_SOP BIT(27)
167 #define XILINX_DMA_BD_EOP BIT(26)
168 #define XILINX_DMA_COALESCE_MAX 255
169 #define XILINX_DMA_NUM_APP_WORDS 5
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)
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
183 /* AXI CDMA Specific Registers/Offsets */
184 #define XILINX_CDMA_REG_SRCADDR 0x18
185 #define XILINX_CDMA_REG_DSTADDR 0x20
187 /* AXI CDMA Specific Masks */
188 #define XILINX_CDMA_CR_SGMODE BIT(3)
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
201 struct xilinx_vdma_desc_hw
{
212 * struct xilinx_axidma_desc_hw - Hardware Descriptor for AXI DMA
213 * @next_desc: Next Descriptor Pointer @0x00
214 * @next_desc_msb: MSB of Next Descriptor Pointer @0x04
215 * @buf_addr: Buffer address @0x08
216 * @buf_addr_msb: MSB of Buffer address @0x0C
217 * @pad1: Reserved @0x10
218 * @pad2: Reserved @0x14
219 * @control: Control field @0x18
220 * @status: Status field @0x1C
221 * @app: APP Fields @0x20 - 0x30
223 struct xilinx_axidma_desc_hw
{
232 u32 app
[XILINX_DMA_NUM_APP_WORDS
];
236 * struct xilinx_cdma_desc_hw - Hardware Descriptor
237 * @next_desc: Next Descriptor Pointer @0x00
238 * @next_descmsb: Next Descriptor Pointer MSB @0x04
239 * @src_addr: Source address @0x08
240 * @src_addrmsb: Source address MSB @0x0C
241 * @dest_addr: Destination address @0x10
242 * @dest_addrmsb: Destination address MSB @0x14
243 * @control: Control field @0x18
244 * @status: Status field @0x1C
246 struct xilinx_cdma_desc_hw
{
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
263 struct xilinx_vdma_tx_segment
{
264 struct xilinx_vdma_desc_hw hw
;
265 struct list_head node
;
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
275 struct xilinx_axidma_tx_segment
{
276 struct xilinx_axidma_desc_hw hw
;
277 struct list_head node
;
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
287 struct xilinx_cdma_tx_segment
{
288 struct xilinx_cdma_desc_hw hw
;
289 struct list_head node
;
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.
300 struct xilinx_dma_tx_descriptor
{
301 struct dma_async_tx_descriptor async_tx
;
302 struct list_head segments
;
303 struct list_head node
;
308 * struct xilinx_dma_chan - Driver specific DMA channel structure
309 * @xdev: Driver specific device structure
310 * @ctrl_offset: Control registers offset
311 * @desc_offset: TX descriptor registers offset
312 * @lock: Descriptor operation lock
313 * @pending_list: Descriptors waiting
314 * @active_list: Descriptors ready to submit
315 * @done_list: Complete descriptors
316 * @common: DMA common channel
317 * @desc_pool: Descriptors pool
318 * @dev: The dma device
321 * @direction: Transfer direction
322 * @num_frms: Number of frames
323 * @has_sg: Support scatter transfers
324 * @cyclic: Check for cyclic transfers.
325 * @genlock: Support genlock mode
326 * @err: Channel has errors
327 * @tasklet: Cleanup work after irq
328 * @config: Device configuration info
329 * @flush_on_fsync: Flush on Frame sync
330 * @desc_pendingcount: Descriptor pending count
331 * @ext_addr: Indicates 64 bit addressing is supported by dma channel
332 * @desc_submitcount: Descriptor h/w submitted count
333 * @residue: Residue for AXI DMA
334 * @seg_v: Statically allocated segments base
335 * @cyclic_seg_v: Statically allocated segment base for cyclic transfers
336 * @start_transfer: Differentiate b/w DMA IP's transfer
338 struct xilinx_dma_chan
{
339 struct xilinx_dma_device
*xdev
;
343 struct list_head pending_list
;
344 struct list_head active_list
;
345 struct list_head done_list
;
346 struct dma_chan common
;
347 struct dma_pool
*desc_pool
;
351 enum dma_transfer_direction direction
;
357 struct tasklet_struct tasklet
;
358 struct xilinx_vdma_config config
;
360 u32 desc_pendingcount
;
362 u32 desc_submitcount
;
364 struct xilinx_axidma_tx_segment
*seg_v
;
365 struct xilinx_axidma_tx_segment
*cyclic_seg_v
;
366 void (*start_transfer
)(struct xilinx_dma_chan
*chan
);
370 struct xilinx_dma_config
{
371 enum xdma_ip_type dmatype
;
372 int (*clk_init
)(struct platform_device
*pdev
, struct clk
**axi_clk
,
373 struct clk
**tx_clk
, struct clk
**txs_clk
,
374 struct clk
**rx_clk
, struct clk
**rxs_clk
);
378 * struct xilinx_dma_device - DMA device structure
379 * @regs: I/O mapped base address
380 * @dev: Device Structure
381 * @common: DMA device structure
382 * @chan: Driver specific DMA channel
383 * @has_sg: Specifies whether Scatter-Gather is present or not
384 * @mcdma: Specifies whether Multi-Channel is present or not
385 * @flush_on_fsync: Flush on frame sync
386 * @ext_addr: Indicates 64 bit addressing is supported by dma device
387 * @pdev: Platform device structure pointer
388 * @dma_config: DMA config structure
389 * @axi_clk: DMA Axi4-lite interace clock
390 * @tx_clk: DMA mm2s clock
391 * @txs_clk: DMA mm2s stream clock
392 * @rx_clk: DMA s2mm clock
393 * @rxs_clk: DMA s2mm stream clock
394 * @nr_channels: Number of channels DMA device supports
395 * @chan_id: DMA channel identifier
397 struct xilinx_dma_device
{
400 struct dma_device common
;
401 struct xilinx_dma_chan
*chan
[XILINX_DMA_MAX_CHANS_PER_DEVICE
];
406 struct platform_device
*pdev
;
407 const struct xilinx_dma_config
*dma_config
;
418 #define to_xilinx_chan(chan) \
419 container_of(chan, struct xilinx_dma_chan, common)
420 #define to_dma_tx_descriptor(tx) \
421 container_of(tx, struct xilinx_dma_tx_descriptor, async_tx)
422 #define xilinx_dma_poll_timeout(chan, reg, val, cond, delay_us, timeout_us) \
423 readl_poll_timeout(chan->xdev->regs + chan->ctrl_offset + reg, val, \
424 cond, delay_us, timeout_us)
427 static inline u32
dma_read(struct xilinx_dma_chan
*chan
, u32 reg
)
429 return ioread32(chan
->xdev
->regs
+ reg
);
432 static inline void dma_write(struct xilinx_dma_chan
*chan
, u32 reg
, u32 value
)
434 iowrite32(value
, chan
->xdev
->regs
+ reg
);
437 static inline void vdma_desc_write(struct xilinx_dma_chan
*chan
, u32 reg
,
440 dma_write(chan
, chan
->desc_offset
+ reg
, value
);
443 static inline u32
dma_ctrl_read(struct xilinx_dma_chan
*chan
, u32 reg
)
445 return dma_read(chan
, chan
->ctrl_offset
+ reg
);
448 static inline void dma_ctrl_write(struct xilinx_dma_chan
*chan
, u32 reg
,
451 dma_write(chan
, chan
->ctrl_offset
+ reg
, value
);
454 static inline void dma_ctrl_clr(struct xilinx_dma_chan
*chan
, u32 reg
,
457 dma_ctrl_write(chan
, reg
, dma_ctrl_read(chan
, reg
) & ~clr
);
460 static inline void dma_ctrl_set(struct xilinx_dma_chan
*chan
, u32 reg
,
463 dma_ctrl_write(chan
, reg
, dma_ctrl_read(chan
, reg
) | set
);
467 * vdma_desc_write_64 - 64-bit descriptor write
468 * @chan: Driver specific VDMA channel
469 * @reg: Register to write
470 * @value_lsb: lower address of the descriptor.
471 * @value_msb: upper address of the descriptor.
473 * Since vdma driver is trying to write to a register offset which is not a
474 * multiple of 64 bits(ex : 0x5c), we are writing as two separate 32 bits
475 * instead of a single 64 bit register write.
477 static inline void vdma_desc_write_64(struct xilinx_dma_chan
*chan
, u32 reg
,
478 u32 value_lsb
, u32 value_msb
)
480 /* Write the lsb 32 bits*/
481 writel(value_lsb
, chan
->xdev
->regs
+ chan
->desc_offset
+ reg
);
483 /* Write the msb 32 bits */
484 writel(value_msb
, chan
->xdev
->regs
+ chan
->desc_offset
+ reg
+ 4);
487 static inline void dma_writeq(struct xilinx_dma_chan
*chan
, u32 reg
, u64 value
)
489 lo_hi_writeq(value
, chan
->xdev
->regs
+ chan
->ctrl_offset
+ reg
);
492 static inline void xilinx_write(struct xilinx_dma_chan
*chan
, u32 reg
,
496 dma_writeq(chan
, reg
, addr
);
498 dma_ctrl_write(chan
, reg
, addr
);
501 static inline void xilinx_axidma_buf(struct xilinx_dma_chan
*chan
,
502 struct xilinx_axidma_desc_hw
*hw
,
503 dma_addr_t buf_addr
, size_t sg_used
,
506 if (chan
->ext_addr
) {
507 hw
->buf_addr
= lower_32_bits(buf_addr
+ sg_used
+ period_len
);
508 hw
->buf_addr_msb
= upper_32_bits(buf_addr
+ sg_used
+
511 hw
->buf_addr
= buf_addr
+ sg_used
+ period_len
;
515 /* -----------------------------------------------------------------------------
516 * Descriptors and segments alloc and free
520 * xilinx_vdma_alloc_tx_segment - Allocate transaction segment
521 * @chan: Driver specific DMA channel
523 * Return: The allocated segment on success and NULL on failure.
525 static struct xilinx_vdma_tx_segment
*
526 xilinx_vdma_alloc_tx_segment(struct xilinx_dma_chan
*chan
)
528 struct xilinx_vdma_tx_segment
*segment
;
531 segment
= dma_pool_zalloc(chan
->desc_pool
, GFP_ATOMIC
, &phys
);
535 segment
->phys
= phys
;
541 * xilinx_cdma_alloc_tx_segment - Allocate transaction segment
542 * @chan: Driver specific DMA channel
544 * Return: The allocated segment on success and NULL on failure.
546 static struct xilinx_cdma_tx_segment
*
547 xilinx_cdma_alloc_tx_segment(struct xilinx_dma_chan
*chan
)
549 struct xilinx_cdma_tx_segment
*segment
;
552 segment
= dma_pool_zalloc(chan
->desc_pool
, GFP_ATOMIC
, &phys
);
556 segment
->phys
= phys
;
562 * xilinx_axidma_alloc_tx_segment - Allocate transaction segment
563 * @chan: Driver specific DMA channel
565 * Return: The allocated segment on success and NULL on failure.
567 static struct xilinx_axidma_tx_segment
*
568 xilinx_axidma_alloc_tx_segment(struct xilinx_dma_chan
*chan
)
570 struct xilinx_axidma_tx_segment
*segment
;
573 segment
= dma_pool_zalloc(chan
->desc_pool
, GFP_ATOMIC
, &phys
);
577 segment
->phys
= phys
;
583 * xilinx_dma_free_tx_segment - Free transaction segment
584 * @chan: Driver specific DMA channel
585 * @segment: DMA transaction segment
587 static void xilinx_dma_free_tx_segment(struct xilinx_dma_chan
*chan
,
588 struct xilinx_axidma_tx_segment
*segment
)
590 dma_pool_free(chan
->desc_pool
, segment
, segment
->phys
);
594 * xilinx_cdma_free_tx_segment - Free transaction segment
595 * @chan: Driver specific DMA channel
596 * @segment: DMA transaction segment
598 static void xilinx_cdma_free_tx_segment(struct xilinx_dma_chan
*chan
,
599 struct xilinx_cdma_tx_segment
*segment
)
601 dma_pool_free(chan
->desc_pool
, segment
, segment
->phys
);
605 * xilinx_vdma_free_tx_segment - Free transaction segment
606 * @chan: Driver specific DMA channel
607 * @segment: DMA transaction segment
609 static void xilinx_vdma_free_tx_segment(struct xilinx_dma_chan
*chan
,
610 struct xilinx_vdma_tx_segment
*segment
)
612 dma_pool_free(chan
->desc_pool
, segment
, segment
->phys
);
616 * xilinx_dma_tx_descriptor - Allocate transaction descriptor
617 * @chan: Driver specific DMA channel
619 * Return: The allocated descriptor on success and NULL on failure.
621 static struct xilinx_dma_tx_descriptor
*
622 xilinx_dma_alloc_tx_descriptor(struct xilinx_dma_chan
*chan
)
624 struct xilinx_dma_tx_descriptor
*desc
;
626 desc
= kzalloc(sizeof(*desc
), GFP_KERNEL
);
630 INIT_LIST_HEAD(&desc
->segments
);
636 * xilinx_dma_free_tx_descriptor - Free transaction descriptor
637 * @chan: Driver specific DMA channel
638 * @desc: DMA transaction descriptor
641 xilinx_dma_free_tx_descriptor(struct xilinx_dma_chan
*chan
,
642 struct xilinx_dma_tx_descriptor
*desc
)
644 struct xilinx_vdma_tx_segment
*segment
, *next
;
645 struct xilinx_cdma_tx_segment
*cdma_segment
, *cdma_next
;
646 struct xilinx_axidma_tx_segment
*axidma_segment
, *axidma_next
;
651 if (chan
->xdev
->dma_config
->dmatype
== XDMA_TYPE_VDMA
) {
652 list_for_each_entry_safe(segment
, next
, &desc
->segments
, node
) {
653 list_del(&segment
->node
);
654 xilinx_vdma_free_tx_segment(chan
, segment
);
656 } else if (chan
->xdev
->dma_config
->dmatype
== XDMA_TYPE_CDMA
) {
657 list_for_each_entry_safe(cdma_segment
, cdma_next
,
658 &desc
->segments
, node
) {
659 list_del(&cdma_segment
->node
);
660 xilinx_cdma_free_tx_segment(chan
, cdma_segment
);
663 list_for_each_entry_safe(axidma_segment
, axidma_next
,
664 &desc
->segments
, node
) {
665 list_del(&axidma_segment
->node
);
666 xilinx_dma_free_tx_segment(chan
, axidma_segment
);
673 /* Required functions */
676 * xilinx_dma_free_desc_list - Free descriptors list
677 * @chan: Driver specific DMA channel
678 * @list: List to parse and delete the descriptor
680 static void xilinx_dma_free_desc_list(struct xilinx_dma_chan
*chan
,
681 struct list_head
*list
)
683 struct xilinx_dma_tx_descriptor
*desc
, *next
;
685 list_for_each_entry_safe(desc
, next
, list
, node
) {
686 list_del(&desc
->node
);
687 xilinx_dma_free_tx_descriptor(chan
, desc
);
692 * xilinx_dma_free_descriptors - Free channel descriptors
693 * @chan: Driver specific DMA channel
695 static void xilinx_dma_free_descriptors(struct xilinx_dma_chan
*chan
)
699 spin_lock_irqsave(&chan
->lock
, flags
);
701 xilinx_dma_free_desc_list(chan
, &chan
->pending_list
);
702 xilinx_dma_free_desc_list(chan
, &chan
->done_list
);
703 xilinx_dma_free_desc_list(chan
, &chan
->active_list
);
705 spin_unlock_irqrestore(&chan
->lock
, flags
);
709 * xilinx_dma_free_chan_resources - Free channel resources
710 * @dchan: DMA channel
712 static void xilinx_dma_free_chan_resources(struct dma_chan
*dchan
)
714 struct xilinx_dma_chan
*chan
= to_xilinx_chan(dchan
);
716 dev_dbg(chan
->dev
, "Free all channel resources.\n");
718 xilinx_dma_free_descriptors(chan
);
719 if (chan
->xdev
->dma_config
->dmatype
== XDMA_TYPE_AXIDMA
) {
720 xilinx_dma_free_tx_segment(chan
, chan
->cyclic_seg_v
);
721 xilinx_dma_free_tx_segment(chan
, chan
->seg_v
);
723 dma_pool_destroy(chan
->desc_pool
);
724 chan
->desc_pool
= NULL
;
728 * xilinx_dma_chan_handle_cyclic - Cyclic dma callback
729 * @chan: Driver specific dma channel
730 * @desc: dma transaction descriptor
731 * @flags: flags for spin lock
733 static void xilinx_dma_chan_handle_cyclic(struct xilinx_dma_chan
*chan
,
734 struct xilinx_dma_tx_descriptor
*desc
,
735 unsigned long *flags
)
737 dma_async_tx_callback callback
;
738 void *callback_param
;
740 callback
= desc
->async_tx
.callback
;
741 callback_param
= desc
->async_tx
.callback_param
;
743 spin_unlock_irqrestore(&chan
->lock
, *flags
);
744 callback(callback_param
);
745 spin_lock_irqsave(&chan
->lock
, *flags
);
750 * xilinx_dma_chan_desc_cleanup - Clean channel descriptors
751 * @chan: Driver specific DMA channel
753 static void xilinx_dma_chan_desc_cleanup(struct xilinx_dma_chan
*chan
)
755 struct xilinx_dma_tx_descriptor
*desc
, *next
;
758 spin_lock_irqsave(&chan
->lock
, flags
);
760 list_for_each_entry_safe(desc
, next
, &chan
->done_list
, node
) {
761 struct dmaengine_desc_callback cb
;
764 xilinx_dma_chan_handle_cyclic(chan
, desc
, &flags
);
768 /* Remove from the list of running transactions */
769 list_del(&desc
->node
);
771 /* Run the link descriptor callback function */
772 dmaengine_desc_get_callback(&desc
->async_tx
, &cb
);
773 if (dmaengine_desc_callback_valid(&cb
)) {
774 spin_unlock_irqrestore(&chan
->lock
, flags
);
775 dmaengine_desc_callback_invoke(&cb
, NULL
);
776 spin_lock_irqsave(&chan
->lock
, flags
);
779 /* Run any dependencies, then free the descriptor */
780 dma_run_dependencies(&desc
->async_tx
);
781 xilinx_dma_free_tx_descriptor(chan
, desc
);
784 spin_unlock_irqrestore(&chan
->lock
, flags
);
788 * xilinx_dma_do_tasklet - Schedule completion tasklet
789 * @data: Pointer to the Xilinx DMA channel structure
791 static void xilinx_dma_do_tasklet(unsigned long data
)
793 struct xilinx_dma_chan
*chan
= (struct xilinx_dma_chan
*)data
;
795 xilinx_dma_chan_desc_cleanup(chan
);
799 * xilinx_dma_alloc_chan_resources - Allocate channel resources
800 * @dchan: DMA channel
802 * Return: '0' on success and failure value on error
804 static int xilinx_dma_alloc_chan_resources(struct dma_chan
*dchan
)
806 struct xilinx_dma_chan
*chan
= to_xilinx_chan(dchan
);
808 /* Has this channel already been allocated? */
813 * We need the descriptor to be aligned to 64bytes
814 * for meeting Xilinx VDMA specification requirement.
816 if (chan
->xdev
->dma_config
->dmatype
== XDMA_TYPE_AXIDMA
) {
817 chan
->desc_pool
= dma_pool_create("xilinx_dma_desc_pool",
819 sizeof(struct xilinx_axidma_tx_segment
),
820 __alignof__(struct xilinx_axidma_tx_segment
),
822 } else if (chan
->xdev
->dma_config
->dmatype
== XDMA_TYPE_CDMA
) {
823 chan
->desc_pool
= dma_pool_create("xilinx_cdma_desc_pool",
825 sizeof(struct xilinx_cdma_tx_segment
),
826 __alignof__(struct xilinx_cdma_tx_segment
),
829 chan
->desc_pool
= dma_pool_create("xilinx_vdma_desc_pool",
831 sizeof(struct xilinx_vdma_tx_segment
),
832 __alignof__(struct xilinx_vdma_tx_segment
),
836 if (!chan
->desc_pool
) {
838 "unable to allocate channel %d descriptor pool\n",
843 if (chan
->xdev
->dma_config
->dmatype
== XDMA_TYPE_AXIDMA
) {
845 * For AXI DMA case after submitting a pending_list, keep
846 * an extra segment allocated so that the "next descriptor"
847 * pointer on the tail descriptor always points to a
848 * valid descriptor, even when paused after reaching taildesc.
849 * This way, it is possible to issue additional
850 * transfers without halting and restarting the channel.
852 chan
->seg_v
= xilinx_axidma_alloc_tx_segment(chan
);
855 * For cyclic DMA mode we need to program the tail Descriptor
856 * register with a value which is not a part of the BD chain
857 * so allocating a desc segment during channel allocation for
858 * programming tail descriptor.
860 chan
->cyclic_seg_v
= xilinx_axidma_alloc_tx_segment(chan
);
863 dma_cookie_init(dchan
);
865 if (chan
->xdev
->dma_config
->dmatype
== XDMA_TYPE_AXIDMA
) {
866 /* For AXI DMA resetting once channel will reset the
867 * other channel as well so enable the interrupts here.
869 dma_ctrl_set(chan
, XILINX_DMA_REG_DMACR
,
870 XILINX_DMA_DMAXR_ALL_IRQ_MASK
);
873 if ((chan
->xdev
->dma_config
->dmatype
== XDMA_TYPE_CDMA
) && chan
->has_sg
)
874 dma_ctrl_set(chan
, XILINX_DMA_REG_DMACR
,
875 XILINX_CDMA_CR_SGMODE
);
881 * xilinx_dma_tx_status - Get DMA transaction status
882 * @dchan: DMA channel
883 * @cookie: Transaction identifier
884 * @txstate: Transaction state
886 * Return: DMA transaction status
888 static enum dma_status
xilinx_dma_tx_status(struct dma_chan
*dchan
,
890 struct dma_tx_state
*txstate
)
892 struct xilinx_dma_chan
*chan
= to_xilinx_chan(dchan
);
893 struct xilinx_dma_tx_descriptor
*desc
;
894 struct xilinx_axidma_tx_segment
*segment
;
895 struct xilinx_axidma_desc_hw
*hw
;
900 ret
= dma_cookie_status(dchan
, cookie
, txstate
);
901 if (ret
== DMA_COMPLETE
|| !txstate
)
904 if (chan
->xdev
->dma_config
->dmatype
== XDMA_TYPE_AXIDMA
) {
905 spin_lock_irqsave(&chan
->lock
, flags
);
907 desc
= list_last_entry(&chan
->active_list
,
908 struct xilinx_dma_tx_descriptor
, node
);
910 list_for_each_entry(segment
, &desc
->segments
, node
) {
912 residue
+= (hw
->control
- hw
->status
) &
913 XILINX_DMA_MAX_TRANS_LEN
;
916 spin_unlock_irqrestore(&chan
->lock
, flags
);
918 chan
->residue
= residue
;
919 dma_set_residue(txstate
, chan
->residue
);
926 * xilinx_dma_is_running - Check if DMA channel is running
927 * @chan: Driver specific DMA channel
929 * Return: '1' if running, '0' if not.
931 static bool xilinx_dma_is_running(struct xilinx_dma_chan
*chan
)
933 return !(dma_ctrl_read(chan
, XILINX_DMA_REG_DMASR
) &
934 XILINX_DMA_DMASR_HALTED
) &&
935 (dma_ctrl_read(chan
, XILINX_DMA_REG_DMACR
) &
936 XILINX_DMA_DMACR_RUNSTOP
);
940 * xilinx_dma_is_idle - Check if DMA channel is idle
941 * @chan: Driver specific DMA channel
943 * Return: '1' if idle, '0' if not.
945 static bool xilinx_dma_is_idle(struct xilinx_dma_chan
*chan
)
947 return dma_ctrl_read(chan
, XILINX_DMA_REG_DMASR
) &
948 XILINX_DMA_DMASR_IDLE
;
952 * xilinx_dma_halt - Halt DMA channel
953 * @chan: Driver specific DMA channel
955 static void xilinx_dma_halt(struct xilinx_dma_chan
*chan
)
960 dma_ctrl_clr(chan
, XILINX_DMA_REG_DMACR
, XILINX_DMA_DMACR_RUNSTOP
);
962 /* Wait for the hardware to halt */
963 err
= xilinx_dma_poll_timeout(chan
, XILINX_DMA_REG_DMASR
, val
,
964 (val
& XILINX_DMA_DMASR_HALTED
), 0,
965 XILINX_DMA_LOOP_COUNT
);
968 dev_err(chan
->dev
, "Cannot stop channel %p: %x\n",
969 chan
, dma_ctrl_read(chan
, XILINX_DMA_REG_DMASR
));
975 * xilinx_dma_start - Start DMA channel
976 * @chan: Driver specific DMA channel
978 static void xilinx_dma_start(struct xilinx_dma_chan
*chan
)
983 dma_ctrl_set(chan
, XILINX_DMA_REG_DMACR
, XILINX_DMA_DMACR_RUNSTOP
);
985 /* Wait for the hardware to start */
986 err
= xilinx_dma_poll_timeout(chan
, XILINX_DMA_REG_DMASR
, val
,
987 !(val
& XILINX_DMA_DMASR_HALTED
), 0,
988 XILINX_DMA_LOOP_COUNT
);
991 dev_err(chan
->dev
, "Cannot start channel %p: %x\n",
992 chan
, dma_ctrl_read(chan
, XILINX_DMA_REG_DMASR
));
999 * xilinx_vdma_start_transfer - Starts VDMA transfer
1000 * @chan: Driver specific channel struct pointer
1002 static void xilinx_vdma_start_transfer(struct xilinx_dma_chan
*chan
)
1004 struct xilinx_vdma_config
*config
= &chan
->config
;
1005 struct xilinx_dma_tx_descriptor
*desc
, *tail_desc
;
1007 struct xilinx_vdma_tx_segment
*tail_segment
;
1009 /* This function was invoked with lock held */
1013 if (list_empty(&chan
->pending_list
))
1016 desc
= list_first_entry(&chan
->pending_list
,
1017 struct xilinx_dma_tx_descriptor
, node
);
1018 tail_desc
= list_last_entry(&chan
->pending_list
,
1019 struct xilinx_dma_tx_descriptor
, node
);
1021 tail_segment
= list_last_entry(&tail_desc
->segments
,
1022 struct xilinx_vdma_tx_segment
, node
);
1024 /* If it is SG mode and hardware is busy, cannot submit */
1025 if (chan
->has_sg
&& xilinx_dma_is_running(chan
) &&
1026 !xilinx_dma_is_idle(chan
)) {
1027 dev_dbg(chan
->dev
, "DMA controller still busy\n");
1032 * If hardware is idle, then all descriptors on the running lists are
1033 * done, start new transfers
1036 dma_ctrl_write(chan
, XILINX_DMA_REG_CURDESC
,
1037 desc
->async_tx
.phys
);
1039 /* Configure the hardware using info in the config structure */
1040 reg
= dma_ctrl_read(chan
, XILINX_DMA_REG_DMACR
);
1042 if (config
->frm_cnt_en
)
1043 reg
|= XILINX_DMA_DMACR_FRAMECNT_EN
;
1045 reg
&= ~XILINX_DMA_DMACR_FRAMECNT_EN
;
1047 /* Configure channel to allow number frame buffers */
1048 dma_ctrl_write(chan
, XILINX_DMA_REG_FRMSTORE
,
1049 chan
->desc_pendingcount
);
1052 * With SG, start with circular mode, so that BDs can be fetched.
1053 * In direct register mode, if not parking, enable circular mode
1055 if (chan
->has_sg
|| !config
->park
)
1056 reg
|= XILINX_DMA_DMACR_CIRC_EN
;
1059 reg
&= ~XILINX_DMA_DMACR_CIRC_EN
;
1061 dma_ctrl_write(chan
, XILINX_DMA_REG_DMACR
, reg
);
1063 if (config
->park
&& (config
->park_frm
>= 0) &&
1064 (config
->park_frm
< chan
->num_frms
)) {
1065 if (chan
->direction
== DMA_MEM_TO_DEV
)
1066 dma_write(chan
, XILINX_DMA_REG_PARK_PTR
,
1068 XILINX_DMA_PARK_PTR_RD_REF_SHIFT
);
1070 dma_write(chan
, XILINX_DMA_REG_PARK_PTR
,
1072 XILINX_DMA_PARK_PTR_WR_REF_SHIFT
);
1075 /* Start the hardware */
1076 xilinx_dma_start(chan
);
1081 /* Start the transfer */
1083 dma_ctrl_write(chan
, XILINX_DMA_REG_TAILDESC
,
1084 tail_segment
->phys
);
1086 struct xilinx_vdma_tx_segment
*segment
, *last
= NULL
;
1089 if (chan
->desc_submitcount
< chan
->num_frms
)
1090 i
= chan
->desc_submitcount
;
1092 list_for_each_entry(segment
, &desc
->segments
, node
) {
1094 vdma_desc_write_64(chan
,
1095 XILINX_VDMA_REG_START_ADDRESS_64(i
++),
1096 segment
->hw
.buf_addr
,
1097 segment
->hw
.buf_addr_msb
);
1099 vdma_desc_write(chan
,
1100 XILINX_VDMA_REG_START_ADDRESS(i
++),
1101 segment
->hw
.buf_addr
);
1109 /* HW expects these parameters to be same for one transaction */
1110 vdma_desc_write(chan
, XILINX_DMA_REG_HSIZE
, last
->hw
.hsize
);
1111 vdma_desc_write(chan
, XILINX_DMA_REG_FRMDLY_STRIDE
,
1113 vdma_desc_write(chan
, XILINX_DMA_REG_VSIZE
, last
->hw
.vsize
);
1116 if (!chan
->has_sg
) {
1117 list_del(&desc
->node
);
1118 list_add_tail(&desc
->node
, &chan
->active_list
);
1119 chan
->desc_submitcount
++;
1120 chan
->desc_pendingcount
--;
1121 if (chan
->desc_submitcount
== chan
->num_frms
)
1122 chan
->desc_submitcount
= 0;
1124 list_splice_tail_init(&chan
->pending_list
, &chan
->active_list
);
1125 chan
->desc_pendingcount
= 0;
1130 * xilinx_cdma_start_transfer - Starts cdma transfer
1131 * @chan: Driver specific channel struct pointer
1133 static void xilinx_cdma_start_transfer(struct xilinx_dma_chan
*chan
)
1135 struct xilinx_dma_tx_descriptor
*head_desc
, *tail_desc
;
1136 struct xilinx_cdma_tx_segment
*tail_segment
;
1137 u32 ctrl_reg
= dma_read(chan
, XILINX_DMA_REG_DMACR
);
1142 if (list_empty(&chan
->pending_list
))
1145 head_desc
= list_first_entry(&chan
->pending_list
,
1146 struct xilinx_dma_tx_descriptor
, node
);
1147 tail_desc
= list_last_entry(&chan
->pending_list
,
1148 struct xilinx_dma_tx_descriptor
, node
);
1149 tail_segment
= list_last_entry(&tail_desc
->segments
,
1150 struct xilinx_cdma_tx_segment
, node
);
1152 if (chan
->desc_pendingcount
<= XILINX_DMA_COALESCE_MAX
) {
1153 ctrl_reg
&= ~XILINX_DMA_CR_COALESCE_MAX
;
1154 ctrl_reg
|= chan
->desc_pendingcount
<<
1155 XILINX_DMA_CR_COALESCE_SHIFT
;
1156 dma_ctrl_write(chan
, XILINX_DMA_REG_DMACR
, ctrl_reg
);
1160 xilinx_write(chan
, XILINX_DMA_REG_CURDESC
,
1161 head_desc
->async_tx
.phys
);
1163 /* Update tail ptr register which will start the transfer */
1164 xilinx_write(chan
, XILINX_DMA_REG_TAILDESC
,
1165 tail_segment
->phys
);
1167 /* In simple mode */
1168 struct xilinx_cdma_tx_segment
*segment
;
1169 struct xilinx_cdma_desc_hw
*hw
;
1171 segment
= list_first_entry(&head_desc
->segments
,
1172 struct xilinx_cdma_tx_segment
,
1177 xilinx_write(chan
, XILINX_CDMA_REG_SRCADDR
, hw
->src_addr
);
1178 xilinx_write(chan
, XILINX_CDMA_REG_DSTADDR
, hw
->dest_addr
);
1180 /* Start the transfer */
1181 dma_ctrl_write(chan
, XILINX_DMA_REG_BTT
,
1182 hw
->control
& XILINX_DMA_MAX_TRANS_LEN
);
1185 list_splice_tail_init(&chan
->pending_list
, &chan
->active_list
);
1186 chan
->desc_pendingcount
= 0;
1190 * xilinx_dma_start_transfer - Starts DMA transfer
1191 * @chan: Driver specific channel struct pointer
1193 static void xilinx_dma_start_transfer(struct xilinx_dma_chan
*chan
)
1195 struct xilinx_dma_tx_descriptor
*head_desc
, *tail_desc
;
1196 struct xilinx_axidma_tx_segment
*tail_segment
, *old_head
, *new_head
;
1202 if (list_empty(&chan
->pending_list
))
1205 /* If it is SG mode and hardware is busy, cannot submit */
1206 if (chan
->has_sg
&& xilinx_dma_is_running(chan
) &&
1207 !xilinx_dma_is_idle(chan
)) {
1208 dev_dbg(chan
->dev
, "DMA controller still busy\n");
1212 head_desc
= list_first_entry(&chan
->pending_list
,
1213 struct xilinx_dma_tx_descriptor
, node
);
1214 tail_desc
= list_last_entry(&chan
->pending_list
,
1215 struct xilinx_dma_tx_descriptor
, node
);
1216 tail_segment
= list_last_entry(&tail_desc
->segments
,
1217 struct xilinx_axidma_tx_segment
, node
);
1219 if (chan
->has_sg
&& !chan
->xdev
->mcdma
) {
1220 old_head
= list_first_entry(&head_desc
->segments
,
1221 struct xilinx_axidma_tx_segment
, node
);
1222 new_head
= chan
->seg_v
;
1223 /* Copy Buffer Descriptor fields. */
1224 new_head
->hw
= old_head
->hw
;
1226 /* Swap and save new reserve */
1227 list_replace_init(&old_head
->node
, &new_head
->node
);
1228 chan
->seg_v
= old_head
;
1230 tail_segment
->hw
.next_desc
= chan
->seg_v
->phys
;
1231 head_desc
->async_tx
.phys
= new_head
->phys
;
1234 reg
= dma_ctrl_read(chan
, XILINX_DMA_REG_DMACR
);
1236 if (chan
->desc_pendingcount
<= XILINX_DMA_COALESCE_MAX
) {
1237 reg
&= ~XILINX_DMA_CR_COALESCE_MAX
;
1238 reg
|= chan
->desc_pendingcount
<<
1239 XILINX_DMA_CR_COALESCE_SHIFT
;
1240 dma_ctrl_write(chan
, XILINX_DMA_REG_DMACR
, reg
);
1243 if (chan
->has_sg
&& !chan
->xdev
->mcdma
)
1244 xilinx_write(chan
, XILINX_DMA_REG_CURDESC
,
1245 head_desc
->async_tx
.phys
);
1247 if (chan
->has_sg
&& chan
->xdev
->mcdma
) {
1248 if (chan
->direction
== DMA_MEM_TO_DEV
) {
1249 dma_ctrl_write(chan
, XILINX_DMA_REG_CURDESC
,
1250 head_desc
->async_tx
.phys
);
1253 dma_ctrl_write(chan
, XILINX_DMA_REG_CURDESC
,
1254 head_desc
->async_tx
.phys
);
1256 dma_ctrl_write(chan
,
1257 XILINX_DMA_MCRX_CDESC(chan
->tdest
),
1258 head_desc
->async_tx
.phys
);
1263 xilinx_dma_start(chan
);
1268 /* Start the transfer */
1269 if (chan
->has_sg
&& !chan
->xdev
->mcdma
) {
1271 xilinx_write(chan
, XILINX_DMA_REG_TAILDESC
,
1272 chan
->cyclic_seg_v
->phys
);
1274 xilinx_write(chan
, XILINX_DMA_REG_TAILDESC
,
1275 tail_segment
->phys
);
1276 } else if (chan
->has_sg
&& chan
->xdev
->mcdma
) {
1277 if (chan
->direction
== DMA_MEM_TO_DEV
) {
1278 dma_ctrl_write(chan
, XILINX_DMA_REG_TAILDESC
,
1279 tail_segment
->phys
);
1282 dma_ctrl_write(chan
, XILINX_DMA_REG_TAILDESC
,
1283 tail_segment
->phys
);
1285 dma_ctrl_write(chan
,
1286 XILINX_DMA_MCRX_TDESC(chan
->tdest
),
1287 tail_segment
->phys
);
1291 struct xilinx_axidma_tx_segment
*segment
;
1292 struct xilinx_axidma_desc_hw
*hw
;
1294 segment
= list_first_entry(&head_desc
->segments
,
1295 struct xilinx_axidma_tx_segment
,
1299 xilinx_write(chan
, XILINX_DMA_REG_SRCDSTADDR
, hw
->buf_addr
);
1301 /* Start the transfer */
1302 dma_ctrl_write(chan
, XILINX_DMA_REG_BTT
,
1303 hw
->control
& XILINX_DMA_MAX_TRANS_LEN
);
1306 list_splice_tail_init(&chan
->pending_list
, &chan
->active_list
);
1307 chan
->desc_pendingcount
= 0;
1311 * xilinx_dma_issue_pending - Issue pending transactions
1312 * @dchan: DMA channel
1314 static void xilinx_dma_issue_pending(struct dma_chan
*dchan
)
1316 struct xilinx_dma_chan
*chan
= to_xilinx_chan(dchan
);
1317 unsigned long flags
;
1319 spin_lock_irqsave(&chan
->lock
, flags
);
1320 chan
->start_transfer(chan
);
1321 spin_unlock_irqrestore(&chan
->lock
, flags
);
1325 * xilinx_dma_complete_descriptor - Mark the active descriptor as complete
1326 * @chan : xilinx DMA channel
1330 static void xilinx_dma_complete_descriptor(struct xilinx_dma_chan
*chan
)
1332 struct xilinx_dma_tx_descriptor
*desc
, *next
;
1334 /* This function was invoked with lock held */
1335 if (list_empty(&chan
->active_list
))
1338 list_for_each_entry_safe(desc
, next
, &chan
->active_list
, node
) {
1339 list_del(&desc
->node
);
1341 dma_cookie_complete(&desc
->async_tx
);
1342 list_add_tail(&desc
->node
, &chan
->done_list
);
1347 * xilinx_dma_reset - Reset DMA channel
1348 * @chan: Driver specific DMA channel
1350 * Return: '0' on success and failure value on error
1352 static int xilinx_dma_reset(struct xilinx_dma_chan
*chan
)
1357 dma_ctrl_set(chan
, XILINX_DMA_REG_DMACR
, XILINX_DMA_DMACR_RESET
);
1359 /* Wait for the hardware to finish reset */
1360 err
= xilinx_dma_poll_timeout(chan
, XILINX_DMA_REG_DMACR
, tmp
,
1361 !(tmp
& XILINX_DMA_DMACR_RESET
), 0,
1362 XILINX_DMA_LOOP_COUNT
);
1365 dev_err(chan
->dev
, "reset timeout, cr %x, sr %x\n",
1366 dma_ctrl_read(chan
, XILINX_DMA_REG_DMACR
),
1367 dma_ctrl_read(chan
, XILINX_DMA_REG_DMASR
));
1377 * xilinx_dma_chan_reset - Reset DMA channel and enable interrupts
1378 * @chan: Driver specific DMA channel
1380 * Return: '0' on success and failure value on error
1382 static int xilinx_dma_chan_reset(struct xilinx_dma_chan
*chan
)
1387 err
= xilinx_dma_reset(chan
);
1391 /* Enable interrupts */
1392 dma_ctrl_set(chan
, XILINX_DMA_REG_DMACR
,
1393 XILINX_DMA_DMAXR_ALL_IRQ_MASK
);
1399 * xilinx_dma_irq_handler - DMA Interrupt handler
1401 * @data: Pointer to the Xilinx DMA channel structure
1403 * Return: IRQ_HANDLED/IRQ_NONE
1405 static irqreturn_t
xilinx_dma_irq_handler(int irq
, void *data
)
1407 struct xilinx_dma_chan
*chan
= data
;
1410 /* Read the status and ack the interrupts. */
1411 status
= dma_ctrl_read(chan
, XILINX_DMA_REG_DMASR
);
1412 if (!(status
& XILINX_DMA_DMAXR_ALL_IRQ_MASK
))
1415 dma_ctrl_write(chan
, XILINX_DMA_REG_DMASR
,
1416 status
& XILINX_DMA_DMAXR_ALL_IRQ_MASK
);
1418 if (status
& XILINX_DMA_DMASR_ERR_IRQ
) {
1420 * An error occurred. If C_FLUSH_ON_FSYNC is enabled and the
1421 * error is recoverable, ignore it. Otherwise flag the error.
1423 * Only recoverable errors can be cleared in the DMASR register,
1424 * make sure not to write to other error bits to 1.
1426 u32 errors
= status
& XILINX_DMA_DMASR_ALL_ERR_MASK
;
1428 dma_ctrl_write(chan
, XILINX_DMA_REG_DMASR
,
1429 errors
& XILINX_DMA_DMASR_ERR_RECOVER_MASK
);
1431 if (!chan
->flush_on_fsync
||
1432 (errors
& ~XILINX_DMA_DMASR_ERR_RECOVER_MASK
)) {
1434 "Channel %p has errors %x, cdr %x tdr %x\n",
1436 dma_ctrl_read(chan
, XILINX_DMA_REG_CURDESC
),
1437 dma_ctrl_read(chan
, XILINX_DMA_REG_TAILDESC
));
1442 if (status
& XILINX_DMA_DMASR_DLY_CNT_IRQ
) {
1444 * Device takes too long to do the transfer when user requires
1447 dev_dbg(chan
->dev
, "Inter-packet latency too long\n");
1450 if (status
& XILINX_DMA_DMASR_FRM_CNT_IRQ
) {
1451 spin_lock(&chan
->lock
);
1452 xilinx_dma_complete_descriptor(chan
);
1453 chan
->start_transfer(chan
);
1454 spin_unlock(&chan
->lock
);
1457 tasklet_schedule(&chan
->tasklet
);
1462 * append_desc_queue - Queuing descriptor
1463 * @chan: Driver specific dma channel
1464 * @desc: dma transaction descriptor
1466 static void append_desc_queue(struct xilinx_dma_chan
*chan
,
1467 struct xilinx_dma_tx_descriptor
*desc
)
1469 struct xilinx_vdma_tx_segment
*tail_segment
;
1470 struct xilinx_dma_tx_descriptor
*tail_desc
;
1471 struct xilinx_axidma_tx_segment
*axidma_tail_segment
;
1472 struct xilinx_cdma_tx_segment
*cdma_tail_segment
;
1474 if (list_empty(&chan
->pending_list
))
1478 * Add the hardware descriptor to the chain of hardware descriptors
1479 * that already exists in memory.
1481 tail_desc
= list_last_entry(&chan
->pending_list
,
1482 struct xilinx_dma_tx_descriptor
, node
);
1483 if (chan
->xdev
->dma_config
->dmatype
== XDMA_TYPE_VDMA
) {
1484 tail_segment
= list_last_entry(&tail_desc
->segments
,
1485 struct xilinx_vdma_tx_segment
,
1487 tail_segment
->hw
.next_desc
= (u32
)desc
->async_tx
.phys
;
1488 } else if (chan
->xdev
->dma_config
->dmatype
== XDMA_TYPE_CDMA
) {
1489 cdma_tail_segment
= list_last_entry(&tail_desc
->segments
,
1490 struct xilinx_cdma_tx_segment
,
1492 cdma_tail_segment
->hw
.next_desc
= (u32
)desc
->async_tx
.phys
;
1494 axidma_tail_segment
= list_last_entry(&tail_desc
->segments
,
1495 struct xilinx_axidma_tx_segment
,
1497 axidma_tail_segment
->hw
.next_desc
= (u32
)desc
->async_tx
.phys
;
1501 * Add the software descriptor and all children to the list
1502 * of pending transactions
1505 list_add_tail(&desc
->node
, &chan
->pending_list
);
1506 chan
->desc_pendingcount
++;
1508 if (chan
->has_sg
&& (chan
->xdev
->dma_config
->dmatype
== XDMA_TYPE_VDMA
)
1509 && unlikely(chan
->desc_pendingcount
> chan
->num_frms
)) {
1510 dev_dbg(chan
->dev
, "desc pendingcount is too high\n");
1511 chan
->desc_pendingcount
= chan
->num_frms
;
1516 * xilinx_dma_tx_submit - Submit DMA transaction
1517 * @tx: Async transaction descriptor
1519 * Return: cookie value on success and failure value on error
1521 static dma_cookie_t
xilinx_dma_tx_submit(struct dma_async_tx_descriptor
*tx
)
1523 struct xilinx_dma_tx_descriptor
*desc
= to_dma_tx_descriptor(tx
);
1524 struct xilinx_dma_chan
*chan
= to_xilinx_chan(tx
->chan
);
1525 dma_cookie_t cookie
;
1526 unsigned long flags
;
1530 xilinx_dma_free_tx_descriptor(chan
, desc
);
1536 * If reset fails, need to hard reset the system.
1537 * Channel is no longer functional
1539 err
= xilinx_dma_chan_reset(chan
);
1544 spin_lock_irqsave(&chan
->lock
, flags
);
1546 cookie
= dma_cookie_assign(tx
);
1548 /* Put this transaction onto the tail of the pending queue */
1549 append_desc_queue(chan
, desc
);
1552 chan
->cyclic
= true;
1554 spin_unlock_irqrestore(&chan
->lock
, flags
);
1560 * xilinx_vdma_dma_prep_interleaved - prepare a descriptor for a
1561 * DMA_SLAVE transaction
1562 * @dchan: DMA channel
1563 * @xt: Interleaved template pointer
1564 * @flags: transfer ack flags
1566 * Return: Async transaction descriptor on success and NULL on failure
1568 static struct dma_async_tx_descriptor
*
1569 xilinx_vdma_dma_prep_interleaved(struct dma_chan
*dchan
,
1570 struct dma_interleaved_template
*xt
,
1571 unsigned long flags
)
1573 struct xilinx_dma_chan
*chan
= to_xilinx_chan(dchan
);
1574 struct xilinx_dma_tx_descriptor
*desc
;
1575 struct xilinx_vdma_tx_segment
*segment
, *prev
= NULL
;
1576 struct xilinx_vdma_desc_hw
*hw
;
1578 if (!is_slave_direction(xt
->dir
))
1581 if (!xt
->numf
|| !xt
->sgl
[0].size
)
1584 if (xt
->frame_size
!= 1)
1587 /* Allocate a transaction descriptor. */
1588 desc
= xilinx_dma_alloc_tx_descriptor(chan
);
1592 dma_async_tx_descriptor_init(&desc
->async_tx
, &chan
->common
);
1593 desc
->async_tx
.tx_submit
= xilinx_dma_tx_submit
;
1594 async_tx_ack(&desc
->async_tx
);
1596 /* Allocate the link descriptor from DMA pool */
1597 segment
= xilinx_vdma_alloc_tx_segment(chan
);
1601 /* Fill in the hardware descriptor */
1603 hw
->vsize
= xt
->numf
;
1604 hw
->hsize
= xt
->sgl
[0].size
;
1605 hw
->stride
= (xt
->sgl
[0].icg
+ xt
->sgl
[0].size
) <<
1606 XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT
;
1607 hw
->stride
|= chan
->config
.frm_dly
<<
1608 XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT
;
1610 if (xt
->dir
!= DMA_MEM_TO_DEV
) {
1611 if (chan
->ext_addr
) {
1612 hw
->buf_addr
= lower_32_bits(xt
->dst_start
);
1613 hw
->buf_addr_msb
= upper_32_bits(xt
->dst_start
);
1615 hw
->buf_addr
= xt
->dst_start
;
1618 if (chan
->ext_addr
) {
1619 hw
->buf_addr
= lower_32_bits(xt
->src_start
);
1620 hw
->buf_addr_msb
= upper_32_bits(xt
->src_start
);
1622 hw
->buf_addr
= xt
->src_start
;
1626 /* Insert the segment into the descriptor segments list. */
1627 list_add_tail(&segment
->node
, &desc
->segments
);
1631 /* Link the last hardware descriptor with the first. */
1632 segment
= list_first_entry(&desc
->segments
,
1633 struct xilinx_vdma_tx_segment
, node
);
1634 desc
->async_tx
.phys
= segment
->phys
;
1636 return &desc
->async_tx
;
1639 xilinx_dma_free_tx_descriptor(chan
, desc
);
1644 * xilinx_cdma_prep_memcpy - prepare descriptors for a memcpy transaction
1645 * @dchan: DMA channel
1646 * @dma_dst: destination address
1647 * @dma_src: source address
1648 * @len: transfer length
1649 * @flags: transfer ack flags
1651 * Return: Async transaction descriptor on success and NULL on failure
1653 static struct dma_async_tx_descriptor
*
1654 xilinx_cdma_prep_memcpy(struct dma_chan
*dchan
, dma_addr_t dma_dst
,
1655 dma_addr_t dma_src
, size_t len
, unsigned long flags
)
1657 struct xilinx_dma_chan
*chan
= to_xilinx_chan(dchan
);
1658 struct xilinx_dma_tx_descriptor
*desc
;
1659 struct xilinx_cdma_tx_segment
*segment
, *prev
;
1660 struct xilinx_cdma_desc_hw
*hw
;
1662 if (!len
|| len
> XILINX_DMA_MAX_TRANS_LEN
)
1665 desc
= xilinx_dma_alloc_tx_descriptor(chan
);
1669 dma_async_tx_descriptor_init(&desc
->async_tx
, &chan
->common
);
1670 desc
->async_tx
.tx_submit
= xilinx_dma_tx_submit
;
1672 /* Allocate the link descriptor from DMA pool */
1673 segment
= xilinx_cdma_alloc_tx_segment(chan
);
1679 hw
->src_addr
= dma_src
;
1680 hw
->dest_addr
= dma_dst
;
1681 if (chan
->ext_addr
) {
1682 hw
->src_addr_msb
= upper_32_bits(dma_src
);
1683 hw
->dest_addr_msb
= upper_32_bits(dma_dst
);
1686 /* Fill the previous next descriptor with current */
1687 prev
= list_last_entry(&desc
->segments
,
1688 struct xilinx_cdma_tx_segment
, node
);
1689 prev
->hw
.next_desc
= segment
->phys
;
1691 /* Insert the segment into the descriptor segments list. */
1692 list_add_tail(&segment
->node
, &desc
->segments
);
1696 /* Link the last hardware descriptor with the first. */
1697 segment
= list_first_entry(&desc
->segments
,
1698 struct xilinx_cdma_tx_segment
, node
);
1699 desc
->async_tx
.phys
= segment
->phys
;
1700 prev
->hw
.next_desc
= segment
->phys
;
1702 return &desc
->async_tx
;
1705 xilinx_dma_free_tx_descriptor(chan
, desc
);
1710 * xilinx_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
1711 * @dchan: DMA channel
1712 * @sgl: scatterlist to transfer to/from
1713 * @sg_len: number of entries in @scatterlist
1714 * @direction: DMA direction
1715 * @flags: transfer ack flags
1716 * @context: APP words of the descriptor
1718 * Return: Async transaction descriptor on success and NULL on failure
1720 static struct dma_async_tx_descriptor
*xilinx_dma_prep_slave_sg(
1721 struct dma_chan
*dchan
, struct scatterlist
*sgl
, unsigned int sg_len
,
1722 enum dma_transfer_direction direction
, unsigned long flags
,
1725 struct xilinx_dma_chan
*chan
= to_xilinx_chan(dchan
);
1726 struct xilinx_dma_tx_descriptor
*desc
;
1727 struct xilinx_axidma_tx_segment
*segment
= NULL
, *prev
= NULL
;
1728 u32
*app_w
= (u32
*)context
;
1729 struct scatterlist
*sg
;
1734 if (!is_slave_direction(direction
))
1737 /* Allocate a transaction descriptor. */
1738 desc
= xilinx_dma_alloc_tx_descriptor(chan
);
1742 dma_async_tx_descriptor_init(&desc
->async_tx
, &chan
->common
);
1743 desc
->async_tx
.tx_submit
= xilinx_dma_tx_submit
;
1745 /* Build transactions using information in the scatter gather list */
1746 for_each_sg(sgl
, sg
, sg_len
, i
) {
1749 /* Loop until the entire scatterlist entry is used */
1750 while (sg_used
< sg_dma_len(sg
)) {
1751 struct xilinx_axidma_desc_hw
*hw
;
1753 /* Get a free segment */
1754 segment
= xilinx_axidma_alloc_tx_segment(chan
);
1759 * Calculate the maximum number of bytes to transfer,
1760 * making sure it is less than the hw limit
1762 copy
= min_t(size_t, sg_dma_len(sg
) - sg_used
,
1763 XILINX_DMA_MAX_TRANS_LEN
);
1766 /* Fill in the descriptor */
1767 xilinx_axidma_buf(chan
, hw
, sg_dma_address(sg
),
1772 if (chan
->direction
== DMA_MEM_TO_DEV
) {
1774 memcpy(hw
->app
, app_w
, sizeof(u32
) *
1775 XILINX_DMA_NUM_APP_WORDS
);
1779 prev
->hw
.next_desc
= segment
->phys
;
1785 * Insert the segment into the descriptor segments
1788 list_add_tail(&segment
->node
, &desc
->segments
);
1792 segment
= list_first_entry(&desc
->segments
,
1793 struct xilinx_axidma_tx_segment
, node
);
1794 desc
->async_tx
.phys
= segment
->phys
;
1795 prev
->hw
.next_desc
= segment
->phys
;
1797 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1798 if (chan
->direction
== DMA_MEM_TO_DEV
) {
1799 segment
->hw
.control
|= XILINX_DMA_BD_SOP
;
1800 segment
= list_last_entry(&desc
->segments
,
1801 struct xilinx_axidma_tx_segment
,
1803 segment
->hw
.control
|= XILINX_DMA_BD_EOP
;
1806 return &desc
->async_tx
;
1809 xilinx_dma_free_tx_descriptor(chan
, desc
);
1814 * xilinx_dma_prep_dma_cyclic - prepare descriptors for a DMA_SLAVE transaction
1815 * @chan: DMA channel
1816 * @sgl: scatterlist to transfer to/from
1817 * @sg_len: number of entries in @scatterlist
1818 * @direction: DMA direction
1819 * @flags: transfer ack flags
1821 static struct dma_async_tx_descriptor
*xilinx_dma_prep_dma_cyclic(
1822 struct dma_chan
*dchan
, dma_addr_t buf_addr
, size_t buf_len
,
1823 size_t period_len
, enum dma_transfer_direction direction
,
1824 unsigned long flags
)
1826 struct xilinx_dma_chan
*chan
= to_xilinx_chan(dchan
);
1827 struct xilinx_dma_tx_descriptor
*desc
;
1828 struct xilinx_axidma_tx_segment
*segment
, *head_segment
, *prev
= NULL
;
1829 size_t copy
, sg_used
;
1830 unsigned int num_periods
;
1837 num_periods
= buf_len
/ period_len
;
1842 if (!is_slave_direction(direction
))
1845 /* Allocate a transaction descriptor. */
1846 desc
= xilinx_dma_alloc_tx_descriptor(chan
);
1850 chan
->direction
= direction
;
1851 dma_async_tx_descriptor_init(&desc
->async_tx
, &chan
->common
);
1852 desc
->async_tx
.tx_submit
= xilinx_dma_tx_submit
;
1854 for (i
= 0; i
< num_periods
; ++i
) {
1857 while (sg_used
< period_len
) {
1858 struct xilinx_axidma_desc_hw
*hw
;
1860 /* Get a free segment */
1861 segment
= xilinx_axidma_alloc_tx_segment(chan
);
1866 * Calculate the maximum number of bytes to transfer,
1867 * making sure it is less than the hw limit
1869 copy
= min_t(size_t, period_len
- sg_used
,
1870 XILINX_DMA_MAX_TRANS_LEN
);
1872 xilinx_axidma_buf(chan
, hw
, buf_addr
, sg_used
,
1877 prev
->hw
.next_desc
= segment
->phys
;
1883 * Insert the segment into the descriptor segments
1886 list_add_tail(&segment
->node
, &desc
->segments
);
1890 head_segment
= list_first_entry(&desc
->segments
,
1891 struct xilinx_axidma_tx_segment
, node
);
1892 desc
->async_tx
.phys
= head_segment
->phys
;
1894 desc
->cyclic
= true;
1895 reg
= dma_ctrl_read(chan
, XILINX_DMA_REG_DMACR
);
1896 reg
|= XILINX_DMA_CR_CYCLIC_BD_EN_MASK
;
1897 dma_ctrl_write(chan
, XILINX_DMA_REG_DMACR
, reg
);
1899 segment
= list_last_entry(&desc
->segments
,
1900 struct xilinx_axidma_tx_segment
,
1902 segment
->hw
.next_desc
= (u32
) head_segment
->phys
;
1904 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1905 if (direction
== DMA_MEM_TO_DEV
) {
1906 head_segment
->hw
.control
|= XILINX_DMA_BD_SOP
;
1907 segment
->hw
.control
|= XILINX_DMA_BD_EOP
;
1910 return &desc
->async_tx
;
1913 xilinx_dma_free_tx_descriptor(chan
, desc
);
1918 * xilinx_dma_prep_interleaved - prepare a descriptor for a
1919 * DMA_SLAVE transaction
1920 * @dchan: DMA channel
1921 * @xt: Interleaved template pointer
1922 * @flags: transfer ack flags
1924 * Return: Async transaction descriptor on success and NULL on failure
1926 static struct dma_async_tx_descriptor
*
1927 xilinx_dma_prep_interleaved(struct dma_chan
*dchan
,
1928 struct dma_interleaved_template
*xt
,
1929 unsigned long flags
)
1931 struct xilinx_dma_chan
*chan
= to_xilinx_chan(dchan
);
1932 struct xilinx_dma_tx_descriptor
*desc
;
1933 struct xilinx_axidma_tx_segment
*segment
;
1934 struct xilinx_axidma_desc_hw
*hw
;
1936 if (!is_slave_direction(xt
->dir
))
1939 if (!xt
->numf
|| !xt
->sgl
[0].size
)
1942 if (xt
->frame_size
!= 1)
1945 /* Allocate a transaction descriptor. */
1946 desc
= xilinx_dma_alloc_tx_descriptor(chan
);
1950 chan
->direction
= xt
->dir
;
1951 dma_async_tx_descriptor_init(&desc
->async_tx
, &chan
->common
);
1952 desc
->async_tx
.tx_submit
= xilinx_dma_tx_submit
;
1954 /* Get a free segment */
1955 segment
= xilinx_axidma_alloc_tx_segment(chan
);
1961 /* Fill in the descriptor */
1962 if (xt
->dir
!= DMA_MEM_TO_DEV
)
1963 hw
->buf_addr
= xt
->dst_start
;
1965 hw
->buf_addr
= xt
->src_start
;
1967 hw
->mcdma_control
= chan
->tdest
& XILINX_DMA_BD_TDEST_MASK
;
1968 hw
->vsize_stride
= (xt
->numf
<< XILINX_DMA_BD_VSIZE_SHIFT
) &
1969 XILINX_DMA_BD_VSIZE_MASK
;
1970 hw
->vsize_stride
|= (xt
->sgl
[0].icg
+ xt
->sgl
[0].size
) &
1971 XILINX_DMA_BD_STRIDE_MASK
;
1972 hw
->control
= xt
->sgl
[0].size
& XILINX_DMA_BD_HSIZE_MASK
;
1975 * Insert the segment into the descriptor segments
1978 list_add_tail(&segment
->node
, &desc
->segments
);
1981 segment
= list_first_entry(&desc
->segments
,
1982 struct xilinx_axidma_tx_segment
, node
);
1983 desc
->async_tx
.phys
= segment
->phys
;
1985 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1986 if (xt
->dir
== DMA_MEM_TO_DEV
) {
1987 segment
->hw
.control
|= XILINX_DMA_BD_SOP
;
1988 segment
= list_last_entry(&desc
->segments
,
1989 struct xilinx_axidma_tx_segment
,
1991 segment
->hw
.control
|= XILINX_DMA_BD_EOP
;
1994 return &desc
->async_tx
;
1997 xilinx_dma_free_tx_descriptor(chan
, desc
);
2002 * xilinx_dma_terminate_all - Halt the channel and free descriptors
2003 * @chan: Driver specific DMA Channel pointer
2005 static int xilinx_dma_terminate_all(struct dma_chan
*dchan
)
2007 struct xilinx_dma_chan
*chan
= to_xilinx_chan(dchan
);
2011 xilinx_dma_chan_reset(chan
);
2013 /* Halt the DMA engine */
2014 xilinx_dma_halt(chan
);
2016 /* Remove and free all of the descriptors in the lists */
2017 xilinx_dma_free_descriptors(chan
);
2020 reg
= dma_ctrl_read(chan
, XILINX_DMA_REG_DMACR
);
2021 reg
&= ~XILINX_DMA_CR_CYCLIC_BD_EN_MASK
;
2022 dma_ctrl_write(chan
, XILINX_DMA_REG_DMACR
, reg
);
2023 chan
->cyclic
= false;
2030 * xilinx_dma_channel_set_config - Configure VDMA channel
2031 * Run-time configuration for Axi VDMA, supports:
2032 * . halt the channel
2033 * . configure interrupt coalescing and inter-packet delay threshold
2034 * . start/stop parking
2037 * @dchan: DMA channel
2038 * @cfg: VDMA device configuration pointer
2040 * Return: '0' on success and failure value on error
2042 int xilinx_vdma_channel_set_config(struct dma_chan
*dchan
,
2043 struct xilinx_vdma_config
*cfg
)
2045 struct xilinx_dma_chan
*chan
= to_xilinx_chan(dchan
);
2049 return xilinx_dma_chan_reset(chan
);
2051 dmacr
= dma_ctrl_read(chan
, XILINX_DMA_REG_DMACR
);
2053 chan
->config
.frm_dly
= cfg
->frm_dly
;
2054 chan
->config
.park
= cfg
->park
;
2056 /* genlock settings */
2057 chan
->config
.gen_lock
= cfg
->gen_lock
;
2058 chan
->config
.master
= cfg
->master
;
2060 dmacr
&= ~XILINX_DMA_DMACR_GENLOCK_EN
;
2061 if (cfg
->gen_lock
&& chan
->genlock
) {
2062 dmacr
|= XILINX_DMA_DMACR_GENLOCK_EN
;
2063 dmacr
&= ~XILINX_DMA_DMACR_MASTER_MASK
;
2064 dmacr
|= cfg
->master
<< XILINX_DMA_DMACR_MASTER_SHIFT
;
2067 chan
->config
.frm_cnt_en
= cfg
->frm_cnt_en
;
2069 chan
->config
.park_frm
= cfg
->park_frm
;
2071 chan
->config
.park_frm
= -1;
2073 chan
->config
.coalesc
= cfg
->coalesc
;
2074 chan
->config
.delay
= cfg
->delay
;
2076 if (cfg
->coalesc
<= XILINX_DMA_DMACR_FRAME_COUNT_MAX
) {
2077 dmacr
&= ~XILINX_DMA_DMACR_FRAME_COUNT_MASK
;
2078 dmacr
|= cfg
->coalesc
<< XILINX_DMA_DMACR_FRAME_COUNT_SHIFT
;
2079 chan
->config
.coalesc
= cfg
->coalesc
;
2082 if (cfg
->delay
<= XILINX_DMA_DMACR_DELAY_MAX
) {
2083 dmacr
&= ~XILINX_DMA_DMACR_DELAY_MASK
;
2084 dmacr
|= cfg
->delay
<< XILINX_DMA_DMACR_DELAY_SHIFT
;
2085 chan
->config
.delay
= cfg
->delay
;
2088 /* FSync Source selection */
2089 dmacr
&= ~XILINX_DMA_DMACR_FSYNCSRC_MASK
;
2090 dmacr
|= cfg
->ext_fsync
<< XILINX_DMA_DMACR_FSYNCSRC_SHIFT
;
2092 dma_ctrl_write(chan
, XILINX_DMA_REG_DMACR
, dmacr
);
2096 EXPORT_SYMBOL(xilinx_vdma_channel_set_config
);
2098 /* -----------------------------------------------------------------------------
2103 * xilinx_dma_chan_remove - Per Channel remove function
2104 * @chan: Driver specific DMA channel
2106 static void xilinx_dma_chan_remove(struct xilinx_dma_chan
*chan
)
2108 /* Disable all interrupts */
2109 dma_ctrl_clr(chan
, XILINX_DMA_REG_DMACR
,
2110 XILINX_DMA_DMAXR_ALL_IRQ_MASK
);
2113 free_irq(chan
->irq
, chan
);
2115 tasklet_kill(&chan
->tasklet
);
2117 list_del(&chan
->common
.device_node
);
2120 static int axidma_clk_init(struct platform_device
*pdev
, struct clk
**axi_clk
,
2121 struct clk
**tx_clk
, struct clk
**rx_clk
,
2122 struct clk
**sg_clk
, struct clk
**tmp_clk
)
2128 *axi_clk
= devm_clk_get(&pdev
->dev
, "s_axi_lite_aclk");
2129 if (IS_ERR(*axi_clk
)) {
2130 err
= PTR_ERR(*axi_clk
);
2131 dev_err(&pdev
->dev
, "failed to get axi_aclk (%u)\n", err
);
2135 *tx_clk
= devm_clk_get(&pdev
->dev
, "m_axi_mm2s_aclk");
2136 if (IS_ERR(*tx_clk
))
2139 *rx_clk
= devm_clk_get(&pdev
->dev
, "m_axi_s2mm_aclk");
2140 if (IS_ERR(*rx_clk
))
2143 *sg_clk
= devm_clk_get(&pdev
->dev
, "m_axi_sg_aclk");
2144 if (IS_ERR(*sg_clk
))
2147 err
= clk_prepare_enable(*axi_clk
);
2149 dev_err(&pdev
->dev
, "failed to enable axi_clk (%u)\n", err
);
2153 err
= clk_prepare_enable(*tx_clk
);
2155 dev_err(&pdev
->dev
, "failed to enable tx_clk (%u)\n", err
);
2156 goto err_disable_axiclk
;
2159 err
= clk_prepare_enable(*rx_clk
);
2161 dev_err(&pdev
->dev
, "failed to enable rx_clk (%u)\n", err
);
2162 goto err_disable_txclk
;
2165 err
= clk_prepare_enable(*sg_clk
);
2167 dev_err(&pdev
->dev
, "failed to enable sg_clk (%u)\n", err
);
2168 goto err_disable_rxclk
;
2174 clk_disable_unprepare(*rx_clk
);
2176 clk_disable_unprepare(*tx_clk
);
2178 clk_disable_unprepare(*axi_clk
);
2183 static int axicdma_clk_init(struct platform_device
*pdev
, struct clk
**axi_clk
,
2184 struct clk
**dev_clk
, struct clk
**tmp_clk
,
2185 struct clk
**tmp1_clk
, struct clk
**tmp2_clk
)
2193 *axi_clk
= devm_clk_get(&pdev
->dev
, "s_axi_lite_aclk");
2194 if (IS_ERR(*axi_clk
)) {
2195 err
= PTR_ERR(*axi_clk
);
2196 dev_err(&pdev
->dev
, "failed to get axi_clk (%u)\n", err
);
2200 *dev_clk
= devm_clk_get(&pdev
->dev
, "m_axi_aclk");
2201 if (IS_ERR(*dev_clk
)) {
2202 err
= PTR_ERR(*dev_clk
);
2203 dev_err(&pdev
->dev
, "failed to get dev_clk (%u)\n", err
);
2207 err
= clk_prepare_enable(*axi_clk
);
2209 dev_err(&pdev
->dev
, "failed to enable axi_clk (%u)\n", err
);
2213 err
= clk_prepare_enable(*dev_clk
);
2215 dev_err(&pdev
->dev
, "failed to enable dev_clk (%u)\n", err
);
2216 goto err_disable_axiclk
;
2222 clk_disable_unprepare(*axi_clk
);
2227 static int axivdma_clk_init(struct platform_device
*pdev
, struct clk
**axi_clk
,
2228 struct clk
**tx_clk
, struct clk
**txs_clk
,
2229 struct clk
**rx_clk
, struct clk
**rxs_clk
)
2233 *axi_clk
= devm_clk_get(&pdev
->dev
, "s_axi_lite_aclk");
2234 if (IS_ERR(*axi_clk
)) {
2235 err
= PTR_ERR(*axi_clk
);
2236 dev_err(&pdev
->dev
, "failed to get axi_aclk (%u)\n", err
);
2240 *tx_clk
= devm_clk_get(&pdev
->dev
, "m_axi_mm2s_aclk");
2241 if (IS_ERR(*tx_clk
))
2244 *txs_clk
= devm_clk_get(&pdev
->dev
, "m_axis_mm2s_aclk");
2245 if (IS_ERR(*txs_clk
))
2248 *rx_clk
= devm_clk_get(&pdev
->dev
, "m_axi_s2mm_aclk");
2249 if (IS_ERR(*rx_clk
))
2252 *rxs_clk
= devm_clk_get(&pdev
->dev
, "s_axis_s2mm_aclk");
2253 if (IS_ERR(*rxs_clk
))
2256 err
= clk_prepare_enable(*axi_clk
);
2258 dev_err(&pdev
->dev
, "failed to enable axi_clk (%u)\n", err
);
2262 err
= clk_prepare_enable(*tx_clk
);
2264 dev_err(&pdev
->dev
, "failed to enable tx_clk (%u)\n", err
);
2265 goto err_disable_axiclk
;
2268 err
= clk_prepare_enable(*txs_clk
);
2270 dev_err(&pdev
->dev
, "failed to enable txs_clk (%u)\n", err
);
2271 goto err_disable_txclk
;
2274 err
= clk_prepare_enable(*rx_clk
);
2276 dev_err(&pdev
->dev
, "failed to enable rx_clk (%u)\n", err
);
2277 goto err_disable_txsclk
;
2280 err
= clk_prepare_enable(*rxs_clk
);
2282 dev_err(&pdev
->dev
, "failed to enable rxs_clk (%u)\n", err
);
2283 goto err_disable_rxclk
;
2289 clk_disable_unprepare(*rx_clk
);
2291 clk_disable_unprepare(*txs_clk
);
2293 clk_disable_unprepare(*tx_clk
);
2295 clk_disable_unprepare(*axi_clk
);
2300 static void xdma_disable_allclks(struct xilinx_dma_device
*xdev
)
2302 clk_disable_unprepare(xdev
->rxs_clk
);
2303 clk_disable_unprepare(xdev
->rx_clk
);
2304 clk_disable_unprepare(xdev
->txs_clk
);
2305 clk_disable_unprepare(xdev
->tx_clk
);
2306 clk_disable_unprepare(xdev
->axi_clk
);
2310 * xilinx_dma_chan_probe - Per Channel Probing
2311 * It get channel features from the device tree entry and
2312 * initialize special channel handling routines
2314 * @xdev: Driver specific device structure
2315 * @node: Device node
2317 * Return: '0' on success and failure value on error
2319 static int xilinx_dma_chan_probe(struct xilinx_dma_device
*xdev
,
2320 struct device_node
*node
, int chan_id
)
2322 struct xilinx_dma_chan
*chan
;
2323 bool has_dre
= false;
2327 /* Allocate and initialize the channel structure */
2328 chan
= devm_kzalloc(xdev
->dev
, sizeof(*chan
), GFP_KERNEL
);
2332 chan
->dev
= xdev
->dev
;
2334 chan
->has_sg
= xdev
->has_sg
;
2335 chan
->desc_pendingcount
= 0x0;
2336 chan
->ext_addr
= xdev
->ext_addr
;
2338 spin_lock_init(&chan
->lock
);
2339 INIT_LIST_HEAD(&chan
->pending_list
);
2340 INIT_LIST_HEAD(&chan
->done_list
);
2341 INIT_LIST_HEAD(&chan
->active_list
);
2343 /* Retrieve the channel properties from the device tree */
2344 has_dre
= of_property_read_bool(node
, "xlnx,include-dre");
2346 chan
->genlock
= of_property_read_bool(node
, "xlnx,genlock-mode");
2348 err
= of_property_read_u32(node
, "xlnx,datawidth", &value
);
2350 dev_err(xdev
->dev
, "missing xlnx,datawidth property\n");
2353 width
= value
>> 3; /* Convert bits to bytes */
2355 /* If data width is greater than 8 bytes, DRE is not in hw */
2360 xdev
->common
.copy_align
= fls(width
- 1);
2362 if (of_device_is_compatible(node
, "xlnx,axi-vdma-mm2s-channel") ||
2363 of_device_is_compatible(node
, "xlnx,axi-dma-mm2s-channel") ||
2364 of_device_is_compatible(node
, "xlnx,axi-cdma-channel")) {
2365 chan
->direction
= DMA_MEM_TO_DEV
;
2367 chan
->tdest
= chan_id
;
2369 chan
->ctrl_offset
= XILINX_DMA_MM2S_CTRL_OFFSET
;
2370 if (xdev
->dma_config
->dmatype
== XDMA_TYPE_VDMA
) {
2371 chan
->desc_offset
= XILINX_VDMA_MM2S_DESC_OFFSET
;
2373 if (xdev
->flush_on_fsync
== XILINX_DMA_FLUSH_BOTH
||
2374 xdev
->flush_on_fsync
== XILINX_DMA_FLUSH_MM2S
)
2375 chan
->flush_on_fsync
= true;
2377 } else if (of_device_is_compatible(node
,
2378 "xlnx,axi-vdma-s2mm-channel") ||
2379 of_device_is_compatible(node
,
2380 "xlnx,axi-dma-s2mm-channel")) {
2381 chan
->direction
= DMA_DEV_TO_MEM
;
2383 chan
->tdest
= chan_id
- xdev
->nr_channels
;
2385 chan
->ctrl_offset
= XILINX_DMA_S2MM_CTRL_OFFSET
;
2386 if (xdev
->dma_config
->dmatype
== XDMA_TYPE_VDMA
) {
2387 chan
->desc_offset
= XILINX_VDMA_S2MM_DESC_OFFSET
;
2389 if (xdev
->flush_on_fsync
== XILINX_DMA_FLUSH_BOTH
||
2390 xdev
->flush_on_fsync
== XILINX_DMA_FLUSH_S2MM
)
2391 chan
->flush_on_fsync
= true;
2394 dev_err(xdev
->dev
, "Invalid channel compatible node\n");
2398 /* Request the interrupt */
2399 chan
->irq
= irq_of_parse_and_map(node
, 0);
2400 err
= request_irq(chan
->irq
, xilinx_dma_irq_handler
, IRQF_SHARED
,
2401 "xilinx-dma-controller", chan
);
2403 dev_err(xdev
->dev
, "unable to request IRQ %d\n", chan
->irq
);
2407 if (xdev
->dma_config
->dmatype
== XDMA_TYPE_AXIDMA
)
2408 chan
->start_transfer
= xilinx_dma_start_transfer
;
2409 else if (xdev
->dma_config
->dmatype
== XDMA_TYPE_CDMA
)
2410 chan
->start_transfer
= xilinx_cdma_start_transfer
;
2412 chan
->start_transfer
= xilinx_vdma_start_transfer
;
2414 /* Initialize the tasklet */
2415 tasklet_init(&chan
->tasklet
, xilinx_dma_do_tasklet
,
2416 (unsigned long)chan
);
2419 * Initialize the DMA channel and add it to the DMA engine channels
2422 chan
->common
.device
= &xdev
->common
;
2424 list_add_tail(&chan
->common
.device_node
, &xdev
->common
.channels
);
2425 xdev
->chan
[chan
->id
] = chan
;
2427 /* Reset the channel */
2428 err
= xilinx_dma_chan_reset(chan
);
2430 dev_err(xdev
->dev
, "Reset channel failed\n");
2438 * xilinx_dma_child_probe - Per child node probe
2439 * It get number of dma-channels per child node from
2440 * device-tree and initializes all the channels.
2442 * @xdev: Driver specific device structure
2443 * @node: Device node
2447 static int xilinx_dma_child_probe(struct xilinx_dma_device
*xdev
,
2448 struct device_node
*node
) {
2449 int ret
, i
, nr_channels
= 1;
2451 ret
= of_property_read_u32(node
, "dma-channels", &nr_channels
);
2452 if ((ret
< 0) && xdev
->mcdma
)
2453 dev_warn(xdev
->dev
, "missing dma-channels property\n");
2455 for (i
= 0; i
< nr_channels
; i
++)
2456 xilinx_dma_chan_probe(xdev
, node
, xdev
->chan_id
++);
2458 xdev
->nr_channels
+= nr_channels
;
2464 * of_dma_xilinx_xlate - Translation function
2465 * @dma_spec: Pointer to DMA specifier as found in the device tree
2466 * @ofdma: Pointer to DMA controller data
2468 * Return: DMA channel pointer on success and NULL on error
2470 static struct dma_chan
*of_dma_xilinx_xlate(struct of_phandle_args
*dma_spec
,
2471 struct of_dma
*ofdma
)
2473 struct xilinx_dma_device
*xdev
= ofdma
->of_dma_data
;
2474 int chan_id
= dma_spec
->args
[0];
2476 if (chan_id
>= xdev
->nr_channels
|| !xdev
->chan
[chan_id
])
2479 return dma_get_slave_channel(&xdev
->chan
[chan_id
]->common
);
2482 static const struct xilinx_dma_config axidma_config
= {
2483 .dmatype
= XDMA_TYPE_AXIDMA
,
2484 .clk_init
= axidma_clk_init
,
2487 static const struct xilinx_dma_config axicdma_config
= {
2488 .dmatype
= XDMA_TYPE_CDMA
,
2489 .clk_init
= axicdma_clk_init
,
2492 static const struct xilinx_dma_config axivdma_config
= {
2493 .dmatype
= XDMA_TYPE_VDMA
,
2494 .clk_init
= axivdma_clk_init
,
2497 static const struct of_device_id xilinx_dma_of_ids
[] = {
2498 { .compatible
= "xlnx,axi-dma-1.00.a", .data
= &axidma_config
},
2499 { .compatible
= "xlnx,axi-cdma-1.00.a", .data
= &axicdma_config
},
2500 { .compatible
= "xlnx,axi-vdma-1.00.a", .data
= &axivdma_config
},
2503 MODULE_DEVICE_TABLE(of
, xilinx_dma_of_ids
);
2506 * xilinx_dma_probe - Driver probe function
2507 * @pdev: Pointer to the platform_device structure
2509 * Return: '0' on success and failure value on error
2511 static int xilinx_dma_probe(struct platform_device
*pdev
)
2513 int (*clk_init
)(struct platform_device
*, struct clk
**, struct clk
**,
2514 struct clk
**, struct clk
**, struct clk
**)
2516 struct device_node
*node
= pdev
->dev
.of_node
;
2517 struct xilinx_dma_device
*xdev
;
2518 struct device_node
*child
, *np
= pdev
->dev
.of_node
;
2519 struct resource
*io
;
2520 u32 num_frames
, addr_width
;
2523 /* Allocate and initialize the DMA engine structure */
2524 xdev
= devm_kzalloc(&pdev
->dev
, sizeof(*xdev
), GFP_KERNEL
);
2528 xdev
->dev
= &pdev
->dev
;
2530 const struct of_device_id
*match
;
2532 match
= of_match_node(xilinx_dma_of_ids
, np
);
2533 if (match
&& match
->data
) {
2534 xdev
->dma_config
= match
->data
;
2535 clk_init
= xdev
->dma_config
->clk_init
;
2539 err
= clk_init(pdev
, &xdev
->axi_clk
, &xdev
->tx_clk
, &xdev
->txs_clk
,
2540 &xdev
->rx_clk
, &xdev
->rxs_clk
);
2544 /* Request and map I/O memory */
2545 io
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2546 xdev
->regs
= devm_ioremap_resource(&pdev
->dev
, io
);
2547 if (IS_ERR(xdev
->regs
))
2548 return PTR_ERR(xdev
->regs
);
2550 /* Retrieve the DMA engine properties from the device tree */
2551 xdev
->has_sg
= of_property_read_bool(node
, "xlnx,include-sg");
2552 if (xdev
->dma_config
->dmatype
== XDMA_TYPE_AXIDMA
)
2553 xdev
->mcdma
= of_property_read_bool(node
, "xlnx,mcdma");
2555 if (xdev
->dma_config
->dmatype
== XDMA_TYPE_VDMA
) {
2556 err
= of_property_read_u32(node
, "xlnx,num-fstores",
2560 "missing xlnx,num-fstores property\n");
2564 err
= of_property_read_u32(node
, "xlnx,flush-fsync",
2565 &xdev
->flush_on_fsync
);
2568 "missing xlnx,flush-fsync property\n");
2571 err
= of_property_read_u32(node
, "xlnx,addrwidth", &addr_width
);
2573 dev_warn(xdev
->dev
, "missing xlnx,addrwidth property\n");
2575 if (addr_width
> 32)
2576 xdev
->ext_addr
= true;
2578 xdev
->ext_addr
= false;
2580 /* Set the dma mask bits */
2581 dma_set_mask(xdev
->dev
, DMA_BIT_MASK(addr_width
));
2583 /* Initialize the DMA engine */
2584 xdev
->common
.dev
= &pdev
->dev
;
2586 INIT_LIST_HEAD(&xdev
->common
.channels
);
2587 if (!(xdev
->dma_config
->dmatype
== XDMA_TYPE_CDMA
)) {
2588 dma_cap_set(DMA_SLAVE
, xdev
->common
.cap_mask
);
2589 dma_cap_set(DMA_PRIVATE
, xdev
->common
.cap_mask
);
2592 xdev
->common
.device_alloc_chan_resources
=
2593 xilinx_dma_alloc_chan_resources
;
2594 xdev
->common
.device_free_chan_resources
=
2595 xilinx_dma_free_chan_resources
;
2596 xdev
->common
.device_terminate_all
= xilinx_dma_terminate_all
;
2597 xdev
->common
.device_tx_status
= xilinx_dma_tx_status
;
2598 xdev
->common
.device_issue_pending
= xilinx_dma_issue_pending
;
2599 if (xdev
->dma_config
->dmatype
== XDMA_TYPE_AXIDMA
) {
2600 dma_cap_set(DMA_CYCLIC
, xdev
->common
.cap_mask
);
2601 xdev
->common
.device_prep_slave_sg
= xilinx_dma_prep_slave_sg
;
2602 xdev
->common
.device_prep_dma_cyclic
=
2603 xilinx_dma_prep_dma_cyclic
;
2604 xdev
->common
.device_prep_interleaved_dma
=
2605 xilinx_dma_prep_interleaved
;
2606 /* Residue calculation is supported by only AXI DMA */
2607 xdev
->common
.residue_granularity
=
2608 DMA_RESIDUE_GRANULARITY_SEGMENT
;
2609 } else if (xdev
->dma_config
->dmatype
== XDMA_TYPE_CDMA
) {
2610 dma_cap_set(DMA_MEMCPY
, xdev
->common
.cap_mask
);
2611 xdev
->common
.device_prep_dma_memcpy
= xilinx_cdma_prep_memcpy
;
2613 xdev
->common
.device_prep_interleaved_dma
=
2614 xilinx_vdma_dma_prep_interleaved
;
2617 platform_set_drvdata(pdev
, xdev
);
2619 /* Initialize the channels */
2620 for_each_child_of_node(node
, child
) {
2621 err
= xilinx_dma_child_probe(xdev
, child
);
2626 if (xdev
->dma_config
->dmatype
== XDMA_TYPE_VDMA
) {
2627 for (i
= 0; i
< xdev
->nr_channels
; i
++)
2629 xdev
->chan
[i
]->num_frms
= num_frames
;
2632 /* Register the DMA engine with the core */
2633 dma_async_device_register(&xdev
->common
);
2635 err
= of_dma_controller_register(node
, of_dma_xilinx_xlate
,
2638 dev_err(&pdev
->dev
, "Unable to register DMA to DT\n");
2639 dma_async_device_unregister(&xdev
->common
);
2643 dev_info(&pdev
->dev
, "Xilinx AXI VDMA Engine Driver Probed!!\n");
2648 xdma_disable_allclks(xdev
);
2650 for (i
= 0; i
< xdev
->nr_channels
; i
++)
2652 xilinx_dma_chan_remove(xdev
->chan
[i
]);
2658 * xilinx_dma_remove - Driver remove function
2659 * @pdev: Pointer to the platform_device structure
2661 * Return: Always '0'
2663 static int xilinx_dma_remove(struct platform_device
*pdev
)
2665 struct xilinx_dma_device
*xdev
= platform_get_drvdata(pdev
);
2668 of_dma_controller_free(pdev
->dev
.of_node
);
2670 dma_async_device_unregister(&xdev
->common
);
2672 for (i
= 0; i
< xdev
->nr_channels
; i
++)
2674 xilinx_dma_chan_remove(xdev
->chan
[i
]);
2676 xdma_disable_allclks(xdev
);
2681 static struct platform_driver xilinx_vdma_driver
= {
2683 .name
= "xilinx-vdma",
2684 .of_match_table
= xilinx_dma_of_ids
,
2686 .probe
= xilinx_dma_probe
,
2687 .remove
= xilinx_dma_remove
,
2690 module_platform_driver(xilinx_vdma_driver
);
2692 MODULE_AUTHOR("Xilinx, Inc.");
2693 MODULE_DESCRIPTION("Xilinx VDMA driver");
2694 MODULE_LICENSE("GPL v2");