improve bsg device allocation
[wrt350n-kernel.git] / drivers / tc / zs.c
blobed979f13908a3f358d08e97786a4a65e27138d38
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>
74 #ifdef CONFIG_KGDB
75 #include <asm/kgdb.h>
76 #endif
77 #ifdef CONFIG_MAGIC_SYSRQ
78 #include <linux/sysrq.h>
79 #endif
81 #include "zs.h"
84 * It would be nice to dynamically allocate everything that
85 * depends on NUM_SERIAL, so we could support any number of
86 * Z8530s, but for now...
88 #define NUM_SERIAL 2 /* Max number of ZS chips supported */
89 #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
90 #define CHANNEL_A_NR (zs_parms->channel_a_offset > zs_parms->channel_b_offset)
91 /* Number of channel A in the chip */
92 #define ZS_CHAN_IO_SIZE 8
93 #define ZS_CLOCK 7372800 /* Z8530 RTxC input clock rate */
95 #define RECOVERY_DELAY udelay(2)
97 struct zs_parms {
98 unsigned long scc0;
99 unsigned long scc1;
100 int channel_a_offset;
101 int channel_b_offset;
102 int irq0;
103 int irq1;
104 int clock;
107 static struct zs_parms *zs_parms;
109 #ifdef CONFIG_MACH_DECSTATION
110 static struct zs_parms ds_parms = {
111 scc0 : IOASIC_SCC0,
112 scc1 : IOASIC_SCC1,
113 channel_a_offset : 1,
114 channel_b_offset : 9,
115 irq0 : -1,
116 irq1 : -1,
117 clock : ZS_CLOCK
119 #endif
121 #ifdef CONFIG_MACH_DECSTATION
122 #define DS_BUS_PRESENT (IOASIC)
123 #else
124 #define DS_BUS_PRESENT 0
125 #endif
127 #define BUS_PRESENT (DS_BUS_PRESENT)
129 DEFINE_SPINLOCK(zs_lock);
131 struct dec_zschannel zs_channels[NUM_CHANNELS];
132 struct dec_serial zs_soft[NUM_CHANNELS];
133 int zs_channels_found;
134 struct dec_serial *zs_chain; /* list of all channels */
136 struct tty_struct zs_ttys[NUM_CHANNELS];
138 #ifdef CONFIG_SERIAL_DEC_CONSOLE
139 static struct console zs_console;
140 #endif
141 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
142 !defined(MODULE)
143 static unsigned long break_pressed; /* break, really ... */
144 #endif
146 static unsigned char zs_init_regs[16] __initdata = {
147 0, /* write 0 */
148 0, /* write 1 */
149 0, /* write 2 */
150 0, /* write 3 */
151 (X16CLK), /* write 4 */
152 0, /* write 5 */
153 0, 0, 0, /* write 6, 7, 8 */
154 (MIE | DLC | NV), /* write 9 */
155 (NRZ), /* write 10 */
156 (TCBR | RCBR), /* write 11 */
157 0, 0, /* BRG time constant, write 12 + 13 */
158 (BRSRC | BRENABL), /* write 14 */
159 0 /* write 15 */
162 static struct tty_driver *serial_driver;
164 /* serial subtype definitions */
165 #define SERIAL_TYPE_NORMAL 1
167 /* number of characters left in xmit buffer before we ask for more */
168 #define WAKEUP_CHARS 256
171 * Debugging.
173 #undef SERIAL_DEBUG_OPEN
174 #undef SERIAL_DEBUG_FLOW
175 #undef SERIAL_DEBUG_THROTTLE
176 #undef SERIAL_PARANOIA_CHECK
178 #undef ZS_DEBUG_REGS
180 #ifdef SERIAL_DEBUG_THROTTLE
181 #define _tty_name(tty,buf) tty_name(tty,buf)
182 #endif
184 #define RS_STROBE_TIME 10
185 #define RS_ISR_PASS_LIMIT 256
187 static void probe_sccs(void);
188 static void change_speed(struct dec_serial *info);
189 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
191 static inline int serial_paranoia_check(struct dec_serial *info,
192 char *name, const char *routine)
194 #ifdef SERIAL_PARANOIA_CHECK
195 static const char *badmagic =
196 "Warning: bad magic number for serial struct %s in %s\n";
197 static const char *badinfo =
198 "Warning: null mac_serial for %s in %s\n";
200 if (!info) {
201 printk(badinfo, name, routine);
202 return 1;
204 if (info->magic != SERIAL_MAGIC) {
205 printk(badmagic, name, routine);
206 return 1;
208 #endif
209 return 0;
213 * This is used to figure out the divisor speeds and the timeouts
215 static int baud_table[] = {
216 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
217 9600, 19200, 38400, 57600, 115200, 0 };
220 * Reading and writing Z8530 registers.
222 static inline unsigned char read_zsreg(struct dec_zschannel *channel,
223 unsigned char reg)
225 unsigned char retval;
227 if (reg != 0) {
228 *channel->control = reg & 0xf;
229 fast_iob(); RECOVERY_DELAY;
231 retval = *channel->control;
232 RECOVERY_DELAY;
233 return retval;
236 static inline void write_zsreg(struct dec_zschannel *channel,
237 unsigned char reg, unsigned char value)
239 if (reg != 0) {
240 *channel->control = reg & 0xf;
241 fast_iob(); RECOVERY_DELAY;
243 *channel->control = value;
244 fast_iob(); RECOVERY_DELAY;
245 return;
248 static inline unsigned char read_zsdata(struct dec_zschannel *channel)
250 unsigned char retval;
252 retval = *channel->data;
253 RECOVERY_DELAY;
254 return retval;
257 static inline void write_zsdata(struct dec_zschannel *channel,
258 unsigned char value)
260 *channel->data = value;
261 fast_iob(); RECOVERY_DELAY;
262 return;
265 static inline void load_zsregs(struct dec_zschannel *channel,
266 unsigned char *regs)
268 /* ZS_CLEARERR(channel);
269 ZS_CLEARFIFO(channel); */
270 /* Load 'em up */
271 write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
272 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
273 write_zsreg(channel, R4, regs[R4]);
274 write_zsreg(channel, R9, regs[R9]);
275 write_zsreg(channel, R1, regs[R1]);
276 write_zsreg(channel, R2, regs[R2]);
277 write_zsreg(channel, R10, regs[R10]);
278 write_zsreg(channel, R11, regs[R11]);
279 write_zsreg(channel, R12, regs[R12]);
280 write_zsreg(channel, R13, regs[R13]);
281 write_zsreg(channel, R14, regs[R14]);
282 write_zsreg(channel, R15, regs[R15]);
283 write_zsreg(channel, R3, regs[R3]);
284 write_zsreg(channel, R5, regs[R5]);
285 return;
288 /* Sets or clears DTR/RTS on the requested line */
289 static inline void zs_rtsdtr(struct dec_serial *info, int which, int set)
291 unsigned long flags;
293 spin_lock_irqsave(&zs_lock, flags);
294 if (info->zs_channel != info->zs_chan_a) {
295 if (set) {
296 info->zs_chan_a->curregs[5] |= (which & (RTS | DTR));
297 } else {
298 info->zs_chan_a->curregs[5] &= ~(which & (RTS | DTR));
300 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
302 spin_unlock_irqrestore(&zs_lock, flags);
305 /* Utility routines for the Zilog */
306 static inline int get_zsbaud(struct dec_serial *ss)
308 struct dec_zschannel *channel = ss->zs_channel;
309 int brg;
311 /* The baud rate is split up between two 8-bit registers in
312 * what is termed 'BRG time constant' format in my docs for
313 * the chip, it is a function of the clk rate the chip is
314 * receiving which happens to be constant.
316 brg = (read_zsreg(channel, 13) << 8);
317 brg |= read_zsreg(channel, 12);
318 return BRG_TO_BPS(brg, (zs_parms->clock/(ss->clk_divisor)));
321 /* On receive, this clears errors and the receiver interrupts */
322 static inline void rs_recv_clear(struct dec_zschannel *zsc)
324 write_zsreg(zsc, 0, ERR_RES);
325 write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
329 * ----------------------------------------------------------------------
331 * Here starts the interrupt handling routines. All of the following
332 * subroutines are declared as inline and are folded into
333 * rs_interrupt(). They were separated out for readability's sake.
335 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
336 * -----------------------------------------------------------------------
340 * This routine is used by the interrupt handler to schedule
341 * processing in the software interrupt portion of the driver.
343 static void rs_sched_event(struct dec_serial *info, int event)
345 info->event |= 1 << event;
346 tasklet_schedule(&info->tlet);
349 static void receive_chars(struct dec_serial *info)
351 struct tty_struct *tty = info->tty;
352 unsigned char ch, stat, flag;
354 while ((read_zsreg(info->zs_channel, R0) & Rx_CH_AV) != 0) {
356 stat = read_zsreg(info->zs_channel, R1);
357 ch = read_zsdata(info->zs_channel);
359 if (!tty && (!info->hook || !info->hook->rx_char))
360 continue;
362 flag = TTY_NORMAL;
363 if (info->tty_break) {
364 info->tty_break = 0;
365 flag = TTY_BREAK;
366 if (info->flags & ZILOG_SAK)
367 do_SAK(tty);
368 /* Ignore the null char got when BREAK is removed. */
369 if (ch == 0)
370 continue;
371 } else {
372 if (stat & Rx_OVR) {
373 flag = TTY_OVERRUN;
374 } else if (stat & FRM_ERR) {
375 flag = TTY_FRAME;
376 } else if (stat & PAR_ERR) {
377 flag = TTY_PARITY;
379 if (flag != TTY_NORMAL)
380 /* reset the error indication */
381 write_zsreg(info->zs_channel, R0, ERR_RES);
384 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
385 !defined(MODULE)
386 if (break_pressed && info->line == zs_console.index) {
387 /* Ignore the null char got when BREAK is removed. */
388 if (ch == 0)
389 continue;
390 if (time_before(jiffies, break_pressed + HZ * 5)) {
391 handle_sysrq(ch, NULL);
392 break_pressed = 0;
393 continue;
395 break_pressed = 0;
397 #endif
399 if (info->hook && info->hook->rx_char) {
400 (*info->hook->rx_char)(ch, flag);
401 return;
404 tty_insert_flip_char(tty, ch, flag);
406 if (tty)
407 tty_flip_buffer_push(tty);
410 static void transmit_chars(struct dec_serial *info)
412 if ((read_zsreg(info->zs_channel, R0) & Tx_BUF_EMP) == 0)
413 return;
414 info->tx_active = 0;
416 if (info->x_char) {
417 /* Send next char */
418 write_zsdata(info->zs_channel, info->x_char);
419 info->x_char = 0;
420 info->tx_active = 1;
421 return;
424 if ((info->xmit_cnt <= 0) || (info->tty && info->tty->stopped)
425 || info->tx_stopped) {
426 write_zsreg(info->zs_channel, R0, RES_Tx_P);
427 return;
429 /* Send char */
430 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
431 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
432 info->xmit_cnt--;
433 info->tx_active = 1;
435 if (info->xmit_cnt < WAKEUP_CHARS)
436 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
439 static void status_handle(struct dec_serial *info)
441 unsigned char stat;
443 /* Get status from Read Register 0 */
444 stat = read_zsreg(info->zs_channel, R0);
446 if ((stat & BRK_ABRT) && !(info->read_reg_zero & BRK_ABRT)) {
447 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
448 !defined(MODULE)
449 if (info->line == zs_console.index) {
450 if (!break_pressed)
451 break_pressed = jiffies;
452 } else
453 #endif
454 info->tty_break = 1;
457 if (info->zs_channel != info->zs_chan_a) {
459 /* Check for DCD transitions */
460 if (info->tty && !C_CLOCAL(info->tty) &&
461 ((stat ^ info->read_reg_zero) & DCD) != 0 ) {
462 if (stat & DCD) {
463 wake_up_interruptible(&info->open_wait);
464 } else {
465 tty_hangup(info->tty);
469 /* Check for CTS transitions */
470 if (info->tty && C_CRTSCTS(info->tty)) {
471 if ((stat & CTS) != 0) {
472 if (info->tx_stopped) {
473 info->tx_stopped = 0;
474 if (!info->tx_active)
475 transmit_chars(info);
477 } else {
478 info->tx_stopped = 1;
484 /* Clear status condition... */
485 write_zsreg(info->zs_channel, R0, RES_EXT_INT);
486 info->read_reg_zero = stat;
490 * This is the serial driver's generic interrupt routine
492 static irqreturn_t rs_interrupt(int irq, void *dev_id)
494 struct dec_serial *info = (struct dec_serial *) dev_id;
495 irqreturn_t status = IRQ_NONE;
496 unsigned char zs_intreg;
497 int shift;
499 /* NOTE: The read register 3, which holds the irq status,
500 * does so for both channels on each chip. Although
501 * the status value itself must be read from the A
502 * channel and is only valid when read from channel A.
503 * Yes... broken hardware...
505 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
507 if (info->zs_chan_a == info->zs_channel)
508 shift = 3; /* Channel A */
509 else
510 shift = 0; /* Channel B */
512 for (;;) {
513 zs_intreg = read_zsreg(info->zs_chan_a, R3) >> shift;
514 if ((zs_intreg & CHAN_IRQMASK) == 0)
515 break;
517 status = IRQ_HANDLED;
519 if (zs_intreg & CHBRxIP) {
520 receive_chars(info);
522 if (zs_intreg & CHBTxIP) {
523 transmit_chars(info);
525 if (zs_intreg & CHBEXT) {
526 status_handle(info);
530 /* Why do we need this ? */
531 write_zsreg(info->zs_channel, 0, RES_H_IUS);
533 return status;
536 #ifdef ZS_DEBUG_REGS
537 void zs_dump (void) {
538 int i, j;
539 for (i = 0; i < zs_channels_found; i++) {
540 struct dec_zschannel *ch = &zs_channels[i];
541 if ((long)ch->control == UNI_IO_BASE+UNI_SCC1A_CTRL) {
542 for (j = 0; j < 15; j++) {
543 printk("W%d = 0x%x\t",
544 j, (int)ch->curregs[j]);
546 for (j = 0; j < 15; j++) {
547 printk("R%d = 0x%x\t",
548 j, (int)read_zsreg(ch,j));
550 printk("\n\n");
554 #endif
557 * -------------------------------------------------------------------
558 * Here ends the serial interrupt routines.
559 * -------------------------------------------------------------------
563 * ------------------------------------------------------------
564 * rs_stop() and rs_start()
566 * This routines are called before setting or resetting tty->stopped.
567 * ------------------------------------------------------------
569 static void rs_stop(struct tty_struct *tty)
571 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
572 unsigned long flags;
574 if (serial_paranoia_check(info, tty->name, "rs_stop"))
575 return;
577 #if 1
578 spin_lock_irqsave(&zs_lock, flags);
579 if (info->zs_channel->curregs[5] & TxENAB) {
580 info->zs_channel->curregs[5] &= ~TxENAB;
581 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
583 spin_unlock_irqrestore(&zs_lock, flags);
584 #endif
587 static void rs_start(struct tty_struct *tty)
589 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
590 unsigned long flags;
592 if (serial_paranoia_check(info, tty->name, "rs_start"))
593 return;
595 spin_lock_irqsave(&zs_lock, flags);
596 #if 1
597 if (info->xmit_cnt && info->xmit_buf && !(info->zs_channel->curregs[5] & TxENAB)) {
598 info->zs_channel->curregs[5] |= TxENAB;
599 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
601 #else
602 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
603 transmit_chars(info);
605 #endif
606 spin_unlock_irqrestore(&zs_lock, flags);
610 * This routine is used to handle the "bottom half" processing for the
611 * serial driver, known also the "software interrupt" processing.
612 * This processing is done at the kernel interrupt level, after the
613 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
614 * is where time-consuming activities which can not be done in the
615 * interrupt driver proper are done; the interrupt driver schedules
616 * them using rs_sched_event(), and they get done here.
619 static void do_softint(unsigned long private_)
621 struct dec_serial *info = (struct dec_serial *) private_;
622 struct tty_struct *tty;
624 tty = info->tty;
625 if (!tty)
626 return;
628 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
629 tty_wakeup(tty);
632 static int zs_startup(struct dec_serial * info)
634 unsigned long flags;
636 if (info->flags & ZILOG_INITIALIZED)
637 return 0;
639 if (!info->xmit_buf) {
640 info->xmit_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
641 if (!info->xmit_buf)
642 return -ENOMEM;
645 spin_lock_irqsave(&zs_lock, flags);
647 #ifdef SERIAL_DEBUG_OPEN
648 printk("starting up ttyS%d (irq %d)...", info->line, info->irq);
649 #endif
652 * Clear the receive FIFO.
654 ZS_CLEARFIFO(info->zs_channel);
655 info->xmit_fifo_size = 1;
658 * Clear the interrupt registers.
660 write_zsreg(info->zs_channel, R0, ERR_RES);
661 write_zsreg(info->zs_channel, R0, RES_H_IUS);
664 * Set the speed of the serial port
666 change_speed(info);
669 * Turn on RTS and DTR.
671 zs_rtsdtr(info, RTS | DTR, 1);
674 * Finally, enable sequencing and interrupts
676 info->zs_channel->curregs[R1] &= ~RxINT_MASK;
677 info->zs_channel->curregs[R1] |= (RxINT_ALL | TxINT_ENAB |
678 EXT_INT_ENAB);
679 info->zs_channel->curregs[R3] |= RxENABLE;
680 info->zs_channel->curregs[R5] |= TxENAB;
681 info->zs_channel->curregs[R15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
682 write_zsreg(info->zs_channel, R1, info->zs_channel->curregs[R1]);
683 write_zsreg(info->zs_channel, R3, info->zs_channel->curregs[R3]);
684 write_zsreg(info->zs_channel, R5, info->zs_channel->curregs[R5]);
685 write_zsreg(info->zs_channel, R15, info->zs_channel->curregs[R15]);
688 * And clear the interrupt registers again for luck.
690 write_zsreg(info->zs_channel, R0, ERR_RES);
691 write_zsreg(info->zs_channel, R0, RES_H_IUS);
693 /* Save the current value of RR0 */
694 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
696 if (info->tty)
697 clear_bit(TTY_IO_ERROR, &info->tty->flags);
698 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
700 info->flags |= ZILOG_INITIALIZED;
701 spin_unlock_irqrestore(&zs_lock, flags);
702 return 0;
706 * This routine will shutdown a serial port; interrupts are disabled, and
707 * DTR is dropped if the hangup on close termio flag is on.
709 static void shutdown(struct dec_serial * info)
711 unsigned long flags;
713 if (!(info->flags & ZILOG_INITIALIZED))
714 return;
716 #ifdef SERIAL_DEBUG_OPEN
717 printk("Shutting down serial port %d (irq %d)....", info->line,
718 info->irq);
719 #endif
721 spin_lock_irqsave(&zs_lock, flags);
723 if (info->xmit_buf) {
724 free_page((unsigned long) info->xmit_buf);
725 info->xmit_buf = 0;
728 info->zs_channel->curregs[1] = 0;
729 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]); /* no interrupts */
731 info->zs_channel->curregs[3] &= ~RxENABLE;
732 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
734 info->zs_channel->curregs[5] &= ~TxENAB;
735 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
736 if (!info->tty || C_HUPCL(info->tty)) {
737 zs_rtsdtr(info, RTS | DTR, 0);
740 if (info->tty)
741 set_bit(TTY_IO_ERROR, &info->tty->flags);
743 info->flags &= ~ZILOG_INITIALIZED;
744 spin_unlock_irqrestore(&zs_lock, flags);
748 * This routine is called to set the UART divisor registers to match
749 * the specified baud rate for a serial port.
751 static void change_speed(struct dec_serial *info)
753 unsigned cflag;
754 int i;
755 int brg, bits;
756 unsigned long flags;
758 if (!info->hook) {
759 if (!info->tty || !info->tty->termios)
760 return;
761 cflag = info->tty->termios->c_cflag;
762 if (!info->port)
763 return;
764 } else {
765 cflag = info->hook->cflags;
768 i = cflag & CBAUD;
769 if (i & CBAUDEX) {
770 i &= ~CBAUDEX;
771 if (i < 1 || i > 2) {
772 if (!info->hook)
773 info->tty->termios->c_cflag &= ~CBAUDEX;
774 else
775 info->hook->cflags &= ~CBAUDEX;
776 } else
777 i += 15;
780 spin_lock_irqsave(&zs_lock, flags);
781 info->zs_baud = baud_table[i];
782 if (info->zs_baud) {
783 brg = BPS_TO_BRG(info->zs_baud, zs_parms->clock/info->clk_divisor);
784 info->zs_channel->curregs[12] = (brg & 255);
785 info->zs_channel->curregs[13] = ((brg >> 8) & 255);
786 zs_rtsdtr(info, DTR, 1);
787 } else {
788 zs_rtsdtr(info, RTS | DTR, 0);
789 return;
792 /* byte size and parity */
793 info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
794 info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
795 switch (cflag & CSIZE) {
796 case CS5:
797 bits = 7;
798 info->zs_channel->curregs[3] |= Rx5;
799 info->zs_channel->curregs[5] |= Tx5;
800 break;
801 case CS6:
802 bits = 8;
803 info->zs_channel->curregs[3] |= Rx6;
804 info->zs_channel->curregs[5] |= Tx6;
805 break;
806 case CS7:
807 bits = 9;
808 info->zs_channel->curregs[3] |= Rx7;
809 info->zs_channel->curregs[5] |= Tx7;
810 break;
811 case CS8:
812 default: /* defaults to 8 bits */
813 bits = 10;
814 info->zs_channel->curregs[3] |= Rx8;
815 info->zs_channel->curregs[5] |= Tx8;
816 break;
819 info->timeout = ((info->xmit_fifo_size*HZ*bits) / info->zs_baud);
820 info->timeout += HZ/50; /* Add .02 seconds of slop */
822 info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
823 if (cflag & CSTOPB) {
824 info->zs_channel->curregs[4] |= SB2;
825 } else {
826 info->zs_channel->curregs[4] |= SB1;
828 if (cflag & PARENB) {
829 info->zs_channel->curregs[4] |= PAR_ENA;
831 if (!(cflag & PARODD)) {
832 info->zs_channel->curregs[4] |= PAR_EVEN;
835 if (!(cflag & CLOCAL)) {
836 if (!(info->zs_channel->curregs[15] & DCDIE))
837 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
838 info->zs_channel->curregs[15] |= DCDIE;
839 } else
840 info->zs_channel->curregs[15] &= ~DCDIE;
841 if (cflag & CRTSCTS) {
842 info->zs_channel->curregs[15] |= CTSIE;
843 if ((read_zsreg(info->zs_channel, 0) & CTS) == 0)
844 info->tx_stopped = 1;
845 } else {
846 info->zs_channel->curregs[15] &= ~CTSIE;
847 info->tx_stopped = 0;
850 /* Load up the new values */
851 load_zsregs(info->zs_channel, info->zs_channel->curregs);
853 spin_unlock_irqrestore(&zs_lock, flags);
856 static void rs_flush_chars(struct tty_struct *tty)
858 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
859 unsigned long flags;
861 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
862 return;
864 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
865 !info->xmit_buf)
866 return;
868 /* Enable transmitter */
869 spin_lock_irqsave(&zs_lock, flags);
870 transmit_chars(info);
871 spin_unlock_irqrestore(&zs_lock, flags);
874 static int rs_write(struct tty_struct * tty,
875 const unsigned char *buf, int count)
877 int c, total = 0;
878 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
879 unsigned long flags;
881 if (serial_paranoia_check(info, tty->name, "rs_write"))
882 return 0;
884 if (!tty || !info->xmit_buf)
885 return 0;
887 while (1) {
888 spin_lock_irqsave(&zs_lock, flags);
889 c = min(count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
890 SERIAL_XMIT_SIZE - info->xmit_head));
891 if (c <= 0)
892 break;
894 memcpy(info->xmit_buf + info->xmit_head, buf, c);
895 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
896 info->xmit_cnt += c;
897 spin_unlock_irqrestore(&zs_lock, flags);
898 buf += c;
899 count -= c;
900 total += c;
903 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
904 && !info->tx_active)
905 transmit_chars(info);
906 spin_unlock_irqrestore(&zs_lock, flags);
907 return total;
910 static int rs_write_room(struct tty_struct *tty)
912 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
913 int ret;
915 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
916 return 0;
917 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
918 if (ret < 0)
919 ret = 0;
920 return ret;
923 static int rs_chars_in_buffer(struct tty_struct *tty)
925 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
927 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
928 return 0;
929 return info->xmit_cnt;
932 static void rs_flush_buffer(struct tty_struct *tty)
934 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
936 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
937 return;
938 spin_lock_irq(&zs_lock);
939 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
940 spin_unlock_irq(&zs_lock);
941 tty_wakeup(tty);
945 * ------------------------------------------------------------
946 * rs_throttle()
948 * This routine is called by the upper-layer tty layer to signal that
949 * incoming characters should be throttled.
950 * ------------------------------------------------------------
952 static void rs_throttle(struct tty_struct * tty)
954 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
955 unsigned long flags;
957 #ifdef SERIAL_DEBUG_THROTTLE
958 char buf[64];
960 printk("throttle %s: %d....\n", _tty_name(tty, buf),
961 tty->ldisc.chars_in_buffer(tty));
962 #endif
964 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
965 return;
967 if (I_IXOFF(tty)) {
968 spin_lock_irqsave(&zs_lock, flags);
969 info->x_char = STOP_CHAR(tty);
970 if (!info->tx_active)
971 transmit_chars(info);
972 spin_unlock_irqrestore(&zs_lock, flags);
975 if (C_CRTSCTS(tty)) {
976 zs_rtsdtr(info, RTS, 0);
980 static void rs_unthrottle(struct tty_struct * tty)
982 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
983 unsigned long flags;
985 #ifdef SERIAL_DEBUG_THROTTLE
986 char buf[64];
988 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
989 tty->ldisc.chars_in_buffer(tty));
990 #endif
992 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
993 return;
995 if (I_IXOFF(tty)) {
996 spin_lock_irqsave(&zs_lock, flags);
997 if (info->x_char)
998 info->x_char = 0;
999 else {
1000 info->x_char = START_CHAR(tty);
1001 if (!info->tx_active)
1002 transmit_chars(info);
1004 spin_unlock_irqrestore(&zs_lock, flags);
1007 if (C_CRTSCTS(tty)) {
1008 zs_rtsdtr(info, RTS, 1);
1013 * ------------------------------------------------------------
1014 * rs_ioctl() and friends
1015 * ------------------------------------------------------------
1018 static int get_serial_info(struct dec_serial * info,
1019 struct serial_struct * retinfo)
1021 struct serial_struct tmp;
1023 if (!retinfo)
1024 return -EFAULT;
1025 memset(&tmp, 0, sizeof(tmp));
1026 tmp.type = info->type;
1027 tmp.line = info->line;
1028 tmp.port = info->port;
1029 tmp.irq = info->irq;
1030 tmp.flags = info->flags;
1031 tmp.baud_base = info->baud_base;
1032 tmp.close_delay = info->close_delay;
1033 tmp.closing_wait = info->closing_wait;
1034 tmp.custom_divisor = info->custom_divisor;
1035 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
1038 static int set_serial_info(struct dec_serial * info,
1039 struct serial_struct * new_info)
1041 struct serial_struct new_serial;
1042 struct dec_serial old_info;
1043 int retval = 0;
1045 if (!new_info)
1046 return -EFAULT;
1047 copy_from_user(&new_serial,new_info,sizeof(new_serial));
1048 old_info = *info;
1050 if (!capable(CAP_SYS_ADMIN)) {
1051 if ((new_serial.baud_base != info->baud_base) ||
1052 (new_serial.type != info->type) ||
1053 (new_serial.close_delay != info->close_delay) ||
1054 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1055 (info->flags & ~ZILOG_USR_MASK)))
1056 return -EPERM;
1057 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1058 (new_serial.flags & ZILOG_USR_MASK));
1059 info->custom_divisor = new_serial.custom_divisor;
1060 goto check_and_exit;
1063 if (info->count > 1)
1064 return -EBUSY;
1067 * OK, past this point, all the error checking has been done.
1068 * At this point, we start making changes.....
1071 info->baud_base = new_serial.baud_base;
1072 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1073 (new_serial.flags & ZILOG_FLAGS));
1074 info->type = new_serial.type;
1075 info->close_delay = new_serial.close_delay;
1076 info->closing_wait = new_serial.closing_wait;
1078 check_and_exit:
1079 retval = zs_startup(info);
1080 return retval;
1084 * get_lsr_info - get line status register info
1086 * Purpose: Let user call ioctl() to get info when the UART physically
1087 * is emptied. On bus types like RS485, the transmitter must
1088 * release the bus after transmitting. This must be done when
1089 * the transmit shift register is empty, not be done when the
1090 * transmit holding register is empty. This functionality
1091 * allows an RS485 driver to be written in user space.
1093 static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1095 unsigned char status;
1097 spin_lock(&zs_lock);
1098 status = read_zsreg(info->zs_channel, 0);
1099 spin_unlock_irq(&zs_lock);
1100 put_user(status,value);
1101 return 0;
1104 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1106 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1107 unsigned char control, status_a, status_b;
1108 unsigned int result;
1110 if (info->hook)
1111 return -ENODEV;
1113 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1114 return -ENODEV;
1116 if (tty->flags & (1 << TTY_IO_ERROR))
1117 return -EIO;
1119 if (info->zs_channel == info->zs_chan_a)
1120 result = 0;
1121 else {
1122 spin_lock(&zs_lock);
1123 control = info->zs_chan_a->curregs[5];
1124 status_a = read_zsreg(info->zs_chan_a, 0);
1125 status_b = read_zsreg(info->zs_channel, 0);
1126 spin_unlock_irq(&zs_lock);
1127 result = ((control & RTS) ? TIOCM_RTS: 0)
1128 | ((control & DTR) ? TIOCM_DTR: 0)
1129 | ((status_b & DCD) ? TIOCM_CAR: 0)
1130 | ((status_a & DCD) ? TIOCM_RNG: 0)
1131 | ((status_a & SYNC_HUNT) ? TIOCM_DSR: 0)
1132 | ((status_b & CTS) ? TIOCM_CTS: 0);
1134 return result;
1137 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1138 unsigned int set, unsigned int clear)
1140 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1142 if (info->hook)
1143 return -ENODEV;
1145 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1146 return -ENODEV;
1148 if (tty->flags & (1 << TTY_IO_ERROR))
1149 return -EIO;
1151 if (info->zs_channel == info->zs_chan_a)
1152 return 0;
1154 spin_lock(&zs_lock);
1155 if (set & TIOCM_RTS)
1156 info->zs_chan_a->curregs[5] |= RTS;
1157 if (set & TIOCM_DTR)
1158 info->zs_chan_a->curregs[5] |= DTR;
1159 if (clear & TIOCM_RTS)
1160 info->zs_chan_a->curregs[5] &= ~RTS;
1161 if (clear & TIOCM_DTR)
1162 info->zs_chan_a->curregs[5] &= ~DTR;
1163 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1164 spin_unlock_irq(&zs_lock);
1165 return 0;
1169 * rs_break - turn transmit break condition on/off
1171 static void rs_break(struct tty_struct *tty, int break_state)
1173 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1174 unsigned long flags;
1176 if (serial_paranoia_check(info, tty->name, "rs_break"))
1177 return;
1178 if (!info->port)
1179 return;
1181 spin_lock_irqsave(&zs_lock, flags);
1182 if (break_state == -1)
1183 info->zs_channel->curregs[5] |= SND_BRK;
1184 else
1185 info->zs_channel->curregs[5] &= ~SND_BRK;
1186 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1187 spin_unlock_irqrestore(&zs_lock, flags);
1190 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1191 unsigned int cmd, unsigned long arg)
1193 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1195 if (info->hook)
1196 return -ENODEV;
1198 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1199 return -ENODEV;
1201 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1202 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1203 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1204 if (tty->flags & (1 << TTY_IO_ERROR))
1205 return -EIO;
1208 switch (cmd) {
1209 case TIOCGSERIAL:
1210 if (!access_ok(VERIFY_WRITE, (void *)arg,
1211 sizeof(struct serial_struct)))
1212 return -EFAULT;
1213 return get_serial_info(info, (struct serial_struct *)arg);
1215 case TIOCSSERIAL:
1216 return set_serial_info(info, (struct serial_struct *)arg);
1218 case TIOCSERGETLSR: /* Get line status register */
1219 if (!access_ok(VERIFY_WRITE, (void *)arg,
1220 sizeof(unsigned int)))
1221 return -EFAULT;
1222 return get_lsr_info(info, (unsigned int *)arg);
1224 case TIOCSERGSTRUCT:
1225 if (!access_ok(VERIFY_WRITE, (void *)arg,
1226 sizeof(struct dec_serial)))
1227 return -EFAULT;
1228 copy_from_user((struct dec_serial *)arg, info,
1229 sizeof(struct dec_serial));
1230 return 0;
1232 default:
1233 return -ENOIOCTLCMD;
1235 return 0;
1238 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1240 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1241 int was_stopped;
1243 if (tty->termios->c_cflag == old_termios->c_cflag)
1244 return;
1245 was_stopped = info->tx_stopped;
1247 change_speed(info);
1249 if (was_stopped && !info->tx_stopped)
1250 rs_start(tty);
1254 * ------------------------------------------------------------
1255 * rs_close()
1257 * This routine is called when the serial port gets closed.
1258 * Wait for the last remaining data to be sent.
1259 * ------------------------------------------------------------
1261 static void rs_close(struct tty_struct *tty, struct file * filp)
1263 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1264 unsigned long flags;
1266 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1267 return;
1269 spin_lock_irqsave(&zs_lock, flags);
1271 if (tty_hung_up_p(filp)) {
1272 spin_unlock_irqrestore(&zs_lock, flags);
1273 return;
1276 #ifdef SERIAL_DEBUG_OPEN
1277 printk("rs_close ttyS%d, count = %d\n", info->line, info->count);
1278 #endif
1279 if ((tty->count == 1) && (info->count != 1)) {
1281 * Uh, oh. tty->count is 1, which means that the tty
1282 * structure will be freed. Info->count should always
1283 * be one in these conditions. If it's greater than
1284 * one, we've got real problems, since it means the
1285 * serial port won't be shutdown.
1287 printk("rs_close: bad serial port count; tty->count is 1, "
1288 "info->count is %d\n", info->count);
1289 info->count = 1;
1291 if (--info->count < 0) {
1292 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1293 info->line, info->count);
1294 info->count = 0;
1296 if (info->count) {
1297 spin_unlock_irqrestore(&zs_lock, flags);
1298 return;
1300 info->flags |= ZILOG_CLOSING;
1302 * Now we wait for the transmit buffer to clear; and we notify
1303 * the line discipline to only process XON/XOFF characters.
1305 tty->closing = 1;
1306 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1307 tty_wait_until_sent(tty, info->closing_wait);
1309 * At this point we stop accepting input. To do this, we
1310 * disable the receiver and receive interrupts.
1312 info->zs_channel->curregs[3] &= ~RxENABLE;
1313 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1314 info->zs_channel->curregs[1] = 0; /* disable any rx ints */
1315 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1316 ZS_CLEARFIFO(info->zs_channel);
1317 if (info->flags & ZILOG_INITIALIZED) {
1319 * Before we drop DTR, make sure the SCC transmitter
1320 * has completely drained.
1322 rs_wait_until_sent(tty, info->timeout);
1325 shutdown(info);
1326 if (tty->driver->flush_buffer)
1327 tty->driver->flush_buffer(tty);
1328 tty_ldisc_flush(tty);
1329 tty->closing = 0;
1330 info->event = 0;
1331 info->tty = 0;
1332 if (info->blocked_open) {
1333 if (info->close_delay) {
1334 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1336 wake_up_interruptible(&info->open_wait);
1338 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CLOSING);
1339 wake_up_interruptible(&info->close_wait);
1340 spin_unlock_irqrestore(&zs_lock, flags);
1344 * rs_wait_until_sent() --- wait until the transmitter is empty
1346 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1348 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1349 unsigned long orig_jiffies;
1350 int char_time;
1352 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1353 return;
1355 orig_jiffies = jiffies;
1357 * Set the check interval to be 1/5 of the estimated time to
1358 * send a single character, and make it at least 1. The check
1359 * interval should also be less than the timeout.
1361 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1362 char_time = char_time / 5;
1363 if (char_time == 0)
1364 char_time = 1;
1365 if (timeout)
1366 char_time = min(char_time, timeout);
1367 while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
1368 msleep_interruptible(jiffies_to_msecs(char_time));
1369 if (signal_pending(current))
1370 break;
1371 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1372 break;
1374 current->state = TASK_RUNNING;
1378 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1380 static void rs_hangup(struct tty_struct *tty)
1382 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1384 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1385 return;
1387 rs_flush_buffer(tty);
1388 shutdown(info);
1389 info->event = 0;
1390 info->count = 0;
1391 info->flags &= ~ZILOG_NORMAL_ACTIVE;
1392 info->tty = 0;
1393 wake_up_interruptible(&info->open_wait);
1397 * ------------------------------------------------------------
1398 * rs_open() and friends
1399 * ------------------------------------------------------------
1401 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1402 struct dec_serial *info)
1404 DECLARE_WAITQUEUE(wait, current);
1405 int retval;
1406 int do_clocal = 0;
1409 * If the device is in the middle of being closed, then block
1410 * until it's done, and then try again.
1412 if (info->flags & ZILOG_CLOSING) {
1413 interruptible_sleep_on(&info->close_wait);
1414 #ifdef SERIAL_DO_RESTART
1415 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1416 -EAGAIN : -ERESTARTSYS);
1417 #else
1418 return -EAGAIN;
1419 #endif
1423 * If non-blocking mode is set, or the port is not enabled,
1424 * then make the check up front and then exit.
1426 if ((filp->f_flags & O_NONBLOCK) ||
1427 (tty->flags & (1 << TTY_IO_ERROR))) {
1428 info->flags |= ZILOG_NORMAL_ACTIVE;
1429 return 0;
1432 if (tty->termios->c_cflag & CLOCAL)
1433 do_clocal = 1;
1436 * Block waiting for the carrier detect and the line to become
1437 * free (i.e., not in use by the callout). While we are in
1438 * this loop, info->count is dropped by one, so that
1439 * rs_close() knows when to free things. We restore it upon
1440 * exit, either normal or abnormal.
1442 retval = 0;
1443 add_wait_queue(&info->open_wait, &wait);
1444 #ifdef SERIAL_DEBUG_OPEN
1445 printk("block_til_ready before block: ttyS%d, count = %d\n",
1446 info->line, info->count);
1447 #endif
1448 spin_lock(&zs_lock);
1449 if (!tty_hung_up_p(filp))
1450 info->count--;
1451 spin_unlock_irq(&zs_lock);
1452 info->blocked_open++;
1453 while (1) {
1454 spin_lock(&zs_lock);
1455 if (tty->termios->c_cflag & CBAUD)
1456 zs_rtsdtr(info, RTS | DTR, 1);
1457 spin_unlock_irq(&zs_lock);
1458 set_current_state(TASK_INTERRUPTIBLE);
1459 if (tty_hung_up_p(filp) ||
1460 !(info->flags & ZILOG_INITIALIZED)) {
1461 #ifdef SERIAL_DO_RESTART
1462 if (info->flags & ZILOG_HUP_NOTIFY)
1463 retval = -EAGAIN;
1464 else
1465 retval = -ERESTARTSYS;
1466 #else
1467 retval = -EAGAIN;
1468 #endif
1469 break;
1471 if (!(info->flags & ZILOG_CLOSING) &&
1472 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1473 break;
1474 if (signal_pending(current)) {
1475 retval = -ERESTARTSYS;
1476 break;
1478 #ifdef SERIAL_DEBUG_OPEN
1479 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1480 info->line, info->count);
1481 #endif
1482 schedule();
1484 current->state = TASK_RUNNING;
1485 remove_wait_queue(&info->open_wait, &wait);
1486 if (!tty_hung_up_p(filp))
1487 info->count++;
1488 info->blocked_open--;
1489 #ifdef SERIAL_DEBUG_OPEN
1490 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1491 info->line, info->count);
1492 #endif
1493 if (retval)
1494 return retval;
1495 info->flags |= ZILOG_NORMAL_ACTIVE;
1496 return 0;
1500 * This routine is called whenever a serial port is opened. It
1501 * enables interrupts for a serial port, linking in its ZILOG structure into
1502 * the IRQ chain. It also performs the serial-specific
1503 * initialization for the tty structure.
1505 static int rs_open(struct tty_struct *tty, struct file * filp)
1507 struct dec_serial *info;
1508 int retval, line;
1510 line = tty->index;
1511 if ((line < 0) || (line >= zs_channels_found))
1512 return -ENODEV;
1513 info = zs_soft + line;
1515 if (info->hook)
1516 return -ENODEV;
1518 if (serial_paranoia_check(info, tty->name, "rs_open"))
1519 return -ENODEV;
1520 #ifdef SERIAL_DEBUG_OPEN
1521 printk("rs_open %s, count = %d\n", tty->name, info->count);
1522 #endif
1524 info->count++;
1525 tty->driver_data = info;
1526 info->tty = tty;
1529 * If the port is the middle of closing, bail out now
1531 if (tty_hung_up_p(filp) ||
1532 (info->flags & ZILOG_CLOSING)) {
1533 if (info->flags & ZILOG_CLOSING)
1534 interruptible_sleep_on(&info->close_wait);
1535 #ifdef SERIAL_DO_RESTART
1536 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1537 -EAGAIN : -ERESTARTSYS);
1538 #else
1539 return -EAGAIN;
1540 #endif
1544 * Start up serial port
1546 retval = zs_startup(info);
1547 if (retval)
1548 return retval;
1550 retval = block_til_ready(tty, filp, info);
1551 if (retval) {
1552 #ifdef SERIAL_DEBUG_OPEN
1553 printk("rs_open returning after block_til_ready with %d\n",
1554 retval);
1555 #endif
1556 return retval;
1559 #ifdef CONFIG_SERIAL_DEC_CONSOLE
1560 if (zs_console.cflag && zs_console.index == line) {
1561 tty->termios->c_cflag = zs_console.cflag;
1562 zs_console.cflag = 0;
1563 change_speed(info);
1565 #endif
1567 #ifdef SERIAL_DEBUG_OPEN
1568 printk("rs_open %s successful...", tty->name);
1569 #endif
1570 /* tty->low_latency = 1; */
1571 return 0;
1574 /* Finally, routines used to initialize the serial driver. */
1576 static void __init show_serial_version(void)
1578 printk("DECstation Z8530 serial driver version 0.09\n");
1581 /* Initialize Z8530s zs_channels
1584 static void __init probe_sccs(void)
1586 struct dec_serial **pp;
1587 int i, n, n_chips = 0, n_channels, chip, channel;
1588 unsigned long flags;
1591 * did we get here by accident?
1593 if(!BUS_PRESENT) {
1594 printk("Not on JUNKIO machine, skipping probe_sccs\n");
1595 return;
1598 switch(mips_machtype) {
1599 #ifdef CONFIG_MACH_DECSTATION
1600 case MACH_DS5000_2X0:
1601 case MACH_DS5900:
1602 n_chips = 2;
1603 zs_parms = &ds_parms;
1604 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1605 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1606 break;
1607 case MACH_DS5000_1XX:
1608 n_chips = 2;
1609 zs_parms = &ds_parms;
1610 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1611 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1612 break;
1613 case MACH_DS5000_XX:
1614 n_chips = 1;
1615 zs_parms = &ds_parms;
1616 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1617 break;
1618 #endif
1619 default:
1620 panic("zs: unsupported bus");
1622 if (!zs_parms)
1623 panic("zs: uninitialized parms");
1625 pp = &zs_chain;
1627 n_channels = 0;
1629 for (chip = 0; chip < n_chips; chip++) {
1630 for (channel = 0; channel <= 1; channel++) {
1632 * The sccs reside on the high byte of the 16 bit IOBUS
1634 zs_channels[n_channels].control =
1635 (volatile void *)CKSEG1ADDR(dec_kn_slot_base +
1636 (0 == chip ? zs_parms->scc0 : zs_parms->scc1) +
1637 (0 == channel ? zs_parms->channel_a_offset :
1638 zs_parms->channel_b_offset));
1639 zs_channels[n_channels].data =
1640 zs_channels[n_channels].control + 4;
1642 #ifndef CONFIG_SERIAL_DEC_CONSOLE
1644 * We're called early and memory managment isn't up, yet.
1645 * Thus request_region would fail.
1647 if (!request_region((unsigned long)
1648 zs_channels[n_channels].control,
1649 ZS_CHAN_IO_SIZE, "SCC"))
1650 panic("SCC I/O region is not free");
1651 #endif
1652 zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1653 /* HACK alert! */
1654 if (!(chip & 1))
1655 zs_soft[n_channels].irq = zs_parms->irq0;
1656 else
1657 zs_soft[n_channels].irq = zs_parms->irq1;
1660 * Identification of channel A. Location of channel A
1661 * inside chip depends on mapping of internal address
1662 * the chip decodes channels by.
1663 * CHANNEL_A_NR returns either 0 (in case of
1664 * DECstations) or 1 (in case of Baget).
1666 if (CHANNEL_A_NR == channel)
1667 zs_soft[n_channels].zs_chan_a =
1668 &zs_channels[n_channels+1-2*CHANNEL_A_NR];
1669 else
1670 zs_soft[n_channels].zs_chan_a =
1671 &zs_channels[n_channels];
1673 *pp = &zs_soft[n_channels];
1674 pp = &zs_soft[n_channels].zs_next;
1675 n_channels++;
1679 *pp = 0;
1680 zs_channels_found = n_channels;
1682 for (n = 0; n < zs_channels_found; n++) {
1683 for (i = 0; i < 16; i++) {
1684 zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1688 spin_lock_irqsave(&zs_lock, flags);
1689 for (n = 0; n < zs_channels_found; n++) {
1690 if (n % 2 == 0) {
1691 write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
1692 udelay(10);
1693 write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
1695 load_zsregs(zs_soft[n].zs_channel,
1696 zs_soft[n].zs_channel->curregs);
1698 spin_unlock_irqrestore(&zs_lock, flags);
1701 static const struct tty_operations serial_ops = {
1702 .open = rs_open,
1703 .close = rs_close,
1704 .write = rs_write,
1705 .flush_chars = rs_flush_chars,
1706 .write_room = rs_write_room,
1707 .chars_in_buffer = rs_chars_in_buffer,
1708 .flush_buffer = rs_flush_buffer,
1709 .ioctl = rs_ioctl,
1710 .throttle = rs_throttle,
1711 .unthrottle = rs_unthrottle,
1712 .set_termios = rs_set_termios,
1713 .stop = rs_stop,
1714 .start = rs_start,
1715 .hangup = rs_hangup,
1716 .break_ctl = rs_break,
1717 .wait_until_sent = rs_wait_until_sent,
1718 .tiocmget = rs_tiocmget,
1719 .tiocmset = rs_tiocmset,
1722 /* zs_init inits the driver */
1723 int __init zs_init(void)
1725 int channel, i;
1726 struct dec_serial *info;
1728 if(!BUS_PRESENT)
1729 return -ENODEV;
1731 /* Find out how many Z8530 SCCs we have */
1732 if (zs_chain == 0)
1733 probe_sccs();
1734 serial_driver = alloc_tty_driver(zs_channels_found);
1735 if (!serial_driver)
1736 return -ENOMEM;
1738 show_serial_version();
1740 /* Initialize the tty_driver structure */
1741 /* Not all of this is exactly right for us. */
1743 serial_driver->owner = THIS_MODULE;
1744 serial_driver->name = "ttyS";
1745 serial_driver->major = TTY_MAJOR;
1746 serial_driver->minor_start = 64;
1747 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1748 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1749 serial_driver->init_termios = tty_std_termios;
1750 serial_driver->init_termios.c_cflag =
1751 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1752 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1753 tty_set_operations(serial_driver, &serial_ops);
1755 if (tty_register_driver(serial_driver))
1756 panic("Couldn't register serial driver");
1758 for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
1760 /* Needed before interrupts are enabled. */
1761 info->tty = 0;
1762 info->x_char = 0;
1764 if (info->hook && info->hook->init_info) {
1765 (*info->hook->init_info)(info);
1766 continue;
1769 info->magic = SERIAL_MAGIC;
1770 info->port = (int) info->zs_channel->control;
1771 info->line = i;
1772 info->custom_divisor = 16;
1773 info->close_delay = 50;
1774 info->closing_wait = 3000;
1775 info->event = 0;
1776 info->count = 0;
1777 info->blocked_open = 0;
1778 tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1779 init_waitqueue_head(&info->open_wait);
1780 init_waitqueue_head(&info->close_wait);
1781 printk("ttyS%02d at 0x%08x (irq = %d) is a Z85C30 SCC\n",
1782 info->line, info->port, info->irq);
1783 tty_register_device(serial_driver, info->line, NULL);
1787 for (channel = 0; channel < zs_channels_found; ++channel) {
1788 zs_soft[channel].clk_divisor = 16;
1789 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1791 if (request_irq(zs_soft[channel].irq, rs_interrupt, IRQF_SHARED,
1792 "scc", &zs_soft[channel]))
1793 printk(KERN_ERR "decserial: can't get irq %d\n",
1794 zs_soft[channel].irq);
1796 if (zs_soft[channel].hook) {
1797 zs_startup(&zs_soft[channel]);
1798 if (zs_soft[channel].hook->init_channel)
1799 (*zs_soft[channel].hook->init_channel)
1800 (&zs_soft[channel]);
1804 return 0;
1808 * polling I/O routines
1810 static int zs_poll_tx_char(void *handle, unsigned char ch)
1812 struct dec_serial *info = handle;
1813 struct dec_zschannel *chan = info->zs_channel;
1814 int ret;
1816 if(chan) {
1817 int loops = 10000;
1819 while (loops && !(read_zsreg(chan, 0) & Tx_BUF_EMP))
1820 loops--;
1822 if (loops) {
1823 write_zsdata(chan, ch);
1824 ret = 0;
1825 } else
1826 ret = -EAGAIN;
1828 return ret;
1829 } else
1830 return -ENODEV;
1833 static int zs_poll_rx_char(void *handle)
1835 struct dec_serial *info = handle;
1836 struct dec_zschannel *chan = info->zs_channel;
1837 int ret;
1839 if(chan) {
1840 int loops = 10000;
1842 while (loops && !(read_zsreg(chan, 0) & Rx_CH_AV))
1843 loops--;
1845 if (loops)
1846 ret = read_zsdata(chan);
1847 else
1848 ret = -EAGAIN;
1850 return ret;
1851 } else
1852 return -ENODEV;
1855 int register_zs_hook(unsigned int channel, struct dec_serial_hook *hook)
1857 struct dec_serial *info = &zs_soft[channel];
1859 if (info->hook) {
1860 printk("%s: line %d has already a hook registered\n",
1861 __FUNCTION__, channel);
1863 return 0;
1864 } else {
1865 hook->poll_rx_char = zs_poll_rx_char;
1866 hook->poll_tx_char = zs_poll_tx_char;
1867 info->hook = hook;
1869 return 1;
1873 int unregister_zs_hook(unsigned int channel)
1875 struct dec_serial *info = &zs_soft[channel];
1877 if (info->hook) {
1878 info->hook = NULL;
1879 return 1;
1880 } else {
1881 printk("%s: trying to unregister hook on line %d,"
1882 " but none is registered\n", __FUNCTION__, channel);
1883 return 0;
1888 * ------------------------------------------------------------
1889 * Serial console driver
1890 * ------------------------------------------------------------
1892 #ifdef CONFIG_SERIAL_DEC_CONSOLE
1896 * Print a string to the serial port trying not to disturb
1897 * any possible real use of the port...
1899 static void serial_console_write(struct console *co, const char *s,
1900 unsigned count)
1902 struct dec_serial *info;
1903 int i;
1905 info = zs_soft + co->index;
1907 for (i = 0; i < count; i++, s++) {
1908 if(*s == '\n')
1909 zs_poll_tx_char(info, '\r');
1910 zs_poll_tx_char(info, *s);
1914 static struct tty_driver *serial_console_device(struct console *c, int *index)
1916 *index = c->index;
1917 return serial_driver;
1921 * Setup initial baud/bits/parity. We do two things here:
1922 * - construct a cflag setting for the first rs_open()
1923 * - initialize the serial port
1924 * Return non-zero if we didn't find a serial port.
1926 static int __init serial_console_setup(struct console *co, char *options)
1928 struct dec_serial *info;
1929 int baud = 9600;
1930 int bits = 8;
1931 int parity = 'n';
1932 int cflag = CREAD | HUPCL | CLOCAL;
1933 int clk_divisor = 16;
1934 int brg;
1935 char *s;
1936 unsigned long flags;
1938 if(!BUS_PRESENT)
1939 return -ENODEV;
1941 info = zs_soft + co->index;
1943 if (zs_chain == 0)
1944 probe_sccs();
1946 info->is_cons = 1;
1948 if (options) {
1949 baud = simple_strtoul(options, NULL, 10);
1950 s = options;
1951 while(*s >= '0' && *s <= '9')
1952 s++;
1953 if (*s)
1954 parity = *s++;
1955 if (*s)
1956 bits = *s - '0';
1960 * Now construct a cflag setting.
1962 switch(baud) {
1963 case 1200:
1964 cflag |= B1200;
1965 break;
1966 case 2400:
1967 cflag |= B2400;
1968 break;
1969 case 4800:
1970 cflag |= B4800;
1971 break;
1972 case 19200:
1973 cflag |= B19200;
1974 break;
1975 case 38400:
1976 cflag |= B38400;
1977 break;
1978 case 57600:
1979 cflag |= B57600;
1980 break;
1981 case 115200:
1982 cflag |= B115200;
1983 break;
1984 case 9600:
1985 default:
1986 cflag |= B9600;
1988 * Set this to a sane value to prevent a divide error.
1990 baud = 9600;
1991 break;
1993 switch(bits) {
1994 case 7:
1995 cflag |= CS7;
1996 break;
1997 default:
1998 case 8:
1999 cflag |= CS8;
2000 break;
2002 switch(parity) {
2003 case 'o': case 'O':
2004 cflag |= PARODD;
2005 break;
2006 case 'e': case 'E':
2007 cflag |= PARENB;
2008 break;
2010 co->cflag = cflag;
2012 spin_lock_irqsave(&zs_lock, flags);
2015 * Set up the baud rate generator.
2017 brg = BPS_TO_BRG(baud, zs_parms->clock / clk_divisor);
2018 info->zs_channel->curregs[R12] = (brg & 255);
2019 info->zs_channel->curregs[R13] = ((brg >> 8) & 255);
2022 * Set byte size and parity.
2024 if (bits == 7) {
2025 info->zs_channel->curregs[R3] |= Rx7;
2026 info->zs_channel->curregs[R5] |= Tx7;
2027 } else {
2028 info->zs_channel->curregs[R3] |= Rx8;
2029 info->zs_channel->curregs[R5] |= Tx8;
2031 if (cflag & PARENB) {
2032 info->zs_channel->curregs[R4] |= PAR_ENA;
2034 if (!(cflag & PARODD)) {
2035 info->zs_channel->curregs[R4] |= PAR_EVEN;
2037 info->zs_channel->curregs[R4] |= SB1;
2040 * Turn on RTS and DTR.
2042 zs_rtsdtr(info, RTS | DTR, 1);
2045 * Finally, enable sequencing.
2047 info->zs_channel->curregs[R3] |= RxENABLE;
2048 info->zs_channel->curregs[R5] |= TxENAB;
2051 * Clear the interrupt registers.
2053 write_zsreg(info->zs_channel, R0, ERR_RES);
2054 write_zsreg(info->zs_channel, R0, RES_H_IUS);
2057 * Load up the new values.
2059 load_zsregs(info->zs_channel, info->zs_channel->curregs);
2061 /* Save the current value of RR0 */
2062 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
2064 zs_soft[co->index].clk_divisor = clk_divisor;
2065 zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
2067 spin_unlock_irqrestore(&zs_lock, flags);
2069 return 0;
2072 static struct console zs_console = {
2073 .name = "ttyS",
2074 .write = serial_console_write,
2075 .device = serial_console_device,
2076 .setup = serial_console_setup,
2077 .flags = CON_PRINTBUFFER,
2078 .index = -1,
2082 * Register console.
2084 void __init zs_serial_console_init(void)
2086 register_console(&zs_console);
2088 #endif /* ifdef CONFIG_SERIAL_DEC_CONSOLE */
2090 #ifdef CONFIG_KGDB
2091 struct dec_zschannel *zs_kgdbchan;
2092 static unsigned char scc_inittab[] = {
2093 9, 0x80, /* reset A side (CHRA) */
2094 13, 0, /* set baud rate divisor */
2095 12, 1,
2096 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
2097 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
2098 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
2099 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
2100 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
2103 /* These are for receiving and sending characters under the kgdb
2104 * source level kernel debugger.
2106 void putDebugChar(char kgdb_char)
2108 struct dec_zschannel *chan = zs_kgdbchan;
2109 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2110 RECOVERY_DELAY;
2111 write_zsdata(chan, kgdb_char);
2113 char getDebugChar(void)
2115 struct dec_zschannel *chan = zs_kgdbchan;
2116 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2117 eieio(); /*barrier();*/
2118 return read_zsdata(chan);
2120 void kgdb_interruptible(int yes)
2122 struct dec_zschannel *chan = zs_kgdbchan;
2123 int one, nine;
2124 nine = read_zsreg(chan, 9);
2125 if (yes == 1) {
2126 one = EXT_INT_ENAB|RxINT_ALL;
2127 nine |= MIE;
2128 printk("turning serial ints on\n");
2129 } else {
2130 one = RxINT_DISAB;
2131 nine &= ~MIE;
2132 printk("turning serial ints off\n");
2134 write_zsreg(chan, 1, one);
2135 write_zsreg(chan, 9, nine);
2138 static int kgdbhook_init_channel(void *handle)
2140 return 0;
2143 static void kgdbhook_init_info(void *handle)
2147 static void kgdbhook_rx_char(void *handle, unsigned char ch, unsigned char fl)
2149 struct dec_serial *info = handle;
2151 if (fl != TTY_NORMAL)
2152 return;
2153 if (ch == 0x03 || ch == '$')
2154 breakpoint();
2157 /* This sets up the serial port we're using, and turns on
2158 * interrupts for that channel, so kgdb is usable once we're done.
2160 static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2162 int brg;
2163 int i, x;
2164 volatile char *sccc = ms->control;
2165 brg = BPS_TO_BRG(bps, zs_parms->clock/16);
2166 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2167 for (i = 20000; i != 0; --i) {
2168 x = *sccc; eieio();
2170 for (i = 0; i < sizeof(scc_inittab); ++i) {
2171 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2172 i++;
2175 /* This is called at boot time to prime the kgdb serial debugging
2176 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2177 * for /dev/ttyb which is determined in setup_arch() from the
2178 * boot command line flags.
2180 struct dec_serial_hook zs_kgdbhook = {
2181 .init_channel = kgdbhook_init_channel,
2182 .init_info = kgdbhook_init_info,
2183 .rx_char = kgdbhook_rx_char,
2184 .cflags = B38400 | CS8 | CLOCAL,
2187 void __init zs_kgdb_hook(int tty_num)
2189 /* Find out how many Z8530 SCCs we have */
2190 if (zs_chain == 0)
2191 probe_sccs();
2192 zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2193 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2194 zs_soft[tty_num].change_needed = 0;
2195 zs_soft[tty_num].clk_divisor = 16;
2196 zs_soft[tty_num].zs_baud = 38400;
2197 zs_soft[tty_num].hook = &zs_kgdbhook; /* This runs kgdb */
2198 /* Turn on transmitter/receiver at 8-bits/char */
2199 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2200 printk("KGDB: on channel %d initialized\n", tty_num);
2201 set_debug_traps(); /* init stub */
2203 #endif /* ifdef CONFIG_KGDB */