1/*
2 * decserial.c: Serial port driver for IOASIC DECstations.
3 *
4 * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
5 * Derived from drivers/macintosh/macserial.c by Harald Koerfgen.
6 *
7 * DECstation changes
8 * Copyright (C) 1998-2000 Harald Koerfgen
9 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005  Maciej W. Rozycki
10 *
11 * For the rest of the code the original Copyright applies:
12 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
13 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
14 *
15 *
16 * Note: for IOASIC systems the wiring is as follows:
17 *
18 * mouse/keyboard:
19 * DIN-7 MJ-4  signal        SCC
20 * 2     1     TxD       <-  A.TxD
21 * 3     4     RxD       ->  A.RxD
22 *
23 * EIA-232/EIA-423:
24 * DB-25 MMJ-6 signal        SCC
25 * 2     2     TxD       <-  B.TxD
26 * 3     5     RxD       ->  B.RxD
27 * 4           RTS       <- ~A.RTS
28 * 5           CTS       -> ~B.CTS
29 * 6     6     DSR       -> ~A.SYNC
30 * 8           CD        -> ~B.DCD
31 * 12          DSRS(DCE) -> ~A.CTS  (*)
32 * 15          TxC       ->  B.TxC
33 * 17          RxC       ->  B.RxC
34 * 20    1     DTR       <- ~A.DTR
35 * 22          RI        -> ~A.DCD
36 * 23          DSRS(DTE) <- ~B.RTS
37 *
38 * (*) EIA-232 defines the signal at this pin to be SCD, while DSRS(DCE)
39 *     is shared with DSRS(DTE) at pin 23.
40 */
41
42#include <linux/errno.h>
43#include <linux/signal.h>
44#include <linux/sched.h>
45#include <linux/timer.h>
46#include <linux/interrupt.h>
47#include <linux/tty.h>
48#include <linux/tty_flip.h>
49#include <linux/major.h>
50#include <linux/string.h>
51#include <linux/fcntl.h>
52#include <linux/mm.h>
53#include <linux/kernel.h>
54#include <linux/delay.h>
55#include <linux/init.h>
56#include <linux/ioport.h>
57#include <linux/spinlock.h>
58#ifdef CONFIG_SERIAL_DEC_CONSOLE
59#include <linux/console.h>
60#endif
61
62#include <asm/io.h>
63#include <asm/pgtable.h>
64#include <asm/irq.h>
65#include <asm/system.h>
66#include <asm/bootinfo.h>
67
68#include <asm/dec/interrupts.h>
69#include <asm/dec/ioasic_addrs.h>
70#include <asm/dec/machtype.h>
71#include <asm/dec/serial.h>
72#include <asm/dec/system.h>
73
74#ifdef CONFIG_KGDB
75#include <asm/kgdb.h>
76#endif
77#ifdef CONFIG_MAGIC_SYSRQ
78#include <linux/sysrq.h>
79#endif
80
81#include "zs.h"
82
83/*
84 * It would be nice to dynamically allocate everything that
85 * depends on NUM_SERIAL, so we could support any number of
86 * Z8530s, but for now...
87 */
88#define NUM_SERIAL	2		/* Max number of ZS chips supported */
89#define NUM_CHANNELS	(NUM_SERIAL * 2)	/* 2 channels per chip */
90#define CHANNEL_A_NR  (zs_parms->channel_a_offset > zs_parms->channel_b_offset)
91                                        /* Number of channel A in the chip */
92#define ZS_CHAN_IO_SIZE 8
93#define ZS_CLOCK        7372800 	/* Z8530 RTxC input clock rate */
94
95#define RECOVERY_DELAY  udelay(2)
96
97struct zs_parms {
98	unsigned long scc0;
99	unsigned long scc1;
100	int channel_a_offset;
101	int channel_b_offset;
102	int irq0;
103	int irq1;
104	int clock;
105};
106
107static struct zs_parms *zs_parms;
108
109#ifdef CONFIG_MACH_DECSTATION
110static struct zs_parms ds_parms = {
111	scc0 : IOASIC_SCC0,
112	scc1 : IOASIC_SCC1,
113	channel_a_offset : 1,
114	channel_b_offset : 9,
115	irq0 : -1,
116	irq1 : -1,
117	clock : ZS_CLOCK
118};
119#endif
120
121#ifdef CONFIG_MACH_DECSTATION
122#define DS_BUS_PRESENT (IOASIC)
123#else
124#define DS_BUS_PRESENT 0
125#endif
126
127#define BUS_PRESENT (DS_BUS_PRESENT)
128
129DEFINE_SPINLOCK(zs_lock);
130
131struct dec_zschannel zs_channels[NUM_CHANNELS];
132struct dec_serial zs_soft[NUM_CHANNELS];
133int zs_channels_found;
134struct dec_serial *zs_chain;	/* list of all channels */
135
136struct tty_struct zs_ttys[NUM_CHANNELS];
137
138#ifdef CONFIG_SERIAL_DEC_CONSOLE
139static struct console sercons;
140#endif
141#if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
142	!defined(MODULE)
143static unsigned long break_pressed; /* break, really ... */
144#endif
145
146static unsigned char zs_init_regs[16] __initdata = {
147	0,				/* write 0 */
148	0,				/* write 1 */
149	0,				/* write 2 */
150	0,				/* write 3 */
151	(X16CLK),			/* write 4 */
152	0,				/* write 5 */
153	0, 0, 0,			/* write 6, 7, 8 */
154	(MIE | DLC | NV),		/* write 9 */
155	(NRZ),				/* write 10 */
156	(TCBR | RCBR),			/* write 11 */
157	0, 0,				/* BRG time constant, write 12 + 13 */
158	(BRSRC | BRENABL),		/* write 14 */
159	0				/* write 15 */
160};
161
162static struct tty_driver *serial_driver;
163
164/* serial subtype definitions */
165#define SERIAL_TYPE_NORMAL	1
166
167/* number of characters left in xmit buffer before we ask for more */
168#define WAKEUP_CHARS 256
169
170/*
171 * Debugging.
172 */
173#undef SERIAL_DEBUG_OPEN
174#undef SERIAL_DEBUG_FLOW
175#undef SERIAL_DEBUG_THROTTLE
176#undef SERIAL_PARANOIA_CHECK
177
178#undef ZS_DEBUG_REGS
179
180#ifdef SERIAL_DEBUG_THROTTLE
181#define _tty_name(tty,buf) tty_name(tty,buf)
182#endif
183
184#define RS_STROBE_TIME 10
185#define RS_ISR_PASS_LIMIT 256
186
187static void probe_sccs(void);
188static void change_speed(struct dec_serial *info);
189static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
190
191static inline int serial_paranoia_check(struct dec_serial *info,
192					char *name, const char *routine)
193{
194#ifdef SERIAL_PARANOIA_CHECK
195	static const char *badmagic =
196		"Warning: bad magic number for serial struct %s in %s\n";
197	static const char *badinfo =
198		"Warning: null mac_serial for %s in %s\n";
199
200	if (!info) {
201		printk(badinfo, name, routine);
202		return 1;
203	}
204	if (info->magic != SERIAL_MAGIC) {
205		printk(badmagic, name, routine);
206		return 1;
207	}
208#endif
209	return 0;
210}
211
212/*
213 * This is used to figure out the divisor speeds and the timeouts
214 */
215static int baud_table[] = {
216	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
217	9600, 19200, 38400, 57600, 115200, 0 };
218
219/*
220 * Reading and writing Z8530 registers.
221 */
222static inline unsigned char read_zsreg(struct dec_zschannel *channel,
223				       unsigned char reg)
224{
225	unsigned char retval;
226
227	if (reg != 0) {
228		*channel->control = reg & 0xf;
229		fast_iob(); RECOVERY_DELAY;
230	}
231	retval = *channel->control;
232	RECOVERY_DELAY;
233	return retval;
234}
235
236static inline void write_zsreg(struct dec_zschannel *channel,
237			       unsigned char reg, unsigned char value)
238{
239	if (reg != 0) {
240		*channel->control = reg & 0xf;
241		fast_iob(); RECOVERY_DELAY;
242	}
243	*channel->control = value;
244	fast_iob(); RECOVERY_DELAY;
245	return;
246}
247
248static inline unsigned char read_zsdata(struct dec_zschannel *channel)
249{
250	unsigned char retval;
251
252	retval = *channel->data;
253	RECOVERY_DELAY;
254	return retval;
255}
256
257static inline void write_zsdata(struct dec_zschannel *channel,
258				unsigned char value)
259{
260	*channel->data = value;
261	fast_iob(); RECOVERY_DELAY;
262	return;
263}
264
265static inline void load_zsregs(struct dec_zschannel *channel,
266			       unsigned char *regs)
267{
268/*	ZS_CLEARERR(channel);
269	ZS_CLEARFIFO(channel); */
270	/* Load 'em up */
271	write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
272	write_zsreg(channel, R5, regs[R5] & ~TxENAB);
273	write_zsreg(channel, R4, regs[R4]);
274	write_zsreg(channel, R9, regs[R9]);
275	write_zsreg(channel, R1, regs[R1]);
276	write_zsreg(channel, R2, regs[R2]);
277	write_zsreg(channel, R10, regs[R10]);
278	write_zsreg(channel, R11, regs[R11]);
279	write_zsreg(channel, R12, regs[R12]);
280	write_zsreg(channel, R13, regs[R13]);
281	write_zsreg(channel, R14, regs[R14]);
282	write_zsreg(channel, R15, regs[R15]);
283	write_zsreg(channel, R3, regs[R3]);
284	write_zsreg(channel, R5, regs[R5]);
285	return;
286}
287
288/* Sets or clears DTR/RTS on the requested line */
289static inline void zs_rtsdtr(struct dec_serial *info, int which, int set)
290{
291        unsigned long flags;
292
293	spin_lock_irqsave(&zs_lock, flags);
294	if (info->zs_channel != info->zs_chan_a) {
295		if (set) {
296			info->zs_chan_a->curregs[5] |= (which & (RTS | DTR));
297		} else {
298			info->zs_chan_a->curregs[5] &= ~(which & (RTS | DTR));
299		}
300		write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
301	}
302	spin_unlock_irqrestore(&zs_lock, flags);
303}
304
305/* Utility routines for the Zilog */
306static inline int get_zsbaud(struct dec_serial *ss)
307{
308	struct dec_zschannel *channel = ss->zs_channel;
309	int brg;
310
311	/* The baud rate is split up between two 8-bit registers in
312	 * what is termed 'BRG time constant' format in my docs for
313	 * the chip, it is a function of the clk rate the chip is
314	 * receiving which happens to be constant.
315	 */
316	brg = (read_zsreg(channel, 13) << 8);
317	brg |= read_zsreg(channel, 12);
318	return BRG_TO_BPS(brg, (zs_parms->clock/(ss->clk_divisor)));
319}
320
321/* On receive, this clears errors and the receiver interrupts */
322static inline void rs_recv_clear(struct dec_zschannel *zsc)
323{
324	write_zsreg(zsc, 0, ERR_RES);
325	write_zsreg(zsc, 0, RES_H_IUS);
326}
327
328/*
329 * ----------------------------------------------------------------------
330 *
331 * Here starts the interrupt handling routines.  All of the following
332 * subroutines are declared as inline and are folded into
333 * rs_interrupt().  They were separated out for readability's sake.
334 *
335 * 				- Ted Ts'o (tytso@mit.edu), 7-Mar-93
336 * -----------------------------------------------------------------------
337 */
338
339/*
340 * This routine is used by the interrupt handler to schedule
341 * processing in the software interrupt portion of the driver.
342 */
343static void rs_sched_event(struct dec_serial *info, int event)
344{
345	info->event |= 1 << event;
346	tasklet_schedule(&info->tlet);
347}
348
349static void receive_chars(struct dec_serial *info)
350{
351	struct tty_struct *tty = info->tty;
352	unsigned char ch, stat, flag;
353
354	while ((read_zsreg(info->zs_channel, R0) & Rx_CH_AV) != 0) {
355
356		stat = read_zsreg(info->zs_channel, R1);
357		ch = read_zsdata(info->zs_channel);
358
359		if (!tty && (!info->hook || !info->hook->rx_char))
360			continue;
361
362		flag = TTY_NORMAL;
363		if (info->tty_break) {
364			info->tty_break = 0;
365			flag = TTY_BREAK;
366			if (info->flags & ZILOG_SAK)
367				do_SAK(tty);
368			/* Ignore the null char got when BREAK is removed.  */
369			if (ch == 0)
370				continue;
371		} else {
372			if (stat & Rx_OVR) {
373				flag = TTY_OVERRUN;
374			} else if (stat & FRM_ERR) {
375				flag = TTY_FRAME;
376			} else if (stat & PAR_ERR) {
377				flag = TTY_PARITY;
378			}
379			if (flag != TTY_NORMAL)
380				/* reset the error indication */
381				write_zsreg(info->zs_channel, R0, ERR_RES);
382		}
383
384#if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
385	!defined(MODULE)
386		if (break_pressed && info->line == sercons.index) {
387			/* Ignore the null char got when BREAK is removed.  */
388			if (ch == 0)
389				continue;
390			if (time_before(jiffies, break_pressed + HZ * 5)) {
391				handle_sysrq(ch, NULL);
392				break_pressed = 0;
393				continue;
394			}
395			break_pressed = 0;
396		}
397#endif
398
399		if (info->hook && info->hook->rx_char) {
400			(*info->hook->rx_char)(ch, flag);
401			return;
402  		}
403
404		tty_insert_flip_char(tty, ch, flag);
405	}
406	if (tty)
407		tty_flip_buffer_push(tty);
408}
409
410static void transmit_chars(struct dec_serial *info)
411{
412	if ((read_zsreg(info->zs_channel, R0) & Tx_BUF_EMP) == 0)
413		return;
414	info->tx_active = 0;
415
416	if (info->x_char) {
417		/* Send next char */
418		write_zsdata(info->zs_channel, info->x_char);
419		info->x_char = 0;
420		info->tx_active = 1;
421		return;
422	}
423
424	if ((info->xmit_cnt <= 0) || (info->tty && info->tty->stopped)
425	    || info->tx_stopped) {
426		write_zsreg(info->zs_channel, R0, RES_Tx_P);
427		return;
428	}
429	/* Send char */
430	write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
431	info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
432	info->xmit_cnt--;
433	info->tx_active = 1;
434
435	if (info->xmit_cnt < WAKEUP_CHARS)
436		rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
437}
438
439static void status_handle(struct dec_serial *info)
440{
441	unsigned char stat;
442
443	/* Get status from Read Register 0 */
444	stat = read_zsreg(info->zs_channel, R0);
445
446	if ((stat & BRK_ABRT) && !(info->read_reg_zero & BRK_ABRT)) {
447#if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
448	!defined(MODULE)
449		if (info->line == sercons.index) {
450			if (!break_pressed)
451				break_pressed = jiffies;
452		} else
453#endif
454			info->tty_break = 1;
455	}
456
457	if (info->zs_channel != info->zs_chan_a) {
458
459		/* Check for DCD transitions */
460		if (info->tty && !C_CLOCAL(info->tty) &&
461		    ((stat ^ info->read_reg_zero) & DCD) != 0 ) {
462			if (stat & DCD) {
463				wake_up_interruptible(&info->open_wait);
464			} else {
465				tty_hangup(info->tty);
466			}
467		}
468
469		/* Check for CTS transitions */
470		if (info->tty && C_CRTSCTS(info->tty)) {
471			if ((stat & CTS) != 0) {
472				if (info->tx_stopped) {
473					info->tx_stopped = 0;
474					if (!info->tx_active)
475						transmit_chars(info);
476				}
477			} else {
478				info->tx_stopped = 1;
479			}
480		}
481
482	}
483
484	/* Clear status condition... */
485	write_zsreg(info->zs_channel, R0, RES_EXT_INT);
486	info->read_reg_zero = stat;
487}
488
489/*
490 * This is the serial driver's generic interrupt routine
491 */
492static irqreturn_t rs_interrupt(int irq, void *dev_id)
493{
494	struct dec_serial *info = (struct dec_serial *) dev_id;
495	irqreturn_t status = IRQ_NONE;
496	unsigned char zs_intreg;
497	int shift;
498
499	/* NOTE: The read register 3, which holds the irq status,
500	 *       does so for both channels on each chip.  Although
501	 *       the status value itself must be read from the A
502	 *       channel and is only valid when read from channel A.
503	 *       Yes... broken hardware...
504	 */
505#define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
506
507	if (info->zs_chan_a == info->zs_channel)
508		shift = 3;	/* Channel A */
509	else
510		shift = 0;	/* Channel B */
511
512	for (;;) {
513		zs_intreg = read_zsreg(info->zs_chan_a, R3) >> shift;
514		if ((zs_intreg & CHAN_IRQMASK) == 0)
515			break;
516
517		status = IRQ_HANDLED;
518
519		if (zs_intreg & CHBRxIP) {
520			receive_chars(info);
521		}
522		if (zs_intreg & CHBTxIP) {
523			transmit_chars(info);
524		}
525		if (zs_intreg & CHBEXT) {
526			status_handle(info);
527		}
528	}
529
530	/* Why do we need this ? */
531	write_zsreg(info->zs_channel, 0, RES_H_IUS);
532
533	return status;
534}
535
536#ifdef ZS_DEBUG_REGS
537void zs_dump (void) {
538	int i, j;
539	for (i = 0; i < zs_channels_found; i++) {
540		struct dec_zschannel *ch = &zs_channels[i];
541		if ((long)ch->control == UNI_IO_BASE+UNI_SCC1A_CTRL) {
542			for (j = 0; j < 15; j++) {
543				printk("W%d = 0x%x\t",
544				       j, (int)ch->curregs[j]);
545			}
546			for (j = 0; j < 15; j++) {
547				printk("R%d = 0x%x\t",
548				       j, (int)read_zsreg(ch,j));
549			}
550			printk("\n\n");
551		}
552	}
553}
554#endif
555
556/*
557 * -------------------------------------------------------------------
558 * Here ends the serial interrupt routines.
559 * -------------------------------------------------------------------
560 */
561
562/*
563 * ------------------------------------------------------------
564 * rs_stop() and rs_start()
565 *
566 * This routines are called before setting or resetting tty->stopped.
567 * ------------------------------------------------------------
568 */
569static void rs_stop(struct tty_struct *tty)
570{
571	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
572	unsigned long flags;
573
574	if (serial_paranoia_check(info, tty->name, "rs_stop"))
575		return;
576
577	spin_lock_irqsave(&zs_lock, flags);
578	if (info->zs_channel->curregs[5] & TxENAB) {
579		info->zs_channel->curregs[5] &= ~TxENAB;
580		write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
581	}
582	spin_unlock_irqrestore(&zs_lock, flags);
583}
584
585static void rs_start(struct tty_struct *tty)
586{
587	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
588	unsigned long flags;
589
590	if (serial_paranoia_check(info, tty->name, "rs_start"))
591		return;
592
593	spin_lock_irqsave(&zs_lock, flags);
594	if (info->xmit_cnt && info->xmit_buf && !(info->zs_channel->curregs[5] & TxENAB)) {
595		info->zs_channel->curregs[5] |= TxENAB;
596		write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
597	}
598	spin_unlock_irqrestore(&zs_lock, flags);
599}
600
601/*
602 * This routine is used to handle the "bottom half" processing for the
603 * serial driver, known also the "software interrupt" processing.
604 * This processing is done at the kernel interrupt level, after the
605 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
606 * is where time-consuming activities which can not be done in the
607 * interrupt driver proper are done; the interrupt driver schedules
608 * them using rs_sched_event(), and they get done here.
609 */
610
611static void do_softint(unsigned long private_)
612{
613	struct dec_serial	*info = (struct dec_serial *) private_;
614	struct tty_struct	*tty;
615
616	tty = info->tty;
617	if (!tty)
618		return;
619
620	if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
621		tty_wakeup(tty);
622}
623
624static int zs_startup(struct dec_serial * info)
625{
626	unsigned long flags;
627
628	if (info->flags & ZILOG_INITIALIZED)
629		return 0;
630
631	if (!info->xmit_buf) {
632		info->xmit_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
633		if (!info->xmit_buf)
634			return -ENOMEM;
635	}
636
637	spin_lock_irqsave(&zs_lock, flags);
638
639#ifdef SERIAL_DEBUG_OPEN
640	printk("starting up ttyS%d (irq %d)...", info->line, info->irq);
641#endif
642
643	/*
644	 * Clear the receive FIFO.
645	 */
646	ZS_CLEARFIFO(info->zs_channel);
647	info->xmit_fifo_size = 1;
648
649	/*
650	 * Clear the interrupt registers.
651	 */
652	write_zsreg(info->zs_channel, R0, ERR_RES);
653	write_zsreg(info->zs_channel, R0, RES_H_IUS);
654
655	/*
656	 * Set the speed of the serial port
657	 */
658	change_speed(info);
659
660	/*
661	 * Turn on RTS and DTR.
662	 */
663	zs_rtsdtr(info, RTS | DTR, 1);
664
665	/*
666	 * Finally, enable sequencing and interrupts
667	 */
668	info->zs_channel->curregs[R1] &= ~RxINT_MASK;
669	info->zs_channel->curregs[R1] |= (RxINT_ALL | TxINT_ENAB |
670					  EXT_INT_ENAB);
671	info->zs_channel->curregs[R3] |= RxENABLE;
672	info->zs_channel->curregs[R5] |= TxENAB;
673	info->zs_channel->curregs[R15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
674	write_zsreg(info->zs_channel, R1, info->zs_channel->curregs[R1]);
675	write_zsreg(info->zs_channel, R3, info->zs_channel->curregs[R3]);
676	write_zsreg(info->zs_channel, R5, info->zs_channel->curregs[R5]);
677	write_zsreg(info->zs_channel, R15, info->zs_channel->curregs[R15]);
678
679	/*
680	 * And clear the interrupt registers again for luck.
681	 */
682	write_zsreg(info->zs_channel, R0, ERR_RES);
683	write_zsreg(info->zs_channel, R0, RES_H_IUS);
684
685	/* Save the current value of RR0 */
686	info->read_reg_zero = read_zsreg(info->zs_channel, R0);
687
688	if (info->tty)
689		clear_bit(TTY_IO_ERROR, &info->tty->flags);
690	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
691
692	info->flags |= ZILOG_INITIALIZED;
693	spin_unlock_irqrestore(&zs_lock, flags);
694	return 0;
695}
696
697/*
698 * This routine will shutdown a serial port; interrupts are disabled, and
699 * DTR is dropped if the hangup on close termio flag is on.
700 */
701static void shutdown(struct dec_serial * info)
702{
703	unsigned long	flags;
704
705	if (!(info->flags & ZILOG_INITIALIZED))
706		return;
707
708#ifdef SERIAL_DEBUG_OPEN
709	printk("Shutting down serial port %d (irq %d)....", info->line,
710	       info->irq);
711#endif
712
713	spin_lock_irqsave(&zs_lock, flags);
714
715	if (info->xmit_buf) {
716		free_page((unsigned long) info->xmit_buf);
717		info->xmit_buf = 0;
718	}
719
720	info->zs_channel->curregs[1] = 0;
721	write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);	/* no interrupts */
722
723	info->zs_channel->curregs[3] &= ~RxENABLE;
724	write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
725
726	info->zs_channel->curregs[5] &= ~TxENAB;
727	write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
728	if (!info->tty || C_HUPCL(info->tty)) {
729		zs_rtsdtr(info, RTS | DTR, 0);
730	}
731
732	if (info->tty)
733		set_bit(TTY_IO_ERROR, &info->tty->flags);
734
735	info->flags &= ~ZILOG_INITIALIZED;
736	spin_unlock_irqrestore(&zs_lock, flags);
737}
738
739/*
740 * This routine is called to set the UART divisor registers to match
741 * the specified baud rate for a serial port.
742 */
743static void change_speed(struct dec_serial *info)
744{
745	unsigned cflag;
746	int	i;
747	int	brg, bits;
748	unsigned long flags;
749
750	if (!info->hook) {
751		if (!info->tty || !info->tty->termios)
752			return;
753		cflag = info->tty->termios->c_cflag;
754		if (!info->port)
755			return;
756	} else {
757		cflag = info->hook->cflags;
758	}
759
760	i = cflag & CBAUD;
761	if (i & CBAUDEX) {
762		i &= ~CBAUDEX;
763		if (i < 1 || i > 2) {
764			if (!info->hook)
765				info->tty->termios->c_cflag &= ~CBAUDEX;
766			else
767				info->hook->cflags &= ~CBAUDEX;
768		} else
769			i += 15;
770	}
771
772	spin_lock_irqsave(&zs_lock, flags);
773	info->zs_baud = baud_table[i];
774	if (info->zs_baud) {
775		brg = BPS_TO_BRG(info->zs_baud, zs_parms->clock/info->clk_divisor);
776		info->zs_channel->curregs[12] = (brg & 255);
777		info->zs_channel->curregs[13] = ((brg >> 8) & 255);
778		zs_rtsdtr(info, DTR, 1);
779	} else {
780		zs_rtsdtr(info, RTS | DTR, 0);
781		return;
782	}
783
784	/* byte size and parity */
785	info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
786	info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
787	switch (cflag & CSIZE) {
788	case CS5:
789		bits = 7;
790		info->zs_channel->curregs[3] |= Rx5;
791		info->zs_channel->curregs[5] |= Tx5;
792		break;
793	case CS6:
794		bits = 8;
795		info->zs_channel->curregs[3] |= Rx6;
796		info->zs_channel->curregs[5] |= Tx6;
797		break;
798	case CS7:
799		bits = 9;
800		info->zs_channel->curregs[3] |= Rx7;
801		info->zs_channel->curregs[5] |= Tx7;
802		break;
803	case CS8:
804	default: /* defaults to 8 bits */
805		bits = 10;
806		info->zs_channel->curregs[3] |= Rx8;
807		info->zs_channel->curregs[5] |= Tx8;
808		break;
809	}
810
811	info->timeout = ((info->xmit_fifo_size*HZ*bits) / info->zs_baud);
812        info->timeout += HZ/50;         /* Add .02 seconds of slop */
813
814	info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
815	if (cflag & CSTOPB) {
816		info->zs_channel->curregs[4] |= SB2;
817	} else {
818		info->zs_channel->curregs[4] |= SB1;
819	}
820	if (cflag & PARENB) {
821		info->zs_channel->curregs[4] |= PAR_ENA;
822	}
823	if (!(cflag & PARODD)) {
824		info->zs_channel->curregs[4] |= PAR_EVEN;
825	}
826
827	if (!(cflag & CLOCAL)) {
828		if (!(info->zs_channel->curregs[15] & DCDIE))
829			info->read_reg_zero = read_zsreg(info->zs_channel, 0);
830		info->zs_channel->curregs[15] |= DCDIE;
831	} else
832		info->zs_channel->curregs[15] &= ~DCDIE;
833	if (cflag & CRTSCTS) {
834		info->zs_channel->curregs[15] |= CTSIE;
835		if ((read_zsreg(info->zs_channel, 0) & CTS) == 0)
836			info->tx_stopped = 1;
837	} else {
838		info->zs_channel->curregs[15] &= ~CTSIE;
839		info->tx_stopped = 0;
840	}
841
842	/* Load up the new values */
843	load_zsregs(info->zs_channel, info->zs_channel->curregs);
844
845	spin_unlock_irqrestore(&zs_lock, flags);
846}
847
848static void rs_flush_chars(struct tty_struct *tty)
849{
850	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
851	unsigned long flags;
852
853	if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
854		return;
855
856	if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
857	    !info->xmit_buf)
858		return;
859
860	/* Enable transmitter */
861	spin_lock_irqsave(&zs_lock, flags);
862	transmit_chars(info);
863	spin_unlock_irqrestore(&zs_lock, flags);
864}
865
866static int rs_write(struct tty_struct * tty,
867		    const unsigned char *buf, int count)
868{
869	int	c, total = 0;
870	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
871	unsigned long flags;
872
873	if (serial_paranoia_check(info, tty->name, "rs_write"))
874		return 0;
875
876	if (!tty || !info->xmit_buf)
877		return 0;
878
879	while (1) {
880		spin_lock_irqsave(&zs_lock, flags);
881		c = min(count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
882				   SERIAL_XMIT_SIZE - info->xmit_head));
883		if (c <= 0)
884			break;
885
886		memcpy(info->xmit_buf + info->xmit_head, buf, c);
887		info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
888		info->xmit_cnt += c;
889		spin_unlock_irqrestore(&zs_lock, flags);
890		buf += c;
891		count -= c;
892		total += c;
893	}
894
895	if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
896	    && !info->tx_active)
897		transmit_chars(info);
898	spin_unlock_irqrestore(&zs_lock, flags);
899	return total;
900}
901
902static int rs_write_room(struct tty_struct *tty)
903{
904	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
905	int	ret;
906
907	if (serial_paranoia_check(info, tty->name, "rs_write_room"))
908		return 0;
909	ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
910	if (ret < 0)
911		ret = 0;
912	return ret;
913}
914
915static int rs_chars_in_buffer(struct tty_struct *tty)
916{
917	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
918
919	if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
920		return 0;
921	return info->xmit_cnt;
922}
923
924static void rs_flush_buffer(struct tty_struct *tty)
925{
926	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
927
928	if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
929		return;
930	spin_lock_irq(&zs_lock);
931	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
932	spin_unlock_irq(&zs_lock);
933	tty_wakeup(tty);
934}
935
936/*
937 * ------------------------------------------------------------
938 * rs_throttle()
939 *
940 * This routine is called by the upper-layer tty layer to signal that
941 * incoming characters should be throttled.
942 * ------------------------------------------------------------
943 */
944static void rs_throttle(struct tty_struct * tty)
945{
946	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
947	unsigned long flags;
948
949#ifdef SERIAL_DEBUG_THROTTLE
950	char	buf[64];
951
952	printk("throttle %s: %d....\n", _tty_name(tty, buf),
953	       tty->ldisc.chars_in_buffer(tty));
954#endif
955
956	if (serial_paranoia_check(info, tty->name, "rs_throttle"))
957		return;
958
959	if (I_IXOFF(tty)) {
960		spin_lock_irqsave(&zs_lock, flags);
961		info->x_char = STOP_CHAR(tty);
962		if (!info->tx_active)
963			transmit_chars(info);
964		spin_unlock_irqrestore(&zs_lock, flags);
965	}
966
967	if (C_CRTSCTS(tty)) {
968		zs_rtsdtr(info, RTS, 0);
969	}
970}
971
972static void rs_unthrottle(struct tty_struct * tty)
973{
974	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
975	unsigned long flags;
976
977#ifdef SERIAL_DEBUG_THROTTLE
978	char	buf[64];
979
980	printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
981	       tty->ldisc.chars_in_buffer(tty));
982#endif
983
984	if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
985		return;
986
987	if (I_IXOFF(tty)) {
988		spin_lock_irqsave(&zs_lock, flags);
989		if (info->x_char)
990			info->x_char = 0;
991		else {
992			info->x_char = START_CHAR(tty);
993			if (!info->tx_active)
994				transmit_chars(info);
995		}
996		spin_unlock_irqrestore(&zs_lock, flags);
997	}
998
999	if (C_CRTSCTS(tty)) {
1000		zs_rtsdtr(info, RTS, 1);
1001	}
1002}
1003
1004/*
1005 * ------------------------------------------------------------
1006 * rs_ioctl() and friends
1007 * ------------------------------------------------------------
1008 */
1009
1010static int get_serial_info(struct dec_serial * info,
1011			   struct serial_struct * retinfo)
1012{
1013	struct serial_struct tmp;
1014
1015	if (!retinfo)
1016		return -EFAULT;
1017	memset(&tmp, 0, sizeof(tmp));
1018	tmp.type = info->type;
1019	tmp.line = info->line;
1020	tmp.port = info->port;
1021	tmp.irq = info->irq;
1022	tmp.flags = info->flags;
1023	tmp.baud_base = info->baud_base;
1024	tmp.close_delay = info->close_delay;
1025	tmp.closing_wait = info->closing_wait;
1026	tmp.custom_divisor = info->custom_divisor;
1027	return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
1028}
1029
1030static int set_serial_info(struct dec_serial * info,
1031			   struct serial_struct * new_info)
1032{
1033	struct serial_struct new_serial;
1034	struct dec_serial old_info;
1035	int 			retval = 0;
1036
1037	if (!new_info)
1038		return -EFAULT;
1039	copy_from_user(&new_serial,new_info,sizeof(new_serial));
1040	old_info = *info;
1041
1042	if (!capable(CAP_SYS_ADMIN)) {
1043		if ((new_serial.baud_base != info->baud_base) ||
1044		    (new_serial.type != info->type) ||
1045		    (new_serial.close_delay != info->close_delay) ||
1046		    ((new_serial.flags & ~ZILOG_USR_MASK) !=
1047		     (info->flags & ~ZILOG_USR_MASK)))
1048			return -EPERM;
1049		info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1050			       (new_serial.flags & ZILOG_USR_MASK));
1051		info->custom_divisor = new_serial.custom_divisor;
1052		goto check_and_exit;
1053	}
1054
1055	if (info->count > 1)
1056		return -EBUSY;
1057
1058	/*
1059	 * OK, past this point, all the error checking has been done.
1060	 * At this point, we start making changes.....
1061	 */
1062
1063	info->baud_base = new_serial.baud_base;
1064	info->flags = ((info->flags & ~ZILOG_FLAGS) |
1065			(new_serial.flags & ZILOG_FLAGS));
1066	info->type = new_serial.type;
1067	info->close_delay = new_serial.close_delay;
1068	info->closing_wait = new_serial.closing_wait;
1069
1070check_and_exit:
1071	retval = zs_startup(info);
1072	return retval;
1073}
1074
1075/*
1076 * get_lsr_info - get line status register info
1077 *
1078 * Purpose: Let user call ioctl() to get info when the UART physically
1079 * 	    is emptied.  On bus types like RS485, the transmitter must
1080 * 	    release the bus after transmitting. This must be done when
1081 * 	    the transmit shift register is empty, not be done when the
1082 * 	    transmit holding register is empty.  This functionality
1083 * 	    allows an RS485 driver to be written in user space.
1084 */
1085static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1086{
1087	unsigned char status;
1088
1089	spin_lock(&zs_lock);
1090	status = read_zsreg(info->zs_channel, 0);
1091	spin_unlock_irq(&zs_lock);
1092	put_user(status,value);
1093	return 0;
1094}
1095
1096static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1097{
1098	struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1099	unsigned char control, status_a, status_b;
1100	unsigned int result;
1101
1102	if (info->hook)
1103		return -ENODEV;
1104
1105	if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1106		return -ENODEV;
1107
1108	if (tty->flags & (1 << TTY_IO_ERROR))
1109		return -EIO;
1110
1111	if (info->zs_channel == info->zs_chan_a)
1112		result = 0;
1113	else {
1114		spin_lock(&zs_lock);
1115		control = info->zs_chan_a->curregs[5];
1116		status_a = read_zsreg(info->zs_chan_a, 0);
1117		status_b = read_zsreg(info->zs_channel, 0);
1118		spin_unlock_irq(&zs_lock);
1119		result =  ((control  & RTS) ? TIOCM_RTS: 0)
1120			| ((control  & DTR) ? TIOCM_DTR: 0)
1121			| ((status_b & DCD) ? TIOCM_CAR: 0)
1122			| ((status_a & DCD) ? TIOCM_RNG: 0)
1123			| ((status_a & SYNC_HUNT) ? TIOCM_DSR: 0)
1124			| ((status_b & CTS) ? TIOCM_CTS: 0);
1125	}
1126	return result;
1127}
1128
1129static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1130                       unsigned int set, unsigned int clear)
1131{
1132	struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1133
1134	if (info->hook)
1135		return -ENODEV;
1136
1137	if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1138		return -ENODEV;
1139
1140	if (tty->flags & (1 << TTY_IO_ERROR))
1141		return -EIO;
1142
1143	if (info->zs_channel == info->zs_chan_a)
1144		return 0;
1145
1146	spin_lock(&zs_lock);
1147	if (set & TIOCM_RTS)
1148		info->zs_chan_a->curregs[5] |= RTS;
1149	if (set & TIOCM_DTR)
1150		info->zs_chan_a->curregs[5] |= DTR;
1151	if (clear & TIOCM_RTS)
1152		info->zs_chan_a->curregs[5] &= ~RTS;
1153	if (clear & TIOCM_DTR)
1154		info->zs_chan_a->curregs[5] &= ~DTR;
1155	write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1156	spin_unlock_irq(&zs_lock);
1157	return 0;
1158}
1159
1160/*
1161 * rs_break - turn transmit break condition on/off
1162 */
1163static void rs_break(struct tty_struct *tty, int break_state)
1164{
1165	struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1166	unsigned long flags;
1167
1168	if (serial_paranoia_check(info, tty->name, "rs_break"))
1169		return;
1170	if (!info->port)
1171		return;
1172
1173	spin_lock_irqsave(&zs_lock, flags);
1174	if (break_state == -1)
1175		info->zs_channel->curregs[5] |= SND_BRK;
1176	else
1177		info->zs_channel->curregs[5] &= ~SND_BRK;
1178	write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1179	spin_unlock_irqrestore(&zs_lock, flags);
1180}
1181
1182static int rs_ioctl(struct tty_struct *tty, struct file * file,
1183		    unsigned int cmd, unsigned long arg)
1184{
1185	struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1186
1187	if (info->hook)
1188		return -ENODEV;
1189
1190	if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1191		return -ENODEV;
1192
1193	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1194	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1195	    (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1196		if (tty->flags & (1 << TTY_IO_ERROR))
1197		    return -EIO;
1198	}
1199
1200	switch (cmd) {
1201	case TIOCGSERIAL:
1202		if (!access_ok(VERIFY_WRITE, (void *)arg,
1203			       sizeof(struct serial_struct)))
1204			return -EFAULT;
1205		return get_serial_info(info, (struct serial_struct *)arg);
1206
1207	case TIOCSSERIAL:
1208		return set_serial_info(info, (struct serial_struct *)arg);
1209
1210	case TIOCSERGETLSR:			/* Get line status register */
1211		if (!access_ok(VERIFY_WRITE, (void *)arg,
1212			       sizeof(unsigned int)))
1213			return -EFAULT;
1214		return get_lsr_info(info, (unsigned int *)arg);
1215
1216	case TIOCSERGSTRUCT:
1217		if (!access_ok(VERIFY_WRITE, (void *)arg,
1218			       sizeof(struct dec_serial)))
1219			return -EFAULT;
1220		copy_from_user((struct dec_serial *)arg, info,
1221			       sizeof(struct dec_serial));
1222		return 0;
1223
1224	default:
1225		return -ENOIOCTLCMD;
1226	}
1227	return 0;
1228}
1229
1230static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1231{
1232	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1233	int was_stopped;
1234
1235	if (tty->termios->c_cflag == old_termios->c_cflag)
1236		return;
1237	was_stopped = info->tx_stopped;
1238
1239	change_speed(info);
1240
1241	if (was_stopped && !info->tx_stopped)
1242		rs_start(tty);
1243}
1244
1245/*
1246 * ------------------------------------------------------------
1247 * rs_close()
1248 *
1249 * This routine is called when the serial port gets closed.
1250 * Wait for the last remaining data to be sent.
1251 * ------------------------------------------------------------
1252 */
1253static void rs_close(struct tty_struct *tty, struct file * filp)
1254{
1255	struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1256	unsigned long flags;
1257
1258	if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1259		return;
1260
1261	spin_lock_irqsave(&zs_lock, flags);
1262
1263	if (tty_hung_up_p(filp)) {
1264		spin_unlock_irqrestore(&zs_lock, flags);
1265		return;
1266	}
1267
1268#ifdef SERIAL_DEBUG_OPEN
1269	printk("rs_close ttyS%d, count = %d\n", info->line, info->count);
1270#endif
1271	if ((tty->count == 1) && (info->count != 1)) {
1272		/*
1273		 * Uh, oh.  tty->count is 1, which means that the tty
1274		 * structure will be freed.  Info->count should always
1275		 * be one in these conditions.  If it's greater than
1276		 * one, we've got real problems, since it means the
1277		 * serial port won't be shutdown.
1278		 */
1279		printk("rs_close: bad serial port count; tty->count is 1, "
1280		       "info->count is %d\n", info->count);
1281		info->count = 1;
1282	}
1283	if (--info->count < 0) {
1284		printk("rs_close: bad serial port count for ttyS%d: %d\n",
1285		       info->line, info->count);
1286		info->count = 0;
1287	}
1288	if (info->count) {
1289		spin_unlock_irqrestore(&zs_lock, flags);
1290		return;
1291	}
1292	info->flags |= ZILOG_CLOSING;
1293	/*
1294	 * Now we wait for the transmit buffer to clear; and we notify
1295	 * the line discipline to only process XON/XOFF characters.
1296	 */
1297	tty->closing = 1;
1298	if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1299		tty_wait_until_sent(tty, info->closing_wait);
1300	/*
1301	 * At this point we stop accepting input.  To do this, we
1302	 * disable the receiver and receive interrupts.
1303	 */
1304	info->zs_channel->curregs[3] &= ~RxENABLE;
1305	write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1306	info->zs_channel->curregs[1] = 0;	/* disable any rx ints */
1307	write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1308	ZS_CLEARFIFO(info->zs_channel);
1309	if (info->flags & ZILOG_INITIALIZED) {
1310		/*
1311		 * Before we drop DTR, make sure the SCC transmitter
1312		 * has completely drained.
1313		 */
1314		rs_wait_until_sent(tty, info->timeout);
1315	}
1316
1317	shutdown(info);
1318	if (tty->driver->flush_buffer)
1319		tty->driver->flush_buffer(tty);
1320	tty_ldisc_flush(tty);
1321	tty->closing = 0;
1322	info->event = 0;
1323	info->tty = 0;
1324	if (info->blocked_open) {
1325		if (info->close_delay) {
1326			msleep_interruptible(jiffies_to_msecs(info->close_delay));
1327		}
1328		wake_up_interruptible(&info->open_wait);
1329	}
1330	info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CLOSING);
1331	wake_up_interruptible(&info->close_wait);
1332	spin_unlock_irqrestore(&zs_lock, flags);
1333}
1334
1335/*
1336 * rs_wait_until_sent() --- wait until the transmitter is empty
1337 */
1338static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1339{
1340	struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1341	unsigned long orig_jiffies;
1342	int char_time;
1343
1344	if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1345		return;
1346
1347	orig_jiffies = jiffies;
1348	/*
1349	 * Set the check interval to be 1/5 of the estimated time to
1350	 * send a single character, and make it at least 1.  The check
1351	 * interval should also be less than the timeout.
1352	 */
1353	char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1354	char_time = char_time / 5;
1355	if (char_time == 0)
1356		char_time = 1;
1357	if (timeout)
1358		char_time = min(char_time, timeout);
1359	while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
1360		msleep_interruptible(jiffies_to_msecs(char_time));
1361		if (signal_pending(current))
1362			break;
1363		if (timeout && time_after(jiffies, orig_jiffies + timeout))
1364			break;
1365	}
1366	current->state = TASK_RUNNING;
1367}
1368
1369/*
1370 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1371 */
1372static void rs_hangup(struct tty_struct *tty)
1373{
1374	struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1375
1376	if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1377		return;
1378
1379	rs_flush_buffer(tty);
1380	shutdown(info);
1381	info->event = 0;
1382	info->count = 0;
1383	info->flags &= ~ZILOG_NORMAL_ACTIVE;
1384	info->tty = 0;
1385	wake_up_interruptible(&info->open_wait);
1386}
1387
1388/*
1389 * ------------------------------------------------------------
1390 * rs_open() and friends
1391 * ------------------------------------------------------------
1392 */
1393static int block_til_ready(struct tty_struct *tty, struct file * filp,
1394			   struct dec_serial *info)
1395{
1396	DECLARE_WAITQUEUE(wait, current);
1397	int		retval;
1398	int		do_clocal = 0;
1399
1400	/*
1401	 * If the device is in the middle of being closed, then block
1402	 * until it's done, and then try again.
1403	 */
1404	if (info->flags & ZILOG_CLOSING) {
1405		interruptible_sleep_on(&info->close_wait);
1406#ifdef SERIAL_DO_RESTART
1407		return ((info->flags & ZILOG_HUP_NOTIFY) ?
1408			-EAGAIN : -ERESTARTSYS);
1409#else
1410		return -EAGAIN;
1411#endif
1412	}
1413
1414	/*
1415	 * If non-blocking mode is set, or the port is not enabled,
1416	 * then make the check up front and then exit.
1417	 */
1418	if ((filp->f_flags & O_NONBLOCK) ||
1419	    (tty->flags & (1 << TTY_IO_ERROR))) {
1420		info->flags |= ZILOG_NORMAL_ACTIVE;
1421		return 0;
1422	}
1423
1424	if (tty->termios->c_cflag & CLOCAL)
1425		do_clocal = 1;
1426
1427	/*
1428	 * Block waiting for the carrier detect and the line to become
1429	 * free (i.e., not in use by the callout).  While we are in
1430	 * this loop, info->count is dropped by one, so that
1431	 * rs_close() knows when to free things.  We restore it upon
1432	 * exit, either normal or abnormal.
1433	 */
1434	retval = 0;
1435	add_wait_queue(&info->open_wait, &wait);
1436#ifdef SERIAL_DEBUG_OPEN
1437	printk("block_til_ready before block: ttyS%d, count = %d\n",
1438	       info->line, info->count);
1439#endif
1440	spin_lock(&zs_lock);
1441	if (!tty_hung_up_p(filp))
1442		info->count--;
1443	spin_unlock_irq(&zs_lock);
1444	info->blocked_open++;
1445	while (1) {
1446		spin_lock(&zs_lock);
1447		if (tty->termios->c_cflag & CBAUD)
1448			zs_rtsdtr(info, RTS | DTR, 1);
1449		spin_unlock_irq(&zs_lock);
1450		set_current_state(TASK_INTERRUPTIBLE);
1451		if (tty_hung_up_p(filp) ||
1452		    !(info->flags & ZILOG_INITIALIZED)) {
1453#ifdef SERIAL_DO_RESTART
1454			if (info->flags & ZILOG_HUP_NOTIFY)
1455				retval = -EAGAIN;
1456			else
1457				retval = -ERESTARTSYS;
1458#else
1459			retval = -EAGAIN;
1460#endif
1461			break;
1462		}
1463		if (!(info->flags & ZILOG_CLOSING) &&
1464		    (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1465			break;
1466		if (signal_pending(current)) {
1467			retval = -ERESTARTSYS;
1468			break;
1469		}
1470#ifdef SERIAL_DEBUG_OPEN
1471		printk("block_til_ready blocking: ttyS%d, count = %d\n",
1472		       info->line, info->count);
1473#endif
1474		schedule();
1475	}
1476	current->state = TASK_RUNNING;
1477	remove_wait_queue(&info->open_wait, &wait);
1478	if (!tty_hung_up_p(filp))
1479		info->count++;
1480	info->blocked_open--;
1481#ifdef SERIAL_DEBUG_OPEN
1482	printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1483	       info->line, info->count);
1484#endif
1485	if (retval)
1486		return retval;
1487	info->flags |= ZILOG_NORMAL_ACTIVE;
1488	return 0;
1489}
1490
1491/*
1492 * This routine is called whenever a serial port is opened.  It
1493 * enables interrupts for a serial port, linking in its ZILOG structure into
1494 * the IRQ chain.   It also performs the serial-specific
1495 * initialization for the tty structure.
1496 */
1497static int rs_open(struct tty_struct *tty, struct file * filp)
1498{
1499	struct dec_serial	*info;
1500	int 			retval, line;
1501
1502	line = tty->index;
1503	if ((line < 0) || (line >= zs_channels_found))
1504		return -ENODEV;
1505	info = zs_soft + line;
1506
1507	if (info->hook)
1508		return -ENODEV;
1509
1510	if (serial_paranoia_check(info, tty->name, "rs_open"))
1511		return -ENODEV;
1512#ifdef SERIAL_DEBUG_OPEN
1513	printk("rs_open %s, count = %d\n", tty->name, info->count);
1514#endif
1515
1516	info->count++;
1517	tty->driver_data = info;
1518	info->tty = tty;
1519
1520	/*
1521	 * If the port is the middle of closing, bail out now
1522	 */
1523	if (tty_hung_up_p(filp) ||
1524	    (info->flags & ZILOG_CLOSING)) {
1525		if (info->flags & ZILOG_CLOSING)
1526			interruptible_sleep_on(&info->close_wait);
1527#ifdef SERIAL_DO_RESTART
1528		return ((info->flags & ZILOG_HUP_NOTIFY) ?
1529			-EAGAIN : -ERESTARTSYS);
1530#else
1531		return -EAGAIN;
1532#endif
1533	}
1534
1535	/*
1536	 * Start up serial port
1537	 */
1538	retval = zs_startup(info);
1539	if (retval)
1540		return retval;
1541
1542	retval = block_til_ready(tty, filp, info);
1543	if (retval) {
1544#ifdef SERIAL_DEBUG_OPEN
1545		printk("rs_open returning after block_til_ready with %d\n",
1546		       retval);
1547#endif
1548		return retval;
1549	}
1550
1551#ifdef CONFIG_SERIAL_DEC_CONSOLE
1552	if (sercons.cflag && sercons.index == line) {
1553		tty->termios->c_cflag = sercons.cflag;
1554		sercons.cflag = 0;
1555		change_speed(info);
1556	}
1557#endif
1558
1559#ifdef SERIAL_DEBUG_OPEN
1560	printk("rs_open %s successful...", tty->name);
1561#endif
1562/* tty->low_latency = 1; */
1563	return 0;
1564}
1565
1566/* Finally, routines used to initialize the serial driver. */
1567
1568static void __init show_serial_version(void)
1569{
1570	printk("DECstation Z8530 serial driver version 0.09\n");
1571}
1572
1573/*  Initialize Z8530s zs_channels
1574 */
1575
1576static void __init probe_sccs(void)
1577{
1578	struct dec_serial **pp;
1579	int i, n, n_chips = 0, n_channels, chip, channel;
1580	unsigned long flags;
1581
1582	/*
1583	 * did we get here by accident?
1584	 */
1585	if(!BUS_PRESENT) {
1586		printk("Not on JUNKIO machine, skipping probe_sccs\n");
1587		return;
1588	}
1589
1590	switch(mips_machtype) {
1591#ifdef CONFIG_MACH_DECSTATION
1592	case MACH_DS5000_2X0:
1593	case MACH_DS5900:
1594		n_chips = 2;
1595		zs_parms = &ds_parms;
1596		zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1597		zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1598		break;
1599	case MACH_DS5000_1XX:
1600		n_chips = 2;
1601		zs_parms = &ds_parms;
1602		zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1603		zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1604		break;
1605	case MACH_DS5000_XX:
1606		n_chips = 1;
1607		zs_parms = &ds_parms;
1608		zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1609		break;
1610#endif
1611	default:
1612		panic("zs: unsupported bus");
1613	}
1614	if (!zs_parms)
1615		panic("zs: uninitialized parms");
1616
1617	pp = &zs_chain;
1618
1619	n_channels = 0;
1620
1621	for (chip = 0; chip < n_chips; chip++) {
1622		for (channel = 0; channel <= 1; channel++) {
1623			/*
1624			 * The sccs reside on the high byte of the 16 bit IOBUS
1625			 */
1626			zs_channels[n_channels].control =
1627				(volatile void *)CKSEG1ADDR(dec_kn_slot_base +
1628			  (0 == chip ? zs_parms->scc0 : zs_parms->scc1) +
1629			  (0 == channel ? zs_parms->channel_a_offset :
1630			                  zs_parms->channel_b_offset));
1631			zs_channels[n_channels].data =
1632				zs_channels[n_channels].control + 4;
1633
1634#ifndef CONFIG_SERIAL_DEC_CONSOLE
1635			/*
1636			 * We're called early and memory managment isn't up, yet.
1637			 * Thus request_region would fail.
1638			 */
1639			if (!request_region((unsigned long)
1640					 zs_channels[n_channels].control,
1641					 ZS_CHAN_IO_SIZE, "SCC"))
1642				panic("SCC I/O region is not free");
1643#endif
1644			zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1645			/* HACK alert! */
1646			if (!(chip & 1))
1647				zs_soft[n_channels].irq = zs_parms->irq0;
1648			else
1649				zs_soft[n_channels].irq = zs_parms->irq1;
1650
1651			/*
1652			 *  Identification of channel A. Location of channel A
1653                         *  inside chip depends on mapping of internal address
1654			 *  the chip decodes channels by.
1655			 *  CHANNEL_A_NR returns either 0 (in case of
1656			 *  DECstations) or 1 (in case of Baget).
1657			 */
1658			if (CHANNEL_A_NR == channel)
1659				zs_soft[n_channels].zs_chan_a =
1660				    &zs_channels[n_channels+1-2*CHANNEL_A_NR];
1661			else
1662				zs_soft[n_channels].zs_chan_a =
1663				    &zs_channels[n_channels];
1664
1665			*pp = &zs_soft[n_channels];
1666			pp = &zs_soft[n_channels].zs_next;
1667			n_channels++;
1668		}
1669	}
1670
1671	*pp = 0;
1672	zs_channels_found = n_channels;
1673
1674	for (n = 0; n < zs_channels_found; n++) {
1675		for (i = 0; i < 16; i++) {
1676			zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1677		}
1678	}
1679
1680	spin_lock_irqsave(&zs_lock, flags);
1681	for (n = 0; n < zs_channels_found; n++) {
1682		if (n % 2 == 0) {
1683			write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
1684			udelay(10);
1685			write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
1686		}
1687		load_zsregs(zs_soft[n].zs_channel,
1688			    zs_soft[n].zs_channel->curregs);
1689	}
1690	spin_unlock_irqrestore(&zs_lock, flags);
1691}
1692
1693static const struct tty_operations serial_ops = {
1694	.open = rs_open,
1695	.close = rs_close,
1696	.write = rs_write,
1697	.flush_chars = rs_flush_chars,
1698	.write_room = rs_write_room,
1699	.chars_in_buffer = rs_chars_in_buffer,
1700	.flush_buffer = rs_flush_buffer,
1701	.ioctl = rs_ioctl,
1702	.throttle = rs_throttle,
1703	.unthrottle = rs_unthrottle,
1704	.set_termios = rs_set_termios,
1705	.stop = rs_stop,
1706	.start = rs_start,
1707	.hangup = rs_hangup,
1708	.break_ctl = rs_break,
1709	.wait_until_sent = rs_wait_until_sent,
1710	.tiocmget = rs_tiocmget,
1711	.tiocmset = rs_tiocmset,
1712};
1713
1714/* zs_init inits the driver */
1715int __init zs_init(void)
1716{
1717	int channel, i;
1718	struct dec_serial *info;
1719
1720	if(!BUS_PRESENT)
1721		return -ENODEV;
1722
1723	/* Find out how many Z8530 SCCs we have */
1724	if (zs_chain == 0)
1725		probe_sccs();
1726	serial_driver = alloc_tty_driver(zs_channels_found);
1727	if (!serial_driver)
1728		return -ENOMEM;
1729
1730	show_serial_version();
1731
1732	/* Initialize the tty_driver structure */
1733	/* Not all of this is exactly right for us. */
1734
1735	serial_driver->owner = THIS_MODULE;
1736	serial_driver->name = "ttyS";
1737	serial_driver->major = TTY_MAJOR;
1738	serial_driver->minor_start = 64;
1739	serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1740	serial_driver->subtype = SERIAL_TYPE_NORMAL;
1741	serial_driver->init_termios = tty_std_termios;
1742	serial_driver->init_termios.c_cflag =
1743		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1744	serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1745	tty_set_operations(serial_driver, &serial_ops);
1746
1747	if (tty_register_driver(serial_driver))
1748		panic("Couldn't register serial driver");
1749
1750	for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
1751
1752		/* Needed before interrupts are enabled. */
1753		info->tty = 0;
1754		info->x_char = 0;
1755
1756		if (info->hook && info->hook->init_info) {
1757			(*info->hook->init_info)(info);
1758			continue;
1759		}
1760
1761		info->magic = SERIAL_MAGIC;
1762		info->port = (int) info->zs_channel->control;
1763		info->line = i;
1764		info->custom_divisor = 16;
1765		info->close_delay = 50;
1766		info->closing_wait = 3000;
1767		info->event = 0;
1768		info->count = 0;
1769		info->blocked_open = 0;
1770		tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1771		init_waitqueue_head(&info->open_wait);
1772		init_waitqueue_head(&info->close_wait);
1773		printk("ttyS%02d at 0x%08x (irq = %d) is a Z85C30 SCC\n",
1774		       info->line, info->port, info->irq);
1775		tty_register_device(serial_driver, info->line, NULL);
1776
1777	}
1778
1779	for (channel = 0; channel < zs_channels_found; ++channel) {
1780		zs_soft[channel].clk_divisor = 16;
1781		zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1782
1783		if (request_irq(zs_soft[channel].irq, rs_interrupt, IRQF_SHARED,
1784				"scc", &zs_soft[channel]))
1785			printk(KERN_ERR "decserial: can't get irq %d\n",
1786			       zs_soft[channel].irq);
1787
1788		if (zs_soft[channel].hook) {
1789			zs_startup(&zs_soft[channel]);
1790			if (zs_soft[channel].hook->init_channel)
1791				(*zs_soft[channel].hook->init_channel)
1792					(&zs_soft[channel]);
1793		}
1794	}
1795
1796	return 0;
1797}
1798
1799/*
1800 * polling I/O routines
1801 */
1802static int zs_poll_tx_char(void *handle, unsigned char ch)
1803{
1804	struct dec_serial *info = handle;
1805	struct dec_zschannel *chan = info->zs_channel;
1806	int    ret;
1807
1808	if(chan) {
1809		int loops = 10000;
1810
1811		while (loops && !(read_zsreg(chan, 0) & Tx_BUF_EMP))
1812			loops--;
1813
1814		if (loops) {
1815			write_zsdata(chan, ch);
1816			ret = 0;
1817		} else
1818			ret = -EAGAIN;
1819
1820		return ret;
1821	} else
1822		return -ENODEV;
1823}
1824
1825static int zs_poll_rx_char(void *handle)
1826{
1827	struct dec_serial *info = handle;
1828        struct dec_zschannel *chan = info->zs_channel;
1829        int    ret;
1830
1831	if(chan) {
1832                int loops = 10000;
1833
1834		while (loops && !(read_zsreg(chan, 0) & Rx_CH_AV))
1835			loops--;
1836
1837                if (loops)
1838                        ret = read_zsdata(chan);
1839                else
1840                        ret = -EAGAIN;
1841
1842		return ret;
1843	} else
1844		return -ENODEV;
1845}
1846
1847int register_zs_hook(unsigned int channel, struct dec_serial_hook *hook)
1848{
1849	struct dec_serial *info = &zs_soft[channel];
1850
1851	if (info->hook) {
1852		printk("%s: line %d has already a hook registered\n",
1853		       __FUNCTION__, channel);
1854
1855		return 0;
1856	} else {
1857		hook->poll_rx_char = zs_poll_rx_char;
1858		hook->poll_tx_char = zs_poll_tx_char;
1859		info->hook = hook;
1860
1861		return 1;
1862	}
1863}
1864
1865int unregister_zs_hook(unsigned int channel)
1866{
1867	struct dec_serial *info = &zs_soft[channel];
1868
1869        if (info->hook) {
1870                info->hook = NULL;
1871                return 1;
1872        } else {
1873                printk("%s: trying to unregister hook on line %d,"
1874                       " but none is registered\n", __FUNCTION__, channel);
1875                return 0;
1876        }
1877}
1878
1879/*
1880 * ------------------------------------------------------------
1881 * Serial console driver
1882 * ------------------------------------------------------------
1883 */
1884#ifdef CONFIG_SERIAL_DEC_CONSOLE
1885
1886
1887/*
1888 *	Print a string to the serial port trying not to disturb
1889 *	any possible real use of the port...
1890 */
1891static void serial_console_write(struct console *co, const char *s,
1892				 unsigned count)
1893{
1894	struct dec_serial *info;
1895	int i;
1896
1897	info = zs_soft + co->index;
1898
1899	for (i = 0; i < count; i++, s++) {
1900		if(*s == '\n')
1901			zs_poll_tx_char(info, '\r');
1902		zs_poll_tx_char(info, *s);
1903	}
1904}
1905
1906static struct tty_driver *serial_console_device(struct console *c, int *index)
1907{
1908	*index = c->index;
1909	return serial_driver;
1910}
1911
1912/*
1913 *	Setup initial baud/bits/parity. We do two things here:
1914 *	- construct a cflag setting for the first rs_open()
1915 *	- initialize the serial port
1916 *	Return non-zero if we didn't find a serial port.
1917 */
1918static int __init serial_console_setup(struct console *co, char *options)
1919{
1920	struct dec_serial *info;
1921	int baud = 9600;
1922	int bits = 8;
1923	int parity = 'n';
1924	int cflag = CREAD | HUPCL | CLOCAL;
1925	int clk_divisor = 16;
1926	int brg;
1927	char *s;
1928	unsigned long flags;
1929
1930	if(!BUS_PRESENT)
1931		return -ENODEV;
1932
1933	info = zs_soft + co->index;
1934
1935	if (zs_chain == 0)
1936		probe_sccs();
1937
1938	info->is_cons = 1;
1939
1940	if (options) {
1941		baud = simple_strtoul(options, NULL, 10);
1942		s = options;
1943		while(*s >= '0' && *s <= '9')
1944			s++;
1945		if (*s)
1946			parity = *s++;
1947		if (*s)
1948			bits   = *s - '0';
1949	}
1950
1951	/*
1952	 *	Now construct a cflag setting.
1953	 */
1954	switch(baud) {
1955	case 1200:
1956		cflag |= B1200;
1957		break;
1958	case 2400:
1959		cflag |= B2400;
1960		break;
1961	case 4800:
1962		cflag |= B4800;
1963		break;
1964	case 19200:
1965		cflag |= B19200;
1966		break;
1967	case 38400:
1968		cflag |= B38400;
1969		break;
1970	case 57600:
1971		cflag |= B57600;
1972		break;
1973	case 115200:
1974		cflag |= B115200;
1975		break;
1976	case 9600:
1977	default:
1978		cflag |= B9600;
1979		/*
1980		 * Set this to a sane value to prevent a divide error.
1981		 */
1982		baud  = 9600;
1983		break;
1984	}
1985	switch(bits) {
1986	case 7:
1987		cflag |= CS7;
1988		break;
1989	default:
1990	case 8:
1991		cflag |= CS8;
1992		break;
1993	}
1994	switch(parity) {
1995	case 'o': case 'O':
1996		cflag |= PARODD;
1997		break;
1998	case 'e': case 'E':
1999		cflag |= PARENB;
2000		break;
2001	}
2002	co->cflag = cflag;
2003
2004	spin_lock_irqsave(&zs_lock, flags);
2005
2006	/*
2007	 * Set up the baud rate generator.
2008	 */
2009	brg = BPS_TO_BRG(baud, zs_parms->clock / clk_divisor);
2010	info->zs_channel->curregs[R12] = (brg & 255);
2011	info->zs_channel->curregs[R13] = ((brg >> 8) & 255);
2012
2013	/*
2014	 * Set byte size and parity.
2015	 */
2016	if (bits == 7) {
2017		info->zs_channel->curregs[R3] |= Rx7;
2018		info->zs_channel->curregs[R5] |= Tx7;
2019	} else {
2020		info->zs_channel->curregs[R3] |= Rx8;
2021		info->zs_channel->curregs[R5] |= Tx8;
2022	}
2023	if (cflag & PARENB) {
2024		info->zs_channel->curregs[R4] |= PAR_ENA;
2025	}
2026	if (!(cflag & PARODD)) {
2027		info->zs_channel->curregs[R4] |= PAR_EVEN;
2028	}
2029	info->zs_channel->curregs[R4] |= SB1;
2030
2031	/*
2032	 * Turn on RTS and DTR.
2033	 */
2034	zs_rtsdtr(info, RTS | DTR, 1);
2035
2036	/*
2037	 * Finally, enable sequencing.
2038	 */
2039	info->zs_channel->curregs[R3] |= RxENABLE;
2040	info->zs_channel->curregs[R5] |= TxENAB;
2041
2042	/*
2043	 * Clear the interrupt registers.
2044	 */
2045	write_zsreg(info->zs_channel, R0, ERR_RES);
2046	write_zsreg(info->zs_channel, R0, RES_H_IUS);
2047
2048	/*
2049	 * Load up the new values.
2050	 */
2051	load_zsregs(info->zs_channel, info->zs_channel->curregs);
2052
2053	/* Save the current value of RR0 */
2054	info->read_reg_zero = read_zsreg(info->zs_channel, R0);
2055
2056	zs_soft[co->index].clk_divisor = clk_divisor;
2057	zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
2058
2059	spin_unlock_irqrestore(&zs_lock, flags);
2060
2061	return 0;
2062}
2063
2064static struct console sercons = {
2065	.name		= "ttyS",
2066	.write		= serial_console_write,
2067	.device		= serial_console_device,
2068	.setup		= serial_console_setup,
2069	.flags		= CON_PRINTBUFFER,
2070	.index		= -1,
2071};
2072
2073/*
2074 *	Register console.
2075 */
2076void __init zs_serial_console_init(void)
2077{
2078	register_console(&sercons);
2079}
2080#endif /* ifdef CONFIG_SERIAL_DEC_CONSOLE */
2081
2082#ifdef CONFIG_KGDB
2083struct dec_zschannel *zs_kgdbchan;
2084static unsigned char scc_inittab[] = {
2085	9,  0x80,	/* reset A side (CHRA) */
2086	13, 0,		/* set baud rate divisor */
2087	12, 1,
2088	14, 1,		/* baud rate gen enable, src=rtxc (BRENABL) */
2089	11, 0x50,	/* clocks = br gen (RCBR | TCBR) */
2090	5,  0x6a,	/* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
2091	4,  0x44,	/* x16 clock, 1 stop (SB1 | X16CLK)*/
2092	3,  0xc1,	/* rx enable, 8 bits (RxENABLE | Rx8)*/
2093};
2094
2095/* These are for receiving and sending characters under the kgdb
2096 * source level kernel debugger.
2097 */
2098void putDebugChar(char kgdb_char)
2099{
2100	struct dec_zschannel *chan = zs_kgdbchan;
2101	while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2102		RECOVERY_DELAY;
2103	write_zsdata(chan, kgdb_char);
2104}
2105char getDebugChar(void)
2106{
2107	struct dec_zschannel *chan = zs_kgdbchan;
2108	while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2109		eieio(); /*barrier();*/
2110	return read_zsdata(chan);
2111}
2112void kgdb_interruptible(int yes)
2113{
2114	struct dec_zschannel *chan = zs_kgdbchan;
2115	int one, nine;
2116	nine = read_zsreg(chan, 9);
2117	if (yes == 1) {
2118		one = EXT_INT_ENAB|RxINT_ALL;
2119		nine |= MIE;
2120		printk("turning serial ints on\n");
2121	} else {
2122		one = RxINT_DISAB;
2123		nine &= ~MIE;
2124		printk("turning serial ints off\n");
2125	}
2126	write_zsreg(chan, 1, one);
2127	write_zsreg(chan, 9, nine);
2128}
2129
2130static int kgdbhook_init_channel(void *handle)
2131{
2132	return 0;
2133}
2134
2135static void kgdbhook_init_info(void *handle)
2136{
2137}
2138
2139static void kgdbhook_rx_char(void *handle, unsigned char ch, unsigned char fl)
2140{
2141	struct dec_serial *info = handle;
2142
2143	if (fl != TTY_NORMAL)
2144		return;
2145	if (ch == 0x03 || ch == '$')
2146		breakpoint();
2147}
2148
2149/* This sets up the serial port we're using, and turns on
2150 * interrupts for that channel, so kgdb is usable once we're done.
2151 */
2152static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2153{
2154	int brg;
2155	int i, x;
2156	volatile char *sccc = ms->control;
2157	brg = BPS_TO_BRG(bps, zs_parms->clock/16);
2158	printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2159	for (i = 20000; i != 0; --i) {
2160		x = *sccc; eieio();
2161	}
2162	for (i = 0; i < sizeof(scc_inittab); ++i) {
2163		write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2164		i++;
2165	}
2166}
2167/* This is called at boot time to prime the kgdb serial debugging
2168 * serial line.  The 'tty_num' argument is 0 for /dev/ttya and 1
2169 * for /dev/ttyb which is determined in setup_arch() from the
2170 * boot command line flags.
2171 */
2172struct dec_serial_hook zs_kgdbhook = {
2173	.init_channel	= kgdbhook_init_channel,
2174	.init_info	= kgdbhook_init_info,
2175	.rx_char	= kgdbhook_rx_char,
2176	.cflags		= B38400 | CS8 | CLOCAL,
2177};
2178
2179void __init zs_kgdb_hook(int tty_num)
2180{
2181	/* Find out how many Z8530 SCCs we have */
2182	if (zs_chain == 0)
2183		probe_sccs();
2184	zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2185	zs_kgdbchan = zs_soft[tty_num].zs_channel;
2186	zs_soft[tty_num].change_needed = 0;
2187	zs_soft[tty_num].clk_divisor = 16;
2188	zs_soft[tty_num].zs_baud = 38400;
2189 	zs_soft[tty_num].hook = &zs_kgdbhook; /* This runs kgdb */
2190	/* Turn on transmitter/receiver at 8-bits/char */
2191        kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2192	printk("KGDB: on channel %d initialized\n", tty_num);
2193	set_debug_traps(); /* init stub */
2194}
2195#endif /* ifdef CONFIG_KGDB */
2196