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