blob: 0768d9f783c964d08af97e063244e88f4a5accc4 [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 Appana1a9e7a02016-06-24 10:51:23 +0530117#define XILINX_DMA_MAX_CHANS_PER_DEVICE 0x20
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 Appana1a9e7a02016-06-24 10:51:23 +0530168/* Multi-Channel DMA Descriptor offsets*/
169#define XILINX_DMA_MCRX_CDESC(x) (0x40 + (x-1) * 0x20)
170#define XILINX_DMA_MCRX_TDESC(x) (0x48 + (x-1) * 0x20)
171
172/* Multi-Channel DMA Masks/Shifts */
173#define XILINX_DMA_BD_HSIZE_MASK GENMASK(15, 0)
174#define XILINX_DMA_BD_STRIDE_MASK GENMASK(15, 0)
175#define XILINX_DMA_BD_VSIZE_MASK GENMASK(31, 19)
176#define XILINX_DMA_BD_TDEST_MASK GENMASK(4, 0)
177#define XILINX_DMA_BD_STRIDE_SHIFT 0
178#define XILINX_DMA_BD_VSIZE_SHIFT 19
179
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530180/* AXI CDMA Specific Registers/Offsets */
181#define XILINX_CDMA_REG_SRCADDR 0x18
182#define XILINX_CDMA_REG_DSTADDR 0x20
183
184/* AXI CDMA Specific Masks */
185#define XILINX_CDMA_CR_SGMODE BIT(3)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530186
187/**
188 * struct xilinx_vdma_desc_hw - Hardware Descriptor
189 * @next_desc: Next Descriptor Pointer @0x00
190 * @pad1: Reserved @0x04
191 * @buf_addr: Buffer address @0x08
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530192 * @buf_addr_msb: MSB of Buffer address @0x0C
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530193 * @vsize: Vertical Size @0x10
194 * @hsize: Horizontal Size @0x14
195 * @stride: Number of bytes between the first
196 * pixels of each horizontal line @0x18
197 */
198struct xilinx_vdma_desc_hw {
199 u32 next_desc;
200 u32 pad1;
201 u32 buf_addr;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530202 u32 buf_addr_msb;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530203 u32 vsize;
204 u32 hsize;
205 u32 stride;
206} __aligned(64);
207
208/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530209 * struct xilinx_axidma_desc_hw - Hardware Descriptor for AXI DMA
210 * @next_desc: Next Descriptor Pointer @0x00
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530211 * @next_desc_msb: MSB of Next Descriptor Pointer @0x04
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530212 * @buf_addr: Buffer address @0x08
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530213 * @buf_addr_msb: MSB of Buffer address @0x0C
214 * @pad1: Reserved @0x10
215 * @pad2: Reserved @0x14
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530216 * @control: Control field @0x18
217 * @status: Status field @0x1C
218 * @app: APP Fields @0x20 - 0x30
219 */
220struct xilinx_axidma_desc_hw {
221 u32 next_desc;
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530222 u32 next_desc_msb;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530223 u32 buf_addr;
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530224 u32 buf_addr_msb;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530225 u32 mcdma_control;
226 u32 vsize_stride;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530227 u32 control;
228 u32 status;
229 u32 app[XILINX_DMA_NUM_APP_WORDS];
230} __aligned(64);
231
232/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530233 * struct xilinx_cdma_desc_hw - Hardware Descriptor
234 * @next_desc: Next Descriptor Pointer @0x00
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530235 * @next_descmsb: Next Descriptor Pointer MSB @0x04
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530236 * @src_addr: Source address @0x08
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530237 * @src_addrmsb: Source address MSB @0x0C
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530238 * @dest_addr: Destination address @0x10
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530239 * @dest_addrmsb: Destination address MSB @0x14
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530240 * @control: Control field @0x18
241 * @status: Status field @0x1C
242 */
243struct xilinx_cdma_desc_hw {
244 u32 next_desc;
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530245 u32 next_desc_msb;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530246 u32 src_addr;
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530247 u32 src_addr_msb;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530248 u32 dest_addr;
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530249 u32 dest_addr_msb;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530250 u32 control;
251 u32 status;
252} __aligned(64);
253
254/**
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530255 * struct xilinx_vdma_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_vdma_tx_segment {
261 struct xilinx_vdma_desc_hw hw;
262 struct list_head node;
263 dma_addr_t phys;
264} __aligned(64);
265
266/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530267 * struct xilinx_axidma_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_axidma_tx_segment {
273 struct xilinx_axidma_desc_hw hw;
274 struct list_head node;
275 dma_addr_t phys;
276} __aligned(64);
277
278/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530279 * struct xilinx_cdma_tx_segment - Descriptor segment
280 * @hw: Hardware descriptor
281 * @node: Node in the descriptor segments list
282 * @phys: Physical address of segment
283 */
284struct xilinx_cdma_tx_segment {
285 struct xilinx_cdma_desc_hw hw;
286 struct list_head node;
287 dma_addr_t phys;
288} __aligned(64);
289
290/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530291 * struct xilinx_dma_tx_descriptor - Per Transaction structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530292 * @async_tx: Async transaction descriptor
293 * @segments: TX segments list
294 * @node: Node in the channel descriptors list
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530295 * @cyclic: Check for cyclic transfers.
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530296 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530297struct xilinx_dma_tx_descriptor {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530298 struct dma_async_tx_descriptor async_tx;
299 struct list_head segments;
300 struct list_head node;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530301 bool cyclic;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530302};
303
304/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530305 * struct xilinx_dma_chan - Driver specific DMA channel structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530306 * @xdev: Driver specific device structure
307 * @ctrl_offset: Control registers offset
308 * @desc_offset: TX descriptor registers offset
309 * @lock: Descriptor operation lock
310 * @pending_list: Descriptors waiting
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530311 * @active_list: Descriptors ready to submit
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530312 * @done_list: Complete descriptors
313 * @common: DMA common channel
314 * @desc_pool: Descriptors pool
315 * @dev: The dma device
316 * @irq: Channel IRQ
317 * @id: Channel ID
318 * @direction: Transfer direction
319 * @num_frms: Number of frames
320 * @has_sg: Support scatter transfers
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530321 * @cyclic: Check for cyclic transfers.
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530322 * @genlock: Support genlock mode
323 * @err: Channel has errors
324 * @tasklet: Cleanup work after irq
325 * @config: Device configuration info
326 * @flush_on_fsync: Flush on Frame sync
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530327 * @desc_pendingcount: Descriptor pending count
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530328 * @ext_addr: Indicates 64 bit addressing is supported by dma channel
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +0530329 * @desc_submitcount: Descriptor h/w submitted count
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530330 * @residue: Residue for AXI DMA
331 * @seg_v: Statically allocated segments base
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530332 * @cyclic_seg_v: Statically allocated segment base for cyclic transfers
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530333 * @start_transfer: Differentiate b/w DMA IP's transfer
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530334 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530335struct xilinx_dma_chan {
336 struct xilinx_dma_device *xdev;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530337 u32 ctrl_offset;
338 u32 desc_offset;
339 spinlock_t lock;
340 struct list_head pending_list;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530341 struct list_head active_list;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530342 struct list_head done_list;
343 struct dma_chan common;
344 struct dma_pool *desc_pool;
345 struct device *dev;
346 int irq;
347 int id;
348 enum dma_transfer_direction direction;
349 int num_frms;
350 bool has_sg;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530351 bool cyclic;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530352 bool genlock;
353 bool err;
354 struct tasklet_struct tasklet;
355 struct xilinx_vdma_config config;
356 bool flush_on_fsync;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530357 u32 desc_pendingcount;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530358 bool ext_addr;
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +0530359 u32 desc_submitcount;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530360 u32 residue;
361 struct xilinx_axidma_tx_segment *seg_v;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530362 struct xilinx_axidma_tx_segment *cyclic_seg_v;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530363 void (*start_transfer)(struct xilinx_dma_chan *chan);
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530364 u16 tdest;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530365};
366
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530367struct xilinx_dma_config {
368 enum xdma_ip_type dmatype;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530369 int (*clk_init)(struct platform_device *pdev, struct clk **axi_clk,
370 struct clk **tx_clk, struct clk **txs_clk,
371 struct clk **rx_clk, struct clk **rxs_clk);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530372};
373
374/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530375 * struct xilinx_dma_device - DMA device structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530376 * @regs: I/O mapped base address
377 * @dev: Device Structure
378 * @common: DMA device structure
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530379 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530380 * @has_sg: Specifies whether Scatter-Gather is present or not
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530381 * @mcdma: Specifies whether Multi-Channel is present or not
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530382 * @flush_on_fsync: Flush on frame sync
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530383 * @ext_addr: Indicates 64 bit addressing is supported by dma device
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530384 * @pdev: Platform device structure pointer
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530385 * @dma_config: DMA config structure
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530386 * @axi_clk: DMA Axi4-lite interace clock
387 * @tx_clk: DMA mm2s clock
388 * @txs_clk: DMA mm2s stream clock
389 * @rx_clk: DMA s2mm clock
390 * @rxs_clk: DMA s2mm stream clock
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530391 * @nr_channels: Number of channels DMA device supports
392 * @chan_id: DMA channel identifier
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530393 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530394struct xilinx_dma_device {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530395 void __iomem *regs;
396 struct device *dev;
397 struct dma_device common;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530398 struct xilinx_dma_chan *chan[XILINX_DMA_MAX_CHANS_PER_DEVICE];
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530399 bool has_sg;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530400 bool mcdma;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530401 u32 flush_on_fsync;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530402 bool ext_addr;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530403 struct platform_device *pdev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530404 const struct xilinx_dma_config *dma_config;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530405 struct clk *axi_clk;
406 struct clk *tx_clk;
407 struct clk *txs_clk;
408 struct clk *rx_clk;
409 struct clk *rxs_clk;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530410 u32 nr_channels;
411 u32 chan_id;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530412};
413
414/* Macros */
415#define to_xilinx_chan(chan) \
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530416 container_of(chan, struct xilinx_dma_chan, common)
417#define to_dma_tx_descriptor(tx) \
418 container_of(tx, struct xilinx_dma_tx_descriptor, async_tx)
419#define xilinx_dma_poll_timeout(chan, reg, val, cond, delay_us, timeout_us) \
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530420 readl_poll_timeout(chan->xdev->regs + chan->ctrl_offset + reg, val, \
421 cond, delay_us, timeout_us)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530422
423/* IO accessors */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530424static inline u32 dma_read(struct xilinx_dma_chan *chan, u32 reg)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530425{
426 return ioread32(chan->xdev->regs + reg);
427}
428
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530429static inline void dma_write(struct xilinx_dma_chan *chan, u32 reg, u32 value)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530430{
431 iowrite32(value, chan->xdev->regs + reg);
432}
433
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530434static inline void vdma_desc_write(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530435 u32 value)
436{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530437 dma_write(chan, chan->desc_offset + reg, value);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530438}
439
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530440static inline u32 dma_ctrl_read(struct xilinx_dma_chan *chan, u32 reg)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530441{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530442 return dma_read(chan, chan->ctrl_offset + reg);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530443}
444
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530445static inline void dma_ctrl_write(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530446 u32 value)
447{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530448 dma_write(chan, chan->ctrl_offset + reg, value);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530449}
450
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530451static inline void dma_ctrl_clr(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530452 u32 clr)
453{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530454 dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) & ~clr);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530455}
456
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530457static inline void dma_ctrl_set(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530458 u32 set)
459{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530460 dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) | set);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530461}
462
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530463/**
464 * vdma_desc_write_64 - 64-bit descriptor write
465 * @chan: Driver specific VDMA channel
466 * @reg: Register to write
467 * @value_lsb: lower address of the descriptor.
468 * @value_msb: upper address of the descriptor.
469 *
470 * Since vdma driver is trying to write to a register offset which is not a
471 * multiple of 64 bits(ex : 0x5c), we are writing as two separate 32 bits
472 * instead of a single 64 bit register write.
473 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530474static inline void vdma_desc_write_64(struct xilinx_dma_chan *chan, u32 reg,
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530475 u32 value_lsb, u32 value_msb)
476{
477 /* Write the lsb 32 bits*/
478 writel(value_lsb, chan->xdev->regs + chan->desc_offset + reg);
479
480 /* Write the msb 32 bits */
481 writel(value_msb, chan->xdev->regs + chan->desc_offset + reg + 4);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530482}
483
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530484static inline void dma_writeq(struct xilinx_dma_chan *chan, u32 reg, u64 value)
485{
486 lo_hi_writeq(value, chan->xdev->regs + chan->ctrl_offset + reg);
487}
488
489static inline void xilinx_write(struct xilinx_dma_chan *chan, u32 reg,
490 dma_addr_t addr)
491{
492 if (chan->ext_addr)
493 dma_writeq(chan, reg, addr);
494 else
495 dma_ctrl_write(chan, reg, addr);
496}
497
498static inline void xilinx_axidma_buf(struct xilinx_dma_chan *chan,
499 struct xilinx_axidma_desc_hw *hw,
500 dma_addr_t buf_addr, size_t sg_used,
501 size_t period_len)
502{
503 if (chan->ext_addr) {
504 hw->buf_addr = lower_32_bits(buf_addr + sg_used + period_len);
505 hw->buf_addr_msb = upper_32_bits(buf_addr + sg_used +
506 period_len);
507 } else {
508 hw->buf_addr = buf_addr + sg_used + period_len;
509 }
510}
511
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530512/* -----------------------------------------------------------------------------
513 * Descriptors and segments alloc and free
514 */
515
516/**
517 * xilinx_vdma_alloc_tx_segment - Allocate transaction segment
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530518 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530519 *
520 * Return: The allocated segment on success and NULL on failure.
521 */
522static struct xilinx_vdma_tx_segment *
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530523xilinx_vdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530524{
525 struct xilinx_vdma_tx_segment *segment;
526 dma_addr_t phys;
527
Julia Lawall2ba4f8a2016-04-29 22:09:09 +0200528 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530529 if (!segment)
530 return NULL;
531
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530532 segment->phys = phys;
533
534 return segment;
535}
536
537/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530538 * xilinx_cdma_alloc_tx_segment - Allocate transaction segment
539 * @chan: Driver specific DMA channel
540 *
541 * Return: The allocated segment on success and NULL on failure.
542 */
543static struct xilinx_cdma_tx_segment *
544xilinx_cdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
545{
546 struct xilinx_cdma_tx_segment *segment;
547 dma_addr_t phys;
548
Kedareswara rao Appana62147862016-05-18 13:17:31 +0530549 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530550 if (!segment)
551 return NULL;
552
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530553 segment->phys = phys;
554
555 return segment;
556}
557
558/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530559 * xilinx_axidma_alloc_tx_segment - Allocate transaction segment
560 * @chan: Driver specific DMA channel
561 *
562 * Return: The allocated segment on success and NULL on failure.
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530563 */
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530564static struct xilinx_axidma_tx_segment *
565xilinx_axidma_alloc_tx_segment(struct xilinx_dma_chan *chan)
566{
567 struct xilinx_axidma_tx_segment *segment;
568 dma_addr_t phys;
569
Kedareswara rao Appana62147862016-05-18 13:17:31 +0530570 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530571 if (!segment)
572 return NULL;
573
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530574 segment->phys = phys;
575
576 return segment;
577}
578
579/**
580 * xilinx_dma_free_tx_segment - Free transaction segment
581 * @chan: Driver specific DMA channel
582 * @segment: DMA transaction segment
583 */
584static void xilinx_dma_free_tx_segment(struct xilinx_dma_chan *chan,
585 struct xilinx_axidma_tx_segment *segment)
586{
587 dma_pool_free(chan->desc_pool, segment, segment->phys);
588}
589
590/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530591 * xilinx_cdma_free_tx_segment - Free transaction segment
592 * @chan: Driver specific DMA channel
593 * @segment: DMA transaction segment
594 */
595static void xilinx_cdma_free_tx_segment(struct xilinx_dma_chan *chan,
596 struct xilinx_cdma_tx_segment *segment)
597{
598 dma_pool_free(chan->desc_pool, segment, segment->phys);
599}
600
601/**
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530602 * xilinx_vdma_free_tx_segment - Free transaction segment
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530603 * @chan: Driver specific DMA channel
604 * @segment: DMA transaction segment
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530605 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530606static void xilinx_vdma_free_tx_segment(struct xilinx_dma_chan *chan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530607 struct xilinx_vdma_tx_segment *segment)
608{
609 dma_pool_free(chan->desc_pool, segment, segment->phys);
610}
611
612/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530613 * xilinx_dma_tx_descriptor - Allocate transaction descriptor
614 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530615 *
616 * Return: The allocated descriptor on success and NULL on failure.
617 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530618static struct xilinx_dma_tx_descriptor *
619xilinx_dma_alloc_tx_descriptor(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530620{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530621 struct xilinx_dma_tx_descriptor *desc;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530622
623 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
624 if (!desc)
625 return NULL;
626
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530627 INIT_LIST_HEAD(&desc->segments);
628
629 return desc;
630}
631
632/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530633 * xilinx_dma_free_tx_descriptor - Free transaction descriptor
634 * @chan: Driver specific DMA channel
635 * @desc: DMA transaction descriptor
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530636 */
637static void
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530638xilinx_dma_free_tx_descriptor(struct xilinx_dma_chan *chan,
639 struct xilinx_dma_tx_descriptor *desc)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530640{
641 struct xilinx_vdma_tx_segment *segment, *next;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530642 struct xilinx_cdma_tx_segment *cdma_segment, *cdma_next;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530643 struct xilinx_axidma_tx_segment *axidma_segment, *axidma_next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530644
645 if (!desc)
646 return;
647
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530648 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530649 list_for_each_entry_safe(segment, next, &desc->segments, node) {
650 list_del(&segment->node);
651 xilinx_vdma_free_tx_segment(chan, segment);
652 }
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530653 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530654 list_for_each_entry_safe(cdma_segment, cdma_next,
655 &desc->segments, node) {
656 list_del(&cdma_segment->node);
657 xilinx_cdma_free_tx_segment(chan, cdma_segment);
658 }
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530659 } else {
660 list_for_each_entry_safe(axidma_segment, axidma_next,
661 &desc->segments, node) {
662 list_del(&axidma_segment->node);
663 xilinx_dma_free_tx_segment(chan, axidma_segment);
664 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530665 }
666
667 kfree(desc);
668}
669
670/* Required functions */
671
672/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530673 * xilinx_dma_free_desc_list - Free descriptors list
674 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530675 * @list: List to parse and delete the descriptor
676 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530677static void xilinx_dma_free_desc_list(struct xilinx_dma_chan *chan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530678 struct list_head *list)
679{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530680 struct xilinx_dma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530681
682 list_for_each_entry_safe(desc, next, list, node) {
683 list_del(&desc->node);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530684 xilinx_dma_free_tx_descriptor(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530685 }
686}
687
688/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530689 * xilinx_dma_free_descriptors - Free channel descriptors
690 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530691 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530692static void xilinx_dma_free_descriptors(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530693{
694 unsigned long flags;
695
696 spin_lock_irqsave(&chan->lock, flags);
697
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530698 xilinx_dma_free_desc_list(chan, &chan->pending_list);
699 xilinx_dma_free_desc_list(chan, &chan->done_list);
700 xilinx_dma_free_desc_list(chan, &chan->active_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530701
702 spin_unlock_irqrestore(&chan->lock, flags);
703}
704
705/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530706 * xilinx_dma_free_chan_resources - Free channel resources
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530707 * @dchan: DMA channel
708 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530709static void xilinx_dma_free_chan_resources(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530710{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530711 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530712
713 dev_dbg(chan->dev, "Free all channel resources.\n");
714
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530715 xilinx_dma_free_descriptors(chan);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530716 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
717 xilinx_dma_free_tx_segment(chan, chan->cyclic_seg_v);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530718 xilinx_dma_free_tx_segment(chan, chan->seg_v);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530719 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530720 dma_pool_destroy(chan->desc_pool);
721 chan->desc_pool = NULL;
722}
723
724/**
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530725 * xilinx_dma_chan_handle_cyclic - Cyclic dma callback
726 * @chan: Driver specific dma channel
727 * @desc: dma transaction descriptor
728 * @flags: flags for spin lock
729 */
730static void xilinx_dma_chan_handle_cyclic(struct xilinx_dma_chan *chan,
731 struct xilinx_dma_tx_descriptor *desc,
732 unsigned long *flags)
733{
734 dma_async_tx_callback callback;
735 void *callback_param;
736
737 callback = desc->async_tx.callback;
738 callback_param = desc->async_tx.callback_param;
739 if (callback) {
740 spin_unlock_irqrestore(&chan->lock, *flags);
741 callback(callback_param);
742 spin_lock_irqsave(&chan->lock, *flags);
743 }
744}
745
746/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530747 * xilinx_dma_chan_desc_cleanup - Clean channel descriptors
748 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530749 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530750static void xilinx_dma_chan_desc_cleanup(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530751{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530752 struct xilinx_dma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530753 unsigned long flags;
754
755 spin_lock_irqsave(&chan->lock, flags);
756
757 list_for_each_entry_safe(desc, next, &chan->done_list, node) {
758 dma_async_tx_callback callback;
759 void *callback_param;
760
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530761 if (desc->cyclic) {
762 xilinx_dma_chan_handle_cyclic(chan, desc, &flags);
763 break;
764 }
765
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530766 /* Remove from the list of running transactions */
767 list_del(&desc->node);
768
769 /* Run the link descriptor callback function */
770 callback = desc->async_tx.callback;
771 callback_param = desc->async_tx.callback_param;
772 if (callback) {
773 spin_unlock_irqrestore(&chan->lock, flags);
774 callback(callback_param);
775 spin_lock_irqsave(&chan->lock, flags);
776 }
777
778 /* Run any dependencies, then free the descriptor */
779 dma_run_dependencies(&desc->async_tx);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530780 xilinx_dma_free_tx_descriptor(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530781 }
782
783 spin_unlock_irqrestore(&chan->lock, flags);
784}
785
786/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530787 * xilinx_dma_do_tasklet - Schedule completion tasklet
788 * @data: Pointer to the Xilinx DMA channel structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530789 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530790static void xilinx_dma_do_tasklet(unsigned long data)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530791{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530792 struct xilinx_dma_chan *chan = (struct xilinx_dma_chan *)data;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530793
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530794 xilinx_dma_chan_desc_cleanup(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530795}
796
797/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530798 * xilinx_dma_alloc_chan_resources - Allocate channel resources
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530799 * @dchan: DMA channel
800 *
801 * Return: '0' on success and failure value on error
802 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530803static int xilinx_dma_alloc_chan_resources(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530804{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530805 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530806
807 /* Has this channel already been allocated? */
808 if (chan->desc_pool)
809 return 0;
810
811 /*
812 * We need the descriptor to be aligned to 64bytes
813 * for meeting Xilinx VDMA specification requirement.
814 */
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530815 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530816 chan->desc_pool = dma_pool_create("xilinx_dma_desc_pool",
817 chan->dev,
818 sizeof(struct xilinx_axidma_tx_segment),
819 __alignof__(struct xilinx_axidma_tx_segment),
820 0);
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530821 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530822 chan->desc_pool = dma_pool_create("xilinx_cdma_desc_pool",
823 chan->dev,
824 sizeof(struct xilinx_cdma_tx_segment),
825 __alignof__(struct xilinx_cdma_tx_segment),
826 0);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530827 } else {
828 chan->desc_pool = dma_pool_create("xilinx_vdma_desc_pool",
829 chan->dev,
830 sizeof(struct xilinx_vdma_tx_segment),
831 __alignof__(struct xilinx_vdma_tx_segment),
832 0);
833 }
834
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530835 if (!chan->desc_pool) {
836 dev_err(chan->dev,
837 "unable to allocate channel %d descriptor pool\n",
838 chan->id);
839 return -ENOMEM;
840 }
841
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530842 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530843 /*
844 * For AXI DMA case after submitting a pending_list, keep
845 * an extra segment allocated so that the "next descriptor"
846 * pointer on the tail descriptor always points to a
847 * valid descriptor, even when paused after reaching taildesc.
848 * This way, it is possible to issue additional
849 * transfers without halting and restarting the channel.
850 */
851 chan->seg_v = xilinx_axidma_alloc_tx_segment(chan);
852
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530853 /*
854 * For cyclic DMA mode we need to program the tail Descriptor
855 * register with a value which is not a part of the BD chain
856 * so allocating a desc segment during channel allocation for
857 * programming tail descriptor.
858 */
859 chan->cyclic_seg_v = xilinx_axidma_alloc_tx_segment(chan);
860 }
861
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530862 dma_cookie_init(dchan);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530863
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530864 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530865 /* For AXI DMA resetting once channel will reset the
866 * other channel as well so enable the interrupts here.
867 */
868 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
869 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
870 }
871
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530872 if ((chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) && chan->has_sg)
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530873 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
874 XILINX_CDMA_CR_SGMODE);
875
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530876 return 0;
877}
878
879/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530880 * xilinx_dma_tx_status - Get DMA transaction status
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530881 * @dchan: DMA channel
882 * @cookie: Transaction identifier
883 * @txstate: Transaction state
884 *
885 * Return: DMA transaction status
886 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530887static enum dma_status xilinx_dma_tx_status(struct dma_chan *dchan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530888 dma_cookie_t cookie,
889 struct dma_tx_state *txstate)
890{
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530891 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
892 struct xilinx_dma_tx_descriptor *desc;
893 struct xilinx_axidma_tx_segment *segment;
894 struct xilinx_axidma_desc_hw *hw;
895 enum dma_status ret;
896 unsigned long flags;
897 u32 residue = 0;
898
899 ret = dma_cookie_status(dchan, cookie, txstate);
900 if (ret == DMA_COMPLETE || !txstate)
901 return ret;
902
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530903 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530904 spin_lock_irqsave(&chan->lock, flags);
905
906 desc = list_last_entry(&chan->active_list,
907 struct xilinx_dma_tx_descriptor, node);
908 if (chan->has_sg) {
909 list_for_each_entry(segment, &desc->segments, node) {
910 hw = &segment->hw;
911 residue += (hw->control - hw->status) &
912 XILINX_DMA_MAX_TRANS_LEN;
913 }
914 }
915 spin_unlock_irqrestore(&chan->lock, flags);
916
917 chan->residue = residue;
918 dma_set_residue(txstate, chan->residue);
919 }
920
921 return ret;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530922}
923
924/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530925 * xilinx_dma_is_running - Check if DMA channel is running
926 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530927 *
928 * Return: '1' if running, '0' if not.
929 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530930static bool xilinx_dma_is_running(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530931{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530932 return !(dma_ctrl_read(chan, XILINX_DMA_REG_DMASR) &
933 XILINX_DMA_DMASR_HALTED) &&
934 (dma_ctrl_read(chan, XILINX_DMA_REG_DMACR) &
935 XILINX_DMA_DMACR_RUNSTOP);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530936}
937
938/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530939 * xilinx_dma_is_idle - Check if DMA channel is idle
940 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530941 *
942 * Return: '1' if idle, '0' if not.
943 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530944static bool xilinx_dma_is_idle(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530945{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530946 return dma_ctrl_read(chan, XILINX_DMA_REG_DMASR) &
947 XILINX_DMA_DMASR_IDLE;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530948}
949
950/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530951 * xilinx_dma_halt - Halt DMA channel
952 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530953 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530954static void xilinx_dma_halt(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530955{
Kedareswara rao Appana69490632016-03-03 23:02:42 +0530956 int err;
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530957 u32 val;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530958
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530959 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530960
961 /* Wait for the hardware to halt */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530962 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
963 (val & XILINX_DMA_DMASR_HALTED), 0,
964 XILINX_DMA_LOOP_COUNT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530965
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530966 if (err) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530967 dev_err(chan->dev, "Cannot stop channel %p: %x\n",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530968 chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530969 chan->err = true;
970 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530971}
972
973/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530974 * xilinx_dma_start - Start DMA channel
975 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530976 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530977static void xilinx_dma_start(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530978{
Kedareswara rao Appana69490632016-03-03 23:02:42 +0530979 int err;
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530980 u32 val;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530981
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530982 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530983
984 /* Wait for the hardware to start */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530985 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
986 !(val & XILINX_DMA_DMASR_HALTED), 0,
987 XILINX_DMA_LOOP_COUNT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530988
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530989 if (err) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530990 dev_err(chan->dev, "Cannot start channel %p: %x\n",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530991 chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530992
993 chan->err = true;
994 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530995}
996
997/**
998 * xilinx_vdma_start_transfer - Starts VDMA transfer
999 * @chan: Driver specific channel struct pointer
1000 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301001static void xilinx_vdma_start_transfer(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301002{
1003 struct xilinx_vdma_config *config = &chan->config;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301004 struct xilinx_dma_tx_descriptor *desc, *tail_desc;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301005 u32 reg;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301006 struct xilinx_vdma_tx_segment *tail_segment;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301007
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301008 /* This function was invoked with lock held */
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301009 if (chan->err)
1010 return;
1011
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301012 if (list_empty(&chan->pending_list))
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301013 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301014
1015 desc = list_first_entry(&chan->pending_list,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301016 struct xilinx_dma_tx_descriptor, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301017 tail_desc = list_last_entry(&chan->pending_list,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301018 struct xilinx_dma_tx_descriptor, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301019
1020 tail_segment = list_last_entry(&tail_desc->segments,
1021 struct xilinx_vdma_tx_segment, node);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301022
1023 /* If it is SG mode and hardware is busy, cannot submit */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301024 if (chan->has_sg && xilinx_dma_is_running(chan) &&
1025 !xilinx_dma_is_idle(chan)) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301026 dev_dbg(chan->dev, "DMA controller still busy\n");
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301027 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301028 }
1029
1030 /*
1031 * If hardware is idle, then all descriptors on the running lists are
1032 * done, start new transfers
1033 */
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301034 if (chan->has_sg)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301035 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301036 desc->async_tx.phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301037
1038 /* Configure the hardware using info in the config structure */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301039 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301040
1041 if (config->frm_cnt_en)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301042 reg |= XILINX_DMA_DMACR_FRAMECNT_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301043 else
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301044 reg &= ~XILINX_DMA_DMACR_FRAMECNT_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301045
Kedareswara rao Appanae2b538a2016-02-26 19:33:53 +05301046 /* Configure channel to allow number frame buffers */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301047 dma_ctrl_write(chan, XILINX_DMA_REG_FRMSTORE,
Kedareswara rao Appanae2b538a2016-02-26 19:33:53 +05301048 chan->desc_pendingcount);
1049
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301050 /*
1051 * With SG, start with circular mode, so that BDs can be fetched.
1052 * In direct register mode, if not parking, enable circular mode
1053 */
1054 if (chan->has_sg || !config->park)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301055 reg |= XILINX_DMA_DMACR_CIRC_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301056
1057 if (config->park)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301058 reg &= ~XILINX_DMA_DMACR_CIRC_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301059
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301060 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301061
1062 if (config->park && (config->park_frm >= 0) &&
1063 (config->park_frm < chan->num_frms)) {
1064 if (chan->direction == DMA_MEM_TO_DEV)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301065 dma_write(chan, XILINX_DMA_REG_PARK_PTR,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301066 config->park_frm <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301067 XILINX_DMA_PARK_PTR_RD_REF_SHIFT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301068 else
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301069 dma_write(chan, XILINX_DMA_REG_PARK_PTR,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301070 config->park_frm <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301071 XILINX_DMA_PARK_PTR_WR_REF_SHIFT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301072 }
1073
1074 /* Start the hardware */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301075 xilinx_dma_start(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301076
1077 if (chan->err)
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301078 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301079
1080 /* Start the transfer */
1081 if (chan->has_sg) {
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301082 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301083 tail_segment->phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301084 } else {
1085 struct xilinx_vdma_tx_segment *segment, *last = NULL;
1086 int i = 0;
1087
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +05301088 if (chan->desc_submitcount < chan->num_frms)
1089 i = chan->desc_submitcount;
1090
1091 list_for_each_entry(segment, &desc->segments, node) {
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05301092 if (chan->ext_addr)
1093 vdma_desc_write_64(chan,
1094 XILINX_VDMA_REG_START_ADDRESS_64(i++),
1095 segment->hw.buf_addr,
1096 segment->hw.buf_addr_msb);
1097 else
1098 vdma_desc_write(chan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301099 XILINX_VDMA_REG_START_ADDRESS(i++),
1100 segment->hw.buf_addr);
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05301101
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301102 last = segment;
1103 }
1104
1105 if (!last)
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301106 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301107
1108 /* HW expects these parameters to be same for one transaction */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301109 vdma_desc_write(chan, XILINX_DMA_REG_HSIZE, last->hw.hsize);
1110 vdma_desc_write(chan, XILINX_DMA_REG_FRMDLY_STRIDE,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301111 last->hw.stride);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301112 vdma_desc_write(chan, XILINX_DMA_REG_VSIZE, last->hw.vsize);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301113 }
1114
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +05301115 if (!chan->has_sg) {
1116 list_del(&desc->node);
1117 list_add_tail(&desc->node, &chan->active_list);
1118 chan->desc_submitcount++;
1119 chan->desc_pendingcount--;
1120 if (chan->desc_submitcount == chan->num_frms)
1121 chan->desc_submitcount = 0;
1122 } else {
1123 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1124 chan->desc_pendingcount = 0;
1125 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301126}
1127
1128/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301129 * xilinx_cdma_start_transfer - Starts cdma transfer
1130 * @chan: Driver specific channel struct pointer
1131 */
1132static void xilinx_cdma_start_transfer(struct xilinx_dma_chan *chan)
1133{
1134 struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1135 struct xilinx_cdma_tx_segment *tail_segment;
1136 u32 ctrl_reg = dma_read(chan, XILINX_DMA_REG_DMACR);
1137
1138 if (chan->err)
1139 return;
1140
1141 if (list_empty(&chan->pending_list))
1142 return;
1143
1144 head_desc = list_first_entry(&chan->pending_list,
1145 struct xilinx_dma_tx_descriptor, node);
1146 tail_desc = list_last_entry(&chan->pending_list,
1147 struct xilinx_dma_tx_descriptor, node);
1148 tail_segment = list_last_entry(&tail_desc->segments,
1149 struct xilinx_cdma_tx_segment, node);
1150
1151 if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1152 ctrl_reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1153 ctrl_reg |= chan->desc_pendingcount <<
1154 XILINX_DMA_CR_COALESCE_SHIFT;
1155 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, ctrl_reg);
1156 }
1157
1158 if (chan->has_sg) {
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301159 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1160 head_desc->async_tx.phys);
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301161
1162 /* Update tail ptr register which will start the transfer */
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301163 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1164 tail_segment->phys);
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301165 } else {
1166 /* In simple mode */
1167 struct xilinx_cdma_tx_segment *segment;
1168 struct xilinx_cdma_desc_hw *hw;
1169
1170 segment = list_first_entry(&head_desc->segments,
1171 struct xilinx_cdma_tx_segment,
1172 node);
1173
1174 hw = &segment->hw;
1175
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301176 xilinx_write(chan, XILINX_CDMA_REG_SRCADDR, hw->src_addr);
1177 xilinx_write(chan, XILINX_CDMA_REG_DSTADDR, hw->dest_addr);
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301178
1179 /* Start the transfer */
1180 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1181 hw->control & XILINX_DMA_MAX_TRANS_LEN);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301182 }
1183
1184 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1185 chan->desc_pendingcount = 0;
1186}
1187
1188/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301189 * xilinx_dma_start_transfer - Starts DMA transfer
1190 * @chan: Driver specific channel struct pointer
1191 */
1192static void xilinx_dma_start_transfer(struct xilinx_dma_chan *chan)
1193{
1194 struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1195 struct xilinx_axidma_tx_segment *tail_segment, *old_head, *new_head;
1196 u32 reg;
1197
1198 if (chan->err)
1199 return;
1200
1201 if (list_empty(&chan->pending_list))
1202 return;
1203
1204 /* If it is SG mode and hardware is busy, cannot submit */
1205 if (chan->has_sg && xilinx_dma_is_running(chan) &&
1206 !xilinx_dma_is_idle(chan)) {
1207 dev_dbg(chan->dev, "DMA controller still busy\n");
1208 return;
1209 }
1210
1211 head_desc = list_first_entry(&chan->pending_list,
1212 struct xilinx_dma_tx_descriptor, node);
1213 tail_desc = list_last_entry(&chan->pending_list,
1214 struct xilinx_dma_tx_descriptor, node);
1215 tail_segment = list_last_entry(&tail_desc->segments,
1216 struct xilinx_axidma_tx_segment, node);
1217
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301218 if (chan->has_sg && !chan->xdev->mcdma) {
1219 old_head = list_first_entry(&head_desc->segments,
1220 struct xilinx_axidma_tx_segment, node);
1221 new_head = chan->seg_v;
1222 /* Copy Buffer Descriptor fields. */
1223 new_head->hw = old_head->hw;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301224
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301225 /* Swap and save new reserve */
1226 list_replace_init(&old_head->node, &new_head->node);
1227 chan->seg_v = old_head;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301228
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301229 tail_segment->hw.next_desc = chan->seg_v->phys;
1230 head_desc->async_tx.phys = new_head->phys;
1231 }
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301232
1233 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1234
1235 if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1236 reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1237 reg |= chan->desc_pendingcount <<
1238 XILINX_DMA_CR_COALESCE_SHIFT;
1239 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1240 }
1241
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301242 if (chan->has_sg && !chan->xdev->mcdma)
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301243 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1244 head_desc->async_tx.phys);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301245
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301246 if (chan->has_sg && chan->xdev->mcdma) {
1247 if (chan->direction == DMA_MEM_TO_DEV) {
1248 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
1249 head_desc->async_tx.phys);
1250 } else {
1251 if (!chan->tdest) {
1252 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
1253 head_desc->async_tx.phys);
1254 } else {
1255 dma_ctrl_write(chan,
1256 XILINX_DMA_MCRX_CDESC(chan->tdest),
1257 head_desc->async_tx.phys);
1258 }
1259 }
1260 }
1261
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301262 xilinx_dma_start(chan);
1263
1264 if (chan->err)
1265 return;
1266
1267 /* Start the transfer */
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301268 if (chan->has_sg && !chan->xdev->mcdma) {
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301269 if (chan->cyclic)
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301270 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1271 chan->cyclic_seg_v->phys);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301272 else
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301273 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1274 tail_segment->phys);
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301275 } else if (chan->has_sg && chan->xdev->mcdma) {
1276 if (chan->direction == DMA_MEM_TO_DEV) {
1277 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
1278 tail_segment->phys);
1279 } else {
1280 if (!chan->tdest) {
1281 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
1282 tail_segment->phys);
1283 } else {
1284 dma_ctrl_write(chan,
1285 XILINX_DMA_MCRX_TDESC(chan->tdest),
1286 tail_segment->phys);
1287 }
1288 }
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301289 } else {
1290 struct xilinx_axidma_tx_segment *segment;
1291 struct xilinx_axidma_desc_hw *hw;
1292
1293 segment = list_first_entry(&head_desc->segments,
1294 struct xilinx_axidma_tx_segment,
1295 node);
1296 hw = &segment->hw;
1297
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301298 xilinx_write(chan, XILINX_DMA_REG_SRCDSTADDR, hw->buf_addr);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301299
1300 /* Start the transfer */
1301 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1302 hw->control & XILINX_DMA_MAX_TRANS_LEN);
1303 }
1304
1305 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1306 chan->desc_pendingcount = 0;
1307}
1308
1309/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301310 * xilinx_dma_issue_pending - Issue pending transactions
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301311 * @dchan: DMA channel
1312 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301313static void xilinx_dma_issue_pending(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301314{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301315 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301316 unsigned long flags;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301317
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301318 spin_lock_irqsave(&chan->lock, flags);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301319 chan->start_transfer(chan);
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301320 spin_unlock_irqrestore(&chan->lock, flags);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301321}
1322
1323/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301324 * xilinx_dma_complete_descriptor - Mark the active descriptor as complete
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301325 * @chan : xilinx DMA channel
1326 *
1327 * CONTEXT: hardirq
1328 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301329static void xilinx_dma_complete_descriptor(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301330{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301331 struct xilinx_dma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301332
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301333 /* This function was invoked with lock held */
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301334 if (list_empty(&chan->active_list))
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301335 return;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301336
1337 list_for_each_entry_safe(desc, next, &chan->active_list, node) {
1338 list_del(&desc->node);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301339 if (!desc->cyclic)
1340 dma_cookie_complete(&desc->async_tx);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301341 list_add_tail(&desc->node, &chan->done_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301342 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301343}
1344
1345/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301346 * xilinx_dma_reset - Reset DMA channel
1347 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301348 *
1349 * Return: '0' on success and failure value on error
1350 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301351static int xilinx_dma_reset(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301352{
Kedareswara rao Appana69490632016-03-03 23:02:42 +05301353 int err;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301354 u32 tmp;
1355
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301356 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RESET);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301357
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301358 /* Wait for the hardware to finish reset */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301359 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMACR, tmp,
1360 !(tmp & XILINX_DMA_DMACR_RESET), 0,
1361 XILINX_DMA_LOOP_COUNT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301362
Kedareswara rao Appana9495f262016-02-26 19:33:54 +05301363 if (err) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301364 dev_err(chan->dev, "reset timeout, cr %x, sr %x\n",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301365 dma_ctrl_read(chan, XILINX_DMA_REG_DMACR),
1366 dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301367 return -ETIMEDOUT;
1368 }
1369
1370 chan->err = false;
1371
Kedareswara rao Appana9495f262016-02-26 19:33:54 +05301372 return err;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301373}
1374
1375/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301376 * xilinx_dma_chan_reset - Reset DMA channel and enable interrupts
1377 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301378 *
1379 * Return: '0' on success and failure value on error
1380 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301381static int xilinx_dma_chan_reset(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301382{
1383 int err;
1384
1385 /* Reset VDMA */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301386 err = xilinx_dma_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301387 if (err)
1388 return err;
1389
1390 /* Enable interrupts */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301391 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
1392 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301393
1394 return 0;
1395}
1396
1397/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301398 * xilinx_dma_irq_handler - DMA Interrupt handler
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301399 * @irq: IRQ number
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301400 * @data: Pointer to the Xilinx DMA channel structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301401 *
1402 * Return: IRQ_HANDLED/IRQ_NONE
1403 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301404static irqreturn_t xilinx_dma_irq_handler(int irq, void *data)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301405{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301406 struct xilinx_dma_chan *chan = data;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301407 u32 status;
1408
1409 /* Read the status and ack the interrupts. */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301410 status = dma_ctrl_read(chan, XILINX_DMA_REG_DMASR);
1411 if (!(status & XILINX_DMA_DMAXR_ALL_IRQ_MASK))
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301412 return IRQ_NONE;
1413
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301414 dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1415 status & XILINX_DMA_DMAXR_ALL_IRQ_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301416
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301417 if (status & XILINX_DMA_DMASR_ERR_IRQ) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301418 /*
1419 * An error occurred. If C_FLUSH_ON_FSYNC is enabled and the
1420 * error is recoverable, ignore it. Otherwise flag the error.
1421 *
1422 * Only recoverable errors can be cleared in the DMASR register,
1423 * make sure not to write to other error bits to 1.
1424 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301425 u32 errors = status & XILINX_DMA_DMASR_ALL_ERR_MASK;
Kedareswara rao Appana48a59ed2016-04-06 10:44:55 +05301426
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301427 dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1428 errors & XILINX_DMA_DMASR_ERR_RECOVER_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301429
1430 if (!chan->flush_on_fsync ||
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301431 (errors & ~XILINX_DMA_DMASR_ERR_RECOVER_MASK)) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301432 dev_err(chan->dev,
1433 "Channel %p has errors %x, cdr %x tdr %x\n",
1434 chan, errors,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301435 dma_ctrl_read(chan, XILINX_DMA_REG_CURDESC),
1436 dma_ctrl_read(chan, XILINX_DMA_REG_TAILDESC));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301437 chan->err = true;
1438 }
1439 }
1440
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301441 if (status & XILINX_DMA_DMASR_DLY_CNT_IRQ) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301442 /*
1443 * Device takes too long to do the transfer when user requires
1444 * responsiveness.
1445 */
1446 dev_dbg(chan->dev, "Inter-packet latency too long\n");
1447 }
1448
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301449 if (status & XILINX_DMA_DMASR_FRM_CNT_IRQ) {
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301450 spin_lock(&chan->lock);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301451 xilinx_dma_complete_descriptor(chan);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301452 chan->start_transfer(chan);
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301453 spin_unlock(&chan->lock);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301454 }
1455
1456 tasklet_schedule(&chan->tasklet);
1457 return IRQ_HANDLED;
1458}
1459
1460/**
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301461 * append_desc_queue - Queuing descriptor
1462 * @chan: Driver specific dma channel
1463 * @desc: dma transaction descriptor
1464 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301465static void append_desc_queue(struct xilinx_dma_chan *chan,
1466 struct xilinx_dma_tx_descriptor *desc)
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301467{
1468 struct xilinx_vdma_tx_segment *tail_segment;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301469 struct xilinx_dma_tx_descriptor *tail_desc;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301470 struct xilinx_axidma_tx_segment *axidma_tail_segment;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301471 struct xilinx_cdma_tx_segment *cdma_tail_segment;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301472
1473 if (list_empty(&chan->pending_list))
1474 goto append;
1475
1476 /*
1477 * Add the hardware descriptor to the chain of hardware descriptors
1478 * that already exists in memory.
1479 */
1480 tail_desc = list_last_entry(&chan->pending_list,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301481 struct xilinx_dma_tx_descriptor, node);
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05301482 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301483 tail_segment = list_last_entry(&tail_desc->segments,
1484 struct xilinx_vdma_tx_segment,
1485 node);
1486 tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05301487 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301488 cdma_tail_segment = list_last_entry(&tail_desc->segments,
1489 struct xilinx_cdma_tx_segment,
1490 node);
1491 cdma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301492 } else {
1493 axidma_tail_segment = list_last_entry(&tail_desc->segments,
1494 struct xilinx_axidma_tx_segment,
1495 node);
1496 axidma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1497 }
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301498
1499 /*
1500 * Add the software descriptor and all children to the list
1501 * of pending transactions
1502 */
1503append:
1504 list_add_tail(&desc->node, &chan->pending_list);
1505 chan->desc_pendingcount++;
1506
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05301507 if (chan->has_sg && (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA)
1508 && unlikely(chan->desc_pendingcount > chan->num_frms)) {
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301509 dev_dbg(chan->dev, "desc pendingcount is too high\n");
1510 chan->desc_pendingcount = chan->num_frms;
1511 }
1512}
1513
1514/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301515 * xilinx_dma_tx_submit - Submit DMA transaction
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301516 * @tx: Async transaction descriptor
1517 *
1518 * Return: cookie value on success and failure value on error
1519 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301520static dma_cookie_t xilinx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301521{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301522 struct xilinx_dma_tx_descriptor *desc = to_dma_tx_descriptor(tx);
1523 struct xilinx_dma_chan *chan = to_xilinx_chan(tx->chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301524 dma_cookie_t cookie;
1525 unsigned long flags;
1526 int err;
1527
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301528 if (chan->cyclic) {
1529 xilinx_dma_free_tx_descriptor(chan, desc);
1530 return -EBUSY;
1531 }
1532
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301533 if (chan->err) {
1534 /*
1535 * If reset fails, need to hard reset the system.
1536 * Channel is no longer functional
1537 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301538 err = xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301539 if (err < 0)
1540 return err;
1541 }
1542
1543 spin_lock_irqsave(&chan->lock, flags);
1544
1545 cookie = dma_cookie_assign(tx);
1546
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301547 /* Put this transaction onto the tail of the pending queue */
1548 append_desc_queue(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301549
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301550 if (desc->cyclic)
1551 chan->cyclic = true;
1552
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301553 spin_unlock_irqrestore(&chan->lock, flags);
1554
1555 return cookie;
1556}
1557
1558/**
1559 * xilinx_vdma_dma_prep_interleaved - prepare a descriptor for a
1560 * DMA_SLAVE transaction
1561 * @dchan: DMA channel
1562 * @xt: Interleaved template pointer
1563 * @flags: transfer ack flags
1564 *
1565 * Return: Async transaction descriptor on success and NULL on failure
1566 */
1567static struct dma_async_tx_descriptor *
1568xilinx_vdma_dma_prep_interleaved(struct dma_chan *dchan,
1569 struct dma_interleaved_template *xt,
1570 unsigned long flags)
1571{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301572 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1573 struct xilinx_dma_tx_descriptor *desc;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301574 struct xilinx_vdma_tx_segment *segment, *prev = NULL;
1575 struct xilinx_vdma_desc_hw *hw;
1576
1577 if (!is_slave_direction(xt->dir))
1578 return NULL;
1579
1580 if (!xt->numf || !xt->sgl[0].size)
1581 return NULL;
1582
Srikanth Thokalaa5e48e22014-11-05 20:37:01 +02001583 if (xt->frame_size != 1)
1584 return NULL;
1585
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301586 /* Allocate a transaction descriptor. */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301587 desc = xilinx_dma_alloc_tx_descriptor(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301588 if (!desc)
1589 return NULL;
1590
1591 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301592 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301593 async_tx_ack(&desc->async_tx);
1594
1595 /* Allocate the link descriptor from DMA pool */
1596 segment = xilinx_vdma_alloc_tx_segment(chan);
1597 if (!segment)
1598 goto error;
1599
1600 /* Fill in the hardware descriptor */
1601 hw = &segment->hw;
1602 hw->vsize = xt->numf;
1603 hw->hsize = xt->sgl[0].size;
Srikanth Thokala6d80f452014-11-05 20:37:02 +02001604 hw->stride = (xt->sgl[0].icg + xt->sgl[0].size) <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301605 XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301606 hw->stride |= chan->config.frm_dly <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301607 XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301608
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05301609 if (xt->dir != DMA_MEM_TO_DEV) {
1610 if (chan->ext_addr) {
1611 hw->buf_addr = lower_32_bits(xt->dst_start);
1612 hw->buf_addr_msb = upper_32_bits(xt->dst_start);
1613 } else {
1614 hw->buf_addr = xt->dst_start;
1615 }
1616 } else {
1617 if (chan->ext_addr) {
1618 hw->buf_addr = lower_32_bits(xt->src_start);
1619 hw->buf_addr_msb = upper_32_bits(xt->src_start);
1620 } else {
1621 hw->buf_addr = xt->src_start;
1622 }
1623 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301624
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301625 /* Insert the segment into the descriptor segments list. */
1626 list_add_tail(&segment->node, &desc->segments);
1627
1628 prev = segment;
1629
1630 /* Link the last hardware descriptor with the first. */
1631 segment = list_first_entry(&desc->segments,
1632 struct xilinx_vdma_tx_segment, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301633 desc->async_tx.phys = segment->phys;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301634
1635 return &desc->async_tx;
1636
1637error:
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301638 xilinx_dma_free_tx_descriptor(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301639 return NULL;
1640}
1641
1642/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301643 * xilinx_cdma_prep_memcpy - prepare descriptors for a memcpy transaction
1644 * @dchan: DMA channel
1645 * @dma_dst: destination address
1646 * @dma_src: source address
1647 * @len: transfer length
1648 * @flags: transfer ack flags
1649 *
1650 * Return: Async transaction descriptor on success and NULL on failure
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301651 */
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301652static struct dma_async_tx_descriptor *
1653xilinx_cdma_prep_memcpy(struct dma_chan *dchan, dma_addr_t dma_dst,
1654 dma_addr_t dma_src, size_t len, unsigned long flags)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301655{
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301656 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1657 struct xilinx_dma_tx_descriptor *desc;
1658 struct xilinx_cdma_tx_segment *segment, *prev;
1659 struct xilinx_cdma_desc_hw *hw;
1660
1661 if (!len || len > XILINX_DMA_MAX_TRANS_LEN)
1662 return NULL;
1663
1664 desc = xilinx_dma_alloc_tx_descriptor(chan);
1665 if (!desc)
1666 return NULL;
1667
1668 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1669 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1670
1671 /* Allocate the link descriptor from DMA pool */
1672 segment = xilinx_cdma_alloc_tx_segment(chan);
1673 if (!segment)
1674 goto error;
1675
1676 hw = &segment->hw;
1677 hw->control = len;
1678 hw->src_addr = dma_src;
1679 hw->dest_addr = dma_dst;
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301680 if (chan->ext_addr) {
1681 hw->src_addr_msb = upper_32_bits(dma_src);
1682 hw->dest_addr_msb = upper_32_bits(dma_dst);
1683 }
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301684
1685 /* Fill the previous next descriptor with current */
1686 prev = list_last_entry(&desc->segments,
1687 struct xilinx_cdma_tx_segment, node);
1688 prev->hw.next_desc = segment->phys;
1689
1690 /* Insert the segment into the descriptor segments list. */
1691 list_add_tail(&segment->node, &desc->segments);
1692
1693 prev = segment;
1694
1695 /* Link the last hardware descriptor with the first. */
1696 segment = list_first_entry(&desc->segments,
1697 struct xilinx_cdma_tx_segment, node);
1698 desc->async_tx.phys = segment->phys;
1699 prev->hw.next_desc = segment->phys;
1700
1701 return &desc->async_tx;
1702
1703error:
1704 xilinx_dma_free_tx_descriptor(chan, desc);
1705 return NULL;
1706}
1707
1708/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301709 * xilinx_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
1710 * @dchan: DMA channel
1711 * @sgl: scatterlist to transfer to/from
1712 * @sg_len: number of entries in @scatterlist
1713 * @direction: DMA direction
1714 * @flags: transfer ack flags
1715 * @context: APP words of the descriptor
1716 *
1717 * Return: Async transaction descriptor on success and NULL on failure
1718 */
1719static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
1720 struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
1721 enum dma_transfer_direction direction, unsigned long flags,
1722 void *context)
1723{
1724 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1725 struct xilinx_dma_tx_descriptor *desc;
1726 struct xilinx_axidma_tx_segment *segment = NULL, *prev = NULL;
1727 u32 *app_w = (u32 *)context;
1728 struct scatterlist *sg;
1729 size_t copy;
1730 size_t sg_used;
1731 unsigned int i;
1732
1733 if (!is_slave_direction(direction))
1734 return NULL;
1735
1736 /* Allocate a transaction descriptor. */
1737 desc = xilinx_dma_alloc_tx_descriptor(chan);
1738 if (!desc)
1739 return NULL;
1740
1741 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1742 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1743
1744 /* Build transactions using information in the scatter gather list */
1745 for_each_sg(sgl, sg, sg_len, i) {
1746 sg_used = 0;
1747
1748 /* Loop until the entire scatterlist entry is used */
1749 while (sg_used < sg_dma_len(sg)) {
1750 struct xilinx_axidma_desc_hw *hw;
1751
1752 /* Get a free segment */
1753 segment = xilinx_axidma_alloc_tx_segment(chan);
1754 if (!segment)
1755 goto error;
1756
1757 /*
1758 * Calculate the maximum number of bytes to transfer,
1759 * making sure it is less than the hw limit
1760 */
1761 copy = min_t(size_t, sg_dma_len(sg) - sg_used,
1762 XILINX_DMA_MAX_TRANS_LEN);
1763 hw = &segment->hw;
1764
1765 /* Fill in the descriptor */
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301766 xilinx_axidma_buf(chan, hw, sg_dma_address(sg),
1767 sg_used, 0);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301768
1769 hw->control = copy;
1770
1771 if (chan->direction == DMA_MEM_TO_DEV) {
1772 if (app_w)
1773 memcpy(hw->app, app_w, sizeof(u32) *
1774 XILINX_DMA_NUM_APP_WORDS);
1775 }
1776
1777 if (prev)
1778 prev->hw.next_desc = segment->phys;
1779
1780 prev = segment;
1781 sg_used += copy;
1782
1783 /*
1784 * Insert the segment into the descriptor segments
1785 * list.
1786 */
1787 list_add_tail(&segment->node, &desc->segments);
1788 }
1789 }
1790
1791 segment = list_first_entry(&desc->segments,
1792 struct xilinx_axidma_tx_segment, node);
1793 desc->async_tx.phys = segment->phys;
1794 prev->hw.next_desc = segment->phys;
1795
1796 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1797 if (chan->direction == DMA_MEM_TO_DEV) {
1798 segment->hw.control |= XILINX_DMA_BD_SOP;
1799 segment = list_last_entry(&desc->segments,
1800 struct xilinx_axidma_tx_segment,
1801 node);
1802 segment->hw.control |= XILINX_DMA_BD_EOP;
1803 }
1804
1805 return &desc->async_tx;
1806
1807error:
1808 xilinx_dma_free_tx_descriptor(chan, desc);
1809 return NULL;
1810}
1811
1812/**
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301813 * xilinx_dma_prep_dma_cyclic - prepare descriptors for a DMA_SLAVE transaction
1814 * @chan: DMA channel
1815 * @sgl: scatterlist to transfer to/from
1816 * @sg_len: number of entries in @scatterlist
1817 * @direction: DMA direction
1818 * @flags: transfer ack flags
1819 */
1820static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
1821 struct dma_chan *dchan, dma_addr_t buf_addr, size_t buf_len,
1822 size_t period_len, enum dma_transfer_direction direction,
1823 unsigned long flags)
1824{
1825 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1826 struct xilinx_dma_tx_descriptor *desc;
1827 struct xilinx_axidma_tx_segment *segment, *head_segment, *prev = NULL;
1828 size_t copy, sg_used;
1829 unsigned int num_periods;
1830 int i;
1831 u32 reg;
1832
Arnd Bergmannf67c3bd2016-06-13 17:07:33 +02001833 if (!period_len)
1834 return NULL;
1835
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301836 num_periods = buf_len / period_len;
1837
Arnd Bergmannf67c3bd2016-06-13 17:07:33 +02001838 if (!num_periods)
1839 return NULL;
1840
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301841 if (!is_slave_direction(direction))
1842 return NULL;
1843
1844 /* Allocate a transaction descriptor. */
1845 desc = xilinx_dma_alloc_tx_descriptor(chan);
1846 if (!desc)
1847 return NULL;
1848
1849 chan->direction = direction;
1850 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1851 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1852
1853 for (i = 0; i < num_periods; ++i) {
1854 sg_used = 0;
1855
1856 while (sg_used < period_len) {
1857 struct xilinx_axidma_desc_hw *hw;
1858
1859 /* Get a free segment */
1860 segment = xilinx_axidma_alloc_tx_segment(chan);
1861 if (!segment)
1862 goto error;
1863
1864 /*
1865 * Calculate the maximum number of bytes to transfer,
1866 * making sure it is less than the hw limit
1867 */
1868 copy = min_t(size_t, period_len - sg_used,
1869 XILINX_DMA_MAX_TRANS_LEN);
1870 hw = &segment->hw;
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301871 xilinx_axidma_buf(chan, hw, buf_addr, sg_used,
1872 period_len * i);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301873 hw->control = copy;
1874
1875 if (prev)
1876 prev->hw.next_desc = segment->phys;
1877
1878 prev = segment;
1879 sg_used += copy;
1880
1881 /*
1882 * Insert the segment into the descriptor segments
1883 * list.
1884 */
1885 list_add_tail(&segment->node, &desc->segments);
1886 }
1887 }
1888
1889 head_segment = list_first_entry(&desc->segments,
1890 struct xilinx_axidma_tx_segment, node);
1891 desc->async_tx.phys = head_segment->phys;
1892
1893 desc->cyclic = true;
1894 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1895 reg |= XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
1896 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1897
1898 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1899 if (direction == DMA_MEM_TO_DEV) {
Kedareswara rao Appanae167a0b2016-06-09 11:32:12 +05301900 head_segment->hw.control |= XILINX_DMA_BD_SOP;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301901 segment = list_last_entry(&desc->segments,
1902 struct xilinx_axidma_tx_segment,
1903 node);
1904 segment->hw.control |= XILINX_DMA_BD_EOP;
1905 segment->hw.next_desc = (u32) head_segment->phys;
1906 }
1907
1908 return &desc->async_tx;
1909
1910error:
1911 xilinx_dma_free_tx_descriptor(chan, desc);
1912 return NULL;
1913}
1914
1915/**
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301916 * xilinx_dma_prep_interleaved - prepare a descriptor for a
1917 * DMA_SLAVE transaction
1918 * @dchan: DMA channel
1919 * @xt: Interleaved template pointer
1920 * @flags: transfer ack flags
1921 *
1922 * Return: Async transaction descriptor on success and NULL on failure
1923 */
1924static struct dma_async_tx_descriptor *
1925xilinx_dma_prep_interleaved(struct dma_chan *dchan,
1926 struct dma_interleaved_template *xt,
1927 unsigned long flags)
1928{
1929 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1930 struct xilinx_dma_tx_descriptor *desc;
1931 struct xilinx_axidma_tx_segment *segment;
1932 struct xilinx_axidma_desc_hw *hw;
1933
1934 if (!is_slave_direction(xt->dir))
1935 return NULL;
1936
1937 if (!xt->numf || !xt->sgl[0].size)
1938 return NULL;
1939
1940 if (xt->frame_size != 1)
1941 return NULL;
1942
1943 /* Allocate a transaction descriptor. */
1944 desc = xilinx_dma_alloc_tx_descriptor(chan);
1945 if (!desc)
1946 return NULL;
1947
1948 chan->direction = xt->dir;
1949 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1950 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1951
1952 /* Get a free segment */
1953 segment = xilinx_axidma_alloc_tx_segment(chan);
1954 if (!segment)
1955 goto error;
1956
1957 hw = &segment->hw;
1958
1959 /* Fill in the descriptor */
1960 if (xt->dir != DMA_MEM_TO_DEV)
1961 hw->buf_addr = xt->dst_start;
1962 else
1963 hw->buf_addr = xt->src_start;
1964
1965 hw->mcdma_control = chan->tdest & XILINX_DMA_BD_TDEST_MASK;
1966 hw->vsize_stride = (xt->numf << XILINX_DMA_BD_VSIZE_SHIFT) &
1967 XILINX_DMA_BD_VSIZE_MASK;
1968 hw->vsize_stride |= (xt->sgl[0].icg + xt->sgl[0].size) &
1969 XILINX_DMA_BD_STRIDE_MASK;
1970 hw->control = xt->sgl[0].size & XILINX_DMA_BD_HSIZE_MASK;
1971
1972 /*
1973 * Insert the segment into the descriptor segments
1974 * list.
1975 */
1976 list_add_tail(&segment->node, &desc->segments);
1977
1978
1979 segment = list_first_entry(&desc->segments,
1980 struct xilinx_axidma_tx_segment, node);
1981 desc->async_tx.phys = segment->phys;
1982
1983 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1984 if (xt->dir == DMA_MEM_TO_DEV) {
1985 segment->hw.control |= XILINX_DMA_BD_SOP;
1986 segment = list_last_entry(&desc->segments,
1987 struct xilinx_axidma_tx_segment,
1988 node);
1989 segment->hw.control |= XILINX_DMA_BD_EOP;
1990 }
1991
1992 return &desc->async_tx;
1993
1994error:
1995 xilinx_dma_free_tx_descriptor(chan, desc);
1996 return NULL;
1997}
1998
1999/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302000 * xilinx_dma_terminate_all - Halt the channel and free descriptors
2001 * @chan: Driver specific DMA Channel pointer
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302002 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302003static int xilinx_dma_terminate_all(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302004{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302005 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302006 u32 reg;
2007
2008 if (chan->cyclic)
2009 xilinx_dma_chan_reset(chan);
Maxime Ripardba714042014-11-17 14:42:38 +01002010
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302011 /* Halt the DMA engine */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302012 xilinx_dma_halt(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302013
2014 /* Remove and free all of the descriptors in the lists */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302015 xilinx_dma_free_descriptors(chan);
Maxime Ripardba714042014-11-17 14:42:38 +01002016
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302017 if (chan->cyclic) {
2018 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
2019 reg &= ~XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
2020 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
2021 chan->cyclic = false;
2022 }
2023
Maxime Ripardba714042014-11-17 14:42:38 +01002024 return 0;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302025}
2026
2027/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302028 * xilinx_dma_channel_set_config - Configure VDMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302029 * Run-time configuration for Axi VDMA, supports:
2030 * . halt the channel
2031 * . configure interrupt coalescing and inter-packet delay threshold
2032 * . start/stop parking
2033 * . enable genlock
2034 *
2035 * @dchan: DMA channel
2036 * @cfg: VDMA device configuration pointer
2037 *
2038 * Return: '0' on success and failure value on error
2039 */
2040int xilinx_vdma_channel_set_config(struct dma_chan *dchan,
2041 struct xilinx_vdma_config *cfg)
2042{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302043 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302044 u32 dmacr;
2045
2046 if (cfg->reset)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302047 return xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302048
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302049 dmacr = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302050
2051 chan->config.frm_dly = cfg->frm_dly;
2052 chan->config.park = cfg->park;
2053
2054 /* genlock settings */
2055 chan->config.gen_lock = cfg->gen_lock;
2056 chan->config.master = cfg->master;
2057
2058 if (cfg->gen_lock && chan->genlock) {
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302059 dmacr |= XILINX_DMA_DMACR_GENLOCK_EN;
2060 dmacr |= cfg->master << XILINX_DMA_DMACR_MASTER_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302061 }
2062
2063 chan->config.frm_cnt_en = cfg->frm_cnt_en;
2064 if (cfg->park)
2065 chan->config.park_frm = cfg->park_frm;
2066 else
2067 chan->config.park_frm = -1;
2068
2069 chan->config.coalesc = cfg->coalesc;
2070 chan->config.delay = cfg->delay;
2071
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302072 if (cfg->coalesc <= XILINX_DMA_DMACR_FRAME_COUNT_MAX) {
2073 dmacr |= cfg->coalesc << XILINX_DMA_DMACR_FRAME_COUNT_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302074 chan->config.coalesc = cfg->coalesc;
2075 }
2076
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302077 if (cfg->delay <= XILINX_DMA_DMACR_DELAY_MAX) {
2078 dmacr |= cfg->delay << XILINX_DMA_DMACR_DELAY_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302079 chan->config.delay = cfg->delay;
2080 }
2081
2082 /* FSync Source selection */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302083 dmacr &= ~XILINX_DMA_DMACR_FSYNCSRC_MASK;
2084 dmacr |= cfg->ext_fsync << XILINX_DMA_DMACR_FSYNCSRC_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302085
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302086 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, dmacr);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302087
2088 return 0;
2089}
2090EXPORT_SYMBOL(xilinx_vdma_channel_set_config);
2091
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302092/* -----------------------------------------------------------------------------
2093 * Probe and remove
2094 */
2095
2096/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302097 * xilinx_dma_chan_remove - Per Channel remove function
2098 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302099 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302100static void xilinx_dma_chan_remove(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302101{
2102 /* Disable all interrupts */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302103 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
2104 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302105
2106 if (chan->irq > 0)
2107 free_irq(chan->irq, chan);
2108
2109 tasklet_kill(&chan->tasklet);
2110
2111 list_del(&chan->common.device_node);
2112}
2113
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302114static int axidma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2115 struct clk **tx_clk, struct clk **rx_clk,
2116 struct clk **sg_clk, struct clk **tmp_clk)
2117{
2118 int err;
2119
2120 *tmp_clk = NULL;
2121
2122 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2123 if (IS_ERR(*axi_clk)) {
2124 err = PTR_ERR(*axi_clk);
2125 dev_err(&pdev->dev, "failed to get axi_aclk (%u)\n", err);
2126 return err;
2127 }
2128
2129 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2130 if (IS_ERR(*tx_clk))
2131 *tx_clk = NULL;
2132
2133 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2134 if (IS_ERR(*rx_clk))
2135 *rx_clk = NULL;
2136
2137 *sg_clk = devm_clk_get(&pdev->dev, "m_axi_sg_aclk");
2138 if (IS_ERR(*sg_clk))
2139 *sg_clk = NULL;
2140
2141 err = clk_prepare_enable(*axi_clk);
2142 if (err) {
2143 dev_err(&pdev->dev, "failed to enable axi_clk (%u)\n", err);
2144 return err;
2145 }
2146
2147 err = clk_prepare_enable(*tx_clk);
2148 if (err) {
2149 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2150 goto err_disable_axiclk;
2151 }
2152
2153 err = clk_prepare_enable(*rx_clk);
2154 if (err) {
2155 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2156 goto err_disable_txclk;
2157 }
2158
2159 err = clk_prepare_enable(*sg_clk);
2160 if (err) {
2161 dev_err(&pdev->dev, "failed to enable sg_clk (%u)\n", err);
2162 goto err_disable_rxclk;
2163 }
2164
2165 return 0;
2166
2167err_disable_rxclk:
2168 clk_disable_unprepare(*rx_clk);
2169err_disable_txclk:
2170 clk_disable_unprepare(*tx_clk);
2171err_disable_axiclk:
2172 clk_disable_unprepare(*axi_clk);
2173
2174 return err;
2175}
2176
2177static int axicdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2178 struct clk **dev_clk, struct clk **tmp_clk,
2179 struct clk **tmp1_clk, struct clk **tmp2_clk)
2180{
2181 int err;
2182
2183 *tmp_clk = NULL;
2184 *tmp1_clk = NULL;
2185 *tmp2_clk = NULL;
2186
2187 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2188 if (IS_ERR(*axi_clk)) {
2189 err = PTR_ERR(*axi_clk);
2190 dev_err(&pdev->dev, "failed to get axi_clk (%u)\n", err);
2191 return err;
2192 }
2193
2194 *dev_clk = devm_clk_get(&pdev->dev, "m_axi_aclk");
2195 if (IS_ERR(*dev_clk)) {
2196 err = PTR_ERR(*dev_clk);
2197 dev_err(&pdev->dev, "failed to get dev_clk (%u)\n", err);
2198 return err;
2199 }
2200
2201 err = clk_prepare_enable(*axi_clk);
2202 if (err) {
2203 dev_err(&pdev->dev, "failed to enable axi_clk (%u)\n", err);
2204 return err;
2205 }
2206
2207 err = clk_prepare_enable(*dev_clk);
2208 if (err) {
2209 dev_err(&pdev->dev, "failed to enable dev_clk (%u)\n", err);
2210 goto err_disable_axiclk;
2211 }
2212
2213 return 0;
2214
2215err_disable_axiclk:
2216 clk_disable_unprepare(*axi_clk);
2217
2218 return err;
2219}
2220
2221static int axivdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2222 struct clk **tx_clk, struct clk **txs_clk,
2223 struct clk **rx_clk, struct clk **rxs_clk)
2224{
2225 int err;
2226
2227 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2228 if (IS_ERR(*axi_clk)) {
2229 err = PTR_ERR(*axi_clk);
2230 dev_err(&pdev->dev, "failed to get axi_aclk (%u)\n", err);
2231 return err;
2232 }
2233
2234 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2235 if (IS_ERR(*tx_clk))
2236 *tx_clk = NULL;
2237
2238 *txs_clk = devm_clk_get(&pdev->dev, "m_axis_mm2s_aclk");
2239 if (IS_ERR(*txs_clk))
2240 *txs_clk = NULL;
2241
2242 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2243 if (IS_ERR(*rx_clk))
2244 *rx_clk = NULL;
2245
2246 *rxs_clk = devm_clk_get(&pdev->dev, "s_axis_s2mm_aclk");
2247 if (IS_ERR(*rxs_clk))
2248 *rxs_clk = NULL;
2249
2250 err = clk_prepare_enable(*axi_clk);
2251 if (err) {
2252 dev_err(&pdev->dev, "failed to enable axi_clk (%u)\n", err);
2253 return err;
2254 }
2255
2256 err = clk_prepare_enable(*tx_clk);
2257 if (err) {
2258 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2259 goto err_disable_axiclk;
2260 }
2261
2262 err = clk_prepare_enable(*txs_clk);
2263 if (err) {
2264 dev_err(&pdev->dev, "failed to enable txs_clk (%u)\n", err);
2265 goto err_disable_txclk;
2266 }
2267
2268 err = clk_prepare_enable(*rx_clk);
2269 if (err) {
2270 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2271 goto err_disable_txsclk;
2272 }
2273
2274 err = clk_prepare_enable(*rxs_clk);
2275 if (err) {
2276 dev_err(&pdev->dev, "failed to enable rxs_clk (%u)\n", err);
2277 goto err_disable_rxclk;
2278 }
2279
2280 return 0;
2281
2282err_disable_rxclk:
2283 clk_disable_unprepare(*rx_clk);
2284err_disable_txsclk:
2285 clk_disable_unprepare(*txs_clk);
2286err_disable_txclk:
2287 clk_disable_unprepare(*tx_clk);
2288err_disable_axiclk:
2289 clk_disable_unprepare(*axi_clk);
2290
2291 return err;
2292}
2293
2294static void xdma_disable_allclks(struct xilinx_dma_device *xdev)
2295{
2296 clk_disable_unprepare(xdev->rxs_clk);
2297 clk_disable_unprepare(xdev->rx_clk);
2298 clk_disable_unprepare(xdev->txs_clk);
2299 clk_disable_unprepare(xdev->tx_clk);
2300 clk_disable_unprepare(xdev->axi_clk);
2301}
2302
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302303/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302304 * xilinx_dma_chan_probe - Per Channel Probing
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302305 * It get channel features from the device tree entry and
2306 * initialize special channel handling routines
2307 *
2308 * @xdev: Driver specific device structure
2309 * @node: Device node
2310 *
2311 * Return: '0' on success and failure value on error
2312 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302313static int xilinx_dma_chan_probe(struct xilinx_dma_device *xdev,
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302314 struct device_node *node, int chan_id)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302315{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302316 struct xilinx_dma_chan *chan;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302317 bool has_dre = false;
2318 u32 value, width;
2319 int err;
2320
2321 /* Allocate and initialize the channel structure */
2322 chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
2323 if (!chan)
2324 return -ENOMEM;
2325
2326 chan->dev = xdev->dev;
2327 chan->xdev = xdev;
2328 chan->has_sg = xdev->has_sg;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05302329 chan->desc_pendingcount = 0x0;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302330 chan->ext_addr = xdev->ext_addr;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302331
2332 spin_lock_init(&chan->lock);
2333 INIT_LIST_HEAD(&chan->pending_list);
2334 INIT_LIST_HEAD(&chan->done_list);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05302335 INIT_LIST_HEAD(&chan->active_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302336
2337 /* Retrieve the channel properties from the device tree */
2338 has_dre = of_property_read_bool(node, "xlnx,include-dre");
2339
2340 chan->genlock = of_property_read_bool(node, "xlnx,genlock-mode");
2341
2342 err = of_property_read_u32(node, "xlnx,datawidth", &value);
2343 if (err) {
2344 dev_err(xdev->dev, "missing xlnx,datawidth property\n");
2345 return err;
2346 }
2347 width = value >> 3; /* Convert bits to bytes */
2348
2349 /* If data width is greater than 8 bytes, DRE is not in hw */
2350 if (width > 8)
2351 has_dre = false;
2352
2353 if (!has_dre)
2354 xdev->common.copy_align = fls(width - 1);
2355
2356 if (of_device_is_compatible(node, "xlnx,axi-vdma-mm2s-channel")) {
2357 chan->direction = DMA_MEM_TO_DEV;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302358 chan->id = chan_id;
2359 chan->tdest = chan_id;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302360
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302361 chan->ctrl_offset = XILINX_DMA_MM2S_CTRL_OFFSET;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302362 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302363 chan->desc_offset = XILINX_VDMA_MM2S_DESC_OFFSET;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302364
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302365 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2366 xdev->flush_on_fsync == XILINX_DMA_FLUSH_MM2S)
2367 chan->flush_on_fsync = true;
2368 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302369 } else if (of_device_is_compatible(node,
2370 "xlnx,axi-vdma-s2mm-channel")) {
2371 chan->direction = DMA_DEV_TO_MEM;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302372 chan->id = chan_id;
2373 chan->tdest = chan_id - xdev->nr_channels;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302374
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302375 chan->ctrl_offset = XILINX_DMA_S2MM_CTRL_OFFSET;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302376 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302377 chan->desc_offset = XILINX_VDMA_S2MM_DESC_OFFSET;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302378
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302379 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2380 xdev->flush_on_fsync == XILINX_DMA_FLUSH_S2MM)
2381 chan->flush_on_fsync = true;
2382 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302383 } else {
2384 dev_err(xdev->dev, "Invalid channel compatible node\n");
2385 return -EINVAL;
2386 }
2387
2388 /* Request the interrupt */
2389 chan->irq = irq_of_parse_and_map(node, 0);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302390 err = request_irq(chan->irq, xilinx_dma_irq_handler, IRQF_SHARED,
2391 "xilinx-dma-controller", chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302392 if (err) {
2393 dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq);
2394 return err;
2395 }
2396
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302397 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA)
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302398 chan->start_transfer = xilinx_dma_start_transfer;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302399 else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA)
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302400 chan->start_transfer = xilinx_cdma_start_transfer;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302401 else
2402 chan->start_transfer = xilinx_vdma_start_transfer;
2403
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302404 /* Initialize the tasklet */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302405 tasklet_init(&chan->tasklet, xilinx_dma_do_tasklet,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302406 (unsigned long)chan);
2407
2408 /*
2409 * Initialize the DMA channel and add it to the DMA engine channels
2410 * list.
2411 */
2412 chan->common.device = &xdev->common;
2413
2414 list_add_tail(&chan->common.device_node, &xdev->common.channels);
2415 xdev->chan[chan->id] = chan;
2416
2417 /* Reset the channel */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302418 err = xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302419 if (err < 0) {
2420 dev_err(xdev->dev, "Reset channel failed\n");
2421 return err;
2422 }
2423
2424 return 0;
2425}
2426
2427/**
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302428 * xilinx_dma_child_probe - Per child node probe
2429 * It get number of dma-channels per child node from
2430 * device-tree and initializes all the channels.
2431 *
2432 * @xdev: Driver specific device structure
2433 * @node: Device node
2434 *
2435 * Return: 0 always.
2436 */
2437static int xilinx_dma_child_probe(struct xilinx_dma_device *xdev,
2438 struct device_node *node) {
2439 int ret, i, nr_channels = 1;
2440
2441 ret = of_property_read_u32(node, "dma-channels", &nr_channels);
2442 if ((ret < 0) && xdev->mcdma)
2443 dev_warn(xdev->dev, "missing dma-channels property\n");
2444
2445 for (i = 0; i < nr_channels; i++)
2446 xilinx_dma_chan_probe(xdev, node, xdev->chan_id++);
2447
2448 xdev->nr_channels += nr_channels;
2449
2450 return 0;
2451}
2452
2453/**
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302454 * of_dma_xilinx_xlate - Translation function
2455 * @dma_spec: Pointer to DMA specifier as found in the device tree
2456 * @ofdma: Pointer to DMA controller data
2457 *
2458 * Return: DMA channel pointer on success and NULL on error
2459 */
2460static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
2461 struct of_dma *ofdma)
2462{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302463 struct xilinx_dma_device *xdev = ofdma->of_dma_data;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302464 int chan_id = dma_spec->args[0];
2465
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302466 if (chan_id >= xdev->nr_channels || !xdev->chan[chan_id])
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302467 return NULL;
2468
2469 return dma_get_slave_channel(&xdev->chan[chan_id]->common);
2470}
2471
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302472static const struct xilinx_dma_config axidma_config = {
2473 .dmatype = XDMA_TYPE_AXIDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302474 .clk_init = axidma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302475};
2476
2477static const struct xilinx_dma_config axicdma_config = {
2478 .dmatype = XDMA_TYPE_CDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302479 .clk_init = axicdma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302480};
2481
2482static const struct xilinx_dma_config axivdma_config = {
2483 .dmatype = XDMA_TYPE_VDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302484 .clk_init = axivdma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302485};
2486
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302487static const struct of_device_id xilinx_dma_of_ids[] = {
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302488 { .compatible = "xlnx,axi-dma-1.00.a", .data = &axidma_config },
2489 { .compatible = "xlnx,axi-cdma-1.00.a", .data = &axicdma_config },
2490 { .compatible = "xlnx,axi-vdma-1.00.a", .data = &axivdma_config },
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302491 {}
2492};
2493MODULE_DEVICE_TABLE(of, xilinx_dma_of_ids);
2494
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302495/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302496 * xilinx_dma_probe - Driver probe function
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302497 * @pdev: Pointer to the platform_device structure
2498 *
2499 * Return: '0' on success and failure value on error
2500 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302501static int xilinx_dma_probe(struct platform_device *pdev)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302502{
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302503 int (*clk_init)(struct platform_device *, struct clk **, struct clk **,
2504 struct clk **, struct clk **, struct clk **)
2505 = axivdma_clk_init;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302506 struct device_node *node = pdev->dev.of_node;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302507 struct xilinx_dma_device *xdev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302508 struct device_node *child, *np = pdev->dev.of_node;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302509 struct resource *io;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302510 u32 num_frames, addr_width;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302511 int i, err;
2512
2513 /* Allocate and initialize the DMA engine structure */
2514 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
2515 if (!xdev)
2516 return -ENOMEM;
2517
2518 xdev->dev = &pdev->dev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302519 if (np) {
2520 const struct of_device_id *match;
2521
2522 match = of_match_node(xilinx_dma_of_ids, np);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302523 if (match && match->data) {
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302524 xdev->dma_config = match->data;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302525 clk_init = xdev->dma_config->clk_init;
2526 }
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302527 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302528
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302529 err = clk_init(pdev, &xdev->axi_clk, &xdev->tx_clk, &xdev->txs_clk,
2530 &xdev->rx_clk, &xdev->rxs_clk);
2531 if (err)
2532 return err;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302533
2534 /* Request and map I/O memory */
2535 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2536 xdev->regs = devm_ioremap_resource(&pdev->dev, io);
2537 if (IS_ERR(xdev->regs))
2538 return PTR_ERR(xdev->regs);
2539
2540 /* Retrieve the DMA engine properties from the device tree */
2541 xdev->has_sg = of_property_read_bool(node, "xlnx,include-sg");
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302542 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA)
2543 xdev->mcdma = of_property_read_bool(node, "xlnx,mcdma");
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302544
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302545 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302546 err = of_property_read_u32(node, "xlnx,num-fstores",
2547 &num_frames);
2548 if (err < 0) {
2549 dev_err(xdev->dev,
2550 "missing xlnx,num-fstores property\n");
2551 return err;
2552 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302553
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302554 err = of_property_read_u32(node, "xlnx,flush-fsync",
2555 &xdev->flush_on_fsync);
2556 if (err < 0)
2557 dev_warn(xdev->dev,
2558 "missing xlnx,flush-fsync property\n");
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302559 }
2560
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302561 err = of_property_read_u32(node, "xlnx,addrwidth", &addr_width);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302562 if (err < 0)
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302563 dev_warn(xdev->dev, "missing xlnx,addrwidth property\n");
2564
2565 if (addr_width > 32)
2566 xdev->ext_addr = true;
2567 else
2568 xdev->ext_addr = false;
2569
2570 /* Set the dma mask bits */
2571 dma_set_mask(xdev->dev, DMA_BIT_MASK(addr_width));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302572
2573 /* Initialize the DMA engine */
2574 xdev->common.dev = &pdev->dev;
2575
2576 INIT_LIST_HEAD(&xdev->common.channels);
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302577 if (!(xdev->dma_config->dmatype == XDMA_TYPE_CDMA)) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302578 dma_cap_set(DMA_SLAVE, xdev->common.cap_mask);
2579 dma_cap_set(DMA_PRIVATE, xdev->common.cap_mask);
2580 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302581
2582 xdev->common.device_alloc_chan_resources =
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302583 xilinx_dma_alloc_chan_resources;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302584 xdev->common.device_free_chan_resources =
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302585 xilinx_dma_free_chan_resources;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302586 xdev->common.device_terminate_all = xilinx_dma_terminate_all;
2587 xdev->common.device_tx_status = xilinx_dma_tx_status;
2588 xdev->common.device_issue_pending = xilinx_dma_issue_pending;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302589 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302590 dma_cap_set(DMA_CYCLIC, xdev->common.cap_mask);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302591 xdev->common.device_prep_slave_sg = xilinx_dma_prep_slave_sg;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302592 xdev->common.device_prep_dma_cyclic =
2593 xilinx_dma_prep_dma_cyclic;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302594 xdev->common.device_prep_interleaved_dma =
2595 xilinx_dma_prep_interleaved;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302596 /* Residue calculation is supported by only AXI DMA */
2597 xdev->common.residue_granularity =
2598 DMA_RESIDUE_GRANULARITY_SEGMENT;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302599 } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302600 dma_cap_set(DMA_MEMCPY, xdev->common.cap_mask);
2601 xdev->common.device_prep_dma_memcpy = xilinx_cdma_prep_memcpy;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302602 } else {
2603 xdev->common.device_prep_interleaved_dma =
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302604 xilinx_vdma_dma_prep_interleaved;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302605 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302606
2607 platform_set_drvdata(pdev, xdev);
2608
2609 /* Initialize the channels */
2610 for_each_child_of_node(node, child) {
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302611 err = xilinx_dma_child_probe(xdev, child);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302612 if (err < 0)
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302613 goto disable_clks;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302614 }
2615
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302616 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302617 for (i = 0; i < xdev->nr_channels; i++)
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302618 if (xdev->chan[i])
2619 xdev->chan[i]->num_frms = num_frames;
2620 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302621
2622 /* Register the DMA engine with the core */
2623 dma_async_device_register(&xdev->common);
2624
2625 err = of_dma_controller_register(node, of_dma_xilinx_xlate,
2626 xdev);
2627 if (err < 0) {
2628 dev_err(&pdev->dev, "Unable to register DMA to DT\n");
2629 dma_async_device_unregister(&xdev->common);
2630 goto error;
2631 }
2632
2633 dev_info(&pdev->dev, "Xilinx AXI VDMA Engine Driver Probed!!\n");
2634
2635 return 0;
2636
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302637disable_clks:
2638 xdma_disable_allclks(xdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302639error:
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302640 for (i = 0; i < xdev->nr_channels; i++)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302641 if (xdev->chan[i])
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302642 xilinx_dma_chan_remove(xdev->chan[i]);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302643
2644 return err;
2645}
2646
2647/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302648 * xilinx_dma_remove - Driver remove function
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302649 * @pdev: Pointer to the platform_device structure
2650 *
2651 * Return: Always '0'
2652 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302653static int xilinx_dma_remove(struct platform_device *pdev)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302654{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302655 struct xilinx_dma_device *xdev = platform_get_drvdata(pdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302656 int i;
2657
2658 of_dma_controller_free(pdev->dev.of_node);
2659
2660 dma_async_device_unregister(&xdev->common);
2661
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302662 for (i = 0; i < xdev->nr_channels; i++)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302663 if (xdev->chan[i])
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302664 xilinx_dma_chan_remove(xdev->chan[i]);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302665
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302666 xdma_disable_allclks(xdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302667
2668 return 0;
2669}
2670
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302671static struct platform_driver xilinx_vdma_driver = {
2672 .driver = {
2673 .name = "xilinx-vdma",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302674 .of_match_table = xilinx_dma_of_ids,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302675 },
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302676 .probe = xilinx_dma_probe,
2677 .remove = xilinx_dma_remove,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302678};
2679
2680module_platform_driver(xilinx_vdma_driver);
2681
2682MODULE_AUTHOR("Xilinx, Inc.");
2683MODULE_DESCRIPTION("Xilinx VDMA driver");
2684MODULE_LICENSE("GPL v2");