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)
76 #define XILINX_DMA_REG_DMASR 0x0004
77 #define XILINX_DMA_DMASR_EOL_LATE_ERR BIT(15)
78 #define XILINX_DMA_DMASR_ERR_IRQ BIT(14)
79 #define XILINX_DMA_DMASR_DLY_CNT_IRQ BIT(13)
80 #define XILINX_DMA_DMASR_FRM_CNT_IRQ BIT(12)
81 #define XILINX_DMA_DMASR_SOF_LATE_ERR BIT(11)
82 #define XILINX_DMA_DMASR_SG_DEC_ERR BIT(10)
83 #define XILINX_DMA_DMASR_SG_SLV_ERR BIT(9)
84 #define XILINX_DMA_DMASR_EOF_EARLY_ERR BIT(8)
85 #define XILINX_DMA_DMASR_SOF_EARLY_ERR BIT(7)
86 #define XILINX_DMA_DMASR_DMA_DEC_ERR BIT(6)
87 #define XILINX_DMA_DMASR_DMA_SLAVE_ERR BIT(5)
88 #define XILINX_DMA_DMASR_DMA_INT_ERR BIT(4)
89 #define XILINX_DMA_DMASR_IDLE BIT(1)
90 #define XILINX_DMA_DMASR_HALTED BIT(0)
91 #define XILINX_DMA_DMASR_DELAY_MASK GENMASK(31, 24)
92 #define XILINX_DMA_DMASR_FRAME_COUNT_MASK GENMASK(23, 16)
94 #define XILINX_DMA_REG_CURDESC 0x0008
95 #define XILINX_DMA_REG_TAILDESC 0x0010
96 #define XILINX_DMA_REG_REG_INDEX 0x0014
97 #define XILINX_DMA_REG_FRMSTORE 0x0018
98 #define XILINX_DMA_REG_THRESHOLD 0x001c
99 #define XILINX_DMA_REG_FRMPTR_STS 0x0024
100 #define XILINX_DMA_REG_PARK_PTR 0x0028
101 #define XILINX_DMA_PARK_PTR_WR_REF_SHIFT 8
102 #define XILINX_DMA_PARK_PTR_WR_REF_MASK GENMASK(12, 8)
103 #define XILINX_DMA_PARK_PTR_RD_REF_SHIFT 0
104 #define XILINX_DMA_PARK_PTR_RD_REF_MASK GENMASK(4, 0)
105 #define XILINX_DMA_REG_VDMA_VERSION 0x002c
107 /* Register Direct Mode Registers */
108 #define XILINX_DMA_REG_VSIZE 0x0000
109 #define XILINX_DMA_REG_HSIZE 0x0004
111 #define XILINX_DMA_REG_FRMDLY_STRIDE 0x0008
112 #define XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT 24
113 #define XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT 0
115 #define XILINX_VDMA_REG_START_ADDRESS(n) (0x000c + 4 * (n))
116 #define XILINX_VDMA_REG_START_ADDRESS_64(n) (0x000c + 8 * (n))
118 #define XILINX_VDMA_REG_ENABLE_VERTICAL_FLIP 0x00ec
119 #define XILINX_VDMA_ENABLE_VERTICAL_FLIP BIT(0)
121 /* HW specific definitions */
122 #define XILINX_DMA_MAX_CHANS_PER_DEVICE 0x20
124 #define XILINX_DMA_DMAXR_ALL_IRQ_MASK \
125 (XILINX_DMA_DMASR_FRM_CNT_IRQ | \
126 XILINX_DMA_DMASR_DLY_CNT_IRQ | \
127 XILINX_DMA_DMASR_ERR_IRQ)
129 #define XILINX_DMA_DMASR_ALL_ERR_MASK \
130 (XILINX_DMA_DMASR_EOL_LATE_ERR | \
131 XILINX_DMA_DMASR_SOF_LATE_ERR | \
132 XILINX_DMA_DMASR_SG_DEC_ERR | \
133 XILINX_DMA_DMASR_SG_SLV_ERR | \
134 XILINX_DMA_DMASR_EOF_EARLY_ERR | \
135 XILINX_DMA_DMASR_SOF_EARLY_ERR | \
136 XILINX_DMA_DMASR_DMA_DEC_ERR | \
137 XILINX_DMA_DMASR_DMA_SLAVE_ERR | \
138 XILINX_DMA_DMASR_DMA_INT_ERR)
141 * Recoverable errors are DMA Internal error, SOF Early, EOF Early
142 * and SOF Late. They are only recoverable when C_FLUSH_ON_FSYNC
143 * is enabled in the h/w system.
145 #define XILINX_DMA_DMASR_ERR_RECOVER_MASK \
146 (XILINX_DMA_DMASR_SOF_LATE_ERR | \
147 XILINX_DMA_DMASR_EOF_EARLY_ERR | \
148 XILINX_DMA_DMASR_SOF_EARLY_ERR | \
149 XILINX_DMA_DMASR_DMA_INT_ERR)
151 /* Axi VDMA Flush on Fsync bits */
152 #define XILINX_DMA_FLUSH_S2MM 3
153 #define XILINX_DMA_FLUSH_MM2S 2
154 #define XILINX_DMA_FLUSH_BOTH 1
156 /* Delay loop counter to prevent hardware failure */
157 #define XILINX_DMA_LOOP_COUNT 1000000
159 /* AXI DMA Specific Registers/Offsets */
160 #define XILINX_DMA_REG_SRCDSTADDR 0x18
161 #define XILINX_DMA_REG_BTT 0x28
163 /* AXI DMA Specific Masks/Bit fields */
164 #define XILINX_DMA_MAX_TRANS_LEN GENMASK(22, 0)
165 #define XILINX_DMA_CR_COALESCE_MAX GENMASK(23, 16)
166 #define XILINX_DMA_CR_CYCLIC_BD_EN_MASK BIT(4)
167 #define XILINX_DMA_CR_COALESCE_SHIFT 16
168 #define XILINX_DMA_BD_SOP BIT(27)
169 #define XILINX_DMA_BD_EOP BIT(26)
170 #define XILINX_DMA_COALESCE_MAX 255
171 #define XILINX_DMA_NUM_DESCS 255
172 #define XILINX_DMA_NUM_APP_WORDS 5
174 /* Multi-Channel DMA Descriptor offsets*/
175 #define XILINX_DMA_MCRX_CDESC(x) (0x40 + (x-1) * 0x20)
176 #define XILINX_DMA_MCRX_TDESC(x) (0x48 + (x-1) * 0x20)
178 /* Multi-Channel DMA Masks/Shifts */
179 #define XILINX_DMA_BD_HSIZE_MASK GENMASK(15, 0)
180 #define XILINX_DMA_BD_STRIDE_MASK GENMASK(15, 0)
181 #define XILINX_DMA_BD_VSIZE_MASK GENMASK(31, 19)
182 #define XILINX_DMA_BD_TDEST_MASK GENMASK(4, 0)
183 #define XILINX_DMA_BD_STRIDE_SHIFT 0
184 #define XILINX_DMA_BD_VSIZE_SHIFT 19
186 /* AXI CDMA Specific Registers/Offsets */
187 #define XILINX_CDMA_REG_SRCADDR 0x18
188 #define XILINX_CDMA_REG_DSTADDR 0x20
190 /* AXI CDMA Specific Masks */
191 #define XILINX_CDMA_CR_SGMODE BIT(3)
194 * struct xilinx_vdma_desc_hw - Hardware Descriptor
195 * @next_desc: Next Descriptor Pointer @0x00
196 * @pad1: Reserved @0x04
197 * @buf_addr: Buffer address @0x08
198 * @buf_addr_msb: MSB of Buffer address @0x0C
199 * @vsize: Vertical Size @0x10
200 * @hsize: Horizontal Size @0x14
201 * @stride: Number of bytes between the first
202 * pixels of each horizontal line @0x18
204 struct xilinx_vdma_desc_hw {
215 * struct xilinx_axidma_desc_hw - Hardware Descriptor for AXI DMA
216 * @next_desc: Next Descriptor Pointer @0x00
217 * @next_desc_msb: MSB of Next Descriptor Pointer @0x04
218 * @buf_addr: Buffer address @0x08
219 * @buf_addr_msb: MSB of Buffer address @0x0C
220 * @mcdma_control: Control field for mcdma @0x10
221 * @vsize_stride: Vsize and Stride field for mcdma @0x14
222 * @control: Control field @0x18
223 * @status: Status field @0x1C
224 * @app: APP Fields @0x20 - 0x30
226 struct xilinx_axidma_desc_hw {
235 u32 app[XILINX_DMA_NUM_APP_WORDS];
239 * struct xilinx_cdma_desc_hw - Hardware Descriptor
240 * @next_desc: Next Descriptor Pointer @0x00
241 * @next_desc_msb: Next Descriptor Pointer MSB @0x04
242 * @src_addr: Source address @0x08
243 * @src_addr_msb: Source address MSB @0x0C
244 * @dest_addr: Destination address @0x10
245 * @dest_addr_msb: Destination address MSB @0x14
246 * @control: Control field @0x18
247 * @status: Status field @0x1C
249 struct xilinx_cdma_desc_hw {
261 * struct xilinx_vdma_tx_segment - Descriptor segment
262 * @hw: Hardware descriptor
263 * @node: Node in the descriptor segments list
264 * @phys: Physical address of segment
266 struct xilinx_vdma_tx_segment {
267 struct xilinx_vdma_desc_hw hw;
268 struct list_head node;
273 * struct xilinx_axidma_tx_segment - Descriptor segment
274 * @hw: Hardware descriptor
275 * @node: Node in the descriptor segments list
276 * @phys: Physical address of segment
278 struct xilinx_axidma_tx_segment {
279 struct xilinx_axidma_desc_hw hw;
280 struct list_head node;
285 * struct xilinx_cdma_tx_segment - Descriptor segment
286 * @hw: Hardware descriptor
287 * @node: Node in the descriptor segments list
288 * @phys: Physical address of segment
290 struct xilinx_cdma_tx_segment {
291 struct xilinx_cdma_desc_hw hw;
292 struct list_head node;
297 * struct xilinx_dma_tx_descriptor - Per Transaction structure
298 * @async_tx: Async transaction descriptor
299 * @segments: TX segments list
300 * @node: Node in the channel descriptors list
301 * @cyclic: Check for cyclic transfers.
303 struct xilinx_dma_tx_descriptor {
304 struct dma_async_tx_descriptor async_tx;
305 struct list_head segments;
306 struct list_head node;
311 * struct xilinx_dma_chan - Driver specific DMA channel structure
312 * @xdev: Driver specific device structure
313 * @ctrl_offset: Control registers offset
314 * @desc_offset: TX descriptor registers offset
315 * @lock: Descriptor operation lock
316 * @pending_list: Descriptors waiting
317 * @active_list: Descriptors ready to submit
318 * @done_list: Complete descriptors
319 * @free_seg_list: Free descriptors
320 * @common: DMA common channel
321 * @desc_pool: Descriptors pool
322 * @dev: The dma device
325 * @direction: Transfer direction
326 * @num_frms: Number of frames
327 * @has_sg: Support scatter transfers
328 * @cyclic: Check for cyclic transfers.
329 * @genlock: Support genlock mode
330 * @err: Channel has errors
331 * @idle: Check for channel idle
332 * @tasklet: Cleanup work after irq
333 * @config: Device configuration info
334 * @flush_on_fsync: Flush on Frame sync
335 * @desc_pendingcount: Descriptor pending count
336 * @ext_addr: Indicates 64 bit addressing is supported by dma channel
337 * @desc_submitcount: Descriptor h/w submitted count
338 * @residue: Residue for AXI DMA
339 * @seg_v: Statically allocated segments base
340 * @seg_p: Physical allocated segments base
341 * @cyclic_seg_v: Statically allocated segment base for cyclic transfers
342 * @cyclic_seg_p: Physical allocated segments base for cyclic dma
343 * @start_transfer: Differentiate b/w DMA IP's transfer
344 * @stop_transfer: Differentiate b/w DMA IP's quiesce
345 * @tdest: TDEST value for mcdma
346 * @has_vflip: S2MM vertical flip
348 struct xilinx_dma_chan {
349 struct xilinx_dma_device *xdev;
353 struct list_head pending_list;
354 struct list_head active_list;
355 struct list_head done_list;
356 struct list_head free_seg_list;
357 struct dma_chan common;
358 struct dma_pool *desc_pool;
362 enum dma_transfer_direction direction;
369 struct tasklet_struct tasklet;
370 struct xilinx_vdma_config config;
372 u32 desc_pendingcount;
374 u32 desc_submitcount;
376 struct xilinx_axidma_tx_segment *seg_v;
378 struct xilinx_axidma_tx_segment *cyclic_seg_v;
379 dma_addr_t cyclic_seg_p;
380 void (*start_transfer)(struct xilinx_dma_chan *chan);
381 int (*stop_transfer)(struct xilinx_dma_chan *chan);
387 * enum xdma_ip_type - DMA IP type.
389 * @XDMA_TYPE_AXIDMA: Axi dma ip.
390 * @XDMA_TYPE_CDMA: Axi cdma ip.
391 * @XDMA_TYPE_VDMA: Axi vdma ip.
395 XDMA_TYPE_AXIDMA = 0,
400 struct xilinx_dma_config {
401 enum xdma_ip_type dmatype;
402 int (*clk_init)(struct platform_device *pdev, struct clk **axi_clk,
403 struct clk **tx_clk, struct clk **txs_clk,
404 struct clk **rx_clk, struct clk **rxs_clk);
408 * struct xilinx_dma_device - DMA device structure
409 * @regs: I/O mapped base address
410 * @dev: Device Structure
411 * @common: DMA device structure
412 * @chan: Driver specific DMA channel
413 * @has_sg: Specifies whether Scatter-Gather is present or not
414 * @mcdma: Specifies whether Multi-Channel is present or not
415 * @flush_on_fsync: Flush on frame sync
416 * @ext_addr: Indicates 64 bit addressing is supported by dma device
417 * @pdev: Platform device structure pointer
418 * @dma_config: DMA config structure
419 * @axi_clk: DMA Axi4-lite interace clock
420 * @tx_clk: DMA mm2s clock
421 * @txs_clk: DMA mm2s stream clock
422 * @rx_clk: DMA s2mm clock
423 * @rxs_clk: DMA s2mm stream clock
424 * @nr_channels: Number of channels DMA device supports
425 * @chan_id: DMA channel identifier
427 struct xilinx_dma_device {
430 struct dma_device common;
431 struct xilinx_dma_chan *chan[XILINX_DMA_MAX_CHANS_PER_DEVICE];
436 struct platform_device *pdev;
437 const struct xilinx_dma_config *dma_config;
448 #define to_xilinx_chan(chan) \
449 container_of(chan, struct xilinx_dma_chan, common)
450 #define to_dma_tx_descriptor(tx) \
451 container_of(tx, struct xilinx_dma_tx_descriptor, async_tx)
452 #define xilinx_dma_poll_timeout(chan, reg, val, cond, delay_us, timeout_us) \
453 readl_poll_timeout(chan->xdev->regs + chan->ctrl_offset + reg, val, \
454 cond, delay_us, timeout_us)
457 static inline u32 dma_read(struct xilinx_dma_chan *chan, u32 reg)
459 return ioread32(chan->xdev->regs + reg);
462 static inline void dma_write(struct xilinx_dma_chan *chan, u32 reg, u32 value)
464 iowrite32(value, chan->xdev->regs + reg);
467 static inline void vdma_desc_write(struct xilinx_dma_chan *chan, u32 reg,
470 dma_write(chan, chan->desc_offset + reg, value);
473 static inline u32 dma_ctrl_read(struct xilinx_dma_chan *chan, u32 reg)
475 return dma_read(chan, chan->ctrl_offset + reg);
478 static inline void dma_ctrl_write(struct xilinx_dma_chan *chan, u32 reg,
481 dma_write(chan, chan->ctrl_offset + reg, value);
484 static inline void dma_ctrl_clr(struct xilinx_dma_chan *chan, u32 reg,
487 dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) & ~clr);
490 static inline void dma_ctrl_set(struct xilinx_dma_chan *chan, u32 reg,
493 dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) | set);
497 * vdma_desc_write_64 - 64-bit descriptor write
498 * @chan: Driver specific VDMA channel
499 * @reg: Register to write
500 * @value_lsb: lower address of the descriptor.
501 * @value_msb: upper address of the descriptor.
503 * Since vdma driver is trying to write to a register offset which is not a
504 * multiple of 64 bits(ex : 0x5c), we are writing as two separate 32 bits
505 * instead of a single 64 bit register write.
507 static inline void vdma_desc_write_64(struct xilinx_dma_chan *chan, u32 reg,
508 u32 value_lsb, u32 value_msb)
510 /* Write the lsb 32 bits*/
511 writel(value_lsb, chan->xdev->regs + chan->desc_offset + reg);
513 /* Write the msb 32 bits */
514 writel(value_msb, chan->xdev->regs + chan->desc_offset + reg + 4);
517 static inline void dma_writeq(struct xilinx_dma_chan *chan, u32 reg, u64 value)
519 lo_hi_writeq(value, chan->xdev->regs + chan->ctrl_offset + reg);
522 static inline void xilinx_write(struct xilinx_dma_chan *chan, u32 reg,
526 dma_writeq(chan, reg, addr);
528 dma_ctrl_write(chan, reg, addr);
531 static inline void xilinx_axidma_buf(struct xilinx_dma_chan *chan,
532 struct xilinx_axidma_desc_hw *hw,
533 dma_addr_t buf_addr, size_t sg_used,
536 if (chan->ext_addr) {
537 hw->buf_addr = lower_32_bits(buf_addr + sg_used + period_len);
538 hw->buf_addr_msb = upper_32_bits(buf_addr + sg_used +
541 hw->buf_addr = buf_addr + sg_used + period_len;
545 /* -----------------------------------------------------------------------------
546 * Descriptors and segments alloc and free
550 * xilinx_vdma_alloc_tx_segment - Allocate transaction segment
551 * @chan: Driver specific DMA channel
553 * Return: The allocated segment on success and NULL on failure.
555 static struct xilinx_vdma_tx_segment *
556 xilinx_vdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
558 struct xilinx_vdma_tx_segment *segment;
561 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
565 segment->phys = phys;
571 * xilinx_cdma_alloc_tx_segment - Allocate transaction segment
572 * @chan: Driver specific DMA channel
574 * Return: The allocated segment on success and NULL on failure.
576 static struct xilinx_cdma_tx_segment *
577 xilinx_cdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
579 struct xilinx_cdma_tx_segment *segment;
582 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
586 segment->phys = phys;
592 * xilinx_axidma_alloc_tx_segment - Allocate transaction segment
593 * @chan: Driver specific DMA channel
595 * Return: The allocated segment on success and NULL on failure.
597 static struct xilinx_axidma_tx_segment *
598 xilinx_axidma_alloc_tx_segment(struct xilinx_dma_chan *chan)
600 struct xilinx_axidma_tx_segment *segment = NULL;
603 spin_lock_irqsave(&chan->lock, flags);
604 if (!list_empty(&chan->free_seg_list)) {
605 segment = list_first_entry(&chan->free_seg_list,
606 struct xilinx_axidma_tx_segment,
608 list_del(&segment->node);
610 spin_unlock_irqrestore(&chan->lock, flags);
615 static void xilinx_dma_clean_hw_desc(struct xilinx_axidma_desc_hw *hw)
617 u32 next_desc = hw->next_desc;
618 u32 next_desc_msb = hw->next_desc_msb;
620 memset(hw, 0, sizeof(struct xilinx_axidma_desc_hw));
622 hw->next_desc = next_desc;
623 hw->next_desc_msb = next_desc_msb;
627 * xilinx_dma_free_tx_segment - Free transaction segment
628 * @chan: Driver specific DMA channel
629 * @segment: DMA transaction segment
631 static void xilinx_dma_free_tx_segment(struct xilinx_dma_chan *chan,
632 struct xilinx_axidma_tx_segment *segment)
634 xilinx_dma_clean_hw_desc(&segment->hw);
636 list_add_tail(&segment->node, &chan->free_seg_list);
640 * xilinx_cdma_free_tx_segment - Free transaction segment
641 * @chan: Driver specific DMA channel
642 * @segment: DMA transaction segment
644 static void xilinx_cdma_free_tx_segment(struct xilinx_dma_chan *chan,
645 struct xilinx_cdma_tx_segment *segment)
647 dma_pool_free(chan->desc_pool, segment, segment->phys);
651 * xilinx_vdma_free_tx_segment - Free transaction segment
652 * @chan: Driver specific DMA channel
653 * @segment: DMA transaction segment
655 static void xilinx_vdma_free_tx_segment(struct xilinx_dma_chan *chan,
656 struct xilinx_vdma_tx_segment *segment)
658 dma_pool_free(chan->desc_pool, segment, segment->phys);
662 * xilinx_dma_tx_descriptor - Allocate transaction descriptor
663 * @chan: Driver specific DMA channel
665 * Return: The allocated descriptor on success and NULL on failure.
667 static struct xilinx_dma_tx_descriptor *
668 xilinx_dma_alloc_tx_descriptor(struct xilinx_dma_chan *chan)
670 struct xilinx_dma_tx_descriptor *desc;
672 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
676 INIT_LIST_HEAD(&desc->segments);
682 * xilinx_dma_free_tx_descriptor - Free transaction descriptor
683 * @chan: Driver specific DMA channel
684 * @desc: DMA transaction descriptor
687 xilinx_dma_free_tx_descriptor(struct xilinx_dma_chan *chan,
688 struct xilinx_dma_tx_descriptor *desc)
690 struct xilinx_vdma_tx_segment *segment, *next;
691 struct xilinx_cdma_tx_segment *cdma_segment, *cdma_next;
692 struct xilinx_axidma_tx_segment *axidma_segment, *axidma_next;
697 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
698 list_for_each_entry_safe(segment, next, &desc->segments, node) {
699 list_del(&segment->node);
700 xilinx_vdma_free_tx_segment(chan, segment);
702 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
703 list_for_each_entry_safe(cdma_segment, cdma_next,
704 &desc->segments, node) {
705 list_del(&cdma_segment->node);
706 xilinx_cdma_free_tx_segment(chan, cdma_segment);
709 list_for_each_entry_safe(axidma_segment, axidma_next,
710 &desc->segments, node) {
711 list_del(&axidma_segment->node);
712 xilinx_dma_free_tx_segment(chan, axidma_segment);
719 /* Required functions */
722 * xilinx_dma_free_desc_list - Free descriptors list
723 * @chan: Driver specific DMA channel
724 * @list: List to parse and delete the descriptor
726 static void xilinx_dma_free_desc_list(struct xilinx_dma_chan *chan,
727 struct list_head *list)
729 struct xilinx_dma_tx_descriptor *desc, *next;
731 list_for_each_entry_safe(desc, next, list, node) {
732 list_del(&desc->node);
733 xilinx_dma_free_tx_descriptor(chan, desc);
738 * xilinx_dma_free_descriptors - Free channel descriptors
739 * @chan: Driver specific DMA channel
741 static void xilinx_dma_free_descriptors(struct xilinx_dma_chan *chan)
745 spin_lock_irqsave(&chan->lock, flags);
747 xilinx_dma_free_desc_list(chan, &chan->pending_list);
748 xilinx_dma_free_desc_list(chan, &chan->done_list);
749 xilinx_dma_free_desc_list(chan, &chan->active_list);
751 spin_unlock_irqrestore(&chan->lock, flags);
755 * xilinx_dma_free_chan_resources - Free channel resources
756 * @dchan: DMA channel
758 static void xilinx_dma_free_chan_resources(struct dma_chan *dchan)
760 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
763 dev_dbg(chan->dev, "Free all channel resources.\n");
765 xilinx_dma_free_descriptors(chan);
767 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
768 spin_lock_irqsave(&chan->lock, flags);
769 INIT_LIST_HEAD(&chan->free_seg_list);
770 spin_unlock_irqrestore(&chan->lock, flags);
772 /* Free memory that is allocated for BD */
773 dma_free_coherent(chan->dev, sizeof(*chan->seg_v) *
774 XILINX_DMA_NUM_DESCS, chan->seg_v,
777 /* Free Memory that is allocated for cyclic DMA Mode */
778 dma_free_coherent(chan->dev, sizeof(*chan->cyclic_seg_v),
779 chan->cyclic_seg_v, chan->cyclic_seg_p);
782 if (chan->xdev->dma_config->dmatype != XDMA_TYPE_AXIDMA) {
783 dma_pool_destroy(chan->desc_pool);
784 chan->desc_pool = NULL;
789 * xilinx_dma_chan_handle_cyclic - Cyclic dma callback
790 * @chan: Driver specific dma channel
791 * @desc: dma transaction descriptor
792 * @flags: flags for spin lock
794 static void xilinx_dma_chan_handle_cyclic(struct xilinx_dma_chan *chan,
795 struct xilinx_dma_tx_descriptor *desc,
796 unsigned long *flags)
798 dma_async_tx_callback callback;
799 void *callback_param;
801 callback = desc->async_tx.callback;
802 callback_param = desc->async_tx.callback_param;
804 spin_unlock_irqrestore(&chan->lock, *flags);
805 callback(callback_param);
806 spin_lock_irqsave(&chan->lock, *flags);
811 * xilinx_dma_chan_desc_cleanup - Clean channel descriptors
812 * @chan: Driver specific DMA channel
814 static void xilinx_dma_chan_desc_cleanup(struct xilinx_dma_chan *chan)
816 struct xilinx_dma_tx_descriptor *desc, *next;
819 spin_lock_irqsave(&chan->lock, flags);
821 list_for_each_entry_safe(desc, next, &chan->done_list, node) {
822 struct dmaengine_desc_callback cb;
825 xilinx_dma_chan_handle_cyclic(chan, desc, &flags);
829 /* Remove from the list of running transactions */
830 list_del(&desc->node);
832 /* Run the link descriptor callback function */
833 dmaengine_desc_get_callback(&desc->async_tx, &cb);
834 if (dmaengine_desc_callback_valid(&cb)) {
835 spin_unlock_irqrestore(&chan->lock, flags);
836 dmaengine_desc_callback_invoke(&cb, NULL);
837 spin_lock_irqsave(&chan->lock, flags);
840 /* Run any dependencies, then free the descriptor */
841 dma_run_dependencies(&desc->async_tx);
842 xilinx_dma_free_tx_descriptor(chan, desc);
845 spin_unlock_irqrestore(&chan->lock, flags);
849 * xilinx_dma_do_tasklet - Schedule completion tasklet
850 * @data: Pointer to the Xilinx DMA channel structure
852 static void xilinx_dma_do_tasklet(unsigned long data)
854 struct xilinx_dma_chan *chan = (struct xilinx_dma_chan *)data;
856 xilinx_dma_chan_desc_cleanup(chan);
860 * xilinx_dma_alloc_chan_resources - Allocate channel resources
861 * @dchan: DMA channel
863 * Return: '0' on success and failure value on error
865 static int xilinx_dma_alloc_chan_resources(struct dma_chan *dchan)
867 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
870 /* Has this channel already been allocated? */
875 * We need the descriptor to be aligned to 64bytes
876 * for meeting Xilinx VDMA specification requirement.
878 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
879 /* Allocate the buffer descriptors. */
880 chan->seg_v = dma_zalloc_coherent(chan->dev,
881 sizeof(*chan->seg_v) *
882 XILINX_DMA_NUM_DESCS,
883 &chan->seg_p, GFP_KERNEL);
886 "unable to allocate channel %d descriptors\n",
891 for (i = 0; i < XILINX_DMA_NUM_DESCS; i++) {
892 chan->seg_v[i].hw.next_desc =
893 lower_32_bits(chan->seg_p + sizeof(*chan->seg_v) *
894 ((i + 1) % XILINX_DMA_NUM_DESCS));
895 chan->seg_v[i].hw.next_desc_msb =
896 upper_32_bits(chan->seg_p + sizeof(*chan->seg_v) *
897 ((i + 1) % XILINX_DMA_NUM_DESCS));
898 chan->seg_v[i].phys = chan->seg_p +
899 sizeof(*chan->seg_v) * i;
900 list_add_tail(&chan->seg_v[i].node,
901 &chan->free_seg_list);
903 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
904 chan->desc_pool = dma_pool_create("xilinx_cdma_desc_pool",
906 sizeof(struct xilinx_cdma_tx_segment),
907 __alignof__(struct xilinx_cdma_tx_segment),
910 chan->desc_pool = dma_pool_create("xilinx_vdma_desc_pool",
912 sizeof(struct xilinx_vdma_tx_segment),
913 __alignof__(struct xilinx_vdma_tx_segment),
917 if (!chan->desc_pool &&
918 (chan->xdev->dma_config->dmatype != XDMA_TYPE_AXIDMA)) {
920 "unable to allocate channel %d descriptor pool\n",
925 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
927 * For cyclic DMA mode we need to program the tail Descriptor
928 * register with a value which is not a part of the BD chain
929 * so allocating a desc segment during channel allocation for
930 * programming tail descriptor.
932 chan->cyclic_seg_v = dma_zalloc_coherent(chan->dev,
933 sizeof(*chan->cyclic_seg_v),
934 &chan->cyclic_seg_p, GFP_KERNEL);
935 if (!chan->cyclic_seg_v) {
937 "unable to allocate desc segment for cyclic DMA\n");
940 chan->cyclic_seg_v->phys = chan->cyclic_seg_p;
943 dma_cookie_init(dchan);
945 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
946 /* For AXI DMA resetting once channel will reset the
947 * other channel as well so enable the interrupts here.
949 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
950 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
953 if ((chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) && chan->has_sg)
954 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
955 XILINX_CDMA_CR_SGMODE);
961 * xilinx_dma_tx_status - Get DMA transaction status
962 * @dchan: DMA channel
963 * @cookie: Transaction identifier
964 * @txstate: Transaction state
966 * Return: DMA transaction status
968 static enum dma_status xilinx_dma_tx_status(struct dma_chan *dchan,
970 struct dma_tx_state *txstate)
972 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
973 struct xilinx_dma_tx_descriptor *desc;
974 struct xilinx_axidma_tx_segment *segment;
975 struct xilinx_axidma_desc_hw *hw;
980 ret = dma_cookie_status(dchan, cookie, txstate);
981 if (ret == DMA_COMPLETE || !txstate)
984 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
985 spin_lock_irqsave(&chan->lock, flags);
987 desc = list_last_entry(&chan->active_list,
988 struct xilinx_dma_tx_descriptor, node);
990 list_for_each_entry(segment, &desc->segments, node) {
992 residue += (hw->control - hw->status) &
993 XILINX_DMA_MAX_TRANS_LEN;
996 spin_unlock_irqrestore(&chan->lock, flags);
998 chan->residue = residue;
999 dma_set_residue(txstate, chan->residue);
1006 * xilinx_dma_stop_transfer - Halt DMA channel
1007 * @chan: Driver specific DMA channel
1009 * Return: '0' on success and failure value on error
1011 static int xilinx_dma_stop_transfer(struct xilinx_dma_chan *chan)
1015 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
1017 /* Wait for the hardware to halt */
1018 return xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
1019 val & XILINX_DMA_DMASR_HALTED, 0,
1020 XILINX_DMA_LOOP_COUNT);
1024 * xilinx_cdma_stop_transfer - Wait for the current transfer to complete
1025 * @chan: Driver specific DMA channel
1027 * Return: '0' on success and failure value on error
1029 static int xilinx_cdma_stop_transfer(struct xilinx_dma_chan *chan)
1033 return xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
1034 val & XILINX_DMA_DMASR_IDLE, 0,
1035 XILINX_DMA_LOOP_COUNT);
1039 * xilinx_dma_start - Start DMA channel
1040 * @chan: Driver specific DMA channel
1042 static void xilinx_dma_start(struct xilinx_dma_chan *chan)
1047 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
1049 /* Wait for the hardware to start */
1050 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
1051 !(val & XILINX_DMA_DMASR_HALTED), 0,
1052 XILINX_DMA_LOOP_COUNT);
1055 dev_err(chan->dev, "Cannot start channel %p: %x\n",
1056 chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
1063 * xilinx_vdma_start_transfer - Starts VDMA transfer
1064 * @chan: Driver specific channel struct pointer
1066 static void xilinx_vdma_start_transfer(struct xilinx_dma_chan *chan)
1068 struct xilinx_vdma_config *config = &chan->config;
1069 struct xilinx_dma_tx_descriptor *desc, *tail_desc;
1071 struct xilinx_vdma_tx_segment *tail_segment;
1073 /* This function was invoked with lock held */
1080 if (list_empty(&chan->pending_list))
1083 desc = list_first_entry(&chan->pending_list,
1084 struct xilinx_dma_tx_descriptor, node);
1085 tail_desc = list_last_entry(&chan->pending_list,
1086 struct xilinx_dma_tx_descriptor, node);
1088 tail_segment = list_last_entry(&tail_desc->segments,
1089 struct xilinx_vdma_tx_segment, node);
1092 * If hardware is idle, then all descriptors on the running lists are
1093 * done, start new transfers
1096 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
1097 desc->async_tx.phys);
1099 /* Configure the hardware using info in the config structure */
1100 if (chan->has_vflip) {
1101 reg = dma_read(chan, XILINX_VDMA_REG_ENABLE_VERTICAL_FLIP);
1102 reg &= ~XILINX_VDMA_ENABLE_VERTICAL_FLIP;
1103 reg |= config->vflip_en;
1104 dma_write(chan, XILINX_VDMA_REG_ENABLE_VERTICAL_FLIP,
1108 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1110 if (config->frm_cnt_en)
1111 reg |= XILINX_DMA_DMACR_FRAMECNT_EN;
1113 reg &= ~XILINX_DMA_DMACR_FRAMECNT_EN;
1116 * With SG, start with circular mode, so that BDs can be fetched.
1117 * In direct register mode, if not parking, enable circular mode
1119 if (chan->has_sg || !config->park)
1120 reg |= XILINX_DMA_DMACR_CIRC_EN;
1123 reg &= ~XILINX_DMA_DMACR_CIRC_EN;
1125 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1127 j = chan->desc_submitcount;
1128 reg = dma_read(chan, XILINX_DMA_REG_PARK_PTR);
1129 if (chan->direction == DMA_MEM_TO_DEV) {
1130 reg &= ~XILINX_DMA_PARK_PTR_RD_REF_MASK;
1131 reg |= j << XILINX_DMA_PARK_PTR_RD_REF_SHIFT;
1133 reg &= ~XILINX_DMA_PARK_PTR_WR_REF_MASK;
1134 reg |= j << XILINX_DMA_PARK_PTR_WR_REF_SHIFT;
1136 dma_write(chan, XILINX_DMA_REG_PARK_PTR, reg);
1138 /* Start the hardware */
1139 xilinx_dma_start(chan);
1144 /* Start the transfer */
1146 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
1147 tail_segment->phys);
1148 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1149 chan->desc_pendingcount = 0;
1151 struct xilinx_vdma_tx_segment *segment, *last = NULL;
1154 if (chan->desc_submitcount < chan->num_frms)
1155 i = chan->desc_submitcount;
1157 list_for_each_entry(segment, &desc->segments, node) {
1159 vdma_desc_write_64(chan,
1160 XILINX_VDMA_REG_START_ADDRESS_64(i++),
1161 segment->hw.buf_addr,
1162 segment->hw.buf_addr_msb);
1164 vdma_desc_write(chan,
1165 XILINX_VDMA_REG_START_ADDRESS(i++),
1166 segment->hw.buf_addr);
1174 /* HW expects these parameters to be same for one transaction */
1175 vdma_desc_write(chan, XILINX_DMA_REG_HSIZE, last->hw.hsize);
1176 vdma_desc_write(chan, XILINX_DMA_REG_FRMDLY_STRIDE,
1178 vdma_desc_write(chan, XILINX_DMA_REG_VSIZE, last->hw.vsize);
1180 chan->desc_submitcount++;
1181 chan->desc_pendingcount--;
1182 list_del(&desc->node);
1183 list_add_tail(&desc->node, &chan->active_list);
1184 if (chan->desc_submitcount == chan->num_frms)
1185 chan->desc_submitcount = 0;
1192 * xilinx_cdma_start_transfer - Starts cdma transfer
1193 * @chan: Driver specific channel struct pointer
1195 static void xilinx_cdma_start_transfer(struct xilinx_dma_chan *chan)
1197 struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1198 struct xilinx_cdma_tx_segment *tail_segment;
1199 u32 ctrl_reg = dma_read(chan, XILINX_DMA_REG_DMACR);
1207 if (list_empty(&chan->pending_list))
1210 head_desc = list_first_entry(&chan->pending_list,
1211 struct xilinx_dma_tx_descriptor, node);
1212 tail_desc = list_last_entry(&chan->pending_list,
1213 struct xilinx_dma_tx_descriptor, node);
1214 tail_segment = list_last_entry(&tail_desc->segments,
1215 struct xilinx_cdma_tx_segment, node);
1217 if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1218 ctrl_reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1219 ctrl_reg |= chan->desc_pendingcount <<
1220 XILINX_DMA_CR_COALESCE_SHIFT;
1221 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, ctrl_reg);
1225 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
1226 XILINX_CDMA_CR_SGMODE);
1228 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
1229 XILINX_CDMA_CR_SGMODE);
1231 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1232 head_desc->async_tx.phys);
1234 /* Update tail ptr register which will start the transfer */
1235 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1236 tail_segment->phys);
1238 /* In simple mode */
1239 struct xilinx_cdma_tx_segment *segment;
1240 struct xilinx_cdma_desc_hw *hw;
1242 segment = list_first_entry(&head_desc->segments,
1243 struct xilinx_cdma_tx_segment,
1248 xilinx_write(chan, XILINX_CDMA_REG_SRCADDR, hw->src_addr);
1249 xilinx_write(chan, XILINX_CDMA_REG_DSTADDR, hw->dest_addr);
1251 /* Start the transfer */
1252 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1253 hw->control & XILINX_DMA_MAX_TRANS_LEN);
1256 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1257 chan->desc_pendingcount = 0;
1262 * xilinx_dma_start_transfer - Starts DMA transfer
1263 * @chan: Driver specific channel struct pointer
1265 static void xilinx_dma_start_transfer(struct xilinx_dma_chan *chan)
1267 struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1268 struct xilinx_axidma_tx_segment *tail_segment;
1274 if (list_empty(&chan->pending_list))
1280 head_desc = list_first_entry(&chan->pending_list,
1281 struct xilinx_dma_tx_descriptor, node);
1282 tail_desc = list_last_entry(&chan->pending_list,
1283 struct xilinx_dma_tx_descriptor, node);
1284 tail_segment = list_last_entry(&tail_desc->segments,
1285 struct xilinx_axidma_tx_segment, node);
1287 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1289 if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1290 reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1291 reg |= chan->desc_pendingcount <<
1292 XILINX_DMA_CR_COALESCE_SHIFT;
1293 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1296 if (chan->has_sg && !chan->xdev->mcdma)
1297 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1298 head_desc->async_tx.phys);
1300 if (chan->has_sg && chan->xdev->mcdma) {
1301 if (chan->direction == DMA_MEM_TO_DEV) {
1302 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
1303 head_desc->async_tx.phys);
1306 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
1307 head_desc->async_tx.phys);
1309 dma_ctrl_write(chan,
1310 XILINX_DMA_MCRX_CDESC(chan->tdest),
1311 head_desc->async_tx.phys);
1316 xilinx_dma_start(chan);
1321 /* Start the transfer */
1322 if (chan->has_sg && !chan->xdev->mcdma) {
1324 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1325 chan->cyclic_seg_v->phys);
1327 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1328 tail_segment->phys);
1329 } else if (chan->has_sg && chan->xdev->mcdma) {
1330 if (chan->direction == DMA_MEM_TO_DEV) {
1331 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
1332 tail_segment->phys);
1335 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
1336 tail_segment->phys);
1338 dma_ctrl_write(chan,
1339 XILINX_DMA_MCRX_TDESC(chan->tdest),
1340 tail_segment->phys);
1344 struct xilinx_axidma_tx_segment *segment;
1345 struct xilinx_axidma_desc_hw *hw;
1347 segment = list_first_entry(&head_desc->segments,
1348 struct xilinx_axidma_tx_segment,
1352 xilinx_write(chan, XILINX_DMA_REG_SRCDSTADDR, hw->buf_addr);
1354 /* Start the transfer */
1355 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1356 hw->control & XILINX_DMA_MAX_TRANS_LEN);
1359 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1360 chan->desc_pendingcount = 0;
1365 * xilinx_dma_issue_pending - Issue pending transactions
1366 * @dchan: DMA channel
1368 static void xilinx_dma_issue_pending(struct dma_chan *dchan)
1370 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1371 unsigned long flags;
1373 spin_lock_irqsave(&chan->lock, flags);
1374 chan->start_transfer(chan);
1375 spin_unlock_irqrestore(&chan->lock, flags);
1379 * xilinx_dma_complete_descriptor - Mark the active descriptor as complete
1380 * @chan : xilinx DMA channel
1384 static void xilinx_dma_complete_descriptor(struct xilinx_dma_chan *chan)
1386 struct xilinx_dma_tx_descriptor *desc, *next;
1388 /* This function was invoked with lock held */
1389 if (list_empty(&chan->active_list))
1392 list_for_each_entry_safe(desc, next, &chan->active_list, node) {
1393 list_del(&desc->node);
1395 dma_cookie_complete(&desc->async_tx);
1396 list_add_tail(&desc->node, &chan->done_list);
1401 * xilinx_dma_reset - Reset DMA channel
1402 * @chan: Driver specific DMA channel
1404 * Return: '0' on success and failure value on error
1406 static int xilinx_dma_reset(struct xilinx_dma_chan *chan)
1411 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RESET);
1413 /* Wait for the hardware to finish reset */
1414 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMACR, tmp,
1415 !(tmp & XILINX_DMA_DMACR_RESET), 0,
1416 XILINX_DMA_LOOP_COUNT);
1419 dev_err(chan->dev, "reset timeout, cr %x, sr %x\n",
1420 dma_ctrl_read(chan, XILINX_DMA_REG_DMACR),
1421 dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
1427 chan->desc_submitcount = 0;
1433 * xilinx_dma_chan_reset - Reset DMA channel and enable interrupts
1434 * @chan: Driver specific DMA channel
1436 * Return: '0' on success and failure value on error
1438 static int xilinx_dma_chan_reset(struct xilinx_dma_chan *chan)
1443 err = xilinx_dma_reset(chan);
1447 /* Enable interrupts */
1448 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
1449 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
1455 * xilinx_dma_irq_handler - DMA Interrupt handler
1457 * @data: Pointer to the Xilinx DMA channel structure
1459 * Return: IRQ_HANDLED/IRQ_NONE
1461 static irqreturn_t xilinx_dma_irq_handler(int irq, void *data)
1463 struct xilinx_dma_chan *chan = data;
1466 /* Read the status and ack the interrupts. */
1467 status = dma_ctrl_read(chan, XILINX_DMA_REG_DMASR);
1468 if (!(status & XILINX_DMA_DMAXR_ALL_IRQ_MASK))
1471 dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1472 status & XILINX_DMA_DMAXR_ALL_IRQ_MASK);
1474 if (status & XILINX_DMA_DMASR_ERR_IRQ) {
1476 * An error occurred. If C_FLUSH_ON_FSYNC is enabled and the
1477 * error is recoverable, ignore it. Otherwise flag the error.
1479 * Only recoverable errors can be cleared in the DMASR register,
1480 * make sure not to write to other error bits to 1.
1482 u32 errors = status & XILINX_DMA_DMASR_ALL_ERR_MASK;
1484 dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1485 errors & XILINX_DMA_DMASR_ERR_RECOVER_MASK);
1487 if (!chan->flush_on_fsync ||
1488 (errors & ~XILINX_DMA_DMASR_ERR_RECOVER_MASK)) {
1490 "Channel %p has errors %x, cdr %x tdr %x\n",
1492 dma_ctrl_read(chan, XILINX_DMA_REG_CURDESC),
1493 dma_ctrl_read(chan, XILINX_DMA_REG_TAILDESC));
1498 if (status & XILINX_DMA_DMASR_DLY_CNT_IRQ) {
1500 * Device takes too long to do the transfer when user requires
1503 dev_dbg(chan->dev, "Inter-packet latency too long\n");
1506 if (status & XILINX_DMA_DMASR_FRM_CNT_IRQ) {
1507 spin_lock(&chan->lock);
1508 xilinx_dma_complete_descriptor(chan);
1510 chan->start_transfer(chan);
1511 spin_unlock(&chan->lock);
1514 tasklet_schedule(&chan->tasklet);
1519 * append_desc_queue - Queuing descriptor
1520 * @chan: Driver specific dma channel
1521 * @desc: dma transaction descriptor
1523 static void append_desc_queue(struct xilinx_dma_chan *chan,
1524 struct xilinx_dma_tx_descriptor *desc)
1526 struct xilinx_vdma_tx_segment *tail_segment;
1527 struct xilinx_dma_tx_descriptor *tail_desc;
1528 struct xilinx_axidma_tx_segment *axidma_tail_segment;
1529 struct xilinx_cdma_tx_segment *cdma_tail_segment;
1531 if (list_empty(&chan->pending_list))
1535 * Add the hardware descriptor to the chain of hardware descriptors
1536 * that already exists in memory.
1538 tail_desc = list_last_entry(&chan->pending_list,
1539 struct xilinx_dma_tx_descriptor, node);
1540 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
1541 tail_segment = list_last_entry(&tail_desc->segments,
1542 struct xilinx_vdma_tx_segment,
1544 tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1545 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
1546 cdma_tail_segment = list_last_entry(&tail_desc->segments,
1547 struct xilinx_cdma_tx_segment,
1549 cdma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1551 axidma_tail_segment = list_last_entry(&tail_desc->segments,
1552 struct xilinx_axidma_tx_segment,
1554 axidma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1558 * Add the software descriptor and all children to the list
1559 * of pending transactions
1562 list_add_tail(&desc->node, &chan->pending_list);
1563 chan->desc_pendingcount++;
1565 if (chan->has_sg && (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA)
1566 && unlikely(chan->desc_pendingcount > chan->num_frms)) {
1567 dev_dbg(chan->dev, "desc pendingcount is too high\n");
1568 chan->desc_pendingcount = chan->num_frms;
1573 * xilinx_dma_tx_submit - Submit DMA transaction
1574 * @tx: Async transaction descriptor
1576 * Return: cookie value on success and failure value on error
1578 static dma_cookie_t xilinx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
1580 struct xilinx_dma_tx_descriptor *desc = to_dma_tx_descriptor(tx);
1581 struct xilinx_dma_chan *chan = to_xilinx_chan(tx->chan);
1582 dma_cookie_t cookie;
1583 unsigned long flags;
1587 xilinx_dma_free_tx_descriptor(chan, desc);
1593 * If reset fails, need to hard reset the system.
1594 * Channel is no longer functional
1596 err = xilinx_dma_chan_reset(chan);
1601 spin_lock_irqsave(&chan->lock, flags);
1603 cookie = dma_cookie_assign(tx);
1605 /* Put this transaction onto the tail of the pending queue */
1606 append_desc_queue(chan, desc);
1609 chan->cyclic = true;
1611 spin_unlock_irqrestore(&chan->lock, flags);
1617 * xilinx_vdma_dma_prep_interleaved - prepare a descriptor for a
1618 * DMA_SLAVE transaction
1619 * @dchan: DMA channel
1620 * @xt: Interleaved template pointer
1621 * @flags: transfer ack flags
1623 * Return: Async transaction descriptor on success and NULL on failure
1625 static struct dma_async_tx_descriptor *
1626 xilinx_vdma_dma_prep_interleaved(struct dma_chan *dchan,
1627 struct dma_interleaved_template *xt,
1628 unsigned long flags)
1630 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1631 struct xilinx_dma_tx_descriptor *desc;
1632 struct xilinx_vdma_tx_segment *segment;
1633 struct xilinx_vdma_desc_hw *hw;
1635 if (!is_slave_direction(xt->dir))
1638 if (!xt->numf || !xt->sgl[0].size)
1641 if (xt->frame_size != 1)
1644 /* Allocate a transaction descriptor. */
1645 desc = xilinx_dma_alloc_tx_descriptor(chan);
1649 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1650 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1651 async_tx_ack(&desc->async_tx);
1653 /* Allocate the link descriptor from DMA pool */
1654 segment = xilinx_vdma_alloc_tx_segment(chan);
1658 /* Fill in the hardware descriptor */
1660 hw->vsize = xt->numf;
1661 hw->hsize = xt->sgl[0].size;
1662 hw->stride = (xt->sgl[0].icg + xt->sgl[0].size) <<
1663 XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT;
1664 hw->stride |= chan->config.frm_dly <<
1665 XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT;
1667 if (xt->dir != DMA_MEM_TO_DEV) {
1668 if (chan->ext_addr) {
1669 hw->buf_addr = lower_32_bits(xt->dst_start);
1670 hw->buf_addr_msb = upper_32_bits(xt->dst_start);
1672 hw->buf_addr = xt->dst_start;
1675 if (chan->ext_addr) {
1676 hw->buf_addr = lower_32_bits(xt->src_start);
1677 hw->buf_addr_msb = upper_32_bits(xt->src_start);
1679 hw->buf_addr = xt->src_start;
1683 /* Insert the segment into the descriptor segments list. */
1684 list_add_tail(&segment->node, &desc->segments);
1686 /* Link the last hardware descriptor with the first. */
1687 segment = list_first_entry(&desc->segments,
1688 struct xilinx_vdma_tx_segment, node);
1689 desc->async_tx.phys = segment->phys;
1691 return &desc->async_tx;
1694 xilinx_dma_free_tx_descriptor(chan, desc);
1699 * xilinx_cdma_prep_memcpy - prepare descriptors for a memcpy transaction
1700 * @dchan: DMA channel
1701 * @dma_dst: destination address
1702 * @dma_src: source address
1703 * @len: transfer length
1704 * @flags: transfer ack flags
1706 * Return: Async transaction descriptor on success and NULL on failure
1708 static struct dma_async_tx_descriptor *
1709 xilinx_cdma_prep_memcpy(struct dma_chan *dchan, dma_addr_t dma_dst,
1710 dma_addr_t dma_src, size_t len, unsigned long flags)
1712 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1713 struct xilinx_dma_tx_descriptor *desc;
1714 struct xilinx_cdma_tx_segment *segment;
1715 struct xilinx_cdma_desc_hw *hw;
1717 if (!len || len > XILINX_DMA_MAX_TRANS_LEN)
1720 desc = xilinx_dma_alloc_tx_descriptor(chan);
1724 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1725 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1727 /* Allocate the link descriptor from DMA pool */
1728 segment = xilinx_cdma_alloc_tx_segment(chan);
1734 hw->src_addr = dma_src;
1735 hw->dest_addr = dma_dst;
1736 if (chan->ext_addr) {
1737 hw->src_addr_msb = upper_32_bits(dma_src);
1738 hw->dest_addr_msb = upper_32_bits(dma_dst);
1741 /* Insert the segment into the descriptor segments list. */
1742 list_add_tail(&segment->node, &desc->segments);
1744 desc->async_tx.phys = segment->phys;
1745 hw->next_desc = segment->phys;
1747 return &desc->async_tx;
1750 xilinx_dma_free_tx_descriptor(chan, desc);
1755 * xilinx_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
1756 * @dchan: DMA channel
1757 * @sgl: scatterlist to transfer to/from
1758 * @sg_len: number of entries in @scatterlist
1759 * @direction: DMA direction
1760 * @flags: transfer ack flags
1761 * @context: APP words of the descriptor
1763 * Return: Async transaction descriptor on success and NULL on failure
1765 static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
1766 struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
1767 enum dma_transfer_direction direction, unsigned long flags,
1770 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1771 struct xilinx_dma_tx_descriptor *desc;
1772 struct xilinx_axidma_tx_segment *segment = NULL;
1773 u32 *app_w = (u32 *)context;
1774 struct scatterlist *sg;
1779 if (!is_slave_direction(direction))
1782 /* Allocate a transaction descriptor. */
1783 desc = xilinx_dma_alloc_tx_descriptor(chan);
1787 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1788 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1790 /* Build transactions using information in the scatter gather list */
1791 for_each_sg(sgl, sg, sg_len, i) {
1794 /* Loop until the entire scatterlist entry is used */
1795 while (sg_used < sg_dma_len(sg)) {
1796 struct xilinx_axidma_desc_hw *hw;
1798 /* Get a free segment */
1799 segment = xilinx_axidma_alloc_tx_segment(chan);
1804 * Calculate the maximum number of bytes to transfer,
1805 * making sure it is less than the hw limit
1807 copy = min_t(size_t, sg_dma_len(sg) - sg_used,
1808 XILINX_DMA_MAX_TRANS_LEN);
1811 /* Fill in the descriptor */
1812 xilinx_axidma_buf(chan, hw, sg_dma_address(sg),
1817 if (chan->direction == DMA_MEM_TO_DEV) {
1819 memcpy(hw->app, app_w, sizeof(u32) *
1820 XILINX_DMA_NUM_APP_WORDS);
1826 * Insert the segment into the descriptor segments
1829 list_add_tail(&segment->node, &desc->segments);
1833 segment = list_first_entry(&desc->segments,
1834 struct xilinx_axidma_tx_segment, node);
1835 desc->async_tx.phys = segment->phys;
1837 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1838 if (chan->direction == DMA_MEM_TO_DEV) {
1839 segment->hw.control |= XILINX_DMA_BD_SOP;
1840 segment = list_last_entry(&desc->segments,
1841 struct xilinx_axidma_tx_segment,
1843 segment->hw.control |= XILINX_DMA_BD_EOP;
1846 return &desc->async_tx;
1849 xilinx_dma_free_tx_descriptor(chan, desc);
1854 * xilinx_dma_prep_dma_cyclic - prepare descriptors for a DMA_SLAVE transaction
1855 * @dchan: DMA channel
1856 * @buf_addr: Physical address of the buffer
1857 * @buf_len: Total length of the cyclic buffers
1858 * @period_len: length of individual cyclic buffer
1859 * @direction: DMA direction
1860 * @flags: transfer ack flags
1862 * Return: Async transaction descriptor on success and NULL on failure
1864 static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
1865 struct dma_chan *dchan, dma_addr_t buf_addr, size_t buf_len,
1866 size_t period_len, enum dma_transfer_direction direction,
1867 unsigned long flags)
1869 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1870 struct xilinx_dma_tx_descriptor *desc;
1871 struct xilinx_axidma_tx_segment *segment, *head_segment, *prev = NULL;
1872 size_t copy, sg_used;
1873 unsigned int num_periods;
1880 num_periods = buf_len / period_len;
1885 if (!is_slave_direction(direction))
1888 /* Allocate a transaction descriptor. */
1889 desc = xilinx_dma_alloc_tx_descriptor(chan);
1893 chan->direction = direction;
1894 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1895 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1897 for (i = 0; i < num_periods; ++i) {
1900 while (sg_used < period_len) {
1901 struct xilinx_axidma_desc_hw *hw;
1903 /* Get a free segment */
1904 segment = xilinx_axidma_alloc_tx_segment(chan);
1909 * Calculate the maximum number of bytes to transfer,
1910 * making sure it is less than the hw limit
1912 copy = min_t(size_t, period_len - sg_used,
1913 XILINX_DMA_MAX_TRANS_LEN);
1915 xilinx_axidma_buf(chan, hw, buf_addr, sg_used,
1920 prev->hw.next_desc = segment->phys;
1926 * Insert the segment into the descriptor segments
1929 list_add_tail(&segment->node, &desc->segments);
1933 head_segment = list_first_entry(&desc->segments,
1934 struct xilinx_axidma_tx_segment, node);
1935 desc->async_tx.phys = head_segment->phys;
1937 desc->cyclic = true;
1938 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1939 reg |= XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
1940 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1942 segment = list_last_entry(&desc->segments,
1943 struct xilinx_axidma_tx_segment,
1945 segment->hw.next_desc = (u32) head_segment->phys;
1947 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1948 if (direction == DMA_MEM_TO_DEV) {
1949 head_segment->hw.control |= XILINX_DMA_BD_SOP;
1950 segment->hw.control |= XILINX_DMA_BD_EOP;
1953 return &desc->async_tx;
1956 xilinx_dma_free_tx_descriptor(chan, desc);
1961 * xilinx_dma_prep_interleaved - prepare a descriptor for a
1962 * DMA_SLAVE transaction
1963 * @dchan: DMA channel
1964 * @xt: Interleaved template pointer
1965 * @flags: transfer ack flags
1967 * Return: Async transaction descriptor on success and NULL on failure
1969 static struct dma_async_tx_descriptor *
1970 xilinx_dma_prep_interleaved(struct dma_chan *dchan,
1971 struct dma_interleaved_template *xt,
1972 unsigned long flags)
1974 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1975 struct xilinx_dma_tx_descriptor *desc;
1976 struct xilinx_axidma_tx_segment *segment;
1977 struct xilinx_axidma_desc_hw *hw;
1979 if (!is_slave_direction(xt->dir))
1982 if (!xt->numf || !xt->sgl[0].size)
1985 if (xt->frame_size != 1)
1988 /* Allocate a transaction descriptor. */
1989 desc = xilinx_dma_alloc_tx_descriptor(chan);
1993 chan->direction = xt->dir;
1994 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1995 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1997 /* Get a free segment */
1998 segment = xilinx_axidma_alloc_tx_segment(chan);
2004 /* Fill in the descriptor */
2005 if (xt->dir != DMA_MEM_TO_DEV)
2006 hw->buf_addr = xt->dst_start;
2008 hw->buf_addr = xt->src_start;
2010 hw->mcdma_control = chan->tdest & XILINX_DMA_BD_TDEST_MASK;
2011 hw->vsize_stride = (xt->numf << XILINX_DMA_BD_VSIZE_SHIFT) &
2012 XILINX_DMA_BD_VSIZE_MASK;
2013 hw->vsize_stride |= (xt->sgl[0].icg + xt->sgl[0].size) &
2014 XILINX_DMA_BD_STRIDE_MASK;
2015 hw->control = xt->sgl[0].size & XILINX_DMA_BD_HSIZE_MASK;
2018 * Insert the segment into the descriptor segments
2021 list_add_tail(&segment->node, &desc->segments);
2024 segment = list_first_entry(&desc->segments,
2025 struct xilinx_axidma_tx_segment, node);
2026 desc->async_tx.phys = segment->phys;
2028 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
2029 if (xt->dir == DMA_MEM_TO_DEV) {
2030 segment->hw.control |= XILINX_DMA_BD_SOP;
2031 segment = list_last_entry(&desc->segments,
2032 struct xilinx_axidma_tx_segment,
2034 segment->hw.control |= XILINX_DMA_BD_EOP;
2037 return &desc->async_tx;
2040 xilinx_dma_free_tx_descriptor(chan, desc);
2045 * xilinx_dma_terminate_all - Halt the channel and free descriptors
2046 * @dchan: Driver specific DMA Channel pointer
2048 * Return: '0' always.
2050 static int xilinx_dma_terminate_all(struct dma_chan *dchan)
2052 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
2057 xilinx_dma_chan_reset(chan);
2059 err = chan->stop_transfer(chan);
2061 dev_err(chan->dev, "Cannot stop channel %p: %x\n",
2062 chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
2066 /* Remove and free all of the descriptors in the lists */
2067 xilinx_dma_free_descriptors(chan);
2071 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
2072 reg &= ~XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
2073 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
2074 chan->cyclic = false;
2077 if ((chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) && chan->has_sg)
2078 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
2079 XILINX_CDMA_CR_SGMODE);
2085 * xilinx_dma_channel_set_config - Configure VDMA channel
2086 * Run-time configuration for Axi VDMA, supports:
2087 * . halt the channel
2088 * . configure interrupt coalescing and inter-packet delay threshold
2089 * . start/stop parking
2092 * @dchan: DMA channel
2093 * @cfg: VDMA device configuration pointer
2095 * Return: '0' on success and failure value on error
2097 int xilinx_vdma_channel_set_config(struct dma_chan *dchan,
2098 struct xilinx_vdma_config *cfg)
2100 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
2104 return xilinx_dma_chan_reset(chan);
2106 dmacr = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
2108 chan->config.frm_dly = cfg->frm_dly;
2109 chan->config.park = cfg->park;
2111 /* genlock settings */
2112 chan->config.gen_lock = cfg->gen_lock;
2113 chan->config.master = cfg->master;
2115 if (cfg->gen_lock && chan->genlock) {
2116 dmacr |= XILINX_DMA_DMACR_GENLOCK_EN;
2117 dmacr |= cfg->master << XILINX_DMA_DMACR_MASTER_SHIFT;
2120 chan->config.frm_cnt_en = cfg->frm_cnt_en;
2121 chan->config.vflip_en = cfg->vflip_en;
2124 chan->config.park_frm = cfg->park_frm;
2126 chan->config.park_frm = -1;
2128 chan->config.coalesc = cfg->coalesc;
2129 chan->config.delay = cfg->delay;
2131 if (cfg->coalesc <= XILINX_DMA_DMACR_FRAME_COUNT_MAX) {
2132 dmacr |= cfg->coalesc << XILINX_DMA_DMACR_FRAME_COUNT_SHIFT;
2133 chan->config.coalesc = cfg->coalesc;
2136 if (cfg->delay <= XILINX_DMA_DMACR_DELAY_MAX) {
2137 dmacr |= cfg->delay << XILINX_DMA_DMACR_DELAY_SHIFT;
2138 chan->config.delay = cfg->delay;
2141 /* FSync Source selection */
2142 dmacr &= ~XILINX_DMA_DMACR_FSYNCSRC_MASK;
2143 dmacr |= cfg->ext_fsync << XILINX_DMA_DMACR_FSYNCSRC_SHIFT;
2145 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, dmacr);
2149 EXPORT_SYMBOL(xilinx_vdma_channel_set_config);
2151 /* -----------------------------------------------------------------------------
2156 * xilinx_dma_chan_remove - Per Channel remove function
2157 * @chan: Driver specific DMA channel
2159 static void xilinx_dma_chan_remove(struct xilinx_dma_chan *chan)
2161 /* Disable all interrupts */
2162 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
2163 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
2166 free_irq(chan->irq, chan);
2168 tasklet_kill(&chan->tasklet);
2170 list_del(&chan->common.device_node);
2173 static int axidma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2174 struct clk **tx_clk, struct clk **rx_clk,
2175 struct clk **sg_clk, struct clk **tmp_clk)
2181 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2182 if (IS_ERR(*axi_clk)) {
2183 err = PTR_ERR(*axi_clk);
2184 dev_err(&pdev->dev, "failed to get axi_aclk (%d)\n", err);
2188 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2189 if (IS_ERR(*tx_clk))
2192 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2193 if (IS_ERR(*rx_clk))
2196 *sg_clk = devm_clk_get(&pdev->dev, "m_axi_sg_aclk");
2197 if (IS_ERR(*sg_clk))
2200 err = clk_prepare_enable(*axi_clk);
2202 dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n", err);
2206 err = clk_prepare_enable(*tx_clk);
2208 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
2209 goto err_disable_axiclk;
2212 err = clk_prepare_enable(*rx_clk);
2214 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
2215 goto err_disable_txclk;
2218 err = clk_prepare_enable(*sg_clk);
2220 dev_err(&pdev->dev, "failed to enable sg_clk (%d)\n", err);
2221 goto err_disable_rxclk;
2227 clk_disable_unprepare(*rx_clk);
2229 clk_disable_unprepare(*tx_clk);
2231 clk_disable_unprepare(*axi_clk);
2236 static int axicdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2237 struct clk **dev_clk, struct clk **tmp_clk,
2238 struct clk **tmp1_clk, struct clk **tmp2_clk)
2246 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2247 if (IS_ERR(*axi_clk)) {
2248 err = PTR_ERR(*axi_clk);
2249 dev_err(&pdev->dev, "failed to get axi_clk (%d)\n", err);
2253 *dev_clk = devm_clk_get(&pdev->dev, "m_axi_aclk");
2254 if (IS_ERR(*dev_clk)) {
2255 err = PTR_ERR(*dev_clk);
2256 dev_err(&pdev->dev, "failed to get dev_clk (%d)\n", err);
2260 err = clk_prepare_enable(*axi_clk);
2262 dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n", err);
2266 err = clk_prepare_enable(*dev_clk);
2268 dev_err(&pdev->dev, "failed to enable dev_clk (%d)\n", err);
2269 goto err_disable_axiclk;
2275 clk_disable_unprepare(*axi_clk);
2280 static int axivdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2281 struct clk **tx_clk, struct clk **txs_clk,
2282 struct clk **rx_clk, struct clk **rxs_clk)
2286 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2287 if (IS_ERR(*axi_clk)) {
2288 err = PTR_ERR(*axi_clk);
2289 dev_err(&pdev->dev, "failed to get axi_aclk (%d)\n", err);
2293 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2294 if (IS_ERR(*tx_clk))
2297 *txs_clk = devm_clk_get(&pdev->dev, "m_axis_mm2s_aclk");
2298 if (IS_ERR(*txs_clk))
2301 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2302 if (IS_ERR(*rx_clk))
2305 *rxs_clk = devm_clk_get(&pdev->dev, "s_axis_s2mm_aclk");
2306 if (IS_ERR(*rxs_clk))
2309 err = clk_prepare_enable(*axi_clk);
2311 dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n", err);
2315 err = clk_prepare_enable(*tx_clk);
2317 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
2318 goto err_disable_axiclk;
2321 err = clk_prepare_enable(*txs_clk);
2323 dev_err(&pdev->dev, "failed to enable txs_clk (%d)\n", err);
2324 goto err_disable_txclk;
2327 err = clk_prepare_enable(*rx_clk);
2329 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
2330 goto err_disable_txsclk;
2333 err = clk_prepare_enable(*rxs_clk);
2335 dev_err(&pdev->dev, "failed to enable rxs_clk (%d)\n", err);
2336 goto err_disable_rxclk;
2342 clk_disable_unprepare(*rx_clk);
2344 clk_disable_unprepare(*txs_clk);
2346 clk_disable_unprepare(*tx_clk);
2348 clk_disable_unprepare(*axi_clk);
2353 static void xdma_disable_allclks(struct xilinx_dma_device *xdev)
2355 clk_disable_unprepare(xdev->rxs_clk);
2356 clk_disable_unprepare(xdev->rx_clk);
2357 clk_disable_unprepare(xdev->txs_clk);
2358 clk_disable_unprepare(xdev->tx_clk);
2359 clk_disable_unprepare(xdev->axi_clk);
2363 * xilinx_dma_chan_probe - Per Channel Probing
2364 * It get channel features from the device tree entry and
2365 * initialize special channel handling routines
2367 * @xdev: Driver specific device structure
2368 * @node: Device node
2369 * @chan_id: DMA Channel id
2371 * Return: '0' on success and failure value on error
2373 static int xilinx_dma_chan_probe(struct xilinx_dma_device *xdev,
2374 struct device_node *node, int chan_id)
2376 struct xilinx_dma_chan *chan;
2377 bool has_dre = false;
2381 /* Allocate and initialize the channel structure */
2382 chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
2386 chan->dev = xdev->dev;
2388 chan->has_sg = xdev->has_sg;
2389 chan->desc_pendingcount = 0x0;
2390 chan->ext_addr = xdev->ext_addr;
2391 /* This variable ensures that descriptors are not
2392 * Submitted when dma engine is in progress. This variable is
2393 * Added to avoid polling for a bit in the status register to
2394 * Know dma state in the driver hot path.
2398 spin_lock_init(&chan->lock);
2399 INIT_LIST_HEAD(&chan->pending_list);
2400 INIT_LIST_HEAD(&chan->done_list);
2401 INIT_LIST_HEAD(&chan->active_list);
2402 INIT_LIST_HEAD(&chan->free_seg_list);
2404 /* Retrieve the channel properties from the device tree */
2405 has_dre = of_property_read_bool(node, "xlnx,include-dre");
2407 chan->genlock = of_property_read_bool(node, "xlnx,genlock-mode");
2409 err = of_property_read_u32(node, "xlnx,datawidth", &value);
2411 dev_err(xdev->dev, "missing xlnx,datawidth property\n");
2414 width = value >> 3; /* Convert bits to bytes */
2416 /* If data width is greater than 8 bytes, DRE is not in hw */
2421 xdev->common.copy_align = fls(width - 1);
2423 if (of_device_is_compatible(node, "xlnx,axi-vdma-mm2s-channel") ||
2424 of_device_is_compatible(node, "xlnx,axi-dma-mm2s-channel") ||
2425 of_device_is_compatible(node, "xlnx,axi-cdma-channel")) {
2426 chan->direction = DMA_MEM_TO_DEV;
2428 chan->tdest = chan_id;
2430 chan->ctrl_offset = XILINX_DMA_MM2S_CTRL_OFFSET;
2431 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
2432 chan->desc_offset = XILINX_VDMA_MM2S_DESC_OFFSET;
2433 chan->config.park = 1;
2435 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2436 xdev->flush_on_fsync == XILINX_DMA_FLUSH_MM2S)
2437 chan->flush_on_fsync = true;
2439 } else if (of_device_is_compatible(node,
2440 "xlnx,axi-vdma-s2mm-channel") ||
2441 of_device_is_compatible(node,
2442 "xlnx,axi-dma-s2mm-channel")) {
2443 chan->direction = DMA_DEV_TO_MEM;
2445 chan->tdest = chan_id - xdev->nr_channels;
2446 chan->has_vflip = of_property_read_bool(node,
2447 "xlnx,enable-vert-flip");
2448 if (chan->has_vflip) {
2449 chan->config.vflip_en = dma_read(chan,
2450 XILINX_VDMA_REG_ENABLE_VERTICAL_FLIP) &
2451 XILINX_VDMA_ENABLE_VERTICAL_FLIP;
2454 chan->ctrl_offset = XILINX_DMA_S2MM_CTRL_OFFSET;
2455 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
2456 chan->desc_offset = XILINX_VDMA_S2MM_DESC_OFFSET;
2457 chan->config.park = 1;
2459 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2460 xdev->flush_on_fsync == XILINX_DMA_FLUSH_S2MM)
2461 chan->flush_on_fsync = true;
2464 dev_err(xdev->dev, "Invalid channel compatible node\n");
2468 /* Request the interrupt */
2469 chan->irq = irq_of_parse_and_map(node, 0);
2470 err = request_irq(chan->irq, xilinx_dma_irq_handler, IRQF_SHARED,
2471 "xilinx-dma-controller", chan);
2473 dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq);
2477 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
2478 chan->start_transfer = xilinx_dma_start_transfer;
2479 chan->stop_transfer = xilinx_dma_stop_transfer;
2480 } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
2481 chan->start_transfer = xilinx_cdma_start_transfer;
2482 chan->stop_transfer = xilinx_cdma_stop_transfer;
2484 chan->start_transfer = xilinx_vdma_start_transfer;
2485 chan->stop_transfer = xilinx_dma_stop_transfer;
2488 /* Initialize the tasklet */
2489 tasklet_init(&chan->tasklet, xilinx_dma_do_tasklet,
2490 (unsigned long)chan);
2493 * Initialize the DMA channel and add it to the DMA engine channels
2496 chan->common.device = &xdev->common;
2498 list_add_tail(&chan->common.device_node, &xdev->common.channels);
2499 xdev->chan[chan->id] = chan;
2501 /* Reset the channel */
2502 err = xilinx_dma_chan_reset(chan);
2504 dev_err(xdev->dev, "Reset channel failed\n");
2512 * xilinx_dma_child_probe - Per child node probe
2513 * It get number of dma-channels per child node from
2514 * device-tree and initializes all the channels.
2516 * @xdev: Driver specific device structure
2517 * @node: Device node
2521 static int xilinx_dma_child_probe(struct xilinx_dma_device *xdev,
2522 struct device_node *node)
2524 int ret, i, nr_channels = 1;
2526 ret = of_property_read_u32(node, "dma-channels", &nr_channels);
2527 if ((ret < 0) && xdev->mcdma)
2528 dev_warn(xdev->dev, "missing dma-channels property\n");
2530 for (i = 0; i < nr_channels; i++)
2531 xilinx_dma_chan_probe(xdev, node, xdev->chan_id++);
2533 xdev->nr_channels += nr_channels;
2539 * of_dma_xilinx_xlate - Translation function
2540 * @dma_spec: Pointer to DMA specifier as found in the device tree
2541 * @ofdma: Pointer to DMA controller data
2543 * Return: DMA channel pointer on success and NULL on error
2545 static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
2546 struct of_dma *ofdma)
2548 struct xilinx_dma_device *xdev = ofdma->of_dma_data;
2549 int chan_id = dma_spec->args[0];
2551 if (chan_id >= xdev->nr_channels || !xdev->chan[chan_id])
2554 return dma_get_slave_channel(&xdev->chan[chan_id]->common);
2557 static const struct xilinx_dma_config axidma_config = {
2558 .dmatype = XDMA_TYPE_AXIDMA,
2559 .clk_init = axidma_clk_init,
2562 static const struct xilinx_dma_config axicdma_config = {
2563 .dmatype = XDMA_TYPE_CDMA,
2564 .clk_init = axicdma_clk_init,
2567 static const struct xilinx_dma_config axivdma_config = {
2568 .dmatype = XDMA_TYPE_VDMA,
2569 .clk_init = axivdma_clk_init,
2572 static const struct of_device_id xilinx_dma_of_ids[] = {
2573 { .compatible = "xlnx,axi-dma-1.00.a", .data = &axidma_config },
2574 { .compatible = "xlnx,axi-cdma-1.00.a", .data = &axicdma_config },
2575 { .compatible = "xlnx,axi-vdma-1.00.a", .data = &axivdma_config },
2578 MODULE_DEVICE_TABLE(of, xilinx_dma_of_ids);
2581 * xilinx_dma_probe - Driver probe function
2582 * @pdev: Pointer to the platform_device structure
2584 * Return: '0' on success and failure value on error
2586 static int xilinx_dma_probe(struct platform_device *pdev)
2588 int (*clk_init)(struct platform_device *, struct clk **, struct clk **,
2589 struct clk **, struct clk **, struct clk **)
2591 struct device_node *node = pdev->dev.of_node;
2592 struct xilinx_dma_device *xdev;
2593 struct device_node *child, *np = pdev->dev.of_node;
2594 struct resource *io;
2595 u32 num_frames, addr_width;
2598 /* Allocate and initialize the DMA engine structure */
2599 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
2603 xdev->dev = &pdev->dev;
2605 const struct of_device_id *match;
2607 match = of_match_node(xilinx_dma_of_ids, np);
2608 if (match && match->data) {
2609 xdev->dma_config = match->data;
2610 clk_init = xdev->dma_config->clk_init;
2614 err = clk_init(pdev, &xdev->axi_clk, &xdev->tx_clk, &xdev->txs_clk,
2615 &xdev->rx_clk, &xdev->rxs_clk);
2619 /* Request and map I/O memory */
2620 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2621 xdev->regs = devm_ioremap_resource(&pdev->dev, io);
2622 if (IS_ERR(xdev->regs))
2623 return PTR_ERR(xdev->regs);
2625 /* Retrieve the DMA engine properties from the device tree */
2626 xdev->has_sg = of_property_read_bool(node, "xlnx,include-sg");
2627 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA)
2628 xdev->mcdma = of_property_read_bool(node, "xlnx,mcdma");
2630 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
2631 err = of_property_read_u32(node, "xlnx,num-fstores",
2635 "missing xlnx,num-fstores property\n");
2639 err = of_property_read_u32(node, "xlnx,flush-fsync",
2640 &xdev->flush_on_fsync);
2643 "missing xlnx,flush-fsync property\n");
2646 err = of_property_read_u32(node, "xlnx,addrwidth", &addr_width);
2648 dev_warn(xdev->dev, "missing xlnx,addrwidth property\n");
2650 if (addr_width > 32)
2651 xdev->ext_addr = true;
2653 xdev->ext_addr = false;
2655 /* Set the dma mask bits */
2656 dma_set_mask(xdev->dev, DMA_BIT_MASK(addr_width));
2658 /* Initialize the DMA engine */
2659 xdev->common.dev = &pdev->dev;
2661 INIT_LIST_HEAD(&xdev->common.channels);
2662 if (!(xdev->dma_config->dmatype == XDMA_TYPE_CDMA)) {
2663 dma_cap_set(DMA_SLAVE, xdev->common.cap_mask);
2664 dma_cap_set(DMA_PRIVATE, xdev->common.cap_mask);
2667 xdev->common.device_alloc_chan_resources =
2668 xilinx_dma_alloc_chan_resources;
2669 xdev->common.device_free_chan_resources =
2670 xilinx_dma_free_chan_resources;
2671 xdev->common.device_terminate_all = xilinx_dma_terminate_all;
2672 xdev->common.device_tx_status = xilinx_dma_tx_status;
2673 xdev->common.device_issue_pending = xilinx_dma_issue_pending;
2674 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
2675 dma_cap_set(DMA_CYCLIC, xdev->common.cap_mask);
2676 xdev->common.device_prep_slave_sg = xilinx_dma_prep_slave_sg;
2677 xdev->common.device_prep_dma_cyclic =
2678 xilinx_dma_prep_dma_cyclic;
2679 xdev->common.device_prep_interleaved_dma =
2680 xilinx_dma_prep_interleaved;
2681 /* Residue calculation is supported by only AXI DMA */
2682 xdev->common.residue_granularity =
2683 DMA_RESIDUE_GRANULARITY_SEGMENT;
2684 } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
2685 dma_cap_set(DMA_MEMCPY, xdev->common.cap_mask);
2686 xdev->common.device_prep_dma_memcpy = xilinx_cdma_prep_memcpy;
2688 xdev->common.device_prep_interleaved_dma =
2689 xilinx_vdma_dma_prep_interleaved;
2692 platform_set_drvdata(pdev, xdev);
2694 /* Initialize the channels */
2695 for_each_child_of_node(node, child) {
2696 err = xilinx_dma_child_probe(xdev, child);
2701 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
2702 for (i = 0; i < xdev->nr_channels; i++)
2704 xdev->chan[i]->num_frms = num_frames;
2707 /* Register the DMA engine with the core */
2708 dma_async_device_register(&xdev->common);
2710 err = of_dma_controller_register(node, of_dma_xilinx_xlate,
2713 dev_err(&pdev->dev, "Unable to register DMA to DT\n");
2714 dma_async_device_unregister(&xdev->common);
2718 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA)
2719 dev_info(&pdev->dev, "Xilinx AXI DMA Engine Driver Probed!!\n");
2720 else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA)
2721 dev_info(&pdev->dev, "Xilinx AXI CDMA Engine Driver Probed!!\n");
2723 dev_info(&pdev->dev, "Xilinx AXI VDMA Engine Driver Probed!!\n");
2728 xdma_disable_allclks(xdev);
2730 for (i = 0; i < xdev->nr_channels; i++)
2732 xilinx_dma_chan_remove(xdev->chan[i]);
2738 * xilinx_dma_remove - Driver remove function
2739 * @pdev: Pointer to the platform_device structure
2741 * Return: Always '0'
2743 static int xilinx_dma_remove(struct platform_device *pdev)
2745 struct xilinx_dma_device *xdev = platform_get_drvdata(pdev);
2748 of_dma_controller_free(pdev->dev.of_node);
2750 dma_async_device_unregister(&xdev->common);
2752 for (i = 0; i < xdev->nr_channels; i++)
2754 xilinx_dma_chan_remove(xdev->chan[i]);
2756 xdma_disable_allclks(xdev);
2761 static struct platform_driver xilinx_vdma_driver = {
2763 .name = "xilinx-vdma",
2764 .of_match_table = xilinx_dma_of_ids,
2766 .probe = xilinx_dma_probe,
2767 .remove = xilinx_dma_remove,
2770 module_platform_driver(xilinx_vdma_driver);
2772 MODULE_AUTHOR("Xilinx, Inc.");
2773 MODULE_DESCRIPTION("Xilinx VDMA driver");
2774 MODULE_LICENSE("GPL v2");