This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmcs9865.c
More file actions
executable file
·2128 lines (1849 loc) · 65 KB
/
mcs9865.c
File metadata and controls
executable file
·2128 lines (1849 loc) · 65 KB
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
/*
* linux/drivers/serial/9865.c
*
* Based on drivers/serial/8250.c by Russell King.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This code is modified to support moschip 9865 series serial devices
*/
#include <linux/version.h>
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,15)
#include <linux/config.h>
#endif
#if defined(CONFIG_SERIAL_9865_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
#define SUPPORT_SYSRQ
#endif
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/console.h>
#include <linux/sysrq.h>
#include <linux/mca.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/serial_reg.h>
#include <linux/serial_core.h>
#include <linux/serial.h>
#include <linux/nmi.h>
#include <linux/bitops.h>
#include <asm/byteorder.h>
#include <asm/io.h>
#include <asm/irq.h>
#include "mcs9865.h"
#define UART9865_NR 16
//All transactions are with memory mapped registers
#define MEM_AXS 1
/*
* Definitions for PCI support.
*/
#define FL_BASE_MASK 0x0007
#define FL_BASE0 0x0000
#define FL_BASE1 0x0001
#define FL_BASE2 0x0002
#define FL_BASE3 0x0003
#define FL_BASE4 0x0004
#define FL_GET_BASE(x) (x & FL_BASE_MASK)
#if 0
#define DEBUG(fmt...) printk(fmt)
#else
#define DEBUG(fmt...) do { } while (0)
#endif
struct uart_9865_port {
struct uart_port port;
spinlock_t lock_9865; //Per port lock
int tx_dma; //Variable to serialise the start_tx calls in dma mode
unsigned int dma_tx; //TX DMA enable or not
unsigned int dma_rx; //RX DMA enable or not
u8 ier; //Interrupt Enable Register
u8 lcr; //Line Control Register
u8 mcr; //Modem Control Register
u8 acr; //Additional Control Register
unsigned int capabilities; //port capabilities
int rxfifotrigger;
int txfifotrigger;
u32 dma_tx_cnt; //Amount of data to be DMA in TX
u32 dma_rx_cnt; //Amount of data to be DMA in RX
char * dma_tx_buf_v; //Virtual Address of DMA Buffer for TX
dma_addr_t dma_tx_buf_p; //Physical Address of DMA Buffer for TX
char * dma_rx_buf_v; //Virtual Address of DMA Buffer for RX
dma_addr_t dma_rx_buf_p; //Physical Address of DMA Buffer for TX
u32 part_done_recv_cnt; //RX DMA CIRC buffer Read index
int rx_dma_done_cnt;
int uart_mode; //SERIAL TYPE
int flow_control; //Flow control is enabled or not
int flow_ctrl_type; //Type of Flow control
u8 x_on; //X-ON Character
u8 x_off; //X-OFF Character
u32 ser_dcr_din_reg; //Device control register
u32 ser_ven_reg; //Vendor register
};
static struct uart_9865_port serial9865_ports[UART9865_NR];
static int test_mode=0;
struct uart_9865_contxt{
int rx_dma_en;
//0-I/O mode of RX
//1 -DMA mode of RX
int tx_dma_en;
//0-I/O mode of TX
//1 -DMA mode of TX
int uart_mode;
//MCS9865_RS232_MODE
//MCS9865_RS422_MODE
//MCS9865_RS485_HALF_DUPLEX
//MCS9865_RS485_FULL_DUPLEX
int en_flow_control;
//0 - No H/W Flow Control
//1 - H/W Flow Control
int flow_ctrl_type;
//MCS9865_DTR_DSR_HW_FLOWCONTROL
//MCS9865_XON_XOFF_HW_FLOWCONTROL
//MCS9865_RTS_CTS_HW_FLOWCONTROL
int rxfifotrigger; //0-127
int txfifotrigger; //0-127
int x_on;
int x_off;
};
static struct uart_9865_contxt uart_9865_contxts[] = {
//Port 0
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger = 64,
.txfifotrigger = 64,
.x_on = SERIAL_DEF_XON,
.x_off = SERIAL_DEF_XOFF,
},
//Port 1
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger = 64,
.txfifotrigger = 64,
.x_on= SERIAL_DEF_XON,
.x_off= SERIAL_DEF_XOFF,
},
//Port 2
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
//Port 3
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
//Port 4
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
//Port 5
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
//Port 6
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
//Port 7
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
//Port 8
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
//Port 9
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
//Port 10
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
//Port 11
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
//Port 12
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
//Port 13
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
//Port 14
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
//Port 15
{
.rx_dma_en = 0,
.tx_dma_en = 0,
.uart_mode = MCS9865_RS232_MODE,
.en_flow_control= 0,
.flow_ctrl_type = MCS9865_XON_XOFF_HW_FLOWCONTROL,
.rxfifotrigger=64,
.txfifotrigger=64,
.x_on=SERIAL_DEF_XON,
.x_off=SERIAL_DEF_XOFF,
},
};
/*
* Here we define the default xmit fifo size used for each type of UART.
*/
static const struct serial9865_config uart_config[] = {
[PORT_UNKNOWN] = {
.fifo_size = 1,
.tx_loadsz = 1,
},
[PORT_16450] = {
.fifo_size = 1,
.tx_loadsz = 1,
},
[PORT_16550] = {
.fifo_size = 16,
.tx_loadsz = 14,
},
[PORT_16550A] = {
.fifo_size = 256,
.tx_loadsz = 128,
.fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01,
.flags = UART_CAP_FIFO,
},
[PORT_16650] = {
.fifo_size = 128,
.tx_loadsz = 128,
.flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
},
[PORT_16750] = {
.fifo_size = 64,
.tx_loadsz = 64,
.fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |UART_FCR7_64BYTE,
.flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
},
[PORT_16850] = {
.fifo_size = 128,
.tx_loadsz = 128,
.fcr = UART_FCR_ENABLE_FIFO,
.flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
},
[PORT_16C950] = {
.fifo_size = 128,
.tx_loadsz = 128,
.fcr = UART_FCR_ENABLE_FIFO,
.flags = UART_CAP_FIFO,
},
[PORT_ENHANC]= {
.fifo_size = 256,
.tx_loadsz = 128,
.fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | UART_FCR_R_TRIG_01,
.flags = UART_CAP_FIFO,
},
};
//helper function for IO type read
static _INLINE_ u8 serial_in(struct uart_9865_port *up, int offset)
{
#if MEM_AXS
u8 tmp1;
tmp1=readl(up->port.membase+0x280+(offset*4));
return tmp1;
#else
return inb(up->port.iobase + offset);
#endif
}
//helper function for IO type write
static _INLINE_ void serial_out(struct uart_9865_port *up, int offset, int value)
{
#if MEM_AXS
writel(value, up->port.membase+0x280+(offset*4));
#else
outb(value, up->port.iobase + offset);
#endif
}
//Helper function to write to index control register
static void serial_icr_write(struct uart_9865_port *up, int offset, int value)
{
DEBUG("UART_LCR=0x%x\n",serial_in(up,UART_LCR));
serial_out(up, UART_SCR, offset);
serial_out(up, UART_ICR, value);
}
//Helper function to read from index control register
static unsigned int serial_icr_read(struct uart_9865_port *up, int offset)
{
unsigned int value;
serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
serial_out(up, UART_SCR, offset);
value = inb(up->port.iobase+UART_ICR);
serial_icr_write(up, UART_ACR, up->acr);
return value;
}
//Helper function to set the 950 mode
void setserial_ENHANC_mode(struct uart_9865_port *up)
{
u8 lcr,efr;
DEBUG("In %s---------------------------------------START\n",__FUNCTION__);
lcr=serial_in(up,UART_LCR);
serial_out(up, UART_LCR, 0xBF);
efr=serial_in(up,UART_EFR);
efr |= UART_EFR_ECB;
serial_out(up, UART_EFR,efr);
serial_out(up, UART_LCR, lcr);
DEBUG("In %s---------------------------------------END\n",__FUNCTION__);
}
// Helper function to clear the FIFO
static inline void serial9865_clear_fifos(struct uart_9865_port *p)
{
if (p->capabilities & UART_CAP_FIFO) {
serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
serial_out(p, UART_FCR, 0);
}
}
//Helper function to set the the UART to sleep mode
static inline void serial9865_set_sleep(struct uart_9865_port *p, int sleep)
{
DEBUG("In %s ---------------------------------------START\n",__FUNCTION__);
if (p->capabilities & UART_CAP_SLEEP) {
if (p->capabilities & UART_CAP_EFR) {
serial_out(p, UART_LCR, 0xBF);
serial_out(p, UART_EFR, UART_EFR_ECB);
serial_out(p, UART_LCR, 0);
}
serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
if (p->capabilities & UART_CAP_EFR) {
serial_out(p, UART_LCR, 0xBF);
serial_out(p, UART_EFR, 0);
serial_out(p, UART_LCR, 0);
}
}
DEBUG("In %s ---------------------------------------END\n",__FUNCTION__);
}
//Member function of the port operations to stop the data transfer
#if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,11)
static void serial9865_stop_tx(struct uart_port *port, unsigned int tty_stop)
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
static void serial9865_stop_tx(struct uart_port *port)
#endif
{
struct uart_9865_port *up = &serial9865_ports[port->line];
DEBUG("In %s ---------------------------------------START\n",__FUNCTION__);
if(up->dma_tx){
up->tx_dma=0;
}else{ //IO mode
if (up->ier & UART_IER_THRI) {
up->ier &= ~UART_IER_THRI;
serial_out(up, UART_IER, up->ier);
}
}
DEBUG("In %s ---------------------------------------END\n",__FUNCTION__);
}
//Member function of the port operations to start the data transfer
#if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,11)
static void serial9865_start_tx(struct uart_port *port, unsigned int tty_start)
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
static void serial9865_start_tx(struct uart_port *port)
#endif
{
struct uart_9865_port *up = &serial9865_ports[port->line];
struct circ_buf *xmit = &up->port.state->xmit;
u32 length=0,len2end;
int tail,head;
DEBUG("In %s ---------------------------------------START\n",__FUNCTION__);
if(up->dma_tx && (up->tx_dma == 0)){
DEBUG(" I WAS IN DMA OF START_TX\n");
//CALCULATING THE AMOUNT OF DATA AVAILABLE FOR THE NEXT TRANSFER
//AND COPYING THE DATA TO THE DMA BUFFER
length = uart_circ_chars_pending(xmit);
head=xmit->head;
tail=xmit->tail;
len2end = CIRC_CNT_TO_END(head, tail, UART_XMIT_SIZE);
DEBUG("In %s -------------------xmit->tail=%d, xmit->head=%d,length=%d,length2end=%d\n",__FUNCTION__,tail,head,length,len2end);
if(tail < head){
if(length <= DMA_TX_BUFFER_SZ){
DEBUG("In %s normal circ buffer\n",__FUNCTION__);
memcpy(up->dma_tx_buf_v,&xmit->buf[tail],length); //xmit->buf + xmit->tail
up->dma_tx_cnt = length;
}else{
memcpy(up->dma_tx_buf_v,&xmit->buf[tail],DMA_TX_BUFFER_SZ);
up->dma_tx_cnt = DMA_TX_BUFFER_SZ;
}
}else{
if(length <= DMA_TX_BUFFER_SZ){
DEBUG("In %s 2 mode circ buffer\n",__FUNCTION__);
memcpy(up->dma_tx_buf_v, &xmit->buf[tail], len2end);
memcpy(up->dma_tx_buf_v+len2end, xmit->buf, head);
up->dma_tx_cnt = length;
}else{
if(len2end <= DMA_TX_BUFFER_SZ){
memcpy(up->dma_tx_buf_v,&xmit->buf[tail],len2end);
memcpy(up->dma_tx_buf_v+len2end, xmit->buf, DMA_TX_BUFFER_SZ-len2end);
up->dma_tx_cnt = len2end;
}else{
memcpy(up->dma_tx_buf_v,&xmit->buf[tail],DMA_TX_BUFFER_SZ);
up->dma_tx_cnt = DMA_TX_BUFFER_SZ;
}
}
}
up->tx_dma++; //variable to serialise the DMA tx calls
DEBUG("In %s up->dma_tx_cnt=%d\n",__FUNCTION__,up->dma_tx_cnt);
DEBUG("IN %s length=%d and data in dma->buf is: %s\n",__FUNCTION__,length,up->dma_tx_buf_v);
spin_lock(&up->lock_9865);
writel(up->dma_tx_buf_p,up->port.membase + REG_TX_DMA_START_ADDRESS_LOW);
writel(0,up->port.membase + REG_TX_DMA_START_ADDRESS_HIGH);
writel(up->dma_tx_cnt,up->port.membase+REG_TX_DMA_LENGTH);
writel(TX_DMA_START_BIT, up->port.membase + REG_TX_DMA_START);
spin_unlock(&up->lock_9865);
DEBUG("In %s programmed registers\n",__FUNCTION__);
}else{
if (!(up->ier & UART_IER_THRI)) {
up->ier |= UART_IER_THRI;
serial_out(up, UART_IER, up->ier);
}
}
DEBUG("In %s ---------------------------------------END\n",__FUNCTION__);
}
//Member function of the port operations to stop receiving the data
static void serial9865_stop_rx(struct uart_port *port)
{
struct uart_9865_port *up = &serial9865_ports[port->line];
u32 value=0;
DEBUG("In %s ---------------------------------------START\n",__FUNCTION__);
if(up->dma_rx){
value |= RX_DMA_STOP_BIT;
writel(value, up->port.membase + REG_RX_DMA_STOP);
}else{
up->ier &= ~UART_IER_RLSI;
up->port.read_status_mask &= ~UART_LSR_DR;
serial_out(up, UART_IER, up->ier);
}
DEBUG("In %s ---------------------------------------END\n",__FUNCTION__);
}
//Member function of the port operations to enable modem status change interrupt
static void serial9865_enable_ms(struct uart_port *port)
{
struct uart_9865_port *up = &serial9865_ports[port->line];
DEBUG("In %s --------------------------------------- START\n",__FUNCTION__);
up->ier |= UART_IER_MSI;
serial_out(up, UART_IER, up->ier);
}
//Function to check modem statuss
static _INLINE_ void check_modem_status(struct uart_9865_port *up)
{
u8 status;
DEBUG("In %s -------------------- START\n",__FUNCTION__);
status = serial_in(up, UART_MSR);
if ((status & UART_MSR_ANY_DELTA) == 0)
return;
if (status & UART_MSR_TERI)
up->port.icount.rng++;
if (status & UART_MSR_DDSR)
up->port.icount.dsr++;
if (status & UART_MSR_DDCD)
uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
if (status & UART_MSR_DCTS)
uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
wake_up_interruptible(&up->port.state->port.delta_msr_wait);
DEBUG("In %s -------------------- END\n",__FUNCTION__);
}
//Helper function used in ISR to receive the the charecters from the UART
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
static _INLINE_ void receive_chars(struct uart_9865_port *up, u8 *status)
#else
static _INLINE_ void receive_chars(struct uart_9865_port *up, u8 *status, struct pt_regs *regs)
#endif
{
struct tty_struct *tty = up->port.state->port.tty;
u8 ch,lsr = *status;
int max_count = 256;
unsigned int flag;
DEBUG("In %s ---------------------------------------START\n",__FUNCTION__);
do {
/* The following is not allowed by the tty layer and
unsafe. It should be fixed ASAP */
#if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,11)
if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
if (tty->low_latency) {
spin_unlock(&up->port.lock);
tty_flip_buffer_push(tty);
spin_lock(&up->port.lock);
}
/*
* If this failed then we will throw away the
* bytes but must do so to clear interrupts
*/
}
#endif
ch = serial_in(up, UART_RX);
flag = TTY_NORMAL;
up->port.icount.rx++;
if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |UART_LSR_FE | UART_LSR_OE))) {
/*
* For statistics only
*/
if (lsr & UART_LSR_BI) {
lsr &= ~(UART_LSR_FE | UART_LSR_PE);
up->port.icount.brk++;
/*
* We do the SysRQ and SAK checking
* here because otherwise the break
* may get masked by ignore_status_mask
* or read_status_mask.
*/
if (uart_handle_break(&up->port))
goto ignore_char;
} else if (lsr & UART_LSR_PE)
up->port.icount.parity++;
else if (lsr & UART_LSR_FE)
up->port.icount.frame++;
if (lsr & UART_LSR_OE)
up->port.icount.overrun++;
/*
* Mask off conditions which should be ignored.
*/
lsr &= up->port.read_status_mask;
if (lsr & UART_LSR_BI) {
DEBUG("handling break....");
flag = TTY_BREAK;
}else if (lsr & UART_LSR_PE)
flag = TTY_PARITY;
else if (lsr & UART_LSR_FE)
flag = TTY_FRAME;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
if (uart_handle_sysrq_char(&up->port, ch, regs))
goto ignore_char;
#else
if (uart_handle_sysrq_char(&up->port, ch))
goto ignore_char;
#endif
uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
ignore_char:
lsr = serial_in(up, UART_LSR);
} while ((lsr & UART_LSR_DR) && (max_count-- > 0));
spin_unlock(&up->port.lock);
tty_flip_buffer_push(tty);
spin_lock(&up->port.lock);
*status = lsr;
DEBUG("In %s -------------------------------------END\n",__FUNCTION__);
}
//Helper function used in ISR to send the data to the UART
static _INLINE_ void transmit_chars(struct uart_9865_port *up)
{
struct circ_buf *xmit = &up->port.state->xmit;
int count;
DEBUG("In %s ---------------------------------------START\n",__FUNCTION__);
if (up->port.x_char) {
serial_out(up, UART_TX, up->port.x_char);
up->port.icount.tx++;
up->port.x_char = 0;
return;
}
if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
#if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,11)
serial9865_stop_tx(&up->port, 0);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
serial9865_stop_tx(&up->port);
#endif
return;
}
count = uart_config[up->port.type].tx_loadsz;
DEBUG("In %s-----------up->port.type=%d,tx_loadsz=%d\n",__FUNCTION__,up->port.type,count);
do {
serial_out(up, UART_TX, xmit->buf[xmit->tail]);
xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
up->port.icount.tx++;
if (uart_circ_empty(xmit))
break;
} while (--count > 0);
if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
uart_write_wakeup(&up->port);
if (uart_circ_empty(xmit)){
#if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,11)
serial9865_stop_tx(&up->port, 0);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
serial9865_stop_tx(&up->port);
#endif
}
DEBUG("In %s --------------------------------------2END\n",__FUNCTION__);
}
//Helper function to stop the characters transmission in DMA mode
static void transmit_chars_dma_stop_done(struct uart_9865_port * up)
{
struct circ_buf *xmit = &up->port.state->xmit;
long int transferred;
DEBUG("In %s ---------------------------------------START\n",__FUNCTION__);
//UPDATING THE TRANSMIT FIFO WITH THE AMOUNT OF DATA TRANSFERRED
transferred=readl(up->port.membase+REG_TX_BYTES_TRANSFERRED);
xmit->tail=((xmit->tail)+transferred) & (UART_XMIT_SIZE-1);
up->port.icount.tx += transferred;
up->tx_dma=0;
memset(up->dma_tx_buf_v,0,DMA_TX_BUFFER_SZ);
DEBUG("In %s ---------------------------------------END\n",__FUNCTION__);
}
//Helper function to do the necessary action upon the successful completion of data transfer in DMA mode
static int transmit_chars_dma_done(struct uart_9865_port * up)
{
struct circ_buf *xmit = &(up->port.state->xmit);
int length,tobe_transferred,transferred,len2end;
DEBUG("In %s ---------------------------------------START\n",__FUNCTION__);
//UPDATING THE xmit FIFO WITH THE AMOUNT OF DATA TRANSFERRED
tobe_transferred=readl(up->port.membase+REG_TX_BYTES_TRANSFERRED);
transferred = (up->dma_tx_cnt - tobe_transferred);
DEBUG("In %s -------------transferred=%d--------------------------START\n",__FUNCTION__,transferred);
xmit->tail = ((xmit->tail) + transferred) & (UART_XMIT_SIZE-1);
up->port.icount.tx += transferred;
length = uart_circ_chars_pending(xmit);
DEBUG("In %s circ_buf lenght=%d after\n",__FUNCTION__,length);
memset(up->dma_tx_buf_v,0,DMA_TX_BUFFER_SZ);
DEBUG("In %s up->dma_tx_buf_v=0x%x ---------------------------------------START\n",__FUNCTION__,(unsigned int)up->dma_tx_buf_v);
if (uart_circ_empty(xmit) ||uart_tx_stopped(&up->port)){
up->tx_dma=0;
if (length < WAKEUP_CHARS)
uart_write_wakeup(&up->port);
return 0;
}
//CALCULATING THE AMOUNT OF DATA AVAILABLE FOR THE NEXT TRANSFER
//AND COPYING THE DATA TO THE DMA BUFFER
length = uart_circ_chars_pending(xmit);
len2end = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
DEBUG("In %s -------------------xmit->tail=%d, xmit->head=%d,length=%d,length2end=%d\n",__FUNCTION__,xmit->tail,xmit->head,length,len2end);
if(xmit->tail < xmit->head){
if(length <= DMA_TX_BUFFER_SZ){
memcpy(up->dma_tx_buf_v,&xmit->buf[xmit->tail],length); //xmit->buf + xmit->tail
up->dma_tx_cnt = length;
DEBUG("In %s Normal mode\n",__FUNCTION__);
}else{
memcpy(up->dma_tx_buf_v,&xmit->buf[xmit->tail],DMA_TX_BUFFER_SZ);
up->dma_tx_cnt = DMA_TX_BUFFER_SZ;
}
}else{
if(length <= DMA_TX_BUFFER_SZ){
DEBUG("In %s 2nd mode\n",__FUNCTION__);
memcpy(up->dma_tx_buf_v,&xmit->buf[xmit->tail],len2end);
memcpy(up->dma_tx_buf_v+len2end,xmit->buf,xmit->head);
up->dma_tx_cnt = length;
}else{
if(len2end <= DMA_TX_BUFFER_SZ){
memcpy(up->dma_tx_buf_v,&xmit->buf[xmit->tail],len2end);
memcpy(up->dma_tx_buf_v+len2end, xmit->buf, DMA_TX_BUFFER_SZ-len2end);
up->dma_tx_cnt = len2end;
}else{
memcpy(up->dma_tx_buf_v,&xmit->buf[xmit->tail],DMA_TX_BUFFER_SZ);
up->dma_tx_cnt = DMA_TX_BUFFER_SZ;
}
}
}
DEBUG("In %s length=%d\n",__FUNCTION__,length);
//INITIATING THE NEXT TRANSFER
//Writing the source address to the TX DMA
writel(up->dma_tx_buf_p,up->port.membase+REG_TX_DMA_START_ADDRESS_LOW);
//Writing the source address to the TX DMA
writel(0,up->port.membase+REG_TX_DMA_START_ADDRESS_HIGH);
//Writing the length of data to the TX DMA Length register
writel(length,up->port.membase+REG_TX_DMA_LENGTH);
//Start the DMA data transfer
writel(TX_DMA_START_BIT,up->port.membase+REG_TX_DMA_START);
// Requesting more data to send out from the TTY layer to the driver
if (length < WAKEUP_CHARS)
uart_write_wakeup(&up->port);
DEBUG("In %s ---------------------------------------END\n",__FUNCTION__);
return 0;
}
//Helper function to do the necessary action upon the successful completion of data receive in DMA mode
static void receive_chars_dma_done(struct uart_9865_port * up, int iirg)
{
struct tty_struct *tty = up->port.state->port.tty;
int i,rxdma_done=0;
u16 received_bytes;
u32 need2recv;
u8 status = serial_in(up, UART_LSR);
DEBUG("In %s ---------iirg=0x%x------------------------------START\n",__FUNCTION__,iirg);
//checking for the flip buffer size and asking to clear it upon some threshold
#if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,11)
if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
if (tty->low_latency) {
spin_unlock(&up->port.lock);
tty_flip_buffer_push(tty);
spin_lock(&up->port.lock);
}
}
#endif
if ((iirg & SPINTR_RXDMA_PARTDONE) || (iirg & SPINTR_RXDMA_DONE) || (iirg & SPINTR_RXDMA_ABORT_DONE)){
//checking for the number of bytes received
need2recv=readl(up->port.membase + REG_RX_BYTES_NEED_TO_RECV);
DEBUG("In %s --------Receive DMA Part Done need2recv=%d\n",__FUNCTION__,need2recv);
if (iirg & SPINTR_RXDMA_ABORT_DONE){
DEBUG("In %s --------Receive DMA ABORT Done\n",__FUNCTION__);
DEBUG("up->rx_dma_done_cnt=%d\n",up->rx_dma_done_cnt);
//Reinitialise the DMA for the rest of the characters in the previous instruction
writel((up->dma_rx_buf_p+(DMA_RX_SZ * (up->rx_dma_done_cnt-1))),up->port.membase+REG_RX_DMA_START_ADDRESS_LOW);
writel(0,up->port.membase+REG_RX_DMA_START_ADDRESS_HIGH);
writel(need2recv,up->port.membase+REG_RX_DMA_LENGTH);
writel(RX_DMA_START_BIT,up->port.membase+REG_RX_DMA_START);
DEBUG("REG_RX_DMA_START_ADDRESS_LOW:0x%x\n",readl(up->port.membase+REG_RX_DMA_START_ADDRESS_LOW));
DEBUG("REG_RX_DMA_START_ADDRESS_HIGH:0x%x\n",readl(up->port.membase+REG_RX_DMA_START_ADDRESS_HIGH));
DEBUG("REG_RX_DMA_LENGTH:0x%x\n",readl(up->port.membase+REG_RX_DMA_LENGTH));
}
if (iirg & SPINTR_RXDMA_DONE){
DEBUG("In %s --------Receive DMA Done\n",__FUNCTION__);
DEBUG("In %s --------up->rx_dma_done_cnt=%d\n",__FUNCTION__,up->rx_dma_done_cnt);
//Reinitialise the DMA
writel((up->dma_rx_buf_p+(DMA_RX_SZ * up->rx_dma_done_cnt)),up->port.membase+REG_RX_DMA_START_ADDRESS_LOW);
writel(0,up->port.membase+REG_RX_DMA_START_ADDRESS_HIGH);
writel(DMA_RX_SZ,up->port.membase+REG_RX_DMA_LENGTH);
writel(RX_DMA_START_BIT,up->port.membase+REG_RX_DMA_START);
DEBUG("REG_RX_DMA_START_ADDRESS_LOW:0x%x\n",readl(up->port.membase+REG_RX_DMA_START_ADDRESS_LOW));
DEBUG("REG_RX_DMA_START_ADDRESS_HIGH:0x%x\n",readl(up->port.membase+REG_RX_DMA_START_ADDRESS_HIGH));
DEBUG("REG_RX_DMA_LENGTH:0x%x\n",readl(up->port.membase+REG_RX_DMA_LENGTH));
rxdma_done=1;
}
received_bytes=((DMA_RX_SZ * (up->rx_dma_done_cnt))-(need2recv + up->part_done_recv_cnt));
if(rxdma_done){
up->rx_dma_done_cnt++;
rxdma_done=0;
}
if (up->rx_dma_done_cnt == (DMA_RX_BUFFER_SZ/DMA_RX_SZ))
up->rx_dma_done_cnt=0;
DEBUG("In %s --------Receive DMA Part Done received_bytes=%d\n part_recv_cnt=%d\n",__FUNCTION__,received_bytes,up->part_done_recv_cnt);
//copiying the recived bytes to the TTY layers flip buffer
if (tty){
DEBUG("received_bytes=%d and up->part_done_recv_cnt=%d\n",received_bytes,up->part_done_recv_cnt);
for (i = 1; i <= received_bytes; i++){
/* if we insert more than TTY_FLIPBUF_SIZE characters, tty layer will drop them. */
#if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,11)
if(tty->flip.count >= TTY_FLIPBUF_SIZE){
tty_flip_buffer_push(tty);
}
#endif
/* this doesn't actually push the data through unless tty->low_latency is set */
uart_insert_char(&up->port, status, UART_LSR_OE, up->dma_rx_buf_v[ up->part_done_recv_cnt], TTY_NORMAL);
up->part_done_recv_cnt++;
DEBUG("char=%c \n",up->dma_rx_buf_v[up->part_done_recv_cnt]);
if(up->part_done_recv_cnt == DMA_RX_BUFFER_SZ)
up->part_done_recv_cnt = 0;
}
tty_flip_buffer_push(tty);
}
up->port.icount.rx += received_bytes;
#if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,11)
if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
if (tty->low_latency) {
spin_unlock(&up->port.lock);
tty_flip_buffer_push(tty);
spin_lock(&up->port.lock);
}
}
#endif
}
}
//This handles the interrupt from a port in IO mode.
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
static inline void serial9865_handle_port(struct uart_9865_port *up)
#else
static inline void serial9865_handle_port(struct uart_9865_port *up, struct pt_regs *regs)
#endif
{
u8 status = serial_in(up, UART_LSR);
struct tty_struct *tty=up->port.state->port.tty;
DEBUG("In %s ---------------------------------------START\n",__FUNCTION__);
DEBUG("UART_LSR = %x...", status);
if((status & UART_LSR_DR) && !up->dma_rx){
DEBUG("RECEIVE_CHARS\n");
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
receive_chars(up, &status);
#else
receive_chars(up, &status, regs);
#endif
}
check_modem_status(up);
if ((status & UART_LSR_THRE) && !up->dma_tx){
DEBUG("TRANSMIT_CHARS\n");
transmit_chars(up);
}
if(up->dma_rx){
if (status & (UART_LSR_BI | UART_LSR_PE |UART_LSR_FE | UART_LSR_OE)){
//For statistics only
if (status & UART_LSR_BI) {
status &= ~(UART_LSR_FE | UART_LSR_PE);
up->port.icount.brk++;
/*
* We do the SysRQ and SAK checking
* here because otherwise the break
* may get masked by ignore_status_mask
* or read_status_mask.
*/
if (uart_handle_break(&up->port))
return;
}else if (status & UART_LSR_PE)
up->port.icount.parity++;
else if (status & UART_LSR_FE)
up->port.icount.frame++;
if (status & UART_LSR_OE)
up->port.icount.overrun++;
//Mask off conditions which should be ignored.