1 From 773a2db89ad2785d72b215673d87c0a51d769f61 Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <phil@raspberrypi.org>
3 Date: Thu, 4 Apr 2019 13:33:47 +0100
4 Subject: [PATCH] bcm2835-dma: Add proper 40-bit DMA support
6 The 40-bit additions are not fully tested, but it should be
7 capable of supporting both 40-bit memcpy on BCM2711 and regular
8 Lite channels on BCM2835.
10 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
12 drivers/dma/bcm2835-dma.c | 421 ++++++++++++++-----
13 drivers/pci/controller/pcie-brcmstb-bounce.c | 30 +-
14 drivers/pci/controller/pcie-brcmstb-bounce.h | 21 +-
15 drivers/pci/controller/pcie-brcmstb.c | 23 +-
16 4 files changed, 369 insertions(+), 126 deletions(-)
18 --- a/drivers/dma/bcm2835-dma.c
19 +++ b/drivers/dma/bcm2835-dma.c
21 #define BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED 14
22 #define BCM2835_DMA_CHAN_NAME_SIZE 8
23 #define BCM2835_DMA_BULK_MASK BIT(0)
24 +#define BCM2838_DMA_MEMCPY_CHAN 14
26 +struct bcm2835_dma_cfg_data {
27 + u32 chan_40bit_mask;
31 * struct bcm2835_dmadev - BCM2835 DMA controller
32 @@ -52,6 +57,7 @@ struct bcm2835_dmadev {
34 struct device_dma_parameters dma_parms;
36 + const struct bcm2835_dma_cfg_data *cfg_data;
39 struct bcm2835_dma_cb {
40 @@ -95,6 +101,7 @@ struct bcm2835_chan {
41 unsigned int irq_flags;
44 + bool is_40bit_channel;
48 @@ -184,7 +191,8 @@ struct bcm2835_desc {
49 #define BCM2835_DMA_DATA_TYPE_S128 16
51 /* Valid only for channels 0 - 14, 15 has its own base address */
52 -#define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
53 +#define BCM2835_DMA_CHAN_SIZE 0x100
54 +#define BCM2835_DMA_CHAN(n) ((n) * BCM2835_DMA_CHAN_SIZE) /* Base address */
55 #define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
57 /* the max dma length for different channels */
58 @@ -195,7 +203,7 @@ struct bcm2835_desc {
59 #define BCM2838_DMA40_CS 0x00
60 #define BCM2838_DMA40_CB 0x04
61 #define BCM2838_DMA40_DEBUG 0x0c
62 -#define BCM2858_DMA40_TI 0x10
63 +#define BCM2838_DMA40_TI 0x10
64 #define BCM2838_DMA40_SRC 0x14
65 #define BCM2838_DMA40_SRCI 0x18
66 #define BCM2838_DMA40_DEST 0x1c
67 @@ -204,32 +212,97 @@ struct bcm2835_desc {
68 #define BCM2838_DMA40_NEXT_CB 0x28
69 #define BCM2838_DMA40_DEBUG2 0x2c
71 -#define BCM2838_DMA40_CS_ACTIVE BIT(0)
72 -#define BCM2838_DMA40_CS_END BIT(1)
73 +#define BCM2838_DMA40_ACTIVE BIT(0)
74 +#define BCM2838_DMA40_END BIT(1)
75 +#define BCM2838_DMA40_INT BIT(2)
76 +#define BCM2838_DMA40_DREQ BIT(3) /* DREQ state */
77 +#define BCM2838_DMA40_RD_PAUSED BIT(4) /* Reading is paused */
78 +#define BCM2838_DMA40_WR_PAUSED BIT(5) /* Writing is paused */
79 +#define BCM2838_DMA40_DREQ_PAUSED BIT(6) /* Is paused by DREQ flow control */
80 +#define BCM2838_DMA40_WAITING_FOR_WRITES BIT(7) /* Waiting for last write */
81 +#define BCM2838_DMA40_ERR BIT(10)
82 +#define BCM2838_DMA40_QOS(x) (((x) & 0x1f) << 16)
83 +#define BCM2838_DMA40_PANIC_QOS(x) (((x) & 0x1f) << 20)
84 +#define BCM2838_DMA40_WAIT_FOR_WRITES BIT(28)
85 +#define BCM2838_DMA40_DISDEBUG BIT(29)
86 +#define BCM2838_DMA40_ABORT BIT(30)
87 +#define BCM2838_DMA40_HALT BIT(31)
88 +#define BCM2838_DMA40_CS_FLAGS(x) (x & (BCM2838_DMA40_QOS(15) | \
89 + BCM2838_DMA40_PANIC_QOS(15) | \
90 + BCM2838_DMA40_WAIT_FOR_WRITES | \
91 + BCM2838_DMA40_DISDEBUG))
93 +/* Transfer information bits */
94 +#define BCM2838_DMA40_INTEN BIT(0)
95 +#define BCM2838_DMA40_TDMODE BIT(1) /* 2D-Mode */
96 +#define BCM2838_DMA40_WAIT_RESP BIT(2) /* wait for AXI write to be acked */
97 +#define BCM2838_DMA40_WAIT_RD_RESP BIT(3) /* wait for AXI read to complete */
98 +#define BCM2838_DMA40_PER_MAP(x) ((x & 31) << 9) /* REQ source */
99 +#define BCM2838_DMA40_S_DREQ BIT(14) /* enable SREQ for source */
100 +#define BCM2838_DMA40_D_DREQ BIT(15) /* enable DREQ for destination */
101 +#define BCM2838_DMA40_S_WAIT(x) ((x & 0xff) << 16) /* add DMA read-wait cycles */
102 +#define BCM2838_DMA40_D_WAIT(x) ((x & 0xff) << 24) /* add DMA write-wait cycles */
104 -#define BCM2838_DMA40_CS_QOS(x) (((x) & 0x1f) << 16)
105 -#define BCM2838_DMA40_CS_PANIC_QOS(x) (((x) & 0x1f) << 20)
106 -#define BCM2838_DMA40_CS_WRITE_WAIT BIT(28)
107 +/* debug register bits */
108 +#define BCM2838_DMA40_DEBUG_WRITE_ERR BIT(0)
109 +#define BCM2838_DMA40_DEBUG_FIFO_ERR BIT(1)
110 +#define BCM2838_DMA40_DEBUG_READ_ERR BIT(2)
111 +#define BCM2838_DMA40_DEBUG_READ_CB_ERR BIT(3)
112 +#define BCM2838_DMA40_DEBUG_IN_ON_ERR BIT(8)
113 +#define BCM2838_DMA40_DEBUG_ABORT_ON_ERR BIT(9)
114 +#define BCM2838_DMA40_DEBUG_HALT_ON_ERR BIT(10)
115 +#define BCM2838_DMA40_DEBUG_DISABLE_CLK_GATE BIT(11)
116 +#define BCM2838_DMA40_DEBUG_RSTATE_SHIFT 14
117 +#define BCM2838_DMA40_DEBUG_RSTATE_BITS 4
118 +#define BCM2838_DMA40_DEBUG_WSTATE_SHIFT 18
119 +#define BCM2838_DMA40_DEBUG_WSTATE_BITS 4
120 +#define BCM2838_DMA40_DEBUG_RESET BIT(23)
121 +#define BCM2838_DMA40_DEBUG_ID_SHIFT 24
122 +#define BCM2838_DMA40_DEBUG_ID_BITS 4
123 +#define BCM2838_DMA40_DEBUG_VERSION_SHIFT 28
124 +#define BCM2838_DMA40_DEBUG_VERSION_BITS 4
126 +/* Valid only for channels 0 - 3 (11 - 14) */
127 +#define BCM2838_DMA40_CHAN(n) (((n) + 11) << 8) /* Base address */
128 +#define BCM2838_DMA40_CHANIO(base, n) ((base) + BCM2838_DMA_CHAN(n))
130 -#define BCM2838_DMA40_BURST_LEN(x) ((((x) - 1) & 0xf) << 8)
131 -#define BCM2838_DMA40_INC BIT(12)
132 -#define BCM2838_DMA40_SIZE_128 (2 << 13)
133 +/* the max dma length for different channels */
134 +#define MAX_DMA40_LEN SZ_1G
136 -#define BCM2838_DMA40_MEMCPY_QOS \
137 - (BCM2838_DMA40_CS_QOS(0x0) | \
138 - BCM2838_DMA40_CS_PANIC_QOS(0x0) | \
139 - BCM2838_DMA40_CS_WRITE_WAIT)
140 +#define BCM2838_DMA40_BURST_LEN(x) ((min(x,16) - 1) << 8)
141 +#define BCM2838_DMA40_INC BIT(12)
142 +#define BCM2838_DMA40_SIZE_32 (0 << 13)
143 +#define BCM2838_DMA40_SIZE_64 (1 << 13)
144 +#define BCM2838_DMA40_SIZE_128 (2 << 13)
145 +#define BCM2838_DMA40_SIZE_256 (3 << 13)
146 +#define BCM2838_DMA40_IGNORE BIT(15)
147 +#define BCM2838_DMA40_STRIDE(x) ((x) << 16) /* For 2D mode */
149 +#define BCM2838_DMA40_MEMCPY_FLAGS \
150 + (BCM2838_DMA40_QOS(0) | \
151 + BCM2838_DMA40_PANIC_QOS(0) | \
152 + BCM2838_DMA40_WAIT_FOR_WRITES | \
153 + BCM2838_DMA40_DISDEBUG)
155 #define BCM2838_DMA40_MEMCPY_XFER_INFO \
156 (BCM2838_DMA40_SIZE_128 | \
157 BCM2838_DMA40_INC | \
158 BCM2838_DMA40_BURST_LEN(16))
160 +struct bcm2835_dmadev *memcpy_parent;
161 static void __iomem *memcpy_chan;
162 static struct bcm2838_dma40_scb *memcpy_scb;
163 static dma_addr_t memcpy_scb_dma;
164 DEFINE_SPINLOCK(memcpy_lock);
166 +static const struct bcm2835_dma_cfg_data bcm2835_dma_cfg = {
167 + .chan_40bit_mask = 0,
170 +static const struct bcm2835_dma_cfg_data bcm2838_dma_cfg = {
171 + .chan_40bit_mask = BIT(11) | BIT(12) | BIT(13) | BIT(14),
174 static inline size_t bcm2835_dma_max_frame_length(struct bcm2835_chan *c)
176 /* lite and normal channels have different max frame length */
177 @@ -259,6 +332,32 @@ static inline struct bcm2835_desc *to_bc
178 return container_of(t, struct bcm2835_desc, vd.tx);
181 +static inline uint32_t to_bcm2838_ti(uint32_t info)
183 + return ((info & BCM2835_DMA_INT_EN) ? BCM2838_DMA40_INTEN : 0) |
184 + ((info & BCM2835_DMA_WAIT_RESP) ? BCM2838_DMA40_WAIT_RESP : 0) |
185 + ((info & BCM2835_DMA_S_DREQ) ?
186 + (BCM2838_DMA40_S_DREQ | BCM2838_DMA40_WAIT_RD_RESP) : 0) |
187 + ((info & BCM2835_DMA_D_DREQ) ? BCM2838_DMA40_D_DREQ : 0) |
188 + BCM2838_DMA40_PER_MAP((info >> 16) & 0x1f);
191 +static inline uint32_t to_bcm2838_srci(uint32_t info)
193 + return ((info & BCM2835_DMA_S_INC) ? BCM2838_DMA40_INC : 0);
196 +static inline uint32_t to_bcm2838_dsti(uint32_t info)
198 + return ((info & BCM2835_DMA_D_INC) ? BCM2838_DMA40_INC : 0);
201 +static inline uint32_t to_bcm2838_cbaddr(dma_addr_t addr)
203 + BUG_ON(addr & 0x1f);
204 + return (addr >> 5);
207 static void bcm2835_dma_free_cb_chain(struct bcm2835_desc *desc)
210 @@ -277,45 +376,53 @@ static void bcm2835_dma_desc_free(struct
213 static void bcm2835_dma_create_cb_set_length(
214 - struct bcm2835_chan *chan,
215 + struct bcm2835_chan *c,
216 struct bcm2835_dma_cb *control_block,
222 - size_t max_len = bcm2835_dma_max_frame_length(chan);
223 + size_t max_len = bcm2835_dma_max_frame_length(c);
226 /* set the length taking lite-channel limitations into account */
227 - control_block->length = min_t(u32, len, max_len);
228 + cb_len = min_t(u32, len, max_len);
230 - /* finished if we have no period_length */
235 + * period_len means: that we need to generate
236 + * transfers that are terminating at every
237 + * multiple of period_len - this is typically
238 + * used to set the interrupt flag in info
239 + * which is required during cyclic transfers
243 - * period_len means: that we need to generate
244 - * transfers that are terminating at every
245 - * multiple of period_len - this is typically
246 - * used to set the interrupt flag in info
247 - * which is required during cyclic transfers
249 + /* have we filled in period_length yet? */
250 + if (*total_len + cb_len < period_len) {
251 + /* update number of bytes in this period so far */
252 + *total_len += cb_len;
254 + /* calculate the length that remains to reach period_len */
255 + cb_len = period_len - *total_len;
257 - /* have we filled in period_length yet? */
258 - if (*total_len + control_block->length < period_len) {
259 - /* update number of bytes in this period so far */
260 - *total_len += control_block->length;
262 + /* reset total_length for next period */
267 - /* calculate the length that remains to reach period_length */
268 - control_block->length = period_len - *total_len;
270 - /* reset total_length for next period */
273 - /* add extrainfo bits in info */
274 - control_block->info |= finalextrainfo;
275 + if (c->is_40bit_channel) {
276 + struct bcm2838_dma40_scb *scb =
277 + (struct bcm2838_dma40_scb *)control_block;
280 + /* add extrainfo bits to ti */
281 + scb->ti |= to_bcm2838_ti(finalextrainfo);
283 + control_block->length = cb_len;
284 + /* add extrainfo bits to info */
285 + control_block->info |= finalextrainfo;
289 static inline size_t bcm2835_dma_count_frames_for_sg(
290 @@ -338,7 +445,7 @@ static inline size_t bcm2835_dma_count_f
292 * bcm2835_dma_create_cb_chain - create a control block and fills data in
294 - * @chan: the @dma_chan for which we run this
295 + * @c: the @bcm2835_chan for which we run this
296 * @direction: the direction in which we transfer
297 * @cyclic: it is a cyclic transfer
298 * @info: the default info bits to apply per controlblock
299 @@ -356,12 +463,11 @@ static inline size_t bcm2835_dma_count_f
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 + struct bcm2835_chan *c, enum dma_transfer_direction direction,
305 bool cyclic, u32 info, u32 finalextrainfo, size_t frames,
306 dma_addr_t src, dma_addr_t dst, size_t buf_len,
307 size_t period_len, gfp_t gfp)
309 - struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
310 size_t len = buf_len, total_len;
312 struct bcm2835_desc *d;
313 @@ -393,11 +499,23 @@ static struct bcm2835_desc *bcm2835_dma_
315 /* fill in the control block */
316 control_block = cb_entry->cb;
317 - control_block->info = info;
318 - control_block->src = src;
319 - control_block->dst = dst;
320 - control_block->stride = 0;
321 - control_block->next = 0;
322 + if (c->is_40bit_channel) {
323 + struct bcm2838_dma40_scb *scb =
324 + (struct bcm2838_dma40_scb *)control_block;
325 + scb->ti = to_bcm2838_ti(info);
326 + scb->src = lower_32_bits(src);
327 + scb->srci= upper_32_bits(src) | to_bcm2838_srci(info);
328 + scb->dst = lower_32_bits(dst);
329 + scb->dsti = upper_32_bits(dst) | to_bcm2838_dsti(info);
332 + control_block->info = info;
333 + control_block->src = src;
334 + control_block->dst = dst;
335 + control_block->stride = 0;
336 + control_block->next = 0;
339 /* set up length in control_block if requested */
341 /* calculate length honoring period_length */
342 @@ -411,7 +529,10 @@ static struct bcm2835_desc *bcm2835_dma_
345 /* link this the last controlblock */
347 + if (frame && c->is_40bit_channel)
348 + d->cb_list[frame - 1].cb->next =
349 + to_bcm2838_cbaddr(cb_entry->paddr);
350 + if (frame && !c->is_40bit_channel)
351 d->cb_list[frame - 1].cb->next = cb_entry->paddr;
353 /* update src and dst and length */
354 @@ -425,7 +546,14 @@ static struct bcm2835_desc *bcm2835_dma_
357 /* the last frame requires extra flags */
358 - d->cb_list[d->frames - 1].cb->info |= finalextrainfo;
359 + if (c->is_40bit_channel) {
360 + struct bcm2838_dma40_scb *scb =
361 + (struct bcm2838_dma40_scb *)d->cb_list[d->frames-1].cb;
363 + scb->ti |= to_bcm2838_ti(finalextrainfo);
365 + d->cb_list[d->frames - 1].cb->info |= finalextrainfo;
368 /* detect a size missmatch */
369 if (buf_len && (d->size != buf_len))
370 @@ -439,13 +567,12 @@ error_cb:
373 static void bcm2835_dma_fill_cb_chain_with_sg(
374 - struct dma_chan *chan,
375 + struct bcm2835_chan *c,
376 enum dma_transfer_direction direction,
377 struct bcm2835_cb_entry *cb,
378 struct scatterlist *sgl,
381 - struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
385 @@ -453,14 +580,34 @@ static void bcm2835_dma_fill_cb_chain_wi
387 max_len = bcm2835_dma_max_frame_length(c);
388 for_each_sg(sgl, sgent, sg_len, i) {
389 - for (addr = sg_dma_address(sgent), len = sg_dma_len(sgent);
391 - addr += cb->cb->length, len -= cb->cb->length, cb++) {
392 - if (direction == DMA_DEV_TO_MEM)
393 - cb->cb->dst = addr;
395 - cb->cb->src = addr;
396 - cb->cb->length = min(len, max_len);
397 + if (c->is_40bit_channel) {
398 + struct bcm2838_dma40_scb *scb =
399 + (struct bcm2838_dma40_scb *)cb->cb;
400 + for (addr = sg_dma_address(sgent),
401 + len = sg_dma_len(sgent);
403 + addr += scb->len, len -= scb->len, scb++) {
404 + if (direction == DMA_DEV_TO_MEM) {
405 + scb->dst = lower_32_bits(addr);
406 + scb->dsti = upper_32_bits(addr) | BCM2838_DMA40_INC;
408 + scb->src = lower_32_bits(addr);
409 + scb->srci = upper_32_bits(addr) | BCM2838_DMA40_INC;
411 + scb->len = min(len, max_len);
414 + for (addr = sg_dma_address(sgent),
415 + len = sg_dma_len(sgent);
417 + addr += cb->cb->length, len -= cb->cb->length,
419 + if (direction == DMA_DEV_TO_MEM)
420 + cb->cb->dst = addr;
422 + cb->cb->src = addr;
423 + cb->cb->length = min(len, max_len);
428 @@ -469,6 +616,10 @@ static void bcm2835_dma_abort(struct bcm
430 void __iomem *chan_base = c->chan_base;
431 long int timeout = 10000;
432 + u32 wait_mask = BCM2835_DMA_WAITING_FOR_WRITES;
434 + if (c->is_40bit_channel)
435 + wait_mask = BCM2838_DMA40_WAITING_FOR_WRITES;
438 * A zero control block address means the channel is idle.
439 @@ -481,8 +632,7 @@ static void bcm2835_dma_abort(struct bcm
440 writel(0, chan_base + BCM2835_DMA_CS);
442 /* Wait for any current AXI transfer to complete */
443 - while ((readl(chan_base + BCM2835_DMA_CS) &
444 - BCM2835_DMA_WAITING_FOR_WRITES) && --timeout)
445 + while ((readl(chan_base + BCM2835_DMA_CS) & wait_mask) && --timeout)
448 /* Peripheral might be stuck and fail to signal AXI write responses */
449 @@ -507,9 +657,16 @@ static void bcm2835_dma_start_desc(struc
451 c->desc = d = to_bcm2835_dma_desc(&vd->tx);
453 - writel(d->cb_list[0].paddr, c->chan_base + BCM2835_DMA_ADDR);
454 - writel(BCM2835_DMA_ACTIVE | BCM2835_DMA_CS_FLAGS(c->dreq),
455 - c->chan_base + BCM2835_DMA_CS);
456 + if (c->is_40bit_channel) {
457 + writel(to_bcm2838_cbaddr(d->cb_list[0].paddr),
458 + c->chan_base + BCM2838_DMA40_CB);
459 + writel(BCM2838_DMA40_ACTIVE | BCM2838_DMA40_CS_FLAGS(c->dreq),
460 + c->chan_base + BCM2838_DMA40_CS);
462 + writel(d->cb_list[0].paddr, c->chan_base + BCM2835_DMA_ADDR);
463 + writel(BCM2835_DMA_ACTIVE | BCM2835_DMA_CS_FLAGS(c->dreq),
464 + c->chan_base + BCM2835_DMA_CS);
468 static irqreturn_t bcm2835_dma_callback(int irq, void *data)
469 @@ -537,7 +694,8 @@ static irqreturn_t bcm2835_dma_callback(
470 * will remain idle despite the ACTIVE flag being set.
472 writel(BCM2835_DMA_INT | BCM2835_DMA_ACTIVE |
473 - BCM2835_DMA_CS_FLAGS(c->dreq),
474 + (c->is_40bit_channel ? BCM2838_DMA40_CS_FLAGS(c->dreq) :
475 + BCM2835_DMA_CS_FLAGS(c->dreq)),
476 c->chan_base + BCM2835_DMA_CS);
479 @@ -640,9 +798,17 @@ static enum dma_status bcm2835_dma_tx_st
480 struct bcm2835_desc *d = c->desc;
483 - if (d->dir == DMA_MEM_TO_DEV)
484 + if (d->dir == DMA_MEM_TO_DEV && c->is_40bit_channel)
485 + pos = readl(c->chan_base + BCM2838_DMA40_SRC) +
486 + ((readl(c->chan_base + BCM2838_DMA40_SRCI) &
488 + else if (d->dir == DMA_MEM_TO_DEV && !c->is_40bit_channel)
489 pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD);
490 - else if (d->dir == DMA_DEV_TO_MEM)
491 + else if (d->dir == DMA_DEV_TO_MEM && c->is_40bit_channel)
492 + pos = readl(c->chan_base + BCM2838_DMA40_DEST) +
493 + ((readl(c->chan_base + BCM2838_DMA40_DESTI) &
495 + else if (d->dir == DMA_DEV_TO_MEM && !c->is_40bit_channel)
496 pos = readl(c->chan_base + BCM2835_DMA_DEST_AD);
499 @@ -688,7 +854,7 @@ static struct dma_async_tx_descriptor *b
500 frames = bcm2835_dma_frames_for_length(len, max_len);
502 /* allocate the CB chain - this also fills in the pointers */
503 - d = bcm2835_dma_create_cb_chain(chan, DMA_MEM_TO_MEM, false,
504 + d = bcm2835_dma_create_cb_chain(c, DMA_MEM_TO_MEM, false,
506 src, dst, len, 0, GFP_KERNEL);
508 @@ -723,11 +889,21 @@ static struct dma_async_tx_descriptor *b
509 if (c->cfg.src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
511 src = c->cfg.src_addr;
513 + * One would think it ought to be possible to get the physical
514 + * to dma address mapping information from the dma-ranges DT
515 + * property, but I've not found a way yet that doesn't involve
516 + * open-coding the whole thing.
518 + if (c->is_40bit_channel)
519 + src |= 0x400000000ull;
520 info |= BCM2835_DMA_S_DREQ | BCM2835_DMA_D_INC;
522 if (c->cfg.dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES)
524 dst = c->cfg.dst_addr;
525 + if (c->is_40bit_channel)
526 + dst |= 0x400000000ull;
527 info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
530 @@ -735,7 +911,7 @@ static struct dma_async_tx_descriptor *b
531 frames = bcm2835_dma_count_frames_for_sg(c, sgl, sg_len);
533 /* allocate the CB chain */
534 - d = bcm2835_dma_create_cb_chain(chan, direction, false,
535 + d = bcm2835_dma_create_cb_chain(c, direction, false,
537 frames, src, dst, 0, 0,
539 @@ -743,7 +919,7 @@ static struct dma_async_tx_descriptor *b
542 /* fill in frames with scatterlist pointers */
543 - bcm2835_dma_fill_cb_chain_with_sg(chan, direction, d->cb_list,
544 + bcm2835_dma_fill_cb_chain_with_sg(c, direction, d->cb_list,
547 return vchan_tx_prep(&c->vc, &d->vd, flags);
548 @@ -822,7 +998,7 @@ static struct dma_async_tx_descriptor *b
549 * note that we need to use GFP_NOWAIT, as the ALSA i2s dmaengine
550 * implementation calls prep_dma_cyclic with interrupts disabled.
552 - d = bcm2835_dma_create_cb_chain(chan, direction, true,
553 + d = bcm2835_dma_create_cb_chain(c, direction, true,
555 frames, src, dst, buf_len,
556 period_len, GFP_NOWAIT);
557 @@ -830,7 +1006,8 @@ static struct dma_async_tx_descriptor *b
560 /* wrap around into a loop */
561 - d->cb_list[d->frames - 1].cb->next = d->cb_list[0].paddr;
562 + d->cb_list[d->frames - 1].cb->next = c->is_40bit_channel ?
563 + to_bcm2838_cbaddr(d->cb_list[0].paddr) : d->cb_list[0].paddr;
565 return vchan_tx_prep(&c->vc, &d->vd, flags);
567 @@ -894,9 +1071,11 @@ static int bcm2835_dma_chan_init(struct
569 c->irq_flags = irq_flags;
571 - /* check in DEBUG register if this is a LITE channel */
572 - if (readl(c->chan_base + BCM2835_DMA_DEBUG) &
573 - BCM2835_DMA_DEBUG_LITE)
574 + /* check for 40bit and lite channels */
575 + if (d->cfg_data->chan_40bit_mask & BIT(chan_id))
576 + c->is_40bit_channel = true;
577 + else if (readl(c->chan_base + BCM2835_DMA_DEBUG) &
578 + BCM2835_DMA_DEBUG_LITE)
579 c->is_lite_channel = true;
582 @@ -916,18 +1095,16 @@ static void bcm2835_dma_free(struct bcm2
583 DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
586 -int bcm2838_dma40_memcpy_init(struct device *dev)
587 +int bcm2838_dma40_memcpy_init(void)
591 + if (!memcpy_parent)
592 + return -EPROBE_DEFER;
594 - memcpy_scb = dma_alloc_coherent(dev, sizeof(*memcpy_scb),
595 - &memcpy_scb_dma, GFP_KERNEL);
600 - pr_err("bcm2838_dma40_memcpy_init failed!\n");
607 @@ -954,20 +1131,22 @@ void bcm2838_dma40_memcpy(dma_addr_t dst
610 writel((u32)(memcpy_scb_dma >> 5), memcpy_chan + BCM2838_DMA40_CB);
611 - writel(BCM2838_DMA40_MEMCPY_QOS + BCM2838_DMA40_CS_ACTIVE,
612 + writel(BCM2838_DMA40_MEMCPY_FLAGS + BCM2838_DMA40_ACTIVE,
613 memcpy_chan + BCM2838_DMA40_CS);
615 /* Poll for completion */
616 - while (!(readl(memcpy_chan + BCM2838_DMA40_CS) & BCM2838_DMA40_CS_END))
617 + while (!(readl(memcpy_chan + BCM2838_DMA40_CS) & BCM2838_DMA40_END))
620 - writel(BCM2838_DMA40_CS_END, memcpy_chan + BCM2838_DMA40_CS);
621 + writel(BCM2838_DMA40_END, memcpy_chan + BCM2838_DMA40_CS);
623 spin_unlock_irqrestore(&memcpy_lock, flags);
625 EXPORT_SYMBOL(bcm2838_dma40_memcpy);
627 static const struct of_device_id bcm2835_dma_of_match[] = {
628 - { .compatible = "brcm,bcm2835-dma", },
629 + { .compatible = "brcm,bcm2835-dma", .data = &bcm2835_dma_cfg },
630 + { .compatible = "brcm,bcm2838-dma", .data = &bcm2838_dma_cfg },
633 MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
634 @@ -999,6 +1178,8 @@ static int bcm2835_dma_probe(struct plat
636 uint32_t chans_available;
637 char chan_name[BCM2835_DMA_CHAN_NAME_SIZE];
638 + const struct of_device_id *of_id;
639 + int chan_count, chan_start, chan_end;
641 if (!pdev->dev.dma_mask)
642 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
643 @@ -1020,9 +1201,13 @@ static int bcm2835_dma_probe(struct plat
644 base = devm_ioremap_resource(&pdev->dev, res);
646 return PTR_ERR(base);
647 - rc = bcm_dmaman_probe(pdev, base, BCM2835_DMA_BULK_MASK);
649 - dev_err(&pdev->dev, "Failed to initialize the legacy API\n");
651 + /* The set of channels can be split across multiple instances. */
652 + chan_start = ((u32)(uintptr_t)base / BCM2835_DMA_CHAN_SIZE) & 0xf;
653 + base -= BCM2835_DMA_CHAN(chan_start);
654 + chan_count = resource_size(res) / BCM2835_DMA_CHAN_SIZE;
655 + chan_end = min(chan_start + chan_count,
656 + BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED + 1);
660 @@ -1059,6 +1244,14 @@ static int bcm2835_dma_probe(struct plat
664 + of_id = of_match_node(bcm2835_dma_of_match, pdev->dev.of_node);
666 + dev_err(&pdev->dev, "Failed to match compatible string\n");
670 + od->cfg_data = of_id->data;
672 /* Request DMA channel mask from device tree */
673 if (of_property_read_u32(pdev->dev.of_node,
674 "brcm,dma-channel-mask",
675 @@ -1068,18 +1261,34 @@ static int bcm2835_dma_probe(struct plat
679 - /* Channel 0 is used by the legacy API */
680 - chans_available &= ~BCM2835_DMA_BULK_MASK;
681 + /* One channel is reserved for the legacy API */
682 + if (chans_available & BCM2835_DMA_BULK_MASK) {
683 + rc = bcm_dmaman_probe(pdev, base,
684 + chans_available & BCM2835_DMA_BULK_MASK);
686 + dev_err(&pdev->dev,
687 + "Failed to initialize the legacy API\n");
689 + chans_available &= ~BCM2835_DMA_BULK_MASK;
692 - /* We can't use channels 11-13 yet */
693 - chans_available &= ~(BIT(11) | BIT(12) | BIT(13));
694 + /* And possibly one for the 40-bit DMA memcpy API */
695 + if (chans_available & od->cfg_data->chan_40bit_mask &
696 + BIT(BCM2838_DMA_MEMCPY_CHAN)) {
697 + memcpy_parent = od;
698 + memcpy_chan = BCM2835_DMA_CHANIO(base, BCM2838_DMA_MEMCPY_CHAN);
699 + memcpy_scb = dma_alloc_coherent(memcpy_parent->ddev.dev,
700 + sizeof(*memcpy_scb),
701 + &memcpy_scb_dma, GFP_KERNEL);
703 + dev_warn(&pdev->dev,
704 + "Failed to allocated memcpy scb\n");
706 - /* Grab channel 14 for the 40-bit DMA memcpy */
707 - chans_available &= ~BIT(14);
708 - memcpy_chan = BCM2835_DMA_CHANIO(base, 14);
709 + chans_available &= ~BIT(BCM2838_DMA_MEMCPY_CHAN);
712 /* get irqs for each channel that we support */
713 - for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
714 + for (i = chan_start; i < chan_end; i++) {
715 /* skip masked out channels */
716 if (!(chans_available & (1 << i))) {
718 @@ -1102,13 +1311,17 @@ static int bcm2835_dma_probe(struct plat
719 irq[i] = platform_get_irq(pdev, i < 11 ? i : 11);
724 /* get irqs for each channel */
725 - for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
726 + for (i = chan_start; i < chan_end; i++) {
727 /* skip channels without irq */
731 /* check if there are other channels that also use this irq */
732 + /* FIXME: This will fail if interrupts are shared across
735 for (j = 0; j <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; j++)
736 if ((i != j) && (irq[j] == irq[i])) {
737 @@ -1120,9 +1333,10 @@ static int bcm2835_dma_probe(struct plat
738 rc = bcm2835_dma_chan_init(od, i, irq[i], irq_flags);
744 - dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);
745 + dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", chan_count);
747 /* Device-tree DMA controller registration */
748 rc = of_dma_controller_register(pdev->dev.of_node,
749 @@ -1154,6 +1368,13 @@ static int bcm2835_dma_remove(struct pla
751 bcm_dmaman_remove(pdev);
752 dma_async_device_unregister(&od->ddev);
753 + if (memcpy_parent == od) {
754 + dma_free_coherent(&pdev->dev, sizeof(*memcpy_scb), memcpy_scb,
756 + memcpy_parent = NULL;
758 + memcpy_chan = NULL;
760 bcm2835_dma_free(od);
763 --- a/drivers/pci/controller/pcie-brcmstb-bounce.c
764 +++ b/drivers/pci/controller/pcie-brcmstb-bounce.c
765 @@ -91,7 +91,7 @@ struct dmabounce_device_info {
767 static struct dmabounce_device_info *g_dmabounce_device_info;
769 -extern int bcm2838_dma40_memcpy_init(struct device *dev);
770 +extern int bcm2838_dma40_memcpy_init(void);
771 extern void bcm2838_dma40_memcpy(dma_addr_t dst, dma_addr_t src, size_t size);
774 @@ -465,9 +465,9 @@ static const struct dma_map_ops dmabounc
775 .dma_supported = dmabounce_dma_supported,
778 -int brcm_pcie_bounce_register_dev(struct device *dev,
779 - unsigned long buffer_size,
780 - dma_addr_t threshold)
781 +int brcm_pcie_bounce_init(struct device *dev,
782 + unsigned long buffer_size,
783 + dma_addr_t threshold)
785 struct dmabounce_device_info *device_info;
787 @@ -476,9 +476,9 @@ int brcm_pcie_bounce_register_dev(struct
788 if (g_dmabounce_device_info)
791 - ret = bcm2838_dma40_memcpy_init(dev);
792 + ret = bcm2838_dma40_memcpy_init();
797 device_info = kmalloc(sizeof(struct dmabounce_device_info), GFP_ATOMIC);
799 @@ -509,9 +509,8 @@ int brcm_pcie_bounce_register_dev(struct
800 device_create_file(dev, &dev_attr_dmabounce_stats));
802 g_dmabounce_device_info = device_info;
803 - set_dma_ops(dev, &dmabounce_ops);
805 - dev_info(dev, "dmabounce: registered device - %ld kB, threshold %pad\n",
806 + dev_info(dev, "dmabounce: initialised - %ld kB, threshold %pad\n",
807 buffer_size / 1024, &threshold);
810 @@ -520,14 +519,13 @@ int brcm_pcie_bounce_register_dev(struct
814 -EXPORT_SYMBOL(brcm_pcie_bounce_register_dev);
815 +EXPORT_SYMBOL(brcm_pcie_bounce_init);
817 -void brcm_pcie_bounce_unregister_dev(struct device *dev)
818 +void brcm_pcie_bounce_uninit(struct device *dev)
820 struct dmabounce_device_info *device_info = g_dmabounce_device_info;
822 g_dmabounce_device_info = NULL;
823 - set_dma_ops(dev, NULL);
827 @@ -548,10 +546,16 @@ void brcm_pcie_bounce_unregister_dev(str
828 device_remove_file(dev, &dev_attr_dmabounce_stats));
832 +EXPORT_SYMBOL(brcm_pcie_bounce_uninit);
834 +int brcm_pcie_bounce_register_dev(struct device *dev)
836 + set_dma_ops(dev, &dmabounce_ops);
838 - dev_info(dev, "dmabounce: device unregistered\n");
841 -EXPORT_SYMBOL(brcm_pcie_bounce_unregister_dev);
842 +EXPORT_SYMBOL(brcm_pcie_bounce_register_dev);
844 MODULE_AUTHOR("Phil Elwell <phil@raspberrypi.org>");
845 MODULE_DESCRIPTION("Dedicate DMA bounce support for pcie-brcmstb");
846 --- a/drivers/pci/controller/pcie-brcmstb-bounce.h
847 +++ b/drivers/pci/controller/pcie-brcmstb-bounce.h
852 -int brcm_pcie_bounce_register_dev(struct device *dev, unsigned long buffer_size,
853 - dma_addr_t threshold);
855 -int brcm_pcie_bounce_unregister_dev(struct device *dev);
856 +int brcm_pcie_bounce_init(struct device *dev, unsigned long buffer_size,
857 + dma_addr_t threshold);
858 +int brcm_pcie_bounce_uninit(struct device *dev);
859 +int brcm_pcie_bounce_register_dev(struct device *dev);
863 -static inline int brcm_pcie_bounce_register_dev(struct device *dev,
864 - unsigned long buffer_size,
865 - dma_addr_t threshold)
866 +static inline int brcm_pcie_bounce_init(struct device *dev,
867 + unsigned long buffer_size,
868 + dma_addr_t threshold)
873 +static inline int brcm_pcie_bounce_uninit(struct device *dev)
878 -static inline int brcm_pcie_bounce_unregister_dev(struct device *dev)
879 +static inline int brcm_pcie_bounce_register_dev(struct device *dev)
883 --- a/drivers/pci/controller/pcie-brcmstb.c
884 +++ b/drivers/pci/controller/pcie-brcmstb.c
885 @@ -644,6 +644,7 @@ static void brcm_set_dma_ops(struct devi
887 static inline void brcm_pcie_perst_set(struct brcm_pcie *pcie,
890 static int brcmstb_platform_notifier(struct notifier_block *nb,
891 unsigned long event, void *__dev)
893 @@ -657,12 +658,11 @@ static int brcmstb_platform_notifier(str
894 strcmp(dev->kobj.name, rc_name)) {
897 - ret = brcm_pcie_bounce_register_dev(dev, bounce_buffer,
898 - (dma_addr_t)bounce_threshold);
899 + ret = brcm_pcie_bounce_register_dev(dev);
902 "brcm_pcie_bounce_register_dev() failed: %d\n",
908 @@ -675,8 +675,6 @@ static int brcmstb_platform_notifier(str
909 brcm_pcie_perst_set(g_pcie, 1);
911 brcm_pcie_perst_set(g_pcie, 0);
912 - } else if (max_pfn > (bounce_threshold/PAGE_SIZE)) {
913 - brcm_pcie_bounce_unregister_dev(dev);
917 @@ -1712,6 +1710,7 @@ static int brcm_pcie_probe(struct platfo
919 struct pci_host_bridge *bridge;
920 struct pci_bus *child;
921 + extern unsigned long max_pfn;
923 bridge = devm_pci_alloc_host_bridge(&pdev->dev, sizeof(*pcie));
925 @@ -1747,6 +1746,20 @@ static int brcm_pcie_probe(struct platfo
927 return PTR_ERR(base);
929 + /* To Do: Add hardware check if this ever gets fixed */
930 + if (max_pfn > (bounce_threshold/PAGE_SIZE)) {
932 + ret = brcm_pcie_bounce_init(&pdev->dev, bounce_buffer,
933 + (dma_addr_t)bounce_threshold);
935 + if (ret != -EPROBE_DEFER)
936 + dev_err(&pdev->dev,
937 + "could not init bounce buffers: %d\n",
943 pcie->clk = of_clk_get_by_name(dn, "sw_pcie");
944 if (IS_ERR(pcie->clk)) {
945 dev_warn(&pdev->dev, "could not get clock\n");