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, 2002, 2003, 2004, 2005 Maciej W. Rozycki
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 #include <linux/spinlock.h>
59 #ifdef CONFIG_SERIAL_DEC_CONSOLE
60 #include <linux/console.h>
64 #include <asm/pgtable.h>
66 #include <asm/system.h>
67 #include <asm/bootinfo.h>
69 #include <asm/dec/interrupts.h>
70 #include <asm/dec/ioasic_addrs.h>
71 #include <asm/dec/machtype.h>
72 #include <asm/dec/serial.h>
73 #include <asm/dec/system.h>
74 #include <asm/dec/tc.h>
79 #ifdef CONFIG_MAGIC_SYSRQ
80 #include <linux/sysrq.h>
86 * It would be nice to dynamically allocate everything that
87 * depends on NUM_SERIAL, so we could support any number of
88 * Z8530s, but for now...
90 #define NUM_SERIAL 2 /* Max number of ZS chips supported */
91 #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
92 #define CHANNEL_A_NR (zs_parms->channel_a_offset > zs_parms->channel_b_offset)
93 /* Number of channel A in the chip */
94 #define ZS_CHAN_IO_SIZE 8
95 #define ZS_CLOCK 7372800 /* Z8530 RTxC input clock rate */
97 #define RECOVERY_DELAY udelay(2)
102 int channel_a_offset;
103 int channel_b_offset;
109 static struct zs_parms *zs_parms;
111 #ifdef CONFIG_MACH_DECSTATION
112 static struct zs_parms ds_parms = {
115 channel_a_offset : 1,
116 channel_b_offset : 9,
123 #ifdef CONFIG_MACH_DECSTATION
124 #define DS_BUS_PRESENT (IOASIC)
126 #define DS_BUS_PRESENT 0
129 #define BUS_PRESENT (DS_BUS_PRESENT)
131 DEFINE_SPINLOCK(zs_lock);
133 struct dec_zschannel zs_channels[NUM_CHANNELS];
134 struct dec_serial zs_soft[NUM_CHANNELS];
135 int zs_channels_found;
136 struct dec_serial *zs_chain; /* list of all channels */
138 struct tty_struct zs_ttys[NUM_CHANNELS];
140 #ifdef CONFIG_SERIAL_DEC_CONSOLE
141 static struct console sercons;
143 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
145 static unsigned long break_pressed; /* break, really ... */
148 static unsigned char zs_init_regs[16] __initdata = {
153 (X16CLK), /* write 4 */
155 0, 0, 0, /* write 6, 7, 8 */
156 (MIE | DLC | NV), /* write 9 */
157 (NRZ), /* write 10 */
158 (TCBR | RCBR), /* write 11 */
159 0, 0, /* BRG time constant, write 12 + 13 */
160 (BRSRC | BRENABL), /* write 14 */
164 static struct tty_driver *serial_driver;
166 /* serial subtype definitions */
167 #define SERIAL_TYPE_NORMAL 1
169 /* number of characters left in xmit buffer before we ask for more */
170 #define WAKEUP_CHARS 256
175 #undef SERIAL_DEBUG_OPEN
176 #undef SERIAL_DEBUG_FLOW
177 #undef SERIAL_DEBUG_THROTTLE
178 #undef SERIAL_PARANOIA_CHECK
182 #ifdef SERIAL_DEBUG_THROTTLE
183 #define _tty_name(tty,buf) tty_name(tty,buf)
186 #define RS_STROBE_TIME 10
187 #define RS_ISR_PASS_LIMIT 256
189 static void probe_sccs(void);
190 static void change_speed(struct dec_serial *info);
191 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
193 static inline int serial_paranoia_check(struct dec_serial *info,
194 char *name, const char *routine)
196 #ifdef SERIAL_PARANOIA_CHECK
197 static const char *badmagic =
198 "Warning: bad magic number for serial struct %s in %s\n";
199 static const char *badinfo =
200 "Warning: null mac_serial for %s in %s\n";
203 printk(badinfo, name, routine);
206 if (info->magic != SERIAL_MAGIC) {
207 printk(badmagic, name, routine);
215 * This is used to figure out the divisor speeds and the timeouts
217 static int baud_table[] = {
218 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
219 9600, 19200, 38400, 57600, 115200, 0 };
222 * Reading and writing Z8530 registers.
224 static inline unsigned char read_zsreg(struct dec_zschannel *channel,
227 unsigned char retval;
230 *channel->control = reg & 0xf;
231 fast_iob(); RECOVERY_DELAY;
233 retval = *channel->control;
238 static inline void write_zsreg(struct dec_zschannel *channel,
239 unsigned char reg, unsigned char value)
242 *channel->control = reg & 0xf;
243 fast_iob(); RECOVERY_DELAY;
245 *channel->control = value;
246 fast_iob(); RECOVERY_DELAY;
250 static inline unsigned char read_zsdata(struct dec_zschannel *channel)
252 unsigned char retval;
254 retval = *channel->data;
259 static inline void write_zsdata(struct dec_zschannel *channel,
262 *channel->data = value;
263 fast_iob(); RECOVERY_DELAY;
267 static inline void load_zsregs(struct dec_zschannel *channel,
270 /* ZS_CLEARERR(channel);
271 ZS_CLEARFIFO(channel); */
273 write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
274 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
275 write_zsreg(channel, R4, regs[R4]);
276 write_zsreg(channel, R9, regs[R9]);
277 write_zsreg(channel, R1, regs[R1]);
278 write_zsreg(channel, R2, regs[R2]);
279 write_zsreg(channel, R10, regs[R10]);
280 write_zsreg(channel, R11, regs[R11]);
281 write_zsreg(channel, R12, regs[R12]);
282 write_zsreg(channel, R13, regs[R13]);
283 write_zsreg(channel, R14, regs[R14]);
284 write_zsreg(channel, R15, regs[R15]);
285 write_zsreg(channel, R3, regs[R3]);
286 write_zsreg(channel, R5, regs[R5]);
290 /* Sets or clears DTR/RTS on the requested line */
291 static inline void zs_rtsdtr(struct dec_serial *info, int which, int set)
295 spin_lock_irqsave(&zs_lock, flags);
296 if (info->zs_channel != info->zs_chan_a) {
298 info->zs_chan_a->curregs[5] |= (which & (RTS | DTR));
300 info->zs_chan_a->curregs[5] &= ~(which & (RTS | DTR));
302 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
304 spin_unlock_irqrestore(&zs_lock, flags);
307 /* Utility routines for the Zilog */
308 static inline int get_zsbaud(struct dec_serial *ss)
310 struct dec_zschannel *channel = ss->zs_channel;
313 /* The baud rate is split up between two 8-bit registers in
314 * what is termed 'BRG time constant' format in my docs for
315 * the chip, it is a function of the clk rate the chip is
316 * receiving which happens to be constant.
318 brg = (read_zsreg(channel, 13) << 8);
319 brg |= read_zsreg(channel, 12);
320 return BRG_TO_BPS(brg, (zs_parms->clock/(ss->clk_divisor)));
323 /* On receive, this clears errors and the receiver interrupts */
324 static inline void rs_recv_clear(struct dec_zschannel *zsc)
326 write_zsreg(zsc, 0, ERR_RES);
327 write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
331 * ----------------------------------------------------------------------
333 * Here starts the interrupt handling routines. All of the following
334 * subroutines are declared as inline and are folded into
335 * rs_interrupt(). They were separated out for readability's sake.
337 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
338 * -----------------------------------------------------------------------
342 * This routine is used by the interrupt handler to schedule
343 * processing in the software interrupt portion of the driver.
345 static void rs_sched_event(struct dec_serial *info, int event)
347 info->event |= 1 << event;
348 tasklet_schedule(&info->tlet);
351 static void receive_chars(struct dec_serial *info, struct pt_regs *regs)
353 struct tty_struct *tty = info->tty;
354 unsigned char ch, stat, flag;
356 while ((read_zsreg(info->zs_channel, R0) & Rx_CH_AV) != 0) {
358 stat = read_zsreg(info->zs_channel, R1);
359 ch = read_zsdata(info->zs_channel);
361 if (!tty && (!info->hook || !info->hook->rx_char))
365 if (info->tty_break) {
368 if (info->flags & ZILOG_SAK)
370 /* Ignore the null char got when BREAK is removed. */
376 } else if (stat & FRM_ERR) {
378 } else if (stat & PAR_ERR) {
381 if (flag != TTY_NORMAL)
382 /* reset the error indication */
383 write_zsreg(info->zs_channel, R0, ERR_RES);
386 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
388 if (break_pressed && info->line == sercons.index) {
389 /* Ignore the null char got when BREAK is removed. */
392 if (time_before(jiffies, break_pressed + HZ * 5)) {
393 handle_sysrq(ch, regs, NULL);
401 if (info->hook && info->hook->rx_char) {
402 (*info->hook->rx_char)(ch, flag);
406 tty_insert_flip_char(tty, ch, flag);
409 tty_flip_buffer_push(tty);
412 static void transmit_chars(struct dec_serial *info)
414 if ((read_zsreg(info->zs_channel, R0) & Tx_BUF_EMP) == 0)
420 write_zsdata(info->zs_channel, info->x_char);
426 if ((info->xmit_cnt <= 0) || (info->tty && info->tty->stopped)
427 || info->tx_stopped) {
428 write_zsreg(info->zs_channel, R0, RES_Tx_P);
432 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
433 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
437 if (info->xmit_cnt < WAKEUP_CHARS)
438 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
441 static void status_handle(struct dec_serial *info)
445 /* Get status from Read Register 0 */
446 stat = read_zsreg(info->zs_channel, R0);
448 if ((stat & BRK_ABRT) && !(info->read_reg_zero & BRK_ABRT)) {
449 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
451 if (info->line == sercons.index) {
453 break_pressed = jiffies;
459 if (info->zs_channel != info->zs_chan_a) {
461 /* Check for DCD transitions */
462 if (info->tty && !C_CLOCAL(info->tty) &&
463 ((stat ^ info->read_reg_zero) & DCD) != 0 ) {
465 wake_up_interruptible(&info->open_wait);
467 tty_hangup(info->tty);
471 /* Check for CTS transitions */
472 if (info->tty && C_CRTSCTS(info->tty)) {
473 if ((stat & CTS) != 0) {
474 if (info->tx_stopped) {
475 info->tx_stopped = 0;
476 if (!info->tx_active)
477 transmit_chars(info);
480 info->tx_stopped = 1;
486 /* Clear status condition... */
487 write_zsreg(info->zs_channel, R0, RES_EXT_INT);
488 info->read_reg_zero = stat;
492 * This is the serial driver's generic interrupt routine
494 static irqreturn_t rs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
496 struct dec_serial *info = (struct dec_serial *) dev_id;
497 irqreturn_t status = IRQ_NONE;
498 unsigned char zs_intreg;
501 /* NOTE: The read register 3, which holds the irq status,
502 * does so for both channels on each chip. Although
503 * the status value itself must be read from the A
504 * channel and is only valid when read from channel A.
505 * Yes... broken hardware...
507 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
509 if (info->zs_chan_a == info->zs_channel)
510 shift = 3; /* Channel A */
512 shift = 0; /* Channel B */
515 zs_intreg = read_zsreg(info->zs_chan_a, R3) >> shift;
516 if ((zs_intreg & CHAN_IRQMASK) == 0)
519 status = IRQ_HANDLED;
521 if (zs_intreg & CHBRxIP) {
522 receive_chars(info, regs);
524 if (zs_intreg & CHBTxIP) {
525 transmit_chars(info);
527 if (zs_intreg & CHBEXT) {
532 /* Why do we need this ? */
533 write_zsreg(info->zs_channel, 0, RES_H_IUS);
539 void zs_dump (void) {
541 for (i = 0; i < zs_channels_found; i++) {
542 struct dec_zschannel *ch = &zs_channels[i];
543 if ((long)ch->control == UNI_IO_BASE+UNI_SCC1A_CTRL) {
544 for (j = 0; j < 15; j++) {
545 printk("W%d = 0x%x\t",
546 j, (int)ch->curregs[j]);
548 for (j = 0; j < 15; j++) {
549 printk("R%d = 0x%x\t",
550 j, (int)read_zsreg(ch,j));
559 * -------------------------------------------------------------------
560 * Here ends the serial interrupt routines.
561 * -------------------------------------------------------------------
565 * ------------------------------------------------------------
566 * rs_stop() and rs_start()
568 * This routines are called before setting or resetting tty->stopped.
569 * ------------------------------------------------------------
571 static void rs_stop(struct tty_struct *tty)
573 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
576 if (serial_paranoia_check(info, tty->name, "rs_stop"))
580 spin_lock_irqsave(&zs_lock, flags);
581 if (info->zs_channel->curregs[5] & TxENAB) {
582 info->zs_channel->curregs[5] &= ~TxENAB;
583 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
585 spin_unlock_irqrestore(&zs_lock, flags);
589 static void rs_start(struct tty_struct *tty)
591 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
594 if (serial_paranoia_check(info, tty->name, "rs_start"))
597 spin_lock_irqsave(&zs_lock, flags);
599 if (info->xmit_cnt && info->xmit_buf && !(info->zs_channel->curregs[5] & TxENAB)) {
600 info->zs_channel->curregs[5] |= TxENAB;
601 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
604 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
605 transmit_chars(info);
608 spin_unlock_irqrestore(&zs_lock, flags);
612 * This routine is used to handle the "bottom half" processing for the
613 * serial driver, known also the "software interrupt" processing.
614 * This processing is done at the kernel interrupt level, after the
615 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
616 * is where time-consuming activities which can not be done in the
617 * interrupt driver proper are done; the interrupt driver schedules
618 * them using rs_sched_event(), and they get done here.
621 static void do_softint(unsigned long private_)
623 struct dec_serial *info = (struct dec_serial *) private_;
624 struct tty_struct *tty;
630 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
632 wake_up_interruptible(&tty->write_wait);
636 static int zs_startup(struct dec_serial * info)
640 if (info->flags & ZILOG_INITIALIZED)
643 if (!info->xmit_buf) {
644 info->xmit_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
649 spin_lock_irqsave(&zs_lock, flags);
651 #ifdef SERIAL_DEBUG_OPEN
652 printk("starting up ttyS%d (irq %d)...", info->line, info->irq);
656 * Clear the receive FIFO.
658 ZS_CLEARFIFO(info->zs_channel);
659 info->xmit_fifo_size = 1;
662 * Clear the interrupt registers.
664 write_zsreg(info->zs_channel, R0, ERR_RES);
665 write_zsreg(info->zs_channel, R0, RES_H_IUS);
668 * Set the speed of the serial port
673 * Turn on RTS and DTR.
675 zs_rtsdtr(info, RTS | DTR, 1);
678 * Finally, enable sequencing and interrupts
680 info->zs_channel->curregs[R1] &= ~RxINT_MASK;
681 info->zs_channel->curregs[R1] |= (RxINT_ALL | TxINT_ENAB |
683 info->zs_channel->curregs[R3] |= RxENABLE;
684 info->zs_channel->curregs[R5] |= TxENAB;
685 info->zs_channel->curregs[R15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
686 write_zsreg(info->zs_channel, R1, info->zs_channel->curregs[R1]);
687 write_zsreg(info->zs_channel, R3, info->zs_channel->curregs[R3]);
688 write_zsreg(info->zs_channel, R5, info->zs_channel->curregs[R5]);
689 write_zsreg(info->zs_channel, R15, info->zs_channel->curregs[R15]);
692 * And clear the interrupt registers again for luck.
694 write_zsreg(info->zs_channel, R0, ERR_RES);
695 write_zsreg(info->zs_channel, R0, RES_H_IUS);
697 /* Save the current value of RR0 */
698 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
701 clear_bit(TTY_IO_ERROR, &info->tty->flags);
702 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
704 info->flags |= ZILOG_INITIALIZED;
705 spin_unlock_irqrestore(&zs_lock, flags);
710 * This routine will shutdown a serial port; interrupts are disabled, and
711 * DTR is dropped if the hangup on close termio flag is on.
713 static void shutdown(struct dec_serial * info)
717 if (!(info->flags & ZILOG_INITIALIZED))
720 #ifdef SERIAL_DEBUG_OPEN
721 printk("Shutting down serial port %d (irq %d)....", info->line,
725 spin_lock_irqsave(&zs_lock, flags);
727 if (info->xmit_buf) {
728 free_page((unsigned long) info->xmit_buf);
732 info->zs_channel->curregs[1] = 0;
733 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]); /* no interrupts */
735 info->zs_channel->curregs[3] &= ~RxENABLE;
736 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
738 info->zs_channel->curregs[5] &= ~TxENAB;
739 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
740 if (!info->tty || C_HUPCL(info->tty)) {
741 zs_rtsdtr(info, RTS | DTR, 0);
745 set_bit(TTY_IO_ERROR, &info->tty->flags);
747 info->flags &= ~ZILOG_INITIALIZED;
748 spin_unlock_irqrestore(&zs_lock, flags);
752 * This routine is called to set the UART divisor registers to match
753 * the specified baud rate for a serial port.
755 static void change_speed(struct dec_serial *info)
763 if (!info->tty || !info->tty->termios)
765 cflag = info->tty->termios->c_cflag;
769 cflag = info->hook->cflags;
775 if (i < 1 || i > 2) {
777 info->tty->termios->c_cflag &= ~CBAUDEX;
779 info->hook->cflags &= ~CBAUDEX;
784 spin_lock_irqsave(&zs_lock, flags);
785 info->zs_baud = baud_table[i];
787 brg = BPS_TO_BRG(info->zs_baud, zs_parms->clock/info->clk_divisor);
788 info->zs_channel->curregs[12] = (brg & 255);
789 info->zs_channel->curregs[13] = ((brg >> 8) & 255);
790 zs_rtsdtr(info, DTR, 1);
792 zs_rtsdtr(info, RTS | DTR, 0);
796 /* byte size and parity */
797 info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
798 info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
799 switch (cflag & CSIZE) {
802 info->zs_channel->curregs[3] |= Rx5;
803 info->zs_channel->curregs[5] |= Tx5;
807 info->zs_channel->curregs[3] |= Rx6;
808 info->zs_channel->curregs[5] |= Tx6;
812 info->zs_channel->curregs[3] |= Rx7;
813 info->zs_channel->curregs[5] |= Tx7;
816 default: /* defaults to 8 bits */
818 info->zs_channel->curregs[3] |= Rx8;
819 info->zs_channel->curregs[5] |= Tx8;
823 info->timeout = ((info->xmit_fifo_size*HZ*bits) / info->zs_baud);
824 info->timeout += HZ/50; /* Add .02 seconds of slop */
826 info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
827 if (cflag & CSTOPB) {
828 info->zs_channel->curregs[4] |= SB2;
830 info->zs_channel->curregs[4] |= SB1;
832 if (cflag & PARENB) {
833 info->zs_channel->curregs[4] |= PAR_ENA;
835 if (!(cflag & PARODD)) {
836 info->zs_channel->curregs[4] |= PAR_EVEN;
839 if (!(cflag & CLOCAL)) {
840 if (!(info->zs_channel->curregs[15] & DCDIE))
841 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
842 info->zs_channel->curregs[15] |= DCDIE;
844 info->zs_channel->curregs[15] &= ~DCDIE;
845 if (cflag & CRTSCTS) {
846 info->zs_channel->curregs[15] |= CTSIE;
847 if ((read_zsreg(info->zs_channel, 0) & CTS) == 0)
848 info->tx_stopped = 1;
850 info->zs_channel->curregs[15] &= ~CTSIE;
851 info->tx_stopped = 0;
854 /* Load up the new values */
855 load_zsregs(info->zs_channel, info->zs_channel->curregs);
857 spin_unlock_irqrestore(&zs_lock, flags);
860 static void rs_flush_chars(struct tty_struct *tty)
862 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
865 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
868 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
872 /* Enable transmitter */
873 spin_lock_irqsave(&zs_lock, flags);
874 transmit_chars(info);
875 spin_unlock_irqrestore(&zs_lock, flags);
878 static int rs_write(struct tty_struct * tty,
879 const unsigned char *buf, int count)
882 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
885 if (serial_paranoia_check(info, tty->name, "rs_write"))
888 if (!tty || !info->xmit_buf)
892 spin_lock_irqsave(&zs_lock, flags);
893 c = min(count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
894 SERIAL_XMIT_SIZE - info->xmit_head));
898 memcpy(info->xmit_buf + info->xmit_head, buf, c);
899 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
901 spin_unlock_irqrestore(&zs_lock, flags);
907 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
909 transmit_chars(info);
910 spin_unlock_irqrestore(&zs_lock, flags);
914 static int rs_write_room(struct tty_struct *tty)
916 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
919 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
921 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
927 static int rs_chars_in_buffer(struct tty_struct *tty)
929 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
931 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
933 return info->xmit_cnt;
936 static void rs_flush_buffer(struct tty_struct *tty)
938 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
940 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
942 spin_lock_irq(&zs_lock);
943 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
944 spin_unlock_irq(&zs_lock);
949 * ------------------------------------------------------------
952 * This routine is called by the upper-layer tty layer to signal that
953 * incoming characters should be throttled.
954 * ------------------------------------------------------------
956 static void rs_throttle(struct tty_struct * tty)
958 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
961 #ifdef SERIAL_DEBUG_THROTTLE
964 printk("throttle %s: %d....\n", _tty_name(tty, buf),
965 tty->ldisc.chars_in_buffer(tty));
968 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
972 spin_lock_irqsave(&zs_lock, flags);
973 info->x_char = STOP_CHAR(tty);
974 if (!info->tx_active)
975 transmit_chars(info);
976 spin_unlock_irqrestore(&zs_lock, flags);
979 if (C_CRTSCTS(tty)) {
980 zs_rtsdtr(info, RTS, 0);
984 static void rs_unthrottle(struct tty_struct * tty)
986 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
989 #ifdef SERIAL_DEBUG_THROTTLE
992 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
993 tty->ldisc.chars_in_buffer(tty));
996 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1000 spin_lock_irqsave(&zs_lock, flags);
1004 info->x_char = START_CHAR(tty);
1005 if (!info->tx_active)
1006 transmit_chars(info);
1008 spin_unlock_irqrestore(&zs_lock, flags);
1011 if (C_CRTSCTS(tty)) {
1012 zs_rtsdtr(info, RTS, 1);
1017 * ------------------------------------------------------------
1018 * rs_ioctl() and friends
1019 * ------------------------------------------------------------
1022 static int get_serial_info(struct dec_serial * info,
1023 struct serial_struct * retinfo)
1025 struct serial_struct tmp;
1029 memset(&tmp, 0, sizeof(tmp));
1030 tmp.type = info->type;
1031 tmp.line = info->line;
1032 tmp.port = info->port;
1033 tmp.irq = info->irq;
1034 tmp.flags = info->flags;
1035 tmp.baud_base = info->baud_base;
1036 tmp.close_delay = info->close_delay;
1037 tmp.closing_wait = info->closing_wait;
1038 tmp.custom_divisor = info->custom_divisor;
1039 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
1042 static int set_serial_info(struct dec_serial * info,
1043 struct serial_struct * new_info)
1045 struct serial_struct new_serial;
1046 struct dec_serial old_info;
1051 copy_from_user(&new_serial,new_info,sizeof(new_serial));
1054 if (!capable(CAP_SYS_ADMIN)) {
1055 if ((new_serial.baud_base != info->baud_base) ||
1056 (new_serial.type != info->type) ||
1057 (new_serial.close_delay != info->close_delay) ||
1058 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1059 (info->flags & ~ZILOG_USR_MASK)))
1061 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1062 (new_serial.flags & ZILOG_USR_MASK));
1063 info->custom_divisor = new_serial.custom_divisor;
1064 goto check_and_exit;
1067 if (info->count > 1)
1071 * OK, past this point, all the error checking has been done.
1072 * At this point, we start making changes.....
1075 info->baud_base = new_serial.baud_base;
1076 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1077 (new_serial.flags & ZILOG_FLAGS));
1078 info->type = new_serial.type;
1079 info->close_delay = new_serial.close_delay;
1080 info->closing_wait = new_serial.closing_wait;
1083 retval = zs_startup(info);
1088 * get_lsr_info - get line status register info
1090 * Purpose: Let user call ioctl() to get info when the UART physically
1091 * is emptied. On bus types like RS485, the transmitter must
1092 * release the bus after transmitting. This must be done when
1093 * the transmit shift register is empty, not be done when the
1094 * transmit holding register is empty. This functionality
1095 * allows an RS485 driver to be written in user space.
1097 static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1099 unsigned char status;
1101 spin_lock(&zs_lock);
1102 status = read_zsreg(info->zs_channel, 0);
1103 spin_unlock_irq(&zs_lock);
1104 put_user(status,value);
1108 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1110 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1111 unsigned char control, status_a, status_b;
1112 unsigned int result;
1117 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1120 if (tty->flags & (1 << TTY_IO_ERROR))
1123 if (info->zs_channel == info->zs_chan_a)
1126 spin_lock(&zs_lock);
1127 control = info->zs_chan_a->curregs[5];
1128 status_a = read_zsreg(info->zs_chan_a, 0);
1129 status_b = read_zsreg(info->zs_channel, 0);
1130 spin_unlock_irq(&zs_lock);
1131 result = ((control & RTS) ? TIOCM_RTS: 0)
1132 | ((control & DTR) ? TIOCM_DTR: 0)
1133 | ((status_b & DCD) ? TIOCM_CAR: 0)
1134 | ((status_a & DCD) ? TIOCM_RNG: 0)
1135 | ((status_a & SYNC_HUNT) ? TIOCM_DSR: 0)
1136 | ((status_b & CTS) ? TIOCM_CTS: 0);
1141 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1142 unsigned int set, unsigned int clear)
1144 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1149 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1152 if (tty->flags & (1 << TTY_IO_ERROR))
1155 if (info->zs_channel == info->zs_chan_a)
1158 spin_lock(&zs_lock);
1159 if (set & TIOCM_RTS)
1160 info->zs_chan_a->curregs[5] |= RTS;
1161 if (set & TIOCM_DTR)
1162 info->zs_chan_a->curregs[5] |= DTR;
1163 if (clear & TIOCM_RTS)
1164 info->zs_chan_a->curregs[5] &= ~RTS;
1165 if (clear & TIOCM_DTR)
1166 info->zs_chan_a->curregs[5] &= ~DTR;
1167 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1168 spin_unlock_irq(&zs_lock);
1173 * rs_break - turn transmit break condition on/off
1175 static void rs_break(struct tty_struct *tty, int break_state)
1177 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1178 unsigned long flags;
1180 if (serial_paranoia_check(info, tty->name, "rs_break"))
1185 spin_lock_irqsave(&zs_lock, flags);
1186 if (break_state == -1)
1187 info->zs_channel->curregs[5] |= SND_BRK;
1189 info->zs_channel->curregs[5] &= ~SND_BRK;
1190 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1191 spin_unlock_irqrestore(&zs_lock, flags);
1194 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1195 unsigned int cmd, unsigned long arg)
1197 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1202 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1205 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1206 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1207 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1208 if (tty->flags & (1 << TTY_IO_ERROR))
1214 if (!access_ok(VERIFY_WRITE, (void *)arg,
1215 sizeof(struct serial_struct)))
1217 return get_serial_info(info, (struct serial_struct *)arg);
1220 return set_serial_info(info, (struct serial_struct *)arg);
1222 case TIOCSERGETLSR: /* Get line status register */
1223 if (!access_ok(VERIFY_WRITE, (void *)arg,
1224 sizeof(unsigned int)))
1226 return get_lsr_info(info, (unsigned int *)arg);
1228 case TIOCSERGSTRUCT:
1229 if (!access_ok(VERIFY_WRITE, (void *)arg,
1230 sizeof(struct dec_serial)))
1232 copy_from_user((struct dec_serial *)arg, info,
1233 sizeof(struct dec_serial));
1237 return -ENOIOCTLCMD;
1242 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1244 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1247 if (tty->termios->c_cflag == old_termios->c_cflag)
1249 was_stopped = info->tx_stopped;
1253 if (was_stopped && !info->tx_stopped)
1258 * ------------------------------------------------------------
1261 * This routine is called when the serial port gets closed.
1262 * Wait for the last remaining data to be sent.
1263 * ------------------------------------------------------------
1265 static void rs_close(struct tty_struct *tty, struct file * filp)
1267 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1268 unsigned long flags;
1270 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1273 spin_lock_irqsave(&zs_lock, flags);
1275 if (tty_hung_up_p(filp)) {
1276 spin_unlock_irqrestore(&zs_lock, flags);
1280 #ifdef SERIAL_DEBUG_OPEN
1281 printk("rs_close ttyS%d, count = %d\n", info->line, info->count);
1283 if ((tty->count == 1) && (info->count != 1)) {
1285 * Uh, oh. tty->count is 1, which means that the tty
1286 * structure will be freed. Info->count should always
1287 * be one in these conditions. If it's greater than
1288 * one, we've got real problems, since it means the
1289 * serial port won't be shutdown.
1291 printk("rs_close: bad serial port count; tty->count is 1, "
1292 "info->count is %d\n", info->count);
1295 if (--info->count < 0) {
1296 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1297 info->line, info->count);
1301 spin_unlock_irqrestore(&zs_lock, flags);
1304 info->flags |= ZILOG_CLOSING;
1306 * Now we wait for the transmit buffer to clear; and we notify
1307 * the line discipline to only process XON/XOFF characters.
1310 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1311 tty_wait_until_sent(tty, info->closing_wait);
1313 * At this point we stop accepting input. To do this, we
1314 * disable the receiver and receive interrupts.
1316 info->zs_channel->curregs[3] &= ~RxENABLE;
1317 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1318 info->zs_channel->curregs[1] = 0; /* disable any rx ints */
1319 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1320 ZS_CLEARFIFO(info->zs_channel);
1321 if (info->flags & ZILOG_INITIALIZED) {
1323 * Before we drop DTR, make sure the SCC transmitter
1324 * has completely drained.
1326 rs_wait_until_sent(tty, info->timeout);
1330 if (tty->driver->flush_buffer)
1331 tty->driver->flush_buffer(tty);
1332 tty_ldisc_flush(tty);
1336 if (info->blocked_open) {
1337 if (info->close_delay) {
1338 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1340 wake_up_interruptible(&info->open_wait);
1342 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CLOSING);
1343 wake_up_interruptible(&info->close_wait);
1344 spin_unlock_irqrestore(&zs_lock, flags);
1348 * rs_wait_until_sent() --- wait until the transmitter is empty
1350 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1352 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1353 unsigned long orig_jiffies;
1356 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1359 orig_jiffies = jiffies;
1361 * Set the check interval to be 1/5 of the estimated time to
1362 * send a single character, and make it at least 1. The check
1363 * interval should also be less than the timeout.
1365 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1366 char_time = char_time / 5;
1370 char_time = min(char_time, timeout);
1371 while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
1372 msleep_interruptible(jiffies_to_msecs(char_time));
1373 if (signal_pending(current))
1375 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1378 current->state = TASK_RUNNING;
1382 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1384 static void rs_hangup(struct tty_struct *tty)
1386 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1388 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1391 rs_flush_buffer(tty);
1395 info->flags &= ~ZILOG_NORMAL_ACTIVE;
1397 wake_up_interruptible(&info->open_wait);
1401 * ------------------------------------------------------------
1402 * rs_open() and friends
1403 * ------------------------------------------------------------
1405 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1406 struct dec_serial *info)
1408 DECLARE_WAITQUEUE(wait, current);
1413 * If the device is in the middle of being closed, then block
1414 * until it's done, and then try again.
1416 if (info->flags & ZILOG_CLOSING) {
1417 interruptible_sleep_on(&info->close_wait);
1418 #ifdef SERIAL_DO_RESTART
1419 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1420 -EAGAIN : -ERESTARTSYS);
1427 * If non-blocking mode is set, or the port is not enabled,
1428 * then make the check up front and then exit.
1430 if ((filp->f_flags & O_NONBLOCK) ||
1431 (tty->flags & (1 << TTY_IO_ERROR))) {
1432 info->flags |= ZILOG_NORMAL_ACTIVE;
1436 if (tty->termios->c_cflag & CLOCAL)
1440 * Block waiting for the carrier detect and the line to become
1441 * free (i.e., not in use by the callout). While we are in
1442 * this loop, info->count is dropped by one, so that
1443 * rs_close() knows when to free things. We restore it upon
1444 * exit, either normal or abnormal.
1447 add_wait_queue(&info->open_wait, &wait);
1448 #ifdef SERIAL_DEBUG_OPEN
1449 printk("block_til_ready before block: ttyS%d, count = %d\n",
1450 info->line, info->count);
1452 spin_lock(&zs_lock);
1453 if (!tty_hung_up_p(filp))
1455 spin_unlock_irq(&zs_lock);
1456 info->blocked_open++;
1458 spin_lock(&zs_lock);
1459 if (tty->termios->c_cflag & CBAUD)
1460 zs_rtsdtr(info, RTS | DTR, 1);
1461 spin_unlock_irq(&zs_lock);
1462 set_current_state(TASK_INTERRUPTIBLE);
1463 if (tty_hung_up_p(filp) ||
1464 !(info->flags & ZILOG_INITIALIZED)) {
1465 #ifdef SERIAL_DO_RESTART
1466 if (info->flags & ZILOG_HUP_NOTIFY)
1469 retval = -ERESTARTSYS;
1475 if (!(info->flags & ZILOG_CLOSING) &&
1476 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1478 if (signal_pending(current)) {
1479 retval = -ERESTARTSYS;
1482 #ifdef SERIAL_DEBUG_OPEN
1483 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1484 info->line, info->count);
1488 current->state = TASK_RUNNING;
1489 remove_wait_queue(&info->open_wait, &wait);
1490 if (!tty_hung_up_p(filp))
1492 info->blocked_open--;
1493 #ifdef SERIAL_DEBUG_OPEN
1494 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1495 info->line, info->count);
1499 info->flags |= ZILOG_NORMAL_ACTIVE;
1504 * This routine is called whenever a serial port is opened. It
1505 * enables interrupts for a serial port, linking in its ZILOG structure into
1506 * the IRQ chain. It also performs the serial-specific
1507 * initialization for the tty structure.
1509 static int rs_open(struct tty_struct *tty, struct file * filp)
1511 struct dec_serial *info;
1515 if ((line < 0) || (line >= zs_channels_found))
1517 info = zs_soft + line;
1522 if (serial_paranoia_check(info, tty->name, "rs_open"))
1524 #ifdef SERIAL_DEBUG_OPEN
1525 printk("rs_open %s, count = %d\n", tty->name, info->count);
1529 tty->driver_data = info;
1533 * If the port is the middle of closing, bail out now
1535 if (tty_hung_up_p(filp) ||
1536 (info->flags & ZILOG_CLOSING)) {
1537 if (info->flags & ZILOG_CLOSING)
1538 interruptible_sleep_on(&info->close_wait);
1539 #ifdef SERIAL_DO_RESTART
1540 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1541 -EAGAIN : -ERESTARTSYS);
1548 * Start up serial port
1550 retval = zs_startup(info);
1554 retval = block_til_ready(tty, filp, info);
1556 #ifdef SERIAL_DEBUG_OPEN
1557 printk("rs_open returning after block_til_ready with %d\n",
1563 #ifdef CONFIG_SERIAL_DEC_CONSOLE
1564 if (sercons.cflag && sercons.index == line) {
1565 tty->termios->c_cflag = sercons.cflag;
1571 #ifdef SERIAL_DEBUG_OPEN
1572 printk("rs_open %s successful...", tty->name);
1574 /* tty->low_latency = 1; */
1578 /* Finally, routines used to initialize the serial driver. */
1580 static void __init show_serial_version(void)
1582 printk("DECstation Z8530 serial driver version 0.09\n");
1585 /* Initialize Z8530s zs_channels
1588 static void __init probe_sccs(void)
1590 struct dec_serial **pp;
1591 int i, n, n_chips = 0, n_channels, chip, channel;
1592 unsigned long flags;
1595 * did we get here by accident?
1598 printk("Not on JUNKIO machine, skipping probe_sccs\n");
1602 switch(mips_machtype) {
1603 #ifdef CONFIG_MACH_DECSTATION
1604 case MACH_DS5000_2X0:
1607 zs_parms = &ds_parms;
1608 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1609 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1611 case MACH_DS5000_1XX:
1613 zs_parms = &ds_parms;
1614 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1615 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1617 case MACH_DS5000_XX:
1619 zs_parms = &ds_parms;
1620 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1624 panic("zs: unsupported bus");
1627 panic("zs: uninitialized parms");
1633 for (chip = 0; chip < n_chips; chip++) {
1634 for (channel = 0; channel <= 1; channel++) {
1636 * The sccs reside on the high byte of the 16 bit IOBUS
1638 zs_channels[n_channels].control =
1639 (volatile void *)CKSEG1ADDR(dec_kn_slot_base +
1640 (0 == chip ? zs_parms->scc0 : zs_parms->scc1) +
1641 (0 == channel ? zs_parms->channel_a_offset :
1642 zs_parms->channel_b_offset));
1643 zs_channels[n_channels].data =
1644 zs_channels[n_channels].control + 4;
1646 #ifndef CONFIG_SERIAL_DEC_CONSOLE
1648 * We're called early and memory managment isn't up, yet.
1649 * Thus request_region would fail.
1651 if (!request_region((unsigned long)
1652 zs_channels[n_channels].control,
1653 ZS_CHAN_IO_SIZE, "SCC"))
1654 panic("SCC I/O region is not free");
1656 zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1659 zs_soft[n_channels].irq = zs_parms->irq0;
1661 zs_soft[n_channels].irq = zs_parms->irq1;
1664 * Identification of channel A. Location of channel A
1665 * inside chip depends on mapping of internal address
1666 * the chip decodes channels by.
1667 * CHANNEL_A_NR returns either 0 (in case of
1668 * DECstations) or 1 (in case of Baget).
1670 if (CHANNEL_A_NR == channel)
1671 zs_soft[n_channels].zs_chan_a =
1672 &zs_channels[n_channels+1-2*CHANNEL_A_NR];
1674 zs_soft[n_channels].zs_chan_a =
1675 &zs_channels[n_channels];
1677 *pp = &zs_soft[n_channels];
1678 pp = &zs_soft[n_channels].zs_next;
1684 zs_channels_found = n_channels;
1686 for (n = 0; n < zs_channels_found; n++) {
1687 for (i = 0; i < 16; i++) {
1688 zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1692 spin_lock_irqsave(&zs_lock, flags);
1693 for (n = 0; n < zs_channels_found; n++) {
1695 write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
1697 write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
1699 load_zsregs(zs_soft[n].zs_channel,
1700 zs_soft[n].zs_channel->curregs);
1702 spin_unlock_irqrestore(&zs_lock, flags);
1705 static struct tty_operations serial_ops = {
1709 .flush_chars = rs_flush_chars,
1710 .write_room = rs_write_room,
1711 .chars_in_buffer = rs_chars_in_buffer,
1712 .flush_buffer = rs_flush_buffer,
1714 .throttle = rs_throttle,
1715 .unthrottle = rs_unthrottle,
1716 .set_termios = rs_set_termios,
1719 .hangup = rs_hangup,
1720 .break_ctl = rs_break,
1721 .wait_until_sent = rs_wait_until_sent,
1722 .tiocmget = rs_tiocmget,
1723 .tiocmset = rs_tiocmset,
1726 /* zs_init inits the driver */
1727 int __init zs_init(void)
1730 struct dec_serial *info;
1735 /* Find out how many Z8530 SCCs we have */
1738 serial_driver = alloc_tty_driver(zs_channels_found);
1742 show_serial_version();
1744 /* Initialize the tty_driver structure */
1745 /* Not all of this is exactly right for us. */
1747 serial_driver->owner = THIS_MODULE;
1748 serial_driver->devfs_name = "tts/";
1749 serial_driver->name = "ttyS";
1750 serial_driver->major = TTY_MAJOR;
1751 serial_driver->minor_start = 64;
1752 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1753 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1754 serial_driver->init_termios = tty_std_termios;
1755 serial_driver->init_termios.c_cflag =
1756 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1757 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1758 tty_set_operations(serial_driver, &serial_ops);
1760 if (tty_register_driver(serial_driver))
1761 panic("Couldn't register serial driver");
1763 for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
1765 /* Needed before interrupts are enabled. */
1769 if (info->hook && info->hook->init_info) {
1770 (*info->hook->init_info)(info);
1774 info->magic = SERIAL_MAGIC;
1775 info->port = (int) info->zs_channel->control;
1777 info->custom_divisor = 16;
1778 info->close_delay = 50;
1779 info->closing_wait = 3000;
1782 info->blocked_open = 0;
1783 tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1784 init_waitqueue_head(&info->open_wait);
1785 init_waitqueue_head(&info->close_wait);
1786 printk("ttyS%02d at 0x%08x (irq = %d) is a Z85C30 SCC\n",
1787 info->line, info->port, info->irq);
1788 tty_register_device(serial_driver, info->line, NULL);
1792 for (channel = 0; channel < zs_channels_found; ++channel) {
1793 zs_soft[channel].clk_divisor = 16;
1794 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1796 if (request_irq(zs_soft[channel].irq, rs_interrupt, SA_SHIRQ,
1797 "scc", &zs_soft[channel]))
1798 printk(KERN_ERR "decserial: can't get irq %d\n",
1799 zs_soft[channel].irq);
1801 if (zs_soft[channel].hook) {
1802 zs_startup(&zs_soft[channel]);
1803 if (zs_soft[channel].hook->init_channel)
1804 (*zs_soft[channel].hook->init_channel)
1805 (&zs_soft[channel]);
1813 * polling I/O routines
1815 static int zs_poll_tx_char(void *handle, unsigned char ch)
1817 struct dec_serial *info = handle;
1818 struct dec_zschannel *chan = info->zs_channel;
1824 while (loops && !(read_zsreg(chan, 0) & Tx_BUF_EMP))
1828 write_zsdata(chan, ch);
1838 static int zs_poll_rx_char(void *handle)
1840 struct dec_serial *info = handle;
1841 struct dec_zschannel *chan = info->zs_channel;
1847 while (loops && !(read_zsreg(chan, 0) & Rx_CH_AV))
1851 ret = read_zsdata(chan);
1860 int register_zs_hook(unsigned int channel, struct dec_serial_hook *hook)
1862 struct dec_serial *info = &zs_soft[channel];
1865 printk("%s: line %d has already a hook registered\n",
1866 __FUNCTION__, channel);
1870 hook->poll_rx_char = zs_poll_rx_char;
1871 hook->poll_tx_char = zs_poll_tx_char;
1878 int unregister_zs_hook(unsigned int channel)
1880 struct dec_serial *info = &zs_soft[channel];
1886 printk("%s: trying to unregister hook on line %d,"
1887 " but none is registered\n", __FUNCTION__, channel);
1893 * ------------------------------------------------------------
1894 * Serial console driver
1895 * ------------------------------------------------------------
1897 #ifdef CONFIG_SERIAL_DEC_CONSOLE
1901 * Print a string to the serial port trying not to disturb
1902 * any possible real use of the port...
1904 static void serial_console_write(struct console *co, const char *s,
1907 struct dec_serial *info;
1910 info = zs_soft + co->index;
1912 for (i = 0; i < count; i++, s++) {
1914 zs_poll_tx_char(info, '\r');
1915 zs_poll_tx_char(info, *s);
1919 static struct tty_driver *serial_console_device(struct console *c, int *index)
1922 return serial_driver;
1926 * Setup initial baud/bits/parity. We do two things here:
1927 * - construct a cflag setting for the first rs_open()
1928 * - initialize the serial port
1929 * Return non-zero if we didn't find a serial port.
1931 static int __init serial_console_setup(struct console *co, char *options)
1933 struct dec_serial *info;
1937 int cflag = CREAD | HUPCL | CLOCAL;
1938 int clk_divisor = 16;
1941 unsigned long flags;
1946 info = zs_soft + co->index;
1954 baud = simple_strtoul(options, NULL, 10);
1956 while(*s >= '0' && *s <= '9')
1965 * Now construct a cflag setting.
1993 * Set this to a sane value to prevent a divide error.
2017 spin_lock_irqsave(&zs_lock, flags);
2020 * Set up the baud rate generator.
2022 brg = BPS_TO_BRG(baud, zs_parms->clock / clk_divisor);
2023 info->zs_channel->curregs[R12] = (brg & 255);
2024 info->zs_channel->curregs[R13] = ((brg >> 8) & 255);
2027 * Set byte size and parity.
2030 info->zs_channel->curregs[R3] |= Rx7;
2031 info->zs_channel->curregs[R5] |= Tx7;
2033 info->zs_channel->curregs[R3] |= Rx8;
2034 info->zs_channel->curregs[R5] |= Tx8;
2036 if (cflag & PARENB) {
2037 info->zs_channel->curregs[R4] |= PAR_ENA;
2039 if (!(cflag & PARODD)) {
2040 info->zs_channel->curregs[R4] |= PAR_EVEN;
2042 info->zs_channel->curregs[R4] |= SB1;
2045 * Turn on RTS and DTR.
2047 zs_rtsdtr(info, RTS | DTR, 1);
2050 * Finally, enable sequencing.
2052 info->zs_channel->curregs[R3] |= RxENABLE;
2053 info->zs_channel->curregs[R5] |= TxENAB;
2056 * Clear the interrupt registers.
2058 write_zsreg(info->zs_channel, R0, ERR_RES);
2059 write_zsreg(info->zs_channel, R0, RES_H_IUS);
2062 * Load up the new values.
2064 load_zsregs(info->zs_channel, info->zs_channel->curregs);
2066 /* Save the current value of RR0 */
2067 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
2069 zs_soft[co->index].clk_divisor = clk_divisor;
2070 zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
2072 spin_unlock_irqrestore(&zs_lock, flags);
2077 static struct console sercons = {
2079 .write = serial_console_write,
2080 .device = serial_console_device,
2081 .setup = serial_console_setup,
2082 .flags = CON_PRINTBUFFER,
2089 void __init zs_serial_console_init(void)
2091 register_console(&sercons);
2093 #endif /* ifdef CONFIG_SERIAL_DEC_CONSOLE */
2096 struct dec_zschannel *zs_kgdbchan;
2097 static unsigned char scc_inittab[] = {
2098 9, 0x80, /* reset A side (CHRA) */
2099 13, 0, /* set baud rate divisor */
2101 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
2102 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
2103 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
2104 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
2105 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
2108 /* These are for receiving and sending characters under the kgdb
2109 * source level kernel debugger.
2111 void putDebugChar(char kgdb_char)
2113 struct dec_zschannel *chan = zs_kgdbchan;
2114 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2116 write_zsdata(chan, kgdb_char);
2118 char getDebugChar(void)
2120 struct dec_zschannel *chan = zs_kgdbchan;
2121 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2122 eieio(); /*barrier();*/
2123 return read_zsdata(chan);
2125 void kgdb_interruptible(int yes)
2127 struct dec_zschannel *chan = zs_kgdbchan;
2129 nine = read_zsreg(chan, 9);
2131 one = EXT_INT_ENAB|RxINT_ALL;
2133 printk("turning serial ints on\n");
2137 printk("turning serial ints off\n");
2139 write_zsreg(chan, 1, one);
2140 write_zsreg(chan, 9, nine);
2143 static int kgdbhook_init_channel(void *handle)
2148 static void kgdbhook_init_info(void *handle)
2152 static void kgdbhook_rx_char(void *handle, unsigned char ch, unsigned char fl)
2154 struct dec_serial *info = handle;
2156 if (fl != TTY_NORMAL)
2158 if (ch == 0x03 || ch == '$')
2162 /* This sets up the serial port we're using, and turns on
2163 * interrupts for that channel, so kgdb is usable once we're done.
2165 static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2169 volatile char *sccc = ms->control;
2170 brg = BPS_TO_BRG(bps, zs_parms->clock/16);
2171 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2172 for (i = 20000; i != 0; --i) {
2175 for (i = 0; i < sizeof(scc_inittab); ++i) {
2176 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2180 /* This is called at boot time to prime the kgdb serial debugging
2181 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2182 * for /dev/ttyb which is determined in setup_arch() from the
2183 * boot command line flags.
2185 struct dec_serial_hook zs_kgdbhook = {
2186 .init_channel = kgdbhook_init_channel,
2187 .init_info = kgdbhook_init_info,
2188 .rx_char = kgdbhook_rx_char,
2189 .cflags = B38400 | CS8 | CLOCAL,
2192 void __init zs_kgdb_hook(int tty_num)
2194 /* Find out how many Z8530 SCCs we have */
2197 zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2198 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2199 zs_soft[tty_num].change_needed = 0;
2200 zs_soft[tty_num].clk_divisor = 16;
2201 zs_soft[tty_num].zs_baud = 38400;
2202 zs_soft[tty_num].hook = &zs_kgdbhook; /* This runs kgdb */
2203 /* Turn on transmitter/receiver at 8-bits/char */
2204 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2205 printk("KGDB: on channel %d initialized\n", tty_num);
2206 set_debug_traps(); /* init stub */
2208 #endif /* ifdef CONFIG_KGDB */