1 /* sgiserial.c: Serial port driver for SGI machines.
3 * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
7 * Note: This driver seems to have been derived from some
8 * version of the sbus/char/zs.c driver. A lot of clean-up
9 * and bug fixes seem to have happened to the Sun driver in
10 * the intervening time. As of 21.09.1999, I have merged in
11 * ONLY the changes necessary to fix observed functional
12 * problems on the Indy. Someone really ought to do a
13 * thorough pass to merge in the rest of the updates.
14 * Better still, someone really ought to make it a common
15 * code module for both platforms. kevink@mips.com
18 #include <linux/config.h> /* for CONFIG_REMOTE_DEBUG */
19 #include <linux/errno.h>
20 #include <linux/signal.h>
21 #include <linux/sched.h>
22 #include <linux/timer.h>
23 #include <linux/interrupt.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/major.h>
27 #include <linux/string.h>
28 #include <linux/fcntl.h>
30 #include <linux/kernel.h>
31 #include <linux/delay.h>
32 #include <linux/console.h>
33 #include <linux/init.h>
37 #include <asm/sgialib.h>
38 #include <asm/system.h>
39 #include <asm/bitops.h>
40 #include <asm/sgi/sgihpc.h>
41 #include <asm/sgi/sgint23.h>
42 #include <asm/uaccess.h>
44 #include "sgiserial.h"
46 #define NUM_SERIAL 1 /* One chip on board. */
47 #define NUM_CHANNELS (NUM_SERIAL * 2)
49 extern wait_queue_head_t keypress_wait
;
51 struct sgi_zslayout
*zs_chips
[NUM_SERIAL
] = { 0, };
52 struct sgi_zschannel
*zs_channels
[NUM_CHANNELS
] = { 0, 0, };
53 struct sgi_zschannel
*zs_conschan
;
54 struct sgi_zschannel
*zs_kgdbchan
;
56 struct sgi_serial zs_soft
[NUM_CHANNELS
];
57 struct sgi_serial
*zs_chain
; /* IRQ servicing chain */
58 static int zilog_irq
= SGI_SERIAL_IRQ
;
60 /* Console hooks... */
61 static int zs_cons_chanout
= 0;
62 static int zs_cons_chanin
= 0;
63 struct sgi_serial
*zs_consinfo
= 0;
65 static unsigned char kgdb_regs
[16] = {
66 0, 0, 0, /* write 0, 1, 2 */
67 (Rx8
| RxENABLE
), /* write 3 */
68 (X16CLK
| SB1
| PAR_EVEN
), /* write 4 */
69 (Tx8
| TxENAB
), /* write 5 */
70 0, 0, 0, /* write 6, 7, 8 */
73 (TCBR
| RCBR
), /* write 11 */
74 0, 0, /* BRG time constant, write 12 + 13 */
75 (BRENABL
), /* write 14 */
76 (DCDIE
) /* write 15 */
79 static unsigned char zscons_regs
[16] = {
81 (EXT_INT_ENAB
| INT_ALL_Rx
), /* write 1 */
83 (Rx8
| RxENABLE
), /* write 3 */
84 (X16CLK
), /* write 4 */
85 (DTR
| Tx8
| TxENAB
), /* write 5 */
86 0, 0, 0, /* write 6, 7, 8 */
87 (NV
| MIE
), /* write 9 */
89 (TCBR
| RCBR
), /* write 11 */
90 0, 0, /* BRG time constant, write 12 + 13 */
91 (BRENABL
), /* write 14 */
92 (DCDIE
| CTSIE
| TxUIE
| BRKIE
) /* write 15 */
95 #define ZS_CLOCK 3672000 /* Zilog input clock rate */
97 DECLARE_TASK_QUEUE(tq_serial
);
99 struct tty_driver serial_driver
, callout_driver
;
100 static int serial_refcount
;
102 /* serial subtype definitions */
103 #define SERIAL_TYPE_NORMAL 1
104 #define SERIAL_TYPE_CALLOUT 2
106 /* number of characters left in xmit buffer before we ask for more */
107 #define WAKEUP_CHARS 256
109 /* Debugging... DEBUG_INTR is bad to use when one of the zs
110 * lines is your console ;(
112 #undef SERIAL_DEBUG_INTR
113 #undef SERIAL_DEBUG_OPEN
114 #undef SERIAL_DEBUG_FLOW
116 #define RS_STROBE_TIME 10
117 #define RS_ISR_PASS_LIMIT 256
119 #define _INLINE_ inline
121 static void change_speed(struct sgi_serial
*info
);
123 static struct tty_struct
*serial_table
[NUM_CHANNELS
];
124 static struct termios
*serial_termios
[NUM_CHANNELS
];
125 static struct termios
*serial_termios_locked
[NUM_CHANNELS
];
128 #define MIN(a,b) ((a) < (b) ? (a) : (b))
132 * tmp_buf is used as a temporary buffer by serial_write. We need to
133 * lock it in case the memcpy_fromfs blocks while swapping in a page,
134 * and some other program tries to do a serial write at the same time.
135 * Since the lock will only come under contention when the system is
136 * swapping and available memory is low, it makes sense to share one
137 * buffer across all the serial ports, since it significantly saves
138 * memory if large numbers of serial ports are open.
140 static unsigned char tmp_buf
[4096]; /* This is cheating */
141 static DECLARE_MUTEX(tmp_buf_sem
);
143 static inline int serial_paranoia_check(struct sgi_serial
*info
,
144 dev_t device
, const char *routine
)
146 #ifdef SERIAL_PARANOIA_CHECK
147 static const char *badmagic
=
148 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
149 static const char *badinfo
=
150 "Warning: null sun_serial for (%d, %d) in %s\n";
153 printk(badinfo
, MAJOR(device
), MINOR(device
), routine
);
156 if (info
->magic
!= SERIAL_MAGIC
) {
157 printk(badmagic
, MAJOR(device
), MINOR(device
), routine
);
165 * This is used to figure out the divisor speeds and the timeouts
167 static int baud_table
[] = {
168 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
169 9600, 19200, 38400, 57600, 115200, 0 };
172 * Reading and writing Zilog8530 registers. The delays are to make this
173 * driver work on the Sun4 which needs a settling delay after each chip
174 * register access, other machines handle this in hardware via auxiliary
175 * flip-flops which implement the settle time we do in software.
177 * read_zsreg() and write_zsreg() may get called from rs_kgdb_hook() before
178 * interrupts are enabled. Therefore we have to check ioc_iocontrol before we
181 static inline unsigned char read_zsreg(struct sgi_zschannel
*channel
, unsigned char reg
)
183 unsigned char retval
;
184 volatile unsigned char junk
;
187 channel
->control
= reg
;
189 junk
= ioc_icontrol
->istat0
;
191 retval
= channel
->control
;
195 static inline void write_zsreg(struct sgi_zschannel
*channel
, unsigned char reg
, unsigned char value
)
197 volatile unsigned char junk
;
200 channel
->control
= reg
;
202 junk
= ioc_icontrol
->istat0
;
204 channel
->control
= value
;
206 junk
= ioc_icontrol
->istat0
;
210 static inline void load_zsregs(struct sgi_zschannel
*channel
, unsigned char *regs
)
212 ZS_CLEARERR(channel
);
213 ZS_CLEARFIFO(channel
);
215 write_zsreg(channel
, R4
, regs
[R4
]);
216 write_zsreg(channel
, R10
, regs
[R10
]);
217 write_zsreg(channel
, R3
, regs
[R3
] & ~RxENABLE
);
218 write_zsreg(channel
, R5
, regs
[R5
] & ~TxENAB
);
219 write_zsreg(channel
, R1
, regs
[R1
]);
220 write_zsreg(channel
, R9
, regs
[R9
]);
221 write_zsreg(channel
, R11
, regs
[R11
]);
222 write_zsreg(channel
, R12
, regs
[R12
]);
223 write_zsreg(channel
, R13
, regs
[R13
]);
224 write_zsreg(channel
, R14
, regs
[R14
]);
225 write_zsreg(channel
, R15
, regs
[R15
]);
226 write_zsreg(channel
, R3
, regs
[R3
]);
227 write_zsreg(channel
, R5
, regs
[R5
]);
231 /* Sets or clears DTR/RTS on the requested line */
232 static inline void zs_rtsdtr(struct sgi_serial
*ss
, int set
)
235 ss
->curregs
[5] |= (RTS
| DTR
);
236 ss
->pendregs
[5] = ss
->curregs
[5];
237 write_zsreg(ss
->zs_channel
, 5, ss
->curregs
[5]);
239 ss
->curregs
[5] &= ~(RTS
| DTR
);
240 ss
->pendregs
[5] = ss
->curregs
[5];
241 write_zsreg(ss
->zs_channel
, 5, ss
->curregs
[5]);
246 static inline void kgdb_chaninit(struct sgi_serial
*ss
, int intson
, int bps
)
251 kgdb_regs
[R1
] = INT_ALL_Rx
;
252 kgdb_regs
[R9
] |= MIE
;
255 kgdb_regs
[R9
] &= ~MIE
;
257 brg
= BPS_TO_BRG(bps
, ZS_CLOCK
/ss
->clk_divisor
);
258 kgdb_regs
[R12
] = (brg
& 255);
259 kgdb_regs
[R13
] = ((brg
>> 8) & 255);
260 load_zsregs(ss
->zs_channel
, kgdb_regs
);
263 /* Utility routines for the Zilog */
264 static inline int get_zsbaud(struct sgi_serial
*ss
)
266 struct sgi_zschannel
*channel
= ss
->zs_channel
;
269 /* The baud rate is split up between two 8-bit registers in
270 * what is termed 'BRG time constant' format in my docs for
271 * the chip, it is a function of the clk rate the chip is
272 * receiving which happens to be constant.
274 brg
= ((read_zsreg(channel
, 13)&0xff) << 8);
275 brg
|= (read_zsreg(channel
, 12)&0xff);
276 return BRG_TO_BPS(brg
, (ZS_CLOCK
/(ss
->clk_divisor
)));
280 * ------------------------------------------------------------
281 * rs_stop() and rs_start()
283 * This routines are called before setting or resetting tty->stopped.
284 * They enable or disable transmitter interrupts, as necessary.
285 * ------------------------------------------------------------
287 static void rs_stop(struct tty_struct
*tty
)
289 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
292 if (serial_paranoia_check(info
, tty
->device
, "rs_stop"))
295 save_flags(flags
); cli();
296 if (info
->curregs
[5] & TxENAB
) {
297 info
->curregs
[5] &= ~TxENAB
;
298 info
->pendregs
[5] &= ~TxENAB
;
299 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
301 restore_flags(flags
);
304 static void rs_start(struct tty_struct
*tty
)
306 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
309 if (serial_paranoia_check(info
, tty
->device
, "rs_start"))
312 save_flags(flags
); cli();
313 if (info
->xmit_cnt
&& info
->xmit_buf
&& !(info
->curregs
[5] & TxENAB
)) {
314 info
->curregs
[5] |= TxENAB
;
315 info
->pendregs
[5] = info
->curregs
[5];
316 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
318 restore_flags(flags
);
321 /* Drop into either the boot monitor or kadb upon receiving a break
322 * from keyboard/console input.
324 static void batten_down_hatches(void)
328 /* If we are doing kadb, we call the debugger
329 * else we just drop into the boot monitor.
330 * Note that we must flush the user windows
331 * first before giving up control.
334 if((((unsigned long)linux_dbvec
)>=DEBUG_FIRSTVADDR
) &&
335 (((unsigned long)linux_dbvec
)<=DEBUG_LASTVADDR
))
340 /* XXX We want to notify the keyboard driver that all
341 * XXX keys are in the up state or else weird things
348 /* On receive, this clears errors and the receiver interrupts */
349 static inline void rs_recv_clear(struct sgi_zschannel
*zsc
)
351 volatile unsigned char junk
;
354 zsc
->control
= ERR_RES
;
355 junk
= ioc_icontrol
->istat0
;
357 zsc
->control
= RES_H_IUS
;
358 junk
= ioc_icontrol
->istat0
;
362 * ----------------------------------------------------------------------
364 * Here starts the interrupt handling routines. All of the following
365 * subroutines are declared as inline and are folded into
366 * rs_interrupt(). They were separated out for readability's sake.
368 * Note: rs_interrupt() is a "fast" interrupt, which means that it
369 * runs with interrupts turned off. People who may want to modify
370 * rs_interrupt() should try to keep the interrupt handler as fast as
371 * possible. After you are done making modifications, it is not a bad
374 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
376 * and look at the resulting assemble code in serial.s.
378 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
379 * -----------------------------------------------------------------------
383 * This routine is used by the interrupt handler to schedule
384 * processing in the software interrupt portion of the driver.
386 static _INLINE_
void rs_sched_event(struct sgi_serial
*info
,
389 info
->event
|= 1 << event
;
390 queue_task(&info
->tqueue
, &tq_serial
);
394 #ifdef CONFIG_REMOTE_DEBUG
395 extern void set_async_breakpoint(unsigned int epc
);
398 static _INLINE_
void receive_chars(struct sgi_serial
*info
, struct pt_regs
*regs
)
400 struct tty_struct
*tty
= info
->tty
;
401 volatile unsigned char junk
;
402 unsigned char ch
, stat
;
405 ch
= info
->zs_channel
->data
;
406 junk
= ioc_icontrol
->istat0
;
408 stat
= read_zsreg(info
->zs_channel
, R1
);
410 /* If this is the console keyboard, we need to handle
414 if(ch
==0) { /* whee, break received */
415 batten_down_hatches();
416 rs_recv_clear(info
->zs_channel
);
418 } else if (ch
== 1) {
421 } else if (ch
== 2) {
425 /* It is a 'keyboard interrupt' ;-) */
426 wake_up(&keypress_wait
);
428 /* Look for kgdb 'stop' character, consult the gdb documentation
429 * for remote target debugging and arch/sparc/kernel/sparc-stub.c
430 * to see how all this works.
432 #ifdef CONFIG_REMOTE_DEBUG
433 if((info
->kgdb_channel
) && (ch
=='\003')) {
434 set_async_breakpoint(read_32bit_cp0_register(CP0_EPC
));
441 if (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
)
442 queue_task(&tty
->flip
.tqueue
, &tq_timer
);
445 *tty
->flip
.flag_buf_ptr
++ = TTY_PARITY
;
446 else if(stat
& Rx_OVR
)
447 *tty
->flip
.flag_buf_ptr
++ = TTY_OVERRUN
;
448 else if(stat
& CRC_ERR
)
449 *tty
->flip
.flag_buf_ptr
++ = TTY_FRAME
;
451 *tty
->flip
.flag_buf_ptr
++ = 0; /* XXX */
452 *tty
->flip
.char_buf_ptr
++ = ch
;
454 queue_task(&tty
->flip
.tqueue
, &tq_timer
);
457 rs_recv_clear(info
->zs_channel
);
461 static _INLINE_
void transmit_chars(struct sgi_serial
*info
)
463 volatile unsigned char junk
;
465 /* P3: In theory we have to test readiness here because a
466 * serial console can clog the chip through zs_cons_put_char().
467 * David did not do this. I think he relies on 3-chars FIFO in 8530.
468 * Let's watch for lost _output_ characters. XXX
471 /* SGI ADDENDUM: On most SGI machines, the Zilog does possess
472 * a 16 or 17 byte fifo, so no worries. -dm
478 info
->zs_channel
->data
= info
->x_char
;
479 junk
= ioc_icontrol
->istat0
;
482 goto clear_and_return
;
485 if((info
->xmit_cnt
<= 0) || info
->tty
->stopped
) {
486 /* That's peculiar... */
488 info
->zs_channel
->control
= RES_Tx_P
;
489 junk
= ioc_icontrol
->istat0
;
490 goto clear_and_return
;
495 info
->zs_channel
->data
= info
->xmit_buf
[info
->xmit_tail
++];
496 junk
= ioc_icontrol
->istat0
;
498 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
501 if (info
->xmit_cnt
< WAKEUP_CHARS
)
502 rs_sched_event(info
, RS_EVENT_WRITE_WAKEUP
);
504 if(info
->xmit_cnt
<= 0) {
506 info
->zs_channel
->control
= RES_Tx_P
;
507 junk
= ioc_icontrol
->istat0
;
508 goto clear_and_return
;
512 /* Clear interrupt */
514 info
->zs_channel
->control
= RES_H_IUS
;
515 junk
= ioc_icontrol
->istat0
;
519 static _INLINE_
void status_handle(struct sgi_serial
*info
)
521 volatile unsigned char junk
;
522 unsigned char status
;
524 /* Get status from Read Register 0 */
526 status
= info
->zs_channel
->control
;
527 junk
= ioc_icontrol
->istat0
;
528 /* Clear status condition... */
530 info
->zs_channel
->control
= RES_EXT_INT
;
531 junk
= ioc_icontrol
->istat0
;
532 /* Clear the interrupt */
534 info
->zs_channel
->control
= RES_H_IUS
;
535 junk
= ioc_icontrol
->istat0
;
539 if((info
->tty
->termios
->c_cflag
& CRTSCTS
) &&
540 ((info
->curregs
[3] & AUTO_ENAB
)==0)) {
541 info
->curregs
[3] |= AUTO_ENAB
;
542 info
->pendregs
[3] |= AUTO_ENAB
;
543 write_zsreg(info
->zs_channel
, 3, info
->curregs
[3]);
546 if((info
->curregs
[3] & AUTO_ENAB
)) {
547 info
->curregs
[3] &= ~AUTO_ENAB
;
548 info
->pendregs
[3] &= ~AUTO_ENAB
;
549 write_zsreg(info
->zs_channel
, 3, info
->curregs
[3]);
553 /* Whee, if this is console input and this is a
554 * 'break asserted' status change interrupt, call
557 if((status
& BRK_ABRT
) && info
->break_abort
)
558 batten_down_hatches();
560 /* XXX Whee, put in a buffer somewhere, the status information
561 * XXX whee whee whee... Where does the information go...
567 * This is the serial driver's generic interrupt routine
569 void rs_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
571 struct sgi_serial
* info
= (struct sgi_serial
*) dev_id
;
572 unsigned char zs_intreg
;
574 zs_intreg
= read_zsreg(info
->zs_next
->zs_channel
, 3);
576 /* NOTE: The read register 3, which holds the irq status,
577 * does so for both channels on each chip. Although
578 * the status value itself must be read from the A
579 * channel and is only valid when read from channel A.
580 * Yes... broken hardware...
582 #define CHAN_A_IRQMASK (CHARxIP | CHATxIP | CHAEXT)
583 #define CHAN_B_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
586 /* Channel B -- /dev/ttyb, could be the console */
587 if(zs_intreg
& CHAN_B_IRQMASK
) {
588 if (zs_intreg
& CHBRxIP
)
589 receive_chars(info
, regs
);
590 if (zs_intreg
& CHBTxIP
)
591 transmit_chars(info
);
592 if (zs_intreg
& CHBEXT
)
598 /* Channel A -- /dev/ttya, could be the console */
599 if(zs_intreg
& CHAN_A_IRQMASK
) {
600 if (zs_intreg
& CHARxIP
)
601 receive_chars(info
, regs
);
602 if (zs_intreg
& CHATxIP
)
603 transmit_chars(info
);
604 if (zs_intreg
& CHAEXT
)
610 * -------------------------------------------------------------------
611 * Here ends the serial interrupt routines.
612 * -------------------------------------------------------------------
616 * This routine is used to handle the "bottom half" processing for the
617 * serial driver, known also the "software interrupt" processing.
618 * This processing is done at the kernel interrupt level, after the
619 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
620 * is where time-consuming activities which can not be done in the
621 * interrupt driver proper are done; the interrupt driver schedules
622 * them using rs_sched_event(), and they get done here.
624 static void do_serial_bh(void)
626 run_task_queue(&tq_serial
);
629 static void do_softint(void *private_
)
631 struct sgi_serial
*info
= (struct sgi_serial
*) private_
;
632 struct tty_struct
*tty
;
638 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
)) {
639 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
640 tty
->ldisc
.write_wakeup
)
641 (tty
->ldisc
.write_wakeup
)(tty
);
642 wake_up_interruptible(&tty
->write_wait
);
647 * This routine is called from the scheduler tqueue when the interrupt
648 * routine has signalled that a hangup has occurred. The path of
649 * hangup processing is:
651 * serial interrupt routine -> (scheduler tqueue) ->
652 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
655 static void do_serial_hangup(void *private_
)
657 struct sgi_serial
*info
= (struct sgi_serial
*) private_
;
658 struct tty_struct
*tty
;
668 static int startup(struct sgi_serial
* info
)
670 volatile unsigned char junk
;
673 if (info
->flags
& ZILOG_INITIALIZED
)
676 if (!info
->xmit_buf
) {
677 info
->xmit_buf
= (unsigned char *) get_free_page(GFP_KERNEL
);
682 save_flags(flags
); cli();
684 #ifdef SERIAL_DEBUG_OPEN
685 printk("starting up ttys%d (irq %d)...", info
->line
, info
->irq
);
689 * Clear the FIFO buffers and disable them
690 * (they will be reenabled in change_speed())
692 ZS_CLEARFIFO(info
->zs_channel
);
693 info
->xmit_fifo_size
= 1;
696 * Clear the interrupt registers.
699 info
->zs_channel
->control
= ERR_RES
;
700 junk
= ioc_icontrol
->istat0
;
702 info
->zs_channel
->control
= RES_H_IUS
;
703 junk
= ioc_icontrol
->istat0
;
706 * Now, initialize the Zilog
711 * Finally, enable sequencing and interrupts
713 info
->curregs
[1] |= (info
->curregs
[1] & ~0x18) | (EXT_INT_ENAB
|INT_ALL_Rx
);
714 info
->pendregs
[1] = info
->curregs
[1];
715 info
->curregs
[3] |= (RxENABLE
| Rx8
);
716 info
->pendregs
[3] = info
->curregs
[3];
717 /* We enable Tx interrupts as needed. */
718 info
->curregs
[5] |= (TxENAB
| Tx8
);
719 info
->pendregs
[5] = info
->curregs
[5];
720 info
->curregs
[9] |= (NV
| MIE
);
721 info
->pendregs
[9] = info
->curregs
[9];
722 write_zsreg(info
->zs_channel
, 3, info
->curregs
[3]);
723 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
724 write_zsreg(info
->zs_channel
, 9, info
->curregs
[9]);
727 * And clear the interrupt registers again for luck.
730 info
->zs_channel
->control
= ERR_RES
;
731 junk
= ioc_icontrol
->istat0
;
733 info
->zs_channel
->control
= RES_H_IUS
;
734 junk
= ioc_icontrol
->istat0
;
737 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
738 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
741 * and set the speed of the serial port
745 info
->flags
|= ZILOG_INITIALIZED
;
746 restore_flags(flags
);
751 * This routine will shutdown a serial port; interrupts are disabled, and
752 * DTR is dropped if the hangup on close termio flag is on.
754 static void shutdown(struct sgi_serial
* info
)
758 if (!(info
->flags
& ZILOG_INITIALIZED
))
761 #ifdef SERIAL_DEBUG_OPEN
762 printk("Shutting down serial port %d (irq %d)....", info
->line
,
766 save_flags(flags
); cli(); /* Disable interrupts */
768 if (info
->xmit_buf
) {
769 free_page((unsigned long) info
->xmit_buf
);
774 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
776 info
->flags
&= ~ZILOG_INITIALIZED
;
777 restore_flags(flags
);
781 * This routine is called to set the UART divisor registers to match
782 * the specified baud rate for a serial port.
784 static void change_speed(struct sgi_serial
*info
)
791 if (!info
->tty
|| !info
->tty
->termios
)
793 cflag
= info
->tty
->termios
->c_cflag
;
794 if (!(port
= info
->port
))
798 /* XXX CBAUDEX is not obeyed.
799 * It is impossible at a 32bits SPARC.
800 * But we have to report this to user ... someday.
805 /* XXX B0, hangup the line. */
806 do_serial_hangup(info
);
807 } else if (baud_table
[i
]) {
808 info
->zs_baud
= baud_table
[i
];
809 info
->clk_divisor
= 16;
811 info
->curregs
[4] = X16CLK
;
812 info
->curregs
[11] = TCBR
| RCBR
;
813 brg
= BPS_TO_BRG(info
->zs_baud
, ZS_CLOCK
/info
->clk_divisor
);
814 info
->curregs
[12] = (brg
& 255);
815 info
->curregs
[13] = ((brg
>> 8) & 255);
816 info
->curregs
[14] = BRENABL
;
819 /* byte size and parity */
820 switch (cflag
& CSIZE
) {
822 info
->curregs
[3] &= ~(0xc0);
823 info
->curregs
[3] |= Rx5
;
824 info
->pendregs
[3] = info
->curregs
[3];
825 info
->curregs
[5] &= ~(0xe0);
826 info
->curregs
[5] |= Tx5
;
827 info
->pendregs
[5] = info
->curregs
[5];
830 info
->curregs
[3] &= ~(0xc0);
831 info
->curregs
[3] |= Rx6
;
832 info
->pendregs
[3] = info
->curregs
[3];
833 info
->curregs
[5] &= ~(0xe0);
834 info
->curregs
[5] |= Tx6
;
835 info
->pendregs
[5] = info
->curregs
[5];
838 info
->curregs
[3] &= ~(0xc0);
839 info
->curregs
[3] |= Rx7
;
840 info
->pendregs
[3] = info
->curregs
[3];
841 info
->curregs
[5] &= ~(0xe0);
842 info
->curregs
[5] |= Tx7
;
843 info
->pendregs
[5] = info
->curregs
[5];
846 default: /* defaults to 8 bits */
847 info
->curregs
[3] &= ~(0xc0);
848 info
->curregs
[3] |= Rx8
;
849 info
->pendregs
[3] = info
->curregs
[3];
850 info
->curregs
[5] &= ~(0xe0);
851 info
->curregs
[5] |= Tx8
;
852 info
->pendregs
[5] = info
->curregs
[5];
855 info
->curregs
[4] &= ~(0x0c);
856 if (cflag
& CSTOPB
) {
857 info
->curregs
[4] |= SB2
;
859 info
->curregs
[4] |= SB1
;
861 info
->pendregs
[4] = info
->curregs
[4];
862 if (cflag
& PARENB
) {
863 info
->curregs
[4] |= PAR_ENA
;
864 info
->pendregs
[4] |= PAR_ENA
;
866 info
->curregs
[4] &= ~PAR_ENA
;
867 info
->pendregs
[4] &= ~PAR_ENA
;
869 if (!(cflag
& PARODD
)) {
870 info
->curregs
[4] |= PAR_EVEN
;
871 info
->pendregs
[4] |= PAR_EVEN
;
873 info
->curregs
[4] &= ~PAR_EVEN
;
874 info
->pendregs
[4] &= ~PAR_EVEN
;
877 /* Load up the new values */
878 load_zsregs(info
->zs_channel
, info
->curregs
);
883 /* This is for console output over ttya/ttyb */
884 static void zs_cons_put_char(char ch
)
886 struct sgi_zschannel
*chan
= zs_conschan
;
887 volatile unsigned char junk
;
888 int flags
, loops
= 0;
890 save_flags(flags
); cli();
891 while(((junk
= chan
->control
) & Tx_BUF_EMP
)==0 && loops
< 10000) {
898 junk
= ioc_icontrol
->istat0
;
899 restore_flags(flags
);
903 * This is the more generic put_char function for the driver.
904 * In earlier versions of this driver, "rs_put_char" was the
905 * name of the console-specific fucntion, now called zs_cons_put_char
908 static void rs_put_char(struct tty_struct
*tty
, char ch
)
910 struct sgi_zschannel
*chan
=
911 ((struct sgi_serial
*)tty
->driver_data
)->zs_channel
;
912 volatile unsigned char junk
;
913 int flags
, loops
= 0;
915 save_flags(flags
); cli();
916 while(((junk
= chan
->control
) & Tx_BUF_EMP
)==0 && loops
< 10000) {
923 junk
= ioc_icontrol
->istat0
;
924 restore_flags(flags
);
927 /* These are for receiving and sending characters under the kgdb
928 * source level kernel debugger.
930 int putDebugChar(char kgdb_char
)
932 struct sgi_zschannel
*chan
= zs_kgdbchan
;
933 volatile unsigned char junk
;
936 save_flags(flags
); cli();
938 while((chan
->control
& Tx_BUF_EMP
)==0)
942 chan
->data
= kgdb_char
;
943 junk
= ioc_icontrol
->istat0
;
944 restore_flags(flags
);
949 char getDebugChar(void)
951 struct sgi_zschannel
*chan
= zs_kgdbchan
;
954 while((chan
->control
& Rx_CH_AV
)==0)
957 junk
= ioc_icontrol
->istat0
;
963 * Fair output driver allows a process to speak.
965 static void rs_fair_output(void)
967 int left
; /* Output no more than that */
969 struct sgi_serial
*info
= zs_consinfo
;
970 volatile unsigned char junk
;
973 if (info
== 0) return;
974 if (info
->xmit_buf
== 0) return;
976 save_flags(flags
); cli();
977 left
= info
->xmit_cnt
;
979 c
= info
->xmit_buf
[info
->xmit_tail
];
980 info
->xmit_tail
= (info
->xmit_tail
+1) & (SERIAL_XMIT_SIZE
-1);
982 restore_flags(flags
);
986 save_flags(flags
); cli();
987 left
= MIN(info
->xmit_cnt
, left
-1);
990 /* Last character is being transmitted now (hopefully). */
992 zs_conschan
->control
= RES_Tx_P
;
993 junk
= ioc_icontrol
->istat0
;
995 restore_flags(flags
);
1000 static int rs_write(struct tty_struct
* tty
, int from_user
,
1001 const unsigned char *buf
, int count
)
1004 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1005 unsigned long flags
;
1007 if (serial_paranoia_check(info
, tty
->device
, "rs_write"))
1010 if (!tty
|| !info
->xmit_buf
)
1016 c
= MIN(count
, MIN(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
1017 SERIAL_XMIT_SIZE
- info
->xmit_head
));
1023 copy_from_user(tmp_buf
, buf
, c
);
1024 c
= MIN(c
, MIN(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
1025 SERIAL_XMIT_SIZE
- info
->xmit_head
));
1026 memcpy(info
->xmit_buf
+ info
->xmit_head
, tmp_buf
, c
);
1029 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
1030 info
->xmit_head
= (info
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
1031 info
->xmit_cnt
+= c
;
1032 restore_flags(flags
);
1038 if (info
->xmit_cnt
&& !tty
->stopped
&& !tty
->hw_stopped
) {
1040 * The above test used to include the condition
1041 * "&& !(info->curregs[5] & TxENAB)", but there
1042 * is reason to suspect that it is never statisfied
1043 * when the port is running. The problem may in fact
1044 * have been masked by the fact that, if O_POST is set,
1045 * there is always a rs_flush_xx operation following the
1046 * rs_write, and the flush ignores that condition when
1047 * it kicks off the transmit.
1049 /* Enable transmitter */
1050 info
->curregs
[1] |= TxINT_ENAB
|EXT_INT_ENAB
;
1051 info
->pendregs
[1] |= TxINT_ENAB
|EXT_INT_ENAB
;
1052 write_zsreg(info
->zs_channel
, 1, info
->curregs
[1]);
1053 info
->curregs
[5] |= TxENAB
;
1054 info
->pendregs
[5] |= TxENAB
;
1055 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
1058 * The following code is imported from the 2.3.6 Sun sbus zs.c
1059 * driver, of which an earlier version served as the basis
1060 * for sgiserial.c. Perhaps due to changes over time in
1061 * the line discipline code, ns_write()s with from_user
1062 * set would not otherwise actually kick-off output in
1063 * Linux 2.2.x or later. Maybe it never really worked.
1066 rs_put_char(tty
, info
->xmit_buf
[info
->xmit_tail
++]);
1067 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
1071 restore_flags(flags
);
1075 static int rs_write_room(struct tty_struct
*tty
)
1077 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1080 if (serial_paranoia_check(info
, tty
->device
, "rs_write_room"))
1082 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
1088 static int rs_chars_in_buffer(struct tty_struct
*tty
)
1090 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1092 if (serial_paranoia_check(info
, tty
->device
, "rs_chars_in_buffer"))
1094 return info
->xmit_cnt
;
1097 static void rs_flush_buffer(struct tty_struct
*tty
)
1099 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1101 if (serial_paranoia_check(info
, tty
->device
, "rs_flush_buffer"))
1104 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
1106 wake_up_interruptible(&tty
->write_wait
);
1107 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
1108 tty
->ldisc
.write_wakeup
)
1109 (tty
->ldisc
.write_wakeup
)(tty
);
1112 static void rs_flush_chars(struct tty_struct
*tty
)
1114 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1115 unsigned long flags
;
1117 if (serial_paranoia_check(info
, tty
->device
, "rs_flush_chars"))
1120 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| tty
->hw_stopped
||
1124 /* Enable transmitter */
1125 save_flags(flags
); cli();
1126 info
->curregs
[1] |= TxINT_ENAB
|EXT_INT_ENAB
;
1127 info
->pendregs
[1] |= TxINT_ENAB
|EXT_INT_ENAB
;
1128 write_zsreg(info
->zs_channel
, 1, info
->curregs
[1]);
1129 info
->curregs
[5] |= TxENAB
;
1130 info
->pendregs
[5] |= TxENAB
;
1131 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
1134 * Send a first (bootstrapping) character. A best solution is
1135 * to call transmit_chars() here which handles output in a
1136 * generic way. Current transmit_chars() not only transmits,
1137 * but resets interrupts also what we do not desire here.
1138 * XXX Discuss with David.
1140 if (info
->zs_channel
->control
& Tx_BUF_EMP
) {
1141 volatile unsigned char junk
;
1145 info
->zs_channel
->data
= info
->xmit_buf
[info
->xmit_tail
++];
1146 junk
= ioc_icontrol
->istat0
;
1147 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
1150 restore_flags(flags
);
1154 * ------------------------------------------------------------
1157 * This routine is called by the upper-layer tty layer to signal that
1158 * incoming characters should be throttled.
1159 * ------------------------------------------------------------
1161 static void rs_throttle(struct tty_struct
* tty
)
1163 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1164 #ifdef SERIAL_DEBUG_THROTTLE
1167 printk("throttle %s: %d....\n", _tty_name(tty
, buf
),
1168 tty
->ldisc
.chars_in_buffer(tty
));
1171 if (serial_paranoia_check(info
, tty
->device
, "rs_throttle"))
1175 info
->x_char
= STOP_CHAR(tty
);
1177 /* Turn off RTS line */
1179 info
->curregs
[5] &= ~RTS
;
1180 info
->pendregs
[5] &= ~RTS
;
1181 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
1185 static void rs_unthrottle(struct tty_struct
* tty
)
1187 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1188 #ifdef SERIAL_DEBUG_THROTTLE
1191 printk("unthrottle %s: %d....\n", _tty_name(tty
, buf
),
1192 tty
->ldisc
.chars_in_buffer(tty
));
1195 if (serial_paranoia_check(info
, tty
->device
, "rs_unthrottle"))
1202 info
->x_char
= START_CHAR(tty
);
1205 /* Assert RTS line */
1207 info
->curregs
[5] |= RTS
;
1208 info
->pendregs
[5] |= RTS
;
1209 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
1214 * ------------------------------------------------------------
1215 * rs_ioctl() and friends
1216 * ------------------------------------------------------------
1219 static int get_serial_info(struct sgi_serial
* info
,
1220 struct serial_struct
* retinfo
)
1222 struct serial_struct tmp
;
1226 memset(&tmp
, 0, sizeof(tmp
));
1227 tmp
.type
= info
->type
;
1228 tmp
.line
= info
->line
;
1229 tmp
.port
= info
->port
;
1230 tmp
.irq
= info
->irq
;
1231 tmp
.flags
= info
->flags
;
1232 tmp
.baud_base
= info
->baud_base
;
1233 tmp
.close_delay
= info
->close_delay
;
1234 tmp
.closing_wait
= info
->closing_wait
;
1235 tmp
.custom_divisor
= info
->custom_divisor
;
1236 return copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
));
1239 static int set_serial_info(struct sgi_serial
* info
,
1240 struct serial_struct
* new_info
)
1242 struct serial_struct new_serial
;
1243 struct sgi_serial old_info
;
1248 copy_from_user(&new_serial
,new_info
,sizeof(new_serial
));
1251 if (!capable(CAP_SYS_ADMIN
)) {
1252 if ((new_serial
.baud_base
!= info
->baud_base
) ||
1253 (new_serial
.type
!= info
->type
) ||
1254 (new_serial
.close_delay
!= info
->close_delay
) ||
1255 ((new_serial
.flags
& ~ZILOG_USR_MASK
) !=
1256 (info
->flags
& ~ZILOG_USR_MASK
)))
1258 info
->flags
= ((info
->flags
& ~ZILOG_USR_MASK
) |
1259 (new_serial
.flags
& ZILOG_USR_MASK
));
1260 info
->custom_divisor
= new_serial
.custom_divisor
;
1261 goto check_and_exit
;
1264 if (info
->count
> 1)
1268 * OK, past this point, all the error checking has been done.
1269 * At this point, we start making changes.....
1272 info
->baud_base
= new_serial
.baud_base
;
1273 info
->flags
= ((info
->flags
& ~ZILOG_FLAGS
) |
1274 (new_serial
.flags
& ZILOG_FLAGS
));
1275 info
->type
= new_serial
.type
;
1276 info
->close_delay
= new_serial
.close_delay
;
1277 info
->closing_wait
= new_serial
.closing_wait
;
1280 retval
= startup(info
);
1285 * get_lsr_info - get line status register info
1287 * Purpose: Let user call ioctl() to get info when the UART physically
1288 * is emptied. On bus types like RS485, the transmitter must
1289 * release the bus after transmitting. This must be done when
1290 * the transmit shift register is empty, not be done when the
1291 * transmit holding register is empty. This functionality
1292 * allows an RS485 driver to be written in user space.
1294 static int get_lsr_info(struct sgi_serial
* info
, unsigned int *value
)
1296 volatile unsigned char junk
;
1297 unsigned char status
;
1301 status
= info
->zs_channel
->control
;
1302 junk
= ioc_icontrol
->istat0
;
1304 return put_user(status
,value
);
1308 * This routine sends a break character out the serial port.
1310 static void send_break( struct sgi_serial
* info
, int duration
)
1314 current
->state
= TASK_INTERRUPTIBLE
;
1316 write_zsreg(info
->zs_channel
, 5, (info
->curregs
[5] | SND_BRK
));
1317 schedule_timeout(duration
);
1318 write_zsreg(info
->zs_channel
, 5, info
->curregs
[5]);
1322 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
1323 unsigned int cmd
, unsigned long arg
)
1326 struct sgi_serial
* info
= (struct sgi_serial
*)tty
->driver_data
;
1329 if (serial_paranoia_check(info
, tty
->device
, "rs_ioctl"))
1332 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
1333 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
1334 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
1335 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1340 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1341 retval
= tty_check_change(tty
);
1344 tty_wait_until_sent(tty
, 0);
1346 send_break(info
, HZ
/4); /* 1/4 second */
1348 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1349 retval
= tty_check_change(tty
);
1352 tty_wait_until_sent(tty
, 0);
1353 send_break(info
, arg
? arg
*(HZ
/10) : HZ
/4);
1356 error
= verify_area(VERIFY_WRITE
, (void *) arg
,sizeof(long));
1359 put_user(C_CLOCAL(tty
) ? 1 : 0,
1360 (unsigned long *) arg
);
1363 error
= get_user(arg
, (unsigned long *)arg
);
1366 tty
->termios
->c_cflag
=
1367 ((tty
->termios
->c_cflag
& ~CLOCAL
) |
1368 (arg
? CLOCAL
: 0));
1371 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1372 sizeof(struct serial_struct
));
1375 return get_serial_info(info
,
1376 (struct serial_struct
*) arg
);
1378 return set_serial_info(info
,
1379 (struct serial_struct
*) arg
);
1380 case TIOCSERGETLSR
: /* Get line status register */
1381 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1382 sizeof(unsigned int));
1386 return get_lsr_info(info
, (unsigned int *) arg
);
1388 case TIOCSERGSTRUCT
:
1389 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1390 sizeof(struct sgi_serial
));
1393 copy_to_user((struct sun_serial
*) arg
,
1394 info
, sizeof(struct sgi_serial
));
1398 return -ENOIOCTLCMD
;
1403 static void rs_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
1405 struct sgi_serial
*info
= (struct sgi_serial
*)tty
->driver_data
;
1407 if (tty
->termios
->c_cflag
== old_termios
->c_cflag
)
1412 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1413 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1414 tty
->hw_stopped
= 0;
1420 * ------------------------------------------------------------
1423 * This routine is called when the serial port gets closed. First, we
1424 * wait for the last remaining data to be sent. Then, we unlink its
1425 * ZILOG structure from the interrupt chain if necessary, and we free
1426 * that IRQ if nothing is left in the chain.
1427 * ------------------------------------------------------------
1429 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1431 struct sgi_serial
* info
= (struct sgi_serial
*)tty
->driver_data
;
1432 unsigned long flags
;
1434 if (!info
|| serial_paranoia_check(info
, tty
->device
, "rs_close"))
1437 save_flags(flags
); cli();
1439 if (tty_hung_up_p(filp
)) {
1440 restore_flags(flags
);
1444 #ifdef SERIAL_DEBUG_OPEN
1445 printk("rs_close ttys%d, count = %d\n", info
->line
, info
->count
);
1447 if ((tty
->count
== 1) && (info
->count
!= 1)) {
1449 * Uh, oh. tty->count is 1, which means that the tty
1450 * structure will be freed. Info->count should always
1451 * be one in these conditions. If it's greater than
1452 * one, we've got real problems, since it means the
1453 * serial port won't be shutdown.
1455 printk("rs_close: bad serial port count; tty->count is 1, "
1456 "info->count is %d\n", info
->count
);
1459 if (--info
->count
< 0) {
1460 printk("rs_close: bad serial port count for ttys%d: %d\n",
1461 info
->line
, info
->count
);
1465 restore_flags(flags
);
1468 info
->flags
|= ZILOG_CLOSING
;
1470 * Save the termios structure, since this port may have
1471 * separate termios for callout and dialin.
1473 if (info
->flags
& ZILOG_NORMAL_ACTIVE
)
1474 info
->normal_termios
= *tty
->termios
;
1475 if (info
->flags
& ZILOG_CALLOUT_ACTIVE
)
1476 info
->callout_termios
= *tty
->termios
;
1478 * Now we wait for the transmit buffer to clear; and we notify
1479 * the line discipline to only process XON/XOFF characters.
1482 if (info
->closing_wait
!= ZILOG_CLOSING_WAIT_NONE
)
1483 tty_wait_until_sent(tty
, info
->closing_wait
);
1485 * At this point we stop accepting input. To do this, we
1486 * disable the receive line status interrupts, and tell the
1487 * interrupt driver to stop checking the data ready bit in the
1488 * line status register.
1490 /** if (!info->iscons) ... **/
1491 info
->curregs
[3] &= ~RxENABLE
;
1492 info
->pendregs
[3] = info
->curregs
[3];
1493 write_zsreg(info
->zs_channel
, 3, info
->curregs
[3]);
1494 info
->curregs
[1] &= ~(0x18);
1495 info
->pendregs
[1] = info
->curregs
[1];
1496 write_zsreg(info
->zs_channel
, 1, info
->curregs
[1]);
1497 ZS_CLEARFIFO(info
->zs_channel
);
1500 if (tty
->driver
.flush_buffer
)
1501 tty
->driver
.flush_buffer(tty
);
1502 if (tty
->ldisc
.flush_buffer
)
1503 tty
->ldisc
.flush_buffer(tty
);
1507 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
1508 if (tty
->ldisc
.close
)
1509 (tty
->ldisc
.close
)(tty
);
1510 tty
->ldisc
= ldiscs
[N_TTY
];
1511 tty
->termios
->c_line
= N_TTY
;
1512 if (tty
->ldisc
.open
)
1513 (tty
->ldisc
.open
)(tty
);
1515 if (info
->blocked_open
) {
1516 if (info
->close_delay
) {
1517 current
->state
= TASK_INTERRUPTIBLE
;
1518 schedule_timeout(info
->close_delay
);
1520 wake_up_interruptible(&info
->open_wait
);
1522 info
->flags
&= ~(ZILOG_NORMAL_ACTIVE
|ZILOG_CALLOUT_ACTIVE
|
1524 wake_up_interruptible(&info
->close_wait
);
1525 restore_flags(flags
);
1529 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1531 void rs_hangup(struct tty_struct
*tty
)
1533 struct sgi_serial
* info
= (struct sgi_serial
*)tty
->driver_data
;
1535 if (serial_paranoia_check(info
, tty
->device
, "rs_hangup"))
1538 rs_flush_buffer(tty
);
1542 info
->flags
&= ~(ZILOG_NORMAL_ACTIVE
|ZILOG_CALLOUT_ACTIVE
);
1544 wake_up_interruptible(&info
->open_wait
);
1548 * ------------------------------------------------------------
1549 * rs_open() and friends
1550 * ------------------------------------------------------------
1552 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1553 struct sgi_serial
*info
)
1555 DECLARE_WAITQUEUE(wait
, current
);
1560 * If the device is in the middle of being closed, then block
1561 * until it's done, and then try again.
1563 if (info
->flags
& ZILOG_CLOSING
) {
1564 interruptible_sleep_on(&info
->close_wait
);
1565 #ifdef SERIAL_DO_RESTART
1566 if (info
->flags
& ZILOG_HUP_NOTIFY
)
1569 return -ERESTARTSYS
;
1576 * If this is a callout device, then just make sure the normal
1577 * device isn't being used.
1579 if (tty
->driver
.subtype
== SERIAL_TYPE_CALLOUT
) {
1580 if (info
->flags
& ZILOG_NORMAL_ACTIVE
)
1582 if ((info
->flags
& ZILOG_CALLOUT_ACTIVE
) &&
1583 (info
->flags
& ZILOG_SESSION_LOCKOUT
) &&
1584 (info
->session
!= current
->session
))
1586 if ((info
->flags
& ZILOG_CALLOUT_ACTIVE
) &&
1587 (info
->flags
& ZILOG_PGRP_LOCKOUT
) &&
1588 (info
->pgrp
!= current
->pgrp
))
1590 info
->flags
|= ZILOG_CALLOUT_ACTIVE
;
1595 * If non-blocking mode is set, or the port is not enabled,
1596 * then make the check up front and then exit.
1598 if ((filp
->f_flags
& O_NONBLOCK
) ||
1599 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1600 if (info
->flags
& ZILOG_CALLOUT_ACTIVE
)
1602 info
->flags
|= ZILOG_NORMAL_ACTIVE
;
1606 if (info
->flags
& ZILOG_CALLOUT_ACTIVE
) {
1607 if (info
->normal_termios
.c_cflag
& CLOCAL
)
1610 if (tty
->termios
->c_cflag
& CLOCAL
)
1615 * Block waiting for the carrier detect and the line to become
1616 * free (i.e., not in use by the callout). While we are in
1617 * this loop, info->count is dropped by one, so that
1618 * rs_close() knows when to free things. We restore it upon
1619 * exit, either normal or abnormal.
1622 add_wait_queue(&info
->open_wait
, &wait
);
1623 #ifdef SERIAL_DEBUG_OPEN
1624 printk("block_til_ready before block: ttys%d, count = %d\n",
1625 info
->line
, info
->count
);
1628 info
->blocked_open
++;
1631 if (!(info
->flags
& ZILOG_CALLOUT_ACTIVE
))
1634 set_current_state(TASK_INTERRUPTIBLE
);
1635 if (tty_hung_up_p(filp
) ||
1636 !(info
->flags
& ZILOG_INITIALIZED
)) {
1637 #ifdef SERIAL_DO_RESTART
1638 if (info
->flags
& ZILOG_HUP_NOTIFY
)
1641 retval
= -ERESTARTSYS
;
1647 if (!(info
->flags
& ZILOG_CALLOUT_ACTIVE
) &&
1648 !(info
->flags
& ZILOG_CLOSING
) && do_clocal
)
1650 if (signal_pending(current
)) {
1651 retval
= -ERESTARTSYS
;
1654 #ifdef SERIAL_DEBUG_OPEN
1655 printk("block_til_ready blocking: ttys%d, count = %d\n",
1656 info
->line
, info
->count
);
1660 current
->state
= TASK_RUNNING
;
1661 remove_wait_queue(&info
->open_wait
, &wait
);
1662 if (!tty_hung_up_p(filp
))
1664 info
->blocked_open
--;
1665 #ifdef SERIAL_DEBUG_OPEN
1666 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1667 info
->line
, info
->count
);
1671 info
->flags
|= ZILOG_NORMAL_ACTIVE
;
1676 * This routine is called whenever a serial port is opened. It
1677 * enables interrupts for a serial port, linking in its ZILOG structure into
1678 * the IRQ chain. It also performs the serial-specific
1679 * initialization for the tty structure.
1681 int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1683 struct sgi_serial
*info
;
1686 line
= MINOR(tty
->device
) - tty
->driver
.minor_start
;
1687 /* The zilog lines for the mouse/keyboard must be
1688 * opened using their respective drivers.
1690 if ((line
< 0) || (line
>= NUM_CHANNELS
))
1692 info
= zs_soft
+ line
;
1693 /* Is the kgdb running over this line? */
1694 if (info
->kgdb_channel
)
1696 if (serial_paranoia_check(info
, tty
->device
, "rs_open"))
1698 #ifdef SERIAL_DEBUG_OPEN
1699 printk("rs_open %s%d, count = %d\n", tty
->driver
.name
, info
->line
,
1703 tty
->driver_data
= info
;
1707 * Start up serial port
1709 retval
= startup(info
);
1713 retval
= block_til_ready(tty
, filp
, info
);
1715 #ifdef SERIAL_DEBUG_OPEN
1716 printk("rs_open returning after block_til_ready with %d\n",
1722 if ((info
->count
== 1) && (info
->flags
& ZILOG_SPLIT_TERMIOS
)) {
1723 if (tty
->driver
.subtype
== SERIAL_TYPE_NORMAL
)
1724 *tty
->termios
= info
->normal_termios
;
1726 *tty
->termios
= info
->callout_termios
;
1730 info
->session
= current
->session
;
1731 info
->pgrp
= current
->pgrp
;
1733 #ifdef SERIAL_DEBUG_OPEN
1734 printk("rs_open ttys%d successful...", info
->line
);
1739 /* Finally, routines used to initialize the serial driver. */
1741 static void show_serial_version(void)
1743 printk("SGI Zilog8530 serial driver version 1.00\n");
1746 /* Return layout for the requested zs chip number. */
1747 static inline struct sgi_zslayout
*get_zs(int chip
)
1749 extern struct hpc3_miscregs
*hpc3mregs
;
1752 prom_printf("Wheee, bogus zs chip number requested.\n");
1756 return (struct sgi_zslayout
*) (&hpc3mregs
->ser1cmd
);
1762 rs_cons_check(struct sgi_serial
*ss
, int channel
)
1765 static int msg_printed
= 0;
1769 /* Is this one of the serial console lines? */
1770 if((zs_cons_chanout
!= channel
) &&
1771 (zs_cons_chanin
!= channel
))
1773 zs_conschan
= ss
->zs_channel
;
1778 /* If this is console input, we handle the break received
1779 * status interrupt on this line to mean prom_halt().
1781 if(zs_cons_chanin
== channel
) {
1782 ss
->break_abort
= 1;
1787 if(ss
->zs_baud
!= 9562) { /* Don't ask... */
1788 prom_printf("BAD console baud rate %d\n", ss
->zs_baud
);
1791 panic("Console baud rate weirdness");
1795 /* Set flag variable for this port so that it cannot be
1796 * opened for other uses by accident.
1802 printk("zs%d: console I/O\n", ((channel
>>1)&1));
1807 printk("zs%d: console %s\n", ((channel
>>1)&1),
1808 (i
==1 ? "input" : (o
==1 ? "output" : "WEIRD")));
1813 volatile int test_done
;
1815 /* rs_init inits the driver */
1818 int chip
, channel
, i
, flags
;
1819 struct sgi_serial
*info
;
1822 /* Setup base handler, and timer table. */
1823 init_bh(SERIAL_BH
, do_serial_bh
);
1825 show_serial_version();
1827 /* Initialize the tty_driver structure */
1828 /* SGI: Not all of this is exactly right for us. */
1830 memset(&serial_driver
, 0, sizeof(struct tty_driver
));
1831 serial_driver
.magic
= TTY_DRIVER_MAGIC
;
1832 serial_driver
.name
= "ttyS";
1833 serial_driver
.major
= TTY_MAJOR
;
1834 serial_driver
.minor_start
= 64;
1835 serial_driver
.num
= NUM_CHANNELS
;
1836 serial_driver
.type
= TTY_DRIVER_TYPE_SERIAL
;
1837 serial_driver
.subtype
= SERIAL_TYPE_NORMAL
;
1838 serial_driver
.init_termios
= tty_std_termios
;
1840 serial_driver
.init_termios
.c_cflag
=
1841 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1842 serial_driver
.flags
= TTY_DRIVER_REAL_RAW
;
1843 serial_driver
.refcount
= &serial_refcount
;
1844 serial_driver
.table
= serial_table
;
1845 serial_driver
.termios
= serial_termios
;
1846 serial_driver
.termios_locked
= serial_termios_locked
;
1848 serial_driver
.open
= rs_open
;
1849 serial_driver
.close
= rs_close
;
1850 serial_driver
.write
= rs_write
;
1851 serial_driver
.flush_chars
= rs_flush_chars
;
1852 serial_driver
.write_room
= rs_write_room
;
1853 serial_driver
.chars_in_buffer
= rs_chars_in_buffer
;
1854 serial_driver
.flush_buffer
= rs_flush_buffer
;
1855 serial_driver
.ioctl
= rs_ioctl
;
1856 serial_driver
.throttle
= rs_throttle
;
1857 serial_driver
.unthrottle
= rs_unthrottle
;
1858 serial_driver
.set_termios
= rs_set_termios
;
1859 serial_driver
.stop
= rs_stop
;
1860 serial_driver
.start
= rs_start
;
1861 serial_driver
.hangup
= rs_hangup
;
1864 * The callout device is just like normal device except for
1865 * major number and the subtype code.
1867 callout_driver
= serial_driver
;
1868 callout_driver
.name
= "cua";
1869 callout_driver
.major
= TTYAUX_MAJOR
;
1870 callout_driver
.subtype
= SERIAL_TYPE_CALLOUT
;
1872 if (tty_register_driver(&serial_driver
))
1873 panic("Couldn't register serial driver\n");
1874 if (tty_register_driver(&callout_driver
))
1875 panic("Couldn't register callout driver\n");
1877 save_flags(flags
); cli();
1879 /* Set up our interrupt linked list */
1880 zs_chain
= &zs_soft
[0];
1881 zs_soft
[0].zs_next
= &zs_soft
[1];
1882 zs_soft
[1].zs_next
= 0;
1884 for(chip
= 0; chip
< NUM_SERIAL
; chip
++) {
1885 /* If we are doing kgdb over one of the channels on
1886 * chip zero, kgdb_channel will be set to 1 by the
1887 * rs_kgdb_hook() routine below.
1889 if(!zs_chips
[chip
]) {
1890 zs_chips
[chip
] = get_zs(chip
);
1891 /* Two channels per chip */
1892 zs_channels
[(chip
*2)] = &zs_chips
[chip
]->channelB
;
1893 zs_channels
[(chip
*2)+1] = &zs_chips
[chip
]->channelA
;
1894 zs_soft
[(chip
*2)].kgdb_channel
= 0;
1895 zs_soft
[(chip
*2)+1].kgdb_channel
= 0;
1897 /* First, set up channel A on this chip. */
1899 zs_soft
[channel
].zs_channel
= zs_channels
[channel
];
1900 zs_soft
[channel
].change_needed
= 0;
1901 zs_soft
[channel
].clk_divisor
= 16;
1902 zs_soft
[channel
].zs_baud
= get_zsbaud(&zs_soft
[channel
]);
1903 zs_soft
[channel
].cons_mouse
= 0;
1904 /* If not keyboard/mouse and is console serial
1905 * line, then enable receiver interrupts.
1907 if(zs_soft
[channel
].is_cons
) {
1908 write_zsreg(zs_soft
[channel
].zs_channel
, R1
,
1909 (EXT_INT_ENAB
| INT_ALL_Rx
));
1910 write_zsreg(zs_soft
[channel
].zs_channel
, R9
, (NV
| MIE
));
1911 write_zsreg(zs_soft
[channel
].zs_channel
, R10
, (NRZ
));
1912 write_zsreg(zs_soft
[channel
].zs_channel
, R3
, (Rx8
|RxENABLE
));
1913 write_zsreg(zs_soft
[channel
].zs_channel
, R5
, (Tx8
| TxENAB
));
1915 /* If this is the kgdb line, enable interrupts because we
1916 * now want to receive the 'control-c' character from the
1917 * client attached to us asynchronously.
1919 if(zs_soft
[channel
].kgdb_channel
)
1920 kgdb_chaninit(&zs_soft
[channel
], 1,
1921 zs_soft
[channel
].zs_baud
);
1923 /* Now, channel B */
1925 zs_soft
[channel
].zs_channel
= zs_channels
[channel
];
1926 zs_soft
[channel
].change_needed
= 0;
1927 zs_soft
[channel
].clk_divisor
= 16;
1928 zs_soft
[channel
].zs_baud
= get_zsbaud(&zs_soft
[channel
]);
1929 zs_soft
[channel
].cons_keyb
= 0;
1930 /* If console serial line, then enable receiver interrupts. */
1931 if(zs_soft
[channel
].is_cons
) {
1932 write_zsreg(zs_soft
[channel
].zs_channel
, R1
,
1933 (EXT_INT_ENAB
| INT_ALL_Rx
));
1934 write_zsreg(zs_soft
[channel
].zs_channel
, R9
,
1936 write_zsreg(zs_soft
[channel
].zs_channel
, R10
,
1938 write_zsreg(zs_soft
[channel
].zs_channel
, R3
,
1940 write_zsreg(zs_soft
[channel
].zs_channel
, R5
,
1941 (Tx8
| TxENAB
| RTS
| DTR
));
1945 for(info
=zs_chain
, i
=0; info
; info
= info
->zs_next
, i
++)
1947 info
->magic
= SERIAL_MAGIC
;
1948 info
->port
= (int) info
->zs_channel
;
1951 info
->irq
= zilog_irq
;
1952 info
->custom_divisor
= 16;
1953 info
->close_delay
= 50;
1954 info
->closing_wait
= 3000;
1958 info
->blocked_open
= 0;
1959 info
->tqueue
.routine
= do_softint
;
1960 info
->tqueue
.data
= info
;
1961 info
->tqueue_hangup
.routine
= do_serial_hangup
;
1962 info
->tqueue_hangup
.data
= info
;
1963 info
->callout_termios
=callout_driver
.init_termios
;
1964 info
->normal_termios
= serial_driver
.init_termios
;
1965 init_waitqueue_head(&info
->open_wait
);
1966 init_waitqueue_head(&info
->close_wait
);
1967 printk("tty%02d at 0x%04x (irq = %d)", info
->line
,
1968 info
->port
, info
->irq
);
1969 printk(" is a Zilog8530\n");
1972 if (request_irq(zilog_irq
, rs_interrupt
, (SA_INTERRUPT
),
1973 "Zilog8530", zs_chain
))
1974 panic("Unable to attach zs intr\n");
1975 restore_flags(flags
);
1981 * register_serial and unregister_serial allows for serial ports to be
1982 * configured at run-time, to support PCMCIA modems.
1984 /* SGI: Unused at this time, just here to make things link. */
1985 int register_serial(struct serial_struct
*req
)
1990 void unregister_serial(int line
)
1995 /* Hooks for running a serial console. con_init() calls this if the
1996 * console is being run over one of the ttya/ttyb serial ports.
1997 * 'chip' should be zero, as chip 1 drives the mouse/keyboard.
1998 * 'channel' is decoded as 0=TTYA 1=TTYB, note that the channels
1999 * are addressed backwards, channel B is first, then channel A.
2002 rs_cons_hook(int chip
, int out
, int line
)
2008 panic("rs_cons_hook called with chip not zero");
2009 if(line
!= 0 && line
!= 1)
2010 panic("rs_cons_hook called with line not ttya or ttyb");
2012 if(!zs_chips
[chip
]) {
2013 zs_chips
[chip
] = get_zs(chip
);
2014 /* Two channels per chip */
2015 zs_channels
[(chip
*2)] = &zs_chips
[chip
]->channelB
;
2016 zs_channels
[(chip
*2)+1] = &zs_chips
[chip
]->channelA
;
2018 zs_soft
[channel
].zs_channel
= zs_channels
[channel
];
2019 zs_soft
[channel
].change_needed
= 0;
2020 zs_soft
[channel
].clk_divisor
= 16;
2021 zs_soft
[channel
].zs_baud
= get_zsbaud(&zs_soft
[channel
]);
2023 zs_cons_chanout
= ((chip
* 2) + channel
);
2025 zs_cons_chanin
= ((chip
* 2) + channel
);
2027 rs_cons_check(&zs_soft
[channel
], channel
);
2030 /* This is called at boot time to prime the kgdb serial debugging
2031 * serial line. The 'tty_num' argument is 0 for /dev/ttyd2 and 1 for
2032 * /dev/ttyd1 (yes they are backwards on purpose) which is determined
2033 * in setup_arch() from the boot command line flags.
2036 rs_kgdb_hook(int tty_num
)
2040 if(!zs_chips
[chip
]) {
2041 zs_chips
[chip
] = get_zs(chip
);
2042 /* Two channels per chip */
2043 zs_channels
[(chip
*2)] = &zs_chips
[chip
]->channelA
;
2044 zs_channels
[(chip
*2)+1] = &zs_chips
[chip
]->channelB
;
2046 zs_soft
[tty_num
].zs_channel
= zs_channels
[tty_num
];
2047 zs_kgdbchan
= zs_soft
[tty_num
].zs_channel
;
2048 zs_soft
[tty_num
].change_needed
= 0;
2049 zs_soft
[tty_num
].clk_divisor
= 16;
2050 zs_soft
[tty_num
].zs_baud
= get_zsbaud(&zs_soft
[tty_num
]);
2051 zs_soft
[tty_num
].kgdb_channel
= 1; /* This runs kgdb */
2052 zs_soft
[tty_num
^ 1].kgdb_channel
= 0; /* This does not */
2054 /* Turn on transmitter/receiver at 8-bits/char */
2055 kgdb_chaninit(&zs_soft
[tty_num
], 0, 9600);
2056 ZS_CLEARERR(zs_kgdbchan
);
2058 ZS_CLEARFIFO(zs_kgdbchan
);
2061 static void zs_console_write(struct console
*co
, const char *str
,
2067 zs_cons_put_char('\r');
2068 zs_cons_put_char(*str
++);
2071 /* Comment this if you want to have a strict interrupt-driven output */
2075 static int zs_console_wait_key(struct console
*con
)
2077 sleep_on(&keypress_wait
);
2081 static kdev_t
zs_console_device(struct console
*con
)
2083 return MKDEV(TTY_MAJOR
, 64 + con
->index
);
2087 static int __init
zs_console_setup(struct console
*con
, char *options
)
2089 struct sgi_serial
*info
;
2093 int cflag
= CREAD
| HUPCL
| CLOCAL
;
2098 baud
= simple_strtoul(options
, NULL
, 10);
2100 while(*s
>= '0' && *s
<= '9')
2102 if (*s
) parity
= *s
++;
2103 if (*s
) bits
= *s
- '0';
2107 * Now construct a cflag setting.
2155 rs_cons_hook(0, 0, con
->index
);
2156 info
= zs_soft
+ con
->index
;
2159 printk("Console: ttyS%d (Zilog8530)\n", info
->line
);
2161 i
= con
->cflag
& CBAUD
;
2162 if (con
->cflag
& CBAUDEX
) {
2164 con
->cflag
&= ~CBAUDEX
;
2166 info
->zs_baud
= baud
;
2168 switch (con
->cflag
& CSIZE
) {
2170 zscons_regs
[3] = Rx5
| RxENABLE
;
2171 zscons_regs
[5] = Tx5
| TxENAB
;
2174 zscons_regs
[3] = Rx6
| RxENABLE
;
2175 zscons_regs
[5] = Tx6
| TxENAB
;
2178 zscons_regs
[3] = Rx7
| RxENABLE
;
2179 zscons_regs
[5] = Tx7
| TxENAB
;
2183 zscons_regs
[3] = Rx8
| RxENABLE
;
2184 zscons_regs
[5] = Tx8
| TxENAB
;
2187 zscons_regs
[5] |= DTR
;
2189 if (con
->cflag
& PARENB
)
2190 zscons_regs
[4] |= PAR_ENA
;
2191 if (!(con
->cflag
& PARODD
))
2192 zscons_regs
[4] |= PAR_EVEN
;
2194 if (con
->cflag
& CSTOPB
)
2195 zscons_regs
[4] |= SB2
;
2197 zscons_regs
[4] |= SB1
;
2199 brg
= BPS_TO_BRG(baud
, ZS_CLOCK
/ info
->clk_divisor
);
2200 zscons_regs
[12] = brg
& 0xff;
2201 zscons_regs
[13] = (brg
>> 8) & 0xff;
2202 memcpy(info
->curregs
, zscons_regs
, sizeof(zscons_regs
));
2203 memcpy(info
->pendregs
, zscons_regs
, sizeof(zscons_regs
));
2204 load_zsregs(info
->zs_channel
, zscons_regs
);
2205 ZS_CLEARERR(info
->zs_channel
);
2206 ZS_CLEARFIFO(info
->zs_channel
);
2210 static struct console sgi_console_driver
= {
2212 write
: zs_console_write
,
2213 device
: zs_console_device
,
2214 wait_key
: zs_console_wait_key
,
2215 setup
: zs_console_setup
,
2216 flags
: CON_PRINTBUFFER
,
2223 void __init
sgi_serial_console_init(void)
2225 register_console(&sgi_console_driver
);
2227 __initcall(rs_init
);