File: /usr/src/linux/drivers/sbus/char/zs.c

1     /* $Id: zs.c,v 1.66 2001/06/29 21:33:22 davem Exp $
2      * zs.c: Zilog serial port driver for the Sparc.
3      *
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>.
7      */
8     
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/bootmem.h>
27     #include <linux/sysrq.h>
28     
29     #include <asm/io.h>
30     #include <asm/irq.h>
31     #include <asm/oplib.h>
32     #include <asm/system.h>
33     #include <asm/uaccess.h>
34     #include <asm/bitops.h>
35     #include <asm/kdebug.h>
36     #include <asm/page.h>
37     #include <asm/pgtable.h>
38     
39     #include <asm/sbus.h>
40     #ifdef __sparc_v9__
41     #include <asm/fhc.h>
42     #endif
43     #ifdef CONFIG_PCI
44     #include <linux/pci.h>
45     #endif
46     
47     #include "sunserial.h"
48     #include "zs.h"
49     #include "sunkbd.h"
50     #include "sunmouse.h"
51     
52     static int num_serial = 2; /* sun4/sun4c/sun4m - Two chips on board. */
53     #define NUM_SERIAL num_serial
54     #define NUM_CHANNELS (NUM_SERIAL * 2)
55     
56     #define KEYBOARD_LINE 0x2
57     #define MOUSE_LINE    0x3
58     
59     /* On 32-bit sparcs we need to delay after register accesses
60      * to accomodate sun4 systems, but we do not need to flush writes.
61      * On 64-bit sparc we only need to flush single writes to ensure
62      * completion.
63      */
64     #ifndef __sparc_v9__
65     #define ZSDELAY()		udelay(5)
66     #define ZSDELAY_LONG()		udelay(20)
67     #define ZS_WSYNC(channel)	do { } while(0)
68     #else
69     #define ZSDELAY()
70     #define ZSDELAY_LONG()
71     #define ZS_WSYNC(__channel) \
72     	sbus_readb(&((__channel)->control))
73     #endif
74     
75     struct sun_zslayout **zs_chips;
76     struct sun_zschannel **zs_channels;
77     struct sun_zschannel *zs_mousechan;
78     struct sun_zschannel *zs_kbdchan;
79     struct sun_zschannel *zs_kgdbchan;
80     int *zs_nodes;
81     
82     struct sun_serial *zs_soft;
83     struct sun_serial *zs_chain;  /* IRQ servicing chain */
84     int zilog_irq;
85     
86     struct tty_struct *zs_ttys;
87     
88     /* Console hooks... */
89     #ifdef CONFIG_SERIAL_CONSOLE
90     static struct console zs_console;
91     static int zs_console_init(void);
92     
93     /*
94      * Define this to get the zs_fair_output() functionality.
95      */
96     #undef SERIAL_CONSOLE_FAIR_OUTPUT
97     #endif /* CONFIG_SERIAL_CONSOLE */
98     
99     static unsigned char kgdb_regs[16] = {
100     	0, 0, 0,                     /* write 0, 1, 2 */
101     	(Rx8 | RxENAB),              /* write 3 */
102     	(X16CLK | SB1 | PAR_EVEN),   /* write 4 */
103     	(DTR | Tx8 | TxENAB),        /* write 5 */
104     	0, 0, 0,                     /* write 6, 7, 8 */
105     	(NV),                        /* write 9 */
106     	(NRZ),                       /* write 10 */
107     	(TCBR | RCBR),               /* write 11 */
108     	0, 0,                        /* BRG time constant, write 12 + 13 */
109     	(BRSRC | BRENAB),            /* write 14 */
110     	(DCDIE)                      /* write 15 */
111     };
112     
113     static unsigned char zscons_regs[16] = {
114     	0,                           /* write 0 */
115     	(EXT_INT_ENAB | INT_ALL_Rx), /* write 1 */
116     	0,                           /* write 2 */
117     	(Rx8 | RxENAB),              /* write 3 */
118     	(X16CLK),                    /* write 4 */
119     	(DTR | Tx8 | TxENAB),        /* write 5 */
120     	0, 0, 0,                     /* write 6, 7, 8 */
121     	(NV | MIE),                  /* write 9 */
122     	(NRZ),                       /* write 10 */
123     	(TCBR | RCBR),               /* write 11 */
124     	0, 0,                        /* BRG time constant, write 12 + 13 */
125     	(BRSRC | BRENAB),            /* write 14 */
126     	(DCDIE | CTSIE | TxUIE | BRKIE) /* write 15 */
127     };
128     
129     #define ZS_CLOCK         4915200   /* Zilog input clock rate */
130     
131     DECLARE_TASK_QUEUE(tq_serial);
132     
133     static struct tty_driver serial_driver, callout_driver;
134     static int serial_refcount;
135     
136     /* serial subtype definitions */
137     #define SERIAL_TYPE_NORMAL	1
138     #define SERIAL_TYPE_CALLOUT	2
139       
140     /* number of characters left in xmit buffer before we ask for more */
141     #define WAKEUP_CHARS 256
142     
143     #define SERIAL_DO_RESTART
144     
145     /* Debugging... DEBUG_INTR is bad to use when one of the zs
146      * lines is your console ;(
147      */
148     #undef SERIAL_DEBUG_INTR
149     #undef SERIAL_DEBUG_OPEN
150     #undef SERIAL_DEBUG_FLOW
151     
152     #define RS_STROBE_TIME 10
153     #define RS_ISR_PASS_LIMIT 256
154     
155     #define _INLINE_ inline
156     
157     int zs_init(void);
158     static void zs_kgdb_hook(int);
159     
160     static void change_speed(struct sun_serial *info);
161     
162     static struct tty_struct **serial_table;
163     static struct termios **serial_termios;
164     static struct termios **serial_termios_locked;
165     
166     #ifndef MIN
167     #define MIN(a,b)	((a) < (b) ? (a) : (b))
168     #endif
169     
170     #undef ZS_LOG
171     #ifdef ZS_LOG
172     struct zs_logent {
173     	u8 reg, val;
174     	u8 write, __pad;
175     #define REGIRQ	0xff
176     #define REGDATA	0xfe
177     #define REGCTRL	0xfd
178     };
179     struct zs_logent zslog[32];
180     int zs_curlog;
181     #define ZSLOG(__reg, __val, __write) \
182     do{	int index = zs_curlog; \
183     	zslog[index].reg = (__reg); \
184     	zslog[index].val = (__val); \
185     	zslog[index].write = (__write); \
186     	zs_curlog = (index + 1) & (32 - 1); \
187     }while(0)
188     int zs_dumplog(char *buffer)
189     {
190     	int len = 0;
191     	int i;
192     
193     	for (i = 0; i < 32; i++) {
194     		u8 reg, val, write;
195     
196     		reg = zslog[i].reg;
197     		val = zslog[i].val;
198     		write = zslog[i].write;
199     		len += sprintf(buffer + len,
200     			       "ZSLOG[%2d]: reg %2x val %2x %s\n",
201     			       i, reg, val, write ? "write" : "read");
202     	}
203     	len += sprintf(buffer + len, "ZS current log index %d\n",
204     		       zs_curlog);
205     	return len;
206     }
207     #else
208     #define ZSLOG(x,y,z)	do { } while (0)
209     #endif
210     
211     /*
212      * tmp_buf is used as a temporary buffer by serial_write.  We need to
213      * lock it in case the memcpy_fromfs blocks while swapping in a page,
214      * and some other program tries to do a serial write at the same time.
215      * Since the lock will only come under contention when the system is
216      * swapping and available memory is low, it makes sense to share one
217      * buffer across all the serial ports, since it significantly saves
218      * memory if large numbers of serial ports are open.
219      */
220     static unsigned char *tmp_buf = 0;
221     static DECLARE_MUTEX(tmp_buf_sem);
222     
223     static inline int serial_paranoia_check(struct sun_serial *info,
224     					dev_t device, const char *routine)
225     {
226     #ifdef SERIAL_PARANOIA_CHECK
227     	static const char *badmagic =
228     		"Warning: bad magic number for serial struct (%d, %d) in %s\n";
229     	static const char *badinfo =
230     		"Warning: null sun_serial for (%d, %d) in %s\n";
231     
232     	if (!info) {
233     		printk(badinfo, MAJOR(device), MINOR(device), routine);
234     		return 1;
235     	}
236     	if (info->magic != SERIAL_MAGIC) {
237     		printk(badmagic, MAJOR(device), MINOR(device), routine);
238     		return 1;
239     	}
240     #endif
241     	return 0;
242     }
243     
244     /* This is used to figure out the divisor speeds and the timeouts. */
245     static int baud_table[] = {
246     	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
247     	9600, 19200, 38400, 76800, 0
248     };
249     
250     /* Reading and writing Zilog8530 registers.  The delays are to make this
251      * driver work on the Sun4 which needs a settling delay after each chip
252      * register access, other machines handle this in hardware via auxiliary
253      * flip-flops which implement the settle time we do in software.
254      */
255     static unsigned char read_zsreg(struct sun_zschannel *channel,
256     				unsigned char reg)
257     {
258     	unsigned char retval;
259     
260     	sbus_writeb(reg, &channel->control);
261     	ZSDELAY();
262     	retval = sbus_readb(&channel->control);
263     	ZSDELAY();
264     	ZSLOG(reg, retval, 0);
265     	return retval;
266     }
267     
268     static void write_zsreg(struct sun_zschannel *channel,
269     			unsigned char reg, unsigned char value)
270     {
271     	ZSLOG(reg, value, 1);
272     	sbus_writeb(reg, &channel->control);
273     	ZSDELAY();
274     	sbus_writeb(value, &channel->control);
275     	ZSDELAY();
276     }
277     
278     static void load_zsregs(struct sun_serial *info, unsigned char *regs)
279     {
280     	struct sun_zschannel *channel = info->zs_channel;
281     	unsigned long flags;
282     	unsigned char stat;
283     	int i;
284     
285     	for (i = 0; i < 1000; i++) {
286     		stat = read_zsreg(channel, R1);
287     		if (stat & ALL_SNT)
288     			break;
289     		udelay(100);
290     	}
291     	write_zsreg(channel, R3, 0);
292     	ZS_CLEARSTAT(channel);
293     	ZS_CLEARERR(channel);
294     	ZS_CLEARFIFO(channel);
295     
296     	/* Load 'em up */
297     	save_flags(flags); cli();
298     	if (info->channelA)
299     		write_zsreg(channel, R9, CHRA);
300     	else
301     		write_zsreg(channel, R9, CHRB);
302     	ZSDELAY_LONG();
303     	write_zsreg(channel, R4, regs[R4]);
304     	write_zsreg(channel, R3, regs[R3] & ~RxENAB);
305     	write_zsreg(channel, R5, regs[R5] & ~TxENAB);
306     	write_zsreg(channel, R9, regs[R9] & ~MIE);
307     	write_zsreg(channel, R10, regs[R10]);
308     	write_zsreg(channel, R11, regs[R11]);
309     	write_zsreg(channel, R12, regs[R12]);
310     	write_zsreg(channel, R13, regs[R13]);
311     	write_zsreg(channel, R14, regs[R14] & ~BRENAB);
312     	write_zsreg(channel, R14, regs[R14]);
313     	write_zsreg(channel, R14, (regs[R14] & ~SNRZI) | BRENAB);
314     	write_zsreg(channel, R3, regs[R3]);
315     	write_zsreg(channel, R5, regs[R5]);
316     	write_zsreg(channel, R15, regs[R15]);
317     	write_zsreg(channel, R0, RES_EXT_INT);
318     	write_zsreg(channel, R0, ERR_RES);
319     	write_zsreg(channel, R1, regs[R1]);
320     	write_zsreg(channel, R9, regs[R9]);
321     	restore_flags(flags);
322     }
323     
324     #define ZS_PUT_CHAR_MAX_DELAY	2000	/* 10 ms */
325     
326     static void zs_put_char(struct sun_zschannel *channel, char ch)
327     {
328     	int loops = ZS_PUT_CHAR_MAX_DELAY;
329     
330     	/* Do not change this to use ZSDELAY as this is
331     	 * a timed polling loop and on sparc64 ZSDELAY
332     	 * is a nop.  -DaveM
333     	 */
334     	do {
335     		u8 val = sbus_readb(&channel->control);
336     		ZSLOG(REGCTRL, val, 0);
337     		if (val & Tx_BUF_EMP)
338     			break;
339     		udelay(5);
340     	} while (--loops);
341     
342     	sbus_writeb(ch, &channel->data);
343     	ZSDELAY();
344     	ZS_WSYNC(channel);
345     	ZSLOG(REGDATA, ch, 1);
346     }
347     
348     /* Sets or clears DTR/RTS on the requested line */
349     static void zs_rtsdtr(struct sun_serial *ss, int set)
350     {
351     	unsigned long flags;
352     
353     	save_flags(flags); cli();
354     	if(set) {
355     		ss->curregs[5] |= (RTS | DTR);
356     		write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
357     	} else {
358     		ss->curregs[5] &= ~(RTS | DTR);
359     		write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
360     	}
361     	restore_flags(flags);
362     	return;
363     }
364     
365     static void kgdb_chaninit(struct sun_serial *ss, int intson, int bps)
366     {
367     	int brg;
368     
369     	if(intson) {
370     		kgdb_regs[R1] = INT_ALL_Rx;
371     		kgdb_regs[R9] |= MIE;
372     	} else {
373     		kgdb_regs[R1] = 0;
374     		kgdb_regs[R9] &= ~MIE;
375     	}
376     	brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
377     	kgdb_regs[R12] = (brg & 255);
378     	kgdb_regs[R13] = ((brg >> 8) & 255);
379     	load_zsregs(ss, kgdb_regs);
380     }
381     
382     /*
383      * ------------------------------------------------------------
384      * zs_stop() and zs_start()
385      *
386      * This routines are called before setting or resetting tty->stopped.
387      * They enable or disable transmitter interrupts, as necessary.
388      * ------------------------------------------------------------
389      */
390     static void zs_stop(struct tty_struct *tty)
391     {
392     	struct sun_serial *info = (struct sun_serial *) tty->driver_data;
393     	unsigned long flags;
394     
395     	if (serial_paranoia_check(info, tty->device, "zs_stop"))
396     		return;
397     	
398     	save_flags(flags); cli();
399     	if (info->curregs[5] & TxENAB) {
400     		info->curregs[5] &= ~TxENAB;
401     		write_zsreg(info->zs_channel, 5, info->curregs[5]);
402     	}
403     	restore_flags(flags);
404     }
405     
406     static void zs_start(struct tty_struct *tty)
407     {
408     	struct sun_serial *info = (struct sun_serial *) tty->driver_data;
409     	unsigned long flags;
410     	
411     	if (serial_paranoia_check(info, tty->device, "zs_start"))
412     		return;
413     	
414     	save_flags(flags); cli();
415     	if (info->xmit_cnt && info->xmit_buf && !(info->curregs[5] & TxENAB)) {
416     		info->curregs[5] |= TxENAB;
417     		write_zsreg(info->zs_channel, 5, info->curregs[5]);
418     	}
419     	restore_flags(flags);
420     }
421     
422     /* Drop into either the boot monitor or kadb upon receiving a break
423      * from keyboard/console input.
424      */
425     void batten_down_hatches(void)
426     {
427     	if (!stop_a_enabled)
428     		return;
429     	/* If we are doing kadb, we call the debugger
430     	 * else we just drop into the boot monitor.
431     	 * Note that we must flush the user windows
432     	 * first before giving up control.
433     	 */
434     	printk("\n");
435     	flush_user_windows();
436     #ifndef __sparc_v9__
437     	if((((unsigned long)linux_dbvec)>=DEBUG_FIRSTVADDR) &&
438     	   (((unsigned long)linux_dbvec)<=DEBUG_LASTVADDR))
439     		sp_enter_debugger();
440     	else
441     #endif
442     		prom_cmdline();
443     
444     	/* XXX We want to notify the keyboard driver that all
445     	 * XXX keys are in the up state or else weird things
446     	 * XXX happen...
447     	 */
448     
449     	return;
450     }
451     
452     
453     /*
454      * ----------------------------------------------------------------------
455      *
456      * Here starts the interrupt handling routines.  All of the following
457      * subroutines are declared as inline and are folded into
458      * zs_interrupt().  They were separated out for readability's sake.
459      *
460      * Note: zs_interrupt() is a "fast" interrupt, which means that it
461      * runs with interrupts turned off.  People who may want to modify
462      * zs_interrupt() should try to keep the interrupt handler as fast as
463      * possible.  After you are done making modifications, it is not a bad
464      * idea to do:
465      * 
466      * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
467      *
468      * and look at the resulting assemble code in serial.s.
469      *
470      * 				- Ted Ts'o (tytso@mit.edu), 7-Mar-93
471      * -----------------------------------------------------------------------
472      */
473     
474     /*
475      * This routine is used by the interrupt handler to schedule
476      * processing in the software interrupt portion of the driver.
477      */
478     static void zs_sched_event(struct sun_serial *info, int event)
479     {
480     	info->event |= 1 << event;
481     	queue_task(&info->tqueue, &tq_serial);
482     	mark_bh(SERIAL_BH);
483     }
484     
485     #ifndef __sparc_v9__
486     extern void breakpoint(void);  /* For the KGDB frame character */
487     #endif
488     
489     static void receive_chars(struct sun_serial *info, struct pt_regs *regs)
490     {
491     	struct tty_struct *tty = info->tty;
492     	unsigned char ch, stat;
493     	int do_queue_task = 1;
494     
495     	do {
496     		ch = sbus_readb(&info->zs_channel->data);
497     		ZSLOG(REGDATA, ch, 0);
498     		ch &= info->parity_mask;
499     		ZSDELAY();
500     
501     		/* If this is the console keyboard, we need to handle
502     		 * L1-A's here.
503     		 */
504     		if(info->cons_keyb) {
505     			if(ch == SUNKBD_RESET) {
506     				l1a_state.kbd_id = 1;
507     				l1a_state.l1_down = 0;
508     			} else if(l1a_state.kbd_id) {
509     				l1a_state.kbd_id = 0;
510     			} else if(ch == SUNKBD_L1) {
511     				l1a_state.l1_down = 1;
512     			} else if(ch == (SUNKBD_L1|SUNKBD_UP)) {
513     				l1a_state.l1_down = 0;
514     			} else if(ch == SUNKBD_A && l1a_state.l1_down) {
515     				/* whee... */
516     				batten_down_hatches();
517     				/* Continue execution... */
518     				l1a_state.l1_down = 0;
519     				l1a_state.kbd_id = 0;
520     				return;
521     			}
522     			sunkbd_inchar(ch, regs);
523     			do_queue_task = 0;
524     			goto next_char;
525     		}
526     		if(info->cons_mouse) {
527     			sun_mouse_inbyte(ch, 0);
528     			do_queue_task = 0;
529     			goto next_char;
530     		}
531     		if(info->is_cons) {
532     			if(ch == 0) {
533     				/* whee, break received */
534     				batten_down_hatches();
535     				/* Continue execution... */
536     				return;
537     			}
538     			/* It is a 'keyboard interrupt' ;-) */
539     			wake_up(&keypress_wait);
540     		}
541     #ifndef __sparc_v9__
542     		/* Look for kgdb 'stop' character, consult the gdb
543     		 * documentation for remote target debugging and
544     		 * arch/sparc/kernel/sparc-stub.c to see how all this works.
545     		 */
546     		if((info->kgdb_channel) && (ch =='\003')) {
547     			breakpoint();
548     			return;
549     		}
550     #endif
551     		if(!tty)
552     			return;
553     
554     		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
555     			break;
556     
557     		tty->flip.count++;
558     		*tty->flip.flag_buf_ptr++ = 0;
559     		*tty->flip.char_buf_ptr++ = ch;
560     
561     	next_char:
562     		/* Check if we have another character... */
563     		stat = sbus_readb(&info->zs_channel->control);
564     		ZSDELAY();
565     		ZSLOG(REGCTRL, stat, 0);
566     		if (!(stat & Rx_CH_AV))
567     			break;
568     
569     		/* ... and see if it is clean. */
570     		stat = read_zsreg(info->zs_channel, R1);
571     	} while (!(stat & (PAR_ERR | Rx_OVR | CRC_ERR)));
572     
573     	if (do_queue_task != 0)
574     		queue_task(&tty->flip.tqueue, &tq_timer);
575     }
576     
577     static void transmit_chars(struct sun_serial *info)
578     {
579     	struct tty_struct *tty = info->tty;
580     
581     	if (info->x_char) {
582     		/* Send next char */
583     		zs_put_char(info->zs_channel, info->x_char);
584     		info->x_char = 0;
585     		return;
586     	}
587     
588     	if((info->xmit_cnt <= 0) || (tty != 0 && tty->stopped)) {
589     		/* That's peculiar... */
590     		sbus_writeb(RES_Tx_P, &info->zs_channel->control);
591     		ZSDELAY();
592     		ZS_WSYNC(info->zs_channel);
593     		ZSLOG(REGCTRL, RES_Tx_P, 1);
594     		return;
595     	}
596     
597     	/* Send char */
598     	zs_put_char(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
599     	info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
600     	info->xmit_cnt--;
601     
602     	if (info->xmit_cnt < WAKEUP_CHARS)
603     		zs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
604     
605     	if(info->xmit_cnt <= 0) {
606     		sbus_writeb(RES_Tx_P, &info->zs_channel->control);
607     		ZSDELAY();
608     		ZS_WSYNC(info->zs_channel);
609     		ZSLOG(REGCTRL, RES_Tx_P, 1);
610     	}
611     }
612     
613     static void status_handle(struct sun_serial *info)
614     {
615     	unsigned char status;
616     
617     	/* Get status from Read Register 0 */
618     	status = sbus_readb(&info->zs_channel->control);
619     	ZSDELAY();
620     	ZSLOG(REGCTRL, status, 0);
621     	/* Clear status condition... */
622     	sbus_writeb(RES_EXT_INT, &info->zs_channel->control);
623     	ZSDELAY();
624     	ZS_WSYNC(info->zs_channel);
625     	ZSLOG(REGCTRL, RES_EXT_INT, 1);
626     #if 0
627     	if(status & DCD) {
628     		if((info->tty->termios->c_cflag & CRTSCTS) &&
629     		   ((info->curregs[3] & AUTO_ENAB)==0)) {
630     			info->curregs[3] |= AUTO_ENAB;
631     			write_zsreg(info->zs_channel, 3, info->curregs[3]);
632     		}
633     	} else {
634     		if((info->curregs[3] & AUTO_ENAB)) {
635     			info->curregs[3] &= ~AUTO_ENAB;
636     			write_zsreg(info->zs_channel, 3, info->curregs[3]);
637     		}
638     	}
639     #endif
640     	/* Whee, if this is console input and this is a
641     	 * 'break asserted' status change interrupt, call
642     	 * the boot prom.
643     	 */
644     	if(status & BRK_ABRT) {
645     		if (info->break_abort)
646     			batten_down_hatches();
647     		if (info->cons_mouse)
648     			sun_mouse_inbyte(0, 1);
649     	}
650     
651     	/* XXX Whee, put in a buffer somewhere, the status information
652     	 * XXX whee whee whee... Where does the information go...
653     	 */
654     	return;
655     }
656     
657     static void special_receive(struct sun_serial *info)
658     {
659     	struct tty_struct *tty = info->tty;
660     	unsigned char ch, stat;
661     
662     	stat = read_zsreg(info->zs_channel, R1);
663     	if (stat & (PAR_ERR | Rx_OVR | CRC_ERR)) {
664     		ch = sbus_readb(&info->zs_channel->data);
665     		ZSDELAY();
666     		ZSLOG(REGDATA, ch, 0);
667     	}
668     
669     	if (!tty)
670     		goto clear;
671     
672     	if (tty->flip.count >= TTY_FLIPBUF_SIZE)
673     		goto done;
674     
675     	tty->flip.count++;
676     	if(stat & PAR_ERR)
677     		*tty->flip.flag_buf_ptr++ = TTY_PARITY;
678     	else if(stat & Rx_OVR)
679     		*tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
680     	else if(stat & CRC_ERR)
681     		*tty->flip.flag_buf_ptr++ = TTY_FRAME;
682     
683     done:
684     	queue_task(&tty->flip.tqueue, &tq_timer);
685     clear:
686     	sbus_writeb(ERR_RES, &info->zs_channel->control);
687     	ZSDELAY();
688     	ZS_WSYNC(info->zs_channel);
689     	ZSLOG(REGCTRL, ERR_RES, 1);
690     }
691     
692     
693     /*
694      * This is the serial driver's generic interrupt routine
695      */
696     void zs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
697     {
698     	struct sun_serial *info;
699     	unsigned char zs_intreg;
700     	int i;
701     
702     	info = (struct sun_serial *)dev_id;
703     	ZSLOG(REGIRQ, 0, 0);
704     	for (i = 0; i < NUM_SERIAL; i++) {
705     		zs_intreg = read_zsreg(info->zs_next->zs_channel, 2);
706     		zs_intreg &= STATUS_MASK;
707     
708     		/* NOTE: The read register 2, which holds the irq status,
709     		 *       does so for both channels on each chip.  Although
710     		 *       the status value itself must be read from the B
711     		 *       channel and is only valid when read from channel B.
712     		 *       When read from channel A, read register 2 contains
713     		 *       the value written to write register 2.
714     		 */
715     
716     		/* Channel A -- /dev/ttya or /dev/kbd, could be the console */
717     		if (zs_intreg == CHA_Rx_AVAIL) {
718     			receive_chars(info, regs);
719     			return;
720     		}
721     		if(zs_intreg == CHA_Tx_EMPTY) {
722     			transmit_chars(info);
723     			return;
724     		}
725     		if (zs_intreg == CHA_EXT_STAT) {
726     			status_handle(info);
727     			return;
728     		}
729     		if (zs_intreg == CHA_SPECIAL) {
730     			special_receive(info);
731     			return;
732     		}
733     
734     		/* Channel B -- /dev/ttyb or /dev/mouse, could be the console */
735     		if(zs_intreg == CHB_Rx_AVAIL) {
736     			receive_chars(info->zs_next, regs);
737     			return;
738     		}
739     		if(zs_intreg == CHB_Tx_EMPTY) {
740     			transmit_chars(info->zs_next);
741     			return;
742     		}
743     		if (zs_intreg == CHB_EXT_STAT) {
744     			status_handle(info->zs_next);
745     			return;
746     		}
747     
748     		/* NOTE: The default value for the IRQ status in read register
749     		 *       2 in channel B is CHB_SPECIAL, so we need to look at
750     		 *       read register 3 in channel A to check if this is a
751     		 *       real interrupt, or just the default value.
752     		 *       Yes... broken hardware...
753     		 */
754     
755     		zs_intreg = read_zsreg(info->zs_channel, 3);
756     		if (zs_intreg & CHBRxIP) {
757     			special_receive(info->zs_next);
758     			return;
759     		}
760     		info = info->zs_next->zs_next;
761     	}
762     }
763     
764     /*
765      * -------------------------------------------------------------------
766      * Here ends the serial interrupt routines.
767      * -------------------------------------------------------------------
768      */
769     
770     /*
771      * This routine is used to handle the "bottom half" processing for the
772      * serial driver, known also the "software interrupt" processing.
773      * This processing is done at the kernel interrupt level, after the
774      * zs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
775      * is where time-consuming activities which can not be done in the
776      * interrupt driver proper are done; the interrupt driver schedules
777      * them using zs_sched_event(), and they get done here.
778      */
779     static void do_serial_bh(void)
780     {
781     	run_task_queue(&tq_serial);
782     }
783     
784     static void do_softint(void *private_)
785     {
786     	struct sun_serial	*info = (struct sun_serial *) private_;
787     	struct tty_struct	*tty;
788     	
789     	tty = info->tty;
790     	if (!tty)
791     		return;
792     
793     	if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
794     		if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
795     		    tty->ldisc.write_wakeup)
796     			(tty->ldisc.write_wakeup)(tty);
797     		wake_up_interruptible(&tty->write_wait);
798     	}
799     }
800     
801     /*
802      * This routine is called from the scheduler tqueue when the interrupt
803      * routine has signalled that a hangup has occurred.  The path of
804      * hangup processing is:
805      *
806      * 	serial interrupt routine -> (scheduler tqueue) ->
807      * 	do_serial_hangup() -> tty->hangup() -> zs_hangup()
808      * 
809      */
810     static void do_serial_hangup(void *private_)
811     {
812     	struct sun_serial	*info = (struct sun_serial *) private_;
813     	struct tty_struct	*tty;
814     	
815     	tty = info->tty;
816     	if (!tty)
817     		return;
818     #ifdef SERIAL_DEBUG_OPEN
819     	printk("do_serial_hangup<%p: tty-%d\n",
820     		__builtin_return_address(0), info->line);
821     #endif
822     
823     	tty_hangup(tty);
824     }
825     
826     static int startup(struct sun_serial * info)
827     {
828     	unsigned long flags;
829     
830     	if (info->flags & ZILOG_INITIALIZED)
831     		return 0;
832     
833     	if (!info->xmit_buf) {
834     		info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
835     		if (!info->xmit_buf)
836     			return -ENOMEM;
837     	}
838     
839     	save_flags(flags); cli();
840     
841     #ifdef SERIAL_DEBUG_OPEN
842     	printk("Starting up tty-%d (irq %d)...\n", info->line, info->irq);
843     #endif
844     
845     	/*
846     	 * Clear the FIFO buffers and disable them
847     	 * (they will be reenabled in change_speed())
848     	 */
849     	ZS_CLEARFIFO(info->zs_channel);
850     	info->xmit_fifo_size = 1;
851     
852     	/*
853     	 * Clear the interrupt registers.
854     	 */
855     	sbus_writeb(ERR_RES, &info->zs_channel->control);
856     	ZSDELAY();
857     	ZS_WSYNC(info->zs_channel);
858     	ZSLOG(REGCTRL, ERR_RES, 1);
859     
860     	sbus_writeb(RES_H_IUS, &info->zs_channel->control);
861     	ZSDELAY();
862     	ZS_WSYNC(info->zs_channel);
863     	ZSLOG(REGCTRL, RES_H_IUS, 1);
864     
865     	/*
866     	 * Now, initialize the Zilog
867     	 */
868     	zs_rtsdtr(info, 1);
869     
870     	/*
871     	 * Finally, enable sequencing and interrupts
872     	 */
873     	info->curregs[1] |= (info->curregs[1] & ~(RxINT_MASK)) |
874     				(EXT_INT_ENAB | INT_ALL_Rx);
875     	info->curregs[3] |= (RxENAB | Rx8);
876     	/* We enable Tx interrupts as needed. */
877     	info->curregs[5] |= (TxENAB | Tx8);
878     	info->curregs[9] |= (NV | MIE);
879     	write_zsreg(info->zs_channel, 3, info->curregs[3]);
880     	write_zsreg(info->zs_channel, 5, info->curregs[5]);
881     	write_zsreg(info->zs_channel, 9, info->curregs[9]);
882     	
883     	/*
884     	 * And clear the interrupt registers again for luck.
885     	 */
886     	sbus_writeb(ERR_RES, &info->zs_channel->control);
887     	ZSDELAY();
888     	ZS_WSYNC(info->zs_channel);
889     	ZSLOG(REGCTRL, ERR_RES, 1);
890     
891     	sbus_writeb(RES_H_IUS, &info->zs_channel->control);
892     	ZSDELAY();
893     	ZS_WSYNC(info->zs_channel);
894     	ZSLOG(REGCTRL, RES_H_IUS, 1);
895     
896     	if (info->tty)
897     		clear_bit(TTY_IO_ERROR, &info->tty->flags);
898     	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
899     
900     	/*
901     	 * and set the speed of the serial port
902     	 */
903     	change_speed(info);
904     
905     	info->flags |= ZILOG_INITIALIZED;
906     	restore_flags(flags);
907     	return 0;
908     }
909     
910     /*
911      * This routine will shutdown a serial port; interrupts are disabled, and
912      * DTR is dropped if the hangup on close termio flag is on.
913      */
914     static void shutdown(struct sun_serial * info)
915     {
916     	unsigned long	flags;
917     
918     	if (!(info->flags & ZILOG_INITIALIZED))
919     		return;
920     
921     #ifdef SERIAL_DEBUG_OPEN
922     	printk("Shutting down serial port %d (irq %d)....", info->line,
923     	       info->irq);
924     #endif
925     	
926     	save_flags(flags); cli(); /* Disable interrupts */
927     	
928     	if (info->xmit_buf) {
929     		free_page((unsigned long) info->xmit_buf);
930     		info->xmit_buf = 0;
931     	}
932     
933     	if (info->tty)
934     		set_bit(TTY_IO_ERROR, &info->tty->flags);
935     	
936     	info->flags &= ~ZILOG_INITIALIZED;
937     	restore_flags(flags);
938     }
939     
940     /*
941      * This routine is called to set the UART divisor registers to match
942      * the specified baud rate for a serial port.
943      */
944     static void change_speed(struct sun_serial *info)
945     {
946     	unsigned cflag;
947     	int	quot = 0;
948     	int	i;
949     	int	brg;
950     
951     	if (!info->tty || !info->tty->termios)
952     		return;
953     	cflag = info->tty->termios->c_cflag;
954     	if (!info->port)
955     		return;
956     	i = cflag & CBAUD;
957     	if (cflag & CBAUDEX) {
958     		i &= ~CBAUDEX;
959     		if (i != 5)
960     			info->tty->termios->c_cflag &= ~CBAUDEX;
961     		else
962     			i = 16;
963     	}
964     	if (i == 15) {
965     		if ((info->flags & ZILOG_SPD_MASK) == ZILOG_SPD_HI)
966     			i += 1;
967     		if ((info->flags & ZILOG_SPD_MASK) == ZILOG_SPD_CUST)
968     			quot = info->custom_divisor;
969     	}
970     	if (quot) {
971     		info->zs_baud = info->baud_base / quot;
972     		info->clk_divisor = 16;
973     
974     		info->curregs[4] = X16CLK;
975     		info->curregs[11] = TCBR | RCBR;
976     		brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
977     		info->curregs[12] = (brg & 255);
978     		info->curregs[13] = ((brg >> 8) & 255);
979     		info->curregs[14] = BRSRC | BRENAB;
980     		zs_rtsdtr(info, 1);
981     	} else if (baud_table[i]) {
982     		info->zs_baud = baud_table[i];
983     		info->clk_divisor = 16;
984     
985     		info->curregs[4] = X16CLK;
986     		info->curregs[11] = TCBR | RCBR;
987     		brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
988     		info->curregs[12] = (brg & 255);
989     		info->curregs[13] = ((brg >> 8) & 255);
990     		info->curregs[14] = BRSRC | BRENAB;
991     		zs_rtsdtr(info, 1);
992     	} else {
993     		zs_rtsdtr(info, 0);
994     		return;
995     	}
996     
997     	/* byte size and parity */
998     	switch (cflag & CSIZE) {
999     	case CS5:
1000     		info->curregs[3] &= ~(RxN_MASK);
1001     		info->curregs[3] |= Rx5;
1002     		info->curregs[5] &= ~(TxN_MASK);
1003     		info->curregs[5] |= Tx5;
1004     		info->parity_mask = 0x1f;
1005     		break;
1006     	case CS6:
1007     		info->curregs[3] &= ~(RxN_MASK);
1008     		info->curregs[3] |= Rx6;
1009     		info->curregs[5] &= ~(TxN_MASK);
1010     		info->curregs[5] |= Tx6;
1011     		info->parity_mask = 0x3f;
1012     		break;
1013     	case CS7:
1014     		info->curregs[3] &= ~(RxN_MASK);
1015     		info->curregs[3] |= Rx7;
1016     		info->curregs[5] &= ~(TxN_MASK);
1017     		info->curregs[5] |= Tx7;
1018     		info->parity_mask = 0x7f;
1019     		break;
1020     	case CS8:
1021     	default: /* defaults to 8 bits */
1022     		info->curregs[3] &= ~(RxN_MASK);
1023     		info->curregs[3] |= Rx8;
1024     		info->curregs[5] &= ~(TxN_MASK);
1025     		info->curregs[5] |= Tx8;
1026     		info->parity_mask = 0xff;
1027     		break;
1028     	}
1029     	info->curregs[4] &= ~(0x0c);
1030     	if (cflag & CSTOPB) {
1031     		info->curregs[4] |= SB2;
1032     	} else {
1033     		info->curregs[4] |= SB1;
1034     	}
1035     	if (cflag & PARENB) {
1036     		info->curregs[4] |= PAR_ENAB;
1037     	} else {
1038     		info->curregs[4] &= ~PAR_ENAB;
1039     	}
1040     	if (!(cflag & PARODD)) {
1041     		info->curregs[4] |= PAR_EVEN;
1042     	} else {
1043     		info->curregs[4] &= ~PAR_EVEN;
1044     	}
1045     
1046     	/* Load up the new values */
1047     	load_zsregs(info, info->curregs);
1048     
1049     	return;
1050     }
1051     
1052     /* This is for mouse/keyboard output.
1053      * XXX mouse output??? can we send it commands??? XXX
1054      */
1055     static void kbd_put_char(unsigned char ch)
1056     {
1057     	struct sun_zschannel *chan = zs_kbdchan;
1058     	unsigned long flags;
1059     
1060     	if(!chan)
1061     		return;
1062     
1063     	save_flags(flags); cli();
1064     	zs_put_char(chan, ch);
1065     	restore_flags(flags);
1066     }
1067     
1068     void mouse_put_char(char ch)
1069     {
1070     	struct sun_zschannel *chan = zs_mousechan;
1071     	unsigned long flags;
1072     
1073     	if(!chan)
1074     		return;
1075     
1076     	save_flags(flags); cli();
1077     	zs_put_char(chan, ch);
1078     	restore_flags(flags);
1079     }
1080     
1081     /* These are for receiving and sending characters under the kgdb
1082      * source level kernel debugger.
1083      */
1084     void putDebugChar(char kgdb_char)
1085     {
1086     	struct sun_zschannel *chan = zs_kgdbchan;
1087     
1088     	while((sbus_readb(&chan->control) & Tx_BUF_EMP)==0)
1089     		udelay(5);
1090     	sbus_writeb(kgdb_char, &chan->data);
1091     	ZS_WSYNC(chan);
1092     	ZSLOG(REGDATA, kgdb_char, 1);
1093     }
1094     
1095     char getDebugChar(void)
1096     {
1097     	struct sun_zschannel *chan = zs_kgdbchan;
1098     	u8 val;
1099     
1100     	do {
1101     		val = sbus_readb(&chan->control);
1102     		ZSLOG(REGCTRL, val, 0);
1103     		udelay(5);
1104     	} while ((val & Rx_CH_AV) == 0);
1105     
1106     	val = sbus_readb(&chan->data);
1107     	ZSLOG(REGDATA, val, 0);
1108     	return val;
1109     }
1110     
1111     static void zs_flush_chars(struct tty_struct *tty)
1112     {
1113     	struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1114     	unsigned long flags;
1115     
1116     	if (serial_paranoia_check(info, tty->device, "zs_flush_chars"))
1117     		return;
1118     
1119     	save_flags(flags); cli();
1120     	if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1121     	    !info->xmit_buf)
1122     		goto out;
1123     
1124     	/* Enable transmitter */
1125     	info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1126     	write_zsreg(info->zs_channel, 1, info->curregs[1]);
1127     	info->curregs[5] |= TxENAB;
1128     	write_zsreg(info->zs_channel, 5, info->curregs[5]);
1129     
1130     	/*
1131     	 * Send a first (bootstrapping) character. A best solution is
1132     	 * to call transmit_chars() here which handles output in a
1133     	 * generic way. Current transmit_chars() not only transmits,
1134     	 * but resets interrupts also what we do not desire here.
1135     	 * XXX Discuss with David.
1136     	 */
1137     	zs_put_char(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
1138     	info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1139     	info->xmit_cnt--;
1140     
1141     out:
1142     	restore_flags(flags);
1143     }
1144     
1145     static int zs_write(struct tty_struct * tty, int from_user,
1146     		    const unsigned char *buf, int count)
1147     {
1148     	struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1149     	unsigned long flags;
1150     	int c, total = 0;
1151     
1152     	if (serial_paranoia_check(info, tty->device, "zs_write"))
1153     		return 0;
1154     
1155     	if (!info || !info->xmit_buf || !tmp_buf)
1156     		return 0;
1157     
1158     	save_flags(flags);
1159     	if (from_user) {
1160     		down(&tmp_buf_sem);
1161     		while (1) {
1162     			c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1163     					   SERIAL_XMIT_SIZE - info->xmit_head));
1164     			if (c <= 0)
1165     				break;
1166     			c -= copy_from_user(tmp_buf, buf, c);
1167     			if (!c) {
1168     				if (!total)
1169     					total = -EFAULT;
1170     				break;
1171     			}
1172     			cli();
1173     			c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1174     				       SERIAL_XMIT_SIZE - info->xmit_head));
1175     			memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1176     			info->xmit_head = ((info->xmit_head + c) &
1177     					   (SERIAL_XMIT_SIZE - 1));
1178     			info->xmit_cnt += c;
1179     			restore_flags(flags);
1180     
1181     			buf += c;
1182     			count -= c;
1183     			total += c;
1184     		}
1185     		up(&tmp_buf_sem);
1186     	} else {
1187     		while (1) {
1188     			cli();		
1189     			c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1190     					   SERIAL_XMIT_SIZE - info->xmit_head));
1191     			if (c <= 0) {
1192     				restore_flags(flags);
1193     				break;
1194     			}
1195     			memcpy(info->xmit_buf + info->xmit_head, buf, c);
1196     			info->xmit_head = ((info->xmit_head + c) &
1197     					   (SERIAL_XMIT_SIZE - 1));
1198     			info->xmit_cnt += c;
1199     			restore_flags(flags);
1200     			buf += c;
1201     			count -= c;
1202     			total += c;
1203     		}
1204     	}
1205     
1206     	cli();		
1207     	if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
1208     		/* Enable transmitter */
1209     		info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1210     		write_zsreg(info->zs_channel, 1, info->curregs[1]);
1211     		info->curregs[5] |= TxENAB;
1212     		write_zsreg(info->zs_channel, 5, info->curregs[5]);
1213     #if 1
1214     		zs_put_char(info->zs_channel,
1215     			    info->xmit_buf[info->xmit_tail++]);
1216     		info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1217     		info->xmit_cnt--;
1218     #endif
1219     	}
1220     
1221     	restore_flags(flags);
1222     	return total;
1223     }
1224     
1225     static int zs_write_room(struct tty_struct *tty)
1226     {
1227     	struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1228     	int ret;
1229     
1230     	if (serial_paranoia_check(info, tty->device, "zs_write_room"))
1231     		return 0;
1232     	ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1233     	if (ret < 0)
1234     		ret = 0;
1235     	return ret;
1236     }
1237     
1238     static int zs_chars_in_buffer(struct tty_struct *tty)
1239     {
1240     	struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1241     
1242     	if (serial_paranoia_check(info, tty->device, "zs_chars_in_buffer"))
1243     		return 0;
1244     	return info->xmit_cnt;
1245     }
1246     
1247     static void zs_flush_buffer(struct tty_struct *tty)
1248     {
1249     	struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1250     
1251     	if (serial_paranoia_check(info, tty->device, "zs_flush_buffer"))
1252     		return;
1253     	cli();
1254     	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1255     	sti();
1256     	wake_up_interruptible(&tty->write_wait);
1257     	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1258     	    tty->ldisc.write_wakeup)
1259     		(tty->ldisc.write_wakeup)(tty);
1260     }
1261     
1262     /*
1263      * ------------------------------------------------------------
1264      * zs_throttle()
1265      * 
1266      * This routine is called by the upper-layer tty layer to signal that
1267      * incoming characters should be throttled.
1268      * ------------------------------------------------------------
1269      */
1270     static void zs_throttle(struct tty_struct * tty)
1271     {
1272     	struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1273     #ifdef SERIAL_DEBUG_THROTTLE
1274     	char	buf[64];
1275     	
1276     	printk("throttle %s: %d....\n", _tty_name(tty, buf),
1277     	       tty->ldisc.chars_in_buffer(tty));
1278     #endif
1279     
1280     	if (serial_paranoia_check(info, tty->device, "zs_throttle"))
1281     		return;
1282     	
1283     	if (I_IXOFF(tty))
1284     		info->x_char = STOP_CHAR(tty);
1285     
1286     	/* Turn off RTS line */
1287     	cli();
1288     	info->curregs[5] &= ~RTS;
1289     	write_zsreg(info->zs_channel, 5, info->curregs[5]);
1290     	sti();
1291     }
1292     
1293     static void zs_unthrottle(struct tty_struct * tty)
1294     {
1295     	struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1296     #ifdef SERIAL_DEBUG_THROTTLE
1297     	char	buf[64];
1298     	
1299     	printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1300     	       tty->ldisc.chars_in_buffer(tty));
1301     #endif
1302     
1303     	if (serial_paranoia_check(info, tty->device, "zs_unthrottle"))
1304     		return;
1305     	
1306     	if (I_IXOFF(tty)) {
1307     		if (info->x_char)
1308     			info->x_char = 0;
1309     		else
1310     			info->x_char = START_CHAR(tty);
1311     	}
1312     
1313     	/* Assert RTS line */
1314     	cli();
1315     	info->curregs[5] |= RTS;
1316     	write_zsreg(info->zs_channel, 5, info->curregs[5]);
1317     	sti();
1318     }
1319     
1320     /*
1321      * ------------------------------------------------------------
1322      * zs_ioctl() and friends
1323      * ------------------------------------------------------------
1324      */
1325     
1326     static int get_serial_info(struct sun_serial * info,
1327     			   struct serial_struct * retinfo)
1328     {
1329     	struct serial_struct tmp;
1330       
1331     	if (!retinfo)
1332     		return -EFAULT;
1333     	memset(&tmp, 0, sizeof(tmp));
1334     	tmp.type = info->type;
1335     	tmp.line = info->line;
1336     	tmp.port = info->port;
1337     	tmp.irq = info->irq;
1338     	tmp.flags = info->flags;
1339     	tmp.baud_base = info->baud_base;
1340     	tmp.close_delay = info->close_delay;
1341     	tmp.closing_wait = info->closing_wait;
1342     	tmp.custom_divisor = info->custom_divisor;
1343     	if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1344     		return -EFAULT;
1345     	return 0;
1346     }
1347     
1348     static int set_serial_info(struct sun_serial * info,
1349     			   struct serial_struct * new_info)
1350     {
1351     	struct serial_struct new_serial;
1352     	struct sun_serial old_info;
1353     	int retval = 0;
1354     
1355     	if (!new_info || copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1356     		return -EFAULT;
1357     	old_info = *info;
1358     
1359     	if (!capable(CAP_SYS_ADMIN)) {
1360     		if ((new_serial.baud_base != info->baud_base) ||
1361     		    (new_serial.type != info->type) ||
1362     		    (new_serial.close_delay != info->close_delay) ||
1363     		    ((new_serial.flags & ~ZILOG_USR_MASK) !=
1364     		     (info->flags & ~ZILOG_USR_MASK)))
1365     			return -EPERM;
1366     		info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1367     			       (new_serial.flags & ZILOG_USR_MASK));
1368     		info->custom_divisor = new_serial.custom_divisor;
1369     		goto check_and_exit;
1370     	}
1371     
1372     	if(new_serial.baud_base < 9600)
1373     		return -EINVAL;
1374     
1375     	if (info->count > 1)
1376     		return -EBUSY;
1377     
1378     	/*
1379     	 * OK, past this point, all the error checking has been done.
1380     	 * At this point, we start making changes.....
1381     	 */
1382     
1383     	info->baud_base = new_serial.baud_base;
1384     	info->flags = ((info->flags & ~ZILOG_FLAGS) |
1385     			(new_serial.flags & ZILOG_FLAGS));
1386     	info->custom_divisor = new_serial.custom_divisor;
1387     	info->type = new_serial.type;
1388     	info->close_delay = new_serial.close_delay;
1389     	info->closing_wait = new_serial.closing_wait;
1390     
1391     check_and_exit:
1392     	retval = startup(info);
1393     	return retval;
1394     }
1395     
1396     /*
1397      * get_lsr_info - get line status register info
1398      *
1399      * Purpose: Let user call ioctl() to get info when the UART physically
1400      * 	    is emptied.  On bus types like RS485, the transmitter must
1401      * 	    release the bus after transmitting. This must be done when
1402      * 	    the transmit shift register is empty, not be done when the
1403      * 	    transmit holding register is empty.  This functionality
1404      * 	    allows an RS485 driver to be written in user space. 
1405      */
1406     static int get_lsr_info(struct sun_serial * info, unsigned int *value)
1407     {
1408     	unsigned char status;
1409     
1410     	cli();
1411     	status = sbus_readb(&info->zs_channel->control);
1412     	ZSDELAY();
1413     	ZSLOG(REGCTRL, status, 0);
1414     	sti();
1415     	if (put_user(status, value))
1416     		return -EFAULT;
1417     	return 0;
1418     }
1419     
1420     static int get_modem_info(struct sun_serial * info, unsigned int *value)
1421     {
1422     	unsigned char status;
1423     	unsigned int result;
1424     
1425     	cli();
1426     	status = sbus_readb(&info->zs_channel->control);
1427     	ZSDELAY();
1428     	ZSLOG(REGCTRL, status, 0);
1429     	sti();
1430     	result =  ((info->curregs[5] & RTS) ? TIOCM_RTS : 0)
1431     		| ((info->curregs[5] & DTR) ? TIOCM_DTR : 0)
1432     		| ((status  & DCD) ? TIOCM_CAR : 0)
1433     		| ((status  & SYNC) ? TIOCM_DSR : 0)
1434     		| ((status  & CTS) ? TIOCM_CTS : 0);
1435     	if (put_user(result, value))
1436     		return -EFAULT;
1437     	return 0;
1438     }
1439     
1440     static int set_modem_info(struct sun_serial * info, unsigned int cmd,
1441     			  unsigned int *value)
1442     {
1443     	unsigned int arg;
1444     
1445     	if (get_user(arg, value))
1446     		return -EFAULT;
1447     	switch (cmd) {
1448     	case TIOCMBIS: 
1449     		if (arg & TIOCM_RTS)
1450     			info->curregs[5] |= RTS;
1451     		if (arg & TIOCM_DTR)
1452     			info->curregs[5] |= DTR;
1453     		break;
1454     	case TIOCMBIC:
1455     		if (arg & TIOCM_RTS)
1456     			info->curregs[5] &= ~RTS;
1457     		if (arg & TIOCM_DTR)
1458     			info->curregs[5] &= ~DTR;
1459     		break;
1460     	case TIOCMSET:
1461     		info->curregs[5] = ((info->curregs[5] & ~(RTS | DTR))
1462     			     | ((arg & TIOCM_RTS) ? RTS : 0)
1463     			     | ((arg & TIOCM_DTR) ? DTR : 0));
1464     		break;
1465     	default:
1466     		return -EINVAL;
1467     	}
1468     	cli();
1469     	write_zsreg(info->zs_channel, 5, info->curregs[5]);
1470     	sti();
1471     	return 0;
1472     }
1473     
1474     /*
1475      * This routine sends a break character out the serial port.
1476      */
1477     static void send_break(	struct sun_serial * info, int duration)
1478     {
1479     	if (!info->port)
1480     		return;
1481     	current->state = TASK_INTERRUPTIBLE;
1482     	cli();
1483     	write_zsreg(info->zs_channel, 5, (info->curregs[5] | SND_BRK));
1484     	schedule_timeout(duration);
1485     	write_zsreg(info->zs_channel, 5, info->curregs[5]);
1486     	sti();
1487     }
1488     
1489     static int zs_ioctl(struct tty_struct *tty, struct file * file,
1490     		    unsigned int cmd, unsigned long arg)
1491     {
1492     	struct sun_serial * info = (struct sun_serial *) tty->driver_data;
1493     	int retval;
1494     
1495     	if (serial_paranoia_check(info, tty->device, "zs_ioctl"))
1496     		return -ENODEV;
1497     
1498     	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1499     	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1500     	    (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1501     		if (tty->flags & (1 << TTY_IO_ERROR))
1502     		    return -EIO;
1503     	}
1504     	
1505     	switch (cmd) {
1506     		case TCSBRK:	/* SVID version: non-zero arg --> no break */
1507     			retval = tty_check_change(tty);
1508     			if (retval)
1509     				return retval;
1510     			tty_wait_until_sent(tty, 0);
1511     			if (!arg)
1512     				send_break(info, HZ/4);	/* 1/4 second */
1513     			return 0;
1514     		case TCSBRKP:	/* support for POSIX tcsendbreak() */
1515     			retval = tty_check_change(tty);
1516     			if (retval)
1517     				return retval;
1518     			tty_wait_until_sent(tty, 0);
1519     			send_break(info, arg ? arg*(HZ/10) : HZ/4);
1520     			return 0;
1521     		case TIOCGSOFTCAR:
1522     			if (put_user(C_CLOCAL(tty) ? 1 : 0,
1523     				     (unsigned long *) arg))
1524     				return -EFAULT;
1525     			return 0;
1526     		case TIOCSSOFTCAR:
1527     			if (get_user(arg, (unsigned long *) arg))
1528     				return -EFAULT;
1529     			tty->termios->c_cflag =
1530     				((tty->termios->c_cflag & ~CLOCAL) |
1531     				 (arg ? CLOCAL : 0));
1532     			return 0;
1533     		case TIOCMGET:
1534     			return get_modem_info(info, (unsigned int *) arg);
1535     		case TIOCMBIS:
1536     		case TIOCMBIC:
1537     		case TIOCMSET:
1538     			return set_modem_info(info, cmd, (unsigned int *) arg);
1539     		case TIOCGSERIAL:
1540     			return get_serial_info(info,
1541     					       (struct serial_struct *) arg);
1542     		case TIOCSSERIAL:
1543     			return set_serial_info(info,
1544     					       (struct serial_struct *) arg);
1545     		case TIOCSERGETLSR: /* Get line status register */
1546     			return get_lsr_info(info, (unsigned int *) arg);
1547     
1548     		case TIOCSERGSTRUCT:
1549     			if (copy_to_user((struct sun_serial *) arg,
1550     				    info, sizeof(struct sun_serial)))
1551     				return -EFAULT;
1552     			return 0;
1553     			
1554     		default:
1555     			return -ENOIOCTLCMD;
1556     		}
1557     	return 0;
1558     }
1559     
1560     static void zs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1561     {
1562     	struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1563     
1564     	if (tty->termios->c_cflag == old_termios->c_cflag)
1565     		return;
1566     
1567     	change_speed(info);
1568     
1569     	if ((old_termios->c_cflag & CRTSCTS) &&
1570     	    !(tty->termios->c_cflag & CRTSCTS)) {
1571     		tty->hw_stopped = 0;
1572     		zs_start(tty);
1573     	}
1574     }
1575     
1576     /*
1577      * ------------------------------------------------------------
1578      * zs_close()
1579      * 
1580      * This routine is called when the serial port gets closed.  First, we
1581      * wait for the last remaining data to be sent.  Then, we unlink its
1582      * ZILOG structure from the interrupt chain if necessary, and we free
1583      * that IRQ if nothing is left in the chain.
1584      * ------------------------------------------------------------
1585      */
1586     static void zs_close(struct tty_struct *tty, struct file * filp)
1587     {
1588     	struct sun_serial * info = (struct sun_serial *) tty->driver_data;
1589     	unsigned long flags;
1590     
1591     	if (!info || serial_paranoia_check(info, tty->device, "zs_close"))
1592     		return;
1593     	
1594     	save_flags(flags); cli();
1595     	
1596     	if (tty_hung_up_p(filp)) {
1597     		restore_flags(flags);
1598     		return;
1599     	}
1600     	
1601     #ifdef SERIAL_DEBUG_OPEN
1602     	printk("zs_close tty-%d, count = %d\n", info->line, info->count);
1603     #endif
1604     	if ((tty->count == 1) && (info->count != 1)) {
1605     		/*
1606     		 * Uh, oh.  tty->count is 1, which means that the tty
1607     		 * structure will be freed.  Info->count should always
1608     		 * be one in these conditions.  If it's greater than
1609     		 * one, we've got real problems, since it means the
1610     		 * serial port won't be shutdown.
1611     		 */
1612     		printk("zs_close: bad serial port count; tty->count is 1, "
1613     		       "info->count is %d\n", info->count);
1614     		info->count = 1;
1615     	}
1616     	if (--info->count < 0) {
1617     		printk("zs_close: bad serial port count for ttys%d: %d\n",
1618     		       info->line, info->count);
1619     		info->count = 0;
1620     	}
1621     	if (info->count) {
1622     		restore_flags(flags);
1623     		return;
1624     	}
1625     	info->flags |= ZILOG_CLOSING;
1626     	/*
1627     	 * Save the termios structure, since this port may have
1628     	 * separate termios for callout and dialin.
1629     	 */
1630     	if (info->flags & ZILOG_NORMAL_ACTIVE)
1631     		info->normal_termios = *tty->termios;
1632     	if (info->flags & ZILOG_CALLOUT_ACTIVE)
1633     		info->callout_termios = *tty->termios;
1634     	/*
1635     	 * Now we wait for the transmit buffer to clear; and we notify 
1636     	 * the line discipline to only process XON/XOFF characters.
1637     	 */
1638     	tty->closing = 1;
1639     	if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1640     		tty_wait_until_sent(tty, info->closing_wait);
1641     	/*
1642     	 * At this point we stop accepting input.  To do this, we
1643     	 * disable the receive line status interrupts, and tell the
1644     	 * interrupt driver to stop checking the data ready bit in the
1645     	 * line status register.
1646     	 */
1647     	/** if (!info->iscons) ... **/
1648     	info->curregs[3] &= ~RxENAB;
1649     	write_zsreg(info->zs_channel, 3, info->curregs[3]);
1650     	info->curregs[1] &= ~(RxINT_MASK);
1651     	write_zsreg(info->zs_channel, 1, info->curregs[1]);
1652     	ZS_CLEARFIFO(info->zs_channel);
1653     
1654     	shutdown(info);
1655     	if (tty->driver.flush_buffer)
1656     		tty->driver.flush_buffer(tty);
1657     	if (tty->ldisc.flush_buffer)
1658     		tty->ldisc.flush_buffer(tty);
1659     	tty->closing = 0;
1660     	info->event = 0;
1661     	info->tty = 0;
1662     	if (tty->ldisc.num != ldiscs[N_TTY].num) {
1663     		if (tty->ldisc.close)
1664     			(tty->ldisc.close)(tty);
1665     		tty->ldisc = ldiscs[N_TTY];
1666     		tty->termios->c_line = N_TTY;
1667     		if (tty->ldisc.open)
1668     			(tty->ldisc.open)(tty);
1669     	}
1670     	if (info->blocked_open) {
1671     		if (info->close_delay) {
1672     			current->state = TASK_INTERRUPTIBLE;
1673     			schedule_timeout(info->close_delay);
1674     		}
1675     		wake_up_interruptible(&info->open_wait);
1676     	}
1677     	info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1678     			 ZILOG_CLOSING);
1679     	wake_up_interruptible(&info->close_wait);
1680     #ifdef SERIAL_DEBUG_OPEN
1681     	printk("zs_close tty-%d exiting, count = %d\n", info->line, info->count);
1682     #endif
1683     	restore_flags(flags);
1684     }
1685     
1686     /*
1687      * zs_hangup() --- called by tty_hangup() when a hangup is signaled.
1688      */
1689     void zs_hangup(struct tty_struct *tty)
1690     {
1691     	struct sun_serial * info = (struct sun_serial *) tty->driver_data;
1692     
1693     	if (serial_paranoia_check(info, tty->device, "zs_hangup"))
1694     		return;
1695     
1696     	if (info->is_cons)
1697     		return;
1698     
1699     #ifdef SERIAL_DEBUG_OPEN
1700     	printk("zs_hangup<%p: tty-%d, count = %d bye\n",
1701     		__builtin_return_address(0), info->line, info->count);
1702     #endif
1703     
1704     	zs_flush_buffer(tty);
1705     	shutdown(info);
1706     	info->event = 0;
1707     	info->count = 0;
1708     	info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1709     	info->tty = 0;
1710     	wake_up_interruptible(&info->open_wait);
1711     }
1712     
1713     /*
1714      * ------------------------------------------------------------
1715      * zs_open() and friends
1716      * ------------------------------------------------------------
1717      */
1718     static int block_til_ready(struct tty_struct *tty, struct file * filp,
1719     			   struct sun_serial *info)
1720     {
1721     	DECLARE_WAITQUEUE(wait, current);
1722     	int retval, do_clocal = 0;
1723     	unsigned char r0;
1724     
1725     	/*
1726     	 * If the device is in the middle of being closed, then block
1727     	 * until it's done, and then try again.
1728     	 */
1729     	if (info->flags & ZILOG_CLOSING) {
1730     		interruptible_sleep_on(&info->close_wait);
1731     #ifdef SERIAL_DO_RESTART
1732     		if (info->flags & ZILOG_HUP_NOTIFY)
1733     			return -EAGAIN;
1734     		else
1735     			return -ERESTARTSYS;
1736     #else
1737     		return -EAGAIN;
1738     #endif
1739     	}
1740     
1741     	/*
1742     	 * If this is a callout device, then just make sure the normal
1743     	 * device isn't being used.
1744     	 */
1745     	if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1746     		if (info->flags & ZILOG_NORMAL_ACTIVE)
1747     			return -EBUSY;
1748     		if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1749     		    (info->flags & ZILOG_SESSION_LOCKOUT) &&
1750     		    (info->session != current->session))
1751     		    return -EBUSY;
1752     		if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1753     		    (info->flags & ZILOG_PGRP_LOCKOUT) &&
1754     		    (info->pgrp != current->pgrp))
1755     		    return -EBUSY;
1756     		info->flags |= ZILOG_CALLOUT_ACTIVE;
1757     		return 0;
1758     	}
1759     	
1760     	/*
1761     	 * If non-blocking mode is set, or the port is not enabled,
1762     	 * then make the check up front and then exit.
1763     	 */
1764     	if ((filp->f_flags & O_NONBLOCK) ||
1765     	    (tty->flags & (1 << TTY_IO_ERROR))) {
1766     		if (info->flags & ZILOG_CALLOUT_ACTIVE)
1767     			return -EBUSY;
1768     		info->flags |= ZILOG_NORMAL_ACTIVE;
1769     		return 0;
1770     	}
1771     
1772     	if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1773     		if (info->normal_termios.c_cflag & CLOCAL)
1774     			do_clocal = 1;
1775     	} else {
1776     		if (tty->termios->c_cflag & CLOCAL)
1777     			do_clocal = 1;
1778     	}
1779     	
1780     	/*
1781     	 * Block waiting for the carrier detect and the line to become
1782     	 * free (i.e., not in use by the callout).  While we are in
1783     	 * this loop, info->count is dropped by one, so that
1784     	 * zs_close() knows when to free things.  We restore it upon
1785     	 * exit, either normal or abnormal.
1786     	 */
1787     	retval = 0;
1788     	add_wait_queue(&info->open_wait, &wait);
1789     #ifdef SERIAL_DEBUG_OPEN
1790     	printk("block_til_ready before block: ttys%d, count = %d\n",
1791     	       info->line, info->count);
1792     #endif
1793     	cli();
1794     	if(!tty_hung_up_p(filp))
1795     		info->count--;
1796     	sti();
1797     	info->blocked_open++;
1798     	while (1) {
1799     		cli();
1800     		if (!(info->flags & ZILOG_CALLOUT_ACTIVE))
1801     			zs_rtsdtr(info, 1);
1802     		sti();
1803     		set_current_state(TASK_INTERRUPTIBLE);
1804     		if (tty_hung_up_p(filp) ||
1805     		    !(info->flags & ZILOG_INITIALIZED)) {
1806     #ifdef SERIAL_DEBUG_OPEN
1807     			printk("block_til_ready hup-ed: ttys%d, count = %d\n",
1808     				info->line, info->count);
1809     #endif
1810     #ifdef SERIAL_DO_RESTART
1811     			if (info->flags & ZILOG_HUP_NOTIFY)
1812     				retval = -EAGAIN;
1813     			else
1814     				retval = -ERESTARTSYS;	
1815     #else
1816     			retval = -EAGAIN;
1817     #endif
1818     			break;
1819     		}
1820     
1821     		cli();
1822     		r0 = read_zsreg(info->zs_channel, R0);
1823     		sti();
1824     		if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1825     		    !(info->flags & ZILOG_CLOSING) &&
1826     		    (do_clocal || (DCD & r0)))
1827     			break;
1828     		if (signal_pending(current)) {
1829     			retval = -ERESTARTSYS;
1830     			break;
1831     		}
1832     #ifdef SERIAL_DEBUG_OPEN
1833     		printk("block_til_ready blocking: ttys%d, count = %d\n",
1834     		       info->line, info->count);
1835     #endif
1836     		schedule();
1837     	}
1838     	current->state = TASK_RUNNING;
1839     	remove_wait_queue(&info->open_wait, &wait);
1840     	if (!tty_hung_up_p(filp))
1841     		info->count++;
1842     	info->blocked_open--;
1843     #ifdef SERIAL_DEBUG_OPEN
1844     	printk("block_til_ready after blocking: ttys%d, count = %d\n",
1845     	       info->line, info->count);
1846     #endif
1847     	if (retval)
1848     		return retval;
1849     	info->flags |= ZILOG_NORMAL_ACTIVE;
1850     	return 0;
1851     }	
1852     
1853     /*
1854      * This routine is called whenever a serial port is opened.  It
1855      * enables interrupts for a serial port, linking in its ZILOG structure into
1856      * the IRQ chain.   It also performs the serial-specific
1857      * initialization for the tty structure.
1858      */
1859     int zs_open(struct tty_struct *tty, struct file * filp)
1860     {
1861     	struct sun_serial *info;
1862     	int retval, line;
1863     
1864     	line = MINOR(tty->device) - tty->driver.minor_start;
1865     
1866     	/* The zilog lines for the mouse/keyboard must be
1867     	 * opened using their respective drivers.
1868     	 */
1869     	if ((line < 0) || (line >= NUM_CHANNELS))
1870     		return -ENODEV;
1871     	if((line == KEYBOARD_LINE) || (line == MOUSE_LINE))
1872     		return -ENODEV;
1873     	info = zs_soft + line;
1874     	/* Is the kgdb running over this line? */
1875     	if (info->kgdb_channel)
1876     		return -ENODEV;
1877     	if (serial_paranoia_check(info, tty->device, "zs_open"))
1878     		return -ENODEV;
1879     #ifdef SERIAL_DEBUG_OPEN
1880     	printk("zs_open %s%d, count = %d\n", tty->driver.name, info->line,
1881     	       info->count);
1882     #endif
1883     	if (info->tty != 0 && info->tty != tty) {
1884     		/* Never happen? */
1885     		printk("zs_open %s%d, tty overwrite.\n", tty->driver.name, info->line);
1886     		return -EBUSY;
1887     	}
1888     
1889     	if (!tmp_buf) {
1890     		unsigned long page = get_free_page(GFP_KERNEL);
1891     		if (!page)
1892     			return -ENOMEM;
1893     		if (tmp_buf)
1894     			free_page(page);
1895     		else
1896     			tmp_buf = (unsigned char *) page;
1897     	}
1898     
1899     	info->count++;
1900     	tty->driver_data = info;
1901     	info->tty = tty;
1902     
1903     	/*
1904     	 * Start up serial port
1905     	 */
1906     	retval = startup(info);
1907     	if (retval)
1908     		return retval;
1909     
1910     	retval = block_til_ready(tty, filp, info);
1911     	if (retval) {
1912     #ifdef SERIAL_DEBUG_OPEN
1913     		printk("zs_open returning after block_til_ready with %d\n",
1914     		       retval);
1915     #endif
1916     		return retval;
1917     	}
1918     
1919     	if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1920     		if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1921     			*tty->termios = info->normal_termios;
1922     		else 
1923     			*tty->termios = info->callout_termios;
1924     		change_speed(info);
1925     	}
1926     
1927     #ifdef CONFIG_SERIAL_CONSOLE
1928     	if (zs_console.cflag && zs_console.index == line) {
1929     		tty->termios->c_cflag = zs_console.cflag;
1930     		zs_console.cflag = 0;
1931     		change_speed(info);
1932     	}
1933     #endif
1934     
1935     	info->session = current->session;
1936     	info->pgrp = current->pgrp;
1937     
1938     #ifdef SERIAL_DEBUG_OPEN
1939     	printk("zs_open ttys%d successful...", info->line);
1940     #endif
1941     	return 0;
1942     }
1943     
1944     /* Finally, routines used to initialize the serial driver. */
1945     
1946     static void show_serial_version(void)
1947     {
1948     	char *revision = "$Revision: 1.66 $";
1949     	char *version, *p;
1950     
1951     	version = strchr(revision, ' ');
1952     	p = strchr(++version, ' ');
1953     	*p = '\0';
1954     	printk("Sparc Zilog8530 serial driver version %s\n", version);
1955     	*p = ' ';
1956     }
1957     
1958     /* Probe the PROM for the request zs chip number.
1959      *
1960      * Note: The Sun Voyager shows two addresses and two intr for it's
1961      *       Zilogs, what the second does, I don't know. It does work
1962      *       with using only the first number of each property.  Also
1963      *       we have a special version for sun4u.
1964      */
1965     #ifdef __sparc_v9__
1966     static struct sun_zslayout * __init get_zs(int chip)
1967     {
1968     	unsigned int vaddr[2] = { 0, 0 };
1969     	unsigned long mapped_addr = 0;
1970     	int busnode, seen, zsnode, sun4u_ino;
1971     	static int irq = 0;
1972     
1973     	if(chip < 0 || chip >= NUM_SERIAL) {
1974     		prom_printf("get_zs bogon zs chip number");
1975     		prom_halt();
1976     	}
1977     
1978     	if(central_bus)
1979     		busnode = central_bus->child->prom_node;
1980     	else
1981     		busnode = prom_searchsiblings(prom_getchild(prom_root_node), "sbus");
1982     	if(busnode == 0 || busnode == -1) {
1983     		prom_printf("get_zs: no zs bus to search");
1984     		prom_halt();
1985     	}
1986     	zsnode = prom_getchild(busnode);
1987     	seen = 0;
1988     	while(zsnode) {
1989     		int slave;
1990     
1991     		zsnode = prom_searchsiblings(zsnode, "zs");
1992     		slave = prom_getintdefault(zsnode, "slave", -1);
1993     		if((slave == chip) || (seen == chip)) {
1994     			int len = prom_getproperty(zsnode, "address",
1995     						   (void *) vaddr, sizeof(vaddr));
1996     
1997     			if(len == -1 || central_bus != NULL) {
1998     				struct sbus_bus *sbus = NULL;
1999     				struct sbus_dev *sdev = NULL;
2000     
2001     				/* "address" property is not guarenteed,
2002     				 * everything in I/O is implicitly mapped
2003     				 * anyways by our clever TLB miss handling
2004     				 * scheme, so don't fail here.  -DaveM
2005     				 */
2006     				if (central_bus == NULL) {
2007     					for_each_sbus(sbus) {
2008     						for_each_sbusdev(sdev, sbus) {
2009     							if (sdev->prom_node == zsnode)
2010     								goto found;
2011     						}
2012     					}
2013     				}
2014     			found:
2015     				if (sdev == NULL && central_bus == NULL)
2016     					prom_halt();
2017     				if (central_bus == NULL) {
2018     					mapped_addr =
2019     					    sbus_ioremap(&sdev->resource[0], 0,
2020     							 PAGE_SIZE, "Zilog Registers");
2021     				} else {
2022     					struct linux_prom_registers zsregs[1];
2023     					int err;
2024     
2025     					err = prom_getproperty(zsnode, "reg",
2026     							       (char *)&zsregs[0],
2027     							       sizeof(zsregs));
2028     					if (err == -1) {
2029     						prom_printf("ZS: Cannot map Zilog regs.\n");
2030     						prom_halt();
2031     					}
2032     					apply_fhc_ranges(central_bus->child, &zsregs[0], 1);
2033     					apply_central_ranges(central_bus, &zsregs[0], 1);
2034     					mapped_addr =
2035     						((((u64)zsregs[0].which_io)<<32UL)|
2036     						 ((u64)zsregs[0].phys_addr));
2037     				}
2038     			} else if(len % sizeof(unsigned int)) {
2039     				prom_printf("WHOOPS:  proplen for %s "
2040     					    "was %d, need multiple of "
2041     					    "%d\n", "address", len,
2042     					    sizeof(unsigned int));
2043     				panic("zilog: address property");
2044     			}
2045     			zs_nodes[chip] = zsnode;
2046     			len = prom_getproperty(zsnode, "interrupts",
2047     					       (char *) &sun4u_ino,
2048     					       (sizeof(sun4u_ino)));
2049     			if(!irq) {
2050     				if (central_bus) {
2051     					unsigned long iclr, imap;
2052     
2053     					iclr = central_bus->child->fhc_regs.uregs + FHC_UREGS_ICLR;
2054     					imap = central_bus->child->fhc_regs.uregs + FHC_UREGS_IMAP;
2055     					irq = zilog_irq = build_irq(12, 0, iclr, imap);
2056     				} else {
2057     					irq = zilog_irq = 
2058     						sbus_build_irq(sbus_root, sun4u_ino);
2059     				}
2060     			}
2061     			break;
2062     		}
2063     		zsnode = prom_getsibling(zsnode);
2064     		seen++;
2065     	}
2066     	if(!zsnode)
2067     		panic("get_zs: whee chip not found");
2068     	if(!vaddr[0] && !mapped_addr)
2069     		panic("get_zs: whee no serial chip mappable");
2070     	if (mapped_addr != 0) {
2071     		return (struct sun_zslayout *) mapped_addr;
2072     	} else {
2073     		pgd_t *pgd = pgd_offset_k((unsigned long)vaddr[0]);
2074     		pmd_t *pmd = pmd_offset(pgd, (unsigned long)vaddr[0]);
2075     		pte_t *pte = pte_offset(pmd, (unsigned long)vaddr[0]);
2076     		unsigned long base = pte_val(*pte) & _PAGE_PADDR;
2077     
2078     		/* Translate PROM's mapping we captured at boot
2079     		 * time into physical address.
2080     		 */
2081     		base += ((unsigned long)vaddr[0] & ~PAGE_MASK);
2082     		return (struct sun_zslayout *) base;
2083     	}
2084     }
2085     #else /* !(__sparc_v9__) */
2086     static struct sun_zslayout * __init get_zs(int chip)
2087     {
2088     	struct linux_prom_irqs tmp_irq[2];
2089     	unsigned int paddr = 0;
2090     	unsigned int vaddr[2] = { 0, 0 };
2091     	int zsnode, tmpnode, iospace, slave, len;
2092     	int cpunode = 0, bbnode = 0;
2093     	static int irq = 0;
2094     	int chipid = chip;
2095     
2096     	iospace = 0;
2097     	if(chip < 0 || chip >= NUM_SERIAL)
2098     		panic("get_zs bogon zs chip number");
2099     
2100     	if(sparc_cpu_model == sun4) {
2101     		struct resource dummy_resource;
2102     
2103     		/* Grrr, these have to be hardcoded aieee */
2104     		switch(chip) {
2105     		case 0:
2106     			paddr = 0xf1000000;
2107     			break;
2108     		case 1:
2109     			paddr = 0xf0000000;
2110     			break;
2111     		};
2112     		iospace = 0;
2113     		zs_nodes[chip] = 0;
2114     		if(!irq)
2115     			zilog_irq = irq = 12;
2116     		dummy_resource.start = paddr;
2117     		dummy_resource.end = paddr + 8 - 1;
2118     		dummy_resource.flags = IORESOURCE_IO;
2119     		vaddr[0] = sbus_ioremap(&dummy_resource, 0,
2120     					8, "Zilog Serial");
2121     	} else {
2122     		/* Can use the prom for other machine types */
2123     		zsnode = prom_getchild(prom_root_node);
2124     		if (sparc_cpu_model == sun4d) {
2125     			int no = 0;
2126     
2127     			tmpnode = zsnode;
2128     			zsnode = 0;
2129     			bbnode = 0;
2130     			while (tmpnode && (tmpnode = prom_searchsiblings(tmpnode, "cpu-unit"))) {
2131     				bbnode = prom_getchild(tmpnode);
2132     				if (bbnode && (bbnode = prom_searchsiblings(bbnode, "bootbus"))) {
2133     					if (no == (chip >> 1)) {
2134     						cpunode = tmpnode;
2135     						zsnode = prom_getchild(bbnode);
2136     						chipid = (chip & 1);
2137     						break;
2138     					}
2139     					no++;
2140     				}
2141     				tmpnode = prom_getsibling(tmpnode);
2142     			}
2143     			if (!tmpnode)
2144     				panic ("get_zs: couldn't find %dth bootbus\n", chip >> 1);
2145     		} else {
2146     			tmpnode = prom_searchsiblings(zsnode, "obio");
2147     			if(tmpnode)
2148     				zsnode = prom_getchild(tmpnode);
2149     		}
2150     		if(!zsnode)
2151     			panic("get_zs no zs serial prom node");
2152     		while(zsnode) {
2153     			zsnode = prom_searchsiblings(zsnode, "zs");
2154     			slave = prom_getintdefault(zsnode, "slave", -1);
2155     			if(slave == chipid) {
2156     				/* The one we want */
2157     				if (sparc_cpu_model != sun4d) {
2158     					len = prom_getproperty(zsnode, "address",
2159     							       (void *) vaddr,
2160     							       sizeof(vaddr));
2161             				if (len % sizeof(unsigned int)) {
2162     						prom_printf("WHOOPS:  proplen for %s "
2163     							"was %d, need multiple of "
2164     							"%d\n", "address", len,
2165     							sizeof(unsigned int));
2166     						panic("zilog: address property");
2167     					}
2168     				} else {
2169     					/* On sun4d don't have address property :( */
2170     					struct linux_prom_registers zsreg[4];
2171     					struct resource res;
2172     					
2173     					if (prom_getproperty(zsnode, "reg", (char *)zsreg, sizeof(zsreg)) == -1) {
2174     						prom_printf ("Cannot map zs regs\n");
2175     						prom_halt();
2176     					}
2177     					prom_apply_generic_ranges(bbnode, cpunode, zsreg, 1);
2178     					res.start = zsreg[0].phys_addr;
2179     					res.end = res.start + 8 - 1;
2180     					res.flags = zsreg[0].which_io | IORESOURCE_IO;
2181     					vaddr[0] = sbus_ioremap(&res, 0,
2182     								8, "Zilog Serial");
2183     				}
2184     				zs_nodes[chip] = zsnode;
2185     				len = prom_getproperty(zsnode, "intr",
2186     						       (char *) tmp_irq,
2187     						       sizeof(tmp_irq));
2188     				if (len % sizeof(struct linux_prom_irqs)) {
2189     					prom_printf(
2190     					      "WHOOPS:  proplen for %s "
2191     					      "was %d, need multiple of "
2192     					      "%d\n", "intr", len,
2193     					      sizeof(struct linux_prom_irqs));
2194     					panic("zilog: intr property");
2195     				}
2196     				if(!irq) {
2197     					irq = zilog_irq = tmp_irq[0].pri;
2198     				} else {
2199     					if(tmp_irq[0].pri != irq)
2200     						panic("zilog: bogon irqs");
2201     				}
2202     				break;
2203     			}
2204     			zsnode = prom_getsibling(zsnode);
2205     		}
2206     		if(!zsnode)
2207     			panic("get_zs whee chip not found");
2208     	}
2209     	if(!vaddr[0])
2210     		panic("get_zs whee no serial chip mappable");
2211     
2212     	return (struct sun_zslayout *)(unsigned long) vaddr[0];
2213     }
2214     #endif
2215     /* This is for the auto baud rate detection in the mouse driver. */
2216     void zs_change_mouse_baud(int newbaud)
2217     {
2218     	int channel = MOUSE_LINE;
2219     	int brg;
2220     
2221     	zs_soft[channel].zs_baud = newbaud;
2222     	brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2223     			 (ZS_CLOCK / zs_soft[channel].clk_divisor));
2224     	write_zsreg(zs_soft[channel].zs_channel, R12, (brg & 0xff));
2225     	write_zsreg(zs_soft[channel].zs_channel, R13, ((brg >> 8) & 0xff));
2226     }
2227     
2228     void __init zs_init_alloc_failure(const char *table_name)
2229     {
2230     	prom_printf("zs_probe: Cannot alloc %s.\n", table_name);
2231     	prom_halt();
2232     }
2233     
2234     void * __init zs_alloc_bootmem(unsigned long size)
2235     {
2236     	void *ret;
2237     
2238     	ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
2239     	if (ret != NULL)
2240     		memset(ret, 0, size);
2241     
2242     	return ret;
2243     }
2244     
2245     void __init zs_alloc_tables(void)
2246     {
2247     	zs_chips = (struct sun_zslayout **)
2248     		zs_alloc_bootmem(NUM_SERIAL * sizeof(struct sun_zslayout *));
2249     	if (zs_chips == NULL)
2250     		zs_init_alloc_failure("zs_chips");
2251     	zs_channels = (struct sun_zschannel **)
2252     		zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct sun_zschannel *));
2253     	if (zs_channels == NULL)
2254     		zs_init_alloc_failure("zs_channels");
2255     	zs_nodes = (int *)
2256     		zs_alloc_bootmem(NUM_SERIAL * sizeof(int));
2257     	if (zs_nodes == NULL)
2258     		zs_init_alloc_failure("zs_nodes");
2259     	zs_soft = (struct sun_serial *)
2260     		zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct sun_serial));
2261     	if (zs_soft == NULL)
2262     		zs_init_alloc_failure("zs_soft");
2263     	zs_ttys = (struct tty_struct *)
2264     		zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct tty_struct));
2265     	if (zs_ttys == NULL)
2266     		zs_init_alloc_failure("zs_ttys");
2267     	serial_table = (struct tty_struct **)
2268     		zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct tty_struct *));
2269     	if (serial_table == NULL)
2270     		zs_init_alloc_failure("serial_table");
2271     	serial_termios = (struct termios **)
2272     		zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct termios *));
2273     	if (serial_termios == NULL)
2274     		zs_init_alloc_failure("serial_termios");
2275     	serial_termios_locked = (struct termios **)
2276     		zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct termios *));
2277     	if (serial_termios_locked == NULL)
2278     		zs_init_alloc_failure("serial_termios_locked");
2279     }
2280     
2281     int __init zs_probe(void)
2282     {
2283     	int node;
2284     
2285     	if(sparc_cpu_model == sun4)
2286     		goto no_probe;
2287     
2288     	NUM_SERIAL = 0;
2289     	
2290     	node = prom_getchild(prom_root_node);
2291     	if (sparc_cpu_model == sun4d) {
2292     		int bbnode;
2293     		
2294     		while (node && (node = prom_searchsiblings(node, "cpu-unit"))) {
2295     			bbnode = prom_getchild(node);
2296     			if (bbnode && prom_searchsiblings(bbnode, "bootbus"))
2297     				NUM_SERIAL += 2;
2298     			node = prom_getsibling(node);
2299     		}
2300     		goto no_probe;
2301     	}
2302     #ifdef __sparc_v9__
2303     	else if (sparc_cpu_model == sun4u) {
2304     		int central_node;
2305     
2306     		/* Central bus zilogs must be checked for first,
2307     		 * since Enterprise boxes might have SBUSes as well.
2308     		 */
2309     		central_node = prom_finddevice("/central");
2310     		if(central_node != 0 && central_node != -1)
2311     			node = prom_searchsiblings(prom_getchild(central_node), "fhc");
2312     		else
2313     			node = prom_searchsiblings(node, "sbus");
2314     		if(node != 0 && node != -1)
2315     			node = prom_getchild(node);
2316     		if(node == 0 || node == -1)
2317     			return -ENODEV;
2318     	}
2319     #endif /* __sparc_v9__ */
2320     	else {
2321     		node = prom_searchsiblings(node, "obio");
2322     		if(node)
2323     			node = prom_getchild(node);
2324     		NUM_SERIAL = 2;
2325     		goto no_probe;
2326     	}
2327     
2328     	node = prom_searchsiblings(node, "zs");
2329     	if (!node)
2330     		return -ENODEV;
2331     		
2332     	NUM_SERIAL = 2;
2333     
2334     no_probe:
2335     	zs_alloc_tables();
2336     
2337     	/* Fill in rs_ops struct... */
2338     #ifdef CONFIG_SERIAL_CONSOLE
2339     	sunserial_setinitfunc(zs_console_init);
2340     #endif
2341     	sunserial_setinitfunc(zs_init);
2342     	rs_ops.rs_kgdb_hook = zs_kgdb_hook;
2343     	rs_ops.rs_change_mouse_baud = zs_change_mouse_baud;
2344     
2345     	sunkbd_setinitfunc(sun_kbd_init);
2346     	kbd_ops.compute_shiftstate = sun_compute_shiftstate;
2347     	kbd_ops.setledstate = sun_setledstate;
2348     	kbd_ops.getledstate = sun_getledstate;
2349     	kbd_ops.setkeycode = sun_setkeycode;
2350     	kbd_ops.getkeycode = sun_getkeycode;
2351     #if defined(__sparc_v9__) && defined(CONFIG_PCI)
2352     	sunkbd_install_keymaps(sun_key_maps, sun_keymap_count,
2353     			       sun_func_buf, sun_func_table,
2354     			       sun_funcbufsize, sun_funcbufleft,
2355     			       sun_accent_table, sun_accent_table_size);
2356     #endif
2357     	return 0;
2358     }
2359     
2360     static inline void zs_prepare(void)
2361     {
2362     	int channel, chip;
2363     	unsigned long flags;
2364     
2365     	if (!NUM_SERIAL)
2366     		return;
2367     	
2368     	save_and_cli(flags);
2369     	
2370     	/* Set up our interrupt linked list */
2371     	zs_chain = &zs_soft[0];
2372     	for(channel = 0; channel < NUM_CHANNELS - 1; channel++) {
2373     		zs_soft[channel].zs_next = &zs_soft[channel + 1];
2374     		zs_soft[channel].line = channel;
2375     	}
2376     	zs_soft[channel].zs_next = 0;
2377     
2378     	/* Initialize Softinfo */
2379     	for(chip = 0; chip < NUM_SERIAL; chip++) {
2380     		/* If we are doing kgdb over one of the channels on
2381     		 * chip zero, kgdb_channel will be set to 1 by the
2382     		 * zs_kgdb_hook() routine below.
2383     		 */
2384     		if(!zs_chips[chip]) {
2385     			zs_chips[chip] = get_zs(chip);
2386     			/* Two channels per chip */
2387     			zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2388     			zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2389     			zs_soft[(chip*2)].kgdb_channel = 0;
2390     			zs_soft[(chip*2)+1].kgdb_channel = 0;
2391     		}
2392     
2393     		/* First, set up channel A on this chip. */
2394     		channel = chip * 2;
2395     		zs_soft[channel].zs_channel = zs_channels[channel];
2396     		zs_soft[channel].change_needed = 0;
2397     		zs_soft[channel].clk_divisor = 16;
2398     		zs_soft[channel].cons_keyb = 0;
2399     		zs_soft[channel].cons_mouse = 0;
2400     		zs_soft[channel].channelA = 1;
2401     
2402     		/* Now, channel B */
2403     		channel++;
2404     		zs_soft[channel].zs_channel = zs_channels[channel];
2405     		zs_soft[channel].change_needed = 0;
2406     		zs_soft[channel].clk_divisor = 16;
2407     		zs_soft[channel].cons_keyb = 0;
2408     		zs_soft[channel].cons_mouse = 0;
2409     		zs_soft[channel].channelA = 0;
2410     	}
2411     	
2412     	restore_flags(flags);
2413     }
2414     
2415     int __init zs_init(void)
2416     {
2417     	int channel, brg, i;
2418     	unsigned long flags;
2419     	struct sun_serial *info;
2420     	char dummy;
2421     
2422     	/* Setup base handler, and timer table. */
2423     	init_bh(SERIAL_BH, do_serial_bh);
2424     
2425     	show_serial_version();
2426     
2427     	/* Initialize the tty_driver structure */
2428     	/* SPARC: Not all of this is exactly right for us. */
2429     	
2430     	memset(&serial_driver, 0, sizeof(struct tty_driver));
2431     	serial_driver.magic = TTY_DRIVER_MAGIC;
2432     	serial_driver.driver_name = "serial";
2433     #ifdef CONFIG_DEVFS_FS
2434     	serial_driver.name = "tts/%d";
2435     #else
2436     	serial_driver.name = "ttyS";
2437     #endif
2438     	serial_driver.major = TTY_MAJOR;
2439     	serial_driver.minor_start = 64;
2440     	serial_driver.num = NUM_CHANNELS;
2441     	serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2442     	serial_driver.subtype = SERIAL_TYPE_NORMAL;
2443     	serial_driver.init_termios = tty_std_termios;
2444     	serial_driver.init_termios.c_cflag =
2445     		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2446     	serial_driver.flags = TTY_DRIVER_REAL_RAW;
2447     	serial_driver.refcount = &serial_refcount;
2448     	serial_driver.table = serial_table;
2449     	serial_driver.termios = serial_termios;
2450     	serial_driver.termios_locked = serial_termios_locked;
2451     
2452     	serial_driver.open = zs_open;
2453     	serial_driver.close = zs_close;
2454     	serial_driver.write = zs_write;
2455     	serial_driver.flush_chars = zs_flush_chars;
2456     	serial_driver.write_room = zs_write_room;
2457     	serial_driver.chars_in_buffer = zs_chars_in_buffer;
2458     	serial_driver.flush_buffer = zs_flush_buffer;
2459     	serial_driver.ioctl = zs_ioctl;
2460     	serial_driver.throttle = zs_throttle;
2461     	serial_driver.unthrottle = zs_unthrottle;
2462     	serial_driver.set_termios = zs_set_termios;
2463     	serial_driver.stop = zs_stop;
2464     	serial_driver.start = zs_start;
2465     	serial_driver.hangup = zs_hangup;
2466     
2467     	/* I'm too lazy, someone write versions of this for us. -DaveM */
2468     	serial_driver.read_proc = 0;
2469     	serial_driver.proc_entry = 0;
2470     
2471     	/*
2472     	 * The callout device is just like normal device except for
2473     	 * major number and the subtype code.
2474     	 */
2475     	callout_driver = serial_driver;
2476     	callout_driver.name = "cua/%d";
2477     	callout_driver.major = TTYAUX_MAJOR;
2478     	callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2479     
2480     	if (tty_register_driver(&serial_driver))
2481     		panic("Couldn't register serial driver\n");
2482     	if (tty_register_driver(&callout_driver))
2483     		panic("Couldn't register callout driver\n");
2484     
2485     	save_flags(flags); cli();
2486     
2487     	/* Initialize Softinfo */
2488     	zs_prepare();
2489     
2490     	/* Grab IRQ line before poking the chips so we do
2491     	 * not lose any interrupts.
2492     	 */
2493     	if (request_irq(zilog_irq, zs_interrupt,
2494     			(SA_INTERRUPT | SA_STATIC_ALLOC),
2495     			"Zilog8530", zs_chain)) {
2496     		prom_printf("Unable to attach zs intr\n");
2497     		prom_halt();
2498     	}
2499     
2500     	/* Initialize Hardware */
2501     	for(channel = 0; channel < NUM_CHANNELS; channel++) {
2502     		/* Hardware reset each chip */
2503     		if (!(channel & 1)) {
2504     			write_zsreg(zs_soft[channel].zs_channel, R9, FHWRES);
2505     			ZSDELAY_LONG();
2506     			dummy = read_zsreg(zs_soft[channel].zs_channel, R0);
2507     		}
2508     
2509     		if(channel == KEYBOARD_LINE) {
2510     			zs_soft[channel].cons_keyb = 1;
2511     			zs_soft[channel].parity_mask = 0xff;
2512     			zs_kbdchan = zs_soft[channel].zs_channel;
2513     
2514     			write_zsreg(zs_soft[channel].zs_channel, R4,
2515     				    (PAR_EVEN | X16CLK | SB1));
2516     			write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2517     			write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2518     			write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2519     			write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2520     			write_zsreg(zs_soft[channel].zs_channel, R11,
2521     				    (TCBR | RCBR));
2522     			zs_soft[channel].zs_baud = 1200;
2523     			brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2524     					 ZS_CLOCK/zs_soft[channel].clk_divisor);
2525     			write_zsreg(zs_soft[channel].zs_channel, R12,
2526     				    (brg & 0xff));
2527     			write_zsreg(zs_soft[channel].zs_channel, R13,
2528     				    ((brg >> 8) & 0xff));
2529     			write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2530     
2531     			/* Enable Rx/Tx, IRQs, and inform kbd driver */
2532     			write_zsreg(zs_soft[channel].zs_channel, R14,
2533     				    (BRSRC | BRENAB));
2534     			write_zsreg(zs_soft[channel].zs_channel, R3,
2535     				    (Rx8 | RxENAB));
2536     			write_zsreg(zs_soft[channel].zs_channel, R5,
2537     				    (Tx8 | TxENAB | DTR | RTS));
2538     
2539     			write_zsreg(zs_soft[channel].zs_channel, R15,
2540     				    (DCDIE | CTSIE | TxUIE | BRKIE));
2541     			write_zsreg(zs_soft[channel].zs_channel, R0,
2542     				    RES_EXT_INT);
2543     			write_zsreg(zs_soft[channel].zs_channel, R0,
2544     				    RES_EXT_INT);
2545     
2546     			write_zsreg(zs_soft[channel].zs_channel, R1,
2547     				    (EXT_INT_ENAB | INT_ALL_Rx));
2548     			write_zsreg(zs_soft[channel].zs_channel, R9,
2549     				    (NV | MIE));
2550     			ZS_CLEARERR(zs_soft[channel].zs_channel);
2551     			ZS_CLEARFIFO(zs_soft[channel].zs_channel);
2552     		} else if(channel == MOUSE_LINE) {
2553     			zs_soft[channel].cons_mouse = 1;
2554     			zs_soft[channel].parity_mask = 0xff;
2555     			zs_mousechan = zs_soft[channel].zs_channel;
2556     
2557     			write_zsreg(zs_soft[channel].zs_channel, R4,
2558     				    (PAR_EVEN | X16CLK | SB1));
2559     			write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2560     			write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2561     			write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2562     			write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2563     			write_zsreg(zs_soft[channel].zs_channel, R11,
2564     				    (TCBR | RCBR));
2565     
2566     			zs_soft[channel].zs_baud = 4800;
2567     			brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2568     					 ZS_CLOCK/zs_soft[channel].clk_divisor);
2569     			write_zsreg(zs_soft[channel].zs_channel, R12,
2570     				    (brg & 0xff));
2571     			write_zsreg(zs_soft[channel].zs_channel, R13,
2572     				    ((brg >> 8) & 0xff));
2573     			write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2574     
2575     			/* Enable Rx, IRQs, and inform mouse driver */
2576     			write_zsreg(zs_soft[channel].zs_channel, R14,
2577     				    (BRSRC | BRENAB));
2578     			write_zsreg(zs_soft[channel].zs_channel, R3,
2579     				    (Rx8 | RxENAB));
2580     			write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2581     
2582     			write_zsreg(zs_soft[channel].zs_channel, R15,
2583     				    (DCDIE | CTSIE | TxUIE | BRKIE));
2584     			write_zsreg(zs_soft[channel].zs_channel, R0,
2585     				    RES_EXT_INT);
2586     			write_zsreg(zs_soft[channel].zs_channel, R0,
2587     				    RES_EXT_INT);
2588     
2589     			write_zsreg(zs_soft[channel].zs_channel, R1,
2590     				    (EXT_INT_ENAB | INT_ALL_Rx));
2591     			write_zsreg(zs_soft[channel].zs_channel, R9,
2592     				    (NV | MIE));
2593     
2594     			sun_mouse_zsinit();
2595     		} else if (zs_soft[channel].is_cons) {
2596     			brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2597     					 ZS_CLOCK/zs_soft[channel].clk_divisor);
2598     			zscons_regs[12] = brg & 0xff;
2599     			zscons_regs[13] = (brg >> 8) & 0xff;
2600     
2601     			memcpy(zs_soft[channel].curregs, zscons_regs, sizeof(zscons_regs));
2602     			load_zsregs(&zs_soft[channel], zscons_regs);
2603     
2604     			ZS_CLEARERR(zs_soft[channel].zs_channel);
2605     			ZS_CLEARFIFO(zs_soft[channel].zs_channel);
2606     		} else if (zs_soft[channel].kgdb_channel) {
2607     			/* If this is the kgdb line, enable interrupts because
2608     			 * we now want to receive the 'control-c' character
2609     			 * from the client attached to us asynchronously.
2610     			 */
2611     			zs_soft[channel].parity_mask = 0xff;
2612             		kgdb_chaninit(&zs_soft[channel], 1,
2613     				      zs_soft[channel].zs_baud);
2614     		} else {
2615     			zs_soft[channel].parity_mask = 0xff;
2616     			write_zsreg(zs_soft[channel].zs_channel, R4,
2617     				    (PAR_EVEN | X16CLK | SB1));
2618     			write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2619     			write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2620     			write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2621     			write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2622     			write_zsreg(zs_soft[channel].zs_channel, R11,
2623     				    (RCBR | TCBR));
2624     			zs_soft[channel].zs_baud = 9600;
2625     			brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2626     					 ZS_CLOCK/zs_soft[channel].clk_divisor);
2627     			write_zsreg(zs_soft[channel].zs_channel, R12,
2628     				    (brg & 0xff));
2629     			write_zsreg(zs_soft[channel].zs_channel, R13,
2630     				    ((brg >> 8) & 0xff));
2631     			write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2632     			write_zsreg(zs_soft[channel].zs_channel, R14,
2633     				    (BRSRC | BRENAB));
2634     			write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2635     			write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2636     			write_zsreg(zs_soft[channel].zs_channel, R15, DCDIE);
2637     			write_zsreg(zs_soft[channel].zs_channel, R9, NV | MIE);
2638     			write_zsreg(zs_soft[channel].zs_channel, R0,
2639     				    RES_EXT_INT);
2640     			write_zsreg(zs_soft[channel].zs_channel, R0,
2641     				    RES_EXT_INT);
2642     		}
2643     	}
2644     
2645     	for (info = zs_chain, i=0; info; info = info->zs_next, i++) {
2646     		info->magic = SERIAL_MAGIC;
2647     		info->port = (long) info->zs_channel;
2648     		info->line = i;
2649     		info->tty = 0;
2650     		info->irq = zilog_irq;
2651     		info->custom_divisor = 16;
2652     		info->close_delay = 50;
2653     		info->closing_wait = 3000;
2654     		info->x_char = 0;
2655     		info->event = 0;
2656     		info->count = 0;
2657     		info->blocked_open = 0;
2658     		info->tqueue.routine = do_softint;
2659     		info->tqueue.data = info;
2660     		info->tqueue_hangup.routine = do_serial_hangup;
2661     		info->tqueue_hangup.data = info;
2662     		info->callout_termios = callout_driver.init_termios;
2663     		info->normal_termios = serial_driver.init_termios;
2664     		init_waitqueue_head(&info->open_wait);
2665     		init_waitqueue_head(&info->close_wait);
2666     		printk("tty%02d at 0x%04x (irq = %s)", info->line, 
2667     		       info->port, __irq_itoa(info->irq));
2668     		printk(" is a Zilog8530\n");
2669     	}
2670     
2671     	restore_flags(flags);
2672     
2673     	keyboard_zsinit(kbd_put_char);
2674     	return 0;
2675     }
2676     
2677     /* This is called at boot time to prime the kgdb serial debugging
2678      * serial line.  The 'tty_num' argument is 0 for /dev/ttya and 1
2679      * for /dev/ttyb which is determined in setup_arch() from the
2680      * boot command line flags.
2681      */
2682     static void __init zs_kgdb_hook(int tty_num)
2683     {
2684     	int chip = 0;
2685     
2686     	if(!zs_chips[chip]) {
2687     		zs_chips[chip] = get_zs(chip);
2688     		/* Two channels per chip */
2689     		zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2690     		zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2691     	}
2692     	zs_soft[tty_num].zs_channel = zs_channels[tty_num];
2693     	zs_kgdbchan = zs_soft[tty_num].zs_channel;
2694     	zs_soft[tty_num].change_needed = 0;
2695     	zs_soft[tty_num].clk_divisor = 16;
2696     	zs_soft[tty_num].zs_baud = 9600;
2697     	zs_soft[tty_num].kgdb_channel = 1;     /* This runs kgdb */
2698     	zs_soft[tty_num ^ 1].kgdb_channel = 0; /* This does not */
2699     	/* Turn on transmitter/receiver at 8-bits/char */
2700             kgdb_chaninit(&zs_soft[tty_num], 0, 9600);
2701             ZS_CLEARERR(zs_kgdbchan);
2702             ZS_CLEARFIFO(zs_kgdbchan);
2703     }
2704     
2705     #ifdef CONFIG_SERIAL_CONSOLE
2706     
2707     /* This is for console output over ttya/ttyb */
2708     static void
2709     zs_console_putchar(struct sun_serial *info, char ch)
2710     {
2711     	int loops = ZS_PUT_CHAR_MAX_DELAY;
2712     	unsigned long flags;
2713     
2714     	if(!info->zs_channel)
2715     		return;
2716     
2717     	save_flags(flags); cli();
2718     	zs_put_char(info->zs_channel, ch);
2719     	while (!(read_zsreg(info->zs_channel, R1) & ALL_SNT) && --loops)
2720     		udelay(5);
2721     	restore_flags(flags);
2722     }
2723     
2724     #ifdef SERIAL_CONSOLE_FAIR_OUTPUT
2725     /*
2726      * Fair output driver allows a process to speak.
2727      */
2728     static void zs_fair_output(struct sun_serial *info)
2729     {
2730     	unsigned long flags;
2731     	int left;		/* Output no more than that */
2732     	char c;
2733     
2734     	if (info == NULL)
2735     		return;
2736     	if (info->xmit_buf == NULL)
2737     		return;
2738     
2739     	save_flags(flags);  cli();
2740     	left = info->xmit_cnt;
2741     	while (left != 0) {
2742     		c = info->xmit_buf[info->xmit_tail];
2743     		info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
2744     		info->xmit_cnt--;
2745     		restore_flags(flags);
2746     
2747     		zs_console_putchar(info, c);
2748     
2749     		cli();
2750     		left = MIN(info->xmit_cnt, left-1);
2751     	}
2752     
2753     	/* Last character is being transmitted now (hopefully). */
2754     	sbus_writeb(RES_Tx_P, &info->zs_channel->control);
2755     	ZSDELAY();
2756     	ZSLOG(REGCTRL, RES_Tx_P, 1);
2757     
2758     	restore_flags(flags);
2759     	return;
2760     }
2761     #endif
2762     
2763     /*
2764      * zs_console_write is registered for printk.
2765      */
2766     static void
2767     zs_console_write(struct console *con, const char *s, unsigned count)
2768     {
2769     	struct sun_serial *info;
2770     	int i;
2771     
2772     	info = zs_soft + con->index;
2773     
2774     	for (i = 0; i < count; i++, s++) {
2775     		if(*s == '\n')
2776     			zs_console_putchar(info, '\r');
2777     		zs_console_putchar(info, *s);
2778     	}
2779     #ifdef SERIAL_CONSOLE_FAIR_OUTPUT
2780     	/* Comment this if you want to have a strict interrupt-driven output */
2781     	zs_fair_output(info);
2782     #endif
2783     }
2784     
2785     static int
2786     zs_console_wait_key(struct console *con)
2787     {
2788     	sleep_on(&keypress_wait);
2789     	return 0;
2790     }
2791     
2792     static kdev_t zs_console_device(struct console *con)
2793     {
2794     	return MKDEV(TTY_MAJOR, 64 + con->index);
2795     }
2796     
2797     static int __init zs_console_setup(struct console *con, char *options)
2798     {
2799     	struct sun_serial *info;
2800     	int i, brg, baud;
2801     
2802     	info = zs_soft + con->index;
2803     	info->is_cons = 1;
2804     
2805     	printk("Console: ttyS%d (Zilog8530)\n", info->line);
2806     
2807     	sunserial_console_termios(con);
2808     
2809     	i = con->cflag & CBAUD;
2810     	if (con->cflag & CBAUDEX) {
2811     		i &= ~CBAUDEX;
2812     		con->cflag &= ~CBAUDEX;
2813     	}
2814     	baud = baud_table[i];
2815     	info->zs_baud = baud;
2816     
2817     	switch (con->cflag & CSIZE) {
2818     		case CS5:
2819     			zscons_regs[3] = Rx5 | RxENAB;
2820     			zscons_regs[5] = Tx5 | TxENAB;
2821     			info->parity_mask = 0x1f;
2822     			break;
2823     		case CS6:
2824     			zscons_regs[3] = Rx6 | RxENAB;
2825     			zscons_regs[5] = Tx6 | TxENAB;
2826     			info->parity_mask = 0x3f;
2827     			break;
2828     		case CS7:
2829     			zscons_regs[3] = Rx7 | RxENAB;
2830     			zscons_regs[5] = Tx7 | TxENAB;
2831     			info->parity_mask = 0x7f;
2832     			break;
2833     		default:
2834     		case CS8:
2835     			zscons_regs[3] = Rx8 | RxENAB;
2836     			zscons_regs[5] = Tx8 | TxENAB;
2837     			info->parity_mask = 0xff;
2838     			break;
2839     	}
2840     	zscons_regs[5] |= DTR;
2841     
2842     	if (con->cflag & PARENB)
2843     		zscons_regs[4] |= PAR_ENAB;
2844     	if (!(con->cflag & PARODD))
2845     		zscons_regs[4] |= PAR_EVEN;
2846     
2847     	if (con->cflag & CSTOPB)
2848     		zscons_regs[4] |= SB2;
2849     	else
2850     		zscons_regs[4] |= SB1;
2851     
2852     	brg = BPS_TO_BRG(baud, ZS_CLOCK / info->clk_divisor);
2853     	zscons_regs[12] = brg & 0xff;
2854     	zscons_regs[13] = (brg >> 8) & 0xff;
2855     
2856     	memcpy(info->curregs, zscons_regs, sizeof(zscons_regs));
2857     	load_zsregs(info, zscons_regs);
2858     
2859     	ZS_CLEARERR(info->zs_channel);
2860     	ZS_CLEARFIFO(info->zs_channel);
2861     	return 0;
2862     }
2863     
2864     static struct console zs_console = {
2865     	name:		"ttyS",
2866     	write:		zs_console_write,
2867     	device:		zs_console_device,
2868     	wait_key:	zs_console_wait_key,
2869     	setup:		zs_console_setup,
2870     	flags:		CON_PRINTBUFFER,
2871     	index:		-1,
2872     };
2873     
2874     static int __init zs_console_init(void)
2875     {
2876     	extern int con_is_present(void);
2877     
2878     	if (con_is_present())
2879     		return 0;
2880     
2881     	zs_console.index = serial_console - 1;
2882     	register_console(&zs_console);
2883     	return 0;
2884     }
2885     
2886     #endif /* CONFIG_SERIAL_CONSOLE */
2887