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