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