[PATCH v3 20/28] dmaengine: bcm2835: Support channel subsets in a controller instance

From: Andrea della Porta

Date: Mon Nov 23 2026 - 09:55:56 EST


On BCM2711 the last four channels of the DMA block use a different, 40-bit
programming model, and are going to be described by a separate DT node
whose reg property only covers those channels. The driver however
assumes that the register range always starts with channel 0.

Derive the index of the first channel from the physical address of the
register range, which is possible since the DMA block is 4 KiB aligned,
and only consider the channels covered by it. Channel numbers, and thus
the dma-channel-mask and interrupt-names properties, keep referring to
the whole DMA block.

Also describe in the platform data which channels of the block use its
programming model, and ignore the other ones: some device trees, e.g. the
downstream BCM2712 one, describe channels with different programming
models using the same compatible, and driving them with the wrong one
could corrupt memory.

While at it, initialize all the entries of the IRQ array, as the
detection of shared interrupts looks at all of them, and report the
number of channels actually initialized.

Signed-off-by: Andrea della Porta <andrea.porta@xxxxxxxx>
---
drivers/dma/bcm2835-dma.c | 58 +++++++++++++++++++++++++++++++++------
1 file changed, 49 insertions(+), 9 deletions(-)

diff --git a/drivers/dma/bcm2835-dma.c b/drivers/dma/bcm2835-dma.c
index 3ff30c41dc63..0ea903274286 100644
--- a/drivers/dma/bcm2835-dma.c
+++ b/drivers/dma/bcm2835-dma.c
@@ -42,6 +42,7 @@
* struct bcm2835_dmadev - BCM2835 DMA controller
* @ddev: DMA device
* @base: base address of register map
+ * @chan_start: index of the first channel in the register map
* @zero_page: bus address of zero page (to detect transactions copying from
* zero page and avoid accessing memory if so)
* @cfg: platform specific description of the DMA channels
@@ -51,6 +52,7 @@
struct bcm2835_dmadev {
struct dma_device ddev;
void __iomem *base;
+ unsigned int chan_start;
dma_addr_t zero_page;
const struct bcm2835_dma_cfg *cfg;
bool map_resource;
@@ -127,6 +129,7 @@ struct bcm2835_chan {
* @abort: stop and reset a channel
* @legacy_bus_addr: translate a slave address for device trees which do not
* describe the peripheral window in dma-ranges
+ * @chan_mask: channels of the DMA block using this programming model
*/
struct bcm2835_dma_cfg {
u32 cs_reg;
@@ -161,6 +164,7 @@ struct bcm2835_dma_cfg {
void (*abort)(struct bcm2835_chan *c);

u64 (*legacy_bus_addr)(struct device *dev, phys_addr_t phys);
+ u32 chan_mask;
};

struct bcm2835_desc {
@@ -271,7 +275,8 @@ struct bcm2835_desc {
#define BCM2835_DMA_DATA_TYPE_S128 16

/* Valid only for channels 0 - 14, 15 has its own base address */
-#define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
+#define BCM2835_DMA_CHAN_SIZE 0x100
+#define BCM2835_DMA_CHAN(n) ((n) * BCM2835_DMA_CHAN_SIZE) /* Base address */
#define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))

/* the max dma length for different channels */
@@ -1189,7 +1194,7 @@ static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id,
c->vc.desc_free = bcm2835_dma_desc_free;
vchan_init(&c->vc, &d->ddev);

- c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
+ c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id - d->chan_start);
c->ch = chan_id;
c->irq_number = irq;
c->irq_flags = irq_flags;
@@ -1237,6 +1242,7 @@ static const struct bcm2835_dma_cfg bcm2835_data = {
.abort = bcm2835_dma_abort_chan,

.legacy_bus_addr = bcm2835_dma_legacy_bus_addr,
+ .chan_mask = GENMASK(BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED, 0),
};

static const struct of_device_id bcm2835_dma_of_match[] = {
@@ -1335,6 +1341,7 @@ static int bcm2835_dma_probe(struct platform_device *pdev)
int irq_flags;
u32 chans_available;
char chan_name[BCM2835_DMA_CHAN_NAME_SIZE];
+ int chan_count, chan_start, chan_end;

if (!pdev->dev.dma_mask)
pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
@@ -1360,7 +1367,19 @@ static int bcm2835_dma_probe(struct platform_device *pdev)
if (IS_ERR(base))
return PTR_ERR(base);

+ /*
+ * The channels of a DMA block can be split across multiple instances,
+ * e.g. because they have a different programming model. The block
+ * itself is 4 KiB aligned, so the position of the first channel of
+ * this instance can be derived from the physical register address.
+ */
+ chan_start = (res->start / BCM2835_DMA_CHAN_SIZE) & 0xf;
+ chan_count = resource_size(res) / BCM2835_DMA_CHAN_SIZE;
+ chan_end = min(chan_start + chan_count,
+ BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED + 1);
+
od->base = base;
+ od->chan_start = chan_start;

od->map_resource = bcm2835_dma_mmio_in_dma_ranges(&pdev->dev,
res->start);
@@ -1417,13 +1436,30 @@ static int bcm2835_dma_probe(struct platform_device *pdev)
dev_warn(&pdev->dev, "Please update DT blob\n");
}

+ /*
+ * Never drive channels with a different programming model, which
+ * some device trees put in the same node.
+ */
+ if (chans_available & ~od->cfg->chan_mask) {
+ dev_warn(&pdev->dev, "ignoring unsupported channels %#x\n",
+ chans_available & ~od->cfg->chan_mask);
+ chans_available &= od->cfg->chan_mask;
+ if (!chans_available) {
+ rc = -ENODEV;
+ goto err_no_dma;
+ }
+ }
+
+ for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++)
+ irq[i] = -1;
+
/* get irqs for each channel that we support */
- for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
+ for (i = chan_start; i < chan_end; i++) {
+ int index = i - chan_start;
+
/* skip masked out channels */
- if (!(chans_available & (1 << i))) {
- irq[i] = -1;
+ if (!(chans_available & (1 << i)))
continue;
- }

/* get the named irq */
snprintf(chan_name, sizeof(chan_name), "dma%i", i);
@@ -1438,11 +1474,13 @@ static int bcm2835_dma_probe(struct platform_device *pdev)
* in case of channel >= 11
* use the 11th interrupt and that is shared
*/
- irq[i] = platform_get_irq(pdev, i < 11 ? i : 11);
+ irq[i] = platform_get_irq(pdev, index < 11 ? index : 11);
}

+ chan_count = 0;
+
/* get irqs for each channel */
- for (i = 0; i <= BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED; i++) {
+ for (i = chan_start; i < chan_end; i++) {
/* skip channels without irq */
if (irq[i] < 0)
continue;
@@ -1459,9 +1497,11 @@ static int bcm2835_dma_probe(struct platform_device *pdev)
rc = bcm2835_dma_chan_init(od, i, irq[i], irq_flags);
if (rc)
goto err_no_dma;
+
+ chan_count++;
}

- dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);
+ dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", chan_count);

/* Device-tree DMA controller registration */
rc = of_dma_controller_register(pdev->dev.of_node,
--
2.51.0