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 Maciej W. Rozycki
11 * For the rest of the code the original Copyright applies:
12 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
13 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
16 * Note: for IOASIC systems the wiring is as follows:
19 * DIN-7 MJ-4 signal SCC
24 * DB-25 MMJ-6 signal SCC
31 * 12 DSRS(DCE) -> ~A.CTS (*)
36 * 23 DSRS(DTE) <- ~B.RTS
38 * (*) EIA-232 defines the signal at this pin to be SCD, while DSRS(DCE)
39 * is shared with DSRS(DTE) at pin 23.
42 #include <linux/config.h>
43 #include <linux/version.h>
44 #include <linux/errno.h>
45 #include <linux/signal.h>
46 #include <linux/sched.h>
47 #include <linux/timer.h>
48 #include <linux/interrupt.h>
49 #include <linux/tty.h>
50 #include <linux/tty_flip.h>
51 #include <linux/major.h>
52 #include <linux/string.h>
53 #include <linux/fcntl.h>
55 #include <linux/kernel.h>
56 #include <linux/delay.h>
57 #include <linux/init.h>
58 #include <linux/ioport.h>
59 #ifdef CONFIG_SERIAL_DEC_CONSOLE
60 #include <linux/console.h>
64 #include <asm/pgtable.h>
66 #include <asm/system.h>
67 #include <asm/segment.h>
68 #include <asm/bitops.h>
69 #include <asm/uaccess.h>
70 #include <asm/bootinfo.h>
71 #ifdef CONFIG_DECSTATION
72 #include <asm/dec/interrupts.h>
73 #include <asm/dec/machtype.h>
74 #include <asm/dec/tc.h>
75 #include <asm/dec/ioasic_addrs.h>
77 #ifdef CONFIG_BAGET_MIPS
78 #include <asm/baget/baget.h>
79 unsigned long system_base;
84 #ifdef CONFIG_MAGIC_SYSRQ
85 #include <linux/sysrq.h>
91 * It would be nice to dynamically allocate everything that
92 * depends on NUM_SERIAL, so we could support any number of
93 * Z8530s, but for now...
95 #define NUM_SERIAL 2 /* Max number of ZS chips supported */
96 #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
97 #define CHANNEL_A_NR (zs_parms->channel_a_offset > zs_parms->channel_b_offset)
98 /* Number of channel A in the chip */
99 #define ZS_CHAN_IO_SIZE 8
100 #define ZS_CLOCK 7372800 /* Z8530 RTxC input clock rate */
102 #define RECOVERY_DELAY udelay(2)
107 int channel_a_offset;
108 int channel_b_offset;
114 static struct zs_parms *zs_parms;
116 #ifdef CONFIG_DECSTATION
117 static struct zs_parms ds_parms = {
120 channel_a_offset : 1,
121 channel_b_offset : 9,
127 #ifdef CONFIG_BAGET_MIPS
128 static struct zs_parms baget_parms = {
131 channel_a_offset : 9,
132 channel_b_offset : 1,
133 irq0 : BAGET_SCC_IRQ,
134 irq1 : BAGET_SCC_IRQ,
139 #ifdef CONFIG_DECSTATION
140 #define DS_BUS_PRESENT (IOASIC)
142 #define DS_BUS_PRESENT 0
145 #ifdef CONFIG_BAGET_MIPS
146 #define BAGET_BUS_PRESENT (mips_machtype == MACH_BAGET202)
148 #define BAGET_BUS_PRESENT 0
151 #define BUS_PRESENT (DS_BUS_PRESENT || BAGET_BUS_PRESENT)
153 struct dec_zschannel zs_channels[NUM_CHANNELS];
154 struct dec_serial zs_soft[NUM_CHANNELS];
155 int zs_channels_found;
156 struct dec_serial *zs_chain; /* list of all channels */
158 struct tty_struct zs_ttys[NUM_CHANNELS];
160 #ifdef CONFIG_SERIAL_DEC_CONSOLE
161 static struct console sercons;
163 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) \
165 static unsigned long break_pressed; /* break, really ... */
168 static unsigned char zs_init_regs[16] __initdata = {
173 (X16CLK), /* write 4 */
175 0, 0, 0, /* write 6, 7, 8 */
176 (MIE | DLC | NV), /* write 9 */
177 (NRZ), /* write 10 */
178 (TCBR | RCBR), /* write 11 */
179 0, 0, /* BRG time constant, write 12 + 13 */
180 (BRSRC | BRENABL), /* write 14 */
184 DECLARE_TASK_QUEUE(tq_zs_serial);
186 struct tty_driver serial_driver, callout_driver;
187 static int serial_refcount;
189 /* serial subtype definitions */
190 #define SERIAL_TYPE_NORMAL 1
191 #define SERIAL_TYPE_CALLOUT 2
193 /* number of characters left in xmit buffer before we ask for more */
194 #define WAKEUP_CHARS 256
199 #undef SERIAL_DEBUG_INTR
200 #undef SERIAL_DEBUG_OPEN
201 #undef SERIAL_DEBUG_FLOW
202 #undef SERIAL_DEBUG_THROTTLE
203 #undef SERIAL_PARANOIA_CHECK
207 #ifdef SERIAL_DEBUG_THROTTLE
208 #define _tty_name(tty,buf) tty_name(tty,buf)
211 #define RS_STROBE_TIME 10
212 #define RS_ISR_PASS_LIMIT 256
214 #define _INLINE_ inline
216 static void probe_sccs(void);
217 static void change_speed(struct dec_serial *info);
218 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
220 static struct tty_struct *serial_table[NUM_CHANNELS];
221 static struct termios *serial_termios[NUM_CHANNELS];
222 static struct termios *serial_termios_locked[NUM_CHANNELS];
225 #define MIN(a,b) ((a) < (b) ? (a) : (b))
229 * tmp_buf is used as a temporary buffer by serial_write. We need to
230 * lock it in case the copy_from_user blocks while swapping in a page,
231 * and some other program tries to do a serial write at the same time.
232 * Since the lock will only come under contention when the system is
233 * swapping and available memory is low, it makes sense to share one
234 * buffer across all the serial ports, since it significantly saves
235 * memory if large numbers of serial ports are open.
237 static unsigned char tmp_buf[4096]; /* This is cheating */
238 static DECLARE_MUTEX(tmp_buf_sem);
240 static inline int serial_paranoia_check(struct dec_serial *info,
241 dev_t device, const char *routine)
243 #ifdef SERIAL_PARANOIA_CHECK
244 static const char *badmagic =
245 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
246 static const char *badinfo =
247 "Warning: null mac_serial for (%d, %d) in %s\n";
250 printk(badinfo, MAJOR(device), MINOR(device), routine);
253 if (info->magic != SERIAL_MAGIC) {
254 printk(badmagic, MAJOR(device), MINOR(device), routine);
262 * This is used to figure out the divisor speeds and the timeouts
264 static int baud_table[] = {
265 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
266 9600, 19200, 38400, 57600, 115200, 0 };
269 * Reading and writing Z8530 registers.
271 static inline unsigned char read_zsreg(struct dec_zschannel *channel,
274 unsigned char retval;
277 *channel->control = reg & 0xf;
278 fast_iob(); RECOVERY_DELAY;
280 retval = *channel->control;
285 static inline void write_zsreg(struct dec_zschannel *channel,
286 unsigned char reg, unsigned char value)
289 *channel->control = reg & 0xf;
290 fast_iob(); RECOVERY_DELAY;
292 *channel->control = value;
293 fast_iob(); RECOVERY_DELAY;
297 static inline unsigned char read_zsdata(struct dec_zschannel *channel)
299 unsigned char retval;
301 retval = *channel->data;
306 static inline void write_zsdata(struct dec_zschannel *channel,
309 *channel->data = value;
310 fast_iob(); RECOVERY_DELAY;
314 static inline void load_zsregs(struct dec_zschannel *channel,
317 /* ZS_CLEARERR(channel);
318 ZS_CLEARFIFO(channel); */
320 write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
321 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
322 write_zsreg(channel, R4, regs[R4]);
323 write_zsreg(channel, R9, regs[R9]);
324 write_zsreg(channel, R1, regs[R1]);
325 write_zsreg(channel, R2, regs[R2]);
326 write_zsreg(channel, R10, regs[R10]);
327 write_zsreg(channel, R11, regs[R11]);
328 write_zsreg(channel, R12, regs[R12]);
329 write_zsreg(channel, R13, regs[R13]);
330 write_zsreg(channel, R14, regs[R14]);
331 write_zsreg(channel, R15, regs[R15]);
332 write_zsreg(channel, R3, regs[R3]);
333 write_zsreg(channel, R5, regs[R5]);
337 /* Sets or clears DTR/RTS on the requested line */
338 static inline void zs_rtsdtr(struct dec_serial *info, int which, int set)
343 save_flags(flags); cli();
344 if (info->zs_channel != info->zs_chan_a) {
346 info->zs_chan_a->curregs[5] |= (which & (RTS | DTR));
348 info->zs_chan_a->curregs[5] &= ~(which & (RTS | DTR));
350 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
352 restore_flags(flags);
355 /* Utility routines for the Zilog */
356 static inline int get_zsbaud(struct dec_serial *ss)
358 struct dec_zschannel *channel = ss->zs_channel;
361 /* The baud rate is split up between two 8-bit registers in
362 * what is termed 'BRG time constant' format in my docs for
363 * the chip, it is a function of the clk rate the chip is
364 * receiving which happens to be constant.
366 brg = (read_zsreg(channel, 13) << 8);
367 brg |= read_zsreg(channel, 12);
368 return BRG_TO_BPS(brg, (zs_parms->clock/(ss->clk_divisor)));
371 /* On receive, this clears errors and the receiver interrupts */
372 static inline void rs_recv_clear(struct dec_zschannel *zsc)
374 write_zsreg(zsc, 0, ERR_RES);
375 write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
379 * ----------------------------------------------------------------------
381 * Here starts the interrupt handling routines. All of the following
382 * subroutines are declared as inline and are folded into
383 * rs_interrupt(). They were separated out for readability's sake.
385 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
386 * -----------------------------------------------------------------------
389 static int tty_break; /* Set whenever BREAK condition is detected. */
392 * This routine is used by the interrupt handler to schedule
393 * processing in the software interrupt portion of the driver.
395 static _INLINE_ void rs_sched_event(struct dec_serial *info,
398 info->event |= 1 << event;
399 queue_task(&info->tqueue, &tq_zs_serial);
403 static _INLINE_ void receive_chars(struct dec_serial *info,
404 struct pt_regs *regs)
406 struct tty_struct *tty = info->tty;
407 unsigned char ch, stat, flag;
409 while ((read_zsreg(info->zs_channel, R0) & Rx_CH_AV) != 0) {
411 stat = read_zsreg(info->zs_channel, R1);
412 ch = read_zsdata(info->zs_channel);
414 if (!tty && (!info->hook || !info->hook->rx_char))
419 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
420 if (info->line == sercons.index) {
421 if (!break_pressed) {
422 break_pressed = jiffies;
429 if (info->flags & ZILOG_SAK)
434 } else if (stat & FRM_ERR) {
436 } else if (stat & PAR_ERR) {
441 /* reset the error indication */
442 write_zsreg(info->zs_channel, R0, ERR_RES);
445 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
446 if (break_pressed && info->line == sercons.index) {
448 time_before(jiffies, break_pressed + HZ*5)) {
449 handle_sysrq(ch, regs, NULL, NULL);
457 if (info->hook && info->hook->rx_char) {
458 (*info->hook->rx_char)(ch, flag);
462 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
463 static int flip_buf_ovf;
469 static int flip_max_cnt;
470 if (flip_max_cnt < tty->flip.count)
471 flip_max_cnt = tty->flip.count;
474 *tty->flip.flag_buf_ptr++ = flag;
475 *tty->flip.char_buf_ptr++ = ch;
476 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
481 tty_flip_buffer_push(tty);
484 static void transmit_chars(struct dec_serial *info)
486 if ((read_zsreg(info->zs_channel, R0) & Tx_BUF_EMP) == 0)
492 write_zsdata(info->zs_channel, info->x_char);
498 if ((info->xmit_cnt <= 0) || (info->tty && info->tty->stopped)
499 || info->tx_stopped) {
500 write_zsreg(info->zs_channel, R0, RES_Tx_P);
504 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
505 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
509 if (info->xmit_cnt < WAKEUP_CHARS)
510 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
513 static _INLINE_ void status_handle(struct dec_serial *info)
517 /* Get status from Read Register 0 */
518 stat = read_zsreg(info->zs_channel, R0);
520 if (stat & BRK_ABRT) {
521 #ifdef SERIAL_DEBUG_INTR
522 printk("handling break....");
527 if (info->zs_channel != info->zs_chan_a) {
529 /* Check for DCD transitions */
530 if (info->tty && !C_CLOCAL(info->tty) &&
531 ((stat ^ info->read_reg_zero) & DCD) != 0 ) {
533 wake_up_interruptible(&info->open_wait);
534 } else if (!(info->flags & ZILOG_CALLOUT_ACTIVE)) {
535 tty_hangup(info->tty);
539 /* Check for CTS transitions */
540 if (info->tty && C_CRTSCTS(info->tty)) {
541 if ((stat & CTS) != 0) {
542 if (info->tx_stopped) {
543 info->tx_stopped = 0;
544 if (!info->tx_active)
545 transmit_chars(info);
548 info->tx_stopped = 1;
554 /* Clear status condition... */
555 write_zsreg(info->zs_channel, R0, RES_EXT_INT);
556 info->read_reg_zero = stat;
560 * This is the serial driver's generic interrupt routine
562 void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
564 struct dec_serial *info = (struct dec_serial *) dev_id;
565 unsigned char zs_intreg;
568 /* NOTE: The read register 3, which holds the irq status,
569 * does so for both channels on each chip. Although
570 * the status value itself must be read from the A
571 * channel and is only valid when read from channel A.
572 * Yes... broken hardware...
574 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
576 if (info->zs_chan_a == info->zs_channel)
577 shift = 3; /* Channel A */
579 shift = 0; /* Channel B */
582 zs_intreg = read_zsreg(info->zs_chan_a, R3) >> shift;
583 if ((zs_intreg & CHAN_IRQMASK) == 0)
586 if (zs_intreg & CHBRxIP) {
587 receive_chars(info, regs);
589 if (zs_intreg & CHBTxIP) {
590 transmit_chars(info);
592 if (zs_intreg & CHBEXT) {
597 /* Why do we need this ? */
598 write_zsreg(info->zs_channel, 0, RES_H_IUS);
602 void zs_dump (void) {
604 for (i = 0; i < zs_channels_found; i++) {
605 struct dec_zschannel *ch = &zs_channels[i];
606 if ((long)ch->control == UNI_IO_BASE+UNI_SCC1A_CTRL) {
607 for (j = 0; j < 15; j++) {
608 printk("W%d = 0x%x\t",
609 j, (int)ch->curregs[j]);
611 for (j = 0; j < 15; j++) {
612 printk("R%d = 0x%x\t",
613 j, (int)read_zsreg(ch,j));
622 * -------------------------------------------------------------------
623 * Here ends the serial interrupt routines.
624 * -------------------------------------------------------------------
628 * ------------------------------------------------------------
629 * rs_stop() and rs_start()
631 * This routines are called before setting or resetting tty->stopped.
632 * ------------------------------------------------------------
634 static void rs_stop(struct tty_struct *tty)
636 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
639 if (serial_paranoia_check(info, tty->device, "rs_stop"))
643 save_flags(flags); cli();
644 if (info->zs_channel->curregs[5] & TxENAB) {
645 info->zs_channel->curregs[5] &= ~TxENAB;
646 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
648 restore_flags(flags);
652 static void rs_start(struct tty_struct *tty)
654 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
657 if (serial_paranoia_check(info, tty->device, "rs_start"))
660 save_flags(flags); cli();
662 if (info->xmit_cnt && info->xmit_buf && !(info->zs_channel->curregs[5] & TxENAB)) {
663 info->zs_channel->curregs[5] |= TxENAB;
664 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
667 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
668 transmit_chars(info);
671 restore_flags(flags);
675 * This routine is used to handle the "bottom half" processing for the
676 * serial driver, known also the "software interrupt" processing.
677 * This processing is done at the kernel interrupt level, after the
678 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
679 * is where time-consuming activities which can not be done in the
680 * interrupt driver proper are done; the interrupt driver schedules
681 * them using rs_sched_event(), and they get done here.
683 static void do_serial_bh(void)
685 run_task_queue(&tq_zs_serial);
688 static void do_softint(void *private_)
690 struct dec_serial *info = (struct dec_serial *) private_;
691 struct tty_struct *tty;
697 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
702 int zs_startup(struct dec_serial * info)
706 if (info->flags & ZILOG_INITIALIZED)
709 if (!info->xmit_buf) {
710 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
715 save_flags(flags); cli();
717 #ifdef SERIAL_DEBUG_OPEN
718 printk("starting up ttyS%02d (irq %d)...", info->line, info->irq);
722 * Clear the receive FIFO.
724 ZS_CLEARFIFO(info->zs_channel);
725 info->xmit_fifo_size = 1;
728 * Clear the interrupt registers.
730 write_zsreg(info->zs_channel, R0, ERR_RES);
731 write_zsreg(info->zs_channel, R0, RES_H_IUS);
734 * Set the speed of the serial port
739 * Turn on RTS and DTR.
741 zs_rtsdtr(info, RTS | DTR, 1);
744 * Finally, enable sequencing and interrupts
746 info->zs_channel->curregs[R1] &= ~RxINT_MASK;
747 info->zs_channel->curregs[R1] |= (RxINT_ALL | TxINT_ENAB |
749 info->zs_channel->curregs[R3] |= RxENABLE;
750 info->zs_channel->curregs[R5] |= TxENAB;
751 info->zs_channel->curregs[R15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
752 write_zsreg(info->zs_channel, R1, info->zs_channel->curregs[R1]);
753 write_zsreg(info->zs_channel, R3, info->zs_channel->curregs[R3]);
754 write_zsreg(info->zs_channel, R5, info->zs_channel->curregs[R5]);
755 write_zsreg(info->zs_channel, R15, info->zs_channel->curregs[R15]);
758 * And clear the interrupt registers again for luck.
760 write_zsreg(info->zs_channel, R0, ERR_RES);
761 write_zsreg(info->zs_channel, R0, RES_H_IUS);
763 /* Save the current value of RR0 */
764 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
767 clear_bit(TTY_IO_ERROR, &info->tty->flags);
768 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
770 info->flags |= ZILOG_INITIALIZED;
771 restore_flags(flags);
776 * This routine will shutdown a serial port; interrupts are disabled, and
777 * DTR is dropped if the hangup on close termio flag is on.
779 static void shutdown(struct dec_serial * info)
783 if (!(info->flags & ZILOG_INITIALIZED))
786 #ifdef SERIAL_DEBUG_OPEN
787 printk("Shutting down serial port %d (irq %d)....", info->line,
791 save_flags(flags); cli(); /* Disable interrupts */
793 if (info->xmit_buf) {
794 free_page((unsigned long) info->xmit_buf);
798 info->zs_channel->curregs[1] = 0;
799 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]); /* no interrupts */
801 info->zs_channel->curregs[3] &= ~RxENABLE;
802 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
804 info->zs_channel->curregs[5] &= ~TxENAB;
805 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
806 if (!info->tty || C_HUPCL(info->tty)) {
807 zs_rtsdtr(info, RTS | DTR, 0);
811 set_bit(TTY_IO_ERROR, &info->tty->flags);
813 info->flags &= ~ZILOG_INITIALIZED;
814 restore_flags(flags);
818 * This routine is called to set the UART divisor registers to match
819 * the specified baud rate for a serial port.
821 static void change_speed(struct dec_serial *info)
829 if (!info->tty || !info->tty->termios)
831 cflag = info->tty->termios->c_cflag;
835 cflag = info->hook->cflags;
841 if (i < 1 || i > 2) {
843 info->tty->termios->c_cflag &= ~CBAUDEX;
845 info->hook->cflags &= ~CBAUDEX;
850 save_flags(flags); cli();
851 info->zs_baud = baud_table[i];
853 brg = BPS_TO_BRG(info->zs_baud, zs_parms->clock/info->clk_divisor);
854 info->zs_channel->curregs[12] = (brg & 255);
855 info->zs_channel->curregs[13] = ((brg >> 8) & 255);
856 zs_rtsdtr(info, DTR, 1);
858 zs_rtsdtr(info, RTS | DTR, 0);
862 /* byte size and parity */
863 info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
864 info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
865 switch (cflag & CSIZE) {
868 info->zs_channel->curregs[3] |= Rx5;
869 info->zs_channel->curregs[5] |= Tx5;
873 info->zs_channel->curregs[3] |= Rx6;
874 info->zs_channel->curregs[5] |= Tx6;
878 info->zs_channel->curregs[3] |= Rx7;
879 info->zs_channel->curregs[5] |= Tx7;
882 default: /* defaults to 8 bits */
884 info->zs_channel->curregs[3] |= Rx8;
885 info->zs_channel->curregs[5] |= Tx8;
889 info->timeout = ((info->xmit_fifo_size*HZ*bits) / info->zs_baud);
890 info->timeout += HZ/50; /* Add .02 seconds of slop */
892 info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
893 if (cflag & CSTOPB) {
894 info->zs_channel->curregs[4] |= SB2;
896 info->zs_channel->curregs[4] |= SB1;
898 if (cflag & PARENB) {
899 info->zs_channel->curregs[4] |= PAR_ENA;
901 if (!(cflag & PARODD)) {
902 info->zs_channel->curregs[4] |= PAR_EVEN;
905 if (!(cflag & CLOCAL)) {
906 if (!(info->zs_channel->curregs[15] & DCDIE))
907 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
908 info->zs_channel->curregs[15] |= DCDIE;
910 info->zs_channel->curregs[15] &= ~DCDIE;
911 if (cflag & CRTSCTS) {
912 info->zs_channel->curregs[15] |= CTSIE;
913 if ((read_zsreg(info->zs_channel, 0) & CTS) == 0)
914 info->tx_stopped = 1;
916 info->zs_channel->curregs[15] &= ~CTSIE;
917 info->tx_stopped = 0;
920 /* Load up the new values */
921 load_zsregs(info->zs_channel, info->zs_channel->curregs);
923 restore_flags(flags);
926 static void rs_flush_chars(struct tty_struct *tty)
928 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
931 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
934 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
938 /* Enable transmitter */
939 save_flags(flags); cli();
940 transmit_chars(info);
941 restore_flags(flags);
944 static int rs_write(struct tty_struct * tty, int from_user,
945 const unsigned char *buf, int count)
948 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
951 if (serial_paranoia_check(info, tty->device, "rs_write"))
954 if (!tty || !info->xmit_buf)
960 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
961 SERIAL_XMIT_SIZE - info->xmit_head));
967 copy_from_user(tmp_buf, buf, c);
968 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
969 SERIAL_XMIT_SIZE - info->xmit_head));
970 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
973 memcpy(info->xmit_buf + info->xmit_head, buf, c);
974 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
976 restore_flags(flags);
982 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
984 transmit_chars(info);
985 restore_flags(flags);
989 static int rs_write_room(struct tty_struct *tty)
991 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
994 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
996 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1002 static int rs_chars_in_buffer(struct tty_struct *tty)
1004 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1006 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1008 return info->xmit_cnt;
1011 static void rs_flush_buffer(struct tty_struct *tty)
1013 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1015 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1018 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1024 * ------------------------------------------------------------
1027 * This routine is called by the upper-layer tty layer to signal that
1028 * incoming characters should be throttled.
1029 * ------------------------------------------------------------
1031 static void rs_throttle(struct tty_struct * tty)
1033 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1034 unsigned long flags;
1036 #ifdef SERIAL_DEBUG_THROTTLE
1039 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1040 tty->ldisc.chars_in_buffer(tty));
1043 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1047 save_flags(flags); cli();
1048 info->x_char = STOP_CHAR(tty);
1049 if (!info->tx_active)
1050 transmit_chars(info);
1051 restore_flags(flags);
1054 if (C_CRTSCTS(tty)) {
1055 zs_rtsdtr(info, RTS, 0);
1059 static void rs_unthrottle(struct tty_struct * tty)
1061 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1062 unsigned long flags;
1064 #ifdef SERIAL_DEBUG_THROTTLE
1067 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1068 tty->ldisc.chars_in_buffer(tty));
1071 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1075 save_flags(flags); cli();
1079 info->x_char = START_CHAR(tty);
1080 if (!info->tx_active)
1081 transmit_chars(info);
1083 restore_flags(flags);
1086 if (C_CRTSCTS(tty)) {
1087 zs_rtsdtr(info, RTS, 1);
1092 * ------------------------------------------------------------
1093 * rs_ioctl() and friends
1094 * ------------------------------------------------------------
1097 static int get_serial_info(struct dec_serial * info,
1098 struct serial_struct * retinfo)
1100 struct serial_struct tmp;
1104 memset(&tmp, 0, sizeof(tmp));
1105 tmp.type = info->type;
1106 tmp.line = info->line;
1107 tmp.port = info->port;
1108 tmp.irq = info->irq;
1109 tmp.flags = info->flags;
1110 tmp.baud_base = info->baud_base;
1111 tmp.close_delay = info->close_delay;
1112 tmp.closing_wait = info->closing_wait;
1113 tmp.custom_divisor = info->custom_divisor;
1114 return copy_to_user(retinfo,&tmp,sizeof(*retinfo));
1117 static int set_serial_info(struct dec_serial * info,
1118 struct serial_struct * new_info)
1120 struct serial_struct new_serial;
1121 struct dec_serial old_info;
1126 copy_from_user(&new_serial,new_info,sizeof(new_serial));
1129 if (!capable(CAP_SYS_ADMIN)) {
1130 if ((new_serial.baud_base != info->baud_base) ||
1131 (new_serial.type != info->type) ||
1132 (new_serial.close_delay != info->close_delay) ||
1133 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1134 (info->flags & ~ZILOG_USR_MASK)))
1136 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1137 (new_serial.flags & ZILOG_USR_MASK));
1138 info->custom_divisor = new_serial.custom_divisor;
1139 goto check_and_exit;
1142 if (info->count > 1)
1146 * OK, past this point, all the error checking has been done.
1147 * At this point, we start making changes.....
1150 info->baud_base = new_serial.baud_base;
1151 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1152 (new_serial.flags & ZILOG_FLAGS));
1153 info->type = new_serial.type;
1154 info->close_delay = new_serial.close_delay;
1155 info->closing_wait = new_serial.closing_wait;
1158 retval = zs_startup(info);
1163 * get_lsr_info - get line status register info
1165 * Purpose: Let user call ioctl() to get info when the UART physically
1166 * is emptied. On bus types like RS485, the transmitter must
1167 * release the bus after transmitting. This must be done when
1168 * the transmit shift register is empty, not be done when the
1169 * transmit holding register is empty. This functionality
1170 * allows an RS485 driver to be written in user space.
1172 static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1174 unsigned char status;
1177 status = read_zsreg(info->zs_channel, 0);
1179 put_user(status,value);
1183 static int get_modem_info(struct dec_serial *info, unsigned int *value)
1185 unsigned char control, status_a, status_b;
1186 unsigned int result;
1188 if (info->zs_channel == info->zs_chan_a)
1192 control = info->zs_chan_a->curregs[5];
1193 status_a = read_zsreg(info->zs_chan_a, 0);
1194 status_b = read_zsreg(info->zs_channel, 0);
1196 result = ((control & RTS) ? TIOCM_RTS: 0)
1197 | ((control & DTR) ? TIOCM_DTR: 0)
1198 | ((status_b & DCD) ? TIOCM_CAR: 0)
1199 | ((status_a & DCD) ? TIOCM_RNG: 0)
1200 | ((status_a & SYNC_HUNT) ? TIOCM_DSR: 0)
1201 | ((status_b & CTS) ? TIOCM_CTS: 0);
1203 put_user(result, value);
1207 static int set_modem_info(struct dec_serial *info, unsigned int cmd,
1208 unsigned int *value)
1211 unsigned int arg, bits;
1213 error = verify_area(VERIFY_READ, value, sizeof(int));
1217 if (info->zs_channel == info->zs_chan_a)
1220 get_user(arg, value);
1221 bits = (arg & TIOCM_RTS? RTS: 0) + (arg & TIOCM_DTR? DTR: 0);
1225 info->zs_chan_a->curregs[5] |= bits;
1228 info->zs_chan_a->curregs[5] &= ~bits;
1231 info->zs_chan_a->curregs[5] =
1232 (info->zs_chan_a->curregs[5] & ~(DTR | RTS)) | bits;
1238 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1244 * rs_break - turn transmit break condition on/off
1246 static void rs_break(struct tty_struct *tty, int break_state)
1248 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1249 unsigned long flags;
1251 if (serial_paranoia_check(info, tty->device, "rs_break"))
1256 save_flags(flags); cli();
1257 if (break_state == -1)
1258 info->zs_channel->curregs[5] |= SND_BRK;
1260 info->zs_channel->curregs[5] &= ~SND_BRK;
1261 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1262 restore_flags(flags);
1265 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1266 unsigned int cmd, unsigned long arg)
1269 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1274 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1277 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1278 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1279 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1280 if (tty->flags & (1 << TTY_IO_ERROR))
1286 error = verify_area(VERIFY_WRITE, (void *) arg,
1287 sizeof(unsigned int));
1290 return get_modem_info(info, (unsigned int *) arg);
1294 return set_modem_info(info, cmd, (unsigned int *) arg);
1296 error = verify_area(VERIFY_WRITE, (void *) arg,
1297 sizeof(struct serial_struct));
1300 return get_serial_info(info,
1301 (struct serial_struct *) arg);
1303 return set_serial_info(info,
1304 (struct serial_struct *) arg);
1305 case TIOCSERGETLSR: /* Get line status register */
1306 error = verify_area(VERIFY_WRITE, (void *) arg,
1307 sizeof(unsigned int));
1311 return get_lsr_info(info, (unsigned int *) arg);
1313 case TIOCSERGSTRUCT:
1314 error = verify_area(VERIFY_WRITE, (void *) arg,
1315 sizeof(struct dec_serial));
1318 copy_from_user((struct dec_serial *) arg,
1319 info, sizeof(struct dec_serial));
1323 return -ENOIOCTLCMD;
1328 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1330 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1333 if (tty->termios->c_cflag == old_termios->c_cflag)
1335 was_stopped = info->tx_stopped;
1339 if (was_stopped && !info->tx_stopped)
1344 * ------------------------------------------------------------
1347 * This routine is called when the serial port gets closed.
1348 * Wait for the last remaining data to be sent.
1349 * ------------------------------------------------------------
1351 static void rs_close(struct tty_struct *tty, struct file * filp)
1353 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1354 unsigned long flags;
1356 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1359 save_flags(flags); cli();
1361 if (tty_hung_up_p(filp)) {
1362 restore_flags(flags);
1366 #ifdef SERIAL_DEBUG_OPEN
1367 printk("rs_close ttyS%02d, count = %d\n", info->line, info->count);
1369 if ((tty->count == 1) && (info->count != 1)) {
1371 * Uh, oh. tty->count is 1, which means that the tty
1372 * structure will be freed. Info->count should always
1373 * be one in these conditions. If it's greater than
1374 * one, we've got real problems, since it means the
1375 * serial port won't be shutdown.
1377 printk("rs_close: bad serial port count; tty->count is 1, "
1378 "info->count is %d\n", info->count);
1381 if (--info->count < 0) {
1382 printk("rs_close: bad serial port count for ttyS%02d: %d\n",
1383 info->line, info->count);
1387 restore_flags(flags);
1390 info->flags |= ZILOG_CLOSING;
1392 * Save the termios structure, since this port may have
1393 * separate termios for callout and dialin.
1395 if (info->flags & ZILOG_NORMAL_ACTIVE)
1396 info->normal_termios = *tty->termios;
1397 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1398 info->callout_termios = *tty->termios;
1400 * Now we wait for the transmit buffer to clear; and we notify
1401 * the line discipline to only process XON/XOFF characters.
1404 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1405 tty_wait_until_sent(tty, info->closing_wait);
1407 * At this point we stop accepting input. To do this, we
1408 * disable the receiver and receive interrupts.
1410 info->zs_channel->curregs[3] &= ~RxENABLE;
1411 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1412 info->zs_channel->curregs[1] = 0; /* disable any rx ints */
1413 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1414 ZS_CLEARFIFO(info->zs_channel);
1415 if (info->flags & ZILOG_INITIALIZED) {
1417 * Before we drop DTR, make sure the SCC transmitter
1418 * has completely drained.
1420 rs_wait_until_sent(tty, info->timeout);
1424 if (tty->driver.flush_buffer)
1425 tty->driver.flush_buffer(tty);
1426 tty_ldisc_flush(tty);
1430 if (info->blocked_open) {
1431 if (info->close_delay) {
1432 current->state = TASK_INTERRUPTIBLE;
1433 schedule_timeout(info->close_delay);
1435 wake_up_interruptible(&info->open_wait);
1437 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1439 wake_up_interruptible(&info->close_wait);
1440 restore_flags(flags);
1444 * rs_wait_until_sent() --- wait until the transmitter is empty
1446 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1448 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1449 unsigned long orig_jiffies, char_time;
1451 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1454 orig_jiffies = jiffies;
1456 * Set the check interval to be 1/5 of the estimated time to
1457 * send a single character, and make it at least 1. The check
1458 * interval should also be less than the timeout.
1460 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1461 char_time = char_time / 5;
1465 char_time = MIN(char_time, timeout);
1466 while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
1467 current->state = TASK_INTERRUPTIBLE;
1468 schedule_timeout(char_time);
1469 if (signal_pending(current))
1471 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1474 current->state = TASK_RUNNING;
1478 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1480 void rs_hangup(struct tty_struct *tty)
1482 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1484 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1487 rs_flush_buffer(tty);
1491 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1493 wake_up_interruptible(&info->open_wait);
1497 * ------------------------------------------------------------
1498 * rs_open() and friends
1499 * ------------------------------------------------------------
1501 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1502 struct dec_serial *info)
1504 DECLARE_WAITQUEUE(wait, current);
1509 * If the device is in the middle of being closed, then block
1510 * until it's done, and then try again.
1512 if (info->flags & ZILOG_CLOSING) {
1513 interruptible_sleep_on(&info->close_wait);
1514 #ifdef SERIAL_DO_RESTART
1515 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1516 -EAGAIN : -ERESTARTSYS);
1523 * If this is a callout device, then just make sure the normal
1524 * device isn't being used.
1526 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1527 if (info->flags & ZILOG_NORMAL_ACTIVE)
1529 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1530 (info->flags & ZILOG_SESSION_LOCKOUT) &&
1531 (info->session != current->session))
1533 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1534 (info->flags & ZILOG_PGRP_LOCKOUT) &&
1535 (info->pgrp != current->pgrp))
1537 info->flags |= ZILOG_CALLOUT_ACTIVE;
1542 * If non-blocking mode is set, or the port is not enabled,
1543 * then make the check up front and then exit.
1545 if ((filp->f_flags & O_NONBLOCK) ||
1546 (tty->flags & (1 << TTY_IO_ERROR))) {
1547 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1549 info->flags |= ZILOG_NORMAL_ACTIVE;
1553 if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1554 if (info->normal_termios.c_cflag & CLOCAL)
1557 if (tty->termios->c_cflag & CLOCAL)
1562 * Block waiting for the carrier detect and the line to become
1563 * free (i.e., not in use by the callout). While we are in
1564 * this loop, info->count is dropped by one, so that
1565 * rs_close() knows when to free things. We restore it upon
1566 * exit, either normal or abnormal.
1569 add_wait_queue(&info->open_wait, &wait);
1570 #ifdef SERIAL_DEBUG_OPEN
1571 printk("block_til_ready before block: ttyS%02d, count = %d\n",
1572 info->line, info->count);
1575 if (!tty_hung_up_p(filp))
1578 info->blocked_open++;
1581 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1582 (tty->termios->c_cflag & CBAUD))
1583 zs_rtsdtr(info, RTS | DTR, 1);
1585 set_current_state(TASK_INTERRUPTIBLE);
1586 if (tty_hung_up_p(filp) ||
1587 !(info->flags & ZILOG_INITIALIZED)) {
1588 #ifdef SERIAL_DO_RESTART
1589 if (info->flags & ZILOG_HUP_NOTIFY)
1592 retval = -ERESTARTSYS;
1598 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1599 !(info->flags & ZILOG_CLOSING) &&
1600 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1602 if (signal_pending(current)) {
1603 retval = -ERESTARTSYS;
1606 #ifdef SERIAL_DEBUG_OPEN
1607 printk("block_til_ready blocking: ttyS%02d, count = %d\n",
1608 info->line, info->count);
1612 current->state = TASK_RUNNING;
1613 remove_wait_queue(&info->open_wait, &wait);
1614 if (!tty_hung_up_p(filp))
1616 info->blocked_open--;
1617 #ifdef SERIAL_DEBUG_OPEN
1618 printk("block_til_ready after blocking: ttyS%02d, count = %d\n",
1619 info->line, info->count);
1623 info->flags |= ZILOG_NORMAL_ACTIVE;
1628 * This routine is called whenever a serial port is opened. It
1629 * enables interrupts for a serial port, linking in its ZILOG structure into
1630 * the IRQ chain. It also performs the serial-specific
1631 * initialization for the tty structure.
1633 int rs_open(struct tty_struct *tty, struct file * filp)
1635 struct dec_serial *info;
1638 line = MINOR(tty->device) - tty->driver.minor_start;
1639 if ((line < 0) || (line >= zs_channels_found))
1641 info = zs_soft + line;
1646 if (serial_paranoia_check(info, tty->device, "rs_open"))
1648 #ifdef SERIAL_DEBUG_OPEN
1649 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1654 tty->driver_data = info;
1658 * If the port is the middle of closing, bail out now
1660 if (tty_hung_up_p(filp) ||
1661 (info->flags & ZILOG_CLOSING)) {
1662 if (info->flags & ZILOG_CLOSING)
1663 interruptible_sleep_on(&info->close_wait);
1664 #ifdef SERIAL_DO_RESTART
1665 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1666 -EAGAIN : -ERESTARTSYS);
1673 * Start up serial port
1675 retval = zs_startup(info);
1679 retval = block_til_ready(tty, filp, info);
1681 #ifdef SERIAL_DEBUG_OPEN
1682 printk("rs_open returning after block_til_ready with %d\n",
1688 if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1689 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1690 *tty->termios = info->normal_termios;
1692 *tty->termios = info->callout_termios;
1695 #ifdef CONFIG_SERIAL_DEC_CONSOLE
1696 if (sercons.cflag && sercons.index == line) {
1697 tty->termios->c_cflag = sercons.cflag;
1703 info->session = current->session;
1704 info->pgrp = current->pgrp;
1706 #ifdef SERIAL_DEBUG_OPEN
1707 printk("rs_open ttyS%02d successful...", info->line);
1709 /* tty->low_latency = 1; */
1713 /* Finally, routines used to initialize the serial driver. */
1715 static void __init show_serial_version(void)
1717 printk("DECstation Z8530 serial driver version 0.08\n");
1720 /* Initialize Z8530s zs_channels
1723 static void __init probe_sccs(void)
1725 struct dec_serial **pp;
1726 int i, n, n_chips = 0, n_channels, chip, channel;
1727 unsigned long flags;
1730 * did we get here by accident?
1733 printk("Not on JUNKIO machine, skipping probe_sccs\n");
1738 * When serial console is activated, tc_init has not been called yet
1739 * and system_base is undefined. Unfortunately we have to hardcode
1740 * system_base for this case :-(. HK
1742 switch(mips_machtype) {
1743 #ifdef CONFIG_DECSTATION
1744 case MACH_DS5000_2X0:
1746 system_base = KSEG1ADDR(0x1f800000);
1748 zs_parms = &ds_parms;
1749 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1750 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1752 case MACH_DS5000_1XX:
1753 system_base = KSEG1ADDR(0x1c000000);
1755 zs_parms = &ds_parms;
1756 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1757 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1759 case MACH_DS5000_XX:
1760 system_base = KSEG1ADDR(0x1c000000);
1762 zs_parms = &ds_parms;
1763 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1766 #ifdef CONFIG_BAGET_MIPS
1768 system_base = UNI_IO_BASE;
1770 zs_parms = &baget_parms;
1771 zs_init_regs[2] = 0x8;
1775 panic("zs: unsupported bus");
1778 panic("zs: uninitialized parms");
1784 for (chip = 0; chip < n_chips; chip++) {
1785 for (channel = 0; channel <= 1; channel++) {
1787 * The sccs reside on the high byte of the 16 bit IOBUS
1789 zs_channels[n_channels].control =
1790 (volatile unsigned char *)system_base +
1791 (0 == chip ? zs_parms->scc0 : zs_parms->scc1) +
1792 (0 == channel ? zs_parms->channel_a_offset :
1793 zs_parms->channel_b_offset);
1794 zs_channels[n_channels].data =
1795 zs_channels[n_channels].control + 4;
1797 #ifndef CONFIG_SERIAL_DEC_CONSOLE
1799 * We're called early and memory managment isn't up, yet.
1800 * Thus check_region would fail.
1802 if (check_region((unsigned long)
1803 zs_channels[n_channels].control,
1804 ZS_CHAN_IO_SIZE) < 0) {
1805 panic("SCC I/O region is not free");
1807 request_region((unsigned long)
1808 zs_channels[n_channels].control,
1809 ZS_CHAN_IO_SIZE, "SCC");
1811 zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1814 zs_soft[n_channels].irq = zs_parms->irq0;
1816 zs_soft[n_channels].irq = zs_parms->irq1;
1819 * Identification of channel A. Location of channel A
1820 * inside chip depends on mapping of internal address
1821 * the chip decodes channels by.
1822 * CHANNEL_A_NR returns either 0 (in case of
1823 * DECstations) or 1 (in case of Baget).
1825 if (CHANNEL_A_NR == channel)
1826 zs_soft[n_channels].zs_chan_a =
1827 &zs_channels[n_channels+1-2*CHANNEL_A_NR];
1829 zs_soft[n_channels].zs_chan_a =
1830 &zs_channels[n_channels];
1832 *pp = &zs_soft[n_channels];
1833 pp = &zs_soft[n_channels].zs_next;
1839 zs_channels_found = n_channels;
1841 for (n = 0; n < zs_channels_found; n++) {
1842 for (i = 0; i < 16; i++) {
1843 zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1847 save_and_cli(flags);
1848 for (n = 0; n < zs_channels_found; n++) {
1850 write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
1852 write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
1854 load_zsregs(zs_soft[n].zs_channel,
1855 zs_soft[n].zs_channel->curregs);
1857 restore_flags(flags);
1860 /* zs_init inits the driver */
1861 int __init zs_init(void)
1864 struct dec_serial *info;
1869 /* Setup base handler, and timer table. */
1870 init_bh(SERIAL_BH, do_serial_bh);
1872 /* Find out how many Z8530 SCCs we have */
1876 show_serial_version();
1878 /* Initialize the tty_driver structure */
1879 /* Not all of this is exactly right for us. */
1881 memset(&serial_driver, 0, sizeof(struct tty_driver));
1882 serial_driver.magic = TTY_DRIVER_MAGIC;
1883 #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
1884 serial_driver.name = "tts/%d";
1886 serial_driver.name = "ttyS";
1888 serial_driver.major = TTY_MAJOR;
1889 serial_driver.minor_start = 64;
1890 serial_driver.num = zs_channels_found;
1891 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
1892 serial_driver.subtype = SERIAL_TYPE_NORMAL;
1893 serial_driver.init_termios = tty_std_termios;
1895 serial_driver.init_termios.c_cflag =
1896 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1897 serial_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1898 serial_driver.refcount = &serial_refcount;
1899 serial_driver.table = serial_table;
1900 serial_driver.termios = serial_termios;
1901 serial_driver.termios_locked = serial_termios_locked;
1903 serial_driver.open = rs_open;
1904 serial_driver.close = rs_close;
1905 serial_driver.write = rs_write;
1906 serial_driver.flush_chars = rs_flush_chars;
1907 serial_driver.write_room = rs_write_room;
1908 serial_driver.chars_in_buffer = rs_chars_in_buffer;
1909 serial_driver.flush_buffer = rs_flush_buffer;
1910 serial_driver.ioctl = rs_ioctl;
1911 serial_driver.throttle = rs_throttle;
1912 serial_driver.unthrottle = rs_unthrottle;
1913 serial_driver.set_termios = rs_set_termios;
1914 serial_driver.stop = rs_stop;
1915 serial_driver.start = rs_start;
1916 serial_driver.hangup = rs_hangup;
1917 serial_driver.break_ctl = rs_break;
1918 serial_driver.wait_until_sent = rs_wait_until_sent;
1921 * The callout device is just like normal device except for
1922 * major number and the subtype code.
1924 callout_driver = serial_driver;
1925 #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
1926 callout_driver.name = "cua/%d";
1928 callout_driver.name = "cua";
1930 callout_driver.major = TTYAUX_MAJOR;
1931 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1933 if (tty_register_driver(&serial_driver))
1934 panic("Couldn't register serial driver");
1935 if (tty_register_driver(&callout_driver))
1936 panic("Couldn't register callout driver");
1938 for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
1940 /* Needed before interrupts are enabled. */
1944 if (info->hook && info->hook->init_info) {
1945 (*info->hook->init_info)(info);
1949 info->magic = SERIAL_MAGIC;
1950 info->port = (int) info->zs_channel->control;
1952 info->custom_divisor = 16;
1953 info->close_delay = 50;
1954 info->closing_wait = 3000;
1957 info->blocked_open = 0;
1958 info->tqueue.routine = do_softint;
1959 info->tqueue.data = info;
1960 info->callout_termios = callout_driver.init_termios;
1961 info->normal_termios = serial_driver.init_termios;
1962 init_waitqueue_head(&info->open_wait);
1963 init_waitqueue_head(&info->close_wait);
1964 printk("ttyS%02d at 0x%08x (irq = %d) is a Z85C30 SCC\n",
1965 info->line, info->port, info->irq);
1966 tty_register_devfs(&serial_driver, 0,
1967 serial_driver.minor_start + info->line);
1968 tty_register_devfs(&callout_driver, 0,
1969 callout_driver.minor_start + info->line);
1973 for (channel = 0; channel < zs_channels_found; ++channel) {
1974 zs_soft[channel].clk_divisor = 16;
1975 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1977 if (request_irq(zs_soft[channel].irq, rs_interrupt, SA_SHIRQ,
1978 "scc", &zs_soft[channel]))
1979 printk(KERN_ERR "decserial: can't get irq %d\n",
1980 zs_soft[channel].irq);
1982 if (zs_soft[channel].hook) {
1983 zs_startup(&zs_soft[channel]);
1984 if (zs_soft[channel].hook->init_channel)
1985 (*zs_soft[channel].hook->init_channel)
1986 (&zs_soft[channel]);
1994 * polling I/O routines
1997 zs_poll_tx_char(struct dec_serial *info, unsigned char ch)
1999 struct dec_zschannel *chan = info->zs_channel;
2005 while (loops && !(read_zsreg(chan, 0) & Tx_BUF_EMP))
2009 write_zsdata(chan, ch);
2020 zs_poll_rx_char(struct dec_serial *info)
2022 struct dec_zschannel *chan = info->zs_channel;
2028 while (loops && !(read_zsreg(chan, 0) & Rx_CH_AV))
2032 ret = read_zsdata(chan);
2041 unsigned int register_zs_hook(unsigned int channel, struct zs_hook *hook)
2043 struct dec_serial *info = &zs_soft[channel];
2046 printk(__FUNCTION__": line %d has already a hook registered\n", channel);
2050 hook->poll_rx_char = zs_poll_rx_char;
2051 hook->poll_tx_char = zs_poll_tx_char;
2058 unsigned int unregister_zs_hook(unsigned int channel)
2060 struct dec_serial *info = &zs_soft[channel];
2066 printk(__FUNCTION__": trying to unregister hook on line %d,"
2067 " but none is registered\n", channel);
2073 * ------------------------------------------------------------
2074 * Serial console driver
2075 * ------------------------------------------------------------
2077 #ifdef CONFIG_SERIAL_DEC_CONSOLE
2081 * Print a string to the serial port trying not to disturb
2082 * any possible real use of the port...
2084 static void serial_console_write(struct console *co, const char *s,
2087 struct dec_serial *info;
2090 info = zs_soft + co->index;
2092 for (i = 0; i < count; i++, s++) {
2094 zs_poll_tx_char(info, '\r');
2095 zs_poll_tx_char(info, *s);
2099 static kdev_t serial_console_device(struct console *c)
2101 return MKDEV(TTY_MAJOR, 64 + c->index);
2105 * Setup initial baud/bits/parity. We do two things here:
2106 * - construct a cflag setting for the first rs_open()
2107 * - initialize the serial port
2108 * Return non-zero if we didn't find a serial port.
2110 static int __init serial_console_setup(struct console *co, char *options)
2112 struct dec_serial *info;
2116 int cflag = CREAD | HUPCL | CLOCAL;
2117 int clk_divisor = 16;
2120 unsigned long flags;
2125 info = zs_soft + co->index;
2133 baud = simple_strtoul(options, NULL, 10);
2135 while(*s >= '0' && *s <= '9')
2144 * Now construct a cflag setting.
2172 * Set this to a sane value to prevent a divide error.
2196 save_and_cli(flags);
2199 * Set up the baud rate generator.
2201 brg = BPS_TO_BRG(baud, zs_parms->clock / clk_divisor);
2202 info->zs_channel->curregs[R12] = (brg & 255);
2203 info->zs_channel->curregs[R13] = ((brg >> 8) & 255);
2206 * Set byte size and parity.
2209 info->zs_channel->curregs[R3] |= Rx7;
2210 info->zs_channel->curregs[R5] |= Tx7;
2212 info->zs_channel->curregs[R3] |= Rx8;
2213 info->zs_channel->curregs[R5] |= Tx8;
2215 if (cflag & PARENB) {
2216 info->zs_channel->curregs[R4] |= PAR_ENA;
2218 if (!(cflag & PARODD)) {
2219 info->zs_channel->curregs[R4] |= PAR_EVEN;
2221 info->zs_channel->curregs[R4] |= SB1;
2224 * Turn on RTS and DTR.
2226 zs_rtsdtr(info, RTS | DTR, 1);
2229 * Finally, enable sequencing.
2231 info->zs_channel->curregs[R3] |= RxENABLE;
2232 info->zs_channel->curregs[R5] |= TxENAB;
2235 * Clear the interrupt registers.
2237 write_zsreg(info->zs_channel, R0, ERR_RES);
2238 write_zsreg(info->zs_channel, R0, RES_H_IUS);
2241 * Load up the new values.
2243 load_zsregs(info->zs_channel, info->zs_channel->curregs);
2245 /* Save the current value of RR0 */
2246 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
2248 zs_soft[co->index].clk_divisor = clk_divisor;
2249 zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
2251 restore_flags(flags);
2256 static struct console sercons = {
2258 .write = serial_console_write,
2259 .device = serial_console_device,
2260 .setup = serial_console_setup,
2261 .flags = CON_PRINTBUFFER,
2268 void __init zs_serial_console_init(void)
2270 register_console(&sercons);
2272 #endif /* ifdef CONFIG_SERIAL_DEC_CONSOLE */
2275 struct dec_zschannel *zs_kgdbchan;
2276 static unsigned char scc_inittab[] = {
2277 9, 0x80, /* reset A side (CHRA) */
2278 13, 0, /* set baud rate divisor */
2280 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
2281 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
2282 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
2283 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
2284 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
2287 /* These are for receiving and sending characters under the kgdb
2288 * source level kernel debugger.
2290 void putDebugChar(char kgdb_char)
2292 struct dec_zschannel *chan = zs_kgdbchan;
2293 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2295 write_zsdata(chan, kgdb_char);
2297 char getDebugChar(void)
2299 struct dec_zschannel *chan = zs_kgdbchan;
2300 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2301 eieio(); /*barrier();*/
2302 return read_zsdata(chan);
2304 void kgdb_interruptible(int yes)
2306 struct dec_zschannel *chan = zs_kgdbchan;
2308 nine = read_zsreg(chan, 9);
2310 one = EXT_INT_ENAB|RxINT_ALL;
2312 printk("turning serial ints on\n");
2316 printk("turning serial ints off\n");
2318 write_zsreg(chan, 1, one);
2319 write_zsreg(chan, 9, nine);
2322 static int kgdbhook_init_channel(struct dec_serial* info)
2327 static void kgdbhook_init_info(struct dec_serial* info)
2331 static void kgdbhook_rx_char(struct dec_serial* info,
2332 unsigned char ch, unsigned char stat)
2334 if (ch == 0x03 || ch == '$')
2336 if (stat & (Rx_OVR|FRM_ERR|PAR_ERR))
2337 write_zsreg(info->zs_channel, 0, ERR_RES);
2340 /* This sets up the serial port we're using, and turns on
2341 * interrupts for that channel, so kgdb is usable once we're done.
2343 static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2347 volatile char *sccc = ms->control;
2348 brg = BPS_TO_BRG(bps, zs_parms->clock/16);
2349 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2350 for (i = 20000; i != 0; --i) {
2353 for (i = 0; i < sizeof(scc_inittab); ++i) {
2354 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2358 /* This is called at boot time to prime the kgdb serial debugging
2359 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2360 * for /dev/ttyb which is determined in setup_arch() from the
2361 * boot command line flags.
2363 struct zs_hook zs_kgdbhook = {
2364 init_channel : kgdbhook_init_channel,
2365 init_info : kgdbhook_init_info,
2366 cflags : B38400|CS8|CLOCAL,
2367 rx_char : kgdbhook_rx_char,
2370 void __init zs_kgdb_hook(int tty_num)
2372 /* Find out how many Z8530 SCCs we have */
2375 zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2376 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2377 zs_soft[tty_num].change_needed = 0;
2378 zs_soft[tty_num].clk_divisor = 16;
2379 zs_soft[tty_num].zs_baud = 38400;
2380 zs_soft[tty_num].hook = &zs_kgdbhook; /* This runs kgdb */
2381 /* Turn on transmitter/receiver at 8-bits/char */
2382 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2383 printk("KGDB: on channel %d initialized\n", tty_num);
2384 set_debug_traps(); /* init stub */
2386 #endif /* ifdef CONFIG_KGDB */