Linux 2.6.20.7
[linux/fpc-iii.git] / drivers / tc / zs.c
blobfc3197273663e93c48f975cded7c748727ad0370
1 /*
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.
7 * DECstation changes
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:
18 * mouse/keyboard:
19 * DIN-7 MJ-4 signal SCC
20 * 2 1 TxD <- A.TxD
21 * 3 4 RxD -> A.RxD
23 * EIA-232/EIA-423:
24 * DB-25 MMJ-6 signal SCC
25 * 2 2 TxD <- B.TxD
26 * 3 5 RxD -> B.RxD
27 * 4 RTS <- ~A.RTS
28 * 5 CTS -> ~B.CTS
29 * 6 6 DSR -> ~A.SYNC
30 * 8 CD -> ~B.DCD
31 * 12 DSRS(DCE) -> ~A.CTS (*)
32 * 15 TxC -> B.TxC
33 * 17 RxC -> B.RxC
34 * 20 1 DTR <- ~A.DTR
35 * 22 RI -> ~A.DCD
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>
52 #include <linux/mm.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>
60 #endif
62 #include <asm/io.h>
63 #include <asm/pgtable.h>
64 #include <asm/irq.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>
75 #ifdef CONFIG_KGDB
76 #include <asm/kgdb.h>
77 #endif
78 #ifdef CONFIG_MAGIC_SYSRQ
79 #include <linux/sysrq.h>
80 #endif
82 #include "zs.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)
98 struct zs_parms {
99 unsigned long scc0;
100 unsigned long scc1;
101 int channel_a_offset;
102 int channel_b_offset;
103 int irq0;
104 int irq1;
105 int clock;
108 static struct zs_parms *zs_parms;
110 #ifdef CONFIG_MACH_DECSTATION
111 static struct zs_parms ds_parms = {
112 scc0 : IOASIC_SCC0,
113 scc1 : IOASIC_SCC1,
114 channel_a_offset : 1,
115 channel_b_offset : 9,
116 irq0 : -1,
117 irq1 : -1,
118 clock : ZS_CLOCK
120 #endif
122 #ifdef CONFIG_MACH_DECSTATION
123 #define DS_BUS_PRESENT (IOASIC)
124 #else
125 #define DS_BUS_PRESENT 0
126 #endif
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;
141 #endif
142 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
143 !defined(MODULE)
144 static unsigned long break_pressed; /* break, really ... */
145 #endif
147 static unsigned char zs_init_regs[16] __initdata = {
148 0, /* write 0 */
149 0, /* write 1 */
150 0, /* write 2 */
151 0, /* write 3 */
152 (X16CLK), /* write 4 */
153 0, /* write 5 */
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 */
160 0 /* write 15 */
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
172 * Debugging.
174 #undef SERIAL_DEBUG_OPEN
175 #undef SERIAL_DEBUG_FLOW
176 #undef SERIAL_DEBUG_THROTTLE
177 #undef SERIAL_PARANOIA_CHECK
179 #undef ZS_DEBUG_REGS
181 #ifdef SERIAL_DEBUG_THROTTLE
182 #define _tty_name(tty,buf) tty_name(tty,buf)
183 #endif
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";
201 if (!info) {
202 printk(badinfo, name, routine);
203 return 1;
205 if (info->magic != SERIAL_MAGIC) {
206 printk(badmagic, name, routine);
207 return 1;
209 #endif
210 return 0;
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,
224 unsigned char reg)
226 unsigned char retval;
228 if (reg != 0) {
229 *channel->control = reg & 0xf;
230 fast_iob(); RECOVERY_DELAY;
232 retval = *channel->control;
233 RECOVERY_DELAY;
234 return retval;
237 static inline void write_zsreg(struct dec_zschannel *channel,
238 unsigned char reg, unsigned char value)
240 if (reg != 0) {
241 *channel->control = reg & 0xf;
242 fast_iob(); RECOVERY_DELAY;
244 *channel->control = value;
245 fast_iob(); RECOVERY_DELAY;
246 return;
249 static inline unsigned char read_zsdata(struct dec_zschannel *channel)
251 unsigned char retval;
253 retval = *channel->data;
254 RECOVERY_DELAY;
255 return retval;
258 static inline void write_zsdata(struct dec_zschannel *channel,
259 unsigned char value)
261 *channel->data = value;
262 fast_iob(); RECOVERY_DELAY;
263 return;
266 static inline void load_zsregs(struct dec_zschannel *channel,
267 unsigned char *regs)
269 /* ZS_CLEARERR(channel);
270 ZS_CLEARFIFO(channel); */
271 /* Load 'em up */
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]);
286 return;
289 /* Sets or clears DTR/RTS on the requested line */
290 static inline void zs_rtsdtr(struct dec_serial *info, int which, int set)
292 unsigned long flags;
294 spin_lock_irqsave(&zs_lock, flags);
295 if (info->zs_channel != info->zs_chan_a) {
296 if (set) {
297 info->zs_chan_a->curregs[5] |= (which & (RTS | DTR));
298 } else {
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;
310 int brg;
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))
361 continue;
363 flag = TTY_NORMAL;
364 if (info->tty_break) {
365 info->tty_break = 0;
366 flag = TTY_BREAK;
367 if (info->flags & ZILOG_SAK)
368 do_SAK(tty);
369 /* Ignore the null char got when BREAK is removed. */
370 if (ch == 0)
371 continue;
372 } else {
373 if (stat & Rx_OVR) {
374 flag = TTY_OVERRUN;
375 } else if (stat & FRM_ERR) {
376 flag = TTY_FRAME;
377 } else if (stat & PAR_ERR) {
378 flag = TTY_PARITY;
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) && \
386 !defined(MODULE)
387 if (break_pressed && info->line == sercons.index) {
388 /* Ignore the null char got when BREAK is removed. */
389 if (ch == 0)
390 continue;
391 if (time_before(jiffies, break_pressed + HZ * 5)) {
392 handle_sysrq(ch, NULL);
393 break_pressed = 0;
394 continue;
396 break_pressed = 0;
398 #endif
400 if (info->hook && info->hook->rx_char) {
401 (*info->hook->rx_char)(ch, flag);
402 return;
405 tty_insert_flip_char(tty, ch, flag);
407 if (tty)
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)
414 return;
415 info->tx_active = 0;
417 if (info->x_char) {
418 /* Send next char */
419 write_zsdata(info->zs_channel, info->x_char);
420 info->x_char = 0;
421 info->tx_active = 1;
422 return;
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);
428 return;
430 /* Send char */
431 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
432 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
433 info->xmit_cnt--;
434 info->tx_active = 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)
442 unsigned char stat;
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) && \
449 !defined(MODULE)
450 if (info->line == sercons.index) {
451 if (!break_pressed)
452 break_pressed = jiffies;
453 } else
454 #endif
455 info->tty_break = 1;
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 ) {
463 if (stat & DCD) {
464 wake_up_interruptible(&info->open_wait);
465 } else {
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);
478 } else {
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;
498 int shift;
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 */
510 else
511 shift = 0; /* Channel B */
513 for (;;) {
514 zs_intreg = read_zsreg(info->zs_chan_a, R3) >> shift;
515 if ((zs_intreg & CHAN_IRQMASK) == 0)
516 break;
518 status = IRQ_HANDLED;
520 if (zs_intreg & CHBRxIP) {
521 receive_chars(info);
523 if (zs_intreg & CHBTxIP) {
524 transmit_chars(info);
526 if (zs_intreg & CHBEXT) {
527 status_handle(info);
531 /* Why do we need this ? */
532 write_zsreg(info->zs_channel, 0, RES_H_IUS);
534 return status;
537 #ifdef ZS_DEBUG_REGS
538 void zs_dump (void) {
539 int i, j;
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));
551 printk("\n\n");
555 #endif
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;
573 unsigned long flags;
575 if (serial_paranoia_check(info, tty->name, "rs_stop"))
576 return;
578 #if 1
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);
585 #endif
588 static void rs_start(struct tty_struct *tty)
590 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
591 unsigned long flags;
593 if (serial_paranoia_check(info, tty->name, "rs_start"))
594 return;
596 spin_lock_irqsave(&zs_lock, flags);
597 #if 1
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]);
602 #else
603 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
604 transmit_chars(info);
606 #endif
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;
625 tty = info->tty;
626 if (!tty)
627 return;
629 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
630 tty_wakeup(tty);
631 wake_up_interruptible(&tty->write_wait);
635 static int zs_startup(struct dec_serial * info)
637 unsigned long flags;
639 if (info->flags & ZILOG_INITIALIZED)
640 return 0;
642 if (!info->xmit_buf) {
643 info->xmit_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
644 if (!info->xmit_buf)
645 return -ENOMEM;
648 spin_lock_irqsave(&zs_lock, flags);
650 #ifdef SERIAL_DEBUG_OPEN
651 printk("starting up ttyS%d (irq %d)...", info->line, info->irq);
652 #endif
655 * Clear the receive FIFO.
657 ZS_CLEARFIFO(info->zs_channel);
658 info->xmit_fifo_size = 1;
661 * Clear the interrupt registers.
663 write_zsreg(info->zs_channel, R0, ERR_RES);
664 write_zsreg(info->zs_channel, R0, RES_H_IUS);
667 * Set the speed of the serial port
669 change_speed(info);
672 * Turn on RTS and DTR.
674 zs_rtsdtr(info, RTS | DTR, 1);
677 * Finally, enable sequencing and interrupts
679 info->zs_channel->curregs[R1] &= ~RxINT_MASK;
680 info->zs_channel->curregs[R1] |= (RxINT_ALL | TxINT_ENAB |
681 EXT_INT_ENAB);
682 info->zs_channel->curregs[R3] |= RxENABLE;
683 info->zs_channel->curregs[R5] |= TxENAB;
684 info->zs_channel->curregs[R15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
685 write_zsreg(info->zs_channel, R1, info->zs_channel->curregs[R1]);
686 write_zsreg(info->zs_channel, R3, info->zs_channel->curregs[R3]);
687 write_zsreg(info->zs_channel, R5, info->zs_channel->curregs[R5]);
688 write_zsreg(info->zs_channel, R15, info->zs_channel->curregs[R15]);
691 * And clear the interrupt registers again for luck.
693 write_zsreg(info->zs_channel, R0, ERR_RES);
694 write_zsreg(info->zs_channel, R0, RES_H_IUS);
696 /* Save the current value of RR0 */
697 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
699 if (info->tty)
700 clear_bit(TTY_IO_ERROR, &info->tty->flags);
701 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
703 info->flags |= ZILOG_INITIALIZED;
704 spin_unlock_irqrestore(&zs_lock, flags);
705 return 0;
709 * This routine will shutdown a serial port; interrupts are disabled, and
710 * DTR is dropped if the hangup on close termio flag is on.
712 static void shutdown(struct dec_serial * info)
714 unsigned long flags;
716 if (!(info->flags & ZILOG_INITIALIZED))
717 return;
719 #ifdef SERIAL_DEBUG_OPEN
720 printk("Shutting down serial port %d (irq %d)....", info->line,
721 info->irq);
722 #endif
724 spin_lock_irqsave(&zs_lock, flags);
726 if (info->xmit_buf) {
727 free_page((unsigned long) info->xmit_buf);
728 info->xmit_buf = 0;
731 info->zs_channel->curregs[1] = 0;
732 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]); /* no interrupts */
734 info->zs_channel->curregs[3] &= ~RxENABLE;
735 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
737 info->zs_channel->curregs[5] &= ~TxENAB;
738 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
739 if (!info->tty || C_HUPCL(info->tty)) {
740 zs_rtsdtr(info, RTS | DTR, 0);
743 if (info->tty)
744 set_bit(TTY_IO_ERROR, &info->tty->flags);
746 info->flags &= ~ZILOG_INITIALIZED;
747 spin_unlock_irqrestore(&zs_lock, flags);
751 * This routine is called to set the UART divisor registers to match
752 * the specified baud rate for a serial port.
754 static void change_speed(struct dec_serial *info)
756 unsigned cflag;
757 int i;
758 int brg, bits;
759 unsigned long flags;
761 if (!info->hook) {
762 if (!info->tty || !info->tty->termios)
763 return;
764 cflag = info->tty->termios->c_cflag;
765 if (!info->port)
766 return;
767 } else {
768 cflag = info->hook->cflags;
771 i = cflag & CBAUD;
772 if (i & CBAUDEX) {
773 i &= ~CBAUDEX;
774 if (i < 1 || i > 2) {
775 if (!info->hook)
776 info->tty->termios->c_cflag &= ~CBAUDEX;
777 else
778 info->hook->cflags &= ~CBAUDEX;
779 } else
780 i += 15;
783 spin_lock_irqsave(&zs_lock, flags);
784 info->zs_baud = baud_table[i];
785 if (info->zs_baud) {
786 brg = BPS_TO_BRG(info->zs_baud, zs_parms->clock/info->clk_divisor);
787 info->zs_channel->curregs[12] = (brg & 255);
788 info->zs_channel->curregs[13] = ((brg >> 8) & 255);
789 zs_rtsdtr(info, DTR, 1);
790 } else {
791 zs_rtsdtr(info, RTS | DTR, 0);
792 return;
795 /* byte size and parity */
796 info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
797 info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
798 switch (cflag & CSIZE) {
799 case CS5:
800 bits = 7;
801 info->zs_channel->curregs[3] |= Rx5;
802 info->zs_channel->curregs[5] |= Tx5;
803 break;
804 case CS6:
805 bits = 8;
806 info->zs_channel->curregs[3] |= Rx6;
807 info->zs_channel->curregs[5] |= Tx6;
808 break;
809 case CS7:
810 bits = 9;
811 info->zs_channel->curregs[3] |= Rx7;
812 info->zs_channel->curregs[5] |= Tx7;
813 break;
814 case CS8:
815 default: /* defaults to 8 bits */
816 bits = 10;
817 info->zs_channel->curregs[3] |= Rx8;
818 info->zs_channel->curregs[5] |= Tx8;
819 break;
822 info->timeout = ((info->xmit_fifo_size*HZ*bits) / info->zs_baud);
823 info->timeout += HZ/50; /* Add .02 seconds of slop */
825 info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
826 if (cflag & CSTOPB) {
827 info->zs_channel->curregs[4] |= SB2;
828 } else {
829 info->zs_channel->curregs[4] |= SB1;
831 if (cflag & PARENB) {
832 info->zs_channel->curregs[4] |= PAR_ENA;
834 if (!(cflag & PARODD)) {
835 info->zs_channel->curregs[4] |= PAR_EVEN;
838 if (!(cflag & CLOCAL)) {
839 if (!(info->zs_channel->curregs[15] & DCDIE))
840 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
841 info->zs_channel->curregs[15] |= DCDIE;
842 } else
843 info->zs_channel->curregs[15] &= ~DCDIE;
844 if (cflag & CRTSCTS) {
845 info->zs_channel->curregs[15] |= CTSIE;
846 if ((read_zsreg(info->zs_channel, 0) & CTS) == 0)
847 info->tx_stopped = 1;
848 } else {
849 info->zs_channel->curregs[15] &= ~CTSIE;
850 info->tx_stopped = 0;
853 /* Load up the new values */
854 load_zsregs(info->zs_channel, info->zs_channel->curregs);
856 spin_unlock_irqrestore(&zs_lock, flags);
859 static void rs_flush_chars(struct tty_struct *tty)
861 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
862 unsigned long flags;
864 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
865 return;
867 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
868 !info->xmit_buf)
869 return;
871 /* Enable transmitter */
872 spin_lock_irqsave(&zs_lock, flags);
873 transmit_chars(info);
874 spin_unlock_irqrestore(&zs_lock, flags);
877 static int rs_write(struct tty_struct * tty,
878 const unsigned char *buf, int count)
880 int c, total = 0;
881 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
882 unsigned long flags;
884 if (serial_paranoia_check(info, tty->name, "rs_write"))
885 return 0;
887 if (!tty || !info->xmit_buf)
888 return 0;
890 while (1) {
891 spin_lock_irqsave(&zs_lock, flags);
892 c = min(count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
893 SERIAL_XMIT_SIZE - info->xmit_head));
894 if (c <= 0)
895 break;
897 memcpy(info->xmit_buf + info->xmit_head, buf, c);
898 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
899 info->xmit_cnt += c;
900 spin_unlock_irqrestore(&zs_lock, flags);
901 buf += c;
902 count -= c;
903 total += c;
906 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
907 && !info->tx_active)
908 transmit_chars(info);
909 spin_unlock_irqrestore(&zs_lock, flags);
910 return total;
913 static int rs_write_room(struct tty_struct *tty)
915 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
916 int ret;
918 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
919 return 0;
920 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
921 if (ret < 0)
922 ret = 0;
923 return ret;
926 static int rs_chars_in_buffer(struct tty_struct *tty)
928 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
930 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
931 return 0;
932 return info->xmit_cnt;
935 static void rs_flush_buffer(struct tty_struct *tty)
937 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
939 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
940 return;
941 spin_lock_irq(&zs_lock);
942 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
943 spin_unlock_irq(&zs_lock);
944 tty_wakeup(tty);
948 * ------------------------------------------------------------
949 * rs_throttle()
951 * This routine is called by the upper-layer tty layer to signal that
952 * incoming characters should be throttled.
953 * ------------------------------------------------------------
955 static void rs_throttle(struct tty_struct * tty)
957 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
958 unsigned long flags;
960 #ifdef SERIAL_DEBUG_THROTTLE
961 char buf[64];
963 printk("throttle %s: %d....\n", _tty_name(tty, buf),
964 tty->ldisc.chars_in_buffer(tty));
965 #endif
967 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
968 return;
970 if (I_IXOFF(tty)) {
971 spin_lock_irqsave(&zs_lock, flags);
972 info->x_char = STOP_CHAR(tty);
973 if (!info->tx_active)
974 transmit_chars(info);
975 spin_unlock_irqrestore(&zs_lock, flags);
978 if (C_CRTSCTS(tty)) {
979 zs_rtsdtr(info, RTS, 0);
983 static void rs_unthrottle(struct tty_struct * tty)
985 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
986 unsigned long flags;
988 #ifdef SERIAL_DEBUG_THROTTLE
989 char buf[64];
991 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
992 tty->ldisc.chars_in_buffer(tty));
993 #endif
995 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
996 return;
998 if (I_IXOFF(tty)) {
999 spin_lock_irqsave(&zs_lock, flags);
1000 if (info->x_char)
1001 info->x_char = 0;
1002 else {
1003 info->x_char = START_CHAR(tty);
1004 if (!info->tx_active)
1005 transmit_chars(info);
1007 spin_unlock_irqrestore(&zs_lock, flags);
1010 if (C_CRTSCTS(tty)) {
1011 zs_rtsdtr(info, RTS, 1);
1016 * ------------------------------------------------------------
1017 * rs_ioctl() and friends
1018 * ------------------------------------------------------------
1021 static int get_serial_info(struct dec_serial * info,
1022 struct serial_struct * retinfo)
1024 struct serial_struct tmp;
1026 if (!retinfo)
1027 return -EFAULT;
1028 memset(&tmp, 0, sizeof(tmp));
1029 tmp.type = info->type;
1030 tmp.line = info->line;
1031 tmp.port = info->port;
1032 tmp.irq = info->irq;
1033 tmp.flags = info->flags;
1034 tmp.baud_base = info->baud_base;
1035 tmp.close_delay = info->close_delay;
1036 tmp.closing_wait = info->closing_wait;
1037 tmp.custom_divisor = info->custom_divisor;
1038 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
1041 static int set_serial_info(struct dec_serial * info,
1042 struct serial_struct * new_info)
1044 struct serial_struct new_serial;
1045 struct dec_serial old_info;
1046 int retval = 0;
1048 if (!new_info)
1049 return -EFAULT;
1050 copy_from_user(&new_serial,new_info,sizeof(new_serial));
1051 old_info = *info;
1053 if (!capable(CAP_SYS_ADMIN)) {
1054 if ((new_serial.baud_base != info->baud_base) ||
1055 (new_serial.type != info->type) ||
1056 (new_serial.close_delay != info->close_delay) ||
1057 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1058 (info->flags & ~ZILOG_USR_MASK)))
1059 return -EPERM;
1060 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1061 (new_serial.flags & ZILOG_USR_MASK));
1062 info->custom_divisor = new_serial.custom_divisor;
1063 goto check_and_exit;
1066 if (info->count > 1)
1067 return -EBUSY;
1070 * OK, past this point, all the error checking has been done.
1071 * At this point, we start making changes.....
1074 info->baud_base = new_serial.baud_base;
1075 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1076 (new_serial.flags & ZILOG_FLAGS));
1077 info->type = new_serial.type;
1078 info->close_delay = new_serial.close_delay;
1079 info->closing_wait = new_serial.closing_wait;
1081 check_and_exit:
1082 retval = zs_startup(info);
1083 return retval;
1087 * get_lsr_info - get line status register info
1089 * Purpose: Let user call ioctl() to get info when the UART physically
1090 * is emptied. On bus types like RS485, the transmitter must
1091 * release the bus after transmitting. This must be done when
1092 * the transmit shift register is empty, not be done when the
1093 * transmit holding register is empty. This functionality
1094 * allows an RS485 driver to be written in user space.
1096 static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1098 unsigned char status;
1100 spin_lock(&zs_lock);
1101 status = read_zsreg(info->zs_channel, 0);
1102 spin_unlock_irq(&zs_lock);
1103 put_user(status,value);
1104 return 0;
1107 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1109 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1110 unsigned char control, status_a, status_b;
1111 unsigned int result;
1113 if (info->hook)
1114 return -ENODEV;
1116 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1117 return -ENODEV;
1119 if (tty->flags & (1 << TTY_IO_ERROR))
1120 return -EIO;
1122 if (info->zs_channel == info->zs_chan_a)
1123 result = 0;
1124 else {
1125 spin_lock(&zs_lock);
1126 control = info->zs_chan_a->curregs[5];
1127 status_a = read_zsreg(info->zs_chan_a, 0);
1128 status_b = read_zsreg(info->zs_channel, 0);
1129 spin_unlock_irq(&zs_lock);
1130 result = ((control & RTS) ? TIOCM_RTS: 0)
1131 | ((control & DTR) ? TIOCM_DTR: 0)
1132 | ((status_b & DCD) ? TIOCM_CAR: 0)
1133 | ((status_a & DCD) ? TIOCM_RNG: 0)
1134 | ((status_a & SYNC_HUNT) ? TIOCM_DSR: 0)
1135 | ((status_b & CTS) ? TIOCM_CTS: 0);
1137 return result;
1140 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1141 unsigned int set, unsigned int clear)
1143 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1145 if (info->hook)
1146 return -ENODEV;
1148 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1149 return -ENODEV;
1151 if (tty->flags & (1 << TTY_IO_ERROR))
1152 return -EIO;
1154 if (info->zs_channel == info->zs_chan_a)
1155 return 0;
1157 spin_lock(&zs_lock);
1158 if (set & TIOCM_RTS)
1159 info->zs_chan_a->curregs[5] |= RTS;
1160 if (set & TIOCM_DTR)
1161 info->zs_chan_a->curregs[5] |= DTR;
1162 if (clear & TIOCM_RTS)
1163 info->zs_chan_a->curregs[5] &= ~RTS;
1164 if (clear & TIOCM_DTR)
1165 info->zs_chan_a->curregs[5] &= ~DTR;
1166 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1167 spin_unlock_irq(&zs_lock);
1168 return 0;
1172 * rs_break - turn transmit break condition on/off
1174 static void rs_break(struct tty_struct *tty, int break_state)
1176 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1177 unsigned long flags;
1179 if (serial_paranoia_check(info, tty->name, "rs_break"))
1180 return;
1181 if (!info->port)
1182 return;
1184 spin_lock_irqsave(&zs_lock, flags);
1185 if (break_state == -1)
1186 info->zs_channel->curregs[5] |= SND_BRK;
1187 else
1188 info->zs_channel->curregs[5] &= ~SND_BRK;
1189 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1190 spin_unlock_irqrestore(&zs_lock, flags);
1193 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1194 unsigned int cmd, unsigned long arg)
1196 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1198 if (info->hook)
1199 return -ENODEV;
1201 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1202 return -ENODEV;
1204 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1205 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1206 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1207 if (tty->flags & (1 << TTY_IO_ERROR))
1208 return -EIO;
1211 switch (cmd) {
1212 case TIOCGSERIAL:
1213 if (!access_ok(VERIFY_WRITE, (void *)arg,
1214 sizeof(struct serial_struct)))
1215 return -EFAULT;
1216 return get_serial_info(info, (struct serial_struct *)arg);
1218 case TIOCSSERIAL:
1219 return set_serial_info(info, (struct serial_struct *)arg);
1221 case TIOCSERGETLSR: /* Get line status register */
1222 if (!access_ok(VERIFY_WRITE, (void *)arg,
1223 sizeof(unsigned int)))
1224 return -EFAULT;
1225 return get_lsr_info(info, (unsigned int *)arg);
1227 case TIOCSERGSTRUCT:
1228 if (!access_ok(VERIFY_WRITE, (void *)arg,
1229 sizeof(struct dec_serial)))
1230 return -EFAULT;
1231 copy_from_user((struct dec_serial *)arg, info,
1232 sizeof(struct dec_serial));
1233 return 0;
1235 default:
1236 return -ENOIOCTLCMD;
1238 return 0;
1241 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1243 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1244 int was_stopped;
1246 if (tty->termios->c_cflag == old_termios->c_cflag)
1247 return;
1248 was_stopped = info->tx_stopped;
1250 change_speed(info);
1252 if (was_stopped && !info->tx_stopped)
1253 rs_start(tty);
1257 * ------------------------------------------------------------
1258 * rs_close()
1260 * This routine is called when the serial port gets closed.
1261 * Wait for the last remaining data to be sent.
1262 * ------------------------------------------------------------
1264 static void rs_close(struct tty_struct *tty, struct file * filp)
1266 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1267 unsigned long flags;
1269 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1270 return;
1272 spin_lock_irqsave(&zs_lock, flags);
1274 if (tty_hung_up_p(filp)) {
1275 spin_unlock_irqrestore(&zs_lock, flags);
1276 return;
1279 #ifdef SERIAL_DEBUG_OPEN
1280 printk("rs_close ttyS%d, count = %d\n", info->line, info->count);
1281 #endif
1282 if ((tty->count == 1) && (info->count != 1)) {
1284 * Uh, oh. tty->count is 1, which means that the tty
1285 * structure will be freed. Info->count should always
1286 * be one in these conditions. If it's greater than
1287 * one, we've got real problems, since it means the
1288 * serial port won't be shutdown.
1290 printk("rs_close: bad serial port count; tty->count is 1, "
1291 "info->count is %d\n", info->count);
1292 info->count = 1;
1294 if (--info->count < 0) {
1295 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1296 info->line, info->count);
1297 info->count = 0;
1299 if (info->count) {
1300 spin_unlock_irqrestore(&zs_lock, flags);
1301 return;
1303 info->flags |= ZILOG_CLOSING;
1305 * Now we wait for the transmit buffer to clear; and we notify
1306 * the line discipline to only process XON/XOFF characters.
1308 tty->closing = 1;
1309 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1310 tty_wait_until_sent(tty, info->closing_wait);
1312 * At this point we stop accepting input. To do this, we
1313 * disable the receiver and receive interrupts.
1315 info->zs_channel->curregs[3] &= ~RxENABLE;
1316 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1317 info->zs_channel->curregs[1] = 0; /* disable any rx ints */
1318 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1319 ZS_CLEARFIFO(info->zs_channel);
1320 if (info->flags & ZILOG_INITIALIZED) {
1322 * Before we drop DTR, make sure the SCC transmitter
1323 * has completely drained.
1325 rs_wait_until_sent(tty, info->timeout);
1328 shutdown(info);
1329 if (tty->driver->flush_buffer)
1330 tty->driver->flush_buffer(tty);
1331 tty_ldisc_flush(tty);
1332 tty->closing = 0;
1333 info->event = 0;
1334 info->tty = 0;
1335 if (info->blocked_open) {
1336 if (info->close_delay) {
1337 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1339 wake_up_interruptible(&info->open_wait);
1341 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CLOSING);
1342 wake_up_interruptible(&info->close_wait);
1343 spin_unlock_irqrestore(&zs_lock, flags);
1347 * rs_wait_until_sent() --- wait until the transmitter is empty
1349 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1351 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1352 unsigned long orig_jiffies;
1353 int char_time;
1355 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1356 return;
1358 orig_jiffies = jiffies;
1360 * Set the check interval to be 1/5 of the estimated time to
1361 * send a single character, and make it at least 1. The check
1362 * interval should also be less than the timeout.
1364 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1365 char_time = char_time / 5;
1366 if (char_time == 0)
1367 char_time = 1;
1368 if (timeout)
1369 char_time = min(char_time, timeout);
1370 while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
1371 msleep_interruptible(jiffies_to_msecs(char_time));
1372 if (signal_pending(current))
1373 break;
1374 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1375 break;
1377 current->state = TASK_RUNNING;
1381 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1383 static void rs_hangup(struct tty_struct *tty)
1385 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1387 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1388 return;
1390 rs_flush_buffer(tty);
1391 shutdown(info);
1392 info->event = 0;
1393 info->count = 0;
1394 info->flags &= ~ZILOG_NORMAL_ACTIVE;
1395 info->tty = 0;
1396 wake_up_interruptible(&info->open_wait);
1400 * ------------------------------------------------------------
1401 * rs_open() and friends
1402 * ------------------------------------------------------------
1404 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1405 struct dec_serial *info)
1407 DECLARE_WAITQUEUE(wait, current);
1408 int retval;
1409 int do_clocal = 0;
1412 * If the device is in the middle of being closed, then block
1413 * until it's done, and then try again.
1415 if (info->flags & ZILOG_CLOSING) {
1416 interruptible_sleep_on(&info->close_wait);
1417 #ifdef SERIAL_DO_RESTART
1418 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1419 -EAGAIN : -ERESTARTSYS);
1420 #else
1421 return -EAGAIN;
1422 #endif
1426 * If non-blocking mode is set, or the port is not enabled,
1427 * then make the check up front and then exit.
1429 if ((filp->f_flags & O_NONBLOCK) ||
1430 (tty->flags & (1 << TTY_IO_ERROR))) {
1431 info->flags |= ZILOG_NORMAL_ACTIVE;
1432 return 0;
1435 if (tty->termios->c_cflag & CLOCAL)
1436 do_clocal = 1;
1439 * Block waiting for the carrier detect and the line to become
1440 * free (i.e., not in use by the callout). While we are in
1441 * this loop, info->count is dropped by one, so that
1442 * rs_close() knows when to free things. We restore it upon
1443 * exit, either normal or abnormal.
1445 retval = 0;
1446 add_wait_queue(&info->open_wait, &wait);
1447 #ifdef SERIAL_DEBUG_OPEN
1448 printk("block_til_ready before block: ttyS%d, count = %d\n",
1449 info->line, info->count);
1450 #endif
1451 spin_lock(&zs_lock);
1452 if (!tty_hung_up_p(filp))
1453 info->count--;
1454 spin_unlock_irq(&zs_lock);
1455 info->blocked_open++;
1456 while (1) {
1457 spin_lock(&zs_lock);
1458 if (tty->termios->c_cflag & CBAUD)
1459 zs_rtsdtr(info, RTS | DTR, 1);
1460 spin_unlock_irq(&zs_lock);
1461 set_current_state(TASK_INTERRUPTIBLE);
1462 if (tty_hung_up_p(filp) ||
1463 !(info->flags & ZILOG_INITIALIZED)) {
1464 #ifdef SERIAL_DO_RESTART
1465 if (info->flags & ZILOG_HUP_NOTIFY)
1466 retval = -EAGAIN;
1467 else
1468 retval = -ERESTARTSYS;
1469 #else
1470 retval = -EAGAIN;
1471 #endif
1472 break;
1474 if (!(info->flags & ZILOG_CLOSING) &&
1475 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1476 break;
1477 if (signal_pending(current)) {
1478 retval = -ERESTARTSYS;
1479 break;
1481 #ifdef SERIAL_DEBUG_OPEN
1482 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1483 info->line, info->count);
1484 #endif
1485 schedule();
1487 current->state = TASK_RUNNING;
1488 remove_wait_queue(&info->open_wait, &wait);
1489 if (!tty_hung_up_p(filp))
1490 info->count++;
1491 info->blocked_open--;
1492 #ifdef SERIAL_DEBUG_OPEN
1493 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1494 info->line, info->count);
1495 #endif
1496 if (retval)
1497 return retval;
1498 info->flags |= ZILOG_NORMAL_ACTIVE;
1499 return 0;
1503 * This routine is called whenever a serial port is opened. It
1504 * enables interrupts for a serial port, linking in its ZILOG structure into
1505 * the IRQ chain. It also performs the serial-specific
1506 * initialization for the tty structure.
1508 static int rs_open(struct tty_struct *tty, struct file * filp)
1510 struct dec_serial *info;
1511 int retval, line;
1513 line = tty->index;
1514 if ((line < 0) || (line >= zs_channels_found))
1515 return -ENODEV;
1516 info = zs_soft + line;
1518 if (info->hook)
1519 return -ENODEV;
1521 if (serial_paranoia_check(info, tty->name, "rs_open"))
1522 return -ENODEV;
1523 #ifdef SERIAL_DEBUG_OPEN
1524 printk("rs_open %s, count = %d\n", tty->name, info->count);
1525 #endif
1527 info->count++;
1528 tty->driver_data = info;
1529 info->tty = tty;
1532 * If the port is the middle of closing, bail out now
1534 if (tty_hung_up_p(filp) ||
1535 (info->flags & ZILOG_CLOSING)) {
1536 if (info->flags & ZILOG_CLOSING)
1537 interruptible_sleep_on(&info->close_wait);
1538 #ifdef SERIAL_DO_RESTART
1539 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1540 -EAGAIN : -ERESTARTSYS);
1541 #else
1542 return -EAGAIN;
1543 #endif
1547 * Start up serial port
1549 retval = zs_startup(info);
1550 if (retval)
1551 return retval;
1553 retval = block_til_ready(tty, filp, info);
1554 if (retval) {
1555 #ifdef SERIAL_DEBUG_OPEN
1556 printk("rs_open returning after block_til_ready with %d\n",
1557 retval);
1558 #endif
1559 return retval;
1562 #ifdef CONFIG_SERIAL_DEC_CONSOLE
1563 if (sercons.cflag && sercons.index == line) {
1564 tty->termios->c_cflag = sercons.cflag;
1565 sercons.cflag = 0;
1566 change_speed(info);
1568 #endif
1570 #ifdef SERIAL_DEBUG_OPEN
1571 printk("rs_open %s successful...", tty->name);
1572 #endif
1573 /* tty->low_latency = 1; */
1574 return 0;
1577 /* Finally, routines used to initialize the serial driver. */
1579 static void __init show_serial_version(void)
1581 printk("DECstation Z8530 serial driver version 0.09\n");
1584 /* Initialize Z8530s zs_channels
1587 static void __init probe_sccs(void)
1589 struct dec_serial **pp;
1590 int i, n, n_chips = 0, n_channels, chip, channel;
1591 unsigned long flags;
1594 * did we get here by accident?
1596 if(!BUS_PRESENT) {
1597 printk("Not on JUNKIO machine, skipping probe_sccs\n");
1598 return;
1601 switch(mips_machtype) {
1602 #ifdef CONFIG_MACH_DECSTATION
1603 case MACH_DS5000_2X0:
1604 case MACH_DS5900:
1605 n_chips = 2;
1606 zs_parms = &ds_parms;
1607 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1608 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1609 break;
1610 case MACH_DS5000_1XX:
1611 n_chips = 2;
1612 zs_parms = &ds_parms;
1613 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1614 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1615 break;
1616 case MACH_DS5000_XX:
1617 n_chips = 1;
1618 zs_parms = &ds_parms;
1619 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1620 break;
1621 #endif
1622 default:
1623 panic("zs: unsupported bus");
1625 if (!zs_parms)
1626 panic("zs: uninitialized parms");
1628 pp = &zs_chain;
1630 n_channels = 0;
1632 for (chip = 0; chip < n_chips; chip++) {
1633 for (channel = 0; channel <= 1; channel++) {
1635 * The sccs reside on the high byte of the 16 bit IOBUS
1637 zs_channels[n_channels].control =
1638 (volatile void *)CKSEG1ADDR(dec_kn_slot_base +
1639 (0 == chip ? zs_parms->scc0 : zs_parms->scc1) +
1640 (0 == channel ? zs_parms->channel_a_offset :
1641 zs_parms->channel_b_offset));
1642 zs_channels[n_channels].data =
1643 zs_channels[n_channels].control + 4;
1645 #ifndef CONFIG_SERIAL_DEC_CONSOLE
1647 * We're called early and memory managment isn't up, yet.
1648 * Thus request_region would fail.
1650 if (!request_region((unsigned long)
1651 zs_channels[n_channels].control,
1652 ZS_CHAN_IO_SIZE, "SCC"))
1653 panic("SCC I/O region is not free");
1654 #endif
1655 zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1656 /* HACK alert! */
1657 if (!(chip & 1))
1658 zs_soft[n_channels].irq = zs_parms->irq0;
1659 else
1660 zs_soft[n_channels].irq = zs_parms->irq1;
1663 * Identification of channel A. Location of channel A
1664 * inside chip depends on mapping of internal address
1665 * the chip decodes channels by.
1666 * CHANNEL_A_NR returns either 0 (in case of
1667 * DECstations) or 1 (in case of Baget).
1669 if (CHANNEL_A_NR == channel)
1670 zs_soft[n_channels].zs_chan_a =
1671 &zs_channels[n_channels+1-2*CHANNEL_A_NR];
1672 else
1673 zs_soft[n_channels].zs_chan_a =
1674 &zs_channels[n_channels];
1676 *pp = &zs_soft[n_channels];
1677 pp = &zs_soft[n_channels].zs_next;
1678 n_channels++;
1682 *pp = 0;
1683 zs_channels_found = n_channels;
1685 for (n = 0; n < zs_channels_found; n++) {
1686 for (i = 0; i < 16; i++) {
1687 zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1691 spin_lock_irqsave(&zs_lock, flags);
1692 for (n = 0; n < zs_channels_found; n++) {
1693 if (n % 2 == 0) {
1694 write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
1695 udelay(10);
1696 write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
1698 load_zsregs(zs_soft[n].zs_channel,
1699 zs_soft[n].zs_channel->curregs);
1701 spin_unlock_irqrestore(&zs_lock, flags);
1704 static const struct tty_operations serial_ops = {
1705 .open = rs_open,
1706 .close = rs_close,
1707 .write = rs_write,
1708 .flush_chars = rs_flush_chars,
1709 .write_room = rs_write_room,
1710 .chars_in_buffer = rs_chars_in_buffer,
1711 .flush_buffer = rs_flush_buffer,
1712 .ioctl = rs_ioctl,
1713 .throttle = rs_throttle,
1714 .unthrottle = rs_unthrottle,
1715 .set_termios = rs_set_termios,
1716 .stop = rs_stop,
1717 .start = rs_start,
1718 .hangup = rs_hangup,
1719 .break_ctl = rs_break,
1720 .wait_until_sent = rs_wait_until_sent,
1721 .tiocmget = rs_tiocmget,
1722 .tiocmset = rs_tiocmset,
1725 /* zs_init inits the driver */
1726 int __init zs_init(void)
1728 int channel, i;
1729 struct dec_serial *info;
1731 if(!BUS_PRESENT)
1732 return -ENODEV;
1734 /* Find out how many Z8530 SCCs we have */
1735 if (zs_chain == 0)
1736 probe_sccs();
1737 serial_driver = alloc_tty_driver(zs_channels_found);
1738 if (!serial_driver)
1739 return -ENOMEM;
1741 show_serial_version();
1743 /* Initialize the tty_driver structure */
1744 /* Not all of this is exactly right for us. */
1746 serial_driver->owner = THIS_MODULE;
1747 serial_driver->name = "ttyS";
1748 serial_driver->major = TTY_MAJOR;
1749 serial_driver->minor_start = 64;
1750 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1751 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1752 serial_driver->init_termios = tty_std_termios;
1753 serial_driver->init_termios.c_cflag =
1754 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1755 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1756 tty_set_operations(serial_driver, &serial_ops);
1758 if (tty_register_driver(serial_driver))
1759 panic("Couldn't register serial driver");
1761 for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
1763 /* Needed before interrupts are enabled. */
1764 info->tty = 0;
1765 info->x_char = 0;
1767 if (info->hook && info->hook->init_info) {
1768 (*info->hook->init_info)(info);
1769 continue;
1772 info->magic = SERIAL_MAGIC;
1773 info->port = (int) info->zs_channel->control;
1774 info->line = i;
1775 info->custom_divisor = 16;
1776 info->close_delay = 50;
1777 info->closing_wait = 3000;
1778 info->event = 0;
1779 info->count = 0;
1780 info->blocked_open = 0;
1781 tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1782 init_waitqueue_head(&info->open_wait);
1783 init_waitqueue_head(&info->close_wait);
1784 printk("ttyS%02d at 0x%08x (irq = %d) is a Z85C30 SCC\n",
1785 info->line, info->port, info->irq);
1786 tty_register_device(serial_driver, info->line, NULL);
1790 for (channel = 0; channel < zs_channels_found; ++channel) {
1791 zs_soft[channel].clk_divisor = 16;
1792 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1794 if (request_irq(zs_soft[channel].irq, rs_interrupt, IRQF_SHARED,
1795 "scc", &zs_soft[channel]))
1796 printk(KERN_ERR "decserial: can't get irq %d\n",
1797 zs_soft[channel].irq);
1799 if (zs_soft[channel].hook) {
1800 zs_startup(&zs_soft[channel]);
1801 if (zs_soft[channel].hook->init_channel)
1802 (*zs_soft[channel].hook->init_channel)
1803 (&zs_soft[channel]);
1807 return 0;
1811 * polling I/O routines
1813 static int zs_poll_tx_char(void *handle, unsigned char ch)
1815 struct dec_serial *info = handle;
1816 struct dec_zschannel *chan = info->zs_channel;
1817 int ret;
1819 if(chan) {
1820 int loops = 10000;
1822 while (loops && !(read_zsreg(chan, 0) & Tx_BUF_EMP))
1823 loops--;
1825 if (loops) {
1826 write_zsdata(chan, ch);
1827 ret = 0;
1828 } else
1829 ret = -EAGAIN;
1831 return ret;
1832 } else
1833 return -ENODEV;
1836 static int zs_poll_rx_char(void *handle)
1838 struct dec_serial *info = handle;
1839 struct dec_zschannel *chan = info->zs_channel;
1840 int ret;
1842 if(chan) {
1843 int loops = 10000;
1845 while (loops && !(read_zsreg(chan, 0) & Rx_CH_AV))
1846 loops--;
1848 if (loops)
1849 ret = read_zsdata(chan);
1850 else
1851 ret = -EAGAIN;
1853 return ret;
1854 } else
1855 return -ENODEV;
1858 int register_zs_hook(unsigned int channel, struct dec_serial_hook *hook)
1860 struct dec_serial *info = &zs_soft[channel];
1862 if (info->hook) {
1863 printk("%s: line %d has already a hook registered\n",
1864 __FUNCTION__, channel);
1866 return 0;
1867 } else {
1868 hook->poll_rx_char = zs_poll_rx_char;
1869 hook->poll_tx_char = zs_poll_tx_char;
1870 info->hook = hook;
1872 return 1;
1876 int unregister_zs_hook(unsigned int channel)
1878 struct dec_serial *info = &zs_soft[channel];
1880 if (info->hook) {
1881 info->hook = NULL;
1882 return 1;
1883 } else {
1884 printk("%s: trying to unregister hook on line %d,"
1885 " but none is registered\n", __FUNCTION__, channel);
1886 return 0;
1891 * ------------------------------------------------------------
1892 * Serial console driver
1893 * ------------------------------------------------------------
1895 #ifdef CONFIG_SERIAL_DEC_CONSOLE
1899 * Print a string to the serial port trying not to disturb
1900 * any possible real use of the port...
1902 static void serial_console_write(struct console *co, const char *s,
1903 unsigned count)
1905 struct dec_serial *info;
1906 int i;
1908 info = zs_soft + co->index;
1910 for (i = 0; i < count; i++, s++) {
1911 if(*s == '\n')
1912 zs_poll_tx_char(info, '\r');
1913 zs_poll_tx_char(info, *s);
1917 static struct tty_driver *serial_console_device(struct console *c, int *index)
1919 *index = c->index;
1920 return serial_driver;
1924 * Setup initial baud/bits/parity. We do two things here:
1925 * - construct a cflag setting for the first rs_open()
1926 * - initialize the serial port
1927 * Return non-zero if we didn't find a serial port.
1929 static int __init serial_console_setup(struct console *co, char *options)
1931 struct dec_serial *info;
1932 int baud = 9600;
1933 int bits = 8;
1934 int parity = 'n';
1935 int cflag = CREAD | HUPCL | CLOCAL;
1936 int clk_divisor = 16;
1937 int brg;
1938 char *s;
1939 unsigned long flags;
1941 if(!BUS_PRESENT)
1942 return -ENODEV;
1944 info = zs_soft + co->index;
1946 if (zs_chain == 0)
1947 probe_sccs();
1949 info->is_cons = 1;
1951 if (options) {
1952 baud = simple_strtoul(options, NULL, 10);
1953 s = options;
1954 while(*s >= '0' && *s <= '9')
1955 s++;
1956 if (*s)
1957 parity = *s++;
1958 if (*s)
1959 bits = *s - '0';
1963 * Now construct a cflag setting.
1965 switch(baud) {
1966 case 1200:
1967 cflag |= B1200;
1968 break;
1969 case 2400:
1970 cflag |= B2400;
1971 break;
1972 case 4800:
1973 cflag |= B4800;
1974 break;
1975 case 19200:
1976 cflag |= B19200;
1977 break;
1978 case 38400:
1979 cflag |= B38400;
1980 break;
1981 case 57600:
1982 cflag |= B57600;
1983 break;
1984 case 115200:
1985 cflag |= B115200;
1986 break;
1987 case 9600:
1988 default:
1989 cflag |= B9600;
1991 * Set this to a sane value to prevent a divide error.
1993 baud = 9600;
1994 break;
1996 switch(bits) {
1997 case 7:
1998 cflag |= CS7;
1999 break;
2000 default:
2001 case 8:
2002 cflag |= CS8;
2003 break;
2005 switch(parity) {
2006 case 'o': case 'O':
2007 cflag |= PARODD;
2008 break;
2009 case 'e': case 'E':
2010 cflag |= PARENB;
2011 break;
2013 co->cflag = cflag;
2015 spin_lock_irqsave(&zs_lock, flags);
2018 * Set up the baud rate generator.
2020 brg = BPS_TO_BRG(baud, zs_parms->clock / clk_divisor);
2021 info->zs_channel->curregs[R12] = (brg & 255);
2022 info->zs_channel->curregs[R13] = ((brg >> 8) & 255);
2025 * Set byte size and parity.
2027 if (bits == 7) {
2028 info->zs_channel->curregs[R3] |= Rx7;
2029 info->zs_channel->curregs[R5] |= Tx7;
2030 } else {
2031 info->zs_channel->curregs[R3] |= Rx8;
2032 info->zs_channel->curregs[R5] |= Tx8;
2034 if (cflag & PARENB) {
2035 info->zs_channel->curregs[R4] |= PAR_ENA;
2037 if (!(cflag & PARODD)) {
2038 info->zs_channel->curregs[R4] |= PAR_EVEN;
2040 info->zs_channel->curregs[R4] |= SB1;
2043 * Turn on RTS and DTR.
2045 zs_rtsdtr(info, RTS | DTR, 1);
2048 * Finally, enable sequencing.
2050 info->zs_channel->curregs[R3] |= RxENABLE;
2051 info->zs_channel->curregs[R5] |= TxENAB;
2054 * Clear the interrupt registers.
2056 write_zsreg(info->zs_channel, R0, ERR_RES);
2057 write_zsreg(info->zs_channel, R0, RES_H_IUS);
2060 * Load up the new values.
2062 load_zsregs(info->zs_channel, info->zs_channel->curregs);
2064 /* Save the current value of RR0 */
2065 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
2067 zs_soft[co->index].clk_divisor = clk_divisor;
2068 zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
2070 spin_unlock_irqrestore(&zs_lock, flags);
2072 return 0;
2075 static struct console sercons = {
2076 .name = "ttyS",
2077 .write = serial_console_write,
2078 .device = serial_console_device,
2079 .setup = serial_console_setup,
2080 .flags = CON_PRINTBUFFER,
2081 .index = -1,
2085 * Register console.
2087 void __init zs_serial_console_init(void)
2089 register_console(&sercons);
2091 #endif /* ifdef CONFIG_SERIAL_DEC_CONSOLE */
2093 #ifdef CONFIG_KGDB
2094 struct dec_zschannel *zs_kgdbchan;
2095 static unsigned char scc_inittab[] = {
2096 9, 0x80, /* reset A side (CHRA) */
2097 13, 0, /* set baud rate divisor */
2098 12, 1,
2099 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
2100 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
2101 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
2102 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
2103 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
2106 /* These are for receiving and sending characters under the kgdb
2107 * source level kernel debugger.
2109 void putDebugChar(char kgdb_char)
2111 struct dec_zschannel *chan = zs_kgdbchan;
2112 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2113 RECOVERY_DELAY;
2114 write_zsdata(chan, kgdb_char);
2116 char getDebugChar(void)
2118 struct dec_zschannel *chan = zs_kgdbchan;
2119 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2120 eieio(); /*barrier();*/
2121 return read_zsdata(chan);
2123 void kgdb_interruptible(int yes)
2125 struct dec_zschannel *chan = zs_kgdbchan;
2126 int one, nine;
2127 nine = read_zsreg(chan, 9);
2128 if (yes == 1) {
2129 one = EXT_INT_ENAB|RxINT_ALL;
2130 nine |= MIE;
2131 printk("turning serial ints on\n");
2132 } else {
2133 one = RxINT_DISAB;
2134 nine &= ~MIE;
2135 printk("turning serial ints off\n");
2137 write_zsreg(chan, 1, one);
2138 write_zsreg(chan, 9, nine);
2141 static int kgdbhook_init_channel(void *handle)
2143 return 0;
2146 static void kgdbhook_init_info(void *handle)
2150 static void kgdbhook_rx_char(void *handle, unsigned char ch, unsigned char fl)
2152 struct dec_serial *info = handle;
2154 if (fl != TTY_NORMAL)
2155 return;
2156 if (ch == 0x03 || ch == '$')
2157 breakpoint();
2160 /* This sets up the serial port we're using, and turns on
2161 * interrupts for that channel, so kgdb is usable once we're done.
2163 static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2165 int brg;
2166 int i, x;
2167 volatile char *sccc = ms->control;
2168 brg = BPS_TO_BRG(bps, zs_parms->clock/16);
2169 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2170 for (i = 20000; i != 0; --i) {
2171 x = *sccc; eieio();
2173 for (i = 0; i < sizeof(scc_inittab); ++i) {
2174 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2175 i++;
2178 /* This is called at boot time to prime the kgdb serial debugging
2179 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2180 * for /dev/ttyb which is determined in setup_arch() from the
2181 * boot command line flags.
2183 struct dec_serial_hook zs_kgdbhook = {
2184 .init_channel = kgdbhook_init_channel,
2185 .init_info = kgdbhook_init_info,
2186 .rx_char = kgdbhook_rx_char,
2187 .cflags = B38400 | CS8 | CLOCAL,
2190 void __init zs_kgdb_hook(int tty_num)
2192 /* Find out how many Z8530 SCCs we have */
2193 if (zs_chain == 0)
2194 probe_sccs();
2195 zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2196 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2197 zs_soft[tty_num].change_needed = 0;
2198 zs_soft[tty_num].clk_divisor = 16;
2199 zs_soft[tty_num].zs_baud = 38400;
2200 zs_soft[tty_num].hook = &zs_kgdbhook; /* This runs kgdb */
2201 /* Turn on transmitter/receiver at 8-bits/char */
2202 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2203 printk("KGDB: on channel %d initialized\n", tty_num);
2204 set_debug_traps(); /* init stub */
2206 #endif /* ifdef CONFIG_KGDB */