File: /usr/src/linux/drivers/sgi/char/sgiserial.c

1     /* sgiserial.c: Serial port driver for SGI machines.
2      *
3      * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
4      */
5     
6     /*
7      * Note: This driver seems to have been derived from some
8      * version of the sbus/char/zs.c driver.  A lot of clean-up
9      * and bug fixes seem to have happened to the Sun driver in
10      * the intervening time.  As of 21.09.1999, I have merged in
11      * ONLY the changes necessary to fix observed functional
12      * problems on the Indy.  Someone really ought to do a
13      * thorough pass to merge in the rest of the updates.
14      * Better still, someone really ought to make it a common
15      * code module for both platforms.   kevink@mips.com
16      *
17      * 20010616 - Klaus Naumann <spock@mgnet.de> : Make serial console work with
18      *                                             any speed - not only 9600
19      */
20     
21     #include <linux/config.h> /* for CONFIG_REMOTE_DEBUG */
22     #include <linux/errno.h>
23     #include <linux/signal.h>
24     #include <linux/sched.h>
25     #include <linux/timer.h>
26     #include <linux/interrupt.h>
27     #include <linux/tty.h>
28     #include <linux/tty_flip.h>
29     #include <linux/major.h>
30     #include <linux/string.h>
31     #include <linux/fcntl.h>
32     #include <linux/mm.h>
33     #include <linux/kernel.h>
34     #include <linux/delay.h>
35     #include <linux/console.h>
36     #include <linux/init.h>
37     
38     #include <asm/io.h>
39     #include <asm/irq.h>
40     #include <asm/sgialib.h>
41     #include <asm/system.h>
42     #include <asm/bitops.h>
43     #include <asm/sgi/sgihpc.h>
44     #include <asm/sgi/sgint23.h>
45     #include <asm/uaccess.h>
46     
47     #include "sgiserial.h"
48     
49     #define NUM_SERIAL 1     /* One chip on board. */
50     #define NUM_CHANNELS (NUM_SERIAL * 2)
51     
52     extern wait_queue_head_t keypress_wait;
53     
54     struct sgi_zslayout *zs_chips[NUM_SERIAL] = { 0, };
55     struct sgi_zschannel *zs_channels[NUM_CHANNELS] = { 0, 0, };
56     struct sgi_zschannel *zs_conschan;
57     struct sgi_zschannel *zs_kgdbchan;
58     
59     struct sgi_serial zs_soft[NUM_CHANNELS];
60     struct sgi_serial *zs_chain;  /* IRQ servicing chain */
61     static int zilog_irq = SGI_SERIAL_IRQ;
62     
63     /* Console hooks... */
64     static int zs_cons_chanout;
65     static int zs_cons_chanin;
66     struct sgi_serial *zs_consinfo;
67     
68     static unsigned char kgdb_regs[16] = {
69     	0, 0, 0,                     /* write 0, 1, 2 */
70     	(Rx8 | RxENABLE),            /* write 3 */
71     	(X16CLK | SB1 | PAR_EVEN),   /* write 4 */
72     	(Tx8 | TxENAB),              /* write 5 */
73     	0, 0, 0,                     /* write 6, 7, 8 */
74     	(NV),                        /* write 9 */
75     	(NRZ),                       /* write 10 */
76     	(TCBR | RCBR),               /* write 11 */
77     	0, 0,                        /* BRG time constant, write 12 + 13 */
78     	(BRENABL),                   /* write 14 */
79     	(DCDIE)                      /* write 15 */
80     };
81     
82     static unsigned char zscons_regs[16] = {
83     	0,                           /* write 0 */
84     	(EXT_INT_ENAB | INT_ALL_Rx), /* write 1 */
85     	0,                           /* write 2 */
86     	(Rx8 | RxENABLE),            /* write 3 */
87     	(X16CLK),                    /* write 4 */
88     	(DTR | Tx8 | TxENAB),        /* write 5 */
89     	0, 0, 0,                     /* write 6, 7, 8 */
90     	(NV | MIE),                  /* write 9 */
91     	(NRZ),                       /* write 10 */
92     	(TCBR | RCBR),               /* write 11 */
93     	0, 0,                        /* BRG time constant, write 12 + 13 */
94     	(BRENABL),                   /* write 14 */
95     	(DCDIE | CTSIE | TxUIE | BRKIE) /* write 15 */
96     };
97     
98     #define ZS_CLOCK         3672000   /* Zilog input clock rate */
99     
100     DECLARE_TASK_QUEUE(tq_serial);
101     
102     struct tty_driver serial_driver, callout_driver;
103     struct console *sgisercon;
104     static int serial_refcount;
105     
106     /* serial subtype definitions */
107     #define SERIAL_TYPE_NORMAL	1
108     #define SERIAL_TYPE_CALLOUT	2
109       
110     /* number of characters left in xmit buffer before we ask for more */
111     #define WAKEUP_CHARS 256
112     
113     /* Debugging... DEBUG_INTR is bad to use when one of the zs
114      * lines is your console ;(
115      */
116     #undef SERIAL_DEBUG_INTR
117     #undef SERIAL_DEBUG_OPEN
118     #undef SERIAL_DEBUG_FLOW
119     
120     #define RS_STROBE_TIME 10
121     #define RS_ISR_PASS_LIMIT 256
122     
123     #define _INLINE_ inline
124     
125     static void change_speed(struct sgi_serial *info);
126     
127     static struct tty_struct *serial_table[NUM_CHANNELS];
128     static struct termios *serial_termios[NUM_CHANNELS];
129     static struct termios *serial_termios_locked[NUM_CHANNELS];
130     
131     #ifndef MIN
132     #define MIN(a,b)	((a) < (b) ? (a) : (b))
133     #endif
134     
135     /*
136      * tmp_buf is used as a temporary buffer by serial_write.  We need to
137      * lock it in case the memcpy_fromfs blocks while swapping in a page,
138      * and some other program tries to do a serial write at the same time.
139      * Since the lock will only come under contention when the system is
140      * swapping and available memory is low, it makes sense to share one
141      * buffer across all the serial ports, since it significantly saves
142      * memory if large numbers of serial ports are open.
143      */
144     static unsigned char tmp_buf[4096]; /* This is cheating */
145     static DECLARE_MUTEX(tmp_buf_sem);
146     
147     static inline int serial_paranoia_check(struct sgi_serial *info,
148     					dev_t device, const char *routine)
149     {
150     #ifdef SERIAL_PARANOIA_CHECK
151     	static const char *badmagic = KERN_WARNING
152     		"Warning: bad magic number for serial struct (%d, %d) in %s\n";
153     	static const char *badinfo = KERN_WARNING
154     		"Warning: null sgi_serial for (%d, %d) in %s\n";
155     
156     	if (!info) {
157     		printk(badinfo, MAJOR(device), MINOR(device), routine);
158     		return 1;
159     	}
160     	if (info->magic != SERIAL_MAGIC) {
161     		printk(badmagic, MAJOR(device), MINOR(device), routine);
162     		return 1;
163     	}
164     #endif
165     	return 0;
166     }
167     
168     /*
169      * This is used to figure out the divisor speeds and the timeouts
170      */
171     static int baud_table[] = {
172     	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
173     	9600, 19200, 38400, 57600, 115200, 0 };
174     
175     /* 
176      * Reading and writing Zilog8530 registers.  The delays are to make this
177      * driver work on the Sun4 which needs a settling delay after each chip
178      * register access, other machines handle this in hardware via auxiliary
179      * flip-flops which implement the settle time we do in software.
180      *
181      * read_zsreg() and write_zsreg() may get called from rs_kgdb_hook() before
182      * interrupts are enabled. Therefore we have to check ioc_iocontrol before we
183      * access it.
184      */
185     static inline unsigned char read_zsreg(struct sgi_zschannel *channel,
186                                            unsigned char reg)
187     {
188     	unsigned char retval;
189     	volatile unsigned char junk;
190     
191     	udelay(2);
192     	channel->control = reg;
193     	if (ioc_icontrol)
194     		junk = ioc_icontrol->istat0;
195     	udelay(1);
196     	retval = channel->control;
197     	return retval;
198     }
199     
200     static inline void write_zsreg(struct sgi_zschannel *channel,
201                                    unsigned char reg, unsigned char value)
202     {
203     	volatile unsigned char junk;
204     
205     	udelay(2);
206     	channel->control = reg;
207     	if (ioc_icontrol)
208     		junk = ioc_icontrol->istat0;
209     	udelay(1);
210     	channel->control = value;
211     	if (ioc_icontrol)
212     		junk = ioc_icontrol->istat0;
213     	return;
214     }
215     
216     static inline void load_zsregs(struct sgi_zschannel *channel, unsigned char *regs)
217     {
218     	ZS_CLEARERR(channel);
219     	ZS_CLEARFIFO(channel);
220     	/* Load 'em up */
221     	write_zsreg(channel, R4, regs[R4]);
222     	write_zsreg(channel, R10, regs[R10]);
223     	write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
224     	write_zsreg(channel, R5, regs[R5] & ~TxENAB);
225     	write_zsreg(channel, R1, regs[R1]);
226     	write_zsreg(channel, R9, regs[R9]);
227     	write_zsreg(channel, R11, regs[R11]);
228     	write_zsreg(channel, R12, regs[R12]);
229     	write_zsreg(channel, R13, regs[R13]);
230     	write_zsreg(channel, R14, regs[R14]);
231     	write_zsreg(channel, R15, regs[R15]);
232     	write_zsreg(channel, R3, regs[R3]);
233     	write_zsreg(channel, R5, regs[R5]);
234     	return;
235     }
236     
237     /* Sets or clears DTR/RTS on the requested line */
238     static inline void zs_rtsdtr(struct sgi_serial *ss, int set)
239     {
240     	if(set) {
241     		ss->curregs[5] |= (RTS | DTR);
242     		ss->pendregs[5] = ss->curregs[5];
243     		write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
244     	} else {
245     		ss->curregs[5] &= ~(RTS | DTR);
246     		ss->pendregs[5] = ss->curregs[5];
247     		write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
248     	}
249     	return;
250     }
251     
252     static inline void kgdb_chaninit(struct sgi_serial *ss, int intson, int bps)
253     {
254     	int brg;
255     
256     	if(intson) {
257     		kgdb_regs[R1] = INT_ALL_Rx;
258     		kgdb_regs[R9] |= MIE;
259     	} else {
260     		kgdb_regs[R1] = 0;
261     		kgdb_regs[R9] &= ~MIE;
262     	}
263     	brg = BPS_TO_BRG(bps, ZS_CLOCK/ss->clk_divisor);
264     	kgdb_regs[R12] = (brg & 255);
265     	kgdb_regs[R13] = ((brg >> 8) & 255);
266     	load_zsregs(ss->zs_channel, kgdb_regs);
267     }
268     
269     /* Utility routines for the Zilog */
270     static inline int get_zsbaud(struct sgi_serial *ss)
271     {
272     	struct sgi_zschannel *channel = ss->zs_channel;
273     	int brg;
274     
275     	/* The baud rate is split up between two 8-bit registers in
276     	 * what is termed 'BRG time constant' format in my docs for
277     	 * the chip, it is a function of the clk rate the chip is
278     	 * receiving which happens to be constant.
279     	 */
280     	brg = ((read_zsreg(channel, 13)&0xff) << 8);
281     	brg |= (read_zsreg(channel, 12)&0xff);
282     	return BRG_TO_BPS(brg, (ZS_CLOCK/(ss->clk_divisor)));
283     }
284     
285     /*
286      * ------------------------------------------------------------
287      * rs_stop() and rs_start()
288      *
289      * This routines are called before setting or resetting tty->stopped.
290      * They enable or disable transmitter interrupts, as necessary.
291      * ------------------------------------------------------------
292      */
293     static void rs_stop(struct tty_struct *tty)
294     {
295     	struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
296     	unsigned long flags;
297     
298     	if (serial_paranoia_check(info, tty->device, "rs_stop"))
299     		return;
300     	
301     	save_flags(flags); cli();
302     	if (info->curregs[5] & TxENAB) {
303     		info->curregs[5] &= ~TxENAB;
304     		info->pendregs[5] &= ~TxENAB;
305     		write_zsreg(info->zs_channel, 5, info->curregs[5]);
306     	}
307     	restore_flags(flags);
308     }
309     
310     static void rs_start(struct tty_struct *tty)
311     {
312     	struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
313     	unsigned long flags;
314     	
315     	if (serial_paranoia_check(info, tty->device, "rs_start"))
316     		return;
317     	
318     	save_flags(flags); cli();
319     	if (info->xmit_cnt && info->xmit_buf && !(info->curregs[5] & TxENAB)) {
320     		info->curregs[5] |= TxENAB;
321     		info->pendregs[5] = info->curregs[5];
322     		write_zsreg(info->zs_channel, 5, info->curregs[5]);
323     	}
324     	restore_flags(flags);
325     }
326     
327     /* Drop into either the boot monitor or kadb upon receiving a break
328      * from keyboard/console input.
329      */
330     static void batten_down_hatches(void)
331     {
332     	ArcEnterInteractiveMode();
333     #if 0
334     	/* If we are doing kadb, we call the debugger
335     	 * else we just drop into the boot monitor.
336     	 * Note that we must flush the user windows
337     	 * first before giving up control.
338     	 */
339     	printk("\n");
340     	if((((unsigned long)linux_dbvec)>=DEBUG_FIRSTVADDR) &&
341     	   (((unsigned long)linux_dbvec)<=DEBUG_LASTVADDR))
342     		sp_enter_debugger();
343     	else
344     		prom_halt();
345     
346     	/* XXX We want to notify the keyboard driver that all
347     	 * XXX keys are in the up state or else weird things
348     	 * XXX happen...
349     	 */
350     #endif
351     	return;
352     }
353     
354     /* On receive, this clears errors and the receiver interrupts */
355     static inline void rs_recv_clear(struct sgi_zschannel *zsc)
356     {
357     	volatile unsigned char junk;
358     
359     	udelay(2);
360     	zsc->control = ERR_RES;
361     	junk = ioc_icontrol->istat0;
362     	udelay(2);
363     	zsc->control = RES_H_IUS;
364     	junk = ioc_icontrol->istat0;
365     }
366     
367     /*
368      * ----------------------------------------------------------------------
369      *
370      * Here starts the interrupt handling routines.  All of the following
371      * subroutines are declared as inline and are folded into
372      * rs_interrupt().  They were separated out for readability's sake.
373      *
374      * Note: rs_interrupt() is a "fast" interrupt, which means that it
375      * runs with interrupts turned off.  People who may want to modify
376      * rs_interrupt() should try to keep the interrupt handler as fast as
377      * possible.  After you are done making modifications, it is not a bad
378      * idea to do:
379      * 
380      * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
381      *
382      * and look at the resulting assemble code in serial.s.
383      *
384      * 				- Ted Ts'o (tytso@mit.edu), 7-Mar-93
385      * -----------------------------------------------------------------------
386      */
387     
388     /*
389      * This routine is used by the interrupt handler to schedule
390      * processing in the software interrupt portion of the driver.
391      */
392     static _INLINE_ void rs_sched_event(struct sgi_serial *info,
393     				    int event)
394     {
395     	info->event |= 1 << event;
396     	queue_task(&info->tqueue, &tq_serial);
397     	mark_bh(SERIAL_BH);
398     }
399     
400     #ifdef CONFIG_REMOTE_DEBUG
401     extern void set_async_breakpoint(unsigned int epc);
402     #endif
403     
404     static _INLINE_ void receive_chars(struct sgi_serial *info, struct pt_regs *regs)
405     {
406     	struct tty_struct *tty = info->tty;
407     	volatile unsigned char junk;
408     	unsigned char ch, stat;
409     
410     	udelay(2);
411     	ch = info->zs_channel->data;
412     	junk = ioc_icontrol->istat0;
413     	udelay(2);
414     	stat = read_zsreg(info->zs_channel, R1);
415     
416     	/* If this is the console keyboard, we need to handle
417     	 * L1-A's here.
418     	 */
419     	if(info->is_cons) {
420     		if(ch==0) { /* whee, break received */
421     			batten_down_hatches();
422     			rs_recv_clear(info->zs_channel);
423     			return;
424     		} else if (ch == 1) {
425     			show_state();
426     			return;
427     		} else if (ch == 2) {
428     			show_buffers();
429     			return;
430     		}
431     		/* It is a 'keyboard interrupt' ;-) */
432     		wake_up(&keypress_wait);
433     	}
434     	/* Look for kgdb 'stop' character, consult the gdb documentation
435     	 * for remote target debugging and arch/sparc/kernel/sparc-stub.c
436     	 * to see how all this works.
437     	 */
438     #ifdef CONFIG_REMOTE_DEBUG
439     	if((info->kgdb_channel) && (ch =='\003')) {
440     		set_async_breakpoint(read_32bit_cp0_register(CP0_EPC));
441     		goto clear_and_exit;
442     	}
443     #endif
444     	if(!tty)
445     		goto clear_and_exit;
446     
447     	if (tty->flip.count >= TTY_FLIPBUF_SIZE)
448     		queue_task(&tty->flip.tqueue, &tq_timer);
449     	tty->flip.count++;
450     	if(stat & PAR_ERR)
451     		*tty->flip.flag_buf_ptr++ = TTY_PARITY;
452     	else if(stat & Rx_OVR)
453     		*tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
454     	else if(stat & CRC_ERR)
455     		*tty->flip.flag_buf_ptr++ = TTY_FRAME;
456     	else
457     		*tty->flip.flag_buf_ptr++ = 0; /* XXX */
458     	*tty->flip.char_buf_ptr++ = ch;
459     
460     	queue_task(&tty->flip.tqueue, &tq_timer);
461     
462     clear_and_exit:
463     	rs_recv_clear(info->zs_channel);
464     	return;
465     }
466     
467     static _INLINE_ void transmit_chars(struct sgi_serial *info)
468     {
469     	volatile unsigned char junk;
470     
471     	/* P3: In theory we have to test readiness here because a
472     	 * serial console can clog the chip through zs_cons_put_char().
473     	 * David did not do this. I think he relies on 3-chars FIFO in 8530.
474     	 * Let's watch for lost _output_ characters. XXX
475     	 */
476     
477     	/* SGI ADDENDUM: On most SGI machines, the Zilog does possess
478     	 *               a 16 or 17 byte fifo, so no worries. -dm
479     	 */
480     
481     	if (info->x_char) {
482     		/* Send next char */
483     		udelay(2);
484     		info->zs_channel->data = info->x_char;
485     		junk = ioc_icontrol->istat0;
486     
487     		info->x_char = 0;
488     		goto clear_and_return;
489     	}
490     
491     	if((info->xmit_cnt <= 0) || info->tty->stopped) {
492     		/* That's peculiar... */
493     		udelay(2);
494     		info->zs_channel->control = RES_Tx_P;
495     		junk = ioc_icontrol->istat0;
496     		goto clear_and_return;
497     	}
498     
499     	/* Send char */
500     	udelay(2);
501     	info->zs_channel->data = info->xmit_buf[info->xmit_tail++];
502     	junk = ioc_icontrol->istat0;
503     
504     	info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
505     	info->xmit_cnt--;
506     
507     	if (info->xmit_cnt < WAKEUP_CHARS)
508     		rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
509     
510     	if(info->xmit_cnt <= 0) {
511     		udelay(2);
512     		info->zs_channel->control = RES_Tx_P;
513     		junk = ioc_icontrol->istat0;
514     		goto clear_and_return;
515     	}
516     
517     clear_and_return:
518     	/* Clear interrupt */
519     	udelay(2);
520     	info->zs_channel->control = RES_H_IUS;
521     	junk = ioc_icontrol->istat0;
522     	return;
523     }
524     
525     static _INLINE_ void status_handle(struct sgi_serial *info)
526     {
527     	volatile unsigned char junk;
528     	unsigned char status;
529     
530     	/* Get status from Read Register 0 */
531     	udelay(2);
532     	status = info->zs_channel->control;
533     	junk = ioc_icontrol->istat0;
534     	/* Clear status condition... */
535     	udelay(2);
536     	info->zs_channel->control = RES_EXT_INT;
537     	junk = ioc_icontrol->istat0;
538     	/* Clear the interrupt */
539     	udelay(2);
540     	info->zs_channel->control = RES_H_IUS;
541     	junk = ioc_icontrol->istat0;
542     
543     #if 0
544     	if(status & DCD) {
545     		if((info->tty->termios->c_cflag & CRTSCTS) &&
546     		   ((info->curregs[3] & AUTO_ENAB)==0)) {
547     			info->curregs[3] |= AUTO_ENAB;
548     			info->pendregs[3] |= AUTO_ENAB;
549     			write_zsreg(info->zs_channel, 3, info->curregs[3]);
550     		}
551     	} else {
552     		if((info->curregs[3] & AUTO_ENAB)) {
553     			info->curregs[3] &= ~AUTO_ENAB;
554     			info->pendregs[3] &= ~AUTO_ENAB;
555     			write_zsreg(info->zs_channel, 3, info->curregs[3]);
556     		}
557     	}
558     #endif
559     	/* Whee, if this is console input and this is a
560     	 * 'break asserted' status change interrupt, call
561     	 * the boot prom.
562     	 */
563     	if((status & BRK_ABRT) && info->break_abort)
564     		batten_down_hatches();
565     
566     	/* XXX Whee, put in a buffer somewhere, the status information
567     	 * XXX whee whee whee... Where does the information go...
568     	 */
569     	return;
570     }
571     
572     /*
573      * This is the serial driver's generic interrupt routine
574      */
575     void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
576     {
577     	struct sgi_serial * info = (struct sgi_serial *) dev_id;
578     	unsigned char zs_intreg;
579     
580     	zs_intreg = read_zsreg(info->zs_next->zs_channel, 3);
581     
582     	/* NOTE: The read register 3, which holds the irq status,
583     	 *       does so for both channels on each chip.  Although
584     	 *       the status value itself must be read from the A
585     	 *       channel and is only valid when read from channel A.
586     	 *       Yes... broken hardware...
587     	 */
588     #define CHAN_A_IRQMASK (CHARxIP | CHATxIP | CHAEXT)
589     #define CHAN_B_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
590     
591     	/* *** Chip 1 *** */
592     	/* Channel B -- /dev/ttyb, could be the console */
593     	if(zs_intreg & CHAN_B_IRQMASK) {
594     		if (zs_intreg & CHBRxIP)
595     			receive_chars(info, regs);
596     		if (zs_intreg & CHBTxIP)
597     			transmit_chars(info);
598     		if (zs_intreg & CHBEXT)
599     			status_handle(info);
600     	}
601     
602     	info=info->zs_next;
603     
604     	/* Channel A -- /dev/ttya, could be the console */
605     	if(zs_intreg & CHAN_A_IRQMASK) {
606     		if (zs_intreg & CHARxIP)
607     			receive_chars(info, regs);
608     		if (zs_intreg & CHATxIP)
609     			transmit_chars(info);
610     		if (zs_intreg & CHAEXT)
611     			status_handle(info);
612     	}
613     }
614     
615     /*
616      * -------------------------------------------------------------------
617      * Here ends the serial interrupt routines.
618      * -------------------------------------------------------------------
619      */
620     
621     /*
622      * This routine is used to handle the "bottom half" processing for the
623      * serial driver, known also the "software interrupt" processing.
624      * This processing is done at the kernel interrupt level, after the
625      * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
626      * is where time-consuming activities which can not be done in the
627      * interrupt driver proper are done; the interrupt driver schedules
628      * them using rs_sched_event(), and they get done here.
629      */
630     static void do_serial_bh(void)
631     {
632     	run_task_queue(&tq_serial);
633     }
634     
635     static void do_softint(void *private_)
636     {
637     	struct sgi_serial	*info = (struct sgi_serial *) private_;
638     	struct tty_struct	*tty;
639     	
640     	tty = info->tty;
641     	if (!tty)
642     		return;
643     
644     	if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
645     		if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
646     		    tty->ldisc.write_wakeup)
647     			(tty->ldisc.write_wakeup)(tty);
648     		wake_up_interruptible(&tty->write_wait);
649     	}
650     }
651     
652     /*
653      * This routine is called from the scheduler tqueue when the interrupt
654      * routine has signalled that a hangup has occurred.  The path of
655      * hangup processing is:
656      *
657      * 	serial interrupt routine -> (scheduler tqueue) ->
658      * 	do_serial_hangup() -> tty->hangup() -> rs_hangup()
659      * 
660      */
661     static void do_serial_hangup(void *private_)
662     {
663     	struct sgi_serial	*info = (struct sgi_serial *) private_;
664     	struct tty_struct	*tty;
665     	
666     	tty = info->tty;
667     	if (!tty)
668     		return;
669     
670     	tty_hangup(tty);
671     }
672     
673     
674     static int startup(struct sgi_serial * info)
675     {
676     	volatile unsigned char junk;
677     	unsigned long flags;
678     
679     	if (info->flags & ZILOG_INITIALIZED)
680     		return 0;
681     
682     	if (!info->xmit_buf) {
683     		info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
684     		if (!info->xmit_buf)
685     			return -ENOMEM;
686     	}
687     
688     	save_flags(flags); cli();
689     
690     #ifdef SERIAL_DEBUG_OPEN
691     	printk("starting up ttys%d (irq %d)...\n", info->line, info->irq);
692     #endif
693     
694     	/*
695     	 * Clear the FIFO buffers and disable them
696     	 * (they will be reenabled in change_speed())
697     	 */
698     	ZS_CLEARFIFO(info->zs_channel);
699     	info->xmit_fifo_size = 1;
700     
701     	/*
702     	 * Clear the interrupt registers.
703     	 */
704     	udelay(2);
705     	info->zs_channel->control = ERR_RES;
706     	junk = ioc_icontrol->istat0;
707     	udelay(2);
708     	info->zs_channel->control = RES_H_IUS;
709     	junk = ioc_icontrol->istat0;
710     
711     	/*
712     	 * Now, initialize the Zilog
713     	 */
714     	zs_rtsdtr(info, 1);
715     
716     	/*
717     	 * Finally, enable sequencing and interrupts
718     	 */
719     	info->curregs[1] |= (info->curregs[1] & ~0x18) | (EXT_INT_ENAB|INT_ALL_Rx);
720     	info->pendregs[1] = info->curregs[1];
721     	info->curregs[3] |= (RxENABLE | Rx8);
722     	info->pendregs[3] = info->curregs[3];
723     	/* We enable Tx interrupts as needed. */
724     	info->curregs[5] |= (TxENAB | Tx8);
725     	info->pendregs[5] = info->curregs[5];
726     	info->curregs[9] |= (NV | MIE);
727     	info->pendregs[9] = info->curregs[9];
728     	write_zsreg(info->zs_channel, 3, info->curregs[3]);
729     	write_zsreg(info->zs_channel, 5, info->curregs[5]);
730     	write_zsreg(info->zs_channel, 9, info->curregs[9]);
731     	
732     	/*
733     	 * And clear the interrupt registers again for luck.
734     	 */
735     	udelay(2);
736     	info->zs_channel->control = ERR_RES;
737     	junk = ioc_icontrol->istat0;
738     	udelay(2);
739     	info->zs_channel->control = RES_H_IUS;
740     	junk = ioc_icontrol->istat0;
741     
742     	if (info->tty)
743     		clear_bit(TTY_IO_ERROR, &info->tty->flags);
744     	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
745     
746     	/*
747     	 * and set the speed of the serial port
748     	 */
749     	change_speed(info);
750     
751     	info->flags |= ZILOG_INITIALIZED;
752     	restore_flags(flags);
753     	return 0;
754     }
755     
756     /*
757      * This routine will shutdown a serial port; interrupts are disabled, and
758      * DTR is dropped if the hangup on close termio flag is on.
759      */
760     static void shutdown(struct sgi_serial * info)
761     {
762     	unsigned long	flags;
763     
764     	if (!(info->flags & ZILOG_INITIALIZED))
765     		return;
766     
767     #ifdef SERIAL_DEBUG_OPEN
768     	printk("Shutting down serial port %d (irq %d)....", info->line,
769     	       info->irq);
770     #endif
771     	
772     	save_flags(flags); cli(); /* Disable interrupts */
773     	
774     	if (info->xmit_buf) {
775     		free_page((unsigned long) info->xmit_buf);
776     		info->xmit_buf = 0;
777     	}
778     
779     	if (info->tty)
780     		set_bit(TTY_IO_ERROR, &info->tty->flags);
781     	
782     	info->flags &= ~ZILOG_INITIALIZED;
783     	restore_flags(flags);
784     }
785     
786     /*
787      * This routine is called to set the UART divisor registers to match
788      * the specified baud rate for a serial port.
789      */
790     static void change_speed(struct sgi_serial *info)
791     {
792     	unsigned short port;
793     	unsigned cflag;
794     	int	i;
795     	int	brg;
796     
797     	if (!info->tty || !info->tty->termios)
798     		return;
799     	cflag = info->tty->termios->c_cflag;
800     	if (!(port = info->port))
801     		return;
802     	i = cflag & CBAUD;
803     	if (i & CBAUDEX) {
804     		/* XXX CBAUDEX is not obeyed.
805     		 * It is impossible at a 32bits SPARC.
806     		 * But we have to report this to user ... someday.
807     		 */
808     		i = B9600;
809     	}
810     	if (i == 0) {
811     		/* XXX B0, hangup the line. */
812     		do_serial_hangup(info);
813     	} else if (baud_table[i]) {
814     		info->zs_baud = baud_table[i];
815     		info->clk_divisor = 16;
816     
817     		info->curregs[4] = X16CLK;
818     		info->curregs[11] = TCBR | RCBR;
819     		brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
820     		info->curregs[12] = (brg & 255);
821     		info->curregs[13] = ((brg >> 8) & 255);
822     		info->curregs[14] = BRENABL;
823     	}
824     
825     	/* byte size and parity */
826     	switch (cflag & CSIZE) {
827     	case CS5:
828     		info->curregs[3] &= ~(0xc0);
829     		info->curregs[3] |= Rx5;
830     		info->pendregs[3] = info->curregs[3];
831     		info->curregs[5] &= ~(0xe0);
832     		info->curregs[5] |= Tx5;
833     		info->pendregs[5] = info->curregs[5];
834     		break;
835     	case CS6:
836     		info->curregs[3] &= ~(0xc0);
837     		info->curregs[3] |= Rx6;
838     		info->pendregs[3] = info->curregs[3];
839     		info->curregs[5] &= ~(0xe0);
840     		info->curregs[5] |= Tx6;
841     		info->pendregs[5] = info->curregs[5];
842     		break;
843     	case CS7:
844     		info->curregs[3] &= ~(0xc0);
845     		info->curregs[3] |= Rx7;
846     		info->pendregs[3] = info->curregs[3];
847     		info->curregs[5] &= ~(0xe0);
848     		info->curregs[5] |= Tx7;
849     		info->pendregs[5] = info->curregs[5];
850     		break;
851     	case CS8:
852     	default: /* defaults to 8 bits */
853     		info->curregs[3] &= ~(0xc0);
854     		info->curregs[3] |= Rx8;
855     		info->pendregs[3] = info->curregs[3];
856     		info->curregs[5] &= ~(0xe0);
857     		info->curregs[5] |= Tx8;
858     		info->pendregs[5] = info->curregs[5];
859     		break;
860     	}
861     	info->curregs[4] &= ~(0x0c);
862     	if (cflag & CSTOPB) {
863     		info->curregs[4] |= SB2;
864     	} else {
865     		info->curregs[4] |= SB1;
866     	}
867     	info->pendregs[4] = info->curregs[4];
868     	if (cflag & PARENB) {
869     		info->curregs[4] |= PAR_ENA;
870     		info->pendregs[4] |= PAR_ENA;
871     	} else {
872     		info->curregs[4] &= ~PAR_ENA;
873     		info->pendregs[4] &= ~PAR_ENA;
874     	}
875     	if (!(cflag & PARODD)) {
876     		info->curregs[4] |= PAR_EVEN;
877     		info->pendregs[4] |= PAR_EVEN;
878     	} else {
879     		info->curregs[4] &= ~PAR_EVEN;
880     		info->pendregs[4] &= ~PAR_EVEN;
881     	}
882     
883     	/* Load up the new values */
884     	load_zsregs(info->zs_channel, info->curregs);
885     
886     	return;
887     }
888     
889     /* This is for console output over ttya/ttyb */
890     static void zs_cons_put_char(char ch)
891     {
892     	struct sgi_zschannel *chan = zs_conschan;
893     	volatile unsigned char junk;
894     	int flags, loops = 0;
895     
896     	save_flags(flags); cli();
897     	while(((junk = chan->control) & Tx_BUF_EMP)==0 && loops < 10000) {
898     		loops++;
899     		udelay(2);
900     	}
901     
902     	udelay(2);
903     	chan->data = ch;
904     	junk = ioc_icontrol->istat0;
905     	restore_flags(flags);
906     }
907     
908     /* 
909      * This is the more generic put_char function for the driver.
910      * In earlier versions of this driver, "rs_put_char" was the
911      * name of the console-specific fucntion, now called zs_cons_put_char
912      */
913     
914     static void rs_put_char(struct tty_struct *tty, char ch)
915     {
916     	struct sgi_zschannel *chan = 
917     		((struct sgi_serial *)tty->driver_data)->zs_channel;
918     	volatile unsigned char junk;
919     	int flags, loops = 0;
920     
921     	save_flags(flags); cli();
922     	while(((junk = chan->control) & Tx_BUF_EMP)==0 && loops < 10000) {
923     		loops++;
924     		udelay(2);
925     	}
926     
927     	udelay(2);
928     	chan->data = ch;
929     	junk = ioc_icontrol->istat0;
930     	restore_flags(flags);
931     }
932     
933     /* These are for receiving and sending characters under the kgdb
934      * source level kernel debugger.
935      */
936     int putDebugChar(char kgdb_char)
937     {
938     	struct sgi_zschannel *chan = zs_kgdbchan;
939     	volatile unsigned char junk;
940     	unsigned long flags;
941     
942     	save_flags(flags); cli();
943     	udelay(2);
944     	while((chan->control & Tx_BUF_EMP)==0)
945     		udelay(2);
946     
947     	udelay(2);
948     	chan->data = kgdb_char;
949     	junk = ioc_icontrol->istat0;
950     	restore_flags(flags);
951     
952     	return 1;
953     }
954     
955     char getDebugChar(void)
956     {
957     	struct sgi_zschannel *chan = zs_kgdbchan;
958     	unsigned char junk;
959     
960     	while((chan->control & Rx_CH_AV)==0)
961     		udelay(2);
962     
963     	junk = ioc_icontrol->istat0;
964     	udelay(2);
965     	return chan->data;
966     }
967     
968     /*
969      * Fair output driver allows a process to speak.
970      */
971     static void rs_fair_output(void)
972     {
973     	int left;		/* Output no more than that */
974     	unsigned long flags;
975     	struct sgi_serial *info = zs_consinfo;
976     	volatile unsigned char junk;
977     	char c;
978     
979     	if (info == 0) return;
980     	if (info->xmit_buf == 0) return;
981     
982     	save_flags(flags);  cli();
983     	left = info->xmit_cnt;
984     	while (left != 0) {
985     		c = info->xmit_buf[info->xmit_tail];
986     		info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
987     		info->xmit_cnt--;
988     		restore_flags(flags);
989     
990     		zs_cons_put_char(c);
991     
992     		save_flags(flags);  cli();
993     		left = MIN(info->xmit_cnt, left-1);
994     	}
995     
996     	/* Last character is being transmitted now (hopefully). */
997     	udelay(2);
998     	zs_conschan->control = RES_Tx_P;
999     	junk = ioc_icontrol->istat0;
1000     
1001     	restore_flags(flags);
1002     	return;
1003     }
1004     
1005     
1006     static int rs_write(struct tty_struct * tty, int from_user,
1007     		    const unsigned char *buf, int count)
1008     {
1009     	int	c, total = 0;
1010     	struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1011     	unsigned long flags;
1012     
1013     	if (serial_paranoia_check(info, tty->device, "rs_write"))
1014     		return 0;
1015     
1016     	if (!tty || !info->xmit_buf)
1017     		return 0;
1018     
1019     	save_flags(flags);
1020     	while (1) {
1021     		cli();		
1022     		c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1023     				   SERIAL_XMIT_SIZE - info->xmit_head));
1024     		if (c <= 0)
1025     			break;
1026     
1027     		if (from_user) {
1028     			down(&tmp_buf_sem);
1029     			copy_from_user(tmp_buf, buf, c);
1030     			c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1031     				       SERIAL_XMIT_SIZE - info->xmit_head));
1032     			memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1033     			up(&tmp_buf_sem);
1034     		} else
1035     			memcpy(info->xmit_buf + info->xmit_head, buf, c);
1036     		info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1037     		info->xmit_cnt += c;
1038     		restore_flags(flags);
1039     		buf += c;
1040     		count -= c;
1041     		total += c;
1042     	}
1043     
1044     	if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
1045     	/*
1046     	 * The above test used to include the condition
1047      	 * "&& !(info->curregs[5] & TxENAB)", but there
1048     	 * is reason to suspect that it is never statisfied
1049     	 * when the port is running.  The problem may in fact
1050     	 * have been masked by the fact that, if O_POST is set,
1051     	 * there is always a rs_flush_xx operation following the
1052     	 * rs_write, and the flush ignores that condition when
1053     	 * it kicks off the transmit.
1054     	 */
1055     		/* Enable transmitter */
1056     		info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1057     		info->pendregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1058     		write_zsreg(info->zs_channel, 1, info->curregs[1]);
1059     		info->curregs[5] |= TxENAB;
1060     		info->pendregs[5] |= TxENAB;
1061     		write_zsreg(info->zs_channel, 5, info->curregs[5]);
1062     
1063     	/*
1064     	 * The following code is imported from the 2.3.6 Sun sbus zs.c
1065     	 * driver, of which an earlier version served as the basis
1066     	 * for sgiserial.c.  Perhaps due to changes over time in
1067     	 * the line discipline code, ns_write()s with from_user
1068     	 * set would not otherwise actually kick-off output in
1069     	 * Linux 2.2.x or later.  Maybe it never really worked.
1070     	 */
1071     
1072     		rs_put_char(tty, info->xmit_buf[info->xmit_tail++]);
1073                     info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1074                     info->xmit_cnt--;
1075     	}
1076     
1077     	restore_flags(flags);
1078     	return total;
1079     }
1080     
1081     static int rs_write_room(struct tty_struct *tty)
1082     {
1083     	struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1084     	int	ret;
1085     				
1086     	if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1087     		return 0;
1088     	ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1089     	if (ret < 0)
1090     		ret = 0;
1091     	return ret;
1092     }
1093     
1094     static int rs_chars_in_buffer(struct tty_struct *tty)
1095     {
1096     	struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1097     				
1098     	if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1099     		return 0;
1100     	return info->xmit_cnt;
1101     }
1102     
1103     static void rs_flush_buffer(struct tty_struct *tty)
1104     {
1105     	struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1106     				
1107     	if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1108     		return;
1109     	cli();
1110     	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1111     	sti();
1112     	wake_up_interruptible(&tty->write_wait);
1113     	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1114     	    tty->ldisc.write_wakeup)
1115     		(tty->ldisc.write_wakeup)(tty);
1116     }
1117     
1118     static void rs_flush_chars(struct tty_struct *tty)
1119     {
1120     	struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1121     	unsigned long flags;
1122     
1123     	if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1124     		return;
1125     
1126     	if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1127     	    !info->xmit_buf)
1128     		return;
1129     
1130     	/* Enable transmitter */
1131     	save_flags(flags); cli();
1132     	info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1133     	info->pendregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1134     	write_zsreg(info->zs_channel, 1, info->curregs[1]);
1135     	info->curregs[5] |= TxENAB;
1136     	info->pendregs[5] |= TxENAB;
1137     	write_zsreg(info->zs_channel, 5, info->curregs[5]);
1138     
1139     	/*
1140     	 * Send a first (bootstrapping) character. A best solution is
1141     	 * to call transmit_chars() here which handles output in a
1142     	 * generic way. Current transmit_chars() not only transmits,
1143     	 * but resets interrupts also what we do not desire here.
1144     	 * XXX Discuss with David.
1145     	 */
1146     	if (info->zs_channel->control & Tx_BUF_EMP) {
1147     		volatile unsigned char junk;
1148     
1149     		/* Send char */
1150     		udelay(2);
1151     		info->zs_channel->data = info->xmit_buf[info->xmit_tail++];
1152     		junk = ioc_icontrol->istat0;
1153     		info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1154     		info->xmit_cnt--;
1155     	}
1156     	restore_flags(flags);
1157     }
1158     
1159     /*
1160      * ------------------------------------------------------------
1161      * rs_throttle()
1162      * 
1163      * This routine is called by the upper-layer tty layer to signal that
1164      * incoming characters should be throttled.
1165      * ------------------------------------------------------------
1166      */
1167     static void rs_throttle(struct tty_struct * tty)
1168     {
1169     	struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1170     #ifdef SERIAL_DEBUG_THROTTLE
1171     	char	buf[64];
1172     	
1173     	printk("throttle %s: %d....\n", _tty_name(tty, buf),
1174     	       tty->ldisc.chars_in_buffer(tty));
1175     #endif
1176     
1177     	if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1178     		return;
1179     	
1180     	if (I_IXOFF(tty))
1181     		info->x_char = STOP_CHAR(tty);
1182     
1183     	/* Turn off RTS line */
1184     	cli();
1185     	info->curregs[5] &= ~RTS;
1186     	info->pendregs[5] &= ~RTS;
1187     	write_zsreg(info->zs_channel, 5, info->curregs[5]);
1188     	sti();
1189     }
1190     
1191     static void rs_unthrottle(struct tty_struct * tty)
1192     {
1193     	struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1194     #ifdef SERIAL_DEBUG_THROTTLE
1195     	char	buf[64];
1196     	
1197     	printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1198     	       tty->ldisc.chars_in_buffer(tty));
1199     #endif
1200     
1201     	if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1202     		return;
1203     	
1204     	if (I_IXOFF(tty)) {
1205     		if (info->x_char)
1206     			info->x_char = 0;
1207     		else
1208     			info->x_char = START_CHAR(tty);
1209     	}
1210     
1211     	/* Assert RTS line */
1212     	cli();
1213     	info->curregs[5] |= RTS;
1214     	info->pendregs[5] |= RTS;
1215     	write_zsreg(info->zs_channel, 5, info->curregs[5]);
1216     	sti();
1217     }
1218     
1219     /*
1220      * ------------------------------------------------------------
1221      * rs_ioctl() and friends
1222      * ------------------------------------------------------------
1223      */
1224     
1225     static int get_serial_info(struct sgi_serial * info,
1226     			   struct serial_struct * retinfo)
1227     {
1228     	struct serial_struct tmp;
1229       
1230     	if (!retinfo)
1231     		return -EFAULT;
1232     	memset(&tmp, 0, sizeof(tmp));
1233     	tmp.type = info->type;
1234     	tmp.line = info->line;
1235     	tmp.port = info->port;
1236     	tmp.irq = info->irq;
1237     	tmp.flags = info->flags;
1238     	tmp.baud_base = info->baud_base;
1239     	tmp.close_delay = info->close_delay;
1240     	tmp.closing_wait = info->closing_wait;
1241     	tmp.custom_divisor = info->custom_divisor;
1242     	return copy_to_user(retinfo,&tmp,sizeof(*retinfo));
1243     }
1244     
1245     static int set_serial_info(struct sgi_serial * info,
1246     			   struct serial_struct * new_info)
1247     {
1248     	struct serial_struct new_serial;
1249     	struct sgi_serial old_info;
1250     	int 			retval = 0;
1251     
1252     	if (!new_info)
1253     		return -EFAULT;
1254     	copy_from_user(&new_serial,new_info,sizeof(new_serial));
1255     	old_info = *info;
1256     
1257     	if (!capable(CAP_SYS_ADMIN)) {
1258     		if ((new_serial.baud_base != info->baud_base) ||
1259     		    (new_serial.type != info->type) ||
1260     		    (new_serial.close_delay != info->close_delay) ||
1261     		    ((new_serial.flags & ~ZILOG_USR_MASK) !=
1262     		     (info->flags & ~ZILOG_USR_MASK)))
1263     			return -EPERM;
1264     		info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1265     			       (new_serial.flags & ZILOG_USR_MASK));
1266     		info->custom_divisor = new_serial.custom_divisor;
1267     		goto check_and_exit;
1268     	}
1269     
1270     	if (info->count > 1)
1271     		return -EBUSY;
1272     
1273     	/*
1274     	 * OK, past this point, all the error checking has been done.
1275     	 * At this point, we start making changes.....
1276     	 */
1277     
1278     	info->baud_base = new_serial.baud_base;
1279     	info->flags = ((info->flags & ~ZILOG_FLAGS) |
1280     			(new_serial.flags & ZILOG_FLAGS));
1281     	info->type = new_serial.type;
1282     	info->close_delay = new_serial.close_delay;
1283     	info->closing_wait = new_serial.closing_wait;
1284     
1285     check_and_exit:
1286     	retval = startup(info);
1287     	return retval;
1288     }
1289     
1290     /*
1291      * get_lsr_info - get line status register info
1292      *
1293      * Purpose: Let user call ioctl() to get info when the UART physically
1294      * 	    is emptied.  On bus types like RS485, the transmitter must
1295      * 	    release the bus after transmitting. This must be done when
1296      * 	    the transmit shift register is empty, not be done when the
1297      * 	    transmit holding register is empty.  This functionality
1298      * 	    allows an RS485 driver to be written in user space. 
1299      */
1300     static int get_lsr_info(struct sgi_serial * info, unsigned int *value)
1301     {
1302     	volatile unsigned char junk;
1303     	unsigned char status;
1304     
1305     	cli();
1306     	udelay(2);
1307     	status = info->zs_channel->control;
1308     	junk = ioc_icontrol->istat0;
1309     	sti();
1310     	return put_user(status,value);
1311     } 
1312     
1313     static int get_modem_info(struct sgi_serial * info, unsigned int *value)
1314     {
1315     	unsigned char status;
1316     	unsigned int result;
1317     
1318     	cli();
1319     	status = info->zs_channel->control;
1320     	udelay(2);
1321     	sti();
1322     	result =  ((info->curregs[5] & RTS) ? TIOCM_RTS : 0)
1323     		| ((info->curregs[5] & DTR) ? TIOCM_DTR : 0)
1324     		| ((status  & DCD) ? TIOCM_CAR : 0)
1325     		| ((status  & SYNC) ? TIOCM_DSR : 0)
1326     		| ((status  & CTS) ? TIOCM_CTS : 0);
1327     	if (put_user(result, value))
1328     		return -EFAULT;
1329     	return 0;
1330     }
1331     
1332     static int set_modem_info(struct sgi_serial * info, unsigned int cmd,
1333     			  unsigned int *value)
1334     {
1335     	unsigned int arg;
1336     
1337     	if (get_user(arg, value))
1338     		return -EFAULT;
1339     	switch (cmd) {
1340     	case TIOCMBIS: 
1341     		if (arg & TIOCM_RTS)
1342     			info->curregs[5] |= RTS;
1343     		if (arg & TIOCM_DTR)
1344     			info->curregs[5] |= DTR;
1345     		break;
1346     	case TIOCMBIC:
1347     		if (arg & TIOCM_RTS)
1348     			info->curregs[5] &= ~RTS;
1349     		if (arg & TIOCM_DTR)
1350     			info->curregs[5] &= ~DTR;
1351     		break;
1352     	case TIOCMSET:
1353     		info->curregs[5] = ((info->curregs[5] & ~(RTS | DTR))
1354     			     | ((arg & TIOCM_RTS) ? RTS : 0)
1355     			     | ((arg & TIOCM_DTR) ? DTR : 0));
1356     		break;
1357     	default:
1358     		return -EINVAL;
1359     	}
1360     	cli();
1361     	write_zsreg(info->zs_channel, 5, info->curregs[5]);
1362     	sti();
1363     	return 0;
1364     }
1365     
1366     /*
1367      * This routine sends a break character out the serial port.
1368      */
1369     static void send_break(	struct sgi_serial * info, int duration)
1370     {
1371     	if (!info->port)
1372     		return;
1373     	current->state = TASK_INTERRUPTIBLE;
1374     	cli();
1375     	write_zsreg(info->zs_channel, 5, (info->curregs[5] | SND_BRK));
1376     	schedule_timeout(duration);
1377     	write_zsreg(info->zs_channel, 5, info->curregs[5]);
1378     	sti();
1379     }
1380     
1381     static int rs_ioctl(struct tty_struct *tty, struct file * file,
1382     		    unsigned int cmd, unsigned long arg)
1383     {
1384     	struct sgi_serial * info = (struct sgi_serial *) tty->driver_data;
1385     	int retval;
1386     
1387     	if (serial_paranoia_check(info, tty->device, "zs_ioctl"))
1388     		return -ENODEV;
1389     
1390     	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1391     	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1392     	    (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1393     		if (tty->flags & (1 << TTY_IO_ERROR))
1394     		    return -EIO;
1395     	}
1396     	
1397     	switch (cmd) {
1398     		case TCSBRK:	/* SVID version: non-zero arg --> no break */
1399     			retval = tty_check_change(tty);
1400     			if (retval)
1401     				return retval;
1402     			tty_wait_until_sent(tty, 0);
1403     			if (!arg)
1404     				send_break(info, HZ/4);	/* 1/4 second */
1405     			return 0;
1406     		case TCSBRKP:	/* support for POSIX tcsendbreak() */
1407     			retval = tty_check_change(tty);
1408     			if (retval)
1409     				return retval;
1410     			tty_wait_until_sent(tty, 0);
1411     			send_break(info, arg ? arg*(HZ/10) : HZ/4);
1412     			return 0;
1413     		case TIOCGSOFTCAR:
1414     			if (put_user(C_CLOCAL(tty) ? 1 : 0,
1415     				     (unsigned long *) arg))
1416     				return -EFAULT;
1417     			return 0;
1418     		case TIOCSSOFTCAR:
1419     			if (get_user(arg, (unsigned long *) arg))
1420     				return -EFAULT;
1421     			tty->termios->c_cflag =
1422     				((tty->termios->c_cflag & ~CLOCAL) |
1423     				 (arg ? CLOCAL : 0));
1424     			return 0;
1425     		case TIOCMGET:
1426     			return get_modem_info(info, (unsigned int *) arg);
1427     		case TIOCMBIS:
1428     		case TIOCMBIC:
1429     		case TIOCMSET:
1430     			return set_modem_info(info, cmd, (unsigned int *) arg);
1431     		case TIOCGSERIAL:
1432     			return get_serial_info(info,
1433     					       (struct serial_struct *) arg);
1434     		case TIOCSSERIAL:
1435     			return set_serial_info(info,
1436     					       (struct serial_struct *) arg);
1437     		case TIOCSERGETLSR: /* Get line status register */
1438     			return get_lsr_info(info, (unsigned int *) arg);
1439     
1440     		case TIOCSERGSTRUCT:
1441     			if (copy_to_user((struct sgi_serial *) arg,
1442     				    info, sizeof(struct sgi_serial)))
1443     				return -EFAULT;
1444     			return 0;
1445     			
1446     		default:
1447     			return -ENOIOCTLCMD;
1448     		}
1449     	return 0;
1450     }
1451     
1452     static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1453     {
1454     	struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1455     
1456     	if (tty->termios->c_cflag == old_termios->c_cflag)
1457     		return;
1458     
1459     	change_speed(info);
1460     
1461     	if ((old_termios->c_cflag & CRTSCTS) &&
1462     	    !(tty->termios->c_cflag & CRTSCTS)) {
1463     		tty->hw_stopped = 0;
1464     		rs_start(tty);
1465     	}
1466     }
1467     
1468     /*
1469      * ------------------------------------------------------------
1470      * rs_close()
1471      * 
1472      * This routine is called when the serial port gets closed.  First, we
1473      * wait for the last remaining data to be sent.  Then, we unlink its
1474      * ZILOG structure from the interrupt chain if necessary, and we free
1475      * that IRQ if nothing is left in the chain.
1476      * ------------------------------------------------------------
1477      */
1478     static void rs_close(struct tty_struct *tty, struct file * filp)
1479     {
1480     	struct sgi_serial * info = (struct sgi_serial *)tty->driver_data;
1481     	unsigned long flags;
1482     
1483     	if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1484     		return;
1485     	
1486     	save_flags(flags); cli();
1487     	
1488     	if (tty_hung_up_p(filp)) {
1489     		restore_flags(flags);
1490     		return;
1491     	}
1492     	
1493     #ifdef SERIAL_DEBUG_OPEN
1494     	printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1495     #endif
1496     	if ((tty->count == 1) && (info->count != 1)) {
1497     		/*
1498     		 * Uh, oh.  tty->count is 1, which means that the tty
1499     		 * structure will be freed.  Info->count should always
1500     		 * be one in these conditions.  If it's greater than
1501     		 * one, we've got real problems, since it means the
1502     		 * serial port won't be shutdown.
1503     		 */
1504     		printk("rs_close: bad serial port count; tty->count is 1, "
1505     		       "info->count is %d\n", info->count);
1506     		info->count = 1;
1507     	}
1508     	if (--info->count < 0) {
1509     		printk("rs_close: bad serial port count for ttys%d: %d\n",
1510     		       info->line, info->count);
1511     		info->count = 0;
1512     	}
1513     	if (info->count) {
1514     		restore_flags(flags);
1515     		return;
1516     	}
1517     	info->flags |= ZILOG_CLOSING;
1518     	/*
1519     	 * Save the termios structure, since this port may have
1520     	 * separate termios for callout and dialin.
1521     	 */
1522     	if (info->flags & ZILOG_NORMAL_ACTIVE)
1523     		info->normal_termios = *tty->termios;
1524     	if (info->flags & ZILOG_CALLOUT_ACTIVE)
1525     		info->callout_termios = *tty->termios;
1526     	/*
1527     	 * Now we wait for the transmit buffer to clear; and we notify 
1528     	 * the line discipline to only process XON/XOFF characters.
1529     	 */
1530     	tty->closing = 1;
1531     	if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1532     		tty_wait_until_sent(tty, info->closing_wait);
1533     	/*
1534     	 * At this point we stop accepting input.  To do this, we
1535     	 * disable the receive line status interrupts, and tell the
1536     	 * interrupt driver to stop checking the data ready bit in the
1537     	 * line status register.
1538     	 */
1539     	/** if (!info->iscons) ... **/
1540     	info->curregs[3] &= ~RxENABLE;
1541     	info->pendregs[3] = info->curregs[3];
1542     	write_zsreg(info->zs_channel, 3, info->curregs[3]);
1543     	info->curregs[1] &= ~(0x18);
1544     	info->pendregs[1] = info->curregs[1];
1545     	write_zsreg(info->zs_channel, 1, info->curregs[1]);
1546     	ZS_CLEARFIFO(info->zs_channel);
1547     
1548     	shutdown(info);
1549     	if (tty->driver.flush_buffer)
1550     		tty->driver.flush_buffer(tty);
1551     	if (tty->ldisc.flush_buffer)
1552     		tty->ldisc.flush_buffer(tty);
1553     	tty->closing = 0;
1554     	info->event = 0;
1555     	info->tty = 0;
1556     	if (tty->ldisc.num != ldiscs[N_TTY].num) {
1557     		if (tty->ldisc.close)
1558     			(tty->ldisc.close)(tty);
1559     		tty->ldisc = ldiscs[N_TTY];
1560     		tty->termios->c_line = N_TTY;
1561     		if (tty->ldisc.open)
1562     			(tty->ldisc.open)(tty);
1563     	}
1564     	if (info->blocked_open) {
1565     		if (info->close_delay) {
1566     			current->state = TASK_INTERRUPTIBLE;
1567     			schedule_timeout(info->close_delay);
1568     		}
1569     		wake_up_interruptible(&info->open_wait);
1570     	}
1571     	info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1572     			 ZILOG_CLOSING);
1573     	wake_up_interruptible(&info->close_wait);
1574     	restore_flags(flags);
1575     }
1576     
1577     /*
1578      * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1579      */
1580     void rs_hangup(struct tty_struct *tty)
1581     {
1582     	struct sgi_serial * info = (struct sgi_serial *)tty->driver_data;
1583     	
1584     	if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1585     		return;
1586     	
1587     	rs_flush_buffer(tty);
1588     	shutdown(info);
1589     	info->event = 0;
1590     	info->count = 0;
1591     	info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1592     	info->tty = 0;
1593     	wake_up_interruptible(&info->open_wait);
1594     }
1595     
1596     /*
1597      * ------------------------------------------------------------
1598      * rs_open() and friends
1599      * ------------------------------------------------------------
1600      */
1601     static int block_til_ready(struct tty_struct *tty, struct file * filp,
1602     			   struct sgi_serial *info)
1603     {
1604     	DECLARE_WAITQUEUE(wait, current);
1605     	int		retval;
1606     	int		do_clocal = 0;
1607     
1608     	/*
1609     	 * If the device is in the middle of being closed, then block
1610     	 * until it's done, and then try again.
1611     	 */
1612     	if (info->flags & ZILOG_CLOSING) {
1613     		interruptible_sleep_on(&info->close_wait);
1614     #ifdef SERIAL_DO_RESTART
1615     		if (info->flags & ZILOG_HUP_NOTIFY)
1616     			return -EAGAIN;
1617     		else
1618     			return -ERESTARTSYS;
1619     #else
1620     		return -EAGAIN;
1621     #endif
1622     	}
1623     
1624     	/*
1625     	 * If this is a callout device, then just make sure the normal
1626     	 * device isn't being used.
1627     	 */
1628     	if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1629     		if (info->flags & ZILOG_NORMAL_ACTIVE)
1630     			return -EBUSY;
1631     		if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1632     		    (info->flags & ZILOG_SESSION_LOCKOUT) &&
1633     		    (info->session != current->session))
1634     		    return -EBUSY;
1635     		if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1636     		    (info->flags & ZILOG_PGRP_LOCKOUT) &&
1637     		    (info->pgrp != current->pgrp))
1638     		    return -EBUSY;
1639     		info->flags |= ZILOG_CALLOUT_ACTIVE;
1640     		return 0;
1641     	}
1642     	
1643     	/*
1644     	 * If non-blocking mode is set, or the port is not enabled,
1645     	 * then make the check up front and then exit.
1646     	 */
1647     	if ((filp->f_flags & O_NONBLOCK) ||
1648     	    (tty->flags & (1 << TTY_IO_ERROR))) {
1649     		if (info->flags & ZILOG_CALLOUT_ACTIVE)
1650     			return -EBUSY;
1651     		info->flags |= ZILOG_NORMAL_ACTIVE;
1652     		return 0;
1653     	}
1654     
1655     	if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1656     		if (info->normal_termios.c_cflag & CLOCAL)
1657     			do_clocal = 1;
1658     	} else {
1659     		if (tty->termios->c_cflag & CLOCAL)
1660     			do_clocal = 1;
1661     	}
1662     	
1663     	/*
1664     	 * Block waiting for the carrier detect and the line to become
1665     	 * free (i.e., not in use by the callout).  While we are in
1666     	 * this loop, info->count is dropped by one, so that
1667     	 * rs_close() knows when to free things.  We restore it upon
1668     	 * exit, either normal or abnormal.
1669     	 */
1670     	retval = 0;
1671     	add_wait_queue(&info->open_wait, &wait);
1672     #ifdef SERIAL_DEBUG_OPEN
1673     	printk("block_til_ready before block: ttys%d, count = %d\n",
1674     	       info->line, info->count);
1675     #endif
1676     	info->count--;
1677     	info->blocked_open++;
1678     	while (1) {
1679     		cli();
1680     		if (!(info->flags & ZILOG_CALLOUT_ACTIVE))
1681     			zs_rtsdtr(info, 1);
1682     		sti();
1683     		set_current_state(TASK_INTERRUPTIBLE);
1684     		if (tty_hung_up_p(filp) ||
1685     		    !(info->flags & ZILOG_INITIALIZED)) {
1686     #ifdef SERIAL_DO_RESTART
1687     			if (info->flags & ZILOG_HUP_NOTIFY)
1688     				retval = -EAGAIN;
1689     			else
1690     				retval = -ERESTARTSYS;	
1691     #else
1692     			retval = -EAGAIN;
1693     #endif
1694     			break;
1695     		}
1696     		if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1697     		    !(info->flags & ZILOG_CLOSING) && do_clocal)
1698     			break;
1699     		if (signal_pending(current)) {
1700     			retval = -ERESTARTSYS;
1701     			break;
1702     		}
1703     #ifdef SERIAL_DEBUG_OPEN
1704     		printk("block_til_ready blocking: ttys%d, count = %d\n",
1705     		       info->line, info->count);
1706     #endif
1707     		schedule();
1708     	}
1709     	current->state = TASK_RUNNING;
1710     	remove_wait_queue(&info->open_wait, &wait);
1711     	if (!tty_hung_up_p(filp))
1712     		info->count++;
1713     	info->blocked_open--;
1714     #ifdef SERIAL_DEBUG_OPEN
1715     	printk("block_til_ready after blocking: ttys%d, count = %d\n",
1716     	       info->line, info->count);
1717     #endif
1718     	if (retval)
1719     		return retval;
1720     	info->flags |= ZILOG_NORMAL_ACTIVE;
1721     	return 0;
1722     }	
1723     
1724     /*
1725      * This routine is called whenever a serial port is opened.  It
1726      * enables interrupts for a serial port, linking in its ZILOG structure into
1727      * the IRQ chain.   It also performs the serial-specific
1728      * initialization for the tty structure.
1729      */
1730     int rs_open(struct tty_struct *tty, struct file * filp)
1731     {
1732     	struct sgi_serial	*info;
1733     	int 			retval, line;
1734     
1735     	line = MINOR(tty->device) - tty->driver.minor_start;
1736     	/* The zilog lines for the mouse/keyboard must be
1737     	 * opened using their respective drivers.
1738     	 */
1739     	if ((line < 0) || (line >= NUM_CHANNELS))
1740     		return -ENODEV;
1741     	info = zs_soft + line;
1742     	/* Is the kgdb running over this line? */
1743     	if (info->kgdb_channel)
1744     		return -ENODEV;
1745     	if (serial_paranoia_check(info, tty->device, "rs_open"))
1746     		return -ENODEV;
1747     #ifdef SERIAL_DEBUG_OPEN
1748     	printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1749     	       info->count);
1750     #endif
1751     	info->count++;
1752     	tty->driver_data = info;
1753     	info->tty = tty;
1754     
1755     	/*
1756     	 * Start up serial port
1757     	 */
1758     	retval = startup(info);
1759     	if (retval)
1760     		return retval;
1761     
1762     	retval = block_til_ready(tty, filp, info);
1763     	if (retval) {
1764     #ifdef SERIAL_DEBUG_OPEN
1765     		printk("rs_open returning after block_til_ready with %d\n",
1766     		       retval);
1767     #endif
1768     		return retval;
1769     	}
1770     
1771     	if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1772     		if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1773     			*tty->termios = info->normal_termios;
1774     		else 
1775     			*tty->termios = info->callout_termios;
1776     		change_speed(info);
1777     	}
1778     
1779     	/* If this is the serial console change the speed to 
1780     	 * the right value
1781     	 */
1782     	if (info->is_cons) {
1783     		info->tty->termios->c_cflag = sgisercon->cflag;
1784     		change_speed(info);		
1785     	}
1786     
1787     	info->session = current->session;
1788     	info->pgrp = current->pgrp;
1789     
1790     #ifdef SERIAL_DEBUG_OPEN
1791     	printk("rs_open ttys%d successful...\n", info->line);
1792     #endif
1793     	return 0;
1794     }
1795     
1796     /* Finally, routines used to initialize the serial driver. */
1797     
1798     static void show_serial_version(void)
1799     {
1800     	printk("SGI Zilog8530 serial driver version 1.00\n");
1801     }
1802     
1803     /* Return layout for the requested zs chip number. */
1804     static inline struct sgi_zslayout *get_zs(int chip)
1805     {
1806     	extern struct hpc3_miscregs *hpc3mregs;
1807     
1808     	if (chip > 0)
1809     		panic("Wheee, bogus zs chip number requested.");
1810     
1811     	return (struct sgi_zslayout *) (&hpc3mregs->ser1cmd);
1812     }
1813     
1814     
1815     static inline void
1816     rs_cons_check(struct sgi_serial *ss, int channel)
1817     {
1818     	int i, o, io;
1819     	static int msg_printed = 0;
1820     
1821     	i = o = io = 0;
1822     
1823     	/* Is this one of the serial console lines? */
1824     	if((zs_cons_chanout != channel) &&
1825     	   (zs_cons_chanin != channel))
1826     		return;
1827     	zs_conschan = ss->zs_channel;
1828     	zs_consinfo = ss;
1829     
1830     
1831     
1832     	/* If this is console input, we handle the break received
1833     	 * status interrupt on this line to mean prom_halt().
1834     	 */
1835     	if(zs_cons_chanin == channel) {
1836     		ss->break_abort = 1;
1837     		i = 1;
1838     	}
1839     	if(o && i)
1840     		io = 1;
1841     
1842     	/* Set flag variable for this port so that it cannot be
1843     	 * opened for other uses by accident.
1844     	 */
1845     	ss->is_cons = 1;
1846     
1847     	if(io) {
1848     		if (!msg_printed) {
1849     			printk("zs%d: console I/O\n", ((channel>>1)&1));
1850     			msg_printed = 1;
1851     		}
1852     
1853     	} else {
1854     		printk("zs%d: console %s\n", ((channel>>1)&1),
1855     		       (i==1 ? "input" : (o==1 ? "output" : "WEIRD")));
1856     	}
1857     }
1858     
1859     volatile int test_done;
1860     
1861     /* rs_init inits the driver */
1862     int rs_init(void)
1863     {
1864     	int chip, channel, i, flags;
1865     	struct sgi_serial *info;
1866     
1867     
1868     	/* Setup base handler, and timer table. */
1869     	init_bh(SERIAL_BH, do_serial_bh);
1870     
1871     	show_serial_version();
1872     
1873     	/* Initialize the tty_driver structure */
1874     	/* SGI: Not all of this is exactly right for us. */
1875     	
1876     	memset(&serial_driver, 0, sizeof(struct tty_driver));
1877     	serial_driver.magic = TTY_DRIVER_MAGIC;
1878     	serial_driver.name = "ttyS";
1879     	serial_driver.major = TTY_MAJOR;
1880     	serial_driver.minor_start = 64;
1881     	serial_driver.num = NUM_CHANNELS;
1882     	serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
1883     	serial_driver.subtype = SERIAL_TYPE_NORMAL;
1884     	serial_driver.init_termios = tty_std_termios;
1885     
1886     	serial_driver.init_termios.c_cflag =
1887     		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1888     	serial_driver.flags = TTY_DRIVER_REAL_RAW;
1889     	serial_driver.refcount = &serial_refcount;
1890     	serial_driver.table = serial_table;
1891     	serial_driver.termios = serial_termios;
1892     	serial_driver.termios_locked = serial_termios_locked;
1893     
1894     	serial_driver.open = rs_open;
1895     	serial_driver.close = rs_close;
1896     	serial_driver.write = rs_write;
1897     	serial_driver.flush_chars = rs_flush_chars;
1898     	serial_driver.write_room = rs_write_room;
1899     	serial_driver.chars_in_buffer = rs_chars_in_buffer;
1900     	serial_driver.flush_buffer = rs_flush_buffer;
1901     	serial_driver.ioctl = rs_ioctl;
1902     	serial_driver.throttle = rs_throttle;
1903     	serial_driver.unthrottle = rs_unthrottle;
1904     	serial_driver.set_termios = rs_set_termios;
1905     	serial_driver.stop = rs_stop;
1906     	serial_driver.start = rs_start;
1907     	serial_driver.hangup = rs_hangup;
1908     
1909     	/*
1910     	 * The callout device is just like normal device except for
1911     	 * major number and the subtype code.
1912     	 */
1913     	callout_driver = serial_driver;
1914     	callout_driver.name = "cua";
1915     	callout_driver.major = TTYAUX_MAJOR;
1916     	callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1917     
1918     	if (tty_register_driver(&serial_driver))
1919     		panic("Couldn't register serial driver\n");
1920     	if (tty_register_driver(&callout_driver))
1921     		panic("Couldn't register callout driver\n");
1922     	
1923     	save_flags(flags); cli();
1924     
1925     	/* Set up our interrupt linked list */
1926     	zs_chain = &zs_soft[0];
1927     	zs_soft[0].zs_next = &zs_soft[1];
1928     	zs_soft[1].zs_next = 0;
1929     
1930     	for(chip = 0; chip < NUM_SERIAL; chip++) {
1931     		/* If we are doing kgdb over one of the channels on
1932     		 * chip zero, kgdb_channel will be set to 1 by the
1933     		 * rs_kgdb_hook() routine below.
1934     		 */
1935     		if(!zs_chips[chip]) {
1936     			zs_chips[chip] = get_zs(chip);
1937     			/* Two channels per chip */
1938     			zs_channels[(chip*2)] = &zs_chips[chip]->channelB;
1939     			zs_channels[(chip*2)+1] = &zs_chips[chip]->channelA;
1940     			zs_soft[(chip*2)].kgdb_channel = 0;
1941     			zs_soft[(chip*2)+1].kgdb_channel = 0;
1942     		}
1943     		/* First, set up channel A on this chip. */
1944     		channel = chip * 2;
1945     		zs_soft[channel].zs_channel = zs_channels[channel];
1946     		zs_soft[channel].change_needed = 0;
1947     		zs_soft[channel].clk_divisor = 16;
1948     		zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1949     		zs_soft[channel].cons_mouse = 0;
1950     		/* If not keyboard/mouse and is console serial
1951     		 * line, then enable receiver interrupts.
1952     		 */
1953     		if(zs_soft[channel].is_cons) {
1954     			write_zsreg(zs_soft[channel].zs_channel, R1,
1955     				    (EXT_INT_ENAB | INT_ALL_Rx));
1956     			write_zsreg(zs_soft[channel].zs_channel, R9, (NV | MIE));
1957     			write_zsreg(zs_soft[channel].zs_channel, R10, (NRZ));
1958     			write_zsreg(zs_soft[channel].zs_channel, R3, (Rx8|RxENABLE));
1959     			write_zsreg(zs_soft[channel].zs_channel, R5, (Tx8 | TxENAB));
1960     		}
1961     		/* If this is the kgdb line, enable interrupts because we
1962     		 * now want to receive the 'control-c' character from the
1963     		 * client attached to us asynchronously.
1964     		 */
1965     		if(zs_soft[channel].kgdb_channel)
1966     			kgdb_chaninit(&zs_soft[channel], 1,
1967     				      zs_soft[channel].zs_baud);
1968     
1969     		/* Now, channel B */
1970     		channel++;
1971     		zs_soft[channel].zs_channel = zs_channels[channel];
1972     		zs_soft[channel].change_needed = 0;
1973     		zs_soft[channel].clk_divisor = 16;
1974     		zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1975     		zs_soft[channel].cons_keyb = 0;
1976     		/* If console serial line, then enable receiver interrupts. */
1977     		if(zs_soft[channel].is_cons) {
1978     			write_zsreg(zs_soft[channel].zs_channel, R1,
1979     				    (EXT_INT_ENAB | INT_ALL_Rx));
1980     			write_zsreg(zs_soft[channel].zs_channel, R9,
1981     				    (NV | MIE));
1982     			write_zsreg(zs_soft[channel].zs_channel, R10,
1983     				    (NRZ));
1984     			write_zsreg(zs_soft[channel].zs_channel, R3,
1985     				    (Rx8|RxENABLE));
1986     			write_zsreg(zs_soft[channel].zs_channel, R5,
1987     				    (Tx8 | TxENAB | RTS | DTR));
1988     		}
1989     	}
1990     
1991     	for(info=zs_chain, i=0; info; info = info->zs_next, i++)
1992     	{
1993     		info->magic = SERIAL_MAGIC;
1994     		info->port = (int) info->zs_channel;
1995     		info->line = i;
1996     		info->tty = 0;
1997     		info->irq = zilog_irq;
1998     		info->custom_divisor = 16;
1999     		info->close_delay = 50;
2000     		info->closing_wait = 3000;
2001     		info->x_char = 0;
2002     		info->event = 0;
2003     		info->count = 0;
2004     		info->blocked_open = 0;
2005     		info->tqueue.routine = do_softint;
2006     		info->tqueue.data = info;
2007     		info->tqueue_hangup.routine = do_serial_hangup;
2008     		info->tqueue_hangup.data = info;
2009     		info->callout_termios =callout_driver.init_termios;
2010     		info->normal_termios = serial_driver.init_termios;
2011     		init_waitqueue_head(&info->open_wait);
2012     		init_waitqueue_head(&info->close_wait);
2013     		printk("tty%02d at 0x%04x (irq = %d)", info->line, 
2014     		       info->port, info->irq);
2015     		printk(" is a Zilog8530\n");
2016     	}
2017     
2018     	if (request_irq(zilog_irq, rs_interrupt, (SA_INTERRUPT),
2019     			"Zilog8530", zs_chain))
2020     		panic("Unable to attach zs intr\n");
2021     	restore_flags(flags);
2022     
2023     	return 0;
2024     }
2025     
2026     /*
2027      * register_serial and unregister_serial allows for serial ports to be
2028      * configured at run-time, to support PCMCIA modems.
2029      */
2030     /* SGI: Unused at this time, just here to make things link. */
2031     int register_serial(struct serial_struct *req)
2032     {
2033     	return -1;
2034     }
2035     
2036     void unregister_serial(int line)
2037     {
2038     	return;
2039     }
2040     
2041     /* Hooks for running a serial console.  con_init() calls this if the
2042      * console is being run over one of the ttya/ttyb serial ports.
2043      * 'chip' should be zero, as chip 1 drives the mouse/keyboard.
2044      * 'channel' is decoded as 0=TTYA 1=TTYB, note that the channels
2045      * are addressed backwards, channel B is first, then channel A.
2046      */
2047     void
2048     rs_cons_hook(int chip, int out, int line)
2049     {
2050     	int channel;
2051     	
2052     	if(chip)
2053     		panic("rs_cons_hook called with chip not zero");
2054     	if(line != 0 && line != 1)
2055     		panic("rs_cons_hook called with line not ttya or ttyb");
2056     	channel = line;
2057     	if(!zs_chips[chip]) {
2058     		zs_chips[chip] = get_zs(chip);
2059     		/* Two channels per chip */
2060     		zs_channels[(chip*2)] = &zs_chips[chip]->channelB;
2061     		zs_channels[(chip*2)+1] = &zs_chips[chip]->channelA;
2062     	}
2063     	zs_soft[channel].zs_channel = zs_channels[channel];
2064     	zs_soft[channel].change_needed = 0;
2065     	zs_soft[channel].clk_divisor = 16;
2066     	zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
2067     	if(out) 
2068     		zs_cons_chanout = ((chip * 2) + channel);
2069     	else 
2070     		zs_cons_chanin = ((chip * 2) + channel);
2071     	
2072     	rs_cons_check(&zs_soft[channel], channel);
2073     }
2074     
2075     /* This is called at boot time to prime the kgdb serial debugging
2076      * serial line.  The 'tty_num' argument is 0 for /dev/ttyd2 and 1 for
2077      * /dev/ttyd1 (yes they are backwards on purpose) which is determined
2078      * in setup_arch() from the boot command line flags.
2079      */
2080     void
2081     rs_kgdb_hook(int tty_num)
2082     {
2083     	int chip = 0;
2084     
2085     	if(!zs_chips[chip]) {
2086     		zs_chips[chip] = get_zs(chip);
2087     		/* Two channels per chip */
2088     		zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2089     		zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2090     	}
2091     	zs_soft[tty_num].zs_channel = zs_channels[tty_num];
2092     	zs_kgdbchan = zs_soft[tty_num].zs_channel;
2093     	zs_soft[tty_num].change_needed = 0;
2094     	zs_soft[tty_num].clk_divisor = 16;
2095     	zs_soft[tty_num].zs_baud = get_zsbaud(&zs_soft[tty_num]);
2096     	zs_soft[tty_num].kgdb_channel = 1;     /* This runs kgdb */
2097     	zs_soft[tty_num ^ 1].kgdb_channel = 0; /* This does not */
2098     
2099     	/* Turn on transmitter/receiver at 8-bits/char */
2100     	kgdb_chaninit(&zs_soft[tty_num], 0, 9600);
2101     	ZS_CLEARERR(zs_kgdbchan);
2102     	udelay(5);
2103     	ZS_CLEARFIFO(zs_kgdbchan);
2104     }
2105     
2106     static void zs_console_write(struct console *co, const char *str,
2107                                  unsigned int count)
2108     {
2109     
2110     	while(count--) {
2111     		if(*str == '\n')
2112     			zs_cons_put_char('\r');
2113     		zs_cons_put_char(*str++);
2114     	}
2115     
2116     	/* Comment this if you want to have a strict interrupt-driven output */
2117     	rs_fair_output();
2118     }
2119     
2120     static int zs_console_wait_key(struct console *con)
2121     {
2122     	sleep_on(&keypress_wait);
2123     	return 0;
2124     }
2125     
2126     static kdev_t zs_console_device(struct console *con)
2127     {
2128     	return MKDEV(TTY_MAJOR, 64 + con->index);
2129     }
2130     
2131     
2132     static int __init zs_console_setup(struct console *con, char *options)
2133     {
2134     	struct sgi_serial *info;
2135     	int	baud;
2136     	int	bits = 8;
2137     	int	parity = 'n';
2138     	int	cflag = CREAD | HUPCL | CLOCAL;
2139     	char	*s, *dbaud;
2140     	int     i, brg;
2141         
2142     	if (options) {
2143     		baud = simple_strtoul(options, NULL, 10);
2144     		s = options;
2145     		while(*s >= '0' && *s <= '9')
2146     			s++;
2147     		if (*s) parity = *s++;
2148     		if (*s) bits   = *s - '0';
2149     	}
2150     	else {
2151     		/* If the user doesn't set console=... try to read the
2152     		 * PROM variable - if this fails use 9600 baud and
2153     		 * inform the user about the problem
2154     		 */
2155     		dbaud = ArcGetEnvironmentVariable("dbaud");
2156     		if(dbaud) baud = simple_strtoul(dbaud, NULL, 10);
2157     		else {
2158     			/* Use prom_printf() to make sure that the user
2159     			 * is getting anything ...
2160     			 */
2161     			prom_printf("No dbaud set in PROM ?!? Using 9600.\n");
2162     			baud = 9600;
2163     		}
2164     	}
2165     
2166     	/*
2167     	 *	Now construct a cflag setting.
2168     	 */
2169     	switch(baud) {
2170     		case 1200:
2171     			cflag |= B1200;
2172     			break;
2173     		case 2400:
2174     			cflag |= B2400;
2175     			break;
2176     		case 4800:
2177     			cflag |= B4800;
2178     			break;
2179     		case 19200:
2180     			cflag |= B19200;
2181     			break;
2182     		case 38400:
2183     			cflag |= B38400;
2184     			break;
2185     		case 57600:
2186     			cflag |= B57600;
2187     			break;
2188     		case 115200:
2189     			cflag |= B115200;
2190     			break;
2191     		case 9600:
2192     		default:
2193     			cflag |= B9600;
2194     			break;
2195     	}
2196     	switch(bits) {
2197     		case 7:
2198     			cflag |= CS7;
2199     			break;
2200     		default:
2201     		case 8:
2202     			cflag |= CS8;
2203     			break;
2204     	}
2205     	switch(parity) {
2206     		case 'o': case 'O':
2207     			cflag |= PARODD;
2208     			break;
2209     		case 'e': case 'E':
2210     			cflag |= PARENB;
2211     			break;
2212     	}
2213     	con->cflag = cflag;
2214     
2215             rs_cons_hook(0, 0, con->index);
2216     	info = zs_soft + con->index;
2217     	info->is_cons = 1;
2218         
2219     	printk("Console: ttyS%d (Zilog8530), %d baud\n", 
2220     						info->line, baud);
2221     
2222     	i = con->cflag & CBAUD;
2223     	if (con->cflag & CBAUDEX) {
2224     		i &= ~CBAUDEX;
2225     		con->cflag &= ~CBAUDEX;
2226     	}
2227     	info->zs_baud = baud;
2228     
2229     	switch (con->cflag & CSIZE) {
2230     		case CS5:
2231     			zscons_regs[3] = Rx5 | RxENABLE;
2232     			zscons_regs[5] = Tx5 | TxENAB;
2233     			break;
2234     		case CS6:
2235     			zscons_regs[3] = Rx6 | RxENABLE;
2236     			zscons_regs[5] = Tx6 | TxENAB;
2237     			break;
2238     		case CS7:
2239     			zscons_regs[3] = Rx7 | RxENABLE;
2240     			zscons_regs[5] = Tx7 | TxENAB;
2241     			break;
2242     		default:
2243     		case CS8:
2244     			zscons_regs[3] = Rx8 | RxENABLE;
2245     			zscons_regs[5] = Tx8 | TxENAB;
2246     			break;
2247     	}
2248     	zscons_regs[5] |= DTR;
2249     
2250     	if (con->cflag & PARENB)
2251     		zscons_regs[4] |= PAR_ENA;
2252     	if (!(con->cflag & PARODD))
2253     		zscons_regs[4] |= PAR_EVEN;
2254     
2255     	if (con->cflag & CSTOPB)
2256     		zscons_regs[4] |= SB2;
2257     	else
2258     		zscons_regs[4] |= SB1;
2259     	
2260     	sgisercon = con;
2261     
2262     	brg = BPS_TO_BRG(baud, ZS_CLOCK / info->clk_divisor);
2263     	zscons_regs[12] = brg & 0xff;
2264     	zscons_regs[13] = (brg >> 8) & 0xff;
2265     	memcpy(info->curregs, zscons_regs, sizeof(zscons_regs));
2266     	memcpy(info->pendregs, zscons_regs, sizeof(zscons_regs));    
2267     	load_zsregs(info->zs_channel, zscons_regs);
2268     	ZS_CLEARERR(info->zs_channel);
2269     	ZS_CLEARFIFO(info->zs_channel);
2270     	return 0;
2271     }
2272     
2273     static struct console sgi_console_driver = {
2274     	name:		"ttyS",
2275     	write:		zs_console_write,
2276     	device:		zs_console_device,
2277     	wait_key:	zs_console_wait_key,
2278     	setup:		zs_console_setup,
2279     	flags:		CON_PRINTBUFFER,
2280     	index:		-1,
2281     };
2282     
2283     /*
2284      *	Register console.
2285      */
2286     void __init sgi_serial_console_init(void)
2287     {
2288     	register_console(&sgi_console_driver);
2289     }
2290     __initcall(rs_init);
2291