blob: 40f754b93c57316fbd4d0a3c12c533b769830acf [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
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530223 * @next_descmsb: Next Descriptor Pointer MSB @0x04
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530224 * @src_addr: Source address @0x08
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530225 * @src_addrmsb: Source address MSB @0x0C
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530226 * @dest_addr: Destination address @0x10
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530227 * @dest_addrmsb: Destination address MSB @0x14
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530228 * @control: Control field @0x18
229 * @status: Status field @0x1C
230 */
231struct xilinx_cdma_desc_hw {
232 u32 next_desc;
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530233 u32 next_desc_msb;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530234 u32 src_addr;
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530235 u32 src_addr_msb;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530236 u32 dest_addr;
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530237 u32 dest_addr_msb;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530238 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) {
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301140 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1141 head_desc->async_tx.phys);
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301142
1143 /* Update tail ptr register which will start the transfer */
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301144 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1145 tail_segment->phys);
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301146 } 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
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301157 xilinx_write(chan, XILINX_CDMA_REG_SRCADDR, hw->src_addr);
1158 xilinx_write(chan, XILINX_CDMA_REG_DSTADDR, hw->dest_addr);
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301159
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;
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301629 if (chan->ext_addr) {
1630 hw->src_addr_msb = upper_32_bits(dma_src);
1631 hw->dest_addr_msb = upper_32_bits(dma_dst);
1632 }
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301633
1634 /* Fill the previous next descriptor with current */
1635 prev = list_last_entry(&desc->segments,
1636 struct xilinx_cdma_tx_segment, node);
1637 prev->hw.next_desc = segment->phys;
1638
1639 /* Insert the segment into the descriptor segments list. */
1640 list_add_tail(&segment->node, &desc->segments);
1641
1642 prev = segment;
1643
1644 /* Link the last hardware descriptor with the first. */
1645 segment = list_first_entry(&desc->segments,
1646 struct xilinx_cdma_tx_segment, node);
1647 desc->async_tx.phys = segment->phys;
1648 prev->hw.next_desc = segment->phys;
1649
1650 return &desc->async_tx;
1651
1652error:
1653 xilinx_dma_free_tx_descriptor(chan, desc);
1654 return NULL;
1655}
1656
1657/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301658 * xilinx_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
1659 * @dchan: DMA channel
1660 * @sgl: scatterlist to transfer to/from
1661 * @sg_len: number of entries in @scatterlist
1662 * @direction: DMA direction
1663 * @flags: transfer ack flags
1664 * @context: APP words of the descriptor
1665 *
1666 * Return: Async transaction descriptor on success and NULL on failure
1667 */
1668static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
1669 struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
1670 enum dma_transfer_direction direction, unsigned long flags,
1671 void *context)
1672{
1673 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1674 struct xilinx_dma_tx_descriptor *desc;
1675 struct xilinx_axidma_tx_segment *segment = NULL, *prev = NULL;
1676 u32 *app_w = (u32 *)context;
1677 struct scatterlist *sg;
1678 size_t copy;
1679 size_t sg_used;
1680 unsigned int i;
1681
1682 if (!is_slave_direction(direction))
1683 return NULL;
1684
1685 /* Allocate a transaction descriptor. */
1686 desc = xilinx_dma_alloc_tx_descriptor(chan);
1687 if (!desc)
1688 return NULL;
1689
1690 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1691 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1692
1693 /* Build transactions using information in the scatter gather list */
1694 for_each_sg(sgl, sg, sg_len, i) {
1695 sg_used = 0;
1696
1697 /* Loop until the entire scatterlist entry is used */
1698 while (sg_used < sg_dma_len(sg)) {
1699 struct xilinx_axidma_desc_hw *hw;
1700
1701 /* Get a free segment */
1702 segment = xilinx_axidma_alloc_tx_segment(chan);
1703 if (!segment)
1704 goto error;
1705
1706 /*
1707 * Calculate the maximum number of bytes to transfer,
1708 * making sure it is less than the hw limit
1709 */
1710 copy = min_t(size_t, sg_dma_len(sg) - sg_used,
1711 XILINX_DMA_MAX_TRANS_LEN);
1712 hw = &segment->hw;
1713
1714 /* Fill in the descriptor */
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301715 xilinx_axidma_buf(chan, hw, sg_dma_address(sg),
1716 sg_used, 0);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301717
1718 hw->control = copy;
1719
1720 if (chan->direction == DMA_MEM_TO_DEV) {
1721 if (app_w)
1722 memcpy(hw->app, app_w, sizeof(u32) *
1723 XILINX_DMA_NUM_APP_WORDS);
1724 }
1725
1726 if (prev)
1727 prev->hw.next_desc = segment->phys;
1728
1729 prev = segment;
1730 sg_used += copy;
1731
1732 /*
1733 * Insert the segment into the descriptor segments
1734 * list.
1735 */
1736 list_add_tail(&segment->node, &desc->segments);
1737 }
1738 }
1739
1740 segment = list_first_entry(&desc->segments,
1741 struct xilinx_axidma_tx_segment, node);
1742 desc->async_tx.phys = segment->phys;
1743 prev->hw.next_desc = segment->phys;
1744
1745 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1746 if (chan->direction == DMA_MEM_TO_DEV) {
1747 segment->hw.control |= XILINX_DMA_BD_SOP;
1748 segment = list_last_entry(&desc->segments,
1749 struct xilinx_axidma_tx_segment,
1750 node);
1751 segment->hw.control |= XILINX_DMA_BD_EOP;
1752 }
1753
1754 return &desc->async_tx;
1755
1756error:
1757 xilinx_dma_free_tx_descriptor(chan, desc);
1758 return NULL;
1759}
1760
1761/**
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301762 * xilinx_dma_prep_dma_cyclic - prepare descriptors for a DMA_SLAVE transaction
1763 * @chan: DMA channel
1764 * @sgl: scatterlist to transfer to/from
1765 * @sg_len: number of entries in @scatterlist
1766 * @direction: DMA direction
1767 * @flags: transfer ack flags
1768 */
1769static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
1770 struct dma_chan *dchan, dma_addr_t buf_addr, size_t buf_len,
1771 size_t period_len, enum dma_transfer_direction direction,
1772 unsigned long flags)
1773{
1774 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1775 struct xilinx_dma_tx_descriptor *desc;
1776 struct xilinx_axidma_tx_segment *segment, *head_segment, *prev = NULL;
1777 size_t copy, sg_used;
1778 unsigned int num_periods;
1779 int i;
1780 u32 reg;
1781
Arnd Bergmannf67c3bd2016-06-13 17:07:33 +02001782 if (!period_len)
1783 return NULL;
1784
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301785 num_periods = buf_len / period_len;
1786
Arnd Bergmannf67c3bd2016-06-13 17:07:33 +02001787 if (!num_periods)
1788 return NULL;
1789
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301790 if (!is_slave_direction(direction))
1791 return NULL;
1792
1793 /* Allocate a transaction descriptor. */
1794 desc = xilinx_dma_alloc_tx_descriptor(chan);
1795 if (!desc)
1796 return NULL;
1797
1798 chan->direction = direction;
1799 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1800 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1801
1802 for (i = 0; i < num_periods; ++i) {
1803 sg_used = 0;
1804
1805 while (sg_used < period_len) {
1806 struct xilinx_axidma_desc_hw *hw;
1807
1808 /* Get a free segment */
1809 segment = xilinx_axidma_alloc_tx_segment(chan);
1810 if (!segment)
1811 goto error;
1812
1813 /*
1814 * Calculate the maximum number of bytes to transfer,
1815 * making sure it is less than the hw limit
1816 */
1817 copy = min_t(size_t, period_len - sg_used,
1818 XILINX_DMA_MAX_TRANS_LEN);
1819 hw = &segment->hw;
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301820 xilinx_axidma_buf(chan, hw, buf_addr, sg_used,
1821 period_len * i);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301822 hw->control = copy;
1823
1824 if (prev)
1825 prev->hw.next_desc = segment->phys;
1826
1827 prev = segment;
1828 sg_used += copy;
1829
1830 /*
1831 * Insert the segment into the descriptor segments
1832 * list.
1833 */
1834 list_add_tail(&segment->node, &desc->segments);
1835 }
1836 }
1837
1838 head_segment = list_first_entry(&desc->segments,
1839 struct xilinx_axidma_tx_segment, node);
1840 desc->async_tx.phys = head_segment->phys;
1841
1842 desc->cyclic = true;
1843 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1844 reg |= XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
1845 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1846
1847 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1848 if (direction == DMA_MEM_TO_DEV) {
Kedareswara rao Appanae167a0b2016-06-09 11:32:12 +05301849 head_segment->hw.control |= XILINX_DMA_BD_SOP;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301850 segment = list_last_entry(&desc->segments,
1851 struct xilinx_axidma_tx_segment,
1852 node);
1853 segment->hw.control |= XILINX_DMA_BD_EOP;
1854 segment->hw.next_desc = (u32) head_segment->phys;
1855 }
1856
1857 return &desc->async_tx;
1858
1859error:
1860 xilinx_dma_free_tx_descriptor(chan, desc);
1861 return NULL;
1862}
1863
1864/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301865 * xilinx_dma_terminate_all - Halt the channel and free descriptors
1866 * @chan: Driver specific DMA Channel pointer
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301867 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301868static int xilinx_dma_terminate_all(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301869{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301870 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301871 u32 reg;
1872
1873 if (chan->cyclic)
1874 xilinx_dma_chan_reset(chan);
Maxime Ripardba714042014-11-17 14:42:38 +01001875
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301876 /* Halt the DMA engine */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301877 xilinx_dma_halt(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301878
1879 /* Remove and free all of the descriptors in the lists */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301880 xilinx_dma_free_descriptors(chan);
Maxime Ripardba714042014-11-17 14:42:38 +01001881
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301882 if (chan->cyclic) {
1883 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1884 reg &= ~XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
1885 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1886 chan->cyclic = false;
1887 }
1888
Maxime Ripardba714042014-11-17 14:42:38 +01001889 return 0;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301890}
1891
1892/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301893 * xilinx_dma_channel_set_config - Configure VDMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301894 * Run-time configuration for Axi VDMA, supports:
1895 * . halt the channel
1896 * . configure interrupt coalescing and inter-packet delay threshold
1897 * . start/stop parking
1898 * . enable genlock
1899 *
1900 * @dchan: DMA channel
1901 * @cfg: VDMA device configuration pointer
1902 *
1903 * Return: '0' on success and failure value on error
1904 */
1905int xilinx_vdma_channel_set_config(struct dma_chan *dchan,
1906 struct xilinx_vdma_config *cfg)
1907{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301908 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301909 u32 dmacr;
1910
1911 if (cfg->reset)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301912 return xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301913
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301914 dmacr = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301915
1916 chan->config.frm_dly = cfg->frm_dly;
1917 chan->config.park = cfg->park;
1918
1919 /* genlock settings */
1920 chan->config.gen_lock = cfg->gen_lock;
1921 chan->config.master = cfg->master;
1922
1923 if (cfg->gen_lock && chan->genlock) {
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301924 dmacr |= XILINX_DMA_DMACR_GENLOCK_EN;
1925 dmacr |= cfg->master << XILINX_DMA_DMACR_MASTER_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301926 }
1927
1928 chan->config.frm_cnt_en = cfg->frm_cnt_en;
1929 if (cfg->park)
1930 chan->config.park_frm = cfg->park_frm;
1931 else
1932 chan->config.park_frm = -1;
1933
1934 chan->config.coalesc = cfg->coalesc;
1935 chan->config.delay = cfg->delay;
1936
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301937 if (cfg->coalesc <= XILINX_DMA_DMACR_FRAME_COUNT_MAX) {
1938 dmacr |= cfg->coalesc << XILINX_DMA_DMACR_FRAME_COUNT_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301939 chan->config.coalesc = cfg->coalesc;
1940 }
1941
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301942 if (cfg->delay <= XILINX_DMA_DMACR_DELAY_MAX) {
1943 dmacr |= cfg->delay << XILINX_DMA_DMACR_DELAY_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301944 chan->config.delay = cfg->delay;
1945 }
1946
1947 /* FSync Source selection */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301948 dmacr &= ~XILINX_DMA_DMACR_FSYNCSRC_MASK;
1949 dmacr |= cfg->ext_fsync << XILINX_DMA_DMACR_FSYNCSRC_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301950
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301951 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, dmacr);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301952
1953 return 0;
1954}
1955EXPORT_SYMBOL(xilinx_vdma_channel_set_config);
1956
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301957/* -----------------------------------------------------------------------------
1958 * Probe and remove
1959 */
1960
1961/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301962 * xilinx_dma_chan_remove - Per Channel remove function
1963 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301964 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301965static void xilinx_dma_chan_remove(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301966{
1967 /* Disable all interrupts */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301968 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
1969 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301970
1971 if (chan->irq > 0)
1972 free_irq(chan->irq, chan);
1973
1974 tasklet_kill(&chan->tasklet);
1975
1976 list_del(&chan->common.device_node);
1977}
1978
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05301979static int axidma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
1980 struct clk **tx_clk, struct clk **rx_clk,
1981 struct clk **sg_clk, struct clk **tmp_clk)
1982{
1983 int err;
1984
1985 *tmp_clk = NULL;
1986
1987 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
1988 if (IS_ERR(*axi_clk)) {
1989 err = PTR_ERR(*axi_clk);
1990 dev_err(&pdev->dev, "failed to get axi_aclk (%u)\n", err);
1991 return err;
1992 }
1993
1994 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
1995 if (IS_ERR(*tx_clk))
1996 *tx_clk = NULL;
1997
1998 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
1999 if (IS_ERR(*rx_clk))
2000 *rx_clk = NULL;
2001
2002 *sg_clk = devm_clk_get(&pdev->dev, "m_axi_sg_aclk");
2003 if (IS_ERR(*sg_clk))
2004 *sg_clk = NULL;
2005
2006 err = clk_prepare_enable(*axi_clk);
2007 if (err) {
2008 dev_err(&pdev->dev, "failed to enable axi_clk (%u)\n", err);
2009 return err;
2010 }
2011
2012 err = clk_prepare_enable(*tx_clk);
2013 if (err) {
2014 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2015 goto err_disable_axiclk;
2016 }
2017
2018 err = clk_prepare_enable(*rx_clk);
2019 if (err) {
2020 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2021 goto err_disable_txclk;
2022 }
2023
2024 err = clk_prepare_enable(*sg_clk);
2025 if (err) {
2026 dev_err(&pdev->dev, "failed to enable sg_clk (%u)\n", err);
2027 goto err_disable_rxclk;
2028 }
2029
2030 return 0;
2031
2032err_disable_rxclk:
2033 clk_disable_unprepare(*rx_clk);
2034err_disable_txclk:
2035 clk_disable_unprepare(*tx_clk);
2036err_disable_axiclk:
2037 clk_disable_unprepare(*axi_clk);
2038
2039 return err;
2040}
2041
2042static int axicdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2043 struct clk **dev_clk, struct clk **tmp_clk,
2044 struct clk **tmp1_clk, struct clk **tmp2_clk)
2045{
2046 int err;
2047
2048 *tmp_clk = NULL;
2049 *tmp1_clk = NULL;
2050 *tmp2_clk = NULL;
2051
2052 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2053 if (IS_ERR(*axi_clk)) {
2054 err = PTR_ERR(*axi_clk);
2055 dev_err(&pdev->dev, "failed to get axi_clk (%u)\n", err);
2056 return err;
2057 }
2058
2059 *dev_clk = devm_clk_get(&pdev->dev, "m_axi_aclk");
2060 if (IS_ERR(*dev_clk)) {
2061 err = PTR_ERR(*dev_clk);
2062 dev_err(&pdev->dev, "failed to get dev_clk (%u)\n", err);
2063 return err;
2064 }
2065
2066 err = clk_prepare_enable(*axi_clk);
2067 if (err) {
2068 dev_err(&pdev->dev, "failed to enable axi_clk (%u)\n", err);
2069 return err;
2070 }
2071
2072 err = clk_prepare_enable(*dev_clk);
2073 if (err) {
2074 dev_err(&pdev->dev, "failed to enable dev_clk (%u)\n", err);
2075 goto err_disable_axiclk;
2076 }
2077
2078 return 0;
2079
2080err_disable_axiclk:
2081 clk_disable_unprepare(*axi_clk);
2082
2083 return err;
2084}
2085
2086static int axivdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2087 struct clk **tx_clk, struct clk **txs_clk,
2088 struct clk **rx_clk, struct clk **rxs_clk)
2089{
2090 int err;
2091
2092 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2093 if (IS_ERR(*axi_clk)) {
2094 err = PTR_ERR(*axi_clk);
2095 dev_err(&pdev->dev, "failed to get axi_aclk (%u)\n", err);
2096 return err;
2097 }
2098
2099 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2100 if (IS_ERR(*tx_clk))
2101 *tx_clk = NULL;
2102
2103 *txs_clk = devm_clk_get(&pdev->dev, "m_axis_mm2s_aclk");
2104 if (IS_ERR(*txs_clk))
2105 *txs_clk = NULL;
2106
2107 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2108 if (IS_ERR(*rx_clk))
2109 *rx_clk = NULL;
2110
2111 *rxs_clk = devm_clk_get(&pdev->dev, "s_axis_s2mm_aclk");
2112 if (IS_ERR(*rxs_clk))
2113 *rxs_clk = NULL;
2114
2115 err = clk_prepare_enable(*axi_clk);
2116 if (err) {
2117 dev_err(&pdev->dev, "failed to enable axi_clk (%u)\n", err);
2118 return err;
2119 }
2120
2121 err = clk_prepare_enable(*tx_clk);
2122 if (err) {
2123 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2124 goto err_disable_axiclk;
2125 }
2126
2127 err = clk_prepare_enable(*txs_clk);
2128 if (err) {
2129 dev_err(&pdev->dev, "failed to enable txs_clk (%u)\n", err);
2130 goto err_disable_txclk;
2131 }
2132
2133 err = clk_prepare_enable(*rx_clk);
2134 if (err) {
2135 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2136 goto err_disable_txsclk;
2137 }
2138
2139 err = clk_prepare_enable(*rxs_clk);
2140 if (err) {
2141 dev_err(&pdev->dev, "failed to enable rxs_clk (%u)\n", err);
2142 goto err_disable_rxclk;
2143 }
2144
2145 return 0;
2146
2147err_disable_rxclk:
2148 clk_disable_unprepare(*rx_clk);
2149err_disable_txsclk:
2150 clk_disable_unprepare(*txs_clk);
2151err_disable_txclk:
2152 clk_disable_unprepare(*tx_clk);
2153err_disable_axiclk:
2154 clk_disable_unprepare(*axi_clk);
2155
2156 return err;
2157}
2158
2159static void xdma_disable_allclks(struct xilinx_dma_device *xdev)
2160{
2161 clk_disable_unprepare(xdev->rxs_clk);
2162 clk_disable_unprepare(xdev->rx_clk);
2163 clk_disable_unprepare(xdev->txs_clk);
2164 clk_disable_unprepare(xdev->tx_clk);
2165 clk_disable_unprepare(xdev->axi_clk);
2166}
2167
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302168/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302169 * xilinx_dma_chan_probe - Per Channel Probing
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302170 * It get channel features from the device tree entry and
2171 * initialize special channel handling routines
2172 *
2173 * @xdev: Driver specific device structure
2174 * @node: Device node
2175 *
2176 * Return: '0' on success and failure value on error
2177 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302178static int xilinx_dma_chan_probe(struct xilinx_dma_device *xdev,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302179 struct device_node *node)
2180{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302181 struct xilinx_dma_chan *chan;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302182 bool has_dre = false;
2183 u32 value, width;
2184 int err;
2185
2186 /* Allocate and initialize the channel structure */
2187 chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
2188 if (!chan)
2189 return -ENOMEM;
2190
2191 chan->dev = xdev->dev;
2192 chan->xdev = xdev;
2193 chan->has_sg = xdev->has_sg;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05302194 chan->desc_pendingcount = 0x0;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302195 chan->ext_addr = xdev->ext_addr;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302196
2197 spin_lock_init(&chan->lock);
2198 INIT_LIST_HEAD(&chan->pending_list);
2199 INIT_LIST_HEAD(&chan->done_list);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05302200 INIT_LIST_HEAD(&chan->active_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302201
2202 /* Retrieve the channel properties from the device tree */
2203 has_dre = of_property_read_bool(node, "xlnx,include-dre");
2204
2205 chan->genlock = of_property_read_bool(node, "xlnx,genlock-mode");
2206
2207 err = of_property_read_u32(node, "xlnx,datawidth", &value);
2208 if (err) {
2209 dev_err(xdev->dev, "missing xlnx,datawidth property\n");
2210 return err;
2211 }
2212 width = value >> 3; /* Convert bits to bytes */
2213
2214 /* If data width is greater than 8 bytes, DRE is not in hw */
2215 if (width > 8)
2216 has_dre = false;
2217
2218 if (!has_dre)
2219 xdev->common.copy_align = fls(width - 1);
2220
2221 if (of_device_is_compatible(node, "xlnx,axi-vdma-mm2s-channel")) {
2222 chan->direction = DMA_MEM_TO_DEV;
2223 chan->id = 0;
2224
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302225 chan->ctrl_offset = XILINX_DMA_MM2S_CTRL_OFFSET;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302226 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302227 chan->desc_offset = XILINX_VDMA_MM2S_DESC_OFFSET;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302228
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302229 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2230 xdev->flush_on_fsync == XILINX_DMA_FLUSH_MM2S)
2231 chan->flush_on_fsync = true;
2232 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302233 } else if (of_device_is_compatible(node,
2234 "xlnx,axi-vdma-s2mm-channel")) {
2235 chan->direction = DMA_DEV_TO_MEM;
2236 chan->id = 1;
2237
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302238 chan->ctrl_offset = XILINX_DMA_S2MM_CTRL_OFFSET;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302239 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302240 chan->desc_offset = XILINX_VDMA_S2MM_DESC_OFFSET;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302241
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302242 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2243 xdev->flush_on_fsync == XILINX_DMA_FLUSH_S2MM)
2244 chan->flush_on_fsync = true;
2245 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302246 } else {
2247 dev_err(xdev->dev, "Invalid channel compatible node\n");
2248 return -EINVAL;
2249 }
2250
2251 /* Request the interrupt */
2252 chan->irq = irq_of_parse_and_map(node, 0);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302253 err = request_irq(chan->irq, xilinx_dma_irq_handler, IRQF_SHARED,
2254 "xilinx-dma-controller", chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302255 if (err) {
2256 dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq);
2257 return err;
2258 }
2259
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302260 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA)
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302261 chan->start_transfer = xilinx_dma_start_transfer;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302262 else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA)
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302263 chan->start_transfer = xilinx_cdma_start_transfer;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302264 else
2265 chan->start_transfer = xilinx_vdma_start_transfer;
2266
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302267 /* Initialize the tasklet */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302268 tasklet_init(&chan->tasklet, xilinx_dma_do_tasklet,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302269 (unsigned long)chan);
2270
2271 /*
2272 * Initialize the DMA channel and add it to the DMA engine channels
2273 * list.
2274 */
2275 chan->common.device = &xdev->common;
2276
2277 list_add_tail(&chan->common.device_node, &xdev->common.channels);
2278 xdev->chan[chan->id] = chan;
2279
2280 /* Reset the channel */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302281 err = xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302282 if (err < 0) {
2283 dev_err(xdev->dev, "Reset channel failed\n");
2284 return err;
2285 }
2286
2287 return 0;
2288}
2289
2290/**
2291 * of_dma_xilinx_xlate - Translation function
2292 * @dma_spec: Pointer to DMA specifier as found in the device tree
2293 * @ofdma: Pointer to DMA controller data
2294 *
2295 * Return: DMA channel pointer on success and NULL on error
2296 */
2297static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
2298 struct of_dma *ofdma)
2299{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302300 struct xilinx_dma_device *xdev = ofdma->of_dma_data;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302301 int chan_id = dma_spec->args[0];
2302
Linus Torvaldsa0d3c7c2016-05-19 11:47:18 -07002303 if (chan_id >= XILINX_DMA_MAX_CHANS_PER_DEVICE || !xdev->chan[chan_id])
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302304 return NULL;
2305
2306 return dma_get_slave_channel(&xdev->chan[chan_id]->common);
2307}
2308
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302309static const struct xilinx_dma_config axidma_config = {
2310 .dmatype = XDMA_TYPE_AXIDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302311 .clk_init = axidma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302312};
2313
2314static const struct xilinx_dma_config axicdma_config = {
2315 .dmatype = XDMA_TYPE_CDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302316 .clk_init = axicdma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302317};
2318
2319static const struct xilinx_dma_config axivdma_config = {
2320 .dmatype = XDMA_TYPE_VDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302321 .clk_init = axivdma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302322};
2323
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302324static const struct of_device_id xilinx_dma_of_ids[] = {
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302325 { .compatible = "xlnx,axi-dma-1.00.a", .data = &axidma_config },
2326 { .compatible = "xlnx,axi-cdma-1.00.a", .data = &axicdma_config },
2327 { .compatible = "xlnx,axi-vdma-1.00.a", .data = &axivdma_config },
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302328 {}
2329};
2330MODULE_DEVICE_TABLE(of, xilinx_dma_of_ids);
2331
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302332/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302333 * xilinx_dma_probe - Driver probe function
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302334 * @pdev: Pointer to the platform_device structure
2335 *
2336 * Return: '0' on success and failure value on error
2337 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302338static int xilinx_dma_probe(struct platform_device *pdev)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302339{
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302340 int (*clk_init)(struct platform_device *, struct clk **, struct clk **,
2341 struct clk **, struct clk **, struct clk **)
2342 = axivdma_clk_init;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302343 struct device_node *node = pdev->dev.of_node;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302344 struct xilinx_dma_device *xdev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302345 struct device_node *child, *np = pdev->dev.of_node;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302346 struct resource *io;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302347 u32 num_frames, addr_width;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302348 int i, err;
2349
2350 /* Allocate and initialize the DMA engine structure */
2351 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
2352 if (!xdev)
2353 return -ENOMEM;
2354
2355 xdev->dev = &pdev->dev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302356 if (np) {
2357 const struct of_device_id *match;
2358
2359 match = of_match_node(xilinx_dma_of_ids, np);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302360 if (match && match->data) {
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302361 xdev->dma_config = match->data;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302362 clk_init = xdev->dma_config->clk_init;
2363 }
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302364 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302365
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302366 err = clk_init(pdev, &xdev->axi_clk, &xdev->tx_clk, &xdev->txs_clk,
2367 &xdev->rx_clk, &xdev->rxs_clk);
2368 if (err)
2369 return err;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302370
2371 /* Request and map I/O memory */
2372 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2373 xdev->regs = devm_ioremap_resource(&pdev->dev, io);
2374 if (IS_ERR(xdev->regs))
2375 return PTR_ERR(xdev->regs);
2376
2377 /* Retrieve the DMA engine properties from the device tree */
2378 xdev->has_sg = of_property_read_bool(node, "xlnx,include-sg");
2379
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302380 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302381 err = of_property_read_u32(node, "xlnx,num-fstores",
2382 &num_frames);
2383 if (err < 0) {
2384 dev_err(xdev->dev,
2385 "missing xlnx,num-fstores property\n");
2386 return err;
2387 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302388
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302389 err = of_property_read_u32(node, "xlnx,flush-fsync",
2390 &xdev->flush_on_fsync);
2391 if (err < 0)
2392 dev_warn(xdev->dev,
2393 "missing xlnx,flush-fsync property\n");
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302394 }
2395
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302396 err = of_property_read_u32(node, "xlnx,addrwidth", &addr_width);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302397 if (err < 0)
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302398 dev_warn(xdev->dev, "missing xlnx,addrwidth property\n");
2399
2400 if (addr_width > 32)
2401 xdev->ext_addr = true;
2402 else
2403 xdev->ext_addr = false;
2404
2405 /* Set the dma mask bits */
2406 dma_set_mask(xdev->dev, DMA_BIT_MASK(addr_width));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302407
2408 /* Initialize the DMA engine */
2409 xdev->common.dev = &pdev->dev;
2410
2411 INIT_LIST_HEAD(&xdev->common.channels);
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302412 if (!(xdev->dma_config->dmatype == XDMA_TYPE_CDMA)) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302413 dma_cap_set(DMA_SLAVE, xdev->common.cap_mask);
2414 dma_cap_set(DMA_PRIVATE, xdev->common.cap_mask);
2415 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302416
2417 xdev->common.device_alloc_chan_resources =
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302418 xilinx_dma_alloc_chan_resources;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302419 xdev->common.device_free_chan_resources =
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302420 xilinx_dma_free_chan_resources;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302421 xdev->common.device_terminate_all = xilinx_dma_terminate_all;
2422 xdev->common.device_tx_status = xilinx_dma_tx_status;
2423 xdev->common.device_issue_pending = xilinx_dma_issue_pending;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302424 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302425 dma_cap_set(DMA_CYCLIC, xdev->common.cap_mask);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302426 xdev->common.device_prep_slave_sg = xilinx_dma_prep_slave_sg;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302427 xdev->common.device_prep_dma_cyclic =
2428 xilinx_dma_prep_dma_cyclic;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302429 /* Residue calculation is supported by only AXI DMA */
2430 xdev->common.residue_granularity =
2431 DMA_RESIDUE_GRANULARITY_SEGMENT;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302432 } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302433 dma_cap_set(DMA_MEMCPY, xdev->common.cap_mask);
2434 xdev->common.device_prep_dma_memcpy = xilinx_cdma_prep_memcpy;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302435 } else {
2436 xdev->common.device_prep_interleaved_dma =
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302437 xilinx_vdma_dma_prep_interleaved;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302438 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302439
2440 platform_set_drvdata(pdev, xdev);
2441
2442 /* Initialize the channels */
2443 for_each_child_of_node(node, child) {
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302444 err = xilinx_dma_chan_probe(xdev, child);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302445 if (err < 0)
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302446 goto disable_clks;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302447 }
2448
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302449 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302450 for (i = 0; i < XILINX_DMA_MAX_CHANS_PER_DEVICE; i++)
2451 if (xdev->chan[i])
2452 xdev->chan[i]->num_frms = num_frames;
2453 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302454
2455 /* Register the DMA engine with the core */
2456 dma_async_device_register(&xdev->common);
2457
2458 err = of_dma_controller_register(node, of_dma_xilinx_xlate,
2459 xdev);
2460 if (err < 0) {
2461 dev_err(&pdev->dev, "Unable to register DMA to DT\n");
2462 dma_async_device_unregister(&xdev->common);
2463 goto error;
2464 }
2465
2466 dev_info(&pdev->dev, "Xilinx AXI VDMA Engine Driver Probed!!\n");
2467
2468 return 0;
2469
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302470disable_clks:
2471 xdma_disable_allclks(xdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302472error:
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302473 for (i = 0; i < XILINX_DMA_MAX_CHANS_PER_DEVICE; i++)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302474 if (xdev->chan[i])
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302475 xilinx_dma_chan_remove(xdev->chan[i]);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302476
2477 return err;
2478}
2479
2480/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302481 * xilinx_dma_remove - Driver remove function
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302482 * @pdev: Pointer to the platform_device structure
2483 *
2484 * Return: Always '0'
2485 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302486static int xilinx_dma_remove(struct platform_device *pdev)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302487{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302488 struct xilinx_dma_device *xdev = platform_get_drvdata(pdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302489 int i;
2490
2491 of_dma_controller_free(pdev->dev.of_node);
2492
2493 dma_async_device_unregister(&xdev->common);
2494
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302495 for (i = 0; i < XILINX_DMA_MAX_CHANS_PER_DEVICE; i++)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302496 if (xdev->chan[i])
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302497 xilinx_dma_chan_remove(xdev->chan[i]);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302498
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302499 xdma_disable_allclks(xdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302500
2501 return 0;
2502}
2503
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302504static struct platform_driver xilinx_vdma_driver = {
2505 .driver = {
2506 .name = "xilinx-vdma",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302507 .of_match_table = xilinx_dma_of_ids,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302508 },
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302509 .probe = xilinx_dma_probe,
2510 .remove = xilinx_dma_remove,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302511};
2512
2513module_platform_driver(xilinx_vdma_driver);
2514
2515MODULE_AUTHOR("Xilinx, Inc.");
2516MODULE_DESCRIPTION("Xilinx VDMA driver");
2517MODULE_LICENSE("GPL v2");