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)
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, 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)
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) {
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))
633 static int zs_startup(struct dec_serial * info)
637 if (info->flags & ZILOG_INITIALIZED)
640 if (!info->xmit_buf) {
641 info->xmit_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
646 spin_lock_irqsave(&zs_lock, flags);
648 #ifdef SERIAL_DEBUG_OPEN
649 printk("starting up ttyS%d (irq %d)...", info->line, info->irq);
653 * Clear the receive FIFO.
655 ZS_CLEARFIFO(info->zs_channel);
656 info->xmit_fifo_size = 1;
659 * Clear the interrupt registers.
661 write_zsreg(info->zs_channel, R0, ERR_RES);
662 write_zsreg(info->zs_channel, R0, RES_H_IUS);
665 * Set the speed of the serial port
670 * Turn on RTS and DTR.
672 zs_rtsdtr(info, RTS | DTR, 1);
675 * Finally, enable sequencing and interrupts
677 info->zs_channel->curregs[R1] &= ~RxINT_MASK;
678 info->zs_channel->curregs[R1] |= (RxINT_ALL | TxINT_ENAB |
680 info->zs_channel->curregs[R3] |= RxENABLE;
681 info->zs_channel->curregs[R5] |= TxENAB;
682 info->zs_channel->curregs[R15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
683 write_zsreg(info->zs_channel, R1, info->zs_channel->curregs[R1]);
684 write_zsreg(info->zs_channel, R3, info->zs_channel->curregs[R3]);
685 write_zsreg(info->zs_channel, R5, info->zs_channel->curregs[R5]);
686 write_zsreg(info->zs_channel, R15, info->zs_channel->curregs[R15]);
689 * And clear the interrupt registers again for luck.
691 write_zsreg(info->zs_channel, R0, ERR_RES);
692 write_zsreg(info->zs_channel, R0, RES_H_IUS);
694 /* Save the current value of RR0 */
695 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
698 clear_bit(TTY_IO_ERROR, &info->tty->flags);
699 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
701 info->flags |= ZILOG_INITIALIZED;
702 spin_unlock_irqrestore(&zs_lock, flags);
707 * This routine will shutdown a serial port; interrupts are disabled, and
708 * DTR is dropped if the hangup on close termio flag is on.
710 static void shutdown(struct dec_serial * info)
714 if (!(info->flags & ZILOG_INITIALIZED))
717 #ifdef SERIAL_DEBUG_OPEN
718 printk("Shutting down serial port %d (irq %d)....", info->line,
722 spin_lock_irqsave(&zs_lock, flags);
724 if (info->xmit_buf) {
725 free_page((unsigned long) info->xmit_buf);
729 info->zs_channel->curregs[1] = 0;
730 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]); /* no interrupts */
732 info->zs_channel->curregs[3] &= ~RxENABLE;
733 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
735 info->zs_channel->curregs[5] &= ~TxENAB;
736 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
737 if (!info->tty || C_HUPCL(info->tty)) {
738 zs_rtsdtr(info, RTS | DTR, 0);
742 set_bit(TTY_IO_ERROR, &info->tty->flags);
744 info->flags &= ~ZILOG_INITIALIZED;
745 spin_unlock_irqrestore(&zs_lock, flags);
749 * This routine is called to set the UART divisor registers to match
750 * the specified baud rate for a serial port.
752 static void change_speed(struct dec_serial *info)
760 if (!info->tty || !info->tty->termios)
762 cflag = info->tty->termios->c_cflag;
766 cflag = info->hook->cflags;
772 if (i < 1 || i > 2) {
774 info->tty->termios->c_cflag &= ~CBAUDEX;
776 info->hook->cflags &= ~CBAUDEX;
781 spin_lock_irqsave(&zs_lock, flags);
782 info->zs_baud = baud_table[i];
784 brg = BPS_TO_BRG(info->zs_baud, zs_parms->clock/info->clk_divisor);
785 info->zs_channel->curregs[12] = (brg & 255);
786 info->zs_channel->curregs[13] = ((brg >> 8) & 255);
787 zs_rtsdtr(info, DTR, 1);
789 zs_rtsdtr(info, RTS | DTR, 0);
793 /* byte size and parity */
794 info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
795 info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
796 switch (cflag & CSIZE) {
799 info->zs_channel->curregs[3] |= Rx5;
800 info->zs_channel->curregs[5] |= Tx5;
804 info->zs_channel->curregs[3] |= Rx6;
805 info->zs_channel->curregs[5] |= Tx6;
809 info->zs_channel->curregs[3] |= Rx7;
810 info->zs_channel->curregs[5] |= Tx7;
813 default: /* defaults to 8 bits */
815 info->zs_channel->curregs[3] |= Rx8;
816 info->zs_channel->curregs[5] |= Tx8;
820 info->timeout = ((info->xmit_fifo_size*HZ*bits) / info->zs_baud);
821 info->timeout += HZ/50; /* Add .02 seconds of slop */
823 info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
824 if (cflag & CSTOPB) {
825 info->zs_channel->curregs[4] |= SB2;
827 info->zs_channel->curregs[4] |= SB1;
829 if (cflag & PARENB) {
830 info->zs_channel->curregs[4] |= PAR_ENA;
832 if (!(cflag & PARODD)) {
833 info->zs_channel->curregs[4] |= PAR_EVEN;
836 if (!(cflag & CLOCAL)) {
837 if (!(info->zs_channel->curregs[15] & DCDIE))
838 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
839 info->zs_channel->curregs[15] |= DCDIE;
841 info->zs_channel->curregs[15] &= ~DCDIE;
842 if (cflag & CRTSCTS) {
843 info->zs_channel->curregs[15] |= CTSIE;
844 if ((read_zsreg(info->zs_channel, 0) & CTS) == 0)
845 info->tx_stopped = 1;
847 info->zs_channel->curregs[15] &= ~CTSIE;
848 info->tx_stopped = 0;
851 /* Load up the new values */
852 load_zsregs(info->zs_channel, info->zs_channel->curregs);
854 spin_unlock_irqrestore(&zs_lock, flags);
857 static void rs_flush_chars(struct tty_struct *tty)
859 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
862 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
865 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
869 /* Enable transmitter */
870 spin_lock_irqsave(&zs_lock, flags);
871 transmit_chars(info);
872 spin_unlock_irqrestore(&zs_lock, flags);
875 static int rs_write(struct tty_struct * tty,
876 const unsigned char *buf, int count)
879 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
882 if (serial_paranoia_check(info, tty->name, "rs_write"))
885 if (!tty || !info->xmit_buf)
889 spin_lock_irqsave(&zs_lock, flags);
890 c = min(count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
891 SERIAL_XMIT_SIZE - info->xmit_head));
895 memcpy(info->xmit_buf + info->xmit_head, buf, c);
896 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
898 spin_unlock_irqrestore(&zs_lock, flags);
904 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
906 transmit_chars(info);
907 spin_unlock_irqrestore(&zs_lock, flags);
911 static int rs_write_room(struct tty_struct *tty)
913 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
916 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
918 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
924 static int rs_chars_in_buffer(struct tty_struct *tty)
926 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
928 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
930 return info->xmit_cnt;
933 static void rs_flush_buffer(struct tty_struct *tty)
935 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
937 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
939 spin_lock_irq(&zs_lock);
940 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
941 spin_unlock_irq(&zs_lock);
946 * ------------------------------------------------------------
949 * This routine is called by the upper-layer tty layer to signal that
950 * incoming characters should be throttled.
951 * ------------------------------------------------------------
953 static void rs_throttle(struct tty_struct * tty)
955 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
958 #ifdef SERIAL_DEBUG_THROTTLE
961 printk("throttle %s: %d....\n", _tty_name(tty, buf),
962 tty->ldisc.chars_in_buffer(tty));
965 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
969 spin_lock_irqsave(&zs_lock, flags);
970 info->x_char = STOP_CHAR(tty);
971 if (!info->tx_active)
972 transmit_chars(info);
973 spin_unlock_irqrestore(&zs_lock, flags);
976 if (C_CRTSCTS(tty)) {
977 zs_rtsdtr(info, RTS, 0);
981 static void rs_unthrottle(struct tty_struct * tty)
983 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
986 #ifdef SERIAL_DEBUG_THROTTLE
989 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
990 tty->ldisc.chars_in_buffer(tty));
993 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
997 spin_lock_irqsave(&zs_lock, flags);
1001 info->x_char = START_CHAR(tty);
1002 if (!info->tx_active)
1003 transmit_chars(info);
1005 spin_unlock_irqrestore(&zs_lock, flags);
1008 if (C_CRTSCTS(tty)) {
1009 zs_rtsdtr(info, RTS, 1);
1014 * ------------------------------------------------------------
1015 * rs_ioctl() and friends
1016 * ------------------------------------------------------------
1019 static int get_serial_info(struct dec_serial * info,
1020 struct serial_struct * retinfo)
1022 struct serial_struct tmp;
1026 memset(&tmp, 0, sizeof(tmp));
1027 tmp.type = info->type;
1028 tmp.line = info->line;
1029 tmp.port = info->port;
1030 tmp.irq = info->irq;
1031 tmp.flags = info->flags;
1032 tmp.baud_base = info->baud_base;
1033 tmp.close_delay = info->close_delay;
1034 tmp.closing_wait = info->closing_wait;
1035 tmp.custom_divisor = info->custom_divisor;
1036 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
1039 static int set_serial_info(struct dec_serial * info,
1040 struct serial_struct * new_info)
1042 struct serial_struct new_serial;
1043 struct dec_serial old_info;
1048 copy_from_user(&new_serial,new_info,sizeof(new_serial));
1051 if (!capable(CAP_SYS_ADMIN)) {
1052 if ((new_serial.baud_base != info->baud_base) ||
1053 (new_serial.type != info->type) ||
1054 (new_serial.close_delay != info->close_delay) ||
1055 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1056 (info->flags & ~ZILOG_USR_MASK)))
1058 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1059 (new_serial.flags & ZILOG_USR_MASK));
1060 info->custom_divisor = new_serial.custom_divisor;
1061 goto check_and_exit;
1064 if (info->count > 1)
1068 * OK, past this point, all the error checking has been done.
1069 * At this point, we start making changes.....
1072 info->baud_base = new_serial.baud_base;
1073 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1074 (new_serial.flags & ZILOG_FLAGS));
1075 info->type = new_serial.type;
1076 info->close_delay = new_serial.close_delay;
1077 info->closing_wait = new_serial.closing_wait;
1080 retval = zs_startup(info);
1085 * get_lsr_info - get line status register info
1087 * Purpose: Let user call ioctl() to get info when the UART physically
1088 * is emptied. On bus types like RS485, the transmitter must
1089 * release the bus after transmitting. This must be done when
1090 * the transmit shift register is empty, not be done when the
1091 * transmit holding register is empty. This functionality
1092 * allows an RS485 driver to be written in user space.
1094 static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1096 unsigned char status;
1098 spin_lock(&zs_lock);
1099 status = read_zsreg(info->zs_channel, 0);
1100 spin_unlock_irq(&zs_lock);
1101 put_user(status,value);
1105 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1107 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1108 unsigned char control, status_a, status_b;
1109 unsigned int result;
1114 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1117 if (tty->flags & (1 << TTY_IO_ERROR))
1120 if (info->zs_channel == info->zs_chan_a)
1123 spin_lock(&zs_lock);
1124 control = info->zs_chan_a->curregs[5];
1125 status_a = read_zsreg(info->zs_chan_a, 0);
1126 status_b = read_zsreg(info->zs_channel, 0);
1127 spin_unlock_irq(&zs_lock);
1128 result = ((control & RTS) ? TIOCM_RTS: 0)
1129 | ((control & DTR) ? TIOCM_DTR: 0)
1130 | ((status_b & DCD) ? TIOCM_CAR: 0)
1131 | ((status_a & DCD) ? TIOCM_RNG: 0)
1132 | ((status_a & SYNC_HUNT) ? TIOCM_DSR: 0)
1133 | ((status_b & CTS) ? TIOCM_CTS: 0);
1138 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1139 unsigned int set, unsigned int clear)
1141 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1146 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1149 if (tty->flags & (1 << TTY_IO_ERROR))
1152 if (info->zs_channel == info->zs_chan_a)
1155 spin_lock(&zs_lock);
1156 if (set & TIOCM_RTS)
1157 info->zs_chan_a->curregs[5] |= RTS;
1158 if (set & TIOCM_DTR)
1159 info->zs_chan_a->curregs[5] |= DTR;
1160 if (clear & TIOCM_RTS)
1161 info->zs_chan_a->curregs[5] &= ~RTS;
1162 if (clear & TIOCM_DTR)
1163 info->zs_chan_a->curregs[5] &= ~DTR;
1164 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1165 spin_unlock_irq(&zs_lock);
1170 * rs_break - turn transmit break condition on/off
1172 static void rs_break(struct tty_struct *tty, int break_state)
1174 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1175 unsigned long flags;
1177 if (serial_paranoia_check(info, tty->name, "rs_break"))
1182 spin_lock_irqsave(&zs_lock, flags);
1183 if (break_state == -1)
1184 info->zs_channel->curregs[5] |= SND_BRK;
1186 info->zs_channel->curregs[5] &= ~SND_BRK;
1187 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1188 spin_unlock_irqrestore(&zs_lock, flags);
1191 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1192 unsigned int cmd, unsigned long arg)
1194 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1199 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1202 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1203 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1204 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1205 if (tty->flags & (1 << TTY_IO_ERROR))
1211 if (!access_ok(VERIFY_WRITE, (void *)arg,
1212 sizeof(struct serial_struct)))
1214 return get_serial_info(info, (struct serial_struct *)arg);
1217 return set_serial_info(info, (struct serial_struct *)arg);
1219 case TIOCSERGETLSR: /* Get line status register */
1220 if (!access_ok(VERIFY_WRITE, (void *)arg,
1221 sizeof(unsigned int)))
1223 return get_lsr_info(info, (unsigned int *)arg);
1225 case TIOCSERGSTRUCT:
1226 if (!access_ok(VERIFY_WRITE, (void *)arg,
1227 sizeof(struct dec_serial)))
1229 copy_from_user((struct dec_serial *)arg, info,
1230 sizeof(struct dec_serial));
1234 return -ENOIOCTLCMD;
1239 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1241 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1244 if (tty->termios->c_cflag == old_termios->c_cflag)
1246 was_stopped = info->tx_stopped;
1250 if (was_stopped && !info->tx_stopped)
1255 * ------------------------------------------------------------
1258 * This routine is called when the serial port gets closed.
1259 * Wait for the last remaining data to be sent.
1260 * ------------------------------------------------------------
1262 static void rs_close(struct tty_struct *tty, struct file * filp)
1264 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1265 unsigned long flags;
1267 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1270 spin_lock_irqsave(&zs_lock, flags);
1272 if (tty_hung_up_p(filp)) {
1273 spin_unlock_irqrestore(&zs_lock, flags);
1277 #ifdef SERIAL_DEBUG_OPEN
1278 printk("rs_close ttyS%d, count = %d\n", info->line, info->count);
1280 if ((tty->count == 1) && (info->count != 1)) {
1282 * Uh, oh. tty->count is 1, which means that the tty
1283 * structure will be freed. Info->count should always
1284 * be one in these conditions. If it's greater than
1285 * one, we've got real problems, since it means the
1286 * serial port won't be shutdown.
1288 printk("rs_close: bad serial port count; tty->count is 1, "
1289 "info->count is %d\n", info->count);
1292 if (--info->count < 0) {
1293 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1294 info->line, info->count);
1298 spin_unlock_irqrestore(&zs_lock, flags);
1301 info->flags |= ZILOG_CLOSING;
1303 * Now we wait for the transmit buffer to clear; and we notify
1304 * the line discipline to only process XON/XOFF characters.
1307 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1308 tty_wait_until_sent(tty, info->closing_wait);
1310 * At this point we stop accepting input. To do this, we
1311 * disable the receiver and receive interrupts.
1313 info->zs_channel->curregs[3] &= ~RxENABLE;
1314 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1315 info->zs_channel->curregs[1] = 0; /* disable any rx ints */
1316 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1317 ZS_CLEARFIFO(info->zs_channel);
1318 if (info->flags & ZILOG_INITIALIZED) {
1320 * Before we drop DTR, make sure the SCC transmitter
1321 * has completely drained.
1323 rs_wait_until_sent(tty, info->timeout);
1327 if (tty->driver->flush_buffer)
1328 tty->driver->flush_buffer(tty);
1329 tty_ldisc_flush(tty);
1333 if (info->blocked_open) {
1334 if (info->close_delay) {
1335 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1337 wake_up_interruptible(&info->open_wait);
1339 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CLOSING);
1340 wake_up_interruptible(&info->close_wait);
1341 spin_unlock_irqrestore(&zs_lock, flags);
1345 * rs_wait_until_sent() --- wait until the transmitter is empty
1347 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1349 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1350 unsigned long orig_jiffies;
1353 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1356 orig_jiffies = jiffies;
1358 * Set the check interval to be 1/5 of the estimated time to
1359 * send a single character, and make it at least 1. The check
1360 * interval should also be less than the timeout.
1362 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1363 char_time = char_time / 5;
1367 char_time = min(char_time, timeout);
1368 while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
1369 msleep_interruptible(jiffies_to_msecs(char_time));
1370 if (signal_pending(current))
1372 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1375 current->state = TASK_RUNNING;
1379 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1381 static void rs_hangup(struct tty_struct *tty)
1383 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1385 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1388 rs_flush_buffer(tty);
1392 info->flags &= ~ZILOG_NORMAL_ACTIVE;
1394 wake_up_interruptible(&info->open_wait);
1398 * ------------------------------------------------------------
1399 * rs_open() and friends
1400 * ------------------------------------------------------------
1402 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1403 struct dec_serial *info)
1405 DECLARE_WAITQUEUE(wait, current);
1410 * If the device is in the middle of being closed, then block
1411 * until it's done, and then try again.
1413 if (info->flags & ZILOG_CLOSING) {
1414 interruptible_sleep_on(&info->close_wait);
1415 #ifdef SERIAL_DO_RESTART
1416 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1417 -EAGAIN : -ERESTARTSYS);
1424 * If non-blocking mode is set, or the port is not enabled,
1425 * then make the check up front and then exit.
1427 if ((filp->f_flags & O_NONBLOCK) ||
1428 (tty->flags & (1 << TTY_IO_ERROR))) {
1429 info->flags |= ZILOG_NORMAL_ACTIVE;
1433 if (tty->termios->c_cflag & CLOCAL)
1437 * Block waiting for the carrier detect and the line to become
1438 * free (i.e., not in use by the callout). While we are in
1439 * this loop, info->count is dropped by one, so that
1440 * rs_close() knows when to free things. We restore it upon
1441 * exit, either normal or abnormal.
1444 add_wait_queue(&info->open_wait, &wait);
1445 #ifdef SERIAL_DEBUG_OPEN
1446 printk("block_til_ready before block: ttyS%d, count = %d\n",
1447 info->line, info->count);
1449 spin_lock(&zs_lock);
1450 if (!tty_hung_up_p(filp))
1452 spin_unlock_irq(&zs_lock);
1453 info->blocked_open++;
1455 spin_lock(&zs_lock);
1456 if (tty->termios->c_cflag & CBAUD)
1457 zs_rtsdtr(info, RTS | DTR, 1);
1458 spin_unlock_irq(&zs_lock);
1459 set_current_state(TASK_INTERRUPTIBLE);
1460 if (tty_hung_up_p(filp) ||
1461 !(info->flags & ZILOG_INITIALIZED)) {
1462 #ifdef SERIAL_DO_RESTART
1463 if (info->flags & ZILOG_HUP_NOTIFY)
1466 retval = -ERESTARTSYS;
1472 if (!(info->flags & ZILOG_CLOSING) &&
1473 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1475 if (signal_pending(current)) {
1476 retval = -ERESTARTSYS;
1479 #ifdef SERIAL_DEBUG_OPEN
1480 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1481 info->line, info->count);
1485 current->state = TASK_RUNNING;
1486 remove_wait_queue(&info->open_wait, &wait);
1487 if (!tty_hung_up_p(filp))
1489 info->blocked_open--;
1490 #ifdef SERIAL_DEBUG_OPEN
1491 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1492 info->line, info->count);
1496 info->flags |= ZILOG_NORMAL_ACTIVE;
1501 * This routine is called whenever a serial port is opened. It
1502 * enables interrupts for a serial port, linking in its ZILOG structure into
1503 * the IRQ chain. It also performs the serial-specific
1504 * initialization for the tty structure.
1506 static int rs_open(struct tty_struct *tty, struct file * filp)
1508 struct dec_serial *info;
1512 if ((line < 0) || (line >= zs_channels_found))
1514 info = zs_soft + line;
1519 if (serial_paranoia_check(info, tty->name, "rs_open"))
1521 #ifdef SERIAL_DEBUG_OPEN
1522 printk("rs_open %s, count = %d\n", tty->name, info->count);
1526 tty->driver_data = info;
1530 * If the port is the middle of closing, bail out now
1532 if (tty_hung_up_p(filp) ||
1533 (info->flags & ZILOG_CLOSING)) {
1534 if (info->flags & ZILOG_CLOSING)
1535 interruptible_sleep_on(&info->close_wait);
1536 #ifdef SERIAL_DO_RESTART
1537 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1538 -EAGAIN : -ERESTARTSYS);
1545 * Start up serial port
1547 retval = zs_startup(info);
1551 retval = block_til_ready(tty, filp, info);
1553 #ifdef SERIAL_DEBUG_OPEN
1554 printk("rs_open returning after block_til_ready with %d\n",
1560 #ifdef CONFIG_SERIAL_DEC_CONSOLE
1561 if (sercons.cflag && sercons.index == line) {
1562 tty->termios->c_cflag = sercons.cflag;
1568 #ifdef SERIAL_DEBUG_OPEN
1569 printk("rs_open %s successful...", tty->name);
1571 /* tty->low_latency = 1; */
1575 /* Finally, routines used to initialize the serial driver. */
1577 static void __init show_serial_version(void)
1579 printk("DECstation Z8530 serial driver version 0.09\n");
1582 /* Initialize Z8530s zs_channels
1585 static void __init probe_sccs(void)
1587 struct dec_serial **pp;
1588 int i, n, n_chips = 0, n_channels, chip, channel;
1589 unsigned long flags;
1592 * did we get here by accident?
1595 printk("Not on JUNKIO machine, skipping probe_sccs\n");
1599 switch(mips_machtype) {
1600 #ifdef CONFIG_MACH_DECSTATION
1601 case MACH_DS5000_2X0:
1604 zs_parms = &ds_parms;
1605 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1606 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1608 case MACH_DS5000_1XX:
1610 zs_parms = &ds_parms;
1611 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1612 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1614 case MACH_DS5000_XX:
1616 zs_parms = &ds_parms;
1617 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1621 panic("zs: unsupported bus");
1624 panic("zs: uninitialized parms");
1630 for (chip = 0; chip < n_chips; chip++) {
1631 for (channel = 0; channel <= 1; channel++) {
1633 * The sccs reside on the high byte of the 16 bit IOBUS
1635 zs_channels[n_channels].control =
1636 (volatile void *)CKSEG1ADDR(dec_kn_slot_base +
1637 (0 == chip ? zs_parms->scc0 : zs_parms->scc1) +
1638 (0 == channel ? zs_parms->channel_a_offset :
1639 zs_parms->channel_b_offset));
1640 zs_channels[n_channels].data =
1641 zs_channels[n_channels].control + 4;
1643 #ifndef CONFIG_SERIAL_DEC_CONSOLE
1645 * We're called early and memory managment isn't up, yet.
1646 * Thus request_region would fail.
1648 if (!request_region((unsigned long)
1649 zs_channels[n_channels].control,
1650 ZS_CHAN_IO_SIZE, "SCC"))
1651 panic("SCC I/O region is not free");
1653 zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1656 zs_soft[n_channels].irq = zs_parms->irq0;
1658 zs_soft[n_channels].irq = zs_parms->irq1;
1661 * Identification of channel A. Location of channel A
1662 * inside chip depends on mapping of internal address
1663 * the chip decodes channels by.
1664 * CHANNEL_A_NR returns either 0 (in case of
1665 * DECstations) or 1 (in case of Baget).
1667 if (CHANNEL_A_NR == channel)
1668 zs_soft[n_channels].zs_chan_a =
1669 &zs_channels[n_channels+1-2*CHANNEL_A_NR];
1671 zs_soft[n_channels].zs_chan_a =
1672 &zs_channels[n_channels];
1674 *pp = &zs_soft[n_channels];
1675 pp = &zs_soft[n_channels].zs_next;
1681 zs_channels_found = n_channels;
1683 for (n = 0; n < zs_channels_found; n++) {
1684 for (i = 0; i < 16; i++) {
1685 zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1689 spin_lock_irqsave(&zs_lock, flags);
1690 for (n = 0; n < zs_channels_found; n++) {
1692 write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
1694 write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
1696 load_zsregs(zs_soft[n].zs_channel,
1697 zs_soft[n].zs_channel->curregs);
1699 spin_unlock_irqrestore(&zs_lock, flags);
1702 static const struct tty_operations serial_ops = {
1706 .flush_chars = rs_flush_chars,
1707 .write_room = rs_write_room,
1708 .chars_in_buffer = rs_chars_in_buffer,
1709 .flush_buffer = rs_flush_buffer,
1711 .throttle = rs_throttle,
1712 .unthrottle = rs_unthrottle,
1713 .set_termios = rs_set_termios,
1716 .hangup = rs_hangup,
1717 .break_ctl = rs_break,
1718 .wait_until_sent = rs_wait_until_sent,
1719 .tiocmget = rs_tiocmget,
1720 .tiocmset = rs_tiocmset,
1723 /* zs_init inits the driver */
1724 int __init zs_init(void)
1727 struct dec_serial *info;
1732 /* Find out how many Z8530 SCCs we have */
1735 serial_driver = alloc_tty_driver(zs_channels_found);
1739 show_serial_version();
1741 /* Initialize the tty_driver structure */
1742 /* Not all of this is exactly right for us. */
1744 serial_driver->owner = THIS_MODULE;
1745 serial_driver->name = "ttyS";
1746 serial_driver->major = TTY_MAJOR;
1747 serial_driver->minor_start = 64;
1748 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1749 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1750 serial_driver->init_termios = tty_std_termios;
1751 serial_driver->init_termios.c_cflag =
1752 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1753 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1754 tty_set_operations(serial_driver, &serial_ops);
1756 if (tty_register_driver(serial_driver))
1757 panic("Couldn't register serial driver");
1759 for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
1761 /* Needed before interrupts are enabled. */
1765 if (info->hook && info->hook->init_info) {
1766 (*info->hook->init_info)(info);
1770 info->magic = SERIAL_MAGIC;
1771 info->port = (int) info->zs_channel->control;
1773 info->custom_divisor = 16;
1774 info->close_delay = 50;
1775 info->closing_wait = 3000;
1778 info->blocked_open = 0;
1779 tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1780 init_waitqueue_head(&info->open_wait);
1781 init_waitqueue_head(&info->close_wait);
1782 printk("ttyS%02d at 0x%08x (irq = %d) is a Z85C30 SCC\n",
1783 info->line, info->port, info->irq);
1784 tty_register_device(serial_driver, info->line, NULL);
1788 for (channel = 0; channel < zs_channels_found; ++channel) {
1789 zs_soft[channel].clk_divisor = 16;
1790 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1792 if (request_irq(zs_soft[channel].irq, rs_interrupt, IRQF_SHARED,
1793 "scc", &zs_soft[channel]))
1794 printk(KERN_ERR "decserial: can't get irq %d\n",
1795 zs_soft[channel].irq);
1797 if (zs_soft[channel].hook) {
1798 zs_startup(&zs_soft[channel]);
1799 if (zs_soft[channel].hook->init_channel)
1800 (*zs_soft[channel].hook->init_channel)
1801 (&zs_soft[channel]);
1809 * polling I/O routines
1811 static int zs_poll_tx_char(void *handle, unsigned char ch)
1813 struct dec_serial *info = handle;
1814 struct dec_zschannel *chan = info->zs_channel;
1820 while (loops && !(read_zsreg(chan, 0) & Tx_BUF_EMP))
1824 write_zsdata(chan, ch);
1834 static int zs_poll_rx_char(void *handle)
1836 struct dec_serial *info = handle;
1837 struct dec_zschannel *chan = info->zs_channel;
1843 while (loops && !(read_zsreg(chan, 0) & Rx_CH_AV))
1847 ret = read_zsdata(chan);
1856 int register_zs_hook(unsigned int channel, struct dec_serial_hook *hook)
1858 struct dec_serial *info = &zs_soft[channel];
1861 printk("%s: line %d has already a hook registered\n",
1862 __FUNCTION__, channel);
1866 hook->poll_rx_char = zs_poll_rx_char;
1867 hook->poll_tx_char = zs_poll_tx_char;
1874 int unregister_zs_hook(unsigned int channel)
1876 struct dec_serial *info = &zs_soft[channel];
1882 printk("%s: trying to unregister hook on line %d,"
1883 " but none is registered\n", __FUNCTION__, channel);
1889 * ------------------------------------------------------------
1890 * Serial console driver
1891 * ------------------------------------------------------------
1893 #ifdef CONFIG_SERIAL_DEC_CONSOLE
1897 * Print a string to the serial port trying not to disturb
1898 * any possible real use of the port...
1900 static void serial_console_write(struct console *co, const char *s,
1903 struct dec_serial *info;
1906 info = zs_soft + co->index;
1908 for (i = 0; i < count; i++, s++) {
1910 zs_poll_tx_char(info, '\r');
1911 zs_poll_tx_char(info, *s);
1915 static struct tty_driver *serial_console_device(struct console *c, int *index)
1918 return serial_driver;
1922 * Setup initial baud/bits/parity. We do two things here:
1923 * - construct a cflag setting for the first rs_open()
1924 * - initialize the serial port
1925 * Return non-zero if we didn't find a serial port.
1927 static int __init serial_console_setup(struct console *co, char *options)
1929 struct dec_serial *info;
1933 int cflag = CREAD | HUPCL | CLOCAL;
1934 int clk_divisor = 16;
1937 unsigned long flags;
1942 info = zs_soft + co->index;
1950 baud = simple_strtoul(options, NULL, 10);
1952 while(*s >= '0' && *s <= '9')
1961 * Now construct a cflag setting.
1989 * Set this to a sane value to prevent a divide error.
2013 spin_lock_irqsave(&zs_lock, flags);
2016 * Set up the baud rate generator.
2018 brg = BPS_TO_BRG(baud, zs_parms->clock / clk_divisor);
2019 info->zs_channel->curregs[R12] = (brg & 255);
2020 info->zs_channel->curregs[R13] = ((brg >> 8) & 255);
2023 * Set byte size and parity.
2026 info->zs_channel->curregs[R3] |= Rx7;
2027 info->zs_channel->curregs[R5] |= Tx7;
2029 info->zs_channel->curregs[R3] |= Rx8;
2030 info->zs_channel->curregs[R5] |= Tx8;
2032 if (cflag & PARENB) {
2033 info->zs_channel->curregs[R4] |= PAR_ENA;
2035 if (!(cflag & PARODD)) {
2036 info->zs_channel->curregs[R4] |= PAR_EVEN;
2038 info->zs_channel->curregs[R4] |= SB1;
2041 * Turn on RTS and DTR.
2043 zs_rtsdtr(info, RTS | DTR, 1);
2046 * Finally, enable sequencing.
2048 info->zs_channel->curregs[R3] |= RxENABLE;
2049 info->zs_channel->curregs[R5] |= TxENAB;
2052 * Clear the interrupt registers.
2054 write_zsreg(info->zs_channel, R0, ERR_RES);
2055 write_zsreg(info->zs_channel, R0, RES_H_IUS);
2058 * Load up the new values.
2060 load_zsregs(info->zs_channel, info->zs_channel->curregs);
2062 /* Save the current value of RR0 */
2063 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
2065 zs_soft[co->index].clk_divisor = clk_divisor;
2066 zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
2068 spin_unlock_irqrestore(&zs_lock, flags);
2073 static struct console sercons = {
2075 .write = serial_console_write,
2076 .device = serial_console_device,
2077 .setup = serial_console_setup,
2078 .flags = CON_PRINTBUFFER,
2085 void __init zs_serial_console_init(void)
2087 register_console(&sercons);
2089 #endif /* ifdef CONFIG_SERIAL_DEC_CONSOLE */
2092 struct dec_zschannel *zs_kgdbchan;
2093 static unsigned char scc_inittab[] = {
2094 9, 0x80, /* reset A side (CHRA) */
2095 13, 0, /* set baud rate divisor */
2097 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
2098 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
2099 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
2100 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
2101 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
2104 /* These are for receiving and sending characters under the kgdb
2105 * source level kernel debugger.
2107 void putDebugChar(char kgdb_char)
2109 struct dec_zschannel *chan = zs_kgdbchan;
2110 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2112 write_zsdata(chan, kgdb_char);
2114 char getDebugChar(void)
2116 struct dec_zschannel *chan = zs_kgdbchan;
2117 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2118 eieio(); /*barrier();*/
2119 return read_zsdata(chan);
2121 void kgdb_interruptible(int yes)
2123 struct dec_zschannel *chan = zs_kgdbchan;
2125 nine = read_zsreg(chan, 9);
2127 one = EXT_INT_ENAB|RxINT_ALL;
2129 printk("turning serial ints on\n");
2133 printk("turning serial ints off\n");
2135 write_zsreg(chan, 1, one);
2136 write_zsreg(chan, 9, nine);
2139 static int kgdbhook_init_channel(void *handle)
2144 static void kgdbhook_init_info(void *handle)
2148 static void kgdbhook_rx_char(void *handle, unsigned char ch, unsigned char fl)
2150 struct dec_serial *info = handle;
2152 if (fl != TTY_NORMAL)
2154 if (ch == 0x03 || ch == '$')
2158 /* This sets up the serial port we're using, and turns on
2159 * interrupts for that channel, so kgdb is usable once we're done.
2161 static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2165 volatile char *sccc = ms->control;
2166 brg = BPS_TO_BRG(bps, zs_parms->clock/16);
2167 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2168 for (i = 20000; i != 0; --i) {
2171 for (i = 0; i < sizeof(scc_inittab); ++i) {
2172 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2176 /* This is called at boot time to prime the kgdb serial debugging
2177 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2178 * for /dev/ttyb which is determined in setup_arch() from the
2179 * boot command line flags.
2181 struct dec_serial_hook zs_kgdbhook = {
2182 .init_channel = kgdbhook_init_channel,
2183 .init_info = kgdbhook_init_info,
2184 .rx_char = kgdbhook_rx_char,
2185 .cflags = B38400 | CS8 | CLOCAL,
2188 void __init zs_kgdb_hook(int tty_num)
2190 /* Find out how many Z8530 SCCs we have */
2193 zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2194 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2195 zs_soft[tty_num].change_needed = 0;
2196 zs_soft[tty_num].clk_divisor = 16;
2197 zs_soft[tty_num].zs_baud = 38400;
2198 zs_soft[tty_num].hook = &zs_kgdbhook; /* This runs kgdb */
2199 /* Turn on transmitter/receiver at 8-bits/char */
2200 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2201 printk("KGDB: on channel %d initialized\n", tty_num);
2202 set_debug_traps(); /* init stub */
2204 #endif /* ifdef CONFIG_KGDB */