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