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