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