Import 2.3.18pre1
[davej-history.git] / drivers / sbus / char / zs.c
blob8ed6e2737e538ec93893c0f01b08f3450e978002
1 /* $Id: zs.c,v 1.45 1999/09/01 08:09:35 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@metabyte.com>.
7 */
9 #include <linux/errno.h>
10 #include <linux/signal.h>
11 #include <linux/sched.h>
12 #include <linux/timer.h>
13 #include <linux/interrupt.h>
14 #include <linux/tty.h>
15 #include <linux/tty_flip.h>
16 #include <linux/config.h>
17 #include <linux/major.h>
18 #include <linux/string.h>
19 #include <linux/fcntl.h>
20 #include <linux/mm.h>
21 #include <linux/kernel.h>
22 #include <linux/keyboard.h>
23 #include <linux/console.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/sysrq.h>
28 #include <asm/io.h>
29 #include <asm/irq.h>
30 #include <asm/oplib.h>
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
33 #include <asm/bitops.h>
34 #include <asm/kdebug.h>
36 #include <asm/sbus.h>
37 #ifdef __sparc_v9__
38 #include <asm/fhc.h>
39 #endif
40 #ifdef CONFIG_PCI
41 #include <linux/pci.h>
42 #endif
44 #include "sunserial.h"
45 #include "zs.h"
46 #include "sunkbd.h"
47 #include "sunmouse.h"
49 static int num_serial = 2; /* sun4/sun4c/sun4m - Two chips on board. */
50 #define NUM_SERIAL num_serial
51 #define NUM_CHANNELS (NUM_SERIAL * 2)
53 #define KEYBOARD_LINE 0x2
54 #define MOUSE_LINE 0x3
56 /* On 32-bit sparcs we need to delay after register accesses
57 * to accomodate sun4 systems, but we do not need to flush writes.
58 * On 64-bit sparc we only need to flush single writes to ensure
59 * completion.
61 #ifndef __sparc_v9__
62 #define ZSDELAY() udelay(5)
63 #define ZSDELAY_LONG() udelay(20)
64 #define ZS_WSYNC(channel) do { } while(0)
65 #else
66 #define ZSDELAY()
67 #define ZSDELAY_LONG()
68 #define ZS_WSYNC(channel) \
69 ((void) *((volatile unsigned char *)(&((channel)->control))))
70 #endif
72 struct sun_zslayout **zs_chips;
73 struct sun_zschannel **zs_channels;
74 struct sun_zschannel *zs_mousechan;
75 struct sun_zschannel *zs_kbdchan;
76 struct sun_zschannel *zs_kgdbchan;
77 int *zs_nodes;
79 struct sun_serial *zs_soft;
80 struct sun_serial *zs_chain; /* IRQ servicing chain */
81 int zilog_irq;
83 struct tty_struct *zs_ttys;
85 /* Console hooks... */
86 #ifdef CONFIG_SERIAL_CONSOLE
87 static struct console zs_console;
88 static int zs_console_init(void);
91 * Define this to get the zs_fair_output() functionality.
93 #undef SERIAL_CONSOLE_FAIR_OUTPUT
94 #endif /* CONFIG_SERIAL_CONSOLE */
96 static unsigned char kgdb_regs[16] = {
97 0, 0, 0, /* write 0, 1, 2 */
98 (Rx8 | RxENAB), /* write 3 */
99 (X16CLK | SB1 | PAR_EVEN), /* write 4 */
100 (DTR | Tx8 | TxENAB), /* write 5 */
101 0, 0, 0, /* write 6, 7, 8 */
102 (NV), /* write 9 */
103 (NRZ), /* write 10 */
104 (TCBR | RCBR), /* write 11 */
105 0, 0, /* BRG time constant, write 12 + 13 */
106 (BRSRC | BRENAB), /* write 14 */
107 (DCDIE) /* write 15 */
110 static unsigned char zscons_regs[16] = {
111 0, /* write 0 */
112 (EXT_INT_ENAB | INT_ALL_Rx), /* write 1 */
113 0, /* write 2 */
114 (Rx8 | RxENAB), /* write 3 */
115 (X16CLK), /* write 4 */
116 (DTR | Tx8 | TxENAB), /* write 5 */
117 0, 0, 0, /* write 6, 7, 8 */
118 (NV | MIE), /* write 9 */
119 (NRZ), /* write 10 */
120 (TCBR | RCBR), /* write 11 */
121 0, 0, /* BRG time constant, write 12 + 13 */
122 (BRSRC | BRENAB), /* write 14 */
123 (DCDIE | CTSIE | TxUIE | BRKIE) /* write 15 */
126 #define ZS_CLOCK 4915200 /* Zilog input clock rate */
128 DECLARE_TASK_QUEUE(tq_serial);
130 static struct tty_driver serial_driver, callout_driver;
131 static int serial_refcount;
133 /* serial subtype definitions */
134 #define SERIAL_TYPE_NORMAL 1
135 #define SERIAL_TYPE_CALLOUT 2
137 /* number of characters left in xmit buffer before we ask for more */
138 #define WAKEUP_CHARS 256
140 #define SERIAL_DO_RESTART
142 /* Debugging... DEBUG_INTR is bad to use when one of the zs
143 * lines is your console ;(
145 #undef SERIAL_DEBUG_INTR
146 #undef SERIAL_DEBUG_OPEN
147 #undef SERIAL_DEBUG_FLOW
149 #define RS_STROBE_TIME 10
150 #define RS_ISR_PASS_LIMIT 256
152 #define _INLINE_ inline
154 int zs_init(void);
155 static void zs_kgdb_hook(int);
157 static void change_speed(struct sun_serial *info);
159 static struct tty_struct **serial_table;
160 static struct termios **serial_termios;
161 static struct termios **serial_termios_locked;
163 #ifndef MIN
164 #define MIN(a,b) ((a) < (b) ? (a) : (b))
165 #endif
168 * tmp_buf is used as a temporary buffer by serial_write. We need to
169 * lock it in case the memcpy_fromfs blocks while swapping in a page,
170 * and some other program tries to do a serial write at the same time.
171 * Since the lock will only come under contention when the system is
172 * swapping and available memory is low, it makes sense to share one
173 * buffer across all the serial ports, since it significantly saves
174 * memory if large numbers of serial ports are open.
176 static unsigned char tmp_buf[4096]; /* This is cheating */
177 static DECLARE_MUTEX(tmp_buf_sem);
179 static inline int serial_paranoia_check(struct sun_serial *info,
180 dev_t device, const char *routine)
182 #ifdef SERIAL_PARANOIA_CHECK
183 static const char *badmagic =
184 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
185 static const char *badinfo =
186 "Warning: null sun_serial for (%d, %d) in %s\n";
188 if (!info) {
189 printk(badinfo, MAJOR(device), MINOR(device), routine);
190 return 1;
192 if (info->magic != SERIAL_MAGIC) {
193 printk(badmagic, MAJOR(device), MINOR(device), routine);
194 return 1;
196 #endif
197 return 0;
201 * This is used to figure out the divisor speeds and the timeouts
203 static int baud_table[] = {
204 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
205 9600, 19200, 38400, 76800, 0 };
208 * Reading and writing Zilog8530 registers. The delays are to make this
209 * driver work on the Sun4 which needs a settling delay after each chip
210 * register access, other machines handle this in hardware via auxiliary
211 * flip-flops which implement the settle time we do in software.
213 static inline unsigned char read_zsreg(struct sun_zschannel *channel,
214 unsigned char reg)
216 unsigned char retval;
218 channel->control = reg;
219 ZSDELAY();
220 retval = channel->control;
221 ZSDELAY();
222 return retval;
225 static inline void write_zsreg(struct sun_zschannel *channel,
226 unsigned char reg, unsigned char value)
228 channel->control = reg;
229 ZSDELAY();
230 channel->control = value;
231 ZSDELAY();
234 static inline void load_zsregs(struct sun_serial *info, unsigned char *regs)
236 unsigned long flags;
237 struct sun_zschannel *channel = info->zs_channel;
238 unsigned char stat;
239 int i;
241 for (i = 0; i < 1000; i++) {
242 stat = read_zsreg(channel, R1);
243 if (stat & ALL_SNT)
244 break;
245 udelay(100);
247 write_zsreg(channel, R3, 0);
248 ZS_CLEARSTAT(channel);
249 ZS_CLEARERR(channel);
250 ZS_CLEARFIFO(channel);
252 /* Load 'em up */
253 save_flags(flags); cli();
254 if (info->channelA)
255 write_zsreg(channel, R9, CHRA);
256 else
257 write_zsreg(channel, R9, CHRB);
258 ZSDELAY_LONG();
259 write_zsreg(channel, R4, regs[R4]);
260 write_zsreg(channel, R3, regs[R3] & ~RxENAB);
261 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
262 write_zsreg(channel, R9, regs[R9] & ~MIE);
263 write_zsreg(channel, R10, regs[R10]);
264 write_zsreg(channel, R11, regs[R11]);
265 write_zsreg(channel, R12, regs[R12]);
266 write_zsreg(channel, R13, regs[R13]);
267 write_zsreg(channel, R14, regs[R14] & ~BRENAB);
268 write_zsreg(channel, R14, regs[R14]);
269 write_zsreg(channel, R14, (regs[R14] & ~SNRZI) | BRENAB);
270 write_zsreg(channel, R3, regs[R3]);
271 write_zsreg(channel, R5, regs[R5]);
272 write_zsreg(channel, R15, regs[R15]);
273 write_zsreg(channel, R0, RES_EXT_INT);
274 write_zsreg(channel, R0, ERR_RES);
275 write_zsreg(channel, R1, regs[R1]);
276 write_zsreg(channel, R9, regs[R9]);
277 restore_flags(flags);
280 #define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
282 static inline void zs_put_char(struct sun_zschannel *channel, char ch)
284 int loops = ZS_PUT_CHAR_MAX_DELAY;
286 /* Do not change this to use ZSDELAY as this is
287 * a timed polling loop and on sparc64 ZSDELAY
288 * is a nop. -DaveM
290 while((channel->control & Tx_BUF_EMP) == 0 && --loops)
291 udelay(5);
293 channel->data = ch;
294 ZSDELAY();
295 ZS_WSYNC(channel);
298 /* Sets or clears DTR/RTS on the requested line */
299 static inline void zs_rtsdtr(struct sun_serial *ss, int set)
301 unsigned long flags;
303 save_flags(flags); cli();
304 if(set) {
305 ss->curregs[5] |= (RTS | DTR);
306 write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
307 } else {
308 ss->curregs[5] &= ~(RTS | DTR);
309 write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
311 restore_flags(flags);
312 return;
315 static inline void kgdb_chaninit(struct sun_serial *ss, int intson, int bps)
317 int brg;
319 if(intson) {
320 kgdb_regs[R1] = INT_ALL_Rx;
321 kgdb_regs[R9] |= MIE;
322 } else {
323 kgdb_regs[R1] = 0;
324 kgdb_regs[R9] &= ~MIE;
326 brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
327 kgdb_regs[R12] = (brg & 255);
328 kgdb_regs[R13] = ((brg >> 8) & 255);
329 load_zsregs(ss, kgdb_regs);
333 * ------------------------------------------------------------
334 * zs_stop() and zs_start()
336 * This routines are called before setting or resetting tty->stopped.
337 * They enable or disable transmitter interrupts, as necessary.
338 * ------------------------------------------------------------
340 static void zs_stop(struct tty_struct *tty)
342 struct sun_serial *info = (struct sun_serial *)tty->driver_data;
343 unsigned long flags;
345 if (serial_paranoia_check(info, tty->device, "zs_stop"))
346 return;
348 save_flags(flags); cli();
349 if (info->curregs[5] & TxENAB) {
350 info->curregs[5] &= ~TxENAB;
351 write_zsreg(info->zs_channel, 5, info->curregs[5]);
353 restore_flags(flags);
356 static void zs_start(struct tty_struct *tty)
358 struct sun_serial *info = (struct sun_serial *)tty->driver_data;
359 unsigned long flags;
361 if (serial_paranoia_check(info, tty->device, "zs_start"))
362 return;
364 save_flags(flags); cli();
365 if (info->xmit_cnt && info->xmit_buf && !(info->curregs[5] & TxENAB)) {
366 info->curregs[5] |= TxENAB;
367 write_zsreg(info->zs_channel, 5, info->curregs[5]);
369 restore_flags(flags);
372 /* Drop into either the boot monitor or kadb upon receiving a break
373 * from keyboard/console input.
375 void batten_down_hatches(void)
377 /* If we are doing kadb, we call the debugger
378 * else we just drop into the boot monitor.
379 * Note that we must flush the user windows
380 * first before giving up control.
382 printk("\n");
383 flush_user_windows();
384 #ifndef __sparc_v9__
385 if((((unsigned long)linux_dbvec)>=DEBUG_FIRSTVADDR) &&
386 (((unsigned long)linux_dbvec)<=DEBUG_LASTVADDR))
387 sp_enter_debugger();
388 else
389 #endif
390 prom_cmdline();
392 /* XXX We want to notify the keyboard driver that all
393 * XXX keys are in the up state or else weird things
394 * XXX happen...
397 return;
402 * ----------------------------------------------------------------------
404 * Here starts the interrupt handling routines. All of the following
405 * subroutines are declared as inline and are folded into
406 * zs_interrupt(). They were separated out for readability's sake.
408 * Note: zs_interrupt() is a "fast" interrupt, which means that it
409 * runs with interrupts turned off. People who may want to modify
410 * zs_interrupt() should try to keep the interrupt handler as fast as
411 * possible. After you are done making modifications, it is not a bad
412 * idea to do:
414 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
416 * and look at the resulting assemble code in serial.s.
418 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
419 * -----------------------------------------------------------------------
423 * This routine is used by the interrupt handler to schedule
424 * processing in the software interrupt portion of the driver.
426 static _INLINE_ void zs_sched_event(struct sun_serial *info,
427 int event)
429 info->event |= 1 << event;
430 queue_task(&info->tqueue, &tq_serial);
431 mark_bh(SERIAL_BH);
434 #ifndef __sparc_v9__
435 extern void breakpoint(void); /* For the KGDB frame character */
436 #endif
438 static _INLINE_ void receive_chars(struct sun_serial *info, struct pt_regs *regs)
440 struct tty_struct *tty = info->tty;
441 unsigned char ch, stat;
443 do {
444 ch = (info->zs_channel->data) & info->parity_mask;
445 ZSDELAY();
447 /* If this is the console keyboard, we need to handle
448 * L1-A's here.
450 if(info->cons_keyb) {
451 if(ch == SUNKBD_RESET) {
452 l1a_state.kbd_id = 1;
453 l1a_state.l1_down = 0;
454 } else if(l1a_state.kbd_id) {
455 l1a_state.kbd_id = 0;
456 } else if(ch == SUNKBD_L1) {
457 l1a_state.l1_down = 1;
458 } else if(ch == (SUNKBD_L1|SUNKBD_UP)) {
459 l1a_state.l1_down = 0;
460 } else if(ch == SUNKBD_A && l1a_state.l1_down) {
461 /* whee... */
462 batten_down_hatches();
463 /* Continue execution... */
464 l1a_state.l1_down = 0;
465 l1a_state.kbd_id = 0;
466 return;
468 sunkbd_inchar(ch, regs);
469 return;
471 if(info->cons_mouse) {
472 sun_mouse_inbyte(ch);
473 return;
475 if(info->is_cons) {
476 if(ch==0) {
477 /* whee, break received */
478 batten_down_hatches();
479 /* Continue execution... */
480 return;
482 /* It is a 'keyboard interrupt' ;-) */
483 wake_up(&keypress_wait);
485 #ifndef __sparc_v9__
486 /* Look for kgdb 'stop' character, consult the gdb
487 * documentation for remote target debugging and
488 * arch/sparc/kernel/sparc-stub.c to see how all this works.
490 if((info->kgdb_channel) && (ch =='\003')) {
491 breakpoint();
492 return;
494 #endif
495 if(!tty)
496 return;
498 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
499 break;
501 tty->flip.count++;
502 *tty->flip.flag_buf_ptr++ = 0;
503 *tty->flip.char_buf_ptr++ = ch;
505 /* Check if we have another character... */
506 stat = info->zs_channel->control;
507 ZSDELAY();
508 if (!(stat & Rx_CH_AV))
509 break;
511 /* ... and see if it is clean. */
512 stat = read_zsreg(info->zs_channel, R1);
513 } while (!(stat & (PAR_ERR | Rx_OVR | CRC_ERR)));
515 queue_task(&tty->flip.tqueue, &tq_timer);
518 static _INLINE_ void transmit_chars(struct sun_serial *info)
520 struct tty_struct *tty = info->tty;
522 if (info->x_char) {
523 /* Send next char */
524 zs_put_char(info->zs_channel, info->x_char);
525 info->x_char = 0;
526 return;
529 if((info->xmit_cnt <= 0) || (tty != 0 && tty->stopped)) {
530 /* That's peculiar... */
531 info->zs_channel->control = RES_Tx_P;
532 ZSDELAY();
533 ZS_WSYNC(info->zs_channel);
534 return;
537 /* Send char */
538 zs_put_char(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
539 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
540 info->xmit_cnt--;
542 if (info->xmit_cnt < WAKEUP_CHARS)
543 zs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
545 if(info->xmit_cnt <= 0) {
546 info->zs_channel->control = RES_Tx_P;
547 ZSDELAY();
548 ZS_WSYNC(info->zs_channel);
552 static _INLINE_ void status_handle(struct sun_serial *info)
554 unsigned char status;
556 /* Get status from Read Register 0 */
557 status = info->zs_channel->control;
558 ZSDELAY();
559 /* Clear status condition... */
560 info->zs_channel->control = RES_EXT_INT;
561 ZSDELAY();
562 ZS_WSYNC(info->zs_channel);
564 #if 0
565 if(status & DCD) {
566 if((info->tty->termios->c_cflag & CRTSCTS) &&
567 ((info->curregs[3] & AUTO_ENAB)==0)) {
568 info->curregs[3] |= AUTO_ENAB;
569 write_zsreg(info->zs_channel, 3, info->curregs[3]);
571 } else {
572 if((info->curregs[3] & AUTO_ENAB)) {
573 info->curregs[3] &= ~AUTO_ENAB;
574 write_zsreg(info->zs_channel, 3, info->curregs[3]);
577 #endif
578 /* Whee, if this is console input and this is a
579 * 'break asserted' status change interrupt, call
580 * the boot prom.
582 if((status & BRK_ABRT) && info->break_abort)
583 batten_down_hatches();
585 /* XXX Whee, put in a buffer somewhere, the status information
586 * XXX whee whee whee... Where does the information go...
588 return;
591 static _INLINE_ void special_receive(struct sun_serial *info)
593 struct tty_struct *tty = info->tty;
594 unsigned char ch, stat;
596 stat = read_zsreg(info->zs_channel, R1);
597 if (stat & (PAR_ERR | Rx_OVR | CRC_ERR)) {
598 ch = info->zs_channel->data;
599 ZSDELAY();
602 if (!tty)
603 goto clear;
605 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
606 goto done;
608 tty->flip.count++;
609 if(stat & PAR_ERR)
610 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
611 else if(stat & Rx_OVR)
612 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
613 else if(stat & CRC_ERR)
614 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
616 done:
617 queue_task(&tty->flip.tqueue, &tq_timer);
618 clear:
619 info->zs_channel->control = ERR_RES;
620 ZSDELAY();
621 ZS_WSYNC(info->zs_channel);
626 * This is the serial driver's generic interrupt routine
628 void zs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
630 struct sun_serial *info;
631 unsigned char zs_intreg;
632 int i;
634 info = (struct sun_serial *)dev_id;
635 for (i = 0; i < NUM_SERIAL; i++) {
636 zs_intreg = read_zsreg(info->zs_next->zs_channel, 2);
637 zs_intreg &= STATUS_MASK;
639 /* NOTE: The read register 2, which holds the irq status,
640 * does so for both channels on each chip. Although
641 * the status value itself must be read from the B
642 * channel and is only valid when read from channel B.
643 * When read from channel A, read register 2 contains
644 * the value written to write register 2.
647 /* Channel A -- /dev/ttya or /dev/kbd, could be the console */
648 if (zs_intreg == CHA_Rx_AVAIL) {
649 receive_chars(info, regs);
650 return;
652 if(zs_intreg == CHA_Tx_EMPTY) {
653 transmit_chars(info);
654 return;
656 if (zs_intreg == CHA_EXT_STAT) {
657 status_handle(info);
658 return;
660 if (zs_intreg == CHA_SPECIAL) {
661 special_receive(info);
662 return;
665 /* Channel B -- /dev/ttyb or /dev/mouse, could be the console */
666 if(zs_intreg == CHB_Rx_AVAIL) {
667 receive_chars(info->zs_next, regs);
668 return;
670 if(zs_intreg == CHB_Tx_EMPTY) {
671 transmit_chars(info->zs_next);
672 return;
674 if (zs_intreg == CHB_EXT_STAT) {
675 status_handle(info->zs_next);
676 return;
679 /* NOTE: The default value for the IRQ status in read register
680 * 2 in channel B is CHB_SPECIAL, so we need to look at
681 * read register 3 in channel A to check if this is a
682 * real interrupt, or just the default value.
683 * Yes... broken hardware...
686 zs_intreg = read_zsreg(info->zs_channel, 3);
687 if (zs_intreg & CHBRxIP) {
688 special_receive(info->zs_next);
689 return;
691 info = info->zs_next->zs_next;
696 * -------------------------------------------------------------------
697 * Here ends the serial interrupt routines.
698 * -------------------------------------------------------------------
702 * This routine is used to handle the "bottom half" processing for the
703 * serial driver, known also the "software interrupt" processing.
704 * This processing is done at the kernel interrupt level, after the
705 * zs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
706 * is where time-consuming activities which can not be done in the
707 * interrupt driver proper are done; the interrupt driver schedules
708 * them using zs_sched_event(), and they get done here.
710 static void do_serial_bh(void)
712 run_task_queue(&tq_serial);
715 static void do_softint(void *private_)
717 struct sun_serial *info = (struct sun_serial *) private_;
718 struct tty_struct *tty;
720 tty = info->tty;
721 if (!tty)
722 return;
724 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
725 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
726 tty->ldisc.write_wakeup)
727 (tty->ldisc.write_wakeup)(tty);
728 wake_up_interruptible(&tty->write_wait);
733 * This routine is called from the scheduler tqueue when the interrupt
734 * routine has signalled that a hangup has occurred. The path of
735 * hangup processing is:
737 * serial interrupt routine -> (scheduler tqueue) ->
738 * do_serial_hangup() -> tty->hangup() -> zs_hangup()
741 static void do_serial_hangup(void *private_)
743 struct sun_serial *info = (struct sun_serial *) private_;
744 struct tty_struct *tty;
746 tty = info->tty;
747 if (!tty)
748 return;
749 #ifdef SERIAL_DEBUG_OPEN
750 printk("do_serial_hangup<%p: tty-%d\n",
751 __builtin_return_address(0), info->line);
752 #endif
754 tty_hangup(tty);
759 * This subroutine is called when the RS_TIMER goes off. It is used
760 * by the serial driver to handle ports that do not have an interrupt
761 * (irq=0). This doesn't work at all for 16450's, as a sun has a Z8530.
764 static void zs_timer(void)
766 printk("zs_timer called\n");
767 prom_halt();
768 return;
771 static int startup(struct sun_serial * info)
773 unsigned long flags;
775 if (info->flags & ZILOG_INITIALIZED)
776 return 0;
778 if (!info->xmit_buf) {
779 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
780 if (!info->xmit_buf)
781 return -ENOMEM;
784 save_flags(flags); cli();
786 #ifdef SERIAL_DEBUG_OPEN
787 printk("Starting up tty-%d (irq %d)...\n", info->line, info->irq);
788 #endif
791 * Clear the FIFO buffers and disable them
792 * (they will be reenabled in change_speed())
794 ZS_CLEARFIFO(info->zs_channel);
795 info->xmit_fifo_size = 1;
798 * Clear the interrupt registers.
800 info->zs_channel->control = ERR_RES;
801 ZSDELAY();
802 ZS_WSYNC(info->zs_channel);
804 info->zs_channel->control = RES_H_IUS;
805 ZSDELAY();
806 ZS_WSYNC(info->zs_channel);
809 * Now, initialize the Zilog
811 zs_rtsdtr(info, 1);
814 * Finally, enable sequencing and interrupts
816 info->curregs[1] |= (info->curregs[1] & ~(RxINT_MASK)) |
817 (EXT_INT_ENAB | INT_ALL_Rx);
818 info->curregs[3] |= (RxENAB | Rx8);
819 /* We enable Tx interrupts as needed. */
820 info->curregs[5] |= (TxENAB | Tx8);
821 info->curregs[9] |= (NV | MIE);
822 write_zsreg(info->zs_channel, 3, info->curregs[3]);
823 write_zsreg(info->zs_channel, 5, info->curregs[5]);
824 write_zsreg(info->zs_channel, 9, info->curregs[9]);
827 * And clear the interrupt registers again for luck.
829 info->zs_channel->control = ERR_RES;
830 ZSDELAY();
831 ZS_WSYNC(info->zs_channel);
833 info->zs_channel->control = RES_H_IUS;
834 ZSDELAY();
835 ZS_WSYNC(info->zs_channel);
837 if (info->tty)
838 clear_bit(TTY_IO_ERROR, &info->tty->flags);
839 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
842 * Set up serial timers...
844 #if 0 /* Works well and stops the machine. */
845 timer_table[RS_TIMER].expires = jiffies + 2;
846 timer_active |= 1 << RS_TIMER;
847 #endif
850 * and set the speed of the serial port
852 change_speed(info);
854 info->flags |= ZILOG_INITIALIZED;
855 restore_flags(flags);
856 return 0;
860 * This routine will shutdown a serial port; interrupts are disabled, and
861 * DTR is dropped if the hangup on close termio flag is on.
863 static void shutdown(struct sun_serial * info)
865 unsigned long flags;
867 if (!(info->flags & ZILOG_INITIALIZED))
868 return;
870 #ifdef SERIAL_DEBUG_OPEN
871 printk("Shutting down serial port %d (irq %d)....", info->line,
872 info->irq);
873 #endif
875 save_flags(flags); cli(); /* Disable interrupts */
877 if (info->xmit_buf) {
878 free_page((unsigned long) info->xmit_buf);
879 info->xmit_buf = 0;
882 if (info->tty)
883 set_bit(TTY_IO_ERROR, &info->tty->flags);
885 info->flags &= ~ZILOG_INITIALIZED;
886 restore_flags(flags);
890 * This routine is called to set the UART divisor registers to match
891 * the specified baud rate for a serial port.
893 static void change_speed(struct sun_serial *info)
895 unsigned short port;
896 unsigned cflag;
897 int quot = 0;
898 int i;
899 int brg;
901 if (!info->tty || !info->tty->termios)
902 return;
903 cflag = info->tty->termios->c_cflag;
904 if (!(port = info->port))
905 return;
906 i = cflag & CBAUD;
907 if (cflag & CBAUDEX) {
908 i &= ~CBAUDEX;
909 if (i != 5)
910 info->tty->termios->c_cflag &= ~CBAUDEX;
911 else
912 i = 16;
914 if (i == 15) {
915 if ((info->flags & ZILOG_SPD_MASK) == ZILOG_SPD_HI)
916 i += 1;
917 if ((info->flags & ZILOG_SPD_MASK) == ZILOG_SPD_CUST)
918 quot = info->custom_divisor;
920 if (quot) {
921 info->zs_baud = info->baud_base / quot;
922 info->clk_divisor = 16;
924 info->curregs[4] = X16CLK;
925 info->curregs[11] = TCBR | RCBR;
926 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
927 info->curregs[12] = (brg & 255);
928 info->curregs[13] = ((brg >> 8) & 255);
929 info->curregs[14] = BRSRC | BRENAB;
930 zs_rtsdtr(info, 1);
931 } else if (baud_table[i]) {
932 info->zs_baud = baud_table[i];
933 info->clk_divisor = 16;
935 info->curregs[4] = X16CLK;
936 info->curregs[11] = TCBR | RCBR;
937 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
938 info->curregs[12] = (brg & 255);
939 info->curregs[13] = ((brg >> 8) & 255);
940 info->curregs[14] = BRSRC | BRENAB;
941 zs_rtsdtr(info, 1);
942 } else {
943 zs_rtsdtr(info, 0);
944 return;
947 /* byte size and parity */
948 switch (cflag & CSIZE) {
949 case CS5:
950 info->curregs[3] &= ~(RxN_MASK);
951 info->curregs[3] |= Rx5;
952 info->curregs[5] &= ~(TxN_MASK);
953 info->curregs[5] |= Tx5;
954 info->parity_mask = 0x1f;
955 break;
956 case CS6:
957 info->curregs[3] &= ~(RxN_MASK);
958 info->curregs[3] |= Rx6;
959 info->curregs[5] &= ~(TxN_MASK);
960 info->curregs[5] |= Tx6;
961 info->parity_mask = 0x3f;
962 break;
963 case CS7:
964 info->curregs[3] &= ~(RxN_MASK);
965 info->curregs[3] |= Rx7;
966 info->curregs[5] &= ~(TxN_MASK);
967 info->curregs[5] |= Tx7;
968 info->parity_mask = 0x7f;
969 break;
970 case CS8:
971 default: /* defaults to 8 bits */
972 info->curregs[3] &= ~(RxN_MASK);
973 info->curregs[3] |= Rx8;
974 info->curregs[5] &= ~(TxN_MASK);
975 info->curregs[5] |= Tx8;
976 info->parity_mask = 0xff;
977 break;
979 info->curregs[4] &= ~(0x0c);
980 if (cflag & CSTOPB) {
981 info->curregs[4] |= SB2;
982 } else {
983 info->curregs[4] |= SB1;
985 if (cflag & PARENB) {
986 info->curregs[4] |= PAR_ENAB;
987 } else {
988 info->curregs[4] &= ~PAR_ENAB;
990 if (!(cflag & PARODD)) {
991 info->curregs[4] |= PAR_EVEN;
992 } else {
993 info->curregs[4] &= ~PAR_EVEN;
996 /* Load up the new values */
997 load_zsregs(info, info->curregs);
999 return;
1002 /* This is for mouse/keyboard output.
1003 * XXX mouse output??? can we send it commands??? XXX
1005 static void kbd_put_char(unsigned char ch)
1007 struct sun_zschannel *chan = zs_kbdchan;
1008 unsigned long flags;
1010 if(!chan)
1011 return;
1013 save_flags(flags); cli();
1014 zs_put_char(chan, ch);
1015 restore_flags(flags);
1018 void mouse_put_char(char ch)
1020 struct sun_zschannel *chan = zs_mousechan;
1021 unsigned long flags;
1023 if(!chan)
1024 return;
1026 save_flags(flags); cli();
1027 zs_put_char(chan, ch);
1028 restore_flags(flags);
1031 /* These are for receiving and sending characters under the kgdb
1032 * source level kernel debugger.
1034 void putDebugChar(char kgdb_char)
1036 struct sun_zschannel *chan = zs_kgdbchan;
1038 while((chan->control & Tx_BUF_EMP)==0)
1039 udelay(5);
1040 chan->data = kgdb_char;
1041 ZS_WSYNC(chan);
1044 char getDebugChar(void)
1046 struct sun_zschannel *chan = zs_kgdbchan;
1048 while((chan->control & Rx_CH_AV)==0)
1049 udelay(5);
1050 return chan->data;
1053 static void zs_flush_chars(struct tty_struct *tty)
1055 struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1056 unsigned long flags;
1058 if (serial_paranoia_check(info, tty->device, "zs_flush_chars"))
1059 return;
1061 save_flags(flags); cli();
1062 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1063 !info->xmit_buf)
1064 goto out;
1066 /* Enable transmitter */
1067 info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1068 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1069 info->curregs[5] |= TxENAB;
1070 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1073 * Send a first (bootstrapping) character. A best solution is
1074 * to call transmit_chars() here which handles output in a
1075 * generic way. Current transmit_chars() not only transmits,
1076 * but resets interrupts also what we do not desire here.
1077 * XXX Discuss with David.
1079 zs_put_char(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
1080 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1081 info->xmit_cnt--;
1083 out:
1084 restore_flags(flags);
1087 static int zs_write(struct tty_struct * tty, int from_user,
1088 const unsigned char *buf, int count)
1090 int c, total = 0;
1091 struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1092 unsigned long flags;
1094 if (serial_paranoia_check(info, tty->device, "zs_write"))
1095 return 0;
1097 if (!info || !info->xmit_buf)
1098 return 0;
1100 save_flags(flags);
1101 while (1) {
1102 cli();
1103 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1104 SERIAL_XMIT_SIZE - info->xmit_head));
1105 if (c <= 0)
1106 break;
1108 if (from_user) {
1109 down(&tmp_buf_sem);
1110 copy_from_user(tmp_buf, buf, c);
1111 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1112 SERIAL_XMIT_SIZE - info->xmit_head));
1113 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1114 up(&tmp_buf_sem);
1115 } else
1116 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1117 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1118 info->xmit_cnt += c;
1119 restore_flags(flags);
1120 buf += c;
1121 count -= c;
1122 total += c;
1125 cli();
1126 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
1127 /* Enable transmitter */
1128 info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1129 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1130 info->curregs[5] |= TxENAB;
1131 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1132 #if 1
1133 zs_put_char(info->zs_channel,
1134 info->xmit_buf[info->xmit_tail++]);
1135 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1136 info->xmit_cnt--;
1137 #endif
1140 restore_flags(flags);
1141 return total;
1144 static int zs_write_room(struct tty_struct *tty)
1146 struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1147 int ret;
1149 if (serial_paranoia_check(info, tty->device, "zs_write_room"))
1150 return 0;
1151 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1152 if (ret < 0)
1153 ret = 0;
1154 return ret;
1157 static int zs_chars_in_buffer(struct tty_struct *tty)
1159 struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1161 if (serial_paranoia_check(info, tty->device, "zs_chars_in_buffer"))
1162 return 0;
1163 return info->xmit_cnt;
1166 static void zs_flush_buffer(struct tty_struct *tty)
1168 struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1170 if (serial_paranoia_check(info, tty->device, "zs_flush_buffer"))
1171 return;
1172 cli();
1173 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1174 sti();
1175 wake_up_interruptible(&tty->write_wait);
1176 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1177 tty->ldisc.write_wakeup)
1178 (tty->ldisc.write_wakeup)(tty);
1182 * ------------------------------------------------------------
1183 * zs_throttle()
1185 * This routine is called by the upper-layer tty layer to signal that
1186 * incoming characters should be throttled.
1187 * ------------------------------------------------------------
1189 static void zs_throttle(struct tty_struct * tty)
1191 struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1192 #ifdef SERIAL_DEBUG_THROTTLE
1193 char buf[64];
1195 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1196 tty->ldisc.chars_in_buffer(tty));
1197 #endif
1199 if (serial_paranoia_check(info, tty->device, "zs_throttle"))
1200 return;
1202 if (I_IXOFF(tty))
1203 info->x_char = STOP_CHAR(tty);
1205 /* Turn off RTS line */
1206 cli();
1207 info->curregs[5] &= ~RTS;
1208 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1209 sti();
1212 static void zs_unthrottle(struct tty_struct * tty)
1214 struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1215 #ifdef SERIAL_DEBUG_THROTTLE
1216 char buf[64];
1218 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1219 tty->ldisc.chars_in_buffer(tty));
1220 #endif
1222 if (serial_paranoia_check(info, tty->device, "zs_unthrottle"))
1223 return;
1225 if (I_IXOFF(tty)) {
1226 if (info->x_char)
1227 info->x_char = 0;
1228 else
1229 info->x_char = START_CHAR(tty);
1232 /* Assert RTS line */
1233 cli();
1234 info->curregs[5] |= RTS;
1235 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1236 sti();
1240 * ------------------------------------------------------------
1241 * zs_ioctl() and friends
1242 * ------------------------------------------------------------
1245 static int get_serial_info(struct sun_serial * info,
1246 struct serial_struct * retinfo)
1248 struct serial_struct tmp;
1250 if (!retinfo)
1251 return -EFAULT;
1252 memset(&tmp, 0, sizeof(tmp));
1253 tmp.type = info->type;
1254 tmp.line = info->line;
1255 tmp.port = info->port;
1256 tmp.irq = info->irq;
1257 tmp.flags = info->flags;
1258 tmp.baud_base = info->baud_base;
1259 tmp.close_delay = info->close_delay;
1260 tmp.closing_wait = info->closing_wait;
1261 tmp.custom_divisor = info->custom_divisor;
1262 copy_to_user_ret(retinfo,&tmp,sizeof(*retinfo), -EFAULT);
1263 return 0;
1266 static int set_serial_info(struct sun_serial * info,
1267 struct serial_struct * new_info)
1269 struct serial_struct new_serial;
1270 struct sun_serial old_info;
1271 int retval = 0;
1273 if (!new_info || copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1274 return -EFAULT;
1275 old_info = *info;
1277 if (!capable(CAP_SYS_ADMIN)) {
1278 if ((new_serial.baud_base != info->baud_base) ||
1279 (new_serial.type != info->type) ||
1280 (new_serial.close_delay != info->close_delay) ||
1281 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1282 (info->flags & ~ZILOG_USR_MASK)))
1283 return -EPERM;
1284 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1285 (new_serial.flags & ZILOG_USR_MASK));
1286 info->custom_divisor = new_serial.custom_divisor;
1287 goto check_and_exit;
1290 if(new_serial.baud_base < 9600)
1291 return -EINVAL;
1293 if (info->count > 1)
1294 return -EBUSY;
1297 * OK, past this point, all the error checking has been done.
1298 * At this point, we start making changes.....
1301 info->baud_base = new_serial.baud_base;
1302 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1303 (new_serial.flags & ZILOG_FLAGS));
1304 info->custom_divisor = new_serial.custom_divisor;
1305 info->type = new_serial.type;
1306 info->close_delay = new_serial.close_delay;
1307 info->closing_wait = new_serial.closing_wait;
1309 check_and_exit:
1310 retval = startup(info);
1311 return retval;
1315 * get_lsr_info - get line status register info
1317 * Purpose: Let user call ioctl() to get info when the UART physically
1318 * is emptied. On bus types like RS485, the transmitter must
1319 * release the bus after transmitting. This must be done when
1320 * the transmit shift register is empty, not be done when the
1321 * transmit holding register is empty. This functionality
1322 * allows an RS485 driver to be written in user space.
1324 static int get_lsr_info(struct sun_serial * info, unsigned int *value)
1326 unsigned char status;
1328 cli();
1329 status = info->zs_channel->control;
1330 ZSDELAY();
1331 sti();
1332 put_user_ret(status,value, -EFAULT);
1333 return 0;
1336 static int get_modem_info(struct sun_serial * info, unsigned int *value)
1338 unsigned char status;
1339 unsigned int result;
1341 cli();
1342 status = info->zs_channel->control;
1343 ZSDELAY();
1344 sti();
1345 result = ((info->curregs[5] & RTS) ? TIOCM_RTS : 0)
1346 | ((info->curregs[5] & DTR) ? TIOCM_DTR : 0)
1347 | ((status & DCD) ? TIOCM_CAR : 0)
1348 | ((status & SYNC) ? TIOCM_DSR : 0)
1349 | ((status & CTS) ? TIOCM_CTS : 0);
1350 put_user_ret(result, value, -EFAULT);
1351 return 0;
1354 static int set_modem_info(struct sun_serial * info, unsigned int cmd,
1355 unsigned int *value)
1357 unsigned int arg;
1359 get_user_ret(arg, value, -EFAULT);
1360 switch (cmd) {
1361 case TIOCMBIS:
1362 if (arg & TIOCM_RTS)
1363 info->curregs[5] |= RTS;
1364 if (arg & TIOCM_DTR)
1365 info->curregs[5] |= DTR;
1366 break;
1367 case TIOCMBIC:
1368 if (arg & TIOCM_RTS)
1369 info->curregs[5] &= ~RTS;
1370 if (arg & TIOCM_DTR)
1371 info->curregs[5] &= ~DTR;
1372 break;
1373 case TIOCMSET:
1374 info->curregs[5] = ((info->curregs[5] & ~(RTS | DTR))
1375 | ((arg & TIOCM_RTS) ? RTS : 0)
1376 | ((arg & TIOCM_DTR) ? DTR : 0));
1377 break;
1378 default:
1379 return -EINVAL;
1381 cli();
1382 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1383 sti();
1384 return 0;
1388 * This routine sends a break character out the serial port.
1390 static void send_break( struct sun_serial * info, int duration)
1392 if (!info->port)
1393 return;
1394 current->state = TASK_INTERRUPTIBLE;
1395 cli();
1396 write_zsreg(info->zs_channel, 5, (info->curregs[5] | SND_BRK));
1397 schedule_timeout(duration);
1398 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1399 sti();
1402 static int zs_ioctl(struct tty_struct *tty, struct file * file,
1403 unsigned int cmd, unsigned long arg)
1405 struct sun_serial * info = (struct sun_serial *)tty->driver_data;
1406 int retval;
1408 if (serial_paranoia_check(info, tty->device, "zs_ioctl"))
1409 return -ENODEV;
1411 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1412 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1413 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1414 if (tty->flags & (1 << TTY_IO_ERROR))
1415 return -EIO;
1418 switch (cmd) {
1419 case TCSBRK: /* SVID version: non-zero arg --> no break */
1420 retval = tty_check_change(tty);
1421 if (retval)
1422 return retval;
1423 tty_wait_until_sent(tty, 0);
1424 if (!arg)
1425 send_break(info, HZ/4); /* 1/4 second */
1426 return 0;
1427 case TCSBRKP: /* support for POSIX tcsendbreak() */
1428 retval = tty_check_change(tty);
1429 if (retval)
1430 return retval;
1431 tty_wait_until_sent(tty, 0);
1432 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1433 return 0;
1434 case TIOCGSOFTCAR:
1435 put_user_ret(C_CLOCAL(tty) ? 1 : 0,
1436 (unsigned long *) arg, -EFAULT);
1437 return 0;
1438 case TIOCSSOFTCAR:
1439 get_user_ret(arg, (unsigned long *) arg, -EFAULT);
1440 tty->termios->c_cflag =
1441 ((tty->termios->c_cflag & ~CLOCAL) |
1442 (arg ? CLOCAL : 0));
1443 return 0;
1444 case TIOCMGET:
1445 return get_modem_info(info, (unsigned int *) arg);
1446 case TIOCMBIS:
1447 case TIOCMBIC:
1448 case TIOCMSET:
1449 return set_modem_info(info, cmd, (unsigned int *) arg);
1450 case TIOCGSERIAL:
1451 return get_serial_info(info,
1452 (struct serial_struct *) arg);
1453 case TIOCSSERIAL:
1454 return set_serial_info(info,
1455 (struct serial_struct *) arg);
1456 case TIOCSERGETLSR: /* Get line status register */
1457 return get_lsr_info(info, (unsigned int *) arg);
1459 case TIOCSERGSTRUCT:
1460 copy_to_user_ret((struct sun_serial *) arg,
1461 info, sizeof(struct sun_serial), -EFAULT);
1462 return 0;
1464 default:
1465 return -ENOIOCTLCMD;
1467 return 0;
1470 static void zs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1472 struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1474 if (tty->termios->c_cflag == old_termios->c_cflag)
1475 return;
1477 change_speed(info);
1479 if ((old_termios->c_cflag & CRTSCTS) &&
1480 !(tty->termios->c_cflag & CRTSCTS)) {
1481 tty->hw_stopped = 0;
1482 zs_start(tty);
1487 * ------------------------------------------------------------
1488 * zs_close()
1490 * This routine is called when the serial port gets closed. First, we
1491 * wait for the last remaining data to be sent. Then, we unlink its
1492 * ZILOG structure from the interrupt chain if necessary, and we free
1493 * that IRQ if nothing is left in the chain.
1494 * ------------------------------------------------------------
1496 static void zs_close(struct tty_struct *tty, struct file * filp)
1498 struct sun_serial * info = (struct sun_serial *)tty->driver_data;
1499 unsigned long flags;
1501 if (!info || serial_paranoia_check(info, tty->device, "zs_close"))
1502 return;
1504 save_flags(flags); cli();
1506 if (tty_hung_up_p(filp)) {
1507 restore_flags(flags);
1508 return;
1511 #ifdef SERIAL_DEBUG_OPEN
1512 printk("zs_close tty-%d, count = %d\n", info->line, info->count);
1513 #endif
1514 if ((tty->count == 1) && (info->count != 1)) {
1516 * Uh, oh. tty->count is 1, which means that the tty
1517 * structure will be freed. Info->count should always
1518 * be one in these conditions. If it's greater than
1519 * one, we've got real problems, since it means the
1520 * serial port won't be shutdown.
1522 printk("zs_close: bad serial port count; tty->count is 1, "
1523 "info->count is %d\n", info->count);
1524 info->count = 1;
1526 if (--info->count < 0) {
1527 printk("zs_close: bad serial port count for ttys%d: %d\n",
1528 info->line, info->count);
1529 info->count = 0;
1531 if (info->count) {
1532 restore_flags(flags);
1533 return;
1535 info->flags |= ZILOG_CLOSING;
1537 * Save the termios structure, since this port may have
1538 * separate termios for callout and dialin.
1540 if (info->flags & ZILOG_NORMAL_ACTIVE)
1541 info->normal_termios = *tty->termios;
1542 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1543 info->callout_termios = *tty->termios;
1545 * Now we wait for the transmit buffer to clear; and we notify
1546 * the line discipline to only process XON/XOFF characters.
1548 tty->closing = 1;
1549 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1550 tty_wait_until_sent(tty, info->closing_wait);
1552 * At this point we stop accepting input. To do this, we
1553 * disable the receive line status interrupts, and tell the
1554 * interrupt driver to stop checking the data ready bit in the
1555 * line status register.
1557 /** if (!info->iscons) ... **/
1558 info->curregs[3] &= ~RxENAB;
1559 write_zsreg(info->zs_channel, 3, info->curregs[3]);
1560 info->curregs[1] &= ~(RxINT_MASK);
1561 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1562 ZS_CLEARFIFO(info->zs_channel);
1564 shutdown(info);
1565 if (tty->driver.flush_buffer)
1566 tty->driver.flush_buffer(tty);
1567 if (tty->ldisc.flush_buffer)
1568 tty->ldisc.flush_buffer(tty);
1569 tty->closing = 0;
1570 info->event = 0;
1571 info->tty = 0;
1572 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1573 if (tty->ldisc.close)
1574 (tty->ldisc.close)(tty);
1575 tty->ldisc = ldiscs[N_TTY];
1576 tty->termios->c_line = N_TTY;
1577 if (tty->ldisc.open)
1578 (tty->ldisc.open)(tty);
1580 if (info->blocked_open) {
1581 if (info->close_delay) {
1582 current->state = TASK_INTERRUPTIBLE;
1583 schedule_timeout(info->close_delay);
1585 wake_up_interruptible(&info->open_wait);
1587 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1588 ZILOG_CLOSING);
1589 wake_up_interruptible(&info->close_wait);
1590 #ifdef SERIAL_DEBUG_OPEN
1591 printk("zs_close tty-%d exiting, count = %d\n", info->line, info->count);
1592 #endif
1593 restore_flags(flags);
1597 * zs_hangup() --- called by tty_hangup() when a hangup is signaled.
1599 void zs_hangup(struct tty_struct *tty)
1601 struct sun_serial * info = (struct sun_serial *)tty->driver_data;
1603 if (serial_paranoia_check(info, tty->device, "zs_hangup"))
1604 return;
1606 if (info->is_cons)
1607 return;
1609 #ifdef SERIAL_DEBUG_OPEN
1610 printk("zs_hangup<%p: tty-%d, count = %d bye\n",
1611 __builtin_return_address(0), info->line, info->count);
1612 #endif
1614 zs_flush_buffer(tty);
1615 shutdown(info);
1616 info->event = 0;
1617 info->count = 0;
1618 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1619 info->tty = 0;
1620 wake_up_interruptible(&info->open_wait);
1624 * ------------------------------------------------------------
1625 * zs_open() and friends
1626 * ------------------------------------------------------------
1628 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1629 struct sun_serial *info)
1631 DECLARE_WAITQUEUE(wait, current);
1632 int retval;
1633 int do_clocal = 0;
1634 unsigned char r0;
1637 * If the device is in the middle of being closed, then block
1638 * until it's done, and then try again.
1640 if (info->flags & ZILOG_CLOSING) {
1641 interruptible_sleep_on(&info->close_wait);
1642 #ifdef SERIAL_DO_RESTART
1643 if (info->flags & ZILOG_HUP_NOTIFY)
1644 return -EAGAIN;
1645 else
1646 return -ERESTARTSYS;
1647 #else
1648 return -EAGAIN;
1649 #endif
1653 * If this is a callout device, then just make sure the normal
1654 * device isn't being used.
1656 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1657 if (info->flags & ZILOG_NORMAL_ACTIVE)
1658 return -EBUSY;
1659 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1660 (info->flags & ZILOG_SESSION_LOCKOUT) &&
1661 (info->session != current->session))
1662 return -EBUSY;
1663 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1664 (info->flags & ZILOG_PGRP_LOCKOUT) &&
1665 (info->pgrp != current->pgrp))
1666 return -EBUSY;
1667 info->flags |= ZILOG_CALLOUT_ACTIVE;
1668 return 0;
1672 * If non-blocking mode is set, or the port is not enabled,
1673 * then make the check up front and then exit.
1675 if ((filp->f_flags & O_NONBLOCK) ||
1676 (tty->flags & (1 << TTY_IO_ERROR))) {
1677 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1678 return -EBUSY;
1679 info->flags |= ZILOG_NORMAL_ACTIVE;
1680 return 0;
1683 if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1684 if (info->normal_termios.c_cflag & CLOCAL)
1685 do_clocal = 1;
1686 } else {
1687 if (tty->termios->c_cflag & CLOCAL)
1688 do_clocal = 1;
1692 * Block waiting for the carrier detect and the line to become
1693 * free (i.e., not in use by the callout). While we are in
1694 * this loop, info->count is dropped by one, so that
1695 * zs_close() knows when to free things. We restore it upon
1696 * exit, either normal or abnormal.
1698 retval = 0;
1699 add_wait_queue(&info->open_wait, &wait);
1700 #ifdef SERIAL_DEBUG_OPEN
1701 printk("block_til_ready before block: ttys%d, count = %d\n",
1702 info->line, info->count);
1703 #endif
1704 cli();
1705 if(!tty_hung_up_p(filp))
1706 info->count--;
1707 sti();
1708 info->blocked_open++;
1709 while (1) {
1710 cli();
1711 if (!(info->flags & ZILOG_CALLOUT_ACTIVE))
1712 zs_rtsdtr(info, 1);
1713 sti();
1714 set_current_state(TASK_INTERRUPTIBLE);
1715 if (tty_hung_up_p(filp) ||
1716 !(info->flags & ZILOG_INITIALIZED)) {
1717 #ifdef SERIAL_DEBUG_OPEN
1718 printk("block_til_ready hup-ed: ttys%d, count = %d\n",
1719 info->line, info->count);
1720 #endif
1721 #ifdef SERIAL_DO_RESTART
1722 if (info->flags & ZILOG_HUP_NOTIFY)
1723 retval = -EAGAIN;
1724 else
1725 retval = -ERESTARTSYS;
1726 #else
1727 retval = -EAGAIN;
1728 #endif
1729 break;
1732 cli();
1733 r0 = read_zsreg(info->zs_channel, R0);
1734 sti();
1735 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1736 !(info->flags & ZILOG_CLOSING) &&
1737 (do_clocal || (DCD & r0)))
1738 break;
1739 if (signal_pending(current)) {
1740 retval = -ERESTARTSYS;
1741 break;
1743 #ifdef SERIAL_DEBUG_OPEN
1744 printk("block_til_ready blocking: ttys%d, count = %d\n",
1745 info->line, info->count);
1746 #endif
1747 schedule();
1749 current->state = TASK_RUNNING;
1750 remove_wait_queue(&info->open_wait, &wait);
1751 if (!tty_hung_up_p(filp))
1752 info->count++;
1753 info->blocked_open--;
1754 #ifdef SERIAL_DEBUG_OPEN
1755 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1756 info->line, info->count);
1757 #endif
1758 if (retval)
1759 return retval;
1760 info->flags |= ZILOG_NORMAL_ACTIVE;
1761 return 0;
1765 * This routine is called whenever a serial port is opened. It
1766 * enables interrupts for a serial port, linking in its ZILOG structure into
1767 * the IRQ chain. It also performs the serial-specific
1768 * initialization for the tty structure.
1770 int zs_open(struct tty_struct *tty, struct file * filp)
1772 struct sun_serial *info;
1773 int retval, line;
1775 line = MINOR(tty->device) - tty->driver.minor_start;
1776 /* The zilog lines for the mouse/keyboard must be
1777 * opened using their respective drivers.
1779 if ((line < 0) || (line >= NUM_CHANNELS))
1780 return -ENODEV;
1781 if((line == KEYBOARD_LINE) || (line == MOUSE_LINE))
1782 return -ENODEV;
1783 info = zs_soft + line;
1784 /* Is the kgdb running over this line? */
1785 if (info->kgdb_channel)
1786 return -ENODEV;
1787 if (serial_paranoia_check(info, tty->device, "zs_open"))
1788 return -ENODEV;
1789 #ifdef SERIAL_DEBUG_OPEN
1790 printk("zs_open %s%d, count = %d\n", tty->driver.name, info->line,
1791 info->count);
1792 #endif
1793 if (info->tty != 0 && info->tty != tty) {
1794 /* Never happen? */
1795 printk("zs_open %s%d, tty overwrite.\n", tty->driver.name, info->line);
1796 return -EBUSY;
1798 info->count++;
1799 tty->driver_data = info;
1800 info->tty = tty;
1803 * Start up serial port
1805 retval = startup(info);
1806 if (retval)
1807 return retval;
1809 retval = block_til_ready(tty, filp, info);
1810 if (retval) {
1811 #ifdef SERIAL_DEBUG_OPEN
1812 printk("zs_open returning after block_til_ready with %d\n",
1813 retval);
1814 #endif
1815 return retval;
1818 if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1819 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1820 *tty->termios = info->normal_termios;
1821 else
1822 *tty->termios = info->callout_termios;
1823 change_speed(info);
1826 #ifdef CONFIG_SERIAL_CONSOLE
1827 if (zs_console.cflag && zs_console.index == line) {
1828 tty->termios->c_cflag = zs_console.cflag;
1829 zs_console.cflag = 0;
1830 change_speed(info);
1832 #endif
1834 info->session = current->session;
1835 info->pgrp = current->pgrp;
1837 #ifdef SERIAL_DEBUG_OPEN
1838 printk("zs_open ttys%d successful...", info->line);
1839 #endif
1840 return 0;
1843 /* Finally, routines used to initialize the serial driver. */
1845 static void show_serial_version(void)
1847 char *revision = "$Revision: 1.45 $";
1848 char *version, *p;
1850 version = strchr(revision, ' ');
1851 p = strchr(++version, ' ');
1852 *p = '\0';
1853 printk("Sparc Zilog8530 serial driver version %s\n", version);
1854 *p = ' ';
1857 /* Probe the PROM for the request zs chip number.
1859 * Note: The Sun Voyager shows two addresses and two intr for it's
1860 * Zilogs, what the second does, I don't know. It does work
1861 * with using only the first number of each property. Also
1862 * we have a special version for sun4u.
1864 #ifdef __sparc_v9__
1865 static struct sun_zslayout * __init get_zs(int chip)
1867 unsigned int vaddr[2] = { 0, 0 };
1868 unsigned long mapped_addr = 0;
1869 int busnode, seen, zsnode, sun4u_ino;
1870 static int irq = 0;
1872 if(chip < 0 || chip >= NUM_SERIAL)
1873 panic("get_zs bogon zs chip number");
1875 if(central_bus)
1876 busnode = central_bus->child->prom_node;
1877 else
1878 busnode = prom_searchsiblings(prom_getchild(prom_root_node), "sbus");
1879 if(busnode == 0 || busnode == -1)
1880 panic("get_zs: no zs bus to search");
1882 zsnode = prom_getchild(busnode);
1883 seen = 0;
1884 while(zsnode) {
1885 int slave;
1887 zsnode = prom_searchsiblings(zsnode, "zs");
1888 slave = prom_getintdefault(zsnode, "slave", -1);
1889 if((slave == chip) || (seen == chip)) {
1890 int len = prom_getproperty(zsnode, "address",
1891 (void *) vaddr, sizeof(vaddr));
1893 if(len == -1 || central_bus != NULL) {
1894 struct linux_sbus *sbus = NULL;
1895 struct linux_sbus_device *sdev = NULL;
1897 /* "address" property is not guarenteed,
1898 * everything in I/O is implicitly mapped
1899 * anyways by our clever TLB miss handling
1900 * scheme, so don't fail here. -DaveM
1902 if (central_bus == NULL) {
1903 for_each_sbus(sbus) {
1904 for_each_sbusdev(sdev, sbus) {
1905 if (sdev->prom_node == zsnode)
1906 goto found;
1910 found:
1911 if (sdev == NULL && central_bus == NULL)
1912 prom_halt();
1913 if (central_bus == NULL) {
1914 prom_apply_sbus_ranges(sbus, sdev->reg_addrs, 1, sdev);
1915 mapped_addr = (unsigned long)
1916 sparc_alloc_io(sdev->reg_addrs[0].phys_addr, 0,
1917 PAGE_SIZE, "Zilog Registers",
1918 sdev->reg_addrs[0].which_io, 0x0);
1919 } else {
1920 struct linux_prom_registers zsregs[1];
1921 int err;
1923 err = prom_getproperty(zsnode, "reg",
1924 (char *)&zsregs[0],
1925 sizeof(zsregs));
1926 if (err == -1) {
1927 prom_printf("ZS: Cannot map Zilog regs.\n");
1928 prom_halt();
1930 prom_apply_fhc_ranges(central_bus->child, &zsregs[0], 1);
1931 prom_apply_central_ranges(central_bus, &zsregs[0], 1);
1932 mapped_addr = (unsigned long)
1933 __va((((unsigned long)zsregs[0].which_io)<<32) |
1934 (((unsigned long)zsregs[0].phys_addr)));
1936 } else if(len % sizeof(unsigned int)) {
1937 prom_printf("WHOOPS: proplen for %s "
1938 "was %d, need multiple of "
1939 "%d\n", "address", len,
1940 sizeof(unsigned int));
1941 panic("zilog: address property");
1943 zs_nodes[chip] = zsnode;
1944 len = prom_getproperty(zsnode, "interrupts",
1945 (char *) &sun4u_ino,
1946 (sizeof(sun4u_ino)));
1947 if(!irq) {
1948 if (central_bus) {
1949 irq = zilog_irq =
1950 build_irq(12, 0,
1951 &central_bus->child->fhc_regs.uregs->fhc_uart_iclr,
1952 &central_bus->child->fhc_regs.uregs->fhc_uart_imap);
1953 } else {
1954 irq = zilog_irq =
1955 sbus_build_irq(SBus_chain, sun4u_ino);
1958 break;
1960 zsnode = prom_getsibling(zsnode);
1961 seen++;
1963 if(!zsnode)
1964 panic("get_zs: whee chip not found");
1965 if(!vaddr[0] && !mapped_addr)
1966 panic("get_zs: whee no serial chip mappable");
1967 if (mapped_addr != 0)
1968 return (struct sun_zslayout *) mapped_addr;
1969 else
1970 return (struct sun_zslayout *) (unsigned long) vaddr[0];
1972 #else /* !(__sparc_v9__) */
1973 static struct sun_zslayout * __init get_zs(int chip)
1975 struct linux_prom_irqs tmp_irq[2];
1976 unsigned int paddr = 0;
1977 unsigned int vaddr[2] = { 0, 0 };
1978 int zsnode, tmpnode, iospace, slave, len;
1979 int cpunode = 0, bbnode = 0;
1980 static int irq = 0;
1981 int chipid = chip;
1983 #if CONFIG_AP1000
1984 printk("No zs chip\n");
1985 return NULL;
1986 #endif
1988 iospace = 0;
1989 if(chip < 0 || chip >= NUM_SERIAL)
1990 panic("get_zs bogon zs chip number");
1992 if(sparc_cpu_model == sun4) {
1993 /* Grrr, these have to be hardcoded aieee */
1994 switch(chip) {
1995 case 0:
1996 paddr = 0xf1000000;
1997 break;
1998 case 1:
1999 paddr = 0xf0000000;
2000 break;
2002 iospace = 0;
2003 zs_nodes[chip] = 0;
2004 if(!irq)
2005 zilog_irq = irq = 12;
2006 vaddr[0] = (unsigned long)
2007 sparc_alloc_io(paddr, 0, 8,
2008 "Zilog Serial", iospace, 0);
2009 } else {
2010 /* Can use the prom for other machine types */
2011 zsnode = prom_getchild(prom_root_node);
2012 if (sparc_cpu_model == sun4d) {
2013 int no = 0;
2015 tmpnode = zsnode;
2016 zsnode = 0;
2017 bbnode = 0;
2018 while (tmpnode && (tmpnode = prom_searchsiblings(tmpnode, "cpu-unit"))) {
2019 bbnode = prom_getchild(tmpnode);
2020 if (bbnode && (bbnode = prom_searchsiblings(bbnode, "bootbus"))) {
2021 if (no == (chip >> 1)) {
2022 cpunode = tmpnode;
2023 zsnode = prom_getchild(bbnode);
2024 chipid = (chip & 1);
2025 break;
2027 no++;
2029 tmpnode = prom_getsibling(tmpnode);
2031 if (!tmpnode)
2032 panic ("get_zs: couldn't find %dth bootbus\n", chip >> 1);
2033 } else {
2034 tmpnode = prom_searchsiblings(zsnode, "obio");
2035 if(tmpnode)
2036 zsnode = prom_getchild(tmpnode);
2038 if(!zsnode)
2039 panic("get_zs no zs serial prom node");
2040 while(zsnode) {
2041 zsnode = prom_searchsiblings(zsnode, "zs");
2042 slave = prom_getintdefault(zsnode, "slave", -1);
2043 if(slave == chipid) {
2044 /* The one we want */
2045 if (sparc_cpu_model != sun4d) {
2046 len = prom_getproperty(zsnode, "address",
2047 (void *) vaddr,
2048 sizeof(vaddr));
2049 if (len % sizeof(unsigned int)) {
2050 prom_printf("WHOOPS: proplen for %s "
2051 "was %d, need multiple of "
2052 "%d\n", "address", len,
2053 sizeof(unsigned int));
2054 panic("zilog: address property");
2056 } else {
2057 /* On sun4d don't have address property :( */
2058 struct linux_prom_registers zsreg[4];
2060 if (prom_getproperty(zsnode, "reg", (char *)zsreg, sizeof(zsreg)) == -1) {
2061 prom_printf ("Cannot map zs regs\n");
2062 prom_halt();
2064 prom_apply_generic_ranges(bbnode, cpunode, zsreg, 1);
2065 vaddr[0] = (unsigned long)
2066 sparc_alloc_io(zsreg[0].phys_addr, 0, 8,
2067 "Zilog Serial", zsreg[0].which_io, 0);
2069 zs_nodes[chip] = zsnode;
2070 len = prom_getproperty(zsnode, "intr",
2071 (char *) tmp_irq,
2072 sizeof(tmp_irq));
2073 if (len % sizeof(struct linux_prom_irqs)) {
2074 prom_printf(
2075 "WHOOPS: proplen for %s "
2076 "was %d, need multiple of "
2077 "%d\n", "address", len,
2078 sizeof(struct linux_prom_irqs));
2079 panic("zilog: address property");
2081 if(!irq) {
2082 irq = zilog_irq = tmp_irq[0].pri;
2083 } else {
2084 if(tmp_irq[0].pri != irq)
2085 panic("zilog: bogon irqs");
2087 break;
2089 zsnode = prom_getsibling(zsnode);
2091 if(!zsnode)
2092 panic("get_zs whee chip not found");
2094 if(!vaddr[0])
2095 panic("get_zs whee no serial chip mappable");
2097 return (struct sun_zslayout *)(unsigned long) vaddr[0];
2099 #endif
2100 /* This is for the auto baud rate detection in the mouse driver. */
2101 void zs_change_mouse_baud(int newbaud)
2103 int channel = MOUSE_LINE;
2104 int brg;
2106 zs_soft[channel].zs_baud = newbaud;
2107 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2108 (ZS_CLOCK / zs_soft[channel].clk_divisor));
2109 write_zsreg(zs_soft[channel].zs_channel, R12, (brg & 0xff));
2110 write_zsreg(zs_soft[channel].zs_channel, R13, ((brg >> 8) & 0xff));
2113 int __init zs_probe (unsigned long *memory_start)
2115 char *p;
2116 int node;
2117 int i;
2119 if(sparc_cpu_model == sun4)
2120 goto no_probe;
2122 NUM_SERIAL = 0;
2124 node = prom_getchild(prom_root_node);
2125 if (sparc_cpu_model == sun4d) {
2126 int bbnode;
2128 while (node && (node = prom_searchsiblings(node, "cpu-unit"))) {
2129 bbnode = prom_getchild(node);
2130 if (bbnode && prom_searchsiblings(bbnode, "bootbus"))
2131 NUM_SERIAL += 2;
2132 node = prom_getsibling(node);
2134 goto no_probe;
2136 #ifdef __sparc_v9__
2137 else if (sparc_cpu_model == sun4u) {
2138 int central_node;
2140 /* Central bus zilogs must be checked for first,
2141 * since Enterprise boxes might have SBUSes as well.
2143 central_node = prom_finddevice("/central");
2144 if(central_node != 0 && central_node != -1)
2145 node = prom_searchsiblings(prom_getchild(central_node), "fhc");
2146 else
2147 node = prom_searchsiblings(node, "sbus");
2148 if(node != 0 && node != -1)
2149 node = prom_getchild(node);
2150 if(node == 0 || node == -1)
2151 return -ENODEV;
2153 #endif /* __sparc_v9__ */
2154 else {
2155 node = prom_searchsiblings(node, "obio");
2156 if(node)
2157 node = prom_getchild(node);
2158 NUM_SERIAL = 2;
2159 goto no_probe;
2162 node = prom_searchsiblings(node, "zs");
2163 if (!node)
2164 return -ENODEV;
2166 NUM_SERIAL = 2;
2168 no_probe:
2169 p = (char *)((*memory_start + 7) & ~7);
2170 zs_chips = (struct sun_zslayout **)(p);
2171 i = NUM_SERIAL * sizeof (struct sun_zslayout *);
2172 zs_channels = (struct sun_zschannel **)(p + i);
2173 i += NUM_CHANNELS * sizeof (struct sun_zschannel *);
2174 zs_nodes = (int *)(p + i);
2175 i += NUM_SERIAL * sizeof (int);
2176 zs_soft = (struct sun_serial *)(p + i);
2177 i += NUM_CHANNELS * sizeof (struct sun_serial);
2178 zs_ttys = (struct tty_struct *)(p + i);
2179 i += NUM_CHANNELS * sizeof (struct tty_struct);
2180 serial_table = (struct tty_struct **)(p + i);
2181 i += NUM_CHANNELS * sizeof (struct tty_struct *);
2182 serial_termios = (struct termios **)(p + i);
2183 i += NUM_CHANNELS * sizeof (struct termios *);
2184 serial_termios_locked = (struct termios **)(p + i);
2185 i += NUM_CHANNELS * sizeof (struct termios *);
2186 memset (p, 0, i);
2187 *memory_start = (((unsigned long)p) + i + 7) & ~7;
2189 /* Fill in rs_ops struct... */
2190 #ifdef CONFIG_SERIAL_CONSOLE
2191 sunserial_setinitfunc(memory_start, zs_console_init);
2192 #endif
2193 sunserial_setinitfunc(memory_start, zs_init);
2194 rs_ops.rs_kgdb_hook = zs_kgdb_hook;
2195 rs_ops.rs_change_mouse_baud = zs_change_mouse_baud;
2197 sunkbd_setinitfunc(memory_start, sun_kbd_init);
2198 kbd_ops.compute_shiftstate = sun_compute_shiftstate;
2199 kbd_ops.setledstate = sun_setledstate;
2200 kbd_ops.getledstate = sun_getledstate;
2201 kbd_ops.setkeycode = sun_setkeycode;
2202 kbd_ops.getkeycode = sun_getkeycode;
2203 #if defined(__sparc_v9__) && defined(CONFIG_PCI)
2204 sunkbd_install_keymaps(memory_start, sun_key_maps, sun_keymap_count,
2205 sun_func_buf, sun_func_table,
2206 sun_funcbufsize, sun_funcbufleft,
2207 sun_accent_table, sun_accent_table_size);
2208 #endif
2209 return 0;
2212 static inline void zs_prepare(void)
2214 int channel, chip;
2215 unsigned long flags;
2217 if (!NUM_SERIAL) return;
2219 save_and_cli(flags);
2221 /* Set up our interrupt linked list */
2222 zs_chain = &zs_soft[0];
2223 for(channel = 0; channel < NUM_CHANNELS - 1; channel++) {
2224 zs_soft[channel].zs_next = &zs_soft[channel + 1];
2225 zs_soft[channel].line = channel;
2227 zs_soft[channel].zs_next = 0;
2229 /* Initialize Softinfo */
2230 for(chip = 0; chip < NUM_SERIAL; chip++) {
2231 /* If we are doing kgdb over one of the channels on
2232 * chip zero, kgdb_channel will be set to 1 by the
2233 * zs_kgdb_hook() routine below.
2235 if(!zs_chips[chip]) {
2236 zs_chips[chip] = get_zs(chip);
2237 /* Two channels per chip */
2238 zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2239 zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2240 zs_soft[(chip*2)].kgdb_channel = 0;
2241 zs_soft[(chip*2)+1].kgdb_channel = 0;
2244 /* First, set up channel A on this chip. */
2245 channel = chip * 2;
2246 zs_soft[channel].zs_channel = zs_channels[channel];
2247 zs_soft[channel].change_needed = 0;
2248 zs_soft[channel].clk_divisor = 16;
2249 zs_soft[channel].cons_keyb = 0;
2250 zs_soft[channel].cons_mouse = 0;
2251 zs_soft[channel].channelA = 1;
2253 /* Now, channel B */
2254 channel++;
2255 zs_soft[channel].zs_channel = zs_channels[channel];
2256 zs_soft[channel].change_needed = 0;
2257 zs_soft[channel].clk_divisor = 16;
2258 zs_soft[channel].cons_keyb = 0;
2259 zs_soft[channel].cons_mouse = 0;
2260 zs_soft[channel].channelA = 0;
2263 restore_flags(flags);
2266 int __init zs_init(void)
2268 int channel, brg, i;
2269 unsigned long flags;
2270 struct sun_serial *info;
2271 char dummy;
2273 #if CONFIG_AP1000
2274 printk("not doing zs_init()\n");
2275 return 0;
2276 #endif
2278 /* Setup base handler, and timer table. */
2279 init_bh(SERIAL_BH, do_serial_bh);
2280 timer_table[RS_TIMER].fn = zs_timer;
2281 timer_table[RS_TIMER].expires = 0;
2283 show_serial_version();
2285 /* Initialize the tty_driver structure */
2286 /* SPARC: Not all of this is exactly right for us. */
2288 memset(&serial_driver, 0, sizeof(struct tty_driver));
2289 serial_driver.magic = TTY_DRIVER_MAGIC;
2290 serial_driver.driver_name = "serial";
2291 serial_driver.name = "ttyS";
2292 serial_driver.major = TTY_MAJOR;
2293 serial_driver.minor_start = 64;
2294 serial_driver.num = NUM_CHANNELS;
2295 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2296 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2297 serial_driver.init_termios = tty_std_termios;
2298 serial_driver.init_termios.c_cflag =
2299 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2300 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2301 serial_driver.refcount = &serial_refcount;
2302 serial_driver.table = serial_table;
2303 serial_driver.termios = serial_termios;
2304 serial_driver.termios_locked = serial_termios_locked;
2306 serial_driver.open = zs_open;
2307 serial_driver.close = zs_close;
2308 serial_driver.write = zs_write;
2309 serial_driver.flush_chars = zs_flush_chars;
2310 serial_driver.write_room = zs_write_room;
2311 serial_driver.chars_in_buffer = zs_chars_in_buffer;
2312 serial_driver.flush_buffer = zs_flush_buffer;
2313 serial_driver.ioctl = zs_ioctl;
2314 serial_driver.throttle = zs_throttle;
2315 serial_driver.unthrottle = zs_unthrottle;
2316 serial_driver.set_termios = zs_set_termios;
2317 serial_driver.stop = zs_stop;
2318 serial_driver.start = zs_start;
2319 serial_driver.hangup = zs_hangup;
2321 /* I'm too lazy, someone write versions of this for us. -DaveM */
2322 serial_driver.read_proc = 0;
2323 serial_driver.proc_entry = 0;
2326 * The callout device is just like normal device except for
2327 * major number and the subtype code.
2329 callout_driver = serial_driver;
2330 callout_driver.name = "cua";
2331 callout_driver.major = TTYAUX_MAJOR;
2332 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2334 if (tty_register_driver(&serial_driver))
2335 panic("Couldn't register serial driver\n");
2336 if (tty_register_driver(&callout_driver))
2337 panic("Couldn't register callout driver\n");
2339 save_flags(flags); cli();
2341 /* Initialize Softinfo */
2342 zs_prepare();
2344 /* Grab IRQ line before poking the chips so we do
2345 * not lose any interrupts.
2347 if (request_irq(zilog_irq, zs_interrupt,
2348 (SA_INTERRUPT | SA_STATIC_ALLOC),
2349 "Zilog8530", zs_chain))
2350 panic("Unable to attach zs intr\n");
2352 /* Initialize Hardware */
2353 for(channel = 0; channel < NUM_CHANNELS; channel++) {
2355 /* Hardware reset each chip */
2356 if (!(channel & 1)) {
2357 write_zsreg(zs_soft[channel].zs_channel, R9, FHWRES);
2358 ZSDELAY_LONG();
2359 dummy = read_zsreg(zs_soft[channel].zs_channel, R0);
2362 if(channel == KEYBOARD_LINE) {
2363 zs_soft[channel].cons_keyb = 1;
2364 zs_soft[channel].parity_mask = 0xff;
2365 zs_kbdchan = zs_soft[channel].zs_channel;
2367 write_zsreg(zs_soft[channel].zs_channel, R4,
2368 (PAR_EVEN | X16CLK | SB1));
2369 write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2370 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2371 write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2372 write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2373 write_zsreg(zs_soft[channel].zs_channel, R11,
2374 (TCBR | RCBR));
2375 zs_soft[channel].zs_baud = 1200;
2376 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2377 ZS_CLOCK/zs_soft[channel].clk_divisor);
2378 write_zsreg(zs_soft[channel].zs_channel, R12,
2379 (brg & 0xff));
2380 write_zsreg(zs_soft[channel].zs_channel, R13,
2381 ((brg >> 8) & 0xff));
2382 write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2384 /* Enable Rx/Tx, IRQs, and inform kbd driver */
2385 write_zsreg(zs_soft[channel].zs_channel, R14,
2386 (BRSRC | BRENAB));
2387 write_zsreg(zs_soft[channel].zs_channel, R3,
2388 (Rx8 | RxENAB));
2389 write_zsreg(zs_soft[channel].zs_channel, R5,
2390 (Tx8 | TxENAB | DTR | RTS));
2392 write_zsreg(zs_soft[channel].zs_channel, R15,
2393 (DCDIE | CTSIE | TxUIE | BRKIE));
2394 write_zsreg(zs_soft[channel].zs_channel, R0,
2395 RES_EXT_INT);
2396 write_zsreg(zs_soft[channel].zs_channel, R0,
2397 RES_EXT_INT);
2399 write_zsreg(zs_soft[channel].zs_channel, R1,
2400 (EXT_INT_ENAB | INT_ALL_Rx));
2401 write_zsreg(zs_soft[channel].zs_channel, R9,
2402 (NV | MIE));
2403 ZS_CLEARERR(zs_soft[channel].zs_channel);
2404 ZS_CLEARFIFO(zs_soft[channel].zs_channel);
2405 } else if(channel == MOUSE_LINE) {
2406 zs_soft[channel].cons_mouse = 1;
2407 zs_soft[channel].parity_mask = 0xff;
2408 zs_mousechan = zs_soft[channel].zs_channel;
2410 write_zsreg(zs_soft[channel].zs_channel, R4,
2411 (PAR_EVEN | X16CLK | SB1));
2412 write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2413 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2414 write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2415 write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2416 write_zsreg(zs_soft[channel].zs_channel, R11,
2417 (TCBR | RCBR));
2419 zs_soft[channel].zs_baud = 4800;
2420 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2421 ZS_CLOCK/zs_soft[channel].clk_divisor);
2422 write_zsreg(zs_soft[channel].zs_channel, R12,
2423 (brg & 0xff));
2424 write_zsreg(zs_soft[channel].zs_channel, R13,
2425 ((brg >> 8) & 0xff));
2426 write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2428 /* Enable Rx, IRQs, and inform mouse driver */
2429 write_zsreg(zs_soft[channel].zs_channel, R14,
2430 (BRSRC | BRENAB));
2431 write_zsreg(zs_soft[channel].zs_channel, R3,
2432 (Rx8 | RxENAB));
2433 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2435 write_zsreg(zs_soft[channel].zs_channel, R15,
2436 (DCDIE | CTSIE | TxUIE | BRKIE));
2437 write_zsreg(zs_soft[channel].zs_channel, R0,
2438 RES_EXT_INT);
2439 write_zsreg(zs_soft[channel].zs_channel, R0,
2440 RES_EXT_INT);
2442 write_zsreg(zs_soft[channel].zs_channel, R1,
2443 (EXT_INT_ENAB | INT_ALL_Rx));
2444 write_zsreg(zs_soft[channel].zs_channel, R9,
2445 (NV | MIE));
2447 sun_mouse_zsinit();
2448 } else if (zs_soft[channel].is_cons) {
2449 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2450 ZS_CLOCK/zs_soft[channel].clk_divisor);
2451 zscons_regs[12] = brg & 0xff;
2452 zscons_regs[13] = (brg >> 8) & 0xff;
2454 memcpy(zs_soft[channel].curregs, zscons_regs, sizeof(zscons_regs));
2455 load_zsregs(&zs_soft[channel], zscons_regs);
2457 ZS_CLEARERR(zs_soft[channel].zs_channel);
2458 ZS_CLEARFIFO(zs_soft[channel].zs_channel);
2459 } else if (zs_soft[channel].kgdb_channel) {
2460 /* If this is the kgdb line, enable interrupts because
2461 * we now want to receive the 'control-c' character
2462 * from the client attached to us asynchronously.
2464 zs_soft[channel].parity_mask = 0xff;
2465 kgdb_chaninit(&zs_soft[channel], 1,
2466 zs_soft[channel].zs_baud);
2467 } else {
2468 zs_soft[channel].parity_mask = 0xff;
2469 write_zsreg(zs_soft[channel].zs_channel, R4,
2470 (PAR_EVEN | X16CLK | SB1));
2471 write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2472 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2473 write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2474 write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2475 write_zsreg(zs_soft[channel].zs_channel, R11,
2476 (RCBR | TCBR));
2477 zs_soft[channel].zs_baud = 9600;
2478 brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2479 ZS_CLOCK/zs_soft[channel].clk_divisor);
2480 write_zsreg(zs_soft[channel].zs_channel, R12,
2481 (brg & 0xff));
2482 write_zsreg(zs_soft[channel].zs_channel, R13,
2483 ((brg >> 8) & 0xff));
2484 write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2485 write_zsreg(zs_soft[channel].zs_channel, R14,
2486 (BRSRC | BRENAB));
2487 write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2488 write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2489 write_zsreg(zs_soft[channel].zs_channel, R15, DCDIE);
2490 write_zsreg(zs_soft[channel].zs_channel, R9, NV | MIE);
2491 write_zsreg(zs_soft[channel].zs_channel, R0,
2492 RES_EXT_INT);
2493 write_zsreg(zs_soft[channel].zs_channel, R0,
2494 RES_EXT_INT);
2498 for (info = zs_chain, i=0; info; info = info->zs_next, i++) {
2499 info->magic = SERIAL_MAGIC;
2500 info->port = (long) info->zs_channel;
2501 info->line = i;
2502 info->tty = 0;
2503 info->irq = zilog_irq;
2504 info->custom_divisor = 16;
2505 info->close_delay = 50;
2506 info->closing_wait = 3000;
2507 info->x_char = 0;
2508 info->event = 0;
2509 info->count = 0;
2510 info->blocked_open = 0;
2511 info->tqueue.routine = do_softint;
2512 info->tqueue.data = info;
2513 info->tqueue_hangup.routine = do_serial_hangup;
2514 info->tqueue_hangup.data = info;
2515 info->callout_termios = callout_driver.init_termios;
2516 info->normal_termios = serial_driver.init_termios;
2517 init_waitqueue_head(&info->open_wait);
2518 init_waitqueue_head(&info->close_wait);
2519 printk("tty%02d at 0x%04x (irq = %s)", info->line,
2520 info->port, __irq_itoa(info->irq));
2521 printk(" is a Zilog8530\n");
2524 restore_flags(flags);
2526 keyboard_zsinit(kbd_put_char);
2527 return 0;
2530 /* This is called at boot time to prime the kgdb serial debugging
2531 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2532 * for /dev/ttyb which is determined in setup_arch() from the
2533 * boot command line flags.
2535 static void __init zs_kgdb_hook(int tty_num)
2537 int chip = 0;
2539 if(!zs_chips[chip]) {
2540 zs_chips[chip] = get_zs(chip);
2541 /* Two channels per chip */
2542 zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2543 zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2545 zs_soft[tty_num].zs_channel = zs_channels[tty_num];
2546 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2547 zs_soft[tty_num].change_needed = 0;
2548 zs_soft[tty_num].clk_divisor = 16;
2549 zs_soft[tty_num].zs_baud = 9600;
2550 zs_soft[tty_num].kgdb_channel = 1; /* This runs kgdb */
2551 zs_soft[tty_num ^ 1].kgdb_channel = 0; /* This does not */
2552 /* Turn on transmitter/receiver at 8-bits/char */
2553 kgdb_chaninit(&zs_soft[tty_num], 0, 9600);
2554 ZS_CLEARERR(zs_kgdbchan);
2555 ZS_CLEARFIFO(zs_kgdbchan);
2558 #ifdef CONFIG_SERIAL_CONSOLE
2560 /* This is for console output over ttya/ttyb */
2561 static void
2562 zs_console_putchar(struct sun_serial *info, char ch)
2564 int loops = ZS_PUT_CHAR_MAX_DELAY;
2565 unsigned long flags;
2567 if(!info->zs_channel)
2568 return;
2570 save_flags(flags); cli();
2571 zs_put_char(info->zs_channel, ch);
2572 while (!(read_zsreg(info->zs_channel, R1) & ALL_SNT) && --loops)
2573 udelay(5);
2574 restore_flags(flags);
2577 #ifdef SERIAL_CONSOLE_FAIR_OUTPUT
2579 * Fair output driver allows a process to speak.
2581 static void zs_fair_output(struct sun_serial *info)
2583 int left; /* Output no more than that */
2584 unsigned long flags;
2585 char c;
2587 if (info == 0) return;
2588 if (info->xmit_buf == 0) return;
2590 save_flags(flags); cli();
2591 left = info->xmit_cnt;
2592 while (left != 0) {
2593 c = info->xmit_buf[info->xmit_tail];
2594 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
2595 info->xmit_cnt--;
2596 restore_flags(flags);
2598 zs_console_putchar(info, c);
2600 cli();
2601 left = MIN(info->xmit_cnt, left-1);
2604 /* Last character is being transmitted now (hopefully). */
2605 info->zs_channel->control = RES_Tx_P;
2606 ZSDELAY();
2608 restore_flags(flags);
2609 return;
2611 #endif
2614 * zs_console_write is registered for printk.
2616 static void
2617 zs_console_write(struct console *con, const char *s, unsigned count)
2619 struct sun_serial *info;
2620 int i;
2622 info = zs_soft + con->index;
2624 for (i = 0; i < count; i++, s++) {
2625 if(*s == '\n')
2626 zs_console_putchar(info, '\r');
2627 zs_console_putchar(info, *s);
2629 #ifdef SERIAL_CONSOLE_FAIR_OUTPUT
2630 /* Comment this if you want to have a strict interrupt-driven output */
2631 zs_fair_output(info);
2632 #endif
2635 static int
2636 zs_console_wait_key(struct console *con)
2638 sleep_on(&keypress_wait);
2639 return 0;
2642 static kdev_t zs_console_device(struct console *con)
2644 return MKDEV(TTY_MAJOR, 64 + con->index);
2647 static int __init zs_console_setup(struct console *con, char *options)
2649 struct sun_serial *info;
2650 int i, brg, baud;
2652 info = zs_soft + con->index;
2653 info->is_cons = 1;
2655 printk("Console: ttyS%d (Zilog8530)\n", info->line);
2657 sunserial_console_termios(con);
2659 i = con->cflag & CBAUD;
2660 if (con->cflag & CBAUDEX) {
2661 i &= ~CBAUDEX;
2662 con->cflag &= ~CBAUDEX;
2664 baud = baud_table[i];
2665 info->zs_baud = baud;
2667 switch (con->cflag & CSIZE) {
2668 case CS5:
2669 zscons_regs[3] = Rx5 | RxENAB;
2670 zscons_regs[5] = Tx5 | TxENAB;
2671 info->parity_mask = 0x1f;
2672 break;
2673 case CS6:
2674 zscons_regs[3] = Rx6 | RxENAB;
2675 zscons_regs[5] = Tx6 | TxENAB;
2676 info->parity_mask = 0x3f;
2677 break;
2678 case CS7:
2679 zscons_regs[3] = Rx7 | RxENAB;
2680 zscons_regs[5] = Tx7 | TxENAB;
2681 info->parity_mask = 0x7f;
2682 break;
2683 default:
2684 case CS8:
2685 zscons_regs[3] = Rx8 | RxENAB;
2686 zscons_regs[5] = Tx8 | TxENAB;
2687 info->parity_mask = 0xff;
2688 break;
2690 zscons_regs[5] |= DTR;
2692 if (con->cflag & PARENB)
2693 zscons_regs[4] |= PAR_ENAB;
2694 if (!(con->cflag & PARODD))
2695 zscons_regs[4] |= PAR_EVEN;
2697 if (con->cflag & CSTOPB)
2698 zscons_regs[4] |= SB2;
2699 else
2700 zscons_regs[4] |= SB1;
2702 brg = BPS_TO_BRG(baud, ZS_CLOCK / info->clk_divisor);
2703 zscons_regs[12] = brg & 0xff;
2704 zscons_regs[13] = (brg >> 8) & 0xff;
2706 memcpy(info->curregs, zscons_regs, sizeof(zscons_regs));
2707 load_zsregs(info, zscons_regs);
2709 ZS_CLEARERR(info->zs_channel);
2710 ZS_CLEARFIFO(info->zs_channel);
2711 return 0;
2714 static struct console zs_console = {
2715 "ttyS",
2716 zs_console_write,
2717 NULL,
2718 zs_console_device,
2719 zs_console_wait_key,
2720 NULL,
2721 zs_console_setup,
2722 CON_PRINTBUFFER,
2725 NULL
2728 static int __init zs_console_init(void)
2730 extern int con_is_present(void);
2732 if (con_is_present())
2733 return 0;
2735 zs_console.index = serial_console - 1;
2736 register_console(&zs_console);
2737 return 0;
2740 #endif /* CONFIG_SERIAL_CONSOLE */