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