-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspi.c
executable file
·1182 lines (1112 loc) · 34 KB
/
spi.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
/************************************************************
*
* Copyright (C) 2003, Motorola. All Rights Reserved
*
* FILE spi.h
* PROGRAMMER(S): J.X.Chang ([email protected])
*
*
* DATE OF CREATION: March 8, 2003
*
* SYNOPSIS:
*
* DESCRIPTION: It's driver of SPI in ADSP25535(ADI's DSP). It can
* only be used in unix or linux.
* CAUTION: It start with a slave, output disable working mode,
* you may need use ioctl to change it's configuration.
**************************************************************
* MODIFICATION HISTORY:
* March 8, 2003 File spi.c Created.
************************************************************/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <asm/io.h>
#include <asm/irq.h>
#include "spi.h"
/* definitions */
#define SPI0_REGBASE 0xffc03000
#define SPI1_REGBASE 0xffc03400
#define SPI_BUF_LEN 128
#define SPI_REGSIZE 16
#define SPI_MAJOR 252 /* experiential */
#define SPI0_MINOR 0
#define SPI1_MINOR 1
#define SPI_DEVNAME "spi"
#define SPI_INT0NAME "spiint0" /* Should be less than 19 chars. */
#define SPI_INT1NAME "spiint1" /* Should be less than 19 chars. */
typedef struct Spi_Device_t
{
int opened;
int nonblock;
int master;
int bdrate;
int channel; /* only valid in master mode */
int polar;
int phase;
int outenable;
int irqnum;
int byteorder; /* 0: MSB first; 1: LSB first; */
int length; /* 0: 8 bits; 1: 16 bits */
int sendopt; /* 0: Sending lastword if Txbuf Empty;
1: Sending 0 if Txbuf Empty; */
int recvopt; /* 0: Discard packet if Rxbuffer is full;
1: Flush Rxbuffer if it is full; */
unsigned int regbase;
unsigned int txrpos;
unsigned int txwpos;
unsigned int rxrpos;
unsigned int rxwpos;
unsigned short txbuf[SPI_BUF_LEN];
unsigned short rxbuf[SPI_BUF_LEN];
struct fasync_struct *fasyc;
wait_queue_head_t* tx_wq;
wait_queue_head_t* rx_wq;
}spi_device_t;
/* Globals */
/* We must declare queue structure by the following macro.
* firstly declare 'wait_queue_head_t' and then 'init_waitqueue_head'
* doesn't work in 2.4.7 kernel / redhat 7.2 */
static DECLARE_WAIT_QUEUE_HEAD(spitxq0);
static DECLARE_WAIT_QUEUE_HEAD(spitxq1);
static DECLARE_WAIT_QUEUE_HEAD(spirxq0);
static DECLARE_WAIT_QUEUE_HEAD(spirxq1);
static spi_device_t spiinfo[2];
static int set_spi_reg(spi_device_t *pdev, unsigned int offset, unsigned short sdata);
static int get_spi_reg(spi_device_t *pdev, unsigned int offset, unsigned short *pdata);
static int txq_isfull(spi_device_t *pdev);
static int rxq_isfull(spi_device_t *pdev);
/***********************************************************
*
* FUNCTION NAME :set_spi_reg
*
* INPUTS/OUTPUTS:
* in_pdev - point to device information structure base address.
* in_offset - register address, offset to it's base.
* in_sdata - data which would be write into register.
*
* VALUE RETURNED:
* Always 0
*
* FUNCTION(S) CALLED:
*
* GLOBAL VARIABLES REFERENCED: spiinfo
*
* GLOBAL VARIABLES MODIFIED: NIL
*
* DESCRIPTION: Using it set SPI's register.
*
* CAUTION: SPI registers' address are in word aliened.
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/
static int set_spi_reg(spi_device_t *pdev, unsigned int offset, unsigned short sdata)
{
*(unsigned short*)(pdev->regbase + offset) = sdata;
asm("ssync;");
return 0;
}
/***********************************************************
*
* FUNCTION NAME :get_spi_reg
*
* INPUTS/OUTPUTS:
* in_pdev - point to device information structure base address.
* in_offset - register address, offset to it's base.
* our_pdata - data which would be read from relative register.
*
* VALUE RETURNED:
* Always 0
*
* FUNCTION(S) CALLED:
*
* GLOBAL VARIABLES REFERENCED: spiinfo
*
* GLOBAL VARIABLES MODIFIED: NIL
*
* DESCRIPTION: Using it set SPI's register.
*
* CAUTION: SPI registers' address are in word aliened.
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/
static int get_spi_reg(spi_device_t *pdev, unsigned int offset, unsigned short *pdata)
{
*pdata = *(unsigned short*)(pdev->regbase + offset);
asm("ssync;");
return 0;
}
/***********************************************************
*
* FUNCTION NAME :txq_isfull
*
* INPUTS/OUTPUTS:
* in_pdev - point to device information structure base address.
*
* VALUE RETURNED:
* 0 Tx queue is empty
* 1 Tx queue is full
* other: A part of queue are in use.
*
* FUNCTION(S) CALLED:
*
* GLOBAL VARIABLES REFERENCED: spiinfo
*
* GLOBAL VARIABLES MODIFIED: NIL
*
* DESCRIPTION: Check whether Tx queue is full or empty
*
* CAUTION:
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/
static int txq_isfull(spi_device_t *pdev)
{
int idlenum;
idlenum = (pdev->txrpos + SPI_BUF_LEN - pdev->txwpos) % SPI_BUF_LEN;
/* num = 1, queue is full, 0 empty, others partly used */
return idlenum;
}
/***********************************************************
*
* FUNCTION NAME :rxq_isfull
*
* INPUTS/OUTPUTS:
* in_pdev - point to device information structure base address.
*
* VALUE RETURNED:
* 0 Rx queue is empty
* 1 Rx queue is full
* other: A part of queue are in use.
*
* FUNCTION(S) CALLED:
*
* GLOBAL VARIABLES REFERENCED: spiinfo
*
* GLOBAL VARIABLES MODIFIED: NIL
*
* DESCRIPTION: Check whether Rx queue is full or empty
*
* CAUTION:
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/
static int rxq_isfull(spi_device_t *pdev)
{
int idlenum;
idlenum = (pdev->rxrpos + SPI_BUF_LEN - pdev->rxwpos) % SPI_BUF_LEN;
/* num = 1, queue is full, 0 empty, others partly used */
return idlenum;
}
/***********************************************************
*
* FUNCTION NAME :spi_reg_reset
*
* INPUTS/OUTPUTS:
* in_idev - device number 0 SPI0, 1 SPI 1, other unavailable.
* VALUE RETURNED:
* void
*
* FUNCTION(S) CALLED:
*
* GLOBAL VARIABLES REFERENCED:
*
* GLOBAL VARIABLES MODIFIED: NIL
*
* DESCRIPTION: Reset SPI to initialization state.
*
* CAUTION:
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/
void spi_reg_reset(spi_device_t *pdev)
{
unsigned short sdata = 0;
/* Ctrl register */
sdata = BIT_CTL_OPENDRAIN | BIT_CTL_PHASE;
set_spi_reg(pdev, SPI_CTRL, sdata); /* Disable SPI, open drain */
set_spi_reg(pdev, SPI_FLAG, 0xff00); /* Disable pin, out 3 state*/
set_spi_reg(pdev, SPI_BAUD, SPI_DEFAULT_BARD); /* Default clock. */
set_spi_reg(pdev, SPI_STAU, 0xffff); /* Clear all status bits.*/
}
/***********************************************************
*
* FUNCTION NAME :spi_irq
*
* INPUTS/OUTPUTS:
* in_irq - Interrupt vector number.
* in_dev_id - point to device information structure base address.
* in_regs - unuse here.
*
* VALUE RETURNED:
* void
*
* FUNCTION(S) CALLED:
*
* GLOBAL VARIABLES REFERENCED: spiinfo
*
* GLOBAL VARIABLES MODIFIED: NIL
*
* DESCRIPTION: ISR of SPI
*
* CAUTION:
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/
void spi_irq(int irq, void *dev_id, struct pt_regs *regs)
{
unsigned short regdata;
spi_device_t *pdev = (spi_device_t*)dev_id;
/* There maybe a interrupt after enable irq before sending */
get_spi_reg(pdev, SPI_STAU, ®data);
if(!(regdata & 0x0001))
return;
/* SPI interrupt is caused sending over.*/
/* Is there any data unsend? */
if(txq_isfull(pdev))
{
set_spi_reg(pdev, SPI_TXBUFF, pdev->txbuf[pdev->txrpos]);
pdev->txrpos++;
if(pdev->txrpos == SPI_BUF_LEN)
pdev->txrpos = 0;
}
else
{
/* There is no data unsend, and it's a master, stop interrupt.
--- Interrupt is cleared by writing Tx register, if we don't
disable this irq, interrupt always on. */
if(pdev->master)
disable_irq(irq);
}
/* Is Rx Buffer full? */
get_spi_reg(pdev, SPI_RXBUFF, ®data);
if(rxq_isfull(pdev) != 1)
{
/* There is free space */
pdev->rxbuf[(pdev->rxwpos)] = regdata;
pdev->rxwpos++;
if(pdev->rxwpos == SPI_BUF_LEN)
pdev->rxwpos = 0;
}
else if(pdev->recvopt == 1)
{
/* There is no space and we must flush old data */
pdev->rxbuf[(pdev->rxwpos)] = regdata;
pdev->rxwpos++;
if(pdev->rxwpos == SPI_BUF_LEN)
pdev->rxwpos = 0;
pdev->rxrpos++;
if(pdev->rxrpos == SPI_BUF_LEN)
pdev->rxrpos = 0;
}
/* Give a signal to user program. */
if(pdev->fasyc)
kill_fasync(&(pdev->fasyc), SIGIO, POLLIN);
/* wake up read/write block. */
wake_up_interruptible(pdev->tx_wq);
wake_up_interruptible(pdev->rx_wq);
return;
}
/***********************************************************
*
* FUNCTION NAME :spi_ioctl
*
* INPUTS/OUTPUTS:
* in_inode - Description of openned file.
* in_filp - Description of openned file.
* in_cmd - Command passed into ioctl system call.
* in/out_arg - It is parameters which is specified by last command
*
* RETURN:
* 0 OK
* -EINVAL Invalid baudrate
*
* FUNCTION(S) CALLED:
*
* GLOBAL VARIABLES REFERENCED: spiinfo
*
* GLOBAL VARIABLES MODIFIED: NIL
*
* DESCRIPTION:
*
* CAUTION:
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/
static int spi_ioctl(struct inode *inode, struct file *filp, uint cmd, unsigned long arg)
{
unsigned short regdata;
spi_device_t *pdev = filp->private_data;
switch (cmd)
{
case CMD_SPI_OUT_ENABLE:
{
get_spi_reg(pdev, SPI_CTRL, ®data);
if(arg)
{
/* Normal output */
pdev->outenable = CFG_SPI_OUTENABLE;
set_spi_reg(pdev, SPI_CTRL, regdata & ~BIT_CTL_OPENDRAIN);
}
else
{
/* Open drain */
pdev->outenable = CFG_SPI_OUTDISABLE;
set_spi_reg(pdev, SPI_CTRL, regdata | BIT_CTL_OPENDRAIN);
}
break;
}
case CMD_SPI_SET_BAUDRATE:
{
/* BaudRate 0,1 unavail */
if((unsigned short)arg <= 1)
return -EINVAL;
/* SPI's baud rate is SCLK / ( arg * 2) */
pdev->bdrate = (unsigned short)arg;
set_spi_reg(pdev, SPI_BAUD, (unsigned short)arg);
break;
}
case CMD_SPI_SET_POLAR:
{
/* Can't change clock polar when queues are not empty. */
if((pdev->txrpos != pdev->txwpos) ||
(pdev->rxrpos != pdev->rxwpos))
return -EBUSY;
get_spi_reg(pdev, SPI_CTRL, ®data);
if(arg)
{
/* Clk Active Low */
pdev->polar = CFG_SPI_ACTLOW;
set_spi_reg(pdev, SPI_CTRL, regdata | BIT_CTL_POLAR );
}
else
{
/* Clk Active High */
pdev->polar = CFG_SPI_ACTHIGH;
set_spi_reg(pdev, SPI_CTRL, regdata & ~BIT_CTL_POLAR );
}
break;
}
case CMD_SPI_SET_PHASE:
{
/* Can't change clock's phase when queues are not empty. */
if((pdev->txrpos != pdev->txwpos) ||
(pdev->rxrpos != pdev->rxwpos))
return -EBUSY;
get_spi_reg(pdev, SPI_CTRL, ®data);
if(arg)
{
/* Clk toggled from transferring */
pdev->phase = CFG_SPI_PHASESTART;
set_spi_reg(pdev, SPI_CTRL, regdata | BIT_CTL_PHASE );
}
else
{
/* Clk toggled middle transferring */
pdev->phase = CFG_SPI_PHASEMID;
set_spi_reg(pdev, SPI_CTRL, regdata & ~BIT_CTL_PHASE );
}
break;
}
case CMD_SPI_SET_MASTER:
{
/* Can't change master mode after transfering. */
if(pdev->txrpos || pdev->txwpos ||
pdev->rxrpos || pdev->rxwpos)
{
return -EBUSY;
}
get_spi_reg(pdev, SPI_CTRL, ®data);
if(arg == 0)
{
pdev->master = CFG_SPI_SLAVE;
/* Slave Mode */
regdata &= ~BIT_CTL_MASTER;
/* Enable SPI */
set_spi_reg(pdev, SPI_CTRL, regdata | BIT_CTL_ENABLE);
}
else
{
pdev->master = CFG_SPI_MASTER;
/* Change Tx mode: Writing Tx Buff causes sending. */
regdata |= BIT_CTL_TXMOD;
/* Master Mode */
regdata |= BIT_CTL_MASTER;
/* Disable Interrupt */
//disable_irq(pdev->irqnum);
/* Enable SPI */
set_spi_reg(pdev, SPI_CTRL, regdata | BIT_CTL_ENABLE);
}
break;
}
case CMD_SPI_SET_SENDOPT:
{
get_spi_reg(pdev, SPI_CTRL, ®data);
if(arg)
{
/* Send 0 if tx buffer is empty. */
pdev->sendopt = CFG_SPI_SENELAST;
set_spi_reg(pdev, SPI_CTRL, regdata | BIT_CTL_SENDOPT );
}
else
{
/* Send last word if tx buffer is empty. */
pdev->sendopt = CFG_SPI_SENDZERO;
set_spi_reg(pdev, SPI_CTRL, regdata & ~BIT_CTL_SENDOPT );
}
break;
}
case CMD_SPI_SET_RECVOPT:
{
get_spi_reg(pdev, SPI_CTRL, ®data);
if(arg)
{
/* Flush received data if Rx Buffer is full */
pdev->recvopt = CFG_SPI_RCVFLUSH;
/*set_spi_reg(pdev, SPI_CTRL, regdata | 0x0008 );*/
}
else
{
/* Discard new data if Rx buffer is null */
pdev->recvopt = CFG_SPI_RCVDISCARD;
/*set_spi_reg(pdev, SPI_CTRL, regdata & ~0x0008 );*/
}
break;
}
case CMD_SPI_SET_ORDER:
{
/* Can't change sending order when queues are not empty. */
if((pdev->txrpos != pdev->txwpos) ||
(pdev->rxrpos != pdev->rxwpos))
return -EBUSY;
get_spi_reg(pdev, SPI_CTRL, ®data);
if(arg)
{
/* LSB first send. */
pdev->byteorder = CFG_SPI_LSBFIRST;
set_spi_reg(pdev, SPI_CTRL, regdata | BIT_CTL_BITORDER);
}
else
{
/* MSB first send. */
pdev->byteorder = CFG_SPI_MSBFIRST;
set_spi_reg(pdev, SPI_CTRL, regdata & ~BIT_CTL_BITORDER);
}
break;
}
case CMD_SPI_SET_LENGTH16:
{
/* Can't change word's length when queues are not empty. */
if((pdev->txrpos != pdev->txwpos) ||
(pdev->rxrpos != pdev->rxwpos))
return -EBUSY;
get_spi_reg(pdev, SPI_CTRL, ®data);
if(arg)
{
/* 16 bits each word, that is, 2 bytes data sent each time. */
pdev->length = CFG_SPI_WORDSIZE16;
set_spi_reg(pdev, SPI_CTRL, regdata | BIT_CTL_WORDSIZE);
}
else
{
/* 8 bits each word, that is, 1 byte data sent each time. */
pdev->length = CFG_SPI_WORDSIZE8;
set_spi_reg(pdev, SPI_CTRL, regdata & ~BIT_CTL_WORDSIZE);
}
break;
}
case CMD_SPI_MISO_ENABLE:
{
get_spi_reg(pdev, SPI_CTRL, ®data);
if(arg)
set_spi_reg(pdev, SPI_CTRL, regdata | BIT_CTL_MISOENABLE);
else
set_spi_reg(pdev, SPI_CTRL, regdata & ~BIT_CTL_MISOENABLE);
break;
}
case CMD_SPI_SET_CSAVAIL:
{
get_spi_reg(pdev, SPI_CTRL, ®data);
/* First clear CS */
if((unsigned short)arg == 0)
set_spi_reg(pdev, SPI_CTRL, 0xff00);
else
set_spi_reg(pdev, SPI_CTRL, regdata | (unsigned short)arg);
break;
}
case CMD_SPI_SET_CSENABLE:
{
if((arg > 7) || (arg < 1))
return -EINVAL;
get_spi_reg(pdev, SPI_FLAG, ®data);
set_spi_reg(pdev, SPI_FLAG, regdata | (unsigned short)(1 << arg));
break;
}
case CMD_SPI_SET_CSDISABLE:
{
if((arg > 7) || (arg < 1))
return -EINVAL;
get_spi_reg(pdev, SPI_FLAG, ®data);
set_spi_reg(pdev, SPI_FLAG, regdata & ~(unsigned short)(1 << arg));
break;
}
case CMD_SPI_SET_CSLOW:
{
if((arg > 7) || (arg < 1))
return -EINVAL;
get_spi_reg(pdev, SPI_FLAG, ®data);
set_spi_reg(pdev, SPI_FLAG, regdata & ~(unsigned short)(1 << arg));
break;
}
case CMD_SPI_SET_CSHIGH:
{
if((arg > 7) || (arg < 1))
return -EINVAL;
get_spi_reg(pdev, SPI_FLAG, ®data);
set_spi_reg(pdev, SPI_FLAG, regdata | (unsigned short)(1 << arg));
break;
}
/* The following is for debug use. */
case CMD_SPI_GET_STAT:
{
/* Return the status register, should be for debug use only. */
get_spi_reg(pdev, SPI_STAU, (unsigned short*)arg);
break;
}
case CMD_SPI_GET_CFG:
{
/* Return the ctrl register, should be for debug use only. */
get_spi_reg(pdev, SPI_CTRL, (unsigned short*)arg);
break;
}
case CMD_SPI_GET_ALLCONFIG:
{
unsigned short usreg;
/*
printk("opened: %d.\n",spiinfo[1].opened);
printk("nonblock: %d.\n",spiinfo[1].nonblock);
printk("master: %d.\n",spiinfo[1].master);
printk("bdrate: %d.\n",spiinfo[1].bdrate);
printk("outenable: %d.\n",spiinfo[1].outenable);
printk("irqnum: %d.\n",spiinfo[1].irqnum);
printk("length: %d.\n",spiinfo[1].length);
*/
get_spi_reg(pdev, SPI_CTRL, &usreg);
printk("Ctrl reg:0x%x.\n", usreg);
break;
}
default:
return -EINVAL;
}
return 0;
}
/***********************************************************
*
* FUNCTION NAME :spi_poll
*
* INPUTS/OUTPUTS:
* in_inode - Description of openned file.
*
* RETURN:
* status of current device.
*
* FUNCTION(S) CALLED:
*
* GLOBAL VARIABLES REFERENCED: spiinfo
*
* GLOBAL VARIABLES MODIFIED: NIL
*
* DESCRIPTION: It is invoked when user use system call 'poll'
* to poll whether there are data coming or empty
* space to sending data.
*
* CAUTION:
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/
unsigned int spi_poll(struct file * filp, struct poll_table_struct * polltbl)
{
unsigned int status = 0;
spi_device_t *pdev = filp->private_data;
poll_wait(filp, pdev->tx_wq, polltbl);
poll_wait(filp, pdev->rx_wq, polltbl);
if(txq_isfull(pdev) != 1)
{
/* There is empty space in tx queue. */
status = POLLOUT;
}
if(rxq_isfull(pdev) != 0)
{
/* There is data in tx queue. */
status |= POLLIN;
}
return status;
}
/***********************************************************
*
* FUNCTION NAME :spi_fasync
*
* INPUTS/OUTPUTS:
* in_fd - File descriptor of openned file.
* in_filp - Description of openned file.
*
* RETURN:
*
* FUNCTION(S) CALLED:
*
* GLOBAL VARIABLES REFERENCED: spiinfo
*
* GLOBAL VARIABLES MODIFIED: NIL
*
* DESCRIPTION: It is invoked when user changes status of sync
* it resister a hook in system. When there is
* data coming, user program would get a signal.
*
* CAUTION:
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/
static int spi_fasync(int fd, struct file *filp, int on)
{
spi_device_t *pdev = filp->private_data;
return fasync_helper(fd, filp, on, &(pdev->fasyc));
}
/***********************************************************
*
* FUNCTION NAME :spi_read
*
* INPUTS/OUTPUTS:
* in_filp - Description of openned file.
* in_count - how many bytes user wants to get.
* out_buf - data would be write to this address.
*
* RETURN
* positive number: bytes read back
* -EINVIL When word size is set to 16, reading odd bytes.
* -EAGAIN When reading mode is set to non block and there is no rx data.
*
* FUNCTION(S) CALLED:
*
* GLOBAL VARIABLES REFERENCED: spiinfo
*
* GLOBAL VARIABLES MODIFIED: NIL
*
* DESCRIPTION: It is invoked when user call 'read' system call
* to read from system.
*
* CAUTION:
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/
static ssize_t spi_read (struct file *filp, char *buf, size_t count, loff_t *pos)
{
int rxqavail, readnum, i ,readpos, ierr;
spi_device_t *pdev = filp->private_data;
if(count <= 0)
return 0;
/* Wait for data available */
if(rxq_isfull(pdev) == 0)
{
if(pdev->nonblock)
return -EAGAIN;
else
{
ierr = wait_event_interruptible(*(pdev->rx_wq),
pdev->rxwpos != pdev->rxrpos);
if(ierr)
{
/* waiting is broken by a signal */
return ierr;
}
}
}
/* How many data available? */
rxqavail = (pdev->rxwpos + SPI_BUF_LEN - pdev->rxrpos)
% SPI_BUF_LEN;
if(pdev->length)
{
/* Since 16 bits format is select, odd count shouldn't be specified.*/
if((count % 2) == 1)
return -EINVAL;
/* Data is store in 16 bits, but reading need 8 bits */
if(rxqavail * 2 > count)
readnum = count;
else
readnum = rxqavail * 2;
readpos = pdev->rxrpos; /* Current available data position. */
/* 16 bits */
for(i = 0; i < readnum; i += 2)
{
buf[i] = (unsigned char)pdev->rxbuf[readpos];
buf[i+1] = (unsigned char)(pdev->rxbuf[readpos] >> 8);
readpos++;
if(readpos == SPI_BUF_LEN)
readpos = 0;
}
pdev->rxrpos = readpos;
}
else
{
/* Data stored in 8 bits */
if(rxqavail > count)
readnum = count;
else
readnum = rxqavail;
readpos = pdev->rxrpos; /* Current available data position. */
for(i = 0; i < readnum; i++)
{
buf[i] = (unsigned char)pdev->rxbuf[readpos];
readpos++;
if(readpos == SPI_BUF_LEN)
readpos = 0; /* It's a recycle buffer */
}
pdev->rxrpos = readpos;
}
return readnum;
}
/***********************************************************
*
* FUNCTION NAME :spi_write
*
* INPUTS/OUTPUTS:
* in_filp - Description of openned file.
* in_count - how many bytes user wants to send.
* out_buf - where we get those sending data.
*
* RETURN
* positive number: bytes sending out.
* 0: There is no data send out or parameter error.
* RETURN:
* >0 The actual count sending out.
* -EINVIL When word size is set to 16, writing odd bytes.
* -EAGAIN When sending mode is set to non block and there is no tx buffer.
*
* FUNCTION(S) CALLED:
*
* GLOBAL VARIABLES REFERENCED: spiinfo
*
* GLOBAL VARIABLES MODIFIED: NIL
*
* DESCRIPTION: It is invoked when user call 'read' system call
* to read from system.
*
* CAUTION:
*************************************************************
* MODIFICATION HISTORY :
*
* March 18, 2003 Fix the txqempty calculating bug.
**************************************************************/
static ssize_t spi_write (struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
int i,ierr;
int sendnum, txqempty;
unsigned short regdata;
unsigned int currpos, lastpos;
spi_device_t *pdev = filp->private_data;
if(count <= 0)
return 0;
if(txq_isfull(pdev) == 1)
{
if(pdev->nonblock)
return -EAGAIN;
else
{
/* Wait for tx space. This waiting would not be raced. */
ierr = wait_event_interruptible(*(pdev->tx_wq), txq_isfull(pdev) != 1);
if(ierr < 0)
return ierr;
}
}
/* This bug fixed. tx queue calculating wrong.
txqempty = (pdev->txrpos + SPI_BUF_LEN - pdev->txwpos) % SPI_BUF_LEN;
*/
txqempty = SPI_BUF_LEN - ((pdev->txwpos + SPI_BUF_LEN
- pdev->txrpos) % SPI_BUF_LEN + 1);
if(pdev->length)
{
/* 16 bits each word. */
if((count % 2) == 1)
return -EINVAL;
/* If not so many data, only write 'count' data. */
/* Buffer can contain SPI_BUF_LEN - 1 data. */
if(2 * txqempty > count)
sendnum = count;
else
sendnum = txqempty * 2;
currpos = pdev->txwpos;
for(i = 0; i < sendnum; i += 2)
{
pdev->txbuf[currpos] = (unsigned short)buf[i / 2];
pdev->txbuf[currpos] |= (unsigned short)(buf[1 + i / 2] << 8);
currpos++;
if(currpos == SPI_BUF_LEN)
currpos = 0;
}
pdev->txwpos = currpos;
}
else
{
/* 8 bits each word */
if(txqempty > count)
sendnum = count;
else
sendnum = txqempty;
currpos = pdev->txwpos;
for(i=0;i<sendnum;i++)
{
pdev->txbuf[currpos] = buf[i];
currpos++;
if(currpos == SPI_BUF_LEN)
currpos = 0;
}
pdev->txwpos = currpos;
}
if(txqempty == (SPI_BUF_LEN - 1))
{
get_spi_reg(pdev, SPI_STAU, ®data);
if(!(regdata & BIT_STU_SENDOVER))
{
/* Bad news! The last sending hasn't been over. */
/* Clear queue. */
pdev->txwpos = pdev->txrpos;
return -EAGAIN;
}
/* This is restart sending or first sending */
if(pdev->txrpos == (SPI_BUF_LEN - 1))
{
lastpos = (SPI_BUF_LEN - 1);
pdev->txrpos = 0;
}
else
{
lastpos = pdev->txrpos;
pdev->txrpos++;
}
set_spi_reg(pdev, SPI_TXBUFF, pdev->txbuf[lastpos]);
/* Open interrupt which maybe closed by IRQ for sending over.*/
enable_irq(pdev->irqnum);
}
return sendnum;
}
/***********************************************************
*
* FUNCTION NAME :spi_open
*
* INPUTS/OUTPUTS:
* in_inode - Description of openned file.
* in_filp - Description of openned file.
*
* RETURN
* 0: Open ok.
* -ENXIO No such device
*
* FUNCTION(S) CALLED:
*
* GLOBAL VARIABLES REFERENCED: spiinfo
*
* GLOBAL VARIABLES MODIFIED: NIL
*
* DESCRIPTION: It is invoked when user call 'open' system call
* to open spi device.
*
* CAUTION:
*************************************************************
* MODIFICATION HISTORY :
*
* March 20, 2003 Chang Junxiao, Change request_irq the 'NULL'
* is changed into infomation pointer. This is
* a bug, the former infomation pointer is changed
* into interrupt name.
**************************************************************/
static int spi_open (struct inode *inode, struct file *filp)
{
int idev = 0;
char intname[20];
int minor = MINOR (inode->i_rdev);
/* SPI 0 or SPI 1? */
if(minor == SPI0_MINOR)
{
idev = 0;
}
else if(minor == SPI1_MINOR)
{
idev = 1;
}
else
return -ENXIO;
if(spiinfo[idev].opened)
return -EMFILE;
/* Clear configuration information */