tty: add SPDX identifiers to all remaining files in drivers/tty/
[linux-block.git] / drivers / tty / serial / ioc3_serial.c
1 // SPDX-License-Identifier: GPL-2.0
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  *
7  * Copyright (C) 2005 Silicon Graphics, Inc.  All Rights Reserved.
8  */
9
10 /*
11  * This file contains a module version of the ioc3 serial driver. This
12  * includes all the support functions needed (support functions, etc.)
13  * and the serial driver itself.
14  */
15 #include <linux/errno.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18 #include <linux/serial.h>
19 #include <linux/circ_buf.h>
20 #include <linux/serial_reg.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/serial_core.h>
24 #include <linux/ioc3.h>
25 #include <linux/slab.h>
26
27 /*
28  * Interesting things about the ioc3
29  */
30
31 #define LOGICAL_PORTS           2       /* rs232(0) and rs422(1) */
32 #define PORTS_PER_CARD          2
33 #define LOGICAL_PORTS_PER_CARD (PORTS_PER_CARD * LOGICAL_PORTS)
34 #define MAX_CARDS               8
35 #define MAX_LOGICAL_PORTS       (LOGICAL_PORTS_PER_CARD * MAX_CARDS)
36
37 /* determine given the sio_ir what port it applies to */
38 #define GET_PORT_FROM_SIO_IR(_x)        (_x & SIO_IR_SA) ? 0 : 1
39
40
41 /*
42  * we have 2 logical ports (rs232, rs422) for each physical port
43  * evens are rs232, odds are rs422
44  */
45 #define GET_PHYSICAL_PORT(_x)   ((_x) >> 1)
46 #define GET_LOGICAL_PORT(_x)    ((_x) & 1)
47 #define IS_PHYSICAL_PORT(_x)    !((_x) & 1)
48 #define IS_RS232(_x)            !((_x) & 1)
49
50 static unsigned int Num_of_ioc3_cards;
51 static unsigned int Submodule_slot;
52
53 /* defining this will get you LOTS of great debug info */
54 //#define DEBUG_INTERRUPTS
55 #define DPRINT_CONFIG(_x...)    ;
56 //#define DPRINT_CONFIG(_x...)  printk _x
57 #define NOT_PROGRESS()  ;
58 //#define NOT_PROGRESS()        printk("%s : fails %d\n", __func__, __LINE__)
59
60 /* number of characters we want to transmit to the lower level at a time */
61 #define MAX_CHARS               256
62 #define FIFO_SIZE               (MAX_CHARS-1)   /* it's a uchar */
63
64 /* Device name we're using */
65 #define DEVICE_NAME             "ttySIOC"
66 #define DEVICE_MAJOR            204
67 #define DEVICE_MINOR            116
68
69 /* flags for next_char_state */
70 #define NCS_BREAK               0x1
71 #define NCS_PARITY              0x2
72 #define NCS_FRAMING             0x4
73 #define NCS_OVERRUN             0x8
74
75 /* cause we need SOME parameters ... */
76 #define MIN_BAUD_SUPPORTED      1200
77 #define MAX_BAUD_SUPPORTED      115200
78
79 /* protocol types supported */
80 #define PROTO_RS232             0
81 #define PROTO_RS422             1
82
83 /* Notification types */
84 #define N_DATA_READY            0x01
85 #define N_OUTPUT_LOWAT          0x02
86 #define N_BREAK                 0x04
87 #define N_PARITY_ERROR          0x08
88 #define N_FRAMING_ERROR         0x10
89 #define N_OVERRUN_ERROR         0x20
90 #define N_DDCD                  0x40
91 #define N_DCTS                  0x80
92
93 #define N_ALL_INPUT             (N_DATA_READY | N_BREAK                    \
94                                         | N_PARITY_ERROR | N_FRAMING_ERROR \
95                                         | N_OVERRUN_ERROR | N_DDCD | N_DCTS)
96
97 #define N_ALL_OUTPUT            N_OUTPUT_LOWAT
98
99 #define N_ALL_ERRORS            (N_PARITY_ERROR | N_FRAMING_ERROR \
100                                                 | N_OVERRUN_ERROR)
101
102 #define N_ALL                   (N_DATA_READY | N_OUTPUT_LOWAT | N_BREAK    \
103                                         | N_PARITY_ERROR | N_FRAMING_ERROR  \
104                                         | N_OVERRUN_ERROR | N_DDCD | N_DCTS)
105
106 #define SER_CLK_SPEED(prediv)   ((22000000 << 1) / prediv)
107 #define SER_DIVISOR(x, clk)     (((clk) + (x) * 8) / ((x) * 16))
108 #define DIVISOR_TO_BAUD(div, clk) ((clk) / 16 / (div))
109
110 /* Some masks */
111 #define LCR_MASK_BITS_CHAR      (UART_LCR_WLEN5 | UART_LCR_WLEN6 \
112                                         | UART_LCR_WLEN7 | UART_LCR_WLEN8)
113 #define LCR_MASK_STOP_BITS      (UART_LCR_STOP)
114
115 #define PENDING(_a, _p)         (readl(&(_p)->vma->sio_ir) & (_a)->ic_enable)
116
117 #define RING_BUF_SIZE           4096
118 #define BUF_SIZE_BIT            SBBR_L_SIZE
119 #define PROD_CONS_MASK          PROD_CONS_PTR_4K
120
121 #define TOTAL_RING_BUF_SIZE     (RING_BUF_SIZE * 4)
122
123 /* driver specific - one per card */
124 struct ioc3_card {
125         struct {
126                 /* uart ports are allocated here */
127                 struct uart_port icp_uart_port[LOGICAL_PORTS];
128                 /* the ioc3_port used for this port */
129                 struct ioc3_port *icp_port;
130         } ic_port[PORTS_PER_CARD];
131         /* currently enabled interrupts */
132         uint32_t ic_enable;
133 };
134
135 /* Local port info for each IOC3 serial port */
136 struct ioc3_port {
137         /* handy reference material */
138         struct uart_port *ip_port;
139         struct ioc3_card *ip_card;
140         struct ioc3_driver_data *ip_idd;
141         struct ioc3_submodule *ip_is;
142
143         /* pci mem addresses for this port */
144         struct ioc3_serialregs __iomem *ip_serial_regs;
145         struct ioc3_uartregs __iomem *ip_uart_regs;
146
147         /* Ring buffer page for this port */
148         dma_addr_t ip_dma_ringbuf;
149         /* vaddr of ring buffer */
150         struct ring_buffer *ip_cpu_ringbuf;
151
152         /* Rings for this port */
153         struct ring *ip_inring;
154         struct ring *ip_outring;
155
156         /* Hook to port specific values */
157         struct port_hooks *ip_hooks;
158
159         spinlock_t ip_lock;
160
161         /* Various rx/tx parameters */
162         int ip_baud;
163         int ip_tx_lowat;
164         int ip_rx_timeout;
165
166         /* Copy of notification bits */
167         int ip_notify;
168
169         /* Shadow copies of various registers so we don't need to PIO
170          * read them constantly
171          */
172         uint32_t ip_sscr;
173         uint32_t ip_tx_prod;
174         uint32_t ip_rx_cons;
175         unsigned char ip_flags;
176 };
177
178 /* tx low water mark.  We need to notify the driver whenever tx is getting
179  * close to empty so it can refill the tx buffer and keep things going.
180  * Let's assume that if we interrupt 1 ms before the tx goes idle, we'll
181  * have no trouble getting in more chars in time (I certainly hope so).
182  */
183 #define TX_LOWAT_LATENCY      1000
184 #define TX_LOWAT_HZ          (1000000 / TX_LOWAT_LATENCY)
185 #define TX_LOWAT_CHARS(baud) (baud / 10 / TX_LOWAT_HZ)
186
187 /* Flags per port */
188 #define INPUT_HIGH              0x01
189         /* used to signify that we have turned off the rx_high
190          * temporarily - we need to drain the fifo and don't
191          * want to get blasted with interrupts.
192          */
193 #define DCD_ON                  0x02
194         /* DCD state is on */
195 #define LOWAT_WRITTEN           0x04
196 #define READ_ABORTED            0x08
197         /* the read was aborted - used to avaoid infinate looping
198          * in the interrupt handler
199          */
200 #define INPUT_ENABLE            0x10
201
202 /* Since each port has different register offsets and bitmasks
203  * for everything, we'll store those that we need in tables so we
204  * don't have to be constantly checking the port we are dealing with.
205  */
206 struct port_hooks {
207         uint32_t intr_delta_dcd;
208         uint32_t intr_delta_cts;
209         uint32_t intr_tx_mt;
210         uint32_t intr_rx_timer;
211         uint32_t intr_rx_high;
212         uint32_t intr_tx_explicit;
213         uint32_t intr_clear;
214         uint32_t intr_all;
215         char rs422_select_pin;
216 };
217
218 static struct port_hooks hooks_array[PORTS_PER_CARD] = {
219         /* values for port A */
220         {
221         .intr_delta_dcd = SIO_IR_SA_DELTA_DCD,
222         .intr_delta_cts = SIO_IR_SA_DELTA_CTS,
223         .intr_tx_mt = SIO_IR_SA_TX_MT,
224         .intr_rx_timer = SIO_IR_SA_RX_TIMER,
225         .intr_rx_high = SIO_IR_SA_RX_HIGH,
226         .intr_tx_explicit = SIO_IR_SA_TX_EXPLICIT,
227         .intr_clear = (SIO_IR_SA_TX_MT | SIO_IR_SA_RX_FULL
228                                 | SIO_IR_SA_RX_HIGH
229                                 | SIO_IR_SA_RX_TIMER
230                                 | SIO_IR_SA_DELTA_DCD
231                                 | SIO_IR_SA_DELTA_CTS
232                                 | SIO_IR_SA_INT
233                                 | SIO_IR_SA_TX_EXPLICIT
234                                 | SIO_IR_SA_MEMERR),
235         .intr_all =  SIO_IR_SA,
236         .rs422_select_pin = GPPR_UARTA_MODESEL_PIN,
237          },
238
239         /* values for port B */
240         {
241         .intr_delta_dcd = SIO_IR_SB_DELTA_DCD,
242         .intr_delta_cts = SIO_IR_SB_DELTA_CTS,
243         .intr_tx_mt = SIO_IR_SB_TX_MT,
244         .intr_rx_timer = SIO_IR_SB_RX_TIMER,
245         .intr_rx_high = SIO_IR_SB_RX_HIGH,
246         .intr_tx_explicit = SIO_IR_SB_TX_EXPLICIT,
247         .intr_clear = (SIO_IR_SB_TX_MT | SIO_IR_SB_RX_FULL
248                                 | SIO_IR_SB_RX_HIGH
249                                 | SIO_IR_SB_RX_TIMER
250                                 | SIO_IR_SB_DELTA_DCD
251                                 | SIO_IR_SB_DELTA_CTS
252                                 | SIO_IR_SB_INT
253                                 | SIO_IR_SB_TX_EXPLICIT
254                                 | SIO_IR_SB_MEMERR),
255         .intr_all = SIO_IR_SB,
256         .rs422_select_pin = GPPR_UARTB_MODESEL_PIN,
257          }
258 };
259
260 struct ring_entry {
261         union {
262                 struct {
263                         uint32_t alldata;
264                         uint32_t allsc;
265                 } all;
266                 struct {
267                         char data[4];   /* data bytes */
268                         char sc[4];     /* status/control */
269                 } s;
270         } u;
271 };
272
273 /* Test the valid bits in any of the 4 sc chars using "allsc" member */
274 #define RING_ANY_VALID \
275         ((uint32_t)(RXSB_MODEM_VALID | RXSB_DATA_VALID) * 0x01010101)
276
277 #define ring_sc         u.s.sc
278 #define ring_data       u.s.data
279 #define ring_allsc      u.all.allsc
280
281 /* Number of entries per ring buffer. */
282 #define ENTRIES_PER_RING (RING_BUF_SIZE / (int) sizeof(struct ring_entry))
283
284 /* An individual ring */
285 struct ring {
286         struct ring_entry entries[ENTRIES_PER_RING];
287 };
288
289 /* The whole enchilada */
290 struct ring_buffer {
291         struct ring TX_A;
292         struct ring RX_A;
293         struct ring TX_B;
294         struct ring RX_B;
295 };
296
297 /* Get a ring from a port struct */
298 #define RING(_p, _wh)   &(((struct ring_buffer *)((_p)->ip_cpu_ringbuf))->_wh)
299
300 /* for Infinite loop detection  */
301 #define MAXITER         10000000
302
303
304 /**
305  * set_baud - Baud rate setting code
306  * @port: port to set
307  * @baud: baud rate to use
308  */
309 static int set_baud(struct ioc3_port *port, int baud)
310 {
311         int divisor;
312         int actual_baud;
313         int diff;
314         int lcr, prediv;
315         struct ioc3_uartregs __iomem *uart;
316
317         for (prediv = 6; prediv < 64; prediv++) {
318                 divisor = SER_DIVISOR(baud, SER_CLK_SPEED(prediv));
319                 if (!divisor)
320                         continue;       /* invalid divisor */
321                 actual_baud = DIVISOR_TO_BAUD(divisor, SER_CLK_SPEED(prediv));
322
323                 diff = actual_baud - baud;
324                 if (diff < 0)
325                         diff = -diff;
326
327                 /* if we're within 1% we've found a match */
328                 if (diff * 100 <= actual_baud)
329                         break;
330         }
331
332         /* if the above loop completed, we didn't match
333          * the baud rate.  give up.
334          */
335         if (prediv == 64) {
336                 NOT_PROGRESS();
337                 return 1;
338         }
339
340         uart = port->ip_uart_regs;
341         lcr = readb(&uart->iu_lcr);
342
343         writeb(lcr | UART_LCR_DLAB, &uart->iu_lcr);
344         writeb((unsigned char)divisor, &uart->iu_dll);
345         writeb((unsigned char)(divisor >> 8), &uart->iu_dlm);
346         writeb((unsigned char)prediv, &uart->iu_scr);
347         writeb((unsigned char)lcr, &uart->iu_lcr);
348
349         return 0;
350 }
351
352 /**
353  * get_ioc3_port - given a uart port, return the control structure
354  * @the_port: uart port to find
355  */
356 static struct ioc3_port *get_ioc3_port(struct uart_port *the_port)
357 {
358         struct ioc3_driver_data *idd = dev_get_drvdata(the_port->dev);
359         struct ioc3_card *card_ptr = idd->data[Submodule_slot];
360         int ii, jj;
361
362         if (!card_ptr) {
363                 NOT_PROGRESS();
364                 return NULL;
365         }
366         for (ii = 0; ii < PORTS_PER_CARD; ii++) {
367                 for (jj = 0; jj < LOGICAL_PORTS; jj++) {
368                         if (the_port == &card_ptr->ic_port[ii].icp_uart_port[jj])
369                                 return card_ptr->ic_port[ii].icp_port;
370                 }
371         }
372         NOT_PROGRESS();
373         return NULL;
374 }
375
376 /**
377  * port_init - Initialize the sio and ioc3 hardware for a given port
378  *                      called per port from attach...
379  * @port: port to initialize
380  */
381 static inline int port_init(struct ioc3_port *port)
382 {
383         uint32_t sio_cr;
384         struct port_hooks *hooks = port->ip_hooks;
385         struct ioc3_uartregs __iomem *uart;
386         int reset_loop_counter = 0xfffff;
387         struct ioc3_driver_data *idd = port->ip_idd;
388
389         /* Idle the IOC3 serial interface */
390         writel(SSCR_RESET, &port->ip_serial_regs->sscr);
391
392         /* Wait until any pending bus activity for this port has ceased */
393         do {
394                 sio_cr = readl(&idd->vma->sio_cr);
395                 if (reset_loop_counter-- <= 0) {
396                         printk(KERN_WARNING
397                                "IOC3 unable to come out of reset"
398                                 " scr 0x%x\n", sio_cr);
399                         return -1;
400                 }
401         } while (!(sio_cr & SIO_CR_ARB_DIAG_IDLE) &&
402                (((sio_cr &= SIO_CR_ARB_DIAG) == SIO_CR_ARB_DIAG_TXA)
403                 || sio_cr == SIO_CR_ARB_DIAG_TXB
404                 || sio_cr == SIO_CR_ARB_DIAG_RXA
405                 || sio_cr == SIO_CR_ARB_DIAG_RXB));
406
407         /* Finish reset sequence */
408         writel(0, &port->ip_serial_regs->sscr);
409
410         /* Once RESET is done, reload cached tx_prod and rx_cons values
411          * and set rings to empty by making prod == cons
412          */
413         port->ip_tx_prod = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
414         writel(port->ip_tx_prod, &port->ip_serial_regs->stpir);
415         port->ip_rx_cons = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
416         writel(port->ip_rx_cons | SRCIR_ARM, &port->ip_serial_regs->srcir);
417
418         /* Disable interrupts for this 16550 */
419         uart = port->ip_uart_regs;
420         writeb(0, &uart->iu_lcr);
421         writeb(0, &uart->iu_ier);
422
423         /* Set the default baud */
424         set_baud(port, port->ip_baud);
425
426         /* Set line control to 8 bits no parity */
427         writeb(UART_LCR_WLEN8 | 0, &uart->iu_lcr);
428         /* UART_LCR_STOP == 1 stop */
429
430         /* Enable the FIFOs */
431         writeb(UART_FCR_ENABLE_FIFO, &uart->iu_fcr);
432         /* then reset 16550 FIFOs */
433         writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
434                &uart->iu_fcr);
435
436         /* Clear modem control register */
437         writeb(0, &uart->iu_mcr);
438
439         /* Clear deltas in modem status register */
440         writel(0, &port->ip_serial_regs->shadow);
441
442         /* Only do this once per port pair */
443         if (port->ip_hooks == &hooks_array[0]) {
444                 unsigned long ring_pci_addr;
445                 uint32_t __iomem *sbbr_l, *sbbr_h;
446
447                 sbbr_l = &idd->vma->sbbr_l;
448                 sbbr_h = &idd->vma->sbbr_h;
449                 ring_pci_addr = (unsigned long __iomem)port->ip_dma_ringbuf;
450                 DPRINT_CONFIG(("%s: ring_pci_addr 0x%p\n",
451                                __func__, (void *)ring_pci_addr));
452
453                 writel((unsigned int)((uint64_t) ring_pci_addr >> 32), sbbr_h);
454                 writel((unsigned int)ring_pci_addr | BUF_SIZE_BIT, sbbr_l);
455         }
456
457         /* Set the receive timeout value to 10 msec */
458         writel(SRTR_HZ / 100, &port->ip_serial_regs->srtr);
459
460         /* Set rx threshold, enable DMA */
461         /* Set high water mark at 3/4 of full ring */
462         port->ip_sscr = (ENTRIES_PER_RING * 3 / 4);
463
464         /* uart experiences pauses at high baud rate reducing actual
465          * throughput by 10% or so unless we enable high speed polling
466          * XXX when this hardware bug is resolved we should revert to
467          * normal polling speed
468          */
469         port->ip_sscr |= SSCR_HIGH_SPD;
470
471         writel(port->ip_sscr, &port->ip_serial_regs->sscr);
472
473         /* Disable and clear all serial related interrupt bits */
474         port->ip_card->ic_enable &= ~hooks->intr_clear;
475         ioc3_disable(port->ip_is, idd, hooks->intr_clear);
476         ioc3_ack(port->ip_is, idd, hooks->intr_clear);
477         return 0;
478 }
479
480 /**
481  * enable_intrs - enable interrupts
482  * @port: port to enable
483  * @mask: mask to use
484  */
485 static void enable_intrs(struct ioc3_port *port, uint32_t mask)
486 {
487         if ((port->ip_card->ic_enable & mask) != mask) {
488                 port->ip_card->ic_enable |= mask;
489                 ioc3_enable(port->ip_is, port->ip_idd, mask);
490         }
491 }
492
493 /**
494  * local_open - local open a port
495  * @port: port to open
496  */
497 static inline int local_open(struct ioc3_port *port)
498 {
499         int spiniter = 0;
500
501         port->ip_flags = INPUT_ENABLE;
502
503         /* Pause the DMA interface if necessary */
504         if (port->ip_sscr & SSCR_DMA_EN) {
505                 writel(port->ip_sscr | SSCR_DMA_PAUSE,
506                        &port->ip_serial_regs->sscr);
507                 while ((readl(&port->ip_serial_regs->sscr)
508                         & SSCR_PAUSE_STATE) == 0) {
509                         spiniter++;
510                         if (spiniter > MAXITER) {
511                                 NOT_PROGRESS();
512                                 return -1;
513                         }
514                 }
515         }
516
517         /* Reset the input fifo.  If the uart received chars while the port
518          * was closed and DMA is not enabled, the uart may have a bunch of
519          * chars hanging around in its rx fifo which will not be discarded
520          * by rclr in the upper layer. We must get rid of them here.
521          */
522         writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR,
523                &port->ip_uart_regs->iu_fcr);
524
525         writeb(UART_LCR_WLEN8, &port->ip_uart_regs->iu_lcr);
526         /* UART_LCR_STOP == 1 stop */
527
528         /* Re-enable DMA, set default threshold to intr whenever there is
529          * data available.
530          */
531         port->ip_sscr &= ~SSCR_RX_THRESHOLD;
532         port->ip_sscr |= 1;     /* default threshold */
533
534         /* Plug in the new sscr.  This implicitly clears the DMA_PAUSE
535          * flag if it was set above
536          */
537         writel(port->ip_sscr, &port->ip_serial_regs->sscr);
538         port->ip_tx_lowat = 1;
539         return 0;
540 }
541
542 /**
543  * set_rx_timeout - Set rx timeout and threshold values.
544  * @port: port to use
545  * @timeout: timeout value in ticks
546  */
547 static inline int set_rx_timeout(struct ioc3_port *port, int timeout)
548 {
549         int threshold;
550
551         port->ip_rx_timeout = timeout;
552
553         /* Timeout is in ticks.  Let's figure out how many chars we
554          * can receive at the current baud rate in that interval
555          * and set the rx threshold to that amount.  There are 4 chars
556          * per ring entry, so we'll divide the number of chars that will
557          * arrive in timeout by 4.
558          * So .... timeout * baud / 10 / HZ / 4, with HZ = 100.
559          */
560         threshold = timeout * port->ip_baud / 4000;
561         if (threshold == 0)
562                 threshold = 1;  /* otherwise we'll intr all the time! */
563
564         if ((unsigned)threshold > (unsigned)SSCR_RX_THRESHOLD)
565                 return 1;
566
567         port->ip_sscr &= ~SSCR_RX_THRESHOLD;
568         port->ip_sscr |= threshold;
569         writel(port->ip_sscr, &port->ip_serial_regs->sscr);
570
571         /* Now set the rx timeout to the given value
572          * again timeout * SRTR_HZ / HZ
573          */
574         timeout = timeout * SRTR_HZ / 100;
575         if (timeout > SRTR_CNT)
576                 timeout = SRTR_CNT;
577         writel(timeout, &port->ip_serial_regs->srtr);
578         return 0;
579 }
580
581 /**
582  * config_port - config the hardware
583  * @port: port to config
584  * @baud: baud rate for the port
585  * @byte_size: data size
586  * @stop_bits: number of stop bits
587  * @parenb: parity enable ?
588  * @parodd: odd parity ?
589  */
590 static inline int
591 config_port(struct ioc3_port *port,
592             int baud, int byte_size, int stop_bits, int parenb, int parodd)
593 {
594         char lcr, sizebits;
595         int spiniter = 0;
596
597         DPRINT_CONFIG(("%s: line %d baud %d byte_size %d stop %d parenb %d "
598                         "parodd %d\n",
599                        __func__, ((struct uart_port *)port->ip_port)->line,
600                         baud, byte_size, stop_bits, parenb, parodd));
601
602         if (set_baud(port, baud))
603                 return 1;
604
605         switch (byte_size) {
606         case 5:
607                 sizebits = UART_LCR_WLEN5;
608                 break;
609         case 6:
610                 sizebits = UART_LCR_WLEN6;
611                 break;
612         case 7:
613                 sizebits = UART_LCR_WLEN7;
614                 break;
615         case 8:
616                 sizebits = UART_LCR_WLEN8;
617                 break;
618         default:
619                 return 1;
620         }
621
622         /* Pause the DMA interface if necessary */
623         if (port->ip_sscr & SSCR_DMA_EN) {
624                 writel(port->ip_sscr | SSCR_DMA_PAUSE,
625                        &port->ip_serial_regs->sscr);
626                 while ((readl(&port->ip_serial_regs->sscr)
627                         & SSCR_PAUSE_STATE) == 0) {
628                         spiniter++;
629                         if (spiniter > MAXITER)
630                                 return -1;
631                 }
632         }
633
634         /* Clear relevant fields in lcr */
635         lcr = readb(&port->ip_uart_regs->iu_lcr);
636         lcr &= ~(LCR_MASK_BITS_CHAR | UART_LCR_EPAR |
637                  UART_LCR_PARITY | LCR_MASK_STOP_BITS);
638
639         /* Set byte size in lcr */
640         lcr |= sizebits;
641
642         /* Set parity */
643         if (parenb) {
644                 lcr |= UART_LCR_PARITY;
645                 if (!parodd)
646                         lcr |= UART_LCR_EPAR;
647         }
648
649         /* Set stop bits */
650         if (stop_bits)
651                 lcr |= UART_LCR_STOP /* 2 stop bits */ ;
652
653         writeb(lcr, &port->ip_uart_regs->iu_lcr);
654
655         /* Re-enable the DMA interface if necessary */
656         if (port->ip_sscr & SSCR_DMA_EN) {
657                 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
658         }
659         port->ip_baud = baud;
660
661         /* When we get within this number of ring entries of filling the
662          * entire ring on tx, place an EXPLICIT intr to generate a lowat
663          * notification when output has drained.
664          */
665         port->ip_tx_lowat = (TX_LOWAT_CHARS(baud) + 3) / 4;
666         if (port->ip_tx_lowat == 0)
667                 port->ip_tx_lowat = 1;
668
669         set_rx_timeout(port, 2);
670         return 0;
671 }
672
673 /**
674  * do_write - Write bytes to the port.  Returns the number of bytes
675  *                      actually written. Called from transmit_chars
676  * @port: port to use
677  * @buf: the stuff to write
678  * @len: how many bytes in 'buf'
679  */
680 static inline int do_write(struct ioc3_port *port, char *buf, int len)
681 {
682         int prod_ptr, cons_ptr, total = 0;
683         struct ring *outring;
684         struct ring_entry *entry;
685         struct port_hooks *hooks = port->ip_hooks;
686
687         BUG_ON(!(len >= 0));
688
689         prod_ptr = port->ip_tx_prod;
690         cons_ptr = readl(&port->ip_serial_regs->stcir) & PROD_CONS_MASK;
691         outring = port->ip_outring;
692
693         /* Maintain a 1-entry red-zone.  The ring buffer is full when
694          * (cons - prod) % ring_size is 1.  Rather than do this subtraction
695          * in the body of the loop, I'll do it now.
696          */
697         cons_ptr = (cons_ptr - (int)sizeof(struct ring_entry)) & PROD_CONS_MASK;
698
699         /* Stuff the bytes into the output */
700         while ((prod_ptr != cons_ptr) && (len > 0)) {
701                 int xx;
702
703                 /* Get 4 bytes (one ring entry) at a time */
704                 entry = (struct ring_entry *)((caddr_t) outring + prod_ptr);
705
706                 /* Invalidate all entries */
707                 entry->ring_allsc = 0;
708
709                 /* Copy in some bytes */
710                 for (xx = 0; (xx < 4) && (len > 0); xx++) {
711                         entry->ring_data[xx] = *buf++;
712                         entry->ring_sc[xx] = TXCB_VALID;
713                         len--;
714                         total++;
715                 }
716
717                 /* If we are within some small threshold of filling up the
718                  * entire ring buffer, we must place an EXPLICIT intr here
719                  * to generate a lowat interrupt in case we subsequently
720                  * really do fill up the ring and the caller goes to sleep.
721                  * No need to place more than one though.
722                  */
723                 if (!(port->ip_flags & LOWAT_WRITTEN) &&
724                     ((cons_ptr - prod_ptr) & PROD_CONS_MASK)
725                     <= port->ip_tx_lowat * (int)sizeof(struct ring_entry)) {
726                         port->ip_flags |= LOWAT_WRITTEN;
727                         entry->ring_sc[0] |= TXCB_INT_WHEN_DONE;
728                 }
729
730                 /* Go on to next entry */
731                 prod_ptr += sizeof(struct ring_entry);
732                 prod_ptr &= PROD_CONS_MASK;
733         }
734
735         /* If we sent something, start DMA if necessary */
736         if (total > 0 && !(port->ip_sscr & SSCR_DMA_EN)) {
737                 port->ip_sscr |= SSCR_DMA_EN;
738                 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
739         }
740
741         /* Store the new producer pointer.  If tx is disabled, we stuff the
742          * data into the ring buffer, but we don't actually start tx.
743          */
744         if (!uart_tx_stopped(port->ip_port)) {
745                 writel(prod_ptr, &port->ip_serial_regs->stpir);
746
747                 /* If we are now transmitting, enable tx_mt interrupt so we
748                  * can disable DMA if necessary when the tx finishes.
749                  */
750                 if (total > 0)
751                         enable_intrs(port, hooks->intr_tx_mt);
752         }
753         port->ip_tx_prod = prod_ptr;
754
755         return total;
756 }
757
758 /**
759  * disable_intrs - disable interrupts
760  * @port: port to enable
761  * @mask: mask to use
762  */
763 static inline void disable_intrs(struct ioc3_port *port, uint32_t mask)
764 {
765         if (port->ip_card->ic_enable & mask) {
766                 ioc3_disable(port->ip_is, port->ip_idd, mask);
767                 port->ip_card->ic_enable &= ~mask;
768         }
769 }
770
771 /**
772  * set_notification - Modify event notification
773  * @port: port to use
774  * @mask: events mask
775  * @set_on: set ?
776  */
777 static int set_notification(struct ioc3_port *port, int mask, int set_on)
778 {
779         struct port_hooks *hooks = port->ip_hooks;
780         uint32_t intrbits, sscrbits;
781
782         BUG_ON(!mask);
783
784         intrbits = sscrbits = 0;
785
786         if (mask & N_DATA_READY)
787                 intrbits |= (hooks->intr_rx_timer | hooks->intr_rx_high);
788         if (mask & N_OUTPUT_LOWAT)
789                 intrbits |= hooks->intr_tx_explicit;
790         if (mask & N_DDCD) {
791                 intrbits |= hooks->intr_delta_dcd;
792                 sscrbits |= SSCR_RX_RING_DCD;
793         }
794         if (mask & N_DCTS)
795                 intrbits |= hooks->intr_delta_cts;
796
797         if (set_on) {
798                 enable_intrs(port, intrbits);
799                 port->ip_notify |= mask;
800                 port->ip_sscr |= sscrbits;
801         } else {
802                 disable_intrs(port, intrbits);
803                 port->ip_notify &= ~mask;
804                 port->ip_sscr &= ~sscrbits;
805         }
806
807         /* We require DMA if either DATA_READY or DDCD notification is
808          * currently requested. If neither of these is requested and
809          * there is currently no tx in progress, DMA may be disabled.
810          */
811         if (port->ip_notify & (N_DATA_READY | N_DDCD))
812                 port->ip_sscr |= SSCR_DMA_EN;
813         else if (!(port->ip_card->ic_enable & hooks->intr_tx_mt))
814                 port->ip_sscr &= ~SSCR_DMA_EN;
815
816         writel(port->ip_sscr, &port->ip_serial_regs->sscr);
817         return 0;
818 }
819
820 /**
821  * set_mcr - set the master control reg
822  * @the_port: port to use
823  * @mask1: mcr mask
824  * @mask2: shadow mask
825  */
826 static inline int set_mcr(struct uart_port *the_port,
827                           int mask1, int mask2)
828 {
829         struct ioc3_port *port = get_ioc3_port(the_port);
830         uint32_t shadow;
831         int spiniter = 0;
832         char mcr;
833
834         if (!port)
835                 return -1;
836
837         /* Pause the DMA interface if necessary */
838         if (port->ip_sscr & SSCR_DMA_EN) {
839                 writel(port->ip_sscr | SSCR_DMA_PAUSE,
840                        &port->ip_serial_regs->sscr);
841                 while ((readl(&port->ip_serial_regs->sscr)
842                         & SSCR_PAUSE_STATE) == 0) {
843                         spiniter++;
844                         if (spiniter > MAXITER)
845                                 return -1;
846                 }
847         }
848         shadow = readl(&port->ip_serial_regs->shadow);
849         mcr = (shadow & 0xff000000) >> 24;
850
851         /* Set new value */
852         mcr |= mask1;
853         shadow |= mask2;
854         writeb(mcr, &port->ip_uart_regs->iu_mcr);
855         writel(shadow, &port->ip_serial_regs->shadow);
856
857         /* Re-enable the DMA interface if necessary */
858         if (port->ip_sscr & SSCR_DMA_EN) {
859                 writel(port->ip_sscr, &port->ip_serial_regs->sscr);
860         }
861         return 0;
862 }
863
864 /**
865  * ioc3_set_proto - set the protocol for the port
866  * @port: port to use
867  * @proto: protocol to use
868  */
869 static int ioc3_set_proto(struct ioc3_port *port, int proto)
870 {
871         struct port_hooks *hooks = port->ip_hooks;
872
873         switch (proto) {
874         default:
875         case PROTO_RS232:
876                 /* Clear the appropriate GIO pin */
877                 DPRINT_CONFIG(("%s: rs232\n", __func__));
878                 writel(0, (&port->ip_idd->vma->gppr[0]
879                                         + hooks->rs422_select_pin));
880                 break;
881
882         case PROTO_RS422:
883                 /* Set the appropriate GIO pin */
884                 DPRINT_CONFIG(("%s: rs422\n", __func__));
885                 writel(1, (&port->ip_idd->vma->gppr[0]
886                                         + hooks->rs422_select_pin));
887                 break;
888         }
889         return 0;
890 }
891
892 /**
893  * transmit_chars - upper level write, called with the_port->lock
894  * @the_port: port to write
895  */
896 static void transmit_chars(struct uart_port *the_port)
897 {
898         int xmit_count, tail, head;
899         int result;
900         char *start;
901         struct tty_struct *tty;
902         struct ioc3_port *port = get_ioc3_port(the_port);
903         struct uart_state *state;
904
905         if (!the_port)
906                 return;
907         if (!port)
908                 return;
909
910         state = the_port->state;
911         tty = state->port.tty;
912
913         if (uart_circ_empty(&state->xmit) || uart_tx_stopped(the_port)) {
914                 /* Nothing to do or hw stopped */
915                 set_notification(port, N_ALL_OUTPUT, 0);
916                 return;
917         }
918
919         head = state->xmit.head;
920         tail = state->xmit.tail;
921         start = (char *)&state->xmit.buf[tail];
922
923         /* write out all the data or until the end of the buffer */
924         xmit_count = (head < tail) ? (UART_XMIT_SIZE - tail) : (head - tail);
925         if (xmit_count > 0) {
926                 result = do_write(port, start, xmit_count);
927                 if (result > 0) {
928                         /* booking */
929                         xmit_count -= result;
930                         the_port->icount.tx += result;
931                         /* advance the pointers */
932                         tail += result;
933                         tail &= UART_XMIT_SIZE - 1;
934                         state->xmit.tail = tail;
935                         start = (char *)&state->xmit.buf[tail];
936                 }
937         }
938         if (uart_circ_chars_pending(&state->xmit) < WAKEUP_CHARS)
939                 uart_write_wakeup(the_port);
940
941         if (uart_circ_empty(&state->xmit)) {
942                 set_notification(port, N_OUTPUT_LOWAT, 0);
943         } else {
944                 set_notification(port, N_OUTPUT_LOWAT, 1);
945         }
946 }
947
948 /**
949  * ioc3_change_speed - change the speed of the port
950  * @the_port: port to change
951  * @new_termios: new termios settings
952  * @old_termios: old termios settings
953  */
954 static void
955 ioc3_change_speed(struct uart_port *the_port,
956                   struct ktermios *new_termios, struct ktermios *old_termios)
957 {
958         struct ioc3_port *port = get_ioc3_port(the_port);
959         unsigned int cflag, iflag;
960         int baud;
961         int new_parity = 0, new_parity_enable = 0, new_stop = 0, new_data = 8;
962         struct uart_state *state = the_port->state;
963
964         cflag = new_termios->c_cflag;
965         iflag = new_termios->c_iflag;
966
967         switch (cflag & CSIZE) {
968         case CS5:
969                 new_data = 5;
970                 break;
971         case CS6:
972                 new_data = 6;
973                 break;
974         case CS7:
975                 new_data = 7;
976                 break;
977         case CS8:
978                 new_data = 8;
979                 break;
980         default:
981                 /* cuz we always need a default ... */
982                 new_data = 5;
983                 break;
984         }
985         if (cflag & CSTOPB) {
986                 new_stop = 1;
987         }
988         if (cflag & PARENB) {
989                 new_parity_enable = 1;
990                 if (cflag & PARODD)
991                         new_parity = 1;
992         }
993         baud = uart_get_baud_rate(the_port, new_termios, old_termios,
994                                   MIN_BAUD_SUPPORTED, MAX_BAUD_SUPPORTED);
995         DPRINT_CONFIG(("%s: returned baud %d for line %d\n", __func__, baud,
996                                 the_port->line));
997
998         if (!the_port->fifosize)
999                 the_port->fifosize = FIFO_SIZE;
1000         uart_update_timeout(the_port, cflag, baud);
1001
1002         the_port->ignore_status_mask = N_ALL_INPUT;
1003
1004         state->port.low_latency = 1;
1005
1006         if (iflag & IGNPAR)
1007                 the_port->ignore_status_mask &= ~(N_PARITY_ERROR
1008                                                   | N_FRAMING_ERROR);
1009         if (iflag & IGNBRK) {
1010                 the_port->ignore_status_mask &= ~N_BREAK;
1011                 if (iflag & IGNPAR)
1012                         the_port->ignore_status_mask &= ~N_OVERRUN_ERROR;
1013         }
1014         if (!(cflag & CREAD)) {
1015                 /* ignore everything */
1016                 the_port->ignore_status_mask &= ~N_DATA_READY;
1017         }
1018
1019         if (cflag & CRTSCTS) {
1020                 /* enable hardware flow control */
1021                 port->ip_sscr |= SSCR_HFC_EN;
1022         }
1023         else {
1024                 /* disable hardware flow control */
1025                 port->ip_sscr &= ~SSCR_HFC_EN;
1026         }
1027         writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1028
1029         /* Set the configuration and proper notification call */
1030         DPRINT_CONFIG(("%s : port 0x%p line %d cflag 0%o "
1031                        "config_port(baud %d data %d stop %d penable %d "
1032                         " parity %d), notification 0x%x\n",
1033                        __func__, (void *)port, the_port->line, cflag, baud,
1034                        new_data, new_stop, new_parity_enable, new_parity,
1035                        the_port->ignore_status_mask));
1036
1037         if ((config_port(port, baud,    /* baud */
1038                          new_data,      /* byte size */
1039                          new_stop,      /* stop bits */
1040                          new_parity_enable,     /* set parity */
1041                          new_parity)) >= 0) {   /* parity 1==odd */
1042                 set_notification(port, the_port->ignore_status_mask, 1);
1043         }
1044 }
1045
1046 /**
1047  * ic3_startup_local - Start up the serial port - returns >= 0 if no errors
1048  * @the_port: Port to operate on
1049  */
1050 static inline int ic3_startup_local(struct uart_port *the_port)
1051 {
1052         struct ioc3_port *port;
1053
1054         if (!the_port) {
1055                 NOT_PROGRESS();
1056                 return -1;
1057         }
1058
1059         port = get_ioc3_port(the_port);
1060         if (!port) {
1061                 NOT_PROGRESS();
1062                 return -1;
1063         }
1064
1065         local_open(port);
1066
1067         /* set the protocol */
1068         ioc3_set_proto(port, IS_RS232(the_port->line) ? PROTO_RS232 :
1069                                                         PROTO_RS422);
1070         return 0;
1071 }
1072
1073 /*
1074  * ioc3_cb_output_lowat - called when the output low water mark is hit
1075  * @port: port to output
1076  */
1077 static void ioc3_cb_output_lowat(struct ioc3_port *port)
1078 {
1079         unsigned long pflags;
1080
1081         /* the_port->lock is set on the call here */
1082         if (port->ip_port) {
1083                 spin_lock_irqsave(&port->ip_port->lock, pflags);
1084                 transmit_chars(port->ip_port);
1085                 spin_unlock_irqrestore(&port->ip_port->lock, pflags);
1086         }
1087 }
1088
1089 /*
1090  * ioc3_cb_post_ncs - called for some basic errors
1091  * @port: port to use
1092  * @ncs: event
1093  */
1094 static void ioc3_cb_post_ncs(struct uart_port *the_port, int ncs)
1095 {
1096         struct uart_icount *icount;
1097
1098         icount = &the_port->icount;
1099
1100         if (ncs & NCS_BREAK)
1101                 icount->brk++;
1102         if (ncs & NCS_FRAMING)
1103                 icount->frame++;
1104         if (ncs & NCS_OVERRUN)
1105                 icount->overrun++;
1106         if (ncs & NCS_PARITY)
1107                 icount->parity++;
1108 }
1109
1110 /**
1111  * do_read - Read in bytes from the port.  Return the number of bytes
1112  *                      actually read.
1113  * @the_port: port to use
1114  * @buf: place to put the stuff we read
1115  * @len: how big 'buf' is
1116  */
1117
1118 static inline int do_read(struct uart_port *the_port, char *buf, int len)
1119 {
1120         int prod_ptr, cons_ptr, total;
1121         struct ioc3_port *port = get_ioc3_port(the_port);
1122         struct ring *inring;
1123         struct ring_entry *entry;
1124         struct port_hooks *hooks;
1125         int byte_num;
1126         char *sc;
1127         int loop_counter;
1128
1129         BUG_ON(!(len >= 0));
1130         BUG_ON(!port);
1131         hooks = port->ip_hooks;
1132
1133         /* There is a nasty timing issue in the IOC3. When the rx_timer
1134          * expires or the rx_high condition arises, we take an interrupt.
1135          * At some point while servicing the interrupt, we read bytes from
1136          * the ring buffer and re-arm the rx_timer.  However the rx_timer is
1137          * not started until the first byte is received *after* it is armed,
1138          * and any bytes pending in the rx construction buffers are not drained
1139          * to memory until either there are 4 bytes available or the rx_timer
1140          * expires.  This leads to a potential situation where data is left
1141          * in the construction buffers forever - 1 to 3 bytes were received
1142          * after the interrupt was generated but before the rx_timer was
1143          * re-armed. At that point as long as no subsequent bytes are received
1144          * the timer will never be started and the bytes will remain in the
1145          * construction buffer forever.  The solution is to execute a DRAIN
1146          * command after rearming the timer.  This way any bytes received before
1147          * the DRAIN will be drained to memory, and any bytes received after
1148          * the DRAIN will start the TIMER and be drained when it expires.
1149          * Luckily, this only needs to be done when the DMA buffer is empty
1150          * since there is no requirement that this function return all
1151          * available data as long as it returns some.
1152          */
1153         /* Re-arm the timer */
1154
1155         writel(port->ip_rx_cons | SRCIR_ARM, &port->ip_serial_regs->srcir);
1156
1157         prod_ptr = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
1158         cons_ptr = port->ip_rx_cons;
1159
1160         if (prod_ptr == cons_ptr) {
1161                 int reset_dma = 0;
1162
1163                 /* Input buffer appears empty, do a flush. */
1164
1165                 /* DMA must be enabled for this to work. */
1166                 if (!(port->ip_sscr & SSCR_DMA_EN)) {
1167                         port->ip_sscr |= SSCR_DMA_EN;
1168                         reset_dma = 1;
1169                 }
1170
1171                 /* Potential race condition: we must reload the srpir after
1172                  * issuing the drain command, otherwise we could think the rx
1173                  * buffer is empty, then take a very long interrupt, and when
1174                  * we come back it's full and we wait forever for the drain to
1175                  * complete.
1176                  */
1177                 writel(port->ip_sscr | SSCR_RX_DRAIN,
1178                        &port->ip_serial_regs->sscr);
1179                 prod_ptr = readl(&port->ip_serial_regs->srpir) & PROD_CONS_MASK;
1180
1181                 /* We must not wait for the DRAIN to complete unless there are
1182                  * at least 8 bytes (2 ring entries) available to receive the
1183                  * data otherwise the DRAIN will never complete and we'll
1184                  * deadlock here.
1185                  * In fact, to make things easier, I'll just ignore the flush if
1186                  * there is any data at all now available.
1187                  */
1188                 if (prod_ptr == cons_ptr) {
1189                         loop_counter = 0;
1190                         while (readl(&port->ip_serial_regs->sscr) &
1191                                SSCR_RX_DRAIN) {
1192                                 loop_counter++;
1193                                 if (loop_counter > MAXITER)
1194                                         return -1;
1195                         }
1196
1197                         /* SIGH. We have to reload the prod_ptr *again* since
1198                          * the drain may have caused it to change
1199                          */
1200                         prod_ptr = readl(&port->ip_serial_regs->srpir)
1201                             & PROD_CONS_MASK;
1202                 }
1203                 if (reset_dma) {
1204                         port->ip_sscr &= ~SSCR_DMA_EN;
1205                         writel(port->ip_sscr, &port->ip_serial_regs->sscr);
1206                 }
1207         }
1208         inring = port->ip_inring;
1209         port->ip_flags &= ~READ_ABORTED;
1210
1211         total = 0;
1212         loop_counter = 0xfffff; /* to avoid hangs */
1213
1214         /* Grab bytes from the hardware */
1215         while ((prod_ptr != cons_ptr) && (len > 0)) {
1216                 entry = (struct ring_entry *)((caddr_t) inring + cons_ptr);
1217
1218                 if (loop_counter-- <= 0) {
1219                         printk(KERN_WARNING "IOC3 serial: "
1220                                "possible hang condition/"
1221                                "port stuck on read (line %d).\n",
1222                                 the_port->line);
1223                         break;
1224                 }
1225
1226                 /* According to the producer pointer, this ring entry
1227                  * must contain some data.  But if the PIO happened faster
1228                  * than the DMA, the data may not be available yet, so let's
1229                  * wait until it arrives.
1230                  */
1231                 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
1232                         /* Indicate the read is aborted so we don't disable
1233                          * the interrupt thinking that the consumer is
1234                          * congested.
1235                          */
1236                         port->ip_flags |= READ_ABORTED;
1237                         len = 0;
1238                         break;
1239                 }
1240
1241                 /* Load the bytes/status out of the ring entry */
1242                 for (byte_num = 0; byte_num < 4 && len > 0; byte_num++) {
1243                         sc = &(entry->ring_sc[byte_num]);
1244
1245                         /* Check for change in modem state or overrun */
1246                         if ((*sc & RXSB_MODEM_VALID)
1247                             && (port->ip_notify & N_DDCD)) {
1248                                 /* Notify upper layer if DCD dropped */
1249                                 if ((port->ip_flags & DCD_ON)
1250                                     && !(*sc & RXSB_DCD)) {
1251                                         /* If we have already copied some data,
1252                                          * return it.  We'll pick up the carrier
1253                                          * drop on the next pass.  That way we
1254                                          * don't throw away the data that has
1255                                          * already been copied back to
1256                                          * the caller's buffer.
1257                                          */
1258                                         if (total > 0) {
1259                                                 len = 0;
1260                                                 break;
1261                                         }
1262                                         port->ip_flags &= ~DCD_ON;
1263
1264                                         /* Turn off this notification so the
1265                                          * carrier drop protocol won't see it
1266                                          * again when it does a read.
1267                                          */
1268                                         *sc &= ~RXSB_MODEM_VALID;
1269
1270                                         /* To keep things consistent, we need
1271                                          * to update the consumer pointer so
1272                                          * the next reader won't come in and
1273                                          * try to read the same ring entries
1274                                          * again. This must be done here before
1275                                          * the dcd change.
1276                                          */
1277
1278                                         if ((entry->ring_allsc & RING_ANY_VALID)
1279                                             == 0) {
1280                                                 cons_ptr += (int)sizeof
1281                                                     (struct ring_entry);
1282                                                 cons_ptr &= PROD_CONS_MASK;
1283                                         }
1284                                         writel(cons_ptr,
1285                                                &port->ip_serial_regs->srcir);
1286                                         port->ip_rx_cons = cons_ptr;
1287
1288                                         /* Notify upper layer of carrier drop */
1289                                         if ((port->ip_notify & N_DDCD)
1290                                             && port->ip_port) {
1291                                                 uart_handle_dcd_change
1292                                                         (port->ip_port, 0);
1293                                                 wake_up_interruptible
1294                                                     (&the_port->state->
1295                                                      port.delta_msr_wait);
1296                                         }
1297
1298                                         /* If we had any data to return, we
1299                                          * would have returned it above.
1300                                          */
1301                                         return 0;
1302                                 }
1303                         }
1304                         if (*sc & RXSB_MODEM_VALID) {
1305                                 /* Notify that an input overrun occurred */
1306                                 if ((*sc & RXSB_OVERRUN)
1307                                     && (port->ip_notify & N_OVERRUN_ERROR)) {
1308                                         ioc3_cb_post_ncs(the_port, NCS_OVERRUN);
1309                                 }
1310                                 /* Don't look at this byte again */
1311                                 *sc &= ~RXSB_MODEM_VALID;
1312                         }
1313
1314                         /* Check for valid data or RX errors */
1315                         if ((*sc & RXSB_DATA_VALID) &&
1316                             ((*sc & (RXSB_PAR_ERR
1317                                      | RXSB_FRAME_ERR | RXSB_BREAK))
1318                              && (port->ip_notify & (N_PARITY_ERROR
1319                                                     | N_FRAMING_ERROR
1320                                                     | N_BREAK)))) {
1321                                 /* There is an error condition on the next byte.
1322                                  * If we have already transferred some bytes,
1323                                  * we'll stop here. Otherwise if this is the
1324                                  * first byte to be read, we'll just transfer
1325                                  * it alone after notifying the
1326                                  * upper layer of its status.
1327                                  */
1328                                 if (total > 0) {
1329                                         len = 0;
1330                                         break;
1331                                 } else {
1332                                         if ((*sc & RXSB_PAR_ERR) &&
1333                                             (port->
1334                                              ip_notify & N_PARITY_ERROR)) {
1335                                                 ioc3_cb_post_ncs(the_port,
1336                                                                  NCS_PARITY);
1337                                         }
1338                                         if ((*sc & RXSB_FRAME_ERR) &&
1339                                             (port->
1340                                              ip_notify & N_FRAMING_ERROR)) {
1341                                                 ioc3_cb_post_ncs(the_port,
1342                                                                  NCS_FRAMING);
1343                                         }
1344                                         if ((*sc & RXSB_BREAK)
1345                                             && (port->ip_notify & N_BREAK)) {
1346                                                 ioc3_cb_post_ncs
1347                                                     (the_port, NCS_BREAK);
1348                                         }
1349                                         len = 1;
1350                                 }
1351                         }
1352                         if (*sc & RXSB_DATA_VALID) {
1353                                 *sc &= ~RXSB_DATA_VALID;
1354                                 *buf = entry->ring_data[byte_num];
1355                                 buf++;
1356                                 len--;
1357                                 total++;
1358                         }
1359                 }
1360
1361                 /* If we used up this entry entirely, go on to the next one,
1362                  * otherwise we must have run out of buffer space, so
1363                  * leave the consumer pointer here for the next read in case
1364                  * there are still unread bytes in this entry.
1365                  */
1366                 if ((entry->ring_allsc & RING_ANY_VALID) == 0) {
1367                         cons_ptr += (int)sizeof(struct ring_entry);
1368                         cons_ptr &= PROD_CONS_MASK;
1369                 }
1370         }
1371
1372         /* Update consumer pointer and re-arm rx timer interrupt */
1373         writel(cons_ptr, &port->ip_serial_regs->srcir);
1374         port->ip_rx_cons = cons_ptr;
1375
1376         /* If we have now dipped below the rx high water mark and we have
1377          * rx_high interrupt turned off, we can now turn it back on again.
1378          */
1379         if ((port->ip_flags & INPUT_HIGH) && (((prod_ptr - cons_ptr)
1380                                                & PROD_CONS_MASK) <
1381                                               ((port->
1382                                                 ip_sscr &
1383                                                 SSCR_RX_THRESHOLD)
1384                                                << PROD_CONS_PTR_OFF))) {
1385                 port->ip_flags &= ~INPUT_HIGH;
1386                 enable_intrs(port, hooks->intr_rx_high);
1387         }
1388         return total;
1389 }
1390
1391 /**
1392  * receive_chars - upper level read.
1393  * @the_port: port to read from
1394  */
1395 static int receive_chars(struct uart_port *the_port)
1396 {
1397         unsigned char ch[MAX_CHARS];
1398         int read_count = 0, read_room, flip = 0;
1399         struct uart_state *state = the_port->state;
1400         struct ioc3_port *port = get_ioc3_port(the_port);
1401         unsigned long pflags;
1402
1403         /* Make sure all the pointers are "good" ones */
1404         if (!state)
1405                 return 0;
1406
1407         if (!(port->ip_flags & INPUT_ENABLE))
1408                 return 0;
1409
1410         spin_lock_irqsave(&the_port->lock, pflags);
1411
1412         read_count = do_read(the_port, ch, MAX_CHARS);
1413         if (read_count > 0) {
1414                 flip = 1;
1415                 read_room = tty_insert_flip_string(&state->port, ch,
1416                                 read_count);
1417                 the_port->icount.rx += read_count;
1418         }
1419         spin_unlock_irqrestore(&the_port->lock, pflags);
1420
1421         if (flip)
1422                 tty_flip_buffer_push(&state->port);
1423
1424         return read_count;
1425 }
1426
1427 /**
1428  * ioc3uart_intr_one - lowest level (per port) interrupt handler.
1429  * @is : submodule
1430  * @idd: driver data
1431  * @pending: interrupts to handle
1432  */
1433
1434 static inline int
1435 ioc3uart_intr_one(struct ioc3_submodule *is,
1436                         struct ioc3_driver_data *idd,
1437                         unsigned int pending)
1438 {
1439         int port_num = GET_PORT_FROM_SIO_IR(pending);
1440         struct port_hooks *hooks;
1441         unsigned int rx_high_rd_aborted = 0;
1442         unsigned long flags;
1443         struct uart_port *the_port;
1444         struct ioc3_port *port;
1445         int loop_counter;
1446         struct ioc3_card *card_ptr;
1447         unsigned int sio_ir;
1448
1449         card_ptr = idd->data[is->id];
1450         port = card_ptr->ic_port[port_num].icp_port;
1451         hooks = port->ip_hooks;
1452
1453         /* Possible race condition here: The tx_mt interrupt bit may be
1454          * cleared without the intervention of the interrupt handler,
1455          * e.g. by a write.  If the top level interrupt handler reads a
1456          * tx_mt, then some other processor does a write, starting up
1457          * output, then we come in here, see the tx_mt and stop DMA, the
1458          * output started by the other processor will hang.  Thus we can
1459          * only rely on tx_mt being legitimate if it is read while the
1460          * port lock is held.  Therefore this bit must be ignored in the
1461          * passed in interrupt mask which was read by the top level
1462          * interrupt handler since the port lock was not held at the time
1463          * it was read.  We can only rely on this bit being accurate if it
1464          * is read while the port lock is held.  So we'll clear it for now,
1465          * and reload it later once we have the port lock.
1466          */
1467
1468         sio_ir = pending & ~(hooks->intr_tx_mt);
1469         spin_lock_irqsave(&port->ip_lock, flags);
1470
1471         loop_counter = MAXITER; /* to avoid hangs */
1472
1473         do {
1474                 uint32_t shadow;
1475
1476                 if (loop_counter-- <= 0) {
1477                         printk(KERN_WARNING "IOC3 serial: "
1478                                "possible hang condition/"
1479                                "port stuck on interrupt (line %d).\n",
1480                                 ((struct uart_port *)port->ip_port)->line);
1481                         break;
1482                 }
1483                 /* Handle a DCD change */
1484                 if (sio_ir & hooks->intr_delta_dcd) {
1485                         ioc3_ack(is, idd, hooks->intr_delta_dcd);
1486                         shadow = readl(&port->ip_serial_regs->shadow);
1487
1488                         if ((port->ip_notify & N_DDCD)
1489                             && (shadow & SHADOW_DCD)
1490                             && (port->ip_port)) {
1491                                 the_port = port->ip_port;
1492                                 uart_handle_dcd_change(the_port,
1493                                                 shadow & SHADOW_DCD);
1494                                 wake_up_interruptible
1495                                     (&the_port->state->port.delta_msr_wait);
1496                         } else if ((port->ip_notify & N_DDCD)
1497                                    && !(shadow & SHADOW_DCD)) {
1498                                 /* Flag delta DCD/no DCD */
1499                                 uart_handle_dcd_change(port->ip_port,
1500                                                 shadow & SHADOW_DCD);
1501                                 port->ip_flags |= DCD_ON;
1502                         }
1503                 }
1504
1505                 /* Handle a CTS change */
1506                 if (sio_ir & hooks->intr_delta_cts) {
1507                         ioc3_ack(is, idd, hooks->intr_delta_cts);
1508                         shadow = readl(&port->ip_serial_regs->shadow);
1509
1510                         if ((port->ip_notify & N_DCTS) && (port->ip_port)) {
1511                                 the_port = port->ip_port;
1512                                 uart_handle_cts_change(the_port, shadow
1513                                                 & SHADOW_CTS);
1514                                 wake_up_interruptible
1515                                     (&the_port->state->port.delta_msr_wait);
1516                         }
1517                 }
1518
1519                 /* rx timeout interrupt.  Must be some data available.  Put this
1520                  * before the check for rx_high since servicing this condition
1521                  * may cause that condition to clear.
1522                  */
1523                 if (sio_ir & hooks->intr_rx_timer) {
1524                         ioc3_ack(is, idd, hooks->intr_rx_timer);
1525                         if ((port->ip_notify & N_DATA_READY)
1526                                                 && (port->ip_port)) {
1527                                 receive_chars(port->ip_port);
1528                         }
1529                 }
1530
1531                 /* rx high interrupt. Must be after rx_timer.  */
1532                 else if (sio_ir & hooks->intr_rx_high) {
1533                         /* Data available, notify upper layer */
1534                         if ((port->ip_notify & N_DATA_READY) && port->ip_port) {
1535                                 receive_chars(port->ip_port);
1536                         }
1537
1538                         /* We can't ACK this interrupt.  If receive_chars didn't
1539                          * cause the condition to clear, we'll have to disable
1540                          * the interrupt until the data is drained.
1541                          * If the read was aborted, don't disable the interrupt
1542                          * as this may cause us to hang indefinitely.  An
1543                          * aborted read generally means that this interrupt
1544                          * hasn't been delivered to the cpu yet anyway, even
1545                          * though we see it as asserted when we read the sio_ir.
1546                          */
1547                         if ((sio_ir = PENDING(card_ptr, idd))
1548                                         & hooks->intr_rx_high) {
1549                                 if (port->ip_flags & READ_ABORTED) {
1550                                         rx_high_rd_aborted++;
1551                                 }
1552                                 else {
1553                                         card_ptr->ic_enable &= ~hooks->intr_rx_high;
1554                                         port->ip_flags |= INPUT_HIGH;
1555                                 }
1556                         }
1557                 }
1558
1559                 /* We got a low water interrupt: notify upper layer to
1560                  * send more data.  Must come before tx_mt since servicing
1561                  * this condition may cause that condition to clear.
1562                  */
1563                 if (sio_ir & hooks->intr_tx_explicit) {
1564                         port->ip_flags &= ~LOWAT_WRITTEN;
1565                         ioc3_ack(is, idd, hooks->intr_tx_explicit);
1566                         if (port->ip_notify & N_OUTPUT_LOWAT)
1567                                 ioc3_cb_output_lowat(port);
1568                 }
1569
1570                 /* Handle tx_mt.  Must come after tx_explicit.  */
1571                 else if (sio_ir & hooks->intr_tx_mt) {
1572                         /* If we are expecting a lowat notification
1573                          * and we get to this point it probably means that for
1574                          * some reason the tx_explicit didn't work as expected
1575                          * (that can legitimately happen if the output buffer is
1576                          * filled up in just the right way).
1577                          * So send the notification now.
1578                          */
1579                         if (port->ip_notify & N_OUTPUT_LOWAT) {
1580                                 ioc3_cb_output_lowat(port);
1581
1582                                 /* We need to reload the sio_ir since the lowat
1583                                  * call may have caused another write to occur,
1584                                  * clearing the tx_mt condition.
1585                                  */
1586                                 sio_ir = PENDING(card_ptr, idd);
1587                         }
1588
1589                         /* If the tx_mt condition still persists even after the
1590                          * lowat call, we've got some work to do.
1591                          */
1592                         if (sio_ir & hooks->intr_tx_mt) {
1593                                 /* If we are not currently expecting DMA input,
1594                                  * and the transmitter has just gone idle,
1595                                  * there is no longer any reason for DMA, so
1596                                  * disable it.
1597                                  */
1598                                 if (!(port->ip_notify
1599                                       & (N_DATA_READY | N_DDCD))) {
1600                                         BUG_ON(!(port->ip_sscr
1601                                                  & SSCR_DMA_EN));
1602                                         port->ip_sscr &= ~SSCR_DMA_EN;
1603                                         writel(port->ip_sscr,
1604                                                &port->ip_serial_regs->sscr);
1605                                 }
1606                                 /* Prevent infinite tx_mt interrupt */
1607                                 card_ptr->ic_enable &= ~hooks->intr_tx_mt;
1608                         }
1609                 }
1610                 sio_ir = PENDING(card_ptr, idd);
1611
1612                 /* if the read was aborted and only hooks->intr_rx_high,
1613                  * clear hooks->intr_rx_high, so we do not loop forever.
1614                  */
1615
1616                 if (rx_high_rd_aborted && (sio_ir == hooks->intr_rx_high)) {
1617                         sio_ir &= ~hooks->intr_rx_high;
1618                 }
1619         } while (sio_ir & hooks->intr_all);
1620
1621         spin_unlock_irqrestore(&port->ip_lock, flags);
1622         ioc3_enable(is, idd, card_ptr->ic_enable);
1623         return 0;
1624 }
1625
1626 /**
1627  * ioc3uart_intr - field all serial interrupts
1628  * @is : submodule
1629  * @idd: driver data
1630  * @pending: interrupts to handle
1631  *
1632  */
1633
1634 static int ioc3uart_intr(struct ioc3_submodule *is,
1635                         struct ioc3_driver_data *idd,
1636                         unsigned int pending)
1637 {
1638         int ret = 0;
1639
1640         /*
1641          * The upper level interrupt handler sends interrupts for both ports
1642          * here. So we need to call for each port with its interrupts.
1643          */
1644
1645         if (pending & SIO_IR_SA)
1646                 ret |= ioc3uart_intr_one(is, idd, pending & SIO_IR_SA);
1647         if (pending & SIO_IR_SB)
1648                 ret |= ioc3uart_intr_one(is, idd, pending & SIO_IR_SB);
1649
1650         return ret;
1651 }
1652
1653 /**
1654  * ic3_type
1655  * @port: Port to operate with (we ignore since we only have one port)
1656  *
1657  */
1658 static const char *ic3_type(struct uart_port *the_port)
1659 {
1660         if (IS_RS232(the_port->line))
1661                 return "SGI IOC3 Serial [rs232]";
1662         else
1663                 return "SGI IOC3 Serial [rs422]";
1664 }
1665
1666 /**
1667  * ic3_tx_empty - Is the transmitter empty?
1668  * @port: Port to operate on
1669  *
1670  */
1671 static unsigned int ic3_tx_empty(struct uart_port *the_port)
1672 {
1673         unsigned int ret = 0;
1674         struct ioc3_port *port = get_ioc3_port(the_port);
1675
1676         if (readl(&port->ip_serial_regs->shadow) & SHADOW_TEMT)
1677                 ret = TIOCSER_TEMT;
1678         return ret;
1679 }
1680
1681 /**
1682  * ic3_stop_tx - stop the transmitter
1683  * @port: Port to operate on
1684  *
1685  */
1686 static void ic3_stop_tx(struct uart_port *the_port)
1687 {
1688         struct ioc3_port *port = get_ioc3_port(the_port);
1689
1690         if (port)
1691                 set_notification(port, N_OUTPUT_LOWAT, 0);
1692 }
1693
1694 /**
1695  * ic3_stop_rx - stop the receiver
1696  * @port: Port to operate on
1697  *
1698  */
1699 static void ic3_stop_rx(struct uart_port *the_port)
1700 {
1701         struct ioc3_port *port = get_ioc3_port(the_port);
1702
1703         if (port)
1704                 port->ip_flags &= ~INPUT_ENABLE;
1705 }
1706
1707 /**
1708  * null_void_function
1709  * @port: Port to operate on
1710  *
1711  */
1712 static void null_void_function(struct uart_port *the_port)
1713 {
1714 }
1715
1716 /**
1717  * ic3_shutdown - shut down the port - free irq and disable
1718  * @port: port to shut down
1719  *
1720  */
1721 static void ic3_shutdown(struct uart_port *the_port)
1722 {
1723         unsigned long port_flags;
1724         struct ioc3_port *port;
1725         struct uart_state *state;
1726
1727         port = get_ioc3_port(the_port);
1728         if (!port)
1729                 return;
1730
1731         state = the_port->state;
1732         wake_up_interruptible(&state->port.delta_msr_wait);
1733
1734         spin_lock_irqsave(&the_port->lock, port_flags);
1735         set_notification(port, N_ALL, 0);
1736         spin_unlock_irqrestore(&the_port->lock, port_flags);
1737 }
1738
1739 /**
1740  * ic3_set_mctrl - set control lines (dtr, rts, etc)
1741  * @port: Port to operate on
1742  * @mctrl: Lines to set/unset
1743  *
1744  */
1745 static void ic3_set_mctrl(struct uart_port *the_port, unsigned int mctrl)
1746 {
1747         unsigned char mcr = 0;
1748
1749         if (mctrl & TIOCM_RTS)
1750                 mcr |= UART_MCR_RTS;
1751         if (mctrl & TIOCM_DTR)
1752                 mcr |= UART_MCR_DTR;
1753         if (mctrl & TIOCM_OUT1)
1754                 mcr |= UART_MCR_OUT1;
1755         if (mctrl & TIOCM_OUT2)
1756                 mcr |= UART_MCR_OUT2;
1757         if (mctrl & TIOCM_LOOP)
1758                 mcr |= UART_MCR_LOOP;
1759
1760         set_mcr(the_port, mcr, SHADOW_DTR);
1761 }
1762
1763 /**
1764  * ic3_get_mctrl - get control line info
1765  * @port: port to operate on
1766  *
1767  */
1768 static unsigned int ic3_get_mctrl(struct uart_port *the_port)
1769 {
1770         struct ioc3_port *port = get_ioc3_port(the_port);
1771         uint32_t shadow;
1772         unsigned int ret = 0;
1773
1774         if (!port)
1775                 return 0;
1776
1777         shadow = readl(&port->ip_serial_regs->shadow);
1778         if (shadow & SHADOW_DCD)
1779                 ret |= TIOCM_CD;
1780         if (shadow & SHADOW_DR)
1781                 ret |= TIOCM_DSR;
1782         if (shadow & SHADOW_CTS)
1783                 ret |= TIOCM_CTS;
1784         return ret;
1785 }
1786
1787 /**
1788  * ic3_start_tx - Start transmitter. Called with the_port->lock
1789  * @port: Port to operate on
1790  *
1791  */
1792 static void ic3_start_tx(struct uart_port *the_port)
1793 {
1794         struct ioc3_port *port = get_ioc3_port(the_port);
1795
1796         if (port) {
1797                 set_notification(port, N_OUTPUT_LOWAT, 1);
1798                 enable_intrs(port, port->ip_hooks->intr_tx_mt);
1799         }
1800 }
1801
1802 /**
1803  * ic3_break_ctl - handle breaks
1804  * @port: Port to operate on
1805  * @break_state: Break state
1806  *
1807  */
1808 static void ic3_break_ctl(struct uart_port *the_port, int break_state)
1809 {
1810 }
1811
1812 /**
1813  * ic3_startup - Start up the serial port - always return 0 (We're always on)
1814  * @port: Port to operate on
1815  *
1816  */
1817 static int ic3_startup(struct uart_port *the_port)
1818 {
1819         int retval;
1820         struct ioc3_port *port;
1821         struct ioc3_card *card_ptr;
1822         unsigned long port_flags;
1823
1824         if (!the_port) {
1825                 NOT_PROGRESS();
1826                 return -ENODEV;
1827         }
1828         port = get_ioc3_port(the_port);
1829         if (!port) {
1830                 NOT_PROGRESS();
1831                 return -ENODEV;
1832         }
1833         card_ptr = port->ip_card;
1834         port->ip_port = the_port;
1835
1836         if (!card_ptr) {
1837                 NOT_PROGRESS();
1838                 return -ENODEV;
1839         }
1840
1841         /* Start up the serial port */
1842         spin_lock_irqsave(&the_port->lock, port_flags);
1843         retval = ic3_startup_local(the_port);
1844         spin_unlock_irqrestore(&the_port->lock, port_flags);
1845         return retval;
1846 }
1847
1848 /**
1849  * ic3_set_termios - set termios stuff
1850  * @port: port to operate on
1851  * @termios: New settings
1852  * @termios: Old
1853  *
1854  */
1855 static void
1856 ic3_set_termios(struct uart_port *the_port,
1857                 struct ktermios *termios, struct ktermios *old_termios)
1858 {
1859         unsigned long port_flags;
1860
1861         spin_lock_irqsave(&the_port->lock, port_flags);
1862         ioc3_change_speed(the_port, termios, old_termios);
1863         spin_unlock_irqrestore(&the_port->lock, port_flags);
1864 }
1865
1866 /**
1867  * ic3_request_port - allocate resources for port - no op....
1868  * @port: port to operate on
1869  *
1870  */
1871 static int ic3_request_port(struct uart_port *port)
1872 {
1873         return 0;
1874 }
1875
1876 /* Associate the uart functions above - given to serial core */
1877 static const struct uart_ops ioc3_ops = {
1878         .tx_empty = ic3_tx_empty,
1879         .set_mctrl = ic3_set_mctrl,
1880         .get_mctrl = ic3_get_mctrl,
1881         .stop_tx = ic3_stop_tx,
1882         .start_tx = ic3_start_tx,
1883         .stop_rx = ic3_stop_rx,
1884         .break_ctl = ic3_break_ctl,
1885         .startup = ic3_startup,
1886         .shutdown = ic3_shutdown,
1887         .set_termios = ic3_set_termios,
1888         .type = ic3_type,
1889         .release_port = null_void_function,
1890         .request_port = ic3_request_port,
1891 };
1892
1893 /*
1894  * Boot-time initialization code
1895  */
1896
1897 static struct uart_driver ioc3_uart = {
1898         .owner = THIS_MODULE,
1899         .driver_name = "ioc3_serial",
1900         .dev_name = DEVICE_NAME,
1901         .major = DEVICE_MAJOR,
1902         .minor = DEVICE_MINOR,
1903         .nr = MAX_LOGICAL_PORTS
1904 };
1905
1906 /**
1907  * ioc3_serial_core_attach - register with serial core
1908  *              This is done during pci probing
1909  * @is: submodule struct for this
1910  * @idd: handle for this card
1911  */
1912 static inline int ioc3_serial_core_attach( struct ioc3_submodule *is,
1913                                 struct ioc3_driver_data *idd)
1914 {
1915         struct ioc3_port *port;
1916         struct uart_port *the_port;
1917         struct ioc3_card *card_ptr = idd->data[is->id];
1918         int ii, phys_port;
1919         struct pci_dev *pdev = idd->pdev;
1920
1921         DPRINT_CONFIG(("%s: attach pdev 0x%p - card_ptr 0x%p\n",
1922                        __func__, pdev, (void *)card_ptr));
1923
1924         if (!card_ptr)
1925                 return -ENODEV;
1926
1927         /* once around for each logical port on this card */
1928         for (ii = 0; ii < LOGICAL_PORTS_PER_CARD; ii++) {
1929                 phys_port = GET_PHYSICAL_PORT(ii);
1930                 the_port = &card_ptr->ic_port[phys_port].
1931                                 icp_uart_port[GET_LOGICAL_PORT(ii)];
1932                 port = card_ptr->ic_port[phys_port].icp_port;
1933                 port->ip_port = the_port;
1934
1935                 DPRINT_CONFIG(("%s: attach the_port 0x%p / port 0x%p [%d/%d]\n",
1936                         __func__, (void *)the_port, (void *)port,
1937                                 phys_port, ii));
1938
1939                 /* membase, iobase and mapbase just need to be non-0 */
1940                 the_port->membase = (unsigned char __iomem *)1;
1941                 the_port->iobase = (pdev->bus->number << 16) |  ii;
1942                 the_port->line = (Num_of_ioc3_cards << 2) | ii;
1943                 the_port->mapbase = 1;
1944                 the_port->type = PORT_16550A;
1945                 the_port->fifosize = FIFO_SIZE;
1946                 the_port->ops = &ioc3_ops;
1947                 the_port->irq = idd->irq_io;
1948                 the_port->dev = &pdev->dev;
1949
1950                 if (uart_add_one_port(&ioc3_uart, the_port) < 0) {
1951                         printk(KERN_WARNING
1952                           "%s: unable to add port %d bus %d\n",
1953                                __func__, the_port->line, pdev->bus->number);
1954                 } else {
1955                         DPRINT_CONFIG(("IOC3 serial port %d irq %d bus %d\n",
1956                           the_port->line, the_port->irq, pdev->bus->number));
1957                 }
1958
1959                 /* all ports are rs232 for now */
1960                 if (IS_PHYSICAL_PORT(ii))
1961                         ioc3_set_proto(port, PROTO_RS232);
1962         }
1963         return 0;
1964 }
1965
1966 /**
1967  * ioc3uart_remove - register detach function
1968  * @is: submodule struct for this submodule
1969  * @idd: ioc3 driver data for this submodule
1970  */
1971
1972 static int ioc3uart_remove(struct ioc3_submodule *is,
1973                         struct ioc3_driver_data *idd)
1974 {
1975         struct ioc3_card *card_ptr = idd->data[is->id];
1976         struct uart_port *the_port;
1977         struct ioc3_port *port;
1978         int ii;
1979
1980         if (card_ptr) {
1981                 for (ii = 0; ii < LOGICAL_PORTS_PER_CARD; ii++) {
1982                         the_port = &card_ptr->ic_port[GET_PHYSICAL_PORT(ii)].
1983                                         icp_uart_port[GET_LOGICAL_PORT(ii)];
1984                         if (the_port)
1985                                 uart_remove_one_port(&ioc3_uart, the_port);
1986                         port = card_ptr->ic_port[GET_PHYSICAL_PORT(ii)].icp_port;
1987                         if (port && IS_PHYSICAL_PORT(ii)
1988                                         && (GET_PHYSICAL_PORT(ii) == 0)) {
1989                                 pci_free_consistent(port->ip_idd->pdev,
1990                                         TOTAL_RING_BUF_SIZE,
1991                                         (void *)port->ip_cpu_ringbuf,
1992                                         port->ip_dma_ringbuf);
1993                                 kfree(port);
1994                                 card_ptr->ic_port[GET_PHYSICAL_PORT(ii)].
1995                                                         icp_port = NULL;
1996                         }
1997                 }
1998                 kfree(card_ptr);
1999                 idd->data[is->id] = NULL;
2000         }
2001         return 0;
2002 }
2003
2004 /**
2005  * ioc3uart_probe - card probe function called from shim driver
2006  * @is: submodule struct for this submodule
2007  * @idd: ioc3 driver data for this card
2008  */
2009
2010 static int
2011 ioc3uart_probe(struct ioc3_submodule *is, struct ioc3_driver_data *idd)
2012 {
2013         struct pci_dev *pdev = idd->pdev;
2014         struct ioc3_card *card_ptr;
2015         int ret = 0;
2016         struct ioc3_port *port;
2017         struct ioc3_port *ports[PORTS_PER_CARD];
2018         int phys_port;
2019         int cnt;
2020
2021         DPRINT_CONFIG(("%s (0x%p, 0x%p)\n", __func__, is, idd));
2022
2023         card_ptr = kzalloc(sizeof(struct ioc3_card), GFP_KERNEL);
2024         if (!card_ptr) {
2025                 printk(KERN_WARNING "ioc3_attach_one"
2026                        ": unable to get memory for the IOC3\n");
2027                 return -ENOMEM;
2028         }
2029         idd->data[is->id] = card_ptr;
2030         Submodule_slot = is->id;
2031
2032         writel(((UARTA_BASE >> 3) << SIO_CR_SER_A_BASE_SHIFT) |
2033                 ((UARTB_BASE >> 3) << SIO_CR_SER_B_BASE_SHIFT) |
2034                 (0xf << SIO_CR_CMD_PULSE_SHIFT), &idd->vma->sio_cr);
2035
2036         pci_write_config_dword(pdev, PCI_LAT, 0xff00);
2037
2038         /* Enable serial port mode select generic PIO pins as outputs */
2039         ioc3_gpcr_set(idd, GPCR_UARTA_MODESEL | GPCR_UARTB_MODESEL);
2040
2041         /* Create port structures for each port */
2042         for (phys_port = 0; phys_port < PORTS_PER_CARD; phys_port++) {
2043                 port = kzalloc(sizeof(struct ioc3_port), GFP_KERNEL);
2044                 if (!port) {
2045                         printk(KERN_WARNING
2046                                "IOC3 serial memory not available for port\n");
2047                         ret = -ENOMEM;
2048                         goto out4;
2049                 }
2050                 spin_lock_init(&port->ip_lock);
2051
2052                 /* we need to remember the previous ones, to point back to
2053                  * them farther down - setting up the ring buffers.
2054                  */
2055                 ports[phys_port] = port;
2056
2057                 /* init to something useful */
2058                 card_ptr->ic_port[phys_port].icp_port = port;
2059                 port->ip_is = is;
2060                 port->ip_idd = idd;
2061                 port->ip_baud = 9600;
2062                 port->ip_card = card_ptr;
2063                 port->ip_hooks = &hooks_array[phys_port];
2064
2065                 /* Setup each port */
2066                 if (phys_port == 0) {
2067                         port->ip_serial_regs = &idd->vma->port_a;
2068                         port->ip_uart_regs = &idd->vma->sregs.uarta;
2069
2070                         DPRINT_CONFIG(("%s : Port A ip_serial_regs 0x%p "
2071                                        "ip_uart_regs 0x%p\n",
2072                                        __func__,
2073                                        (void *)port->ip_serial_regs,
2074                                        (void *)port->ip_uart_regs));
2075
2076                         /* setup ring buffers */
2077                         port->ip_cpu_ringbuf = pci_alloc_consistent(pdev,
2078                                 TOTAL_RING_BUF_SIZE, &port->ip_dma_ringbuf);
2079
2080                         BUG_ON(!((((int64_t) port->ip_dma_ringbuf) &
2081                                   (TOTAL_RING_BUF_SIZE - 1)) == 0));
2082                         port->ip_inring = RING(port, RX_A);
2083                         port->ip_outring = RING(port, TX_A);
2084                         DPRINT_CONFIG(("%s : Port A ip_cpu_ringbuf 0x%p "
2085                                        "ip_dma_ringbuf 0x%p, ip_inring 0x%p "
2086                                         "ip_outring 0x%p\n",
2087                                        __func__,
2088                                        (void *)port->ip_cpu_ringbuf,
2089                                        (void *)port->ip_dma_ringbuf,
2090                                        (void *)port->ip_inring,
2091                                        (void *)port->ip_outring));
2092                 }
2093                 else {
2094                         port->ip_serial_regs = &idd->vma->port_b;
2095                         port->ip_uart_regs = &idd->vma->sregs.uartb;
2096
2097                         DPRINT_CONFIG(("%s : Port B ip_serial_regs 0x%p "
2098                                        "ip_uart_regs 0x%p\n",
2099                                        __func__,
2100                                        (void *)port->ip_serial_regs,
2101                                        (void *)port->ip_uart_regs));
2102
2103                         /* share the ring buffers */
2104                         port->ip_dma_ringbuf =
2105                             ports[phys_port - 1]->ip_dma_ringbuf;
2106                         port->ip_cpu_ringbuf =
2107                             ports[phys_port - 1]->ip_cpu_ringbuf;
2108                         port->ip_inring = RING(port, RX_B);
2109                         port->ip_outring = RING(port, TX_B);
2110                         DPRINT_CONFIG(("%s : Port B ip_cpu_ringbuf 0x%p "
2111                                        "ip_dma_ringbuf 0x%p, ip_inring 0x%p "
2112                                         "ip_outring 0x%p\n",
2113                                        __func__,
2114                                        (void *)port->ip_cpu_ringbuf,
2115                                        (void *)port->ip_dma_ringbuf,
2116                                        (void *)port->ip_inring,
2117                                        (void *)port->ip_outring));
2118                 }
2119
2120                 DPRINT_CONFIG(("%s : port %d [addr 0x%p] card_ptr 0x%p",
2121                                __func__,
2122                                phys_port, (void *)port, (void *)card_ptr));
2123                 DPRINT_CONFIG((" ip_serial_regs 0x%p ip_uart_regs 0x%p\n",
2124                                (void *)port->ip_serial_regs,
2125                                (void *)port->ip_uart_regs));
2126
2127                 /* Initialize the hardware for IOC3 */
2128                 port_init(port);
2129
2130                 DPRINT_CONFIG(("%s: phys_port %d port 0x%p inring 0x%p "
2131                                "outring 0x%p\n",
2132                                __func__,
2133                                phys_port, (void *)port,
2134                                (void *)port->ip_inring,
2135                                (void *)port->ip_outring));
2136
2137         }
2138
2139         /* register port with the serial core */
2140
2141         ret = ioc3_serial_core_attach(is, idd);
2142         if (ret)
2143                 goto out4;
2144
2145         Num_of_ioc3_cards++;
2146
2147         return ret;
2148
2149         /* error exits that give back resources */
2150 out4:
2151         for (cnt = 0; cnt < phys_port; cnt++)
2152                 kfree(ports[cnt]);
2153
2154         kfree(card_ptr);
2155         return ret;
2156 }
2157
2158 static struct ioc3_submodule ioc3uart_ops = {
2159         .name = "IOC3uart",
2160         .probe = ioc3uart_probe,
2161         .remove = ioc3uart_remove,
2162         /* call .intr for both ports initially */
2163         .irq_mask = SIO_IR_SA | SIO_IR_SB,
2164         .intr = ioc3uart_intr,
2165         .owner = THIS_MODULE,
2166 };
2167
2168 /**
2169  * ioc3_detect - module init called,
2170  */
2171 static int __init ioc3uart_init(void)
2172 {
2173         int ret;
2174
2175         /* register with serial core */
2176         if ((ret = uart_register_driver(&ioc3_uart)) < 0) {
2177                 printk(KERN_WARNING
2178                        "%s: Couldn't register IOC3 uart serial driver\n",
2179                        __func__);
2180                 return ret;
2181         }
2182         ret = ioc3_register_submodule(&ioc3uart_ops);
2183         if (ret)
2184                 uart_unregister_driver(&ioc3_uart);
2185         return ret;
2186 }
2187
2188 static void __exit ioc3uart_exit(void)
2189 {
2190         ioc3_unregister_submodule(&ioc3uart_ops);
2191         uart_unregister_driver(&ioc3_uart);
2192 }
2193
2194 module_init(ioc3uart_init);
2195 module_exit(ioc3uart_exit);
2196
2197 MODULE_AUTHOR("Pat Gefre - Silicon Graphics Inc. (SGI) <pfg@sgi.com>");
2198 MODULE_DESCRIPTION("Serial PCI driver module for SGI IOC3 card");
2199 MODULE_LICENSE("GPL");