1 /* sgiserial.c: Serial port driver for SGI machines.
3 * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
7 #include <linux/config.h> /* for CONFIG_REMOTE_DEBUG */
8 #include <linux/errno.h>
9 #include <linux/signal.h>
10 #include <linux/sched.h>
11 #include <linux/timer.h>
12 #include <linux/interrupt.h>
13 #include <linux/tty.h>
14 #include <linux/tty_flip.h>
15 #include <linux/major.h>
16 #include <linux/string.h>
17 #include <linux/fcntl.h>
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/console.h>
22 #include <linux/init.h>
26 #include <asm/sgialib.h>
27 #include <asm/system.h>
28 #include <asm/bitops.h>
29 #include <asm/sgihpc.h>
30 #include <asm/sgint23.h>
31 #include <asm/uaccess.h>
33 #include "sgiserial.h"
35 #define NUM_SERIAL 1 /* One chip on board. */
36 #define NUM_CHANNELS (NUM_SERIAL * 2)
38 extern wait_queue_head_t keypress_wait
;
40 struct sgi_zslayout
*zs_chips
[NUM_SERIAL
] = { 0, };
41 struct sgi_zschannel
*zs_channels
[NUM_CHANNELS
] = { 0, 0, };
42 struct sgi_zschannel
*zs_conschan
;
43 struct sgi_zschannel
*zs_kgdbchan
;
45 struct sgi_serial zs_soft
[NUM_CHANNELS
];
46 struct sgi_serial
*zs_chain
; /* IRQ servicing chain */
47 static int zilog_irq
= 21;
49 /* Console hooks... */
50 static int zs_cons_chanout
= 0;
51 static int zs_cons_chanin
= 0;
52 struct sgi_serial
*zs_consinfo
= 0;
54 static unsigned char kgdb_regs
[16] = {
55 0, 0, 0, /* write 0, 1, 2 */
56 (Rx8
| RxENABLE
), /* write 3 */
57 (X16CLK
| SB1
| PAR_EVEN
), /* write 4 */
58 (Tx8
| TxENAB
), /* write 5 */
59 0, 0, 0, /* write 6, 7, 8 */
62 (TCBR
| RCBR
), /* write 11 */
63 0, 0, /* BRG time constant, write 12 + 13 */
64 (BRENABL
), /* write 14 */
65 (DCDIE
) /* write 15 */
68 static unsigned char zscons_regs
[16] = {
70 (EXT_INT_ENAB
| INT_ALL_Rx
), /* write 1 */
72 (Rx8
| RxENABLE
), /* write 3 */
73 (X16CLK
), /* write 4 */
74 (DTR
| Tx8
| TxENAB
), /* write 5 */
75 0, 0, 0, /* write 6, 7, 8 */
76 (NV
| MIE
), /* write 9 */
78 (TCBR
| RCBR
), /* write 11 */
79 0, 0, /* BRG time constant, write 12 + 13 */
80 (BRENABL
), /* write 14 */
81 (DCDIE
| CTSIE
| TxUIE
| BRKIE
) /* write 15 */
84 #define ZS_CLOCK 3672000 /* Zilog input clock rate */
86 DECLARE_TASK_QUEUE(tq_serial
);
88 struct tty_driver serial_driver
, callout_driver
;
89 static int serial_refcount
;
91 /* serial subtype definitions */
92 #define SERIAL_TYPE_NORMAL 1
93 #define SERIAL_TYPE_CALLOUT 2
95 /* number of characters left in xmit buffer before we ask for more */
96 #define WAKEUP_CHARS 256
98 /* Debugging... DEBUG_INTR is bad to use when one of the zs
99 * lines is your console ;(
101 #undef SERIAL_DEBUG_INTR
102 #undef SERIAL_DEBUG_OPEN
103 #undef SERIAL_DEBUG_FLOW
105 #define RS_STROBE_TIME 10
106 #define RS_ISR_PASS_LIMIT 256
108 #define _INLINE_ inline
110 static void change_speed(struct sgi_serial
*info
);
112 static struct tty_struct
*serial_table
[NUM_CHANNELS
];
113 static struct termios
*serial_termios
[NUM_CHANNELS
];
114 static struct termios
*serial_termios_locked
[NUM_CHANNELS
];
117 #define MIN(a,b) ((a) < (b) ? (a) : (b))
121 * tmp_buf is used as a temporary buffer by serial_write. We need to
122 * lock it in case the memcpy_fromfs blocks while swapping in a page,
123 * and some other program tries to do a serial write at the same time.
124 * Since the lock will only come under contention when the system is
125 * swapping and available memory is low, it makes sense to share one
126 * buffer across all the serial ports, since it significantly saves
127 * memory if large numbers of serial ports are open.
129 static unsigned char tmp_buf
[4096]; /* This is cheating */
130 static DECLARE_MUTEX(tmp_buf_sem
);
132 static inline int serial_paranoia_check(struct sgi_serial
*info
,
133 dev_t device
, const char *routine
)
135 #ifdef SERIAL_PARANOIA_CHECK
136 static const char *badmagic
=
137 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
138 static const char *badinfo
=
139 "Warning: null sun_serial for (%d, %d) in %s\n";
142 printk(badinfo
, MAJOR(device
), MINOR(device
), routine
);
145 if (info
->magic
!= SERIAL_MAGIC
) {
146 printk(badmagic
, MAJOR(device
), MINOR(device
), routine
);
154 * This is used to figure out the divisor speeds and the timeouts
156 static int baud_table
[] = {
157 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
158 9600, 19200, 38400, 57600, 115200, 0 };
161 * Reading and writing Zilog8530 registers. The delays are to make this
162 * driver work on the Sun4 which needs a settling delay after each chip
163 * register access, other machines handle this in hardware via auxiliary
164 * flip-flops which implement the settle time we do in software.
166 * read_zsreg() and write_zsreg() may get called from rs_kgdb_hook() before
167 * interrupts are enabled. Therefore we have to check ioc_iocontrol before we
170 static inline unsigned char read_zsreg(struct sgi_zschannel
*channel
, unsigned char reg
)
172 unsigned char retval
;
173 volatile unsigned char junk
;
176 channel
->control
= reg
;
178 junk
= ioc_icontrol
->istat0
;
180 retval
= channel
->control
;
184 static inline void write_zsreg(struct sgi_zschannel
*channel
, unsigned char reg
, unsigned char value
)
186 volatile unsigned char junk
;
189 channel
->control
= reg
;
191 junk
= ioc_icontrol
->istat0
;
193 channel
->control
= value
;
195 junk
= ioc_icontrol
->istat0
;
199 static inline void load_zsregs(struct sgi_zschannel
*channel
, unsigned char *regs
)
201 ZS_CLEARERR(channel
);
202 ZS_CLEARFIFO(channel
);
204 write_zsreg(channel
, R4
, regs
[R4
]);
205 write_zsreg(channel
, R10
, regs
[R10
]);
206 write_zsreg(channel
, R3
, regs
[R3
] & ~RxENABLE
);
207 write_zsreg(channel
, R5
, regs
[R5
] & ~TxENAB
);
208 write_zsreg(channel
, R1
, regs
[R1
]);
209 write_zsreg(channel
, R9
, regs
[R9
]);
210 write_zsreg(channel
, R11
, regs
[R11
]);
211 write_zsreg(channel
, R12
, regs
[R12
]);
212 write_zsreg(channel
, R13
, regs
[R13
]);
213 write_zsreg(channel
, R14
, regs
[R14
]);
214 write_zsreg(channel
, R15
, regs
[R15
]);
215 write_zsreg(channel
, R3
, regs
[R3
]);
216 write_zsreg(channel
, R5
, regs
[R5
]);
220 /* Sets or clears DTR/RTS on the requested line */
221 static inline void zs_rtsdtr(struct sgi_serial
*ss
, int set
)
224 ss
->curregs
[5] |= (RTS
| DTR
);
225 ss
->pendregs
[5] = ss
->curregs
[5];
226 write_zsreg(ss
->zs_channel
, 5, ss
->curregs
[5]);
228 ss
->curregs
[5] &= ~(RTS
| DTR
);
229 ss
->pendregs
[5] = ss
->curregs
[5];
230 write_zsreg(ss
->zs_channel
, 5, ss
->curregs
[5]);
235 static inline void kgdb_chaninit(struct sgi_serial
*ss
, int intson
, int bps
)
240 kgdb_regs
[R1
] = INT_ALL_Rx
;
241 kgdb_regs
[R9
] |= MIE
;
244 kgdb_regs
[R9
] &= ~MIE
;
246 brg
= BPS_TO_BRG(bps
, ZS_CLOCK
/ss
->clk_divisor
);
247 kgdb_regs
[R12
] = (brg
& 255);
248 kgdb_regs
[R13
] = ((brg
>> 8) & 255);
249 load_zsregs(ss
->zs_channel
, kgdb_regs
);
252 /* Utility routines for the Zilog */
253 static inline int get_zsbaud(struct sgi_serial
*ss
)
255 struct sgi_zschannel
*channel
= ss
->zs_channel
;
258 /* The baud rate is split up between two 8-bit registers in
259 * what is termed 'BRG time constant' format in my docs for
260 * the chip, it is a function of the clk rate the chip is
261 * receiving which happens to be constant.
263 brg
= ((read_zsreg(channel
, 13)&0xff) << 8);
264 brg
|= (read_zsreg(channel
, 12)&0xff);
265 return BRG_TO_BPS(brg
, (ZS_CLOCK
/(ss
->clk_divisor
)));
269 * ------------------------------------------------------------
270 * rs_stop() and rs_start()
272 * This routines are called before setting or resetting tty->stopped.
273 * They enable or disable transmitter interrupts, as necessary.
274 * ------------------------------------------------------------
276 static void rs_stop(struct tty_struct
*tty
)
278 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
281 if (serial_paranoia_check(info
, tty
->device
, "rs_stop"))
284 save_flags(flags
); cli();
285 if (info
->curregs
[5] & TxENAB
) {
286 info
->curregs
[5] &= ~TxENAB
;
287 info
->pendregs
[5] &= ~TxENAB
;
288 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
290 restore_flags(flags
);
293 static void rs_start(struct tty_struct
*tty
)
295 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
298 if (serial_paranoia_check(info
, tty
->device
, "rs_start"))
301 save_flags(flags
); cli();
302 if (info
->xmit_cnt
&& info
->xmit_buf
&& !(info
->curregs
[5] & TxENAB
)) {
303 info
->curregs
[5] |= TxENAB
;
304 info
->pendregs
[5] = info
->curregs
[5];
305 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
307 restore_flags(flags
);
310 /* Drop into either the boot monitor or kadb upon receiving a break
311 * from keyboard/console input.
313 static void batten_down_hatches(void)
317 /* If we are doing kadb, we call the debugger
318 * else we just drop into the boot monitor.
319 * Note that we must flush the user windows
320 * first before giving up control.
323 if((((unsigned long)linux_dbvec
)>=DEBUG_FIRSTVADDR
) &&
324 (((unsigned long)linux_dbvec
)<=DEBUG_LASTVADDR
))
329 /* XXX We want to notify the keyboard driver that all
330 * XXX keys are in the up state or else weird things
337 /* On receive, this clears errors and the receiver interrupts */
338 static inline void rs_recv_clear(struct sgi_zschannel
*zsc
)
340 volatile unsigned char junk
;
343 zsc
->control
= ERR_RES
;
344 junk
= ioc_icontrol
->istat0
;
346 zsc
->control
= RES_H_IUS
;
347 junk
= ioc_icontrol
->istat0
;
351 * ----------------------------------------------------------------------
353 * Here starts the interrupt handling routines. All of the following
354 * subroutines are declared as inline and are folded into
355 * rs_interrupt(). They were separated out for readability's sake.
357 * Note: rs_interrupt() is a "fast" interrupt, which means that it
358 * runs with interrupts turned off. People who may want to modify
359 * rs_interrupt() should try to keep the interrupt handler as fast as
360 * possible. After you are done making modifications, it is not a bad
363 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
365 * and look at the resulting assemble code in serial.s.
367 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
368 * -----------------------------------------------------------------------
372 * This routine is used by the interrupt handler to schedule
373 * processing in the software interrupt portion of the driver.
375 static _INLINE_
void rs_sched_event(struct sgi_serial
*info
,
378 info
->event
|= 1 << event
;
379 queue_task(&info
->tqueue
, &tq_serial
);
383 #ifdef CONFIG_REMOTE_DEBUG
384 extern void set_async_breakpoint(unsigned int epc
);
387 static _INLINE_
void receive_chars(struct sgi_serial
*info
, struct pt_regs
*regs
)
389 struct tty_struct
*tty
= info
->tty
;
390 volatile unsigned char junk
;
391 unsigned char ch
, stat
;
394 ch
= info
->zs_channel
->data
;
395 junk
= ioc_icontrol
->istat0
;
397 stat
= read_zsreg(info
->zs_channel
, R1
);
399 /* If this is the console keyboard, we need to handle
403 if(ch
==0) { /* whee, break received */
404 batten_down_hatches();
405 rs_recv_clear(info
->zs_channel
);
407 } else if (ch
== 1) {
410 } else if (ch
== 2) {
413 /* It is a 'keyboard interrupt' ;-) */
414 wake_up(&keypress_wait
);
416 /* Look for kgdb 'stop' character, consult the gdb documentation
417 * for remote target debugging and arch/sparc/kernel/sparc-stub.c
418 * to see how all this works.
420 #ifdef CONFIG_REMOTE_DEBUG
421 if((info
->kgdb_channel
) && (ch
=='\003')) {
422 set_async_breakpoint(read_32bit_cp0_register(CP0_EPC
));
429 if (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
)
430 queue_task(&tty
->flip
.tqueue
, &tq_timer
);
433 *tty
->flip
.flag_buf_ptr
++ = TTY_PARITY
;
434 else if(stat
& Rx_OVR
)
435 *tty
->flip
.flag_buf_ptr
++ = TTY_OVERRUN
;
436 else if(stat
& CRC_ERR
)
437 *tty
->flip
.flag_buf_ptr
++ = TTY_FRAME
;
439 *tty
->flip
.flag_buf_ptr
++ = 0; /* XXX */
440 *tty
->flip
.char_buf_ptr
++ = ch
;
442 queue_task(&tty
->flip
.tqueue
, &tq_timer
);
445 rs_recv_clear(info
->zs_channel
);
449 static _INLINE_
void transmit_chars(struct sgi_serial
*info
)
451 volatile unsigned char junk
;
453 /* P3: In theory we have to test readiness here because a
454 * serial console can clog the chip through rs_put_char().
455 * David did not do this. I think he relies on 3-chars FIFO in 8530.
456 * Let's watch for lost _output_ characters. XXX
459 /* SGI ADDENDUM: On most SGI machines, the Zilog does possess
460 * a 16 or 17 byte fifo, so no worries. -dm
466 info
->zs_channel
->data
= info
->x_char
;
467 junk
= ioc_icontrol
->istat0
;
470 goto clear_and_return
;
473 if((info
->xmit_cnt
<= 0) || info
->tty
->stopped
) {
474 /* That's peculiar... */
476 info
->zs_channel
->control
= RES_Tx_P
;
477 junk
= ioc_icontrol
->istat0
;
478 goto clear_and_return
;
483 info
->zs_channel
->data
= info
->xmit_buf
[info
->xmit_tail
++];
484 junk
= ioc_icontrol
->istat0
;
486 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
489 if (info
->xmit_cnt
< WAKEUP_CHARS
)
490 rs_sched_event(info
, RS_EVENT_WRITE_WAKEUP
);
492 if(info
->xmit_cnt
<= 0) {
494 info
->zs_channel
->control
= RES_Tx_P
;
495 junk
= ioc_icontrol
->istat0
;
496 goto clear_and_return
;
500 /* Clear interrupt */
502 info
->zs_channel
->control
= RES_H_IUS
;
503 junk
= ioc_icontrol
->istat0
;
507 static _INLINE_
void status_handle(struct sgi_serial
*info
)
509 volatile unsigned char junk
;
510 unsigned char status
;
512 /* Get status from Read Register 0 */
514 status
= info
->zs_channel
->control
;
515 junk
= ioc_icontrol
->istat0
;
516 /* Clear status condition... */
518 info
->zs_channel
->control
= RES_EXT_INT
;
519 junk
= ioc_icontrol
->istat0
;
520 /* Clear the interrupt */
522 info
->zs_channel
->control
= RES_H_IUS
;
523 junk
= ioc_icontrol
->istat0
;
527 if((info
->tty
->termios
->c_cflag
& CRTSCTS
) &&
528 ((info
->curregs
[3] & AUTO_ENAB
)==0)) {
529 info
->curregs
[3] |= AUTO_ENAB
;
530 info
->pendregs
[3] |= AUTO_ENAB
;
531 write_zsreg(info
->zs_channel
, 3, info
->curregs
[3]);
534 if((info
->curregs
[3] & AUTO_ENAB
)) {
535 info
->curregs
[3] &= ~AUTO_ENAB
;
536 info
->pendregs
[3] &= ~AUTO_ENAB
;
537 write_zsreg(info
->zs_channel
, 3, info
->curregs
[3]);
541 /* Whee, if this is console input and this is a
542 * 'break asserted' status change interrupt, call
545 if((status
& BRK_ABRT
) && info
->break_abort
)
546 batten_down_hatches();
548 /* XXX Whee, put in a buffer somewhere, the status information
549 * XXX whee whee whee... Where does the information go...
555 * This is the serial driver's generic interrupt routine
557 void rs_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
559 struct sgi_serial
* info
= (struct sgi_serial
*) dev_id
;
560 unsigned char zs_intreg
;
562 zs_intreg
= read_zsreg(info
->zs_next
->zs_channel
, 3);
564 /* NOTE: The read register 3, which holds the irq status,
565 * does so for both channels on each chip. Although
566 * the status value itself must be read from the A
567 * channel and is only valid when read from channel A.
568 * Yes... broken hardware...
570 #define CHAN_A_IRQMASK (CHARxIP | CHATxIP | CHAEXT)
571 #define CHAN_B_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
574 /* Channel B -- /dev/ttyb, could be the console */
575 if(zs_intreg
& CHAN_B_IRQMASK
) {
576 if (zs_intreg
& CHBRxIP
)
577 receive_chars(info
, regs
);
578 if (zs_intreg
& CHBTxIP
)
579 transmit_chars(info
);
580 if (zs_intreg
& CHBEXT
)
586 /* Channel A -- /dev/ttya, could be the console */
587 if(zs_intreg
& CHAN_A_IRQMASK
) {
588 if (zs_intreg
& CHARxIP
)
589 receive_chars(info
, regs
);
590 if (zs_intreg
& CHATxIP
)
591 transmit_chars(info
);
592 if (zs_intreg
& CHAEXT
)
598 * -------------------------------------------------------------------
599 * Here ends the serial interrupt routines.
600 * -------------------------------------------------------------------
604 * This routine is used to handle the "bottom half" processing for the
605 * serial driver, known also the "software interrupt" processing.
606 * This processing is done at the kernel interrupt level, after the
607 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
608 * is where time-consuming activities which can not be done in the
609 * interrupt driver proper are done; the interrupt driver schedules
610 * them using rs_sched_event(), and they get done here.
612 static void do_serial_bh(void)
614 run_task_queue(&tq_serial
);
617 static void do_softint(void *private_
)
619 struct sgi_serial
*info
= (struct sgi_serial
*) private_
;
620 struct tty_struct
*tty
;
626 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
)) {
627 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
628 tty
->ldisc
.write_wakeup
)
629 (tty
->ldisc
.write_wakeup
)(tty
);
630 wake_up_interruptible(&tty
->write_wait
);
635 * This routine is called from the scheduler tqueue when the interrupt
636 * routine has signalled that a hangup has occurred. The path of
637 * hangup processing is:
639 * serial interrupt routine -> (scheduler tqueue) ->
640 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
643 static void do_serial_hangup(void *private_
)
645 struct sgi_serial
*info
= (struct sgi_serial
*) private_
;
646 struct tty_struct
*tty
;
657 * This subroutine is called when the RS_TIMER goes off. It is used
658 * by the serial driver to handle ports that do not have an interrupt
659 * (irq=0). This doesn't work at all for 16450's, as a sun has a Z8530.
662 static void rs_timer(void)
664 printk("rs_timer called\n");
669 static int startup(struct sgi_serial
* info
)
671 volatile unsigned char junk
;
674 if (info
->flags
& ZILOG_INITIALIZED
)
677 if (!info
->xmit_buf
) {
678 info
->xmit_buf
= (unsigned char *) get_free_page(GFP_KERNEL
);
683 save_flags(flags
); cli();
685 #ifdef SERIAL_DEBUG_OPEN
686 printk("starting up ttys%d (irq %d)...", info
->line
, info
->irq
);
690 * Clear the FIFO buffers and disable them
691 * (they will be reenabled in change_speed())
693 ZS_CLEARFIFO(info
->zs_channel
);
694 info
->xmit_fifo_size
= 1;
697 * Clear the interrupt registers.
700 info
->zs_channel
->control
= ERR_RES
;
701 junk
= ioc_icontrol
->istat0
;
703 info
->zs_channel
->control
= RES_H_IUS
;
704 junk
= ioc_icontrol
->istat0
;
707 * Now, initialize the Zilog
712 * Finally, enable sequencing and interrupts
714 info
->curregs
[1] |= (info
->curregs
[1] & ~0x18) | (EXT_INT_ENAB
|INT_ALL_Rx
);
715 info
->pendregs
[1] = info
->curregs
[1];
716 info
->curregs
[3] |= (RxENABLE
| Rx8
);
717 info
->pendregs
[3] = info
->curregs
[3];
718 /* We enable Tx interrupts as needed. */
719 info
->curregs
[5] |= (TxENAB
| Tx8
);
720 info
->pendregs
[5] = info
->curregs
[5];
721 info
->curregs
[9] |= (NV
| MIE
);
722 info
->pendregs
[9] = info
->curregs
[9];
723 write_zsreg(info
->zs_channel
, 3, info
->curregs
[3]);
724 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
725 write_zsreg(info
->zs_channel
, 9, info
->curregs
[9]);
728 * And clear the interrupt registers again for luck.
731 info
->zs_channel
->control
= ERR_RES
;
732 junk
= ioc_icontrol
->istat0
;
734 info
->zs_channel
->control
= RES_H_IUS
;
735 junk
= ioc_icontrol
->istat0
;
738 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
739 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
742 * Set up serial timers...
744 #if 0 /* Works well and stops the machine. */
745 timer_table
[RS_TIMER
].expires
= jiffies
+ 2;
746 timer_active
|= 1 << RS_TIMER
;
750 * and set the speed of the serial port
754 info
->flags
|= ZILOG_INITIALIZED
;
755 restore_flags(flags
);
760 * This routine will shutdown a serial port; interrupts are disabled, and
761 * DTR is dropped if the hangup on close termio flag is on.
763 static void shutdown(struct sgi_serial
* info
)
767 if (!(info
->flags
& ZILOG_INITIALIZED
))
770 #ifdef SERIAL_DEBUG_OPEN
771 printk("Shutting down serial port %d (irq %d)....", info
->line
,
775 save_flags(flags
); cli(); /* Disable interrupts */
777 if (info
->xmit_buf
) {
778 free_page((unsigned long) info
->xmit_buf
);
783 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
785 info
->flags
&= ~ZILOG_INITIALIZED
;
786 restore_flags(flags
);
790 * This routine is called to set the UART divisor registers to match
791 * the specified baud rate for a serial port.
793 static void change_speed(struct sgi_serial
*info
)
800 if (!info
->tty
|| !info
->tty
->termios
)
802 cflag
= info
->tty
->termios
->c_cflag
;
803 if (!(port
= info
->port
))
807 /* XXX CBAUDEX is not obeyed.
808 * It is impossible at a 32bits SPARC.
809 * But we have to report this to user ... someday.
814 /* XXX B0, hangup the line. */
815 do_serial_hangup(info
);
816 } else if (baud_table
[i
]) {
817 info
->zs_baud
= baud_table
[i
];
818 info
->clk_divisor
= 16;
820 info
->curregs
[4] = X16CLK
;
821 info
->curregs
[11] = TCBR
| RCBR
;
822 brg
= BPS_TO_BRG(info
->zs_baud
, ZS_CLOCK
/info
->clk_divisor
);
823 info
->curregs
[12] = (brg
& 255);
824 info
->curregs
[13] = ((brg
>> 8) & 255);
825 info
->curregs
[14] = BRENABL
;
828 /* byte size and parity */
829 switch (cflag
& CSIZE
) {
831 info
->curregs
[3] &= ~(0xc0);
832 info
->curregs
[3] |= Rx5
;
833 info
->pendregs
[3] = info
->curregs
[3];
834 info
->curregs
[5] &= ~(0xe0);
835 info
->curregs
[5] |= Tx5
;
836 info
->pendregs
[5] = info
->curregs
[5];
839 info
->curregs
[3] &= ~(0xc0);
840 info
->curregs
[3] |= Rx6
;
841 info
->pendregs
[3] = info
->curregs
[3];
842 info
->curregs
[5] &= ~(0xe0);
843 info
->curregs
[5] |= Tx6
;
844 info
->pendregs
[5] = info
->curregs
[5];
847 info
->curregs
[3] &= ~(0xc0);
848 info
->curregs
[3] |= Rx7
;
849 info
->pendregs
[3] = info
->curregs
[3];
850 info
->curregs
[5] &= ~(0xe0);
851 info
->curregs
[5] |= Tx7
;
852 info
->pendregs
[5] = info
->curregs
[5];
855 default: /* defaults to 8 bits */
856 info
->curregs
[3] &= ~(0xc0);
857 info
->curregs
[3] |= Rx8
;
858 info
->pendregs
[3] = info
->curregs
[3];
859 info
->curregs
[5] &= ~(0xe0);
860 info
->curregs
[5] |= Tx8
;
861 info
->pendregs
[5] = info
->curregs
[5];
864 info
->curregs
[4] &= ~(0x0c);
865 if (cflag
& CSTOPB
) {
866 info
->curregs
[4] |= SB2
;
868 info
->curregs
[4] |= SB1
;
870 info
->pendregs
[4] = info
->curregs
[4];
871 if (cflag
& PARENB
) {
872 info
->curregs
[4] |= PAR_ENA
;
873 info
->pendregs
[4] |= PAR_ENA
;
875 info
->curregs
[4] &= ~PAR_ENA
;
876 info
->pendregs
[4] &= ~PAR_ENA
;
878 if (!(cflag
& PARODD
)) {
879 info
->curregs
[4] |= PAR_EVEN
;
880 info
->pendregs
[4] |= PAR_EVEN
;
882 info
->curregs
[4] &= ~PAR_EVEN
;
883 info
->pendregs
[4] &= ~PAR_EVEN
;
886 /* Load up the new values */
887 load_zsregs(info
->zs_channel
, info
->curregs
);
892 /* This is for console output over ttya/ttyb */
893 static void rs_put_char(char ch
)
895 struct sgi_zschannel
*chan
= zs_conschan
;
896 volatile unsigned char junk
;
897 int flags
, loops
= 0;
899 save_flags(flags
); cli();
900 while(((junk
= chan
->control
) & Tx_BUF_EMP
)==0 && loops
< 10000) {
907 junk
= ioc_icontrol
->istat0
;
908 restore_flags(flags
);
911 /* These are for receiving and sending characters under the kgdb
912 * source level kernel debugger.
914 int putDebugChar(char kgdb_char
)
916 struct sgi_zschannel
*chan
= zs_kgdbchan
;
917 volatile unsigned char junk
;
920 save_flags(flags
); cli();
922 while((chan
->control
& Tx_BUF_EMP
)==0)
926 chan
->data
= kgdb_char
;
927 junk
= ioc_icontrol
->istat0
;
928 restore_flags(flags
);
933 char getDebugChar(void)
935 struct sgi_zschannel
*chan
= zs_kgdbchan
;
938 while((chan
->control
& Rx_CH_AV
)==0)
941 junk
= ioc_icontrol
->istat0
;
947 * Fair output driver allows a process to speak.
949 static void rs_fair_output(void)
951 int left
; /* Output no more than that */
953 struct sgi_serial
*info
= zs_consinfo
;
954 volatile unsigned char junk
;
957 if (info
== 0) return;
958 if (info
->xmit_buf
== 0) return;
960 save_flags(flags
); cli();
961 left
= info
->xmit_cnt
;
963 c
= info
->xmit_buf
[info
->xmit_tail
];
964 info
->xmit_tail
= (info
->xmit_tail
+1) & (SERIAL_XMIT_SIZE
-1);
966 restore_flags(flags
);
970 save_flags(flags
); cli();
971 left
= MIN(info
->xmit_cnt
, left
-1);
974 /* Last character is being transmitted now (hopefully). */
976 zs_conschan
->control
= RES_Tx_P
;
977 junk
= ioc_icontrol
->istat0
;
979 restore_flags(flags
);
984 static int rs_write(struct tty_struct
* tty
, int from_user
,
985 const unsigned char *buf
, int count
)
988 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
991 if (serial_paranoia_check(info
, tty
->device
, "rs_write"))
994 if (!tty
|| !info
->xmit_buf
)
1000 c
= MIN(count
, MIN(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
1001 SERIAL_XMIT_SIZE
- info
->xmit_head
));
1007 copy_from_user(tmp_buf
, buf
, c
);
1008 c
= MIN(c
, MIN(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
1009 SERIAL_XMIT_SIZE
- info
->xmit_head
));
1010 memcpy(info
->xmit_buf
+ info
->xmit_head
, tmp_buf
, c
);
1013 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
1014 info
->xmit_head
= (info
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
1015 info
->xmit_cnt
+= c
;
1016 restore_flags(flags
);
1021 if (info
->xmit_cnt
&& !tty
->stopped
&& !tty
->hw_stopped
&&
1022 !(info
->curregs
[5] & TxENAB
)) {
1023 /* Enable transmitter */
1024 info
->curregs
[1] |= TxINT_ENAB
|EXT_INT_ENAB
;
1025 info
->pendregs
[1] |= TxINT_ENAB
|EXT_INT_ENAB
;
1026 write_zsreg(info
->zs_channel
, 1, info
->curregs
[1]);
1027 info
->curregs
[5] |= TxENAB
;
1028 info
->pendregs
[5] |= TxENAB
;
1029 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
1031 restore_flags(flags
);
1035 static int rs_write_room(struct tty_struct
*tty
)
1037 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1040 if (serial_paranoia_check(info
, tty
->device
, "rs_write_room"))
1042 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
1048 static int rs_chars_in_buffer(struct tty_struct
*tty
)
1050 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1052 if (serial_paranoia_check(info
, tty
->device
, "rs_chars_in_buffer"))
1054 return info
->xmit_cnt
;
1057 static void rs_flush_buffer(struct tty_struct
*tty
)
1059 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1061 if (serial_paranoia_check(info
, tty
->device
, "rs_flush_buffer"))
1064 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
1066 wake_up_interruptible(&tty
->write_wait
);
1067 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
1068 tty
->ldisc
.write_wakeup
)
1069 (tty
->ldisc
.write_wakeup
)(tty
);
1072 static void rs_flush_chars(struct tty_struct
*tty
)
1074 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1075 unsigned long flags
;
1077 if (serial_paranoia_check(info
, tty
->device
, "rs_flush_chars"))
1080 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| tty
->hw_stopped
||
1084 /* Enable transmitter */
1085 save_flags(flags
); cli();
1086 info
->curregs
[1] |= TxINT_ENAB
|EXT_INT_ENAB
;
1087 info
->pendregs
[1] |= TxINT_ENAB
|EXT_INT_ENAB
;
1088 write_zsreg(info
->zs_channel
, 1, info
->curregs
[1]);
1089 info
->curregs
[5] |= TxENAB
;
1090 info
->pendregs
[5] |= TxENAB
;
1091 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
1094 * Send a first (bootstrapping) character. A best solution is
1095 * to call transmit_chars() here which handles output in a
1096 * generic way. Current transmit_chars() not only transmits,
1097 * but resets interrupts also what we do not desire here.
1098 * XXX Discuss with David.
1100 if (info
->zs_channel
->control
& Tx_BUF_EMP
) {
1101 volatile unsigned char junk
;
1105 info
->zs_channel
->data
= info
->xmit_buf
[info
->xmit_tail
++];
1106 junk
= ioc_icontrol
->istat0
;
1107 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
1110 restore_flags(flags
);
1114 * ------------------------------------------------------------
1117 * This routine is called by the upper-layer tty layer to signal that
1118 * incoming characters should be throttled.
1119 * ------------------------------------------------------------
1121 static void rs_throttle(struct tty_struct
* tty
)
1123 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1124 #ifdef SERIAL_DEBUG_THROTTLE
1127 printk("throttle %s: %d....\n", _tty_name(tty
, buf
),
1128 tty
->ldisc
.chars_in_buffer(tty
));
1131 if (serial_paranoia_check(info
, tty
->device
, "rs_throttle"))
1135 info
->x_char
= STOP_CHAR(tty
);
1137 /* Turn off RTS line */
1139 info
->curregs
[5] &= ~RTS
;
1140 info
->pendregs
[5] &= ~RTS
;
1141 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
1145 static void rs_unthrottle(struct tty_struct
* tty
)
1147 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1148 #ifdef SERIAL_DEBUG_THROTTLE
1151 printk("unthrottle %s: %d....\n", _tty_name(tty
, buf
),
1152 tty
->ldisc
.chars_in_buffer(tty
));
1155 if (serial_paranoia_check(info
, tty
->device
, "rs_unthrottle"))
1162 info
->x_char
= START_CHAR(tty
);
1165 /* Assert RTS line */
1167 info
->curregs
[5] |= RTS
;
1168 info
->pendregs
[5] |= RTS
;
1169 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
1174 * ------------------------------------------------------------
1175 * rs_ioctl() and friends
1176 * ------------------------------------------------------------
1179 static int get_serial_info(struct sgi_serial
* info
,
1180 struct serial_struct
* retinfo
)
1182 struct serial_struct tmp
;
1186 memset(&tmp
, 0, sizeof(tmp
));
1187 tmp
.type
= info
->type
;
1188 tmp
.line
= info
->line
;
1189 tmp
.port
= info
->port
;
1190 tmp
.irq
= info
->irq
;
1191 tmp
.flags
= info
->flags
;
1192 tmp
.baud_base
= info
->baud_base
;
1193 tmp
.close_delay
= info
->close_delay
;
1194 tmp
.closing_wait
= info
->closing_wait
;
1195 tmp
.custom_divisor
= info
->custom_divisor
;
1196 return copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
));
1199 static int set_serial_info(struct sgi_serial
* info
,
1200 struct serial_struct
* new_info
)
1202 struct serial_struct new_serial
;
1203 struct sgi_serial old_info
;
1208 copy_from_user(&new_serial
,new_info
,sizeof(new_serial
));
1211 if (!capable(CAP_SYS_ADMIN
)) {
1212 if ((new_serial
.baud_base
!= info
->baud_base
) ||
1213 (new_serial
.type
!= info
->type
) ||
1214 (new_serial
.close_delay
!= info
->close_delay
) ||
1215 ((new_serial
.flags
& ~ZILOG_USR_MASK
) !=
1216 (info
->flags
& ~ZILOG_USR_MASK
)))
1218 info
->flags
= ((info
->flags
& ~ZILOG_USR_MASK
) |
1219 (new_serial
.flags
& ZILOG_USR_MASK
));
1220 info
->custom_divisor
= new_serial
.custom_divisor
;
1221 goto check_and_exit
;
1224 if (info
->count
> 1)
1228 * OK, past this point, all the error checking has been done.
1229 * At this point, we start making changes.....
1232 info
->baud_base
= new_serial
.baud_base
;
1233 info
->flags
= ((info
->flags
& ~ZILOG_FLAGS
) |
1234 (new_serial
.flags
& ZILOG_FLAGS
));
1235 info
->type
= new_serial
.type
;
1236 info
->close_delay
= new_serial
.close_delay
;
1237 info
->closing_wait
= new_serial
.closing_wait
;
1240 retval
= startup(info
);
1245 * get_lsr_info - get line status register info
1247 * Purpose: Let user call ioctl() to get info when the UART physically
1248 * is emptied. On bus types like RS485, the transmitter must
1249 * release the bus after transmitting. This must be done when
1250 * the transmit shift register is empty, not be done when the
1251 * transmit holding register is empty. This functionality
1252 * allows an RS485 driver to be written in user space.
1254 static int get_lsr_info(struct sgi_serial
* info
, unsigned int *value
)
1256 volatile unsigned char junk
;
1257 unsigned char status
;
1261 status
= info
->zs_channel
->control
;
1262 junk
= ioc_icontrol
->istat0
;
1264 return put_user(status
,value
);
1268 * This routine sends a break character out the serial port.
1270 static void send_break( struct sgi_serial
* info
, int duration
)
1274 current
->state
= TASK_INTERRUPTIBLE
;
1276 write_zsreg(info
->zs_channel
, 5, (info
->curregs
[5] | SND_BRK
));
1277 schedule_timeout(duration
);
1278 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
1282 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
1283 unsigned int cmd
, unsigned long arg
)
1286 struct sgi_serial
* info
= (struct sgi_serial
*)tty
->driver_data
;
1289 if (serial_paranoia_check(info
, tty
->device
, "rs_ioctl"))
1292 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
1293 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
1294 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
1295 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1300 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1301 retval
= tty_check_change(tty
);
1304 tty_wait_until_sent(tty
, 0);
1306 send_break(info
, HZ
/4); /* 1/4 second */
1308 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1309 retval
= tty_check_change(tty
);
1312 tty_wait_until_sent(tty
, 0);
1313 send_break(info
, arg
? arg
*(HZ
/10) : HZ
/4);
1316 error
= verify_area(VERIFY_WRITE
, (void *) arg
,sizeof(long));
1319 put_user(C_CLOCAL(tty
) ? 1 : 0,
1320 (unsigned long *) arg
);
1323 error
= get_user(arg
, (unsigned long *)arg
);
1326 tty
->termios
->c_cflag
=
1327 ((tty
->termios
->c_cflag
& ~CLOCAL
) |
1328 (arg
? CLOCAL
: 0));
1331 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1332 sizeof(struct serial_struct
));
1335 return get_serial_info(info
,
1336 (struct serial_struct
*) arg
);
1338 return set_serial_info(info
,
1339 (struct serial_struct
*) arg
);
1340 case TIOCSERGETLSR
: /* Get line status register */
1341 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1342 sizeof(unsigned int));
1346 return get_lsr_info(info
, (unsigned int *) arg
);
1348 case TIOCSERGSTRUCT
:
1349 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1350 sizeof(struct sgi_serial
));
1353 copy_to_user((struct sun_serial
*) arg
,
1354 info
, sizeof(struct sgi_serial
));
1358 return -ENOIOCTLCMD
;
1363 static void rs_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
1365 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1367 if (tty
->termios
->c_cflag
== old_termios
->c_cflag
)
1372 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1373 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1374 tty
->hw_stopped
= 0;
1380 * ------------------------------------------------------------
1383 * This routine is called when the serial port gets closed. First, we
1384 * wait for the last remaining data to be sent. Then, we unlink its
1385 * ZILOG structure from the interrupt chain if necessary, and we free
1386 * that IRQ if nothing is left in the chain.
1387 * ------------------------------------------------------------
1389 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1391 struct sgi_serial
* info
= (struct sgi_serial
*)tty
->driver_data
;
1392 unsigned long flags
;
1394 if (!info
|| serial_paranoia_check(info
, tty
->device
, "rs_close"))
1397 save_flags(flags
); cli();
1399 if (tty_hung_up_p(filp
)) {
1400 restore_flags(flags
);
1404 #ifdef SERIAL_DEBUG_OPEN
1405 printk("rs_close ttys%d, count = %d\n", info
->line
, info
->count
);
1407 if ((tty
->count
== 1) && (info
->count
!= 1)) {
1409 * Uh, oh. tty->count is 1, which means that the tty
1410 * structure will be freed. Info->count should always
1411 * be one in these conditions. If it's greater than
1412 * one, we've got real problems, since it means the
1413 * serial port won't be shutdown.
1415 printk("rs_close: bad serial port count; tty->count is 1, "
1416 "info->count is %d\n", info
->count
);
1419 if (--info
->count
< 0) {
1420 printk("rs_close: bad serial port count for ttys%d: %d\n",
1421 info
->line
, info
->count
);
1425 restore_flags(flags
);
1428 info
->flags
|= ZILOG_CLOSING
;
1430 * Save the termios structure, since this port may have
1431 * separate termios for callout and dialin.
1433 if (info
->flags
& ZILOG_NORMAL_ACTIVE
)
1434 info
->normal_termios
= *tty
->termios
;
1435 if (info
->flags
& ZILOG_CALLOUT_ACTIVE
)
1436 info
->callout_termios
= *tty
->termios
;
1438 * Now we wait for the transmit buffer to clear; and we notify
1439 * the line discipline to only process XON/XOFF characters.
1442 if (info
->closing_wait
!= ZILOG_CLOSING_WAIT_NONE
)
1443 tty_wait_until_sent(tty
, info
->closing_wait
);
1445 * At this point we stop accepting input. To do this, we
1446 * disable the receive line status interrupts, and tell the
1447 * interrupt driver to stop checking the data ready bit in the
1448 * line status register.
1450 /** if (!info->iscons) ... **/
1451 info
->curregs
[3] &= ~RxENABLE
;
1452 info
->pendregs
[3] = info
->curregs
[3];
1453 write_zsreg(info
->zs_channel
, 3, info
->curregs
[3]);
1454 info
->curregs
[1] &= ~(0x18);
1455 info
->pendregs
[1] = info
->curregs
[1];
1456 write_zsreg(info
->zs_channel
, 1, info
->curregs
[1]);
1457 ZS_CLEARFIFO(info
->zs_channel
);
1460 if (tty
->driver
.flush_buffer
)
1461 tty
->driver
.flush_buffer(tty
);
1462 if (tty
->ldisc
.flush_buffer
)
1463 tty
->ldisc
.flush_buffer(tty
);
1467 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
1468 if (tty
->ldisc
.close
)
1469 (tty
->ldisc
.close
)(tty
);
1470 tty
->ldisc
= ldiscs
[N_TTY
];
1471 tty
->termios
->c_line
= N_TTY
;
1472 if (tty
->ldisc
.open
)
1473 (tty
->ldisc
.open
)(tty
);
1475 if (info
->blocked_open
) {
1476 if (info
->close_delay
) {
1477 current
->state
= TASK_INTERRUPTIBLE
;
1478 schedule_timeout(info
->close_delay
);
1480 wake_up_interruptible(&info
->open_wait
);
1482 info
->flags
&= ~(ZILOG_NORMAL_ACTIVE
|ZILOG_CALLOUT_ACTIVE
|
1484 wake_up_interruptible(&info
->close_wait
);
1485 restore_flags(flags
);
1489 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1491 void rs_hangup(struct tty_struct
*tty
)
1493 struct sgi_serial
* info
= (struct sgi_serial
*)tty
->driver_data
;
1495 if (serial_paranoia_check(info
, tty
->device
, "rs_hangup"))
1498 rs_flush_buffer(tty
);
1502 info
->flags
&= ~(ZILOG_NORMAL_ACTIVE
|ZILOG_CALLOUT_ACTIVE
);
1504 wake_up_interruptible(&info
->open_wait
);
1508 * ------------------------------------------------------------
1509 * rs_open() and friends
1510 * ------------------------------------------------------------
1512 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1513 struct sgi_serial
*info
)
1515 DECLARE_WAITQUEUE(wait
, current
);
1520 * If the device is in the middle of being closed, then block
1521 * until it's done, and then try again.
1523 if (info
->flags
& ZILOG_CLOSING
) {
1524 interruptible_sleep_on(&info
->close_wait
);
1525 #ifdef SERIAL_DO_RESTART
1526 if (info
->flags
& ZILOG_HUP_NOTIFY
)
1529 return -ERESTARTSYS
;
1536 * If this is a callout device, then just make sure the normal
1537 * device isn't being used.
1539 if (tty
->driver
.subtype
== SERIAL_TYPE_CALLOUT
) {
1540 if (info
->flags
& ZILOG_NORMAL_ACTIVE
)
1542 if ((info
->flags
& ZILOG_CALLOUT_ACTIVE
) &&
1543 (info
->flags
& ZILOG_SESSION_LOCKOUT
) &&
1544 (info
->session
!= current
->session
))
1546 if ((info
->flags
& ZILOG_CALLOUT_ACTIVE
) &&
1547 (info
->flags
& ZILOG_PGRP_LOCKOUT
) &&
1548 (info
->pgrp
!= current
->pgrp
))
1550 info
->flags
|= ZILOG_CALLOUT_ACTIVE
;
1555 * If non-blocking mode is set, or the port is not enabled,
1556 * then make the check up front and then exit.
1558 if ((filp
->f_flags
& O_NONBLOCK
) ||
1559 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1560 if (info
->flags
& ZILOG_CALLOUT_ACTIVE
)
1562 info
->flags
|= ZILOG_NORMAL_ACTIVE
;
1566 if (info
->flags
& ZILOG_CALLOUT_ACTIVE
) {
1567 if (info
->normal_termios
.c_cflag
& CLOCAL
)
1570 if (tty
->termios
->c_cflag
& CLOCAL
)
1575 * Block waiting for the carrier detect and the line to become
1576 * free (i.e., not in use by the callout). While we are in
1577 * this loop, info->count is dropped by one, so that
1578 * rs_close() knows when to free things. We restore it upon
1579 * exit, either normal or abnormal.
1582 add_wait_queue(&info
->open_wait
, &wait
);
1583 #ifdef SERIAL_DEBUG_OPEN
1584 printk("block_til_ready before block: ttys%d, count = %d\n",
1585 info
->line
, info
->count
);
1588 info
->blocked_open
++;
1591 if (!(info
->flags
& ZILOG_CALLOUT_ACTIVE
))
1594 set_current_state(TASK_INTERRUPTIBLE
);
1595 if (tty_hung_up_p(filp
) ||
1596 !(info
->flags
& ZILOG_INITIALIZED
)) {
1597 #ifdef SERIAL_DO_RESTART
1598 if (info
->flags
& ZILOG_HUP_NOTIFY
)
1601 retval
= -ERESTARTSYS
;
1607 if (!(info
->flags
& ZILOG_CALLOUT_ACTIVE
) &&
1608 !(info
->flags
& ZILOG_CLOSING
) && do_clocal
)
1610 if (signal_pending(current
)) {
1611 retval
= -ERESTARTSYS
;
1614 #ifdef SERIAL_DEBUG_OPEN
1615 printk("block_til_ready blocking: ttys%d, count = %d\n",
1616 info
->line
, info
->count
);
1620 current
->state
= TASK_RUNNING
;
1621 remove_wait_queue(&info
->open_wait
, &wait
);
1622 if (!tty_hung_up_p(filp
))
1624 info
->blocked_open
--;
1625 #ifdef SERIAL_DEBUG_OPEN
1626 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1627 info
->line
, info
->count
);
1631 info
->flags
|= ZILOG_NORMAL_ACTIVE
;
1636 * This routine is called whenever a serial port is opened. It
1637 * enables interrupts for a serial port, linking in its ZILOG structure into
1638 * the IRQ chain. It also performs the serial-specific
1639 * initialization for the tty structure.
1641 int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1643 struct sgi_serial
*info
;
1646 line
= MINOR(tty
->device
) - tty
->driver
.minor_start
;
1647 /* The zilog lines for the mouse/keyboard must be
1648 * opened using their respective drivers.
1650 if ((line
< 0) || (line
>= NUM_CHANNELS
))
1652 info
= zs_soft
+ line
;
1653 /* Is the kgdb running over this line? */
1654 if (info
->kgdb_channel
)
1656 if (serial_paranoia_check(info
, tty
->device
, "rs_open"))
1658 #ifdef SERIAL_DEBUG_OPEN
1659 printk("rs_open %s%d, count = %d\n", tty
->driver
.name
, info
->line
,
1663 tty
->driver_data
= info
;
1667 * Start up serial port
1669 retval
= startup(info
);
1673 retval
= block_til_ready(tty
, filp
, info
);
1675 #ifdef SERIAL_DEBUG_OPEN
1676 printk("rs_open returning after block_til_ready with %d\n",
1682 if ((info
->count
== 1) && (info
->flags
& ZILOG_SPLIT_TERMIOS
)) {
1683 if (tty
->driver
.subtype
== SERIAL_TYPE_NORMAL
)
1684 *tty
->termios
= info
->normal_termios
;
1686 *tty
->termios
= info
->callout_termios
;
1690 info
->session
= current
->session
;
1691 info
->pgrp
= current
->pgrp
;
1693 #ifdef SERIAL_DEBUG_OPEN
1694 printk("rs_open ttys%d successful...", info
->line
);
1699 /* Finally, routines used to initialize the serial driver. */
1701 static void show_serial_version(void)
1703 printk("SGI Zilog8530 serial driver version 1.00\n");
1706 /* Return layout for the requested zs chip number. */
1707 static inline struct sgi_zslayout
*get_zs(int chip
)
1709 extern struct hpc3_miscregs
*hpc3mregs
;
1712 prom_printf("Wheee, bogus zs chip number requested.\n");
1716 return (struct sgi_zslayout
*) (&hpc3mregs
->ser1cmd
);
1722 rs_cons_check(struct sgi_serial
*ss
, int channel
)
1725 static int msg_printed
= 0;
1729 /* Is this one of the serial console lines? */
1730 if((zs_cons_chanout
!= channel
) &&
1731 (zs_cons_chanin
!= channel
))
1733 zs_conschan
= ss
->zs_channel
;
1738 /* If this is console input, we handle the break received
1739 * status interrupt on this line to mean prom_halt().
1741 if(zs_cons_chanin
== channel
) {
1742 ss
->break_abort
= 1;
1747 if(ss
->zs_baud
!= 9562) { /* Don't ask... */
1748 prom_printf("BAD console baud rate %d\n", ss
->zs_baud
);
1751 panic("Console baud rate weirdness");
1755 /* Set flag variable for this port so that it cannot be
1756 * opened for other uses by accident.
1762 printk("zs%d: console I/O\n", ((channel
>>1)&1));
1767 printk("zs%d: console %s\n", ((channel
>>1)&1),
1768 (i
==1 ? "input" : (o
==1 ? "output" : "WEIRD")));
1773 volatile int test_done
;
1775 /* rs_init inits the driver */
1778 int chip
, channel
, i
, flags
;
1779 struct sgi_serial
*info
;
1782 /* Setup base handler, and timer table. */
1783 init_bh(SERIAL_BH
, do_serial_bh
);
1784 timer_table
[RS_TIMER
].fn
= rs_timer
;
1785 timer_table
[RS_TIMER
].expires
= 0;
1787 show_serial_version();
1789 /* Initialize the tty_driver structure */
1790 /* SGI: Not all of this is exactly right for us. */
1792 memset(&serial_driver
, 0, sizeof(struct tty_driver
));
1793 serial_driver
.magic
= TTY_DRIVER_MAGIC
;
1794 serial_driver
.name
= "ttyS";
1795 serial_driver
.major
= TTY_MAJOR
;
1796 serial_driver
.minor_start
= 64;
1797 serial_driver
.num
= NUM_CHANNELS
;
1798 serial_driver
.type
= TTY_DRIVER_TYPE_SERIAL
;
1799 serial_driver
.subtype
= SERIAL_TYPE_NORMAL
;
1800 serial_driver
.init_termios
= tty_std_termios
;
1802 serial_driver
.init_termios
.c_cflag
=
1803 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1804 serial_driver
.flags
= TTY_DRIVER_REAL_RAW
;
1805 serial_driver
.refcount
= &serial_refcount
;
1806 serial_driver
.table
= serial_table
;
1807 serial_driver
.termios
= serial_termios
;
1808 serial_driver
.termios_locked
= serial_termios_locked
;
1810 serial_driver
.open
= rs_open
;
1811 serial_driver
.close
= rs_close
;
1812 serial_driver
.write
= rs_write
;
1813 serial_driver
.flush_chars
= rs_flush_chars
;
1814 serial_driver
.write_room
= rs_write_room
;
1815 serial_driver
.chars_in_buffer
= rs_chars_in_buffer
;
1816 serial_driver
.flush_buffer
= rs_flush_buffer
;
1817 serial_driver
.ioctl
= rs_ioctl
;
1818 serial_driver
.throttle
= rs_throttle
;
1819 serial_driver
.unthrottle
= rs_unthrottle
;
1820 serial_driver
.set_termios
= rs_set_termios
;
1821 serial_driver
.stop
= rs_stop
;
1822 serial_driver
.start
= rs_start
;
1823 serial_driver
.hangup
= rs_hangup
;
1826 * The callout device is just like normal device except for
1827 * major number and the subtype code.
1829 callout_driver
= serial_driver
;
1830 callout_driver
.name
= "cua";
1831 callout_driver
.major
= TTYAUX_MAJOR
;
1832 callout_driver
.subtype
= SERIAL_TYPE_CALLOUT
;
1834 if (tty_register_driver(&serial_driver
))
1835 panic("Couldn't register serial driver\n");
1836 if (tty_register_driver(&callout_driver
))
1837 panic("Couldn't register callout driver\n");
1839 save_flags(flags
); cli();
1841 /* Set up our interrupt linked list */
1842 zs_chain
= &zs_soft
[0];
1843 zs_soft
[0].zs_next
= &zs_soft
[1];
1844 zs_soft
[1].zs_next
= 0;
1846 for(chip
= 0; chip
< NUM_SERIAL
; chip
++) {
1847 /* If we are doing kgdb over one of the channels on
1848 * chip zero, kgdb_channel will be set to 1 by the
1849 * rs_kgdb_hook() routine below.
1851 if(!zs_chips
[chip
]) {
1852 zs_chips
[chip
] = get_zs(chip
);
1853 /* Two channels per chip */
1854 zs_channels
[(chip
*2)] = &zs_chips
[chip
]->channelB
;
1855 zs_channels
[(chip
*2)+1] = &zs_chips
[chip
]->channelA
;
1856 zs_soft
[(chip
*2)].kgdb_channel
= 0;
1857 zs_soft
[(chip
*2)+1].kgdb_channel
= 0;
1859 /* First, set up channel A on this chip. */
1861 zs_soft
[channel
].zs_channel
= zs_channels
[channel
];
1862 zs_soft
[channel
].change_needed
= 0;
1863 zs_soft
[channel
].clk_divisor
= 16;
1864 zs_soft
[channel
].zs_baud
= get_zsbaud(&zs_soft
[channel
]);
1865 zs_soft
[channel
].cons_mouse
= 0;
1866 /* If not keyboard/mouse and is console serial
1867 * line, then enable receiver interrupts.
1869 if(zs_soft
[channel
].is_cons
) {
1870 write_zsreg(zs_soft
[channel
].zs_channel
, R1
,
1871 (EXT_INT_ENAB
| INT_ALL_Rx
));
1872 write_zsreg(zs_soft
[channel
].zs_channel
, R9
, (NV
| MIE
));
1873 write_zsreg(zs_soft
[channel
].zs_channel
, R10
, (NRZ
));
1874 write_zsreg(zs_soft
[channel
].zs_channel
, R3
, (Rx8
|RxENABLE
));
1875 write_zsreg(zs_soft
[channel
].zs_channel
, R5
, (Tx8
| TxENAB
));
1877 /* If this is the kgdb line, enable interrupts because we
1878 * now want to receive the 'control-c' character from the
1879 * client attached to us asynchronously.
1881 if(zs_soft
[channel
].kgdb_channel
)
1882 kgdb_chaninit(&zs_soft
[channel
], 1,
1883 zs_soft
[channel
].zs_baud
);
1885 /* Now, channel B */
1887 zs_soft
[channel
].zs_channel
= zs_channels
[channel
];
1888 zs_soft
[channel
].change_needed
= 0;
1889 zs_soft
[channel
].clk_divisor
= 16;
1890 zs_soft
[channel
].zs_baud
= get_zsbaud(&zs_soft
[channel
]);
1891 zs_soft
[channel
].cons_keyb
= 0;
1892 /* If console serial line, then enable receiver interrupts. */
1893 if(zs_soft
[channel
].is_cons
) {
1894 write_zsreg(zs_soft
[channel
].zs_channel
, R1
,
1895 (EXT_INT_ENAB
| INT_ALL_Rx
));
1896 write_zsreg(zs_soft
[channel
].zs_channel
, R9
,
1898 write_zsreg(zs_soft
[channel
].zs_channel
, R10
,
1900 write_zsreg(zs_soft
[channel
].zs_channel
, R3
,
1902 write_zsreg(zs_soft
[channel
].zs_channel
, R5
,
1903 (Tx8
| TxENAB
| RTS
| DTR
));
1907 for(info
=zs_chain
, i
=0; info
; info
= info
->zs_next
, i
++)
1909 info
->magic
= SERIAL_MAGIC
;
1910 info
->port
= (int) info
->zs_channel
;
1913 info
->irq
= zilog_irq
;
1914 info
->custom_divisor
= 16;
1915 info
->close_delay
= 50;
1916 info
->closing_wait
= 3000;
1920 info
->blocked_open
= 0;
1921 info
->tqueue
.routine
= do_softint
;
1922 info
->tqueue
.data
= info
;
1923 info
->tqueue_hangup
.routine
= do_serial_hangup
;
1924 info
->tqueue_hangup
.data
= info
;
1925 info
->callout_termios
=callout_driver
.init_termios
;
1926 info
->normal_termios
= serial_driver
.init_termios
;
1927 init_waitqueue_head(&info
->open_wait
);
1928 init_waitqueue_head(&info
->close_wait
);
1929 printk("tty%02d at 0x%04x (irq = %d)", info
->line
,
1930 info
->port
, info
->irq
);
1931 printk(" is a Zilog8530\n");
1934 if (request_irq(zilog_irq
, rs_interrupt
, (SA_INTERRUPT
),
1935 "Zilog8530", zs_chain
))
1936 panic("Unable to attach zs intr\n");
1937 restore_flags(flags
);
1943 * register_serial and unregister_serial allows for serial ports to be
1944 * configured at run-time, to support PCMCIA modems.
1946 /* SGI: Unused at this time, just here to make things link. */
1947 int register_serial(struct serial_struct
*req
)
1952 void unregister_serial(int line
)
1957 /* Hooks for running a serial console. con_init() calls this if the
1958 * console is being run over one of the ttya/ttyb serial ports.
1959 * 'chip' should be zero, as chip 1 drives the mouse/keyboard.
1960 * 'channel' is decoded as 0=TTYA 1=TTYB, note that the channels
1961 * are addressed backwards, channel B is first, then channel A.
1964 rs_cons_hook(int chip
, int out
, int line
)
1970 panic("rs_cons_hook called with chip not zero");
1971 if(line
!= 0 && line
!= 1)
1972 panic("rs_cons_hook called with line not ttya or ttyb");
1974 if(!zs_chips
[chip
]) {
1975 zs_chips
[chip
] = get_zs(chip
);
1976 /* Two channels per chip */
1977 zs_channels
[(chip
*2)] = &zs_chips
[chip
]->channelB
;
1978 zs_channels
[(chip
*2)+1] = &zs_chips
[chip
]->channelA
;
1980 zs_soft
[channel
].zs_channel
= zs_channels
[channel
];
1981 zs_soft
[channel
].change_needed
= 0;
1982 zs_soft
[channel
].clk_divisor
= 16;
1983 zs_soft
[channel
].zs_baud
= get_zsbaud(&zs_soft
[channel
]);
1985 zs_cons_chanout
= ((chip
* 2) + channel
);
1987 zs_cons_chanin
= ((chip
* 2) + channel
);
1989 rs_cons_check(&zs_soft
[channel
], channel
);
1992 /* This is called at boot time to prime the kgdb serial debugging
1993 * serial line. The 'tty_num' argument is 0 for /dev/ttyd2 and 1 for
1994 * /dev/ttyd1 (yes they are backwards on purpose) which is determined
1995 * in setup_arch() from the boot command line flags.
1998 rs_kgdb_hook(int tty_num
)
2002 if(!zs_chips
[chip
]) {
2003 zs_chips
[chip
] = get_zs(chip
);
2004 /* Two channels per chip */
2005 zs_channels
[(chip
*2)] = &zs_chips
[chip
]->channelA
;
2006 zs_channels
[(chip
*2)+1] = &zs_chips
[chip
]->channelB
;
2008 zs_soft
[tty_num
].zs_channel
= zs_channels
[tty_num
];
2009 zs_kgdbchan
= zs_soft
[tty_num
].zs_channel
;
2010 zs_soft
[tty_num
].change_needed
= 0;
2011 zs_soft
[tty_num
].clk_divisor
= 16;
2012 zs_soft
[tty_num
].zs_baud
= get_zsbaud(&zs_soft
[tty_num
]);
2013 zs_soft
[tty_num
].kgdb_channel
= 1; /* This runs kgdb */
2014 zs_soft
[tty_num
^ 1].kgdb_channel
= 0; /* This does not */
2016 /* Turn on transmitter/receiver at 8-bits/char */
2017 kgdb_chaninit(&zs_soft
[tty_num
], 0, 9600);
2018 ZS_CLEARERR(zs_kgdbchan
);
2020 ZS_CLEARFIFO(zs_kgdbchan
);
2023 static void zs_console_write(struct console
*co
, const char *str
, unsigned int count
)
2029 rs_put_char(*str
++);
2032 /* Comment this if you want to have a strict interrupt-driven output */
2036 static int zs_console_wait_key(struct console
*con
)
2038 sleep_on(&keypress_wait
);
2042 static kdev_t
zs_console_device(struct console
*con
)
2044 return MKDEV(TTY_MAJOR
, 64 + con
->index
);
2048 static int __init
zs_console_setup(struct console
*con
, char *options
)
2050 struct sgi_serial
*info
;
2054 int cflag
= CREAD
| HUPCL
| CLOCAL
;
2059 baud
= simple_strtoul(options
, NULL
, 10);
2061 while(*s
>= '0' && *s
<= '9')
2063 if (*s
) parity
= *s
++;
2064 if (*s
) bits
= *s
- '0';
2068 * Now construct a cflag setting.
2116 rs_cons_hook(0, 0, con
->index
);
2117 info
= zs_soft
+ con
->index
;
2120 printk("Console: ttyS%d (Zilog8530)\n", info
->line
);
2122 i
= con
->cflag
& CBAUD
;
2123 if (con
->cflag
& CBAUDEX
) {
2125 con
->cflag
&= ~CBAUDEX
;
2127 info
->zs_baud
= baud
;
2129 switch (con
->cflag
& CSIZE
) {
2131 zscons_regs
[3] = Rx5
| RxENABLE
;
2132 zscons_regs
[5] = Tx5
| TxENAB
;
2135 zscons_regs
[3] = Rx6
| RxENABLE
;
2136 zscons_regs
[5] = Tx6
| TxENAB
;
2139 zscons_regs
[3] = Rx7
| RxENABLE
;
2140 zscons_regs
[5] = Tx7
| TxENAB
;
2144 zscons_regs
[3] = Rx8
| RxENABLE
;
2145 zscons_regs
[5] = Tx8
| TxENAB
;
2148 zscons_regs
[5] |= DTR
;
2150 if (con
->cflag
& PARENB
)
2151 zscons_regs
[4] |= PAR_ENA
;
2152 if (!(con
->cflag
& PARODD
))
2153 zscons_regs
[4] |= PAR_EVEN
;
2155 if (con
->cflag
& CSTOPB
)
2156 zscons_regs
[4] |= SB2
;
2158 zscons_regs
[4] |= SB1
;
2160 brg
= BPS_TO_BRG(baud
, ZS_CLOCK
/ info
->clk_divisor
);
2161 zscons_regs
[12] = brg
& 0xff;
2162 zscons_regs
[13] = (brg
>> 8) & 0xff;
2163 memcpy(info
->curregs
, zscons_regs
, sizeof(zscons_regs
));
2164 memcpy(info
->pendregs
, zscons_regs
, sizeof(zscons_regs
));
2165 load_zsregs(info
->zs_channel
, zscons_regs
);
2166 ZS_CLEARERR(info
->zs_channel
);
2167 ZS_CLEARFIFO(info
->zs_channel
);
2171 static struct console sgi_console_driver
= {
2173 zs_console_write
, /* write */
2175 zs_console_device
, /* device */
2176 zs_console_wait_key
, /* wait_key */
2178 zs_console_setup
, /* setup */
2188 long __init
serial_console_init(long kmem_start
, long kmem_end
)
2190 register_console(&sgi_console_driver
);