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