2 * BCM2835 DMA engine support
4 * This driver only supports cyclic DMA transfers
5 * as needed for the I2S module.
7 * Author: Florian Meier <florian.meier@koalo.de>
11 * OMAP DMAengine support by Russell King
14 * Copyright (C) 2010 Broadcom
16 * Raspberry Pi PCM I2S ALSA Driver
17 * Copyright (c) by Phil Poole 2013
19 * MARVELL MMP Peripheral DMA Driver
20 * Copyright 2012 Marvell International Ltd.
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 #include <linux/dmaengine.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/err.h>
35 #include <linux/init.h>
36 #include <linux/interrupt.h>
37 #include <linux/list.h>
38 #include <linux/module.h>
39 #include <linux/platform_device.h>
40 #include <linux/slab.h>
42 #include <linux/spinlock.h>
44 #include <linux/of_dma.h>
48 struct bcm2835_dmadev
{
49 struct dma_device ddev
;
52 struct device_dma_parameters dma_parms
;
55 struct bcm2835_dma_cb
{
66 struct virt_dma_chan vc
;
67 struct list_head node
;
69 struct dma_slave_config cfg
;
74 struct bcm2835_desc
*desc
;
76 void __iomem
*chan_base
;
81 struct virt_dma_desc vd
;
82 enum dma_transfer_direction dir
;
84 unsigned int control_block_size
;
85 struct bcm2835_dma_cb
*control_block_base
;
86 dma_addr_t control_block_base_phys
;
92 #define BCM2835_DMA_CS 0x00
93 #define BCM2835_DMA_ADDR 0x04
94 #define BCM2835_DMA_SOURCE_AD 0x0c
95 #define BCM2835_DMA_DEST_AD 0x10
96 #define BCM2835_DMA_NEXTCB 0x1C
98 /* DMA CS Control and Status bits */
99 #define BCM2835_DMA_ACTIVE BIT(0)
100 #define BCM2835_DMA_INT BIT(2)
101 #define BCM2835_DMA_ISPAUSED BIT(4) /* Pause requested or not active */
102 #define BCM2835_DMA_ISHELD BIT(5) /* Is held by DREQ flow control */
103 #define BCM2835_DMA_ERR BIT(8)
104 #define BCM2835_DMA_ABORT BIT(30) /* Stop current CB, go to next, WO */
105 #define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */
107 #define BCM2835_DMA_INT_EN BIT(0)
108 #define BCM2835_DMA_D_INC BIT(4)
109 #define BCM2835_DMA_D_DREQ BIT(6)
110 #define BCM2835_DMA_S_INC BIT(8)
111 #define BCM2835_DMA_S_DREQ BIT(10)
113 #define BCM2835_DMA_PER_MAP(x) ((x) << 16)
115 #define BCM2835_DMA_DATA_TYPE_S8 1
116 #define BCM2835_DMA_DATA_TYPE_S16 2
117 #define BCM2835_DMA_DATA_TYPE_S32 4
118 #define BCM2835_DMA_DATA_TYPE_S128 16
120 #define BCM2835_DMA_BULK_MASK BIT(0)
121 #define BCM2835_DMA_FIQ_MASK (BIT(2) | BIT(3))
123 /* Valid only for channels 0 - 14, 15 has its own base address */
124 #define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
125 #define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
127 static inline struct bcm2835_dmadev
*to_bcm2835_dma_dev(struct dma_device
*d
)
129 return container_of(d
, struct bcm2835_dmadev
, ddev
);
132 static inline struct bcm2835_chan
*to_bcm2835_dma_chan(struct dma_chan
*c
)
134 return container_of(c
, struct bcm2835_chan
, vc
.chan
);
137 static inline struct bcm2835_desc
*to_bcm2835_dma_desc(
138 struct dma_async_tx_descriptor
*t
)
140 return container_of(t
, struct bcm2835_desc
, vd
.tx
);
143 static void bcm2835_dma_desc_free(struct virt_dma_desc
*vd
)
145 struct bcm2835_desc
*desc
= container_of(vd
, struct bcm2835_desc
, vd
);
146 dma_free_coherent(desc
->vd
.tx
.chan
->device
->dev
,
147 desc
->control_block_size
,
148 desc
->control_block_base
,
149 desc
->control_block_base_phys
);
153 static int bcm2835_dma_abort(void __iomem
*chan_base
)
156 long int timeout
= 10000;
158 cs
= readl(chan_base
+ BCM2835_DMA_CS
);
159 if (!(cs
& BCM2835_DMA_ACTIVE
))
162 /* Write 0 to the active bit - Pause the DMA */
163 writel(0, chan_base
+ BCM2835_DMA_CS
);
165 /* Wait for any current AXI transfer to complete */
166 while ((cs
& BCM2835_DMA_ISPAUSED
) && --timeout
) {
168 cs
= readl(chan_base
+ BCM2835_DMA_CS
);
171 /* We'll un-pause when we set of our next DMA */
175 if (!(cs
& BCM2835_DMA_ACTIVE
))
178 /* Terminate the control block chain */
179 writel(0, chan_base
+ BCM2835_DMA_NEXTCB
);
181 /* Abort the whole DMA */
182 writel(BCM2835_DMA_ABORT
| BCM2835_DMA_ACTIVE
,
183 chan_base
+ BCM2835_DMA_CS
);
188 static void bcm2835_dma_start_desc(struct bcm2835_chan
*c
)
190 struct virt_dma_desc
*vd
= vchan_next_desc(&c
->vc
);
191 struct bcm2835_desc
*d
;
200 c
->desc
= d
= to_bcm2835_dma_desc(&vd
->tx
);
202 writel(d
->control_block_base_phys
, c
->chan_base
+ BCM2835_DMA_ADDR
);
203 writel(BCM2835_DMA_ACTIVE
, c
->chan_base
+ BCM2835_DMA_CS
);
206 static irqreturn_t
bcm2835_dma_callback(int irq
, void *data
)
208 struct bcm2835_chan
*c
= data
;
209 struct bcm2835_desc
*d
;
212 spin_lock_irqsave(&c
->vc
.lock
, flags
);
214 /* Acknowledge interrupt */
215 writel(BCM2835_DMA_INT
, c
->chan_base
+ BCM2835_DMA_CS
);
220 /* TODO Only works for cyclic DMA */
221 vchan_cyclic_callback(&d
->vd
);
224 /* Keep the DMA engine running */
225 writel(BCM2835_DMA_ACTIVE
, c
->chan_base
+ BCM2835_DMA_CS
);
227 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
232 static int bcm2835_dma_alloc_chan_resources(struct dma_chan
*chan
)
234 struct bcm2835_chan
*c
= to_bcm2835_dma_chan(chan
);
236 dev_dbg(c
->vc
.chan
.device
->dev
,
237 "Allocating DMA channel %d\n", c
->ch
);
239 return request_irq(c
->irq_number
,
240 bcm2835_dma_callback
, 0, "DMA IRQ", c
);
243 static void bcm2835_dma_free_chan_resources(struct dma_chan
*chan
)
245 struct bcm2835_chan
*c
= to_bcm2835_dma_chan(chan
);
247 vchan_free_chan_resources(&c
->vc
);
248 free_irq(c
->irq_number
, c
);
250 dev_dbg(c
->vc
.chan
.device
->dev
, "Freeing DMA channel %u\n", c
->ch
);
253 static size_t bcm2835_dma_desc_size(struct bcm2835_desc
*d
)
258 static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc
*d
, dma_addr_t addr
)
263 for (size
= i
= 0; i
< d
->frames
; i
++) {
264 struct bcm2835_dma_cb
*control_block
=
265 &d
->control_block_base
[i
];
266 size_t this_size
= control_block
->length
;
269 if (d
->dir
== DMA_DEV_TO_MEM
)
270 dma
= control_block
->dst
;
272 dma
= control_block
->src
;
276 else if (addr
>= dma
&& addr
< dma
+ this_size
)
277 size
+= dma
+ this_size
- addr
;
283 static enum dma_status
bcm2835_dma_tx_status(struct dma_chan
*chan
,
284 dma_cookie_t cookie
, struct dma_tx_state
*txstate
)
286 struct bcm2835_chan
*c
= to_bcm2835_dma_chan(chan
);
287 struct virt_dma_desc
*vd
;
291 ret
= dma_cookie_status(chan
, cookie
, txstate
);
292 if (ret
== DMA_COMPLETE
|| !txstate
)
295 spin_lock_irqsave(&c
->vc
.lock
, flags
);
296 vd
= vchan_find_desc(&c
->vc
, cookie
);
299 bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd
->tx
));
300 } else if (c
->desc
&& c
->desc
->vd
.tx
.cookie
== cookie
) {
301 struct bcm2835_desc
*d
= c
->desc
;
304 if (d
->dir
== DMA_MEM_TO_DEV
)
305 pos
= readl(c
->chan_base
+ BCM2835_DMA_SOURCE_AD
);
306 else if (d
->dir
== DMA_DEV_TO_MEM
)
307 pos
= readl(c
->chan_base
+ BCM2835_DMA_DEST_AD
);
311 txstate
->residue
= bcm2835_dma_desc_size_pos(d
, pos
);
313 txstate
->residue
= 0;
316 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
321 static void bcm2835_dma_issue_pending(struct dma_chan
*chan
)
323 struct bcm2835_chan
*c
= to_bcm2835_dma_chan(chan
);
326 c
->cyclic
= true; /* Nothing else is implemented */
328 spin_lock_irqsave(&c
->vc
.lock
, flags
);
329 if (vchan_issue_pending(&c
->vc
) && !c
->desc
)
330 bcm2835_dma_start_desc(c
);
332 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
335 static struct dma_async_tx_descriptor
*bcm2835_dma_prep_dma_cyclic(
336 struct dma_chan
*chan
, dma_addr_t buf_addr
, size_t buf_len
,
337 size_t period_len
, enum dma_transfer_direction direction
,
340 struct bcm2835_chan
*c
= to_bcm2835_dma_chan(chan
);
341 enum dma_slave_buswidth dev_width
;
342 struct bcm2835_desc
*d
;
344 unsigned int es
, sync_type
;
347 /* Grab configuration */
348 if (!is_slave_direction(direction
)) {
349 dev_err(chan
->device
->dev
, "%s: bad direction?\n", __func__
);
353 if (direction
== DMA_DEV_TO_MEM
) {
354 dev_addr
= c
->cfg
.src_addr
;
355 dev_width
= c
->cfg
.src_addr_width
;
356 sync_type
= BCM2835_DMA_S_DREQ
;
358 dev_addr
= c
->cfg
.dst_addr
;
359 dev_width
= c
->cfg
.dst_addr_width
;
360 sync_type
= BCM2835_DMA_D_DREQ
;
363 /* Bus width translates to the element size (ES) */
365 case DMA_SLAVE_BUSWIDTH_4_BYTES
:
366 es
= BCM2835_DMA_DATA_TYPE_S32
;
372 /* Now allocate and setup the descriptor. */
373 d
= kzalloc(sizeof(*d
), GFP_NOWAIT
);
378 d
->frames
= buf_len
/ period_len
;
380 /* Allocate memory for control blocks */
381 d
->control_block_size
= d
->frames
* sizeof(struct bcm2835_dma_cb
);
382 d
->control_block_base
= dma_zalloc_coherent(chan
->device
->dev
,
383 d
->control_block_size
, &d
->control_block_base_phys
,
386 if (!d
->control_block_base
) {
392 * Iterate over all frames, create a control block
393 * for each frame and link them together.
395 for (frame
= 0; frame
< d
->frames
; frame
++) {
396 struct bcm2835_dma_cb
*control_block
=
397 &d
->control_block_base
[frame
];
400 if (d
->dir
== DMA_DEV_TO_MEM
) {
401 control_block
->info
= BCM2835_DMA_D_INC
;
402 control_block
->src
= dev_addr
;
403 control_block
->dst
= buf_addr
+ frame
* period_len
;
405 control_block
->info
= BCM2835_DMA_S_INC
;
406 control_block
->src
= buf_addr
+ frame
* period_len
;
407 control_block
->dst
= dev_addr
;
410 /* Enable interrupt */
411 control_block
->info
|= BCM2835_DMA_INT_EN
;
413 /* Setup synchronization */
415 control_block
->info
|= sync_type
;
417 /* Setup DREQ channel */
419 control_block
->info
|=
420 BCM2835_DMA_PER_MAP(c
->dreq
);
422 /* Length of a frame */
423 control_block
->length
= period_len
;
424 d
->size
+= control_block
->length
;
427 * Next block is the next frame.
428 * This DMA engine driver currently only supports cyclic DMA.
429 * Therefore, wrap around at number of frames.
431 control_block
->next
= d
->control_block_base_phys
+
432 sizeof(struct bcm2835_dma_cb
)
433 * ((frame
+ 1) % d
->frames
);
436 return vchan_tx_prep(&c
->vc
, &d
->vd
, flags
);
439 static int bcm2835_dma_slave_config(struct dma_chan
*chan
,
440 struct dma_slave_config
*cfg
)
442 struct bcm2835_chan
*c
= to_bcm2835_dma_chan(chan
);
444 if ((cfg
->direction
== DMA_DEV_TO_MEM
&&
445 cfg
->src_addr_width
!= DMA_SLAVE_BUSWIDTH_4_BYTES
) ||
446 (cfg
->direction
== DMA_MEM_TO_DEV
&&
447 cfg
->dst_addr_width
!= DMA_SLAVE_BUSWIDTH_4_BYTES
) ||
448 !is_slave_direction(cfg
->direction
)) {
457 static int bcm2835_dma_terminate_all(struct dma_chan
*chan
)
459 struct bcm2835_chan
*c
= to_bcm2835_dma_chan(chan
);
460 struct bcm2835_dmadev
*d
= to_bcm2835_dma_dev(c
->vc
.chan
.device
);
465 spin_lock_irqsave(&c
->vc
.lock
, flags
);
467 /* Prevent this channel being scheduled */
469 list_del_init(&c
->node
);
470 spin_unlock(&d
->lock
);
473 * Stop DMA activity: we assume the callback will not be called
474 * after bcm_dma_abort() returns (even if it does, it will see
475 * c->desc is NULL and exit.)
478 bcm2835_dma_desc_free(&c
->desc
->vd
);
480 bcm2835_dma_abort(c
->chan_base
);
482 /* Wait for stopping */
484 if (!(readl(c
->chan_base
+ BCM2835_DMA_CS
) &
492 dev_err(d
->ddev
.dev
, "DMA transfer could not be terminated\n");
495 vchan_get_all_descriptors(&c
->vc
, &head
);
496 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
497 vchan_dma_desc_free_list(&c
->vc
, &head
);
502 static int bcm2835_dma_chan_init(struct bcm2835_dmadev
*d
, int chan_id
, int irq
)
504 struct bcm2835_chan
*c
;
506 c
= devm_kzalloc(d
->ddev
.dev
, sizeof(*c
), GFP_KERNEL
);
510 c
->vc
.desc_free
= bcm2835_dma_desc_free
;
511 vchan_init(&c
->vc
, &d
->ddev
);
512 INIT_LIST_HEAD(&c
->node
);
514 c
->chan_base
= BCM2835_DMA_CHANIO(d
->base
, chan_id
);
521 static void bcm2835_dma_free(struct bcm2835_dmadev
*od
)
523 struct bcm2835_chan
*c
, *next
;
525 list_for_each_entry_safe(c
, next
, &od
->ddev
.channels
,
526 vc
.chan
.device_node
) {
527 list_del(&c
->vc
.chan
.device_node
);
528 tasklet_kill(&c
->vc
.task
);
532 static const struct of_device_id bcm2835_dma_of_match
[] = {
533 { .compatible
= "brcm,bcm2835-dma", },
536 MODULE_DEVICE_TABLE(of
, bcm2835_dma_of_match
);
538 static struct dma_chan
*bcm2835_dma_xlate(struct of_phandle_args
*spec
,
539 struct of_dma
*ofdma
)
541 struct bcm2835_dmadev
*d
= ofdma
->of_dma_data
;
542 struct dma_chan
*chan
;
544 chan
= dma_get_any_slave_channel(&d
->ddev
);
548 /* Set DREQ from param */
549 to_bcm2835_dma_chan(chan
)->dreq
= spec
->args
[0];
554 static int bcm2835_dma_probe(struct platform_device
*pdev
)
556 struct bcm2835_dmadev
*od
;
557 struct resource
*res
;
562 uint32_t chans_available
;
564 if (!pdev
->dev
.dma_mask
)
565 pdev
->dev
.dma_mask
= &pdev
->dev
.coherent_dma_mask
;
567 rc
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(32));
571 od
= devm_kzalloc(&pdev
->dev
, sizeof(*od
), GFP_KERNEL
);
575 pdev
->dev
.dma_parms
= &od
->dma_parms
;
576 dma_set_max_seg_size(&pdev
->dev
, 0x3FFFFFFF);
578 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
579 base
= devm_ioremap_resource(&pdev
->dev
, res
);
581 return PTR_ERR(base
);
585 dma_cap_set(DMA_SLAVE
, od
->ddev
.cap_mask
);
586 dma_cap_set(DMA_PRIVATE
, od
->ddev
.cap_mask
);
587 dma_cap_set(DMA_CYCLIC
, od
->ddev
.cap_mask
);
588 od
->ddev
.device_alloc_chan_resources
= bcm2835_dma_alloc_chan_resources
;
589 od
->ddev
.device_free_chan_resources
= bcm2835_dma_free_chan_resources
;
590 od
->ddev
.device_tx_status
= bcm2835_dma_tx_status
;
591 od
->ddev
.device_issue_pending
= bcm2835_dma_issue_pending
;
592 od
->ddev
.device_prep_dma_cyclic
= bcm2835_dma_prep_dma_cyclic
;
593 od
->ddev
.device_config
= bcm2835_dma_slave_config
;
594 od
->ddev
.device_terminate_all
= bcm2835_dma_terminate_all
;
595 od
->ddev
.src_addr_widths
= BIT(DMA_SLAVE_BUSWIDTH_4_BYTES
);
596 od
->ddev
.dst_addr_widths
= BIT(DMA_SLAVE_BUSWIDTH_4_BYTES
);
597 od
->ddev
.directions
= BIT(DMA_DEV_TO_MEM
) | BIT(DMA_MEM_TO_DEV
);
598 od
->ddev
.dev
= &pdev
->dev
;
599 INIT_LIST_HEAD(&od
->ddev
.channels
);
600 spin_lock_init(&od
->lock
);
602 platform_set_drvdata(pdev
, od
);
604 /* Request DMA channel mask from device tree */
605 if (of_property_read_u32(pdev
->dev
.of_node
,
606 "brcm,dma-channel-mask",
608 dev_err(&pdev
->dev
, "Failed to get channel mask\n");
614 * Do not use the FIQ and BULK channels,
615 * because they are used by the GPU.
617 chans_available
&= ~(BCM2835_DMA_FIQ_MASK
| BCM2835_DMA_BULK_MASK
);
619 for (i
= 0; i
< pdev
->num_resources
; i
++) {
620 irq
= platform_get_irq(pdev
, i
);
624 if (chans_available
& (1 << i
)) {
625 rc
= bcm2835_dma_chan_init(od
, i
, irq
);
631 dev_dbg(&pdev
->dev
, "Initialized %i DMA channels\n", i
);
633 /* Device-tree DMA controller registration */
634 rc
= of_dma_controller_register(pdev
->dev
.of_node
,
635 bcm2835_dma_xlate
, od
);
637 dev_err(&pdev
->dev
, "Failed to register DMA controller\n");
641 rc
= dma_async_device_register(&od
->ddev
);
644 "Failed to register slave DMA engine device: %d\n", rc
);
648 dev_dbg(&pdev
->dev
, "Load BCM2835 DMA engine driver\n");
653 bcm2835_dma_free(od
);
657 static int bcm2835_dma_remove(struct platform_device
*pdev
)
659 struct bcm2835_dmadev
*od
= platform_get_drvdata(pdev
);
661 dma_async_device_unregister(&od
->ddev
);
662 bcm2835_dma_free(od
);
667 static struct platform_driver bcm2835_dma_driver
= {
668 .probe
= bcm2835_dma_probe
,
669 .remove
= bcm2835_dma_remove
,
671 .name
= "bcm2835-dma",
672 .of_match_table
= of_match_ptr(bcm2835_dma_of_match
),
676 module_platform_driver(bcm2835_dma_driver
);
678 MODULE_ALIAS("platform:bcm2835-dma");
679 MODULE_DESCRIPTION("BCM2835 DMA engine driver");
680 MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
681 MODULE_LICENSE("GPL v2");