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