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_data/dma-bcm2708.h>
29 #include <linux/platform_device.h>
30 #include <linux/slab.h>
32 #include <linux/spinlock.h>
34 #include <linux/of_dma.h>
38 #define BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED 14
39 #define BCM2835_DMA_CHAN_NAME_SIZE 8
40 #define BCM2835_DMA_BULK_MASK BIT(0)
41 #define BCM2711_DMA_MEMCPY_CHAN 14
43 struct bcm2835_dma_cfg_data {
49 * struct bcm2835_dmadev - BCM2835 DMA controller
51 * @base: base address of register map
52 * @zero_page: bus address of zero page (to detect transactions copying from
53 * zero page and avoid accessing memory if so)
55 struct bcm2835_dmadev {
56 struct dma_device ddev;
59 const struct bcm2835_dma_cfg_data *cfg_data;
62 struct bcm2835_dma_cb {
72 struct bcm2711_dma40_scb {
83 struct bcm2835_cb_entry {
84 struct bcm2835_dma_cb *cb;
89 struct virt_dma_chan vc;
91 struct dma_slave_config cfg;
95 struct bcm2835_desc *desc;
96 struct dma_pool *cb_pool;
98 void __iomem *chan_base;
100 unsigned int irq_flags;
102 bool is_lite_channel;
103 bool is_40bit_channel;
106 struct bcm2835_desc {
107 struct bcm2835_chan *c;
108 struct virt_dma_desc vd;
109 enum dma_transfer_direction dir;
116 struct bcm2835_cb_entry cb_list[];
119 #define BCM2835_DMA_CS 0x00
120 #define BCM2835_DMA_ADDR 0x04
121 #define BCM2835_DMA_TI 0x08
122 #define BCM2835_DMA_SOURCE_AD 0x0c
123 #define BCM2835_DMA_DEST_AD 0x10
124 #define BCM2835_DMA_LEN 0x14
125 #define BCM2835_DMA_STRIDE 0x18
126 #define BCM2835_DMA_NEXTCB 0x1c
127 #define BCM2835_DMA_DEBUG 0x20
129 /* DMA CS Control and Status bits */
130 #define BCM2835_DMA_ACTIVE BIT(0) /* activate the DMA */
131 #define BCM2835_DMA_END BIT(1) /* current CB has ended */
132 #define BCM2835_DMA_INT BIT(2) /* interrupt status */
133 #define BCM2835_DMA_DREQ BIT(3) /* DREQ state */
134 #define BCM2835_DMA_ISPAUSED BIT(4) /* Pause requested or not active */
135 #define BCM2835_DMA_ISHELD BIT(5) /* Is held by DREQ flow control */
136 #define BCM2835_DMA_WAITING_FOR_WRITES BIT(6) /* waiting for last
139 #define BCM2835_DMA_ERR BIT(8)
140 #define BCM2835_DMA_PRIORITY(x) ((x & 15) << 16) /* AXI priority */
141 #define BCM2835_DMA_PANIC_PRIORITY(x) ((x & 15) << 20) /* panic priority */
142 /* current value of TI.BCM2835_DMA_WAIT_RESP */
143 #define BCM2835_DMA_WAIT_FOR_WRITES BIT(28)
144 #define BCM2835_DMA_DIS_DEBUG BIT(29) /* disable debug pause signal */
145 #define BCM2835_DMA_ABORT BIT(30) /* Stop current CB, go to next, WO */
146 #define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */
148 /* Transfer information bits - also bcm2835_cb.info field */
149 #define BCM2835_DMA_INT_EN BIT(0)
150 #define BCM2835_DMA_TDMODE BIT(1) /* 2D-Mode */
151 #define BCM2835_DMA_WAIT_RESP BIT(3) /* wait for AXI-write to be acked */
152 #define BCM2835_DMA_D_INC BIT(4)
153 #define BCM2835_DMA_D_WIDTH BIT(5) /* 128bit writes if set */
154 #define BCM2835_DMA_D_DREQ BIT(6) /* enable DREQ for destination */
155 #define BCM2835_DMA_D_IGNORE BIT(7) /* ignore destination writes */
156 #define BCM2835_DMA_S_INC BIT(8)
157 #define BCM2835_DMA_S_WIDTH BIT(9) /* 128bit writes if set */
158 #define BCM2835_DMA_S_DREQ BIT(10) /* enable SREQ for source */
159 #define BCM2835_DMA_S_IGNORE BIT(11) /* ignore source reads - read 0 */
160 #define BCM2835_DMA_BURST_LENGTH(x) ((x & 15) << 12)
161 #define BCM2835_DMA_CS_FLAGS(x) (x & (BCM2835_DMA_PRIORITY(15) | \
162 BCM2835_DMA_PANIC_PRIORITY(15) | \
163 BCM2835_DMA_WAIT_FOR_WRITES | \
164 BCM2835_DMA_DIS_DEBUG))
165 #define BCM2835_DMA_PER_MAP(x) ((x & 31) << 16) /* REQ source */
166 #define BCM2835_DMA_WAIT(x) ((x & 31) << 21) /* add DMA-wait cycles */
167 #define BCM2835_DMA_NO_WIDE_BURSTS BIT(26) /* no 2 beat write bursts */
169 /* A fake bit to request that the driver doesn't set the WAIT_RESP bit. */
170 #define BCM2835_DMA_NO_WAIT_RESP BIT(27)
171 #define WAIT_RESP(x) ((x & BCM2835_DMA_NO_WAIT_RESP) ? \
172 0 : BCM2835_DMA_WAIT_RESP)
174 /* A fake bit to request that the driver requires wide reads */
175 #define BCM2835_DMA_WIDE_SOURCE BIT(24)
176 #define WIDE_SOURCE(x) ((x & BCM2835_DMA_WIDE_SOURCE) ? \
177 BCM2835_DMA_S_WIDTH : 0)
179 /* A fake bit to request that the driver requires wide writes */
180 #define BCM2835_DMA_WIDE_DEST BIT(25)
181 #define WIDE_DEST(x) ((x & BCM2835_DMA_WIDE_DEST) ? \
182 BCM2835_DMA_D_WIDTH : 0)
185 /* debug register bits */
186 #define BCM2835_DMA_DEBUG_LAST_NOT_SET_ERR BIT(0)
187 #define BCM2835_DMA_DEBUG_FIFO_ERR BIT(1)
188 #define BCM2835_DMA_DEBUG_READ_ERR BIT(2)
189 #define BCM2835_DMA_DEBUG_OUTSTANDING_WRITES_SHIFT 4
190 #define BCM2835_DMA_DEBUG_OUTSTANDING_WRITES_BITS 4
191 #define BCM2835_DMA_DEBUG_ID_SHIFT 16
192 #define BCM2835_DMA_DEBUG_ID_BITS 9
193 #define BCM2835_DMA_DEBUG_STATE_SHIFT 16
194 #define BCM2835_DMA_DEBUG_STATE_BITS 9
195 #define BCM2835_DMA_DEBUG_VERSION_SHIFT 25
196 #define BCM2835_DMA_DEBUG_VERSION_BITS 3
197 #define BCM2835_DMA_DEBUG_LITE BIT(28)
199 /* shared registers for all dma channels */
200 #define BCM2835_DMA_INT_STATUS 0xfe0
201 #define BCM2835_DMA_ENABLE 0xff0
203 #define BCM2835_DMA_DATA_TYPE_S8 1
204 #define BCM2835_DMA_DATA_TYPE_S16 2
205 #define BCM2835_DMA_DATA_TYPE_S32 4
206 #define BCM2835_DMA_DATA_TYPE_S128 16
208 /* Valid only for channels 0 - 14, 15 has its own base address */
209 #define BCM2835_DMA_CHAN_SIZE 0x100
210 #define BCM2835_DMA_CHAN(n) ((n) * BCM2835_DMA_CHAN_SIZE) /* Base address */
211 #define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
213 /* the max dma length for different channels */
214 #define MAX_DMA_LEN SZ_1G
215 #define MAX_LITE_DMA_LEN (SZ_64K - 4)
217 /* 40-bit DMA support */
218 #define BCM2711_DMA40_CS 0x00
219 #define BCM2711_DMA40_CB 0x04
220 #define BCM2711_DMA40_DEBUG 0x0c
221 #define BCM2711_DMA40_TI 0x10
222 #define BCM2711_DMA40_SRC 0x14
223 #define BCM2711_DMA40_SRCI 0x18
224 #define BCM2711_DMA40_DEST 0x1c
225 #define BCM2711_DMA40_DESTI 0x20
226 #define BCM2711_DMA40_LEN 0x24
227 #define BCM2711_DMA40_NEXT_CB 0x28
228 #define BCM2711_DMA40_DEBUG2 0x2c
230 #define BCM2711_DMA40_ACTIVE BIT(0)
231 #define BCM2711_DMA40_END BIT(1)
232 #define BCM2711_DMA40_INT BIT(2)
233 #define BCM2711_DMA40_DREQ BIT(3) /* DREQ state */
234 #define BCM2711_DMA40_RD_PAUSED BIT(4) /* Reading is paused */
235 #define BCM2711_DMA40_WR_PAUSED BIT(5) /* Writing is paused */
236 #define BCM2711_DMA40_DREQ_PAUSED BIT(6) /* Is paused by DREQ flow control */
237 #define BCM2711_DMA40_WAITING_FOR_WRITES BIT(7) /* Waiting for last write */
238 #define BCM2711_DMA40_ERR BIT(10)
239 #define BCM2711_DMA40_QOS(x) (((x) & 0x1f) << 16)
240 #define BCM2711_DMA40_PANIC_QOS(x) (((x) & 0x1f) << 20)
241 #define BCM2711_DMA40_WAIT_FOR_WRITES BIT(28)
242 #define BCM2711_DMA40_DISDEBUG BIT(29)
243 #define BCM2711_DMA40_ABORT BIT(30)
244 #define BCM2711_DMA40_HALT BIT(31)
245 #define BCM2711_DMA40_CS_FLAGS(x) (x & (BCM2711_DMA40_QOS(15) | \
246 BCM2711_DMA40_PANIC_QOS(15) | \
247 BCM2711_DMA40_WAIT_FOR_WRITES | \
248 BCM2711_DMA40_DISDEBUG))
250 /* Transfer information bits */
251 #define BCM2711_DMA40_INTEN BIT(0)
252 #define BCM2711_DMA40_TDMODE BIT(1) /* 2D-Mode */
253 #define BCM2711_DMA40_WAIT_RESP BIT(2) /* wait for AXI write to be acked */
254 #define BCM2711_DMA40_WAIT_RD_RESP BIT(3) /* wait for AXI read to complete */
255 #define BCM2711_DMA40_PER_MAP(x) ((x & 31) << 9) /* REQ source */
256 #define BCM2711_DMA40_S_DREQ BIT(14) /* enable SREQ for source */
257 #define BCM2711_DMA40_D_DREQ BIT(15) /* enable DREQ for destination */
258 #define BCM2711_DMA40_S_WAIT(x) ((x & 0xff) << 16) /* add DMA read-wait cycles */
259 #define BCM2711_DMA40_D_WAIT(x) ((x & 0xff) << 24) /* add DMA write-wait cycles */
261 /* debug register bits */
262 #define BCM2711_DMA40_DEBUG_WRITE_ERR BIT(0)
263 #define BCM2711_DMA40_DEBUG_FIFO_ERR BIT(1)
264 #define BCM2711_DMA40_DEBUG_READ_ERR BIT(2)
265 #define BCM2711_DMA40_DEBUG_READ_CB_ERR BIT(3)
266 #define BCM2711_DMA40_DEBUG_IN_ON_ERR BIT(8)
267 #define BCM2711_DMA40_DEBUG_ABORT_ON_ERR BIT(9)
268 #define BCM2711_DMA40_DEBUG_HALT_ON_ERR BIT(10)
269 #define BCM2711_DMA40_DEBUG_DISABLE_CLK_GATE BIT(11)
270 #define BCM2711_DMA40_DEBUG_RSTATE_SHIFT 14
271 #define BCM2711_DMA40_DEBUG_RSTATE_BITS 4
272 #define BCM2711_DMA40_DEBUG_WSTATE_SHIFT 18
273 #define BCM2711_DMA40_DEBUG_WSTATE_BITS 4
274 #define BCM2711_DMA40_DEBUG_RESET BIT(23)
275 #define BCM2711_DMA40_DEBUG_ID_SHIFT 24
276 #define BCM2711_DMA40_DEBUG_ID_BITS 4
277 #define BCM2711_DMA40_DEBUG_VERSION_SHIFT 28
278 #define BCM2711_DMA40_DEBUG_VERSION_BITS 4
280 /* Valid only for channels 0 - 3 (11 - 14) */
281 #define BCM2711_DMA40_CHAN(n) (((n) + 11) << 8) /* Base address */
282 #define BCM2711_DMA40_CHANIO(base, n) ((base) + BCM2711_DMA_CHAN(n))
284 /* the max dma length for different channels */
285 #define MAX_DMA40_LEN SZ_1G
287 #define BCM2711_DMA40_BURST_LEN(x) ((min(x,16) - 1) << 8)
288 #define BCM2711_DMA40_INC BIT(12)
289 #define BCM2711_DMA40_SIZE_32 (0 << 13)
290 #define BCM2711_DMA40_SIZE_64 (1 << 13)
291 #define BCM2711_DMA40_SIZE_128 (2 << 13)
292 #define BCM2711_DMA40_SIZE_256 (3 << 13)
293 #define BCM2711_DMA40_IGNORE BIT(15)
294 #define BCM2711_DMA40_STRIDE(x) ((x) << 16) /* For 2D mode */
296 #define BCM2711_DMA40_MEMCPY_FLAGS \
297 (BCM2711_DMA40_QOS(0) | \
298 BCM2711_DMA40_PANIC_QOS(0) | \
299 BCM2711_DMA40_WAIT_FOR_WRITES | \
300 BCM2711_DMA40_DISDEBUG)
302 #define BCM2711_DMA40_MEMCPY_XFER_INFO \
303 (BCM2711_DMA40_SIZE_128 | \
304 BCM2711_DMA40_INC | \
305 BCM2711_DMA40_BURST_LEN(16))
307 struct bcm2835_dmadev *memcpy_parent;
308 static void __iomem *memcpy_chan;
309 static struct bcm2711_dma40_scb *memcpy_scb;
310 static dma_addr_t memcpy_scb_dma;
311 DEFINE_SPINLOCK(memcpy_lock);
313 static const struct bcm2835_dma_cfg_data bcm2835_dma_cfg = {
314 .chan_40bit_mask = 0,
315 .dma_mask = DMA_BIT_MASK(32),
318 static const struct bcm2835_dma_cfg_data bcm2711_dma_cfg = {
319 .chan_40bit_mask = BIT(11) | BIT(12) | BIT(13) | BIT(14),
320 .dma_mask = DMA_BIT_MASK(36),
323 static inline size_t bcm2835_dma_max_frame_length(struct bcm2835_chan *c)
325 /* lite and normal channels have different max frame length */
326 return c->is_lite_channel ? MAX_LITE_DMA_LEN : MAX_DMA_LEN;
329 /* how many frames of max_len size do we need to transfer len bytes */
330 static inline size_t bcm2835_dma_frames_for_length(size_t len,
333 return DIV_ROUND_UP(len, max_len);
336 static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d)
338 return container_of(d, struct bcm2835_dmadev, ddev);
341 static inline struct bcm2835_chan *to_bcm2835_dma_chan(struct dma_chan *c)
343 return container_of(c, struct bcm2835_chan, vc.chan);
346 static inline struct bcm2835_desc *to_bcm2835_dma_desc(
347 struct dma_async_tx_descriptor *t)
349 return container_of(t, struct bcm2835_desc, vd.tx);
352 static inline uint32_t to_bcm2711_ti(uint32_t info)
354 return ((info & BCM2835_DMA_INT_EN) ? BCM2711_DMA40_INTEN : 0) |
355 ((info & BCM2835_DMA_WAIT_RESP) ? BCM2711_DMA40_WAIT_RESP : 0) |
356 ((info & BCM2835_DMA_S_DREQ) ?
357 (BCM2711_DMA40_S_DREQ | BCM2711_DMA40_WAIT_RD_RESP) : 0) |
358 ((info & BCM2835_DMA_D_DREQ) ? BCM2711_DMA40_D_DREQ : 0) |
359 BCM2711_DMA40_PER_MAP((info >> 16) & 0x1f);
362 static inline uint32_t to_bcm2711_srci(uint32_t info)
364 return ((info & BCM2835_DMA_S_INC) ? BCM2711_DMA40_INC : 0);
367 static inline uint32_t to_bcm2711_dsti(uint32_t info)
369 return ((info & BCM2835_DMA_D_INC) ? BCM2711_DMA40_INC : 0);
372 static inline uint32_t to_bcm2711_cbaddr(dma_addr_t addr)
378 static void bcm2835_dma_free_cb_chain(struct bcm2835_desc *desc)
382 for (i = 0; i < desc->frames; i++)
383 dma_pool_free(desc->c->cb_pool, desc->cb_list[i].cb,
384 desc->cb_list[i].paddr);
389 static void bcm2835_dma_desc_free(struct virt_dma_desc *vd)
391 bcm2835_dma_free_cb_chain(
392 container_of(vd, struct bcm2835_desc, vd));
395 static void bcm2835_dma_create_cb_set_length(
396 struct bcm2835_chan *c,
397 struct bcm2835_dma_cb *control_block,
403 size_t max_len = bcm2835_dma_max_frame_length(c);
406 /* set the length taking lite-channel limitations into account */
407 cb_len = min_t(u32, len, max_len);
411 * period_len means: that we need to generate
412 * transfers that are terminating at every
413 * multiple of period_len - this is typically
414 * used to set the interrupt flag in info
415 * which is required during cyclic transfers
418 /* have we filled in period_length yet? */
419 if (*total_len + cb_len < period_len) {
420 /* update number of bytes in this period so far */
421 *total_len += cb_len;
423 /* calculate the length that remains to reach period_len */
424 cb_len = period_len - *total_len;
426 /* reset total_length for next period */
431 if (c->is_40bit_channel) {
432 struct bcm2711_dma40_scb *scb =
433 (struct bcm2711_dma40_scb *)control_block;
436 /* add extrainfo bits to ti */
437 scb->ti |= to_bcm2711_ti(finalextrainfo);
439 control_block->length = cb_len;
440 /* add extrainfo bits to info */
441 control_block->info |= finalextrainfo;
445 static inline size_t bcm2835_dma_count_frames_for_sg(
446 struct bcm2835_chan *c,
447 struct scatterlist *sgl,
451 struct scatterlist *sgent;
453 size_t plength = bcm2835_dma_max_frame_length(c);
455 for_each_sg(sgl, sgent, sg_len, i)
456 frames += bcm2835_dma_frames_for_length(
457 sg_dma_len(sgent), plength);
463 * bcm2835_dma_create_cb_chain - create a control block and fills data in
465 * @c: the @bcm2835_chan for which we run this
466 * @direction: the direction in which we transfer
467 * @cyclic: it is a cyclic transfer
468 * @info: the default info bits to apply per controlblock
469 * @frames: number of controlblocks to allocate
470 * @src: the src address to assign (if the S_INC bit is set
471 * in @info, then it gets incremented)
472 * @dst: the dst address to assign (if the D_INC bit is set
473 * in @info, then it gets incremented)
474 * @buf_len: the full buffer length (may also be 0)
475 * @period_len: the period length when to apply @finalextrainfo
476 * in addition to the last transfer
477 * this will also break some control-blocks early
478 * @finalextrainfo: additional bits in last controlblock
479 * (or when period_len is reached in case of cyclic)
480 * @gfp: the GFP flag to use for allocation
482 static struct bcm2835_desc *bcm2835_dma_create_cb_chain(
483 struct bcm2835_chan *c, enum dma_transfer_direction direction,
484 bool cyclic, u32 info, u32 finalextrainfo, size_t frames,
485 dma_addr_t src, dma_addr_t dst, size_t buf_len,
486 size_t period_len, gfp_t gfp)
488 size_t len = buf_len, total_len;
490 struct bcm2835_desc *d;
491 struct bcm2835_cb_entry *cb_entry;
492 struct bcm2835_dma_cb *control_block;
497 /* allocate and setup the descriptor. */
498 d = kzalloc(struct_size(d, cb_list, frames), gfp);
507 * Iterate over all frames, create a control block
508 * for each frame and link them together.
510 for (frame = 0, total_len = 0; frame < frames; d->frames++, frame++) {
511 cb_entry = &d->cb_list[frame];
512 cb_entry->cb = dma_pool_alloc(c->cb_pool, gfp,
517 /* fill in the control block */
518 control_block = cb_entry->cb;
519 if (c->is_40bit_channel) {
520 struct bcm2711_dma40_scb *scb =
521 (struct bcm2711_dma40_scb *)control_block;
522 scb->ti = to_bcm2711_ti(info);
523 scb->src = lower_32_bits(src);
524 scb->srci= upper_32_bits(src) | to_bcm2711_srci(info);
525 scb->dst = lower_32_bits(dst);
526 scb->dsti = upper_32_bits(dst) | to_bcm2711_dsti(info);
529 control_block->info = info;
530 control_block->src = src;
531 control_block->dst = dst;
532 control_block->stride = 0;
533 control_block->next = 0;
536 /* set up length in control_block if requested */
538 /* calculate length honoring period_length */
539 bcm2835_dma_create_cb_set_length(
541 len, period_len, &total_len,
542 cyclic ? finalextrainfo : 0);
544 /* calculate new remaining length */
545 len -= control_block->length;
548 /* link this the last controlblock */
549 if (frame && c->is_40bit_channel)
550 ((struct bcm2711_dma40_scb *)
551 d->cb_list[frame - 1].cb)->next_cb =
552 to_bcm2711_cbaddr(cb_entry->paddr);
553 if (frame && !c->is_40bit_channel)
554 d->cb_list[frame - 1].cb->next = cb_entry->paddr;
556 /* update src and dst and length */
557 if (src && (info & BCM2835_DMA_S_INC))
558 src += control_block->length;
559 if (dst && (info & BCM2835_DMA_D_INC))
560 dst += control_block->length;
562 /* Length of total transfer */
563 if (c->is_40bit_channel)
564 d->size += ((struct bcm2711_dma40_scb *)control_block)->len;
566 d->size += control_block->length;
569 /* the last frame requires extra flags */
570 if (c->is_40bit_channel) {
571 struct bcm2711_dma40_scb *scb =
572 (struct bcm2711_dma40_scb *)d->cb_list[d->frames-1].cb;
574 scb->ti |= to_bcm2711_ti(finalextrainfo);
576 d->cb_list[d->frames - 1].cb->info |= finalextrainfo;
579 /* detect a size missmatch */
580 if (buf_len && (d->size != buf_len))
585 bcm2835_dma_free_cb_chain(d);
590 static void bcm2835_dma_fill_cb_chain_with_sg(
591 struct bcm2835_chan *c,
592 enum dma_transfer_direction direction,
593 struct bcm2835_cb_entry *cb,
594 struct scatterlist *sgl,
600 struct scatterlist *sgent;
602 max_len = bcm2835_dma_max_frame_length(c);
603 for_each_sg(sgl, sgent, sg_len, i) {
604 if (c->is_40bit_channel) {
605 struct bcm2711_dma40_scb *scb;
607 for (addr = sg_dma_address(sgent),
608 len = sg_dma_len(sgent);
610 addr += scb->len, len -= scb->len, cb++) {
611 scb = (struct bcm2711_dma40_scb *)cb->cb;
612 if (direction == DMA_DEV_TO_MEM) {
613 scb->dst = lower_32_bits(addr);
614 scb->dsti = upper_32_bits(addr) | BCM2711_DMA40_INC;
616 scb->src = lower_32_bits(addr);
617 scb->srci = upper_32_bits(addr) | BCM2711_DMA40_INC;
619 scb->len = min(len, max_len);
622 for (addr = sg_dma_address(sgent),
623 len = sg_dma_len(sgent);
625 addr += cb->cb->length, len -= cb->cb->length,
627 if (direction == DMA_DEV_TO_MEM)
631 cb->cb->length = min(len, max_len);
637 static void bcm2835_dma_abort(struct bcm2835_chan *c)
639 void __iomem *chan_base = c->chan_base;
640 long int timeout = 10000;
641 u32 wait_mask = BCM2835_DMA_WAITING_FOR_WRITES;
643 if (c->is_40bit_channel)
644 wait_mask = BCM2711_DMA40_WAITING_FOR_WRITES;
647 * A zero control block address means the channel is idle.
648 * (The ACTIVE flag in the CS register is not a reliable indicator.)
650 if (!readl(chan_base + BCM2835_DMA_ADDR))
653 /* Write 0 to the active bit - Pause the DMA */
654 writel(0, chan_base + BCM2835_DMA_CS);
656 /* Wait for any current AXI transfer to complete */
657 while ((readl(chan_base + BCM2835_DMA_CS) & wait_mask) && --timeout)
660 /* Peripheral might be stuck and fail to signal AXI write responses */
662 dev_err(c->vc.chan.device->dev,
663 "failed to complete outstanding writes\n");
665 writel(BCM2835_DMA_RESET, chan_base + BCM2835_DMA_CS);
668 static void bcm2835_dma_start_desc(struct bcm2835_chan *c)
670 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
671 struct bcm2835_desc *d;
680 c->desc = d = to_bcm2835_dma_desc(&vd->tx);
682 if (c->is_40bit_channel) {
683 writel(to_bcm2711_cbaddr(d->cb_list[0].paddr),
684 c->chan_base + BCM2711_DMA40_CB);
685 writel(BCM2711_DMA40_ACTIVE | BCM2711_DMA40_CS_FLAGS(c->dreq),
686 c->chan_base + BCM2711_DMA40_CS);
688 writel(d->cb_list[0].paddr, c->chan_base + BCM2835_DMA_ADDR);
689 writel(BCM2835_DMA_ACTIVE | BCM2835_DMA_CS_FLAGS(c->dreq),
690 c->chan_base + BCM2835_DMA_CS);
694 static irqreturn_t bcm2835_dma_callback(int irq, void *data)
696 struct bcm2835_chan *c = data;
697 struct bcm2835_desc *d;
700 /* check the shared interrupt */
701 if (c->irq_flags & IRQF_SHARED) {
702 /* check if the interrupt is enabled */
703 flags = readl(c->chan_base + BCM2835_DMA_CS);
704 /* if not set then we are not the reason for the irq */
705 if (!(flags & BCM2835_DMA_INT))
709 spin_lock_irqsave(&c->vc.lock, flags);
712 * Clear the INT flag to receive further interrupts. Keep the channel
713 * active in case the descriptor is cyclic or in case the client has
714 * already terminated the descriptor and issued a new one. (May happen
715 * if this IRQ handler is threaded.) If the channel is finished, it
716 * will remain idle despite the ACTIVE flag being set.
718 writel(BCM2835_DMA_INT | BCM2835_DMA_ACTIVE | BCM2835_DMA_CS_FLAGS(c->dreq),
719 c->chan_base + BCM2835_DMA_CS);
725 /* call the cyclic callback */
726 vchan_cyclic_callback(&d->vd);
727 } else if (!readl(c->chan_base + BCM2835_DMA_ADDR)) {
728 vchan_cookie_complete(&c->desc->vd);
729 bcm2835_dma_start_desc(c);
733 spin_unlock_irqrestore(&c->vc.lock, flags);
738 static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan)
740 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
741 struct device *dev = c->vc.chan.device->dev;
743 dev_dbg(dev, "Allocating DMA channel %d\n", c->ch);
746 * Control blocks are 256 bit in length and must start at a 256 bit
747 * (32 byte) aligned address (BCM2835 ARM Peripherals, sec. 4.2.1.1).
749 c->cb_pool = dma_pool_create(dev_name(dev), dev,
750 sizeof(struct bcm2835_dma_cb), 32, 0);
752 dev_err(dev, "unable to allocate descriptor pool\n");
756 return request_irq(c->irq_number, bcm2835_dma_callback,
757 c->irq_flags, "DMA IRQ", c);
760 static void bcm2835_dma_free_chan_resources(struct dma_chan *chan)
762 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
764 vchan_free_chan_resources(&c->vc);
765 free_irq(c->irq_number, c);
766 dma_pool_destroy(c->cb_pool);
768 dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
771 static size_t bcm2835_dma_desc_size(struct bcm2835_desc *d)
776 static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc *d, dma_addr_t addr)
781 for (size = i = 0; i < d->frames; i++) {
782 struct bcm2835_dma_cb *control_block = d->cb_list[i].cb;
783 size_t this_size = control_block->length;
786 if (d->dir == DMA_DEV_TO_MEM)
787 dma = control_block->dst;
789 dma = control_block->src;
793 else if (addr >= dma && addr < dma + this_size)
794 size += dma + this_size - addr;
800 static enum dma_status bcm2835_dma_tx_status(struct dma_chan *chan,
801 dma_cookie_t cookie, struct dma_tx_state *txstate)
803 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
804 struct virt_dma_desc *vd;
808 ret = dma_cookie_status(chan, cookie, txstate);
809 if (ret == DMA_COMPLETE || !txstate)
812 spin_lock_irqsave(&c->vc.lock, flags);
813 vd = vchan_find_desc(&c->vc, cookie);
816 bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd->tx));
817 } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
818 struct bcm2835_desc *d = c->desc;
821 if (d->dir == DMA_MEM_TO_DEV && c->is_40bit_channel)
822 pos = readl(c->chan_base + BCM2711_DMA40_SRC) +
823 ((readl(c->chan_base + BCM2711_DMA40_SRCI) &
825 else if (d->dir == DMA_MEM_TO_DEV && !c->is_40bit_channel)
826 pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD);
827 else if (d->dir == DMA_DEV_TO_MEM && c->is_40bit_channel)
828 pos = readl(c->chan_base + BCM2711_DMA40_DEST) +
829 ((readl(c->chan_base + BCM2711_DMA40_DESTI) &
831 else if (d->dir == DMA_DEV_TO_MEM && !c->is_40bit_channel)
832 pos = readl(c->chan_base + BCM2835_DMA_DEST_AD);
836 txstate->residue = bcm2835_dma_desc_size_pos(d, pos);
838 txstate->residue = 0;
841 spin_unlock_irqrestore(&c->vc.lock, flags);
846 static void bcm2835_dma_issue_pending(struct dma_chan *chan)
848 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
851 spin_lock_irqsave(&c->vc.lock, flags);
852 if (vchan_issue_pending(&c->vc) && !c->desc)
853 bcm2835_dma_start_desc(c);
855 spin_unlock_irqrestore(&c->vc.lock, flags);
858 static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_memcpy(
859 struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
860 size_t len, unsigned long flags)
862 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
863 struct bcm2835_desc *d;
864 u32 info = BCM2835_DMA_D_INC | BCM2835_DMA_S_INC |
865 WIDE_SOURCE(c->dreq) | WIDE_DEST(c->dreq);
866 u32 extra = BCM2835_DMA_INT_EN | WAIT_RESP(c->dreq);
867 size_t max_len = bcm2835_dma_max_frame_length(c);
870 /* if src, dst or len is not given return with an error */
871 if (!src || !dst || !len)
874 /* calculate number of frames */
875 frames = bcm2835_dma_frames_for_length(len, max_len);
877 /* allocate the CB chain - this also fills in the pointers */
878 d = bcm2835_dma_create_cb_chain(c, DMA_MEM_TO_MEM, false,
880 src, dst, len, 0, GFP_KERNEL);
884 return vchan_tx_prep(&c->vc, &d->vd, flags);
887 static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg(
888 struct dma_chan *chan,
889 struct scatterlist *sgl, unsigned int sg_len,
890 enum dma_transfer_direction direction,
891 unsigned long flags, void *context)
893 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
894 struct bcm2835_desc *d;
895 dma_addr_t src = 0, dst = 0;
896 u32 info = WAIT_RESP(c->dreq) |
897 WIDE_SOURCE(c->dreq) | WIDE_DEST(c->dreq);
898 u32 extra = BCM2835_DMA_INT_EN;
901 if (!is_slave_direction(direction)) {
902 dev_err(chan->device->dev,
903 "%s: bad direction?\n", __func__);
908 info |= BCM2835_DMA_PER_MAP(c->dreq);
910 if (direction == DMA_DEV_TO_MEM) {
911 if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
913 src = c->cfg.src_addr;
915 * One would think it ought to be possible to get the physical
916 * to dma address mapping information from the dma-ranges DT
917 * property, but I've not found a way yet that doesn't involve
918 * open-coding the whole thing.
920 if (c->is_40bit_channel)
921 src |= 0x400000000ull;
922 info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
924 if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
926 dst = c->cfg.dst_addr;
927 if (c->is_40bit_channel)
928 dst |= 0x400000000ull;
929 info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
932 /* count frames in sg list */
933 frames = bcm2835_dma_count_frames_for_sg(c, sgl, sg_len);
935 /* allocate the CB chain */
936 d = bcm2835_dma_create_cb_chain(c, direction, false,
938 frames, src, dst, 0, 0,
943 /* fill in frames with scatterlist pointers */
944 bcm2835_dma_fill_cb_chain_with_sg(c, direction, d->cb_list,
947 return vchan_tx_prep(&c->vc, &d->vd, flags);
950 static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
951 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
952 size_t period_len, enum dma_transfer_direction direction,
955 struct bcm2835_dmadev *od = to_bcm2835_dma_dev(chan->device);
956 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
957 struct bcm2835_desc *d;
959 u32 info = WAIT_RESP(c->dreq) | WIDE_SOURCE(c->dreq) | WIDE_DEST(c->dreq);
961 size_t max_len = bcm2835_dma_max_frame_length(c);
964 /* Grab configuration */
965 if (!is_slave_direction(direction)) {
966 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
971 dev_err(chan->device->dev,
972 "%s: bad buffer length (= 0)\n", __func__);
976 if (flags & DMA_PREP_INTERRUPT)
977 extra |= BCM2835_DMA_INT_EN;
979 period_len = buf_len;
982 * warn if buf_len is not a multiple of period_len - this may leed
983 * to unexpected latencies for interrupts and thus audiable clicks
985 if (buf_len % period_len)
986 dev_warn_once(chan->device->dev,
987 "%s: buffer_length (%zd) is not a multiple of period_len (%zd)\n",
988 __func__, buf_len, period_len);
990 /* Setup DREQ channel */
992 info |= BCM2835_DMA_PER_MAP(c->dreq);
994 if (direction == DMA_DEV_TO_MEM) {
995 if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
997 src = c->cfg.src_addr;
998 if (c->is_40bit_channel)
999 src |= 0x400000000ull;
1001 info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
1003 if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
1005 dst = c->cfg.dst_addr;
1006 if (c->is_40bit_channel)
1007 dst |= 0x400000000ull;
1009 info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
1011 /* non-lite channels can write zeroes w/o accessing memory */
1012 if (buf_addr == od->zero_page && !c->is_lite_channel)
1013 info |= BCM2835_DMA_S_IGNORE;
1016 /* calculate number of frames */
1017 frames = /* number of periods */
1018 DIV_ROUND_UP(buf_len, period_len) *
1019 /* number of frames per period */
1020 bcm2835_dma_frames_for_length(period_len, max_len);
1023 * allocate the CB chain
1024 * note that we need to use GFP_NOWAIT, as the ALSA i2s dmaengine
1025 * implementation calls prep_dma_cyclic with interrupts disabled.
1027 d = bcm2835_dma_create_cb_chain(c, direction, true,
1029 frames, src, dst, buf_len,
1030 period_len, GFP_NOWAIT);
1034 /* wrap around into a loop */
1035 if (c->is_40bit_channel)
1036 ((struct bcm2711_dma40_scb *)
1037 d->cb_list[frames - 1].cb)->next_cb =
1038 to_bcm2711_cbaddr(d->cb_list[0].paddr);
1040 d->cb_list[d->frames - 1].cb->next = d->cb_list[0].paddr;
1042 return vchan_tx_prep(&c->vc, &d->vd, flags);
1045 static int bcm2835_dma_slave_config(struct dma_chan *chan,
1046 struct dma_slave_config *cfg)
1048 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
1055 static int bcm2835_dma_terminate_all(struct dma_chan *chan)
1057 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
1058 unsigned long flags;
1061 spin_lock_irqsave(&c->vc.lock, flags);
1063 /* stop DMA activity */
1065 vchan_terminate_vdesc(&c->desc->vd);
1067 bcm2835_dma_abort(c);
1070 vchan_get_all_descriptors(&c->vc, &head);
1071 spin_unlock_irqrestore(&c->vc.lock, flags);
1072 vchan_dma_desc_free_list(&c->vc, &head);
1077 static void bcm2835_dma_synchronize(struct dma_chan *chan)
1079 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
1081 vchan_synchronize(&c->vc);
1084 static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id,
1085 int irq, unsigned int irq_flags)
1087 struct bcm2835_chan *c;
1089 c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
1093 c->vc.desc_free = bcm2835_dma_desc_free;
1094 vchan_init(&c->vc, &d->ddev);
1096 c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
1098 c->irq_number = irq;
1099 c->irq_flags = irq_flags;
1101 /* check for 40bit and lite channels */
1102 if (d->cfg_data->chan_40bit_mask & BIT(chan_id))
1103 c->is_40bit_channel = true;
1104 else if (readl(c->chan_base + BCM2835_DMA_DEBUG) &
1105 BCM2835_DMA_DEBUG_LITE)
1106 c->is_lite_channel = true;
1111 static void bcm2835_dma_free(struct bcm2835_dmadev *od)
1113 struct bcm2835_chan *c, *next;
1115 list_for_each_entry_safe(c, next, &od->ddev.channels,
1116 vc.chan.device_node) {
1117 list_del(&c->vc.chan.device_node);
1118 tasklet_kill(&c->vc.task);
1121 dma_unmap_page_attrs(od->ddev.dev, od->zero_page, PAGE_SIZE,
1122 DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
1125 int bcm2711_dma40_memcpy_init(void)
1128 return -EPROBE_DEFER;
1138 EXPORT_SYMBOL(bcm2711_dma40_memcpy_init);
1140 void bcm2711_dma40_memcpy(dma_addr_t dst, dma_addr_t src, size_t size)
1142 struct bcm2711_dma40_scb *scb = memcpy_scb;
1143 unsigned long flags;
1146 pr_err("bcm2711_dma40_memcpy not initialised!\n");
1150 spin_lock_irqsave(&memcpy_lock, flags);
1153 scb->src = lower_32_bits(src);
1154 scb->srci = upper_32_bits(src) | BCM2711_DMA40_MEMCPY_XFER_INFO;
1155 scb->dst = lower_32_bits(dst);
1156 scb->dsti = upper_32_bits(dst) | BCM2711_DMA40_MEMCPY_XFER_INFO;
1160 writel((u32)(memcpy_scb_dma >> 5), memcpy_chan + BCM2711_DMA40_CB);
1161 writel(BCM2711_DMA40_MEMCPY_FLAGS + BCM2711_DMA40_ACTIVE,
1162 memcpy_chan + BCM2711_DMA40_CS);
1164 /* Poll for completion */
1165 while (!(readl(memcpy_chan + BCM2711_DMA40_CS) & BCM2711_DMA40_END))
1168 writel(BCM2711_DMA40_END, memcpy_chan + BCM2711_DMA40_CS);
1170 spin_unlock_irqrestore(&memcpy_lock, flags);
1172 EXPORT_SYMBOL(bcm2711_dma40_memcpy);
1174 static const struct of_device_id bcm2835_dma_of_match[] = {
1175 { .compatible = "brcm,bcm2835-dma", .data = &bcm2835_dma_cfg },
1176 { .compatible = "brcm,bcm2711-dma", .data = &bcm2711_dma_cfg },
1179 MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
1181 static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec,
1182 struct of_dma *ofdma)
1184 struct bcm2835_dmadev *d = ofdma->of_dma_data;
1185 struct dma_chan *chan;
1187 chan = dma_get_any_slave_channel(&d->ddev);
1191 /* Set DREQ from param */
1192 to_bcm2835_dma_chan(chan)->dreq = spec->args[0];
1197 static int bcm2835_dma_probe(struct platform_device *pdev)
1199 const struct bcm2835_dma_cfg_data *cfg_data;
1200 const struct of_device_id *of_id;
1201 struct bcm2835_dmadev *od;
1202 struct resource *res;
1206 int irq[BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED + 1];
1208 uint32_t chans_available;
1209 char chan_name[BCM2835_DMA_CHAN_NAME_SIZE];
1210 int chan_count, chan_start, chan_end;
1212 of_id = of_match_node(bcm2835_dma_of_match, pdev->dev.of_node);
1214 dev_err(&pdev->dev, "Failed to match compatible string\n");
1218 cfg_data = of_id->data;
1220 if (!pdev->dev.dma_mask)
1221 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
1223 rc = dma_set_mask_and_coherent(&pdev->dev, cfg_data->dma_mask);
1225 dev_err(&pdev->dev, "Unable to set DMA mask\n");
1229 od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
1233 dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
1235 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1236 base = devm_ioremap_resource(&pdev->dev, res);
1238 return PTR_ERR(base);
1240 /* The set of channels can be split across multiple instances. */
1241 chan_start = ((u32)(uintptr_t)base / BCM2835_DMA_CHAN_SIZE) & 0xf;
1242 base -= BCM2835_DMA_CHAN(chan_start);
1243 chan_count = resource_size(res) / BCM2835_DMA_CHAN_SIZE;
1244 chan_end = min(chan_start + chan_count,
1245 BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED + 1);
1249 dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
1250 dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
1251 dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
1252 dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask);
1253 od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
1254 od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
1255 od->ddev.device_tx_status = bcm2835_dma_tx_status;
1256 od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
1257 od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
1258 od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
1259 od->ddev.device_prep_dma_memcpy = bcm2835_dma_prep_dma_memcpy;
1260 od->ddev.device_config = bcm2835_dma_slave_config;
1261 od->ddev.device_terminate_all = bcm2835_dma_terminate_all;
1262 od->ddev.device_synchronize = bcm2835_dma_synchronize;
1263 od->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1264 od->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1265 od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
1266 BIT(DMA_MEM_TO_MEM);
1267 od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1268 od->ddev.descriptor_reuse = true;
1269 od->ddev.dev = &pdev->dev;
1270 INIT_LIST_HEAD(&od->ddev.channels);
1272 platform_set_drvdata(pdev, od);
1274 od->zero_page = dma_map_page_attrs(od->ddev.dev, ZERO_PAGE(0), 0,
1275 PAGE_SIZE, DMA_TO_DEVICE,
1276 DMA_ATTR_SKIP_CPU_SYNC);
1277 if (dma_mapping_error(od->ddev.dev, od->zero_page)) {
1278 dev_err(&pdev->dev, "Failed to map zero page\n");
1282 of_id = of_match_node(bcm2835_dma_of_match, pdev->dev.of_node);
1284 dev_err(&pdev->dev, "Failed to match compatible string\n");
1288 od->cfg_data = cfg_data;
1290 /* Request DMA channel mask from device tree */
1291 if (of_property_read_u32(pdev->dev.of_node,
1292 "brcm,dma-channel-mask",
1293 &chans_available)) {
1294 dev_err(&pdev->dev, "Failed to get channel mask\n");
1299 #ifdef CONFIG_DMA_BCM2708
1300 /* One channel is reserved for the legacy API */
1301 if (chans_available & BCM2835_DMA_BULK_MASK) {
1302 rc = bcm_dmaman_probe(pdev, base,
1303 chans_available & BCM2835_DMA_BULK_MASK);
1306 "Failed to initialize the legacy API\n");
1308 chans_available &= ~BCM2835_DMA_BULK_MASK;
1312 /* And possibly one for the 40-bit DMA memcpy API */
1313 if (chans_available & od->cfg_data->chan_40bit_mask &
1314 BIT(BCM2711_DMA_MEMCPY_CHAN)) {
1316 memcpy_chan = BCM2835_DMA_CHANIO(base, BCM2711_DMA_MEMCPY_CHAN);
1317 memcpy_scb = dma_alloc_coherent(memcpy_parent->ddev.dev,
1318 sizeof(*memcpy_scb),
1319 &memcpy_scb_dma, GFP_KERNEL);
1321 dev_warn(&pdev->dev,
1322 "Failed to allocated memcpy scb\n");
1324 chans_available &= ~BIT(BCM2711_DMA_MEMCPY_CHAN);
1327 /* get irqs for each channel that we support */
1328 for (i = chan_start; i < chan_end; i++) {
1329 /* skip masked out channels */
1330 if (!(chans_available & (1 << i))) {
1335 /* get the named irq */
1336 snprintf(chan_name, sizeof(chan_name), "dma%i", i);
1337 irq[i] = platform_get_irq_byname(pdev, chan_name);
1341 /* legacy device tree case handling */
1342 dev_warn_once(&pdev->dev,
1343 "missing interrupt-names property in device tree - legacy interpretation is used\n");
1345 * in case of channel >= 11
1346 * use the 11th interrupt and that is shared
1348 irq[i] = platform_get_irq(pdev, i < 11 ? i : 11);
1353 /* get irqs for each channel */
1354 for (i = chan_start; i < chan_end; i++) {
1355 /* skip channels without irq */
1359 /* check if there are other channels that also use this irq */
1360 /* FIXME: This will fail if interrupts are shared across
1363 for (j = 0; j <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; j++)
1364 if ((i != j) && (irq[j] == irq[i])) {
1365 irq_flags = IRQF_SHARED;
1369 /* initialize the channel */
1370 rc = bcm2835_dma_chan_init(od, i, irq[i], irq_flags);
1376 dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", chan_count);
1378 /* Device-tree DMA controller registration */
1379 rc = of_dma_controller_register(pdev->dev.of_node,
1380 bcm2835_dma_xlate, od);
1382 dev_err(&pdev->dev, "Failed to register DMA controller\n");
1386 rc = dma_async_device_register(&od->ddev);
1389 "Failed to register slave DMA engine device: %d\n", rc);
1393 dev_dbg(&pdev->dev, "Load BCM2835 DMA engine driver\n");
1398 bcm2835_dma_free(od);
1402 static int bcm2835_dma_remove(struct platform_device *pdev)
1404 struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
1406 bcm_dmaman_remove(pdev);
1407 dma_async_device_unregister(&od->ddev);
1408 if (memcpy_parent == od) {
1409 dma_free_coherent(&pdev->dev, sizeof(*memcpy_scb), memcpy_scb,
1411 memcpy_parent = NULL;
1415 bcm2835_dma_free(od);
1420 static struct platform_driver bcm2835_dma_driver = {
1421 .probe = bcm2835_dma_probe,
1422 .remove = bcm2835_dma_remove,
1424 .name = "bcm2835-dma",
1425 .of_match_table = of_match_ptr(bcm2835_dma_of_match),
1429 static int bcm2835_dma_init(void)
1431 return platform_driver_register(&bcm2835_dma_driver);
1434 static void bcm2835_dma_exit(void)
1436 platform_driver_unregister(&bcm2835_dma_driver);
1440 * Load after serial driver (arch_initcall) so we see the messages if it fails,
1441 * but before drivers (module_init) that need a DMA channel.
1443 subsys_initcall(bcm2835_dma_init);
1444 module_exit(bcm2835_dma_exit);
1446 MODULE_ALIAS("platform:bcm2835-dma");
1447 MODULE_DESCRIPTION("BCM2835 DMA engine driver");
1448 MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
1449 MODULE_LICENSE("GPL");