1 /* $Id: zs.c,v 1.1.1.1 2005/04/11 02:50:34 jack 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 sparc we only need to flush single writes to ensure
72 #define ZSDELAY() udelay(5)
73 #define ZSDELAY_LONG() udelay(20)
74 #define ZS_WSYNC(channel) do { } while(0)
77 #define ZSDELAY_LONG()
78 #define ZS_WSYNC(__channel) \
79 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)) {
749 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
750 tty->ldisc.write_wakeup)
751 (tty->ldisc.write_wakeup)(tty);
752 wake_up_interruptible(&tty->write_wait);
757 * This routine is called from the scheduler tqueue when the interrupt
758 * routine has signalled that a hangup has occurred. The path of
759 * hangup processing is:
761 * serial interrupt routine -> (scheduler tqueue) ->
762 * do_serial_hangup() -> tty->hangup() -> zs_hangup()
765 static void do_serial_hangup(void *private_)
767 struct sun_serial *info = (struct sun_serial *) private_;
768 struct tty_struct *tty;
773 #ifdef SERIAL_DEBUG_OPEN
774 printk("do_serial_hangup<%p: tty-%d\n",
775 __builtin_return_address(0), info->line);
781 static int startup(struct sun_serial * info)
785 if (info->flags & ZILOG_INITIALIZED)
788 if (!info->xmit_buf) {
789 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
794 save_flags(flags); cli();
796 #ifdef SERIAL_DEBUG_OPEN
797 printk("Starting up tty-%d (irq %d)...\n", info->line, info->irq);
801 * Clear the FIFO buffers and disable them
802 * (they will be reenabled in change_speed())
804 ZS_CLEARFIFO(info->zs_channel);
805 info->xmit_fifo_size = 1;
808 * Clear the interrupt registers.
810 sbus_writeb(ERR_RES, &info->zs_channel->control);
812 ZS_WSYNC(info->zs_channel);
813 ZSLOG(REGCTRL, ERR_RES, 1);
815 sbus_writeb(RES_H_IUS, &info->zs_channel->control);
817 ZS_WSYNC(info->zs_channel);
818 ZSLOG(REGCTRL, RES_H_IUS, 1);
821 * Now, initialize the Zilog
826 * Finally, enable sequencing and interrupts
828 info->curregs[1] |= (info->curregs[1] & ~(RxINT_MASK)) |
829 (EXT_INT_ENAB | INT_ALL_Rx);
830 info->curregs[3] |= (RxENAB | Rx8);
831 /* We enable Tx interrupts as needed. */
832 info->curregs[5] |= (TxENAB | Tx8);
833 info->curregs[9] |= (NV | MIE);
834 write_zsreg(info->zs_channel, 3, info->curregs[3]);
835 write_zsreg(info->zs_channel, 5, info->curregs[5]);
836 write_zsreg(info->zs_channel, 9, info->curregs[9]);
839 * And clear the interrupt registers again for luck.
841 sbus_writeb(ERR_RES, &info->zs_channel->control);
843 ZS_WSYNC(info->zs_channel);
844 ZSLOG(REGCTRL, ERR_RES, 1);
846 sbus_writeb(RES_H_IUS, &info->zs_channel->control);
848 ZS_WSYNC(info->zs_channel);
849 ZSLOG(REGCTRL, RES_H_IUS, 1);
852 clear_bit(TTY_IO_ERROR, &info->tty->flags);
853 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
856 * and set the speed of the serial port
860 info->flags |= ZILOG_INITIALIZED;
861 restore_flags(flags);
866 * This routine will shutdown a serial port; interrupts are disabled, and
867 * DTR is dropped if the hangup on close termio flag is on.
869 static void shutdown(struct sun_serial * info)
873 if (!(info->flags & ZILOG_INITIALIZED))
876 #ifdef SERIAL_DEBUG_OPEN
877 printk("Shutting down serial port %d (irq %d)....", info->line,
881 save_flags(flags); cli(); /* Disable interrupts */
883 if (info->xmit_buf) {
884 free_page((unsigned long) info->xmit_buf);
889 set_bit(TTY_IO_ERROR, &info->tty->flags);
891 info->flags &= ~ZILOG_INITIALIZED;
892 restore_flags(flags);
896 * This routine is called to set the UART divisor registers to match
897 * the specified baud rate for a serial port.
899 static void change_speed(struct sun_serial *info)
905 if (!info->tty || !info->tty->termios)
907 cflag = info->tty->termios->c_cflag;
910 baud = tty_get_baud_rate(info->tty);
912 if ((baud == 38400) &&
913 ((info->flags & ZILOG_SPD_MASK) == ZILOG_SPD_CUST))
914 quot = info->custom_divisor;
917 info->zs_baud = info->baud_base / quot;
918 info->clk_divisor = 16;
920 info->curregs[4] = X16CLK;
921 info->curregs[11] = TCBR | RCBR;
922 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
923 info->curregs[12] = (brg & 255);
924 info->curregs[13] = ((brg >> 8) & 255);
925 info->curregs[14] = BRSRC | BRENAB;
928 info->zs_baud = baud;
929 info->clk_divisor = 16;
931 info->curregs[4] = X16CLK;
932 info->curregs[11] = TCBR | RCBR;
933 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
934 info->curregs[12] = (brg & 255);
935 info->curregs[13] = ((brg >> 8) & 255);
936 info->curregs[14] = BRSRC | BRENAB;
943 /* byte size and parity */
944 switch (cflag & CSIZE) {
946 info->curregs[3] &= ~(RxN_MASK);
947 info->curregs[3] |= Rx5;
948 info->curregs[5] &= ~(TxN_MASK);
949 info->curregs[5] |= Tx5;
950 info->parity_mask = 0x1f;
953 info->curregs[3] &= ~(RxN_MASK);
954 info->curregs[3] |= Rx6;
955 info->curregs[5] &= ~(TxN_MASK);
956 info->curregs[5] |= Tx6;
957 info->parity_mask = 0x3f;
960 info->curregs[3] &= ~(RxN_MASK);
961 info->curregs[3] |= Rx7;
962 info->curregs[5] &= ~(TxN_MASK);
963 info->curregs[5] |= Tx7;
964 info->parity_mask = 0x7f;
967 default: /* defaults to 8 bits */
968 info->curregs[3] &= ~(RxN_MASK);
969 info->curregs[3] |= Rx8;
970 info->curregs[5] &= ~(TxN_MASK);
971 info->curregs[5] |= Tx8;
972 info->parity_mask = 0xff;
975 info->curregs[4] &= ~(0x0c);
976 if (cflag & CSTOPB) {
977 info->curregs[4] |= SB2;
979 info->curregs[4] |= SB1;
981 if (cflag & PARENB) {
982 info->curregs[4] |= PAR_ENAB;
984 info->curregs[4] &= ~PAR_ENAB;
986 if (!(cflag & PARODD)) {
987 info->curregs[4] |= PAR_EVEN;
989 info->curregs[4] &= ~PAR_EVEN;
992 /* Load up the new values */
993 load_zsregs(info, info->curregs);
998 /* This is for mouse/keyboard output.
999 * XXX mouse output??? can we send it commands??? XXX
1001 static void kbd_put_char(unsigned char ch)
1003 struct sun_zschannel *chan = zs_kbdchan;
1004 unsigned long flags;
1009 save_flags(flags); cli();
1010 zs_put_char(chan, ch);
1011 restore_flags(flags);
1014 void mouse_put_char(char ch)
1016 struct sun_zschannel *chan = zs_mousechan;
1017 unsigned long flags;
1022 save_flags(flags); cli();
1023 zs_put_char(chan, ch);
1024 restore_flags(flags);
1027 /* These are for receiving and sending characters under the kgdb
1028 * source level kernel debugger.
1030 void putDebugChar(char kgdb_char)
1032 struct sun_zschannel *chan = zs_kgdbchan;
1034 while((sbus_readb(&chan->control) & Tx_BUF_EMP)==0)
1036 sbus_writeb(kgdb_char, &chan->data);
1038 ZSLOG(REGDATA, kgdb_char, 1);
1041 char getDebugChar(void)
1043 struct sun_zschannel *chan = zs_kgdbchan;
1047 val = sbus_readb(&chan->control);
1048 ZSLOG(REGCTRL, val, 0);
1050 } while ((val & Rx_CH_AV) == 0);
1052 val = sbus_readb(&chan->data);
1053 ZSLOG(REGDATA, val, 0);
1057 static void zs_flush_chars(struct tty_struct *tty)
1059 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1060 unsigned long flags;
1062 if (serial_paranoia_check(info, tty->device, "zs_flush_chars"))
1065 save_flags(flags); cli();
1066 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1070 /* Enable transmitter */
1071 info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1072 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1073 info->curregs[5] |= TxENAB;
1074 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1077 * Send a first (bootstrapping) character. A best solution is
1078 * to call transmit_chars() here which handles output in a
1079 * generic way. Current transmit_chars() not only transmits,
1080 * but resets interrupts also what we do not desire here.
1081 * XXX Discuss with David.
1083 zs_put_char(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
1084 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1088 restore_flags(flags);
1091 static int zs_write(struct tty_struct * tty, int from_user,
1092 const unsigned char *buf, int count)
1094 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1095 unsigned long flags;
1098 if (serial_paranoia_check(info, tty->device, "zs_write"))
1101 if (!info || !info->xmit_buf || !tmp_buf)
1108 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1109 SERIAL_XMIT_SIZE - info->xmit_head));
1112 c -= copy_from_user(tmp_buf, buf, c);
1119 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1120 SERIAL_XMIT_SIZE - info->xmit_head));
1121 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1122 info->xmit_head = ((info->xmit_head + c) &
1123 (SERIAL_XMIT_SIZE - 1));
1124 info->xmit_cnt += c;
1125 restore_flags(flags);
1135 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1136 SERIAL_XMIT_SIZE - info->xmit_head));
1138 restore_flags(flags);
1141 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1142 info->xmit_head = ((info->xmit_head + c) &
1143 (SERIAL_XMIT_SIZE - 1));
1144 info->xmit_cnt += c;
1145 restore_flags(flags);
1153 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
1154 /* Enable transmitter */
1155 info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1156 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1157 info->curregs[5] |= TxENAB;
1158 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1160 zs_put_char(info->zs_channel,
1161 info->xmit_buf[info->xmit_tail++]);
1162 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1167 restore_flags(flags);
1171 static int zs_write_room(struct tty_struct *tty)
1173 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1176 if (serial_paranoia_check(info, tty->device, "zs_write_room"))
1178 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1184 static int zs_chars_in_buffer(struct tty_struct *tty)
1186 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1188 if (serial_paranoia_check(info, tty->device, "zs_chars_in_buffer"))
1190 return info->xmit_cnt;
1193 static void zs_flush_buffer(struct tty_struct *tty)
1195 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1197 if (serial_paranoia_check(info, tty->device, "zs_flush_buffer"))
1200 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1202 wake_up_interruptible(&tty->write_wait);
1203 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1204 tty->ldisc.write_wakeup)
1205 (tty->ldisc.write_wakeup)(tty);
1209 * ------------------------------------------------------------
1212 * This routine is called by the upper-layer tty layer to signal that
1213 * incoming characters should be throttled.
1214 * ------------------------------------------------------------
1216 static void zs_throttle(struct tty_struct * tty)
1218 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1219 #ifdef SERIAL_DEBUG_THROTTLE
1222 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1223 tty->ldisc.chars_in_buffer(tty));
1226 if (serial_paranoia_check(info, tty->device, "zs_throttle"))
1230 info->x_char = STOP_CHAR(tty);
1232 /* Turn off RTS line */
1234 info->curregs[5] &= ~RTS;
1235 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1239 static void zs_unthrottle(struct tty_struct * tty)
1241 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1242 #ifdef SERIAL_DEBUG_THROTTLE
1245 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1246 tty->ldisc.chars_in_buffer(tty));
1249 if (serial_paranoia_check(info, tty->device, "zs_unthrottle"))
1256 info->x_char = START_CHAR(tty);
1259 /* Assert RTS line */
1261 info->curregs[5] |= RTS;
1262 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1267 * ------------------------------------------------------------
1268 * zs_ioctl() and friends
1269 * ------------------------------------------------------------
1272 static int get_serial_info(struct sun_serial * info,
1273 struct serial_struct * retinfo)
1275 struct serial_struct tmp;
1279 memset(&tmp, 0, sizeof(tmp));
1280 tmp.type = info->type;
1281 tmp.line = info->line;
1282 tmp.port = info->port;
1283 tmp.irq = info->irq;
1284 tmp.flags = info->flags;
1285 tmp.baud_base = info->baud_base;
1286 tmp.close_delay = info->close_delay;
1287 tmp.closing_wait = info->closing_wait;
1288 tmp.custom_divisor = info->custom_divisor;
1289 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1294 static int set_serial_info(struct sun_serial * info,
1295 struct serial_struct * new_info)
1297 struct serial_struct new_serial;
1298 struct sun_serial old_info;
1301 if (!new_info || copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1305 if (!capable(CAP_SYS_ADMIN)) {
1306 if ((new_serial.baud_base != info->baud_base) ||
1307 (new_serial.type != info->type) ||
1308 (new_serial.close_delay != info->close_delay) ||
1309 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1310 (info->flags & ~ZILOG_USR_MASK)))
1312 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1313 (new_serial.flags & ZILOG_USR_MASK));
1314 info->custom_divisor = new_serial.custom_divisor;
1315 goto check_and_exit;
1318 if(new_serial.baud_base < 9600)
1321 if (info->count > 1)
1325 * OK, past this point, all the error checking has been done.
1326 * At this point, we start making changes.....
1329 info->baud_base = new_serial.baud_base;
1330 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1331 (new_serial.flags & ZILOG_FLAGS));
1332 info->custom_divisor = new_serial.custom_divisor;
1333 info->type = new_serial.type;
1334 info->close_delay = new_serial.close_delay;
1335 info->closing_wait = new_serial.closing_wait;
1338 retval = startup(info);
1343 * get_lsr_info - get line status register info
1345 * Purpose: Let user call ioctl() to get info when the UART physically
1346 * is emptied. On bus types like RS485, the transmitter must
1347 * release the bus after transmitting. This must be done when
1348 * the transmit shift register is empty, not be done when the
1349 * transmit holding register is empty. This functionality
1350 * allows an RS485 driver to be written in user space.
1352 static int get_lsr_info(struct sun_serial * info, unsigned int *value)
1354 unsigned char status;
1357 status = sbus_readb(&info->zs_channel->control);
1359 ZSLOG(REGCTRL, status, 0);
1361 if (put_user(status, value))
1366 static int get_modem_info(struct sun_serial * info, unsigned int *value)
1368 unsigned char status;
1369 unsigned int result;
1372 status = sbus_readb(&info->zs_channel->control);
1374 ZSLOG(REGCTRL, status, 0);
1376 result = ((info->curregs[5] & RTS) ? TIOCM_RTS : 0)
1377 | ((info->curregs[5] & DTR) ? TIOCM_DTR : 0)
1378 | ((status & DCD) ? TIOCM_CAR : 0)
1379 | ((status & SYNC) ? TIOCM_DSR : 0)
1380 | ((status & CTS) ? TIOCM_CTS : 0);
1381 if (put_user(result, value))
1386 static int set_modem_info(struct sun_serial * info, unsigned int cmd,
1387 unsigned int *value)
1391 if (get_user(arg, value))
1395 if (arg & TIOCM_RTS)
1396 info->curregs[5] |= RTS;
1397 if (arg & TIOCM_DTR)
1398 info->curregs[5] |= DTR;
1401 if (arg & TIOCM_RTS)
1402 info->curregs[5] &= ~RTS;
1403 if (arg & TIOCM_DTR)
1404 info->curregs[5] &= ~DTR;
1407 info->curregs[5] = ((info->curregs[5] & ~(RTS | DTR))
1408 | ((arg & TIOCM_RTS) ? RTS : 0)
1409 | ((arg & TIOCM_DTR) ? DTR : 0));
1415 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1421 * This routine sends a break character out the serial port.
1423 static void send_break( struct sun_serial * info, int duration)
1427 current->state = TASK_INTERRUPTIBLE;
1429 write_zsreg(info->zs_channel, 5, (info->curregs[5] | SND_BRK));
1430 schedule_timeout(duration);
1431 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1435 static int zs_ioctl(struct tty_struct *tty, struct file * file,
1436 unsigned int cmd, unsigned long arg)
1438 struct sun_serial * info = (struct sun_serial *) tty->driver_data;
1441 if (serial_paranoia_check(info, tty->device, "zs_ioctl"))
1444 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1445 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1446 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1447 if (tty->flags & (1 << TTY_IO_ERROR))
1452 case TCSBRK: /* SVID version: non-zero arg --> no break */
1453 retval = tty_check_change(tty);
1456 tty_wait_until_sent(tty, 0);
1458 send_break(info, HZ/4); /* 1/4 second */
1460 case TCSBRKP: /* support for POSIX tcsendbreak() */
1461 retval = tty_check_change(tty);
1464 tty_wait_until_sent(tty, 0);
1465 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1468 if (put_user(C_CLOCAL(tty) ? 1 : 0,
1469 (unsigned long *) arg))
1473 if (get_user(arg, (unsigned long *) arg))
1475 tty->termios->c_cflag =
1476 ((tty->termios->c_cflag & ~CLOCAL) |
1477 (arg ? CLOCAL : 0));
1480 return get_modem_info(info, (unsigned int *) arg);
1484 return set_modem_info(info, cmd, (unsigned int *) arg);
1486 return get_serial_info(info,
1487 (struct serial_struct *) arg);
1489 return set_serial_info(info,
1490 (struct serial_struct *) arg);
1491 case TIOCSERGETLSR: /* Get line status register */
1492 return get_lsr_info(info, (unsigned int *) arg);
1494 case TIOCSERGSTRUCT:
1495 if (copy_to_user((struct sun_serial *) arg,
1496 info, sizeof(struct sun_serial)))
1501 return -ENOIOCTLCMD;
1506 static void zs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1508 struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1510 if (tty->termios->c_cflag == old_termios->c_cflag)
1515 if ((old_termios->c_cflag & CRTSCTS) &&
1516 !(tty->termios->c_cflag & CRTSCTS)) {
1517 tty->hw_stopped = 0;
1523 * ------------------------------------------------------------
1526 * This routine is called when the serial port gets closed. First, we
1527 * wait for the last remaining data to be sent. Then, we unlink its
1528 * ZILOG structure from the interrupt chain if necessary, and we free
1529 * that IRQ if nothing is left in the chain.
1530 * ------------------------------------------------------------
1532 static void zs_close(struct tty_struct *tty, struct file * filp)
1534 struct sun_serial * info = (struct sun_serial *) tty->driver_data;
1535 unsigned long flags;
1537 if (!info || serial_paranoia_check(info, tty->device, "zs_close"))
1540 save_flags(flags); cli();
1542 if (tty_hung_up_p(filp)) {
1543 restore_flags(flags);
1547 #ifdef SERIAL_DEBUG_OPEN
1548 printk("zs_close tty-%d, count = %d\n", info->line, info->count);
1550 if ((tty->count == 1) && (info->count != 1)) {
1552 * Uh, oh. tty->count is 1, which means that the tty
1553 * structure will be freed. Info->count should always
1554 * be one in these conditions. If it's greater than
1555 * one, we've got real problems, since it means the
1556 * serial port won't be shutdown.
1558 printk("zs_close: bad serial port count; tty->count is 1, "
1559 "info->count is %d\n", info->count);
1562 if (--info->count < 0) {
1563 printk("zs_close: bad serial port count for ttys%d: %d\n",
1564 info->line, info->count);
1568 restore_flags(flags);
1571 info->flags |= ZILOG_CLOSING;
1573 * Save the termios structure, since this port may have
1574 * separate termios for callout and dialin.
1576 if (info->flags & ZILOG_NORMAL_ACTIVE)
1577 info->normal_termios = *tty->termios;
1578 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1579 info->callout_termios = *tty->termios;
1581 * Now we wait for the transmit buffer to clear; and we notify
1582 * the line discipline to only process XON/XOFF characters.
1585 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1586 tty_wait_until_sent(tty, info->closing_wait);
1588 * At this point we stop accepting input. To do this, we
1589 * disable the receive line status interrupts, and tell the
1590 * interrupt driver to stop checking the data ready bit in the
1591 * line status register.
1593 /** if (!info->iscons) ... **/
1594 info->curregs[3] &= ~RxENAB;
1595 write_zsreg(info->zs_channel, 3, info->curregs[3]);
1596 info->curregs[1] &= ~(RxINT_MASK);
1597 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1598 ZS_CLEARFIFO(info->zs_channel);
1601 if (tty->driver.flush_buffer)
1602 tty->driver.flush_buffer(tty);
1603 if (tty->ldisc.flush_buffer)
1604 tty->ldisc.flush_buffer(tty);
1608 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1609 if (tty->ldisc.close)
1610 (tty->ldisc.close)(tty);
1611 tty->ldisc = ldiscs[N_TTY];
1612 tty->termios->c_line = N_TTY;
1613 if (tty->ldisc.open)
1614 (tty->ldisc.open)(tty);
1616 if (info->blocked_open) {
1617 if (info->close_delay) {
1618 current->state = TASK_INTERRUPTIBLE;
1619 schedule_timeout(info->close_delay);
1621 wake_up_interruptible(&info->open_wait);
1623 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1625 wake_up_interruptible(&info->close_wait);
1626 #ifdef SERIAL_DEBUG_OPEN
1627 printk("zs_close tty-%d exiting, count = %d\n", info->line, info->count);
1629 restore_flags(flags);
1633 * zs_hangup() --- called by tty_hangup() when a hangup is signaled.
1635 void zs_hangup(struct tty_struct *tty)
1637 struct sun_serial * info = (struct sun_serial *) tty->driver_data;
1639 if (serial_paranoia_check(info, tty->device, "zs_hangup"))
1645 #ifdef SERIAL_DEBUG_OPEN
1646 printk("zs_hangup<%p: tty-%d, count = %d bye\n",
1647 __builtin_return_address(0), info->line, info->count);
1650 zs_flush_buffer(tty);
1654 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1656 wake_up_interruptible(&info->open_wait);
1661 * line_info - returns information about each channel
1664 static inline int line_info(char *buf, struct sun_serial *info)
1666 unsigned char status;
1670 ret = sprintf(buf, "%d: uart:Zilog8530 port:%x irq:%d",
1671 info->line, info->port, info->irq);
1674 status = sbus_readb(&info->zs_channel->control);
1676 ZSLOG(REGCTRL, status, 0);
1681 if (info->curregs[5] & RTS)
1682 strcat(stat_buf, "|RTS");
1684 strcat(stat_buf, "|CTS");
1685 if (info->curregs[5] & DTR)
1686 strcat(stat_buf, "|DTR");
1688 strcat(stat_buf, "|DSR");
1690 strcat(stat_buf, "|CD");
1692 ret += sprintf(buf + ret, " baud:%d %s\n", info->zs_baud, stat_buf + 1);
1698 * zs_read_proc() - called when /proc/tty/driver/serial is read.
1701 int zs_read_proc(char *page, char **start, off_t off, int count,
1702 int *eof, void *data)
1704 char *revision = "$Revision: 1.1.1.1 $";
1709 version = strchr(revision, ' ');
1710 p = strchr(++version, ' ');
1712 len += sprintf(page, "serinfo:1.0 driver:%s\n", version);
1715 for (i = 0; i < NUM_CHANNELS && len < 4000; i++) {
1716 l = line_info(page + len, &zs_soft[i]);
1718 if (len+begin > off+count)
1720 if (len+begin < off) {
1728 if (off >= len+begin)
1730 *start = page + (off-begin);
1731 return ((count < begin+len-off) ? count : begin+len-off);
1735 * ------------------------------------------------------------
1736 * zs_open() and friends
1737 * ------------------------------------------------------------
1739 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1740 struct sun_serial *info)
1742 DECLARE_WAITQUEUE(wait, current);
1743 int retval, do_clocal = 0;
1747 * If the device is in the middle of being closed, then block
1748 * until it's done, and then try again.
1750 if (info->flags & ZILOG_CLOSING) {
1751 interruptible_sleep_on(&info->close_wait);
1752 #ifdef SERIAL_DO_RESTART
1753 if (info->flags & ZILOG_HUP_NOTIFY)
1756 return -ERESTARTSYS;
1763 * If this is a callout device, then just make sure the normal
1764 * device isn't being used.
1766 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1767 if (info->flags & ZILOG_NORMAL_ACTIVE)
1769 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1770 (info->flags & ZILOG_SESSION_LOCKOUT) &&
1771 (info->session != current->session))
1773 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1774 (info->flags & ZILOG_PGRP_LOCKOUT) &&
1775 (info->pgrp != current->pgrp))
1777 info->flags |= ZILOG_CALLOUT_ACTIVE;
1782 * If non-blocking mode is set, or the port is not enabled,
1783 * then make the check up front and then exit.
1785 if ((filp->f_flags & O_NONBLOCK) ||
1786 (tty->flags & (1 << TTY_IO_ERROR))) {
1787 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1789 info->flags |= ZILOG_NORMAL_ACTIVE;
1793 if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1794 if (info->normal_termios.c_cflag & CLOCAL)
1797 if (tty->termios->c_cflag & CLOCAL)
1802 * Block waiting for the carrier detect and the line to become
1803 * free (i.e., not in use by the callout). While we are in
1804 * this loop, info->count is dropped by one, so that
1805 * zs_close() knows when to free things. We restore it upon
1806 * exit, either normal or abnormal.
1809 add_wait_queue(&info->open_wait, &wait);
1810 #ifdef SERIAL_DEBUG_OPEN
1811 printk("block_til_ready before block: ttys%d, count = %d\n",
1812 info->line, info->count);
1815 if(!tty_hung_up_p(filp))
1818 info->blocked_open++;
1821 if (!(info->flags & ZILOG_CALLOUT_ACTIVE))
1824 set_current_state(TASK_INTERRUPTIBLE);
1825 if (tty_hung_up_p(filp) ||
1826 !(info->flags & ZILOG_INITIALIZED)) {
1827 #ifdef SERIAL_DEBUG_OPEN
1828 printk("block_til_ready hup-ed: ttys%d, count = %d\n",
1829 info->line, info->count);
1831 #ifdef SERIAL_DO_RESTART
1832 if (info->flags & ZILOG_HUP_NOTIFY)
1835 retval = -ERESTARTSYS;
1843 r0 = read_zsreg(info->zs_channel, R0);
1845 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1846 !(info->flags & ZILOG_CLOSING) &&
1847 (do_clocal || (DCD & r0)))
1849 if (signal_pending(current)) {
1850 retval = -ERESTARTSYS;
1853 #ifdef SERIAL_DEBUG_OPEN
1854 printk("block_til_ready blocking: ttys%d, count = %d\n",
1855 info->line, info->count);
1859 current->state = TASK_RUNNING;
1860 remove_wait_queue(&info->open_wait, &wait);
1861 if (!tty_hung_up_p(filp))
1863 info->blocked_open--;
1864 #ifdef SERIAL_DEBUG_OPEN
1865 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1866 info->line, info->count);
1870 info->flags |= ZILOG_NORMAL_ACTIVE;
1875 * This routine is called whenever a serial port is opened. It
1876 * enables interrupts for a serial port, linking in its ZILOG structure into
1877 * the IRQ chain. It also performs the serial-specific
1878 * initialization for the tty structure.
1880 int zs_open(struct tty_struct *tty, struct file * filp)
1882 struct sun_serial *info;
1885 line = MINOR(tty->device) - tty->driver.minor_start;
1887 /* The zilog lines for the mouse/keyboard must be
1888 * opened using their respective drivers.
1890 if ((line < 0) || (line >= NUM_CHANNELS))
1892 if((line == KEYBOARD_LINE) || (line == MOUSE_LINE))
1894 info = zs_soft + line;
1895 /* Is the kgdb running over this line? */
1896 if (info->kgdb_channel)
1898 if (serial_paranoia_check(info, tty->device, "zs_open"))
1900 #ifdef SERIAL_DEBUG_OPEN
1901 printk("zs_open %s%d, count = %d\n", tty->driver.name, info->line,
1904 if (info->tty != 0 && info->tty != tty) {
1906 printk("zs_open %s%d, tty overwrite.\n", tty->driver.name, info->line);
1911 unsigned long page = get_free_page(GFP_KERNEL);
1917 tmp_buf = (unsigned char *) page;
1921 tty->driver_data = info;
1925 * Start up serial port
1927 retval = startup(info);
1931 retval = block_til_ready(tty, filp, info);
1933 #ifdef SERIAL_DEBUG_OPEN
1934 printk("zs_open returning after block_til_ready with %d\n",
1940 if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1941 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1942 *tty->termios = info->normal_termios;
1944 *tty->termios = info->callout_termios;
1948 #ifdef CONFIG_SERIAL_CONSOLE
1949 if (zs_console.cflag && zs_console.index == line) {
1950 tty->termios->c_cflag = zs_console.cflag;
1951 zs_console.cflag = 0;
1956 info->session = current->session;
1957 info->pgrp = current->pgrp;
1959 #ifdef SERIAL_DEBUG_OPEN
1960 printk("zs_open ttys%d successful...", info->line);
1965 /* Finally, routines used to initialize the serial driver. */
1967 static void show_serial_version(void)
1969 char *revision = "$Revision: 1.1.1.1 $";
1972 version = strchr(revision, ' ');
1973 p = strchr(++version, ' ');
1975 printk("Sparc Zilog8530 serial driver version %s\n", version);
1979 /* Probe the PROM for the request zs chip number.
1981 * Note: The Sun Voyager shows two addresses and two intr for it's
1982 * Zilogs, what the second does, I don't know. It does work
1983 * with using only the first number of each property. Also
1984 * we have a special version for sun4u.
1987 static struct sun_zslayout * __init get_zs(int chip)
1989 unsigned int vaddr[2] = { 0, 0 };
1990 unsigned long mapped_addr = 0;
1991 int busnode, seen, zsnode, sun4u_ino;
1994 if(chip < 0 || chip >= NUM_SERIAL) {
1995 prom_printf("get_zs bogon zs chip number");
2000 busnode = central_bus->child->prom_node;
2002 busnode = prom_searchsiblings(prom_getchild(prom_root_node), "sbus");
2003 if(busnode == 0 || busnode == -1) {
2004 prom_printf("get_zs: no zs bus to search");
2007 zsnode = prom_getchild(busnode);
2012 zsnode = prom_searchsiblings(zsnode, "zs");
2013 slave = prom_getintdefault(zsnode, "slave", -1);
2014 if((slave == chip) || (seen == chip)) {
2015 int len = prom_getproperty(zsnode, "address",
2016 (void *) vaddr, sizeof(vaddr));
2018 if(len == -1 || central_bus != NULL) {
2019 struct sbus_bus *sbus = NULL;
2020 struct sbus_dev *sdev = NULL;
2022 /* "address" property is not guarenteed,
2023 * everything in I/O is implicitly mapped
2024 * anyways by our clever TLB miss handling
2025 * scheme, so don't fail here. -DaveM
2027 if (central_bus == NULL) {
2028 for_each_sbus(sbus) {
2029 for_each_sbusdev(sdev, sbus) {
2030 if (sdev->prom_node == zsnode)
2036 if (sdev == NULL && central_bus == NULL)
2038 if (central_bus == NULL) {
2040 sbus_ioremap(&sdev->resource[0], 0,
2041 PAGE_SIZE, "Zilog Registers");
2043 struct linux_prom_registers zsregs[1];
2046 err = prom_getproperty(zsnode, "reg",
2050 prom_printf("ZS: Cannot map Zilog regs.\n");
2053 apply_fhc_ranges(central_bus->child, &zsregs[0], 1);
2054 apply_central_ranges(central_bus, &zsregs[0], 1);
2056 ((((u64)zsregs[0].which_io)<<32UL)|
2057 ((u64)zsregs[0].phys_addr));
2059 } else if(len % sizeof(unsigned int)) {
2060 prom_printf("WHOOPS: proplen for %s "
2061 "was %d, need multiple of "
2062 "%d\n", "address", len,
2063 sizeof(unsigned int));
2064 panic("zilog: address property");
2066 zs_nodes[chip] = zsnode;
2067 len = prom_getproperty(zsnode, "interrupts",
2068 (char *) &sun4u_ino,
2069 (sizeof(sun4u_ino)));
2072 unsigned long iclr, imap;
2074 iclr = central_bus->child->fhc_regs.uregs + FHC_UREGS_ICLR;
2075 imap = central_bus->child->fhc_regs.uregs + FHC_UREGS_IMAP;
2076 irq = zilog_irq = build_irq(12, 0, iclr, imap);
2079 sbus_build_irq(sbus_root, sun4u_ino);
2084 zsnode = prom_getsibling(zsnode);
2088 panic("get_zs: whee chip not found");
2089 if(!vaddr[0] && !mapped_addr)
2090 panic("get_zs: whee no serial chip mappable");
2091 if (mapped_addr != 0) {
2092 return (struct sun_zslayout *) mapped_addr;
2094 return (struct sun_zslayout *) prom_virt_to_phys((unsigned long)vaddr[0], 0);
2097 #else /* !(__sparc_v9__) */
2098 static struct sun_zslayout * __init get_zs(int chip)
2100 struct linux_prom_irqs tmp_irq[2];
2101 unsigned int paddr = 0;
2102 unsigned int vaddr[2] = { 0, 0 };
2103 int zsnode, tmpnode, iospace, slave, len;
2104 int cpunode = 0, bbnode = 0;
2109 if(chip < 0 || chip >= NUM_SERIAL)
2110 panic("get_zs bogon zs chip number");
2112 if(sparc_cpu_model == sun4) {
2113 struct resource dummy_resource;
2115 /* Grrr, these have to be hardcoded aieee */
2127 zilog_irq = irq = 12;
2128 dummy_resource.start = paddr;
2129 dummy_resource.end = paddr + 8 - 1;
2130 dummy_resource.flags = IORESOURCE_IO;
2131 vaddr[0] = sbus_ioremap(&dummy_resource, 0,
2134 /* Can use the prom for other machine types */
2135 zsnode = prom_getchild(prom_root_node);
2136 if (sparc_cpu_model == sun4d) {
2142 while (tmpnode && (tmpnode = prom_searchsiblings(tmpnode, "cpu-unit"))) {
2143 bbnode = prom_getchild(tmpnode);
2144 if (bbnode && (bbnode = prom_searchsiblings(bbnode, "bootbus"))) {
2145 if (no == (chip >> 1)) {
2147 zsnode = prom_getchild(bbnode);
2148 chipid = (chip & 1);
2153 tmpnode = prom_getsibling(tmpnode);
2156 panic ("get_zs: couldn't find %dth bootbus\n", chip >> 1);
2158 tmpnode = prom_searchsiblings(zsnode, "obio");
2160 zsnode = prom_getchild(tmpnode);
2163 panic("get_zs no zs serial prom node");
2165 zsnode = prom_searchsiblings(zsnode, "zs");
2166 slave = prom_getintdefault(zsnode, "slave", -1);
2167 if(slave == chipid) {
2168 /* The one we want */
2169 if (sparc_cpu_model != sun4d) {
2170 len = prom_getproperty(zsnode, "address",
2173 if (len % sizeof(unsigned int)) {
2174 prom_printf("WHOOPS: proplen for %s "
2175 "was %d, need multiple of "
2176 "%d\n", "address", len,
2177 sizeof(unsigned int));
2178 panic("zilog: address property");
2181 /* On sun4d don't have address property :( */
2182 struct linux_prom_registers zsreg[4];
2183 struct resource res;
2185 if (prom_getproperty(zsnode, "reg", (char *)zsreg, sizeof(zsreg)) == -1) {
2186 prom_printf ("Cannot map zs regs\n");
2189 prom_apply_generic_ranges(bbnode, cpunode, zsreg, 1);
2190 res.start = zsreg[0].phys_addr;
2191 res.end = res.start + 8 - 1;
2192 res.flags = zsreg[0].which_io | IORESOURCE_IO;
2193 vaddr[0] = sbus_ioremap(&res, 0,
2196 zs_nodes[chip] = zsnode;
2197 len = prom_getproperty(zsnode, "intr",
2200 if (len % sizeof(struct linux_prom_irqs)) {
2202 "WHOOPS: proplen for %s "
2203 "was %d, need multiple of "
2204 "%d\n", "intr", len,
2205 sizeof(struct linux_prom_irqs));
2206 panic("zilog: intr property");
2209 irq = zilog_irq = tmp_irq[0].pri;
2211 if(tmp_irq[0].pri != irq)
2212 panic("zilog: bogon irqs");
2216 zsnode = prom_getsibling(zsnode);
2219 panic("get_zs whee chip not found");
2222 panic("get_zs whee no serial chip mappable");
2224 return (struct sun_zslayout *)(unsigned long) vaddr[0];
2227 /* This is for the auto baud rate detection in the mouse driver. */
2228 void zs_change_mouse_baud(int newbaud)
2230 int channel = MOUSE_LINE;
2233 zs_soft[channel].zs_baud = newbaud;
2234 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2235 (ZS_CLOCK / zs_soft[channel].clk_divisor));
2236 write_zsreg(zs_soft[channel].zs_channel, R12, (brg & 0xff));
2237 write_zsreg(zs_soft[channel].zs_channel, R13, ((brg >> 8) & 0xff));
2240 void __init zs_init_alloc_failure(const char *table_name)
2242 prom_printf("zs_probe: Cannot alloc %s.\n", table_name);
2246 void * __init zs_alloc_bootmem(unsigned long size)
2250 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
2252 memset(ret, 0, size);
2257 void __init zs_alloc_tables(void)
2259 zs_chips = (struct sun_zslayout **)
2260 zs_alloc_bootmem(NUM_SERIAL * sizeof(struct sun_zslayout *));
2261 if (zs_chips == NULL)
2262 zs_init_alloc_failure("zs_chips");
2263 zs_channels = (struct sun_zschannel **)
2264 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct sun_zschannel *));
2265 if (zs_channels == NULL)
2266 zs_init_alloc_failure("zs_channels");
2268 zs_alloc_bootmem(NUM_SERIAL * sizeof(int));
2269 if (zs_nodes == NULL)
2270 zs_init_alloc_failure("zs_nodes");
2271 zs_soft = (struct sun_serial *)
2272 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct sun_serial));
2273 if (zs_soft == NULL)
2274 zs_init_alloc_failure("zs_soft");
2275 zs_ttys = (struct tty_struct *)
2276 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct tty_struct));
2277 if (zs_ttys == NULL)
2278 zs_init_alloc_failure("zs_ttys");
2279 serial_table = (struct tty_struct **)
2280 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct tty_struct *));
2281 if (serial_table == NULL)
2282 zs_init_alloc_failure("serial_table");
2283 serial_termios = (struct termios **)
2284 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct termios *));
2285 if (serial_termios == NULL)
2286 zs_init_alloc_failure("serial_termios");
2287 serial_termios_locked = (struct termios **)
2288 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct termios *));
2289 if (serial_termios_locked == NULL)
2290 zs_init_alloc_failure("serial_termios_locked");
2293 int __init zs_probe(void)
2297 if(sparc_cpu_model == sun4)
2302 node = prom_getchild(prom_root_node);
2303 if (sparc_cpu_model == sun4d) {
2306 while (node && (node = prom_searchsiblings(node, "cpu-unit"))) {
2307 bbnode = prom_getchild(node);
2308 if (bbnode && prom_searchsiblings(bbnode, "bootbus"))
2310 node = prom_getsibling(node);
2315 else if (sparc_cpu_model == sun4u) {
2318 /* Central bus zilogs must be checked for first,
2319 * since Enterprise boxes might have SBUSes as well.
2321 central_node = prom_finddevice("/central");
2322 if(central_node != 0 && central_node != -1)
2323 node = prom_searchsiblings(prom_getchild(central_node), "fhc");
2325 node = prom_searchsiblings(node, "sbus");
2326 if(node != 0 && node != -1)
2327 node = prom_getchild(node);
2328 if(node == 0 || node == -1)
2331 #endif /* __sparc_v9__ */
2333 node = prom_searchsiblings(node, "obio");
2335 node = prom_getchild(node);
2340 node = prom_searchsiblings(node, "zs");
2349 /* Fill in rs_ops struct... */
2350 #ifdef CONFIG_SERIAL_CONSOLE
2351 sunserial_setinitfunc(zs_console_init);
2353 sunserial_setinitfunc(zs_init);
2354 rs_ops.rs_kgdb_hook = zs_kgdb_hook;
2355 rs_ops.rs_change_mouse_baud = zs_change_mouse_baud;
2357 sunkbd_setinitfunc(sun_kbd_init);
2358 kbd_ops.compute_shiftstate = sun_compute_shiftstate;
2359 kbd_ops.setledstate = sun_setledstate;
2360 kbd_ops.getledstate = sun_getledstate;
2361 kbd_ops.setkeycode = sun_setkeycode;
2362 kbd_ops.getkeycode = sun_getkeycode;
2363 #if defined(__sparc_v9__) && defined(CONFIG_PCI)
2364 sunkbd_install_keymaps(sun_key_maps, sun_keymap_count,
2365 sun_func_buf, sun_func_table,
2366 sun_funcbufsize, sun_funcbufleft,
2367 sun_accent_table, sun_accent_table_size);
2372 static inline void zs_prepare(void)
2375 unsigned long flags;
2380 save_and_cli(flags);
2382 /* Set up our interrupt linked list */
2383 zs_chain = &zs_soft[0];
2384 for(channel = 0; channel < NUM_CHANNELS - 1; channel++) {
2385 zs_soft[channel].zs_next = &zs_soft[channel + 1];
2386 zs_soft[channel].line = channel;
2388 zs_soft[channel].zs_next = 0;
2390 /* Initialize Softinfo */
2391 for(chip = 0; chip < NUM_SERIAL; chip++) {
2392 /* If we are doing kgdb over one of the channels on
2393 * chip zero, kgdb_channel will be set to 1 by the
2394 * zs_kgdb_hook() routine below.
2396 if(!zs_chips[chip]) {
2397 zs_chips[chip] = get_zs(chip);
2398 /* Two channels per chip */
2399 zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2400 zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2401 zs_soft[(chip*2)].kgdb_channel = 0;
2402 zs_soft[(chip*2)+1].kgdb_channel = 0;
2405 /* First, set up channel A on this chip. */
2407 zs_soft[channel].zs_channel = zs_channels[channel];
2408 zs_soft[channel].change_needed = 0;
2409 zs_soft[channel].clk_divisor = 16;
2410 zs_soft[channel].cons_keyb = 0;
2411 zs_soft[channel].cons_mouse = 0;
2412 zs_soft[channel].channelA = 1;
2414 /* Now, channel B */
2416 zs_soft[channel].zs_channel = zs_channels[channel];
2417 zs_soft[channel].change_needed = 0;
2418 zs_soft[channel].clk_divisor = 16;
2419 zs_soft[channel].cons_keyb = 0;
2420 zs_soft[channel].cons_mouse = 0;
2421 zs_soft[channel].channelA = 0;
2424 restore_flags(flags);
2427 int __init zs_init(void)
2429 int channel, brg, i;
2430 unsigned long flags;
2431 struct sun_serial *info;
2434 /* Setup base handler, and timer table. */
2435 init_bh(SERIAL_BH, do_serial_bh);
2437 show_serial_version();
2439 /* Initialize the tty_driver structure */
2440 /* SPARC: Not all of this is exactly right for us. */
2442 memset(&serial_driver, 0, sizeof(struct tty_driver));
2443 serial_driver.magic = TTY_DRIVER_MAGIC;
2444 serial_driver.driver_name = "serial";
2445 #ifdef CONFIG_DEVFS_FS
2446 serial_driver.name = "tts/%d";
2448 serial_driver.name = "ttyS";
2450 serial_driver.major = TTY_MAJOR;
2451 serial_driver.minor_start = 64;
2452 serial_driver.num = NUM_CHANNELS;
2453 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2454 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2455 serial_driver.init_termios = tty_std_termios;
2456 serial_driver.init_termios.c_cflag =
2457 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2458 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2459 serial_driver.refcount = &serial_refcount;
2460 serial_driver.table = serial_table;
2461 serial_driver.termios = serial_termios;
2462 serial_driver.termios_locked = serial_termios_locked;
2464 serial_driver.open = zs_open;
2465 serial_driver.close = zs_close;
2466 serial_driver.write = zs_write;
2467 serial_driver.flush_chars = zs_flush_chars;
2468 serial_driver.write_room = zs_write_room;
2469 serial_driver.chars_in_buffer = zs_chars_in_buffer;
2470 serial_driver.flush_buffer = zs_flush_buffer;
2471 serial_driver.ioctl = zs_ioctl;
2472 serial_driver.throttle = zs_throttle;
2473 serial_driver.unthrottle = zs_unthrottle;
2474 serial_driver.set_termios = zs_set_termios;
2475 serial_driver.stop = zs_stop;
2476 serial_driver.start = zs_start;
2477 serial_driver.hangup = zs_hangup;
2479 /* I'm too lazy, someone write versions of this for us. -DaveM */
2480 /* I just did. :-) -AIB 2001-12-23 */
2481 serial_driver.read_proc = zs_read_proc;
2484 * The callout device is just like normal device except for
2485 * major number and the subtype code.
2487 callout_driver = serial_driver;
2488 callout_driver.name = "cua/%d";
2489 callout_driver.major = TTYAUX_MAJOR;
2490 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2491 callout_driver.read_proc = 0;
2492 callout_driver.proc_entry = 0;
2494 if (tty_register_driver(&serial_driver))
2495 panic("Couldn't register serial driver\n");
2496 if (tty_register_driver(&callout_driver))
2497 panic("Couldn't register callout driver\n");
2499 save_flags(flags); cli();
2501 /* Initialize Softinfo */
2504 /* Grab IRQ line before poking the chips so we do
2505 * not lose any interrupts.
2507 if (request_irq(zilog_irq, zs_interrupt, SA_SHIRQ,
2508 "Zilog8530", zs_chain)) {
2509 prom_printf("Unable to attach zs intr\n");
2513 /* Initialize Hardware */
2514 for(channel = 0; channel < NUM_CHANNELS; channel++) {
2515 /* Hardware reset each chip */
2516 if (!(channel & 1)) {
2517 write_zsreg(zs_soft[channel].zs_channel, R9, FHWRES);
2519 dummy = read_zsreg(zs_soft[channel].zs_channel, R0);
2522 if(channel == KEYBOARD_LINE) {
2523 zs_soft[channel].cons_keyb = 1;
2524 zs_soft[channel].parity_mask = 0xff;
2525 zs_kbdchan = zs_soft[channel].zs_channel;
2527 write_zsreg(zs_soft[channel].zs_channel, R4,
2528 (PAR_EVEN | X16CLK | SB1));
2529 write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2530 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2531 write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2532 write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2533 write_zsreg(zs_soft[channel].zs_channel, R11,
2535 zs_soft[channel].zs_baud = 1200;
2536 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2537 ZS_CLOCK/zs_soft[channel].clk_divisor);
2538 write_zsreg(zs_soft[channel].zs_channel, R12,
2540 write_zsreg(zs_soft[channel].zs_channel, R13,
2541 ((brg >> 8) & 0xff));
2542 write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2544 /* Enable Rx/Tx, IRQs, and inform kbd driver */
2545 write_zsreg(zs_soft[channel].zs_channel, R14,
2547 write_zsreg(zs_soft[channel].zs_channel, R3,
2549 write_zsreg(zs_soft[channel].zs_channel, R5,
2550 (Tx8 | TxENAB | DTR | RTS));
2552 write_zsreg(zs_soft[channel].zs_channel, R15,
2553 (DCDIE | CTSIE | TxUIE | BRKIE));
2554 write_zsreg(zs_soft[channel].zs_channel, R0,
2556 write_zsreg(zs_soft[channel].zs_channel, R0,
2559 write_zsreg(zs_soft[channel].zs_channel, R1,
2560 (EXT_INT_ENAB | INT_ALL_Rx));
2561 write_zsreg(zs_soft[channel].zs_channel, R9,
2563 ZS_CLEARERR(zs_soft[channel].zs_channel);
2564 ZS_CLEARFIFO(zs_soft[channel].zs_channel);
2565 } else if(channel == MOUSE_LINE) {
2566 zs_soft[channel].cons_mouse = 1;
2567 zs_soft[channel].parity_mask = 0xff;
2568 zs_mousechan = zs_soft[channel].zs_channel;
2570 write_zsreg(zs_soft[channel].zs_channel, R4,
2571 (PAR_EVEN | X16CLK | SB1));
2572 write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2573 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2574 write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2575 write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2576 write_zsreg(zs_soft[channel].zs_channel, R11,
2579 zs_soft[channel].zs_baud = 4800;
2580 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2581 ZS_CLOCK/zs_soft[channel].clk_divisor);
2582 write_zsreg(zs_soft[channel].zs_channel, R12,
2584 write_zsreg(zs_soft[channel].zs_channel, R13,
2585 ((brg >> 8) & 0xff));
2586 write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2588 /* Enable Rx, IRQs, and inform mouse driver */
2589 write_zsreg(zs_soft[channel].zs_channel, R14,
2591 write_zsreg(zs_soft[channel].zs_channel, R3,
2593 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2595 write_zsreg(zs_soft[channel].zs_channel, R15,
2596 (DCDIE | CTSIE | TxUIE | BRKIE));
2597 write_zsreg(zs_soft[channel].zs_channel, R0,
2599 write_zsreg(zs_soft[channel].zs_channel, R0,
2602 write_zsreg(zs_soft[channel].zs_channel, R1,
2603 (EXT_INT_ENAB | INT_ALL_Rx));
2604 write_zsreg(zs_soft[channel].zs_channel, R9,
2608 } else if (zs_soft[channel].is_cons) {
2609 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2610 ZS_CLOCK/zs_soft[channel].clk_divisor);
2611 zscons_regs[12] = brg & 0xff;
2612 zscons_regs[13] = (brg >> 8) & 0xff;
2614 memcpy(zs_soft[channel].curregs, zscons_regs, sizeof(zscons_regs));
2615 load_zsregs(&zs_soft[channel], zscons_regs);
2617 ZS_CLEARERR(zs_soft[channel].zs_channel);
2618 ZS_CLEARFIFO(zs_soft[channel].zs_channel);
2619 } else if (zs_soft[channel].kgdb_channel) {
2620 /* If this is the kgdb line, enable interrupts because
2621 * we now want to receive the 'control-c' character
2622 * from the client attached to us asynchronously.
2624 zs_soft[channel].parity_mask = 0xff;
2625 kgdb_chaninit(&zs_soft[channel], 1,
2626 zs_soft[channel].zs_baud);
2628 zs_soft[channel].parity_mask = 0xff;
2629 write_zsreg(zs_soft[channel].zs_channel, R4,
2630 (PAR_EVEN | X16CLK | SB1));
2631 write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2632 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2633 write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2634 write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2635 write_zsreg(zs_soft[channel].zs_channel, R11,
2637 zs_soft[channel].zs_baud = 9600;
2638 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2639 ZS_CLOCK/zs_soft[channel].clk_divisor);
2640 write_zsreg(zs_soft[channel].zs_channel, R12,
2642 write_zsreg(zs_soft[channel].zs_channel, R13,
2643 ((brg >> 8) & 0xff));
2644 write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2645 write_zsreg(zs_soft[channel].zs_channel, R14,
2647 write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2648 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2649 write_zsreg(zs_soft[channel].zs_channel, R15, DCDIE);
2650 write_zsreg(zs_soft[channel].zs_channel, R9, NV | MIE);
2651 write_zsreg(zs_soft[channel].zs_channel, R0,
2653 write_zsreg(zs_soft[channel].zs_channel, R0,
2658 for (info = zs_chain, i=0; info; info = info->zs_next, i++) {
2659 info->magic = SERIAL_MAGIC;
2660 info->port = (long) info->zs_channel;
2663 info->irq = zilog_irq;
2664 info->custom_divisor = 16;
2665 info->close_delay = 50;
2666 info->closing_wait = 3000;
2670 info->blocked_open = 0;
2671 info->tqueue.routine = do_softint;
2672 info->tqueue.data = info;
2673 info->tqueue_hangup.routine = do_serial_hangup;
2674 info->tqueue_hangup.data = info;
2675 info->callout_termios = callout_driver.init_termios;
2676 info->normal_termios = serial_driver.init_termios;
2677 init_waitqueue_head(&info->open_wait);
2678 init_waitqueue_head(&info->close_wait);
2679 printk("tty%02d at 0x%04x (irq = %s)", info->line,
2680 info->port, __irq_itoa(info->irq));
2681 printk(" is a Zilog8530\n");
2684 restore_flags(flags);
2686 keyboard_zsinit(kbd_put_char);
2690 /* This is called at boot time to prime the kgdb serial debugging
2691 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2692 * for /dev/ttyb which is determined in setup_arch() from the
2693 * boot command line flags.
2695 static void __init zs_kgdb_hook(int tty_num)
2699 if(!zs_chips[chip]) {
2700 zs_chips[chip] = get_zs(chip);
2701 /* Two channels per chip */
2702 zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2703 zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2705 zs_soft[tty_num].zs_channel = zs_channels[tty_num];
2706 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2707 zs_soft[tty_num].change_needed = 0;
2708 zs_soft[tty_num].clk_divisor = 16;
2709 zs_soft[tty_num].zs_baud = 9600;
2710 zs_soft[tty_num].kgdb_channel = 1; /* This runs kgdb */
2711 zs_soft[tty_num ^ 1].kgdb_channel = 0; /* This does not */
2712 /* Turn on transmitter/receiver at 8-bits/char */
2713 kgdb_chaninit(&zs_soft[tty_num], 0, 9600);
2714 ZS_CLEARERR(zs_kgdbchan);
2715 ZS_CLEARFIFO(zs_kgdbchan);
2718 #ifdef CONFIG_SERIAL_CONSOLE
2720 /* This is for console output over ttya/ttyb */
2722 zs_console_putchar(struct sun_serial *info, char ch)
2724 int loops = ZS_PUT_CHAR_MAX_DELAY;
2725 unsigned long flags;
2727 if(!info->zs_channel)
2730 save_flags(flags); cli();
2731 zs_put_char(info->zs_channel, ch);
2732 while (!(read_zsreg(info->zs_channel, R1) & ALL_SNT) && --loops)
2734 restore_flags(flags);
2737 #ifdef SERIAL_CONSOLE_FAIR_OUTPUT
2739 * Fair output driver allows a process to speak.
2741 static void zs_fair_output(struct sun_serial *info)
2743 unsigned long flags;
2744 int left; /* Output no more than that */
2749 if (info->xmit_buf == NULL)
2752 save_flags(flags); cli();
2753 left = info->xmit_cnt;
2755 c = info->xmit_buf[info->xmit_tail];
2756 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
2758 restore_flags(flags);
2760 zs_console_putchar(info, c);
2763 left = MIN(info->xmit_cnt, left-1);
2766 /* Last character is being transmitted now (hopefully). */
2767 sbus_writeb(RES_Tx_P, &info->zs_channel->control);
2769 ZSLOG(REGCTRL, RES_Tx_P, 1);
2771 restore_flags(flags);
2777 * zs_console_write is registered for printk.
2780 zs_console_write(struct console *con, const char *s, unsigned count)
2782 struct sun_serial *info;
2785 info = zs_soft + con->index;
2787 for (i = 0; i < count; i++, s++) {
2789 zs_console_putchar(info, '\r');
2790 zs_console_putchar(info, *s);
2792 #ifdef SERIAL_CONSOLE_FAIR_OUTPUT
2793 /* Comment this if you want to have a strict interrupt-driven output */
2794 zs_fair_output(info);
2798 static kdev_t zs_console_device(struct console *con)
2800 return MKDEV(TTY_MAJOR, 64 + con->index);
2803 static int __init zs_console_setup(struct console *con, char *options)
2805 static struct tty_struct c_tty;
2806 static struct termios c_termios;
2807 struct sun_serial *info;
2810 info = zs_soft + con->index;
2813 printk("Console: ttyS%d (Zilog8530)\n", info->line);
2815 sunserial_console_termios(con);
2816 memset(&c_tty, 0, sizeof(c_tty));
2817 memset(&c_termios, 0, sizeof(c_termios));
2818 c_tty.termios = &c_termios;
2819 c_termios.c_cflag = con->cflag;
2820 baud = tty_get_baud_rate(&c_tty);
2822 info->zs_baud = baud;
2824 switch (con->cflag & CSIZE) {
2826 zscons_regs[3] = Rx5 | RxENAB;
2827 zscons_regs[5] = Tx5 | TxENAB;
2828 info->parity_mask = 0x1f;
2831 zscons_regs[3] = Rx6 | RxENAB;
2832 zscons_regs[5] = Tx6 | TxENAB;
2833 info->parity_mask = 0x3f;
2836 zscons_regs[3] = Rx7 | RxENAB;
2837 zscons_regs[5] = Tx7 | TxENAB;
2838 info->parity_mask = 0x7f;
2842 zscons_regs[3] = Rx8 | RxENAB;
2843 zscons_regs[5] = Tx8 | TxENAB;
2844 info->parity_mask = 0xff;
2847 zscons_regs[5] |= DTR;
2849 if (con->cflag & PARENB)
2850 zscons_regs[4] |= PAR_ENAB;
2851 if (!(con->cflag & PARODD))
2852 zscons_regs[4] |= PAR_EVEN;
2854 if (con->cflag & CSTOPB)
2855 zscons_regs[4] |= SB2;
2857 zscons_regs[4] |= SB1;
2859 brg = BPS_TO_BRG(baud, ZS_CLOCK / info->clk_divisor);
2860 zscons_regs[12] = brg & 0xff;
2861 zscons_regs[13] = (brg >> 8) & 0xff;
2863 memcpy(info->curregs, zscons_regs, sizeof(zscons_regs));
2864 load_zsregs(info, zscons_regs);
2866 ZS_CLEARERR(info->zs_channel);
2867 ZS_CLEARFIFO(info->zs_channel);
2871 static struct console zs_console = {
2873 write: zs_console_write,
2874 device: zs_console_device,
2875 setup: zs_console_setup,
2876 flags: CON_PRINTBUFFER,
2880 static int __init zs_console_init(void)
2882 extern int con_is_present(void);
2884 if (con_is_present())
2887 zs_console.index = serial_console - 1;
2888 register_console(&zs_console);
2892 #endif /* CONFIG_SERIAL_CONSOLE */