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