blob: e96ff9dea40da7f37075b1e5704c1a966b46a05a [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
Kedareswara rao Appanae2b538a2016-02-26 19:33:53 +0530651 /* Configure channel to allow number frame buffers */
652 vdma_ctrl_write(chan, XILINX_VDMA_REG_FRMSTORE,
653 chan->desc_pendingcount);
654
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530655 /*
656 * With SG, start with circular mode, so that BDs can be fetched.
657 * In direct register mode, if not parking, enable circular mode
658 */
659 if (chan->has_sg || !config->park)
660 reg |= XILINX_VDMA_DMACR_CIRC_EN;
661
662 if (config->park)
663 reg &= ~XILINX_VDMA_DMACR_CIRC_EN;
664
665 vdma_ctrl_write(chan, XILINX_VDMA_REG_DMACR, reg);
666
667 if (config->park && (config->park_frm >= 0) &&
668 (config->park_frm < chan->num_frms)) {
669 if (chan->direction == DMA_MEM_TO_DEV)
670 vdma_write(chan, XILINX_VDMA_REG_PARK_PTR,
671 config->park_frm <<
672 XILINX_VDMA_PARK_PTR_RD_REF_SHIFT);
673 else
674 vdma_write(chan, XILINX_VDMA_REG_PARK_PTR,
675 config->park_frm <<
676 XILINX_VDMA_PARK_PTR_WR_REF_SHIFT);
677 }
678
679 /* Start the hardware */
680 xilinx_vdma_start(chan);
681
682 if (chan->err)
683 goto out_unlock;
684
685 /* Start the transfer */
686 if (chan->has_sg) {
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530687 vdma_ctrl_write(chan, XILINX_VDMA_REG_TAILDESC,
688 tail_segment->phys);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530689 } else {
690 struct xilinx_vdma_tx_segment *segment, *last = NULL;
691 int i = 0;
692
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530693 list_for_each_entry(desc, &chan->pending_list, node) {
694 segment = list_first_entry(&desc->segments,
695 struct xilinx_vdma_tx_segment, node);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530696 vdma_desc_write(chan,
697 XILINX_VDMA_REG_START_ADDRESS(i++),
698 segment->hw.buf_addr);
699 last = segment;
700 }
701
702 if (!last)
703 goto out_unlock;
704
705 /* HW expects these parameters to be same for one transaction */
706 vdma_desc_write(chan, XILINX_VDMA_REG_HSIZE, last->hw.hsize);
707 vdma_desc_write(chan, XILINX_VDMA_REG_FRMDLY_STRIDE,
708 last->hw.stride);
709 vdma_desc_write(chan, XILINX_VDMA_REG_VSIZE, last->hw.vsize);
710 }
711
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530712 list_splice_tail_init(&chan->pending_list, &chan->active_list);
713 chan->desc_pendingcount = 0;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530714
715out_unlock:
716 spin_unlock_irqrestore(&chan->lock, flags);
717}
718
719/**
720 * xilinx_vdma_issue_pending - Issue pending transactions
721 * @dchan: DMA channel
722 */
723static void xilinx_vdma_issue_pending(struct dma_chan *dchan)
724{
725 struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
726
727 xilinx_vdma_start_transfer(chan);
728}
729
730/**
731 * xilinx_vdma_complete_descriptor - Mark the active descriptor as complete
732 * @chan : xilinx DMA channel
733 *
734 * CONTEXT: hardirq
735 */
736static void xilinx_vdma_complete_descriptor(struct xilinx_vdma_chan *chan)
737{
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530738 struct xilinx_vdma_tx_descriptor *desc, *next;
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530739 unsigned long flags;
740
741 spin_lock_irqsave(&chan->lock, flags);
742
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530743 if (list_empty(&chan->active_list))
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530744 goto out_unlock;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530745
746 list_for_each_entry_safe(desc, next, &chan->active_list, node) {
747 list_del(&desc->node);
748 dma_cookie_complete(&desc->async_tx);
749 list_add_tail(&desc->node, &chan->done_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530750 }
751
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530752out_unlock:
753 spin_unlock_irqrestore(&chan->lock, flags);
754}
755
756/**
757 * xilinx_vdma_reset - Reset VDMA channel
758 * @chan: Driver specific VDMA channel
759 *
760 * Return: '0' on success and failure value on error
761 */
762static int xilinx_vdma_reset(struct xilinx_vdma_chan *chan)
763{
764 int loop = XILINX_VDMA_LOOP_COUNT;
765 u32 tmp;
766
767 vdma_ctrl_set(chan, XILINX_VDMA_REG_DMACR, XILINX_VDMA_DMACR_RESET);
768
769 tmp = vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR) &
770 XILINX_VDMA_DMACR_RESET;
771
772 /* Wait for the hardware to finish reset */
773 do {
774 tmp = vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR) &
775 XILINX_VDMA_DMACR_RESET;
776 } while (loop-- && tmp);
777
778 if (!loop) {
779 dev_err(chan->dev, "reset timeout, cr %x, sr %x\n",
780 vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR),
781 vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR));
782 return -ETIMEDOUT;
783 }
784
785 chan->err = false;
786
787 return 0;
788}
789
790/**
791 * xilinx_vdma_chan_reset - Reset VDMA channel and enable interrupts
792 * @chan: Driver specific VDMA channel
793 *
794 * Return: '0' on success and failure value on error
795 */
796static int xilinx_vdma_chan_reset(struct xilinx_vdma_chan *chan)
797{
798 int err;
799
800 /* Reset VDMA */
801 err = xilinx_vdma_reset(chan);
802 if (err)
803 return err;
804
805 /* Enable interrupts */
806 vdma_ctrl_set(chan, XILINX_VDMA_REG_DMACR,
807 XILINX_VDMA_DMAXR_ALL_IRQ_MASK);
808
809 return 0;
810}
811
812/**
813 * xilinx_vdma_irq_handler - VDMA Interrupt handler
814 * @irq: IRQ number
815 * @data: Pointer to the Xilinx VDMA channel structure
816 *
817 * Return: IRQ_HANDLED/IRQ_NONE
818 */
819static irqreturn_t xilinx_vdma_irq_handler(int irq, void *data)
820{
821 struct xilinx_vdma_chan *chan = data;
822 u32 status;
823
824 /* Read the status and ack the interrupts. */
825 status = vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR);
826 if (!(status & XILINX_VDMA_DMAXR_ALL_IRQ_MASK))
827 return IRQ_NONE;
828
829 vdma_ctrl_write(chan, XILINX_VDMA_REG_DMASR,
830 status & XILINX_VDMA_DMAXR_ALL_IRQ_MASK);
831
832 if (status & XILINX_VDMA_DMASR_ERR_IRQ) {
833 /*
834 * An error occurred. If C_FLUSH_ON_FSYNC is enabled and the
835 * error is recoverable, ignore it. Otherwise flag the error.
836 *
837 * Only recoverable errors can be cleared in the DMASR register,
838 * make sure not to write to other error bits to 1.
839 */
840 u32 errors = status & XILINX_VDMA_DMASR_ALL_ERR_MASK;
841 vdma_ctrl_write(chan, XILINX_VDMA_REG_DMASR,
842 errors & XILINX_VDMA_DMASR_ERR_RECOVER_MASK);
843
844 if (!chan->flush_on_fsync ||
845 (errors & ~XILINX_VDMA_DMASR_ERR_RECOVER_MASK)) {
846 dev_err(chan->dev,
847 "Channel %p has errors %x, cdr %x tdr %x\n",
848 chan, errors,
849 vdma_ctrl_read(chan, XILINX_VDMA_REG_CURDESC),
850 vdma_ctrl_read(chan, XILINX_VDMA_REG_TAILDESC));
851 chan->err = true;
852 }
853 }
854
855 if (status & XILINX_VDMA_DMASR_DLY_CNT_IRQ) {
856 /*
857 * Device takes too long to do the transfer when user requires
858 * responsiveness.
859 */
860 dev_dbg(chan->dev, "Inter-packet latency too long\n");
861 }
862
863 if (status & XILINX_VDMA_DMASR_FRM_CNT_IRQ) {
864 xilinx_vdma_complete_descriptor(chan);
865 xilinx_vdma_start_transfer(chan);
866 }
867
868 tasklet_schedule(&chan->tasklet);
869 return IRQ_HANDLED;
870}
871
872/**
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530873 * append_desc_queue - Queuing descriptor
874 * @chan: Driver specific dma channel
875 * @desc: dma transaction descriptor
876 */
877static void append_desc_queue(struct xilinx_vdma_chan *chan,
878 struct xilinx_vdma_tx_descriptor *desc)
879{
880 struct xilinx_vdma_tx_segment *tail_segment;
881 struct xilinx_vdma_tx_descriptor *tail_desc;
882
883 if (list_empty(&chan->pending_list))
884 goto append;
885
886 /*
887 * Add the hardware descriptor to the chain of hardware descriptors
888 * that already exists in memory.
889 */
890 tail_desc = list_last_entry(&chan->pending_list,
891 struct xilinx_vdma_tx_descriptor, node);
892 tail_segment = list_last_entry(&tail_desc->segments,
893 struct xilinx_vdma_tx_segment, node);
894 tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
895
896 /*
897 * Add the software descriptor and all children to the list
898 * of pending transactions
899 */
900append:
901 list_add_tail(&desc->node, &chan->pending_list);
902 chan->desc_pendingcount++;
903
904 if (unlikely(chan->desc_pendingcount > chan->num_frms)) {
905 dev_dbg(chan->dev, "desc pendingcount is too high\n");
906 chan->desc_pendingcount = chan->num_frms;
907 }
908}
909
910/**
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530911 * xilinx_vdma_tx_submit - Submit DMA transaction
912 * @tx: Async transaction descriptor
913 *
914 * Return: cookie value on success and failure value on error
915 */
916static dma_cookie_t xilinx_vdma_tx_submit(struct dma_async_tx_descriptor *tx)
917{
918 struct xilinx_vdma_tx_descriptor *desc = to_vdma_tx_descriptor(tx);
919 struct xilinx_vdma_chan *chan = to_xilinx_chan(tx->chan);
920 dma_cookie_t cookie;
921 unsigned long flags;
922 int err;
923
924 if (chan->err) {
925 /*
926 * If reset fails, need to hard reset the system.
927 * Channel is no longer functional
928 */
929 err = xilinx_vdma_chan_reset(chan);
930 if (err < 0)
931 return err;
932 }
933
934 spin_lock_irqsave(&chan->lock, flags);
935
936 cookie = dma_cookie_assign(tx);
937
Kedareswara rao Appana7096f362016-02-26 19:33:51 +0530938 /* Put this transaction onto the tail of the pending queue */
939 append_desc_queue(chan, desc);
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530940
941 spin_unlock_irqrestore(&chan->lock, flags);
942
943 return cookie;
944}
945
946/**
947 * xilinx_vdma_dma_prep_interleaved - prepare a descriptor for a
948 * DMA_SLAVE transaction
949 * @dchan: DMA channel
950 * @xt: Interleaved template pointer
951 * @flags: transfer ack flags
952 *
953 * Return: Async transaction descriptor on success and NULL on failure
954 */
955static struct dma_async_tx_descriptor *
956xilinx_vdma_dma_prep_interleaved(struct dma_chan *dchan,
957 struct dma_interleaved_template *xt,
958 unsigned long flags)
959{
960 struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
961 struct xilinx_vdma_tx_descriptor *desc;
962 struct xilinx_vdma_tx_segment *segment, *prev = NULL;
963 struct xilinx_vdma_desc_hw *hw;
964
965 if (!is_slave_direction(xt->dir))
966 return NULL;
967
968 if (!xt->numf || !xt->sgl[0].size)
969 return NULL;
970
Srikanth Thokalaa5e48e22014-11-05 20:37:01 +0200971 if (xt->frame_size != 1)
972 return NULL;
973
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530974 /* Allocate a transaction descriptor. */
975 desc = xilinx_vdma_alloc_tx_descriptor(chan);
976 if (!desc)
977 return NULL;
978
979 dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
980 desc->async_tx.tx_submit = xilinx_vdma_tx_submit;
981 async_tx_ack(&desc->async_tx);
982
983 /* Allocate the link descriptor from DMA pool */
984 segment = xilinx_vdma_alloc_tx_segment(chan);
985 if (!segment)
986 goto error;
987
988 /* Fill in the hardware descriptor */
989 hw = &segment->hw;
990 hw->vsize = xt->numf;
991 hw->hsize = xt->sgl[0].size;
Srikanth Thokala6d80f452014-11-05 20:37:02 +0200992 hw->stride = (xt->sgl[0].icg + xt->sgl[0].size) <<
Srikanth Thokala9cd43602014-04-23 20:23:26 +0530993 XILINX_VDMA_FRMDLY_STRIDE_STRIDE_SHIFT;
994 hw->stride |= chan->config.frm_dly <<
995 XILINX_VDMA_FRMDLY_STRIDE_FRMDLY_SHIFT;
996
997 if (xt->dir != DMA_MEM_TO_DEV)
998 hw->buf_addr = xt->dst_start;
999 else
1000 hw->buf_addr = xt->src_start;
1001
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301002 /* Insert the segment into the descriptor segments list. */
1003 list_add_tail(&segment->node, &desc->segments);
1004
1005 prev = segment;
1006
1007 /* Link the last hardware descriptor with the first. */
1008 segment = list_first_entry(&desc->segments,
1009 struct xilinx_vdma_tx_segment, node);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301010 desc->async_tx.phys = segment->phys;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301011
1012 return &desc->async_tx;
1013
1014error:
1015 xilinx_vdma_free_tx_descriptor(chan, desc);
1016 return NULL;
1017}
1018
1019/**
1020 * xilinx_vdma_terminate_all - Halt the channel and free descriptors
1021 * @chan: Driver specific VDMA Channel pointer
1022 */
Maxime Ripardba714042014-11-17 14:42:38 +01001023static int xilinx_vdma_terminate_all(struct dma_chan *dchan)
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301024{
Maxime Ripardba714042014-11-17 14:42:38 +01001025 struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
1026
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301027 /* Halt the DMA engine */
1028 xilinx_vdma_halt(chan);
1029
1030 /* Remove and free all of the descriptors in the lists */
1031 xilinx_vdma_free_descriptors(chan);
Maxime Ripardba714042014-11-17 14:42:38 +01001032
1033 return 0;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301034}
1035
1036/**
1037 * xilinx_vdma_channel_set_config - Configure VDMA channel
1038 * Run-time configuration for Axi VDMA, supports:
1039 * . halt the channel
1040 * . configure interrupt coalescing and inter-packet delay threshold
1041 * . start/stop parking
1042 * . enable genlock
1043 *
1044 * @dchan: DMA channel
1045 * @cfg: VDMA device configuration pointer
1046 *
1047 * Return: '0' on success and failure value on error
1048 */
1049int xilinx_vdma_channel_set_config(struct dma_chan *dchan,
1050 struct xilinx_vdma_config *cfg)
1051{
1052 struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
1053 u32 dmacr;
1054
1055 if (cfg->reset)
1056 return xilinx_vdma_chan_reset(chan);
1057
1058 dmacr = vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR);
1059
1060 chan->config.frm_dly = cfg->frm_dly;
1061 chan->config.park = cfg->park;
1062
1063 /* genlock settings */
1064 chan->config.gen_lock = cfg->gen_lock;
1065 chan->config.master = cfg->master;
1066
1067 if (cfg->gen_lock && chan->genlock) {
1068 dmacr |= XILINX_VDMA_DMACR_GENLOCK_EN;
1069 dmacr |= cfg->master << XILINX_VDMA_DMACR_MASTER_SHIFT;
1070 }
1071
1072 chan->config.frm_cnt_en = cfg->frm_cnt_en;
1073 if (cfg->park)
1074 chan->config.park_frm = cfg->park_frm;
1075 else
1076 chan->config.park_frm = -1;
1077
1078 chan->config.coalesc = cfg->coalesc;
1079 chan->config.delay = cfg->delay;
1080
1081 if (cfg->coalesc <= XILINX_VDMA_DMACR_FRAME_COUNT_MAX) {
1082 dmacr |= cfg->coalesc << XILINX_VDMA_DMACR_FRAME_COUNT_SHIFT;
1083 chan->config.coalesc = cfg->coalesc;
1084 }
1085
1086 if (cfg->delay <= XILINX_VDMA_DMACR_DELAY_MAX) {
1087 dmacr |= cfg->delay << XILINX_VDMA_DMACR_DELAY_SHIFT;
1088 chan->config.delay = cfg->delay;
1089 }
1090
1091 /* FSync Source selection */
1092 dmacr &= ~XILINX_VDMA_DMACR_FSYNCSRC_MASK;
1093 dmacr |= cfg->ext_fsync << XILINX_VDMA_DMACR_FSYNCSRC_SHIFT;
1094
1095 vdma_ctrl_write(chan, XILINX_VDMA_REG_DMACR, dmacr);
1096
1097 return 0;
1098}
1099EXPORT_SYMBOL(xilinx_vdma_channel_set_config);
1100
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301101/* -----------------------------------------------------------------------------
1102 * Probe and remove
1103 */
1104
1105/**
1106 * xilinx_vdma_chan_remove - Per Channel remove function
1107 * @chan: Driver specific VDMA channel
1108 */
1109static void xilinx_vdma_chan_remove(struct xilinx_vdma_chan *chan)
1110{
1111 /* Disable all interrupts */
1112 vdma_ctrl_clr(chan, XILINX_VDMA_REG_DMACR,
1113 XILINX_VDMA_DMAXR_ALL_IRQ_MASK);
1114
1115 if (chan->irq > 0)
1116 free_irq(chan->irq, chan);
1117
1118 tasklet_kill(&chan->tasklet);
1119
1120 list_del(&chan->common.device_node);
1121}
1122
1123/**
1124 * xilinx_vdma_chan_probe - Per Channel Probing
1125 * It get channel features from the device tree entry and
1126 * initialize special channel handling routines
1127 *
1128 * @xdev: Driver specific device structure
1129 * @node: Device node
1130 *
1131 * Return: '0' on success and failure value on error
1132 */
1133static int xilinx_vdma_chan_probe(struct xilinx_vdma_device *xdev,
1134 struct device_node *node)
1135{
1136 struct xilinx_vdma_chan *chan;
1137 bool has_dre = false;
1138 u32 value, width;
1139 int err;
1140
1141 /* Allocate and initialize the channel structure */
1142 chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
1143 if (!chan)
1144 return -ENOMEM;
1145
1146 chan->dev = xdev->dev;
1147 chan->xdev = xdev;
1148 chan->has_sg = xdev->has_sg;
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301149 chan->desc_pendingcount = 0x0;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301150
1151 spin_lock_init(&chan->lock);
1152 INIT_LIST_HEAD(&chan->pending_list);
1153 INIT_LIST_HEAD(&chan->done_list);
Kedareswara rao Appana7096f362016-02-26 19:33:51 +05301154 INIT_LIST_HEAD(&chan->active_list);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301155
1156 /* Retrieve the channel properties from the device tree */
1157 has_dre = of_property_read_bool(node, "xlnx,include-dre");
1158
1159 chan->genlock = of_property_read_bool(node, "xlnx,genlock-mode");
1160
1161 err = of_property_read_u32(node, "xlnx,datawidth", &value);
1162 if (err) {
1163 dev_err(xdev->dev, "missing xlnx,datawidth property\n");
1164 return err;
1165 }
1166 width = value >> 3; /* Convert bits to bytes */
1167
1168 /* If data width is greater than 8 bytes, DRE is not in hw */
1169 if (width > 8)
1170 has_dre = false;
1171
1172 if (!has_dre)
1173 xdev->common.copy_align = fls(width - 1);
1174
1175 if (of_device_is_compatible(node, "xlnx,axi-vdma-mm2s-channel")) {
1176 chan->direction = DMA_MEM_TO_DEV;
1177 chan->id = 0;
1178
1179 chan->ctrl_offset = XILINX_VDMA_MM2S_CTRL_OFFSET;
1180 chan->desc_offset = XILINX_VDMA_MM2S_DESC_OFFSET;
1181
1182 if (xdev->flush_on_fsync == XILINX_VDMA_FLUSH_BOTH ||
1183 xdev->flush_on_fsync == XILINX_VDMA_FLUSH_MM2S)
1184 chan->flush_on_fsync = true;
1185 } else if (of_device_is_compatible(node,
1186 "xlnx,axi-vdma-s2mm-channel")) {
1187 chan->direction = DMA_DEV_TO_MEM;
1188 chan->id = 1;
1189
1190 chan->ctrl_offset = XILINX_VDMA_S2MM_CTRL_OFFSET;
1191 chan->desc_offset = XILINX_VDMA_S2MM_DESC_OFFSET;
1192
1193 if (xdev->flush_on_fsync == XILINX_VDMA_FLUSH_BOTH ||
1194 xdev->flush_on_fsync == XILINX_VDMA_FLUSH_S2MM)
1195 chan->flush_on_fsync = true;
1196 } else {
1197 dev_err(xdev->dev, "Invalid channel compatible node\n");
1198 return -EINVAL;
1199 }
1200
1201 /* Request the interrupt */
1202 chan->irq = irq_of_parse_and_map(node, 0);
1203 err = request_irq(chan->irq, xilinx_vdma_irq_handler, IRQF_SHARED,
1204 "xilinx-vdma-controller", chan);
1205 if (err) {
1206 dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq);
1207 return err;
1208 }
1209
1210 /* Initialize the tasklet */
1211 tasklet_init(&chan->tasklet, xilinx_vdma_do_tasklet,
1212 (unsigned long)chan);
1213
1214 /*
1215 * Initialize the DMA channel and add it to the DMA engine channels
1216 * list.
1217 */
1218 chan->common.device = &xdev->common;
1219
1220 list_add_tail(&chan->common.device_node, &xdev->common.channels);
1221 xdev->chan[chan->id] = chan;
1222
1223 /* Reset the channel */
1224 err = xilinx_vdma_chan_reset(chan);
1225 if (err < 0) {
1226 dev_err(xdev->dev, "Reset channel failed\n");
1227 return err;
1228 }
1229
1230 return 0;
1231}
1232
1233/**
1234 * of_dma_xilinx_xlate - Translation function
1235 * @dma_spec: Pointer to DMA specifier as found in the device tree
1236 * @ofdma: Pointer to DMA controller data
1237 *
1238 * Return: DMA channel pointer on success and NULL on error
1239 */
1240static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
1241 struct of_dma *ofdma)
1242{
1243 struct xilinx_vdma_device *xdev = ofdma->of_dma_data;
1244 int chan_id = dma_spec->args[0];
1245
1246 if (chan_id >= XILINX_VDMA_MAX_CHANS_PER_DEVICE)
1247 return NULL;
1248
1249 return dma_get_slave_channel(&xdev->chan[chan_id]->common);
1250}
1251
1252/**
1253 * xilinx_vdma_probe - Driver probe function
1254 * @pdev: Pointer to the platform_device structure
1255 *
1256 * Return: '0' on success and failure value on error
1257 */
1258static int xilinx_vdma_probe(struct platform_device *pdev)
1259{
1260 struct device_node *node = pdev->dev.of_node;
1261 struct xilinx_vdma_device *xdev;
1262 struct device_node *child;
1263 struct resource *io;
1264 u32 num_frames;
1265 int i, err;
1266
1267 /* Allocate and initialize the DMA engine structure */
1268 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
1269 if (!xdev)
1270 return -ENOMEM;
1271
1272 xdev->dev = &pdev->dev;
1273
1274 /* Request and map I/O memory */
1275 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1276 xdev->regs = devm_ioremap_resource(&pdev->dev, io);
1277 if (IS_ERR(xdev->regs))
1278 return PTR_ERR(xdev->regs);
1279
1280 /* Retrieve the DMA engine properties from the device tree */
1281 xdev->has_sg = of_property_read_bool(node, "xlnx,include-sg");
1282
1283 err = of_property_read_u32(node, "xlnx,num-fstores", &num_frames);
1284 if (err < 0) {
1285 dev_err(xdev->dev, "missing xlnx,num-fstores property\n");
1286 return err;
1287 }
1288
1289 err = of_property_read_u32(node, "xlnx,flush-fsync",
1290 &xdev->flush_on_fsync);
1291 if (err < 0)
1292 dev_warn(xdev->dev, "missing xlnx,flush-fsync property\n");
1293
1294 /* Initialize the DMA engine */
1295 xdev->common.dev = &pdev->dev;
1296
1297 INIT_LIST_HEAD(&xdev->common.channels);
1298 dma_cap_set(DMA_SLAVE, xdev->common.cap_mask);
1299 dma_cap_set(DMA_PRIVATE, xdev->common.cap_mask);
1300
1301 xdev->common.device_alloc_chan_resources =
1302 xilinx_vdma_alloc_chan_resources;
1303 xdev->common.device_free_chan_resources =
1304 xilinx_vdma_free_chan_resources;
1305 xdev->common.device_prep_interleaved_dma =
1306 xilinx_vdma_dma_prep_interleaved;
Maxime Ripardba714042014-11-17 14:42:38 +01001307 xdev->common.device_terminate_all = xilinx_vdma_terminate_all;
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301308 xdev->common.device_tx_status = xilinx_vdma_tx_status;
1309 xdev->common.device_issue_pending = xilinx_vdma_issue_pending;
1310
1311 platform_set_drvdata(pdev, xdev);
1312
1313 /* Initialize the channels */
1314 for_each_child_of_node(node, child) {
1315 err = xilinx_vdma_chan_probe(xdev, child);
1316 if (err < 0)
1317 goto error;
1318 }
1319
1320 for (i = 0; i < XILINX_VDMA_MAX_CHANS_PER_DEVICE; i++)
1321 if (xdev->chan[i])
1322 xdev->chan[i]->num_frms = num_frames;
1323
1324 /* Register the DMA engine with the core */
1325 dma_async_device_register(&xdev->common);
1326
1327 err = of_dma_controller_register(node, of_dma_xilinx_xlate,
1328 xdev);
1329 if (err < 0) {
1330 dev_err(&pdev->dev, "Unable to register DMA to DT\n");
1331 dma_async_device_unregister(&xdev->common);
1332 goto error;
1333 }
1334
1335 dev_info(&pdev->dev, "Xilinx AXI VDMA Engine Driver Probed!!\n");
1336
1337 return 0;
1338
1339error:
1340 for (i = 0; i < XILINX_VDMA_MAX_CHANS_PER_DEVICE; i++)
1341 if (xdev->chan[i])
1342 xilinx_vdma_chan_remove(xdev->chan[i]);
1343
1344 return err;
1345}
1346
1347/**
1348 * xilinx_vdma_remove - Driver remove function
1349 * @pdev: Pointer to the platform_device structure
1350 *
1351 * Return: Always '0'
1352 */
1353static int xilinx_vdma_remove(struct platform_device *pdev)
1354{
1355 struct xilinx_vdma_device *xdev = platform_get_drvdata(pdev);
1356 int i;
1357
1358 of_dma_controller_free(pdev->dev.of_node);
1359
1360 dma_async_device_unregister(&xdev->common);
1361
1362 for (i = 0; i < XILINX_VDMA_MAX_CHANS_PER_DEVICE; i++)
1363 if (xdev->chan[i])
1364 xilinx_vdma_chan_remove(xdev->chan[i]);
1365
1366 return 0;
1367}
1368
1369static const struct of_device_id xilinx_vdma_of_ids[] = {
1370 { .compatible = "xlnx,axi-vdma-1.00.a",},
1371 {}
1372};
Luis de Bethencourtad577e42015-09-16 23:00:17 +02001373MODULE_DEVICE_TABLE(of, xilinx_vdma_of_ids);
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301374
1375static struct platform_driver xilinx_vdma_driver = {
1376 .driver = {
1377 .name = "xilinx-vdma",
Srikanth Thokala9cd43602014-04-23 20:23:26 +05301378 .of_match_table = xilinx_vdma_of_ids,
1379 },
1380 .probe = xilinx_vdma_probe,
1381 .remove = xilinx_vdma_remove,
1382};
1383
1384module_platform_driver(xilinx_vdma_driver);
1385
1386MODULE_AUTHOR("Xilinx, Inc.");
1387MODULE_DESCRIPTION("Xilinx VDMA driver");
1388MODULE_LICENSE("GPL v2");