blob: 252b59c1d1d514ff3da6acb7ee5bf30ccc85ab9c [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;
Icenowy Zheng0430a7c2017-08-29 12:51:26 +0800104 /*
105 * In the datasheets/user manuals of newer Allwinner SoCs, a special
106 * bit (bit 2 at register 0x20) is present.
107 * It's named "DMA MCLK interface circuit auto gating bit" in the
108 * documents, and the footnote of this register says that this bit
109 * should be set up when initializing the DMA controller.
110 * Allwinner A23/A33 user manuals do not have this bit documented,
111 * however these SoCs really have and need this bit, as seen in the
112 * BSP kernel source code.
113 */
114 bool gate_needed;
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800115};
116
117/*
Maxime Ripard55585932014-07-17 21:46:16 +0200118 * Hardware representation of the LLI
119 *
120 * The hardware will be fed the physical address of this structure,
121 * and read its content in order to start the transfer.
122 */
123struct sun6i_dma_lli {
124 u32 cfg;
125 u32 src;
126 u32 dst;
127 u32 len;
128 u32 para;
129 u32 p_lli_next;
130
131 /*
132 * This field is not used by the DMA controller, but will be
133 * used by the CPU to go through the list (mostly for dumping
134 * or freeing it).
135 */
136 struct sun6i_dma_lli *v_lli_next;
137};
138
139
140struct sun6i_desc {
141 struct virt_dma_desc vd;
142 dma_addr_t p_lli;
143 struct sun6i_dma_lli *v_lli;
144};
145
146struct sun6i_pchan {
147 u32 idx;
148 void __iomem *base;
149 struct sun6i_vchan *vchan;
150 struct sun6i_desc *desc;
151 struct sun6i_desc *done;
152};
153
154struct sun6i_vchan {
155 struct virt_dma_chan vc;
156 struct list_head node;
157 struct dma_slave_config cfg;
158 struct sun6i_pchan *phy;
159 u8 port;
Jean-Francois Moinea90e1732016-04-28 17:13:46 +0200160 u8 irq_type;
161 bool cyclic;
Maxime Ripard55585932014-07-17 21:46:16 +0200162};
163
164struct sun6i_dma_dev {
165 struct dma_device slave;
166 void __iomem *base;
167 struct clk *clk;
168 int irq;
169 spinlock_t lock;
170 struct reset_control *rstc;
171 struct tasklet_struct task;
172 atomic_t tasklet_shutdown;
173 struct list_head pending;
174 struct dma_pool *pool;
175 struct sun6i_pchan *pchans;
176 struct sun6i_vchan *vchans;
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800177 const struct sun6i_dma_config *cfg;
Maxime Ripard55585932014-07-17 21:46:16 +0200178};
179
180static struct device *chan2dev(struct dma_chan *chan)
181{
182 return &chan->dev->device;
183}
184
185static inline struct sun6i_dma_dev *to_sun6i_dma_dev(struct dma_device *d)
186{
187 return container_of(d, struct sun6i_dma_dev, slave);
188}
189
190static inline struct sun6i_vchan *to_sun6i_vchan(struct dma_chan *chan)
191{
192 return container_of(chan, struct sun6i_vchan, vc.chan);
193}
194
195static inline struct sun6i_desc *
196to_sun6i_desc(struct dma_async_tx_descriptor *tx)
197{
198 return container_of(tx, struct sun6i_desc, vd.tx);
199}
200
201static inline void sun6i_dma_dump_com_regs(struct sun6i_dma_dev *sdev)
202{
203 dev_dbg(sdev->slave.dev, "Common register:\n"
204 "\tmask0(%04x): 0x%08x\n"
205 "\tmask1(%04x): 0x%08x\n"
206 "\tpend0(%04x): 0x%08x\n"
207 "\tpend1(%04x): 0x%08x\n"
208 "\tstats(%04x): 0x%08x\n",
209 DMA_IRQ_EN(0), readl(sdev->base + DMA_IRQ_EN(0)),
210 DMA_IRQ_EN(1), readl(sdev->base + DMA_IRQ_EN(1)),
211 DMA_IRQ_STAT(0), readl(sdev->base + DMA_IRQ_STAT(0)),
212 DMA_IRQ_STAT(1), readl(sdev->base + DMA_IRQ_STAT(1)),
213 DMA_STAT, readl(sdev->base + DMA_STAT));
214}
215
216static inline void sun6i_dma_dump_chan_regs(struct sun6i_dma_dev *sdev,
217 struct sun6i_pchan *pchan)
218{
Vinod Koul42c0d542014-07-28 11:57:25 +0530219 phys_addr_t reg = virt_to_phys(pchan->base);
Maxime Ripard55585932014-07-17 21:46:16 +0200220
221 dev_dbg(sdev->slave.dev, "Chan %d reg: %pa\n"
222 "\t___en(%04x): \t0x%08x\n"
223 "\tpause(%04x): \t0x%08x\n"
224 "\tstart(%04x): \t0x%08x\n"
225 "\t__cfg(%04x): \t0x%08x\n"
226 "\t__src(%04x): \t0x%08x\n"
227 "\t__dst(%04x): \t0x%08x\n"
228 "\tcount(%04x): \t0x%08x\n"
229 "\t_para(%04x): \t0x%08x\n\n",
230 pchan->idx, &reg,
231 DMA_CHAN_ENABLE,
232 readl(pchan->base + DMA_CHAN_ENABLE),
233 DMA_CHAN_PAUSE,
234 readl(pchan->base + DMA_CHAN_PAUSE),
235 DMA_CHAN_LLI_ADDR,
236 readl(pchan->base + DMA_CHAN_LLI_ADDR),
237 DMA_CHAN_CUR_CFG,
238 readl(pchan->base + DMA_CHAN_CUR_CFG),
239 DMA_CHAN_CUR_SRC,
240 readl(pchan->base + DMA_CHAN_CUR_SRC),
241 DMA_CHAN_CUR_DST,
242 readl(pchan->base + DMA_CHAN_CUR_DST),
243 DMA_CHAN_CUR_CNT,
244 readl(pchan->base + DMA_CHAN_CUR_CNT),
245 DMA_CHAN_CUR_PARA,
246 readl(pchan->base + DMA_CHAN_CUR_PARA));
247}
248
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100249static inline s8 convert_burst(u32 maxburst)
Maxime Ripard55585932014-07-17 21:46:16 +0200250{
251 switch (maxburst) {
252 case 1:
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100253 return 0;
Maxime Ripard55585932014-07-17 21:46:16 +0200254 case 8:
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100255 return 2;
Maxime Ripard55585932014-07-17 21:46:16 +0200256 default:
257 return -EINVAL;
258 }
Maxime Ripard55585932014-07-17 21:46:16 +0200259}
260
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100261static inline s8 convert_buswidth(enum dma_slave_buswidth addr_width)
Maxime Ripard55585932014-07-17 21:46:16 +0200262{
Maxime Ripard92e4a3b2014-07-30 10:30:21 +0200263 if ((addr_width < DMA_SLAVE_BUSWIDTH_1_BYTE) ||
264 (addr_width > DMA_SLAVE_BUSWIDTH_4_BYTES))
Maxime Ripard55585932014-07-17 21:46:16 +0200265 return -EINVAL;
Maxime Ripard55585932014-07-17 21:46:16 +0200266
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100267 return addr_width >> 1;
Maxime Ripard55585932014-07-17 21:46:16 +0200268}
269
Jean-Francois Moinea90e1732016-04-28 17:13:46 +0200270static size_t sun6i_get_chan_size(struct sun6i_pchan *pchan)
271{
272 struct sun6i_desc *txd = pchan->desc;
273 struct sun6i_dma_lli *lli;
274 size_t bytes;
275 dma_addr_t pos;
276
277 pos = readl(pchan->base + DMA_CHAN_LLI_ADDR);
278 bytes = readl(pchan->base + DMA_CHAN_CUR_CNT);
279
280 if (pos == LLI_LAST_ITEM)
281 return bytes;
282
283 for (lli = txd->v_lli; lli; lli = lli->v_lli_next) {
284 if (lli->p_lli_next == pos) {
285 for (lli = lli->v_lli_next; lli; lli = lli->v_lli_next)
286 bytes += lli->len;
287 break;
288 }
289 }
290
291 return bytes;
292}
293
Maxime Ripard55585932014-07-17 21:46:16 +0200294static void *sun6i_dma_lli_add(struct sun6i_dma_lli *prev,
295 struct sun6i_dma_lli *next,
296 dma_addr_t next_phy,
297 struct sun6i_desc *txd)
298{
299 if ((!prev && !txd) || !next)
300 return NULL;
301
302 if (!prev) {
303 txd->p_lli = next_phy;
304 txd->v_lli = next;
305 } else {
306 prev->p_lli_next = next_phy;
307 prev->v_lli_next = next;
308 }
309
310 next->p_lli_next = LLI_LAST_ITEM;
311 next->v_lli_next = NULL;
312
313 return next;
314}
315
Maxime Ripard55585932014-07-17 21:46:16 +0200316static inline void sun6i_dma_dump_lli(struct sun6i_vchan *vchan,
317 struct sun6i_dma_lli *lli)
318{
Vinod Koul42c0d542014-07-28 11:57:25 +0530319 phys_addr_t p_lli = virt_to_phys(lli);
Maxime Ripard55585932014-07-17 21:46:16 +0200320
321 dev_dbg(chan2dev(&vchan->vc.chan),
322 "\n\tdesc: p - %pa v - 0x%p\n"
323 "\t\tc - 0x%08x s - 0x%08x d - 0x%08x\n"
324 "\t\tl - 0x%08x p - 0x%08x n - 0x%08x\n",
325 &p_lli, lli,
326 lli->cfg, lli->src, lli->dst,
327 lli->len, lli->para, lli->p_lli_next);
328}
329
330static void sun6i_dma_free_desc(struct virt_dma_desc *vd)
331{
332 struct sun6i_desc *txd = to_sun6i_desc(&vd->tx);
333 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vd->tx.chan->device);
334 struct sun6i_dma_lli *v_lli, *v_next;
335 dma_addr_t p_lli, p_next;
336
337 if (unlikely(!txd))
338 return;
339
340 p_lli = txd->p_lli;
341 v_lli = txd->v_lli;
342
343 while (v_lli) {
344 v_next = v_lli->v_lli_next;
345 p_next = v_lli->p_lli_next;
346
347 dma_pool_free(sdev->pool, v_lli, p_lli);
348
349 v_lli = v_next;
350 p_lli = p_next;
351 }
352
353 kfree(txd);
354}
355
Maxime Ripard55585932014-07-17 21:46:16 +0200356static int sun6i_dma_start_desc(struct sun6i_vchan *vchan)
357{
358 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vchan->vc.chan.device);
359 struct virt_dma_desc *desc = vchan_next_desc(&vchan->vc);
360 struct sun6i_pchan *pchan = vchan->phy;
361 u32 irq_val, irq_reg, irq_offset;
362
363 if (!pchan)
364 return -EAGAIN;
365
366 if (!desc) {
367 pchan->desc = NULL;
368 pchan->done = NULL;
369 return -EAGAIN;
370 }
371
372 list_del(&desc->node);
373
374 pchan->desc = to_sun6i_desc(&desc->tx);
375 pchan->done = NULL;
376
377 sun6i_dma_dump_lli(vchan, pchan->desc->v_lli);
378
379 irq_reg = pchan->idx / DMA_IRQ_CHAN_NR;
380 irq_offset = pchan->idx % DMA_IRQ_CHAN_NR;
381
Jean-Francois Moinea90e1732016-04-28 17:13:46 +0200382 vchan->irq_type = vchan->cyclic ? DMA_IRQ_PKG : DMA_IRQ_QUEUE;
383
Jean-Francois Moine128fe7e2016-04-22 08:14:33 +0200384 irq_val = readl(sdev->base + DMA_IRQ_EN(irq_reg));
Jean-Francois Moinea90e1732016-04-28 17:13:46 +0200385 irq_val &= ~((DMA_IRQ_HALF | DMA_IRQ_PKG | DMA_IRQ_QUEUE) <<
386 (irq_offset * DMA_IRQ_CHAN_WIDTH));
387 irq_val |= vchan->irq_type << (irq_offset * DMA_IRQ_CHAN_WIDTH);
Jean-Francois Moine128fe7e2016-04-22 08:14:33 +0200388 writel(irq_val, sdev->base + DMA_IRQ_EN(irq_reg));
Maxime Ripard55585932014-07-17 21:46:16 +0200389
390 writel(pchan->desc->p_lli, pchan->base + DMA_CHAN_LLI_ADDR);
391 writel(DMA_CHAN_ENABLE_START, pchan->base + DMA_CHAN_ENABLE);
392
393 sun6i_dma_dump_com_regs(sdev);
394 sun6i_dma_dump_chan_regs(sdev, pchan);
395
396 return 0;
397}
398
399static void sun6i_dma_tasklet(unsigned long data)
400{
401 struct sun6i_dma_dev *sdev = (struct sun6i_dma_dev *)data;
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800402 const struct sun6i_dma_config *cfg = sdev->cfg;
Maxime Ripard55585932014-07-17 21:46:16 +0200403 struct sun6i_vchan *vchan;
404 struct sun6i_pchan *pchan;
405 unsigned int pchan_alloc = 0;
406 unsigned int pchan_idx;
407
408 list_for_each_entry(vchan, &sdev->slave.channels, vc.chan.device_node) {
409 spin_lock_irq(&vchan->vc.lock);
410
411 pchan = vchan->phy;
412
413 if (pchan && pchan->done) {
414 if (sun6i_dma_start_desc(vchan)) {
415 /*
416 * No current txd associated with this channel
417 */
418 dev_dbg(sdev->slave.dev, "pchan %u: free\n",
419 pchan->idx);
420
421 /* Mark this channel free */
422 vchan->phy = NULL;
423 pchan->vchan = NULL;
424 }
425 }
426 spin_unlock_irq(&vchan->vc.lock);
427 }
428
429 spin_lock_irq(&sdev->lock);
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800430 for (pchan_idx = 0; pchan_idx < cfg->nr_max_channels; pchan_idx++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200431 pchan = &sdev->pchans[pchan_idx];
432
433 if (pchan->vchan || list_empty(&sdev->pending))
434 continue;
435
436 vchan = list_first_entry(&sdev->pending,
437 struct sun6i_vchan, node);
438
439 /* Remove from pending channels */
440 list_del_init(&vchan->node);
441 pchan_alloc |= BIT(pchan_idx);
442
443 /* Mark this channel allocated */
444 pchan->vchan = vchan;
445 vchan->phy = pchan;
446 dev_dbg(sdev->slave.dev, "pchan %u: alloc vchan %p\n",
447 pchan->idx, &vchan->vc);
448 }
449 spin_unlock_irq(&sdev->lock);
450
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800451 for (pchan_idx = 0; pchan_idx < cfg->nr_max_channels; pchan_idx++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200452 if (!(pchan_alloc & BIT(pchan_idx)))
453 continue;
454
455 pchan = sdev->pchans + pchan_idx;
456 vchan = pchan->vchan;
457 if (vchan) {
458 spin_lock_irq(&vchan->vc.lock);
459 sun6i_dma_start_desc(vchan);
460 spin_unlock_irq(&vchan->vc.lock);
461 }
462 }
463}
464
465static irqreturn_t sun6i_dma_interrupt(int irq, void *dev_id)
466{
467 struct sun6i_dma_dev *sdev = dev_id;
468 struct sun6i_vchan *vchan;
469 struct sun6i_pchan *pchan;
470 int i, j, ret = IRQ_NONE;
471 u32 status;
472
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800473 for (i = 0; i < sdev->cfg->nr_max_channels / DMA_IRQ_CHAN_NR; i++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200474 status = readl(sdev->base + DMA_IRQ_STAT(i));
475 if (!status)
476 continue;
477
478 dev_dbg(sdev->slave.dev, "DMA irq status %s: 0x%x\n",
479 i ? "high" : "low", status);
480
481 writel(status, sdev->base + DMA_IRQ_STAT(i));
482
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800483 for (j = 0; (j < DMA_IRQ_CHAN_NR) && status; j++) {
Jean-Francois Moinea90e1732016-04-28 17:13:46 +0200484 pchan = sdev->pchans + j;
485 vchan = pchan->vchan;
486 if (vchan && (status & vchan->irq_type)) {
487 if (vchan->cyclic) {
488 vchan_cyclic_callback(&pchan->desc->vd);
489 } else {
Maxime Ripard55585932014-07-17 21:46:16 +0200490 spin_lock(&vchan->vc.lock);
491 vchan_cookie_complete(&pchan->desc->vd);
492 pchan->done = pchan->desc;
493 spin_unlock(&vchan->vc.lock);
494 }
495 }
496
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800497 status = status >> DMA_IRQ_CHAN_WIDTH;
Maxime Ripard55585932014-07-17 21:46:16 +0200498 }
499
500 if (!atomic_read(&sdev->tasklet_shutdown))
501 tasklet_schedule(&sdev->task);
502 ret = IRQ_HANDLED;
503 }
504
505 return ret;
506}
507
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200508static int set_config(struct sun6i_dma_dev *sdev,
509 struct dma_slave_config *sconfig,
510 enum dma_transfer_direction direction,
511 u32 *p_cfg)
512{
513 s8 src_width, dst_width, src_burst, dst_burst;
514
Jean-Francois Moinea4eb36b2016-04-28 17:07:02 +0200515 switch (direction) {
516 case DMA_MEM_TO_DEV:
517 src_burst = convert_burst(sconfig->src_maxburst ?
518 sconfig->src_maxburst : 8);
519 src_width = convert_buswidth(sconfig->src_addr_width !=
520 DMA_SLAVE_BUSWIDTH_UNDEFINED ?
521 sconfig->src_addr_width :
522 DMA_SLAVE_BUSWIDTH_4_BYTES);
523 dst_burst = convert_burst(sconfig->dst_maxburst);
524 dst_width = convert_buswidth(sconfig->dst_addr_width);
525 break;
526 case DMA_DEV_TO_MEM:
527 src_burst = convert_burst(sconfig->src_maxburst);
528 src_width = convert_buswidth(sconfig->src_addr_width);
529 dst_burst = convert_burst(sconfig->dst_maxburst ?
530 sconfig->dst_maxburst : 8);
531 dst_width = convert_buswidth(sconfig->dst_addr_width !=
532 DMA_SLAVE_BUSWIDTH_UNDEFINED ?
533 sconfig->dst_addr_width :
534 DMA_SLAVE_BUSWIDTH_4_BYTES);
535 break;
536 default:
537 return -EINVAL;
538 }
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200539
540 if (src_burst < 0)
541 return src_burst;
542 if (src_width < 0)
543 return src_width;
544 if (dst_burst < 0)
545 return dst_burst;
546 if (dst_width < 0)
547 return dst_width;
548
549 *p_cfg = DMA_CHAN_CFG_SRC_BURST(src_burst) |
550 DMA_CHAN_CFG_SRC_WIDTH(src_width) |
551 DMA_CHAN_CFG_DST_BURST(dst_burst) |
552 DMA_CHAN_CFG_DST_WIDTH(dst_width);
553
554 return 0;
555}
556
Maxime Ripard55585932014-07-17 21:46:16 +0200557static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_memcpy(
558 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
559 size_t len, unsigned long flags)
560{
561 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
562 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
Maxime Ripard55585932014-07-17 21:46:16 +0200563 struct sun6i_dma_lli *v_lli;
564 struct sun6i_desc *txd;
565 dma_addr_t p_lli;
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100566 s8 burst, width;
Maxime Ripard55585932014-07-17 21:46:16 +0200567
568 dev_dbg(chan2dev(chan),
569 "%s; chan: %d, dest: %pad, src: %pad, len: %zu. flags: 0x%08lx\n",
570 __func__, vchan->vc.chan.chan_id, &dest, &src, len, flags);
571
572 if (!len)
573 return NULL;
574
575 txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
576 if (!txd)
577 return NULL;
578
579 v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
580 if (!v_lli) {
581 dev_err(sdev->slave.dev, "Failed to alloc lli memory\n");
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200582 goto err_txd_free;
Maxime Ripard55585932014-07-17 21:46:16 +0200583 }
584
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100585 v_lli->src = src;
586 v_lli->dst = dest;
587 v_lli->len = len;
588 v_lli->para = NORMAL_WAIT;
Maxime Ripard55585932014-07-17 21:46:16 +0200589
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100590 burst = convert_burst(8);
591 width = convert_buswidth(DMA_SLAVE_BUSWIDTH_4_BYTES);
Axl-zhangf732c5b2016-11-02 13:31:12 +0800592 v_lli->cfg = DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
Maxime Ripard55585932014-07-17 21:46:16 +0200593 DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
594 DMA_CHAN_CFG_DST_LINEAR_MODE |
Maxime Ripard1f9cd912014-11-11 19:35:52 +0100595 DMA_CHAN_CFG_SRC_LINEAR_MODE |
596 DMA_CHAN_CFG_SRC_BURST(burst) |
597 DMA_CHAN_CFG_SRC_WIDTH(width) |
598 DMA_CHAN_CFG_DST_BURST(burst) |
599 DMA_CHAN_CFG_DST_WIDTH(width);
Maxime Ripard55585932014-07-17 21:46:16 +0200600
601 sun6i_dma_lli_add(NULL, v_lli, p_lli, txd);
602
603 sun6i_dma_dump_lli(vchan, v_lli);
604
605 return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
606
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200607err_txd_free:
608 kfree(txd);
Maxime Ripard55585932014-07-17 21:46:16 +0200609 return NULL;
610}
611
612static struct dma_async_tx_descriptor *sun6i_dma_prep_slave_sg(
613 struct dma_chan *chan, struct scatterlist *sgl,
614 unsigned int sg_len, enum dma_transfer_direction dir,
615 unsigned long flags, void *context)
616{
617 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
618 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
619 struct dma_slave_config *sconfig = &vchan->cfg;
620 struct sun6i_dma_lli *v_lli, *prev = NULL;
621 struct sun6i_desc *txd;
622 struct scatterlist *sg;
623 dma_addr_t p_lli;
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200624 u32 lli_cfg;
Maxime Ripard55585932014-07-17 21:46:16 +0200625 int i, ret;
626
627 if (!sgl)
628 return NULL;
629
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200630 ret = set_config(sdev, sconfig, dir, &lli_cfg);
631 if (ret) {
632 dev_err(chan2dev(chan), "Invalid DMA configuration\n");
633 return NULL;
634 }
635
Maxime Ripard55585932014-07-17 21:46:16 +0200636 txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
637 if (!txd)
638 return NULL;
639
640 for_each_sg(sgl, sg, sg_len, i) {
641 v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200642 if (!v_lli)
643 goto err_lli_free;
Maxime Ripard55585932014-07-17 21:46:16 +0200644
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200645 v_lli->len = sg_dma_len(sg);
646 v_lli->para = NORMAL_WAIT;
Maxime Ripard55585932014-07-17 21:46:16 +0200647
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200648 if (dir == DMA_MEM_TO_DEV) {
649 v_lli->src = sg_dma_address(sg);
650 v_lli->dst = sconfig->dst_addr;
651 v_lli->cfg = lli_cfg |
652 DMA_CHAN_CFG_DST_IO_MODE |
Maxime Ripard55585932014-07-17 21:46:16 +0200653 DMA_CHAN_CFG_SRC_LINEAR_MODE |
654 DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
655 DMA_CHAN_CFG_DST_DRQ(vchan->port);
656
657 dev_dbg(chan2dev(chan),
Vinod Koul7f5e03e2014-07-28 12:32:51 +0530658 "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
Maxime Ripard55585932014-07-17 21:46:16 +0200659 __func__, vchan->vc.chan.chan_id,
660 &sconfig->dst_addr, &sg_dma_address(sg),
661 sg_dma_len(sg), flags);
662
663 } else {
Jean-Francois Moine52c87172016-04-22 08:47:29 +0200664 v_lli->src = sconfig->src_addr;
665 v_lli->dst = sg_dma_address(sg);
666 v_lli->cfg = lli_cfg |
667 DMA_CHAN_CFG_DST_LINEAR_MODE |
Maxime Ripard55585932014-07-17 21:46:16 +0200668 DMA_CHAN_CFG_SRC_IO_MODE |
669 DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
670 DMA_CHAN_CFG_SRC_DRQ(vchan->port);
671
672 dev_dbg(chan2dev(chan),
Vinod Koul7f5e03e2014-07-28 12:32:51 +0530673 "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
Maxime Ripard55585932014-07-17 21:46:16 +0200674 __func__, vchan->vc.chan.chan_id,
675 &sg_dma_address(sg), &sconfig->src_addr,
676 sg_dma_len(sg), flags);
677 }
678
679 prev = sun6i_dma_lli_add(prev, v_lli, p_lli, txd);
680 }
681
682 dev_dbg(chan2dev(chan), "First: %pad\n", &txd->p_lli);
683 for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
684 sun6i_dma_dump_lli(vchan, prev);
685
686 return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
687
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200688err_lli_free:
689 for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
690 dma_pool_free(sdev->pool, prev, virt_to_phys(prev));
691 kfree(txd);
Maxime Ripard55585932014-07-17 21:46:16 +0200692 return NULL;
693}
694
Jean-Francois Moinea90e1732016-04-28 17:13:46 +0200695static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_cyclic(
696 struct dma_chan *chan,
697 dma_addr_t buf_addr,
698 size_t buf_len,
699 size_t period_len,
700 enum dma_transfer_direction dir,
701 unsigned long flags)
702{
703 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
704 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
705 struct dma_slave_config *sconfig = &vchan->cfg;
706 struct sun6i_dma_lli *v_lli, *prev = NULL;
707 struct sun6i_desc *txd;
708 dma_addr_t p_lli;
709 u32 lli_cfg;
710 unsigned int i, periods = buf_len / period_len;
711 int ret;
712
713 ret = set_config(sdev, sconfig, dir, &lli_cfg);
714 if (ret) {
715 dev_err(chan2dev(chan), "Invalid DMA configuration\n");
716 return NULL;
717 }
718
719 txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
720 if (!txd)
721 return NULL;
722
723 for (i = 0; i < periods; i++) {
724 v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
725 if (!v_lli) {
726 dev_err(sdev->slave.dev, "Failed to alloc lli memory\n");
727 goto err_lli_free;
728 }
729
730 v_lli->len = period_len;
731 v_lli->para = NORMAL_WAIT;
732
733 if (dir == DMA_MEM_TO_DEV) {
734 v_lli->src = buf_addr + period_len * i;
735 v_lli->dst = sconfig->dst_addr;
736 v_lli->cfg = lli_cfg |
737 DMA_CHAN_CFG_DST_IO_MODE |
738 DMA_CHAN_CFG_SRC_LINEAR_MODE |
739 DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
740 DMA_CHAN_CFG_DST_DRQ(vchan->port);
741 } else {
742 v_lli->src = sconfig->src_addr;
743 v_lli->dst = buf_addr + period_len * i;
744 v_lli->cfg = lli_cfg |
745 DMA_CHAN_CFG_DST_LINEAR_MODE |
746 DMA_CHAN_CFG_SRC_IO_MODE |
747 DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
748 DMA_CHAN_CFG_SRC_DRQ(vchan->port);
749 }
750
751 prev = sun6i_dma_lli_add(prev, v_lli, p_lli, txd);
752 }
753
754 prev->p_lli_next = txd->p_lli; /* cyclic list */
755
756 vchan->cyclic = true;
757
758 return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
759
760err_lli_free:
761 for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
762 dma_pool_free(sdev->pool, prev, virt_to_phys(prev));
763 kfree(txd);
764 return NULL;
765}
766
Maxime Ripard826b15a2014-11-17 14:42:35 +0100767static int sun6i_dma_config(struct dma_chan *chan,
768 struct dma_slave_config *config)
769{
770 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
771
772 memcpy(&vchan->cfg, config, sizeof(*config));
773
774 return 0;
775}
776
777static int sun6i_dma_pause(struct dma_chan *chan)
778{
779 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
780 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
781 struct sun6i_pchan *pchan = vchan->phy;
782
783 dev_dbg(chan2dev(chan), "vchan %p: pause\n", &vchan->vc);
784
785 if (pchan) {
786 writel(DMA_CHAN_PAUSE_PAUSE,
787 pchan->base + DMA_CHAN_PAUSE);
788 } else {
789 spin_lock(&sdev->lock);
790 list_del_init(&vchan->node);
791 spin_unlock(&sdev->lock);
792 }
793
794 return 0;
795}
796
797static int sun6i_dma_resume(struct dma_chan *chan)
Maxime Ripard55585932014-07-17 21:46:16 +0200798{
799 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
800 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
801 struct sun6i_pchan *pchan = vchan->phy;
802 unsigned long flags;
Maxime Ripard55585932014-07-17 21:46:16 +0200803
Maxime Ripard826b15a2014-11-17 14:42:35 +0100804 dev_dbg(chan2dev(chan), "vchan %p: resume\n", &vchan->vc);
Maxime Ripard55585932014-07-17 21:46:16 +0200805
Maxime Ripard826b15a2014-11-17 14:42:35 +0100806 spin_lock_irqsave(&vchan->vc.lock, flags);
Maxime Ripard55585932014-07-17 21:46:16 +0200807
Maxime Ripard826b15a2014-11-17 14:42:35 +0100808 if (pchan) {
809 writel(DMA_CHAN_PAUSE_RESUME,
810 pchan->base + DMA_CHAN_PAUSE);
811 } else if (!list_empty(&vchan->vc.desc_issued)) {
812 spin_lock(&sdev->lock);
813 list_add_tail(&vchan->node, &sdev->pending);
814 spin_unlock(&sdev->lock);
Maxime Ripard55585932014-07-17 21:46:16 +0200815 }
Maxime Ripard826b15a2014-11-17 14:42:35 +0100816
817 spin_unlock_irqrestore(&vchan->vc.lock, flags);
818
819 return 0;
820}
821
822static int sun6i_dma_terminate_all(struct dma_chan *chan)
823{
824 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
825 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
826 struct sun6i_pchan *pchan = vchan->phy;
827 unsigned long flags;
828 LIST_HEAD(head);
829
830 spin_lock(&sdev->lock);
831 list_del_init(&vchan->node);
832 spin_unlock(&sdev->lock);
833
834 spin_lock_irqsave(&vchan->vc.lock, flags);
835
Jean-Francois Moinea90e1732016-04-28 17:13:46 +0200836 if (vchan->cyclic) {
837 vchan->cyclic = false;
838 if (pchan && pchan->desc) {
839 struct virt_dma_desc *vd = &pchan->desc->vd;
840 struct virt_dma_chan *vc = &vchan->vc;
841
842 list_add_tail(&vd->node, &vc->desc_completed);
843 }
844 }
845
Maxime Ripard826b15a2014-11-17 14:42:35 +0100846 vchan_get_all_descriptors(&vchan->vc, &head);
847
848 if (pchan) {
849 writel(DMA_CHAN_ENABLE_STOP, pchan->base + DMA_CHAN_ENABLE);
850 writel(DMA_CHAN_PAUSE_RESUME, pchan->base + DMA_CHAN_PAUSE);
851
852 vchan->phy = NULL;
853 pchan->vchan = NULL;
854 pchan->desc = NULL;
855 pchan->done = NULL;
856 }
857
858 spin_unlock_irqrestore(&vchan->vc.lock, flags);
859
860 vchan_dma_desc_free_list(&vchan->vc, &head);
861
862 return 0;
Maxime Ripard55585932014-07-17 21:46:16 +0200863}
864
865static enum dma_status sun6i_dma_tx_status(struct dma_chan *chan,
866 dma_cookie_t cookie,
867 struct dma_tx_state *state)
868{
869 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
870 struct sun6i_pchan *pchan = vchan->phy;
871 struct sun6i_dma_lli *lli;
872 struct virt_dma_desc *vd;
873 struct sun6i_desc *txd;
874 enum dma_status ret;
875 unsigned long flags;
876 size_t bytes = 0;
877
878 ret = dma_cookie_status(chan, cookie, state);
Peter Griffinb9ab9d12016-06-07 18:38:39 +0100879 if (ret == DMA_COMPLETE || !state)
Maxime Ripard55585932014-07-17 21:46:16 +0200880 return ret;
881
882 spin_lock_irqsave(&vchan->vc.lock, flags);
883
884 vd = vchan_find_desc(&vchan->vc, cookie);
885 txd = to_sun6i_desc(&vd->tx);
886
887 if (vd) {
888 for (lli = txd->v_lli; lli != NULL; lli = lli->v_lli_next)
889 bytes += lli->len;
890 } else if (!pchan || !pchan->desc) {
891 bytes = 0;
892 } else {
Jean-Francois Moinea90e1732016-04-28 17:13:46 +0200893 bytes = sun6i_get_chan_size(pchan);
Maxime Ripard55585932014-07-17 21:46:16 +0200894 }
895
896 spin_unlock_irqrestore(&vchan->vc.lock, flags);
897
898 dma_set_residue(state, bytes);
899
900 return ret;
901}
902
903static void sun6i_dma_issue_pending(struct dma_chan *chan)
904{
905 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
906 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
907 unsigned long flags;
908
909 spin_lock_irqsave(&vchan->vc.lock, flags);
910
911 if (vchan_issue_pending(&vchan->vc)) {
912 spin_lock(&sdev->lock);
913
914 if (!vchan->phy && list_empty(&vchan->node)) {
915 list_add_tail(&vchan->node, &sdev->pending);
916 tasklet_schedule(&sdev->task);
917 dev_dbg(chan2dev(chan), "vchan %p: issued\n",
918 &vchan->vc);
919 }
920
921 spin_unlock(&sdev->lock);
922 } else {
923 dev_dbg(chan2dev(chan), "vchan %p: nothing to issue\n",
924 &vchan->vc);
925 }
926
927 spin_unlock_irqrestore(&vchan->vc.lock, flags);
928}
929
Maxime Ripard55585932014-07-17 21:46:16 +0200930static void sun6i_dma_free_chan_resources(struct dma_chan *chan)
931{
932 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
933 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
934 unsigned long flags;
935
936 spin_lock_irqsave(&sdev->lock, flags);
937 list_del_init(&vchan->node);
938 spin_unlock_irqrestore(&sdev->lock, flags);
939
940 vchan_free_chan_resources(&vchan->vc);
941}
942
943static struct dma_chan *sun6i_dma_of_xlate(struct of_phandle_args *dma_spec,
944 struct of_dma *ofdma)
945{
946 struct sun6i_dma_dev *sdev = ofdma->of_dma_data;
947 struct sun6i_vchan *vchan;
948 struct dma_chan *chan;
949 u8 port = dma_spec->args[0];
950
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800951 if (port > sdev->cfg->nr_max_requests)
Maxime Ripard55585932014-07-17 21:46:16 +0200952 return NULL;
953
954 chan = dma_get_any_slave_channel(&sdev->slave);
955 if (!chan)
956 return NULL;
957
958 vchan = to_sun6i_vchan(chan);
959 vchan->port = port;
960
961 return chan;
962}
963
964static inline void sun6i_kill_tasklet(struct sun6i_dma_dev *sdev)
965{
966 /* Disable all interrupts from DMA */
967 writel(0, sdev->base + DMA_IRQ_EN(0));
968 writel(0, sdev->base + DMA_IRQ_EN(1));
969
970 /* Prevent spurious interrupts from scheduling the tasklet */
971 atomic_inc(&sdev->tasklet_shutdown);
972
Maxime Ripard174427c2014-07-30 10:30:22 +0200973 /* Make sure we won't have any further interrupts */
974 devm_free_irq(sdev->slave.dev, sdev->irq, sdev);
Maxime Ripard55585932014-07-17 21:46:16 +0200975
976 /* Actually prevent the tasklet from being scheduled */
977 tasklet_kill(&sdev->task);
978}
979
980static inline void sun6i_dma_free(struct sun6i_dma_dev *sdev)
981{
982 int i;
983
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800984 for (i = 0; i < sdev->cfg->nr_max_vchans; i++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200985 struct sun6i_vchan *vchan = &sdev->vchans[i];
986
987 list_del(&vchan->vc.chan.device_node);
988 tasklet_kill(&vchan->vc.task);
989 }
990}
991
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800992/*
993 * For A31:
994 *
995 * There's 16 physical channels that can work in parallel.
996 *
997 * However we have 30 different endpoints for our requests.
998 *
999 * Since the channels are able to handle only an unidirectional
1000 * transfer, we need to allocate more virtual channels so that
1001 * everyone can grab one channel.
1002 *
1003 * Some devices can't work in both direction (mostly because it
1004 * wouldn't make sense), so we have a bit fewer virtual channels than
1005 * 2 channels per endpoints.
1006 */
1007
1008static struct sun6i_dma_config sun6i_a31_dma_cfg = {
1009 .nr_max_channels = 16,
1010 .nr_max_requests = 30,
1011 .nr_max_vchans = 53,
1012};
1013
Chen-Yu Tsai0b04ddf2014-11-07 12:15:47 +08001014/*
1015 * The A23 only has 8 physical channels, a maximum DRQ port id of 24,
1016 * and a total of 37 usable source and destination endpoints.
1017 */
1018
1019static struct sun6i_dma_config sun8i_a23_dma_cfg = {
1020 .nr_max_channels = 8,
1021 .nr_max_requests = 24,
1022 .nr_max_vchans = 37,
Icenowy Zheng0430a7c2017-08-29 12:51:26 +08001023 .gate_needed = true,
Chen-Yu Tsai0b04ddf2014-11-07 12:15:47 +08001024};
1025
Jean-Francois Moine3a03ea72016-09-18 09:59:50 +02001026static struct sun6i_dma_config sun8i_a83t_dma_cfg = {
1027 .nr_max_channels = 8,
1028 .nr_max_requests = 28,
1029 .nr_max_vchans = 39,
1030};
1031
Jens Kuskef008db82015-05-06 11:31:31 +02001032/*
1033 * The H3 has 12 physical channels, a maximum DRQ port id of 27,
1034 * and a total of 34 usable source and destination endpoints.
1035 */
1036
1037static struct sun6i_dma_config sun8i_h3_dma_cfg = {
1038 .nr_max_channels = 12,
1039 .nr_max_requests = 27,
1040 .nr_max_vchans = 34,
1041};
1042
Fabian Frederick57c03422015-03-16 20:17:14 +01001043static const struct of_device_id sun6i_dma_match[] = {
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +08001044 { .compatible = "allwinner,sun6i-a31-dma", .data = &sun6i_a31_dma_cfg },
Chen-Yu Tsai0b04ddf2014-11-07 12:15:47 +08001045 { .compatible = "allwinner,sun8i-a23-dma", .data = &sun8i_a23_dma_cfg },
Jean-Francois Moine3a03ea72016-09-18 09:59:50 +02001046 { .compatible = "allwinner,sun8i-a83t-dma", .data = &sun8i_a83t_dma_cfg },
Jens Kuskef008db82015-05-06 11:31:31 +02001047 { .compatible = "allwinner,sun8i-h3-dma", .data = &sun8i_h3_dma_cfg },
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +08001048 { /* sentinel */ }
1049};
Luis de Bethencourtc719d7f2015-09-16 22:59:31 +02001050MODULE_DEVICE_TABLE(of, sun6i_dma_match);
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +08001051
Maxime Ripard55585932014-07-17 21:46:16 +02001052static int sun6i_dma_probe(struct platform_device *pdev)
1053{
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +08001054 const struct of_device_id *device;
Maxime Ripard55585932014-07-17 21:46:16 +02001055 struct sun6i_dma_dev *sdc;
1056 struct resource *res;
Maxime Ripard55585932014-07-17 21:46:16 +02001057 int ret, i;
1058
1059 sdc = devm_kzalloc(&pdev->dev, sizeof(*sdc), GFP_KERNEL);
1060 if (!sdc)
1061 return -ENOMEM;
1062
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +08001063 device = of_match_device(sun6i_dma_match, &pdev->dev);
1064 if (!device)
1065 return -ENODEV;
1066 sdc->cfg = device->data;
1067
Maxime Ripard55585932014-07-17 21:46:16 +02001068 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1069 sdc->base = devm_ioremap_resource(&pdev->dev, res);
1070 if (IS_ERR(sdc->base))
1071 return PTR_ERR(sdc->base);
1072
1073 sdc->irq = platform_get_irq(pdev, 0);
1074 if (sdc->irq < 0) {
1075 dev_err(&pdev->dev, "Cannot claim IRQ\n");
1076 return sdc->irq;
1077 }
1078
1079 sdc->clk = devm_clk_get(&pdev->dev, NULL);
1080 if (IS_ERR(sdc->clk)) {
1081 dev_err(&pdev->dev, "No clock specified\n");
1082 return PTR_ERR(sdc->clk);
1083 }
1084
Maxime Ripard55585932014-07-17 21:46:16 +02001085 sdc->rstc = devm_reset_control_get(&pdev->dev, NULL);
1086 if (IS_ERR(sdc->rstc)) {
1087 dev_err(&pdev->dev, "No reset controller specified\n");
1088 return PTR_ERR(sdc->rstc);
1089 }
1090
1091 sdc->pool = dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
1092 sizeof(struct sun6i_dma_lli), 4, 0);
1093 if (!sdc->pool) {
1094 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1095 return -ENOMEM;
1096 }
1097
1098 platform_set_drvdata(pdev, sdc);
1099 INIT_LIST_HEAD(&sdc->pending);
1100 spin_lock_init(&sdc->lock);
1101
1102 dma_cap_set(DMA_PRIVATE, sdc->slave.cap_mask);
1103 dma_cap_set(DMA_MEMCPY, sdc->slave.cap_mask);
1104 dma_cap_set(DMA_SLAVE, sdc->slave.cap_mask);
Jean-Francois Moinea90e1732016-04-28 17:13:46 +02001105 dma_cap_set(DMA_CYCLIC, sdc->slave.cap_mask);
Maxime Ripard55585932014-07-17 21:46:16 +02001106
1107 INIT_LIST_HEAD(&sdc->slave.channels);
Maxime Ripard55585932014-07-17 21:46:16 +02001108 sdc->slave.device_free_chan_resources = sun6i_dma_free_chan_resources;
1109 sdc->slave.device_tx_status = sun6i_dma_tx_status;
1110 sdc->slave.device_issue_pending = sun6i_dma_issue_pending;
1111 sdc->slave.device_prep_slave_sg = sun6i_dma_prep_slave_sg;
1112 sdc->slave.device_prep_dma_memcpy = sun6i_dma_prep_dma_memcpy;
Jean-Francois Moinea90e1732016-04-28 17:13:46 +02001113 sdc->slave.device_prep_dma_cyclic = sun6i_dma_prep_dma_cyclic;
Maxime Ripard77a68e52015-07-20 10:41:32 +02001114 sdc->slave.copy_align = DMAENGINE_ALIGN_4_BYTES;
Maxime Ripard826b15a2014-11-17 14:42:35 +01001115 sdc->slave.device_config = sun6i_dma_config;
1116 sdc->slave.device_pause = sun6i_dma_pause;
1117 sdc->slave.device_resume = sun6i_dma_resume;
1118 sdc->slave.device_terminate_all = sun6i_dma_terminate_all;
Maxime Ripard1cac81b2014-11-17 14:42:52 +01001119 sdc->slave.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1120 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1121 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1122 sdc->slave.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1123 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1124 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1125 sdc->slave.directions = BIT(DMA_DEV_TO_MEM) |
1126 BIT(DMA_MEM_TO_DEV);
1127 sdc->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Maxime Ripard55585932014-07-17 21:46:16 +02001128 sdc->slave.dev = &pdev->dev;
1129
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +08001130 sdc->pchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_channels,
Maxime Ripard55585932014-07-17 21:46:16 +02001131 sizeof(struct sun6i_pchan), GFP_KERNEL);
1132 if (!sdc->pchans)
1133 return -ENOMEM;
1134
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +08001135 sdc->vchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_vchans,
Maxime Ripard55585932014-07-17 21:46:16 +02001136 sizeof(struct sun6i_vchan), GFP_KERNEL);
1137 if (!sdc->vchans)
1138 return -ENOMEM;
1139
1140 tasklet_init(&sdc->task, sun6i_dma_tasklet, (unsigned long)sdc);
1141
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +08001142 for (i = 0; i < sdc->cfg->nr_max_channels; i++) {
Maxime Ripard55585932014-07-17 21:46:16 +02001143 struct sun6i_pchan *pchan = &sdc->pchans[i];
1144
1145 pchan->idx = i;
1146 pchan->base = sdc->base + 0x100 + i * 0x40;
1147 }
1148
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +08001149 for (i = 0; i < sdc->cfg->nr_max_vchans; i++) {
Maxime Ripard55585932014-07-17 21:46:16 +02001150 struct sun6i_vchan *vchan = &sdc->vchans[i];
1151
1152 INIT_LIST_HEAD(&vchan->node);
1153 vchan->vc.desc_free = sun6i_dma_free_desc;
1154 vchan_init(&vchan->vc, &sdc->slave);
1155 }
1156
1157 ret = reset_control_deassert(sdc->rstc);
1158 if (ret) {
1159 dev_err(&pdev->dev, "Couldn't deassert the device from reset\n");
1160 goto err_chan_free;
1161 }
1162
1163 ret = clk_prepare_enable(sdc->clk);
1164 if (ret) {
1165 dev_err(&pdev->dev, "Couldn't enable the clock\n");
1166 goto err_reset_assert;
1167 }
1168
1169 ret = devm_request_irq(&pdev->dev, sdc->irq, sun6i_dma_interrupt, 0,
1170 dev_name(&pdev->dev), sdc);
1171 if (ret) {
1172 dev_err(&pdev->dev, "Cannot request IRQ\n");
1173 goto err_clk_disable;
1174 }
1175
1176 ret = dma_async_device_register(&sdc->slave);
1177 if (ret) {
1178 dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
1179 goto err_irq_disable;
1180 }
1181
1182 ret = of_dma_controller_register(pdev->dev.of_node, sun6i_dma_of_xlate,
1183 sdc);
1184 if (ret) {
1185 dev_err(&pdev->dev, "of_dma_controller_register failed\n");
1186 goto err_dma_unregister;
1187 }
1188
Icenowy Zheng0430a7c2017-08-29 12:51:26 +08001189 if (sdc->cfg->gate_needed)
Chen-Yu Tsai0b04ddf2014-11-07 12:15:47 +08001190 writel(SUN8I_DMA_GATE_ENABLE, sdc->base + SUN8I_DMA_GATE);
1191
Maxime Ripard55585932014-07-17 21:46:16 +02001192 return 0;
1193
1194err_dma_unregister:
1195 dma_async_device_unregister(&sdc->slave);
1196err_irq_disable:
1197 sun6i_kill_tasklet(sdc);
1198err_clk_disable:
1199 clk_disable_unprepare(sdc->clk);
1200err_reset_assert:
1201 reset_control_assert(sdc->rstc);
1202err_chan_free:
1203 sun6i_dma_free(sdc);
1204 return ret;
1205}
1206
1207static int sun6i_dma_remove(struct platform_device *pdev)
1208{
1209 struct sun6i_dma_dev *sdc = platform_get_drvdata(pdev);
1210
1211 of_dma_controller_free(pdev->dev.of_node);
1212 dma_async_device_unregister(&sdc->slave);
1213
1214 sun6i_kill_tasklet(sdc);
1215
1216 clk_disable_unprepare(sdc->clk);
1217 reset_control_assert(sdc->rstc);
1218
1219 sun6i_dma_free(sdc);
1220
1221 return 0;
1222}
1223
Maxime Ripard55585932014-07-17 21:46:16 +02001224static struct platform_driver sun6i_dma_driver = {
1225 .probe = sun6i_dma_probe,
1226 .remove = sun6i_dma_remove,
1227 .driver = {
1228 .name = "sun6i-dma",
1229 .of_match_table = sun6i_dma_match,
1230 },
1231};
1232module_platform_driver(sun6i_dma_driver);
1233
1234MODULE_DESCRIPTION("Allwinner A31 DMA Controller Driver");
1235MODULE_AUTHOR("Sugar <shuge@allwinnertech.com>");
1236MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1237MODULE_LICENSE("GPL");