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