forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioc3_serial.c
2199 lines (1905 loc) · 57.5 KB
/
ioc3_serial.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
/*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file "COPYING" in the main directory of this archive
* for more details.
*
* Copyright (C) 2005 Silicon Graphics, Inc. All Rights Reserved.
*/
/*
* This file contains a module version of the ioc3 serial driver. This
* includes all the support functions needed (support functions, etc.)
* and the serial driver itself.
*/
#include <linux/errno.h>
#include <linux/tty.h>
#include <linux/serial.h>
#include <linux/circ_buf.h>
#include <linux/serial_reg.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/serial_core.h>
#include <linux/ioc3.h>
#include <linux/slab.h>
/*
* Interesting things about the ioc3
*/
#define LOGICAL_PORTS 2 /* rs232(0) and rs422(1) */
#define PORTS_PER_CARD 2
#define LOGICAL_PORTS_PER_CARD (PORTS_PER_CARD * LOGICAL_PORTS)
#define MAX_CARDS 8
#define MAX_LOGICAL_PORTS (LOGICAL_PORTS_PER_CARD * MAX_CARDS)
/* determine given the sio_ir what port it applies to */
#define GET_PORT_FROM_SIO_IR(_x) (_x & SIO_IR_SA) ? 0 : 1
/*
* we have 2 logical ports (rs232, rs422) for each physical port
* evens are rs232, odds are rs422
*/
#define GET_PHYSICAL_PORT(_x) ((_x) >> 1)
#define GET_LOGICAL_PORT(_x) ((_x) & 1)
#define IS_PHYSICAL_PORT(_x) !((_x) & 1)
#define IS_RS232(_x) !((_x) & 1)
static unsigned int Num_of_ioc3_cards;
static unsigned int Submodule_slot;
/* defining this will get you LOTS of great debug info */
//#define DEBUG_INTERRUPTS
#define DPRINT_CONFIG(_x...) ;
//#define DPRINT_CONFIG(_x...) printk _x
#define NOT_PROGRESS() ;
//#define NOT_PROGRESS() printk("%s : fails %d\n", __func__, __LINE__)
/* number of characters we want to transmit to the lower level at a time */
#define MAX_CHARS 256
#define FIFO_SIZE (MAX_CHARS-1) /* it's a uchar */
/* Device name we're using */
#define DEVICE_NAME "ttySIOC"
#define DEVICE_MAJOR 204
#define DEVICE_MINOR 116
/* flags for next_char_state */
#define NCS_BREAK 0x1
#define NCS_PARITY 0x2
#define NCS_FRAMING 0x4
#define NCS_OVERRUN 0x8
/* cause we need SOME parameters ... */
#define MIN_BAUD_SUPPORTED 1200
#define MAX_BAUD_SUPPORTED 115200
/* protocol types supported */
#define PROTO_RS232 0
#define PROTO_RS422 1
/* Notification types */
#define N_DATA_READY 0x01
#define N_OUTPUT_LOWAT 0x02
#define N_BREAK 0x04
#define N_PARITY_ERROR 0x08
#define N_FRAMING_ERROR 0x10
#define N_OVERRUN_ERROR 0x20
#define N_DDCD 0x40
#define N_DCTS 0x80
#define N_ALL_INPUT (N_DATA_READY | N_BREAK \
| N_PARITY_ERROR | N_FRAMING_ERROR \
| N_OVERRUN_ERROR | N_DDCD | N_DCTS)
#define N_ALL_OUTPUT N_OUTPUT_LOWAT
#define N_ALL_ERRORS (N_PARITY_ERROR | N_FRAMING_ERROR \
| N_OVERRUN_ERROR)
#define N_ALL (N_DATA_READY | N_OUTPUT_LOWAT | N_BREAK \
| N_PARITY_ERROR | N_FRAMING_ERROR \
| N_OVERRUN_ERROR | N_DDCD | N_DCTS)
#define SER_CLK_SPEED(prediv) ((22000000 << 1) / prediv)
#define SER_DIVISOR(x, clk) (((clk) + (x) * 8) / ((x) * 16))
#define DIVISOR_TO_BAUD(div, clk) ((clk) / 16 / (div))
/* Some masks */
#define LCR_MASK_BITS_CHAR (UART_LCR_WLEN5 | UART_LCR_WLEN6 \
| UART_LCR_WLEN7 | UART_LCR_WLEN8)
#define LCR_MASK_STOP_BITS (UART_LCR_STOP)
#define PENDING(_a, _p) (readl(&(_p)->vma->sio_ir) & (_a)->ic_enable)
#define RING_BUF_SIZE 4096
#define BUF_SIZE_BIT SBBR_L_SIZE
#define PROD_CONS_MASK PROD_CONS_PTR_4K
#define TOTAL_RING_BUF_SIZE (RING_BUF_SIZE * 4)
/* driver specific - one per card */
struct ioc3_card {
struct {
/* uart ports are allocated here */
struct uart_port icp_uart_port[LOGICAL_PORTS];
/* the ioc3_port used for this port */
struct ioc3_port *icp_port;
} ic_port[PORTS_PER_CARD];
/* currently enabled interrupts */
uint32_t ic_enable;
};
/* Local port info for each IOC3 serial port */
struct ioc3_port {
/* handy reference material */
struct uart_port *ip_port;
struct ioc3_card *ip_card;
struct ioc3_driver_data *ip_idd;
struct ioc3_submodule *ip_is;
/* pci mem addresses for this port */
struct ioc3_serialregs __iomem *ip_serial_regs;
struct ioc3_uartregs __iomem *ip_uart_regs;
/* Ring buffer page for this port */
dma_addr_t ip_dma_ringbuf;
/* vaddr of ring buffer */
struct ring_buffer *ip_cpu_ringbuf;
/* Rings for this port */
struct ring *ip_inring;
struct ring *ip_outring;
/* Hook to port specific values */
struct port_hooks *ip_hooks;
spinlock_t ip_lock;
/* Various rx/tx parameters */
int ip_baud;
int ip_tx_lowat;
int ip_rx_timeout;
/* Copy of notification bits */
int ip_notify;
/* Shadow copies of various registers so we don't need to PIO
* read them constantly
*/
uint32_t ip_sscr;
uint32_t ip_tx_prod;
uint32_t ip_rx_cons;
unsigned char ip_flags;
};
/* tx low water mark. We need to notify the driver whenever tx is getting
* close to empty so it can refill the tx buffer and keep things going.
* Let's assume that if we interrupt 1 ms before the tx goes idle, we'll
* have no trouble getting in more chars in time (I certainly hope so).
*/
#define TX_LOWAT_LATENCY 1000
#define TX_LOWAT_HZ (1000000 / TX_LOWAT_LATENCY)
#define TX_LOWAT_CHARS(baud) (baud / 10 / TX_LOWAT_HZ)
/* Flags per port */
#define INPUT_HIGH 0x01
/* used to signify that we have turned off the rx_high
* temporarily - we need to drain the fifo and don't
* want to get blasted with interrupts.
*/
#define DCD_ON 0x02
/* DCD state is on */
#define LOWAT_WRITTEN 0x04
#define READ_ABORTED 0x08
/* the read was aborted - used to avaoid infinate looping
* in the interrupt handler
*/
#define INPUT_ENABLE 0x10
/* Since each port has different register offsets and bitmasks
* for everything, we'll store those that we need in tables so we
* don't have to be constantly checking the port we are dealing with.
*/
struct port_hooks {
uint32_t intr_delta_dcd;
uint32_t intr_delta_cts;
uint32_t intr_tx_mt;
uint32_t intr_rx_timer;
uint32_t intr_rx_high;
uint32_t intr_tx_explicit;
uint32_t intr_clear;
uint32_t intr_all;
char rs422_select_pin;
};
static struct port_hooks hooks_array[PORTS_PER_CARD] = {
/* values for port A */
{
.intr_delta_dcd = SIO_IR_SA_DELTA_DCD,
.intr_delta_cts = SIO_IR_SA_DELTA_CTS,
.intr_tx_mt = SIO_IR_SA_TX_MT,
.intr_rx_timer = SIO_IR_SA_RX_TIMER,
.intr_rx_high = SIO_IR_SA_RX_HIGH,
.intr_tx_explicit = SIO_IR_SA_TX_EXPLICIT,
.intr_clear = (SIO_IR_SA_TX_MT | SIO_IR_SA_RX_FULL
| SIO_IR_SA_RX_HIGH
| SIO_IR_SA_RX_TIMER
| SIO_IR_SA_DELTA_DCD
| SIO_IR_SA_DELTA_CTS
| SIO_IR_SA_INT
| SIO_IR_SA_TX_EXPLICIT
| SIO_IR_SA_MEMERR),
.intr_all = SIO_IR_SA,
.rs422_select_pin = GPPR_UARTA_MODESEL_PIN,
},
/* values for port B */
{
.intr_delta_dcd = SIO_IR_SB_DELTA_DCD,
.intr_delta_cts = SIO_IR_SB_DELTA_CTS,
.intr_tx_mt = SIO_IR_SB_TX_MT,
.intr_rx_timer = SIO_IR_SB_RX_TIMER,
.intr_rx_high = SIO_IR_SB_RX_HIGH,
.intr_tx_explicit = SIO_IR_SB_TX_EXPLICIT,
.intr_clear = (SIO_IR_SB_TX_MT | SIO_IR_SB_RX_FULL
| SIO_IR_SB_RX_HIGH
| SIO_IR_SB_RX_TIMER
| SIO_IR_SB_DELTA_DCD
| SIO_IR_SB_DELTA_CTS
| SIO_IR_SB_INT
| SIO_IR_SB_TX_EXPLICIT
| SIO_IR_SB_MEMERR),
.intr_all = SIO_IR_SB,
.rs422_select_pin = GPPR_UARTB_MODESEL_PIN,
}
};
struct ring_entry {
union {
struct {
uint32_t alldata;
uint32_t allsc;
} all;
struct {
char data[4]; /* data bytes */
char sc[4]; /* status/control */
} s;
} u;
};
/* Test the valid bits in any of the 4 sc chars using "allsc" member */
#define RING_ANY_VALID \
((uint32_t)(RXSB_MODEM_VALID | RXSB_DATA_VALID) * 0x01010101)
#define ring_sc u.s.sc
#define ring_data u.s.data
#define ring_allsc u.all.allsc
/* Number of entries per ring buffer. */
#define ENTRIES_PER_RING (RING_BUF_SIZE / (int) sizeof(struct ring_entry))
/* An individual ring */
struct ring {
struct ring_entry entries[ENTRIES_PER_RING];
};
/* The whole enchilada */
struct ring_buffer {
struct ring TX_A;
struct ring RX_A;
struct ring TX_B;
struct ring RX_B;
};
/* Get a ring from a port struct */
#define RING(_p, _wh) &(((struct ring_buffer *)((_p)->ip_cpu_ringbuf))->_wh)
/* for Infinite loop detection */
#define MAXITER 10000000
/**
* set_baud - Baud rate setting code
* @port: port to set
* @baud: baud rate to use
*/
static int set_baud(struct ioc3_port *port, int baud)
{
int divisor;
int actual_baud;
int diff;
int lcr, prediv;
struct ioc3_uartregs __iomem *uart;
for (prediv = 6; prediv < 64; prediv++) {
divisor = SER_DIVISOR(baud, SER_CLK_SPEED(prediv));
if (!divisor)
continue; /* invalid divisor */
actual_baud = DIVISOR_TO_BAUD(divisor, SER_CLK_SPEED(prediv));
diff = actual_baud - baud;
if (diff < 0)
diff = -diff;
/* if we're within 1% we've found a match */
if (diff * 100 <= actual_baud)
break;
}
/* if the above loop completed, we didn't match
* the baud rate. give up.
*/
if (prediv == 64) {
NOT_PROGRESS();
return 1;
}
uart = port->ip_uart_regs;
lcr = readb(&uart->iu_lcr);
writeb(lcr | UART_LCR_DLAB, &uart->iu_lcr);
writeb((unsigned char)divisor, &uart->iu_dll);
writeb((unsigned char)(divisor >> 8), &uart->iu_dlm);
writeb((unsigned char)prediv, &uart->iu_scr);
writeb((unsigned char)lcr, &uart->iu_lcr);
return 0;
}
/**
* get_ioc3_port - given a uart port, return the control structure
* @the_port: uart port to find
*/
static struct ioc3_port *get_ioc3_port(struct uart_port *the_port)
{
struct ioc3_driver_data *idd = dev_get_drvdata(the_port->dev);
struct ioc3_card *card_ptr = idd->data[Submodule_slot];
int ii, jj;
if (!card_ptr) {
NOT_PROGRESS();
return NULL;
}
for (ii = 0; ii < PORTS_PER_CARD; ii++) {
for (jj = 0; jj < LOGICAL_PORTS; jj++) {
if (the_port == &card_ptr->ic_port[ii].icp_uart_port[jj])
return card_ptr->ic_port[ii].icp_port;
}
}
NOT_PROGRESS();
return NULL;
}
/**
* port_init - Initialize the sio and ioc3 hardware for a given port
* called per port from attach...
* @port: port to initialize
*/
static int inline port_init(struct ioc3_port *port)
{
uint32_t sio_cr;
struct port_hooks *hooks = port->ip_hooks;
struct ioc3_uartregs __iomem *uart;
int reset_loop_counter = 0xfffff;
struct ioc3_driver_data *idd = port->ip_idd;
/* Idle the IOC3 serial interface */
writel(SSCR_RESET, &port->ip_serial_regs->sscr);
/* Wait until any pending bus activity for this port has ceased */
do {
sio_cr = readl(&idd->vma->sio_cr);
if (reset_loop_counter-- <= 0) {
printk(KERN_WARNING
"IOC3 unable to come out of reset"
" scr 0x%x\n", sio_cr);
return -1;
}
} while (!(sio_cr & SIO_CR_ARB_DIAG_IDLE) &&
(((sio_cr &= SIO_CR_ARB_DIAG) == SIO_CR_ARB_DIAG_TXA)
|| sio_cr == SIO_CR_ARB_DIAG_TXB
|| sio_cr == SIO_CR_ARB_DIAG_RXA
|| sio_cr == SIO_CR_ARB_DIAG_RXB));
/* Finish reset sequence */
writel(0, &port->ip_serial_regs->sscr);
/* Once RESET is done, reload cached tx_prod and rx_cons values
* and set rings to empty by making prod == cons
*/
port->ip_tx_prod = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
writel(port->ip_tx_prod, &port->ip_serial_regs->stpir);
port->ip_rx_cons = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
writel(port->ip_rx_cons | SRCIR_ARM, &port->ip_serial_regs->srcir);
/* Disable interrupts for this 16550 */
uart = port->ip_uart_regs;
writeb(0, &uart->iu_lcr);
writeb(0, &uart->iu_ier);
/* Set the default baud */
set_baud(port, port->ip_baud);
/* Set line control to 8 bits no parity */
writeb(UART_LCR_WLEN8 | 0, &uart->iu_lcr);
/* UART_LCR_STOP == 1 stop */
/* Enable the FIFOs */
writeb(UART_FCR_ENABLE_FIFO, &uart->iu_fcr);
/* then reset 16550 FIFOs */
writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
&uart->iu_fcr);
/* Clear modem control register */
writeb(0, &uart->iu_mcr);
/* Clear deltas in modem status register */
writel(0, &port->ip_serial_regs->shadow);
/* Only do this once per port pair */
if (port->ip_hooks == &hooks_array[0]) {
unsigned long ring_pci_addr;
uint32_t __iomem *sbbr_l, *sbbr_h;
sbbr_l = &idd->vma->sbbr_l;
sbbr_h = &idd->vma->sbbr_h;
ring_pci_addr = (unsigned long __iomem)port->ip_dma_ringbuf;
DPRINT_CONFIG(("%s: ring_pci_addr 0x%p\n",
__func__, (void *)ring_pci_addr));
writel((unsigned int)((uint64_t) ring_pci_addr >> 32), sbbr_h);
writel((unsigned int)ring_pci_addr | BUF_SIZE_BIT, sbbr_l);
}
/* Set the receive timeout value to 10 msec */
writel(SRTR_HZ / 100, &port->ip_serial_regs->srtr);
/* Set rx threshold, enable DMA */
/* Set high water mark at 3/4 of full ring */
port->ip_sscr = (ENTRIES_PER_RING * 3 / 4);
/* uart experiences pauses at high baud rate reducing actual
* throughput by 10% or so unless we enable high speed polling
* XXX when this hardware bug is resolved we should revert to
* normal polling speed
*/
port->ip_sscr |= SSCR_HIGH_SPD;
writel(port->ip_sscr, &port->ip_serial_regs->sscr);
/* Disable and clear all serial related interrupt bits */
port->ip_card->ic_enable &= ~hooks->intr_clear;
ioc3_disable(port->ip_is, idd, hooks->intr_clear);
ioc3_ack(port->ip_is, idd, hooks->intr_clear);
return 0;
}
/**
* enable_intrs - enable interrupts
* @port: port to enable
* @mask: mask to use
*/
static void enable_intrs(struct ioc3_port *port, uint32_t mask)
{
if ((port->ip_card->ic_enable & mask) != mask) {
port->ip_card->ic_enable |= mask;
ioc3_enable(port->ip_is, port->ip_idd, mask);
}
}
/**
* local_open - local open a port
* @port: port to open
*/
static inline int local_open(struct ioc3_port *port)
{
int spiniter = 0;
port->ip_flags = INPUT_ENABLE;
/* Pause the DMA interface if necessary */
if (port->ip_sscr & SSCR_DMA_EN) {
writel(port->ip_sscr | SSCR_DMA_PAUSE,
&port->ip_serial_regs->sscr);
while ((readl(&port->ip_serial_regs->sscr)
& SSCR_PAUSE_STATE) == 0) {
spiniter++;
if (spiniter > MAXITER) {
NOT_PROGRESS();
return -1;
}
}
}
/* Reset the input fifo. If the uart received chars while the port
* was closed and DMA is not enabled, the uart may have a bunch of
* chars hanging around in its rx fifo which will not be discarded
* by rclr in the upper layer. We must get rid of them here.
*/
writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR,
&port->ip_uart_regs->iu_fcr);
writeb(UART_LCR_WLEN8, &port->ip_uart_regs->iu_lcr);
/* UART_LCR_STOP == 1 stop */
/* Re-enable DMA, set default threshold to intr whenever there is
* data available.
*/
port->ip_sscr &= ~SSCR_RX_THRESHOLD;
port->ip_sscr |= 1; /* default threshold */
/* Plug in the new sscr. This implicitly clears the DMA_PAUSE
* flag if it was set above
*/
writel(port->ip_sscr, &port->ip_serial_regs->sscr);
port->ip_tx_lowat = 1;
return 0;
}
/**
* set_rx_timeout - Set rx timeout and threshold values.
* @port: port to use
* @timeout: timeout value in ticks
*/
static inline int set_rx_timeout(struct ioc3_port *port, int timeout)
{
int threshold;
port->ip_rx_timeout = timeout;
/* Timeout is in ticks. Let's figure out how many chars we
* can receive at the current baud rate in that interval
* and set the rx threshold to that amount. There are 4 chars
* per ring entry, so we'll divide the number of chars that will
* arrive in timeout by 4.
* So .... timeout * baud / 10 / HZ / 4, with HZ = 100.
*/
threshold = timeout * port->ip_baud / 4000;
if (threshold == 0)
threshold = 1; /* otherwise we'll intr all the time! */
if ((unsigned)threshold > (unsigned)SSCR_RX_THRESHOLD)
return 1;
port->ip_sscr &= ~SSCR_RX_THRESHOLD;
port->ip_sscr |= threshold;
writel(port->ip_sscr, &port->ip_serial_regs->sscr);
/* Now set the rx timeout to the given value
* again timeout * SRTR_HZ / HZ
*/
timeout = timeout * SRTR_HZ / 100;
if (timeout > SRTR_CNT)
timeout = SRTR_CNT;
writel(timeout, &port->ip_serial_regs->srtr);
return 0;
}
/**
* config_port - config the hardware
* @port: port to config
* @baud: baud rate for the port
* @byte_size: data size
* @stop_bits: number of stop bits
* @parenb: parity enable ?
* @parodd: odd parity ?
*/
static inline int
config_port(struct ioc3_port *port,
int baud, int byte_size, int stop_bits, int parenb, int parodd)
{
char lcr, sizebits;
int spiniter = 0;
DPRINT_CONFIG(("%s: line %d baud %d byte_size %d stop %d parenb %d "
"parodd %d\n",
__func__, ((struct uart_port *)port->ip_port)->line,
baud, byte_size, stop_bits, parenb, parodd));
if (set_baud(port, baud))
return 1;
switch (byte_size) {
case 5:
sizebits = UART_LCR_WLEN5;
break;
case 6:
sizebits = UART_LCR_WLEN6;
break;
case 7:
sizebits = UART_LCR_WLEN7;
break;
case 8:
sizebits = UART_LCR_WLEN8;
break;
default:
return 1;
}
/* Pause the DMA interface if necessary */
if (port->ip_sscr & SSCR_DMA_EN) {
writel(port->ip_sscr | SSCR_DMA_PAUSE,
&port->ip_serial_regs->sscr);
while ((readl(&port->ip_serial_regs->sscr)
& SSCR_PAUSE_STATE) == 0) {
spiniter++;
if (spiniter > MAXITER)
return -1;
}
}
/* Clear relevant fields in lcr */
lcr = readb(&port->ip_uart_regs->iu_lcr);
lcr &= ~(LCR_MASK_BITS_CHAR | UART_LCR_EPAR |
UART_LCR_PARITY | LCR_MASK_STOP_BITS);
/* Set byte size in lcr */
lcr |= sizebits;
/* Set parity */
if (parenb) {
lcr |= UART_LCR_PARITY;
if (!parodd)
lcr |= UART_LCR_EPAR;
}
/* Set stop bits */
if (stop_bits)
lcr |= UART_LCR_STOP /* 2 stop bits */ ;
writeb(lcr, &port->ip_uart_regs->iu_lcr);
/* Re-enable the DMA interface if necessary */
if (port->ip_sscr & SSCR_DMA_EN) {
writel(port->ip_sscr, &port->ip_serial_regs->sscr);
}
port->ip_baud = baud;
/* When we get within this number of ring entries of filling the
* entire ring on tx, place an EXPLICIT intr to generate a lowat
* notification when output has drained.
*/
port->ip_tx_lowat = (TX_LOWAT_CHARS(baud) + 3) / 4;
if (port->ip_tx_lowat == 0)
port->ip_tx_lowat = 1;
set_rx_timeout(port, 2);
return 0;
}
/**
* do_write - Write bytes to the port. Returns the number of bytes
* actually written. Called from transmit_chars
* @port: port to use
* @buf: the stuff to write
* @len: how many bytes in 'buf'
*/
static inline int do_write(struct ioc3_port *port, char *buf, int len)
{
int prod_ptr, cons_ptr, total = 0;
struct ring *outring;
struct ring_entry *entry;
struct port_hooks *hooks = port->ip_hooks;
BUG_ON(!(len >= 0));
prod_ptr = port->ip_tx_prod;
cons_ptr = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
outring = port->ip_outring;
/* Maintain a 1-entry red-zone. The ring buffer is full when
* (cons - prod) % ring_size is 1. Rather than do this subtraction
* in the body of the loop, I'll do it now.
*/
cons_ptr = (cons_ptr - (int)sizeof(struct ring_entry)) & PROD_CONS_MASK;
/* Stuff the bytes into the output */
while ((prod_ptr != cons_ptr) && (len > 0)) {
int xx;
/* Get 4 bytes (one ring entry) at a time */
entry = (struct ring_entry *)((caddr_t) outring + prod_ptr);
/* Invalidate all entries */
entry->ring_allsc = 0;
/* Copy in some bytes */
for (xx = 0; (xx < 4) && (len > 0); xx++) {
entry->ring_data[xx] = *buf++;
entry->ring_sc[xx] = TXCB_VALID;
len--;
total++;
}
/* If we are within some small threshold of filling up the
* entire ring buffer, we must place an EXPLICIT intr here
* to generate a lowat interrupt in case we subsequently
* really do fill up the ring and the caller goes to sleep.
* No need to place more than one though.
*/
if (!(port->ip_flags & LOWAT_WRITTEN) &&
((cons_ptr - prod_ptr) & PROD_CONS_MASK)
<= port->ip_tx_lowat * (int)sizeof(struct ring_entry)) {
port->ip_flags |= LOWAT_WRITTEN;
entry->ring_sc[0] |= TXCB_INT_WHEN_DONE;
}
/* Go on to next entry */
prod_ptr += sizeof(struct ring_entry);
prod_ptr &= PROD_CONS_MASK;
}
/* If we sent something, start DMA if necessary */
if (total > 0 && !(port->ip_sscr & SSCR_DMA_EN)) {
port->ip_sscr |= SSCR_DMA_EN;
writel(port->ip_sscr, &port->ip_serial_regs->sscr);
}
/* Store the new producer pointer. If tx is disabled, we stuff the
* data into the ring buffer, but we don't actually start tx.
*/
if (!uart_tx_stopped(port->ip_port)) {
writel(prod_ptr, &port->ip_serial_regs->stpir);
/* If we are now transmitting, enable tx_mt interrupt so we
* can disable DMA if necessary when the tx finishes.
*/
if (total > 0)
enable_intrs(port, hooks->intr_tx_mt);
}
port->ip_tx_prod = prod_ptr;
return total;
}
/**
* disable_intrs - disable interrupts
* @port: port to enable
* @mask: mask to use
*/
static inline void disable_intrs(struct ioc3_port *port, uint32_t mask)
{
if (port->ip_card->ic_enable & mask) {
ioc3_disable(port->ip_is, port->ip_idd, mask);
port->ip_card->ic_enable &= ~mask;
}
}
/**
* set_notification - Modify event notification
* @port: port to use
* @mask: events mask
* @set_on: set ?
*/
static int set_notification(struct ioc3_port *port, int mask, int set_on)
{
struct port_hooks *hooks = port->ip_hooks;
uint32_t intrbits, sscrbits;
BUG_ON(!mask);
intrbits = sscrbits = 0;
if (mask & N_DATA_READY)
intrbits |= (hooks->intr_rx_timer | hooks->intr_rx_high);
if (mask & N_OUTPUT_LOWAT)
intrbits |= hooks->intr_tx_explicit;
if (mask & N_DDCD) {
intrbits |= hooks->intr_delta_dcd;
sscrbits |= SSCR_RX_RING_DCD;
}
if (mask & N_DCTS)
intrbits |= hooks->intr_delta_cts;
if (set_on) {
enable_intrs(port, intrbits);
port->ip_notify |= mask;
port->ip_sscr |= sscrbits;
} else {
disable_intrs(port, intrbits);
port->ip_notify &= ~mask;
port->ip_sscr &= ~sscrbits;
}
/* We require DMA if either DATA_READY or DDCD notification is
* currently requested. If neither of these is requested and
* there is currently no tx in progress, DMA may be disabled.
*/
if (port->ip_notify & (N_DATA_READY | N_DDCD))
port->ip_sscr |= SSCR_DMA_EN;
else if (!(port->ip_card->ic_enable & hooks->intr_tx_mt))
port->ip_sscr &= ~SSCR_DMA_EN;
writel(port->ip_sscr, &port->ip_serial_regs->sscr);
return 0;
}
/**
* set_mcr - set the master control reg
* @the_port: port to use
* @mask1: mcr mask
* @mask2: shadow mask
*/
static inline int set_mcr(struct uart_port *the_port,
int mask1, int mask2)
{
struct ioc3_port *port = get_ioc3_port(the_port);
uint32_t shadow;
int spiniter = 0;
char mcr;
if (!port)
return -1;
/* Pause the DMA interface if necessary */
if (port->ip_sscr & SSCR_DMA_EN) {
writel(port->ip_sscr | SSCR_DMA_PAUSE,
&port->ip_serial_regs->sscr);
while ((readl(&port->ip_serial_regs->sscr)
& SSCR_PAUSE_STATE) == 0) {
spiniter++;
if (spiniter > MAXITER)
return -1;
}
}
shadow = readl(&port->ip_serial_regs->shadow);
mcr = (shadow & 0xff000000) >> 24;
/* Set new value */
mcr |= mask1;
shadow |= mask2;
writeb(mcr, &port->ip_uart_regs->iu_mcr);
writel(shadow, &port->ip_serial_regs->shadow);
/* Re-enable the DMA interface if necessary */
if (port->ip_sscr & SSCR_DMA_EN) {
writel(port->ip_sscr, &port->ip_serial_regs->sscr);
}
return 0;
}
/**
* ioc3_set_proto - set the protocol for the port
* @port: port to use
* @proto: protocol to use
*/
static int ioc3_set_proto(struct ioc3_port *port, int proto)
{
struct port_hooks *hooks = port->ip_hooks;
switch (proto) {
default:
case PROTO_RS232:
/* Clear the appropriate GIO pin */
DPRINT_CONFIG(("%s: rs232\n", __func__));
writel(0, (&port->ip_idd->vma->gppr[0]
+ hooks->rs422_select_pin));
break;
case PROTO_RS422:
/* Set the appropriate GIO pin */
DPRINT_CONFIG(("%s: rs422\n", __func__));
writel(1, (&port->ip_idd->vma->gppr[0]
+ hooks->rs422_select_pin));
break;
}
return 0;
}
/**
* transmit_chars - upper level write, called with the_port->lock
* @the_port: port to write
*/
static void transmit_chars(struct uart_port *the_port)
{
int xmit_count, tail, head;
int result;
char *start;
struct tty_struct *tty;
struct ioc3_port *port = get_ioc3_port(the_port);
struct uart_state *state;
if (!the_port)
return;
if (!port)
return;
state = the_port->state;
tty = state->port.tty;
if (uart_circ_empty(&state->xmit) || uart_tx_stopped(the_port)) {
/* Nothing to do or hw stopped */
set_notification(port, N_ALL_OUTPUT, 0);
return;
}
head = state->xmit.head;
tail = state->xmit.tail;
start = (char *)&state->xmit.buf[tail];
/* write out all the data or until the end of the buffer */
xmit_count = (head < tail) ? (UART_XMIT_SIZE - tail) : (head - tail);
if (xmit_count > 0) {
result = do_write(port, start, xmit_count);
if (result > 0) {
/* booking */
xmit_count -= result;
the_port->icount.tx += result;
/* advance the pointers */
tail += result;
tail &= UART_XMIT_SIZE - 1;
state->xmit.tail = tail;
start = (char *)&state->xmit.buf[tail];
}
}
if (uart_circ_chars_pending(&state->xmit) < WAKEUP_CHARS)
uart_write_wakeup(the_port);
if (uart_circ_empty(&state->xmit)) {
set_notification(port, N_OUTPUT_LOWAT, 0);
} else {
set_notification(port, N_OUTPUT_LOWAT, 1);
}
}
/**
* ioc3_change_speed - change the speed of the port
* @the_port: port to change
* @new_termios: new termios settings
* @old_termios: old termios settings
*/
static void
ioc3_change_speed(struct uart_port *the_port,
struct ktermios *new_termios, struct ktermios *old_termios)
{
struct ioc3_port *port = get_ioc3_port(the_port);
unsigned int cflag, iflag;
int baud;
int new_parity = 0, new_parity_enable = 0, new_stop = 0, new_data = 8;
struct uart_state *state = the_port->state;
cflag = new_termios->c_cflag;
iflag = new_termios->c_iflag;
switch (cflag & CSIZE) {
case CS5:
new_data = 5;
break;
case CS6:
new_data = 6;
break;
case CS7:
new_data = 7;
break;
case CS8:
new_data = 8;
break;
default:
/* cuz we always need a default ... */
new_data = 5;
break;
}
if (cflag & CSTOPB) {
new_stop = 1;
}
if (cflag & PARENB) {
new_parity_enable = 1;
if (cflag & PARODD)
new_parity = 1;
}
baud = uart_get_baud_rate(the_port, new_termios, old_termios,
MIN_BAUD_SUPPORTED, MAX_BAUD_SUPPORTED);
DPRINT_CONFIG(("%s: returned baud %d for line %d\n", __func__, baud,
the_port->line));
if (!the_port->fifosize)
the_port->fifosize = FIFO_SIZE;
uart_update_timeout(the_port, cflag, baud);
the_port->ignore_status_mask = N_ALL_INPUT;