1 /* $Id: sab82532.c,v 1.65 2001/10/13 08:27:50 davem 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)) {
677 * This routine is called from the scheduler tqueue when the interrupt
678 * routine has signalled that a hangup has occurred. The path of
679 * hangup processing is:
681 * serial interrupt routine -> (scheduler tqueue) ->
682 * do_serial_hangup() -> tty->hangup() -> sab82532_hangup()
685 static void do_serial_hangup(void *private_)
687 struct sab82532 *info = (struct sab82532 *) private_;
688 struct tty_struct *tty;
697 sab82532_init_line(struct sab82532 *info)
699 unsigned char stat, tmp;
702 * Wait for any commands or immediate characters
704 sab82532_cec_wait(info);
705 sab82532_tec_wait(info);
708 * Clear the FIFO buffers.
710 writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
711 sab82532_cec_wait(info);
712 writeb(SAB82532_CMDR_XRES, &info->regs->w.cmdr);
715 * Clear the interrupt registers.
717 stat = readb(&info->regs->r.isr0);
718 stat = readb(&info->regs->r.isr1);
721 * Now, initialize the UART
723 writeb(0, &info->regs->w.ccr0); /* power-down */
724 writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
725 SAB82532_CCR0_SM_ASYNC, &info->regs->w.ccr0);
726 writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &info->regs->w.ccr1);
727 writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
728 SAB82532_CCR2_TOE, &info->regs->w.ccr2);
729 writeb(0, &info->regs->w.ccr3);
730 writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &info->regs->w.ccr4);
731 writeb(SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
732 SAB82532_MODE_RAC, &info->regs->w.mode);
733 writeb(SAB82532_RFC_DPS | SAB82532_RFC_RFDF, &info->regs->w.rfc);
734 switch (info->recv_fifo_size) {
736 tmp = readb(&info->regs->w.rfc);
737 tmp |= SAB82532_RFC_RFTH_1;
738 writeb(tmp, &info->regs->w.rfc);
741 tmp = readb(&info->regs->w.rfc);
742 tmp |= SAB82532_RFC_RFTH_4;
743 writeb(tmp, &info->regs->w.rfc);
746 tmp = readb(&info->regs->w.rfc);
747 tmp |= SAB82532_RFC_RFTH_16;
748 writeb(tmp, &info->regs->w.rfc);
751 info->recv_fifo_size = 32;
754 tmp = readb(&info->regs->w.rfc);
755 tmp |= SAB82532_RFC_RFTH_32;
756 writeb(tmp, &info->regs->w.rfc);
759 tmp = readb(&info->regs->rw.ccr0);
760 tmp |= SAB82532_CCR0_PU; /* power-up */
761 writeb(tmp, &info->regs->rw.ccr0);
764 static int startup(struct sab82532 *info)
770 page = get_free_page(GFP_KERNEL);
774 save_flags(flags); cli();
776 if (info->flags & ASYNC_INITIALIZED) {
783 set_bit(TTY_IO_ERROR, &info->tty->flags);
791 info->xmit.buf = (unsigned char *)page;
793 #ifdef SERIAL_DEBUG_OPEN
794 printk("starting up serial port %d...", info->line);
798 * Initialize the Hardware
800 sab82532_init_line(info);
802 if (info->tty->termios->c_cflag & CBAUD) {
805 tmp = readb(&info->regs->rw.mode);
806 tmp &= ~(SAB82532_MODE_FRTS);
807 tmp |= SAB82532_MODE_RTS;
808 writeb(tmp, &info->regs->rw.mode);
810 tmp = readb(&info->regs->rw.pvr);
811 tmp &= ~(info->pvr_dtr_bit);
812 writeb(tmp, &info->regs->rw.pvr);
816 * Finally, enable interrupts
818 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
820 writeb(info->interrupt_mask0, &info->regs->w.imr0);
821 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
822 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
823 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
825 writeb(info->interrupt_mask1, &info->regs->w.imr1);
826 set_bit(SAB82532_ALLS, &info->irqflags);
829 clear_bit(TTY_IO_ERROR, &info->tty->flags);
830 info->xmit.head = info->xmit.tail = 0;
832 set_bit(SAB82532_XPR, &info->irqflags);
835 * and set the speed of the serial port
839 info->flags |= ASYNC_INITIALIZED;
840 restore_flags(flags);
844 restore_flags(flags);
849 * This routine will shutdown a serial port; interrupts are disabled, and
850 * DTR is dropped if the hangup on close termio flag is on.
852 static void shutdown(struct sab82532 *info)
857 if (!(info->flags & ASYNC_INITIALIZED))
860 #ifdef SERIAL_DEBUG_OPEN
861 printk("Shutting down serial port %d...", info->line);
864 save_flags(flags); cli(); /* Disable interrupts */
867 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
868 * here so the queue might never be waken up
870 wake_up_interruptible(&info->delta_msr_wait);
872 if (info->xmit.buf) {
873 free_page((unsigned long)info->xmit.buf);
877 #ifdef CONFIG_SERIAL_CONSOLE
878 if (info->is_console) {
879 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
880 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
881 writeb(info->interrupt_mask0, &info->regs->w.imr0);
882 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
883 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
884 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
886 writeb(info->interrupt_mask1, &info->regs->w.imr1);
888 set_bit(TTY_IO_ERROR, &info->tty->flags);
889 info->flags &= ~ASYNC_INITIALIZED;
890 restore_flags(flags);
895 /* Disable Interrupts */
896 info->interrupt_mask0 = 0xff;
897 writeb(info->interrupt_mask0, &info->regs->w.imr0);
898 info->interrupt_mask1 = 0xff;
899 writeb(info->interrupt_mask1, &info->regs->w.imr1);
901 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
902 tmp = readb(&info->regs->r.mode);
903 tmp |= (SAB82532_MODE_FRTS | SAB82532_MODE_RTS);
904 writeb(tmp, &info->regs->rw.mode);
905 writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit,
906 &info->regs->rw.pvr);
909 /* Disable break condition */
910 tmp = readb(&info->regs->rw.dafo);
911 tmp &= ~(SAB82532_DAFO_XBRK);
912 writeb(tmp, &info->regs->rw.dafo);
914 /* Disable Receiver */
915 tmp = readb(&info->regs->rw.mode);
916 tmp &= ~(SAB82532_MODE_RAC);
917 writeb(tmp, &info->regs->rw.mode);
920 tmp = readb(&info->regs->rw.ccr0);
921 tmp &= ~(SAB82532_CCR0_PU);
922 writeb(tmp, &info->regs->rw.ccr0);
925 set_bit(TTY_IO_ERROR, &info->tty->flags);
927 info->flags &= ~ASYNC_INITIALIZED;
928 restore_flags(flags);
932 * This routine is called to set the UART divisor registers to match
933 * the specified baud rate for a serial port.
935 static void change_speed(struct sab82532 *info)
943 if (!info->tty || !info->tty->termios)
945 cflag = info->tty->termios->c_cflag;
947 /* Byte size and parity */
948 switch (cflag & CSIZE) {
949 case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
950 case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
951 case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
952 case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
953 /* Never happens, but GCC is too dumb to figure it out */
954 default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
957 if (cflag & CSTOPB) {
958 dafo |= SAB82532_DAFO_STOP;
962 if (cflag & PARENB) {
963 dafo |= SAB82532_DAFO_PARE;
967 if (cflag & PARODD) {
970 dafo |= SAB82532_DAFO_PAR_MARK;
973 dafo |= SAB82532_DAFO_PAR_ODD;
977 dafo |= SAB82532_DAFO_PAR_SPACE;
980 dafo |= SAB82532_DAFO_PAR_EVEN;
983 /* Determine EBRG values based on baud rate */
984 info->baud = tty_get_baud_rate(info->tty);
985 calc_ebrg(info->baud, &n, &m);
990 info->timeout = (info->xmit_fifo_size * HZ * bits) / info->baud;
991 info->tec_timeout = (10 * 1000000) / info->baud;
992 info->cec_timeout = info->tec_timeout >> 2;
995 info->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
996 info->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
998 info->timeout += HZ / 50; /* Add .02 seconds of slop */
1000 /* CTS flow control flags */
1001 if (cflag & CRTSCTS)
1002 info->flags |= ASYNC_CTS_FLOW;
1004 info->flags &= ~(ASYNC_CTS_FLOW);
1007 info->flags &= ~(ASYNC_CHECK_CD);
1009 info->flags |= ASYNC_CHECK_CD;
1011 info->tty->hw_stopped = 0;
1014 * Set up parity check flag
1015 * XXX: not implemented, yet.
1017 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1020 * Characters to ignore
1021 * XXX: not implemented, yet.
1025 * !!! ignore all characters if CREAD is not set
1026 * XXX: not implemented, yet.
1028 if ((cflag & CREAD) == 0)
1029 info->ignore_status_mask |= SAB82532_ISR0_RPF |
1033 save_flags(flags); cli();
1034 sab82532_cec_wait(info);
1035 sab82532_tec_wait(info);
1036 writeb(dafo, &info->regs->w.dafo);
1037 writeb(ebrg & 0xff, &info->regs->w.bgr);
1038 writeb(readb(&info->regs->rw.ccr2) & ~(0xc0), &info->regs->rw.ccr2);
1039 writeb(readb(&info->regs->rw.ccr2) | ((ebrg >> 2) & 0xc0), &info->regs->rw.ccr2);
1040 if (info->flags & ASYNC_CTS_FLOW) {
1041 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1042 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1043 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FCTS), &info->regs->rw.mode);
1044 info->interrupt_mask1 &= ~(SAB82532_IMR1_CSC);
1045 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1047 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1048 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1049 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FCTS, &info->regs->rw.mode);
1050 info->interrupt_mask1 |= SAB82532_IMR1_CSC;
1051 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1053 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RAC, &info->regs->rw.mode);
1054 restore_flags(flags);
1057 static void sab82532_put_char(struct tty_struct *tty, unsigned char ch)
1059 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1060 unsigned long flags;
1062 if (serial_paranoia_check(info, tty->device, "sab82532_put_char"))
1065 if (!tty || !info->xmit.buf)
1068 save_flags(flags); cli();
1069 if (!CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)) {
1070 restore_flags(flags);
1074 info->xmit.buf[info->xmit.head] = ch;
1075 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
1076 restore_flags(flags);
1079 static void sab82532_flush_chars(struct tty_struct *tty)
1081 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1082 unsigned long flags;
1084 if (serial_paranoia_check(info, tty->device, "sab82532_flush_chars"))
1087 if ((info->xmit.head == info->xmit.tail) ||
1088 tty->stopped || tty->hw_stopped || !info->xmit.buf)
1091 save_flags(flags); cli();
1092 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1093 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1094 sab82532_start_tx(info);
1095 restore_flags(flags);
1098 static int sab82532_write(struct tty_struct * tty, int from_user,
1099 const unsigned char *buf, int count)
1102 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1103 unsigned long flags;
1105 if (serial_paranoia_check(info, tty->device, "sab82532_write"))
1108 if (!tty || !info->xmit.buf || !tmp_buf)
1116 c = CIRC_SPACE_TO_END(info->xmit.head,
1124 c -= copy_from_user(tmp_buf, buf, c);
1131 c1 = CIRC_SPACE_TO_END(info->xmit.head,
1136 memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
1137 info->xmit.head = (info->xmit.head + c) & (SERIAL_XMIT_SIZE-1);
1138 restore_flags(flags);
1147 c = CIRC_SPACE_TO_END(info->xmit.head,
1154 memcpy(info->xmit.buf + info->xmit.head, buf, c);
1155 info->xmit.head = (info->xmit.head + c) & (SERIAL_XMIT_SIZE-1);
1160 restore_flags(flags);
1163 if ((info->xmit.head != info->xmit.tail) &&
1164 !tty->stopped && !tty->hw_stopped) {
1165 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1166 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1167 sab82532_start_tx(info);
1170 restore_flags(flags);
1174 static int sab82532_write_room(struct tty_struct *tty)
1176 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1178 if (serial_paranoia_check(info, tty->device, "sab82532_write_room"))
1181 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1184 static int sab82532_chars_in_buffer(struct tty_struct *tty)
1186 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1188 if (serial_paranoia_check(info, tty->device, "sab82532_chars_in_buffer"))
1191 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1194 static void sab82532_flush_buffer(struct tty_struct *tty)
1196 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1197 unsigned long flags;
1199 if (serial_paranoia_check(info, tty->device, "sab82532_flush_buffer"))
1202 save_flags(flags); cli();
1203 info->xmit.head = info->xmit.tail = 0;
1204 restore_flags(flags);
1210 * This function is used to send a high-priority XON/XOFF character to
1213 static void sab82532_send_xchar(struct tty_struct *tty, char ch)
1215 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1216 unsigned long flags;
1218 if (serial_paranoia_check(info, tty->device, "sab82532_send_xchar"))
1221 save_flags(flags); cli();
1222 sab82532_tec_wait(info);
1223 writeb(ch, &info->regs->w.tic);
1224 restore_flags(flags);
1228 * ------------------------------------------------------------
1229 * sab82532_throttle()
1231 * This routine is called by the upper-layer tty layer to signal that
1232 * incoming characters should be throttled.
1233 * ------------------------------------------------------------
1235 static void sab82532_throttle(struct tty_struct * tty)
1237 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1238 #ifdef SERIAL_DEBUG_THROTTLE
1241 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1242 tty->ldisc.chars_in_buffer(tty));
1245 if (serial_paranoia_check(info, tty->device, "sab82532_throttle"))
1249 sab82532_send_xchar(tty, STOP_CHAR(tty));
1251 if (tty->termios->c_cflag & CRTSCTS) {
1252 u8 mode = readb(&info->regs->r.mode);
1253 mode &= ~(SAB82532_MODE_FRTS | SAB82532_MODE_RTS);
1254 writeb(mode, &info->regs->w.mode);
1258 static void sab82532_unthrottle(struct tty_struct * tty)
1260 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1261 #ifdef SERIAL_DEBUG_THROTTLE
1264 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1265 tty->ldisc.chars_in_buffer(tty));
1268 if (serial_paranoia_check(info, tty->device, "sab82532_unthrottle"))
1275 sab82532_send_xchar(tty, START_CHAR(tty));
1278 if (tty->termios->c_cflag & CRTSCTS) {
1279 u8 mode = readb(&info->regs->r.mode);
1280 mode &= ~(SAB82532_MODE_RTS);
1281 mode |= SAB82532_MODE_FRTS;
1282 writeb(mode, &info->regs->w.mode);
1287 * ------------------------------------------------------------
1288 * sab82532_ioctl() and friends
1289 * ------------------------------------------------------------
1292 static int get_serial_info(struct sab82532 *info,
1293 struct serial_struct *retinfo)
1295 struct serial_struct tmp;
1299 memset(&tmp, 0, sizeof(tmp));
1300 tmp.type = info->type;
1301 tmp.line = info->line;
1302 tmp.port = (unsigned long)info->regs;
1303 tmp.irq = info->irq;
1304 tmp.flags = info->flags;
1305 tmp.xmit_fifo_size = info->xmit_fifo_size;
1306 tmp.baud_base = info->baud_base;
1307 tmp.close_delay = info->close_delay;
1308 tmp.closing_wait = info->closing_wait;
1309 tmp.custom_divisor = info->custom_divisor;
1311 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1316 static int set_serial_info(struct sab82532 *info,
1317 struct serial_struct *new_info)
1324 * get_lsr_info - get line status register info
1326 * Purpose: Let user call ioctl() to get info when the UART physically
1327 * is emptied. On bus types like RS485, the transmitter must
1328 * release the bus after transmitting. This must be done when
1329 * the transmit shift register is empty, not be done when the
1330 * transmit holding register is empty. This functionality
1331 * allows an RS485 driver to be written in user space.
1333 static int get_lsr_info(struct sab82532 * info, unsigned int *value)
1335 unsigned int result;
1337 result = (!info->xmit.buf && test_bit(SAB82532_ALLS, &info->irqflags))
1339 return put_user(result, value);
1343 static int get_modem_info(struct sab82532 * info, unsigned int *value)
1345 unsigned int result;
1347 result = ((readb(&info->regs->r.mode) & SAB82532_MODE_RTS) ?
1348 ((readb(&info->regs->r.mode) & SAB82532_MODE_FRTS) ? 0 : TIOCM_RTS)
1350 | ((readb(&info->regs->r.pvr) & info->pvr_dtr_bit) ? 0 : TIOCM_DTR)
1351 | ((readb(&info->regs->r.vstr) & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR)
1352 | ((readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ? 0 : TIOCM_DSR)
1353 | ((readb(&info->regs->r.star) & SAB82532_STAR_CTS) ? TIOCM_CTS : 0);
1354 return put_user(result,value);
1357 static int set_modem_info(struct sab82532 * info, unsigned int cmd,
1358 unsigned int *value)
1362 if (get_user(arg, value))
1366 if (arg & TIOCM_RTS) {
1367 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1368 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1370 if (arg & TIOCM_DTR) {
1371 writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1375 if (arg & TIOCM_RTS) {
1376 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1377 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1379 if (arg & TIOCM_DTR) {
1380 writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1384 if (arg & TIOCM_RTS) {
1385 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1386 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1388 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1389 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1391 if (arg & TIOCM_DTR) {
1392 writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1394 writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1404 * This routine sends a break character out the serial port.
1406 static void sab82532_break(struct tty_struct *tty, int break_state)
1408 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1409 unsigned long flags;
1411 if (serial_paranoia_check(info, tty->device, "sab82532_break"))
1417 #ifdef SERIAL_DEBUG_SEND_BREAK
1418 printk("sab82532_break(%d) jiff=%lu...", break_state, jiffies);
1420 save_flags(flags); cli();
1421 if (break_state == -1)
1422 writeb(readb(&info->regs->rw.dafo) | SAB82532_DAFO_XBRK, &info->regs->rw.dafo);
1424 writeb(readb(&info->regs->rw.dafo) & ~(SAB82532_DAFO_XBRK), &info->regs->rw.dafo);
1425 restore_flags(flags);
1429 static int sab82532_ioctl(struct tty_struct *tty, struct file * file,
1430 unsigned int cmd, unsigned long arg)
1432 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1433 struct async_icount cprev, cnow; /* kernel counter temps */
1434 struct serial_icounter_struct *p_cuser; /* user space */
1436 if (serial_paranoia_check(info, tty->device, "sab82532_ioctl"))
1439 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1440 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1441 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1442 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1443 if (tty->flags & (1 << TTY_IO_ERROR))
1449 return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1451 if (get_user(arg, (unsigned int *) arg))
1453 tty->termios->c_cflag =
1454 ((tty->termios->c_cflag & ~CLOCAL) |
1455 (arg ? CLOCAL : 0));
1458 return get_modem_info(info, (unsigned int *) arg);
1462 return set_modem_info(info, cmd, (unsigned int *) arg);
1464 return get_serial_info(info,
1465 (struct serial_struct *) arg);
1467 return set_serial_info(info,
1468 (struct serial_struct *) arg);
1470 case TIOCSERGETLSR: /* Get line status register */
1471 return get_lsr_info(info, (unsigned int *) arg);
1473 case TIOCSERGSTRUCT:
1474 if (copy_to_user((struct sab82532 *) arg,
1475 info, sizeof(struct sab82532)))
1480 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1481 * - mask passed in arg for lines of interest
1482 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1483 * Caller should use TIOCGICOUNT to see which one it was
1487 /* note the counters on entry */
1488 cprev = info->icount;
1491 interruptible_sleep_on(&info->delta_msr_wait);
1492 /* see if a signal did it */
1493 if (signal_pending(current))
1494 return -ERESTARTSYS;
1496 cnow = info->icount; /* atomic copy */
1498 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1499 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1500 return -EIO; /* no change => error */
1501 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1502 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1503 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1504 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1512 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1513 * Return: write counters to the user passed counter struct
1514 * NB: both 1->0 and 0->1 transitions are counted except for
1515 * RI where only 0->1 is counted.
1519 cnow = info->icount;
1521 p_cuser = (struct serial_icounter_struct *) arg;
1522 if (put_user(cnow.cts, &p_cuser->cts) ||
1523 put_user(cnow.dsr, &p_cuser->dsr) ||
1524 put_user(cnow.rng, &p_cuser->rng) ||
1525 put_user(cnow.dcd, &p_cuser->dcd))
1530 return -ENOIOCTLCMD;
1535 static void sab82532_set_termios(struct tty_struct *tty,
1536 struct termios *old_termios)
1538 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1540 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1541 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1542 == RELEVANT_IFLAG(old_termios->c_iflag)))
1547 /* Handle transition to B0 status */
1548 if ((old_termios->c_cflag & CBAUD) &&
1549 !(tty->termios->c_cflag & CBAUD)) {
1550 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_FRTS, &info->regs->w.mode);
1551 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1552 writeb(readb(&info->regs->w.pvr) | info->pvr_dtr_bit, &info->regs->w.pvr);
1555 /* Handle transition away from B0 status */
1556 if (!(old_termios->c_cflag & CBAUD) &&
1557 (tty->termios->c_cflag & CBAUD)) {
1558 writeb(readb(&info->regs->w.pvr) & ~(info->pvr_dtr_bit), &info->regs->w.pvr);
1559 if (tty->termios->c_cflag & CRTSCTS) {
1560 writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_RTS), &info->regs->w.mode);
1561 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_FRTS, &info->regs->w.mode);
1562 } else if (test_bit(TTY_THROTTLED, &tty->flags)) {
1563 writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_FRTS | SAB82532_MODE_RTS), &info->regs->w.mode);
1565 writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_FRTS), &info->regs->w.mode);
1566 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1570 /* Handle turning off CRTSCTS */
1571 if ((old_termios->c_cflag & CRTSCTS) &&
1572 !(tty->termios->c_cflag & CRTSCTS)) {
1573 tty->hw_stopped = 0;
1574 sab82532_start(tty);
1579 * ------------------------------------------------------------
1582 * This routine is called when the serial port gets closed. First, we
1583 * wait for the last remaining data to be sent. Then, we unlink its
1584 * async structure from the interrupt chain if necessary, and we free
1585 * that IRQ if nothing is left in the chain.
1586 * ------------------------------------------------------------
1588 static void sab82532_close(struct tty_struct *tty, struct file * filp)
1590 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1591 unsigned long flags;
1593 if (!info || serial_paranoia_check(info, tty->device, "sab82532_close"))
1596 save_flags(flags); cli();
1598 if (tty_hung_up_p(filp)) {
1600 restore_flags(flags);
1604 #ifdef SERIAL_DEBUG_OPEN
1605 printk("sab82532_close ttys%d, count = %d\n", info->line, info->count);
1607 if ((tty->count == 1) && (info->count != 1)) {
1609 * Uh, oh. tty->count is 1, which means that the tty
1610 * structure will be freed. info->count should always
1611 * be one in these conditions. If it's greater than
1612 * one, we've got real problems, since it means the
1613 * serial port won't be shutdown.
1615 printk("sab82532_close: bad serial port count; tty->count is 1,"
1616 " info->count is %d\n", info->count);
1619 if (--info->count < 0) {
1620 printk("sab82532_close: bad serial port count for ttys%d: %d\n",
1621 info->line, info->count);
1626 restore_flags(flags);
1629 info->flags |= ASYNC_CLOSING;
1631 * Save the termios structure, since this port may have
1632 * separate termios for callout and dialin.
1634 if (info->flags & ASYNC_NORMAL_ACTIVE)
1635 info->normal_termios = *tty->termios;
1636 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1637 info->callout_termios = *tty->termios;
1639 * Now we wait for the transmit buffer to clear; and we notify
1640 * the line discipline to only process XON/XOFF characters.
1643 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1644 tty_wait_until_sent(tty, info->closing_wait);
1647 * At this point we stop accepting input. To do this, we
1648 * disable the receive line status interrupts, and turn off
1651 info->interrupt_mask0 |= SAB82532_IMR0_TCD;
1652 writeb(info->interrupt_mask0, &info->regs->w.imr0);
1653 if (info->flags & ASYNC_INITIALIZED) {
1655 * Before we drop DTR, make sure the UART transmitter
1656 * has completely drained; this is especially
1657 * important if there is a transmit FIFO!
1659 sab82532_wait_until_sent(tty, info->timeout);
1662 if (tty->driver.flush_buffer)
1663 tty->driver.flush_buffer(tty);
1664 tty_ldisc_flush(tty);
1668 if (info->blocked_open) {
1669 if (info->close_delay) {
1670 current->state = TASK_INTERRUPTIBLE;
1671 schedule_timeout(info->close_delay);
1673 wake_up_interruptible(&info->open_wait);
1675 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1677 wake_up_interruptible(&info->close_wait);
1679 restore_flags(flags);
1683 * sab82532_wait_until_sent() --- wait until the transmitter is empty
1685 static void sab82532_wait_until_sent(struct tty_struct *tty, int timeout)
1687 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1688 unsigned long orig_jiffies, char_time;
1690 if (serial_paranoia_check(info,tty->device,"sab82532_wait_until_sent"))
1694 * Set the check interval to be 1/5 of the estimated time to
1695 * send a single character, and make it at least 1. The check
1696 * interval should also be less than the timeout.
1698 * Note: we have to use pretty tight timings here to satisfy
1701 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1702 char_time = char_time / 5;
1706 char_time = MIN(char_time, timeout);
1707 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1708 printk("In sab82532_wait_until_sent(%d) check=%lu "
1709 "xmit_cnt = %ld, alls = %d (jiff=%lu)...\n",
1711 CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
1712 test_bit(SAB82532_ALLS, &info->irqflags), jiffies);
1714 orig_jiffies = jiffies;
1715 while ((info->xmit.head != info->xmit.tail) ||
1716 !test_bit(SAB82532_ALLS, &info->irqflags)) {
1717 current->state = TASK_INTERRUPTIBLE;
1718 schedule_timeout(char_time);
1719 if (signal_pending(current))
1721 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1724 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1725 printk("xmit_cnt = %ld, alls = %d (jiff=%lu)...done\n",
1726 CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
1727 test_bit(SAB82532_ALLS, &info->irqflags), jiffies);
1732 * sab82532_hangup() --- called by tty_hangup() when a hangup is signaled.
1734 static void sab82532_hangup(struct tty_struct *tty)
1736 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1738 if (serial_paranoia_check(info, tty->device, "sab82532_hangup"))
1741 #ifdef CONFIG_SERIAL_CONSOLE
1742 if (info->is_console)
1746 sab82532_flush_buffer(tty);
1750 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1752 wake_up_interruptible(&info->open_wait);
1756 * ------------------------------------------------------------
1757 * sab82532_open() and friends
1758 * ------------------------------------------------------------
1760 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1761 struct sab82532 *info)
1763 DECLARE_WAITQUEUE(wait, current);
1768 * If the device is in the middle of being closed, then block
1769 * until it's done, and then try again.
1771 if (tty_hung_up_p(filp) ||
1772 (info->flags & ASYNC_CLOSING)) {
1773 if (info->flags & ASYNC_CLOSING)
1774 interruptible_sleep_on(&info->close_wait);
1775 #ifdef SERIAL_DO_RESTART
1776 if (info->flags & ASYNC_HUP_NOTIFY)
1779 return -ERESTARTSYS;
1786 * If this is a callout device, then just make sure the normal
1787 * device isn't being used.
1789 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1790 if (info->flags & ASYNC_NORMAL_ACTIVE)
1792 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1793 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1794 (info->session != current->session))
1796 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1797 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1798 (info->pgrp != current->pgrp))
1800 info->flags |= ASYNC_CALLOUT_ACTIVE;
1805 * If non-blocking mode is set, or the port is not enabled,
1806 * then make the check up front and then exit.
1808 if ((filp->f_flags & O_NONBLOCK) ||
1809 (tty->flags & (1 << TTY_IO_ERROR))) {
1810 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1812 info->flags |= ASYNC_NORMAL_ACTIVE;
1816 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1817 if (info->normal_termios.c_cflag & CLOCAL)
1820 if (tty->termios->c_cflag & CLOCAL)
1825 * Block waiting for the carrier detect and the line to become
1826 * free (i.e., not in use by the callout). While we are in
1827 * this loop, info->count is dropped by one, so that
1828 * sab82532_close() knows when to free things. We restore it upon
1829 * exit, either normal or abnormal.
1832 add_wait_queue(&info->open_wait, &wait);
1833 #ifdef SERIAL_DEBUG_OPEN
1834 printk("block_til_ready before block: ttyS%d, count = %d\n",
1835 info->line, info->count);
1838 if (!tty_hung_up_p(filp))
1841 info->blocked_open++;
1844 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1845 (tty->termios->c_cflag & CBAUD)) {
1846 writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1847 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1848 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1851 set_current_state(TASK_INTERRUPTIBLE);
1852 if (tty_hung_up_p(filp) ||
1853 !(info->flags & ASYNC_INITIALIZED)) {
1854 #ifdef SERIAL_DO_RESTART
1855 if (info->flags & ASYNC_HUP_NOTIFY)
1858 retval = -ERESTARTSYS;
1864 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1865 !(info->flags & ASYNC_CLOSING) &&
1866 (do_clocal || !(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD)))
1868 if (signal_pending(current)) {
1869 retval = -ERESTARTSYS;
1872 #ifdef SERIAL_DEBUG_OPEN
1873 printk("block_til_ready blocking: ttyS%d, count = %d, flags = %x, clocal = %d, vstr = %02x\n",
1874 info->line, info->count, info->flags, do_clocal, readb(&info->regs->r.vstr));
1878 current->state = TASK_RUNNING;
1879 remove_wait_queue(&info->open_wait, &wait);
1880 if (!tty_hung_up_p(filp))
1882 info->blocked_open--;
1883 #ifdef SERIAL_DEBUG_OPEN
1884 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1885 info->line, info->count);
1889 info->flags |= ASYNC_NORMAL_ACTIVE;
1894 * This routine is called whenever a serial port is opened. It
1895 * enables interrupts for a serial port, linking in its async structure into
1896 * the IRQ chain. It also performs the serial-specific
1897 * initialization for the tty structure.
1899 static int sab82532_open(struct tty_struct *tty, struct file * filp)
1901 struct sab82532 *info = sab82532_chain;
1905 #ifdef SERIAL_DEBUG_OPEN
1906 printk("sab82532_open: count = %d\n", info->count);
1909 line = MINOR(tty->device) - tty->driver.minor_start;
1910 if ((line < 0) || (line >= NR_PORTS))
1914 if (info->line == line)
1919 printk("sab82532_open: can't find info for line %d\n",
1924 if (serial_paranoia_check(info, tty->device, "sab82532_open"))
1927 #ifdef SERIAL_DEBUG_OPEN
1928 printk("sab82532_open %s%d, count = %d\n", tty->driver.name, info->line,
1933 page = get_free_page(GFP_KERNEL);
1939 tmp_buf = (unsigned char *) page;
1943 tty->driver_data = info;
1947 * If the port is in the middle of closing, bail out now.
1949 if (tty_hung_up_p(filp) ||
1950 (info->flags & ASYNC_CLOSING)) {
1951 if (info->flags & ASYNC_CLOSING)
1952 interruptible_sleep_on(&info->close_wait);
1953 #ifdef SERIAL_DO_RESTART
1954 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1955 -EAGAIN : -ERESTARTSYS);
1962 * Start up serial port
1964 retval = startup(info);
1969 retval = block_til_ready(tty, filp, info);
1971 #ifdef SERIAL_DEBUG_OPEN
1972 printk("sab82532_open returning after block_til_ready with %d\n",
1978 if ((info->count == 1) &&
1979 (info->flags & ASYNC_SPLIT_TERMIOS)) {
1980 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1981 *tty->termios = info->normal_termios;
1983 *tty->termios = info->callout_termios;
1987 #ifdef CONFIG_SERIAL_CONSOLE
1988 if (sab82532_console.cflag && sab82532_console.index == line) {
1989 tty->termios->c_cflag = sab82532_console.cflag;
1990 sab82532_console.cflag = 0;
1995 info->session = current->session;
1996 info->pgrp = current->pgrp;
1998 #ifdef SERIAL_DEBUG_OPEN
1999 printk("sab82532_open ttys%d successful... count %d", info->line, info->count);
2005 * /proc fs routines....
2008 static __inline__ int
2009 line_info(char *buf, struct sab82532 *info)
2011 unsigned long flags;
2015 ret = sprintf(buf, "%u: uart:SAB82532 ", info->line);
2016 switch (info->type) {
2018 ret += sprintf(buf+ret, "V1.0 ");
2021 ret += sprintf(buf+ret, "V2.0 ");
2024 ret += sprintf(buf+ret, "V3.2 ");
2027 ret += sprintf(buf+ret, "V?.? ");
2030 ret += sprintf(buf+ret, "port:%lX irq:%s",
2031 (unsigned long)info->regs, __irq_itoa(info->irq));
2034 ret += sprintf(buf+ret, "\n");
2039 * Figure out the current RS-232 lines
2043 save_flags(flags); cli();
2044 if (readb(&info->regs->r.mode) & SAB82532_MODE_RTS) {
2045 if (!(readb(&info->regs->r.mode) & SAB82532_MODE_FRTS))
2046 strcat(stat_buf, "|RTS");
2048 strcat(stat_buf, "|RTS");
2050 if (readb(&info->regs->r.star) & SAB82532_STAR_CTS)
2051 strcat(stat_buf, "|CTS");
2052 if (!(readb(&info->regs->r.pvr) & info->pvr_dtr_bit))
2053 strcat(stat_buf, "|DTR");
2054 if (!(readb(&info->regs->r.pvr) & info->pvr_dsr_bit))
2055 strcat(stat_buf, "|DSR");
2056 if (!(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD))
2057 strcat(stat_buf, "|CD");
2058 restore_flags(flags);
2061 ret += sprintf(buf+ret, " baud:%u", info->baud);
2063 ret += sprintf(buf+ret, " tx:%u rx:%u",
2064 info->icount.tx, info->icount.rx);
2066 if (info->icount.frame)
2067 ret += sprintf(buf+ret, " fe:%u", info->icount.frame);
2069 if (info->icount.parity)
2070 ret += sprintf(buf+ret, " pe:%u", info->icount.parity);
2072 if (info->icount.brk)
2073 ret += sprintf(buf+ret, " brk:%u", info->icount.brk);
2075 if (info->icount.overrun)
2076 ret += sprintf(buf+ret, " oe:%u", info->icount.overrun);
2079 * Last thing is the RS-232 status lines.
2081 ret += sprintf(buf+ret, " %s\n", stat_buf + 1);
2085 int sab82532_read_proc(char *page, char **start, off_t off, int count,
2086 int *eof, void *data)
2088 struct sab82532 *info = sab82532_chain;
2092 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2093 for (info = sab82532_chain; info && len < 4000; info = info->next) {
2094 len += line_info(page + len, info);
2095 if (len+begin > off+count)
2097 if (len+begin < off) {
2104 if (off >= len+begin)
2106 *start = page + (off-begin);
2107 return ((count < begin+len-off) ? count : begin+len-off);
2111 * ---------------------------------------------------------------------
2112 * sab82532_init() and friends
2114 * sab82532_init() is called at boot-time to initialize the serial driver.
2115 * ---------------------------------------------------------------------
2117 static int __init get_sab82532(unsigned long *memory_start)
2119 struct linux_ebus *ebus;
2120 struct linux_ebus_device *edev = 0;
2121 struct sab82532 *sab;
2122 unsigned long regs, offset;
2125 for_each_ebus(ebus) {
2126 for_each_ebusdev(edev, ebus) {
2127 if (!strcmp(edev->prom_name, "se"))
2130 if (!strcmp(edev->prom_name, "serial")) {
2134 /* On RIO this can be an SE, check it. We could
2135 * just check ebus->is_rio, but this is more portable.
2137 clen = prom_getproperty(edev->prom_node, "compatible",
2138 compat, sizeof(compat));
2140 if (strncmp(compat, "sab82532", 8) == 0) {
2152 regs = edev->resource[0].start;
2153 offset = sizeof(union sab82532_async_regs);
2155 for (i = 0; i < 2; i++) {
2157 *memory_start = (*memory_start + 7) & ~(7);
2158 sab = (struct sab82532 *)*memory_start;
2159 *memory_start += sizeof(struct sab82532);
2161 sab = (struct sab82532 *)kmalloc(sizeof(struct sab82532),
2164 printk("sab82532: can't alloc sab struct\n");
2168 memset(sab, 0, sizeof(struct sab82532));
2170 sab->regs = ioremap(regs + offset, sizeof(union sab82532_async_regs));
2171 sab->irq = edev->irqs[0];
2173 sab->xmit_fifo_size = 32;
2174 sab->recv_fifo_size = 32;
2176 writeb(SAB82532_IPC_IC_ACT_LOW, &sab->regs->w.ipc);
2178 sab->next = sab82532_chain;
2179 sab82532_chain = sab;
2181 offset -= sizeof(union sab82532_async_regs);
2187 static void __init sab82532_kgdb_hook(int line)
2189 prom_printf("sab82532: kgdb support is not implemented, yet\n");
2194 static inline void __init show_serial_version(void)
2196 char *revision = "$Revision: 1.65 $";
2199 version = strchr(revision, ' ');
2200 strcpy(serial_version, ++version);
2201 p = strchr(serial_version, ' ');
2203 printk("SAB82532 serial driver version %s\n", serial_version);
2206 extern int su_num_ports;
2209 * The serial driver boot-time initialization code!
2211 int __init sab82532_init(void)
2213 struct sab82532 *info;
2216 if (!sab82532_chain)
2218 if (!sab82532_chain)
2221 init_bh(SERIAL_BH, do_serial_bh);
2223 show_serial_version();
2225 /* Initialize the tty_driver structure */
2226 memset(&serial_driver, 0, sizeof(struct tty_driver));
2227 serial_driver.magic = TTY_DRIVER_MAGIC;
2228 serial_driver.driver_name = "serial";
2229 #ifdef CONFIG_DEVFS_FS
2230 serial_driver.name = "tts/%d";
2232 serial_driver.name = "ttyS";
2234 serial_driver.major = TTY_MAJOR;
2235 serial_driver.minor_start = 64 + su_num_ports;
2236 serial_driver.num = NR_PORTS;
2237 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2238 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2239 serial_driver.init_termios = tty_std_termios;
2240 serial_driver.init_termios.c_cflag =
2241 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2242 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2243 serial_driver.refcount = &sab82532_refcount;
2244 serial_driver.table = sab82532_table;
2245 serial_driver.termios = sab82532_termios;
2246 serial_driver.termios_locked = sab82532_termios_locked;
2248 serial_driver.open = sab82532_open;
2249 serial_driver.close = sab82532_close;
2250 serial_driver.write = sab82532_write;
2251 serial_driver.put_char = sab82532_put_char;
2252 serial_driver.flush_chars = sab82532_flush_chars;
2253 serial_driver.write_room = sab82532_write_room;
2254 serial_driver.chars_in_buffer = sab82532_chars_in_buffer;
2255 serial_driver.flush_buffer = sab82532_flush_buffer;
2256 serial_driver.ioctl = sab82532_ioctl;
2257 serial_driver.throttle = sab82532_throttle;
2258 serial_driver.unthrottle = sab82532_unthrottle;
2259 serial_driver.send_xchar = sab82532_send_xchar;
2260 serial_driver.set_termios = sab82532_set_termios;
2261 serial_driver.stop = sab82532_stop;
2262 serial_driver.start = sab82532_start;
2263 serial_driver.hangup = sab82532_hangup;
2264 serial_driver.break_ctl = sab82532_break;
2265 serial_driver.wait_until_sent = sab82532_wait_until_sent;
2266 serial_driver.read_proc = sab82532_read_proc;
2269 * The callout device is just like normal device except for
2270 * major number and the subtype code.
2272 callout_driver = serial_driver;
2273 #ifdef CONFIG_DEVFS_FS
2274 callout_driver.name = "cua/%d";
2276 callout_driver.name = "cua";
2278 callout_driver.major = TTYAUX_MAJOR;
2279 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2280 callout_driver.read_proc = 0;
2281 callout_driver.proc_entry = 0;
2283 if (tty_register_driver(&serial_driver))
2284 panic("Couldn't register serial driver\n");
2285 if (tty_register_driver(&callout_driver))
2286 panic("Couldn't register callout driver\n");
2288 for (info = sab82532_chain, i = 0; info; info = info->next, i++) {
2289 info->magic = SERIAL_MAGIC;
2291 info->type = readb(&info->regs->r.vstr) & 0x0f;
2292 writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &info->regs->w.pcr);
2293 writeb(0xff, &info->regs->w.pim);
2294 if (info->line == 0) {
2295 info->pvr_dsr_bit = (1 << 0);
2296 info->pvr_dtr_bit = (1 << 1);
2298 info->pvr_dsr_bit = (1 << 3);
2299 info->pvr_dtr_bit = (1 << 2);
2301 writeb((1 << 1) | (1 << 2) | (1 << 4), &info->regs->w.pvr);
2302 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
2303 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
2305 info->custom_divisor = 16;
2306 info->close_delay = 5*HZ/10;
2307 info->closing_wait = 30*HZ;
2308 info->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
2309 info->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
2312 info->blocked_open = 0;
2313 info->tqueue.routine = do_softint;
2314 info->tqueue.data = info;
2315 info->tqueue_hangup.routine = do_serial_hangup;
2316 info->tqueue_hangup.data = info;
2317 info->callout_termios = callout_driver.init_termios;
2318 info->normal_termios = serial_driver.init_termios;
2319 init_waitqueue_head(&info->open_wait);
2320 init_waitqueue_head(&info->close_wait);
2321 init_waitqueue_head(&info->delta_msr_wait);
2322 info->icount.cts = info->icount.dsr =
2323 info->icount.rng = info->icount.dcd = 0;
2324 info->icount.rx = info->icount.tx = 0;
2325 info->icount.frame = info->icount.parity = 0;
2326 info->icount.overrun = info->icount.brk = 0;
2328 if (!(info->line & 0x01)) {
2329 if (request_irq(info->irq, sab82532_interrupt, SA_SHIRQ,
2330 "serial(sab82532)", info)) {
2331 printk("sab82532: can't get IRQ %x\n",
2333 panic("sab82532 initialization failed");
2338 "ttyS%02d at 0x%lx (irq = %s) is a SAB82532 %s\n",
2339 info->line + su_num_ports, (unsigned long)info->regs,
2340 __irq_itoa(info->irq), sab82532_version[info->type]);
2343 #ifdef SERIAL_LOG_DEVICE
2344 dprint_init(SERIAL_LOG_DEVICE);
2349 int __init sab82532_probe(void)
2351 int node, enode, snode;
2355 node = prom_getchild(prom_root_node);
2356 node = prom_searchsiblings(node, "pci");
2359 * Check for SUNW,sabre on Ultra 5/10/AXi.
2361 len = prom_getproperty(node, "model", model, sizeof(model));
2362 if ((len > 0) && !strncmp(model, "SUNW,sabre", len)) {
2363 node = prom_getchild(node);
2364 node = prom_searchsiblings(node, "pci");
2368 * For each PCI bus...
2371 enode = prom_getchild(node);
2372 enode = prom_searchsiblings(enode, "ebus");
2375 * For each EBus on this PCI...
2380 child = prom_getchild(enode);
2381 snode = prom_searchsiblings(child, "se");
2385 snode = prom_searchsiblings(child, "serial");
2390 clen = prom_getproperty(snode, "compatible",
2391 compat, sizeof(compat));
2393 if (strncmp(compat, "sab82532", 8) == 0)
2398 enode = prom_getsibling(enode);
2399 enode = prom_searchsiblings(enode, "ebus");
2401 node = prom_getsibling(node);
2402 node = prom_searchsiblings(node, "pci");
2407 #ifdef CONFIG_SERIAL_CONSOLE
2408 sunserial_setinitfunc(sab82532_console_init);
2411 sunserial_setinitfunc(sab82532_init);
2412 rs_ops.rs_kgdb_hook = sab82532_kgdb_hook;
2418 MODULE_LICENSE("GPL");
2420 int init_module(void)
2422 if (get_sab82532(0))
2425 return sab82532_init();
2428 void cleanup_module(void)
2430 struct sab82532 *sab;
2431 unsigned long flags;
2434 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2437 remove_bh(SERIAL_BH);
2438 if ((e1 = tty_unregister_driver(&serial_driver)))
2439 printk("SERIAL: failed to unregister serial driver (%d)\n",
2441 if ((e2 = tty_unregister_driver(&callout_driver)))
2442 printk("SERIAL: failed to unregister callout driver (%d)\n",
2444 restore_flags(flags);
2447 free_page((unsigned long) tmp_buf);
2450 for (sab = sab82532_chain; sab; sab = sab->next) {
2451 if (!(sab->line & 0x01))
2452 free_irq(sab->irq, sab);
2458 #ifdef CONFIG_SERIAL_CONSOLE
2460 batten_down_hatches(struct sab82532 *info)
2462 unsigned char saved_rfc, tmp;
2464 if (!stop_a_enabled)
2467 /* If we are doing kadb, we call the debugger
2468 * else we just drop into the boot monitor.
2469 * Note that we must flush the user windows
2470 * first before giving up control.
2473 flush_user_windows();
2476 * Set FIFO to single character mode.
2478 saved_rfc = readb(&info->regs->r.rfc);
2479 tmp = readb(&info->regs->rw.rfc);
2480 tmp &= ~(SAB82532_RFC_RFDF);
2481 writeb(tmp, &info->regs->rw.rfc);
2482 sab82532_cec_wait(info);
2483 writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
2485 #ifndef __sparc_v9__
2486 if ((((unsigned long)linux_dbvec) >= DEBUG_FIRSTVADDR) &&
2487 (((unsigned long)linux_dbvec) <= DEBUG_LASTVADDR))
2488 sp_enter_debugger();
2494 * Reset FIFO to character + status mode.
2496 writeb(saved_rfc, &info->regs->w.rfc);
2497 sab82532_cec_wait(info);
2498 writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
2501 static __inline__ void
2502 sab82532_console_putchar(struct sab82532 *info, char c)
2504 unsigned long flags;
2506 save_flags(flags); cli();
2507 sab82532_tec_wait(info);
2508 writeb(c, &info->regs->w.tic);
2509 restore_flags(flags);
2513 sab82532_console_write(struct console *con, const char *s, unsigned n)
2515 struct sab82532 *info;
2518 info = sab82532_chain;
2519 for (i = con->index; i; i--) {
2525 for (i = 0; i < n; i++) {
2527 sab82532_console_putchar(info, '\r');
2528 sab82532_console_putchar(info, *s++);
2530 sab82532_tec_wait(info);
2534 sab82532_console_device(struct console *con)
2536 return MKDEV(TTY_MAJOR, 64 + con->index);
2540 sab82532_console_setup(struct console *con, char *options)
2542 static struct tty_struct c_tty;
2543 static struct termios c_termios;
2544 struct sab82532 *info;
2548 info = sab82532_chain;
2549 for (i = con->index; i; i--) {
2554 info->is_console = 1;
2557 * Initialize the hardware
2559 sab82532_init_line(info);
2562 * Finally, enable interrupts
2564 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
2565 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
2566 writeb(info->interrupt_mask0, &info->regs->w.imr0);
2567 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
2568 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
2569 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
2571 writeb(info->interrupt_mask1, &info->regs->w.imr1);
2573 printk("Console: ttyS%d (SAB82532)\n", info->line);
2575 sunserial_console_termios(con);
2579 * Fake up the tty and tty->termios structures so we can use
2580 * change_speed (and eliminate a lot of duplicate code).
2583 memset(&c_tty, 0, sizeof(c_tty));
2586 if (!info->tty->termios) {
2587 memset(&c_termios, 0, sizeof(c_termios));
2588 info->tty->termios = &c_termios;
2590 info->tty->termios->c_cflag = con->cflag;
2594 /* Now take out the pointers to static structures if necessary */
2595 if (info->tty->termios == &c_termios)
2596 info->tty->termios = 0;
2597 if (info->tty == &c_tty)
2603 static struct console sab82532_console = {
2605 write: sab82532_console_write,
2606 device: sab82532_console_device,
2607 setup: sab82532_console_setup,
2608 flags: CON_PRINTBUFFER,
2612 int __init sab82532_console_init(void)
2614 extern int con_is_present(void);
2615 extern int su_console_registered;
2617 if (con_is_present() || su_console_registered)
2620 if (!sab82532_chain) {
2621 prom_printf("sab82532_console_setup: can't get SAB82532 chain");
2625 sab82532_console.index = serial_console - 1;
2626 register_console(&sab82532_console);
2630 #ifdef SERIAL_LOG_DEVICE
2632 static int serial_log_device = 0;
2635 dprint_init(int tty)
2637 serial_console = tty + 1;
2638 sab82532_console.index = tty;
2639 sab82532_console_setup(&sab82532_console, "");
2641 serial_log_device = tty + 1;
2645 dprintf(const char *fmt, ...)
2647 static char buffer[4096];
2651 if (!serial_log_device)
2654 va_start(args, fmt);
2655 i = vsprintf(buffer, fmt, args);
2657 sab82532_console.write(&sab82532_console, buffer, i);
2660 #endif /* SERIAL_LOG_DEVICE */
2661 #endif /* CONFIG_SERIAL_CONSOLE */