1
1
/*
2
2
* BCM2835 DMA engine support
3
3
*
4
- * This driver only supports cyclic DMA transfers
5
- * as needed for the I2S module.
6
- *
7
4
* Author: Florian Meier <[email protected] >
8
5
* Copyright 2013
6
+ * Gellert Weisz <[email protected] >
7
+ * Copyright 2013-2014
9
8
*
10
9
* Based on
11
10
* OMAP DMAengine support by Russell King
@@ -95,6 +94,8 @@ struct bcm2835_desc {
95
94
size_t size ;
96
95
};
97
96
97
+ #define BCM2835_DMA_WAIT_CYCLES 0 /* Slow down DMA transfers: 0-31 */
98
+
98
99
#define BCM2835_DMA_CS 0x00
99
100
#define BCM2835_DMA_ADDR 0x04
100
101
#define BCM2835_DMA_SOURCE_AD 0x0c
@@ -111,12 +112,16 @@ struct bcm2835_desc {
111
112
#define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */
112
113
113
114
#define BCM2835_DMA_INT_EN BIT(0)
115
+ #define BCM2835_DMA_WAIT_RESP BIT(3)
114
116
#define BCM2835_DMA_D_INC BIT(4)
117
+ #define BCM2835_DMA_D_WIDTH BIT(5)
115
118
#define BCM2835_DMA_D_DREQ BIT(6)
116
119
#define BCM2835_DMA_S_INC BIT(8)
120
+ #define BCM2835_DMA_S_WIDTH BIT(9)
117
121
#define BCM2835_DMA_S_DREQ BIT(10)
118
122
119
123
#define BCM2835_DMA_PER_MAP (x ) ((x) << 16)
124
+ #define BCM2835_DMA_WAITS (x ) (((x) & 0x1f) << 21)
120
125
121
126
#define BCM2835_DMA_DATA_TYPE_S8 1
122
127
#define BCM2835_DMA_DATA_TYPE_S16 2
@@ -130,6 +135,14 @@ struct bcm2835_desc {
130
135
#define BCM2835_DMA_CHAN (n ) ((n) << 8) /* Base address */
131
136
#define BCM2835_DMA_CHANIO (base , n ) ((base) + BCM2835_DMA_CHAN(n))
132
137
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
+
133
146
static inline struct bcm2835_dmadev * to_bcm2835_dma_dev (struct dma_device * d )
134
147
{
135
148
return container_of (d , struct bcm2835_dmadev , ddev );
@@ -226,12 +239,18 @@ static irqreturn_t bcm2835_dma_callback(int irq, void *data)
226
239
d = c -> desc ;
227
240
228
241
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 );
232
244
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
+ }
235
254
236
255
spin_unlock_irqrestore (& c -> vc .lock , flags );
237
256
@@ -339,8 +358,6 @@ static void bcm2835_dma_issue_pending(struct dma_chan *chan)
339
358
struct bcm2835_chan * c = to_bcm2835_dma_chan (chan );
340
359
unsigned long flags ;
341
360
342
- c -> cyclic = true; /* Nothing else is implemented */
343
-
344
361
spin_lock_irqsave (& c -> vc .lock , flags );
345
362
if (vchan_issue_pending (& c -> vc ) && !c -> desc )
346
363
bcm2835_dma_start_desc (c );
@@ -358,7 +375,7 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
358
375
struct bcm2835_desc * d ;
359
376
dma_addr_t dev_addr ;
360
377
unsigned int es , sync_type ;
361
- unsigned int frame ;
378
+ unsigned int frame , max_size ;
362
379
int i ;
363
380
364
381
/* Grab configuration */
@@ -393,7 +410,12 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
393
410
394
411
d -> c = c ;
395
412
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 );
397
419
398
420
d -> cb_list = kcalloc (d -> frames , sizeof (* d -> cb_list ), GFP_KERNEL );
399
421
if (!d -> cb_list ) {
@@ -441,17 +463,171 @@ static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
441
463
BCM2835_DMA_PER_MAP (c -> dreq );
442
464
443
465
/* 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 ;
445
471
d -> size += control_block -> length ;
446
472
447
473
/*
448
474
* 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 .
450
476
* Therefore, wrap around at number of frames.
451
477
*/
452
478
control_block -> next = d -> cb_list [((frame + 1 ) % d -> frames )].paddr ;
453
479
}
454
480
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
+
455
631
return vchan_tx_prep (& c -> vc , & d -> vd , flags );
456
632
error_cb :
457
633
i -- ;
@@ -620,6 +796,7 @@ static int bcm2835_dma_probe(struct platform_device *pdev)
620
796
od -> ddev .device_tx_status = bcm2835_dma_tx_status ;
621
797
od -> ddev .device_issue_pending = bcm2835_dma_issue_pending ;
622
798
od -> ddev .device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic ;
799
+ od -> ddev .device_prep_slave_sg = bcm2835_dma_prep_slave_sg ;
623
800
od -> ddev .device_config = bcm2835_dma_slave_config ;
624
801
od -> ddev .device_terminate_all = bcm2835_dma_terminate_all ;
625
802
od -> ddev .src_addr_widths = BIT (DMA_SLAVE_BUSWIDTH_4_BYTES );
@@ -708,4 +885,5 @@ module_platform_driver(bcm2835_dma_driver);
708
885
MODULE_ALIAS ("platform:bcm2835-dma" );
709
886
MODULE_DESCRIPTION ("BCM2835 DMA engine driver" );
710
887
MODULE_AUTHOR (
"Florian Meier <[email protected] >" );
888
+ MODULE_AUTHOR (
"Gellert Weisz <[email protected] >" );
711
889
MODULE_LICENSE ("GPL v2" );
0 commit comments