Skip to content

Commit c776a0f

Browse files
notropopcornmix
authored andcommitted
dmaengine: bcm2835: Add slave dma support
Add slave transfer capability to BCM2835 dmaengine driver. This patch is pulled from the bcm2708-dmaengine driver in the Raspberry Pi repo. The work was done by Gellert Weisz. Tested using the bcm2835-mmc driver from the same repo. Signed-off-by: Noralf Trønnes <[email protected]>
1 parent 99466dd commit c776a0f

File tree

1 file changed

+192
-14
lines changed

1 file changed

+192
-14
lines changed

drivers/dma/bcm2835-dma.c

Lines changed: 192 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/*
22
* BCM2835 DMA engine support
33
*
4-
* This driver only supports cyclic DMA transfers
5-
* as needed for the I2S module.
6-
*
74
* Author: Florian Meier <[email protected]>
85
* Copyright 2013
6+
* Gellert Weisz <[email protected]>
7+
* Copyright 2013-2014
98
*
109
* Based on
1110
* OMAP DMAengine support by Russell King
@@ -95,6 +94,8 @@ struct bcm2835_desc {
9594
size_t size;
9695
};
9796

97+
#define BCM2835_DMA_WAIT_CYCLES 0 /* Slow down DMA transfers: 0-31 */
98+
9899
#define BCM2835_DMA_CS 0x00
99100
#define BCM2835_DMA_ADDR 0x04
100101
#define BCM2835_DMA_SOURCE_AD 0x0c
@@ -111,12 +112,16 @@ struct bcm2835_desc {
111112
#define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */
112113

113114
#define BCM2835_DMA_INT_EN BIT(0)
115+
#define BCM2835_DMA_WAIT_RESP BIT(3)
114116
#define BCM2835_DMA_D_INC BIT(4)
117+
#define BCM2835_DMA_D_WIDTH BIT(5)
115118
#define BCM2835_DMA_D_DREQ BIT(6)
116119
#define BCM2835_DMA_S_INC BIT(8)
120+
#define BCM2835_DMA_S_WIDTH BIT(9)
117121
#define BCM2835_DMA_S_DREQ BIT(10)
118122

119123
#define BCM2835_DMA_PER_MAP(x) ((x) << 16)
124+
#define BCM2835_DMA_WAITS(x) (((x) & 0x1f) << 21)
120125

121126
#define BCM2835_DMA_DATA_TYPE_S8 1
122127
#define BCM2835_DMA_DATA_TYPE_S16 2
@@ -130,6 +135,14 @@ struct bcm2835_desc {
130135
#define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
131136
#define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
132137

138+
#define MAX_NORMAL_TRANSFER SZ_1G
139+
/*
140+
* Max length on a Lite channel is 65535 bytes.
141+
* DMA handles byte-enables on SDRAM reads and writes even on 128-bit accesses,
142+
* but byte-enables don't exist on peripheral addresses, so align to 32-bit.
143+
*/
144+
#define MAX_LITE_TRANSFER (SZ_64K - 4)
145+
133146
static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d)
134147
{
135148
return container_of(d, struct bcm2835_dmadev, ddev);
@@ -226,12 +239,18 @@ static irqreturn_t bcm2835_dma_callback(int irq, void *data)
226239
d = c->desc;
227240

228241
if (d) {
229-
/* TODO Only works for cyclic DMA */
230-
vchan_cyclic_callback(&d->vd);
231-
}
242+
if (c->cyclic) {
243+
vchan_cyclic_callback(&d->vd);
232244

233-
/* Keep the DMA engine running */
234-
writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS);
245+
/* Keep the DMA engine running */
246+
writel(BCM2835_DMA_ACTIVE,
247+
c->chan_base + BCM2835_DMA_CS);
248+
249+
} else {
250+
vchan_cookie_complete(&c->desc->vd);
251+
bcm2835_dma_start_desc(c);
252+
}
253+
}
235254

236255
spin_unlock_irqrestore(&c->vc.lock, flags);
237256

@@ -339,8 +358,6 @@ static void bcm2835_dma_issue_pending(struct dma_chan *chan)
339358
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
340359
unsigned long flags;
341360

342-
c->cyclic = true; /* Nothing else is implemented */
343-
344361
spin_lock_irqsave(&c->vc.lock, flags);
345362
if (vchan_issue_pending(&c->vc) && !c->desc)
346363
bcm2835_dma_start_desc(c);
@@ -358,7 +375,7 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
358375
struct bcm2835_desc *d;
359376
dma_addr_t dev_addr;
360377
unsigned int es, sync_type;
361-
unsigned int frame;
378+
unsigned int frame, max_size;
362379
int i;
363380

364381
/* Grab configuration */
@@ -393,7 +410,12 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
393410

394411
d->c = c;
395412
d->dir = direction;
396-
d->frames = buf_len / period_len;
413+
if (c->ch >= 8) /* LITE channel */
414+
max_size = MAX_LITE_TRANSFER;
415+
else
416+
max_size = MAX_NORMAL_TRANSFER;
417+
period_len = min(period_len, max_size);
418+
d->frames = (buf_len - 1) / (period_len + 1);
397419

398420
d->cb_list = kcalloc(d->frames, sizeof(*d->cb_list), GFP_KERNEL);
399421
if (!d->cb_list) {
@@ -441,17 +463,171 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
441463
BCM2835_DMA_PER_MAP(c->dreq);
442464

443465
/* Length of a frame */
444-
control_block->length = period_len;
466+
if (frame != d->frames - 1)
467+
control_block->length = period_len;
468+
else
469+
control_block->length = buf_len - (d->frames - 1) *
470+
period_len;
445471
d->size += control_block->length;
446472

447473
/*
448474
* Next block is the next frame.
449-
* This DMA engine driver currently only supports cyclic DMA.
475+
* This function is called on cyclic DMA transfers.
450476
* Therefore, wrap around at number of frames.
451477
*/
452478
control_block->next = d->cb_list[((frame + 1) % d->frames)].paddr;
453479
}
454480

481+
c->cyclic = true;
482+
483+
return vchan_tx_prep(&c->vc, &d->vd, flags);
484+
}
485+
486+
static struct dma_async_tx_descriptor *
487+
bcm2835_dma_prep_slave_sg(struct dma_chan *chan,
488+
struct scatterlist *sgl,
489+
unsigned int sg_len,
490+
enum dma_transfer_direction direction,
491+
unsigned long flags, void *context)
492+
{
493+
struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
494+
enum dma_slave_buswidth dev_width;
495+
struct bcm2835_desc *d;
496+
dma_addr_t dev_addr;
497+
struct scatterlist *sgent;
498+
unsigned int i, sync_type, split_cnt, max_size;
499+
500+
if (!is_slave_direction(direction)) {
501+
dev_err(chan->device->dev, "direction not supported\n");
502+
return NULL;
503+
}
504+
505+
if (direction == DMA_DEV_TO_MEM) {
506+
dev_addr = c->cfg.src_addr;
507+
dev_width = c->cfg.src_addr_width;
508+
sync_type = BCM2835_DMA_S_DREQ;
509+
} else {
510+
dev_addr = c->cfg.dst_addr;
511+
dev_width = c->cfg.dst_addr_width;
512+
sync_type = BCM2835_DMA_D_DREQ;
513+
}
514+
515+
/* Bus width translates to the element size (ES) */
516+
switch (dev_width) {
517+
case DMA_SLAVE_BUSWIDTH_4_BYTES:
518+
break;
519+
default:
520+
dev_err(chan->device->dev, "buswidth not supported: %i\n",
521+
dev_width);
522+
return NULL;
523+
}
524+
525+
/* Allocate and setup the descriptor. */
526+
d = kzalloc(sizeof(*d), GFP_NOWAIT);
527+
if (!d)
528+
return NULL;
529+
530+
d->dir = direction;
531+
532+
if (c->ch >= 8) /* LITE channel */
533+
max_size = MAX_LITE_TRANSFER;
534+
else
535+
max_size = MAX_NORMAL_TRANSFER;
536+
537+
/*
538+
* Store the length of the SG list in d->frames
539+
* taking care to account for splitting up transfers
540+
* too large for a LITE channel
541+
*/
542+
d->frames = 0;
543+
for_each_sg(sgl, sgent, sg_len, i) {
544+
unsigned int len = sg_dma_len(sgent);
545+
546+
d->frames += len / max_size + 1;
547+
}
548+
549+
/* Allocate memory for control blocks */
550+
d->control_block_size = d->frames * sizeof(struct bcm2835_dma_cb);
551+
d->control_block_base = dma_zalloc_coherent(chan->device->dev,
552+
d->control_block_size, &d->control_block_base_phys,
553+
GFP_NOWAIT);
554+
if (!d->control_block_base) {
555+
kfree(d);
556+
return NULL;
557+
}
558+
559+
/*
560+
* Iterate over all SG entries, create a control block
561+
* for each frame and link them together.
562+
* Count the number of times an SG entry had to be split
563+
* as a result of using a LITE channel
564+
*/
565+
split_cnt = 0;
566+
567+
for_each_sg(sgl, sgent, sg_len, i) {
568+
unsigned int j;
569+
dma_addr_t addr = sg_dma_address(sgent);
570+
unsigned int len = sg_dma_len(sgent);
571+
572+
for (j = 0; j < len; j += max_size) {
573+
struct bcm2835_dma_cb *control_block =
574+
&d->control_block_base[i + split_cnt];
575+
576+
/* Setup addresses */
577+
if (d->dir == DMA_DEV_TO_MEM) {
578+
control_block->info = BCM2835_DMA_D_INC |
579+
BCM2835_DMA_D_WIDTH |
580+
BCM2835_DMA_S_DREQ;
581+
control_block->src = dev_addr;
582+
control_block->dst = addr + (dma_addr_t)j;
583+
} else {
584+
control_block->info = BCM2835_DMA_S_INC |
585+
BCM2835_DMA_S_WIDTH |
586+
BCM2835_DMA_D_DREQ;
587+
control_block->src = addr + (dma_addr_t)j;
588+
control_block->dst = dev_addr;
589+
}
590+
591+
/* Common part */
592+
control_block->info |=
593+
BCM2835_DMA_WAITS(BCM2835_DMA_WAIT_CYCLES);
594+
control_block->info |= BCM2835_DMA_WAIT_RESP;
595+
596+
/* Enable */
597+
if (i == sg_len - 1 && len - j <= max_size)
598+
control_block->info |= BCM2835_DMA_INT_EN;
599+
600+
/* Setup synchronization */
601+
if (sync_type)
602+
control_block->info |= sync_type;
603+
604+
/* Setup DREQ channel */
605+
if (c->dreq)
606+
control_block->info |=
607+
BCM2835_DMA_PER_MAP(c->dreq);
608+
609+
/* Length of a frame */
610+
control_block->length = min(len - j, max_size);
611+
d->size += control_block->length;
612+
613+
if (i < sg_len - 1 || len - j > max_size) {
614+
/* Next block is the next frame. */
615+
control_block->next =
616+
d->control_block_base_phys +
617+
sizeof(struct bcm2835_dma_cb) *
618+
(i + split_cnt + 1);
619+
} else {
620+
/* Next block is empty. */
621+
control_block->next = 0;
622+
}
623+
624+
if (len - j > max_size)
625+
split_cnt++;
626+
}
627+
}
628+
629+
c->cyclic = false;
630+
455631
return vchan_tx_prep(&c->vc, &d->vd, flags);
456632
error_cb:
457633
i--;
@@ -620,6 +796,7 @@ static int bcm2835_dma_probe(struct platform_device *pdev)
620796
od->ddev.device_tx_status = bcm2835_dma_tx_status;
621797
od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
622798
od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
799+
od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
623800
od->ddev.device_config = bcm2835_dma_slave_config;
624801
od->ddev.device_terminate_all = bcm2835_dma_terminate_all;
625802
od->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
@@ -708,4 +885,5 @@ module_platform_driver(bcm2835_dma_driver);
708885
MODULE_ALIAS("platform:bcm2835-dma");
709886
MODULE_DESCRIPTION("BCM2835 DMA engine driver");
710887
MODULE_AUTHOR("Florian Meier <[email protected]>");
888+
MODULE_AUTHOR("Gellert Weisz <[email protected]>");
711889
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)