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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
// SPDX-License-Identifier: GPL-2.0
/***************************************************************************
* GPIB Driver for Fluke cda devices. Basically, its a driver for a (bugfixed)
* cb7210 connected to channel 0 of a pl330 dma controller.
* Author: Frank Mori Hess <fmh6jj@gmail.com>
* copyright: (C) 2006, 2010, 2015 Fluke Corporation
***************************************************************************/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#define dev_fmt pr_fmt
#define DRV_NAME KBUILD_MODNAME
#include "fluke_gpib.h"
#include "gpibP.h"
#include <linux/dma-mapping.h>
#include <linux/ioport.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("GPIB Driver for Fluke cda devices");
static int fluke_attach_holdoff_all(struct gpib_board *board, const gpib_board_config_t *config);
static int fluke_attach_holdoff_end(struct gpib_board *board, const gpib_board_config_t *config);
static void fluke_detach(struct gpib_board *board);
static int fluke_config_dma(struct gpib_board *board, int output);
static irqreturn_t fluke_gpib_internal_interrupt(struct gpib_board *board);
static struct platform_device *fluke_gpib_pdev;
static uint8_t fluke_locking_read_byte(struct nec7210_priv *nec_priv, unsigned int register_number)
{
u8 retval;
unsigned long flags;
spin_lock_irqsave(&nec_priv->register_page_lock, flags);
retval = fluke_read_byte_nolock(nec_priv, register_number);
spin_unlock_irqrestore(&nec_priv->register_page_lock, flags);
return retval;
}
static void fluke_locking_write_byte(struct nec7210_priv *nec_priv, uint8_t byte,
unsigned int register_number)
{
unsigned long flags;
spin_lock_irqsave(&nec_priv->register_page_lock, flags);
fluke_write_byte_nolock(nec_priv, byte, register_number);
spin_unlock_irqrestore(&nec_priv->register_page_lock, flags);
}
// wrappers for interface functions
static int fluke_read(struct gpib_board *board, uint8_t *buffer, size_t length, int *end,
size_t *bytes_read)
{
struct fluke_priv *priv = board->private_data;
return nec7210_read(board, &priv->nec7210_priv, buffer, length, end, bytes_read);
}
static int fluke_write(struct gpib_board *board, uint8_t *buffer, size_t length,
int send_eoi, size_t *bytes_written)
{
struct fluke_priv *priv = board->private_data;
return nec7210_write(board, &priv->nec7210_priv, buffer, length, send_eoi, bytes_written);
}
static int fluke_command(struct gpib_board *board, uint8_t *buffer,
size_t length, size_t *bytes_written)
{
struct fluke_priv *priv = board->private_data;
return nec7210_command(board, &priv->nec7210_priv, buffer, length, bytes_written);
}
static int fluke_take_control(struct gpib_board *board, int synchronous)
{
struct fluke_priv *priv = board->private_data;
return nec7210_take_control(board, &priv->nec7210_priv, synchronous);
}
static int fluke_go_to_standby(struct gpib_board *board)
{
struct fluke_priv *priv = board->private_data;
return nec7210_go_to_standby(board, &priv->nec7210_priv);
}
static void fluke_request_system_control(struct gpib_board *board, int request_control)
{
struct fluke_priv *priv = board->private_data;
struct nec7210_priv *nec_priv = &priv->nec7210_priv;
nec7210_request_system_control(board, nec_priv, request_control);
}
static void fluke_interface_clear(struct gpib_board *board, int assert)
{
struct fluke_priv *priv = board->private_data;
nec7210_interface_clear(board, &priv->nec7210_priv, assert);
}
static void fluke_remote_enable(struct gpib_board *board, int enable)
{
struct fluke_priv *priv = board->private_data;
nec7210_remote_enable(board, &priv->nec7210_priv, enable);
}
static int fluke_enable_eos(struct gpib_board *board, uint8_t eos_byte, int compare_8_bits)
{
struct fluke_priv *priv = board->private_data;
return nec7210_enable_eos(board, &priv->nec7210_priv, eos_byte, compare_8_bits);
}
static void fluke_disable_eos(struct gpib_board *board)
{
struct fluke_priv *priv = board->private_data;
nec7210_disable_eos(board, &priv->nec7210_priv);
}
static unsigned int fluke_update_status(struct gpib_board *board, unsigned int clear_mask)
{
struct fluke_priv *priv = board->private_data;
return nec7210_update_status(board, &priv->nec7210_priv, clear_mask);
}
static int fluke_primary_address(struct gpib_board *board, unsigned int address)
{
struct fluke_priv *priv = board->private_data;
return nec7210_primary_address(board, &priv->nec7210_priv, address);
}
static int fluke_secondary_address(struct gpib_board *board, unsigned int address, int enable)
{
struct fluke_priv *priv = board->private_data;
return nec7210_secondary_address(board, &priv->nec7210_priv, address, enable);
}
static int fluke_parallel_poll(struct gpib_board *board, uint8_t *result)
{
struct fluke_priv *priv = board->private_data;
return nec7210_parallel_poll(board, &priv->nec7210_priv, result);
}
static void fluke_parallel_poll_configure(struct gpib_board *board, uint8_t configuration)
{
struct fluke_priv *priv = board->private_data;
nec7210_parallel_poll_configure(board, &priv->nec7210_priv, configuration);
}
static void fluke_parallel_poll_response(struct gpib_board *board, int ist)
{
struct fluke_priv *priv = board->private_data;
nec7210_parallel_poll_response(board, &priv->nec7210_priv, ist);
}
static void fluke_serial_poll_response(struct gpib_board *board, uint8_t status)
{
struct fluke_priv *priv = board->private_data;
nec7210_serial_poll_response(board, &priv->nec7210_priv, status);
}
static uint8_t fluke_serial_poll_status(struct gpib_board *board)
{
struct fluke_priv *priv = board->private_data;
return nec7210_serial_poll_status(board, &priv->nec7210_priv);
}
static void fluke_return_to_local(struct gpib_board *board)
{
struct fluke_priv *priv = board->private_data;
struct nec7210_priv *nec_priv = &priv->nec7210_priv;
write_byte(nec_priv, AUX_RTL2, AUXMR);
udelay(1);
write_byte(nec_priv, AUX_RTL, AUXMR);
}
static int fluke_line_status(const struct gpib_board *board)
{
int status = VALID_ALL;
int bsr_bits;
struct fluke_priv *e_priv;
e_priv = board->private_data;
bsr_bits = fluke_paged_read_byte(e_priv, BUS_STATUS, BUS_STATUS_PAGE);
if ((bsr_bits & BSR_REN_BIT) == 0)
status |= BUS_REN;
if ((bsr_bits & BSR_IFC_BIT) == 0)
status |= BUS_IFC;
if ((bsr_bits & BSR_SRQ_BIT) == 0)
status |= BUS_SRQ;
if ((bsr_bits & BSR_EOI_BIT) == 0)
status |= BUS_EOI;
if ((bsr_bits & BSR_NRFD_BIT) == 0)
status |= BUS_NRFD;
if ((bsr_bits & BSR_NDAC_BIT) == 0)
status |= BUS_NDAC;
if ((bsr_bits & BSR_DAV_BIT) == 0)
status |= BUS_DAV;
if ((bsr_bits & BSR_ATN_BIT) == 0)
status |= BUS_ATN;
return status;
}
static int fluke_t1_delay(struct gpib_board *board, unsigned int nano_sec)
{
struct fluke_priv *e_priv = board->private_data;
struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
unsigned int retval;
retval = nec7210_t1_delay(board, nec_priv, nano_sec);
if (nano_sec <= 350) {
write_byte(nec_priv, AUX_HI_SPEED, AUXMR);
retval = 350;
} else {
write_byte(nec_priv, AUX_LO_SPEED, AUXMR);
}
return retval;
}
static int lacs_or_read_ready(struct gpib_board *board)
{
const struct fluke_priv *e_priv = board->private_data;
const struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
unsigned long flags;
int retval;
spin_lock_irqsave(&board->spinlock, flags);
retval = test_bit(LACS_NUM, &board->status) || test_bit(READ_READY_BN, &nec_priv->state);
spin_unlock_irqrestore(&board->spinlock, flags);
return retval;
}
/* Wait until it is possible for a read to do something useful. This
* is not essential, it only exists to prevent RFD holdoff from being released pointlessly.
*/
static int wait_for_read(struct gpib_board *board)
{
struct fluke_priv *e_priv = board->private_data;
struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
int retval = 0;
if (wait_event_interruptible(board->wait,
lacs_or_read_ready(board) ||
test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
test_bit(TIMO_NUM, &board->status)))
retval = -ERESTARTSYS;
if (test_bit(TIMO_NUM, &board->status))
retval = -ETIMEDOUT;
if (test_and_clear_bit(DEV_CLEAR_BN, &nec_priv->state))
retval = -EINTR;
return retval;
}
/* Check if the SH state machine is in SGNS. We check twice since there is a very small chance
* we could be blowing through SGNS from SIDS to SDYS if there is already a
* byte available in the handshake state machine. We are interested
* in the case where the handshake is stuck in SGNS due to no byte being
* available to the chip (and thus we can be confident a dma transfer will
* result in at least one byte making it into the chip). This matters
* because we want to be confident before sending a "send eoi" auxilary
* command that we will be able to also put the associated data byte
* in the chip before any potential timeout.
*/
static int source_handshake_is_sgns(struct fluke_priv *e_priv)
{
int i;
for (i = 0; i < 2; ++i) {
if ((fluke_paged_read_byte(e_priv, STATE1_REG, STATE1_PAGE) &
SOURCE_HANDSHAKE_MASK) != SOURCE_HANDSHAKE_SGNS_BITS) {
return 0;
}
}
return 1;
}
static int source_handshake_is_sids_or_sgns(struct fluke_priv *e_priv)
{
unsigned int source_handshake_bits;
source_handshake_bits = fluke_paged_read_byte(e_priv, STATE1_REG, STATE1_PAGE) &
SOURCE_HANDSHAKE_MASK;
return (source_handshake_bits == SOURCE_HANDSHAKE_SGNS_BITS) ||
(source_handshake_bits == SOURCE_HANDSHAKE_SIDS_BITS);
}
/* Wait until the gpib chip is ready to accept a data out byte.
* If the chip is SGNS it is probably waiting for a a byte to
* be written to it.
*/
static int wait_for_data_out_ready(struct gpib_board *board)
{
struct fluke_priv *e_priv = board->private_data;
struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
int retval = 0;
if (wait_event_interruptible(board->wait,
(test_bit(TACS_NUM, &board->status) &&
source_handshake_is_sgns(e_priv)) ||
test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
test_bit(TIMO_NUM, &board->status)))
retval = -ERESTARTSYS;
if (test_bit(TIMO_NUM, &board->status))
retval = -ETIMEDOUT;
if (test_and_clear_bit(DEV_CLEAR_BN, &nec_priv->state))
retval = -EINTR;
return retval;
}
static int wait_for_sids_or_sgns(struct gpib_board *board)
{
struct fluke_priv *e_priv = board->private_data;
struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
int retval = 0;
if (wait_event_interruptible(board->wait,
source_handshake_is_sids_or_sgns(e_priv) ||
test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
test_bit(TIMO_NUM, &board->status)))
retval = -ERESTARTSYS;
if (test_bit(TIMO_NUM, &board->status))
retval = -ETIMEDOUT;
if (test_and_clear_bit(DEV_CLEAR_BN, &nec_priv->state))
retval = -EINTR;
return retval;
}
static void fluke_dma_callback(void *arg)
{
struct gpib_board *board = arg;
struct fluke_priv *e_priv = board->private_data;
struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
unsigned long flags;
spin_lock_irqsave(&board->spinlock, flags);
nec7210_set_reg_bits(nec_priv, IMR1, HR_DOIE | HR_DIIE, HR_DOIE | HR_DIIE);
wake_up_interruptible(&board->wait);
fluke_gpib_internal_interrupt(board);
clear_bit(DMA_WRITE_IN_PROGRESS_BN, &nec_priv->state);
clear_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state);
spin_unlock_irqrestore(&board->spinlock, flags);
}
static int fluke_dma_write(struct gpib_board *board, uint8_t *buffer, size_t length,
size_t *bytes_written)
{
struct fluke_priv *e_priv = board->private_data;
struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
unsigned long flags;
int retval = 0;
dma_addr_t address;
struct dma_async_tx_descriptor *tx_desc;
*bytes_written = 0;
if (WARN_ON_ONCE(length > e_priv->dma_buffer_size))
return -EFAULT;
dmaengine_terminate_all(e_priv->dma_channel);
// write-clear counter
writel(0x0, e_priv->write_transfer_counter);
memcpy(e_priv->dma_buffer, buffer, length);
address = dma_map_single(board->dev, e_priv->dma_buffer,
length, DMA_TO_DEVICE);
/* program dma controller */
retval = fluke_config_dma(board, 1);
if (retval)
goto cleanup;
tx_desc = dmaengine_prep_slave_single(e_priv->dma_channel, address, length, DMA_MEM_TO_DEV,
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
if (!tx_desc) {
dev_err(board->gpib_dev, "failed to allocate dma transmit descriptor\n");
retval = -ENOMEM;
goto cleanup;
}
tx_desc->callback = fluke_dma_callback;
tx_desc->callback_param = board;
spin_lock_irqsave(&board->spinlock, flags);
nec7210_set_reg_bits(nec_priv, IMR1, HR_DOIE, 0);
nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAO, HR_DMAO);
dmaengine_submit(tx_desc);
dma_async_issue_pending(e_priv->dma_channel);
clear_bit(WRITE_READY_BN, &nec_priv->state);
set_bit(DMA_WRITE_IN_PROGRESS_BN, &nec_priv->state);
spin_unlock_irqrestore(&board->spinlock, flags);
// suspend until message is sent
if (wait_event_interruptible(board->wait,
((readl(e_priv->write_transfer_counter) &
write_transfer_counter_mask) == length) ||
test_bit(BUS_ERROR_BN, &nec_priv->state) ||
test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
test_bit(TIMO_NUM, &board->status))) {
retval = -ERESTARTSYS;
}
if (test_bit(TIMO_NUM, &board->status))
retval = -ETIMEDOUT;
if (test_and_clear_bit(DEV_CLEAR_BN, &nec_priv->state))
retval = -EINTR;
if (test_and_clear_bit(BUS_ERROR_BN, &nec_priv->state))
retval = -EIO;
// disable board's dma
nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAO, 0);
dmaengine_terminate_all(e_priv->dma_channel);
// make sure fluke_dma_callback got called
if (test_bit(DMA_WRITE_IN_PROGRESS_BN, &nec_priv->state))
fluke_dma_callback(board);
/* if everything went fine, try to wait until last byte is actually
* transmitted across gpib (but don't try _too_ hard)
*/
if (retval == 0)
retval = wait_for_sids_or_sgns(board);
*bytes_written = readl(e_priv->write_transfer_counter) & write_transfer_counter_mask;
if (WARN_ON_ONCE(*bytes_written > length))
return -EFAULT;
cleanup:
dma_unmap_single(board->dev, address, length, DMA_TO_DEVICE);
return retval;
}
static int fluke_accel_write(struct gpib_board *board, uint8_t *buffer, size_t length,
int send_eoi, size_t *bytes_written)
{
struct fluke_priv *e_priv = board->private_data;
struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
size_t remainder = length;
size_t transfer_size;
ssize_t retval = 0;
size_t dma_remainder = remainder;
if (!e_priv->dma_channel) {
dev_err(board->gpib_dev, "No dma channel available, cannot do accel write.");
return -ENXIO;
}
*bytes_written = 0;
if (length < 1)
return 0;
clear_bit(DEV_CLEAR_BN, &nec_priv->state); // XXX FIXME
if (send_eoi)
--dma_remainder;
while (dma_remainder > 0) {
size_t num_bytes;
retval = wait_for_data_out_ready(board);
if (retval < 0)
break;
transfer_size = (e_priv->dma_buffer_size < dma_remainder) ?
e_priv->dma_buffer_size : dma_remainder;
retval = fluke_dma_write(board, buffer, transfer_size, &num_bytes);
*bytes_written += num_bytes;
if (retval < 0)
break;
dma_remainder -= num_bytes;
remainder -= num_bytes;
buffer += num_bytes;
if (need_resched())
schedule();
}
if (retval < 0)
return retval;
//handle sending of last byte with eoi
if (send_eoi) {
size_t num_bytes;
if (WARN_ON_ONCE(remainder != 1))
return -EFAULT;
/* wait until we are sure we will be able to write the data byte
* into the chip before we send AUX_SEOI. This prevents a timeout
* scenerio where we send AUX_SEOI but then timeout without getting
* any bytes into the gpib chip. This will result in the first byte
* of the next write having a spurious EOI set on the first byte.
*/
retval = wait_for_data_out_ready(board);
if (retval < 0)
return retval;
write_byte(nec_priv, AUX_SEOI, AUXMR);
retval = fluke_dma_write(board, buffer, remainder, &num_bytes);
*bytes_written += num_bytes;
if (retval < 0)
return retval;
remainder -= num_bytes;
}
return 0;
}
static int fluke_get_dma_residue(struct dma_chan *chan, dma_cookie_t cookie)
{
struct dma_tx_state state;
int result;
result = dmaengine_pause(chan);
if (result < 0) {
pr_err("dma pause failed?\n");
return result;
}
dmaengine_tx_status(chan, cookie, &state);
// hardware doesn't support resume, so dont call this
// method unless the dma transfer is done.
return state.residue;
}
static int fluke_dma_read(struct gpib_board *board, uint8_t *buffer,
size_t length, int *end, size_t *bytes_read)
{
struct fluke_priv *e_priv = board->private_data;
struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
int retval = 0;
unsigned long flags;
int residue;
dma_addr_t bus_address;
struct dma_async_tx_descriptor *tx_desc;
dma_cookie_t dma_cookie;
int i;
static const int timeout = 10;
*bytes_read = 0;
*end = 0;
if (length == 0)
return 0;
bus_address = dma_map_single(board->dev, e_priv->dma_buffer,
length, DMA_FROM_DEVICE);
/* program dma controller */
retval = fluke_config_dma(board, 0);
if (retval) {
dma_unmap_single(board->dev, bus_address, length, DMA_FROM_DEVICE);
return retval;
}
tx_desc = dmaengine_prep_slave_single(e_priv->dma_channel,
bus_address, length, DMA_DEV_TO_MEM,
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
if (!tx_desc) {
dev_err(board->gpib_dev, "failed to allocate dma transmit descriptor\n");
dma_unmap_single(NULL, bus_address, length, DMA_FROM_DEVICE);
return -EIO;
}
tx_desc->callback = fluke_dma_callback;
tx_desc->callback_param = board;
spin_lock_irqsave(&board->spinlock, flags);
// enable nec7210 dma
nec7210_set_reg_bits(nec_priv, IMR1, HR_DIIE, 0);
nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, HR_DMAI);
dma_cookie = dmaengine_submit(tx_desc);
dma_async_issue_pending(e_priv->dma_channel);
set_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state);
clear_bit(READ_READY_BN, &nec_priv->state);
spin_unlock_irqrestore(&board->spinlock, flags);
// wait for data to transfer
if (wait_event_interruptible(board->wait,
test_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state) == 0 ||
test_bit(RECEIVED_END_BN, &nec_priv->state) ||
test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
test_bit(TIMO_NUM, &board->status))) {
retval = -ERESTARTSYS;
}
if (test_bit(TIMO_NUM, &board->status))
retval = -ETIMEDOUT;
if (test_bit(DEV_CLEAR_BN, &nec_priv->state))
retval = -EINTR;
/* If we woke up because of end, wait until the dma transfer has pulled
* the data byte associated with the end before we cancel the dma transfer.
*/
if (test_bit(RECEIVED_END_BN, &nec_priv->state)) {
for (i = 0; i < timeout; ++i) {
if (test_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state) == 0)
break;
if ((read_byte(nec_priv, ADR0) & DATA_IN_STATUS) == 0)
break;
usleep_range(10, 15);
}
if (i == timeout)
pr_warn("fluke_gpib: timeout waiting for dma to transfer end data byte.\n");
}
// stop the dma transfer
nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, 0);
/* delay a little just to make sure any bytes in dma controller's fifo get
* written to memory before we disable it
*/
usleep_range(10, 15);
residue = fluke_get_dma_residue(e_priv->dma_channel, dma_cookie);
if (WARN_ON_ONCE(residue > length || residue < 0))
return -EFAULT;
*bytes_read += length - residue;
dmaengine_terminate_all(e_priv->dma_channel);
// make sure fluke_dma_callback got called
if (test_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state))
fluke_dma_callback(board);
dma_unmap_single(board->dev, bus_address, length, DMA_FROM_DEVICE);
memcpy(buffer, e_priv->dma_buffer, *bytes_read);
/* If we got an end interrupt, figure out if it was
* associated with the last byte we dma'd or with a
* byte still sitting on the cb7210.
*/
spin_lock_irqsave(&board->spinlock, flags);
if (test_bit(READ_READY_BN, &nec_priv->state) == 0) {
// There is no byte sitting on the cb7210. If we
// saw an end interrupt, we need to deal with it now
if (test_and_clear_bit(RECEIVED_END_BN, &nec_priv->state))
*end = 1;
}
spin_unlock_irqrestore(&board->spinlock, flags);
return retval;
}
static int fluke_accel_read(struct gpib_board *board, uint8_t *buffer, size_t length,
int *end, size_t *bytes_read)
{
struct fluke_priv *e_priv = board->private_data;
struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
size_t remain = length;
size_t transfer_size;
int retval = 0;
size_t dma_nbytes;
*end = 0;
*bytes_read = 0;
smp_mb__before_atomic();
clear_bit(DEV_CLEAR_BN, &nec_priv->state); // XXX FIXME
smp_mb__after_atomic();
retval = wait_for_read(board);
if (retval < 0)
return retval;
nec7210_release_rfd_holdoff(board, nec_priv);
while (remain > 0) {
transfer_size = (e_priv->dma_buffer_size < remain) ?
e_priv->dma_buffer_size : remain;
retval = fluke_dma_read(board, buffer, transfer_size, end, &dma_nbytes);
remain -= dma_nbytes;
buffer += dma_nbytes;
*bytes_read += dma_nbytes;
if (*end)
break;
if (retval < 0)
return retval;
if (need_resched())
schedule();
}
return retval;
}
static gpib_interface_t fluke_unaccel_interface = {
.name = "fluke_unaccel",
.attach = fluke_attach_holdoff_all,
.detach = fluke_detach,
.read = fluke_read,
.write = fluke_write,
.command = fluke_command,
.take_control = fluke_take_control,
.go_to_standby = fluke_go_to_standby,
.request_system_control = fluke_request_system_control,
.interface_clear = fluke_interface_clear,
.remote_enable = fluke_remote_enable,
.enable_eos = fluke_enable_eos,
.disable_eos = fluke_disable_eos,
.parallel_poll = fluke_parallel_poll,
.parallel_poll_configure = fluke_parallel_poll_configure,
.parallel_poll_response = fluke_parallel_poll_response,
.line_status = fluke_line_status,
.update_status = fluke_update_status,
.primary_address = fluke_primary_address,
.secondary_address = fluke_secondary_address,
.serial_poll_response = fluke_serial_poll_response,
.serial_poll_status = fluke_serial_poll_status,
.t1_delay = fluke_t1_delay,
.return_to_local = fluke_return_to_local,
};
/* fluke_hybrid uses dma for writes but not for reads. Added
* to deal with occasional corruption of bytes seen when doing dma
* reads. From looking at the cb7210 vhdl, I believe the corruption
* is due to a hardware bug triggered by the cpu reading a cb7210
* }
* register just as the dma controller is also doing a read.
*/
static gpib_interface_t fluke_hybrid_interface = {
.name = "fluke_hybrid",
.attach = fluke_attach_holdoff_all,
.detach = fluke_detach,
.read = fluke_read,
.write = fluke_accel_write,
.command = fluke_command,
.take_control = fluke_take_control,
.go_to_standby = fluke_go_to_standby,
.request_system_control = fluke_request_system_control,
.interface_clear = fluke_interface_clear,
.remote_enable = fluke_remote_enable,
.enable_eos = fluke_enable_eos,
.disable_eos = fluke_disable_eos,
.parallel_poll = fluke_parallel_poll,
.parallel_poll_configure = fluke_parallel_poll_configure,
.parallel_poll_response = fluke_parallel_poll_response,
.line_status = fluke_line_status,
.update_status = fluke_update_status,
.primary_address = fluke_primary_address,
.secondary_address = fluke_secondary_address,
.serial_poll_response = fluke_serial_poll_response,
.serial_poll_status = fluke_serial_poll_status,
.t1_delay = fluke_t1_delay,
.return_to_local = fluke_return_to_local,
};
static gpib_interface_t fluke_interface = {
.name = "fluke",
.attach = fluke_attach_holdoff_end,
.detach = fluke_detach,
.read = fluke_accel_read,
.write = fluke_accel_write,
.command = fluke_command,
.take_control = fluke_take_control,
.go_to_standby = fluke_go_to_standby,
.request_system_control = fluke_request_system_control,
.interface_clear = fluke_interface_clear,
.remote_enable = fluke_remote_enable,
.enable_eos = fluke_enable_eos,
.disable_eos = fluke_disable_eos,
.parallel_poll = fluke_parallel_poll,
.parallel_poll_configure = fluke_parallel_poll_configure,
.parallel_poll_response = fluke_parallel_poll_response,
.line_status = fluke_line_status,
.update_status = fluke_update_status,
.primary_address = fluke_primary_address,
.secondary_address = fluke_secondary_address,
.serial_poll_response = fluke_serial_poll_response,
.serial_poll_status = fluke_serial_poll_status,
.t1_delay = fluke_t1_delay,
.return_to_local = fluke_return_to_local,
};
irqreturn_t fluke_gpib_internal_interrupt(struct gpib_board *board)
{
int status0, status1, status2;
struct fluke_priv *priv = board->private_data;
struct nec7210_priv *nec_priv = &priv->nec7210_priv;
int retval = IRQ_NONE;
if (read_byte(nec_priv, ADR0) & DATA_IN_STATUS)
set_bit(READ_READY_BN, &nec_priv->state);
status0 = fluke_paged_read_byte(priv, ISR0_IMR0, ISR0_IMR0_PAGE);
status1 = read_byte(nec_priv, ISR1);
status2 = read_byte(nec_priv, ISR2);
if (status0 & FLUKE_IFCI_BIT) {
push_gpib_event(board, EventIFC);
retval = IRQ_HANDLED;
}
if (nec7210_interrupt_have_status(board, nec_priv, status1, status2) == IRQ_HANDLED)
retval = IRQ_HANDLED;
if (read_byte(nec_priv, ADR0) & DATA_IN_STATUS) {
if (test_bit(RFD_HOLDOFF_BN, &nec_priv->state))
set_bit(READ_READY_BN, &nec_priv->state);
else
clear_bit(READ_READY_BN, &nec_priv->state);
}
if (retval == IRQ_HANDLED)
wake_up_interruptible(&board->wait);
return retval;
}
static irqreturn_t fluke_gpib_interrupt(int irq, void *arg)
{
struct gpib_board *board = arg;
unsigned long flags;
irqreturn_t retval;
spin_lock_irqsave(&board->spinlock, flags);
retval = fluke_gpib_internal_interrupt(board);
spin_unlock_irqrestore(&board->spinlock, flags);
return retval;
}
static int fluke_allocate_private(struct gpib_board *board)
{
struct fluke_priv *priv;
board->private_data = kmalloc(sizeof(struct fluke_priv), GFP_KERNEL);
if (!board->private_data)
return -ENOMEM;
priv = board->private_data;
memset(priv, 0, sizeof(struct fluke_priv));
init_nec7210_private(&priv->nec7210_priv);
priv->dma_buffer_size = 0x7ff;
priv->dma_buffer = kmalloc(priv->dma_buffer_size, GFP_KERNEL);
if (!priv->dma_buffer)
return -ENOMEM;
return 0;
}
static void fluke_generic_detach(struct gpib_board *board)
{
if (board->private_data) {
struct fluke_priv *e_priv = board->private_data;
kfree(e_priv->dma_buffer);
kfree(board->private_data);
board->private_data = NULL;
}
}
// generic part of attach functions shared by all cb7210 boards
static int fluke_generic_attach(struct gpib_board *board)
{
struct fluke_priv *e_priv;
struct nec7210_priv *nec_priv;
int retval;
board->status = 0;
retval = fluke_allocate_private(board);
if (retval < 0)
return retval;
e_priv = board->private_data;
nec_priv = &e_priv->nec7210_priv;
nec_priv->read_byte = fluke_locking_read_byte;
nec_priv->write_byte = fluke_locking_write_byte;
nec_priv->offset = fluke_reg_offset;
nec_priv->type = CB7210;
return 0;
}
static int fluke_config_dma(struct gpib_board *board, int output)
{
struct fluke_priv *e_priv = board->private_data;
struct dma_slave_config config;
config.src_maxburst = 1;
config.dst_maxburst = 1;
config.device_fc = true;
if (output) {
config.direction = DMA_MEM_TO_DEV;
config.src_addr = 0;
config.dst_addr = e_priv->dma_port_res->start;
config.src_addr_width = 1;
config.dst_addr_width = 1;
} else {
config.direction = DMA_DEV_TO_MEM;
config.src_addr = e_priv->dma_port_res->start;
config.dst_addr = 0;
config.src_addr_width = 1;
config.dst_addr_width = 1;
}
return dmaengine_slave_config(e_priv->dma_channel, &config);
}
static int fluke_init(struct fluke_priv *e_priv, struct gpib_board *board, int handshake_mode)
{
struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
nec7210_board_reset(nec_priv, board);
write_byte(nec_priv, AUX_LO_SPEED, AUXMR);
/* set clock register for driving frequency
* ICR should be set to clock in megahertz (1-15) and to zero
* for clocks faster than 15 MHz (max 20MHz)
*/
write_byte(nec_priv, ICR | 10, AUXMR);
nec7210_set_handshake_mode(board, nec_priv, handshake_mode);
nec7210_board_online(nec_priv, board);
/* poll so we can detect ATN changes */
if (gpib_request_pseudo_irq(board, fluke_gpib_interrupt)) {
dev_err(board->gpib_dev, "failed to allocate pseudo_irq\n");
return -EINVAL;
}
fluke_paged_write_byte(e_priv, FLUKE_IFCIE_BIT, ISR0_IMR0, ISR0_IMR0_PAGE);
return 0;
}
/* This function is passed to dma_request_channel() in order to
* select the pl330 dma channel which has been hardwired to
* the gpib controller.
*/
static bool gpib_dma_channel_filter(struct dma_chan *chan, void *filter_param)
{
// select the channel which is wired to the gpib chip
return chan->chan_id == 0;
}
static int fluke_attach_impl(struct gpib_board *board, const gpib_board_config_t *config,
unsigned int handshake_mode)
{
struct fluke_priv *e_priv;
struct nec7210_priv *nec_priv;
int isr_flags = 0;
int retval;
int irq;
struct resource *res;
dma_cap_mask_t dma_cap;
if (!fluke_gpib_pdev) {
dev_err(board->gpib_dev, "No fluke device was found, attach failed.\n");
return -ENODEV;
}
retval = fluke_generic_attach(board);
if (retval)
return retval;
e_priv = board->private_data;
nec_priv = &e_priv->nec7210_priv;
nec_priv->offset = fluke_reg_offset;
board->dev = &fluke_gpib_pdev->dev;
res = platform_get_resource(fluke_gpib_pdev, IORESOURCE_MEM, 0);
if (!res) {
dev_err(&fluke_gpib_pdev->dev, "Unable to locate mmio resource\n");
return -ENODEV;
}
if (request_mem_region(res->start,
resource_size(res),
fluke_gpib_pdev->name) == NULL) {
dev_err(&fluke_gpib_pdev->dev, "cannot claim registers\n");
return -ENXIO;
}
e_priv->gpib_iomem_res = res;
nec_priv->mmiobase = ioremap(e_priv->gpib_iomem_res->start,
resource_size(e_priv->gpib_iomem_res));
if (!nec_priv->mmiobase) {
dev_err(&fluke_gpib_pdev->dev, "Could not map I/O memory\n");
return -ENOMEM;
}
res = platform_get_resource(fluke_gpib_pdev, IORESOURCE_MEM, 1);
if (!res) {
dev_err(&fluke_gpib_pdev->dev, "Unable to locate mmio resource for gpib dma port\n");
return -ENODEV;
}
if (request_mem_region(res->start,
resource_size(res),
fluke_gpib_pdev->name) == NULL) {
dev_err(&fluke_gpib_pdev->dev, "cannot claim registers\n");
return -ENXIO;
}
e_priv->dma_port_res = res;
res = platform_get_resource(fluke_gpib_pdev, IORESOURCE_MEM, 2);
if (!res) {
dev_err(&fluke_gpib_pdev->dev, "Unable to locate mmio resource for write transfer counter\n");
return -ENODEV;
}
if (request_mem_region(res->start,
resource_size(res),
fluke_gpib_pdev->name) == NULL) {
dev_err(&fluke_gpib_pdev->dev, "cannot claim registers\n");
return -ENXIO;
}
e_priv->write_transfer_counter_res = res;
e_priv->write_transfer_counter = ioremap(e_priv->write_transfer_counter_res->start,
resource_size(e_priv->write_transfer_counter_res));
if (!e_priv->write_transfer_counter) {
dev_err(&fluke_gpib_pdev->dev, "Could not map I/O memory\n");
return -ENOMEM;
}
irq = platform_get_irq(fluke_gpib_pdev, 0);
if (irq < 0) {
dev_err(&fluke_gpib_pdev->dev, "failed to obtain IRQ\n");
return -EBUSY;
}
retval = request_irq(irq, fluke_gpib_interrupt, isr_flags, fluke_gpib_pdev->name, board);
if (retval) {
dev_err(&fluke_gpib_pdev->dev,
"cannot register interrupt handler err=%d\n",
retval);
return retval;
}
e_priv->irq = irq;
dma_cap_zero(dma_cap);
dma_cap_set(DMA_SLAVE, dma_cap);
e_priv->dma_channel = dma_request_channel(dma_cap, gpib_dma_channel_filter, NULL);
if (!e_priv->dma_channel) {
dev_err(board->gpib_dev, "failed to allocate a dma channel.\n");
// we don't error out here because unaccel interface will still
// work without dma
}
return fluke_init(e_priv, board, handshake_mode);
}
int fluke_attach_holdoff_all(struct gpib_board *board, const gpib_board_config_t *config)
{
return fluke_attach_impl(board, config, HR_HLDA);
}
int fluke_attach_holdoff_end(struct gpib_board *board, const gpib_board_config_t *config)
{
return fluke_attach_impl(board, config, HR_HLDE);
}
void fluke_detach(struct gpib_board *board)
{
struct fluke_priv *e_priv = board->private_data;
struct nec7210_priv *nec_priv;
if (e_priv) {
if (e_priv->dma_channel)
dma_release_channel(e_priv->dma_channel);
gpib_free_pseudo_irq(board);
nec_priv = &e_priv->nec7210_priv;
if (nec_priv->mmiobase) {
fluke_paged_write_byte(e_priv, 0, ISR0_IMR0, ISR0_IMR0_PAGE);
nec7210_board_reset(nec_priv, board);
}
if (e_priv->irq)
free_irq(e_priv->irq, board);
if (e_priv->write_transfer_counter_res) {
release_mem_region(e_priv->write_transfer_counter_res->start,
resource_size(e_priv->write_transfer_counter_res));
}
if (e_priv->dma_port_res) {
release_mem_region(e_priv->dma_port_res->start,
resource_size(e_priv->dma_port_res));
}
if (e_priv->gpib_iomem_res)
release_mem_region(e_priv->gpib_iomem_res->start,
resource_size(e_priv->gpib_iomem_res));
}
fluke_generic_detach(board);
}
static int fluke_gpib_probe(struct platform_device *pdev)
{
fluke_gpib_pdev = pdev;
return 0;
}
static const struct of_device_id fluke_gpib_of_match[] = {
{ .compatible = "flk,fgpib-4.0"},
{ {0} }
};
MODULE_DEVICE_TABLE(of, fluke_gpib_of_match);
static struct platform_driver fluke_gpib_platform_driver = {
.driver = {
.name = DRV_NAME,
.of_match_table = fluke_gpib_of_match,
},
.probe = &fluke_gpib_probe
};
static int __init fluke_init_module(void)
{
int result;
result = platform_driver_register(&fluke_gpib_platform_driver);
if (result) {
pr_err("platform_driver_register failed: error = %d\n", result);
return result;
}
result = gpib_register_driver(&fluke_unaccel_interface, THIS_MODULE);
if (result) {
pr_err("gpib_register_driver failed: error = %d\n", result);
goto err_unaccel;
}
result = gpib_register_driver(&fluke_hybrid_interface, THIS_MODULE);
if (result) {
pr_err("gpib_register_driver failed: error = %d\n", result);
goto err_hybrid;
}
result = gpib_register_driver(&fluke_interface, THIS_MODULE);
if (result) {
pr_err("gpib_register_driver failed: error = %d\n", result);
goto err_interface;
}
return 0;
err_interface:
gpib_unregister_driver(&fluke_hybrid_interface);
err_hybrid:
gpib_unregister_driver(&fluke_unaccel_interface);
err_unaccel:
platform_driver_unregister(&fluke_gpib_platform_driver);
return result;
}
static void __exit fluke_exit_module(void)
{
gpib_unregister_driver(&fluke_unaccel_interface);
gpib_unregister_driver(&fluke_hybrid_interface);
gpib_unregister_driver(&fluke_interface);
platform_driver_unregister(&fluke_gpib_platform_driver);
}
module_init(fluke_init_module);
module_exit(fluke_exit_module);