xref: /netbsd-src/sys/dev/ic/cy.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: cy.c,v 1.15 2000/07/08 18:36:02 sommerfeld Exp $	*/
2 
3 /*
4  * cy.c
5  *
6  * Driver for Cyclades Cyclom-8/16/32 multiport serial cards
7  * (currently not tested with Cyclom-32 cards)
8  *
9  * Timo Rossi, 1996
10  *
11  * Supports both ISA and PCI Cyclom cards
12  *
13  * Lots of debug output can be enabled by defining CY_DEBUG
14  * Some debugging counters (number of receive/transmit interrupts etc.)
15  * can be enabled by defining CY_DEBUG1
16  */
17 
18 #include <sys/types.h>
19 #include <sys/param.h>
20 #include <sys/ioctl.h>
21 #include <sys/syslog.h>
22 #include <sys/fcntl.h>
23 #include <sys/tty.h>
24 #include <sys/proc.h>
25 #include <sys/conf.h>
26 #include <sys/user.h>
27 #include <sys/ioctl.h>
28 #include <sys/select.h>
29 #include <sys/device.h>
30 #include <sys/malloc.h>
31 #include <sys/systm.h>
32 #include <sys/callout.h>
33 
34 #include <machine/bus.h>
35 
36 #include <dev/ic/cd1400reg.h>
37 #include <dev/ic/cyreg.h>
38 #include <dev/ic/cyvar.h>
39 
40 /* Macros to clear/set/test flags. */
41 #define	SET(t, f)	(t) |= (f)
42 #define	CLR(t, f)	(t) &= ~(f)
43 #define	ISSET(t, f)	((t) & (f))
44 
45 static int cyparam __P((struct tty *, struct termios *));
46 static void cystart __P((struct tty *));
47 static void cy_poll __P((void *));
48 static int cy_modem_control __P((struct cy_softc *,
49     struct cy_port *, int, int));
50 static void cy_enable_transmitter __P((struct cy_softc *, struct cy_port *));
51 static void cd1400_channel_cmd __P((struct cy_softc *, struct cy_port *, int));
52 static int cy_speed __P((speed_t, int *, int *, int));
53 
54 extern struct cfdriver cy_cd;
55 
56 static int      cy_open = 0;
57 static int      cy_events = 0;
58 
59 cdev_decl(cy);
60 
61 struct callout cy_poll_callout = CALLOUT_INITIALIZER;
62 
63 /*
64  * Common probe routine
65  */
66 int
67 cy_find(sc)
68 	struct cy_softc *sc;
69 {
70 	int cy_chip, chip;
71 	u_char firmware_ver;
72 	bus_space_tag_t tag = sc->sc_memt;
73 	bus_space_handle_t bsh = sc->sc_bsh;
74 	int bustype = sc->sc_bustype;
75 
76 	/* Cyclom card hardware reset */
77 	bus_space_write_1(tag, bsh, CY16_RESET << bustype, 0);
78 	DELAY(500);		/* wait for reset to complete */
79 	bus_space_write_1(tag, bsh, CY_CLEAR_INTR << bustype, 0);
80 
81 #ifdef CY_DEBUG
82 	printf("cy: card reset done\n");
83 #endif
84 	sc->sc_nchips = 0;
85 
86 	for (cy_chip = 0, chip = 0; cy_chip < CY_MAX_CD1400s;
87 	    cy_chip++, chip += (CY_CD1400_MEMSPACING << bustype)) {
88 		int i;
89 
90 		/*
91 		 * the last 4 nchips are 'interleaved' with the first 4 on
92 		 * 32-port boards
93 		 */
94 		if (cy_chip == 4)
95 			chip -= (CY32_ADDR_FIX << bustype);
96 
97 #ifdef CY_DEBUG
98 		printf("%s probe chip %d offset 0x%x ... ",
99 		    sc->sc_dev.dv_xname, cy_chip, chip);
100 #endif
101 
102 		/* wait until the chip is ready for command */
103 		DELAY(1000);
104 		if (bus_space_read_1(tag, bsh, chip +
105 		    ((CD1400_CCR << 1) << bustype)) != 0) {
106 #ifdef CY_DEBUG
107 			printf("not ready for command\n");
108 #endif
109 			break;
110 		}
111 		/* clear the firmware version reg. */
112 		bus_space_write_1(tag, bsh, chip +
113 		    ((CD1400_GFRCR << 1) << bustype), 0);
114 
115 		/*
116 	         * On Cyclom-16 references to non-existent chip 4
117 	         * actually access chip 0 (address line 9 not decoded).
118 	         * Here we check if the clearing of chip 4 GFRCR actually
119 	         * cleared chip 0 GFRCR. In that case we have a 16 port card.
120 	         */
121 		if (cy_chip == 4 &&
122 		    bus_space_read_1(tag, bsh, /* off for chip 0 (0) + */
123 		       ((CD1400_GFRCR << 1) << bustype)) == 0)
124 			break;
125 
126 		/* reset the chip */
127 		bus_space_write_1(tag, bsh, chip +
128 		    ((CD1400_CCR << 1) << bustype),
129 		    CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
130 
131 		/* wait for the chip to initialize itself */
132 		for (i = 0; i < 200; i++) {
133 			DELAY(50);
134 			firmware_ver = bus_space_read_1(tag, bsh, chip +
135 			    ((CD1400_GFRCR << 1) << bustype));
136 			if ((firmware_ver & 0xf0) == 0x40) /* found a CD1400 */
137 				break;
138 		}
139 #ifdef CY_DEBUG
140 		printf("firmware version 0x%x\n", firmware_ver);
141 #endif
142 
143 		if ((firmware_ver & 0xf0) != 0x40)
144 			break;
145 
146 		/* firmware version OK, CD1400 found */
147 		sc->sc_nchips++;
148 	}
149 
150 	if (sc->sc_nchips == 0) {
151 #ifdef CY_DEBUG
152 		printf("no CD1400s found\n");
153 #endif
154 		return 0;
155 	}
156 #ifdef CY_DEBUG
157 	printf("found %d CD1400s\n", sc->sc_nchips);
158 #endif
159 
160 	return 1;
161 }
162 
163 void
164 cy_attach(parent, self, aux)
165 	struct device  *parent, *self;
166 	void *aux;
167 {
168 	int  port, cy_chip, num_chips, cdu, chip;
169 	struct cy_softc *sc = (void *) self;
170 	int cy_clock;
171 
172 	num_chips = sc->sc_nchips;
173 	if (num_chips == 0)
174 		return;
175 
176 	bzero(sc->sc_ports, sizeof(sc->sc_ports));
177 
178 	port = 0;
179 	for (cy_chip = 0, chip = 0; cy_chip < num_chips; cy_chip++,
180 	    chip += (CY_CD1400_MEMSPACING << sc->sc_bustype)) {
181 
182 		if (cy_chip == 4)
183 			chip -= (CY32_ADDR_FIX << sc->sc_bustype);
184 
185 #ifdef CY_DEBUG
186 		printf("attach CD1400 #%d offset 0x%x\n", cy_chip, chip);
187 #endif
188 		sc->sc_cd1400_offs[cy_chip] = chip;
189 
190 		/*
191 		 * configure port 0 as serial port (should already be after
192 		 * reset)
193 		 */
194 		cd_write_reg(sc, cy_chip, CD1400_GCR, 0);
195 
196 		if (cd_read_reg(sc, cy_chip, CD1400_GFRCR) <= 0x46)
197 			cy_clock = CY_CLOCK;
198 		else
199 			cy_clock = CY_CLOCK_60;
200 
201 		/* set up a receive timeout period (1ms) */
202 		cd_write_reg(sc, cy_chip, CD1400_PPR,
203 		    (cy_clock / CD1400_PPR_PRESCALER / 1000) + 1);
204 
205 		for (cdu = 0; cdu < CD1400_NO_OF_CHANNELS; cdu++) {
206 			sc->sc_ports[port].cy_port_num = port;
207 			sc->sc_ports[port].cy_chip = cy_chip;
208 			sc->sc_ports[port].cy_clock = cy_clock;
209 
210 			/* should we initialize anything else here? */
211 			port++;
212 		} /* for(each port on one CD1400...) */
213 
214 	} /* for(each CD1400 on a card... ) */
215 
216 	printf(": %d ports\n", port);
217 
218 	/* ensure an edge for the next interrupt */
219 	bus_space_write_1(sc->sc_memt, sc->sc_bsh,
220 	    CY_CLEAR_INTR << sc->sc_bustype, 0);
221 }
222 
223 /*
224  * open routine. returns zero if successfull, else error code
225  */
226 int
227 cyopen(dev, flag, mode, p)
228 	dev_t dev;
229 	int flag, mode;
230 	struct proc *p;
231 {
232 	int port = CY_PORT(dev);
233 	struct cy_softc *sc;
234 	struct cy_port *cy;
235 	struct tty *tp;
236 	int s, error;
237 
238 #ifdef CY_DEBUG
239 	printf("cy%d open port %d flag 0x%x mode 0x%x\n",
240 	    card, port, flag, mode);
241 #endif
242 
243 	sc = device_lookup(&cy_cd, CY_CARD(dev));
244 	if (sc == NULL)
245 		return (ENXIO);
246 	cy = &sc->sc_ports[port];
247 
248 	s = spltty();
249 	if (cy->cy_tty == NULL) {
250 		if ((cy->cy_tty = ttymalloc()) == NULL) {
251 			splx(s);
252 			printf("%s: port %d: can't allocate tty\n",
253 			    sc->sc_dev.dv_xname, port);
254 			return ENOMEM;
255 		}
256 		tty_attach(cy->cy_tty);
257 	}
258 	splx(s);
259 
260 	tp = cy->cy_tty;
261 	tp->t_oproc = cystart;
262 	tp->t_param = cyparam;
263 	tp->t_dev = dev;
264 
265 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
266 		ttychars(tp);
267 		tp->t_iflag = TTYDEF_IFLAG;
268 		tp->t_oflag = TTYDEF_OFLAG;
269 		tp->t_cflag = TTYDEF_CFLAG;
270 		if (ISSET(cy->cy_openflags, TIOCFLAG_CLOCAL))
271 			SET(tp->t_cflag, CLOCAL);
272 		if (ISSET(cy->cy_openflags, TIOCFLAG_CRTSCTS))
273 			SET(tp->t_cflag, CRTSCTS);
274 		if (ISSET(cy->cy_openflags, TIOCFLAG_MDMBUF))
275 			SET(tp->t_cflag, MDMBUF);
276 		tp->t_lflag = TTYDEF_LFLAG;
277 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
278 
279 		s = spltty();
280 
281 		/*
282 		 * Allocate input ring buffer if we don't already have one
283 		 */
284 		if (cy->cy_ibuf == NULL) {
285 			cy->cy_ibuf = malloc(CY_IBUF_SIZE, M_DEVBUF, M_NOWAIT);
286 			if (cy->cy_ibuf == NULL) {
287 				printf("%s: port %d: can't allocate input buffer\n",
288 				       sc->sc_dev.dv_xname, port);
289 				splx(s);
290 				return ENOMEM;
291 			}
292 			cy->cy_ibuf_end = cy->cy_ibuf + CY_IBUF_SIZE;
293 		}
294 		/* mark the ring buffer as empty */
295 		cy->cy_ibuf_rd_ptr = cy->cy_ibuf_wr_ptr = cy->cy_ibuf;
296 
297 		/* select CD1400 channel */
298 		cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
299 		    port & CD1400_CAR_CHAN);
300 		/* reset the channel */
301 		cd1400_channel_cmd(sc, cy, CD1400_CCR_CMDRESET);
302 		/* encode unit (port) number in LIVR */
303 		/* there is just enough space for 5 bits (32 ports) */
304 		cd_write_reg(sc, cy->cy_chip, CD1400_LIVR, port << 3);
305 
306 		cy->cy_channel_control = 0;
307 
308 		/* hmm... need spltty() here? */
309 		if (cy_open == 0) {
310 			cy_open = 1;
311 			callout_reset(&cy_poll_callout, 1, cy_poll, NULL);
312 		}
313 		/* this sets parameters and raises DTR */
314 		cyparam(tp, &tp->t_termios);
315 
316 		ttsetwater(tp);
317 
318 		/* raise RTS too */
319 		cy_modem_control(sc, cy, TIOCM_RTS, DMBIS);
320 
321 		cy->cy_carrier_stat =
322 			cd_read_reg(sc, cy->cy_chip, CD1400_MSVR2);
323 
324 		/* enable receiver and modem change interrupts */
325 		cd_write_reg(sc, cy->cy_chip, CD1400_SRER,
326 		    CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
327 
328 		if (CY_DIALOUT(dev) ||
329 		    ISSET(cy->cy_openflags, TIOCFLAG_SOFTCAR) ||
330 		    ISSET(tp->t_cflag, MDMBUF) ||
331 		    ISSET(cy->cy_carrier_stat, CD1400_MSVR2_CD))
332 			SET(tp->t_state, TS_CARR_ON);
333 		else
334 			CLR(tp->t_state, TS_CARR_ON);
335 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0) {
336 		return EBUSY;
337 	} else {
338 		s = spltty();
339 	}
340 
341 	/* wait for carrier if necessary */
342 	if (!ISSET(flag, O_NONBLOCK)) {
343 		while (!ISSET(tp->t_cflag, CLOCAL) &&
344 		    !ISSET(tp->t_state, TS_CARR_ON)) {
345 			tp->t_wopen++;
346 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
347 			    "cydcd", 0);
348 			tp->t_wopen--;
349 			if (error != 0) {
350 				splx(s);
351 				return error;
352 			}
353 		}
354 	}
355 	splx(s);
356 
357 	return (*linesw[tp->t_line].l_open) (dev, tp);
358 }
359 
360 /*
361  * close routine. returns zero if successfull, else error code
362  */
363 int
364 cyclose(dev, flag, mode, p)
365 	dev_t dev;
366 	int flag, mode;
367 	struct proc *p;
368 {
369 	int port = CY_PORT(dev);
370 	struct cy_softc *sc = device_lookup(&cy_cd, CY_CARD(dev));
371 	struct cy_port *cy = &sc->sc_ports[port];
372 	struct tty *tp = cy->cy_tty;
373 	int s;
374 
375 #ifdef CY_DEBUG
376 	printf("%s: close port %d, flag 0x%x, mode 0x%x\n",
377 	    sc->sc_dev.dv_xname, port, flag, mode);
378 #endif
379 
380 	(*linesw[tp->t_line].l_close) (tp, flag);
381 	s = spltty();
382 
383 	if (ISSET(tp->t_cflag, HUPCL) &&
384 	    !ISSET(cy->cy_openflags, TIOCFLAG_SOFTCAR)) {
385 		/*
386 		 * drop DTR and RTS (should we wait for output buffer to
387 		 * become empty first?)
388 		 */
389 		cy_modem_control(sc, cy, 0, DMSET);
390 	}
391 	/*
392 	 * XXX should we disable modem change and
393 	 * receive interrupts here or somewhere ?
394 	 */
395 	CLR(tp->t_state, TS_BUSY | TS_FLUSH);
396 
397 	splx(s);
398 	ttyclose(tp);
399 
400 	return 0;
401 }
402 
403 /*
404  * Read routine
405  */
406 int
407 cyread(dev, uio, flag)
408 	dev_t dev;
409 	struct uio *uio;
410 	int flag;
411 {
412 	int port = CY_PORT(dev);
413 	struct cy_softc *sc = device_lookup(&cy_cd, CY_CARD(dev));
414 	struct cy_port *cy = &sc->sc_ports[port];
415 	struct tty *tp = cy->cy_tty;
416 
417 #ifdef CY_DEBUG
418 	printf("%s: read port %d uio %p flag 0x%x\n",
419 	    sc->sc_dev.dv_xname, port, uio, flag);
420 #endif
421 
422 	return ((*linesw[tp->t_line].l_read) (tp, uio, flag));
423 }
424 
425 /*
426  * Write routine
427  */
428 int
429 cywrite(dev, uio, flag)
430 	dev_t dev;
431 	struct uio *uio;
432 	int flag;
433 {
434 	int port = CY_PORT(dev);
435 	struct cy_softc *sc = device_lookup(&cy_cd, CY_CARD(dev));
436 	struct cy_port *cy = &sc->sc_ports[port];
437 	struct tty *tp = cy->cy_tty;
438 
439 #ifdef CY_DEBUG
440 	printf("%s: write port %d uio %p flag 0x%x\n",
441 	    sc->sc_dev.dv_xname, port, uio, flag);
442 #endif
443 
444 	return ((*linesw[tp->t_line].l_write) (tp, uio, flag));
445 }
446 
447 /*
448  * return tty pointer
449  */
450 struct tty *
451 cytty(dev)
452 	dev_t dev;
453 {
454 	int port = CY_PORT(dev);
455 	struct cy_softc *sc = device_lookup(&cy_cd, CY_CARD(dev));
456 	struct cy_port *cy = &sc->sc_ports[port];
457 	struct tty *tp = cy->cy_tty;
458 
459 #ifdef CY_DEBUG
460 	printf("%s: tty port %d tp %p\n", sc->sc_dev.dv_xname, port, tp);
461 #endif
462 	return tp;
463 }
464 
465 /*
466  * ioctl routine
467  */
468 int
469 cyioctl(dev, cmd, data, flag, p)
470 	dev_t dev;
471 	u_long cmd;
472 	caddr_t data;
473 	int flag;
474 	struct proc *p;
475 {
476 	int port = CY_PORT(dev);
477 	struct cy_softc *sc = device_lookup(&cy_cd, CY_CARD(dev));
478 	struct cy_port *cy = &sc->sc_ports[port];
479 	struct tty *tp = cy->cy_tty;
480 	int error;
481 
482 #ifdef CY_DEBUG
483 	printf("%s: port %d ioctl cmd 0x%lx data %p flag 0x%x\n",
484 	    sc->sc_dev.dv_xname, port, cmd, data, flag);
485 #endif
486 
487 	error = (*linesw[tp->t_line].l_ioctl) (tp, cmd, data, flag, p);
488 	if (error >= 0)
489 		return error;
490 
491 	error = ttioctl(tp, cmd, data, flag, p);
492 	if (error >= 0)
493 		return error;
494 
495 	/* XXX should not allow dropping DTR when dialin? */
496 
497 	switch (cmd) {
498 	case TIOCSBRK:		/* start break */
499 		SET(cy->cy_flags, CY_F_START_BREAK);
500 		cy_enable_transmitter(sc, cy);
501 		break;
502 
503 	case TIOCCBRK:		/* stop break */
504 		SET(cy->cy_flags, CY_F_END_BREAK);
505 		cy_enable_transmitter(sc, cy);
506 		break;
507 
508 	case TIOCSDTR:		/* DTR on */
509 		cy_modem_control(sc, cy, TIOCM_DTR, DMBIS);
510 		break;
511 
512 	case TIOCCDTR:		/* DTR off */
513 		cy_modem_control(sc, cy, TIOCM_DTR, DMBIC);
514 		break;
515 
516 	case TIOCMSET:		/* set new modem control line values */
517 		cy_modem_control(sc, cy, *((int *) data), DMSET);
518 		break;
519 
520 	case TIOCMBIS:		/* turn modem control bits on */
521 		cy_modem_control(sc, cy, *((int *) data), DMBIS);
522 		break;
523 
524 	case TIOCMBIC:		/* turn modem control bits off */
525 		cy_modem_control(sc, cy, *((int *) data), DMBIC);
526 		break;
527 
528 	case TIOCMGET:		/* get modem control/status line state */
529 		*((int *) data) = cy_modem_control(sc, cy, 0, DMGET);
530 		break;
531 
532 	case TIOCGFLAGS:
533 		*((int *) data) = cy->cy_openflags |
534 			(CY_DIALOUT(dev) ? TIOCFLAG_SOFTCAR : 0);
535 		break;
536 
537 	case TIOCSFLAGS:
538 		error = suser(p->p_ucred, &p->p_acflag);
539 		if (error != 0)
540 			return EPERM;
541 
542 		cy->cy_openflags = *((int *) data) &
543 		    (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
544 		     TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
545 		break;
546 
547 	default:
548 		return ENOTTY;
549 	}
550 
551 	return 0;
552 }
553 
554 /*
555  * start output
556  */
557 void
558 cystart(tp)
559 	struct tty *tp;
560 {
561 	int port = CY_PORT(tp->t_dev);
562 	struct cy_softc *sc = device_lookup(&cy_cd, CY_CARD(tp->t_dev));
563 	struct cy_port *cy = &sc->sc_ports[port];
564 	int s;
565 
566 #ifdef CY_DEBUG
567 	printf("%s: port %d start, tty %p\n", sc->sc_dev.dv_xname, port, tp);
568 #endif
569 
570 
571 	s = spltty();
572 
573 #ifdef CY_DEBUG1
574 	cy->cy_start_count++;
575 #endif
576 
577 	if (!ISSET(tp->t_state, TS_TTSTOP | TS_TIMEOUT | TS_BUSY)) {
578 		if (tp->t_outq.c_cc <= tp->t_lowat) {
579 			if (ISSET(tp->t_state, TS_ASLEEP)) {
580 				CLR(tp->t_state, TS_ASLEEP);
581 				wakeup(&tp->t_outq);
582 			}
583 			selwakeup(&tp->t_wsel);
584 
585 			if (tp->t_outq.c_cc == 0)
586 				goto out;
587 		}
588 		SET(tp->t_state, TS_BUSY);
589 		cy_enable_transmitter(sc, cy);
590 	}
591 out:
592 
593 	splx(s);
594 }
595 
596 /*
597  * stop output
598  */
599 void
600 cystop(tp, flag)
601 	struct tty *tp;
602 	int flag;
603 {
604 	int port = CY_PORT(tp->t_dev);
605 	struct cy_softc *sc = device_lookup(&cy_cd, CY_CARD(tp->t_dev));
606 	struct cy_port *cy = &sc->sc_ports[port];
607 	int s;
608 
609 #ifdef CY_DEBUG
610 	printf("%s: port %d stop tty %p flag 0x%x\n",
611 	    sc->sc_dev.dv_xname, port, tp, flag);
612 #endif
613 
614 	s = spltty();
615 
616 	if (ISSET(tp->t_state, TS_BUSY)) {
617 		if (!ISSET(tp->t_state, TS_TTSTOP))
618 			SET(tp->t_state, TS_FLUSH);
619 
620 		/*
621 		 * the transmit interrupt routine will disable transmit when it
622 		 * notices that CY_F_STOP has been set.
623 		 */
624 		SET(cy->cy_flags, CY_F_STOP);
625 	}
626 	splx(s);
627 }
628 
629 /*
630  * parameter setting routine.
631  * returns 0 if successfull, else returns error code
632  */
633 static int
634 cyparam(tp, t)
635 	struct tty *tp;
636 	struct termios *t;
637 {
638 	int port = CY_PORT(tp->t_dev);
639 	struct cy_softc *sc = device_lookup(&cy_cd, CY_CARD(tp->t_dev));
640 	struct cy_port *cy = &sc->sc_ports[port];
641 	int ibpr, obpr, i_clk_opt, o_clk_opt;
642 	int s, opt;
643 
644 #ifdef CY_DEBUG
645 	printf("%s: port %d param tty %p termios %p\n",
646 	    sc->sc_dev.dv_xname, port, tp, t);
647 	printf("ispeed %d ospeed %d\n", t->c_ispeed, t->c_ospeed);
648 #endif
649 
650 	if (t->c_ospeed != 0 && cy_speed(t->c_ospeed, &o_clk_opt, &obpr, cy->cy_clock) < 0)
651 		return EINVAL;
652 
653 	if (t->c_ispeed != 0 && cy_speed(t->c_ispeed, &i_clk_opt, &ibpr, cy->cy_clock) < 0)
654 		return EINVAL;
655 
656 	s = spltty();
657 
658 	/* hang up the line is ospeed is zero, else turn DTR on */
659 	cy_modem_control(sc, cy, TIOCM_DTR, (t->c_ospeed == 0 ? DMBIC : DMBIS));
660 
661 	/* channel was selected by the above call to cy_modem_control() */
662 #if 0
663 	cd_write_reg(sc, cy->cy_chip, CD1400_CAR, port & CD1400_CAR_CHAN);
664 #endif
665 
666 	/* set transmit speed */
667 	if (t->c_ospeed != 0) {
668 		cd_write_reg(sc, cy->cy_chip, CD1400_TCOR, o_clk_opt);
669 		cd_write_reg(sc, cy->cy_chip, CD1400_TBPR, obpr);
670 	}
671 	/* set receive speed */
672 	if (t->c_ispeed != 0) {
673 		cd_write_reg(sc, cy->cy_chip, CD1400_RCOR, i_clk_opt);
674 		cd_write_reg(sc, cy->cy_chip, CD1400_RBPR, ibpr);
675 	}
676 	opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN
677 		| (ISSET(t->c_cflag, CREAD) ? CD1400_CCR_RCVEN : CD1400_CCR_RCVDIS);
678 
679 	if (opt != cy->cy_channel_control) {
680 		cy->cy_channel_control = opt;
681 		cd1400_channel_cmd(sc, cy, opt);
682 	}
683 	/* compute COR1 contents */
684 	opt = 0;
685 	if (ISSET(t->c_cflag, PARENB)) {
686 		if (ISSET(t->c_cflag, PARODD))
687 			opt |= CD1400_COR1_PARODD;
688 		opt |= CD1400_COR1_PARNORMAL;
689 	}
690 	if (!ISSET(t->c_iflag, INPCK))
691 		opt |= CD1400_COR1_NOINPCK;	/* no parity checking */
692 
693 	if (ISSET(t->c_cflag, CSTOPB))
694 		opt |= CD1400_COR1_STOP2;
695 
696 	switch (t->c_cflag & CSIZE) {
697 	case CS5:
698 		opt |= CD1400_COR1_CS5;
699 		break;
700 
701 	case CS6:
702 		opt |= CD1400_COR1_CS6;
703 		break;
704 
705 	case CS7:
706 		opt |= CD1400_COR1_CS7;
707 		break;
708 
709 	default:
710 		opt |= CD1400_COR1_CS8;
711 		break;
712 	}
713 
714 	cd_write_reg(sc, cy->cy_chip, CD1400_COR1, opt);
715 
716 #ifdef CY_DEBUG
717 	printf("cor1 = 0x%x...", opt);
718 #endif
719 
720 	/*
721 	 * use the CD1400 automatic CTS flow control if CRTSCTS is set
722 	 *
723 	 * CD1400_COR2_ETC is used because breaks are generated with
724 	 * embedded transmit commands
725 	 */
726 	cd_write_reg(sc, cy->cy_chip, CD1400_COR2,
727 		     CD1400_COR2_ETC |
728 		 (ISSET(t->c_cflag, CRTSCTS) ? CD1400_COR2_CCTS_OFLOW : 0));
729 
730 	cd_write_reg(sc, cy->cy_chip, CD1400_COR3, CY_RX_FIFO_THRESHOLD);
731 
732 	cd1400_channel_cmd(sc, cy, CD1400_CCR_CMDCORCHG |
733 	    CD1400_CCR_COR1 | CD1400_CCR_COR2 | CD1400_CCR_COR3);
734 
735 	cd_write_reg(sc, cy->cy_chip, CD1400_COR4, CD1400_COR4_PFO_EXCEPTION);
736 	cd_write_reg(sc, cy->cy_chip, CD1400_COR5, 0);
737 
738 	/*
739          * set modem change option registers to generate interrupts
740          * on carrier detect changes.
741          *
742          * if hardware RTS handshaking is used
743          * also set the handshaking threshold.
744          */
745 	if (cy->cy_clock == CY_CLOCK_60) {
746 	   cd_write_reg(sc, cy->cy_chip, CD1400_MCOR1, CD1400_MCOR1_CDzd |
747     	      (ISSET(t->c_cflag, CRTSCTS) ? CY_RX_DTR_THRESHOLD : 0));
748 	} else {
749 	   cd_write_reg(sc, cy->cy_chip, CD1400_MCOR1, CD1400_MCOR1_CDzd);
750 	}
751 
752 	cd_write_reg(sc, cy->cy_chip, CD1400_MCOR2, CD1400_MCOR2_CDod);
753 
754 	/*
755          * set receive timeout to approx. 2ms
756          * could use more complex logic here...
757          * (but is it actually needed or even useful?)
758          */
759 	cd_write_reg(sc, cy->cy_chip, CD1400_RTPR, 2);
760 
761 	/*
762          * should do anything else here?
763          * XXX check MDMBUF handshaking like in com.c?
764          */
765 
766 	splx(s);
767 	return 0;
768 }
769 
770 /*
771  * set/get modem line status
772  *
773  * bits can be: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, TIOCM_CD, TIOCM_RI, TIOCM_DSR
774  *
775  */
776 static int
777 cy_modem_control(sc, cy, bits, howto)
778 	struct cy_softc *sc;
779 	struct cy_port *cy;
780 	int bits;
781 	int howto;
782 {
783 	int s, msvr;
784 	struct tty *tp = cy->cy_tty;
785 
786 	s = spltty();
787 
788 	/* select channel */
789 	cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
790 	    cy->cy_port_num & CD1400_CAR_CHAN);
791 
792 	/* does not manipulate RTS if it is used for flow control */
793 	switch (howto) {
794 	case DMGET:
795 		splx(s);
796 		bits = 0;
797 		if (cy->cy_channel_control & CD1400_CCR_RCVEN)
798 			bits |= TIOCM_LE;
799 		msvr = cd_read_reg(sc, cy->cy_chip, CD1400_MSVR2);
800 		if (cy->cy_clock == CY_CLOCK_60) {
801 			if (cd_read_reg(sc, cy->cy_chip, CD1400_MSVR1) &
802 		    		CD1400_MSVR1_RTS)
803 				bits |= TIOCM_DTR;
804 			if (msvr & CD1400_MSVR2_DTR)
805 				bits |= TIOCM_RTS;
806 		} else {
807 			if (cd_read_reg(sc, cy->cy_chip, CD1400_MSVR1) &
808 			    CD1400_MSVR1_RTS)
809 				bits |= TIOCM_RTS;
810 			if (msvr & CD1400_MSVR2_DTR)
811 				bits |= TIOCM_DTR;
812 		}
813 		if (msvr & CD1400_MSVR2_CTS)
814 			bits |= TIOCM_CTS;
815 		if (msvr & CD1400_MSVR2_CD)
816 			bits |= TIOCM_CD;
817 		if (msvr & CD1400_MSVR2_DSR)	/* not connected on some
818 						 * Cyclom cards? */
819 			bits |= TIOCM_DSR;
820 		if (msvr & CD1400_MSVR2_RI)	/* not connected on Cyclom-8Y
821 						 * cards? */
822 			bits |= TIOCM_RI;
823 		splx(s);
824 		return bits;
825 
826 	case DMSET:		/* replace old values with new ones */
827 		if (cy->cy_clock == CY_CLOCK_60) {
828 			if (!ISSET(tp->t_cflag, CRTSCTS))
829 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
830 				   ((bits & TIOCM_RTS) ? CD1400_MSVR2_DTR : 0));
831 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
832 			    ((bits & TIOCM_DTR) ? CD1400_MSVR1_RTS : 0));
833 		} else {
834 			if (!ISSET(tp->t_cflag, CRTSCTS))
835 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
836 				   ((bits & TIOCM_RTS) ? CD1400_MSVR1_RTS : 0));
837 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
838 			    ((bits & TIOCM_DTR) ? CD1400_MSVR2_DTR : 0));
839 		}
840 		break;
841 
842 	case DMBIS:		/* set bits */
843 		if (cy->cy_clock == CY_CLOCK_60) {
844 			if (!ISSET(tp->t_cflag, CRTSCTS) && (bits & TIOCM_RTS) != 0)
845 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
846 				    CD1400_MSVR2_DTR);
847 			if (bits & TIOCM_DTR)
848 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
849 				    CD1400_MSVR1_RTS);
850 		} else {
851 			if (!ISSET(tp->t_cflag, CRTSCTS) && (bits & TIOCM_RTS) != 0)
852 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
853 				    CD1400_MSVR1_RTS);
854 			if (bits & TIOCM_DTR)
855 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
856 				    CD1400_MSVR2_DTR);
857 		}
858 		break;
859 
860 	case DMBIC:		/* clear bits */
861 		if (cy->cy_clock == CY_CLOCK_60) {
862 			if (!ISSET(tp->t_cflag, CRTSCTS) && (bits & TIOCM_RTS))
863 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2, 0);
864 			if (bits & TIOCM_DTR)
865 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1, 0);
866 		} else {
867 			if (!ISSET(tp->t_cflag, CRTSCTS) && (bits & TIOCM_RTS))
868 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1, 0);
869 			if (bits & TIOCM_DTR)
870 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2, 0);
871 		}
872 		break;
873 	}
874 	splx(s);
875 	return 0;
876 }
877 
878 /*
879  * Upper-level handler loop (called from timer interrupt?)
880  * This routine is common for multiple cards
881  */
882 static void
883 cy_poll(arg)
884 	void *arg;
885 {
886 	int card, port;
887 	struct cy_softc *sc;
888 	struct cy_port *cy;
889 	struct tty *tp;
890 	static int counter = 0;
891 #ifdef CY_DEBUG1
892 	int did_something;
893 #endif
894 	int s = spltty();
895 
896 	if (cy_events == 0 && ++counter < 200) {
897 		splx(s);
898 		goto out;
899 	}
900 	cy_events = 0;
901 	splx(s);
902 
903 	for (card = 0; card < cy_cd.cd_ndevs; card++) {
904 		sc = device_lookup(&cy_cd, card);
905 		if (sc == NULL)
906 			continue;
907 
908 #ifdef CY_DEBUG1
909 		sc->sc_poll_count1++;
910 		did_something = 0;
911 #endif
912 
913 		for (port = 0; port < sc->sc_nchips * CD1400_NO_OF_CHANNELS;
914 		    port++) {
915 			cy = &sc->sc_ports[port];
916 			if ((tp = cy->cy_tty) == NULL || cy->cy_ibuf == NULL ||
917 			    (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0))
918 				continue;
919 
920 			/*
921 		         * handle received data
922 		         */
923 			while (cy->cy_ibuf_rd_ptr != cy->cy_ibuf_wr_ptr) {
924 				u_char line_stat;
925 				int chr;
926 
927 				line_stat = cy->cy_ibuf_rd_ptr[0];
928 				chr = cy->cy_ibuf_rd_ptr[1];
929 
930 				if (line_stat &
931 				    (CD1400_RDSR_BREAK | CD1400_RDSR_FE))
932 					chr |= TTY_FE;
933 				if (line_stat & CD1400_RDSR_PE)
934 					chr |= TTY_PE;
935 
936 				/*
937 				 * on an overrun error the data is treated as
938 				 * good just as it should be.
939 				 */
940 
941 #ifdef CY_DEBUG
942 				printf("%s: port %d ttyinput 0x%x\n",
943 				    sc->sc_dev.dv_xname, port, chr);
944 #endif
945 
946 				(*linesw[tp->t_line].l_rint) (chr, tp);
947 
948 				s = spltty();	/* really necessary? */
949 				if ((cy->cy_ibuf_rd_ptr += 2) ==
950 				    cy->cy_ibuf_end)
951 					cy->cy_ibuf_rd_ptr = cy->cy_ibuf;
952 				splx(s);
953 
954 #ifdef CY_DEBUG1
955 				did_something = 1;
956 #endif
957 			}
958 
959 			/*
960 			 * If we don't have any received data in ibuf and
961 			 * CRTSCTS is on and RTS is turned off, it is time to
962 			 * turn RTS back on
963 			 */
964 			if (ISSET(tp->t_cflag, CRTSCTS)) {
965 				/*
966 				 * we can't use cy_modem_control() here as it
967 				 * doesn't change RTS if RTSCTS is on
968 				 */
969 				cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
970 				    port & CD1400_CAR_CHAN);
971 
972 				if (cy->cy_clock == CY_CLOCK_60) {
973 				  if ((cd_read_reg(sc, cy->cy_chip,
974 				    CD1400_MSVR2) & CD1400_MSVR2_DTR) == 0) {
975 					cd_write_reg(sc, cy->cy_chip,
976 					CD1400_MSVR2,CD1400_MSVR2_DTR);
977 #ifdef CY_DEBUG1
978 					did_something = 1;
979 #endif
980 				  }
981 				} else {
982 				  if ((cd_read_reg(sc, cy->cy_chip,
983 				    CD1400_MSVR1) & CD1400_MSVR1_RTS) == 0) {
984 					cd_write_reg(sc, cy->cy_chip,
985 					CD1400_MSVR1,CD1400_MSVR1_RTS);
986 #ifdef CY_DEBUG1
987 					did_something = 1;
988 #endif
989 				  }
990 				}
991 			}
992 
993 			/*
994 		         * handle carrier changes
995 		         */
996 			s = spltty();
997 			if (ISSET(cy->cy_flags, CY_F_CARRIER_CHANGED)) {
998 				int             carrier;
999 
1000 				CLR(cy->cy_flags, CY_F_CARRIER_CHANGED);
1001 				splx(s);
1002 
1003 				carrier = ((cy->cy_carrier_stat &
1004 				    CD1400_MSVR2_CD) != 0);
1005 
1006 #ifdef CY_DEBUG
1007 				printf("cy_poll: carrier change "
1008 				    "(card %d, port %d, carrier %d)\n",
1009 				    card, port, carrier);
1010 #endif
1011 				if (CY_DIALIN(tp->t_dev) &&
1012 				    !(*linesw[tp->t_line].l_modem)(tp, carrier))
1013 					cy_modem_control(sc, cy,
1014 					    TIOCM_DTR, DMBIC);
1015 
1016 #ifdef CY_DEBUG1
1017 				did_something = 1;
1018 #endif
1019 			} else
1020 				splx(s);
1021 
1022 			s = spltty();
1023 			if (ISSET(cy->cy_flags, CY_F_START)) {
1024 				CLR(cy->cy_flags, CY_F_START);
1025 				splx(s);
1026 
1027 				(*linesw[tp->t_line].l_start) (tp);
1028 
1029 #ifdef CY_DEBUG1
1030 				did_something = 1;
1031 #endif
1032 			} else
1033 				splx(s);
1034 
1035 			/* could move this to even upper level... */
1036 			if (cy->cy_fifo_overruns) {
1037 				cy->cy_fifo_overruns = 0;
1038 				/*
1039 				 * doesn't report overrun count, but
1040 				 * shouldn't really matter
1041 				 */
1042 				log(LOG_WARNING, "%s: port %d fifo overrun\n",
1043 				    sc->sc_dev.dv_xname, port);
1044 			}
1045 			if (cy->cy_ibuf_overruns) {
1046 				cy->cy_ibuf_overruns = 0;
1047 				log(LOG_WARNING, "%s: port %d ibuf overrun\n",
1048 				    sc->sc_dev.dv_xname, port);
1049 			}
1050 		}		/* for(port...) */
1051 #ifdef CY_DEBUG1
1052 		if (did_something && counter >= 200)
1053 			sc->sc_poll_count2++;
1054 #endif
1055 	} /* for(card...) */
1056 
1057 	counter = 0;
1058 
1059 out:
1060 	callout_reset(&cy_poll_callout, 1, cy_poll, NULL);
1061 }
1062 
1063 /*
1064  * hardware interrupt routine
1065  */
1066 int
1067 cy_intr(arg)
1068 	void *arg;
1069 {
1070 	struct cy_softc *sc = arg;
1071 	struct cy_port *cy;
1072 	int cy_chip, stat;
1073 	int int_serviced = 0;
1074 
1075 	/*
1076 	 * Check interrupt status of each CD1400 chip on this card
1077 	 * (multiple cards cannot share the same interrupt)
1078 	 */
1079 	for (cy_chip = 0; cy_chip < sc->sc_nchips; cy_chip++) {
1080 
1081 		stat = cd_read_reg(sc, cy_chip, CD1400_SVRR);
1082 		if (stat == 0)
1083 			continue;
1084 
1085 		if (ISSET(stat, CD1400_SVRR_RXRDY)) {
1086 			u_char save_car, save_rir, serv_type;
1087 			u_char line_stat, recv_data, n_chars;
1088 			u_char *buf_p;
1089 
1090 			save_rir = cd_read_reg(sc, cy_chip, CD1400_RIR);
1091 			save_car = cd_read_reg(sc, cy_chip, CD1400_CAR);
1092 			/* enter rx service */
1093 			cd_write_reg(sc, cy_chip, CD1400_CAR, save_rir);
1094 
1095 			serv_type = cd_read_reg(sc, cy_chip, CD1400_RIVR);
1096 			cy = &sc->sc_ports[serv_type >> 3];
1097 
1098 #ifdef CY_DEBUG1
1099 			cy->cy_rx_int_count++;
1100 #endif
1101 
1102 			buf_p = cy->cy_ibuf_wr_ptr;
1103 
1104 			if (ISSET(serv_type, CD1400_RIVR_EXCEPTION)) {
1105 				line_stat = cd_read_reg(sc, cy->cy_chip,
1106 				    CD1400_RDSR);
1107 				recv_data = cd_read_reg(sc, cy->cy_chip,
1108 				    CD1400_RDSR);
1109 
1110 				if (cy->cy_tty == NULL ||
1111 				    !ISSET(cy->cy_tty->t_state, TS_ISOPEN))
1112 					goto end_rx_serv;
1113 
1114 #ifdef CY_DEBUG
1115 				printf("%s port %d recv exception, line_stat 0x%x, char 0x%x\n",
1116 				sc->sc_dev.dv_xname, cy->cy_port_num, line_stat, recv_data);
1117 #endif
1118 				if (ISSET(line_stat, CD1400_RDSR_OE))
1119 					cy->cy_fifo_overruns++;
1120 
1121 				*buf_p++ = line_stat;
1122 				*buf_p++ = recv_data;
1123 				if (buf_p == cy->cy_ibuf_end)
1124 					buf_p = cy->cy_ibuf;
1125 
1126 				if (buf_p == cy->cy_ibuf_rd_ptr) {
1127 					if (buf_p == cy->cy_ibuf)
1128 						buf_p = cy->cy_ibuf_end;
1129 					buf_p -= 2;
1130 					cy->cy_ibuf_overruns++;
1131 				}
1132 				cy_events = 1;
1133 			} else {/* no exception, received data OK */
1134 				n_chars = cd_read_reg(sc, cy->cy_chip,
1135 				    CD1400_RDCR);
1136 
1137 				/* If no tty or not open, discard data */
1138 				if (cy->cy_tty == NULL ||
1139 				    !ISSET(cy->cy_tty->t_state, TS_ISOPEN)) {
1140 					while (n_chars--)
1141 						cd_read_reg(sc, cy->cy_chip,
1142 							    CD1400_RDSR);
1143 					goto end_rx_serv;
1144 				}
1145 
1146 #ifdef CY_DEBUG
1147 				printf("%s port %d receive ok %d chars\n",
1148 				    sc->sc_dev.dv_xname, cy->cy_port_num, n_chars);
1149 #endif
1150 				while (n_chars--) {
1151 					*buf_p++ = 0;	/* status: OK */
1152 					/* data byte */
1153 					*buf_p++ = cd_read_reg(sc,
1154 					    cy->cy_chip, CD1400_RDSR);
1155 					if (buf_p == cy->cy_ibuf_end)
1156 						buf_p = cy->cy_ibuf;
1157 					if (buf_p == cy->cy_ibuf_rd_ptr) {
1158 						if (buf_p == cy->cy_ibuf)
1159 							buf_p = cy->cy_ibuf_end;
1160 						buf_p -= 2;
1161 						cy->cy_ibuf_overruns++;
1162 						break;
1163 					}
1164 				}
1165 				cy_events = 1;
1166 			}
1167 
1168 			cy->cy_ibuf_wr_ptr = buf_p;
1169 
1170 			/* RTS handshaking for incoming data */
1171 			if (ISSET(cy->cy_tty->t_cflag, CRTSCTS)) {
1172 				int bf, msvr;
1173 
1174 				bf = buf_p - cy->cy_ibuf_rd_ptr;
1175 				if (bf < 0)
1176 					bf += CY_IBUF_SIZE;
1177 
1178 				if (bf > (CY_IBUF_SIZE / 2)) {
1179 					/* turn RTS off */
1180 					if (cy->cy_clock == CY_CLOCK_60)
1181 						msvr = CD1400_MSVR2;
1182 					else
1183 						msvr = CD1400_MSVR1;
1184 					cd_write_reg(sc, cy->cy_chip, msvr, 0);
1185 				}
1186 			}
1187 
1188 	end_rx_serv:
1189 			/* terminate service context */
1190 			cd_write_reg(sc, cy->cy_chip, CD1400_RIR,
1191 				     save_rir & 0x3f);
1192 			cd_write_reg(sc, cy->cy_chip, CD1400_CAR, save_car);
1193 			int_serviced = 1;
1194 		} /* if (rx_service...) */
1195 		if (ISSET(stat, CD1400_SVRR_MDMCH)) {
1196 			u_char save_car, save_mir, serv_type, modem_stat;
1197 
1198 			save_mir = cd_read_reg(sc, cy_chip, CD1400_MIR);
1199 			save_car = cd_read_reg(sc, cy_chip, CD1400_CAR);
1200 			/* enter modem service */
1201 			cd_write_reg(sc, cy_chip, CD1400_CAR, save_mir);
1202 
1203 			serv_type = cd_read_reg(sc, cy_chip, CD1400_MIVR);
1204 			cy = &sc->sc_ports[serv_type >> 3];
1205 
1206 #ifdef CY_DEBUG1
1207 			cy->cy_modem_int_count++;
1208 #endif
1209 
1210 			modem_stat = cd_read_reg(sc, cy->cy_chip, CD1400_MSVR2);
1211 
1212 #ifdef CY_DEBUG
1213 			printf("%s port %d modem line change, new stat 0x%x\n",
1214 			       sc->sc_dev.dv_xname, cy->cy_port_num, modem_stat);
1215 #endif
1216 			if (ISSET((cy->cy_carrier_stat ^ modem_stat), CD1400_MSVR2_CD)) {
1217 				SET(cy->cy_flags, CY_F_CARRIER_CHANGED);
1218 				cy_events = 1;
1219 			}
1220 			cy->cy_carrier_stat = modem_stat;
1221 
1222 			/* terminate service context */
1223 			cd_write_reg(sc, cy->cy_chip, CD1400_MIR, save_mir & 0x3f);
1224 			cd_write_reg(sc, cy->cy_chip, CD1400_CAR, save_car);
1225 			int_serviced = 1;
1226 		} /* if (modem_service...) */
1227 		if (ISSET(stat, CD1400_SVRR_TXRDY)) {
1228 			u_char          save_car, save_tir, serv_type,
1229 			                count, ch;
1230 			struct tty     *tp;
1231 
1232 			save_tir = cd_read_reg(sc, cy_chip, CD1400_TIR);
1233 			save_car = cd_read_reg(sc, cy_chip, CD1400_CAR);
1234 			/* enter tx service */
1235 			cd_write_reg(sc, cy_chip, CD1400_CAR, save_tir);
1236 
1237 			serv_type = cd_read_reg(sc, cy_chip, CD1400_TIVR);
1238 			cy = &sc->sc_ports[serv_type >> 3];
1239 
1240 #ifdef CY_DEBUG1
1241 			cy->cy_tx_int_count++;
1242 #endif
1243 #ifdef CY_DEBUG
1244 			printf("%s port %d tx service\n", sc->sc_dev.dv_xname,
1245 			    cy->cy_port_num);
1246 #endif
1247 
1248 			/* stop transmitting if no tty or CY_F_STOP set */
1249 			tp = cy->cy_tty;
1250 			if (tp == NULL || ISSET(cy->cy_flags, CY_F_STOP))
1251 				goto txdone;
1252 
1253 			count = 0;
1254 			if (ISSET(cy->cy_flags, CY_F_SEND_NUL)) {
1255 				cd_write_reg(sc, cy->cy_chip, CD1400_TDR, 0);
1256 				cd_write_reg(sc, cy->cy_chip, CD1400_TDR, 0);
1257 				count += 2;
1258 				CLR(cy->cy_flags, CY_F_SEND_NUL);
1259 			}
1260 			if (tp->t_outq.c_cc > 0) {
1261 				SET(tp->t_state, TS_BUSY);
1262 				while (tp->t_outq.c_cc > 0 &&
1263 				    count < CD1400_TX_FIFO_SIZE) {
1264 					ch = getc(&tp->t_outq);
1265 					/*
1266 					 * remember to double NUL characters
1267 					 * because embedded transmit commands
1268 					 * are enabled
1269 					 */
1270 					if (ch == 0) {
1271 						if (count >= CD1400_TX_FIFO_SIZE - 2) {
1272 							SET(cy->cy_flags, CY_F_SEND_NUL);
1273 							break;
1274 						}
1275 						cd_write_reg(sc, cy->cy_chip,
1276 						    CD1400_TDR, ch);
1277 						count++;
1278 					}
1279 					cd_write_reg(sc, cy->cy_chip,
1280 					    CD1400_TDR, ch);
1281 					count++;
1282 				}
1283 			} else {
1284 				/*
1285 				 * no data to send -- check if we should
1286 				 * start/stop a break
1287 				 */
1288 				/*
1289 				 * XXX does this cause too much delay before
1290 				 * breaks?
1291 				 */
1292 				if (ISSET(cy->cy_flags, CY_F_START_BREAK)) {
1293 					cd_write_reg(sc, cy->cy_chip,
1294 					    CD1400_TDR, 0);
1295 					cd_write_reg(sc, cy->cy_chip,
1296 					    CD1400_TDR, 0x81);
1297 					CLR(cy->cy_flags, CY_F_START_BREAK);
1298 				}
1299 				if (ISSET(cy->cy_flags, CY_F_END_BREAK)) {
1300 					cd_write_reg(sc, cy->cy_chip,
1301 					    CD1400_TDR, 0);
1302 					cd_write_reg(sc, cy->cy_chip,
1303 					    CD1400_TDR, 0x83);
1304 					CLR(cy->cy_flags, CY_F_END_BREAK);
1305 				}
1306 			}
1307 
1308 			if (tp->t_outq.c_cc == 0) {
1309 		txdone:
1310 				/*
1311 				 * No data to send or requested to stop.
1312 				 * Disable transmit interrupt
1313 				 */
1314 				cd_write_reg(sc, cy->cy_chip, CD1400_SRER,
1315 				     cd_read_reg(sc, cy->cy_chip, CD1400_SRER)
1316 				     & ~CD1400_SRER_TXRDY);
1317 				CLR(cy->cy_flags, CY_F_STOP);
1318 				CLR(tp->t_state, TS_BUSY);
1319 			}
1320 			if (tp->t_outq.c_cc <= tp->t_lowat) {
1321 				SET(cy->cy_flags, CY_F_START);
1322 				cy_events = 1;
1323 			}
1324 			/* terminate service context */
1325 			cd_write_reg(sc, cy->cy_chip, CD1400_TIR, save_tir & 0x3f);
1326 			cd_write_reg(sc, cy->cy_chip, CD1400_CAR, save_car);
1327 			int_serviced = 1;
1328 		}		/* if (tx_service...) */
1329 	}			/* for(...all CD1400s on a card) */
1330 
1331 	/* ensure an edge for next interrupt */
1332 	bus_space_write_1(sc->sc_memt, sc->sc_bsh,
1333 			CY_CLEAR_INTR << sc->sc_bustype, 0);
1334 	return int_serviced;
1335 }
1336 
1337 /*
1338  * subroutine to enable CD1400 transmitter
1339  */
1340 static void
1341 cy_enable_transmitter(sc, cy)
1342 	struct cy_softc *sc;
1343 	struct cy_port *cy;
1344 {
1345 	int s = spltty();
1346 	cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
1347 	    cy->cy_port_num & CD1400_CAR_CHAN);
1348 	cd_write_reg(sc, cy->cy_chip, CD1400_SRER,
1349 	    cd_read_reg(sc, cy->cy_chip, CD1400_SRER) | CD1400_SRER_TXRDY);
1350 	splx(s);
1351 }
1352 
1353 /*
1354  * Execute a CD1400 channel command
1355  */
1356 static void
1357 cd1400_channel_cmd(sc, cy, cmd)
1358 	struct cy_softc *sc;
1359 	struct cy_port *cy;
1360 	int cmd;
1361 {
1362 	u_int waitcnt = 5 * 8 * 1024;	/* approx 5 ms */
1363 
1364 #ifdef CY_DEBUG
1365 	printf("c1400_channel_cmd cy %p command 0x%x\n", cy, cmd);
1366 #endif
1367 
1368 	/* wait until cd1400 is ready to process a new command */
1369 	while (cd_read_reg(sc, cy->cy_chip, CD1400_CCR) != 0 && waitcnt-- > 0);
1370 
1371 	if (waitcnt == 0)
1372 		log(LOG_ERR, "%s: channel command timeout\n",
1373 		    sc->sc_dev.dv_xname);
1374 
1375 	cd_write_reg(sc, cy->cy_chip, CD1400_CCR, cmd);
1376 }
1377 
1378 /*
1379  * Compute clock option register and baud rate register values
1380  * for a given speed. Return 0 on success, -1 on failure.
1381  *
1382  * The error between requested and actual speed seems
1383  * to be well within allowed limits (less than 3%)
1384  * with every speed value between 50 and 150000 bps.
1385  */
1386 static int
1387 cy_speed(speed, cor, bpr, cy_clock)
1388     speed_t speed;
1389     int *cor, *bpr, cy_clock;
1390 {
1391 	int c, co, br;
1392 
1393 	if (speed < 50 || speed > 150000)
1394 		return -1;
1395 
1396 	for (c = 0, co = 8; co <= 2048; co <<= 2, c++) {
1397 		br = (cy_clock + (co * speed) / 2) / (co * speed);
1398 		if (br < 0x100) {
1399 			*bpr = br;
1400 			*cor = c;
1401 			return 0;
1402 		}
1403 	}
1404 
1405 	return -1;
1406 }
1407