blob: 06bffec934d22d2fb0a3a27bc2ea22978905d1a0 [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 *
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 2 of the License, or
22 * (at your option) any later version.
23 */
24
Srikanth Thokala9cd43602014-04-23 20:23:26 +053025#include <linux/bitops.h>
26#include <linux/dmapool.h>
Kedareswara rao Appana937abe82015-03-02 23:24:24 +053027#include <linux/dma/xilinx_dma.h>
Srikanth Thokala9cd43602014-04-23 20:23:26 +053028#include <linux/init.h>
29#include <linux/interrupt.h>
30#include <linux/io.h>
31#include <linux/module.h>
32#include <linux/of_address.h>
33#include <linux/of_dma.h>
34#include <linux/of_platform.h>
35#include <linux/of_irq.h>
36#include <linux/slab.h>
37
38#include "../dmaengine.h"
39
40/* Register/Descriptor Offsets */
41#define XILINX_VDMA_MM2S_CTRL_OFFSET 0x0000
42#define XILINX_VDMA_S2MM_CTRL_OFFSET 0x0030
43#define XILINX_VDMA_MM2S_DESC_OFFSET 0x0050
44#define XILINX_VDMA_S2MM_DESC_OFFSET 0x00a0
45
46/* Control Registers */
47#define XILINX_VDMA_REG_DMACR 0x0000
48#define XILINX_VDMA_DMACR_DELAY_MAX 0xff
49#define XILINX_VDMA_DMACR_DELAY_SHIFT 24
50#define XILINX_VDMA_DMACR_FRAME_COUNT_MAX 0xff
51#define XILINX_VDMA_DMACR_FRAME_COUNT_SHIFT 16
52#define XILINX_VDMA_DMACR_ERR_IRQ BIT(14)
53#define XILINX_VDMA_DMACR_DLY_CNT_IRQ BIT(13)
54#define XILINX_VDMA_DMACR_FRM_CNT_IRQ BIT(12)
55#define XILINX_VDMA_DMACR_MASTER_SHIFT 8
56#define XILINX_VDMA_DMACR_FSYNCSRC_SHIFT 5
57#define XILINX_VDMA_DMACR_FRAMECNT_EN BIT(4)
58#define XILINX_VDMA_DMACR_GENLOCK_EN BIT(3)
59#define XILINX_VDMA_DMACR_RESET BIT(2)
60#define XILINX_VDMA_DMACR_CIRC_EN BIT(1)
61#define XILINX_VDMA_DMACR_RUNSTOP BIT(0)
62#define XILINX_VDMA_DMACR_FSYNCSRC_MASK GENMASK(6, 5)
63
64#define XILINX_VDMA_REG_DMASR 0x0004
65#define XILINX_VDMA_DMASR_EOL_LATE_ERR BIT(15)
66#define XILINX_VDMA_DMASR_ERR_IRQ BIT(14)
67#define XILINX_VDMA_DMASR_DLY_CNT_IRQ BIT(13)
68#define XILINX_VDMA_DMASR_FRM_CNT_IRQ BIT(12)
69#define XILINX_VDMA_DMASR_SOF_LATE_ERR BIT(11)
70#define XILINX_VDMA_DMASR_SG_DEC_ERR BIT(10)
71#define XILINX_VDMA_DMASR_SG_SLV_ERR BIT(9)
72#define XILINX_VDMA_DMASR_EOF_EARLY_ERR BIT(8)
73#define XILINX_VDMA_DMASR_SOF_EARLY_ERR BIT(7)
74#define XILINX_VDMA_DMASR_DMA_DEC_ERR BIT(6)
75#define XILINX_VDMA_DMASR_DMA_SLAVE_ERR BIT(5)
76#define XILINX_VDMA_DMASR_DMA_INT_ERR BIT(4)
77#define XILINX_VDMA_DMASR_IDLE BIT(1)
78#define XILINX_VDMA_DMASR_HALTED BIT(0)
79#define XILINX_VDMA_DMASR_DELAY_MASK GENMASK(31, 24)
80#define XILINX_VDMA_DMASR_FRAME_COUNT_MASK GENMASK(23, 16)
81
82#define XILINX_VDMA_REG_CURDESC 0x0008
83#define XILINX_VDMA_REG_TAILDESC 0x0010
84#define XILINX_VDMA_REG_REG_INDEX 0x0014
85#define XILINX_VDMA_REG_FRMSTORE 0x0018
86#define XILINX_VDMA_REG_THRESHOLD 0x001c
87#define XILINX_VDMA_REG_FRMPTR_STS 0x0024
88#define XILINX_VDMA_REG_PARK_PTR 0x0028
89#define XILINX_VDMA_PARK_PTR_WR_REF_SHIFT 8
90#define XILINX_VDMA_PARK_PTR_RD_REF_SHIFT 0
91#define XILINX_VDMA_REG_VDMA_VERSION 0x002c
92
93/* Register Direct Mode Registers */
94#define XILINX_VDMA_REG_VSIZE 0x0000
95#define XILINX_VDMA_REG_HSIZE 0x0004
96
97#define XILINX_VDMA_REG_FRMDLY_STRIDE 0x0008
98#define XILINX_VDMA_FRMDLY_STRIDE_FRMDLY_SHIFT 24
99#define XILINX_VDMA_FRMDLY_STRIDE_STRIDE_SHIFT 0
100
101#define XILINX_VDMA_REG_START_ADDRESS(n) (0x000c + 4 * (n))
102
103/* HW specific definitions */
104#define XILINX_VDMA_MAX_CHANS_PER_DEVICE 0x2
105
106#define XILINX_VDMA_DMAXR_ALL_IRQ_MASK \
107 (XILINX_VDMA_DMASR_FRM_CNT_IRQ | \
108 XILINX_VDMA_DMASR_DLY_CNT_IRQ | \
109 XILINX_VDMA_DMASR_ERR_IRQ)
110
111#define XILINX_VDMA_DMASR_ALL_ERR_MASK \
112 (XILINX_VDMA_DMASR_EOL_LATE_ERR | \
113 XILINX_VDMA_DMASR_SOF_LATE_ERR | \
114 XILINX_VDMA_DMASR_SG_DEC_ERR | \
115 XILINX_VDMA_DMASR_SG_SLV_ERR | \
116 XILINX_VDMA_DMASR_EOF_EARLY_ERR | \
117 XILINX_VDMA_DMASR_SOF_EARLY_ERR | \
118 XILINX_VDMA_DMASR_DMA_DEC_ERR | \
119 XILINX_VDMA_DMASR_DMA_SLAVE_ERR | \
120 XILINX_VDMA_DMASR_DMA_INT_ERR)
121
122/*
123 * Recoverable errors are DMA Internal error, SOF Early, EOF Early
124 * and SOF Late. They are only recoverable when C_FLUSH_ON_FSYNC
125 * is enabled in the h/w system.
126 */
127#define XILINX_VDMA_DMASR_ERR_RECOVER_MASK \
128 (XILINX_VDMA_DMASR_SOF_LATE_ERR | \
129 XILINX_VDMA_DMASR_EOF_EARLY_ERR | \
130 XILINX_VDMA_DMASR_SOF_EARLY_ERR | \
131 XILINX_VDMA_DMASR_DMA_INT_ERR)
132
133/* Axi VDMA Flush on Fsync bits */
134#define XILINX_VDMA_FLUSH_S2MM 3
135#define XILINX_VDMA_FLUSH_MM2S 2
136#define XILINX_VDMA_FLUSH_BOTH 1
137
138/* Delay loop counter to prevent hardware failure */
139#define XILINX_VDMA_LOOP_COUNT 1000000
140
141/**
142 * struct xilinx_vdma_desc_hw - Hardware Descriptor
143 * @next_desc: Next Descriptor Pointer @0x00
144 * @pad1: Reserved @0x04
145 * @buf_addr: Buffer address @0x08
146 * @pad2: Reserved @0x0C
147 * @vsize: Vertical Size @0x10
148 * @hsize: Horizontal Size @0x14
149 * @stride: Number of bytes between the first
150 * pixels of each horizontal line @0x18
151 */
152struct xilinx_vdma_desc_hw {
153 u32 next_desc;
154 u32 pad1;
155 u32 buf_addr;
156 u32 pad2;
157 u32 vsize;
158 u32 hsize;
159 u32 stride;
160} __aligned(64);
161
162/**
163 * struct xilinx_vdma_tx_segment - Descriptor segment
164 * @hw: Hardware descriptor
165 * @node: Node in the descriptor segments list
166 * @phys: Physical address of segment
167 */
168struct xilinx_vdma_tx_segment {
169 struct xilinx_vdma_desc_hw hw;
170 struct list_head node;
171 dma_addr_t phys;
172} __aligned(64);
173
174/**
175 * struct xilinx_vdma_tx_descriptor - Per Transaction structure
176 * @async_tx: Async transaction descriptor
177 * @segments: TX segments list
178 * @node: Node in the channel descriptors list
179 */
180struct xilinx_vdma_tx_descriptor {
181 struct dma_async_tx_descriptor async_tx;
182 struct list_head segments;
183 struct list_head node;
184};
185
186/**
187 * struct xilinx_vdma_chan - Driver specific VDMA channel structure
188 * @xdev: Driver specific device structure
189 * @ctrl_offset: Control registers offset
190 * @desc_offset: TX descriptor registers offset
191 * @lock: Descriptor operation lock
192 * @pending_list: Descriptors waiting
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530193 * @active_list: Descriptors ready to submit
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530194 * @done_list: Complete descriptors
195 * @common: DMA common channel
196 * @desc_pool: Descriptors pool
197 * @dev: The dma device
198 * @irq: Channel IRQ
199 * @id: Channel ID
200 * @direction: Transfer direction
201 * @num_frms: Number of frames
202 * @has_sg: Support scatter transfers
203 * @genlock: Support genlock mode
204 * @err: Channel has errors
205 * @tasklet: Cleanup work after irq
206 * @config: Device configuration info
207 * @flush_on_fsync: Flush on Frame sync
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530208 * @desc_pendingcount: Descriptor pending count
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530209 */
210struct xilinx_vdma_chan {
211 struct xilinx_vdma_device *xdev;
212 u32 ctrl_offset;
213 u32 desc_offset;
214 spinlock_t lock;
215 struct list_head pending_list;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530216 struct list_head active_list;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530217 struct list_head done_list;
218 struct dma_chan common;
219 struct dma_pool *desc_pool;
220 struct device *dev;
221 int irq;
222 int id;
223 enum dma_transfer_direction direction;
224 int num_frms;
225 bool has_sg;
226 bool genlock;
227 bool err;
228 struct tasklet_struct tasklet;
229 struct xilinx_vdma_config config;
230 bool flush_on_fsync;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530231 u32 desc_pendingcount;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530232};
233
234/**
235 * struct xilinx_vdma_device - VDMA device structure
236 * @regs: I/O mapped base address
237 * @dev: Device Structure
238 * @common: DMA device structure
239 * @chan: Driver specific VDMA channel
240 * @has_sg: Specifies whether Scatter-Gather is present or not
241 * @flush_on_fsync: Flush on frame sync
242 */
243struct xilinx_vdma_device {
244 void __iomem *regs;
245 struct device *dev;
246 struct dma_device common;
247 struct xilinx_vdma_chan *chan[XILINX_VDMA_MAX_CHANS_PER_DEVICE];
248 bool has_sg;
249 u32 flush_on_fsync;
250};
251
252/* Macros */
253#define to_xilinx_chan(chan) \
254 container_of(chan, struct xilinx_vdma_chan, common)
255#define to_vdma_tx_descriptor(tx) \
256 container_of(tx, struct xilinx_vdma_tx_descriptor, async_tx)
257
258/* IO accessors */
259static inline u32 vdma_read(struct xilinx_vdma_chan *chan, u32 reg)
260{
261 return ioread32(chan->xdev->regs + reg);
262}
263
264static inline void vdma_write(struct xilinx_vdma_chan *chan, u32 reg, u32 value)
265{
266 iowrite32(value, chan->xdev->regs + reg);
267}
268
269static inline void vdma_desc_write(struct xilinx_vdma_chan *chan, u32 reg,
270 u32 value)
271{
272 vdma_write(chan, chan->desc_offset + reg, value);
273}
274
275static inline u32 vdma_ctrl_read(struct xilinx_vdma_chan *chan, u32 reg)
276{
277 return vdma_read(chan, chan->ctrl_offset + reg);
278}
279
280static inline void vdma_ctrl_write(struct xilinx_vdma_chan *chan, u32 reg,
281 u32 value)
282{
283 vdma_write(chan, chan->ctrl_offset + reg, value);
284}
285
286static inline void vdma_ctrl_clr(struct xilinx_vdma_chan *chan, u32 reg,
287 u32 clr)
288{
289 vdma_ctrl_write(chan, reg, vdma_ctrl_read(chan, reg) & ~clr);
290}
291
292static inline void vdma_ctrl_set(struct xilinx_vdma_chan *chan, u32 reg,
293 u32 set)
294{
295 vdma_ctrl_write(chan, reg, vdma_ctrl_read(chan, reg) | set);
296}
297
298/* -----------------------------------------------------------------------------
299 * Descriptors and segments alloc and free
300 */
301
302/**
303 * xilinx_vdma_alloc_tx_segment - Allocate transaction segment
304 * @chan: Driver specific VDMA channel
305 *
306 * Return: The allocated segment on success and NULL on failure.
307 */
308static struct xilinx_vdma_tx_segment *
309xilinx_vdma_alloc_tx_segment(struct xilinx_vdma_chan *chan)
310{
311 struct xilinx_vdma_tx_segment *segment;
312 dma_addr_t phys;
313
314 segment = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &phys);
315 if (!segment)
316 return NULL;
317
318 memset(segment, 0, sizeof(*segment));
319 segment->phys = phys;
320
321 return segment;
322}
323
324/**
325 * xilinx_vdma_free_tx_segment - Free transaction segment
326 * @chan: Driver specific VDMA channel
327 * @segment: VDMA transaction segment
328 */
329static void xilinx_vdma_free_tx_segment(struct xilinx_vdma_chan *chan,
330 struct xilinx_vdma_tx_segment *segment)
331{
332 dma_pool_free(chan->desc_pool, segment, segment->phys);
333}
334
335/**
336 * xilinx_vdma_tx_descriptor - Allocate transaction descriptor
337 * @chan: Driver specific VDMA channel
338 *
339 * Return: The allocated descriptor on success and NULL on failure.
340 */
341static struct xilinx_vdma_tx_descriptor *
342xilinx_vdma_alloc_tx_descriptor(struct xilinx_vdma_chan *chan)
343{
344 struct xilinx_vdma_tx_descriptor *desc;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530345
346 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
347 if (!desc)
348 return NULL;
349
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530350 INIT_LIST_HEAD(&desc->segments);
351
352 return desc;
353}
354
355/**
356 * xilinx_vdma_free_tx_descriptor - Free transaction descriptor
357 * @chan: Driver specific VDMA channel
358 * @desc: VDMA transaction descriptor
359 */
360static void
361xilinx_vdma_free_tx_descriptor(struct xilinx_vdma_chan *chan,
362 struct xilinx_vdma_tx_descriptor *desc)
363{
364 struct xilinx_vdma_tx_segment *segment, *next;
365
366 if (!desc)
367 return;
368
369 list_for_each_entry_safe(segment, next, &desc->segments, node) {
370 list_del(&segment->node);
371 xilinx_vdma_free_tx_segment(chan, segment);
372 }
373
374 kfree(desc);
375}
376
377/* Required functions */
378
379/**
380 * xilinx_vdma_free_desc_list - Free descriptors list
381 * @chan: Driver specific VDMA channel
382 * @list: List to parse and delete the descriptor
383 */
384static void xilinx_vdma_free_desc_list(struct xilinx_vdma_chan *chan,
385 struct list_head *list)
386{
387 struct xilinx_vdma_tx_descriptor *desc, *next;
388
389 list_for_each_entry_safe(desc, next, list, node) {
390 list_del(&desc->node);
391 xilinx_vdma_free_tx_descriptor(chan, desc);
392 }
393}
394
395/**
396 * xilinx_vdma_free_descriptors - Free channel descriptors
397 * @chan: Driver specific VDMA channel
398 */
399static void xilinx_vdma_free_descriptors(struct xilinx_vdma_chan *chan)
400{
401 unsigned long flags;
402
403 spin_lock_irqsave(&chan->lock, flags);
404
405 xilinx_vdma_free_desc_list(chan, &chan->pending_list);
406 xilinx_vdma_free_desc_list(chan, &chan->done_list);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530407 xilinx_vdma_free_desc_list(chan, &chan->active_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530408
409 spin_unlock_irqrestore(&chan->lock, flags);
410}
411
412/**
413 * xilinx_vdma_free_chan_resources - Free channel resources
414 * @dchan: DMA channel
415 */
416static void xilinx_vdma_free_chan_resources(struct dma_chan *dchan)
417{
418 struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
419
420 dev_dbg(chan->dev, "Free all channel resources.\n");
421
422 xilinx_vdma_free_descriptors(chan);
423 dma_pool_destroy(chan->desc_pool);
424 chan->desc_pool = NULL;
425}
426
427/**
428 * xilinx_vdma_chan_desc_cleanup - Clean channel descriptors
429 * @chan: Driver specific VDMA channel
430 */
431static void xilinx_vdma_chan_desc_cleanup(struct xilinx_vdma_chan *chan)
432{
433 struct xilinx_vdma_tx_descriptor *desc, *next;
434 unsigned long flags;
435
436 spin_lock_irqsave(&chan->lock, flags);
437
438 list_for_each_entry_safe(desc, next, &chan->done_list, node) {
439 dma_async_tx_callback callback;
440 void *callback_param;
441
442 /* Remove from the list of running transactions */
443 list_del(&desc->node);
444
445 /* Run the link descriptor callback function */
446 callback = desc->async_tx.callback;
447 callback_param = desc->async_tx.callback_param;
448 if (callback) {
449 spin_unlock_irqrestore(&chan->lock, flags);
450 callback(callback_param);
451 spin_lock_irqsave(&chan->lock, flags);
452 }
453
454 /* Run any dependencies, then free the descriptor */
455 dma_run_dependencies(&desc->async_tx);
456 xilinx_vdma_free_tx_descriptor(chan, desc);
457 }
458
459 spin_unlock_irqrestore(&chan->lock, flags);
460}
461
462/**
463 * xilinx_vdma_do_tasklet - Schedule completion tasklet
464 * @data: Pointer to the Xilinx VDMA channel structure
465 */
466static void xilinx_vdma_do_tasklet(unsigned long data)
467{
468 struct xilinx_vdma_chan *chan = (struct xilinx_vdma_chan *)data;
469
470 xilinx_vdma_chan_desc_cleanup(chan);
471}
472
473/**
474 * xilinx_vdma_alloc_chan_resources - Allocate channel resources
475 * @dchan: DMA channel
476 *
477 * Return: '0' on success and failure value on error
478 */
479static int xilinx_vdma_alloc_chan_resources(struct dma_chan *dchan)
480{
481 struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
482
483 /* Has this channel already been allocated? */
484 if (chan->desc_pool)
485 return 0;
486
487 /*
488 * We need the descriptor to be aligned to 64bytes
489 * for meeting Xilinx VDMA specification requirement.
490 */
491 chan->desc_pool = dma_pool_create("xilinx_vdma_desc_pool",
492 chan->dev,
493 sizeof(struct xilinx_vdma_tx_segment),
494 __alignof__(struct xilinx_vdma_tx_segment), 0);
495 if (!chan->desc_pool) {
496 dev_err(chan->dev,
497 "unable to allocate channel %d descriptor pool\n",
498 chan->id);
499 return -ENOMEM;
500 }
501
502 dma_cookie_init(dchan);
503 return 0;
504}
505
506/**
507 * xilinx_vdma_tx_status - Get VDMA transaction status
508 * @dchan: DMA channel
509 * @cookie: Transaction identifier
510 * @txstate: Transaction state
511 *
512 * Return: DMA transaction status
513 */
514static enum dma_status xilinx_vdma_tx_status(struct dma_chan *dchan,
515 dma_cookie_t cookie,
516 struct dma_tx_state *txstate)
517{
518 return dma_cookie_status(dchan, cookie, txstate);
519}
520
521/**
522 * xilinx_vdma_is_running - Check if VDMA channel is running
523 * @chan: Driver specific VDMA channel
524 *
525 * Return: '1' if running, '0' if not.
526 */
527static bool xilinx_vdma_is_running(struct xilinx_vdma_chan *chan)
528{
529 return !(vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR) &
530 XILINX_VDMA_DMASR_HALTED) &&
531 (vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR) &
532 XILINX_VDMA_DMACR_RUNSTOP);
533}
534
535/**
536 * xilinx_vdma_is_idle - Check if VDMA channel is idle
537 * @chan: Driver specific VDMA channel
538 *
539 * Return: '1' if idle, '0' if not.
540 */
541static bool xilinx_vdma_is_idle(struct xilinx_vdma_chan *chan)
542{
543 return vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR) &
544 XILINX_VDMA_DMASR_IDLE;
545}
546
547/**
548 * xilinx_vdma_halt - Halt VDMA channel
549 * @chan: Driver specific VDMA channel
550 */
551static void xilinx_vdma_halt(struct xilinx_vdma_chan *chan)
552{
553 int loop = XILINX_VDMA_LOOP_COUNT;
554
555 vdma_ctrl_clr(chan, XILINX_VDMA_REG_DMACR, XILINX_VDMA_DMACR_RUNSTOP);
556
557 /* Wait for the hardware to halt */
558 do {
559 if (vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR) &
560 XILINX_VDMA_DMASR_HALTED)
561 break;
562 } while (loop--);
563
564 if (!loop) {
565 dev_err(chan->dev, "Cannot stop channel %p: %x\n",
566 chan, vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR));
567 chan->err = true;
568 }
569
570 return;
571}
572
573/**
574 * xilinx_vdma_start - Start VDMA channel
575 * @chan: Driver specific VDMA channel
576 */
577static void xilinx_vdma_start(struct xilinx_vdma_chan *chan)
578{
579 int loop = XILINX_VDMA_LOOP_COUNT;
580
581 vdma_ctrl_set(chan, XILINX_VDMA_REG_DMACR, XILINX_VDMA_DMACR_RUNSTOP);
582
583 /* Wait for the hardware to start */
584 do {
585 if (!(vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR) &
586 XILINX_VDMA_DMASR_HALTED))
587 break;
588 } while (loop--);
589
590 if (!loop) {
591 dev_err(chan->dev, "Cannot start channel %p: %x\n",
592 chan, vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR));
593
594 chan->err = true;
595 }
596
597 return;
598}
599
600/**
601 * xilinx_vdma_start_transfer - Starts VDMA transfer
602 * @chan: Driver specific channel struct pointer
603 */
604static void xilinx_vdma_start_transfer(struct xilinx_vdma_chan *chan)
605{
606 struct xilinx_vdma_config *config = &chan->config;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530607 struct xilinx_vdma_tx_descriptor *desc, *tail_desc;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530608 unsigned long flags;
609 u32 reg;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530610 struct xilinx_vdma_tx_segment *tail_segment;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530611
612 if (chan->err)
613 return;
614
615 spin_lock_irqsave(&chan->lock, flags);
616
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530617 if (list_empty(&chan->pending_list))
618 goto out_unlock;
619
620 desc = list_first_entry(&chan->pending_list,
621 struct xilinx_vdma_tx_descriptor, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530622 tail_desc = list_last_entry(&chan->pending_list,
623 struct xilinx_vdma_tx_descriptor, node);
624
625 tail_segment = list_last_entry(&tail_desc->segments,
626 struct xilinx_vdma_tx_segment, node);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530627
628 /* If it is SG mode and hardware is busy, cannot submit */
629 if (chan->has_sg && xilinx_vdma_is_running(chan) &&
630 !xilinx_vdma_is_idle(chan)) {
631 dev_dbg(chan->dev, "DMA controller still busy\n");
632 goto out_unlock;
633 }
634
635 /*
636 * If hardware is idle, then all descriptors on the running lists are
637 * done, start new transfers
638 */
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530639 if (chan->has_sg)
640 vdma_ctrl_write(chan, XILINX_VDMA_REG_CURDESC,
641 desc->async_tx.phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530642
643 /* Configure the hardware using info in the config structure */
644 reg = vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR);
645
646 if (config->frm_cnt_en)
647 reg |= XILINX_VDMA_DMACR_FRAMECNT_EN;
648 else
649 reg &= ~XILINX_VDMA_DMACR_FRAMECNT_EN;
650
651 /*
652 * With SG, start with circular mode, so that BDs can be fetched.
653 * In direct register mode, if not parking, enable circular mode
654 */
655 if (chan->has_sg || !config->park)
656 reg |= XILINX_VDMA_DMACR_CIRC_EN;
657
658 if (config->park)
659 reg &= ~XILINX_VDMA_DMACR_CIRC_EN;
660
661 vdma_ctrl_write(chan, XILINX_VDMA_REG_DMACR, reg);
662
663 if (config->park && (config->park_frm >= 0) &&
664 (config->park_frm < chan->num_frms)) {
665 if (chan->direction == DMA_MEM_TO_DEV)
666 vdma_write(chan, XILINX_VDMA_REG_PARK_PTR,
667 config->park_frm <<
668 XILINX_VDMA_PARK_PTR_RD_REF_SHIFT);
669 else
670 vdma_write(chan, XILINX_VDMA_REG_PARK_PTR,
671 config->park_frm <<
672 XILINX_VDMA_PARK_PTR_WR_REF_SHIFT);
673 }
674
675 /* Start the hardware */
676 xilinx_vdma_start(chan);
677
678 if (chan->err)
679 goto out_unlock;
680
681 /* Start the transfer */
682 if (chan->has_sg) {
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530683 vdma_ctrl_write(chan, XILINX_VDMA_REG_TAILDESC,
684 tail_segment->phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530685 } else {
686 struct xilinx_vdma_tx_segment *segment, *last = NULL;
687 int i = 0;
688
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530689 list_for_each_entry(desc, &chan->pending_list, node) {
690 segment = list_first_entry(&desc->segments,
691 struct xilinx_vdma_tx_segment, node);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530692 vdma_desc_write(chan,
693 XILINX_VDMA_REG_START_ADDRESS(i++),
694 segment->hw.buf_addr);
695 last = segment;
696 }
697
698 if (!last)
699 goto out_unlock;
700
701 /* HW expects these parameters to be same for one transaction */
702 vdma_desc_write(chan, XILINX_VDMA_REG_HSIZE, last->hw.hsize);
703 vdma_desc_write(chan, XILINX_VDMA_REG_FRMDLY_STRIDE,
704 last->hw.stride);
705 vdma_desc_write(chan, XILINX_VDMA_REG_VSIZE, last->hw.vsize);
706 }
707
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530708 list_splice_tail_init(&chan->pending_list, &chan->active_list);
709 chan->desc_pendingcount = 0;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530710
711out_unlock:
712 spin_unlock_irqrestore(&chan->lock, flags);
713}
714
715/**
716 * xilinx_vdma_issue_pending - Issue pending transactions
717 * @dchan: DMA channel
718 */
719static void xilinx_vdma_issue_pending(struct dma_chan *dchan)
720{
721 struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
722
723 xilinx_vdma_start_transfer(chan);
724}
725
726/**
727 * xilinx_vdma_complete_descriptor - Mark the active descriptor as complete
728 * @chan : xilinx DMA channel
729 *
730 * CONTEXT: hardirq
731 */
732static void xilinx_vdma_complete_descriptor(struct xilinx_vdma_chan *chan)
733{
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530734 struct xilinx_vdma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530735 unsigned long flags;
736
737 spin_lock_irqsave(&chan->lock, flags);
738
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530739 if (list_empty(&chan->active_list))
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530740 goto out_unlock;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530741
742 list_for_each_entry_safe(desc, next, &chan->active_list, node) {
743 list_del(&desc->node);
744 dma_cookie_complete(&desc->async_tx);
745 list_add_tail(&desc->node, &chan->done_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530746 }
747
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530748out_unlock:
749 spin_unlock_irqrestore(&chan->lock, flags);
750}
751
752/**
753 * xilinx_vdma_reset - Reset VDMA channel
754 * @chan: Driver specific VDMA channel
755 *
756 * Return: '0' on success and failure value on error
757 */
758static int xilinx_vdma_reset(struct xilinx_vdma_chan *chan)
759{
760 int loop = XILINX_VDMA_LOOP_COUNT;
761 u32 tmp;
762
763 vdma_ctrl_set(chan, XILINX_VDMA_REG_DMACR, XILINX_VDMA_DMACR_RESET);
764
765 tmp = vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR) &
766 XILINX_VDMA_DMACR_RESET;
767
768 /* Wait for the hardware to finish reset */
769 do {
770 tmp = vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR) &
771 XILINX_VDMA_DMACR_RESET;
772 } while (loop-- && tmp);
773
774 if (!loop) {
775 dev_err(chan->dev, "reset timeout, cr %x, sr %x\n",
776 vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR),
777 vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR));
778 return -ETIMEDOUT;
779 }
780
781 chan->err = false;
782
783 return 0;
784}
785
786/**
787 * xilinx_vdma_chan_reset - Reset VDMA channel and enable interrupts
788 * @chan: Driver specific VDMA channel
789 *
790 * Return: '0' on success and failure value on error
791 */
792static int xilinx_vdma_chan_reset(struct xilinx_vdma_chan *chan)
793{
794 int err;
795
796 /* Reset VDMA */
797 err = xilinx_vdma_reset(chan);
798 if (err)
799 return err;
800
801 /* Enable interrupts */
802 vdma_ctrl_set(chan, XILINX_VDMA_REG_DMACR,
803 XILINX_VDMA_DMAXR_ALL_IRQ_MASK);
804
805 return 0;
806}
807
808/**
809 * xilinx_vdma_irq_handler - VDMA Interrupt handler
810 * @irq: IRQ number
811 * @data: Pointer to the Xilinx VDMA channel structure
812 *
813 * Return: IRQ_HANDLED/IRQ_NONE
814 */
815static irqreturn_t xilinx_vdma_irq_handler(int irq, void *data)
816{
817 struct xilinx_vdma_chan *chan = data;
818 u32 status;
819
820 /* Read the status and ack the interrupts. */
821 status = vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR);
822 if (!(status & XILINX_VDMA_DMAXR_ALL_IRQ_MASK))
823 return IRQ_NONE;
824
825 vdma_ctrl_write(chan, XILINX_VDMA_REG_DMASR,
826 status & XILINX_VDMA_DMAXR_ALL_IRQ_MASK);
827
828 if (status & XILINX_VDMA_DMASR_ERR_IRQ) {
829 /*
830 * An error occurred. If C_FLUSH_ON_FSYNC is enabled and the
831 * error is recoverable, ignore it. Otherwise flag the error.
832 *
833 * Only recoverable errors can be cleared in the DMASR register,
834 * make sure not to write to other error bits to 1.
835 */
836 u32 errors = status & XILINX_VDMA_DMASR_ALL_ERR_MASK;
837 vdma_ctrl_write(chan, XILINX_VDMA_REG_DMASR,
838 errors & XILINX_VDMA_DMASR_ERR_RECOVER_MASK);
839
840 if (!chan->flush_on_fsync ||
841 (errors & ~XILINX_VDMA_DMASR_ERR_RECOVER_MASK)) {
842 dev_err(chan->dev,
843 "Channel %p has errors %x, cdr %x tdr %x\n",
844 chan, errors,
845 vdma_ctrl_read(chan, XILINX_VDMA_REG_CURDESC),
846 vdma_ctrl_read(chan, XILINX_VDMA_REG_TAILDESC));
847 chan->err = true;
848 }
849 }
850
851 if (status & XILINX_VDMA_DMASR_DLY_CNT_IRQ) {
852 /*
853 * Device takes too long to do the transfer when user requires
854 * responsiveness.
855 */
856 dev_dbg(chan->dev, "Inter-packet latency too long\n");
857 }
858
859 if (status & XILINX_VDMA_DMASR_FRM_CNT_IRQ) {
860 xilinx_vdma_complete_descriptor(chan);
861 xilinx_vdma_start_transfer(chan);
862 }
863
864 tasklet_schedule(&chan->tasklet);
865 return IRQ_HANDLED;
866}
867
868/**
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530869 * append_desc_queue - Queuing descriptor
870 * @chan: Driver specific dma channel
871 * @desc: dma transaction descriptor
872 */
873static void append_desc_queue(struct xilinx_vdma_chan *chan,
874 struct xilinx_vdma_tx_descriptor *desc)
875{
876 struct xilinx_vdma_tx_segment *tail_segment;
877 struct xilinx_vdma_tx_descriptor *tail_desc;
878
879 if (list_empty(&chan->pending_list))
880 goto append;
881
882 /*
883 * Add the hardware descriptor to the chain of hardware descriptors
884 * that already exists in memory.
885 */
886 tail_desc = list_last_entry(&chan->pending_list,
887 struct xilinx_vdma_tx_descriptor, node);
888 tail_segment = list_last_entry(&tail_desc->segments,
889 struct xilinx_vdma_tx_segment, node);
890 tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
891
892 /*
893 * Add the software descriptor and all children to the list
894 * of pending transactions
895 */
896append:
897 list_add_tail(&desc->node, &chan->pending_list);
898 chan->desc_pendingcount++;
899
900 if (unlikely(chan->desc_pendingcount > chan->num_frms)) {
901 dev_dbg(chan->dev, "desc pendingcount is too high\n");
902 chan->desc_pendingcount = chan->num_frms;
903 }
904}
905
906/**
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530907 * xilinx_vdma_tx_submit - Submit DMA transaction
908 * @tx: Async transaction descriptor
909 *
910 * Return: cookie value on success and failure value on error
911 */
912static dma_cookie_t xilinx_vdma_tx_submit(struct dma_async_tx_descriptor *tx)
913{
914 struct xilinx_vdma_tx_descriptor *desc = to_vdma_tx_descriptor(tx);
915 struct xilinx_vdma_chan *chan = to_xilinx_chan(tx->chan);
916 dma_cookie_t cookie;
917 unsigned long flags;
918 int err;
919
920 if (chan->err) {
921 /*
922 * If reset fails, need to hard reset the system.
923 * Channel is no longer functional
924 */
925 err = xilinx_vdma_chan_reset(chan);
926 if (err < 0)
927 return err;
928 }
929
930 spin_lock_irqsave(&chan->lock, flags);
931
932 cookie = dma_cookie_assign(tx);
933
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530934 /* Put this transaction onto the tail of the pending queue */
935 append_desc_queue(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530936
937 spin_unlock_irqrestore(&chan->lock, flags);
938
939 return cookie;
940}
941
942/**
943 * xilinx_vdma_dma_prep_interleaved - prepare a descriptor for a
944 * DMA_SLAVE transaction
945 * @dchan: DMA channel
946 * @xt: Interleaved template pointer
947 * @flags: transfer ack flags
948 *
949 * Return: Async transaction descriptor on success and NULL on failure
950 */
951static struct dma_async_tx_descriptor *
952xilinx_vdma_dma_prep_interleaved(struct dma_chan *dchan,
953 struct dma_interleaved_template *xt,
954 unsigned long flags)
955{
956 struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
957 struct xilinx_vdma_tx_descriptor *desc;
958 struct xilinx_vdma_tx_segment *segment, *prev = NULL;
959 struct xilinx_vdma_desc_hw *hw;
960
961 if (!is_slave_direction(xt->dir))
962 return NULL;
963
964 if (!xt->numf || !xt->sgl[0].size)
965 return NULL;
966
Srikanth Thokalaa5e48e22014-11-05 20:37:01 +0200967 if (xt->frame_size != 1)
968 return NULL;
969
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530970 /* Allocate a transaction descriptor. */
971 desc = xilinx_vdma_alloc_tx_descriptor(chan);
972 if (!desc)
973 return NULL;
974
975 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
976 desc->async_tx.tx_submit = xilinx_vdma_tx_submit;
977 async_tx_ack(&desc->async_tx);
978
979 /* Allocate the link descriptor from DMA pool */
980 segment = xilinx_vdma_alloc_tx_segment(chan);
981 if (!segment)
982 goto error;
983
984 /* Fill in the hardware descriptor */
985 hw = &segment->hw;
986 hw->vsize = xt->numf;
987 hw->hsize = xt->sgl[0].size;
Srikanth Thokala6d80f452014-11-05 20:37:02 +0200988 hw->stride = (xt->sgl[0].icg + xt->sgl[0].size) <<
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530989 XILINX_VDMA_FRMDLY_STRIDE_STRIDE_SHIFT;
990 hw->stride |= chan->config.frm_dly <<
991 XILINX_VDMA_FRMDLY_STRIDE_FRMDLY_SHIFT;
992
993 if (xt->dir != DMA_MEM_TO_DEV)
994 hw->buf_addr = xt->dst_start;
995 else
996 hw->buf_addr = xt->src_start;
997
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530998 /* Insert the segment into the descriptor segments list. */
999 list_add_tail(&segment->node, &desc->segments);
1000
1001 prev = segment;
1002
1003 /* Link the last hardware descriptor with the first. */
1004 segment = list_first_entry(&desc->segments,
1005 struct xilinx_vdma_tx_segment, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301006 desc->async_tx.phys = segment->phys;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301007
1008 return &desc->async_tx;
1009
1010error:
1011 xilinx_vdma_free_tx_descriptor(chan, desc);
1012 return NULL;
1013}
1014
1015/**
1016 * xilinx_vdma_terminate_all - Halt the channel and free descriptors
1017 * @chan: Driver specific VDMA Channel pointer
1018 */
Maxime Ripardba714042014-11-17 14:42:38 +01001019static int xilinx_vdma_terminate_all(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301020{
Maxime Ripardba714042014-11-17 14:42:38 +01001021 struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
1022
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301023 /* Halt the DMA engine */
1024 xilinx_vdma_halt(chan);
1025
1026 /* Remove and free all of the descriptors in the lists */
1027 xilinx_vdma_free_descriptors(chan);
Maxime Ripardba714042014-11-17 14:42:38 +01001028
1029 return 0;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301030}
1031
1032/**
1033 * xilinx_vdma_channel_set_config - Configure VDMA channel
1034 * Run-time configuration for Axi VDMA, supports:
1035 * . halt the channel
1036 * . configure interrupt coalescing and inter-packet delay threshold
1037 * . start/stop parking
1038 * . enable genlock
1039 *
1040 * @dchan: DMA channel
1041 * @cfg: VDMA device configuration pointer
1042 *
1043 * Return: '0' on success and failure value on error
1044 */
1045int xilinx_vdma_channel_set_config(struct dma_chan *dchan,
1046 struct xilinx_vdma_config *cfg)
1047{
1048 struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
1049 u32 dmacr;
1050
1051 if (cfg->reset)
1052 return xilinx_vdma_chan_reset(chan);
1053
1054 dmacr = vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR);
1055
1056 chan->config.frm_dly = cfg->frm_dly;
1057 chan->config.park = cfg->park;
1058
1059 /* genlock settings */
1060 chan->config.gen_lock = cfg->gen_lock;
1061 chan->config.master = cfg->master;
1062
1063 if (cfg->gen_lock && chan->genlock) {
1064 dmacr |= XILINX_VDMA_DMACR_GENLOCK_EN;
1065 dmacr |= cfg->master << XILINX_VDMA_DMACR_MASTER_SHIFT;
1066 }
1067
1068 chan->config.frm_cnt_en = cfg->frm_cnt_en;
1069 if (cfg->park)
1070 chan->config.park_frm = cfg->park_frm;
1071 else
1072 chan->config.park_frm = -1;
1073
1074 chan->config.coalesc = cfg->coalesc;
1075 chan->config.delay = cfg->delay;
1076
1077 if (cfg->coalesc <= XILINX_VDMA_DMACR_FRAME_COUNT_MAX) {
1078 dmacr |= cfg->coalesc << XILINX_VDMA_DMACR_FRAME_COUNT_SHIFT;
1079 chan->config.coalesc = cfg->coalesc;
1080 }
1081
1082 if (cfg->delay <= XILINX_VDMA_DMACR_DELAY_MAX) {
1083 dmacr |= cfg->delay << XILINX_VDMA_DMACR_DELAY_SHIFT;
1084 chan->config.delay = cfg->delay;
1085 }
1086
1087 /* FSync Source selection */
1088 dmacr &= ~XILINX_VDMA_DMACR_FSYNCSRC_MASK;
1089 dmacr |= cfg->ext_fsync << XILINX_VDMA_DMACR_FSYNCSRC_SHIFT;
1090
1091 vdma_ctrl_write(chan, XILINX_VDMA_REG_DMACR, dmacr);
1092
1093 return 0;
1094}
1095EXPORT_SYMBOL(xilinx_vdma_channel_set_config);
1096
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301097/* -----------------------------------------------------------------------------
1098 * Probe and remove
1099 */
1100
1101/**
1102 * xilinx_vdma_chan_remove - Per Channel remove function
1103 * @chan: Driver specific VDMA channel
1104 */
1105static void xilinx_vdma_chan_remove(struct xilinx_vdma_chan *chan)
1106{
1107 /* Disable all interrupts */
1108 vdma_ctrl_clr(chan, XILINX_VDMA_REG_DMACR,
1109 XILINX_VDMA_DMAXR_ALL_IRQ_MASK);
1110
1111 if (chan->irq > 0)
1112 free_irq(chan->irq, chan);
1113
1114 tasklet_kill(&chan->tasklet);
1115
1116 list_del(&chan->common.device_node);
1117}
1118
1119/**
1120 * xilinx_vdma_chan_probe - Per Channel Probing
1121 * It get channel features from the device tree entry and
1122 * initialize special channel handling routines
1123 *
1124 * @xdev: Driver specific device structure
1125 * @node: Device node
1126 *
1127 * Return: '0' on success and failure value on error
1128 */
1129static int xilinx_vdma_chan_probe(struct xilinx_vdma_device *xdev,
1130 struct device_node *node)
1131{
1132 struct xilinx_vdma_chan *chan;
1133 bool has_dre = false;
1134 u32 value, width;
1135 int err;
1136
1137 /* Allocate and initialize the channel structure */
1138 chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
1139 if (!chan)
1140 return -ENOMEM;
1141
1142 chan->dev = xdev->dev;
1143 chan->xdev = xdev;
1144 chan->has_sg = xdev->has_sg;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301145 chan->desc_pendingcount = 0x0;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301146
1147 spin_lock_init(&chan->lock);
1148 INIT_LIST_HEAD(&chan->pending_list);
1149 INIT_LIST_HEAD(&chan->done_list);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301150 INIT_LIST_HEAD(&chan->active_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301151
1152 /* Retrieve the channel properties from the device tree */
1153 has_dre = of_property_read_bool(node, "xlnx,include-dre");
1154
1155 chan->genlock = of_property_read_bool(node, "xlnx,genlock-mode");
1156
1157 err = of_property_read_u32(node, "xlnx,datawidth", &value);
1158 if (err) {
1159 dev_err(xdev->dev, "missing xlnx,datawidth property\n");
1160 return err;
1161 }
1162 width = value >> 3; /* Convert bits to bytes */
1163
1164 /* If data width is greater than 8 bytes, DRE is not in hw */
1165 if (width > 8)
1166 has_dre = false;
1167
1168 if (!has_dre)
1169 xdev->common.copy_align = fls(width - 1);
1170
1171 if (of_device_is_compatible(node, "xlnx,axi-vdma-mm2s-channel")) {
1172 chan->direction = DMA_MEM_TO_DEV;
1173 chan->id = 0;
1174
1175 chan->ctrl_offset = XILINX_VDMA_MM2S_CTRL_OFFSET;
1176 chan->desc_offset = XILINX_VDMA_MM2S_DESC_OFFSET;
1177
1178 if (xdev->flush_on_fsync == XILINX_VDMA_FLUSH_BOTH ||
1179 xdev->flush_on_fsync == XILINX_VDMA_FLUSH_MM2S)
1180 chan->flush_on_fsync = true;
1181 } else if (of_device_is_compatible(node,
1182 "xlnx,axi-vdma-s2mm-channel")) {
1183 chan->direction = DMA_DEV_TO_MEM;
1184 chan->id = 1;
1185
1186 chan->ctrl_offset = XILINX_VDMA_S2MM_CTRL_OFFSET;
1187 chan->desc_offset = XILINX_VDMA_S2MM_DESC_OFFSET;
1188
1189 if (xdev->flush_on_fsync == XILINX_VDMA_FLUSH_BOTH ||
1190 xdev->flush_on_fsync == XILINX_VDMA_FLUSH_S2MM)
1191 chan->flush_on_fsync = true;
1192 } else {
1193 dev_err(xdev->dev, "Invalid channel compatible node\n");
1194 return -EINVAL;
1195 }
1196
1197 /* Request the interrupt */
1198 chan->irq = irq_of_parse_and_map(node, 0);
1199 err = request_irq(chan->irq, xilinx_vdma_irq_handler, IRQF_SHARED,
1200 "xilinx-vdma-controller", chan);
1201 if (err) {
1202 dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq);
1203 return err;
1204 }
1205
1206 /* Initialize the tasklet */
1207 tasklet_init(&chan->tasklet, xilinx_vdma_do_tasklet,
1208 (unsigned long)chan);
1209
1210 /*
1211 * Initialize the DMA channel and add it to the DMA engine channels
1212 * list.
1213 */
1214 chan->common.device = &xdev->common;
1215
1216 list_add_tail(&chan->common.device_node, &xdev->common.channels);
1217 xdev->chan[chan->id] = chan;
1218
1219 /* Reset the channel */
1220 err = xilinx_vdma_chan_reset(chan);
1221 if (err < 0) {
1222 dev_err(xdev->dev, "Reset channel failed\n");
1223 return err;
1224 }
1225
1226 return 0;
1227}
1228
1229/**
1230 * of_dma_xilinx_xlate - Translation function
1231 * @dma_spec: Pointer to DMA specifier as found in the device tree
1232 * @ofdma: Pointer to DMA controller data
1233 *
1234 * Return: DMA channel pointer on success and NULL on error
1235 */
1236static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
1237 struct of_dma *ofdma)
1238{
1239 struct xilinx_vdma_device *xdev = ofdma->of_dma_data;
1240 int chan_id = dma_spec->args[0];
1241
1242 if (chan_id >= XILINX_VDMA_MAX_CHANS_PER_DEVICE)
1243 return NULL;
1244
1245 return dma_get_slave_channel(&xdev->chan[chan_id]->common);
1246}
1247
1248/**
1249 * xilinx_vdma_probe - Driver probe function
1250 * @pdev: Pointer to the platform_device structure
1251 *
1252 * Return: '0' on success and failure value on error
1253 */
1254static int xilinx_vdma_probe(struct platform_device *pdev)
1255{
1256 struct device_node *node = pdev->dev.of_node;
1257 struct xilinx_vdma_device *xdev;
1258 struct device_node *child;
1259 struct resource *io;
1260 u32 num_frames;
1261 int i, err;
1262
1263 /* Allocate and initialize the DMA engine structure */
1264 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
1265 if (!xdev)
1266 return -ENOMEM;
1267
1268 xdev->dev = &pdev->dev;
1269
1270 /* Request and map I/O memory */
1271 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1272 xdev->regs = devm_ioremap_resource(&pdev->dev, io);
1273 if (IS_ERR(xdev->regs))
1274 return PTR_ERR(xdev->regs);
1275
1276 /* Retrieve the DMA engine properties from the device tree */
1277 xdev->has_sg = of_property_read_bool(node, "xlnx,include-sg");
1278
1279 err = of_property_read_u32(node, "xlnx,num-fstores", &num_frames);
1280 if (err < 0) {
1281 dev_err(xdev->dev, "missing xlnx,num-fstores property\n");
1282 return err;
1283 }
1284
1285 err = of_property_read_u32(node, "xlnx,flush-fsync",
1286 &xdev->flush_on_fsync);
1287 if (err < 0)
1288 dev_warn(xdev->dev, "missing xlnx,flush-fsync property\n");
1289
1290 /* Initialize the DMA engine */
1291 xdev->common.dev = &pdev->dev;
1292
1293 INIT_LIST_HEAD(&xdev->common.channels);
1294 dma_cap_set(DMA_SLAVE, xdev->common.cap_mask);
1295 dma_cap_set(DMA_PRIVATE, xdev->common.cap_mask);
1296
1297 xdev->common.device_alloc_chan_resources =
1298 xilinx_vdma_alloc_chan_resources;
1299 xdev->common.device_free_chan_resources =
1300 xilinx_vdma_free_chan_resources;
1301 xdev->common.device_prep_interleaved_dma =
1302 xilinx_vdma_dma_prep_interleaved;
Maxime Ripardba714042014-11-17 14:42:38 +01001303 xdev->common.device_terminate_all = xilinx_vdma_terminate_all;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301304 xdev->common.device_tx_status = xilinx_vdma_tx_status;
1305 xdev->common.device_issue_pending = xilinx_vdma_issue_pending;
1306
1307 platform_set_drvdata(pdev, xdev);
1308
1309 /* Initialize the channels */
1310 for_each_child_of_node(node, child) {
1311 err = xilinx_vdma_chan_probe(xdev, child);
1312 if (err < 0)
1313 goto error;
1314 }
1315
1316 for (i = 0; i < XILINX_VDMA_MAX_CHANS_PER_DEVICE; i++)
1317 if (xdev->chan[i])
1318 xdev->chan[i]->num_frms = num_frames;
1319
1320 /* Register the DMA engine with the core */
1321 dma_async_device_register(&xdev->common);
1322
1323 err = of_dma_controller_register(node, of_dma_xilinx_xlate,
1324 xdev);
1325 if (err < 0) {
1326 dev_err(&pdev->dev, "Unable to register DMA to DT\n");
1327 dma_async_device_unregister(&xdev->common);
1328 goto error;
1329 }
1330
1331 dev_info(&pdev->dev, "Xilinx AXI VDMA Engine Driver Probed!!\n");
1332
1333 return 0;
1334
1335error:
1336 for (i = 0; i < XILINX_VDMA_MAX_CHANS_PER_DEVICE; i++)
1337 if (xdev->chan[i])
1338 xilinx_vdma_chan_remove(xdev->chan[i]);
1339
1340 return err;
1341}
1342
1343/**
1344 * xilinx_vdma_remove - Driver remove function
1345 * @pdev: Pointer to the platform_device structure
1346 *
1347 * Return: Always '0'
1348 */
1349static int xilinx_vdma_remove(struct platform_device *pdev)
1350{
1351 struct xilinx_vdma_device *xdev = platform_get_drvdata(pdev);
1352 int i;
1353
1354 of_dma_controller_free(pdev->dev.of_node);
1355
1356 dma_async_device_unregister(&xdev->common);
1357
1358 for (i = 0; i < XILINX_VDMA_MAX_CHANS_PER_DEVICE; i++)
1359 if (xdev->chan[i])
1360 xilinx_vdma_chan_remove(xdev->chan[i]);
1361
1362 return 0;
1363}
1364
1365static const struct of_device_id xilinx_vdma_of_ids[] = {
1366 { .compatible = "xlnx,axi-vdma-1.00.a",},
1367 {}
1368};
Luis de Bethencourtad577e42015-09-16 23:00:17 +02001369MODULE_DEVICE_TABLE(of, xilinx_vdma_of_ids);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301370
1371static struct platform_driver xilinx_vdma_driver = {
1372 .driver = {
1373 .name = "xilinx-vdma",
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301374 .of_match_table = xilinx_vdma_of_ids,
1375 },
1376 .probe = xilinx_vdma_probe,
1377 .remove = xilinx_vdma_remove,
1378};
1379
1380module_platform_driver(xilinx_vdma_driver);
1381
1382MODULE_AUTHOR("Xilinx, Inc.");
1383MODULE_DESCRIPTION("Xilinx VDMA driver");
1384MODULE_LICENSE("GPL v2");