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