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