blob: b09a8efcfb2d134bb2560350a0c4ed1032e4b25c [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
Kedareswara rao Appanafe0503e2017-12-07 10:51:03 +0530102#define XILINX_DMA_PARK_PTR_WR_REF_MASK GENMASK(12, 8)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530103#define XILINX_DMA_PARK_PTR_RD_REF_SHIFT 0
Kedareswara rao Appanafe0503e2017-12-07 10:51:03 +0530104#define XILINX_DMA_PARK_PTR_RD_REF_MASK GENMASK(4, 0)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530105#define XILINX_DMA_REG_VDMA_VERSION 0x002c
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530106
107/* Register Direct Mode Registers */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530108#define XILINX_DMA_REG_VSIZE 0x0000
109#define XILINX_DMA_REG_HSIZE 0x0004
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530110
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530111#define XILINX_DMA_REG_FRMDLY_STRIDE 0x0008
112#define XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT 24
113#define XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT 0
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530114
115#define XILINX_VDMA_REG_START_ADDRESS(n) (0x000c + 4 * (n))
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530116#define XILINX_VDMA_REG_START_ADDRESS_64(n) (0x000c + 8 * (n))
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530117
118/* HW specific definitions */
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530119#define XILINX_DMA_MAX_CHANS_PER_DEVICE 0x20
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530120
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530121#define XILINX_DMA_DMAXR_ALL_IRQ_MASK \
122 (XILINX_DMA_DMASR_FRM_CNT_IRQ | \
123 XILINX_DMA_DMASR_DLY_CNT_IRQ | \
124 XILINX_DMA_DMASR_ERR_IRQ)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530125
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530126#define XILINX_DMA_DMASR_ALL_ERR_MASK \
127 (XILINX_DMA_DMASR_EOL_LATE_ERR | \
128 XILINX_DMA_DMASR_SOF_LATE_ERR | \
129 XILINX_DMA_DMASR_SG_DEC_ERR | \
130 XILINX_DMA_DMASR_SG_SLV_ERR | \
131 XILINX_DMA_DMASR_EOF_EARLY_ERR | \
132 XILINX_DMA_DMASR_SOF_EARLY_ERR | \
133 XILINX_DMA_DMASR_DMA_DEC_ERR | \
134 XILINX_DMA_DMASR_DMA_SLAVE_ERR | \
135 XILINX_DMA_DMASR_DMA_INT_ERR)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530136
137/*
138 * Recoverable errors are DMA Internal error, SOF Early, EOF Early
139 * and SOF Late. They are only recoverable when C_FLUSH_ON_FSYNC
140 * is enabled in the h/w system.
141 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530142#define XILINX_DMA_DMASR_ERR_RECOVER_MASK \
143 (XILINX_DMA_DMASR_SOF_LATE_ERR | \
144 XILINX_DMA_DMASR_EOF_EARLY_ERR | \
145 XILINX_DMA_DMASR_SOF_EARLY_ERR | \
146 XILINX_DMA_DMASR_DMA_INT_ERR)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530147
148/* Axi VDMA Flush on Fsync bits */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530149#define XILINX_DMA_FLUSH_S2MM 3
150#define XILINX_DMA_FLUSH_MM2S 2
151#define XILINX_DMA_FLUSH_BOTH 1
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530152
153/* Delay loop counter to prevent hardware failure */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530154#define XILINX_DMA_LOOP_COUNT 1000000
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530155
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530156/* AXI DMA Specific Registers/Offsets */
157#define XILINX_DMA_REG_SRCDSTADDR 0x18
158#define XILINX_DMA_REG_BTT 0x28
159
160/* AXI DMA Specific Masks/Bit fields */
161#define XILINX_DMA_MAX_TRANS_LEN GENMASK(22, 0)
162#define XILINX_DMA_CR_COALESCE_MAX GENMASK(23, 16)
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530163#define XILINX_DMA_CR_CYCLIC_BD_EN_MASK BIT(4)
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530164#define XILINX_DMA_CR_COALESCE_SHIFT 16
165#define XILINX_DMA_BD_SOP BIT(27)
166#define XILINX_DMA_BD_EOP BIT(26)
167#define XILINX_DMA_COALESCE_MAX 255
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530168#define XILINX_DMA_NUM_DESCS 255
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530169#define XILINX_DMA_NUM_APP_WORDS 5
170
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530171/* Multi-Channel DMA Descriptor offsets*/
172#define XILINX_DMA_MCRX_CDESC(x) (0x40 + (x-1) * 0x20)
173#define XILINX_DMA_MCRX_TDESC(x) (0x48 + (x-1) * 0x20)
174
175/* Multi-Channel DMA Masks/Shifts */
176#define XILINX_DMA_BD_HSIZE_MASK GENMASK(15, 0)
177#define XILINX_DMA_BD_STRIDE_MASK GENMASK(15, 0)
178#define XILINX_DMA_BD_VSIZE_MASK GENMASK(31, 19)
179#define XILINX_DMA_BD_TDEST_MASK GENMASK(4, 0)
180#define XILINX_DMA_BD_STRIDE_SHIFT 0
181#define XILINX_DMA_BD_VSIZE_SHIFT 19
182
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530183/* AXI CDMA Specific Registers/Offsets */
184#define XILINX_CDMA_REG_SRCADDR 0x18
185#define XILINX_CDMA_REG_DSTADDR 0x20
186
187/* AXI CDMA Specific Masks */
188#define XILINX_CDMA_CR_SGMODE BIT(3)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530189
190/**
191 * struct xilinx_vdma_desc_hw - Hardware Descriptor
192 * @next_desc: Next Descriptor Pointer @0x00
193 * @pad1: Reserved @0x04
194 * @buf_addr: Buffer address @0x08
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530195 * @buf_addr_msb: MSB of Buffer address @0x0C
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530196 * @vsize: Vertical Size @0x10
197 * @hsize: Horizontal Size @0x14
198 * @stride: Number of bytes between the first
199 * pixels of each horizontal line @0x18
200 */
201struct xilinx_vdma_desc_hw {
202 u32 next_desc;
203 u32 pad1;
204 u32 buf_addr;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530205 u32 buf_addr_msb;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530206 u32 vsize;
207 u32 hsize;
208 u32 stride;
209} __aligned(64);
210
211/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530212 * struct xilinx_axidma_desc_hw - Hardware Descriptor for AXI DMA
213 * @next_desc: Next Descriptor Pointer @0x00
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530214 * @next_desc_msb: MSB of Next Descriptor Pointer @0x04
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530215 * @buf_addr: Buffer address @0x08
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530216 * @buf_addr_msb: MSB of Buffer address @0x0C
Kedareswara rao Appanae50a0ad2017-12-07 10:51:05 +0530217 * @mcdma_control: Control field for mcdma @0x10
218 * @vsize_stride: Vsize and Stride field for mcdma @0x14
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530219 * @control: Control field @0x18
220 * @status: Status field @0x1C
221 * @app: APP Fields @0x20 - 0x30
222 */
223struct xilinx_axidma_desc_hw {
224 u32 next_desc;
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530225 u32 next_desc_msb;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530226 u32 buf_addr;
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530227 u32 buf_addr_msb;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530228 u32 mcdma_control;
229 u32 vsize_stride;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530230 u32 control;
231 u32 status;
232 u32 app[XILINX_DMA_NUM_APP_WORDS];
233} __aligned(64);
234
235/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530236 * struct xilinx_cdma_desc_hw - Hardware Descriptor
237 * @next_desc: Next Descriptor Pointer @0x00
Kedareswara rao Appanae50a0ad2017-12-07 10:51:05 +0530238 * @next_desc_msb: Next Descriptor Pointer MSB @0x04
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530239 * @src_addr: Source address @0x08
Kedareswara rao Appanae50a0ad2017-12-07 10:51:05 +0530240 * @src_addr_msb: Source address MSB @0x0C
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530241 * @dest_addr: Destination address @0x10
Kedareswara rao Appanae50a0ad2017-12-07 10:51:05 +0530242 * @dest_addr_msb: Destination address MSB @0x14
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530243 * @control: Control field @0x18
244 * @status: Status field @0x1C
245 */
246struct xilinx_cdma_desc_hw {
247 u32 next_desc;
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530248 u32 next_desc_msb;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530249 u32 src_addr;
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530250 u32 src_addr_msb;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530251 u32 dest_addr;
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530252 u32 dest_addr_msb;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530253 u32 control;
254 u32 status;
255} __aligned(64);
256
257/**
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530258 * struct xilinx_vdma_tx_segment - Descriptor segment
259 * @hw: Hardware descriptor
260 * @node: Node in the descriptor segments list
261 * @phys: Physical address of segment
262 */
263struct xilinx_vdma_tx_segment {
264 struct xilinx_vdma_desc_hw hw;
265 struct list_head node;
266 dma_addr_t phys;
267} __aligned(64);
268
269/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530270 * struct xilinx_axidma_tx_segment - Descriptor segment
271 * @hw: Hardware descriptor
272 * @node: Node in the descriptor segments list
273 * @phys: Physical address of segment
274 */
275struct xilinx_axidma_tx_segment {
276 struct xilinx_axidma_desc_hw hw;
277 struct list_head node;
278 dma_addr_t phys;
279} __aligned(64);
280
281/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530282 * struct xilinx_cdma_tx_segment - Descriptor segment
283 * @hw: Hardware descriptor
284 * @node: Node in the descriptor segments list
285 * @phys: Physical address of segment
286 */
287struct xilinx_cdma_tx_segment {
288 struct xilinx_cdma_desc_hw hw;
289 struct list_head node;
290 dma_addr_t phys;
291} __aligned(64);
292
293/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530294 * struct xilinx_dma_tx_descriptor - Per Transaction structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530295 * @async_tx: Async transaction descriptor
296 * @segments: TX segments list
297 * @node: Node in the channel descriptors list
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530298 * @cyclic: Check for cyclic transfers.
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530299 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530300struct xilinx_dma_tx_descriptor {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530301 struct dma_async_tx_descriptor async_tx;
302 struct list_head segments;
303 struct list_head node;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530304 bool cyclic;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530305};
306
307/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530308 * struct xilinx_dma_chan - Driver specific DMA channel structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530309 * @xdev: Driver specific device structure
310 * @ctrl_offset: Control registers offset
311 * @desc_offset: TX descriptor registers offset
312 * @lock: Descriptor operation lock
313 * @pending_list: Descriptors waiting
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530314 * @active_list: Descriptors ready to submit
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530315 * @done_list: Complete descriptors
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530316 * @free_seg_list: Free descriptors
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530317 * @common: DMA common channel
318 * @desc_pool: Descriptors pool
319 * @dev: The dma device
320 * @irq: Channel IRQ
321 * @id: Channel ID
322 * @direction: Transfer direction
323 * @num_frms: Number of frames
324 * @has_sg: Support scatter transfers
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530325 * @cyclic: Check for cyclic transfers.
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530326 * @genlock: Support genlock mode
327 * @err: Channel has errors
Kedareswara rao Appana21e02a32017-12-07 10:51:02 +0530328 * @idle: Check for channel idle
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530329 * @tasklet: Cleanup work after irq
330 * @config: Device configuration info
331 * @flush_on_fsync: Flush on Frame sync
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530332 * @desc_pendingcount: Descriptor pending count
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530333 * @ext_addr: Indicates 64 bit addressing is supported by dma channel
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +0530334 * @desc_submitcount: Descriptor h/w submitted count
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530335 * @residue: Residue for AXI DMA
336 * @seg_v: Statically allocated segments base
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530337 * @seg_p: Physical allocated segments base
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530338 * @cyclic_seg_v: Statically allocated segment base for cyclic transfers
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530339 * @cyclic_seg_p: Physical allocated segments base for cyclic dma
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530340 * @start_transfer: Differentiate b/w DMA IP's transfer
Akinobu Mita676f9c22017-03-14 00:59:11 +0900341 * @stop_transfer: Differentiate b/w DMA IP's quiesce
Kedareswara rao Appanae50a0ad2017-12-07 10:51:05 +0530342 * @tdest: TDEST value for mcdma
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530343 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530344struct xilinx_dma_chan {
345 struct xilinx_dma_device *xdev;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530346 u32 ctrl_offset;
347 u32 desc_offset;
348 spinlock_t lock;
349 struct list_head pending_list;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530350 struct list_head active_list;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530351 struct list_head done_list;
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530352 struct list_head free_seg_list;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530353 struct dma_chan common;
354 struct dma_pool *desc_pool;
355 struct device *dev;
356 int irq;
357 int id;
358 enum dma_transfer_direction direction;
359 int num_frms;
360 bool has_sg;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530361 bool cyclic;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530362 bool genlock;
363 bool err;
Kedareswara rao Appana21e02a32017-12-07 10:51:02 +0530364 bool idle;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530365 struct tasklet_struct tasklet;
366 struct xilinx_vdma_config config;
367 bool flush_on_fsync;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530368 u32 desc_pendingcount;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530369 bool ext_addr;
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +0530370 u32 desc_submitcount;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530371 u32 residue;
372 struct xilinx_axidma_tx_segment *seg_v;
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530373 dma_addr_t seg_p;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530374 struct xilinx_axidma_tx_segment *cyclic_seg_v;
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530375 dma_addr_t cyclic_seg_p;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530376 void (*start_transfer)(struct xilinx_dma_chan *chan);
Akinobu Mita676f9c22017-03-14 00:59:11 +0900377 int (*stop_transfer)(struct xilinx_dma_chan *chan);
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530378 u16 tdest;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530379};
380
Lars-Peter Clausenf3ae7d92017-09-05 16:43:49 +0200381/**
Kedareswara rao Appanae50a0ad2017-12-07 10:51:05 +0530382 * enum xdma_ip_type - DMA IP type.
Lars-Peter Clausenf3ae7d92017-09-05 16:43:49 +0200383 *
Kedareswara rao Appanae50a0ad2017-12-07 10:51:05 +0530384 * @XDMA_TYPE_AXIDMA: Axi dma ip.
385 * @XDMA_TYPE_CDMA: Axi cdma ip.
386 * @XDMA_TYPE_VDMA: Axi vdma ip.
Lars-Peter Clausenf3ae7d92017-09-05 16:43:49 +0200387 *
388 */
389enum xdma_ip_type {
390 XDMA_TYPE_AXIDMA = 0,
391 XDMA_TYPE_CDMA,
392 XDMA_TYPE_VDMA,
393};
394
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530395struct xilinx_dma_config {
396 enum xdma_ip_type dmatype;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530397 int (*clk_init)(struct platform_device *pdev, struct clk **axi_clk,
398 struct clk **tx_clk, struct clk **txs_clk,
399 struct clk **rx_clk, struct clk **rxs_clk);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530400};
401
402/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530403 * struct xilinx_dma_device - DMA device structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530404 * @regs: I/O mapped base address
405 * @dev: Device Structure
406 * @common: DMA device structure
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530407 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530408 * @has_sg: Specifies whether Scatter-Gather is present or not
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530409 * @mcdma: Specifies whether Multi-Channel is present or not
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530410 * @flush_on_fsync: Flush on frame sync
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530411 * @ext_addr: Indicates 64 bit addressing is supported by dma device
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530412 * @pdev: Platform device structure pointer
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530413 * @dma_config: DMA config structure
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530414 * @axi_clk: DMA Axi4-lite interace clock
415 * @tx_clk: DMA mm2s clock
416 * @txs_clk: DMA mm2s stream clock
417 * @rx_clk: DMA s2mm clock
418 * @rxs_clk: DMA s2mm stream clock
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530419 * @nr_channels: Number of channels DMA device supports
420 * @chan_id: DMA channel identifier
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530421 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530422struct xilinx_dma_device {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530423 void __iomem *regs;
424 struct device *dev;
425 struct dma_device common;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530426 struct xilinx_dma_chan *chan[XILINX_DMA_MAX_CHANS_PER_DEVICE];
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530427 bool has_sg;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530428 bool mcdma;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530429 u32 flush_on_fsync;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530430 bool ext_addr;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530431 struct platform_device *pdev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530432 const struct xilinx_dma_config *dma_config;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530433 struct clk *axi_clk;
434 struct clk *tx_clk;
435 struct clk *txs_clk;
436 struct clk *rx_clk;
437 struct clk *rxs_clk;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530438 u32 nr_channels;
439 u32 chan_id;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530440};
441
442/* Macros */
443#define to_xilinx_chan(chan) \
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530444 container_of(chan, struct xilinx_dma_chan, common)
445#define to_dma_tx_descriptor(tx) \
446 container_of(tx, struct xilinx_dma_tx_descriptor, async_tx)
447#define xilinx_dma_poll_timeout(chan, reg, val, cond, delay_us, timeout_us) \
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530448 readl_poll_timeout(chan->xdev->regs + chan->ctrl_offset + reg, val, \
449 cond, delay_us, timeout_us)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530450
451/* IO accessors */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530452static inline u32 dma_read(struct xilinx_dma_chan *chan, u32 reg)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530453{
454 return ioread32(chan->xdev->regs + reg);
455}
456
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530457static inline void dma_write(struct xilinx_dma_chan *chan, u32 reg, u32 value)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530458{
459 iowrite32(value, chan->xdev->regs + reg);
460}
461
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530462static inline void vdma_desc_write(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530463 u32 value)
464{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530465 dma_write(chan, chan->desc_offset + reg, value);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530466}
467
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530468static inline u32 dma_ctrl_read(struct xilinx_dma_chan *chan, u32 reg)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530469{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530470 return dma_read(chan, chan->ctrl_offset + reg);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530471}
472
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530473static inline void dma_ctrl_write(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530474 u32 value)
475{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530476 dma_write(chan, chan->ctrl_offset + reg, value);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530477}
478
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530479static inline void dma_ctrl_clr(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530480 u32 clr)
481{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530482 dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) & ~clr);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530483}
484
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530485static inline void dma_ctrl_set(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530486 u32 set)
487{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530488 dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) | set);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530489}
490
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530491/**
492 * vdma_desc_write_64 - 64-bit descriptor write
493 * @chan: Driver specific VDMA channel
494 * @reg: Register to write
495 * @value_lsb: lower address of the descriptor.
496 * @value_msb: upper address of the descriptor.
497 *
498 * Since vdma driver is trying to write to a register offset which is not a
499 * multiple of 64 bits(ex : 0x5c), we are writing as two separate 32 bits
500 * instead of a single 64 bit register write.
501 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530502static inline void vdma_desc_write_64(struct xilinx_dma_chan *chan, u32 reg,
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530503 u32 value_lsb, u32 value_msb)
504{
505 /* Write the lsb 32 bits*/
506 writel(value_lsb, chan->xdev->regs + chan->desc_offset + reg);
507
508 /* Write the msb 32 bits */
509 writel(value_msb, chan->xdev->regs + chan->desc_offset + reg + 4);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530510}
511
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530512static inline void dma_writeq(struct xilinx_dma_chan *chan, u32 reg, u64 value)
513{
514 lo_hi_writeq(value, chan->xdev->regs + chan->ctrl_offset + reg);
515}
516
517static inline void xilinx_write(struct xilinx_dma_chan *chan, u32 reg,
518 dma_addr_t addr)
519{
520 if (chan->ext_addr)
521 dma_writeq(chan, reg, addr);
522 else
523 dma_ctrl_write(chan, reg, addr);
524}
525
526static inline void xilinx_axidma_buf(struct xilinx_dma_chan *chan,
527 struct xilinx_axidma_desc_hw *hw,
528 dma_addr_t buf_addr, size_t sg_used,
529 size_t period_len)
530{
531 if (chan->ext_addr) {
532 hw->buf_addr = lower_32_bits(buf_addr + sg_used + period_len);
533 hw->buf_addr_msb = upper_32_bits(buf_addr + sg_used +
534 period_len);
535 } else {
536 hw->buf_addr = buf_addr + sg_used + period_len;
537 }
538}
539
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530540/* -----------------------------------------------------------------------------
541 * Descriptors and segments alloc and free
542 */
543
544/**
545 * xilinx_vdma_alloc_tx_segment - Allocate transaction segment
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530546 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530547 *
548 * Return: The allocated segment on success and NULL on failure.
549 */
550static struct xilinx_vdma_tx_segment *
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530551xilinx_vdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530552{
553 struct xilinx_vdma_tx_segment *segment;
554 dma_addr_t phys;
555
Julia Lawall2ba4f8a2016-04-29 22:09:09 +0200556 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530557 if (!segment)
558 return NULL;
559
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530560 segment->phys = phys;
561
562 return segment;
563}
564
565/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530566 * xilinx_cdma_alloc_tx_segment - Allocate transaction segment
567 * @chan: Driver specific DMA channel
568 *
569 * Return: The allocated segment on success and NULL on failure.
570 */
571static struct xilinx_cdma_tx_segment *
572xilinx_cdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
573{
574 struct xilinx_cdma_tx_segment *segment;
575 dma_addr_t phys;
576
Kedareswara rao Appana62147862016-05-18 13:17:31 +0530577 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530578 if (!segment)
579 return NULL;
580
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530581 segment->phys = phys;
582
583 return segment;
584}
585
586/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530587 * xilinx_axidma_alloc_tx_segment - Allocate transaction segment
588 * @chan: Driver specific DMA channel
589 *
590 * Return: The allocated segment on success and NULL on failure.
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530591 */
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530592static struct xilinx_axidma_tx_segment *
593xilinx_axidma_alloc_tx_segment(struct xilinx_dma_chan *chan)
594{
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530595 struct xilinx_axidma_tx_segment *segment = NULL;
596 unsigned long flags;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530597
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530598 spin_lock_irqsave(&chan->lock, flags);
599 if (!list_empty(&chan->free_seg_list)) {
600 segment = list_first_entry(&chan->free_seg_list,
601 struct xilinx_axidma_tx_segment,
602 node);
603 list_del(&segment->node);
604 }
605 spin_unlock_irqrestore(&chan->lock, flags);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530606
607 return segment;
608}
609
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530610static void xilinx_dma_clean_hw_desc(struct xilinx_axidma_desc_hw *hw)
611{
612 u32 next_desc = hw->next_desc;
613 u32 next_desc_msb = hw->next_desc_msb;
614
615 memset(hw, 0, sizeof(struct xilinx_axidma_desc_hw));
616
617 hw->next_desc = next_desc;
618 hw->next_desc_msb = next_desc_msb;
619}
620
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530621/**
622 * xilinx_dma_free_tx_segment - Free transaction segment
623 * @chan: Driver specific DMA channel
624 * @segment: DMA transaction segment
625 */
626static void xilinx_dma_free_tx_segment(struct xilinx_dma_chan *chan,
627 struct xilinx_axidma_tx_segment *segment)
628{
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530629 xilinx_dma_clean_hw_desc(&segment->hw);
630
631 list_add_tail(&segment->node, &chan->free_seg_list);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530632}
633
634/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530635 * xilinx_cdma_free_tx_segment - Free transaction segment
636 * @chan: Driver specific DMA channel
637 * @segment: DMA transaction segment
638 */
639static void xilinx_cdma_free_tx_segment(struct xilinx_dma_chan *chan,
640 struct xilinx_cdma_tx_segment *segment)
641{
642 dma_pool_free(chan->desc_pool, segment, segment->phys);
643}
644
645/**
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530646 * xilinx_vdma_free_tx_segment - Free transaction segment
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530647 * @chan: Driver specific DMA channel
648 * @segment: DMA transaction segment
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530649 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530650static void xilinx_vdma_free_tx_segment(struct xilinx_dma_chan *chan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530651 struct xilinx_vdma_tx_segment *segment)
652{
653 dma_pool_free(chan->desc_pool, segment, segment->phys);
654}
655
656/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530657 * xilinx_dma_tx_descriptor - Allocate transaction descriptor
658 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530659 *
660 * Return: The allocated descriptor on success and NULL on failure.
661 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530662static struct xilinx_dma_tx_descriptor *
663xilinx_dma_alloc_tx_descriptor(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530664{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530665 struct xilinx_dma_tx_descriptor *desc;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530666
667 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
668 if (!desc)
669 return NULL;
670
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530671 INIT_LIST_HEAD(&desc->segments);
672
673 return desc;
674}
675
676/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530677 * xilinx_dma_free_tx_descriptor - Free transaction descriptor
678 * @chan: Driver specific DMA channel
679 * @desc: DMA transaction descriptor
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530680 */
681static void
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530682xilinx_dma_free_tx_descriptor(struct xilinx_dma_chan *chan,
683 struct xilinx_dma_tx_descriptor *desc)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530684{
685 struct xilinx_vdma_tx_segment *segment, *next;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530686 struct xilinx_cdma_tx_segment *cdma_segment, *cdma_next;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530687 struct xilinx_axidma_tx_segment *axidma_segment, *axidma_next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530688
689 if (!desc)
690 return;
691
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530692 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530693 list_for_each_entry_safe(segment, next, &desc->segments, node) {
694 list_del(&segment->node);
695 xilinx_vdma_free_tx_segment(chan, segment);
696 }
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530697 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530698 list_for_each_entry_safe(cdma_segment, cdma_next,
699 &desc->segments, node) {
700 list_del(&cdma_segment->node);
701 xilinx_cdma_free_tx_segment(chan, cdma_segment);
702 }
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530703 } else {
704 list_for_each_entry_safe(axidma_segment, axidma_next,
705 &desc->segments, node) {
706 list_del(&axidma_segment->node);
707 xilinx_dma_free_tx_segment(chan, axidma_segment);
708 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530709 }
710
711 kfree(desc);
712}
713
714/* Required functions */
715
716/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530717 * xilinx_dma_free_desc_list - Free descriptors list
718 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530719 * @list: List to parse and delete the descriptor
720 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530721static void xilinx_dma_free_desc_list(struct xilinx_dma_chan *chan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530722 struct list_head *list)
723{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530724 struct xilinx_dma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530725
726 list_for_each_entry_safe(desc, next, list, node) {
727 list_del(&desc->node);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530728 xilinx_dma_free_tx_descriptor(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530729 }
730}
731
732/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530733 * xilinx_dma_free_descriptors - Free channel descriptors
734 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530735 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530736static void xilinx_dma_free_descriptors(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530737{
738 unsigned long flags;
739
740 spin_lock_irqsave(&chan->lock, flags);
741
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530742 xilinx_dma_free_desc_list(chan, &chan->pending_list);
743 xilinx_dma_free_desc_list(chan, &chan->done_list);
744 xilinx_dma_free_desc_list(chan, &chan->active_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530745
746 spin_unlock_irqrestore(&chan->lock, flags);
747}
748
749/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530750 * xilinx_dma_free_chan_resources - Free channel resources
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530751 * @dchan: DMA channel
752 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530753static void xilinx_dma_free_chan_resources(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530754{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530755 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530756 unsigned long flags;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530757
758 dev_dbg(chan->dev, "Free all channel resources.\n");
759
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530760 xilinx_dma_free_descriptors(chan);
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530761
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530762 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530763 spin_lock_irqsave(&chan->lock, flags);
764 INIT_LIST_HEAD(&chan->free_seg_list);
765 spin_unlock_irqrestore(&chan->lock, flags);
766
767 /* Free Memory that is allocated for cyclic DMA Mode */
768 dma_free_coherent(chan->dev, sizeof(*chan->cyclic_seg_v),
769 chan->cyclic_seg_v, chan->cyclic_seg_p);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530770 }
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530771
772 if (chan->xdev->dma_config->dmatype != XDMA_TYPE_AXIDMA) {
773 dma_pool_destroy(chan->desc_pool);
774 chan->desc_pool = NULL;
775 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530776}
777
778/**
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530779 * xilinx_dma_chan_handle_cyclic - Cyclic dma callback
780 * @chan: Driver specific dma channel
781 * @desc: dma transaction descriptor
782 * @flags: flags for spin lock
783 */
784static void xilinx_dma_chan_handle_cyclic(struct xilinx_dma_chan *chan,
785 struct xilinx_dma_tx_descriptor *desc,
786 unsigned long *flags)
787{
788 dma_async_tx_callback callback;
789 void *callback_param;
790
791 callback = desc->async_tx.callback;
792 callback_param = desc->async_tx.callback_param;
793 if (callback) {
794 spin_unlock_irqrestore(&chan->lock, *flags);
795 callback(callback_param);
796 spin_lock_irqsave(&chan->lock, *flags);
797 }
798}
799
800/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530801 * xilinx_dma_chan_desc_cleanup - Clean channel descriptors
802 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530803 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530804static void xilinx_dma_chan_desc_cleanup(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530805{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530806 struct xilinx_dma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530807 unsigned long flags;
808
809 spin_lock_irqsave(&chan->lock, flags);
810
811 list_for_each_entry_safe(desc, next, &chan->done_list, node) {
Vinod Koul369dbad2016-08-04 18:06:13 +0530812 struct dmaengine_desc_callback cb;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530813
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530814 if (desc->cyclic) {
815 xilinx_dma_chan_handle_cyclic(chan, desc, &flags);
816 break;
817 }
818
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530819 /* Remove from the list of running transactions */
820 list_del(&desc->node);
821
822 /* Run the link descriptor callback function */
Vinod Koul369dbad2016-08-04 18:06:13 +0530823 dmaengine_desc_get_callback(&desc->async_tx, &cb);
824 if (dmaengine_desc_callback_valid(&cb)) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530825 spin_unlock_irqrestore(&chan->lock, flags);
Vinod Koul369dbad2016-08-04 18:06:13 +0530826 dmaengine_desc_callback_invoke(&cb, NULL);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530827 spin_lock_irqsave(&chan->lock, flags);
828 }
829
830 /* Run any dependencies, then free the descriptor */
831 dma_run_dependencies(&desc->async_tx);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530832 xilinx_dma_free_tx_descriptor(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530833 }
834
835 spin_unlock_irqrestore(&chan->lock, flags);
836}
837
838/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530839 * xilinx_dma_do_tasklet - Schedule completion tasklet
840 * @data: Pointer to the Xilinx DMA channel structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530841 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530842static void xilinx_dma_do_tasklet(unsigned long data)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530843{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530844 struct xilinx_dma_chan *chan = (struct xilinx_dma_chan *)data;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530845
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530846 xilinx_dma_chan_desc_cleanup(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530847}
848
849/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530850 * xilinx_dma_alloc_chan_resources - Allocate channel resources
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530851 * @dchan: DMA channel
852 *
853 * Return: '0' on success and failure value on error
854 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530855static int xilinx_dma_alloc_chan_resources(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530856{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530857 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530858 int i;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530859
860 /* Has this channel already been allocated? */
861 if (chan->desc_pool)
862 return 0;
863
864 /*
865 * We need the descriptor to be aligned to 64bytes
866 * for meeting Xilinx VDMA specification requirement.
867 */
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530868 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530869 /* Allocate the buffer descriptors. */
870 chan->seg_v = dma_zalloc_coherent(chan->dev,
871 sizeof(*chan->seg_v) *
872 XILINX_DMA_NUM_DESCS,
873 &chan->seg_p, GFP_KERNEL);
874 if (!chan->seg_v) {
875 dev_err(chan->dev,
876 "unable to allocate channel %d descriptors\n",
877 chan->id);
878 return -ENOMEM;
879 }
880
881 for (i = 0; i < XILINX_DMA_NUM_DESCS; i++) {
882 chan->seg_v[i].hw.next_desc =
883 lower_32_bits(chan->seg_p + sizeof(*chan->seg_v) *
884 ((i + 1) % XILINX_DMA_NUM_DESCS));
885 chan->seg_v[i].hw.next_desc_msb =
886 upper_32_bits(chan->seg_p + sizeof(*chan->seg_v) *
887 ((i + 1) % XILINX_DMA_NUM_DESCS));
888 chan->seg_v[i].phys = chan->seg_p +
889 sizeof(*chan->seg_v) * i;
890 list_add_tail(&chan->seg_v[i].node,
891 &chan->free_seg_list);
892 }
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530893 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530894 chan->desc_pool = dma_pool_create("xilinx_cdma_desc_pool",
895 chan->dev,
896 sizeof(struct xilinx_cdma_tx_segment),
897 __alignof__(struct xilinx_cdma_tx_segment),
898 0);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530899 } else {
900 chan->desc_pool = dma_pool_create("xilinx_vdma_desc_pool",
901 chan->dev,
902 sizeof(struct xilinx_vdma_tx_segment),
903 __alignof__(struct xilinx_vdma_tx_segment),
904 0);
905 }
906
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530907 if (!chan->desc_pool &&
908 (chan->xdev->dma_config->dmatype != XDMA_TYPE_AXIDMA)) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530909 dev_err(chan->dev,
910 "unable to allocate channel %d descriptor pool\n",
911 chan->id);
912 return -ENOMEM;
913 }
914
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530915 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530916 /*
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530917 * For cyclic DMA mode we need to program the tail Descriptor
918 * register with a value which is not a part of the BD chain
919 * so allocating a desc segment during channel allocation for
920 * programming tail descriptor.
921 */
Kedareswara rao Appana23059402017-12-07 10:51:04 +0530922 chan->cyclic_seg_v = dma_zalloc_coherent(chan->dev,
923 sizeof(*chan->cyclic_seg_v),
924 &chan->cyclic_seg_p, GFP_KERNEL);
925 if (!chan->cyclic_seg_v) {
926 dev_err(chan->dev,
927 "unable to allocate desc segment for cyclic DMA\n");
928 return -ENOMEM;
929 }
930 chan->cyclic_seg_v->phys = chan->cyclic_seg_p;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530931 }
932
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530933 dma_cookie_init(dchan);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530934
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530935 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530936 /* For AXI DMA resetting once channel will reset the
937 * other channel as well so enable the interrupts here.
938 */
939 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
940 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
941 }
942
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530943 if ((chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) && chan->has_sg)
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530944 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
945 XILINX_CDMA_CR_SGMODE);
946
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530947 return 0;
948}
949
950/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530951 * xilinx_dma_tx_status - Get DMA transaction status
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530952 * @dchan: DMA channel
953 * @cookie: Transaction identifier
954 * @txstate: Transaction state
955 *
956 * Return: DMA transaction status
957 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530958static enum dma_status xilinx_dma_tx_status(struct dma_chan *dchan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530959 dma_cookie_t cookie,
960 struct dma_tx_state *txstate)
961{
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530962 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
963 struct xilinx_dma_tx_descriptor *desc;
964 struct xilinx_axidma_tx_segment *segment;
965 struct xilinx_axidma_desc_hw *hw;
966 enum dma_status ret;
967 unsigned long flags;
968 u32 residue = 0;
969
970 ret = dma_cookie_status(dchan, cookie, txstate);
971 if (ret == DMA_COMPLETE || !txstate)
972 return ret;
973
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530974 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530975 spin_lock_irqsave(&chan->lock, flags);
976
977 desc = list_last_entry(&chan->active_list,
978 struct xilinx_dma_tx_descriptor, node);
979 if (chan->has_sg) {
980 list_for_each_entry(segment, &desc->segments, node) {
981 hw = &segment->hw;
982 residue += (hw->control - hw->status) &
983 XILINX_DMA_MAX_TRANS_LEN;
984 }
985 }
986 spin_unlock_irqrestore(&chan->lock, flags);
987
988 chan->residue = residue;
989 dma_set_residue(txstate, chan->residue);
990 }
991
992 return ret;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530993}
994
995/**
Akinobu Mita676f9c22017-03-14 00:59:11 +0900996 * xilinx_dma_stop_transfer - Halt DMA channel
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530997 * @chan: Driver specific DMA channel
Kedareswara rao Appanae50a0ad2017-12-07 10:51:05 +0530998 *
999 * Return: '0' on success and failure value on error
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301000 */
Akinobu Mita676f9c22017-03-14 00:59:11 +09001001static int xilinx_dma_stop_transfer(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301002{
Kedareswara rao Appana9495f262016-02-26 19:33:54 +05301003 u32 val;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301004
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301005 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301006
1007 /* Wait for the hardware to halt */
Akinobu Mita676f9c22017-03-14 00:59:11 +09001008 return xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
1009 val & XILINX_DMA_DMASR_HALTED, 0,
1010 XILINX_DMA_LOOP_COUNT);
1011}
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301012
Akinobu Mita676f9c22017-03-14 00:59:11 +09001013/**
1014 * xilinx_cdma_stop_transfer - Wait for the current transfer to complete
1015 * @chan: Driver specific DMA channel
Kedareswara rao Appanae50a0ad2017-12-07 10:51:05 +05301016 *
1017 * Return: '0' on success and failure value on error
Akinobu Mita676f9c22017-03-14 00:59:11 +09001018 */
1019static int xilinx_cdma_stop_transfer(struct xilinx_dma_chan *chan)
1020{
1021 u32 val;
1022
1023 return xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
1024 val & XILINX_DMA_DMASR_IDLE, 0,
1025 XILINX_DMA_LOOP_COUNT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301026}
1027
1028/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301029 * xilinx_dma_start - Start DMA channel
1030 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301031 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301032static void xilinx_dma_start(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301033{
Kedareswara rao Appana69490632016-03-03 23:02:42 +05301034 int err;
Kedareswara rao Appana9495f262016-02-26 19:33:54 +05301035 u32 val;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301036
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301037 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301038
1039 /* Wait for the hardware to start */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301040 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
1041 !(val & XILINX_DMA_DMASR_HALTED), 0,
1042 XILINX_DMA_LOOP_COUNT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301043
Kedareswara rao Appana9495f262016-02-26 19:33:54 +05301044 if (err) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301045 dev_err(chan->dev, "Cannot start channel %p: %x\n",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301046 chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301047
1048 chan->err = true;
1049 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301050}
1051
1052/**
1053 * xilinx_vdma_start_transfer - Starts VDMA transfer
1054 * @chan: Driver specific channel struct pointer
1055 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301056static void xilinx_vdma_start_transfer(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301057{
1058 struct xilinx_vdma_config *config = &chan->config;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301059 struct xilinx_dma_tx_descriptor *desc, *tail_desc;
Kedareswara rao Appanafe0503e2017-12-07 10:51:03 +05301060 u32 reg, j;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301061 struct xilinx_vdma_tx_segment *tail_segment;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301062
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301063 /* This function was invoked with lock held */
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301064 if (chan->err)
1065 return;
1066
Kedareswara rao Appana21e02a32017-12-07 10:51:02 +05301067 if (!chan->idle)
1068 return;
1069
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301070 if (list_empty(&chan->pending_list))
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301071 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301072
1073 desc = list_first_entry(&chan->pending_list,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301074 struct xilinx_dma_tx_descriptor, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301075 tail_desc = list_last_entry(&chan->pending_list,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301076 struct xilinx_dma_tx_descriptor, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301077
1078 tail_segment = list_last_entry(&tail_desc->segments,
1079 struct xilinx_vdma_tx_segment, node);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301080
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301081 /*
1082 * If hardware is idle, then all descriptors on the running lists are
1083 * done, start new transfers
1084 */
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301085 if (chan->has_sg)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301086 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301087 desc->async_tx.phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301088
1089 /* Configure the hardware using info in the config structure */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301090 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301091
1092 if (config->frm_cnt_en)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301093 reg |= XILINX_DMA_DMACR_FRAMECNT_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301094 else
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301095 reg &= ~XILINX_DMA_DMACR_FRAMECNT_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301096
1097 /*
1098 * With SG, start with circular mode, so that BDs can be fetched.
1099 * In direct register mode, if not parking, enable circular mode
1100 */
1101 if (chan->has_sg || !config->park)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301102 reg |= XILINX_DMA_DMACR_CIRC_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301103
1104 if (config->park)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301105 reg &= ~XILINX_DMA_DMACR_CIRC_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301106
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301107 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301108
Kedareswara rao Appanafe0503e2017-12-07 10:51:03 +05301109 j = chan->desc_submitcount;
1110 reg = dma_read(chan, XILINX_DMA_REG_PARK_PTR);
1111 if (chan->direction == DMA_MEM_TO_DEV) {
1112 reg &= ~XILINX_DMA_PARK_PTR_RD_REF_MASK;
1113 reg |= j << XILINX_DMA_PARK_PTR_RD_REF_SHIFT;
1114 } else {
1115 reg &= ~XILINX_DMA_PARK_PTR_WR_REF_MASK;
1116 reg |= j << XILINX_DMA_PARK_PTR_WR_REF_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301117 }
Kedareswara rao Appanafe0503e2017-12-07 10:51:03 +05301118 dma_write(chan, XILINX_DMA_REG_PARK_PTR, reg);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301119
1120 /* Start the hardware */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301121 xilinx_dma_start(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301122
1123 if (chan->err)
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301124 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301125
1126 /* Start the transfer */
1127 if (chan->has_sg) {
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301128 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301129 tail_segment->phys);
Kedareswara rao Appanafe0503e2017-12-07 10:51:03 +05301130 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1131 chan->desc_pendingcount = 0;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301132 } else {
1133 struct xilinx_vdma_tx_segment *segment, *last = NULL;
1134 int i = 0;
1135
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +05301136 if (chan->desc_submitcount < chan->num_frms)
1137 i = chan->desc_submitcount;
1138
1139 list_for_each_entry(segment, &desc->segments, node) {
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05301140 if (chan->ext_addr)
1141 vdma_desc_write_64(chan,
1142 XILINX_VDMA_REG_START_ADDRESS_64(i++),
1143 segment->hw.buf_addr,
1144 segment->hw.buf_addr_msb);
1145 else
1146 vdma_desc_write(chan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301147 XILINX_VDMA_REG_START_ADDRESS(i++),
1148 segment->hw.buf_addr);
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05301149
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301150 last = segment;
1151 }
1152
1153 if (!last)
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301154 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301155
1156 /* HW expects these parameters to be same for one transaction */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301157 vdma_desc_write(chan, XILINX_DMA_REG_HSIZE, last->hw.hsize);
1158 vdma_desc_write(chan, XILINX_DMA_REG_FRMDLY_STRIDE,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301159 last->hw.stride);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301160 vdma_desc_write(chan, XILINX_DMA_REG_VSIZE, last->hw.vsize);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301161
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +05301162 chan->desc_submitcount++;
1163 chan->desc_pendingcount--;
Kedareswara rao Appanafe0503e2017-12-07 10:51:03 +05301164 list_del(&desc->node);
1165 list_add_tail(&desc->node, &chan->active_list);
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +05301166 if (chan->desc_submitcount == chan->num_frms)
1167 chan->desc_submitcount = 0;
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +05301168 }
Kedareswara rao Appana21e02a32017-12-07 10:51:02 +05301169
1170 chan->idle = false;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301171}
1172
1173/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301174 * xilinx_cdma_start_transfer - Starts cdma transfer
1175 * @chan: Driver specific channel struct pointer
1176 */
1177static void xilinx_cdma_start_transfer(struct xilinx_dma_chan *chan)
1178{
1179 struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1180 struct xilinx_cdma_tx_segment *tail_segment;
1181 u32 ctrl_reg = dma_read(chan, XILINX_DMA_REG_DMACR);
1182
1183 if (chan->err)
1184 return;
1185
Kedareswara rao Appana21e02a32017-12-07 10:51:02 +05301186 if (!chan->idle)
1187 return;
1188
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301189 if (list_empty(&chan->pending_list))
1190 return;
1191
1192 head_desc = list_first_entry(&chan->pending_list,
1193 struct xilinx_dma_tx_descriptor, node);
1194 tail_desc = list_last_entry(&chan->pending_list,
1195 struct xilinx_dma_tx_descriptor, node);
1196 tail_segment = list_last_entry(&tail_desc->segments,
1197 struct xilinx_cdma_tx_segment, node);
1198
1199 if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1200 ctrl_reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1201 ctrl_reg |= chan->desc_pendingcount <<
1202 XILINX_DMA_CR_COALESCE_SHIFT;
1203 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, ctrl_reg);
1204 }
1205
1206 if (chan->has_sg) {
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301207 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1208 head_desc->async_tx.phys);
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301209
1210 /* Update tail ptr register which will start the transfer */
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301211 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1212 tail_segment->phys);
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301213 } else {
1214 /* In simple mode */
1215 struct xilinx_cdma_tx_segment *segment;
1216 struct xilinx_cdma_desc_hw *hw;
1217
1218 segment = list_first_entry(&head_desc->segments,
1219 struct xilinx_cdma_tx_segment,
1220 node);
1221
1222 hw = &segment->hw;
1223
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301224 xilinx_write(chan, XILINX_CDMA_REG_SRCADDR, hw->src_addr);
1225 xilinx_write(chan, XILINX_CDMA_REG_DSTADDR, hw->dest_addr);
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301226
1227 /* Start the transfer */
1228 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1229 hw->control & XILINX_DMA_MAX_TRANS_LEN);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301230 }
1231
1232 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1233 chan->desc_pendingcount = 0;
Kedareswara rao Appana21e02a32017-12-07 10:51:02 +05301234 chan->idle = false;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301235}
1236
1237/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301238 * xilinx_dma_start_transfer - Starts DMA transfer
1239 * @chan: Driver specific channel struct pointer
1240 */
1241static void xilinx_dma_start_transfer(struct xilinx_dma_chan *chan)
1242{
1243 struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
Kedareswara rao Appana23059402017-12-07 10:51:04 +05301244 struct xilinx_axidma_tx_segment *tail_segment;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301245 u32 reg;
1246
1247 if (chan->err)
1248 return;
1249
1250 if (list_empty(&chan->pending_list))
1251 return;
1252
Kedareswara rao Appana21e02a32017-12-07 10:51:02 +05301253 if (!chan->idle)
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301254 return;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301255
1256 head_desc = list_first_entry(&chan->pending_list,
1257 struct xilinx_dma_tx_descriptor, node);
1258 tail_desc = list_last_entry(&chan->pending_list,
1259 struct xilinx_dma_tx_descriptor, node);
1260 tail_segment = list_last_entry(&tail_desc->segments,
1261 struct xilinx_axidma_tx_segment, node);
1262
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301263 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1264
1265 if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1266 reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1267 reg |= chan->desc_pendingcount <<
1268 XILINX_DMA_CR_COALESCE_SHIFT;
1269 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1270 }
1271
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301272 if (chan->has_sg && !chan->xdev->mcdma)
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301273 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1274 head_desc->async_tx.phys);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301275
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301276 if (chan->has_sg && chan->xdev->mcdma) {
1277 if (chan->direction == DMA_MEM_TO_DEV) {
1278 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
1279 head_desc->async_tx.phys);
1280 } else {
1281 if (!chan->tdest) {
1282 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
1283 head_desc->async_tx.phys);
1284 } else {
1285 dma_ctrl_write(chan,
1286 XILINX_DMA_MCRX_CDESC(chan->tdest),
1287 head_desc->async_tx.phys);
1288 }
1289 }
1290 }
1291
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301292 xilinx_dma_start(chan);
1293
1294 if (chan->err)
1295 return;
1296
1297 /* Start the transfer */
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301298 if (chan->has_sg && !chan->xdev->mcdma) {
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301299 if (chan->cyclic)
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301300 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1301 chan->cyclic_seg_v->phys);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301302 else
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301303 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1304 tail_segment->phys);
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301305 } else if (chan->has_sg && chan->xdev->mcdma) {
1306 if (chan->direction == DMA_MEM_TO_DEV) {
1307 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
1308 tail_segment->phys);
1309 } else {
1310 if (!chan->tdest) {
1311 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
1312 tail_segment->phys);
1313 } else {
1314 dma_ctrl_write(chan,
1315 XILINX_DMA_MCRX_TDESC(chan->tdest),
1316 tail_segment->phys);
1317 }
1318 }
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301319 } else {
1320 struct xilinx_axidma_tx_segment *segment;
1321 struct xilinx_axidma_desc_hw *hw;
1322
1323 segment = list_first_entry(&head_desc->segments,
1324 struct xilinx_axidma_tx_segment,
1325 node);
1326 hw = &segment->hw;
1327
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301328 xilinx_write(chan, XILINX_DMA_REG_SRCDSTADDR, hw->buf_addr);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301329
1330 /* Start the transfer */
1331 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1332 hw->control & XILINX_DMA_MAX_TRANS_LEN);
1333 }
1334
1335 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1336 chan->desc_pendingcount = 0;
Kedareswara rao Appana21e02a32017-12-07 10:51:02 +05301337 chan->idle = false;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301338}
1339
1340/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301341 * xilinx_dma_issue_pending - Issue pending transactions
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301342 * @dchan: DMA channel
1343 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301344static void xilinx_dma_issue_pending(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301345{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301346 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301347 unsigned long flags;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301348
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301349 spin_lock_irqsave(&chan->lock, flags);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301350 chan->start_transfer(chan);
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301351 spin_unlock_irqrestore(&chan->lock, flags);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301352}
1353
1354/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301355 * xilinx_dma_complete_descriptor - Mark the active descriptor as complete
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301356 * @chan : xilinx DMA channel
1357 *
1358 * CONTEXT: hardirq
1359 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301360static void xilinx_dma_complete_descriptor(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301361{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301362 struct xilinx_dma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301363
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301364 /* This function was invoked with lock held */
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301365 if (list_empty(&chan->active_list))
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301366 return;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301367
1368 list_for_each_entry_safe(desc, next, &chan->active_list, node) {
1369 list_del(&desc->node);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301370 if (!desc->cyclic)
1371 dma_cookie_complete(&desc->async_tx);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301372 list_add_tail(&desc->node, &chan->done_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301373 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301374}
1375
1376/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301377 * xilinx_dma_reset - Reset DMA channel
1378 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301379 *
1380 * Return: '0' on success and failure value on error
1381 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301382static int xilinx_dma_reset(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301383{
Kedareswara rao Appana69490632016-03-03 23:02:42 +05301384 int err;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301385 u32 tmp;
1386
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301387 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RESET);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301388
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301389 /* Wait for the hardware to finish reset */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301390 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMACR, tmp,
1391 !(tmp & XILINX_DMA_DMACR_RESET), 0,
1392 XILINX_DMA_LOOP_COUNT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301393
Kedareswara rao Appana9495f262016-02-26 19:33:54 +05301394 if (err) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301395 dev_err(chan->dev, "reset timeout, cr %x, sr %x\n",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301396 dma_ctrl_read(chan, XILINX_DMA_REG_DMACR),
1397 dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301398 return -ETIMEDOUT;
1399 }
1400
1401 chan->err = false;
Kedareswara rao Appana21e02a32017-12-07 10:51:02 +05301402 chan->idle = true;
Kedareswara rao Appanafe0503e2017-12-07 10:51:03 +05301403 chan->desc_submitcount = 0;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301404
Kedareswara rao Appana9495f262016-02-26 19:33:54 +05301405 return err;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301406}
1407
1408/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301409 * xilinx_dma_chan_reset - Reset DMA channel and enable interrupts
1410 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301411 *
1412 * Return: '0' on success and failure value on error
1413 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301414static int xilinx_dma_chan_reset(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301415{
1416 int err;
1417
1418 /* Reset VDMA */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301419 err = xilinx_dma_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301420 if (err)
1421 return err;
1422
1423 /* Enable interrupts */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301424 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
1425 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301426
1427 return 0;
1428}
1429
1430/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301431 * xilinx_dma_irq_handler - DMA Interrupt handler
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301432 * @irq: IRQ number
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301433 * @data: Pointer to the Xilinx DMA channel structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301434 *
1435 * Return: IRQ_HANDLED/IRQ_NONE
1436 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301437static irqreturn_t xilinx_dma_irq_handler(int irq, void *data)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301438{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301439 struct xilinx_dma_chan *chan = data;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301440 u32 status;
1441
1442 /* Read the status and ack the interrupts. */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301443 status = dma_ctrl_read(chan, XILINX_DMA_REG_DMASR);
1444 if (!(status & XILINX_DMA_DMAXR_ALL_IRQ_MASK))
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301445 return IRQ_NONE;
1446
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301447 dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1448 status & XILINX_DMA_DMAXR_ALL_IRQ_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301449
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301450 if (status & XILINX_DMA_DMASR_ERR_IRQ) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301451 /*
1452 * An error occurred. If C_FLUSH_ON_FSYNC is enabled and the
1453 * error is recoverable, ignore it. Otherwise flag the error.
1454 *
1455 * Only recoverable errors can be cleared in the DMASR register,
1456 * make sure not to write to other error bits to 1.
1457 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301458 u32 errors = status & XILINX_DMA_DMASR_ALL_ERR_MASK;
Kedareswara rao Appana48a59ed2016-04-06 10:44:55 +05301459
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301460 dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1461 errors & XILINX_DMA_DMASR_ERR_RECOVER_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301462
1463 if (!chan->flush_on_fsync ||
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301464 (errors & ~XILINX_DMA_DMASR_ERR_RECOVER_MASK)) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301465 dev_err(chan->dev,
1466 "Channel %p has errors %x, cdr %x tdr %x\n",
1467 chan, errors,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301468 dma_ctrl_read(chan, XILINX_DMA_REG_CURDESC),
1469 dma_ctrl_read(chan, XILINX_DMA_REG_TAILDESC));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301470 chan->err = true;
1471 }
1472 }
1473
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301474 if (status & XILINX_DMA_DMASR_DLY_CNT_IRQ) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301475 /*
1476 * Device takes too long to do the transfer when user requires
1477 * responsiveness.
1478 */
1479 dev_dbg(chan->dev, "Inter-packet latency too long\n");
1480 }
1481
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301482 if (status & XILINX_DMA_DMASR_FRM_CNT_IRQ) {
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301483 spin_lock(&chan->lock);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301484 xilinx_dma_complete_descriptor(chan);
Kedareswara rao Appana21e02a32017-12-07 10:51:02 +05301485 chan->idle = true;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301486 chan->start_transfer(chan);
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301487 spin_unlock(&chan->lock);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301488 }
1489
1490 tasklet_schedule(&chan->tasklet);
1491 return IRQ_HANDLED;
1492}
1493
1494/**
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301495 * append_desc_queue - Queuing descriptor
1496 * @chan: Driver specific dma channel
1497 * @desc: dma transaction descriptor
1498 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301499static void append_desc_queue(struct xilinx_dma_chan *chan,
1500 struct xilinx_dma_tx_descriptor *desc)
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301501{
1502 struct xilinx_vdma_tx_segment *tail_segment;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301503 struct xilinx_dma_tx_descriptor *tail_desc;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301504 struct xilinx_axidma_tx_segment *axidma_tail_segment;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301505 struct xilinx_cdma_tx_segment *cdma_tail_segment;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301506
1507 if (list_empty(&chan->pending_list))
1508 goto append;
1509
1510 /*
1511 * Add the hardware descriptor to the chain of hardware descriptors
1512 * that already exists in memory.
1513 */
1514 tail_desc = list_last_entry(&chan->pending_list,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301515 struct xilinx_dma_tx_descriptor, node);
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05301516 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301517 tail_segment = list_last_entry(&tail_desc->segments,
1518 struct xilinx_vdma_tx_segment,
1519 node);
1520 tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05301521 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301522 cdma_tail_segment = list_last_entry(&tail_desc->segments,
1523 struct xilinx_cdma_tx_segment,
1524 node);
1525 cdma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301526 } else {
1527 axidma_tail_segment = list_last_entry(&tail_desc->segments,
1528 struct xilinx_axidma_tx_segment,
1529 node);
1530 axidma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1531 }
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301532
1533 /*
1534 * Add the software descriptor and all children to the list
1535 * of pending transactions
1536 */
1537append:
1538 list_add_tail(&desc->node, &chan->pending_list);
1539 chan->desc_pendingcount++;
1540
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05301541 if (chan->has_sg && (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA)
1542 && unlikely(chan->desc_pendingcount > chan->num_frms)) {
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301543 dev_dbg(chan->dev, "desc pendingcount is too high\n");
1544 chan->desc_pendingcount = chan->num_frms;
1545 }
1546}
1547
1548/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301549 * xilinx_dma_tx_submit - Submit DMA transaction
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301550 * @tx: Async transaction descriptor
1551 *
1552 * Return: cookie value on success and failure value on error
1553 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301554static dma_cookie_t xilinx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301555{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301556 struct xilinx_dma_tx_descriptor *desc = to_dma_tx_descriptor(tx);
1557 struct xilinx_dma_chan *chan = to_xilinx_chan(tx->chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301558 dma_cookie_t cookie;
1559 unsigned long flags;
1560 int err;
1561
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301562 if (chan->cyclic) {
1563 xilinx_dma_free_tx_descriptor(chan, desc);
1564 return -EBUSY;
1565 }
1566
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301567 if (chan->err) {
1568 /*
1569 * If reset fails, need to hard reset the system.
1570 * Channel is no longer functional
1571 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301572 err = xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301573 if (err < 0)
1574 return err;
1575 }
1576
1577 spin_lock_irqsave(&chan->lock, flags);
1578
1579 cookie = dma_cookie_assign(tx);
1580
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301581 /* Put this transaction onto the tail of the pending queue */
1582 append_desc_queue(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301583
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301584 if (desc->cyclic)
1585 chan->cyclic = true;
1586
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301587 spin_unlock_irqrestore(&chan->lock, flags);
1588
1589 return cookie;
1590}
1591
1592/**
1593 * xilinx_vdma_dma_prep_interleaved - prepare a descriptor for a
1594 * DMA_SLAVE transaction
1595 * @dchan: DMA channel
1596 * @xt: Interleaved template pointer
1597 * @flags: transfer ack flags
1598 *
1599 * Return: Async transaction descriptor on success and NULL on failure
1600 */
1601static struct dma_async_tx_descriptor *
1602xilinx_vdma_dma_prep_interleaved(struct dma_chan *dchan,
1603 struct dma_interleaved_template *xt,
1604 unsigned long flags)
1605{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301606 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1607 struct xilinx_dma_tx_descriptor *desc;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301608 struct xilinx_vdma_tx_segment *segment, *prev = NULL;
1609 struct xilinx_vdma_desc_hw *hw;
1610
1611 if (!is_slave_direction(xt->dir))
1612 return NULL;
1613
1614 if (!xt->numf || !xt->sgl[0].size)
1615 return NULL;
1616
Srikanth Thokalaa5e48e22014-11-05 20:37:01 +02001617 if (xt->frame_size != 1)
1618 return NULL;
1619
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301620 /* Allocate a transaction descriptor. */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301621 desc = xilinx_dma_alloc_tx_descriptor(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301622 if (!desc)
1623 return NULL;
1624
1625 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301626 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301627 async_tx_ack(&desc->async_tx);
1628
1629 /* Allocate the link descriptor from DMA pool */
1630 segment = xilinx_vdma_alloc_tx_segment(chan);
1631 if (!segment)
1632 goto error;
1633
1634 /* Fill in the hardware descriptor */
1635 hw = &segment->hw;
1636 hw->vsize = xt->numf;
1637 hw->hsize = xt->sgl[0].size;
Srikanth Thokala6d80f452014-11-05 20:37:02 +02001638 hw->stride = (xt->sgl[0].icg + xt->sgl[0].size) <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301639 XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301640 hw->stride |= chan->config.frm_dly <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301641 XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301642
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05301643 if (xt->dir != DMA_MEM_TO_DEV) {
1644 if (chan->ext_addr) {
1645 hw->buf_addr = lower_32_bits(xt->dst_start);
1646 hw->buf_addr_msb = upper_32_bits(xt->dst_start);
1647 } else {
1648 hw->buf_addr = xt->dst_start;
1649 }
1650 } else {
1651 if (chan->ext_addr) {
1652 hw->buf_addr = lower_32_bits(xt->src_start);
1653 hw->buf_addr_msb = upper_32_bits(xt->src_start);
1654 } else {
1655 hw->buf_addr = xt->src_start;
1656 }
1657 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301658
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301659 /* Insert the segment into the descriptor segments list. */
1660 list_add_tail(&segment->node, &desc->segments);
1661
1662 prev = segment;
1663
1664 /* Link the last hardware descriptor with the first. */
1665 segment = list_first_entry(&desc->segments,
1666 struct xilinx_vdma_tx_segment, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301667 desc->async_tx.phys = segment->phys;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301668
1669 return &desc->async_tx;
1670
1671error:
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301672 xilinx_dma_free_tx_descriptor(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301673 return NULL;
1674}
1675
1676/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301677 * xilinx_cdma_prep_memcpy - prepare descriptors for a memcpy transaction
1678 * @dchan: DMA channel
1679 * @dma_dst: destination address
1680 * @dma_src: source address
1681 * @len: transfer length
1682 * @flags: transfer ack flags
1683 *
1684 * Return: Async transaction descriptor on success and NULL on failure
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301685 */
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301686static struct dma_async_tx_descriptor *
1687xilinx_cdma_prep_memcpy(struct dma_chan *dchan, dma_addr_t dma_dst,
1688 dma_addr_t dma_src, size_t len, unsigned long flags)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301689{
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301690 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1691 struct xilinx_dma_tx_descriptor *desc;
Akinobu Mitadb6a3d02017-03-14 00:59:12 +09001692 struct xilinx_cdma_tx_segment *segment;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301693 struct xilinx_cdma_desc_hw *hw;
1694
1695 if (!len || len > XILINX_DMA_MAX_TRANS_LEN)
1696 return NULL;
1697
1698 desc = xilinx_dma_alloc_tx_descriptor(chan);
1699 if (!desc)
1700 return NULL;
1701
1702 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1703 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1704
1705 /* Allocate the link descriptor from DMA pool */
1706 segment = xilinx_cdma_alloc_tx_segment(chan);
1707 if (!segment)
1708 goto error;
1709
1710 hw = &segment->hw;
1711 hw->control = len;
1712 hw->src_addr = dma_src;
1713 hw->dest_addr = dma_dst;
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301714 if (chan->ext_addr) {
1715 hw->src_addr_msb = upper_32_bits(dma_src);
1716 hw->dest_addr_msb = upper_32_bits(dma_dst);
1717 }
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301718
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301719 /* Insert the segment into the descriptor segments list. */
1720 list_add_tail(&segment->node, &desc->segments);
1721
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301722 desc->async_tx.phys = segment->phys;
Akinobu Mitadb6a3d02017-03-14 00:59:12 +09001723 hw->next_desc = segment->phys;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301724
1725 return &desc->async_tx;
1726
1727error:
1728 xilinx_dma_free_tx_descriptor(chan, desc);
1729 return NULL;
1730}
1731
1732/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301733 * xilinx_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
1734 * @dchan: DMA channel
1735 * @sgl: scatterlist to transfer to/from
1736 * @sg_len: number of entries in @scatterlist
1737 * @direction: DMA direction
1738 * @flags: transfer ack flags
1739 * @context: APP words of the descriptor
1740 *
1741 * Return: Async transaction descriptor on success and NULL on failure
1742 */
1743static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
1744 struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
1745 enum dma_transfer_direction direction, unsigned long flags,
1746 void *context)
1747{
1748 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1749 struct xilinx_dma_tx_descriptor *desc;
Kedareswara rao Appana23059402017-12-07 10:51:04 +05301750 struct xilinx_axidma_tx_segment *segment = NULL;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301751 u32 *app_w = (u32 *)context;
1752 struct scatterlist *sg;
1753 size_t copy;
1754 size_t sg_used;
1755 unsigned int i;
1756
1757 if (!is_slave_direction(direction))
1758 return NULL;
1759
1760 /* Allocate a transaction descriptor. */
1761 desc = xilinx_dma_alloc_tx_descriptor(chan);
1762 if (!desc)
1763 return NULL;
1764
1765 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1766 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1767
1768 /* Build transactions using information in the scatter gather list */
1769 for_each_sg(sgl, sg, sg_len, i) {
1770 sg_used = 0;
1771
1772 /* Loop until the entire scatterlist entry is used */
1773 while (sg_used < sg_dma_len(sg)) {
1774 struct xilinx_axidma_desc_hw *hw;
1775
1776 /* Get a free segment */
1777 segment = xilinx_axidma_alloc_tx_segment(chan);
1778 if (!segment)
1779 goto error;
1780
1781 /*
1782 * Calculate the maximum number of bytes to transfer,
1783 * making sure it is less than the hw limit
1784 */
1785 copy = min_t(size_t, sg_dma_len(sg) - sg_used,
1786 XILINX_DMA_MAX_TRANS_LEN);
1787 hw = &segment->hw;
1788
1789 /* Fill in the descriptor */
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301790 xilinx_axidma_buf(chan, hw, sg_dma_address(sg),
1791 sg_used, 0);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301792
1793 hw->control = copy;
1794
1795 if (chan->direction == DMA_MEM_TO_DEV) {
1796 if (app_w)
1797 memcpy(hw->app, app_w, sizeof(u32) *
1798 XILINX_DMA_NUM_APP_WORDS);
1799 }
1800
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301801 sg_used += copy;
1802
1803 /*
1804 * Insert the segment into the descriptor segments
1805 * list.
1806 */
1807 list_add_tail(&segment->node, &desc->segments);
1808 }
1809 }
1810
1811 segment = list_first_entry(&desc->segments,
1812 struct xilinx_axidma_tx_segment, node);
1813 desc->async_tx.phys = segment->phys;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301814
1815 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1816 if (chan->direction == DMA_MEM_TO_DEV) {
1817 segment->hw.control |= XILINX_DMA_BD_SOP;
1818 segment = list_last_entry(&desc->segments,
1819 struct xilinx_axidma_tx_segment,
1820 node);
1821 segment->hw.control |= XILINX_DMA_BD_EOP;
1822 }
1823
1824 return &desc->async_tx;
1825
1826error:
1827 xilinx_dma_free_tx_descriptor(chan, desc);
1828 return NULL;
1829}
1830
1831/**
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301832 * xilinx_dma_prep_dma_cyclic - prepare descriptors for a DMA_SLAVE transaction
Kedareswara rao Appanae50a0ad2017-12-07 10:51:05 +05301833 * @dchan: DMA channel
1834 * @buf_addr: Physical address of the buffer
1835 * @buf_len: Total length of the cyclic buffers
1836 * @period_len: length of individual cyclic buffer
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301837 * @direction: DMA direction
1838 * @flags: transfer ack flags
Kedareswara rao Appanae50a0ad2017-12-07 10:51:05 +05301839 *
1840 * Return: Async transaction descriptor on success and NULL on failure
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301841 */
1842static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
1843 struct dma_chan *dchan, dma_addr_t buf_addr, size_t buf_len,
1844 size_t period_len, enum dma_transfer_direction direction,
1845 unsigned long flags)
1846{
1847 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1848 struct xilinx_dma_tx_descriptor *desc;
1849 struct xilinx_axidma_tx_segment *segment, *head_segment, *prev = NULL;
1850 size_t copy, sg_used;
1851 unsigned int num_periods;
1852 int i;
1853 u32 reg;
1854
Arnd Bergmannf67c3bd2016-06-13 17:07:33 +02001855 if (!period_len)
1856 return NULL;
1857
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301858 num_periods = buf_len / period_len;
1859
Arnd Bergmannf67c3bd2016-06-13 17:07:33 +02001860 if (!num_periods)
1861 return NULL;
1862
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301863 if (!is_slave_direction(direction))
1864 return NULL;
1865
1866 /* Allocate a transaction descriptor. */
1867 desc = xilinx_dma_alloc_tx_descriptor(chan);
1868 if (!desc)
1869 return NULL;
1870
1871 chan->direction = direction;
1872 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1873 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1874
1875 for (i = 0; i < num_periods; ++i) {
1876 sg_used = 0;
1877
1878 while (sg_used < period_len) {
1879 struct xilinx_axidma_desc_hw *hw;
1880
1881 /* Get a free segment */
1882 segment = xilinx_axidma_alloc_tx_segment(chan);
1883 if (!segment)
1884 goto error;
1885
1886 /*
1887 * Calculate the maximum number of bytes to transfer,
1888 * making sure it is less than the hw limit
1889 */
1890 copy = min_t(size_t, period_len - sg_used,
1891 XILINX_DMA_MAX_TRANS_LEN);
1892 hw = &segment->hw;
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301893 xilinx_axidma_buf(chan, hw, buf_addr, sg_used,
1894 period_len * i);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301895 hw->control = copy;
1896
1897 if (prev)
1898 prev->hw.next_desc = segment->phys;
1899
1900 prev = segment;
1901 sg_used += copy;
1902
1903 /*
1904 * Insert the segment into the descriptor segments
1905 * list.
1906 */
1907 list_add_tail(&segment->node, &desc->segments);
1908 }
1909 }
1910
1911 head_segment = list_first_entry(&desc->segments,
1912 struct xilinx_axidma_tx_segment, node);
1913 desc->async_tx.phys = head_segment->phys;
1914
1915 desc->cyclic = true;
1916 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1917 reg |= XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
1918 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1919
Kedareswara rao Appanae598e6e2016-07-09 14:09:48 +05301920 segment = list_last_entry(&desc->segments,
1921 struct xilinx_axidma_tx_segment,
1922 node);
1923 segment->hw.next_desc = (u32) head_segment->phys;
1924
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301925 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1926 if (direction == DMA_MEM_TO_DEV) {
Kedareswara rao Appanae167a0b2016-06-09 11:32:12 +05301927 head_segment->hw.control |= XILINX_DMA_BD_SOP;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301928 segment->hw.control |= XILINX_DMA_BD_EOP;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301929 }
1930
1931 return &desc->async_tx;
1932
1933error:
1934 xilinx_dma_free_tx_descriptor(chan, desc);
1935 return NULL;
1936}
1937
1938/**
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301939 * xilinx_dma_prep_interleaved - prepare a descriptor for a
1940 * DMA_SLAVE transaction
1941 * @dchan: DMA channel
1942 * @xt: Interleaved template pointer
1943 * @flags: transfer ack flags
1944 *
1945 * Return: Async transaction descriptor on success and NULL on failure
1946 */
1947static struct dma_async_tx_descriptor *
1948xilinx_dma_prep_interleaved(struct dma_chan *dchan,
1949 struct dma_interleaved_template *xt,
1950 unsigned long flags)
1951{
1952 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1953 struct xilinx_dma_tx_descriptor *desc;
1954 struct xilinx_axidma_tx_segment *segment;
1955 struct xilinx_axidma_desc_hw *hw;
1956
1957 if (!is_slave_direction(xt->dir))
1958 return NULL;
1959
1960 if (!xt->numf || !xt->sgl[0].size)
1961 return NULL;
1962
1963 if (xt->frame_size != 1)
1964 return NULL;
1965
1966 /* Allocate a transaction descriptor. */
1967 desc = xilinx_dma_alloc_tx_descriptor(chan);
1968 if (!desc)
1969 return NULL;
1970
1971 chan->direction = xt->dir;
1972 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1973 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1974
1975 /* Get a free segment */
1976 segment = xilinx_axidma_alloc_tx_segment(chan);
1977 if (!segment)
1978 goto error;
1979
1980 hw = &segment->hw;
1981
1982 /* Fill in the descriptor */
1983 if (xt->dir != DMA_MEM_TO_DEV)
1984 hw->buf_addr = xt->dst_start;
1985 else
1986 hw->buf_addr = xt->src_start;
1987
1988 hw->mcdma_control = chan->tdest & XILINX_DMA_BD_TDEST_MASK;
1989 hw->vsize_stride = (xt->numf << XILINX_DMA_BD_VSIZE_SHIFT) &
1990 XILINX_DMA_BD_VSIZE_MASK;
1991 hw->vsize_stride |= (xt->sgl[0].icg + xt->sgl[0].size) &
1992 XILINX_DMA_BD_STRIDE_MASK;
1993 hw->control = xt->sgl[0].size & XILINX_DMA_BD_HSIZE_MASK;
1994
1995 /*
1996 * Insert the segment into the descriptor segments
1997 * list.
1998 */
1999 list_add_tail(&segment->node, &desc->segments);
2000
2001
2002 segment = list_first_entry(&desc->segments,
2003 struct xilinx_axidma_tx_segment, node);
2004 desc->async_tx.phys = segment->phys;
2005
2006 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
2007 if (xt->dir == DMA_MEM_TO_DEV) {
2008 segment->hw.control |= XILINX_DMA_BD_SOP;
2009 segment = list_last_entry(&desc->segments,
2010 struct xilinx_axidma_tx_segment,
2011 node);
2012 segment->hw.control |= XILINX_DMA_BD_EOP;
2013 }
2014
2015 return &desc->async_tx;
2016
2017error:
2018 xilinx_dma_free_tx_descriptor(chan, desc);
2019 return NULL;
2020}
2021
2022/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302023 * xilinx_dma_terminate_all - Halt the channel and free descriptors
Kedareswara rao Appanae50a0ad2017-12-07 10:51:05 +05302024 * @dchan: Driver specific DMA Channel pointer
2025 *
2026 * Return: '0' always.
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302027 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302028static int xilinx_dma_terminate_all(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302029{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302030 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302031 u32 reg;
Akinobu Mita676f9c22017-03-14 00:59:11 +09002032 int err;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302033
2034 if (chan->cyclic)
2035 xilinx_dma_chan_reset(chan);
Maxime Ripardba714042014-11-17 14:42:38 +01002036
Akinobu Mita676f9c22017-03-14 00:59:11 +09002037 err = chan->stop_transfer(chan);
2038 if (err) {
2039 dev_err(chan->dev, "Cannot stop channel %p: %x\n",
2040 chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
2041 chan->err = true;
2042 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302043
2044 /* Remove and free all of the descriptors in the lists */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302045 xilinx_dma_free_descriptors(chan);
Kedareswara rao Appana21e02a32017-12-07 10:51:02 +05302046 chan->idle = true;
Maxime Ripardba714042014-11-17 14:42:38 +01002047
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302048 if (chan->cyclic) {
2049 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
2050 reg &= ~XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
2051 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
2052 chan->cyclic = false;
2053 }
2054
Maxime Ripardba714042014-11-17 14:42:38 +01002055 return 0;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302056}
2057
2058/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302059 * xilinx_dma_channel_set_config - Configure VDMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302060 * Run-time configuration for Axi VDMA, supports:
2061 * . halt the channel
2062 * . configure interrupt coalescing and inter-packet delay threshold
2063 * . start/stop parking
2064 * . enable genlock
2065 *
2066 * @dchan: DMA channel
2067 * @cfg: VDMA device configuration pointer
2068 *
2069 * Return: '0' on success and failure value on error
2070 */
2071int xilinx_vdma_channel_set_config(struct dma_chan *dchan,
2072 struct xilinx_vdma_config *cfg)
2073{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302074 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302075 u32 dmacr;
2076
2077 if (cfg->reset)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302078 return xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302079
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302080 dmacr = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302081
2082 chan->config.frm_dly = cfg->frm_dly;
2083 chan->config.park = cfg->park;
2084
2085 /* genlock settings */
2086 chan->config.gen_lock = cfg->gen_lock;
2087 chan->config.master = cfg->master;
2088
2089 if (cfg->gen_lock && chan->genlock) {
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302090 dmacr |= XILINX_DMA_DMACR_GENLOCK_EN;
2091 dmacr |= cfg->master << XILINX_DMA_DMACR_MASTER_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302092 }
2093
2094 chan->config.frm_cnt_en = cfg->frm_cnt_en;
2095 if (cfg->park)
2096 chan->config.park_frm = cfg->park_frm;
2097 else
2098 chan->config.park_frm = -1;
2099
2100 chan->config.coalesc = cfg->coalesc;
2101 chan->config.delay = cfg->delay;
2102
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302103 if (cfg->coalesc <= XILINX_DMA_DMACR_FRAME_COUNT_MAX) {
2104 dmacr |= cfg->coalesc << XILINX_DMA_DMACR_FRAME_COUNT_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302105 chan->config.coalesc = cfg->coalesc;
2106 }
2107
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302108 if (cfg->delay <= XILINX_DMA_DMACR_DELAY_MAX) {
2109 dmacr |= cfg->delay << XILINX_DMA_DMACR_DELAY_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302110 chan->config.delay = cfg->delay;
2111 }
2112
2113 /* FSync Source selection */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302114 dmacr &= ~XILINX_DMA_DMACR_FSYNCSRC_MASK;
2115 dmacr |= cfg->ext_fsync << XILINX_DMA_DMACR_FSYNCSRC_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302116
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302117 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, dmacr);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302118
2119 return 0;
2120}
2121EXPORT_SYMBOL(xilinx_vdma_channel_set_config);
2122
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302123/* -----------------------------------------------------------------------------
2124 * Probe and remove
2125 */
2126
2127/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302128 * xilinx_dma_chan_remove - Per Channel remove function
2129 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302130 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302131static void xilinx_dma_chan_remove(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302132{
2133 /* Disable all interrupts */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302134 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
2135 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302136
2137 if (chan->irq > 0)
2138 free_irq(chan->irq, chan);
2139
2140 tasklet_kill(&chan->tasklet);
2141
2142 list_del(&chan->common.device_node);
2143}
2144
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302145static int axidma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2146 struct clk **tx_clk, struct clk **rx_clk,
2147 struct clk **sg_clk, struct clk **tmp_clk)
2148{
2149 int err;
2150
2151 *tmp_clk = NULL;
2152
2153 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2154 if (IS_ERR(*axi_clk)) {
2155 err = PTR_ERR(*axi_clk);
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002156 dev_err(&pdev->dev, "failed to get axi_aclk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302157 return err;
2158 }
2159
2160 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2161 if (IS_ERR(*tx_clk))
2162 *tx_clk = NULL;
2163
2164 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2165 if (IS_ERR(*rx_clk))
2166 *rx_clk = NULL;
2167
2168 *sg_clk = devm_clk_get(&pdev->dev, "m_axi_sg_aclk");
2169 if (IS_ERR(*sg_clk))
2170 *sg_clk = NULL;
2171
2172 err = clk_prepare_enable(*axi_clk);
2173 if (err) {
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002174 dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302175 return err;
2176 }
2177
2178 err = clk_prepare_enable(*tx_clk);
2179 if (err) {
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002180 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302181 goto err_disable_axiclk;
2182 }
2183
2184 err = clk_prepare_enable(*rx_clk);
2185 if (err) {
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002186 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302187 goto err_disable_txclk;
2188 }
2189
2190 err = clk_prepare_enable(*sg_clk);
2191 if (err) {
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002192 dev_err(&pdev->dev, "failed to enable sg_clk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302193 goto err_disable_rxclk;
2194 }
2195
2196 return 0;
2197
2198err_disable_rxclk:
2199 clk_disable_unprepare(*rx_clk);
2200err_disable_txclk:
2201 clk_disable_unprepare(*tx_clk);
2202err_disable_axiclk:
2203 clk_disable_unprepare(*axi_clk);
2204
2205 return err;
2206}
2207
2208static int axicdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2209 struct clk **dev_clk, struct clk **tmp_clk,
2210 struct clk **tmp1_clk, struct clk **tmp2_clk)
2211{
2212 int err;
2213
2214 *tmp_clk = NULL;
2215 *tmp1_clk = NULL;
2216 *tmp2_clk = NULL;
2217
2218 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2219 if (IS_ERR(*axi_clk)) {
2220 err = PTR_ERR(*axi_clk);
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002221 dev_err(&pdev->dev, "failed to get axi_clk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302222 return err;
2223 }
2224
2225 *dev_clk = devm_clk_get(&pdev->dev, "m_axi_aclk");
2226 if (IS_ERR(*dev_clk)) {
2227 err = PTR_ERR(*dev_clk);
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002228 dev_err(&pdev->dev, "failed to get dev_clk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302229 return err;
2230 }
2231
2232 err = clk_prepare_enable(*axi_clk);
2233 if (err) {
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002234 dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302235 return err;
2236 }
2237
2238 err = clk_prepare_enable(*dev_clk);
2239 if (err) {
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002240 dev_err(&pdev->dev, "failed to enable dev_clk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302241 goto err_disable_axiclk;
2242 }
2243
2244 return 0;
2245
2246err_disable_axiclk:
2247 clk_disable_unprepare(*axi_clk);
2248
2249 return err;
2250}
2251
2252static int axivdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2253 struct clk **tx_clk, struct clk **txs_clk,
2254 struct clk **rx_clk, struct clk **rxs_clk)
2255{
2256 int err;
2257
2258 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2259 if (IS_ERR(*axi_clk)) {
2260 err = PTR_ERR(*axi_clk);
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002261 dev_err(&pdev->dev, "failed to get axi_aclk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302262 return err;
2263 }
2264
2265 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2266 if (IS_ERR(*tx_clk))
2267 *tx_clk = NULL;
2268
2269 *txs_clk = devm_clk_get(&pdev->dev, "m_axis_mm2s_aclk");
2270 if (IS_ERR(*txs_clk))
2271 *txs_clk = NULL;
2272
2273 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2274 if (IS_ERR(*rx_clk))
2275 *rx_clk = NULL;
2276
2277 *rxs_clk = devm_clk_get(&pdev->dev, "s_axis_s2mm_aclk");
2278 if (IS_ERR(*rxs_clk))
2279 *rxs_clk = NULL;
2280
2281 err = clk_prepare_enable(*axi_clk);
2282 if (err) {
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002283 dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302284 return err;
2285 }
2286
2287 err = clk_prepare_enable(*tx_clk);
2288 if (err) {
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002289 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302290 goto err_disable_axiclk;
2291 }
2292
2293 err = clk_prepare_enable(*txs_clk);
2294 if (err) {
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002295 dev_err(&pdev->dev, "failed to enable txs_clk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302296 goto err_disable_txclk;
2297 }
2298
2299 err = clk_prepare_enable(*rx_clk);
2300 if (err) {
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002301 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302302 goto err_disable_txsclk;
2303 }
2304
2305 err = clk_prepare_enable(*rxs_clk);
2306 if (err) {
Lars-Peter Clausen574897d2017-08-31 13:35:10 +02002307 dev_err(&pdev->dev, "failed to enable rxs_clk (%d)\n", err);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302308 goto err_disable_rxclk;
2309 }
2310
2311 return 0;
2312
2313err_disable_rxclk:
2314 clk_disable_unprepare(*rx_clk);
2315err_disable_txsclk:
2316 clk_disable_unprepare(*txs_clk);
2317err_disable_txclk:
2318 clk_disable_unprepare(*tx_clk);
2319err_disable_axiclk:
2320 clk_disable_unprepare(*axi_clk);
2321
2322 return err;
2323}
2324
2325static void xdma_disable_allclks(struct xilinx_dma_device *xdev)
2326{
2327 clk_disable_unprepare(xdev->rxs_clk);
2328 clk_disable_unprepare(xdev->rx_clk);
2329 clk_disable_unprepare(xdev->txs_clk);
2330 clk_disable_unprepare(xdev->tx_clk);
2331 clk_disable_unprepare(xdev->axi_clk);
2332}
2333
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302334/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302335 * xilinx_dma_chan_probe - Per Channel Probing
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302336 * It get channel features from the device tree entry and
2337 * initialize special channel handling routines
2338 *
2339 * @xdev: Driver specific device structure
2340 * @node: Device node
Kedareswara rao Appanae50a0ad2017-12-07 10:51:05 +05302341 * @chan_id: DMA Channel id
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302342 *
2343 * Return: '0' on success and failure value on error
2344 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302345static int xilinx_dma_chan_probe(struct xilinx_dma_device *xdev,
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302346 struct device_node *node, int chan_id)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302347{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302348 struct xilinx_dma_chan *chan;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302349 bool has_dre = false;
2350 u32 value, width;
2351 int err;
2352
2353 /* Allocate and initialize the channel structure */
2354 chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
2355 if (!chan)
2356 return -ENOMEM;
2357
2358 chan->dev = xdev->dev;
2359 chan->xdev = xdev;
2360 chan->has_sg = xdev->has_sg;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05302361 chan->desc_pendingcount = 0x0;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302362 chan->ext_addr = xdev->ext_addr;
Kedareswara rao Appana21e02a32017-12-07 10:51:02 +05302363 /* This variable enusres that descripotrs are not
2364 * Submited when dma engine is in progress. This variable is
2365 * Added to avoid pollling for a bit in the status register to
2366 * Know dma state in the driver hot path.
2367 */
2368 chan->idle = true;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302369
2370 spin_lock_init(&chan->lock);
2371 INIT_LIST_HEAD(&chan->pending_list);
2372 INIT_LIST_HEAD(&chan->done_list);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05302373 INIT_LIST_HEAD(&chan->active_list);
Kedareswara rao Appana23059402017-12-07 10:51:04 +05302374 INIT_LIST_HEAD(&chan->free_seg_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302375
2376 /* Retrieve the channel properties from the device tree */
2377 has_dre = of_property_read_bool(node, "xlnx,include-dre");
2378
2379 chan->genlock = of_property_read_bool(node, "xlnx,genlock-mode");
2380
2381 err = of_property_read_u32(node, "xlnx,datawidth", &value);
2382 if (err) {
2383 dev_err(xdev->dev, "missing xlnx,datawidth property\n");
2384 return err;
2385 }
2386 width = value >> 3; /* Convert bits to bytes */
2387
2388 /* If data width is greater than 8 bytes, DRE is not in hw */
2389 if (width > 8)
2390 has_dre = false;
2391
2392 if (!has_dre)
2393 xdev->common.copy_align = fls(width - 1);
2394
Kedareswara rao Appanae131f1b2016-06-24 10:51:26 +05302395 if (of_device_is_compatible(node, "xlnx,axi-vdma-mm2s-channel") ||
2396 of_device_is_compatible(node, "xlnx,axi-dma-mm2s-channel") ||
2397 of_device_is_compatible(node, "xlnx,axi-cdma-channel")) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302398 chan->direction = DMA_MEM_TO_DEV;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302399 chan->id = chan_id;
2400 chan->tdest = chan_id;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302401
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302402 chan->ctrl_offset = XILINX_DMA_MM2S_CTRL_OFFSET;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302403 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302404 chan->desc_offset = XILINX_VDMA_MM2S_DESC_OFFSET;
Kedareswara rao Appanafe0503e2017-12-07 10:51:03 +05302405 chan->config.park = 1;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302406
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302407 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2408 xdev->flush_on_fsync == XILINX_DMA_FLUSH_MM2S)
2409 chan->flush_on_fsync = true;
2410 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302411 } else if (of_device_is_compatible(node,
Kedareswara rao Appanae131f1b2016-06-24 10:51:26 +05302412 "xlnx,axi-vdma-s2mm-channel") ||
2413 of_device_is_compatible(node,
2414 "xlnx,axi-dma-s2mm-channel")) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302415 chan->direction = DMA_DEV_TO_MEM;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302416 chan->id = chan_id;
2417 chan->tdest = chan_id - xdev->nr_channels;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302418
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302419 chan->ctrl_offset = XILINX_DMA_S2MM_CTRL_OFFSET;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302420 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302421 chan->desc_offset = XILINX_VDMA_S2MM_DESC_OFFSET;
Kedareswara rao Appanafe0503e2017-12-07 10:51:03 +05302422 chan->config.park = 1;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302423
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302424 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2425 xdev->flush_on_fsync == XILINX_DMA_FLUSH_S2MM)
2426 chan->flush_on_fsync = true;
2427 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302428 } else {
2429 dev_err(xdev->dev, "Invalid channel compatible node\n");
2430 return -EINVAL;
2431 }
2432
2433 /* Request the interrupt */
2434 chan->irq = irq_of_parse_and_map(node, 0);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302435 err = request_irq(chan->irq, xilinx_dma_irq_handler, IRQF_SHARED,
2436 "xilinx-dma-controller", chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302437 if (err) {
2438 dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq);
2439 return err;
2440 }
2441
Akinobu Mita676f9c22017-03-14 00:59:11 +09002442 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302443 chan->start_transfer = xilinx_dma_start_transfer;
Akinobu Mita676f9c22017-03-14 00:59:11 +09002444 chan->stop_transfer = xilinx_dma_stop_transfer;
2445 } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302446 chan->start_transfer = xilinx_cdma_start_transfer;
Akinobu Mita676f9c22017-03-14 00:59:11 +09002447 chan->stop_transfer = xilinx_cdma_stop_transfer;
2448 } else {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302449 chan->start_transfer = xilinx_vdma_start_transfer;
Akinobu Mita676f9c22017-03-14 00:59:11 +09002450 chan->stop_transfer = xilinx_dma_stop_transfer;
2451 }
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302452
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302453 /* Initialize the tasklet */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302454 tasklet_init(&chan->tasklet, xilinx_dma_do_tasklet,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302455 (unsigned long)chan);
2456
2457 /*
2458 * Initialize the DMA channel and add it to the DMA engine channels
2459 * list.
2460 */
2461 chan->common.device = &xdev->common;
2462
2463 list_add_tail(&chan->common.device_node, &xdev->common.channels);
2464 xdev->chan[chan->id] = chan;
2465
2466 /* Reset the channel */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302467 err = xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302468 if (err < 0) {
2469 dev_err(xdev->dev, "Reset channel failed\n");
2470 return err;
2471 }
2472
2473 return 0;
2474}
2475
2476/**
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302477 * xilinx_dma_child_probe - Per child node probe
2478 * It get number of dma-channels per child node from
2479 * device-tree and initializes all the channels.
2480 *
2481 * @xdev: Driver specific device structure
2482 * @node: Device node
2483 *
2484 * Return: 0 always.
2485 */
2486static int xilinx_dma_child_probe(struct xilinx_dma_device *xdev,
Kedareswara rao Appana22653af2017-12-07 10:51:06 +05302487 struct device_node *node)
2488{
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302489 int ret, i, nr_channels = 1;
2490
2491 ret = of_property_read_u32(node, "dma-channels", &nr_channels);
2492 if ((ret < 0) && xdev->mcdma)
2493 dev_warn(xdev->dev, "missing dma-channels property\n");
2494
2495 for (i = 0; i < nr_channels; i++)
2496 xilinx_dma_chan_probe(xdev, node, xdev->chan_id++);
2497
2498 xdev->nr_channels += nr_channels;
2499
2500 return 0;
2501}
2502
2503/**
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302504 * of_dma_xilinx_xlate - Translation function
2505 * @dma_spec: Pointer to DMA specifier as found in the device tree
2506 * @ofdma: Pointer to DMA controller data
2507 *
2508 * Return: DMA channel pointer on success and NULL on error
2509 */
2510static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
2511 struct of_dma *ofdma)
2512{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302513 struct xilinx_dma_device *xdev = ofdma->of_dma_data;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302514 int chan_id = dma_spec->args[0];
2515
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302516 if (chan_id >= xdev->nr_channels || !xdev->chan[chan_id])
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302517 return NULL;
2518
2519 return dma_get_slave_channel(&xdev->chan[chan_id]->common);
2520}
2521
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302522static const struct xilinx_dma_config axidma_config = {
2523 .dmatype = XDMA_TYPE_AXIDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302524 .clk_init = axidma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302525};
2526
2527static const struct xilinx_dma_config axicdma_config = {
2528 .dmatype = XDMA_TYPE_CDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302529 .clk_init = axicdma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302530};
2531
2532static const struct xilinx_dma_config axivdma_config = {
2533 .dmatype = XDMA_TYPE_VDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302534 .clk_init = axivdma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302535};
2536
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302537static const struct of_device_id xilinx_dma_of_ids[] = {
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302538 { .compatible = "xlnx,axi-dma-1.00.a", .data = &axidma_config },
2539 { .compatible = "xlnx,axi-cdma-1.00.a", .data = &axicdma_config },
2540 { .compatible = "xlnx,axi-vdma-1.00.a", .data = &axivdma_config },
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302541 {}
2542};
2543MODULE_DEVICE_TABLE(of, xilinx_dma_of_ids);
2544
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302545/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302546 * xilinx_dma_probe - Driver probe function
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302547 * @pdev: Pointer to the platform_device structure
2548 *
2549 * Return: '0' on success and failure value on error
2550 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302551static int xilinx_dma_probe(struct platform_device *pdev)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302552{
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302553 int (*clk_init)(struct platform_device *, struct clk **, struct clk **,
2554 struct clk **, struct clk **, struct clk **)
2555 = axivdma_clk_init;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302556 struct device_node *node = pdev->dev.of_node;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302557 struct xilinx_dma_device *xdev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302558 struct device_node *child, *np = pdev->dev.of_node;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302559 struct resource *io;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302560 u32 num_frames, addr_width;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302561 int i, err;
2562
2563 /* Allocate and initialize the DMA engine structure */
2564 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
2565 if (!xdev)
2566 return -ENOMEM;
2567
2568 xdev->dev = &pdev->dev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302569 if (np) {
2570 const struct of_device_id *match;
2571
2572 match = of_match_node(xilinx_dma_of_ids, np);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302573 if (match && match->data) {
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302574 xdev->dma_config = match->data;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302575 clk_init = xdev->dma_config->clk_init;
2576 }
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302577 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302578
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302579 err = clk_init(pdev, &xdev->axi_clk, &xdev->tx_clk, &xdev->txs_clk,
2580 &xdev->rx_clk, &xdev->rxs_clk);
2581 if (err)
2582 return err;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302583
2584 /* Request and map I/O memory */
2585 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2586 xdev->regs = devm_ioremap_resource(&pdev->dev, io);
2587 if (IS_ERR(xdev->regs))
2588 return PTR_ERR(xdev->regs);
2589
2590 /* Retrieve the DMA engine properties from the device tree */
2591 xdev->has_sg = of_property_read_bool(node, "xlnx,include-sg");
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302592 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA)
2593 xdev->mcdma = of_property_read_bool(node, "xlnx,mcdma");
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302594
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302595 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302596 err = of_property_read_u32(node, "xlnx,num-fstores",
2597 &num_frames);
2598 if (err < 0) {
2599 dev_err(xdev->dev,
2600 "missing xlnx,num-fstores property\n");
2601 return err;
2602 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302603
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302604 err = of_property_read_u32(node, "xlnx,flush-fsync",
2605 &xdev->flush_on_fsync);
2606 if (err < 0)
2607 dev_warn(xdev->dev,
2608 "missing xlnx,flush-fsync property\n");
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302609 }
2610
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302611 err = of_property_read_u32(node, "xlnx,addrwidth", &addr_width);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302612 if (err < 0)
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302613 dev_warn(xdev->dev, "missing xlnx,addrwidth property\n");
2614
2615 if (addr_width > 32)
2616 xdev->ext_addr = true;
2617 else
2618 xdev->ext_addr = false;
2619
2620 /* Set the dma mask bits */
2621 dma_set_mask(xdev->dev, DMA_BIT_MASK(addr_width));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302622
2623 /* Initialize the DMA engine */
2624 xdev->common.dev = &pdev->dev;
2625
2626 INIT_LIST_HEAD(&xdev->common.channels);
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302627 if (!(xdev->dma_config->dmatype == XDMA_TYPE_CDMA)) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302628 dma_cap_set(DMA_SLAVE, xdev->common.cap_mask);
2629 dma_cap_set(DMA_PRIVATE, xdev->common.cap_mask);
2630 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302631
2632 xdev->common.device_alloc_chan_resources =
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302633 xilinx_dma_alloc_chan_resources;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302634 xdev->common.device_free_chan_resources =
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302635 xilinx_dma_free_chan_resources;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302636 xdev->common.device_terminate_all = xilinx_dma_terminate_all;
2637 xdev->common.device_tx_status = xilinx_dma_tx_status;
2638 xdev->common.device_issue_pending = xilinx_dma_issue_pending;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302639 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302640 dma_cap_set(DMA_CYCLIC, xdev->common.cap_mask);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302641 xdev->common.device_prep_slave_sg = xilinx_dma_prep_slave_sg;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302642 xdev->common.device_prep_dma_cyclic =
2643 xilinx_dma_prep_dma_cyclic;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302644 xdev->common.device_prep_interleaved_dma =
2645 xilinx_dma_prep_interleaved;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302646 /* Residue calculation is supported by only AXI DMA */
2647 xdev->common.residue_granularity =
2648 DMA_RESIDUE_GRANULARITY_SEGMENT;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302649 } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302650 dma_cap_set(DMA_MEMCPY, xdev->common.cap_mask);
2651 xdev->common.device_prep_dma_memcpy = xilinx_cdma_prep_memcpy;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302652 } else {
2653 xdev->common.device_prep_interleaved_dma =
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302654 xilinx_vdma_dma_prep_interleaved;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302655 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302656
2657 platform_set_drvdata(pdev, xdev);
2658
2659 /* Initialize the channels */
2660 for_each_child_of_node(node, child) {
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302661 err = xilinx_dma_child_probe(xdev, child);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302662 if (err < 0)
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302663 goto disable_clks;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302664 }
2665
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302666 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302667 for (i = 0; i < xdev->nr_channels; i++)
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302668 if (xdev->chan[i])
2669 xdev->chan[i]->num_frms = num_frames;
2670 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302671
2672 /* Register the DMA engine with the core */
2673 dma_async_device_register(&xdev->common);
2674
2675 err = of_dma_controller_register(node, of_dma_xilinx_xlate,
2676 xdev);
2677 if (err < 0) {
2678 dev_err(&pdev->dev, "Unable to register DMA to DT\n");
2679 dma_async_device_unregister(&xdev->common);
2680 goto error;
2681 }
2682
2683 dev_info(&pdev->dev, "Xilinx AXI VDMA Engine Driver Probed!!\n");
2684
2685 return 0;
2686
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302687disable_clks:
2688 xdma_disable_allclks(xdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302689error:
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302690 for (i = 0; i < xdev->nr_channels; i++)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302691 if (xdev->chan[i])
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302692 xilinx_dma_chan_remove(xdev->chan[i]);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302693
2694 return err;
2695}
2696
2697/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302698 * xilinx_dma_remove - Driver remove function
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302699 * @pdev: Pointer to the platform_device structure
2700 *
2701 * Return: Always '0'
2702 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302703static int xilinx_dma_remove(struct platform_device *pdev)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302704{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302705 struct xilinx_dma_device *xdev = platform_get_drvdata(pdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302706 int i;
2707
2708 of_dma_controller_free(pdev->dev.of_node);
2709
2710 dma_async_device_unregister(&xdev->common);
2711
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302712 for (i = 0; i < xdev->nr_channels; i++)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302713 if (xdev->chan[i])
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302714 xilinx_dma_chan_remove(xdev->chan[i]);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302715
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302716 xdma_disable_allclks(xdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302717
2718 return 0;
2719}
2720
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302721static struct platform_driver xilinx_vdma_driver = {
2722 .driver = {
2723 .name = "xilinx-vdma",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302724 .of_match_table = xilinx_dma_of_ids,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302725 },
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302726 .probe = xilinx_dma_probe,
2727 .remove = xilinx_dma_remove,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302728};
2729
2730module_platform_driver(xilinx_vdma_driver);
2731
2732MODULE_AUTHOR("Xilinx, Inc.");
2733MODULE_DESCRIPTION("Xilinx VDMA driver");
2734MODULE_LICENSE("GPL v2");