1 // SPDX-License-Identifier: GPL-2.0+
3 * BCM2835 DMA engine support
5 * Author: Florian Meier <florian.meier@koalo.de>
9 * OMAP DMAengine support by Russell King
12 * Copyright (C) 2010 Broadcom
14 * Raspberry Pi PCM I2S ALSA Driver
15 * Copyright (c) by Phil Poole 2013
17 * MARVELL MMP Peripheral DMA Driver
18 * Copyright 2012 Marvell International Ltd.
20 #include <linux/dmaengine.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/dmapool.h>
23 #include <linux/err.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
31 #include <linux/spinlock.h>
33 #include <linux/of_dma.h>
37 #define BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED 14
38 #define BCM2835_DMA_CHAN_NAME_SIZE 8
41 * struct bcm2835_dmadev - BCM2835 DMA controller
43 * @base: base address of register map
44 * @dma_parms: DMA parameters (to convey 1 GByte max segment size to clients)
45 * @zero_page: bus address of zero page (to detect transactions copying from
46 * zero page and avoid accessing memory if so)
48 struct bcm2835_dmadev {
49 struct dma_device ddev;
51 struct device_dma_parameters dma_parms;
55 struct bcm2835_dma_cb {
65 struct bcm2835_cb_entry {
66 struct bcm2835_dma_cb *cb;
71 struct virt_dma_chan vc;
73 struct dma_slave_config cfg;
77 struct bcm2835_desc *desc;
78 struct dma_pool *cb_pool;
80 void __iomem *chan_base;
82 unsigned int irq_flags;
88 struct bcm2835_chan *c;
89 struct virt_dma_desc vd;
90 enum dma_transfer_direction dir;
97 struct bcm2835_cb_entry cb_list[];
100 #define BCM2835_DMA_CS 0x00
101 #define BCM2835_DMA_ADDR 0x04
102 #define BCM2835_DMA_TI 0x08
103 #define BCM2835_DMA_SOURCE_AD 0x0c
104 #define BCM2835_DMA_DEST_AD 0x10
105 #define BCM2835_DMA_LEN 0x14
106 #define BCM2835_DMA_STRIDE 0x18
107 #define BCM2835_DMA_NEXTCB 0x1c
108 #define BCM2835_DMA_DEBUG 0x20
110 /* DMA CS Control and Status bits */
111 #define BCM2835_DMA_ACTIVE BIT(0) /* activate the DMA */
112 #define BCM2835_DMA_END BIT(1) /* current CB has ended */
113 #define BCM2835_DMA_INT BIT(2) /* interrupt status */
114 #define BCM2835_DMA_DREQ BIT(3) /* DREQ state */
115 #define BCM2835_DMA_ISPAUSED BIT(4) /* Pause requested or not active */
116 #define BCM2835_DMA_ISHELD BIT(5) /* Is held by DREQ flow control */
117 #define BCM2835_DMA_WAITING_FOR_WRITES BIT(6) /* waiting for last
120 #define BCM2835_DMA_ERR BIT(8)
121 #define BCM2835_DMA_PRIORITY(x) ((x & 15) << 16) /* AXI priority */
122 #define BCM2835_DMA_PANIC_PRIORITY(x) ((x & 15) << 20) /* panic priority */
123 /* current value of TI.BCM2835_DMA_WAIT_RESP */
124 #define BCM2835_DMA_WAIT_FOR_WRITES BIT(28)
125 #define BCM2835_DMA_DIS_DEBUG BIT(29) /* disable debug pause signal */
126 #define BCM2835_DMA_ABORT BIT(30) /* Stop current CB, go to next, WO */
127 #define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */
129 /* Transfer information bits - also bcm2835_cb.info field */
130 #define BCM2835_DMA_INT_EN BIT(0)
131 #define BCM2835_DMA_TDMODE BIT(1) /* 2D-Mode */
132 #define BCM2835_DMA_WAIT_RESP BIT(3) /* wait for AXI-write to be acked */
133 #define BCM2835_DMA_D_INC BIT(4)
134 #define BCM2835_DMA_D_WIDTH BIT(5) /* 128bit writes if set */
135 #define BCM2835_DMA_D_DREQ BIT(6) /* enable DREQ for destination */
136 #define BCM2835_DMA_D_IGNORE BIT(7) /* ignore destination writes */
137 #define BCM2835_DMA_S_INC BIT(8)
138 #define BCM2835_DMA_S_WIDTH BIT(9) /* 128bit writes if set */
139 #define BCM2835_DMA_S_DREQ BIT(10) /* enable SREQ for source */
140 #define BCM2835_DMA_S_IGNORE BIT(11) /* ignore source reads - read 0 */
141 #define BCM2835_DMA_BURST_LENGTH(x) ((x & 15) << 12)
142 #define BCM2835_DMA_PER_MAP(x) ((x & 31) << 16) /* REQ source */
143 #define BCM2835_DMA_WAIT(x) ((x & 31) << 21) /* add DMA-wait cycles */
144 #define BCM2835_DMA_NO_WIDE_BURSTS BIT(26) /* no 2 beat write bursts */
146 /* debug register bits */
147 #define BCM2835_DMA_DEBUG_LAST_NOT_SET_ERR BIT(0)
148 #define BCM2835_DMA_DEBUG_FIFO_ERR BIT(1)
149 #define BCM2835_DMA_DEBUG_READ_ERR BIT(2)
150 #define BCM2835_DMA_DEBUG_OUTSTANDING_WRITES_SHIFT 4
151 #define BCM2835_DMA_DEBUG_OUTSTANDING_WRITES_BITS 4
152 #define BCM2835_DMA_DEBUG_ID_SHIFT 16
153 #define BCM2835_DMA_DEBUG_ID_BITS 9
154 #define BCM2835_DMA_DEBUG_STATE_SHIFT 16
155 #define BCM2835_DMA_DEBUG_STATE_BITS 9
156 #define BCM2835_DMA_DEBUG_VERSION_SHIFT 25
157 #define BCM2835_DMA_DEBUG_VERSION_BITS 3
158 #define BCM2835_DMA_DEBUG_LITE BIT(28)
160 /* shared registers for all dma channels */
161 #define BCM2835_DMA_INT_STATUS 0xfe0
162 #define BCM2835_DMA_ENABLE 0xff0
164 #define BCM2835_DMA_DATA_TYPE_S8 1
165 #define BCM2835_DMA_DATA_TYPE_S16 2
166 #define BCM2835_DMA_DATA_TYPE_S32 4
167 #define BCM2835_DMA_DATA_TYPE_S128 16
169 /* Valid only for channels 0 - 14, 15 has its own base address */
170 #define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
171 #define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
173 /* the max dma length for different channels */
174 #define MAX_DMA_LEN SZ_1G
175 #define MAX_LITE_DMA_LEN (SZ_64K - 4)
177 static inline size_t bcm2835_dma_max_frame_length(struct bcm2835_chan *c)
179 /* lite and normal channels have different max frame length */
180 return c->is_lite_channel ? MAX_LITE_DMA_LEN : MAX_DMA_LEN;
183 /* how many frames of max_len size do we need to transfer len bytes */
184 static inline size_t bcm2835_dma_frames_for_length(size_t len,
187 return DIV_ROUND_UP(len, max_len);
190 static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d)
192 return container_of(d, struct bcm2835_dmadev, ddev);
195 static inline struct bcm2835_chan *to_bcm2835_dma_chan(struct dma_chan *c)
197 return container_of(c, struct bcm2835_chan, vc.chan);
200 static inline struct bcm2835_desc *to_bcm2835_dma_desc(
201 struct dma_async_tx_descriptor *t)
203 return container_of(t, struct bcm2835_desc, vd.tx);
206 static void bcm2835_dma_free_cb_chain(struct bcm2835_desc *desc)
210 for (i = 0; i < desc->frames; i++)
211 dma_pool_free(desc->c->cb_pool, desc->cb_list[i].cb,
212 desc->cb_list[i].paddr);
217 static void bcm2835_dma_desc_free(struct virt_dma_desc *vd)
219 bcm2835_dma_free_cb_chain(
220 container_of(vd, struct bcm2835_desc, vd));
223 static void bcm2835_dma_create_cb_set_length(
224 struct bcm2835_chan *chan,
225 struct bcm2835_dma_cb *control_block,
231 size_t max_len = bcm2835_dma_max_frame_length(chan);
233 /* set the length taking lite-channel limitations into account */
234 control_block->length = min_t(u32, len, max_len);
236 /* finished if we have no period_length */
241 * period_len means: that we need to generate
242 * transfers that are terminating at every
243 * multiple of period_len - this is typically
244 * used to set the interrupt flag in info
245 * which is required during cyclic transfers
248 /* have we filled in period_length yet? */
249 if (*total_len + control_block->length < period_len) {
250 /* update number of bytes in this period so far */
251 *total_len += control_block->length;
255 /* calculate the length that remains to reach period_length */
256 control_block->length = period_len - *total_len;
258 /* reset total_length for next period */
261 /* add extrainfo bits in info */
262 control_block->info |= finalextrainfo;
265 static inline size_t bcm2835_dma_count_frames_for_sg(
266 struct bcm2835_chan *c,
267 struct scatterlist *sgl,
271 struct scatterlist *sgent;
273 size_t plength = bcm2835_dma_max_frame_length(c);
275 for_each_sg(sgl, sgent, sg_len, i)
276 frames += bcm2835_dma_frames_for_length(
277 sg_dma_len(sgent), plength);
283 * bcm2835_dma_create_cb_chain - create a control block and fills data in
285 * @chan: the @dma_chan for which we run this
286 * @direction: the direction in which we transfer
287 * @cyclic: it is a cyclic transfer
288 * @info: the default info bits to apply per controlblock
289 * @frames: number of controlblocks to allocate
290 * @src: the src address to assign (if the S_INC bit is set
291 * in @info, then it gets incremented)
292 * @dst: the dst address to assign (if the D_INC bit is set
293 * in @info, then it gets incremented)
294 * @buf_len: the full buffer length (may also be 0)
295 * @period_len: the period length when to apply @finalextrainfo
296 * in addition to the last transfer
297 * this will also break some control-blocks early
298 * @finalextrainfo: additional bits in last controlblock
299 * (or when period_len is reached in case of cyclic)
300 * @gfp: the GFP flag to use for allocation
302 static struct bcm2835_desc *bcm2835_dma_create_cb_chain(
303 struct dma_chan *chan, enum dma_transfer_direction direction,
304 bool cyclic, u32 info, u32 finalextrainfo, size_t frames,
305 dma_addr_t src, dma_addr_t dst, size_t buf_len,
306 size_t period_len, gfp_t gfp)
308 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
309 size_t len = buf_len, total_len;
311 struct bcm2835_desc *d;
312 struct bcm2835_cb_entry *cb_entry;
313 struct bcm2835_dma_cb *control_block;
318 /* allocate and setup the descriptor. */
319 d = kzalloc(struct_size(d, cb_list, frames), gfp);
328 * Iterate over all frames, create a control block
329 * for each frame and link them together.
331 for (frame = 0, total_len = 0; frame < frames; d->frames++, frame++) {
332 cb_entry = &d->cb_list[frame];
333 cb_entry->cb = dma_pool_alloc(c->cb_pool, gfp,
338 /* fill in the control block */
339 control_block = cb_entry->cb;
340 control_block->info = info;
341 control_block->src = src;
342 control_block->dst = dst;
343 control_block->stride = 0;
344 control_block->next = 0;
345 /* set up length in control_block if requested */
347 /* calculate length honoring period_length */
348 bcm2835_dma_create_cb_set_length(
350 len, period_len, &total_len,
351 cyclic ? finalextrainfo : 0);
353 /* calculate new remaining length */
354 len -= control_block->length;
357 /* link this the last controlblock */
359 d->cb_list[frame - 1].cb->next = cb_entry->paddr;
361 /* update src and dst and length */
362 if (src && (info & BCM2835_DMA_S_INC))
363 src += control_block->length;
364 if (dst && (info & BCM2835_DMA_D_INC))
365 dst += control_block->length;
367 /* Length of total transfer */
368 d->size += control_block->length;
371 /* the last frame requires extra flags */
372 d->cb_list[d->frames - 1].cb->info |= finalextrainfo;
374 /* detect a size missmatch */
375 if (buf_len && (d->size != buf_len))
380 bcm2835_dma_free_cb_chain(d);
385 static void bcm2835_dma_fill_cb_chain_with_sg(
386 struct dma_chan *chan,
387 enum dma_transfer_direction direction,
388 struct bcm2835_cb_entry *cb,
389 struct scatterlist *sgl,
392 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
396 struct scatterlist *sgent;
398 max_len = bcm2835_dma_max_frame_length(c);
399 for_each_sg(sgl, sgent, sg_len, i) {
400 for (addr = sg_dma_address(sgent), len = sg_dma_len(sgent);
402 addr += cb->cb->length, len -= cb->cb->length, cb++) {
403 if (direction == DMA_DEV_TO_MEM)
407 cb->cb->length = min(len, max_len);
412 static void bcm2835_dma_abort(struct bcm2835_chan *c)
414 void __iomem *chan_base = c->chan_base;
415 long int timeout = 10000;
418 * A zero control block address means the channel is idle.
419 * (The ACTIVE flag in the CS register is not a reliable indicator.)
421 if (!readl(chan_base + BCM2835_DMA_ADDR))
424 /* Write 0 to the active bit - Pause the DMA */
425 writel(0, chan_base + BCM2835_DMA_CS);
427 /* Wait for any current AXI transfer to complete */
428 while ((readl(chan_base + BCM2835_DMA_CS) &
429 BCM2835_DMA_WAITING_FOR_WRITES) && --timeout)
432 /* Peripheral might be stuck and fail to signal AXI write responses */
434 dev_err(c->vc.chan.device->dev,
435 "failed to complete outstanding writes\n");
437 writel(BCM2835_DMA_RESET, chan_base + BCM2835_DMA_CS);
440 static void bcm2835_dma_start_desc(struct bcm2835_chan *c)
442 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
443 struct bcm2835_desc *d;
452 c->desc = d = to_bcm2835_dma_desc(&vd->tx);
454 writel(d->cb_list[0].paddr, c->chan_base + BCM2835_DMA_ADDR);
455 writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS);
458 static irqreturn_t bcm2835_dma_callback(int irq, void *data)
460 struct bcm2835_chan *c = data;
461 struct bcm2835_desc *d;
464 /* check the shared interrupt */
465 if (c->irq_flags & IRQF_SHARED) {
466 /* check if the interrupt is enabled */
467 flags = readl(c->chan_base + BCM2835_DMA_CS);
468 /* if not set then we are not the reason for the irq */
469 if (!(flags & BCM2835_DMA_INT))
473 spin_lock_irqsave(&c->vc.lock, flags);
476 * Clear the INT flag to receive further interrupts. Keep the channel
477 * active in case the descriptor is cyclic or in case the client has
478 * already terminated the descriptor and issued a new one. (May happen
479 * if this IRQ handler is threaded.) If the channel is finished, it
480 * will remain idle despite the ACTIVE flag being set.
482 writel(BCM2835_DMA_INT | BCM2835_DMA_ACTIVE,
483 c->chan_base + BCM2835_DMA_CS);
489 /* call the cyclic callback */
490 vchan_cyclic_callback(&d->vd);
491 } else if (!readl(c->chan_base + BCM2835_DMA_ADDR)) {
492 vchan_cookie_complete(&c->desc->vd);
493 bcm2835_dma_start_desc(c);
497 spin_unlock_irqrestore(&c->vc.lock, flags);
502 static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan)
504 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
505 struct device *dev = c->vc.chan.device->dev;
507 dev_dbg(dev, "Allocating DMA channel %d\n", c->ch);
510 * Control blocks are 256 bit in length and must start at a 256 bit
511 * (32 byte) aligned address (BCM2835 ARM Peripherals, sec. 4.2.1.1).
513 c->cb_pool = dma_pool_create(dev_name(dev), dev,
514 sizeof(struct bcm2835_dma_cb), 32, 0);
516 dev_err(dev, "unable to allocate descriptor pool\n");
520 return request_irq(c->irq_number, bcm2835_dma_callback,
521 c->irq_flags, "DMA IRQ", c);
524 static void bcm2835_dma_free_chan_resources(struct dma_chan *chan)
526 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
528 vchan_free_chan_resources(&c->vc);
529 free_irq(c->irq_number, c);
530 dma_pool_destroy(c->cb_pool);
532 dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
535 static size_t bcm2835_dma_desc_size(struct bcm2835_desc *d)
540 static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc *d, dma_addr_t addr)
545 for (size = i = 0; i < d->frames; i++) {
546 struct bcm2835_dma_cb *control_block = d->cb_list[i].cb;
547 size_t this_size = control_block->length;
550 if (d->dir == DMA_DEV_TO_MEM)
551 dma = control_block->dst;
553 dma = control_block->src;
557 else if (addr >= dma && addr < dma + this_size)
558 size += dma + this_size - addr;
564 static enum dma_status bcm2835_dma_tx_status(struct dma_chan *chan,
565 dma_cookie_t cookie, struct dma_tx_state *txstate)
567 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
568 struct virt_dma_desc *vd;
572 ret = dma_cookie_status(chan, cookie, txstate);
573 if (ret == DMA_COMPLETE || !txstate)
576 spin_lock_irqsave(&c->vc.lock, flags);
577 vd = vchan_find_desc(&c->vc, cookie);
580 bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd->tx));
581 } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
582 struct bcm2835_desc *d = c->desc;
585 if (d->dir == DMA_MEM_TO_DEV)
586 pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD);
587 else if (d->dir == DMA_DEV_TO_MEM)
588 pos = readl(c->chan_base + BCM2835_DMA_DEST_AD);
592 txstate->residue = bcm2835_dma_desc_size_pos(d, pos);
594 txstate->residue = 0;
597 spin_unlock_irqrestore(&c->vc.lock, flags);
602 static void bcm2835_dma_issue_pending(struct dma_chan *chan)
604 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
607 spin_lock_irqsave(&c->vc.lock, flags);
608 if (vchan_issue_pending(&c->vc) && !c->desc)
609 bcm2835_dma_start_desc(c);
611 spin_unlock_irqrestore(&c->vc.lock, flags);
614 static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_memcpy(
615 struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
616 size_t len, unsigned long flags)
618 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
619 struct bcm2835_desc *d;
620 u32 info = BCM2835_DMA_D_INC | BCM2835_DMA_S_INC;
621 u32 extra = BCM2835_DMA_INT_EN | BCM2835_DMA_WAIT_RESP;
622 size_t max_len = bcm2835_dma_max_frame_length(c);
625 /* if src, dst or len is not given return with an error */
626 if (!src || !dst || !len)
629 /* calculate number of frames */
630 frames = bcm2835_dma_frames_for_length(len, max_len);
632 /* allocate the CB chain - this also fills in the pointers */
633 d = bcm2835_dma_create_cb_chain(chan, DMA_MEM_TO_MEM, false,
635 src, dst, len, 0, GFP_KERNEL);
639 return vchan_tx_prep(&c->vc, &d->vd, flags);
642 static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg(
643 struct dma_chan *chan,
644 struct scatterlist *sgl, unsigned int sg_len,
645 enum dma_transfer_direction direction,
646 unsigned long flags, void *context)
648 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
649 struct bcm2835_desc *d;
650 dma_addr_t src = 0, dst = 0;
651 u32 info = BCM2835_DMA_WAIT_RESP;
652 u32 extra = BCM2835_DMA_INT_EN;
655 if (!is_slave_direction(direction)) {
656 dev_err(chan->device->dev,
657 "%s: bad direction?\n", __func__);
662 info |= BCM2835_DMA_PER_MAP(c->dreq);
664 if (direction == DMA_DEV_TO_MEM) {
665 if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
667 src = c->cfg.src_addr;
668 info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
670 if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
672 dst = c->cfg.dst_addr;
673 info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
676 /* count frames in sg list */
677 frames = bcm2835_dma_count_frames_for_sg(c, sgl, sg_len);
679 /* allocate the CB chain */
680 d = bcm2835_dma_create_cb_chain(chan, direction, false,
682 frames, src, dst, 0, 0,
687 /* fill in frames with scatterlist pointers */
688 bcm2835_dma_fill_cb_chain_with_sg(chan, direction, d->cb_list,
691 return vchan_tx_prep(&c->vc, &d->vd, flags);
694 static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
695 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
696 size_t period_len, enum dma_transfer_direction direction,
699 struct bcm2835_dmadev *od = to_bcm2835_dma_dev(chan->device);
700 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
701 struct bcm2835_desc *d;
703 u32 info = BCM2835_DMA_WAIT_RESP;
705 size_t max_len = bcm2835_dma_max_frame_length(c);
708 /* Grab configuration */
709 if (!is_slave_direction(direction)) {
710 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
715 dev_err(chan->device->dev,
716 "%s: bad buffer length (= 0)\n", __func__);
720 if (flags & DMA_PREP_INTERRUPT)
721 extra |= BCM2835_DMA_INT_EN;
723 period_len = buf_len;
726 * warn if buf_len is not a multiple of period_len - this may leed
727 * to unexpected latencies for interrupts and thus audiable clicks
729 if (buf_len % period_len)
730 dev_warn_once(chan->device->dev,
731 "%s: buffer_length (%zd) is not a multiple of period_len (%zd)\n",
732 __func__, buf_len, period_len);
734 /* Setup DREQ channel */
736 info |= BCM2835_DMA_PER_MAP(c->dreq);
738 if (direction == DMA_DEV_TO_MEM) {
739 if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
741 src = c->cfg.src_addr;
743 info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
745 if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
747 dst = c->cfg.dst_addr;
749 info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
751 /* non-lite channels can write zeroes w/o accessing memory */
752 if (buf_addr == od->zero_page && !c->is_lite_channel)
753 info |= BCM2835_DMA_S_IGNORE;
756 /* calculate number of frames */
757 frames = /* number of periods */
758 DIV_ROUND_UP(buf_len, period_len) *
759 /* number of frames per period */
760 bcm2835_dma_frames_for_length(period_len, max_len);
763 * allocate the CB chain
764 * note that we need to use GFP_NOWAIT, as the ALSA i2s dmaengine
765 * implementation calls prep_dma_cyclic with interrupts disabled.
767 d = bcm2835_dma_create_cb_chain(chan, direction, true,
769 frames, src, dst, buf_len,
770 period_len, GFP_NOWAIT);
774 /* wrap around into a loop */
775 d->cb_list[d->frames - 1].cb->next = d->cb_list[0].paddr;
777 return vchan_tx_prep(&c->vc, &d->vd, flags);
780 static int bcm2835_dma_slave_config(struct dma_chan *chan,
781 struct dma_slave_config *cfg)
783 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
790 static int bcm2835_dma_terminate_all(struct dma_chan *chan)
792 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
796 spin_lock_irqsave(&c->vc.lock, flags);
798 /* stop DMA activity */
800 if (c->desc->vd.tx.flags & DMA_PREP_INTERRUPT)
801 vchan_terminate_vdesc(&c->desc->vd);
803 vchan_vdesc_fini(&c->desc->vd);
805 bcm2835_dma_abort(c);
808 vchan_get_all_descriptors(&c->vc, &head);
809 spin_unlock_irqrestore(&c->vc.lock, flags);
810 vchan_dma_desc_free_list(&c->vc, &head);
815 static void bcm2835_dma_synchronize(struct dma_chan *chan)
817 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
819 vchan_synchronize(&c->vc);
822 static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id,
823 int irq, unsigned int irq_flags)
825 struct bcm2835_chan *c;
827 c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
831 c->vc.desc_free = bcm2835_dma_desc_free;
832 vchan_init(&c->vc, &d->ddev);
834 c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
837 c->irq_flags = irq_flags;
839 /* check in DEBUG register if this is a LITE channel */
840 if (readl(c->chan_base + BCM2835_DMA_DEBUG) &
841 BCM2835_DMA_DEBUG_LITE)
842 c->is_lite_channel = true;
847 static void bcm2835_dma_free(struct bcm2835_dmadev *od)
849 struct bcm2835_chan *c, *next;
851 list_for_each_entry_safe(c, next, &od->ddev.channels,
852 vc.chan.device_node) {
853 list_del(&c->vc.chan.device_node);
854 tasklet_kill(&c->vc.task);
857 dma_unmap_page_attrs(od->ddev.dev, od->zero_page, PAGE_SIZE,
858 DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
861 static const struct of_device_id bcm2835_dma_of_match[] = {
862 { .compatible = "brcm,bcm2835-dma", },
865 MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
867 static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec,
868 struct of_dma *ofdma)
870 struct bcm2835_dmadev *d = ofdma->of_dma_data;
871 struct dma_chan *chan;
873 chan = dma_get_any_slave_channel(&d->ddev);
877 /* Set DREQ from param */
878 to_bcm2835_dma_chan(chan)->dreq = spec->args[0];
883 static int bcm2835_dma_probe(struct platform_device *pdev)
885 struct bcm2835_dmadev *od;
886 struct resource *res;
890 int irq[BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED + 1];
892 uint32_t chans_available;
893 char chan_name[BCM2835_DMA_CHAN_NAME_SIZE];
895 if (!pdev->dev.dma_mask)
896 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
898 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
900 dev_err(&pdev->dev, "Unable to set DMA mask\n");
904 od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
908 pdev->dev.dma_parms = &od->dma_parms;
909 dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
911 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
912 base = devm_ioremap_resource(&pdev->dev, res);
914 return PTR_ERR(base);
918 dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
919 dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
920 dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
921 dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask);
922 od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
923 od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
924 od->ddev.device_tx_status = bcm2835_dma_tx_status;
925 od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
926 od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
927 od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
928 od->ddev.device_prep_dma_memcpy = bcm2835_dma_prep_dma_memcpy;
929 od->ddev.device_config = bcm2835_dma_slave_config;
930 od->ddev.device_terminate_all = bcm2835_dma_terminate_all;
931 od->ddev.device_synchronize = bcm2835_dma_synchronize;
932 od->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
933 od->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
934 od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
936 od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
937 od->ddev.descriptor_reuse = true;
938 od->ddev.dev = &pdev->dev;
939 INIT_LIST_HEAD(&od->ddev.channels);
941 platform_set_drvdata(pdev, od);
943 od->zero_page = dma_map_page_attrs(od->ddev.dev, ZERO_PAGE(0), 0,
944 PAGE_SIZE, DMA_TO_DEVICE,
945 DMA_ATTR_SKIP_CPU_SYNC);
946 if (dma_mapping_error(od->ddev.dev, od->zero_page)) {
947 dev_err(&pdev->dev, "Failed to map zero page\n");
951 /* Request DMA channel mask from device tree */
952 if (of_property_read_u32(pdev->dev.of_node,
953 "brcm,dma-channel-mask",
955 dev_err(&pdev->dev, "Failed to get channel mask\n");
960 /* get irqs for each channel that we support */
961 for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
962 /* skip masked out channels */
963 if (!(chans_available & (1 << i))) {
968 /* get the named irq */
969 snprintf(chan_name, sizeof(chan_name), "dma%i", i);
970 irq[i] = platform_get_irq_byname(pdev, chan_name);
974 /* legacy device tree case handling */
975 dev_warn_once(&pdev->dev,
976 "missing interrupt-names property in device tree - legacy interpretation is used\n");
978 * in case of channel >= 11
979 * use the 11th interrupt and that is shared
981 irq[i] = platform_get_irq(pdev, i < 11 ? i : 11);
984 /* get irqs for each channel */
985 for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
986 /* skip channels without irq */
990 /* check if there are other channels that also use this irq */
992 for (j = 0; j <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; j++)
993 if ((i != j) && (irq[j] == irq[i])) {
994 irq_flags = IRQF_SHARED;
998 /* initialize the channel */
999 rc = bcm2835_dma_chan_init(od, i, irq[i], irq_flags);
1004 dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);
1006 /* Device-tree DMA controller registration */
1007 rc = of_dma_controller_register(pdev->dev.of_node,
1008 bcm2835_dma_xlate, od);
1010 dev_err(&pdev->dev, "Failed to register DMA controller\n");
1014 rc = dma_async_device_register(&od->ddev);
1017 "Failed to register slave DMA engine device: %d\n", rc);
1021 dev_dbg(&pdev->dev, "Load BCM2835 DMA engine driver\n");
1026 bcm2835_dma_free(od);
1030 static int bcm2835_dma_remove(struct platform_device *pdev)
1032 struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
1034 dma_async_device_unregister(&od->ddev);
1035 bcm2835_dma_free(od);
1040 static struct platform_driver bcm2835_dma_driver = {
1041 .probe = bcm2835_dma_probe,
1042 .remove = bcm2835_dma_remove,
1044 .name = "bcm2835-dma",
1045 .of_match_table = of_match_ptr(bcm2835_dma_of_match),
1049 module_platform_driver(bcm2835_dma_driver);
1051 MODULE_ALIAS("platform:bcm2835-dma");
1052 MODULE_DESCRIPTION("BCM2835 DMA engine driver");
1053 MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
1054 MODULE_LICENSE("GPL");