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