2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2005 Silicon Graphics, Inc. All Rights Reserved.
10 * This file contains a module version of the ioc3 serial driver. This
11 * includes all the support functions needed (support functions, etc.)
12 * and the serial driver itself.
14 #include <linux/errno.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
17 #include <linux/serial.h>
18 #include <linux/circ_buf.h>
19 #include <linux/serial_reg.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/serial_core.h>
23 #include <linux/ioc3.h>
24 #include <linux/slab.h>
27 * Interesting things about the ioc3
30 #define LOGICAL_PORTS 2 /* rs232(0) and rs422(1) */
31 #define PORTS_PER_CARD 2
32 #define LOGICAL_PORTS_PER_CARD (PORTS_PER_CARD * LOGICAL_PORTS)
34 #define MAX_LOGICAL_PORTS (LOGICAL_PORTS_PER_CARD * MAX_CARDS)
36 /* determine given the sio_ir what port it applies to */
37 #define GET_PORT_FROM_SIO_IR(_x) (_x & SIO_IR_SA) ? 0 : 1
41 * we have 2 logical ports (rs232, rs422) for each physical port
42 * evens are rs232, odds are rs422
44 #define GET_PHYSICAL_PORT(_x) ((_x) >> 1)
45 #define GET_LOGICAL_PORT(_x) ((_x) & 1)
46 #define IS_PHYSICAL_PORT(_x) !((_x) & 1)
47 #define IS_RS232(_x) !((_x) & 1)
49 static unsigned int Num_of_ioc3_cards
;
50 static unsigned int Submodule_slot
;
52 /* defining this will get you LOTS of great debug info */
53 //#define DEBUG_INTERRUPTS
54 #define DPRINT_CONFIG(_x...) ;
55 //#define DPRINT_CONFIG(_x...) printk _x
56 #define NOT_PROGRESS() ;
57 //#define NOT_PROGRESS() printk("%s : fails %d\n", __func__, __LINE__)
59 /* number of characters we want to transmit to the lower level at a time */
61 #define FIFO_SIZE (MAX_CHARS-1) /* it's a uchar */
63 /* Device name we're using */
64 #define DEVICE_NAME "ttySIOC"
65 #define DEVICE_MAJOR 204
66 #define DEVICE_MINOR 116
68 /* flags for next_char_state */
70 #define NCS_PARITY 0x2
71 #define NCS_FRAMING 0x4
72 #define NCS_OVERRUN 0x8
74 /* cause we need SOME parameters ... */
75 #define MIN_BAUD_SUPPORTED 1200
76 #define MAX_BAUD_SUPPORTED 115200
78 /* protocol types supported */
82 /* Notification types */
83 #define N_DATA_READY 0x01
84 #define N_OUTPUT_LOWAT 0x02
86 #define N_PARITY_ERROR 0x08
87 #define N_FRAMING_ERROR 0x10
88 #define N_OVERRUN_ERROR 0x20
92 #define N_ALL_INPUT (N_DATA_READY | N_BREAK \
93 | N_PARITY_ERROR | N_FRAMING_ERROR \
94 | N_OVERRUN_ERROR | N_DDCD | N_DCTS)
96 #define N_ALL_OUTPUT N_OUTPUT_LOWAT
98 #define N_ALL_ERRORS (N_PARITY_ERROR | N_FRAMING_ERROR \
101 #define N_ALL (N_DATA_READY | N_OUTPUT_LOWAT | N_BREAK \
102 | N_PARITY_ERROR | N_FRAMING_ERROR \
103 | N_OVERRUN_ERROR | N_DDCD | N_DCTS)
105 #define SER_CLK_SPEED(prediv) ((22000000 << 1) / prediv)
106 #define SER_DIVISOR(x, clk) (((clk) + (x) * 8) / ((x) * 16))
107 #define DIVISOR_TO_BAUD(div, clk) ((clk) / 16 / (div))
110 #define LCR_MASK_BITS_CHAR (UART_LCR_WLEN5 | UART_LCR_WLEN6 \
111 | UART_LCR_WLEN7 | UART_LCR_WLEN8)
112 #define LCR_MASK_STOP_BITS (UART_LCR_STOP)
114 #define PENDING(_a, _p) (readl(&(_p)->vma->sio_ir) & (_a)->ic_enable)
116 #define RING_BUF_SIZE 4096
117 #define BUF_SIZE_BIT SBBR_L_SIZE
118 #define PROD_CONS_MASK PROD_CONS_PTR_4K
120 #define TOTAL_RING_BUF_SIZE (RING_BUF_SIZE * 4)
122 /* driver specific - one per card */
125 /* uart ports are allocated here */
126 struct uart_port icp_uart_port
[LOGICAL_PORTS
];
127 /* the ioc3_port used for this port */
128 struct ioc3_port
*icp_port
;
129 } ic_port
[PORTS_PER_CARD
];
130 /* currently enabled interrupts */
134 /* Local port info for each IOC3 serial port */
136 /* handy reference material */
137 struct uart_port
*ip_port
;
138 struct ioc3_card
*ip_card
;
139 struct ioc3_driver_data
*ip_idd
;
140 struct ioc3_submodule
*ip_is
;
142 /* pci mem addresses for this port */
143 struct ioc3_serialregs __iomem
*ip_serial_regs
;
144 struct ioc3_uartregs __iomem
*ip_uart_regs
;
146 /* Ring buffer page for this port */
147 dma_addr_t ip_dma_ringbuf
;
148 /* vaddr of ring buffer */
149 struct ring_buffer
*ip_cpu_ringbuf
;
151 /* Rings for this port */
152 struct ring
*ip_inring
;
153 struct ring
*ip_outring
;
155 /* Hook to port specific values */
156 struct port_hooks
*ip_hooks
;
160 /* Various rx/tx parameters */
165 /* Copy of notification bits */
168 /* Shadow copies of various registers so we don't need to PIO
169 * read them constantly
174 unsigned char ip_flags
;
177 /* tx low water mark. We need to notify the driver whenever tx is getting
178 * close to empty so it can refill the tx buffer and keep things going.
179 * Let's assume that if we interrupt 1 ms before the tx goes idle, we'll
180 * have no trouble getting in more chars in time (I certainly hope so).
182 #define TX_LOWAT_LATENCY 1000
183 #define TX_LOWAT_HZ (1000000 / TX_LOWAT_LATENCY)
184 #define TX_LOWAT_CHARS(baud) (baud / 10 / TX_LOWAT_HZ)
187 #define INPUT_HIGH 0x01
188 /* used to signify that we have turned off the rx_high
189 * temporarily - we need to drain the fifo and don't
190 * want to get blasted with interrupts.
193 /* DCD state is on */
194 #define LOWAT_WRITTEN 0x04
195 #define READ_ABORTED 0x08
196 /* the read was aborted - used to avaoid infinate looping
197 * in the interrupt handler
199 #define INPUT_ENABLE 0x10
201 /* Since each port has different register offsets and bitmasks
202 * for everything, we'll store those that we need in tables so we
203 * don't have to be constantly checking the port we are dealing with.
206 uint32_t intr_delta_dcd
;
207 uint32_t intr_delta_cts
;
209 uint32_t intr_rx_timer
;
210 uint32_t intr_rx_high
;
211 uint32_t intr_tx_explicit
;
214 char rs422_select_pin
;
217 static struct port_hooks hooks_array
[PORTS_PER_CARD
] = {
218 /* values for port A */
220 .intr_delta_dcd
= SIO_IR_SA_DELTA_DCD
,
221 .intr_delta_cts
= SIO_IR_SA_DELTA_CTS
,
222 .intr_tx_mt
= SIO_IR_SA_TX_MT
,
223 .intr_rx_timer
= SIO_IR_SA_RX_TIMER
,
224 .intr_rx_high
= SIO_IR_SA_RX_HIGH
,
225 .intr_tx_explicit
= SIO_IR_SA_TX_EXPLICIT
,
226 .intr_clear
= (SIO_IR_SA_TX_MT
| SIO_IR_SA_RX_FULL
229 | SIO_IR_SA_DELTA_DCD
230 | SIO_IR_SA_DELTA_CTS
232 | SIO_IR_SA_TX_EXPLICIT
234 .intr_all
= SIO_IR_SA
,
235 .rs422_select_pin
= GPPR_UARTA_MODESEL_PIN
,
238 /* values for port B */
240 .intr_delta_dcd
= SIO_IR_SB_DELTA_DCD
,
241 .intr_delta_cts
= SIO_IR_SB_DELTA_CTS
,
242 .intr_tx_mt
= SIO_IR_SB_TX_MT
,
243 .intr_rx_timer
= SIO_IR_SB_RX_TIMER
,
244 .intr_rx_high
= SIO_IR_SB_RX_HIGH
,
245 .intr_tx_explicit
= SIO_IR_SB_TX_EXPLICIT
,
246 .intr_clear
= (SIO_IR_SB_TX_MT
| SIO_IR_SB_RX_FULL
249 | SIO_IR_SB_DELTA_DCD
250 | SIO_IR_SB_DELTA_CTS
252 | SIO_IR_SB_TX_EXPLICIT
254 .intr_all
= SIO_IR_SB
,
255 .rs422_select_pin
= GPPR_UARTB_MODESEL_PIN
,
266 char data
[4]; /* data bytes */
267 char sc
[4]; /* status/control */
272 /* Test the valid bits in any of the 4 sc chars using "allsc" member */
273 #define RING_ANY_VALID \
274 ((uint32_t)(RXSB_MODEM_VALID | RXSB_DATA_VALID) * 0x01010101)
276 #define ring_sc u.s.sc
277 #define ring_data u.s.data
278 #define ring_allsc u.all.allsc
280 /* Number of entries per ring buffer. */
281 #define ENTRIES_PER_RING (RING_BUF_SIZE / (int) sizeof(struct ring_entry))
283 /* An individual ring */
285 struct ring_entry entries
[ENTRIES_PER_RING
];
288 /* The whole enchilada */
296 /* Get a ring from a port struct */
297 #define RING(_p, _wh) &(((struct ring_buffer *)((_p)->ip_cpu_ringbuf))->_wh)
299 /* for Infinite loop detection */
300 #define MAXITER 10000000
304 * set_baud - Baud rate setting code
306 * @baud: baud rate to use
308 static int set_baud(struct ioc3_port
*port
, int baud
)
314 struct ioc3_uartregs __iomem
*uart
;
316 for (prediv
= 6; prediv
< 64; prediv
++) {
317 divisor
= SER_DIVISOR(baud
, SER_CLK_SPEED(prediv
));
319 continue; /* invalid divisor */
320 actual_baud
= DIVISOR_TO_BAUD(divisor
, SER_CLK_SPEED(prediv
));
322 diff
= actual_baud
- baud
;
326 /* if we're within 1% we've found a match */
327 if (diff
* 100 <= actual_baud
)
331 /* if the above loop completed, we didn't match
332 * the baud rate. give up.
339 uart
= port
->ip_uart_regs
;
340 lcr
= readb(&uart
->iu_lcr
);
342 writeb(lcr
| UART_LCR_DLAB
, &uart
->iu_lcr
);
343 writeb((unsigned char)divisor
, &uart
->iu_dll
);
344 writeb((unsigned char)(divisor
>> 8), &uart
->iu_dlm
);
345 writeb((unsigned char)prediv
, &uart
->iu_scr
);
346 writeb((unsigned char)lcr
, &uart
->iu_lcr
);
352 * get_ioc3_port - given a uart port, return the control structure
353 * @the_port: uart port to find
355 static struct ioc3_port
*get_ioc3_port(struct uart_port
*the_port
)
357 struct ioc3_driver_data
*idd
= dev_get_drvdata(the_port
->dev
);
358 struct ioc3_card
*card_ptr
= idd
->data
[Submodule_slot
];
365 for (ii
= 0; ii
< PORTS_PER_CARD
; ii
++) {
366 for (jj
= 0; jj
< LOGICAL_PORTS
; jj
++) {
367 if (the_port
== &card_ptr
->ic_port
[ii
].icp_uart_port
[jj
])
368 return card_ptr
->ic_port
[ii
].icp_port
;
376 * port_init - Initialize the sio and ioc3 hardware for a given port
377 * called per port from attach...
378 * @port: port to initialize
380 static int inline port_init(struct ioc3_port
*port
)
383 struct port_hooks
*hooks
= port
->ip_hooks
;
384 struct ioc3_uartregs __iomem
*uart
;
385 int reset_loop_counter
= 0xfffff;
386 struct ioc3_driver_data
*idd
= port
->ip_idd
;
388 /* Idle the IOC3 serial interface */
389 writel(SSCR_RESET
, &port
->ip_serial_regs
->sscr
);
391 /* Wait until any pending bus activity for this port has ceased */
393 sio_cr
= readl(&idd
->vma
->sio_cr
);
394 if (reset_loop_counter
-- <= 0) {
396 "IOC3 unable to come out of reset"
397 " scr 0x%x\n", sio_cr
);
400 } while (!(sio_cr
& SIO_CR_ARB_DIAG_IDLE
) &&
401 (((sio_cr
&= SIO_CR_ARB_DIAG
) == SIO_CR_ARB_DIAG_TXA
)
402 || sio_cr
== SIO_CR_ARB_DIAG_TXB
403 || sio_cr
== SIO_CR_ARB_DIAG_RXA
404 || sio_cr
== SIO_CR_ARB_DIAG_RXB
));
406 /* Finish reset sequence */
407 writel(0, &port
->ip_serial_regs
->sscr
);
409 /* Once RESET is done, reload cached tx_prod and rx_cons values
410 * and set rings to empty by making prod == cons
412 port
->ip_tx_prod
= readl(&port
->ip_serial_regs
->stcir
) & PROD_CONS_MASK
;
413 writel(port
->ip_tx_prod
, &port
->ip_serial_regs
->stpir
);
414 port
->ip_rx_cons
= readl(&port
->ip_serial_regs
->srpir
) & PROD_CONS_MASK
;
415 writel(port
->ip_rx_cons
| SRCIR_ARM
, &port
->ip_serial_regs
->srcir
);
417 /* Disable interrupts for this 16550 */
418 uart
= port
->ip_uart_regs
;
419 writeb(0, &uart
->iu_lcr
);
420 writeb(0, &uart
->iu_ier
);
422 /* Set the default baud */
423 set_baud(port
, port
->ip_baud
);
425 /* Set line control to 8 bits no parity */
426 writeb(UART_LCR_WLEN8
| 0, &uart
->iu_lcr
);
427 /* UART_LCR_STOP == 1 stop */
429 /* Enable the FIFOs */
430 writeb(UART_FCR_ENABLE_FIFO
, &uart
->iu_fcr
);
431 /* then reset 16550 FIFOs */
432 writeb(UART_FCR_ENABLE_FIFO
| UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
,
435 /* Clear modem control register */
436 writeb(0, &uart
->iu_mcr
);
438 /* Clear deltas in modem status register */
439 writel(0, &port
->ip_serial_regs
->shadow
);
441 /* Only do this once per port pair */
442 if (port
->ip_hooks
== &hooks_array
[0]) {
443 unsigned long ring_pci_addr
;
444 uint32_t __iomem
*sbbr_l
, *sbbr_h
;
446 sbbr_l
= &idd
->vma
->sbbr_l
;
447 sbbr_h
= &idd
->vma
->sbbr_h
;
448 ring_pci_addr
= (unsigned long __iomem
)port
->ip_dma_ringbuf
;
449 DPRINT_CONFIG(("%s: ring_pci_addr 0x%p\n",
450 __func__
, (void *)ring_pci_addr
));
452 writel((unsigned int)((uint64_t) ring_pci_addr
>> 32), sbbr_h
);
453 writel((unsigned int)ring_pci_addr
| BUF_SIZE_BIT
, sbbr_l
);
456 /* Set the receive timeout value to 10 msec */
457 writel(SRTR_HZ
/ 100, &port
->ip_serial_regs
->srtr
);
459 /* Set rx threshold, enable DMA */
460 /* Set high water mark at 3/4 of full ring */
461 port
->ip_sscr
= (ENTRIES_PER_RING
* 3 / 4);
463 /* uart experiences pauses at high baud rate reducing actual
464 * throughput by 10% or so unless we enable high speed polling
465 * XXX when this hardware bug is resolved we should revert to
466 * normal polling speed
468 port
->ip_sscr
|= SSCR_HIGH_SPD
;
470 writel(port
->ip_sscr
, &port
->ip_serial_regs
->sscr
);
472 /* Disable and clear all serial related interrupt bits */
473 port
->ip_card
->ic_enable
&= ~hooks
->intr_clear
;
474 ioc3_disable(port
->ip_is
, idd
, hooks
->intr_clear
);
475 ioc3_ack(port
->ip_is
, idd
, hooks
->intr_clear
);
480 * enable_intrs - enable interrupts
481 * @port: port to enable
484 static void enable_intrs(struct ioc3_port
*port
, uint32_t mask
)
486 if ((port
->ip_card
->ic_enable
& mask
) != mask
) {
487 port
->ip_card
->ic_enable
|= mask
;
488 ioc3_enable(port
->ip_is
, port
->ip_idd
, mask
);
493 * local_open - local open a port
494 * @port: port to open
496 static inline int local_open(struct ioc3_port
*port
)
500 port
->ip_flags
= INPUT_ENABLE
;
502 /* Pause the DMA interface if necessary */
503 if (port
->ip_sscr
& SSCR_DMA_EN
) {
504 writel(port
->ip_sscr
| SSCR_DMA_PAUSE
,
505 &port
->ip_serial_regs
->sscr
);
506 while ((readl(&port
->ip_serial_regs
->sscr
)
507 & SSCR_PAUSE_STATE
) == 0) {
509 if (spiniter
> MAXITER
) {
516 /* Reset the input fifo. If the uart received chars while the port
517 * was closed and DMA is not enabled, the uart may have a bunch of
518 * chars hanging around in its rx fifo which will not be discarded
519 * by rclr in the upper layer. We must get rid of them here.
521 writeb(UART_FCR_ENABLE_FIFO
| UART_FCR_CLEAR_RCVR
,
522 &port
->ip_uart_regs
->iu_fcr
);
524 writeb(UART_LCR_WLEN8
, &port
->ip_uart_regs
->iu_lcr
);
525 /* UART_LCR_STOP == 1 stop */
527 /* Re-enable DMA, set default threshold to intr whenever there is
530 port
->ip_sscr
&= ~SSCR_RX_THRESHOLD
;
531 port
->ip_sscr
|= 1; /* default threshold */
533 /* Plug in the new sscr. This implicitly clears the DMA_PAUSE
534 * flag if it was set above
536 writel(port
->ip_sscr
, &port
->ip_serial_regs
->sscr
);
537 port
->ip_tx_lowat
= 1;
542 * set_rx_timeout - Set rx timeout and threshold values.
544 * @timeout: timeout value in ticks
546 static inline int set_rx_timeout(struct ioc3_port
*port
, int timeout
)
550 port
->ip_rx_timeout
= timeout
;
552 /* Timeout is in ticks. Let's figure out how many chars we
553 * can receive at the current baud rate in that interval
554 * and set the rx threshold to that amount. There are 4 chars
555 * per ring entry, so we'll divide the number of chars that will
556 * arrive in timeout by 4.
557 * So .... timeout * baud / 10 / HZ / 4, with HZ = 100.
559 threshold
= timeout
* port
->ip_baud
/ 4000;
561 threshold
= 1; /* otherwise we'll intr all the time! */
563 if ((unsigned)threshold
> (unsigned)SSCR_RX_THRESHOLD
)
566 port
->ip_sscr
&= ~SSCR_RX_THRESHOLD
;
567 port
->ip_sscr
|= threshold
;
568 writel(port
->ip_sscr
, &port
->ip_serial_regs
->sscr
);
570 /* Now set the rx timeout to the given value
571 * again timeout * SRTR_HZ / HZ
573 timeout
= timeout
* SRTR_HZ
/ 100;
574 if (timeout
> SRTR_CNT
)
576 writel(timeout
, &port
->ip_serial_regs
->srtr
);
581 * config_port - config the hardware
582 * @port: port to config
583 * @baud: baud rate for the port
584 * @byte_size: data size
585 * @stop_bits: number of stop bits
586 * @parenb: parity enable ?
587 * @parodd: odd parity ?
590 config_port(struct ioc3_port
*port
,
591 int baud
, int byte_size
, int stop_bits
, int parenb
, int parodd
)
596 DPRINT_CONFIG(("%s: line %d baud %d byte_size %d stop %d parenb %d "
598 __func__
, ((struct uart_port
*)port
->ip_port
)->line
,
599 baud
, byte_size
, stop_bits
, parenb
, parodd
));
601 if (set_baud(port
, baud
))
606 sizebits
= UART_LCR_WLEN5
;
609 sizebits
= UART_LCR_WLEN6
;
612 sizebits
= UART_LCR_WLEN7
;
615 sizebits
= UART_LCR_WLEN8
;
621 /* Pause the DMA interface if necessary */
622 if (port
->ip_sscr
& SSCR_DMA_EN
) {
623 writel(port
->ip_sscr
| SSCR_DMA_PAUSE
,
624 &port
->ip_serial_regs
->sscr
);
625 while ((readl(&port
->ip_serial_regs
->sscr
)
626 & SSCR_PAUSE_STATE
) == 0) {
628 if (spiniter
> MAXITER
)
633 /* Clear relevant fields in lcr */
634 lcr
= readb(&port
->ip_uart_regs
->iu_lcr
);
635 lcr
&= ~(LCR_MASK_BITS_CHAR
| UART_LCR_EPAR
|
636 UART_LCR_PARITY
| LCR_MASK_STOP_BITS
);
638 /* Set byte size in lcr */
643 lcr
|= UART_LCR_PARITY
;
645 lcr
|= UART_LCR_EPAR
;
650 lcr
|= UART_LCR_STOP
/* 2 stop bits */ ;
652 writeb(lcr
, &port
->ip_uart_regs
->iu_lcr
);
654 /* Re-enable the DMA interface if necessary */
655 if (port
->ip_sscr
& SSCR_DMA_EN
) {
656 writel(port
->ip_sscr
, &port
->ip_serial_regs
->sscr
);
658 port
->ip_baud
= baud
;
660 /* When we get within this number of ring entries of filling the
661 * entire ring on tx, place an EXPLICIT intr to generate a lowat
662 * notification when output has drained.
664 port
->ip_tx_lowat
= (TX_LOWAT_CHARS(baud
) + 3) / 4;
665 if (port
->ip_tx_lowat
== 0)
666 port
->ip_tx_lowat
= 1;
668 set_rx_timeout(port
, 2);
673 * do_write - Write bytes to the port. Returns the number of bytes
674 * actually written. Called from transmit_chars
676 * @buf: the stuff to write
677 * @len: how many bytes in 'buf'
679 static inline int do_write(struct ioc3_port
*port
, char *buf
, int len
)
681 int prod_ptr
, cons_ptr
, total
= 0;
682 struct ring
*outring
;
683 struct ring_entry
*entry
;
684 struct port_hooks
*hooks
= port
->ip_hooks
;
688 prod_ptr
= port
->ip_tx_prod
;
689 cons_ptr
= readl(&port
->ip_serial_regs
->stcir
) & PROD_CONS_MASK
;
690 outring
= port
->ip_outring
;
692 /* Maintain a 1-entry red-zone. The ring buffer is full when
693 * (cons - prod) % ring_size is 1. Rather than do this subtraction
694 * in the body of the loop, I'll do it now.
696 cons_ptr
= (cons_ptr
- (int)sizeof(struct ring_entry
)) & PROD_CONS_MASK
;
698 /* Stuff the bytes into the output */
699 while ((prod_ptr
!= cons_ptr
) && (len
> 0)) {
702 /* Get 4 bytes (one ring entry) at a time */
703 entry
= (struct ring_entry
*)((caddr_t
) outring
+ prod_ptr
);
705 /* Invalidate all entries */
706 entry
->ring_allsc
= 0;
708 /* Copy in some bytes */
709 for (xx
= 0; (xx
< 4) && (len
> 0); xx
++) {
710 entry
->ring_data
[xx
] = *buf
++;
711 entry
->ring_sc
[xx
] = TXCB_VALID
;
716 /* If we are within some small threshold of filling up the
717 * entire ring buffer, we must place an EXPLICIT intr here
718 * to generate a lowat interrupt in case we subsequently
719 * really do fill up the ring and the caller goes to sleep.
720 * No need to place more than one though.
722 if (!(port
->ip_flags
& LOWAT_WRITTEN
) &&
723 ((cons_ptr
- prod_ptr
) & PROD_CONS_MASK
)
724 <= port
->ip_tx_lowat
* (int)sizeof(struct ring_entry
)) {
725 port
->ip_flags
|= LOWAT_WRITTEN
;
726 entry
->ring_sc
[0] |= TXCB_INT_WHEN_DONE
;
729 /* Go on to next entry */
730 prod_ptr
+= sizeof(struct ring_entry
);
731 prod_ptr
&= PROD_CONS_MASK
;
734 /* If we sent something, start DMA if necessary */
735 if (total
> 0 && !(port
->ip_sscr
& SSCR_DMA_EN
)) {
736 port
->ip_sscr
|= SSCR_DMA_EN
;
737 writel(port
->ip_sscr
, &port
->ip_serial_regs
->sscr
);
740 /* Store the new producer pointer. If tx is disabled, we stuff the
741 * data into the ring buffer, but we don't actually start tx.
743 if (!uart_tx_stopped(port
->ip_port
)) {
744 writel(prod_ptr
, &port
->ip_serial_regs
->stpir
);
746 /* If we are now transmitting, enable tx_mt interrupt so we
747 * can disable DMA if necessary when the tx finishes.
750 enable_intrs(port
, hooks
->intr_tx_mt
);
752 port
->ip_tx_prod
= prod_ptr
;
758 * disable_intrs - disable interrupts
759 * @port: port to enable
762 static inline void disable_intrs(struct ioc3_port
*port
, uint32_t mask
)
764 if (port
->ip_card
->ic_enable
& mask
) {
765 ioc3_disable(port
->ip_is
, port
->ip_idd
, mask
);
766 port
->ip_card
->ic_enable
&= ~mask
;
771 * set_notification - Modify event notification
776 static int set_notification(struct ioc3_port
*port
, int mask
, int set_on
)
778 struct port_hooks
*hooks
= port
->ip_hooks
;
779 uint32_t intrbits
, sscrbits
;
783 intrbits
= sscrbits
= 0;
785 if (mask
& N_DATA_READY
)
786 intrbits
|= (hooks
->intr_rx_timer
| hooks
->intr_rx_high
);
787 if (mask
& N_OUTPUT_LOWAT
)
788 intrbits
|= hooks
->intr_tx_explicit
;
790 intrbits
|= hooks
->intr_delta_dcd
;
791 sscrbits
|= SSCR_RX_RING_DCD
;
794 intrbits
|= hooks
->intr_delta_cts
;
797 enable_intrs(port
, intrbits
);
798 port
->ip_notify
|= mask
;
799 port
->ip_sscr
|= sscrbits
;
801 disable_intrs(port
, intrbits
);
802 port
->ip_notify
&= ~mask
;
803 port
->ip_sscr
&= ~sscrbits
;
806 /* We require DMA if either DATA_READY or DDCD notification is
807 * currently requested. If neither of these is requested and
808 * there is currently no tx in progress, DMA may be disabled.
810 if (port
->ip_notify
& (N_DATA_READY
| N_DDCD
))
811 port
->ip_sscr
|= SSCR_DMA_EN
;
812 else if (!(port
->ip_card
->ic_enable
& hooks
->intr_tx_mt
))
813 port
->ip_sscr
&= ~SSCR_DMA_EN
;
815 writel(port
->ip_sscr
, &port
->ip_serial_regs
->sscr
);
820 * set_mcr - set the master control reg
821 * @the_port: port to use
823 * @mask2: shadow mask
825 static inline int set_mcr(struct uart_port
*the_port
,
826 int mask1
, int mask2
)
828 struct ioc3_port
*port
= get_ioc3_port(the_port
);
836 /* Pause the DMA interface if necessary */
837 if (port
->ip_sscr
& SSCR_DMA_EN
) {
838 writel(port
->ip_sscr
| SSCR_DMA_PAUSE
,
839 &port
->ip_serial_regs
->sscr
);
840 while ((readl(&port
->ip_serial_regs
->sscr
)
841 & SSCR_PAUSE_STATE
) == 0) {
843 if (spiniter
> MAXITER
)
847 shadow
= readl(&port
->ip_serial_regs
->shadow
);
848 mcr
= (shadow
& 0xff000000) >> 24;
853 writeb(mcr
, &port
->ip_uart_regs
->iu_mcr
);
854 writel(shadow
, &port
->ip_serial_regs
->shadow
);
856 /* Re-enable the DMA interface if necessary */
857 if (port
->ip_sscr
& SSCR_DMA_EN
) {
858 writel(port
->ip_sscr
, &port
->ip_serial_regs
->sscr
);
864 * ioc3_set_proto - set the protocol for the port
866 * @proto: protocol to use
868 static int ioc3_set_proto(struct ioc3_port
*port
, int proto
)
870 struct port_hooks
*hooks
= port
->ip_hooks
;
875 /* Clear the appropriate GIO pin */
876 DPRINT_CONFIG(("%s: rs232\n", __func__
));
877 writel(0, (&port
->ip_idd
->vma
->gppr
[0]
878 + hooks
->rs422_select_pin
));
882 /* Set the appropriate GIO pin */
883 DPRINT_CONFIG(("%s: rs422\n", __func__
));
884 writel(1, (&port
->ip_idd
->vma
->gppr
[0]
885 + hooks
->rs422_select_pin
));
892 * transmit_chars - upper level write, called with the_port->lock
893 * @the_port: port to write
895 static void transmit_chars(struct uart_port
*the_port
)
897 int xmit_count
, tail
, head
;
900 struct tty_struct
*tty
;
901 struct ioc3_port
*port
= get_ioc3_port(the_port
);
902 struct uart_state
*state
;
909 state
= the_port
->state
;
910 tty
= state
->port
.tty
;
912 if (uart_circ_empty(&state
->xmit
) || uart_tx_stopped(the_port
)) {
913 /* Nothing to do or hw stopped */
914 set_notification(port
, N_ALL_OUTPUT
, 0);
918 head
= state
->xmit
.head
;
919 tail
= state
->xmit
.tail
;
920 start
= (char *)&state
->xmit
.buf
[tail
];
922 /* write out all the data or until the end of the buffer */
923 xmit_count
= (head
< tail
) ? (UART_XMIT_SIZE
- tail
) : (head
- tail
);
924 if (xmit_count
> 0) {
925 result
= do_write(port
, start
, xmit_count
);
928 xmit_count
-= result
;
929 the_port
->icount
.tx
+= result
;
930 /* advance the pointers */
932 tail
&= UART_XMIT_SIZE
- 1;
933 state
->xmit
.tail
= tail
;
934 start
= (char *)&state
->xmit
.buf
[tail
];
937 if (uart_circ_chars_pending(&state
->xmit
) < WAKEUP_CHARS
)
938 uart_write_wakeup(the_port
);
940 if (uart_circ_empty(&state
->xmit
)) {
941 set_notification(port
, N_OUTPUT_LOWAT
, 0);
943 set_notification(port
, N_OUTPUT_LOWAT
, 1);
948 * ioc3_change_speed - change the speed of the port
949 * @the_port: port to change
950 * @new_termios: new termios settings
951 * @old_termios: old termios settings
954 ioc3_change_speed(struct uart_port
*the_port
,
955 struct ktermios
*new_termios
, struct ktermios
*old_termios
)
957 struct ioc3_port
*port
= get_ioc3_port(the_port
);
958 unsigned int cflag
, iflag
;
960 int new_parity
= 0, new_parity_enable
= 0, new_stop
= 0, new_data
= 8;
961 struct uart_state
*state
= the_port
->state
;
963 cflag
= new_termios
->c_cflag
;
964 iflag
= new_termios
->c_iflag
;
966 switch (cflag
& CSIZE
) {
980 /* cuz we always need a default ... */
984 if (cflag
& CSTOPB
) {
987 if (cflag
& PARENB
) {
988 new_parity_enable
= 1;
992 baud
= uart_get_baud_rate(the_port
, new_termios
, old_termios
,
993 MIN_BAUD_SUPPORTED
, MAX_BAUD_SUPPORTED
);
994 DPRINT_CONFIG(("%s: returned baud %d for line %d\n", __func__
, baud
,
997 if (!the_port
->fifosize
)
998 the_port
->fifosize
= FIFO_SIZE
;
999 uart_update_timeout(the_port
, cflag
, baud
);
1001 the_port
->ignore_status_mask
= N_ALL_INPUT
;
1003 state
->port
.tty
->low_latency
= 1;
1006 the_port
->ignore_status_mask
&= ~(N_PARITY_ERROR
1008 if (iflag
& IGNBRK
) {
1009 the_port
->ignore_status_mask
&= ~N_BREAK
;
1011 the_port
->ignore_status_mask
&= ~N_OVERRUN_ERROR
;
1013 if (!(cflag
& CREAD
)) {
1014 /* ignore everything */
1015 the_port
->ignore_status_mask
&= ~N_DATA_READY
;
1018 if (cflag
& CRTSCTS
) {
1019 /* enable hardware flow control */
1020 port
->ip_sscr
|= SSCR_HFC_EN
;
1023 /* disable hardware flow control */
1024 port
->ip_sscr
&= ~SSCR_HFC_EN
;
1026 writel(port
->ip_sscr
, &port
->ip_serial_regs
->sscr
);
1028 /* Set the configuration and proper notification call */
1029 DPRINT_CONFIG(("%s : port 0x%p line %d cflag 0%o "
1030 "config_port(baud %d data %d stop %d penable %d "
1031 " parity %d), notification 0x%x\n",
1032 __func__
, (void *)port
, the_port
->line
, cflag
, baud
,
1033 new_data
, new_stop
, new_parity_enable
, new_parity
,
1034 the_port
->ignore_status_mask
));
1036 if ((config_port(port
, baud
, /* baud */
1037 new_data
, /* byte size */
1038 new_stop
, /* stop bits */
1039 new_parity_enable
, /* set parity */
1040 new_parity
)) >= 0) { /* parity 1==odd */
1041 set_notification(port
, the_port
->ignore_status_mask
, 1);
1046 * ic3_startup_local - Start up the serial port - returns >= 0 if no errors
1047 * @the_port: Port to operate on
1049 static inline int ic3_startup_local(struct uart_port
*the_port
)
1051 struct ioc3_port
*port
;
1058 port
= get_ioc3_port(the_port
);
1066 /* set the protocol */
1067 ioc3_set_proto(port
, IS_RS232(the_port
->line
) ? PROTO_RS232
:
1073 * ioc3_cb_output_lowat - called when the output low water mark is hit
1074 * @port: port to output
1076 static void ioc3_cb_output_lowat(struct ioc3_port
*port
)
1078 unsigned long pflags
;
1080 /* the_port->lock is set on the call here */
1081 if (port
->ip_port
) {
1082 spin_lock_irqsave(&port
->ip_port
->lock
, pflags
);
1083 transmit_chars(port
->ip_port
);
1084 spin_unlock_irqrestore(&port
->ip_port
->lock
, pflags
);
1089 * ioc3_cb_post_ncs - called for some basic errors
1090 * @port: port to use
1093 static void ioc3_cb_post_ncs(struct uart_port
*the_port
, int ncs
)
1095 struct uart_icount
*icount
;
1097 icount
= &the_port
->icount
;
1099 if (ncs
& NCS_BREAK
)
1101 if (ncs
& NCS_FRAMING
)
1103 if (ncs
& NCS_OVERRUN
)
1105 if (ncs
& NCS_PARITY
)
1110 * do_read - Read in bytes from the port. Return the number of bytes
1112 * @the_port: port to use
1113 * @buf: place to put the stuff we read
1114 * @len: how big 'buf' is
1117 static inline int do_read(struct uart_port
*the_port
, char *buf
, int len
)
1119 int prod_ptr
, cons_ptr
, total
;
1120 struct ioc3_port
*port
= get_ioc3_port(the_port
);
1121 struct ring
*inring
;
1122 struct ring_entry
*entry
;
1123 struct port_hooks
*hooks
= port
->ip_hooks
;
1128 BUG_ON(!(len
>= 0));
1131 /* There is a nasty timing issue in the IOC3. When the rx_timer
1132 * expires or the rx_high condition arises, we take an interrupt.
1133 * At some point while servicing the interrupt, we read bytes from
1134 * the ring buffer and re-arm the rx_timer. However the rx_timer is
1135 * not started until the first byte is received *after* it is armed,
1136 * and any bytes pending in the rx construction buffers are not drained
1137 * to memory until either there are 4 bytes available or the rx_timer
1138 * expires. This leads to a potential situation where data is left
1139 * in the construction buffers forever - 1 to 3 bytes were received
1140 * after the interrupt was generated but before the rx_timer was
1141 * re-armed. At that point as long as no subsequent bytes are received
1142 * the timer will never be started and the bytes will remain in the
1143 * construction buffer forever. The solution is to execute a DRAIN
1144 * command after rearming the timer. This way any bytes received before
1145 * the DRAIN will be drained to memory, and any bytes received after
1146 * the DRAIN will start the TIMER and be drained when it expires.
1147 * Luckily, this only needs to be done when the DMA buffer is empty
1148 * since there is no requirement that this function return all
1149 * available data as long as it returns some.
1151 /* Re-arm the timer */
1153 writel(port
->ip_rx_cons
| SRCIR_ARM
, &port
->ip_serial_regs
->srcir
);
1155 prod_ptr
= readl(&port
->ip_serial_regs
->srpir
) & PROD_CONS_MASK
;
1156 cons_ptr
= port
->ip_rx_cons
;
1158 if (prod_ptr
== cons_ptr
) {
1161 /* Input buffer appears empty, do a flush. */
1163 /* DMA must be enabled for this to work. */
1164 if (!(port
->ip_sscr
& SSCR_DMA_EN
)) {
1165 port
->ip_sscr
|= SSCR_DMA_EN
;
1169 /* Potential race condition: we must reload the srpir after
1170 * issuing the drain command, otherwise we could think the rx
1171 * buffer is empty, then take a very long interrupt, and when
1172 * we come back it's full and we wait forever for the drain to
1175 writel(port
->ip_sscr
| SSCR_RX_DRAIN
,
1176 &port
->ip_serial_regs
->sscr
);
1177 prod_ptr
= readl(&port
->ip_serial_regs
->srpir
) & PROD_CONS_MASK
;
1179 /* We must not wait for the DRAIN to complete unless there are
1180 * at least 8 bytes (2 ring entries) available to receive the
1181 * data otherwise the DRAIN will never complete and we'll
1183 * In fact, to make things easier, I'll just ignore the flush if
1184 * there is any data at all now available.
1186 if (prod_ptr
== cons_ptr
) {
1188 while (readl(&port
->ip_serial_regs
->sscr
) &
1191 if (loop_counter
> MAXITER
)
1195 /* SIGH. We have to reload the prod_ptr *again* since
1196 * the drain may have caused it to change
1198 prod_ptr
= readl(&port
->ip_serial_regs
->srpir
)
1202 port
->ip_sscr
&= ~SSCR_DMA_EN
;
1203 writel(port
->ip_sscr
, &port
->ip_serial_regs
->sscr
);
1206 inring
= port
->ip_inring
;
1207 port
->ip_flags
&= ~READ_ABORTED
;
1210 loop_counter
= 0xfffff; /* to avoid hangs */
1212 /* Grab bytes from the hardware */
1213 while ((prod_ptr
!= cons_ptr
) && (len
> 0)) {
1214 entry
= (struct ring_entry
*)((caddr_t
) inring
+ cons_ptr
);
1216 if (loop_counter
-- <= 0) {
1217 printk(KERN_WARNING
"IOC3 serial: "
1218 "possible hang condition/"
1219 "port stuck on read (line %d).\n",
1224 /* According to the producer pointer, this ring entry
1225 * must contain some data. But if the PIO happened faster
1226 * than the DMA, the data may not be available yet, so let's
1227 * wait until it arrives.
1229 if ((entry
->ring_allsc
& RING_ANY_VALID
) == 0) {
1230 /* Indicate the read is aborted so we don't disable
1231 * the interrupt thinking that the consumer is
1234 port
->ip_flags
|= READ_ABORTED
;
1239 /* Load the bytes/status out of the ring entry */
1240 for (byte_num
= 0; byte_num
< 4 && len
> 0; byte_num
++) {
1241 sc
= &(entry
->ring_sc
[byte_num
]);
1243 /* Check for change in modem state or overrun */
1244 if ((*sc
& RXSB_MODEM_VALID
)
1245 && (port
->ip_notify
& N_DDCD
)) {
1246 /* Notify upper layer if DCD dropped */
1247 if ((port
->ip_flags
& DCD_ON
)
1248 && !(*sc
& RXSB_DCD
)) {
1249 /* If we have already copied some data,
1250 * return it. We'll pick up the carrier
1251 * drop on the next pass. That way we
1252 * don't throw away the data that has
1253 * already been copied back to
1254 * the caller's buffer.
1260 port
->ip_flags
&= ~DCD_ON
;
1262 /* Turn off this notification so the
1263 * carrier drop protocol won't see it
1264 * again when it does a read.
1266 *sc
&= ~RXSB_MODEM_VALID
;
1268 /* To keep things consistent, we need
1269 * to update the consumer pointer so
1270 * the next reader won't come in and
1271 * try to read the same ring entries
1272 * again. This must be done here before
1276 if ((entry
->ring_allsc
& RING_ANY_VALID
)
1278 cons_ptr
+= (int)sizeof
1279 (struct ring_entry
);
1280 cons_ptr
&= PROD_CONS_MASK
;
1283 &port
->ip_serial_regs
->srcir
);
1284 port
->ip_rx_cons
= cons_ptr
;
1286 /* Notify upper layer of carrier drop */
1287 if ((port
->ip_notify
& N_DDCD
)
1289 uart_handle_dcd_change
1291 wake_up_interruptible
1293 port
.delta_msr_wait
);
1296 /* If we had any data to return, we
1297 * would have returned it above.
1302 if (*sc
& RXSB_MODEM_VALID
) {
1303 /* Notify that an input overrun occurred */
1304 if ((*sc
& RXSB_OVERRUN
)
1305 && (port
->ip_notify
& N_OVERRUN_ERROR
)) {
1306 ioc3_cb_post_ncs(the_port
, NCS_OVERRUN
);
1308 /* Don't look at this byte again */
1309 *sc
&= ~RXSB_MODEM_VALID
;
1312 /* Check for valid data or RX errors */
1313 if ((*sc
& RXSB_DATA_VALID
) &&
1314 ((*sc
& (RXSB_PAR_ERR
1315 | RXSB_FRAME_ERR
| RXSB_BREAK
))
1316 && (port
->ip_notify
& (N_PARITY_ERROR
1319 /* There is an error condition on the next byte.
1320 * If we have already transferred some bytes,
1321 * we'll stop here. Otherwise if this is the
1322 * first byte to be read, we'll just transfer
1323 * it alone after notifying the
1324 * upper layer of its status.
1330 if ((*sc
& RXSB_PAR_ERR
) &&
1332 ip_notify
& N_PARITY_ERROR
)) {
1333 ioc3_cb_post_ncs(the_port
,
1336 if ((*sc
& RXSB_FRAME_ERR
) &&
1338 ip_notify
& N_FRAMING_ERROR
)) {
1339 ioc3_cb_post_ncs(the_port
,
1342 if ((*sc
& RXSB_BREAK
)
1343 && (port
->ip_notify
& N_BREAK
)) {
1345 (the_port
, NCS_BREAK
);
1350 if (*sc
& RXSB_DATA_VALID
) {
1351 *sc
&= ~RXSB_DATA_VALID
;
1352 *buf
= entry
->ring_data
[byte_num
];
1359 /* If we used up this entry entirely, go on to the next one,
1360 * otherwise we must have run out of buffer space, so
1361 * leave the consumer pointer here for the next read in case
1362 * there are still unread bytes in this entry.
1364 if ((entry
->ring_allsc
& RING_ANY_VALID
) == 0) {
1365 cons_ptr
+= (int)sizeof(struct ring_entry
);
1366 cons_ptr
&= PROD_CONS_MASK
;
1370 /* Update consumer pointer and re-arm rx timer interrupt */
1371 writel(cons_ptr
, &port
->ip_serial_regs
->srcir
);
1372 port
->ip_rx_cons
= cons_ptr
;
1374 /* If we have now dipped below the rx high water mark and we have
1375 * rx_high interrupt turned off, we can now turn it back on again.
1377 if ((port
->ip_flags
& INPUT_HIGH
) && (((prod_ptr
- cons_ptr
)
1382 << PROD_CONS_PTR_OFF
))) {
1383 port
->ip_flags
&= ~INPUT_HIGH
;
1384 enable_intrs(port
, hooks
->intr_rx_high
);
1390 * receive_chars - upper level read.
1391 * @the_port: port to read from
1393 static int receive_chars(struct uart_port
*the_port
)
1395 struct tty_struct
*tty
;
1396 unsigned char ch
[MAX_CHARS
];
1397 int read_count
= 0, read_room
, flip
= 0;
1398 struct uart_state
*state
= the_port
->state
;
1399 struct ioc3_port
*port
= get_ioc3_port(the_port
);
1400 unsigned long pflags
;
1402 /* Make sure all the pointers are "good" ones */
1405 if (!state
->port
.tty
)
1408 if (!(port
->ip_flags
& INPUT_ENABLE
))
1411 spin_lock_irqsave(&the_port
->lock
, pflags
);
1412 tty
= state
->port
.tty
;
1414 read_count
= do_read(the_port
, ch
, MAX_CHARS
);
1415 if (read_count
> 0) {
1417 read_room
= tty_insert_flip_string(tty
, ch
, read_count
);
1418 the_port
->icount
.rx
+= read_count
;
1420 spin_unlock_irqrestore(&the_port
->lock
, pflags
);
1423 tty_flip_buffer_push(tty
);
1429 * ioc3uart_intr_one - lowest level (per port) interrupt handler.
1432 * @pending: interrupts to handle
1436 ioc3uart_intr_one(struct ioc3_submodule
*is
,
1437 struct ioc3_driver_data
*idd
,
1438 unsigned int pending
)
1440 int port_num
= GET_PORT_FROM_SIO_IR(pending
);
1441 struct port_hooks
*hooks
;
1442 unsigned int rx_high_rd_aborted
= 0;
1443 unsigned long flags
;
1444 struct uart_port
*the_port
;
1445 struct ioc3_port
*port
;
1447 struct ioc3_card
*card_ptr
;
1448 unsigned int sio_ir
;
1450 card_ptr
= idd
->data
[is
->id
];
1451 port
= card_ptr
->ic_port
[port_num
].icp_port
;
1452 hooks
= port
->ip_hooks
;
1454 /* Possible race condition here: The tx_mt interrupt bit may be
1455 * cleared without the intervention of the interrupt handler,
1456 * e.g. by a write. If the top level interrupt handler reads a
1457 * tx_mt, then some other processor does a write, starting up
1458 * output, then we come in here, see the tx_mt and stop DMA, the
1459 * output started by the other processor will hang. Thus we can
1460 * only rely on tx_mt being legitimate if it is read while the
1461 * port lock is held. Therefore this bit must be ignored in the
1462 * passed in interrupt mask which was read by the top level
1463 * interrupt handler since the port lock was not held at the time
1464 * it was read. We can only rely on this bit being accurate if it
1465 * is read while the port lock is held. So we'll clear it for now,
1466 * and reload it later once we have the port lock.
1469 sio_ir
= pending
& ~(hooks
->intr_tx_mt
);
1470 spin_lock_irqsave(&port
->ip_lock
, flags
);
1472 loop_counter
= MAXITER
; /* to avoid hangs */
1477 if (loop_counter
-- <= 0) {
1478 printk(KERN_WARNING
"IOC3 serial: "
1479 "possible hang condition/"
1480 "port stuck on interrupt (line %d).\n",
1481 ((struct uart_port
*)port
->ip_port
)->line
);
1484 /* Handle a DCD change */
1485 if (sio_ir
& hooks
->intr_delta_dcd
) {
1486 ioc3_ack(is
, idd
, hooks
->intr_delta_dcd
);
1487 shadow
= readl(&port
->ip_serial_regs
->shadow
);
1489 if ((port
->ip_notify
& N_DDCD
)
1490 && (shadow
& SHADOW_DCD
)
1491 && (port
->ip_port
)) {
1492 the_port
= port
->ip_port
;
1493 uart_handle_dcd_change(the_port
,
1494 shadow
& SHADOW_DCD
);
1495 wake_up_interruptible
1496 (&the_port
->state
->port
.delta_msr_wait
);
1497 } else if ((port
->ip_notify
& N_DDCD
)
1498 && !(shadow
& SHADOW_DCD
)) {
1499 /* Flag delta DCD/no DCD */
1500 uart_handle_dcd_change(port
->ip_port
,
1501 shadow
& SHADOW_DCD
);
1502 port
->ip_flags
|= DCD_ON
;
1506 /* Handle a CTS change */
1507 if (sio_ir
& hooks
->intr_delta_cts
) {
1508 ioc3_ack(is
, idd
, hooks
->intr_delta_cts
);
1509 shadow
= readl(&port
->ip_serial_regs
->shadow
);
1511 if ((port
->ip_notify
& N_DCTS
) && (port
->ip_port
)) {
1512 the_port
= port
->ip_port
;
1513 uart_handle_cts_change(the_port
, shadow
1515 wake_up_interruptible
1516 (&the_port
->state
->port
.delta_msr_wait
);
1520 /* rx timeout interrupt. Must be some data available. Put this
1521 * before the check for rx_high since servicing this condition
1522 * may cause that condition to clear.
1524 if (sio_ir
& hooks
->intr_rx_timer
) {
1525 ioc3_ack(is
, idd
, hooks
->intr_rx_timer
);
1526 if ((port
->ip_notify
& N_DATA_READY
)
1527 && (port
->ip_port
)) {
1528 receive_chars(port
->ip_port
);
1532 /* rx high interrupt. Must be after rx_timer. */
1533 else if (sio_ir
& hooks
->intr_rx_high
) {
1534 /* Data available, notify upper layer */
1535 if ((port
->ip_notify
& N_DATA_READY
) && port
->ip_port
) {
1536 receive_chars(port
->ip_port
);
1539 /* We can't ACK this interrupt. If receive_chars didn't
1540 * cause the condition to clear, we'll have to disable
1541 * the interrupt until the data is drained.
1542 * If the read was aborted, don't disable the interrupt
1543 * as this may cause us to hang indefinitely. An
1544 * aborted read generally means that this interrupt
1545 * hasn't been delivered to the cpu yet anyway, even
1546 * though we see it as asserted when we read the sio_ir.
1548 if ((sio_ir
= PENDING(card_ptr
, idd
))
1549 & hooks
->intr_rx_high
) {
1550 if (port
->ip_flags
& READ_ABORTED
) {
1551 rx_high_rd_aborted
++;
1554 card_ptr
->ic_enable
&= ~hooks
->intr_rx_high
;
1555 port
->ip_flags
|= INPUT_HIGH
;
1560 /* We got a low water interrupt: notify upper layer to
1561 * send more data. Must come before tx_mt since servicing
1562 * this condition may cause that condition to clear.
1564 if (sio_ir
& hooks
->intr_tx_explicit
) {
1565 port
->ip_flags
&= ~LOWAT_WRITTEN
;
1566 ioc3_ack(is
, idd
, hooks
->intr_tx_explicit
);
1567 if (port
->ip_notify
& N_OUTPUT_LOWAT
)
1568 ioc3_cb_output_lowat(port
);
1571 /* Handle tx_mt. Must come after tx_explicit. */
1572 else if (sio_ir
& hooks
->intr_tx_mt
) {
1573 /* If we are expecting a lowat notification
1574 * and we get to this point it probably means that for
1575 * some reason the tx_explicit didn't work as expected
1576 * (that can legitimately happen if the output buffer is
1577 * filled up in just the right way).
1578 * So send the notification now.
1580 if (port
->ip_notify
& N_OUTPUT_LOWAT
) {
1581 ioc3_cb_output_lowat(port
);
1583 /* We need to reload the sio_ir since the lowat
1584 * call may have caused another write to occur,
1585 * clearing the tx_mt condition.
1587 sio_ir
= PENDING(card_ptr
, idd
);
1590 /* If the tx_mt condition still persists even after the
1591 * lowat call, we've got some work to do.
1593 if (sio_ir
& hooks
->intr_tx_mt
) {
1594 /* If we are not currently expecting DMA input,
1595 * and the transmitter has just gone idle,
1596 * there is no longer any reason for DMA, so
1599 if (!(port
->ip_notify
1600 & (N_DATA_READY
| N_DDCD
))) {
1601 BUG_ON(!(port
->ip_sscr
1603 port
->ip_sscr
&= ~SSCR_DMA_EN
;
1604 writel(port
->ip_sscr
,
1605 &port
->ip_serial_regs
->sscr
);
1607 /* Prevent infinite tx_mt interrupt */
1608 card_ptr
->ic_enable
&= ~hooks
->intr_tx_mt
;
1611 sio_ir
= PENDING(card_ptr
, idd
);
1613 /* if the read was aborted and only hooks->intr_rx_high,
1614 * clear hooks->intr_rx_high, so we do not loop forever.
1617 if (rx_high_rd_aborted
&& (sio_ir
== hooks
->intr_rx_high
)) {
1618 sio_ir
&= ~hooks
->intr_rx_high
;
1620 } while (sio_ir
& hooks
->intr_all
);
1622 spin_unlock_irqrestore(&port
->ip_lock
, flags
);
1623 ioc3_enable(is
, idd
, card_ptr
->ic_enable
);
1628 * ioc3uart_intr - field all serial interrupts
1631 * @pending: interrupts to handle
1635 static int ioc3uart_intr(struct ioc3_submodule
*is
,
1636 struct ioc3_driver_data
*idd
,
1637 unsigned int pending
)
1642 * The upper level interrupt handler sends interrupts for both ports
1643 * here. So we need to call for each port with its interrupts.
1646 if (pending
& SIO_IR_SA
)
1647 ret
|= ioc3uart_intr_one(is
, idd
, pending
& SIO_IR_SA
);
1648 if (pending
& SIO_IR_SB
)
1649 ret
|= ioc3uart_intr_one(is
, idd
, pending
& SIO_IR_SB
);
1656 * @port: Port to operate with (we ignore since we only have one port)
1659 static const char *ic3_type(struct uart_port
*the_port
)
1661 if (IS_RS232(the_port
->line
))
1662 return "SGI IOC3 Serial [rs232]";
1664 return "SGI IOC3 Serial [rs422]";
1668 * ic3_tx_empty - Is the transmitter empty?
1669 * @port: Port to operate on
1672 static unsigned int ic3_tx_empty(struct uart_port
*the_port
)
1674 unsigned int ret
= 0;
1675 struct ioc3_port
*port
= get_ioc3_port(the_port
);
1677 if (readl(&port
->ip_serial_regs
->shadow
) & SHADOW_TEMT
)
1683 * ic3_stop_tx - stop the transmitter
1684 * @port: Port to operate on
1687 static void ic3_stop_tx(struct uart_port
*the_port
)
1689 struct ioc3_port
*port
= get_ioc3_port(the_port
);
1692 set_notification(port
, N_OUTPUT_LOWAT
, 0);
1696 * ic3_stop_rx - stop the receiver
1697 * @port: Port to operate on
1700 static void ic3_stop_rx(struct uart_port
*the_port
)
1702 struct ioc3_port
*port
= get_ioc3_port(the_port
);
1705 port
->ip_flags
&= ~INPUT_ENABLE
;
1709 * null_void_function
1710 * @port: Port to operate on
1713 static void null_void_function(struct uart_port
*the_port
)
1718 * ic3_shutdown - shut down the port - free irq and disable
1719 * @port: port to shut down
1722 static void ic3_shutdown(struct uart_port
*the_port
)
1724 unsigned long port_flags
;
1725 struct ioc3_port
*port
;
1726 struct uart_state
*state
;
1728 port
= get_ioc3_port(the_port
);
1732 state
= the_port
->state
;
1733 wake_up_interruptible(&state
->port
.delta_msr_wait
);
1735 spin_lock_irqsave(&the_port
->lock
, port_flags
);
1736 set_notification(port
, N_ALL
, 0);
1737 spin_unlock_irqrestore(&the_port
->lock
, port_flags
);
1741 * ic3_set_mctrl - set control lines (dtr, rts, etc)
1742 * @port: Port to operate on
1743 * @mctrl: Lines to set/unset
1746 static void ic3_set_mctrl(struct uart_port
*the_port
, unsigned int mctrl
)
1748 unsigned char mcr
= 0;
1750 if (mctrl
& TIOCM_RTS
)
1751 mcr
|= UART_MCR_RTS
;
1752 if (mctrl
& TIOCM_DTR
)
1753 mcr
|= UART_MCR_DTR
;
1754 if (mctrl
& TIOCM_OUT1
)
1755 mcr
|= UART_MCR_OUT1
;
1756 if (mctrl
& TIOCM_OUT2
)
1757 mcr
|= UART_MCR_OUT2
;
1758 if (mctrl
& TIOCM_LOOP
)
1759 mcr
|= UART_MCR_LOOP
;
1761 set_mcr(the_port
, mcr
, SHADOW_DTR
);
1765 * ic3_get_mctrl - get control line info
1766 * @port: port to operate on
1769 static unsigned int ic3_get_mctrl(struct uart_port
*the_port
)
1771 struct ioc3_port
*port
= get_ioc3_port(the_port
);
1773 unsigned int ret
= 0;
1778 shadow
= readl(&port
->ip_serial_regs
->shadow
);
1779 if (shadow
& SHADOW_DCD
)
1781 if (shadow
& SHADOW_DR
)
1783 if (shadow
& SHADOW_CTS
)
1789 * ic3_start_tx - Start transmitter. Called with the_port->lock
1790 * @port: Port to operate on
1793 static void ic3_start_tx(struct uart_port
*the_port
)
1795 struct ioc3_port
*port
= get_ioc3_port(the_port
);
1798 set_notification(port
, N_OUTPUT_LOWAT
, 1);
1799 enable_intrs(port
, port
->ip_hooks
->intr_tx_mt
);
1804 * ic3_break_ctl - handle breaks
1805 * @port: Port to operate on
1806 * @break_state: Break state
1809 static void ic3_break_ctl(struct uart_port
*the_port
, int break_state
)
1814 * ic3_startup - Start up the serial port - always return 0 (We're always on)
1815 * @port: Port to operate on
1818 static int ic3_startup(struct uart_port
*the_port
)
1821 struct ioc3_port
*port
;
1822 struct ioc3_card
*card_ptr
;
1823 unsigned long port_flags
;
1829 port
= get_ioc3_port(the_port
);
1834 card_ptr
= port
->ip_card
;
1835 port
->ip_port
= the_port
;
1842 /* Start up the serial port */
1843 spin_lock_irqsave(&the_port
->lock
, port_flags
);
1844 retval
= ic3_startup_local(the_port
);
1845 spin_unlock_irqrestore(&the_port
->lock
, port_flags
);
1850 * ic3_set_termios - set termios stuff
1851 * @port: port to operate on
1852 * @termios: New settings
1857 ic3_set_termios(struct uart_port
*the_port
,
1858 struct ktermios
*termios
, struct ktermios
*old_termios
)
1860 unsigned long port_flags
;
1862 spin_lock_irqsave(&the_port
->lock
, port_flags
);
1863 ioc3_change_speed(the_port
, termios
, old_termios
);
1864 spin_unlock_irqrestore(&the_port
->lock
, port_flags
);
1868 * ic3_request_port - allocate resources for port - no op....
1869 * @port: port to operate on
1872 static int ic3_request_port(struct uart_port
*port
)
1877 /* Associate the uart functions above - given to serial core */
1878 static struct uart_ops ioc3_ops
= {
1879 .tx_empty
= ic3_tx_empty
,
1880 .set_mctrl
= ic3_set_mctrl
,
1881 .get_mctrl
= ic3_get_mctrl
,
1882 .stop_tx
= ic3_stop_tx
,
1883 .start_tx
= ic3_start_tx
,
1884 .stop_rx
= ic3_stop_rx
,
1885 .enable_ms
= null_void_function
,
1886 .break_ctl
= ic3_break_ctl
,
1887 .startup
= ic3_startup
,
1888 .shutdown
= ic3_shutdown
,
1889 .set_termios
= ic3_set_termios
,
1891 .release_port
= null_void_function
,
1892 .request_port
= ic3_request_port
,
1896 * Boot-time initialization code
1899 static struct uart_driver ioc3_uart
= {
1900 .owner
= THIS_MODULE
,
1901 .driver_name
= "ioc3_serial",
1902 .dev_name
= DEVICE_NAME
,
1903 .major
= DEVICE_MAJOR
,
1904 .minor
= DEVICE_MINOR
,
1905 .nr
= MAX_LOGICAL_PORTS
1909 * ioc3_serial_core_attach - register with serial core
1910 * This is done during pci probing
1911 * @is: submodule struct for this
1912 * @idd: handle for this card
1914 static inline int ioc3_serial_core_attach( struct ioc3_submodule
*is
,
1915 struct ioc3_driver_data
*idd
)
1917 struct ioc3_port
*port
;
1918 struct uart_port
*the_port
;
1919 struct ioc3_card
*card_ptr
= idd
->data
[is
->id
];
1921 struct pci_dev
*pdev
= idd
->pdev
;
1923 DPRINT_CONFIG(("%s: attach pdev 0x%p - card_ptr 0x%p\n",
1924 __func__
, pdev
, (void *)card_ptr
));
1929 /* once around for each logical port on this card */
1930 for (ii
= 0; ii
< LOGICAL_PORTS_PER_CARD
; ii
++) {
1931 phys_port
= GET_PHYSICAL_PORT(ii
);
1932 the_port
= &card_ptr
->ic_port
[phys_port
].
1933 icp_uart_port
[GET_LOGICAL_PORT(ii
)];
1934 port
= card_ptr
->ic_port
[phys_port
].icp_port
;
1935 port
->ip_port
= the_port
;
1937 DPRINT_CONFIG(("%s: attach the_port 0x%p / port 0x%p [%d/%d]\n",
1938 __func__
, (void *)the_port
, (void *)port
,
1941 /* membase, iobase and mapbase just need to be non-0 */
1942 the_port
->membase
= (unsigned char __iomem
*)1;
1943 the_port
->iobase
= (pdev
->bus
->number
<< 16) | ii
;
1944 the_port
->line
= (Num_of_ioc3_cards
<< 2) | ii
;
1945 the_port
->mapbase
= 1;
1946 the_port
->type
= PORT_16550A
;
1947 the_port
->fifosize
= FIFO_SIZE
;
1948 the_port
->ops
= &ioc3_ops
;
1949 the_port
->irq
= idd
->irq_io
;
1950 the_port
->dev
= &pdev
->dev
;
1952 if (uart_add_one_port(&ioc3_uart
, the_port
) < 0) {
1954 "%s: unable to add port %d bus %d\n",
1955 __func__
, the_port
->line
, pdev
->bus
->number
);
1957 DPRINT_CONFIG(("IOC3 serial port %d irq %d bus %d\n",
1958 the_port
->line
, the_port
->irq
, pdev
->bus
->number
));
1961 /* all ports are rs232 for now */
1962 if (IS_PHYSICAL_PORT(ii
))
1963 ioc3_set_proto(port
, PROTO_RS232
);
1969 * ioc3uart_remove - register detach function
1970 * @is: submodule struct for this submodule
1971 * @idd: ioc3 driver data for this submodule
1974 static int ioc3uart_remove(struct ioc3_submodule
*is
,
1975 struct ioc3_driver_data
*idd
)
1977 struct ioc3_card
*card_ptr
= idd
->data
[is
->id
];
1978 struct uart_port
*the_port
;
1979 struct ioc3_port
*port
;
1983 for (ii
= 0; ii
< LOGICAL_PORTS_PER_CARD
; ii
++) {
1984 the_port
= &card_ptr
->ic_port
[GET_PHYSICAL_PORT(ii
)].
1985 icp_uart_port
[GET_LOGICAL_PORT(ii
)];
1987 uart_remove_one_port(&ioc3_uart
, the_port
);
1988 port
= card_ptr
->ic_port
[GET_PHYSICAL_PORT(ii
)].icp_port
;
1989 if (port
&& IS_PHYSICAL_PORT(ii
)
1990 && (GET_PHYSICAL_PORT(ii
) == 0)) {
1991 pci_free_consistent(port
->ip_idd
->pdev
,
1992 TOTAL_RING_BUF_SIZE
,
1993 (void *)port
->ip_cpu_ringbuf
,
1994 port
->ip_dma_ringbuf
);
1996 card_ptr
->ic_port
[GET_PHYSICAL_PORT(ii
)].
2001 idd
->data
[is
->id
] = NULL
;
2007 * ioc3uart_probe - card probe function called from shim driver
2008 * @is: submodule struct for this submodule
2009 * @idd: ioc3 driver data for this card
2012 static int __devinit
2013 ioc3uart_probe(struct ioc3_submodule
*is
, struct ioc3_driver_data
*idd
)
2015 struct pci_dev
*pdev
= idd
->pdev
;
2016 struct ioc3_card
*card_ptr
;
2018 struct ioc3_port
*port
;
2019 struct ioc3_port
*ports
[PORTS_PER_CARD
];
2023 DPRINT_CONFIG(("%s (0x%p, 0x%p)\n", __func__
, is
, idd
));
2025 card_ptr
= kzalloc(sizeof(struct ioc3_card
), GFP_KERNEL
);
2027 printk(KERN_WARNING
"ioc3_attach_one"
2028 ": unable to get memory for the IOC3\n");
2031 idd
->data
[is
->id
] = card_ptr
;
2032 Submodule_slot
= is
->id
;
2034 writel(((UARTA_BASE
>> 3) << SIO_CR_SER_A_BASE_SHIFT
) |
2035 ((UARTB_BASE
>> 3) << SIO_CR_SER_B_BASE_SHIFT
) |
2036 (0xf << SIO_CR_CMD_PULSE_SHIFT
), &idd
->vma
->sio_cr
);
2038 pci_write_config_dword(pdev
, PCI_LAT
, 0xff00);
2040 /* Enable serial port mode select generic PIO pins as outputs */
2041 ioc3_gpcr_set(idd
, GPCR_UARTA_MODESEL
| GPCR_UARTB_MODESEL
);
2043 /* Create port structures for each port */
2044 for (phys_port
= 0; phys_port
< PORTS_PER_CARD
; phys_port
++) {
2045 port
= kzalloc(sizeof(struct ioc3_port
), GFP_KERNEL
);
2048 "IOC3 serial memory not available for port\n");
2052 spin_lock_init(&port
->ip_lock
);
2054 /* we need to remember the previous ones, to point back to
2055 * them farther down - setting up the ring buffers.
2057 ports
[phys_port
] = port
;
2059 /* init to something useful */
2060 card_ptr
->ic_port
[phys_port
].icp_port
= port
;
2063 port
->ip_baud
= 9600;
2064 port
->ip_card
= card_ptr
;
2065 port
->ip_hooks
= &hooks_array
[phys_port
];
2067 /* Setup each port */
2068 if (phys_port
== 0) {
2069 port
->ip_serial_regs
= &idd
->vma
->port_a
;
2070 port
->ip_uart_regs
= &idd
->vma
->sregs
.uarta
;
2072 DPRINT_CONFIG(("%s : Port A ip_serial_regs 0x%p "
2073 "ip_uart_regs 0x%p\n",
2075 (void *)port
->ip_serial_regs
,
2076 (void *)port
->ip_uart_regs
));
2078 /* setup ring buffers */
2079 port
->ip_cpu_ringbuf
= pci_alloc_consistent(pdev
,
2080 TOTAL_RING_BUF_SIZE
, &port
->ip_dma_ringbuf
);
2082 BUG_ON(!((((int64_t) port
->ip_dma_ringbuf
) &
2083 (TOTAL_RING_BUF_SIZE
- 1)) == 0));
2084 port
->ip_inring
= RING(port
, RX_A
);
2085 port
->ip_outring
= RING(port
, TX_A
);
2086 DPRINT_CONFIG(("%s : Port A ip_cpu_ringbuf 0x%p "
2087 "ip_dma_ringbuf 0x%p, ip_inring 0x%p "
2088 "ip_outring 0x%p\n",
2090 (void *)port
->ip_cpu_ringbuf
,
2091 (void *)port
->ip_dma_ringbuf
,
2092 (void *)port
->ip_inring
,
2093 (void *)port
->ip_outring
));
2096 port
->ip_serial_regs
= &idd
->vma
->port_b
;
2097 port
->ip_uart_regs
= &idd
->vma
->sregs
.uartb
;
2099 DPRINT_CONFIG(("%s : Port B ip_serial_regs 0x%p "
2100 "ip_uart_regs 0x%p\n",
2102 (void *)port
->ip_serial_regs
,
2103 (void *)port
->ip_uart_regs
));
2105 /* share the ring buffers */
2106 port
->ip_dma_ringbuf
=
2107 ports
[phys_port
- 1]->ip_dma_ringbuf
;
2108 port
->ip_cpu_ringbuf
=
2109 ports
[phys_port
- 1]->ip_cpu_ringbuf
;
2110 port
->ip_inring
= RING(port
, RX_B
);
2111 port
->ip_outring
= RING(port
, TX_B
);
2112 DPRINT_CONFIG(("%s : Port B ip_cpu_ringbuf 0x%p "
2113 "ip_dma_ringbuf 0x%p, ip_inring 0x%p "
2114 "ip_outring 0x%p\n",
2116 (void *)port
->ip_cpu_ringbuf
,
2117 (void *)port
->ip_dma_ringbuf
,
2118 (void *)port
->ip_inring
,
2119 (void *)port
->ip_outring
));
2122 DPRINT_CONFIG(("%s : port %d [addr 0x%p] card_ptr 0x%p",
2124 phys_port
, (void *)port
, (void *)card_ptr
));
2125 DPRINT_CONFIG((" ip_serial_regs 0x%p ip_uart_regs 0x%p\n",
2126 (void *)port
->ip_serial_regs
,
2127 (void *)port
->ip_uart_regs
));
2129 /* Initialize the hardware for IOC3 */
2132 DPRINT_CONFIG(("%s: phys_port %d port 0x%p inring 0x%p "
2135 phys_port
, (void *)port
,
2136 (void *)port
->ip_inring
,
2137 (void *)port
->ip_outring
));
2141 /* register port with the serial core */
2143 if ((ret
= ioc3_serial_core_attach(is
, idd
)))
2146 Num_of_ioc3_cards
++;
2150 /* error exits that give back resources */
2152 for (cnt
= 0; cnt
< phys_port
; cnt
++)
2159 static struct ioc3_submodule ioc3uart_ops
= {
2161 .probe
= ioc3uart_probe
,
2162 .remove
= ioc3uart_remove
,
2163 /* call .intr for both ports initially */
2164 .irq_mask
= SIO_IR_SA
| SIO_IR_SB
,
2165 .intr
= ioc3uart_intr
,
2166 .owner
= THIS_MODULE
,
2170 * ioc3_detect - module init called,
2172 static int __init
ioc3uart_init(void)
2176 /* register with serial core */
2177 if ((ret
= uart_register_driver(&ioc3_uart
)) < 0) {
2179 "%s: Couldn't register IOC3 uart serial driver\n",
2183 ret
= ioc3_register_submodule(&ioc3uart_ops
);
2185 uart_unregister_driver(&ioc3_uart
);
2189 static void __exit
ioc3uart_exit(void)
2191 ioc3_unregister_submodule(&ioc3uart_ops
);
2192 uart_unregister_driver(&ioc3_uart
);
2195 module_init(ioc3uart_init
);
2196 module_exit(ioc3uart_exit
);
2198 MODULE_AUTHOR("Pat Gefre - Silicon Graphics Inc. (SGI) <pfg@sgi.com>");
2199 MODULE_DESCRIPTION("Serial PCI driver module for SGI IOC3 card");
2200 MODULE_LICENSE("GPL");