2 * Copyright (C) 1995 by Pavel Antonov, Moscow, Russia.
3 * Copyright (C) 1995 by Andrey A. Chernov, Moscow, Russia.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/i386/isa/rc.c,v 1.53.2.1 2001/02/26 04:23:10 jlemon Exp $
28 * $DragonFly: src/sys/dev/serial/rc/rc.c,v 1.22 2006/12/22 23:26:24 swildner Exp $
33 * SDL Communications Riscom/8 (based on Cirrus Logic CL-CD180) driver
41 #include <sys/param.h>
42 #include <sys/systm.h>
46 #include <sys/dkstat.h>
47 #include <sys/fcntl.h>
48 #include <sys/interrupt.h>
49 #include <sys/kernel.h>
50 #include <sys/thread2.h>
51 #include <machine/clock.h>
53 #include <bus/isa/i386/isa_device.h>
55 #include <machine_base/isa/ic/cd180.h>
59 static int rcprobe (struct isa_device *);
60 static int rcattach (struct isa_device *);
62 #define rcin(port) RC_IN (nec, port)
63 #define rcout(port,v) RC_OUT (nec, port, v)
65 #define WAITFORCCR(u,c) rc_wait0(nec, (u), (c), __LINE__)
66 #define CCRCMD(u,c,cmd) WAITFORCCR((u), (c)); rcout(CD180_CCR, (cmd))
68 #define RC_IBUFSIZE 256
69 #define RB_I_HIGH_WATER (TTYHOG - 2 * RC_IBUFSIZE)
70 #define RC_OBUFSIZE 512
71 #define RC_IHIGHWATER (3 * RC_IBUFSIZE / 4)
72 #define INPUT_FLAGS_SHIFT (2 * RC_IBUFSIZE)
73 #define LOTS_OF_EVENTS 64
75 #define RC_FAKEID 0x10
80 #define GET_UNIT(dev) (minor(dev) & 0x3F)
81 #define CALLOUT(dev) (minor(dev) & 0x80)
83 /* For isa routines */
84 struct isa_driver rcdriver = {
85 rcprobe, rcattach, "rc"
88 static d_open_t rcopen;
89 static d_close_t rcclose;
90 static d_ioctl_t rcioctl;
93 static struct dev_ops rc_ops = {
94 { "rc", CDEV_MAJOR, D_TTY | D_KQFILTER },
101 .d_kqfilter = ttykqfilter
104 /* Per-board structure */
105 static struct rc_softc {
106 u_int rcb_probed; /* 1 - probed, 2 - attached */
107 u_int rcb_addr; /* Base I/O addr */
108 u_int rcb_unit; /* unit # */
109 u_char rcb_dtr; /* DTR status */
110 struct rc_chans *rcb_baserc; /* base rc ptr */
113 /* Per-channel structure */
114 static struct rc_chans {
115 struct rc_softc *rc_rcb; /* back ptr */
116 u_short rc_flags; /* Misc. flags */
117 int rc_chan; /* Channel # */
118 u_char rc_ier; /* intr. enable reg */
119 u_char rc_msvr; /* modem sig. status */
120 u_char rc_cor2; /* options reg */
121 u_char rc_pendcmd; /* special cmd pending */
122 u_int rc_dtrwait; /* dtr timeout */
123 u_int rc_dcdwaits; /* how many waits DCD in open */
124 u_char rc_hotchar; /* end packed optimize */
125 struct tty *rc_tp; /* tty struct */
126 u_char *rc_iptr; /* Chars input buffer */
127 u_char *rc_hiwat; /* hi-water mark */
128 u_char *rc_bufend; /* end of buffer */
129 u_char *rc_optr; /* ptr in output buf */
130 u_char *rc_obufend; /* end of output buf */
131 struct callout rc_dtr_ch;
132 u_char rc_ibuf[4 * RC_IBUFSIZE]; /* input buffer */
133 u_char rc_obuf[RC_OBUFSIZE]; /* output buffer */
134 } rc_chans[NRC * CD180_NCHAN];
136 static int rc_scheduled_event = 0;
137 static struct callout rc_wakeup_ch;
140 static struct tty rc_tty[NRC * CD180_NCHAN];
141 static const int nrc_tty = NRC * CD180_NCHAN;
144 #define RC_DTR_OFF 0x0001 /* DTR wait, for close/open */
145 #define RC_ACTOUT 0x0002 /* Dial-out port active */
146 #define RC_RTSFLOW 0x0004 /* RTS flow ctl enabled */
147 #define RC_CTSFLOW 0x0008 /* CTS flow ctl enabled */
148 #define RC_DORXFER 0x0010 /* RXFER event planned */
149 #define RC_DOXXFER 0x0020 /* XXFER event planned */
150 #define RC_MODCHG 0x0040 /* Modem status changed */
151 #define RC_OSUSP 0x0080 /* Output suspended */
152 #define RC_OSBUSY 0x0100 /* start() routine in progress */
153 #define RC_WAS_BUFOVFL 0x0200 /* low-level buffer ovferflow */
154 #define RC_WAS_SILOVFL 0x0400 /* silo buffer overflow */
155 #define RC_SEND_RDY 0x0800 /* ready to send */
157 /* Table for translation of RCSR status bits to internal form */
158 static int rc_rcsrt[16] = {
160 TTY_FE|TTY_OE, TTY_PE, TTY_PE|TTY_OE,
161 TTY_PE|TTY_FE, TTY_PE|TTY_FE|TTY_OE, TTY_BI,
162 TTY_BI|TTY_OE, TTY_BI|TTY_FE, TTY_BI|TTY_FE|TTY_OE,
163 TTY_BI|TTY_PE, TTY_BI|TTY_PE|TTY_OE, TTY_BI|TTY_PE|TTY_FE,
164 TTY_BI|TTY_PE|TTY_FE|TTY_OE
167 /* Static prototypes */
168 static inthand2_t rcintr;
169 static void rc_hwreset (int, int, unsigned int);
170 static int rc_test (int, int);
171 static void rc_discard_output (struct rc_chans *);
172 static void rc_hardclose (struct rc_chans *);
173 static int rc_modctl (struct rc_chans *, int, int);
174 static void rc_start (struct tty *);
175 static void rc_stop (struct tty *, int rw);
176 static int rc_param (struct tty *, struct termios *);
177 static inthand2_t rcpoll;
178 static void rc_reinit (struct rc_softc *);
180 static void printrcflags();
182 static timeout_t rc_dtrwakeup;
183 static timeout_t rc_wakeup;
184 static void disc_optim (struct tty *tp, struct termios *t, struct rc_chans *);
185 static void rc_wait0 (int nec, int unit, int chan, int line);
187 /**********************************************/
189 /* Quick device probing */
192 struct isa_device *dvp;
194 int irq = ffs(dvp->id_irq) - 1;
195 int nec = dvp->id_iobase;
197 if (dvp->id_unit > NRC)
199 if (!RC_VALIDADDR(nec)) {
200 kprintf("rc%d: illegal base address %x\n", dvp->id_unit, nec);
203 if (!RC_VALIDIRQ(irq)) {
204 kprintf("rc%d: illegal IRQ value %d\n", dvp->id_unit, irq);
207 rcout(CD180_PPRL, 0x22); /* Random values to Prescale reg. */
208 rcout(CD180_PPRH, 0x11);
209 if (rcin(CD180_PPRL) != 0x22 || rcin(CD180_PPRH) != 0x11)
211 /* Now, test the board more thoroughly, with diagnostic */
212 if (rc_test(nec, dvp->id_unit))
214 rc_softc[dvp->id_unit].rcb_probed = RC_PROBED;
221 struct isa_device *dvp;
223 int chan, nec = dvp->id_iobase;
224 struct rc_softc *rcb = &rc_softc[dvp->id_unit];
225 struct rc_chans *rc = &rc_chans[dvp->id_unit * CD180_NCHAN];
226 static int rc_started = 0;
229 dvp->id_intr = rcintr;
231 /* Thorooughly test the device */
232 if (rcb->rcb_probed != RC_PROBED)
236 rcb->rcb_baserc = rc;
237 rcb->rcb_unit = dvp->id_unit;
238 /*rcb->rcb_chipid = 0x10 + dvp->id_unit;*/
239 kprintf("rc%d: %d chans, firmware rev. %c\n", rcb->rcb_unit,
240 CD180_NCHAN, (rcin(CD180_GFRCR) & 0xF) + 'A');
242 for (chan = 0; chan < CD180_NCHAN; chan++, rc++) {
243 callout_init(&rc->rc_dtr_ch);
246 rc->rc_iptr = rc->rc_ibuf;
247 rc->rc_bufend = &rc->rc_ibuf[RC_IBUFSIZE];
248 rc->rc_hiwat = &rc->rc_ibuf[RC_IHIGHWATER];
249 rc->rc_flags = rc->rc_ier = rc->rc_msvr = 0;
250 rc->rc_cor2 = rc->rc_pendcmd = 0;
251 rc->rc_optr = rc->rc_obufend = rc->rc_obuf;
252 rc->rc_dtrwait = 3 * hz;
255 tp = rc->rc_tp = &rc_tty[chan + (dvp->id_unit * CD180_NCHAN)];
257 tp->t_lflag = tp->t_iflag = tp->t_oflag = 0;
258 tp->t_cflag = TTYDEF_CFLAG;
259 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
261 rcb->rcb_probed = RC_ATTACHED;
263 dev_ops_add(&rc_ops, -1, rcb->rcb_unit);
264 register_swi(SWI_TTY, rcpoll, NULL, "rcpoll", NULL);
265 callout_init(&rc_wakeup_ch);
272 /* RC interrupt handling */
274 rcintr(void *arg, void *frame)
277 struct rc_softc *rcb = &rc_softc[unit];
280 u_char val, iack, bsr, ucnt, *optr;
281 int good_data, t_state;
283 if (rcb->rcb_probed != RC_ATTACHED) {
284 kprintf("rc%d: bogus interrupt\n", unit);
289 bsr = ~(rcin(RC_BSR));
291 if (!(bsr & (RC_BSR_TOUT|RC_BSR_RXINT|RC_BSR_TXINT|RC_BSR_MOINT))) {
292 kprintf("rc%d: extra interrupt\n", unit);
293 rcout(CD180_EOIR, 0);
297 while (bsr & (RC_BSR_TOUT|RC_BSR_RXINT|RC_BSR_TXINT|RC_BSR_MOINT)) {
298 #ifdef RCDEBUG_DETAILED
299 kprintf("rc%d: intr (%02x) %s%s%s%s\n", unit, bsr,
300 (bsr & RC_BSR_TOUT)?"TOUT ":"",
301 (bsr & RC_BSR_RXINT)?"RXINT ":"",
302 (bsr & RC_BSR_TXINT)?"TXINT ":"",
303 (bsr & RC_BSR_MOINT)?"MOINT":"");
305 if (bsr & RC_BSR_TOUT) {
306 kprintf("rc%d: hardware failure, reset board\n", unit);
311 if (bsr & RC_BSR_RXINT) {
312 iack = rcin(RC_PILR_RX);
313 good_data = (iack == (GIVR_IT_RGDI | RC_FAKEID));
314 if (!good_data && iack != (GIVR_IT_REI | RC_FAKEID)) {
315 kprintf("rc%d: fake rxint: %02x\n", unit, iack);
318 rc = rcb->rcb_baserc + ((rcin(CD180_GICR) & GICR_CHAN) >> GICR_LSH);
319 t_state = rc->rc_tp->t_state;
320 /* Do RTS flow control stuff */
321 if ( (rc->rc_flags & RC_RTSFLOW)
322 || !(t_state & TS_ISOPEN)
324 if ( ( !(t_state & TS_ISOPEN)
325 || (t_state & TS_TBLOCK)
327 && (rc->rc_msvr & MSVR_RTS)
330 rc->rc_msvr &= ~MSVR_RTS);
331 else if (!(rc->rc_msvr & MSVR_RTS))
333 rc->rc_msvr |= MSVR_RTS);
335 ucnt = rcin(CD180_RDCR) & 0xF;
338 if (t_state & TS_ISOPEN) {
339 /* check for input buffer overflow */
340 if ((rc->rc_iptr + ucnt) >= rc->rc_bufend) {
342 ucnt = rc->rc_bufend - rc->rc_iptr;
344 if (!(rc->rc_flags & RC_WAS_BUFOVFL)) {
345 rc->rc_flags |= RC_WAS_BUFOVFL;
346 rc_scheduled_event++;
350 /* check foor good data */
353 val = rcin(CD180_RDR);
355 optr[INPUT_FLAGS_SHIFT] = 0;
357 rc_scheduled_event++;
358 if (val != 0 && val == rc->rc_hotchar)
362 /* Store also status data */
364 iack = rcin(CD180_RCSR);
365 if (iack & RCSR_Timeout)
367 if ( (iack & RCSR_OE)
368 && !(rc->rc_flags & RC_WAS_SILOVFL)) {
369 rc->rc_flags |= RC_WAS_SILOVFL;
370 rc_scheduled_event++;
372 val = rcin(CD180_RDR);
374 Don't store PE if IGNPAR and BREAK if IGNBRK,
375 this hack allows "raw" tty optimization
376 works even if IGN* is set.
378 if ( !(iack & (RCSR_PE|RCSR_FE|RCSR_Break))
379 || ((!(iack & (RCSR_PE|RCSR_FE))
380 || !(rc->rc_tp->t_iflag & IGNPAR))
381 && (!(iack & RCSR_Break)
382 || !(rc->rc_tp->t_iflag & IGNBRK)))) {
383 if ( (iack & (RCSR_PE|RCSR_FE))
384 && (t_state & TS_CAN_BYPASS_L_RINT)
387 && (rc->rc_tp->t_iflag & INPCK))))
389 else if (val != 0 && val == rc->rc_hotchar)
392 optr[INPUT_FLAGS_SHIFT] = iack;
394 rc_scheduled_event++;
399 rc->rc_flags |= RC_DORXFER;
402 /* Clear FIFO if necessary */
403 while (resid-- > 0) {
405 iack = rcin(CD180_RCSR);
408 if (iack & RCSR_Timeout)
410 (void) rcin(CD180_RDR);
414 if (bsr & RC_BSR_MOINT) {
415 iack = rcin(RC_PILR_MODEM);
416 if (iack != (GIVR_IT_MSCI | RC_FAKEID)) {
417 kprintf("rc%d: fake moint: %02x\n", unit, iack);
420 rc = rcb->rcb_baserc + ((rcin(CD180_GICR) & GICR_CHAN) >> GICR_LSH);
421 iack = rcin(CD180_MCR);
422 rc->rc_msvr = rcin(CD180_MSVR);
425 printrcflags(rc, "moint");
427 if (rc->rc_flags & RC_CTSFLOW) {
428 if (rc->rc_msvr & MSVR_CTS)
429 rc->rc_flags |= RC_SEND_RDY;
431 rc->rc_flags &= ~RC_SEND_RDY;
433 rc->rc_flags |= RC_SEND_RDY;
434 if ((iack & MCR_CDchg) && !(rc->rc_flags & RC_MODCHG)) {
435 rc_scheduled_event += LOTS_OF_EVENTS;
436 rc->rc_flags |= RC_MODCHG;
441 if (bsr & RC_BSR_TXINT) {
442 iack = rcin(RC_PILR_TX);
443 if (iack != (GIVR_IT_TDI | RC_FAKEID)) {
444 kprintf("rc%d: fake txint: %02x\n", unit, iack);
447 rc = rcb->rcb_baserc + ((rcin(CD180_GICR) & GICR_CHAN) >> GICR_LSH);
448 if ( (rc->rc_flags & RC_OSUSP)
449 || !(rc->rc_flags & RC_SEND_RDY)
452 /* Handle breaks and other stuff */
453 if (rc->rc_pendcmd) {
454 rcout(CD180_COR2, rc->rc_cor2 |= COR2_ETC);
455 rcout(CD180_TDR, CD180_C_ESC);
456 rcout(CD180_TDR, rc->rc_pendcmd);
457 rcout(CD180_COR2, rc->rc_cor2 &= ~COR2_ETC);
462 resid = rc->rc_obufend - optr;
463 if (resid > CD180_NFIFO)
466 rcout(CD180_TDR, *optr++);
469 /* output completed? */
470 if (optr >= rc->rc_obufend) {
471 rcout(CD180_IER, rc->rc_ier &= ~IER_TxRdy);
473 kprintf("rc%d/%d: output completed\n", unit, rc->rc_chan);
475 if (!(rc->rc_flags & RC_DOXXFER)) {
476 rc_scheduled_event += LOTS_OF_EVENTS;
477 rc->rc_flags |= RC_DOXXFER;
483 rcout(CD180_EOIR, 0); /* end of interrupt */
485 bsr = ~(rcin(RC_BSR));
489 /* Feed characters to output buffer */
490 static void rc_start(tp)
493 struct rc_chans *rc = &rc_chans[GET_UNIT(tp->t_dev)];
494 int nec = rc->rc_rcb->rcb_addr;
496 if (rc->rc_flags & RC_OSBUSY)
499 rc->rc_flags |= RC_OSBUSY;
501 if (tp->t_state & TS_TTSTOP)
502 rc->rc_flags |= RC_OSUSP;
504 rc->rc_flags &= ~RC_OSUSP;
505 /* Do RTS flow control stuff */
506 if ( (rc->rc_flags & RC_RTSFLOW)
507 && (tp->t_state & TS_TBLOCK)
508 && (rc->rc_msvr & MSVR_RTS)
510 rcout(CD180_CAR, rc->rc_chan);
511 rcout(CD180_MSVR, rc->rc_msvr &= ~MSVR_RTS);
512 } else if (!(rc->rc_msvr & MSVR_RTS)) {
513 rcout(CD180_CAR, rc->rc_chan);
514 rcout(CD180_MSVR, rc->rc_msvr |= MSVR_RTS);
517 if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP))
520 printrcflags(rc, "rcstart");
524 kprintf("rcstart: outq = %d obuf = %d\n",
525 tp->t_outq.c_cc, rc->rc_obufend - rc->rc_optr);
527 if (tp->t_state & TS_BUSY)
528 goto out; /* output still in progress ... */
530 if (tp->t_outq.c_cc > 0) {
533 tp->t_state |= TS_BUSY;
534 ocnt = q_to_b(&tp->t_outq, rc->rc_obuf, sizeof rc->rc_obuf);
536 rc->rc_optr = rc->rc_obuf;
537 rc->rc_obufend = rc->rc_optr + ocnt;
539 if (!(rc->rc_ier & IER_TxRdy)) {
541 kprintf("rc%d/%d: rcstart enable txint\n", rc->rc_rcb->rcb_unit, rc->rc_chan);
543 rcout(CD180_CAR, rc->rc_chan);
544 rcout(CD180_IER, rc->rc_ier |= IER_TxRdy);
548 rc->rc_flags &= ~RC_OSBUSY;
552 /* Handle delayed events. */
554 rcpoll(void *dummy, void *frame)
557 struct rc_softc *rcb;
560 int chan, icnt, nec, unit;
562 if (rc_scheduled_event == 0)
565 for (unit = 0; unit < NRC; unit++) {
566 rcb = &rc_softc[unit];
567 rc = rcb->rcb_baserc;
568 nec = rc->rc_rcb->rcb_addr;
569 for (chan = 0; chan < CD180_NCHAN; rc++, chan++) {
572 if (rc->rc_flags & (RC_DORXFER|RC_DOXXFER|RC_MODCHG|
573 RC_WAS_BUFOVFL|RC_WAS_SILOVFL))
574 printrcflags(rc, "rcevent");
576 if (rc->rc_flags & RC_WAS_BUFOVFL) {
578 rc->rc_flags &= ~RC_WAS_BUFOVFL;
579 rc_scheduled_event--;
581 kprintf("rc%d/%d: interrupt-level buffer overflow\n",
584 if (rc->rc_flags & RC_WAS_SILOVFL) {
586 rc->rc_flags &= ~RC_WAS_SILOVFL;
587 rc_scheduled_event--;
589 kprintf("rc%d/%d: silo overflow\n",
592 if (rc->rc_flags & RC_MODCHG) {
594 rc->rc_flags &= ~RC_MODCHG;
595 rc_scheduled_event -= LOTS_OF_EVENTS;
597 (*linesw[tp->t_line].l_modem)(tp, !!(rc->rc_msvr & MSVR_CD));
599 if (rc->rc_flags & RC_DORXFER) {
601 rc->rc_flags &= ~RC_DORXFER;
603 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE])
604 tptr = &rc->rc_ibuf[RC_IBUFSIZE];
609 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) {
610 rc->rc_iptr = rc->rc_ibuf;
611 rc->rc_bufend = &rc->rc_ibuf[RC_IBUFSIZE];
612 rc->rc_hiwat = &rc->rc_ibuf[RC_IHIGHWATER];
614 rc->rc_iptr = &rc->rc_ibuf[RC_IBUFSIZE];
615 rc->rc_bufend = &rc->rc_ibuf[2 * RC_IBUFSIZE];
617 &rc->rc_ibuf[RC_IBUFSIZE + RC_IHIGHWATER];
619 if ( (rc->rc_flags & RC_RTSFLOW)
620 && (tp->t_state & TS_ISOPEN)
621 && !(tp->t_state & TS_TBLOCK)
622 && !(rc->rc_msvr & MSVR_RTS)
624 rcout(CD180_CAR, chan);
626 rc->rc_msvr |= MSVR_RTS);
628 rc_scheduled_event -= icnt;
632 if (icnt <= 0 || !(tp->t_state & TS_ISOPEN))
635 if ( (tp->t_state & TS_CAN_BYPASS_L_RINT)
636 && !(tp->t_state & TS_LOCAL)) {
637 if ((tp->t_rawq.c_cc + icnt) >= RB_I_HIGH_WATER
638 && ((rc->rc_flags & RC_RTSFLOW) || (tp->t_iflag & IXOFF))
639 && !(tp->t_state & TS_TBLOCK))
644 if (b_to_q(tptr, icnt, &tp->t_rawq))
645 kprintf("rc%d/%d: tty-level buffer overflow\n",
648 if ((tp->t_state & TS_TTSTOP) && ((tp->t_iflag & IXANY)
649 || (tp->t_cc[VSTART] == tp->t_cc[VSTOP]))) {
650 tp->t_state &= ~TS_TTSTOP;
651 tp->t_lflag &= ~FLUSHO;
655 for (; tptr < eptr; tptr++)
656 (*linesw[tp->t_line].l_rint)
658 rc_rcsrt[tptr[INPUT_FLAGS_SHIFT] & 0xF], tp);
662 if (rc->rc_flags & RC_DOXXFER) {
664 rc_scheduled_event -= LOTS_OF_EVENTS;
665 rc->rc_flags &= ~RC_DOXXFER;
666 rc->rc_tp->t_state &= ~TS_BUSY;
668 (*linesw[tp->t_line].l_start)(tp);
671 if (rc_scheduled_event == 0)
674 if (rc_scheduled_event >= LOTS_OF_EVENTS)
683 struct rc_chans *rc = &rc_chans[GET_UNIT(tp->t_dev)];
687 kprintf("rc%d/%d: rc_stop %s%s\n", rc->rc_rcb->rcb_unit, rc->rc_chan,
688 (rw & FWRITE)?"FWRITE ":"", (rw & FREAD)?"FREAD":"");
691 rc_discard_output(rc);
694 rc->rc_flags &= ~RC_DORXFER;
696 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) {
697 tptr = &rc->rc_ibuf[RC_IBUFSIZE];
698 rc->rc_iptr = &rc->rc_ibuf[RC_IBUFSIZE];
701 rc->rc_iptr = rc->rc_ibuf;
703 rc_scheduled_event -= eptr - tptr;
705 if (tp->t_state & TS_TTSTOP)
706 rc->rc_flags |= RC_OSUSP;
708 rc->rc_flags &= ~RC_OSUSP;
713 rcopen(struct dev_open_args *ap)
715 cdev_t dev = ap->a_head.a_dev;
718 int unit, nec, error = 0;
720 unit = GET_UNIT(dev);
721 if (unit >= NRC * CD180_NCHAN)
723 if (rc_softc[unit / CD180_NCHAN].rcb_probed != RC_ATTACHED)
725 rc = &rc_chans[unit];
728 nec = rc->rc_rcb->rcb_addr;
730 kprintf("rc%d/%d: rcopen: dev %x\n", rc->rc_rcb->rcb_unit, unit, dev);
735 while (rc->rc_flags & RC_DTR_OFF) {
736 error = tsleep(&(rc->rc_dtrwait), PCATCH, "rcdtr", 0);
740 if (tp->t_state & TS_ISOPEN) {
742 if (!(rc->rc_flags & RC_ACTOUT)) {
747 if (rc->rc_flags & RC_ACTOUT) {
748 if (ap->a_oflags & O_NONBLOCK) {
752 error = tsleep(&rc->rc_rcb, PCATCH, "rcbi", 0);
758 if (tp->t_state & TS_XCLUDE &&
759 suser_cred(ap->a_cred, 0)) {
764 tp->t_oproc = rc_start;
765 tp->t_param = rc_param;
766 tp->t_stop = rc_stop;
770 tp->t_cflag |= CLOCAL;
772 tp->t_cflag &= ~CLOCAL;
774 error = rc_param(tp, &tp->t_termios);
777 (void) rc_modctl(rc, TIOCM_RTS|TIOCM_DTR, DMSET);
779 if ((rc->rc_msvr & MSVR_CD) || CALLOUT(dev))
780 (*linesw[tp->t_line].l_modem)(tp, 1);
782 if (!(tp->t_state & TS_CARR_ON) && !CALLOUT(dev)
783 && !(tp->t_cflag & CLOCAL) && !(ap->a_oflags & O_NONBLOCK)) {
785 error = tsleep(TSA_CARR_ON(tp), PCATCH, "rcdcd", 0);
791 error = (*linesw[tp->t_line].l_open)(dev, tp);
792 disc_optim(tp, &tp->t_termios, rc);
793 if ((tp->t_state & TS_ISOPEN) && CALLOUT(dev))
794 rc->rc_flags |= RC_ACTOUT;
798 if(rc->rc_dcdwaits == 0 && !(tp->t_state & TS_ISOPEN))
805 rcclose(struct dev_close_args *ap)
807 cdev_t dev = ap->a_head.a_dev;
810 int unit = GET_UNIT(dev);
812 if (unit >= NRC * CD180_NCHAN)
814 rc = &rc_chans[unit];
817 kprintf("rc%d/%d: rcclose dev %x\n", rc->rc_rcb->rcb_unit, unit, dev);
820 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
821 disc_optim(tp, &tp->t_termios, rc);
822 rc_stop(tp, FREAD | FWRITE);
829 static void rc_hardclose(rc)
832 int nec = rc->rc_rcb->rcb_addr;
833 struct tty *tp = rc->rc_tp;
836 rcout(CD180_CAR, rc->rc_chan);
838 /* Disable rx/tx intrs */
839 rcout(CD180_IER, rc->rc_ier = 0);
840 if ( (tp->t_cflag & HUPCL)
841 || (!(rc->rc_flags & RC_ACTOUT)
842 && !(rc->rc_msvr & MSVR_CD)
843 && !(tp->t_cflag & CLOCAL))
844 || !(tp->t_state & TS_ISOPEN)
846 CCRCMD(rc->rc_rcb->rcb_unit, rc->rc_chan, CCR_ResetChan);
847 WAITFORCCR(rc->rc_rcb->rcb_unit, rc->rc_chan);
848 (void) rc_modctl(rc, TIOCM_RTS, DMSET);
849 if (rc->rc_dtrwait) {
850 callout_reset(&rc->rc_dtr_ch, rc->rc_dtrwait,
852 rc->rc_flags |= RC_DTR_OFF;
855 rc->rc_flags &= ~RC_ACTOUT;
856 wakeup((caddr_t) &rc->rc_rcb); /* wake bi */
857 wakeup(TSA_CARR_ON(tp));
861 /* Reset the bastard */
862 static void rc_hwreset(unit, nec, chipid)
866 CCRCMD(unit, -1, CCR_HWRESET); /* Hardware reset */
868 WAITFORCCR(unit, -1);
870 rcout(RC_CTOUT, 0); /* Clear timeout */
871 rcout(CD180_GIVR, chipid);
872 rcout(CD180_GICR, 0);
874 /* Set Prescaler Registers (1 msec) */
875 rcout(CD180_PPRL, ((RC_OSCFREQ + 999) / 1000) & 0xFF);
876 rcout(CD180_PPRH, ((RC_OSCFREQ + 999) / 1000) >> 8);
878 /* Initialize Priority Interrupt Level Registers */
879 rcout(CD180_PILR1, RC_PILR_MODEM);
880 rcout(CD180_PILR2, RC_PILR_TX);
881 rcout(CD180_PILR3, RC_PILR_RX);
887 /* Set channel parameters */
888 static int rc_param(tp, ts)
892 struct rc_chans *rc = &rc_chans[GET_UNIT(tp->t_dev)];
893 int nec = rc->rc_rcb->rcb_addr;
894 int idivs, odivs, val, cflag, iflag, lflag, inpflow;
896 if ( ts->c_ospeed < 0 || ts->c_ospeed > 76800
897 || ts->c_ispeed < 0 || ts->c_ispeed > 76800
900 if (ts->c_ispeed == 0)
901 ts->c_ispeed = ts->c_ospeed;
902 odivs = RC_BRD(ts->c_ospeed);
903 idivs = RC_BRD(ts->c_ispeed);
908 rcout(CD180_CAR, rc->rc_chan);
910 /* If speed == 0, hangup line */
911 if (ts->c_ospeed == 0) {
912 CCRCMD(rc->rc_rcb->rcb_unit, rc->rc_chan, CCR_ResetChan);
913 WAITFORCCR(rc->rc_rcb->rcb_unit, rc->rc_chan);
914 (void) rc_modctl(rc, TIOCM_DTR, DMBIC);
917 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
923 rcout(CD180_RBPRL, idivs & 0xFF);
924 rcout(CD180_RBPRH, idivs >> 8);
927 rcout(CD180_TBPRL, odivs & 0xFF);
928 rcout(CD180_TBPRH, odivs >> 8);
931 /* set timeout value */
932 if (ts->c_ispeed > 0) {
933 int itm = ts->c_ispeed > 2400 ? 5 : 10000 / ts->c_ispeed + 1;
935 if ( !(lflag & ICANON)
936 && ts->c_cc[VMIN] != 0 && ts->c_cc[VTIME] != 0
937 && ts->c_cc[VTIME] * 10 > itm)
938 itm = ts->c_cc[VTIME] * 10;
940 rcout(CD180_RTPR, itm <= 255 ? itm : 255);
943 switch (cflag & CSIZE) {
944 case CS5: val = COR1_5BITS; break;
945 case CS6: val = COR1_6BITS; break;
946 case CS7: val = COR1_7BITS; break;
948 case CS8: val = COR1_8BITS; break;
950 if (cflag & PARENB) {
954 if (!(cflag & INPCK))
960 rcout(CD180_COR1, val);
962 /* Set FIFO threshold */
963 val = ts->c_ospeed <= 4800 ? 1 : CD180_NFIFO / 2;
966 && ( ts->c_cc[VSTOP] != _POSIX_VDISABLE
967 && ( ts->c_cc[VSTART] != _POSIX_VDISABLE
973 val |= COR3_SCDE|COR3_FCT;
975 rcout(CD180_COR3, val);
977 /* Initialize on-chip automatic flow control */
979 rc->rc_flags &= ~(RC_CTSFLOW|RC_SEND_RDY);
980 if (cflag & CCTS_OFLOW) {
981 rc->rc_flags |= RC_CTSFLOW;
984 rc->rc_flags |= RC_SEND_RDY;
985 if (tp->t_state & TS_TTSTOP)
986 rc->rc_flags |= RC_OSUSP;
988 rc->rc_flags &= ~RC_OSUSP;
989 if (cflag & CRTS_IFLOW)
990 rc->rc_flags |= RC_RTSFLOW;
992 rc->rc_flags &= ~RC_RTSFLOW;
995 if (ts->c_cc[VSTART] != _POSIX_VDISABLE)
996 rcout(CD180_SCHR1, ts->c_cc[VSTART]);
997 rcout(CD180_SCHR2, ts->c_cc[VSTOP]);
1003 rcout(CD180_COR2, rc->rc_cor2 = val);
1005 CCRCMD(rc->rc_rcb->rcb_unit, rc->rc_chan,
1006 CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
1008 disc_optim(tp, ts, rc);
1011 val = cflag & CLOCAL ? 0 : MCOR1_CDzd;
1012 if (cflag & CCTS_OFLOW)
1014 rcout(CD180_MCOR1, val);
1016 val = cflag & CLOCAL ? 0 : MCOR2_CDod;
1017 if (cflag & CCTS_OFLOW)
1019 rcout(CD180_MCOR2, val);
1021 /* enable i/o and interrupts */
1022 CCRCMD(rc->rc_rcb->rcb_unit, rc->rc_chan,
1023 CCR_XMTREN | ((cflag & CREAD) ? CCR_RCVREN : CCR_RCVRDIS));
1024 WAITFORCCR(rc->rc_rcb->rcb_unit, rc->rc_chan);
1026 rc->rc_ier = cflag & CLOCAL ? 0 : IER_CD;
1027 if (cflag & CCTS_OFLOW)
1028 rc->rc_ier |= IER_CTS;
1030 rc->rc_ier |= IER_RxData;
1031 if (tp->t_state & TS_BUSY)
1032 rc->rc_ier |= IER_TxRdy;
1033 if (ts->c_ospeed != 0)
1034 rc_modctl(rc, TIOCM_DTR, DMBIS);
1035 if ((cflag & CCTS_OFLOW) && (rc->rc_msvr & MSVR_CTS))
1036 rc->rc_flags |= RC_SEND_RDY;
1037 rcout(CD180_IER, rc->rc_ier);
1042 /* Re-initialize board after bogus interrupts */
1043 static void rc_reinit(rcb)
1044 struct rc_softc *rcb;
1046 struct rc_chans *rc, *rce;
1049 nec = rcb->rcb_addr;
1050 rc_hwreset(rcb->rcb_unit, nec, RC_FAKEID);
1051 rc = &rc_chans[rcb->rcb_unit * CD180_NCHAN];
1052 rce = rc + CD180_NCHAN;
1053 for (; rc < rce; rc++)
1054 (void) rc_param(rc->rc_tp, &rc->rc_tp->t_termios);
1058 rcioctl(struct dev_ioctl_args *ap)
1060 cdev_t dev = ap->a_head.a_dev;
1061 struct rc_chans *rc = &rc_chans[GET_UNIT(dev)];
1063 struct tty *tp = rc->rc_tp;
1065 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
1066 ap->a_fflag, ap->a_cred);
1067 if (error != ENOIOCTL)
1069 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
1070 disc_optim(tp, &tp->t_termios, rc);
1071 if (error != ENOIOCTL)
1075 switch (ap->a_cmd) {
1077 rc->rc_pendcmd = CD180_C_SBRK;
1081 rc->rc_pendcmd = CD180_C_EBRK;
1085 (void) rc_modctl(rc, TIOCM_DTR, DMBIS);
1089 (void) rc_modctl(rc, TIOCM_DTR, DMBIC);
1093 *(int *) ap->a_data = rc_modctl(rc, 0, DMGET);
1097 (void) rc_modctl(rc, *(int *) ap->a_data, DMSET);
1101 (void) rc_modctl(rc, *(int *) ap->a_data, DMBIC);
1105 (void) rc_modctl(rc, *(int *) ap->a_data, DMBIS);
1109 error = suser_cred(ap->a_cred, 0);
1114 rc->rc_dtrwait = *(int *)ap->a_data * hz / 100;
1118 *(int *)ap->a_data = rc->rc_dtrwait * 100 / hz;
1130 /* Modem control routines */
1132 static int rc_modctl(rc, bits, cmd)
1133 struct rc_chans *rc;
1136 int nec = rc->rc_rcb->rcb_addr;
1137 u_char *dtr = &rc->rc_rcb->rcb_dtr, msvr;
1139 rcout(CD180_CAR, rc->rc_chan);
1143 rcout(RC_DTREG, (bits & TIOCM_DTR) ?
1144 ~(*dtr |= 1 << rc->rc_chan) :
1145 ~(*dtr &= ~(1 << rc->rc_chan)));
1146 msvr = rcin(CD180_MSVR);
1147 if (bits & TIOCM_RTS)
1151 if (bits & TIOCM_DTR)
1155 rcout(CD180_MSVR, msvr);
1159 if (bits & TIOCM_DTR)
1160 rcout(RC_DTREG, ~(*dtr |= 1 << rc->rc_chan));
1161 msvr = rcin(CD180_MSVR);
1162 if (bits & TIOCM_RTS)
1164 if (bits & TIOCM_DTR)
1166 rcout(CD180_MSVR, msvr);
1171 msvr = rc->rc_msvr = rcin(CD180_MSVR);
1173 if (msvr & MSVR_RTS)
1175 if (msvr & MSVR_CTS)
1177 if (msvr & MSVR_DSR)
1179 if (msvr & MSVR_DTR)
1183 if (~rcin(RC_RIREG) & (1 << rc->rc_chan))
1188 if (bits & TIOCM_DTR)
1189 rcout(RC_DTREG, ~(*dtr &= ~(1 << rc->rc_chan)));
1190 msvr = rcin(CD180_MSVR);
1191 if (bits & TIOCM_RTS)
1193 if (bits & TIOCM_DTR)
1195 rcout(CD180_MSVR, msvr);
1198 rc->rc_msvr = rcin(CD180_MSVR);
1202 /* Test the board. */
1203 int rc_test(nec, unit)
1209 unsigned int iack, chipid;
1210 unsigned short divs;
1211 static u_char ctest[] = "\377\125\252\045\244\0\377";
1214 kprintf("rc%d: ", unit); kprintf s ; kprintf("\n"); \
1215 crit_exit(); return 1; }
1218 u_char txbuf[CD180_NFIFO]; /* TX buffer */
1219 u_char rxbuf[CD180_NFIFO]; /* RX buffer */
1220 int rxptr; /* RX pointer */
1221 int txptr; /* TX pointer */
1222 } tchans[CD180_NCHAN];
1228 /* First, reset board to inital state */
1229 rc_hwreset(unit, nec, chipid);
1231 divs = RC_BRD(19200);
1233 /* Initialize channels */
1234 for (chan = 0; chan < CD180_NCHAN; chan++) {
1236 /* Select and reset channel */
1237 rcout(CD180_CAR, chan);
1238 CCRCMD(unit, chan, CCR_ResetChan);
1239 WAITFORCCR(unit, chan);
1242 rcout(CD180_RBPRL, divs & 0xFF);
1243 rcout(CD180_RBPRH, divs >> 8);
1244 rcout(CD180_TBPRL, divs & 0xFF);
1245 rcout(CD180_TBPRH, divs >> 8);
1247 /* set timeout value */
1248 rcout(CD180_RTPR, 0);
1250 /* Establish local loopback */
1251 rcout(CD180_COR1, COR1_NOPAR | COR1_8BITS | COR1_1SB);
1252 rcout(CD180_COR2, COR2_LLM);
1253 rcout(CD180_COR3, CD180_NFIFO);
1254 CCRCMD(unit, chan, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
1255 CCRCMD(unit, chan, CCR_RCVREN | CCR_XMTREN);
1256 WAITFORCCR(unit, chan);
1257 rcout(CD180_MSVR, MSVR_RTS);
1259 /* Fill TXBUF with test data */
1260 for (i = 0; i < CD180_NFIFO; i++) {
1261 tchans[chan].txbuf[i] = ctest[i];
1262 tchans[chan].rxbuf[i] = 0;
1264 tchans[chan].txptr = tchans[chan].rxptr = 0;
1266 /* Now, start transmit */
1267 rcout(CD180_IER, IER_TxMpty|IER_RxData);
1269 /* Pseudo-interrupt poll stuff */
1270 for (rcnt = 10000; rcnt-- > 0; rcnt--) {
1271 i = ~(rcin(RC_BSR));
1272 if (i & RC_BSR_TOUT)
1273 ERR(("BSR timeout bit set\n"))
1274 else if (i & RC_BSR_TXINT) {
1275 iack = rcin(RC_PILR_TX);
1276 if (iack != (GIVR_IT_TDI | chipid))
1277 ERR(("Bad TX intr ack (%02x != %02x)\n",
1278 iack, GIVR_IT_TDI | chipid));
1279 chan = (rcin(CD180_GICR) & GICR_CHAN) >> GICR_LSH;
1280 /* If no more data to transmit, disable TX intr */
1281 if (tchans[chan].txptr >= CD180_NFIFO) {
1282 iack = rcin(CD180_IER);
1283 rcout(CD180_IER, iack & ~IER_TxMpty);
1285 for (iack = tchans[chan].txptr;
1286 iack < CD180_NFIFO; iack++)
1288 tchans[chan].txbuf[iack]);
1289 tchans[chan].txptr = iack;
1291 rcout(CD180_EOIR, 0);
1292 } else if (i & RC_BSR_RXINT) {
1295 iack = rcin(RC_PILR_RX);
1296 if (iack != (GIVR_IT_RGDI | chipid) &&
1297 iack != (GIVR_IT_REI | chipid))
1298 ERR(("Bad RX intr ack (%02x != %02x)\n",
1299 iack, GIVR_IT_RGDI | chipid))
1300 chan = (rcin(CD180_GICR) & GICR_CHAN) >> GICR_LSH;
1301 ucnt = rcin(CD180_RDCR) & 0xF;
1302 while (ucnt-- > 0) {
1303 iack = rcin(CD180_RCSR);
1304 if (iack & RCSR_Timeout)
1307 ERR(("Bad char chan %d (RCSR = %02X)\n",
1309 if (tchans[chan].rxptr > CD180_NFIFO)
1310 ERR(("Got extra chars chan %d\n",
1312 tchans[chan].rxbuf[tchans[chan].rxptr++] =
1315 rcout(CD180_EOIR, 0);
1318 for (iack = chan = 0; chan < CD180_NCHAN; chan++)
1319 if (tchans[chan].rxptr >= CD180_NFIFO)
1321 if (iack == CD180_NCHAN)
1324 for (chan = 0; chan < CD180_NCHAN; chan++) {
1325 /* Select and reset channel */
1326 rcout(CD180_CAR, chan);
1327 CCRCMD(unit, chan, CCR_ResetChan);
1331 ERR(("looses characters during local loopback\n"))
1332 /* Now, check data */
1333 for (chan = 0; chan < CD180_NCHAN; chan++)
1334 for (i = 0; i < CD180_NFIFO; i++)
1335 if (ctest[i] != tchans[chan].rxbuf[i])
1336 ERR(("data mismatch chan %d ptr %d (%d != %d)\n",
1337 chan, i, ctest[i], tchans[chan].rxbuf[i]))
1343 static void printrcflags(rc, comment)
1344 struct rc_chans *rc;
1347 u_short f = rc->rc_flags;
1348 int nec = rc->rc_rcb->rcb_addr;
1350 kprintf("rc%d/%d: %s flags: %s%s%s%s%s%s%s%s%s%s%s%s\n",
1351 rc->rc_rcb->rcb_unit, rc->rc_chan, comment,
1352 (f & RC_DTR_OFF)?"DTR_OFF " :"",
1353 (f & RC_ACTOUT) ?"ACTOUT " :"",
1354 (f & RC_RTSFLOW)?"RTSFLOW " :"",
1355 (f & RC_CTSFLOW)?"CTSFLOW " :"",
1356 (f & RC_DORXFER)?"DORXFER " :"",
1357 (f & RC_DOXXFER)?"DOXXFER " :"",
1358 (f & RC_MODCHG) ?"MODCHG " :"",
1359 (f & RC_OSUSP) ?"OSUSP " :"",
1360 (f & RC_OSBUSY) ?"OSBUSY " :"",
1361 (f & RC_WAS_BUFOVFL) ?"BUFOVFL " :"",
1362 (f & RC_WAS_SILOVFL) ?"SILOVFL " :"",
1363 (f & RC_SEND_RDY) ?"SEND_RDY":"");
1365 rcout(CD180_CAR, rc->rc_chan);
1367 kprintf("rc%d/%d: msvr %02x ier %02x ccsr %02x\n",
1368 rc->rc_rcb->rcb_unit, rc->rc_chan,
1373 #endif /* RCDEBUG */
1379 struct rc_chans *rc;
1381 rc = (struct rc_chans *)chan;
1382 rc->rc_flags &= ~RC_DTR_OFF;
1383 wakeup(&rc->rc_dtrwait);
1387 rc_discard_output(rc)
1388 struct rc_chans *rc;
1391 if (rc->rc_flags & RC_DOXXFER) {
1392 rc_scheduled_event -= LOTS_OF_EVENTS;
1393 rc->rc_flags &= ~RC_DOXXFER;
1395 rc->rc_optr = rc->rc_obufend;
1396 rc->rc_tp->t_state &= ~TS_BUSY;
1398 ttwwakeup(rc->rc_tp);
1405 if (rc_scheduled_event != 0) {
1410 callout_reset(&rc_wakeup_ch, 1, rc_wakeup, NULL);
1414 disc_optim(tp, t, rc)
1417 struct rc_chans *rc;
1420 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1421 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1422 && (!(t->c_iflag & PARMRK)
1423 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1424 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1425 && linesw[tp->t_line].l_rint == ttyinput)
1426 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1428 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1429 rc->rc_hotchar = linesw[tp->t_line].l_hotchar;
1433 rc_wait0(nec, unit, chan, line)
1434 int nec, unit, chan, line;
1438 for (rcnt = 50; rcnt && rcin(CD180_CCR); rcnt--)
1441 kprintf("rc%d/%d: channel command timeout, rc.c line: %d\n",