blob: 40509a44aae102081d21d3010708912849e413bd [file] [log] [blame]
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301/*
2 * DMA driver for Xilinx Video DMA Engine
3 *
4 * Copyright (C) 2010-2014 Xilinx, Inc. All rights reserved.
5 *
6 * Based on the Freescale DMA driver.
7 *
8 * Description:
9 * The AXI Video Direct Memory Access (AXI VDMA) core is a soft Xilinx IP
10 * core that provides high-bandwidth direct memory access between memory
11 * and AXI4-Stream type video target peripherals. The core provides efficient
12 * two dimensional DMA operations with independent asynchronous read (S2MM)
13 * and write (MM2S) channel operation. It can be configured to have either
14 * one channel or two channels. If configured as two channels, one is to
15 * transmit to the video device (MM2S) and another is to receive from the
16 * video device (S2MM). Initialization, status, interrupt and management
17 * registers are accessed through an AXI4-Lite slave interface.
18 *
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +053019 * The AXI Direct Memory Access (AXI DMA) core is a soft Xilinx IP core that
20 * provides high-bandwidth one dimensional direct memory access between memory
21 * and AXI4-Stream target peripherals. It supports one receive and one
22 * transmit channel, both of them optional at synthesis time.
23 *
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +053024 * The AXI CDMA, is a soft IP, which provides high-bandwidth Direct Memory
25 * Access (DMA) between a memory-mapped source address and a memory-mapped
26 * destination address.
27 *
Srikanth Thokala9cd43602014-04-23 20:23:26 +053028 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 2 of the License, or
31 * (at your option) any later version.
32 */
33
Srikanth Thokala9cd43602014-04-23 20:23:26 +053034#include <linux/bitops.h>
35#include <linux/dmapool.h>
Kedareswara rao Appana937abe82015-03-02 23:24:24 +053036#include <linux/dma/xilinx_dma.h>
Srikanth Thokala9cd43602014-04-23 20:23:26 +053037#include <linux/init.h>
38#include <linux/interrupt.h>
39#include <linux/io.h>
Kedareswara rao Appana9495f262016-02-26 19:33:54 +053040#include <linux/iopoll.h>
Srikanth Thokala9cd43602014-04-23 20:23:26 +053041#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>
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +053047#include <linux/clk.h>
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +053048#include <linux/io-64-nonatomic-lo-hi.h>
Srikanth Thokala9cd43602014-04-23 20:23:26 +053049
50#include "../dmaengine.h"
51
52/* Register/Descriptor Offsets */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +053053#define XILINX_DMA_MM2S_CTRL_OFFSET 0x0000
54#define XILINX_DMA_S2MM_CTRL_OFFSET 0x0030
Srikanth Thokala9cd43602014-04-23 20:23:26 +053055#define XILINX_VDMA_MM2S_DESC_OFFSET 0x0050
56#define XILINX_VDMA_S2MM_DESC_OFFSET 0x00a0
57
58/* Control Registers */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +053059#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)
Srikanth Thokala9cd43602014-04-23 20:23:26 +053075
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +053076#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)
Srikanth Thokala9cd43602014-04-23 20:23:26 +053093
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +053094#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_RD_REF_SHIFT 0
103#define XILINX_DMA_REG_VDMA_VERSION 0x002c
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530104
105/* Register Direct Mode Registers */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530106#define XILINX_DMA_REG_VSIZE 0x0000
107#define XILINX_DMA_REG_HSIZE 0x0004
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530108
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530109#define XILINX_DMA_REG_FRMDLY_STRIDE 0x0008
110#define XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT 24
111#define XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT 0
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530112
113#define XILINX_VDMA_REG_START_ADDRESS(n) (0x000c + 4 * (n))
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530114#define XILINX_VDMA_REG_START_ADDRESS_64(n) (0x000c + 8 * (n))
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530115
116/* HW specific definitions */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530117#define XILINX_DMA_MAX_CHANS_PER_DEVICE 0x2
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530118
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530119#define XILINX_DMA_DMAXR_ALL_IRQ_MASK \
120 (XILINX_DMA_DMASR_FRM_CNT_IRQ | \
121 XILINX_DMA_DMASR_DLY_CNT_IRQ | \
122 XILINX_DMA_DMASR_ERR_IRQ)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530123
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530124#define XILINX_DMA_DMASR_ALL_ERR_MASK \
125 (XILINX_DMA_DMASR_EOL_LATE_ERR | \
126 XILINX_DMA_DMASR_SOF_LATE_ERR | \
127 XILINX_DMA_DMASR_SG_DEC_ERR | \
128 XILINX_DMA_DMASR_SG_SLV_ERR | \
129 XILINX_DMA_DMASR_EOF_EARLY_ERR | \
130 XILINX_DMA_DMASR_SOF_EARLY_ERR | \
131 XILINX_DMA_DMASR_DMA_DEC_ERR | \
132 XILINX_DMA_DMASR_DMA_SLAVE_ERR | \
133 XILINX_DMA_DMASR_DMA_INT_ERR)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530134
135/*
136 * Recoverable errors are DMA Internal error, SOF Early, EOF Early
137 * and SOF Late. They are only recoverable when C_FLUSH_ON_FSYNC
138 * is enabled in the h/w system.
139 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530140#define XILINX_DMA_DMASR_ERR_RECOVER_MASK \
141 (XILINX_DMA_DMASR_SOF_LATE_ERR | \
142 XILINX_DMA_DMASR_EOF_EARLY_ERR | \
143 XILINX_DMA_DMASR_SOF_EARLY_ERR | \
144 XILINX_DMA_DMASR_DMA_INT_ERR)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530145
146/* Axi VDMA Flush on Fsync bits */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530147#define XILINX_DMA_FLUSH_S2MM 3
148#define XILINX_DMA_FLUSH_MM2S 2
149#define XILINX_DMA_FLUSH_BOTH 1
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530150
151/* Delay loop counter to prevent hardware failure */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530152#define XILINX_DMA_LOOP_COUNT 1000000
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530153
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530154/* AXI DMA Specific Registers/Offsets */
155#define XILINX_DMA_REG_SRCDSTADDR 0x18
156#define XILINX_DMA_REG_BTT 0x28
157
158/* AXI DMA Specific Masks/Bit fields */
159#define XILINX_DMA_MAX_TRANS_LEN GENMASK(22, 0)
160#define XILINX_DMA_CR_COALESCE_MAX GENMASK(23, 16)
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530161#define XILINX_DMA_CR_CYCLIC_BD_EN_MASK BIT(4)
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530162#define XILINX_DMA_CR_COALESCE_SHIFT 16
163#define XILINX_DMA_BD_SOP BIT(27)
164#define XILINX_DMA_BD_EOP BIT(26)
165#define XILINX_DMA_COALESCE_MAX 255
166#define XILINX_DMA_NUM_APP_WORDS 5
167
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530168/* AXI CDMA Specific Registers/Offsets */
169#define XILINX_CDMA_REG_SRCADDR 0x18
170#define XILINX_CDMA_REG_DSTADDR 0x20
171
172/* AXI CDMA Specific Masks */
173#define XILINX_CDMA_CR_SGMODE BIT(3)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530174
175/**
176 * struct xilinx_vdma_desc_hw - Hardware Descriptor
177 * @next_desc: Next Descriptor Pointer @0x00
178 * @pad1: Reserved @0x04
179 * @buf_addr: Buffer address @0x08
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530180 * @buf_addr_msb: MSB of Buffer address @0x0C
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530181 * @vsize: Vertical Size @0x10
182 * @hsize: Horizontal Size @0x14
183 * @stride: Number of bytes between the first
184 * pixels of each horizontal line @0x18
185 */
186struct xilinx_vdma_desc_hw {
187 u32 next_desc;
188 u32 pad1;
189 u32 buf_addr;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530190 u32 buf_addr_msb;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530191 u32 vsize;
192 u32 hsize;
193 u32 stride;
194} __aligned(64);
195
196/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530197 * struct xilinx_axidma_desc_hw - Hardware Descriptor for AXI DMA
198 * @next_desc: Next Descriptor Pointer @0x00
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530199 * @next_desc_msb: MSB of Next Descriptor Pointer @0x04
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530200 * @buf_addr: Buffer address @0x08
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530201 * @buf_addr_msb: MSB of Buffer address @0x0C
202 * @pad1: Reserved @0x10
203 * @pad2: Reserved @0x14
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530204 * @control: Control field @0x18
205 * @status: Status field @0x1C
206 * @app: APP Fields @0x20 - 0x30
207 */
208struct xilinx_axidma_desc_hw {
209 u32 next_desc;
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530210 u32 next_desc_msb;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530211 u32 buf_addr;
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530212 u32 buf_addr_msb;
213 u32 pad1;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530214 u32 pad2;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530215 u32 control;
216 u32 status;
217 u32 app[XILINX_DMA_NUM_APP_WORDS];
218} __aligned(64);
219
220/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530221 * struct xilinx_cdma_desc_hw - Hardware Descriptor
222 * @next_desc: Next Descriptor Pointer @0x00
223 * @pad1: Reserved @0x04
224 * @src_addr: Source address @0x08
225 * @pad2: Reserved @0x0C
226 * @dest_addr: Destination address @0x10
227 * @pad3: Reserved @0x14
228 * @control: Control field @0x18
229 * @status: Status field @0x1C
230 */
231struct xilinx_cdma_desc_hw {
232 u32 next_desc;
233 u32 pad1;
234 u32 src_addr;
235 u32 pad2;
236 u32 dest_addr;
237 u32 pad3;
238 u32 control;
239 u32 status;
240} __aligned(64);
241
242/**
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530243 * struct xilinx_vdma_tx_segment - Descriptor segment
244 * @hw: Hardware descriptor
245 * @node: Node in the descriptor segments list
246 * @phys: Physical address of segment
247 */
248struct xilinx_vdma_tx_segment {
249 struct xilinx_vdma_desc_hw hw;
250 struct list_head node;
251 dma_addr_t phys;
252} __aligned(64);
253
254/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530255 * struct xilinx_axidma_tx_segment - Descriptor segment
256 * @hw: Hardware descriptor
257 * @node: Node in the descriptor segments list
258 * @phys: Physical address of segment
259 */
260struct xilinx_axidma_tx_segment {
261 struct xilinx_axidma_desc_hw hw;
262 struct list_head node;
263 dma_addr_t phys;
264} __aligned(64);
265
266/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530267 * struct xilinx_cdma_tx_segment - Descriptor segment
268 * @hw: Hardware descriptor
269 * @node: Node in the descriptor segments list
270 * @phys: Physical address of segment
271 */
272struct xilinx_cdma_tx_segment {
273 struct xilinx_cdma_desc_hw hw;
274 struct list_head node;
275 dma_addr_t phys;
276} __aligned(64);
277
278/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530279 * struct xilinx_dma_tx_descriptor - Per Transaction structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530280 * @async_tx: Async transaction descriptor
281 * @segments: TX segments list
282 * @node: Node in the channel descriptors list
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530283 * @cyclic: Check for cyclic transfers.
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530284 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530285struct xilinx_dma_tx_descriptor {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530286 struct dma_async_tx_descriptor async_tx;
287 struct list_head segments;
288 struct list_head node;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530289 bool cyclic;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530290};
291
292/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530293 * struct xilinx_dma_chan - Driver specific DMA channel structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530294 * @xdev: Driver specific device structure
295 * @ctrl_offset: Control registers offset
296 * @desc_offset: TX descriptor registers offset
297 * @lock: Descriptor operation lock
298 * @pending_list: Descriptors waiting
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530299 * @active_list: Descriptors ready to submit
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530300 * @done_list: Complete descriptors
301 * @common: DMA common channel
302 * @desc_pool: Descriptors pool
303 * @dev: The dma device
304 * @irq: Channel IRQ
305 * @id: Channel ID
306 * @direction: Transfer direction
307 * @num_frms: Number of frames
308 * @has_sg: Support scatter transfers
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530309 * @cyclic: Check for cyclic transfers.
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530310 * @genlock: Support genlock mode
311 * @err: Channel has errors
312 * @tasklet: Cleanup work after irq
313 * @config: Device configuration info
314 * @flush_on_fsync: Flush on Frame sync
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530315 * @desc_pendingcount: Descriptor pending count
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530316 * @ext_addr: Indicates 64 bit addressing is supported by dma channel
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +0530317 * @desc_submitcount: Descriptor h/w submitted count
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530318 * @residue: Residue for AXI DMA
319 * @seg_v: Statically allocated segments base
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530320 * @cyclic_seg_v: Statically allocated segment base for cyclic transfers
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530321 * @start_transfer: Differentiate b/w DMA IP's transfer
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530322 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530323struct xilinx_dma_chan {
324 struct xilinx_dma_device *xdev;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530325 u32 ctrl_offset;
326 u32 desc_offset;
327 spinlock_t lock;
328 struct list_head pending_list;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530329 struct list_head active_list;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530330 struct list_head done_list;
331 struct dma_chan common;
332 struct dma_pool *desc_pool;
333 struct device *dev;
334 int irq;
335 int id;
336 enum dma_transfer_direction direction;
337 int num_frms;
338 bool has_sg;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530339 bool cyclic;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530340 bool genlock;
341 bool err;
342 struct tasklet_struct tasklet;
343 struct xilinx_vdma_config config;
344 bool flush_on_fsync;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530345 u32 desc_pendingcount;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530346 bool ext_addr;
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +0530347 u32 desc_submitcount;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530348 u32 residue;
349 struct xilinx_axidma_tx_segment *seg_v;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530350 struct xilinx_axidma_tx_segment *cyclic_seg_v;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530351 void (*start_transfer)(struct xilinx_dma_chan *chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530352};
353
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530354struct xilinx_dma_config {
355 enum xdma_ip_type dmatype;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530356 int (*clk_init)(struct platform_device *pdev, struct clk **axi_clk,
357 struct clk **tx_clk, struct clk **txs_clk,
358 struct clk **rx_clk, struct clk **rxs_clk);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530359};
360
361/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530362 * struct xilinx_dma_device - DMA device structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530363 * @regs: I/O mapped base address
364 * @dev: Device Structure
365 * @common: DMA device structure
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530366 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530367 * @has_sg: Specifies whether Scatter-Gather is present or not
368 * @flush_on_fsync: Flush on frame sync
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530369 * @ext_addr: Indicates 64 bit addressing is supported by dma device
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530370 * @pdev: Platform device structure pointer
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530371 * @dma_config: DMA config structure
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530372 * @axi_clk: DMA Axi4-lite interace clock
373 * @tx_clk: DMA mm2s clock
374 * @txs_clk: DMA mm2s stream clock
375 * @rx_clk: DMA s2mm clock
376 * @rxs_clk: DMA s2mm stream clock
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530377 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530378struct xilinx_dma_device {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530379 void __iomem *regs;
380 struct device *dev;
381 struct dma_device common;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530382 struct xilinx_dma_chan *chan[XILINX_DMA_MAX_CHANS_PER_DEVICE];
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530383 bool has_sg;
384 u32 flush_on_fsync;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530385 bool ext_addr;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530386 struct platform_device *pdev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530387 const struct xilinx_dma_config *dma_config;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530388 struct clk *axi_clk;
389 struct clk *tx_clk;
390 struct clk *txs_clk;
391 struct clk *rx_clk;
392 struct clk *rxs_clk;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530393};
394
395/* Macros */
396#define to_xilinx_chan(chan) \
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530397 container_of(chan, struct xilinx_dma_chan, common)
398#define to_dma_tx_descriptor(tx) \
399 container_of(tx, struct xilinx_dma_tx_descriptor, async_tx)
400#define xilinx_dma_poll_timeout(chan, reg, val, cond, delay_us, timeout_us) \
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530401 readl_poll_timeout(chan->xdev->regs + chan->ctrl_offset + reg, val, \
402 cond, delay_us, timeout_us)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530403
404/* IO accessors */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530405static inline u32 dma_read(struct xilinx_dma_chan *chan, u32 reg)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530406{
407 return ioread32(chan->xdev->regs + reg);
408}
409
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530410static inline void dma_write(struct xilinx_dma_chan *chan, u32 reg, u32 value)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530411{
412 iowrite32(value, chan->xdev->regs + reg);
413}
414
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530415static inline void vdma_desc_write(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530416 u32 value)
417{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530418 dma_write(chan, chan->desc_offset + reg, value);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530419}
420
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530421static inline u32 dma_ctrl_read(struct xilinx_dma_chan *chan, u32 reg)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530422{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530423 return dma_read(chan, chan->ctrl_offset + reg);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530424}
425
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530426static inline void dma_ctrl_write(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530427 u32 value)
428{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530429 dma_write(chan, chan->ctrl_offset + reg, value);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530430}
431
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530432static inline void dma_ctrl_clr(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530433 u32 clr)
434{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530435 dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) & ~clr);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530436}
437
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530438static inline void dma_ctrl_set(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530439 u32 set)
440{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530441 dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) | set);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530442}
443
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530444/**
445 * vdma_desc_write_64 - 64-bit descriptor write
446 * @chan: Driver specific VDMA channel
447 * @reg: Register to write
448 * @value_lsb: lower address of the descriptor.
449 * @value_msb: upper address of the descriptor.
450 *
451 * Since vdma driver is trying to write to a register offset which is not a
452 * multiple of 64 bits(ex : 0x5c), we are writing as two separate 32 bits
453 * instead of a single 64 bit register write.
454 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530455static inline void vdma_desc_write_64(struct xilinx_dma_chan *chan, u32 reg,
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530456 u32 value_lsb, u32 value_msb)
457{
458 /* Write the lsb 32 bits*/
459 writel(value_lsb, chan->xdev->regs + chan->desc_offset + reg);
460
461 /* Write the msb 32 bits */
462 writel(value_msb, chan->xdev->regs + chan->desc_offset + reg + 4);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530463}
464
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530465static inline void dma_writeq(struct xilinx_dma_chan *chan, u32 reg, u64 value)
466{
467 lo_hi_writeq(value, chan->xdev->regs + chan->ctrl_offset + reg);
468}
469
470static inline void xilinx_write(struct xilinx_dma_chan *chan, u32 reg,
471 dma_addr_t addr)
472{
473 if (chan->ext_addr)
474 dma_writeq(chan, reg, addr);
475 else
476 dma_ctrl_write(chan, reg, addr);
477}
478
479static inline void xilinx_axidma_buf(struct xilinx_dma_chan *chan,
480 struct xilinx_axidma_desc_hw *hw,
481 dma_addr_t buf_addr, size_t sg_used,
482 size_t period_len)
483{
484 if (chan->ext_addr) {
485 hw->buf_addr = lower_32_bits(buf_addr + sg_used + period_len);
486 hw->buf_addr_msb = upper_32_bits(buf_addr + sg_used +
487 period_len);
488 } else {
489 hw->buf_addr = buf_addr + sg_used + period_len;
490 }
491}
492
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530493/* -----------------------------------------------------------------------------
494 * Descriptors and segments alloc and free
495 */
496
497/**
498 * xilinx_vdma_alloc_tx_segment - Allocate transaction segment
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530499 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530500 *
501 * Return: The allocated segment on success and NULL on failure.
502 */
503static struct xilinx_vdma_tx_segment *
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530504xilinx_vdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530505{
506 struct xilinx_vdma_tx_segment *segment;
507 dma_addr_t phys;
508
Julia Lawall2ba4f8a2016-04-29 22:09:09 +0200509 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530510 if (!segment)
511 return NULL;
512
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530513 segment->phys = phys;
514
515 return segment;
516}
517
518/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530519 * xilinx_cdma_alloc_tx_segment - Allocate transaction segment
520 * @chan: Driver specific DMA channel
521 *
522 * Return: The allocated segment on success and NULL on failure.
523 */
524static struct xilinx_cdma_tx_segment *
525xilinx_cdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
526{
527 struct xilinx_cdma_tx_segment *segment;
528 dma_addr_t phys;
529
Kedareswara rao Appana62147862016-05-18 13:17:31 +0530530 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530531 if (!segment)
532 return NULL;
533
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530534 segment->phys = phys;
535
536 return segment;
537}
538
539/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530540 * xilinx_axidma_alloc_tx_segment - Allocate transaction segment
541 * @chan: Driver specific DMA channel
542 *
543 * Return: The allocated segment on success and NULL on failure.
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530544 */
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530545static struct xilinx_axidma_tx_segment *
546xilinx_axidma_alloc_tx_segment(struct xilinx_dma_chan *chan)
547{
548 struct xilinx_axidma_tx_segment *segment;
549 dma_addr_t phys;
550
Kedareswara rao Appana62147862016-05-18 13:17:31 +0530551 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530552 if (!segment)
553 return NULL;
554
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530555 segment->phys = phys;
556
557 return segment;
558}
559
560/**
561 * xilinx_dma_free_tx_segment - Free transaction segment
562 * @chan: Driver specific DMA channel
563 * @segment: DMA transaction segment
564 */
565static void xilinx_dma_free_tx_segment(struct xilinx_dma_chan *chan,
566 struct xilinx_axidma_tx_segment *segment)
567{
568 dma_pool_free(chan->desc_pool, segment, segment->phys);
569}
570
571/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530572 * xilinx_cdma_free_tx_segment - Free transaction segment
573 * @chan: Driver specific DMA channel
574 * @segment: DMA transaction segment
575 */
576static void xilinx_cdma_free_tx_segment(struct xilinx_dma_chan *chan,
577 struct xilinx_cdma_tx_segment *segment)
578{
579 dma_pool_free(chan->desc_pool, segment, segment->phys);
580}
581
582/**
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530583 * xilinx_vdma_free_tx_segment - Free transaction segment
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530584 * @chan: Driver specific DMA channel
585 * @segment: DMA transaction segment
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530586 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530587static void xilinx_vdma_free_tx_segment(struct xilinx_dma_chan *chan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530588 struct xilinx_vdma_tx_segment *segment)
589{
590 dma_pool_free(chan->desc_pool, segment, segment->phys);
591}
592
593/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530594 * xilinx_dma_tx_descriptor - Allocate transaction descriptor
595 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530596 *
597 * Return: The allocated descriptor on success and NULL on failure.
598 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530599static struct xilinx_dma_tx_descriptor *
600xilinx_dma_alloc_tx_descriptor(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530601{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530602 struct xilinx_dma_tx_descriptor *desc;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530603
604 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
605 if (!desc)
606 return NULL;
607
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530608 INIT_LIST_HEAD(&desc->segments);
609
610 return desc;
611}
612
613/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530614 * xilinx_dma_free_tx_descriptor - Free transaction descriptor
615 * @chan: Driver specific DMA channel
616 * @desc: DMA transaction descriptor
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530617 */
618static void
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530619xilinx_dma_free_tx_descriptor(struct xilinx_dma_chan *chan,
620 struct xilinx_dma_tx_descriptor *desc)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530621{
622 struct xilinx_vdma_tx_segment *segment, *next;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530623 struct xilinx_cdma_tx_segment *cdma_segment, *cdma_next;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530624 struct xilinx_axidma_tx_segment *axidma_segment, *axidma_next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530625
626 if (!desc)
627 return;
628
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530629 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530630 list_for_each_entry_safe(segment, next, &desc->segments, node) {
631 list_del(&segment->node);
632 xilinx_vdma_free_tx_segment(chan, segment);
633 }
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530634 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530635 list_for_each_entry_safe(cdma_segment, cdma_next,
636 &desc->segments, node) {
637 list_del(&cdma_segment->node);
638 xilinx_cdma_free_tx_segment(chan, cdma_segment);
639 }
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530640 } else {
641 list_for_each_entry_safe(axidma_segment, axidma_next,
642 &desc->segments, node) {
643 list_del(&axidma_segment->node);
644 xilinx_dma_free_tx_segment(chan, axidma_segment);
645 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530646 }
647
648 kfree(desc);
649}
650
651/* Required functions */
652
653/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530654 * xilinx_dma_free_desc_list - Free descriptors list
655 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530656 * @list: List to parse and delete the descriptor
657 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530658static void xilinx_dma_free_desc_list(struct xilinx_dma_chan *chan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530659 struct list_head *list)
660{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530661 struct xilinx_dma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530662
663 list_for_each_entry_safe(desc, next, list, node) {
664 list_del(&desc->node);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530665 xilinx_dma_free_tx_descriptor(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530666 }
667}
668
669/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530670 * xilinx_dma_free_descriptors - Free channel descriptors
671 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530672 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530673static void xilinx_dma_free_descriptors(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530674{
675 unsigned long flags;
676
677 spin_lock_irqsave(&chan->lock, flags);
678
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530679 xilinx_dma_free_desc_list(chan, &chan->pending_list);
680 xilinx_dma_free_desc_list(chan, &chan->done_list);
681 xilinx_dma_free_desc_list(chan, &chan->active_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530682
683 spin_unlock_irqrestore(&chan->lock, flags);
684}
685
686/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530687 * xilinx_dma_free_chan_resources - Free channel resources
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530688 * @dchan: DMA channel
689 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530690static void xilinx_dma_free_chan_resources(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530691{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530692 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530693
694 dev_dbg(chan->dev, "Free all channel resources.\n");
695
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530696 xilinx_dma_free_descriptors(chan);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530697 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
698 xilinx_dma_free_tx_segment(chan, chan->cyclic_seg_v);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530699 xilinx_dma_free_tx_segment(chan, chan->seg_v);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530700 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530701 dma_pool_destroy(chan->desc_pool);
702 chan->desc_pool = NULL;
703}
704
705/**
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530706 * xilinx_dma_chan_handle_cyclic - Cyclic dma callback
707 * @chan: Driver specific dma channel
708 * @desc: dma transaction descriptor
709 * @flags: flags for spin lock
710 */
711static void xilinx_dma_chan_handle_cyclic(struct xilinx_dma_chan *chan,
712 struct xilinx_dma_tx_descriptor *desc,
713 unsigned long *flags)
714{
715 dma_async_tx_callback callback;
716 void *callback_param;
717
718 callback = desc->async_tx.callback;
719 callback_param = desc->async_tx.callback_param;
720 if (callback) {
721 spin_unlock_irqrestore(&chan->lock, *flags);
722 callback(callback_param);
723 spin_lock_irqsave(&chan->lock, *flags);
724 }
725}
726
727/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530728 * xilinx_dma_chan_desc_cleanup - Clean channel descriptors
729 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530730 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530731static void xilinx_dma_chan_desc_cleanup(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530732{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530733 struct xilinx_dma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530734 unsigned long flags;
735
736 spin_lock_irqsave(&chan->lock, flags);
737
738 list_for_each_entry_safe(desc, next, &chan->done_list, node) {
739 dma_async_tx_callback callback;
740 void *callback_param;
741
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530742 if (desc->cyclic) {
743 xilinx_dma_chan_handle_cyclic(chan, desc, &flags);
744 break;
745 }
746
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530747 /* Remove from the list of running transactions */
748 list_del(&desc->node);
749
750 /* Run the link descriptor callback function */
751 callback = desc->async_tx.callback;
752 callback_param = desc->async_tx.callback_param;
753 if (callback) {
754 spin_unlock_irqrestore(&chan->lock, flags);
755 callback(callback_param);
756 spin_lock_irqsave(&chan->lock, flags);
757 }
758
759 /* Run any dependencies, then free the descriptor */
760 dma_run_dependencies(&desc->async_tx);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530761 xilinx_dma_free_tx_descriptor(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530762 }
763
764 spin_unlock_irqrestore(&chan->lock, flags);
765}
766
767/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530768 * xilinx_dma_do_tasklet - Schedule completion tasklet
769 * @data: Pointer to the Xilinx DMA channel structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530770 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530771static void xilinx_dma_do_tasklet(unsigned long data)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530772{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530773 struct xilinx_dma_chan *chan = (struct xilinx_dma_chan *)data;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530774
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530775 xilinx_dma_chan_desc_cleanup(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530776}
777
778/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530779 * xilinx_dma_alloc_chan_resources - Allocate channel resources
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530780 * @dchan: DMA channel
781 *
782 * Return: '0' on success and failure value on error
783 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530784static int xilinx_dma_alloc_chan_resources(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530785{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530786 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530787
788 /* Has this channel already been allocated? */
789 if (chan->desc_pool)
790 return 0;
791
792 /*
793 * We need the descriptor to be aligned to 64bytes
794 * for meeting Xilinx VDMA specification requirement.
795 */
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530796 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530797 chan->desc_pool = dma_pool_create("xilinx_dma_desc_pool",
798 chan->dev,
799 sizeof(struct xilinx_axidma_tx_segment),
800 __alignof__(struct xilinx_axidma_tx_segment),
801 0);
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530802 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530803 chan->desc_pool = dma_pool_create("xilinx_cdma_desc_pool",
804 chan->dev,
805 sizeof(struct xilinx_cdma_tx_segment),
806 __alignof__(struct xilinx_cdma_tx_segment),
807 0);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530808 } else {
809 chan->desc_pool = dma_pool_create("xilinx_vdma_desc_pool",
810 chan->dev,
811 sizeof(struct xilinx_vdma_tx_segment),
812 __alignof__(struct xilinx_vdma_tx_segment),
813 0);
814 }
815
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530816 if (!chan->desc_pool) {
817 dev_err(chan->dev,
818 "unable to allocate channel %d descriptor pool\n",
819 chan->id);
820 return -ENOMEM;
821 }
822
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530823 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530824 /*
825 * For AXI DMA case after submitting a pending_list, keep
826 * an extra segment allocated so that the "next descriptor"
827 * pointer on the tail descriptor always points to a
828 * valid descriptor, even when paused after reaching taildesc.
829 * This way, it is possible to issue additional
830 * transfers without halting and restarting the channel.
831 */
832 chan->seg_v = xilinx_axidma_alloc_tx_segment(chan);
833
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530834 /*
835 * For cyclic DMA mode we need to program the tail Descriptor
836 * register with a value which is not a part of the BD chain
837 * so allocating a desc segment during channel allocation for
838 * programming tail descriptor.
839 */
840 chan->cyclic_seg_v = xilinx_axidma_alloc_tx_segment(chan);
841 }
842
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530843 dma_cookie_init(dchan);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530844
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530845 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530846 /* For AXI DMA resetting once channel will reset the
847 * other channel as well so enable the interrupts here.
848 */
849 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
850 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
851 }
852
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530853 if ((chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) && chan->has_sg)
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530854 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
855 XILINX_CDMA_CR_SGMODE);
856
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530857 return 0;
858}
859
860/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530861 * xilinx_dma_tx_status - Get DMA transaction status
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530862 * @dchan: DMA channel
863 * @cookie: Transaction identifier
864 * @txstate: Transaction state
865 *
866 * Return: DMA transaction status
867 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530868static enum dma_status xilinx_dma_tx_status(struct dma_chan *dchan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530869 dma_cookie_t cookie,
870 struct dma_tx_state *txstate)
871{
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530872 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
873 struct xilinx_dma_tx_descriptor *desc;
874 struct xilinx_axidma_tx_segment *segment;
875 struct xilinx_axidma_desc_hw *hw;
876 enum dma_status ret;
877 unsigned long flags;
878 u32 residue = 0;
879
880 ret = dma_cookie_status(dchan, cookie, txstate);
881 if (ret == DMA_COMPLETE || !txstate)
882 return ret;
883
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530884 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530885 spin_lock_irqsave(&chan->lock, flags);
886
887 desc = list_last_entry(&chan->active_list,
888 struct xilinx_dma_tx_descriptor, node);
889 if (chan->has_sg) {
890 list_for_each_entry(segment, &desc->segments, node) {
891 hw = &segment->hw;
892 residue += (hw->control - hw->status) &
893 XILINX_DMA_MAX_TRANS_LEN;
894 }
895 }
896 spin_unlock_irqrestore(&chan->lock, flags);
897
898 chan->residue = residue;
899 dma_set_residue(txstate, chan->residue);
900 }
901
902 return ret;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530903}
904
905/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530906 * xilinx_dma_is_running - Check if DMA channel is running
907 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530908 *
909 * Return: '1' if running, '0' if not.
910 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530911static bool xilinx_dma_is_running(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530912{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530913 return !(dma_ctrl_read(chan, XILINX_DMA_REG_DMASR) &
914 XILINX_DMA_DMASR_HALTED) &&
915 (dma_ctrl_read(chan, XILINX_DMA_REG_DMACR) &
916 XILINX_DMA_DMACR_RUNSTOP);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530917}
918
919/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530920 * xilinx_dma_is_idle - Check if DMA channel is idle
921 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530922 *
923 * Return: '1' if idle, '0' if not.
924 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530925static bool xilinx_dma_is_idle(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530926{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530927 return dma_ctrl_read(chan, XILINX_DMA_REG_DMASR) &
928 XILINX_DMA_DMASR_IDLE;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530929}
930
931/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530932 * xilinx_dma_halt - Halt DMA channel
933 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530934 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530935static void xilinx_dma_halt(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530936{
Kedareswara rao Appana69490632016-03-03 23:02:42 +0530937 int err;
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530938 u32 val;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530939
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530940 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530941
942 /* Wait for the hardware to halt */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530943 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
944 (val & XILINX_DMA_DMASR_HALTED), 0,
945 XILINX_DMA_LOOP_COUNT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530946
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530947 if (err) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530948 dev_err(chan->dev, "Cannot stop channel %p: %x\n",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530949 chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530950 chan->err = true;
951 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530952}
953
954/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530955 * xilinx_dma_start - Start DMA channel
956 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530957 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530958static void xilinx_dma_start(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530959{
Kedareswara rao Appana69490632016-03-03 23:02:42 +0530960 int err;
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530961 u32 val;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530962
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530963 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530964
965 /* Wait for the hardware to start */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530966 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
967 !(val & XILINX_DMA_DMASR_HALTED), 0,
968 XILINX_DMA_LOOP_COUNT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530969
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530970 if (err) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530971 dev_err(chan->dev, "Cannot start channel %p: %x\n",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530972 chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530973
974 chan->err = true;
975 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530976}
977
978/**
979 * xilinx_vdma_start_transfer - Starts VDMA transfer
980 * @chan: Driver specific channel struct pointer
981 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530982static void xilinx_vdma_start_transfer(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530983{
984 struct xilinx_vdma_config *config = &chan->config;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530985 struct xilinx_dma_tx_descriptor *desc, *tail_desc;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530986 u32 reg;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530987 struct xilinx_vdma_tx_segment *tail_segment;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530988
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +0530989 /* This function was invoked with lock held */
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530990 if (chan->err)
991 return;
992
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530993 if (list_empty(&chan->pending_list))
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +0530994 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530995
996 desc = list_first_entry(&chan->pending_list,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530997 struct xilinx_dma_tx_descriptor, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530998 tail_desc = list_last_entry(&chan->pending_list,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530999 struct xilinx_dma_tx_descriptor, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301000
1001 tail_segment = list_last_entry(&tail_desc->segments,
1002 struct xilinx_vdma_tx_segment, node);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301003
1004 /* If it is SG mode and hardware is busy, cannot submit */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301005 if (chan->has_sg && xilinx_dma_is_running(chan) &&
1006 !xilinx_dma_is_idle(chan)) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301007 dev_dbg(chan->dev, "DMA controller still busy\n");
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301008 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301009 }
1010
1011 /*
1012 * If hardware is idle, then all descriptors on the running lists are
1013 * done, start new transfers
1014 */
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301015 if (chan->has_sg)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301016 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301017 desc->async_tx.phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301018
1019 /* Configure the hardware using info in the config structure */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301020 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301021
1022 if (config->frm_cnt_en)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301023 reg |= XILINX_DMA_DMACR_FRAMECNT_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301024 else
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301025 reg &= ~XILINX_DMA_DMACR_FRAMECNT_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301026
Kedareswara rao Appanae2b538a2016-02-26 19:33:53 +05301027 /* Configure channel to allow number frame buffers */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301028 dma_ctrl_write(chan, XILINX_DMA_REG_FRMSTORE,
Kedareswara rao Appanae2b538a2016-02-26 19:33:53 +05301029 chan->desc_pendingcount);
1030
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301031 /*
1032 * With SG, start with circular mode, so that BDs can be fetched.
1033 * In direct register mode, if not parking, enable circular mode
1034 */
1035 if (chan->has_sg || !config->park)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301036 reg |= XILINX_DMA_DMACR_CIRC_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301037
1038 if (config->park)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301039 reg &= ~XILINX_DMA_DMACR_CIRC_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301040
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301041 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301042
1043 if (config->park && (config->park_frm >= 0) &&
1044 (config->park_frm < chan->num_frms)) {
1045 if (chan->direction == DMA_MEM_TO_DEV)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301046 dma_write(chan, XILINX_DMA_REG_PARK_PTR,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301047 config->park_frm <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301048 XILINX_DMA_PARK_PTR_RD_REF_SHIFT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301049 else
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301050 dma_write(chan, XILINX_DMA_REG_PARK_PTR,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301051 config->park_frm <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301052 XILINX_DMA_PARK_PTR_WR_REF_SHIFT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301053 }
1054
1055 /* Start the hardware */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301056 xilinx_dma_start(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301057
1058 if (chan->err)
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301059 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301060
1061 /* Start the transfer */
1062 if (chan->has_sg) {
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301063 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301064 tail_segment->phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301065 } else {
1066 struct xilinx_vdma_tx_segment *segment, *last = NULL;
1067 int i = 0;
1068
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +05301069 if (chan->desc_submitcount < chan->num_frms)
1070 i = chan->desc_submitcount;
1071
1072 list_for_each_entry(segment, &desc->segments, node) {
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05301073 if (chan->ext_addr)
1074 vdma_desc_write_64(chan,
1075 XILINX_VDMA_REG_START_ADDRESS_64(i++),
1076 segment->hw.buf_addr,
1077 segment->hw.buf_addr_msb);
1078 else
1079 vdma_desc_write(chan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301080 XILINX_VDMA_REG_START_ADDRESS(i++),
1081 segment->hw.buf_addr);
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05301082
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301083 last = segment;
1084 }
1085
1086 if (!last)
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301087 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301088
1089 /* HW expects these parameters to be same for one transaction */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301090 vdma_desc_write(chan, XILINX_DMA_REG_HSIZE, last->hw.hsize);
1091 vdma_desc_write(chan, XILINX_DMA_REG_FRMDLY_STRIDE,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301092 last->hw.stride);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301093 vdma_desc_write(chan, XILINX_DMA_REG_VSIZE, last->hw.vsize);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301094 }
1095
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +05301096 if (!chan->has_sg) {
1097 list_del(&desc->node);
1098 list_add_tail(&desc->node, &chan->active_list);
1099 chan->desc_submitcount++;
1100 chan->desc_pendingcount--;
1101 if (chan->desc_submitcount == chan->num_frms)
1102 chan->desc_submitcount = 0;
1103 } else {
1104 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1105 chan->desc_pendingcount = 0;
1106 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301107}
1108
1109/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301110 * xilinx_cdma_start_transfer - Starts cdma transfer
1111 * @chan: Driver specific channel struct pointer
1112 */
1113static void xilinx_cdma_start_transfer(struct xilinx_dma_chan *chan)
1114{
1115 struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1116 struct xilinx_cdma_tx_segment *tail_segment;
1117 u32 ctrl_reg = dma_read(chan, XILINX_DMA_REG_DMACR);
1118
1119 if (chan->err)
1120 return;
1121
1122 if (list_empty(&chan->pending_list))
1123 return;
1124
1125 head_desc = list_first_entry(&chan->pending_list,
1126 struct xilinx_dma_tx_descriptor, node);
1127 tail_desc = list_last_entry(&chan->pending_list,
1128 struct xilinx_dma_tx_descriptor, node);
1129 tail_segment = list_last_entry(&tail_desc->segments,
1130 struct xilinx_cdma_tx_segment, node);
1131
1132 if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1133 ctrl_reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1134 ctrl_reg |= chan->desc_pendingcount <<
1135 XILINX_DMA_CR_COALESCE_SHIFT;
1136 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, ctrl_reg);
1137 }
1138
1139 if (chan->has_sg) {
1140 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
1141 head_desc->async_tx.phys);
1142
1143 /* Update tail ptr register which will start the transfer */
1144 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
1145 tail_segment->phys);
1146 } else {
1147 /* In simple mode */
1148 struct xilinx_cdma_tx_segment *segment;
1149 struct xilinx_cdma_desc_hw *hw;
1150
1151 segment = list_first_entry(&head_desc->segments,
1152 struct xilinx_cdma_tx_segment,
1153 node);
1154
1155 hw = &segment->hw;
1156
1157 dma_ctrl_write(chan, XILINX_CDMA_REG_SRCADDR, hw->src_addr);
1158 dma_ctrl_write(chan, XILINX_CDMA_REG_DSTADDR, hw->dest_addr);
1159
1160 /* Start the transfer */
1161 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1162 hw->control & XILINX_DMA_MAX_TRANS_LEN);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301163 }
1164
1165 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1166 chan->desc_pendingcount = 0;
1167}
1168
1169/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301170 * xilinx_dma_start_transfer - Starts DMA transfer
1171 * @chan: Driver specific channel struct pointer
1172 */
1173static void xilinx_dma_start_transfer(struct xilinx_dma_chan *chan)
1174{
1175 struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1176 struct xilinx_axidma_tx_segment *tail_segment, *old_head, *new_head;
1177 u32 reg;
1178
1179 if (chan->err)
1180 return;
1181
1182 if (list_empty(&chan->pending_list))
1183 return;
1184
1185 /* If it is SG mode and hardware is busy, cannot submit */
1186 if (chan->has_sg && xilinx_dma_is_running(chan) &&
1187 !xilinx_dma_is_idle(chan)) {
1188 dev_dbg(chan->dev, "DMA controller still busy\n");
1189 return;
1190 }
1191
1192 head_desc = list_first_entry(&chan->pending_list,
1193 struct xilinx_dma_tx_descriptor, node);
1194 tail_desc = list_last_entry(&chan->pending_list,
1195 struct xilinx_dma_tx_descriptor, node);
1196 tail_segment = list_last_entry(&tail_desc->segments,
1197 struct xilinx_axidma_tx_segment, node);
1198
1199 old_head = list_first_entry(&head_desc->segments,
1200 struct xilinx_axidma_tx_segment, node);
1201 new_head = chan->seg_v;
1202 /* Copy Buffer Descriptor fields. */
1203 new_head->hw = old_head->hw;
1204
1205 /* Swap and save new reserve */
1206 list_replace_init(&old_head->node, &new_head->node);
1207 chan->seg_v = old_head;
1208
1209 tail_segment->hw.next_desc = chan->seg_v->phys;
1210 head_desc->async_tx.phys = new_head->phys;
1211
1212 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1213
1214 if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1215 reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1216 reg |= chan->desc_pendingcount <<
1217 XILINX_DMA_CR_COALESCE_SHIFT;
1218 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1219 }
1220
1221 if (chan->has_sg)
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301222 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1223 head_desc->async_tx.phys);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301224
1225 xilinx_dma_start(chan);
1226
1227 if (chan->err)
1228 return;
1229
1230 /* Start the transfer */
1231 if (chan->has_sg) {
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301232 if (chan->cyclic)
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301233 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1234 chan->cyclic_seg_v->phys);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301235 else
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301236 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1237 tail_segment->phys);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301238 } else {
1239 struct xilinx_axidma_tx_segment *segment;
1240 struct xilinx_axidma_desc_hw *hw;
1241
1242 segment = list_first_entry(&head_desc->segments,
1243 struct xilinx_axidma_tx_segment,
1244 node);
1245 hw = &segment->hw;
1246
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301247 xilinx_write(chan, XILINX_DMA_REG_SRCDSTADDR, hw->buf_addr);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301248
1249 /* Start the transfer */
1250 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1251 hw->control & XILINX_DMA_MAX_TRANS_LEN);
1252 }
1253
1254 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1255 chan->desc_pendingcount = 0;
1256}
1257
1258/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301259 * xilinx_dma_issue_pending - Issue pending transactions
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301260 * @dchan: DMA channel
1261 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301262static void xilinx_dma_issue_pending(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301263{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301264 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301265 unsigned long flags;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301266
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301267 spin_lock_irqsave(&chan->lock, flags);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301268 chan->start_transfer(chan);
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301269 spin_unlock_irqrestore(&chan->lock, flags);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301270}
1271
1272/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301273 * xilinx_dma_complete_descriptor - Mark the active descriptor as complete
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301274 * @chan : xilinx DMA channel
1275 *
1276 * CONTEXT: hardirq
1277 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301278static void xilinx_dma_complete_descriptor(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301279{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301280 struct xilinx_dma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301281
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301282 /* This function was invoked with lock held */
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301283 if (list_empty(&chan->active_list))
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301284 return;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301285
1286 list_for_each_entry_safe(desc, next, &chan->active_list, node) {
1287 list_del(&desc->node);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301288 if (!desc->cyclic)
1289 dma_cookie_complete(&desc->async_tx);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301290 list_add_tail(&desc->node, &chan->done_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301291 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301292}
1293
1294/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301295 * xilinx_dma_reset - Reset DMA channel
1296 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301297 *
1298 * Return: '0' on success and failure value on error
1299 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301300static int xilinx_dma_reset(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301301{
Kedareswara rao Appana69490632016-03-03 23:02:42 +05301302 int err;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301303 u32 tmp;
1304
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301305 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RESET);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301306
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301307 /* Wait for the hardware to finish reset */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301308 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMACR, tmp,
1309 !(tmp & XILINX_DMA_DMACR_RESET), 0,
1310 XILINX_DMA_LOOP_COUNT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301311
Kedareswara rao Appana9495f262016-02-26 19:33:54 +05301312 if (err) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301313 dev_err(chan->dev, "reset timeout, cr %x, sr %x\n",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301314 dma_ctrl_read(chan, XILINX_DMA_REG_DMACR),
1315 dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301316 return -ETIMEDOUT;
1317 }
1318
1319 chan->err = false;
1320
Kedareswara rao Appana9495f262016-02-26 19:33:54 +05301321 return err;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301322}
1323
1324/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301325 * xilinx_dma_chan_reset - Reset DMA channel and enable interrupts
1326 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301327 *
1328 * Return: '0' on success and failure value on error
1329 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301330static int xilinx_dma_chan_reset(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301331{
1332 int err;
1333
1334 /* Reset VDMA */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301335 err = xilinx_dma_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301336 if (err)
1337 return err;
1338
1339 /* Enable interrupts */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301340 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
1341 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301342
1343 return 0;
1344}
1345
1346/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301347 * xilinx_dma_irq_handler - DMA Interrupt handler
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301348 * @irq: IRQ number
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301349 * @data: Pointer to the Xilinx DMA channel structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301350 *
1351 * Return: IRQ_HANDLED/IRQ_NONE
1352 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301353static irqreturn_t xilinx_dma_irq_handler(int irq, void *data)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301354{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301355 struct xilinx_dma_chan *chan = data;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301356 u32 status;
1357
1358 /* Read the status and ack the interrupts. */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301359 status = dma_ctrl_read(chan, XILINX_DMA_REG_DMASR);
1360 if (!(status & XILINX_DMA_DMAXR_ALL_IRQ_MASK))
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301361 return IRQ_NONE;
1362
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301363 dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1364 status & XILINX_DMA_DMAXR_ALL_IRQ_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301365
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301366 if (status & XILINX_DMA_DMASR_ERR_IRQ) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301367 /*
1368 * An error occurred. If C_FLUSH_ON_FSYNC is enabled and the
1369 * error is recoverable, ignore it. Otherwise flag the error.
1370 *
1371 * Only recoverable errors can be cleared in the DMASR register,
1372 * make sure not to write to other error bits to 1.
1373 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301374 u32 errors = status & XILINX_DMA_DMASR_ALL_ERR_MASK;
Kedareswara rao Appana48a59ed2016-04-06 10:44:55 +05301375
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301376 dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1377 errors & XILINX_DMA_DMASR_ERR_RECOVER_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301378
1379 if (!chan->flush_on_fsync ||
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301380 (errors & ~XILINX_DMA_DMASR_ERR_RECOVER_MASK)) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301381 dev_err(chan->dev,
1382 "Channel %p has errors %x, cdr %x tdr %x\n",
1383 chan, errors,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301384 dma_ctrl_read(chan, XILINX_DMA_REG_CURDESC),
1385 dma_ctrl_read(chan, XILINX_DMA_REG_TAILDESC));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301386 chan->err = true;
1387 }
1388 }
1389
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301390 if (status & XILINX_DMA_DMASR_DLY_CNT_IRQ) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301391 /*
1392 * Device takes too long to do the transfer when user requires
1393 * responsiveness.
1394 */
1395 dev_dbg(chan->dev, "Inter-packet latency too long\n");
1396 }
1397
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301398 if (status & XILINX_DMA_DMASR_FRM_CNT_IRQ) {
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301399 spin_lock(&chan->lock);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301400 xilinx_dma_complete_descriptor(chan);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301401 chan->start_transfer(chan);
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301402 spin_unlock(&chan->lock);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301403 }
1404
1405 tasklet_schedule(&chan->tasklet);
1406 return IRQ_HANDLED;
1407}
1408
1409/**
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301410 * append_desc_queue - Queuing descriptor
1411 * @chan: Driver specific dma channel
1412 * @desc: dma transaction descriptor
1413 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301414static void append_desc_queue(struct xilinx_dma_chan *chan,
1415 struct xilinx_dma_tx_descriptor *desc)
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301416{
1417 struct xilinx_vdma_tx_segment *tail_segment;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301418 struct xilinx_dma_tx_descriptor *tail_desc;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301419 struct xilinx_axidma_tx_segment *axidma_tail_segment;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301420 struct xilinx_cdma_tx_segment *cdma_tail_segment;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301421
1422 if (list_empty(&chan->pending_list))
1423 goto append;
1424
1425 /*
1426 * Add the hardware descriptor to the chain of hardware descriptors
1427 * that already exists in memory.
1428 */
1429 tail_desc = list_last_entry(&chan->pending_list,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301430 struct xilinx_dma_tx_descriptor, node);
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05301431 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301432 tail_segment = list_last_entry(&tail_desc->segments,
1433 struct xilinx_vdma_tx_segment,
1434 node);
1435 tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05301436 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301437 cdma_tail_segment = list_last_entry(&tail_desc->segments,
1438 struct xilinx_cdma_tx_segment,
1439 node);
1440 cdma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301441 } else {
1442 axidma_tail_segment = list_last_entry(&tail_desc->segments,
1443 struct xilinx_axidma_tx_segment,
1444 node);
1445 axidma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1446 }
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301447
1448 /*
1449 * Add the software descriptor and all children to the list
1450 * of pending transactions
1451 */
1452append:
1453 list_add_tail(&desc->node, &chan->pending_list);
1454 chan->desc_pendingcount++;
1455
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05301456 if (chan->has_sg && (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA)
1457 && unlikely(chan->desc_pendingcount > chan->num_frms)) {
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301458 dev_dbg(chan->dev, "desc pendingcount is too high\n");
1459 chan->desc_pendingcount = chan->num_frms;
1460 }
1461}
1462
1463/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301464 * xilinx_dma_tx_submit - Submit DMA transaction
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301465 * @tx: Async transaction descriptor
1466 *
1467 * Return: cookie value on success and failure value on error
1468 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301469static dma_cookie_t xilinx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301470{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301471 struct xilinx_dma_tx_descriptor *desc = to_dma_tx_descriptor(tx);
1472 struct xilinx_dma_chan *chan = to_xilinx_chan(tx->chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301473 dma_cookie_t cookie;
1474 unsigned long flags;
1475 int err;
1476
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301477 if (chan->cyclic) {
1478 xilinx_dma_free_tx_descriptor(chan, desc);
1479 return -EBUSY;
1480 }
1481
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301482 if (chan->err) {
1483 /*
1484 * If reset fails, need to hard reset the system.
1485 * Channel is no longer functional
1486 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301487 err = xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301488 if (err < 0)
1489 return err;
1490 }
1491
1492 spin_lock_irqsave(&chan->lock, flags);
1493
1494 cookie = dma_cookie_assign(tx);
1495
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301496 /* Put this transaction onto the tail of the pending queue */
1497 append_desc_queue(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301498
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301499 if (desc->cyclic)
1500 chan->cyclic = true;
1501
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301502 spin_unlock_irqrestore(&chan->lock, flags);
1503
1504 return cookie;
1505}
1506
1507/**
1508 * xilinx_vdma_dma_prep_interleaved - prepare a descriptor for a
1509 * DMA_SLAVE transaction
1510 * @dchan: DMA channel
1511 * @xt: Interleaved template pointer
1512 * @flags: transfer ack flags
1513 *
1514 * Return: Async transaction descriptor on success and NULL on failure
1515 */
1516static struct dma_async_tx_descriptor *
1517xilinx_vdma_dma_prep_interleaved(struct dma_chan *dchan,
1518 struct dma_interleaved_template *xt,
1519 unsigned long flags)
1520{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301521 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1522 struct xilinx_dma_tx_descriptor *desc;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301523 struct xilinx_vdma_tx_segment *segment, *prev = NULL;
1524 struct xilinx_vdma_desc_hw *hw;
1525
1526 if (!is_slave_direction(xt->dir))
1527 return NULL;
1528
1529 if (!xt->numf || !xt->sgl[0].size)
1530 return NULL;
1531
Srikanth Thokalaa5e48e22014-11-05 20:37:01 +02001532 if (xt->frame_size != 1)
1533 return NULL;
1534
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301535 /* Allocate a transaction descriptor. */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301536 desc = xilinx_dma_alloc_tx_descriptor(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301537 if (!desc)
1538 return NULL;
1539
1540 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301541 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301542 async_tx_ack(&desc->async_tx);
1543
1544 /* Allocate the link descriptor from DMA pool */
1545 segment = xilinx_vdma_alloc_tx_segment(chan);
1546 if (!segment)
1547 goto error;
1548
1549 /* Fill in the hardware descriptor */
1550 hw = &segment->hw;
1551 hw->vsize = xt->numf;
1552 hw->hsize = xt->sgl[0].size;
Srikanth Thokala6d80f452014-11-05 20:37:02 +02001553 hw->stride = (xt->sgl[0].icg + xt->sgl[0].size) <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301554 XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301555 hw->stride |= chan->config.frm_dly <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301556 XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301557
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05301558 if (xt->dir != DMA_MEM_TO_DEV) {
1559 if (chan->ext_addr) {
1560 hw->buf_addr = lower_32_bits(xt->dst_start);
1561 hw->buf_addr_msb = upper_32_bits(xt->dst_start);
1562 } else {
1563 hw->buf_addr = xt->dst_start;
1564 }
1565 } else {
1566 if (chan->ext_addr) {
1567 hw->buf_addr = lower_32_bits(xt->src_start);
1568 hw->buf_addr_msb = upper_32_bits(xt->src_start);
1569 } else {
1570 hw->buf_addr = xt->src_start;
1571 }
1572 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301573
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301574 /* Insert the segment into the descriptor segments list. */
1575 list_add_tail(&segment->node, &desc->segments);
1576
1577 prev = segment;
1578
1579 /* Link the last hardware descriptor with the first. */
1580 segment = list_first_entry(&desc->segments,
1581 struct xilinx_vdma_tx_segment, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301582 desc->async_tx.phys = segment->phys;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301583
1584 return &desc->async_tx;
1585
1586error:
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301587 xilinx_dma_free_tx_descriptor(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301588 return NULL;
1589}
1590
1591/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301592 * xilinx_cdma_prep_memcpy - prepare descriptors for a memcpy transaction
1593 * @dchan: DMA channel
1594 * @dma_dst: destination address
1595 * @dma_src: source address
1596 * @len: transfer length
1597 * @flags: transfer ack flags
1598 *
1599 * Return: Async transaction descriptor on success and NULL on failure
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301600 */
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301601static struct dma_async_tx_descriptor *
1602xilinx_cdma_prep_memcpy(struct dma_chan *dchan, dma_addr_t dma_dst,
1603 dma_addr_t dma_src, size_t len, unsigned long flags)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301604{
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301605 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1606 struct xilinx_dma_tx_descriptor *desc;
1607 struct xilinx_cdma_tx_segment *segment, *prev;
1608 struct xilinx_cdma_desc_hw *hw;
1609
1610 if (!len || len > XILINX_DMA_MAX_TRANS_LEN)
1611 return NULL;
1612
1613 desc = xilinx_dma_alloc_tx_descriptor(chan);
1614 if (!desc)
1615 return NULL;
1616
1617 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1618 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1619
1620 /* Allocate the link descriptor from DMA pool */
1621 segment = xilinx_cdma_alloc_tx_segment(chan);
1622 if (!segment)
1623 goto error;
1624
1625 hw = &segment->hw;
1626 hw->control = len;
1627 hw->src_addr = dma_src;
1628 hw->dest_addr = dma_dst;
1629
1630 /* Fill the previous next descriptor with current */
1631 prev = list_last_entry(&desc->segments,
1632 struct xilinx_cdma_tx_segment, node);
1633 prev->hw.next_desc = segment->phys;
1634
1635 /* Insert the segment into the descriptor segments list. */
1636 list_add_tail(&segment->node, &desc->segments);
1637
1638 prev = segment;
1639
1640 /* Link the last hardware descriptor with the first. */
1641 segment = list_first_entry(&desc->segments,
1642 struct xilinx_cdma_tx_segment, node);
1643 desc->async_tx.phys = segment->phys;
1644 prev->hw.next_desc = segment->phys;
1645
1646 return &desc->async_tx;
1647
1648error:
1649 xilinx_dma_free_tx_descriptor(chan, desc);
1650 return NULL;
1651}
1652
1653/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301654 * xilinx_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
1655 * @dchan: DMA channel
1656 * @sgl: scatterlist to transfer to/from
1657 * @sg_len: number of entries in @scatterlist
1658 * @direction: DMA direction
1659 * @flags: transfer ack flags
1660 * @context: APP words of the descriptor
1661 *
1662 * Return: Async transaction descriptor on success and NULL on failure
1663 */
1664static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
1665 struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
1666 enum dma_transfer_direction direction, unsigned long flags,
1667 void *context)
1668{
1669 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1670 struct xilinx_dma_tx_descriptor *desc;
1671 struct xilinx_axidma_tx_segment *segment = NULL, *prev = NULL;
1672 u32 *app_w = (u32 *)context;
1673 struct scatterlist *sg;
1674 size_t copy;
1675 size_t sg_used;
1676 unsigned int i;
1677
1678 if (!is_slave_direction(direction))
1679 return NULL;
1680
1681 /* Allocate a transaction descriptor. */
1682 desc = xilinx_dma_alloc_tx_descriptor(chan);
1683 if (!desc)
1684 return NULL;
1685
1686 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1687 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1688
1689 /* Build transactions using information in the scatter gather list */
1690 for_each_sg(sgl, sg, sg_len, i) {
1691 sg_used = 0;
1692
1693 /* Loop until the entire scatterlist entry is used */
1694 while (sg_used < sg_dma_len(sg)) {
1695 struct xilinx_axidma_desc_hw *hw;
1696
1697 /* Get a free segment */
1698 segment = xilinx_axidma_alloc_tx_segment(chan);
1699 if (!segment)
1700 goto error;
1701
1702 /*
1703 * Calculate the maximum number of bytes to transfer,
1704 * making sure it is less than the hw limit
1705 */
1706 copy = min_t(size_t, sg_dma_len(sg) - sg_used,
1707 XILINX_DMA_MAX_TRANS_LEN);
1708 hw = &segment->hw;
1709
1710 /* Fill in the descriptor */
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301711 xilinx_axidma_buf(chan, hw, sg_dma_address(sg),
1712 sg_used, 0);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301713
1714 hw->control = copy;
1715
1716 if (chan->direction == DMA_MEM_TO_DEV) {
1717 if (app_w)
1718 memcpy(hw->app, app_w, sizeof(u32) *
1719 XILINX_DMA_NUM_APP_WORDS);
1720 }
1721
1722 if (prev)
1723 prev->hw.next_desc = segment->phys;
1724
1725 prev = segment;
1726 sg_used += copy;
1727
1728 /*
1729 * Insert the segment into the descriptor segments
1730 * list.
1731 */
1732 list_add_tail(&segment->node, &desc->segments);
1733 }
1734 }
1735
1736 segment = list_first_entry(&desc->segments,
1737 struct xilinx_axidma_tx_segment, node);
1738 desc->async_tx.phys = segment->phys;
1739 prev->hw.next_desc = segment->phys;
1740
1741 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1742 if (chan->direction == DMA_MEM_TO_DEV) {
1743 segment->hw.control |= XILINX_DMA_BD_SOP;
1744 segment = list_last_entry(&desc->segments,
1745 struct xilinx_axidma_tx_segment,
1746 node);
1747 segment->hw.control |= XILINX_DMA_BD_EOP;
1748 }
1749
1750 return &desc->async_tx;
1751
1752error:
1753 xilinx_dma_free_tx_descriptor(chan, desc);
1754 return NULL;
1755}
1756
1757/**
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301758 * xilinx_dma_prep_dma_cyclic - prepare descriptors for a DMA_SLAVE transaction
1759 * @chan: DMA channel
1760 * @sgl: scatterlist to transfer to/from
1761 * @sg_len: number of entries in @scatterlist
1762 * @direction: DMA direction
1763 * @flags: transfer ack flags
1764 */
1765static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
1766 struct dma_chan *dchan, dma_addr_t buf_addr, size_t buf_len,
1767 size_t period_len, enum dma_transfer_direction direction,
1768 unsigned long flags)
1769{
1770 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1771 struct xilinx_dma_tx_descriptor *desc;
1772 struct xilinx_axidma_tx_segment *segment, *head_segment, *prev = NULL;
1773 size_t copy, sg_used;
1774 unsigned int num_periods;
1775 int i;
1776 u32 reg;
1777
Arnd Bergmannf67c3bd2016-06-13 17:07:33 +02001778 if (!period_len)
1779 return NULL;
1780
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301781 num_periods = buf_len / period_len;
1782
Arnd Bergmannf67c3bd2016-06-13 17:07:33 +02001783 if (!num_periods)
1784 return NULL;
1785
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301786 if (!is_slave_direction(direction))
1787 return NULL;
1788
1789 /* Allocate a transaction descriptor. */
1790 desc = xilinx_dma_alloc_tx_descriptor(chan);
1791 if (!desc)
1792 return NULL;
1793
1794 chan->direction = direction;
1795 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1796 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1797
1798 for (i = 0; i < num_periods; ++i) {
1799 sg_used = 0;
1800
1801 while (sg_used < period_len) {
1802 struct xilinx_axidma_desc_hw *hw;
1803
1804 /* Get a free segment */
1805 segment = xilinx_axidma_alloc_tx_segment(chan);
1806 if (!segment)
1807 goto error;
1808
1809 /*
1810 * Calculate the maximum number of bytes to transfer,
1811 * making sure it is less than the hw limit
1812 */
1813 copy = min_t(size_t, period_len - sg_used,
1814 XILINX_DMA_MAX_TRANS_LEN);
1815 hw = &segment->hw;
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301816 xilinx_axidma_buf(chan, hw, buf_addr, sg_used,
1817 period_len * i);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301818 hw->control = copy;
1819
1820 if (prev)
1821 prev->hw.next_desc = segment->phys;
1822
1823 prev = segment;
1824 sg_used += copy;
1825
1826 /*
1827 * Insert the segment into the descriptor segments
1828 * list.
1829 */
1830 list_add_tail(&segment->node, &desc->segments);
1831 }
1832 }
1833
1834 head_segment = list_first_entry(&desc->segments,
1835 struct xilinx_axidma_tx_segment, node);
1836 desc->async_tx.phys = head_segment->phys;
1837
1838 desc->cyclic = true;
1839 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1840 reg |= XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
1841 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1842
1843 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1844 if (direction == DMA_MEM_TO_DEV) {
Kedareswara rao Appanae167a0b2016-06-09 11:32:12 +05301845 head_segment->hw.control |= XILINX_DMA_BD_SOP;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301846 segment = list_last_entry(&desc->segments,
1847 struct xilinx_axidma_tx_segment,
1848 node);
1849 segment->hw.control |= XILINX_DMA_BD_EOP;
1850 segment->hw.next_desc = (u32) head_segment->phys;
1851 }
1852
1853 return &desc->async_tx;
1854
1855error:
1856 xilinx_dma_free_tx_descriptor(chan, desc);
1857 return NULL;
1858}
1859
1860/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301861 * xilinx_dma_terminate_all - Halt the channel and free descriptors
1862 * @chan: Driver specific DMA Channel pointer
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301863 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301864static int xilinx_dma_terminate_all(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301865{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301866 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301867 u32 reg;
1868
1869 if (chan->cyclic)
1870 xilinx_dma_chan_reset(chan);
Maxime Ripardba714042014-11-17 14:42:38 +01001871
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301872 /* Halt the DMA engine */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301873 xilinx_dma_halt(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301874
1875 /* Remove and free all of the descriptors in the lists */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301876 xilinx_dma_free_descriptors(chan);
Maxime Ripardba714042014-11-17 14:42:38 +01001877
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301878 if (chan->cyclic) {
1879 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1880 reg &= ~XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
1881 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1882 chan->cyclic = false;
1883 }
1884
Maxime Ripardba714042014-11-17 14:42:38 +01001885 return 0;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301886}
1887
1888/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301889 * xilinx_dma_channel_set_config - Configure VDMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301890 * Run-time configuration for Axi VDMA, supports:
1891 * . halt the channel
1892 * . configure interrupt coalescing and inter-packet delay threshold
1893 * . start/stop parking
1894 * . enable genlock
1895 *
1896 * @dchan: DMA channel
1897 * @cfg: VDMA device configuration pointer
1898 *
1899 * Return: '0' on success and failure value on error
1900 */
1901int xilinx_vdma_channel_set_config(struct dma_chan *dchan,
1902 struct xilinx_vdma_config *cfg)
1903{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301904 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301905 u32 dmacr;
1906
1907 if (cfg->reset)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301908 return xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301909
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301910 dmacr = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301911
1912 chan->config.frm_dly = cfg->frm_dly;
1913 chan->config.park = cfg->park;
1914
1915 /* genlock settings */
1916 chan->config.gen_lock = cfg->gen_lock;
1917 chan->config.master = cfg->master;
1918
1919 if (cfg->gen_lock && chan->genlock) {
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301920 dmacr |= XILINX_DMA_DMACR_GENLOCK_EN;
1921 dmacr |= cfg->master << XILINX_DMA_DMACR_MASTER_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301922 }
1923
1924 chan->config.frm_cnt_en = cfg->frm_cnt_en;
1925 if (cfg->park)
1926 chan->config.park_frm = cfg->park_frm;
1927 else
1928 chan->config.park_frm = -1;
1929
1930 chan->config.coalesc = cfg->coalesc;
1931 chan->config.delay = cfg->delay;
1932
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301933 if (cfg->coalesc <= XILINX_DMA_DMACR_FRAME_COUNT_MAX) {
1934 dmacr |= cfg->coalesc << XILINX_DMA_DMACR_FRAME_COUNT_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301935 chan->config.coalesc = cfg->coalesc;
1936 }
1937
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301938 if (cfg->delay <= XILINX_DMA_DMACR_DELAY_MAX) {
1939 dmacr |= cfg->delay << XILINX_DMA_DMACR_DELAY_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301940 chan->config.delay = cfg->delay;
1941 }
1942
1943 /* FSync Source selection */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301944 dmacr &= ~XILINX_DMA_DMACR_FSYNCSRC_MASK;
1945 dmacr |= cfg->ext_fsync << XILINX_DMA_DMACR_FSYNCSRC_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301946
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301947 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, dmacr);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301948
1949 return 0;
1950}
1951EXPORT_SYMBOL(xilinx_vdma_channel_set_config);
1952
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301953/* -----------------------------------------------------------------------------
1954 * Probe and remove
1955 */
1956
1957/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301958 * xilinx_dma_chan_remove - Per Channel remove function
1959 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301960 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301961static void xilinx_dma_chan_remove(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301962{
1963 /* Disable all interrupts */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301964 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
1965 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301966
1967 if (chan->irq > 0)
1968 free_irq(chan->irq, chan);
1969
1970 tasklet_kill(&chan->tasklet);
1971
1972 list_del(&chan->common.device_node);
1973}
1974
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05301975static int axidma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
1976 struct clk **tx_clk, struct clk **rx_clk,
1977 struct clk **sg_clk, struct clk **tmp_clk)
1978{
1979 int err;
1980
1981 *tmp_clk = NULL;
1982
1983 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
1984 if (IS_ERR(*axi_clk)) {
1985 err = PTR_ERR(*axi_clk);
1986 dev_err(&pdev->dev, "failed to get axi_aclk (%u)\n", err);
1987 return err;
1988 }
1989
1990 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
1991 if (IS_ERR(*tx_clk))
1992 *tx_clk = NULL;
1993
1994 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
1995 if (IS_ERR(*rx_clk))
1996 *rx_clk = NULL;
1997
1998 *sg_clk = devm_clk_get(&pdev->dev, "m_axi_sg_aclk");
1999 if (IS_ERR(*sg_clk))
2000 *sg_clk = NULL;
2001
2002 err = clk_prepare_enable(*axi_clk);
2003 if (err) {
2004 dev_err(&pdev->dev, "failed to enable axi_clk (%u)\n", err);
2005 return err;
2006 }
2007
2008 err = clk_prepare_enable(*tx_clk);
2009 if (err) {
2010 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2011 goto err_disable_axiclk;
2012 }
2013
2014 err = clk_prepare_enable(*rx_clk);
2015 if (err) {
2016 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2017 goto err_disable_txclk;
2018 }
2019
2020 err = clk_prepare_enable(*sg_clk);
2021 if (err) {
2022 dev_err(&pdev->dev, "failed to enable sg_clk (%u)\n", err);
2023 goto err_disable_rxclk;
2024 }
2025
2026 return 0;
2027
2028err_disable_rxclk:
2029 clk_disable_unprepare(*rx_clk);
2030err_disable_txclk:
2031 clk_disable_unprepare(*tx_clk);
2032err_disable_axiclk:
2033 clk_disable_unprepare(*axi_clk);
2034
2035 return err;
2036}
2037
2038static int axicdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2039 struct clk **dev_clk, struct clk **tmp_clk,
2040 struct clk **tmp1_clk, struct clk **tmp2_clk)
2041{
2042 int err;
2043
2044 *tmp_clk = NULL;
2045 *tmp1_clk = NULL;
2046 *tmp2_clk = NULL;
2047
2048 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2049 if (IS_ERR(*axi_clk)) {
2050 err = PTR_ERR(*axi_clk);
2051 dev_err(&pdev->dev, "failed to get axi_clk (%u)\n", err);
2052 return err;
2053 }
2054
2055 *dev_clk = devm_clk_get(&pdev->dev, "m_axi_aclk");
2056 if (IS_ERR(*dev_clk)) {
2057 err = PTR_ERR(*dev_clk);
2058 dev_err(&pdev->dev, "failed to get dev_clk (%u)\n", err);
2059 return err;
2060 }
2061
2062 err = clk_prepare_enable(*axi_clk);
2063 if (err) {
2064 dev_err(&pdev->dev, "failed to enable axi_clk (%u)\n", err);
2065 return err;
2066 }
2067
2068 err = clk_prepare_enable(*dev_clk);
2069 if (err) {
2070 dev_err(&pdev->dev, "failed to enable dev_clk (%u)\n", err);
2071 goto err_disable_axiclk;
2072 }
2073
2074 return 0;
2075
2076err_disable_axiclk:
2077 clk_disable_unprepare(*axi_clk);
2078
2079 return err;
2080}
2081
2082static int axivdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2083 struct clk **tx_clk, struct clk **txs_clk,
2084 struct clk **rx_clk, struct clk **rxs_clk)
2085{
2086 int err;
2087
2088 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2089 if (IS_ERR(*axi_clk)) {
2090 err = PTR_ERR(*axi_clk);
2091 dev_err(&pdev->dev, "failed to get axi_aclk (%u)\n", err);
2092 return err;
2093 }
2094
2095 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2096 if (IS_ERR(*tx_clk))
2097 *tx_clk = NULL;
2098
2099 *txs_clk = devm_clk_get(&pdev->dev, "m_axis_mm2s_aclk");
2100 if (IS_ERR(*txs_clk))
2101 *txs_clk = NULL;
2102
2103 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2104 if (IS_ERR(*rx_clk))
2105 *rx_clk = NULL;
2106
2107 *rxs_clk = devm_clk_get(&pdev->dev, "s_axis_s2mm_aclk");
2108 if (IS_ERR(*rxs_clk))
2109 *rxs_clk = NULL;
2110
2111 err = clk_prepare_enable(*axi_clk);
2112 if (err) {
2113 dev_err(&pdev->dev, "failed to enable axi_clk (%u)\n", err);
2114 return err;
2115 }
2116
2117 err = clk_prepare_enable(*tx_clk);
2118 if (err) {
2119 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2120 goto err_disable_axiclk;
2121 }
2122
2123 err = clk_prepare_enable(*txs_clk);
2124 if (err) {
2125 dev_err(&pdev->dev, "failed to enable txs_clk (%u)\n", err);
2126 goto err_disable_txclk;
2127 }
2128
2129 err = clk_prepare_enable(*rx_clk);
2130 if (err) {
2131 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2132 goto err_disable_txsclk;
2133 }
2134
2135 err = clk_prepare_enable(*rxs_clk);
2136 if (err) {
2137 dev_err(&pdev->dev, "failed to enable rxs_clk (%u)\n", err);
2138 goto err_disable_rxclk;
2139 }
2140
2141 return 0;
2142
2143err_disable_rxclk:
2144 clk_disable_unprepare(*rx_clk);
2145err_disable_txsclk:
2146 clk_disable_unprepare(*txs_clk);
2147err_disable_txclk:
2148 clk_disable_unprepare(*tx_clk);
2149err_disable_axiclk:
2150 clk_disable_unprepare(*axi_clk);
2151
2152 return err;
2153}
2154
2155static void xdma_disable_allclks(struct xilinx_dma_device *xdev)
2156{
2157 clk_disable_unprepare(xdev->rxs_clk);
2158 clk_disable_unprepare(xdev->rx_clk);
2159 clk_disable_unprepare(xdev->txs_clk);
2160 clk_disable_unprepare(xdev->tx_clk);
2161 clk_disable_unprepare(xdev->axi_clk);
2162}
2163
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302164/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302165 * xilinx_dma_chan_probe - Per Channel Probing
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302166 * It get channel features from the device tree entry and
2167 * initialize special channel handling routines
2168 *
2169 * @xdev: Driver specific device structure
2170 * @node: Device node
2171 *
2172 * Return: '0' on success and failure value on error
2173 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302174static int xilinx_dma_chan_probe(struct xilinx_dma_device *xdev,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302175 struct device_node *node)
2176{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302177 struct xilinx_dma_chan *chan;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302178 bool has_dre = false;
2179 u32 value, width;
2180 int err;
2181
2182 /* Allocate and initialize the channel structure */
2183 chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
2184 if (!chan)
2185 return -ENOMEM;
2186
2187 chan->dev = xdev->dev;
2188 chan->xdev = xdev;
2189 chan->has_sg = xdev->has_sg;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05302190 chan->desc_pendingcount = 0x0;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302191 chan->ext_addr = xdev->ext_addr;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302192
2193 spin_lock_init(&chan->lock);
2194 INIT_LIST_HEAD(&chan->pending_list);
2195 INIT_LIST_HEAD(&chan->done_list);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05302196 INIT_LIST_HEAD(&chan->active_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302197
2198 /* Retrieve the channel properties from the device tree */
2199 has_dre = of_property_read_bool(node, "xlnx,include-dre");
2200
2201 chan->genlock = of_property_read_bool(node, "xlnx,genlock-mode");
2202
2203 err = of_property_read_u32(node, "xlnx,datawidth", &value);
2204 if (err) {
2205 dev_err(xdev->dev, "missing xlnx,datawidth property\n");
2206 return err;
2207 }
2208 width = value >> 3; /* Convert bits to bytes */
2209
2210 /* If data width is greater than 8 bytes, DRE is not in hw */
2211 if (width > 8)
2212 has_dre = false;
2213
2214 if (!has_dre)
2215 xdev->common.copy_align = fls(width - 1);
2216
2217 if (of_device_is_compatible(node, "xlnx,axi-vdma-mm2s-channel")) {
2218 chan->direction = DMA_MEM_TO_DEV;
2219 chan->id = 0;
2220
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302221 chan->ctrl_offset = XILINX_DMA_MM2S_CTRL_OFFSET;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302222 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302223 chan->desc_offset = XILINX_VDMA_MM2S_DESC_OFFSET;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302224
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302225 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2226 xdev->flush_on_fsync == XILINX_DMA_FLUSH_MM2S)
2227 chan->flush_on_fsync = true;
2228 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302229 } else if (of_device_is_compatible(node,
2230 "xlnx,axi-vdma-s2mm-channel")) {
2231 chan->direction = DMA_DEV_TO_MEM;
2232 chan->id = 1;
2233
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302234 chan->ctrl_offset = XILINX_DMA_S2MM_CTRL_OFFSET;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302235 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302236 chan->desc_offset = XILINX_VDMA_S2MM_DESC_OFFSET;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302237
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302238 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2239 xdev->flush_on_fsync == XILINX_DMA_FLUSH_S2MM)
2240 chan->flush_on_fsync = true;
2241 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302242 } else {
2243 dev_err(xdev->dev, "Invalid channel compatible node\n");
2244 return -EINVAL;
2245 }
2246
2247 /* Request the interrupt */
2248 chan->irq = irq_of_parse_and_map(node, 0);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302249 err = request_irq(chan->irq, xilinx_dma_irq_handler, IRQF_SHARED,
2250 "xilinx-dma-controller", chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302251 if (err) {
2252 dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq);
2253 return err;
2254 }
2255
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302256 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA)
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302257 chan->start_transfer = xilinx_dma_start_transfer;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302258 else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA)
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302259 chan->start_transfer = xilinx_cdma_start_transfer;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302260 else
2261 chan->start_transfer = xilinx_vdma_start_transfer;
2262
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302263 /* Initialize the tasklet */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302264 tasklet_init(&chan->tasklet, xilinx_dma_do_tasklet,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302265 (unsigned long)chan);
2266
2267 /*
2268 * Initialize the DMA channel and add it to the DMA engine channels
2269 * list.
2270 */
2271 chan->common.device = &xdev->common;
2272
2273 list_add_tail(&chan->common.device_node, &xdev->common.channels);
2274 xdev->chan[chan->id] = chan;
2275
2276 /* Reset the channel */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302277 err = xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302278 if (err < 0) {
2279 dev_err(xdev->dev, "Reset channel failed\n");
2280 return err;
2281 }
2282
2283 return 0;
2284}
2285
2286/**
2287 * of_dma_xilinx_xlate - Translation function
2288 * @dma_spec: Pointer to DMA specifier as found in the device tree
2289 * @ofdma: Pointer to DMA controller data
2290 *
2291 * Return: DMA channel pointer on success and NULL on error
2292 */
2293static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
2294 struct of_dma *ofdma)
2295{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302296 struct xilinx_dma_device *xdev = ofdma->of_dma_data;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302297 int chan_id = dma_spec->args[0];
2298
Linus Torvaldsa0d3c7c2016-05-19 11:47:18 -07002299 if (chan_id >= XILINX_DMA_MAX_CHANS_PER_DEVICE || !xdev->chan[chan_id])
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302300 return NULL;
2301
2302 return dma_get_slave_channel(&xdev->chan[chan_id]->common);
2303}
2304
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302305static const struct xilinx_dma_config axidma_config = {
2306 .dmatype = XDMA_TYPE_AXIDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302307 .clk_init = axidma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302308};
2309
2310static const struct xilinx_dma_config axicdma_config = {
2311 .dmatype = XDMA_TYPE_CDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302312 .clk_init = axicdma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302313};
2314
2315static const struct xilinx_dma_config axivdma_config = {
2316 .dmatype = XDMA_TYPE_VDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302317 .clk_init = axivdma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302318};
2319
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302320static const struct of_device_id xilinx_dma_of_ids[] = {
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302321 { .compatible = "xlnx,axi-dma-1.00.a", .data = &axidma_config },
2322 { .compatible = "xlnx,axi-cdma-1.00.a", .data = &axicdma_config },
2323 { .compatible = "xlnx,axi-vdma-1.00.a", .data = &axivdma_config },
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302324 {}
2325};
2326MODULE_DEVICE_TABLE(of, xilinx_dma_of_ids);
2327
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302328/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302329 * xilinx_dma_probe - Driver probe function
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302330 * @pdev: Pointer to the platform_device structure
2331 *
2332 * Return: '0' on success and failure value on error
2333 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302334static int xilinx_dma_probe(struct platform_device *pdev)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302335{
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302336 int (*clk_init)(struct platform_device *, struct clk **, struct clk **,
2337 struct clk **, struct clk **, struct clk **)
2338 = axivdma_clk_init;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302339 struct device_node *node = pdev->dev.of_node;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302340 struct xilinx_dma_device *xdev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302341 struct device_node *child, *np = pdev->dev.of_node;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302342 struct resource *io;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302343 u32 num_frames, addr_width;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302344 int i, err;
2345
2346 /* Allocate and initialize the DMA engine structure */
2347 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
2348 if (!xdev)
2349 return -ENOMEM;
2350
2351 xdev->dev = &pdev->dev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302352 if (np) {
2353 const struct of_device_id *match;
2354
2355 match = of_match_node(xilinx_dma_of_ids, np);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302356 if (match && match->data) {
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302357 xdev->dma_config = match->data;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302358 clk_init = xdev->dma_config->clk_init;
2359 }
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302360 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302361
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302362 err = clk_init(pdev, &xdev->axi_clk, &xdev->tx_clk, &xdev->txs_clk,
2363 &xdev->rx_clk, &xdev->rxs_clk);
2364 if (err)
2365 return err;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302366
2367 /* Request and map I/O memory */
2368 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2369 xdev->regs = devm_ioremap_resource(&pdev->dev, io);
2370 if (IS_ERR(xdev->regs))
2371 return PTR_ERR(xdev->regs);
2372
2373 /* Retrieve the DMA engine properties from the device tree */
2374 xdev->has_sg = of_property_read_bool(node, "xlnx,include-sg");
2375
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302376 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302377 err = of_property_read_u32(node, "xlnx,num-fstores",
2378 &num_frames);
2379 if (err < 0) {
2380 dev_err(xdev->dev,
2381 "missing xlnx,num-fstores property\n");
2382 return err;
2383 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302384
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302385 err = of_property_read_u32(node, "xlnx,flush-fsync",
2386 &xdev->flush_on_fsync);
2387 if (err < 0)
2388 dev_warn(xdev->dev,
2389 "missing xlnx,flush-fsync property\n");
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302390 }
2391
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302392 err = of_property_read_u32(node, "xlnx,addrwidth", &addr_width);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302393 if (err < 0)
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302394 dev_warn(xdev->dev, "missing xlnx,addrwidth property\n");
2395
2396 if (addr_width > 32)
2397 xdev->ext_addr = true;
2398 else
2399 xdev->ext_addr = false;
2400
2401 /* Set the dma mask bits */
2402 dma_set_mask(xdev->dev, DMA_BIT_MASK(addr_width));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302403
2404 /* Initialize the DMA engine */
2405 xdev->common.dev = &pdev->dev;
2406
2407 INIT_LIST_HEAD(&xdev->common.channels);
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302408 if (!(xdev->dma_config->dmatype == XDMA_TYPE_CDMA)) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302409 dma_cap_set(DMA_SLAVE, xdev->common.cap_mask);
2410 dma_cap_set(DMA_PRIVATE, xdev->common.cap_mask);
2411 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302412
2413 xdev->common.device_alloc_chan_resources =
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302414 xilinx_dma_alloc_chan_resources;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302415 xdev->common.device_free_chan_resources =
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302416 xilinx_dma_free_chan_resources;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302417 xdev->common.device_terminate_all = xilinx_dma_terminate_all;
2418 xdev->common.device_tx_status = xilinx_dma_tx_status;
2419 xdev->common.device_issue_pending = xilinx_dma_issue_pending;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302420 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302421 dma_cap_set(DMA_CYCLIC, xdev->common.cap_mask);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302422 xdev->common.device_prep_slave_sg = xilinx_dma_prep_slave_sg;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302423 xdev->common.device_prep_dma_cyclic =
2424 xilinx_dma_prep_dma_cyclic;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302425 /* Residue calculation is supported by only AXI DMA */
2426 xdev->common.residue_granularity =
2427 DMA_RESIDUE_GRANULARITY_SEGMENT;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302428 } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302429 dma_cap_set(DMA_MEMCPY, xdev->common.cap_mask);
2430 xdev->common.device_prep_dma_memcpy = xilinx_cdma_prep_memcpy;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302431 } else {
2432 xdev->common.device_prep_interleaved_dma =
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302433 xilinx_vdma_dma_prep_interleaved;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302434 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302435
2436 platform_set_drvdata(pdev, xdev);
2437
2438 /* Initialize the channels */
2439 for_each_child_of_node(node, child) {
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302440 err = xilinx_dma_chan_probe(xdev, child);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302441 if (err < 0)
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302442 goto disable_clks;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302443 }
2444
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302445 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302446 for (i = 0; i < XILINX_DMA_MAX_CHANS_PER_DEVICE; i++)
2447 if (xdev->chan[i])
2448 xdev->chan[i]->num_frms = num_frames;
2449 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302450
2451 /* Register the DMA engine with the core */
2452 dma_async_device_register(&xdev->common);
2453
2454 err = of_dma_controller_register(node, of_dma_xilinx_xlate,
2455 xdev);
2456 if (err < 0) {
2457 dev_err(&pdev->dev, "Unable to register DMA to DT\n");
2458 dma_async_device_unregister(&xdev->common);
2459 goto error;
2460 }
2461
2462 dev_info(&pdev->dev, "Xilinx AXI VDMA Engine Driver Probed!!\n");
2463
2464 return 0;
2465
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302466disable_clks:
2467 xdma_disable_allclks(xdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302468error:
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302469 for (i = 0; i < XILINX_DMA_MAX_CHANS_PER_DEVICE; i++)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302470 if (xdev->chan[i])
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302471 xilinx_dma_chan_remove(xdev->chan[i]);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302472
2473 return err;
2474}
2475
2476/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302477 * xilinx_dma_remove - Driver remove function
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302478 * @pdev: Pointer to the platform_device structure
2479 *
2480 * Return: Always '0'
2481 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302482static int xilinx_dma_remove(struct platform_device *pdev)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302483{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302484 struct xilinx_dma_device *xdev = platform_get_drvdata(pdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302485 int i;
2486
2487 of_dma_controller_free(pdev->dev.of_node);
2488
2489 dma_async_device_unregister(&xdev->common);
2490
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302491 for (i = 0; i < XILINX_DMA_MAX_CHANS_PER_DEVICE; i++)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302492 if (xdev->chan[i])
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302493 xilinx_dma_chan_remove(xdev->chan[i]);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302494
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302495 xdma_disable_allclks(xdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302496
2497 return 0;
2498}
2499
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302500static struct platform_driver xilinx_vdma_driver = {
2501 .driver = {
2502 .name = "xilinx-vdma",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302503 .of_match_table = xilinx_dma_of_ids,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302504 },
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302505 .probe = xilinx_dma_probe,
2506 .remove = xilinx_dma_remove,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302507};
2508
2509module_platform_driver(xilinx_vdma_driver);
2510
2511MODULE_AUTHOR("Xilinx, Inc.");
2512MODULE_DESCRIPTION("Xilinx VDMA driver");
2513MODULE_LICENSE("GPL v2");