-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfin_lp.c
1950 lines (1651 loc) · 52 KB
/
bfin_lp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Blackfin Linkport driver
*
* Copyright 2012-2013 Analog Devices Inc.
*
* Licensed under the GPL-2 or later.
*
*
*
* Changed 2017-2020 by DEMCON.
*/
/*!
* \file
* \brief \ref linkport implementation.
* \author Jochem Rutgers ([email protected])
*/
/*!
* \defgroup linkport Link port driver
* \brief This unit implements the Linux kernel link port character device.
*
* As this port has to process much data, up to 32 MB/s input and output, performance of this driver is key.
*
* This unit is tested by bfin_lp_test.c.
*
* <b>DMA and buffers</b>
*
* DMA is used to copy data from/to the link port peripheral.
* For this, the kernel has to allocate a block of memory, which is used by the DMA.
* To do this, there are multiple options:
*
* - \c kmalloc() a buffer in DDR, and pass this to \c dma_map_single().
* This way, 'streaming DMA' can be used, which flushes the data cache before and after a read or write.
* For this, \c dma_sync_single_for_cpu() and \c dma_sync_single_for_device() are called.
* Flushing/invalidating caches takes significant amount of time at the intended throughput.
* - Call \c dma_alloc_coherent() to allocate a buffer in DDR, which is called 'coherent DMA'.
* This buffer is mapped to kernel space with caches disabled.
* Therefore, flushing/invalidating the cache is not required, which saves some processor time in the driver.
* However, accessing the data is expensive, as the L1 cache is not used.
*
* DDR is relatively expensive, as it is off-chip memory.
* The L2 SRAM can also be used (via the \c sram_alloc() API).
* In theory, both solutions from above can be combined with L2 instead of DDR.
* Instead of using \c dma_map_single() for streaming DMA, \c dma_map_resource() utilizes the L2 IO region.
* However, \c dma_map_resources() is not available in the currently used kernel version, so L2 cannot be used in combination with streaming DMA at the moment.
* To use the L2 for coherent DMA, the allocated L2 buffer can be passed to the coherent pool via \c dma_declare_coherent_memory().
* This way, uncached access to the L2 buffer is realized.
* Performance tests show that the DMA is significantly faster using L2 instead of DDR, but uncached access latency to either buffer by the application is equivalent.
*
* When #LP_DMA_SRAM is defined, coherent DMA utilizes the L2 instead of DDR.
* This macro has no effect on streaming DMA.
*
* To get the data from/to the DMA buffer, the data has to be transferred between user space and kernel space.
* For this, there are several options:
*
* - Given a user space buffer in the application, do a \c copy_from_user() (writes) or \c copy_to_user() (reads) to/from kernel space.
* There is overhead in copying the data, but the kernel has highly optimized methods for this.
* - \c mmap() the DMA buffer from kernel space to user space, such that the application can access the buffer directly.
* In case of coherent DMA, the application accesses the buffer uncached, which may result in a significant performance hit when the application does this inefficiently.
*
* All permutations of the partial solutions above are valid, but with different performance.
* The following approach seems to be the best.
*
* For writes, use an uncached L2 buffer \c mmap()ed to user space, in combination with coherent DMA.
* This way, the DMA is fast, as it runs from L2; writes are uncached, so no cache flushes are required; no buffers are copied as \c mmap() is used.
* Therefore, the driver defines #LP_DMA_COHERENT_WRITE.
*
* For reads, use a cached DDR buffer \c mmap()ed to user space, in combination with streaming DMA.
* Reads must process/decode all data, so access from the application is important.
* Uncached access to the buffer is too expensive.
* This gives two solutions: uncached buffer (coherent DMA) and use \c copy_to_user(), or cached buffer (streaming DMA) with \c mmap() and flush the cache.
* In both cases, the kernel has to iterate over the buffer, either for flush or copy, where the latter is assumed to be more efficient.
* However, L2 cannot be used, due to the lack of \c dma_map_resource().
* Therefore, the driver does not define \c LP_DMA_COHERENT_READ.
*
* <b>Double buffering</b>
*
* In case \c mmap() is used to access the DMA buffer, double buffering can be used.
* Via a \c ioctl() call, both buffers (ping and pong) can be mapped to user space (see #bfin_lp_ioctl()).
* When opened in non-blocking mode, reads and writes can be initiated on one buffer, while the other can be processed/prepared for the next read/write.
* See #bfin_lp_read() and #bfin_lp_write() for examples.
*
* <b>Interrupts</b>
*
* A read works conceptually as follows:
*
* -# Configure and start the DMA given the used buffer and buffer length.
* -# Suspend process while waiting for the DMA to complete.
* -# When the DMA completes, an interrupt is generated, which wakes the process.
* -# Copy kernel space buffer to user space, when the supplied buffer was not \c mmap()ed.
*
* A write works conceptually as follows:
*
* -# When a previous write was not finished, enable interrupts, suspend the process, and wait for the DMA to finish and trigger an interrupt.
* -# Copy user space buffer to kernel space, when the supplied buffer was not \c mmap()ed.
* -# Disable DMA interrupt.
* -# Configure and start the DMA given the used buffer and buffer length.
*
* When writes are sufficiently spaced in time, the DMA completes before the next write is initiated.
* Then, it is checked whether the previous write was finished, without the overhead of interrupts, and sleeping and resuming the process.
*
* For non-blocking reads/writes, use \c poll() or \c select(), as exemplified by #bfin_lp_read() and #bfin_lp_write().
*
* \ingroup app
*/
//#define DEBUG
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/spinlock.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/dma-mapping.h>
#include <linux/uaccess.h>
#include <mach/dma.h>
#include <mach/sc58x.h>
#include <mach/cpu.h>
#include <mach/irqs.h>
#include <asm/cacheflush.h>
#include <mach/sram.h>
#include <mach/hardware.h>
//////////////////////////////////////////////////////
// Configuration
//////////////////////////////////////////////////////
/*!
* \brief When defined, use L2 SRAM for the DMA memory.
* \ingroup linkport
*/
#define LP_DMA_SRAM
/*
* \brief When defined, use coherent (uncached) dma for reads (either DDR or L2).
* Otherwise, use streaming (cached) dma (DDR, never L2).
* \ingroup linkport
*/
//#define LP_DMA_COHERENT_READ
/*!
* \brief When defined, use coherent (uncached) dma for writes (either DDR or L2).
* Otherwise, use streaming (cached) dma (DDR, never L2).
* \ingroup linkport
*/
#define LP_DMA_COHERENT_WRITE
/*!
* \brief Size of one ping (or pong) buffer.
* \ingroup linkport
*/
#define LP_DMA_BUFSIZE_PINGPONG_ 0x2000
/*!
* \brief Default clock divider.
* \ingroup linkport
*/
#define LP_DIV 0
//////////////////////////////////////////////////////
// Configuration
//////////////////////////////////////////////////////
#define LINKPORT_DRVNAME "linkport-dma"
// Register definitions
#define LP_REG_SIZE 0x20
#define LP_CTL_EN 0x1
#define LP_CTL_TRAN 0x8
#define LP_CTL_TRQMSK 0x100
#define LP_CTL_RRQMSK 0x200
#define LP_CTL_ITMSK 0x800
#define LP_STAT_FREE 0x0
#define LP_STAT_INUSE 0x8000
#define LP_STAT_DMA 0x4000
#define LP_STAT_WNR 0x2000
#define LP_STAT_DONE 0x1000
#define LP_STAT_LTRQ 0x1
#define LP_STAT_LRRQ 0x2
#define LP_STAT_LPIT 0x8
#define LP_STAT_FFST 0x70
#define LP_STAT_LERR 0x80
#define LP_STAT_LPBS 0x100
#define LP_CTL_OFF 0x0
#define LP_STAT_OFF 0x4
#define LP_DIV_OFF 0x8
#define LP_CNT_OFF 0xC
#define LP_TX_OFF 0x10
#define LP_RX_OFF 0x14
#define LP_TX_SHADOW_OFF 0x18
#define LP_RX_SHADOW_OFF 0x1C
#define DMA_CTL_PDRF 0x10000000
// Number of bytes to pad at the end of a write (rounded down to full words)
#define LP_TX_PADDING 3
/*!
* \brief \c ioctl() magic number.
* \ingroup linkport
*/
#define LP_IOCTL_MAGIC 15
/*!
* \brief Set clock divider via \c ioctl().
* \ingroup linkport
*/
#define LP_IOCTL_CLKDIV _IOW(LP_IOCTL_MAGIC, 1, uint8_t)
/*!
* \brief \c mmap() ping buffer and return a user space address via \c ioctl().
* \ingroup linkport
*/
#define LP_IOCTL_BUFADDR_PING _IOR(LP_IOCTL_MAGIC, 2, void*)
/*!
* \brief \c mmap() pong buffer and return a user space address via \c ioctl().
* \ingroup linkport
*/
#define LP_IOCTL_BUFADDR_PONG _IOR(LP_IOCTL_MAGIC, 3, void*)
/*!
* \brief Return the size of the ping and pong buffer \c ioctl().
* \ingroup linkport
*/
#define LP_IOCTL_BUFSIZE _IO(LP_IOCTL_MAGIC, 4)
#define LP_DMA_BUFSIZE_PINGPONG ((LP_DMA_BUFSIZE_PINGPONG_ + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
#define LP_DMA_BUFSIZE (LP_DMA_BUFSIZE_PINGPONG * 2)
#define LP_DMA_PING_ADDR(dev) ((dev)->dma_handle)
#define LP_DMA_PONG_ADDR(dev) ((dev)->dma_handle + LP_DMA_BUFSIZE_PINGPONG)
#if defined(LP_DMA_COHERENT_WRITE) && !defined(LP_DMA_COHERENT_READ)
# define IS_DMA_COHERENT(filp) ((filp)->f_mode & FMODE_WRITE)
#elif !defined(LP_DMA_COHERENT_WRITE) && defined(LP_DMA_COHERENT_READ)
# define IS_DMA_COHERENT(filp) (!((filp)->f_mode & FMODE_WRITE))
#elif defined(LP_DMA_COHERENT_WRITE) && defined(LP_DMA_COHERENT_READ)
# define IS_DMA_COHERENT(filp) true
#else
# define IS_DMA_COHERENT(filp) false
#endif
//////////////////////////////////////////////////////
// Data structures
//////////////////////////////////////////////////////
/*!
* \brief Link port /dev file description.
* \ingroup linkport
*/
struct bfin_linkport {
struct list_head lp_dev; //!< \brief The corresponding link port device list.
struct class *class; //!< \brief The class of the link port character device.
int major; //!< \brief The major number.
};
/*!
* \brief Link port device description.
* \ingroup linkport
*/
struct bfin_lp_dev {
struct list_head list; //!< \brief Device list administration.
struct device *device; //!< \brief The corresponding Linux device.
phys_addr_t const preg_base;//!< \brief The physical address of the link port peripheral registers.
int irq; //!< \brief the IRQ for DMA.
bool volatile irq_disabled; //!< \brief Flag to indicate that the DMA interrupt is disabled.
int status_irq; //!< \brief The IRQ for transmit/receive requests (not used).
int const dma_chan; //!< \brief The DMA channel.
int linkport_num; //!< \brief Index of this link port (corresponds to /dev/linkport*).
void __iomem *reg_base; //!< \brief \c ioremap() of #preg_base.
spinlock_t lock; //!< \brief Lock for opening the device and checking its administration.
struct mutex mutex; //!< \brief Mutex to protect concurrent reads/writes to the same fd.
struct completion complete; //!< \brief Signal to indicate that the DMA has finished.
struct pinctrl* pinctrl; //!< \brief Administration of the link port pins.
uint8_t clk_div; //!< \brief Request clock divider (see LP_DIV register).
int status; //!< \brief Status of this device.
bool nosleep; //!< \brief Indicates whether the process had to sleep while waiting for the DMA.
dma_addr_t dma_handle; //!< \brief Physical address of #dma_buffer.
dma_addr_t dma_start_addr; //!< \brief Currently used start address of the DMA transfer.
void* dma_buffer; //!< \brief Virtual address of #dma_handle.
#ifdef LP_DMA_SRAM
void* sram_buffer; //!< \brief Allocated L2 memory, which is used as pool for coherent DMA.
#endif
size_t dma_count; //!< \brief Length of currently running DMA transfer.
unsigned long ping_mmap; //!< \brief User space address of ping buffer within #dma_buffer.
unsigned long pong_mmap; //!< \brief User space address of pong buffer within #dma_buffer.
};
/*!
* \brief List of all link port devices.
*/
static struct bfin_linkport *linkport_dev;
/*!
* \brief List of link port peripherals.
*/
static struct bfin_lp_dev lp_dev_info[2] = {
{
.preg_base = LP0_CTL,
.irq = IRQ_LP0,
.status_irq = IRQ_LP0_STAT,
.dma_chan = CH_LP0,
},
{
.preg_base = LP1_CTL,
.irq = IRQ_LP1,
.status_irq = IRQ_LP1_STAT,
.dma_chan = CH_LP1,
},
};
/*! \brief Number of link ports. */
#define LP_NUM (sizeof(lp_dev_info) / sizeof(lp_dev_info[0]))
//////////////////////////////////////////////////////
// Misc
//////////////////////////////////////////////////////
/*!
* \brief Returns the current direction of the opened device, for streaming DMA functions.
* \ingroup linkport
*/
static enum dma_data_direction bfin_lp_dma_direction(struct bfin_lp_dev* dev)
{
return dev && (dev->status & LP_STAT_WNR) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
}
/*!
* \brief Configure the link port peripheral.
* \ingroup linkport
*/
static void bfin_lp_config_channel(struct bfin_lp_dev *dev)
{
uint32_t reg;
uint32_t cur;
cur = readl(dev->reg_base + LP_CTL_OFF);
if(dev->status & LP_STAT_WNR)
{
// read/receive
reg = 0; //| LP_CTL_RRQMSK;
if(!(cur & LP_CTL_TRAN) && (cur & LP_CTL_EN))
// keep port enabled when already in receive mode
reg |= LP_CTL_EN;
}
else
{
// write/transmit
reg = LP_CTL_TRAN; //| LP_CTL_TRQMSK;
if((cur & LP_CTL_TRAN) && (cur & LP_CTL_EN))
// keep port enabled when already in transmit mode
reg |= LP_CTL_EN;
writel(dev->clk_div, dev->reg_base + LP_DIV_OFF);
}
writel(reg, dev->reg_base + LP_CTL_OFF);
}
/*!
* \brief Enable a configured link port peripheral.
* \details Call #bfin_lp_config_channel() first.
* \ingroup linkport
*/
static void bfin_lp_enable(struct bfin_lp_dev *lpdev)
{
uint32_t ctl;
ctl = readl(lpdev->reg_base + LP_CTL_OFF);
if(!(ctl & LP_CTL_EN))
writel(ctl | LP_CTL_EN, lpdev->reg_base + LP_CTL_OFF);
}
/*!
* \brief Disable a configured link port peripheral.
* \ingroup linkport
*/
static void bfin_lp_disable(struct bfin_lp_dev *lpdev)
{
uint32_t ctl;
ctl = readl(lpdev->reg_base + LP_CTL_OFF);
if(ctl & LP_CTL_EN)
writel(ctl & ~LP_CTL_EN, lpdev->reg_base + LP_CTL_OFF);
}
/*!
* \brief Resets a link port peripheral.
* \ingroup linkport
*/
static int bfin_lp_reset(struct bfin_lp_dev *dev)
{
writel(0, dev->reg_base + LP_CTL_OFF);
writel(dev->clk_div, dev->reg_base + LP_DIV_OFF);
// clear all (pending) interrupts
writel(0xFF, dev->reg_base + LP_STAT_OFF);
writel(0, dev->reg_base + LP_CTL_OFF);
return 0;
}
#ifdef LP_DMA_SRAM
/*!
* \brief Returns the physical address of an allocated L2 buffer address.
* \ingroup linkport
*/
static inline dma_addr_t sram_dma_addr(void* sram_virt_addr)
{
uintptr_t mmio_addr = (uintptr_t)sram_virt_addr;
uintptr_t offs = mmio_addr - (uintptr_t)IO_ADDRESS(SYS_SRAM_BASE);
return (dma_addr_t)(SYS_SRAM_BASE + offs);
}
#endif
//////////////////////////////////////////////////////
// IRQ handlers
//////////////////////////////////////////////////////
/*!
* \brief IRQ handler for the status IRQ.
* \ingroup linkport
*/
static irqreturn_t bfin_lp_irq(int irq, void *dev_id)
{
struct bfin_lp_dev *dev = (struct bfin_lp_dev *)dev_id;
uint32_t stat;
if(unlikely(!dev))
// can't do anything...
return IRQ_HANDLED;
stat = readl(dev->reg_base + LP_STAT_OFF);
dev_dbg(dev->device, "lp irq %d stat %x dev %p status 0x%x\n", irq, stat, dev, dev->status);
writel(stat, dev->reg_base + LP_STAT_OFF);
return IRQ_HANDLED;
}
/*!
* \brief Checks whether the DMA completed (whether it is currently in idle/stop state).
* \details This function may be called from the IRQ context and from the normal context.
* \return 0 when finished and \c -EINPROGRESS when it is still running
* \ingroup linkport
*/
static int bfin_dma_check(struct bfin_lp_dev* dev)
{
unsigned long dma_stat;
if(unlikely(!(dev->status & LP_STAT_DMA)))
return -EIO;
dma_stat = get_dma_curr_irqstat(dev->dma_chan);
dev_dbg(dev->device, "dmastat 0x%lx\n", dma_stat);
if(unlikely(!(dma_stat & DMA_DONE) && (dma_stat & DMA_RUN_MASK)))
return -EINPROGRESS;
// For transmit, DMA_DONE means that the DMA finished reading memory,
// but may not be finished writing to the peripheral.
// So, the run status may not be idle/stop yet.
if(__atomic_fetch_and(&dev->status, ~LP_STAT_DMA, __ATOMIC_RELAXED) & LP_STAT_DMA)
{
// dma finished
complete(&dev->complete);
}
if(unlikely(dma_stat & DMA_ERR))
dev_warn(dev->device, "dma error (stat: 0x%lx); ignored\n", dma_stat);
return 0;
}
/*!
* \brief IRQ handler when the DMA finishes.
* \ingroup linkport
*/
static irqreturn_t bfin_dma_irq(int irq, void *dev_id)
{
struct bfin_lp_dev *dev = dev_id;
BUG_ON(!dev || irq != dev->irq);
dev_dbg(dev->device, "dma irq %d dev %p status 0x%x\n", irq, dev, dev->status);
switch(bfin_dma_check(dev))
{
case -EIO:
// not sure where this irq came from
disable_irq_nosync(irq);
dev->irq_disabled = true;
break;
case 0:
// done
break;
default:;
// still waiting to complete...
}
clear_dma_irqstat(dev->dma_chan);
return IRQ_HANDLED;
}
//////////////////////////////////////////////////////
// read/write/ioctl
//////////////////////////////////////////////////////
/*!
* \brief Waits until a previous read/write finishes.
* \details The caller must hold the <code>dev->mutex</code>.
* \return 0 when a new read/write can be started, -EWOULDBLOCK when the DMA is currently busy and the file is opened as non-blocking
* \ingroup linkport
*/
static int bfin_lp_complete(struct bfin_lp_dev* dev, bool blocking)
{
unsigned long irq_flags;
if(unlikely(!dev))
return -EINVAL;
if(unlikely(!(dev->status & (LP_STAT_LTRQ | LP_STAT_LRRQ))))
// no outstanding reads/writes
return 0;
if(unlikely(dev->irq_disabled && bfin_dma_check(dev) == -EINPROGRESS && blocking))
{
// Someone is waiting for the DMA to finish, but IRQ was disabled.
// Enable so we can properly sleep.
dev->irq_disabled = false;
enable_irq(dev->irq);
// Flag that we have to sleep before the operation finishes.
dev->nosleep = false;
}
// In case res == -EINPROGRESS, waiting for completion will sleep until the irq triggers.
// In case res == 0, waiting for completion returns immediately and successfully.
// transmit is/was in progress, wait for completion
if(blocking)
{
if(try_wait_for_completion(&dev->complete)) {
// finished immediately
// nosleep is true when interrupts where enabled and the DMA already finished,
// or nosleep is false in the race condition that it just finished between enabling the irq and now.
} else if(wait_for_completion_interruptible(&dev->complete)) {
// interrupted
return -ERESTARTSYS;
} else {
// we did sleep for a while
dev->nosleep = false;
}
}
else
{
if((dev->status & LP_STAT_LTRQ) && (get_dma_curr_irqstat(dev->dma_chan) & DMA_RUN_MASK))
// The done IRQ is triggered when the DMA finished reading memory, but may still
// be busy transferring data to the peripheral.
return -EWOULDBLOCK;
if(!try_wait_for_completion(&dev->complete))
// not completed yet
return -EWOULDBLOCK;
}
// When we get here, a read/write was finished.
//
// We assume that reads/writes are called in a repetitive process.
// When writes are sufficiently spaced in time, the DMA interrupt can be suppressed.
// We have to detect whether this is the case.
// When nosleep is true, it indicates that the DMA was finished before the call to bfin_lp_complete() and generated an interrupt.
// However, when interrupts would have been disabled, the call to bfin_dma_check() would have noticed that DMA was finished,
// without the need to generate and handle the interrupt.
// So, in case of write() and nosleep, the DMA interrupt can be disabled, and it is assumed that it does not have to be reenabled
// over and over again for every write.
barrier();
if(dev->status & LP_STAT_LRRQ)
{
#ifndef LP_DMA_COHERENT_READ
dma_sync_single_for_cpu(dev->device, dev->dma_start_addr, dev->dma_count, DMA_FROM_DEVICE);
#endif
dev_dbg(dev->device, "finished read\n");
}
else if(dev->status & LP_STAT_LTRQ)
{
unsigned long u = 1;
#ifndef LP_DMA_COHERENT_WRITE
dma_sync_single_for_cpu(dev->device, dev->dma_start_addr, dev->dma_count, DMA_TO_DEVICE);
#endif
if(unlikely(dev->nosleep && !dev->irq_disabled))
{
// Disable DMA interrupt, as it does not seem to be required, as writes are not been done too quickly after each other.
local_irq_save(irq_flags);
if(!dev->irq_disabled)
{
disable_irq_nosync(dev->irq);
dev->irq_disabled = true;
}
local_irq_restore(irq_flags);
dev_dbg(dev->device, "nosleep; disable irq\n");
}
while(unlikely(get_dma_curr_irqstat(dev->dma_chan) & DMA_RUN_MASK))
{
// Wait until it is really stopped/idle, otherwise we cannot restart it.
if(u <= 8)
{
// busy-wait, up to 15 us
udelay(u);
}
else
{
// allow context switch
usleep_range(u, u * 4);
if(signal_pending(current))
return -ERESTARTSYS;
}
if(u < 1024)
// exponential back-off up to 1 ms
u *= 2;
}
dev_dbg(dev->device, "finished write\n");
}
dev->status &= ~(LP_STAT_LTRQ | LP_STAT_LRRQ);
return 0;
}
/*!
* \brief \c fsync() implementation.
* \details This is essentially a wrapper for #bfin_lp_complete() that always blocks, regardless whether the file was opened in non-blocking mode.
* \ingroup linkport
*/
static int bfin_lp_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
{
struct bfin_lp_dev *dev;
int ret = 0;
if(unlikely(!filp))
return -EINVAL;
dev = filp->private_data;
if(unlikely(!dev))
return -EINVAL;
if(mutex_lock_interruptible(&dev->mutex))
return -ERESTARTSYS;
ret = bfin_lp_complete(dev, true);
mutex_unlock(&dev->mutex);
return ret;
}
/*!
* \brief Implementation of \c read().
*
* Simple example:
*
* \code
* int fd = open("/dev/linkport0", O_RDONLY);
* char buf[16];
* int count = read(fd, buf, sizeof(buf));
* // use buf...
* \endcode
*
*
* Non blocking example.
* Note that the DMA must be initialized (so at least one \c read() must be made) before sleeping by \c poll() and friends.
* Moreover, the supplied buffer size is the amount of data to wait for; read will not complete until all data was received.
* This is not conforming to the normal non-blocking interface, but required for this specific driver.
* The successive read returns the result of the previous DMA transfer, so one must supply the same buffer and the same buffer length to the second read as was supplied to the first one.
*
* \code
* int fd = open("/dev/linkport0", O_RDONLY | O_NONBLOCK);
* char buf[16];
* int count = read(fd, buf, sizeof(buf)); // initiate DMA for given amount of data
*
* if(count < 0 && errno == EINPROGRESS)
* {
* // a read must have been initiated before poll will mark the fd as readible
* poll(read fd...); // may use select/fsync too
* count = read(fd, buf, sizeof(buf));
* // use buf...
* }
* \endcode
*
*
* DMA double buffer with \c mmap() (implicitly by \c ioctl()) example.
*
* \code
* int fd = open("/dev/linkport0", O_RDONLY | O_NONBLOCK);
* void* ping;
* ioctl(fd, LP_IOCTL_BUFADDR_PING, &ping);
* // ioctl() also mmap()s the buffer to user space.
* void* pong;
* ioctl(fd, LP_IOCTL_BUFADDR_PONG, &pong);
* size_t bufsize = (size_t)ioctl(fd, LP_IOCTL_BUFSIZE);
*
* while(true)
* {
* // start DMA for ping
* read(fd, ping, expected_data_size); // returns EINPROGRESS
*
* // do something with pongbuf while ping is transferred
*
* poll(read fd...) // wait for DMA to finish ping; may use select/fsync too
* read(fd, ping, expected_data_size); // actual read
*
* // start DMA for pong
* read(fd, pong, expected_data_size); // returns EINPROGRESS
*
* // do something with pingbuf while pong is transferred
*
* poll(read fd...) // wait for DMA to finish pong; may use select/fsync too
* read(fd, pong, expected_data_size); // actual read
* }
* \endcode
*
*
* If neither the \c mmap()ed ping and pong buffer is passed to \c read(), internally the ping buffer is
* used to DMA the data into, before coping the data to userspace.
* Be careful when mixing \c mmap()ed and normal reads.
*
* \ingroup linkport
*/
static ssize_t bfin_lp_read(struct file *filp, char *buf, size_t count, loff_t *pos)
{
struct bfin_lp_dev *dev;
unsigned long irq_flags;
int ret;
if(unlikely(count == 0))
return 0;
if(unlikely(count <= 3))
return -EINVAL;
if(unlikely(!filp))
return -EINVAL;
if(unlikely(filp->f_mode & FMODE_WRITE))
return -EPERM;
dev = filp->private_data;
if(unlikely(!dev || !dev->dma_buffer))
return -EINVAL;
if(!buf)
buf = (char*)dev->ping_mmap;
count = (count > LP_DMA_BUFSIZE_PINGPONG ? LP_DMA_BUFSIZE_PINGPONG : count) & ~3;
if(filp->f_flags & O_NONBLOCK)
{
if (!mutex_trylock(&dev->mutex))
return -EWOULDBLOCK;
}
else
{
if (mutex_lock_interruptible(&dev->mutex))
return -ERESTARTSYS;
}
if(!(dev->status & LP_STAT_LRRQ))
{
// this is a new read operation
dev_dbg(dev->device, "read 0x%x bytes\n", count);
if(buf == (char*)dev->ping_mmap)
{
dev->dma_start_addr = LP_DMA_PING_ADDR(dev);
#ifdef DEBUG
memset(dev->dma_buffer, 0xcd, LP_DMA_BUFSIZE_PINGPONG);
#endif
}
else if(buf == (char*)dev->pong_mmap)
{
dev->dma_start_addr = LP_DMA_PONG_ADDR(dev);
#ifdef DEBUG
memset((void*)((uintptr_t)dev->dma_buffer + LP_DMA_BUFSIZE_PINGPONG), 0xcd, LP_DMA_BUFSIZE_PINGPONG);
#endif
}
else
{
dev->dma_start_addr = dev->dma_handle;
#ifdef DEBUG
memset(dev->dma_buffer, 0xcd, LP_DMA_BUFSIZE_PINGPONG);
#endif
}
dev->dma_count = count;
#ifndef LP_DMA_COHERENT_READ
dma_sync_single_for_device(dev->device, dev->dma_start_addr, dev->dma_count, DMA_FROM_DEVICE);
#endif
// Configure dma
set_dma_start_addr(dev->dma_chan, dev->dma_start_addr);
set_dma_x_count(dev->dma_chan, dev->dma_count / 4);
// start dma
local_irq_save(irq_flags);
dev->status |= LP_STAT_LRRQ | LP_STAT_DMA;
mb();
enable_dma(dev->dma_chan);
if(dev->irq_disabled)
{
dev->irq_disabled = false;
enable_irq(dev->irq);
}
local_irq_restore(irq_flags);
// When the DMA completes, bfin_dma_irq() sets dev->complete.
// In case the file is opened non-blocking, it is very unlikely
// that the read finishes immediately. So, we guarantee that
// the read that initiated a DMA transfer always returns
// -EINPROGRESS.
if(filp->f_flags & O_NONBLOCK)
{
count = -EINPROGRESS;
goto done;
}
}
else
{
dev_dbg(dev->device, "read already in progress\n");
if(count < dev->dma_count)
{
// buf too small
count = -EINVAL;
goto done;
}
if(count > dev->dma_count)
// do not read more than the dma was configured for
count = dev->dma_count;
}
// try to complete this read right now
if((ret = bfin_lp_complete(dev, !(filp->f_flags & O_NONBLOCK))))
{
// not possible right now
dev_dbg(dev->device, "read would block");
count = (ret == -EWOULDBLOCK ? -EINPROGRESS : ret);
goto done;
}
// dma completed
if(buf != (char*)dev->ping_mmap && buf != (char*)dev->pong_mmap)
{
if(unlikely(copy_to_user(buf, dev->dma_buffer, count)))
{
// data has been lost
count = -EFAULT;
goto done;
}
}
done:
mutex_unlock(&dev->mutex);
return count;
}
/*!
* \brief Implementation of \c write().
*
* Simple example.
* Even though the port is opened in blocking mode, \c write() returns before the data was actually transmitted.
* A successive write will block until the previous write finishes.
* Since the buffer was duplicated by the driver, \c buf can be reused safely meanwhile.
*
* \code
* int fd = open("/dev/linkport0", O_WRONLY);
* char buf[16];
* // fill buf
* write(fd, buf, sizeof(buf));
* \endcode
*
*
* Non blocking example:
*
* \code
* int fd = open("/dev/linkport0", O_WRONLY | O_NONBLOCK);
* char buf[16];
* // fill buf
* poll(write fd...); // in contrast to read+poll, no write is required before; may use select/fsync too
* write(fd, buf, sizeof(buf)); // may still return EWOULDBLOCK in some cases, see bfin_lp_poll()
* \endcode
*
*
* DMA double buffer example.
* In order to be able to \c mmap() the DMA buffer to user space, the file must be opened with read mode.
* Hence, \c O_RDWR is interpreted as a write-only, but allows \c mmap().
* Do not use O_RDWR when the file is intended to be \c read().
*
* \code
* int fd = open("/dev/linkport0", O_RDWR | O_NONBLOCK);
* void* ping;
* ioctl(fd, LP_IOCTL_BUFADDR_PING, &ping);
* void* pong;
* ioctl(fd, LP_IOCTL_BUFADDR_PONG, &pong);
* size_t bufsize = (size_t)ioctl(fd, LP_IOCTL_BUFSIZE);
*
* while(true)
* {
* // fill pingbuf, up to bufsize bytes, while pong is being transferred
*
* fsync(fd); // wait for previous DMA to complete; may use select/poll too
* write(fd, ping, length); // start DMA
*
* // fill pongbuf, up to bufsize bytes, while ping is being transferred
*
* fsync(fd); // wait for previous DMA to complete; may use select/poll too
* write(fd, pong, length); // start DMA
* }
* \endcode
*
*
* If neither the \c mmap()ed ping and pong buffer is passed to \c write(), internally the ping buffer is
* used to copy the data into from userspace.
* Be careful when mixing \c mmap()ed and normal writes.
*
* \ingroup linkport
*/
static ssize_t bfin_lp_write(struct file *filp, const char *buf, size_t count, loff_t *pos)
{
struct bfin_lp_dev *dev;
unsigned long irq_flags;
int ret;
size_t padding;
if(unlikely(count == 0))
return 0;
if(unlikely(!filp))
return -EINVAL;
if(unlikely(!(filp->f_mode & FMODE_WRITE)))
return -EPERM;
dev = filp->private_data;
if(unlikely(!dev || !dev->dma_buffer))
return -EINVAL;
padding = ((count + LP_TX_PADDING) & ~3) - count;
if(count + padding > LP_DMA_BUFSIZE_PINGPONG)
{
padding = LP_TX_PADDING & ~3;
count = LP_DMA_BUFSIZE_PINGPONG - padding;
}
if(filp->f_flags & O_NONBLOCK)
{
if (!mutex_trylock(&dev->mutex))
return -EWOULDBLOCK;
}
else
{
if (mutex_lock_interruptible(&dev->mutex))
return -ERESTARTSYS;
}