1 /* $Id: sab82532.c,v 1.1.1.1 2005/04/11 02:50:34 jack Exp $
2 * sab82532.c: ASYNC Driver for the SIEMENS SAB82532 DUSCC.
4 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
6 * Rewrote buffer handling to use CIRC(Circular Buffer) macros.
7 * Maxim Krasnyanskiy <maxk@qualcomm.com>
9 * Fixed to use tty_get_baud_rate, and to allow for arbitrary baud
10 * rates to be programmed into the UART. Also eliminated a lot of
11 * duplicated code in the console setup.
12 * Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/errno.h>
18 #include <linux/signal.h>
19 #include <linux/sched.h>
20 #include <linux/timer.h>
21 #include <linux/interrupt.h>
22 #include <linux/tty.h>
23 #include <linux/tty_flip.h>
24 #include <linux/serial.h>
25 #include <linux/serialP.h>
26 #include <linux/serial_reg.h>
27 #include <linux/console.h>
28 #include <linux/major.h>
29 #include <linux/string.h>
30 #include <linux/fcntl.h>
31 #include <linux/ptrace.h>
32 #include <linux/ioport.h>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
38 #include <asm/sab82532.h>
39 #include <asm/uaccess.h>
43 #include "sunserial.h"
45 static DECLARE_TASK_QUEUE(tq_serial);
47 /* This is (one of many) a special gross hack to allow SU and
48 * SAB serials to co-exist on the same machine. -DaveM
51 #define SERIAL_BH AURORA_BH
53 static struct tty_driver serial_driver, callout_driver;
54 static int sab82532_refcount;
56 /* number of characters left in xmit buffer before we ask for more */
57 #define WAKEUP_CHARS 256
59 #undef SERIAL_PARANOIA_CHECK
60 #define SERIAL_DO_RESTART
62 /* Set of debugging defines */
63 #undef SERIAL_DEBUG_OPEN
64 #undef SERIAL_DEBUG_FLOW
65 #undef SERIAL_DEBUG_MODEM
66 #undef SERIAL_DEBUG_WAIT_UNTIL_SENT
67 #undef SERIAL_DEBUG_SEND_BREAK
68 #undef SERIAL_DEBUG_INTR
69 #undef SERIAL_DEBUG_FIFO
70 #define SERIAL_DEBUG_OVERFLOW 1
72 /* Trace things on serial device, useful for console debugging: */
73 #undef SERIAL_LOG_DEVICE
75 #ifdef SERIAL_LOG_DEVICE
76 static void dprint_init(int tty);
79 static void change_speed(struct sab82532 *info);
80 static void sab82532_wait_until_sent(struct tty_struct *tty, int timeout);
83 * This assumes you have a 29.4912 MHz clock for your UART.
85 #define BASE_BAUD ( 29491200 / 16 )
87 static struct sab82532 *sab82532_chain = 0;
88 static struct tty_struct *sab82532_table[NR_PORTS];
89 static struct termios *sab82532_termios[NR_PORTS];
90 static struct termios *sab82532_termios_locked[NR_PORTS];
93 #undef CONFIG_SERIAL_CONSOLE
96 #ifdef CONFIG_SERIAL_CONSOLE
97 extern int serial_console;
98 static struct console sab82532_console;
99 static int sab82532_console_init(void);
100 static void batten_down_hatches(struct sab82532 *info);
104 #define MIN(a,b) ((a) < (b) ? (a) : (b))
107 static char *sab82532_version[16] = {
108 "V1.0", "V2.0", "V3.2", "V(0x03)",
109 "V(0x04)", "V(0x05)", "V(0x06)", "V(0x07)",
110 "V(0x08)", "V(0x09)", "V(0x0a)", "V(0x0b)",
111 "V(0x0c)", "V(0x0d)", "V(0x0e)", "V(0x0f)"
113 static char serial_version[16];
116 * tmp_buf is used as a temporary buffer by sab82532_write. We need to
117 * lock it in case the copy_from_user blocks while swapping in a page,
118 * and some other program tries to do a serial write at the same time.
119 * Since the lock will only come under contention when the system is
120 * swapping and available memory is low, it makes sense to share one
121 * buffer across all the serial ports, since it significantly saves
122 * memory if large numbers of serial ports are open.
124 static unsigned char *tmp_buf = 0;
125 static DECLARE_MUTEX(tmp_buf_sem);
127 static inline int serial_paranoia_check(struct sab82532 *info,
128 kdev_t device, const char *routine)
130 #ifdef SERIAL_PARANOIA_CHECK
131 static const char *badmagic =
132 "Warning: bad magic number for serial struct (%s) in %s\n";
133 static const char *badinfo =
134 "Warning: null sab82532 for (%s) in %s\n";
137 printk(badinfo, kdevname(device), routine);
140 if (info->magic != SERIAL_MAGIC) {
141 printk(badmagic, kdevname(device), routine);
149 * This is used to figure out the divisor speeds.
151 * The formula is: Baud = BASE_BAUD / ((N + 1) * (1 << M)),
153 * with 0 <= N < 64 and 0 <= M < 16
155 * 12-Oct-2001 - Replaced table driven approach with code written by
156 * Theodore Ts'o <tytso@alum.mit.edu> which exactly replicates the
157 * table. (Modulo bugs for the 307200 and 61440 baud rates, which
158 * were clearly incorrectly calculated in the original table. This is
159 * why tables filled with magic constants are evil.)
162 static void calc_ebrg(int baud, int *n_ret, int *m_ret)
173 * We scale numbers by 10 so that we get better accuracy
174 * without having to use floating point. Here we increment m
175 * until n is within the valid range.
177 n = (BASE_BAUD*10) / baud;
185 * We try very hard to avoid speeds with M == 0 since they may
186 * not work correctly for XTAL frequences above 10 MHz.
188 if ((m == 0) && ((n & 1) == 0)) {
196 #define SAB82532_MAX_TEC_TIMEOUT 200000 /* 1 character time (at 50 baud) */
197 #define SAB82532_MAX_CEC_TIMEOUT 50000 /* 2.5 TX CLKs (at 50 baud) */
199 static __inline__ void sab82532_tec_wait(struct sab82532 *info)
201 int timeout = info->tec_timeout;
203 while ((readb(&info->regs->r.star) & SAB82532_STAR_TEC) && --timeout)
207 static __inline__ void sab82532_cec_wait(struct sab82532 *info)
209 int timeout = info->cec_timeout;
211 while ((readb(&info->regs->r.star) & SAB82532_STAR_CEC) && --timeout)
215 static __inline__ void sab82532_start_tx(struct sab82532 *info)
220 save_flags(flags); cli();
222 if (info->xmit.head == info->xmit.tail)
225 if (!test_bit(SAB82532_XPR, &info->irqflags))
228 info->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS);
229 writeb(info->interrupt_mask1, &info->regs->w.imr1);
230 clear_bit(SAB82532_ALLS, &info->irqflags);
232 clear_bit(SAB82532_XPR, &info->irqflags);
233 for (i = 0; i < info->xmit_fifo_size; i++) {
234 writeb(info->xmit.buf[info->xmit.tail],
235 &info->regs->w.xfifo[i]);
236 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
238 if (info->xmit.head == info->xmit.tail)
242 /* Issue a Transmit Frame command. */
243 sab82532_cec_wait(info);
244 writeb(SAB82532_CMDR_XF, &info->regs->w.cmdr);
247 restore_flags(flags);
252 * ------------------------------------------------------------
253 * sab82532_stop() and sab82532_start()
255 * This routines are called before setting or resetting tty->stopped.
256 * They enable or disable transmitter interrupts, as necessary.
257 * ------------------------------------------------------------
259 static void sab82532_stop(struct tty_struct *tty)
261 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
264 if (serial_paranoia_check(info, tty->device, "sab82532_stop"))
267 save_flags(flags); cli();
268 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
269 writeb(info->interrupt_mask1, &info->regs->w.imr1);
270 restore_flags(flags);
273 static void sab82532_start(struct tty_struct *tty)
275 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
278 if (serial_paranoia_check(info, tty->device, "sab82532_start"))
281 save_flags(flags); cli();
282 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
283 writeb(info->interrupt_mask1, &info->regs->w.imr1);
284 sab82532_start_tx(info);
285 restore_flags(flags);
289 * ----------------------------------------------------------------------
291 * Here starts the interrupt handling routines. All of the following
292 * subroutines are declared as inline and are folded into
293 * sab82532_interrupt(). They were separated out for readability's sake.
295 * Note: sab82532_interrupt() is a "fast" interrupt, which means that it
296 * runs with interrupts turned off. People who may want to modify
297 * sab82532_interrupt() should try to keep the interrupt handler as fast as
298 * possible. After you are done making modifications, it is not a bad
301 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
303 * and look at the resulting assemble code in serial.s.
305 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
306 * -----------------------------------------------------------------------
310 * This routine is used by the interrupt handler to schedule
311 * processing in the software interrupt portion of the driver.
313 static void sab82532_sched_event(struct sab82532 *info, int event)
315 info->event |= 1 << event;
316 queue_task(&info->tqueue, &tq_serial);
320 static void receive_chars(struct sab82532 *info,
321 union sab82532_irq_status *stat)
323 struct tty_struct *tty = info->tty;
324 unsigned char buf[32];
325 unsigned char status;
329 /* Read number of BYTES (Character + Status) available. */
330 if (stat->sreg.isr0 & SAB82532_ISR0_RPF) {
331 count = info->recv_fifo_size;
335 if (stat->sreg.isr0 & SAB82532_ISR0_TCD) {
336 count = readb(&info->regs->r.rbcl) & (info->recv_fifo_size - 1);
340 /* Issue a FIFO read command in case we where idle. */
341 if (stat->sreg.isr0 & SAB82532_ISR0_TIME) {
342 sab82532_cec_wait(info);
343 writeb(SAB82532_CMDR_RFRD, &info->regs->w.cmdr);
347 if (stat->sreg.isr0 & SAB82532_ISR0_RFO) {
348 #ifdef SERIAL_DEBUG_OVERFLOW
349 printk("sab82532: receive_chars: RFO");
355 for (i = 0; i < count; i++)
356 buf[i] = readb(&info->regs->r.rfifo[i]);
358 /* Issue Receive Message Complete command. */
360 sab82532_cec_wait(info);
361 writeb(SAB82532_CMDR_RMC, &info->regs->w.cmdr);
367 for (i = 0; i < count; ) {
368 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
369 #ifdef SERIAL_DEBUG_OVERFLOW
370 printk("sab82532: receive_chars: tty overrun\n");
372 info->icount.buf_overrun++;
377 *tty->flip.char_buf_ptr++ = buf[i++];
381 #ifdef SERIAL_DEBUG_INTR
382 printk("DR%02x:%02x...", (unsigned char)*(tty->flip.char_buf_ptr - 1), status);
385 if (status & SAB82532_RSTAT_PE) {
386 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
387 info->icount.parity++;
388 } else if (status & SAB82532_RSTAT_FE) {
389 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
390 info->icount.frame++;
393 *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
396 queue_task(&tty->flip.tqueue, &tq_timer);
399 static void transmit_chars(struct sab82532 *info,
400 union sab82532_irq_status *stat)
404 if (stat->sreg.isr1 & SAB82532_ISR1_ALLS) {
405 info->interrupt_mask1 |= SAB82532_IMR1_ALLS;
406 writeb(info->interrupt_mask1, &info->regs->w.imr1);
407 set_bit(SAB82532_ALLS, &info->irqflags);
410 if (!(stat->sreg.isr1 & SAB82532_ISR1_XPR))
413 if (!(readb(&info->regs->r.star) & SAB82532_STAR_XFW)) {
414 #ifdef SERIAL_DEBUG_FIFO
415 printk("%s: XPR, but no XFW (?)\n", __FUNCTION__);
420 set_bit(SAB82532_XPR, &info->irqflags);
423 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
424 writeb(info->interrupt_mask1, &info->regs->w.imr1);
428 if ((info->xmit.head == info->xmit.tail) ||
429 info->tty->stopped || info->tty->hw_stopped) {
430 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
431 writeb(info->interrupt_mask1, &info->regs->w.imr1);
435 info->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS);
436 writeb(info->interrupt_mask1, &info->regs->w.imr1);
437 clear_bit(SAB82532_ALLS, &info->irqflags);
439 /* Stuff 32 bytes into Transmit FIFO. */
440 clear_bit(SAB82532_XPR, &info->irqflags);
441 for (i = 0; i < info->xmit_fifo_size; i++) {
442 writeb(info->xmit.buf[info->xmit.tail],
443 &info->regs->w.xfifo[i]);
444 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
446 if (info->xmit.head == info->xmit.tail)
450 /* Issue a Transmit Frame command. */
451 sab82532_cec_wait(info);
452 writeb(SAB82532_CMDR_XF, &info->regs->w.cmdr);
454 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
455 sab82532_sched_event(info, RS_EVENT_WRITE_WAKEUP);
457 #ifdef SERIAL_DEBUG_INTR
462 static void check_status(struct sab82532 *info,
463 union sab82532_irq_status *stat)
465 struct tty_struct *tty = info->tty;
466 int modem_change = 0;
468 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
469 #ifdef CONFIG_SERIAL_CONSOLE
470 if (info->is_console) {
471 batten_down_hatches(info);
475 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
476 info->icount.buf_overrun++;
480 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
481 *tty->flip.char_buf_ptr++ = 0;
488 if (stat->sreg.isr0 & SAB82532_ISR0_RFO) {
489 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
490 info->icount.buf_overrun++;
494 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
495 *tty->flip.char_buf_ptr++ = 0;
496 info->icount.overrun++;
500 if (stat->sreg.isr0 & SAB82532_ISR0_CDSC) {
501 info->dcd = (readb(&info->regs->r.vstr) & SAB82532_VSTR_CD) ? 0 : 1;
504 #ifdef SERIAL_DEBUG_MODEM
505 printk("DCD change: %d\n", info->icount.dcd);
508 if (stat->sreg.isr1 & SAB82532_ISR1_CSC) {
509 info->cts = readb(&info->regs->r.star) & SAB82532_STAR_CTS;
512 #ifdef SERIAL_DEBUG_MODEM
513 printk("CTS change: %d, CTS %s\n", info->icount.cts, info->cts ? "on" : "off");
516 if ((readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ^ info->dsr) {
517 info->dsr = (readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ? 0 : 1;
520 #ifdef SERIAL_DEBUG_MODEM
521 printk("DSR change: %d\n", info->icount.dsr);
525 wake_up_interruptible(&info->delta_msr_wait);
527 if ((info->flags & ASYNC_CHECK_CD) &&
528 (stat->sreg.isr0 & SAB82532_ISR0_CDSC)) {
530 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
531 printk("ttys%d CD now %s...", info->line,
532 (info->dcd) ? "on" : "off");
536 wake_up_interruptible(&info->open_wait);
537 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
538 (info->flags & ASYNC_CALLOUT_NOHUP))) {
540 #ifdef SERIAL_DEBUG_OPEN
541 printk("scheduling hangup...");
544 if (schedule_task(&info->tqueue_hangup) == 0)
549 if (info->flags & ASYNC_CTS_FLOW) {
550 if (info->tty->hw_stopped) {
553 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
554 printk("CTS tx start...");
556 info->tty->hw_stopped = 0;
557 sab82532_sched_event(info,
558 RS_EVENT_WRITE_WAKEUP);
559 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
560 writeb(info->interrupt_mask1, &info->regs->w.imr1);
561 sab82532_start_tx(info);
566 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
567 printk("CTS tx stop...");
569 info->tty->hw_stopped = 1;
576 * This is the serial driver's generic interrupt routine
578 static void sab82532_interrupt(int irq, void *dev_id, struct pt_regs *regs)
580 struct sab82532 *info = dev_id;
581 union sab82532_irq_status status;
583 #ifdef SERIAL_DEBUG_INTR
584 printk("sab82532_interrupt(%d)...", irq);
588 if (readb(&info->regs->r.gis) & SAB82532_GIS_ISA0)
589 status.sreg.isr0 = readb(&info->regs->r.isr0);
590 if (readb(&info->regs->r.gis) & SAB82532_GIS_ISA1)
591 status.sreg.isr1 = readb(&info->regs->r.isr1);
593 #ifdef SERIAL_DEBUG_INTR
594 printk("%d<%02x.%02x>", info->line,
595 status.sreg.isr0, status.sreg.isr1);
601 if (status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
602 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF))
603 receive_chars(info, &status);
604 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
605 (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
606 check_status(info, &status);
607 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
608 transmit_chars(info, &status);
613 if (readb(&info->regs->r.gis) & SAB82532_GIS_ISB0)
614 status.sreg.isr0 = readb(&info->regs->r.isr0);
615 if (readb(&info->regs->r.gis) & SAB82532_GIS_ISB1)
616 status.sreg.isr1 = readb(&info->regs->r.isr1);
618 #ifdef SERIAL_DEBUG_INTR
619 printk("%d<%02x.%02x>", info->line,
620 status.sreg.isr0, status.sreg.isr1);
626 if (status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
627 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF))
628 receive_chars(info, &status);
629 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
630 (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
631 check_status(info, &status);
632 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
633 transmit_chars(info, &status);
637 #ifdef SERIAL_DEBUG_INTR
643 * -------------------------------------------------------------------
644 * Here ends the serial interrupt routines.
645 * -------------------------------------------------------------------
649 * This routine is used to handle the "bottom half" processing for the
650 * serial driver, known also the "software interrupt" processing.
651 * This processing is done at the kernel interrupt level, after the
652 * sab82532_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
653 * is where time-consuming activities which can not be done in the
654 * interrupt driver proper are done; the interrupt driver schedules
655 * them using sab82532_sched_event(), and they get done here.
657 static void do_serial_bh(void)
659 run_task_queue(&tq_serial);
662 static void do_softint(void *private_)
664 struct sab82532 *info = (struct sab82532 *)private_;
665 struct tty_struct *tty;
671 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
672 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
673 tty->ldisc.write_wakeup)
674 (tty->ldisc.write_wakeup)(tty);
675 wake_up_interruptible(&tty->write_wait);
680 * This routine is called from the scheduler tqueue when the interrupt
681 * routine has signalled that a hangup has occurred. The path of
682 * hangup processing is:
684 * serial interrupt routine -> (scheduler tqueue) ->
685 * do_serial_hangup() -> tty->hangup() -> sab82532_hangup()
688 static void do_serial_hangup(void *private_)
690 struct sab82532 *info = (struct sab82532 *) private_;
691 struct tty_struct *tty;
700 sab82532_init_line(struct sab82532 *info)
702 unsigned char stat, tmp;
705 * Wait for any commands or immediate characters
707 sab82532_cec_wait(info);
708 sab82532_tec_wait(info);
711 * Clear the FIFO buffers.
713 writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
714 sab82532_cec_wait(info);
715 writeb(SAB82532_CMDR_XRES, &info->regs->w.cmdr);
718 * Clear the interrupt registers.
720 stat = readb(&info->regs->r.isr0);
721 stat = readb(&info->regs->r.isr1);
724 * Now, initialize the UART
726 writeb(0, &info->regs->w.ccr0); /* power-down */
727 writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
728 SAB82532_CCR0_SM_ASYNC, &info->regs->w.ccr0);
729 writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &info->regs->w.ccr1);
730 writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
731 SAB82532_CCR2_TOE, &info->regs->w.ccr2);
732 writeb(0, &info->regs->w.ccr3);
733 writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &info->regs->w.ccr4);
734 writeb(SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
735 SAB82532_MODE_RAC, &info->regs->w.mode);
736 writeb(SAB82532_RFC_DPS | SAB82532_RFC_RFDF, &info->regs->w.rfc);
737 switch (info->recv_fifo_size) {
739 tmp = readb(&info->regs->w.rfc);
740 tmp |= SAB82532_RFC_RFTH_1;
741 writeb(tmp, &info->regs->w.rfc);
744 tmp = readb(&info->regs->w.rfc);
745 tmp |= SAB82532_RFC_RFTH_4;
746 writeb(tmp, &info->regs->w.rfc);
749 tmp = readb(&info->regs->w.rfc);
750 tmp |= SAB82532_RFC_RFTH_16;
751 writeb(tmp, &info->regs->w.rfc);
754 info->recv_fifo_size = 32;
757 tmp = readb(&info->regs->w.rfc);
758 tmp |= SAB82532_RFC_RFTH_32;
759 writeb(tmp, &info->regs->w.rfc);
762 tmp = readb(&info->regs->rw.ccr0);
763 tmp |= SAB82532_CCR0_PU; /* power-up */
764 writeb(tmp, &info->regs->rw.ccr0);
767 static int startup(struct sab82532 *info)
773 page = get_free_page(GFP_KERNEL);
777 save_flags(flags); cli();
779 if (info->flags & ASYNC_INITIALIZED) {
786 set_bit(TTY_IO_ERROR, &info->tty->flags);
794 info->xmit.buf = (unsigned char *)page;
796 #ifdef SERIAL_DEBUG_OPEN
797 printk("starting up serial port %d...", info->line);
801 * Initialize the Hardware
803 sab82532_init_line(info);
805 if (info->tty->termios->c_cflag & CBAUD) {
808 tmp = readb(&info->regs->rw.mode);
809 tmp &= ~(SAB82532_MODE_FRTS);
810 tmp |= SAB82532_MODE_RTS;
811 writeb(tmp, &info->regs->rw.mode);
813 tmp = readb(&info->regs->rw.pvr);
814 tmp &= ~(info->pvr_dtr_bit);
815 writeb(tmp, &info->regs->rw.pvr);
819 * Finally, enable interrupts
821 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
823 writeb(info->interrupt_mask0, &info->regs->w.imr0);
824 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
825 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
826 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
828 writeb(info->interrupt_mask1, &info->regs->w.imr1);
829 set_bit(SAB82532_ALLS, &info->irqflags);
832 clear_bit(TTY_IO_ERROR, &info->tty->flags);
833 info->xmit.head = info->xmit.tail = 0;
835 set_bit(SAB82532_XPR, &info->irqflags);
838 * and set the speed of the serial port
842 info->flags |= ASYNC_INITIALIZED;
843 restore_flags(flags);
847 restore_flags(flags);
852 * This routine will shutdown a serial port; interrupts are disabled, and
853 * DTR is dropped if the hangup on close termio flag is on.
855 static void shutdown(struct sab82532 *info)
860 if (!(info->flags & ASYNC_INITIALIZED))
863 #ifdef SERIAL_DEBUG_OPEN
864 printk("Shutting down serial port %d...", info->line);
867 save_flags(flags); cli(); /* Disable interrupts */
870 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
871 * here so the queue might never be waken up
873 wake_up_interruptible(&info->delta_msr_wait);
875 if (info->xmit.buf) {
876 free_page((unsigned long)info->xmit.buf);
880 #ifdef CONFIG_SERIAL_CONSOLE
881 if (info->is_console) {
882 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
883 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
884 writeb(info->interrupt_mask0, &info->regs->w.imr0);
885 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
886 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
887 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
889 writeb(info->interrupt_mask1, &info->regs->w.imr1);
891 set_bit(TTY_IO_ERROR, &info->tty->flags);
892 info->flags &= ~ASYNC_INITIALIZED;
893 restore_flags(flags);
898 /* Disable Interrupts */
899 info->interrupt_mask0 = 0xff;
900 writeb(info->interrupt_mask0, &info->regs->w.imr0);
901 info->interrupt_mask1 = 0xff;
902 writeb(info->interrupt_mask1, &info->regs->w.imr1);
904 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
905 tmp = readb(&info->regs->r.mode);
906 tmp |= (SAB82532_MODE_FRTS | SAB82532_MODE_RTS);
907 writeb(tmp, &info->regs->rw.mode);
908 writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit,
909 &info->regs->rw.pvr);
912 /* Disable break condition */
913 tmp = readb(&info->regs->rw.dafo);
914 tmp &= ~(SAB82532_DAFO_XBRK);
915 writeb(tmp, &info->regs->rw.dafo);
917 /* Disable Receiver */
918 tmp = readb(&info->regs->rw.mode);
919 tmp &= ~(SAB82532_MODE_RAC);
920 writeb(tmp, &info->regs->rw.mode);
923 tmp = readb(&info->regs->rw.ccr0);
924 tmp &= ~(SAB82532_CCR0_PU);
925 writeb(tmp, &info->regs->rw.ccr0);
928 set_bit(TTY_IO_ERROR, &info->tty->flags);
930 info->flags &= ~ASYNC_INITIALIZED;
931 restore_flags(flags);
935 * This routine is called to set the UART divisor registers to match
936 * the specified baud rate for a serial port.
938 static void change_speed(struct sab82532 *info)
946 if (!info->tty || !info->tty->termios)
948 cflag = info->tty->termios->c_cflag;
950 /* Byte size and parity */
951 switch (cflag & CSIZE) {
952 case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
953 case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
954 case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
955 case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
956 /* Never happens, but GCC is too dumb to figure it out */
957 default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
960 if (cflag & CSTOPB) {
961 dafo |= SAB82532_DAFO_STOP;
965 if (cflag & PARENB) {
966 dafo |= SAB82532_DAFO_PARE;
970 if (cflag & PARODD) {
973 dafo |= SAB82532_DAFO_PAR_MARK;
976 dafo |= SAB82532_DAFO_PAR_ODD;
980 dafo |= SAB82532_DAFO_PAR_SPACE;
983 dafo |= SAB82532_DAFO_PAR_EVEN;
986 /* Determine EBRG values based on baud rate */
987 info->baud = tty_get_baud_rate(info->tty);
988 calc_ebrg(info->baud, &n, &m);
993 info->timeout = (info->xmit_fifo_size * HZ * bits) / info->baud;
994 info->tec_timeout = (10 * 1000000) / info->baud;
995 info->cec_timeout = info->tec_timeout >> 2;
998 info->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
999 info->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
1001 info->timeout += HZ / 50; /* Add .02 seconds of slop */
1003 /* CTS flow control flags */
1004 if (cflag & CRTSCTS)
1005 info->flags |= ASYNC_CTS_FLOW;
1007 info->flags &= ~(ASYNC_CTS_FLOW);
1010 info->flags &= ~(ASYNC_CHECK_CD);
1012 info->flags |= ASYNC_CHECK_CD;
1014 info->tty->hw_stopped = 0;
1017 * Set up parity check flag
1018 * XXX: not implemented, yet.
1020 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1023 * Characters to ignore
1024 * XXX: not implemented, yet.
1028 * !!! ignore all characters if CREAD is not set
1029 * XXX: not implemented, yet.
1031 if ((cflag & CREAD) == 0)
1032 info->ignore_status_mask |= SAB82532_ISR0_RPF |
1036 save_flags(flags); cli();
1037 sab82532_cec_wait(info);
1038 sab82532_tec_wait(info);
1039 writeb(dafo, &info->regs->w.dafo);
1040 writeb(ebrg & 0xff, &info->regs->w.bgr);
1041 writeb(readb(&info->regs->rw.ccr2) & ~(0xc0), &info->regs->rw.ccr2);
1042 writeb(readb(&info->regs->rw.ccr2) | ((ebrg >> 2) & 0xc0), &info->regs->rw.ccr2);
1043 if (info->flags & ASYNC_CTS_FLOW) {
1044 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1045 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1046 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FCTS), &info->regs->rw.mode);
1047 info->interrupt_mask1 &= ~(SAB82532_IMR1_CSC);
1048 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1050 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1051 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1052 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FCTS, &info->regs->rw.mode);
1053 info->interrupt_mask1 |= SAB82532_IMR1_CSC;
1054 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1056 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RAC, &info->regs->rw.mode);
1057 restore_flags(flags);
1060 static void sab82532_put_char(struct tty_struct *tty, unsigned char ch)
1062 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1063 unsigned long flags;
1065 if (serial_paranoia_check(info, tty->device, "sab82532_put_char"))
1068 if (!tty || !info->xmit.buf)
1071 save_flags(flags); cli();
1072 if (!CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)) {
1073 restore_flags(flags);
1077 info->xmit.buf[info->xmit.head] = ch;
1078 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
1079 restore_flags(flags);
1082 static void sab82532_flush_chars(struct tty_struct *tty)
1084 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1085 unsigned long flags;
1087 if (serial_paranoia_check(info, tty->device, "sab82532_flush_chars"))
1090 if ((info->xmit.head == info->xmit.tail) ||
1091 tty->stopped || tty->hw_stopped || !info->xmit.buf)
1094 save_flags(flags); cli();
1095 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1096 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1097 sab82532_start_tx(info);
1098 restore_flags(flags);
1101 static int sab82532_write(struct tty_struct * tty, int from_user,
1102 const unsigned char *buf, int count)
1105 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1106 unsigned long flags;
1108 if (serial_paranoia_check(info, tty->device, "sab82532_write"))
1111 if (!tty || !info->xmit.buf || !tmp_buf)
1119 c = CIRC_SPACE_TO_END(info->xmit.head,
1127 c -= copy_from_user(tmp_buf, buf, c);
1134 c1 = CIRC_SPACE_TO_END(info->xmit.head,
1139 memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
1140 info->xmit.head = (info->xmit.head + c) & (SERIAL_XMIT_SIZE-1);
1141 restore_flags(flags);
1150 c = CIRC_SPACE_TO_END(info->xmit.head,
1157 memcpy(info->xmit.buf + info->xmit.head, buf, c);
1158 info->xmit.head = (info->xmit.head + c) & (SERIAL_XMIT_SIZE-1);
1163 restore_flags(flags);
1166 if ((info->xmit.head != info->xmit.tail) &&
1167 !tty->stopped && !tty->hw_stopped) {
1168 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1169 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1170 sab82532_start_tx(info);
1173 restore_flags(flags);
1177 static int sab82532_write_room(struct tty_struct *tty)
1179 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1181 if (serial_paranoia_check(info, tty->device, "sab82532_write_room"))
1184 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1187 static int sab82532_chars_in_buffer(struct tty_struct *tty)
1189 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1191 if (serial_paranoia_check(info, tty->device, "sab82532_chars_in_buffer"))
1194 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1197 static void sab82532_flush_buffer(struct tty_struct *tty)
1199 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1200 unsigned long flags;
1202 if (serial_paranoia_check(info, tty->device, "sab82532_flush_buffer"))
1205 save_flags(flags); cli();
1206 info->xmit.head = info->xmit.tail = 0;
1207 restore_flags(flags);
1209 wake_up_interruptible(&tty->write_wait);
1210 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1211 tty->ldisc.write_wakeup)
1212 (tty->ldisc.write_wakeup)(tty);
1216 * This function is used to send a high-priority XON/XOFF character to
1219 static void sab82532_send_xchar(struct tty_struct *tty, char ch)
1221 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1222 unsigned long flags;
1224 if (serial_paranoia_check(info, tty->device, "sab82532_send_xchar"))
1227 save_flags(flags); cli();
1228 sab82532_tec_wait(info);
1229 writeb(ch, &info->regs->w.tic);
1230 restore_flags(flags);
1234 * ------------------------------------------------------------
1235 * sab82532_throttle()
1237 * This routine is called by the upper-layer tty layer to signal that
1238 * incoming characters should be throttled.
1239 * ------------------------------------------------------------
1241 static void sab82532_throttle(struct tty_struct * tty)
1243 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1244 #ifdef SERIAL_DEBUG_THROTTLE
1247 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1248 tty->ldisc.chars_in_buffer(tty));
1251 if (serial_paranoia_check(info, tty->device, "sab82532_throttle"))
1255 sab82532_send_xchar(tty, STOP_CHAR(tty));
1257 if (tty->termios->c_cflag & CRTSCTS) {
1258 u8 mode = readb(&info->regs->r.mode);
1259 mode &= ~(SAB82532_MODE_FRTS | SAB82532_MODE_RTS);
1260 writeb(mode, &info->regs->w.mode);
1264 static void sab82532_unthrottle(struct tty_struct * tty)
1266 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1267 #ifdef SERIAL_DEBUG_THROTTLE
1270 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1271 tty->ldisc.chars_in_buffer(tty));
1274 if (serial_paranoia_check(info, tty->device, "sab82532_unthrottle"))
1281 sab82532_send_xchar(tty, START_CHAR(tty));
1284 if (tty->termios->c_cflag & CRTSCTS) {
1285 u8 mode = readb(&info->regs->r.mode);
1286 mode &= ~(SAB82532_MODE_RTS);
1287 mode |= SAB82532_MODE_FRTS;
1288 writeb(mode, &info->regs->w.mode);
1293 * ------------------------------------------------------------
1294 * sab82532_ioctl() and friends
1295 * ------------------------------------------------------------
1298 static int get_serial_info(struct sab82532 *info,
1299 struct serial_struct *retinfo)
1301 struct serial_struct tmp;
1305 memset(&tmp, 0, sizeof(tmp));
1306 tmp.type = info->type;
1307 tmp.line = info->line;
1308 tmp.port = (unsigned long)info->regs;
1309 tmp.irq = info->irq;
1310 tmp.flags = info->flags;
1311 tmp.xmit_fifo_size = info->xmit_fifo_size;
1312 tmp.baud_base = info->baud_base;
1313 tmp.close_delay = info->close_delay;
1314 tmp.closing_wait = info->closing_wait;
1315 tmp.custom_divisor = info->custom_divisor;
1317 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1322 static int set_serial_info(struct sab82532 *info,
1323 struct serial_struct *new_info)
1330 * get_lsr_info - get line status register info
1332 * Purpose: Let user call ioctl() to get info when the UART physically
1333 * is emptied. On bus types like RS485, the transmitter must
1334 * release the bus after transmitting. This must be done when
1335 * the transmit shift register is empty, not be done when the
1336 * transmit holding register is empty. This functionality
1337 * allows an RS485 driver to be written in user space.
1339 static int get_lsr_info(struct sab82532 * info, unsigned int *value)
1341 unsigned int result;
1343 result = (!info->xmit.buf && test_bit(SAB82532_ALLS, &info->irqflags))
1345 return put_user(result, value);
1349 static int get_modem_info(struct sab82532 * info, unsigned int *value)
1351 unsigned int result;
1353 result = ((readb(&info->regs->r.mode) & SAB82532_MODE_RTS) ?
1354 ((readb(&info->regs->r.mode) & SAB82532_MODE_FRTS) ? 0 : TIOCM_RTS)
1356 | ((readb(&info->regs->r.pvr) & info->pvr_dtr_bit) ? 0 : TIOCM_DTR)
1357 | ((readb(&info->regs->r.vstr) & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR)
1358 | ((readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ? 0 : TIOCM_DSR)
1359 | ((readb(&info->regs->r.star) & SAB82532_STAR_CTS) ? TIOCM_CTS : 0);
1360 return put_user(result,value);
1363 static int set_modem_info(struct sab82532 * info, unsigned int cmd,
1364 unsigned int *value)
1368 if (get_user(arg, value))
1372 if (arg & TIOCM_RTS) {
1373 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1374 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1376 if (arg & TIOCM_DTR) {
1377 writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1381 if (arg & TIOCM_RTS) {
1382 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1383 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1385 if (arg & TIOCM_DTR) {
1386 writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1390 if (arg & TIOCM_RTS) {
1391 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1392 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1394 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1395 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1397 if (arg & TIOCM_DTR) {
1398 writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1400 writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1410 * This routine sends a break character out the serial port.
1412 static void sab82532_break(struct tty_struct *tty, int break_state)
1414 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1415 unsigned long flags;
1417 if (serial_paranoia_check(info, tty->device, "sab82532_break"))
1423 #ifdef SERIAL_DEBUG_SEND_BREAK
1424 printk("sab82532_break(%d) jiff=%lu...", break_state, jiffies);
1426 save_flags(flags); cli();
1427 if (break_state == -1)
1428 writeb(readb(&info->regs->rw.dafo) | SAB82532_DAFO_XBRK, &info->regs->rw.dafo);
1430 writeb(readb(&info->regs->rw.dafo) & ~(SAB82532_DAFO_XBRK), &info->regs->rw.dafo);
1431 restore_flags(flags);
1435 static int sab82532_ioctl(struct tty_struct *tty, struct file * file,
1436 unsigned int cmd, unsigned long arg)
1438 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1439 struct async_icount cprev, cnow; /* kernel counter temps */
1440 struct serial_icounter_struct *p_cuser; /* user space */
1442 if (serial_paranoia_check(info, tty->device, "sab82532_ioctl"))
1445 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1446 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1447 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1448 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1449 if (tty->flags & (1 << TTY_IO_ERROR))
1455 return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1457 if (get_user(arg, (unsigned int *) arg))
1459 tty->termios->c_cflag =
1460 ((tty->termios->c_cflag & ~CLOCAL) |
1461 (arg ? CLOCAL : 0));
1464 return get_modem_info(info, (unsigned int *) arg);
1468 return set_modem_info(info, cmd, (unsigned int *) arg);
1470 return get_serial_info(info,
1471 (struct serial_struct *) arg);
1473 return set_serial_info(info,
1474 (struct serial_struct *) arg);
1476 case TIOCSERGETLSR: /* Get line status register */
1477 return get_lsr_info(info, (unsigned int *) arg);
1479 case TIOCSERGSTRUCT:
1480 if (copy_to_user((struct sab82532 *) arg,
1481 info, sizeof(struct sab82532)))
1486 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1487 * - mask passed in arg for lines of interest
1488 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1489 * Caller should use TIOCGICOUNT to see which one it was
1493 /* note the counters on entry */
1494 cprev = info->icount;
1497 interruptible_sleep_on(&info->delta_msr_wait);
1498 /* see if a signal did it */
1499 if (signal_pending(current))
1500 return -ERESTARTSYS;
1502 cnow = info->icount; /* atomic copy */
1504 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1505 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1506 return -EIO; /* no change => error */
1507 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1508 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1509 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1510 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1518 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1519 * Return: write counters to the user passed counter struct
1520 * NB: both 1->0 and 0->1 transitions are counted except for
1521 * RI where only 0->1 is counted.
1525 cnow = info->icount;
1527 p_cuser = (struct serial_icounter_struct *) arg;
1528 if (put_user(cnow.cts, &p_cuser->cts) ||
1529 put_user(cnow.dsr, &p_cuser->dsr) ||
1530 put_user(cnow.rng, &p_cuser->rng) ||
1531 put_user(cnow.dcd, &p_cuser->dcd))
1536 return -ENOIOCTLCMD;
1541 static void sab82532_set_termios(struct tty_struct *tty,
1542 struct termios *old_termios)
1544 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1546 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1547 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1548 == RELEVANT_IFLAG(old_termios->c_iflag)))
1553 /* Handle transition to B0 status */
1554 if ((old_termios->c_cflag & CBAUD) &&
1555 !(tty->termios->c_cflag & CBAUD)) {
1556 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_FRTS, &info->regs->w.mode);
1557 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1558 writeb(readb(&info->regs->w.pvr) | info->pvr_dtr_bit, &info->regs->w.pvr);
1561 /* Handle transition away from B0 status */
1562 if (!(old_termios->c_cflag & CBAUD) &&
1563 (tty->termios->c_cflag & CBAUD)) {
1564 writeb(readb(&info->regs->w.pvr) & ~(info->pvr_dtr_bit), &info->regs->w.pvr);
1565 if (tty->termios->c_cflag & CRTSCTS) {
1566 writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_RTS), &info->regs->w.mode);
1567 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_FRTS, &info->regs->w.mode);
1568 } else if (test_bit(TTY_THROTTLED, &tty->flags)) {
1569 writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_FRTS | SAB82532_MODE_RTS), &info->regs->w.mode);
1571 writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_FRTS), &info->regs->w.mode);
1572 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1576 /* Handle turning off CRTSCTS */
1577 if ((old_termios->c_cflag & CRTSCTS) &&
1578 !(tty->termios->c_cflag & CRTSCTS)) {
1579 tty->hw_stopped = 0;
1580 sab82532_start(tty);
1585 * ------------------------------------------------------------
1588 * This routine is called when the serial port gets closed. First, we
1589 * wait for the last remaining data to be sent. Then, we unlink its
1590 * async structure from the interrupt chain if necessary, and we free
1591 * that IRQ if nothing is left in the chain.
1592 * ------------------------------------------------------------
1594 static void sab82532_close(struct tty_struct *tty, struct file * filp)
1596 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1597 unsigned long flags;
1599 if (!info || serial_paranoia_check(info, tty->device, "sab82532_close"))
1602 save_flags(flags); cli();
1604 if (tty_hung_up_p(filp)) {
1606 restore_flags(flags);
1610 #ifdef SERIAL_DEBUG_OPEN
1611 printk("sab82532_close ttys%d, count = %d\n", info->line, info->count);
1613 if ((tty->count == 1) && (info->count != 1)) {
1615 * Uh, oh. tty->count is 1, which means that the tty
1616 * structure will be freed. info->count should always
1617 * be one in these conditions. If it's greater than
1618 * one, we've got real problems, since it means the
1619 * serial port won't be shutdown.
1621 printk("sab82532_close: bad serial port count; tty->count is 1,"
1622 " info->count is %d\n", info->count);
1625 if (--info->count < 0) {
1626 printk("sab82532_close: bad serial port count for ttys%d: %d\n",
1627 info->line, info->count);
1632 restore_flags(flags);
1635 info->flags |= ASYNC_CLOSING;
1637 * Save the termios structure, since this port may have
1638 * separate termios for callout and dialin.
1640 if (info->flags & ASYNC_NORMAL_ACTIVE)
1641 info->normal_termios = *tty->termios;
1642 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1643 info->callout_termios = *tty->termios;
1645 * Now we wait for the transmit buffer to clear; and we notify
1646 * the line discipline to only process XON/XOFF characters.
1649 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1650 tty_wait_until_sent(tty, info->closing_wait);
1653 * At this point we stop accepting input. To do this, we
1654 * disable the receive line status interrupts, and turn off
1657 info->interrupt_mask0 |= SAB82532_IMR0_TCD;
1658 writeb(info->interrupt_mask0, &info->regs->w.imr0);
1659 if (info->flags & ASYNC_INITIALIZED) {
1661 * Before we drop DTR, make sure the UART transmitter
1662 * has completely drained; this is especially
1663 * important if there is a transmit FIFO!
1665 sab82532_wait_until_sent(tty, info->timeout);
1668 if (tty->driver.flush_buffer)
1669 tty->driver.flush_buffer(tty);
1670 if (tty->ldisc.flush_buffer)
1671 tty->ldisc.flush_buffer(tty);
1675 if (info->blocked_open) {
1676 if (info->close_delay) {
1677 current->state = TASK_INTERRUPTIBLE;
1678 schedule_timeout(info->close_delay);
1680 wake_up_interruptible(&info->open_wait);
1682 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1684 wake_up_interruptible(&info->close_wait);
1686 restore_flags(flags);
1690 * sab82532_wait_until_sent() --- wait until the transmitter is empty
1692 static void sab82532_wait_until_sent(struct tty_struct *tty, int timeout)
1694 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1695 unsigned long orig_jiffies, char_time;
1697 if (serial_paranoia_check(info,tty->device,"sab82532_wait_until_sent"))
1701 * Set the check interval to be 1/5 of the estimated time to
1702 * send a single character, and make it at least 1. The check
1703 * interval should also be less than the timeout.
1705 * Note: we have to use pretty tight timings here to satisfy
1708 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1709 char_time = char_time / 5;
1713 char_time = MIN(char_time, timeout);
1714 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1715 printk("In sab82532_wait_until_sent(%d) check=%lu "
1716 "xmit_cnt = %ld, alls = %d (jiff=%lu)...\n",
1718 CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
1719 test_bit(SAB82532_ALLS, &info->irqflags), jiffies);
1721 orig_jiffies = jiffies;
1722 while ((info->xmit.head != info->xmit.tail) ||
1723 !test_bit(SAB82532_ALLS, &info->irqflags)) {
1724 current->state = TASK_INTERRUPTIBLE;
1725 schedule_timeout(char_time);
1726 if (signal_pending(current))
1728 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1731 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1732 printk("xmit_cnt = %ld, alls = %d (jiff=%lu)...done\n",
1733 CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
1734 test_bit(SAB82532_ALLS, &info->irqflags), jiffies);
1739 * sab82532_hangup() --- called by tty_hangup() when a hangup is signaled.
1741 static void sab82532_hangup(struct tty_struct *tty)
1743 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1745 if (serial_paranoia_check(info, tty->device, "sab82532_hangup"))
1748 #ifdef CONFIG_SERIAL_CONSOLE
1749 if (info->is_console)
1753 sab82532_flush_buffer(tty);
1757 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1759 wake_up_interruptible(&info->open_wait);
1763 * ------------------------------------------------------------
1764 * sab82532_open() and friends
1765 * ------------------------------------------------------------
1767 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1768 struct sab82532 *info)
1770 DECLARE_WAITQUEUE(wait, current);
1775 * If the device is in the middle of being closed, then block
1776 * until it's done, and then try again.
1778 if (tty_hung_up_p(filp) ||
1779 (info->flags & ASYNC_CLOSING)) {
1780 if (info->flags & ASYNC_CLOSING)
1781 interruptible_sleep_on(&info->close_wait);
1782 #ifdef SERIAL_DO_RESTART
1783 if (info->flags & ASYNC_HUP_NOTIFY)
1786 return -ERESTARTSYS;
1793 * If this is a callout device, then just make sure the normal
1794 * device isn't being used.
1796 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1797 if (info->flags & ASYNC_NORMAL_ACTIVE)
1799 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1800 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1801 (info->session != current->session))
1803 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1804 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1805 (info->pgrp != current->pgrp))
1807 info->flags |= ASYNC_CALLOUT_ACTIVE;
1812 * If non-blocking mode is set, or the port is not enabled,
1813 * then make the check up front and then exit.
1815 if ((filp->f_flags & O_NONBLOCK) ||
1816 (tty->flags & (1 << TTY_IO_ERROR))) {
1817 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1819 info->flags |= ASYNC_NORMAL_ACTIVE;
1823 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1824 if (info->normal_termios.c_cflag & CLOCAL)
1827 if (tty->termios->c_cflag & CLOCAL)
1832 * Block waiting for the carrier detect and the line to become
1833 * free (i.e., not in use by the callout). While we are in
1834 * this loop, info->count is dropped by one, so that
1835 * sab82532_close() knows when to free things. We restore it upon
1836 * exit, either normal or abnormal.
1839 add_wait_queue(&info->open_wait, &wait);
1840 #ifdef SERIAL_DEBUG_OPEN
1841 printk("block_til_ready before block: ttyS%d, count = %d\n",
1842 info->line, info->count);
1845 if (!tty_hung_up_p(filp))
1848 info->blocked_open++;
1851 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1852 (tty->termios->c_cflag & CBAUD)) {
1853 writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1854 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1855 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1858 set_current_state(TASK_INTERRUPTIBLE);
1859 if (tty_hung_up_p(filp) ||
1860 !(info->flags & ASYNC_INITIALIZED)) {
1861 #ifdef SERIAL_DO_RESTART
1862 if (info->flags & ASYNC_HUP_NOTIFY)
1865 retval = -ERESTARTSYS;
1871 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1872 !(info->flags & ASYNC_CLOSING) &&
1873 (do_clocal || !(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD)))
1875 if (signal_pending(current)) {
1876 retval = -ERESTARTSYS;
1879 #ifdef SERIAL_DEBUG_OPEN
1880 printk("block_til_ready blocking: ttyS%d, count = %d, flags = %x, clocal = %d, vstr = %02x\n",
1881 info->line, info->count, info->flags, do_clocal, readb(&info->regs->r.vstr));
1885 current->state = TASK_RUNNING;
1886 remove_wait_queue(&info->open_wait, &wait);
1887 if (!tty_hung_up_p(filp))
1889 info->blocked_open--;
1890 #ifdef SERIAL_DEBUG_OPEN
1891 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1892 info->line, info->count);
1896 info->flags |= ASYNC_NORMAL_ACTIVE;
1901 * This routine is called whenever a serial port is opened. It
1902 * enables interrupts for a serial port, linking in its async structure into
1903 * the IRQ chain. It also performs the serial-specific
1904 * initialization for the tty structure.
1906 static int sab82532_open(struct tty_struct *tty, struct file * filp)
1908 struct sab82532 *info = sab82532_chain;
1912 #ifdef SERIAL_DEBUG_OPEN
1913 printk("sab82532_open: count = %d\n", info->count);
1916 line = MINOR(tty->device) - tty->driver.minor_start;
1917 if ((line < 0) || (line >= NR_PORTS))
1921 if (info->line == line)
1926 printk("sab82532_open: can't find info for line %d\n",
1931 if (serial_paranoia_check(info, tty->device, "sab82532_open"))
1934 #ifdef SERIAL_DEBUG_OPEN
1935 printk("sab82532_open %s%d, count = %d\n", tty->driver.name, info->line,
1940 page = get_free_page(GFP_KERNEL);
1946 tmp_buf = (unsigned char *) page;
1950 tty->driver_data = info;
1954 * If the port is in the middle of closing, bail out now.
1956 if (tty_hung_up_p(filp) ||
1957 (info->flags & ASYNC_CLOSING)) {
1958 if (info->flags & ASYNC_CLOSING)
1959 interruptible_sleep_on(&info->close_wait);
1960 #ifdef SERIAL_DO_RESTART
1961 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1962 -EAGAIN : -ERESTARTSYS);
1969 * Start up serial port
1971 retval = startup(info);
1976 retval = block_til_ready(tty, filp, info);
1978 #ifdef SERIAL_DEBUG_OPEN
1979 printk("sab82532_open returning after block_til_ready with %d\n",
1985 if ((info->count == 1) &&
1986 (info->flags & ASYNC_SPLIT_TERMIOS)) {
1987 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1988 *tty->termios = info->normal_termios;
1990 *tty->termios = info->callout_termios;
1994 #ifdef CONFIG_SERIAL_CONSOLE
1995 if (sab82532_console.cflag && sab82532_console.index == line) {
1996 tty->termios->c_cflag = sab82532_console.cflag;
1997 sab82532_console.cflag = 0;
2002 info->session = current->session;
2003 info->pgrp = current->pgrp;
2005 #ifdef SERIAL_DEBUG_OPEN
2006 printk("sab82532_open ttys%d successful... count %d", info->line, info->count);
2012 * /proc fs routines....
2015 static __inline__ int
2016 line_info(char *buf, struct sab82532 *info)
2018 unsigned long flags;
2022 ret = sprintf(buf, "%u: uart:SAB82532 ", info->line);
2023 switch (info->type) {
2025 ret += sprintf(buf+ret, "V1.0 ");
2028 ret += sprintf(buf+ret, "V2.0 ");
2031 ret += sprintf(buf+ret, "V3.2 ");
2034 ret += sprintf(buf+ret, "V?.? ");
2037 ret += sprintf(buf+ret, "port:%lX irq:%s",
2038 (unsigned long)info->regs, __irq_itoa(info->irq));
2041 ret += sprintf(buf+ret, "\n");
2046 * Figure out the current RS-232 lines
2050 save_flags(flags); cli();
2051 if (readb(&info->regs->r.mode) & SAB82532_MODE_RTS) {
2052 if (!(readb(&info->regs->r.mode) & SAB82532_MODE_FRTS))
2053 strcat(stat_buf, "|RTS");
2055 strcat(stat_buf, "|RTS");
2057 if (readb(&info->regs->r.star) & SAB82532_STAR_CTS)
2058 strcat(stat_buf, "|CTS");
2059 if (!(readb(&info->regs->r.pvr) & info->pvr_dtr_bit))
2060 strcat(stat_buf, "|DTR");
2061 if (!(readb(&info->regs->r.pvr) & info->pvr_dsr_bit))
2062 strcat(stat_buf, "|DSR");
2063 if (!(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD))
2064 strcat(stat_buf, "|CD");
2065 restore_flags(flags);
2068 ret += sprintf(buf+ret, " baud:%u", info->baud);
2070 ret += sprintf(buf+ret, " tx:%u rx:%u",
2071 info->icount.tx, info->icount.rx);
2073 if (info->icount.frame)
2074 ret += sprintf(buf+ret, " fe:%u", info->icount.frame);
2076 if (info->icount.parity)
2077 ret += sprintf(buf+ret, " pe:%u", info->icount.parity);
2079 if (info->icount.brk)
2080 ret += sprintf(buf+ret, " brk:%u", info->icount.brk);
2082 if (info->icount.overrun)
2083 ret += sprintf(buf+ret, " oe:%u", info->icount.overrun);
2086 * Last thing is the RS-232 status lines.
2088 ret += sprintf(buf+ret, " %s\n", stat_buf + 1);
2092 int sab82532_read_proc(char *page, char **start, off_t off, int count,
2093 int *eof, void *data)
2095 struct sab82532 *info = sab82532_chain;
2099 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2100 for (info = sab82532_chain; info && len < 4000; info = info->next) {
2101 len += line_info(page + len, info);
2102 if (len+begin > off+count)
2104 if (len+begin < off) {
2111 if (off >= len+begin)
2113 *start = page + (off-begin);
2114 return ((count < begin+len-off) ? count : begin+len-off);
2118 * ---------------------------------------------------------------------
2119 * sab82532_init() and friends
2121 * sab82532_init() is called at boot-time to initialize the serial driver.
2122 * ---------------------------------------------------------------------
2124 static int __init get_sab82532(unsigned long *memory_start)
2126 struct linux_ebus *ebus;
2127 struct linux_ebus_device *edev = 0;
2128 struct sab82532 *sab;
2129 unsigned long regs, offset;
2132 for_each_ebus(ebus) {
2133 for_each_ebusdev(edev, ebus) {
2134 if (!strcmp(edev->prom_name, "se"))
2137 if (!strcmp(edev->prom_name, "serial")) {
2141 /* On RIO this can be an SE, check it. We could
2142 * just check ebus->is_rio, but this is more portable.
2144 clen = prom_getproperty(edev->prom_node, "compatible",
2145 compat, sizeof(compat));
2147 if (strncmp(compat, "sab82532", 8) == 0) {
2159 regs = edev->resource[0].start;
2160 offset = sizeof(union sab82532_async_regs);
2162 for (i = 0; i < 2; i++) {
2164 *memory_start = (*memory_start + 7) & ~(7);
2165 sab = (struct sab82532 *)*memory_start;
2166 *memory_start += sizeof(struct sab82532);
2168 sab = (struct sab82532 *)kmalloc(sizeof(struct sab82532),
2171 printk("sab82532: can't alloc sab struct\n");
2175 memset(sab, 0, sizeof(struct sab82532));
2177 sab->regs = ioremap(regs + offset, sizeof(union sab82532_async_regs));
2178 sab->irq = edev->irqs[0];
2180 sab->xmit_fifo_size = 32;
2181 sab->recv_fifo_size = 32;
2183 writeb(SAB82532_IPC_IC_ACT_LOW, &sab->regs->w.ipc);
2185 sab->next = sab82532_chain;
2186 sab82532_chain = sab;
2188 offset -= sizeof(union sab82532_async_regs);
2194 static void __init sab82532_kgdb_hook(int line)
2196 prom_printf("sab82532: kgdb support is not implemented, yet\n");
2201 static inline void __init show_serial_version(void)
2203 char *revision = "$Revision: 1.1.1.1 $";
2206 version = strchr(revision, ' ');
2207 strcpy(serial_version, ++version);
2208 p = strchr(serial_version, ' ');
2210 printk("SAB82532 serial driver version %s\n", serial_version);
2213 extern int su_num_ports;
2216 * The serial driver boot-time initialization code!
2218 int __init sab82532_init(void)
2220 struct sab82532 *info;
2223 if (!sab82532_chain)
2225 if (!sab82532_chain)
2228 init_bh(SERIAL_BH, do_serial_bh);
2230 show_serial_version();
2232 /* Initialize the tty_driver structure */
2233 memset(&serial_driver, 0, sizeof(struct tty_driver));
2234 serial_driver.magic = TTY_DRIVER_MAGIC;
2235 serial_driver.driver_name = "serial";
2236 #ifdef CONFIG_DEVFS_FS
2237 serial_driver.name = "tts/%d";
2239 serial_driver.name = "ttyS";
2241 serial_driver.major = TTY_MAJOR;
2242 serial_driver.minor_start = 64 + su_num_ports;
2243 serial_driver.num = NR_PORTS;
2244 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2245 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2246 serial_driver.init_termios = tty_std_termios;
2247 serial_driver.init_termios.c_cflag =
2248 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2249 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2250 serial_driver.refcount = &sab82532_refcount;
2251 serial_driver.table = sab82532_table;
2252 serial_driver.termios = sab82532_termios;
2253 serial_driver.termios_locked = sab82532_termios_locked;
2255 serial_driver.open = sab82532_open;
2256 serial_driver.close = sab82532_close;
2257 serial_driver.write = sab82532_write;
2258 serial_driver.put_char = sab82532_put_char;
2259 serial_driver.flush_chars = sab82532_flush_chars;
2260 serial_driver.write_room = sab82532_write_room;
2261 serial_driver.chars_in_buffer = sab82532_chars_in_buffer;
2262 serial_driver.flush_buffer = sab82532_flush_buffer;
2263 serial_driver.ioctl = sab82532_ioctl;
2264 serial_driver.throttle = sab82532_throttle;
2265 serial_driver.unthrottle = sab82532_unthrottle;
2266 serial_driver.send_xchar = sab82532_send_xchar;
2267 serial_driver.set_termios = sab82532_set_termios;
2268 serial_driver.stop = sab82532_stop;
2269 serial_driver.start = sab82532_start;
2270 serial_driver.hangup = sab82532_hangup;
2271 serial_driver.break_ctl = sab82532_break;
2272 serial_driver.wait_until_sent = sab82532_wait_until_sent;
2273 serial_driver.read_proc = sab82532_read_proc;
2276 * The callout device is just like normal device except for
2277 * major number and the subtype code.
2279 callout_driver = serial_driver;
2280 #ifdef CONFIG_DEVFS_FS
2281 callout_driver.name = "cua/%d";
2283 callout_driver.name = "cua";
2285 callout_driver.major = TTYAUX_MAJOR;
2286 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2287 callout_driver.read_proc = 0;
2288 callout_driver.proc_entry = 0;
2290 if (tty_register_driver(&serial_driver))
2291 panic("Couldn't register serial driver\n");
2292 if (tty_register_driver(&callout_driver))
2293 panic("Couldn't register callout driver\n");
2295 for (info = sab82532_chain, i = 0; info; info = info->next, i++) {
2296 info->magic = SERIAL_MAGIC;
2298 info->type = readb(&info->regs->r.vstr) & 0x0f;
2299 writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &info->regs->w.pcr);
2300 writeb(0xff, &info->regs->w.pim);
2301 if (info->line == 0) {
2302 info->pvr_dsr_bit = (1 << 0);
2303 info->pvr_dtr_bit = (1 << 1);
2305 info->pvr_dsr_bit = (1 << 3);
2306 info->pvr_dtr_bit = (1 << 2);
2308 writeb((1 << 1) | (1 << 2) | (1 << 4), &info->regs->w.pvr);
2309 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
2310 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
2312 info->custom_divisor = 16;
2313 info->close_delay = 5*HZ/10;
2314 info->closing_wait = 30*HZ;
2315 info->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
2316 info->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
2319 info->blocked_open = 0;
2320 info->tqueue.routine = do_softint;
2321 info->tqueue.data = info;
2322 info->tqueue_hangup.routine = do_serial_hangup;
2323 info->tqueue_hangup.data = info;
2324 info->callout_termios = callout_driver.init_termios;
2325 info->normal_termios = serial_driver.init_termios;
2326 init_waitqueue_head(&info->open_wait);
2327 init_waitqueue_head(&info->close_wait);
2328 init_waitqueue_head(&info->delta_msr_wait);
2329 info->icount.cts = info->icount.dsr =
2330 info->icount.rng = info->icount.dcd = 0;
2331 info->icount.rx = info->icount.tx = 0;
2332 info->icount.frame = info->icount.parity = 0;
2333 info->icount.overrun = info->icount.brk = 0;
2335 if (!(info->line & 0x01)) {
2336 if (request_irq(info->irq, sab82532_interrupt, SA_SHIRQ,
2337 "serial(sab82532)", info)) {
2338 printk("sab82532: can't get IRQ %x\n",
2340 panic("sab82532 initialization failed");
2345 "ttyS%02d at 0x%lx (irq = %s) is a SAB82532 %s\n",
2346 info->line + su_num_ports, (unsigned long)info->regs,
2347 __irq_itoa(info->irq), sab82532_version[info->type]);
2350 #ifdef SERIAL_LOG_DEVICE
2351 dprint_init(SERIAL_LOG_DEVICE);
2356 int __init sab82532_probe(void)
2358 int node, enode, snode;
2362 node = prom_getchild(prom_root_node);
2363 node = prom_searchsiblings(node, "pci");
2366 * Check for SUNW,sabre on Ultra 5/10/AXi.
2368 len = prom_getproperty(node, "model", model, sizeof(model));
2369 if ((len > 0) && !strncmp(model, "SUNW,sabre", len)) {
2370 node = prom_getchild(node);
2371 node = prom_searchsiblings(node, "pci");
2375 * For each PCI bus...
2378 enode = prom_getchild(node);
2379 enode = prom_searchsiblings(enode, "ebus");
2382 * For each EBus on this PCI...
2387 child = prom_getchild(enode);
2388 snode = prom_searchsiblings(child, "se");
2392 snode = prom_searchsiblings(child, "serial");
2397 clen = prom_getproperty(snode, "compatible",
2398 compat, sizeof(compat));
2400 if (strncmp(compat, "sab82532", 8) == 0)
2405 enode = prom_getsibling(enode);
2406 enode = prom_searchsiblings(enode, "ebus");
2408 node = prom_getsibling(node);
2409 node = prom_searchsiblings(node, "pci");
2414 #ifdef CONFIG_SERIAL_CONSOLE
2415 sunserial_setinitfunc(sab82532_console_init);
2418 sunserial_setinitfunc(sab82532_init);
2419 rs_ops.rs_kgdb_hook = sab82532_kgdb_hook;
2425 MODULE_LICENSE("GPL");
2427 int init_module(void)
2429 if (get_sab82532(0))
2432 return sab82532_init();
2435 void cleanup_module(void)
2437 struct sab82532 *sab;
2438 unsigned long flags;
2441 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2444 remove_bh(SERIAL_BH);
2445 if ((e1 = tty_unregister_driver(&serial_driver)))
2446 printk("SERIAL: failed to unregister serial driver (%d)\n",
2448 if ((e2 = tty_unregister_driver(&callout_driver)))
2449 printk("SERIAL: failed to unregister callout driver (%d)\n",
2451 restore_flags(flags);
2454 free_page((unsigned long) tmp_buf);
2457 for (sab = sab82532_chain; sab; sab = sab->next) {
2458 if (!(sab->line & 0x01))
2459 free_irq(sab->irq, sab);
2465 #ifdef CONFIG_SERIAL_CONSOLE
2467 batten_down_hatches(struct sab82532 *info)
2469 unsigned char saved_rfc, tmp;
2471 if (!stop_a_enabled)
2474 /* If we are doing kadb, we call the debugger
2475 * else we just drop into the boot monitor.
2476 * Note that we must flush the user windows
2477 * first before giving up control.
2480 flush_user_windows();
2483 * Set FIFO to single character mode.
2485 saved_rfc = readb(&info->regs->r.rfc);
2486 tmp = readb(&info->regs->rw.rfc);
2487 tmp &= ~(SAB82532_RFC_RFDF);
2488 writeb(tmp, &info->regs->rw.rfc);
2489 sab82532_cec_wait(info);
2490 writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
2492 #ifndef __sparc_v9__
2493 if ((((unsigned long)linux_dbvec) >= DEBUG_FIRSTVADDR) &&
2494 (((unsigned long)linux_dbvec) <= DEBUG_LASTVADDR))
2495 sp_enter_debugger();
2501 * Reset FIFO to character + status mode.
2503 writeb(saved_rfc, &info->regs->w.rfc);
2504 sab82532_cec_wait(info);
2505 writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
2508 static __inline__ void
2509 sab82532_console_putchar(struct sab82532 *info, char c)
2511 unsigned long flags;
2513 save_flags(flags); cli();
2514 sab82532_tec_wait(info);
2515 writeb(c, &info->regs->w.tic);
2516 restore_flags(flags);
2520 sab82532_console_write(struct console *con, const char *s, unsigned n)
2522 struct sab82532 *info;
2525 info = sab82532_chain;
2526 for (i = con->index; i; i--) {
2532 for (i = 0; i < n; i++) {
2534 sab82532_console_putchar(info, '\r');
2535 sab82532_console_putchar(info, *s++);
2537 sab82532_tec_wait(info);
2541 sab82532_console_device(struct console *con)
2543 return MKDEV(TTY_MAJOR, 64 + con->index);
2547 sab82532_console_setup(struct console *con, char *options)
2549 static struct tty_struct c_tty;
2550 static struct termios c_termios;
2551 struct sab82532 *info;
2555 info = sab82532_chain;
2556 for (i = con->index; i; i--) {
2561 info->is_console = 1;
2564 * Initialize the hardware
2566 sab82532_init_line(info);
2569 * Finally, enable interrupts
2571 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
2572 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
2573 writeb(info->interrupt_mask0, &info->regs->w.imr0);
2574 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
2575 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
2576 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
2578 writeb(info->interrupt_mask1, &info->regs->w.imr1);
2580 printk("Console: ttyS%d (SAB82532)\n", info->line);
2582 sunserial_console_termios(con);
2586 * Fake up the tty and tty->termios structures so we can use
2587 * change_speed (and eliminate a lot of duplicate code).
2590 memset(&c_tty, 0, sizeof(c_tty));
2593 if (!info->tty->termios) {
2594 memset(&c_termios, 0, sizeof(c_termios));
2595 info->tty->termios = &c_termios;
2597 info->tty->termios->c_cflag = con->cflag;
2601 /* Now take out the pointers to static structures if necessary */
2602 if (info->tty->termios == &c_termios)
2603 info->tty->termios = 0;
2604 if (info->tty == &c_tty)
2610 static struct console sab82532_console = {
2612 write: sab82532_console_write,
2613 device: sab82532_console_device,
2614 setup: sab82532_console_setup,
2615 flags: CON_PRINTBUFFER,
2619 int __init sab82532_console_init(void)
2621 extern int con_is_present(void);
2622 extern int su_console_registered;
2624 if (con_is_present() || su_console_registered)
2627 if (!sab82532_chain) {
2628 prom_printf("sab82532_console_setup: can't get SAB82532 chain");
2632 sab82532_console.index = serial_console - 1;
2633 register_console(&sab82532_console);
2637 #ifdef SERIAL_LOG_DEVICE
2639 static int serial_log_device = 0;
2642 dprint_init(int tty)
2644 serial_console = tty + 1;
2645 sab82532_console.index = tty;
2646 sab82532_console_setup(&sab82532_console, "");
2648 serial_log_device = tty + 1;
2652 dprintf(const char *fmt, ...)
2654 static char buffer[4096];
2658 if (!serial_log_device)
2661 va_start(args, fmt);
2662 i = vsprintf(buffer, fmt, args);
2664 sab82532_console.write(&sab82532_console, buffer, i);
2667 #endif /* SERIAL_LOG_DEVICE */
2668 #endif /* CONFIG_SERIAL_CONSOLE */