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