blob: cd271f78260516483aa792d4a7a621c3afbdc09f [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)
Radhey Shyam Pandey35614ac2019-09-26 16:20:58 +053075#define XILINX_DMA_DMACR_DELAY_MASK GENMASK(31, 24)
76#define XILINX_DMA_DMACR_FRAME_COUNT_MASK GENMASK(23, 16)
77#define XILINX_DMA_DMACR_MASTER_MASK GENMASK(11, 8)
Srikanth Thokala9cd43602014-04-23 20:23:26 +053078
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +053079#define XILINX_DMA_REG_DMASR 0x0004
80#define XILINX_DMA_DMASR_EOL_LATE_ERR BIT(15)
81#define XILINX_DMA_DMASR_ERR_IRQ BIT(14)
82#define XILINX_DMA_DMASR_DLY_CNT_IRQ BIT(13)
83#define XILINX_DMA_DMASR_FRM_CNT_IRQ BIT(12)
84#define XILINX_DMA_DMASR_SOF_LATE_ERR BIT(11)
85#define XILINX_DMA_DMASR_SG_DEC_ERR BIT(10)
86#define XILINX_DMA_DMASR_SG_SLV_ERR BIT(9)
87#define XILINX_DMA_DMASR_EOF_EARLY_ERR BIT(8)
88#define XILINX_DMA_DMASR_SOF_EARLY_ERR BIT(7)
89#define XILINX_DMA_DMASR_DMA_DEC_ERR BIT(6)
90#define XILINX_DMA_DMASR_DMA_SLAVE_ERR BIT(5)
91#define XILINX_DMA_DMASR_DMA_INT_ERR BIT(4)
92#define XILINX_DMA_DMASR_IDLE BIT(1)
93#define XILINX_DMA_DMASR_HALTED BIT(0)
94#define XILINX_DMA_DMASR_DELAY_MASK GENMASK(31, 24)
95#define XILINX_DMA_DMASR_FRAME_COUNT_MASK GENMASK(23, 16)
Srikanth Thokala9cd43602014-04-23 20:23:26 +053096
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +053097#define XILINX_DMA_REG_CURDESC 0x0008
98#define XILINX_DMA_REG_TAILDESC 0x0010
99#define XILINX_DMA_REG_REG_INDEX 0x0014
100#define XILINX_DMA_REG_FRMSTORE 0x0018
101#define XILINX_DMA_REG_THRESHOLD 0x001c
102#define XILINX_DMA_REG_FRMPTR_STS 0x0024
103#define XILINX_DMA_REG_PARK_PTR 0x0028
104#define XILINX_DMA_PARK_PTR_WR_REF_SHIFT 8
105#define XILINX_DMA_PARK_PTR_RD_REF_SHIFT 0
106#define XILINX_DMA_REG_VDMA_VERSION 0x002c
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530107
108/* Register Direct Mode Registers */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530109#define XILINX_DMA_REG_VSIZE 0x0000
110#define XILINX_DMA_REG_HSIZE 0x0004
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530111
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530112#define XILINX_DMA_REG_FRMDLY_STRIDE 0x0008
113#define XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT 24
114#define XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT 0
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530115
116#define XILINX_VDMA_REG_START_ADDRESS(n) (0x000c + 4 * (n))
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530117#define XILINX_VDMA_REG_START_ADDRESS_64(n) (0x000c + 8 * (n))
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530118
119/* HW specific definitions */
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530120#define XILINX_DMA_MAX_CHANS_PER_DEVICE 0x20
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530121
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530122#define XILINX_DMA_DMAXR_ALL_IRQ_MASK \
123 (XILINX_DMA_DMASR_FRM_CNT_IRQ | \
124 XILINX_DMA_DMASR_DLY_CNT_IRQ | \
125 XILINX_DMA_DMASR_ERR_IRQ)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530126
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530127#define XILINX_DMA_DMASR_ALL_ERR_MASK \
128 (XILINX_DMA_DMASR_EOL_LATE_ERR | \
129 XILINX_DMA_DMASR_SOF_LATE_ERR | \
130 XILINX_DMA_DMASR_SG_DEC_ERR | \
131 XILINX_DMA_DMASR_SG_SLV_ERR | \
132 XILINX_DMA_DMASR_EOF_EARLY_ERR | \
133 XILINX_DMA_DMASR_SOF_EARLY_ERR | \
134 XILINX_DMA_DMASR_DMA_DEC_ERR | \
135 XILINX_DMA_DMASR_DMA_SLAVE_ERR | \
136 XILINX_DMA_DMASR_DMA_INT_ERR)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530137
138/*
139 * Recoverable errors are DMA Internal error, SOF Early, EOF Early
140 * and SOF Late. They are only recoverable when C_FLUSH_ON_FSYNC
141 * is enabled in the h/w system.
142 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530143#define XILINX_DMA_DMASR_ERR_RECOVER_MASK \
144 (XILINX_DMA_DMASR_SOF_LATE_ERR | \
145 XILINX_DMA_DMASR_EOF_EARLY_ERR | \
146 XILINX_DMA_DMASR_SOF_EARLY_ERR | \
147 XILINX_DMA_DMASR_DMA_INT_ERR)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530148
149/* Axi VDMA Flush on Fsync bits */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530150#define XILINX_DMA_FLUSH_S2MM 3
151#define XILINX_DMA_FLUSH_MM2S 2
152#define XILINX_DMA_FLUSH_BOTH 1
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530153
154/* Delay loop counter to prevent hardware failure */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530155#define XILINX_DMA_LOOP_COUNT 1000000
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530156
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530157/* AXI DMA Specific Registers/Offsets */
158#define XILINX_DMA_REG_SRCDSTADDR 0x18
159#define XILINX_DMA_REG_BTT 0x28
160
161/* AXI DMA Specific Masks/Bit fields */
162#define XILINX_DMA_MAX_TRANS_LEN GENMASK(22, 0)
163#define XILINX_DMA_CR_COALESCE_MAX GENMASK(23, 16)
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530164#define XILINX_DMA_CR_CYCLIC_BD_EN_MASK BIT(4)
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530165#define XILINX_DMA_CR_COALESCE_SHIFT 16
166#define XILINX_DMA_BD_SOP BIT(27)
167#define XILINX_DMA_BD_EOP BIT(26)
168#define XILINX_DMA_COALESCE_MAX 255
169#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
217 * @pad1: Reserved @0x10
218 * @pad2: Reserved @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 Appana9791e712016-06-07 19:21:16 +0530238 * @next_descmsb: Next Descriptor Pointer MSB @0x04
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530239 * @src_addr: Source address @0x08
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530240 * @src_addrmsb: Source address MSB @0x0C
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530241 * @dest_addr: Destination address @0x10
Kedareswara rao Appana9791e712016-06-07 19:21:16 +0530242 * @dest_addrmsb: 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
316 * @common: DMA common channel
317 * @desc_pool: Descriptors pool
318 * @dev: The dma device
319 * @irq: Channel IRQ
320 * @id: Channel ID
321 * @direction: Transfer direction
322 * @num_frms: Number of frames
323 * @has_sg: Support scatter transfers
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530324 * @cyclic: Check for cyclic transfers.
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530325 * @genlock: Support genlock mode
326 * @err: Channel has errors
327 * @tasklet: Cleanup work after irq
328 * @config: Device configuration info
329 * @flush_on_fsync: Flush on Frame sync
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530330 * @desc_pendingcount: Descriptor pending count
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530331 * @ext_addr: Indicates 64 bit addressing is supported by dma channel
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +0530332 * @desc_submitcount: Descriptor h/w submitted count
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530333 * @residue: Residue for AXI DMA
334 * @seg_v: Statically allocated segments base
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530335 * @cyclic_seg_v: Statically allocated segment base for cyclic transfers
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530336 * @start_transfer: Differentiate b/w DMA IP's transfer
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530337 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530338struct xilinx_dma_chan {
339 struct xilinx_dma_device *xdev;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530340 u32 ctrl_offset;
341 u32 desc_offset;
342 spinlock_t lock;
343 struct list_head pending_list;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530344 struct list_head active_list;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530345 struct list_head done_list;
346 struct dma_chan common;
347 struct dma_pool *desc_pool;
348 struct device *dev;
349 int irq;
350 int id;
351 enum dma_transfer_direction direction;
352 int num_frms;
353 bool has_sg;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530354 bool cyclic;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530355 bool genlock;
356 bool err;
357 struct tasklet_struct tasklet;
358 struct xilinx_vdma_config config;
359 bool flush_on_fsync;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530360 u32 desc_pendingcount;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530361 bool ext_addr;
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +0530362 u32 desc_submitcount;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530363 u32 residue;
364 struct xilinx_axidma_tx_segment *seg_v;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530365 struct xilinx_axidma_tx_segment *cyclic_seg_v;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530366 void (*start_transfer)(struct xilinx_dma_chan *chan);
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530367 u16 tdest;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530368};
369
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530370struct xilinx_dma_config {
371 enum xdma_ip_type dmatype;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530372 int (*clk_init)(struct platform_device *pdev, struct clk **axi_clk,
373 struct clk **tx_clk, struct clk **txs_clk,
374 struct clk **rx_clk, struct clk **rxs_clk);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530375};
376
377/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530378 * struct xilinx_dma_device - DMA device structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530379 * @regs: I/O mapped base address
380 * @dev: Device Structure
381 * @common: DMA device structure
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530382 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530383 * @has_sg: Specifies whether Scatter-Gather is present or not
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530384 * @mcdma: Specifies whether Multi-Channel is present or not
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530385 * @flush_on_fsync: Flush on frame sync
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530386 * @ext_addr: Indicates 64 bit addressing is supported by dma device
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530387 * @pdev: Platform device structure pointer
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530388 * @dma_config: DMA config structure
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530389 * @axi_clk: DMA Axi4-lite interace clock
390 * @tx_clk: DMA mm2s clock
391 * @txs_clk: DMA mm2s stream clock
392 * @rx_clk: DMA s2mm clock
393 * @rxs_clk: DMA s2mm stream clock
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530394 * @nr_channels: Number of channels DMA device supports
395 * @chan_id: DMA channel identifier
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530396 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530397struct xilinx_dma_device {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530398 void __iomem *regs;
399 struct device *dev;
400 struct dma_device common;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530401 struct xilinx_dma_chan *chan[XILINX_DMA_MAX_CHANS_PER_DEVICE];
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530402 bool has_sg;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530403 bool mcdma;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530404 u32 flush_on_fsync;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530405 bool ext_addr;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530406 struct platform_device *pdev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530407 const struct xilinx_dma_config *dma_config;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +0530408 struct clk *axi_clk;
409 struct clk *tx_clk;
410 struct clk *txs_clk;
411 struct clk *rx_clk;
412 struct clk *rxs_clk;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +0530413 u32 nr_channels;
414 u32 chan_id;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530415};
416
417/* Macros */
418#define to_xilinx_chan(chan) \
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530419 container_of(chan, struct xilinx_dma_chan, common)
420#define to_dma_tx_descriptor(tx) \
421 container_of(tx, struct xilinx_dma_tx_descriptor, async_tx)
422#define xilinx_dma_poll_timeout(chan, reg, val, cond, delay_us, timeout_us) \
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530423 readl_poll_timeout(chan->xdev->regs + chan->ctrl_offset + reg, val, \
424 cond, delay_us, timeout_us)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530425
426/* IO accessors */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530427static inline u32 dma_read(struct xilinx_dma_chan *chan, u32 reg)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530428{
429 return ioread32(chan->xdev->regs + reg);
430}
431
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530432static inline void dma_write(struct xilinx_dma_chan *chan, u32 reg, u32 value)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530433{
434 iowrite32(value, chan->xdev->regs + reg);
435}
436
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530437static inline void vdma_desc_write(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530438 u32 value)
439{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530440 dma_write(chan, chan->desc_offset + reg, value);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530441}
442
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530443static inline u32 dma_ctrl_read(struct xilinx_dma_chan *chan, u32 reg)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530444{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530445 return dma_read(chan, chan->ctrl_offset + reg);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530446}
447
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530448static inline void dma_ctrl_write(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530449 u32 value)
450{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530451 dma_write(chan, chan->ctrl_offset + reg, value);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530452}
453
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530454static inline void dma_ctrl_clr(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530455 u32 clr)
456{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530457 dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) & ~clr);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530458}
459
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530460static inline void dma_ctrl_set(struct xilinx_dma_chan *chan, u32 reg,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530461 u32 set)
462{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530463 dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) | set);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530464}
465
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530466/**
467 * vdma_desc_write_64 - 64-bit descriptor write
468 * @chan: Driver specific VDMA channel
469 * @reg: Register to write
470 * @value_lsb: lower address of the descriptor.
471 * @value_msb: upper address of the descriptor.
472 *
473 * Since vdma driver is trying to write to a register offset which is not a
474 * multiple of 64 bits(ex : 0x5c), we are writing as two separate 32 bits
475 * instead of a single 64 bit register write.
476 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530477static inline void vdma_desc_write_64(struct xilinx_dma_chan *chan, u32 reg,
Kedareswara rao Appanab72db402016-04-06 10:38:08 +0530478 u32 value_lsb, u32 value_msb)
479{
480 /* Write the lsb 32 bits*/
481 writel(value_lsb, chan->xdev->regs + chan->desc_offset + reg);
482
483 /* Write the msb 32 bits */
484 writel(value_msb, chan->xdev->regs + chan->desc_offset + reg + 4);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530485}
486
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +0530487static inline void dma_writeq(struct xilinx_dma_chan *chan, u32 reg, u64 value)
488{
489 lo_hi_writeq(value, chan->xdev->regs + chan->ctrl_offset + reg);
490}
491
492static inline void xilinx_write(struct xilinx_dma_chan *chan, u32 reg,
493 dma_addr_t addr)
494{
495 if (chan->ext_addr)
496 dma_writeq(chan, reg, addr);
497 else
498 dma_ctrl_write(chan, reg, addr);
499}
500
501static inline void xilinx_axidma_buf(struct xilinx_dma_chan *chan,
502 struct xilinx_axidma_desc_hw *hw,
503 dma_addr_t buf_addr, size_t sg_used,
504 size_t period_len)
505{
506 if (chan->ext_addr) {
507 hw->buf_addr = lower_32_bits(buf_addr + sg_used + period_len);
508 hw->buf_addr_msb = upper_32_bits(buf_addr + sg_used +
509 period_len);
510 } else {
511 hw->buf_addr = buf_addr + sg_used + period_len;
512 }
513}
514
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530515/* -----------------------------------------------------------------------------
516 * Descriptors and segments alloc and free
517 */
518
519/**
520 * xilinx_vdma_alloc_tx_segment - Allocate transaction segment
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530521 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530522 *
523 * Return: The allocated segment on success and NULL on failure.
524 */
525static struct xilinx_vdma_tx_segment *
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530526xilinx_vdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530527{
528 struct xilinx_vdma_tx_segment *segment;
529 dma_addr_t phys;
530
Julia Lawall2ba4f8a2016-04-29 22:09:09 +0200531 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530532 if (!segment)
533 return NULL;
534
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530535 segment->phys = phys;
536
537 return segment;
538}
539
540/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530541 * xilinx_cdma_alloc_tx_segment - Allocate transaction segment
542 * @chan: Driver specific DMA channel
543 *
544 * Return: The allocated segment on success and NULL on failure.
545 */
546static struct xilinx_cdma_tx_segment *
547xilinx_cdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
548{
549 struct xilinx_cdma_tx_segment *segment;
550 dma_addr_t phys;
551
Kedareswara rao Appana62147862016-05-18 13:17:31 +0530552 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530553 if (!segment)
554 return NULL;
555
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530556 segment->phys = phys;
557
558 return segment;
559}
560
561/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530562 * xilinx_axidma_alloc_tx_segment - Allocate transaction segment
563 * @chan: Driver specific DMA channel
564 *
565 * Return: The allocated segment on success and NULL on failure.
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530566 */
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530567static struct xilinx_axidma_tx_segment *
568xilinx_axidma_alloc_tx_segment(struct xilinx_dma_chan *chan)
569{
570 struct xilinx_axidma_tx_segment *segment;
571 dma_addr_t phys;
572
Kedareswara rao Appana62147862016-05-18 13:17:31 +0530573 segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530574 if (!segment)
575 return NULL;
576
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530577 segment->phys = phys;
578
579 return segment;
580}
581
582/**
583 * xilinx_dma_free_tx_segment - Free transaction segment
584 * @chan: Driver specific DMA channel
585 * @segment: DMA transaction segment
586 */
587static void xilinx_dma_free_tx_segment(struct xilinx_dma_chan *chan,
588 struct xilinx_axidma_tx_segment *segment)
589{
590 dma_pool_free(chan->desc_pool, segment, segment->phys);
591}
592
593/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530594 * xilinx_cdma_free_tx_segment - Free transaction segment
595 * @chan: Driver specific DMA channel
596 * @segment: DMA transaction segment
597 */
598static void xilinx_cdma_free_tx_segment(struct xilinx_dma_chan *chan,
599 struct xilinx_cdma_tx_segment *segment)
600{
601 dma_pool_free(chan->desc_pool, segment, segment->phys);
602}
603
604/**
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530605 * xilinx_vdma_free_tx_segment - Free transaction segment
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530606 * @chan: Driver specific DMA channel
607 * @segment: DMA transaction segment
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530608 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530609static void xilinx_vdma_free_tx_segment(struct xilinx_dma_chan *chan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530610 struct xilinx_vdma_tx_segment *segment)
611{
612 dma_pool_free(chan->desc_pool, segment, segment->phys);
613}
614
615/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530616 * xilinx_dma_tx_descriptor - Allocate transaction descriptor
617 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530618 *
619 * Return: The allocated descriptor on success and NULL on failure.
620 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530621static struct xilinx_dma_tx_descriptor *
622xilinx_dma_alloc_tx_descriptor(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530623{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530624 struct xilinx_dma_tx_descriptor *desc;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530625
626 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
627 if (!desc)
628 return NULL;
629
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530630 INIT_LIST_HEAD(&desc->segments);
631
632 return desc;
633}
634
635/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530636 * xilinx_dma_free_tx_descriptor - Free transaction descriptor
637 * @chan: Driver specific DMA channel
638 * @desc: DMA transaction descriptor
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530639 */
640static void
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530641xilinx_dma_free_tx_descriptor(struct xilinx_dma_chan *chan,
642 struct xilinx_dma_tx_descriptor *desc)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530643{
644 struct xilinx_vdma_tx_segment *segment, *next;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530645 struct xilinx_cdma_tx_segment *cdma_segment, *cdma_next;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530646 struct xilinx_axidma_tx_segment *axidma_segment, *axidma_next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530647
648 if (!desc)
649 return;
650
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530651 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530652 list_for_each_entry_safe(segment, next, &desc->segments, node) {
653 list_del(&segment->node);
654 xilinx_vdma_free_tx_segment(chan, segment);
655 }
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530656 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530657 list_for_each_entry_safe(cdma_segment, cdma_next,
658 &desc->segments, node) {
659 list_del(&cdma_segment->node);
660 xilinx_cdma_free_tx_segment(chan, cdma_segment);
661 }
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530662 } else {
663 list_for_each_entry_safe(axidma_segment, axidma_next,
664 &desc->segments, node) {
665 list_del(&axidma_segment->node);
666 xilinx_dma_free_tx_segment(chan, axidma_segment);
667 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530668 }
669
670 kfree(desc);
671}
672
673/* Required functions */
674
675/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530676 * xilinx_dma_free_desc_list - Free descriptors list
677 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530678 * @list: List to parse and delete the descriptor
679 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530680static void xilinx_dma_free_desc_list(struct xilinx_dma_chan *chan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530681 struct list_head *list)
682{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530683 struct xilinx_dma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530684
685 list_for_each_entry_safe(desc, next, list, node) {
686 list_del(&desc->node);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530687 xilinx_dma_free_tx_descriptor(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530688 }
689}
690
691/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530692 * xilinx_dma_free_descriptors - Free channel descriptors
693 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530694 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530695static void xilinx_dma_free_descriptors(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530696{
697 unsigned long flags;
698
699 spin_lock_irqsave(&chan->lock, flags);
700
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530701 xilinx_dma_free_desc_list(chan, &chan->pending_list);
702 xilinx_dma_free_desc_list(chan, &chan->done_list);
703 xilinx_dma_free_desc_list(chan, &chan->active_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530704
705 spin_unlock_irqrestore(&chan->lock, flags);
706}
707
708/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530709 * xilinx_dma_free_chan_resources - Free channel resources
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530710 * @dchan: DMA channel
711 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530712static void xilinx_dma_free_chan_resources(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530713{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530714 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530715
716 dev_dbg(chan->dev, "Free all channel resources.\n");
717
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530718 xilinx_dma_free_descriptors(chan);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530719 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
720 xilinx_dma_free_tx_segment(chan, chan->cyclic_seg_v);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530721 xilinx_dma_free_tx_segment(chan, chan->seg_v);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530722 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530723 dma_pool_destroy(chan->desc_pool);
724 chan->desc_pool = NULL;
725}
726
727/**
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530728 * xilinx_dma_chan_handle_cyclic - Cyclic dma callback
729 * @chan: Driver specific dma channel
730 * @desc: dma transaction descriptor
731 * @flags: flags for spin lock
732 */
733static void xilinx_dma_chan_handle_cyclic(struct xilinx_dma_chan *chan,
734 struct xilinx_dma_tx_descriptor *desc,
735 unsigned long *flags)
736{
737 dma_async_tx_callback callback;
738 void *callback_param;
739
740 callback = desc->async_tx.callback;
741 callback_param = desc->async_tx.callback_param;
742 if (callback) {
743 spin_unlock_irqrestore(&chan->lock, *flags);
744 callback(callback_param);
745 spin_lock_irqsave(&chan->lock, *flags);
746 }
747}
748
749/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530750 * xilinx_dma_chan_desc_cleanup - Clean channel descriptors
751 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530752 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530753static void xilinx_dma_chan_desc_cleanup(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530754{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530755 struct xilinx_dma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530756 unsigned long flags;
757
758 spin_lock_irqsave(&chan->lock, flags);
759
760 list_for_each_entry_safe(desc, next, &chan->done_list, node) {
Vinod Koul369dbad2016-08-04 18:06:13 +0530761 struct dmaengine_desc_callback cb;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530762
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530763 if (desc->cyclic) {
764 xilinx_dma_chan_handle_cyclic(chan, desc, &flags);
765 break;
766 }
767
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530768 /* Remove from the list of running transactions */
769 list_del(&desc->node);
770
771 /* Run the link descriptor callback function */
Vinod Koul369dbad2016-08-04 18:06:13 +0530772 dmaengine_desc_get_callback(&desc->async_tx, &cb);
773 if (dmaengine_desc_callback_valid(&cb)) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530774 spin_unlock_irqrestore(&chan->lock, flags);
Vinod Koul369dbad2016-08-04 18:06:13 +0530775 dmaengine_desc_callback_invoke(&cb, NULL);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530776 spin_lock_irqsave(&chan->lock, flags);
777 }
778
779 /* Run any dependencies, then free the descriptor */
780 dma_run_dependencies(&desc->async_tx);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530781 xilinx_dma_free_tx_descriptor(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530782 }
783
784 spin_unlock_irqrestore(&chan->lock, flags);
785}
786
787/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530788 * xilinx_dma_do_tasklet - Schedule completion tasklet
789 * @data: Pointer to the Xilinx DMA channel structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530790 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530791static void xilinx_dma_do_tasklet(unsigned long data)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530792{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530793 struct xilinx_dma_chan *chan = (struct xilinx_dma_chan *)data;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530794
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530795 xilinx_dma_chan_desc_cleanup(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530796}
797
798/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530799 * xilinx_dma_alloc_chan_resources - Allocate channel resources
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530800 * @dchan: DMA channel
801 *
802 * Return: '0' on success and failure value on error
803 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530804static int xilinx_dma_alloc_chan_resources(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530805{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530806 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530807
808 /* Has this channel already been allocated? */
809 if (chan->desc_pool)
810 return 0;
811
812 /*
813 * We need the descriptor to be aligned to 64bytes
814 * for meeting Xilinx VDMA specification requirement.
815 */
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530816 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530817 chan->desc_pool = dma_pool_create("xilinx_dma_desc_pool",
818 chan->dev,
819 sizeof(struct xilinx_axidma_tx_segment),
820 __alignof__(struct xilinx_axidma_tx_segment),
821 0);
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530822 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530823 chan->desc_pool = dma_pool_create("xilinx_cdma_desc_pool",
824 chan->dev,
825 sizeof(struct xilinx_cdma_tx_segment),
826 __alignof__(struct xilinx_cdma_tx_segment),
827 0);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530828 } else {
829 chan->desc_pool = dma_pool_create("xilinx_vdma_desc_pool",
830 chan->dev,
831 sizeof(struct xilinx_vdma_tx_segment),
832 __alignof__(struct xilinx_vdma_tx_segment),
833 0);
834 }
835
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530836 if (!chan->desc_pool) {
837 dev_err(chan->dev,
838 "unable to allocate channel %d descriptor pool\n",
839 chan->id);
840 return -ENOMEM;
841 }
842
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530843 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530844 /*
845 * For AXI DMA case after submitting a pending_list, keep
846 * an extra segment allocated so that the "next descriptor"
847 * pointer on the tail descriptor always points to a
848 * valid descriptor, even when paused after reaching taildesc.
849 * This way, it is possible to issue additional
850 * transfers without halting and restarting the channel.
851 */
852 chan->seg_v = xilinx_axidma_alloc_tx_segment(chan);
853
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +0530854 /*
855 * For cyclic DMA mode we need to program the tail Descriptor
856 * register with a value which is not a part of the BD chain
857 * so allocating a desc segment during channel allocation for
858 * programming tail descriptor.
859 */
860 chan->cyclic_seg_v = xilinx_axidma_alloc_tx_segment(chan);
861 }
862
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530863 dma_cookie_init(dchan);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530864
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530865 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530866 /* For AXI DMA resetting once channel will reset the
867 * other channel as well so enable the interrupts here.
868 */
869 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
870 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
871 }
872
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530873 if ((chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) && chan->has_sg)
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +0530874 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
875 XILINX_CDMA_CR_SGMODE);
876
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530877 return 0;
878}
879
880/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530881 * xilinx_dma_tx_status - Get DMA transaction status
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530882 * @dchan: DMA channel
883 * @cookie: Transaction identifier
884 * @txstate: Transaction state
885 *
886 * Return: DMA transaction status
887 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530888static enum dma_status xilinx_dma_tx_status(struct dma_chan *dchan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530889 dma_cookie_t cookie,
890 struct dma_tx_state *txstate)
891{
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530892 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
893 struct xilinx_dma_tx_descriptor *desc;
894 struct xilinx_axidma_tx_segment *segment;
895 struct xilinx_axidma_desc_hw *hw;
896 enum dma_status ret;
897 unsigned long flags;
898 u32 residue = 0;
899
900 ret = dma_cookie_status(dchan, cookie, txstate);
901 if (ret == DMA_COMPLETE || !txstate)
902 return ret;
903
Kedareswara rao Appanafb236662016-05-13 12:33:29 +0530904 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +0530905 spin_lock_irqsave(&chan->lock, flags);
906
907 desc = list_last_entry(&chan->active_list,
908 struct xilinx_dma_tx_descriptor, node);
909 if (chan->has_sg) {
910 list_for_each_entry(segment, &desc->segments, node) {
911 hw = &segment->hw;
912 residue += (hw->control - hw->status) &
913 XILINX_DMA_MAX_TRANS_LEN;
914 }
915 }
916 spin_unlock_irqrestore(&chan->lock, flags);
917
918 chan->residue = residue;
919 dma_set_residue(txstate, chan->residue);
920 }
921
922 return ret;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530923}
924
925/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530926 * xilinx_dma_is_running - Check if DMA channel is running
927 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530928 *
929 * Return: '1' if running, '0' if not.
930 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530931static bool xilinx_dma_is_running(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530932{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530933 return !(dma_ctrl_read(chan, XILINX_DMA_REG_DMASR) &
934 XILINX_DMA_DMASR_HALTED) &&
935 (dma_ctrl_read(chan, XILINX_DMA_REG_DMACR) &
936 XILINX_DMA_DMACR_RUNSTOP);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530937}
938
939/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530940 * xilinx_dma_is_idle - Check if DMA channel is idle
941 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530942 *
943 * Return: '1' if idle, '0' if not.
944 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530945static bool xilinx_dma_is_idle(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530946{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530947 return dma_ctrl_read(chan, XILINX_DMA_REG_DMASR) &
948 XILINX_DMA_DMASR_IDLE;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530949}
950
951/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530952 * xilinx_dma_halt - Halt DMA channel
953 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530954 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530955static void xilinx_dma_halt(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530956{
Kedareswara rao Appana69490632016-03-03 23:02:42 +0530957 int err;
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530958 u32 val;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530959
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530960 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530961
962 /* Wait for the hardware to halt */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530963 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
964 (val & XILINX_DMA_DMASR_HALTED), 0,
965 XILINX_DMA_LOOP_COUNT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530966
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530967 if (err) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530968 dev_err(chan->dev, "Cannot stop channel %p: %x\n",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530969 chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530970 chan->err = true;
971 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530972}
973
974/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530975 * xilinx_dma_start - Start DMA channel
976 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530977 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530978static void xilinx_dma_start(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530979{
Kedareswara rao Appana69490632016-03-03 23:02:42 +0530980 int err;
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530981 u32 val;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530982
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530983 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530984
985 /* Wait for the hardware to start */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530986 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
987 !(val & XILINX_DMA_DMASR_HALTED), 0,
988 XILINX_DMA_LOOP_COUNT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530989
Kedareswara rao Appana9495f262016-02-26 19:33:54 +0530990 if (err) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530991 dev_err(chan->dev, "Cannot start channel %p: %x\n",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +0530992 chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530993
994 chan->err = true;
995 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530996}
997
998/**
999 * xilinx_vdma_start_transfer - Starts VDMA transfer
1000 * @chan: Driver specific channel struct pointer
1001 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301002static void xilinx_vdma_start_transfer(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301003{
1004 struct xilinx_vdma_config *config = &chan->config;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301005 struct xilinx_dma_tx_descriptor *desc, *tail_desc;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301006 u32 reg;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301007 struct xilinx_vdma_tx_segment *tail_segment;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301008
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301009 /* This function was invoked with lock held */
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301010 if (chan->err)
1011 return;
1012
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301013 if (list_empty(&chan->pending_list))
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301014 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301015
1016 desc = list_first_entry(&chan->pending_list,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301017 struct xilinx_dma_tx_descriptor, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301018 tail_desc = list_last_entry(&chan->pending_list,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301019 struct xilinx_dma_tx_descriptor, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301020
1021 tail_segment = list_last_entry(&tail_desc->segments,
1022 struct xilinx_vdma_tx_segment, node);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301023
1024 /* If it is SG mode and hardware is busy, cannot submit */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301025 if (chan->has_sg && xilinx_dma_is_running(chan) &&
1026 !xilinx_dma_is_idle(chan)) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301027 dev_dbg(chan->dev, "DMA controller still busy\n");
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301028 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301029 }
1030
1031 /*
1032 * If hardware is idle, then all descriptors on the running lists are
1033 * done, start new transfers
1034 */
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301035 if (chan->has_sg)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301036 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301037 desc->async_tx.phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301038
1039 /* Configure the hardware using info in the config structure */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301040 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301041
1042 if (config->frm_cnt_en)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301043 reg |= XILINX_DMA_DMACR_FRAMECNT_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301044 else
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301045 reg &= ~XILINX_DMA_DMACR_FRAMECNT_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301046
Kedareswara rao Appanae2b538a2016-02-26 19:33:53 +05301047 /* Configure channel to allow number frame buffers */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301048 dma_ctrl_write(chan, XILINX_DMA_REG_FRMSTORE,
Kedareswara rao Appanae2b538a2016-02-26 19:33:53 +05301049 chan->desc_pendingcount);
1050
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301051 /*
1052 * With SG, start with circular mode, so that BDs can be fetched.
1053 * In direct register mode, if not parking, enable circular mode
1054 */
1055 if (chan->has_sg || !config->park)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301056 reg |= XILINX_DMA_DMACR_CIRC_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301057
1058 if (config->park)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301059 reg &= ~XILINX_DMA_DMACR_CIRC_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301060
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301061 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301062
1063 if (config->park && (config->park_frm >= 0) &&
1064 (config->park_frm < chan->num_frms)) {
1065 if (chan->direction == DMA_MEM_TO_DEV)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301066 dma_write(chan, XILINX_DMA_REG_PARK_PTR,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301067 config->park_frm <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301068 XILINX_DMA_PARK_PTR_RD_REF_SHIFT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301069 else
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301070 dma_write(chan, XILINX_DMA_REG_PARK_PTR,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301071 config->park_frm <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301072 XILINX_DMA_PARK_PTR_WR_REF_SHIFT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301073 }
1074
1075 /* Start the hardware */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301076 xilinx_dma_start(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301077
1078 if (chan->err)
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301079 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301080
1081 /* Start the transfer */
1082 if (chan->has_sg) {
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301083 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301084 tail_segment->phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301085 } else {
1086 struct xilinx_vdma_tx_segment *segment, *last = NULL;
1087 int i = 0;
1088
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +05301089 if (chan->desc_submitcount < chan->num_frms)
1090 i = chan->desc_submitcount;
1091
1092 list_for_each_entry(segment, &desc->segments, node) {
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05301093 if (chan->ext_addr)
1094 vdma_desc_write_64(chan,
1095 XILINX_VDMA_REG_START_ADDRESS_64(i++),
1096 segment->hw.buf_addr,
1097 segment->hw.buf_addr_msb);
1098 else
1099 vdma_desc_write(chan,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301100 XILINX_VDMA_REG_START_ADDRESS(i++),
1101 segment->hw.buf_addr);
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05301102
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301103 last = segment;
1104 }
1105
1106 if (!last)
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301107 return;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301108
1109 /* HW expects these parameters to be same for one transaction */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301110 vdma_desc_write(chan, XILINX_DMA_REG_HSIZE, last->hw.hsize);
1111 vdma_desc_write(chan, XILINX_DMA_REG_FRMDLY_STRIDE,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301112 last->hw.stride);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301113 vdma_desc_write(chan, XILINX_DMA_REG_VSIZE, last->hw.vsize);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301114 }
1115
Kedareswara rao Appanaa65cf5122016-04-06 10:38:09 +05301116 if (!chan->has_sg) {
1117 list_del(&desc->node);
1118 list_add_tail(&desc->node, &chan->active_list);
1119 chan->desc_submitcount++;
1120 chan->desc_pendingcount--;
1121 if (chan->desc_submitcount == chan->num_frms)
1122 chan->desc_submitcount = 0;
1123 } else {
1124 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1125 chan->desc_pendingcount = 0;
1126 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301127}
1128
1129/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301130 * xilinx_cdma_start_transfer - Starts cdma transfer
1131 * @chan: Driver specific channel struct pointer
1132 */
1133static void xilinx_cdma_start_transfer(struct xilinx_dma_chan *chan)
1134{
1135 struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1136 struct xilinx_cdma_tx_segment *tail_segment;
1137 u32 ctrl_reg = dma_read(chan, XILINX_DMA_REG_DMACR);
1138
1139 if (chan->err)
1140 return;
1141
1142 if (list_empty(&chan->pending_list))
1143 return;
1144
1145 head_desc = list_first_entry(&chan->pending_list,
1146 struct xilinx_dma_tx_descriptor, node);
1147 tail_desc = list_last_entry(&chan->pending_list,
1148 struct xilinx_dma_tx_descriptor, node);
1149 tail_segment = list_last_entry(&tail_desc->segments,
1150 struct xilinx_cdma_tx_segment, node);
1151
1152 if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1153 ctrl_reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1154 ctrl_reg |= chan->desc_pendingcount <<
1155 XILINX_DMA_CR_COALESCE_SHIFT;
1156 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, ctrl_reg);
1157 }
1158
1159 if (chan->has_sg) {
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301160 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1161 head_desc->async_tx.phys);
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301162
1163 /* Update tail ptr register which will start the transfer */
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301164 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1165 tail_segment->phys);
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301166 } else {
1167 /* In simple mode */
1168 struct xilinx_cdma_tx_segment *segment;
1169 struct xilinx_cdma_desc_hw *hw;
1170
1171 segment = list_first_entry(&head_desc->segments,
1172 struct xilinx_cdma_tx_segment,
1173 node);
1174
1175 hw = &segment->hw;
1176
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301177 xilinx_write(chan, XILINX_CDMA_REG_SRCADDR, hw->src_addr);
1178 xilinx_write(chan, XILINX_CDMA_REG_DSTADDR, hw->dest_addr);
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301179
1180 /* Start the transfer */
1181 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1182 hw->control & XILINX_DMA_MAX_TRANS_LEN);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301183 }
1184
1185 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1186 chan->desc_pendingcount = 0;
1187}
1188
1189/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301190 * xilinx_dma_start_transfer - Starts DMA transfer
1191 * @chan: Driver specific channel struct pointer
1192 */
1193static void xilinx_dma_start_transfer(struct xilinx_dma_chan *chan)
1194{
1195 struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
1196 struct xilinx_axidma_tx_segment *tail_segment, *old_head, *new_head;
1197 u32 reg;
1198
1199 if (chan->err)
1200 return;
1201
1202 if (list_empty(&chan->pending_list))
1203 return;
1204
1205 /* If it is SG mode and hardware is busy, cannot submit */
1206 if (chan->has_sg && xilinx_dma_is_running(chan) &&
1207 !xilinx_dma_is_idle(chan)) {
1208 dev_dbg(chan->dev, "DMA controller still busy\n");
1209 return;
1210 }
1211
1212 head_desc = list_first_entry(&chan->pending_list,
1213 struct xilinx_dma_tx_descriptor, node);
1214 tail_desc = list_last_entry(&chan->pending_list,
1215 struct xilinx_dma_tx_descriptor, node);
1216 tail_segment = list_last_entry(&tail_desc->segments,
1217 struct xilinx_axidma_tx_segment, node);
1218
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301219 if (chan->has_sg && !chan->xdev->mcdma) {
1220 old_head = list_first_entry(&head_desc->segments,
1221 struct xilinx_axidma_tx_segment, node);
1222 new_head = chan->seg_v;
1223 /* Copy Buffer Descriptor fields. */
1224 new_head->hw = old_head->hw;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301225
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301226 /* Swap and save new reserve */
1227 list_replace_init(&old_head->node, &new_head->node);
1228 chan->seg_v = old_head;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301229
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301230 tail_segment->hw.next_desc = chan->seg_v->phys;
1231 head_desc->async_tx.phys = new_head->phys;
1232 }
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301233
1234 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1235
1236 if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
1237 reg &= ~XILINX_DMA_CR_COALESCE_MAX;
1238 reg |= chan->desc_pendingcount <<
1239 XILINX_DMA_CR_COALESCE_SHIFT;
1240 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1241 }
1242
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301243 if (chan->has_sg && !chan->xdev->mcdma)
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301244 xilinx_write(chan, XILINX_DMA_REG_CURDESC,
1245 head_desc->async_tx.phys);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301246
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301247 if (chan->has_sg && chan->xdev->mcdma) {
1248 if (chan->direction == DMA_MEM_TO_DEV) {
1249 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
1250 head_desc->async_tx.phys);
1251 } else {
1252 if (!chan->tdest) {
1253 dma_ctrl_write(chan, XILINX_DMA_REG_CURDESC,
1254 head_desc->async_tx.phys);
1255 } else {
1256 dma_ctrl_write(chan,
1257 XILINX_DMA_MCRX_CDESC(chan->tdest),
1258 head_desc->async_tx.phys);
1259 }
1260 }
1261 }
1262
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301263 xilinx_dma_start(chan);
1264
1265 if (chan->err)
1266 return;
1267
1268 /* Start the transfer */
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301269 if (chan->has_sg && !chan->xdev->mcdma) {
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301270 if (chan->cyclic)
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301271 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1272 chan->cyclic_seg_v->phys);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301273 else
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301274 xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
1275 tail_segment->phys);
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301276 } else if (chan->has_sg && chan->xdev->mcdma) {
1277 if (chan->direction == DMA_MEM_TO_DEV) {
1278 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
1279 tail_segment->phys);
1280 } else {
1281 if (!chan->tdest) {
1282 dma_ctrl_write(chan, XILINX_DMA_REG_TAILDESC,
1283 tail_segment->phys);
1284 } else {
1285 dma_ctrl_write(chan,
1286 XILINX_DMA_MCRX_TDESC(chan->tdest),
1287 tail_segment->phys);
1288 }
1289 }
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301290 } else {
1291 struct xilinx_axidma_tx_segment *segment;
1292 struct xilinx_axidma_desc_hw *hw;
1293
1294 segment = list_first_entry(&head_desc->segments,
1295 struct xilinx_axidma_tx_segment,
1296 node);
1297 hw = &segment->hw;
1298
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301299 xilinx_write(chan, XILINX_DMA_REG_SRCDSTADDR, hw->buf_addr);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301300
1301 /* Start the transfer */
1302 dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
1303 hw->control & XILINX_DMA_MAX_TRANS_LEN);
1304 }
1305
1306 list_splice_tail_init(&chan->pending_list, &chan->active_list);
1307 chan->desc_pendingcount = 0;
1308}
1309
1310/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301311 * xilinx_dma_issue_pending - Issue pending transactions
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301312 * @dchan: DMA channel
1313 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301314static void xilinx_dma_issue_pending(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301315{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301316 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301317 unsigned long flags;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301318
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301319 spin_lock_irqsave(&chan->lock, flags);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301320 chan->start_transfer(chan);
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301321 spin_unlock_irqrestore(&chan->lock, flags);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301322}
1323
1324/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301325 * xilinx_dma_complete_descriptor - Mark the active descriptor as complete
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301326 * @chan : xilinx DMA channel
1327 *
1328 * CONTEXT: hardirq
1329 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301330static void xilinx_dma_complete_descriptor(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301331{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301332 struct xilinx_dma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301333
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301334 /* This function was invoked with lock held */
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301335 if (list_empty(&chan->active_list))
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301336 return;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301337
1338 list_for_each_entry_safe(desc, next, &chan->active_list, node) {
1339 list_del(&desc->node);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301340 if (!desc->cyclic)
1341 dma_cookie_complete(&desc->async_tx);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301342 list_add_tail(&desc->node, &chan->done_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301343 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301344}
1345
1346/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301347 * xilinx_dma_reset - Reset DMA channel
1348 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301349 *
1350 * Return: '0' on success and failure value on error
1351 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301352static int xilinx_dma_reset(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301353{
Kedareswara rao Appana69490632016-03-03 23:02:42 +05301354 int err;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301355 u32 tmp;
1356
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301357 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RESET);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301358
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301359 /* Wait for the hardware to finish reset */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301360 err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMACR, tmp,
1361 !(tmp & XILINX_DMA_DMACR_RESET), 0,
1362 XILINX_DMA_LOOP_COUNT);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301363
Kedareswara rao Appana9495f262016-02-26 19:33:54 +05301364 if (err) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301365 dev_err(chan->dev, "reset timeout, cr %x, sr %x\n",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301366 dma_ctrl_read(chan, XILINX_DMA_REG_DMACR),
1367 dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301368 return -ETIMEDOUT;
1369 }
1370
1371 chan->err = false;
1372
Kedareswara rao Appana9495f262016-02-26 19:33:54 +05301373 return err;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301374}
1375
1376/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301377 * xilinx_dma_chan_reset - Reset DMA channel and enable interrupts
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_chan_reset(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301383{
1384 int err;
1385
1386 /* Reset VDMA */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301387 err = xilinx_dma_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301388 if (err)
1389 return err;
1390
1391 /* Enable interrupts */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301392 dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
1393 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301394
1395 return 0;
1396}
1397
1398/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301399 * xilinx_dma_irq_handler - DMA Interrupt handler
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301400 * @irq: IRQ number
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301401 * @data: Pointer to the Xilinx DMA channel structure
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301402 *
1403 * Return: IRQ_HANDLED/IRQ_NONE
1404 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301405static irqreturn_t xilinx_dma_irq_handler(int irq, void *data)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301406{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301407 struct xilinx_dma_chan *chan = data;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301408 u32 status;
1409
1410 /* Read the status and ack the interrupts. */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301411 status = dma_ctrl_read(chan, XILINX_DMA_REG_DMASR);
1412 if (!(status & XILINX_DMA_DMAXR_ALL_IRQ_MASK))
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301413 return IRQ_NONE;
1414
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301415 dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1416 status & XILINX_DMA_DMAXR_ALL_IRQ_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301417
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301418 if (status & XILINX_DMA_DMASR_ERR_IRQ) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301419 /*
1420 * An error occurred. If C_FLUSH_ON_FSYNC is enabled and the
1421 * error is recoverable, ignore it. Otherwise flag the error.
1422 *
1423 * Only recoverable errors can be cleared in the DMASR register,
1424 * make sure not to write to other error bits to 1.
1425 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301426 u32 errors = status & XILINX_DMA_DMASR_ALL_ERR_MASK;
Kedareswara rao Appana48a59ed2016-04-06 10:44:55 +05301427
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301428 dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
1429 errors & XILINX_DMA_DMASR_ERR_RECOVER_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301430
1431 if (!chan->flush_on_fsync ||
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301432 (errors & ~XILINX_DMA_DMASR_ERR_RECOVER_MASK)) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301433 dev_err(chan->dev,
1434 "Channel %p has errors %x, cdr %x tdr %x\n",
1435 chan, errors,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301436 dma_ctrl_read(chan, XILINX_DMA_REG_CURDESC),
1437 dma_ctrl_read(chan, XILINX_DMA_REG_TAILDESC));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301438 chan->err = true;
1439 }
1440 }
1441
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301442 if (status & XILINX_DMA_DMASR_DLY_CNT_IRQ) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301443 /*
1444 * Device takes too long to do the transfer when user requires
1445 * responsiveness.
1446 */
1447 dev_dbg(chan->dev, "Inter-packet latency too long\n");
1448 }
1449
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301450 if (status & XILINX_DMA_DMASR_FRM_CNT_IRQ) {
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301451 spin_lock(&chan->lock);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301452 xilinx_dma_complete_descriptor(chan);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301453 chan->start_transfer(chan);
Kedareswara rao Appana26c5e362016-02-26 19:33:52 +05301454 spin_unlock(&chan->lock);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301455 }
1456
1457 tasklet_schedule(&chan->tasklet);
1458 return IRQ_HANDLED;
1459}
1460
1461/**
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301462 * append_desc_queue - Queuing descriptor
1463 * @chan: Driver specific dma channel
1464 * @desc: dma transaction descriptor
1465 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301466static void append_desc_queue(struct xilinx_dma_chan *chan,
1467 struct xilinx_dma_tx_descriptor *desc)
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301468{
1469 struct xilinx_vdma_tx_segment *tail_segment;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301470 struct xilinx_dma_tx_descriptor *tail_desc;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301471 struct xilinx_axidma_tx_segment *axidma_tail_segment;
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301472 struct xilinx_cdma_tx_segment *cdma_tail_segment;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301473
1474 if (list_empty(&chan->pending_list))
1475 goto append;
1476
1477 /*
1478 * Add the hardware descriptor to the chain of hardware descriptors
1479 * that already exists in memory.
1480 */
1481 tail_desc = list_last_entry(&chan->pending_list,
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301482 struct xilinx_dma_tx_descriptor, node);
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05301483 if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301484 tail_segment = list_last_entry(&tail_desc->segments,
1485 struct xilinx_vdma_tx_segment,
1486 node);
1487 tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05301488 } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301489 cdma_tail_segment = list_last_entry(&tail_desc->segments,
1490 struct xilinx_cdma_tx_segment,
1491 node);
1492 cdma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301493 } else {
1494 axidma_tail_segment = list_last_entry(&tail_desc->segments,
1495 struct xilinx_axidma_tx_segment,
1496 node);
1497 axidma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
1498 }
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301499
1500 /*
1501 * Add the software descriptor and all children to the list
1502 * of pending transactions
1503 */
1504append:
1505 list_add_tail(&desc->node, &chan->pending_list);
1506 chan->desc_pendingcount++;
1507
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05301508 if (chan->has_sg && (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA)
1509 && unlikely(chan->desc_pendingcount > chan->num_frms)) {
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301510 dev_dbg(chan->dev, "desc pendingcount is too high\n");
1511 chan->desc_pendingcount = chan->num_frms;
1512 }
1513}
1514
1515/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301516 * xilinx_dma_tx_submit - Submit DMA transaction
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301517 * @tx: Async transaction descriptor
1518 *
1519 * Return: cookie value on success and failure value on error
1520 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301521static dma_cookie_t xilinx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301522{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301523 struct xilinx_dma_tx_descriptor *desc = to_dma_tx_descriptor(tx);
1524 struct xilinx_dma_chan *chan = to_xilinx_chan(tx->chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301525 dma_cookie_t cookie;
1526 unsigned long flags;
1527 int err;
1528
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301529 if (chan->cyclic) {
1530 xilinx_dma_free_tx_descriptor(chan, desc);
1531 return -EBUSY;
1532 }
1533
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301534 if (chan->err) {
1535 /*
1536 * If reset fails, need to hard reset the system.
1537 * Channel is no longer functional
1538 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301539 err = xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301540 if (err < 0)
1541 return err;
1542 }
1543
1544 spin_lock_irqsave(&chan->lock, flags);
1545
1546 cookie = dma_cookie_assign(tx);
1547
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301548 /* Put this transaction onto the tail of the pending queue */
1549 append_desc_queue(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301550
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301551 if (desc->cyclic)
1552 chan->cyclic = true;
1553
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301554 spin_unlock_irqrestore(&chan->lock, flags);
1555
1556 return cookie;
1557}
1558
1559/**
1560 * xilinx_vdma_dma_prep_interleaved - prepare a descriptor for a
1561 * DMA_SLAVE transaction
1562 * @dchan: DMA channel
1563 * @xt: Interleaved template pointer
1564 * @flags: transfer ack flags
1565 *
1566 * Return: Async transaction descriptor on success and NULL on failure
1567 */
1568static struct dma_async_tx_descriptor *
1569xilinx_vdma_dma_prep_interleaved(struct dma_chan *dchan,
1570 struct dma_interleaved_template *xt,
1571 unsigned long flags)
1572{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301573 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1574 struct xilinx_dma_tx_descriptor *desc;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301575 struct xilinx_vdma_tx_segment *segment, *prev = NULL;
1576 struct xilinx_vdma_desc_hw *hw;
1577
1578 if (!is_slave_direction(xt->dir))
1579 return NULL;
1580
1581 if (!xt->numf || !xt->sgl[0].size)
1582 return NULL;
1583
Srikanth Thokalaa5e48e22014-11-05 20:37:01 +02001584 if (xt->frame_size != 1)
1585 return NULL;
1586
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301587 /* Allocate a transaction descriptor. */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301588 desc = xilinx_dma_alloc_tx_descriptor(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301589 if (!desc)
1590 return NULL;
1591
1592 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301593 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301594 async_tx_ack(&desc->async_tx);
1595
1596 /* Allocate the link descriptor from DMA pool */
1597 segment = xilinx_vdma_alloc_tx_segment(chan);
1598 if (!segment)
1599 goto error;
1600
1601 /* Fill in the hardware descriptor */
1602 hw = &segment->hw;
1603 hw->vsize = xt->numf;
1604 hw->hsize = xt->sgl[0].size;
Srikanth Thokala6d80f452014-11-05 20:37:02 +02001605 hw->stride = (xt->sgl[0].icg + xt->sgl[0].size) <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301606 XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301607 hw->stride |= chan->config.frm_dly <<
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301608 XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301609
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05301610 if (xt->dir != DMA_MEM_TO_DEV) {
1611 if (chan->ext_addr) {
1612 hw->buf_addr = lower_32_bits(xt->dst_start);
1613 hw->buf_addr_msb = upper_32_bits(xt->dst_start);
1614 } else {
1615 hw->buf_addr = xt->dst_start;
1616 }
1617 } else {
1618 if (chan->ext_addr) {
1619 hw->buf_addr = lower_32_bits(xt->src_start);
1620 hw->buf_addr_msb = upper_32_bits(xt->src_start);
1621 } else {
1622 hw->buf_addr = xt->src_start;
1623 }
1624 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301625
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301626 /* Insert the segment into the descriptor segments list. */
1627 list_add_tail(&segment->node, &desc->segments);
1628
1629 prev = segment;
1630
1631 /* Link the last hardware descriptor with the first. */
1632 segment = list_first_entry(&desc->segments,
1633 struct xilinx_vdma_tx_segment, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301634 desc->async_tx.phys = segment->phys;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301635
1636 return &desc->async_tx;
1637
1638error:
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05301639 xilinx_dma_free_tx_descriptor(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301640 return NULL;
1641}
1642
1643/**
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301644 * xilinx_cdma_prep_memcpy - prepare descriptors for a memcpy transaction
1645 * @dchan: DMA channel
1646 * @dma_dst: destination address
1647 * @dma_src: source address
1648 * @len: transfer length
1649 * @flags: transfer ack flags
1650 *
1651 * Return: Async transaction descriptor on success and NULL on failure
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301652 */
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301653static struct dma_async_tx_descriptor *
1654xilinx_cdma_prep_memcpy(struct dma_chan *dchan, dma_addr_t dma_dst,
1655 dma_addr_t dma_src, size_t len, unsigned long flags)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301656{
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301657 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1658 struct xilinx_dma_tx_descriptor *desc;
1659 struct xilinx_cdma_tx_segment *segment, *prev;
1660 struct xilinx_cdma_desc_hw *hw;
1661
1662 if (!len || len > XILINX_DMA_MAX_TRANS_LEN)
1663 return NULL;
1664
1665 desc = xilinx_dma_alloc_tx_descriptor(chan);
1666 if (!desc)
1667 return NULL;
1668
1669 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1670 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1671
1672 /* Allocate the link descriptor from DMA pool */
1673 segment = xilinx_cdma_alloc_tx_segment(chan);
1674 if (!segment)
1675 goto error;
1676
1677 hw = &segment->hw;
1678 hw->control = len;
1679 hw->src_addr = dma_src;
1680 hw->dest_addr = dma_dst;
Kedareswara rao Appana9791e712016-06-07 19:21:16 +05301681 if (chan->ext_addr) {
1682 hw->src_addr_msb = upper_32_bits(dma_src);
1683 hw->dest_addr_msb = upper_32_bits(dma_dst);
1684 }
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05301685
1686 /* Fill the previous next descriptor with current */
1687 prev = list_last_entry(&desc->segments,
1688 struct xilinx_cdma_tx_segment, node);
1689 prev->hw.next_desc = segment->phys;
1690
1691 /* Insert the segment into the descriptor segments list. */
1692 list_add_tail(&segment->node, &desc->segments);
1693
1694 prev = segment;
1695
1696 /* Link the last hardware descriptor with the first. */
1697 segment = list_first_entry(&desc->segments,
1698 struct xilinx_cdma_tx_segment, node);
1699 desc->async_tx.phys = segment->phys;
1700 prev->hw.next_desc = segment->phys;
1701
1702 return &desc->async_tx;
1703
1704error:
1705 xilinx_dma_free_tx_descriptor(chan, desc);
1706 return NULL;
1707}
1708
1709/**
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301710 * xilinx_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
1711 * @dchan: DMA channel
1712 * @sgl: scatterlist to transfer to/from
1713 * @sg_len: number of entries in @scatterlist
1714 * @direction: DMA direction
1715 * @flags: transfer ack flags
1716 * @context: APP words of the descriptor
1717 *
1718 * Return: Async transaction descriptor on success and NULL on failure
1719 */
1720static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
1721 struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
1722 enum dma_transfer_direction direction, unsigned long flags,
1723 void *context)
1724{
1725 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1726 struct xilinx_dma_tx_descriptor *desc;
1727 struct xilinx_axidma_tx_segment *segment = NULL, *prev = NULL;
1728 u32 *app_w = (u32 *)context;
1729 struct scatterlist *sg;
1730 size_t copy;
1731 size_t sg_used;
1732 unsigned int i;
1733
1734 if (!is_slave_direction(direction))
1735 return NULL;
1736
1737 /* Allocate a transaction descriptor. */
1738 desc = xilinx_dma_alloc_tx_descriptor(chan);
1739 if (!desc)
1740 return NULL;
1741
1742 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1743 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1744
1745 /* Build transactions using information in the scatter gather list */
1746 for_each_sg(sgl, sg, sg_len, i) {
1747 sg_used = 0;
1748
1749 /* Loop until the entire scatterlist entry is used */
1750 while (sg_used < sg_dma_len(sg)) {
1751 struct xilinx_axidma_desc_hw *hw;
1752
1753 /* Get a free segment */
1754 segment = xilinx_axidma_alloc_tx_segment(chan);
1755 if (!segment)
1756 goto error;
1757
1758 /*
1759 * Calculate the maximum number of bytes to transfer,
1760 * making sure it is less than the hw limit
1761 */
1762 copy = min_t(size_t, sg_dma_len(sg) - sg_used,
1763 XILINX_DMA_MAX_TRANS_LEN);
1764 hw = &segment->hw;
1765
1766 /* Fill in the descriptor */
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301767 xilinx_axidma_buf(chan, hw, sg_dma_address(sg),
1768 sg_used, 0);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05301769
1770 hw->control = copy;
1771
1772 if (chan->direction == DMA_MEM_TO_DEV) {
1773 if (app_w)
1774 memcpy(hw->app, app_w, sizeof(u32) *
1775 XILINX_DMA_NUM_APP_WORDS);
1776 }
1777
1778 if (prev)
1779 prev->hw.next_desc = segment->phys;
1780
1781 prev = segment;
1782 sg_used += copy;
1783
1784 /*
1785 * Insert the segment into the descriptor segments
1786 * list.
1787 */
1788 list_add_tail(&segment->node, &desc->segments);
1789 }
1790 }
1791
1792 segment = list_first_entry(&desc->segments,
1793 struct xilinx_axidma_tx_segment, node);
1794 desc->async_tx.phys = segment->phys;
1795 prev->hw.next_desc = segment->phys;
1796
1797 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1798 if (chan->direction == DMA_MEM_TO_DEV) {
1799 segment->hw.control |= XILINX_DMA_BD_SOP;
1800 segment = list_last_entry(&desc->segments,
1801 struct xilinx_axidma_tx_segment,
1802 node);
1803 segment->hw.control |= XILINX_DMA_BD_EOP;
1804 }
1805
1806 return &desc->async_tx;
1807
1808error:
1809 xilinx_dma_free_tx_descriptor(chan, desc);
1810 return NULL;
1811}
1812
1813/**
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301814 * xilinx_dma_prep_dma_cyclic - prepare descriptors for a DMA_SLAVE transaction
1815 * @chan: DMA channel
1816 * @sgl: scatterlist to transfer to/from
1817 * @sg_len: number of entries in @scatterlist
1818 * @direction: DMA direction
1819 * @flags: transfer ack flags
1820 */
1821static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
1822 struct dma_chan *dchan, dma_addr_t buf_addr, size_t buf_len,
1823 size_t period_len, enum dma_transfer_direction direction,
1824 unsigned long flags)
1825{
1826 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1827 struct xilinx_dma_tx_descriptor *desc;
1828 struct xilinx_axidma_tx_segment *segment, *head_segment, *prev = NULL;
1829 size_t copy, sg_used;
1830 unsigned int num_periods;
1831 int i;
1832 u32 reg;
1833
Arnd Bergmannf67c3bd2016-06-13 17:07:33 +02001834 if (!period_len)
1835 return NULL;
1836
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301837 num_periods = buf_len / period_len;
1838
Arnd Bergmannf67c3bd2016-06-13 17:07:33 +02001839 if (!num_periods)
1840 return NULL;
1841
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301842 if (!is_slave_direction(direction))
1843 return NULL;
1844
1845 /* Allocate a transaction descriptor. */
1846 desc = xilinx_dma_alloc_tx_descriptor(chan);
1847 if (!desc)
1848 return NULL;
1849
1850 chan->direction = direction;
1851 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1852 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1853
1854 for (i = 0; i < num_periods; ++i) {
1855 sg_used = 0;
1856
1857 while (sg_used < period_len) {
1858 struct xilinx_axidma_desc_hw *hw;
1859
1860 /* Get a free segment */
1861 segment = xilinx_axidma_alloc_tx_segment(chan);
1862 if (!segment)
1863 goto error;
1864
1865 /*
1866 * Calculate the maximum number of bytes to transfer,
1867 * making sure it is less than the hw limit
1868 */
1869 copy = min_t(size_t, period_len - sg_used,
1870 XILINX_DMA_MAX_TRANS_LEN);
1871 hw = &segment->hw;
Kedareswara rao Appanaf0cba682016-06-07 19:21:15 +05301872 xilinx_axidma_buf(chan, hw, buf_addr, sg_used,
1873 period_len * i);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301874 hw->control = copy;
1875
1876 if (prev)
1877 prev->hw.next_desc = segment->phys;
1878
1879 prev = segment;
1880 sg_used += copy;
1881
1882 /*
1883 * Insert the segment into the descriptor segments
1884 * list.
1885 */
1886 list_add_tail(&segment->node, &desc->segments);
1887 }
1888 }
1889
1890 head_segment = list_first_entry(&desc->segments,
1891 struct xilinx_axidma_tx_segment, node);
1892 desc->async_tx.phys = head_segment->phys;
1893
1894 desc->cyclic = true;
1895 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
1896 reg |= XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
1897 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
1898
Kedareswara rao Appanae598e6e2016-07-09 14:09:48 +05301899 segment = list_last_entry(&desc->segments,
1900 struct xilinx_axidma_tx_segment,
1901 node);
1902 segment->hw.next_desc = (u32) head_segment->phys;
1903
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301904 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1905 if (direction == DMA_MEM_TO_DEV) {
Kedareswara rao Appanae167a0b2016-06-09 11:32:12 +05301906 head_segment->hw.control |= XILINX_DMA_BD_SOP;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301907 segment->hw.control |= XILINX_DMA_BD_EOP;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05301908 }
1909
1910 return &desc->async_tx;
1911
1912error:
1913 xilinx_dma_free_tx_descriptor(chan, desc);
1914 return NULL;
1915}
1916
1917/**
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05301918 * xilinx_dma_prep_interleaved - prepare a descriptor for a
1919 * DMA_SLAVE transaction
1920 * @dchan: DMA channel
1921 * @xt: Interleaved template pointer
1922 * @flags: transfer ack flags
1923 *
1924 * Return: Async transaction descriptor on success and NULL on failure
1925 */
1926static struct dma_async_tx_descriptor *
1927xilinx_dma_prep_interleaved(struct dma_chan *dchan,
1928 struct dma_interleaved_template *xt,
1929 unsigned long flags)
1930{
1931 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
1932 struct xilinx_dma_tx_descriptor *desc;
1933 struct xilinx_axidma_tx_segment *segment;
1934 struct xilinx_axidma_desc_hw *hw;
1935
1936 if (!is_slave_direction(xt->dir))
1937 return NULL;
1938
1939 if (!xt->numf || !xt->sgl[0].size)
1940 return NULL;
1941
1942 if (xt->frame_size != 1)
1943 return NULL;
1944
1945 /* Allocate a transaction descriptor. */
1946 desc = xilinx_dma_alloc_tx_descriptor(chan);
1947 if (!desc)
1948 return NULL;
1949
1950 chan->direction = xt->dir;
1951 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
1952 desc->async_tx.tx_submit = xilinx_dma_tx_submit;
1953
1954 /* Get a free segment */
1955 segment = xilinx_axidma_alloc_tx_segment(chan);
1956 if (!segment)
1957 goto error;
1958
1959 hw = &segment->hw;
1960
1961 /* Fill in the descriptor */
1962 if (xt->dir != DMA_MEM_TO_DEV)
1963 hw->buf_addr = xt->dst_start;
1964 else
1965 hw->buf_addr = xt->src_start;
1966
1967 hw->mcdma_control = chan->tdest & XILINX_DMA_BD_TDEST_MASK;
1968 hw->vsize_stride = (xt->numf << XILINX_DMA_BD_VSIZE_SHIFT) &
1969 XILINX_DMA_BD_VSIZE_MASK;
1970 hw->vsize_stride |= (xt->sgl[0].icg + xt->sgl[0].size) &
1971 XILINX_DMA_BD_STRIDE_MASK;
1972 hw->control = xt->sgl[0].size & XILINX_DMA_BD_HSIZE_MASK;
1973
1974 /*
1975 * Insert the segment into the descriptor segments
1976 * list.
1977 */
1978 list_add_tail(&segment->node, &desc->segments);
1979
1980
1981 segment = list_first_entry(&desc->segments,
1982 struct xilinx_axidma_tx_segment, node);
1983 desc->async_tx.phys = segment->phys;
1984
1985 /* For the last DMA_MEM_TO_DEV transfer, set EOP */
1986 if (xt->dir == DMA_MEM_TO_DEV) {
1987 segment->hw.control |= XILINX_DMA_BD_SOP;
1988 segment = list_last_entry(&desc->segments,
1989 struct xilinx_axidma_tx_segment,
1990 node);
1991 segment->hw.control |= XILINX_DMA_BD_EOP;
1992 }
1993
1994 return &desc->async_tx;
1995
1996error:
1997 xilinx_dma_free_tx_descriptor(chan, desc);
1998 return NULL;
1999}
2000
2001/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302002 * xilinx_dma_terminate_all - Halt the channel and free descriptors
2003 * @chan: Driver specific DMA Channel pointer
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302004 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302005static int xilinx_dma_terminate_all(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302006{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302007 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302008 u32 reg;
2009
2010 if (chan->cyclic)
2011 xilinx_dma_chan_reset(chan);
Maxime Ripardba714042014-11-17 14:42:38 +01002012
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302013 /* Halt the DMA engine */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302014 xilinx_dma_halt(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302015
2016 /* Remove and free all of the descriptors in the lists */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302017 xilinx_dma_free_descriptors(chan);
Maxime Ripardba714042014-11-17 14:42:38 +01002018
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302019 if (chan->cyclic) {
2020 reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
2021 reg &= ~XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
2022 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
2023 chan->cyclic = false;
2024 }
2025
Maxime Ripardba714042014-11-17 14:42:38 +01002026 return 0;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302027}
2028
2029/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302030 * xilinx_dma_channel_set_config - Configure VDMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302031 * Run-time configuration for Axi VDMA, supports:
2032 * . halt the channel
2033 * . configure interrupt coalescing and inter-packet delay threshold
2034 * . start/stop parking
2035 * . enable genlock
2036 *
2037 * @dchan: DMA channel
2038 * @cfg: VDMA device configuration pointer
2039 *
2040 * Return: '0' on success and failure value on error
2041 */
2042int xilinx_vdma_channel_set_config(struct dma_chan *dchan,
2043 struct xilinx_vdma_config *cfg)
2044{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302045 struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302046 u32 dmacr;
2047
2048 if (cfg->reset)
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302049 return xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302050
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302051 dmacr = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302052
2053 chan->config.frm_dly = cfg->frm_dly;
2054 chan->config.park = cfg->park;
2055
2056 /* genlock settings */
2057 chan->config.gen_lock = cfg->gen_lock;
2058 chan->config.master = cfg->master;
2059
Radhey Shyam Pandey35614ac2019-09-26 16:20:58 +05302060 dmacr &= ~XILINX_DMA_DMACR_GENLOCK_EN;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302061 if (cfg->gen_lock && chan->genlock) {
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302062 dmacr |= XILINX_DMA_DMACR_GENLOCK_EN;
Radhey Shyam Pandey35614ac2019-09-26 16:20:58 +05302063 dmacr &= ~XILINX_DMA_DMACR_MASTER_MASK;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302064 dmacr |= cfg->master << XILINX_DMA_DMACR_MASTER_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302065 }
2066
2067 chan->config.frm_cnt_en = cfg->frm_cnt_en;
2068 if (cfg->park)
2069 chan->config.park_frm = cfg->park_frm;
2070 else
2071 chan->config.park_frm = -1;
2072
2073 chan->config.coalesc = cfg->coalesc;
2074 chan->config.delay = cfg->delay;
2075
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302076 if (cfg->coalesc <= XILINX_DMA_DMACR_FRAME_COUNT_MAX) {
Radhey Shyam Pandey35614ac2019-09-26 16:20:58 +05302077 dmacr &= ~XILINX_DMA_DMACR_FRAME_COUNT_MASK;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302078 dmacr |= cfg->coalesc << XILINX_DMA_DMACR_FRAME_COUNT_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302079 chan->config.coalesc = cfg->coalesc;
2080 }
2081
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302082 if (cfg->delay <= XILINX_DMA_DMACR_DELAY_MAX) {
Radhey Shyam Pandey35614ac2019-09-26 16:20:58 +05302083 dmacr &= ~XILINX_DMA_DMACR_DELAY_MASK;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302084 dmacr |= cfg->delay << XILINX_DMA_DMACR_DELAY_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302085 chan->config.delay = cfg->delay;
2086 }
2087
2088 /* FSync Source selection */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302089 dmacr &= ~XILINX_DMA_DMACR_FSYNCSRC_MASK;
2090 dmacr |= cfg->ext_fsync << XILINX_DMA_DMACR_FSYNCSRC_SHIFT;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302091
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302092 dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, dmacr);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302093
2094 return 0;
2095}
2096EXPORT_SYMBOL(xilinx_vdma_channel_set_config);
2097
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302098/* -----------------------------------------------------------------------------
2099 * Probe and remove
2100 */
2101
2102/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302103 * xilinx_dma_chan_remove - Per Channel remove function
2104 * @chan: Driver specific DMA channel
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302105 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302106static void xilinx_dma_chan_remove(struct xilinx_dma_chan *chan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302107{
2108 /* Disable all interrupts */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302109 dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
2110 XILINX_DMA_DMAXR_ALL_IRQ_MASK);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302111
2112 if (chan->irq > 0)
2113 free_irq(chan->irq, chan);
2114
2115 tasklet_kill(&chan->tasklet);
2116
2117 list_del(&chan->common.device_node);
2118}
2119
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302120static int axidma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2121 struct clk **tx_clk, struct clk **rx_clk,
2122 struct clk **sg_clk, struct clk **tmp_clk)
2123{
2124 int err;
2125
2126 *tmp_clk = NULL;
2127
2128 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2129 if (IS_ERR(*axi_clk)) {
2130 err = PTR_ERR(*axi_clk);
2131 dev_err(&pdev->dev, "failed to get axi_aclk (%u)\n", err);
2132 return err;
2133 }
2134
2135 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2136 if (IS_ERR(*tx_clk))
2137 *tx_clk = NULL;
2138
2139 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2140 if (IS_ERR(*rx_clk))
2141 *rx_clk = NULL;
2142
2143 *sg_clk = devm_clk_get(&pdev->dev, "m_axi_sg_aclk");
2144 if (IS_ERR(*sg_clk))
2145 *sg_clk = NULL;
2146
2147 err = clk_prepare_enable(*axi_clk);
2148 if (err) {
2149 dev_err(&pdev->dev, "failed to enable axi_clk (%u)\n", err);
2150 return err;
2151 }
2152
2153 err = clk_prepare_enable(*tx_clk);
2154 if (err) {
2155 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2156 goto err_disable_axiclk;
2157 }
2158
2159 err = clk_prepare_enable(*rx_clk);
2160 if (err) {
2161 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2162 goto err_disable_txclk;
2163 }
2164
2165 err = clk_prepare_enable(*sg_clk);
2166 if (err) {
2167 dev_err(&pdev->dev, "failed to enable sg_clk (%u)\n", err);
2168 goto err_disable_rxclk;
2169 }
2170
2171 return 0;
2172
2173err_disable_rxclk:
2174 clk_disable_unprepare(*rx_clk);
2175err_disable_txclk:
2176 clk_disable_unprepare(*tx_clk);
2177err_disable_axiclk:
2178 clk_disable_unprepare(*axi_clk);
2179
2180 return err;
2181}
2182
2183static int axicdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2184 struct clk **dev_clk, struct clk **tmp_clk,
2185 struct clk **tmp1_clk, struct clk **tmp2_clk)
2186{
2187 int err;
2188
2189 *tmp_clk = NULL;
2190 *tmp1_clk = NULL;
2191 *tmp2_clk = NULL;
2192
2193 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2194 if (IS_ERR(*axi_clk)) {
2195 err = PTR_ERR(*axi_clk);
2196 dev_err(&pdev->dev, "failed to get axi_clk (%u)\n", err);
2197 return err;
2198 }
2199
2200 *dev_clk = devm_clk_get(&pdev->dev, "m_axi_aclk");
2201 if (IS_ERR(*dev_clk)) {
2202 err = PTR_ERR(*dev_clk);
2203 dev_err(&pdev->dev, "failed to get dev_clk (%u)\n", err);
2204 return err;
2205 }
2206
2207 err = clk_prepare_enable(*axi_clk);
2208 if (err) {
2209 dev_err(&pdev->dev, "failed to enable axi_clk (%u)\n", err);
2210 return err;
2211 }
2212
2213 err = clk_prepare_enable(*dev_clk);
2214 if (err) {
2215 dev_err(&pdev->dev, "failed to enable dev_clk (%u)\n", err);
2216 goto err_disable_axiclk;
2217 }
2218
2219 return 0;
2220
2221err_disable_axiclk:
2222 clk_disable_unprepare(*axi_clk);
2223
2224 return err;
2225}
2226
2227static int axivdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
2228 struct clk **tx_clk, struct clk **txs_clk,
2229 struct clk **rx_clk, struct clk **rxs_clk)
2230{
2231 int err;
2232
2233 *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
2234 if (IS_ERR(*axi_clk)) {
2235 err = PTR_ERR(*axi_clk);
2236 dev_err(&pdev->dev, "failed to get axi_aclk (%u)\n", err);
2237 return err;
2238 }
2239
2240 *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
2241 if (IS_ERR(*tx_clk))
2242 *tx_clk = NULL;
2243
2244 *txs_clk = devm_clk_get(&pdev->dev, "m_axis_mm2s_aclk");
2245 if (IS_ERR(*txs_clk))
2246 *txs_clk = NULL;
2247
2248 *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
2249 if (IS_ERR(*rx_clk))
2250 *rx_clk = NULL;
2251
2252 *rxs_clk = devm_clk_get(&pdev->dev, "s_axis_s2mm_aclk");
2253 if (IS_ERR(*rxs_clk))
2254 *rxs_clk = NULL;
2255
2256 err = clk_prepare_enable(*axi_clk);
2257 if (err) {
2258 dev_err(&pdev->dev, "failed to enable axi_clk (%u)\n", err);
2259 return err;
2260 }
2261
2262 err = clk_prepare_enable(*tx_clk);
2263 if (err) {
2264 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2265 goto err_disable_axiclk;
2266 }
2267
2268 err = clk_prepare_enable(*txs_clk);
2269 if (err) {
2270 dev_err(&pdev->dev, "failed to enable txs_clk (%u)\n", err);
2271 goto err_disable_txclk;
2272 }
2273
2274 err = clk_prepare_enable(*rx_clk);
2275 if (err) {
2276 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2277 goto err_disable_txsclk;
2278 }
2279
2280 err = clk_prepare_enable(*rxs_clk);
2281 if (err) {
2282 dev_err(&pdev->dev, "failed to enable rxs_clk (%u)\n", err);
2283 goto err_disable_rxclk;
2284 }
2285
2286 return 0;
2287
2288err_disable_rxclk:
2289 clk_disable_unprepare(*rx_clk);
2290err_disable_txsclk:
2291 clk_disable_unprepare(*txs_clk);
2292err_disable_txclk:
2293 clk_disable_unprepare(*tx_clk);
2294err_disable_axiclk:
2295 clk_disable_unprepare(*axi_clk);
2296
2297 return err;
2298}
2299
2300static void xdma_disable_allclks(struct xilinx_dma_device *xdev)
2301{
2302 clk_disable_unprepare(xdev->rxs_clk);
2303 clk_disable_unprepare(xdev->rx_clk);
2304 clk_disable_unprepare(xdev->txs_clk);
2305 clk_disable_unprepare(xdev->tx_clk);
2306 clk_disable_unprepare(xdev->axi_clk);
2307}
2308
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302309/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302310 * xilinx_dma_chan_probe - Per Channel Probing
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302311 * It get channel features from the device tree entry and
2312 * initialize special channel handling routines
2313 *
2314 * @xdev: Driver specific device structure
2315 * @node: Device node
2316 *
2317 * Return: '0' on success and failure value on error
2318 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302319static int xilinx_dma_chan_probe(struct xilinx_dma_device *xdev,
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302320 struct device_node *node, int chan_id)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302321{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302322 struct xilinx_dma_chan *chan;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302323 bool has_dre = false;
2324 u32 value, width;
2325 int err;
2326
2327 /* Allocate and initialize the channel structure */
2328 chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
2329 if (!chan)
2330 return -ENOMEM;
2331
2332 chan->dev = xdev->dev;
2333 chan->xdev = xdev;
2334 chan->has_sg = xdev->has_sg;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05302335 chan->desc_pendingcount = 0x0;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302336 chan->ext_addr = xdev->ext_addr;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302337
2338 spin_lock_init(&chan->lock);
2339 INIT_LIST_HEAD(&chan->pending_list);
2340 INIT_LIST_HEAD(&chan->done_list);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05302341 INIT_LIST_HEAD(&chan->active_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302342
2343 /* Retrieve the channel properties from the device tree */
2344 has_dre = of_property_read_bool(node, "xlnx,include-dre");
2345
2346 chan->genlock = of_property_read_bool(node, "xlnx,genlock-mode");
2347
2348 err = of_property_read_u32(node, "xlnx,datawidth", &value);
2349 if (err) {
2350 dev_err(xdev->dev, "missing xlnx,datawidth property\n");
2351 return err;
2352 }
2353 width = value >> 3; /* Convert bits to bytes */
2354
2355 /* If data width is greater than 8 bytes, DRE is not in hw */
2356 if (width > 8)
2357 has_dre = false;
2358
2359 if (!has_dre)
2360 xdev->common.copy_align = fls(width - 1);
2361
Kedareswara rao Appanae131f1b2016-06-24 10:51:26 +05302362 if (of_device_is_compatible(node, "xlnx,axi-vdma-mm2s-channel") ||
2363 of_device_is_compatible(node, "xlnx,axi-dma-mm2s-channel") ||
2364 of_device_is_compatible(node, "xlnx,axi-cdma-channel")) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302365 chan->direction = DMA_MEM_TO_DEV;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302366 chan->id = chan_id;
2367 chan->tdest = chan_id;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302368
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302369 chan->ctrl_offset = XILINX_DMA_MM2S_CTRL_OFFSET;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302370 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302371 chan->desc_offset = XILINX_VDMA_MM2S_DESC_OFFSET;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302372
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302373 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2374 xdev->flush_on_fsync == XILINX_DMA_FLUSH_MM2S)
2375 chan->flush_on_fsync = true;
2376 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302377 } else if (of_device_is_compatible(node,
Kedareswara rao Appanae131f1b2016-06-24 10:51:26 +05302378 "xlnx,axi-vdma-s2mm-channel") ||
2379 of_device_is_compatible(node,
2380 "xlnx,axi-dma-s2mm-channel")) {
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302381 chan->direction = DMA_DEV_TO_MEM;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302382 chan->id = chan_id;
2383 chan->tdest = chan_id - xdev->nr_channels;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302384
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302385 chan->ctrl_offset = XILINX_DMA_S2MM_CTRL_OFFSET;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302386 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302387 chan->desc_offset = XILINX_VDMA_S2MM_DESC_OFFSET;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302388
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302389 if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
2390 xdev->flush_on_fsync == XILINX_DMA_FLUSH_S2MM)
2391 chan->flush_on_fsync = true;
2392 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302393 } else {
2394 dev_err(xdev->dev, "Invalid channel compatible node\n");
2395 return -EINVAL;
2396 }
2397
2398 /* Request the interrupt */
2399 chan->irq = irq_of_parse_and_map(node, 0);
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302400 err = request_irq(chan->irq, xilinx_dma_irq_handler, IRQF_SHARED,
2401 "xilinx-dma-controller", chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302402 if (err) {
2403 dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq);
2404 return err;
2405 }
2406
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302407 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA)
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302408 chan->start_transfer = xilinx_dma_start_transfer;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302409 else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA)
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302410 chan->start_transfer = xilinx_cdma_start_transfer;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302411 else
2412 chan->start_transfer = xilinx_vdma_start_transfer;
2413
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302414 /* Initialize the tasklet */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302415 tasklet_init(&chan->tasklet, xilinx_dma_do_tasklet,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302416 (unsigned long)chan);
2417
2418 /*
2419 * Initialize the DMA channel and add it to the DMA engine channels
2420 * list.
2421 */
2422 chan->common.device = &xdev->common;
2423
2424 list_add_tail(&chan->common.device_node, &xdev->common.channels);
2425 xdev->chan[chan->id] = chan;
2426
2427 /* Reset the channel */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302428 err = xilinx_dma_chan_reset(chan);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302429 if (err < 0) {
2430 dev_err(xdev->dev, "Reset channel failed\n");
2431 return err;
2432 }
2433
2434 return 0;
2435}
2436
2437/**
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302438 * xilinx_dma_child_probe - Per child node probe
2439 * It get number of dma-channels per child node from
2440 * device-tree and initializes all the channels.
2441 *
2442 * @xdev: Driver specific device structure
2443 * @node: Device node
2444 *
2445 * Return: 0 always.
2446 */
2447static int xilinx_dma_child_probe(struct xilinx_dma_device *xdev,
2448 struct device_node *node) {
2449 int ret, i, nr_channels = 1;
2450
2451 ret = of_property_read_u32(node, "dma-channels", &nr_channels);
2452 if ((ret < 0) && xdev->mcdma)
2453 dev_warn(xdev->dev, "missing dma-channels property\n");
2454
2455 for (i = 0; i < nr_channels; i++)
2456 xilinx_dma_chan_probe(xdev, node, xdev->chan_id++);
2457
2458 xdev->nr_channels += nr_channels;
2459
2460 return 0;
2461}
2462
2463/**
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302464 * of_dma_xilinx_xlate - Translation function
2465 * @dma_spec: Pointer to DMA specifier as found in the device tree
2466 * @ofdma: Pointer to DMA controller data
2467 *
2468 * Return: DMA channel pointer on success and NULL on error
2469 */
2470static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
2471 struct of_dma *ofdma)
2472{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302473 struct xilinx_dma_device *xdev = ofdma->of_dma_data;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302474 int chan_id = dma_spec->args[0];
2475
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302476 if (chan_id >= xdev->nr_channels || !xdev->chan[chan_id])
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302477 return NULL;
2478
2479 return dma_get_slave_channel(&xdev->chan[chan_id]->common);
2480}
2481
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302482static const struct xilinx_dma_config axidma_config = {
2483 .dmatype = XDMA_TYPE_AXIDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302484 .clk_init = axidma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302485};
2486
2487static const struct xilinx_dma_config axicdma_config = {
2488 .dmatype = XDMA_TYPE_CDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302489 .clk_init = axicdma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302490};
2491
2492static const struct xilinx_dma_config axivdma_config = {
2493 .dmatype = XDMA_TYPE_VDMA,
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302494 .clk_init = axivdma_clk_init,
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302495};
2496
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302497static const struct of_device_id xilinx_dma_of_ids[] = {
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302498 { .compatible = "xlnx,axi-dma-1.00.a", .data = &axidma_config },
2499 { .compatible = "xlnx,axi-cdma-1.00.a", .data = &axicdma_config },
2500 { .compatible = "xlnx,axi-vdma-1.00.a", .data = &axivdma_config },
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302501 {}
2502};
2503MODULE_DEVICE_TABLE(of, xilinx_dma_of_ids);
2504
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302505/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302506 * xilinx_dma_probe - Driver probe function
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302507 * @pdev: Pointer to the platform_device structure
2508 *
2509 * Return: '0' on success and failure value on error
2510 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302511static int xilinx_dma_probe(struct platform_device *pdev)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302512{
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302513 int (*clk_init)(struct platform_device *, struct clk **, struct clk **,
2514 struct clk **, struct clk **, struct clk **)
2515 = axivdma_clk_init;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302516 struct device_node *node = pdev->dev.of_node;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302517 struct xilinx_dma_device *xdev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302518 struct device_node *child, *np = pdev->dev.of_node;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302519 struct resource *io;
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302520 u32 num_frames, addr_width;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302521 int i, err;
2522
2523 /* Allocate and initialize the DMA engine structure */
2524 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
2525 if (!xdev)
2526 return -ENOMEM;
2527
2528 xdev->dev = &pdev->dev;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302529 if (np) {
2530 const struct of_device_id *match;
2531
2532 match = of_match_node(xilinx_dma_of_ids, np);
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302533 if (match && match->data) {
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302534 xdev->dma_config = match->data;
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302535 clk_init = xdev->dma_config->clk_init;
2536 }
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302537 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302538
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302539 err = clk_init(pdev, &xdev->axi_clk, &xdev->tx_clk, &xdev->txs_clk,
2540 &xdev->rx_clk, &xdev->rxs_clk);
2541 if (err)
2542 return err;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302543
2544 /* Request and map I/O memory */
2545 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2546 xdev->regs = devm_ioremap_resource(&pdev->dev, io);
2547 if (IS_ERR(xdev->regs))
2548 return PTR_ERR(xdev->regs);
2549
2550 /* Retrieve the DMA engine properties from the device tree */
2551 xdev->has_sg = of_property_read_bool(node, "xlnx,include-sg");
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302552 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA)
2553 xdev->mcdma = of_property_read_bool(node, "xlnx,mcdma");
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302554
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302555 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302556 err = of_property_read_u32(node, "xlnx,num-fstores",
2557 &num_frames);
2558 if (err < 0) {
2559 dev_err(xdev->dev,
2560 "missing xlnx,num-fstores property\n");
2561 return err;
2562 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302563
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302564 err = of_property_read_u32(node, "xlnx,flush-fsync",
2565 &xdev->flush_on_fsync);
2566 if (err < 0)
2567 dev_warn(xdev->dev,
2568 "missing xlnx,flush-fsync property\n");
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302569 }
2570
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302571 err = of_property_read_u32(node, "xlnx,addrwidth", &addr_width);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302572 if (err < 0)
Kedareswara rao Appanab72db402016-04-06 10:38:08 +05302573 dev_warn(xdev->dev, "missing xlnx,addrwidth property\n");
2574
2575 if (addr_width > 32)
2576 xdev->ext_addr = true;
2577 else
2578 xdev->ext_addr = false;
2579
2580 /* Set the dma mask bits */
2581 dma_set_mask(xdev->dev, DMA_BIT_MASK(addr_width));
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302582
2583 /* Initialize the DMA engine */
2584 xdev->common.dev = &pdev->dev;
2585
2586 INIT_LIST_HEAD(&xdev->common.channels);
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302587 if (!(xdev->dma_config->dmatype == XDMA_TYPE_CDMA)) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302588 dma_cap_set(DMA_SLAVE, xdev->common.cap_mask);
2589 dma_cap_set(DMA_PRIVATE, xdev->common.cap_mask);
2590 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302591
2592 xdev->common.device_alloc_chan_resources =
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302593 xilinx_dma_alloc_chan_resources;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302594 xdev->common.device_free_chan_resources =
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302595 xilinx_dma_free_chan_resources;
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302596 xdev->common.device_terminate_all = xilinx_dma_terminate_all;
2597 xdev->common.device_tx_status = xilinx_dma_tx_status;
2598 xdev->common.device_issue_pending = xilinx_dma_issue_pending;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302599 if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302600 dma_cap_set(DMA_CYCLIC, xdev->common.cap_mask);
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302601 xdev->common.device_prep_slave_sg = xilinx_dma_prep_slave_sg;
Kedareswara rao Appana92d794d2016-05-18 13:17:30 +05302602 xdev->common.device_prep_dma_cyclic =
2603 xilinx_dma_prep_dma_cyclic;
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302604 xdev->common.device_prep_interleaved_dma =
2605 xilinx_dma_prep_interleaved;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302606 /* Residue calculation is supported by only AXI DMA */
2607 xdev->common.residue_granularity =
2608 DMA_RESIDUE_GRANULARITY_SEGMENT;
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302609 } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
Kedareswara rao Appana07b0e7d2016-04-07 10:59:45 +05302610 dma_cap_set(DMA_MEMCPY, xdev->common.cap_mask);
2611 xdev->common.device_prep_dma_memcpy = xilinx_cdma_prep_memcpy;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302612 } else {
2613 xdev->common.device_prep_interleaved_dma =
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302614 xilinx_vdma_dma_prep_interleaved;
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302615 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302616
2617 platform_set_drvdata(pdev, xdev);
2618
2619 /* Initialize the channels */
2620 for_each_child_of_node(node, child) {
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302621 err = xilinx_dma_child_probe(xdev, child);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302622 if (err < 0)
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302623 goto disable_clks;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302624 }
2625
Kedareswara rao Appanafb236662016-05-13 12:33:29 +05302626 if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302627 for (i = 0; i < xdev->nr_channels; i++)
Kedareswara rao Appanac0bba3a2016-04-07 10:59:43 +05302628 if (xdev->chan[i])
2629 xdev->chan[i]->num_frms = num_frames;
2630 }
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302631
2632 /* Register the DMA engine with the core */
2633 dma_async_device_register(&xdev->common);
2634
2635 err = of_dma_controller_register(node, of_dma_xilinx_xlate,
2636 xdev);
2637 if (err < 0) {
2638 dev_err(&pdev->dev, "Unable to register DMA to DT\n");
2639 dma_async_device_unregister(&xdev->common);
2640 goto error;
2641 }
2642
2643 dev_info(&pdev->dev, "Xilinx AXI VDMA Engine Driver Probed!!\n");
2644
2645 return 0;
2646
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302647disable_clks:
2648 xdma_disable_allclks(xdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302649error:
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302650 for (i = 0; i < xdev->nr_channels; i++)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302651 if (xdev->chan[i])
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302652 xilinx_dma_chan_remove(xdev->chan[i]);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302653
2654 return err;
2655}
2656
2657/**
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302658 * xilinx_dma_remove - Driver remove function
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302659 * @pdev: Pointer to the platform_device structure
2660 *
2661 * Return: Always '0'
2662 */
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302663static int xilinx_dma_remove(struct platform_device *pdev)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302664{
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302665 struct xilinx_dma_device *xdev = platform_get_drvdata(pdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302666 int i;
2667
2668 of_dma_controller_free(pdev->dev.of_node);
2669
2670 dma_async_device_unregister(&xdev->common);
2671
Kedareswara rao Appana1a9e7a02016-06-24 10:51:23 +05302672 for (i = 0; i < xdev->nr_channels; i++)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302673 if (xdev->chan[i])
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302674 xilinx_dma_chan_remove(xdev->chan[i]);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302675
Kedareswara rao Appanaba16db32016-05-13 12:33:31 +05302676 xdma_disable_allclks(xdev);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302677
2678 return 0;
2679}
2680
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302681static struct platform_driver xilinx_vdma_driver = {
2682 .driver = {
2683 .name = "xilinx-vdma",
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302684 .of_match_table = xilinx_dma_of_ids,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302685 },
Kedareswara rao Appana42c1a2e2016-04-07 10:59:41 +05302686 .probe = xilinx_dma_probe,
2687 .remove = xilinx_dma_remove,
Srikanth Thokala9cd43602014-04-23 20:23:26 +05302688};
2689
2690module_platform_driver(xilinx_vdma_driver);
2691
2692MODULE_AUTHOR("Xilinx, Inc.");
2693MODULE_DESCRIPTION("Xilinx VDMA driver");
2694MODULE_LICENSE("GPL v2");