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.19 2006/10/23 21:50:32 dillon 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>
52 #include <machine/ipl.h>
54 #include <bus/isa/i386/isa_device.h>
56 #include <arch/isa/ic/cd180.h>
60 static int rcprobe (struct isa_device *);
61 static int rcattach (struct isa_device *);
63 #define rcin(port) RC_IN (nec, port)
64 #define rcout(port,v) RC_OUT (nec, port, v)
66 #define WAITFORCCR(u,c) rc_wait0(nec, (u), (c), __LINE__)
67 #define CCRCMD(u,c,cmd) WAITFORCCR((u), (c)); rcout(CD180_CCR, (cmd))
69 #define RC_IBUFSIZE 256
70 #define RB_I_HIGH_WATER (TTYHOG - 2 * RC_IBUFSIZE)
71 #define RC_OBUFSIZE 512
72 #define RC_IHIGHWATER (3 * RC_IBUFSIZE / 4)
73 #define INPUT_FLAGS_SHIFT (2 * RC_IBUFSIZE)
74 #define LOTS_OF_EVENTS 64
76 #define RC_FAKEID 0x10
81 #define GET_UNIT(dev) (minor(dev) & 0x3F)
82 #define CALLOUT(dev) (minor(dev) & 0x80)
84 /* For isa routines */
85 struct isa_driver rcdriver = {
86 rcprobe, rcattach, "rc"
89 static d_open_t rcopen;
90 static d_close_t rcclose;
91 static d_ioctl_t rcioctl;
94 static struct dev_ops rc_ops = {
95 { "rc", CDEV_MAJOR, D_TTY | D_KQFILTER },
102 .d_kqfilter = ttykqfilter
105 /* Per-board structure */
106 static struct rc_softc {
107 u_int rcb_probed; /* 1 - probed, 2 - attached */
108 u_int rcb_addr; /* Base I/O addr */
109 u_int rcb_unit; /* unit # */
110 u_char rcb_dtr; /* DTR status */
111 struct rc_chans *rcb_baserc; /* base rc ptr */
114 /* Per-channel structure */
115 static struct rc_chans {
116 struct rc_softc *rc_rcb; /* back ptr */
117 u_short rc_flags; /* Misc. flags */
118 int rc_chan; /* Channel # */
119 u_char rc_ier; /* intr. enable reg */
120 u_char rc_msvr; /* modem sig. status */
121 u_char rc_cor2; /* options reg */
122 u_char rc_pendcmd; /* special cmd pending */
123 u_int rc_dtrwait; /* dtr timeout */
124 u_int rc_dcdwaits; /* how many waits DCD in open */
125 u_char rc_hotchar; /* end packed optimize */
126 struct tty *rc_tp; /* tty struct */
127 u_char *rc_iptr; /* Chars input buffer */
128 u_char *rc_hiwat; /* hi-water mark */
129 u_char *rc_bufend; /* end of buffer */
130 u_char *rc_optr; /* ptr in output buf */
131 u_char *rc_obufend; /* end of output buf */
132 struct callout rc_dtr_ch;
133 u_char rc_ibuf[4 * RC_IBUFSIZE]; /* input buffer */
134 u_char rc_obuf[RC_OBUFSIZE]; /* output buffer */
135 } rc_chans[NRC * CD180_NCHAN];
137 static int rc_scheduled_event = 0;
138 static struct callout rc_wakeup_ch;
141 static struct tty rc_tty[NRC * CD180_NCHAN];
142 static const int nrc_tty = NRC * CD180_NCHAN;
145 #define RC_DTR_OFF 0x0001 /* DTR wait, for close/open */
146 #define RC_ACTOUT 0x0002 /* Dial-out port active */
147 #define RC_RTSFLOW 0x0004 /* RTS flow ctl enabled */
148 #define RC_CTSFLOW 0x0008 /* CTS flow ctl enabled */
149 #define RC_DORXFER 0x0010 /* RXFER event planned */
150 #define RC_DOXXFER 0x0020 /* XXFER event planned */
151 #define RC_MODCHG 0x0040 /* Modem status changed */
152 #define RC_OSUSP 0x0080 /* Output suspended */
153 #define RC_OSBUSY 0x0100 /* start() routine in progress */
154 #define RC_WAS_BUFOVFL 0x0200 /* low-level buffer ovferflow */
155 #define RC_WAS_SILOVFL 0x0400 /* silo buffer overflow */
156 #define RC_SEND_RDY 0x0800 /* ready to send */
158 /* Table for translation of RCSR status bits to internal form */
159 static int rc_rcsrt[16] = {
161 TTY_FE|TTY_OE, TTY_PE, TTY_PE|TTY_OE,
162 TTY_PE|TTY_FE, TTY_PE|TTY_FE|TTY_OE, TTY_BI,
163 TTY_BI|TTY_OE, TTY_BI|TTY_FE, TTY_BI|TTY_FE|TTY_OE,
164 TTY_BI|TTY_PE, TTY_BI|TTY_PE|TTY_OE, TTY_BI|TTY_PE|TTY_FE,
165 TTY_BI|TTY_PE|TTY_FE|TTY_OE
168 /* Static prototypes */
169 static inthand2_t rcintr;
170 static void rc_hwreset (int, int, unsigned int);
171 static int rc_test (int, int);
172 static void rc_discard_output (struct rc_chans *);
173 static void rc_hardclose (struct rc_chans *);
174 static int rc_modctl (struct rc_chans *, int, int);
175 static void rc_start (struct tty *);
176 static void rc_stop (struct tty *, int rw);
177 static int rc_param (struct tty *, struct termios *);
178 static inthand2_t rcpoll;
179 static void rc_reinit (struct rc_softc *);
181 static void printrcflags();
183 static timeout_t rc_dtrwakeup;
184 static timeout_t rc_wakeup;
185 static void disc_optim (struct tty *tp, struct termios *t, struct rc_chans *);
186 static void rc_wait0 (int nec, int unit, int chan, int line);
188 /**********************************************/
190 /* Quick device probing */
193 struct isa_device *dvp;
195 int irq = ffs(dvp->id_irq) - 1;
196 int nec = dvp->id_iobase;
198 if (dvp->id_unit > NRC)
200 if (!RC_VALIDADDR(nec)) {
201 printf("rc%d: illegal base address %x\n", dvp->id_unit, nec);
204 if (!RC_VALIDIRQ(irq)) {
205 printf("rc%d: illegal IRQ value %d\n", dvp->id_unit, irq);
208 rcout(CD180_PPRL, 0x22); /* Random values to Prescale reg. */
209 rcout(CD180_PPRH, 0x11);
210 if (rcin(CD180_PPRL) != 0x22 || rcin(CD180_PPRH) != 0x11)
212 /* Now, test the board more thoroughly, with diagnostic */
213 if (rc_test(nec, dvp->id_unit))
215 rc_softc[dvp->id_unit].rcb_probed = RC_PROBED;
222 struct isa_device *dvp;
224 int chan, nec = dvp->id_iobase;
225 struct rc_softc *rcb = &rc_softc[dvp->id_unit];
226 struct rc_chans *rc = &rc_chans[dvp->id_unit * CD180_NCHAN];
227 static int rc_started = 0;
230 dvp->id_intr = rcintr;
232 /* Thorooughly test the device */
233 if (rcb->rcb_probed != RC_PROBED)
237 rcb->rcb_baserc = rc;
238 rcb->rcb_unit = dvp->id_unit;
239 /*rcb->rcb_chipid = 0x10 + dvp->id_unit;*/
240 printf("rc%d: %d chans, firmware rev. %c\n", rcb->rcb_unit,
241 CD180_NCHAN, (rcin(CD180_GFRCR) & 0xF) + 'A');
243 for (chan = 0; chan < CD180_NCHAN; chan++, rc++) {
244 callout_init(&rc->rc_dtr_ch);
247 rc->rc_iptr = rc->rc_ibuf;
248 rc->rc_bufend = &rc->rc_ibuf[RC_IBUFSIZE];
249 rc->rc_hiwat = &rc->rc_ibuf[RC_IHIGHWATER];
250 rc->rc_flags = rc->rc_ier = rc->rc_msvr = 0;
251 rc->rc_cor2 = rc->rc_pendcmd = 0;
252 rc->rc_optr = rc->rc_obufend = rc->rc_obuf;
253 rc->rc_dtrwait = 3 * hz;
256 tp = rc->rc_tp = &rc_tty[chan + (dvp->id_unit * CD180_NCHAN)];
258 tp->t_lflag = tp->t_iflag = tp->t_oflag = 0;
259 tp->t_cflag = TTYDEF_CFLAG;
260 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
262 rcb->rcb_probed = RC_ATTACHED;
264 dev_ops_add(&rc_ops, -1, rcb->rcb_unit);
265 register_swi(SWI_TTY, rcpoll, NULL, "rcpoll", NULL);
266 callout_init(&rc_wakeup_ch);
273 /* RC interrupt handling */
275 rcintr(void *arg, void *frame)
278 struct rc_softc *rcb = &rc_softc[unit];
281 u_char val, iack, bsr, ucnt, *optr;
282 int good_data, t_state;
284 if (rcb->rcb_probed != RC_ATTACHED) {
285 printf("rc%d: bogus interrupt\n", unit);
290 bsr = ~(rcin(RC_BSR));
292 if (!(bsr & (RC_BSR_TOUT|RC_BSR_RXINT|RC_BSR_TXINT|RC_BSR_MOINT))) {
293 printf("rc%d: extra interrupt\n", unit);
294 rcout(CD180_EOIR, 0);
298 while (bsr & (RC_BSR_TOUT|RC_BSR_RXINT|RC_BSR_TXINT|RC_BSR_MOINT)) {
299 #ifdef RCDEBUG_DETAILED
300 printf("rc%d: intr (%02x) %s%s%s%s\n", unit, bsr,
301 (bsr & RC_BSR_TOUT)?"TOUT ":"",
302 (bsr & RC_BSR_RXINT)?"RXINT ":"",
303 (bsr & RC_BSR_TXINT)?"TXINT ":"",
304 (bsr & RC_BSR_MOINT)?"MOINT":"");
306 if (bsr & RC_BSR_TOUT) {
307 printf("rc%d: hardware failure, reset board\n", unit);
312 if (bsr & RC_BSR_RXINT) {
313 iack = rcin(RC_PILR_RX);
314 good_data = (iack == (GIVR_IT_RGDI | RC_FAKEID));
315 if (!good_data && iack != (GIVR_IT_REI | RC_FAKEID)) {
316 printf("rc%d: fake rxint: %02x\n", unit, iack);
319 rc = rcb->rcb_baserc + ((rcin(CD180_GICR) & GICR_CHAN) >> GICR_LSH);
320 t_state = rc->rc_tp->t_state;
321 /* Do RTS flow control stuff */
322 if ( (rc->rc_flags & RC_RTSFLOW)
323 || !(t_state & TS_ISOPEN)
325 if ( ( !(t_state & TS_ISOPEN)
326 || (t_state & TS_TBLOCK)
328 && (rc->rc_msvr & MSVR_RTS)
331 rc->rc_msvr &= ~MSVR_RTS);
332 else if (!(rc->rc_msvr & MSVR_RTS))
334 rc->rc_msvr |= MSVR_RTS);
336 ucnt = rcin(CD180_RDCR) & 0xF;
339 if (t_state & TS_ISOPEN) {
340 /* check for input buffer overflow */
341 if ((rc->rc_iptr + ucnt) >= rc->rc_bufend) {
343 ucnt = rc->rc_bufend - rc->rc_iptr;
345 if (!(rc->rc_flags & RC_WAS_BUFOVFL)) {
346 rc->rc_flags |= RC_WAS_BUFOVFL;
347 rc_scheduled_event++;
351 /* check foor good data */
354 val = rcin(CD180_RDR);
356 optr[INPUT_FLAGS_SHIFT] = 0;
358 rc_scheduled_event++;
359 if (val != 0 && val == rc->rc_hotchar)
363 /* Store also status data */
365 iack = rcin(CD180_RCSR);
366 if (iack & RCSR_Timeout)
368 if ( (iack & RCSR_OE)
369 && !(rc->rc_flags & RC_WAS_SILOVFL)) {
370 rc->rc_flags |= RC_WAS_SILOVFL;
371 rc_scheduled_event++;
373 val = rcin(CD180_RDR);
375 Don't store PE if IGNPAR and BREAK if IGNBRK,
376 this hack allows "raw" tty optimization
377 works even if IGN* is set.
379 if ( !(iack & (RCSR_PE|RCSR_FE|RCSR_Break))
380 || ((!(iack & (RCSR_PE|RCSR_FE))
381 || !(rc->rc_tp->t_iflag & IGNPAR))
382 && (!(iack & RCSR_Break)
383 || !(rc->rc_tp->t_iflag & IGNBRK)))) {
384 if ( (iack & (RCSR_PE|RCSR_FE))
385 && (t_state & TS_CAN_BYPASS_L_RINT)
388 && (rc->rc_tp->t_iflag & INPCK))))
390 else if (val != 0 && val == rc->rc_hotchar)
393 optr[INPUT_FLAGS_SHIFT] = iack;
395 rc_scheduled_event++;
400 rc->rc_flags |= RC_DORXFER;
403 /* Clear FIFO if necessary */
404 while (resid-- > 0) {
406 iack = rcin(CD180_RCSR);
409 if (iack & RCSR_Timeout)
411 (void) rcin(CD180_RDR);
415 if (bsr & RC_BSR_MOINT) {
416 iack = rcin(RC_PILR_MODEM);
417 if (iack != (GIVR_IT_MSCI | RC_FAKEID)) {
418 printf("rc%d: fake moint: %02x\n", unit, iack);
421 rc = rcb->rcb_baserc + ((rcin(CD180_GICR) & GICR_CHAN) >> GICR_LSH);
422 iack = rcin(CD180_MCR);
423 rc->rc_msvr = rcin(CD180_MSVR);
426 printrcflags(rc, "moint");
428 if (rc->rc_flags & RC_CTSFLOW) {
429 if (rc->rc_msvr & MSVR_CTS)
430 rc->rc_flags |= RC_SEND_RDY;
432 rc->rc_flags &= ~RC_SEND_RDY;
434 rc->rc_flags |= RC_SEND_RDY;
435 if ((iack & MCR_CDchg) && !(rc->rc_flags & RC_MODCHG)) {
436 rc_scheduled_event += LOTS_OF_EVENTS;
437 rc->rc_flags |= RC_MODCHG;
442 if (bsr & RC_BSR_TXINT) {
443 iack = rcin(RC_PILR_TX);
444 if (iack != (GIVR_IT_TDI | RC_FAKEID)) {
445 printf("rc%d: fake txint: %02x\n", unit, iack);
448 rc = rcb->rcb_baserc + ((rcin(CD180_GICR) & GICR_CHAN) >> GICR_LSH);
449 if ( (rc->rc_flags & RC_OSUSP)
450 || !(rc->rc_flags & RC_SEND_RDY)
453 /* Handle breaks and other stuff */
454 if (rc->rc_pendcmd) {
455 rcout(CD180_COR2, rc->rc_cor2 |= COR2_ETC);
456 rcout(CD180_TDR, CD180_C_ESC);
457 rcout(CD180_TDR, rc->rc_pendcmd);
458 rcout(CD180_COR2, rc->rc_cor2 &= ~COR2_ETC);
463 resid = rc->rc_obufend - optr;
464 if (resid > CD180_NFIFO)
467 rcout(CD180_TDR, *optr++);
470 /* output completed? */
471 if (optr >= rc->rc_obufend) {
472 rcout(CD180_IER, rc->rc_ier &= ~IER_TxRdy);
474 printf("rc%d/%d: output completed\n", unit, rc->rc_chan);
476 if (!(rc->rc_flags & RC_DOXXFER)) {
477 rc_scheduled_event += LOTS_OF_EVENTS;
478 rc->rc_flags |= RC_DOXXFER;
484 rcout(CD180_EOIR, 0); /* end of interrupt */
486 bsr = ~(rcin(RC_BSR));
490 /* Feed characters to output buffer */
491 static void rc_start(tp)
494 struct rc_chans *rc = &rc_chans[GET_UNIT(tp->t_dev)];
495 int nec = rc->rc_rcb->rcb_addr;
497 if (rc->rc_flags & RC_OSBUSY)
500 rc->rc_flags |= RC_OSBUSY;
502 if (tp->t_state & TS_TTSTOP)
503 rc->rc_flags |= RC_OSUSP;
505 rc->rc_flags &= ~RC_OSUSP;
506 /* Do RTS flow control stuff */
507 if ( (rc->rc_flags & RC_RTSFLOW)
508 && (tp->t_state & TS_TBLOCK)
509 && (rc->rc_msvr & MSVR_RTS)
511 rcout(CD180_CAR, rc->rc_chan);
512 rcout(CD180_MSVR, rc->rc_msvr &= ~MSVR_RTS);
513 } else if (!(rc->rc_msvr & MSVR_RTS)) {
514 rcout(CD180_CAR, rc->rc_chan);
515 rcout(CD180_MSVR, rc->rc_msvr |= MSVR_RTS);
518 if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP))
521 printrcflags(rc, "rcstart");
525 printf("rcstart: outq = %d obuf = %d\n",
526 tp->t_outq.c_cc, rc->rc_obufend - rc->rc_optr);
528 if (tp->t_state & TS_BUSY)
529 goto out; /* output still in progress ... */
531 if (tp->t_outq.c_cc > 0) {
534 tp->t_state |= TS_BUSY;
535 ocnt = q_to_b(&tp->t_outq, rc->rc_obuf, sizeof rc->rc_obuf);
537 rc->rc_optr = rc->rc_obuf;
538 rc->rc_obufend = rc->rc_optr + ocnt;
540 if (!(rc->rc_ier & IER_TxRdy)) {
542 printf("rc%d/%d: rcstart enable txint\n", rc->rc_rcb->rcb_unit, rc->rc_chan);
544 rcout(CD180_CAR, rc->rc_chan);
545 rcout(CD180_IER, rc->rc_ier |= IER_TxRdy);
549 rc->rc_flags &= ~RC_OSBUSY;
553 /* Handle delayed events. */
555 rcpoll(void *dummy, void *frame)
558 struct rc_softc *rcb;
561 int chan, icnt, nec, unit;
563 if (rc_scheduled_event == 0)
566 for (unit = 0; unit < NRC; unit++) {
567 rcb = &rc_softc[unit];
568 rc = rcb->rcb_baserc;
569 nec = rc->rc_rcb->rcb_addr;
570 for (chan = 0; chan < CD180_NCHAN; rc++, chan++) {
573 if (rc->rc_flags & (RC_DORXFER|RC_DOXXFER|RC_MODCHG|
574 RC_WAS_BUFOVFL|RC_WAS_SILOVFL))
575 printrcflags(rc, "rcevent");
577 if (rc->rc_flags & RC_WAS_BUFOVFL) {
579 rc->rc_flags &= ~RC_WAS_BUFOVFL;
580 rc_scheduled_event--;
582 printf("rc%d/%d: interrupt-level buffer overflow\n",
585 if (rc->rc_flags & RC_WAS_SILOVFL) {
587 rc->rc_flags &= ~RC_WAS_SILOVFL;
588 rc_scheduled_event--;
590 printf("rc%d/%d: silo overflow\n",
593 if (rc->rc_flags & RC_MODCHG) {
595 rc->rc_flags &= ~RC_MODCHG;
596 rc_scheduled_event -= LOTS_OF_EVENTS;
598 (*linesw[tp->t_line].l_modem)(tp, !!(rc->rc_msvr & MSVR_CD));
600 if (rc->rc_flags & RC_DORXFER) {
602 rc->rc_flags &= ~RC_DORXFER;
604 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE])
605 tptr = &rc->rc_ibuf[RC_IBUFSIZE];
610 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) {
611 rc->rc_iptr = rc->rc_ibuf;
612 rc->rc_bufend = &rc->rc_ibuf[RC_IBUFSIZE];
613 rc->rc_hiwat = &rc->rc_ibuf[RC_IHIGHWATER];
615 rc->rc_iptr = &rc->rc_ibuf[RC_IBUFSIZE];
616 rc->rc_bufend = &rc->rc_ibuf[2 * RC_IBUFSIZE];
618 &rc->rc_ibuf[RC_IBUFSIZE + RC_IHIGHWATER];
620 if ( (rc->rc_flags & RC_RTSFLOW)
621 && (tp->t_state & TS_ISOPEN)
622 && !(tp->t_state & TS_TBLOCK)
623 && !(rc->rc_msvr & MSVR_RTS)
625 rcout(CD180_CAR, chan);
627 rc->rc_msvr |= MSVR_RTS);
629 rc_scheduled_event -= icnt;
633 if (icnt <= 0 || !(tp->t_state & TS_ISOPEN))
636 if ( (tp->t_state & TS_CAN_BYPASS_L_RINT)
637 && !(tp->t_state & TS_LOCAL)) {
638 if ((tp->t_rawq.c_cc + icnt) >= RB_I_HIGH_WATER
639 && ((rc->rc_flags & RC_RTSFLOW) || (tp->t_iflag & IXOFF))
640 && !(tp->t_state & TS_TBLOCK))
645 if (b_to_q(tptr, icnt, &tp->t_rawq))
646 printf("rc%d/%d: tty-level buffer overflow\n",
649 if ((tp->t_state & TS_TTSTOP) && ((tp->t_iflag & IXANY)
650 || (tp->t_cc[VSTART] == tp->t_cc[VSTOP]))) {
651 tp->t_state &= ~TS_TTSTOP;
652 tp->t_lflag &= ~FLUSHO;
656 for (; tptr < eptr; tptr++)
657 (*linesw[tp->t_line].l_rint)
659 rc_rcsrt[tptr[INPUT_FLAGS_SHIFT] & 0xF], tp);
663 if (rc->rc_flags & RC_DOXXFER) {
665 rc_scheduled_event -= LOTS_OF_EVENTS;
666 rc->rc_flags &= ~RC_DOXXFER;
667 rc->rc_tp->t_state &= ~TS_BUSY;
669 (*linesw[tp->t_line].l_start)(tp);
672 if (rc_scheduled_event == 0)
675 if (rc_scheduled_event >= LOTS_OF_EVENTS)
684 struct rc_chans *rc = &rc_chans[GET_UNIT(tp->t_dev)];
688 printf("rc%d/%d: rc_stop %s%s\n", rc->rc_rcb->rcb_unit, rc->rc_chan,
689 (rw & FWRITE)?"FWRITE ":"", (rw & FREAD)?"FREAD":"");
692 rc_discard_output(rc);
695 rc->rc_flags &= ~RC_DORXFER;
697 if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) {
698 tptr = &rc->rc_ibuf[RC_IBUFSIZE];
699 rc->rc_iptr = &rc->rc_ibuf[RC_IBUFSIZE];
702 rc->rc_iptr = rc->rc_ibuf;
704 rc_scheduled_event -= eptr - tptr;
706 if (tp->t_state & TS_TTSTOP)
707 rc->rc_flags |= RC_OSUSP;
709 rc->rc_flags &= ~RC_OSUSP;
714 rcopen(struct dev_open_args *ap)
716 cdev_t dev = ap->a_head.a_dev;
719 int unit, nec, error = 0;
721 unit = GET_UNIT(dev);
722 if (unit >= NRC * CD180_NCHAN)
724 if (rc_softc[unit / CD180_NCHAN].rcb_probed != RC_ATTACHED)
726 rc = &rc_chans[unit];
729 nec = rc->rc_rcb->rcb_addr;
731 printf("rc%d/%d: rcopen: dev %x\n", rc->rc_rcb->rcb_unit, unit, dev);
736 while (rc->rc_flags & RC_DTR_OFF) {
737 error = tsleep(&(rc->rc_dtrwait), PCATCH, "rcdtr", 0);
741 if (tp->t_state & TS_ISOPEN) {
743 if (!(rc->rc_flags & RC_ACTOUT)) {
748 if (rc->rc_flags & RC_ACTOUT) {
749 if (ap->a_oflags & O_NONBLOCK) {
753 error = tsleep(&rc->rc_rcb, PCATCH, "rcbi", 0);
759 if (tp->t_state & TS_XCLUDE &&
760 suser_cred(ap->a_cred, 0)) {
765 tp->t_oproc = rc_start;
766 tp->t_param = rc_param;
767 tp->t_stop = rc_stop;
771 tp->t_cflag |= CLOCAL;
773 tp->t_cflag &= ~CLOCAL;
775 error = rc_param(tp, &tp->t_termios);
778 (void) rc_modctl(rc, TIOCM_RTS|TIOCM_DTR, DMSET);
780 if ((rc->rc_msvr & MSVR_CD) || CALLOUT(dev))
781 (*linesw[tp->t_line].l_modem)(tp, 1);
783 if (!(tp->t_state & TS_CARR_ON) && !CALLOUT(dev)
784 && !(tp->t_cflag & CLOCAL) && !(ap->a_oflags & O_NONBLOCK)) {
786 error = tsleep(TSA_CARR_ON(tp), PCATCH, "rcdcd", 0);
792 error = (*linesw[tp->t_line].l_open)(dev, tp);
793 disc_optim(tp, &tp->t_termios, rc);
794 if ((tp->t_state & TS_ISOPEN) && CALLOUT(dev))
795 rc->rc_flags |= RC_ACTOUT;
799 if(rc->rc_dcdwaits == 0 && !(tp->t_state & TS_ISOPEN))
806 rcclose(struct dev_close_args *ap)
808 cdev_t dev = ap->a_head.a_dev;
811 int unit = GET_UNIT(dev);
813 if (unit >= NRC * CD180_NCHAN)
815 rc = &rc_chans[unit];
818 printf("rc%d/%d: rcclose dev %x\n", rc->rc_rcb->rcb_unit, unit, dev);
821 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
822 disc_optim(tp, &tp->t_termios, rc);
823 rc_stop(tp, FREAD | FWRITE);
830 static void rc_hardclose(rc)
833 int nec = rc->rc_rcb->rcb_addr;
834 struct tty *tp = rc->rc_tp;
837 rcout(CD180_CAR, rc->rc_chan);
839 /* Disable rx/tx intrs */
840 rcout(CD180_IER, rc->rc_ier = 0);
841 if ( (tp->t_cflag & HUPCL)
842 || (!(rc->rc_flags & RC_ACTOUT)
843 && !(rc->rc_msvr & MSVR_CD)
844 && !(tp->t_cflag & CLOCAL))
845 || !(tp->t_state & TS_ISOPEN)
847 CCRCMD(rc->rc_rcb->rcb_unit, rc->rc_chan, CCR_ResetChan);
848 WAITFORCCR(rc->rc_rcb->rcb_unit, rc->rc_chan);
849 (void) rc_modctl(rc, TIOCM_RTS, DMSET);
850 if (rc->rc_dtrwait) {
851 callout_reset(&rc->rc_dtr_ch, rc->rc_dtrwait,
853 rc->rc_flags |= RC_DTR_OFF;
856 rc->rc_flags &= ~RC_ACTOUT;
857 wakeup((caddr_t) &rc->rc_rcb); /* wake bi */
858 wakeup(TSA_CARR_ON(tp));
862 /* Reset the bastard */
863 static void rc_hwreset(unit, nec, chipid)
867 CCRCMD(unit, -1, CCR_HWRESET); /* Hardware reset */
869 WAITFORCCR(unit, -1);
871 rcout(RC_CTOUT, 0); /* Clear timeout */
872 rcout(CD180_GIVR, chipid);
873 rcout(CD180_GICR, 0);
875 /* Set Prescaler Registers (1 msec) */
876 rcout(CD180_PPRL, ((RC_OSCFREQ + 999) / 1000) & 0xFF);
877 rcout(CD180_PPRH, ((RC_OSCFREQ + 999) / 1000) >> 8);
879 /* Initialize Priority Interrupt Level Registers */
880 rcout(CD180_PILR1, RC_PILR_MODEM);
881 rcout(CD180_PILR2, RC_PILR_TX);
882 rcout(CD180_PILR3, RC_PILR_RX);
888 /* Set channel parameters */
889 static int rc_param(tp, ts)
893 struct rc_chans *rc = &rc_chans[GET_UNIT(tp->t_dev)];
894 int nec = rc->rc_rcb->rcb_addr;
895 int idivs, odivs, val, cflag, iflag, lflag, inpflow;
897 if ( ts->c_ospeed < 0 || ts->c_ospeed > 76800
898 || ts->c_ispeed < 0 || ts->c_ispeed > 76800
901 if (ts->c_ispeed == 0)
902 ts->c_ispeed = ts->c_ospeed;
903 odivs = RC_BRD(ts->c_ospeed);
904 idivs = RC_BRD(ts->c_ispeed);
909 rcout(CD180_CAR, rc->rc_chan);
911 /* If speed == 0, hangup line */
912 if (ts->c_ospeed == 0) {
913 CCRCMD(rc->rc_rcb->rcb_unit, rc->rc_chan, CCR_ResetChan);
914 WAITFORCCR(rc->rc_rcb->rcb_unit, rc->rc_chan);
915 (void) rc_modctl(rc, TIOCM_DTR, DMBIC);
918 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
924 rcout(CD180_RBPRL, idivs & 0xFF);
925 rcout(CD180_RBPRH, idivs >> 8);
928 rcout(CD180_TBPRL, odivs & 0xFF);
929 rcout(CD180_TBPRH, odivs >> 8);
932 /* set timeout value */
933 if (ts->c_ispeed > 0) {
934 int itm = ts->c_ispeed > 2400 ? 5 : 10000 / ts->c_ispeed + 1;
936 if ( !(lflag & ICANON)
937 && ts->c_cc[VMIN] != 0 && ts->c_cc[VTIME] != 0
938 && ts->c_cc[VTIME] * 10 > itm)
939 itm = ts->c_cc[VTIME] * 10;
941 rcout(CD180_RTPR, itm <= 255 ? itm : 255);
944 switch (cflag & CSIZE) {
945 case CS5: val = COR1_5BITS; break;
946 case CS6: val = COR1_6BITS; break;
947 case CS7: val = COR1_7BITS; break;
949 case CS8: val = COR1_8BITS; break;
951 if (cflag & PARENB) {
955 if (!(cflag & INPCK))
961 rcout(CD180_COR1, val);
963 /* Set FIFO threshold */
964 val = ts->c_ospeed <= 4800 ? 1 : CD180_NFIFO / 2;
967 && ( ts->c_cc[VSTOP] != _POSIX_VDISABLE
968 && ( ts->c_cc[VSTART] != _POSIX_VDISABLE
974 val |= COR3_SCDE|COR3_FCT;
976 rcout(CD180_COR3, val);
978 /* Initialize on-chip automatic flow control */
980 rc->rc_flags &= ~(RC_CTSFLOW|RC_SEND_RDY);
981 if (cflag & CCTS_OFLOW) {
982 rc->rc_flags |= RC_CTSFLOW;
985 rc->rc_flags |= RC_SEND_RDY;
986 if (tp->t_state & TS_TTSTOP)
987 rc->rc_flags |= RC_OSUSP;
989 rc->rc_flags &= ~RC_OSUSP;
990 if (cflag & CRTS_IFLOW)
991 rc->rc_flags |= RC_RTSFLOW;
993 rc->rc_flags &= ~RC_RTSFLOW;
996 if (ts->c_cc[VSTART] != _POSIX_VDISABLE)
997 rcout(CD180_SCHR1, ts->c_cc[VSTART]);
998 rcout(CD180_SCHR2, ts->c_cc[VSTOP]);
1004 rcout(CD180_COR2, rc->rc_cor2 = val);
1006 CCRCMD(rc->rc_rcb->rcb_unit, rc->rc_chan,
1007 CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
1009 disc_optim(tp, ts, rc);
1012 val = cflag & CLOCAL ? 0 : MCOR1_CDzd;
1013 if (cflag & CCTS_OFLOW)
1015 rcout(CD180_MCOR1, val);
1017 val = cflag & CLOCAL ? 0 : MCOR2_CDod;
1018 if (cflag & CCTS_OFLOW)
1020 rcout(CD180_MCOR2, val);
1022 /* enable i/o and interrupts */
1023 CCRCMD(rc->rc_rcb->rcb_unit, rc->rc_chan,
1024 CCR_XMTREN | ((cflag & CREAD) ? CCR_RCVREN : CCR_RCVRDIS));
1025 WAITFORCCR(rc->rc_rcb->rcb_unit, rc->rc_chan);
1027 rc->rc_ier = cflag & CLOCAL ? 0 : IER_CD;
1028 if (cflag & CCTS_OFLOW)
1029 rc->rc_ier |= IER_CTS;
1031 rc->rc_ier |= IER_RxData;
1032 if (tp->t_state & TS_BUSY)
1033 rc->rc_ier |= IER_TxRdy;
1034 if (ts->c_ospeed != 0)
1035 rc_modctl(rc, TIOCM_DTR, DMBIS);
1036 if ((cflag & CCTS_OFLOW) && (rc->rc_msvr & MSVR_CTS))
1037 rc->rc_flags |= RC_SEND_RDY;
1038 rcout(CD180_IER, rc->rc_ier);
1043 /* Re-initialize board after bogus interrupts */
1044 static void rc_reinit(rcb)
1045 struct rc_softc *rcb;
1047 struct rc_chans *rc, *rce;
1050 nec = rcb->rcb_addr;
1051 rc_hwreset(rcb->rcb_unit, nec, RC_FAKEID);
1052 rc = &rc_chans[rcb->rcb_unit * CD180_NCHAN];
1053 rce = rc + CD180_NCHAN;
1054 for (; rc < rce; rc++)
1055 (void) rc_param(rc->rc_tp, &rc->rc_tp->t_termios);
1059 rcioctl(struct dev_ioctl_args *ap)
1061 cdev_t dev = ap->a_head.a_dev;
1062 struct rc_chans *rc = &rc_chans[GET_UNIT(dev)];
1064 struct tty *tp = rc->rc_tp;
1066 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
1067 ap->a_fflag, ap->a_cred);
1068 if (error != ENOIOCTL)
1070 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
1071 disc_optim(tp, &tp->t_termios, rc);
1072 if (error != ENOIOCTL)
1076 switch (ap->a_cmd) {
1078 rc->rc_pendcmd = CD180_C_SBRK;
1082 rc->rc_pendcmd = CD180_C_EBRK;
1086 (void) rc_modctl(rc, TIOCM_DTR, DMBIS);
1090 (void) rc_modctl(rc, TIOCM_DTR, DMBIC);
1094 *(int *) ap->a_data = rc_modctl(rc, 0, DMGET);
1098 (void) rc_modctl(rc, *(int *) ap->a_data, DMSET);
1102 (void) rc_modctl(rc, *(int *) ap->a_data, DMBIC);
1106 (void) rc_modctl(rc, *(int *) ap->a_data, DMBIS);
1110 error = suser_cred(ap->a_cred, 0);
1115 rc->rc_dtrwait = *(int *)ap->a_data * hz / 100;
1119 *(int *)ap->a_data = rc->rc_dtrwait * 100 / hz;
1131 /* Modem control routines */
1133 static int rc_modctl(rc, bits, cmd)
1134 struct rc_chans *rc;
1137 int nec = rc->rc_rcb->rcb_addr;
1138 u_char *dtr = &rc->rc_rcb->rcb_dtr, msvr;
1140 rcout(CD180_CAR, rc->rc_chan);
1144 rcout(RC_DTREG, (bits & TIOCM_DTR) ?
1145 ~(*dtr |= 1 << rc->rc_chan) :
1146 ~(*dtr &= ~(1 << rc->rc_chan)));
1147 msvr = rcin(CD180_MSVR);
1148 if (bits & TIOCM_RTS)
1152 if (bits & TIOCM_DTR)
1156 rcout(CD180_MSVR, msvr);
1160 if (bits & TIOCM_DTR)
1161 rcout(RC_DTREG, ~(*dtr |= 1 << rc->rc_chan));
1162 msvr = rcin(CD180_MSVR);
1163 if (bits & TIOCM_RTS)
1165 if (bits & TIOCM_DTR)
1167 rcout(CD180_MSVR, msvr);
1172 msvr = rc->rc_msvr = rcin(CD180_MSVR);
1174 if (msvr & MSVR_RTS)
1176 if (msvr & MSVR_CTS)
1178 if (msvr & MSVR_DSR)
1180 if (msvr & MSVR_DTR)
1184 if (~rcin(RC_RIREG) & (1 << rc->rc_chan))
1189 if (bits & TIOCM_DTR)
1190 rcout(RC_DTREG, ~(*dtr &= ~(1 << rc->rc_chan)));
1191 msvr = rcin(CD180_MSVR);
1192 if (bits & TIOCM_RTS)
1194 if (bits & TIOCM_DTR)
1196 rcout(CD180_MSVR, msvr);
1199 rc->rc_msvr = rcin(CD180_MSVR);
1203 /* Test the board. */
1204 int rc_test(nec, unit)
1210 unsigned int iack, chipid;
1211 unsigned short divs;
1212 static u_char ctest[] = "\377\125\252\045\244\0\377";
1215 printf("rc%d: ", unit); printf s ; printf("\n"); \
1216 crit_exit(); return 1; }
1219 u_char txbuf[CD180_NFIFO]; /* TX buffer */
1220 u_char rxbuf[CD180_NFIFO]; /* RX buffer */
1221 int rxptr; /* RX pointer */
1222 int txptr; /* TX pointer */
1223 } tchans[CD180_NCHAN];
1229 /* First, reset board to inital state */
1230 rc_hwreset(unit, nec, chipid);
1232 divs = RC_BRD(19200);
1234 /* Initialize channels */
1235 for (chan = 0; chan < CD180_NCHAN; chan++) {
1237 /* Select and reset channel */
1238 rcout(CD180_CAR, chan);
1239 CCRCMD(unit, chan, CCR_ResetChan);
1240 WAITFORCCR(unit, chan);
1243 rcout(CD180_RBPRL, divs & 0xFF);
1244 rcout(CD180_RBPRH, divs >> 8);
1245 rcout(CD180_TBPRL, divs & 0xFF);
1246 rcout(CD180_TBPRH, divs >> 8);
1248 /* set timeout value */
1249 rcout(CD180_RTPR, 0);
1251 /* Establish local loopback */
1252 rcout(CD180_COR1, COR1_NOPAR | COR1_8BITS | COR1_1SB);
1253 rcout(CD180_COR2, COR2_LLM);
1254 rcout(CD180_COR3, CD180_NFIFO);
1255 CCRCMD(unit, chan, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
1256 CCRCMD(unit, chan, CCR_RCVREN | CCR_XMTREN);
1257 WAITFORCCR(unit, chan);
1258 rcout(CD180_MSVR, MSVR_RTS);
1260 /* Fill TXBUF with test data */
1261 for (i = 0; i < CD180_NFIFO; i++) {
1262 tchans[chan].txbuf[i] = ctest[i];
1263 tchans[chan].rxbuf[i] = 0;
1265 tchans[chan].txptr = tchans[chan].rxptr = 0;
1267 /* Now, start transmit */
1268 rcout(CD180_IER, IER_TxMpty|IER_RxData);
1270 /* Pseudo-interrupt poll stuff */
1271 for (rcnt = 10000; rcnt-- > 0; rcnt--) {
1272 i = ~(rcin(RC_BSR));
1273 if (i & RC_BSR_TOUT)
1274 ERR(("BSR timeout bit set\n"))
1275 else if (i & RC_BSR_TXINT) {
1276 iack = rcin(RC_PILR_TX);
1277 if (iack != (GIVR_IT_TDI | chipid))
1278 ERR(("Bad TX intr ack (%02x != %02x)\n",
1279 iack, GIVR_IT_TDI | chipid));
1280 chan = (rcin(CD180_GICR) & GICR_CHAN) >> GICR_LSH;
1281 /* If no more data to transmit, disable TX intr */
1282 if (tchans[chan].txptr >= CD180_NFIFO) {
1283 iack = rcin(CD180_IER);
1284 rcout(CD180_IER, iack & ~IER_TxMpty);
1286 for (iack = tchans[chan].txptr;
1287 iack < CD180_NFIFO; iack++)
1289 tchans[chan].txbuf[iack]);
1290 tchans[chan].txptr = iack;
1292 rcout(CD180_EOIR, 0);
1293 } else if (i & RC_BSR_RXINT) {
1296 iack = rcin(RC_PILR_RX);
1297 if (iack != (GIVR_IT_RGDI | chipid) &&
1298 iack != (GIVR_IT_REI | chipid))
1299 ERR(("Bad RX intr ack (%02x != %02x)\n",
1300 iack, GIVR_IT_RGDI | chipid))
1301 chan = (rcin(CD180_GICR) & GICR_CHAN) >> GICR_LSH;
1302 ucnt = rcin(CD180_RDCR) & 0xF;
1303 while (ucnt-- > 0) {
1304 iack = rcin(CD180_RCSR);
1305 if (iack & RCSR_Timeout)
1308 ERR(("Bad char chan %d (RCSR = %02X)\n",
1310 if (tchans[chan].rxptr > CD180_NFIFO)
1311 ERR(("Got extra chars chan %d\n",
1313 tchans[chan].rxbuf[tchans[chan].rxptr++] =
1316 rcout(CD180_EOIR, 0);
1319 for (iack = chan = 0; chan < CD180_NCHAN; chan++)
1320 if (tchans[chan].rxptr >= CD180_NFIFO)
1322 if (iack == CD180_NCHAN)
1325 for (chan = 0; chan < CD180_NCHAN; chan++) {
1326 /* Select and reset channel */
1327 rcout(CD180_CAR, chan);
1328 CCRCMD(unit, chan, CCR_ResetChan);
1332 ERR(("looses characters during local loopback\n"))
1333 /* Now, check data */
1334 for (chan = 0; chan < CD180_NCHAN; chan++)
1335 for (i = 0; i < CD180_NFIFO; i++)
1336 if (ctest[i] != tchans[chan].rxbuf[i])
1337 ERR(("data mismatch chan %d ptr %d (%d != %d)\n",
1338 chan, i, ctest[i], tchans[chan].rxbuf[i]))
1344 static void printrcflags(rc, comment)
1345 struct rc_chans *rc;
1348 u_short f = rc->rc_flags;
1349 int nec = rc->rc_rcb->rcb_addr;
1351 printf("rc%d/%d: %s flags: %s%s%s%s%s%s%s%s%s%s%s%s\n",
1352 rc->rc_rcb->rcb_unit, rc->rc_chan, comment,
1353 (f & RC_DTR_OFF)?"DTR_OFF " :"",
1354 (f & RC_ACTOUT) ?"ACTOUT " :"",
1355 (f & RC_RTSFLOW)?"RTSFLOW " :"",
1356 (f & RC_CTSFLOW)?"CTSFLOW " :"",
1357 (f & RC_DORXFER)?"DORXFER " :"",
1358 (f & RC_DOXXFER)?"DOXXFER " :"",
1359 (f & RC_MODCHG) ?"MODCHG " :"",
1360 (f & RC_OSUSP) ?"OSUSP " :"",
1361 (f & RC_OSBUSY) ?"OSBUSY " :"",
1362 (f & RC_WAS_BUFOVFL) ?"BUFOVFL " :"",
1363 (f & RC_WAS_SILOVFL) ?"SILOVFL " :"",
1364 (f & RC_SEND_RDY) ?"SEND_RDY":"");
1366 rcout(CD180_CAR, rc->rc_chan);
1368 printf("rc%d/%d: msvr %02x ier %02x ccsr %02x\n",
1369 rc->rc_rcb->rcb_unit, rc->rc_chan,
1374 #endif /* RCDEBUG */
1380 struct rc_chans *rc;
1382 rc = (struct rc_chans *)chan;
1383 rc->rc_flags &= ~RC_DTR_OFF;
1384 wakeup(&rc->rc_dtrwait);
1388 rc_discard_output(rc)
1389 struct rc_chans *rc;
1392 if (rc->rc_flags & RC_DOXXFER) {
1393 rc_scheduled_event -= LOTS_OF_EVENTS;
1394 rc->rc_flags &= ~RC_DOXXFER;
1396 rc->rc_optr = rc->rc_obufend;
1397 rc->rc_tp->t_state &= ~TS_BUSY;
1399 ttwwakeup(rc->rc_tp);
1406 if (rc_scheduled_event != 0) {
1411 callout_reset(&rc_wakeup_ch, 1, rc_wakeup, NULL);
1415 disc_optim(tp, t, rc)
1418 struct rc_chans *rc;
1421 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1422 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1423 && (!(t->c_iflag & PARMRK)
1424 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1425 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1426 && linesw[tp->t_line].l_rint == ttyinput)
1427 tp->t_state |= TS_CAN_BYPASS_L_RINT;
1429 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1430 rc->rc_hotchar = linesw[tp->t_line].l_hotchar;
1434 rc_wait0(nec, unit, chan, line)
1435 int nec, unit, chan, line;
1439 for (rcnt = 50; rcnt && rcin(CD180_CCR); rcnt--)
1442 printf("rc%d/%d: channel command timeout, rc.c line: %d\n",