]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/dgnc/dgnc_neo.c
Merge branch 'next' into for-linus
[karo-tx-linux.git] / drivers / staging / dgnc / dgnc_neo.c
1 /*
2  * Copyright 2003 Digi International (www.digi.com)
3  *      Scott H Kilau <Scott_Kilau at digi dot com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  * PURPOSE.  See the GNU General Public License for more details.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/sched.h>        /* For jiffies, task states */
18 #include <linux/interrupt.h>    /* For tasklet and interrupt structs/defines */
19 #include <linux/delay.h>        /* For udelay */
20 #include <linux/io.h>           /* For read[bwl]/write[bwl] */
21 #include <linux/serial.h>       /* For struct async_serial */
22 #include <linux/serial_reg.h>   /* For the various UART offsets */
23
24 #include "dgnc_driver.h"        /* Driver main header file */
25 #include "dgnc_neo.h"           /* Our header file */
26 #include "dgnc_tty.h"
27
28 static inline void neo_parse_lsr(struct dgnc_board *brd, uint port);
29 static inline void neo_parse_isr(struct dgnc_board *brd, uint port);
30 static void neo_copy_data_from_uart_to_queue(struct channel_t *ch);
31 static inline void neo_clear_break(struct channel_t *ch, int force);
32 static inline void neo_set_cts_flow_control(struct channel_t *ch);
33 static inline void neo_set_rts_flow_control(struct channel_t *ch);
34 static inline void neo_set_ixon_flow_control(struct channel_t *ch);
35 static inline void neo_set_ixoff_flow_control(struct channel_t *ch);
36 static inline void neo_set_no_output_flow_control(struct channel_t *ch);
37 static inline void neo_set_no_input_flow_control(struct channel_t *ch);
38 static inline void neo_set_new_start_stop_chars(struct channel_t *ch);
39 static void neo_parse_modem(struct channel_t *ch, unsigned char signals);
40 static void neo_tasklet(unsigned long data);
41 static void neo_vpd(struct dgnc_board *brd);
42 static void neo_uart_init(struct channel_t *ch);
43 static void neo_uart_off(struct channel_t *ch);
44 static int neo_drain(struct tty_struct *tty, uint seconds);
45 static void neo_param(struct tty_struct *tty);
46 static void neo_assert_modem_signals(struct channel_t *ch);
47 static void neo_flush_uart_write(struct channel_t *ch);
48 static void neo_flush_uart_read(struct channel_t *ch);
49 static void neo_disable_receiver(struct channel_t *ch);
50 static void neo_enable_receiver(struct channel_t *ch);
51 static void neo_send_break(struct channel_t *ch, int msecs);
52 static void neo_send_start_character(struct channel_t *ch);
53 static void neo_send_stop_character(struct channel_t *ch);
54 static void neo_copy_data_from_queue_to_uart(struct channel_t *ch);
55 static uint neo_get_uart_bytes_left(struct channel_t *ch);
56 static void neo_send_immediate_char(struct channel_t *ch, unsigned char c);
57 static irqreturn_t neo_intr(int irq, void *voidbrd);
58
59 struct board_ops dgnc_neo_ops = {
60         .tasklet =                      neo_tasklet,
61         .intr =                         neo_intr,
62         .uart_init =                    neo_uart_init,
63         .uart_off =                     neo_uart_off,
64         .drain =                        neo_drain,
65         .param =                        neo_param,
66         .vpd =                          neo_vpd,
67         .assert_modem_signals =         neo_assert_modem_signals,
68         .flush_uart_write =             neo_flush_uart_write,
69         .flush_uart_read =              neo_flush_uart_read,
70         .disable_receiver =             neo_disable_receiver,
71         .enable_receiver =              neo_enable_receiver,
72         .send_break =                   neo_send_break,
73         .send_start_character =         neo_send_start_character,
74         .send_stop_character =          neo_send_stop_character,
75         .copy_data_from_queue_to_uart = neo_copy_data_from_queue_to_uart,
76         .get_uart_bytes_left =          neo_get_uart_bytes_left,
77         .send_immediate_char =          neo_send_immediate_char
78 };
79
80 /*
81  * This function allows calls to ensure that all outstanding
82  * PCI writes have been completed, by doing a PCI read against
83  * a non-destructive, read-only location on the Neo card.
84  *
85  * In this case, we are reading the DVID (Read-only Device Identification)
86  * value of the Neo card.
87  */
88 static inline void neo_pci_posting_flush(struct dgnc_board *bd)
89 {
90         readb(bd->re_map_membase + 0x8D);
91 }
92
93 static inline void neo_set_cts_flow_control(struct channel_t *ch)
94 {
95         unsigned char ier = readb(&ch->ch_neo_uart->ier);
96         unsigned char efr = readb(&ch->ch_neo_uart->efr);
97
98         /* Turn on auto CTS flow control */
99 #if 1
100         ier |= UART_17158_IER_CTSDSR;
101 #else
102         ier &= ~(UART_17158_IER_CTSDSR);
103 #endif
104
105         efr |= (UART_17158_EFR_ECB | UART_17158_EFR_CTSDSR);
106
107         /* Turn off auto Xon flow control */
108         efr &= ~UART_17158_EFR_IXON;
109
110         /* Why? Becuz Exar's spec says we have to zero it out before setting it */
111         writeb(0, &ch->ch_neo_uart->efr);
112
113         /* Turn on UART enhanced bits */
114         writeb(efr, &ch->ch_neo_uart->efr);
115
116         /* Turn on table D, with 8 char hi/low watermarks */
117         writeb(UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_4DELAY,
118                &ch->ch_neo_uart->fctr);
119
120         /* Feed the UART our trigger levels */
121         writeb(8, &ch->ch_neo_uart->tfifo);
122         ch->ch_t_tlevel = 8;
123
124         writeb(ier, &ch->ch_neo_uart->ier);
125
126         neo_pci_posting_flush(ch->ch_bd);
127 }
128
129 static inline void neo_set_rts_flow_control(struct channel_t *ch)
130 {
131         unsigned char ier = readb(&ch->ch_neo_uart->ier);
132         unsigned char efr = readb(&ch->ch_neo_uart->efr);
133
134         /* Turn on auto RTS flow control */
135 #if 1
136         ier |= UART_17158_IER_RTSDTR;
137 #else
138         ier &= ~(UART_17158_IER_RTSDTR);
139 #endif
140         efr |= (UART_17158_EFR_ECB | UART_17158_EFR_RTSDTR);
141
142         /* Turn off auto Xoff flow control */
143         ier &= ~UART_17158_IER_XOFF;
144         efr &= ~UART_17158_EFR_IXOFF;
145
146         /* Why? Becuz Exar's spec says we have to zero it out before setting it */
147         writeb(0, &ch->ch_neo_uart->efr);
148
149         /* Turn on UART enhanced bits */
150         writeb(efr, &ch->ch_neo_uart->efr);
151
152         writeb(UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_4DELAY,
153                &ch->ch_neo_uart->fctr);
154         ch->ch_r_watermark = 4;
155
156         writeb(32, &ch->ch_neo_uart->rfifo);
157         ch->ch_r_tlevel = 32;
158
159         writeb(ier, &ch->ch_neo_uart->ier);
160
161         /*
162          * From the Neo UART spec sheet:
163          * The auto RTS/DTR function must be started by asserting
164          * RTS/DTR# output pin (MCR bit-0 or 1 to logic 1 after
165          * it is enabled.
166          */
167         ch->ch_mostat |= UART_MCR_RTS;
168
169         neo_pci_posting_flush(ch->ch_bd);
170 }
171
172 static inline void neo_set_ixon_flow_control(struct channel_t *ch)
173 {
174         unsigned char ier = readb(&ch->ch_neo_uart->ier);
175         unsigned char efr = readb(&ch->ch_neo_uart->efr);
176
177         /* Turn off auto CTS flow control */
178         ier &= ~UART_17158_IER_CTSDSR;
179         efr &= ~UART_17158_EFR_CTSDSR;
180
181         /* Turn on auto Xon flow control */
182         efr |= (UART_17158_EFR_ECB | UART_17158_EFR_IXON);
183
184         /* Why? Becuz Exar's spec says we have to zero it out before setting it */
185         writeb(0, &ch->ch_neo_uart->efr);
186
187         /* Turn on UART enhanced bits */
188         writeb(efr, &ch->ch_neo_uart->efr);
189
190         writeb(UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY,
191                &ch->ch_neo_uart->fctr);
192         ch->ch_r_watermark = 4;
193
194         writeb(32, &ch->ch_neo_uart->rfifo);
195         ch->ch_r_tlevel = 32;
196
197         /* Tell UART what start/stop chars it should be looking for */
198         writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
199         writeb(0, &ch->ch_neo_uart->xonchar2);
200
201         writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
202         writeb(0, &ch->ch_neo_uart->xoffchar2);
203
204         writeb(ier, &ch->ch_neo_uart->ier);
205
206         neo_pci_posting_flush(ch->ch_bd);
207 }
208
209 static inline void neo_set_ixoff_flow_control(struct channel_t *ch)
210 {
211         unsigned char ier = readb(&ch->ch_neo_uart->ier);
212         unsigned char efr = readb(&ch->ch_neo_uart->efr);
213
214         /* Turn off auto RTS flow control */
215         ier &= ~UART_17158_IER_RTSDTR;
216         efr &= ~UART_17158_EFR_RTSDTR;
217
218         /* Turn on auto Xoff flow control */
219         ier |= UART_17158_IER_XOFF;
220         efr |= (UART_17158_EFR_ECB | UART_17158_EFR_IXOFF);
221
222         /* Why? Becuz Exar's spec says we have to zero it out before setting it */
223         writeb(0, &ch->ch_neo_uart->efr);
224
225         /* Turn on UART enhanced bits */
226         writeb(efr, &ch->ch_neo_uart->efr);
227
228         /* Turn on table D, with 8 char hi/low watermarks */
229         writeb(UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY,
230                &ch->ch_neo_uart->fctr);
231
232         writeb(8, &ch->ch_neo_uart->tfifo);
233         ch->ch_t_tlevel = 8;
234
235         /* Tell UART what start/stop chars it should be looking for */
236         writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
237         writeb(0, &ch->ch_neo_uart->xonchar2);
238
239         writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
240         writeb(0, &ch->ch_neo_uart->xoffchar2);
241
242         writeb(ier, &ch->ch_neo_uart->ier);
243
244         neo_pci_posting_flush(ch->ch_bd);
245 }
246
247 static inline void neo_set_no_input_flow_control(struct channel_t *ch)
248 {
249         unsigned char ier = readb(&ch->ch_neo_uart->ier);
250         unsigned char efr = readb(&ch->ch_neo_uart->efr);
251
252         /* Turn off auto RTS flow control */
253         ier &= ~UART_17158_IER_RTSDTR;
254         efr &= ~UART_17158_EFR_RTSDTR;
255
256         /* Turn off auto Xoff flow control */
257         ier &= ~UART_17158_IER_XOFF;
258         if (ch->ch_c_iflag & IXON)
259                 efr &= ~(UART_17158_EFR_IXOFF);
260         else
261                 efr &= ~(UART_17158_EFR_ECB | UART_17158_EFR_IXOFF);
262
263         /* Why? Becuz Exar's spec says we have to zero it out before setting it */
264         writeb(0, &ch->ch_neo_uart->efr);
265
266         /* Turn on UART enhanced bits */
267         writeb(efr, &ch->ch_neo_uart->efr);
268
269         /* Turn on table D, with 8 char hi/low watermarks */
270         writeb(UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY,
271                &ch->ch_neo_uart->fctr);
272
273         ch->ch_r_watermark = 0;
274
275         writeb(16, &ch->ch_neo_uart->tfifo);
276         ch->ch_t_tlevel = 16;
277
278         writeb(16, &ch->ch_neo_uart->rfifo);
279         ch->ch_r_tlevel = 16;
280
281         writeb(ier, &ch->ch_neo_uart->ier);
282
283         neo_pci_posting_flush(ch->ch_bd);
284 }
285
286 static inline void neo_set_no_output_flow_control(struct channel_t *ch)
287 {
288         unsigned char ier = readb(&ch->ch_neo_uart->ier);
289         unsigned char efr = readb(&ch->ch_neo_uart->efr);
290
291         /* Turn off auto CTS flow control */
292         ier &= ~UART_17158_IER_CTSDSR;
293         efr &= ~UART_17158_EFR_CTSDSR;
294
295         /* Turn off auto Xon flow control */
296         if (ch->ch_c_iflag & IXOFF)
297                 efr &= ~UART_17158_EFR_IXON;
298         else
299                 efr &= ~(UART_17158_EFR_ECB | UART_17158_EFR_IXON);
300
301         /* Why? Becuz Exar's spec says we have to zero it out before setting it */
302         writeb(0, &ch->ch_neo_uart->efr);
303
304         /* Turn on UART enhanced bits */
305         writeb(efr, &ch->ch_neo_uart->efr);
306
307         /* Turn on table D, with 8 char hi/low watermarks */
308         writeb(UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY,
309                &ch->ch_neo_uart->fctr);
310
311         ch->ch_r_watermark = 0;
312
313         writeb(16, &ch->ch_neo_uart->tfifo);
314         ch->ch_t_tlevel = 16;
315
316         writeb(16, &ch->ch_neo_uart->rfifo);
317         ch->ch_r_tlevel = 16;
318
319         writeb(ier, &ch->ch_neo_uart->ier);
320
321         neo_pci_posting_flush(ch->ch_bd);
322 }
323
324 /* change UARTs start/stop chars */
325 static inline void neo_set_new_start_stop_chars(struct channel_t *ch)
326 {
327         /* if hardware flow control is set, then skip this whole thing */
328         if (ch->ch_digi.digi_flags & (CTSPACE | RTSPACE) ||
329             ch->ch_c_cflag & CRTSCTS)
330                 return;
331
332         /* Tell UART what start/stop chars it should be looking for */
333         writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
334         writeb(0, &ch->ch_neo_uart->xonchar2);
335
336         writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
337         writeb(0, &ch->ch_neo_uart->xoffchar2);
338
339         neo_pci_posting_flush(ch->ch_bd);
340 }
341
342 /*
343  * No locks are assumed to be held when calling this function.
344  */
345 static inline void neo_clear_break(struct channel_t *ch, int force)
346 {
347         unsigned long flags;
348
349         spin_lock_irqsave(&ch->ch_lock, flags);
350
351         /* Bail if we aren't currently sending a break. */
352         if (!ch->ch_stop_sending_break) {
353                 spin_unlock_irqrestore(&ch->ch_lock, flags);
354                 return;
355         }
356
357         /* Turn break off, and unset some variables */
358         if (ch->ch_flags & CH_BREAK_SENDING) {
359                 if (force ||
360                     time_after_eq(jiffies, ch->ch_stop_sending_break)) {
361                         unsigned char temp = readb(&ch->ch_neo_uart->lcr);
362
363                         writeb((temp & ~UART_LCR_SBC), &ch->ch_neo_uart->lcr);
364                         neo_pci_posting_flush(ch->ch_bd);
365                         ch->ch_flags &= ~(CH_BREAK_SENDING);
366                         ch->ch_stop_sending_break = 0;
367                 }
368         }
369         spin_unlock_irqrestore(&ch->ch_lock, flags);
370 }
371
372 /*
373  * Parse the ISR register.
374  */
375 static inline void neo_parse_isr(struct dgnc_board *brd, uint port)
376 {
377         struct channel_t *ch;
378         unsigned char isr;
379         unsigned char cause;
380         unsigned long flags;
381
382         ch = brd->channels[port];
383         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
384                 return;
385
386         /* Here we try to figure out what caused the interrupt to happen */
387         while (1) {
388                 isr = readb(&ch->ch_neo_uart->isr_fcr);
389
390                 /* Bail if no pending interrupt */
391                 if (isr & UART_IIR_NO_INT)
392                         break;
393
394                 /*
395                  * Yank off the upper 2 bits,
396                  * which just show that the FIFO's are enabled.
397                  */
398                 isr &= ~(UART_17158_IIR_FIFO_ENABLED);
399
400                 if (isr & (UART_17158_IIR_RDI_TIMEOUT | UART_IIR_RDI)) {
401                         /* Read data from uart -> queue */
402                         brd->intr_rx++;
403                         ch->ch_intr_rx++;
404                         neo_copy_data_from_uart_to_queue(ch);
405
406                         /* Call our tty layer to enforce queue flow control if needed. */
407                         spin_lock_irqsave(&ch->ch_lock, flags);
408                         dgnc_check_queue_flow_control(ch);
409                         spin_unlock_irqrestore(&ch->ch_lock, flags);
410                 }
411
412                 if (isr & UART_IIR_THRI) {
413                         brd->intr_tx++;
414                         ch->ch_intr_tx++;
415                         /* Transfer data (if any) from Write Queue -> UART. */
416                         spin_lock_irqsave(&ch->ch_lock, flags);
417                         ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
418                         spin_unlock_irqrestore(&ch->ch_lock, flags);
419                         neo_copy_data_from_queue_to_uart(ch);
420                 }
421
422                 if (isr & UART_17158_IIR_XONXOFF) {
423                         cause = readb(&ch->ch_neo_uart->xoffchar1);
424
425                         /*
426                          * Since the UART detected either an XON or
427                          * XOFF match, we need to figure out which
428                          * one it was, so we can suspend or resume data flow.
429                          */
430                         if (cause == UART_17158_XON_DETECT) {
431                                 /* Is output stopped right now, if so, resume it */
432                                 if (brd->channels[port]->ch_flags & CH_STOP) {
433                                         spin_lock_irqsave(&ch->ch_lock,
434                                                           flags);
435                                         ch->ch_flags &= ~(CH_STOP);
436                                         spin_unlock_irqrestore(&ch->ch_lock,
437                                                                flags);
438                                 }
439                         } else if (cause == UART_17158_XOFF_DETECT) {
440                                 if (!(brd->channels[port]->ch_flags & CH_STOP)) {
441                                         spin_lock_irqsave(&ch->ch_lock,
442                                                           flags);
443                                         ch->ch_flags |= CH_STOP;
444                                         spin_unlock_irqrestore(&ch->ch_lock,
445                                                                flags);
446                                 }
447                         }
448                 }
449
450                 if (isr & UART_17158_IIR_HWFLOW_STATE_CHANGE) {
451                         /*
452                          * If we get here, this means the hardware is doing auto flow control.
453                          * Check to see whether RTS/DTR or CTS/DSR caused this interrupt.
454                          */
455                         brd->intr_modem++;
456                         ch->ch_intr_modem++;
457                         cause = readb(&ch->ch_neo_uart->mcr);
458                         /* Which pin is doing auto flow? RTS or DTR? */
459                         if ((cause & 0x4) == 0) {
460                                 if (cause & UART_MCR_RTS) {
461                                         spin_lock_irqsave(&ch->ch_lock,
462                                                           flags);
463                                         ch->ch_mostat |= UART_MCR_RTS;
464                                         spin_unlock_irqrestore(&ch->ch_lock,
465                                                                flags);
466                                 } else {
467                                         spin_lock_irqsave(&ch->ch_lock,
468                                                           flags);
469                                         ch->ch_mostat &= ~(UART_MCR_RTS);
470                                         spin_unlock_irqrestore(&ch->ch_lock,
471                                                                flags);
472                                 }
473                         } else {
474                                 if (cause & UART_MCR_DTR) {
475                                         spin_lock_irqsave(&ch->ch_lock,
476                                                           flags);
477                                         ch->ch_mostat |= UART_MCR_DTR;
478                                         spin_unlock_irqrestore(&ch->ch_lock,
479                                                                flags);
480                                 } else {
481                                         spin_lock_irqsave(&ch->ch_lock,
482                                                           flags);
483                                         ch->ch_mostat &= ~(UART_MCR_DTR);
484                                         spin_unlock_irqrestore(&ch->ch_lock,
485                                                                flags);
486                                 }
487                         }
488                 }
489
490                 /* Parse any modem signal changes */
491                 neo_parse_modem(ch, readb(&ch->ch_neo_uart->msr));
492         }
493 }
494
495 static inline void neo_parse_lsr(struct dgnc_board *brd, uint port)
496 {
497         struct channel_t *ch;
498         int linestatus;
499         unsigned long flags;
500
501         /*
502          * Check to make sure it didn't receive interrupt with a null board
503          * associated or a board pointer that wasn't ours.
504          */
505         if (!brd || brd->magic != DGNC_BOARD_MAGIC)
506                 return;
507
508         if (port >= brd->maxports)
509                 return;
510
511         ch = brd->channels[port];
512         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
513                 return;
514
515         linestatus = readb(&ch->ch_neo_uart->lsr);
516
517         ch->ch_cached_lsr |= linestatus;
518
519         if (ch->ch_cached_lsr & UART_LSR_DR) {
520                 brd->intr_rx++;
521                 ch->ch_intr_rx++;
522                 /* Read data from uart -> queue */
523                 neo_copy_data_from_uart_to_queue(ch);
524                 spin_lock_irqsave(&ch->ch_lock, flags);
525                 dgnc_check_queue_flow_control(ch);
526                 spin_unlock_irqrestore(&ch->ch_lock, flags);
527         }
528
529         /*
530          * The next 3 tests should *NOT* happen, as the above test
531          * should encapsulate all 3... At least, thats what Exar says.
532          */
533
534         if (linestatus & UART_LSR_PE)
535                 ch->ch_err_parity++;
536
537         if (linestatus & UART_LSR_FE)
538                 ch->ch_err_frame++;
539
540         if (linestatus & UART_LSR_BI)
541                 ch->ch_err_break++;
542
543         if (linestatus & UART_LSR_OE) {
544                 /*
545                  * Rx Oruns. Exar says that an orun will NOT corrupt
546                  * the FIFO. It will just replace the holding register
547                  * with this new data byte. So basically just ignore this.
548                  * Probably we should eventually have an orun stat in our driver...
549                  */
550                 ch->ch_err_overrun++;
551         }
552
553         if (linestatus & UART_LSR_THRE) {
554                 brd->intr_tx++;
555                 ch->ch_intr_tx++;
556                 spin_lock_irqsave(&ch->ch_lock, flags);
557                 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
558                 spin_unlock_irqrestore(&ch->ch_lock, flags);
559
560                 /* Transfer data (if any) from Write Queue -> UART. */
561                 neo_copy_data_from_queue_to_uart(ch);
562         } else if (linestatus & UART_17158_TX_AND_FIFO_CLR) {
563                 brd->intr_tx++;
564                 ch->ch_intr_tx++;
565                 spin_lock_irqsave(&ch->ch_lock, flags);
566                 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
567                 spin_unlock_irqrestore(&ch->ch_lock, flags);
568
569                 /* Transfer data (if any) from Write Queue -> UART. */
570                 neo_copy_data_from_queue_to_uart(ch);
571         }
572 }
573
574 /*
575  * neo_param()
576  * Send any/all changes to the line to the UART.
577  */
578 static void neo_param(struct tty_struct *tty)
579 {
580         unsigned char lcr = 0;
581         unsigned char uart_lcr = 0;
582         unsigned char ier = 0;
583         unsigned char uart_ier = 0;
584         uint baud = 9600;
585         int quot = 0;
586         struct dgnc_board *bd;
587         struct channel_t *ch;
588         struct un_t   *un;
589
590         if (!tty || tty->magic != TTY_MAGIC)
591                 return;
592
593         un = (struct un_t *)tty->driver_data;
594         if (!un || un->magic != DGNC_UNIT_MAGIC)
595                 return;
596
597         ch = un->un_ch;
598         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
599                 return;
600
601         bd = ch->ch_bd;
602         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
603                 return;
604
605         /*
606          * If baud rate is zero, flush queues, and set mval to drop DTR.
607          */
608         if ((ch->ch_c_cflag & (CBAUD)) == 0) {
609                 ch->ch_r_head = 0;
610                 ch->ch_r_tail = 0;
611                 ch->ch_e_head = 0;
612                 ch->ch_e_tail = 0;
613                 ch->ch_w_head = 0;
614                 ch->ch_w_tail = 0;
615
616                 neo_flush_uart_write(ch);
617                 neo_flush_uart_read(ch);
618
619                 /* The baudrate is B0 so all modem lines are to be dropped. */
620                 ch->ch_flags |= (CH_BAUD0);
621                 ch->ch_mostat &= ~(UART_MCR_RTS | UART_MCR_DTR);
622                 neo_assert_modem_signals(ch);
623                 ch->ch_old_baud = 0;
624                 return;
625
626         } else if (ch->ch_custom_speed) {
627                 baud = ch->ch_custom_speed;
628                 /* Handle transition from B0 */
629                 if (ch->ch_flags & CH_BAUD0) {
630                         ch->ch_flags &= ~(CH_BAUD0);
631
632                         /*
633                          * Bring back up RTS and DTR...
634                          * Also handle RTS or DTR toggle if set.
635                          */
636                         if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
637                                 ch->ch_mostat |= (UART_MCR_RTS);
638                         if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
639                                 ch->ch_mostat |= (UART_MCR_DTR);
640                 }
641         } else {
642                 int iindex = 0;
643                 int jindex = 0;
644
645                 ulong bauds[4][16] = {
646                         { /* slowbaud */
647                                 0,      50,     75,     110,
648                                 134,    150,    200,    300,
649                                 600,    1200,   1800,   2400,
650                                 4800,   9600,   19200,  38400 },
651                         { /* slowbaud & CBAUDEX */
652                                 0,      57600,  115200, 230400,
653                                 460800, 150,    200,    921600,
654                                 600,    1200,   1800,   2400,
655                                 4800,   9600,   19200,  38400 },
656                         { /* fastbaud */
657                                 0,      57600,   76800, 115200,
658                                 131657, 153600, 230400, 460800,
659                                 921600, 1200,   1800,   2400,
660                                 4800,   9600,   19200,  38400 },
661                         { /* fastbaud & CBAUDEX */
662                                 0,      57600,  115200, 230400,
663                                 460800, 150,    200,    921600,
664                                 600,    1200,   1800,   2400,
665                                 4800,   9600,   19200,  38400 }
666                 };
667
668                 /* Only use the TXPrint baud rate if the terminal unit is NOT open */
669                 if (!(ch->ch_tun.un_flags & UN_ISOPEN) &&
670                     (un->un_type == DGNC_PRINT))
671                         baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
672                 else
673                         baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
674
675                 if (ch->ch_c_cflag & CBAUDEX)
676                         iindex = 1;
677
678                 if (ch->ch_digi.digi_flags & DIGI_FAST)
679                         iindex += 2;
680
681                 jindex = baud;
682
683                 if ((iindex >= 0) && (iindex < 4) &&
684                     (jindex >= 0) && (jindex < 16))
685                         baud = bauds[iindex][jindex];
686                 else
687                         baud = 0;
688
689                 if (baud == 0)
690                         baud = 9600;
691
692                 /* Handle transition from B0 */
693                 if (ch->ch_flags & CH_BAUD0) {
694                         ch->ch_flags &= ~(CH_BAUD0);
695
696                         /*
697                          * Bring back up RTS and DTR...
698                          * Also handle RTS or DTR toggle if set.
699                          */
700                         if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
701                                 ch->ch_mostat |= (UART_MCR_RTS);
702                         if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
703                                 ch->ch_mostat |= (UART_MCR_DTR);
704                 }
705         }
706
707         if (ch->ch_c_cflag & PARENB)
708                 lcr |= UART_LCR_PARITY;
709
710         if (!(ch->ch_c_cflag & PARODD))
711                 lcr |= UART_LCR_EPAR;
712
713         /*
714          * Not all platforms support mark/space parity,
715          * so this will hide behind an ifdef.
716          */
717 #ifdef CMSPAR
718         if (ch->ch_c_cflag & CMSPAR)
719                 lcr |= UART_LCR_SPAR;
720 #endif
721
722         if (ch->ch_c_cflag & CSTOPB)
723                 lcr |= UART_LCR_STOP;
724
725         switch (ch->ch_c_cflag & CSIZE) {
726         case CS5:
727                 lcr |= UART_LCR_WLEN5;
728                 break;
729         case CS6:
730                 lcr |= UART_LCR_WLEN6;
731                 break;
732         case CS7:
733                 lcr |= UART_LCR_WLEN7;
734                 break;
735         case CS8:
736         default:
737                 lcr |= UART_LCR_WLEN8;
738                 break;
739         }
740
741         uart_ier = readb(&ch->ch_neo_uart->ier);
742         ier = uart_ier;
743
744         uart_lcr = readb(&ch->ch_neo_uart->lcr);
745
746         if (baud == 0)
747                 baud = 9600;
748
749         quot = ch->ch_bd->bd_dividend / baud;
750
751         if (quot != 0 && ch->ch_old_baud != baud) {
752                 ch->ch_old_baud = baud;
753                 writeb(UART_LCR_DLAB, &ch->ch_neo_uart->lcr);
754                 writeb((quot & 0xff), &ch->ch_neo_uart->txrx);
755                 writeb((quot >> 8), &ch->ch_neo_uart->ier);
756                 writeb(lcr, &ch->ch_neo_uart->lcr);
757         }
758
759         if (uart_lcr != lcr)
760                 writeb(lcr, &ch->ch_neo_uart->lcr);
761
762         if (ch->ch_c_cflag & CREAD)
763                 ier |= (UART_IER_RDI | UART_IER_RLSI);
764         else
765                 ier &= ~(UART_IER_RDI | UART_IER_RLSI);
766
767         /*
768          * Have the UART interrupt on modem signal changes ONLY when
769          * we are in hardware flow control mode, or CLOCAL/FORCEDCD is not set.
770          */
771         if ((ch->ch_digi.digi_flags & CTSPACE) ||
772             (ch->ch_digi.digi_flags & RTSPACE) ||
773             (ch->ch_c_cflag & CRTSCTS) ||
774             !(ch->ch_digi.digi_flags & DIGI_FORCEDCD) ||
775             !(ch->ch_c_cflag & CLOCAL))
776                 ier |= UART_IER_MSI;
777         else
778                 ier &= ~UART_IER_MSI;
779
780         ier |= UART_IER_THRI;
781
782         if (ier != uart_ier)
783                 writeb(ier, &ch->ch_neo_uart->ier);
784
785         /* Set new start/stop chars */
786         neo_set_new_start_stop_chars(ch);
787
788         if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
789                 neo_set_cts_flow_control(ch);
790         } else if (ch->ch_c_iflag & IXON) {
791                 /* If start/stop is set to disable, then we should disable flow control */
792                 if ((ch->ch_startc == _POSIX_VDISABLE) ||
793                     (ch->ch_stopc == _POSIX_VDISABLE))
794                         neo_set_no_output_flow_control(ch);
795                 else
796                         neo_set_ixon_flow_control(ch);
797         } else {
798                 neo_set_no_output_flow_control(ch);
799         }
800
801         if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
802                 neo_set_rts_flow_control(ch);
803         } else if (ch->ch_c_iflag & IXOFF) {
804                 /* If start/stop is set to disable, then we should disable flow control */
805                 if ((ch->ch_startc == _POSIX_VDISABLE) ||
806                     (ch->ch_stopc == _POSIX_VDISABLE))
807                         neo_set_no_input_flow_control(ch);
808                 else
809                         neo_set_ixoff_flow_control(ch);
810         } else {
811                 neo_set_no_input_flow_control(ch);
812         }
813
814         /*
815          * Adjust the RX FIFO Trigger level if baud is less than 9600.
816          * Not exactly elegant, but this is needed because of the Exar chip's
817          * delay on firing off the RX FIFO interrupt on slower baud rates.
818          */
819         if (baud < 9600) {
820                 writeb(1, &ch->ch_neo_uart->rfifo);
821                 ch->ch_r_tlevel = 1;
822         }
823
824         neo_assert_modem_signals(ch);
825
826         /* Get current status of the modem signals now */
827         neo_parse_modem(ch, readb(&ch->ch_neo_uart->msr));
828 }
829
830 /*
831  * Our board poller function.
832  */
833 static void neo_tasklet(unsigned long data)
834 {
835         struct dgnc_board *bd = (struct dgnc_board *)data;
836         struct channel_t *ch;
837         unsigned long flags;
838         int i;
839         int state = 0;
840         int ports = 0;
841
842         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
843                 return;
844
845         /* Cache a couple board values */
846         spin_lock_irqsave(&bd->bd_lock, flags);
847         state = bd->state;
848         ports = bd->nasync;
849         spin_unlock_irqrestore(&bd->bd_lock, flags);
850
851         /*
852          * Do NOT allow the interrupt routine to read the intr registers
853          * Until we release this lock.
854          */
855         spin_lock_irqsave(&bd->bd_intr_lock, flags);
856
857         /*
858          * If board is ready, parse deeper to see if there is anything to do.
859          */
860         if ((state == BOARD_READY) && (ports > 0)) {
861                 /* Loop on each port */
862                 for (i = 0; i < ports; i++) {
863                         ch = bd->channels[i];
864
865                         /* Just being careful... */
866                         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
867                                 continue;
868
869                         /*
870                          * NOTE: Remember you CANNOT hold any channel
871                          * locks when calling the input routine.
872                          *
873                          * During input processing, its possible we
874                          * will call the Linux ld, which might in turn,
875                          * do a callback right back into us, resulting
876                          * in us trying to grab the channel lock twice!
877                          */
878                         dgnc_input(ch);
879
880                         /*
881                          * Channel lock is grabbed and then released
882                          * inside both of these routines, but neither
883                          * call anything else that could call back into us.
884                          */
885                         neo_copy_data_from_queue_to_uart(ch);
886                         dgnc_wakeup_writes(ch);
887
888                         /*
889                          * Call carrier carrier function, in case something
890                          * has changed.
891                          */
892                         dgnc_carrier(ch);
893
894                         /*
895                          * Check to see if we need to turn off a sending break.
896                          * The timing check is done inside clear_break()
897                          */
898                         if (ch->ch_stop_sending_break)
899                                 neo_clear_break(ch, 0);
900                 }
901         }
902
903         /* Allow interrupt routine to access the interrupt register again */
904         spin_unlock_irqrestore(&bd->bd_intr_lock, flags);
905 }
906
907 /*
908  * dgnc_neo_intr()
909  *
910  * Neo specific interrupt handler.
911  */
912 static irqreturn_t neo_intr(int irq, void *voidbrd)
913 {
914         struct dgnc_board *brd = voidbrd;
915         struct channel_t *ch;
916         int port = 0;
917         int type;
918         u32 uart_poll;
919         unsigned long flags;
920         unsigned long flags2;
921
922         /*
923          * Check to make sure it didn't receive interrupt with a null board
924          * associated or a board pointer that wasn't ours.
925          */
926         if (!brd || brd->magic != DGNC_BOARD_MAGIC)
927                 return IRQ_NONE;
928
929         brd->intr_count++;
930
931         /* Lock out the slow poller from running on this board. */
932         spin_lock_irqsave(&brd->bd_intr_lock, flags);
933
934         /*
935          * Read in "extended" IRQ information from the 32bit Neo register.
936          * Bits 0-7: What port triggered the interrupt.
937          * Bits 8-31: Each 3bits indicate what type of interrupt occurred.
938          */
939         uart_poll = readl(brd->re_map_membase + UART_17158_POLL_ADDR_OFFSET);
940
941         /*
942          * If 0, no interrupts pending.
943          * This can happen if the IRQ is shared among a couple Neo/Classic boards.
944          */
945         if (!uart_poll) {
946                 spin_unlock_irqrestore(&brd->bd_intr_lock, flags);
947                 return IRQ_NONE;
948         }
949
950         /* At this point, we have at least SOMETHING to service, dig further... */
951
952         /* Loop on each port */
953         while ((uart_poll & 0xff) != 0) {
954                 type = uart_poll >> (8 + (port * 3));
955                 type &= 0x7;
956
957                 uart_poll &= ~(0x01 << port);
958
959                 /* Switch on type of interrupt we have */
960                 switch (type) {
961                 case UART_17158_RXRDY_TIMEOUT:
962                         /*
963                          * RXRDY Time-out is cleared by reading data in the
964                          * RX FIFO until it falls below the trigger level.
965                          */
966
967                         /* Verify the port is in range. */
968                         if (port >= brd->nasync)
969                                 break;
970
971                         ch = brd->channels[port];
972                         neo_copy_data_from_uart_to_queue(ch);
973
974                         /* Call our tty layer to enforce queue flow control if needed. */
975                         spin_lock_irqsave(&ch->ch_lock, flags2);
976                         dgnc_check_queue_flow_control(ch);
977                         spin_unlock_irqrestore(&ch->ch_lock, flags2);
978
979                         break;
980
981                 case UART_17158_RX_LINE_STATUS:
982                         /*
983                          * RXRDY and RX LINE Status (logic OR of LSR[4:1])
984                          */
985                         neo_parse_lsr(brd, port);
986                         break;
987
988                 case UART_17158_TXRDY:
989                         /*
990                          * TXRDY interrupt clears after reading ISR register for the UART channel.
991                          */
992
993                         /*
994                          * Yes, this is odd...
995                          * Why would I check EVERY possibility of type of
996                          * interrupt, when we know its TXRDY???
997                          * Becuz for some reason, even tho we got triggered for TXRDY,
998                          * it seems to be occasionally wrong. Instead of TX, which
999                          * it should be, I was getting things like RXDY too. Weird.
1000                          */
1001                         neo_parse_isr(brd, port);
1002                         break;
1003
1004                 case UART_17158_MSR:
1005                         /*
1006                          * MSR or flow control was seen.
1007                          */
1008                         neo_parse_isr(brd, port);
1009                         break;
1010
1011                 default:
1012                         /*
1013                          * The UART triggered us with a bogus interrupt type.
1014                          * It appears the Exar chip, when REALLY bogged down, will throw
1015                          * these once and awhile.
1016                          * Its harmless, just ignore it and move on.
1017                          */
1018                         break;
1019                 }
1020
1021                 port++;
1022         }
1023
1024         /*
1025          * Schedule tasklet to more in-depth servicing at a better time.
1026          */
1027         tasklet_schedule(&brd->helper_tasklet);
1028
1029         spin_unlock_irqrestore(&brd->bd_intr_lock, flags);
1030
1031         return IRQ_HANDLED;
1032 }
1033
1034 /*
1035  * Neo specific way of turning off the receiver.
1036  * Used as a way to enforce queue flow control when in
1037  * hardware flow control mode.
1038  */
1039 static void neo_disable_receiver(struct channel_t *ch)
1040 {
1041         unsigned char tmp = readb(&ch->ch_neo_uart->ier);
1042
1043         tmp &= ~(UART_IER_RDI);
1044         writeb(tmp, &ch->ch_neo_uart->ier);
1045         neo_pci_posting_flush(ch->ch_bd);
1046 }
1047
1048 /*
1049  * Neo specific way of turning on the receiver.
1050  * Used as a way to un-enforce queue flow control when in
1051  * hardware flow control mode.
1052  */
1053 static void neo_enable_receiver(struct channel_t *ch)
1054 {
1055         unsigned char tmp = readb(&ch->ch_neo_uart->ier);
1056
1057         tmp |= (UART_IER_RDI);
1058         writeb(tmp, &ch->ch_neo_uart->ier);
1059         neo_pci_posting_flush(ch->ch_bd);
1060 }
1061
1062 static void neo_copy_data_from_uart_to_queue(struct channel_t *ch)
1063 {
1064         int qleft = 0;
1065         unsigned char linestatus = 0;
1066         unsigned char error_mask = 0;
1067         int n = 0;
1068         int total = 0;
1069         ushort head;
1070         ushort tail;
1071         unsigned long flags;
1072
1073         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1074                 return;
1075
1076         spin_lock_irqsave(&ch->ch_lock, flags);
1077
1078         /* cache head and tail of queue */
1079         head = ch->ch_r_head & RQUEUEMASK;
1080         tail = ch->ch_r_tail & RQUEUEMASK;
1081
1082         /* Get our cached LSR */
1083         linestatus = ch->ch_cached_lsr;
1084         ch->ch_cached_lsr = 0;
1085
1086         /* Store how much space we have left in the queue */
1087         qleft = tail - head - 1;
1088         if (qleft < 0)
1089                 qleft += RQUEUEMASK + 1;
1090
1091         /*
1092          * If the UART is not in FIFO mode, force the FIFO copy to
1093          * NOT be run, by setting total to 0.
1094          *
1095          * On the other hand, if the UART IS in FIFO mode, then ask
1096          * the UART to give us an approximation of data it has RX'ed.
1097          */
1098         if (!(ch->ch_flags & CH_FIFO_ENABLED)) {
1099                 total = 0;
1100         } else {
1101                 total = readb(&ch->ch_neo_uart->rfifo);
1102
1103                 /*
1104                  * EXAR chip bug - RX FIFO COUNT - Fudge factor.
1105                  *
1106                  * This resolves a problem/bug with the Exar chip that sometimes
1107                  * returns a bogus value in the rfifo register.
1108                  * The count can be any where from 0-3 bytes "off".
1109                  * Bizarre, but true.
1110                  */
1111                 if ((ch->ch_bd->dvid & 0xf0) >= UART_XR17E158_DVID)
1112                         total -= 1;
1113                 else
1114                         total -= 3;
1115         }
1116
1117         /*
1118          * Finally, bound the copy to make sure we don't overflow
1119          * our own queue...
1120          * The byte by byte copy loop below this loop this will
1121          * deal with the queue overflow possibility.
1122          */
1123         total = min(total, qleft);
1124
1125         while (total > 0) {
1126                 /*
1127                  * Grab the linestatus register, we need to check
1128                  * to see if there are any errors in the FIFO.
1129                  */
1130                 linestatus = readb(&ch->ch_neo_uart->lsr);
1131
1132                 /*
1133                  * Break out if there is a FIFO error somewhere.
1134                  * This will allow us to go byte by byte down below,
1135                  * finding the exact location of the error.
1136                  */
1137                 if (linestatus & UART_17158_RX_FIFO_DATA_ERROR)
1138                         break;
1139
1140                 /* Make sure we don't go over the end of our queue */
1141                 n = min(((uint)total), (RQUEUESIZE - (uint)head));
1142
1143                 /*
1144                  * Cut down n even further if needed, this is to fix
1145                  * a problem with memcpy_fromio() with the Neo on the
1146                  * IBM pSeries platform.
1147                  * 15 bytes max appears to be the magic number.
1148                  */
1149                 n = min_t(uint, n, 12);
1150
1151                 /*
1152                  * Since we are grabbing the linestatus register, which
1153                  * will reset some bits after our read, we need to ensure
1154                  * we don't miss our TX FIFO emptys.
1155                  */
1156                 if (linestatus & (UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR))
1157                         ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1158
1159                 linestatus = 0;
1160
1161                 /* Copy data from uart to the queue */
1162                 memcpy_fromio(ch->ch_rqueue + head,
1163                               &ch->ch_neo_uart->txrxburst, n);
1164
1165                 /*
1166                  * Since RX_FIFO_DATA_ERROR was 0, we are guaranteed
1167                  * that all the data currently in the FIFO is free of
1168                  * breaks and parity/frame/orun errors.
1169                  */
1170                 memset(ch->ch_equeue + head, 0, n);
1171
1172                 /* Add to and flip head if needed */
1173                 head = (head + n) & RQUEUEMASK;
1174                 total -= n;
1175                 qleft -= n;
1176                 ch->ch_rxcount += n;
1177         }
1178
1179         /*
1180          * Create a mask to determine whether we should
1181          * insert the character (if any) into our queue.
1182          */
1183         if (ch->ch_c_iflag & IGNBRK)
1184                 error_mask |= UART_LSR_BI;
1185
1186         /*
1187          * Now cleanup any leftover bytes still in the UART.
1188          * Also deal with any possible queue overflow here as well.
1189          */
1190         while (1) {
1191                 /*
1192                  * Its possible we have a linestatus from the loop above
1193                  * this, so we "OR" on any extra bits.
1194                  */
1195                 linestatus |= readb(&ch->ch_neo_uart->lsr);
1196
1197                 /*
1198                  * If the chip tells us there is no more data pending to
1199                  * be read, we can then leave.
1200                  * But before we do, cache the linestatus, just in case.
1201                  */
1202                 if (!(linestatus & UART_LSR_DR)) {
1203                         ch->ch_cached_lsr = linestatus;
1204                         break;
1205                 }
1206
1207                 /* No need to store this bit */
1208                 linestatus &= ~UART_LSR_DR;
1209
1210                 /*
1211                  * Since we are grabbing the linestatus register, which
1212                  * will reset some bits after our read, we need to ensure
1213                  * we don't miss our TX FIFO emptys.
1214                  */
1215                 if (linestatus & (UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR)) {
1216                         linestatus &= ~(UART_LSR_THRE |
1217                                         UART_17158_TX_AND_FIFO_CLR);
1218                         ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1219                 }
1220
1221                 /*
1222                  * Discard character if we are ignoring the error mask.
1223                  */
1224                 if (linestatus & error_mask)  {
1225                         unsigned char discard;
1226
1227                         linestatus = 0;
1228                         memcpy_fromio(&discard, &ch->ch_neo_uart->txrxburst, 1);
1229                         continue;
1230                 }
1231
1232                 /*
1233                  * If our queue is full, we have no choice but to drop some data.
1234                  * The assumption is that HWFLOW or SWFLOW should have stopped
1235                  * things way way before we got to this point.
1236                  *
1237                  * I decided that I wanted to ditch the oldest data first,
1238                  * I hope thats okay with everyone? Yes? Good.
1239                  */
1240                 while (qleft < 1) {
1241                         tail = (tail + 1) & RQUEUEMASK;
1242                         ch->ch_r_tail = tail;
1243                         ch->ch_err_overrun++;
1244                         qleft++;
1245                 }
1246
1247                 memcpy_fromio(ch->ch_rqueue + head,
1248                               &ch->ch_neo_uart->txrxburst, 1);
1249                 ch->ch_equeue[head] = (unsigned char)linestatus;
1250
1251                 /* Ditch any remaining linestatus value. */
1252                 linestatus = 0;
1253
1254                 /* Add to and flip head if needed */
1255                 head = (head + 1) & RQUEUEMASK;
1256
1257                 qleft--;
1258                 ch->ch_rxcount++;
1259         }
1260
1261         /*
1262          * Write new final heads to channel structure.
1263          */
1264         ch->ch_r_head = head & RQUEUEMASK;
1265         ch->ch_e_head = head & EQUEUEMASK;
1266
1267         spin_unlock_irqrestore(&ch->ch_lock, flags);
1268 }
1269
1270 /*
1271  * This function basically goes to sleep for secs, or until
1272  * it gets signalled that the port has fully drained.
1273  */
1274 static int neo_drain(struct tty_struct *tty, uint seconds)
1275 {
1276         unsigned long flags;
1277         struct channel_t *ch;
1278         struct un_t *un;
1279         int rc = 0;
1280
1281         if (!tty || tty->magic != TTY_MAGIC)
1282                 return -ENXIO;
1283
1284         un = (struct un_t *)tty->driver_data;
1285         if (!un || un->magic != DGNC_UNIT_MAGIC)
1286                 return -ENXIO;
1287
1288         ch = un->un_ch;
1289         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1290                 return -ENXIO;
1291
1292         spin_lock_irqsave(&ch->ch_lock, flags);
1293         un->un_flags |= UN_EMPTY;
1294         spin_unlock_irqrestore(&ch->ch_lock, flags);
1295
1296         /*
1297          * Go to sleep waiting for the tty layer to wake me back up when
1298          * the empty flag goes away.
1299          */
1300         rc = wait_event_interruptible_timeout(un->un_flags_wait,
1301                                               ((un->un_flags & UN_EMPTY) == 0),
1302                                               msecs_to_jiffies(seconds * 1000));
1303
1304         /* If ret is non-zero, user ctrl-c'ed us */
1305         return rc;
1306 }
1307
1308 /*
1309  * Flush the WRITE FIFO on the Neo.
1310  *
1311  * NOTE: Channel lock MUST be held before calling this function!
1312  */
1313 static void neo_flush_uart_write(struct channel_t *ch)
1314 {
1315         unsigned char tmp = 0;
1316         int i = 0;
1317
1318         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1319                 return;
1320
1321         writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_XMIT),
1322                &ch->ch_neo_uart->isr_fcr);
1323         neo_pci_posting_flush(ch->ch_bd);
1324
1325         for (i = 0; i < 10; i++) {
1326                 /* Check to see if the UART feels it completely flushed the FIFO. */
1327                 tmp = readb(&ch->ch_neo_uart->isr_fcr);
1328                 if (tmp & 4)
1329                         udelay(10);
1330                 else
1331                         break;
1332         }
1333
1334         ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1335 }
1336
1337 /*
1338  * Flush the READ FIFO on the Neo.
1339  *
1340  * NOTE: Channel lock MUST be held before calling this function!
1341  */
1342 static void neo_flush_uart_read(struct channel_t *ch)
1343 {
1344         unsigned char tmp = 0;
1345         int i = 0;
1346
1347         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1348                 return;
1349
1350         writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR,
1351                &ch->ch_neo_uart->isr_fcr);
1352         neo_pci_posting_flush(ch->ch_bd);
1353
1354         for (i = 0; i < 10; i++) {
1355                 /* Check to see if the UART feels it completely flushed the FIFO. */
1356                 tmp = readb(&ch->ch_neo_uart->isr_fcr);
1357                 if (tmp & 2)
1358                         udelay(10);
1359                 else
1360                         break;
1361         }
1362 }
1363
1364 static void neo_copy_data_from_queue_to_uart(struct channel_t *ch)
1365 {
1366         ushort head;
1367         ushort tail;
1368         int n;
1369         int s;
1370         int qlen;
1371         uint len_written = 0;
1372         unsigned long flags;
1373
1374         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1375                 return;
1376
1377         spin_lock_irqsave(&ch->ch_lock, flags);
1378
1379         /* No data to write to the UART */
1380         if (ch->ch_w_tail == ch->ch_w_head)
1381                 goto exit_unlock;
1382
1383         /* If port is "stopped", don't send any data to the UART */
1384         if ((ch->ch_flags & CH_FORCED_STOP) ||
1385             (ch->ch_flags & CH_BREAK_SENDING))
1386                 goto exit_unlock;
1387
1388         /*
1389          * If FIFOs are disabled. Send data directly to txrx register
1390          */
1391         if (!(ch->ch_flags & CH_FIFO_ENABLED)) {
1392                 unsigned char lsrbits = readb(&ch->ch_neo_uart->lsr);
1393
1394                 /* Cache the LSR bits for later parsing */
1395                 ch->ch_cached_lsr |= lsrbits;
1396                 if (ch->ch_cached_lsr & UART_LSR_THRE) {
1397                         ch->ch_cached_lsr &= ~(UART_LSR_THRE);
1398
1399                         /*
1400                          * If RTS Toggle mode is on, turn on RTS now if not already set,
1401                          * and make sure we get an event when the data transfer has completed.
1402                          */
1403                         if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1404                                 if (!(ch->ch_mostat & UART_MCR_RTS)) {
1405                                         ch->ch_mostat |= (UART_MCR_RTS);
1406                                         neo_assert_modem_signals(ch);
1407                                 }
1408                                 ch->ch_tun.un_flags |= (UN_EMPTY);
1409                         }
1410                         /*
1411                          * If DTR Toggle mode is on, turn on DTR now if not already set,
1412                          * and make sure we get an event when the data transfer has completed.
1413                          */
1414                         if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1415                                 if (!(ch->ch_mostat & UART_MCR_DTR)) {
1416                                         ch->ch_mostat |= (UART_MCR_DTR);
1417                                         neo_assert_modem_signals(ch);
1418                                 }
1419                                 ch->ch_tun.un_flags |= (UN_EMPTY);
1420                         }
1421
1422                         writeb(ch->ch_wqueue[ch->ch_w_tail],
1423                                &ch->ch_neo_uart->txrx);
1424                         ch->ch_w_tail++;
1425                         ch->ch_w_tail &= WQUEUEMASK;
1426                         ch->ch_txcount++;
1427                 }
1428
1429                 goto exit_unlock;
1430         }
1431
1432         /*
1433          * We have to do it this way, because of the EXAR TXFIFO count bug.
1434          */
1435         if ((ch->ch_bd->dvid & 0xf0) < UART_XR17E158_DVID) {
1436                 if (!(ch->ch_flags & (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM)))
1437                         goto exit_unlock;
1438
1439                 len_written = 0;
1440
1441                 n = readb(&ch->ch_neo_uart->tfifo);
1442
1443                 if ((unsigned int)n > ch->ch_t_tlevel)
1444                         goto exit_unlock;
1445
1446                 n = UART_17158_TX_FIFOSIZE - ch->ch_t_tlevel;
1447         } else {
1448                 n = UART_17158_TX_FIFOSIZE - readb(&ch->ch_neo_uart->tfifo);
1449         }
1450
1451         /* cache head and tail of queue */
1452         head = ch->ch_w_head & WQUEUEMASK;
1453         tail = ch->ch_w_tail & WQUEUEMASK;
1454         qlen = (head - tail) & WQUEUEMASK;
1455
1456         /* Find minimum of the FIFO space, versus queue length */
1457         n = min(n, qlen);
1458
1459         while (n > 0) {
1460                 s = ((head >= tail) ? head : WQUEUESIZE) - tail;
1461                 s = min(s, n);
1462
1463                 if (s <= 0)
1464                         break;
1465
1466                 /*
1467                  * If RTS Toggle mode is on, turn on RTS now if not already set,
1468                  * and make sure we get an event when the data transfer has completed.
1469                  */
1470                 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1471                         if (!(ch->ch_mostat & UART_MCR_RTS)) {
1472                                 ch->ch_mostat |= (UART_MCR_RTS);
1473                                 neo_assert_modem_signals(ch);
1474                         }
1475                         ch->ch_tun.un_flags |= (UN_EMPTY);
1476                 }
1477
1478                 /*
1479                  * If DTR Toggle mode is on, turn on DTR now if not already set,
1480                  * and make sure we get an event when the data transfer has completed.
1481                  */
1482                 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1483                         if (!(ch->ch_mostat & UART_MCR_DTR)) {
1484                                 ch->ch_mostat |= (UART_MCR_DTR);
1485                                 neo_assert_modem_signals(ch);
1486                         }
1487                         ch->ch_tun.un_flags |= (UN_EMPTY);
1488                 }
1489
1490                 memcpy_toio(&ch->ch_neo_uart->txrxburst,
1491                             ch->ch_wqueue + tail, s);
1492
1493                 /* Add and flip queue if needed */
1494                 tail = (tail + s) & WQUEUEMASK;
1495                 n -= s;
1496                 ch->ch_txcount += s;
1497                 len_written += s;
1498         }
1499
1500         /* Update the final tail */
1501         ch->ch_w_tail = tail & WQUEUEMASK;
1502
1503         if (len_written > 0) {
1504                 neo_pci_posting_flush(ch->ch_bd);
1505                 ch->ch_flags &= ~(CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1506         }
1507
1508 exit_unlock:
1509         spin_unlock_irqrestore(&ch->ch_lock, flags);
1510 }
1511
1512 static void neo_parse_modem(struct channel_t *ch, unsigned char signals)
1513 {
1514         unsigned char msignals = signals;
1515
1516         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1517                 return;
1518
1519         /*
1520          * Do altpin switching. Altpin switches DCD and DSR.
1521          * This prolly breaks DSRPACE, so we should be more clever here.
1522          */
1523         if (ch->ch_digi.digi_flags & DIGI_ALTPIN) {
1524                 unsigned char mswap = msignals;
1525
1526                 if (mswap & UART_MSR_DDCD) {
1527                         msignals &= ~UART_MSR_DDCD;
1528                         msignals |= UART_MSR_DDSR;
1529                 }
1530                 if (mswap & UART_MSR_DDSR) {
1531                         msignals &= ~UART_MSR_DDSR;
1532                         msignals |= UART_MSR_DDCD;
1533                 }
1534                 if (mswap & UART_MSR_DCD) {
1535                         msignals &= ~UART_MSR_DCD;
1536                         msignals |= UART_MSR_DSR;
1537                 }
1538                 if (mswap & UART_MSR_DSR) {
1539                         msignals &= ~UART_MSR_DSR;
1540                         msignals |= UART_MSR_DCD;
1541                 }
1542         }
1543
1544         /* Scrub off lower bits. They signify delta's, which I don't care about */
1545         msignals &= 0xf0;
1546
1547         if (msignals & UART_MSR_DCD)
1548                 ch->ch_mistat |= UART_MSR_DCD;
1549         else
1550                 ch->ch_mistat &= ~UART_MSR_DCD;
1551
1552         if (msignals & UART_MSR_DSR)
1553                 ch->ch_mistat |= UART_MSR_DSR;
1554         else
1555                 ch->ch_mistat &= ~UART_MSR_DSR;
1556
1557         if (msignals & UART_MSR_RI)
1558                 ch->ch_mistat |= UART_MSR_RI;
1559         else
1560                 ch->ch_mistat &= ~UART_MSR_RI;
1561
1562         if (msignals & UART_MSR_CTS)
1563                 ch->ch_mistat |= UART_MSR_CTS;
1564         else
1565                 ch->ch_mistat &= ~UART_MSR_CTS;
1566 }
1567
1568 /* Make the UART raise any of the output signals we want up */
1569 static void neo_assert_modem_signals(struct channel_t *ch)
1570 {
1571         unsigned char out;
1572
1573         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1574                 return;
1575
1576         out = ch->ch_mostat;
1577
1578         if (ch->ch_flags & CH_LOOPBACK)
1579                 out |= UART_MCR_LOOP;
1580
1581         writeb(out, &ch->ch_neo_uart->mcr);
1582         neo_pci_posting_flush(ch->ch_bd);
1583
1584         /* Give time for the UART to actually raise/drop the signals */
1585         udelay(10);
1586 }
1587
1588 static void neo_send_start_character(struct channel_t *ch)
1589 {
1590         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1591                 return;
1592
1593         if (ch->ch_startc != _POSIX_VDISABLE) {
1594                 ch->ch_xon_sends++;
1595                 writeb(ch->ch_startc, &ch->ch_neo_uart->txrx);
1596                 neo_pci_posting_flush(ch->ch_bd);
1597                 udelay(10);
1598         }
1599 }
1600
1601 static void neo_send_stop_character(struct channel_t *ch)
1602 {
1603         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1604                 return;
1605
1606         if (ch->ch_stopc != _POSIX_VDISABLE) {
1607                 ch->ch_xoff_sends++;
1608                 writeb(ch->ch_stopc, &ch->ch_neo_uart->txrx);
1609                 neo_pci_posting_flush(ch->ch_bd);
1610                 udelay(10);
1611         }
1612 }
1613
1614 /*
1615  * neo_uart_init
1616  */
1617 static void neo_uart_init(struct channel_t *ch)
1618 {
1619         writeb(0, &ch->ch_neo_uart->ier);
1620         writeb(0, &ch->ch_neo_uart->efr);
1621         writeb(UART_EFR_ECB, &ch->ch_neo_uart->efr);
1622
1623         /* Clear out UART and FIFO */
1624         readb(&ch->ch_neo_uart->txrx);
1625         writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
1626                &ch->ch_neo_uart->isr_fcr);
1627         readb(&ch->ch_neo_uart->lsr);
1628         readb(&ch->ch_neo_uart->msr);
1629
1630         ch->ch_flags |= CH_FIFO_ENABLED;
1631
1632         /* Assert any signals we want up */
1633         writeb(ch->ch_mostat, &ch->ch_neo_uart->mcr);
1634         neo_pci_posting_flush(ch->ch_bd);
1635 }
1636
1637 /*
1638  * Make the UART completely turn off.
1639  */
1640 static void neo_uart_off(struct channel_t *ch)
1641 {
1642         /* Turn off UART enhanced bits */
1643         writeb(0, &ch->ch_neo_uart->efr);
1644
1645         /* Stop all interrupts from occurring. */
1646         writeb(0, &ch->ch_neo_uart->ier);
1647         neo_pci_posting_flush(ch->ch_bd);
1648 }
1649
1650 static uint neo_get_uart_bytes_left(struct channel_t *ch)
1651 {
1652         unsigned char left = 0;
1653         unsigned char lsr = readb(&ch->ch_neo_uart->lsr);
1654
1655         /* We must cache the LSR as some of the bits get reset once read... */
1656         ch->ch_cached_lsr |= lsr;
1657
1658         /* Determine whether the Transmitter is empty or not */
1659         if (!(lsr & UART_LSR_TEMT)) {
1660                 if (ch->ch_flags & CH_TX_FIFO_EMPTY)
1661                         tasklet_schedule(&ch->ch_bd->helper_tasklet);
1662                 left = 1;
1663         } else {
1664                 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1665                 left = 0;
1666         }
1667
1668         return left;
1669 }
1670
1671 /* Channel lock MUST be held by the calling function! */
1672 static void neo_send_break(struct channel_t *ch, int msecs)
1673 {
1674         /*
1675          * If we receive a time of 0, this means turn off the break.
1676          */
1677         if (msecs == 0) {
1678                 if (ch->ch_flags & CH_BREAK_SENDING) {
1679                         unsigned char temp = readb(&ch->ch_neo_uart->lcr);
1680
1681                         writeb((temp & ~UART_LCR_SBC), &ch->ch_neo_uart->lcr);
1682                         neo_pci_posting_flush(ch->ch_bd);
1683                         ch->ch_flags &= ~(CH_BREAK_SENDING);
1684                         ch->ch_stop_sending_break = 0;
1685                 }
1686                 return;
1687         }
1688
1689         /*
1690          * Set the time we should stop sending the break.
1691          * If we are already sending a break, toss away the existing
1692          * time to stop, and use this new value instead.
1693          */
1694         ch->ch_stop_sending_break = jiffies + dgnc_jiffies_from_ms(msecs);
1695
1696         /* Tell the UART to start sending the break */
1697         if (!(ch->ch_flags & CH_BREAK_SENDING)) {
1698                 unsigned char temp = readb(&ch->ch_neo_uart->lcr);
1699
1700                 writeb((temp | UART_LCR_SBC), &ch->ch_neo_uart->lcr);
1701                 neo_pci_posting_flush(ch->ch_bd);
1702                 ch->ch_flags |= (CH_BREAK_SENDING);
1703         }
1704 }
1705
1706 /*
1707  * neo_send_immediate_char.
1708  *
1709  * Sends a specific character as soon as possible to the UART,
1710  * jumping over any bytes that might be in the write queue.
1711  *
1712  * The channel lock MUST be held by the calling function.
1713  */
1714 static void neo_send_immediate_char(struct channel_t *ch, unsigned char c)
1715 {
1716         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1717                 return;
1718
1719         writeb(c, &ch->ch_neo_uart->txrx);
1720         neo_pci_posting_flush(ch->ch_bd);
1721 }
1722
1723 static unsigned int neo_read_eeprom(unsigned char __iomem *base,
1724                                     unsigned int address)
1725 {
1726         unsigned int enable;
1727         unsigned int bits;
1728         unsigned int databit;
1729         unsigned int val;
1730
1731         /* enable chip select */
1732         writeb(NEO_EECS, base + NEO_EEREG);
1733         /* READ */
1734         enable = address | 0x180;
1735
1736         for (bits = 9; bits--; ) {
1737                 databit = (enable & (1 << bits)) ? NEO_EEDI : 0;
1738                 /* Set read address */
1739                 writeb(databit | NEO_EECS, base + NEO_EEREG);
1740                 writeb(databit | NEO_EECS | NEO_EECK, base + NEO_EEREG);
1741         }
1742
1743         val = 0;
1744
1745         for (bits = 17; bits--; ) {
1746                 /* clock to EEPROM */
1747                 writeb(NEO_EECS, base + NEO_EEREG);
1748                 writeb(NEO_EECS | NEO_EECK, base + NEO_EEREG);
1749                 val <<= 1;
1750                 /* read EEPROM */
1751                 if (readb(base + NEO_EEREG) & NEO_EEDO)
1752                         val |= 1;
1753         }
1754
1755         /* clock falling edge */
1756         writeb(NEO_EECS, base + NEO_EEREG);
1757
1758         /* drop chip select */
1759         writeb(0x00, base + NEO_EEREG);
1760
1761         return val;
1762 }
1763
1764 static void neo_vpd(struct dgnc_board *brd)
1765 {
1766         unsigned int i = 0;
1767         unsigned int a;
1768
1769         if (!brd || brd->magic != DGNC_BOARD_MAGIC)
1770                 return;
1771
1772         if (!brd->re_map_membase)
1773                 return;
1774
1775         /* Store the VPD into our buffer */
1776         for (i = 0; i < NEO_VPD_IMAGESIZE; i++) {
1777                 a = neo_read_eeprom(brd->re_map_membase, i);
1778                 brd->vpd[i * 2] = a & 0xff;
1779                 brd->vpd[(i * 2) + 1] = (a >> 8) & 0xff;
1780         }
1781
1782         /*
1783          * brd->vpd has different name tags by below index.
1784          * 0x08 : long resource name tag
1785          * 0x10 : long resource name tage (PCI-66 files)
1786          * 0x7F : small resource end tag
1787          */
1788         if  (((brd->vpd[0x08] != 0x82) &&
1789               (brd->vpd[0x10] != 0x82)) ||
1790              (brd->vpd[0x7F] != 0x78)) {
1791                 memset(brd->vpd, '\0', NEO_VPD_IMAGESIZE);
1792         } else {
1793                 /* Search for the serial number */
1794                 for (i = 0; i < NEO_VPD_IMAGEBYTES - 3; i++)
1795                         if (brd->vpd[i] == 'S' && brd->vpd[i + 1] == 'N')
1796                                 strncpy(brd->serial_num, &brd->vpd[i + 3], 9);
1797         }
1798 }