blob: f9f8f4d9915f6928532f53ef52296455cfc3b594 [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
236static inline int convert_burst(u32 maxburst, u8 *burst)
237{
238 switch (maxburst) {
239 case 1:
240 *burst = 0;
241 break;
242 case 8:
243 *burst = 2;
244 break;
245 default:
246 return -EINVAL;
247 }
248
249 return 0;
250}
251
252static inline int convert_buswidth(enum dma_slave_buswidth addr_width, u8 *width)
253{
Maxime Ripard92e4a3b2014-07-30 10:30:21 +0200254 if ((addr_width < DMA_SLAVE_BUSWIDTH_1_BYTE) ||
255 (addr_width > DMA_SLAVE_BUSWIDTH_4_BYTES))
Maxime Ripard55585932014-07-17 21:46:16 +0200256 return -EINVAL;
Maxime Ripard55585932014-07-17 21:46:16 +0200257
Maxime Ripard92e4a3b2014-07-30 10:30:21 +0200258 *width = addr_width >> 1;
Maxime Ripard55585932014-07-17 21:46:16 +0200259 return 0;
260}
261
262static void *sun6i_dma_lli_add(struct sun6i_dma_lli *prev,
263 struct sun6i_dma_lli *next,
264 dma_addr_t next_phy,
265 struct sun6i_desc *txd)
266{
267 if ((!prev && !txd) || !next)
268 return NULL;
269
270 if (!prev) {
271 txd->p_lli = next_phy;
272 txd->v_lli = next;
273 } else {
274 prev->p_lli_next = next_phy;
275 prev->v_lli_next = next;
276 }
277
278 next->p_lli_next = LLI_LAST_ITEM;
279 next->v_lli_next = NULL;
280
281 return next;
282}
283
284static inline int sun6i_dma_cfg_lli(struct sun6i_dma_lli *lli,
285 dma_addr_t src,
286 dma_addr_t dst, u32 len,
287 struct dma_slave_config *config)
288{
289 u8 src_width, dst_width, src_burst, dst_burst;
290 int ret;
291
292 if (!config)
293 return -EINVAL;
294
295 ret = convert_burst(config->src_maxburst, &src_burst);
296 if (ret)
297 return ret;
298
299 ret = convert_burst(config->dst_maxburst, &dst_burst);
300 if (ret)
301 return ret;
302
303 ret = convert_buswidth(config->src_addr_width, &src_width);
304 if (ret)
305 return ret;
306
307 ret = convert_buswidth(config->dst_addr_width, &dst_width);
308 if (ret)
309 return ret;
310
311 lli->cfg = DMA_CHAN_CFG_SRC_BURST(src_burst) |
312 DMA_CHAN_CFG_SRC_WIDTH(src_width) |
313 DMA_CHAN_CFG_DST_BURST(dst_burst) |
314 DMA_CHAN_CFG_DST_WIDTH(dst_width);
315
316 lli->src = src;
317 lli->dst = dst;
318 lli->len = len;
319 lli->para = NORMAL_WAIT;
320
321 return 0;
322}
323
324static inline void sun6i_dma_dump_lli(struct sun6i_vchan *vchan,
325 struct sun6i_dma_lli *lli)
326{
Vinod Koul42c0d542014-07-28 11:57:25 +0530327 phys_addr_t p_lli = virt_to_phys(lli);
Maxime Ripard55585932014-07-17 21:46:16 +0200328
329 dev_dbg(chan2dev(&vchan->vc.chan),
330 "\n\tdesc: p - %pa v - 0x%p\n"
331 "\t\tc - 0x%08x s - 0x%08x d - 0x%08x\n"
332 "\t\tl - 0x%08x p - 0x%08x n - 0x%08x\n",
333 &p_lli, lli,
334 lli->cfg, lli->src, lli->dst,
335 lli->len, lli->para, lli->p_lli_next);
336}
337
338static void sun6i_dma_free_desc(struct virt_dma_desc *vd)
339{
340 struct sun6i_desc *txd = to_sun6i_desc(&vd->tx);
341 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vd->tx.chan->device);
342 struct sun6i_dma_lli *v_lli, *v_next;
343 dma_addr_t p_lli, p_next;
344
345 if (unlikely(!txd))
346 return;
347
348 p_lli = txd->p_lli;
349 v_lli = txd->v_lli;
350
351 while (v_lli) {
352 v_next = v_lli->v_lli_next;
353 p_next = v_lli->p_lli_next;
354
355 dma_pool_free(sdev->pool, v_lli, p_lli);
356
357 v_lli = v_next;
358 p_lli = p_next;
359 }
360
361 kfree(txd);
362}
363
364static int sun6i_dma_terminate_all(struct sun6i_vchan *vchan)
365{
366 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vchan->vc.chan.device);
367 struct sun6i_pchan *pchan = vchan->phy;
368 unsigned long flags;
369 LIST_HEAD(head);
370
371 spin_lock(&sdev->lock);
372 list_del_init(&vchan->node);
373 spin_unlock(&sdev->lock);
374
375 spin_lock_irqsave(&vchan->vc.lock, flags);
376
377 vchan_get_all_descriptors(&vchan->vc, &head);
378
379 if (pchan) {
380 writel(DMA_CHAN_ENABLE_STOP, pchan->base + DMA_CHAN_ENABLE);
381 writel(DMA_CHAN_PAUSE_RESUME, pchan->base + DMA_CHAN_PAUSE);
382
383 vchan->phy = NULL;
384 pchan->vchan = NULL;
385 pchan->desc = NULL;
386 pchan->done = NULL;
387 }
388
389 spin_unlock_irqrestore(&vchan->vc.lock, flags);
390
391 vchan_dma_desc_free_list(&vchan->vc, &head);
392
393 return 0;
394}
395
396static int sun6i_dma_start_desc(struct sun6i_vchan *vchan)
397{
398 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(vchan->vc.chan.device);
399 struct virt_dma_desc *desc = vchan_next_desc(&vchan->vc);
400 struct sun6i_pchan *pchan = vchan->phy;
401 u32 irq_val, irq_reg, irq_offset;
402
403 if (!pchan)
404 return -EAGAIN;
405
406 if (!desc) {
407 pchan->desc = NULL;
408 pchan->done = NULL;
409 return -EAGAIN;
410 }
411
412 list_del(&desc->node);
413
414 pchan->desc = to_sun6i_desc(&desc->tx);
415 pchan->done = NULL;
416
417 sun6i_dma_dump_lli(vchan, pchan->desc->v_lli);
418
419 irq_reg = pchan->idx / DMA_IRQ_CHAN_NR;
420 irq_offset = pchan->idx % DMA_IRQ_CHAN_NR;
421
422 irq_val = readl(sdev->base + DMA_IRQ_EN(irq_offset));
423 irq_val |= DMA_IRQ_QUEUE << (irq_offset * DMA_IRQ_CHAN_WIDTH);
424 writel(irq_val, sdev->base + DMA_IRQ_EN(irq_offset));
425
426 writel(pchan->desc->p_lli, pchan->base + DMA_CHAN_LLI_ADDR);
427 writel(DMA_CHAN_ENABLE_START, pchan->base + DMA_CHAN_ENABLE);
428
429 sun6i_dma_dump_com_regs(sdev);
430 sun6i_dma_dump_chan_regs(sdev, pchan);
431
432 return 0;
433}
434
435static void sun6i_dma_tasklet(unsigned long data)
436{
437 struct sun6i_dma_dev *sdev = (struct sun6i_dma_dev *)data;
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800438 const struct sun6i_dma_config *cfg = sdev->cfg;
Maxime Ripard55585932014-07-17 21:46:16 +0200439 struct sun6i_vchan *vchan;
440 struct sun6i_pchan *pchan;
441 unsigned int pchan_alloc = 0;
442 unsigned int pchan_idx;
443
444 list_for_each_entry(vchan, &sdev->slave.channels, vc.chan.device_node) {
445 spin_lock_irq(&vchan->vc.lock);
446
447 pchan = vchan->phy;
448
449 if (pchan && pchan->done) {
450 if (sun6i_dma_start_desc(vchan)) {
451 /*
452 * No current txd associated with this channel
453 */
454 dev_dbg(sdev->slave.dev, "pchan %u: free\n",
455 pchan->idx);
456
457 /* Mark this channel free */
458 vchan->phy = NULL;
459 pchan->vchan = NULL;
460 }
461 }
462 spin_unlock_irq(&vchan->vc.lock);
463 }
464
465 spin_lock_irq(&sdev->lock);
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800466 for (pchan_idx = 0; pchan_idx < cfg->nr_max_channels; pchan_idx++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200467 pchan = &sdev->pchans[pchan_idx];
468
469 if (pchan->vchan || list_empty(&sdev->pending))
470 continue;
471
472 vchan = list_first_entry(&sdev->pending,
473 struct sun6i_vchan, node);
474
475 /* Remove from pending channels */
476 list_del_init(&vchan->node);
477 pchan_alloc |= BIT(pchan_idx);
478
479 /* Mark this channel allocated */
480 pchan->vchan = vchan;
481 vchan->phy = pchan;
482 dev_dbg(sdev->slave.dev, "pchan %u: alloc vchan %p\n",
483 pchan->idx, &vchan->vc);
484 }
485 spin_unlock_irq(&sdev->lock);
486
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800487 for (pchan_idx = 0; pchan_idx < cfg->nr_max_channels; pchan_idx++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200488 if (!(pchan_alloc & BIT(pchan_idx)))
489 continue;
490
491 pchan = sdev->pchans + pchan_idx;
492 vchan = pchan->vchan;
493 if (vchan) {
494 spin_lock_irq(&vchan->vc.lock);
495 sun6i_dma_start_desc(vchan);
496 spin_unlock_irq(&vchan->vc.lock);
497 }
498 }
499}
500
501static irqreturn_t sun6i_dma_interrupt(int irq, void *dev_id)
502{
503 struct sun6i_dma_dev *sdev = dev_id;
504 struct sun6i_vchan *vchan;
505 struct sun6i_pchan *pchan;
506 int i, j, ret = IRQ_NONE;
507 u32 status;
508
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800509 for (i = 0; i < sdev->cfg->nr_max_channels / DMA_IRQ_CHAN_NR; i++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200510 status = readl(sdev->base + DMA_IRQ_STAT(i));
511 if (!status)
512 continue;
513
514 dev_dbg(sdev->slave.dev, "DMA irq status %s: 0x%x\n",
515 i ? "high" : "low", status);
516
517 writel(status, sdev->base + DMA_IRQ_STAT(i));
518
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800519 for (j = 0; (j < DMA_IRQ_CHAN_NR) && status; j++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200520 if (status & DMA_IRQ_QUEUE) {
521 pchan = sdev->pchans + j;
522 vchan = pchan->vchan;
523
524 if (vchan) {
525 spin_lock(&vchan->vc.lock);
526 vchan_cookie_complete(&pchan->desc->vd);
527 pchan->done = pchan->desc;
528 spin_unlock(&vchan->vc.lock);
529 }
530 }
531
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800532 status = status >> DMA_IRQ_CHAN_WIDTH;
Maxime Ripard55585932014-07-17 21:46:16 +0200533 }
534
535 if (!atomic_read(&sdev->tasklet_shutdown))
536 tasklet_schedule(&sdev->task);
537 ret = IRQ_HANDLED;
538 }
539
540 return ret;
541}
542
543static struct dma_async_tx_descriptor *sun6i_dma_prep_dma_memcpy(
544 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
545 size_t len, unsigned long flags)
546{
547 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
548 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
549 struct dma_slave_config *sconfig = &vchan->cfg;
550 struct sun6i_dma_lli *v_lli;
551 struct sun6i_desc *txd;
552 dma_addr_t p_lli;
553 int ret;
554
555 dev_dbg(chan2dev(chan),
556 "%s; chan: %d, dest: %pad, src: %pad, len: %zu. flags: 0x%08lx\n",
557 __func__, vchan->vc.chan.chan_id, &dest, &src, len, flags);
558
559 if (!len)
560 return NULL;
561
562 txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
563 if (!txd)
564 return NULL;
565
566 v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
567 if (!v_lli) {
568 dev_err(sdev->slave.dev, "Failed to alloc lli memory\n");
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200569 goto err_txd_free;
Maxime Ripard55585932014-07-17 21:46:16 +0200570 }
571
572 ret = sun6i_dma_cfg_lli(v_lli, src, dest, len, sconfig);
573 if (ret)
574 goto err_dma_free;
575
576 v_lli->cfg |= DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
577 DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
578 DMA_CHAN_CFG_DST_LINEAR_MODE |
579 DMA_CHAN_CFG_SRC_LINEAR_MODE;
580
581 sun6i_dma_lli_add(NULL, v_lli, p_lli, txd);
582
583 sun6i_dma_dump_lli(vchan, v_lli);
584
585 return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
586
587err_dma_free:
588 dma_pool_free(sdev->pool, v_lli, p_lli);
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200589err_txd_free:
590 kfree(txd);
Maxime Ripard55585932014-07-17 21:46:16 +0200591 return NULL;
592}
593
594static struct dma_async_tx_descriptor *sun6i_dma_prep_slave_sg(
595 struct dma_chan *chan, struct scatterlist *sgl,
596 unsigned int sg_len, enum dma_transfer_direction dir,
597 unsigned long flags, void *context)
598{
599 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
600 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
601 struct dma_slave_config *sconfig = &vchan->cfg;
602 struct sun6i_dma_lli *v_lli, *prev = NULL;
603 struct sun6i_desc *txd;
604 struct scatterlist *sg;
605 dma_addr_t p_lli;
606 int i, ret;
607
608 if (!sgl)
609 return NULL;
610
611 if (!is_slave_direction(dir)) {
612 dev_err(chan2dev(chan), "Invalid DMA direction\n");
613 return NULL;
614 }
615
616 txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
617 if (!txd)
618 return NULL;
619
620 for_each_sg(sgl, sg, sg_len, i) {
621 v_lli = dma_pool_alloc(sdev->pool, GFP_NOWAIT, &p_lli);
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200622 if (!v_lli)
623 goto err_lli_free;
Maxime Ripard55585932014-07-17 21:46:16 +0200624
625 if (dir == DMA_MEM_TO_DEV) {
626 ret = sun6i_dma_cfg_lli(v_lli, sg_dma_address(sg),
627 sconfig->dst_addr, sg_dma_len(sg),
628 sconfig);
629 if (ret)
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200630 goto err_cur_lli_free;
Maxime Ripard55585932014-07-17 21:46:16 +0200631
632 v_lli->cfg |= DMA_CHAN_CFG_DST_IO_MODE |
633 DMA_CHAN_CFG_SRC_LINEAR_MODE |
634 DMA_CHAN_CFG_SRC_DRQ(DRQ_SDRAM) |
635 DMA_CHAN_CFG_DST_DRQ(vchan->port);
636
637 dev_dbg(chan2dev(chan),
Vinod Koul7f5e03e2014-07-28 12:32:51 +0530638 "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
Maxime Ripard55585932014-07-17 21:46:16 +0200639 __func__, vchan->vc.chan.chan_id,
640 &sconfig->dst_addr, &sg_dma_address(sg),
641 sg_dma_len(sg), flags);
642
643 } else {
644 ret = sun6i_dma_cfg_lli(v_lli, sconfig->src_addr,
645 sg_dma_address(sg), sg_dma_len(sg),
646 sconfig);
647 if (ret)
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200648 goto err_cur_lli_free;
Maxime Ripard55585932014-07-17 21:46:16 +0200649
650 v_lli->cfg |= DMA_CHAN_CFG_DST_LINEAR_MODE |
651 DMA_CHAN_CFG_SRC_IO_MODE |
652 DMA_CHAN_CFG_DST_DRQ(DRQ_SDRAM) |
653 DMA_CHAN_CFG_SRC_DRQ(vchan->port);
654
655 dev_dbg(chan2dev(chan),
Vinod Koul7f5e03e2014-07-28 12:32:51 +0530656 "%s; chan: %d, dest: %pad, src: %pad, len: %u. flags: 0x%08lx\n",
Maxime Ripard55585932014-07-17 21:46:16 +0200657 __func__, vchan->vc.chan.chan_id,
658 &sg_dma_address(sg), &sconfig->src_addr,
659 sg_dma_len(sg), flags);
660 }
661
662 prev = sun6i_dma_lli_add(prev, v_lli, p_lli, txd);
663 }
664
665 dev_dbg(chan2dev(chan), "First: %pad\n", &txd->p_lli);
666 for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
667 sun6i_dma_dump_lli(vchan, prev);
668
669 return vchan_tx_prep(&vchan->vc, &txd->vd, flags);
670
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200671err_cur_lli_free:
Maxime Ripard55585932014-07-17 21:46:16 +0200672 dma_pool_free(sdev->pool, v_lli, p_lli);
Maxime Ripard4fbd8042014-07-30 10:30:23 +0200673err_lli_free:
674 for (prev = txd->v_lli; prev; prev = prev->v_lli_next)
675 dma_pool_free(sdev->pool, prev, virt_to_phys(prev));
676 kfree(txd);
Maxime Ripard55585932014-07-17 21:46:16 +0200677 return NULL;
678}
679
680static int sun6i_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
681 unsigned long arg)
682{
683 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
684 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
685 struct sun6i_pchan *pchan = vchan->phy;
686 unsigned long flags;
687 int ret = 0;
688
689 switch (cmd) {
690 case DMA_RESUME:
691 dev_dbg(chan2dev(chan), "vchan %p: resume\n", &vchan->vc);
692
693 spin_lock_irqsave(&vchan->vc.lock, flags);
694
695 if (pchan) {
696 writel(DMA_CHAN_PAUSE_RESUME,
697 pchan->base + DMA_CHAN_PAUSE);
698 } else if (!list_empty(&vchan->vc.desc_issued)) {
699 spin_lock(&sdev->lock);
700 list_add_tail(&vchan->node, &sdev->pending);
701 spin_unlock(&sdev->lock);
702 }
703
704 spin_unlock_irqrestore(&vchan->vc.lock, flags);
705 break;
706
707 case DMA_PAUSE:
708 dev_dbg(chan2dev(chan), "vchan %p: pause\n", &vchan->vc);
709
710 if (pchan) {
711 writel(DMA_CHAN_PAUSE_PAUSE,
712 pchan->base + DMA_CHAN_PAUSE);
713 } else {
714 spin_lock(&sdev->lock);
715 list_del_init(&vchan->node);
716 spin_unlock(&sdev->lock);
717 }
718 break;
719
720 case DMA_TERMINATE_ALL:
721 ret = sun6i_dma_terminate_all(vchan);
722 break;
723 case DMA_SLAVE_CONFIG:
724 memcpy(&vchan->cfg, (void *)arg, sizeof(struct dma_slave_config));
725 break;
726 default:
727 ret = -ENXIO;
728 break;
729 }
730 return ret;
731}
732
733static enum dma_status sun6i_dma_tx_status(struct dma_chan *chan,
734 dma_cookie_t cookie,
735 struct dma_tx_state *state)
736{
737 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
738 struct sun6i_pchan *pchan = vchan->phy;
739 struct sun6i_dma_lli *lli;
740 struct virt_dma_desc *vd;
741 struct sun6i_desc *txd;
742 enum dma_status ret;
743 unsigned long flags;
744 size_t bytes = 0;
745
746 ret = dma_cookie_status(chan, cookie, state);
747 if (ret == DMA_COMPLETE)
748 return ret;
749
750 spin_lock_irqsave(&vchan->vc.lock, flags);
751
752 vd = vchan_find_desc(&vchan->vc, cookie);
753 txd = to_sun6i_desc(&vd->tx);
754
755 if (vd) {
756 for (lli = txd->v_lli; lli != NULL; lli = lli->v_lli_next)
757 bytes += lli->len;
758 } else if (!pchan || !pchan->desc) {
759 bytes = 0;
760 } else {
761 bytes = readl(pchan->base + DMA_CHAN_CUR_CNT);
762 }
763
764 spin_unlock_irqrestore(&vchan->vc.lock, flags);
765
766 dma_set_residue(state, bytes);
767
768 return ret;
769}
770
771static void sun6i_dma_issue_pending(struct dma_chan *chan)
772{
773 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
774 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
775 unsigned long flags;
776
777 spin_lock_irqsave(&vchan->vc.lock, flags);
778
779 if (vchan_issue_pending(&vchan->vc)) {
780 spin_lock(&sdev->lock);
781
782 if (!vchan->phy && list_empty(&vchan->node)) {
783 list_add_tail(&vchan->node, &sdev->pending);
784 tasklet_schedule(&sdev->task);
785 dev_dbg(chan2dev(chan), "vchan %p: issued\n",
786 &vchan->vc);
787 }
788
789 spin_unlock(&sdev->lock);
790 } else {
791 dev_dbg(chan2dev(chan), "vchan %p: nothing to issue\n",
792 &vchan->vc);
793 }
794
795 spin_unlock_irqrestore(&vchan->vc.lock, flags);
796}
797
798static int sun6i_dma_alloc_chan_resources(struct dma_chan *chan)
799{
800 return 0;
801}
802
803static void sun6i_dma_free_chan_resources(struct dma_chan *chan)
804{
805 struct sun6i_dma_dev *sdev = to_sun6i_dma_dev(chan->device);
806 struct sun6i_vchan *vchan = to_sun6i_vchan(chan);
807 unsigned long flags;
808
809 spin_lock_irqsave(&sdev->lock, flags);
810 list_del_init(&vchan->node);
811 spin_unlock_irqrestore(&sdev->lock, flags);
812
813 vchan_free_chan_resources(&vchan->vc);
814}
815
816static struct dma_chan *sun6i_dma_of_xlate(struct of_phandle_args *dma_spec,
817 struct of_dma *ofdma)
818{
819 struct sun6i_dma_dev *sdev = ofdma->of_dma_data;
820 struct sun6i_vchan *vchan;
821 struct dma_chan *chan;
822 u8 port = dma_spec->args[0];
823
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800824 if (port > sdev->cfg->nr_max_requests)
Maxime Ripard55585932014-07-17 21:46:16 +0200825 return NULL;
826
827 chan = dma_get_any_slave_channel(&sdev->slave);
828 if (!chan)
829 return NULL;
830
831 vchan = to_sun6i_vchan(chan);
832 vchan->port = port;
833
834 return chan;
835}
836
837static inline void sun6i_kill_tasklet(struct sun6i_dma_dev *sdev)
838{
839 /* Disable all interrupts from DMA */
840 writel(0, sdev->base + DMA_IRQ_EN(0));
841 writel(0, sdev->base + DMA_IRQ_EN(1));
842
843 /* Prevent spurious interrupts from scheduling the tasklet */
844 atomic_inc(&sdev->tasklet_shutdown);
845
Maxime Ripard174427c2014-07-30 10:30:22 +0200846 /* Make sure we won't have any further interrupts */
847 devm_free_irq(sdev->slave.dev, sdev->irq, sdev);
Maxime Ripard55585932014-07-17 21:46:16 +0200848
849 /* Actually prevent the tasklet from being scheduled */
850 tasklet_kill(&sdev->task);
851}
852
853static inline void sun6i_dma_free(struct sun6i_dma_dev *sdev)
854{
855 int i;
856
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800857 for (i = 0; i < sdev->cfg->nr_max_vchans; i++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200858 struct sun6i_vchan *vchan = &sdev->vchans[i];
859
860 list_del(&vchan->vc.chan.device_node);
861 tasklet_kill(&vchan->vc.task);
862 }
863}
864
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800865/*
866 * For A31:
867 *
868 * There's 16 physical channels that can work in parallel.
869 *
870 * However we have 30 different endpoints for our requests.
871 *
872 * Since the channels are able to handle only an unidirectional
873 * transfer, we need to allocate more virtual channels so that
874 * everyone can grab one channel.
875 *
876 * Some devices can't work in both direction (mostly because it
877 * wouldn't make sense), so we have a bit fewer virtual channels than
878 * 2 channels per endpoints.
879 */
880
881static struct sun6i_dma_config sun6i_a31_dma_cfg = {
882 .nr_max_channels = 16,
883 .nr_max_requests = 30,
884 .nr_max_vchans = 53,
885};
886
Chen-Yu Tsai0b04ddf2014-11-07 12:15:47 +0800887/*
888 * The A23 only has 8 physical channels, a maximum DRQ port id of 24,
889 * and a total of 37 usable source and destination endpoints.
890 */
891
892static struct sun6i_dma_config sun8i_a23_dma_cfg = {
893 .nr_max_channels = 8,
894 .nr_max_requests = 24,
895 .nr_max_vchans = 37,
896};
897
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800898static struct of_device_id sun6i_dma_match[] = {
899 { .compatible = "allwinner,sun6i-a31-dma", .data = &sun6i_a31_dma_cfg },
Chen-Yu Tsai0b04ddf2014-11-07 12:15:47 +0800900 { .compatible = "allwinner,sun8i-a23-dma", .data = &sun8i_a23_dma_cfg },
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800901 { /* sentinel */ }
902};
903
Maxime Ripard55585932014-07-17 21:46:16 +0200904static int sun6i_dma_probe(struct platform_device *pdev)
905{
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800906 const struct of_device_id *device;
Maxime Ripard55585932014-07-17 21:46:16 +0200907 struct sun6i_dma_dev *sdc;
908 struct resource *res;
Maxime Ripard55585932014-07-17 21:46:16 +0200909 int ret, i;
910
911 sdc = devm_kzalloc(&pdev->dev, sizeof(*sdc), GFP_KERNEL);
912 if (!sdc)
913 return -ENOMEM;
914
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800915 device = of_match_device(sun6i_dma_match, &pdev->dev);
916 if (!device)
917 return -ENODEV;
918 sdc->cfg = device->data;
919
Maxime Ripard55585932014-07-17 21:46:16 +0200920 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
921 sdc->base = devm_ioremap_resource(&pdev->dev, res);
922 if (IS_ERR(sdc->base))
923 return PTR_ERR(sdc->base);
924
925 sdc->irq = platform_get_irq(pdev, 0);
926 if (sdc->irq < 0) {
927 dev_err(&pdev->dev, "Cannot claim IRQ\n");
928 return sdc->irq;
929 }
930
931 sdc->clk = devm_clk_get(&pdev->dev, NULL);
932 if (IS_ERR(sdc->clk)) {
933 dev_err(&pdev->dev, "No clock specified\n");
934 return PTR_ERR(sdc->clk);
935 }
936
Maxime Ripard55585932014-07-17 21:46:16 +0200937 sdc->rstc = devm_reset_control_get(&pdev->dev, NULL);
938 if (IS_ERR(sdc->rstc)) {
939 dev_err(&pdev->dev, "No reset controller specified\n");
940 return PTR_ERR(sdc->rstc);
941 }
942
943 sdc->pool = dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
944 sizeof(struct sun6i_dma_lli), 4, 0);
945 if (!sdc->pool) {
946 dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
947 return -ENOMEM;
948 }
949
950 platform_set_drvdata(pdev, sdc);
951 INIT_LIST_HEAD(&sdc->pending);
952 spin_lock_init(&sdc->lock);
953
954 dma_cap_set(DMA_PRIVATE, sdc->slave.cap_mask);
955 dma_cap_set(DMA_MEMCPY, sdc->slave.cap_mask);
956 dma_cap_set(DMA_SLAVE, sdc->slave.cap_mask);
957
958 INIT_LIST_HEAD(&sdc->slave.channels);
959 sdc->slave.device_alloc_chan_resources = sun6i_dma_alloc_chan_resources;
960 sdc->slave.device_free_chan_resources = sun6i_dma_free_chan_resources;
961 sdc->slave.device_tx_status = sun6i_dma_tx_status;
962 sdc->slave.device_issue_pending = sun6i_dma_issue_pending;
963 sdc->slave.device_prep_slave_sg = sun6i_dma_prep_slave_sg;
964 sdc->slave.device_prep_dma_memcpy = sun6i_dma_prep_dma_memcpy;
965 sdc->slave.device_control = sun6i_dma_control;
Maxime Ripard55585932014-07-17 21:46:16 +0200966
967 sdc->slave.dev = &pdev->dev;
968
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800969 sdc->pchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_channels,
Maxime Ripard55585932014-07-17 21:46:16 +0200970 sizeof(struct sun6i_pchan), GFP_KERNEL);
971 if (!sdc->pchans)
972 return -ENOMEM;
973
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800974 sdc->vchans = devm_kcalloc(&pdev->dev, sdc->cfg->nr_max_vchans,
Maxime Ripard55585932014-07-17 21:46:16 +0200975 sizeof(struct sun6i_vchan), GFP_KERNEL);
976 if (!sdc->vchans)
977 return -ENOMEM;
978
979 tasklet_init(&sdc->task, sun6i_dma_tasklet, (unsigned long)sdc);
980
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800981 for (i = 0; i < sdc->cfg->nr_max_channels; i++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200982 struct sun6i_pchan *pchan = &sdc->pchans[i];
983
984 pchan->idx = i;
985 pchan->base = sdc->base + 0x100 + i * 0x40;
986 }
987
Chen-Yu Tsai25a37c22014-11-07 12:15:46 +0800988 for (i = 0; i < sdc->cfg->nr_max_vchans; i++) {
Maxime Ripard55585932014-07-17 21:46:16 +0200989 struct sun6i_vchan *vchan = &sdc->vchans[i];
990
991 INIT_LIST_HEAD(&vchan->node);
992 vchan->vc.desc_free = sun6i_dma_free_desc;
993 vchan_init(&vchan->vc, &sdc->slave);
994 }
995
996 ret = reset_control_deassert(sdc->rstc);
997 if (ret) {
998 dev_err(&pdev->dev, "Couldn't deassert the device from reset\n");
999 goto err_chan_free;
1000 }
1001
1002 ret = clk_prepare_enable(sdc->clk);
1003 if (ret) {
1004 dev_err(&pdev->dev, "Couldn't enable the clock\n");
1005 goto err_reset_assert;
1006 }
1007
1008 ret = devm_request_irq(&pdev->dev, sdc->irq, sun6i_dma_interrupt, 0,
1009 dev_name(&pdev->dev), sdc);
1010 if (ret) {
1011 dev_err(&pdev->dev, "Cannot request IRQ\n");
1012 goto err_clk_disable;
1013 }
1014
1015 ret = dma_async_device_register(&sdc->slave);
1016 if (ret) {
1017 dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
1018 goto err_irq_disable;
1019 }
1020
1021 ret = of_dma_controller_register(pdev->dev.of_node, sun6i_dma_of_xlate,
1022 sdc);
1023 if (ret) {
1024 dev_err(&pdev->dev, "of_dma_controller_register failed\n");
1025 goto err_dma_unregister;
1026 }
1027
Chen-Yu Tsai0b04ddf2014-11-07 12:15:47 +08001028 /*
1029 * sun8i variant requires us to toggle a dma gating register,
1030 * as seen in Allwinner's SDK. This register is not documented
1031 * in the A23 user manual.
1032 */
1033 if (of_device_is_compatible(pdev->dev.of_node,
1034 "allwinner,sun8i-a23-dma"))
1035 writel(SUN8I_DMA_GATE_ENABLE, sdc->base + SUN8I_DMA_GATE);
1036
Maxime Ripard55585932014-07-17 21:46:16 +02001037 return 0;
1038
1039err_dma_unregister:
1040 dma_async_device_unregister(&sdc->slave);
1041err_irq_disable:
1042 sun6i_kill_tasklet(sdc);
1043err_clk_disable:
1044 clk_disable_unprepare(sdc->clk);
1045err_reset_assert:
1046 reset_control_assert(sdc->rstc);
1047err_chan_free:
1048 sun6i_dma_free(sdc);
1049 return ret;
1050}
1051
1052static int sun6i_dma_remove(struct platform_device *pdev)
1053{
1054 struct sun6i_dma_dev *sdc = platform_get_drvdata(pdev);
1055
1056 of_dma_controller_free(pdev->dev.of_node);
1057 dma_async_device_unregister(&sdc->slave);
1058
1059 sun6i_kill_tasklet(sdc);
1060
1061 clk_disable_unprepare(sdc->clk);
1062 reset_control_assert(sdc->rstc);
1063
1064 sun6i_dma_free(sdc);
1065
1066 return 0;
1067}
1068
Maxime Ripard55585932014-07-17 21:46:16 +02001069static struct platform_driver sun6i_dma_driver = {
1070 .probe = sun6i_dma_probe,
1071 .remove = sun6i_dma_remove,
1072 .driver = {
1073 .name = "sun6i-dma",
1074 .of_match_table = sun6i_dma_match,
1075 },
1076};
1077module_platform_driver(sun6i_dma_driver);
1078
1079MODULE_DESCRIPTION("Allwinner A31 DMA Controller Driver");
1080MODULE_AUTHOR("Sugar <shuge@allwinnertech.com>");
1081MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1082MODULE_LICENSE("GPL");