2 * Renesas R-Car Gen2 DMA Controller Driver
4 * Copyright (C) 2014 Renesas Electronics Inc.
6 * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
13 #include <linux/dma-mapping.h>
14 #include <linux/dmaengine.h>
15 #include <linux/interrupt.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
20 #include <linux/of_dma.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
27 #include "../dmaengine.h"
30 * struct rcar_dmac_xfer_chunk - Descriptor for a hardware transfer
31 * @node: entry in the parent's chunks list
32 * @src_addr: device source address
33 * @dst_addr: device destination address
34 * @size: transfer size in bytes
36 struct rcar_dmac_xfer_chunk {
37 struct list_head node;
45 * struct rcar_dmac_hw_desc - Hardware descriptor for a transfer chunk
46 * @sar: value of the SAR register (source address)
47 * @dar: value of the DAR register (destination address)
48 * @tcr: value of the TCR register (transfer count)
50 struct rcar_dmac_hw_desc {
55 } __attribute__((__packed__));
58 * struct rcar_dmac_desc - R-Car Gen2 DMA Transfer Descriptor
59 * @async_tx: base DMA asynchronous transaction descriptor
60 * @direction: direction of the DMA transfer
61 * @xfer_shift: log2 of the transfer size
62 * @chcr: value of the channel configuration register for this transfer
63 * @node: entry in the channel's descriptors lists
64 * @chunks: list of transfer chunks for this transfer
65 * @running: the transfer chunk being currently processed
66 * @nchunks: number of transfer chunks for this transfer
67 * @hwdescs.mem: hardware descriptors memory for the transfer
68 * @hwdescs.dma: device address of the hardware descriptors memory
69 * @hwdescs.size: size of the hardware descriptors in bytes
70 * @size: transfer size in bytes
71 * @cyclic: when set indicates that the DMA transfer is cyclic
73 struct rcar_dmac_desc {
74 struct dma_async_tx_descriptor async_tx;
75 enum dma_transfer_direction direction;
76 unsigned int xfer_shift;
79 struct list_head node;
80 struct list_head chunks;
81 struct rcar_dmac_xfer_chunk *running;
85 struct rcar_dmac_hw_desc *mem;
94 #define to_rcar_dmac_desc(d) container_of(d, struct rcar_dmac_desc, async_tx)
97 * struct rcar_dmac_desc_page - One page worth of descriptors
98 * @node: entry in the channel's pages list
99 * @descs: array of DMA descriptors
100 * @chunks: array of transfer chunk descriptors
102 struct rcar_dmac_desc_page {
103 struct list_head node;
106 struct rcar_dmac_desc descs[0];
107 struct rcar_dmac_xfer_chunk chunks[0];
111 #define RCAR_DMAC_DESCS_PER_PAGE \
112 ((PAGE_SIZE - offsetof(struct rcar_dmac_desc_page, descs)) / \
113 sizeof(struct rcar_dmac_desc))
114 #define RCAR_DMAC_XFER_CHUNKS_PER_PAGE \
115 ((PAGE_SIZE - offsetof(struct rcar_dmac_desc_page, chunks)) / \
116 sizeof(struct rcar_dmac_xfer_chunk))
119 * struct rcar_dmac_chan - R-Car Gen2 DMA Controller Channel
120 * @chan: base DMA channel object
121 * @iomem: channel I/O memory base
122 * @index: index of this channel in the controller
123 * @src_xfer_size: size (in bytes) of hardware transfers on the source side
124 * @dst_xfer_size: size (in bytes) of hardware transfers on the destination side
125 * @src_slave_addr: slave source memory address
126 * @dst_slave_addr: slave destination memory address
127 * @mid_rid: hardware MID/RID for the DMA client using this channel
128 * @lock: protects the channel CHCR register and the desc members
129 * @desc.free: list of free descriptors
130 * @desc.pending: list of pending descriptors (submitted with tx_submit)
131 * @desc.active: list of active descriptors (activated with issue_pending)
132 * @desc.done: list of completed descriptors
133 * @desc.wait: list of descriptors waiting for an ack
134 * @desc.running: the descriptor being processed (a member of the active list)
135 * @desc.chunks_free: list of free transfer chunk descriptors
136 * @desc.pages: list of pages used by allocated descriptors
138 struct rcar_dmac_chan {
139 struct dma_chan chan;
143 unsigned int src_xfer_size;
144 unsigned int dst_xfer_size;
145 dma_addr_t src_slave_addr;
146 dma_addr_t dst_slave_addr;
152 struct list_head free;
153 struct list_head pending;
154 struct list_head active;
155 struct list_head done;
156 struct list_head wait;
157 struct rcar_dmac_desc *running;
159 struct list_head chunks_free;
161 struct list_head pages;
165 #define to_rcar_dmac_chan(c) container_of(c, struct rcar_dmac_chan, chan)
168 * struct rcar_dmac - R-Car Gen2 DMA Controller
169 * @engine: base DMA engine object
170 * @dev: the hardware device
171 * @iomem: remapped I/O memory base
172 * @n_channels: number of available channels
173 * @channels: array of DMAC channels
174 * @modules: bitmask of client modules in use
177 struct dma_device engine;
181 unsigned int n_channels;
182 struct rcar_dmac_chan *channels;
184 unsigned long modules[256 / BITS_PER_LONG];
187 #define to_rcar_dmac(d) container_of(d, struct rcar_dmac, engine)
189 /* -----------------------------------------------------------------------------
193 #define RCAR_DMAC_CHAN_OFFSET(i) (0x8000 + 0x80 * (i))
195 #define RCAR_DMAISTA 0x0020
196 #define RCAR_DMASEC 0x0030
197 #define RCAR_DMAOR 0x0060
198 #define RCAR_DMAOR_PRI_FIXED (0 << 8)
199 #define RCAR_DMAOR_PRI_ROUND_ROBIN (3 << 8)
200 #define RCAR_DMAOR_AE (1 << 2)
201 #define RCAR_DMAOR_DME (1 << 0)
202 #define RCAR_DMACHCLR 0x0080
203 #define RCAR_DMADPSEC 0x00a0
205 #define RCAR_DMASAR 0x0000
206 #define RCAR_DMADAR 0x0004
207 #define RCAR_DMATCR 0x0008
208 #define RCAR_DMATCR_MASK 0x00ffffff
209 #define RCAR_DMATSR 0x0028
210 #define RCAR_DMACHCR 0x000c
211 #define RCAR_DMACHCR_CAE (1 << 31)
212 #define RCAR_DMACHCR_CAIE (1 << 30)
213 #define RCAR_DMACHCR_DPM_DISABLED (0 << 28)
214 #define RCAR_DMACHCR_DPM_ENABLED (1 << 28)
215 #define RCAR_DMACHCR_DPM_REPEAT (2 << 28)
216 #define RCAR_DMACHCR_DPM_INFINITE (3 << 28)
217 #define RCAR_DMACHCR_RPT_SAR (1 << 27)
218 #define RCAR_DMACHCR_RPT_DAR (1 << 26)
219 #define RCAR_DMACHCR_RPT_TCR (1 << 25)
220 #define RCAR_DMACHCR_DPB (1 << 22)
221 #define RCAR_DMACHCR_DSE (1 << 19)
222 #define RCAR_DMACHCR_DSIE (1 << 18)
223 #define RCAR_DMACHCR_TS_1B ((0 << 20) | (0 << 3))
224 #define RCAR_DMACHCR_TS_2B ((0 << 20) | (1 << 3))
225 #define RCAR_DMACHCR_TS_4B ((0 << 20) | (2 << 3))
226 #define RCAR_DMACHCR_TS_16B ((0 << 20) | (3 << 3))
227 #define RCAR_DMACHCR_TS_32B ((1 << 20) | (0 << 3))
228 #define RCAR_DMACHCR_TS_64B ((1 << 20) | (1 << 3))
229 #define RCAR_DMACHCR_TS_8B ((1 << 20) | (3 << 3))
230 #define RCAR_DMACHCR_DM_FIXED (0 << 14)
231 #define RCAR_DMACHCR_DM_INC (1 << 14)
232 #define RCAR_DMACHCR_DM_DEC (2 << 14)
233 #define RCAR_DMACHCR_SM_FIXED (0 << 12)
234 #define RCAR_DMACHCR_SM_INC (1 << 12)
235 #define RCAR_DMACHCR_SM_DEC (2 << 12)
236 #define RCAR_DMACHCR_RS_AUTO (4 << 8)
237 #define RCAR_DMACHCR_RS_DMARS (8 << 8)
238 #define RCAR_DMACHCR_IE (1 << 2)
239 #define RCAR_DMACHCR_TE (1 << 1)
240 #define RCAR_DMACHCR_DE (1 << 0)
241 #define RCAR_DMATCRB 0x0018
242 #define RCAR_DMATSRB 0x0038
243 #define RCAR_DMACHCRB 0x001c
244 #define RCAR_DMACHCRB_DCNT(n) ((n) << 24)
245 #define RCAR_DMACHCRB_DPTR_MASK (0xff << 16)
246 #define RCAR_DMACHCRB_DPTR_SHIFT 16
247 #define RCAR_DMACHCRB_DRST (1 << 15)
248 #define RCAR_DMACHCRB_DTS (1 << 8)
249 #define RCAR_DMACHCRB_SLM_NORMAL (0 << 4)
250 #define RCAR_DMACHCRB_SLM_CLK(n) ((8 | (n)) << 4)
251 #define RCAR_DMACHCRB_PRI(n) ((n) << 0)
252 #define RCAR_DMARS 0x0040
253 #define RCAR_DMABUFCR 0x0048
254 #define RCAR_DMABUFCR_MBU(n) ((n) << 16)
255 #define RCAR_DMABUFCR_ULB(n) ((n) << 0)
256 #define RCAR_DMADPBASE 0x0050
257 #define RCAR_DMADPBASE_MASK 0xfffffff0
258 #define RCAR_DMADPBASE_SEL (1 << 0)
259 #define RCAR_DMADPCR 0x0054
260 #define RCAR_DMADPCR_DIPT(n) ((n) << 24)
261 #define RCAR_DMAFIXSAR 0x0010
262 #define RCAR_DMAFIXDAR 0x0014
263 #define RCAR_DMAFIXDPBASE 0x0060
265 /* Hardcode the MEMCPY transfer size to 4 bytes. */
266 #define RCAR_DMAC_MEMCPY_XFER_SIZE 4
268 /* -----------------------------------------------------------------------------
272 static void rcar_dmac_write(struct rcar_dmac *dmac, u32 reg, u32 data)
274 if (reg == RCAR_DMAOR)
275 writew(data, dmac->iomem + reg);
277 writel(data, dmac->iomem + reg);
280 static u32 rcar_dmac_read(struct rcar_dmac *dmac, u32 reg)
282 if (reg == RCAR_DMAOR)
283 return readw(dmac->iomem + reg);
285 return readl(dmac->iomem + reg);
288 static u32 rcar_dmac_chan_read(struct rcar_dmac_chan *chan, u32 reg)
290 if (reg == RCAR_DMARS)
291 return readw(chan->iomem + reg);
293 return readl(chan->iomem + reg);
296 static void rcar_dmac_chan_write(struct rcar_dmac_chan *chan, u32 reg, u32 data)
298 if (reg == RCAR_DMARS)
299 writew(data, chan->iomem + reg);
301 writel(data, chan->iomem + reg);
304 /* -----------------------------------------------------------------------------
305 * Initialization and configuration
308 static bool rcar_dmac_chan_is_busy(struct rcar_dmac_chan *chan)
310 u32 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
312 return (chcr & (RCAR_DMACHCR_DE | RCAR_DMACHCR_TE)) == RCAR_DMACHCR_DE;
315 static void rcar_dmac_chan_start_xfer(struct rcar_dmac_chan *chan)
317 struct rcar_dmac_desc *desc = chan->desc.running;
318 u32 chcr = desc->chcr;
320 WARN_ON_ONCE(rcar_dmac_chan_is_busy(chan));
322 if (chan->mid_rid >= 0)
323 rcar_dmac_chan_write(chan, RCAR_DMARS, chan->mid_rid);
325 if (desc->hwdescs.mem) {
326 dev_dbg(chan->chan.device->dev,
327 "chan%u: queue desc %p: %u@%pad\n",
328 chan->index, desc, desc->nchunks, &desc->hwdescs.dma);
330 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
331 rcar_dmac_chan_write(chan, RCAR_DMAFIXDPBASE,
332 desc->hwdescs.dma >> 32);
334 rcar_dmac_chan_write(chan, RCAR_DMADPBASE,
335 (desc->hwdescs.dma & 0xfffffff0) |
337 rcar_dmac_chan_write(chan, RCAR_DMACHCRB,
338 RCAR_DMACHCRB_DCNT(desc->nchunks - 1) |
342 * Program the descriptor stage interrupt to occur after the end
343 * of the first stage.
345 rcar_dmac_chan_write(chan, RCAR_DMADPCR, RCAR_DMADPCR_DIPT(1));
347 chcr |= RCAR_DMACHCR_RPT_SAR | RCAR_DMACHCR_RPT_DAR
348 | RCAR_DMACHCR_RPT_TCR | RCAR_DMACHCR_DPB;
351 * If the descriptor isn't cyclic enable normal descriptor mode
352 * and the transfer completion interrupt.
355 chcr |= RCAR_DMACHCR_DPM_ENABLED | RCAR_DMACHCR_IE;
357 * If the descriptor is cyclic and has a callback enable the
358 * descriptor stage interrupt in infinite repeat mode.
360 else if (desc->async_tx.callback)
361 chcr |= RCAR_DMACHCR_DPM_INFINITE | RCAR_DMACHCR_DSIE;
363 * Otherwise just select infinite repeat mode without any
367 chcr |= RCAR_DMACHCR_DPM_INFINITE;
369 struct rcar_dmac_xfer_chunk *chunk = desc->running;
371 dev_dbg(chan->chan.device->dev,
372 "chan%u: queue chunk %p: %u@%pad -> %pad\n",
373 chan->index, chunk, chunk->size, &chunk->src_addr,
376 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
377 rcar_dmac_chan_write(chan, RCAR_DMAFIXSAR,
378 chunk->src_addr >> 32);
379 rcar_dmac_chan_write(chan, RCAR_DMAFIXDAR,
380 chunk->dst_addr >> 32);
382 rcar_dmac_chan_write(chan, RCAR_DMASAR,
383 chunk->src_addr & 0xffffffff);
384 rcar_dmac_chan_write(chan, RCAR_DMADAR,
385 chunk->dst_addr & 0xffffffff);
386 rcar_dmac_chan_write(chan, RCAR_DMATCR,
387 chunk->size >> desc->xfer_shift);
389 chcr |= RCAR_DMACHCR_DPM_DISABLED | RCAR_DMACHCR_IE;
392 rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr | RCAR_DMACHCR_DE);
395 static int rcar_dmac_init(struct rcar_dmac *dmac)
399 /* Clear all channels and enable the DMAC globally. */
400 rcar_dmac_write(dmac, RCAR_DMACHCLR, 0x7fff);
401 rcar_dmac_write(dmac, RCAR_DMAOR,
402 RCAR_DMAOR_PRI_FIXED | RCAR_DMAOR_DME);
404 dmaor = rcar_dmac_read(dmac, RCAR_DMAOR);
405 if ((dmaor & (RCAR_DMAOR_AE | RCAR_DMAOR_DME)) != RCAR_DMAOR_DME) {
406 dev_warn(dmac->dev, "DMAOR initialization failed.\n");
413 /* -----------------------------------------------------------------------------
414 * Descriptors submission
417 static dma_cookie_t rcar_dmac_tx_submit(struct dma_async_tx_descriptor *tx)
419 struct rcar_dmac_chan *chan = to_rcar_dmac_chan(tx->chan);
420 struct rcar_dmac_desc *desc = to_rcar_dmac_desc(tx);
424 spin_lock_irqsave(&chan->lock, flags);
426 cookie = dma_cookie_assign(tx);
428 dev_dbg(chan->chan.device->dev, "chan%u: submit #%d@%p\n",
429 chan->index, tx->cookie, desc);
431 list_add_tail(&desc->node, &chan->desc.pending);
432 desc->running = list_first_entry(&desc->chunks,
433 struct rcar_dmac_xfer_chunk, node);
435 spin_unlock_irqrestore(&chan->lock, flags);
440 /* -----------------------------------------------------------------------------
441 * Descriptors allocation and free
445 * rcar_dmac_desc_alloc - Allocate a page worth of DMA descriptors
446 * @chan: the DMA channel
447 * @gfp: allocation flags
449 static int rcar_dmac_desc_alloc(struct rcar_dmac_chan *chan, gfp_t gfp)
451 struct rcar_dmac_desc_page *page;
455 page = (void *)get_zeroed_page(gfp);
459 for (i = 0; i < RCAR_DMAC_DESCS_PER_PAGE; ++i) {
460 struct rcar_dmac_desc *desc = &page->descs[i];
462 dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan);
463 desc->async_tx.tx_submit = rcar_dmac_tx_submit;
464 INIT_LIST_HEAD(&desc->chunks);
466 list_add_tail(&desc->node, &list);
469 spin_lock_irq(&chan->lock);
470 list_splice_tail(&list, &chan->desc.free);
471 list_add_tail(&page->node, &chan->desc.pages);
472 spin_unlock_irq(&chan->lock);
478 * rcar_dmac_desc_put - Release a DMA transfer descriptor
479 * @chan: the DMA channel
480 * @desc: the descriptor
482 * Put the descriptor and its transfer chunk descriptors back in the channel's
483 * free descriptors lists, and free the hardware descriptors list memory. The
484 * descriptor's chunks list will be reinitialized to an empty list as a result.
486 * The descriptor must have been removed from the channel's lists before calling
489 * Locking: Must be called in non-atomic context.
491 static void rcar_dmac_desc_put(struct rcar_dmac_chan *chan,
492 struct rcar_dmac_desc *desc)
494 if (desc->hwdescs.mem) {
495 dma_free_coherent(NULL, desc->hwdescs.size, desc->hwdescs.mem,
497 desc->hwdescs.mem = NULL;
500 spin_lock_irq(&chan->lock);
501 list_splice_tail_init(&desc->chunks, &chan->desc.chunks_free);
502 list_add_tail(&desc->node, &chan->desc.free);
503 spin_unlock_irq(&chan->lock);
506 static void rcar_dmac_desc_recycle_acked(struct rcar_dmac_chan *chan)
508 struct rcar_dmac_desc *desc, *_desc;
512 * We have to temporarily move all descriptors from the wait list to a
513 * local list as iterating over the wait list, even with
514 * list_for_each_entry_safe, isn't safe if we release the channel lock
515 * around the rcar_dmac_desc_put() call.
517 spin_lock_irq(&chan->lock);
518 list_splice_init(&chan->desc.wait, &list);
519 spin_unlock_irq(&chan->lock);
521 list_for_each_entry_safe(desc, _desc, &list, node) {
522 if (async_tx_test_ack(&desc->async_tx)) {
523 list_del(&desc->node);
524 rcar_dmac_desc_put(chan, desc);
528 if (list_empty(&list))
531 /* Put the remaining descriptors back in the wait list. */
532 spin_lock_irq(&chan->lock);
533 list_splice(&list, &chan->desc.wait);
534 spin_unlock_irq(&chan->lock);
538 * rcar_dmac_desc_get - Allocate a descriptor for a DMA transfer
539 * @chan: the DMA channel
541 * Locking: This function must be called in a non-atomic context.
543 * Return: A pointer to the allocated descriptor or NULL if no descriptor can
546 static struct rcar_dmac_desc *rcar_dmac_desc_get(struct rcar_dmac_chan *chan)
548 struct rcar_dmac_desc *desc;
551 /* Recycle acked descriptors before attempting allocation. */
552 rcar_dmac_desc_recycle_acked(chan);
554 spin_lock_irq(&chan->lock);
557 if (list_empty(&chan->desc.free)) {
559 * No free descriptors, allocate a page worth of them
560 * and try again, as someone else could race us to get
561 * the newly allocated descriptors. If the allocation
562 * fails return an error.
564 spin_unlock_irq(&chan->lock);
565 ret = rcar_dmac_desc_alloc(chan, GFP_NOWAIT);
568 spin_lock_irq(&chan->lock);
572 desc = list_first_entry(&chan->desc.free, struct rcar_dmac_desc,
574 list_del(&desc->node);
577 spin_unlock_irq(&chan->lock);
583 * rcar_dmac_xfer_chunk_alloc - Allocate a page worth of transfer chunks
584 * @chan: the DMA channel
585 * @gfp: allocation flags
587 static int rcar_dmac_xfer_chunk_alloc(struct rcar_dmac_chan *chan, gfp_t gfp)
589 struct rcar_dmac_desc_page *page;
593 page = (void *)get_zeroed_page(gfp);
597 for (i = 0; i < RCAR_DMAC_XFER_CHUNKS_PER_PAGE; ++i) {
598 struct rcar_dmac_xfer_chunk *chunk = &page->chunks[i];
600 list_add_tail(&chunk->node, &list);
603 spin_lock_irq(&chan->lock);
604 list_splice_tail(&list, &chan->desc.chunks_free);
605 list_add_tail(&page->node, &chan->desc.pages);
606 spin_unlock_irq(&chan->lock);
612 * rcar_dmac_xfer_chunk_get - Allocate a transfer chunk for a DMA transfer
613 * @chan: the DMA channel
615 * Locking: This function must be called in a non-atomic context.
617 * Return: A pointer to the allocated transfer chunk descriptor or NULL if no
618 * descriptor can be allocated.
620 static struct rcar_dmac_xfer_chunk *
621 rcar_dmac_xfer_chunk_get(struct rcar_dmac_chan *chan)
623 struct rcar_dmac_xfer_chunk *chunk;
626 spin_lock_irq(&chan->lock);
629 if (list_empty(&chan->desc.chunks_free)) {
631 * No free descriptors, allocate a page worth of them
632 * and try again, as someone else could race us to get
633 * the newly allocated descriptors. If the allocation
634 * fails return an error.
636 spin_unlock_irq(&chan->lock);
637 ret = rcar_dmac_xfer_chunk_alloc(chan, GFP_NOWAIT);
640 spin_lock_irq(&chan->lock);
644 chunk = list_first_entry(&chan->desc.chunks_free,
645 struct rcar_dmac_xfer_chunk, node);
646 list_del(&chunk->node);
649 spin_unlock_irq(&chan->lock);
654 static void rcar_dmac_alloc_hwdesc(struct rcar_dmac_chan *chan,
655 struct rcar_dmac_desc *desc)
657 struct rcar_dmac_xfer_chunk *chunk;
658 struct rcar_dmac_hw_desc *hwdesc;
659 size_t size = desc->nchunks * sizeof(*hwdesc);
661 hwdesc = dma_alloc_coherent(NULL, size, &desc->hwdescs.dma, GFP_NOWAIT);
665 desc->hwdescs.mem = hwdesc;
666 desc->hwdescs.size = size;
668 list_for_each_entry(chunk, &desc->chunks, node) {
669 hwdesc->sar = chunk->src_addr;
670 hwdesc->dar = chunk->dst_addr;
671 hwdesc->tcr = chunk->size >> desc->xfer_shift;
676 /* -----------------------------------------------------------------------------
680 static void rcar_dmac_chan_halt(struct rcar_dmac_chan *chan)
682 u32 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
684 chcr &= ~(RCAR_DMACHCR_DSE | RCAR_DMACHCR_DSIE | RCAR_DMACHCR_IE |
685 RCAR_DMACHCR_TE | RCAR_DMACHCR_DE);
686 rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr);
689 static void rcar_dmac_chan_reinit(struct rcar_dmac_chan *chan)
691 struct rcar_dmac_desc *desc, *_desc;
695 spin_lock_irqsave(&chan->lock, flags);
697 /* Move all non-free descriptors to the local lists. */
698 list_splice_init(&chan->desc.pending, &descs);
699 list_splice_init(&chan->desc.active, &descs);
700 list_splice_init(&chan->desc.done, &descs);
701 list_splice_init(&chan->desc.wait, &descs);
703 chan->desc.running = NULL;
705 spin_unlock_irqrestore(&chan->lock, flags);
707 list_for_each_entry_safe(desc, _desc, &descs, node) {
708 list_del(&desc->node);
709 rcar_dmac_desc_put(chan, desc);
713 static void rcar_dmac_stop(struct rcar_dmac *dmac)
715 rcar_dmac_write(dmac, RCAR_DMAOR, 0);
718 static void rcar_dmac_abort(struct rcar_dmac *dmac)
722 /* Stop all channels. */
723 for (i = 0; i < dmac->n_channels; ++i) {
724 struct rcar_dmac_chan *chan = &dmac->channels[i];
726 /* Stop and reinitialize the channel. */
727 spin_lock(&chan->lock);
728 rcar_dmac_chan_halt(chan);
729 spin_unlock(&chan->lock);
731 rcar_dmac_chan_reinit(chan);
735 /* -----------------------------------------------------------------------------
736 * Descriptors preparation
739 static void rcar_dmac_chan_configure_desc(struct rcar_dmac_chan *chan,
740 struct rcar_dmac_desc *desc)
742 static const u32 chcr_ts[] = {
743 RCAR_DMACHCR_TS_1B, RCAR_DMACHCR_TS_2B,
744 RCAR_DMACHCR_TS_4B, RCAR_DMACHCR_TS_8B,
745 RCAR_DMACHCR_TS_16B, RCAR_DMACHCR_TS_32B,
749 unsigned int xfer_size;
752 switch (desc->direction) {
754 chcr = RCAR_DMACHCR_DM_INC | RCAR_DMACHCR_SM_FIXED
755 | RCAR_DMACHCR_RS_DMARS;
756 xfer_size = chan->src_xfer_size;
760 chcr = RCAR_DMACHCR_DM_FIXED | RCAR_DMACHCR_SM_INC
761 | RCAR_DMACHCR_RS_DMARS;
762 xfer_size = chan->dst_xfer_size;
767 chcr = RCAR_DMACHCR_DM_INC | RCAR_DMACHCR_SM_INC
768 | RCAR_DMACHCR_RS_AUTO;
769 xfer_size = RCAR_DMAC_MEMCPY_XFER_SIZE;
773 desc->xfer_shift = ilog2(xfer_size);
774 desc->chcr = chcr | chcr_ts[desc->xfer_shift];
778 * rcar_dmac_chan_prep_sg - prepare transfer descriptors from an SG list
780 * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
781 * converted to scatter-gather to guarantee consistent locking and a correct
782 * list manipulation. For slave DMA direction carries the usual meaning, and,
783 * logically, the SG list is RAM and the addr variable contains slave address,
784 * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_MEM_TO_MEM
785 * and the SG list contains only one element and points at the source buffer.
787 static struct dma_async_tx_descriptor *
788 rcar_dmac_chan_prep_sg(struct rcar_dmac_chan *chan, struct scatterlist *sgl,
789 unsigned int sg_len, dma_addr_t dev_addr,
790 enum dma_transfer_direction dir, unsigned long dma_flags,
793 struct rcar_dmac_xfer_chunk *chunk;
794 struct rcar_dmac_desc *desc;
795 struct scatterlist *sg;
796 unsigned int nchunks = 0;
797 unsigned int max_chunk_size;
798 unsigned int full_size = 0;
799 bool highmem = false;
802 desc = rcar_dmac_desc_get(chan);
806 desc->async_tx.flags = dma_flags;
807 desc->async_tx.cookie = -EBUSY;
809 desc->cyclic = cyclic;
810 desc->direction = dir;
812 rcar_dmac_chan_configure_desc(chan, desc);
814 max_chunk_size = (RCAR_DMATCR_MASK + 1) << desc->xfer_shift;
817 * Allocate and fill the transfer chunk descriptors. We own the only
818 * reference to the DMA descriptor, there's no need for locking.
820 for_each_sg(sgl, sg, sg_len, i) {
821 dma_addr_t mem_addr = sg_dma_address(sg);
822 unsigned int len = sg_dma_len(sg);
827 unsigned int size = min(len, max_chunk_size);
829 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
831 * Prevent individual transfers from crossing 4GB
834 if (dev_addr >> 32 != (dev_addr + size - 1) >> 32)
835 size = ALIGN(dev_addr, 1ULL << 32) - dev_addr;
836 if (mem_addr >> 32 != (mem_addr + size - 1) >> 32)
837 size = ALIGN(mem_addr, 1ULL << 32) - mem_addr;
840 * Check if either of the source or destination address
841 * can't be expressed in 32 bits. If so we can't use
842 * hardware descriptor lists.
844 if (dev_addr >> 32 || mem_addr >> 32)
848 chunk = rcar_dmac_xfer_chunk_get(chan);
850 rcar_dmac_desc_put(chan, desc);
854 if (dir == DMA_DEV_TO_MEM) {
855 chunk->src_addr = dev_addr;
856 chunk->dst_addr = mem_addr;
858 chunk->src_addr = mem_addr;
859 chunk->dst_addr = dev_addr;
864 dev_dbg(chan->chan.device->dev,
865 "chan%u: chunk %p/%p sgl %u@%p, %u/%u %pad -> %pad\n",
866 chan->index, chunk, desc, i, sg, size, len,
867 &chunk->src_addr, &chunk->dst_addr);
870 if (dir == DMA_MEM_TO_MEM)
875 list_add_tail(&chunk->node, &desc->chunks);
880 desc->nchunks = nchunks;
881 desc->size = full_size;
884 * Use hardware descriptor lists if possible when more than one chunk
885 * needs to be transferred (otherwise they don't make much sense).
887 * The highmem check currently covers the whole transfer. As an
888 * optimization we could use descriptor lists for consecutive lowmem
889 * chunks and direct manual mode for highmem chunks. Whether the
890 * performance improvement would be significant enough compared to the
891 * additional complexity remains to be investigated.
893 if (!highmem && nchunks > 1)
894 rcar_dmac_alloc_hwdesc(chan, desc);
896 return &desc->async_tx;
899 /* -----------------------------------------------------------------------------
900 * DMA engine operations
903 static int rcar_dmac_alloc_chan_resources(struct dma_chan *chan)
905 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
908 INIT_LIST_HEAD(&rchan->desc.free);
909 INIT_LIST_HEAD(&rchan->desc.pending);
910 INIT_LIST_HEAD(&rchan->desc.active);
911 INIT_LIST_HEAD(&rchan->desc.done);
912 INIT_LIST_HEAD(&rchan->desc.wait);
913 INIT_LIST_HEAD(&rchan->desc.chunks_free);
914 INIT_LIST_HEAD(&rchan->desc.pages);
916 /* Preallocate descriptors. */
917 ret = rcar_dmac_xfer_chunk_alloc(rchan, GFP_KERNEL);
921 ret = rcar_dmac_desc_alloc(rchan, GFP_KERNEL);
925 return pm_runtime_get_sync(chan->device->dev);
928 static void rcar_dmac_free_chan_resources(struct dma_chan *chan)
930 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
931 struct rcar_dmac *dmac = to_rcar_dmac(chan->device);
932 struct rcar_dmac_desc_page *page, *_page;
934 /* Protect against ISR */
935 spin_lock_irq(&rchan->lock);
936 rcar_dmac_chan_halt(rchan);
937 spin_unlock_irq(&rchan->lock);
939 /* Now no new interrupts will occur */
941 if (rchan->mid_rid >= 0) {
942 /* The caller is holding dma_list_mutex */
943 clear_bit(rchan->mid_rid, dmac->modules);
944 rchan->mid_rid = -EINVAL;
947 list_for_each_entry_safe(page, _page, &rchan->desc.pages, node) {
948 list_del(&page->node);
949 free_page((unsigned long)page);
952 pm_runtime_put(chan->device->dev);
955 static struct dma_async_tx_descriptor *
956 rcar_dmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
957 dma_addr_t dma_src, size_t len, unsigned long flags)
959 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
960 struct scatterlist sgl;
965 sg_init_table(&sgl, 1);
966 sg_set_page(&sgl, pfn_to_page(PFN_DOWN(dma_src)), len,
967 offset_in_page(dma_src));
968 sg_dma_address(&sgl) = dma_src;
969 sg_dma_len(&sgl) = len;
971 return rcar_dmac_chan_prep_sg(rchan, &sgl, 1, dma_dest,
972 DMA_MEM_TO_MEM, flags, false);
975 static struct dma_async_tx_descriptor *
976 rcar_dmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
977 unsigned int sg_len, enum dma_transfer_direction dir,
978 unsigned long flags, void *context)
980 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
983 /* Someone calling slave DMA on a generic channel? */
984 if (rchan->mid_rid < 0 || !sg_len) {
985 dev_warn(chan->device->dev,
986 "%s: bad parameter: len=%d, id=%d\n",
987 __func__, sg_len, rchan->mid_rid);
991 dev_addr = dir == DMA_DEV_TO_MEM
992 ? rchan->src_slave_addr : rchan->dst_slave_addr;
993 return rcar_dmac_chan_prep_sg(rchan, sgl, sg_len, dev_addr,
997 #define RCAR_DMAC_MAX_SG_LEN 32
999 static struct dma_async_tx_descriptor *
1000 rcar_dmac_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
1001 size_t buf_len, size_t period_len,
1002 enum dma_transfer_direction dir, unsigned long flags)
1004 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1005 struct dma_async_tx_descriptor *desc;
1006 struct scatterlist *sgl;
1007 dma_addr_t dev_addr;
1008 unsigned int sg_len;
1011 /* Someone calling slave DMA on a generic channel? */
1012 if (rchan->mid_rid < 0 || buf_len < period_len) {
1013 dev_warn(chan->device->dev,
1014 "%s: bad parameter: buf_len=%zu, period_len=%zu, id=%d\n",
1015 __func__, buf_len, period_len, rchan->mid_rid);
1019 sg_len = buf_len / period_len;
1020 if (sg_len > RCAR_DMAC_MAX_SG_LEN) {
1021 dev_err(chan->device->dev,
1022 "chan%u: sg length %d exceds limit %d",
1023 rchan->index, sg_len, RCAR_DMAC_MAX_SG_LEN);
1028 * Allocate the sg list dynamically as it would consume too much stack
1031 sgl = kcalloc(sg_len, sizeof(*sgl), GFP_NOWAIT);
1035 sg_init_table(sgl, sg_len);
1037 for (i = 0; i < sg_len; ++i) {
1038 dma_addr_t src = buf_addr + (period_len * i);
1040 sg_set_page(&sgl[i], pfn_to_page(PFN_DOWN(src)), period_len,
1041 offset_in_page(src));
1042 sg_dma_address(&sgl[i]) = src;
1043 sg_dma_len(&sgl[i]) = period_len;
1046 dev_addr = dir == DMA_DEV_TO_MEM
1047 ? rchan->src_slave_addr : rchan->dst_slave_addr;
1048 desc = rcar_dmac_chan_prep_sg(rchan, sgl, sg_len, dev_addr,
1055 static int rcar_dmac_device_config(struct dma_chan *chan,
1056 struct dma_slave_config *cfg)
1058 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1061 * We could lock this, but you shouldn't be configuring the
1062 * channel, while using it...
1064 rchan->src_slave_addr = cfg->src_addr;
1065 rchan->dst_slave_addr = cfg->dst_addr;
1066 rchan->src_xfer_size = cfg->src_addr_width;
1067 rchan->dst_xfer_size = cfg->dst_addr_width;
1072 static int rcar_dmac_chan_terminate_all(struct dma_chan *chan)
1074 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1075 unsigned long flags;
1077 spin_lock_irqsave(&rchan->lock, flags);
1078 rcar_dmac_chan_halt(rchan);
1079 spin_unlock_irqrestore(&rchan->lock, flags);
1082 * FIXME: No new interrupt can occur now, but the IRQ thread might still
1086 rcar_dmac_chan_reinit(rchan);
1091 static unsigned int rcar_dmac_chan_get_residue(struct rcar_dmac_chan *chan,
1092 dma_cookie_t cookie)
1094 struct rcar_dmac_desc *desc = chan->desc.running;
1095 struct rcar_dmac_xfer_chunk *running = NULL;
1096 struct rcar_dmac_xfer_chunk *chunk;
1097 unsigned int residue = 0;
1098 unsigned int dptr = 0;
1104 * If the cookie doesn't correspond to the currently running transfer
1105 * then the descriptor hasn't been processed yet, and the residue is
1106 * equal to the full descriptor size.
1108 if (cookie != desc->async_tx.cookie)
1112 * In descriptor mode the descriptor running pointer is not maintained
1113 * by the interrupt handler, find the running descriptor from the
1114 * descriptor pointer field in the CHCRB register. In non-descriptor
1115 * mode just use the running descriptor pointer.
1117 if (desc->hwdescs.mem) {
1118 dptr = (rcar_dmac_chan_read(chan, RCAR_DMACHCRB) &
1119 RCAR_DMACHCRB_DPTR_MASK) >> RCAR_DMACHCRB_DPTR_SHIFT;
1120 WARN_ON(dptr >= desc->nchunks);
1122 running = desc->running;
1125 /* Compute the size of all chunks still to be transferred. */
1126 list_for_each_entry_reverse(chunk, &desc->chunks, node) {
1127 if (chunk == running || ++dptr == desc->nchunks)
1130 residue += chunk->size;
1133 /* Add the residue for the current chunk. */
1134 residue += rcar_dmac_chan_read(chan, RCAR_DMATCR) << desc->xfer_shift;
1139 static enum dma_status rcar_dmac_tx_status(struct dma_chan *chan,
1140 dma_cookie_t cookie,
1141 struct dma_tx_state *txstate)
1143 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1144 enum dma_status status;
1145 unsigned long flags;
1146 unsigned int residue;
1148 status = dma_cookie_status(chan, cookie, txstate);
1149 if (status == DMA_COMPLETE || !txstate)
1152 spin_lock_irqsave(&rchan->lock, flags);
1153 residue = rcar_dmac_chan_get_residue(rchan, cookie);
1154 spin_unlock_irqrestore(&rchan->lock, flags);
1156 dma_set_residue(txstate, residue);
1161 static void rcar_dmac_issue_pending(struct dma_chan *chan)
1163 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1164 unsigned long flags;
1166 spin_lock_irqsave(&rchan->lock, flags);
1168 if (list_empty(&rchan->desc.pending))
1171 /* Append the pending list to the active list. */
1172 list_splice_tail_init(&rchan->desc.pending, &rchan->desc.active);
1175 * If no transfer is running pick the first descriptor from the active
1176 * list and start the transfer.
1178 if (!rchan->desc.running) {
1179 struct rcar_dmac_desc *desc;
1181 desc = list_first_entry(&rchan->desc.active,
1182 struct rcar_dmac_desc, node);
1183 rchan->desc.running = desc;
1185 rcar_dmac_chan_start_xfer(rchan);
1189 spin_unlock_irqrestore(&rchan->lock, flags);
1192 /* -----------------------------------------------------------------------------
1196 static irqreturn_t rcar_dmac_isr_desc_stage_end(struct rcar_dmac_chan *chan)
1198 struct rcar_dmac_desc *desc = chan->desc.running;
1201 if (WARN_ON(!desc || !desc->cyclic)) {
1203 * This should never happen, there should always be a running
1204 * cyclic descriptor when a descriptor stage end interrupt is
1205 * triggered. Warn and return.
1210 /* Program the interrupt pointer to the next stage. */
1211 stage = (rcar_dmac_chan_read(chan, RCAR_DMACHCRB) &
1212 RCAR_DMACHCRB_DPTR_MASK) >> RCAR_DMACHCRB_DPTR_SHIFT;
1213 rcar_dmac_chan_write(chan, RCAR_DMADPCR, RCAR_DMADPCR_DIPT(stage));
1215 return IRQ_WAKE_THREAD;
1218 static irqreturn_t rcar_dmac_isr_transfer_end(struct rcar_dmac_chan *chan)
1220 struct rcar_dmac_desc *desc = chan->desc.running;
1221 irqreturn_t ret = IRQ_WAKE_THREAD;
1223 if (WARN_ON_ONCE(!desc)) {
1225 * This should never happen, there should always be a running
1226 * descriptor when a transfer end interrupt is triggered. Warn
1233 * The transfer end interrupt isn't generated for each chunk when using
1234 * descriptor mode. Only update the running chunk pointer in
1235 * non-descriptor mode.
1237 if (!desc->hwdescs.mem) {
1239 * If we haven't completed the last transfer chunk simply move
1240 * to the next one. Only wake the IRQ thread if the transfer is
1243 if (!list_is_last(&desc->running->node, &desc->chunks)) {
1244 desc->running = list_next_entry(desc->running, node);
1251 * We've completed the last transfer chunk. If the transfer is
1252 * cyclic, move back to the first one.
1256 list_first_entry(&desc->chunks,
1257 struct rcar_dmac_xfer_chunk,
1263 /* The descriptor is complete, move it to the done list. */
1264 list_move_tail(&desc->node, &chan->desc.done);
1266 /* Queue the next descriptor, if any. */
1267 if (!list_empty(&chan->desc.active))
1268 chan->desc.running = list_first_entry(&chan->desc.active,
1269 struct rcar_dmac_desc,
1272 chan->desc.running = NULL;
1275 if (chan->desc.running)
1276 rcar_dmac_chan_start_xfer(chan);
1281 static irqreturn_t rcar_dmac_isr_channel(int irq, void *dev)
1283 u32 mask = RCAR_DMACHCR_DSE | RCAR_DMACHCR_TE;
1284 struct rcar_dmac_chan *chan = dev;
1285 irqreturn_t ret = IRQ_NONE;
1288 spin_lock(&chan->lock);
1290 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
1291 if (chcr & RCAR_DMACHCR_TE)
1292 mask |= RCAR_DMACHCR_DE;
1293 rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr & ~mask);
1295 if (chcr & RCAR_DMACHCR_DSE)
1296 ret |= rcar_dmac_isr_desc_stage_end(chan);
1298 if (chcr & RCAR_DMACHCR_TE)
1299 ret |= rcar_dmac_isr_transfer_end(chan);
1301 spin_unlock(&chan->lock);
1306 static irqreturn_t rcar_dmac_isr_channel_thread(int irq, void *dev)
1308 struct rcar_dmac_chan *chan = dev;
1309 struct rcar_dmac_desc *desc;
1311 spin_lock_irq(&chan->lock);
1313 /* For cyclic transfers notify the user after every chunk. */
1314 if (chan->desc.running && chan->desc.running->cyclic) {
1315 dma_async_tx_callback callback;
1316 void *callback_param;
1318 desc = chan->desc.running;
1319 callback = desc->async_tx.callback;
1320 callback_param = desc->async_tx.callback_param;
1323 spin_unlock_irq(&chan->lock);
1324 callback(callback_param);
1325 spin_lock_irq(&chan->lock);
1330 * Call the callback function for all descriptors on the done list and
1331 * move them to the ack wait list.
1333 while (!list_empty(&chan->desc.done)) {
1334 desc = list_first_entry(&chan->desc.done, struct rcar_dmac_desc,
1336 dma_cookie_complete(&desc->async_tx);
1337 list_del(&desc->node);
1339 if (desc->async_tx.callback) {
1340 spin_unlock_irq(&chan->lock);
1342 * We own the only reference to this descriptor, we can
1343 * safely dereference it without holding the channel
1346 desc->async_tx.callback(desc->async_tx.callback_param);
1347 spin_lock_irq(&chan->lock);
1350 list_add_tail(&desc->node, &chan->desc.wait);
1353 spin_unlock_irq(&chan->lock);
1355 /* Recycle all acked descriptors. */
1356 rcar_dmac_desc_recycle_acked(chan);
1361 static irqreturn_t rcar_dmac_isr_error(int irq, void *data)
1363 struct rcar_dmac *dmac = data;
1365 if (!(rcar_dmac_read(dmac, RCAR_DMAOR) & RCAR_DMAOR_AE))
1369 * An unrecoverable error occurred on an unknown channel. Halt the DMAC,
1370 * abort transfers on all channels, and reinitialize the DMAC.
1372 rcar_dmac_stop(dmac);
1373 rcar_dmac_abort(dmac);
1374 rcar_dmac_init(dmac);
1379 /* -----------------------------------------------------------------------------
1380 * OF xlate and channel filter
1383 static bool rcar_dmac_chan_filter(struct dma_chan *chan, void *arg)
1385 struct rcar_dmac *dmac = to_rcar_dmac(chan->device);
1386 struct of_phandle_args *dma_spec = arg;
1389 * FIXME: Using a filter on OF platforms is a nonsense. The OF xlate
1390 * function knows from which device it wants to allocate a channel from,
1391 * and would be perfectly capable of selecting the channel it wants.
1392 * Forcing it to call dma_request_channel() and iterate through all
1393 * channels from all controllers is just pointless.
1395 if (chan->device->device_config != rcar_dmac_device_config ||
1396 dma_spec->np != chan->device->dev->of_node)
1399 return !test_and_set_bit(dma_spec->args[0], dmac->modules);
1402 static struct dma_chan *rcar_dmac_of_xlate(struct of_phandle_args *dma_spec,
1403 struct of_dma *ofdma)
1405 struct rcar_dmac_chan *rchan;
1406 struct dma_chan *chan;
1407 dma_cap_mask_t mask;
1409 if (dma_spec->args_count != 1)
1412 /* Only slave DMA channels can be allocated via DT */
1414 dma_cap_set(DMA_SLAVE, mask);
1416 chan = dma_request_channel(mask, rcar_dmac_chan_filter, dma_spec);
1420 rchan = to_rcar_dmac_chan(chan);
1421 rchan->mid_rid = dma_spec->args[0];
1426 /* -----------------------------------------------------------------------------
1430 #ifdef CONFIG_PM_SLEEP
1431 static int rcar_dmac_sleep_suspend(struct device *dev)
1434 * TODO: Wait for the current transfer to complete and stop the device.
1439 static int rcar_dmac_sleep_resume(struct device *dev)
1441 /* TODO: Resume transfers, if any. */
1447 static int rcar_dmac_runtime_suspend(struct device *dev)
1452 static int rcar_dmac_runtime_resume(struct device *dev)
1454 struct rcar_dmac *dmac = dev_get_drvdata(dev);
1456 return rcar_dmac_init(dmac);
1460 static const struct dev_pm_ops rcar_dmac_pm = {
1461 SET_SYSTEM_SLEEP_PM_OPS(rcar_dmac_sleep_suspend, rcar_dmac_sleep_resume)
1462 SET_RUNTIME_PM_OPS(rcar_dmac_runtime_suspend, rcar_dmac_runtime_resume,
1466 /* -----------------------------------------------------------------------------
1470 static int rcar_dmac_chan_probe(struct rcar_dmac *dmac,
1471 struct rcar_dmac_chan *rchan,
1474 struct platform_device *pdev = to_platform_device(dmac->dev);
1475 struct dma_chan *chan = &rchan->chan;
1476 char pdev_irqname[5];
1481 rchan->index = index;
1482 rchan->iomem = dmac->iomem + RCAR_DMAC_CHAN_OFFSET(index);
1483 rchan->mid_rid = -EINVAL;
1485 spin_lock_init(&rchan->lock);
1487 /* Request the channel interrupt. */
1488 sprintf(pdev_irqname, "ch%u", index);
1489 irq = platform_get_irq_byname(pdev, pdev_irqname);
1491 dev_err(dmac->dev, "no IRQ specified for channel %u\n", index);
1495 irqname = devm_kasprintf(dmac->dev, GFP_KERNEL, "%s:%u",
1496 dev_name(dmac->dev), index);
1500 ret = devm_request_threaded_irq(dmac->dev, irq, rcar_dmac_isr_channel,
1501 rcar_dmac_isr_channel_thread, 0,
1504 dev_err(dmac->dev, "failed to request IRQ %u (%d)\n", irq, ret);
1509 * Initialize the DMA engine channel and add it to the DMA engine
1512 chan->device = &dmac->engine;
1513 dma_cookie_init(chan);
1515 list_add_tail(&chan->device_node, &dmac->engine.channels);
1520 static int rcar_dmac_parse_of(struct device *dev, struct rcar_dmac *dmac)
1522 struct device_node *np = dev->of_node;
1525 ret = of_property_read_u32(np, "dma-channels", &dmac->n_channels);
1527 dev_err(dev, "unable to read dma-channels property\n");
1531 if (dmac->n_channels <= 0 || dmac->n_channels >= 100) {
1532 dev_err(dev, "invalid number of channels %u\n",
1540 static int rcar_dmac_probe(struct platform_device *pdev)
1542 const enum dma_slave_buswidth widths = DMA_SLAVE_BUSWIDTH_1_BYTE |
1543 DMA_SLAVE_BUSWIDTH_2_BYTES | DMA_SLAVE_BUSWIDTH_4_BYTES |
1544 DMA_SLAVE_BUSWIDTH_8_BYTES | DMA_SLAVE_BUSWIDTH_16_BYTES |
1545 DMA_SLAVE_BUSWIDTH_32_BYTES | DMA_SLAVE_BUSWIDTH_64_BYTES;
1546 struct dma_device *engine;
1547 struct rcar_dmac *dmac;
1548 struct resource *mem;
1554 dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL);
1558 dmac->dev = &pdev->dev;
1559 platform_set_drvdata(pdev, dmac);
1561 ret = rcar_dmac_parse_of(&pdev->dev, dmac);
1565 dmac->channels = devm_kcalloc(&pdev->dev, dmac->n_channels,
1566 sizeof(*dmac->channels), GFP_KERNEL);
1567 if (!dmac->channels)
1570 /* Request resources. */
1571 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1572 dmac->iomem = devm_ioremap_resource(&pdev->dev, mem);
1573 if (IS_ERR(dmac->iomem))
1574 return PTR_ERR(dmac->iomem);
1576 irq = platform_get_irq_byname(pdev, "error");
1578 dev_err(&pdev->dev, "no error IRQ specified\n");
1582 irqname = devm_kasprintf(dmac->dev, GFP_KERNEL, "%s:error",
1583 dev_name(dmac->dev));
1587 ret = devm_request_irq(&pdev->dev, irq, rcar_dmac_isr_error, 0,
1590 dev_err(&pdev->dev, "failed to request IRQ %u (%d)\n",
1595 /* Enable runtime PM and initialize the device. */
1596 pm_runtime_enable(&pdev->dev);
1597 ret = pm_runtime_get_sync(&pdev->dev);
1599 dev_err(&pdev->dev, "runtime PM get sync failed (%d)\n", ret);
1603 ret = rcar_dmac_init(dmac);
1604 pm_runtime_put(&pdev->dev);
1607 dev_err(&pdev->dev, "failed to reset device\n");
1611 /* Initialize the channels. */
1612 INIT_LIST_HEAD(&dmac->engine.channels);
1614 for (i = 0; i < dmac->n_channels; ++i) {
1615 ret = rcar_dmac_chan_probe(dmac, &dmac->channels[i], i);
1620 /* Register the DMAC as a DMA provider for DT. */
1621 ret = of_dma_controller_register(pdev->dev.of_node, rcar_dmac_of_xlate,
1627 * Register the DMA engine device.
1629 * Default transfer size of 32 bytes requires 32-byte alignment.
1631 engine = &dmac->engine;
1632 dma_cap_set(DMA_MEMCPY, engine->cap_mask);
1633 dma_cap_set(DMA_SLAVE, engine->cap_mask);
1635 engine->dev = &pdev->dev;
1636 engine->copy_align = ilog2(RCAR_DMAC_MEMCPY_XFER_SIZE);
1638 engine->src_addr_widths = widths;
1639 engine->dst_addr_widths = widths;
1640 engine->directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
1641 engine->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1643 engine->device_alloc_chan_resources = rcar_dmac_alloc_chan_resources;
1644 engine->device_free_chan_resources = rcar_dmac_free_chan_resources;
1645 engine->device_prep_dma_memcpy = rcar_dmac_prep_dma_memcpy;
1646 engine->device_prep_slave_sg = rcar_dmac_prep_slave_sg;
1647 engine->device_prep_dma_cyclic = rcar_dmac_prep_dma_cyclic;
1648 engine->device_config = rcar_dmac_device_config;
1649 engine->device_terminate_all = rcar_dmac_chan_terminate_all;
1650 engine->device_tx_status = rcar_dmac_tx_status;
1651 engine->device_issue_pending = rcar_dmac_issue_pending;
1653 ret = dma_async_device_register(engine);
1660 of_dma_controller_free(pdev->dev.of_node);
1661 pm_runtime_disable(&pdev->dev);
1665 static int rcar_dmac_remove(struct platform_device *pdev)
1667 struct rcar_dmac *dmac = platform_get_drvdata(pdev);
1669 of_dma_controller_free(pdev->dev.of_node);
1670 dma_async_device_unregister(&dmac->engine);
1672 pm_runtime_disable(&pdev->dev);
1677 static void rcar_dmac_shutdown(struct platform_device *pdev)
1679 struct rcar_dmac *dmac = platform_get_drvdata(pdev);
1681 rcar_dmac_stop(dmac);
1684 static const struct of_device_id rcar_dmac_of_ids[] = {
1685 { .compatible = "renesas,rcar-dmac", },
1688 MODULE_DEVICE_TABLE(of, rcar_dmac_of_ids);
1690 static struct platform_driver rcar_dmac_driver = {
1692 .pm = &rcar_dmac_pm,
1693 .name = "rcar-dmac",
1694 .of_match_table = rcar_dmac_of_ids,
1696 .probe = rcar_dmac_probe,
1697 .remove = rcar_dmac_remove,
1698 .shutdown = rcar_dmac_shutdown,
1701 module_platform_driver(rcar_dmac_driver);
1703 MODULE_DESCRIPTION("R-Car Gen2 DMA Controller Driver");
1704 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1705 MODULE_LICENSE("GPL v2");