1 /* $Id: zs.c,v 1.68.2.2 2002/01/12 07:04:33 davem Exp $
2 * zs.c: Zilog serial port driver for the Sparc.
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
6 * Fixes by Pete A. Zaitcev <zaitcev@yahoo.com>.
8 * Fixed to use tty_get_baud_rate().
9 * Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
11 * /proc/tty/driver/serial now exists and is readable.
12 * Alex Buell <alex.buell@tahallah.demon.co.uk>, 2001-12-23
16 #include <linux/errno.h>
17 #include <linux/signal.h>
18 #include <linux/sched.h>
19 #include <linux/timer.h>
20 #include <linux/interrupt.h>
21 #include <linux/tty.h>
22 #include <linux/tty_flip.h>
23 #include <linux/config.h>
24 #include <linux/major.h>
25 #include <linux/string.h>
26 #include <linux/fcntl.h>
28 #include <linux/kernel.h>
29 #include <linux/keyboard.h>
30 #include <linux/console.h>
31 #include <linux/delay.h>
32 #include <linux/init.h>
33 #include <linux/bootmem.h>
34 #include <linux/sysrq.h>
38 #include <asm/oplib.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41 #include <asm/bitops.h>
42 #include <asm/kdebug.h>
44 #include <asm/pgtable.h>
51 #include <linux/pci.h>
54 #include "sunserial.h"
59 static int num_serial = 2; /* sun4/sun4c/sun4m - Two chips on board. */
60 #define NUM_SERIAL num_serial
61 #define NUM_CHANNELS (NUM_SERIAL * 2)
63 #define KEYBOARD_LINE 0x2
64 #define MOUSE_LINE 0x3
66 /* On 32-bit sparcs we need to delay after register accesses
67 * to accomodate sun4 systems, but we do not need to flush writes.
68 * On 64-bit and sun4d we need to flush single writes to ensure
72 #define ZSDELAY() udelay(5)
73 #define ZSDELAY_LONG() udelay(20)
76 #define ZSDELAY_LONG()
79 #define ZS_WSYNC(__channel) \
80 sbus_readb(&((__channel)->control))
82 struct sun_zslayout **zs_chips;
83 struct sun_zschannel **zs_channels;
84 struct sun_zschannel *zs_mousechan;
85 struct sun_zschannel *zs_kbdchan;
86 struct sun_zschannel *zs_kgdbchan;
89 struct sun_serial *zs_soft;
90 struct sun_serial *zs_chain; /* IRQ servicing chain */
93 struct tty_struct *zs_ttys;
95 /* Console hooks... */
96 #ifdef CONFIG_SERIAL_CONSOLE
97 static struct console zs_console;
98 static int zs_console_init(void);
101 * Define this to get the zs_fair_output() functionality.
103 #undef SERIAL_CONSOLE_FAIR_OUTPUT
104 #endif /* CONFIG_SERIAL_CONSOLE */
106 static unsigned char kgdb_regs[16] = {
107 0, 0, 0, /* write 0, 1, 2 */
108 (Rx8 | RxENAB), /* write 3 */
109 (X16CLK | SB1 | PAR_EVEN), /* write 4 */
110 (DTR | Tx8 | TxENAB), /* write 5 */
111 0, 0, 0, /* write 6, 7, 8 */
113 (NRZ), /* write 10 */
114 (TCBR | RCBR), /* write 11 */
115 0, 0, /* BRG time constant, write 12 + 13 */
116 (BRSRC | BRENAB), /* write 14 */
117 (DCDIE) /* write 15 */
120 static unsigned char zscons_regs[16] = {
122 (EXT_INT_ENAB | INT_ALL_Rx), /* write 1 */
124 (Rx8 | RxENAB), /* write 3 */
125 (X16CLK), /* write 4 */
126 (DTR | Tx8 | TxENAB), /* write 5 */
127 0, 0, 0, /* write 6, 7, 8 */
128 (NV | MIE), /* write 9 */
129 (NRZ), /* write 10 */
130 (TCBR | RCBR), /* write 11 */
131 0, 0, /* BRG time constant, write 12 + 13 */
132 (BRSRC | BRENAB), /* write 14 */
133 (DCDIE | CTSIE | TxUIE | BRKIE) /* write 15 */
136 #define ZS_CLOCK 4915200 /* Zilog input clock rate */
138 DECLARE_TASK_QUEUE(tq_serial);
140 static struct tty_driver serial_driver, callout_driver;
141 static int serial_refcount;
143 /* serial subtype definitions */
144 #define SERIAL_TYPE_NORMAL 1
145 #define SERIAL_TYPE_CALLOUT 2
147 /* number of characters left in xmit buffer before we ask for more */
148 #define WAKEUP_CHARS 256
150 #define SERIAL_DO_RESTART
152 /* Debugging... DEBUG_INTR is bad to use when one of the zs
153 * lines is your console ;(
155 #undef SERIAL_DEBUG_INTR
156 #undef SERIAL_DEBUG_OPEN
157 #undef SERIAL_DEBUG_FLOW
159 #define RS_STROBE_TIME 10
160 #define RS_ISR_PASS_LIMIT 256
162 #define _INLINE_ inline
165 static void zs_kgdb_hook(int);
167 static void change_speed(struct sun_serial *info);
169 static struct tty_struct **serial_table;
170 static struct termios **serial_termios;
171 static struct termios **serial_termios_locked;
174 #define MIN(a,b) ((a) < (b) ? (a) : (b))
186 struct zs_logent zslog[32];
188 #define ZSLOG(__reg, __val, __write) \
189 do{ int index = zs_curlog; \
190 zslog[index].reg = (__reg); \
191 zslog[index].val = (__val); \
192 zslog[index].write = (__write); \
193 zs_curlog = (index + 1) & (32 - 1); \
195 int zs_dumplog(char *buffer)
200 for (i = 0; i < 32; i++) {
205 write = zslog[i].write;
206 len += sprintf(buffer + len,
207 "ZSLOG[%2d]: reg %2x val %2x %s\n",
208 i, reg, val, write ? "write" : "read");
210 len += sprintf(buffer + len, "ZS current log index %d\n",
215 #define ZSLOG(x,y,z) do { } while (0)
219 * tmp_buf is used as a temporary buffer by serial_write. We need to
220 * lock it in case the memcpy_fromfs blocks while swapping in a page,
221 * and some other program tries to do a serial write at the same time.
222 * Since the lock will only come under contention when the system is
223 * swapping and available memory is low, it makes sense to share one
224 * buffer across all the serial ports, since it significantly saves
225 * memory if large numbers of serial ports are open.
227 static unsigned char *tmp_buf = 0;
228 static DECLARE_MUTEX(tmp_buf_sem);
230 static inline int serial_paranoia_check(struct sun_serial *info,
231 dev_t device, const char *routine)
233 #ifdef SERIAL_PARANOIA_CHECK
234 static const char *badmagic =
235 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
236 static const char *badinfo =
237 "Warning: null sun_serial for (%d, %d) in %s\n";
240 printk(badinfo, MAJOR(device), MINOR(device), routine);
243 if (info->magic != SERIAL_MAGIC) {
244 printk(badmagic, MAJOR(device), MINOR(device), routine);
251 /* Reading and writing Zilog8530 registers. The delays are to make this
252 * driver work on the Sun4 which needs a settling delay after each chip
253 * register access, other machines handle this in hardware via auxiliary
254 * flip-flops which implement the settle time we do in software.
256 static unsigned char read_zsreg(struct sun_zschannel *channel,
259 unsigned char retval;
261 sbus_writeb(reg, &channel->control);
263 retval = sbus_readb(&channel->control);
265 ZSLOG(reg, retval, 0);
269 static void write_zsreg(struct sun_zschannel *channel,
270 unsigned char reg, unsigned char value)
272 ZSLOG(reg, value, 1);
273 sbus_writeb(reg, &channel->control);
275 sbus_writeb(value, &channel->control);
279 static void load_zsregs(struct sun_serial *info, unsigned char *regs)
281 struct sun_zschannel *channel = info->zs_channel;
286 for (i = 0; i < 1000; i++) {
287 stat = read_zsreg(channel, R1);
292 write_zsreg(channel, R3, 0);
293 ZS_CLEARSTAT(channel);
294 ZS_CLEARERR(channel);
295 ZS_CLEARFIFO(channel);
298 save_flags(flags); cli();
300 write_zsreg(channel, R9, CHRA);
302 write_zsreg(channel, R9, CHRB);
304 write_zsreg(channel, R4, regs[R4]);
305 write_zsreg(channel, R3, regs[R3] & ~RxENAB);
306 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
307 write_zsreg(channel, R9, regs[R9] & ~MIE);
308 write_zsreg(channel, R10, regs[R10]);
309 write_zsreg(channel, R11, regs[R11]);
310 write_zsreg(channel, R12, regs[R12]);
311 write_zsreg(channel, R13, regs[R13]);
312 write_zsreg(channel, R14, regs[R14] & ~BRENAB);
313 write_zsreg(channel, R14, regs[R14]);
314 write_zsreg(channel, R14, (regs[R14] & ~SNRZI) | BRENAB);
315 write_zsreg(channel, R3, regs[R3]);
316 write_zsreg(channel, R5, regs[R5]);
317 write_zsreg(channel, R15, regs[R15]);
318 write_zsreg(channel, R0, RES_EXT_INT);
319 write_zsreg(channel, R0, ERR_RES);
320 write_zsreg(channel, R1, regs[R1]);
321 write_zsreg(channel, R9, regs[R9]);
322 restore_flags(flags);
325 #define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
327 static void zs_put_char(struct sun_zschannel *channel, char ch)
329 int loops = ZS_PUT_CHAR_MAX_DELAY;
331 /* Do not change this to use ZSDELAY as this is
332 * a timed polling loop and on sparc64 ZSDELAY
336 u8 val = sbus_readb(&channel->control);
337 ZSLOG(REGCTRL, val, 0);
338 if (val & Tx_BUF_EMP)
343 sbus_writeb(ch, &channel->data);
346 ZSLOG(REGDATA, ch, 1);
349 /* Sets or clears DTR/RTS on the requested line */
350 static void zs_rtsdtr(struct sun_serial *ss, int set)
354 save_flags(flags); cli();
356 ss->curregs[5] |= (RTS | DTR);
357 write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
359 ss->curregs[5] &= ~(RTS | DTR);
360 write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
362 restore_flags(flags);
366 static void kgdb_chaninit(struct sun_serial *ss, int intson, int bps)
371 kgdb_regs[R1] = INT_ALL_Rx;
372 kgdb_regs[R9] |= MIE;
375 kgdb_regs[R9] &= ~MIE;
377 brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
378 kgdb_regs[R12] = (brg & 255);
379 kgdb_regs[R13] = ((brg >> 8) & 255);
380 load_zsregs(ss, kgdb_regs);
384 * ------------------------------------------------------------
385 * zs_stop() and zs_start()
387 * This routines are called before setting or resetting tty->stopped.
388 * They enable or disable transmitter interrupts, as necessary.
389 * ------------------------------------------------------------
391 static void zs_stop(struct tty_struct *tty)
393 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
396 if (serial_paranoia_check(info, tty->device, "zs_stop"))
399 save_flags(flags); cli();
400 if (info->curregs[5] & TxENAB) {
401 info->curregs[5] &= ~TxENAB;
402 write_zsreg(info->zs_channel, 5, info->curregs[5]);
404 restore_flags(flags);
407 static void zs_start(struct tty_struct *tty)
409 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
412 if (serial_paranoia_check(info, tty->device, "zs_start"))
415 save_flags(flags); cli();
416 if (info->xmit_cnt && info->xmit_buf && !(info->curregs[5] & TxENAB)) {
417 info->curregs[5] |= TxENAB;
418 write_zsreg(info->zs_channel, 5, info->curregs[5]);
420 restore_flags(flags);
423 /* Drop into either the boot monitor or kadb upon receiving a break
424 * from keyboard/console input.
426 void batten_down_hatches(void)
430 /* If we are doing kadb, we call the debugger
431 * else we just drop into the boot monitor.
432 * Note that we must flush the user windows
433 * first before giving up control.
436 flush_user_windows();
438 if((((unsigned long)linux_dbvec)>=DEBUG_FIRSTVADDR) &&
439 (((unsigned long)linux_dbvec)<=DEBUG_LASTVADDR))
445 /* XXX We want to notify the keyboard driver that all
446 * XXX keys are in the up state or else weird things
455 * ----------------------------------------------------------------------
457 * Here starts the interrupt handling routines. All of the following
458 * subroutines are declared as inline and are folded into
459 * zs_interrupt(). They were separated out for readability's sake.
461 * Note: zs_interrupt() is a "fast" interrupt, which means that it
462 * runs with interrupts turned off. People who may want to modify
463 * zs_interrupt() should try to keep the interrupt handler as fast as
464 * possible. After you are done making modifications, it is not a bad
467 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
469 * and look at the resulting assemble code in serial.s.
471 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
472 * -----------------------------------------------------------------------
476 * This routine is used by the interrupt handler to schedule
477 * processing in the software interrupt portion of the driver.
479 static void zs_sched_event(struct sun_serial *info, int event)
481 info->event |= 1 << event;
482 queue_task(&info->tqueue, &tq_serial);
487 extern void breakpoint(void); /* For the KGDB frame character */
490 static void receive_chars(struct sun_serial *info, struct pt_regs *regs)
492 struct tty_struct *tty = info->tty;
493 int do_queue_task = 0;
496 unsigned char ch, r1;
498 r1 = read_zsreg(info->zs_channel, R1);
499 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
500 sbus_writeb(ERR_RES, &info->zs_channel->control);
502 ZS_WSYNC(info->zs_channel);
503 ZSLOG(REGCTRL, ERR_RES, 1);
506 ch = sbus_readb(&info->zs_channel->data);
507 ZSLOG(REGDATA, ch, 0);
508 ch &= info->parity_mask;
511 /* If this is the console keyboard, we need to handle
514 if (info->cons_keyb) {
515 if (ch == SUNKBD_RESET) {
516 l1a_state.kbd_id = 1;
517 l1a_state.l1_down = 0;
518 } else if (l1a_state.kbd_id) {
519 l1a_state.kbd_id = 0;
520 } else if (ch == SUNKBD_L1) {
521 l1a_state.l1_down = 1;
522 } else if (ch == (SUNKBD_L1|SUNKBD_UP)) {
523 l1a_state.l1_down = 0;
524 } else if (ch == SUNKBD_A && l1a_state.l1_down) {
526 batten_down_hatches();
527 /* Continue execution... */
528 l1a_state.l1_down = 0;
529 l1a_state.kbd_id = 0;
532 sunkbd_inchar(ch, regs);
535 if (info->cons_mouse) {
536 sun_mouse_inbyte(ch, 0);
541 /* whee, break received */
542 batten_down_hatches();
543 /* Continue execution... */
548 /* Look for kgdb 'stop' character, consult the gdb
549 * documentation for remote target debugging and
550 * arch/sparc/kernel/sparc-stub.c to see how all this works.
552 if (info->kgdb_channel && (ch =='\003')) {
562 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
567 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
568 else if (r1 & Rx_OVR)
569 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
570 else if (r1 & CRC_ERR)
571 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
573 *tty->flip.flag_buf_ptr++ = 0;
574 *tty->flip.char_buf_ptr++ = ch;
580 /* Check if we have another character... */
581 stat = sbus_readb(&info->zs_channel->control);
583 ZSLOG(REGCTRL, stat, 0);
584 if (!(stat & Rx_CH_AV))
589 if (do_queue_task != 0)
590 queue_task(&tty->flip.tqueue, &tq_timer);
593 static void transmit_chars(struct sun_serial *info)
595 struct tty_struct *tty = info->tty;
599 zs_put_char(info->zs_channel, info->x_char);
604 if ((info->xmit_cnt <= 0) || (tty != 0 && tty->stopped)) {
605 /* That's peculiar... */
606 sbus_writeb(RES_Tx_P, &info->zs_channel->control);
608 ZS_WSYNC(info->zs_channel);
609 ZSLOG(REGCTRL, RES_Tx_P, 1);
614 zs_put_char(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
615 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
618 if (info->xmit_cnt < WAKEUP_CHARS)
619 zs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
621 if (info->xmit_cnt <= 0) {
622 sbus_writeb(RES_Tx_P, &info->zs_channel->control);
624 ZS_WSYNC(info->zs_channel);
625 ZSLOG(REGCTRL, RES_Tx_P, 1);
629 static void status_handle(struct sun_serial *info)
631 unsigned char status;
633 /* Get status from Read Register 0 */
634 status = sbus_readb(&info->zs_channel->control);
636 ZSLOG(REGCTRL, status, 0);
637 /* Clear status condition... */
638 sbus_writeb(RES_EXT_INT, &info->zs_channel->control);
640 ZS_WSYNC(info->zs_channel);
641 ZSLOG(REGCTRL, RES_EXT_INT, 1);
644 if ((info->tty->termios->c_cflag & CRTSCTS) &&
645 ((info->curregs[3] & AUTO_ENAB)==0)) {
646 info->curregs[3] |= AUTO_ENAB;
647 write_zsreg(info->zs_channel, 3, info->curregs[3]);
650 if ((info->curregs[3] & AUTO_ENAB)) {
651 info->curregs[3] &= ~AUTO_ENAB;
652 write_zsreg(info->zs_channel, 3, info->curregs[3]);
656 /* Whee, if this is console input and this is a
657 * 'break asserted' status change interrupt, call
660 if (status & BRK_ABRT) {
661 if (info->break_abort)
662 batten_down_hatches();
663 if (info->cons_mouse)
664 sun_mouse_inbyte(0, 1);
667 /* XXX Whee, put in a buffer somewhere, the status information
668 * XXX whee whee whee... Where does the information go...
674 * This is the serial driver's generic interrupt routine
676 void zs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
678 struct sun_serial *info;
681 info = (struct sun_serial *)dev_id;
683 for (i = 0; i < NUM_SERIAL; i++) {
684 unsigned char r3 = read_zsreg(info->zs_channel, 3);
686 /* Channel A -- /dev/ttya or /dev/kbd, could be the console */
687 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
688 sbus_writeb(RES_H_IUS, &info->zs_channel->control);
690 ZS_WSYNC(info->zs_channel);
691 ZSLOG(REGCTRL, RES_H_IUS, 1);
693 receive_chars(info, regs);
697 transmit_chars(info);
700 /* Channel B -- /dev/ttyb or /dev/mouse, could be the console */
701 info = info->zs_next;
702 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
703 sbus_writeb(RES_H_IUS, &info->zs_channel->control);
705 ZS_WSYNC(info->zs_channel);
706 ZSLOG(REGCTRL, RES_H_IUS, 1);
708 receive_chars(info, regs);
712 transmit_chars(info);
715 info = info->zs_next;
720 * -------------------------------------------------------------------
721 * Here ends the serial interrupt routines.
722 * -------------------------------------------------------------------
726 * This routine is used to handle the "bottom half" processing for the
727 * serial driver, known also the "software interrupt" processing.
728 * This processing is done at the kernel interrupt level, after the
729 * zs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
730 * is where time-consuming activities which can not be done in the
731 * interrupt driver proper are done; the interrupt driver schedules
732 * them using zs_sched_event(), and they get done here.
734 static void do_serial_bh(void)
736 run_task_queue(&tq_serial);
739 static void do_softint(void *private_)
741 struct sun_serial *info = (struct sun_serial *) private_;
742 struct tty_struct *tty;
748 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
754 * This routine is called from the scheduler tqueue when the interrupt
755 * routine has signalled that a hangup has occurred. The path of
756 * hangup processing is:
758 * serial interrupt routine -> (scheduler tqueue) ->
759 * do_serial_hangup() -> tty->hangup() -> zs_hangup()
762 static void do_serial_hangup(void *private_)
764 struct sun_serial *info = (struct sun_serial *) private_;
765 struct tty_struct *tty;
770 #ifdef SERIAL_DEBUG_OPEN
771 printk("do_serial_hangup<%p: tty-%d\n",
772 __builtin_return_address(0), info->line);
778 static int startup(struct sun_serial * info)
782 if (info->flags & ZILOG_INITIALIZED)
785 if (!info->xmit_buf) {
786 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
791 save_flags(flags); cli();
793 #ifdef SERIAL_DEBUG_OPEN
794 printk("Starting up tty-%d (irq %d)...\n", info->line, info->irq);
798 * Clear the FIFO buffers and disable them
799 * (they will be reenabled in change_speed())
801 ZS_CLEARFIFO(info->zs_channel);
802 info->xmit_fifo_size = 1;
805 * Clear the interrupt registers.
807 sbus_writeb(ERR_RES, &info->zs_channel->control);
809 ZS_WSYNC(info->zs_channel);
810 ZSLOG(REGCTRL, ERR_RES, 1);
812 sbus_writeb(RES_H_IUS, &info->zs_channel->control);
814 ZS_WSYNC(info->zs_channel);
815 ZSLOG(REGCTRL, RES_H_IUS, 1);
818 * Now, initialize the Zilog
823 * Finally, enable sequencing and interrupts
825 info->curregs[1] |= (info->curregs[1] & ~(RxINT_MASK)) |
826 (EXT_INT_ENAB | INT_ALL_Rx);
827 info->curregs[3] |= (RxENAB | Rx8);
828 /* We enable Tx interrupts as needed. */
829 info->curregs[5] |= (TxENAB | Tx8);
830 info->curregs[9] |= (NV | MIE);
831 write_zsreg(info->zs_channel, 3, info->curregs[3]);
832 write_zsreg(info->zs_channel, 5, info->curregs[5]);
833 write_zsreg(info->zs_channel, 9, info->curregs[9]);
836 * And clear the interrupt registers again for luck.
838 sbus_writeb(ERR_RES, &info->zs_channel->control);
840 ZS_WSYNC(info->zs_channel);
841 ZSLOG(REGCTRL, ERR_RES, 1);
843 sbus_writeb(RES_H_IUS, &info->zs_channel->control);
845 ZS_WSYNC(info->zs_channel);
846 ZSLOG(REGCTRL, RES_H_IUS, 1);
849 clear_bit(TTY_IO_ERROR, &info->tty->flags);
850 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
853 * and set the speed of the serial port
857 info->flags |= ZILOG_INITIALIZED;
858 restore_flags(flags);
863 * This routine will shutdown a serial port; interrupts are disabled, and
864 * DTR is dropped if the hangup on close termio flag is on.
866 static void shutdown(struct sun_serial * info)
870 if (!(info->flags & ZILOG_INITIALIZED))
873 #ifdef SERIAL_DEBUG_OPEN
874 printk("Shutting down serial port %d (irq %d)....", info->line,
878 save_flags(flags); cli(); /* Disable interrupts */
880 if (info->xmit_buf) {
881 free_page((unsigned long) info->xmit_buf);
886 set_bit(TTY_IO_ERROR, &info->tty->flags);
888 info->flags &= ~ZILOG_INITIALIZED;
889 restore_flags(flags);
893 * This routine is called to set the UART divisor registers to match
894 * the specified baud rate for a serial port.
896 static void change_speed(struct sun_serial *info)
902 if (!info->tty || !info->tty->termios)
904 cflag = info->tty->termios->c_cflag;
907 baud = tty_get_baud_rate(info->tty);
909 if ((baud == 38400) &&
910 ((info->flags & ZILOG_SPD_MASK) == ZILOG_SPD_CUST))
911 quot = info->custom_divisor;
914 info->zs_baud = info->baud_base / quot;
915 info->clk_divisor = 16;
917 info->curregs[4] = X16CLK;
918 info->curregs[11] = TCBR | RCBR;
919 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
920 info->curregs[12] = (brg & 255);
921 info->curregs[13] = ((brg >> 8) & 255);
922 info->curregs[14] = BRSRC | BRENAB;
925 info->zs_baud = baud;
926 info->clk_divisor = 16;
928 info->curregs[4] = X16CLK;
929 info->curregs[11] = TCBR | RCBR;
930 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
931 info->curregs[12] = (brg & 255);
932 info->curregs[13] = ((brg >> 8) & 255);
933 info->curregs[14] = BRSRC | BRENAB;
940 /* byte size and parity */
941 switch (cflag & CSIZE) {
943 info->curregs[3] &= ~(RxN_MASK);
944 info->curregs[3] |= Rx5;
945 info->curregs[5] &= ~(TxN_MASK);
946 info->curregs[5] |= Tx5;
947 info->parity_mask = 0x1f;
950 info->curregs[3] &= ~(RxN_MASK);
951 info->curregs[3] |= Rx6;
952 info->curregs[5] &= ~(TxN_MASK);
953 info->curregs[5] |= Tx6;
954 info->parity_mask = 0x3f;
957 info->curregs[3] &= ~(RxN_MASK);
958 info->curregs[3] |= Rx7;
959 info->curregs[5] &= ~(TxN_MASK);
960 info->curregs[5] |= Tx7;
961 info->parity_mask = 0x7f;
964 default: /* defaults to 8 bits */
965 info->curregs[3] &= ~(RxN_MASK);
966 info->curregs[3] |= Rx8;
967 info->curregs[5] &= ~(TxN_MASK);
968 info->curregs[5] |= Tx8;
969 info->parity_mask = 0xff;
972 info->curregs[4] &= ~(0x0c);
973 if (cflag & CSTOPB) {
974 info->curregs[4] |= SB2;
976 info->curregs[4] |= SB1;
978 if (cflag & PARENB) {
979 info->curregs[4] |= PAR_ENAB;
981 info->curregs[4] &= ~PAR_ENAB;
983 if (!(cflag & PARODD)) {
984 info->curregs[4] |= PAR_EVEN;
986 info->curregs[4] &= ~PAR_EVEN;
989 /* Load up the new values */
990 load_zsregs(info, info->curregs);
995 /* This is for mouse/keyboard output.
996 * XXX mouse output??? can we send it commands??? XXX
998 static void kbd_put_char(unsigned char ch)
1000 struct sun_zschannel *chan = zs_kbdchan;
1001 unsigned long flags;
1006 save_flags(flags); cli();
1007 zs_put_char(chan, ch);
1008 restore_flags(flags);
1011 void mouse_put_char(char ch)
1013 struct sun_zschannel *chan = zs_mousechan;
1014 unsigned long flags;
1019 save_flags(flags); cli();
1020 zs_put_char(chan, ch);
1021 restore_flags(flags);
1024 /* These are for receiving and sending characters under the kgdb
1025 * source level kernel debugger.
1027 void putDebugChar(char kgdb_char)
1029 struct sun_zschannel *chan = zs_kgdbchan;
1031 while((sbus_readb(&chan->control) & Tx_BUF_EMP)==0)
1033 sbus_writeb(kgdb_char, &chan->data);
1035 ZSLOG(REGDATA, kgdb_char, 1);
1038 char getDebugChar(void)
1040 struct sun_zschannel *chan = zs_kgdbchan;
1044 val = sbus_readb(&chan->control);
1045 ZSLOG(REGCTRL, val, 0);
1047 } while ((val & Rx_CH_AV) == 0);
1049 val = sbus_readb(&chan->data);
1050 ZSLOG(REGDATA, val, 0);
1054 static void zs_flush_chars(struct tty_struct *tty)
1056 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1057 unsigned long flags;
1059 if (serial_paranoia_check(info, tty->device, "zs_flush_chars"))
1062 save_flags(flags); cli();
1063 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1067 /* Enable transmitter */
1068 info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1069 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1070 info->curregs[5] |= TxENAB;
1071 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1074 * Send a first (bootstrapping) character. A best solution is
1075 * to call transmit_chars() here which handles output in a
1076 * generic way. Current transmit_chars() not only transmits,
1077 * but resets interrupts also what we do not desire here.
1078 * XXX Discuss with David.
1080 zs_put_char(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
1081 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1085 restore_flags(flags);
1088 static int zs_write(struct tty_struct * tty, int from_user,
1089 const unsigned char *buf, int count)
1091 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1092 unsigned long flags;
1095 if (serial_paranoia_check(info, tty->device, "zs_write"))
1098 if (!info || !info->xmit_buf || !tmp_buf)
1105 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1106 SERIAL_XMIT_SIZE - info->xmit_head));
1109 c -= copy_from_user(tmp_buf, buf, c);
1116 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1117 SERIAL_XMIT_SIZE - info->xmit_head));
1118 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1119 info->xmit_head = ((info->xmit_head + c) &
1120 (SERIAL_XMIT_SIZE - 1));
1121 info->xmit_cnt += c;
1122 restore_flags(flags);
1132 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1133 SERIAL_XMIT_SIZE - info->xmit_head));
1135 restore_flags(flags);
1138 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1139 info->xmit_head = ((info->xmit_head + c) &
1140 (SERIAL_XMIT_SIZE - 1));
1141 info->xmit_cnt += c;
1142 restore_flags(flags);
1150 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
1151 /* Enable transmitter */
1152 info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1153 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1154 info->curregs[5] |= TxENAB;
1155 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1157 zs_put_char(info->zs_channel,
1158 info->xmit_buf[info->xmit_tail++]);
1159 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1164 restore_flags(flags);
1168 static int zs_write_room(struct tty_struct *tty)
1170 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1173 if (serial_paranoia_check(info, tty->device, "zs_write_room"))
1175 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1181 static int zs_chars_in_buffer(struct tty_struct *tty)
1183 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1185 if (serial_paranoia_check(info, tty->device, "zs_chars_in_buffer"))
1187 return info->xmit_cnt;
1190 static void zs_flush_buffer(struct tty_struct *tty)
1192 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1194 if (serial_paranoia_check(info, tty->device, "zs_flush_buffer"))
1197 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1203 * ------------------------------------------------------------
1206 * This routine is called by the upper-layer tty layer to signal that
1207 * incoming characters should be throttled.
1208 * ------------------------------------------------------------
1210 static void zs_throttle(struct tty_struct * tty)
1212 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1213 #ifdef SERIAL_DEBUG_THROTTLE
1216 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1217 tty->ldisc.chars_in_buffer(tty));
1220 if (serial_paranoia_check(info, tty->device, "zs_throttle"))
1224 info->x_char = STOP_CHAR(tty);
1226 /* Turn off RTS line */
1228 info->curregs[5] &= ~RTS;
1229 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1233 static void zs_unthrottle(struct tty_struct * tty)
1235 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1236 #ifdef SERIAL_DEBUG_THROTTLE
1239 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1240 tty->ldisc.chars_in_buffer(tty));
1243 if (serial_paranoia_check(info, tty->device, "zs_unthrottle"))
1250 info->x_char = START_CHAR(tty);
1253 /* Assert RTS line */
1255 info->curregs[5] |= RTS;
1256 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1261 * ------------------------------------------------------------
1262 * zs_ioctl() and friends
1263 * ------------------------------------------------------------
1266 static int get_serial_info(struct sun_serial * info,
1267 struct serial_struct * retinfo)
1269 struct serial_struct tmp;
1273 memset(&tmp, 0, sizeof(tmp));
1274 tmp.type = info->type;
1275 tmp.line = info->line;
1276 tmp.port = info->port;
1277 tmp.irq = info->irq;
1278 tmp.flags = info->flags;
1279 tmp.baud_base = info->baud_base;
1280 tmp.close_delay = info->close_delay;
1281 tmp.closing_wait = info->closing_wait;
1282 tmp.custom_divisor = info->custom_divisor;
1283 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1288 static int set_serial_info(struct sun_serial * info,
1289 struct serial_struct * new_info)
1291 struct serial_struct new_serial;
1292 struct sun_serial old_info;
1295 if (!new_info || copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1299 if (!capable(CAP_SYS_ADMIN)) {
1300 if ((new_serial.baud_base != info->baud_base) ||
1301 (new_serial.type != info->type) ||
1302 (new_serial.close_delay != info->close_delay) ||
1303 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1304 (info->flags & ~ZILOG_USR_MASK)))
1306 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1307 (new_serial.flags & ZILOG_USR_MASK));
1308 info->custom_divisor = new_serial.custom_divisor;
1309 goto check_and_exit;
1312 if(new_serial.baud_base < 9600)
1315 if (info->count > 1)
1319 * OK, past this point, all the error checking has been done.
1320 * At this point, we start making changes.....
1323 info->baud_base = new_serial.baud_base;
1324 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1325 (new_serial.flags & ZILOG_FLAGS));
1326 info->custom_divisor = new_serial.custom_divisor;
1327 info->type = new_serial.type;
1328 info->close_delay = new_serial.close_delay;
1329 info->closing_wait = new_serial.closing_wait;
1332 retval = startup(info);
1337 * get_lsr_info - get line status register info
1339 * Purpose: Let user call ioctl() to get info when the UART physically
1340 * is emptied. On bus types like RS485, the transmitter must
1341 * release the bus after transmitting. This must be done when
1342 * the transmit shift register is empty, not be done when the
1343 * transmit holding register is empty. This functionality
1344 * allows an RS485 driver to be written in user space.
1346 static int get_lsr_info(struct sun_serial * info, unsigned int *value)
1348 unsigned char status;
1351 status = sbus_readb(&info->zs_channel->control);
1353 ZSLOG(REGCTRL, status, 0);
1355 if (put_user(status, value))
1360 static int get_modem_info(struct sun_serial * info, unsigned int *value)
1362 unsigned char status;
1363 unsigned int result;
1366 status = sbus_readb(&info->zs_channel->control);
1368 ZSLOG(REGCTRL, status, 0);
1370 result = ((info->curregs[5] & RTS) ? TIOCM_RTS : 0)
1371 | ((info->curregs[5] & DTR) ? TIOCM_DTR : 0)
1372 | ((status & DCD) ? TIOCM_CAR : 0)
1373 | ((status & SYNC) ? TIOCM_DSR : 0)
1374 | ((status & CTS) ? TIOCM_CTS : 0);
1375 if (put_user(result, value))
1380 static int set_modem_info(struct sun_serial * info, unsigned int cmd,
1381 unsigned int *value)
1385 if (get_user(arg, value))
1389 if (arg & TIOCM_RTS)
1390 info->curregs[5] |= RTS;
1391 if (arg & TIOCM_DTR)
1392 info->curregs[5] |= DTR;
1395 if (arg & TIOCM_RTS)
1396 info->curregs[5] &= ~RTS;
1397 if (arg & TIOCM_DTR)
1398 info->curregs[5] &= ~DTR;
1401 info->curregs[5] = ((info->curregs[5] & ~(RTS | DTR))
1402 | ((arg & TIOCM_RTS) ? RTS : 0)
1403 | ((arg & TIOCM_DTR) ? DTR : 0));
1409 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1415 * This routine sends a break character out the serial port.
1417 static void send_break( struct sun_serial * info, int duration)
1421 current->state = TASK_INTERRUPTIBLE;
1423 write_zsreg(info->zs_channel, 5, (info->curregs[5] | SND_BRK));
1424 schedule_timeout(duration);
1425 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1429 static int zs_ioctl(struct tty_struct *tty, struct file * file,
1430 unsigned int cmd, unsigned long arg)
1432 struct sun_serial * info = (struct sun_serial *) tty->driver_data;
1435 if (serial_paranoia_check(info, tty->device, "zs_ioctl"))
1438 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1439 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1440 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1441 if (tty->flags & (1 << TTY_IO_ERROR))
1446 case TCSBRK: /* SVID version: non-zero arg --> no break */
1447 retval = tty_check_change(tty);
1450 tty_wait_until_sent(tty, 0);
1452 send_break(info, HZ/4); /* 1/4 second */
1454 case TCSBRKP: /* support for POSIX tcsendbreak() */
1455 retval = tty_check_change(tty);
1458 tty_wait_until_sent(tty, 0);
1459 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1462 if (put_user(C_CLOCAL(tty) ? 1 : 0,
1463 (unsigned long *) arg))
1467 if (get_user(arg, (unsigned long *) arg))
1469 tty->termios->c_cflag =
1470 ((tty->termios->c_cflag & ~CLOCAL) |
1471 (arg ? CLOCAL : 0));
1474 return get_modem_info(info, (unsigned int *) arg);
1478 return set_modem_info(info, cmd, (unsigned int *) arg);
1480 return get_serial_info(info,
1481 (struct serial_struct *) arg);
1483 return set_serial_info(info,
1484 (struct serial_struct *) arg);
1485 case TIOCSERGETLSR: /* Get line status register */
1486 return get_lsr_info(info, (unsigned int *) arg);
1488 case TIOCSERGSTRUCT:
1489 if (copy_to_user((struct sun_serial *) arg,
1490 info, sizeof(struct sun_serial)))
1495 return -ENOIOCTLCMD;
1500 static void zs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1502 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1504 if (tty->termios->c_cflag == old_termios->c_cflag)
1509 if ((old_termios->c_cflag & CRTSCTS) &&
1510 !(tty->termios->c_cflag & CRTSCTS)) {
1511 tty->hw_stopped = 0;
1517 * ------------------------------------------------------------
1520 * This routine is called when the serial port gets closed. First, we
1521 * wait for the last remaining data to be sent. Then, we unlink its
1522 * ZILOG structure from the interrupt chain if necessary, and we free
1523 * that IRQ if nothing is left in the chain.
1524 * ------------------------------------------------------------
1526 static void zs_close(struct tty_struct *tty, struct file * filp)
1528 struct sun_serial * info = (struct sun_serial *) tty->driver_data;
1529 unsigned long flags;
1531 if (!info || serial_paranoia_check(info, tty->device, "zs_close"))
1534 save_flags(flags); cli();
1536 if (tty_hung_up_p(filp)) {
1537 restore_flags(flags);
1541 #ifdef SERIAL_DEBUG_OPEN
1542 printk("zs_close tty-%d, count = %d\n", info->line, info->count);
1544 if ((tty->count == 1) && (info->count != 1)) {
1546 * Uh, oh. tty->count is 1, which means that the tty
1547 * structure will be freed. Info->count should always
1548 * be one in these conditions. If it's greater than
1549 * one, we've got real problems, since it means the
1550 * serial port won't be shutdown.
1552 printk("zs_close: bad serial port count; tty->count is 1, "
1553 "info->count is %d\n", info->count);
1556 if (--info->count < 0) {
1557 printk("zs_close: bad serial port count for ttys%d: %d\n",
1558 info->line, info->count);
1562 restore_flags(flags);
1565 info->flags |= ZILOG_CLOSING;
1567 * Save the termios structure, since this port may have
1568 * separate termios for callout and dialin.
1570 if (info->flags & ZILOG_NORMAL_ACTIVE)
1571 info->normal_termios = *tty->termios;
1572 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1573 info->callout_termios = *tty->termios;
1575 * Now we wait for the transmit buffer to clear; and we notify
1576 * the line discipline to only process XON/XOFF characters.
1579 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1580 tty_wait_until_sent(tty, info->closing_wait);
1582 * At this point we stop accepting input. To do this, we
1583 * disable the receive line status interrupts, and tell the
1584 * interrupt driver to stop checking the data ready bit in the
1585 * line status register.
1587 /** if (!info->iscons) ... **/
1588 info->curregs[3] &= ~RxENAB;
1589 write_zsreg(info->zs_channel, 3, info->curregs[3]);
1590 info->curregs[1] &= ~(RxINT_MASK);
1591 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1592 ZS_CLEARFIFO(info->zs_channel);
1595 if (tty->driver.flush_buffer)
1596 tty->driver.flush_buffer(tty);
1597 tty_ldisc_flush(tty);
1601 if (tty->ldisc.num != N_TTY) {
1602 if (tty->ldisc.close)
1603 (tty->ldisc.close)(tty);
1604 tty->ldisc = *(tty_ldisc_get(N_TTY));
1605 tty->termios->c_line = N_TTY;
1606 if (tty->ldisc.open)
1607 (tty->ldisc.open)(tty);
1609 if (info->blocked_open) {
1610 if (info->close_delay) {
1611 current->state = TASK_INTERRUPTIBLE;
1612 schedule_timeout(info->close_delay);
1614 wake_up_interruptible(&info->open_wait);
1616 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1618 wake_up_interruptible(&info->close_wait);
1619 #ifdef SERIAL_DEBUG_OPEN
1620 printk("zs_close tty-%d exiting, count = %d\n", info->line, info->count);
1622 restore_flags(flags);
1626 * zs_hangup() --- called by tty_hangup() when a hangup is signaled.
1628 void zs_hangup(struct tty_struct *tty)
1630 struct sun_serial * info = (struct sun_serial *) tty->driver_data;
1632 if (serial_paranoia_check(info, tty->device, "zs_hangup"))
1638 #ifdef SERIAL_DEBUG_OPEN
1639 printk("zs_hangup<%p: tty-%d, count = %d bye\n",
1640 __builtin_return_address(0), info->line, info->count);
1643 zs_flush_buffer(tty);
1647 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1649 wake_up_interruptible(&info->open_wait);
1654 * line_info - returns information about each channel
1657 static inline int line_info(char *buf, struct sun_serial *info)
1659 unsigned char status;
1663 ret = sprintf(buf, "%d: uart:Zilog8530 port:%x irq:%d",
1664 info->line, info->port, info->irq);
1667 status = sbus_readb(&info->zs_channel->control);
1669 ZSLOG(REGCTRL, status, 0);
1674 if (info->curregs[5] & RTS)
1675 strcat(stat_buf, "|RTS");
1677 strcat(stat_buf, "|CTS");
1678 if (info->curregs[5] & DTR)
1679 strcat(stat_buf, "|DTR");
1681 strcat(stat_buf, "|DSR");
1683 strcat(stat_buf, "|CD");
1685 ret += sprintf(buf + ret, " baud:%d %s\n", info->zs_baud, stat_buf + 1);
1691 * zs_read_proc() - called when /proc/tty/driver/serial is read.
1694 int zs_read_proc(char *page, char **start, off_t off, int count,
1695 int *eof, void *data)
1697 char *revision = "$Revision: 1.68.2.2 $";
1702 version = strchr(revision, ' ');
1703 p = strchr(++version, ' ');
1705 len += sprintf(page, "serinfo:1.0 driver:%s\n", version);
1708 for (i = 0; i < NUM_CHANNELS && len < 4000; i++) {
1709 l = line_info(page + len, &zs_soft[i]);
1711 if (len+begin > off+count)
1713 if (len+begin < off) {
1721 if (off >= len+begin)
1723 *start = page + (off-begin);
1724 return ((count < begin+len-off) ? count : begin+len-off);
1728 * ------------------------------------------------------------
1729 * zs_open() and friends
1730 * ------------------------------------------------------------
1732 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1733 struct sun_serial *info)
1735 DECLARE_WAITQUEUE(wait, current);
1736 int retval, do_clocal = 0;
1740 * If the device is in the middle of being closed, then block
1741 * until it's done, and then try again.
1743 if (info->flags & ZILOG_CLOSING) {
1744 interruptible_sleep_on(&info->close_wait);
1745 #ifdef SERIAL_DO_RESTART
1746 if (info->flags & ZILOG_HUP_NOTIFY)
1749 return -ERESTARTSYS;
1756 * If this is a callout device, then just make sure the normal
1757 * device isn't being used.
1759 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1760 if (info->flags & ZILOG_NORMAL_ACTIVE)
1762 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1763 (info->flags & ZILOG_SESSION_LOCKOUT) &&
1764 (info->session != current->session))
1766 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1767 (info->flags & ZILOG_PGRP_LOCKOUT) &&
1768 (info->pgrp != current->pgrp))
1770 info->flags |= ZILOG_CALLOUT_ACTIVE;
1775 * If non-blocking mode is set, or the port is not enabled,
1776 * then make the check up front and then exit.
1778 if ((filp->f_flags & O_NONBLOCK) ||
1779 (tty->flags & (1 << TTY_IO_ERROR))) {
1780 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1782 info->flags |= ZILOG_NORMAL_ACTIVE;
1786 if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1787 if (info->normal_termios.c_cflag & CLOCAL)
1790 if (tty->termios->c_cflag & CLOCAL)
1795 * Block waiting for the carrier detect and the line to become
1796 * free (i.e., not in use by the callout). While we are in
1797 * this loop, info->count is dropped by one, so that
1798 * zs_close() knows when to free things. We restore it upon
1799 * exit, either normal or abnormal.
1802 add_wait_queue(&info->open_wait, &wait);
1803 #ifdef SERIAL_DEBUG_OPEN
1804 printk("block_til_ready before block: ttys%d, count = %d\n",
1805 info->line, info->count);
1808 if(!tty_hung_up_p(filp))
1811 info->blocked_open++;
1814 if (!(info->flags & ZILOG_CALLOUT_ACTIVE))
1817 set_current_state(TASK_INTERRUPTIBLE);
1818 if (tty_hung_up_p(filp) ||
1819 !(info->flags & ZILOG_INITIALIZED)) {
1820 #ifdef SERIAL_DEBUG_OPEN
1821 printk("block_til_ready hup-ed: ttys%d, count = %d\n",
1822 info->line, info->count);
1824 #ifdef SERIAL_DO_RESTART
1825 if (info->flags & ZILOG_HUP_NOTIFY)
1828 retval = -ERESTARTSYS;
1836 r0 = read_zsreg(info->zs_channel, R0);
1838 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1839 !(info->flags & ZILOG_CLOSING) &&
1840 (do_clocal || (DCD & r0)))
1842 if (signal_pending(current)) {
1843 retval = -ERESTARTSYS;
1846 #ifdef SERIAL_DEBUG_OPEN
1847 printk("block_til_ready blocking: ttys%d, count = %d\n",
1848 info->line, info->count);
1852 current->state = TASK_RUNNING;
1853 remove_wait_queue(&info->open_wait, &wait);
1854 if (!tty_hung_up_p(filp))
1856 info->blocked_open--;
1857 #ifdef SERIAL_DEBUG_OPEN
1858 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1859 info->line, info->count);
1863 info->flags |= ZILOG_NORMAL_ACTIVE;
1868 * This routine is called whenever a serial port is opened. It
1869 * enables interrupts for a serial port, linking in its ZILOG structure into
1870 * the IRQ chain. It also performs the serial-specific
1871 * initialization for the tty structure.
1873 int zs_open(struct tty_struct *tty, struct file * filp)
1875 struct sun_serial *info;
1878 line = MINOR(tty->device) - tty->driver.minor_start;
1880 /* The zilog lines for the mouse/keyboard must be
1881 * opened using their respective drivers.
1883 if ((line < 0) || (line >= NUM_CHANNELS))
1885 if((line == KEYBOARD_LINE) || (line == MOUSE_LINE))
1887 info = zs_soft + line;
1888 /* Is the kgdb running over this line? */
1889 if (info->kgdb_channel)
1891 if (serial_paranoia_check(info, tty->device, "zs_open"))
1893 #ifdef SERIAL_DEBUG_OPEN
1894 printk("zs_open %s%d, count = %d\n", tty->driver.name, info->line,
1897 if (info->tty != 0 && info->tty != tty) {
1899 printk("zs_open %s%d, tty overwrite.\n", tty->driver.name, info->line);
1904 unsigned long page = get_free_page(GFP_KERNEL);
1910 tmp_buf = (unsigned char *) page;
1914 tty->driver_data = info;
1918 * Start up serial port
1920 retval = startup(info);
1924 retval = block_til_ready(tty, filp, info);
1926 #ifdef SERIAL_DEBUG_OPEN
1927 printk("zs_open returning after block_til_ready with %d\n",
1933 if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1934 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1935 *tty->termios = info->normal_termios;
1937 *tty->termios = info->callout_termios;
1941 #ifdef CONFIG_SERIAL_CONSOLE
1942 if (zs_console.cflag && zs_console.index == line) {
1943 tty->termios->c_cflag = zs_console.cflag;
1944 zs_console.cflag = 0;
1949 info->session = current->session;
1950 info->pgrp = current->pgrp;
1952 #ifdef SERIAL_DEBUG_OPEN
1953 printk("zs_open ttys%d successful...", info->line);
1958 /* Finally, routines used to initialize the serial driver. */
1960 static void show_serial_version(void)
1962 char *revision = "$Revision: 1.68.2.2 $";
1965 version = strchr(revision, ' ');
1966 p = strchr(++version, ' ');
1968 printk("Sparc Zilog8530 serial driver version %s\n", version);
1972 /* Probe the PROM for the request zs chip number.
1974 * Note: The Sun Voyager shows two addresses and two intr for it's
1975 * Zilogs, what the second does, I don't know. It does work
1976 * with using only the first number of each property. Also
1977 * we have a special version for sun4u.
1980 static struct sun_zslayout * __init get_zs(int chip)
1982 unsigned int vaddr[2] = { 0, 0 };
1983 unsigned long mapped_addr = 0;
1984 int busnode, seen, zsnode, sun4u_ino;
1987 if(chip < 0 || chip >= NUM_SERIAL) {
1988 prom_printf("get_zs bogon zs chip number");
1993 busnode = central_bus->child->prom_node;
1995 busnode = prom_searchsiblings(prom_getchild(prom_root_node), "sbus");
1996 if(busnode == 0 || busnode == -1) {
1997 prom_printf("get_zs: no zs bus to search");
2000 zsnode = prom_getchild(busnode);
2005 zsnode = prom_searchsiblings(zsnode, "zs");
2006 slave = prom_getintdefault(zsnode, "slave", -1);
2007 if((slave == chip) || (seen == chip)) {
2008 int len = prom_getproperty(zsnode, "address",
2009 (void *) vaddr, sizeof(vaddr));
2011 if(len == -1 || central_bus != NULL) {
2012 struct sbus_bus *sbus = NULL;
2013 struct sbus_dev *sdev = NULL;
2015 /* "address" property is not guarenteed,
2016 * everything in I/O is implicitly mapped
2017 * anyways by our clever TLB miss handling
2018 * scheme, so don't fail here. -DaveM
2020 if (central_bus == NULL) {
2021 for_each_sbus(sbus) {
2022 for_each_sbusdev(sdev, sbus) {
2023 if (sdev->prom_node == zsnode)
2029 if (sdev == NULL && central_bus == NULL)
2031 if (central_bus == NULL) {
2033 sbus_ioremap(&sdev->resource[0], 0,
2034 PAGE_SIZE, "Zilog Registers");
2036 struct linux_prom_registers zsregs[1];
2039 err = prom_getproperty(zsnode, "reg",
2043 prom_printf("ZS: Cannot map Zilog regs.\n");
2046 apply_fhc_ranges(central_bus->child, &zsregs[0], 1);
2047 apply_central_ranges(central_bus, &zsregs[0], 1);
2049 ((((u64)zsregs[0].which_io)<<32UL)|
2050 ((u64)zsregs[0].phys_addr));
2052 } else if(len % sizeof(unsigned int)) {
2053 prom_printf("WHOOPS: proplen for %s "
2054 "was %d, need multiple of "
2055 "%d\n", "address", len,
2056 sizeof(unsigned int));
2057 panic("zilog: address property");
2059 zs_nodes[chip] = zsnode;
2060 len = prom_getproperty(zsnode, "interrupts",
2061 (char *) &sun4u_ino,
2062 (sizeof(sun4u_ino)));
2065 unsigned long iclr, imap;
2067 iclr = central_bus->child->fhc_regs.uregs + FHC_UREGS_ICLR;
2068 imap = central_bus->child->fhc_regs.uregs + FHC_UREGS_IMAP;
2069 irq = zilog_irq = build_irq(12, 0, iclr, imap);
2072 sbus_build_irq(sbus_root, sun4u_ino);
2077 zsnode = prom_getsibling(zsnode);
2081 panic("get_zs: whee chip not found");
2082 if(!vaddr[0] && !mapped_addr)
2083 panic("get_zs: whee no serial chip mappable");
2084 if (mapped_addr != 0) {
2085 return (struct sun_zslayout *) mapped_addr;
2087 return (struct sun_zslayout *) prom_virt_to_phys((unsigned long)vaddr[0], 0);
2090 #else /* !(__sparc_v9__) */
2091 static struct sun_zslayout * __init get_zs(int chip)
2093 struct linux_prom_irqs tmp_irq[2];
2094 unsigned int paddr = 0;
2095 unsigned int vaddr[2] = { 0, 0 };
2096 int zsnode, tmpnode, iospace, slave, len;
2097 int cpunode = 0, bbnode = 0;
2102 if(chip < 0 || chip >= NUM_SERIAL)
2103 panic("get_zs bogon zs chip number");
2105 if(sparc_cpu_model == sun4) {
2106 struct resource dummy_resource;
2108 /* Grrr, these have to be hardcoded aieee */
2120 zilog_irq = irq = 12;
2121 dummy_resource.start = paddr;
2122 dummy_resource.end = paddr + 8 - 1;
2123 dummy_resource.flags = IORESOURCE_IO;
2124 vaddr[0] = sbus_ioremap(&dummy_resource, 0,
2127 /* Can use the prom for other machine types */
2128 zsnode = prom_getchild(prom_root_node);
2129 if (sparc_cpu_model == sun4d) {
2135 while (tmpnode && (tmpnode = prom_searchsiblings(tmpnode, "cpu-unit"))) {
2136 bbnode = prom_getchild(tmpnode);
2137 if (bbnode && (bbnode = prom_searchsiblings(bbnode, "bootbus"))) {
2138 if (no == (chip >> 1)) {
2140 zsnode = prom_getchild(bbnode);
2141 chipid = (chip & 1);
2146 tmpnode = prom_getsibling(tmpnode);
2149 panic ("get_zs: couldn't find %dth bootbus\n", chip >> 1);
2151 tmpnode = prom_searchsiblings(zsnode, "obio");
2153 zsnode = prom_getchild(tmpnode);
2156 panic("get_zs no zs serial prom node");
2158 zsnode = prom_searchsiblings(zsnode, "zs");
2159 slave = prom_getintdefault(zsnode, "slave", -1);
2160 if(slave == chipid) {
2161 /* The one we want */
2162 if (sparc_cpu_model != sun4d) {
2163 len = prom_getproperty(zsnode, "address",
2166 if (len % sizeof(unsigned int)) {
2167 prom_printf("WHOOPS: proplen for %s "
2168 "was %d, need multiple of "
2169 "%d\n", "address", len,
2170 sizeof(unsigned int));
2171 panic("zilog: address property");
2174 /* On sun4d don't have address property :( */
2175 struct linux_prom_registers zsreg[4];
2176 struct resource res;
2178 if (prom_getproperty(zsnode, "reg", (char *)zsreg, sizeof(zsreg)) == -1) {
2179 prom_printf ("Cannot map zs regs\n");
2182 prom_apply_generic_ranges(bbnode, cpunode, zsreg, 1);
2183 res.start = zsreg[0].phys_addr;
2184 res.end = res.start + 8 - 1;
2185 res.flags = zsreg[0].which_io | IORESOURCE_IO;
2186 vaddr[0] = sbus_ioremap(&res, 0,
2189 zs_nodes[chip] = zsnode;
2190 len = prom_getproperty(zsnode, "intr",
2193 if (len % sizeof(struct linux_prom_irqs)) {
2195 "WHOOPS: proplen for %s "
2196 "was %d, need multiple of "
2197 "%d\n", "intr", len,
2198 sizeof(struct linux_prom_irqs));
2199 panic("zilog: intr property");
2202 irq = zilog_irq = tmp_irq[0].pri;
2204 if(tmp_irq[0].pri != irq)
2205 panic("zilog: bogon irqs");
2209 zsnode = prom_getsibling(zsnode);
2212 panic("get_zs whee chip not found");
2215 panic("get_zs whee no serial chip mappable");
2217 return (struct sun_zslayout *)(unsigned long) vaddr[0];
2220 /* This is for the auto baud rate detection in the mouse driver. */
2221 void zs_change_mouse_baud(int newbaud)
2223 int channel = MOUSE_LINE;
2226 zs_soft[channel].zs_baud = newbaud;
2227 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2228 (ZS_CLOCK / zs_soft[channel].clk_divisor));
2229 write_zsreg(zs_soft[channel].zs_channel, R12, (brg & 0xff));
2230 write_zsreg(zs_soft[channel].zs_channel, R13, ((brg >> 8) & 0xff));
2233 void __init zs_init_alloc_failure(const char *table_name)
2235 prom_printf("zs_probe: Cannot alloc %s.\n", table_name);
2239 void * __init zs_alloc_bootmem(unsigned long size)
2243 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
2245 memset(ret, 0, size);
2250 void __init zs_alloc_tables(void)
2252 zs_chips = (struct sun_zslayout **)
2253 zs_alloc_bootmem(NUM_SERIAL * sizeof(struct sun_zslayout *));
2254 if (zs_chips == NULL)
2255 zs_init_alloc_failure("zs_chips");
2256 zs_channels = (struct sun_zschannel **)
2257 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct sun_zschannel *));
2258 if (zs_channels == NULL)
2259 zs_init_alloc_failure("zs_channels");
2261 zs_alloc_bootmem(NUM_SERIAL * sizeof(int));
2262 if (zs_nodes == NULL)
2263 zs_init_alloc_failure("zs_nodes");
2264 zs_soft = (struct sun_serial *)
2265 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct sun_serial));
2266 if (zs_soft == NULL)
2267 zs_init_alloc_failure("zs_soft");
2268 zs_ttys = (struct tty_struct *)
2269 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct tty_struct));
2270 if (zs_ttys == NULL)
2271 zs_init_alloc_failure("zs_ttys");
2272 serial_table = (struct tty_struct **)
2273 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct tty_struct *));
2274 if (serial_table == NULL)
2275 zs_init_alloc_failure("serial_table");
2276 serial_termios = (struct termios **)
2277 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct termios *));
2278 if (serial_termios == NULL)
2279 zs_init_alloc_failure("serial_termios");
2280 serial_termios_locked = (struct termios **)
2281 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct termios *));
2282 if (serial_termios_locked == NULL)
2283 zs_init_alloc_failure("serial_termios_locked");
2286 int __init zs_probe(void)
2290 if(sparc_cpu_model == sun4)
2295 node = prom_getchild(prom_root_node);
2296 if (sparc_cpu_model == sun4d) {
2299 while (node && (node = prom_searchsiblings(node, "cpu-unit"))) {
2300 bbnode = prom_getchild(node);
2301 if (bbnode && prom_searchsiblings(bbnode, "bootbus"))
2303 node = prom_getsibling(node);
2308 else if (sparc_cpu_model == sun4u) {
2311 /* Central bus zilogs must be checked for first,
2312 * since Enterprise boxes might have SBUSes as well.
2314 central_node = prom_finddevice("/central");
2315 if(central_node != 0 && central_node != -1)
2316 node = prom_searchsiblings(prom_getchild(central_node), "fhc");
2318 node = prom_searchsiblings(node, "sbus");
2319 if(node != 0 && node != -1)
2320 node = prom_getchild(node);
2321 if(node == 0 || node == -1)
2324 #endif /* __sparc_v9__ */
2326 node = prom_searchsiblings(node, "obio");
2328 node = prom_getchild(node);
2333 node = prom_searchsiblings(node, "zs");
2342 /* Fill in rs_ops struct... */
2343 #ifdef CONFIG_SERIAL_CONSOLE
2344 sunserial_setinitfunc(zs_console_init);
2346 sunserial_setinitfunc(zs_init);
2347 rs_ops.rs_kgdb_hook = zs_kgdb_hook;
2348 rs_ops.rs_change_mouse_baud = zs_change_mouse_baud;
2350 sunkbd_setinitfunc(sun_kbd_init);
2351 kbd_ops.compute_shiftstate = sun_compute_shiftstate;
2352 kbd_ops.setledstate = sun_setledstate;
2353 kbd_ops.getledstate = sun_getledstate;
2354 kbd_ops.setkeycode = sun_setkeycode;
2355 kbd_ops.getkeycode = sun_getkeycode;
2356 #if defined(__sparc_v9__) && defined(CONFIG_PCI)
2357 sunkbd_install_keymaps(sun_key_maps, sun_keymap_count,
2358 sun_func_buf, sun_func_table,
2359 sun_funcbufsize, sun_funcbufleft,
2360 sun_accent_table, sun_accent_table_size);
2365 static inline void zs_prepare(void)
2368 unsigned long flags;
2373 save_and_cli(flags);
2375 /* Set up our interrupt linked list */
2376 zs_chain = &zs_soft[0];
2377 for(channel = 0; channel < NUM_CHANNELS - 1; channel++) {
2378 zs_soft[channel].zs_next = &zs_soft[channel + 1];
2379 zs_soft[channel].line = channel;
2381 zs_soft[channel].zs_next = 0;
2383 /* Initialize Softinfo */
2384 for(chip = 0; chip < NUM_SERIAL; chip++) {
2385 /* If we are doing kgdb over one of the channels on
2386 * chip zero, kgdb_channel will be set to 1 by the
2387 * zs_kgdb_hook() routine below.
2389 if(!zs_chips[chip]) {
2390 zs_chips[chip] = get_zs(chip);
2391 /* Two channels per chip */
2392 zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2393 zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2394 zs_soft[(chip*2)].kgdb_channel = 0;
2395 zs_soft[(chip*2)+1].kgdb_channel = 0;
2398 /* First, set up channel A on this chip. */
2400 zs_soft[channel].zs_channel = zs_channels[channel];
2401 zs_soft[channel].change_needed = 0;
2402 zs_soft[channel].clk_divisor = 16;
2403 zs_soft[channel].cons_keyb = 0;
2404 zs_soft[channel].cons_mouse = 0;
2405 zs_soft[channel].channelA = 1;
2407 /* Now, channel B */
2409 zs_soft[channel].zs_channel = zs_channels[channel];
2410 zs_soft[channel].change_needed = 0;
2411 zs_soft[channel].clk_divisor = 16;
2412 zs_soft[channel].cons_keyb = 0;
2413 zs_soft[channel].cons_mouse = 0;
2414 zs_soft[channel].channelA = 0;
2417 restore_flags(flags);
2420 int __init zs_init(void)
2422 int channel, brg, i;
2423 unsigned long flags;
2424 struct sun_serial *info;
2427 /* Setup base handler, and timer table. */
2428 init_bh(SERIAL_BH, do_serial_bh);
2430 show_serial_version();
2432 /* Initialize the tty_driver structure */
2433 /* SPARC: Not all of this is exactly right for us. */
2435 memset(&serial_driver, 0, sizeof(struct tty_driver));
2436 serial_driver.magic = TTY_DRIVER_MAGIC;
2437 serial_driver.driver_name = "serial";
2438 #ifdef CONFIG_DEVFS_FS
2439 serial_driver.name = "tts/%d";
2441 serial_driver.name = "ttyS";
2443 serial_driver.major = TTY_MAJOR;
2444 serial_driver.minor_start = 64;
2445 serial_driver.num = NUM_CHANNELS;
2446 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2447 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2448 serial_driver.init_termios = tty_std_termios;
2449 serial_driver.init_termios.c_cflag =
2450 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2451 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2452 serial_driver.refcount = &serial_refcount;
2453 serial_driver.table = serial_table;
2454 serial_driver.termios = serial_termios;
2455 serial_driver.termios_locked = serial_termios_locked;
2457 serial_driver.open = zs_open;
2458 serial_driver.close = zs_close;
2459 serial_driver.write = zs_write;
2460 serial_driver.flush_chars = zs_flush_chars;
2461 serial_driver.write_room = zs_write_room;
2462 serial_driver.chars_in_buffer = zs_chars_in_buffer;
2463 serial_driver.flush_buffer = zs_flush_buffer;
2464 serial_driver.ioctl = zs_ioctl;
2465 serial_driver.throttle = zs_throttle;
2466 serial_driver.unthrottle = zs_unthrottle;
2467 serial_driver.set_termios = zs_set_termios;
2468 serial_driver.stop = zs_stop;
2469 serial_driver.start = zs_start;
2470 serial_driver.hangup = zs_hangup;
2472 /* I'm too lazy, someone write versions of this for us. -DaveM */
2473 /* I just did. :-) -AIB 2001-12-23 */
2474 serial_driver.read_proc = zs_read_proc;
2477 * The callout device is just like normal device except for
2478 * major number and the subtype code.
2480 callout_driver = serial_driver;
2481 callout_driver.name = "cua/%d";
2482 callout_driver.major = TTYAUX_MAJOR;
2483 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2484 callout_driver.read_proc = 0;
2485 callout_driver.proc_entry = 0;
2487 if (tty_register_driver(&serial_driver))
2488 panic("Couldn't register serial driver\n");
2489 if (tty_register_driver(&callout_driver))
2490 panic("Couldn't register callout driver\n");
2492 save_flags(flags); cli();
2494 /* Initialize Softinfo */
2497 /* Grab IRQ line before poking the chips so we do
2498 * not lose any interrupts.
2500 if (request_irq(zilog_irq, zs_interrupt, SA_SHIRQ,
2501 "Zilog8530", zs_chain)) {
2502 prom_printf("Unable to attach zs intr\n");
2506 /* Initialize Hardware */
2507 for(channel = 0; channel < NUM_CHANNELS; channel++) {
2508 /* Hardware reset each chip */
2509 if (!(channel & 1)) {
2510 write_zsreg(zs_soft[channel].zs_channel, R9, FHWRES);
2512 dummy = read_zsreg(zs_soft[channel].zs_channel, R0);
2515 if(channel == KEYBOARD_LINE) {
2516 zs_soft[channel].cons_keyb = 1;
2517 zs_soft[channel].parity_mask = 0xff;
2518 zs_kbdchan = zs_soft[channel].zs_channel;
2520 write_zsreg(zs_soft[channel].zs_channel, R4,
2521 (PAR_EVEN | X16CLK | SB1));
2522 write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2523 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2524 write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2525 write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2526 write_zsreg(zs_soft[channel].zs_channel, R11,
2528 zs_soft[channel].zs_baud = 1200;
2529 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2530 ZS_CLOCK/zs_soft[channel].clk_divisor);
2531 write_zsreg(zs_soft[channel].zs_channel, R12,
2533 write_zsreg(zs_soft[channel].zs_channel, R13,
2534 ((brg >> 8) & 0xff));
2535 write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2537 /* Enable Rx/Tx, IRQs, and inform kbd driver */
2538 write_zsreg(zs_soft[channel].zs_channel, R14,
2540 write_zsreg(zs_soft[channel].zs_channel, R3,
2542 write_zsreg(zs_soft[channel].zs_channel, R5,
2543 (Tx8 | TxENAB | DTR | RTS));
2545 write_zsreg(zs_soft[channel].zs_channel, R15,
2546 (DCDIE | CTSIE | TxUIE | BRKIE));
2547 write_zsreg(zs_soft[channel].zs_channel, R0,
2549 write_zsreg(zs_soft[channel].zs_channel, R0,
2552 write_zsreg(zs_soft[channel].zs_channel, R1,
2553 (EXT_INT_ENAB | INT_ALL_Rx));
2554 write_zsreg(zs_soft[channel].zs_channel, R9,
2556 ZS_CLEARERR(zs_soft[channel].zs_channel);
2557 ZS_CLEARFIFO(zs_soft[channel].zs_channel);
2558 } else if(channel == MOUSE_LINE) {
2559 zs_soft[channel].cons_mouse = 1;
2560 zs_soft[channel].parity_mask = 0xff;
2561 zs_mousechan = zs_soft[channel].zs_channel;
2563 write_zsreg(zs_soft[channel].zs_channel, R4,
2564 (PAR_EVEN | X16CLK | SB1));
2565 write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2566 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2567 write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2568 write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2569 write_zsreg(zs_soft[channel].zs_channel, R11,
2572 zs_soft[channel].zs_baud = 4800;
2573 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2574 ZS_CLOCK/zs_soft[channel].clk_divisor);
2575 write_zsreg(zs_soft[channel].zs_channel, R12,
2577 write_zsreg(zs_soft[channel].zs_channel, R13,
2578 ((brg >> 8) & 0xff));
2579 write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2581 /* Enable Rx, IRQs, and inform mouse driver */
2582 write_zsreg(zs_soft[channel].zs_channel, R14,
2584 write_zsreg(zs_soft[channel].zs_channel, R3,
2586 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2588 write_zsreg(zs_soft[channel].zs_channel, R15,
2589 (DCDIE | CTSIE | TxUIE | BRKIE));
2590 write_zsreg(zs_soft[channel].zs_channel, R0,
2592 write_zsreg(zs_soft[channel].zs_channel, R0,
2595 write_zsreg(zs_soft[channel].zs_channel, R1,
2596 (EXT_INT_ENAB | INT_ALL_Rx));
2597 write_zsreg(zs_soft[channel].zs_channel, R9,
2601 } else if (zs_soft[channel].is_cons) {
2602 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2603 ZS_CLOCK/zs_soft[channel].clk_divisor);
2604 zscons_regs[12] = brg & 0xff;
2605 zscons_regs[13] = (brg >> 8) & 0xff;
2607 memcpy(zs_soft[channel].curregs, zscons_regs, sizeof(zscons_regs));
2608 load_zsregs(&zs_soft[channel], zscons_regs);
2610 ZS_CLEARERR(zs_soft[channel].zs_channel);
2611 ZS_CLEARFIFO(zs_soft[channel].zs_channel);
2612 } else if (zs_soft[channel].kgdb_channel) {
2613 /* If this is the kgdb line, enable interrupts because
2614 * we now want to receive the 'control-c' character
2615 * from the client attached to us asynchronously.
2617 zs_soft[channel].parity_mask = 0xff;
2618 kgdb_chaninit(&zs_soft[channel], 1,
2619 zs_soft[channel].zs_baud);
2621 zs_soft[channel].parity_mask = 0xff;
2622 write_zsreg(zs_soft[channel].zs_channel, R4,
2623 (PAR_EVEN | X16CLK | SB1));
2624 write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2625 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2626 write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2627 write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2628 write_zsreg(zs_soft[channel].zs_channel, R11,
2630 zs_soft[channel].zs_baud = 9600;
2631 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2632 ZS_CLOCK/zs_soft[channel].clk_divisor);
2633 write_zsreg(zs_soft[channel].zs_channel, R12,
2635 write_zsreg(zs_soft[channel].zs_channel, R13,
2636 ((brg >> 8) & 0xff));
2637 write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2638 write_zsreg(zs_soft[channel].zs_channel, R14,
2640 write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2641 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2642 write_zsreg(zs_soft[channel].zs_channel, R15, DCDIE);
2643 write_zsreg(zs_soft[channel].zs_channel, R9, NV | MIE);
2644 write_zsreg(zs_soft[channel].zs_channel, R0,
2646 write_zsreg(zs_soft[channel].zs_channel, R0,
2651 for (info = zs_chain, i=0; info; info = info->zs_next, i++) {
2652 info->magic = SERIAL_MAGIC;
2653 info->port = (long) info->zs_channel;
2656 info->irq = zilog_irq;
2657 info->custom_divisor = 16;
2658 info->close_delay = 50;
2659 info->closing_wait = 3000;
2663 info->blocked_open = 0;
2664 info->tqueue.routine = do_softint;
2665 info->tqueue.data = info;
2666 info->tqueue_hangup.routine = do_serial_hangup;
2667 info->tqueue_hangup.data = info;
2668 info->callout_termios = callout_driver.init_termios;
2669 info->normal_termios = serial_driver.init_termios;
2670 init_waitqueue_head(&info->open_wait);
2671 init_waitqueue_head(&info->close_wait);
2672 printk("tty%02d at 0x%04x (irq = %s)", info->line,
2673 info->port, __irq_itoa(info->irq));
2674 printk(" is a Zilog8530\n");
2677 restore_flags(flags);
2679 keyboard_zsinit(kbd_put_char);
2683 /* This is called at boot time to prime the kgdb serial debugging
2684 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2685 * for /dev/ttyb which is determined in setup_arch() from the
2686 * boot command line flags.
2688 static void __init zs_kgdb_hook(int tty_num)
2692 if(!zs_chips[chip]) {
2693 zs_chips[chip] = get_zs(chip);
2694 /* Two channels per chip */
2695 zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2696 zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2698 zs_soft[tty_num].zs_channel = zs_channels[tty_num];
2699 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2700 zs_soft[tty_num].change_needed = 0;
2701 zs_soft[tty_num].clk_divisor = 16;
2702 zs_soft[tty_num].zs_baud = 9600;
2703 zs_soft[tty_num].kgdb_channel = 1; /* This runs kgdb */
2704 zs_soft[tty_num ^ 1].kgdb_channel = 0; /* This does not */
2705 /* Turn on transmitter/receiver at 8-bits/char */
2706 kgdb_chaninit(&zs_soft[tty_num], 0, 9600);
2707 ZS_CLEARERR(zs_kgdbchan);
2708 ZS_CLEARFIFO(zs_kgdbchan);
2711 #ifdef CONFIG_SERIAL_CONSOLE
2713 /* This is for console output over ttya/ttyb */
2715 zs_console_putchar(struct sun_serial *info, char ch)
2717 int loops = ZS_PUT_CHAR_MAX_DELAY;
2718 unsigned long flags;
2720 if(!info->zs_channel)
2723 save_flags(flags); cli();
2724 zs_put_char(info->zs_channel, ch);
2725 while (!(read_zsreg(info->zs_channel, R1) & ALL_SNT) && --loops)
2727 restore_flags(flags);
2730 #ifdef SERIAL_CONSOLE_FAIR_OUTPUT
2732 * Fair output driver allows a process to speak.
2734 static void zs_fair_output(struct sun_serial *info)
2736 unsigned long flags;
2737 int left; /* Output no more than that */
2742 if (info->xmit_buf == NULL)
2745 save_flags(flags); cli();
2746 left = info->xmit_cnt;
2748 c = info->xmit_buf[info->xmit_tail];
2749 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
2751 restore_flags(flags);
2753 zs_console_putchar(info, c);
2756 left = MIN(info->xmit_cnt, left-1);
2759 /* Last character is being transmitted now (hopefully). */
2760 sbus_writeb(RES_Tx_P, &info->zs_channel->control);
2762 ZSLOG(REGCTRL, RES_Tx_P, 1);
2764 restore_flags(flags);
2770 * zs_console_write is registered for printk.
2773 zs_console_write(struct console *con, const char *s, unsigned count)
2775 struct sun_serial *info;
2778 info = zs_soft + con->index;
2780 for (i = 0; i < count; i++, s++) {
2782 zs_console_putchar(info, '\r');
2783 zs_console_putchar(info, *s);
2785 #ifdef SERIAL_CONSOLE_FAIR_OUTPUT
2786 /* Comment this if you want to have a strict interrupt-driven output */
2787 zs_fair_output(info);
2791 static kdev_t zs_console_device(struct console *con)
2793 return MKDEV(TTY_MAJOR, 64 + con->index);
2796 static int __init zs_console_setup(struct console *con, char *options)
2798 static struct tty_struct c_tty;
2799 static struct termios c_termios;
2800 struct sun_serial *info;
2803 info = zs_soft + con->index;
2806 printk("Console: ttyS%d (Zilog8530)\n", info->line);
2808 sunserial_console_termios(con);
2809 memset(&c_tty, 0, sizeof(c_tty));
2810 memset(&c_termios, 0, sizeof(c_termios));
2811 c_tty.termios = &c_termios;
2812 c_termios.c_cflag = con->cflag;
2813 baud = tty_get_baud_rate(&c_tty);
2815 info->zs_baud = baud;
2817 switch (con->cflag & CSIZE) {
2819 zscons_regs[3] = Rx5 | RxENAB;
2820 zscons_regs[5] = Tx5 | TxENAB;
2821 info->parity_mask = 0x1f;
2824 zscons_regs[3] = Rx6 | RxENAB;
2825 zscons_regs[5] = Tx6 | TxENAB;
2826 info->parity_mask = 0x3f;
2829 zscons_regs[3] = Rx7 | RxENAB;
2830 zscons_regs[5] = Tx7 | TxENAB;
2831 info->parity_mask = 0x7f;
2835 zscons_regs[3] = Rx8 | RxENAB;
2836 zscons_regs[5] = Tx8 | TxENAB;
2837 info->parity_mask = 0xff;
2840 zscons_regs[5] |= DTR;
2842 if (con->cflag & PARENB)
2843 zscons_regs[4] |= PAR_ENAB;
2844 if (!(con->cflag & PARODD))
2845 zscons_regs[4] |= PAR_EVEN;
2847 if (con->cflag & CSTOPB)
2848 zscons_regs[4] |= SB2;
2850 zscons_regs[4] |= SB1;
2852 brg = BPS_TO_BRG(baud, ZS_CLOCK / info->clk_divisor);
2853 zscons_regs[12] = brg & 0xff;
2854 zscons_regs[13] = (brg >> 8) & 0xff;
2856 memcpy(info->curregs, zscons_regs, sizeof(zscons_regs));
2857 load_zsregs(info, zscons_regs);
2859 ZS_CLEARERR(info->zs_channel);
2860 ZS_CLEARFIFO(info->zs_channel);
2864 static struct console zs_console = {
2866 write: zs_console_write,
2867 device: zs_console_device,
2868 setup: zs_console_setup,
2869 flags: CON_PRINTBUFFER,
2873 static int __init zs_console_init(void)
2875 extern int con_is_present(void);
2877 if (con_is_present())
2880 zs_console.index = serial_console - 1;
2881 register_console(&zs_console);
2885 #endif /* CONFIG_SERIAL_CONSOLE */