1 /************************************************************************
2 * Copyright 2003 Digi International (www.digi.com)
4 * Copyright (C) 2004 IBM Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 * Temple Place - Suite 330, Boston,
21 * Contact Information:
22 * Scott H Kilau <Scott_Kilau@digi.com>
23 * Wendy Xiong <wendyx@us.ltcfwd.linux.ibm.com>
25 ***********************************************************************/
26 #include <linux/delay.h> /* For udelay */
27 #include <linux/serial_reg.h> /* For the various UART offsets */
28 #include <linux/tty.h>
29 #include <linux/pci.h>
32 #include "jsm.h" /* Driver main header file */
34 static u32 jsm_offset_table[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
37 * This function allows calls to ensure that all outstanding
38 * PCI writes have been completed, by doing a PCI read against
39 * a non-destructive, read-only location on the Neo card.
41 * In this case, we are reading the DVID (Read-only Device Identification)
42 * value of the Neo card.
44 static inline void neo_pci_posting_flush(struct jsm_board *bd)
46 readb(bd->re_map_membase + 0x8D);
49 static void neo_set_cts_flow_control(struct jsm_channel *ch)
51 u8 ier = readb(&ch->ch_neo_uart->ier);
52 u8 efr = readb(&ch->ch_neo_uart->efr);
54 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting CTSFLOW\n");
56 /* Turn on auto CTS flow control */
57 ier |= (UART_17158_IER_CTSDSR);
58 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_CTSDSR);
60 /* Turn off auto Xon flow control */
61 efr &= ~(UART_17158_EFR_IXON);
63 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
64 writeb(0, &ch->ch_neo_uart->efr);
66 /* Turn on UART enhanced bits */
67 writeb(efr, &ch->ch_neo_uart->efr);
69 /* Turn on table D, with 8 char hi/low watermarks */
70 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_4DELAY), &ch->ch_neo_uart->fctr);
72 /* Feed the UART our trigger levels */
73 writeb(8, &ch->ch_neo_uart->tfifo);
76 writeb(ier, &ch->ch_neo_uart->ier);
79 static void neo_set_rts_flow_control(struct jsm_channel *ch)
81 u8 ier = readb(&ch->ch_neo_uart->ier);
82 u8 efr = readb(&ch->ch_neo_uart->efr);
84 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting RTSFLOW\n");
86 /* Turn on auto RTS flow control */
87 ier |= (UART_17158_IER_RTSDTR);
88 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_RTSDTR);
90 /* Turn off auto Xoff flow control */
91 ier &= ~(UART_17158_IER_XOFF);
92 efr &= ~(UART_17158_EFR_IXOFF);
94 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
95 writeb(0, &ch->ch_neo_uart->efr);
97 /* Turn on UART enhanced bits */
98 writeb(efr, &ch->ch_neo_uart->efr);
100 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_4DELAY), &ch->ch_neo_uart->fctr);
101 ch->ch_r_watermark = 4;
103 writeb(56, &ch->ch_neo_uart->rfifo);
104 ch->ch_r_tlevel = 56;
106 writeb(ier, &ch->ch_neo_uart->ier);
109 * From the Neo UART spec sheet:
110 * The auto RTS/DTR function must be started by asserting
111 * RTS/DTR# output pin (MCR bit-0 or 1 to logic 1 after
114 ch->ch_mostat |= (UART_MCR_RTS);
118 static void neo_set_ixon_flow_control(struct jsm_channel *ch)
120 u8 ier = readb(&ch->ch_neo_uart->ier);
121 u8 efr = readb(&ch->ch_neo_uart->efr);
123 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting IXON FLOW\n");
125 /* Turn off auto CTS flow control */
126 ier &= ~(UART_17158_IER_CTSDSR);
127 efr &= ~(UART_17158_EFR_CTSDSR);
129 /* Turn on auto Xon flow control */
130 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_IXON);
132 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
133 writeb(0, &ch->ch_neo_uart->efr);
135 /* Turn on UART enhanced bits */
136 writeb(efr, &ch->ch_neo_uart->efr);
138 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
139 ch->ch_r_watermark = 4;
141 writeb(32, &ch->ch_neo_uart->rfifo);
142 ch->ch_r_tlevel = 32;
144 /* Tell UART what start/stop chars it should be looking for */
145 writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
146 writeb(0, &ch->ch_neo_uart->xonchar2);
148 writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
149 writeb(0, &ch->ch_neo_uart->xoffchar2);
151 writeb(ier, &ch->ch_neo_uart->ier);
154 static void neo_set_ixoff_flow_control(struct jsm_channel *ch)
156 u8 ier = readb(&ch->ch_neo_uart->ier);
157 u8 efr = readb(&ch->ch_neo_uart->efr);
159 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Setting IXOFF FLOW\n");
161 /* Turn off auto RTS flow control */
162 ier &= ~(UART_17158_IER_RTSDTR);
163 efr &= ~(UART_17158_EFR_RTSDTR);
165 /* Turn on auto Xoff flow control */
166 ier |= (UART_17158_IER_XOFF);
167 efr |= (UART_17158_EFR_ECB | UART_17158_EFR_IXOFF);
169 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
170 writeb(0, &ch->ch_neo_uart->efr);
172 /* Turn on UART enhanced bits */
173 writeb(efr, &ch->ch_neo_uart->efr);
175 /* Turn on table D, with 8 char hi/low watermarks */
176 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
178 writeb(8, &ch->ch_neo_uart->tfifo);
181 /* Tell UART what start/stop chars it should be looking for */
182 writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
183 writeb(0, &ch->ch_neo_uart->xonchar2);
185 writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
186 writeb(0, &ch->ch_neo_uart->xoffchar2);
188 writeb(ier, &ch->ch_neo_uart->ier);
191 static void neo_set_no_input_flow_control(struct jsm_channel *ch)
193 u8 ier = readb(&ch->ch_neo_uart->ier);
194 u8 efr = readb(&ch->ch_neo_uart->efr);
196 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Unsetting Input FLOW\n");
198 /* Turn off auto RTS flow control */
199 ier &= ~(UART_17158_IER_RTSDTR);
200 efr &= ~(UART_17158_EFR_RTSDTR);
202 /* Turn off auto Xoff flow control */
203 ier &= ~(UART_17158_IER_XOFF);
204 if (ch->ch_c_iflag & IXON)
205 efr &= ~(UART_17158_EFR_IXOFF);
207 efr &= ~(UART_17158_EFR_ECB | UART_17158_EFR_IXOFF);
209 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
210 writeb(0, &ch->ch_neo_uart->efr);
212 /* Turn on UART enhanced bits */
213 writeb(efr, &ch->ch_neo_uart->efr);
215 /* Turn on table D, with 8 char hi/low watermarks */
216 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
218 ch->ch_r_watermark = 0;
220 writeb(16, &ch->ch_neo_uart->tfifo);
221 ch->ch_t_tlevel = 16;
223 writeb(16, &ch->ch_neo_uart->rfifo);
224 ch->ch_r_tlevel = 16;
226 writeb(ier, &ch->ch_neo_uart->ier);
229 static void neo_set_no_output_flow_control(struct jsm_channel *ch)
231 u8 ier = readb(&ch->ch_neo_uart->ier);
232 u8 efr = readb(&ch->ch_neo_uart->efr);
234 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "Unsetting Output FLOW\n");
236 /* Turn off auto CTS flow control */
237 ier &= ~(UART_17158_IER_CTSDSR);
238 efr &= ~(UART_17158_EFR_CTSDSR);
240 /* Turn off auto Xon flow control */
241 if (ch->ch_c_iflag & IXOFF)
242 efr &= ~(UART_17158_EFR_IXON);
244 efr &= ~(UART_17158_EFR_ECB | UART_17158_EFR_IXON);
246 /* Why? Becuz Exar's spec says we have to zero it out before setting it */
247 writeb(0, &ch->ch_neo_uart->efr);
249 /* Turn on UART enhanced bits */
250 writeb(efr, &ch->ch_neo_uart->efr);
252 /* Turn on table D, with 8 char hi/low watermarks */
253 writeb((UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY), &ch->ch_neo_uart->fctr);
255 ch->ch_r_watermark = 0;
257 writeb(16, &ch->ch_neo_uart->tfifo);
258 ch->ch_t_tlevel = 16;
260 writeb(16, &ch->ch_neo_uart->rfifo);
261 ch->ch_r_tlevel = 16;
263 writeb(ier, &ch->ch_neo_uart->ier);
266 static inline void neo_set_new_start_stop_chars(struct jsm_channel *ch)
269 /* if hardware flow control is set, then skip this whole thing */
270 if (ch->ch_c_cflag & CRTSCTS)
273 jsm_printk(PARAM, INFO, &ch->ch_bd->pci_dev, "start\n");
275 /* Tell UART what start/stop chars it should be looking for */
276 writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
277 writeb(0, &ch->ch_neo_uart->xonchar2);
279 writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
280 writeb(0, &ch->ch_neo_uart->xoffchar2);
283 static void neo_copy_data_from_uart_to_queue(struct jsm_channel *ch)
296 /* cache head and tail of queue */
297 head = ch->ch_r_head & RQUEUEMASK;
298 tail = ch->ch_r_tail & RQUEUEMASK;
300 /* Get our cached LSR */
301 linestatus = ch->ch_cached_lsr;
302 ch->ch_cached_lsr = 0;
304 /* Store how much space we have left in the queue */
305 if ((qleft = tail - head - 1) < 0)
306 qleft += RQUEUEMASK + 1;
309 * If the UART is not in FIFO mode, force the FIFO copy to
310 * NOT be run, by setting total to 0.
312 * On the other hand, if the UART IS in FIFO mode, then ask
313 * the UART to give us an approximation of data it has RX'ed.
315 if (!(ch->ch_flags & CH_FIFO_ENABLED))
318 total = readb(&ch->ch_neo_uart->rfifo);
321 * EXAR chip bug - RX FIFO COUNT - Fudge factor.
323 * This resolves a problem/bug with the Exar chip that sometimes
324 * returns a bogus value in the rfifo register.
325 * The count can be any where from 0-3 bytes "off".
332 * Finally, bound the copy to make sure we don't overflow
334 * The byte by byte copy loop below this loop this will
335 * deal with the queue overflow possibility.
337 total = min(total, qleft);
341 * Grab the linestatus register, we need to check
342 * to see if there are any errors in the FIFO.
344 linestatus = readb(&ch->ch_neo_uart->lsr);
347 * Break out if there is a FIFO error somewhere.
348 * This will allow us to go byte by byte down below,
349 * finding the exact location of the error.
351 if (linestatus & UART_17158_RX_FIFO_DATA_ERROR)
354 /* Make sure we don't go over the end of our queue */
355 n = min(((u32) total), (RQUEUESIZE - (u32) head));
358 * Cut down n even further if needed, this is to fix
359 * a problem with memcpy_fromio() with the Neo on the
360 * IBM pSeries platform.
361 * 15 bytes max appears to be the magic number.
363 n = min((u32) n, (u32) 12);
366 * Since we are grabbing the linestatus register, which
367 * will reset some bits after our read, we need to ensure
368 * we don't miss our TX FIFO emptys.
370 if (linestatus & (UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR))
371 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
375 /* Copy data from uart to the queue */
376 memcpy_fromio(ch->ch_rqueue + head, &ch->ch_neo_uart->txrxburst, n);
378 * Since RX_FIFO_DATA_ERROR was 0, we are guarenteed
379 * that all the data currently in the FIFO is free of
380 * breaks and parity/frame/orun errors.
382 memset(ch->ch_equeue + head, 0, n);
384 /* Add to and flip head if needed */
385 head = (head + n) & RQUEUEMASK;
392 * Create a mask to determine whether we should
393 * insert the character (if any) into our queue.
395 if (ch->ch_c_iflag & IGNBRK)
396 error_mask |= UART_LSR_BI;
399 * Now cleanup any leftover bytes still in the UART.
400 * Also deal with any possible queue overflow here as well.
405 * Its possible we have a linestatus from the loop above
406 * this, so we "OR" on any extra bits.
408 linestatus |= readb(&ch->ch_neo_uart->lsr);
411 * If the chip tells us there is no more data pending to
412 * be read, we can then leave.
413 * But before we do, cache the linestatus, just in case.
415 if (!(linestatus & UART_LSR_DR)) {
416 ch->ch_cached_lsr = linestatus;
420 /* No need to store this bit */
421 linestatus &= ~UART_LSR_DR;
424 * Since we are grabbing the linestatus register, which
425 * will reset some bits after our read, we need to ensure
426 * we don't miss our TX FIFO emptys.
428 if (linestatus & (UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR)) {
429 linestatus &= ~(UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR);
430 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
434 * Discard character if we are ignoring the error mask.
436 if (linestatus & error_mask) {
439 memcpy_fromio(&discard, &ch->ch_neo_uart->txrxburst, 1);
444 * If our queue is full, we have no choice but to drop some data.
445 * The assumption is that HWFLOW or SWFLOW should have stopped
446 * things way way before we got to this point.
448 * I decided that I wanted to ditch the oldest data first,
449 * I hope thats okay with everyone? Yes? Good.
452 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
453 "Queue full, dropping DATA:%x LSR:%x\n",
454 ch->ch_rqueue[tail], ch->ch_equeue[tail]);
456 ch->ch_r_tail = tail = (tail + 1) & RQUEUEMASK;
457 ch->ch_err_overrun++;
461 memcpy_fromio(ch->ch_rqueue + head, &ch->ch_neo_uart->txrxburst, 1);
462 ch->ch_equeue[head] = (u8) linestatus;
464 jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
465 "DATA/LSR pair: %x %x\n", ch->ch_rqueue[head], ch->ch_equeue[head]);
467 /* Ditch any remaining linestatus value. */
470 /* Add to and flip head if needed */
471 head = (head + 1) & RQUEUEMASK;
478 * Write new final heads to channel structure.
480 ch->ch_r_head = head & RQUEUEMASK;
481 ch->ch_e_head = head & EQUEUEMASK;
485 static void neo_copy_data_from_queue_to_uart(struct jsm_channel *ch)
497 /* No data to write to the UART */
498 if (ch->ch_w_tail == ch->ch_w_head)
501 /* If port is "stopped", don't send any data to the UART */
502 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_BREAK_SENDING))
505 * If FIFOs are disabled. Send data directly to txrx register
507 if (!(ch->ch_flags & CH_FIFO_ENABLED)) {
508 u8 lsrbits = readb(&ch->ch_neo_uart->lsr);
510 ch->ch_cached_lsr |= lsrbits;
511 if (ch->ch_cached_lsr & UART_LSR_THRE) {
512 ch->ch_cached_lsr &= ~(UART_LSR_THRE);
514 writeb(ch->ch_wqueue[ch->ch_w_tail], &ch->ch_neo_uart->txrx);
515 jsm_printk(WRITE, INFO, &ch->ch_bd->pci_dev,
516 "Tx data: %x\n", ch->ch_wqueue[ch->ch_w_head]);
518 ch->ch_w_tail &= WQUEUEMASK;
525 * We have to do it this way, because of the EXAR TXFIFO count bug.
527 if (!(ch->ch_flags & (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM)))
531 n = UART_17158_TX_FIFOSIZE - ch->ch_t_tlevel;
533 /* cache head and tail of queue */
534 head = ch->ch_w_head & WQUEUEMASK;
535 tail = ch->ch_w_tail & WQUEUEMASK;
536 qlen = (head - tail) & WQUEUEMASK;
538 /* Find minimum of the FIFO space, versus queue length */
543 s = ((head >= tail) ? head : WQUEUESIZE) - tail;
549 memcpy_toio(&ch->ch_neo_uart->txrxburst, ch->ch_wqueue + tail, s);
550 /* Add and flip queue if needed */
551 tail = (tail + s) & WQUEUEMASK;
557 /* Update the final tail */
558 ch->ch_w_tail = tail & WQUEUEMASK;
560 if (len_written >= ch->ch_t_tlevel)
561 ch->ch_flags &= ~(CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
563 if (!jsm_tty_write(&ch->uart_port))
564 uart_write_wakeup(&ch->uart_port);
567 static void neo_parse_modem(struct jsm_channel *ch, u8 signals)
569 u8 msignals = signals;
571 jsm_printk(MSIGS, INFO, &ch->ch_bd->pci_dev,
572 "neo_parse_modem: port: %d msignals: %x\n", ch->ch_portnum, msignals);
577 /* Scrub off lower bits. They signify delta's, which I don't care about */
580 if (msignals & UART_MSR_DCD)
581 ch->ch_mistat |= UART_MSR_DCD;
583 ch->ch_mistat &= ~UART_MSR_DCD;
585 if (msignals & UART_MSR_DSR)
586 ch->ch_mistat |= UART_MSR_DSR;
588 ch->ch_mistat &= ~UART_MSR_DSR;
590 if (msignals & UART_MSR_RI)
591 ch->ch_mistat |= UART_MSR_RI;
593 ch->ch_mistat &= ~UART_MSR_RI;
595 if (msignals & UART_MSR_CTS)
596 ch->ch_mistat |= UART_MSR_CTS;
598 ch->ch_mistat &= ~UART_MSR_CTS;
600 jsm_printk(MSIGS, INFO, &ch->ch_bd->pci_dev,
601 "Port: %d DTR: %d RTS: %d CTS: %d DSR: %d " "RI: %d CD: %d\n",
603 !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_DTR),
604 !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_RTS),
605 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_CTS),
606 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DSR),
607 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_RI),
608 !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DCD));
611 /* Make the UART raise any of the output signals we want up */
612 static void neo_assert_modem_signals(struct jsm_channel *ch)
621 writeb(out, &ch->ch_neo_uart->mcr);
623 /* flush write operation */
624 neo_pci_posting_flush(ch->ch_bd);
628 * Flush the WRITE FIFO on the Neo.
630 * NOTE: Channel lock MUST be held before calling this function!
632 static void neo_flush_uart_write(struct jsm_channel *ch)
640 writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_XMIT), &ch->ch_neo_uart->isr_fcr);
642 for (i = 0; i < 10; i++) {
644 /* Check to see if the UART feels it completely flushed the FIFO. */
645 tmp = readb(&ch->ch_neo_uart->isr_fcr);
647 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev,
648 "Still flushing TX UART... i: %d\n", i);
655 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
660 * Flush the READ FIFO on the Neo.
662 * NOTE: Channel lock MUST be held before calling this function!
664 static void neo_flush_uart_read(struct jsm_channel *ch)
672 writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR), &ch->ch_neo_uart->isr_fcr);
674 for (i = 0; i < 10; i++) {
676 /* Check to see if the UART feels it completely flushed the FIFO. */
677 tmp = readb(&ch->ch_neo_uart->isr_fcr);
679 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev,
680 "Still flushing RX UART... i: %d\n", i);
689 * No locks are assumed to be held when calling this function.
691 void neo_clear_break(struct jsm_channel *ch, int force)
693 unsigned long lock_flags;
695 spin_lock_irqsave(&ch->ch_lock, lock_flags);
697 /* Turn break off, and unset some variables */
698 if (ch->ch_flags & CH_BREAK_SENDING) {
699 u8 temp = readb(&ch->ch_neo_uart->lcr);
700 writeb((temp & ~UART_LCR_SBC), &ch->ch_neo_uart->lcr);
702 ch->ch_flags &= ~(CH_BREAK_SENDING);
703 jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev,
704 "clear break Finishing UART_LCR_SBC! finished: %lx\n", jiffies);
706 /* flush write operation */
707 neo_pci_posting_flush(ch->ch_bd);
709 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
713 * Parse the ISR register.
715 static inline void neo_parse_isr(struct jsm_board *brd, u32 port)
717 struct jsm_channel *ch;
720 unsigned long lock_flags;
725 if (port > brd->maxports)
728 ch = brd->channels[port];
732 /* Here we try to figure out what caused the interrupt to happen */
735 isr = readb(&ch->ch_neo_uart->isr_fcr);
737 /* Bail if no pending interrupt */
738 if (isr & UART_IIR_NO_INT)
742 * Yank off the upper 2 bits, which just show that the FIFO's are enabled.
744 isr &= ~(UART_17158_IIR_FIFO_ENABLED);
746 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
747 "%s:%d isr: %x\n", __FILE__, __LINE__, isr);
749 if (isr & (UART_17158_IIR_RDI_TIMEOUT | UART_IIR_RDI)) {
750 /* Read data from uart -> queue */
751 neo_copy_data_from_uart_to_queue(ch);
753 /* Call our tty layer to enforce queue flow control if needed. */
754 spin_lock_irqsave(&ch->ch_lock, lock_flags);
755 jsm_check_queue_flow_control(ch);
756 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
759 if (isr & UART_IIR_THRI) {
760 /* Transfer data (if any) from Write Queue -> UART. */
761 spin_lock_irqsave(&ch->ch_lock, lock_flags);
762 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
763 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
764 neo_copy_data_from_queue_to_uart(ch);
767 if (isr & UART_17158_IIR_XONXOFF) {
768 cause = readb(&ch->ch_neo_uart->xoffchar1);
770 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
771 "Port %d. Got ISR_XONXOFF: cause:%x\n", port, cause);
774 * Since the UART detected either an XON or
775 * XOFF match, we need to figure out which
776 * one it was, so we can suspend or resume data flow.
778 spin_lock_irqsave(&ch->ch_lock, lock_flags);
779 if (cause == UART_17158_XON_DETECT) {
780 /* Is output stopped right now, if so, resume it */
781 if (brd->channels[port]->ch_flags & CH_STOP) {
782 ch->ch_flags &= ~(CH_STOP);
784 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
785 "Port %d. XON detected in incoming data\n", port);
787 else if (cause == UART_17158_XOFF_DETECT) {
788 if (!(brd->channels[port]->ch_flags & CH_STOP)) {
789 ch->ch_flags |= CH_STOP;
790 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
791 "Setting CH_STOP\n");
793 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
794 "Port: %d. XOFF detected in incoming data\n", port);
796 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
799 if (isr & UART_17158_IIR_HWFLOW_STATE_CHANGE) {
801 * If we get here, this means the hardware is doing auto flow control.
802 * Check to see whether RTS/DTR or CTS/DSR caused this interrupt.
804 cause = readb(&ch->ch_neo_uart->mcr);
806 /* Which pin is doing auto flow? RTS or DTR? */
807 spin_lock_irqsave(&ch->ch_lock, lock_flags);
808 if ((cause & 0x4) == 0) {
809 if (cause & UART_MCR_RTS)
810 ch->ch_mostat |= UART_MCR_RTS;
812 ch->ch_mostat &= ~(UART_MCR_RTS);
814 if (cause & UART_MCR_DTR)
815 ch->ch_mostat |= UART_MCR_DTR;
817 ch->ch_mostat &= ~(UART_MCR_DTR);
819 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
822 /* Parse any modem signal changes */
823 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
824 "MOD_STAT: sending to parse_modem_sigs\n");
825 neo_parse_modem(ch, readb(&ch->ch_neo_uart->msr));
829 static inline void neo_parse_lsr(struct jsm_board *brd, u32 port)
831 struct jsm_channel *ch;
833 unsigned long lock_flags;
838 if (port > brd->maxports)
841 ch = brd->channels[port];
845 linestatus = readb(&ch->ch_neo_uart->lsr);
847 jsm_printk(INTR, INFO, &ch->ch_bd->pci_dev,
848 "%s:%d port: %d linestatus: %x\n", __FILE__, __LINE__, port, linestatus);
850 ch->ch_cached_lsr |= linestatus;
852 if (ch->ch_cached_lsr & UART_LSR_DR) {
853 /* Read data from uart -> queue */
854 neo_copy_data_from_uart_to_queue(ch);
855 spin_lock_irqsave(&ch->ch_lock, lock_flags);
856 jsm_check_queue_flow_control(ch);
857 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
861 * This is a special flag. It indicates that at least 1
862 * RX error (parity, framing, or break) has happened.
863 * Mark this in our struct, which will tell me that I have
864 *to do the special RX+LSR read for this FIFO load.
866 if (linestatus & UART_17158_RX_FIFO_DATA_ERROR)
867 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
868 "%s:%d Port: %d Got an RX error, need to parse LSR\n",
869 __FILE__, __LINE__, port);
872 * The next 3 tests should *NOT* happen, as the above test
873 * should encapsulate all 3... At least, thats what Exar says.
876 if (linestatus & UART_LSR_PE) {
878 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
879 "%s:%d Port: %d. PAR ERR!\n", __FILE__, __LINE__, port);
882 if (linestatus & UART_LSR_FE) {
884 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
885 "%s:%d Port: %d. FRM ERR!\n", __FILE__, __LINE__, port);
888 if (linestatus & UART_LSR_BI) {
890 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
891 "%s:%d Port: %d. BRK INTR!\n", __FILE__, __LINE__, port);
894 if (linestatus & UART_LSR_OE) {
896 * Rx Oruns. Exar says that an orun will NOT corrupt
897 * the FIFO. It will just replace the holding register
898 * with this new data byte. So basically just ignore this.
899 * Probably we should eventually have an orun stat in our driver...
901 ch->ch_err_overrun++;
902 jsm_printk(INTR, DEBUG, &ch->ch_bd->pci_dev,
903 "%s:%d Port: %d. Rx Overrun!\n", __FILE__, __LINE__, port);
906 if (linestatus & UART_LSR_THRE) {
907 spin_lock_irqsave(&ch->ch_lock, lock_flags);
908 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
909 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
911 /* Transfer data (if any) from Write Queue -> UART. */
912 neo_copy_data_from_queue_to_uart(ch);
914 else if (linestatus & UART_17158_TX_AND_FIFO_CLR) {
915 spin_lock_irqsave(&ch->ch_lock, lock_flags);
916 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
917 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
919 /* Transfer data (if any) from Write Queue -> UART. */
920 neo_copy_data_from_queue_to_uart(ch);
926 * Send any/all changes to the line to the UART.
928 static void neo_param(struct jsm_channel *ch)
935 struct jsm_board *bd;
942 * If baud rate is zero, flush queues, and set mval to drop DTR.
944 if ((ch->ch_c_cflag & (CBAUD)) == 0) {
945 ch->ch_r_head = ch->ch_r_tail = 0;
946 ch->ch_e_head = ch->ch_e_tail = 0;
947 ch->ch_w_head = ch->ch_w_tail = 0;
949 neo_flush_uart_write(ch);
950 neo_flush_uart_read(ch);
952 ch->ch_flags |= (CH_BAUD0);
953 ch->ch_mostat &= ~(UART_MCR_RTS | UART_MCR_DTR);
954 neo_assert_modem_signals(ch);
958 } else if (ch->ch_custom_speed) {
959 baud = ch->ch_custom_speed;
960 if (ch->ch_flags & CH_BAUD0)
961 ch->ch_flags &= ~(CH_BAUD0);
966 const u64 bauds[4][16] = {
970 600, 1200, 1800, 2400,
971 4800, 9600, 19200, 38400 },
973 0, 57600, 115200, 230400,
974 460800, 150, 200, 921600,
975 600, 1200, 1800, 2400,
976 4800, 9600, 19200, 38400 },
978 0, 57600, 76800, 115200,
979 131657, 153600, 230400, 460800,
980 921600, 1200, 1800, 2400,
981 4800, 9600, 19200, 38400 },
983 0, 57600, 115200, 230400,
984 460800, 150, 200, 921600,
985 600, 1200, 1800, 2400,
986 4800, 9600, 19200, 38400 }
989 baud = C_BAUD(ch->uart_port.info->tty) & 0xff;
991 if (ch->ch_c_cflag & CBAUDEX)
996 if ((iindex >= 0) && (iindex < 4) && (jindex >= 0) && (jindex < 16))
997 baud = bauds[iindex][jindex];
999 jsm_printk(IOCTL, DEBUG, &ch->ch_bd->pci_dev,
1000 "baud indices were out of range (%d)(%d)",
1008 if (ch->ch_flags & CH_BAUD0)
1009 ch->ch_flags &= ~(CH_BAUD0);
1012 if (ch->ch_c_cflag & PARENB)
1013 lcr |= UART_LCR_PARITY;
1015 if (!(ch->ch_c_cflag & PARODD))
1016 lcr |= UART_LCR_EPAR;
1019 * Not all platforms support mark/space parity,
1020 * so this will hide behind an ifdef.
1023 if (ch->ch_c_cflag & CMSPAR)
1024 lcr |= UART_LCR_SPAR;
1027 if (ch->ch_c_cflag & CSTOPB)
1028 lcr |= UART_LCR_STOP;
1030 switch (ch->ch_c_cflag & CSIZE) {
1032 lcr |= UART_LCR_WLEN5;
1035 lcr |= UART_LCR_WLEN6;
1038 lcr |= UART_LCR_WLEN7;
1042 lcr |= UART_LCR_WLEN8;
1046 ier = readb(&ch->ch_neo_uart->ier);
1047 uart_lcr = readb(&ch->ch_neo_uart->lcr);
1052 quot = ch->ch_bd->bd_dividend / baud;
1055 ch->ch_old_baud = baud;
1056 writeb(UART_LCR_DLAB, &ch->ch_neo_uart->lcr);
1057 writeb((quot & 0xff), &ch->ch_neo_uart->txrx);
1058 writeb((quot >> 8), &ch->ch_neo_uart->ier);
1059 writeb(lcr, &ch->ch_neo_uart->lcr);
1062 if (uart_lcr != lcr)
1063 writeb(lcr, &ch->ch_neo_uart->lcr);
1065 if (ch->ch_c_cflag & CREAD)
1066 ier |= (UART_IER_RDI | UART_IER_RLSI);
1068 ier |= (UART_IER_THRI | UART_IER_MSI);
1070 writeb(ier, &ch->ch_neo_uart->ier);
1072 /* Set new start/stop chars */
1073 neo_set_new_start_stop_chars(ch);
1075 if (ch->ch_c_cflag & CRTSCTS)
1076 neo_set_cts_flow_control(ch);
1077 else if (ch->ch_c_iflag & IXON) {
1078 /* If start/stop is set to disable, then we should disable flow control */
1079 if ((ch->ch_startc == __DISABLED_CHAR) || (ch->ch_stopc == __DISABLED_CHAR))
1080 neo_set_no_output_flow_control(ch);
1082 neo_set_ixon_flow_control(ch);
1085 neo_set_no_output_flow_control(ch);
1087 if (ch->ch_c_cflag & CRTSCTS)
1088 neo_set_rts_flow_control(ch);
1089 else if (ch->ch_c_iflag & IXOFF) {
1090 /* If start/stop is set to disable, then we should disable flow control */
1091 if ((ch->ch_startc == __DISABLED_CHAR) || (ch->ch_stopc == __DISABLED_CHAR))
1092 neo_set_no_input_flow_control(ch);
1094 neo_set_ixoff_flow_control(ch);
1097 neo_set_no_input_flow_control(ch);
1099 * Adjust the RX FIFO Trigger level if baud is less than 9600.
1100 * Not exactly elegant, but this is needed because of the Exar chip's
1101 * delay on firing off the RX FIFO interrupt on slower baud rates.
1104 writeb(1, &ch->ch_neo_uart->rfifo);
1105 ch->ch_r_tlevel = 1;
1108 neo_assert_modem_signals(ch);
1110 /* Get current status of the modem signals now */
1111 neo_parse_modem(ch, readb(&ch->ch_neo_uart->msr));
1118 * Neo specific interrupt handler.
1120 static irqreturn_t neo_intr(int irq, void *voidbrd, struct pt_regs *regs)
1122 struct jsm_board *brd = (struct jsm_board *) voidbrd;
1123 struct jsm_channel *ch;
1129 unsigned long lock_flags;
1130 unsigned long lock_flags2;
1131 int outofloop_count = 0;
1135 /* Lock out the slow poller from running on this board. */
1136 spin_lock_irqsave(&brd->bd_intr_lock, lock_flags);
1139 * Read in "extended" IRQ information from the 32bit Neo register.
1140 * Bits 0-7: What port triggered the interrupt.
1141 * Bits 8-31: Each 3bits indicate what type of interrupt occurred.
1143 uart_poll = readl(brd->re_map_membase + UART_17158_POLL_ADDR_OFFSET);
1145 jsm_printk(INTR, INFO, &brd->pci_dev,
1146 "%s:%d uart_poll: %x\n", __FILE__, __LINE__, uart_poll);
1149 jsm_printk(INTR, INFO, &brd->pci_dev,
1150 "Kernel interrupted to me, but no pending interrupts...\n");
1151 spin_unlock_irqrestore(&brd->bd_intr_lock, lock_flags);
1155 /* At this point, we have at least SOMETHING to service, dig further... */
1159 /* Loop on each port */
1160 while (((uart_poll & 0xff) != 0) && (outofloop_count < 0xff)){
1165 /* Check current port to see if it has interrupt pending */
1166 if ((tmp & jsm_offset_table[current_port]) != 0) {
1167 port = current_port;
1168 type = tmp >> (8 + (port * 3));
1175 jsm_printk(INTR, INFO, &brd->pci_dev,
1176 "%s:%d port: %x type: %x\n", __FILE__, __LINE__, port, type);
1178 /* Remove this port + type from uart_poll */
1179 uart_poll &= ~(jsm_offset_table[port]);
1182 /* If no type, just ignore it, and move onto next port */
1183 jsm_printk(INTR, ERR, &brd->pci_dev,
1184 "Interrupt with no type! port: %d\n", port);
1188 /* Switch on type of interrupt we have */
1191 case UART_17158_RXRDY_TIMEOUT:
1193 * RXRDY Time-out is cleared by reading data in the
1194 * RX FIFO until it falls below the trigger level.
1197 /* Verify the port is in range. */
1198 if (port > brd->nasync)
1201 ch = brd->channels[port];
1202 neo_copy_data_from_uart_to_queue(ch);
1204 /* Call our tty layer to enforce queue flow control if needed. */
1205 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
1206 jsm_check_queue_flow_control(ch);
1207 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1211 case UART_17158_RX_LINE_STATUS:
1213 * RXRDY and RX LINE Status (logic OR of LSR[4:1])
1215 neo_parse_lsr(brd, port);
1218 case UART_17158_TXRDY:
1220 * TXRDY interrupt clears after reading ISR register for the UART channel.
1224 * Yes, this is odd...
1225 * Why would I check EVERY possibility of type of
1226 * interrupt, when we know its TXRDY???
1227 * Becuz for some reason, even tho we got triggered for TXRDY,
1228 * it seems to be occassionally wrong. Instead of TX, which
1229 * it should be, I was getting things like RXDY too. Weird.
1231 neo_parse_isr(brd, port);
1234 case UART_17158_MSR:
1236 * MSR or flow control was seen.
1238 neo_parse_isr(brd, port);
1243 * The UART triggered us with a bogus interrupt type.
1244 * It appears the Exar chip, when REALLY bogged down, will throw
1245 * these once and awhile.
1246 * Its harmless, just ignore it and move on.
1248 jsm_printk(INTR, ERR, &brd->pci_dev,
1249 "%s:%d Unknown Interrupt type: %x\n", __FILE__, __LINE__, type);
1254 spin_unlock_irqrestore(&brd->bd_intr_lock, lock_flags);
1256 jsm_printk(INTR, INFO, &brd->pci_dev, "finish.\n");
1261 * Neo specific way of turning off the receiver.
1262 * Used as a way to enforce queue flow control when in
1263 * hardware flow control mode.
1265 static void neo_disable_receiver(struct jsm_channel *ch)
1267 u8 tmp = readb(&ch->ch_neo_uart->ier);
1268 tmp &= ~(UART_IER_RDI);
1269 writeb(tmp, &ch->ch_neo_uart->ier);
1271 /* flush write operation */
1272 neo_pci_posting_flush(ch->ch_bd);
1277 * Neo specific way of turning on the receiver.
1278 * Used as a way to un-enforce queue flow control when in
1279 * hardware flow control mode.
1281 static void neo_enable_receiver(struct jsm_channel *ch)
1283 u8 tmp = readb(&ch->ch_neo_uart->ier);
1284 tmp |= (UART_IER_RDI);
1285 writeb(tmp, &ch->ch_neo_uart->ier);
1287 /* flush write operation */
1288 neo_pci_posting_flush(ch->ch_bd);
1291 static void neo_send_start_character(struct jsm_channel *ch)
1296 if (ch->ch_startc != __DISABLED_CHAR) {
1298 writeb(ch->ch_startc, &ch->ch_neo_uart->txrx);
1300 /* flush write operation */
1301 neo_pci_posting_flush(ch->ch_bd);
1305 static void neo_send_stop_character(struct jsm_channel *ch)
1310 if (ch->ch_stopc != __DISABLED_CHAR) {
1311 ch->ch_xoff_sends++;
1312 writeb(ch->ch_stopc, &ch->ch_neo_uart->txrx);
1314 /* flush write operation */
1315 neo_pci_posting_flush(ch->ch_bd);
1322 static void neo_uart_init(struct jsm_channel *ch)
1324 writeb(0, &ch->ch_neo_uart->ier);
1325 writeb(0, &ch->ch_neo_uart->efr);
1326 writeb(UART_EFR_ECB, &ch->ch_neo_uart->efr);
1328 /* Clear out UART and FIFO */
1329 readb(&ch->ch_neo_uart->txrx);
1330 writeb((UART_FCR_ENABLE_FIFO|UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT), &ch->ch_neo_uart->isr_fcr);
1331 readb(&ch->ch_neo_uart->lsr);
1332 readb(&ch->ch_neo_uart->msr);
1334 ch->ch_flags |= CH_FIFO_ENABLED;
1336 /* Assert any signals we want up */
1337 writeb(ch->ch_mostat, &ch->ch_neo_uart->mcr);
1341 * Make the UART completely turn off.
1343 static void neo_uart_off(struct jsm_channel *ch)
1345 /* Turn off UART enhanced bits */
1346 writeb(0, &ch->ch_neo_uart->efr);
1348 /* Stop all interrupts from occurring. */
1349 writeb(0, &ch->ch_neo_uart->ier);
1352 static u32 neo_get_uart_bytes_left(struct jsm_channel *ch)
1355 u8 lsr = readb(&ch->ch_neo_uart->lsr);
1357 /* We must cache the LSR as some of the bits get reset once read... */
1358 ch->ch_cached_lsr |= lsr;
1360 /* Determine whether the Transmitter is empty or not */
1361 if (!(lsr & UART_LSR_TEMT))
1364 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1371 /* Channel lock MUST be held by the calling function! */
1372 static void neo_send_break(struct jsm_channel *ch)
1375 * Set the time we should stop sending the break.
1376 * If we are already sending a break, toss away the existing
1377 * time to stop, and use this new value instead.
1380 /* Tell the UART to start sending the break */
1381 if (!(ch->ch_flags & CH_BREAK_SENDING)) {
1382 u8 temp = readb(&ch->ch_neo_uart->lcr);
1383 writeb((temp | UART_LCR_SBC), &ch->ch_neo_uart->lcr);
1384 ch->ch_flags |= (CH_BREAK_SENDING);
1386 /* flush write operation */
1387 neo_pci_posting_flush(ch->ch_bd);
1392 * neo_send_immediate_char.
1394 * Sends a specific character as soon as possible to the UART,
1395 * jumping over any bytes that might be in the write queue.
1397 * The channel lock MUST be held by the calling function.
1399 static void neo_send_immediate_char(struct jsm_channel *ch, unsigned char c)
1404 writeb(c, &ch->ch_neo_uart->txrx);
1406 /* flush write operation */
1407 neo_pci_posting_flush(ch->ch_bd);
1410 struct board_ops jsm_neo_ops = {
1412 .uart_init = neo_uart_init,
1413 .uart_off = neo_uart_off,
1415 .assert_modem_signals = neo_assert_modem_signals,
1416 .flush_uart_write = neo_flush_uart_write,
1417 .flush_uart_read = neo_flush_uart_read,
1418 .disable_receiver = neo_disable_receiver,
1419 .enable_receiver = neo_enable_receiver,
1420 .send_break = neo_send_break,
1421 .clear_break = neo_clear_break,
1422 .send_start_character = neo_send_start_character,
1423 .send_stop_character = neo_send_stop_character,
1424 .copy_data_from_queue_to_uart = neo_copy_data_from_queue_to_uart,
1425 .get_uart_bytes_left = neo_get_uart_bytes_left,
1426 .send_immediate_char = neo_send_immediate_char