xref: /netbsd/sys/dev/ic/cd18xx.c (revision bf9ec67e)
1 /*	$NetBSD: cd18xx.c,v 1.3 2002/03/17 19:40:56 atatat Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 2001 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*-
32  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
33  * All rights reserved.
34  *
35  * This code is derived from software contributed to The NetBSD Foundation
36  * by Charles M. Hannum.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *        This product includes software developed by the NetBSD
49  *        Foundation, Inc. and its contributors.
50  * 4. Neither the name of The NetBSD Foundation nor the names of its
51  *    contributors may be used to endorse or promote products derived
52  *    from this software without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
55  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64  * POSSIBILITY OF SUCH DAMAGE.
65  */
66 
67 /*
68  * Copyright (c) 1991 The Regents of the University of California.
69  * All rights reserved.
70  *
71  * Redistribution and use in source and binary forms, with or without
72  * modification, are permitted provided that the following conditions
73  * are met:
74  * 1. Redistributions of source code must retain the above copyright
75  *    notice, this list of conditions and the following disclaimer.
76  * 2. Redistributions in binary form must reproduce the above copyright
77  *    notice, this list of conditions and the following disclaimer in the
78  *    documentation and/or other materials provided with the distribution.
79  * 3. All advertising materials mentioning features or use of this software
80  *    must display the following acknowledgement:
81  *	This product includes software developed by the University of
82  *	California, Berkeley and its contributors.
83  * 4. Neither the name of the University nor the names of its contributors
84  *    may be used to endorse or promote products derived from this software
85  *    without specific prior written permission.
86  *
87  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
88  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
90  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
91  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
93  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
94  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
95  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
96  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
97  * SUCH DAMAGE.
98  *
99  *	@(#)com.c	7.5 (Berkeley) 5/16/91
100  */
101 
102 /*
103  * cirrus logic CL-CD180/CD1864/CD1865 driver, based in (large) parts on
104  * the com and z8530 drivers.  thanks charles.
105  */
106 
107 #include <sys/cdefs.h>
108 __KERNEL_RCSID(0, "$NetBSD: cd18xx.c,v 1.3 2002/03/17 19:40:56 atatat Exp $");
109 
110 #include <sys/param.h>
111 #include <sys/conf.h>
112 #include <sys/device.h>
113 #include <sys/systm.h>
114 #include <sys/malloc.h>
115 #include <sys/proc.h>
116 #include <sys/kernel.h>
117 #include <sys/tty.h>
118 #include <sys/fcntl.h>
119 
120 #include <machine/bus.h>
121 
122 #include <dev/ic/cd18xxvar.h>
123 #include <dev/ic/cd18xxreg.h>
124 
125 #include "ioconf.h"
126 
127 /*
128  * some helpers
129  */
130 
131 /* macros to clear/set/test flags. */
132 #define SET(t, f)	(t) |= (f)
133 #define CLR(t, f)	(t) &= ~(f)
134 #define ISSET(t, f)	((t) & (f))
135 
136 static void	cdtty_attach(struct cd18xx_softc *, int);
137 
138 static __inline void cd18xx_rint(struct cd18xx_softc *, int *);
139 static __inline void cd18xx_tint(struct cd18xx_softc *, int *);
140 static __inline void cd18xx_mint(struct cd18xx_softc *, int *);
141 
142 void cdtty_rxsoft(struct cd18xx_softc *, struct cdtty_port *, struct tty *);
143 void cdtty_txsoft(struct cd18xx_softc *, struct cdtty_port *, struct tty *);
144 void cdtty_stsoft(struct cd18xx_softc *, struct cdtty_port *, struct tty *);
145 void cd18xx_softintr(void *);
146 
147 cdev_decl(cdtty);
148 
149 static void	cdtty_shutdown(struct cd18xx_softc *, struct cdtty_port*);
150 static void	cdttystart(struct tty *);
151 static int	cdttyparam(struct tty *, struct termios *);
152 static void	cdtty_break(struct cd18xx_softc *, struct cdtty_port *, int);
153 static void	cdtty_modem(struct cd18xx_softc *, struct cdtty_port *, int);
154 static int	cdttyhwiflow(struct tty *, int);
155 static void	cdtty_hwiflow(struct cd18xx_softc *, struct cdtty_port *);
156 
157 static void	cdtty_loadchannelregs(struct cd18xx_softc *,
158 					   struct cdtty_port *);
159 
160 /* default read buffer size */
161 u_int cdtty_rbuf_size = CDTTY_RING_SIZE;
162 
163 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
164 u_int cdtty_rbuf_hiwat = (CDTTY_RING_SIZE * 1) / 4;
165 u_int cdtty_rbuf_lowat = (CDTTY_RING_SIZE * 3) / 4;
166 
167 #define CD18XXDEBUG
168 #ifdef CD18XXDEBUG
169 #define CDD_INFO	0x0001
170 #define CDD_INTR	0x0002
171 int cd18xx_debug = CDD_INTR|CDD_INFO;
172 # define DPRINTF(l, x)	if (cd18xx_debug & l) printf x
173 #else
174 # define DPRINTF(l, x)	/* nothing */
175 #endif
176 
177 /* Known supported revisions. */
178 struct cd18xx_revs {
179 	u_char	revision;
180 	u_char	onehundred_pin;
181 	char	*name;
182 } cd18xx_revs[] = {
183 	{ CD180_GFRCR_REV_B,		0, "CL-CD180 rev. B" },
184 	{ CD180_GFRCR_REV_C,		0, "CL-CD180 rev. C" },
185 	{ CD1864_GFRCR_REVISION_A,	1, "CL-CD1864 rev. A" },
186 	{ CD1865_GFRCR_REVISION_A,	1, "CL-CD1865 rev. A" },
187 	{ CD1865_GFRCR_REVISION_B,	1, "CL-CD1865 rev. B" },
188 	{ CD1865_GFRCR_REVISION_C,	1, "CL-CD1865 rev. C" },
189 	{ 0, 0, 0 }
190 };
191 
192 /* wait for the CCR to go to zero */
193 static __inline int cd18xx_wait_ccr(struct cd18xx_softc *);
194 static __inline int
195 cd18xx_wait_ccr(sc)
196 	struct cd18xx_softc *sc;
197 {
198 	int i = 100000;
199 
200 	while (--i &&
201 	    bus_space_read_1(sc->sc_tag, sc->sc_handle, CD18xx_CCR) == 0)
202 		break;
203 	return (i == 0);
204 }
205 
206 /*
207  * device attach routine, high-end portion
208  */
209 void
210 cd18xx_attach(sc)
211 	struct cd18xx_softc *sc;
212 {
213 	static int chip_id_next = 1;
214 	int onehundred_pin, revision, i, port;
215 
216 	/* read and print the revision */
217 	revision = cd18xx_read(sc, CD18xx_GFRCR);
218 	onehundred_pin = ISSET(cd18xx_read(sc, CD18xx_SRCR),CD18xx_SRCR_PKGTYP);
219 	for (i = 0; cd18xx_revs[i].name; i++)
220 		if (revision == cd18xx_revs[i].revision ||
221 		    onehundred_pin == cd18xx_revs[i].onehundred_pin) {
222 			printf(": %s", cd18xx_revs[i].name);
223 			break;
224 		}
225 
226 	if (cd18xx_revs[i].name == NULL) {
227 		printf("%s: unknown revision, bailing.\n", sc->sc_dev.dv_xname);
228 		return;
229 	}
230 
231 	/* prepare for reset */
232 	cd18xx_set_car(sc, 0);
233 	cd18xx_write(sc, CD18xx_GSVR, CD18xx_GSVR_CLEAR);
234 
235 	/* wait for CCR to go to zero */
236 	if (cd18xx_wait_ccr(sc)) {
237 		printf("cd18xx_attach: reset change command timed out\n");
238 		return;
239 	}
240 
241 	/* full reset of all channels */
242 	cd18xx_write(sc, CD18xx_CCR,
243 	    CD18xx_CCR_RESET|CD18xx_CCR_RESET_HARD);
244 
245 	/* loop until the GSVR is ready */
246 	i = 100000;
247 	while (--i && cd18xx_read(sc, CD18xx_GSVR) == CD18xx_GSVR_READY)
248 		;
249 	if (i == 0) {
250 		printf("\n%s: did not reset!\n", sc->sc_dev.dv_xname);
251 		return;
252 	}
253 
254 	/* write the chip_id */
255 	sc->sc_chip_id = chip_id_next++;
256 #ifdef DIAGNOSTIC
257 	if (sc->sc_chip_id > 31)
258 		panic("more than 31 cd18xx's?  help.");
259 #endif
260 	cd18xx_write(sc, CD18xx_GSVR, CD18xx_GSVR_SETID(sc));
261 
262 	/* rx/tx/modem service match vectors, initalised by higher level */
263 	cd18xx_write(sc, CD18xx_MSMR, sc->sc_msmr | 0x80);
264 	cd18xx_write(sc, CD18xx_TSMR, sc->sc_tsmr | 0x80);
265 	cd18xx_write(sc, CD18xx_RSMR, sc->sc_rsmr | 0x80);
266 
267 	printf(", gsvr %x msmr %x tsmr %x rsmr %x",
268 	    cd18xx_read(sc, CD18xx_GSVR),
269 	    cd18xx_read(sc, CD18xx_MSMR),
270 	    cd18xx_read(sc, CD18xx_TSMR),
271 	    cd18xx_read(sc, CD18xx_RSMR));
272 
273 	/* prescale registers */
274 	sc->sc_pprh = 0xf0;
275 	sc->sc_pprl = 0;
276 	cd18xx_write(sc, CD18xx_PPRH, sc->sc_pprh);
277 	cd18xx_write(sc, CD18xx_PPRL, sc->sc_pprl);
278 
279 	/* establish our soft interrupt. */
280 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, cd18xx_softintr, sc);
281 
282 	printf(", 8 ports ready (chip id %d)\n", sc->sc_chip_id);
283 
284 	/*
285 	 * finally, we loop over all 8 channels initialising them
286 	 */
287 	for (port = 0; port < 8; port++)
288 		cdtty_attach(sc, port);
289 }
290 
291 /* tty portion of the code */
292 
293 /*
294  * tty portion attach routine
295  */
296 void
297 cdtty_attach(sc, port)
298 	struct	cd18xx_softc *sc;
299 	int port;
300 {
301 	struct cdtty_port *p = &sc->sc_ports[port];
302 	int i;
303 
304 	/* load CAR with channel number */
305 	cd18xx_set_car(sc, port);
306 
307 	/* wait for CCR to go to zero */
308 	if (cd18xx_wait_ccr(sc)) {
309 		printf("cd18xx_attach: change command timed out setting "
310 		       "CAR for port %d\n", i);
311 		return;
312 	}
313 
314 	/* set the RPTR to (arbitrary) 8 */
315 	cd18xx_write(sc, CD18xx_RTPR, 8);
316 
317 	/* reset the modem signal value register */
318 	sc->sc_ports[port].p_msvr = CD18xx_MSVR_RESET;
319 
320 	/* zero the service request enable register */
321 	cd18xx_write(sc, CD18xx_SRER, 0);
322 
323 	/* enable the transmitter & receiver */
324 	SET(p->p_chanctl, CD18xx_CCR_CHANCTL |
325 		          CD18xx_CCR_CHANCTL_TxEN |
326 		          CD18xx_CCR_CHANCTL_RxEN);
327 
328 	/* XXX no console or kgdb support yet! */
329 
330 	/* get a tty structure */
331 	p->p_tty = ttymalloc();
332 	p->p_tty->t_oproc = cdttystart;
333 	p->p_tty->t_param = cdttyparam;
334 	p->p_tty->t_hwiflow = cdttyhwiflow;
335 
336 	p->p_rbuf = malloc(cdtty_rbuf_size << 1, M_DEVBUF, M_WAITOK);
337 	p->p_rbput = p->p_rbget = p->p_rbuf;
338 	p->p_rbavail = cdtty_rbuf_size;
339 	if (p->p_rbuf == NULL) {
340 		printf("%s: unable to allocate ring buffer for tty %d\n",
341 		    sc->sc_dev.dv_xname, port);
342 		return;
343 	}
344 	p->p_ebuf = p->p_rbuf + (cdtty_rbuf_size << 1);
345 
346 	tty_attach(p->p_tty);
347 }
348 
349 /*
350  * below here are the tty portion device routines.
351  */
352 void
353 cdtty_shutdown(sc, p)
354 	struct cd18xx_softc *sc;
355 	struct cdtty_port *p;
356 {
357 	struct tty *tp = p->p_tty;
358 	int s;
359 
360 	s = splserial();
361 
362 	/* If we were asserting flow control, then deassert it. */
363 	SET(p->p_rx_flags, RX_IBUF_BLOCKED);
364 	cdtty_hwiflow(sc, p);
365 
366 	/* Clear any break condition set with TIOCSBRK. */
367 	cdtty_break(sc, p, 0);
368 
369 	/*
370 	 * Hang up if necessary.  Wait a bit, so the other side has time to
371 	 * notice even if we immediately open the port again.
372 	 * Avoid tsleeping above splhigh().
373 	 */
374 	if (ISSET(tp->t_cflag, HUPCL)) {
375 		cdtty_modem(sc, p, 0);
376 		splx(s);
377 		/* XXX tsleep will only timeout */
378 		(void) tsleep(sc, TTIPRI, ttclos, hz);
379 		s = splserial();
380 	}
381 
382 	/* Turn off interrupts. */
383 	p->p_srer = 0;
384 	cd18xx_write(sc, CD18xx_SRER, p->p_srer);
385 
386 	splx(s);
387 }
388 
389 /*
390  * cdttyopen:  open syscall for cdtty terminals..
391  */
392 int
393 cdttyopen(dev, flag, mode, p)
394 	dev_t dev;
395 	int flag;
396 	int mode;
397 	struct proc *p;
398 {
399 	struct tty *tp;
400 	struct cd18xx_softc *sc;
401 	struct cdtty_port *port;
402 	int channel, instance, s, error;
403 
404 	channel = CD18XX_CHANNEL(dev);
405 	instance = CD18XX_INSTANCE(dev);
406 
407 	/* ensure instance is valid */
408 	if (instance >= clcd_cd.cd_ndevs)
409 		return (ENXIO);
410 
411 	/* get softc and port */
412 	sc = clcd_cd.cd_devs[instance];
413 	if (sc == NULL)
414 		return (ENXIO);
415 	port = &sc->sc_ports[channel];
416 	if (port == NULL || port->p_rbuf == NULL)
417 		return (ENXIO);
418 
419 	/* kgdb support?  maybe later... */
420 
421 	tp = port->p_tty;
422 
423 	/* enforce exclude */
424 	if (tp == NULL ||
425 	    (ISSET(tp->t_state, TS_ISOPEN) &&
426 	    ISSET(tp->t_state, TS_XCLUDE) &&
427 	    (p->p_ucred->cr_uid != 0)))
428 		return (EBUSY);
429 
430 	s = spltty();
431 
432 	/*
433 	 * Do the following iff this is a first open.
434 	 */
435 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
436 		struct termios t;
437 
438 		/* set up things in tp as necessary */
439 		tp->t_dev = dev;
440 
441 		/*
442 		 * Initialize the termios status to the defaults.  Add in the
443 		 * sticky bits from TIOCSFLAGS.
444 		 */
445 		t.c_ispeed = 0;
446 		t.c_ospeed = TTYDEF_SPEED;
447 		t.c_cflag = TTYDEF_CFLAG;
448 
449 		if (ISSET(port->p_swflags, TIOCFLAG_CLOCAL))
450 			SET(t.c_cflag, CLOCAL);
451 		if (ISSET(port->p_swflags, TIOCFLAG_CRTSCTS))
452 			SET(t.c_cflag, CRTSCTS);
453 		if (ISSET(port->p_swflags, TIOCFLAG_CDTRCTS))
454 			SET(t.c_cflag, CDTRCTS);
455 		if (ISSET(port->p_swflags, TIOCFLAG_MDMBUF))
456 			SET(t.c_cflag, MDMBUF);
457 
458 		/* Make sure param will see changes. */
459 		tp->t_ospeed = 0;
460 		(void)cdttyparam(tp, &t);
461 
462 		tp->t_iflag = TTYDEF_IFLAG;
463 		tp->t_oflag = TTYDEF_OFLAG;
464 		tp->t_lflag = TTYDEF_LFLAG;
465 		ttychars(tp);
466 		ttsetwater(tp);
467 
468 		(void)splserial();
469 
470 		/* turn on rx and modem interrupts */
471 		cd18xx_set_car(sc, CD18XX_CHANNEL(dev));
472 		SET(port->p_srer, CD18xx_SRER_Rx |
473 				  CD18xx_SRER_RxSC |
474 				  CD18xx_SRER_CD);
475 		cd18xx_write(sc, CD18xx_SRER, port->p_srer);
476 
477 		/* always turn on DTR when open */
478 		cdtty_modem(sc, port, 1);
479 
480 		/* initialise ring buffer */
481 		port->p_rbget = port->p_rbput = port->p_rbuf;
482 		port->p_rbavail = cdtty_rbuf_size;
483 		CLR(port->p_rx_flags, RX_ANY_BLOCK);
484 		cdtty_hwiflow(sc, port);
485 	}
486 
487 	/* drop spl back before going into the line open */
488 	splx(s);
489 
490 	error = ttyopen(tp, CD18XX_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
491 	if (error == 0)
492 		error = (*tp->t_linesw->l_open)(dev, tp);
493 
494 	return (error);
495 }
496 
497 /*
498  * cdttyclose:  close syscall for cdtty terminals..
499  */
500 int
501 cdttyclose(dev, flag, mode, p)
502 	dev_t dev;
503 	int flag;
504 	int mode;
505 	struct proc *p;
506 {
507 	struct cd18xx_softc *sc;
508 	struct cdtty_port *port;
509 	struct tty *tp;
510 	int channel, instance;
511 
512 	channel = CD18XX_CHANNEL(dev);
513 	instance = CD18XX_INSTANCE(dev);
514 
515 	/* ensure instance is valid */
516 	if (instance >= clcd_cd.cd_ndevs)
517 		return (ENXIO);
518 
519 	/* get softc and port */
520 	sc = clcd_cd.cd_devs[instance];
521 	if (sc == NULL)
522 		return (ENXIO);
523 	port = &sc->sc_ports[channel];
524 
525 	tp = port->p_tty;
526 
527 	(*tp->t_linesw->l_close)(tp, flag);
528 	ttyclose(tp);
529 
530 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
531 		/*
532 		 * Although we got a last close, the device may still be in
533 		 * use; e.g. if this was the dialout node, and there are still
534 		 * processes waiting for carrier on the non-dialout node.
535 		 */
536 		cdtty_shutdown(sc, port);
537 	}
538 
539 	return (0);
540 }
541 
542 /*
543  * cdttyread:  read syscall for cdtty terminals..
544  */
545 int
546 cdttyread(dev, uio, flag)
547 	dev_t dev;
548 	struct uio *uio;
549 	int flag;
550 {
551 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
552 	struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
553 	struct tty *tp = port->p_tty;
554 
555 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
556 }
557 
558 /*
559  * cdttywrite:  write syscall for cdtty terminals..
560  */
561 int
562 cdttywrite(dev, uio, flag)
563 	dev_t dev;
564 	struct uio *uio;
565 	int flag;
566 {
567 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
568 	struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
569 	struct tty *tp = port->p_tty;
570 
571 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
572 }
573 
574 int
575 cdttypoll(dev, events, p)
576 	dev_t dev;
577 	int events;
578 	struct proc *p;
579 {
580 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
581 	struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
582 	struct tty *tp = port->p_tty;
583 
584 	return ((*tp->t_linesw->l_poll)(tp, events, p));
585 }
586 
587 /*
588  * cdttytty:  return a pointer to our (cdtty) tp.
589  */
590 struct tty *
591 cdttytty(dev)
592 	dev_t dev;
593 {
594 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
595 	struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
596 
597 	return (port->p_tty);
598 }
599 
600 /*
601  * cdttyioctl:  ioctl syscall for cdtty terminals..
602  */
603 int
604 cdttyioctl(dev, cmd, data, flag, p)
605 	dev_t dev;
606 	u_long cmd;
607 	caddr_t data;
608 	int flag;
609 	struct proc *p;
610 {
611 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
612 	struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
613 	struct tty *tp = port->p_tty;
614 	int error, s;
615 
616 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
617 	if (error != EPASSTHROUGH)
618 		return (error);
619 
620 	error = ttioctl(tp, cmd, data, flag, p);
621 	if (error != EPASSTHROUGH)
622 		return (error);
623 
624 	s = splserial();
625 
626 	switch (cmd) {
627 	case TIOCSBRK:
628 		cdtty_break(sc, port, 1);
629 		break;
630 
631 	case TIOCCBRK:
632 		cdtty_break(sc, port, 0);
633 		break;
634 
635 	case TIOCSDTR:
636 		cdtty_modem(sc, port, 1);
637 		break;
638 
639 	case TIOCCDTR:
640 		cdtty_modem(sc, port, 0);
641 		break;
642 
643 	case TIOCGFLAGS:
644 		*(int *)data = port->p_swflags;
645 		break;
646 
647 	case TIOCSFLAGS:
648 		error = suser(p->p_ucred, &p->p_acflag);
649 		if (error)
650 			return (error);
651 		port->p_swflags = *(int *)data;
652 		break;
653 
654 	case TIOCMSET:
655 	case TIOCMBIS:
656 	case TIOCMBIC:
657 	case TIOCMGET:
658 	default:
659 		return (EPASSTHROUGH);
660 	}
661 
662 	splx(s);
663 	return (0);
664 }
665 
666 /*
667  * Start or restart transmission.
668  */
669 static void
670 cdttystart(tp)
671 	struct tty *tp;
672 {
673 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(tp->t_dev)];
674 	struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
675 	int s;
676 
677 	s = spltty();
678 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
679 		goto out;
680 	if (p->p_tx_stopped)
681 		goto out;
682 
683 	if (tp->t_outq.c_cc <= tp->t_lowat) {
684 		if (ISSET(tp->t_state, TS_ASLEEP)) {
685 			CLR(tp->t_state, TS_ASLEEP);
686 			wakeup((caddr_t)&tp->t_outq);
687 		}
688 		selwakeup(&tp->t_wsel);
689 		if (tp->t_outq.c_cc == 0)
690 			goto out;
691 	}
692 
693 	/* Grab the first contiguous region of buffer space. */
694 	{
695 		u_char *tba;
696 		int tbc;
697 
698 		tba = tp->t_outq.c_cf;
699 		tbc = ndqb(&tp->t_outq, 0);
700 
701 		(void)splserial();
702 
703 		p->p_tba = tba;
704 		p->p_tbc = tbc;
705 	}
706 
707 	SET(tp->t_state, TS_BUSY);
708 	p->p_tx_busy = 1;
709 
710 	/* turn on tx interrupts */
711 	if ((p->p_srer & CD18xx_SRER_Tx) == 0) {
712 		cd18xx_set_car(sc, CD18XX_CHANNEL(tp->t_dev));
713 		SET(p->p_srer, CD18xx_SRER_Tx);
714 		cd18xx_write(sc, CD18xx_SRER, p->p_srer);
715 	}
716 
717 	/*
718 	 * Now bail; we can't actually transmit bytes until we're in a
719 	 * transmit interrupt service routine.
720 	 */
721 out:
722 	splx(s);
723 	return;
724 }
725 
726 /*
727  * cdttystop:  handing ^S or other stop signals, for a cdtty
728  */
729 void
730 cdttystop(tp, flag)
731 	struct tty *tp;
732 	int flag;
733 {
734 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(tp->t_dev)];
735 	struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
736 	int s;
737 
738 	s = splserial();
739 	if (ISSET(tp->t_state, TS_BUSY)) {
740 		/* Stop transmitting at the next chunk. */
741 		p->p_tbc = 0;
742 		p->p_heldtbc = 0;
743 		if (!ISSET(tp->t_state, TS_TTSTOP))
744 			SET(tp->t_state, TS_FLUSH);
745 	}
746 	splx(s);
747 }
748 
749 /*
750  * load a channel's registers.
751  */
752 void
753 cdtty_loadchannelregs(sc, p)
754 	struct cd18xx_softc *sc;
755 	struct cdtty_port *p;
756 {
757 
758 	cd18xx_set_car(sc, CD18XX_CHANNEL(p->p_tty->t_dev));
759 	cd18xx_write(sc, CD18xx_SRER, p->p_srer);
760 	cd18xx_write(sc, CD18xx_MSVR, p->p_msvr_active = p->p_msvr);
761 	cd18xx_write(sc, CD18xx_COR1, p->p_cor1);
762 	cd18xx_write(sc, CD18xx_COR2, p->p_cor2);
763 	cd18xx_write(sc, CD18xx_COR3, p->p_cor3);
764 	/*
765 	 * COR2 and COR3 change commands are not required here for
766 	 * the CL-CD1865 but we do them anyway for simplicity.
767 	 */
768 	cd18xx_write(sc, CD18xx_CCR, CD18xx_CCR_CORCHG |
769 				     CD18xx_CCR_CORCHG_COR1 |
770 				     CD18xx_CCR_CORCHG_COR2 |
771 				     CD18xx_CCR_CORCHG_COR3);
772 	cd18xx_write(sc, CD18xx_RBPRH, p->p_rbprh);
773 	cd18xx_write(sc, CD18xx_RBPRL, p->p_rbprl);
774 	cd18xx_write(sc, CD18xx_TBPRH, p->p_tbprh);
775 	cd18xx_write(sc, CD18xx_TBPRL, p->p_tbprl);
776 	if (cd18xx_wait_ccr(sc)) {
777 		DPRINTF(CDD_INFO,
778 		    ("%s: cdtty_loadchannelregs ccr wait timed out\n",
779 		    sc->sc_dev.dv_xname));
780 	}
781 	cd18xx_write(sc, CD18xx_CCR, p->p_chanctl);
782 }
783 
784 /*
785  * Set tty parameters from termios.
786  * XXX - Should just copy the whole termios after
787  * making sure all the changes could be done.
788  */
789 static int
790 cdttyparam(tp, t)
791 	struct tty *tp;
792 	struct termios *t;
793 {
794 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(tp->t_dev)];
795 	struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
796 	int s;
797 
798 	/* Check requested parameters. */
799 	if (t->c_ospeed < 0)
800 		return (EINVAL);
801 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
802 		return (EINVAL);
803 
804 	/*
805 	 * For the console, always force CLOCAL and !HUPCL, so that the port
806 	 * is always active.
807 	 */
808 	if (ISSET(p->p_swflags, TIOCFLAG_SOFTCAR)) {
809 		SET(t->c_cflag, CLOCAL);
810 		CLR(t->c_cflag, HUPCL);
811 	}
812 
813 	/*
814 	 * If there were no changes, don't do anything.  This avoids dropping
815 	 * input and improves performance when all we did was frob things like
816 	 * VMIN and VTIME.
817 	 */
818 	if (tp->t_ospeed == t->c_ospeed &&
819 	    tp->t_cflag == t->c_cflag)
820 		return (0);
821 
822 	/*
823 	 * Block interrupts so that state will not
824 	 * be altered until we are done setting it up.
825 	 */
826 	s = splserial();
827 
828 	/*
829 	 * Copy across the size, parity and stop bit info.
830 	 */
831 	switch (t->c_cflag & CSIZE) {
832 	case CS5:
833 		p->p_cor1 = CD18xx_COR1_CS5;
834 		break;
835 	case CS6:
836 		p->p_cor1 = CD18xx_COR1_CS6;
837 		break;
838 	case CS7:
839 		p->p_cor1 = CD18xx_COR1_CS7;
840 		break;
841 	default:
842 		p->p_cor1 = CD18xx_COR1_CS8;
843 		break;
844 	}
845 	if (ISSET(t->c_cflag, PARENB)) {
846 		SET(p->p_cor1, CD18xx_COR1_PARITY_NORMAL);
847 		if (ISSET(t->c_cflag, PARODD))
848 			SET(p->p_cor1, CD18xx_COR1_PARITY_ODD);
849 	}
850 	if (!ISSET(t->c_iflag, INPCK))
851 		SET(p->p_cor1, CD18xx_COR1_IGNORE);
852 	if (ISSET(t->c_cflag, CSTOPB))
853 		SET(p->p_cor1, CD18xx_COR1_STOPBIT_2);
854 
855 	/*
856 	 * If we're not in a mode that assumes a connection is present, then
857 	 * ignore carrier changes.
858 	 */
859 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
860 		p->p_msvr_dcd = 0;
861 	else
862 		p->p_msvr_dcd = CD18xx_MSVR_CD;
863 
864 	/*
865 	 * Set the flow control pins depending on the current flow control
866 	 * mode.
867 	 */
868 	if (ISSET(t->c_cflag, CRTSCTS)) {
869 		p->p_mcor1_dtr = CD18xx_MCOR1_DTR;
870 		p->p_msvr_rts = CD18xx_MSVR_RTS;
871 		p->p_msvr_cts = CD18xx_MSVR_CTS;
872 		p->p_cor2 = CD18xx_COR2_RTSAOE|CD18xx_COR2_CTSAE;
873 	} else if (ISSET(t->c_cflag, MDMBUF)) {
874 		/*
875 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
876 		 * carrier detection.
877 		 */
878 		p->p_mcor1_dtr = 0;
879 		p->p_msvr_rts = CD18xx_MSVR_DTR;
880 		p->p_msvr_cts = CD18xx_MSVR_CD;
881 		p->p_cor2 = 0;
882 	} else {
883 		/*
884 		 * If no flow control, then always set RTS.  This will make
885 		 * the other side happy if it mistakenly thinks we're doing
886 		 * RTS/CTS flow control.
887 		 */
888 		p->p_mcor1_dtr = CD18xx_MSVR_DTR;
889 		p->p_msvr_rts = 0;
890 		p->p_msvr_cts = 0;
891 		p->p_cor2 = 0;
892 	}
893 	p->p_msvr_mask = p->p_msvr_cts | p->p_msvr_dcd;
894 
895 	/*
896 	 * Set the FIFO threshold based on the receive speed.
897 	 *
898 	 *  * If it's a low speed, it's probably a mouse or some other
899 	 *    interactive device, so set the threshold low.
900 	 *  * If it's a high speed, trim the trigger level down to prevent
901 	 *    overflows.
902 	 *  * Otherwise set it a bit higher.
903 	 */
904 	p->p_cor3 = (t->c_ospeed <= 1200 ? 1 :
905 			 t->c_ospeed <= 38400 ? 8 : 4);
906 
907 #define PORT_RATE(o, s)	\
908 	(((((o) + (s)/2) / (s)) + CD18xx_xBRPR_TPC/2) / CD18xx_xBRPR_TPC)
909 	/* Compute BPS for the requested speeds */
910 	if (t->c_ospeed) {
911 		u_int32_t tbpr = PORT_RATE(sc->sc_osc, t->c_ospeed);
912 
913 		if (tbpr == 0 || tbpr > 0xffff)
914 			return (EINVAL);
915 
916 		p->p_tbprh = tbpr >> 8;
917 		p->p_tbprl = tbpr & 0xff;
918 	}
919 
920 	if (t->c_ispeed) {
921 		u_int32_t rbpr = PORT_RATE(sc->sc_osc, t->c_ispeed);
922 
923 		if (rbpr == 0 || rbpr > 0xffff)
924 			return (EINVAL);
925 
926 		p->p_rbprh = rbpr >> 8;
927 		p->p_rbprl = rbpr & 0xff;
928 	}
929 
930 	/* And copy to tty. */
931 	tp->t_ispeed = 0;
932 	tp->t_ospeed = t->c_ospeed;
933 	tp->t_cflag = t->c_cflag;
934 
935 	if (!p->p_heldchange) {
936 		if (p->p_tx_busy) {
937 			p->p_heldtbc = p->p_tbc;
938 			p->p_tbc = 0;
939 			p->p_heldchange = 1;
940 		} else
941 			cdtty_loadchannelregs(sc, p);
942 	}
943 
944 	if (!ISSET(t->c_cflag, CHWFLOW)) {
945 		/* Disable the high water mark. */
946 		p->p_r_hiwat = 0;
947 		p->p_r_lowat = 0;
948 		if (ISSET(p->p_rx_flags, RX_TTY_OVERFLOWED)) {
949 			CLR(p->p_rx_flags, RX_TTY_OVERFLOWED);
950 			softintr_schedule(sc->sc_si);
951 		}
952 		if (ISSET(p->p_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
953 			CLR(p->p_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
954 			cdtty_hwiflow(sc, p);
955 		}
956 	} else {
957 		p->p_r_hiwat = cdtty_rbuf_hiwat;
958 		p->p_r_lowat = cdtty_rbuf_lowat;
959 	}
960 
961 	splx(s);
962 
963 	/*
964 	 * Update the tty layer's idea of the carrier bit, in case we changed
965 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
966 	 * explicit request.
967 	 */
968 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(p->p_msvr, CD18xx_MSVR_CD));
969 
970 	if (!ISSET(t->c_cflag, CHWFLOW)) {
971 		if (p->p_tx_stopped) {
972 			p->p_tx_stopped = 0;
973 			cdttystart(tp);
974 		}
975 	}
976 
977 	return (0);
978 }
979 
980 static void
981 cdtty_break(sc, p, onoff)
982 	struct cd18xx_softc *sc;
983 	struct cdtty_port *p;
984 	int onoff;
985 {
986 
987 	/* tell tx intr handler we need a break */
988 	p->p_needbreak = !!onoff;
989 
990 	/* turn on tx interrupts if break has changed */
991 	if (p->p_needbreak != p->p_break)
992 		SET(p->p_srer, CD18xx_SRER_Tx);
993 
994 	if (!p->p_heldchange) {
995 		if (p->p_tx_busy) {
996 			p->p_heldtbc = p->p_tbc;
997 			p->p_tbc = 0;
998 			p->p_heldchange = 1;
999 		} else
1000 			cdtty_loadchannelregs(sc, p);
1001 	}
1002 }
1003 
1004 /*
1005  * Raise or lower modem control (DTR/RTS) signals.  If a character is
1006  * in transmission, the change is deferred.
1007  */
1008 static void
1009 cdtty_modem(sc, p, onoff)
1010 	struct cd18xx_softc *sc;
1011 	struct cdtty_port *p;
1012 	int onoff;
1013 {
1014 
1015 	if (p->p_mcor1_dtr == 0)
1016 		return;
1017 
1018 	if (onoff)
1019 		CLR(p->p_mcor1, p->p_mcor1_dtr);
1020 	else
1021 		SET(p->p_mcor1, p->p_mcor1_dtr);
1022 
1023 	if (!p->p_heldchange) {
1024 		if (p->p_tx_busy) {
1025 			p->p_heldtbc = p->p_tbc;
1026 			p->p_tbc = 0;
1027 			p->p_heldchange = 1;
1028 		} else
1029 			cdtty_loadchannelregs(sc, p);
1030 	}
1031 }
1032 
1033 /*
1034  * Try to block or unblock input using hardware flow-control.
1035  * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
1036  * if this function returns non-zero, the TS_TBLOCK flag will
1037  * be set or cleared according to the "block" arg passed.
1038  */
1039 int
1040 cdttyhwiflow(tp, block)
1041 	struct tty *tp;
1042 	int block;
1043 {
1044 	struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(tp->t_dev)];
1045 	struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
1046 	int s;
1047 
1048 	if (p->p_msvr_rts == 0)
1049 		return (0);
1050 
1051 	s = splserial();
1052 	if (block) {
1053 		if (!ISSET(p->p_rx_flags, RX_TTY_BLOCKED)) {
1054 			SET(p->p_rx_flags, RX_TTY_BLOCKED);
1055 			cdtty_hwiflow(sc, p);
1056 		}
1057 	} else {
1058 		if (ISSET(p->p_rx_flags, RX_TTY_OVERFLOWED)) {
1059 			CLR(p->p_rx_flags, RX_TTY_OVERFLOWED);
1060 			softintr_schedule(sc->sc_si);
1061 		}
1062 		if (ISSET(p->p_rx_flags, RX_TTY_BLOCKED)) {
1063 			CLR(p->p_rx_flags, RX_TTY_BLOCKED);
1064 			cdtty_hwiflow(sc, p);
1065 		}
1066 	}
1067 	splx(s);
1068 	return (1);
1069 }
1070 
1071 /*
1072  * Internal version of cdttyhwiflow, called at cdtty's priority.
1073  */
1074 static void
1075 cdtty_hwiflow(sc, p)
1076 	struct cd18xx_softc *sc;
1077 	struct cdtty_port *p;
1078 {
1079 
1080 	if (p->p_msvr_rts == 0)
1081 		return;
1082 
1083 	if (ISSET(p->p_rx_flags, RX_ANY_BLOCK)) {
1084 		CLR(p->p_msvr, p->p_msvr_rts);
1085 		CLR(p->p_msvr_active, p->p_msvr_rts);
1086 	} else {
1087 		SET(p->p_msvr, p->p_msvr_rts);
1088 		SET(p->p_msvr_active, p->p_msvr_rts);
1089 	}
1090 	cd18xx_set_car(sc, CD18XX_CHANNEL(p->p_tty->t_dev));
1091 	cd18xx_write(sc, CD18xx_MSVR, p->p_msvr_active);
1092 }
1093 
1094 /*
1095  * indiviual interrupt routines.
1096  */
1097 
1098 /*
1099  * this is the number of interrupts allowed, total.  set it to 0
1100  * to allow unlimited interrpts
1101  */
1102 #define INTR_MAX_ALLOWED	0
1103 
1104 #if INTR_MAX_ALLOWED == 0
1105 #define GOTINTR(sc, p)	/* nothing */
1106 #else
1107 int intrcount;
1108 #define GOTINTR(sc, p)	\
1109 do { \
1110 	if (intrcount++ == INTR_MAX_ALLOWED) { \
1111 		CLR(p->p_srer, CD18xx_SRER_Tx); \
1112 		cd18xx_write(sc, CD18xx_SRER, p->p_srer); \
1113 	} \
1114 	DPRINTF(CDD_INTR, (", intrcount %d srer %x", intrcount, p->p_srer)); \
1115 } while (0)
1116 #endif
1117 
1118 /* receiver interrupt */
1119 static __inline void
1120 cd18xx_rint(sc, ns)
1121 	struct cd18xx_softc *sc;
1122 	int *ns;
1123 {
1124 	struct cdtty_port *p;
1125 	u_int channel, count;
1126 	u_char *put, *end;
1127 	u_int cc;
1128 
1129 	/* work out the channel and softc */
1130 	channel = cd18xx_get_gscr1_channel(sc);
1131 	p = &sc->sc_ports[channel];
1132 	DPRINTF(CDD_INTR, ("%s: rint: channel %d", sc->sc_dev.dv_xname, channel));
1133 	GOTINTR(sc, p);
1134 
1135 	end = p->p_ebuf;
1136 	put = p->p_rbput;
1137 	cc = p->p_rbavail;
1138 
1139 	/* read as many bytes as necessary */
1140 	count = cd18xx_read(sc, CD18xx_RDCR);
1141 	DPRINTF(CDD_INTR, (", %d bytes available: ", count));
1142 
1143 	while (cc > 0 && count > 0) {
1144 		u_char rcsr = cd18xx_read(sc, CD18xx_RCSR);
1145 
1146 		put[0] = cd18xx_read(sc, CD18xx_RDR);
1147 		put[1] = rcsr;
1148 
1149 		if (rcsr)
1150 			*ns = 1;
1151 
1152 		put += 2;
1153 		if (put >= end)
1154 			put = p->p_rbuf;
1155 
1156 		DPRINTF(CDD_INTR, ("."));
1157 		cc--;
1158 		count--;
1159 	}
1160 
1161 	DPRINTF(CDD_INTR, (" finished reading"));
1162 
1163 	/*
1164 	 * Current string of incoming characters ended because
1165 	 * no more data was available or we ran out of space.
1166 	 * If we're out of space, turn off receive interrupts.
1167 	 */
1168 	p->p_rbput = put;
1169 	p->p_rbavail = cc;
1170 	if (!ISSET(p->p_rx_flags, RX_TTY_OVERFLOWED)) {
1171 		p->p_rx_ready = 1;
1172 	}
1173 
1174 	/*
1175 	 * If we're out of space, disable receive interrupts
1176 	 * until the queue has drained a bit.
1177 	 */
1178 	if (!cc) {
1179 		SET(p->p_rx_flags, RX_IBUF_OVERFLOWED);
1180 		CLR(p->p_srer, CD18xx_SRER_Rx |
1181 			       CD18xx_SRER_RxSC |
1182 			       CD18xx_SRER_CD);
1183 		cd18xx_write(sc, CD18xx_SRER, p->p_srer);
1184 	}
1185 
1186 	/* finish the interrupt transaction with the IC */
1187 	cd18xx_write(sc, CD18xx_EOSRR, 0);
1188 	DPRINTF(CDD_INTR, (", done\n"));
1189 }
1190 
1191 /*
1192  * transmitter interrupt
1193  *
1194  * note this relys on the fact that we allow the transmitter FIFO to
1195  * drain completely
1196  */
1197 static __inline void
1198 cd18xx_tint(sc, ns)
1199 	struct cd18xx_softc *sc;
1200 	int *ns;
1201 {
1202 	struct cdtty_port *p;
1203 	u_int channel;
1204 
1205 	/* work out the channel and softc */
1206 	channel = cd18xx_get_gscr1_channel(sc);
1207 	p = &sc->sc_ports[channel];
1208 	DPRINTF(CDD_INTR, ("%s: tint: channel %d", sc->sc_dev.dv_xname,
1209 	    channel));
1210 	GOTINTR(sc, p);
1211 
1212 	/* if the current break condition is wrong, fix it */
1213 	if (p->p_break != p->p_needbreak) {
1214 		u_char buf[2];
1215 
1216 		DPRINTF(CDD_INTR, (", changing break to %d", p->p_needbreak));
1217 
1218 		/* turn on ETC processing */
1219 		cd18xx_write(sc, CD18xx_COR2, p->p_cor2 | CD18xx_COR2_ETC);
1220 
1221 		buf[0] = CD18xx_TDR_ETC_BYTE;
1222 		buf[1] = p->p_needbreak ? CD18xx_TDR_BREAK_BYTE :
1223 					    CD18xx_TDR_NOBREAK_BYTE;
1224 		cd18xx_write_multi(sc, CD18xx_TDR, buf, 2);
1225 
1226 		p->p_break = p->p_needbreak;
1227 
1228 		/* turn off ETC processing */
1229 		cd18xx_write(sc, CD18xx_COR2, p->p_cor2);
1230 	}
1231 
1232 	/*
1233 	 * If we've delayed a parameter change, do it now, and restart
1234 	 * output.
1235 	 */
1236 	if (p->p_heldchange) {
1237 		cdtty_loadchannelregs(sc, p);
1238 		p->p_heldchange = 0;
1239 		p->p_tbc = p->p_heldtbc;
1240 		p->p_heldtbc = 0;
1241 	}
1242 
1243 	/* Output the next chunk of the contiguous buffer, if any. */
1244 	if (p->p_tbc > 0) {
1245 		int n;
1246 
1247 		n = p->p_tbc;
1248 		if (n > 8) /* write up to 8 entries */
1249 			n = 8;
1250 		DPRINTF(CDD_INTR, (", writing %d bytes to TDR", n));
1251 		cd18xx_write_multi(sc, CD18xx_TDR, p->p_tba, n);
1252 		p->p_tbc -= n;
1253 		p->p_tba += n;
1254 	}
1255 
1256 	/* Disable transmit completion interrupts if we ran out of bytes. */
1257 	if (p->p_tbc == 0) {
1258 		/* Note that Tx interupts should already be enabled */
1259 		if (ISSET(p->p_srer, CD18xx_SRER_Tx)) {
1260 			DPRINTF(CDD_INTR, (", disabling tx interrupts"));
1261 			CLR(p->p_srer, CD18xx_SRER_Tx);
1262 			cd18xx_write(sc, CD18xx_SRER, p->p_srer);
1263 		}
1264 		if (p->p_tx_busy) {
1265 			p->p_tx_busy = 0;
1266 			p->p_tx_done = 1;
1267 		}
1268 	}
1269 	*ns = 1;
1270 
1271 	/* finish the interrupt transaction with the IC */
1272 	cd18xx_write(sc, CD18xx_EOSRR, 0);
1273 	DPRINTF(CDD_INTR, (", done\n"));
1274 }
1275 
1276 /* modem signal change interrupt */
1277 static __inline void
1278 cd18xx_mint(sc, ns)
1279 	struct cd18xx_softc *sc;
1280 	int *ns;
1281 {
1282 	struct cdtty_port *p;
1283 	u_int channel;
1284 	u_char msvr, delta;
1285 
1286 	/* work out the channel and softc */
1287 	channel = cd18xx_get_gscr1_channel(sc);
1288 	p = &sc->sc_ports[channel];
1289 	DPRINTF(CDD_INTR, ("%s: mint: channel %d", sc->sc_dev.dv_xname, channel));
1290 	GOTINTR(sc, p);
1291 
1292 	/*
1293 	 * We ignore the MCR register, and handle detecting deltas
1294 	 * via software, like many other serial drivers.
1295 	 */
1296 	msvr = cd18xx_read(sc, CD18xx_MSVR);
1297 	delta = msvr ^ p->p_msvr;
1298 	DPRINTF(CDD_INTR, (", msvr %d", msvr));
1299 
1300 	/*
1301 	 * Process normal status changes
1302 	 */
1303 	if (ISSET(delta, p->p_msvr_mask)) {
1304 		SET(p->p_msvr_delta, delta);
1305 
1306 		DPRINTF(CDD_INTR, (", status changed delta %d", delta));
1307 		/*
1308 		 * Stop output immediately if we lose the output
1309 		 * flow control signal or carrier detect.
1310 		 */
1311 		if (ISSET(~msvr, p->p_msvr_mask)) {
1312 			p->p_tbc = 0;
1313 			p->p_heldtbc = 0;
1314 			/* Stop modem interrupt processing */
1315 		}
1316 		p->p_st_check = 1;
1317 		*ns = 1;
1318 	}
1319 
1320 	/* reset the modem signal register */
1321 	cd18xx_write(sc, CD18xx_MCR, 0);
1322 
1323 	/* finish the interrupt transaction with the IC */
1324 	cd18xx_write(sc, CD18xx_EOSRR, 0);
1325 	DPRINTF(CDD_INTR, (", done\n"));
1326 }
1327 
1328 /*
1329  * hardware interrupt routine.  call the relevant interrupt routines until
1330  * no interrupts are pending.
1331  *
1332  * note:  we do receive interrupts before all others (as we'd rather lose
1333  * a chance to transmit, than lose a character).  and we do transmit
1334  * interrupts before modem interrupts.
1335  *
1336  * we have to traverse all of the cd18xx's attached, unfortunately.
1337  */
1338 int
1339 cd18xx_hardintr(v)
1340 	void *v;
1341 {
1342 	int i, rv = 0;
1343 	u_char ack;
1344 
1345 	DPRINTF(CDD_INTR, ("cd18xx_hardintr (ndevs %d):\n", clcd_cd.cd_ndevs));
1346 	for (i = 0; i < clcd_cd.cd_ndevs; i++)
1347 	{
1348 		struct cd18xx_softc *sc = clcd_cd.cd_devs[i];
1349 		int status, ns = 0;
1350 		int count = 1;	/* process only 1 interrupts at a time for now */
1351 
1352 		if (sc == NULL)
1353 			continue;
1354 
1355 		DPRINTF(CDD_INTR, ("%s:", sc->sc_dev.dv_xname));
1356 		while (count-- &&
1357 		    (status = (cd18xx_read(sc, CD18xx_SRSR) &
1358 		     CD18xx_SRSR_PENDING))) {
1359 			rv = 1;
1360 
1361 			DPRINTF(CDD_INTR, (" status %x:", status));
1362 			if (ISSET(status, CD18xx_SRSR_RxPEND)) {
1363 				ack = (*sc->sc_ackfunc)(sc->sc_ackfunc_arg,
1364 				    CD18xx_INTRACK_RxINT);
1365 				DPRINTF(CDD_INTR, (" rx: ack1 %x\n", ack));
1366 				cd18xx_rint(sc, &ns);
1367 			}
1368 			if (ISSET(status, CD18xx_SRSR_TxPEND)) {
1369 				ack = (*sc->sc_ackfunc)(sc->sc_ackfunc_arg,
1370 				    CD18xx_INTRACK_TxINT);
1371 				DPRINTF(CDD_INTR, (" tx: ack1 %x\n", ack));
1372 				cd18xx_tint(sc, &ns);
1373 
1374 			}
1375 			if (ISSET(status, CD18xx_SRSR_MxPEND)) {
1376 				ack = (*sc->sc_ackfunc)(sc->sc_ackfunc_arg,
1377 				    CD18xx_INTRACK_MxINT);
1378 				DPRINTF(CDD_INTR, (" mx: ack1 %x\n", ack));
1379 				cd18xx_mint(sc, &ns);
1380 			}
1381 		}
1382 		if (ns)
1383 			softintr_schedule(sc->sc_si);
1384 	}
1385 
1386 	return (rv);
1387 }
1388 
1389 /*
1390  * software interrupt
1391  */
1392 
1393 void
1394 cdtty_rxsoft(sc, p, tp)
1395 	struct cd18xx_softc *sc;
1396 	struct cdtty_port *p;
1397 	struct tty *tp;
1398 {
1399 	u_char *get, *end;
1400 	u_int cc, scc;
1401 	u_char rcsr;
1402 	int code;
1403 	int s;
1404 
1405 	end = p->p_ebuf;
1406 	get = p->p_rbget;
1407 	scc = cc = cdtty_rbuf_size - p->p_rbavail;
1408 
1409 	if (cc == cdtty_rbuf_size) {
1410 		p->p_floods++;
1411 #if 0
1412 		if (p->p_errors++ == 0)
1413 			callout_reset(&p->p_diag_callout, 60 * hz,
1414 			    cdttydiag, p);
1415 #endif
1416 	}
1417 
1418 	while (cc) {
1419 		code = get[0];
1420 		rcsr = get[1];
1421 		if (ISSET(rcsr, CD18xx_RCSR_OVERRUNERR | CD18xx_RCSR_BREAK |
1422 				CD18xx_RCSR_FRAMERR | CD18xx_RCSR_PARITYERR)) {
1423 			if (ISSET(rcsr, CD18xx_RCSR_OVERRUNERR)) {
1424 				p->p_overflows++;
1425 #if 0
1426 				if (p->p_errors++ == 0)
1427 					callout_reset(&p->p_diag_callout,
1428 					    60 * hz, cdttydiag, p);
1429 #endif
1430 			}
1431 			if (ISSET(rcsr, CD18xx_RCSR_BREAK|CD18xx_RCSR_FRAMERR))
1432 				SET(code, TTY_FE);
1433 			if (ISSET(rcsr, CD18xx_RCSR_PARITYERR))
1434 				SET(code, TTY_PE);
1435 		}
1436 		if ((*tp->t_linesw->l_rint)(code, tp) == -1) {
1437 			/*
1438 			 * The line discipline's buffer is out of space.
1439 			 */
1440 			if (!ISSET(p->p_rx_flags, RX_TTY_BLOCKED)) {
1441 				/*
1442 				 * We're either not using flow control, or the
1443 				 * line discipline didn't tell us to block for
1444 				 * some reason.  Either way, we have no way to
1445 				 * know when there's more space available, so
1446 				 * just drop the rest of the data.
1447 				 */
1448 				get += cc << 1;
1449 				if (get >= end)
1450 					get -= cdtty_rbuf_size << 1;
1451 				cc = 0;
1452 			} else {
1453 				/*
1454 				 * Don't schedule any more receive processing
1455 				 * until the line discipline tells us there's
1456 				 * space available (through cdttyhwiflow()).
1457 				 * Leave the rest of the data in the input
1458 				 * buffer.
1459 				 */
1460 				SET(p->p_rx_flags, RX_TTY_OVERFLOWED);
1461 			}
1462 			break;
1463 		}
1464 		get += 2;
1465 		if (get >= end)
1466 			get = p->p_rbuf;
1467 		cc--;
1468 	}
1469 
1470 	if (cc != scc) {
1471 		p->p_rbget = get;
1472 		s = splserial();
1473 
1474 		cc = p->p_rbavail += scc - cc;
1475 		/* Buffers should be ok again, release possible block. */
1476 		if (cc >= p->p_r_lowat) {
1477 			if (ISSET(p->p_rx_flags, RX_IBUF_OVERFLOWED)) {
1478 				CLR(p->p_rx_flags, RX_IBUF_OVERFLOWED);
1479 				cd18xx_set_car(sc, CD18XX_CHANNEL(tp->t_dev));
1480 				SET(p->p_srer, CD18xx_SRER_Rx |
1481 					       CD18xx_SRER_RxSC |
1482 					       CD18xx_SRER_CD);
1483 				cd18xx_write(sc, CD18xx_SRER, p->p_srer);
1484 			}
1485 			if (ISSET(p->p_rx_flags, RX_IBUF_BLOCKED)) {
1486 				CLR(p->p_rx_flags, RX_IBUF_BLOCKED);
1487 				cdtty_hwiflow(sc, p);
1488 			}
1489 		}
1490 		splx(s);
1491 	}
1492 }
1493 
1494 void
1495 cdtty_txsoft(sc, p, tp)
1496 	struct cd18xx_softc *sc;
1497 	struct cdtty_port *p;
1498 	struct tty *tp;
1499 {
1500 
1501 	CLR(tp->t_state, TS_BUSY);
1502 	if (ISSET(tp->t_state, TS_FLUSH))
1503 		CLR(tp->t_state, TS_FLUSH);
1504 	else
1505 		ndflush(&tp->t_outq, (int)(p->p_tba - tp->t_outq.c_cf));
1506 	(*tp->t_linesw->l_start)(tp);
1507 }
1508 
1509 void
1510 cdtty_stsoft(sc, p, tp)
1511 	struct cd18xx_softc *sc;
1512 	struct cdtty_port *p;
1513 	struct tty *tp;
1514 {
1515 	u_char msvr, delta;
1516 	int s;
1517 
1518 	s = splserial();
1519 	msvr = p->p_msvr;
1520 	delta = p->p_msvr_delta;
1521 	p->p_msvr_delta = 0;
1522 	splx(s);
1523 
1524 	if (ISSET(delta, p->p_msvr_dcd)) {
1525 		/*
1526 		 * Inform the tty layer that carrier detect changed.
1527 		 */
1528 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(msvr, CD18xx_MSVR_CD));
1529 	}
1530 
1531 	if (ISSET(delta, p->p_msvr_cts)) {
1532 		/* Block or unblock output according to flow control. */
1533 		if (ISSET(msvr, p->p_msvr_cts)) {
1534 			p->p_tx_stopped = 0;
1535 			(*tp->t_linesw->l_start)(tp);
1536 		} else {
1537 			p->p_tx_stopped = 1;
1538 		}
1539 	}
1540 }
1541 
1542 void
1543 cd18xx_softintr(v)
1544 	void *v;
1545 {
1546 	struct cd18xx_softc *sc = v;
1547 	struct cdtty_port *p;
1548 	struct tty *tp;
1549 	int i;
1550 
1551 	for (i = 0; i < 8; i++) {
1552 		p = &sc->sc_ports[i];
1553 
1554 		tp = p->p_tty;
1555 		if (tp == NULL)
1556 			continue;
1557 		if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
1558 			continue;
1559 
1560 		if (p->p_rx_ready) {
1561 			p->p_rx_ready = 0;
1562 			cdtty_rxsoft(sc, p, tp);
1563 		}
1564 
1565 		if (p->p_st_check) {
1566 			p->p_st_check = 0;
1567 			cdtty_stsoft(sc, p, tp);
1568 		}
1569 
1570 		if (p->p_tx_done) {
1571 			p->p_tx_done = 0;
1572 			cdtty_txsoft(sc, p, tp);
1573 		}
1574 	}
1575 }
1576