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