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