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/errno.h>
43 #include <linux/signal.h>
44 #include <linux/sched.h>
45 #include <linux/timer.h>
46 #include <linux/interrupt.h>
47 #include <linux/tty.h>
48 #include <linux/tty_flip.h>
49 #include <linux/major.h>
50 #include <linux/string.h>
51 #include <linux/fcntl.h>
53 #include <linux/kernel.h>
54 #include <linux/delay.h>
55 #include <linux/init.h>
56 #include <linux/ioport.h>
57 #include <linux/spinlock.h>
58 #ifdef CONFIG_SERIAL_DEC_CONSOLE
59 #include <linux/console.h>
63 #include <asm/pgtable.h>
65 #include <asm/system.h>
66 #include <asm/bootinfo.h>
68 #include <asm/dec/interrupts.h>
69 #include <asm/dec/ioasic_addrs.h>
70 #include <asm/dec/machtype.h>
71 #include <asm/dec/serial.h>
72 #include <asm/dec/system.h>
73 #include <asm/dec/tc.h>
78 #ifdef CONFIG_MAGIC_SYSRQ
79 #include <linux/sysrq.h>
85 * It would be nice to dynamically allocate everything that
86 * depends on NUM_SERIAL, so we could support any number of
87 * Z8530s, but for now...
89 #define NUM_SERIAL 2 /* Max number of ZS chips supported */
90 #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
91 #define CHANNEL_A_NR (zs_parms->channel_a_offset > zs_parms->channel_b_offset)
92 /* Number of channel A in the chip */
93 #define ZS_CHAN_IO_SIZE 8
94 #define ZS_CLOCK 7372800 /* Z8530 RTxC input clock rate */
96 #define RECOVERY_DELAY udelay(2)
101 int channel_a_offset;
102 int channel_b_offset;
108 static struct zs_parms *zs_parms;
110 #ifdef CONFIG_MACH_DECSTATION
111 static struct zs_parms ds_parms = {
114 channel_a_offset : 1,
115 channel_b_offset : 9,
122 #ifdef CONFIG_MACH_DECSTATION
123 #define DS_BUS_PRESENT (IOASIC)
125 #define DS_BUS_PRESENT 0
128 #define BUS_PRESENT (DS_BUS_PRESENT)
130 DEFINE_SPINLOCK(zs_lock);
132 struct dec_zschannel zs_channels[NUM_CHANNELS];
133 struct dec_serial zs_soft[NUM_CHANNELS];
134 int zs_channels_found;
135 struct dec_serial *zs_chain; /* list of all channels */
137 struct tty_struct zs_ttys[NUM_CHANNELS];
139 #ifdef CONFIG_SERIAL_DEC_CONSOLE
140 static struct console sercons;
142 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
144 static unsigned long break_pressed; /* break, really ... */
147 static unsigned char zs_init_regs[16] __initdata = {
152 (X16CLK), /* write 4 */
154 0, 0, 0, /* write 6, 7, 8 */
155 (MIE | DLC | NV), /* write 9 */
156 (NRZ), /* write 10 */
157 (TCBR | RCBR), /* write 11 */
158 0, 0, /* BRG time constant, write 12 + 13 */
159 (BRSRC | BRENABL), /* write 14 */
163 static struct tty_driver *serial_driver;
165 /* serial subtype definitions */
166 #define SERIAL_TYPE_NORMAL 1
168 /* number of characters left in xmit buffer before we ask for more */
169 #define WAKEUP_CHARS 256
174 #undef SERIAL_DEBUG_OPEN
175 #undef SERIAL_DEBUG_FLOW
176 #undef SERIAL_DEBUG_THROTTLE
177 #undef SERIAL_PARANOIA_CHECK
181 #ifdef SERIAL_DEBUG_THROTTLE
182 #define _tty_name(tty,buf) tty_name(tty,buf)
185 #define RS_STROBE_TIME 10
186 #define RS_ISR_PASS_LIMIT 256
188 static void probe_sccs(void);
189 static void change_speed(struct dec_serial *info);
190 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
192 static inline int serial_paranoia_check(struct dec_serial *info,
193 char *name, const char *routine)
195 #ifdef SERIAL_PARANOIA_CHECK
196 static const char *badmagic =
197 "Warning: bad magic number for serial struct %s in %s\n";
198 static const char *badinfo =
199 "Warning: null mac_serial for %s in %s\n";
202 printk(badinfo, name, routine);
205 if (info->magic != SERIAL_MAGIC) {
206 printk(badmagic, name, routine);
214 * This is used to figure out the divisor speeds and the timeouts
216 static int baud_table[] = {
217 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
218 9600, 19200, 38400, 57600, 115200, 0 };
221 * Reading and writing Z8530 registers.
223 static inline unsigned char read_zsreg(struct dec_zschannel *channel,
226 unsigned char retval;
229 *channel->control = reg & 0xf;
230 fast_iob(); RECOVERY_DELAY;
232 retval = *channel->control;
237 static inline void write_zsreg(struct dec_zschannel *channel,
238 unsigned char reg, unsigned char value)
241 *channel->control = reg & 0xf;
242 fast_iob(); RECOVERY_DELAY;
244 *channel->control = value;
245 fast_iob(); RECOVERY_DELAY;
249 static inline unsigned char read_zsdata(struct dec_zschannel *channel)
251 unsigned char retval;
253 retval = *channel->data;
258 static inline void write_zsdata(struct dec_zschannel *channel,
261 *channel->data = value;
262 fast_iob(); RECOVERY_DELAY;
266 static inline void load_zsregs(struct dec_zschannel *channel,
269 /* ZS_CLEARERR(channel);
270 ZS_CLEARFIFO(channel); */
272 write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
273 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
274 write_zsreg(channel, R4, regs[R4]);
275 write_zsreg(channel, R9, regs[R9]);
276 write_zsreg(channel, R1, regs[R1]);
277 write_zsreg(channel, R2, regs[R2]);
278 write_zsreg(channel, R10, regs[R10]);
279 write_zsreg(channel, R11, regs[R11]);
280 write_zsreg(channel, R12, regs[R12]);
281 write_zsreg(channel, R13, regs[R13]);
282 write_zsreg(channel, R14, regs[R14]);
283 write_zsreg(channel, R15, regs[R15]);
284 write_zsreg(channel, R3, regs[R3]);
285 write_zsreg(channel, R5, regs[R5]);
289 /* Sets or clears DTR/RTS on the requested line */
290 static inline void zs_rtsdtr(struct dec_serial *info, int which, int set)
294 spin_lock_irqsave(&zs_lock, flags);
295 if (info->zs_channel != info->zs_chan_a) {
297 info->zs_chan_a->curregs[5] |= (which & (RTS | DTR));
299 info->zs_chan_a->curregs[5] &= ~(which & (RTS | DTR));
301 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
303 spin_unlock_irqrestore(&zs_lock, flags);
306 /* Utility routines for the Zilog */
307 static inline int get_zsbaud(struct dec_serial *ss)
309 struct dec_zschannel *channel = ss->zs_channel;
312 /* The baud rate is split up between two 8-bit registers in
313 * what is termed 'BRG time constant' format in my docs for
314 * the chip, it is a function of the clk rate the chip is
315 * receiving which happens to be constant.
317 brg = (read_zsreg(channel, 13) << 8);
318 brg |= read_zsreg(channel, 12);
319 return BRG_TO_BPS(brg, (zs_parms->clock/(ss->clk_divisor)));
322 /* On receive, this clears errors and the receiver interrupts */
323 static inline void rs_recv_clear(struct dec_zschannel *zsc)
325 write_zsreg(zsc, 0, ERR_RES);
326 write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
330 * ----------------------------------------------------------------------
332 * Here starts the interrupt handling routines. All of the following
333 * subroutines are declared as inline and are folded into
334 * rs_interrupt(). They were separated out for readability's sake.
336 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
337 * -----------------------------------------------------------------------
341 * This routine is used by the interrupt handler to schedule
342 * processing in the software interrupt portion of the driver.
344 static void rs_sched_event(struct dec_serial *info, int event)
346 info->event |= 1 << event;
347 tasklet_schedule(&info->tlet);
350 static void receive_chars(struct dec_serial *info, struct pt_regs *regs)
352 struct tty_struct *tty = info->tty;
353 unsigned char ch, stat, flag;
355 while ((read_zsreg(info->zs_channel, R0) & Rx_CH_AV) != 0) {
357 stat = read_zsreg(info->zs_channel, R1);
358 ch = read_zsdata(info->zs_channel);
360 if (!tty && (!info->hook || !info->hook->rx_char))
364 if (info->tty_break) {
367 if (info->flags & ZILOG_SAK)
369 /* Ignore the null char got when BREAK is removed. */
375 } else if (stat & FRM_ERR) {
377 } else if (stat & PAR_ERR) {
380 if (flag != TTY_NORMAL)
381 /* reset the error indication */
382 write_zsreg(info->zs_channel, R0, ERR_RES);
385 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
387 if (break_pressed && info->line == sercons.index) {
388 /* Ignore the null char got when BREAK is removed. */
391 if (time_before(jiffies, break_pressed + HZ * 5)) {
392 handle_sysrq(ch, regs, NULL);
400 if (info->hook && info->hook->rx_char) {
401 (*info->hook->rx_char)(ch, flag);
405 tty_insert_flip_char(tty, ch, flag);
408 tty_flip_buffer_push(tty);
411 static void transmit_chars(struct dec_serial *info)
413 if ((read_zsreg(info->zs_channel, R0) & Tx_BUF_EMP) == 0)
419 write_zsdata(info->zs_channel, info->x_char);
425 if ((info->xmit_cnt <= 0) || (info->tty && info->tty->stopped)
426 || info->tx_stopped) {
427 write_zsreg(info->zs_channel, R0, RES_Tx_P);
431 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
432 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
436 if (info->xmit_cnt < WAKEUP_CHARS)
437 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
440 static void status_handle(struct dec_serial *info)
444 /* Get status from Read Register 0 */
445 stat = read_zsreg(info->zs_channel, R0);
447 if ((stat & BRK_ABRT) && !(info->read_reg_zero & BRK_ABRT)) {
448 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
450 if (info->line == sercons.index) {
452 break_pressed = jiffies;
458 if (info->zs_channel != info->zs_chan_a) {
460 /* Check for DCD transitions */
461 if (info->tty && !C_CLOCAL(info->tty) &&
462 ((stat ^ info->read_reg_zero) & DCD) != 0 ) {
464 wake_up_interruptible(&info->open_wait);
466 tty_hangup(info->tty);
470 /* Check for CTS transitions */
471 if (info->tty && C_CRTSCTS(info->tty)) {
472 if ((stat & CTS) != 0) {
473 if (info->tx_stopped) {
474 info->tx_stopped = 0;
475 if (!info->tx_active)
476 transmit_chars(info);
479 info->tx_stopped = 1;
485 /* Clear status condition... */
486 write_zsreg(info->zs_channel, R0, RES_EXT_INT);
487 info->read_reg_zero = stat;
491 * This is the serial driver's generic interrupt routine
493 static irqreturn_t rs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
495 struct dec_serial *info = (struct dec_serial *) dev_id;
496 irqreturn_t status = IRQ_NONE;
497 unsigned char zs_intreg;
500 /* NOTE: The read register 3, which holds the irq status,
501 * does so for both channels on each chip. Although
502 * the status value itself must be read from the A
503 * channel and is only valid when read from channel A.
504 * Yes... broken hardware...
506 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
508 if (info->zs_chan_a == info->zs_channel)
509 shift = 3; /* Channel A */
511 shift = 0; /* Channel B */
514 zs_intreg = read_zsreg(info->zs_chan_a, R3) >> shift;
515 if ((zs_intreg & CHAN_IRQMASK) == 0)
518 status = IRQ_HANDLED;
520 if (zs_intreg & CHBRxIP) {
521 receive_chars(info, regs);
523 if (zs_intreg & CHBTxIP) {
524 transmit_chars(info);
526 if (zs_intreg & CHBEXT) {
531 /* Why do we need this ? */
532 write_zsreg(info->zs_channel, 0, RES_H_IUS);
538 void zs_dump (void) {
540 for (i = 0; i < zs_channels_found; i++) {
541 struct dec_zschannel *ch = &zs_channels[i];
542 if ((long)ch->control == UNI_IO_BASE+UNI_SCC1A_CTRL) {
543 for (j = 0; j < 15; j++) {
544 printk("W%d = 0x%x\t",
545 j, (int)ch->curregs[j]);
547 for (j = 0; j < 15; j++) {
548 printk("R%d = 0x%x\t",
549 j, (int)read_zsreg(ch,j));
558 * -------------------------------------------------------------------
559 * Here ends the serial interrupt routines.
560 * -------------------------------------------------------------------
564 * ------------------------------------------------------------
565 * rs_stop() and rs_start()
567 * This routines are called before setting or resetting tty->stopped.
568 * ------------------------------------------------------------
570 static void rs_stop(struct tty_struct *tty)
572 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
575 if (serial_paranoia_check(info, tty->name, "rs_stop"))
579 spin_lock_irqsave(&zs_lock, flags);
580 if (info->zs_channel->curregs[5] & TxENAB) {
581 info->zs_channel->curregs[5] &= ~TxENAB;
582 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
584 spin_unlock_irqrestore(&zs_lock, flags);
588 static void rs_start(struct tty_struct *tty)
590 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
593 if (serial_paranoia_check(info, tty->name, "rs_start"))
596 spin_lock_irqsave(&zs_lock, flags);
598 if (info->xmit_cnt && info->xmit_buf && !(info->zs_channel->curregs[5] & TxENAB)) {
599 info->zs_channel->curregs[5] |= TxENAB;
600 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
603 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
604 transmit_chars(info);
607 spin_unlock_irqrestore(&zs_lock, flags);
611 * This routine is used to handle the "bottom half" processing for the
612 * serial driver, known also the "software interrupt" processing.
613 * This processing is done at the kernel interrupt level, after the
614 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
615 * is where time-consuming activities which can not be done in the
616 * interrupt driver proper are done; the interrupt driver schedules
617 * them using rs_sched_event(), and they get done here.
620 static void do_softint(unsigned long private_)
622 struct dec_serial *info = (struct dec_serial *) private_;
623 struct tty_struct *tty;
629 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
631 wake_up_interruptible(&tty->write_wait);
635 static int zs_startup(struct dec_serial * info)
639 if (info->flags & ZILOG_INITIALIZED)
642 if (!info->xmit_buf) {
643 info->xmit_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
648 spin_lock_irqsave(&zs_lock, flags);
650 #ifdef SERIAL_DEBUG_OPEN
651 printk("starting up ttyS%d (irq %d)...", info->line, info->irq);
655 * Clear the receive FIFO.
657 ZS_CLEARFIFO(info->zs_channel);
658 info->xmit_fifo_size = 1;
661 * Clear the interrupt registers.
663 write_zsreg(info->zs_channel, R0, ERR_RES);
664 write_zsreg(info->zs_channel, R0, RES_H_IUS);
667 * Set the speed of the serial port
672 * Turn on RTS and DTR.
674 zs_rtsdtr(info, RTS | DTR, 1);
677 * Finally, enable sequencing and interrupts
679 info->zs_channel->curregs[R1] &= ~RxINT_MASK;
680 info->zs_channel->curregs[R1] |= (RxINT_ALL | TxINT_ENAB |
682 info->zs_channel->curregs[R3] |= RxENABLE;
683 info->zs_channel->curregs[R5] |= TxENAB;
684 info->zs_channel->curregs[R15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
685 write_zsreg(info->zs_channel, R1, info->zs_channel->curregs[R1]);
686 write_zsreg(info->zs_channel, R3, info->zs_channel->curregs[R3]);
687 write_zsreg(info->zs_channel, R5, info->zs_channel->curregs[R5]);
688 write_zsreg(info->zs_channel, R15, info->zs_channel->curregs[R15]);
691 * And clear the interrupt registers again for luck.
693 write_zsreg(info->zs_channel, R0, ERR_RES);
694 write_zsreg(info->zs_channel, R0, RES_H_IUS);
696 /* Save the current value of RR0 */
697 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
700 clear_bit(TTY_IO_ERROR, &info->tty->flags);
701 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
703 info->flags |= ZILOG_INITIALIZED;
704 spin_unlock_irqrestore(&zs_lock, flags);
709 * This routine will shutdown a serial port; interrupts are disabled, and
710 * DTR is dropped if the hangup on close termio flag is on.
712 static void shutdown(struct dec_serial * info)
716 if (!(info->flags & ZILOG_INITIALIZED))
719 #ifdef SERIAL_DEBUG_OPEN
720 printk("Shutting down serial port %d (irq %d)....", info->line,
724 spin_lock_irqsave(&zs_lock, flags);
726 if (info->xmit_buf) {
727 free_page((unsigned long) info->xmit_buf);
731 info->zs_channel->curregs[1] = 0;
732 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]); /* no interrupts */
734 info->zs_channel->curregs[3] &= ~RxENABLE;
735 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
737 info->zs_channel->curregs[5] &= ~TxENAB;
738 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
739 if (!info->tty || C_HUPCL(info->tty)) {
740 zs_rtsdtr(info, RTS | DTR, 0);
744 set_bit(TTY_IO_ERROR, &info->tty->flags);
746 info->flags &= ~ZILOG_INITIALIZED;
747 spin_unlock_irqrestore(&zs_lock, flags);
751 * This routine is called to set the UART divisor registers to match
752 * the specified baud rate for a serial port.
754 static void change_speed(struct dec_serial *info)
762 if (!info->tty || !info->tty->termios)
764 cflag = info->tty->termios->c_cflag;
768 cflag = info->hook->cflags;
774 if (i < 1 || i > 2) {
776 info->tty->termios->c_cflag &= ~CBAUDEX;
778 info->hook->cflags &= ~CBAUDEX;
783 spin_lock_irqsave(&zs_lock, flags);
784 info->zs_baud = baud_table[i];
786 brg = BPS_TO_BRG(info->zs_baud, zs_parms->clock/info->clk_divisor);
787 info->zs_channel->curregs[12] = (brg & 255);
788 info->zs_channel->curregs[13] = ((brg >> 8) & 255);
789 zs_rtsdtr(info, DTR, 1);
791 zs_rtsdtr(info, RTS | DTR, 0);
795 /* byte size and parity */
796 info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
797 info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
798 switch (cflag & CSIZE) {
801 info->zs_channel->curregs[3] |= Rx5;
802 info->zs_channel->curregs[5] |= Tx5;
806 info->zs_channel->curregs[3] |= Rx6;
807 info->zs_channel->curregs[5] |= Tx6;
811 info->zs_channel->curregs[3] |= Rx7;
812 info->zs_channel->curregs[5] |= Tx7;
815 default: /* defaults to 8 bits */
817 info->zs_channel->curregs[3] |= Rx8;
818 info->zs_channel->curregs[5] |= Tx8;
822 info->timeout = ((info->xmit_fifo_size*HZ*bits) / info->zs_baud);
823 info->timeout += HZ/50; /* Add .02 seconds of slop */
825 info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
826 if (cflag & CSTOPB) {
827 info->zs_channel->curregs[4] |= SB2;
829 info->zs_channel->curregs[4] |= SB1;
831 if (cflag & PARENB) {
832 info->zs_channel->curregs[4] |= PAR_ENA;
834 if (!(cflag & PARODD)) {
835 info->zs_channel->curregs[4] |= PAR_EVEN;
838 if (!(cflag & CLOCAL)) {
839 if (!(info->zs_channel->curregs[15] & DCDIE))
840 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
841 info->zs_channel->curregs[15] |= DCDIE;
843 info->zs_channel->curregs[15] &= ~DCDIE;
844 if (cflag & CRTSCTS) {
845 info->zs_channel->curregs[15] |= CTSIE;
846 if ((read_zsreg(info->zs_channel, 0) & CTS) == 0)
847 info->tx_stopped = 1;
849 info->zs_channel->curregs[15] &= ~CTSIE;
850 info->tx_stopped = 0;
853 /* Load up the new values */
854 load_zsregs(info->zs_channel, info->zs_channel->curregs);
856 spin_unlock_irqrestore(&zs_lock, flags);
859 static void rs_flush_chars(struct tty_struct *tty)
861 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
864 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
867 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
871 /* Enable transmitter */
872 spin_lock_irqsave(&zs_lock, flags);
873 transmit_chars(info);
874 spin_unlock_irqrestore(&zs_lock, flags);
877 static int rs_write(struct tty_struct * tty,
878 const unsigned char *buf, int count)
881 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
884 if (serial_paranoia_check(info, tty->name, "rs_write"))
887 if (!tty || !info->xmit_buf)
891 spin_lock_irqsave(&zs_lock, flags);
892 c = min(count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
893 SERIAL_XMIT_SIZE - info->xmit_head));
897 memcpy(info->xmit_buf + info->xmit_head, buf, c);
898 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
900 spin_unlock_irqrestore(&zs_lock, flags);
906 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
908 transmit_chars(info);
909 spin_unlock_irqrestore(&zs_lock, flags);
913 static int rs_write_room(struct tty_struct *tty)
915 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
918 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
920 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
926 static int rs_chars_in_buffer(struct tty_struct *tty)
928 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
930 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
932 return info->xmit_cnt;
935 static void rs_flush_buffer(struct tty_struct *tty)
937 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
939 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
941 spin_lock_irq(&zs_lock);
942 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
943 spin_unlock_irq(&zs_lock);
948 * ------------------------------------------------------------
951 * This routine is called by the upper-layer tty layer to signal that
952 * incoming characters should be throttled.
953 * ------------------------------------------------------------
955 static void rs_throttle(struct tty_struct * tty)
957 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
960 #ifdef SERIAL_DEBUG_THROTTLE
963 printk("throttle %s: %d....\n", _tty_name(tty, buf),
964 tty->ldisc.chars_in_buffer(tty));
967 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
971 spin_lock_irqsave(&zs_lock, flags);
972 info->x_char = STOP_CHAR(tty);
973 if (!info->tx_active)
974 transmit_chars(info);
975 spin_unlock_irqrestore(&zs_lock, flags);
978 if (C_CRTSCTS(tty)) {
979 zs_rtsdtr(info, RTS, 0);
983 static void rs_unthrottle(struct tty_struct * tty)
985 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
988 #ifdef SERIAL_DEBUG_THROTTLE
991 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
992 tty->ldisc.chars_in_buffer(tty));
995 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
999 spin_lock_irqsave(&zs_lock, flags);
1003 info->x_char = START_CHAR(tty);
1004 if (!info->tx_active)
1005 transmit_chars(info);
1007 spin_unlock_irqrestore(&zs_lock, flags);
1010 if (C_CRTSCTS(tty)) {
1011 zs_rtsdtr(info, RTS, 1);
1016 * ------------------------------------------------------------
1017 * rs_ioctl() and friends
1018 * ------------------------------------------------------------
1021 static int get_serial_info(struct dec_serial * info,
1022 struct serial_struct * retinfo)
1024 struct serial_struct tmp;
1028 memset(&tmp, 0, sizeof(tmp));
1029 tmp.type = info->type;
1030 tmp.line = info->line;
1031 tmp.port = info->port;
1032 tmp.irq = info->irq;
1033 tmp.flags = info->flags;
1034 tmp.baud_base = info->baud_base;
1035 tmp.close_delay = info->close_delay;
1036 tmp.closing_wait = info->closing_wait;
1037 tmp.custom_divisor = info->custom_divisor;
1038 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
1041 static int set_serial_info(struct dec_serial * info,
1042 struct serial_struct * new_info)
1044 struct serial_struct new_serial;
1045 struct dec_serial old_info;
1050 copy_from_user(&new_serial,new_info,sizeof(new_serial));
1053 if (!capable(CAP_SYS_ADMIN)) {
1054 if ((new_serial.baud_base != info->baud_base) ||
1055 (new_serial.type != info->type) ||
1056 (new_serial.close_delay != info->close_delay) ||
1057 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1058 (info->flags & ~ZILOG_USR_MASK)))
1060 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1061 (new_serial.flags & ZILOG_USR_MASK));
1062 info->custom_divisor = new_serial.custom_divisor;
1063 goto check_and_exit;
1066 if (info->count > 1)
1070 * OK, past this point, all the error checking has been done.
1071 * At this point, we start making changes.....
1074 info->baud_base = new_serial.baud_base;
1075 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1076 (new_serial.flags & ZILOG_FLAGS));
1077 info->type = new_serial.type;
1078 info->close_delay = new_serial.close_delay;
1079 info->closing_wait = new_serial.closing_wait;
1082 retval = zs_startup(info);
1087 * get_lsr_info - get line status register info
1089 * Purpose: Let user call ioctl() to get info when the UART physically
1090 * is emptied. On bus types like RS485, the transmitter must
1091 * release the bus after transmitting. This must be done when
1092 * the transmit shift register is empty, not be done when the
1093 * transmit holding register is empty. This functionality
1094 * allows an RS485 driver to be written in user space.
1096 static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1098 unsigned char status;
1100 spin_lock(&zs_lock);
1101 status = read_zsreg(info->zs_channel, 0);
1102 spin_unlock_irq(&zs_lock);
1103 put_user(status,value);
1107 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1109 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1110 unsigned char control, status_a, status_b;
1111 unsigned int result;
1116 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1119 if (tty->flags & (1 << TTY_IO_ERROR))
1122 if (info->zs_channel == info->zs_chan_a)
1125 spin_lock(&zs_lock);
1126 control = info->zs_chan_a->curregs[5];
1127 status_a = read_zsreg(info->zs_chan_a, 0);
1128 status_b = read_zsreg(info->zs_channel, 0);
1129 spin_unlock_irq(&zs_lock);
1130 result = ((control & RTS) ? TIOCM_RTS: 0)
1131 | ((control & DTR) ? TIOCM_DTR: 0)
1132 | ((status_b & DCD) ? TIOCM_CAR: 0)
1133 | ((status_a & DCD) ? TIOCM_RNG: 0)
1134 | ((status_a & SYNC_HUNT) ? TIOCM_DSR: 0)
1135 | ((status_b & CTS) ? TIOCM_CTS: 0);
1140 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1141 unsigned int set, unsigned int clear)
1143 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1148 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1151 if (tty->flags & (1 << TTY_IO_ERROR))
1154 if (info->zs_channel == info->zs_chan_a)
1157 spin_lock(&zs_lock);
1158 if (set & TIOCM_RTS)
1159 info->zs_chan_a->curregs[5] |= RTS;
1160 if (set & TIOCM_DTR)
1161 info->zs_chan_a->curregs[5] |= DTR;
1162 if (clear & TIOCM_RTS)
1163 info->zs_chan_a->curregs[5] &= ~RTS;
1164 if (clear & TIOCM_DTR)
1165 info->zs_chan_a->curregs[5] &= ~DTR;
1166 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1167 spin_unlock_irq(&zs_lock);
1172 * rs_break - turn transmit break condition on/off
1174 static void rs_break(struct tty_struct *tty, int break_state)
1176 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1177 unsigned long flags;
1179 if (serial_paranoia_check(info, tty->name, "rs_break"))
1184 spin_lock_irqsave(&zs_lock, flags);
1185 if (break_state == -1)
1186 info->zs_channel->curregs[5] |= SND_BRK;
1188 info->zs_channel->curregs[5] &= ~SND_BRK;
1189 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1190 spin_unlock_irqrestore(&zs_lock, flags);
1193 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1194 unsigned int cmd, unsigned long arg)
1196 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1201 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1204 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1205 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1206 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1207 if (tty->flags & (1 << TTY_IO_ERROR))
1213 if (!access_ok(VERIFY_WRITE, (void *)arg,
1214 sizeof(struct serial_struct)))
1216 return get_serial_info(info, (struct serial_struct *)arg);
1219 return set_serial_info(info, (struct serial_struct *)arg);
1221 case TIOCSERGETLSR: /* Get line status register */
1222 if (!access_ok(VERIFY_WRITE, (void *)arg,
1223 sizeof(unsigned int)))
1225 return get_lsr_info(info, (unsigned int *)arg);
1227 case TIOCSERGSTRUCT:
1228 if (!access_ok(VERIFY_WRITE, (void *)arg,
1229 sizeof(struct dec_serial)))
1231 copy_from_user((struct dec_serial *)arg, info,
1232 sizeof(struct dec_serial));
1236 return -ENOIOCTLCMD;
1241 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1243 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1246 if (tty->termios->c_cflag == old_termios->c_cflag)
1248 was_stopped = info->tx_stopped;
1252 if (was_stopped && !info->tx_stopped)
1257 * ------------------------------------------------------------
1260 * This routine is called when the serial port gets closed.
1261 * Wait for the last remaining data to be sent.
1262 * ------------------------------------------------------------
1264 static void rs_close(struct tty_struct *tty, struct file * filp)
1266 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1267 unsigned long flags;
1269 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1272 spin_lock_irqsave(&zs_lock, flags);
1274 if (tty_hung_up_p(filp)) {
1275 spin_unlock_irqrestore(&zs_lock, flags);
1279 #ifdef SERIAL_DEBUG_OPEN
1280 printk("rs_close ttyS%d, count = %d\n", info->line, info->count);
1282 if ((tty->count == 1) && (info->count != 1)) {
1284 * Uh, oh. tty->count is 1, which means that the tty
1285 * structure will be freed. Info->count should always
1286 * be one in these conditions. If it's greater than
1287 * one, we've got real problems, since it means the
1288 * serial port won't be shutdown.
1290 printk("rs_close: bad serial port count; tty->count is 1, "
1291 "info->count is %d\n", info->count);
1294 if (--info->count < 0) {
1295 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1296 info->line, info->count);
1300 spin_unlock_irqrestore(&zs_lock, flags);
1303 info->flags |= ZILOG_CLOSING;
1305 * Now we wait for the transmit buffer to clear; and we notify
1306 * the line discipline to only process XON/XOFF characters.
1309 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1310 tty_wait_until_sent(tty, info->closing_wait);
1312 * At this point we stop accepting input. To do this, we
1313 * disable the receiver and receive interrupts.
1315 info->zs_channel->curregs[3] &= ~RxENABLE;
1316 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1317 info->zs_channel->curregs[1] = 0; /* disable any rx ints */
1318 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1319 ZS_CLEARFIFO(info->zs_channel);
1320 if (info->flags & ZILOG_INITIALIZED) {
1322 * Before we drop DTR, make sure the SCC transmitter
1323 * has completely drained.
1325 rs_wait_until_sent(tty, info->timeout);
1329 if (tty->driver->flush_buffer)
1330 tty->driver->flush_buffer(tty);
1331 tty_ldisc_flush(tty);
1335 if (info->blocked_open) {
1336 if (info->close_delay) {
1337 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1339 wake_up_interruptible(&info->open_wait);
1341 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CLOSING);
1342 wake_up_interruptible(&info->close_wait);
1343 spin_unlock_irqrestore(&zs_lock, flags);
1347 * rs_wait_until_sent() --- wait until the transmitter is empty
1349 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1351 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1352 unsigned long orig_jiffies;
1355 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1358 orig_jiffies = jiffies;
1360 * Set the check interval to be 1/5 of the estimated time to
1361 * send a single character, and make it at least 1. The check
1362 * interval should also be less than the timeout.
1364 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1365 char_time = char_time / 5;
1369 char_time = min(char_time, timeout);
1370 while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
1371 msleep_interruptible(jiffies_to_msecs(char_time));
1372 if (signal_pending(current))
1374 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1377 current->state = TASK_RUNNING;
1381 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1383 static void rs_hangup(struct tty_struct *tty)
1385 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1387 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1390 rs_flush_buffer(tty);
1394 info->flags &= ~ZILOG_NORMAL_ACTIVE;
1396 wake_up_interruptible(&info->open_wait);
1400 * ------------------------------------------------------------
1401 * rs_open() and friends
1402 * ------------------------------------------------------------
1404 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1405 struct dec_serial *info)
1407 DECLARE_WAITQUEUE(wait, current);
1412 * If the device is in the middle of being closed, then block
1413 * until it's done, and then try again.
1415 if (info->flags & ZILOG_CLOSING) {
1416 interruptible_sleep_on(&info->close_wait);
1417 #ifdef SERIAL_DO_RESTART
1418 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1419 -EAGAIN : -ERESTARTSYS);
1426 * If non-blocking mode is set, or the port is not enabled,
1427 * then make the check up front and then exit.
1429 if ((filp->f_flags & O_NONBLOCK) ||
1430 (tty->flags & (1 << TTY_IO_ERROR))) {
1431 info->flags |= ZILOG_NORMAL_ACTIVE;
1435 if (tty->termios->c_cflag & CLOCAL)
1439 * Block waiting for the carrier detect and the line to become
1440 * free (i.e., not in use by the callout). While we are in
1441 * this loop, info->count is dropped by one, so that
1442 * rs_close() knows when to free things. We restore it upon
1443 * exit, either normal or abnormal.
1446 add_wait_queue(&info->open_wait, &wait);
1447 #ifdef SERIAL_DEBUG_OPEN
1448 printk("block_til_ready before block: ttyS%d, count = %d\n",
1449 info->line, info->count);
1451 spin_lock(&zs_lock);
1452 if (!tty_hung_up_p(filp))
1454 spin_unlock_irq(&zs_lock);
1455 info->blocked_open++;
1457 spin_lock(&zs_lock);
1458 if (tty->termios->c_cflag & CBAUD)
1459 zs_rtsdtr(info, RTS | DTR, 1);
1460 spin_unlock_irq(&zs_lock);
1461 set_current_state(TASK_INTERRUPTIBLE);
1462 if (tty_hung_up_p(filp) ||
1463 !(info->flags & ZILOG_INITIALIZED)) {
1464 #ifdef SERIAL_DO_RESTART
1465 if (info->flags & ZILOG_HUP_NOTIFY)
1468 retval = -ERESTARTSYS;
1474 if (!(info->flags & ZILOG_CLOSING) &&
1475 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1477 if (signal_pending(current)) {
1478 retval = -ERESTARTSYS;
1481 #ifdef SERIAL_DEBUG_OPEN
1482 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1483 info->line, info->count);
1487 current->state = TASK_RUNNING;
1488 remove_wait_queue(&info->open_wait, &wait);
1489 if (!tty_hung_up_p(filp))
1491 info->blocked_open--;
1492 #ifdef SERIAL_DEBUG_OPEN
1493 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1494 info->line, info->count);
1498 info->flags |= ZILOG_NORMAL_ACTIVE;
1503 * This routine is called whenever a serial port is opened. It
1504 * enables interrupts for a serial port, linking in its ZILOG structure into
1505 * the IRQ chain. It also performs the serial-specific
1506 * initialization for the tty structure.
1508 static int rs_open(struct tty_struct *tty, struct file * filp)
1510 struct dec_serial *info;
1514 if ((line < 0) || (line >= zs_channels_found))
1516 info = zs_soft + line;
1521 if (serial_paranoia_check(info, tty->name, "rs_open"))
1523 #ifdef SERIAL_DEBUG_OPEN
1524 printk("rs_open %s, count = %d\n", tty->name, info->count);
1528 tty->driver_data = info;
1532 * If the port is the middle of closing, bail out now
1534 if (tty_hung_up_p(filp) ||
1535 (info->flags & ZILOG_CLOSING)) {
1536 if (info->flags & ZILOG_CLOSING)
1537 interruptible_sleep_on(&info->close_wait);
1538 #ifdef SERIAL_DO_RESTART
1539 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1540 -EAGAIN : -ERESTARTSYS);
1547 * Start up serial port
1549 retval = zs_startup(info);
1553 retval = block_til_ready(tty, filp, info);
1555 #ifdef SERIAL_DEBUG_OPEN
1556 printk("rs_open returning after block_til_ready with %d\n",
1562 #ifdef CONFIG_SERIAL_DEC_CONSOLE
1563 if (sercons.cflag && sercons.index == line) {
1564 tty->termios->c_cflag = sercons.cflag;
1570 #ifdef SERIAL_DEBUG_OPEN
1571 printk("rs_open %s successful...", tty->name);
1573 /* tty->low_latency = 1; */
1577 /* Finally, routines used to initialize the serial driver. */
1579 static void __init show_serial_version(void)
1581 printk("DECstation Z8530 serial driver version 0.09\n");
1584 /* Initialize Z8530s zs_channels
1587 static void __init probe_sccs(void)
1589 struct dec_serial **pp;
1590 int i, n, n_chips = 0, n_channels, chip, channel;
1591 unsigned long flags;
1594 * did we get here by accident?
1597 printk("Not on JUNKIO machine, skipping probe_sccs\n");
1601 switch(mips_machtype) {
1602 #ifdef CONFIG_MACH_DECSTATION
1603 case MACH_DS5000_2X0:
1606 zs_parms = &ds_parms;
1607 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1608 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1610 case MACH_DS5000_1XX:
1612 zs_parms = &ds_parms;
1613 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1614 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1616 case MACH_DS5000_XX:
1618 zs_parms = &ds_parms;
1619 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1623 panic("zs: unsupported bus");
1626 panic("zs: uninitialized parms");
1632 for (chip = 0; chip < n_chips; chip++) {
1633 for (channel = 0; channel <= 1; channel++) {
1635 * The sccs reside on the high byte of the 16 bit IOBUS
1637 zs_channels[n_channels].control =
1638 (volatile void *)CKSEG1ADDR(dec_kn_slot_base +
1639 (0 == chip ? zs_parms->scc0 : zs_parms->scc1) +
1640 (0 == channel ? zs_parms->channel_a_offset :
1641 zs_parms->channel_b_offset));
1642 zs_channels[n_channels].data =
1643 zs_channels[n_channels].control + 4;
1645 #ifndef CONFIG_SERIAL_DEC_CONSOLE
1647 * We're called early and memory managment isn't up, yet.
1648 * Thus request_region would fail.
1650 if (!request_region((unsigned long)
1651 zs_channels[n_channels].control,
1652 ZS_CHAN_IO_SIZE, "SCC"))
1653 panic("SCC I/O region is not free");
1655 zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1658 zs_soft[n_channels].irq = zs_parms->irq0;
1660 zs_soft[n_channels].irq = zs_parms->irq1;
1663 * Identification of channel A. Location of channel A
1664 * inside chip depends on mapping of internal address
1665 * the chip decodes channels by.
1666 * CHANNEL_A_NR returns either 0 (in case of
1667 * DECstations) or 1 (in case of Baget).
1669 if (CHANNEL_A_NR == channel)
1670 zs_soft[n_channels].zs_chan_a =
1671 &zs_channels[n_channels+1-2*CHANNEL_A_NR];
1673 zs_soft[n_channels].zs_chan_a =
1674 &zs_channels[n_channels];
1676 *pp = &zs_soft[n_channels];
1677 pp = &zs_soft[n_channels].zs_next;
1683 zs_channels_found = n_channels;
1685 for (n = 0; n < zs_channels_found; n++) {
1686 for (i = 0; i < 16; i++) {
1687 zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1691 spin_lock_irqsave(&zs_lock, flags);
1692 for (n = 0; n < zs_channels_found; n++) {
1694 write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
1696 write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
1698 load_zsregs(zs_soft[n].zs_channel,
1699 zs_soft[n].zs_channel->curregs);
1701 spin_unlock_irqrestore(&zs_lock, flags);
1704 static struct tty_operations serial_ops = {
1708 .flush_chars = rs_flush_chars,
1709 .write_room = rs_write_room,
1710 .chars_in_buffer = rs_chars_in_buffer,
1711 .flush_buffer = rs_flush_buffer,
1713 .throttle = rs_throttle,
1714 .unthrottle = rs_unthrottle,
1715 .set_termios = rs_set_termios,
1718 .hangup = rs_hangup,
1719 .break_ctl = rs_break,
1720 .wait_until_sent = rs_wait_until_sent,
1721 .tiocmget = rs_tiocmget,
1722 .tiocmset = rs_tiocmset,
1725 /* zs_init inits the driver */
1726 int __init zs_init(void)
1729 struct dec_serial *info;
1734 /* Find out how many Z8530 SCCs we have */
1737 serial_driver = alloc_tty_driver(zs_channels_found);
1741 show_serial_version();
1743 /* Initialize the tty_driver structure */
1744 /* Not all of this is exactly right for us. */
1746 serial_driver->owner = THIS_MODULE;
1747 serial_driver->name = "ttyS";
1748 serial_driver->major = TTY_MAJOR;
1749 serial_driver->minor_start = 64;
1750 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1751 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1752 serial_driver->init_termios = tty_std_termios;
1753 serial_driver->init_termios.c_cflag =
1754 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1755 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1756 tty_set_operations(serial_driver, &serial_ops);
1758 if (tty_register_driver(serial_driver))
1759 panic("Couldn't register serial driver");
1761 for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
1763 /* Needed before interrupts are enabled. */
1767 if (info->hook && info->hook->init_info) {
1768 (*info->hook->init_info)(info);
1772 info->magic = SERIAL_MAGIC;
1773 info->port = (int) info->zs_channel->control;
1775 info->custom_divisor = 16;
1776 info->close_delay = 50;
1777 info->closing_wait = 3000;
1780 info->blocked_open = 0;
1781 tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1782 init_waitqueue_head(&info->open_wait);
1783 init_waitqueue_head(&info->close_wait);
1784 printk("ttyS%02d at 0x%08x (irq = %d) is a Z85C30 SCC\n",
1785 info->line, info->port, info->irq);
1786 tty_register_device(serial_driver, info->line, NULL);
1790 for (channel = 0; channel < zs_channels_found; ++channel) {
1791 zs_soft[channel].clk_divisor = 16;
1792 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1794 if (request_irq(zs_soft[channel].irq, rs_interrupt, IRQF_SHARED,
1795 "scc", &zs_soft[channel]))
1796 printk(KERN_ERR "decserial: can't get irq %d\n",
1797 zs_soft[channel].irq);
1799 if (zs_soft[channel].hook) {
1800 zs_startup(&zs_soft[channel]);
1801 if (zs_soft[channel].hook->init_channel)
1802 (*zs_soft[channel].hook->init_channel)
1803 (&zs_soft[channel]);
1811 * polling I/O routines
1813 static int zs_poll_tx_char(void *handle, unsigned char ch)
1815 struct dec_serial *info = handle;
1816 struct dec_zschannel *chan = info->zs_channel;
1822 while (loops && !(read_zsreg(chan, 0) & Tx_BUF_EMP))
1826 write_zsdata(chan, ch);
1836 static int zs_poll_rx_char(void *handle)
1838 struct dec_serial *info = handle;
1839 struct dec_zschannel *chan = info->zs_channel;
1845 while (loops && !(read_zsreg(chan, 0) & Rx_CH_AV))
1849 ret = read_zsdata(chan);
1858 int register_zs_hook(unsigned int channel, struct dec_serial_hook *hook)
1860 struct dec_serial *info = &zs_soft[channel];
1863 printk("%s: line %d has already a hook registered\n",
1864 __FUNCTION__, channel);
1868 hook->poll_rx_char = zs_poll_rx_char;
1869 hook->poll_tx_char = zs_poll_tx_char;
1876 int unregister_zs_hook(unsigned int channel)
1878 struct dec_serial *info = &zs_soft[channel];
1884 printk("%s: trying to unregister hook on line %d,"
1885 " but none is registered\n", __FUNCTION__, channel);
1891 * ------------------------------------------------------------
1892 * Serial console driver
1893 * ------------------------------------------------------------
1895 #ifdef CONFIG_SERIAL_DEC_CONSOLE
1899 * Print a string to the serial port trying not to disturb
1900 * any possible real use of the port...
1902 static void serial_console_write(struct console *co, const char *s,
1905 struct dec_serial *info;
1908 info = zs_soft + co->index;
1910 for (i = 0; i < count; i++, s++) {
1912 zs_poll_tx_char(info, '\r');
1913 zs_poll_tx_char(info, *s);
1917 static struct tty_driver *serial_console_device(struct console *c, int *index)
1920 return serial_driver;
1924 * Setup initial baud/bits/parity. We do two things here:
1925 * - construct a cflag setting for the first rs_open()
1926 * - initialize the serial port
1927 * Return non-zero if we didn't find a serial port.
1929 static int __init serial_console_setup(struct console *co, char *options)
1931 struct dec_serial *info;
1935 int cflag = CREAD | HUPCL | CLOCAL;
1936 int clk_divisor = 16;
1939 unsigned long flags;
1944 info = zs_soft + co->index;
1952 baud = simple_strtoul(options, NULL, 10);
1954 while(*s >= '0' && *s <= '9')
1963 * Now construct a cflag setting.
1991 * Set this to a sane value to prevent a divide error.
2015 spin_lock_irqsave(&zs_lock, flags);
2018 * Set up the baud rate generator.
2020 brg = BPS_TO_BRG(baud, zs_parms->clock / clk_divisor);
2021 info->zs_channel->curregs[R12] = (brg & 255);
2022 info->zs_channel->curregs[R13] = ((brg >> 8) & 255);
2025 * Set byte size and parity.
2028 info->zs_channel->curregs[R3] |= Rx7;
2029 info->zs_channel->curregs[R5] |= Tx7;
2031 info->zs_channel->curregs[R3] |= Rx8;
2032 info->zs_channel->curregs[R5] |= Tx8;
2034 if (cflag & PARENB) {
2035 info->zs_channel->curregs[R4] |= PAR_ENA;
2037 if (!(cflag & PARODD)) {
2038 info->zs_channel->curregs[R4] |= PAR_EVEN;
2040 info->zs_channel->curregs[R4] |= SB1;
2043 * Turn on RTS and DTR.
2045 zs_rtsdtr(info, RTS | DTR, 1);
2048 * Finally, enable sequencing.
2050 info->zs_channel->curregs[R3] |= RxENABLE;
2051 info->zs_channel->curregs[R5] |= TxENAB;
2054 * Clear the interrupt registers.
2056 write_zsreg(info->zs_channel, R0, ERR_RES);
2057 write_zsreg(info->zs_channel, R0, RES_H_IUS);
2060 * Load up the new values.
2062 load_zsregs(info->zs_channel, info->zs_channel->curregs);
2064 /* Save the current value of RR0 */
2065 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
2067 zs_soft[co->index].clk_divisor = clk_divisor;
2068 zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
2070 spin_unlock_irqrestore(&zs_lock, flags);
2075 static struct console sercons = {
2077 .write = serial_console_write,
2078 .device = serial_console_device,
2079 .setup = serial_console_setup,
2080 .flags = CON_PRINTBUFFER,
2087 void __init zs_serial_console_init(void)
2089 register_console(&sercons);
2091 #endif /* ifdef CONFIG_SERIAL_DEC_CONSOLE */
2094 struct dec_zschannel *zs_kgdbchan;
2095 static unsigned char scc_inittab[] = {
2096 9, 0x80, /* reset A side (CHRA) */
2097 13, 0, /* set baud rate divisor */
2099 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
2100 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
2101 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
2102 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
2103 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
2106 /* These are for receiving and sending characters under the kgdb
2107 * source level kernel debugger.
2109 void putDebugChar(char kgdb_char)
2111 struct dec_zschannel *chan = zs_kgdbchan;
2112 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2114 write_zsdata(chan, kgdb_char);
2116 char getDebugChar(void)
2118 struct dec_zschannel *chan = zs_kgdbchan;
2119 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2120 eieio(); /*barrier();*/
2121 return read_zsdata(chan);
2123 void kgdb_interruptible(int yes)
2125 struct dec_zschannel *chan = zs_kgdbchan;
2127 nine = read_zsreg(chan, 9);
2129 one = EXT_INT_ENAB|RxINT_ALL;
2131 printk("turning serial ints on\n");
2135 printk("turning serial ints off\n");
2137 write_zsreg(chan, 1, one);
2138 write_zsreg(chan, 9, nine);
2141 static int kgdbhook_init_channel(void *handle)
2146 static void kgdbhook_init_info(void *handle)
2150 static void kgdbhook_rx_char(void *handle, unsigned char ch, unsigned char fl)
2152 struct dec_serial *info = handle;
2154 if (fl != TTY_NORMAL)
2156 if (ch == 0x03 || ch == '$')
2160 /* This sets up the serial port we're using, and turns on
2161 * interrupts for that channel, so kgdb is usable once we're done.
2163 static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2167 volatile char *sccc = ms->control;
2168 brg = BPS_TO_BRG(bps, zs_parms->clock/16);
2169 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2170 for (i = 20000; i != 0; --i) {
2173 for (i = 0; i < sizeof(scc_inittab); ++i) {
2174 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2178 /* This is called at boot time to prime the kgdb serial debugging
2179 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2180 * for /dev/ttyb which is determined in setup_arch() from the
2181 * boot command line flags.
2183 struct dec_serial_hook zs_kgdbhook = {
2184 .init_channel = kgdbhook_init_channel,
2185 .init_info = kgdbhook_init_info,
2186 .rx_char = kgdbhook_rx_char,
2187 .cflags = B38400 | CS8 | CLOCAL,
2190 void __init zs_kgdb_hook(int tty_num)
2192 /* Find out how many Z8530 SCCs we have */
2195 zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2196 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2197 zs_soft[tty_num].change_needed = 0;
2198 zs_soft[tty_num].clk_divisor = 16;
2199 zs_soft[tty_num].zs_baud = 38400;
2200 zs_soft[tty_num].hook = &zs_kgdbhook; /* This runs kgdb */
2201 /* Turn on transmitter/receiver at 8-bits/char */
2202 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2203 printk("KGDB: on channel %d initialized\n", tty_num);
2204 set_debug_traps(); /* init stub */
2206 #endif /* ifdef CONFIG_KGDB */