Linux 4.19.133
[linux/fpc-iii.git] / drivers / dma / sh / rcar-dmac.c
blob80ff95f75199f0ce30f733ebba6a727a7921a31b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Renesas R-Car Gen2 DMA Controller Driver
5 * Copyright (C) 2014 Renesas Electronics Inc.
7 * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 */
10 #include <linux/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/dmaengine.h>
13 #include <linux/interrupt.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/of.h>
18 #include <linux/of_dma.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
25 #include "../dmaengine.h"
28 * struct rcar_dmac_xfer_chunk - Descriptor for a hardware transfer
29 * @node: entry in the parent's chunks list
30 * @src_addr: device source address
31 * @dst_addr: device destination address
32 * @size: transfer size in bytes
34 struct rcar_dmac_xfer_chunk {
35 struct list_head node;
37 dma_addr_t src_addr;
38 dma_addr_t dst_addr;
39 u32 size;
43 * struct rcar_dmac_hw_desc - Hardware descriptor for a transfer chunk
44 * @sar: value of the SAR register (source address)
45 * @dar: value of the DAR register (destination address)
46 * @tcr: value of the TCR register (transfer count)
48 struct rcar_dmac_hw_desc {
49 u32 sar;
50 u32 dar;
51 u32 tcr;
52 u32 reserved;
53 } __attribute__((__packed__));
56 * struct rcar_dmac_desc - R-Car Gen2 DMA Transfer Descriptor
57 * @async_tx: base DMA asynchronous transaction descriptor
58 * @direction: direction of the DMA transfer
59 * @xfer_shift: log2 of the transfer size
60 * @chcr: value of the channel configuration register for this transfer
61 * @node: entry in the channel's descriptors lists
62 * @chunks: list of transfer chunks for this transfer
63 * @running: the transfer chunk being currently processed
64 * @nchunks: number of transfer chunks for this transfer
65 * @hwdescs.use: whether the transfer descriptor uses hardware descriptors
66 * @hwdescs.mem: hardware descriptors memory for the transfer
67 * @hwdescs.dma: device address of the hardware descriptors memory
68 * @hwdescs.size: size of the hardware descriptors in bytes
69 * @size: transfer size in bytes
70 * @cyclic: when set indicates that the DMA transfer is cyclic
72 struct rcar_dmac_desc {
73 struct dma_async_tx_descriptor async_tx;
74 enum dma_transfer_direction direction;
75 unsigned int xfer_shift;
76 u32 chcr;
78 struct list_head node;
79 struct list_head chunks;
80 struct rcar_dmac_xfer_chunk *running;
81 unsigned int nchunks;
83 struct {
84 bool use;
85 struct rcar_dmac_hw_desc *mem;
86 dma_addr_t dma;
87 size_t size;
88 } hwdescs;
90 unsigned int size;
91 bool cyclic;
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;
105 union {
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_slave - Slave configuration
120 * @slave_addr: slave memory address
121 * @xfer_size: size (in bytes) of hardware transfers
123 struct rcar_dmac_chan_slave {
124 phys_addr_t slave_addr;
125 unsigned int xfer_size;
129 * struct rcar_dmac_chan_map - Map of slave device phys to dma address
130 * @addr: slave dma address
131 * @dir: direction of mapping
132 * @slave: slave configuration that is mapped
134 struct rcar_dmac_chan_map {
135 dma_addr_t addr;
136 enum dma_data_direction dir;
137 struct rcar_dmac_chan_slave slave;
141 * struct rcar_dmac_chan - R-Car Gen2 DMA Controller Channel
142 * @chan: base DMA channel object
143 * @iomem: channel I/O memory base
144 * @index: index of this channel in the controller
145 * @irq: channel IRQ
146 * @src: slave memory address and size on the source side
147 * @dst: slave memory address and size on the destination side
148 * @mid_rid: hardware MID/RID for the DMA client using this channel
149 * @lock: protects the channel CHCR register and the desc members
150 * @desc.free: list of free descriptors
151 * @desc.pending: list of pending descriptors (submitted with tx_submit)
152 * @desc.active: list of active descriptors (activated with issue_pending)
153 * @desc.done: list of completed descriptors
154 * @desc.wait: list of descriptors waiting for an ack
155 * @desc.running: the descriptor being processed (a member of the active list)
156 * @desc.chunks_free: list of free transfer chunk descriptors
157 * @desc.pages: list of pages used by allocated descriptors
159 struct rcar_dmac_chan {
160 struct dma_chan chan;
161 void __iomem *iomem;
162 unsigned int index;
163 int irq;
165 struct rcar_dmac_chan_slave src;
166 struct rcar_dmac_chan_slave dst;
167 struct rcar_dmac_chan_map map;
168 int mid_rid;
170 spinlock_t lock;
172 struct {
173 struct list_head free;
174 struct list_head pending;
175 struct list_head active;
176 struct list_head done;
177 struct list_head wait;
178 struct rcar_dmac_desc *running;
180 struct list_head chunks_free;
182 struct list_head pages;
183 } desc;
186 #define to_rcar_dmac_chan(c) container_of(c, struct rcar_dmac_chan, chan)
189 * struct rcar_dmac - R-Car Gen2 DMA Controller
190 * @engine: base DMA engine object
191 * @dev: the hardware device
192 * @iomem: remapped I/O memory base
193 * @n_channels: number of available channels
194 * @channels: array of DMAC channels
195 * @modules: bitmask of client modules in use
197 struct rcar_dmac {
198 struct dma_device engine;
199 struct device *dev;
200 void __iomem *iomem;
201 struct device_dma_parameters parms;
203 unsigned int n_channels;
204 struct rcar_dmac_chan *channels;
206 DECLARE_BITMAP(modules, 256);
209 #define to_rcar_dmac(d) container_of(d, struct rcar_dmac, engine)
211 /* -----------------------------------------------------------------------------
212 * Registers
215 #define RCAR_DMAC_CHAN_OFFSET(i) (0x8000 + 0x80 * (i))
217 #define RCAR_DMAISTA 0x0020
218 #define RCAR_DMASEC 0x0030
219 #define RCAR_DMAOR 0x0060
220 #define RCAR_DMAOR_PRI_FIXED (0 << 8)
221 #define RCAR_DMAOR_PRI_ROUND_ROBIN (3 << 8)
222 #define RCAR_DMAOR_AE (1 << 2)
223 #define RCAR_DMAOR_DME (1 << 0)
224 #define RCAR_DMACHCLR 0x0080
225 #define RCAR_DMADPSEC 0x00a0
227 #define RCAR_DMASAR 0x0000
228 #define RCAR_DMADAR 0x0004
229 #define RCAR_DMATCR 0x0008
230 #define RCAR_DMATCR_MASK 0x00ffffff
231 #define RCAR_DMATSR 0x0028
232 #define RCAR_DMACHCR 0x000c
233 #define RCAR_DMACHCR_CAE (1 << 31)
234 #define RCAR_DMACHCR_CAIE (1 << 30)
235 #define RCAR_DMACHCR_DPM_DISABLED (0 << 28)
236 #define RCAR_DMACHCR_DPM_ENABLED (1 << 28)
237 #define RCAR_DMACHCR_DPM_REPEAT (2 << 28)
238 #define RCAR_DMACHCR_DPM_INFINITE (3 << 28)
239 #define RCAR_DMACHCR_RPT_SAR (1 << 27)
240 #define RCAR_DMACHCR_RPT_DAR (1 << 26)
241 #define RCAR_DMACHCR_RPT_TCR (1 << 25)
242 #define RCAR_DMACHCR_DPB (1 << 22)
243 #define RCAR_DMACHCR_DSE (1 << 19)
244 #define RCAR_DMACHCR_DSIE (1 << 18)
245 #define RCAR_DMACHCR_TS_1B ((0 << 20) | (0 << 3))
246 #define RCAR_DMACHCR_TS_2B ((0 << 20) | (1 << 3))
247 #define RCAR_DMACHCR_TS_4B ((0 << 20) | (2 << 3))
248 #define RCAR_DMACHCR_TS_16B ((0 << 20) | (3 << 3))
249 #define RCAR_DMACHCR_TS_32B ((1 << 20) | (0 << 3))
250 #define RCAR_DMACHCR_TS_64B ((1 << 20) | (1 << 3))
251 #define RCAR_DMACHCR_TS_8B ((1 << 20) | (3 << 3))
252 #define RCAR_DMACHCR_DM_FIXED (0 << 14)
253 #define RCAR_DMACHCR_DM_INC (1 << 14)
254 #define RCAR_DMACHCR_DM_DEC (2 << 14)
255 #define RCAR_DMACHCR_SM_FIXED (0 << 12)
256 #define RCAR_DMACHCR_SM_INC (1 << 12)
257 #define RCAR_DMACHCR_SM_DEC (2 << 12)
258 #define RCAR_DMACHCR_RS_AUTO (4 << 8)
259 #define RCAR_DMACHCR_RS_DMARS (8 << 8)
260 #define RCAR_DMACHCR_IE (1 << 2)
261 #define RCAR_DMACHCR_TE (1 << 1)
262 #define RCAR_DMACHCR_DE (1 << 0)
263 #define RCAR_DMATCRB 0x0018
264 #define RCAR_DMATSRB 0x0038
265 #define RCAR_DMACHCRB 0x001c
266 #define RCAR_DMACHCRB_DCNT(n) ((n) << 24)
267 #define RCAR_DMACHCRB_DPTR_MASK (0xff << 16)
268 #define RCAR_DMACHCRB_DPTR_SHIFT 16
269 #define RCAR_DMACHCRB_DRST (1 << 15)
270 #define RCAR_DMACHCRB_DTS (1 << 8)
271 #define RCAR_DMACHCRB_SLM_NORMAL (0 << 4)
272 #define RCAR_DMACHCRB_SLM_CLK(n) ((8 | (n)) << 4)
273 #define RCAR_DMACHCRB_PRI(n) ((n) << 0)
274 #define RCAR_DMARS 0x0040
275 #define RCAR_DMABUFCR 0x0048
276 #define RCAR_DMABUFCR_MBU(n) ((n) << 16)
277 #define RCAR_DMABUFCR_ULB(n) ((n) << 0)
278 #define RCAR_DMADPBASE 0x0050
279 #define RCAR_DMADPBASE_MASK 0xfffffff0
280 #define RCAR_DMADPBASE_SEL (1 << 0)
281 #define RCAR_DMADPCR 0x0054
282 #define RCAR_DMADPCR_DIPT(n) ((n) << 24)
283 #define RCAR_DMAFIXSAR 0x0010
284 #define RCAR_DMAFIXDAR 0x0014
285 #define RCAR_DMAFIXDPBASE 0x0060
287 /* Hardcode the MEMCPY transfer size to 4 bytes. */
288 #define RCAR_DMAC_MEMCPY_XFER_SIZE 4
290 /* -----------------------------------------------------------------------------
291 * Device access
294 static void rcar_dmac_write(struct rcar_dmac *dmac, u32 reg, u32 data)
296 if (reg == RCAR_DMAOR)
297 writew(data, dmac->iomem + reg);
298 else
299 writel(data, dmac->iomem + reg);
302 static u32 rcar_dmac_read(struct rcar_dmac *dmac, u32 reg)
304 if (reg == RCAR_DMAOR)
305 return readw(dmac->iomem + reg);
306 else
307 return readl(dmac->iomem + reg);
310 static u32 rcar_dmac_chan_read(struct rcar_dmac_chan *chan, u32 reg)
312 if (reg == RCAR_DMARS)
313 return readw(chan->iomem + reg);
314 else
315 return readl(chan->iomem + reg);
318 static void rcar_dmac_chan_write(struct rcar_dmac_chan *chan, u32 reg, u32 data)
320 if (reg == RCAR_DMARS)
321 writew(data, chan->iomem + reg);
322 else
323 writel(data, chan->iomem + reg);
326 /* -----------------------------------------------------------------------------
327 * Initialization and configuration
330 static bool rcar_dmac_chan_is_busy(struct rcar_dmac_chan *chan)
332 u32 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
334 return !!(chcr & (RCAR_DMACHCR_DE | RCAR_DMACHCR_TE));
337 static void rcar_dmac_chan_start_xfer(struct rcar_dmac_chan *chan)
339 struct rcar_dmac_desc *desc = chan->desc.running;
340 u32 chcr = desc->chcr;
342 WARN_ON_ONCE(rcar_dmac_chan_is_busy(chan));
344 if (chan->mid_rid >= 0)
345 rcar_dmac_chan_write(chan, RCAR_DMARS, chan->mid_rid);
347 if (desc->hwdescs.use) {
348 struct rcar_dmac_xfer_chunk *chunk =
349 list_first_entry(&desc->chunks,
350 struct rcar_dmac_xfer_chunk, node);
352 dev_dbg(chan->chan.device->dev,
353 "chan%u: queue desc %p: %u@%pad\n",
354 chan->index, desc, desc->nchunks, &desc->hwdescs.dma);
356 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
357 rcar_dmac_chan_write(chan, RCAR_DMAFIXSAR,
358 chunk->src_addr >> 32);
359 rcar_dmac_chan_write(chan, RCAR_DMAFIXDAR,
360 chunk->dst_addr >> 32);
361 rcar_dmac_chan_write(chan, RCAR_DMAFIXDPBASE,
362 desc->hwdescs.dma >> 32);
363 #endif
364 rcar_dmac_chan_write(chan, RCAR_DMADPBASE,
365 (desc->hwdescs.dma & 0xfffffff0) |
366 RCAR_DMADPBASE_SEL);
367 rcar_dmac_chan_write(chan, RCAR_DMACHCRB,
368 RCAR_DMACHCRB_DCNT(desc->nchunks - 1) |
369 RCAR_DMACHCRB_DRST);
372 * Errata: When descriptor memory is accessed through an IOMMU
373 * the DMADAR register isn't initialized automatically from the
374 * first descriptor at beginning of transfer by the DMAC like it
375 * should. Initialize it manually with the destination address
376 * of the first chunk.
378 rcar_dmac_chan_write(chan, RCAR_DMADAR,
379 chunk->dst_addr & 0xffffffff);
382 * Program the descriptor stage interrupt to occur after the end
383 * of the first stage.
385 rcar_dmac_chan_write(chan, RCAR_DMADPCR, RCAR_DMADPCR_DIPT(1));
387 chcr |= RCAR_DMACHCR_RPT_SAR | RCAR_DMACHCR_RPT_DAR
388 | RCAR_DMACHCR_RPT_TCR | RCAR_DMACHCR_DPB;
391 * If the descriptor isn't cyclic enable normal descriptor mode
392 * and the transfer completion interrupt.
394 if (!desc->cyclic)
395 chcr |= RCAR_DMACHCR_DPM_ENABLED | RCAR_DMACHCR_IE;
397 * If the descriptor is cyclic and has a callback enable the
398 * descriptor stage interrupt in infinite repeat mode.
400 else if (desc->async_tx.callback)
401 chcr |= RCAR_DMACHCR_DPM_INFINITE | RCAR_DMACHCR_DSIE;
403 * Otherwise just select infinite repeat mode without any
404 * interrupt.
406 else
407 chcr |= RCAR_DMACHCR_DPM_INFINITE;
408 } else {
409 struct rcar_dmac_xfer_chunk *chunk = desc->running;
411 dev_dbg(chan->chan.device->dev,
412 "chan%u: queue chunk %p: %u@%pad -> %pad\n",
413 chan->index, chunk, chunk->size, &chunk->src_addr,
414 &chunk->dst_addr);
416 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
417 rcar_dmac_chan_write(chan, RCAR_DMAFIXSAR,
418 chunk->src_addr >> 32);
419 rcar_dmac_chan_write(chan, RCAR_DMAFIXDAR,
420 chunk->dst_addr >> 32);
421 #endif
422 rcar_dmac_chan_write(chan, RCAR_DMASAR,
423 chunk->src_addr & 0xffffffff);
424 rcar_dmac_chan_write(chan, RCAR_DMADAR,
425 chunk->dst_addr & 0xffffffff);
426 rcar_dmac_chan_write(chan, RCAR_DMATCR,
427 chunk->size >> desc->xfer_shift);
429 chcr |= RCAR_DMACHCR_DPM_DISABLED | RCAR_DMACHCR_IE;
432 rcar_dmac_chan_write(chan, RCAR_DMACHCR,
433 chcr | RCAR_DMACHCR_DE | RCAR_DMACHCR_CAIE);
436 static int rcar_dmac_init(struct rcar_dmac *dmac)
438 u16 dmaor;
440 /* Clear all channels and enable the DMAC globally. */
441 rcar_dmac_write(dmac, RCAR_DMACHCLR, GENMASK(dmac->n_channels - 1, 0));
442 rcar_dmac_write(dmac, RCAR_DMAOR,
443 RCAR_DMAOR_PRI_FIXED | RCAR_DMAOR_DME);
445 dmaor = rcar_dmac_read(dmac, RCAR_DMAOR);
446 if ((dmaor & (RCAR_DMAOR_AE | RCAR_DMAOR_DME)) != RCAR_DMAOR_DME) {
447 dev_warn(dmac->dev, "DMAOR initialization failed.\n");
448 return -EIO;
451 return 0;
454 /* -----------------------------------------------------------------------------
455 * Descriptors submission
458 static dma_cookie_t rcar_dmac_tx_submit(struct dma_async_tx_descriptor *tx)
460 struct rcar_dmac_chan *chan = to_rcar_dmac_chan(tx->chan);
461 struct rcar_dmac_desc *desc = to_rcar_dmac_desc(tx);
462 unsigned long flags;
463 dma_cookie_t cookie;
465 spin_lock_irqsave(&chan->lock, flags);
467 cookie = dma_cookie_assign(tx);
469 dev_dbg(chan->chan.device->dev, "chan%u: submit #%d@%p\n",
470 chan->index, tx->cookie, desc);
472 list_add_tail(&desc->node, &chan->desc.pending);
473 desc->running = list_first_entry(&desc->chunks,
474 struct rcar_dmac_xfer_chunk, node);
476 spin_unlock_irqrestore(&chan->lock, flags);
478 return cookie;
481 /* -----------------------------------------------------------------------------
482 * Descriptors allocation and free
486 * rcar_dmac_desc_alloc - Allocate a page worth of DMA descriptors
487 * @chan: the DMA channel
488 * @gfp: allocation flags
490 static int rcar_dmac_desc_alloc(struct rcar_dmac_chan *chan, gfp_t gfp)
492 struct rcar_dmac_desc_page *page;
493 unsigned long flags;
494 LIST_HEAD(list);
495 unsigned int i;
497 page = (void *)get_zeroed_page(gfp);
498 if (!page)
499 return -ENOMEM;
501 for (i = 0; i < RCAR_DMAC_DESCS_PER_PAGE; ++i) {
502 struct rcar_dmac_desc *desc = &page->descs[i];
504 dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan);
505 desc->async_tx.tx_submit = rcar_dmac_tx_submit;
506 INIT_LIST_HEAD(&desc->chunks);
508 list_add_tail(&desc->node, &list);
511 spin_lock_irqsave(&chan->lock, flags);
512 list_splice_tail(&list, &chan->desc.free);
513 list_add_tail(&page->node, &chan->desc.pages);
514 spin_unlock_irqrestore(&chan->lock, flags);
516 return 0;
520 * rcar_dmac_desc_put - Release a DMA transfer descriptor
521 * @chan: the DMA channel
522 * @desc: the descriptor
524 * Put the descriptor and its transfer chunk descriptors back in the channel's
525 * free descriptors lists. The descriptor's chunks list will be reinitialized to
526 * an empty list as a result.
528 * The descriptor must have been removed from the channel's lists before calling
529 * this function.
531 static void rcar_dmac_desc_put(struct rcar_dmac_chan *chan,
532 struct rcar_dmac_desc *desc)
534 unsigned long flags;
536 spin_lock_irqsave(&chan->lock, flags);
537 list_splice_tail_init(&desc->chunks, &chan->desc.chunks_free);
538 list_add(&desc->node, &chan->desc.free);
539 spin_unlock_irqrestore(&chan->lock, flags);
542 static void rcar_dmac_desc_recycle_acked(struct rcar_dmac_chan *chan)
544 struct rcar_dmac_desc *desc, *_desc;
545 unsigned long flags;
546 LIST_HEAD(list);
549 * We have to temporarily move all descriptors from the wait list to a
550 * local list as iterating over the wait list, even with
551 * list_for_each_entry_safe, isn't safe if we release the channel lock
552 * around the rcar_dmac_desc_put() call.
554 spin_lock_irqsave(&chan->lock, flags);
555 list_splice_init(&chan->desc.wait, &list);
556 spin_unlock_irqrestore(&chan->lock, flags);
558 list_for_each_entry_safe(desc, _desc, &list, node) {
559 if (async_tx_test_ack(&desc->async_tx)) {
560 list_del(&desc->node);
561 rcar_dmac_desc_put(chan, desc);
565 if (list_empty(&list))
566 return;
568 /* Put the remaining descriptors back in the wait list. */
569 spin_lock_irqsave(&chan->lock, flags);
570 list_splice(&list, &chan->desc.wait);
571 spin_unlock_irqrestore(&chan->lock, flags);
575 * rcar_dmac_desc_get - Allocate a descriptor for a DMA transfer
576 * @chan: the DMA channel
578 * Locking: This function must be called in a non-atomic context.
580 * Return: A pointer to the allocated descriptor or NULL if no descriptor can
581 * be allocated.
583 static struct rcar_dmac_desc *rcar_dmac_desc_get(struct rcar_dmac_chan *chan)
585 struct rcar_dmac_desc *desc;
586 unsigned long flags;
587 int ret;
589 /* Recycle acked descriptors before attempting allocation. */
590 rcar_dmac_desc_recycle_acked(chan);
592 spin_lock_irqsave(&chan->lock, flags);
594 while (list_empty(&chan->desc.free)) {
596 * No free descriptors, allocate a page worth of them and try
597 * again, as someone else could race us to get the newly
598 * allocated descriptors. If the allocation fails return an
599 * error.
601 spin_unlock_irqrestore(&chan->lock, flags);
602 ret = rcar_dmac_desc_alloc(chan, GFP_NOWAIT);
603 if (ret < 0)
604 return NULL;
605 spin_lock_irqsave(&chan->lock, flags);
608 desc = list_first_entry(&chan->desc.free, struct rcar_dmac_desc, node);
609 list_del(&desc->node);
611 spin_unlock_irqrestore(&chan->lock, flags);
613 return desc;
617 * rcar_dmac_xfer_chunk_alloc - Allocate a page worth of transfer chunks
618 * @chan: the DMA channel
619 * @gfp: allocation flags
621 static int rcar_dmac_xfer_chunk_alloc(struct rcar_dmac_chan *chan, gfp_t gfp)
623 struct rcar_dmac_desc_page *page;
624 unsigned long flags;
625 LIST_HEAD(list);
626 unsigned int i;
628 page = (void *)get_zeroed_page(gfp);
629 if (!page)
630 return -ENOMEM;
632 for (i = 0; i < RCAR_DMAC_XFER_CHUNKS_PER_PAGE; ++i) {
633 struct rcar_dmac_xfer_chunk *chunk = &page->chunks[i];
635 list_add_tail(&chunk->node, &list);
638 spin_lock_irqsave(&chan->lock, flags);
639 list_splice_tail(&list, &chan->desc.chunks_free);
640 list_add_tail(&page->node, &chan->desc.pages);
641 spin_unlock_irqrestore(&chan->lock, flags);
643 return 0;
647 * rcar_dmac_xfer_chunk_get - Allocate a transfer chunk for a DMA transfer
648 * @chan: the DMA channel
650 * Locking: This function must be called in a non-atomic context.
652 * Return: A pointer to the allocated transfer chunk descriptor or NULL if no
653 * descriptor can be allocated.
655 static struct rcar_dmac_xfer_chunk *
656 rcar_dmac_xfer_chunk_get(struct rcar_dmac_chan *chan)
658 struct rcar_dmac_xfer_chunk *chunk;
659 unsigned long flags;
660 int ret;
662 spin_lock_irqsave(&chan->lock, flags);
664 while (list_empty(&chan->desc.chunks_free)) {
666 * No free descriptors, allocate a page worth of them and try
667 * again, as someone else could race us to get the newly
668 * allocated descriptors. If the allocation fails return an
669 * error.
671 spin_unlock_irqrestore(&chan->lock, flags);
672 ret = rcar_dmac_xfer_chunk_alloc(chan, GFP_NOWAIT);
673 if (ret < 0)
674 return NULL;
675 spin_lock_irqsave(&chan->lock, flags);
678 chunk = list_first_entry(&chan->desc.chunks_free,
679 struct rcar_dmac_xfer_chunk, node);
680 list_del(&chunk->node);
682 spin_unlock_irqrestore(&chan->lock, flags);
684 return chunk;
687 static void rcar_dmac_realloc_hwdesc(struct rcar_dmac_chan *chan,
688 struct rcar_dmac_desc *desc, size_t size)
691 * dma_alloc_coherent() allocates memory in page size increments. To
692 * avoid reallocating the hardware descriptors when the allocated size
693 * wouldn't change align the requested size to a multiple of the page
694 * size.
696 size = PAGE_ALIGN(size);
698 if (desc->hwdescs.size == size)
699 return;
701 if (desc->hwdescs.mem) {
702 dma_free_coherent(chan->chan.device->dev, desc->hwdescs.size,
703 desc->hwdescs.mem, desc->hwdescs.dma);
704 desc->hwdescs.mem = NULL;
705 desc->hwdescs.size = 0;
708 if (!size)
709 return;
711 desc->hwdescs.mem = dma_alloc_coherent(chan->chan.device->dev, size,
712 &desc->hwdescs.dma, GFP_NOWAIT);
713 if (!desc->hwdescs.mem)
714 return;
716 desc->hwdescs.size = size;
719 static int rcar_dmac_fill_hwdesc(struct rcar_dmac_chan *chan,
720 struct rcar_dmac_desc *desc)
722 struct rcar_dmac_xfer_chunk *chunk;
723 struct rcar_dmac_hw_desc *hwdesc;
725 rcar_dmac_realloc_hwdesc(chan, desc, desc->nchunks * sizeof(*hwdesc));
727 hwdesc = desc->hwdescs.mem;
728 if (!hwdesc)
729 return -ENOMEM;
731 list_for_each_entry(chunk, &desc->chunks, node) {
732 hwdesc->sar = chunk->src_addr;
733 hwdesc->dar = chunk->dst_addr;
734 hwdesc->tcr = chunk->size >> desc->xfer_shift;
735 hwdesc++;
738 return 0;
741 /* -----------------------------------------------------------------------------
742 * Stop and reset
744 static void rcar_dmac_chcr_de_barrier(struct rcar_dmac_chan *chan)
746 u32 chcr;
747 unsigned int i;
750 * Ensure that the setting of the DE bit is actually 0 after
751 * clearing it.
753 for (i = 0; i < 1024; i++) {
754 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
755 if (!(chcr & RCAR_DMACHCR_DE))
756 return;
757 udelay(1);
760 dev_err(chan->chan.device->dev, "CHCR DE check error\n");
763 static void rcar_dmac_clear_chcr_de(struct rcar_dmac_chan *chan)
765 u32 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
767 /* set DE=0 and flush remaining data */
768 rcar_dmac_chan_write(chan, RCAR_DMACHCR, (chcr & ~RCAR_DMACHCR_DE));
770 /* make sure all remaining data was flushed */
771 rcar_dmac_chcr_de_barrier(chan);
774 static void rcar_dmac_chan_halt(struct rcar_dmac_chan *chan)
776 u32 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
778 chcr &= ~(RCAR_DMACHCR_DSE | RCAR_DMACHCR_DSIE | RCAR_DMACHCR_IE |
779 RCAR_DMACHCR_TE | RCAR_DMACHCR_DE |
780 RCAR_DMACHCR_CAE | RCAR_DMACHCR_CAIE);
781 rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr);
782 rcar_dmac_chcr_de_barrier(chan);
785 static void rcar_dmac_chan_reinit(struct rcar_dmac_chan *chan)
787 struct rcar_dmac_desc *desc, *_desc;
788 unsigned long flags;
789 LIST_HEAD(descs);
791 spin_lock_irqsave(&chan->lock, flags);
793 /* Move all non-free descriptors to the local lists. */
794 list_splice_init(&chan->desc.pending, &descs);
795 list_splice_init(&chan->desc.active, &descs);
796 list_splice_init(&chan->desc.done, &descs);
797 list_splice_init(&chan->desc.wait, &descs);
799 chan->desc.running = NULL;
801 spin_unlock_irqrestore(&chan->lock, flags);
803 list_for_each_entry_safe(desc, _desc, &descs, node) {
804 list_del(&desc->node);
805 rcar_dmac_desc_put(chan, desc);
809 static void rcar_dmac_stop_all_chan(struct rcar_dmac *dmac)
811 unsigned int i;
813 /* Stop all channels. */
814 for (i = 0; i < dmac->n_channels; ++i) {
815 struct rcar_dmac_chan *chan = &dmac->channels[i];
817 /* Stop and reinitialize the channel. */
818 spin_lock_irq(&chan->lock);
819 rcar_dmac_chan_halt(chan);
820 spin_unlock_irq(&chan->lock);
824 static int rcar_dmac_chan_pause(struct dma_chan *chan)
826 unsigned long flags;
827 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
829 spin_lock_irqsave(&rchan->lock, flags);
830 rcar_dmac_clear_chcr_de(rchan);
831 spin_unlock_irqrestore(&rchan->lock, flags);
833 return 0;
836 /* -----------------------------------------------------------------------------
837 * Descriptors preparation
840 static void rcar_dmac_chan_configure_desc(struct rcar_dmac_chan *chan,
841 struct rcar_dmac_desc *desc)
843 static const u32 chcr_ts[] = {
844 RCAR_DMACHCR_TS_1B, RCAR_DMACHCR_TS_2B,
845 RCAR_DMACHCR_TS_4B, RCAR_DMACHCR_TS_8B,
846 RCAR_DMACHCR_TS_16B, RCAR_DMACHCR_TS_32B,
847 RCAR_DMACHCR_TS_64B,
850 unsigned int xfer_size;
851 u32 chcr;
853 switch (desc->direction) {
854 case DMA_DEV_TO_MEM:
855 chcr = RCAR_DMACHCR_DM_INC | RCAR_DMACHCR_SM_FIXED
856 | RCAR_DMACHCR_RS_DMARS;
857 xfer_size = chan->src.xfer_size;
858 break;
860 case DMA_MEM_TO_DEV:
861 chcr = RCAR_DMACHCR_DM_FIXED | RCAR_DMACHCR_SM_INC
862 | RCAR_DMACHCR_RS_DMARS;
863 xfer_size = chan->dst.xfer_size;
864 break;
866 case DMA_MEM_TO_MEM:
867 default:
868 chcr = RCAR_DMACHCR_DM_INC | RCAR_DMACHCR_SM_INC
869 | RCAR_DMACHCR_RS_AUTO;
870 xfer_size = RCAR_DMAC_MEMCPY_XFER_SIZE;
871 break;
874 desc->xfer_shift = ilog2(xfer_size);
875 desc->chcr = chcr | chcr_ts[desc->xfer_shift];
879 * rcar_dmac_chan_prep_sg - prepare transfer descriptors from an SG list
881 * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
882 * converted to scatter-gather to guarantee consistent locking and a correct
883 * list manipulation. For slave DMA direction carries the usual meaning, and,
884 * logically, the SG list is RAM and the addr variable contains slave address,
885 * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_MEM_TO_MEM
886 * and the SG list contains only one element and points at the source buffer.
888 static struct dma_async_tx_descriptor *
889 rcar_dmac_chan_prep_sg(struct rcar_dmac_chan *chan, struct scatterlist *sgl,
890 unsigned int sg_len, dma_addr_t dev_addr,
891 enum dma_transfer_direction dir, unsigned long dma_flags,
892 bool cyclic)
894 struct rcar_dmac_xfer_chunk *chunk;
895 struct rcar_dmac_desc *desc;
896 struct scatterlist *sg;
897 unsigned int nchunks = 0;
898 unsigned int max_chunk_size;
899 unsigned int full_size = 0;
900 bool cross_boundary = false;
901 unsigned int i;
902 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
903 u32 high_dev_addr;
904 u32 high_mem_addr;
905 #endif
907 desc = rcar_dmac_desc_get(chan);
908 if (!desc)
909 return NULL;
911 desc->async_tx.flags = dma_flags;
912 desc->async_tx.cookie = -EBUSY;
914 desc->cyclic = cyclic;
915 desc->direction = dir;
917 rcar_dmac_chan_configure_desc(chan, desc);
919 max_chunk_size = RCAR_DMATCR_MASK << desc->xfer_shift;
922 * Allocate and fill the transfer chunk descriptors. We own the only
923 * reference to the DMA descriptor, there's no need for locking.
925 for_each_sg(sgl, sg, sg_len, i) {
926 dma_addr_t mem_addr = sg_dma_address(sg);
927 unsigned int len = sg_dma_len(sg);
929 full_size += len;
931 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
932 if (i == 0) {
933 high_dev_addr = dev_addr >> 32;
934 high_mem_addr = mem_addr >> 32;
937 if ((dev_addr >> 32 != high_dev_addr) ||
938 (mem_addr >> 32 != high_mem_addr))
939 cross_boundary = true;
940 #endif
941 while (len) {
942 unsigned int size = min(len, max_chunk_size);
944 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
946 * Prevent individual transfers from crossing 4GB
947 * boundaries.
949 if (dev_addr >> 32 != (dev_addr + size - 1) >> 32) {
950 size = ALIGN(dev_addr, 1ULL << 32) - dev_addr;
951 cross_boundary = true;
953 if (mem_addr >> 32 != (mem_addr + size - 1) >> 32) {
954 size = ALIGN(mem_addr, 1ULL << 32) - mem_addr;
955 cross_boundary = true;
957 #endif
959 chunk = rcar_dmac_xfer_chunk_get(chan);
960 if (!chunk) {
961 rcar_dmac_desc_put(chan, desc);
962 return NULL;
965 if (dir == DMA_DEV_TO_MEM) {
966 chunk->src_addr = dev_addr;
967 chunk->dst_addr = mem_addr;
968 } else {
969 chunk->src_addr = mem_addr;
970 chunk->dst_addr = dev_addr;
973 chunk->size = size;
975 dev_dbg(chan->chan.device->dev,
976 "chan%u: chunk %p/%p sgl %u@%p, %u/%u %pad -> %pad\n",
977 chan->index, chunk, desc, i, sg, size, len,
978 &chunk->src_addr, &chunk->dst_addr);
980 mem_addr += size;
981 if (dir == DMA_MEM_TO_MEM)
982 dev_addr += size;
984 len -= size;
986 list_add_tail(&chunk->node, &desc->chunks);
987 nchunks++;
991 desc->nchunks = nchunks;
992 desc->size = full_size;
995 * Use hardware descriptor lists if possible when more than one chunk
996 * needs to be transferred (otherwise they don't make much sense).
998 * Source/Destination address should be located in same 4GiB region
999 * in the 40bit address space when it uses Hardware descriptor,
1000 * and cross_boundary is checking it.
1002 desc->hwdescs.use = !cross_boundary && nchunks > 1;
1003 if (desc->hwdescs.use) {
1004 if (rcar_dmac_fill_hwdesc(chan, desc) < 0)
1005 desc->hwdescs.use = false;
1008 return &desc->async_tx;
1011 /* -----------------------------------------------------------------------------
1012 * DMA engine operations
1015 static int rcar_dmac_alloc_chan_resources(struct dma_chan *chan)
1017 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1018 int ret;
1020 INIT_LIST_HEAD(&rchan->desc.chunks_free);
1021 INIT_LIST_HEAD(&rchan->desc.pages);
1023 /* Preallocate descriptors. */
1024 ret = rcar_dmac_xfer_chunk_alloc(rchan, GFP_KERNEL);
1025 if (ret < 0)
1026 return -ENOMEM;
1028 ret = rcar_dmac_desc_alloc(rchan, GFP_KERNEL);
1029 if (ret < 0)
1030 return -ENOMEM;
1032 return pm_runtime_get_sync(chan->device->dev);
1035 static void rcar_dmac_free_chan_resources(struct dma_chan *chan)
1037 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1038 struct rcar_dmac *dmac = to_rcar_dmac(chan->device);
1039 struct rcar_dmac_chan_map *map = &rchan->map;
1040 struct rcar_dmac_desc_page *page, *_page;
1041 struct rcar_dmac_desc *desc;
1042 LIST_HEAD(list);
1044 /* Protect against ISR */
1045 spin_lock_irq(&rchan->lock);
1046 rcar_dmac_chan_halt(rchan);
1047 spin_unlock_irq(&rchan->lock);
1050 * Now no new interrupts will occur, but one might already be
1051 * running. Wait for it to finish before freeing resources.
1053 synchronize_irq(rchan->irq);
1055 if (rchan->mid_rid >= 0) {
1056 /* The caller is holding dma_list_mutex */
1057 clear_bit(rchan->mid_rid, dmac->modules);
1058 rchan->mid_rid = -EINVAL;
1061 list_splice_init(&rchan->desc.free, &list);
1062 list_splice_init(&rchan->desc.pending, &list);
1063 list_splice_init(&rchan->desc.active, &list);
1064 list_splice_init(&rchan->desc.done, &list);
1065 list_splice_init(&rchan->desc.wait, &list);
1067 rchan->desc.running = NULL;
1069 list_for_each_entry(desc, &list, node)
1070 rcar_dmac_realloc_hwdesc(rchan, desc, 0);
1072 list_for_each_entry_safe(page, _page, &rchan->desc.pages, node) {
1073 list_del(&page->node);
1074 free_page((unsigned long)page);
1077 /* Remove slave mapping if present. */
1078 if (map->slave.xfer_size) {
1079 dma_unmap_resource(chan->device->dev, map->addr,
1080 map->slave.xfer_size, map->dir, 0);
1081 map->slave.xfer_size = 0;
1084 pm_runtime_put(chan->device->dev);
1087 static struct dma_async_tx_descriptor *
1088 rcar_dmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
1089 dma_addr_t dma_src, size_t len, unsigned long flags)
1091 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1092 struct scatterlist sgl;
1094 if (!len)
1095 return NULL;
1097 sg_init_table(&sgl, 1);
1098 sg_set_page(&sgl, pfn_to_page(PFN_DOWN(dma_src)), len,
1099 offset_in_page(dma_src));
1100 sg_dma_address(&sgl) = dma_src;
1101 sg_dma_len(&sgl) = len;
1103 return rcar_dmac_chan_prep_sg(rchan, &sgl, 1, dma_dest,
1104 DMA_MEM_TO_MEM, flags, false);
1107 static int rcar_dmac_map_slave_addr(struct dma_chan *chan,
1108 enum dma_transfer_direction dir)
1110 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1111 struct rcar_dmac_chan_map *map = &rchan->map;
1112 phys_addr_t dev_addr;
1113 size_t dev_size;
1114 enum dma_data_direction dev_dir;
1116 if (dir == DMA_DEV_TO_MEM) {
1117 dev_addr = rchan->src.slave_addr;
1118 dev_size = rchan->src.xfer_size;
1119 dev_dir = DMA_TO_DEVICE;
1120 } else {
1121 dev_addr = rchan->dst.slave_addr;
1122 dev_size = rchan->dst.xfer_size;
1123 dev_dir = DMA_FROM_DEVICE;
1126 /* Reuse current map if possible. */
1127 if (dev_addr == map->slave.slave_addr &&
1128 dev_size == map->slave.xfer_size &&
1129 dev_dir == map->dir)
1130 return 0;
1132 /* Remove old mapping if present. */
1133 if (map->slave.xfer_size)
1134 dma_unmap_resource(chan->device->dev, map->addr,
1135 map->slave.xfer_size, map->dir, 0);
1136 map->slave.xfer_size = 0;
1138 /* Create new slave address map. */
1139 map->addr = dma_map_resource(chan->device->dev, dev_addr, dev_size,
1140 dev_dir, 0);
1142 if (dma_mapping_error(chan->device->dev, map->addr)) {
1143 dev_err(chan->device->dev,
1144 "chan%u: failed to map %zx@%pap", rchan->index,
1145 dev_size, &dev_addr);
1146 return -EIO;
1149 dev_dbg(chan->device->dev, "chan%u: map %zx@%pap to %pad dir: %s\n",
1150 rchan->index, dev_size, &dev_addr, &map->addr,
1151 dev_dir == DMA_TO_DEVICE ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE");
1153 map->slave.slave_addr = dev_addr;
1154 map->slave.xfer_size = dev_size;
1155 map->dir = dev_dir;
1157 return 0;
1160 static struct dma_async_tx_descriptor *
1161 rcar_dmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
1162 unsigned int sg_len, enum dma_transfer_direction dir,
1163 unsigned long flags, void *context)
1165 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1167 /* Someone calling slave DMA on a generic channel? */
1168 if (rchan->mid_rid < 0 || !sg_len || !sg_dma_len(sgl)) {
1169 dev_warn(chan->device->dev,
1170 "%s: bad parameter: len=%d, id=%d\n",
1171 __func__, sg_len, rchan->mid_rid);
1172 return NULL;
1175 if (rcar_dmac_map_slave_addr(chan, dir))
1176 return NULL;
1178 return rcar_dmac_chan_prep_sg(rchan, sgl, sg_len, rchan->map.addr,
1179 dir, flags, false);
1182 #define RCAR_DMAC_MAX_SG_LEN 32
1184 static struct dma_async_tx_descriptor *
1185 rcar_dmac_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
1186 size_t buf_len, size_t period_len,
1187 enum dma_transfer_direction dir, unsigned long flags)
1189 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1190 struct dma_async_tx_descriptor *desc;
1191 struct scatterlist *sgl;
1192 unsigned int sg_len;
1193 unsigned int i;
1195 /* Someone calling slave DMA on a generic channel? */
1196 if (rchan->mid_rid < 0 || buf_len < period_len) {
1197 dev_warn(chan->device->dev,
1198 "%s: bad parameter: buf_len=%zu, period_len=%zu, id=%d\n",
1199 __func__, buf_len, period_len, rchan->mid_rid);
1200 return NULL;
1203 if (rcar_dmac_map_slave_addr(chan, dir))
1204 return NULL;
1206 sg_len = buf_len / period_len;
1207 if (sg_len > RCAR_DMAC_MAX_SG_LEN) {
1208 dev_err(chan->device->dev,
1209 "chan%u: sg length %d exceds limit %d",
1210 rchan->index, sg_len, RCAR_DMAC_MAX_SG_LEN);
1211 return NULL;
1215 * Allocate the sg list dynamically as it would consume too much stack
1216 * space.
1218 sgl = kcalloc(sg_len, sizeof(*sgl), GFP_NOWAIT);
1219 if (!sgl)
1220 return NULL;
1222 sg_init_table(sgl, sg_len);
1224 for (i = 0; i < sg_len; ++i) {
1225 dma_addr_t src = buf_addr + (period_len * i);
1227 sg_set_page(&sgl[i], pfn_to_page(PFN_DOWN(src)), period_len,
1228 offset_in_page(src));
1229 sg_dma_address(&sgl[i]) = src;
1230 sg_dma_len(&sgl[i]) = period_len;
1233 desc = rcar_dmac_chan_prep_sg(rchan, sgl, sg_len, rchan->map.addr,
1234 dir, flags, true);
1236 kfree(sgl);
1237 return desc;
1240 static int rcar_dmac_device_config(struct dma_chan *chan,
1241 struct dma_slave_config *cfg)
1243 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1246 * We could lock this, but you shouldn't be configuring the
1247 * channel, while using it...
1249 rchan->src.slave_addr = cfg->src_addr;
1250 rchan->dst.slave_addr = cfg->dst_addr;
1251 rchan->src.xfer_size = cfg->src_addr_width;
1252 rchan->dst.xfer_size = cfg->dst_addr_width;
1254 return 0;
1257 static int rcar_dmac_chan_terminate_all(struct dma_chan *chan)
1259 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1260 unsigned long flags;
1262 spin_lock_irqsave(&rchan->lock, flags);
1263 rcar_dmac_chan_halt(rchan);
1264 spin_unlock_irqrestore(&rchan->lock, flags);
1267 * FIXME: No new interrupt can occur now, but the IRQ thread might still
1268 * be running.
1271 rcar_dmac_chan_reinit(rchan);
1273 return 0;
1276 static unsigned int rcar_dmac_chan_get_residue(struct rcar_dmac_chan *chan,
1277 dma_cookie_t cookie)
1279 struct rcar_dmac_desc *desc = chan->desc.running;
1280 struct rcar_dmac_xfer_chunk *running = NULL;
1281 struct rcar_dmac_xfer_chunk *chunk;
1282 enum dma_status status;
1283 unsigned int residue = 0;
1284 unsigned int dptr = 0;
1285 unsigned int chcrb;
1286 unsigned int tcrb;
1287 unsigned int i;
1289 if (!desc)
1290 return 0;
1293 * If the cookie corresponds to a descriptor that has been completed
1294 * there is no residue. The same check has already been performed by the
1295 * caller but without holding the channel lock, so the descriptor could
1296 * now be complete.
1298 status = dma_cookie_status(&chan->chan, cookie, NULL);
1299 if (status == DMA_COMPLETE)
1300 return 0;
1303 * If the cookie doesn't correspond to the currently running transfer
1304 * then the descriptor hasn't been processed yet, and the residue is
1305 * equal to the full descriptor size.
1306 * Also, a client driver is possible to call this function before
1307 * rcar_dmac_isr_channel_thread() runs. In this case, the "desc.running"
1308 * will be the next descriptor, and the done list will appear. So, if
1309 * the argument cookie matches the done list's cookie, we can assume
1310 * the residue is zero.
1312 if (cookie != desc->async_tx.cookie) {
1313 list_for_each_entry(desc, &chan->desc.done, node) {
1314 if (cookie == desc->async_tx.cookie)
1315 return 0;
1317 list_for_each_entry(desc, &chan->desc.pending, node) {
1318 if (cookie == desc->async_tx.cookie)
1319 return desc->size;
1321 list_for_each_entry(desc, &chan->desc.active, node) {
1322 if (cookie == desc->async_tx.cookie)
1323 return desc->size;
1327 * No descriptor found for the cookie, there's thus no residue.
1328 * This shouldn't happen if the calling driver passes a correct
1329 * cookie value.
1331 WARN(1, "No descriptor for cookie!");
1332 return 0;
1336 * We need to read two registers.
1337 * Make sure the control register does not skip to next chunk
1338 * while reading the counter.
1339 * Trying it 3 times should be enough: Initial read, retry, retry
1340 * for the paranoid.
1342 for (i = 0; i < 3; i++) {
1343 chcrb = rcar_dmac_chan_read(chan, RCAR_DMACHCRB) &
1344 RCAR_DMACHCRB_DPTR_MASK;
1345 tcrb = rcar_dmac_chan_read(chan, RCAR_DMATCRB);
1346 /* Still the same? */
1347 if (chcrb == (rcar_dmac_chan_read(chan, RCAR_DMACHCRB) &
1348 RCAR_DMACHCRB_DPTR_MASK))
1349 break;
1351 WARN_ONCE(i >= 3, "residue might be not continuous!");
1354 * In descriptor mode the descriptor running pointer is not maintained
1355 * by the interrupt handler, find the running descriptor from the
1356 * descriptor pointer field in the CHCRB register. In non-descriptor
1357 * mode just use the running descriptor pointer.
1359 if (desc->hwdescs.use) {
1360 dptr = chcrb >> RCAR_DMACHCRB_DPTR_SHIFT;
1361 if (dptr == 0)
1362 dptr = desc->nchunks;
1363 dptr--;
1364 WARN_ON(dptr >= desc->nchunks);
1365 } else {
1366 running = desc->running;
1369 /* Compute the size of all chunks still to be transferred. */
1370 list_for_each_entry_reverse(chunk, &desc->chunks, node) {
1371 if (chunk == running || ++dptr == desc->nchunks)
1372 break;
1374 residue += chunk->size;
1377 /* Add the residue for the current chunk. */
1378 residue += tcrb << desc->xfer_shift;
1380 return residue;
1383 static enum dma_status rcar_dmac_tx_status(struct dma_chan *chan,
1384 dma_cookie_t cookie,
1385 struct dma_tx_state *txstate)
1387 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1388 enum dma_status status;
1389 unsigned long flags;
1390 unsigned int residue;
1391 bool cyclic;
1393 status = dma_cookie_status(chan, cookie, txstate);
1394 if (status == DMA_COMPLETE || !txstate)
1395 return status;
1397 spin_lock_irqsave(&rchan->lock, flags);
1398 residue = rcar_dmac_chan_get_residue(rchan, cookie);
1399 cyclic = rchan->desc.running ? rchan->desc.running->cyclic : false;
1400 spin_unlock_irqrestore(&rchan->lock, flags);
1402 /* if there's no residue, the cookie is complete */
1403 if (!residue && !cyclic)
1404 return DMA_COMPLETE;
1406 dma_set_residue(txstate, residue);
1408 return status;
1411 static void rcar_dmac_issue_pending(struct dma_chan *chan)
1413 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1414 unsigned long flags;
1416 spin_lock_irqsave(&rchan->lock, flags);
1418 if (list_empty(&rchan->desc.pending))
1419 goto done;
1421 /* Append the pending list to the active list. */
1422 list_splice_tail_init(&rchan->desc.pending, &rchan->desc.active);
1425 * If no transfer is running pick the first descriptor from the active
1426 * list and start the transfer.
1428 if (!rchan->desc.running) {
1429 struct rcar_dmac_desc *desc;
1431 desc = list_first_entry(&rchan->desc.active,
1432 struct rcar_dmac_desc, node);
1433 rchan->desc.running = desc;
1435 rcar_dmac_chan_start_xfer(rchan);
1438 done:
1439 spin_unlock_irqrestore(&rchan->lock, flags);
1442 static void rcar_dmac_device_synchronize(struct dma_chan *chan)
1444 struct rcar_dmac_chan *rchan = to_rcar_dmac_chan(chan);
1446 synchronize_irq(rchan->irq);
1449 /* -----------------------------------------------------------------------------
1450 * IRQ handling
1453 static irqreturn_t rcar_dmac_isr_desc_stage_end(struct rcar_dmac_chan *chan)
1455 struct rcar_dmac_desc *desc = chan->desc.running;
1456 unsigned int stage;
1458 if (WARN_ON(!desc || !desc->cyclic)) {
1460 * This should never happen, there should always be a running
1461 * cyclic descriptor when a descriptor stage end interrupt is
1462 * triggered. Warn and return.
1464 return IRQ_NONE;
1467 /* Program the interrupt pointer to the next stage. */
1468 stage = (rcar_dmac_chan_read(chan, RCAR_DMACHCRB) &
1469 RCAR_DMACHCRB_DPTR_MASK) >> RCAR_DMACHCRB_DPTR_SHIFT;
1470 rcar_dmac_chan_write(chan, RCAR_DMADPCR, RCAR_DMADPCR_DIPT(stage));
1472 return IRQ_WAKE_THREAD;
1475 static irqreturn_t rcar_dmac_isr_transfer_end(struct rcar_dmac_chan *chan)
1477 struct rcar_dmac_desc *desc = chan->desc.running;
1478 irqreturn_t ret = IRQ_WAKE_THREAD;
1480 if (WARN_ON_ONCE(!desc)) {
1482 * This should never happen, there should always be a running
1483 * descriptor when a transfer end interrupt is triggered. Warn
1484 * and return.
1486 return IRQ_NONE;
1490 * The transfer end interrupt isn't generated for each chunk when using
1491 * descriptor mode. Only update the running chunk pointer in
1492 * non-descriptor mode.
1494 if (!desc->hwdescs.use) {
1496 * If we haven't completed the last transfer chunk simply move
1497 * to the next one. Only wake the IRQ thread if the transfer is
1498 * cyclic.
1500 if (!list_is_last(&desc->running->node, &desc->chunks)) {
1501 desc->running = list_next_entry(desc->running, node);
1502 if (!desc->cyclic)
1503 ret = IRQ_HANDLED;
1504 goto done;
1508 * We've completed the last transfer chunk. If the transfer is
1509 * cyclic, move back to the first one.
1511 if (desc->cyclic) {
1512 desc->running =
1513 list_first_entry(&desc->chunks,
1514 struct rcar_dmac_xfer_chunk,
1515 node);
1516 goto done;
1520 /* The descriptor is complete, move it to the done list. */
1521 list_move_tail(&desc->node, &chan->desc.done);
1523 /* Queue the next descriptor, if any. */
1524 if (!list_empty(&chan->desc.active))
1525 chan->desc.running = list_first_entry(&chan->desc.active,
1526 struct rcar_dmac_desc,
1527 node);
1528 else
1529 chan->desc.running = NULL;
1531 done:
1532 if (chan->desc.running)
1533 rcar_dmac_chan_start_xfer(chan);
1535 return ret;
1538 static irqreturn_t rcar_dmac_isr_channel(int irq, void *dev)
1540 u32 mask = RCAR_DMACHCR_DSE | RCAR_DMACHCR_TE;
1541 struct rcar_dmac_chan *chan = dev;
1542 irqreturn_t ret = IRQ_NONE;
1543 bool reinit = false;
1544 u32 chcr;
1546 spin_lock(&chan->lock);
1548 chcr = rcar_dmac_chan_read(chan, RCAR_DMACHCR);
1549 if (chcr & RCAR_DMACHCR_CAE) {
1550 struct rcar_dmac *dmac = to_rcar_dmac(chan->chan.device);
1553 * We don't need to call rcar_dmac_chan_halt()
1554 * because channel is already stopped in error case.
1555 * We need to clear register and check DE bit as recovery.
1557 rcar_dmac_write(dmac, RCAR_DMACHCLR, 1 << chan->index);
1558 rcar_dmac_chcr_de_barrier(chan);
1559 reinit = true;
1560 goto spin_lock_end;
1563 if (chcr & RCAR_DMACHCR_TE)
1564 mask |= RCAR_DMACHCR_DE;
1565 rcar_dmac_chan_write(chan, RCAR_DMACHCR, chcr & ~mask);
1566 if (mask & RCAR_DMACHCR_DE)
1567 rcar_dmac_chcr_de_barrier(chan);
1569 if (chcr & RCAR_DMACHCR_DSE)
1570 ret |= rcar_dmac_isr_desc_stage_end(chan);
1572 if (chcr & RCAR_DMACHCR_TE)
1573 ret |= rcar_dmac_isr_transfer_end(chan);
1575 spin_lock_end:
1576 spin_unlock(&chan->lock);
1578 if (reinit) {
1579 dev_err(chan->chan.device->dev, "Channel Address Error\n");
1581 rcar_dmac_chan_reinit(chan);
1582 ret = IRQ_HANDLED;
1585 return ret;
1588 static irqreturn_t rcar_dmac_isr_channel_thread(int irq, void *dev)
1590 struct rcar_dmac_chan *chan = dev;
1591 struct rcar_dmac_desc *desc;
1592 struct dmaengine_desc_callback cb;
1594 spin_lock_irq(&chan->lock);
1596 /* For cyclic transfers notify the user after every chunk. */
1597 if (chan->desc.running && chan->desc.running->cyclic) {
1598 desc = chan->desc.running;
1599 dmaengine_desc_get_callback(&desc->async_tx, &cb);
1601 if (dmaengine_desc_callback_valid(&cb)) {
1602 spin_unlock_irq(&chan->lock);
1603 dmaengine_desc_callback_invoke(&cb, NULL);
1604 spin_lock_irq(&chan->lock);
1609 * Call the callback function for all descriptors on the done list and
1610 * move them to the ack wait list.
1612 while (!list_empty(&chan->desc.done)) {
1613 desc = list_first_entry(&chan->desc.done, struct rcar_dmac_desc,
1614 node);
1615 dma_cookie_complete(&desc->async_tx);
1616 list_del(&desc->node);
1618 dmaengine_desc_get_callback(&desc->async_tx, &cb);
1619 if (dmaengine_desc_callback_valid(&cb)) {
1620 spin_unlock_irq(&chan->lock);
1622 * We own the only reference to this descriptor, we can
1623 * safely dereference it without holding the channel
1624 * lock.
1626 dmaengine_desc_callback_invoke(&cb, NULL);
1627 spin_lock_irq(&chan->lock);
1630 list_add_tail(&desc->node, &chan->desc.wait);
1633 spin_unlock_irq(&chan->lock);
1635 /* Recycle all acked descriptors. */
1636 rcar_dmac_desc_recycle_acked(chan);
1638 return IRQ_HANDLED;
1641 /* -----------------------------------------------------------------------------
1642 * OF xlate and channel filter
1645 static bool rcar_dmac_chan_filter(struct dma_chan *chan, void *arg)
1647 struct rcar_dmac *dmac = to_rcar_dmac(chan->device);
1648 struct of_phandle_args *dma_spec = arg;
1651 * FIXME: Using a filter on OF platforms is a nonsense. The OF xlate
1652 * function knows from which device it wants to allocate a channel from,
1653 * and would be perfectly capable of selecting the channel it wants.
1654 * Forcing it to call dma_request_channel() and iterate through all
1655 * channels from all controllers is just pointless.
1657 if (chan->device->device_config != rcar_dmac_device_config ||
1658 dma_spec->np != chan->device->dev->of_node)
1659 return false;
1661 return !test_and_set_bit(dma_spec->args[0], dmac->modules);
1664 static struct dma_chan *rcar_dmac_of_xlate(struct of_phandle_args *dma_spec,
1665 struct of_dma *ofdma)
1667 struct rcar_dmac_chan *rchan;
1668 struct dma_chan *chan;
1669 dma_cap_mask_t mask;
1671 if (dma_spec->args_count != 1)
1672 return NULL;
1674 /* Only slave DMA channels can be allocated via DT */
1675 dma_cap_zero(mask);
1676 dma_cap_set(DMA_SLAVE, mask);
1678 chan = dma_request_channel(mask, rcar_dmac_chan_filter, dma_spec);
1679 if (!chan)
1680 return NULL;
1682 rchan = to_rcar_dmac_chan(chan);
1683 rchan->mid_rid = dma_spec->args[0];
1685 return chan;
1688 /* -----------------------------------------------------------------------------
1689 * Power management
1692 #ifdef CONFIG_PM
1693 static int rcar_dmac_runtime_suspend(struct device *dev)
1695 return 0;
1698 static int rcar_dmac_runtime_resume(struct device *dev)
1700 struct rcar_dmac *dmac = dev_get_drvdata(dev);
1702 return rcar_dmac_init(dmac);
1704 #endif
1706 static const struct dev_pm_ops rcar_dmac_pm = {
1708 * TODO for system sleep/resume:
1709 * - Wait for the current transfer to complete and stop the device,
1710 * - Resume transfers, if any.
1712 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1713 pm_runtime_force_resume)
1714 SET_RUNTIME_PM_OPS(rcar_dmac_runtime_suspend, rcar_dmac_runtime_resume,
1715 NULL)
1718 /* -----------------------------------------------------------------------------
1719 * Probe and remove
1722 static int rcar_dmac_chan_probe(struct rcar_dmac *dmac,
1723 struct rcar_dmac_chan *rchan,
1724 unsigned int index)
1726 struct platform_device *pdev = to_platform_device(dmac->dev);
1727 struct dma_chan *chan = &rchan->chan;
1728 char pdev_irqname[5];
1729 char *irqname;
1730 int ret;
1732 rchan->index = index;
1733 rchan->iomem = dmac->iomem + RCAR_DMAC_CHAN_OFFSET(index);
1734 rchan->mid_rid = -EINVAL;
1736 spin_lock_init(&rchan->lock);
1738 INIT_LIST_HEAD(&rchan->desc.free);
1739 INIT_LIST_HEAD(&rchan->desc.pending);
1740 INIT_LIST_HEAD(&rchan->desc.active);
1741 INIT_LIST_HEAD(&rchan->desc.done);
1742 INIT_LIST_HEAD(&rchan->desc.wait);
1744 /* Request the channel interrupt. */
1745 sprintf(pdev_irqname, "ch%u", index);
1746 rchan->irq = platform_get_irq_byname(pdev, pdev_irqname);
1747 if (rchan->irq < 0) {
1748 dev_err(dmac->dev, "no IRQ specified for channel %u\n", index);
1749 return -ENODEV;
1752 irqname = devm_kasprintf(dmac->dev, GFP_KERNEL, "%s:%u",
1753 dev_name(dmac->dev), index);
1754 if (!irqname)
1755 return -ENOMEM;
1758 * Initialize the DMA engine channel and add it to the DMA engine
1759 * channels list.
1761 chan->device = &dmac->engine;
1762 dma_cookie_init(chan);
1764 list_add_tail(&chan->device_node, &dmac->engine.channels);
1766 ret = devm_request_threaded_irq(dmac->dev, rchan->irq,
1767 rcar_dmac_isr_channel,
1768 rcar_dmac_isr_channel_thread, 0,
1769 irqname, rchan);
1770 if (ret) {
1771 dev_err(dmac->dev, "failed to request IRQ %u (%d)\n",
1772 rchan->irq, ret);
1773 return ret;
1776 return 0;
1779 static int rcar_dmac_parse_of(struct device *dev, struct rcar_dmac *dmac)
1781 struct device_node *np = dev->of_node;
1782 int ret;
1784 ret = of_property_read_u32(np, "dma-channels", &dmac->n_channels);
1785 if (ret < 0) {
1786 dev_err(dev, "unable to read dma-channels property\n");
1787 return ret;
1790 if (dmac->n_channels <= 0 || dmac->n_channels >= 100) {
1791 dev_err(dev, "invalid number of channels %u\n",
1792 dmac->n_channels);
1793 return -EINVAL;
1796 return 0;
1799 static int rcar_dmac_probe(struct platform_device *pdev)
1801 const enum dma_slave_buswidth widths = DMA_SLAVE_BUSWIDTH_1_BYTE |
1802 DMA_SLAVE_BUSWIDTH_2_BYTES | DMA_SLAVE_BUSWIDTH_4_BYTES |
1803 DMA_SLAVE_BUSWIDTH_8_BYTES | DMA_SLAVE_BUSWIDTH_16_BYTES |
1804 DMA_SLAVE_BUSWIDTH_32_BYTES | DMA_SLAVE_BUSWIDTH_64_BYTES;
1805 unsigned int channels_offset = 0;
1806 struct dma_device *engine;
1807 struct rcar_dmac *dmac;
1808 struct resource *mem;
1809 unsigned int i;
1810 int ret;
1812 dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL);
1813 if (!dmac)
1814 return -ENOMEM;
1816 dmac->dev = &pdev->dev;
1817 platform_set_drvdata(pdev, dmac);
1818 dmac->dev->dma_parms = &dmac->parms;
1819 dma_set_max_seg_size(dmac->dev, RCAR_DMATCR_MASK);
1820 dma_set_mask_and_coherent(dmac->dev, DMA_BIT_MASK(40));
1822 ret = rcar_dmac_parse_of(&pdev->dev, dmac);
1823 if (ret < 0)
1824 return ret;
1827 * A still unconfirmed hardware bug prevents the IPMMU microTLB 0 to be
1828 * flushed correctly, resulting in memory corruption. DMAC 0 channel 0
1829 * is connected to microTLB 0 on currently supported platforms, so we
1830 * can't use it with the IPMMU. As the IOMMU API operates at the device
1831 * level we can't disable it selectively, so ignore channel 0 for now if
1832 * the device is part of an IOMMU group.
1834 if (pdev->dev.iommu_group) {
1835 dmac->n_channels--;
1836 channels_offset = 1;
1839 dmac->channels = devm_kcalloc(&pdev->dev, dmac->n_channels,
1840 sizeof(*dmac->channels), GFP_KERNEL);
1841 if (!dmac->channels)
1842 return -ENOMEM;
1844 /* Request resources. */
1845 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1846 dmac->iomem = devm_ioremap_resource(&pdev->dev, mem);
1847 if (IS_ERR(dmac->iomem))
1848 return PTR_ERR(dmac->iomem);
1850 /* Enable runtime PM and initialize the device. */
1851 pm_runtime_enable(&pdev->dev);
1852 ret = pm_runtime_get_sync(&pdev->dev);
1853 if (ret < 0) {
1854 dev_err(&pdev->dev, "runtime PM get sync failed (%d)\n", ret);
1855 return ret;
1858 ret = rcar_dmac_init(dmac);
1859 pm_runtime_put(&pdev->dev);
1861 if (ret) {
1862 dev_err(&pdev->dev, "failed to reset device\n");
1863 goto error;
1866 /* Initialize engine */
1867 engine = &dmac->engine;
1869 dma_cap_set(DMA_MEMCPY, engine->cap_mask);
1870 dma_cap_set(DMA_SLAVE, engine->cap_mask);
1872 engine->dev = &pdev->dev;
1873 engine->copy_align = ilog2(RCAR_DMAC_MEMCPY_XFER_SIZE);
1875 engine->src_addr_widths = widths;
1876 engine->dst_addr_widths = widths;
1877 engine->directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
1878 engine->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1880 engine->device_alloc_chan_resources = rcar_dmac_alloc_chan_resources;
1881 engine->device_free_chan_resources = rcar_dmac_free_chan_resources;
1882 engine->device_prep_dma_memcpy = rcar_dmac_prep_dma_memcpy;
1883 engine->device_prep_slave_sg = rcar_dmac_prep_slave_sg;
1884 engine->device_prep_dma_cyclic = rcar_dmac_prep_dma_cyclic;
1885 engine->device_config = rcar_dmac_device_config;
1886 engine->device_pause = rcar_dmac_chan_pause;
1887 engine->device_terminate_all = rcar_dmac_chan_terminate_all;
1888 engine->device_tx_status = rcar_dmac_tx_status;
1889 engine->device_issue_pending = rcar_dmac_issue_pending;
1890 engine->device_synchronize = rcar_dmac_device_synchronize;
1892 INIT_LIST_HEAD(&engine->channels);
1894 for (i = 0; i < dmac->n_channels; ++i) {
1895 ret = rcar_dmac_chan_probe(dmac, &dmac->channels[i],
1896 i + channels_offset);
1897 if (ret < 0)
1898 goto error;
1901 /* Register the DMAC as a DMA provider for DT. */
1902 ret = of_dma_controller_register(pdev->dev.of_node, rcar_dmac_of_xlate,
1903 NULL);
1904 if (ret < 0)
1905 goto error;
1908 * Register the DMA engine device.
1910 * Default transfer size of 32 bytes requires 32-byte alignment.
1912 ret = dma_async_device_register(engine);
1913 if (ret < 0)
1914 goto error;
1916 return 0;
1918 error:
1919 of_dma_controller_free(pdev->dev.of_node);
1920 pm_runtime_disable(&pdev->dev);
1921 return ret;
1924 static int rcar_dmac_remove(struct platform_device *pdev)
1926 struct rcar_dmac *dmac = platform_get_drvdata(pdev);
1928 of_dma_controller_free(pdev->dev.of_node);
1929 dma_async_device_unregister(&dmac->engine);
1931 pm_runtime_disable(&pdev->dev);
1933 return 0;
1936 static void rcar_dmac_shutdown(struct platform_device *pdev)
1938 struct rcar_dmac *dmac = platform_get_drvdata(pdev);
1940 rcar_dmac_stop_all_chan(dmac);
1943 static const struct of_device_id rcar_dmac_of_ids[] = {
1944 { .compatible = "renesas,rcar-dmac", },
1945 { /* Sentinel */ }
1947 MODULE_DEVICE_TABLE(of, rcar_dmac_of_ids);
1949 static struct platform_driver rcar_dmac_driver = {
1950 .driver = {
1951 .pm = &rcar_dmac_pm,
1952 .name = "rcar-dmac",
1953 .of_match_table = rcar_dmac_of_ids,
1955 .probe = rcar_dmac_probe,
1956 .remove = rcar_dmac_remove,
1957 .shutdown = rcar_dmac_shutdown,
1960 module_platform_driver(rcar_dmac_driver);
1962 MODULE_DESCRIPTION("R-Car Gen2 DMA Controller Driver");
1963 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1964 MODULE_LICENSE("GPL v2");