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