Home | History | Annotate | Line # | Download | only in sbus
spif.c revision 1.17
      1 /*	$NetBSD: spif.c,v 1.17 2008/06/11 18:50:59 cegger Exp $	*/
      2 /*	$OpenBSD: spif.c,v 1.12 2003/10/03 16:44:51 miod Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1999-2002 Jason L. Wright (jason (at) thought.net)
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  * Effort sponsored in part by the Defense Advanced Research Projects
     30  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     31  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     32  *
     33  */
     34 
     35 /*
     36  * Driver for the SUNW,spif: 8 serial, 1 parallel sbus board
     37  * based heavily on Iain Hibbert's driver for the MAGMA cards
     38  */
     39 
     40 /* Ported to NetBSD 2.0 by Hauke Fath */
     41 
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: spif.c,v 1.17 2008/06/11 18:50:59 cegger Exp $");
     45 
     46 #include "spif.h"
     47 #if NSPIF > 0
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/proc.h>
     52 #include <sys/device.h>
     53 #include <sys/file.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/malloc.h>
     56 #include <sys/tty.h>
     57 #include <sys/time.h>
     58 #include <sys/kernel.h>
     59 #include <sys/syslog.h>
     60 #include <sys/conf.h>
     61 #include <sys/errno.h>
     62 #include <sys/kauth.h>
     63 #include <sys/intr.h>
     64 
     65 #include <sys/bus.h>
     66 #include <machine/autoconf.h>
     67 #include <machine/promlib.h>
     68 
     69 #include <dev/sbus/sbusvar.h>
     70 
     71 #include <dev/sbus/spifvar.h>
     72 #include <dev/sbus/spifreg.h>
     73 
     74 
     75 /* Autoconfig stuff */
     76 
     77 CFATTACH_DECL(spif, sizeof(struct spif_softc),
     78     spif_match, spif_attach, NULL, NULL);
     79 
     80 CFATTACH_DECL(stty, sizeof(struct stty_softc),
     81     stty_match, stty_attach, NULL, NULL);
     82 
     83 CFATTACH_DECL(sbpp, sizeof(struct sbpp_softc),
     84     sbpp_match, sbpp_attach, NULL, NULL);
     85 
     86 extern struct cfdriver spif_cd;
     87 extern struct cfdriver stty_cd;
     88 extern struct cfdriver sbpp_cd;
     89 
     90 dev_type_open(stty_open);
     91 dev_type_close(stty_close);
     92 dev_type_read(stty_read);
     93 dev_type_write(stty_write);
     94 dev_type_ioctl(stty_ioctl);
     95 dev_type_stop(stty_stop);
     96 dev_type_tty(stty_tty);
     97 dev_type_poll(stty_poll);
     98 
     99 const struct cdevsw stty_cdevsw = {
    100 	stty_open, stty_close, stty_read, stty_write, stty_ioctl,
    101 	stty_stop, stty_tty, stty_poll, nommap, ttykqfilter, D_TTY
    102 };
    103 
    104 dev_type_open(sbpp_open);
    105 dev_type_close(sbpp_close);
    106 dev_type_read(sbpp_read);
    107 dev_type_write(sbpp_write);
    108 dev_type_ioctl(sbpp_ioctl);
    109 dev_type_poll(sbpp_poll);
    110 
    111 const struct cdevsw sbpp_cdevsw = {
    112 	sbpp_open, sbpp_close, sbpp_read, sbpp_write, sbpp_ioctl,
    113 	nostop, notty, sbpp_poll, nommap, nokqfilter,
    114 };
    115 
    116 
    117 /* normal STC access */
    118 #define	STC_WRITE(sc,r,v)	\
    119     bus_space_write_1((sc)->sc_bustag, (sc)->sc_stch, (r), (v))
    120 #define	STC_READ(sc,r)		\
    121     bus_space_read_1((sc)->sc_bustag, (sc)->sc_stch, (r))
    122 
    123 /* IACK STC access */
    124 #define	ISTC_WRITE(sc,r,v)	\
    125     bus_space_write_1((sc)->sc_bustag, (sc)->sc_istch, (r), (v))
    126 #define	ISTC_READ(sc,r)		\
    127     bus_space_read_1((sc)->sc_bustag, (sc)->sc_istch, (r))
    128 
    129 /* PPC access */
    130 #define	PPC_WRITE(sc,r,v)	\
    131     bus_space_write_1((sc)->sc_bustag, (sc)->sc_ppch, (r), (v))
    132 #define	PPC_READ(sc,r)		\
    133     bus_space_read_1((sc)->sc_bustag, (sc)->sc_ppch, (r))
    134 
    135 #define	DTR_WRITE(sc,port,v)						\
    136     do {								\
    137 	sc->sc_ttys->sc_port[(port)].sp_dtr = v;			\
    138 	bus_space_write_1((sc)->sc_bustag,				\
    139 	    sc->sc_dtrh, port, (v == 0) ? 1 : 0);			\
    140     } while (0)
    141 
    142 #define	DTR_READ(sc,port)	((sc)->sc_ttys->sc_port[(port)].sp_dtr)
    143 
    144 
    145 int
    146 spif_match(parent, vcf, aux)
    147 	struct device *parent;
    148 	struct cfdata *vcf;
    149 	void *aux;
    150 {
    151 	struct sbus_attach_args *sa = aux;
    152 
    153 	if (strcmp(vcf->cf_name, sa->sa_name) &&
    154 	    strcmp("SUNW,spif", sa->sa_name))
    155 		return (0);
    156 	return (1);
    157 }
    158 
    159 void
    160 spif_attach(parent, self, aux)
    161 	struct device *parent, *self;
    162 	void *aux;
    163 {
    164 	struct spif_softc *sc = (struct spif_softc *)self;
    165 	struct sbus_attach_args *sa = aux;
    166 
    167 	if (sa->sa_nintr != 2) {
    168 		printf(": expected %d interrupts, got %d\n", 2, sa->sa_nintr);
    169 		return;
    170 	}
    171 
    172 	if (sa->sa_nreg != 1) {
    173 		printf(": expected %d registers, got %d\n", 1, sa->sa_nreg);
    174 		return;
    175 	}
    176 
    177 	sc->sc_bustag = sa->sa_bustag;
    178 	if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    179 	    sa->sa_offset, sa->sa_size,
    180 	    0, &sc->sc_regh) != 0) {
    181 		printf(": can't map registers\n");
    182 		return;
    183 	}
    184 
    185 	if (bus_space_subregion(sc->sc_bustag, sc->sc_regh,
    186 	    DTR_REG_OFFSET, DTR_REG_LEN, &sc->sc_dtrh) != 0) {
    187 		printf(": can't map dtr regs\n");
    188 		goto fail_unmapregs;
    189 	}
    190 
    191 	if (bus_space_subregion(sc->sc_bustag, sc->sc_regh,
    192 	    STC_REG_OFFSET, STC_REG_LEN, &sc->sc_stch) != 0) {
    193 		printf(": can't map dtr regs\n");
    194 		goto fail_unmapregs;
    195 	}
    196 
    197 	if (bus_space_subregion(sc->sc_bustag, sc->sc_regh,
    198 	    ISTC_REG_OFFSET, ISTC_REG_LEN, &sc->sc_istch) != 0) {
    199 		printf(": can't map dtr regs\n");
    200 		goto fail_unmapregs;
    201 	}
    202 
    203 	if (bus_space_subregion(sc->sc_bustag, sc->sc_regh,
    204 	    PPC_REG_OFFSET, PPC_REG_LEN, &sc->sc_ppch) != 0) {
    205 		printf(": can't map dtr regs\n");
    206 		goto fail_unmapregs;
    207 	}
    208 
    209 	sc->sc_ppcih = bus_intr_establish(sa->sa_bustag,
    210 	    sa->sa_intr[PARALLEL_INTR].oi_pri, IPL_SERIAL, spif_ppcintr, sc);
    211 	if (sc->sc_ppcih == NULL) {
    212 		printf(": failed to establish ppc interrupt\n");
    213 		goto fail_unmapregs;
    214 	}
    215 
    216 	sc->sc_stcih = bus_intr_establish(sa->sa_bustag,
    217 	    sa->sa_intr[SERIAL_INTR].oi_pri, IPL_SERIAL, spif_stcintr, sc);
    218 	if (sc->sc_stcih == NULL) {
    219 		printf(": failed to establish stc interrupt\n");
    220 		goto fail_unmapregs;
    221 	}
    222 
    223 	sc->sc_softih = softint_establish(SOFTINT_SERIAL, spif_softintr, sc);
    224 	if (sc->sc_softih == NULL) {
    225 		printf(": can't get soft intr\n");
    226 		goto fail_unmapregs;
    227 	}
    228 
    229 	sc->sc_node = sa->sa_node;
    230 
    231 	sc->sc_rev = prom_getpropint(sc->sc_node, "revlev", 0);
    232 
    233 	sc->sc_osc = prom_getpropint(sc->sc_node, "verosc", 0);
    234 	switch (sc->sc_osc) {
    235 	case SPIF_OSC10:
    236 		sc->sc_osc = 10000000;
    237 		break;
    238 	case SPIF_OSC9:
    239 	default:
    240 		sc->sc_osc = 9830400;
    241 		break;
    242 	}
    243 
    244 	sc->sc_nser = 8;
    245 	sc->sc_npar = 1;
    246 
    247 	sc->sc_rev2 = STC_READ(sc, STC_GFRCR);
    248 	STC_WRITE(sc, STC_GSVR, 0);
    249 
    250 	stty_write_ccr(sc, CD180_CCR_CMD_RESET | CD180_CCR_RESETALL);
    251 	while (STC_READ(sc, STC_GSVR) != 0xff);
    252 	while (STC_READ(sc, STC_GFRCR) != sc->sc_rev2);
    253 
    254 	STC_WRITE(sc, STC_PPRH, CD180_PPRH);
    255 	STC_WRITE(sc, STC_PPRL, CD180_PPRL);
    256 	STC_WRITE(sc, STC_MSMR, SPIF_MSMR);
    257 	STC_WRITE(sc, STC_TSMR, SPIF_TSMR);
    258 	STC_WRITE(sc, STC_RSMR, SPIF_RSMR);
    259 	STC_WRITE(sc, STC_GSVR, 0);
    260 	STC_WRITE(sc, STC_GSCR1, 0);
    261 	STC_WRITE(sc, STC_GSCR2, 0);
    262 	STC_WRITE(sc, STC_GSCR3, 0);
    263 
    264 	printf(": rev %x chiprev %x osc %sMHz\n",
    265 	    sc->sc_rev, sc->sc_rev2, clockfreq(sc->sc_osc));
    266 
    267 	(void)config_found(self, stty_match, NULL);
    268 	(void)config_found(self, sbpp_match, NULL);
    269 
    270 	return;
    271 
    272 fail_unmapregs:
    273 	bus_space_unmap(sa->sa_bustag, sc->sc_regh, sa->sa_size);
    274 }
    275 
    276 int
    277 stty_match(parent, vcf, aux)
    278 	struct device *parent;
    279 	struct cfdata *vcf;
    280 	void *aux;
    281 {
    282 	struct spif_softc *sc = (struct spif_softc *)parent;
    283 
    284 	return (aux == stty_match && sc->sc_ttys == NULL);
    285 }
    286 
    287 void
    288 stty_attach(parent, dev, aux)
    289 	struct device *parent, *dev;
    290 	void *aux;
    291 {
    292 	struct spif_softc *sc = (struct spif_softc *)parent;
    293 	struct stty_softc *ssc = (struct stty_softc *)dev;
    294 	int port;
    295 
    296 	sc->sc_ttys = ssc;
    297 
    298 	for (port = 0; port < sc->sc_nser; port++) {
    299 		struct stty_port *sp = &ssc->sc_port[port];
    300 		struct tty *tp;
    301 
    302 		DTR_WRITE(sc, port, 0);
    303 
    304 		tp = ttymalloc();
    305 
    306 		tp->t_oproc = stty_start;
    307 		tp->t_param = stty_param;
    308 
    309 		sp->sp_tty = tp;
    310 		sp->sp_sc = sc;
    311 		sp->sp_channel = port;
    312 
    313 		sp->sp_rbuf = malloc(STTY_RBUF_SIZE, M_DEVBUF, M_NOWAIT);
    314 		if(sp->sp_rbuf == NULL)
    315 			break;
    316 
    317 		sp->sp_rend = sp->sp_rbuf + STTY_RBUF_SIZE;
    318 	}
    319 
    320 	ssc->sc_nports = port;
    321 
    322 	printf(": %d tty%s\n", port, port == 1 ? "" : "s");
    323 }
    324 
    325 int
    326 stty_open(dev_t dev, int flags, int mode, struct lwp *l)
    327 {
    328 	struct spif_softc *csc;
    329 	struct stty_softc *sc;
    330 	struct stty_port *sp;
    331 	struct tty *tp;
    332 	int card = SPIF_CARD(dev);
    333 	int port = SPIF_PORT(dev);
    334 
    335 	sc = device_lookup_private(&stty_cd, card);
    336 	csc = device_lookup_private(&spif_cd, card);
    337 	if (sc == NULL || csc == NULL)
    338 		return (ENXIO);
    339 
    340 	if (port >= sc->sc_nports)
    341 		return (ENXIO);
    342 
    343 	sp = &sc->sc_port[port];
    344 	tp = sp->sp_tty;
    345 	tp->t_dev = dev;
    346 
    347 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    348 		return (EBUSY);
    349 
    350 	mutex_spin_enter(&tty_lock);
    351 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    352 		ttychars(tp);
    353 		tp->t_iflag = TTYDEF_IFLAG;
    354 		tp->t_oflag = TTYDEF_OFLAG;
    355 		tp->t_cflag = TTYDEF_CFLAG;
    356 		if (ISSET(sp->sp_openflags, TIOCFLAG_CLOCAL))
    357 			SET(tp->t_cflag, CLOCAL);
    358 		if (ISSET(sp->sp_openflags, TIOCFLAG_CRTSCTS))
    359 			SET(tp->t_cflag, CRTSCTS);
    360 		if (ISSET(sp->sp_openflags, TIOCFLAG_MDMBUF))
    361 			SET(tp->t_cflag, MDMBUF);
    362 		tp->t_lflag = TTYDEF_LFLAG;
    363 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    364 
    365 		sp->sp_rput = sp->sp_rget = sp->sp_rbuf;
    366 
    367 		STC_WRITE(csc, STC_CAR, sp->sp_channel);
    368 		stty_write_ccr(csc, CD180_CCR_CMD_RESET|CD180_CCR_RESETCHAN);
    369 		STC_WRITE(csc, STC_CAR, sp->sp_channel);
    370 
    371 		stty_param(tp, &tp->t_termios);
    372 
    373 		ttsetwater(tp);
    374 
    375 		STC_WRITE(csc, STC_SRER, CD180_SRER_CD | CD180_SRER_RXD);
    376 
    377 		if (ISSET(sp->sp_openflags, TIOCFLAG_SOFTCAR) || sp->sp_carrier)
    378 			SET(tp->t_state, TS_CARR_ON);
    379 		else
    380 			CLR(tp->t_state, TS_CARR_ON);
    381 	}
    382 
    383 	if (!ISSET(flags, O_NONBLOCK)) {
    384 		while (!ISSET(tp->t_cflag, CLOCAL) &&
    385 		    !ISSET(tp->t_state, TS_CARR_ON)) {
    386 			int error;
    387 			error = ttysleep(tp, &tp->t_rawcv, true, 0);
    388 			if (error != 0) {
    389 				mutex_spin_exit(&tty_lock);
    390 				return (error);
    391 			}
    392 		}
    393 	}
    394 	mutex_spin_exit(&tty_lock);
    395 
    396 	return ((*tp->t_linesw->l_open)(dev, tp));
    397 }
    398 
    399 int
    400 stty_close(dev_t dev, int flags, int mode, struct lwp *l)
    401 {
    402 	struct stty_softc *sc = device_lookup_private(&stty_cd, SPIF_CARD(dev));
    403 	struct stty_port *sp = &sc->sc_port[SPIF_PORT(dev)];
    404 	struct spif_softc *csc = sp->sp_sc;
    405 	struct tty *tp = sp->sp_tty;
    406 	int port = SPIF_PORT(dev);
    407 	int s;
    408 
    409 	(*tp->t_linesw->l_close)(tp, flags);
    410 	s = spltty();
    411 
    412 	if (ISSET(tp->t_cflag, HUPCL) || !ISSET(tp->t_state, TS_ISOPEN)) {
    413 		stty_modem_control(sp, 0, DMSET);
    414 		STC_WRITE(csc, STC_CAR, port);
    415 		STC_WRITE(csc, STC_CCR,
    416 		    CD180_CCR_CMD_RESET|CD180_CCR_RESETCHAN);
    417 	}
    418 
    419 	splx(s);
    420 	ttyclose(tp);
    421 	return (0);
    422 }
    423 
    424 int
    425 stty_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    426 {
    427 	struct stty_softc *stc = device_lookup_private(&stty_cd, SPIF_CARD(dev));
    428 	struct stty_port *sp = &stc->sc_port[SPIF_PORT(dev)];
    429 	struct spif_softc *sc = sp->sp_sc;
    430 	struct tty *tp = sp->sp_tty;
    431 	int error;
    432 
    433 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flags, l);
    434 	if (error >= 0)
    435 		return (error);
    436 
    437 	error = ttioctl(tp, cmd, data, flags, l);
    438 	if (error >= 0)
    439 		return (error);
    440 
    441 	error = 0;
    442 
    443 	switch (cmd) {
    444 	case TIOCSBRK:
    445 		SET(sp->sp_flags, STTYF_SET_BREAK);
    446 		STC_WRITE(sc, STC_CAR, sp->sp_channel);
    447 		STC_WRITE(sc, STC_SRER,
    448 		    STC_READ(sc, STC_SRER) | CD180_SRER_TXD);
    449 		break;
    450 	case TIOCCBRK:
    451 		SET(sp->sp_flags, STTYF_CLR_BREAK);
    452 		STC_WRITE(sc, STC_CAR, sp->sp_channel);
    453 		STC_WRITE(sc, STC_SRER,
    454 		    STC_READ(sc, STC_SRER) | CD180_SRER_TXD);
    455 		break;
    456 	case TIOCSDTR:
    457 		stty_modem_control(sp, TIOCM_DTR, DMBIS);
    458 		break;
    459 	case TIOCCDTR:
    460 		stty_modem_control(sp, TIOCM_DTR, DMBIC);
    461 		break;
    462 	case TIOCMBIS:
    463 		stty_modem_control(sp, *((int *)data), DMBIS);
    464 		break;
    465 	case TIOCMBIC:
    466 		stty_modem_control(sp, *((int *)data), DMBIC);
    467 		break;
    468 	case TIOCMGET:
    469 		*((int *)data) = stty_modem_control(sp, 0, DMGET);
    470 		break;
    471 	case TIOCMSET:
    472 		stty_modem_control(sp, *((int *)data), DMSET);
    473 		break;
    474 	case TIOCGFLAGS:
    475 		*((int *)data) = sp->sp_openflags;
    476 		break;
    477 	case TIOCSFLAGS:
    478 		if (kauth_authorize_device_tty(l->l_cred,
    479 		    KAUTH_DEVICE_TTY_PRIVSET, tp))
    480 			error = EPERM;
    481 		else
    482 			sp->sp_openflags = *((int *)data) &
    483 			    (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
    484 			     TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
    485 		break;
    486 	default:
    487 		error = ENOTTY;
    488 	}
    489 
    490 	return (error);
    491 }
    492 
    493 int
    494 stty_modem_control(sp, bits, how)
    495 	struct stty_port *sp;
    496 	int bits, how;
    497 {
    498 	struct spif_softc *csc = sp->sp_sc;
    499 	struct tty *tp = sp->sp_tty;
    500 	int s, msvr;
    501 
    502 	s = spltty();
    503 	STC_WRITE(csc, STC_CAR, sp->sp_channel);
    504 
    505 	switch (how) {
    506 	case DMGET:
    507 		bits = TIOCM_LE;
    508 		if (DTR_READ(csc, sp->sp_channel))
    509 			bits |= TIOCM_DTR;
    510 		msvr = STC_READ(csc, STC_MSVR);
    511 		if (ISSET(msvr, CD180_MSVR_DSR))
    512 			bits |= TIOCM_DSR;
    513 		if (ISSET(msvr, CD180_MSVR_CD))
    514 			bits |= TIOCM_CD;
    515 		if (ISSET(msvr, CD180_MSVR_CTS))
    516 			bits |= TIOCM_CTS;
    517 		if (ISSET(msvr, CD180_MSVR_RTS))
    518 			bits |= TIOCM_RTS;
    519 		break;
    520 	case DMSET:
    521 		DTR_WRITE(csc, sp->sp_channel, ISSET(bits, TIOCM_DTR) ? 1 : 0);
    522 		if (ISSET(bits, TIOCM_RTS))
    523 			STC_WRITE(csc, STC_MSVR,
    524 			    STC_READ(csc, STC_MSVR) & (~CD180_MSVR_RTS));
    525 		else
    526 			STC_WRITE(csc, STC_MSVR,
    527 			    STC_READ(csc, STC_MSVR) | CD180_MSVR_RTS);
    528 		break;
    529 	case DMBIS:
    530 		if (ISSET(bits, TIOCM_DTR))
    531 			DTR_WRITE(csc, sp->sp_channel, 1);
    532 		if (ISSET(bits, TIOCM_RTS) && !ISSET(tp->t_cflag, CRTSCTS))
    533 			STC_WRITE(csc, STC_MSVR,
    534 			    STC_READ(csc, STC_MSVR) & (~CD180_MSVR_RTS));
    535 		break;
    536 	case DMBIC:
    537 		if (ISSET(bits, TIOCM_DTR))
    538 			DTR_WRITE(csc, sp->sp_channel, 0);
    539 		if (ISSET(bits, TIOCM_RTS))
    540 			STC_WRITE(csc, STC_MSVR,
    541 			    STC_READ(csc, STC_MSVR) | CD180_MSVR_RTS);
    542 		break;
    543 	}
    544 
    545 	splx(s);
    546 	return (bits);
    547 }
    548 
    549 int
    550 stty_param(struct tty *tp, struct termios *t)
    551 {
    552 	struct stty_softc *st = device_lookup_private(&stty_cd, SPIF_CARD(tp->t_dev));
    553 	struct stty_port *sp = &st->sc_port[SPIF_PORT(tp->t_dev)];
    554 	struct spif_softc *sc = sp->sp_sc;
    555 	u_int8_t rbprl, rbprh, tbprl, tbprh;
    556 	int s, opt;
    557 
    558 	if (t->c_ospeed &&
    559 	    stty_compute_baud(t->c_ospeed, sc->sc_osc, &tbprl, &tbprh))
    560 		return (EINVAL);
    561 
    562 	if (t->c_ispeed &&
    563 	    stty_compute_baud(t->c_ispeed, sc->sc_osc, &rbprl, &rbprh))
    564 		return (EINVAL);
    565 
    566 	s = spltty();
    567 
    568 	/* hang up line if ospeed is zero, otherwise raise DTR */
    569 	stty_modem_control(sp, TIOCM_DTR,
    570 	    (t->c_ospeed == 0 ? DMBIC : DMBIS));
    571 
    572 	STC_WRITE(sc, STC_CAR, sp->sp_channel);
    573 
    574 	opt = 0;
    575 	if (ISSET(t->c_cflag, PARENB)) {
    576 		opt |= CD180_COR1_PARMODE_NORMAL;
    577 		opt |= (ISSET(t->c_cflag, PARODD) ?
    578 				CD180_COR1_ODDPAR :
    579 				CD180_COR1_EVENPAR);
    580 	}
    581 	else
    582 		opt |= CD180_COR1_PARMODE_NO;
    583 
    584 	if (!ISSET(t->c_iflag, INPCK))
    585 		opt |= CD180_COR1_IGNPAR;
    586 
    587 	if (ISSET(t->c_cflag, CSTOPB))
    588 		opt |= CD180_COR1_STOP2;
    589 
    590 	switch (t->c_cflag & CSIZE) {
    591 	case CS5:
    592 		opt |= CD180_COR1_CS5;
    593 		break;
    594 	case CS6:
    595 		opt |= CD180_COR1_CS6;
    596 		break;
    597 	case CS7:
    598 		opt |= CD180_COR1_CS7;
    599 		break;
    600 	default:
    601 		opt |= CD180_COR1_CS8;
    602 		break;
    603 	}
    604 	STC_WRITE(sc, STC_COR1, opt);
    605 	stty_write_ccr(sc, CD180_CCR_CMD_COR|CD180_CCR_CORCHG1);
    606 
    607 	opt = CD180_COR2_ETC;
    608 	if (ISSET(t->c_cflag, CRTSCTS))
    609 		opt |= CD180_COR2_CTSAE;
    610 	STC_WRITE(sc, STC_COR2, opt);
    611 	stty_write_ccr(sc, CD180_CCR_CMD_COR|CD180_CCR_CORCHG2);
    612 
    613 	STC_WRITE(sc, STC_COR3, STTY_RX_FIFO_THRESHOLD);
    614 	stty_write_ccr(sc, CD180_CCR_CMD_COR|CD180_CCR_CORCHG3);
    615 
    616 	STC_WRITE(sc, STC_SCHR1, 0x11);
    617 	STC_WRITE(sc, STC_SCHR2, 0x13);
    618 	STC_WRITE(sc, STC_SCHR3, 0x11);
    619 	STC_WRITE(sc, STC_SCHR4, 0x13);
    620 	STC_WRITE(sc, STC_RTPR, 0x12);
    621 
    622 	STC_WRITE(sc, STC_MCOR1, CD180_MCOR1_CDZD | STTY_RX_DTR_THRESHOLD);
    623 	STC_WRITE(sc, STC_MCOR2, CD180_MCOR2_CDOD);
    624 	STC_WRITE(sc, STC_MCR, 0);
    625 
    626 	if (t->c_ospeed) {
    627 		STC_WRITE(sc, STC_TBPRH, tbprh);
    628 		STC_WRITE(sc, STC_TBPRL, tbprl);
    629 	}
    630 
    631 	if (t->c_ispeed) {
    632 		STC_WRITE(sc, STC_RBPRH, rbprh);
    633 		STC_WRITE(sc, STC_RBPRL, rbprl);
    634 	}
    635 
    636 	stty_write_ccr(sc, CD180_CCR_CMD_CHAN |
    637 	    CD180_CCR_CHAN_TXEN | CD180_CCR_CHAN_RXEN);
    638 
    639 	sp->sp_carrier = STC_READ(sc, STC_MSVR) & CD180_MSVR_CD;
    640 
    641 	splx(s);
    642 	return (0);
    643 }
    644 
    645 int
    646 stty_read(dev_t dev, struct uio *uio, int flags)
    647 {
    648 	struct stty_softc *sc = device_lookup_private(&stty_cd, SPIF_CARD(dev));
    649 	struct stty_port *sp = &sc->sc_port[SPIF_PORT(dev)];
    650 	struct tty *tp = sp->sp_tty;
    651 
    652 	return ((*tp->t_linesw->l_read)(tp, uio, flags));
    653 }
    654 
    655 int
    656 stty_write(dev_t dev, struct uio *uio, int flags)
    657 {
    658 	struct stty_softc *sc = device_lookup_private(&stty_cd, SPIF_CARD(dev));
    659 	struct stty_port *sp = &sc->sc_port[SPIF_PORT(dev)];
    660 	struct tty *tp = sp->sp_tty;
    661 
    662 	return ((*tp->t_linesw->l_write)(tp, uio, flags));
    663 }
    664 
    665 int
    666 stty_poll(dev_t dev, int events, struct lwp *l)
    667 {
    668 	struct stty_softc *sc = device_lookup_private(&stty_cd, SPIF_CARD(dev));
    669 	struct stty_port *sp = &sc->sc_port[SPIF_PORT(dev)];
    670 	struct tty *tp = sp->sp_tty;
    671 
    672 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    673 }
    674 
    675 struct tty *
    676 stty_tty(dev_t dev)
    677 {
    678 	struct stty_softc *sc = device_lookup_private(&stty_cd, SPIF_CARD(dev));
    679 	struct stty_port *sp = &sc->sc_port[SPIF_PORT(dev)];
    680 
    681 	return (sp->sp_tty);
    682 }
    683 
    684 void
    685 stty_stop(struct tty *tp, int flags)
    686 {
    687 	struct stty_softc *sc = device_lookup_private(&stty_cd, SPIF_CARD(tp->t_dev));
    688 	struct stty_port *sp = &sc->sc_port[SPIF_PORT(tp->t_dev)];
    689 	int s;
    690 
    691 	s = spltty();
    692 	if (ISSET(tp->t_state, TS_BUSY)) {
    693 		if (!ISSET(tp->t_state, TS_TTSTOP))
    694 			SET(tp->t_state, TS_FLUSH);
    695 		SET(sp->sp_flags, STTYF_STOP);
    696 	}
    697 	splx(s);
    698 }
    699 
    700 void
    701 stty_start(struct tty *tp)
    702 {
    703 	struct stty_softc *stc = device_lookup_private(&stty_cd, SPIF_CARD(tp->t_dev));
    704 	struct stty_port *sp = &stc->sc_port[SPIF_PORT(tp->t_dev)];
    705 	struct spif_softc *sc = sp->sp_sc;
    706 	int s;
    707 
    708 	s = spltty();
    709 
    710 	if (!ISSET(tp->t_state, TS_TTSTOP | TS_TIMEOUT | TS_BUSY)) {
    711 		if (ttypull(tp)) {
    712 			sp->sp_txc = ndqb(&tp->t_outq, 0);
    713 			sp->sp_txp = tp->t_outq.c_cf;
    714 			SET(tp->t_state, TS_BUSY);
    715 			STC_WRITE(sc, STC_CAR, sp->sp_channel);
    716 			STC_WRITE(sc, STC_SRER,
    717 			    STC_READ(sc, STC_SRER) | CD180_SRER_TXD);
    718 		}
    719 	}
    720 
    721 	splx(s);
    722 }
    723 
    724 int
    725 spif_stcintr_rxexception(sc, needsoftp)
    726 	struct spif_softc *sc;
    727 	int *needsoftp;
    728 {
    729 	struct stty_port *sp;
    730 	u_int8_t channel, *ptr;
    731 
    732 	channel = CD180_GSCR_CHANNEL(STC_READ(sc, STC_GSCR1));
    733 	sp = &sc->sc_ttys->sc_port[channel];
    734 	ptr = sp->sp_rput;
    735 	*ptr++ = STC_READ(sc, STC_RCSR);
    736 	*ptr++ = STC_READ(sc, STC_RDR);
    737 	if (ptr == sp->sp_rend)
    738 		ptr = sp->sp_rbuf;
    739 	if (ptr == sp->sp_rget) {
    740 		if (ptr == sp->sp_rbuf)
    741 			ptr = sp->sp_rend;
    742 		ptr -= 2;
    743 		SET(sp->sp_flags, STTYF_RING_OVERFLOW);
    744 	}
    745 	STC_WRITE(sc, STC_EOSRR, 0);
    746 	*needsoftp = 1;
    747 	sp->sp_rput = ptr;
    748 	return (1);
    749 }
    750 
    751 int
    752 spif_stcintr_rx(sc, needsoftp)
    753 	struct spif_softc *sc;
    754 	int *needsoftp;
    755 {
    756 	struct stty_port *sp;
    757 	u_int8_t channel, *ptr, cnt, rcsr;
    758 	int i;
    759 
    760 	channel = CD180_GSCR_CHANNEL(STC_READ(sc, STC_GSCR1));
    761 	sp = &sc->sc_ttys->sc_port[channel];
    762 	ptr = sp->sp_rput;
    763 	cnt = STC_READ(sc, STC_RDCR);
    764 	for (i = 0; i < cnt; i++) {
    765 		*ptr++ = 0;
    766 		rcsr = STC_READ(sc, STC_RCSR);
    767 		*ptr++ = STC_READ(sc, STC_RDR);
    768 		if (ptr == sp->sp_rend)
    769 			ptr = sp->sp_rbuf;
    770 		if (ptr == sp->sp_rget) {
    771 			if (ptr == sp->sp_rbuf)
    772 				ptr = sp->sp_rend;
    773 			ptr -= 2;
    774 			SET(sp->sp_flags, STTYF_RING_OVERFLOW);
    775 			break;
    776 		}
    777 	}
    778 	STC_WRITE(sc, STC_EOSRR, 0);
    779 	if (cnt) {
    780 		*needsoftp = 1;
    781 		sp->sp_rput = ptr;
    782 	}
    783 	return (1);
    784 }
    785 
    786 int
    787 spif_stcintr_tx(sc, needsoftp)
    788 	struct spif_softc *sc;
    789 	int *needsoftp;
    790 {
    791 	struct stty_port *sp;
    792 	u_int8_t channel, ch;
    793 	int cnt = 0;
    794 
    795 	channel = CD180_GSCR_CHANNEL(STC_READ(sc, STC_GSCR1));
    796 	sp = &sc->sc_ttys->sc_port[channel];
    797 	if (!ISSET(sp->sp_flags, STTYF_STOP)) {
    798 		if (ISSET(sp->sp_flags, STTYF_SET_BREAK)) {
    799 			STC_WRITE(sc, STC_TDR, 0);
    800 			STC_WRITE(sc, STC_TDR, 0x81);
    801 			CLR(sp->sp_flags, STTYF_SET_BREAK);
    802 			cnt += 2;
    803 		}
    804 		if (ISSET(sp->sp_flags, STTYF_CLR_BREAK)) {
    805 			STC_WRITE(sc, STC_TDR, 0);
    806 			STC_WRITE(sc, STC_TDR, 0x83);
    807 			CLR(sp->sp_flags, STTYF_CLR_BREAK);
    808 			cnt += 2;
    809 		}
    810 
    811 		while (sp->sp_txc > 0 && cnt < (CD180_TX_FIFO_SIZE-1)) {
    812 			ch = *sp->sp_txp;
    813 			sp->sp_txc--;
    814 			sp->sp_txp++;
    815 
    816 			if (ch == 0) {
    817 				STC_WRITE(sc, STC_TDR, ch);
    818 				cnt++;
    819 			}
    820 			STC_WRITE(sc, STC_TDR, ch);
    821 			cnt++;
    822 		}
    823 	}
    824 
    825 	if (sp->sp_txc == 0 ||
    826 	    ISSET(sp->sp_flags, STTYF_STOP)) {
    827 		STC_WRITE(sc, STC_SRER, STC_READ(sc, STC_SRER) &
    828 		    (~CD180_SRER_TXD));
    829 		CLR(sp->sp_flags, STTYF_STOP);
    830 		SET(sp->sp_flags, STTYF_DONE);
    831 		*needsoftp = 1;
    832 	}
    833 
    834 	STC_WRITE(sc, STC_EOSRR, 0);
    835 
    836 	return (1);
    837 }
    838 
    839 int
    840 spif_stcintr_mx(sc, needsoftp)
    841 	struct spif_softc *sc;
    842 	int *needsoftp;
    843 {
    844 	struct stty_port *sp;
    845 	u_int8_t channel, mcr;
    846 
    847 	channel = CD180_GSCR_CHANNEL(STC_READ(sc, STC_GSCR1));
    848 	sp = &sc->sc_ttys->sc_port[channel];
    849 	mcr = STC_READ(sc, STC_MCR);
    850 	if (mcr & CD180_MCR_CD) {
    851 		SET(sp->sp_flags, STTYF_CDCHG);
    852 		*needsoftp = 1;
    853 	}
    854 	STC_WRITE(sc, STC_MCR, 0);
    855 	STC_WRITE(sc, STC_EOSRR, 0);
    856 	return (1);
    857 }
    858 
    859 int
    860 spif_stcintr(vsc)
    861 	void *vsc;
    862 {
    863 	struct spif_softc *sc = (struct spif_softc *)vsc;
    864 	int needsoft = 0, r = 0, i;
    865 	u_int8_t ar;
    866 
    867 	for (i = 0; i < 8; i++) {
    868 		ar = ISTC_READ(sc, STC_RRAR) & CD180_GSVR_IMASK;
    869 		if (ar == CD180_GSVR_RXGOOD)
    870 			r |= spif_stcintr_rx(sc, &needsoft);
    871 		else if (ar == CD180_GSVR_RXEXCEPTION)
    872 			r |= spif_stcintr_rxexception(sc, &needsoft);
    873 	}
    874 
    875 	for (i = 0; i < 8; i++) {
    876 		ar = ISTC_READ(sc, STC_TRAR) & CD180_GSVR_IMASK;
    877 		if (ar == CD180_GSVR_TXDATA)
    878 			r |= spif_stcintr_tx(sc, &needsoft);
    879 	}
    880 
    881 	for (i = 0; i < 8; i++) {
    882 		ar = ISTC_READ(sc, STC_MRAR) & CD180_GSVR_IMASK;
    883 		if (ar == CD180_GSVR_STATCHG)
    884 			r |= spif_stcintr_mx(sc, &needsoft);
    885 	}
    886 
    887 	if (needsoft)
    888 		softint_schedule(sc->sc_softih);
    889 	return (r);
    890 }
    891 
    892 void
    893 spif_softintr(vsc)
    894 	void *vsc;
    895 {
    896 	struct spif_softc *sc = (struct spif_softc *)vsc;
    897 	struct stty_softc *stc = sc->sc_ttys;
    898 	int r = 0, i, data, s, flags;
    899 	u_int8_t stat, msvr;
    900 	struct stty_port *sp;
    901 	struct tty *tp;
    902 
    903 	if (stc != NULL) {
    904 		for (i = 0; i < stc->sc_nports; i++) {
    905 			sp = &stc->sc_port[i];
    906 			tp = sp->sp_tty;
    907 
    908 			if (!ISSET(tp->t_state, TS_ISOPEN))
    909 				continue;
    910 
    911 			while (sp->sp_rget != sp->sp_rput) {
    912 				stat = sp->sp_rget[0];
    913 				data = sp->sp_rget[1];
    914 				sp->sp_rget += 2;
    915 				if (sp->sp_rget == sp->sp_rend)
    916 					sp->sp_rget = sp->sp_rbuf;
    917 
    918 				if (stat & (CD180_RCSR_BE | CD180_RCSR_FE))
    919 					data |= TTY_FE;
    920 
    921 				if (stat & CD180_RCSR_PE)
    922 					data |= TTY_PE;
    923 
    924 				(*tp->t_linesw->l_rint)(data, tp);
    925 				r = 1;
    926 			}
    927 
    928 			s = splhigh();
    929 			flags = sp->sp_flags;
    930 			CLR(sp->sp_flags, STTYF_DONE | STTYF_CDCHG |
    931 			    STTYF_RING_OVERFLOW);
    932 			splx(s);
    933 
    934 			if (ISSET(flags, STTYF_CDCHG)) {
    935 				s = spltty();
    936 				STC_WRITE(sc, STC_CAR, i);
    937 				msvr = STC_READ(sc, STC_MSVR);
    938 				splx(s);
    939 
    940 				sp->sp_carrier = msvr & CD180_MSVR_CD;
    941 				(*tp->t_linesw->l_modem)(tp,
    942 				    sp->sp_carrier);
    943 				r = 1;
    944 			}
    945 
    946 			if (ISSET(flags, STTYF_RING_OVERFLOW)) {
    947 				log(LOG_WARNING, "%s-%x: ring overflow\n",
    948 					device_xname(&stc->sc_dev), i);
    949 				r = 1;
    950 			}
    951 
    952 			if (ISSET(flags, STTYF_DONE)) {
    953 				ndflush(&tp->t_outq,
    954 				    sp->sp_txp - tp->t_outq.c_cf);
    955 				CLR(tp->t_state, TS_BUSY);
    956 				(*tp->t_linesw->l_start)(tp);
    957 				r = 1;
    958 			}
    959 		}
    960 	}
    961 }
    962 
    963 void
    964 stty_write_ccr(sc, val)
    965 	struct spif_softc *sc;
    966 	u_int8_t val;
    967 {
    968 	int tries = 100000;
    969 
    970 	while (STC_READ(sc, STC_CCR) && tries--)
    971 		/*EMPTY*/;
    972 	if (tries == 0)
    973 		aprint_error_dev(&sc->sc_dev, "ccr timeout\n");
    974 	STC_WRITE(sc, STC_CCR, val);
    975 }
    976 
    977 int
    978 stty_compute_baud(speed, clock, bprlp, bprhp)
    979 	speed_t speed;
    980 	int clock;
    981 	u_int8_t *bprlp, *bprhp;
    982 {
    983 	u_int32_t rate;
    984 
    985 	rate = (2 * clock) / (16 * speed);
    986 	if (rate & 1)
    987 		rate = (rate >> 1) + 1;
    988 	else
    989 		rate = rate >> 1;
    990 
    991 	if (rate > 0xffff || rate == 0)
    992 		return (1);
    993 
    994 	*bprlp = rate & 0xff;
    995 	*bprhp = (rate >> 8) & 0xff;
    996 	return (0);
    997 }
    998 
    999 int
   1000 sbpp_match(parent, vcf, aux)
   1001 	struct device *parent;
   1002 	struct cfdata *vcf;
   1003 	void *aux;
   1004 {
   1005 	struct spif_softc *sc = (struct spif_softc *)parent;
   1006 
   1007 	return (aux == sbpp_match && sc->sc_bpps == NULL);
   1008 }
   1009 
   1010 void
   1011 sbpp_attach(parent, dev, aux)
   1012 	struct device *parent, *dev;
   1013 	void *aux;
   1014 {
   1015 	struct spif_softc *sc = (struct spif_softc *)parent;
   1016 	struct sbpp_softc *psc = (struct sbpp_softc *)dev;
   1017 	int port;
   1018 
   1019 	sc->sc_bpps = psc;
   1020 
   1021 	for (port = 0; port < sc->sc_npar; port++) {
   1022 	}
   1023 
   1024 	psc->sc_nports = port;
   1025 	printf(": %d port%s\n", port, port == 1 ? "" : "s");
   1026 }
   1027 
   1028 int
   1029 sbpp_open(dev, flags, mode, l)
   1030 	dev_t dev;
   1031 	int flags;
   1032 	int mode;
   1033 	struct lwp *l;
   1034 {
   1035 	return (ENXIO);
   1036 }
   1037 
   1038 int
   1039 sbpp_close(dev, flags, mode, l)
   1040 	dev_t dev;
   1041 	int flags;
   1042 	int mode;
   1043 	struct lwp *l;
   1044 {
   1045 	return (ENXIO);
   1046 }
   1047 
   1048 int
   1049 spif_ppcintr(v)
   1050 	void *v;
   1051 {
   1052 	return (0);
   1053 }
   1054 
   1055 int
   1056 sbpp_read(dev, uio, flags)
   1057 	dev_t dev;
   1058 	struct uio *uio;
   1059 	int flags;
   1060 {
   1061 	return (sbpp_rw(dev, uio));
   1062 }
   1063 
   1064 int
   1065 sbpp_write(dev, uio, flags)
   1066 	dev_t dev;
   1067 	struct uio *uio;
   1068 	int flags;
   1069 {
   1070 	return (sbpp_rw(dev, uio));
   1071 }
   1072 
   1073 int
   1074 sbpp_rw(dev, uio)
   1075 	dev_t dev;
   1076 	struct uio *uio;
   1077 {
   1078 	return (ENXIO);
   1079 }
   1080 
   1081 int
   1082 sbpp_poll(dev, events, l)
   1083 	dev_t dev;
   1084 	int events;
   1085 	struct lwp *l;
   1086 {
   1087 	return (seltrue(dev, events, l));
   1088 }
   1089 
   1090 int
   1091 sbpp_ioctl(dev, cmd, data, flags, l)
   1092 	dev_t dev;
   1093 	u_long cmd;
   1094 	void *data;
   1095 	int flags;
   1096 	struct lwp *l;
   1097 {
   1098 	int error;
   1099 
   1100 	error = ENOTTY;
   1101 
   1102 	return (error);
   1103 }
   1104 
   1105 #endif /* NSPIF */
   1106