import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / sbus / char / zs.c
1 /* $Id: zs.c,v 1.1.1.1 2005/04/11 02:50:34 jack 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 sparc we only 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 #define ZS_WSYNC(channel)       do { } while(0)
75 #else
76 #define ZSDELAY()
77 #define ZSDELAY_LONG()
78 #define ZS_WSYNC(__channel) \
79         sbus_readb(&((__channel)->control))
80 #endif
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)
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
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  */
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
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
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
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 */
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
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  */
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
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  */
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  */
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
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
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
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  */
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  */
734 static void do_serial_bh(void)
735 {
736         run_task_queue(&tq_serial);
737 }
738
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                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
750                     tty->ldisc.write_wakeup)
751                         (tty->ldisc.write_wakeup)(tty);
752                 wake_up_interruptible(&tty->write_wait);
753         }
754 }
755
756 /*
757  * This routine is called from the scheduler tqueue when the interrupt
758  * routine has signalled that a hangup has occurred.  The path of
759  * hangup processing is:
760  *
761  *      serial interrupt routine -> (scheduler tqueue) ->
762  *      do_serial_hangup() -> tty->hangup() -> zs_hangup()
763  * 
764  */
765 static void do_serial_hangup(void *private_)
766 {
767         struct sun_serial       *info = (struct sun_serial *) private_;
768         struct tty_struct       *tty;
769         
770         tty = info->tty;
771         if (!tty)
772                 return;
773 #ifdef SERIAL_DEBUG_OPEN
774         printk("do_serial_hangup<%p: tty-%d\n",
775                 __builtin_return_address(0), info->line);
776 #endif
777
778         tty_hangup(tty);
779 }
780
781 static int startup(struct sun_serial * info)
782 {
783         unsigned long flags;
784
785         if (info->flags & ZILOG_INITIALIZED)
786                 return 0;
787
788         if (!info->xmit_buf) {
789                 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
790                 if (!info->xmit_buf)
791                         return -ENOMEM;
792         }
793
794         save_flags(flags); cli();
795
796 #ifdef SERIAL_DEBUG_OPEN
797         printk("Starting up tty-%d (irq %d)...\n", info->line, info->irq);
798 #endif
799
800         /*
801          * Clear the FIFO buffers and disable them
802          * (they will be reenabled in change_speed())
803          */
804         ZS_CLEARFIFO(info->zs_channel);
805         info->xmit_fifo_size = 1;
806
807         /*
808          * Clear the interrupt registers.
809          */
810         sbus_writeb(ERR_RES, &info->zs_channel->control);
811         ZSDELAY();
812         ZS_WSYNC(info->zs_channel);
813         ZSLOG(REGCTRL, ERR_RES, 1);
814
815         sbus_writeb(RES_H_IUS, &info->zs_channel->control);
816         ZSDELAY();
817         ZS_WSYNC(info->zs_channel);
818         ZSLOG(REGCTRL, RES_H_IUS, 1);
819
820         /*
821          * Now, initialize the Zilog
822          */
823         zs_rtsdtr(info, 1);
824
825         /*
826          * Finally, enable sequencing and interrupts
827          */
828         info->curregs[1] |= (info->curregs[1] & ~(RxINT_MASK)) |
829                                 (EXT_INT_ENAB | INT_ALL_Rx);
830         info->curregs[3] |= (RxENAB | Rx8);
831         /* We enable Tx interrupts as needed. */
832         info->curregs[5] |= (TxENAB | Tx8);
833         info->curregs[9] |= (NV | MIE);
834         write_zsreg(info->zs_channel, 3, info->curregs[3]);
835         write_zsreg(info->zs_channel, 5, info->curregs[5]);
836         write_zsreg(info->zs_channel, 9, info->curregs[9]);
837         
838         /*
839          * And clear the interrupt registers again for luck.
840          */
841         sbus_writeb(ERR_RES, &info->zs_channel->control);
842         ZSDELAY();
843         ZS_WSYNC(info->zs_channel);
844         ZSLOG(REGCTRL, ERR_RES, 1);
845
846         sbus_writeb(RES_H_IUS, &info->zs_channel->control);
847         ZSDELAY();
848         ZS_WSYNC(info->zs_channel);
849         ZSLOG(REGCTRL, RES_H_IUS, 1);
850
851         if (info->tty)
852                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
853         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
854
855         /*
856          * and set the speed of the serial port
857          */
858         change_speed(info);
859
860         info->flags |= ZILOG_INITIALIZED;
861         restore_flags(flags);
862         return 0;
863 }
864
865 /*
866  * This routine will shutdown a serial port; interrupts are disabled, and
867  * DTR is dropped if the hangup on close termio flag is on.
868  */
869 static void shutdown(struct sun_serial * info)
870 {
871         unsigned long   flags;
872
873         if (!(info->flags & ZILOG_INITIALIZED))
874                 return;
875
876 #ifdef SERIAL_DEBUG_OPEN
877         printk("Shutting down serial port %d (irq %d)....", info->line,
878                info->irq);
879 #endif
880         
881         save_flags(flags); cli(); /* Disable interrupts */
882         
883         if (info->xmit_buf) {
884                 free_page((unsigned long) info->xmit_buf);
885                 info->xmit_buf = 0;
886         }
887
888         if (info->tty)
889                 set_bit(TTY_IO_ERROR, &info->tty->flags);
890         
891         info->flags &= ~ZILOG_INITIALIZED;
892         restore_flags(flags);
893 }
894
895 /*
896  * This routine is called to set the UART divisor registers to match
897  * the specified baud rate for a serial port.
898  */
899 static void change_speed(struct sun_serial *info)
900 {
901         unsigned cflag;
902         int     baud, quot = 0;
903         int     brg;
904
905         if (!info->tty || !info->tty->termios)
906                 return;
907         cflag = info->tty->termios->c_cflag;
908         if (!info->port)
909                 return;
910         baud = tty_get_baud_rate(info->tty);
911         
912         if ((baud == 38400) && 
913             ((info->flags & ZILOG_SPD_MASK) == ZILOG_SPD_CUST))
914                 quot = info->custom_divisor;
915
916         if (quot) {
917                 info->zs_baud = info->baud_base / quot;
918                 info->clk_divisor = 16;
919
920                 info->curregs[4] = X16CLK;
921                 info->curregs[11] = TCBR | RCBR;
922                 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
923                 info->curregs[12] = (brg & 255);
924                 info->curregs[13] = ((brg >> 8) & 255);
925                 info->curregs[14] = BRSRC | BRENAB;
926                 zs_rtsdtr(info, 1);
927         } else if (baud) {
928                 info->zs_baud = baud;
929                 info->clk_divisor = 16;
930
931                 info->curregs[4] = X16CLK;
932                 info->curregs[11] = TCBR | RCBR;
933                 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
934                 info->curregs[12] = (brg & 255);
935                 info->curregs[13] = ((brg >> 8) & 255);
936                 info->curregs[14] = BRSRC | BRENAB;
937                 zs_rtsdtr(info, 1);
938         } else {
939                 zs_rtsdtr(info, 0);
940                 return;
941         }
942
943         /* byte size and parity */
944         switch (cflag & CSIZE) {
945         case CS5:
946                 info->curregs[3] &= ~(RxN_MASK);
947                 info->curregs[3] |= Rx5;
948                 info->curregs[5] &= ~(TxN_MASK);
949                 info->curregs[5] |= Tx5;
950                 info->parity_mask = 0x1f;
951                 break;
952         case CS6:
953                 info->curregs[3] &= ~(RxN_MASK);
954                 info->curregs[3] |= Rx6;
955                 info->curregs[5] &= ~(TxN_MASK);
956                 info->curregs[5] |= Tx6;
957                 info->parity_mask = 0x3f;
958                 break;
959         case CS7:
960                 info->curregs[3] &= ~(RxN_MASK);
961                 info->curregs[3] |= Rx7;
962                 info->curregs[5] &= ~(TxN_MASK);
963                 info->curregs[5] |= Tx7;
964                 info->parity_mask = 0x7f;
965                 break;
966         case CS8:
967         default: /* defaults to 8 bits */
968                 info->curregs[3] &= ~(RxN_MASK);
969                 info->curregs[3] |= Rx8;
970                 info->curregs[5] &= ~(TxN_MASK);
971                 info->curregs[5] |= Tx8;
972                 info->parity_mask = 0xff;
973                 break;
974         }
975         info->curregs[4] &= ~(0x0c);
976         if (cflag & CSTOPB) {
977                 info->curregs[4] |= SB2;
978         } else {
979                 info->curregs[4] |= SB1;
980         }
981         if (cflag & PARENB) {
982                 info->curregs[4] |= PAR_ENAB;
983         } else {
984                 info->curregs[4] &= ~PAR_ENAB;
985         }
986         if (!(cflag & PARODD)) {
987                 info->curregs[4] |= PAR_EVEN;
988         } else {
989                 info->curregs[4] &= ~PAR_EVEN;
990         }
991
992         /* Load up the new values */
993         load_zsregs(info, info->curregs);
994
995         return;
996 }
997
998 /* This is for mouse/keyboard output.
999  * XXX mouse output??? can we send it commands??? XXX
1000  */
1001 static void kbd_put_char(unsigned char ch)
1002 {
1003         struct sun_zschannel *chan = zs_kbdchan;
1004         unsigned long flags;
1005
1006         if(!chan)
1007                 return;
1008
1009         save_flags(flags); cli();
1010         zs_put_char(chan, ch);
1011         restore_flags(flags);
1012 }
1013
1014 void mouse_put_char(char ch)
1015 {
1016         struct sun_zschannel *chan = zs_mousechan;
1017         unsigned long flags;
1018
1019         if(!chan)
1020                 return;
1021
1022         save_flags(flags); cli();
1023         zs_put_char(chan, ch);
1024         restore_flags(flags);
1025 }
1026
1027 /* These are for receiving and sending characters under the kgdb
1028  * source level kernel debugger.
1029  */
1030 void putDebugChar(char kgdb_char)
1031 {
1032         struct sun_zschannel *chan = zs_kgdbchan;
1033
1034         while((sbus_readb(&chan->control) & Tx_BUF_EMP)==0)
1035                 udelay(5);
1036         sbus_writeb(kgdb_char, &chan->data);
1037         ZS_WSYNC(chan);
1038         ZSLOG(REGDATA, kgdb_char, 1);
1039 }
1040
1041 char getDebugChar(void)
1042 {
1043         struct sun_zschannel *chan = zs_kgdbchan;
1044         u8 val;
1045
1046         do {
1047                 val = sbus_readb(&chan->control);
1048                 ZSLOG(REGCTRL, val, 0);
1049                 udelay(5);
1050         } while ((val & Rx_CH_AV) == 0);
1051
1052         val = sbus_readb(&chan->data);
1053         ZSLOG(REGDATA, val, 0);
1054         return val;
1055 }
1056
1057 static void zs_flush_chars(struct tty_struct *tty)
1058 {
1059         struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1060         unsigned long flags;
1061
1062         if (serial_paranoia_check(info, tty->device, "zs_flush_chars"))
1063                 return;
1064
1065         save_flags(flags); cli();
1066         if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1067             !info->xmit_buf)
1068                 goto out;
1069
1070         /* Enable transmitter */
1071         info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1072         write_zsreg(info->zs_channel, 1, info->curregs[1]);
1073         info->curregs[5] |= TxENAB;
1074         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1075
1076         /*
1077          * Send a first (bootstrapping) character. A best solution is
1078          * to call transmit_chars() here which handles output in a
1079          * generic way. Current transmit_chars() not only transmits,
1080          * but resets interrupts also what we do not desire here.
1081          * XXX Discuss with David.
1082          */
1083         zs_put_char(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
1084         info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1085         info->xmit_cnt--;
1086
1087 out:
1088         restore_flags(flags);
1089 }
1090
1091 static int zs_write(struct tty_struct * tty, int from_user,
1092                     const unsigned char *buf, int count)
1093 {
1094         struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1095         unsigned long flags;
1096         int c, total = 0;
1097
1098         if (serial_paranoia_check(info, tty->device, "zs_write"))
1099                 return 0;
1100
1101         if (!info || !info->xmit_buf || !tmp_buf)
1102                 return 0;
1103
1104         save_flags(flags);
1105         if (from_user) {
1106                 down(&tmp_buf_sem);
1107                 while (1) {
1108                         c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1109                                            SERIAL_XMIT_SIZE - info->xmit_head));
1110                         if (c <= 0)
1111                                 break;
1112                         c -= copy_from_user(tmp_buf, buf, c);
1113                         if (!c) {
1114                                 if (!total)
1115                                         total = -EFAULT;
1116                                 break;
1117                         }
1118                         cli();
1119                         c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1120                                        SERIAL_XMIT_SIZE - info->xmit_head));
1121                         memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1122                         info->xmit_head = ((info->xmit_head + c) &
1123                                            (SERIAL_XMIT_SIZE - 1));
1124                         info->xmit_cnt += c;
1125                         restore_flags(flags);
1126
1127                         buf += c;
1128                         count -= c;
1129                         total += c;
1130                 }
1131                 up(&tmp_buf_sem);
1132         } else {
1133                 while (1) {
1134                         cli();          
1135                         c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1136                                            SERIAL_XMIT_SIZE - info->xmit_head));
1137                         if (c <= 0) {
1138                                 restore_flags(flags);
1139                                 break;
1140                         }
1141                         memcpy(info->xmit_buf + info->xmit_head, buf, c);
1142                         info->xmit_head = ((info->xmit_head + c) &
1143                                            (SERIAL_XMIT_SIZE - 1));
1144                         info->xmit_cnt += c;
1145                         restore_flags(flags);
1146                         buf += c;
1147                         count -= c;
1148                         total += c;
1149                 }
1150         }
1151
1152         cli();          
1153         if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
1154                 /* Enable transmitter */
1155                 info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1156                 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1157                 info->curregs[5] |= TxENAB;
1158                 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1159 #if 1
1160                 zs_put_char(info->zs_channel,
1161                             info->xmit_buf[info->xmit_tail++]);
1162                 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1163                 info->xmit_cnt--;
1164 #endif
1165         }
1166
1167         restore_flags(flags);
1168         return total;
1169 }
1170
1171 static int zs_write_room(struct tty_struct *tty)
1172 {
1173         struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1174         int ret;
1175
1176         if (serial_paranoia_check(info, tty->device, "zs_write_room"))
1177                 return 0;
1178         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1179         if (ret < 0)
1180                 ret = 0;
1181         return ret;
1182 }
1183
1184 static int zs_chars_in_buffer(struct tty_struct *tty)
1185 {
1186         struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1187
1188         if (serial_paranoia_check(info, tty->device, "zs_chars_in_buffer"))
1189                 return 0;
1190         return info->xmit_cnt;
1191 }
1192
1193 static void zs_flush_buffer(struct tty_struct *tty)
1194 {
1195         struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1196
1197         if (serial_paranoia_check(info, tty->device, "zs_flush_buffer"))
1198                 return;
1199         cli();
1200         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1201         sti();
1202         wake_up_interruptible(&tty->write_wait);
1203         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1204             tty->ldisc.write_wakeup)
1205                 (tty->ldisc.write_wakeup)(tty);
1206 }
1207
1208 /*
1209  * ------------------------------------------------------------
1210  * zs_throttle()
1211  * 
1212  * This routine is called by the upper-layer tty layer to signal that
1213  * incoming characters should be throttled.
1214  * ------------------------------------------------------------
1215  */
1216 static void zs_throttle(struct tty_struct * tty)
1217 {
1218         struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1219 #ifdef SERIAL_DEBUG_THROTTLE
1220         char    buf[64];
1221         
1222         printk("throttle %s: %d....\n", _tty_name(tty, buf),
1223                tty->ldisc.chars_in_buffer(tty));
1224 #endif
1225
1226         if (serial_paranoia_check(info, tty->device, "zs_throttle"))
1227                 return;
1228         
1229         if (I_IXOFF(tty))
1230                 info->x_char = STOP_CHAR(tty);
1231
1232         /* Turn off RTS line */
1233         cli();
1234         info->curregs[5] &= ~RTS;
1235         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1236         sti();
1237 }
1238
1239 static void zs_unthrottle(struct tty_struct * tty)
1240 {
1241         struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1242 #ifdef SERIAL_DEBUG_THROTTLE
1243         char    buf[64];
1244         
1245         printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1246                tty->ldisc.chars_in_buffer(tty));
1247 #endif
1248
1249         if (serial_paranoia_check(info, tty->device, "zs_unthrottle"))
1250                 return;
1251         
1252         if (I_IXOFF(tty)) {
1253                 if (info->x_char)
1254                         info->x_char = 0;
1255                 else
1256                         info->x_char = START_CHAR(tty);
1257         }
1258
1259         /* Assert RTS line */
1260         cli();
1261         info->curregs[5] |= RTS;
1262         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1263         sti();
1264 }
1265
1266 /*
1267  * ------------------------------------------------------------
1268  * zs_ioctl() and friends
1269  * ------------------------------------------------------------
1270  */
1271
1272 static int get_serial_info(struct sun_serial * info,
1273                            struct serial_struct * retinfo)
1274 {
1275         struct serial_struct tmp;
1276   
1277         if (!retinfo)
1278                 return -EFAULT;
1279         memset(&tmp, 0, sizeof(tmp));
1280         tmp.type = info->type;
1281         tmp.line = info->line;
1282         tmp.port = info->port;
1283         tmp.irq = info->irq;
1284         tmp.flags = info->flags;
1285         tmp.baud_base = info->baud_base;
1286         tmp.close_delay = info->close_delay;
1287         tmp.closing_wait = info->closing_wait;
1288         tmp.custom_divisor = info->custom_divisor;
1289         if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1290                 return -EFAULT;
1291         return 0;
1292 }
1293
1294 static int set_serial_info(struct sun_serial * info,
1295                            struct serial_struct * new_info)
1296 {
1297         struct serial_struct new_serial;
1298         struct sun_serial old_info;
1299         int retval = 0;
1300
1301         if (!new_info || copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1302                 return -EFAULT;
1303         old_info = *info;
1304
1305         if (!capable(CAP_SYS_ADMIN)) {
1306                 if ((new_serial.baud_base != info->baud_base) ||
1307                     (new_serial.type != info->type) ||
1308                     (new_serial.close_delay != info->close_delay) ||
1309                     ((new_serial.flags & ~ZILOG_USR_MASK) !=
1310                      (info->flags & ~ZILOG_USR_MASK)))
1311                         return -EPERM;
1312                 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1313                                (new_serial.flags & ZILOG_USR_MASK));
1314                 info->custom_divisor = new_serial.custom_divisor;
1315                 goto check_and_exit;
1316         }
1317
1318         if(new_serial.baud_base < 9600)
1319                 return -EINVAL;
1320
1321         if (info->count > 1)
1322                 return -EBUSY;
1323
1324         /*
1325          * OK, past this point, all the error checking has been done.
1326          * At this point, we start making changes.....
1327          */
1328
1329         info->baud_base = new_serial.baud_base;
1330         info->flags = ((info->flags & ~ZILOG_FLAGS) |
1331                         (new_serial.flags & ZILOG_FLAGS));
1332         info->custom_divisor = new_serial.custom_divisor;
1333         info->type = new_serial.type;
1334         info->close_delay = new_serial.close_delay;
1335         info->closing_wait = new_serial.closing_wait;
1336
1337 check_and_exit:
1338         retval = startup(info);
1339         return retval;
1340 }
1341
1342 /*
1343  * get_lsr_info - get line status register info
1344  *
1345  * Purpose: Let user call ioctl() to get info when the UART physically
1346  *          is emptied.  On bus types like RS485, the transmitter must
1347  *          release the bus after transmitting. This must be done when
1348  *          the transmit shift register is empty, not be done when the
1349  *          transmit holding register is empty.  This functionality
1350  *          allows an RS485 driver to be written in user space. 
1351  */
1352 static int get_lsr_info(struct sun_serial * info, unsigned int *value)
1353 {
1354         unsigned char status;
1355
1356         cli();
1357         status = sbus_readb(&info->zs_channel->control);
1358         ZSDELAY();
1359         ZSLOG(REGCTRL, status, 0);
1360         sti();
1361         if (put_user(status, value))
1362                 return -EFAULT;
1363         return 0;
1364 }
1365
1366 static int get_modem_info(struct sun_serial * info, unsigned int *value)
1367 {
1368         unsigned char status;
1369         unsigned int result;
1370
1371         cli();
1372         status = sbus_readb(&info->zs_channel->control);
1373         ZSDELAY();
1374         ZSLOG(REGCTRL, status, 0);
1375         sti();
1376         result =  ((info->curregs[5] & RTS) ? TIOCM_RTS : 0)
1377                 | ((info->curregs[5] & DTR) ? TIOCM_DTR : 0)
1378                 | ((status  & DCD) ? TIOCM_CAR : 0)
1379                 | ((status  & SYNC) ? TIOCM_DSR : 0)
1380                 | ((status  & CTS) ? TIOCM_CTS : 0);
1381         if (put_user(result, value))
1382                 return -EFAULT;
1383         return 0;
1384 }
1385
1386 static int set_modem_info(struct sun_serial * info, unsigned int cmd,
1387                           unsigned int *value)
1388 {
1389         unsigned int arg;
1390
1391         if (get_user(arg, value))
1392                 return -EFAULT;
1393         switch (cmd) {
1394         case TIOCMBIS: 
1395                 if (arg & TIOCM_RTS)
1396                         info->curregs[5] |= RTS;
1397                 if (arg & TIOCM_DTR)
1398                         info->curregs[5] |= DTR;
1399                 break;
1400         case TIOCMBIC:
1401                 if (arg & TIOCM_RTS)
1402                         info->curregs[5] &= ~RTS;
1403                 if (arg & TIOCM_DTR)
1404                         info->curregs[5] &= ~DTR;
1405                 break;
1406         case TIOCMSET:
1407                 info->curregs[5] = ((info->curregs[5] & ~(RTS | DTR))
1408                              | ((arg & TIOCM_RTS) ? RTS : 0)
1409                              | ((arg & TIOCM_DTR) ? DTR : 0));
1410                 break;
1411         default:
1412                 return -EINVAL;
1413         }
1414         cli();
1415         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1416         sti();
1417         return 0;
1418 }
1419
1420 /*
1421  * This routine sends a break character out the serial port.
1422  */
1423 static void send_break( struct sun_serial * info, int duration)
1424 {
1425         if (!info->port)
1426                 return;
1427         current->state = TASK_INTERRUPTIBLE;
1428         cli();
1429         write_zsreg(info->zs_channel, 5, (info->curregs[5] | SND_BRK));
1430         schedule_timeout(duration);
1431         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1432         sti();
1433 }
1434
1435 static int zs_ioctl(struct tty_struct *tty, struct file * file,
1436                     unsigned int cmd, unsigned long arg)
1437 {
1438         struct sun_serial * info = (struct sun_serial *) tty->driver_data;
1439         int retval;
1440
1441         if (serial_paranoia_check(info, tty->device, "zs_ioctl"))
1442                 return -ENODEV;
1443
1444         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1445             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1446             (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1447                 if (tty->flags & (1 << TTY_IO_ERROR))
1448                     return -EIO;
1449         }
1450         
1451         switch (cmd) {
1452                 case TCSBRK:    /* SVID version: non-zero arg --> no break */
1453                         retval = tty_check_change(tty);
1454                         if (retval)
1455                                 return retval;
1456                         tty_wait_until_sent(tty, 0);
1457                         if (!arg)
1458                                 send_break(info, HZ/4); /* 1/4 second */
1459                         return 0;
1460                 case TCSBRKP:   /* support for POSIX tcsendbreak() */
1461                         retval = tty_check_change(tty);
1462                         if (retval)
1463                                 return retval;
1464                         tty_wait_until_sent(tty, 0);
1465                         send_break(info, arg ? arg*(HZ/10) : HZ/4);
1466                         return 0;
1467                 case TIOCGSOFTCAR:
1468                         if (put_user(C_CLOCAL(tty) ? 1 : 0,
1469                                      (unsigned long *) arg))
1470                                 return -EFAULT;
1471                         return 0;
1472                 case TIOCSSOFTCAR:
1473                         if (get_user(arg, (unsigned long *) arg))
1474                                 return -EFAULT;
1475                         tty->termios->c_cflag =
1476                                 ((tty->termios->c_cflag & ~CLOCAL) |
1477                                  (arg ? CLOCAL : 0));
1478                         return 0;
1479                 case TIOCMGET:
1480                         return get_modem_info(info, (unsigned int *) arg);
1481                 case TIOCMBIS:
1482                 case TIOCMBIC:
1483                 case TIOCMSET:
1484                         return set_modem_info(info, cmd, (unsigned int *) arg);
1485                 case TIOCGSERIAL:
1486                         return get_serial_info(info,
1487                                                (struct serial_struct *) arg);
1488                 case TIOCSSERIAL:
1489                         return set_serial_info(info,
1490                                                (struct serial_struct *) arg);
1491                 case TIOCSERGETLSR: /* Get line status register */
1492                         return get_lsr_info(info, (unsigned int *) arg);
1493
1494                 case TIOCSERGSTRUCT:
1495                         if (copy_to_user((struct sun_serial *) arg,
1496                                     info, sizeof(struct sun_serial)))
1497                                 return -EFAULT;
1498                         return 0;
1499                         
1500                 default:
1501                         return -ENOIOCTLCMD;
1502                 }
1503         return 0;
1504 }
1505
1506 static void zs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1507 {
1508         struct sun_serial *info = (struct sun_serial *) tty->driver_data;
1509
1510         if (tty->termios->c_cflag == old_termios->c_cflag)
1511                 return;
1512
1513         change_speed(info);
1514
1515         if ((old_termios->c_cflag & CRTSCTS) &&
1516             !(tty->termios->c_cflag & CRTSCTS)) {
1517                 tty->hw_stopped = 0;
1518                 zs_start(tty);
1519         }
1520 }
1521
1522 /*
1523  * ------------------------------------------------------------
1524  * zs_close()
1525  * 
1526  * This routine is called when the serial port gets closed.  First, we
1527  * wait for the last remaining data to be sent.  Then, we unlink its
1528  * ZILOG structure from the interrupt chain if necessary, and we free
1529  * that IRQ if nothing is left in the chain.
1530  * ------------------------------------------------------------
1531  */
1532 static void zs_close(struct tty_struct *tty, struct file * filp)
1533 {
1534         struct sun_serial * info = (struct sun_serial *) tty->driver_data;
1535         unsigned long flags;
1536
1537         if (!info || serial_paranoia_check(info, tty->device, "zs_close"))
1538                 return;
1539         
1540         save_flags(flags); cli();
1541         
1542         if (tty_hung_up_p(filp)) {
1543                 restore_flags(flags);
1544                 return;
1545         }
1546         
1547 #ifdef SERIAL_DEBUG_OPEN
1548         printk("zs_close tty-%d, count = %d\n", info->line, info->count);
1549 #endif
1550         if ((tty->count == 1) && (info->count != 1)) {
1551                 /*
1552                  * Uh, oh.  tty->count is 1, which means that the tty
1553                  * structure will be freed.  Info->count should always
1554                  * be one in these conditions.  If it's greater than
1555                  * one, we've got real problems, since it means the
1556                  * serial port won't be shutdown.
1557                  */
1558                 printk("zs_close: bad serial port count; tty->count is 1, "
1559                        "info->count is %d\n", info->count);
1560                 info->count = 1;
1561         }
1562         if (--info->count < 0) {
1563                 printk("zs_close: bad serial port count for ttys%d: %d\n",
1564                        info->line, info->count);
1565                 info->count = 0;
1566         }
1567         if (info->count) {
1568                 restore_flags(flags);
1569                 return;
1570         }
1571         info->flags |= ZILOG_CLOSING;
1572         /*
1573          * Save the termios structure, since this port may have
1574          * separate termios for callout and dialin.
1575          */
1576         if (info->flags & ZILOG_NORMAL_ACTIVE)
1577                 info->normal_termios = *tty->termios;
1578         if (info->flags & ZILOG_CALLOUT_ACTIVE)
1579                 info->callout_termios = *tty->termios;
1580         /*
1581          * Now we wait for the transmit buffer to clear; and we notify 
1582          * the line discipline to only process XON/XOFF characters.
1583          */
1584         tty->closing = 1;
1585         if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1586                 tty_wait_until_sent(tty, info->closing_wait);
1587         /*
1588          * At this point we stop accepting input.  To do this, we
1589          * disable the receive line status interrupts, and tell the
1590          * interrupt driver to stop checking the data ready bit in the
1591          * line status register.
1592          */
1593         /** if (!info->iscons) ... **/
1594         info->curregs[3] &= ~RxENAB;
1595         write_zsreg(info->zs_channel, 3, info->curregs[3]);
1596         info->curregs[1] &= ~(RxINT_MASK);
1597         write_zsreg(info->zs_channel, 1, info->curregs[1]);
1598         ZS_CLEARFIFO(info->zs_channel);
1599
1600         shutdown(info);
1601         if (tty->driver.flush_buffer)
1602                 tty->driver.flush_buffer(tty);
1603         if (tty->ldisc.flush_buffer)
1604                 tty->ldisc.flush_buffer(tty);
1605         tty->closing = 0;
1606         info->event = 0;
1607         info->tty = 0;
1608         if (tty->ldisc.num != ldiscs[N_TTY].num) {
1609                 if (tty->ldisc.close)
1610                         (tty->ldisc.close)(tty);
1611                 tty->ldisc = ldiscs[N_TTY];
1612                 tty->termios->c_line = N_TTY;
1613                 if (tty->ldisc.open)
1614                         (tty->ldisc.open)(tty);
1615         }
1616         if (info->blocked_open) {
1617                 if (info->close_delay) {
1618                         current->state = TASK_INTERRUPTIBLE;
1619                         schedule_timeout(info->close_delay);
1620                 }
1621                 wake_up_interruptible(&info->open_wait);
1622         }
1623         info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1624                          ZILOG_CLOSING);
1625         wake_up_interruptible(&info->close_wait);
1626 #ifdef SERIAL_DEBUG_OPEN
1627         printk("zs_close tty-%d exiting, count = %d\n", info->line, info->count);
1628 #endif
1629         restore_flags(flags);
1630 }
1631
1632 /*
1633  * zs_hangup() --- called by tty_hangup() when a hangup is signaled.
1634  */
1635 void zs_hangup(struct tty_struct *tty)
1636 {
1637         struct sun_serial * info = (struct sun_serial *) tty->driver_data;
1638
1639         if (serial_paranoia_check(info, tty->device, "zs_hangup"))
1640                 return;
1641
1642         if (info->is_cons)
1643                 return;
1644
1645 #ifdef SERIAL_DEBUG_OPEN
1646         printk("zs_hangup<%p: tty-%d, count = %d bye\n",
1647                 __builtin_return_address(0), info->line, info->count);
1648 #endif
1649
1650         zs_flush_buffer(tty);
1651         shutdown(info);
1652         info->event = 0;
1653         info->count = 0;
1654         info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1655         info->tty = 0;
1656         wake_up_interruptible(&info->open_wait);
1657 }
1658
1659 /*
1660  *
1661  * line_info - returns information about each channel
1662  *
1663  */
1664 static inline int line_info(char *buf, struct sun_serial *info)
1665 {
1666         unsigned char status;
1667         char stat_buf[30];
1668         int ret;
1669
1670         ret = sprintf(buf, "%d: uart:Zilog8530 port:%x irq:%d",
1671                 info->line, info->port, info->irq);
1672
1673         cli();
1674         status = sbus_readb(&info->zs_channel->control);
1675         ZSDELAY();
1676         ZSLOG(REGCTRL, status, 0);
1677         sti();
1678
1679         stat_buf[0] = 0;
1680         stat_buf[1] = 0;
1681         if (info->curregs[5] & RTS)
1682                 strcat(stat_buf, "|RTS");
1683         if (status & CTS)
1684                 strcat(stat_buf, "|CTS");
1685         if (info->curregs[5] & DTR)
1686                 strcat(stat_buf, "|DTR");
1687         if (status & SYNC)
1688                 strcat(stat_buf, "|DSR");
1689         if (status & DCD)
1690                 strcat(stat_buf, "|CD");
1691
1692         ret += sprintf(buf + ret, " baud:%d %s\n", info->zs_baud, stat_buf + 1);
1693         return ret;
1694 }
1695
1696 /*
1697  *
1698  * zs_read_proc() - called when /proc/tty/driver/serial is read.
1699  *
1700  */
1701 int zs_read_proc(char *page, char **start, off_t off, int count,
1702                  int *eof, void *data)
1703 {
1704         char *revision = "$Revision: 1.1.1.1 $";
1705         char *version, *p;
1706         int i, len = 0, l;
1707         off_t begin = 0;
1708
1709         version = strchr(revision, ' ');
1710         p = strchr(++version, ' ');
1711         *p = '\0';
1712         len += sprintf(page, "serinfo:1.0 driver:%s\n", version);
1713         *p = ' ';
1714
1715         for (i = 0; i < NUM_CHANNELS && len < 4000; i++) {
1716                 l = line_info(page + len, &zs_soft[i]);
1717                 len += l;
1718                 if (len+begin > off+count)
1719                         goto done;
1720                 if (len+begin < off) {
1721                         begin += len;
1722                         len = 0;
1723                 }
1724         }
1725
1726         *eof = 1;
1727 done:
1728         if (off >= len+begin)
1729                 return 0;
1730         *start = page + (off-begin);
1731         return ((count < begin+len-off) ? count : begin+len-off);
1732 }
1733
1734 /*
1735  * ------------------------------------------------------------
1736  * zs_open() and friends
1737  * ------------------------------------------------------------
1738  */
1739 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1740                            struct sun_serial *info)
1741 {
1742         DECLARE_WAITQUEUE(wait, current);
1743         int retval, do_clocal = 0;
1744         unsigned char r0;
1745
1746         /*
1747          * If the device is in the middle of being closed, then block
1748          * until it's done, and then try again.
1749          */
1750         if (info->flags & ZILOG_CLOSING) {
1751                 interruptible_sleep_on(&info->close_wait);
1752 #ifdef SERIAL_DO_RESTART
1753                 if (info->flags & ZILOG_HUP_NOTIFY)
1754                         return -EAGAIN;
1755                 else
1756                         return -ERESTARTSYS;
1757 #else
1758                 return -EAGAIN;
1759 #endif
1760         }
1761
1762         /*
1763          * If this is a callout device, then just make sure the normal
1764          * device isn't being used.
1765          */
1766         if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1767                 if (info->flags & ZILOG_NORMAL_ACTIVE)
1768                         return -EBUSY;
1769                 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1770                     (info->flags & ZILOG_SESSION_LOCKOUT) &&
1771                     (info->session != current->session))
1772                     return -EBUSY;
1773                 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1774                     (info->flags & ZILOG_PGRP_LOCKOUT) &&
1775                     (info->pgrp != current->pgrp))
1776                     return -EBUSY;
1777                 info->flags |= ZILOG_CALLOUT_ACTIVE;
1778                 return 0;
1779         }
1780         
1781         /*
1782          * If non-blocking mode is set, or the port is not enabled,
1783          * then make the check up front and then exit.
1784          */
1785         if ((filp->f_flags & O_NONBLOCK) ||
1786             (tty->flags & (1 << TTY_IO_ERROR))) {
1787                 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1788                         return -EBUSY;
1789                 info->flags |= ZILOG_NORMAL_ACTIVE;
1790                 return 0;
1791         }
1792
1793         if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1794                 if (info->normal_termios.c_cflag & CLOCAL)
1795                         do_clocal = 1;
1796         } else {
1797                 if (tty->termios->c_cflag & CLOCAL)
1798                         do_clocal = 1;
1799         }
1800         
1801         /*
1802          * Block waiting for the carrier detect and the line to become
1803          * free (i.e., not in use by the callout).  While we are in
1804          * this loop, info->count is dropped by one, so that
1805          * zs_close() knows when to free things.  We restore it upon
1806          * exit, either normal or abnormal.
1807          */
1808         retval = 0;
1809         add_wait_queue(&info->open_wait, &wait);
1810 #ifdef SERIAL_DEBUG_OPEN
1811         printk("block_til_ready before block: ttys%d, count = %d\n",
1812                info->line, info->count);
1813 #endif
1814         cli();
1815         if(!tty_hung_up_p(filp))
1816                 info->count--;
1817         sti();
1818         info->blocked_open++;
1819         while (1) {
1820                 cli();
1821                 if (!(info->flags & ZILOG_CALLOUT_ACTIVE))
1822                         zs_rtsdtr(info, 1);
1823                 sti();
1824                 set_current_state(TASK_INTERRUPTIBLE);
1825                 if (tty_hung_up_p(filp) ||
1826                     !(info->flags & ZILOG_INITIALIZED)) {
1827 #ifdef SERIAL_DEBUG_OPEN
1828                         printk("block_til_ready hup-ed: ttys%d, count = %d\n",
1829                                 info->line, info->count);
1830 #endif
1831 #ifdef SERIAL_DO_RESTART
1832                         if (info->flags & ZILOG_HUP_NOTIFY)
1833                                 retval = -EAGAIN;
1834                         else
1835                                 retval = -ERESTARTSYS;  
1836 #else
1837                         retval = -EAGAIN;
1838 #endif
1839                         break;
1840                 }
1841
1842                 cli();
1843                 r0 = read_zsreg(info->zs_channel, R0);
1844                 sti();
1845                 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1846                     !(info->flags & ZILOG_CLOSING) &&
1847                     (do_clocal || (DCD & r0)))
1848                         break;
1849                 if (signal_pending(current)) {
1850                         retval = -ERESTARTSYS;
1851                         break;
1852                 }
1853 #ifdef SERIAL_DEBUG_OPEN
1854                 printk("block_til_ready blocking: ttys%d, count = %d\n",
1855                        info->line, info->count);
1856 #endif
1857                 schedule();
1858         }
1859         current->state = TASK_RUNNING;
1860         remove_wait_queue(&info->open_wait, &wait);
1861         if (!tty_hung_up_p(filp))
1862                 info->count++;
1863         info->blocked_open--;
1864 #ifdef SERIAL_DEBUG_OPEN
1865         printk("block_til_ready after blocking: ttys%d, count = %d\n",
1866                info->line, info->count);
1867 #endif
1868         if (retval)
1869                 return retval;
1870         info->flags |= ZILOG_NORMAL_ACTIVE;
1871         return 0;
1872 }       
1873
1874 /*
1875  * This routine is called whenever a serial port is opened.  It
1876  * enables interrupts for a serial port, linking in its ZILOG structure into
1877  * the IRQ chain.   It also performs the serial-specific
1878  * initialization for the tty structure.
1879  */
1880 int zs_open(struct tty_struct *tty, struct file * filp)
1881 {
1882         struct sun_serial *info;
1883         int retval, line;
1884
1885         line = MINOR(tty->device) - tty->driver.minor_start;
1886
1887         /* The zilog lines for the mouse/keyboard must be
1888          * opened using their respective drivers.
1889          */
1890         if ((line < 0) || (line >= NUM_CHANNELS))
1891                 return -ENODEV;
1892         if((line == KEYBOARD_LINE) || (line == MOUSE_LINE))
1893                 return -ENODEV;
1894         info = zs_soft + line;
1895         /* Is the kgdb running over this line? */
1896         if (info->kgdb_channel)
1897                 return -ENODEV;
1898         if (serial_paranoia_check(info, tty->device, "zs_open"))
1899                 return -ENODEV;
1900 #ifdef SERIAL_DEBUG_OPEN
1901         printk("zs_open %s%d, count = %d\n", tty->driver.name, info->line,
1902                info->count);
1903 #endif
1904         if (info->tty != 0 && info->tty != tty) {
1905                 /* Never happen? */
1906                 printk("zs_open %s%d, tty overwrite.\n", tty->driver.name, info->line);
1907                 return -EBUSY;
1908         }
1909
1910         if (!tmp_buf) {
1911                 unsigned long page = get_free_page(GFP_KERNEL);
1912                 if (!page)
1913                         return -ENOMEM;
1914                 if (tmp_buf)
1915                         free_page(page);
1916                 else
1917                         tmp_buf = (unsigned char *) page;
1918         }
1919
1920         info->count++;
1921         tty->driver_data = info;
1922         info->tty = tty;
1923
1924         /*
1925          * Start up serial port
1926          */
1927         retval = startup(info);
1928         if (retval)
1929                 return retval;
1930
1931         retval = block_til_ready(tty, filp, info);
1932         if (retval) {
1933 #ifdef SERIAL_DEBUG_OPEN
1934                 printk("zs_open returning after block_til_ready with %d\n",
1935                        retval);
1936 #endif
1937                 return retval;
1938         }
1939
1940         if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1941                 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1942                         *tty->termios = info->normal_termios;
1943                 else 
1944                         *tty->termios = info->callout_termios;
1945                 change_speed(info);
1946         }
1947
1948 #ifdef CONFIG_SERIAL_CONSOLE
1949         if (zs_console.cflag && zs_console.index == line) {
1950                 tty->termios->c_cflag = zs_console.cflag;
1951                 zs_console.cflag = 0;
1952                 change_speed(info);
1953         }
1954 #endif
1955
1956         info->session = current->session;
1957         info->pgrp = current->pgrp;
1958
1959 #ifdef SERIAL_DEBUG_OPEN
1960         printk("zs_open ttys%d successful...", info->line);
1961 #endif
1962         return 0;
1963 }
1964
1965 /* Finally, routines used to initialize the serial driver. */
1966
1967 static void show_serial_version(void)
1968 {
1969         char *revision = "$Revision: 1.1.1.1 $";
1970         char *version, *p;
1971
1972         version = strchr(revision, ' ');
1973         p = strchr(++version, ' ');
1974         *p = '\0';
1975         printk("Sparc Zilog8530 serial driver version %s\n", version);
1976         *p = ' ';
1977 }
1978
1979 /* Probe the PROM for the request zs chip number.
1980  *
1981  * Note: The Sun Voyager shows two addresses and two intr for it's
1982  *       Zilogs, what the second does, I don't know. It does work
1983  *       with using only the first number of each property.  Also
1984  *       we have a special version for sun4u.
1985  */
1986 #ifdef __sparc_v9__
1987 static struct sun_zslayout * __init get_zs(int chip)
1988 {
1989         unsigned int vaddr[2] = { 0, 0 };
1990         unsigned long mapped_addr = 0;
1991         int busnode, seen, zsnode, sun4u_ino;
1992         static int irq = 0;
1993
1994         if(chip < 0 || chip >= NUM_SERIAL) {
1995                 prom_printf("get_zs bogon zs chip number");
1996                 prom_halt();
1997         }
1998
1999         if(central_bus)
2000                 busnode = central_bus->child->prom_node;
2001         else
2002                 busnode = prom_searchsiblings(prom_getchild(prom_root_node), "sbus");
2003         if(busnode == 0 || busnode == -1) {
2004                 prom_printf("get_zs: no zs bus to search");
2005                 prom_halt();
2006         }
2007         zsnode = prom_getchild(busnode);
2008         seen = 0;
2009         while(zsnode) {
2010                 int slave;
2011
2012                 zsnode = prom_searchsiblings(zsnode, "zs");
2013                 slave = prom_getintdefault(zsnode, "slave", -1);
2014                 if((slave == chip) || (seen == chip)) {
2015                         int len = prom_getproperty(zsnode, "address",
2016                                                    (void *) vaddr, sizeof(vaddr));
2017
2018                         if(len == -1 || central_bus != NULL) {
2019                                 struct sbus_bus *sbus = NULL;
2020                                 struct sbus_dev *sdev = NULL;
2021
2022                                 /* "address" property is not guarenteed,
2023                                  * everything in I/O is implicitly mapped
2024                                  * anyways by our clever TLB miss handling
2025                                  * scheme, so don't fail here.  -DaveM
2026                                  */
2027                                 if (central_bus == NULL) {
2028                                         for_each_sbus(sbus) {
2029                                                 for_each_sbusdev(sdev, sbus) {
2030                                                         if (sdev->prom_node == zsnode)
2031                                                                 goto found;
2032                                                 }
2033                                         }
2034                                 }
2035                         found:
2036                                 if (sdev == NULL && central_bus == NULL)
2037                                         prom_halt();
2038                                 if (central_bus == NULL) {
2039                                         mapped_addr =
2040                                             sbus_ioremap(&sdev->resource[0], 0,
2041                                                          PAGE_SIZE, "Zilog Registers");
2042                                 } else {
2043                                         struct linux_prom_registers zsregs[1];
2044                                         int err;
2045
2046                                         err = prom_getproperty(zsnode, "reg",
2047                                                                (char *)&zsregs[0],
2048                                                                sizeof(zsregs));
2049                                         if (err == -1) {
2050                                                 prom_printf("ZS: Cannot map Zilog regs.\n");
2051                                                 prom_halt();
2052                                         }
2053                                         apply_fhc_ranges(central_bus->child, &zsregs[0], 1);
2054                                         apply_central_ranges(central_bus, &zsregs[0], 1);
2055                                         mapped_addr =
2056                                                 ((((u64)zsregs[0].which_io)<<32UL)|
2057                                                  ((u64)zsregs[0].phys_addr));
2058                                 }
2059                         } else if(len % sizeof(unsigned int)) {
2060                                 prom_printf("WHOOPS:  proplen for %s "
2061                                             "was %d, need multiple of "
2062                                             "%d\n", "address", len,
2063                                             sizeof(unsigned int));
2064                                 panic("zilog: address property");
2065                         }
2066                         zs_nodes[chip] = zsnode;
2067                         len = prom_getproperty(zsnode, "interrupts",
2068                                                (char *) &sun4u_ino,
2069                                                (sizeof(sun4u_ino)));
2070                         if(!irq) {
2071                                 if (central_bus) {
2072                                         unsigned long iclr, imap;
2073
2074                                         iclr = central_bus->child->fhc_regs.uregs + FHC_UREGS_ICLR;
2075                                         imap = central_bus->child->fhc_regs.uregs + FHC_UREGS_IMAP;
2076                                         irq = zilog_irq = build_irq(12, 0, iclr, imap);
2077                                 } else {
2078                                         irq = zilog_irq = 
2079                                                 sbus_build_irq(sbus_root, sun4u_ino);
2080                                 }
2081                         }
2082                         break;
2083                 }
2084                 zsnode = prom_getsibling(zsnode);
2085                 seen++;
2086         }
2087         if(!zsnode)
2088                 panic("get_zs: whee chip not found");
2089         if(!vaddr[0] && !mapped_addr)
2090                 panic("get_zs: whee no serial chip mappable");
2091         if (mapped_addr != 0) {
2092                 return (struct sun_zslayout *) mapped_addr;
2093         } else {
2094                 return (struct sun_zslayout *) prom_virt_to_phys((unsigned long)vaddr[0], 0);
2095         }
2096 }
2097 #else /* !(__sparc_v9__) */
2098 static struct sun_zslayout * __init get_zs(int chip)
2099 {
2100         struct linux_prom_irqs tmp_irq[2];
2101         unsigned int paddr = 0;
2102         unsigned int vaddr[2] = { 0, 0 };
2103         int zsnode, tmpnode, iospace, slave, len;
2104         int cpunode = 0, bbnode = 0;
2105         static int irq = 0;
2106         int chipid = chip;
2107
2108         iospace = 0;
2109         if(chip < 0 || chip >= NUM_SERIAL)
2110                 panic("get_zs bogon zs chip number");
2111
2112         if(sparc_cpu_model == sun4) {
2113                 struct resource dummy_resource;
2114
2115                 /* Grrr, these have to be hardcoded aieee */
2116                 switch(chip) {
2117                 case 0:
2118                         paddr = 0xf1000000;
2119                         break;
2120                 case 1:
2121                         paddr = 0xf0000000;
2122                         break;
2123                 };
2124                 iospace = 0;
2125                 zs_nodes[chip] = 0;
2126                 if(!irq)
2127                         zilog_irq = irq = 12;
2128                 dummy_resource.start = paddr;
2129                 dummy_resource.end = paddr + 8 - 1;
2130                 dummy_resource.flags = IORESOURCE_IO;
2131                 vaddr[0] = sbus_ioremap(&dummy_resource, 0,
2132                                         8, "Zilog Serial");
2133         } else {
2134                 /* Can use the prom for other machine types */
2135                 zsnode = prom_getchild(prom_root_node);
2136                 if (sparc_cpu_model == sun4d) {
2137                         int no = 0;
2138
2139                         tmpnode = zsnode;
2140                         zsnode = 0;
2141                         bbnode = 0;
2142                         while (tmpnode && (tmpnode = prom_searchsiblings(tmpnode, "cpu-unit"))) {
2143                                 bbnode = prom_getchild(tmpnode);
2144                                 if (bbnode && (bbnode = prom_searchsiblings(bbnode, "bootbus"))) {
2145                                         if (no == (chip >> 1)) {
2146                                                 cpunode = tmpnode;
2147                                                 zsnode = prom_getchild(bbnode);
2148                                                 chipid = (chip & 1);
2149                                                 break;
2150                                         }
2151                                         no++;
2152                                 }
2153                                 tmpnode = prom_getsibling(tmpnode);
2154                         }
2155                         if (!tmpnode)
2156                                 panic ("get_zs: couldn't find %dth bootbus\n", chip >> 1);
2157                 } else {
2158                         tmpnode = prom_searchsiblings(zsnode, "obio");
2159                         if(tmpnode)
2160                                 zsnode = prom_getchild(tmpnode);
2161                 }
2162                 if(!zsnode)
2163                         panic("get_zs no zs serial prom node");
2164                 while(zsnode) {
2165                         zsnode = prom_searchsiblings(zsnode, "zs");
2166                         slave = prom_getintdefault(zsnode, "slave", -1);
2167                         if(slave == chipid) {
2168                                 /* The one we want */
2169                                 if (sparc_cpu_model != sun4d) {
2170                                         len = prom_getproperty(zsnode, "address",
2171                                                                (void *) vaddr,
2172                                                                sizeof(vaddr));
2173                                         if (len % sizeof(unsigned int)) {
2174                                                 prom_printf("WHOOPS:  proplen for %s "
2175                                                         "was %d, need multiple of "
2176                                                         "%d\n", "address", len,
2177                                                         sizeof(unsigned int));
2178                                                 panic("zilog: address property");
2179                                         }
2180                                 } else {
2181                                         /* On sun4d don't have address property :( */
2182                                         struct linux_prom_registers zsreg[4];
2183                                         struct resource res;
2184                                         
2185                                         if (prom_getproperty(zsnode, "reg", (char *)zsreg, sizeof(zsreg)) == -1) {
2186                                                 prom_printf ("Cannot map zs regs\n");
2187                                                 prom_halt();
2188                                         }
2189                                         prom_apply_generic_ranges(bbnode, cpunode, zsreg, 1);
2190                                         res.start = zsreg[0].phys_addr;
2191                                         res.end = res.start + 8 - 1;
2192                                         res.flags = zsreg[0].which_io | IORESOURCE_IO;
2193                                         vaddr[0] = sbus_ioremap(&res, 0,
2194                                                                 8, "Zilog Serial");
2195                                 }
2196                                 zs_nodes[chip] = zsnode;
2197                                 len = prom_getproperty(zsnode, "intr",
2198                                                        (char *) tmp_irq,
2199                                                        sizeof(tmp_irq));
2200                                 if (len % sizeof(struct linux_prom_irqs)) {
2201                                         prom_printf(
2202                                               "WHOOPS:  proplen for %s "
2203                                               "was %d, need multiple of "
2204                                               "%d\n", "intr", len,
2205                                               sizeof(struct linux_prom_irqs));
2206                                         panic("zilog: intr property");
2207                                 }
2208                                 if(!irq) {
2209                                         irq = zilog_irq = tmp_irq[0].pri;
2210                                 } else {
2211                                         if(tmp_irq[0].pri != irq)
2212                                                 panic("zilog: bogon irqs");
2213                                 }
2214                                 break;
2215                         }
2216                         zsnode = prom_getsibling(zsnode);
2217                 }
2218                 if(!zsnode)
2219                         panic("get_zs whee chip not found");
2220         }
2221         if(!vaddr[0])
2222                 panic("get_zs whee no serial chip mappable");
2223
2224         return (struct sun_zslayout *)(unsigned long) vaddr[0];
2225 }
2226 #endif
2227 /* This is for the auto baud rate detection in the mouse driver. */
2228 void zs_change_mouse_baud(int newbaud)
2229 {
2230         int channel = MOUSE_LINE;
2231         int brg;
2232
2233         zs_soft[channel].zs_baud = newbaud;
2234         brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2235                          (ZS_CLOCK / zs_soft[channel].clk_divisor));
2236         write_zsreg(zs_soft[channel].zs_channel, R12, (brg & 0xff));
2237         write_zsreg(zs_soft[channel].zs_channel, R13, ((brg >> 8) & 0xff));
2238 }
2239
2240 void __init zs_init_alloc_failure(const char *table_name)
2241 {
2242         prom_printf("zs_probe: Cannot alloc %s.\n", table_name);
2243         prom_halt();
2244 }
2245
2246 void * __init zs_alloc_bootmem(unsigned long size)
2247 {
2248         void *ret;
2249
2250         ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
2251         if (ret != NULL)
2252                 memset(ret, 0, size);
2253
2254         return ret;
2255 }
2256
2257 void __init zs_alloc_tables(void)
2258 {
2259         zs_chips = (struct sun_zslayout **)
2260                 zs_alloc_bootmem(NUM_SERIAL * sizeof(struct sun_zslayout *));
2261         if (zs_chips == NULL)
2262                 zs_init_alloc_failure("zs_chips");
2263         zs_channels = (struct sun_zschannel **)
2264                 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct sun_zschannel *));
2265         if (zs_channels == NULL)
2266                 zs_init_alloc_failure("zs_channels");
2267         zs_nodes = (int *)
2268                 zs_alloc_bootmem(NUM_SERIAL * sizeof(int));
2269         if (zs_nodes == NULL)
2270                 zs_init_alloc_failure("zs_nodes");
2271         zs_soft = (struct sun_serial *)
2272                 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct sun_serial));
2273         if (zs_soft == NULL)
2274                 zs_init_alloc_failure("zs_soft");
2275         zs_ttys = (struct tty_struct *)
2276                 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct tty_struct));
2277         if (zs_ttys == NULL)
2278                 zs_init_alloc_failure("zs_ttys");
2279         serial_table = (struct tty_struct **)
2280                 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct tty_struct *));
2281         if (serial_table == NULL)
2282                 zs_init_alloc_failure("serial_table");
2283         serial_termios = (struct termios **)
2284                 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct termios *));
2285         if (serial_termios == NULL)
2286                 zs_init_alloc_failure("serial_termios");
2287         serial_termios_locked = (struct termios **)
2288                 zs_alloc_bootmem(NUM_CHANNELS * sizeof(struct termios *));
2289         if (serial_termios_locked == NULL)
2290                 zs_init_alloc_failure("serial_termios_locked");
2291 }
2292
2293 int __init zs_probe(void)
2294 {
2295         int node;
2296
2297         if(sparc_cpu_model == sun4)
2298                 goto no_probe;
2299
2300         NUM_SERIAL = 0;
2301         
2302         node = prom_getchild(prom_root_node);
2303         if (sparc_cpu_model == sun4d) {
2304                 int bbnode;
2305                 
2306                 while (node && (node = prom_searchsiblings(node, "cpu-unit"))) {
2307                         bbnode = prom_getchild(node);
2308                         if (bbnode && prom_searchsiblings(bbnode, "bootbus"))
2309                                 NUM_SERIAL += 2;
2310                         node = prom_getsibling(node);
2311                 }
2312                 goto no_probe;
2313         }
2314 #ifdef __sparc_v9__
2315         else if (sparc_cpu_model == sun4u) {
2316                 int central_node;
2317
2318                 /* Central bus zilogs must be checked for first,
2319                  * since Enterprise boxes might have SBUSes as well.
2320                  */
2321                 central_node = prom_finddevice("/central");
2322                 if(central_node != 0 && central_node != -1)
2323                         node = prom_searchsiblings(prom_getchild(central_node), "fhc");
2324                 else
2325                         node = prom_searchsiblings(node, "sbus");
2326                 if(node != 0 && node != -1)
2327                         node = prom_getchild(node);
2328                 if(node == 0 || node == -1)
2329                         return -ENODEV;
2330         }
2331 #endif /* __sparc_v9__ */
2332         else {
2333                 node = prom_searchsiblings(node, "obio");
2334                 if(node)
2335                         node = prom_getchild(node);
2336                 NUM_SERIAL = 2;
2337                 goto no_probe;
2338         }
2339
2340         node = prom_searchsiblings(node, "zs");
2341         if (!node)
2342                 return -ENODEV;
2343                 
2344         NUM_SERIAL = 2;
2345
2346 no_probe:
2347         zs_alloc_tables();
2348
2349         /* Fill in rs_ops struct... */
2350 #ifdef CONFIG_SERIAL_CONSOLE
2351         sunserial_setinitfunc(zs_console_init);
2352 #endif
2353         sunserial_setinitfunc(zs_init);
2354         rs_ops.rs_kgdb_hook = zs_kgdb_hook;
2355         rs_ops.rs_change_mouse_baud = zs_change_mouse_baud;
2356
2357         sunkbd_setinitfunc(sun_kbd_init);
2358         kbd_ops.compute_shiftstate = sun_compute_shiftstate;
2359         kbd_ops.setledstate = sun_setledstate;
2360         kbd_ops.getledstate = sun_getledstate;
2361         kbd_ops.setkeycode = sun_setkeycode;
2362         kbd_ops.getkeycode = sun_getkeycode;
2363 #if defined(__sparc_v9__) && defined(CONFIG_PCI)
2364         sunkbd_install_keymaps(sun_key_maps, sun_keymap_count,
2365                                sun_func_buf, sun_func_table,
2366                                sun_funcbufsize, sun_funcbufleft,
2367                                sun_accent_table, sun_accent_table_size);
2368 #endif
2369         return 0;
2370 }
2371
2372 static inline void zs_prepare(void)
2373 {
2374         int channel, chip;
2375         unsigned long flags;
2376
2377         if (!NUM_SERIAL)
2378                 return;
2379         
2380         save_and_cli(flags);
2381         
2382         /* Set up our interrupt linked list */
2383         zs_chain = &zs_soft[0];
2384         for(channel = 0; channel < NUM_CHANNELS - 1; channel++) {
2385                 zs_soft[channel].zs_next = &zs_soft[channel + 1];
2386                 zs_soft[channel].line = channel;
2387         }
2388         zs_soft[channel].zs_next = 0;
2389
2390         /* Initialize Softinfo */
2391         for(chip = 0; chip < NUM_SERIAL; chip++) {
2392                 /* If we are doing kgdb over one of the channels on
2393                  * chip zero, kgdb_channel will be set to 1 by the
2394                  * zs_kgdb_hook() routine below.
2395                  */
2396                 if(!zs_chips[chip]) {
2397                         zs_chips[chip] = get_zs(chip);
2398                         /* Two channels per chip */
2399                         zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2400                         zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2401                         zs_soft[(chip*2)].kgdb_channel = 0;
2402                         zs_soft[(chip*2)+1].kgdb_channel = 0;
2403                 }
2404
2405                 /* First, set up channel A on this chip. */
2406                 channel = chip * 2;
2407                 zs_soft[channel].zs_channel = zs_channels[channel];
2408                 zs_soft[channel].change_needed = 0;
2409                 zs_soft[channel].clk_divisor = 16;
2410                 zs_soft[channel].cons_keyb = 0;
2411                 zs_soft[channel].cons_mouse = 0;
2412                 zs_soft[channel].channelA = 1;
2413
2414                 /* Now, channel B */
2415                 channel++;
2416                 zs_soft[channel].zs_channel = zs_channels[channel];
2417                 zs_soft[channel].change_needed = 0;
2418                 zs_soft[channel].clk_divisor = 16;
2419                 zs_soft[channel].cons_keyb = 0;
2420                 zs_soft[channel].cons_mouse = 0;
2421                 zs_soft[channel].channelA = 0;
2422         }
2423         
2424         restore_flags(flags);
2425 }
2426
2427 int __init zs_init(void)
2428 {
2429         int channel, brg, i;
2430         unsigned long flags;
2431         struct sun_serial *info;
2432         char dummy;
2433
2434         /* Setup base handler, and timer table. */
2435         init_bh(SERIAL_BH, do_serial_bh);
2436
2437         show_serial_version();
2438
2439         /* Initialize the tty_driver structure */
2440         /* SPARC: Not all of this is exactly right for us. */
2441         
2442         memset(&serial_driver, 0, sizeof(struct tty_driver));
2443         serial_driver.magic = TTY_DRIVER_MAGIC;
2444         serial_driver.driver_name = "serial";
2445 #ifdef CONFIG_DEVFS_FS
2446         serial_driver.name = "tts/%d";
2447 #else
2448         serial_driver.name = "ttyS";
2449 #endif
2450         serial_driver.major = TTY_MAJOR;
2451         serial_driver.minor_start = 64;
2452         serial_driver.num = NUM_CHANNELS;
2453         serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2454         serial_driver.subtype = SERIAL_TYPE_NORMAL;
2455         serial_driver.init_termios = tty_std_termios;
2456         serial_driver.init_termios.c_cflag =
2457                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2458         serial_driver.flags = TTY_DRIVER_REAL_RAW;
2459         serial_driver.refcount = &serial_refcount;
2460         serial_driver.table = serial_table;
2461         serial_driver.termios = serial_termios;
2462         serial_driver.termios_locked = serial_termios_locked;
2463
2464         serial_driver.open = zs_open;
2465         serial_driver.close = zs_close;
2466         serial_driver.write = zs_write;
2467         serial_driver.flush_chars = zs_flush_chars;
2468         serial_driver.write_room = zs_write_room;
2469         serial_driver.chars_in_buffer = zs_chars_in_buffer;
2470         serial_driver.flush_buffer = zs_flush_buffer;
2471         serial_driver.ioctl = zs_ioctl;
2472         serial_driver.throttle = zs_throttle;
2473         serial_driver.unthrottle = zs_unthrottle;
2474         serial_driver.set_termios = zs_set_termios;
2475         serial_driver.stop = zs_stop;
2476         serial_driver.start = zs_start;
2477         serial_driver.hangup = zs_hangup;
2478
2479         /* I'm too lazy, someone write versions of this for us. -DaveM */
2480         /* I just did. :-) -AIB 2001-12-23 */
2481         serial_driver.read_proc = zs_read_proc;
2482
2483         /*
2484          * The callout device is just like normal device except for
2485          * major number and the subtype code.
2486          */
2487         callout_driver = serial_driver;
2488         callout_driver.name = "cua/%d";
2489         callout_driver.major = TTYAUX_MAJOR;
2490         callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2491         callout_driver.read_proc = 0;
2492         callout_driver.proc_entry = 0;
2493
2494         if (tty_register_driver(&serial_driver))
2495                 panic("Couldn't register serial driver\n");
2496         if (tty_register_driver(&callout_driver))
2497                 panic("Couldn't register callout driver\n");
2498
2499         save_flags(flags); cli();
2500
2501         /* Initialize Softinfo */
2502         zs_prepare();
2503
2504         /* Grab IRQ line before poking the chips so we do
2505          * not lose any interrupts.
2506          */
2507         if (request_irq(zilog_irq, zs_interrupt, SA_SHIRQ,
2508                         "Zilog8530", zs_chain)) {
2509                 prom_printf("Unable to attach zs intr\n");
2510                 prom_halt();
2511         }
2512
2513         /* Initialize Hardware */
2514         for(channel = 0; channel < NUM_CHANNELS; channel++) {
2515                 /* Hardware reset each chip */
2516                 if (!(channel & 1)) {
2517                         write_zsreg(zs_soft[channel].zs_channel, R9, FHWRES);
2518                         ZSDELAY_LONG();
2519                         dummy = read_zsreg(zs_soft[channel].zs_channel, R0);
2520                 }
2521
2522                 if(channel == KEYBOARD_LINE) {
2523                         zs_soft[channel].cons_keyb = 1;
2524                         zs_soft[channel].parity_mask = 0xff;
2525                         zs_kbdchan = zs_soft[channel].zs_channel;
2526
2527                         write_zsreg(zs_soft[channel].zs_channel, R4,
2528                                     (PAR_EVEN | X16CLK | SB1));
2529                         write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2530                         write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2531                         write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2532                         write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2533                         write_zsreg(zs_soft[channel].zs_channel, R11,
2534                                     (TCBR | RCBR));
2535                         zs_soft[channel].zs_baud = 1200;
2536                         brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2537                                          ZS_CLOCK/zs_soft[channel].clk_divisor);
2538                         write_zsreg(zs_soft[channel].zs_channel, R12,
2539                                     (brg & 0xff));
2540                         write_zsreg(zs_soft[channel].zs_channel, R13,
2541                                     ((brg >> 8) & 0xff));
2542                         write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2543
2544                         /* Enable Rx/Tx, IRQs, and inform kbd driver */
2545                         write_zsreg(zs_soft[channel].zs_channel, R14,
2546                                     (BRSRC | BRENAB));
2547                         write_zsreg(zs_soft[channel].zs_channel, R3,
2548                                     (Rx8 | RxENAB));
2549                         write_zsreg(zs_soft[channel].zs_channel, R5,
2550                                     (Tx8 | TxENAB | DTR | RTS));
2551
2552                         write_zsreg(zs_soft[channel].zs_channel, R15,
2553                                     (DCDIE | CTSIE | TxUIE | BRKIE));
2554                         write_zsreg(zs_soft[channel].zs_channel, R0,
2555                                     RES_EXT_INT);
2556                         write_zsreg(zs_soft[channel].zs_channel, R0,
2557                                     RES_EXT_INT);
2558
2559                         write_zsreg(zs_soft[channel].zs_channel, R1,
2560                                     (EXT_INT_ENAB | INT_ALL_Rx));
2561                         write_zsreg(zs_soft[channel].zs_channel, R9,
2562                                     (NV | MIE));
2563                         ZS_CLEARERR(zs_soft[channel].zs_channel);
2564                         ZS_CLEARFIFO(zs_soft[channel].zs_channel);
2565                 } else if(channel == MOUSE_LINE) {
2566                         zs_soft[channel].cons_mouse = 1;
2567                         zs_soft[channel].parity_mask = 0xff;
2568                         zs_mousechan = zs_soft[channel].zs_channel;
2569
2570                         write_zsreg(zs_soft[channel].zs_channel, R4,
2571                                     (PAR_EVEN | X16CLK | SB1));
2572                         write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2573                         write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2574                         write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2575                         write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2576                         write_zsreg(zs_soft[channel].zs_channel, R11,
2577                                     (TCBR | RCBR));
2578
2579                         zs_soft[channel].zs_baud = 4800;
2580                         brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2581                                          ZS_CLOCK/zs_soft[channel].clk_divisor);
2582                         write_zsreg(zs_soft[channel].zs_channel, R12,
2583                                     (brg & 0xff));
2584                         write_zsreg(zs_soft[channel].zs_channel, R13,
2585                                     ((brg >> 8) & 0xff));
2586                         write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2587
2588                         /* Enable Rx, IRQs, and inform mouse driver */
2589                         write_zsreg(zs_soft[channel].zs_channel, R14,
2590                                     (BRSRC | BRENAB));
2591                         write_zsreg(zs_soft[channel].zs_channel, R3,
2592                                     (Rx8 | RxENAB));
2593                         write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2594
2595                         write_zsreg(zs_soft[channel].zs_channel, R15,
2596                                     (DCDIE | CTSIE | TxUIE | BRKIE));
2597                         write_zsreg(zs_soft[channel].zs_channel, R0,
2598                                     RES_EXT_INT);
2599                         write_zsreg(zs_soft[channel].zs_channel, R0,
2600                                     RES_EXT_INT);
2601
2602                         write_zsreg(zs_soft[channel].zs_channel, R1,
2603                                     (EXT_INT_ENAB | INT_ALL_Rx));
2604                         write_zsreg(zs_soft[channel].zs_channel, R9,
2605                                     (NV | MIE));
2606
2607                         sun_mouse_zsinit();
2608                 } else if (zs_soft[channel].is_cons) {
2609                         brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2610                                          ZS_CLOCK/zs_soft[channel].clk_divisor);
2611                         zscons_regs[12] = brg & 0xff;
2612                         zscons_regs[13] = (brg >> 8) & 0xff;
2613
2614                         memcpy(zs_soft[channel].curregs, zscons_regs, sizeof(zscons_regs));
2615                         load_zsregs(&zs_soft[channel], zscons_regs);
2616
2617                         ZS_CLEARERR(zs_soft[channel].zs_channel);
2618                         ZS_CLEARFIFO(zs_soft[channel].zs_channel);
2619                 } else if (zs_soft[channel].kgdb_channel) {
2620                         /* If this is the kgdb line, enable interrupts because
2621                          * we now want to receive the 'control-c' character
2622                          * from the client attached to us asynchronously.
2623                          */
2624                         zs_soft[channel].parity_mask = 0xff;
2625                         kgdb_chaninit(&zs_soft[channel], 1,
2626                                       zs_soft[channel].zs_baud);
2627                 } else {
2628                         zs_soft[channel].parity_mask = 0xff;
2629                         write_zsreg(zs_soft[channel].zs_channel, R4,
2630                                     (PAR_EVEN | X16CLK | SB1));
2631                         write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2632                         write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2633                         write_zsreg(zs_soft[channel].zs_channel, R9, NV);
2634                         write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
2635                         write_zsreg(zs_soft[channel].zs_channel, R11,
2636                                     (RCBR | TCBR));
2637                         zs_soft[channel].zs_baud = 9600;
2638                         brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
2639                                          ZS_CLOCK/zs_soft[channel].clk_divisor);
2640                         write_zsreg(zs_soft[channel].zs_channel, R12,
2641                                     (brg & 0xff));
2642                         write_zsreg(zs_soft[channel].zs_channel, R13,
2643                                     ((brg >> 8) & 0xff));
2644                         write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
2645                         write_zsreg(zs_soft[channel].zs_channel, R14,
2646                                     (BRSRC | BRENAB));
2647                         write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
2648                         write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
2649                         write_zsreg(zs_soft[channel].zs_channel, R15, DCDIE);
2650                         write_zsreg(zs_soft[channel].zs_channel, R9, NV | MIE);
2651                         write_zsreg(zs_soft[channel].zs_channel, R0,
2652                                     RES_EXT_INT);
2653                         write_zsreg(zs_soft[channel].zs_channel, R0,
2654                                     RES_EXT_INT);
2655                 }
2656         }
2657
2658         for (info = zs_chain, i=0; info; info = info->zs_next, i++) {
2659                 info->magic = SERIAL_MAGIC;
2660                 info->port = (long) info->zs_channel;
2661                 info->line = i;
2662                 info->tty = 0;
2663                 info->irq = zilog_irq;
2664                 info->custom_divisor = 16;
2665                 info->close_delay = 50;
2666                 info->closing_wait = 3000;
2667                 info->x_char = 0;
2668                 info->event = 0;
2669                 info->count = 0;
2670                 info->blocked_open = 0;
2671                 info->tqueue.routine = do_softint;
2672                 info->tqueue.data = info;
2673                 info->tqueue_hangup.routine = do_serial_hangup;
2674                 info->tqueue_hangup.data = info;
2675                 info->callout_termios = callout_driver.init_termios;
2676                 info->normal_termios = serial_driver.init_termios;
2677                 init_waitqueue_head(&info->open_wait);
2678                 init_waitqueue_head(&info->close_wait);
2679                 printk("tty%02d at 0x%04x (irq = %s)", info->line, 
2680                        info->port, __irq_itoa(info->irq));
2681                 printk(" is a Zilog8530\n");
2682         }
2683
2684         restore_flags(flags);
2685
2686         keyboard_zsinit(kbd_put_char);
2687         return 0;
2688 }
2689
2690 /* This is called at boot time to prime the kgdb serial debugging
2691  * serial line.  The 'tty_num' argument is 0 for /dev/ttya and 1
2692  * for /dev/ttyb which is determined in setup_arch() from the
2693  * boot command line flags.
2694  */
2695 static void __init zs_kgdb_hook(int tty_num)
2696 {
2697         int chip = 0;
2698
2699         if(!zs_chips[chip]) {
2700                 zs_chips[chip] = get_zs(chip);
2701                 /* Two channels per chip */
2702                 zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2703                 zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2704         }
2705         zs_soft[tty_num].zs_channel = zs_channels[tty_num];
2706         zs_kgdbchan = zs_soft[tty_num].zs_channel;
2707         zs_soft[tty_num].change_needed = 0;
2708         zs_soft[tty_num].clk_divisor = 16;
2709         zs_soft[tty_num].zs_baud = 9600;
2710         zs_soft[tty_num].kgdb_channel = 1;     /* This runs kgdb */
2711         zs_soft[tty_num ^ 1].kgdb_channel = 0; /* This does not */
2712         /* Turn on transmitter/receiver at 8-bits/char */
2713         kgdb_chaninit(&zs_soft[tty_num], 0, 9600);
2714         ZS_CLEARERR(zs_kgdbchan);
2715         ZS_CLEARFIFO(zs_kgdbchan);
2716 }
2717
2718 #ifdef CONFIG_SERIAL_CONSOLE
2719
2720 /* This is for console output over ttya/ttyb */
2721 static void
2722 zs_console_putchar(struct sun_serial *info, char ch)
2723 {
2724         int loops = ZS_PUT_CHAR_MAX_DELAY;
2725         unsigned long flags;
2726
2727         if(!info->zs_channel)
2728                 return;
2729
2730         save_flags(flags); cli();
2731         zs_put_char(info->zs_channel, ch);
2732         while (!(read_zsreg(info->zs_channel, R1) & ALL_SNT) && --loops)
2733                 udelay(5);
2734         restore_flags(flags);
2735 }
2736
2737 #ifdef SERIAL_CONSOLE_FAIR_OUTPUT
2738 /*
2739  * Fair output driver allows a process to speak.
2740  */
2741 static void zs_fair_output(struct sun_serial *info)
2742 {
2743         unsigned long flags;
2744         int left;               /* Output no more than that */
2745         char c;
2746
2747         if (info == NULL)
2748                 return;
2749         if (info->xmit_buf == NULL)
2750                 return;
2751
2752         save_flags(flags);  cli();
2753         left = info->xmit_cnt;
2754         while (left != 0) {
2755                 c = info->xmit_buf[info->xmit_tail];
2756                 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
2757                 info->xmit_cnt--;
2758                 restore_flags(flags);
2759
2760                 zs_console_putchar(info, c);
2761
2762                 cli();
2763                 left = MIN(info->xmit_cnt, left-1);
2764         }
2765
2766         /* Last character is being transmitted now (hopefully). */
2767         sbus_writeb(RES_Tx_P, &info->zs_channel->control);
2768         ZSDELAY();
2769         ZSLOG(REGCTRL, RES_Tx_P, 1);
2770
2771         restore_flags(flags);
2772         return;
2773 }
2774 #endif
2775
2776 /*
2777  * zs_console_write is registered for printk.
2778  */
2779 static void
2780 zs_console_write(struct console *con, const char *s, unsigned count)
2781 {
2782         struct sun_serial *info;
2783         int i;
2784
2785         info = zs_soft + con->index;
2786
2787         for (i = 0; i < count; i++, s++) {
2788                 if(*s == '\n')
2789                         zs_console_putchar(info, '\r');
2790                 zs_console_putchar(info, *s);
2791         }
2792 #ifdef SERIAL_CONSOLE_FAIR_OUTPUT
2793         /* Comment this if you want to have a strict interrupt-driven output */
2794         zs_fair_output(info);
2795 #endif
2796 }
2797
2798 static kdev_t zs_console_device(struct console *con)
2799 {
2800         return MKDEV(TTY_MAJOR, 64 + con->index);
2801 }
2802
2803 static int __init zs_console_setup(struct console *con, char *options)
2804 {
2805         static struct tty_struct c_tty;
2806         static struct termios c_termios;
2807         struct sun_serial *info;
2808         int brg, baud;
2809
2810         info = zs_soft + con->index;
2811         info->is_cons = 1;
2812
2813         printk("Console: ttyS%d (Zilog8530)\n", info->line);
2814         
2815         sunserial_console_termios(con);
2816         memset(&c_tty, 0, sizeof(c_tty));
2817         memset(&c_termios, 0, sizeof(c_termios));
2818         c_tty.termios = &c_termios;
2819         c_termios.c_cflag = con->cflag;
2820         baud = tty_get_baud_rate(&c_tty);
2821
2822         info->zs_baud = baud;
2823
2824         switch (con->cflag & CSIZE) {
2825                 case CS5:
2826                         zscons_regs[3] = Rx5 | RxENAB;
2827                         zscons_regs[5] = Tx5 | TxENAB;
2828                         info->parity_mask = 0x1f;
2829                         break;
2830                 case CS6:
2831                         zscons_regs[3] = Rx6 | RxENAB;
2832                         zscons_regs[5] = Tx6 | TxENAB;
2833                         info->parity_mask = 0x3f;
2834                         break;
2835                 case CS7:
2836                         zscons_regs[3] = Rx7 | RxENAB;
2837                         zscons_regs[5] = Tx7 | TxENAB;
2838                         info->parity_mask = 0x7f;
2839                         break;
2840                 default:
2841                 case CS8:
2842                         zscons_regs[3] = Rx8 | RxENAB;
2843                         zscons_regs[5] = Tx8 | TxENAB;
2844                         info->parity_mask = 0xff;
2845                         break;
2846         }
2847         zscons_regs[5] |= DTR;
2848
2849         if (con->cflag & PARENB)
2850                 zscons_regs[4] |= PAR_ENAB;
2851         if (!(con->cflag & PARODD))
2852                 zscons_regs[4] |= PAR_EVEN;
2853
2854         if (con->cflag & CSTOPB)
2855                 zscons_regs[4] |= SB2;
2856         else
2857                 zscons_regs[4] |= SB1;
2858
2859         brg = BPS_TO_BRG(baud, ZS_CLOCK / info->clk_divisor);
2860         zscons_regs[12] = brg & 0xff;
2861         zscons_regs[13] = (brg >> 8) & 0xff;
2862
2863         memcpy(info->curregs, zscons_regs, sizeof(zscons_regs));
2864         load_zsregs(info, zscons_regs);
2865
2866         ZS_CLEARERR(info->zs_channel);
2867         ZS_CLEARFIFO(info->zs_channel);
2868         return 0;
2869 }
2870
2871 static struct console zs_console = {
2872         name:           "ttyS",
2873         write:          zs_console_write,
2874         device:         zs_console_device,
2875         setup:          zs_console_setup,
2876         flags:          CON_PRINTBUFFER,
2877         index:          -1,
2878 };
2879
2880 static int __init zs_console_init(void)
2881 {
2882         extern int con_is_present(void);
2883
2884         if (con_is_present())
2885                 return 0;
2886
2887         zs_console.index = serial_console - 1;
2888         register_console(&zs_console);
2889         return 0;
2890 }
2891
2892 #endif /* CONFIG_SERIAL_CONSOLE */