1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas R-Car Gen2/Gen3 DMA Controller Driver
5 * Copyright (C) 2014-2019 Renesas Electronics Inc.
7 * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
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>
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
;
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
{
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
;
78 struct list_head node
;
79 struct list_head chunks
;
80 struct rcar_dmac_xfer_chunk
*running
;
85 struct rcar_dmac_hw_desc
*mem
;
94 #define to_rcar_dmac_desc(d) container_of(d, struct rcar_dmac_desc, async_tx)
97 * struct rcar_dmac_desc_page - One page worth of descriptors
98 * @node: entry in the channel's pages list
99 * @descs: array of DMA descriptors
100 * @chunks: array of transfer chunk descriptors
102 struct rcar_dmac_desc_page
{
103 struct list_head node
;
106 struct rcar_dmac_desc descs
[0];
107 struct rcar_dmac_xfer_chunk chunks
[0];
111 #define RCAR_DMAC_DESCS_PER_PAGE \
112 ((PAGE_SIZE - offsetof(struct rcar_dmac_desc_page, descs)) / \
113 sizeof(struct rcar_dmac_desc))
114 #define RCAR_DMAC_XFER_CHUNKS_PER_PAGE \
115 ((PAGE_SIZE - offsetof(struct rcar_dmac_desc_page, chunks)) / \
116 sizeof(struct rcar_dmac_xfer_chunk))
119 * struct rcar_dmac_chan_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
{
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
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
;
165 struct rcar_dmac_chan_slave src
;
166 struct rcar_dmac_chan_slave dst
;
167 struct rcar_dmac_chan_map map
;
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
;
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 * @channels_mask: bitfield of which DMA channels are managed by this driver
196 * @modules: bitmask of client modules in use
199 struct dma_device engine
;
203 unsigned int n_channels
;
204 struct rcar_dmac_chan
*channels
;
207 DECLARE_BITMAP(modules
, 256);
210 #define to_rcar_dmac(d) container_of(d, struct rcar_dmac, engine)
213 * struct rcar_dmac_of_data - This driver's OF data
214 * @chan_offset_base: DMAC channels base offset
215 * @chan_offset_stride: DMAC channels offset stride
217 struct rcar_dmac_of_data
{
218 u32 chan_offset_base
;
219 u32 chan_offset_stride
;
222 /* -----------------------------------------------------------------------------
226 #define RCAR_DMAISTA 0x0020
227 #define RCAR_DMASEC 0x0030
228 #define RCAR_DMAOR 0x0060
229 #define RCAR_DMAOR_PRI_FIXED (0 << 8)
230 #define RCAR_DMAOR_PRI_ROUND_ROBIN (3 << 8)
231 #define RCAR_DMAOR_AE (1 << 2)
232 #define RCAR_DMAOR_DME (1 << 0)
233 #define RCAR_DMACHCLR 0x0080
234 #define RCAR_DMADPSEC 0x00a0
236 #define RCAR_DMASAR 0x0000
237 #define RCAR_DMADAR 0x0004
238 #define RCAR_DMATCR 0x0008
239 #define RCAR_DMATCR_MASK 0x00ffffff
240 #define RCAR_DMATSR 0x0028
241 #define RCAR_DMACHCR 0x000c
242 #define RCAR_DMACHCR_CAE (1 << 31)
243 #define RCAR_DMACHCR_CAIE (1 << 30)
244 #define RCAR_DMACHCR_DPM_DISABLED (0 << 28)
245 #define RCAR_DMACHCR_DPM_ENABLED (1 << 28)
246 #define RCAR_DMACHCR_DPM_REPEAT (2 << 28)
247 #define RCAR_DMACHCR_DPM_INFINITE (3 << 28)
248 #define RCAR_DMACHCR_RPT_SAR (1 << 27)
249 #define RCAR_DMACHCR_RPT_DAR (1 << 26)
250 #define RCAR_DMACHCR_RPT_TCR (1 << 25)
251 #define RCAR_DMACHCR_DPB (1 << 22)
252 #define RCAR_DMACHCR_DSE (1 << 19)
253 #define RCAR_DMACHCR_DSIE (1 << 18)
254 #define RCAR_DMACHCR_TS_1B ((0 << 20) | (0 << 3))
255 #define RCAR_DMACHCR_TS_2B ((0 << 20) | (1 << 3))
256 #define RCAR_DMACHCR_TS_4B ((0 << 20) | (2 << 3))
257 #define RCAR_DMACHCR_TS_16B ((0 << 20) | (3 << 3))
258 #define RCAR_DMACHCR_TS_32B ((1 << 20) | (0 << 3))
259 #define RCAR_DMACHCR_TS_64B ((1 << 20) | (1 << 3))
260 #define RCAR_DMACHCR_TS_8B ((1 << 20) | (3 << 3))
261 #define RCAR_DMACHCR_DM_FIXED (0 << 14)
262 #define RCAR_DMACHCR_DM_INC (1 << 14)
263 #define RCAR_DMACHCR_DM_DEC (2 << 14)
264 #define RCAR_DMACHCR_SM_FIXED (0 << 12)
265 #define RCAR_DMACHCR_SM_INC (1 << 12)
266 #define RCAR_DMACHCR_SM_DEC (2 << 12)
267 #define RCAR_DMACHCR_RS_AUTO (4 << 8)
268 #define RCAR_DMACHCR_RS_DMARS (8 << 8)
269 #define RCAR_DMACHCR_IE (1 << 2)
270 #define RCAR_DMACHCR_TE (1 << 1)
271 #define RCAR_DMACHCR_DE (1 << 0)
272 #define RCAR_DMATCRB 0x0018
273 #define RCAR_DMATSRB 0x0038
274 #define RCAR_DMACHCRB 0x001c
275 #define RCAR_DMACHCRB_DCNT(n) ((n) << 24)
276 #define RCAR_DMACHCRB_DPTR_MASK (0xff << 16)
277 #define RCAR_DMACHCRB_DPTR_SHIFT 16
278 #define RCAR_DMACHCRB_DRST (1 << 15)
279 #define RCAR_DMACHCRB_DTS (1 << 8)
280 #define RCAR_DMACHCRB_SLM_NORMAL (0 << 4)
281 #define RCAR_DMACHCRB_SLM_CLK(n) ((8 | (n)) << 4)
282 #define RCAR_DMACHCRB_PRI(n) ((n) << 0)
283 #define RCAR_DMARS 0x0040
284 #define RCAR_DMABUFCR 0x0048
285 #define RCAR_DMABUFCR_MBU(n) ((n) << 16)
286 #define RCAR_DMABUFCR_ULB(n) ((n) << 0)
287 #define RCAR_DMADPBASE 0x0050
288 #define RCAR_DMADPBASE_MASK 0xfffffff0
289 #define RCAR_DMADPBASE_SEL (1 << 0)
290 #define RCAR_DMADPCR 0x0054
291 #define RCAR_DMADPCR_DIPT(n) ((n) << 24)
292 #define RCAR_DMAFIXSAR 0x0010
293 #define RCAR_DMAFIXDAR 0x0014
294 #define RCAR_DMAFIXDPBASE 0x0060
296 /* Hardcode the MEMCPY transfer size to 4 bytes. */
297 #define RCAR_DMAC_MEMCPY_XFER_SIZE 4
299 /* -----------------------------------------------------------------------------
303 static void rcar_dmac_write(struct rcar_dmac
*dmac
, u32 reg
, u32 data
)
305 if (reg
== RCAR_DMAOR
)
306 writew(data
, dmac
->iomem
+ reg
);
308 writel(data
, dmac
->iomem
+ reg
);
311 static u32
rcar_dmac_read(struct rcar_dmac
*dmac
, u32 reg
)
313 if (reg
== RCAR_DMAOR
)
314 return readw(dmac
->iomem
+ reg
);
316 return readl(dmac
->iomem
+ reg
);
319 static u32
rcar_dmac_chan_read(struct rcar_dmac_chan
*chan
, u32 reg
)
321 if (reg
== RCAR_DMARS
)
322 return readw(chan
->iomem
+ reg
);
324 return readl(chan
->iomem
+ reg
);
327 static void rcar_dmac_chan_write(struct rcar_dmac_chan
*chan
, u32 reg
, u32 data
)
329 if (reg
== RCAR_DMARS
)
330 writew(data
, chan
->iomem
+ reg
);
332 writel(data
, chan
->iomem
+ reg
);
335 /* -----------------------------------------------------------------------------
336 * Initialization and configuration
339 static bool rcar_dmac_chan_is_busy(struct rcar_dmac_chan
*chan
)
341 u32 chcr
= rcar_dmac_chan_read(chan
, RCAR_DMACHCR
);
343 return !!(chcr
& (RCAR_DMACHCR_DE
| RCAR_DMACHCR_TE
));
346 static void rcar_dmac_chan_start_xfer(struct rcar_dmac_chan
*chan
)
348 struct rcar_dmac_desc
*desc
= chan
->desc
.running
;
349 u32 chcr
= desc
->chcr
;
351 WARN_ON_ONCE(rcar_dmac_chan_is_busy(chan
));
353 if (chan
->mid_rid
>= 0)
354 rcar_dmac_chan_write(chan
, RCAR_DMARS
, chan
->mid_rid
);
356 if (desc
->hwdescs
.use
) {
357 struct rcar_dmac_xfer_chunk
*chunk
=
358 list_first_entry(&desc
->chunks
,
359 struct rcar_dmac_xfer_chunk
, node
);
361 dev_dbg(chan
->chan
.device
->dev
,
362 "chan%u: queue desc %p: %u@%pad\n",
363 chan
->index
, desc
, desc
->nchunks
, &desc
->hwdescs
.dma
);
365 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
366 rcar_dmac_chan_write(chan
, RCAR_DMAFIXSAR
,
367 chunk
->src_addr
>> 32);
368 rcar_dmac_chan_write(chan
, RCAR_DMAFIXDAR
,
369 chunk
->dst_addr
>> 32);
370 rcar_dmac_chan_write(chan
, RCAR_DMAFIXDPBASE
,
371 desc
->hwdescs
.dma
>> 32);
373 rcar_dmac_chan_write(chan
, RCAR_DMADPBASE
,
374 (desc
->hwdescs
.dma
& 0xfffffff0) |
376 rcar_dmac_chan_write(chan
, RCAR_DMACHCRB
,
377 RCAR_DMACHCRB_DCNT(desc
->nchunks
- 1) |
381 * Errata: When descriptor memory is accessed through an IOMMU
382 * the DMADAR register isn't initialized automatically from the
383 * first descriptor at beginning of transfer by the DMAC like it
384 * should. Initialize it manually with the destination address
385 * of the first chunk.
387 rcar_dmac_chan_write(chan
, RCAR_DMADAR
,
388 chunk
->dst_addr
& 0xffffffff);
391 * Program the descriptor stage interrupt to occur after the end
392 * of the first stage.
394 rcar_dmac_chan_write(chan
, RCAR_DMADPCR
, RCAR_DMADPCR_DIPT(1));
396 chcr
|= RCAR_DMACHCR_RPT_SAR
| RCAR_DMACHCR_RPT_DAR
397 | RCAR_DMACHCR_RPT_TCR
| RCAR_DMACHCR_DPB
;
400 * If the descriptor isn't cyclic enable normal descriptor mode
401 * and the transfer completion interrupt.
404 chcr
|= RCAR_DMACHCR_DPM_ENABLED
| RCAR_DMACHCR_IE
;
406 * If the descriptor is cyclic and has a callback enable the
407 * descriptor stage interrupt in infinite repeat mode.
409 else if (desc
->async_tx
.callback
)
410 chcr
|= RCAR_DMACHCR_DPM_INFINITE
| RCAR_DMACHCR_DSIE
;
412 * Otherwise just select infinite repeat mode without any
416 chcr
|= RCAR_DMACHCR_DPM_INFINITE
;
418 struct rcar_dmac_xfer_chunk
*chunk
= desc
->running
;
420 dev_dbg(chan
->chan
.device
->dev
,
421 "chan%u: queue chunk %p: %u@%pad -> %pad\n",
422 chan
->index
, chunk
, chunk
->size
, &chunk
->src_addr
,
425 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
426 rcar_dmac_chan_write(chan
, RCAR_DMAFIXSAR
,
427 chunk
->src_addr
>> 32);
428 rcar_dmac_chan_write(chan
, RCAR_DMAFIXDAR
,
429 chunk
->dst_addr
>> 32);
431 rcar_dmac_chan_write(chan
, RCAR_DMASAR
,
432 chunk
->src_addr
& 0xffffffff);
433 rcar_dmac_chan_write(chan
, RCAR_DMADAR
,
434 chunk
->dst_addr
& 0xffffffff);
435 rcar_dmac_chan_write(chan
, RCAR_DMATCR
,
436 chunk
->size
>> desc
->xfer_shift
);
438 chcr
|= RCAR_DMACHCR_DPM_DISABLED
| RCAR_DMACHCR_IE
;
441 rcar_dmac_chan_write(chan
, RCAR_DMACHCR
,
442 chcr
| RCAR_DMACHCR_DE
| RCAR_DMACHCR_CAIE
);
445 static int rcar_dmac_init(struct rcar_dmac
*dmac
)
449 /* Clear all channels and enable the DMAC globally. */
450 rcar_dmac_write(dmac
, RCAR_DMACHCLR
, dmac
->channels_mask
);
451 rcar_dmac_write(dmac
, RCAR_DMAOR
,
452 RCAR_DMAOR_PRI_FIXED
| RCAR_DMAOR_DME
);
454 dmaor
= rcar_dmac_read(dmac
, RCAR_DMAOR
);
455 if ((dmaor
& (RCAR_DMAOR_AE
| RCAR_DMAOR_DME
)) != RCAR_DMAOR_DME
) {
456 dev_warn(dmac
->dev
, "DMAOR initialization failed.\n");
463 /* -----------------------------------------------------------------------------
464 * Descriptors submission
467 static dma_cookie_t
rcar_dmac_tx_submit(struct dma_async_tx_descriptor
*tx
)
469 struct rcar_dmac_chan
*chan
= to_rcar_dmac_chan(tx
->chan
);
470 struct rcar_dmac_desc
*desc
= to_rcar_dmac_desc(tx
);
474 spin_lock_irqsave(&chan
->lock
, flags
);
476 cookie
= dma_cookie_assign(tx
);
478 dev_dbg(chan
->chan
.device
->dev
, "chan%u: submit #%d@%p\n",
479 chan
->index
, tx
->cookie
, desc
);
481 list_add_tail(&desc
->node
, &chan
->desc
.pending
);
482 desc
->running
= list_first_entry(&desc
->chunks
,
483 struct rcar_dmac_xfer_chunk
, node
);
485 spin_unlock_irqrestore(&chan
->lock
, flags
);
490 /* -----------------------------------------------------------------------------
491 * Descriptors allocation and free
495 * rcar_dmac_desc_alloc - Allocate a page worth of DMA descriptors
496 * @chan: the DMA channel
497 * @gfp: allocation flags
499 static int rcar_dmac_desc_alloc(struct rcar_dmac_chan
*chan
, gfp_t gfp
)
501 struct rcar_dmac_desc_page
*page
;
506 page
= (void *)get_zeroed_page(gfp
);
510 for (i
= 0; i
< RCAR_DMAC_DESCS_PER_PAGE
; ++i
) {
511 struct rcar_dmac_desc
*desc
= &page
->descs
[i
];
513 dma_async_tx_descriptor_init(&desc
->async_tx
, &chan
->chan
);
514 desc
->async_tx
.tx_submit
= rcar_dmac_tx_submit
;
515 INIT_LIST_HEAD(&desc
->chunks
);
517 list_add_tail(&desc
->node
, &list
);
520 spin_lock_irqsave(&chan
->lock
, flags
);
521 list_splice_tail(&list
, &chan
->desc
.free
);
522 list_add_tail(&page
->node
, &chan
->desc
.pages
);
523 spin_unlock_irqrestore(&chan
->lock
, flags
);
529 * rcar_dmac_desc_put - Release a DMA transfer descriptor
530 * @chan: the DMA channel
531 * @desc: the descriptor
533 * Put the descriptor and its transfer chunk descriptors back in the channel's
534 * free descriptors lists. The descriptor's chunks list will be reinitialized to
535 * an empty list as a result.
537 * The descriptor must have been removed from the channel's lists before calling
540 static void rcar_dmac_desc_put(struct rcar_dmac_chan
*chan
,
541 struct rcar_dmac_desc
*desc
)
545 spin_lock_irqsave(&chan
->lock
, flags
);
546 list_splice_tail_init(&desc
->chunks
, &chan
->desc
.chunks_free
);
547 list_add(&desc
->node
, &chan
->desc
.free
);
548 spin_unlock_irqrestore(&chan
->lock
, flags
);
551 static void rcar_dmac_desc_recycle_acked(struct rcar_dmac_chan
*chan
)
553 struct rcar_dmac_desc
*desc
, *_desc
;
558 * We have to temporarily move all descriptors from the wait list to a
559 * local list as iterating over the wait list, even with
560 * list_for_each_entry_safe, isn't safe if we release the channel lock
561 * around the rcar_dmac_desc_put() call.
563 spin_lock_irqsave(&chan
->lock
, flags
);
564 list_splice_init(&chan
->desc
.wait
, &list
);
565 spin_unlock_irqrestore(&chan
->lock
, flags
);
567 list_for_each_entry_safe(desc
, _desc
, &list
, node
) {
568 if (async_tx_test_ack(&desc
->async_tx
)) {
569 list_del(&desc
->node
);
570 rcar_dmac_desc_put(chan
, desc
);
574 if (list_empty(&list
))
577 /* Put the remaining descriptors back in the wait list. */
578 spin_lock_irqsave(&chan
->lock
, flags
);
579 list_splice(&list
, &chan
->desc
.wait
);
580 spin_unlock_irqrestore(&chan
->lock
, flags
);
584 * rcar_dmac_desc_get - Allocate a descriptor for a DMA transfer
585 * @chan: the DMA channel
587 * Locking: This function must be called in a non-atomic context.
589 * Return: A pointer to the allocated descriptor or NULL if no descriptor can
592 static struct rcar_dmac_desc
*rcar_dmac_desc_get(struct rcar_dmac_chan
*chan
)
594 struct rcar_dmac_desc
*desc
;
598 /* Recycle acked descriptors before attempting allocation. */
599 rcar_dmac_desc_recycle_acked(chan
);
601 spin_lock_irqsave(&chan
->lock
, flags
);
603 while (list_empty(&chan
->desc
.free
)) {
605 * No free descriptors, allocate a page worth of them and try
606 * again, as someone else could race us to get the newly
607 * allocated descriptors. If the allocation fails return an
610 spin_unlock_irqrestore(&chan
->lock
, flags
);
611 ret
= rcar_dmac_desc_alloc(chan
, GFP_NOWAIT
);
614 spin_lock_irqsave(&chan
->lock
, flags
);
617 desc
= list_first_entry(&chan
->desc
.free
, struct rcar_dmac_desc
, node
);
618 list_del(&desc
->node
);
620 spin_unlock_irqrestore(&chan
->lock
, flags
);
626 * rcar_dmac_xfer_chunk_alloc - Allocate a page worth of transfer chunks
627 * @chan: the DMA channel
628 * @gfp: allocation flags
630 static int rcar_dmac_xfer_chunk_alloc(struct rcar_dmac_chan
*chan
, gfp_t gfp
)
632 struct rcar_dmac_desc_page
*page
;
637 page
= (void *)get_zeroed_page(gfp
);
641 for (i
= 0; i
< RCAR_DMAC_XFER_CHUNKS_PER_PAGE
; ++i
) {
642 struct rcar_dmac_xfer_chunk
*chunk
= &page
->chunks
[i
];
644 list_add_tail(&chunk
->node
, &list
);
647 spin_lock_irqsave(&chan
->lock
, flags
);
648 list_splice_tail(&list
, &chan
->desc
.chunks_free
);
649 list_add_tail(&page
->node
, &chan
->desc
.pages
);
650 spin_unlock_irqrestore(&chan
->lock
, flags
);
656 * rcar_dmac_xfer_chunk_get - Allocate a transfer chunk for a DMA transfer
657 * @chan: the DMA channel
659 * Locking: This function must be called in a non-atomic context.
661 * Return: A pointer to the allocated transfer chunk descriptor or NULL if no
662 * descriptor can be allocated.
664 static struct rcar_dmac_xfer_chunk
*
665 rcar_dmac_xfer_chunk_get(struct rcar_dmac_chan
*chan
)
667 struct rcar_dmac_xfer_chunk
*chunk
;
671 spin_lock_irqsave(&chan
->lock
, flags
);
673 while (list_empty(&chan
->desc
.chunks_free
)) {
675 * No free descriptors, allocate a page worth of them and try
676 * again, as someone else could race us to get the newly
677 * allocated descriptors. If the allocation fails return an
680 spin_unlock_irqrestore(&chan
->lock
, flags
);
681 ret
= rcar_dmac_xfer_chunk_alloc(chan
, GFP_NOWAIT
);
684 spin_lock_irqsave(&chan
->lock
, flags
);
687 chunk
= list_first_entry(&chan
->desc
.chunks_free
,
688 struct rcar_dmac_xfer_chunk
, node
);
689 list_del(&chunk
->node
);
691 spin_unlock_irqrestore(&chan
->lock
, flags
);
696 static void rcar_dmac_realloc_hwdesc(struct rcar_dmac_chan
*chan
,
697 struct rcar_dmac_desc
*desc
, size_t size
)
700 * dma_alloc_coherent() allocates memory in page size increments. To
701 * avoid reallocating the hardware descriptors when the allocated size
702 * wouldn't change align the requested size to a multiple of the page
705 size
= PAGE_ALIGN(size
);
707 if (desc
->hwdescs
.size
== size
)
710 if (desc
->hwdescs
.mem
) {
711 dma_free_coherent(chan
->chan
.device
->dev
, desc
->hwdescs
.size
,
712 desc
->hwdescs
.mem
, desc
->hwdescs
.dma
);
713 desc
->hwdescs
.mem
= NULL
;
714 desc
->hwdescs
.size
= 0;
720 desc
->hwdescs
.mem
= dma_alloc_coherent(chan
->chan
.device
->dev
, size
,
721 &desc
->hwdescs
.dma
, GFP_NOWAIT
);
722 if (!desc
->hwdescs
.mem
)
725 desc
->hwdescs
.size
= size
;
728 static int rcar_dmac_fill_hwdesc(struct rcar_dmac_chan
*chan
,
729 struct rcar_dmac_desc
*desc
)
731 struct rcar_dmac_xfer_chunk
*chunk
;
732 struct rcar_dmac_hw_desc
*hwdesc
;
734 rcar_dmac_realloc_hwdesc(chan
, desc
, desc
->nchunks
* sizeof(*hwdesc
));
736 hwdesc
= desc
->hwdescs
.mem
;
740 list_for_each_entry(chunk
, &desc
->chunks
, node
) {
741 hwdesc
->sar
= chunk
->src_addr
;
742 hwdesc
->dar
= chunk
->dst_addr
;
743 hwdesc
->tcr
= chunk
->size
>> desc
->xfer_shift
;
750 /* -----------------------------------------------------------------------------
753 static void rcar_dmac_chcr_de_barrier(struct rcar_dmac_chan
*chan
)
759 * Ensure that the setting of the DE bit is actually 0 after
762 for (i
= 0; i
< 1024; i
++) {
763 chcr
= rcar_dmac_chan_read(chan
, RCAR_DMACHCR
);
764 if (!(chcr
& RCAR_DMACHCR_DE
))
769 dev_err(chan
->chan
.device
->dev
, "CHCR DE check error\n");
772 static void rcar_dmac_clear_chcr_de(struct rcar_dmac_chan
*chan
)
774 u32 chcr
= rcar_dmac_chan_read(chan
, RCAR_DMACHCR
);
776 /* set DE=0 and flush remaining data */
777 rcar_dmac_chan_write(chan
, RCAR_DMACHCR
, (chcr
& ~RCAR_DMACHCR_DE
));
779 /* make sure all remaining data was flushed */
780 rcar_dmac_chcr_de_barrier(chan
);
783 static void rcar_dmac_chan_halt(struct rcar_dmac_chan
*chan
)
785 u32 chcr
= rcar_dmac_chan_read(chan
, RCAR_DMACHCR
);
787 chcr
&= ~(RCAR_DMACHCR_DSE
| RCAR_DMACHCR_DSIE
| RCAR_DMACHCR_IE
|
788 RCAR_DMACHCR_TE
| RCAR_DMACHCR_DE
|
789 RCAR_DMACHCR_CAE
| RCAR_DMACHCR_CAIE
);
790 rcar_dmac_chan_write(chan
, RCAR_DMACHCR
, chcr
);
791 rcar_dmac_chcr_de_barrier(chan
);
794 static void rcar_dmac_chan_reinit(struct rcar_dmac_chan
*chan
)
796 struct rcar_dmac_desc
*desc
, *_desc
;
800 spin_lock_irqsave(&chan
->lock
, flags
);
802 /* Move all non-free descriptors to the local lists. */
803 list_splice_init(&chan
->desc
.pending
, &descs
);
804 list_splice_init(&chan
->desc
.active
, &descs
);
805 list_splice_init(&chan
->desc
.done
, &descs
);
806 list_splice_init(&chan
->desc
.wait
, &descs
);
808 chan
->desc
.running
= NULL
;
810 spin_unlock_irqrestore(&chan
->lock
, flags
);
812 list_for_each_entry_safe(desc
, _desc
, &descs
, node
) {
813 list_del(&desc
->node
);
814 rcar_dmac_desc_put(chan
, desc
);
818 static void rcar_dmac_stop_all_chan(struct rcar_dmac
*dmac
)
822 /* Stop all channels. */
823 for (i
= 0; i
< dmac
->n_channels
; ++i
) {
824 struct rcar_dmac_chan
*chan
= &dmac
->channels
[i
];
826 if (!(dmac
->channels_mask
& BIT(i
)))
829 /* Stop and reinitialize the channel. */
830 spin_lock_irq(&chan
->lock
);
831 rcar_dmac_chan_halt(chan
);
832 spin_unlock_irq(&chan
->lock
);
836 static int rcar_dmac_chan_pause(struct dma_chan
*chan
)
839 struct rcar_dmac_chan
*rchan
= to_rcar_dmac_chan(chan
);
841 spin_lock_irqsave(&rchan
->lock
, flags
);
842 rcar_dmac_clear_chcr_de(rchan
);
843 spin_unlock_irqrestore(&rchan
->lock
, flags
);
848 /* -----------------------------------------------------------------------------
849 * Descriptors preparation
852 static void rcar_dmac_chan_configure_desc(struct rcar_dmac_chan
*chan
,
853 struct rcar_dmac_desc
*desc
)
855 static const u32 chcr_ts
[] = {
856 RCAR_DMACHCR_TS_1B
, RCAR_DMACHCR_TS_2B
,
857 RCAR_DMACHCR_TS_4B
, RCAR_DMACHCR_TS_8B
,
858 RCAR_DMACHCR_TS_16B
, RCAR_DMACHCR_TS_32B
,
862 unsigned int xfer_size
;
865 switch (desc
->direction
) {
867 chcr
= RCAR_DMACHCR_DM_INC
| RCAR_DMACHCR_SM_FIXED
868 | RCAR_DMACHCR_RS_DMARS
;
869 xfer_size
= chan
->src
.xfer_size
;
873 chcr
= RCAR_DMACHCR_DM_FIXED
| RCAR_DMACHCR_SM_INC
874 | RCAR_DMACHCR_RS_DMARS
;
875 xfer_size
= chan
->dst
.xfer_size
;
880 chcr
= RCAR_DMACHCR_DM_INC
| RCAR_DMACHCR_SM_INC
881 | RCAR_DMACHCR_RS_AUTO
;
882 xfer_size
= RCAR_DMAC_MEMCPY_XFER_SIZE
;
886 desc
->xfer_shift
= ilog2(xfer_size
);
887 desc
->chcr
= chcr
| chcr_ts
[desc
->xfer_shift
];
891 * rcar_dmac_chan_prep_sg - prepare transfer descriptors from an SG list
893 * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
894 * converted to scatter-gather to guarantee consistent locking and a correct
895 * list manipulation. For slave DMA direction carries the usual meaning, and,
896 * logically, the SG list is RAM and the addr variable contains slave address,
897 * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_MEM_TO_MEM
898 * and the SG list contains only one element and points at the source buffer.
900 static struct dma_async_tx_descriptor
*
901 rcar_dmac_chan_prep_sg(struct rcar_dmac_chan
*chan
, struct scatterlist
*sgl
,
902 unsigned int sg_len
, dma_addr_t dev_addr
,
903 enum dma_transfer_direction dir
, unsigned long dma_flags
,
906 struct rcar_dmac_xfer_chunk
*chunk
;
907 struct rcar_dmac_desc
*desc
;
908 struct scatterlist
*sg
;
909 unsigned int nchunks
= 0;
910 unsigned int max_chunk_size
;
911 unsigned int full_size
= 0;
912 bool cross_boundary
= false;
914 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
919 desc
= rcar_dmac_desc_get(chan
);
923 desc
->async_tx
.flags
= dma_flags
;
924 desc
->async_tx
.cookie
= -EBUSY
;
926 desc
->cyclic
= cyclic
;
927 desc
->direction
= dir
;
929 rcar_dmac_chan_configure_desc(chan
, desc
);
931 max_chunk_size
= RCAR_DMATCR_MASK
<< desc
->xfer_shift
;
934 * Allocate and fill the transfer chunk descriptors. We own the only
935 * reference to the DMA descriptor, there's no need for locking.
937 for_each_sg(sgl
, sg
, sg_len
, i
) {
938 dma_addr_t mem_addr
= sg_dma_address(sg
);
939 unsigned int len
= sg_dma_len(sg
);
943 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
945 high_dev_addr
= dev_addr
>> 32;
946 high_mem_addr
= mem_addr
>> 32;
949 if ((dev_addr
>> 32 != high_dev_addr
) ||
950 (mem_addr
>> 32 != high_mem_addr
))
951 cross_boundary
= true;
954 unsigned int size
= min(len
, max_chunk_size
);
956 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
958 * Prevent individual transfers from crossing 4GB
961 if (dev_addr
>> 32 != (dev_addr
+ size
- 1) >> 32) {
962 size
= ALIGN(dev_addr
, 1ULL << 32) - dev_addr
;
963 cross_boundary
= true;
965 if (mem_addr
>> 32 != (mem_addr
+ size
- 1) >> 32) {
966 size
= ALIGN(mem_addr
, 1ULL << 32) - mem_addr
;
967 cross_boundary
= true;
971 chunk
= rcar_dmac_xfer_chunk_get(chan
);
973 rcar_dmac_desc_put(chan
, desc
);
977 if (dir
== DMA_DEV_TO_MEM
) {
978 chunk
->src_addr
= dev_addr
;
979 chunk
->dst_addr
= mem_addr
;
981 chunk
->src_addr
= mem_addr
;
982 chunk
->dst_addr
= dev_addr
;
987 dev_dbg(chan
->chan
.device
->dev
,
988 "chan%u: chunk %p/%p sgl %u@%p, %u/%u %pad -> %pad\n",
989 chan
->index
, chunk
, desc
, i
, sg
, size
, len
,
990 &chunk
->src_addr
, &chunk
->dst_addr
);
993 if (dir
== DMA_MEM_TO_MEM
)
998 list_add_tail(&chunk
->node
, &desc
->chunks
);
1003 desc
->nchunks
= nchunks
;
1004 desc
->size
= full_size
;
1007 * Use hardware descriptor lists if possible when more than one chunk
1008 * needs to be transferred (otherwise they don't make much sense).
1010 * Source/Destination address should be located in same 4GiB region
1011 * in the 40bit address space when it uses Hardware descriptor,
1012 * and cross_boundary is checking it.
1014 desc
->hwdescs
.use
= !cross_boundary
&& nchunks
> 1;
1015 if (desc
->hwdescs
.use
) {
1016 if (rcar_dmac_fill_hwdesc(chan
, desc
) < 0)
1017 desc
->hwdescs
.use
= false;
1020 return &desc
->async_tx
;
1023 /* -----------------------------------------------------------------------------
1024 * DMA engine operations
1027 static int rcar_dmac_alloc_chan_resources(struct dma_chan
*chan
)
1029 struct rcar_dmac_chan
*rchan
= to_rcar_dmac_chan(chan
);
1032 INIT_LIST_HEAD(&rchan
->desc
.chunks_free
);
1033 INIT_LIST_HEAD(&rchan
->desc
.pages
);
1035 /* Preallocate descriptors. */
1036 ret
= rcar_dmac_xfer_chunk_alloc(rchan
, GFP_KERNEL
);
1040 ret
= rcar_dmac_desc_alloc(rchan
, GFP_KERNEL
);
1044 return pm_runtime_get_sync(chan
->device
->dev
);
1047 static void rcar_dmac_free_chan_resources(struct dma_chan
*chan
)
1049 struct rcar_dmac_chan
*rchan
= to_rcar_dmac_chan(chan
);
1050 struct rcar_dmac
*dmac
= to_rcar_dmac(chan
->device
);
1051 struct rcar_dmac_chan_map
*map
= &rchan
->map
;
1052 struct rcar_dmac_desc_page
*page
, *_page
;
1053 struct rcar_dmac_desc
*desc
;
1056 /* Protect against ISR */
1057 spin_lock_irq(&rchan
->lock
);
1058 rcar_dmac_chan_halt(rchan
);
1059 spin_unlock_irq(&rchan
->lock
);
1062 * Now no new interrupts will occur, but one might already be
1063 * running. Wait for it to finish before freeing resources.
1065 synchronize_irq(rchan
->irq
);
1067 if (rchan
->mid_rid
>= 0) {
1068 /* The caller is holding dma_list_mutex */
1069 clear_bit(rchan
->mid_rid
, dmac
->modules
);
1070 rchan
->mid_rid
= -EINVAL
;
1073 list_splice_init(&rchan
->desc
.free
, &list
);
1074 list_splice_init(&rchan
->desc
.pending
, &list
);
1075 list_splice_init(&rchan
->desc
.active
, &list
);
1076 list_splice_init(&rchan
->desc
.done
, &list
);
1077 list_splice_init(&rchan
->desc
.wait
, &list
);
1079 rchan
->desc
.running
= NULL
;
1081 list_for_each_entry(desc
, &list
, node
)
1082 rcar_dmac_realloc_hwdesc(rchan
, desc
, 0);
1084 list_for_each_entry_safe(page
, _page
, &rchan
->desc
.pages
, node
) {
1085 list_del(&page
->node
);
1086 free_page((unsigned long)page
);
1089 /* Remove slave mapping if present. */
1090 if (map
->slave
.xfer_size
) {
1091 dma_unmap_resource(chan
->device
->dev
, map
->addr
,
1092 map
->slave
.xfer_size
, map
->dir
, 0);
1093 map
->slave
.xfer_size
= 0;
1096 pm_runtime_put(chan
->device
->dev
);
1099 static struct dma_async_tx_descriptor
*
1100 rcar_dmac_prep_dma_memcpy(struct dma_chan
*chan
, dma_addr_t dma_dest
,
1101 dma_addr_t dma_src
, size_t len
, unsigned long flags
)
1103 struct rcar_dmac_chan
*rchan
= to_rcar_dmac_chan(chan
);
1104 struct scatterlist sgl
;
1109 sg_init_table(&sgl
, 1);
1110 sg_set_page(&sgl
, pfn_to_page(PFN_DOWN(dma_src
)), len
,
1111 offset_in_page(dma_src
));
1112 sg_dma_address(&sgl
) = dma_src
;
1113 sg_dma_len(&sgl
) = len
;
1115 return rcar_dmac_chan_prep_sg(rchan
, &sgl
, 1, dma_dest
,
1116 DMA_MEM_TO_MEM
, flags
, false);
1119 static int rcar_dmac_map_slave_addr(struct dma_chan
*chan
,
1120 enum dma_transfer_direction dir
)
1122 struct rcar_dmac_chan
*rchan
= to_rcar_dmac_chan(chan
);
1123 struct rcar_dmac_chan_map
*map
= &rchan
->map
;
1124 phys_addr_t dev_addr
;
1126 enum dma_data_direction dev_dir
;
1128 if (dir
== DMA_DEV_TO_MEM
) {
1129 dev_addr
= rchan
->src
.slave_addr
;
1130 dev_size
= rchan
->src
.xfer_size
;
1131 dev_dir
= DMA_TO_DEVICE
;
1133 dev_addr
= rchan
->dst
.slave_addr
;
1134 dev_size
= rchan
->dst
.xfer_size
;
1135 dev_dir
= DMA_FROM_DEVICE
;
1138 /* Reuse current map if possible. */
1139 if (dev_addr
== map
->slave
.slave_addr
&&
1140 dev_size
== map
->slave
.xfer_size
&&
1141 dev_dir
== map
->dir
)
1144 /* Remove old mapping if present. */
1145 if (map
->slave
.xfer_size
)
1146 dma_unmap_resource(chan
->device
->dev
, map
->addr
,
1147 map
->slave
.xfer_size
, map
->dir
, 0);
1148 map
->slave
.xfer_size
= 0;
1150 /* Create new slave address map. */
1151 map
->addr
= dma_map_resource(chan
->device
->dev
, dev_addr
, dev_size
,
1154 if (dma_mapping_error(chan
->device
->dev
, map
->addr
)) {
1155 dev_err(chan
->device
->dev
,
1156 "chan%u: failed to map %zx@%pap", rchan
->index
,
1157 dev_size
, &dev_addr
);
1161 dev_dbg(chan
->device
->dev
, "chan%u: map %zx@%pap to %pad dir: %s\n",
1162 rchan
->index
, dev_size
, &dev_addr
, &map
->addr
,
1163 dev_dir
== DMA_TO_DEVICE
? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE");
1165 map
->slave
.slave_addr
= dev_addr
;
1166 map
->slave
.xfer_size
= dev_size
;
1172 static struct dma_async_tx_descriptor
*
1173 rcar_dmac_prep_slave_sg(struct dma_chan
*chan
, struct scatterlist
*sgl
,
1174 unsigned int sg_len
, enum dma_transfer_direction dir
,
1175 unsigned long flags
, void *context
)
1177 struct rcar_dmac_chan
*rchan
= to_rcar_dmac_chan(chan
);
1179 /* Someone calling slave DMA on a generic channel? */
1180 if (rchan
->mid_rid
< 0 || !sg_len
|| !sg_dma_len(sgl
)) {
1181 dev_warn(chan
->device
->dev
,
1182 "%s: bad parameter: len=%d, id=%d\n",
1183 __func__
, sg_len
, rchan
->mid_rid
);
1187 if (rcar_dmac_map_slave_addr(chan
, dir
))
1190 return rcar_dmac_chan_prep_sg(rchan
, sgl
, sg_len
, rchan
->map
.addr
,
1194 #define RCAR_DMAC_MAX_SG_LEN 32
1196 static struct dma_async_tx_descriptor
*
1197 rcar_dmac_prep_dma_cyclic(struct dma_chan
*chan
, dma_addr_t buf_addr
,
1198 size_t buf_len
, size_t period_len
,
1199 enum dma_transfer_direction dir
, unsigned long flags
)
1201 struct rcar_dmac_chan
*rchan
= to_rcar_dmac_chan(chan
);
1202 struct dma_async_tx_descriptor
*desc
;
1203 struct scatterlist
*sgl
;
1204 unsigned int sg_len
;
1207 /* Someone calling slave DMA on a generic channel? */
1208 if (rchan
->mid_rid
< 0 || buf_len
< period_len
) {
1209 dev_warn(chan
->device
->dev
,
1210 "%s: bad parameter: buf_len=%zu, period_len=%zu, id=%d\n",
1211 __func__
, buf_len
, period_len
, rchan
->mid_rid
);
1215 if (rcar_dmac_map_slave_addr(chan
, dir
))
1218 sg_len
= buf_len
/ period_len
;
1219 if (sg_len
> RCAR_DMAC_MAX_SG_LEN
) {
1220 dev_err(chan
->device
->dev
,
1221 "chan%u: sg length %d exceeds limit %d",
1222 rchan
->index
, sg_len
, RCAR_DMAC_MAX_SG_LEN
);
1227 * Allocate the sg list dynamically as it would consume too much stack
1230 sgl
= kmalloc_array(sg_len
, sizeof(*sgl
), GFP_NOWAIT
);
1234 sg_init_table(sgl
, sg_len
);
1236 for (i
= 0; i
< sg_len
; ++i
) {
1237 dma_addr_t src
= buf_addr
+ (period_len
* i
);
1239 sg_set_page(&sgl
[i
], pfn_to_page(PFN_DOWN(src
)), period_len
,
1240 offset_in_page(src
));
1241 sg_dma_address(&sgl
[i
]) = src
;
1242 sg_dma_len(&sgl
[i
]) = period_len
;
1245 desc
= rcar_dmac_chan_prep_sg(rchan
, sgl
, sg_len
, rchan
->map
.addr
,
1252 static int rcar_dmac_device_config(struct dma_chan
*chan
,
1253 struct dma_slave_config
*cfg
)
1255 struct rcar_dmac_chan
*rchan
= to_rcar_dmac_chan(chan
);
1258 * We could lock this, but you shouldn't be configuring the
1259 * channel, while using it...
1261 rchan
->src
.slave_addr
= cfg
->src_addr
;
1262 rchan
->dst
.slave_addr
= cfg
->dst_addr
;
1263 rchan
->src
.xfer_size
= cfg
->src_addr_width
;
1264 rchan
->dst
.xfer_size
= cfg
->dst_addr_width
;
1269 static int rcar_dmac_chan_terminate_all(struct dma_chan
*chan
)
1271 struct rcar_dmac_chan
*rchan
= to_rcar_dmac_chan(chan
);
1272 unsigned long flags
;
1274 spin_lock_irqsave(&rchan
->lock
, flags
);
1275 rcar_dmac_chan_halt(rchan
);
1276 spin_unlock_irqrestore(&rchan
->lock
, flags
);
1279 * FIXME: No new interrupt can occur now, but the IRQ thread might still
1283 rcar_dmac_chan_reinit(rchan
);
1288 static unsigned int rcar_dmac_chan_get_residue(struct rcar_dmac_chan
*chan
,
1289 dma_cookie_t cookie
)
1291 struct rcar_dmac_desc
*desc
= chan
->desc
.running
;
1292 struct rcar_dmac_xfer_chunk
*running
= NULL
;
1293 struct rcar_dmac_xfer_chunk
*chunk
;
1294 enum dma_status status
;
1295 unsigned int residue
= 0;
1296 unsigned int dptr
= 0;
1305 * If the cookie corresponds to a descriptor that has been completed
1306 * there is no residue. The same check has already been performed by the
1307 * caller but without holding the channel lock, so the descriptor could
1310 status
= dma_cookie_status(&chan
->chan
, cookie
, NULL
);
1311 if (status
== DMA_COMPLETE
)
1315 * If the cookie doesn't correspond to the currently running transfer
1316 * then the descriptor hasn't been processed yet, and the residue is
1317 * equal to the full descriptor size.
1318 * Also, a client driver is possible to call this function before
1319 * rcar_dmac_isr_channel_thread() runs. In this case, the "desc.running"
1320 * will be the next descriptor, and the done list will appear. So, if
1321 * the argument cookie matches the done list's cookie, we can assume
1322 * the residue is zero.
1324 if (cookie
!= desc
->async_tx
.cookie
) {
1325 list_for_each_entry(desc
, &chan
->desc
.done
, node
) {
1326 if (cookie
== desc
->async_tx
.cookie
)
1329 list_for_each_entry(desc
, &chan
->desc
.pending
, node
) {
1330 if (cookie
== desc
->async_tx
.cookie
)
1333 list_for_each_entry(desc
, &chan
->desc
.active
, node
) {
1334 if (cookie
== desc
->async_tx
.cookie
)
1339 * No descriptor found for the cookie, there's thus no residue.
1340 * This shouldn't happen if the calling driver passes a correct
1343 WARN(1, "No descriptor for cookie!");
1348 * We need to read two registers.
1349 * Make sure the control register does not skip to next chunk
1350 * while reading the counter.
1351 * Trying it 3 times should be enough: Initial read, retry, retry
1354 for (i
= 0; i
< 3; i
++) {
1355 chcrb
= rcar_dmac_chan_read(chan
, RCAR_DMACHCRB
) &
1356 RCAR_DMACHCRB_DPTR_MASK
;
1357 tcrb
= rcar_dmac_chan_read(chan
, RCAR_DMATCRB
);
1358 /* Still the same? */
1359 if (chcrb
== (rcar_dmac_chan_read(chan
, RCAR_DMACHCRB
) &
1360 RCAR_DMACHCRB_DPTR_MASK
))
1363 WARN_ONCE(i
>= 3, "residue might be not continuous!");
1366 * In descriptor mode the descriptor running pointer is not maintained
1367 * by the interrupt handler, find the running descriptor from the
1368 * descriptor pointer field in the CHCRB register. In non-descriptor
1369 * mode just use the running descriptor pointer.
1371 if (desc
->hwdescs
.use
) {
1372 dptr
= chcrb
>> RCAR_DMACHCRB_DPTR_SHIFT
;
1374 dptr
= desc
->nchunks
;
1376 WARN_ON(dptr
>= desc
->nchunks
);
1378 running
= desc
->running
;
1381 /* Compute the size of all chunks still to be transferred. */
1382 list_for_each_entry_reverse(chunk
, &desc
->chunks
, node
) {
1383 if (chunk
== running
|| ++dptr
== desc
->nchunks
)
1386 residue
+= chunk
->size
;
1389 /* Add the residue for the current chunk. */
1390 residue
+= tcrb
<< desc
->xfer_shift
;
1395 static enum dma_status
rcar_dmac_tx_status(struct dma_chan
*chan
,
1396 dma_cookie_t cookie
,
1397 struct dma_tx_state
*txstate
)
1399 struct rcar_dmac_chan
*rchan
= to_rcar_dmac_chan(chan
);
1400 enum dma_status status
;
1401 unsigned long flags
;
1402 unsigned int residue
;
1405 status
= dma_cookie_status(chan
, cookie
, txstate
);
1406 if (status
== DMA_COMPLETE
|| !txstate
)
1409 spin_lock_irqsave(&rchan
->lock
, flags
);
1410 residue
= rcar_dmac_chan_get_residue(rchan
, cookie
);
1411 cyclic
= rchan
->desc
.running
? rchan
->desc
.running
->cyclic
: false;
1412 spin_unlock_irqrestore(&rchan
->lock
, flags
);
1414 /* if there's no residue, the cookie is complete */
1415 if (!residue
&& !cyclic
)
1416 return DMA_COMPLETE
;
1418 dma_set_residue(txstate
, residue
);
1423 static void rcar_dmac_issue_pending(struct dma_chan
*chan
)
1425 struct rcar_dmac_chan
*rchan
= to_rcar_dmac_chan(chan
);
1426 unsigned long flags
;
1428 spin_lock_irqsave(&rchan
->lock
, flags
);
1430 if (list_empty(&rchan
->desc
.pending
))
1433 /* Append the pending list to the active list. */
1434 list_splice_tail_init(&rchan
->desc
.pending
, &rchan
->desc
.active
);
1437 * If no transfer is running pick the first descriptor from the active
1438 * list and start the transfer.
1440 if (!rchan
->desc
.running
) {
1441 struct rcar_dmac_desc
*desc
;
1443 desc
= list_first_entry(&rchan
->desc
.active
,
1444 struct rcar_dmac_desc
, node
);
1445 rchan
->desc
.running
= desc
;
1447 rcar_dmac_chan_start_xfer(rchan
);
1451 spin_unlock_irqrestore(&rchan
->lock
, flags
);
1454 static void rcar_dmac_device_synchronize(struct dma_chan
*chan
)
1456 struct rcar_dmac_chan
*rchan
= to_rcar_dmac_chan(chan
);
1458 synchronize_irq(rchan
->irq
);
1461 /* -----------------------------------------------------------------------------
1465 static irqreturn_t
rcar_dmac_isr_desc_stage_end(struct rcar_dmac_chan
*chan
)
1467 struct rcar_dmac_desc
*desc
= chan
->desc
.running
;
1470 if (WARN_ON(!desc
|| !desc
->cyclic
)) {
1472 * This should never happen, there should always be a running
1473 * cyclic descriptor when a descriptor stage end interrupt is
1474 * triggered. Warn and return.
1479 /* Program the interrupt pointer to the next stage. */
1480 stage
= (rcar_dmac_chan_read(chan
, RCAR_DMACHCRB
) &
1481 RCAR_DMACHCRB_DPTR_MASK
) >> RCAR_DMACHCRB_DPTR_SHIFT
;
1482 rcar_dmac_chan_write(chan
, RCAR_DMADPCR
, RCAR_DMADPCR_DIPT(stage
));
1484 return IRQ_WAKE_THREAD
;
1487 static irqreturn_t
rcar_dmac_isr_transfer_end(struct rcar_dmac_chan
*chan
)
1489 struct rcar_dmac_desc
*desc
= chan
->desc
.running
;
1490 irqreturn_t ret
= IRQ_WAKE_THREAD
;
1492 if (WARN_ON_ONCE(!desc
)) {
1494 * This should never happen, there should always be a running
1495 * descriptor when a transfer end interrupt is triggered. Warn
1502 * The transfer end interrupt isn't generated for each chunk when using
1503 * descriptor mode. Only update the running chunk pointer in
1504 * non-descriptor mode.
1506 if (!desc
->hwdescs
.use
) {
1508 * If we haven't completed the last transfer chunk simply move
1509 * to the next one. Only wake the IRQ thread if the transfer is
1512 if (!list_is_last(&desc
->running
->node
, &desc
->chunks
)) {
1513 desc
->running
= list_next_entry(desc
->running
, node
);
1520 * We've completed the last transfer chunk. If the transfer is
1521 * cyclic, move back to the first one.
1525 list_first_entry(&desc
->chunks
,
1526 struct rcar_dmac_xfer_chunk
,
1532 /* The descriptor is complete, move it to the done list. */
1533 list_move_tail(&desc
->node
, &chan
->desc
.done
);
1535 /* Queue the next descriptor, if any. */
1536 if (!list_empty(&chan
->desc
.active
))
1537 chan
->desc
.running
= list_first_entry(&chan
->desc
.active
,
1538 struct rcar_dmac_desc
,
1541 chan
->desc
.running
= NULL
;
1544 if (chan
->desc
.running
)
1545 rcar_dmac_chan_start_xfer(chan
);
1550 static irqreturn_t
rcar_dmac_isr_channel(int irq
, void *dev
)
1552 u32 mask
= RCAR_DMACHCR_DSE
| RCAR_DMACHCR_TE
;
1553 struct rcar_dmac_chan
*chan
= dev
;
1554 irqreturn_t ret
= IRQ_NONE
;
1555 bool reinit
= false;
1558 spin_lock(&chan
->lock
);
1560 chcr
= rcar_dmac_chan_read(chan
, RCAR_DMACHCR
);
1561 if (chcr
& RCAR_DMACHCR_CAE
) {
1562 struct rcar_dmac
*dmac
= to_rcar_dmac(chan
->chan
.device
);
1565 * We don't need to call rcar_dmac_chan_halt()
1566 * because channel is already stopped in error case.
1567 * We need to clear register and check DE bit as recovery.
1569 rcar_dmac_write(dmac
, RCAR_DMACHCLR
, 1 << chan
->index
);
1570 rcar_dmac_chcr_de_barrier(chan
);
1575 if (chcr
& RCAR_DMACHCR_TE
)
1576 mask
|= RCAR_DMACHCR_DE
;
1577 rcar_dmac_chan_write(chan
, RCAR_DMACHCR
, chcr
& ~mask
);
1578 if (mask
& RCAR_DMACHCR_DE
)
1579 rcar_dmac_chcr_de_barrier(chan
);
1581 if (chcr
& RCAR_DMACHCR_DSE
)
1582 ret
|= rcar_dmac_isr_desc_stage_end(chan
);
1584 if (chcr
& RCAR_DMACHCR_TE
)
1585 ret
|= rcar_dmac_isr_transfer_end(chan
);
1588 spin_unlock(&chan
->lock
);
1591 dev_err(chan
->chan
.device
->dev
, "Channel Address Error\n");
1593 rcar_dmac_chan_reinit(chan
);
1600 static irqreturn_t
rcar_dmac_isr_channel_thread(int irq
, void *dev
)
1602 struct rcar_dmac_chan
*chan
= dev
;
1603 struct rcar_dmac_desc
*desc
;
1604 struct dmaengine_desc_callback cb
;
1606 spin_lock_irq(&chan
->lock
);
1608 /* For cyclic transfers notify the user after every chunk. */
1609 if (chan
->desc
.running
&& chan
->desc
.running
->cyclic
) {
1610 desc
= chan
->desc
.running
;
1611 dmaengine_desc_get_callback(&desc
->async_tx
, &cb
);
1613 if (dmaengine_desc_callback_valid(&cb
)) {
1614 spin_unlock_irq(&chan
->lock
);
1615 dmaengine_desc_callback_invoke(&cb
, NULL
);
1616 spin_lock_irq(&chan
->lock
);
1621 * Call the callback function for all descriptors on the done list and
1622 * move them to the ack wait list.
1624 while (!list_empty(&chan
->desc
.done
)) {
1625 desc
= list_first_entry(&chan
->desc
.done
, struct rcar_dmac_desc
,
1627 dma_cookie_complete(&desc
->async_tx
);
1628 list_del(&desc
->node
);
1630 dmaengine_desc_get_callback(&desc
->async_tx
, &cb
);
1631 if (dmaengine_desc_callback_valid(&cb
)) {
1632 spin_unlock_irq(&chan
->lock
);
1634 * We own the only reference to this descriptor, we can
1635 * safely dereference it without holding the channel
1638 dmaengine_desc_callback_invoke(&cb
, NULL
);
1639 spin_lock_irq(&chan
->lock
);
1642 list_add_tail(&desc
->node
, &chan
->desc
.wait
);
1645 spin_unlock_irq(&chan
->lock
);
1647 /* Recycle all acked descriptors. */
1648 rcar_dmac_desc_recycle_acked(chan
);
1653 /* -----------------------------------------------------------------------------
1654 * OF xlate and channel filter
1657 static bool rcar_dmac_chan_filter(struct dma_chan
*chan
, void *arg
)
1659 struct rcar_dmac
*dmac
= to_rcar_dmac(chan
->device
);
1660 struct of_phandle_args
*dma_spec
= arg
;
1663 * FIXME: Using a filter on OF platforms is a nonsense. The OF xlate
1664 * function knows from which device it wants to allocate a channel from,
1665 * and would be perfectly capable of selecting the channel it wants.
1666 * Forcing it to call dma_request_channel() and iterate through all
1667 * channels from all controllers is just pointless.
1669 if (chan
->device
->device_config
!= rcar_dmac_device_config
)
1672 return !test_and_set_bit(dma_spec
->args
[0], dmac
->modules
);
1675 static struct dma_chan
*rcar_dmac_of_xlate(struct of_phandle_args
*dma_spec
,
1676 struct of_dma
*ofdma
)
1678 struct rcar_dmac_chan
*rchan
;
1679 struct dma_chan
*chan
;
1680 dma_cap_mask_t mask
;
1682 if (dma_spec
->args_count
!= 1)
1685 /* Only slave DMA channels can be allocated via DT */
1687 dma_cap_set(DMA_SLAVE
, mask
);
1689 chan
= __dma_request_channel(&mask
, rcar_dmac_chan_filter
, dma_spec
,
1694 rchan
= to_rcar_dmac_chan(chan
);
1695 rchan
->mid_rid
= dma_spec
->args
[0];
1700 /* -----------------------------------------------------------------------------
1705 static int rcar_dmac_runtime_suspend(struct device
*dev
)
1710 static int rcar_dmac_runtime_resume(struct device
*dev
)
1712 struct rcar_dmac
*dmac
= dev_get_drvdata(dev
);
1714 return rcar_dmac_init(dmac
);
1718 static const struct dev_pm_ops rcar_dmac_pm
= {
1720 * TODO for system sleep/resume:
1721 * - Wait for the current transfer to complete and stop the device,
1722 * - Resume transfers, if any.
1724 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1725 pm_runtime_force_resume
)
1726 SET_RUNTIME_PM_OPS(rcar_dmac_runtime_suspend
, rcar_dmac_runtime_resume
,
1730 /* -----------------------------------------------------------------------------
1734 static int rcar_dmac_chan_probe(struct rcar_dmac
*dmac
,
1735 struct rcar_dmac_chan
*rchan
,
1736 const struct rcar_dmac_of_data
*data
,
1739 struct platform_device
*pdev
= to_platform_device(dmac
->dev
);
1740 struct dma_chan
*chan
= &rchan
->chan
;
1741 char pdev_irqname
[5];
1745 rchan
->index
= index
;
1746 rchan
->iomem
= dmac
->iomem
+ data
->chan_offset_base
+
1747 data
->chan_offset_stride
* index
;
1748 rchan
->mid_rid
= -EINVAL
;
1750 spin_lock_init(&rchan
->lock
);
1752 INIT_LIST_HEAD(&rchan
->desc
.free
);
1753 INIT_LIST_HEAD(&rchan
->desc
.pending
);
1754 INIT_LIST_HEAD(&rchan
->desc
.active
);
1755 INIT_LIST_HEAD(&rchan
->desc
.done
);
1756 INIT_LIST_HEAD(&rchan
->desc
.wait
);
1758 /* Request the channel interrupt. */
1759 sprintf(pdev_irqname
, "ch%u", index
);
1760 rchan
->irq
= platform_get_irq_byname(pdev
, pdev_irqname
);
1764 irqname
= devm_kasprintf(dmac
->dev
, GFP_KERNEL
, "%s:%u",
1765 dev_name(dmac
->dev
), index
);
1770 * Initialize the DMA engine channel and add it to the DMA engine
1773 chan
->device
= &dmac
->engine
;
1774 dma_cookie_init(chan
);
1776 list_add_tail(&chan
->device_node
, &dmac
->engine
.channels
);
1778 ret
= devm_request_threaded_irq(dmac
->dev
, rchan
->irq
,
1779 rcar_dmac_isr_channel
,
1780 rcar_dmac_isr_channel_thread
, 0,
1783 dev_err(dmac
->dev
, "failed to request IRQ %u (%d)\n",
1791 #define RCAR_DMAC_MAX_CHANNELS 32
1793 static int rcar_dmac_parse_of(struct device
*dev
, struct rcar_dmac
*dmac
)
1795 struct device_node
*np
= dev
->of_node
;
1798 ret
= of_property_read_u32(np
, "dma-channels", &dmac
->n_channels
);
1800 dev_err(dev
, "unable to read dma-channels property\n");
1804 /* The hardware and driver don't support more than 32 bits in CHCLR */
1805 if (dmac
->n_channels
<= 0 ||
1806 dmac
->n_channels
>= RCAR_DMAC_MAX_CHANNELS
) {
1807 dev_err(dev
, "invalid number of channels %u\n",
1813 * If the driver is unable to read dma-channel-mask property,
1814 * the driver assumes that it can use all channels.
1816 dmac
->channels_mask
= GENMASK(dmac
->n_channels
- 1, 0);
1817 of_property_read_u32(np
, "dma-channel-mask", &dmac
->channels_mask
);
1819 /* If the property has out-of-channel mask, this driver clears it */
1820 dmac
->channels_mask
&= GENMASK(dmac
->n_channels
- 1, 0);
1825 static int rcar_dmac_probe(struct platform_device
*pdev
)
1827 const enum dma_slave_buswidth widths
= DMA_SLAVE_BUSWIDTH_1_BYTE
|
1828 DMA_SLAVE_BUSWIDTH_2_BYTES
| DMA_SLAVE_BUSWIDTH_4_BYTES
|
1829 DMA_SLAVE_BUSWIDTH_8_BYTES
| DMA_SLAVE_BUSWIDTH_16_BYTES
|
1830 DMA_SLAVE_BUSWIDTH_32_BYTES
| DMA_SLAVE_BUSWIDTH_64_BYTES
;
1831 struct dma_device
*engine
;
1832 struct rcar_dmac
*dmac
;
1833 const struct rcar_dmac_of_data
*data
;
1837 data
= of_device_get_match_data(&pdev
->dev
);
1841 dmac
= devm_kzalloc(&pdev
->dev
, sizeof(*dmac
), GFP_KERNEL
);
1845 dmac
->dev
= &pdev
->dev
;
1846 platform_set_drvdata(pdev
, dmac
);
1847 dma_set_max_seg_size(dmac
->dev
, RCAR_DMATCR_MASK
);
1848 dma_set_mask_and_coherent(dmac
->dev
, DMA_BIT_MASK(40));
1850 ret
= rcar_dmac_parse_of(&pdev
->dev
, dmac
);
1855 * A still unconfirmed hardware bug prevents the IPMMU microTLB 0 to be
1856 * flushed correctly, resulting in memory corruption. DMAC 0 channel 0
1857 * is connected to microTLB 0 on currently supported platforms, so we
1858 * can't use it with the IPMMU. As the IOMMU API operates at the device
1859 * level we can't disable it selectively, so ignore channel 0 for now if
1860 * the device is part of an IOMMU group.
1862 if (device_iommu_mapped(&pdev
->dev
))
1863 dmac
->channels_mask
&= ~BIT(0);
1865 dmac
->channels
= devm_kcalloc(&pdev
->dev
, dmac
->n_channels
,
1866 sizeof(*dmac
->channels
), GFP_KERNEL
);
1867 if (!dmac
->channels
)
1870 /* Request resources. */
1871 dmac
->iomem
= devm_platform_ioremap_resource(pdev
, 0);
1872 if (IS_ERR(dmac
->iomem
))
1873 return PTR_ERR(dmac
->iomem
);
1875 /* Enable runtime PM and initialize the device. */
1876 pm_runtime_enable(&pdev
->dev
);
1877 ret
= pm_runtime_get_sync(&pdev
->dev
);
1879 dev_err(&pdev
->dev
, "runtime PM get sync failed (%d)\n", ret
);
1883 ret
= rcar_dmac_init(dmac
);
1884 pm_runtime_put(&pdev
->dev
);
1887 dev_err(&pdev
->dev
, "failed to reset device\n");
1891 /* Initialize engine */
1892 engine
= &dmac
->engine
;
1894 dma_cap_set(DMA_MEMCPY
, engine
->cap_mask
);
1895 dma_cap_set(DMA_SLAVE
, engine
->cap_mask
);
1897 engine
->dev
= &pdev
->dev
;
1898 engine
->copy_align
= ilog2(RCAR_DMAC_MEMCPY_XFER_SIZE
);
1900 engine
->src_addr_widths
= widths
;
1901 engine
->dst_addr_widths
= widths
;
1902 engine
->directions
= BIT(DMA_MEM_TO_DEV
) | BIT(DMA_DEV_TO_MEM
);
1903 engine
->residue_granularity
= DMA_RESIDUE_GRANULARITY_BURST
;
1905 engine
->device_alloc_chan_resources
= rcar_dmac_alloc_chan_resources
;
1906 engine
->device_free_chan_resources
= rcar_dmac_free_chan_resources
;
1907 engine
->device_prep_dma_memcpy
= rcar_dmac_prep_dma_memcpy
;
1908 engine
->device_prep_slave_sg
= rcar_dmac_prep_slave_sg
;
1909 engine
->device_prep_dma_cyclic
= rcar_dmac_prep_dma_cyclic
;
1910 engine
->device_config
= rcar_dmac_device_config
;
1911 engine
->device_pause
= rcar_dmac_chan_pause
;
1912 engine
->device_terminate_all
= rcar_dmac_chan_terminate_all
;
1913 engine
->device_tx_status
= rcar_dmac_tx_status
;
1914 engine
->device_issue_pending
= rcar_dmac_issue_pending
;
1915 engine
->device_synchronize
= rcar_dmac_device_synchronize
;
1917 INIT_LIST_HEAD(&engine
->channels
);
1919 for (i
= 0; i
< dmac
->n_channels
; ++i
) {
1920 if (!(dmac
->channels_mask
& BIT(i
)))
1923 ret
= rcar_dmac_chan_probe(dmac
, &dmac
->channels
[i
], data
, i
);
1928 /* Register the DMAC as a DMA provider for DT. */
1929 ret
= of_dma_controller_register(pdev
->dev
.of_node
, rcar_dmac_of_xlate
,
1935 * Register the DMA engine device.
1937 * Default transfer size of 32 bytes requires 32-byte alignment.
1939 ret
= dma_async_device_register(engine
);
1946 of_dma_controller_free(pdev
->dev
.of_node
);
1947 pm_runtime_disable(&pdev
->dev
);
1951 static int rcar_dmac_remove(struct platform_device
*pdev
)
1953 struct rcar_dmac
*dmac
= platform_get_drvdata(pdev
);
1955 of_dma_controller_free(pdev
->dev
.of_node
);
1956 dma_async_device_unregister(&dmac
->engine
);
1958 pm_runtime_disable(&pdev
->dev
);
1963 static void rcar_dmac_shutdown(struct platform_device
*pdev
)
1965 struct rcar_dmac
*dmac
= platform_get_drvdata(pdev
);
1967 rcar_dmac_stop_all_chan(dmac
);
1970 static const struct rcar_dmac_of_data rcar_dmac_data
= {
1971 .chan_offset_base
= 0x8000,
1972 .chan_offset_stride
= 0x80,
1975 static const struct of_device_id rcar_dmac_of_ids
[] = {
1977 .compatible
= "renesas,rcar-dmac",
1978 .data
= &rcar_dmac_data
,
1982 MODULE_DEVICE_TABLE(of
, rcar_dmac_of_ids
);
1984 static struct platform_driver rcar_dmac_driver
= {
1986 .pm
= &rcar_dmac_pm
,
1987 .name
= "rcar-dmac",
1988 .of_match_table
= rcar_dmac_of_ids
,
1990 .probe
= rcar_dmac_probe
,
1991 .remove
= rcar_dmac_remove
,
1992 .shutdown
= rcar_dmac_shutdown
,
1995 module_platform_driver(rcar_dmac_driver
);
1997 MODULE_DESCRIPTION("R-Car Gen2 DMA Controller Driver");
1998 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1999 MODULE_LICENSE("GPL v2");