2 * decserial.c: Serial port driver for IOASIC DECstations.
4 * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
5 * Derived from drivers/macintosh/macserial.c by Harald Koerfgen.
8 * Copyright (C) 1998-2000 Harald Koerfgen
9 * Copyright (C) 2000,2001 Maciej W. Rozycki <macro@ds2.pg.gda.pl>
11 * For the rest of the code the original Copyright applies:
12 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
13 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
16 * Note: for IOASIC systems the wiring is as follows:
19 * DIN-7 MJ-4 signal SCC
24 * DB-25 MMJ-6 signal SCC
31 * 12 DSRS(DCE) -> ~A.CTS (*)
36 * 23 DSRS(DTE) <- ~B.RTS
38 * (*) EIA-232 defines the signal at this pin to be SCD, while DSRS(DCE)
39 * is shared with DSRS(DTE) at pin 23.
42 #include <linux/config.h>
43 #include <linux/errno.h>
44 #include <linux/signal.h>
45 #include <linux/sched.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/tty.h>
49 #include <linux/tty_flip.h>
50 #include <linux/major.h>
51 #include <linux/string.h>
52 #include <linux/fcntl.h>
54 #include <linux/kernel.h>
55 #include <linux/delay.h>
56 #include <linux/init.h>
57 #include <linux/ioport.h>
58 #ifdef CONFIG_SERIAL_CONSOLE
59 #include <linux/console.h>
63 #include <asm/pgtable.h>
65 #include <asm/system.h>
66 #include <asm/bitops.h>
67 #include <asm/uaccess.h>
68 #include <asm/wbflush.h>
69 #include <asm/bootinfo.h>
70 #ifdef CONFIG_MACH_DECSTATION
71 #include <asm/dec/interrupts.h>
72 #include <asm/dec/machtype.h>
73 #include <asm/dec/tc.h>
74 #include <asm/dec/ioasic_addrs.h>
76 #ifdef CONFIG_BAGET_MIPS
77 #include <asm/baget/baget.h>
78 unsigned long system_base;
83 #ifdef CONFIG_MAGIC_SYSRQ
84 #include <linux/sysrq.h>
90 * It would be nice to dynamically allocate everything that
91 * depends on NUM_SERIAL, so we could support any number of
92 * Z8530s, but for now...
94 #define NUM_SERIAL 2 /* Max number of ZS chips supported */
95 #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
96 #define CHANNEL_A_NR (zs_parms->channel_a_offset > zs_parms->channel_b_offset)
97 /* Number of channel A in the chip */
98 #define ZS_CHAN_IO_SIZE 8
99 #define ZS_CLOCK 7372800 /* Z8530 RTxC input clock rate */
101 #define RECOVERY_DELAY udelay(2)
106 int channel_a_offset;
107 int channel_b_offset;
112 static struct zs_parms *zs_parms;
114 #ifdef CONFIG_MACH_DECSTATION
115 static struct zs_parms ds_parms = {
118 channel_a_offset : 1,
119 channel_b_offset : 9,
124 #ifdef CONFIG_BAGET_MIPS
125 static struct zs_parms baget_parms = {
128 channel_a_offset : 9,
129 channel_b_offset : 1,
135 #ifdef CONFIG_MACH_DECSTATION
136 #define DS_BUS_PRESENT (IOASIC)
138 #define DS_BUS_PRESENT 0
141 #ifdef CONFIG_BAGET_MIPS
142 #define BAGET_BUS_PRESENT (mips_machtype == MACH_BAGET202)
144 #define BAGET_BUS_PRESENT 0
147 #define BUS_PRESENT (DS_BUS_PRESENT || BAGET_BUS_PRESENT)
149 struct dec_zschannel zs_channels[NUM_CHANNELS];
150 struct dec_serial zs_soft[NUM_CHANNELS];
151 int zs_channels_found;
152 struct dec_serial *zs_chain; /* list of all channels */
154 struct tty_struct zs_ttys[NUM_CHANNELS];
156 #ifdef CONFIG_SERIAL_CONSOLE
157 static struct console sercons;
159 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) \
161 static unsigned long break_pressed; /* break, really ... */
164 static unsigned char zs_init_regs[16] __initdata = {
169 (X16CLK | SB1), /* write 4 */
171 0, 0, 0, /* write 6, 7, 8 */
173 (NRZ), /* write 10 */
174 (TCBR | RCBR), /* write 11 */
175 0, 0, /* BRG time constant, write 12 + 13 */
176 (BRSRC | BRENABL), /* write 14 */
180 DECLARE_TASK_QUEUE(tq_zs_serial);
182 static struct tty_driver *serial_driver;
184 /* serial subtype definitions */
185 #define SERIAL_TYPE_NORMAL 1
187 /* number of characters left in xmit buffer before we ask for more */
188 #define WAKEUP_CHARS 256
193 #undef SERIAL_DEBUG_INTR
194 #undef SERIAL_DEBUG_OPEN
195 #undef SERIAL_DEBUG_FLOW
196 #undef SERIAL_DEBUG_THROTTLE
197 #undef SERIAL_PARANOIA_CHECK
201 #ifdef SERIAL_DEBUG_THROTTLE
202 #define _tty_name(tty,buf) tty_name(tty,buf)
205 #define RS_STROBE_TIME 10
206 #define RS_ISR_PASS_LIMIT 256
208 #define _INLINE_ inline
210 static void probe_sccs(void);
211 static void change_speed(struct dec_serial *info);
212 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
215 * tmp_buf is used as a temporary buffer by serial_write. We need to
216 * lock it in case the copy_from_user blocks while swapping in a page,
217 * and some other program tries to do a serial write at the same time.
218 * Since the lock will only come under contention when the system is
219 * swapping and available memory is low, it makes sense to share one
220 * buffer across all the serial ports, since it significantly saves
221 * memory if large numbers of serial ports are open.
223 static unsigned char tmp_buf[4096]; /* This is cheating */
224 static DECLARE_MUTEX(tmp_buf_sem);
226 static inline int serial_paranoia_check(struct dec_serial *info,
227 char *name, const char *routine)
229 #ifdef SERIAL_PARANOIA_CHECK
230 static const char *badmagic =
231 "Warning: bad magic number for serial struct %s in %s\n";
232 static const char *badinfo =
233 "Warning: null mac_serial for %s in %s\n";
236 printk(badinfo, name, routine);
239 if (info->magic != SERIAL_MAGIC) {
240 printk(badmagic, name, routine);
248 * This is used to figure out the divisor speeds and the timeouts
250 static int baud_table[] = {
251 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
252 9600, 19200, 38400, 57600, 115200, 0 };
255 * Reading and writing Z8530 registers.
257 static inline unsigned char read_zsreg(struct dec_zschannel *channel,
260 unsigned char retval;
263 *channel->control = reg & 0xf;
264 wbflush(); RECOVERY_DELAY;
266 retval = *channel->control;
271 static inline void write_zsreg(struct dec_zschannel *channel,
272 unsigned char reg, unsigned char value)
275 *channel->control = reg & 0xf;
276 wbflush(); RECOVERY_DELAY;
278 *channel->control = value;
279 wbflush(); RECOVERY_DELAY;
283 static inline unsigned char read_zsdata(struct dec_zschannel *channel)
285 unsigned char retval;
287 retval = *channel->data;
292 static inline void write_zsdata(struct dec_zschannel *channel,
295 *channel->data = value;
296 wbflush(); RECOVERY_DELAY;
300 static inline void load_zsregs(struct dec_zschannel *channel,
303 /* ZS_CLEARERR(channel);
304 ZS_CLEARFIFO(channel); */
306 write_zsreg(channel, R4, regs[R4]);
307 write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
308 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
309 write_zsreg(channel, R9, regs[R9]);
310 write_zsreg(channel, R1, regs[R1]);
311 write_zsreg(channel, R2, regs[R2]);
312 write_zsreg(channel, R10, regs[R10]);
313 write_zsreg(channel, R11, regs[R11]);
314 write_zsreg(channel, R12, regs[R12]);
315 write_zsreg(channel, R13, regs[R13]);
316 write_zsreg(channel, R14, regs[R14]);
317 write_zsreg(channel, R15, regs[R15]);
318 write_zsreg(channel, R3, regs[R3]);
319 write_zsreg(channel, R5, regs[R5]);
323 /* Sets or clears DTR/RTS on the requested line */
324 static inline void zs_rtsdtr(struct dec_serial *info, int which, int set)
329 save_flags(flags); cli();
330 if (info->zs_channel != info->zs_chan_a) {
332 info->zs_chan_a->curregs[5] |= (which & (RTS | DTR));
334 info->zs_chan_a->curregs[5] &= ~(which & (RTS | DTR));
336 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
338 restore_flags(flags);
341 /* Utility routines for the Zilog */
342 static inline int get_zsbaud(struct dec_serial *ss)
344 struct dec_zschannel *channel = ss->zs_channel;
347 /* The baud rate is split up between two 8-bit registers in
348 * what is termed 'BRG time constant' format in my docs for
349 * the chip, it is a function of the clk rate the chip is
350 * receiving which happens to be constant.
352 brg = (read_zsreg(channel, 13) << 8);
353 brg |= read_zsreg(channel, 12);
354 return BRG_TO_BPS(brg, (zs_parms->clock/(ss->clk_divisor)));
357 /* On receive, this clears errors and the receiver interrupts */
358 static inline void rs_recv_clear(struct dec_zschannel *zsc)
360 write_zsreg(zsc, 0, ERR_RES);
361 write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
365 * ----------------------------------------------------------------------
367 * Here starts the interrupt handling routines. All of the following
368 * subroutines are declared as inline and are folded into
369 * rs_interrupt(). They were separated out for readability's sake.
371 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
372 * -----------------------------------------------------------------------
375 static int tty_break; /* Set whenever BREAK condition is detected. */
378 * This routine is used by the interrupt handler to schedule
379 * processing in the software interrupt portion of the driver.
381 static _INLINE_ void rs_sched_event(struct dec_serial *info,
384 info->event |= 1 << event;
385 queue_task(&info->tqueue, &tq_zs_serial);
389 static _INLINE_ void receive_chars(struct dec_serial *info,
390 struct pt_regs *regs)
392 struct tty_struct *tty = info->tty;
393 unsigned char ch, stat, flag;
395 while ((read_zsreg(info->zs_channel, R0) & Rx_CH_AV) != 0) {
397 stat = read_zsreg(info->zs_channel, R1);
398 ch = read_zsdata(info->zs_channel);
400 if (!tty && !info->hook && !info->hook->rx_char)
405 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
406 if (info->line == sercons.index) {
407 if (!break_pressed) {
408 break_pressed = jiffies;
415 if (info->flags & ZILOG_SAK)
420 } else if (stat & FRM_ERR) {
422 } else if (stat & PAR_ERR) {
427 /* reset the error indication */
428 write_zsreg(info->zs_channel, R0, ERR_RES);
431 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
432 if (break_pressed && info->line == sercons.index) {
434 time_before(jiffies, break_pressed + HZ*5)) {
435 handle_sysrq(ch, regs, NULL);
443 if (info->hook && info->hook->rx_char) {
444 (*info->hook->rx_char)(ch, flag);
448 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
449 static int flip_buf_ovf;
455 static int flip_max_cnt;
456 if (flip_max_cnt < tty->flip.count)
457 flip_max_cnt = tty->flip.count;
460 *tty->flip.flag_buf_ptr++ = flag;
461 *tty->flip.char_buf_ptr++ = ch;
465 tty_flip_buffer_push(tty);
468 static void transmit_chars(struct dec_serial *info)
470 if ((read_zsreg(info->zs_channel, R0) & Tx_BUF_EMP) == 0)
476 write_zsdata(info->zs_channel, info->x_char);
482 if ((info->xmit_cnt <= 0) || (info->tty && info->tty->stopped)
483 || info->tx_stopped) {
484 write_zsreg(info->zs_channel, R0, RES_Tx_P);
488 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
489 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
493 if (info->xmit_cnt < WAKEUP_CHARS)
494 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
497 static _INLINE_ void status_handle(struct dec_serial *info)
501 /* Get status from Read Register 0 */
502 stat = read_zsreg(info->zs_channel, R0);
504 if (stat & BRK_ABRT) {
505 #ifdef SERIAL_DEBUG_INTR
506 printk("handling break....");
511 if (info->zs_channel != info->zs_chan_a) {
513 /* FIXEM: Check for DCD transitions */
514 if (((stat ^ info->read_reg_zero) & DCD) != 0
515 && info->tty && !C_CLOCAL(info->tty)) {
517 wake_up_interruptible(&info->open_wait);
519 tty_hangup(info->tty);
523 /* Check for CTS transitions */
524 if (info->tty && C_CRTSCTS(info->tty)) {
525 if ((stat & CTS) != 0) {
526 if (info->tx_stopped) {
527 info->tx_stopped = 0;
528 if (!info->tx_active)
529 transmit_chars(info);
532 info->tx_stopped = 1;
538 /* Clear status condition... */
539 write_zsreg(info->zs_channel, R0, RES_EXT_INT);
540 info->read_reg_zero = stat;
544 * This is the serial driver's generic interrupt routine
546 void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
548 struct dec_serial *info = (struct dec_serial *) dev_id;
549 unsigned char zs_intreg;
552 /* NOTE: The read register 3, which holds the irq status,
553 * does so for both channels on each chip. Although
554 * the status value itself must be read from the A
555 * channel and is only valid when read from channel A.
556 * Yes... broken hardware...
558 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
560 if (info->zs_chan_a == info->zs_channel)
561 shift = 3; /* Channel A */
563 shift = 0; /* Channel B */
566 zs_intreg = read_zsreg(info->zs_chan_a, R3) >> shift;
567 if ((zs_intreg & CHAN_IRQMASK) == 0)
570 if (zs_intreg & CHBRxIP) {
571 receive_chars(info, regs);
573 if (zs_intreg & CHBTxIP) {
574 transmit_chars(info);
576 if (zs_intreg & CHBEXT) {
581 /* Why do we need this ? */
582 write_zsreg(info->zs_channel, 0, RES_H_IUS);
586 void zs_dump (void) {
588 for (i = 0; i < zs_channels_found; i++) {
589 struct dec_zschannel *ch = &zs_channels[i];
590 if ((long)ch->control == UNI_IO_BASE+UNI_SCC1A_CTRL) {
591 for (j = 0; j < 15; j++) {
592 printk("W%d = 0x%x\t",
593 j, (int)ch->curregs[j]);
595 for (j = 0; j < 15; j++) {
596 printk("R%d = 0x%x\t",
597 j, (int)read_zsreg(ch,j));
606 * -------------------------------------------------------------------
607 * Here ends the serial interrupt routines.
608 * -------------------------------------------------------------------
612 * ------------------------------------------------------------
613 * rs_stop() and rs_start()
615 * This routines are called before setting or resetting tty->stopped.
616 * ------------------------------------------------------------
618 static void rs_stop(struct tty_struct *tty)
620 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
623 if (serial_paranoia_check(info, tty->name, "rs_stop"))
627 save_flags(flags); cli();
628 if (info->zs_channel->curregs[5] & TxENAB) {
629 info->zs_channel->curregs[5] &= ~TxENAB;
630 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
632 restore_flags(flags);
636 static void rs_start(struct tty_struct *tty)
638 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
641 if (serial_paranoia_check(info, tty->name, "rs_start"))
644 save_flags(flags); cli();
646 if (info->xmit_cnt && info->xmit_buf && !(info->zs_channel->curregs[5] & TxENAB)) {
647 info->zs_channel->curregs[5] |= TxENAB;
648 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
651 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
652 transmit_chars(info);
655 restore_flags(flags);
659 * This routine is used to handle the "bottom half" processing for the
660 * serial driver, known also the "software interrupt" processing.
661 * This processing is done at the kernel interrupt level, after the
662 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
663 * is where time-consuming activities which can not be done in the
664 * interrupt driver proper are done; the interrupt driver schedules
665 * them using rs_sched_event(), and they get done here.
667 static void do_serial_bh(void)
669 run_task_queue(&tq_zs_serial);
672 static void do_softint(void *private_)
674 struct dec_serial *info = (struct dec_serial *) private_;
675 struct tty_struct *tty;
681 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
686 int zs_startup(struct dec_serial * info)
690 if (info->flags & ZILOG_INITIALIZED)
693 if (!info->xmit_buf) {
694 info->xmit_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
699 save_flags(flags); cli();
701 #ifdef SERIAL_DEBUG_OPEN
702 printk("starting up ttyS%d (irq %d)...", info->line, info->irq);
706 * Clear the receive FIFO.
708 ZS_CLEARFIFO(info->zs_channel);
709 info->xmit_fifo_size = 1;
712 * Clear the interrupt registers.
714 write_zsreg(info->zs_channel, 0, ERR_RES);
715 write_zsreg(info->zs_channel, 0, RES_H_IUS);
718 * Turn on RTS and DTR.
720 zs_rtsdtr(info, RTS | DTR, 1);
723 * Finally, enable sequencing and interrupts
725 info->zs_channel->curregs[1] = (info->zs_channel->curregs[1] & ~0x18) | (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB);
726 info->zs_channel->curregs[3] |= (RxENABLE | Rx8);
727 info->zs_channel->curregs[5] |= (TxENAB | Tx8);
728 info->zs_channel->curregs[15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
729 info->zs_channel->curregs[9] |= (VIS | MIE);
730 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
731 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
732 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
733 write_zsreg(info->zs_channel, 15, info->zs_channel->curregs[15]);
734 write_zsreg(info->zs_channel, 9, info->zs_channel->curregs[9]);
737 * And clear the interrupt registers again for luck.
739 write_zsreg(info->zs_channel, 0, ERR_RES);
740 write_zsreg(info->zs_channel, 0, RES_H_IUS);
743 clear_bit(TTY_IO_ERROR, &info->tty->flags);
744 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
747 * Set the speed of the serial port
751 /* Save the current value of RR0 */
752 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
754 info->flags |= ZILOG_INITIALIZED;
755 restore_flags(flags);
760 * This routine will shutdown a serial port; interrupts are disabled, and
761 * DTR is dropped if the hangup on close termio flag is on.
763 static void shutdown(struct dec_serial * info)
767 if (!(info->flags & ZILOG_INITIALIZED))
770 #ifdef SERIAL_DEBUG_OPEN
771 printk("Shutting down serial port %d (irq %d)....", info->line,
775 save_flags(flags); cli(); /* Disable interrupts */
777 if (info->xmit_buf) {
778 free_page((unsigned long) info->xmit_buf);
782 info->zs_channel->curregs[1] = 0;
783 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]); /* no interrupts */
785 info->zs_channel->curregs[3] &= ~RxENABLE;
786 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
788 info->zs_channel->curregs[5] &= ~TxENAB;
789 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
790 if (!info->tty || C_HUPCL(info->tty)) {
791 zs_rtsdtr(info, RTS | DTR, 0);
795 set_bit(TTY_IO_ERROR, &info->tty->flags);
797 info->flags &= ~ZILOG_INITIALIZED;
798 restore_flags(flags);
802 * This routine is called to set the UART divisor registers to match
803 * the specified baud rate for a serial port.
805 static void change_speed(struct dec_serial *info)
813 if (!info->tty || !info->tty->termios)
815 cflag = info->tty->termios->c_cflag;
819 cflag = info->hook->cflags;
825 if (i < 1 || i > 2) {
827 info->tty->termios->c_cflag &= ~CBAUDEX;
829 info->hook->cflags &= ~CBAUDEX;
834 save_flags(flags); cli();
835 info->zs_baud = baud_table[i];
836 info->clk_divisor = 16;
838 info->zs_channel->curregs[4] = X16CLK;
839 brg = BPS_TO_BRG(info->zs_baud, zs_parms->clock/info->clk_divisor);
840 info->zs_channel->curregs[12] = (brg & 255);
841 info->zs_channel->curregs[13] = ((brg >> 8) & 255);
842 zs_rtsdtr(info, DTR, 1);
844 zs_rtsdtr(info, RTS | DTR, 0);
848 /* byte size and parity */
849 info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
850 info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
851 switch (cflag & CSIZE) {
854 info->zs_channel->curregs[3] |= Rx5;
855 info->zs_channel->curregs[5] |= Tx5;
859 info->zs_channel->curregs[3] |= Rx6;
860 info->zs_channel->curregs[5] |= Tx6;
864 info->zs_channel->curregs[3] |= Rx7;
865 info->zs_channel->curregs[5] |= Tx7;
868 default: /* defaults to 8 bits */
870 info->zs_channel->curregs[3] |= Rx8;
871 info->zs_channel->curregs[5] |= Tx8;
875 info->timeout = ((info->xmit_fifo_size*HZ*bits) / info->zs_baud);
876 info->timeout += HZ/50; /* Add .02 seconds of slop */
878 info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
879 if (cflag & CSTOPB) {
880 info->zs_channel->curregs[4] |= SB2;
882 info->zs_channel->curregs[4] |= SB1;
884 if (cflag & PARENB) {
885 info->zs_channel->curregs[4] |= PAR_ENA;
887 if (!(cflag & PARODD)) {
888 info->zs_channel->curregs[4] |= PAR_EVEN;
891 if (!(cflag & CLOCAL)) {
892 if (!(info->zs_channel->curregs[15] & DCDIE))
893 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
894 info->zs_channel->curregs[15] |= DCDIE;
896 info->zs_channel->curregs[15] &= ~DCDIE;
897 if (cflag & CRTSCTS) {
898 info->zs_channel->curregs[15] |= CTSIE;
899 if ((read_zsreg(info->zs_channel, 0) & CTS) == 0)
900 info->tx_stopped = 1;
902 info->zs_channel->curregs[15] &= ~CTSIE;
903 info->tx_stopped = 0;
906 /* Load up the new values */
907 load_zsregs(info->zs_channel, info->zs_channel->curregs);
909 restore_flags(flags);
912 static void rs_flush_chars(struct tty_struct *tty)
914 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
917 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
920 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
924 /* Enable transmitter */
925 save_flags(flags); cli();
926 transmit_chars(info);
927 restore_flags(flags);
930 static int rs_write(struct tty_struct * tty, int from_user,
931 const unsigned char *buf, int count)
934 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
937 if (serial_paranoia_check(info, tty->name, "rs_write"))
940 if (!tty || !info->xmit_buf)
946 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
947 SERIAL_XMIT_SIZE - info->xmit_head));
953 copy_from_user(tmp_buf, buf, c);
954 c = min_t(int, c, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
955 SERIAL_XMIT_SIZE - info->xmit_head));
956 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
959 memcpy(info->xmit_buf + info->xmit_head, buf, c);
960 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
962 restore_flags(flags);
968 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
970 transmit_chars(info);
971 restore_flags(flags);
975 static int rs_write_room(struct tty_struct *tty)
977 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
980 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
982 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
988 static int rs_chars_in_buffer(struct tty_struct *tty)
990 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
992 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
994 return info->xmit_cnt;
997 static void rs_flush_buffer(struct tty_struct *tty)
999 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1001 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1004 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1010 * ------------------------------------------------------------
1013 * This routine is called by the upper-layer tty layer to signal that
1014 * incoming characters should be throttled.
1015 * ------------------------------------------------------------
1017 static void rs_throttle(struct tty_struct * tty)
1019 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1020 unsigned long flags;
1022 #ifdef SERIAL_DEBUG_THROTTLE
1025 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1026 tty->ldisc.chars_in_buffer(tty));
1029 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1033 save_flags(flags); cli();
1034 info->x_char = STOP_CHAR(tty);
1035 if (!info->tx_active)
1036 transmit_chars(info);
1037 restore_flags(flags);
1040 if (C_CRTSCTS(tty)) {
1041 zs_rtsdtr(info, RTS, 0);
1045 static void rs_unthrottle(struct tty_struct * tty)
1047 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1048 unsigned long flags;
1050 #ifdef SERIAL_DEBUG_THROTTLE
1053 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1054 tty->ldisc.chars_in_buffer(tty));
1057 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1061 save_flags(flags); cli();
1065 info->x_char = START_CHAR(tty);
1066 if (!info->tx_active)
1067 transmit_chars(info);
1069 restore_flags(flags);
1072 if (C_CRTSCTS(tty)) {
1073 zs_rtsdtr(info, RTS, 1);
1078 * ------------------------------------------------------------
1079 * rs_ioctl() and friends
1080 * ------------------------------------------------------------
1083 static int get_serial_info(struct dec_serial * info,
1084 struct serial_struct * retinfo)
1086 struct serial_struct tmp;
1090 memset(&tmp, 0, sizeof(tmp));
1091 tmp.type = info->type;
1092 tmp.line = info->line;
1093 tmp.port = info->port;
1094 tmp.irq = info->irq;
1095 tmp.flags = info->flags;
1096 tmp.baud_base = info->baud_base;
1097 tmp.close_delay = info->close_delay;
1098 tmp.closing_wait = info->closing_wait;
1099 tmp.custom_divisor = info->custom_divisor;
1100 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
1103 static int set_serial_info(struct dec_serial * info,
1104 struct serial_struct * new_info)
1106 struct serial_struct new_serial;
1107 struct dec_serial old_info;
1112 copy_from_user(&new_serial,new_info,sizeof(new_serial));
1115 if (!capable(CAP_SYS_ADMIN)) {
1116 if ((new_serial.baud_base != info->baud_base) ||
1117 (new_serial.type != info->type) ||
1118 (new_serial.close_delay != info->close_delay) ||
1119 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1120 (info->flags & ~ZILOG_USR_MASK)))
1122 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1123 (new_serial.flags & ZILOG_USR_MASK));
1124 info->custom_divisor = new_serial.custom_divisor;
1125 goto check_and_exit;
1128 if (info->count > 1)
1132 * OK, past this point, all the error checking has been done.
1133 * At this point, we start making changes.....
1136 info->baud_base = new_serial.baud_base;
1137 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1138 (new_serial.flags & ZILOG_FLAGS));
1139 info->type = new_serial.type;
1140 info->close_delay = new_serial.close_delay;
1141 info->closing_wait = new_serial.closing_wait;
1144 retval = zs_startup(info);
1149 * get_lsr_info - get line status register info
1151 * Purpose: Let user call ioctl() to get info when the UART physically
1152 * is emptied. On bus types like RS485, the transmitter must
1153 * release the bus after transmitting. This must be done when
1154 * the transmit shift register is empty, not be done when the
1155 * transmit holding register is empty. This functionality
1156 * allows an RS485 driver to be written in user space.
1158 static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1160 unsigned char status;
1163 status = read_zsreg(info->zs_channel, 0);
1165 put_user(status,value);
1169 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1171 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1172 unsigned char control, status_a, status_b;
1173 unsigned int result;
1178 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1181 if (tty->flags & (1 << TTY_IO_ERROR))
1184 if (info->zs_channel == info->zs_chan_a)
1188 control = info->zs_chan_a->curregs[5];
1189 status_a = read_zsreg(info->zs_chan_a, 0);
1190 status_b = read_zsreg(info->zs_channel, 0);
1192 result = ((control & RTS) ? TIOCM_RTS: 0)
1193 | ((control & DTR) ? TIOCM_DTR: 0)
1194 | ((status_b & DCD) ? TIOCM_CAR: 0)
1195 | ((status_a & DCD) ? TIOCM_RNG: 0)
1196 | ((status_a & SYNC_HUNT) ? TIOCM_DSR: 0)
1197 | ((status_b & CTS) ? TIOCM_CTS: 0);
1202 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1203 unsigned int set, unsigned int clear)
1205 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1207 unsigned int arg, bits;
1212 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1215 if (tty->flags & (1 << TTY_IO_ERROR))
1218 if (info->zs_channel == info->zs_chan_a)
1222 if (set & TIOCM_RTS)
1223 info->zs_chan_a->curregs[5] |= RTS;
1224 if (set & TIOCM_DTR)
1225 info->zs_chan_a->curregs[5] |= DTR;
1226 if (clear & TIOCM_RTS)
1227 info->zs_chan_a->curregs[5] &= ~RTS;
1228 if (clear & TIOCM_DTR)
1229 info->zs_chan_a->curregs[5] &= ~DTR;
1230 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1236 * rs_break - turn transmit break condition on/off
1238 static void rs_break(struct tty_struct *tty, int break_state)
1240 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1241 unsigned long flags;
1243 if (serial_paranoia_check(info, tty->name, "rs_break"))
1248 save_flags(flags); cli();
1249 if (break_state == -1)
1250 info->zs_channel->curregs[5] |= SND_BRK;
1252 info->zs_channel->curregs[5] &= ~SND_BRK;
1253 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1254 restore_flags(flags);
1257 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1258 unsigned int cmd, unsigned long arg)
1261 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1266 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1269 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1270 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1271 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1272 if (tty->flags & (1 << TTY_IO_ERROR))
1278 error = verify_area(VERIFY_WRITE, (void *) arg,
1279 sizeof(struct serial_struct));
1282 return get_serial_info(info,
1283 (struct serial_struct *) arg);
1285 return set_serial_info(info,
1286 (struct serial_struct *) arg);
1287 case TIOCSERGETLSR: /* Get line status register */
1288 error = verify_area(VERIFY_WRITE, (void *) arg,
1289 sizeof(unsigned int));
1293 return get_lsr_info(info, (unsigned int *) arg);
1295 case TIOCSERGSTRUCT:
1296 error = verify_area(VERIFY_WRITE, (void *) arg,
1297 sizeof(struct dec_serial));
1300 copy_from_user((struct dec_serial *) arg,
1301 info, sizeof(struct dec_serial));
1305 return -ENOIOCTLCMD;
1310 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1312 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1315 if (tty->termios->c_cflag == old_termios->c_cflag)
1317 was_stopped = info->tx_stopped;
1321 if (was_stopped && !info->tx_stopped)
1326 * ------------------------------------------------------------
1329 * This routine is called when the serial port gets closed.
1330 * Wait for the last remaining data to be sent.
1331 * ------------------------------------------------------------
1333 static void rs_close(struct tty_struct *tty, struct file * filp)
1335 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1336 unsigned long flags;
1338 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1341 save_flags(flags); cli();
1343 if (tty_hung_up_p(filp)) {
1344 restore_flags(flags);
1348 #ifdef SERIAL_DEBUG_OPEN
1349 printk("rs_close ttyS%d, count = %d\n", info->line, info->count);
1351 if ((tty->count == 1) && (info->count != 1)) {
1353 * Uh, oh. tty->count is 1, which means that the tty
1354 * structure will be freed. Info->count should always
1355 * be one in these conditions. If it's greater than
1356 * one, we've got real problems, since it means the
1357 * serial port won't be shutdown.
1359 printk("rs_close: bad serial port count; tty->count is 1, "
1360 "info->count is %d\n", info->count);
1363 if (--info->count < 0) {
1364 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1365 info->line, info->count);
1369 restore_flags(flags);
1372 info->flags |= ZILOG_CLOSING;
1374 * Now we wait for the transmit buffer to clear; and we notify
1375 * the line discipline to only process XON/XOFF characters.
1378 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1379 tty_wait_until_sent(tty, info->closing_wait);
1381 * At this point we stop accepting input. To do this, we
1382 * disable the receiver and receive interrupts.
1384 info->zs_channel->curregs[3] &= ~RxENABLE;
1385 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1386 info->zs_channel->curregs[1] = 0; /* disable any rx ints */
1387 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1388 ZS_CLEARFIFO(info->zs_channel);
1389 if (info->flags & ZILOG_INITIALIZED) {
1391 * Before we drop DTR, make sure the SCC transmitter
1392 * has completely drained.
1394 rs_wait_until_sent(tty, info->timeout);
1398 if (tty->driver->flush_buffer)
1399 tty->driver->flush_buffer(tty);
1400 tty_ldisc_flush(tty);
1404 if (info->blocked_open) {
1405 if (info->close_delay) {
1406 current->state = TASK_INTERRUPTIBLE;
1407 schedule_timeout(info->close_delay);
1409 wake_up_interruptible(&info->open_wait);
1411 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CLOSING);
1412 wake_up_interruptible(&info->close_wait);
1413 restore_flags(flags);
1417 * rs_wait_until_sent() --- wait until the transmitter is empty
1419 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1421 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1422 unsigned long orig_jiffies, char_time;
1424 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1427 orig_jiffies = jiffies;
1429 * Set the check interval to be 1/5 of the estimated time to
1430 * send a single character, and make it at least 1. The check
1431 * interval should also be less than the timeout.
1433 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1434 char_time = char_time / 5;
1438 char_time = min_t(unsigned long, char_time, timeout);
1439 while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
1440 current->state = TASK_INTERRUPTIBLE;
1441 schedule_timeout(char_time);
1442 if (signal_pending(current))
1444 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1447 current->state = TASK_RUNNING;
1451 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1453 void rs_hangup(struct tty_struct *tty)
1455 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1457 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1460 rs_flush_buffer(tty);
1464 info->flags &= ~ZILOG_NORMAL_ACTIVE;
1466 wake_up_interruptible(&info->open_wait);
1470 * ------------------------------------------------------------
1471 * rs_open() and friends
1472 * ------------------------------------------------------------
1474 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1475 struct dec_serial *info)
1477 DECLARE_WAITQUEUE(wait, current);
1482 * If the device is in the middle of being closed, then block
1483 * until it's done, and then try again.
1485 if (info->flags & ZILOG_CLOSING) {
1486 interruptible_sleep_on(&info->close_wait);
1487 #ifdef SERIAL_DO_RESTART
1488 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1489 -EAGAIN : -ERESTARTSYS);
1496 * If this is a callout device, then just make sure the normal
1497 * device isn't being used.
1501 * If non-blocking mode is set, or the port is not enabled,
1502 * then make the check up front and then exit.
1504 if ((filp->f_flags & O_NONBLOCK) ||
1505 (tty->flags & (1 << TTY_IO_ERROR))) {
1506 info->flags |= ZILOG_NORMAL_ACTIVE;
1510 if (tty->termios->c_cflag & CLOCAL)
1514 * Block waiting for the carrier detect and the line to become
1515 * free (i.e., not in use by the callout). While we are in
1516 * this loop, info->count is dropped by one, so that
1517 * rs_close() knows when to free things. We restore it upon
1518 * exit, either normal or abnormal.
1521 add_wait_queue(&info->open_wait, &wait);
1522 #ifdef SERIAL_DEBUG_OPEN
1523 printk("block_til_ready before block: ttyS%d, count = %d\n",
1524 info->line, info->count);
1527 if (!tty_hung_up_p(filp))
1530 info->blocked_open++;
1533 if (tty->termios->c_cflag & CBAUD)
1534 zs_rtsdtr(info, RTS | DTR, 1);
1536 set_current_state(TASK_INTERRUPTIBLE);
1537 if (tty_hung_up_p(filp) ||
1538 !(info->flags & ZILOG_INITIALIZED)) {
1539 #ifdef SERIAL_DO_RESTART
1540 if (info->flags & ZILOG_HUP_NOTIFY)
1543 retval = -ERESTARTSYS;
1549 if (!(info->flags & ZILOG_CLOSING) &&
1550 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1552 if (signal_pending(current)) {
1553 retval = -ERESTARTSYS;
1556 #ifdef SERIAL_DEBUG_OPEN
1557 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1558 info->line, info->count);
1562 current->state = TASK_RUNNING;
1563 remove_wait_queue(&info->open_wait, &wait);
1564 if (!tty_hung_up_p(filp))
1566 info->blocked_open--;
1567 #ifdef SERIAL_DEBUG_OPEN
1568 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1569 info->line, info->count);
1573 info->flags |= ZILOG_NORMAL_ACTIVE;
1578 * This routine is called whenever a serial port is opened. It
1579 * enables interrupts for a serial port, linking in its ZILOG structure into
1580 * the IRQ chain. It also performs the serial-specific
1581 * initialization for the tty structure.
1583 int rs_open(struct tty_struct *tty, struct file * filp)
1585 struct dec_serial *info;
1589 if ((line < 0) || (line >= zs_channels_found))
1591 info = zs_soft + line;
1596 if (serial_paranoia_check(info, tty->name, "rs_open"))
1598 #ifdef SERIAL_DEBUG_OPEN
1599 printk("rs_open %s, count = %d\n", tty->name, info->count);
1603 tty->driver_data = info;
1607 * If the port is the middle of closing, bail out now
1609 if (tty_hung_up_p(filp) ||
1610 (info->flags & ZILOG_CLOSING)) {
1611 if (info->flags & ZILOG_CLOSING)
1612 interruptible_sleep_on(&info->close_wait);
1613 #ifdef SERIAL_DO_RESTART
1614 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1615 -EAGAIN : -ERESTARTSYS);
1622 * Start up serial port
1624 retval = zs_startup(info);
1628 retval = block_til_ready(tty, filp, info);
1630 #ifdef SERIAL_DEBUG_OPEN
1631 printk("rs_open returning after block_til_ready with %d\n",
1637 #ifdef CONFIG_SERIAL_CONSOLE
1638 if (sercons.cflag && sercons.index == line) {
1639 tty->termios->c_cflag = sercons.cflag;
1645 #ifdef SERIAL_DEBUG_OPEN
1646 printk("rs_open %s successful...", tty->name);
1648 /* tty->low_latency = 1; */
1652 /* Finally, routines used to initialize the serial driver. */
1654 static void __init show_serial_version(void)
1656 printk("DECstation Z8530 serial driver version 0.05\n");
1659 /* Initialize Z8530s zs_channels
1662 static void __init probe_sccs(void)
1664 struct dec_serial **pp;
1665 int i, n, n_chips = 0, n_channels, chip, channel;
1668 * did we get here by accident?
1671 printk("Not on JUNKIO machine, skipping probe_sccs\n");
1676 * When serial console is activated, tc_init has not been called yet
1677 * and system_base is undefined. Unfortunately we have to hardcode
1678 * system_base for this case :-(. HK
1680 switch(mips_machtype) {
1681 #ifdef CONFIG_MACH_DECSTATION
1682 case MACH_DS5000_2X0:
1683 system_base = 0xbf800000;
1685 zs_parms = &ds_parms;
1687 case MACH_DS5000_1XX:
1688 system_base = 0xbc000000;
1690 zs_parms = &ds_parms;
1692 case MACH_DS5000_XX:
1693 system_base = 0xbc000000;
1695 zs_parms = &ds_parms;
1698 #ifdef CONFIG_BAGET_MIPS
1700 system_base = UNI_IO_BASE;
1702 zs_parms = &baget_parms;
1703 zs_init_regs[2] = 0x8;
1707 panic("zs: unsupported bus");
1710 panic("zs: uninitialized parms");
1716 for (chip = 0; chip < n_chips; chip++) {
1717 for (channel = 0; channel <= 1; channel++) {
1719 * The sccs reside on the high byte of the 16 bit IOBUS
1721 zs_channels[n_channels].control =
1722 (volatile unsigned char *)system_base +
1723 (0 == chip ? zs_parms->scc0 : zs_parms->scc1) +
1724 (0 == channel ? zs_parms->channel_a_offset :
1725 zs_parms->channel_b_offset);
1726 zs_channels[n_channels].data =
1727 zs_channels[n_channels].control + 4;
1729 #ifndef CONFIG_SERIAL_CONSOLE
1731 * We're called early and memory managment isn't up, yet.
1732 * Thus check_region would fail.
1734 if (!request_region((unsigned long)
1735 zs_channels[n_channels].control,
1736 ZS_CHAN_IO_SIZE, "SCC"))
1737 panic("SCC I/O region is not free");
1739 zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1740 zs_soft[n_channels].irq = zs_parms->irq;
1743 * Identification of channel A. Location of channel A
1744 * inside chip depends on mapping of internal address
1745 * the chip decodes channels by.
1746 * CHANNEL_A_NR returns either 0 (in case of
1747 * DECstations) or 1 (in case of Baget).
1749 if (CHANNEL_A_NR == channel)
1750 zs_soft[n_channels].zs_chan_a =
1751 &zs_channels[n_channels+1-2*CHANNEL_A_NR];
1753 zs_soft[n_channels].zs_chan_a =
1754 &zs_channels[n_channels];
1756 *pp = &zs_soft[n_channels];
1757 pp = &zs_soft[n_channels].zs_next;
1763 zs_channels_found = n_channels;
1765 for (n = 0; n < zs_channels_found; n++) {
1766 for (i = 0; i < 16; i++) {
1767 zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1771 /* save_and_cli(flags);
1772 for (n = 0; n < zs_channels_found; n++) {
1773 if (((int)zs_channels[n].control & 0xf) == 1) {
1774 write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
1776 write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
1778 load_zsregs(zs_soft[n].zs_channel, zs_soft[n].zs_channel->curregs);
1780 restore_flags(flags); */
1783 static struct tty_operations serial_ops = {
1787 .flush_chars = rs_flush_chars,
1788 .write_room = rs_write_room,
1789 .chars_in_buffer = rs_chars_in_buffer,
1790 .flush_buffer = rs_flush_buffer,
1792 .throttle = rs_throttle,
1793 .unthrottle = rs_unthrottle,
1794 .set_termios = rs_set_termios,
1797 .hangup = rs_hangup,
1798 .break_ctl = rs_break,
1799 .wait_until_sent = rs_wait_until_sent,
1800 .tiocmget = rs_tiocmget,
1801 .tiocmset = rs_tiocmset,
1804 /* zs_init inits the driver */
1805 int __init zs_init(void)
1808 unsigned long flags;
1809 struct dec_serial *info;
1814 /* Setup base handler, and timer table. */
1815 init_bh(SERIAL_BH, do_serial_bh);
1817 /* Find out how many Z8530 SCCs we have */
1821 serial_driver = alloc_tty_driver(zs_channels_found);
1825 show_serial_version();
1827 /* Initialize the tty_driver structure */
1828 /* Not all of this is exactly right for us. */
1830 serial_driver->owner = THIS_MODULE;
1831 serial_driver->devfs_name = "tts/";
1832 serial_driver->name = "ttyS";
1833 serial_driver->major = TTY_MAJOR;
1834 serial_driver->minor_start = 64;
1835 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1836 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1837 serial_driver->init_termios = tty_std_termios;
1838 serial_driver->init_termios.c_cflag =
1839 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1840 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1841 tty_set_operations(serial_driver, &serial_ops);
1843 if (tty_register_driver(serial_driver))
1844 panic("Couldn't register serial driver\n");
1846 save_flags(flags); cli();
1848 for (channel = 0; channel < zs_channels_found; ++channel) {
1849 if (zs_soft[channel].hook &&
1850 zs_soft[channel].hook->init_channel)
1851 (*zs_soft[channel].hook->init_channel)
1852 (&zs_soft[channel]);
1854 zs_soft[channel].clk_divisor = 16;
1855 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1857 if (request_irq(zs_parms->irq, rs_interrupt, SA_SHIRQ,
1858 "SCC", &zs_soft[channel]))
1859 printk(KERN_ERR "decserial: can't get irq %d\n",
1863 for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
1865 if (info->hook && info->hook->init_info) {
1866 (*info->hook->init_info)(info);
1869 info->magic = SERIAL_MAGIC;
1870 info->port = (int) info->zs_channel->control;
1873 info->custom_divisor = 16;
1874 info->close_delay = 50;
1875 info->closing_wait = 3000;
1879 info->blocked_open = 0;
1880 info->tqueue.routine = do_softint;
1881 info->tqueue.data = info;
1882 init_waitqueue_head(&info->open_wait);
1883 init_waitqueue_head(&info->close_wait);
1884 printk("ttyS%d at 0x%08x (irq = %d)", info->line,
1885 info->port, info->irq);
1886 printk(" is a Z85C30 SCC\n");
1887 tty_register_device(serial_driver, info->line, NULL);
1890 restore_flags(flags);
1896 * register_serial and unregister_serial allows for serial ports to be
1897 * configured at run-time, to support PCMCIA modems.
1899 /* PowerMac: Unused at this time, just here to make things link. */
1900 int register_serial(struct serial_struct *req)
1905 void unregister_serial(int line)
1911 * polling I/O routines
1914 zs_poll_tx_char(struct dec_serial *info, unsigned char ch)
1916 struct dec_zschannel *chan = info->zs_channel;
1921 // int nine = read_zsreg(chan, R9);
1924 // write_zsreg(chan, R9, nine & ~MIE);
1928 while (!(*(chan->control) & Tx_BUF_EMP) && --loops)
1939 // write_zsreg(chan, R9, nine);
1950 zs_poll_rx_char(struct dec_serial *info)
1952 struct dec_zschannel *chan = info->zs_channel;
1958 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
1962 ret = read_zsdata(chan);
1971 unsigned int register_zs_hook(unsigned int channel, struct zs_hook *hook)
1973 struct dec_serial *info = &zs_soft[channel];
1976 printk("%s: line %d has already a hook registered\n",
1977 __FUNCTION__, channel);
1986 if (!(info->flags & ZILOG_INITIALIZED))
1989 hook->poll_rx_char = zs_poll_rx_char;
1990 hook->poll_tx_char = zs_poll_tx_char;
1996 unsigned int unregister_zs_hook(unsigned int channel)
1998 struct dec_serial *info = &zs_soft[channel];
2004 printk("%s: trying to unregister hook on line %d,"
2005 " but none is registered\n", __FUNCTION__, channel);
2011 * ------------------------------------------------------------
2012 * Serial console driver
2013 * ------------------------------------------------------------
2015 #ifdef CONFIG_SERIAL_CONSOLE
2019 * Print a string to the serial port trying not to disturb
2020 * any possible real use of the port...
2022 static void serial_console_write(struct console *co, const char *s,
2025 struct dec_serial *info;
2028 info = zs_soft + co->index;
2030 for (i = 0; i < count; i++, s++) {
2032 zs_poll_tx_char(info, '\r');
2033 zs_poll_tx_char(info, *s);
2037 static struct tty_driver *serial_console_device(struct console *c, int *index)
2040 return serial_driver;
2044 * Setup initial baud/bits/parity. We do two things here:
2045 * - construct a cflag setting for the first rs_open()
2046 * - initialize the serial port
2047 * Return non-zero if we didn't find a serial port.
2049 static int __init serial_console_setup(struct console *co, char *options)
2051 struct dec_serial *info;
2055 int cflag = CREAD | HUPCL | CLOCAL;
2057 unsigned long flags;
2062 info = zs_soft + co->index;
2070 baud = simple_strtoul(options, NULL, 10);
2072 while(*s >= '0' && *s <= '9')
2081 * Now construct a cflag setting.
2129 save_and_cli(flags);
2132 * Turn on RTS and DTR.
2134 zs_rtsdtr(info, RTS | DTR, 1);
2137 * Finally, enable sequencing
2139 info->zs_channel->curregs[3] |= (RxENABLE | Rx8);
2140 info->zs_channel->curregs[5] |= (TxENAB | Tx8);
2141 info->zs_channel->curregs[9] |= (VIS);
2142 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
2143 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
2144 write_zsreg(info->zs_channel, 9, info->zs_channel->curregs[9]);
2147 * Clear the interrupt registers.
2149 write_zsreg(info->zs_channel, 0, ERR_RES);
2150 write_zsreg(info->zs_channel, 0, RES_H_IUS);
2153 * Set the speed of the serial port
2157 /* Save the current value of RR0 */
2158 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
2160 zs_soft[co->index].clk_divisor = 16;
2161 zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
2163 restore_flags(flags);
2168 static struct console sercons = {
2170 .write = serial_console_write,
2171 .device = serial_console_device,
2172 .setup = serial_console_setup,
2173 .flags = CON_PRINTBUFFER,
2180 void __init zs_serial_console_init(void)
2182 register_console(&sercons);
2184 #endif /* ifdef CONFIG_SERIAL_CONSOLE */
2187 struct dec_zschannel *zs_kgdbchan;
2188 static unsigned char scc_inittab[] = {
2189 9, 0x80, /* reset A side (CHRA) */
2190 13, 0, /* set baud rate divisor */
2192 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
2193 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
2194 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
2195 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
2196 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
2199 /* These are for receiving and sending characters under the kgdb
2200 * source level kernel debugger.
2202 void putDebugChar(char kgdb_char)
2204 struct dec_zschannel *chan = zs_kgdbchan;
2205 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2207 write_zsdata(chan, kgdb_char);
2209 char getDebugChar(void)
2211 struct dec_zschannel *chan = zs_kgdbchan;
2212 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2213 eieio(); /*barrier();*/
2214 return read_zsdata(chan);
2216 void kgdb_interruptible(int yes)
2218 struct dec_zschannel *chan = zs_kgdbchan;
2220 nine = read_zsreg(chan, 9);
2222 one = EXT_INT_ENAB|INT_ALL_Rx;
2224 printk("turning serial ints on\n");
2228 printk("turning serial ints off\n");
2230 write_zsreg(chan, 1, one);
2231 write_zsreg(chan, 9, nine);
2234 static int kgdbhook_init_channel(struct dec_serial* info)
2239 static void kgdbhook_init_info(struct dec_serial* info)
2243 static void kgdbhook_rx_char(struct dec_serial* info,
2244 unsigned char ch, unsigned char stat)
2246 if (ch == 0x03 || ch == '$')
2248 if (stat & (Rx_OVR|FRM_ERR|PAR_ERR))
2249 write_zsreg(info->zs_channel, 0, ERR_RES);
2252 /* This sets up the serial port we're using, and turns on
2253 * interrupts for that channel, so kgdb is usable once we're done.
2255 static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2259 volatile char *sccc = ms->control;
2260 brg = BPS_TO_BRG(bps, zs_parms->clock/16);
2261 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2262 for (i = 20000; i != 0; --i) {
2265 for (i = 0; i < sizeof(scc_inittab); ++i) {
2266 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2270 /* This is called at boot time to prime the kgdb serial debugging
2271 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2272 * for /dev/ttyb which is determined in setup_arch() from the
2273 * boot command line flags.
2275 struct zs_hook zs_kgdbhook = {
2276 init_channel : kgdbhook_init_channel,
2277 init_info : kgdbhook_init_info,
2278 cflags : B38400|CS8|CLOCAL,
2279 rx_char : kgdbhook_rx_char,
2282 void __init zs_kgdb_hook(int tty_num)
2284 /* Find out how many Z8530 SCCs we have */
2287 zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2288 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2289 zs_soft[tty_num].change_needed = 0;
2290 zs_soft[tty_num].clk_divisor = 16;
2291 zs_soft[tty_num].zs_baud = 38400;
2292 zs_soft[tty_num].hook = &zs_kgdbhook; /* This runs kgdb */
2293 /* Turn on transmitter/receiver at 8-bits/char */
2294 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2295 printk("KGDB: on channel %d initialized\n", tty_num);
2296 set_debug_traps(); /* init stub */
2298 #endif /* ifdef CONFIG_KGDB */