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