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