1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2003 Digi International (www.digi.com)
4 * Scott H Kilau <Scott_Kilau at digi dot com>
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
9 #include <linux/interrupt.h>
10 #include <linux/delay.h>
12 #include <linux/serial.h>
13 #include <linux/serial_reg.h>
14 #include <linux/pci.h>
16 #include "dgnc_driver.h"
20 static inline void cls_set_cts_flow_control(struct channel_t *ch)
22 unsigned char lcrb = readb(&ch->ch_cls_uart->lcr);
23 unsigned char ier = readb(&ch->ch_cls_uart->ier);
24 unsigned char isr_fcr = 0;
27 * The Enhanced Register Set may only be accessed when
28 * the Line Control Register is set to 0xBFh.
30 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
32 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
34 /* Turn on CTS flow control, turn off IXON flow control */
35 isr_fcr |= (UART_EXAR654_EFR_ECB | UART_EXAR654_EFR_CTSDSR);
36 isr_fcr &= ~(UART_EXAR654_EFR_IXON);
38 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
40 /* Write old LCR value back out, which turns enhanced access off */
41 writeb(lcrb, &ch->ch_cls_uart->lcr);
44 * Enable interrupts for CTS flow, turn off interrupts for
47 ier |= (UART_EXAR654_IER_CTSDSR);
48 ier &= ~(UART_EXAR654_IER_XOFF);
49 writeb(ier, &ch->ch_cls_uart->ier);
51 /* Set the usual FIFO values */
52 writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
54 writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_56 |
55 UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
56 &ch->ch_cls_uart->isr_fcr);
61 static inline void cls_set_ixon_flow_control(struct channel_t *ch)
63 unsigned char lcrb = readb(&ch->ch_cls_uart->lcr);
64 unsigned char ier = readb(&ch->ch_cls_uart->ier);
65 unsigned char isr_fcr = 0;
68 * The Enhanced Register Set may only be accessed when
69 * the Line Control Register is set to 0xBFh.
71 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
73 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
75 /* Turn on IXON flow control, turn off CTS flow control */
76 isr_fcr |= (UART_EXAR654_EFR_ECB | UART_EXAR654_EFR_IXON);
77 isr_fcr &= ~(UART_EXAR654_EFR_CTSDSR);
79 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
81 /* Now set our current start/stop chars while in enhanced mode */
82 writeb(ch->ch_startc, &ch->ch_cls_uart->mcr);
83 writeb(0, &ch->ch_cls_uart->lsr);
84 writeb(ch->ch_stopc, &ch->ch_cls_uart->msr);
85 writeb(0, &ch->ch_cls_uart->spr);
87 /* Write old LCR value back out, which turns enhanced access off */
88 writeb(lcrb, &ch->ch_cls_uart->lcr);
91 * Disable interrupts for CTS flow, turn on interrupts for
94 ier &= ~(UART_EXAR654_IER_CTSDSR);
95 ier |= (UART_EXAR654_IER_XOFF);
96 writeb(ier, &ch->ch_cls_uart->ier);
98 /* Set the usual FIFO values */
99 writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
101 writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_16 |
102 UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
103 &ch->ch_cls_uart->isr_fcr);
106 static inline void cls_set_no_output_flow_control(struct channel_t *ch)
108 unsigned char lcrb = readb(&ch->ch_cls_uart->lcr);
109 unsigned char ier = readb(&ch->ch_cls_uart->ier);
110 unsigned char isr_fcr = 0;
113 * The Enhanced Register Set may only be accessed when
114 * the Line Control Register is set to 0xBFh.
116 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
118 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
120 /* Turn off IXON flow control, turn off CTS flow control */
121 isr_fcr |= (UART_EXAR654_EFR_ECB);
122 isr_fcr &= ~(UART_EXAR654_EFR_CTSDSR | UART_EXAR654_EFR_IXON);
124 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
126 /* Write old LCR value back out, which turns enhanced access off */
127 writeb(lcrb, &ch->ch_cls_uart->lcr);
130 * Disable interrupts for CTS flow, turn off interrupts for
131 * received XOFF chars
133 ier &= ~(UART_EXAR654_IER_CTSDSR);
134 ier &= ~(UART_EXAR654_IER_XOFF);
135 writeb(ier, &ch->ch_cls_uart->ier);
137 /* Set the usual FIFO values */
138 writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
140 writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_16 |
141 UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
142 &ch->ch_cls_uart->isr_fcr);
144 ch->ch_r_watermark = 0;
145 ch->ch_t_tlevel = 16;
146 ch->ch_r_tlevel = 16;
149 static inline void cls_set_rts_flow_control(struct channel_t *ch)
151 unsigned char lcrb = readb(&ch->ch_cls_uart->lcr);
152 unsigned char ier = readb(&ch->ch_cls_uart->ier);
153 unsigned char isr_fcr = 0;
156 * The Enhanced Register Set may only be accessed when
157 * the Line Control Register is set to 0xBFh.
159 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
161 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
163 /* Turn on RTS flow control, turn off IXOFF flow control */
164 isr_fcr |= (UART_EXAR654_EFR_ECB | UART_EXAR654_EFR_RTSDTR);
165 isr_fcr &= ~(UART_EXAR654_EFR_IXOFF);
167 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
169 /* Write old LCR value back out, which turns enhanced access off */
170 writeb(lcrb, &ch->ch_cls_uart->lcr);
172 /* Enable interrupts for RTS flow */
173 ier |= (UART_EXAR654_IER_RTSDTR);
174 writeb(ier, &ch->ch_cls_uart->ier);
176 /* Set the usual FIFO values */
177 writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
179 writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_56 |
180 UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
181 &ch->ch_cls_uart->isr_fcr);
183 ch->ch_r_watermark = 4;
187 static inline void cls_set_ixoff_flow_control(struct channel_t *ch)
189 unsigned char lcrb = readb(&ch->ch_cls_uart->lcr);
190 unsigned char ier = readb(&ch->ch_cls_uart->ier);
191 unsigned char isr_fcr = 0;
194 * The Enhanced Register Set may only be accessed when
195 * the Line Control Register is set to 0xBFh.
197 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
199 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
201 /* Turn on IXOFF flow control, turn off RTS flow control */
202 isr_fcr |= (UART_EXAR654_EFR_ECB | UART_EXAR654_EFR_IXOFF);
203 isr_fcr &= ~(UART_EXAR654_EFR_RTSDTR);
205 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
207 /* Now set our current start/stop chars while in enhanced mode */
208 writeb(ch->ch_startc, &ch->ch_cls_uart->mcr);
209 writeb(0, &ch->ch_cls_uart->lsr);
210 writeb(ch->ch_stopc, &ch->ch_cls_uart->msr);
211 writeb(0, &ch->ch_cls_uart->spr);
213 /* Write old LCR value back out, which turns enhanced access off */
214 writeb(lcrb, &ch->ch_cls_uart->lcr);
216 /* Disable interrupts for RTS flow */
217 ier &= ~(UART_EXAR654_IER_RTSDTR);
218 writeb(ier, &ch->ch_cls_uart->ier);
220 /* Set the usual FIFO values */
221 writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
223 writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_16 |
224 UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
225 &ch->ch_cls_uart->isr_fcr);
228 static inline void cls_set_no_input_flow_control(struct channel_t *ch)
230 unsigned char lcrb = readb(&ch->ch_cls_uart->lcr);
231 unsigned char ier = readb(&ch->ch_cls_uart->ier);
232 unsigned char isr_fcr = 0;
235 * The Enhanced Register Set may only be accessed when
236 * the Line Control Register is set to 0xBFh.
238 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
240 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
242 /* Turn off IXOFF flow control, turn off RTS flow control */
243 isr_fcr |= (UART_EXAR654_EFR_ECB);
244 isr_fcr &= ~(UART_EXAR654_EFR_RTSDTR | UART_EXAR654_EFR_IXOFF);
246 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
248 /* Write old LCR value back out, which turns enhanced access off */
249 writeb(lcrb, &ch->ch_cls_uart->lcr);
251 /* Disable interrupts for RTS flow */
252 ier &= ~(UART_EXAR654_IER_RTSDTR);
253 writeb(ier, &ch->ch_cls_uart->ier);
255 /* Set the usual FIFO values */
256 writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
258 writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_16 |
259 UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
260 &ch->ch_cls_uart->isr_fcr);
262 ch->ch_t_tlevel = 16;
263 ch->ch_r_tlevel = 16;
267 * Determines whether its time to shut off break condition.
269 * No locks are assumed to be held when calling this function.
270 * channel lock is held and released in this function.
272 static inline void cls_clear_break(struct channel_t *ch, int force)
279 spin_lock_irqsave(&ch->ch_lock, flags);
281 if (!ch->ch_stop_sending_break) {
282 spin_unlock_irqrestore(&ch->ch_lock, flags);
286 /* Turn break off, and unset some variables */
287 if (ch->ch_flags & CH_BREAK_SENDING) {
288 if (time_after(jiffies, ch->ch_stop_sending_break) || force) {
289 unsigned char temp = readb(&ch->ch_cls_uart->lcr);
291 writeb((temp & ~UART_LCR_SBC), &ch->ch_cls_uart->lcr);
292 ch->ch_flags &= ~(CH_BREAK_SENDING);
293 ch->ch_stop_sending_break = 0;
296 spin_unlock_irqrestore(&ch->ch_lock, flags);
299 static void cls_copy_data_from_uart_to_queue(struct channel_t *ch)
302 unsigned char linestatus = 0;
303 unsigned char error_mask = 0;
311 spin_lock_irqsave(&ch->ch_lock, flags);
313 head = ch->ch_r_head;
314 tail = ch->ch_r_tail;
316 qleft = tail - head - 1;
318 qleft += RQUEUEMASK + 1;
321 * Create a mask to determine whether we should
322 * insert the character (if any) into our queue.
324 if (ch->ch_c_iflag & IGNBRK)
325 error_mask |= UART_LSR_BI;
328 linestatus = readb(&ch->ch_cls_uart->lsr);
330 if (!(linestatus & (UART_LSR_DR)))
333 /* Discard character if we are ignoring the error mask. */
334 if (linestatus & error_mask) {
336 readb(&ch->ch_cls_uart->txrx);
341 * If our queue is full, we have no choice but to drop some
342 * data. The assumption is that HWFLOW or SWFLOW should have
343 * stopped things way way before we got to this point.
346 tail = (tail + 1) & RQUEUEMASK;
347 ch->ch_r_tail = tail;
348 ch->ch_err_overrun++;
352 ch->ch_equeue[head] = linestatus & (UART_LSR_BI | UART_LSR_PE
354 ch->ch_rqueue[head] = readb(&ch->ch_cls_uart->txrx);
358 if (ch->ch_equeue[head] & UART_LSR_PE)
360 if (ch->ch_equeue[head] & UART_LSR_BI)
362 if (ch->ch_equeue[head] & UART_LSR_FE)
365 head = (head + 1) & RQUEUEMASK;
369 ch->ch_r_head = head & RQUEUEMASK;
370 ch->ch_e_head = head & EQUEUEMASK;
372 spin_unlock_irqrestore(&ch->ch_lock, flags);
375 /* Make the UART raise any of the output signals we want up */
376 static void cls_assert_modem_signals(struct channel_t *ch)
385 if (ch->ch_flags & CH_LOOPBACK)
386 out |= UART_MCR_LOOP;
388 writeb(out, &ch->ch_cls_uart->mcr);
390 /* Give time for the UART to actually drop the signals */
391 usleep_range(10, 20);
394 static void cls_copy_data_from_queue_to_uart(struct channel_t *ch)
400 uint len_written = 0;
406 spin_lock_irqsave(&ch->ch_lock, flags);
408 if (ch->ch_w_tail == ch->ch_w_head)
411 /* If port is "stopped", don't send any data to the UART */
412 if ((ch->ch_flags & CH_FORCED_STOP) ||
413 (ch->ch_flags & CH_BREAK_SENDING))
416 if (!(ch->ch_flags & (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM)))
421 head = ch->ch_w_head & WQUEUEMASK;
422 tail = ch->ch_w_tail & WQUEUEMASK;
423 qlen = (head - tail) & WQUEUEMASK;
429 * If RTS Toggle mode is on, turn on RTS now if not already set,
430 * and make sure we get an event when the data transfer has
433 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
434 if (!(ch->ch_mostat & UART_MCR_RTS)) {
435 ch->ch_mostat |= (UART_MCR_RTS);
436 cls_assert_modem_signals(ch);
438 ch->ch_tun.un_flags |= (UN_EMPTY);
442 * If DTR Toggle mode is on, turn on DTR now if not already set,
443 * and make sure we get an event when the data transfer has
446 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
447 if (!(ch->ch_mostat & UART_MCR_DTR)) {
448 ch->ch_mostat |= (UART_MCR_DTR);
449 cls_assert_modem_signals(ch);
451 ch->ch_tun.un_flags |= (UN_EMPTY);
453 writeb(ch->ch_wqueue[ch->ch_w_tail], &ch->ch_cls_uart->txrx);
455 ch->ch_w_tail &= WQUEUEMASK;
462 ch->ch_flags &= ~(CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
465 spin_unlock_irqrestore(&ch->ch_lock, flags);
468 static void cls_parse_modem(struct channel_t *ch, unsigned char signals)
470 unsigned char msignals = signals;
477 * Do altpin switching. Altpin switches DCD and DSR.
478 * This prolly breaks DSRPACE, so we should be more clever here.
480 spin_lock_irqsave(&ch->ch_lock, flags);
481 if (ch->ch_digi.digi_flags & DIGI_ALTPIN) {
482 unsigned char mswap = signals;
484 if (mswap & UART_MSR_DDCD) {
485 msignals &= ~UART_MSR_DDCD;
486 msignals |= UART_MSR_DDSR;
488 if (mswap & UART_MSR_DDSR) {
489 msignals &= ~UART_MSR_DDSR;
490 msignals |= UART_MSR_DDCD;
492 if (mswap & UART_MSR_DCD) {
493 msignals &= ~UART_MSR_DCD;
494 msignals |= UART_MSR_DSR;
496 if (mswap & UART_MSR_DSR) {
497 msignals &= ~UART_MSR_DSR;
498 msignals |= UART_MSR_DCD;
501 spin_unlock_irqrestore(&ch->ch_lock, flags);
503 /* Scrub off lower bits. They signify delta's */
506 spin_lock_irqsave(&ch->ch_lock, flags);
507 if (msignals & UART_MSR_DCD)
508 ch->ch_mistat |= UART_MSR_DCD;
510 ch->ch_mistat &= ~UART_MSR_DCD;
512 if (msignals & UART_MSR_DSR)
513 ch->ch_mistat |= UART_MSR_DSR;
515 ch->ch_mistat &= ~UART_MSR_DSR;
517 if (msignals & UART_MSR_RI)
518 ch->ch_mistat |= UART_MSR_RI;
520 ch->ch_mistat &= ~UART_MSR_RI;
522 if (msignals & UART_MSR_CTS)
523 ch->ch_mistat |= UART_MSR_CTS;
525 ch->ch_mistat &= ~UART_MSR_CTS;
526 spin_unlock_irqrestore(&ch->ch_lock, flags);
529 /* Parse the ISR register for the specific port */
530 static inline void cls_parse_isr(struct dgnc_board *brd, uint port)
532 struct channel_t *ch;
533 unsigned char isr = 0;
537 * No need to verify board pointer, it was already
538 * verified in the interrupt routine.
541 if (port >= brd->nasync)
544 ch = brd->channels[port];
546 /* Here we try to figure out what caused the interrupt to happen */
548 isr = readb(&ch->ch_cls_uart->isr_fcr);
550 if (isr & UART_IIR_NO_INT)
553 /* Receive Interrupt pending */
554 if (isr & (UART_IIR_RDI | UART_IIR_RDI_TIMEOUT)) {
555 cls_copy_data_from_uart_to_queue(ch);
556 dgnc_check_queue_flow_control(ch);
559 /* Transmit Hold register empty pending */
560 if (isr & UART_IIR_THRI) {
561 spin_lock_irqsave(&ch->ch_lock, flags);
562 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
563 spin_unlock_irqrestore(&ch->ch_lock, flags);
564 cls_copy_data_from_queue_to_uart(ch);
567 cls_parse_modem(ch, readb(&ch->ch_cls_uart->msr));
571 /* Channel lock MUST be held before calling this function! */
572 static void cls_flush_uart_write(struct channel_t *ch)
577 writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_XMIT),
578 &ch->ch_cls_uart->isr_fcr);
580 /* Must use *delay family functions in atomic context */
583 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
586 /* Channel lock MUST be held before calling this function! */
587 static void cls_flush_uart_read(struct channel_t *ch)
593 * For complete POSIX compatibility, we should be purging the
594 * read FIFO in the UART here.
596 * However, clearing the read FIFO (UART_FCR_CLEAR_RCVR) also
597 * incorrectly flushes write data as well as just basically trashing the
600 * Presumably, this is a bug in this UART.
606 /* Send any/all changes to the line to the UART. */
607 static void cls_param(struct tty_struct *tty)
609 unsigned char lcr = 0;
610 unsigned char uart_lcr = 0;
611 unsigned char ier = 0;
612 unsigned char uart_ier = 0;
615 struct dgnc_board *bd;
616 struct channel_t *ch;
622 un = (struct un_t *)tty->driver_data;
634 /* If baud rate is zero, flush queues, and set mval to drop DTR. */
635 if ((ch->ch_c_cflag & (CBAUD)) == 0) {
643 cls_flush_uart_write(ch);
644 cls_flush_uart_read(ch);
646 /* The baudrate is B0 so all modem lines are to be dropped. */
647 ch->ch_flags |= (CH_BAUD0);
648 ch->ch_mostat &= ~(UART_MCR_RTS | UART_MCR_DTR);
649 cls_assert_modem_signals(ch);
652 } else if (ch->ch_custom_speed) {
653 baud = ch->ch_custom_speed;
654 /* Handle transition from B0 */
655 if (ch->ch_flags & CH_BAUD0) {
656 ch->ch_flags &= ~(CH_BAUD0);
659 * Bring back up RTS and DTR...
660 * Also handle RTS or DTR toggle if set.
662 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
663 ch->ch_mostat |= (UART_MCR_RTS);
664 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
665 ch->ch_mostat |= (UART_MCR_DTR);
672 ulong bauds[4][16] = {
676 600, 1200, 1800, 2400,
677 4800, 9600, 19200, 38400 },
678 { /* slowbaud & CBAUDEX */
679 0, 57600, 115200, 230400,
680 460800, 150, 200, 921600,
681 600, 1200, 1800, 2400,
682 4800, 9600, 19200, 38400 },
684 0, 57600, 76800, 115200,
685 131657, 153600, 230400, 460800,
686 921600, 1200, 1800, 2400,
687 4800, 9600, 19200, 38400 },
688 { /* fastbaud & CBAUDEX */
689 0, 57600, 115200, 230400,
690 460800, 150, 200, 921600,
691 600, 1200, 1800, 2400,
692 4800, 9600, 19200, 38400 }
696 * Only use the TXPrint baud rate if the terminal
699 if (!(ch->ch_tun.un_flags & UN_ISOPEN) &&
700 (un->un_type == DGNC_PRINT))
701 baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
703 baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
705 if (ch->ch_c_cflag & CBAUDEX)
708 if (ch->ch_digi.digi_flags & DIGI_FAST)
713 if ((iindex >= 0) && (iindex < 4) && (jindex >= 0) &&
715 baud = bauds[iindex][jindex];
723 /* Handle transition from B0 */
724 if (ch->ch_flags & CH_BAUD0) {
725 ch->ch_flags &= ~(CH_BAUD0);
728 * Bring back up RTS and DTR...
729 * Also handle RTS or DTR toggle if set.
731 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
732 ch->ch_mostat |= (UART_MCR_RTS);
733 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
734 ch->ch_mostat |= (UART_MCR_DTR);
738 if (ch->ch_c_cflag & PARENB)
739 lcr |= UART_LCR_PARITY;
741 if (!(ch->ch_c_cflag & PARODD))
742 lcr |= UART_LCR_EPAR;
745 if (ch->ch_c_cflag & CMSPAR)
746 lcr |= UART_LCR_SPAR;
749 if (ch->ch_c_cflag & CSTOPB)
750 lcr |= UART_LCR_STOP;
752 switch (ch->ch_c_cflag & CSIZE) {
754 lcr |= UART_LCR_WLEN5;
757 lcr |= UART_LCR_WLEN6;
760 lcr |= UART_LCR_WLEN7;
764 lcr |= UART_LCR_WLEN8;
768 uart_ier = readb(&ch->ch_cls_uart->ier);
770 uart_lcr = readb(&ch->ch_cls_uart->lcr);
775 quot = ch->ch_bd->bd_dividend / baud;
777 if (quot != 0 && ch->ch_old_baud != baud) {
778 ch->ch_old_baud = baud;
779 writeb(UART_LCR_DLAB, &ch->ch_cls_uart->lcr);
780 writeb((quot & 0xff), &ch->ch_cls_uart->txrx);
781 writeb((quot >> 8), &ch->ch_cls_uart->ier);
782 writeb(lcr, &ch->ch_cls_uart->lcr);
786 writeb(lcr, &ch->ch_cls_uart->lcr);
788 if (ch->ch_c_cflag & CREAD)
789 ier |= (UART_IER_RDI | UART_IER_RLSI);
791 ier &= ~(UART_IER_RDI | UART_IER_RLSI);
794 * Have the UART interrupt on modem signal changes ONLY when
795 * we are in hardware flow control mode, or CLOCAL/FORCEDCD is not set.
797 if ((ch->ch_digi.digi_flags & CTSPACE) ||
798 (ch->ch_digi.digi_flags & RTSPACE) ||
799 (ch->ch_c_cflag & CRTSCTS) ||
800 !(ch->ch_digi.digi_flags & DIGI_FORCEDCD) ||
801 !(ch->ch_c_cflag & CLOCAL))
804 ier &= ~UART_IER_MSI;
806 ier |= UART_IER_THRI;
809 writeb(ier, &ch->ch_cls_uart->ier);
811 if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
812 cls_set_cts_flow_control(ch);
813 } else if (ch->ch_c_iflag & IXON) {
814 if ((ch->ch_startc == _POSIX_VDISABLE) ||
815 (ch->ch_stopc == _POSIX_VDISABLE))
816 cls_set_no_output_flow_control(ch);
818 cls_set_ixon_flow_control(ch);
820 cls_set_no_output_flow_control(ch);
823 if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
824 cls_set_rts_flow_control(ch);
825 } else if (ch->ch_c_iflag & IXOFF) {
826 if ((ch->ch_startc == _POSIX_VDISABLE) ||
827 (ch->ch_stopc == _POSIX_VDISABLE))
828 cls_set_no_input_flow_control(ch);
830 cls_set_ixoff_flow_control(ch);
832 cls_set_no_input_flow_control(ch);
835 cls_assert_modem_signals(ch);
837 cls_parse_modem(ch, readb(&ch->ch_cls_uart->msr));
840 /* Board poller function. */
841 static void cls_tasklet(unsigned long data)
843 struct dgnc_board *bd = (struct dgnc_board *)data;
844 struct channel_t *ch;
853 spin_lock_irqsave(&bd->bd_lock, flags);
856 spin_unlock_irqrestore(&bd->bd_lock, flags);
859 * Do NOT allow the interrupt routine to read the intr registers
860 * Until we release this lock.
862 spin_lock_irqsave(&bd->bd_intr_lock, flags);
864 if ((state == BOARD_READY) && (ports > 0)) {
865 for (i = 0; i < ports; i++) {
866 ch = bd->channels[i];
869 * NOTE: Remember you CANNOT hold any channel
870 * locks when calling input.
871 * During input processing, its possible we
872 * will call ld, which might do callbacks back
878 * Channel lock is grabbed and then released
879 * inside this routine.
881 cls_copy_data_from_queue_to_uart(ch);
882 dgnc_wakeup_writes(ch);
887 * The timing check of turning off the break is done
888 * inside clear_break()
890 if (ch->ch_stop_sending_break)
891 cls_clear_break(ch, 0);
895 spin_unlock_irqrestore(&bd->bd_intr_lock, flags);
898 /* Classic specific interrupt handler. */
899 static irqreturn_t cls_intr(int irq, void *voidbrd)
901 struct dgnc_board *brd = voidbrd;
903 unsigned char poll_reg;
909 spin_lock_irqsave(&brd->bd_intr_lock, flags);
911 poll_reg = readb(brd->re_map_membase + UART_CLASSIC_POLL_ADDR_OFFSET);
913 spin_unlock_irqrestore(&brd->bd_intr_lock, flags);
917 for (i = 0; i < brd->nasync; i++)
918 cls_parse_isr(brd, i);
920 tasklet_schedule(&brd->helper_tasklet);
922 spin_unlock_irqrestore(&brd->bd_intr_lock, flags);
927 static void cls_disable_receiver(struct channel_t *ch)
929 unsigned char tmp = readb(&ch->ch_cls_uart->ier);
931 tmp &= ~(UART_IER_RDI);
932 writeb(tmp, &ch->ch_cls_uart->ier);
935 static void cls_enable_receiver(struct channel_t *ch)
937 unsigned char tmp = readb(&ch->ch_cls_uart->ier);
939 tmp |= (UART_IER_RDI);
940 writeb(tmp, &ch->ch_cls_uart->ier);
944 * This function basically goes to sleep for seconds, or until
945 * it gets signalled that the port has fully drained.
947 static int cls_drain(struct tty_struct *tty, uint seconds)
950 struct channel_t *ch;
956 un = (struct un_t *)tty->driver_data;
964 spin_lock_irqsave(&ch->ch_lock, flags);
965 un->un_flags |= UN_EMPTY;
966 spin_unlock_irqrestore(&ch->ch_lock, flags);
968 /* NOTE: Do something with time passed in. */
970 /* If ret is non-zero, user ctrl-c'ed us */
972 return wait_event_interruptible(un->un_flags_wait,
973 ((un->un_flags & UN_EMPTY) == 0));
976 static void cls_send_start_character(struct channel_t *ch)
981 if (ch->ch_startc != _POSIX_VDISABLE) {
983 writeb(ch->ch_startc, &ch->ch_cls_uart->txrx);
987 static void cls_send_stop_character(struct channel_t *ch)
992 if (ch->ch_stopc != _POSIX_VDISABLE) {
994 writeb(ch->ch_stopc, &ch->ch_cls_uart->txrx);
998 static void cls_uart_init(struct channel_t *ch)
1000 unsigned char lcrb = readb(&ch->ch_cls_uart->lcr);
1001 unsigned char isr_fcr = 0;
1003 writeb(0, &ch->ch_cls_uart->ier);
1006 * The Enhanced Register Set may only be accessed when
1007 * the Line Control Register is set to 0xBFh.
1009 writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
1011 isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
1013 /* Turn on Enhanced/Extended controls */
1014 isr_fcr |= (UART_EXAR654_EFR_ECB);
1016 writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
1018 /* Write old LCR value back out, which turns enhanced access off */
1019 writeb(lcrb, &ch->ch_cls_uart->lcr);
1021 /* Clear out UART and FIFO */
1022 readb(&ch->ch_cls_uart->txrx);
1024 writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
1025 &ch->ch_cls_uart->isr_fcr);
1026 usleep_range(10, 20);
1028 ch->ch_flags |= (CH_FIFO_ENABLED | CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1030 readb(&ch->ch_cls_uart->lsr);
1031 readb(&ch->ch_cls_uart->msr);
1034 static void cls_uart_off(struct channel_t *ch)
1036 writeb(0, &ch->ch_cls_uart->ier);
1040 * The channel lock MUST be held by the calling function.
1041 * Returns 0 is nothing left in the FIFO, returns 1 otherwise.
1043 static uint cls_get_uart_bytes_left(struct channel_t *ch)
1045 unsigned char left = 0;
1046 unsigned char lsr = 0;
1051 lsr = readb(&ch->ch_cls_uart->lsr);
1053 /* Determine whether the Transmitter is empty or not */
1054 if (!(lsr & UART_LSR_TEMT)) {
1055 if (ch->ch_flags & CH_TX_FIFO_EMPTY)
1056 tasklet_schedule(&ch->ch_bd->helper_tasklet);
1059 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1067 * Starts sending a break thru the UART.
1068 * The channel lock MUST be held by the calling function.
1070 static void cls_send_break(struct channel_t *ch, int msecs)
1075 /* If we receive a time of 0, this means turn off the break. */
1077 if (ch->ch_flags & CH_BREAK_SENDING) {
1078 unsigned char temp = readb(&ch->ch_cls_uart->lcr);
1080 writeb((temp & ~UART_LCR_SBC), &ch->ch_cls_uart->lcr);
1081 ch->ch_flags &= ~(CH_BREAK_SENDING);
1082 ch->ch_stop_sending_break = 0;
1088 * Set the time we should stop sending the break.
1089 * If we are already sending a break, toss away the existing
1090 * time to stop, and use this new value instead.
1092 ch->ch_stop_sending_break = jiffies + dgnc_jiffies_from_ms(msecs);
1094 /* Tell the UART to start sending the break */
1095 if (!(ch->ch_flags & CH_BREAK_SENDING)) {
1096 unsigned char temp = readb(&ch->ch_cls_uart->lcr);
1098 writeb((temp | UART_LCR_SBC), &ch->ch_cls_uart->lcr);
1099 ch->ch_flags |= (CH_BREAK_SENDING);
1104 * Sends a specific character as soon as possible to the UART,
1105 * jumping over any bytes that might be in the write queue.
1107 * The channel lock MUST be held by the calling function.
1109 static void cls_send_immediate_char(struct channel_t *ch, unsigned char c)
1114 writeb(c, &ch->ch_cls_uart->txrx);
1117 struct board_ops dgnc_cls_ops = {
1118 .tasklet = cls_tasklet,
1120 .uart_init = cls_uart_init,
1121 .uart_off = cls_uart_off,
1124 .assert_modem_signals = cls_assert_modem_signals,
1125 .flush_uart_write = cls_flush_uart_write,
1126 .flush_uart_read = cls_flush_uart_read,
1127 .disable_receiver = cls_disable_receiver,
1128 .enable_receiver = cls_enable_receiver,
1129 .send_break = cls_send_break,
1130 .send_start_character = cls_send_start_character,
1131 .send_stop_character = cls_send_stop_character,
1132 .copy_data_from_queue_to_uart = cls_copy_data_from_queue_to_uart,
1133 .get_uart_bytes_left = cls_get_uart_bytes_left,
1134 .send_immediate_char = cls_send_immediate_char