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