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