Home | History | Annotate | Line # | Download | only in ic
cy.c revision 1.29
      1 /*	$NetBSD: cy.c,v 1.29 2003/01/06 12:46:10 wiz 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/cdefs.h>
     19 __KERNEL_RCSID(0, "$NetBSD: cy.c,v 1.29 2003/01/06 12:46:10 wiz Exp $");
     20 
     21 #include <sys/param.h>
     22 #include <sys/ioctl.h>
     23 #include <sys/syslog.h>
     24 #include <sys/fcntl.h>
     25 #include <sys/tty.h>
     26 #include <sys/proc.h>
     27 #include <sys/conf.h>
     28 #include <sys/user.h>
     29 #include <sys/ioctl.h>
     30 #include <sys/select.h>
     31 #include <sys/device.h>
     32 #include <sys/malloc.h>
     33 #include <sys/systm.h>
     34 #include <sys/callout.h>
     35 
     36 #include <machine/bus.h>
     37 
     38 #include <dev/ic/cd1400reg.h>
     39 #include <dev/ic/cyreg.h>
     40 #include <dev/ic/cyvar.h>
     41 
     42 /* Macros to clear/set/test flags. */
     43 #define	SET(t, f)	(t) |= (f)
     44 #define	CLR(t, f)	(t) &= ~(f)
     45 #define	ISSET(t, f)	((t) & (f))
     46 
     47 int	cyparam(struct tty *, struct termios *);
     48 void	cystart(struct tty *);
     49 void	cy_poll(void *);
     50 int	cy_modem_control(struct cy_softc *, struct cy_port *, int, int);
     51 void	cy_enable_transmitter(struct cy_softc *, struct cy_port *);
     52 void	cd1400_channel_cmd(struct cy_softc *, struct cy_port *, int);
     53 int	cy_speed(speed_t, int *, int *, int);
     54 
     55 extern struct cfdriver cy_cd;
     56 
     57 dev_type_open(cyopen);
     58 dev_type_close(cyclose);
     59 dev_type_read(cyread);
     60 dev_type_write(cywrite);
     61 dev_type_ioctl(cyioctl);
     62 dev_type_stop(cystop);
     63 dev_type_tty(cytty);
     64 dev_type_poll(cypoll);
     65 
     66 const struct cdevsw cy_cdevsw = {
     67 	cyopen, cyclose, cyread, cywrite, cyioctl,
     68 	cystop, cytty, cypoll, nommap, ttykqfilter, D_TTY
     69 };
     70 
     71 static int      cy_open = 0;
     72 static int      cy_events = 0;
     73 
     74 int	cy_attached_ttys;
     75 
     76 struct callout cy_poll_callout = CALLOUT_INITIALIZER;
     77 
     78 /*
     79  * Common probe routine
     80  */
     81 int
     82 cy_find(struct cy_softc *sc)
     83 {
     84 	int cy_chip, chip;
     85 	u_char firmware_ver;
     86 	bus_space_tag_t tag = sc->sc_memt;
     87 	bus_space_handle_t bsh = sc->sc_bsh;
     88 	int bustype = sc->sc_bustype;
     89 
     90 	/* Cyclom card hardware reset */
     91 	bus_space_write_1(tag, bsh, CY16_RESET << bustype, 0);
     92 	DELAY(500);		/* wait for reset to complete */
     93 	bus_space_write_1(tag, bsh, CY_CLEAR_INTR << bustype, 0);
     94 
     95 #ifdef CY_DEBUG
     96 	printf("cy: card reset done\n");
     97 #endif
     98 	sc->sc_nchips = 0;
     99 
    100 	for (cy_chip = 0, chip = 0; cy_chip < CY_MAX_CD1400s;
    101 	    cy_chip++, chip += (CY_CD1400_MEMSPACING << bustype)) {
    102 		int i;
    103 
    104 		/*
    105 		 * the last 4 nchips are 'interleaved' with the first 4 on
    106 		 * 32-port boards
    107 		 */
    108 		if (cy_chip == 4)
    109 			chip -= (CY32_ADDR_FIX << bustype);
    110 
    111 #ifdef CY_DEBUG
    112 		printf("%s probe chip %d offset 0x%x ... ",
    113 		    sc->sc_dev.dv_xname, cy_chip, chip);
    114 #endif
    115 
    116 		/* wait until the chip is ready for command */
    117 		DELAY(1000);
    118 		if (bus_space_read_1(tag, bsh, chip +
    119 		    ((CD1400_CCR << 1) << bustype)) != 0) {
    120 #ifdef CY_DEBUG
    121 			printf("not ready for command\n");
    122 #endif
    123 			break;
    124 		}
    125 		/* clear the firmware version reg. */
    126 		bus_space_write_1(tag, bsh, chip +
    127 		    ((CD1400_GFRCR << 1) << bustype), 0);
    128 
    129 		/*
    130 	         * On Cyclom-16 references to non-existent chip 4
    131 	         * actually access chip 0 (address line 9 not decoded).
    132 	         * Here we check if the clearing of chip 4 GFRCR actually
    133 	         * cleared chip 0 GFRCR. In that case we have a 16 port card.
    134 	         */
    135 		if (cy_chip == 4 &&
    136 		    bus_space_read_1(tag, bsh, /* off for chip 0 (0) + */
    137 		       ((CD1400_GFRCR << 1) << bustype)) == 0)
    138 			break;
    139 
    140 		/* reset the chip */
    141 		bus_space_write_1(tag, bsh, chip +
    142 		    ((CD1400_CCR << 1) << bustype),
    143 		    CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
    144 
    145 		/* wait for the chip to initialize itself */
    146 		for (i = 0; i < 200; i++) {
    147 			DELAY(50);
    148 			firmware_ver = bus_space_read_1(tag, bsh, chip +
    149 			    ((CD1400_GFRCR << 1) << bustype));
    150 			if ((firmware_ver & 0xf0) == 0x40) /* found a CD1400 */
    151 				break;
    152 		}
    153 #ifdef CY_DEBUG
    154 		printf("firmware version 0x%x\n", firmware_ver);
    155 #endif
    156 
    157 		if ((firmware_ver & 0xf0) != 0x40)
    158 			break;
    159 
    160 		/* firmware version OK, CD1400 found */
    161 		sc->sc_nchips++;
    162 	}
    163 
    164 	if (sc->sc_nchips == 0) {
    165 #ifdef CY_DEBUG
    166 		printf("no CD1400s found\n");
    167 #endif
    168 		return 0;
    169 	}
    170 #ifdef CY_DEBUG
    171 	printf("found %d CD1400s\n", sc->sc_nchips);
    172 #endif
    173 
    174 	return 1;
    175 }
    176 
    177 void
    178 cy_attach(struct cy_softc *sc)
    179 {
    180 	int port, cy_chip, num_chips, cdu, chip;
    181 	int cy_clock;
    182 
    183 	num_chips = sc->sc_nchips;
    184 	if (num_chips == 0)
    185 		return;
    186 
    187 	memset(sc->sc_ports, 0, sizeof(sc->sc_ports));
    188 
    189 	port = 0;
    190 	for (cy_chip = 0, chip = 0; cy_chip < num_chips; cy_chip++,
    191 	    chip += (CY_CD1400_MEMSPACING << sc->sc_bustype)) {
    192 
    193 		if (cy_chip == 4)
    194 			chip -= (CY32_ADDR_FIX << sc->sc_bustype);
    195 
    196 #ifdef CY_DEBUG
    197 		printf("attach CD1400 #%d offset 0x%x\n", cy_chip, chip);
    198 #endif
    199 		sc->sc_cd1400_offs[cy_chip] = chip;
    200 
    201 		/*
    202 		 * configure port 0 as serial port (should already be after
    203 		 * reset)
    204 		 */
    205 		cd_write_reg(sc, cy_chip, CD1400_GCR, 0);
    206 
    207 		if (cd_read_reg(sc, cy_chip, CD1400_GFRCR) <= 0x46)
    208 			cy_clock = CY_CLOCK;
    209 		else
    210 			cy_clock = CY_CLOCK_60;
    211 
    212 		/* set up a receive timeout period (1ms) */
    213 		cd_write_reg(sc, cy_chip, CD1400_PPR,
    214 		    (cy_clock / CD1400_PPR_PRESCALER / 1000) + 1);
    215 
    216 		for (cdu = 0; cdu < CD1400_NO_OF_CHANNELS; cdu++) {
    217 			sc->sc_ports[port].cy_softc = sc;
    218 			sc->sc_ports[port].cy_port_num = port;
    219 			sc->sc_ports[port].cy_chip = cy_chip;
    220 			sc->sc_ports[port].cy_clock = cy_clock;
    221 
    222 			/* should we initialize anything else here? */
    223 			port++;
    224 		} /* for(each port on one CD1400...) */
    225 
    226 	} /* for(each CD1400 on a card... ) */
    227 
    228 	sc->sc_nchannels = port;
    229 
    230 	printf("%s: %d channels (ttyCY%03d..ttyCY%03d)\n",
    231 	    sc->sc_dev.dv_xname, sc->sc_nchannels, cy_attached_ttys,
    232 	    cy_attached_ttys + (sc->sc_nchannels - 1));
    233 
    234 	cy_attached_ttys += sc->sc_nchannels;
    235 
    236 	/* ensure an edge for the next interrupt */
    237 	bus_space_write_1(sc->sc_memt, sc->sc_bsh,
    238 	    CY_CLEAR_INTR << sc->sc_bustype, 0);
    239 }
    240 
    241 #define	CYDIALOUT_MASK		0x80000
    242 #define	CY_DIALOUT(dev)		(minor(dev) & CYDIALOUT_MASK)
    243 
    244 #define	CY_PORT(dev)		cy_getport((dev))
    245 #define	CY_BOARD(cy)		((cy)->cy_softc)
    246 
    247 static struct cy_port *
    248 cy_getport(dev_t dev)
    249 {
    250 	int i, j, k, u = minor(dev) & ~CYDIALOUT_MASK;
    251 	struct cy_softc *sc;
    252 
    253 	for (i = 0, j = 0; i < cy_cd.cd_ndevs; i++) {
    254 		k = j;
    255 		sc = device_lookup(&cy_cd, i);
    256 		if (sc == NULL)
    257 			continue;
    258 		if (sc->sc_nchannels == 0)
    259 			continue;
    260 		j += sc->sc_nchannels;
    261 		if (j > u)
    262 			break;
    263 	}
    264 
    265 	if (i == cy_cd.cd_ndevs)
    266 		return (NULL);
    267 	else
    268 		return (&sc->sc_ports[u - k]);
    269 }
    270 
    271 /*
    272  * open routine. returns zero if successful, else error code
    273  */
    274 int
    275 cyopen(dev_t dev, int flag, int mode, struct proc *p)
    276 {
    277 	struct cy_softc *sc;
    278 	struct cy_port *cy;
    279 	struct tty *tp;
    280 	int s, error;
    281 
    282 	cy = CY_PORT(dev);
    283 	if (cy == NULL)
    284 		return (ENXIO);
    285 	sc = CY_BOARD(cy);
    286 
    287 	s = spltty();
    288 	if (cy->cy_tty == NULL) {
    289 		if ((cy->cy_tty = ttymalloc()) == NULL) {
    290 			splx(s);
    291 			printf("%s: port %d: can't allocate tty\n",
    292 			    sc->sc_dev.dv_xname, cy->cy_port_num);
    293 			return (ENOMEM);
    294 		}
    295 		tty_attach(cy->cy_tty);
    296 	}
    297 	splx(s);
    298 
    299 	tp = cy->cy_tty;
    300 	tp->t_oproc = cystart;
    301 	tp->t_param = cyparam;
    302 	tp->t_dev = dev;
    303 
    304 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    305 		ttychars(tp);
    306 		tp->t_iflag = TTYDEF_IFLAG;
    307 		tp->t_oflag = TTYDEF_OFLAG;
    308 		tp->t_cflag = TTYDEF_CFLAG;
    309 		if (ISSET(cy->cy_openflags, TIOCFLAG_CLOCAL))
    310 			SET(tp->t_cflag, CLOCAL);
    311 		if (ISSET(cy->cy_openflags, TIOCFLAG_CRTSCTS))
    312 			SET(tp->t_cflag, CRTSCTS);
    313 		if (ISSET(cy->cy_openflags, TIOCFLAG_MDMBUF))
    314 			SET(tp->t_cflag, MDMBUF);
    315 		tp->t_lflag = TTYDEF_LFLAG;
    316 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    317 
    318 		s = spltty();
    319 
    320 		/*
    321 		 * Allocate input ring buffer if we don't already have one
    322 		 */
    323 		if (cy->cy_ibuf == NULL) {
    324 			cy->cy_ibuf = malloc(CY_IBUF_SIZE, M_DEVBUF, M_NOWAIT);
    325 			if (cy->cy_ibuf == NULL) {
    326 				printf("%s: port %d: can't allocate input buffer\n",
    327 				       sc->sc_dev.dv_xname, cy->cy_port_num);
    328 				splx(s);
    329 				return ENOMEM;
    330 			}
    331 			cy->cy_ibuf_end = cy->cy_ibuf + CY_IBUF_SIZE;
    332 		}
    333 		/* mark the ring buffer as empty */
    334 		cy->cy_ibuf_rd_ptr = cy->cy_ibuf_wr_ptr = cy->cy_ibuf;
    335 
    336 		/* select CD1400 channel */
    337 		cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
    338 		    cy->cy_port_num & CD1400_CAR_CHAN);
    339 		/* reset the channel */
    340 		cd1400_channel_cmd(sc, cy, CD1400_CCR_CMDRESET);
    341 		/* encode unit (port) number in LIVR */
    342 		/* there is just enough space for 5 bits (32 ports) */
    343 		cd_write_reg(sc, cy->cy_chip, CD1400_LIVR,
    344 		    cy->cy_port_num << 3);
    345 
    346 		cy->cy_channel_control = 0;
    347 
    348 		/* hmm... need spltty() here? */
    349 		if (cy_open == 0) {
    350 			cy_open = 1;
    351 			callout_reset(&cy_poll_callout, 1, cy_poll, NULL);
    352 		}
    353 		/* this sets parameters and raises DTR */
    354 		cyparam(tp, &tp->t_termios);
    355 
    356 		ttsetwater(tp);
    357 
    358 		/* raise RTS too */
    359 		cy_modem_control(sc, cy, TIOCM_RTS, DMBIS);
    360 
    361 		cy->cy_carrier_stat =
    362 			cd_read_reg(sc, cy->cy_chip, CD1400_MSVR2);
    363 
    364 		/* enable receiver and modem change interrupts */
    365 		cd_write_reg(sc, cy->cy_chip, CD1400_SRER,
    366 		    CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
    367 
    368 		if (CY_DIALOUT(dev) ||
    369 		    ISSET(cy->cy_openflags, TIOCFLAG_SOFTCAR) ||
    370 		    ISSET(tp->t_cflag, MDMBUF) ||
    371 		    ISSET(cy->cy_carrier_stat, CD1400_MSVR2_CD))
    372 			SET(tp->t_state, TS_CARR_ON);
    373 		else
    374 			CLR(tp->t_state, TS_CARR_ON);
    375 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0) {
    376 		return EBUSY;
    377 	} else {
    378 		s = spltty();
    379 	}
    380 
    381 	/* wait for carrier if necessary */
    382 	if (!ISSET(flag, O_NONBLOCK)) {
    383 		while (!ISSET(tp->t_cflag, CLOCAL) &&
    384 		    !ISSET(tp->t_state, TS_CARR_ON)) {
    385 			tp->t_wopen++;
    386 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
    387 			    "cydcd", 0);
    388 			tp->t_wopen--;
    389 			if (error != 0) {
    390 				splx(s);
    391 				return error;
    392 			}
    393 		}
    394 	}
    395 	splx(s);
    396 
    397 	return (*tp->t_linesw->l_open) (dev, tp);
    398 }
    399 
    400 /*
    401  * close routine. returns zero if successful, else error code
    402  */
    403 int
    404 cyclose(dev_t dev, int flag, int mode, struct proc *p)
    405 {
    406 	struct cy_softc *sc;
    407 	struct cy_port *cy;
    408 	struct tty *tp;
    409 	int s;
    410 
    411 	cy = CY_PORT(dev);
    412 	sc = CY_BOARD(cy);
    413 	tp = cy->cy_tty;
    414 
    415 	(*tp->t_linesw->l_close) (tp, flag);
    416 	s = spltty();
    417 
    418 	if (ISSET(tp->t_cflag, HUPCL) &&
    419 	    !ISSET(cy->cy_openflags, TIOCFLAG_SOFTCAR)) {
    420 		/*
    421 		 * drop DTR and RTS (should we wait for output buffer to
    422 		 * become empty first?)
    423 		 */
    424 		cy_modem_control(sc, cy, 0, DMSET);
    425 	}
    426 	/*
    427 	 * XXX should we disable modem change and
    428 	 * receive interrupts here or somewhere ?
    429 	 */
    430 	CLR(tp->t_state, TS_BUSY | TS_FLUSH);
    431 
    432 	splx(s);
    433 	ttyclose(tp);
    434 
    435 	return 0;
    436 }
    437 
    438 /*
    439  * Read routine
    440  */
    441 int
    442 cyread(dev_t dev, struct uio *uio, int flag)
    443 {
    444 	struct cy_port *cy;
    445 	struct tty *tp;
    446 
    447 	cy = CY_PORT(dev);
    448 	tp = cy->cy_tty;
    449 
    450 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    451 }
    452 
    453 /*
    454  * Write routine
    455  */
    456 int
    457 cywrite(dev_t dev, struct uio *uio, int flag)
    458 {
    459 	struct cy_port *cy;
    460 	struct tty *tp;
    461 
    462 	cy = CY_PORT(dev);
    463 	tp = cy->cy_tty;
    464 
    465 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    466 }
    467 
    468 /*
    469  * Poll routine
    470  */
    471 int
    472 cypoll(dev, events, p)
    473 	dev_t dev;
    474 	int events;
    475 	struct proc *p;
    476 {
    477 	struct cy_port *cy;
    478 	struct tty *tp;
    479 
    480 	cy = CY_PORT(dev);
    481 	tp = cy->cy_tty;
    482 
    483 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    484 }
    485 
    486 /*
    487  * return tty pointer
    488  */
    489 struct tty *
    490 cytty(dev_t dev)
    491 {
    492 	struct cy_port *cy;
    493 
    494 	cy = CY_PORT(dev);
    495 
    496 	return (cy->cy_tty);
    497 }
    498 
    499 /*
    500  * ioctl routine
    501  */
    502 int
    503 cyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    504 {
    505 	struct cy_softc *sc;
    506 	struct cy_port *cy;
    507 	struct tty *tp;
    508 	int error;
    509 
    510 	cy = CY_PORT(dev);
    511 	sc = CY_BOARD(cy);
    512 	tp = cy->cy_tty;
    513 
    514 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    515 	if (error != EPASSTHROUGH)
    516 		return error;
    517 
    518 	error = ttioctl(tp, cmd, data, flag, p);
    519 	if (error != EPASSTHROUGH)
    520 		return error;
    521 
    522 	/* XXX should not allow dropping DTR when dialin? */
    523 
    524 	switch (cmd) {
    525 	case TIOCSBRK:		/* start break */
    526 		SET(cy->cy_flags, CY_F_START_BREAK);
    527 		cy_enable_transmitter(sc, cy);
    528 		break;
    529 
    530 	case TIOCCBRK:		/* stop break */
    531 		SET(cy->cy_flags, CY_F_END_BREAK);
    532 		cy_enable_transmitter(sc, cy);
    533 		break;
    534 
    535 	case TIOCSDTR:		/* DTR on */
    536 		cy_modem_control(sc, cy, TIOCM_DTR, DMBIS);
    537 		break;
    538 
    539 	case TIOCCDTR:		/* DTR off */
    540 		cy_modem_control(sc, cy, TIOCM_DTR, DMBIC);
    541 		break;
    542 
    543 	case TIOCMSET:		/* set new modem control line values */
    544 		cy_modem_control(sc, cy, *((int *) data), DMSET);
    545 		break;
    546 
    547 	case TIOCMBIS:		/* turn modem control bits on */
    548 		cy_modem_control(sc, cy, *((int *) data), DMBIS);
    549 		break;
    550 
    551 	case TIOCMBIC:		/* turn modem control bits off */
    552 		cy_modem_control(sc, cy, *((int *) data), DMBIC);
    553 		break;
    554 
    555 	case TIOCMGET:		/* get modem control/status line state */
    556 		*((int *) data) = cy_modem_control(sc, cy, 0, DMGET);
    557 		break;
    558 
    559 	case TIOCGFLAGS:
    560 		*((int *) data) = cy->cy_openflags |
    561 			(CY_DIALOUT(dev) ? TIOCFLAG_SOFTCAR : 0);
    562 		break;
    563 
    564 	case TIOCSFLAGS:
    565 		error = suser(p->p_ucred, &p->p_acflag);
    566 		if (error != 0)
    567 			return EPERM;
    568 
    569 		cy->cy_openflags = *((int *) data) &
    570 		    (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
    571 		     TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
    572 		break;
    573 
    574 	default:
    575 		return EPASSTHROUGH;
    576 	}
    577 
    578 	return 0;
    579 }
    580 
    581 /*
    582  * start output
    583  */
    584 void
    585 cystart(struct tty *tp)
    586 {
    587 	struct cy_softc *sc;
    588 	struct cy_port *cy;
    589 	int s;
    590 
    591 	cy = CY_PORT(tp->t_dev);
    592 	sc = cy->cy_softc;
    593 
    594 	s = spltty();
    595 
    596 #ifdef CY_DEBUG1
    597 	cy->cy_start_count++;
    598 #endif
    599 
    600 	if (!ISSET(tp->t_state, TS_TTSTOP | TS_TIMEOUT | TS_BUSY)) {
    601 		if (tp->t_outq.c_cc <= tp->t_lowat) {
    602 			if (ISSET(tp->t_state, TS_ASLEEP)) {
    603 				CLR(tp->t_state, TS_ASLEEP);
    604 				wakeup(&tp->t_outq);
    605 			}
    606 			selwakeup(&tp->t_wsel);
    607 
    608 			if (tp->t_outq.c_cc == 0)
    609 				goto out;
    610 		}
    611 		SET(tp->t_state, TS_BUSY);
    612 		cy_enable_transmitter(sc, cy);
    613 	}
    614 out:
    615 
    616 	splx(s);
    617 }
    618 
    619 /*
    620  * stop output
    621  */
    622 void
    623 cystop(struct tty *tp, int flag)
    624 {
    625 	struct cy_port *cy;
    626 	int s;
    627 
    628 	cy = CY_PORT(tp->t_dev);
    629 
    630 	s = spltty();
    631 	if (ISSET(tp->t_state, TS_BUSY)) {
    632 		if (!ISSET(tp->t_state, TS_TTSTOP))
    633 			SET(tp->t_state, TS_FLUSH);
    634 
    635 		/*
    636 		 * the transmit interrupt routine will disable transmit when it
    637 		 * notices that CY_F_STOP has been set.
    638 		 */
    639 		SET(cy->cy_flags, CY_F_STOP);
    640 	}
    641 	splx(s);
    642 }
    643 
    644 /*
    645  * parameter setting routine.
    646  * returns 0 if successful, else returns error code
    647  */
    648 int
    649 cyparam(struct tty *tp, struct termios *t)
    650 {
    651 	struct cy_softc *sc;
    652 	struct cy_port *cy;
    653 	int ibpr, obpr, i_clk_opt, o_clk_opt, s, opt;
    654 
    655 	cy = CY_PORT(tp->t_dev);
    656 	sc = CY_BOARD(cy);
    657 
    658 	if (t->c_ospeed != 0 && cy_speed(t->c_ospeed, &o_clk_opt, &obpr, cy->cy_clock) < 0)
    659 		return EINVAL;
    660 
    661 	if (t->c_ispeed != 0 && cy_speed(t->c_ispeed, &i_clk_opt, &ibpr, cy->cy_clock) < 0)
    662 		return EINVAL;
    663 
    664 	s = spltty();
    665 
    666 	/* hang up the line is ospeed is zero, else turn DTR on */
    667 	cy_modem_control(sc, cy, TIOCM_DTR, (t->c_ospeed == 0 ? DMBIC : DMBIS));
    668 
    669 	/* channel was selected by the above call to cy_modem_control() */
    670 #if 0
    671 	cd_write_reg(sc, cy->cy_chip, CD1400_CAR, port & CD1400_CAR_CHAN);
    672 #endif
    673 
    674 	/* set transmit speed */
    675 	if (t->c_ospeed != 0) {
    676 		cd_write_reg(sc, cy->cy_chip, CD1400_TCOR, o_clk_opt);
    677 		cd_write_reg(sc, cy->cy_chip, CD1400_TBPR, obpr);
    678 	}
    679 	/* set receive speed */
    680 	if (t->c_ispeed != 0) {
    681 		cd_write_reg(sc, cy->cy_chip, CD1400_RCOR, i_clk_opt);
    682 		cd_write_reg(sc, cy->cy_chip, CD1400_RBPR, ibpr);
    683 	}
    684 	opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN
    685 		| (ISSET(t->c_cflag, CREAD) ? CD1400_CCR_RCVEN : CD1400_CCR_RCVDIS);
    686 
    687 	if (opt != cy->cy_channel_control) {
    688 		cy->cy_channel_control = opt;
    689 		cd1400_channel_cmd(sc, cy, opt);
    690 	}
    691 	/* compute COR1 contents */
    692 	opt = 0;
    693 	if (ISSET(t->c_cflag, PARENB)) {
    694 		if (ISSET(t->c_cflag, PARODD))
    695 			opt |= CD1400_COR1_PARODD;
    696 		opt |= CD1400_COR1_PARNORMAL;
    697 	}
    698 	if (!ISSET(t->c_iflag, INPCK))
    699 		opt |= CD1400_COR1_NOINPCK;	/* no parity checking */
    700 
    701 	if (ISSET(t->c_cflag, CSTOPB))
    702 		opt |= CD1400_COR1_STOP2;
    703 
    704 	switch (t->c_cflag & CSIZE) {
    705 	case CS5:
    706 		opt |= CD1400_COR1_CS5;
    707 		break;
    708 
    709 	case CS6:
    710 		opt |= CD1400_COR1_CS6;
    711 		break;
    712 
    713 	case CS7:
    714 		opt |= CD1400_COR1_CS7;
    715 		break;
    716 
    717 	default:
    718 		opt |= CD1400_COR1_CS8;
    719 		break;
    720 	}
    721 
    722 	cd_write_reg(sc, cy->cy_chip, CD1400_COR1, opt);
    723 
    724 #ifdef CY_DEBUG
    725 	printf("cor1 = 0x%x...", opt);
    726 #endif
    727 
    728 	/*
    729 	 * use the CD1400 automatic CTS flow control if CRTSCTS is set
    730 	 *
    731 	 * CD1400_COR2_ETC is used because breaks are generated with
    732 	 * embedded transmit commands
    733 	 */
    734 	cd_write_reg(sc, cy->cy_chip, CD1400_COR2,
    735 		     CD1400_COR2_ETC |
    736 		 (ISSET(t->c_cflag, CRTSCTS) ? CD1400_COR2_CCTS_OFLOW : 0));
    737 
    738 	cd_write_reg(sc, cy->cy_chip, CD1400_COR3, CY_RX_FIFO_THRESHOLD);
    739 
    740 	cd1400_channel_cmd(sc, cy, CD1400_CCR_CMDCORCHG |
    741 	    CD1400_CCR_COR1 | CD1400_CCR_COR2 | CD1400_CCR_COR3);
    742 
    743 	cd_write_reg(sc, cy->cy_chip, CD1400_COR4, CD1400_COR4_PFO_EXCEPTION);
    744 	cd_write_reg(sc, cy->cy_chip, CD1400_COR5, 0);
    745 
    746 	/*
    747          * set modem change option registers to generate interrupts
    748          * on carrier detect changes.
    749          *
    750          * if hardware RTS handshaking is used
    751          * also set the handshaking threshold.
    752          */
    753 	if (cy->cy_clock == CY_CLOCK_60) {
    754 	   cd_write_reg(sc, cy->cy_chip, CD1400_MCOR1, CD1400_MCOR1_CDzd |
    755     	      (ISSET(t->c_cflag, CRTSCTS) ? CY_RX_DTR_THRESHOLD : 0));
    756 	} else {
    757 	   cd_write_reg(sc, cy->cy_chip, CD1400_MCOR1, CD1400_MCOR1_CDzd);
    758 	}
    759 
    760 	cd_write_reg(sc, cy->cy_chip, CD1400_MCOR2, CD1400_MCOR2_CDod);
    761 
    762 	/*
    763          * set receive timeout to approx. 2ms
    764          * could use more complex logic here...
    765          * (but is it actually needed or even useful?)
    766          */
    767 	cd_write_reg(sc, cy->cy_chip, CD1400_RTPR, 2);
    768 
    769 	/*
    770          * should do anything else here?
    771          * XXX check MDMBUF handshaking like in com.c?
    772          */
    773 
    774 	splx(s);
    775 	return 0;
    776 }
    777 
    778 /*
    779  * set/get modem line status
    780  *
    781  * bits can be: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, TIOCM_CD, TIOCM_RI, TIOCM_DSR
    782  */
    783 int
    784 cy_modem_control(struct cy_softc *sc, struct cy_port *cy, int bits, int howto)
    785 {
    786 	struct tty *tp = cy->cy_tty;
    787 	int s, msvr;
    788 
    789 	s = spltty();
    790 
    791 	/* select channel */
    792 	cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
    793 	    cy->cy_port_num & CD1400_CAR_CHAN);
    794 
    795 	/* Does not manipulate RTS if it is used for flow control. */
    796 	switch (howto) {
    797 	case DMGET:
    798 		bits = 0;
    799 		if (cy->cy_channel_control & CD1400_CCR_RCVEN)
    800 			bits |= TIOCM_LE;
    801 		msvr = cd_read_reg(sc, cy->cy_chip, CD1400_MSVR2);
    802 		if (cy->cy_clock == CY_CLOCK_60) {
    803 			if (cd_read_reg(sc, cy->cy_chip, CD1400_MSVR1) &
    804 		    		CD1400_MSVR1_RTS)
    805 				bits |= TIOCM_DTR;
    806 			if (msvr & CD1400_MSVR2_DTR)
    807 				bits |= TIOCM_RTS;
    808 		} else {
    809 			if (cd_read_reg(sc, cy->cy_chip, CD1400_MSVR1) &
    810 			    CD1400_MSVR1_RTS)
    811 				bits |= TIOCM_RTS;
    812 			if (msvr & CD1400_MSVR2_DTR)
    813 				bits |= TIOCM_DTR;
    814 		}
    815 		if (msvr & CD1400_MSVR2_CTS)
    816 			bits |= TIOCM_CTS;
    817 		if (msvr & CD1400_MSVR2_CD)
    818 			bits |= TIOCM_CD;
    819 		/* Not connected on some Cyclom-Y boards? */
    820 		if (msvr & CD1400_MSVR2_DSR)
    821 			bits |= TIOCM_DSR;
    822 		/* Not connected on some Cyclom-8Y boards? */
    823 		if (msvr & CD1400_MSVR2_RI)
    824 			bits |= TIOCM_RI;
    825 		break;
    826 
    827 	case DMSET:		/* replace old values with new ones */
    828 		if (cy->cy_clock == CY_CLOCK_60) {
    829 			if (!ISSET(tp->t_cflag, CRTSCTS))
    830 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
    831 				   ((bits & TIOCM_RTS) ? CD1400_MSVR2_DTR : 0));
    832 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
    833 			    ((bits & TIOCM_DTR) ? CD1400_MSVR1_RTS : 0));
    834 		} else {
    835 			if (!ISSET(tp->t_cflag, CRTSCTS))
    836 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
    837 				   ((bits & TIOCM_RTS) ? CD1400_MSVR1_RTS : 0));
    838 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
    839 			    ((bits & TIOCM_DTR) ? CD1400_MSVR2_DTR : 0));
    840 		}
    841 		break;
    842 
    843 	case DMBIS:		/* set bits */
    844 		if (cy->cy_clock == CY_CLOCK_60) {
    845 			if (!ISSET(tp->t_cflag, CRTSCTS) &&
    846 			    (bits & TIOCM_RTS) != 0)
    847 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
    848 				    CD1400_MSVR2_DTR);
    849 			if (bits & TIOCM_DTR)
    850 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
    851 				    CD1400_MSVR1_RTS);
    852 		} else {
    853 			if (!ISSET(tp->t_cflag, CRTSCTS) &&
    854 			    (bits & TIOCM_RTS) != 0)
    855 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
    856 				    CD1400_MSVR1_RTS);
    857 			if (bits & TIOCM_DTR)
    858 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
    859 				    CD1400_MSVR2_DTR);
    860 		}
    861 		break;
    862 
    863 	case DMBIC:		/* clear bits */
    864 		if (cy->cy_clock == CY_CLOCK_60) {
    865 			if (!ISSET(tp->t_cflag, CRTSCTS) && (bits & TIOCM_RTS))
    866 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2, 0);
    867 			if (bits & TIOCM_DTR)
    868 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1, 0);
    869 		} else {
    870 			if (!ISSET(tp->t_cflag, CRTSCTS) && (bits & TIOCM_RTS))
    871 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1, 0);
    872 			if (bits & TIOCM_DTR)
    873 				cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2, 0);
    874 		}
    875 		break;
    876 	}
    877 	splx(s);
    878 	return ((howto == DMGET) ? bits : 0);
    879 }
    880 
    881 /*
    882  * Upper-level handler loop (called from timer interrupt?)
    883  * This routine is common for multiple cards
    884  */
    885 void
    886 cy_poll(void *arg)
    887 {
    888 	int card, port;
    889 	struct cy_softc *sc;
    890 	struct cy_port *cy;
    891 	struct tty *tp;
    892 	static int counter = 0;
    893 #ifdef CY_DEBUG1
    894 	int did_something;
    895 #endif
    896 	int s = spltty();
    897 
    898 	if (cy_events == 0 && ++counter < 200) {
    899 		splx(s);
    900 		goto out;
    901 	}
    902 	cy_events = 0;
    903 	splx(s);
    904 
    905 	for (card = 0; card < cy_cd.cd_ndevs; card++) {
    906 		sc = device_lookup(&cy_cd, card);
    907 		if (sc == NULL)
    908 			continue;
    909 
    910 #ifdef CY_DEBUG1
    911 		sc->sc_poll_count1++;
    912 		did_something = 0;
    913 #endif
    914 
    915 		for (port = 0; port < sc->sc_nchannels; port++) {
    916 			cy = &sc->sc_ports[port];
    917 			if ((tp = cy->cy_tty) == NULL || cy->cy_ibuf == NULL ||
    918 			    (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0))
    919 				continue;
    920 
    921 			/*
    922 		         * handle received data
    923 		         */
    924 			while (cy->cy_ibuf_rd_ptr != cy->cy_ibuf_wr_ptr) {
    925 				u_char line_stat;
    926 				int chr;
    927 
    928 				line_stat = cy->cy_ibuf_rd_ptr[0];
    929 				chr = cy->cy_ibuf_rd_ptr[1];
    930 
    931 				if (line_stat &
    932 				    (CD1400_RDSR_BREAK | CD1400_RDSR_FE))
    933 					chr |= TTY_FE;
    934 				if (line_stat & CD1400_RDSR_PE)
    935 					chr |= TTY_PE;
    936 
    937 				/*
    938 				 * on an overrun error the data is treated as
    939 				 * good just as it should be.
    940 				 */
    941 
    942 #ifdef CY_DEBUG
    943 				printf("%s: port %d ttyinput 0x%x\n",
    944 				    sc->sc_dev.dv_xname, port, chr);
    945 #endif
    946 
    947 				(*tp->t_linesw->l_rint) (chr, tp);
    948 
    949 				s = spltty();	/* really necessary? */
    950 				if ((cy->cy_ibuf_rd_ptr += 2) ==
    951 				    cy->cy_ibuf_end)
    952 					cy->cy_ibuf_rd_ptr = cy->cy_ibuf;
    953 				splx(s);
    954 
    955 #ifdef CY_DEBUG1
    956 				did_something = 1;
    957 #endif
    958 			}
    959 
    960 			/*
    961 			 * If we don't have any received data in ibuf and
    962 			 * CRTSCTS is on and RTS is turned off, it is time to
    963 			 * turn RTS back on
    964 			 */
    965 			if (ISSET(tp->t_cflag, CRTSCTS)) {
    966 				/*
    967 				 * we can't use cy_modem_control() here as it
    968 				 * doesn't change RTS if RTSCTS is on
    969 				 */
    970 				cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
    971 				    port & CD1400_CAR_CHAN);
    972 
    973 				if (cy->cy_clock == CY_CLOCK_60) {
    974 				  if ((cd_read_reg(sc, cy->cy_chip,
    975 				    CD1400_MSVR2) & CD1400_MSVR2_DTR) == 0) {
    976 					cd_write_reg(sc, cy->cy_chip,
    977 					CD1400_MSVR2,CD1400_MSVR2_DTR);
    978 #ifdef CY_DEBUG1
    979 					did_something = 1;
    980 #endif
    981 				  }
    982 				} else {
    983 				  if ((cd_read_reg(sc, cy->cy_chip,
    984 				    CD1400_MSVR1) & CD1400_MSVR1_RTS) == 0) {
    985 					cd_write_reg(sc, cy->cy_chip,
    986 					CD1400_MSVR1,CD1400_MSVR1_RTS);
    987 #ifdef CY_DEBUG1
    988 					did_something = 1;
    989 #endif
    990 				  }
    991 				}
    992 			}
    993 
    994 			/*
    995 		         * handle carrier changes
    996 		         */
    997 			s = spltty();
    998 			if (ISSET(cy->cy_flags, CY_F_CARRIER_CHANGED)) {
    999 				int             carrier;
   1000 
   1001 				CLR(cy->cy_flags, CY_F_CARRIER_CHANGED);
   1002 				splx(s);
   1003 
   1004 				carrier = ((cy->cy_carrier_stat &
   1005 				    CD1400_MSVR2_CD) != 0);
   1006 
   1007 #ifdef CY_DEBUG
   1008 				printf("cy_poll: carrier change "
   1009 				    "(card %d, port %d, carrier %d)\n",
   1010 				    card, port, carrier);
   1011 #endif
   1012 				if (CY_DIALOUT(tp->t_dev) == 0 &&
   1013 				    !(*tp->t_linesw->l_modem)(tp, carrier))
   1014 					cy_modem_control(sc, cy,
   1015 					    TIOCM_DTR, DMBIC);
   1016 
   1017 #ifdef CY_DEBUG1
   1018 				did_something = 1;
   1019 #endif
   1020 			} else
   1021 				splx(s);
   1022 
   1023 			s = spltty();
   1024 			if (ISSET(cy->cy_flags, CY_F_START)) {
   1025 				CLR(cy->cy_flags, CY_F_START);
   1026 				splx(s);
   1027 
   1028 				(*tp->t_linesw->l_start) (tp);
   1029 
   1030 #ifdef CY_DEBUG1
   1031 				did_something = 1;
   1032 #endif
   1033 			} else
   1034 				splx(s);
   1035 
   1036 			/* could move this to even upper level... */
   1037 			if (cy->cy_fifo_overruns) {
   1038 				cy->cy_fifo_overruns = 0;
   1039 				/*
   1040 				 * doesn't report overrun count, but
   1041 				 * shouldn't really matter
   1042 				 */
   1043 				log(LOG_WARNING, "%s: port %d fifo overrun\n",
   1044 				    sc->sc_dev.dv_xname, port);
   1045 			}
   1046 			if (cy->cy_ibuf_overruns) {
   1047 				cy->cy_ibuf_overruns = 0;
   1048 				log(LOG_WARNING, "%s: port %d ibuf overrun\n",
   1049 				    sc->sc_dev.dv_xname, port);
   1050 			}
   1051 		}		/* for(port...) */
   1052 #ifdef CY_DEBUG1
   1053 		if (did_something && counter >= 200)
   1054 			sc->sc_poll_count2++;
   1055 #endif
   1056 	} /* for(card...) */
   1057 
   1058 	counter = 0;
   1059 
   1060 out:
   1061 	callout_reset(&cy_poll_callout, 1, cy_poll, NULL);
   1062 }
   1063 
   1064 /*
   1065  * hardware interrupt routine
   1066  */
   1067 int
   1068 cy_intr(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 void
   1341 cy_enable_transmitter(struct cy_softc *sc, struct cy_port *cy)
   1342 {
   1343 	int s = spltty();
   1344 	cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
   1345 	    cy->cy_port_num & CD1400_CAR_CHAN);
   1346 	cd_write_reg(sc, cy->cy_chip, CD1400_SRER,
   1347 	    cd_read_reg(sc, cy->cy_chip, CD1400_SRER) | CD1400_SRER_TXRDY);
   1348 	splx(s);
   1349 }
   1350 
   1351 /*
   1352  * Execute a CD1400 channel command
   1353  */
   1354 void
   1355 cd1400_channel_cmd(struct cy_softc *sc, struct cy_port *cy, int cmd)
   1356 {
   1357 	u_int waitcnt = 5 * 8 * 1024;	/* approx 5 ms */
   1358 
   1359 #ifdef CY_DEBUG
   1360 	printf("c1400_channel_cmd cy %p command 0x%x\n", cy, cmd);
   1361 #endif
   1362 
   1363 	/* wait until cd1400 is ready to process a new command */
   1364 	while (cd_read_reg(sc, cy->cy_chip, CD1400_CCR) != 0 && waitcnt-- > 0);
   1365 
   1366 	if (waitcnt == 0)
   1367 		log(LOG_ERR, "%s: channel command timeout\n",
   1368 		    sc->sc_dev.dv_xname);
   1369 
   1370 	cd_write_reg(sc, cy->cy_chip, CD1400_CCR, cmd);
   1371 }
   1372 
   1373 /*
   1374  * Compute clock option register and baud rate register values
   1375  * for a given speed. Return 0 on success, -1 on failure.
   1376  *
   1377  * The error between requested and actual speed seems
   1378  * to be well within allowed limits (less than 3%)
   1379  * with every speed value between 50 and 150000 bps.
   1380  */
   1381 int
   1382 cy_speed(speed_t speed, int *cor, int *bpr, int cy_clock)
   1383 {
   1384 	int c, co, br;
   1385 
   1386 	if (speed < 50 || speed > 150000)
   1387 		return -1;
   1388 
   1389 	for (c = 0, co = 8; co <= 2048; co <<= 2, c++) {
   1390 		br = (cy_clock + (co * speed) / 2) / (co * speed);
   1391 		if (br < 0x100) {
   1392 			*bpr = br;
   1393 			*cor = c;
   1394 			return 0;
   1395 		}
   1396 	}
   1397 
   1398 	return -1;
   1399 }
   1400