[SERIAL] Clean up and fix tty transmission start/stoping
[pandora-kernel.git] / drivers / serial / ioc4_serial.c
1 /*
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
4  * for more details.
5  *
6  * Copyright (C) 2003-2005 Silicon Graphics, Inc.  All Rights Reserved.
7  */
8
9
10 /*
11  * This file contains a module version of the ioc4 serial driver. This
12  * includes all the support functions needed (support functions, etc.)
13  * and the serial driver itself.
14  */
15 #include <linux/errno.h>
16 #include <linux/tty.h>
17 #include <linux/serial.h>
18 #include <linux/serialP.h>
19 #include <linux/circ_buf.h>
20 #include <linux/serial_reg.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/ioc4.h>
24 #include <linux/serial_core.h>
25
26 /*
27  * interesting things about the ioc4
28  */
29
30 #define IOC4_NUM_SERIAL_PORTS   4       /* max ports per card */
31 #define IOC4_NUM_CARDS          8       /* max cards per partition */
32
33 #define GET_SIO_IR(_n)  (_n == 0) ? (IOC4_SIO_IR_S0) : \
34                                 (_n == 1) ? (IOC4_SIO_IR_S1) : \
35                                 (_n == 2) ? (IOC4_SIO_IR_S2) : \
36                                 (IOC4_SIO_IR_S3)
37
38 #define GET_OTHER_IR(_n)  (_n == 0) ? (IOC4_OTHER_IR_S0_MEMERR) : \
39                                 (_n == 1) ? (IOC4_OTHER_IR_S1_MEMERR) : \
40                                 (_n == 2) ? (IOC4_OTHER_IR_S2_MEMERR) : \
41                                 (IOC4_OTHER_IR_S3_MEMERR)
42
43
44 /*
45  * All IOC4 registers are 32 bits wide.
46  */
47
48 /*
49  * PCI Memory Space Map
50  */
51 #define IOC4_PCI_ERR_ADDR_L     0x000   /* Low Error Address */
52 #define IOC4_PCI_ERR_ADDR_VLD           (0x1 << 0)
53 #define IOC4_PCI_ERR_ADDR_MST_ID_MSK    (0xf << 1)
54 #define IOC4_PCI_ERR_ADDR_MST_NUM_MSK   (0xe << 1)
55 #define IOC4_PCI_ERR_ADDR_MST_TYP_MSK   (0x1 << 1)
56 #define IOC4_PCI_ERR_ADDR_MUL_ERR       (0x1 << 5)
57 #define IOC4_PCI_ERR_ADDR_ADDR_MSK      (0x3ffffff << 6)
58
59 /* Interrupt types */
60 #define IOC4_SIO_INTR_TYPE      0
61 #define IOC4_OTHER_INTR_TYPE    1
62 #define IOC4_NUM_INTR_TYPES     2
63
64 /* Bitmasks for IOC4_SIO_IR, IOC4_SIO_IEC, and IOC4_SIO_IES  */
65 #define IOC4_SIO_IR_S0_TX_MT       0x00000001   /* Serial port 0 TX empty */
66 #define IOC4_SIO_IR_S0_RX_FULL     0x00000002   /* Port 0 RX buf full */
67 #define IOC4_SIO_IR_S0_RX_HIGH     0x00000004   /* Port 0 RX hiwat */
68 #define IOC4_SIO_IR_S0_RX_TIMER    0x00000008   /* Port 0 RX timeout */
69 #define IOC4_SIO_IR_S0_DELTA_DCD   0x00000010   /* Port 0 delta DCD */
70 #define IOC4_SIO_IR_S0_DELTA_CTS   0x00000020   /* Port 0 delta CTS */
71 #define IOC4_SIO_IR_S0_INT         0x00000040   /* Port 0 pass-thru intr */
72 #define IOC4_SIO_IR_S0_TX_EXPLICIT 0x00000080   /* Port 0 explicit TX thru */
73 #define IOC4_SIO_IR_S1_TX_MT       0x00000100   /* Serial port 1 */
74 #define IOC4_SIO_IR_S1_RX_FULL     0x00000200   /* */
75 #define IOC4_SIO_IR_S1_RX_HIGH     0x00000400   /* */
76 #define IOC4_SIO_IR_S1_RX_TIMER    0x00000800   /* */
77 #define IOC4_SIO_IR_S1_DELTA_DCD   0x00001000   /* */
78 #define IOC4_SIO_IR_S1_DELTA_CTS   0x00002000   /* */
79 #define IOC4_SIO_IR_S1_INT         0x00004000   /* */
80 #define IOC4_SIO_IR_S1_TX_EXPLICIT 0x00008000   /* */
81 #define IOC4_SIO_IR_S2_TX_MT       0x00010000   /* Serial port 2 */
82 #define IOC4_SIO_IR_S2_RX_FULL     0x00020000   /* */
83 #define IOC4_SIO_IR_S2_RX_HIGH     0x00040000   /* */
84 #define IOC4_SIO_IR_S2_RX_TIMER    0x00080000   /* */
85 #define IOC4_SIO_IR_S2_DELTA_DCD   0x00100000   /* */
86 #define IOC4_SIO_IR_S2_DELTA_CTS   0x00200000   /* */
87 #define IOC4_SIO_IR_S2_INT         0x00400000   /* */
88 #define IOC4_SIO_IR_S2_TX_EXPLICIT 0x00800000   /* */
89 #define IOC4_SIO_IR_S3_TX_MT       0x01000000   /* Serial port 3 */
90 #define IOC4_SIO_IR_S3_RX_FULL     0x02000000   /* */
91 #define IOC4_SIO_IR_S3_RX_HIGH     0x04000000   /* */
92 #define IOC4_SIO_IR_S3_RX_TIMER    0x08000000   /* */
93 #define IOC4_SIO_IR_S3_DELTA_DCD   0x10000000   /* */
94 #define IOC4_SIO_IR_S3_DELTA_CTS   0x20000000   /* */
95 #define IOC4_SIO_IR_S3_INT         0x40000000   /* */
96 #define IOC4_SIO_IR_S3_TX_EXPLICIT 0x80000000   /* */
97
98 /* Per device interrupt masks */
99 #define IOC4_SIO_IR_S0          (IOC4_SIO_IR_S0_TX_MT | \
100                                  IOC4_SIO_IR_S0_RX_FULL | \
101                                  IOC4_SIO_IR_S0_RX_HIGH | \
102                                  IOC4_SIO_IR_S0_RX_TIMER | \
103                                  IOC4_SIO_IR_S0_DELTA_DCD | \
104                                  IOC4_SIO_IR_S0_DELTA_CTS | \
105                                  IOC4_SIO_IR_S0_INT | \
106                                  IOC4_SIO_IR_S0_TX_EXPLICIT)
107 #define IOC4_SIO_IR_S1          (IOC4_SIO_IR_S1_TX_MT | \
108                                  IOC4_SIO_IR_S1_RX_FULL | \
109                                  IOC4_SIO_IR_S1_RX_HIGH | \
110                                  IOC4_SIO_IR_S1_RX_TIMER | \
111                                  IOC4_SIO_IR_S1_DELTA_DCD | \
112                                  IOC4_SIO_IR_S1_DELTA_CTS | \
113                                  IOC4_SIO_IR_S1_INT | \
114                                  IOC4_SIO_IR_S1_TX_EXPLICIT)
115 #define IOC4_SIO_IR_S2          (IOC4_SIO_IR_S2_TX_MT | \
116                                  IOC4_SIO_IR_S2_RX_FULL | \
117                                  IOC4_SIO_IR_S2_RX_HIGH | \
118                                  IOC4_SIO_IR_S2_RX_TIMER | \
119                                  IOC4_SIO_IR_S2_DELTA_DCD | \
120                                  IOC4_SIO_IR_S2_DELTA_CTS | \
121                                  IOC4_SIO_IR_S2_INT | \
122                                  IOC4_SIO_IR_S2_TX_EXPLICIT)
123 #define IOC4_SIO_IR_S3          (IOC4_SIO_IR_S3_TX_MT | \
124                                  IOC4_SIO_IR_S3_RX_FULL | \
125                                  IOC4_SIO_IR_S3_RX_HIGH | \
126                                  IOC4_SIO_IR_S3_RX_TIMER | \
127                                  IOC4_SIO_IR_S3_DELTA_DCD | \
128                                  IOC4_SIO_IR_S3_DELTA_CTS | \
129                                  IOC4_SIO_IR_S3_INT | \
130                                  IOC4_SIO_IR_S3_TX_EXPLICIT)
131
132 /* Bitmasks for IOC4_OTHER_IR, IOC4_OTHER_IEC, and IOC4_OTHER_IES  */
133 #define IOC4_OTHER_IR_ATA_INT           0x00000001  /* ATAPI intr pass-thru */
134 #define IOC4_OTHER_IR_ATA_MEMERR        0x00000002  /* ATAPI DMA PCI error */
135 #define IOC4_OTHER_IR_S0_MEMERR         0x00000004  /* Port 0 PCI error */
136 #define IOC4_OTHER_IR_S1_MEMERR         0x00000008  /* Port 1 PCI error */
137 #define IOC4_OTHER_IR_S2_MEMERR         0x00000010  /* Port 2 PCI error */
138 #define IOC4_OTHER_IR_S3_MEMERR         0x00000020  /* Port 3 PCI error */
139 #define IOC4_OTHER_IR_KBD_INT           0x00000040  /* Keyboard/mouse */
140 #define IOC4_OTHER_IR_RESERVED          0x007fff80  /* Reserved */
141 #define IOC4_OTHER_IR_RT_INT            0x00800000  /* INT_OUT section output */
142 #define IOC4_OTHER_IR_GEN_INT           0xff000000  /* Generic pins */
143
144 #define IOC4_OTHER_IR_SER_MEMERR (IOC4_OTHER_IR_S0_MEMERR | IOC4_OTHER_IR_S1_MEMERR | \
145                                   IOC4_OTHER_IR_S2_MEMERR | IOC4_OTHER_IR_S3_MEMERR)
146
147 /* Bitmasks for IOC4_SIO_CR */
148 #define IOC4_SIO_CR_CMD_PULSE_SHIFT              0  /* byte bus strobe shift */
149 #define IOC4_SIO_CR_ARB_DIAG_TX0        0x00000000
150 #define IOC4_SIO_CR_ARB_DIAG_RX0        0x00000010
151 #define IOC4_SIO_CR_ARB_DIAG_TX1        0x00000020
152 #define IOC4_SIO_CR_ARB_DIAG_RX1        0x00000030
153 #define IOC4_SIO_CR_ARB_DIAG_TX2        0x00000040
154 #define IOC4_SIO_CR_ARB_DIAG_RX2        0x00000050
155 #define IOC4_SIO_CR_ARB_DIAG_TX3        0x00000060
156 #define IOC4_SIO_CR_ARB_DIAG_RX3        0x00000070
157 #define IOC4_SIO_CR_SIO_DIAG_IDLE       0x00000080  /* 0 -> active request among
158                                                            serial ports (ro) */
159 /* Defs for some of the generic I/O pins */
160 #define IOC4_GPCR_UART0_MODESEL    0x10 /* Pin is output to port 0
161                                                    mode sel */
162 #define IOC4_GPCR_UART1_MODESEL    0x20 /* Pin is output to port 1
163                                                    mode sel */
164 #define IOC4_GPCR_UART2_MODESEL    0x40 /* Pin is output to port 2
165                                                    mode sel */
166 #define IOC4_GPCR_UART3_MODESEL    0x80 /* Pin is output to port 3
167                                                    mode sel */
168
169 #define IOC4_GPPR_UART0_MODESEL_PIN   4 /* GIO pin controlling
170                                            uart 0 mode select */
171 #define IOC4_GPPR_UART1_MODESEL_PIN   5 /* GIO pin controlling
172                                            uart 1 mode select */
173 #define IOC4_GPPR_UART2_MODESEL_PIN   6 /* GIO pin controlling
174                                            uart 2 mode select */
175 #define IOC4_GPPR_UART3_MODESEL_PIN   7 /* GIO pin controlling
176                                            uart 3 mode select */
177
178 /* Bitmasks for serial RX status byte */
179 #define IOC4_RXSB_OVERRUN       0x01    /* Char(s) lost */
180 #define IOC4_RXSB_PAR_ERR       0x02    /* Parity error */
181 #define IOC4_RXSB_FRAME_ERR     0x04    /* Framing error */
182 #define IOC4_RXSB_BREAK         0x08    /* Break character */
183 #define IOC4_RXSB_CTS           0x10    /* State of CTS */
184 #define IOC4_RXSB_DCD           0x20    /* State of DCD */
185 #define IOC4_RXSB_MODEM_VALID   0x40    /* DCD, CTS, and OVERRUN are valid */
186 #define IOC4_RXSB_DATA_VALID    0x80    /* Data byte, FRAME_ERR PAR_ERR
187                                          * & BREAK valid */
188
189 /* Bitmasks for serial TX control byte */
190 #define IOC4_TXCB_INT_WHEN_DONE 0x20    /* Interrupt after this byte is sent */
191 #define IOC4_TXCB_INVALID       0x00    /* Byte is invalid */
192 #define IOC4_TXCB_VALID         0x40    /* Byte is valid */
193 #define IOC4_TXCB_MCR           0x80    /* Data<7:0> to modem control reg */
194 #define IOC4_TXCB_DELAY         0xc0    /* Delay data<7:0> mSec */
195
196 /* Bitmasks for IOC4_SBBR_L */
197 #define IOC4_SBBR_L_SIZE        0x00000001  /* 0 == 1KB rings, 1 == 4KB rings */
198
199 /* Bitmasks for IOC4_SSCR_<3:0> */
200 #define IOC4_SSCR_RX_THRESHOLD  0x000001ff  /* Hiwater mark */
201 #define IOC4_SSCR_TX_TIMER_BUSY 0x00010000  /* TX timer in progress */
202 #define IOC4_SSCR_HFC_EN        0x00020000  /* Hardware flow control enabled */
203 #define IOC4_SSCR_RX_RING_DCD   0x00040000  /* Post RX record on delta-DCD */
204 #define IOC4_SSCR_RX_RING_CTS   0x00080000  /* Post RX record on delta-CTS */
205 #define IOC4_SSCR_DIAG          0x00200000  /* Bypass clock divider for sim */
206 #define IOC4_SSCR_RX_DRAIN      0x08000000  /* Drain RX buffer to memory */
207 #define IOC4_SSCR_DMA_EN        0x10000000  /* Enable ring buffer DMA */
208 #define IOC4_SSCR_DMA_PAUSE     0x20000000  /* Pause DMA */
209 #define IOC4_SSCR_PAUSE_STATE   0x40000000  /* Sets when PAUSE takes effect */
210 #define IOC4_SSCR_RESET         0x80000000  /* Reset DMA channels */
211
212 /* All producer/comsumer pointers are the same bitfield */
213 #define IOC4_PROD_CONS_PTR_4K   0x00000ff8      /* For 4K buffers */
214 #define IOC4_PROD_CONS_PTR_1K   0x000003f8      /* For 1K buffers */
215 #define IOC4_PROD_CONS_PTR_OFF           3
216
217 /* Bitmasks for IOC4_SRCIR_<3:0> */
218 #define IOC4_SRCIR_ARM          0x80000000      /* Arm RX timer */
219
220 /* Bitmasks for IOC4_SHADOW_<3:0> */
221 #define IOC4_SHADOW_DR   0x00000001     /* Data ready */
222 #define IOC4_SHADOW_OE   0x00000002     /* Overrun error */
223 #define IOC4_SHADOW_PE   0x00000004     /* Parity error */
224 #define IOC4_SHADOW_FE   0x00000008     /* Framing error */
225 #define IOC4_SHADOW_BI   0x00000010     /* Break interrupt */
226 #define IOC4_SHADOW_THRE 0x00000020     /* Xmit holding register empty */
227 #define IOC4_SHADOW_TEMT 0x00000040     /* Xmit shift register empty */
228 #define IOC4_SHADOW_RFCE 0x00000080     /* Char in RX fifo has an error */
229 #define IOC4_SHADOW_DCTS 0x00010000     /* Delta clear to send */
230 #define IOC4_SHADOW_DDCD 0x00080000     /* Delta data carrier detect */
231 #define IOC4_SHADOW_CTS  0x00100000     /* Clear to send */
232 #define IOC4_SHADOW_DCD  0x00800000     /* Data carrier detect */
233 #define IOC4_SHADOW_DTR  0x01000000     /* Data terminal ready */
234 #define IOC4_SHADOW_RTS  0x02000000     /* Request to send */
235 #define IOC4_SHADOW_OUT1 0x04000000     /* 16550 OUT1 bit */
236 #define IOC4_SHADOW_OUT2 0x08000000     /* 16550 OUT2 bit */
237 #define IOC4_SHADOW_LOOP 0x10000000     /* Loopback enabled */
238
239 /* Bitmasks for IOC4_SRTR_<3:0> */
240 #define IOC4_SRTR_CNT           0x00000fff      /* Reload value for RX timer */
241 #define IOC4_SRTR_CNT_VAL       0x0fff0000      /* Current value of RX timer */
242 #define IOC4_SRTR_CNT_VAL_SHIFT         16
243 #define IOC4_SRTR_HZ                 16000      /* SRTR clock frequency */
244
245 /* Serial port register map used for DMA and PIO serial I/O */
246 struct ioc4_serialregs {
247         uint32_t sscr;
248         uint32_t stpir;
249         uint32_t stcir;
250         uint32_t srpir;
251         uint32_t srcir;
252         uint32_t srtr;
253         uint32_t shadow;
254 };
255
256 /* IOC4 UART register map */
257 struct ioc4_uartregs {
258         char i4u_lcr;
259         union {
260                 char iir;       /* read only */
261                 char fcr;       /* write only */
262         } u3;
263         union {
264                 char ier;       /* DLAB == 0 */
265                 char dlm;       /* DLAB == 1 */
266         } u2;
267         union {
268                 char rbr;       /* read only, DLAB == 0 */
269                 char thr;       /* write only, DLAB == 0 */
270                 char dll;       /* DLAB == 1 */
271         } u1;
272         char i4u_scr;
273         char i4u_msr;
274         char i4u_lsr;
275         char i4u_mcr;
276 };
277
278 /* short names */
279 #define i4u_dll u1.dll
280 #define i4u_ier u2.ier
281 #define i4u_dlm u2.dlm
282 #define i4u_fcr u3.fcr
283
284 /* Serial port registers used for DMA serial I/O */
285 struct ioc4_serial {
286         uint32_t sbbr01_l;
287         uint32_t sbbr01_h;
288         uint32_t sbbr23_l;
289         uint32_t sbbr23_h;
290
291         struct ioc4_serialregs port_0;
292         struct ioc4_serialregs port_1;
293         struct ioc4_serialregs port_2;
294         struct ioc4_serialregs port_3;
295         struct ioc4_uartregs uart_0;
296         struct ioc4_uartregs uart_1;
297         struct ioc4_uartregs uart_2;
298         struct ioc4_uartregs uart_3;
299 } ioc4_serial;
300
301 /* UART clock speed */
302 #define IOC4_SER_XIN_CLK_66     66666667
303 #define IOC4_SER_XIN_CLK_33     33333333
304
305 #define IOC4_W_IES              0
306 #define IOC4_W_IEC              1
307
308 typedef void ioc4_intr_func_f(void *, uint32_t);
309 typedef ioc4_intr_func_f *ioc4_intr_func_t;
310
311 /* defining this will get you LOTS of great debug info */
312 //#define DEBUG_INTERRUPTS
313 #define DPRINT_CONFIG(_x...)    ;
314 //#define DPRINT_CONFIG(_x...)  printk _x
315
316 /* number of characters left in xmit buffer before we ask for more */
317 #define WAKEUP_CHARS    256
318
319 /* number of characters we want to transmit to the lower level at a time */
320 #define IOC4_MAX_CHARS  128
321
322 /* Device name we're using */
323 #define DEVICE_NAME     "ttyIOC"
324 #define DEVICE_MAJOR 204
325 #define DEVICE_MINOR 50
326
327 /* register offsets */
328 #define IOC4_SERIAL_OFFSET      0x300
329
330 /* flags for next_char_state */
331 #define NCS_BREAK       0x1
332 #define NCS_PARITY      0x2
333 #define NCS_FRAMING     0x4
334 #define NCS_OVERRUN     0x8
335
336 /* cause we need SOME parameters ... */
337 #define MIN_BAUD_SUPPORTED      1200
338 #define MAX_BAUD_SUPPORTED      115200
339
340 /* protocol types supported */
341 enum sio_proto {
342         PROTO_RS232,
343         PROTO_RS422
344 };
345
346 /* Notification types */
347 #define N_DATA_READY    0x01
348 #define N_OUTPUT_LOWAT  0x02
349 #define N_BREAK         0x04
350 #define N_PARITY_ERROR  0x08
351 #define N_FRAMING_ERROR 0x10
352 #define N_OVERRUN_ERROR 0x20
353 #define N_DDCD          0x40
354 #define N_DCTS          0x80
355
356 #define N_ALL_INPUT     (N_DATA_READY | N_BREAK |                       \
357                          N_PARITY_ERROR | N_FRAMING_ERROR |             \
358                          N_OVERRUN_ERROR | N_DDCD | N_DCTS)
359
360 #define N_ALL_OUTPUT    N_OUTPUT_LOWAT
361
362 #define N_ALL_ERRORS    (N_PARITY_ERROR | N_FRAMING_ERROR | N_OVERRUN_ERROR)
363
364 #define N_ALL           (N_DATA_READY | N_OUTPUT_LOWAT | N_BREAK |      \
365                          N_PARITY_ERROR | N_FRAMING_ERROR |             \
366                          N_OVERRUN_ERROR | N_DDCD | N_DCTS)
367
368 #define SER_DIVISOR(_x, clk)            (((clk) + (_x) * 8) / ((_x) * 16))
369 #define DIVISOR_TO_BAUD(div, clk)       ((clk) / 16 / (div))
370
371 /* Some masks */
372 #define LCR_MASK_BITS_CHAR      (UART_LCR_WLEN5 | UART_LCR_WLEN6 \
373                                         | UART_LCR_WLEN7 | UART_LCR_WLEN8)
374 #define LCR_MASK_STOP_BITS      (UART_LCR_STOP)
375
376 #define PENDING(_p)     (readl(&(_p)->ip_mem->sio_ir.raw) & _p->ip_ienb)
377 #define READ_SIO_IR(_p) readl(&(_p)->ip_mem->sio_ir.raw)
378
379 /* Default to 4k buffers */
380 #ifdef IOC4_1K_BUFFERS
381 #define RING_BUF_SIZE 1024
382 #define IOC4_BUF_SIZE_BIT 0
383 #define PROD_CONS_MASK IOC4_PROD_CONS_PTR_1K
384 #else
385 #define RING_BUF_SIZE 4096
386 #define IOC4_BUF_SIZE_BIT IOC4_SBBR_L_SIZE
387 #define PROD_CONS_MASK IOC4_PROD_CONS_PTR_4K
388 #endif
389
390 #define TOTAL_RING_BUF_SIZE (RING_BUF_SIZE * 4)
391
392 /*
393  * This is the entry saved by the driver - one per card
394  */
395 struct ioc4_control {
396         int ic_irq;
397         struct {
398                 /* uart ports are allocated here */
399                 struct uart_port icp_uart_port;
400                 /* Handy reference material */
401                 struct ioc4_port *icp_port;
402         } ic_port[IOC4_NUM_SERIAL_PORTS];
403         struct ioc4_soft *ic_soft;
404 };
405
406 /*
407  * per-IOC4 data structure
408  */
409 #define MAX_IOC4_INTR_ENTS      (8 * sizeof(uint32_t))
410 struct ioc4_soft {
411         struct ioc4_misc_regs __iomem *is_ioc4_misc_addr;
412         struct ioc4_serial __iomem *is_ioc4_serial_addr;
413
414         /* Each interrupt type has an entry in the array */
415         struct ioc4_intr_type {
416
417                 /*
418                  * Each in-use entry in this array contains at least
419                  * one nonzero bit in sd_bits; no two entries in this
420                  * array have overlapping sd_bits values.
421                  */
422                 struct ioc4_intr_info {
423                         uint32_t sd_bits;
424                         ioc4_intr_func_f *sd_intr;
425                         void *sd_info;
426                 } is_intr_info[MAX_IOC4_INTR_ENTS];
427
428                 /* Number of entries active in the above array */
429                 atomic_t is_num_intrs;
430         } is_intr_type[IOC4_NUM_INTR_TYPES];
431
432         /* is_ir_lock must be held while
433          * modifying sio_ie values, so
434          * we can be sure that sio_ie is
435          * not changing when we read it
436          * along with sio_ir.
437          */
438         spinlock_t is_ir_lock;  /* SIO_IE[SC] mod lock */
439 };
440
441 /* Local port info for each IOC4 serial ports */
442 struct ioc4_port {
443         struct uart_port *ip_port;
444         /* Back ptrs for this port */
445         struct ioc4_control *ip_control;
446         struct pci_dev *ip_pdev;
447         struct ioc4_soft *ip_ioc4_soft;
448
449         /* pci mem addresses */
450         struct ioc4_misc_regs __iomem *ip_mem;
451         struct ioc4_serial __iomem *ip_serial;
452         struct ioc4_serialregs __iomem *ip_serial_regs;
453         struct ioc4_uartregs __iomem *ip_uart_regs;
454
455         /* Ring buffer page for this port */
456         dma_addr_t ip_dma_ringbuf;
457         /* vaddr of ring buffer */
458         struct ring_buffer *ip_cpu_ringbuf;
459
460         /* Rings for this port */
461         struct ring *ip_inring;
462         struct ring *ip_outring;
463
464         /* Hook to port specific values */
465         struct hooks *ip_hooks;
466
467         spinlock_t ip_lock;
468
469         /* Various rx/tx parameters */
470         int ip_baud;
471         int ip_tx_lowat;
472         int ip_rx_timeout;
473
474         /* Copy of notification bits */
475         int ip_notify;
476
477         /* Shadow copies of various registers so we don't need to PIO
478          * read them constantly
479          */
480         uint32_t ip_ienb;       /* Enabled interrupts */
481         uint32_t ip_sscr;
482         uint32_t ip_tx_prod;
483         uint32_t ip_rx_cons;
484         int ip_pci_bus_speed;
485         unsigned char ip_flags;
486 };
487
488 /* tx low water mark.  We need to notify the driver whenever tx is getting
489  * close to empty so it can refill the tx buffer and keep things going.
490  * Let's assume that if we interrupt 1 ms before the tx goes idle, we'll
491  * have no trouble getting in more chars in time (I certainly hope so).
492  */
493 #define TX_LOWAT_LATENCY      1000
494 #define TX_LOWAT_HZ          (1000000 / TX_LOWAT_LATENCY)
495 #define TX_LOWAT_CHARS(baud) (baud / 10 / TX_LOWAT_HZ)
496
497 /* Flags per port */
498 #define INPUT_HIGH      0x01
499 #define DCD_ON          0x02
500 #define LOWAT_WRITTEN   0x04
501 #define READ_ABORTED    0x08
502
503 /* Since each port has different register offsets and bitmasks
504  * for everything, we'll store those that we need in tables so we
505  * don't have to be constantly checking the port we are dealing with.
506  */
507 struct hooks {
508         uint32_t intr_delta_dcd;
509         uint32_t intr_delta_cts;
510         uint32_t intr_tx_mt;
511         uint32_t intr_rx_timer;
512         uint32_t intr_rx_high;
513         uint32_t intr_tx_explicit;
514         uint32_t intr_dma_error;
515         uint32_t intr_clear;
516         uint32_t intr_all;
517         int rs422_select_pin;
518 };
519
520 static struct hooks hooks_array[IOC4_NUM_SERIAL_PORTS] = {
521         /* Values for port 0 */
522         {
523          IOC4_SIO_IR_S0_DELTA_DCD, IOC4_SIO_IR_S0_DELTA_CTS,
524          IOC4_SIO_IR_S0_TX_MT, IOC4_SIO_IR_S0_RX_TIMER,
525          IOC4_SIO_IR_S0_RX_HIGH, IOC4_SIO_IR_S0_TX_EXPLICIT,
526          IOC4_OTHER_IR_S0_MEMERR,
527          (IOC4_SIO_IR_S0_TX_MT | IOC4_SIO_IR_S0_RX_FULL |
528           IOC4_SIO_IR_S0_RX_HIGH | IOC4_SIO_IR_S0_RX_TIMER |
529           IOC4_SIO_IR_S0_DELTA_DCD | IOC4_SIO_IR_S0_DELTA_CTS |
530           IOC4_SIO_IR_S0_INT | IOC4_SIO_IR_S0_TX_EXPLICIT),
531          IOC4_SIO_IR_S0, IOC4_GPPR_UART0_MODESEL_PIN,
532          },
533
534         /* Values for port 1 */
535         {
536          IOC4_SIO_IR_S1_DELTA_DCD, IOC4_SIO_IR_S1_DELTA_CTS,
537          IOC4_SIO_IR_S1_TX_MT, IOC4_SIO_IR_S1_RX_TIMER,
538          IOC4_SIO_IR_S1_RX_HIGH, IOC4_SIO_IR_S1_TX_EXPLICIT,
539          IOC4_OTHER_IR_S1_MEMERR,
540          (IOC4_SIO_IR_S1_TX_MT | IOC4_SIO_IR_S1_RX_FULL |
541           IOC4_SIO_IR_S1_RX_HIGH | IOC4_SIO_IR_S1_RX_TIMER |
542           IOC4_SIO_IR_S1_DELTA_DCD | IOC4_SIO_IR_S1_DELTA_CTS |
543           IOC4_SIO_IR_S1_INT | IOC4_SIO_IR_S1_TX_EXPLICIT),
544          IOC4_SIO_IR_S1, IOC4_GPPR_UART1_MODESEL_PIN,
545          },
546
547         /* Values for port 2 */
548         {
549          IOC4_SIO_IR_S2_DELTA_DCD, IOC4_SIO_IR_S2_DELTA_CTS,
550          IOC4_SIO_IR_S2_TX_MT, IOC4_SIO_IR_S2_RX_TIMER,
551          IOC4_SIO_IR_S2_RX_HIGH, IOC4_SIO_IR_S2_TX_EXPLICIT,
552          IOC4_OTHER_IR_S2_MEMERR,
553          (IOC4_SIO_IR_S2_TX_MT | IOC4_SIO_IR_S2_RX_FULL |
554           IOC4_SIO_IR_S2_RX_HIGH | IOC4_SIO_IR_S2_RX_TIMER |
555           IOC4_SIO_IR_S2_DELTA_DCD | IOC4_SIO_IR_S2_DELTA_CTS |
556           IOC4_SIO_IR_S2_INT | IOC4_SIO_IR_S2_TX_EXPLICIT),
557          IOC4_SIO_IR_S2, IOC4_GPPR_UART2_MODESEL_PIN,
558          },
559
560         /* Values for port 3 */
561         {
562          IOC4_SIO_IR_S3_DELTA_DCD, IOC4_SIO_IR_S3_DELTA_CTS,
563          IOC4_SIO_IR_S3_TX_MT, IOC4_SIO_IR_S3_RX_TIMER,
564          IOC4_SIO_IR_S3_RX_HIGH, IOC4_SIO_IR_S3_TX_EXPLICIT,
565          IOC4_OTHER_IR_S3_MEMERR,
566          (IOC4_SIO_IR_S3_TX_MT | IOC4_SIO_IR_S3_RX_FULL |
567           IOC4_SIO_IR_S3_RX_HIGH | IOC4_SIO_IR_S3_RX_TIMER |
568           IOC4_SIO_IR_S3_DELTA_DCD | IOC4_SIO_IR_S3_DELTA_CTS |
569           IOC4_SIO_IR_S3_INT | IOC4_SIO_IR_S3_TX_EXPLICIT),
570          IOC4_SIO_IR_S3, IOC4_GPPR_UART3_MODESEL_PIN,
571          }
572 };
573
574 /* A ring buffer entry */
575 struct ring_entry {
576         union {
577                 struct {
578                         uint32_t alldata;
579                         uint32_t allsc;
580                 } all;
581                 struct {
582                         char data[4];   /* data bytes */
583                         char sc[4];     /* status/control */
584                 } s;
585         } u;
586 };
587
588 /* Test the valid bits in any of the 4 sc chars using "allsc" member */
589 #define RING_ANY_VALID \
590         ((uint32_t)(IOC4_RXSB_MODEM_VALID | IOC4_RXSB_DATA_VALID) * 0x01010101)
591
592 #define ring_sc     u.s.sc
593 #define ring_data   u.s.data
594 #define ring_allsc  u.all.allsc
595
596 /* Number of entries per ring buffer. */
597 #define ENTRIES_PER_RING (RING_BUF_SIZE / (int) sizeof(struct ring_entry))
598
599 /* An individual ring */
600 struct ring {
601         struct ring_entry entries[ENTRIES_PER_RING];
602 };
603
604 /* The whole enchilada */
605 struct ring_buffer {
606         struct ring TX_0_OR_2;
607         struct ring RX_0_OR_2;
608         struct ring TX_1_OR_3;
609         struct ring RX_1_OR_3;
610 };
611
612 /* Get a ring from a port struct */
613 #define RING(_p, _wh)   &(((struct ring_buffer *)((_p)->ip_cpu_ringbuf))->_wh)
614
615 /* Infinite loop detection.
616  */
617 #define MAXITER 10000000
618
619 /* Prototypes */
620 static void receive_chars(struct uart_port *);
621 static void handle_intr(void *arg, uint32_t sio_ir);
622
623 /**
624  * write_ireg - write the interrupt regs
625  * @ioc4_soft: ptr to soft struct for this port
626  * @val: value to write
627  * @which: which register
628  * @type: which ireg set
629  */
630 static inline void
631 write_ireg(struct ioc4_soft *ioc4_soft, uint32_t val, int which, int type)
632 {
633         struct ioc4_misc_regs __iomem *mem = ioc4_soft->is_ioc4_misc_addr;
634         unsigned long flags;
635
636         spin_lock_irqsave(&ioc4_soft->is_ir_lock, flags);
637
638         switch (type) {
639         case IOC4_SIO_INTR_TYPE:
640                 switch (which) {
641                 case IOC4_W_IES:
642                         writel(val, &mem->sio_ies.raw);
643                         break;
644
645                 case IOC4_W_IEC:
646                         writel(val, &mem->sio_iec.raw);
647                         break;
648                 }
649                 break;
650
651         case IOC4_OTHER_INTR_TYPE:
652                 switch (which) {
653                 case IOC4_W_IES:
654                         writel(val, &mem->other_ies.raw);
655                         break;
656
657                 case IOC4_W_IEC:
658                         writel(val, &mem->other_iec.raw);
659                         break;
660                 }
661                 break;
662
663         default:
664                 break;
665         }
666         spin_unlock_irqrestore(&ioc4_soft->is_ir_lock, flags);
667 }
668
669 /**
670  * set_baud - Baud rate setting code
671  * @port: port to set
672  * @baud: baud rate to use
673  */
674 static int set_baud(struct ioc4_port *port, int baud)
675 {
676         int actual_baud;
677         int diff;
678         int lcr;
679         unsigned short divisor;
680         struct ioc4_uartregs __iomem *uart;
681
682         divisor = SER_DIVISOR(baud, port->ip_pci_bus_speed);
683         if (!divisor)
684                 return 1;
685         actual_baud = DIVISOR_TO_BAUD(divisor, port->ip_pci_bus_speed);
686
687         diff = actual_baud - baud;
688         if (diff < 0)
689                 diff = -diff;
690
691         /* If we're within 1%, we've found a match */
692         if (diff * 100 > actual_baud)
693                 return 1;
694
695         uart = port->ip_uart_regs;
696         lcr = readb(&uart->i4u_lcr);
697         writeb(lcr | UART_LCR_DLAB, &uart->i4u_lcr);
698         writeb((unsigned char)divisor, &uart->i4u_dll);
699         writeb((unsigned char)(divisor >> 8), &uart->i4u_dlm);
700         writeb(lcr, &uart->i4u_lcr);
701         return 0;
702 }
703
704
705 /**
706  * get_ioc4_port - given a uart port, return the control structure
707  * @port: uart port
708  */
709 static struct ioc4_port *get_ioc4_port(struct uart_port *the_port)
710 {
711         struct ioc4_driver_data *idd = dev_get_drvdata(the_port->dev);
712         struct ioc4_control *control = idd->idd_serial_data;
713         int ii;
714
715         if (control) {
716                 for ( ii = 0; ii < IOC4_NUM_SERIAL_PORTS; ii++ ) {
717                         if (!control->ic_port[ii].icp_port)
718                                 continue;
719                         if (the_port == control->ic_port[ii].icp_port->ip_port)
720                                 return control->ic_port[ii].icp_port;
721                 }
722         }
723         return NULL;
724 }
725
726 /* The IOC4 hardware provides no atomic way to determine if interrupts
727  * are pending since two reads are required to do so.  The handler must
728  * read the SIO_IR and the SIO_IES, and take the logical and of the
729  * two.  When this value is zero, all interrupts have been serviced and
730  * the handler may return.
731  *
732  * This has the unfortunate "hole" that, if some other CPU or
733  * some other thread or some higher level interrupt manages to
734  * modify SIO_IE between our reads of SIO_IR and SIO_IE, we may
735  * think we have observed SIO_IR&SIO_IE==0 when in fact this
736  * condition never really occurred.
737  *
738  * To solve this, we use a simple spinlock that must be held
739  * whenever modifying SIO_IE; holding this lock while observing
740  * both SIO_IR and SIO_IE guarantees that we do not falsely
741  * conclude that no enabled interrupts are pending.
742  */
743
744 static inline uint32_t
745 pending_intrs(struct ioc4_soft *soft, int type)
746 {
747         struct ioc4_misc_regs __iomem *mem = soft->is_ioc4_misc_addr;
748         unsigned long flag;
749         uint32_t intrs = 0;
750
751         BUG_ON(!((type == IOC4_SIO_INTR_TYPE)
752                || (type == IOC4_OTHER_INTR_TYPE)));
753
754         spin_lock_irqsave(&soft->is_ir_lock, flag);
755
756         switch (type) {
757         case IOC4_SIO_INTR_TYPE:
758                 intrs = readl(&mem->sio_ir.raw) & readl(&mem->sio_ies.raw);
759                 break;
760
761         case IOC4_OTHER_INTR_TYPE:
762                 intrs = readl(&mem->other_ir.raw) & readl(&mem->other_ies.raw);
763
764                 /* Don't process any ATA interrupte */
765                 intrs &= ~(IOC4_OTHER_IR_ATA_INT | IOC4_OTHER_IR_ATA_MEMERR);
766                 break;
767
768         default:
769                 break;
770         }
771         spin_unlock_irqrestore(&soft->is_ir_lock, flag);
772         return intrs;
773 }
774
775 /**
776  * port_init - Initialize the sio and ioc4 hardware for a given port
777  *                      called per port from attach...
778  * @port: port to initialize
779  */
780 static int inline port_init(struct ioc4_port *port)
781 {
782         uint32_t sio_cr;
783         struct hooks *hooks = port->ip_hooks;
784         struct ioc4_uartregs __iomem *uart;
785
786         /* Idle the IOC4 serial interface */
787         writel(IOC4_SSCR_RESET, &port->ip_serial_regs->sscr);
788
789         /* Wait until any pending bus activity for this port has ceased */
790         do
791                 sio_cr = readl(&port->ip_mem->sio_cr.raw);
792         while (!(sio_cr & IOC4_SIO_CR_SIO_DIAG_IDLE));
793
794         /* Finish reset sequence */
795         writel(0, &port->ip_serial_regs->sscr);
796
797         /* Once RESET is done, reload cached tx_prod and rx_cons values
798          * and set rings to empty by making prod == cons
799          */
800         port->ip_tx_prod = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
801         writel(port->ip_tx_prod, &port->ip_serial_regs->stpir);
802         port->ip_rx_cons = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
803         writel(port->ip_rx_cons | IOC4_SRCIR_ARM, &port->ip_serial_regs->srcir);
804
805         /* Disable interrupts for this 16550 */
806         uart = port->ip_uart_regs;
807         writeb(0, &uart->i4u_lcr);
808         writeb(0, &uart->i4u_ier);
809
810         /* Set the default baud */
811         set_baud(port, port->ip_baud);
812
813         /* Set line control to 8 bits no parity */
814         writeb(UART_LCR_WLEN8 | 0, &uart->i4u_lcr);
815                                         /* UART_LCR_STOP == 1 stop */
816
817         /* Enable the FIFOs */
818         writeb(UART_FCR_ENABLE_FIFO, &uart->i4u_fcr);
819         /* then reset 16550 FIFOs */
820         writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
821                         &uart->i4u_fcr);
822
823         /* Clear modem control register */
824         writeb(0, &uart->i4u_mcr);
825
826         /* Clear deltas in modem status register */
827         readb(&uart->i4u_msr);
828
829         /* Only do this once per port pair */
830         if (port->ip_hooks == &hooks_array[0]
831                             || port->ip_hooks == &hooks_array[2]) {
832                 unsigned long ring_pci_addr;
833                 uint32_t __iomem *sbbr_l;
834                 uint32_t __iomem *sbbr_h;
835
836                 if (port->ip_hooks == &hooks_array[0]) {
837                         sbbr_l = &port->ip_serial->sbbr01_l;
838                         sbbr_h = &port->ip_serial->sbbr01_h;
839                 } else {
840                         sbbr_l = &port->ip_serial->sbbr23_l;
841                         sbbr_h = &port->ip_serial->sbbr23_h;
842                 }
843
844                 ring_pci_addr = (unsigned long __iomem)port->ip_dma_ringbuf;
845                 DPRINT_CONFIG(("%s: ring_pci_addr 0x%lx\n",
846                                         __FUNCTION__, ring_pci_addr));
847
848                 writel((unsigned int)((uint64_t)ring_pci_addr >> 32), sbbr_h);
849                 writel((unsigned int)ring_pci_addr | IOC4_BUF_SIZE_BIT, sbbr_l);
850         }
851
852         /* Set the receive timeout value to 10 msec */
853         writel(IOC4_SRTR_HZ / 100, &port->ip_serial_regs->srtr);
854
855         /* Set rx threshold, enable DMA */
856         /* Set high water mark at 3/4 of full ring */
857         port->ip_sscr = (ENTRIES_PER_RING * 3 / 4);
858         writel(port->ip_sscr, &port->ip_serial_regs->sscr);
859
860         /* Disable and clear all serial related interrupt bits */
861         write_ireg(port->ip_ioc4_soft, hooks->intr_clear,
862                        IOC4_W_IEC, IOC4_SIO_INTR_TYPE);
863         port->ip_ienb &= ~hooks->intr_clear;
864         writel(hooks->intr_clear, &port->ip_mem->sio_ir.raw);
865         return 0;
866 }
867
868 /**
869  * handle_dma_error_intr - service any pending DMA error interrupts for the
870  *                      given port - 2nd level called via sd_intr
871  * @arg: handler arg
872  * @other_ir: ioc4regs
873  */
874 static void handle_dma_error_intr(void *arg, uint32_t other_ir)
875 {
876         struct ioc4_port *port = (struct ioc4_port *)arg;
877         struct hooks *hooks = port->ip_hooks;
878         unsigned int flags;
879
880         spin_lock_irqsave(&port->ip_lock, flags);
881
882         /* ACK the interrupt */
883         writel(hooks->intr_dma_error, &port->ip_mem->other_ir.raw);
884
885         if (readl(&port->ip_mem->pci_err_addr_l.raw) & IOC4_PCI_ERR_ADDR_VLD) {
886                 printk(KERN_ERR
887                         "PCI error address is 0x%lx, "
888                                 "master is serial port %c %s\n",
889                      (((uint64_t)readl(&port->ip_mem->pci_err_addr_h)
890                                                          << 32)
891                                 | readl(&port->ip_mem->pci_err_addr_l.raw))
892                                         & IOC4_PCI_ERR_ADDR_ADDR_MSK, '1' +
893                      ((char)(readl(&port->ip_mem->pci_err_addr_l.raw) &
894                              IOC4_PCI_ERR_ADDR_MST_NUM_MSK) >> 1),
895                      (readl(&port->ip_mem->pci_err_addr_l.raw)
896                                 & IOC4_PCI_ERR_ADDR_MST_TYP_MSK)
897                                 ? "RX" : "TX");
898
899                 if (readl(&port->ip_mem->pci_err_addr_l.raw)
900                                                 & IOC4_PCI_ERR_ADDR_MUL_ERR) {
901                         printk(KERN_ERR
902                                 "Multiple errors occurred\n");
903                 }
904         }
905         spin_unlock_irqrestore(&port->ip_lock, flags);
906
907         /* Re-enable DMA error interrupts */
908         write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error, IOC4_W_IES,
909                                                 IOC4_OTHER_INTR_TYPE);
910 }
911
912 /**
913  * intr_connect - interrupt connect function
914  * @soft: soft struct for this card
915  * @type: interrupt type
916  * @intrbits: bit pattern to set
917  * @intr: handler function
918  * @info: handler arg
919  */
920 static void
921 intr_connect(struct ioc4_soft *soft, int type,
922                   uint32_t intrbits, ioc4_intr_func_f * intr, void *info)
923 {
924         int i;
925         struct ioc4_intr_info *intr_ptr;
926
927         BUG_ON(!((type == IOC4_SIO_INTR_TYPE)
928                || (type == IOC4_OTHER_INTR_TYPE)));
929
930         i = atomic_inc(&soft-> is_intr_type[type].is_num_intrs) - 1;
931         BUG_ON(!(i < MAX_IOC4_INTR_ENTS || (printk("i %d\n", i), 0)));
932
933         /* Save off the lower level interrupt handler */
934         intr_ptr = &soft->is_intr_type[type].is_intr_info[i];
935         intr_ptr->sd_bits = intrbits;
936         intr_ptr->sd_intr = intr;
937         intr_ptr->sd_info = info;
938 }
939
940 /**
941  * ioc4_intr - Top level IOC4 interrupt handler.
942  * @irq: irq value
943  * @arg: handler arg
944  * @regs: registers
945  */
946 static irqreturn_t ioc4_intr(int irq, void *arg, struct pt_regs *regs)
947 {
948         struct ioc4_soft *soft;
949         uint32_t this_ir, this_mir;
950         int xx, num_intrs = 0;
951         int intr_type;
952         int handled = 0;
953         struct ioc4_intr_info *ii;
954
955         soft = arg;
956         for (intr_type = 0; intr_type < IOC4_NUM_INTR_TYPES; intr_type++) {
957                 num_intrs = (int)atomic_read(
958                                 &soft->is_intr_type[intr_type].is_num_intrs);
959
960                 this_mir = this_ir = pending_intrs(soft, intr_type);
961
962                 /* Farm out the interrupt to the various drivers depending on
963                  * which interrupt bits are set.
964                  */
965                 for (xx = 0; xx < num_intrs; xx++) {
966                         ii = &soft->is_intr_type[intr_type].is_intr_info[xx];
967                         if ((this_mir = this_ir & ii->sd_bits)) {
968                                 /* Disable owned interrupts, call handler */
969                                 handled++;
970                                 write_ireg(soft, ii->sd_bits, IOC4_W_IEC,
971                                                                 intr_type);
972                                 ii->sd_intr(ii->sd_info, this_mir);
973                                 this_ir &= ~this_mir;
974                         }
975                 }
976                 if (this_ir) {
977                         printk(KERN_ERR
978                                "unknown IOC4 %s interrupt 0x%x, sio_ir = 0x%x,"
979                                 " sio_ies = 0x%x, other_ir = 0x%x :"
980                                 "other_ies = 0x%x\n",
981                                (intr_type == IOC4_SIO_INTR_TYPE) ? "sio" :
982                                "other", this_ir,
983                                readl(&soft->is_ioc4_misc_addr->sio_ir.raw),
984                                readl(&soft->is_ioc4_misc_addr->sio_ies.raw),
985                                readl(&soft->is_ioc4_misc_addr->other_ir.raw),
986                                readl(&soft->is_ioc4_misc_addr->other_ies.raw));
987                 }
988         }
989 #ifdef DEBUG_INTERRUPTS
990         {
991                 struct ioc4_misc_regs __iomem *mem = soft->is_ioc4_misc_addr;
992                 spinlock_t *lp = &soft->is_ir_lock;
993                 unsigned long flag;
994
995                 spin_lock_irqsave(&soft->is_ir_lock, flag);
996                 printk ("%s : %d : mem 0x%p sio_ir 0x%x sio_ies 0x%x "
997                                 "other_ir 0x%x other_ies 0x%x mask 0x%x\n",
998                      __FUNCTION__, __LINE__,
999                      (void *)mem, readl(&mem->sio_ir.raw),
1000                      readl(&mem->sio_ies.raw),
1001                      readl(&mem->other_ir.raw),
1002                      readl(&mem->other_ies.raw),
1003                      IOC4_OTHER_IR_ATA_INT | IOC4_OTHER_IR_ATA_MEMERR);
1004                 spin_unlock_irqrestore(&soft->is_ir_lock, flag);
1005         }
1006 #endif
1007         return handled ? IRQ_HANDLED : IRQ_NONE;
1008 }
1009
1010 /**
1011  * ioc4_attach_local - Device initialization.
1012  *                      Called at *_attach() time for each
1013  *                      IOC4 with serial ports in the system.
1014  * @idd: Master module data for this IOC4
1015  */
1016 static int inline ioc4_attach_local(struct ioc4_driver_data *idd)
1017 {
1018         struct ioc4_port *port;
1019         struct ioc4_port *ports[IOC4_NUM_SERIAL_PORTS];
1020         int port_number;
1021         uint16_t ioc4_revid_min = 62;
1022         uint16_t ioc4_revid;
1023         struct pci_dev *pdev = idd->idd_pdev;
1024         struct ioc4_control* control = idd->idd_serial_data;
1025         struct ioc4_soft *soft = control->ic_soft;
1026         void __iomem *ioc4_misc = idd->idd_misc_regs;
1027         void __iomem *ioc4_serial = soft->is_ioc4_serial_addr;
1028
1029         /* IOC4 firmware must be at least rev 62 */
1030         pci_read_config_word(pdev, PCI_COMMAND_SPECIAL, &ioc4_revid);
1031
1032         printk(KERN_INFO "IOC4 firmware revision %d\n", ioc4_revid);
1033         if (ioc4_revid < ioc4_revid_min) {
1034                 printk(KERN_WARNING
1035                     "IOC4 serial not supported on firmware rev %d, "
1036                                 "please upgrade to rev %d or higher\n",
1037                                 ioc4_revid, ioc4_revid_min);
1038                 return -EPERM;
1039         }
1040         BUG_ON(ioc4_misc == NULL);
1041         BUG_ON(ioc4_serial == NULL);
1042
1043         /* Create port structures for each port */
1044         for (port_number = 0; port_number < IOC4_NUM_SERIAL_PORTS;
1045                                                         port_number++) {
1046                 port = kmalloc(sizeof(struct ioc4_port), GFP_KERNEL);
1047                 if (!port) {
1048                         printk(KERN_WARNING
1049                                 "IOC4 serial memory not available for port\n");
1050                         return -ENOMEM;
1051                 }
1052                 memset(port, 0, sizeof(struct ioc4_port));
1053
1054                 /* we need to remember the previous ones, to point back to
1055                  * them farther down - setting up the ring buffers.
1056                  */
1057                 ports[port_number] = port;
1058
1059                 /* Allocate buffers and jumpstart the hardware.  */
1060                 control->ic_port[port_number].icp_port = port;
1061                 port->ip_ioc4_soft = soft;
1062                 port->ip_pdev = pdev;
1063                 port->ip_ienb = 0;
1064                 /* Use baud rate calculations based on detected PCI
1065                  * bus speed.  Simply test whether the PCI clock is
1066                  * running closer to 66MHz or 33MHz.
1067                  */
1068                 if (idd->count_period/IOC4_EXTINT_COUNT_DIVISOR < 20) {
1069                         port->ip_pci_bus_speed = IOC4_SER_XIN_CLK_66;
1070                 } else {
1071                         port->ip_pci_bus_speed = IOC4_SER_XIN_CLK_33;
1072                 }
1073                 port->ip_baud = 9600;
1074                 port->ip_control = control;
1075                 port->ip_mem = ioc4_misc;
1076                 port->ip_serial = ioc4_serial;
1077
1078                 /* point to the right hook */
1079                 port->ip_hooks = &hooks_array[port_number];
1080
1081                 /* Get direct hooks to the serial regs and uart regs
1082                  * for this port
1083                  */
1084                 switch (port_number) {
1085                 case 0:
1086                         port->ip_serial_regs = &(port->ip_serial->port_0);
1087                         port->ip_uart_regs = &(port->ip_serial->uart_0);
1088                         break;
1089                 case 1:
1090                         port->ip_serial_regs = &(port->ip_serial->port_1);
1091                         port->ip_uart_regs = &(port->ip_serial->uart_1);
1092                         break;
1093                 case 2:
1094                         port->ip_serial_regs = &(port->ip_serial->port_2);
1095                         port->ip_uart_regs = &(port->ip_serial->uart_2);
1096                         break;
1097                 default:
1098                 case 3:
1099                         port->ip_serial_regs = &(port->ip_serial->port_3);
1100                         port->ip_uart_regs = &(port->ip_serial->uart_3);
1101                         break;
1102                 }
1103
1104                 /* ring buffers are 1 to a pair of ports */
1105                 if (port_number && (port_number & 1)) {
1106                         /* odd use the evens buffer */
1107                         port->ip_dma_ringbuf =
1108                                         ports[port_number - 1]->ip_dma_ringbuf;
1109                         port->ip_cpu_ringbuf =
1110                                         ports[port_number - 1]->ip_cpu_ringbuf;
1111                         port->ip_inring = RING(port, RX_1_OR_3);
1112                         port->ip_outring = RING(port, TX_1_OR_3);
1113
1114                 } else {
1115                         if (port->ip_dma_ringbuf == 0) {
1116                                 port->ip_cpu_ringbuf = pci_alloc_consistent
1117                                         (pdev, TOTAL_RING_BUF_SIZE,
1118                                         &port->ip_dma_ringbuf);
1119
1120                         }
1121                         BUG_ON(!((((int64_t)port->ip_dma_ringbuf) &
1122                                 (TOTAL_RING_BUF_SIZE - 1)) == 0));
1123                         DPRINT_CONFIG(("%s : ip_cpu_ringbuf 0x%p "
1124                                                 "ip_dma_ringbuf 0x%p\n",
1125                                         __FUNCTION__,
1126                                         (void *)port->ip_cpu_ringbuf,
1127                                         (void *)port->ip_dma_ringbuf));
1128                         port->ip_inring = RING(port, RX_0_OR_2);
1129                         port->ip_outring = RING(port, TX_0_OR_2);
1130                 }
1131                 DPRINT_CONFIG(("%s : port %d [addr 0x%p] control 0x%p",
1132                                 __FUNCTION__,
1133                                 port_number, (void *)port, (void *)control));
1134                 DPRINT_CONFIG((" ip_serial_regs 0x%p ip_uart_regs 0x%p\n",
1135                                 (void *)port->ip_serial_regs,
1136                                 (void *)port->ip_uart_regs));
1137
1138                 /* Initialize the hardware for IOC4 */
1139                 port_init(port);
1140
1141                 DPRINT_CONFIG(("%s: port_number %d port 0x%p inring 0x%p "
1142                                                 "outring 0x%p\n",
1143                                 __FUNCTION__,
1144                                 port_number, (void *)port,
1145                                 (void *)port->ip_inring,
1146                                 (void *)port->ip_outring));
1147
1148                 /* Attach interrupt handlers */
1149                 intr_connect(soft, IOC4_SIO_INTR_TYPE,
1150                                 GET_SIO_IR(port_number),
1151                                 handle_intr, port);
1152
1153                 intr_connect(soft, IOC4_OTHER_INTR_TYPE,
1154                                 GET_OTHER_IR(port_number),
1155                                 handle_dma_error_intr, port);
1156         }
1157         return 0;
1158 }
1159
1160 /**
1161  * enable_intrs - enable interrupts
1162  * @port: port to enable
1163  * @mask: mask to use
1164  */
1165 static void enable_intrs(struct ioc4_port *port, uint32_t mask)
1166 {
1167         struct hooks *hooks = port->ip_hooks;
1168
1169         if ((port->ip_ienb & mask) != mask) {
1170                 write_ireg(port->ip_ioc4_soft, mask, IOC4_W_IES,
1171                                                 IOC4_SIO_INTR_TYPE);
1172                 port->ip_ienb |= mask;
1173         }
1174
1175         if (port->ip_ienb)
1176                 write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error,
1177                                 IOC4_W_IES, IOC4_OTHER_INTR_TYPE);
1178 }
1179
1180 /**
1181  * local_open - local open a port
1182  * @port: port to open
1183  */
1184 static inline int local_open(struct ioc4_port *port)
1185 {
1186         int spiniter = 0;
1187
1188         port->ip_flags = 0;
1189
1190         /* Pause the DMA interface if necessary */
1191         if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1192                 writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
1193                         &port->ip_serial_regs->sscr);
1194                 while((readl(&port->ip_serial_regs-> sscr)
1195                                 & IOC4_SSCR_PAUSE_STATE) == 0) {
1196                         spiniter++;
1197                         if (spiniter > MAXITER) {
1198                                 return -1;
1199                         }
1200                 }
1201         }
1202
1203         /* Reset the input fifo.  If the uart received chars while the port
1204          * was closed and DMA is not enabled, the uart may have a bunch of
1205          * chars hanging around in its rx fifo which will not be discarded
1206          * by rclr in the upper layer. We must get rid of them here.
1207          */
1208         writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR,
1209                                 &port->ip_uart_regs->i4u_fcr);
1210
1211         writeb(UART_LCR_WLEN8, &port->ip_uart_regs->i4u_lcr);
1212                                         /* UART_LCR_STOP == 1 stop */
1213
1214         /* Re-enable DMA, set default threshold to intr whenever there is
1215          * data available.
1216          */
1217         port->ip_sscr &= ~IOC4_SSCR_RX_THRESHOLD;
1218         port->ip_sscr |= 1;     /* default threshold */
1219
1220         /* Plug in the new sscr.  This implicitly clears the DMA_PAUSE
1221          * flag if it was set above
1222          */
1223         writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1224         port->ip_tx_lowat = 1;
1225         return 0;
1226 }
1227
1228 /**
1229  * set_rx_timeout - Set rx timeout and threshold values.
1230  * @port: port to use
1231  * @timeout: timeout value in ticks
1232  */
1233 static inline int set_rx_timeout(struct ioc4_port *port, int timeout)
1234 {
1235         int threshold;
1236
1237         port->ip_rx_timeout = timeout;
1238
1239         /* Timeout is in ticks.  Let's figure out how many chars we
1240          * can receive at the current baud rate in that interval
1241          * and set the rx threshold to that amount.  There are 4 chars
1242          * per ring entry, so we'll divide the number of chars that will
1243          * arrive in timeout by 4.
1244          * So .... timeout * baud / 10 / HZ / 4, with HZ = 100.
1245          */
1246         threshold = timeout * port->ip_baud / 4000;
1247         if (threshold == 0)
1248                 threshold = 1;  /* otherwise we'll intr all the time! */
1249
1250         if ((unsigned)threshold > (unsigned)IOC4_SSCR_RX_THRESHOLD)
1251                 return 1;
1252
1253         port->ip_sscr &= ~IOC4_SSCR_RX_THRESHOLD;
1254         port->ip_sscr |= threshold;
1255
1256         writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1257
1258         /* Now set the rx timeout to the given value
1259          * again timeout * IOC4_SRTR_HZ / HZ
1260          */
1261         timeout = timeout * IOC4_SRTR_HZ / 100;
1262         if (timeout > IOC4_SRTR_CNT)
1263                 timeout = IOC4_SRTR_CNT;
1264
1265         writel(timeout, &port->ip_serial_regs->srtr);
1266         return 0;
1267 }
1268
1269 /**
1270  * config_port - config the hardware
1271  * @port: port to config
1272  * @baud: baud rate for the port
1273  * @byte_size: data size
1274  * @stop_bits: number of stop bits
1275  * @parenb: parity enable ?
1276  * @parodd: odd parity ?
1277  */
1278 static inline int
1279 config_port(struct ioc4_port *port,
1280             int baud, int byte_size, int stop_bits, int parenb, int parodd)
1281 {
1282         char lcr, sizebits;
1283         int spiniter = 0;
1284
1285         DPRINT_CONFIG(("%s: baud %d byte_size %d stop %d parenb %d parodd %d\n",
1286                 __FUNCTION__, baud, byte_size, stop_bits, parenb, parodd));
1287
1288         if (set_baud(port, baud))
1289                 return 1;
1290
1291         switch (byte_size) {
1292         case 5:
1293                 sizebits = UART_LCR_WLEN5;
1294                 break;
1295         case 6:
1296                 sizebits = UART_LCR_WLEN6;
1297                 break;
1298         case 7:
1299                 sizebits = UART_LCR_WLEN7;
1300                 break;
1301         case 8:
1302                 sizebits = UART_LCR_WLEN8;
1303                 break;
1304         default:
1305                 return 1;
1306         }
1307
1308         /* Pause the DMA interface if necessary */
1309         if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1310                 writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
1311                         &port->ip_serial_regs->sscr);
1312                 while((readl(&port->ip_serial_regs->sscr)
1313                                                 & IOC4_SSCR_PAUSE_STATE) == 0) {
1314                         spiniter++;
1315                         if (spiniter > MAXITER)
1316                                 return -1;
1317                 }
1318         }
1319
1320         /* Clear relevant fields in lcr */
1321         lcr = readb(&port->ip_uart_regs->i4u_lcr);
1322         lcr &= ~(LCR_MASK_BITS_CHAR | UART_LCR_EPAR |
1323                  UART_LCR_PARITY | LCR_MASK_STOP_BITS);
1324
1325         /* Set byte size in lcr */
1326         lcr |= sizebits;
1327
1328         /* Set parity */
1329         if (parenb) {
1330                 lcr |= UART_LCR_PARITY;
1331                 if (!parodd)
1332                         lcr |= UART_LCR_EPAR;
1333         }
1334
1335         /* Set stop bits */
1336         if (stop_bits)
1337                 lcr |= UART_LCR_STOP /* 2 stop bits */ ;
1338
1339         writeb(lcr, &port->ip_uart_regs->i4u_lcr);
1340
1341         /* Re-enable the DMA interface if necessary */
1342         if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1343                 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1344         }
1345         port->ip_baud = baud;
1346
1347         /* When we get within this number of ring entries of filling the
1348          * entire ring on tx, place an EXPLICIT intr to generate a lowat
1349          * notification when output has drained.
1350          */
1351         port->ip_tx_lowat = (TX_LOWAT_CHARS(baud) + 3) / 4;
1352         if (port->ip_tx_lowat == 0)
1353                 port->ip_tx_lowat = 1;
1354
1355         set_rx_timeout(port, 2);
1356
1357         return 0;
1358 }
1359
1360 /**
1361  * do_write - Write bytes to the port.  Returns the number of bytes
1362  *                      actually written. Called from transmit_chars
1363  * @port: port to use
1364  * @buf: the stuff to write
1365  * @len: how many bytes in 'buf'
1366  */
1367 static inline int do_write(struct ioc4_port *port, char *buf, int len)
1368 {
1369         int prod_ptr, cons_ptr, total = 0;
1370         struct ring *outring;
1371         struct ring_entry *entry;
1372         struct hooks *hooks = port->ip_hooks;
1373
1374         BUG_ON(!(len >= 0));
1375
1376         prod_ptr = port->ip_tx_prod;
1377         cons_ptr = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
1378         outring = port->ip_outring;
1379
1380         /* Maintain a 1-entry red-zone.  The ring buffer is full when
1381          * (cons - prod) % ring_size is 1.  Rather than do this subtraction
1382          * in the body of the loop, I'll do it now.
1383          */
1384         cons_ptr = (cons_ptr - (int)sizeof(struct ring_entry)) & PROD_CONS_MASK;
1385
1386         /* Stuff the bytes into the output */
1387         while ((prod_ptr != cons_ptr) && (len > 0)) {
1388                 int xx;
1389
1390                 /* Get 4 bytes (one ring entry) at a time */
1391                 entry = (struct ring_entry *)((caddr_t) outring + prod_ptr);
1392
1393                 /* Invalidate all entries */
1394                 entry->ring_allsc = 0;
1395
1396                 /* Copy in some bytes */
1397                 for (xx = 0; (xx < 4) && (len > 0); xx++) {
1398                         entry->ring_data[xx] = *buf++;
1399                         entry->ring_sc[xx] = IOC4_TXCB_VALID;
1400                         len--;
1401                         total++;
1402                 }
1403
1404                 /* If we are within some small threshold of filling up the
1405                  * entire ring buffer, we must place an EXPLICIT intr here
1406                  * to generate a lowat interrupt in case we subsequently
1407                  * really do fill up the ring and the caller goes to sleep.
1408                  * No need to place more than one though.
1409                  */
1410                 if (!(port->ip_flags & LOWAT_WRITTEN) &&
1411                         ((cons_ptr - prod_ptr) & PROD_CONS_MASK)
1412                                 <= port->ip_tx_lowat
1413                                         * (int)sizeof(struct ring_entry)) {
1414                         port->ip_flags |= LOWAT_WRITTEN;
1415                         entry->ring_sc[0] |= IOC4_TXCB_INT_WHEN_DONE;
1416                 }
1417
1418                 /* Go on to next entry */
1419                 prod_ptr += sizeof(struct ring_entry);
1420                 prod_ptr &= PROD_CONS_MASK;
1421         }
1422
1423         /* If we sent something, start DMA if necessary */
1424         if (total > 0 && !(port->ip_sscr & IOC4_SSCR_DMA_EN)) {
1425                 port->ip_sscr |= IOC4_SSCR_DMA_EN;
1426                 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1427         }
1428
1429         /* Store the new producer pointer.  If tx is disabled, we stuff the
1430          * data into the ring buffer, but we don't actually start tx.
1431          */
1432         if (!uart_tx_stopped(port->ip_port)) {
1433                 writel(prod_ptr, &port->ip_serial_regs->stpir);
1434
1435                 /* If we are now transmitting, enable tx_mt interrupt so we
1436                  * can disable DMA if necessary when the tx finishes.
1437                  */
1438                 if (total > 0)
1439                         enable_intrs(port, hooks->intr_tx_mt);
1440         }
1441         port->ip_tx_prod = prod_ptr;
1442         return total;
1443 }
1444
1445 /**
1446  * disable_intrs - disable interrupts
1447  * @port: port to enable
1448  * @mask: mask to use
1449  */
1450 static void disable_intrs(struct ioc4_port *port, uint32_t mask)
1451 {
1452         struct hooks *hooks = port->ip_hooks;
1453
1454         if (port->ip_ienb & mask) {
1455                 write_ireg(port->ip_ioc4_soft, mask, IOC4_W_IEC,
1456                                         IOC4_SIO_INTR_TYPE);
1457                 port->ip_ienb &= ~mask;
1458         }
1459
1460         if (!port->ip_ienb)
1461                 write_ireg(port->ip_ioc4_soft, hooks->intr_dma_error,
1462                                 IOC4_W_IEC, IOC4_OTHER_INTR_TYPE);
1463 }
1464
1465 /**
1466  * set_notification - Modify event notification
1467  * @port: port to use
1468  * @mask: events mask
1469  * @set_on: set ?
1470  */
1471 static int set_notification(struct ioc4_port *port, int mask, int set_on)
1472 {
1473         struct hooks *hooks = port->ip_hooks;
1474         uint32_t intrbits, sscrbits;
1475
1476         BUG_ON(!mask);
1477
1478         intrbits = sscrbits = 0;
1479
1480         if (mask & N_DATA_READY)
1481                 intrbits |= (hooks->intr_rx_timer | hooks->intr_rx_high);
1482         if (mask & N_OUTPUT_LOWAT)
1483                 intrbits |= hooks->intr_tx_explicit;
1484         if (mask & N_DDCD) {
1485                 intrbits |= hooks->intr_delta_dcd;
1486                 sscrbits |= IOC4_SSCR_RX_RING_DCD;
1487         }
1488         if (mask & N_DCTS)
1489                 intrbits |= hooks->intr_delta_cts;
1490
1491         if (set_on) {
1492                 enable_intrs(port, intrbits);
1493                 port->ip_notify |= mask;
1494                 port->ip_sscr |= sscrbits;
1495         } else {
1496                 disable_intrs(port, intrbits);
1497                 port->ip_notify &= ~mask;
1498                 port->ip_sscr &= ~sscrbits;
1499         }
1500
1501         /* We require DMA if either DATA_READY or DDCD notification is
1502          * currently requested. If neither of these is requested and
1503          * there is currently no tx in progress, DMA may be disabled.
1504          */
1505         if (port->ip_notify & (N_DATA_READY | N_DDCD))
1506                 port->ip_sscr |= IOC4_SSCR_DMA_EN;
1507         else if (!(port->ip_ienb & hooks->intr_tx_mt))
1508                 port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
1509
1510         writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1511         return 0;
1512 }
1513
1514 /**
1515  * set_mcr - set the master control reg
1516  * @the_port: port to use
1517  * @set: set ?
1518  * @mask1: mcr mask
1519  * @mask2: shadow mask
1520  */
1521 static inline int set_mcr(struct uart_port *the_port, int set,
1522                 int mask1, int mask2)
1523 {
1524         struct ioc4_port *port = get_ioc4_port(the_port);
1525         uint32_t shadow;
1526         int spiniter = 0;
1527         char mcr;
1528
1529         if (!port)
1530                 return -1;
1531
1532         /* Pause the DMA interface if necessary */
1533         if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1534                 writel(port->ip_sscr | IOC4_SSCR_DMA_PAUSE,
1535                         &port->ip_serial_regs->sscr);
1536                 while ((readl(&port->ip_serial_regs->sscr)
1537                                         & IOC4_SSCR_PAUSE_STATE) == 0) {
1538                         spiniter++;
1539                         if (spiniter > MAXITER)
1540                                 return -1;
1541                 }
1542         }
1543         shadow = readl(&port->ip_serial_regs->shadow);
1544         mcr = (shadow & 0xff000000) >> 24;
1545
1546         /* Set new value */
1547         if (set) {
1548                 mcr |= mask1;
1549                 shadow |= mask2;
1550         } else {
1551                 mcr &= ~mask1;
1552                 shadow &= ~mask2;
1553         }
1554         writeb(mcr, &port->ip_uart_regs->i4u_mcr);
1555         writel(shadow, &port->ip_serial_regs->shadow);
1556
1557         /* Re-enable the DMA interface if necessary */
1558         if (port->ip_sscr & IOC4_SSCR_DMA_EN) {
1559                 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1560         }
1561         return 0;
1562 }
1563
1564 /**
1565  * ioc4_set_proto - set the protocol for the port
1566  * @port: port to use
1567  * @proto: protocol to use
1568  */
1569 static int ioc4_set_proto(struct ioc4_port *port, enum sio_proto proto)
1570 {
1571         struct hooks *hooks = port->ip_hooks;
1572
1573         switch (proto) {
1574         case PROTO_RS232:
1575                 /* Clear the appropriate GIO pin */
1576                 writel(0, (&port->ip_mem->gppr[hooks->rs422_select_pin].raw));
1577                 break;
1578
1579         case PROTO_RS422:
1580                 /* Set the appropriate GIO pin */
1581                 writel(1, (&port->ip_mem->gppr[hooks->rs422_select_pin].raw));
1582                 break;
1583
1584         default:
1585                 return 1;
1586         }
1587         return 0;
1588 }
1589
1590 /**
1591  * transmit_chars - upper level write, called with ip_lock
1592  * @the_port: port to write
1593  */
1594 static void transmit_chars(struct uart_port *the_port)
1595 {
1596         int xmit_count, tail, head;
1597         int result;
1598         char *start;
1599         struct tty_struct *tty;
1600         struct ioc4_port *port = get_ioc4_port(the_port);
1601         struct uart_info *info;
1602
1603         if (!the_port)
1604                 return;
1605         if (!port)
1606                 return;
1607
1608         info = the_port->info;
1609         tty = info->tty;
1610
1611         if (uart_circ_empty(&info->xmit) || uart_tx_stopped(the_port)) {
1612                 /* Nothing to do or hw stopped */
1613                 set_notification(port, N_ALL_OUTPUT, 0);
1614                 return;
1615         }
1616
1617         head = info->xmit.head;
1618         tail = info->xmit.tail;
1619         start = (char *)&info->xmit.buf[tail];
1620
1621         /* write out all the data or until the end of the buffer */
1622         xmit_count = (head < tail) ? (UART_XMIT_SIZE - tail) : (head - tail);
1623         if (xmit_count > 0) {
1624                 result = do_write(port, start, xmit_count);
1625                 if (result > 0) {
1626                         /* booking */
1627                         xmit_count -= result;
1628                         the_port->icount.tx += result;
1629                         /* advance the pointers */
1630                         tail += result;
1631                         tail &= UART_XMIT_SIZE - 1;
1632                         info->xmit.tail = tail;
1633                         start = (char *)&info->xmit.buf[tail];
1634                 }
1635         }
1636         if (uart_circ_chars_pending(&info->xmit) < WAKEUP_CHARS)
1637                 uart_write_wakeup(the_port);
1638
1639         if (uart_circ_empty(&info->xmit)) {
1640                 set_notification(port, N_OUTPUT_LOWAT, 0);
1641         } else {
1642                 set_notification(port, N_OUTPUT_LOWAT, 1);
1643         }
1644 }
1645
1646 /**
1647  * ioc4_change_speed - change the speed of the port
1648  * @the_port: port to change
1649  * @new_termios: new termios settings
1650  * @old_termios: old termios settings
1651  */
1652 static void
1653 ioc4_change_speed(struct uart_port *the_port,
1654                   struct termios *new_termios, struct termios *old_termios)
1655 {
1656         struct ioc4_port *port = get_ioc4_port(the_port);
1657         int baud, bits;
1658         unsigned cflag;
1659         int new_parity = 0, new_parity_enable = 0, new_stop = 0, new_data = 8;
1660         struct uart_info *info = the_port->info;
1661
1662         cflag = new_termios->c_cflag;
1663
1664         switch (cflag & CSIZE) {
1665         case CS5:
1666                 new_data = 5;
1667                 bits = 7;
1668                 break;
1669         case CS6:
1670                 new_data = 6;
1671                 bits = 8;
1672                 break;
1673         case CS7:
1674                 new_data = 7;
1675                 bits = 9;
1676                 break;
1677         case CS8:
1678                 new_data = 8;
1679                 bits = 10;
1680                 break;
1681         default:
1682                 /* cuz we always need a default ... */
1683                 new_data = 5;
1684                 bits = 7;
1685                 break;
1686         }
1687         if (cflag & CSTOPB) {
1688                 bits++;
1689                 new_stop = 1;
1690         }
1691         if (cflag & PARENB) {
1692                 bits++;
1693                 new_parity_enable = 1;
1694                 if (cflag & PARODD)
1695                         new_parity = 1;
1696         }
1697         baud = uart_get_baud_rate(the_port, new_termios, old_termios,
1698                                 MIN_BAUD_SUPPORTED, MAX_BAUD_SUPPORTED);
1699         DPRINT_CONFIG(("%s: returned baud %d\n", __FUNCTION__, baud));
1700
1701         /* default is 9600 */
1702         if (!baud)
1703                 baud = 9600;
1704
1705         if (!the_port->fifosize)
1706                 the_port->fifosize = IOC4_MAX_CHARS;
1707         the_port->timeout = ((the_port->fifosize * HZ * bits) / (baud / 10));
1708         the_port->timeout += HZ / 50;   /* Add .02 seconds of slop */
1709
1710         the_port->ignore_status_mask = N_ALL_INPUT;
1711
1712         if (I_IGNPAR(info->tty))
1713                 the_port->ignore_status_mask &= ~(N_PARITY_ERROR
1714                                                 | N_FRAMING_ERROR);
1715         if (I_IGNBRK(info->tty)) {
1716                 the_port->ignore_status_mask &= ~N_BREAK;
1717                 if (I_IGNPAR(info->tty))
1718                         the_port->ignore_status_mask &= ~N_OVERRUN_ERROR;
1719         }
1720         if (!(cflag & CREAD)) {
1721                 /* ignore everything */
1722                 the_port->ignore_status_mask &= ~N_DATA_READY;
1723         }
1724
1725         if (cflag & CRTSCTS) {
1726                 info->flags |= ASYNC_CTS_FLOW;
1727                 port->ip_sscr |= IOC4_SSCR_HFC_EN;
1728         }
1729         else {
1730                 info->flags &= ~ASYNC_CTS_FLOW;
1731                 port->ip_sscr &= ~IOC4_SSCR_HFC_EN;
1732         }
1733         writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1734
1735         /* Set the configuration and proper notification call */
1736         DPRINT_CONFIG(("%s : port 0x%p cflag 0%o "
1737                 "config_port(baud %d data %d stop %d p enable %d parity %d),"
1738                 " notification 0x%x\n",
1739              __FUNCTION__, (void *)port, cflag, baud, new_data, new_stop,
1740              new_parity_enable, new_parity, the_port->ignore_status_mask));
1741
1742         if ((config_port(port, baud,            /* baud */
1743                          new_data,              /* byte size */
1744                          new_stop,              /* stop bits */
1745                          new_parity_enable,     /* set parity */
1746                          new_parity)) >= 0) {   /* parity 1==odd */
1747                 set_notification(port, the_port->ignore_status_mask, 1);
1748         }
1749 }
1750
1751 /**
1752  * ic4_startup_local - Start up the serial port - returns >= 0 if no errors
1753  * @the_port: Port to operate on
1754  */
1755 static inline int ic4_startup_local(struct uart_port *the_port)
1756 {
1757         int retval = 0;
1758         struct ioc4_port *port;
1759         struct uart_info *info;
1760
1761         if (!the_port)
1762                 return -1;
1763
1764         port = get_ioc4_port(the_port);
1765         if (!port)
1766                 return -1;
1767
1768         info = the_port->info;
1769         if (info->flags & UIF_INITIALIZED) {
1770                 return retval;
1771         }
1772
1773         if (info->tty) {
1774                 set_bit(TTY_IO_ERROR, &info->tty->flags);
1775                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1776                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1777                         info->tty->alt_speed = 57600;
1778                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1779                         info->tty->alt_speed = 115200;
1780                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1781                         info->tty->alt_speed = 230400;
1782                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1783                         info->tty->alt_speed = 460800;
1784         }
1785         local_open(port);
1786
1787         /* set the speed of the serial port */
1788         ioc4_change_speed(the_port, info->tty->termios, (struct termios *)0);
1789
1790         info->flags |= UIF_INITIALIZED;
1791         return 0;
1792 }
1793
1794 /*
1795  * ioc4_cb_output_lowat - called when the output low water mark is hit
1796  * @port: port to output
1797  */
1798 static void ioc4_cb_output_lowat(struct ioc4_port *port)
1799 {
1800         /* ip_lock is set on the call here */
1801         if (port->ip_port) {
1802                 transmit_chars(port->ip_port);
1803         }
1804 }
1805
1806 /**
1807  * handle_intr - service any interrupts for the given port - 2nd level
1808  *                      called via sd_intr
1809  * @arg: handler arg
1810  * @sio_ir: ioc4regs
1811  */
1812 static void handle_intr(void *arg, uint32_t sio_ir)
1813 {
1814         struct ioc4_port *port = (struct ioc4_port *)arg;
1815         struct hooks *hooks = port->ip_hooks;
1816         unsigned int rx_high_rd_aborted = 0;
1817         unsigned int flags;
1818         struct uart_port *the_port;
1819         int loop_counter;
1820
1821         /* Possible race condition here: The tx_mt interrupt bit may be
1822          * cleared without the intervention of the interrupt handler,
1823          * e.g. by a write.  If the top level interrupt handler reads a
1824          * tx_mt, then some other processor does a write, starting up
1825          * output, then we come in here, see the tx_mt and stop DMA, the
1826          * output started by the other processor will hang.  Thus we can
1827          * only rely on tx_mt being legitimate if it is read while the
1828          * port lock is held.  Therefore this bit must be ignored in the
1829          * passed in interrupt mask which was read by the top level
1830          * interrupt handler since the port lock was not held at the time
1831          * it was read.  We can only rely on this bit being accurate if it
1832          * is read while the port lock is held.  So we'll clear it for now,
1833          * and reload it later once we have the port lock.
1834          */
1835         sio_ir &= ~(hooks->intr_tx_mt);
1836
1837         spin_lock_irqsave(&port->ip_lock, flags);
1838
1839         loop_counter = MAXITER; /* to avoid hangs */
1840
1841         do {
1842                 uint32_t shadow;
1843
1844                 if ( loop_counter-- <= 0 ) {
1845                         printk(KERN_WARNING "IOC4 serial: "
1846                                         "possible hang condition/"
1847                                         "port stuck on interrupt.\n");
1848                         break;
1849                 }
1850
1851                 /* Handle a DCD change */
1852                 if (sio_ir & hooks->intr_delta_dcd) {
1853                         /* ACK the interrupt */
1854                         writel(hooks->intr_delta_dcd,
1855                                 &port->ip_mem->sio_ir.raw);
1856
1857                         shadow = readl(&port->ip_serial_regs->shadow);
1858
1859                         if ((port->ip_notify & N_DDCD)
1860                                         && (shadow & IOC4_SHADOW_DCD)
1861                                         && (port->ip_port)) {
1862                                 the_port = port->ip_port;
1863                                 the_port->icount.dcd = 1;
1864                                 wake_up_interruptible
1865                                             (&the_port-> info->delta_msr_wait);
1866                         } else if ((port->ip_notify & N_DDCD)
1867                                         && !(shadow & IOC4_SHADOW_DCD)) {
1868                                 /* Flag delta DCD/no DCD */
1869                                 port->ip_flags |= DCD_ON;
1870                         }
1871                 }
1872
1873                 /* Handle a CTS change */
1874                 if (sio_ir & hooks->intr_delta_cts) {
1875                         /* ACK the interrupt */
1876                         writel(hooks->intr_delta_cts,
1877                                         &port->ip_mem->sio_ir.raw);
1878
1879                         shadow = readl(&port->ip_serial_regs->shadow);
1880
1881                         if ((port->ip_notify & N_DCTS)
1882                                         && (port->ip_port)) {
1883                                 the_port = port->ip_port;
1884                                 the_port->icount.cts =
1885                                         (shadow & IOC4_SHADOW_CTS) ? 1 : 0;
1886                                 wake_up_interruptible
1887                                         (&the_port->info->delta_msr_wait);
1888                         }
1889                 }
1890
1891                 /* rx timeout interrupt.  Must be some data available.  Put this
1892                  * before the check for rx_high since servicing this condition
1893                  * may cause that condition to clear.
1894                  */
1895                 if (sio_ir & hooks->intr_rx_timer) {
1896                         /* ACK the interrupt */
1897                         writel(hooks->intr_rx_timer,
1898                                 &port->ip_mem->sio_ir.raw);
1899
1900                         if ((port->ip_notify & N_DATA_READY)
1901                                         && (port->ip_port)) {
1902                                 /* ip_lock is set on call here */
1903                                 receive_chars(port->ip_port);
1904                         }
1905                 }
1906
1907                 /* rx high interrupt. Must be after rx_timer.  */
1908                 else if (sio_ir & hooks->intr_rx_high) {
1909                         /* Data available, notify upper layer */
1910                         if ((port->ip_notify & N_DATA_READY)
1911                                                 && port->ip_port) {
1912                                 /* ip_lock is set on call here */
1913                                 receive_chars(port->ip_port);
1914                         }
1915
1916                         /* We can't ACK this interrupt.  If receive_chars didn't
1917                          * cause the condition to clear, we'll have to disable
1918                          * the interrupt until the data is drained.
1919                          * If the read was aborted, don't disable the interrupt
1920                          * as this may cause us to hang indefinitely.  An
1921                          * aborted read generally means that this interrupt
1922                          * hasn't been delivered to the cpu yet anyway, even
1923                          * though we see it as asserted when we read the sio_ir.
1924                          */
1925                         if ((sio_ir = PENDING(port)) & hooks->intr_rx_high) {
1926                                 if ((port->ip_flags & READ_ABORTED) == 0) {
1927                                         port->ip_ienb &= ~hooks->intr_rx_high;
1928                                         port->ip_flags |= INPUT_HIGH;
1929                                 } else {
1930                                         rx_high_rd_aborted++;
1931                                 }
1932                         }
1933                 }
1934
1935                 /* We got a low water interrupt: notify upper layer to
1936                  * send more data.  Must come before tx_mt since servicing
1937                  * this condition may cause that condition to clear.
1938                  */
1939                 if (sio_ir & hooks->intr_tx_explicit) {
1940                         port->ip_flags &= ~LOWAT_WRITTEN;
1941
1942                         /* ACK the interrupt */
1943                         writel(hooks->intr_tx_explicit,
1944                                         &port->ip_mem->sio_ir.raw);
1945
1946                         if (port->ip_notify & N_OUTPUT_LOWAT)
1947                                 ioc4_cb_output_lowat(port);
1948                 }
1949
1950                 /* Handle tx_mt.  Must come after tx_explicit.  */
1951                 else if (sio_ir & hooks->intr_tx_mt) {
1952                         /* If we are expecting a lowat notification
1953                          * and we get to this point it probably means that for
1954                          * some reason the tx_explicit didn't work as expected
1955                          * (that can legitimately happen if the output buffer is
1956                          * filled up in just the right way).
1957                          * So send the notification now.
1958                          */
1959                         if (port->ip_notify & N_OUTPUT_LOWAT) {
1960                                 ioc4_cb_output_lowat(port);
1961
1962                                 /* We need to reload the sio_ir since the lowat
1963                                  * call may have caused another write to occur,
1964                                  * clearing the tx_mt condition.
1965                                  */
1966                                 sio_ir = PENDING(port);
1967                         }
1968
1969                         /* If the tx_mt condition still persists even after the
1970                          * lowat call, we've got some work to do.
1971                          */
1972                         if (sio_ir & hooks->intr_tx_mt) {
1973
1974                                 /* If we are not currently expecting DMA input,
1975                                  * and the transmitter has just gone idle,
1976                                  * there is no longer any reason for DMA, so
1977                                  * disable it.
1978                                  */
1979                                 if (!(port->ip_notify
1980                                                 & (N_DATA_READY | N_DDCD))) {
1981                                         BUG_ON(!(port->ip_sscr
1982                                                         & IOC4_SSCR_DMA_EN));
1983                                         port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
1984                                         writel(port->ip_sscr,
1985                                            &port->ip_serial_regs->sscr);
1986                                 }
1987
1988                                 /* Prevent infinite tx_mt interrupt */
1989                                 port->ip_ienb &= ~hooks->intr_tx_mt;
1990                         }
1991                 }
1992                 sio_ir = PENDING(port);
1993
1994                 /* if the read was aborted and only hooks->intr_rx_high,
1995                  * clear hooks->intr_rx_high, so we do not loop forever.
1996                  */
1997
1998                 if (rx_high_rd_aborted && (sio_ir == hooks->intr_rx_high)) {
1999                         sio_ir &= ~hooks->intr_rx_high;
2000                 }
2001         } while (sio_ir & hooks->intr_all);
2002
2003         spin_unlock_irqrestore(&port->ip_lock, flags);
2004
2005         /* Re-enable interrupts before returning from interrupt handler.
2006          * Getting interrupted here is okay.  It'll just v() our semaphore, and
2007          * we'll come through the loop again.
2008          */
2009
2010         write_ireg(port->ip_ioc4_soft, port->ip_ienb, IOC4_W_IES,
2011                                                         IOC4_SIO_INTR_TYPE);
2012 }
2013
2014 /*
2015  * ioc4_cb_post_ncs - called for some basic errors
2016  * @port: port to use
2017  * @ncs: event
2018  */
2019 static void ioc4_cb_post_ncs(struct uart_port *the_port, int ncs)
2020 {
2021         struct uart_icount *icount;
2022
2023         icount = &the_port->icount;
2024
2025         if (ncs & NCS_BREAK)
2026                 icount->brk++;
2027         if (ncs & NCS_FRAMING)
2028                 icount->frame++;
2029         if (ncs & NCS_OVERRUN)
2030                 icount->overrun++;
2031         if (ncs & NCS_PARITY)
2032                 icount->parity++;
2033 }
2034
2035 /**
2036  * do_read - Read in bytes from the port.  Return the number of bytes
2037  *                      actually read.
2038  * @the_port: port to use
2039  * @buf: place to put the stuff we read
2040  * @len: how big 'buf' is
2041  */
2042
2043 static inline int do_read(struct uart_port *the_port, unsigned char *buf,
2044                                 int len)
2045 {
2046         int prod_ptr, cons_ptr, total;
2047         struct ioc4_port *port = get_ioc4_port(the_port);
2048         struct ring *inring;
2049         struct ring_entry *entry;
2050         struct hooks *hooks = port->ip_hooks;
2051         int byte_num;
2052         char *sc;
2053         int loop_counter;
2054
2055         BUG_ON(!(len >= 0));
2056         BUG_ON(!port);
2057
2058         /* There is a nasty timing issue in the IOC4. When the rx_timer
2059          * expires or the rx_high condition arises, we take an interrupt.
2060          * At some point while servicing the interrupt, we read bytes from
2061          * the ring buffer and re-arm the rx_timer.  However the rx_timer is
2062          * not started until the first byte is received *after* it is armed,
2063          * and any bytes pending in the rx construction buffers are not drained
2064          * to memory until either there are 4 bytes available or the rx_timer
2065          * expires.  This leads to a potential situation where data is left
2066          * in the construction buffers forever - 1 to 3 bytes were received
2067          * after the interrupt was generated but before the rx_timer was
2068          * re-armed. At that point as long as no subsequent bytes are received
2069          * the timer will never be started and the bytes will remain in the
2070          * construction buffer forever.  The solution is to execute a DRAIN
2071          * command after rearming the timer.  This way any bytes received before
2072          * the DRAIN will be drained to memory, and any bytes received after
2073          * the DRAIN will start the TIMER and be drained when it expires.
2074          * Luckily, this only needs to be done when the DMA buffer is empty
2075          * since there is no requirement that this function return all
2076          * available data as long as it returns some.
2077          */
2078         /* Re-arm the timer */
2079         writel(port->ip_rx_cons | IOC4_SRCIR_ARM,
2080                         &port->ip_serial_regs->srcir);
2081
2082         prod_ptr = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
2083         cons_ptr = port->ip_rx_cons;
2084
2085         if (prod_ptr == cons_ptr) {
2086                 int reset_dma = 0;
2087
2088                 /* Input buffer appears empty, do a flush. */
2089
2090                 /* DMA must be enabled for this to work. */
2091                 if (!(port->ip_sscr & IOC4_SSCR_DMA_EN)) {
2092                         port->ip_sscr |= IOC4_SSCR_DMA_EN;
2093                         reset_dma = 1;
2094                 }
2095
2096                 /* Potential race condition: we must reload the srpir after
2097                  * issuing the drain command, otherwise we could think the rx
2098                  * buffer is empty, then take a very long interrupt, and when
2099                  * we come back it's full and we wait forever for the drain to
2100                  * complete.
2101                  */
2102                 writel(port->ip_sscr | IOC4_SSCR_RX_DRAIN,
2103                                 &port->ip_serial_regs->sscr);
2104                 prod_ptr = readl(&port->ip_serial_regs->srpir)
2105                                 & PROD_CONS_MASK;
2106
2107                 /* We must not wait for the DRAIN to complete unless there are
2108                  * at least 8 bytes (2 ring entries) available to receive the
2109                  * data otherwise the DRAIN will never complete and we'll
2110                  * deadlock here.
2111                  * In fact, to make things easier, I'll just ignore the flush if
2112                  * there is any data at all now available.
2113                  */
2114                 if (prod_ptr == cons_ptr) {
2115                         loop_counter = 0;
2116                         while (readl(&port->ip_serial_regs->sscr) &
2117                                                 IOC4_SSCR_RX_DRAIN) {
2118                                 loop_counter++;
2119                                 if (loop_counter > MAXITER)
2120                                         return -1;
2121                         }
2122
2123                         /* SIGH. We have to reload the prod_ptr *again* since
2124                          * the drain may have caused it to change
2125                          */
2126                         prod_ptr = readl(&port->ip_serial_regs->srpir)
2127                                                         & PROD_CONS_MASK;
2128                 }
2129                 if (reset_dma) {
2130                         port->ip_sscr &= ~IOC4_SSCR_DMA_EN;
2131                         writel(port->ip_sscr, &port->ip_serial_regs->sscr);
2132                 }
2133         }
2134         inring = port->ip_inring;
2135         port->ip_flags &= ~READ_ABORTED;
2136
2137         total = 0;
2138         loop_counter = 0xfffff; /* to avoid hangs */
2139
2140         /* Grab bytes from the hardware */
2141         while ((prod_ptr != cons_ptr) && (len > 0)) {
2142                 entry = (struct ring_entry *)((caddr_t)inring + cons_ptr);
2143
2144                 if ( loop_counter-- <= 0 ) {
2145                         printk(KERN_WARNING "IOC4 serial: "
2146                                         "possible hang condition/"
2147                                         "port stuck on read.\n");
2148                         break;
2149                 }
2150
2151                 /* According to the producer pointer, this ring entry
2152                  * must contain some data.  But if the PIO happened faster
2153                  * than the DMA, the data may not be available yet, so let's
2154                  * wait until it arrives.
2155                  */
2156                 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
2157                         /* Indicate the read is aborted so we don't disable
2158                          * the interrupt thinking that the consumer is
2159                          * congested.
2160                          */
2161                         port->ip_flags |= READ_ABORTED;
2162                         len = 0;
2163                         break;
2164                 }
2165
2166                 /* Load the bytes/status out of the ring entry */
2167                 for (byte_num = 0; byte_num < 4 && len > 0; byte_num++) {
2168                         sc = &(entry->ring_sc[byte_num]);
2169
2170                         /* Check for change in modem state or overrun */
2171                         if ((*sc & IOC4_RXSB_MODEM_VALID)
2172                                                 && (port->ip_notify & N_DDCD)) {
2173                                 /* Notify upper layer if DCD dropped */
2174
2175                                 if ((port->ip_flags & DCD_ON)
2176                                                 && !(*sc & IOC4_RXSB_DCD)) {
2177
2178                                         /* If we have already copied some data,
2179                                          * return it.  We'll pick up the carrier
2180                                          * drop on the next pass.  That way we
2181                                          * don't throw away the data that has
2182                                          * already been copied back to
2183                                          * the caller's buffer.
2184                                          */
2185                                         if (total > 0) {
2186                                                 len = 0;
2187                                                 break;
2188                                         }
2189                                         port->ip_flags &= ~DCD_ON;
2190
2191                                         /* Turn off this notification so the
2192                                          * carrier drop protocol won't see it
2193                                          * again when it does a read.
2194                                          */
2195                                         *sc &= ~IOC4_RXSB_MODEM_VALID;
2196
2197                                         /* To keep things consistent, we need
2198                                          * to update the consumer pointer so
2199                                          * the next reader won't come in and
2200                                          * try to read the same ring entries
2201                                          * again. This must be done here before
2202                                          * the dcd change.
2203                                          */
2204
2205                                         if ((entry->ring_allsc & RING_ANY_VALID)
2206                                                                         == 0) {
2207                                                 cons_ptr += (int)sizeof
2208                                                         (struct ring_entry);
2209                                                 cons_ptr &= PROD_CONS_MASK;
2210                                         }
2211                                         writel(cons_ptr,
2212                                                 &port->ip_serial_regs->srcir);
2213                                         port->ip_rx_cons = cons_ptr;
2214
2215                                         /* Notify upper layer of carrier drop */
2216                                         if ((port->ip_notify & N_DDCD)
2217                                                    && port->ip_port) {
2218                                                 the_port->icount.dcd = 0;
2219                                                 wake_up_interruptible
2220                                                     (&the_port->info->
2221                                                         delta_msr_wait);
2222                                         }
2223
2224                                         /* If we had any data to return, we
2225                                          * would have returned it above.
2226                                          */
2227                                         return 0;
2228                                 }
2229                         }
2230                         if (*sc & IOC4_RXSB_MODEM_VALID) {
2231                                 /* Notify that an input overrun occurred */
2232                                 if ((*sc & IOC4_RXSB_OVERRUN)
2233                                     && (port->ip_notify & N_OVERRUN_ERROR)) {
2234                                         ioc4_cb_post_ncs(the_port, NCS_OVERRUN);
2235                                 }
2236                                 /* Don't look at this byte again */
2237                                 *sc &= ~IOC4_RXSB_MODEM_VALID;
2238                         }
2239
2240                         /* Check for valid data or RX errors */
2241                         if ((*sc & IOC4_RXSB_DATA_VALID) &&
2242                                         ((*sc & (IOC4_RXSB_PAR_ERR
2243                                                         | IOC4_RXSB_FRAME_ERR
2244                                                         | IOC4_RXSB_BREAK))
2245                                         && (port->ip_notify & (N_PARITY_ERROR
2246                                                         | N_FRAMING_ERROR
2247                                                         | N_BREAK)))) {
2248                                 /* There is an error condition on the next byte.
2249                                  * If we have already transferred some bytes,
2250                                  * we'll stop here. Otherwise if this is the
2251                                  * first byte to be read, we'll just transfer
2252                                  * it alone after notifying the
2253                                  * upper layer of its status.
2254                                  */
2255                                 if (total > 0) {
2256                                         len = 0;
2257                                         break;
2258                                 } else {
2259                                         if ((*sc & IOC4_RXSB_PAR_ERR) &&
2260                                            (port->ip_notify & N_PARITY_ERROR)) {
2261                                                 ioc4_cb_post_ncs(the_port,
2262                                                                 NCS_PARITY);
2263                                         }
2264                                         if ((*sc & IOC4_RXSB_FRAME_ERR) &&
2265                                            (port->ip_notify & N_FRAMING_ERROR)){
2266                                                 ioc4_cb_post_ncs(the_port,
2267                                                                 NCS_FRAMING);
2268                                         }
2269                                         if ((*sc & IOC4_RXSB_BREAK)
2270                                             && (port->ip_notify & N_BREAK)) {
2271                                                         ioc4_cb_post_ncs
2272                                                                     (the_port,
2273                                                                      NCS_BREAK);
2274                                         }
2275                                         len = 1;
2276                                 }
2277                         }
2278                         if (*sc & IOC4_RXSB_DATA_VALID) {
2279                                 *sc &= ~IOC4_RXSB_DATA_VALID;
2280                                 *buf = entry->ring_data[byte_num];
2281                                 buf++;
2282                                 len--;
2283                                 total++;
2284                         }
2285                 }
2286
2287                 /* If we used up this entry entirely, go on to the next one,
2288                  * otherwise we must have run out of buffer space, so
2289                  * leave the consumer pointer here for the next read in case
2290                  * there are still unread bytes in this entry.
2291                  */
2292                 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
2293                         cons_ptr += (int)sizeof(struct ring_entry);
2294                         cons_ptr &= PROD_CONS_MASK;
2295                 }
2296         }
2297
2298         /* Update consumer pointer and re-arm rx timer interrupt */
2299         writel(cons_ptr, &port->ip_serial_regs->srcir);
2300         port->ip_rx_cons = cons_ptr;
2301
2302         /* If we have now dipped below the rx high water mark and we have
2303          * rx_high interrupt turned off, we can now turn it back on again.
2304          */
2305         if ((port->ip_flags & INPUT_HIGH) && (((prod_ptr - cons_ptr)
2306                         & PROD_CONS_MASK) < ((port->ip_sscr &
2307                                 IOC4_SSCR_RX_THRESHOLD)
2308                                         << IOC4_PROD_CONS_PTR_OFF))) {
2309                 port->ip_flags &= ~INPUT_HIGH;
2310                 enable_intrs(port, hooks->intr_rx_high);
2311         }
2312         return total;
2313 }
2314 /**
2315  * receive_chars - upper level read. Called with ip_lock.
2316  * @the_port: port to read from
2317  */
2318 static void receive_chars(struct uart_port *the_port)
2319 {
2320         struct tty_struct *tty;
2321         unsigned char ch[IOC4_MAX_CHARS];
2322         int read_count, request_count;
2323         struct uart_icount *icount;
2324         struct uart_info *info = the_port->info;
2325
2326         /* Make sure all the pointers are "good" ones */
2327         if (!info)
2328                 return;
2329         if (!info->tty)
2330                 return;
2331
2332         tty = info->tty;
2333
2334         request_count = TTY_FLIPBUF_SIZE - tty->flip.count - 1;
2335
2336         if (request_count > 0) {
2337                 if (request_count > IOC4_MAX_CHARS - 2)
2338                         request_count = IOC4_MAX_CHARS - 2;
2339                 icount = &the_port->icount;
2340                 read_count = do_read(the_port, ch, request_count);
2341                 if (read_count > 0) {
2342                         memcpy(tty->flip.char_buf_ptr, ch, read_count);
2343                         memset(tty->flip.flag_buf_ptr, TTY_NORMAL, read_count);
2344                         tty->flip.char_buf_ptr += read_count;
2345                         tty->flip.flag_buf_ptr += read_count;
2346                         tty->flip.count += read_count;
2347                         icount->rx += read_count;
2348                 }
2349         }
2350         tty_flip_buffer_push(tty);
2351 }
2352
2353 /**
2354  * ic4_type - What type of console are we?
2355  * @port: Port to operate with (we ignore since we only have one port)
2356  *
2357  */
2358 static const char *ic4_type(struct uart_port *the_port)
2359 {
2360         return "SGI IOC4 Serial";
2361 }
2362
2363 /**
2364  * ic4_tx_empty - Is the transmitter empty?  We pretend we're always empty
2365  * @port: Port to operate on (we ignore since we always return 1)
2366  *
2367  */
2368 static unsigned int ic4_tx_empty(struct uart_port *the_port)
2369 {
2370         return 1;
2371 }
2372
2373 /**
2374  * ic4_stop_tx - stop the transmitter
2375  * @port: Port to operate on
2376  *
2377  */
2378 static void ic4_stop_tx(struct uart_port *the_port)
2379 {
2380 }
2381
2382 /**
2383  * null_void_function -
2384  * @port: Port to operate on
2385  *
2386  */
2387 static void null_void_function(struct uart_port *the_port)
2388 {
2389 }
2390
2391 /**
2392  * ic4_shutdown - shut down the port - free irq and disable
2393  * @port: Port to shut down
2394  *
2395  */
2396 static void ic4_shutdown(struct uart_port *the_port)
2397 {
2398         unsigned long port_flags;
2399         struct ioc4_port *port;
2400         struct uart_info *info;
2401
2402         port = get_ioc4_port(the_port);
2403         if (!port)
2404                 return;
2405
2406         info = the_port->info;
2407
2408         if (!(info->flags & UIF_INITIALIZED))
2409                 return;
2410
2411         wake_up_interruptible(&info->delta_msr_wait);
2412
2413         if (info->tty)
2414                 set_bit(TTY_IO_ERROR, &info->tty->flags);
2415
2416         spin_lock_irqsave(&port->ip_lock, port_flags);
2417         set_notification(port, N_ALL, 0);
2418         info->flags &= ~UIF_INITIALIZED;
2419         spin_unlock_irqrestore(&port->ip_lock, port_flags);
2420 }
2421
2422 /**
2423  * ic4_set_mctrl - set control lines (dtr, rts, etc)
2424  * @port: Port to operate on
2425  * @mctrl: Lines to set/unset
2426  *
2427  */
2428 static void ic4_set_mctrl(struct uart_port *the_port, unsigned int mctrl)
2429 {
2430         unsigned char mcr = 0;
2431
2432         if (mctrl & TIOCM_RTS)
2433                 mcr |= UART_MCR_RTS;
2434         if (mctrl & TIOCM_DTR)
2435                 mcr |= UART_MCR_DTR;
2436         if (mctrl & TIOCM_OUT1)
2437                 mcr |= UART_MCR_OUT1;
2438         if (mctrl & TIOCM_OUT2)
2439                 mcr |= UART_MCR_OUT2;
2440         if (mctrl & TIOCM_LOOP)
2441                 mcr |= UART_MCR_LOOP;
2442
2443         set_mcr(the_port, 1, mcr, IOC4_SHADOW_DTR);
2444 }
2445
2446 /**
2447  * ic4_get_mctrl - get control line info
2448  * @port: port to operate on
2449  *
2450  */
2451 static unsigned int ic4_get_mctrl(struct uart_port *the_port)
2452 {
2453         struct ioc4_port *port = get_ioc4_port(the_port);
2454         uint32_t shadow;
2455         unsigned int ret = 0;
2456
2457         if (!port)
2458                 return 0;
2459
2460         shadow = readl(&port->ip_serial_regs->shadow);
2461         if (shadow & IOC4_SHADOW_DCD)
2462                 ret |= TIOCM_CAR;
2463         if (shadow & IOC4_SHADOW_DR)
2464                 ret |= TIOCM_DSR;
2465         if (shadow & IOC4_SHADOW_CTS)
2466                 ret |= TIOCM_CTS;
2467         return ret;
2468 }
2469
2470 /**
2471  * ic4_start_tx - Start transmitter, flush any output
2472  * @port: Port to operate on
2473  *
2474  */
2475 static void ic4_start_tx(struct uart_port *the_port)
2476 {
2477         struct ioc4_port *port = get_ioc4_port(the_port);
2478         unsigned long flags;
2479
2480         if (port) {
2481                 spin_lock_irqsave(&port->ip_lock, flags);
2482                 transmit_chars(the_port);
2483                 spin_unlock_irqrestore(&port->ip_lock, flags);
2484         }
2485 }
2486
2487 /**
2488  * ic4_break_ctl - handle breaks
2489  * @port: Port to operate on
2490  * @break_state: Break state
2491  *
2492  */
2493 static void ic4_break_ctl(struct uart_port *the_port, int break_state)
2494 {
2495 }
2496
2497 /**
2498  * ic4_startup - Start up the serial port - always return 0 (We're always on)
2499  * @port: Port to operate on
2500  *
2501  */
2502 static int ic4_startup(struct uart_port *the_port)
2503 {
2504         int retval;
2505         struct ioc4_port *port;
2506         struct ioc4_control *control;
2507         struct uart_info *info;
2508         unsigned long port_flags;
2509
2510         if (!the_port) {
2511                 return -ENODEV;
2512         }
2513         port = get_ioc4_port(the_port);
2514         if (!port) {
2515                 return -ENODEV;
2516         }
2517         info = the_port->info;
2518
2519         control = port->ip_control;
2520         if (!control) {
2521                 return -ENODEV;
2522         }
2523
2524         /* Start up the serial port */
2525         spin_lock_irqsave(&port->ip_lock, port_flags);
2526         retval = ic4_startup_local(the_port);
2527         spin_unlock_irqrestore(&port->ip_lock, port_flags);
2528         return retval;
2529 }
2530
2531 /**
2532  * ic4_set_termios - set termios stuff
2533  * @port: port to operate on
2534  * @termios: New settings
2535  * @termios: Old
2536  *
2537  */
2538 static void
2539 ic4_set_termios(struct uart_port *the_port,
2540                 struct termios *termios, struct termios *old_termios)
2541 {
2542         struct ioc4_port *port = get_ioc4_port(the_port);
2543         unsigned long port_flags;
2544
2545         spin_lock_irqsave(&port->ip_lock, port_flags);
2546         ioc4_change_speed(the_port, termios, old_termios);
2547         spin_unlock_irqrestore(&port->ip_lock, port_flags);
2548 }
2549
2550 /**
2551  * ic4_request_port - allocate resources for port - no op....
2552  * @port: port to operate on
2553  *
2554  */
2555 static int ic4_request_port(struct uart_port *port)
2556 {
2557         return 0;
2558 }
2559
2560 /* Associate the uart functions above - given to serial core */
2561
2562 static struct uart_ops ioc4_ops = {
2563         .tx_empty       = ic4_tx_empty,
2564         .set_mctrl      = ic4_set_mctrl,
2565         .get_mctrl      = ic4_get_mctrl,
2566         .stop_tx        = ic4_stop_tx,
2567         .start_tx       = ic4_start_tx,
2568         .stop_rx        = null_void_function,
2569         .enable_ms      = null_void_function,
2570         .break_ctl      = ic4_break_ctl,
2571         .startup        = ic4_startup,
2572         .shutdown       = ic4_shutdown,
2573         .set_termios    = ic4_set_termios,
2574         .type           = ic4_type,
2575         .release_port   = null_void_function,
2576         .request_port   = ic4_request_port,
2577 };
2578
2579 /*
2580  * Boot-time initialization code
2581  */
2582
2583 static struct uart_driver ioc4_uart = {
2584         .owner          = THIS_MODULE,
2585         .driver_name    = "ioc4_serial",
2586         .dev_name       = DEVICE_NAME,
2587         .major          = DEVICE_MAJOR,
2588         .minor          = DEVICE_MINOR,
2589         .nr             = IOC4_NUM_CARDS * IOC4_NUM_SERIAL_PORTS,
2590 };
2591
2592 /**
2593  * ioc4_serial_core_attach - register with serial core
2594  *              This is done during pci probing
2595  * @pdev: handle for this card
2596  */
2597 static inline int
2598 ioc4_serial_core_attach(struct pci_dev *pdev)
2599 {
2600         struct ioc4_port *port;
2601         struct uart_port *the_port;
2602         struct ioc4_driver_data *idd = pci_get_drvdata(pdev);
2603         struct ioc4_control *control = idd->idd_serial_data;
2604         int ii;
2605
2606         DPRINT_CONFIG(("%s: attach pdev 0x%p - control 0x%p\n",
2607                         __FUNCTION__, pdev, (void *)control));
2608
2609         if (!control)
2610                 return -ENODEV;
2611
2612         /* once around for each port on this card */
2613         for (ii = 0; ii < IOC4_NUM_SERIAL_PORTS; ii++) {
2614                 the_port = &control->ic_port[ii].icp_uart_port;
2615                 port = control->ic_port[ii].icp_port;
2616                 port->ip_port = the_port;
2617
2618                 DPRINT_CONFIG(("%s: attach the_port 0x%p / port 0x%p\n",
2619                                 __FUNCTION__, (void *)the_port,
2620                                 (void *)port));
2621
2622                 spin_lock_init(&the_port->lock);
2623                 /* membase, iobase and mapbase just need to be non-0 */
2624                 the_port->membase = (unsigned char __iomem *)1;
2625                 the_port->line = the_port->iobase = ii;
2626                 the_port->mapbase = 1;
2627                 the_port->type = PORT_16550A;
2628                 the_port->fifosize = IOC4_MAX_CHARS;
2629                 the_port->ops = &ioc4_ops;
2630                 the_port->irq = control->ic_irq;
2631                 the_port->dev = &pdev->dev;
2632                 if (uart_add_one_port(&ioc4_uart, the_port) < 0) {
2633                         printk(KERN_WARNING
2634                                        "%s: unable to add port %d\n",
2635                                        __FUNCTION__, the_port->line);
2636                 } else {
2637                         DPRINT_CONFIG(
2638                                     ("IOC4 serial driver port %d irq = %d\n",
2639                                        the_port->line, the_port->irq));
2640                 }
2641                 /* all ports are rs232 for now */
2642                 ioc4_set_proto(port, PROTO_RS232);
2643         }
2644         return 0;
2645 }
2646
2647 /**
2648  * ioc4_serial_attach_one - register attach function
2649  *              called per card found from IOC4 master module.
2650  * @idd: Master module data for this IOC4
2651  */
2652 int
2653 ioc4_serial_attach_one(struct ioc4_driver_data *idd)
2654 {
2655         unsigned long tmp_addr1;
2656         struct ioc4_serial __iomem *serial;
2657         struct ioc4_soft *soft;
2658         struct ioc4_control *control;
2659         int ret = 0;
2660
2661
2662         DPRINT_CONFIG(("%s (0x%p, 0x%p)\n", __FUNCTION__, idd->idd_pdev, idd->idd_pci_id));
2663
2664         /* request serial registers */
2665         tmp_addr1 = idd->idd_bar0 + IOC4_SERIAL_OFFSET;
2666
2667         if (!request_region(tmp_addr1, sizeof(struct ioc4_serial),
2668                                         "sioc4_uart")) {
2669                 printk(KERN_WARNING
2670                         "ioc4 (%p): unable to get request region for "
2671                                 "uart space\n", (void *)idd->idd_pdev);
2672                 ret = -ENODEV;
2673                 goto out1;
2674         }
2675         serial = ioremap(tmp_addr1, sizeof(struct ioc4_serial));
2676         if (!serial) {
2677                 printk(KERN_WARNING
2678                          "ioc4 (%p) : unable to remap ioc4 serial register\n",
2679                                 (void *)idd->idd_pdev);
2680                 ret = -ENODEV;
2681                 goto out2;
2682         }
2683         DPRINT_CONFIG(("%s : mem 0x%p, serial 0x%p\n",
2684                                 __FUNCTION__, (void *)idd->idd_misc_regs, (void *)serial));
2685
2686         /* Get memory for the new card */
2687         control = kmalloc(sizeof(struct ioc4_control) * IOC4_NUM_SERIAL_PORTS,
2688                                                 GFP_KERNEL);
2689
2690         if (!control) {
2691                 printk(KERN_WARNING "ioc4_attach_one"
2692                        ": unable to get memory for the IOC4\n");
2693                 ret = -ENOMEM;
2694                 goto out2;
2695         }
2696         memset(control, 0, sizeof(struct ioc4_control));
2697         idd->idd_serial_data = control;
2698
2699         /* Allocate the soft structure */
2700         soft = kmalloc(sizeof(struct ioc4_soft), GFP_KERNEL);
2701         if (!soft) {
2702                 printk(KERN_WARNING
2703                        "ioc4 (%p): unable to get memory for the soft struct\n",
2704                        (void *)idd->idd_pdev);
2705                 ret = -ENOMEM;
2706                 goto out3;
2707         }
2708         memset(soft, 0, sizeof(struct ioc4_soft));
2709
2710         spin_lock_init(&soft->is_ir_lock);
2711         soft->is_ioc4_misc_addr = idd->idd_misc_regs;
2712         soft->is_ioc4_serial_addr = serial;
2713
2714         /* Init the IOC4 */
2715         writel(0xf << IOC4_SIO_CR_CMD_PULSE_SHIFT,
2716                &idd->idd_misc_regs->sio_cr.raw);
2717
2718         /* Enable serial port mode select generic PIO pins as outputs */
2719         writel(IOC4_GPCR_UART0_MODESEL | IOC4_GPCR_UART1_MODESEL
2720                 | IOC4_GPCR_UART2_MODESEL | IOC4_GPCR_UART3_MODESEL,
2721                 &idd->idd_misc_regs->gpcr_s.raw);
2722
2723         /* Clear and disable all serial interrupts */
2724         write_ireg(soft, ~0, IOC4_W_IEC, IOC4_SIO_INTR_TYPE);
2725         writel(~0, &idd->idd_misc_regs->sio_ir.raw);
2726         write_ireg(soft, IOC4_OTHER_IR_SER_MEMERR, IOC4_W_IEC,
2727                    IOC4_OTHER_INTR_TYPE);
2728         writel(IOC4_OTHER_IR_SER_MEMERR, &idd->idd_misc_regs->other_ir.raw);
2729         control->ic_soft = soft;
2730
2731         /* Hook up interrupt handler */
2732         if (!request_irq(idd->idd_pdev->irq, ioc4_intr, SA_SHIRQ,
2733                                 "sgi-ioc4serial", (void *)soft)) {
2734                 control->ic_irq = idd->idd_pdev->irq;
2735         } else {
2736                 printk(KERN_WARNING
2737                     "%s : request_irq fails for IRQ 0x%x\n ",
2738                         __FUNCTION__, idd->idd_pdev->irq);
2739         }
2740         ret = ioc4_attach_local(idd);
2741         if (ret)
2742                 goto out4;
2743
2744         /* register port with the serial core */
2745
2746         if ((ret = ioc4_serial_core_attach(idd->idd_pdev)))
2747                 goto out4;
2748
2749         return ret;
2750
2751         /* error exits that give back resources */
2752 out4:
2753         kfree(soft);
2754 out3:
2755         kfree(control);
2756 out2:
2757         release_region(tmp_addr1, sizeof(struct ioc4_serial));
2758 out1:
2759
2760         return ret;
2761 }
2762
2763
2764 /**
2765  * ioc4_serial_remove_one - detach function
2766  *
2767  * @idd: IOC4 master module data for this IOC4
2768  */
2769
2770 int ioc4_serial_remove_one(struct ioc4_driver_data *idd)
2771 {
2772         int ii;
2773         struct ioc4_control *control;
2774         struct uart_port *the_port;
2775         struct ioc4_port *port;
2776         struct ioc4_soft *soft;
2777
2778         control = idd->idd_serial_data;
2779
2780         for (ii = 0; ii < IOC4_NUM_SERIAL_PORTS; ii++) {
2781                 the_port = &control->ic_port[ii].icp_uart_port;
2782                 if (the_port) {
2783                         uart_remove_one_port(&ioc4_uart, the_port);
2784                 }
2785                 port = control->ic_port[ii].icp_port;
2786                 if (!(ii & 1) && port) {
2787                         pci_free_consistent(port->ip_pdev,
2788                                         TOTAL_RING_BUF_SIZE,
2789                                         (void *)port->ip_cpu_ringbuf,
2790                                         port->ip_dma_ringbuf);
2791                         kfree(port);
2792                 }
2793         }
2794         soft = control->ic_soft;
2795         if (soft) {
2796                 free_irq(control->ic_irq, (void *)soft);
2797                 if (soft->is_ioc4_serial_addr) {
2798                         release_region((unsigned long)
2799                              soft->is_ioc4_serial_addr,
2800                                 sizeof(struct ioc4_serial));
2801                 }
2802                 kfree(soft);
2803         }
2804         kfree(control);
2805         idd->idd_serial_data = NULL;
2806
2807         return 0;
2808 }
2809
2810 static struct ioc4_submodule ioc4_serial_submodule = {
2811         .is_name = "IOC4_serial",
2812         .is_owner = THIS_MODULE,
2813         .is_probe = ioc4_serial_attach_one,
2814         .is_remove = ioc4_serial_remove_one,
2815 };
2816
2817 /**
2818  * ioc4_serial_init - module init
2819  */
2820 int ioc4_serial_init(void)
2821 {
2822         int ret;
2823
2824         /* register with serial core */
2825         if ((ret = uart_register_driver(&ioc4_uart)) < 0) {
2826                 printk(KERN_WARNING
2827                         "%s: Couldn't register IOC4 serial driver\n",
2828                         __FUNCTION__);
2829                 return ret;
2830         }
2831
2832         /* register with IOC4 main module */
2833         return ioc4_register_submodule(&ioc4_serial_submodule);
2834 }
2835
2836 static void __devexit ioc4_serial_exit(void)
2837 {
2838         ioc4_unregister_submodule(&ioc4_serial_submodule);
2839         uart_unregister_driver(&ioc4_uart);
2840 }
2841
2842 module_init(ioc4_serial_init);
2843 module_exit(ioc4_serial_exit);
2844
2845 MODULE_AUTHOR("Pat Gefre - Silicon Graphics Inc. (SGI) <pfg@sgi.com>");
2846 MODULE_DESCRIPTION("Serial PCI driver module for SGI IOC4 Base-IO Card");
2847 MODULE_LICENSE("GPL");