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