Linux 4.18.10
[linux/fpc-iii.git] / drivers / dma / sh / rcar-dmac.c
blob8305a1ce8a9b2e229e5d3ea134d251b0009b0883
1 /*
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/delay.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/dmaengine.h>
16 #include <linux/interrupt.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/of.h>
21 #include <linux/of_dma.h>
22 #include <linux/of_platform.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
28 #include "../dmaengine.h"
31 * struct rcar_dmac_xfer_chunk - Descriptor for a hardware transfer
32 * @node: entry in the parent's chunks list
33 * @src_addr: device source address
34 * @dst_addr: device destination address
35 * @size: transfer size in bytes
37 struct rcar_dmac_xfer_chunk {
38 struct list_head node;
40 dma_addr_t src_addr;
41 dma_addr_t dst_addr;
42 u32 size;
46 * struct rcar_dmac_hw_desc - Hardware descriptor for a transfer chunk
47 * @sar: value of the SAR register (source address)
48 * @dar: value of the DAR register (destination address)
49 * @tcr: value of the TCR register (transfer count)
51 struct rcar_dmac_hw_desc {
52 u32 sar;
53 u32 dar;
54 u32 tcr;
55 u32 reserved;
56 } __attribute__((__packed__));
59 * struct rcar_dmac_desc - R-Car Gen2 DMA Transfer Descriptor
60 * @async_tx: base DMA asynchronous transaction descriptor
61 * @direction: direction of the DMA transfer
62 * @xfer_shift: log2 of the transfer size
63 * @chcr: value of the channel configuration register for this transfer
64 * @node: entry in the channel's descriptors lists
65 * @chunks: list of transfer chunks for this transfer
66 * @running: the transfer chunk being currently processed
67 * @nchunks: number of transfer chunks for this transfer
68 * @hwdescs.use: whether the transfer descriptor uses hardware descriptors
69 * @hwdescs.mem: hardware descriptors memory for the transfer
70 * @hwdescs.dma: device address of the hardware descriptors memory
71 * @hwdescs.size: size of the hardware descriptors in bytes
72 * @size: transfer size in bytes
73 * @cyclic: when set indicates that the DMA transfer is cyclic
75 struct rcar_dmac_desc {
76 struct dma_async_tx_descriptor async_tx;
77 enum dma_transfer_direction direction;
78 unsigned int xfer_shift;
79 u32 chcr;
81 struct list_head node;
82 struct list_head chunks;
83 struct rcar_dmac_xfer_chunk *running;
84 unsigned int nchunks;
86 struct {
87 bool use;
88 struct rcar_dmac_hw_desc *mem;
89 dma_addr_t dma;
90 size_t size;
91 } hwdescs;
93 unsigned int size;
94 bool cyclic;
97 #define to_rcar_dmac_desc(d) container_of(d, struct rcar_dmac_desc, async_tx)
100 * struct rcar_dmac_desc_page - One page worth of descriptors
101 * @node: entry in the channel's pages list
102 * @descs: array of DMA descriptors
103 * @chunks: array of transfer chunk descriptors
105 struct rcar_dmac_desc_page {
106 struct list_head node;
108 union {
109 struct rcar_dmac_desc descs[0];
110 struct rcar_dmac_xfer_chunk chunks[0];
114 #define RCAR_DMAC_DESCS_PER_PAGE \
115 ((PAGE_SIZE - offsetof(struct rcar_dmac_desc_page, descs)) / \
116 sizeof(struct rcar_dmac_desc))
117 #define RCAR_DMAC_XFER_CHUNKS_PER_PAGE \
118 ((PAGE_SIZE - offsetof(struct rcar_dmac_desc_page, chunks)) / \
119 sizeof(struct rcar_dmac_xfer_chunk))
122 * struct rcar_dmac_chan_slave - Slave configuration
123 * @slave_addr: slave memory address
124 * @xfer_size: size (in bytes) of hardware transfers
126 struct rcar_dmac_chan_slave {
127 phys_addr_t slave_addr;
128 unsigned int xfer_size;
132 * struct rcar_dmac_chan_map - Map of slave device phys to dma address
133 * @addr: slave dma address
134 * @dir: direction of mapping
135 * @slave: slave configuration that is mapped
137 struct rcar_dmac_chan_map {
138 dma_addr_t addr;
139 enum dma_data_direction dir;
140 struct rcar_dmac_chan_slave slave;
144 * struct rcar_dmac_chan - R-Car Gen2 DMA Controller Channel
145 * @chan: base DMA channel object
146 * @iomem: channel I/O memory base
147 * @index: index of this channel in the controller
148 * @irq: channel IRQ
149 * @src: slave memory address and size on the source side
150 * @dst: slave memory address and size on the destination side
151 * @mid_rid: hardware MID/RID for the DMA client using this channel
152 * @lock: protects the channel CHCR register and the desc members
153 * @desc.free: list of free descriptors
154 * @desc.pending: list of pending descriptors (submitted with tx_submit)
155 * @desc.active: list of active descriptors (activated with issue_pending)
156 * @desc.done: list of completed descriptors
157 * @desc.wait: list of descriptors waiting for an ack
158 * @desc.running: the descriptor being processed (a member of the active list)
159 * @desc.chunks_free: list of free transfer chunk descriptors
160 * @desc.pages: list of pages used by allocated descriptors
162 struct rcar_dmac_chan {
163 struct dma_chan chan;
164 void __iomem *iomem;
165 unsigned int index;
166 int irq;
168 struct rcar_dmac_chan_slave src;
169 struct rcar_dmac_chan_slave dst;
170 struct rcar_dmac_chan_map map;
171 int mid_rid;
173 spinlock_t lock;
175 struct {
176 struct list_head free;
177 struct list_head pending;
178 struct list_head active;
179 struct list_head done;
180 struct list_head wait;
181 struct rcar_dmac_desc *running;
183 struct list_head chunks_free;
185 struct list_head pages;
186 } desc;
189 #define to_rcar_dmac_chan(c) container_of(c, struct rcar_dmac_chan, chan)
192 * struct rcar_dmac - R-Car Gen2 DMA Controller
193 * @engine: base DMA engine object
194 * @dev: the hardware device
195 * @iomem: remapped I/O memory base
196 * @n_channels: number of available channels
197 * @channels: array of DMAC channels
198 * @modules: bitmask of client modules in use
200 struct rcar_dmac {
201 struct dma_device engine;
202 struct device *dev;
203 void __iomem *iomem;
205 unsigned int n_channels;
206 struct rcar_dmac_chan *channels;
208 DECLARE_BITMAP(modules, 256);
211 #define to_rcar_dmac(d) container_of(d, struct rcar_dmac, engine)
213 /* -----------------------------------------------------------------------------
214 * Registers
217 #define RCAR_DMAC_CHAN_OFFSET(i) (0x8000 + 0x80 * (i))
219 #define RCAR_DMAISTA 0x0020
220 #define RCAR_DMASEC 0x0030
221 #define RCAR_DMAOR 0x0060
222 #define RCAR_DMAOR_PRI_FIXED (0 << 8)
223 #define RCAR_DMAOR_PRI_ROUND_ROBIN (3 << 8)
224 #define RCAR_DMAOR_AE (1 << 2)
225 #define RCAR_DMAOR_DME (1 << 0)
226 #define RCAR_DMACHCLR 0x0080
227 #define RCAR_DMADPSEC 0x00a0
229 #define RCAR_DMASAR 0x0000
230 #define RCAR_DMADAR 0x0004
231 #define RCAR_DMATCR 0x0008
232 #define RCAR_DMATCR_MASK 0x00ffffff
233 #define RCAR_DMATSR 0x0028
234 #define RCAR_DMACHCR 0x000c
235 #define RCAR_DMACHCR_CAE (1 << 31)
236 #define RCAR_DMACHCR_CAIE (1 << 30)
237 #define RCAR_DMACHCR_DPM_DISABLED (0 << 28)
238 #define RCAR_DMACHCR_DPM_ENABLED (1 << 28)
239 #define RCAR_DMACHCR_DPM_REPEAT (2 << 28)
240 #define RCAR_DMACHCR_DPM_INFINITE (3 << 28)
241 #define RCAR_DMACHCR_RPT_SAR (1 << 27)
242 #define RCAR_DMACHCR_RPT_DAR (1 << 26)
243 #define RCAR_DMACHCR_RPT_TCR (1 << 25)
244 #define RCAR_DMACHCR_DPB (1 << 22)
245 #define RCAR_DMACHCR_DSE (1 << 19)
246 #define RCAR_DMACHCR_DSIE (1 << 18)
247 #define RCAR_DMACHCR_TS_1B ((0 << 20) | (0 << 3))
248 #define RCAR_DMACHCR_TS_2B ((0 << 20) | (1 << 3))
249 #define RCAR_DMACHCR_TS_4B ((0 << 20) | (2 << 3))
250 #define RCAR_DMACHCR_TS_16B ((0 << 20) | (3 << 3))
251 #define RCAR_DMACHCR_TS_32B ((1 << 20) | (0 << 3))
252 #define RCAR_DMACHCR_TS_64B ((1 << 20) | (1 << 3))
253 #define RCAR_DMACHCR_TS_8B ((1 << 20) | (3 << 3))
254 #define RCAR_DMACHCR_DM_FIXED (0 << 14)
255 #define RCAR_DMACHCR_DM_INC (1 << 14)
256 #define RCAR_DMACHCR_DM_DEC (2 << 14)
257 #define RCAR_DMACHCR_SM_FIXED (0 << 12)
258 #define RCAR_DMACHCR_SM_INC (1 << 12)
259 #define RCAR_DMACHCR_SM_DEC (2 << 12)
260 #define RCAR_DMACHCR_RS_AUTO (4 << 8)
261 #define RCAR_DMACHCR_RS_DMARS (8 << 8)
262 #define RCAR_DMACHCR_IE (1 << 2)
263 #define RCAR_DMACHCR_TE (1 << 1)
264 #define RCAR_DMACHCR_DE (1 << 0)
265 #define RCAR_DMATCRB 0x0018
266 #define RCAR_DMATSRB 0x0038
267 #define RCAR_DMACHCRB 0x001c
268 #define RCAR_DMACHCRB_DCNT(n) ((n) << 24)
269 #define RCAR_DMACHCRB_DPTR_MASK (0xff << 16)
270 #define RCAR_DMACHCRB_DPTR_SHIFT 16
271 #define RCAR_DMACHCRB_DRST (1 << 15)
272 #define RCAR_DMACHCRB_DTS (1 << 8)
273 #define RCAR_DMACHCRB_SLM_NORMAL (0 << 4)
274 #define RCAR_DMACHCRB_SLM_CLK(n) ((8 | (n)) << 4)
275 #define RCAR_DMACHCRB_PRI(n) ((n) << 0)
276 #define RCAR_DMARS 0x0040
277 #define RCAR_DMABUFCR 0x0048
278 #define RCAR_DMABUFCR_MBU(n) ((n) << 16)
279 #define RCAR_DMABUFCR_ULB(n) ((n) << 0)
280 #define RCAR_DMADPBASE 0x0050
281 #define RCAR_DMADPBASE_MASK 0xfffffff0
282 #define RCAR_DMADPBASE_SEL (1 << 0)
283 #define RCAR_DMADPCR 0x0054
284 #define RCAR_DMADPCR_DIPT(n) ((n) << 24)
285 #define RCAR_DMAFIXSAR 0x0010
286 #define RCAR_DMAFIXDAR 0x0014
287 #define RCAR_DMAFIXDPBASE 0x0060
289 /* Hardcode the MEMCPY transfer size to 4 bytes. */
290 #define RCAR_DMAC_MEMCPY_XFER_SIZE 4
292 /* -----------------------------------------------------------------------------
293 * Device access
296 static void rcar_dmac_write(struct rcar_dmac *dmac, u32 reg, u32 data)
298 if (reg == RCAR_DMAOR)
299 writew(data, dmac->iomem + reg);
300 else
301 writel(data, dmac->iomem + reg);
304 static u32 rcar_dmac_read(struct rcar_dmac *dmac, u32 reg)
306 if (reg == RCAR_DMAOR)
307 return readw(dmac->iomem + reg);
308 else
309 return readl(dmac->iomem + reg);
312 static u32 rcar_dmac_chan_read(struct rcar_dmac_chan *chan, u32 reg)
314 if (reg == RCAR_DMARS)
315 return readw(chan->iomem + reg);
316 else
317 return readl(chan->iomem + reg);
320 static void rcar_dmac_chan_write(struct rcar_dmac_chan *chan, u32 reg, u32 data)
322 if (reg == RCAR_DMARS)
323 writew(data, chan->iomem + reg);
324 else
325 writel(data, chan->iomem + reg);
328 /* -----------------------------------------------------------------------------
329 * Initialization and configuration
332 static bool rcar_dmac_chan_is_busy(struct rcar_dmac_chan *chan)
334 u32 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
336 return !!(chcr & (RCAR_DMACHCR_DE | RCAR_DMACHCR_TE));
339 static void rcar_dmac_chan_start_xfer(struct rcar_dmac_chan *chan)
341 struct rcar_dmac_desc *desc = chan->desc.running;
342 u32 chcr = desc->chcr;
344 WARN_ON_ONCE(rcar_dmac_chan_is_busy(chan));
346 if (chan->mid_rid >= 0)
347 rcar_dmac_chan_write(chan, RCAR_DMARS, chan->mid_rid);
349 if (desc->hwdescs.use) {
350 struct rcar_dmac_xfer_chunk *chunk =
351 list_first_entry(&desc->chunks,
352 struct rcar_dmac_xfer_chunk, node);
354 dev_dbg(chan->chan.device->dev,
355 "chan%u: queue desc %p: %u@%pad\n",
356 chan->index, desc, desc->nchunks, &desc->hwdescs.dma);
358 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
359 rcar_dmac_chan_write(chan, RCAR_DMAFIXSAR,
360 chunk->src_addr >> 32);
361 rcar_dmac_chan_write(chan, RCAR_DMAFIXDAR,
362 chunk->dst_addr >> 32);
363 rcar_dmac_chan_write(chan, RCAR_DMAFIXDPBASE,
364 desc->hwdescs.dma >> 32);
365 #endif
366 rcar_dmac_chan_write(chan, RCAR_DMADPBASE,
367 (desc->hwdescs.dma & 0xfffffff0) |
368 RCAR_DMADPBASE_SEL);
369 rcar_dmac_chan_write(chan, RCAR_DMACHCRB,
370 RCAR_DMACHCRB_DCNT(desc->nchunks - 1) |
371 RCAR_DMACHCRB_DRST);
374 * Errata: When descriptor memory is accessed through an IOMMU
375 * the DMADAR register isn't initialized automatically from the
376 * first descriptor at beginning of transfer by the DMAC like it
377 * should. Initialize it manually with the destination address
378 * of the first chunk.
380 rcar_dmac_chan_write(chan, RCAR_DMADAR,
381 chunk->dst_addr & 0xffffffff);
384 * Program the descriptor stage interrupt to occur after the end
385 * of the first stage.
387 rcar_dmac_chan_write(chan, RCAR_DMADPCR, RCAR_DMADPCR_DIPT(1));
389 chcr |= RCAR_DMACHCR_RPT_SAR | RCAR_DMACHCR_RPT_DAR
390 | RCAR_DMACHCR_RPT_TCR | RCAR_DMACHCR_DPB;
393 * If the descriptor isn't cyclic enable normal descriptor mode
394 * and the transfer completion interrupt.
396 if (!desc->cyclic)
397 chcr |= RCAR_DMACHCR_DPM_ENABLED | RCAR_DMACHCR_IE;
399 * If the descriptor is cyclic and has a callback enable the
400 * descriptor stage interrupt in infinite repeat mode.
402 else if (desc->async_tx.callback)
403 chcr |= RCAR_DMACHCR_DPM_INFINITE | RCAR_DMACHCR_DSIE;
405 * Otherwise just select infinite repeat mode without any
406 * interrupt.
408 else
409 chcr |= RCAR_DMACHCR_DPM_INFINITE;
410 } else {
411 struct rcar_dmac_xfer_chunk *chunk = desc->running;
413 dev_dbg(chan->chan.device->dev,
414 "chan%u: queue chunk %p: %u@%pad -> %pad\n",
415 chan->index, chunk, chunk->size, &chunk->src_addr,
416 &chunk->dst_addr);
418 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
419 rcar_dmac_chan_write(chan, RCAR_DMAFIXSAR,
420 chunk->src_addr >> 32);
421 rcar_dmac_chan_write(chan, RCAR_DMAFIXDAR,
422 chunk->dst_addr >> 32);
423 #endif
424 rcar_dmac_chan_write(chan, RCAR_DMASAR,
425 chunk->src_addr & 0xffffffff);
426 rcar_dmac_chan_write(chan, RCAR_DMADAR,
427 chunk->dst_addr & 0xffffffff);
428 rcar_dmac_chan_write(chan, RCAR_DMATCR,
429 chunk->size >> desc->xfer_shift);
431 chcr |= RCAR_DMACHCR_DPM_DISABLED | RCAR_DMACHCR_IE;
434 rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr | RCAR_DMACHCR_DE);
437 static int rcar_dmac_init(struct rcar_dmac *dmac)
439 u16 dmaor;
441 /* Clear all channels and enable the DMAC globally. */
442 rcar_dmac_write(dmac, RCAR_DMACHCLR, GENMASK(dmac->n_channels - 1, 0));
443 rcar_dmac_write(dmac, RCAR_DMAOR,
444 RCAR_DMAOR_PRI_FIXED | RCAR_DMAOR_DME);
446 dmaor = rcar_dmac_read(dmac, RCAR_DMAOR);
447 if ((dmaor & (RCAR_DMAOR_AE | RCAR_DMAOR_DME)) != RCAR_DMAOR_DME) {
448 dev_warn(dmac->dev, "DMAOR initialization failed.\n");
449 return -EIO;
452 return 0;
455 /* -----------------------------------------------------------------------------
456 * Descriptors submission
459 static dma_cookie_t rcar_dmac_tx_submit(struct dma_async_tx_descriptor *tx)
461 struct rcar_dmac_chan *chan = to_rcar_dmac_chan(tx->chan);
462 struct rcar_dmac_desc *desc = to_rcar_dmac_desc(tx);
463 unsigned long flags;
464 dma_cookie_t cookie;
466 spin_lock_irqsave(&chan->lock, flags);
468 cookie = dma_cookie_assign(tx);
470 dev_dbg(chan->chan.device->dev, "chan%u: submit #%d@%p\n",
471 chan->index, tx->cookie, desc);
473 list_add_tail(&desc->node, &chan->desc.pending);
474 desc->running = list_first_entry(&desc->chunks,
475 struct rcar_dmac_xfer_chunk, node);
477 spin_unlock_irqrestore(&chan->lock, flags);
479 return cookie;
482 /* -----------------------------------------------------------------------------
483 * Descriptors allocation and free
487 * rcar_dmac_desc_alloc - Allocate a page worth of DMA descriptors
488 * @chan: the DMA channel
489 * @gfp: allocation flags
491 static int rcar_dmac_desc_alloc(struct rcar_dmac_chan *chan, gfp_t gfp)
493 struct rcar_dmac_desc_page *page;
494 unsigned long flags;
495 LIST_HEAD(list);
496 unsigned int i;
498 page = (void *)get_zeroed_page(gfp);
499 if (!page)
500 return -ENOMEM;
502 for (i = 0; i < RCAR_DMAC_DESCS_PER_PAGE; ++i) {
503 struct rcar_dmac_desc *desc = &page->descs[i];
505 dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan);
506 desc->async_tx.tx_submit = rcar_dmac_tx_submit;
507 INIT_LIST_HEAD(&desc->chunks);
509 list_add_tail(&desc->node, &list);
512 spin_lock_irqsave(&chan->lock, flags);
513 list_splice_tail(&list, &chan->desc.free);
514 list_add_tail(&page->node, &chan->desc.pages);
515 spin_unlock_irqrestore(&chan->lock, flags);
517 return 0;
521 * rcar_dmac_desc_put - Release a DMA transfer descriptor
522 * @chan: the DMA channel
523 * @desc: the descriptor
525 * Put the descriptor and its transfer chunk descriptors back in the channel's
526 * free descriptors lists. The descriptor's chunks list will be reinitialized to
527 * an empty list as a result.
529 * The descriptor must have been removed from the channel's lists before calling
530 * this function.
532 static void rcar_dmac_desc_put(struct rcar_dmac_chan *chan,
533 struct rcar_dmac_desc *desc)
535 unsigned long flags;
537 spin_lock_irqsave(&chan->lock, flags);
538 list_splice_tail_init(&desc->chunks, &chan->desc.chunks_free);
539 list_add(&desc->node, &chan->desc.free);
540 spin_unlock_irqrestore(&chan->lock, flags);
543 static void rcar_dmac_desc_recycle_acked(struct rcar_dmac_chan *chan)
545 struct rcar_dmac_desc *desc, *_desc;
546 unsigned long flags;
547 LIST_HEAD(list);
550 * We have to temporarily move all descriptors from the wait list to a
551 * local list as iterating over the wait list, even with
552 * list_for_each_entry_safe, isn't safe if we release the channel lock
553 * around the rcar_dmac_desc_put() call.
555 spin_lock_irqsave(&chan->lock, flags);
556 list_splice_init(&chan->desc.wait, &list);
557 spin_unlock_irqrestore(&chan->lock, flags);
559 list_for_each_entry_safe(desc, _desc, &list, node) {
560 if (async_tx_test_ack(&desc->async_tx)) {
561 list_del(&desc->node);
562 rcar_dmac_desc_put(chan, desc);
566 if (list_empty(&list))
567 return;
569 /* Put the remaining descriptors back in the wait list. */
570 spin_lock_irqsave(&chan->lock, flags);
571 list_splice(&list, &chan->desc.wait);
572 spin_unlock_irqrestore(&chan->lock, flags);
576 * rcar_dmac_desc_get - Allocate a descriptor for a DMA transfer
577 * @chan: the DMA channel
579 * Locking: This function must be called in a non-atomic context.
581 * Return: A pointer to the allocated descriptor or NULL if no descriptor can
582 * be allocated.
584 static struct rcar_dmac_desc *rcar_dmac_desc_get(struct rcar_dmac_chan *chan)
586 struct rcar_dmac_desc *desc;
587 unsigned long flags;
588 int ret;
590 /* Recycle acked descriptors before attempting allocation. */
591 rcar_dmac_desc_recycle_acked(chan);
593 spin_lock_irqsave(&chan->lock, flags);
595 while (list_empty(&chan->desc.free)) {
597 * No free descriptors, allocate a page worth of them and try
598 * again, as someone else could race us to get the newly
599 * allocated descriptors. If the allocation fails return an
600 * error.
602 spin_unlock_irqrestore(&chan->lock, flags);
603 ret = rcar_dmac_desc_alloc(chan, GFP_NOWAIT);
604 if (ret < 0)
605 return NULL;
606 spin_lock_irqsave(&chan->lock, flags);
609 desc = list_first_entry(&chan->desc.free, struct rcar_dmac_desc, node);
610 list_del(&desc->node);
612 spin_unlock_irqrestore(&chan->lock, flags);
614 return desc;
618 * rcar_dmac_xfer_chunk_alloc - Allocate a page worth of transfer chunks
619 * @chan: the DMA channel
620 * @gfp: allocation flags
622 static int rcar_dmac_xfer_chunk_alloc(struct rcar_dmac_chan *chan, gfp_t gfp)
624 struct rcar_dmac_desc_page *page;
625 unsigned long flags;
626 LIST_HEAD(list);
627 unsigned int i;
629 page = (void *)get_zeroed_page(gfp);
630 if (!page)
631 return -ENOMEM;
633 for (i = 0; i < RCAR_DMAC_XFER_CHUNKS_PER_PAGE; ++i) {
634 struct rcar_dmac_xfer_chunk *chunk = &page->chunks[i];
636 list_add_tail(&chunk->node, &list);
639 spin_lock_irqsave(&chan->lock, flags);
640 list_splice_tail(&list, &chan->desc.chunks_free);
641 list_add_tail(&page->node, &chan->desc.pages);
642 spin_unlock_irqrestore(&chan->lock, flags);
644 return 0;
648 * rcar_dmac_xfer_chunk_get - Allocate a transfer chunk for a DMA transfer
649 * @chan: the DMA channel
651 * Locking: This function must be called in a non-atomic context.
653 * Return: A pointer to the allocated transfer chunk descriptor or NULL if no
654 * descriptor can be allocated.
656 static struct rcar_dmac_xfer_chunk *
657 rcar_dmac_xfer_chunk_get(struct rcar_dmac_chan *chan)
659 struct rcar_dmac_xfer_chunk *chunk;
660 unsigned long flags;
661 int ret;
663 spin_lock_irqsave(&chan->lock, flags);
665 while (list_empty(&chan->desc.chunks_free)) {
667 * No free descriptors, allocate a page worth of them and try
668 * again, as someone else could race us to get the newly
669 * allocated descriptors. If the allocation fails return an
670 * error.
672 spin_unlock_irqrestore(&chan->lock, flags);
673 ret = rcar_dmac_xfer_chunk_alloc(chan, GFP_NOWAIT);
674 if (ret < 0)
675 return NULL;
676 spin_lock_irqsave(&chan->lock, flags);
679 chunk = list_first_entry(&chan->desc.chunks_free,
680 struct rcar_dmac_xfer_chunk, node);
681 list_del(&chunk->node);
683 spin_unlock_irqrestore(&chan->lock, flags);
685 return chunk;
688 static void rcar_dmac_realloc_hwdesc(struct rcar_dmac_chan *chan,
689 struct rcar_dmac_desc *desc, size_t size)
692 * dma_alloc_coherent() allocates memory in page size increments. To
693 * avoid reallocating the hardware descriptors when the allocated size
694 * wouldn't change align the requested size to a multiple of the page
695 * size.
697 size = PAGE_ALIGN(size);
699 if (desc->hwdescs.size == size)
700 return;
702 if (desc->hwdescs.mem) {
703 dma_free_coherent(chan->chan.device->dev, desc->hwdescs.size,
704 desc->hwdescs.mem, desc->hwdescs.dma);
705 desc->hwdescs.mem = NULL;
706 desc->hwdescs.size = 0;
709 if (!size)
710 return;
712 desc->hwdescs.mem = dma_alloc_coherent(chan->chan.device->dev, size,
713 &desc->hwdescs.dma, GFP_NOWAIT);
714 if (!desc->hwdescs.mem)
715 return;
717 desc->hwdescs.size = size;
720 static int rcar_dmac_fill_hwdesc(struct rcar_dmac_chan *chan,
721 struct rcar_dmac_desc *desc)
723 struct rcar_dmac_xfer_chunk *chunk;
724 struct rcar_dmac_hw_desc *hwdesc;
726 rcar_dmac_realloc_hwdesc(chan, desc, desc->nchunks * sizeof(*hwdesc));
728 hwdesc = desc->hwdescs.mem;
729 if (!hwdesc)
730 return -ENOMEM;
732 list_for_each_entry(chunk, &desc->chunks, node) {
733 hwdesc->sar = chunk->src_addr;
734 hwdesc->dar = chunk->dst_addr;
735 hwdesc->tcr = chunk->size >> desc->xfer_shift;
736 hwdesc++;
739 return 0;
742 /* -----------------------------------------------------------------------------
743 * Stop and reset
745 static void rcar_dmac_chcr_de_barrier(struct rcar_dmac_chan *chan)
747 u32 chcr;
748 unsigned int i;
751 * Ensure that the setting of the DE bit is actually 0 after
752 * clearing it.
754 for (i = 0; i < 1024; i++) {
755 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
756 if (!(chcr & RCAR_DMACHCR_DE))
757 return;
758 udelay(1);
761 dev_err(chan->chan.device->dev, "CHCR DE check error\n");
764 static void rcar_dmac_sync_tcr(struct rcar_dmac_chan *chan)
766 u32 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
768 if (!(chcr & RCAR_DMACHCR_DE))
769 return;
771 /* set DE=0 and flush remaining data */
772 rcar_dmac_chan_write(chan, RCAR_DMACHCR, (chcr & ~RCAR_DMACHCR_DE));
774 /* make sure all remaining data was flushed */
775 rcar_dmac_chcr_de_barrier(chan);
777 /* back DE if remain data exists */
778 if (rcar_dmac_chan_read(chan, RCAR_DMATCR))
779 rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr);
782 static void rcar_dmac_chan_halt(struct rcar_dmac_chan *chan)
784 u32 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
786 chcr &= ~(RCAR_DMACHCR_DSE | RCAR_DMACHCR_DSIE | RCAR_DMACHCR_IE |
787 RCAR_DMACHCR_TE | RCAR_DMACHCR_DE);
788 rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr);
789 rcar_dmac_chcr_de_barrier(chan);
792 static void rcar_dmac_chan_reinit(struct rcar_dmac_chan *chan)
794 struct rcar_dmac_desc *desc, *_desc;
795 unsigned long flags;
796 LIST_HEAD(descs);
798 spin_lock_irqsave(&chan->lock, flags);
800 /* Move all non-free descriptors to the local lists. */
801 list_splice_init(&chan->desc.pending, &descs);
802 list_splice_init(&chan->desc.active, &descs);
803 list_splice_init(&chan->desc.done, &descs);
804 list_splice_init(&chan->desc.wait, &descs);
806 chan->desc.running = NULL;
808 spin_unlock_irqrestore(&chan->lock, flags);
810 list_for_each_entry_safe(desc, _desc, &descs, node) {
811 list_del(&desc->node);
812 rcar_dmac_desc_put(chan, desc);
816 static void rcar_dmac_stop(struct rcar_dmac *dmac)
818 rcar_dmac_write(dmac, RCAR_DMAOR, 0);
821 static void rcar_dmac_abort(struct rcar_dmac *dmac)
823 unsigned int i;
825 /* Stop all channels. */
826 for (i = 0; i < dmac->n_channels; ++i) {
827 struct rcar_dmac_chan *chan = &dmac->channels[i];
829 /* Stop and reinitialize the channel. */
830 spin_lock(&chan->lock);
831 rcar_dmac_chan_halt(chan);
832 spin_unlock(&chan->lock);
834 rcar_dmac_chan_reinit(chan);
838 /* -----------------------------------------------------------------------------
839 * Descriptors preparation
842 static void rcar_dmac_chan_configure_desc(struct rcar_dmac_chan *chan,
843 struct rcar_dmac_desc *desc)
845 static const u32 chcr_ts[] = {
846 RCAR_DMACHCR_TS_1B, RCAR_DMACHCR_TS_2B,
847 RCAR_DMACHCR_TS_4B, RCAR_DMACHCR_TS_8B,
848 RCAR_DMACHCR_TS_16B, RCAR_DMACHCR_TS_32B,
849 RCAR_DMACHCR_TS_64B,
852 unsigned int xfer_size;
853 u32 chcr;
855 switch (desc->direction) {
856 case DMA_DEV_TO_MEM:
857 chcr = RCAR_DMACHCR_DM_INC | RCAR_DMACHCR_SM_FIXED
858 | RCAR_DMACHCR_RS_DMARS;
859 xfer_size = chan->src.xfer_size;
860 break;
862 case DMA_MEM_TO_DEV:
863 chcr = RCAR_DMACHCR_DM_FIXED | RCAR_DMACHCR_SM_INC
864 | RCAR_DMACHCR_RS_DMARS;
865 xfer_size = chan->dst.xfer_size;
866 break;
868 case DMA_MEM_TO_MEM:
869 default:
870 chcr = RCAR_DMACHCR_DM_INC | RCAR_DMACHCR_SM_INC
871 | RCAR_DMACHCR_RS_AUTO;
872 xfer_size = RCAR_DMAC_MEMCPY_XFER_SIZE;
873 break;
876 desc->xfer_shift = ilog2(xfer_size);
877 desc->chcr = chcr | chcr_ts[desc->xfer_shift];
881 * rcar_dmac_chan_prep_sg - prepare transfer descriptors from an SG list
883 * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
884 * converted to scatter-gather to guarantee consistent locking and a correct
885 * list manipulation. For slave DMA direction carries the usual meaning, and,
886 * logically, the SG list is RAM and the addr variable contains slave address,
887 * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_MEM_TO_MEM
888 * and the SG list contains only one element and points at the source buffer.
890 static struct dma_async_tx_descriptor *
891 rcar_dmac_chan_prep_sg(struct rcar_dmac_chan *chan, struct scatterlist *sgl,
892 unsigned int sg_len, dma_addr_t dev_addr,
893 enum dma_transfer_direction dir, unsigned long dma_flags,
894 bool cyclic)
896 struct rcar_dmac_xfer_chunk *chunk;
897 struct rcar_dmac_desc *desc;
898 struct scatterlist *sg;
899 unsigned int nchunks = 0;
900 unsigned int max_chunk_size;
901 unsigned int full_size = 0;
902 bool cross_boundary = false;
903 unsigned int i;
904 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
905 u32 high_dev_addr;
906 u32 high_mem_addr;
907 #endif
909 desc = rcar_dmac_desc_get(chan);
910 if (!desc)
911 return NULL;
913 desc->async_tx.flags = dma_flags;
914 desc->async_tx.cookie = -EBUSY;
916 desc->cyclic = cyclic;
917 desc->direction = dir;
919 rcar_dmac_chan_configure_desc(chan, desc);
921 max_chunk_size = RCAR_DMATCR_MASK << desc->xfer_shift;
924 * Allocate and fill the transfer chunk descriptors. We own the only
925 * reference to the DMA descriptor, there's no need for locking.
927 for_each_sg(sgl, sg, sg_len, i) {
928 dma_addr_t mem_addr = sg_dma_address(sg);
929 unsigned int len = sg_dma_len(sg);
931 full_size += len;
933 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
934 if (i == 0) {
935 high_dev_addr = dev_addr >> 32;
936 high_mem_addr = mem_addr >> 32;
939 if ((dev_addr >> 32 != high_dev_addr) ||
940 (mem_addr >> 32 != high_mem_addr))
941 cross_boundary = true;
942 #endif
943 while (len) {
944 unsigned int size = min(len, max_chunk_size);
946 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
948 * Prevent individual transfers from crossing 4GB
949 * boundaries.
951 if (dev_addr >> 32 != (dev_addr + size - 1) >> 32) {
952 size = ALIGN(dev_addr, 1ULL << 32) - dev_addr;
953 cross_boundary = true;
955 if (mem_addr >> 32 != (mem_addr + size - 1) >> 32) {
956 size = ALIGN(mem_addr, 1ULL << 32) - mem_addr;
957 cross_boundary = true;
959 #endif
961 chunk = rcar_dmac_xfer_chunk_get(chan);
962 if (!chunk) {
963 rcar_dmac_desc_put(chan, desc);
964 return NULL;
967 if (dir == DMA_DEV_TO_MEM) {
968 chunk->src_addr = dev_addr;
969 chunk->dst_addr = mem_addr;
970 } else {
971 chunk->src_addr = mem_addr;
972 chunk->dst_addr = dev_addr;
975 chunk->size = size;
977 dev_dbg(chan->chan.device->dev,
978 "chan%u: chunk %p/%p sgl %u@%p, %u/%u %pad -> %pad\n",
979 chan->index, chunk, desc, i, sg, size, len,
980 &chunk->src_addr, &chunk->dst_addr);
982 mem_addr += size;
983 if (dir == DMA_MEM_TO_MEM)
984 dev_addr += size;
986 len -= size;
988 list_add_tail(&chunk->node, &desc->chunks);
989 nchunks++;
993 desc->nchunks = nchunks;
994 desc->size = full_size;
997 * Use hardware descriptor lists if possible when more than one chunk
998 * needs to be transferred (otherwise they don't make much sense).
1000 * Source/Destination address should be located in same 4GiB region
1001 * in the 40bit address space when it uses Hardware descriptor,
1002 * and cross_boundary is checking it.
1004 desc->hwdescs.use = !cross_boundary && nchunks > 1;
1005 if (desc->hwdescs.use) {
1006 if (rcar_dmac_fill_hwdesc(chan, desc) < 0)
1007 desc->hwdescs.use = false;
1010 return &desc->async_tx;
1013 /* -----------------------------------------------------------------------------
1014 * DMA engine operations
1017 static int rcar_dmac_alloc_chan_resources(struct dma_chan *chan)
1019 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1020 int ret;
1022 INIT_LIST_HEAD(&rchan->desc.chunks_free);
1023 INIT_LIST_HEAD(&rchan->desc.pages);
1025 /* Preallocate descriptors. */
1026 ret = rcar_dmac_xfer_chunk_alloc(rchan, GFP_KERNEL);
1027 if (ret < 0)
1028 return -ENOMEM;
1030 ret = rcar_dmac_desc_alloc(rchan, GFP_KERNEL);
1031 if (ret < 0)
1032 return -ENOMEM;
1034 return pm_runtime_get_sync(chan->device->dev);
1037 static void rcar_dmac_free_chan_resources(struct dma_chan *chan)
1039 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1040 struct rcar_dmac *dmac = to_rcar_dmac(chan->device);
1041 struct rcar_dmac_chan_map *map = &rchan->map;
1042 struct rcar_dmac_desc_page *page, *_page;
1043 struct rcar_dmac_desc *desc;
1044 LIST_HEAD(list);
1046 /* Protect against ISR */
1047 spin_lock_irq(&rchan->lock);
1048 rcar_dmac_chan_halt(rchan);
1049 spin_unlock_irq(&rchan->lock);
1052 * Now no new interrupts will occur, but one might already be
1053 * running. Wait for it to finish before freeing resources.
1055 synchronize_irq(rchan->irq);
1057 if (rchan->mid_rid >= 0) {
1058 /* The caller is holding dma_list_mutex */
1059 clear_bit(rchan->mid_rid, dmac->modules);
1060 rchan->mid_rid = -EINVAL;
1063 list_splice_init(&rchan->desc.free, &list);
1064 list_splice_init(&rchan->desc.pending, &list);
1065 list_splice_init(&rchan->desc.active, &list);
1066 list_splice_init(&rchan->desc.done, &list);
1067 list_splice_init(&rchan->desc.wait, &list);
1069 rchan->desc.running = NULL;
1071 list_for_each_entry(desc, &list, node)
1072 rcar_dmac_realloc_hwdesc(rchan, desc, 0);
1074 list_for_each_entry_safe(page, _page, &rchan->desc.pages, node) {
1075 list_del(&page->node);
1076 free_page((unsigned long)page);
1079 /* Remove slave mapping if present. */
1080 if (map->slave.xfer_size) {
1081 dma_unmap_resource(chan->device->dev, map->addr,
1082 map->slave.xfer_size, map->dir, 0);
1083 map->slave.xfer_size = 0;
1086 pm_runtime_put(chan->device->dev);
1089 static struct dma_async_tx_descriptor *
1090 rcar_dmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
1091 dma_addr_t dma_src, size_t len, unsigned long flags)
1093 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1094 struct scatterlist sgl;
1096 if (!len)
1097 return NULL;
1099 sg_init_table(&sgl, 1);
1100 sg_set_page(&sgl, pfn_to_page(PFN_DOWN(dma_src)), len,
1101 offset_in_page(dma_src));
1102 sg_dma_address(&sgl) = dma_src;
1103 sg_dma_len(&sgl) = len;
1105 return rcar_dmac_chan_prep_sg(rchan, &sgl, 1, dma_dest,
1106 DMA_MEM_TO_MEM, flags, false);
1109 static int rcar_dmac_map_slave_addr(struct dma_chan *chan,
1110 enum dma_transfer_direction dir)
1112 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1113 struct rcar_dmac_chan_map *map = &rchan->map;
1114 phys_addr_t dev_addr;
1115 size_t dev_size;
1116 enum dma_data_direction dev_dir;
1118 if (dir == DMA_DEV_TO_MEM) {
1119 dev_addr = rchan->src.slave_addr;
1120 dev_size = rchan->src.xfer_size;
1121 dev_dir = DMA_TO_DEVICE;
1122 } else {
1123 dev_addr = rchan->dst.slave_addr;
1124 dev_size = rchan->dst.xfer_size;
1125 dev_dir = DMA_FROM_DEVICE;
1128 /* Reuse current map if possible. */
1129 if (dev_addr == map->slave.slave_addr &&
1130 dev_size == map->slave.xfer_size &&
1131 dev_dir == map->dir)
1132 return 0;
1134 /* Remove old mapping if present. */
1135 if (map->slave.xfer_size)
1136 dma_unmap_resource(chan->device->dev, map->addr,
1137 map->slave.xfer_size, map->dir, 0);
1138 map->slave.xfer_size = 0;
1140 /* Create new slave address map. */
1141 map->addr = dma_map_resource(chan->device->dev, dev_addr, dev_size,
1142 dev_dir, 0);
1144 if (dma_mapping_error(chan->device->dev, map->addr)) {
1145 dev_err(chan->device->dev,
1146 "chan%u: failed to map %zx@%pap", rchan->index,
1147 dev_size, &dev_addr);
1148 return -EIO;
1151 dev_dbg(chan->device->dev, "chan%u: map %zx@%pap to %pad dir: %s\n",
1152 rchan->index, dev_size, &dev_addr, &map->addr,
1153 dev_dir == DMA_TO_DEVICE ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE");
1155 map->slave.slave_addr = dev_addr;
1156 map->slave.xfer_size = dev_size;
1157 map->dir = dev_dir;
1159 return 0;
1162 static struct dma_async_tx_descriptor *
1163 rcar_dmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
1164 unsigned int sg_len, enum dma_transfer_direction dir,
1165 unsigned long flags, void *context)
1167 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1169 /* Someone calling slave DMA on a generic channel? */
1170 if (rchan->mid_rid < 0 || !sg_len) {
1171 dev_warn(chan->device->dev,
1172 "%s: bad parameter: len=%d, id=%d\n",
1173 __func__, sg_len, rchan->mid_rid);
1174 return NULL;
1177 if (rcar_dmac_map_slave_addr(chan, dir))
1178 return NULL;
1180 return rcar_dmac_chan_prep_sg(rchan, sgl, sg_len, rchan->map.addr,
1181 dir, flags, false);
1184 #define RCAR_DMAC_MAX_SG_LEN 32
1186 static struct dma_async_tx_descriptor *
1187 rcar_dmac_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
1188 size_t buf_len, size_t period_len,
1189 enum dma_transfer_direction dir, unsigned long flags)
1191 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1192 struct dma_async_tx_descriptor *desc;
1193 struct scatterlist *sgl;
1194 unsigned int sg_len;
1195 unsigned int i;
1197 /* Someone calling slave DMA on a generic channel? */
1198 if (rchan->mid_rid < 0 || buf_len < period_len) {
1199 dev_warn(chan->device->dev,
1200 "%s: bad parameter: buf_len=%zu, period_len=%zu, id=%d\n",
1201 __func__, buf_len, period_len, rchan->mid_rid);
1202 return NULL;
1205 if (rcar_dmac_map_slave_addr(chan, dir))
1206 return NULL;
1208 sg_len = buf_len / period_len;
1209 if (sg_len > RCAR_DMAC_MAX_SG_LEN) {
1210 dev_err(chan->device->dev,
1211 "chan%u: sg length %d exceds limit %d",
1212 rchan->index, sg_len, RCAR_DMAC_MAX_SG_LEN);
1213 return NULL;
1217 * Allocate the sg list dynamically as it would consume too much stack
1218 * space.
1220 sgl = kcalloc(sg_len, sizeof(*sgl), GFP_NOWAIT);
1221 if (!sgl)
1222 return NULL;
1224 sg_init_table(sgl, sg_len);
1226 for (i = 0; i < sg_len; ++i) {
1227 dma_addr_t src = buf_addr + (period_len * i);
1229 sg_set_page(&sgl[i], pfn_to_page(PFN_DOWN(src)), period_len,
1230 offset_in_page(src));
1231 sg_dma_address(&sgl[i]) = src;
1232 sg_dma_len(&sgl[i]) = period_len;
1235 desc = rcar_dmac_chan_prep_sg(rchan, sgl, sg_len, rchan->map.addr,
1236 dir, flags, true);
1238 kfree(sgl);
1239 return desc;
1242 static int rcar_dmac_device_config(struct dma_chan *chan,
1243 struct dma_slave_config *cfg)
1245 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1248 * We could lock this, but you shouldn't be configuring the
1249 * channel, while using it...
1251 rchan->src.slave_addr = cfg->src_addr;
1252 rchan->dst.slave_addr = cfg->dst_addr;
1253 rchan->src.xfer_size = cfg->src_addr_width;
1254 rchan->dst.xfer_size = cfg->dst_addr_width;
1256 return 0;
1259 static int rcar_dmac_chan_terminate_all(struct dma_chan *chan)
1261 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1262 unsigned long flags;
1264 spin_lock_irqsave(&rchan->lock, flags);
1265 rcar_dmac_chan_halt(rchan);
1266 spin_unlock_irqrestore(&rchan->lock, flags);
1269 * FIXME: No new interrupt can occur now, but the IRQ thread might still
1270 * be running.
1273 rcar_dmac_chan_reinit(rchan);
1275 return 0;
1278 static unsigned int rcar_dmac_chan_get_residue(struct rcar_dmac_chan *chan,
1279 dma_cookie_t cookie)
1281 struct rcar_dmac_desc *desc = chan->desc.running;
1282 struct rcar_dmac_xfer_chunk *running = NULL;
1283 struct rcar_dmac_xfer_chunk *chunk;
1284 enum dma_status status;
1285 unsigned int residue = 0;
1286 unsigned int dptr = 0;
1288 if (!desc)
1289 return 0;
1292 * If the cookie corresponds to a descriptor that has been completed
1293 * there is no residue. The same check has already been performed by the
1294 * caller but without holding the channel lock, so the descriptor could
1295 * now be complete.
1297 status = dma_cookie_status(&chan->chan, cookie, NULL);
1298 if (status == DMA_COMPLETE)
1299 return 0;
1302 * If the cookie doesn't correspond to the currently running transfer
1303 * then the descriptor hasn't been processed yet, and the residue is
1304 * equal to the full descriptor size.
1305 * Also, a client driver is possible to call this function before
1306 * rcar_dmac_isr_channel_thread() runs. In this case, the "desc.running"
1307 * will be the next descriptor, and the done list will appear. So, if
1308 * the argument cookie matches the done list's cookie, we can assume
1309 * the residue is zero.
1311 if (cookie != desc->async_tx.cookie) {
1312 list_for_each_entry(desc, &chan->desc.done, node) {
1313 if (cookie == desc->async_tx.cookie)
1314 return 0;
1316 list_for_each_entry(desc, &chan->desc.pending, node) {
1317 if (cookie == desc->async_tx.cookie)
1318 return desc->size;
1320 list_for_each_entry(desc, &chan->desc.active, node) {
1321 if (cookie == desc->async_tx.cookie)
1322 return desc->size;
1326 * No descriptor found for the cookie, there's thus no residue.
1327 * This shouldn't happen if the calling driver passes a correct
1328 * cookie value.
1330 WARN(1, "No descriptor for cookie!");
1331 return 0;
1335 * In descriptor mode the descriptor running pointer is not maintained
1336 * by the interrupt handler, find the running descriptor from the
1337 * descriptor pointer field in the CHCRB register. In non-descriptor
1338 * mode just use the running descriptor pointer.
1340 if (desc->hwdescs.use) {
1341 dptr = (rcar_dmac_chan_read(chan, RCAR_DMACHCRB) &
1342 RCAR_DMACHCRB_DPTR_MASK) >> RCAR_DMACHCRB_DPTR_SHIFT;
1343 if (dptr == 0)
1344 dptr = desc->nchunks;
1345 dptr--;
1346 WARN_ON(dptr >= desc->nchunks);
1347 } else {
1348 running = desc->running;
1351 /* Compute the size of all chunks still to be transferred. */
1352 list_for_each_entry_reverse(chunk, &desc->chunks, node) {
1353 if (chunk == running || ++dptr == desc->nchunks)
1354 break;
1356 residue += chunk->size;
1359 if (desc->direction == DMA_DEV_TO_MEM)
1360 rcar_dmac_sync_tcr(chan);
1362 /* Add the residue for the current chunk. */
1363 residue += rcar_dmac_chan_read(chan, RCAR_DMATCRB) << desc->xfer_shift;
1365 return residue;
1368 static enum dma_status rcar_dmac_tx_status(struct dma_chan *chan,
1369 dma_cookie_t cookie,
1370 struct dma_tx_state *txstate)
1372 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1373 enum dma_status status;
1374 unsigned long flags;
1375 unsigned int residue;
1377 status = dma_cookie_status(chan, cookie, txstate);
1378 if (status == DMA_COMPLETE || !txstate)
1379 return status;
1381 spin_lock_irqsave(&rchan->lock, flags);
1382 residue = rcar_dmac_chan_get_residue(rchan, cookie);
1383 spin_unlock_irqrestore(&rchan->lock, flags);
1385 /* if there's no residue, the cookie is complete */
1386 if (!residue)
1387 return DMA_COMPLETE;
1389 dma_set_residue(txstate, residue);
1391 return status;
1394 static void rcar_dmac_issue_pending(struct dma_chan *chan)
1396 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1397 unsigned long flags;
1399 spin_lock_irqsave(&rchan->lock, flags);
1401 if (list_empty(&rchan->desc.pending))
1402 goto done;
1404 /* Append the pending list to the active list. */
1405 list_splice_tail_init(&rchan->desc.pending, &rchan->desc.active);
1408 * If no transfer is running pick the first descriptor from the active
1409 * list and start the transfer.
1411 if (!rchan->desc.running) {
1412 struct rcar_dmac_desc *desc;
1414 desc = list_first_entry(&rchan->desc.active,
1415 struct rcar_dmac_desc, node);
1416 rchan->desc.running = desc;
1418 rcar_dmac_chan_start_xfer(rchan);
1421 done:
1422 spin_unlock_irqrestore(&rchan->lock, flags);
1425 static void rcar_dmac_device_synchronize(struct dma_chan *chan)
1427 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1429 synchronize_irq(rchan->irq);
1432 /* -----------------------------------------------------------------------------
1433 * IRQ handling
1436 static irqreturn_t rcar_dmac_isr_desc_stage_end(struct rcar_dmac_chan *chan)
1438 struct rcar_dmac_desc *desc = chan->desc.running;
1439 unsigned int stage;
1441 if (WARN_ON(!desc || !desc->cyclic)) {
1443 * This should never happen, there should always be a running
1444 * cyclic descriptor when a descriptor stage end interrupt is
1445 * triggered. Warn and return.
1447 return IRQ_NONE;
1450 /* Program the interrupt pointer to the next stage. */
1451 stage = (rcar_dmac_chan_read(chan, RCAR_DMACHCRB) &
1452 RCAR_DMACHCRB_DPTR_MASK) >> RCAR_DMACHCRB_DPTR_SHIFT;
1453 rcar_dmac_chan_write(chan, RCAR_DMADPCR, RCAR_DMADPCR_DIPT(stage));
1455 return IRQ_WAKE_THREAD;
1458 static irqreturn_t rcar_dmac_isr_transfer_end(struct rcar_dmac_chan *chan)
1460 struct rcar_dmac_desc *desc = chan->desc.running;
1461 irqreturn_t ret = IRQ_WAKE_THREAD;
1463 if (WARN_ON_ONCE(!desc)) {
1465 * This should never happen, there should always be a running
1466 * descriptor when a transfer end interrupt is triggered. Warn
1467 * and return.
1469 return IRQ_NONE;
1473 * The transfer end interrupt isn't generated for each chunk when using
1474 * descriptor mode. Only update the running chunk pointer in
1475 * non-descriptor mode.
1477 if (!desc->hwdescs.use) {
1479 * If we haven't completed the last transfer chunk simply move
1480 * to the next one. Only wake the IRQ thread if the transfer is
1481 * cyclic.
1483 if (!list_is_last(&desc->running->node, &desc->chunks)) {
1484 desc->running = list_next_entry(desc->running, node);
1485 if (!desc->cyclic)
1486 ret = IRQ_HANDLED;
1487 goto done;
1491 * We've completed the last transfer chunk. If the transfer is
1492 * cyclic, move back to the first one.
1494 if (desc->cyclic) {
1495 desc->running =
1496 list_first_entry(&desc->chunks,
1497 struct rcar_dmac_xfer_chunk,
1498 node);
1499 goto done;
1503 /* The descriptor is complete, move it to the done list. */
1504 list_move_tail(&desc->node, &chan->desc.done);
1506 /* Queue the next descriptor, if any. */
1507 if (!list_empty(&chan->desc.active))
1508 chan->desc.running = list_first_entry(&chan->desc.active,
1509 struct rcar_dmac_desc,
1510 node);
1511 else
1512 chan->desc.running = NULL;
1514 done:
1515 if (chan->desc.running)
1516 rcar_dmac_chan_start_xfer(chan);
1518 return ret;
1521 static irqreturn_t rcar_dmac_isr_channel(int irq, void *dev)
1523 u32 mask = RCAR_DMACHCR_DSE | RCAR_DMACHCR_TE;
1524 struct rcar_dmac_chan *chan = dev;
1525 irqreturn_t ret = IRQ_NONE;
1526 u32 chcr;
1528 spin_lock(&chan->lock);
1530 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
1531 if (chcr & RCAR_DMACHCR_TE)
1532 mask |= RCAR_DMACHCR_DE;
1533 rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr & ~mask);
1534 if (mask & RCAR_DMACHCR_DE)
1535 rcar_dmac_chcr_de_barrier(chan);
1537 if (chcr & RCAR_DMACHCR_DSE)
1538 ret |= rcar_dmac_isr_desc_stage_end(chan);
1540 if (chcr & RCAR_DMACHCR_TE)
1541 ret |= rcar_dmac_isr_transfer_end(chan);
1543 spin_unlock(&chan->lock);
1545 return ret;
1548 static irqreturn_t rcar_dmac_isr_channel_thread(int irq, void *dev)
1550 struct rcar_dmac_chan *chan = dev;
1551 struct rcar_dmac_desc *desc;
1552 struct dmaengine_desc_callback cb;
1554 spin_lock_irq(&chan->lock);
1556 /* For cyclic transfers notify the user after every chunk. */
1557 if (chan->desc.running && chan->desc.running->cyclic) {
1558 desc = chan->desc.running;
1559 dmaengine_desc_get_callback(&desc->async_tx, &cb);
1561 if (dmaengine_desc_callback_valid(&cb)) {
1562 spin_unlock_irq(&chan->lock);
1563 dmaengine_desc_callback_invoke(&cb, NULL);
1564 spin_lock_irq(&chan->lock);
1569 * Call the callback function for all descriptors on the done list and
1570 * move them to the ack wait list.
1572 while (!list_empty(&chan->desc.done)) {
1573 desc = list_first_entry(&chan->desc.done, struct rcar_dmac_desc,
1574 node);
1575 dma_cookie_complete(&desc->async_tx);
1576 list_del(&desc->node);
1578 dmaengine_desc_get_callback(&desc->async_tx, &cb);
1579 if (dmaengine_desc_callback_valid(&cb)) {
1580 spin_unlock_irq(&chan->lock);
1582 * We own the only reference to this descriptor, we can
1583 * safely dereference it without holding the channel
1584 * lock.
1586 dmaengine_desc_callback_invoke(&cb, NULL);
1587 spin_lock_irq(&chan->lock);
1590 list_add_tail(&desc->node, &chan->desc.wait);
1593 spin_unlock_irq(&chan->lock);
1595 /* Recycle all acked descriptors. */
1596 rcar_dmac_desc_recycle_acked(chan);
1598 return IRQ_HANDLED;
1601 static irqreturn_t rcar_dmac_isr_error(int irq, void *data)
1603 struct rcar_dmac *dmac = data;
1605 if (!(rcar_dmac_read(dmac, RCAR_DMAOR) & RCAR_DMAOR_AE))
1606 return IRQ_NONE;
1609 * An unrecoverable error occurred on an unknown channel. Halt the DMAC,
1610 * abort transfers on all channels, and reinitialize the DMAC.
1612 rcar_dmac_stop(dmac);
1613 rcar_dmac_abort(dmac);
1614 rcar_dmac_init(dmac);
1616 return IRQ_HANDLED;
1619 /* -----------------------------------------------------------------------------
1620 * OF xlate and channel filter
1623 static bool rcar_dmac_chan_filter(struct dma_chan *chan, void *arg)
1625 struct rcar_dmac *dmac = to_rcar_dmac(chan->device);
1626 struct of_phandle_args *dma_spec = arg;
1629 * FIXME: Using a filter on OF platforms is a nonsense. The OF xlate
1630 * function knows from which device it wants to allocate a channel from,
1631 * and would be perfectly capable of selecting the channel it wants.
1632 * Forcing it to call dma_request_channel() and iterate through all
1633 * channels from all controllers is just pointless.
1635 if (chan->device->device_config != rcar_dmac_device_config ||
1636 dma_spec->np != chan->device->dev->of_node)
1637 return false;
1639 return !test_and_set_bit(dma_spec->args[0], dmac->modules);
1642 static struct dma_chan *rcar_dmac_of_xlate(struct of_phandle_args *dma_spec,
1643 struct of_dma *ofdma)
1645 struct rcar_dmac_chan *rchan;
1646 struct dma_chan *chan;
1647 dma_cap_mask_t mask;
1649 if (dma_spec->args_count != 1)
1650 return NULL;
1652 /* Only slave DMA channels can be allocated via DT */
1653 dma_cap_zero(mask);
1654 dma_cap_set(DMA_SLAVE, mask);
1656 chan = dma_request_channel(mask, rcar_dmac_chan_filter, dma_spec);
1657 if (!chan)
1658 return NULL;
1660 rchan = to_rcar_dmac_chan(chan);
1661 rchan->mid_rid = dma_spec->args[0];
1663 return chan;
1666 /* -----------------------------------------------------------------------------
1667 * Power management
1670 #ifdef CONFIG_PM
1671 static int rcar_dmac_runtime_suspend(struct device *dev)
1673 return 0;
1676 static int rcar_dmac_runtime_resume(struct device *dev)
1678 struct rcar_dmac *dmac = dev_get_drvdata(dev);
1680 return rcar_dmac_init(dmac);
1682 #endif
1684 static const struct dev_pm_ops rcar_dmac_pm = {
1686 * TODO for system sleep/resume:
1687 * - Wait for the current transfer to complete and stop the device,
1688 * - Resume transfers, if any.
1690 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1691 pm_runtime_force_resume)
1692 SET_RUNTIME_PM_OPS(rcar_dmac_runtime_suspend, rcar_dmac_runtime_resume,
1693 NULL)
1696 /* -----------------------------------------------------------------------------
1697 * Probe and remove
1700 static int rcar_dmac_chan_probe(struct rcar_dmac *dmac,
1701 struct rcar_dmac_chan *rchan,
1702 unsigned int index)
1704 struct platform_device *pdev = to_platform_device(dmac->dev);
1705 struct dma_chan *chan = &rchan->chan;
1706 char pdev_irqname[5];
1707 char *irqname;
1708 int ret;
1710 rchan->index = index;
1711 rchan->iomem = dmac->iomem + RCAR_DMAC_CHAN_OFFSET(index);
1712 rchan->mid_rid = -EINVAL;
1714 spin_lock_init(&rchan->lock);
1716 INIT_LIST_HEAD(&rchan->desc.free);
1717 INIT_LIST_HEAD(&rchan->desc.pending);
1718 INIT_LIST_HEAD(&rchan->desc.active);
1719 INIT_LIST_HEAD(&rchan->desc.done);
1720 INIT_LIST_HEAD(&rchan->desc.wait);
1722 /* Request the channel interrupt. */
1723 sprintf(pdev_irqname, "ch%u", index);
1724 rchan->irq = platform_get_irq_byname(pdev, pdev_irqname);
1725 if (rchan->irq < 0) {
1726 dev_err(dmac->dev, "no IRQ specified for channel %u\n", index);
1727 return -ENODEV;
1730 irqname = devm_kasprintf(dmac->dev, GFP_KERNEL, "%s:%u",
1731 dev_name(dmac->dev), index);
1732 if (!irqname)
1733 return -ENOMEM;
1736 * Initialize the DMA engine channel and add it to the DMA engine
1737 * channels list.
1739 chan->device = &dmac->engine;
1740 dma_cookie_init(chan);
1742 list_add_tail(&chan->device_node, &dmac->engine.channels);
1744 ret = devm_request_threaded_irq(dmac->dev, rchan->irq,
1745 rcar_dmac_isr_channel,
1746 rcar_dmac_isr_channel_thread, 0,
1747 irqname, rchan);
1748 if (ret) {
1749 dev_err(dmac->dev, "failed to request IRQ %u (%d)\n",
1750 rchan->irq, ret);
1751 return ret;
1754 return 0;
1757 static int rcar_dmac_parse_of(struct device *dev, struct rcar_dmac *dmac)
1759 struct device_node *np = dev->of_node;
1760 int ret;
1762 ret = of_property_read_u32(np, "dma-channels", &dmac->n_channels);
1763 if (ret < 0) {
1764 dev_err(dev, "unable to read dma-channels property\n");
1765 return ret;
1768 if (dmac->n_channels <= 0 || dmac->n_channels >= 100) {
1769 dev_err(dev, "invalid number of channels %u\n",
1770 dmac->n_channels);
1771 return -EINVAL;
1774 return 0;
1777 static int rcar_dmac_probe(struct platform_device *pdev)
1779 const enum dma_slave_buswidth widths = DMA_SLAVE_BUSWIDTH_1_BYTE |
1780 DMA_SLAVE_BUSWIDTH_2_BYTES | DMA_SLAVE_BUSWIDTH_4_BYTES |
1781 DMA_SLAVE_BUSWIDTH_8_BYTES | DMA_SLAVE_BUSWIDTH_16_BYTES |
1782 DMA_SLAVE_BUSWIDTH_32_BYTES | DMA_SLAVE_BUSWIDTH_64_BYTES;
1783 unsigned int channels_offset = 0;
1784 struct dma_device *engine;
1785 struct rcar_dmac *dmac;
1786 struct resource *mem;
1787 unsigned int i;
1788 char *irqname;
1789 int irq;
1790 int ret;
1792 dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL);
1793 if (!dmac)
1794 return -ENOMEM;
1796 dmac->dev = &pdev->dev;
1797 platform_set_drvdata(pdev, dmac);
1798 dma_set_mask_and_coherent(dmac->dev, DMA_BIT_MASK(40));
1800 ret = rcar_dmac_parse_of(&pdev->dev, dmac);
1801 if (ret < 0)
1802 return ret;
1805 * A still unconfirmed hardware bug prevents the IPMMU microTLB 0 to be
1806 * flushed correctly, resulting in memory corruption. DMAC 0 channel 0
1807 * is connected to microTLB 0 on currently supported platforms, so we
1808 * can't use it with the IPMMU. As the IOMMU API operates at the device
1809 * level we can't disable it selectively, so ignore channel 0 for now if
1810 * the device is part of an IOMMU group.
1812 if (pdev->dev.iommu_group) {
1813 dmac->n_channels--;
1814 channels_offset = 1;
1817 dmac->channels = devm_kcalloc(&pdev->dev, dmac->n_channels,
1818 sizeof(*dmac->channels), GFP_KERNEL);
1819 if (!dmac->channels)
1820 return -ENOMEM;
1822 /* Request resources. */
1823 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1824 dmac->iomem = devm_ioremap_resource(&pdev->dev, mem);
1825 if (IS_ERR(dmac->iomem))
1826 return PTR_ERR(dmac->iomem);
1828 irq = platform_get_irq_byname(pdev, "error");
1829 if (irq < 0) {
1830 dev_err(&pdev->dev, "no error IRQ specified\n");
1831 return -ENODEV;
1834 irqname = devm_kasprintf(dmac->dev, GFP_KERNEL, "%s:error",
1835 dev_name(dmac->dev));
1836 if (!irqname)
1837 return -ENOMEM;
1839 /* Enable runtime PM and initialize the device. */
1840 pm_runtime_enable(&pdev->dev);
1841 ret = pm_runtime_get_sync(&pdev->dev);
1842 if (ret < 0) {
1843 dev_err(&pdev->dev, "runtime PM get sync failed (%d)\n", ret);
1844 return ret;
1847 ret = rcar_dmac_init(dmac);
1848 pm_runtime_put(&pdev->dev);
1850 if (ret) {
1851 dev_err(&pdev->dev, "failed to reset device\n");
1852 goto error;
1855 /* Initialize engine */
1856 engine = &dmac->engine;
1858 dma_cap_set(DMA_MEMCPY, engine->cap_mask);
1859 dma_cap_set(DMA_SLAVE, engine->cap_mask);
1861 engine->dev = &pdev->dev;
1862 engine->copy_align = ilog2(RCAR_DMAC_MEMCPY_XFER_SIZE);
1864 engine->src_addr_widths = widths;
1865 engine->dst_addr_widths = widths;
1866 engine->directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
1867 engine->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1869 engine->device_alloc_chan_resources = rcar_dmac_alloc_chan_resources;
1870 engine->device_free_chan_resources = rcar_dmac_free_chan_resources;
1871 engine->device_prep_dma_memcpy = rcar_dmac_prep_dma_memcpy;
1872 engine->device_prep_slave_sg = rcar_dmac_prep_slave_sg;
1873 engine->device_prep_dma_cyclic = rcar_dmac_prep_dma_cyclic;
1874 engine->device_config = rcar_dmac_device_config;
1875 engine->device_terminate_all = rcar_dmac_chan_terminate_all;
1876 engine->device_tx_status = rcar_dmac_tx_status;
1877 engine->device_issue_pending = rcar_dmac_issue_pending;
1878 engine->device_synchronize = rcar_dmac_device_synchronize;
1880 INIT_LIST_HEAD(&engine->channels);
1882 for (i = 0; i < dmac->n_channels; ++i) {
1883 ret = rcar_dmac_chan_probe(dmac, &dmac->channels[i],
1884 i + channels_offset);
1885 if (ret < 0)
1886 goto error;
1889 ret = devm_request_irq(&pdev->dev, irq, rcar_dmac_isr_error, 0,
1890 irqname, dmac);
1891 if (ret) {
1892 dev_err(&pdev->dev, "failed to request IRQ %u (%d)\n",
1893 irq, ret);
1894 return ret;
1897 /* Register the DMAC as a DMA provider for DT. */
1898 ret = of_dma_controller_register(pdev->dev.of_node, rcar_dmac_of_xlate,
1899 NULL);
1900 if (ret < 0)
1901 goto error;
1904 * Register the DMA engine device.
1906 * Default transfer size of 32 bytes requires 32-byte alignment.
1908 ret = dma_async_device_register(engine);
1909 if (ret < 0)
1910 goto error;
1912 return 0;
1914 error:
1915 of_dma_controller_free(pdev->dev.of_node);
1916 pm_runtime_disable(&pdev->dev);
1917 return ret;
1920 static int rcar_dmac_remove(struct platform_device *pdev)
1922 struct rcar_dmac *dmac = platform_get_drvdata(pdev);
1924 of_dma_controller_free(pdev->dev.of_node);
1925 dma_async_device_unregister(&dmac->engine);
1927 pm_runtime_disable(&pdev->dev);
1929 return 0;
1932 static void rcar_dmac_shutdown(struct platform_device *pdev)
1934 struct rcar_dmac *dmac = platform_get_drvdata(pdev);
1936 rcar_dmac_stop(dmac);
1939 static const struct of_device_id rcar_dmac_of_ids[] = {
1940 { .compatible = "renesas,rcar-dmac", },
1941 { /* Sentinel */ }
1943 MODULE_DEVICE_TABLE(of, rcar_dmac_of_ids);
1945 static struct platform_driver rcar_dmac_driver = {
1946 .driver = {
1947 .pm = &rcar_dmac_pm,
1948 .name = "rcar-dmac",
1949 .of_match_table = rcar_dmac_of_ids,
1951 .probe = rcar_dmac_probe,
1952 .remove = rcar_dmac_remove,
1953 .shutdown = rcar_dmac_shutdown,
1956 module_platform_driver(rcar_dmac_driver);
1958 MODULE_DESCRIPTION("R-Car Gen2 DMA Controller Driver");
1959 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1960 MODULE_LICENSE("GPL v2");