Home | History | Annotate | Line # | Download | only in sa11x0
sa11x0_com.c revision 1.3.4.1
      1 /*      $NetBSD: sa11x0_com.c,v 1.3.4.1 2002/05/19 07:41:36 gehenna Exp $        */
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by IWAMOTO Toshihiro.
      9  *
     10  * This code is derived from software contributed to The NetBSD Foundation
     11  * by Charles M. Hannum.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *        This product includes software developed by the NetBSD
     24  *        Foundation, Inc. and its contributors.
     25  * 4. Neither the name of The NetBSD Foundation nor the names of its
     26  *    contributors may be used to endorse or promote products derived
     27  *    from this software without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     39  * POSSIBILITY OF SUCH DAMAGE.
     40  */
     41 
     42 /*
     43  * Copyright (c) 1991 The Regents of the University of California.
     44  * All rights reserved.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. All advertising materials mentioning features or use of this software
     55  *    must display the following acknowledgement:
     56  *	This product includes software developed by the University of
     57  *	California, Berkeley and its contributors.
     58  * 4. Neither the name of the University nor the names of its contributors
     59  *    may be used to endorse or promote products derived from this software
     60  *    without specific prior written permission.
     61  *
     62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72  * SUCH DAMAGE.
     73  *
     74  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     75  */
     76 
     77 #include "opt_com.h"
     78 #include "opt_ddb.h"
     79 #include "opt_kgdb.h"
     80 
     81 #include "rnd.h"
     82 #if NRND > 0 && defined(RND_COM)
     83 #include <sys/rnd.h>
     84 #endif
     85 
     86 #include <sys/param.h>
     87 #include <sys/systm.h>
     88 #include <sys/types.h>
     89 #include <sys/conf.h>
     90 #include <sys/file.h>
     91 #include <sys/device.h>
     92 #include <sys/kernel.h>
     93 #include <sys/malloc.h>
     94 #include <sys/tty.h>
     95 #include <sys/uio.h>
     96 #include <sys/vnode.h>
     97 
     98 #include <dev/cons.h>
     99 
    100 #include <machine/bus.h>
    101 #include <arm/sa11x0/sa11x0_reg.h>
    102 #include <arm/sa11x0/sa11x0_var.h>
    103 #include <arm/sa11x0/sa11x0_comreg.h>
    104 #include <arm/sa11x0/sa11x0_comvar.h>
    105 
    106 #include "sacom.h"
    107 
    108 dev_type_open(sacomopen);
    109 dev_type_close(sacomclose);
    110 dev_type_read(sacomread);
    111 dev_type_write(sacomwrite);
    112 dev_type_ioctl(sacomioctl);
    113 dev_type_stop(sacomstop);
    114 dev_type_tty(sacomtty);
    115 dev_type_poll(sacompoll);
    116 
    117 const struct cdevsw sacom_cdevsw = {
    118 	sacomopen, sacomclose, sacomread, sacomwrite, sacomioctl,
    119 	sacomstop, sacomtty, sacompoll, nommap, D_TTY
    120 };
    121 
    122 static	int	sacom_match(struct device *, struct cfdata *, void *);
    123 static	void	sacom_attach(struct device *, struct device *, void *);
    124 static	void	sacom_filltx(struct sacom_softc *);
    125 static	void	sacom_attach_subr(struct sacom_softc *);
    126 #if defined(DDB) || defined(KGDB)
    127 static	void	sacom_enable_debugport(struct sacom_softc *);
    128 #endif
    129 void		sacom_config(struct sacom_softc *);
    130 void		sacom_shutdown(struct sacom_softc *);
    131 static	u_int	cflag2cr0(tcflag_t);
    132 int		sacomparam(struct tty *, struct termios *);
    133 void		sacomstart(struct tty *);
    134 int		sacomhwiflow(struct tty *, int);
    135 
    136 void		sacom_loadchannelregs(struct sacom_softc *);
    137 void		sacom_hwiflow(struct sacom_softc *);
    138 void		sacom_break(struct sacom_softc *, int);
    139 void		sacom_modem(struct sacom_softc *, int);
    140 void		tiocm_to_sacom(struct sacom_softc *, u_long, int);
    141 int		sacom_to_tiocm(struct sacom_softc *);
    142 void		sacom_iflush(struct sacom_softc *);
    143 
    144 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    145 void 		sacomsoft(void *);
    146 #else
    147 void 		sacomsoft(void);
    148 #endif
    149 
    150 static inline void sacom_rxsoft(struct sacom_softc *, struct tty *);
    151 static inline void sacom_txsoft(struct sacom_softc *, struct tty *);
    152 static inline void sacom_stsoft(struct sacom_softc *, struct tty *);
    153 static inline void sacom_schedrx(struct sacom_softc *);
    154 
    155 
    156 #define COMUNIT_MASK	0x7ffff
    157 #define COMDIALOUT_MASK	0x80000
    158 
    159 #define COMUNIT(x)	(minor(x) & COMUNIT_MASK)
    160 #define COMDIALOUT(x)	(minor(x) & COMDIALOUT_MASK)
    161 
    162 #define COM_ISALIVE(sc)	((sc)->enabled != 0 && \
    163 			 ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
    164 
    165 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
    166 #define COM_LOCK(sc)
    167 #define COM_UNLOCK(sc)
    168 
    169 #define SET(t, f)	(t) |= (f)
    170 #define CLR(t, f)	(t) &= ~(f)
    171 #define ISSET(t, f)	((t) & (f))
    172 
    173 int		sacomintr(void *);
    174 int		sacomcngetc(dev_t);
    175 void		sacomcnputc(dev_t, int);
    176 void		sacomcnpollc(dev_t, int);
    177 
    178 void		sacomcnprobe(struct consdev *);
    179 void		sacomcninit(struct consdev *);
    180 
    181 extern struct bus_space sa11x0_bs_tag;
    182 
    183 static bus_space_tag_t sacomconstag;
    184 static bus_space_handle_t sacomconsioh;
    185 static bus_addr_t sacomconsaddr = SACOM3_BASE; /* XXX */
    186 
    187 static int sacomconsattached;
    188 static int sacomconsrate;
    189 static tcflag_t sacomconscflag;
    190 
    191 struct cfattach sacom_ca = {
    192 	sizeof(struct sacom_softc), sacom_match, sacom_attach
    193 };
    194 extern struct cfdriver sacom_cd;
    195 
    196 struct consdev sacomcons = {
    197 	NULL, NULL, sacomcngetc, sacomcnputc, sacomcnpollc, NULL,
    198 	NODEV, CN_NORMAL
    199 };
    200 
    201 #ifndef CONMODE
    202 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    203 #endif
    204 #ifndef CONSPEED
    205 #define CONSPEED 9600
    206 #endif
    207 #ifndef CONADDR
    208 #define CONADDR SACOM3_BASE
    209 #endif
    210 
    211 static int
    212 sacom_match(parent, match, aux)
    213 	struct device *parent;
    214 	struct cfdata *match;
    215 	void *aux;
    216 {
    217 	return (1);
    218 }
    219 
    220 void
    221 sacom_attach(parent, self, aux)
    222 	struct device *parent;
    223 	struct device *self;
    224 	void *aux;
    225 {
    226 	struct sacom_softc *sc = (struct sacom_softc*)self;
    227 	struct sa11x0_attach_args *sa = aux;
    228 
    229 	printf("\n");
    230 
    231 	sc->sc_iot = sa->sa_iot;
    232 	sc->sc_baseaddr = sa->sa_addr;
    233 
    234 	if(bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
    235 			&sc->sc_ioh)) {
    236 		printf("%s: unable to map registers\n", sc->sc_dev.dv_xname);
    237 		return;
    238 	}
    239 
    240 	printf("%s: ", sc->sc_dev.dv_xname);
    241 	switch(sc->sc_baseaddr) {
    242 	case 0x80050000:
    243 		printf("SA11x0 UART3\n");
    244 		break;
    245 	case 0x80010000:
    246 		printf("SA11x0 UART1\n");
    247 		break;
    248 	case 0x80030000:
    249 		printf("SA11x0 UART2 (IRDA)\n");
    250 		break;
    251 	default:
    252 		printf("unknown SA11x0 UART\n");
    253 		break;
    254 	}
    255 
    256 	sacom_attach_subr(sc);
    257 
    258 	sa11x0_intr_establish(0, sa->sa_intr, 1, IPL_SERIAL, sacomintr, sc);
    259 }
    260 
    261 void
    262 sacom_attach_subr(sc)
    263 	struct sacom_softc *sc;
    264 {
    265 	bus_addr_t iobase = sc->sc_baseaddr;
    266 	bus_space_tag_t iot = sc->sc_iot;
    267 	struct tty *tp;
    268 
    269 #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(COM_MPLOCK)
    270 	simple_lock_init(&sc->sc_lock);
    271 #endif
    272 
    273 	/* XXX Do we need to disable interrupts here? */
    274 
    275 	if (iot == sacomconstag && iobase == sacomconsaddr) {
    276 		sacomconsattached = 1;
    277 		sc->sc_speed = SACOMSPEED(sacomconsrate);
    278 
    279 		/* Make sure the console is always "hardwired". */
    280 		delay(10000);			/* wait for output to finish */
    281 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    282 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    283 	}
    284 
    285 	tp = ttymalloc();
    286 	tp->t_oproc = sacomstart;
    287 	tp->t_param = sacomparam;
    288 	tp->t_hwiflow = sacomhwiflow;
    289 
    290 	sc->sc_tty = tp;
    291 	sc->sc_rbuf = malloc(SACOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
    292 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    293 	sc->sc_rbavail = SACOM_RING_SIZE;
    294 	if (sc->sc_rbuf == NULL) {
    295 		printf("%s: unable to allocate ring buffer\n",
    296 		    sc->sc_dev.dv_xname);
    297 		return;
    298 	}
    299 	sc->sc_ebuf = sc->sc_rbuf + (SACOM_RING_SIZE << 1);
    300 	sc->sc_tbc = 0;
    301 
    302 	tty_attach(tp);
    303 
    304 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    305 		int maj;
    306 
    307 		/* locate the major number */
    308 		maj = cdevsw_lookup_major(&sacom_cdevsw);
    309 
    310 		cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
    311 
    312 		delay(10000); /* XXX */
    313 		printf("%s: console\n", sc->sc_dev.dv_xname);
    314 		delay(10000); /* XXX */
    315 	}
    316 
    317 
    318 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    319 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, sacomsoft, sc);
    320 #endif
    321 
    322 #if NRND > 0 && defined(RND_COM)
    323 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    324 			  RND_TYPE_TTY, 0);
    325 #endif
    326 
    327 	/* if there are no enable/disable functions, assume the device
    328 	   is always enabled */
    329 	if (!sc->enable)
    330 		sc->enabled = 1;
    331 
    332 	sacom_config(sc);
    333 
    334 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
    335 }
    336 
    337 /* This is necessary when dynamically changing SAIP configuration. */
    338 int
    339 sacom_detach(self, flags)
    340 	struct device *self;
    341 	int flags;
    342 {
    343 	struct sacom_softc *sc = (struct sacom_softc *)self;
    344 	int maj, mn;
    345 
    346 	/* locate the major number */
    347 	maj = cdevsw_lookup_major(&sacom_cdevsw);
    348 
    349 	/* Nuke the vnodes for any open instances. */
    350 	mn = self->dv_unit;
    351 	vdevgone(maj, mn, mn, VCHR);
    352 
    353 	mn |= COMDIALOUT_MASK;
    354 	vdevgone(maj, mn, mn, VCHR);
    355 
    356 	/* Free the receive buffer. */
    357 	free(sc->sc_rbuf, M_DEVBUF);
    358 
    359 	/* Detach and free the tty. */
    360 	tty_detach(sc->sc_tty);
    361 	ttyfree(sc->sc_tty);
    362 
    363 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    364 	/* Unhook the soft interrupt handler. */
    365 	softintr_disestablish(sc->sc_si);
    366 #endif
    367 
    368 #if NRND > 0 && defined(RND_COM)
    369 	/* Unhook the entropy source. */
    370 	rnd_detach_source(&sc->rnd_source);
    371 #endif
    372 
    373 	return (0);
    374 }
    375 
    376 void
    377 sacom_config(sc)
    378 	struct sacom_softc *sc;
    379 {
    380 	bus_space_tag_t iot = sc->sc_iot;
    381 	bus_space_handle_t ioh = sc->sc_ioh;
    382 
    383 	/* Disable engine before configuring the device. */
    384 	sc->sc_cr3 = 0;
    385 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
    386 
    387 #ifdef DDB
    388 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    389 		sacom_enable_debugport(sc);
    390 #endif
    391 }
    392 
    393 #ifdef DDB
    394 static void
    395 sacom_enable_debugport(sc)
    396 	struct sacom_softc *sc;
    397 {
    398 	bus_space_tag_t iot = sc->sc_iot;
    399 	bus_space_handle_t ioh = sc->sc_ioh;
    400 	int s;
    401 
    402 	s = splserial();
    403 	COM_LOCK(sc);
    404 	sc->sc_cr3 = CR3_RXE | CR3_TXE;
    405 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
    406 	COM_UNLOCK(sc);
    407 	splx(s);
    408 }
    409 #endif
    410 
    411 int
    412 sacom_activate(self, act)
    413 	struct device *self;
    414 	enum devact act;
    415 {
    416 	struct sacom_softc *sc = (struct sacom_softc *)self;
    417 	int s, rv = 0;
    418 
    419 	s = splserial();
    420 	COM_LOCK(sc);
    421 	switch (act) {
    422 	case DVACT_ACTIVATE:
    423 		rv = EOPNOTSUPP;
    424 		break;
    425 
    426 	case DVACT_DEACTIVATE:
    427 		if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB)) {
    428 			rv = EBUSY;
    429 			break;
    430 		}
    431 
    432 		if (sc->disable != NULL && sc->enabled != 0) {
    433 			(*sc->disable)(sc);
    434 			sc->enabled = 0;
    435 		}
    436 		break;
    437 	}
    438 
    439 	COM_UNLOCK(sc);
    440 	splx(s);
    441 	return (rv);
    442 }
    443 
    444 void
    445 sacom_shutdown(sc)
    446 	struct sacom_softc *sc;
    447 {
    448 	struct tty *tp = sc->sc_tty;
    449 	int s;
    450 
    451 	s = splserial();
    452 	COM_LOCK(sc);
    453 
    454 	/* Clear any break condition set with TIOCSBRK. */
    455 	sacom_break(sc, 0);
    456 
    457 	/*
    458 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    459 	 * notice even if we immediately open the port again.
    460 	 * Avoid tsleeping above splhigh().
    461 	 */
    462 	if (ISSET(tp->t_cflag, HUPCL)) {
    463 		sacom_modem(sc, 0);
    464 		COM_UNLOCK(sc);
    465 		splx(s);
    466 		/* XXX tsleep will only timeout */
    467 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    468 		s = splserial();
    469 		COM_LOCK(sc);
    470 	}
    471 
    472 	/* Turn off interrupts. */
    473 	sc->sc_cr3 = 0;
    474 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3, sc->sc_cr3);
    475 
    476 	if (sc->disable) {
    477 #ifdef DIAGNOSTIC
    478 		if (!sc->enabled)
    479 			panic("sacom_shutdown: not enabled?");
    480 #endif
    481 		(*sc->disable)(sc);
    482 		sc->enabled = 0;
    483 	}
    484 	COM_UNLOCK(sc);
    485 	splx(s);
    486 }
    487 
    488 int
    489 sacomopen(dev, flag, mode, p)
    490 	dev_t dev;
    491 	int flag, mode;
    492 	struct proc *p;
    493 {
    494 	struct sacom_softc *sc;
    495 	struct tty *tp;
    496 	int s, s2;
    497 	int error;
    498 
    499 	sc = device_lookup(&sacom_cd, COMUNIT(dev));
    500 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
    501 		sc->sc_rbuf == NULL)
    502 		return (ENXIO);
    503 
    504 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    505 		return (ENXIO);
    506 
    507 	tp = sc->sc_tty;
    508 
    509 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    510 	    ISSET(tp->t_state, TS_XCLUDE) &&
    511 		p->p_ucred->cr_uid != 0)
    512 		return (EBUSY);
    513 
    514 	s = spltty();
    515 
    516 	/*
    517 	 * Do the following iff this is a first open.
    518 	 */
    519 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    520 		struct termios t;
    521 
    522 		tp->t_dev = dev;
    523 
    524 		s2 = splserial();
    525 		COM_LOCK(sc);
    526 
    527 		if (sc->enable) {
    528 			if ((*sc->enable)(sc)) {
    529 				COM_UNLOCK(sc);
    530 				splx(s2);
    531 				splx(s);
    532 				printf("%s: device enable failed\n",
    533 				       sc->sc_dev.dv_xname);
    534 				return (EIO);
    535 			}
    536 			sc->enabled = 1;
    537 			sacom_config(sc);
    538 		}
    539 
    540 		/* Turn on interrupts. */
    541 		sc->sc_cr3 = CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE;
    542 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3,
    543 				  sc->sc_cr3);
    544 
    545 
    546 		COM_UNLOCK(sc);
    547 		splx(s2);
    548 
    549 		/*
    550 		 * Initialize the termios status to the defaults.  Add in the
    551 		 * sticky bits from TIOCSFLAGS.
    552 		 */
    553 		t.c_ispeed = 0;
    554 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    555 			t.c_ospeed = sacomconsrate;
    556 			t.c_cflag = sacomconscflag;
    557 		} else {
    558 			t.c_ospeed = TTYDEF_SPEED;
    559 			t.c_cflag = TTYDEF_CFLAG;
    560 		}
    561 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    562 			SET(t.c_cflag, CLOCAL);
    563 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    564 			SET(t.c_cflag, CRTSCTS);
    565 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    566 			SET(t.c_cflag, MDMBUF);
    567 		/* Make sure sacomparam() will do something. */
    568 		tp->t_ospeed = 0;
    569 		(void) sacomparam(tp, &t);
    570 		tp->t_iflag = TTYDEF_IFLAG;
    571 		tp->t_oflag = TTYDEF_OFLAG;
    572 		tp->t_lflag = TTYDEF_LFLAG;
    573 		ttychars(tp);
    574 		ttsetwater(tp);
    575 
    576 		s2 = splserial();
    577 		COM_LOCK(sc);
    578 
    579 		/*
    580 		 * Turn on DTR.  We must always do this, even if carrier is not
    581 		 * present, because otherwise we'd have to use TIOCSDTR
    582 		 * immediately after setting CLOCAL, which applications do not
    583 		 * expect.  We always assert DTR while the device is open
    584 		 * unless explicitly requested to deassert it.
    585 		 */
    586 		sacom_modem(sc, 1);
    587 
    588 		/* Clear the input ring, and unblock. */
    589 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    590 		sc->sc_rbavail = SACOM_RING_SIZE;
    591 		sacom_iflush(sc);
    592 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    593 		sacom_hwiflow(sc);
    594 
    595 #ifdef COM_DEBUG
    596 		if (sacom_debug)
    597 			comstatus(sc, "sacomopen  ");
    598 #endif
    599 
    600 		COM_UNLOCK(sc);
    601 		splx(s2);
    602 	}
    603 
    604 	splx(s);
    605 
    606 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    607 	if (error)
    608 		goto bad;
    609 
    610 	error = (*tp->t_linesw->l_open)(dev, tp);
    611 	if (error)
    612 		goto bad;
    613 
    614 	return (0);
    615 
    616 bad:
    617 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    618 		/*
    619 		 * We failed to open the device, and nobody else had it opened.
    620 		 * Clean up the state as appropriate.
    621 		 */
    622 		sacom_shutdown(sc);
    623 	}
    624 
    625 	return (error);
    626 }
    627 
    628 int
    629 sacomclose(dev, flag, mode, p)
    630 	dev_t dev;
    631 	int flag, mode;
    632 	struct proc *p;
    633 {
    634 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    635 	struct tty *tp = sc->sc_tty;
    636 
    637 	/* XXX This is for cons.c. */
    638 	if (!ISSET(tp->t_state, TS_ISOPEN))
    639 		return (0);
    640 
    641 	(*tp->t_linesw->l_close)(tp, flag);
    642 	ttyclose(tp);
    643 
    644 	if (COM_ISALIVE(sc) == 0)
    645 		return (0);
    646 
    647 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    648 		/*
    649 		 * Although we got a last close, the device may still be in
    650 		 * use; e.g. if this was the dialout node, and there are still
    651 		 * processes waiting for carrier on the non-dialout node.
    652 		 */
    653 		sacom_shutdown(sc);
    654 	}
    655 
    656 	return (0);
    657 }
    658 
    659 int
    660 sacomread(dev, uio, flag)
    661 	dev_t dev;
    662 	struct uio *uio;
    663 	int flag;
    664 {
    665 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    666 	struct tty *tp = sc->sc_tty;
    667 
    668 	if (COM_ISALIVE(sc) == 0)
    669 		return (EIO);
    670 
    671 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    672 }
    673 
    674 int
    675 sacomwrite(dev, uio, flag)
    676 	dev_t dev;
    677 	struct uio *uio;
    678 	int flag;
    679 {
    680 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    681 	struct tty *tp = sc->sc_tty;
    682 
    683 	if (COM_ISALIVE(sc) == 0)
    684 		return (EIO);
    685 
    686 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    687 }
    688 
    689 int
    690 sacompoll(dev, events, p)
    691 	dev_t dev;
    692 	int events;
    693 	struct proc *p;
    694 {
    695 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    696 	struct tty *tp = sc->sc_tty;
    697 
    698 	if (COM_ISALIVE(sc) == 0)
    699 		return (EIO);
    700 
    701 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    702 }
    703 
    704 struct tty *
    705 sacomtty(dev)
    706 	dev_t dev;
    707 {
    708 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    709 	struct tty *tp = sc->sc_tty;
    710 
    711 	return (tp);
    712 }
    713 
    714 int
    715 sacomioctl(dev, cmd, data, flag, p)
    716 	dev_t dev;
    717 	u_long cmd;
    718 	caddr_t data;
    719 	int flag;
    720 	struct proc *p;
    721 {
    722 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    723 	struct tty *tp = sc->sc_tty;
    724 	int error;
    725 	int s;
    726 
    727 	if (COM_ISALIVE(sc) == 0)
    728 		return (EIO);
    729 
    730 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    731 	if (error != EPASSTHROUGH)
    732 		return (error);
    733 
    734 	error = ttioctl(tp, cmd, data, flag, p);
    735 	if (error != EPASSTHROUGH)
    736 		return (error);
    737 
    738 	error = 0;
    739 
    740 	s = splserial();
    741 	COM_LOCK(sc);
    742 
    743 	switch (cmd) {
    744 	case TIOCSBRK:
    745 		sacom_break(sc, 1);
    746 		break;
    747 
    748 	case TIOCCBRK:
    749 		sacom_break(sc, 0);
    750 		break;
    751 
    752 	case TIOCSDTR:
    753 		sacom_modem(sc, 1);
    754 		break;
    755 
    756 	case TIOCCDTR:
    757 		sacom_modem(sc, 0);
    758 		break;
    759 
    760 	case TIOCGFLAGS:
    761 		*(int *)data = sc->sc_swflags;
    762 		break;
    763 
    764 	case TIOCSFLAGS:
    765 		error = suser(p->p_ucred, &p->p_acflag);
    766 		if (error)
    767 			break;
    768 		sc->sc_swflags = *(int *)data;
    769 		break;
    770 
    771 	case TIOCMSET:
    772 	case TIOCMBIS:
    773 	case TIOCMBIC:
    774 		tiocm_to_sacom(sc, cmd, *(int *)data);
    775 		break;
    776 
    777 	case TIOCMGET:
    778 		*(int *)data = sacom_to_tiocm(sc);
    779 		break;
    780 
    781 	default:
    782 		error = EPASSTHROUGH;
    783 		break;
    784 	}
    785 
    786 	COM_UNLOCK(sc);
    787 	splx(s);
    788 
    789 #ifdef COM_DEBUG
    790 	if (sacom_debug)
    791 		comstatus(sc, "comioctl ");
    792 #endif
    793 
    794 	return (error);
    795 }
    796 
    797 static inline void
    798 sacom_schedrx(sc)
    799 	struct sacom_softc *sc;
    800 {
    801 
    802 	sc->sc_rx_ready = 1;
    803 
    804 	/* Wake up the poller. */
    805 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    806 	softintr_schedule(sc->sc_si);
    807 #else
    808 	setsoftserial();
    809 #endif
    810 }
    811 
    812 void
    813 sacom_break(sc, onoff)
    814 	struct sacom_softc *sc;
    815 	int onoff;
    816 {
    817 
    818 	if (onoff)
    819 		SET(sc->sc_cr3, CR3_BRK);
    820 	else
    821 		CLR(sc->sc_cr3, CR3_BRK);
    822 
    823 	if (!sc->sc_heldchange) {
    824 		if (sc->sc_tx_busy) {
    825 			sc->sc_heldtbc = sc->sc_tbc;
    826 			sc->sc_tbc = 0;
    827 			sc->sc_heldchange = 1;
    828 		} else
    829 			sacom_loadchannelregs(sc);
    830 	}
    831 }
    832 
    833 void
    834 sacom_modem(sc, onoff)
    835 	struct sacom_softc *sc;
    836 	int onoff;
    837 {
    838 	if (!sc->sc_heldchange) {
    839 		if (sc->sc_tx_busy) {
    840 			sc->sc_heldtbc = sc->sc_tbc;
    841 			sc->sc_tbc = 0;
    842 			sc->sc_heldchange = 1;
    843 		} else
    844 			sacom_loadchannelregs(sc);
    845 	}
    846 }
    847 
    848 void
    849 tiocm_to_sacom(sc, how, ttybits)
    850 	struct sacom_softc *sc;
    851 	u_long how;
    852 	int ttybits;
    853 {
    854 }
    855 
    856 int
    857 sacom_to_tiocm(sc)
    858 	struct sacom_softc *sc;
    859 {
    860 	int ttybits = 0;
    861 
    862 	if (sc->sc_cr3 != 0)
    863 		SET(ttybits, TIOCM_LE);
    864 
    865 	return (ttybits);
    866 }
    867 
    868 static u_int
    869 cflag2cr0(cflag)
    870 	tcflag_t cflag;
    871 {
    872 	u_int cr0;
    873 
    874 	cr0  = (cflag & PARENB) ? CR0_PE : 0;
    875 	cr0 |= (cflag & PARODD) ? 0 : CR0_OES;
    876 	cr0 |= (cflag & CSTOPB) ? CR0_SBS : 0;
    877 	cr0 |= ((cflag & CSIZE) == CS8) ? CR0_DSS : 0;
    878 
    879 	return (cr0);
    880 }
    881 
    882 int
    883 sacomparam(tp, t)
    884 	struct tty *tp;
    885 	struct termios *t;
    886 {
    887 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
    888 	int ospeed = SACOMSPEED(t->c_ospeed);
    889 	u_int cr0;
    890 	int s;
    891 
    892 	if (COM_ISALIVE(sc) == 0)
    893 		return (EIO);
    894 
    895 	/* Check requested parameters. */
    896 	if (ospeed < 0)
    897 		return (EINVAL);
    898 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    899 		return (EINVAL);
    900 
    901 	/*
    902 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    903 	 * is always active.
    904 	 */
    905 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    906 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    907 		SET(t->c_cflag, CLOCAL);
    908 		CLR(t->c_cflag, HUPCL);
    909 	}
    910 
    911 	/*
    912 	 * If there were no changes, don't do anything.  This avoids dropping
    913 	 * input and improves performance when all we did was frob things like
    914 	 * VMIN and VTIME.
    915 	 */
    916 	if (tp->t_ospeed == t->c_ospeed &&
    917 	    tp->t_cflag == t->c_cflag)
    918 		return (0);
    919 
    920 	cr0 = cflag2cr0(t->c_cflag);
    921 
    922 	s = splserial();
    923 	COM_LOCK(sc);
    924 
    925 	sc->sc_cr0 = cr0;
    926 
    927 	sc->sc_speed = ospeed;
    928 
    929 	/* And copy to tty. */
    930 	tp->t_ispeed = 0;
    931 	tp->t_ospeed = t->c_ospeed;
    932 	tp->t_cflag = t->c_cflag;
    933 
    934 	if (!sc->sc_heldchange) {
    935 		if (sc->sc_tx_busy) {
    936 			sc->sc_heldtbc = sc->sc_tbc;
    937 			sc->sc_tbc = 0;
    938 			sc->sc_heldchange = 1;
    939 		} else
    940 			sacom_loadchannelregs(sc);
    941 	}
    942 
    943 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    944 		/* Disable the high water mark. */
    945 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
    946 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
    947 			sacom_schedrx(sc);
    948 		}
    949 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
    950 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
    951 			sacom_hwiflow(sc);
    952 		}
    953 	}
    954 
    955 	COM_UNLOCK(sc);
    956 	splx(s);
    957 
    958 	(void) (*tp->t_linesw->l_modem)(tp, 1);
    959 
    960 #ifdef COM_DEBUG
    961 	if (sacom_debug)
    962 		comstatus(sc, "comparam ");
    963 #endif
    964 
    965 	return (0);
    966 }
    967 
    968 void
    969 sacom_iflush(sc)
    970 	struct sacom_softc *sc;
    971 {
    972 	bus_space_tag_t iot = sc->sc_iot;
    973 	bus_space_handle_t ioh = sc->sc_ioh;
    974 #ifdef DIAGNOSTIC
    975 	int reg;
    976 #endif
    977 	int timo;
    978 
    979 #ifdef DIAGNOSTIC
    980 	reg = 0xffff;
    981 #endif
    982 	timo = 50;
    983 	/* flush any pending I/O */
    984 	if (sc->sc_cr3 & CR3_RXE) {
    985 	while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_RNE)
    986 	    && --timo)
    987 #ifdef DIAGNOSTIC
    988 		reg =
    989 #else
    990 		    (void)
    991 #endif
    992 		    bus_space_read_4(iot, ioh, SACOM_DR);
    993 	}
    994 #if 0
    995 	/* XXX is it good idea to wait TX finish? */
    996 	if (sc->sc_cr3 & CR3_TXE) {
    997 	timo = 500;
    998 	while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_TBY)
    999 	    && --timo)
   1000 		delay(100);
   1001 	}
   1002 #endif
   1003 #ifdef DIAGNOSTIC
   1004 	if (!timo)
   1005 		printf("%s: sacom_iflush timeout %02x\n", sc->sc_dev.dv_xname,
   1006 		       reg);
   1007 #endif
   1008 }
   1009 
   1010 void
   1011 sacom_loadchannelregs(sc)
   1012 	struct sacom_softc *sc;
   1013 {
   1014 	bus_space_tag_t iot = sc->sc_iot;
   1015 	bus_space_handle_t ioh = sc->sc_ioh;
   1016 
   1017 	/* XXXXX necessary? */
   1018 	sacom_iflush(sc);
   1019 
   1020 	/* Need to stop engines first. */
   1021 	bus_space_write_4(iot, ioh, SACOM_CR3, 0);
   1022 
   1023 	bus_space_write_4(iot, ioh, SACOM_CR1, sc->sc_speed >> 8);
   1024 	bus_space_write_4(iot, ioh, SACOM_CR2, sc->sc_speed & 0xff);
   1025 
   1026 	bus_space_write_4(iot, ioh, SACOM_CR0, sc->sc_cr0);
   1027 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
   1028 }
   1029 
   1030 int
   1031 sacomhwiflow(tp, block)
   1032 	struct tty *tp;
   1033 	int block;
   1034 {
   1035 #if 0
   1036 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
   1037 	int s;
   1038 
   1039 	if (COM_ISALIVE(sc) == 0)
   1040 		return (0);
   1041 
   1042 	if (sc->sc_mcr_rts == 0)
   1043 		return (0);
   1044 
   1045 	s = splserial();
   1046 	COM_LOCK(sc);
   1047 
   1048 	if (block) {
   1049 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1050 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1051 			sacom_hwiflow(sc);
   1052 		}
   1053 	} else {
   1054 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1055 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1056 			sacom_schedrx(sc);
   1057 		}
   1058 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1059 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1060 			sacom_hwiflow(sc);
   1061 		}
   1062 	}
   1063 
   1064 	COM_UNLOCK(sc);
   1065 	splx(s);
   1066 #endif
   1067 	return (1);
   1068 }
   1069 
   1070 /*
   1071  * (un)block input via hw flowcontrol
   1072  */
   1073 void
   1074 sacom_hwiflow(sc)
   1075 	struct sacom_softc *sc;
   1076 {
   1077 #if 0
   1078 	bus_space_tag_t iot = sc->sc_iot;
   1079 	bus_space_handle_t ioh = sc->sc_ioh;
   1080 
   1081 	/* XXX implement */
   1082 #endif
   1083 }
   1084 
   1085 
   1086 void
   1087 sacomstart(tp)
   1088 	struct tty *tp;
   1089 {
   1090 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
   1091 	bus_space_tag_t iot = sc->sc_iot;
   1092 	bus_space_handle_t ioh = sc->sc_ioh;
   1093 	int s;
   1094 
   1095 	if (COM_ISALIVE(sc) == 0)
   1096 		return;
   1097 
   1098 	s = spltty();
   1099 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1100 		goto out;
   1101 
   1102 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1103 		if (ISSET(tp->t_state, TS_ASLEEP)) {
   1104 			CLR(tp->t_state, TS_ASLEEP);
   1105 			wakeup(&tp->t_outq);
   1106 		}
   1107 		selwakeup(&tp->t_wsel);
   1108 		if (tp->t_outq.c_cc == 0)
   1109 			goto out;
   1110 	}
   1111 
   1112 	/* Grab the first contiguous region of buffer space. */
   1113 	{
   1114 		u_char *tba;
   1115 		int tbc;
   1116 
   1117 		tba = tp->t_outq.c_cf;
   1118 		tbc = ndqb(&tp->t_outq, 0);
   1119 
   1120 		(void)splserial();
   1121 		COM_LOCK(sc);
   1122 
   1123 		sc->sc_tba = tba;
   1124 		sc->sc_tbc = tbc;
   1125 	}
   1126 
   1127 	SET(tp->t_state, TS_BUSY);
   1128 	sc->sc_tx_busy = 1;
   1129 
   1130 	/* Enable transmit completion interrupts if necessary. */
   1131 	if (!ISSET(sc->sc_cr3, CR3_TIE)) {
   1132 		SET(sc->sc_cr3, CR3_TIE);
   1133 		bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
   1134 	}
   1135 
   1136 	/* Output the first chunk of the contiguous buffer. */
   1137 	sacom_filltx(sc);
   1138 
   1139 	COM_UNLOCK(sc);
   1140 out:
   1141 	splx(s);
   1142 	return;
   1143 }
   1144 
   1145 void
   1146 sacom_filltx(sc)
   1147 	struct sacom_softc *sc;
   1148 {
   1149 	bus_space_tag_t iot = sc->sc_iot;
   1150 	bus_space_handle_t ioh = sc->sc_ioh;
   1151 	int c, n;
   1152 
   1153 	n = 0;
   1154 	while(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1155 	      & SR1_TNF) {
   1156 		if (n == SACOM_TXFIFOLEN || n == sc->sc_tbc)
   1157 			break;
   1158 		c = *(sc->sc_tba + n);
   1159 		c &= 0xff;
   1160 		bus_space_write_4(iot, ioh, SACOM_DR, c);
   1161 		n++;
   1162 	}
   1163 	sc->sc_tbc -= n;
   1164 	sc->sc_tba += n;
   1165 }
   1166 
   1167 /*
   1168  * Stop output on a line.
   1169  */
   1170 void
   1171 sacomstop(tp, flag)
   1172 	struct tty *tp;
   1173 	int flag;
   1174 {
   1175 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
   1176 	int s;
   1177 
   1178 	s = splserial();
   1179 	COM_LOCK(sc);
   1180 	if (ISSET(tp->t_state, TS_BUSY)) {
   1181 		/* Stop transmitting at the next chunk. */
   1182 		sc->sc_tbc = 0;
   1183 		sc->sc_heldtbc = 0;
   1184 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1185 			SET(tp->t_state, TS_FLUSH);
   1186 	}
   1187 	COM_UNLOCK(sc);
   1188 	splx(s);
   1189 }
   1190 
   1191 static inline void
   1192 sacom_rxsoft(sc, tp)
   1193 	struct sacom_softc *sc;
   1194 	struct tty *tp;
   1195 {
   1196 	int (*rint)(int c, struct tty *tp) = tp->t_linesw->l_rint;
   1197 	u_char *get, *end;
   1198 	u_int cc, scc;
   1199 	u_char sr1;
   1200 	int code;
   1201 	int s;
   1202 
   1203 	end = sc->sc_ebuf;
   1204 	get = sc->sc_rbget;
   1205 	scc = cc = SACOM_RING_SIZE - sc->sc_rbavail;
   1206 
   1207 	while (cc) {
   1208 		code = get[0];
   1209 		sr1 = get[1];
   1210 		if (ISSET(sr1, SR1_FRE))
   1211 			SET(code, TTY_FE);
   1212 		if (ISSET(sr1, SR1_PRE))
   1213 			SET(code, TTY_PE);
   1214 		if ((*rint)(code, tp) == -1) {
   1215 			/*
   1216 			 * The line discipline's buffer is out of space.
   1217 			 */
   1218 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1219 				/*
   1220 				 * We're either not using flow control, or the
   1221 				 * line discipline didn't tell us to block for
   1222 				 * some reason.  Either way, we have no way to
   1223 				 * know when there's more space available, so
   1224 				 * just drop the rest of the data.
   1225 				 */
   1226 				get += cc << 1;
   1227 				if (get >= end)
   1228 					get -= SACOM_RING_SIZE << 1;
   1229 				cc = 0;
   1230 			} else {
   1231 				/*
   1232 				 * Don't schedule any more receive processing
   1233 				 * until the line discipline tells us there's
   1234 				 * space available (through comhwiflow()).
   1235 				 * Leave the rest of the data in the input
   1236 				 * buffer.
   1237 				 */
   1238 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1239 			}
   1240 			break;
   1241 		}
   1242 		get += 2;
   1243 		if (get >= end)
   1244 			get = sc->sc_rbuf;
   1245 		cc--;
   1246 	}
   1247 
   1248 	if (cc != scc) {
   1249 		sc->sc_rbget = get;
   1250 		s = splserial();
   1251 		COM_LOCK(sc);
   1252 
   1253 		cc = sc->sc_rbavail += scc - cc;
   1254 		/* Buffers should be ok again, release possible block. */
   1255 		if (cc >= 1) {
   1256 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1257 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1258 				SET(sc->sc_cr3, CR3_RIE);
   1259 				bus_space_write_4(sc->sc_iot, sc->sc_ioh,
   1260 						  SACOM_CR3, sc->sc_cr3);
   1261 			}
   1262 		}
   1263 		COM_UNLOCK(sc);
   1264 		splx(s);
   1265 	}
   1266 }
   1267 
   1268 static inline void
   1269 sacom_txsoft(sc, tp)
   1270 	struct sacom_softc *sc;
   1271 	struct tty *tp;
   1272 {
   1273 
   1274 	CLR(tp->t_state, TS_BUSY);
   1275 	if (ISSET(tp->t_state, TS_FLUSH))
   1276 		CLR(tp->t_state, TS_FLUSH);
   1277 	else
   1278 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1279 	(*tp->t_linesw->l_start)(tp);
   1280 }
   1281 
   1282 static inline void
   1283 sacom_stsoft(sc, tp)
   1284 	struct sacom_softc *sc;
   1285 	struct tty *tp;
   1286 {
   1287 }
   1288 
   1289 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1290 void
   1291 sacomsoft(arg)
   1292 	void *arg;
   1293 {
   1294 	struct sacom_softc *sc = arg;
   1295 	struct tty *tp;
   1296 
   1297 	if (COM_ISALIVE(sc) == 0)
   1298 		return;
   1299 
   1300 	{
   1301 #else
   1302 void
   1303 sacomsoft()
   1304 {
   1305 	struct sacom_softc	*sc;
   1306 	struct tty	*tp;
   1307 	int	unit;
   1308 
   1309 	for (unit = 0; unit < sacom_cd.cd_ndevs; unit++) {
   1310 		sc = device_lookup(&sacom_cd, unit);
   1311 		if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
   1312 			continue;
   1313 
   1314 		if (COM_ISALIVE(sc) == 0)
   1315 			continue;
   1316 
   1317 		tp = sc->sc_tty;
   1318 		if (tp == NULL)
   1319 			continue;
   1320 		if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
   1321 			continue;
   1322 #endif
   1323 		tp = sc->sc_tty;
   1324 
   1325 		if (sc->sc_rx_ready) {
   1326 			sc->sc_rx_ready = 0;
   1327 			sacom_rxsoft(sc, tp);
   1328 		}
   1329 
   1330 		if (sc->sc_st_check) {
   1331 			sc->sc_st_check = 0;
   1332 			sacom_stsoft(sc, tp);
   1333 		}
   1334 
   1335 		if (sc->sc_tx_done) {
   1336 			sc->sc_tx_done = 0;
   1337 			sacom_txsoft(sc, tp);
   1338 		}
   1339 	}
   1340 
   1341 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
   1342 #endif
   1343 }
   1344 
   1345 #ifdef __ALIGN_BRACKET_LEVEL_FOR_CTAGS
   1346 	/* there has got to be a better way to do comsoft() */
   1347 }}
   1348 #endif
   1349 
   1350 int
   1351 sacomintr(arg)
   1352 	void *arg;
   1353 {
   1354 	struct sacom_softc *sc = arg;
   1355 	bus_space_tag_t iot = sc->sc_iot;
   1356 	bus_space_handle_t ioh = sc->sc_ioh;
   1357 	u_char *put, *end;
   1358 	u_int cc;
   1359 	u_int sr0, sr1;
   1360 
   1361 	if (COM_ISALIVE(sc) == 0)
   1362 		return (0);
   1363 
   1364 	COM_LOCK(sc);
   1365 	sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
   1366 	if (! sr0) {
   1367 		COM_UNLOCK(sc);
   1368 		return (0);
   1369 	}
   1370 	if (ISSET(sr0, SR0_EIF))
   1371 		/* XXX silently discard error bits */
   1372 		bus_space_read_4(iot, ioh, SACOM_DR);
   1373 	if (ISSET(sr0, SR0_RBB))
   1374 		bus_space_write_4(iot, ioh, SACOM_SR0, SR0_RBB);
   1375 	if (ISSET(sr0, SR0_REB)) {
   1376 		bus_space_write_4(iot, ioh, SACOM_SR0, SR0_REB);
   1377 #if defined(DDB) || defined(KGDB)
   1378 #ifndef DDB_BREAK_CHAR
   1379 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1380 			console_debugger();
   1381 		}
   1382 #endif
   1383 #endif /* DDB || KGDB */
   1384 	}
   1385 
   1386 
   1387 	end = sc->sc_ebuf;
   1388 	put = sc->sc_rbput;
   1389 	cc = sc->sc_rbavail;
   1390 
   1391 	sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
   1392 	if (ISSET(sr0, SR0_RFS | SR0_RID)) {
   1393 		if (! ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1394 			while (cc > 0) {
   1395 				if (!ISSET(sr1, SR1_RNE)) {
   1396 					bus_space_write_4(iot, ioh, SACOM_SR0,
   1397 							  SR0_RID);
   1398 					break;
   1399 				}
   1400 				put[0] = bus_space_read_4(iot, ioh, SACOM_DR);
   1401 				put[1] = sr1;
   1402 #if defined(DDB) && defined(DDB_BREAK_CHAR)
   1403 				if (put[0] == DDB_BREAK_CHAR &&
   1404 				    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1405 					console_debugger();
   1406 
   1407 					sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
   1408 					continue;
   1409 				}
   1410 #endif
   1411 				put += 2;
   1412 				if (put >= end)
   1413 					put = sc->sc_rbuf;
   1414 				cc--;
   1415 
   1416 				sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
   1417 			}
   1418 
   1419 			/*
   1420 			 * Current string of incoming characters ended because
   1421 			 * no more data was available or we ran out of space.
   1422 			 * Schedule a receive event if any data was received.
   1423 			 * If we're out of space, turn off receive interrupts.
   1424 			 */
   1425 			sc->sc_rbput = put;
   1426 			sc->sc_rbavail = cc;
   1427 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1428 				sc->sc_rx_ready = 1;
   1429 
   1430 			/* XXX do RX hardware flow control */
   1431 
   1432 			/*
   1433 			 * If we're out of space, disable receive interrupts
   1434 			 * until the queue has drained a bit.
   1435 			 */
   1436 			if (!cc) {
   1437 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1438 				CLR(sc->sc_cr3, CR3_RIE);
   1439 				bus_space_write_4(iot, ioh, SACOM_CR3,
   1440 						  sc->sc_cr3);
   1441 			}
   1442 		} else {
   1443 #ifdef DIAGNOSTIC
   1444 			panic("sacomintr: we shouldn't reach here\n");
   1445 #endif
   1446 			CLR(sc->sc_cr3, CR3_RIE);
   1447 			bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
   1448 		}
   1449 	}
   1450 
   1451 	/*
   1452 	 * Done handling any receive interrupts. See if data can be
   1453 	 * transmitted as well. Schedule tx done event if no data left
   1454 	 * and tty was marked busy.
   1455 	 */
   1456 	sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
   1457 	if (ISSET(sr0, SR0_TFS)) {
   1458 		/*
   1459 		 * If we've delayed a parameter change, do it now, and restart
   1460 		 * output.
   1461 		 * XXX sacom_loadchanelregs() waits TX completion,
   1462 		 * XXX resulting in ~0.1s hang (300bps, 4 bytes) in worst case
   1463 		 */
   1464 		if (sc->sc_heldchange) {
   1465 			sacom_loadchannelregs(sc);
   1466 			sc->sc_heldchange = 0;
   1467 			sc->sc_tbc = sc->sc_heldtbc;
   1468 			sc->sc_heldtbc = 0;
   1469 		}
   1470 
   1471 		/* Output the next chunk of the contiguous buffer, if any. */
   1472 		if (sc->sc_tbc > 0) {
   1473 			sacom_filltx(sc);
   1474 		} else {
   1475 			/* Disable transmit completion interrupts if necessary. */
   1476 			if (ISSET(sc->sc_cr3, CR3_TIE)) {
   1477 				CLR(sc->sc_cr3, CR3_TIE);
   1478 				bus_space_write_4(iot, ioh, SACOM_CR3,
   1479 						  sc->sc_cr3);
   1480 			}
   1481 			if (sc->sc_tx_busy) {
   1482 				sc->sc_tx_busy = 0;
   1483 				sc->sc_tx_done = 1;
   1484 			}
   1485 		}
   1486 	}
   1487 	COM_UNLOCK(sc);
   1488 
   1489 	/* Wake up the poller. */
   1490 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1491 	softintr_schedule(sc->sc_si);
   1492 #else
   1493 	setsoftserial();
   1494 #endif
   1495 
   1496 #if NRND > 0 && defined(RND_COM)
   1497 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   1498 #endif
   1499 	return (1);
   1500 }
   1501 
   1502 /* Initialization for serial console */
   1503 int
   1504 sacominit(iot, iobase, baud, cflag, iohp)
   1505 	bus_space_tag_t iot;
   1506 	bus_addr_t iobase;
   1507 	int baud;
   1508 	tcflag_t cflag;
   1509 	bus_space_handle_t *iohp;
   1510 {
   1511 	int brd, cr0;
   1512 
   1513 	if (bus_space_map(iot, iobase, SACOM_NPORTS, 0, iohp))
   1514 		printf("register map failed\n");
   1515 
   1516 	/* wait for the Tx queue to drain and disable the UART */
   1517 	while(bus_space_read_4(iot, *iohp, SACOM_SR1) & SR1_TBY)
   1518 		;
   1519 	bus_space_write_4(iot, *iohp, SACOM_CR3, 0);
   1520 
   1521 	cr0  = cflag2cr0(cflag);
   1522 	bus_space_write_4(iot, *iohp, SACOM_CR0, cr0);
   1523 
   1524 	brd = SACOMSPEED(baud);
   1525 	sacomconsrate = baud;
   1526 	sacomconsaddr = iobase;
   1527 	sacomconscflag = cflag;
   1528 	/* XXX assumes little endian */
   1529 	bus_space_write_4(iot, *iohp, SACOM_CR1, brd >> 8);
   1530 	bus_space_write_4(iot, *iohp, SACOM_CR2, brd & 0xff);
   1531 
   1532 	/* enable the UART */
   1533 	bus_space_write_4(iot, *iohp, SACOM_CR3, CR3_RXE | CR3_TXE);
   1534 
   1535 	return (0);
   1536 }
   1537 
   1538 void
   1539 sacomcnprobe(cp)
   1540 	struct consdev *cp;
   1541 {
   1542 	cp->cn_pri = CN_REMOTE;
   1543 }
   1544 
   1545 void
   1546 sacomcninit(cp)
   1547 	struct consdev *cp;
   1548 {
   1549 	if (cp == NULL) {
   1550 		/* XXX cp == NULL means that MMU is disabled. */
   1551 		sacomconsioh = SACOM3_HW_BASE;
   1552 		sacomconstag = &sa11x0_bs_tag;
   1553 		cn_tab = &sacomcons;
   1554 		return;
   1555 	}
   1556 
   1557 	if (sacominit(&sa11x0_bs_tag, CONADDR, CONSPEED,
   1558 			  CONMODE, &sacomconsioh))
   1559 		panic("can't init serial console @%x", CONADDR);
   1560 	cn_tab = &sacomcons;
   1561 	sacomconstag = &sa11x0_bs_tag;
   1562 }
   1563 
   1564 int
   1565 sacomcngetc(dev)
   1566 	dev_t dev;
   1567 {
   1568 	int c, s;
   1569 
   1570 	s = spltty();	/* XXX do we need this? */
   1571 
   1572 	while(! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1573 		 & SR1_RNE)) {
   1574 #if defined(DDB) || defined(KGDB)
   1575 #ifndef DDB_BREAK_CHAR
   1576 		u_int sr0;
   1577 		extern int db_active;
   1578 
   1579 		sr0 = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR0);
   1580 		if (ISSET(sr0, SR0_RBB))
   1581 			bus_space_write_4(sacomconstag, sacomconsioh,
   1582 					  SACOM_SR0, SR0_RBB);
   1583 		if (ISSET(sr0, SR0_REB)) {
   1584 			bus_space_write_4(sacomconstag, sacomconsioh,
   1585 					  SACOM_SR0, SR0_REB);
   1586 			if (db_active == 0)
   1587 				console_debugger();
   1588 		}
   1589 #endif
   1590 #endif /* DDB || KGDB */
   1591 	}
   1592 
   1593 	c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
   1594 	c &= 0xff;
   1595 	splx(s);
   1596 
   1597 	return (c);
   1598 }
   1599 
   1600 void
   1601 sacomcnputc(dev, c)
   1602 	dev_t dev;
   1603 	int c;
   1604 {
   1605 	int s;
   1606 
   1607 	s = spltty();	/* XXX do we need this? */
   1608 
   1609 	while(! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1610 		 & SR1_TNF))
   1611 		;
   1612 
   1613 	bus_space_write_4(sacomconstag, sacomconsioh, SACOM_DR, c);
   1614 	splx(s);
   1615 }
   1616 
   1617 void
   1618 sacomcnpollc(dev, on)
   1619 	dev_t dev;
   1620 	int on;
   1621 {
   1622 
   1623 }
   1624 
   1625 #ifdef DEBUG
   1626 int
   1627 sacomcncharpoll()
   1628 {
   1629 	int c;
   1630 
   1631 	if (! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1632 		 & SR1_RNE))
   1633 		return -1;
   1634 
   1635 	c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
   1636 	c &= 0xff;
   1637 
   1638 	return (c);
   1639 }
   1640 
   1641 #endif
   1642 
   1643 /* helper function to identify the com ports used by
   1644  console or KGDB (and not yet autoconf attached) */
   1645 int
   1646 sacom_is_console(iot, iobase, ioh)
   1647 	bus_space_tag_t iot;
   1648 	bus_addr_t iobase;
   1649 	bus_space_handle_t *ioh;
   1650 {
   1651 	bus_space_handle_t help;
   1652 
   1653 	if (! sacomconsattached &&
   1654 	    iot == sacomconstag && iobase == sacomconsaddr)
   1655 		help = sacomconsioh;
   1656 	else
   1657 		return (0);
   1658 
   1659 	if (ioh)
   1660 		*ioh = help;
   1661 	return (1);
   1662 }
   1663