blob: cd436c6a29a75a1fca5b5f89806ddc3b07904fbd [file] [log] [blame]
Maxime Ripard55585932014-07-17 21:46:16 +02001/*
2 * Copyright (C) 2013-2014 Allwinner Tech Co., Ltd
3 * Author: Sugar <shuge@allwinnertech.com>
4 *
5 * Copyright (C) 2014 Maxime Ripard
6 * Maxime Ripard <maxime.ripard@free-electrons.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/dmaengine.h>
17#include <linux/dmapool.h>
18#include <linux/interrupt.h>
19#include <linux/module.h>
20#include <linux/of_dma.h>
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +080021#include <linux/of_device.h>
Maxime Ripard55585932014-07-17 21:46:16 +020022#include <linux/platform_device.h>
23#include <linux/reset.h>
24#include <linux/slab.h>
25#include <linux/types.h>
26
27#include "virt-dma.h"
28
29/*
Maxime Ripard55585932014-07-17 21:46:16 +020030 * Common registers
31 */
32#define DMA_IRQ_EN(x) ((x) * 0x04)
33#define DMA_IRQ_HALF BIT(0)
34#define DMA_IRQ_PKG BIT(1)
35#define DMA_IRQ_QUEUE BIT(2)
36
37#define DMA_IRQ_CHAN_NR 8
38#define DMA_IRQ_CHAN_WIDTH 4
39
40
41#define DMA_IRQ_STAT(x) ((x) * 0x04 + 0x10)
42
43#define DMA_STAT 0x30
44
45/*
Chen-Yu Tsai0b04ddf2014-11-07 12:15:47 +080046 * sun8i specific registers
47 */
48#define SUN8I_DMA_GATE 0x20
49#define SUN8I_DMA_GATE_ENABLE 0x4
50
51/*
Maxime Ripard55585932014-07-17 21:46:16 +020052 * Channels specific registers
53 */
54#define DMA_CHAN_ENABLE 0x00
55#define DMA_CHAN_ENABLE_START BIT(0)
56#define DMA_CHAN_ENABLE_STOP 0
57
58#define DMA_CHAN_PAUSE 0x04
59#define DMA_CHAN_PAUSE_PAUSE BIT(1)
60#define DMA_CHAN_PAUSE_RESUME 0
61
62#define DMA_CHAN_LLI_ADDR 0x08
63
64#define DMA_CHAN_CUR_CFG 0x0c
65#define DMA_CHAN_CFG_SRC_DRQ(x) ((x) & 0x1f)
66#define DMA_CHAN_CFG_SRC_IO_MODE BIT(5)
67#define DMA_CHAN_CFG_SRC_LINEAR_MODE (0 << 5)
68#define DMA_CHAN_CFG_SRC_BURST(x) (((x) & 0x3) << 7)
69#define DMA_CHAN_CFG_SRC_WIDTH(x) (((x) & 0x3) << 9)
70
71#define DMA_CHAN_CFG_DST_DRQ(x) (DMA_CHAN_CFG_SRC_DRQ(x) << 16)
72#define DMA_CHAN_CFG_DST_IO_MODE (DMA_CHAN_CFG_SRC_IO_MODE << 16)
73#define DMA_CHAN_CFG_DST_LINEAR_MODE (DMA_CHAN_CFG_SRC_LINEAR_MODE << 16)
74#define DMA_CHAN_CFG_DST_BURST(x) (DMA_CHAN_CFG_SRC_BURST(x) << 16)
75#define DMA_CHAN_CFG_DST_WIDTH(x) (DMA_CHAN_CFG_SRC_WIDTH(x) << 16)
76
77#define DMA_CHAN_CUR_SRC 0x10
78
79#define DMA_CHAN_CUR_DST 0x14
80
81#define DMA_CHAN_CUR_CNT 0x18
82
83#define DMA_CHAN_CUR_PARA 0x1c
84
85
86/*
87 * Various hardware related defines
88 */
89#define LLI_LAST_ITEM 0xfffff800
90#define NORMAL_WAIT 8
91#define DRQ_SDRAM 1
92
93/*
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +080094 * Hardware channels / ports representation
95 *
96 * The hardware is used in several SoCs, with differing numbers
97 * of channels and endpoints. This structure ties those numbers
98 * to a certain compatible string.
99 */
100struct sun6i_dma_config {
101 u32 nr_max_channels;
102 u32 nr_max_requests;
103 u32 nr_max_vchans;
104};
105
106/*
Maxime Ripard55585932014-07-17 21:46:16 +0200107 * Hardware representation of the LLI
108 *
109 * The hardware will be fed the physical address of this structure,
110 * and read its content in order to start the transfer.
111 */
112struct sun6i_dma_lli {
113 u32 cfg;
114 u32 src;
115 u32 dst;
116 u32 len;
117 u32 para;
118 u32 p_lli_next;
119
120 /*
121 * This field is not used by the DMA controller, but will be
122 * used by the CPU to go through the list (mostly for dumping
123 * or freeing it).
124 */
125 struct sun6i_dma_lli *v_lli_next;
126};
127
128
129struct sun6i_desc {
130 struct virt_dma_desc vd;
131 dma_addr_t p_lli;
132 struct sun6i_dma_lli *v_lli;
133};
134
135struct sun6i_pchan {
136 u32 idx;
137 void __iomem *base;
138 struct sun6i_vchan *vchan;
139 struct sun6i_desc *desc;
140 struct sun6i_desc *done;
141};
142
143struct sun6i_vchan {
144 struct virt_dma_chan vc;
145 struct list_head node;
146 struct dma_slave_config cfg;
147 struct sun6i_pchan *phy;
148 u8 port;
149};
150
151struct sun6i_dma_dev {
152 struct dma_device slave;
153 void __iomem *base;
154 struct clk *clk;
155 int irq;
156 spinlock_t lock;
157 struct reset_control *rstc;
158 struct tasklet_struct task;
159 atomic_t tasklet_shutdown;
160 struct list_head pending;
161 struct dma_pool *pool;
162 struct sun6i_pchan *pchans;
163 struct sun6i_vchan *vchans;
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800164 const struct sun6i_dma_config *cfg;
Maxime Ripard55585932014-07-17 21:46:16 +0200165};
166
167static struct device *chan2dev(struct dma_chan *chan)
168{
169 return &chan->dev->device;
170}
171
172static inline struct sun6i_dma_dev *to_sun6i_dma_dev(struct dma_device *d)
173{
174 return container_of(d, struct sun6i_dma_dev, slave);
175}
176
177static inline struct sun6i_vchan *to_sun6i_vchan(struct dma_chan *chan)
178{
179 return container_of(chan, struct sun6i_vchan, vc.chan);
180}
181
182static inline struct sun6i_desc *
183to_sun6i_desc(struct dma_async_tx_descriptor *tx)
184{
185 return container_of(tx, struct sun6i_desc, vd.tx);
186}
187
188static inline void sun6i_dma_dump_com_regs(struct sun6i_dma_dev *sdev)
189{
190 dev_dbg(sdev->slave.dev, "Common register:\n"
191 "\tmask0(%04x): 0x%08x\n"
192 "\tmask1(%04x): 0x%08x\n"
193 "\tpend0(%04x): 0x%08x\n"
194 "\tpend1(%04x): 0x%08x\n"
195 "\tstats(%04x): 0x%08x\n",
196 DMA_IRQ_EN(0), readl(sdev->base + DMA_IRQ_EN(0)),
197 DMA_IRQ_EN(1), readl(sdev->base + DMA_IRQ_EN(1)),
198 DMA_IRQ_STAT(0), readl(sdev->base + DMA_IRQ_STAT(0)),
199 DMA_IRQ_STAT(1), readl(sdev->base + DMA_IRQ_STAT(1)),
200 DMA_STAT, readl(sdev->base + DMA_STAT));
201}
202
203static inline void sun6i_dma_dump_chan_regs(struct sun6i_dma_dev *sdev,
204 struct sun6i_pchan *pchan)
205{
Vinod Koul42c0d542014-07-28 11:57:25 +0530206 phys_addr_t reg = virt_to_phys(pchan->base);
Maxime Ripard55585932014-07-17 21:46:16 +0200207
208 dev_dbg(sdev->slave.dev, "Chan %d reg: %pa\n"
209 "\t___en(%04x): \t0x%08x\n"
210 "\tpause(%04x): \t0x%08x\n"
211 "\tstart(%04x): \t0x%08x\n"
212 "\t__cfg(%04x): \t0x%08x\n"
213 "\t__src(%04x): \t0x%08x\n"
214 "\t__dst(%04x): \t0x%08x\n"
215 "\tcount(%04x): \t0x%08x\n"
216 "\t_para(%04x): \t0x%08x\n\n",
217 pchan->idx, &reg,
218 DMA_CHAN_ENABLE,
219 readl(pchan->base + DMA_CHAN_ENABLE),
220 DMA_CHAN_PAUSE,
221 readl(pchan->base + DMA_CHAN_PAUSE),
222 DMA_CHAN_LLI_ADDR,
223 readl(pchan->base + DMA_CHAN_LLI_ADDR),
224 DMA_CHAN_CUR_CFG,
225 readl(pchan->base + DMA_CHAN_CUR_CFG),
226 DMA_CHAN_CUR_SRC,
227 readl(pchan->base + DMA_CHAN_CUR_SRC),
228 DMA_CHAN_CUR_DST,
229 readl(pchan->base + DMA_CHAN_CUR_DST),
230 DMA_CHAN_CUR_CNT,
231 readl(pchan->base + DMA_CHAN_CUR_CNT),
232 DMA_CHAN_CUR_PARA,
233 readl(pchan->base + DMA_CHAN_CUR_PARA));
234}
235
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100236static inline s8 convert_burst(u32 maxburst)
Maxime Ripard55585932014-07-17 21:46:16 +0200237{
238 switch (maxburst) {
239 case 1:
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100240 return 0;
Maxime Ripard55585932014-07-17 21:46:16 +0200241 case 8:
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100242 return 2;
Maxime Ripard55585932014-07-17 21:46:16 +0200243 default:
244 return -EINVAL;
245 }
Maxime Ripard55585932014-07-17 21:46:16 +0200246}
247
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100248static inline s8 convert_buswidth(enum dma_slave_buswidth addr_width)
Maxime Ripard55585932014-07-17 21:46:16 +0200249{
Maxime Ripard92e4a3b2014-07-30 10:30:21 +0200250 if ((addr_width < DMA_SLAVE_BUSWIDTH_1_BYTE) ||
251 (addr_width > DMA_SLAVE_BUSWIDTH_4_BYTES))
Maxime Ripard55585932014-07-17 21:46:16 +0200252 return -EINVAL;
Maxime Ripard55585932014-07-17 21:46:16 +0200253
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100254 return addr_width >> 1;
Maxime Ripard55585932014-07-17 21:46:16 +0200255}
256
257static void *sun6i_dma_lli_add(struct sun6i_dma_lli *prev,
258 struct sun6i_dma_lli *next,
259 dma_addr_t next_phy,
260 struct sun6i_desc *txd)
261{
262 if ((!prev && !txd) || !next)
263 return NULL;
264
265 if (!prev) {
266 txd->p_lli = next_phy;
267 txd->v_lli = next;
268 } else {
269 prev->p_lli_next = next_phy;
270 prev->v_lli_next = next;
271 }
272
273 next->p_lli_next = LLI_LAST_ITEM;
274 next->v_lli_next = NULL;
275
276 return next;
277}
278
Maxime Ripard55585932014-07-17 21:46:16 +0200279static inline void sun6i_dma_dump_lli(struct sun6i_vchan *vchan,
280 struct sun6i_dma_lli *lli)
281{
Vinod Koul42c0d542014-07-28 11:57:25 +0530282 phys_addr_t p_lli = virt_to_phys(lli);
Maxime Ripard55585932014-07-17 21:46:16 +0200283
284 dev_dbg(chan2dev(&vchan->vc.chan),
285 "\n\tdesc: p - %pa v - 0x%p\n"
286 "\t\tc - 0x%08x s - 0x%08x d - 0x%08x\n"
287 "\t\tl - 0x%08x p - 0x%08x n - 0x%08x\n",
288 &p_lli, lli,
289 lli->cfg, lli->src, lli->dst,
290 lli->len, lli->para, lli->p_lli_next);
291}
292
293static void sun6i_dma_free_desc(struct virt_dma_desc *vd)
294{
295 struct sun6i_desc *txd = to_sun6i_desc(&vd->tx);
296 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vd->tx.chan->device);
297 struct sun6i_dma_lli *v_lli, *v_next;
298 dma_addr_t p_lli, p_next;
299
300 if (unlikely(!txd))
301 return;
302
303 p_lli = txd->p_lli;
304 v_lli = txd->v_lli;
305
306 while (v_lli) {
307 v_next = v_lli->v_lli_next;
308 p_next = v_lli->p_lli_next;
309
310 dma_pool_free(sdev->pool, v_lli, p_lli);
311
312 v_lli = v_next;
313 p_lli = p_next;
314 }
315
316 kfree(txd);
317}
318
Maxime Ripard55585932014-07-17 21:46:16 +0200319static int sun6i_dma_start_desc(struct sun6i_vchan *vchan)
320{
321 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vchan->vc.chan.device);
322 struct virt_dma_desc *desc = vchan_next_desc(&vchan->vc);
323 struct sun6i_pchan *pchan = vchan->phy;
324 u32 irq_val, irq_reg, irq_offset;
325
326 if (!pchan)
327 return -EAGAIN;
328
329 if (!desc) {
330 pchan->desc = NULL;
331 pchan->done = NULL;
332 return -EAGAIN;
333 }
334
335 list_del(&desc->node);
336
337 pchan->desc = to_sun6i_desc(&desc->tx);
338 pchan->done = NULL;
339
340 sun6i_dma_dump_lli(vchan, pchan->desc->v_lli);
341
342 irq_reg = pchan->idx / DMA_IRQ_CHAN_NR;
343 irq_offset = pchan->idx % DMA_IRQ_CHAN_NR;
344
Jean-Francois Moine128fe7e2016-04-22 08:14:33 +0200345 irq_val = readl(sdev->base + DMA_IRQ_EN(irq_reg));
Maxime Ripard55585932014-07-17 21:46:16 +0200346 irq_val |= DMA_IRQ_QUEUE << (irq_offset * DMA_IRQ_CHAN_WIDTH);
Jean-Francois Moine128fe7e2016-04-22 08:14:33 +0200347 writel(irq_val, sdev->base + DMA_IRQ_EN(irq_reg));
Maxime Ripard55585932014-07-17 21:46:16 +0200348
349 writel(pchan->desc->p_lli, pchan->base + DMA_CHAN_LLI_ADDR);
350 writel(DMA_CHAN_ENABLE_START, pchan->base + DMA_CHAN_ENABLE);
351
352 sun6i_dma_dump_com_regs(sdev);
353 sun6i_dma_dump_chan_regs(sdev, pchan);
354
355 return 0;
356}
357
358static void sun6i_dma_tasklet(unsigned long data)
359{
360 struct sun6i_dma_dev *sdev = (struct sun6i_dma_dev *)data;
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800361 const struct sun6i_dma_config *cfg = sdev->cfg;
Maxime Ripard55585932014-07-17 21:46:16 +0200362 struct sun6i_vchan *vchan;
363 struct sun6i_pchan *pchan;
364 unsigned int pchan_alloc = 0;
365 unsigned int pchan_idx;
366
367 list_for_each_entry(vchan, &sdev->slave.channels, vc.chan.device_node) {
368 spin_lock_irq(&vchan->vc.lock);
369
370 pchan = vchan->phy;
371
372 if (pchan && pchan->done) {
373 if (sun6i_dma_start_desc(vchan)) {
374 /*
375 * No current txd associated with this channel
376 */
377 dev_dbg(sdev->slave.dev, "pchan %u: free\n",
378 pchan->idx);
379
380 /* Mark this channel free */
381 vchan->phy = NULL;
382 pchan->vchan = NULL;
383 }
384 }
385 spin_unlock_irq(&vchan->vc.lock);
386 }
387
388 spin_lock_irq(&sdev->lock);
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800389 for (pchan_idx = 0; pchan_idx < cfg->nr_max_channels; pchan_idx++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200390 pchan = &sdev->pchans[pchan_idx];
391
392 if (pchan->vchan || list_empty(&sdev->pending))
393 continue;
394
395 vchan = list_first_entry(&sdev->pending,
396 struct sun6i_vchan, node);
397
398 /* Remove from pending channels */
399 list_del_init(&vchan->node);
400 pchan_alloc |= BIT(pchan_idx);
401
402 /* Mark this channel allocated */
403 pchan->vchan = vchan;
404 vchan->phy = pchan;
405 dev_dbg(sdev->slave.dev, "pchan %u: alloc vchan %p\n",
406 pchan->idx, &vchan->vc);
407 }
408 spin_unlock_irq(&sdev->lock);
409
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800410 for (pchan_idx = 0; pchan_idx < cfg->nr_max_channels; pchan_idx++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200411 if (!(pchan_alloc & BIT(pchan_idx)))
412 continue;
413
414 pchan = sdev->pchans + pchan_idx;
415 vchan = pchan->vchan;
416 if (vchan) {
417 spin_lock_irq(&vchan->vc.lock);
418 sun6i_dma_start_desc(vchan);
419 spin_unlock_irq(&vchan->vc.lock);
420 }
421 }
422}
423
424static irqreturn_t sun6i_dma_interrupt(int irq, void *dev_id)
425{
426 struct sun6i_dma_dev *sdev = dev_id;
427 struct sun6i_vchan *vchan;
428 struct sun6i_pchan *pchan;
429 int i, j, ret = IRQ_NONE;
430 u32 status;
431
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800432 for (i = 0; i < sdev->cfg->nr_max_channels / DMA_IRQ_CHAN_NR; i++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200433 status = readl(sdev->base + DMA_IRQ_STAT(i));
434 if (!status)
435 continue;
436
437 dev_dbg(sdev->slave.dev, "DMA irq status %s: 0x%x\n",
438 i ? "high" : "low", status);
439
440 writel(status, sdev->base + DMA_IRQ_STAT(i));
441
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800442 for (j = 0; (j < DMA_IRQ_CHAN_NR) && status; j++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200443 if (status & DMA_IRQ_QUEUE) {
444 pchan = sdev->pchans + j;
445 vchan = pchan->vchan;
446
447 if (vchan) {
448 spin_lock(&vchan->vc.lock);
449 vchan_cookie_complete(&pchan->desc->vd);
450 pchan->done = pchan->desc;
451 spin_unlock(&vchan->vc.lock);
452 }
453 }
454
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800455 status = status >> DMA_IRQ_CHAN_WIDTH;
Maxime Ripard55585932014-07-17 21:46:16 +0200456 }
457
458 if (!atomic_read(&sdev->tasklet_shutdown))
459 tasklet_schedule(&sdev->task);
460 ret = IRQ_HANDLED;
461 }
462
463 return ret;
464}
465
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200466static int set_config(struct sun6i_dma_dev *sdev,
467 struct dma_slave_config *sconfig,
468 enum dma_transfer_direction direction,
469 u32 *p_cfg)
470{
471 s8 src_width, dst_width, src_burst, dst_burst;
472
Jean-Francois Moinea4eb36b2016-04-28 17:07:02 +0200473 switch (direction) {
474 case DMA_MEM_TO_DEV:
475 src_burst = convert_burst(sconfig->src_maxburst ?
476 sconfig->src_maxburst : 8);
477 src_width = convert_buswidth(sconfig->src_addr_width !=
478 DMA_SLAVE_BUSWIDTH_UNDEFINED ?
479 sconfig->src_addr_width :
480 DMA_SLAVE_BUSWIDTH_4_BYTES);
481 dst_burst = convert_burst(sconfig->dst_maxburst);
482 dst_width = convert_buswidth(sconfig->dst_addr_width);
483 break;
484 case DMA_DEV_TO_MEM:
485 src_burst = convert_burst(sconfig->src_maxburst);
486 src_width = convert_buswidth(sconfig->src_addr_width);
487 dst_burst = convert_burst(sconfig->dst_maxburst ?
488 sconfig->dst_maxburst : 8);
489 dst_width = convert_buswidth(sconfig->dst_addr_width !=
490 DMA_SLAVE_BUSWIDTH_UNDEFINED ?
491 sconfig->dst_addr_width :
492 DMA_SLAVE_BUSWIDTH_4_BYTES);
493 break;
494 default:
495 return -EINVAL;
496 }
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200497
498 if (src_burst < 0)
499 return src_burst;
500 if (src_width < 0)
501 return src_width;
502 if (dst_burst < 0)
503 return dst_burst;
504 if (dst_width < 0)
505 return dst_width;
506
507 *p_cfg = DMA_CHAN_CFG_SRC_BURST(src_burst) |
508 DMA_CHAN_CFG_SRC_WIDTH(src_width) |
509 DMA_CHAN_CFG_DST_BURST(dst_burst) |
510 DMA_CHAN_CFG_DST_WIDTH(dst_width);
511
512 return 0;
513}
514
Maxime Ripard55585932014-07-17 21:46:16 +0200515static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_memcpy(
516 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
517 size_t len, unsigned long flags)
518{
519 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
520 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
Maxime Ripard55585932014-07-17 21:46:16 +0200521 struct sun6i_dma_lli *v_lli;
522 struct sun6i_desc *txd;
523 dma_addr_t p_lli;
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100524 s8 burst, width;
Maxime Ripard55585932014-07-17 21:46:16 +0200525
526 dev_dbg(chan2dev(chan),
527 "%s; chan: %d, dest: %pad, src: %pad, len: %zu. flags: 0x%08lx\n",
528 __func__, vchan->vc.chan.chan_id, &dest, &src, len, flags);
529
530 if (!len)
531 return NULL;
532
533 txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
534 if (!txd)
535 return NULL;
536
537 v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
538 if (!v_lli) {
539 dev_err(sdev->slave.dev, "Failed to alloc lli memory\n");
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200540 goto err_txd_free;
Maxime Ripard55585932014-07-17 21:46:16 +0200541 }
542
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100543 v_lli->src = src;
544 v_lli->dst = dest;
545 v_lli->len = len;
546 v_lli->para = NORMAL_WAIT;
Maxime Ripard55585932014-07-17 21:46:16 +0200547
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100548 burst = convert_burst(8);
549 width = convert_buswidth(DMA_SLAVE_BUSWIDTH_4_BYTES);
Maxime Ripard55585932014-07-17 21:46:16 +0200550 v_lli->cfg |= DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
551 DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
552 DMA_CHAN_CFG_DST_LINEAR_MODE |
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100553 DMA_CHAN_CFG_SRC_LINEAR_MODE |
554 DMA_CHAN_CFG_SRC_BURST(burst) |
555 DMA_CHAN_CFG_SRC_WIDTH(width) |
556 DMA_CHAN_CFG_DST_BURST(burst) |
557 DMA_CHAN_CFG_DST_WIDTH(width);
Maxime Ripard55585932014-07-17 21:46:16 +0200558
559 sun6i_dma_lli_add(NULL, v_lli, p_lli, txd);
560
561 sun6i_dma_dump_lli(vchan, v_lli);
562
563 return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
564
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200565err_txd_free:
566 kfree(txd);
Maxime Ripard55585932014-07-17 21:46:16 +0200567 return NULL;
568}
569
570static struct dma_async_tx_descriptor *sun6i_dma_prep_slave_sg(
571 struct dma_chan *chan, struct scatterlist *sgl,
572 unsigned int sg_len, enum dma_transfer_direction dir,
573 unsigned long flags, void *context)
574{
575 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
576 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
577 struct dma_slave_config *sconfig = &vchan->cfg;
578 struct sun6i_dma_lli *v_lli, *prev = NULL;
579 struct sun6i_desc *txd;
580 struct scatterlist *sg;
581 dma_addr_t p_lli;
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200582 u32 lli_cfg;
Maxime Ripard55585932014-07-17 21:46:16 +0200583 int i, ret;
584
585 if (!sgl)
586 return NULL;
587
588 if (!is_slave_direction(dir)) {
589 dev_err(chan2dev(chan), "Invalid DMA direction\n");
590 return NULL;
591 }
592
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200593 ret = set_config(sdev, sconfig, dir, &lli_cfg);
594 if (ret) {
595 dev_err(chan2dev(chan), "Invalid DMA configuration\n");
596 return NULL;
597 }
598
Maxime Ripard55585932014-07-17 21:46:16 +0200599 txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
600 if (!txd)
601 return NULL;
602
603 for_each_sg(sgl, sg, sg_len, i) {
604 v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200605 if (!v_lli)
606 goto err_lli_free;
Maxime Ripard55585932014-07-17 21:46:16 +0200607
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200608 v_lli->len = sg_dma_len(sg);
609 v_lli->para = NORMAL_WAIT;
Maxime Ripard55585932014-07-17 21:46:16 +0200610
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200611 if (dir == DMA_MEM_TO_DEV) {
612 v_lli->src = sg_dma_address(sg);
613 v_lli->dst = sconfig->dst_addr;
614 v_lli->cfg = lli_cfg |
615 DMA_CHAN_CFG_DST_IO_MODE |
Maxime Ripard55585932014-07-17 21:46:16 +0200616 DMA_CHAN_CFG_SRC_LINEAR_MODE |
617 DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
618 DMA_CHAN_CFG_DST_DRQ(vchan->port);
619
620 dev_dbg(chan2dev(chan),
Vinod Koul7f5e03e2014-07-28 12:32:51 +0530621 "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
Maxime Ripard55585932014-07-17 21:46:16 +0200622 __func__, vchan->vc.chan.chan_id,
623 &sconfig->dst_addr, &sg_dma_address(sg),
624 sg_dma_len(sg), flags);
625
626 } else {
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200627 v_lli->src = sconfig->src_addr;
628 v_lli->dst = sg_dma_address(sg);
629 v_lli->cfg = lli_cfg |
630 DMA_CHAN_CFG_DST_LINEAR_MODE |
Maxime Ripard55585932014-07-17 21:46:16 +0200631 DMA_CHAN_CFG_SRC_IO_MODE |
632 DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
633 DMA_CHAN_CFG_SRC_DRQ(vchan->port);
634
635 dev_dbg(chan2dev(chan),
Vinod Koul7f5e03e2014-07-28 12:32:51 +0530636 "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
Maxime Ripard55585932014-07-17 21:46:16 +0200637 __func__, vchan->vc.chan.chan_id,
638 &sg_dma_address(sg), &sconfig->src_addr,
639 sg_dma_len(sg), flags);
640 }
641
642 prev = sun6i_dma_lli_add(prev, v_lli, p_lli, txd);
643 }
644
645 dev_dbg(chan2dev(chan), "First: %pad\n", &txd->p_lli);
646 for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
647 sun6i_dma_dump_lli(vchan, prev);
648
649 return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
650
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200651err_lli_free:
652 for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
653 dma_pool_free(sdev->pool, prev, virt_to_phys(prev));
654 kfree(txd);
Maxime Ripard55585932014-07-17 21:46:16 +0200655 return NULL;
656}
657
Maxime Ripard826b15a2014-11-17 14:42:35 +0100658static int sun6i_dma_config(struct dma_chan *chan,
659 struct dma_slave_config *config)
660{
661 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
662
663 memcpy(&vchan->cfg, config, sizeof(*config));
664
665 return 0;
666}
667
668static int sun6i_dma_pause(struct dma_chan *chan)
669{
670 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
671 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
672 struct sun6i_pchan *pchan = vchan->phy;
673
674 dev_dbg(chan2dev(chan), "vchan %p: pause\n", &vchan->vc);
675
676 if (pchan) {
677 writel(DMA_CHAN_PAUSE_PAUSE,
678 pchan->base + DMA_CHAN_PAUSE);
679 } else {
680 spin_lock(&sdev->lock);
681 list_del_init(&vchan->node);
682 spin_unlock(&sdev->lock);
683 }
684
685 return 0;
686}
687
688static int sun6i_dma_resume(struct dma_chan *chan)
Maxime Ripard55585932014-07-17 21:46:16 +0200689{
690 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
691 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
692 struct sun6i_pchan *pchan = vchan->phy;
693 unsigned long flags;
Maxime Ripard55585932014-07-17 21:46:16 +0200694
Maxime Ripard826b15a2014-11-17 14:42:35 +0100695 dev_dbg(chan2dev(chan), "vchan %p: resume\n", &vchan->vc);
Maxime Ripard55585932014-07-17 21:46:16 +0200696
Maxime Ripard826b15a2014-11-17 14:42:35 +0100697 spin_lock_irqsave(&vchan->vc.lock, flags);
Maxime Ripard55585932014-07-17 21:46:16 +0200698
Maxime Ripard826b15a2014-11-17 14:42:35 +0100699 if (pchan) {
700 writel(DMA_CHAN_PAUSE_RESUME,
701 pchan->base + DMA_CHAN_PAUSE);
702 } else if (!list_empty(&vchan->vc.desc_issued)) {
703 spin_lock(&sdev->lock);
704 list_add_tail(&vchan->node, &sdev->pending);
705 spin_unlock(&sdev->lock);
Maxime Ripard55585932014-07-17 21:46:16 +0200706 }
Maxime Ripard826b15a2014-11-17 14:42:35 +0100707
708 spin_unlock_irqrestore(&vchan->vc.lock, flags);
709
710 return 0;
711}
712
713static int sun6i_dma_terminate_all(struct dma_chan *chan)
714{
715 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
716 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
717 struct sun6i_pchan *pchan = vchan->phy;
718 unsigned long flags;
719 LIST_HEAD(head);
720
721 spin_lock(&sdev->lock);
722 list_del_init(&vchan->node);
723 spin_unlock(&sdev->lock);
724
725 spin_lock_irqsave(&vchan->vc.lock, flags);
726
727 vchan_get_all_descriptors(&vchan->vc, &head);
728
729 if (pchan) {
730 writel(DMA_CHAN_ENABLE_STOP, pchan->base + DMA_CHAN_ENABLE);
731 writel(DMA_CHAN_PAUSE_RESUME, pchan->base + DMA_CHAN_PAUSE);
732
733 vchan->phy = NULL;
734 pchan->vchan = NULL;
735 pchan->desc = NULL;
736 pchan->done = NULL;
737 }
738
739 spin_unlock_irqrestore(&vchan->vc.lock, flags);
740
741 vchan_dma_desc_free_list(&vchan->vc, &head);
742
743 return 0;
Maxime Ripard55585932014-07-17 21:46:16 +0200744}
745
746static enum dma_status sun6i_dma_tx_status(struct dma_chan *chan,
747 dma_cookie_t cookie,
748 struct dma_tx_state *state)
749{
750 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
751 struct sun6i_pchan *pchan = vchan->phy;
752 struct sun6i_dma_lli *lli;
753 struct virt_dma_desc *vd;
754 struct sun6i_desc *txd;
755 enum dma_status ret;
756 unsigned long flags;
757 size_t bytes = 0;
758
759 ret = dma_cookie_status(chan, cookie, state);
760 if (ret == DMA_COMPLETE)
761 return ret;
762
763 spin_lock_irqsave(&vchan->vc.lock, flags);
764
765 vd = vchan_find_desc(&vchan->vc, cookie);
766 txd = to_sun6i_desc(&vd->tx);
767
768 if (vd) {
769 for (lli = txd->v_lli; lli != NULL; lli = lli->v_lli_next)
770 bytes += lli->len;
771 } else if (!pchan || !pchan->desc) {
772 bytes = 0;
773 } else {
774 bytes = readl(pchan->base + DMA_CHAN_CUR_CNT);
775 }
776
777 spin_unlock_irqrestore(&vchan->vc.lock, flags);
778
779 dma_set_residue(state, bytes);
780
781 return ret;
782}
783
784static void sun6i_dma_issue_pending(struct dma_chan *chan)
785{
786 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
787 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
788 unsigned long flags;
789
790 spin_lock_irqsave(&vchan->vc.lock, flags);
791
792 if (vchan_issue_pending(&vchan->vc)) {
793 spin_lock(&sdev->lock);
794
795 if (!vchan->phy && list_empty(&vchan->node)) {
796 list_add_tail(&vchan->node, &sdev->pending);
797 tasklet_schedule(&sdev->task);
798 dev_dbg(chan2dev(chan), "vchan %p: issued\n",
799 &vchan->vc);
800 }
801
802 spin_unlock(&sdev->lock);
803 } else {
804 dev_dbg(chan2dev(chan), "vchan %p: nothing to issue\n",
805 &vchan->vc);
806 }
807
808 spin_unlock_irqrestore(&vchan->vc.lock, flags);
809}
810
Maxime Ripard55585932014-07-17 21:46:16 +0200811static void sun6i_dma_free_chan_resources(struct dma_chan *chan)
812{
813 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
814 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
815 unsigned long flags;
816
817 spin_lock_irqsave(&sdev->lock, flags);
818 list_del_init(&vchan->node);
819 spin_unlock_irqrestore(&sdev->lock, flags);
820
821 vchan_free_chan_resources(&vchan->vc);
822}
823
824static struct dma_chan *sun6i_dma_of_xlate(struct of_phandle_args *dma_spec,
825 struct of_dma *ofdma)
826{
827 struct sun6i_dma_dev *sdev = ofdma->of_dma_data;
828 struct sun6i_vchan *vchan;
829 struct dma_chan *chan;
830 u8 port = dma_spec->args[0];
831
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800832 if (port > sdev->cfg->nr_max_requests)
Maxime Ripard55585932014-07-17 21:46:16 +0200833 return NULL;
834
835 chan = dma_get_any_slave_channel(&sdev->slave);
836 if (!chan)
837 return NULL;
838
839 vchan = to_sun6i_vchan(chan);
840 vchan->port = port;
841
842 return chan;
843}
844
845static inline void sun6i_kill_tasklet(struct sun6i_dma_dev *sdev)
846{
847 /* Disable all interrupts from DMA */
848 writel(0, sdev->base + DMA_IRQ_EN(0));
849 writel(0, sdev->base + DMA_IRQ_EN(1));
850
851 /* Prevent spurious interrupts from scheduling the tasklet */
852 atomic_inc(&sdev->tasklet_shutdown);
853
Maxime Ripard174427c2014-07-30 10:30:22 +0200854 /* Make sure we won't have any further interrupts */
855 devm_free_irq(sdev->slave.dev, sdev->irq, sdev);
Maxime Ripard55585932014-07-17 21:46:16 +0200856
857 /* Actually prevent the tasklet from being scheduled */
858 tasklet_kill(&sdev->task);
859}
860
861static inline void sun6i_dma_free(struct sun6i_dma_dev *sdev)
862{
863 int i;
864
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800865 for (i = 0; i < sdev->cfg->nr_max_vchans; i++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200866 struct sun6i_vchan *vchan = &sdev->vchans[i];
867
868 list_del(&vchan->vc.chan.device_node);
869 tasklet_kill(&vchan->vc.task);
870 }
871}
872
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800873/*
874 * For A31:
875 *
876 * There's 16 physical channels that can work in parallel.
877 *
878 * However we have 30 different endpoints for our requests.
879 *
880 * Since the channels are able to handle only an unidirectional
881 * transfer, we need to allocate more virtual channels so that
882 * everyone can grab one channel.
883 *
884 * Some devices can't work in both direction (mostly because it
885 * wouldn't make sense), so we have a bit fewer virtual channels than
886 * 2 channels per endpoints.
887 */
888
889static struct sun6i_dma_config sun6i_a31_dma_cfg = {
890 .nr_max_channels = 16,
891 .nr_max_requests = 30,
892 .nr_max_vchans = 53,
893};
894
Chen-Yu Tsai0b04ddf2014-11-07 12:15:47 +0800895/*
896 * The A23 only has 8 physical channels, a maximum DRQ port id of 24,
897 * and a total of 37 usable source and destination endpoints.
898 */
899
900static struct sun6i_dma_config sun8i_a23_dma_cfg = {
901 .nr_max_channels = 8,
902 .nr_max_requests = 24,
903 .nr_max_vchans = 37,
904};
905
Jens Kuskef008db82015-05-06 11:31:31 +0200906/*
907 * The H3 has 12 physical channels, a maximum DRQ port id of 27,
908 * and a total of 34 usable source and destination endpoints.
909 */
910
911static struct sun6i_dma_config sun8i_h3_dma_cfg = {
912 .nr_max_channels = 12,
913 .nr_max_requests = 27,
914 .nr_max_vchans = 34,
915};
916
Fabian Frederick57c03422015-03-16 20:17:14 +0100917static const struct of_device_id sun6i_dma_match[] = {
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800918 { .compatible = "allwinner,sun6i-a31-dma", .data = &sun6i_a31_dma_cfg },
Chen-Yu Tsai0b04ddf2014-11-07 12:15:47 +0800919 { .compatible = "allwinner,sun8i-a23-dma", .data = &sun8i_a23_dma_cfg },
Jens Kuskef008db82015-05-06 11:31:31 +0200920 { .compatible = "allwinner,sun8i-h3-dma", .data = &sun8i_h3_dma_cfg },
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800921 { /* sentinel */ }
922};
Luis de Bethencourtc719d7f2015-09-16 22:59:31 +0200923MODULE_DEVICE_TABLE(of, sun6i_dma_match);
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800924
Maxime Ripard55585932014-07-17 21:46:16 +0200925static int sun6i_dma_probe(struct platform_device *pdev)
926{
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800927 const struct of_device_id *device;
Maxime Ripard55585932014-07-17 21:46:16 +0200928 struct sun6i_dma_dev *sdc;
929 struct resource *res;
Maxime Ripard55585932014-07-17 21:46:16 +0200930 int ret, i;
931
932 sdc = devm_kzalloc(&pdev->dev, sizeof(*sdc), GFP_KERNEL);
933 if (!sdc)
934 return -ENOMEM;
935
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800936 device = of_match_device(sun6i_dma_match, &pdev->dev);
937 if (!device)
938 return -ENODEV;
939 sdc->cfg = device->data;
940
Maxime Ripard55585932014-07-17 21:46:16 +0200941 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
942 sdc->base = devm_ioremap_resource(&pdev->dev, res);
943 if (IS_ERR(sdc->base))
944 return PTR_ERR(sdc->base);
945
946 sdc->irq = platform_get_irq(pdev, 0);
947 if (sdc->irq < 0) {
948 dev_err(&pdev->dev, "Cannot claim IRQ\n");
949 return sdc->irq;
950 }
951
952 sdc->clk = devm_clk_get(&pdev->dev, NULL);
953 if (IS_ERR(sdc->clk)) {
954 dev_err(&pdev->dev, "No clock specified\n");
955 return PTR_ERR(sdc->clk);
956 }
957
Maxime Ripard55585932014-07-17 21:46:16 +0200958 sdc->rstc = devm_reset_control_get(&pdev->dev, NULL);
959 if (IS_ERR(sdc->rstc)) {
960 dev_err(&pdev->dev, "No reset controller specified\n");
961 return PTR_ERR(sdc->rstc);
962 }
963
964 sdc->pool = dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
965 sizeof(struct sun6i_dma_lli), 4, 0);
966 if (!sdc->pool) {
967 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
968 return -ENOMEM;
969 }
970
971 platform_set_drvdata(pdev, sdc);
972 INIT_LIST_HEAD(&sdc->pending);
973 spin_lock_init(&sdc->lock);
974
975 dma_cap_set(DMA_PRIVATE, sdc->slave.cap_mask);
976 dma_cap_set(DMA_MEMCPY, sdc->slave.cap_mask);
977 dma_cap_set(DMA_SLAVE, sdc->slave.cap_mask);
978
979 INIT_LIST_HEAD(&sdc->slave.channels);
Maxime Ripard55585932014-07-17 21:46:16 +0200980 sdc->slave.device_free_chan_resources = sun6i_dma_free_chan_resources;
981 sdc->slave.device_tx_status = sun6i_dma_tx_status;
982 sdc->slave.device_issue_pending = sun6i_dma_issue_pending;
983 sdc->slave.device_prep_slave_sg = sun6i_dma_prep_slave_sg;
984 sdc->slave.device_prep_dma_memcpy = sun6i_dma_prep_dma_memcpy;
Maxime Ripard77a68e52015-07-20 10:41:32 +0200985 sdc->slave.copy_align = DMAENGINE_ALIGN_4_BYTES;
Maxime Ripard826b15a2014-11-17 14:42:35 +0100986 sdc->slave.device_config = sun6i_dma_config;
987 sdc->slave.device_pause = sun6i_dma_pause;
988 sdc->slave.device_resume = sun6i_dma_resume;
989 sdc->slave.device_terminate_all = sun6i_dma_terminate_all;
Maxime Ripard1cac81b2014-11-17 14:42:52 +0100990 sdc->slave.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
991 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
992 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
993 sdc->slave.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
994 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
995 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
996 sdc->slave.directions = BIT(DMA_DEV_TO_MEM) |
997 BIT(DMA_MEM_TO_DEV);
998 sdc->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Maxime Ripard55585932014-07-17 21:46:16 +0200999 sdc->slave.dev = &pdev->dev;
1000
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +08001001 sdc->pchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_channels,
Maxime Ripard55585932014-07-17 21:46:16 +02001002 sizeof(struct sun6i_pchan), GFP_KERNEL);
1003 if (!sdc->pchans)
1004 return -ENOMEM;
1005
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +08001006 sdc->vchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_vchans,
Maxime Ripard55585932014-07-17 21:46:16 +02001007 sizeof(struct sun6i_vchan), GFP_KERNEL);
1008 if (!sdc->vchans)
1009 return -ENOMEM;
1010
1011 tasklet_init(&sdc->task, sun6i_dma_tasklet, (unsigned long)sdc);
1012
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +08001013 for (i = 0; i < sdc->cfg->nr_max_channels; i++) {
Maxime Ripard55585932014-07-17 21:46:16 +02001014 struct sun6i_pchan *pchan = &sdc->pchans[i];
1015
1016 pchan->idx = i;
1017 pchan->base = sdc->base + 0x100 + i * 0x40;
1018 }
1019
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +08001020 for (i = 0; i < sdc->cfg->nr_max_vchans; i++) {
Maxime Ripard55585932014-07-17 21:46:16 +02001021 struct sun6i_vchan *vchan = &sdc->vchans[i];
1022
1023 INIT_LIST_HEAD(&vchan->node);
1024 vchan->vc.desc_free = sun6i_dma_free_desc;
1025 vchan_init(&vchan->vc, &sdc->slave);
1026 }
1027
1028 ret = reset_control_deassert(sdc->rstc);
1029 if (ret) {
1030 dev_err(&pdev->dev, "Couldn't deassert the device from reset\n");
1031 goto err_chan_free;
1032 }
1033
1034 ret = clk_prepare_enable(sdc->clk);
1035 if (ret) {
1036 dev_err(&pdev->dev, "Couldn't enable the clock\n");
1037 goto err_reset_assert;
1038 }
1039
1040 ret = devm_request_irq(&pdev->dev, sdc->irq, sun6i_dma_interrupt, 0,
1041 dev_name(&pdev->dev), sdc);
1042 if (ret) {
1043 dev_err(&pdev->dev, "Cannot request IRQ\n");
1044 goto err_clk_disable;
1045 }
1046
1047 ret = dma_async_device_register(&sdc->slave);
1048 if (ret) {
1049 dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
1050 goto err_irq_disable;
1051 }
1052
1053 ret = of_dma_controller_register(pdev->dev.of_node, sun6i_dma_of_xlate,
1054 sdc);
1055 if (ret) {
1056 dev_err(&pdev->dev, "of_dma_controller_register failed\n");
1057 goto err_dma_unregister;
1058 }
1059
Chen-Yu Tsai0b04ddf2014-11-07 12:15:47 +08001060 /*
1061 * sun8i variant requires us to toggle a dma gating register,
1062 * as seen in Allwinner's SDK. This register is not documented
1063 * in the A23 user manual.
1064 */
1065 if (of_device_is_compatible(pdev->dev.of_node,
1066 "allwinner,sun8i-a23-dma"))
1067 writel(SUN8I_DMA_GATE_ENABLE, sdc->base + SUN8I_DMA_GATE);
1068
Maxime Ripard55585932014-07-17 21:46:16 +02001069 return 0;
1070
1071err_dma_unregister:
1072 dma_async_device_unregister(&sdc->slave);
1073err_irq_disable:
1074 sun6i_kill_tasklet(sdc);
1075err_clk_disable:
1076 clk_disable_unprepare(sdc->clk);
1077err_reset_assert:
1078 reset_control_assert(sdc->rstc);
1079err_chan_free:
1080 sun6i_dma_free(sdc);
1081 return ret;
1082}
1083
1084static int sun6i_dma_remove(struct platform_device *pdev)
1085{
1086 struct sun6i_dma_dev *sdc = platform_get_drvdata(pdev);
1087
1088 of_dma_controller_free(pdev->dev.of_node);
1089 dma_async_device_unregister(&sdc->slave);
1090
1091 sun6i_kill_tasklet(sdc);
1092
1093 clk_disable_unprepare(sdc->clk);
1094 reset_control_assert(sdc->rstc);
1095
1096 sun6i_dma_free(sdc);
1097
1098 return 0;
1099}
1100
Maxime Ripard55585932014-07-17 21:46:16 +02001101static struct platform_driver sun6i_dma_driver = {
1102 .probe = sun6i_dma_probe,
1103 .remove = sun6i_dma_remove,
1104 .driver = {
1105 .name = "sun6i-dma",
1106 .of_match_table = sun6i_dma_match,
1107 },
1108};
1109module_platform_driver(sun6i_dma_driver);
1110
1111MODULE_DESCRIPTION("Allwinner A31 DMA Controller Driver");
1112MODULE_AUTHOR("Sugar <shuge@allwinnertech.com>");
1113MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1114MODULE_LICENSE("GPL");