Home | History | Annotate | Line # | Download | only in sa11x0
sa11x0_com.c revision 1.37
      1 /*      $NetBSD: sa11x0_com.c,v 1.37 2006/10/01 20:31:49 elad 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.37 2006/10/01 20:31:49 elad 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 (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    525 		return (EBUSY);
    526 
    527 	s = spltty();
    528 
    529 	/*
    530 	 * Do the following iff this is a first open.
    531 	 */
    532 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    533 		struct termios t;
    534 
    535 		tp->t_dev = dev;
    536 
    537 		s2 = splserial();
    538 		COM_LOCK(sc);
    539 
    540 		if (sc->enable) {
    541 			if ((*sc->enable)(sc)) {
    542 				COM_UNLOCK(sc);
    543 				splx(s2);
    544 				splx(s);
    545 				printf("%s: device enable failed\n",
    546 				       sc->sc_dev.dv_xname);
    547 				return EIO;
    548 			}
    549 			sc->enabled = 1;
    550 			sacom_config(sc);
    551 		}
    552 
    553 		/* Turn on interrupts. */
    554 		sc->sc_cr3 = CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE;
    555 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3,
    556 				  sc->sc_cr3);
    557 
    558 
    559 		COM_UNLOCK(sc);
    560 		splx(s2);
    561 
    562 		/*
    563 		 * Initialize the termios status to the defaults.  Add in the
    564 		 * sticky bits from TIOCSFLAGS.
    565 		 */
    566 		t.c_ispeed = 0;
    567 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    568 			t.c_ospeed = sacomconsrate;
    569 			t.c_cflag = sacomconscflag;
    570 		} else {
    571 			t.c_ospeed = TTYDEF_SPEED;
    572 			t.c_cflag = TTYDEF_CFLAG;
    573 		}
    574 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    575 			SET(t.c_cflag, CLOCAL);
    576 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    577 			SET(t.c_cflag, CRTSCTS);
    578 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    579 			SET(t.c_cflag, MDMBUF);
    580 		/* Make sure sacomparam() will do something. */
    581 		tp->t_ospeed = 0;
    582 		(void) sacomparam(tp, &t);
    583 		tp->t_iflag = TTYDEF_IFLAG;
    584 		tp->t_oflag = TTYDEF_OFLAG;
    585 		tp->t_lflag = TTYDEF_LFLAG;
    586 		ttychars(tp);
    587 		ttsetwater(tp);
    588 
    589 		s2 = splserial();
    590 		COM_LOCK(sc);
    591 
    592 		/*
    593 		 * Turn on DTR.  We must always do this, even if carrier is not
    594 		 * present, because otherwise we'd have to use TIOCSDTR
    595 		 * immediately after setting CLOCAL, which applications do not
    596 		 * expect.  We always assert DTR while the device is open
    597 		 * unless explicitly requested to deassert it.
    598 		 */
    599 		sacom_modem(sc, 1);
    600 
    601 		/* Clear the input ring, and unblock. */
    602 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    603 		sc->sc_rbavail = SACOM_RING_SIZE;
    604 		sacom_iflush(sc);
    605 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    606 		sacom_hwiflow(sc);
    607 
    608 #ifdef COM_DEBUG
    609 		if (sacom_debug)
    610 			comstatus(sc, "sacomopen  ");
    611 #endif
    612 
    613 		COM_UNLOCK(sc);
    614 		splx(s2);
    615 	}
    616 
    617 	splx(s);
    618 
    619 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    620 	if (error)
    621 		goto bad;
    622 
    623 	error = (*tp->t_linesw->l_open)(dev, tp);
    624 	if (error)
    625 		goto bad;
    626 
    627 	return 0;
    628 
    629 bad:
    630 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    631 		/*
    632 		 * We failed to open the device, and nobody else had it opened.
    633 		 * Clean up the state as appropriate.
    634 		 */
    635 		sacom_shutdown(sc);
    636 	}
    637 
    638 	return error;
    639 }
    640 
    641 int
    642 sacomclose(dev_t dev, int flag, int mode, struct lwp *l)
    643 {
    644 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    645 	struct tty *tp = sc->sc_tty;
    646 
    647 	/* XXX This is for cons.c. */
    648 	if (!ISSET(tp->t_state, TS_ISOPEN))
    649 		return 0;
    650 
    651 	(*tp->t_linesw->l_close)(tp, flag);
    652 	ttyclose(tp);
    653 
    654 	if (COM_ISALIVE(sc) == 0)
    655 		return 0;
    656 
    657 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    658 		/*
    659 		 * Although we got a last close, the device may still be in
    660 		 * use; e.g. if this was the dialout node, and there are still
    661 		 * processes waiting for carrier on the non-dialout node.
    662 		 */
    663 		sacom_shutdown(sc);
    664 	}
    665 
    666 	return 0;
    667 }
    668 
    669 int
    670 sacomread(dev_t dev, struct uio *uio, int flag)
    671 {
    672 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    673 	struct tty *tp = sc->sc_tty;
    674 
    675 	if (COM_ISALIVE(sc) == 0)
    676 		return EIO;
    677 
    678 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    679 }
    680 
    681 int
    682 sacomwrite(dev_t dev, struct uio *uio, int flag)
    683 {
    684 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    685 	struct tty *tp = sc->sc_tty;
    686 
    687 	if (COM_ISALIVE(sc) == 0)
    688 		return EIO;
    689 
    690 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    691 }
    692 
    693 int
    694 sacompoll(dev_t dev, int events, struct lwp *l)
    695 {
    696 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    697 	struct tty *tp = sc->sc_tty;
    698 
    699 	if (COM_ISALIVE(sc) == 0)
    700 		return EIO;
    701 
    702 	return (*tp->t_linesw->l_poll)(tp, events, l);
    703 }
    704 
    705 struct tty *
    706 sacomtty(dev_t dev)
    707 {
    708 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    709 	struct tty *tp = sc->sc_tty;
    710 
    711 	return tp;
    712 }
    713 
    714 int
    715 sacomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    716 {
    717 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    718 	struct tty *tp = sc->sc_tty;
    719 	int error;
    720 	int s;
    721 
    722 	if (COM_ISALIVE(sc) == 0)
    723 		return EIO;
    724 
    725 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    726 	if (error != EPASSTHROUGH)
    727 		return error;
    728 
    729 	error = ttioctl(tp, cmd, data, flag, l);
    730 	if (error != EPASSTHROUGH)
    731 		return error;
    732 
    733 	error = 0;
    734 
    735 	s = splserial();
    736 	COM_LOCK(sc);
    737 
    738 	switch (cmd) {
    739 	case TIOCSBRK:
    740 		sacom_break(sc, 1);
    741 		break;
    742 
    743 	case TIOCCBRK:
    744 		sacom_break(sc, 0);
    745 		break;
    746 
    747 	case TIOCSDTR:
    748 		sacom_modem(sc, 1);
    749 		break;
    750 
    751 	case TIOCCDTR:
    752 		sacom_modem(sc, 0);
    753 		break;
    754 
    755 	case TIOCGFLAGS:
    756 		*(int *)data = sc->sc_swflags;
    757 		break;
    758 
    759 	case TIOCSFLAGS:
    760 		error = kauth_authorize_device_tty(l->l_cred,
    761 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    762 		if (error)
    763 			break;
    764 		sc->sc_swflags = *(int *)data;
    765 		break;
    766 
    767 	case TIOCMSET:
    768 	case TIOCMBIS:
    769 	case TIOCMBIC:
    770 		tiocm_to_sacom(sc, cmd, *(int *)data);
    771 		break;
    772 
    773 	case TIOCMGET:
    774 		*(int *)data = sacom_to_tiocm(sc);
    775 		break;
    776 
    777 	default:
    778 		error = EPASSTHROUGH;
    779 		break;
    780 	}
    781 
    782 	COM_UNLOCK(sc);
    783 	splx(s);
    784 
    785 #ifdef COM_DEBUG
    786 	if (sacom_debug)
    787 		comstatus(sc, "comioctl ");
    788 #endif
    789 
    790 	return error;
    791 }
    792 
    793 static inline void
    794 sacom_schedrx(struct sacom_softc *sc)
    795 {
    796 
    797 	sc->sc_rx_ready = 1;
    798 
    799 	/* Wake up the poller. */
    800 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    801 	softintr_schedule(sc->sc_si);
    802 #else
    803 	setsoftserial();
    804 #endif
    805 }
    806 
    807 void
    808 sacom_break(struct sacom_softc *sc, int onoff)
    809 {
    810 
    811 	if (onoff)
    812 		SET(sc->sc_cr3, CR3_BRK);
    813 	else
    814 		CLR(sc->sc_cr3, CR3_BRK);
    815 
    816 	if (!sc->sc_heldchange) {
    817 		if (sc->sc_tx_busy) {
    818 			sc->sc_heldtbc = sc->sc_tbc;
    819 			sc->sc_tbc = 0;
    820 			sc->sc_heldchange = 1;
    821 		} else
    822 			sacom_loadchannelregs(sc);
    823 	}
    824 }
    825 
    826 void
    827 sacom_modem(struct sacom_softc *sc, int onoff)
    828 {
    829 	if (!sc->sc_heldchange) {
    830 		if (sc->sc_tx_busy) {
    831 			sc->sc_heldtbc = sc->sc_tbc;
    832 			sc->sc_tbc = 0;
    833 			sc->sc_heldchange = 1;
    834 		} else
    835 			sacom_loadchannelregs(sc);
    836 	}
    837 }
    838 
    839 void
    840 tiocm_to_sacom(struct sacom_softc *sc, u_long how, int ttybits)
    841 {
    842 }
    843 
    844 int
    845 sacom_to_tiocm(struct sacom_softc *sc)
    846 {
    847 	int ttybits = 0;
    848 
    849 	if (sc->sc_cr3 != 0)
    850 		SET(ttybits, TIOCM_LE);
    851 
    852 	return ttybits;
    853 }
    854 
    855 static u_int
    856 cflag2cr0(tcflag_t cflag)
    857 {
    858 	u_int cr0;
    859 
    860 	cr0  = (cflag & PARENB) ? CR0_PE : 0;
    861 	cr0 |= (cflag & PARODD) ? 0 : CR0_OES;
    862 	cr0 |= (cflag & CSTOPB) ? CR0_SBS : 0;
    863 	cr0 |= ((cflag & CSIZE) == CS8) ? CR0_DSS : 0;
    864 
    865 	return cr0;
    866 }
    867 
    868 int
    869 sacomparam(struct tty *tp, struct termios *t)
    870 {
    871 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
    872 	int ospeed = SACOMSPEED(t->c_ospeed);
    873 	u_int cr0;
    874 	int s;
    875 
    876 	if (COM_ISALIVE(sc) == 0)
    877 		return EIO;
    878 
    879 	/* Check requested parameters. */
    880 	if (ospeed < 0)
    881 		return EINVAL;
    882 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    883 		return EINVAL;
    884 
    885 	/*
    886 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    887 	 * is always active.
    888 	 */
    889 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    890 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    891 		SET(t->c_cflag, CLOCAL);
    892 		CLR(t->c_cflag, HUPCL);
    893 	}
    894 
    895 	/*
    896 	 * If there were no changes, don't do anything.  This avoids dropping
    897 	 * input and improves performance when all we did was frob things like
    898 	 * VMIN and VTIME.
    899 	 */
    900 	if (tp->t_ospeed == t->c_ospeed &&
    901 	    tp->t_cflag == t->c_cflag)
    902 		return 0;
    903 
    904 	cr0 = cflag2cr0(t->c_cflag);
    905 
    906 	s = splserial();
    907 	COM_LOCK(sc);
    908 
    909 	sc->sc_cr0 = cr0;
    910 
    911 	sc->sc_speed = ospeed;
    912 
    913 	/* And copy to tty. */
    914 	tp->t_ispeed = 0;
    915 	tp->t_ospeed = t->c_ospeed;
    916 	tp->t_cflag = t->c_cflag;
    917 
    918 	if (!sc->sc_heldchange) {
    919 		if (sc->sc_tx_busy) {
    920 			sc->sc_heldtbc = sc->sc_tbc;
    921 			sc->sc_tbc = 0;
    922 			sc->sc_heldchange = 1;
    923 		} else
    924 			sacom_loadchannelregs(sc);
    925 	}
    926 
    927 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    928 		/* Disable the high water mark. */
    929 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
    930 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
    931 			sacom_schedrx(sc);
    932 		}
    933 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
    934 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
    935 			sacom_hwiflow(sc);
    936 		}
    937 	}
    938 
    939 	COM_UNLOCK(sc);
    940 	splx(s);
    941 
    942 	(void) (*tp->t_linesw->l_modem)(tp, 1);
    943 
    944 #ifdef COM_DEBUG
    945 	if (sacom_debug)
    946 		comstatus(sc, "comparam ");
    947 #endif
    948 
    949 	return 0;
    950 }
    951 
    952 void
    953 sacom_iflush(struct sacom_softc *sc)
    954 {
    955 	bus_space_tag_t iot = sc->sc_iot;
    956 	bus_space_handle_t ioh = sc->sc_ioh;
    957 #ifdef DIAGNOSTIC
    958 	int reg;
    959 #endif
    960 	int timo;
    961 
    962 #ifdef DIAGNOSTIC
    963 	reg = 0xffff;
    964 #endif
    965 	timo = 50;
    966 	/* flush any pending I/O */
    967 	if (sc->sc_cr3 & CR3_RXE) {
    968 	while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_RNE)
    969 	    && --timo)
    970 #ifdef DIAGNOSTIC
    971 		reg =
    972 #else
    973 		    (void)
    974 #endif
    975 		    bus_space_read_4(iot, ioh, SACOM_DR);
    976 	}
    977 #if 0
    978 	/* XXX is it good idea to wait TX finish? */
    979 	if (sc->sc_cr3 & CR3_TXE) {
    980 	timo = 500;
    981 	while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_TBY)
    982 	    && --timo)
    983 		delay(100);
    984 	}
    985 #endif
    986 #ifdef DIAGNOSTIC
    987 	if (!timo)
    988 		printf("%s: sacom_iflush timeout %02x\n", sc->sc_dev.dv_xname,
    989 		       reg);
    990 #endif
    991 }
    992 
    993 void
    994 sacom_loadchannelregs(struct sacom_softc *sc)
    995 {
    996 	bus_space_tag_t iot = sc->sc_iot;
    997 	bus_space_handle_t ioh = sc->sc_ioh;
    998 
    999 	/* XXXXX necessary? */
   1000 	sacom_iflush(sc);
   1001 
   1002 	/* Need to stop engines first. */
   1003 	bus_space_write_4(iot, ioh, SACOM_CR3, 0);
   1004 
   1005 	bus_space_write_4(iot, ioh, SACOM_CR1, sc->sc_speed >> 8);
   1006 	bus_space_write_4(iot, ioh, SACOM_CR2, sc->sc_speed & 0xff);
   1007 
   1008 	bus_space_write_4(iot, ioh, SACOM_CR0, sc->sc_cr0);
   1009 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
   1010 }
   1011 
   1012 int
   1013 sacomhwiflow(struct tty *tp, int block)
   1014 {
   1015 #if 0
   1016 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
   1017 	int s;
   1018 
   1019 	if (COM_ISALIVE(sc) == 0)
   1020 		return 0;
   1021 
   1022 	if (sc->sc_mcr_rts == 0)
   1023 		return 0;
   1024 
   1025 	s = splserial();
   1026 	COM_LOCK(sc);
   1027 
   1028 	if (block) {
   1029 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1030 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1031 			sacom_hwiflow(sc);
   1032 		}
   1033 	} else {
   1034 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1035 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1036 			sacom_schedrx(sc);
   1037 		}
   1038 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1039 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1040 			sacom_hwiflow(sc);
   1041 		}
   1042 	}
   1043 
   1044 	COM_UNLOCK(sc);
   1045 	splx(s);
   1046 #endif
   1047 	return 1;
   1048 }
   1049 
   1050 /*
   1051  * (un)block input via hw flowcontrol
   1052  */
   1053 void
   1054 sacom_hwiflow(struct sacom_softc *sc)
   1055 {
   1056 #if 0
   1057 	bus_space_tag_t iot = sc->sc_iot;
   1058 	bus_space_handle_t ioh = sc->sc_ioh;
   1059 
   1060 	/* XXX implement */
   1061 #endif
   1062 }
   1063 
   1064 
   1065 void
   1066 sacomstart(struct tty *tp)
   1067 {
   1068 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
   1069 	bus_space_tag_t iot = sc->sc_iot;
   1070 	bus_space_handle_t ioh = sc->sc_ioh;
   1071 	int s;
   1072 
   1073 	if (COM_ISALIVE(sc) == 0)
   1074 		return;
   1075 
   1076 	s = spltty();
   1077 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1078 		goto out;
   1079 
   1080 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1081 		if (ISSET(tp->t_state, TS_ASLEEP)) {
   1082 			CLR(tp->t_state, TS_ASLEEP);
   1083 			wakeup(&tp->t_outq);
   1084 		}
   1085 		selwakeup(&tp->t_wsel);
   1086 		if (tp->t_outq.c_cc == 0)
   1087 			goto out;
   1088 	}
   1089 
   1090 	/* Grab the first contiguous region of buffer space. */
   1091 	{
   1092 		u_char *tba;
   1093 		int tbc;
   1094 
   1095 		tba = tp->t_outq.c_cf;
   1096 		tbc = ndqb(&tp->t_outq, 0);
   1097 
   1098 		(void)splserial();
   1099 		COM_LOCK(sc);
   1100 
   1101 		sc->sc_tba = tba;
   1102 		sc->sc_tbc = tbc;
   1103 	}
   1104 
   1105 	SET(tp->t_state, TS_BUSY);
   1106 	sc->sc_tx_busy = 1;
   1107 
   1108 	/* Enable transmit completion interrupts if necessary. */
   1109 	if (!ISSET(sc->sc_cr3, CR3_TIE)) {
   1110 		SET(sc->sc_cr3, CR3_TIE);
   1111 		bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
   1112 	}
   1113 
   1114 	/* Output the first chunk of the contiguous buffer. */
   1115 	sacom_filltx(sc);
   1116 
   1117 	COM_UNLOCK(sc);
   1118 out:
   1119 	splx(s);
   1120 	return;
   1121 }
   1122 
   1123 void
   1124 sacom_filltx(struct sacom_softc *sc)
   1125 {
   1126 	bus_space_tag_t iot = sc->sc_iot;
   1127 	bus_space_handle_t ioh = sc->sc_ioh;
   1128 	int c, n;
   1129 
   1130 	n = 0;
   1131 	while (bus_space_read_4(iot, ioh, SACOM_SR1) & SR1_TNF) {
   1132 		if (n == SACOM_TXFIFOLEN || n == sc->sc_tbc)
   1133 			break;
   1134 		c = *(sc->sc_tba + n);
   1135 		c &= 0xff;
   1136 		bus_space_write_4(iot, ioh, SACOM_DR, c);
   1137 		n++;
   1138 	}
   1139 	sc->sc_tbc -= n;
   1140 	sc->sc_tba += n;
   1141 }
   1142 
   1143 /*
   1144  * Stop output on a line.
   1145  */
   1146 void
   1147 sacomstop(struct tty *tp, int flag)
   1148 {
   1149 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
   1150 	int s;
   1151 
   1152 	s = splserial();
   1153 	COM_LOCK(sc);
   1154 	if (ISSET(tp->t_state, TS_BUSY)) {
   1155 		/* Stop transmitting at the next chunk. */
   1156 		sc->sc_tbc = 0;
   1157 		sc->sc_heldtbc = 0;
   1158 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1159 			SET(tp->t_state, TS_FLUSH);
   1160 	}
   1161 	COM_UNLOCK(sc);
   1162 	splx(s);
   1163 }
   1164 
   1165 static inline void
   1166 sacom_rxsoft(struct sacom_softc *sc, struct tty *tp)
   1167 {
   1168 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
   1169 	u_char *get, *end;
   1170 	u_int cc, scc;
   1171 	u_char sr1;
   1172 	int code;
   1173 	int s;
   1174 
   1175 	end = sc->sc_ebuf;
   1176 	get = sc->sc_rbget;
   1177 	scc = cc = SACOM_RING_SIZE - sc->sc_rbavail;
   1178 
   1179 	while (cc) {
   1180 		code = get[0];
   1181 		sr1 = get[1];
   1182 		if (ISSET(sr1, SR1_FRE))
   1183 			SET(code, TTY_FE);
   1184 		if (ISSET(sr1, SR1_PRE))
   1185 			SET(code, TTY_PE);
   1186 		if ((*rint)(code, tp) == -1) {
   1187 			/*
   1188 			 * The line discipline's buffer is out of space.
   1189 			 */
   1190 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1191 				/*
   1192 				 * We're either not using flow control, or the
   1193 				 * line discipline didn't tell us to block for
   1194 				 * some reason.  Either way, we have no way to
   1195 				 * know when there's more space available, so
   1196 				 * just drop the rest of the data.
   1197 				 */
   1198 				get += cc << 1;
   1199 				if (get >= end)
   1200 					get -= SACOM_RING_SIZE << 1;
   1201 				cc = 0;
   1202 			} else {
   1203 				/*
   1204 				 * Don't schedule any more receive processing
   1205 				 * until the line discipline tells us there's
   1206 				 * space available (through comhwiflow()).
   1207 				 * Leave the rest of the data in the input
   1208 				 * buffer.
   1209 				 */
   1210 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1211 			}
   1212 			break;
   1213 		}
   1214 		get += 2;
   1215 		if (get >= end)
   1216 			get = sc->sc_rbuf;
   1217 		cc--;
   1218 	}
   1219 
   1220 	if (cc != scc) {
   1221 		sc->sc_rbget = get;
   1222 		s = splserial();
   1223 		COM_LOCK(sc);
   1224 
   1225 		cc = sc->sc_rbavail += scc - cc;
   1226 		/* Buffers should be ok again, release possible block. */
   1227 		if (cc >= 1) {
   1228 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1229 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1230 				SET(sc->sc_cr3, CR3_RIE);
   1231 				bus_space_write_4(sc->sc_iot, sc->sc_ioh,
   1232 						  SACOM_CR3, sc->sc_cr3);
   1233 			}
   1234 		}
   1235 		COM_UNLOCK(sc);
   1236 		splx(s);
   1237 	}
   1238 }
   1239 
   1240 static inline void
   1241 sacom_txsoft(struct sacom_softc *sc, struct tty *tp)
   1242 {
   1243 
   1244 	CLR(tp->t_state, TS_BUSY);
   1245 	if (ISSET(tp->t_state, TS_FLUSH))
   1246 		CLR(tp->t_state, TS_FLUSH);
   1247 	else
   1248 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1249 	(*tp->t_linesw->l_start)(tp);
   1250 }
   1251 
   1252 static inline void
   1253 sacom_stsoft(struct sacom_softc *sc, struct tty *tp)
   1254 {
   1255 }
   1256 
   1257 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1258 void
   1259 sacomsoft(void *arg)
   1260 {
   1261 	struct sacom_softc *sc = arg;
   1262 	struct tty *tp;
   1263 
   1264 	if (COM_ISALIVE(sc) == 0)
   1265 		return;
   1266 
   1267 	{
   1268 #else
   1269 void
   1270 sacomsoft(void)
   1271 {
   1272 	struct sacom_softc	*sc;
   1273 	struct tty	*tp;
   1274 	int	unit;
   1275 
   1276 	for (unit = 0; unit < sacom_cd.cd_ndevs; unit++) {
   1277 		sc = device_lookup(&sacom_cd, unit);
   1278 		if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
   1279 			continue;
   1280 
   1281 		if (COM_ISALIVE(sc) == 0)
   1282 			continue;
   1283 
   1284 		tp = sc->sc_tty;
   1285 		if (tp == NULL)
   1286 			continue;
   1287 		if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
   1288 			continue;
   1289 #endif
   1290 		tp = sc->sc_tty;
   1291 
   1292 		if (sc->sc_rx_ready) {
   1293 			sc->sc_rx_ready = 0;
   1294 			sacom_rxsoft(sc, tp);
   1295 		}
   1296 
   1297 		if (sc->sc_st_check) {
   1298 			sc->sc_st_check = 0;
   1299 			sacom_stsoft(sc, tp);
   1300 		}
   1301 
   1302 		if (sc->sc_tx_done) {
   1303 			sc->sc_tx_done = 0;
   1304 			sacom_txsoft(sc, tp);
   1305 		}
   1306 	}
   1307 
   1308 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
   1309 #endif
   1310 }
   1311 
   1312 #ifdef __ALIGN_BRACKET_LEVEL_FOR_CTAGS
   1313 	/* there has got to be a better way to do comsoft() */
   1314 }}
   1315 #endif
   1316 
   1317 int
   1318 sacomintr(void *arg)
   1319 {
   1320 	struct sacom_softc *sc = arg;
   1321 	bus_space_tag_t iot = sc->sc_iot;
   1322 	bus_space_handle_t ioh = sc->sc_ioh;
   1323 	u_char *put, *end;
   1324 	u_int cc;
   1325 	u_int sr0, sr1;
   1326 
   1327 	if (COM_ISALIVE(sc) == 0)
   1328 		return 0;
   1329 
   1330 	COM_LOCK(sc);
   1331 	sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
   1332 	if (!sr0) {
   1333 		COM_UNLOCK(sc);
   1334 		return 0;
   1335 	}
   1336 	if (ISSET(sr0, SR0_EIF))
   1337 		/* XXX silently discard error bits */
   1338 		bus_space_read_4(iot, ioh, SACOM_DR);
   1339 	if (ISSET(sr0, SR0_RBB))
   1340 		bus_space_write_4(iot, ioh, SACOM_SR0, SR0_RBB);
   1341 	if (ISSET(sr0, SR0_REB)) {
   1342 		bus_space_write_4(iot, ioh, SACOM_SR0, SR0_REB);
   1343 #if defined(DDB) || defined(KGDB)
   1344 #ifndef DDB_BREAK_CHAR
   1345 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1346 			console_debugger();
   1347 		}
   1348 #endif
   1349 #endif /* DDB || KGDB */
   1350 	}
   1351 
   1352 
   1353 	end = sc->sc_ebuf;
   1354 	put = sc->sc_rbput;
   1355 	cc = sc->sc_rbavail;
   1356 
   1357 	sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
   1358 	if (ISSET(sr0, SR0_RFS | SR0_RID)) {
   1359 		if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1360 			while (cc > 0) {
   1361 				if (!ISSET(sr1, SR1_RNE)) {
   1362 					bus_space_write_4(iot, ioh, SACOM_SR0,
   1363 							  SR0_RID);
   1364 					break;
   1365 				}
   1366 				put[0] = bus_space_read_4(iot, ioh, SACOM_DR);
   1367 				put[1] = sr1;
   1368 #if defined(DDB) && defined(DDB_BREAK_CHAR)
   1369 				if (put[0] == DDB_BREAK_CHAR &&
   1370 				    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1371 					console_debugger();
   1372 
   1373 					sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
   1374 					continue;
   1375 				}
   1376 #endif
   1377 				put += 2;
   1378 				if (put >= end)
   1379 					put = sc->sc_rbuf;
   1380 				cc--;
   1381 
   1382 				sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
   1383 			}
   1384 
   1385 			/*
   1386 			 * Current string of incoming characters ended because
   1387 			 * no more data was available or we ran out of space.
   1388 			 * Schedule a receive event if any data was received.
   1389 			 * If we're out of space, turn off receive interrupts.
   1390 			 */
   1391 			sc->sc_rbput = put;
   1392 			sc->sc_rbavail = cc;
   1393 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1394 				sc->sc_rx_ready = 1;
   1395 
   1396 			/* XXX do RX hardware flow control */
   1397 
   1398 			/*
   1399 			 * If we're out of space, disable receive interrupts
   1400 			 * until the queue has drained a bit.
   1401 			 */
   1402 			if (!cc) {
   1403 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1404 				CLR(sc->sc_cr3, CR3_RIE);
   1405 				bus_space_write_4(iot, ioh, SACOM_CR3,
   1406 						  sc->sc_cr3);
   1407 			}
   1408 		} else {
   1409 #ifdef DIAGNOSTIC
   1410 			panic("sacomintr: we shouldn't reach here");
   1411 #endif
   1412 			CLR(sc->sc_cr3, CR3_RIE);
   1413 			bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
   1414 		}
   1415 	}
   1416 
   1417 	/*
   1418 	 * Done handling any receive interrupts. See if data can be
   1419 	 * transmitted as well. Schedule tx done event if no data left
   1420 	 * and tty was marked busy.
   1421 	 */
   1422 	sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
   1423 	if (ISSET(sr0, SR0_TFS)) {
   1424 		/*
   1425 		 * If we've delayed a parameter change, do it now, and restart
   1426 		 * output.
   1427 		 * XXX sacom_loadchannelregs() waits TX completion,
   1428 		 * XXX resulting in ~0.1s hang (300bps, 4 bytes) in worst case
   1429 		 */
   1430 		if (sc->sc_heldchange) {
   1431 			sacom_loadchannelregs(sc);
   1432 			sc->sc_heldchange = 0;
   1433 			sc->sc_tbc = sc->sc_heldtbc;
   1434 			sc->sc_heldtbc = 0;
   1435 		}
   1436 
   1437 		/* Output the next chunk of the contiguous buffer, if any. */
   1438 		if (sc->sc_tbc > 0) {
   1439 			sacom_filltx(sc);
   1440 		} else {
   1441 			/* Disable transmit completion interrupts if necessary. */
   1442 			if (ISSET(sc->sc_cr3, CR3_TIE)) {
   1443 				CLR(sc->sc_cr3, CR3_TIE);
   1444 				bus_space_write_4(iot, ioh, SACOM_CR3,
   1445 						  sc->sc_cr3);
   1446 			}
   1447 			if (sc->sc_tx_busy) {
   1448 				sc->sc_tx_busy = 0;
   1449 				sc->sc_tx_done = 1;
   1450 			}
   1451 		}
   1452 	}
   1453 	COM_UNLOCK(sc);
   1454 
   1455 	/* Wake up the poller. */
   1456 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1457 	softintr_schedule(sc->sc_si);
   1458 #else
   1459 	setsoftserial();
   1460 #endif
   1461 
   1462 #if NRND > 0 && defined(RND_COM)
   1463 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   1464 #endif
   1465 	return 1;
   1466 }
   1467 
   1468 static void
   1469 sacom_j720_init(struct sa11x0_softc *parent, struct sacom_softc *sc) {
   1470 
   1471 	/* XXX  this should be done at sc->enable function */
   1472 	bus_space_write_4(parent->sc_iot, parent->sc_gpioh,
   1473 	    SAGPIO_PCR, 0xa0000);
   1474 	bus_space_write_4(parent->sc_iot, parent->sc_gpioh,
   1475 	    SAGPIO_PSR, 0x100);
   1476 }
   1477 
   1478 /* Initialization for serial console */
   1479 int
   1480 sacominit(bus_space_tag_t iot, bus_addr_t iobase, int baud, tcflag_t cflag,
   1481     bus_space_handle_t *iohp)
   1482 {
   1483 	int brd, cr0;
   1484 
   1485 	if (bus_space_map(iot, iobase, SACOM_NPORTS, 0, iohp))
   1486 		printf("register map failed\n");
   1487 
   1488 	/* wait for the Tx queue to drain and disable the UART */
   1489 	while (bus_space_read_4(iot, *iohp, SACOM_SR1) & SR1_TBY)
   1490 		continue;
   1491 	bus_space_write_4(iot, *iohp, SACOM_CR3, 0);
   1492 
   1493 	cr0  = cflag2cr0(cflag);
   1494 	bus_space_write_4(iot, *iohp, SACOM_CR0, cr0);
   1495 
   1496 	brd = SACOMSPEED(baud);
   1497 	sacomconsrate = baud;
   1498 	sacomconsaddr = iobase;
   1499 	sacomconscflag = cflag;
   1500 	/* XXX assumes little endian */
   1501 	bus_space_write_4(iot, *iohp, SACOM_CR1, brd >> 8);
   1502 	bus_space_write_4(iot, *iohp, SACOM_CR2, brd & 0xff);
   1503 
   1504 	/* enable the UART */
   1505 	bus_space_write_4(iot, *iohp, SACOM_CR3, CR3_RXE | CR3_TXE);
   1506 
   1507 	return 0;
   1508 }
   1509 
   1510 void
   1511 sacomcnprobe(struct consdev *cp)
   1512 {
   1513 	cp->cn_pri = CN_REMOTE;
   1514 }
   1515 
   1516 void
   1517 sacomcninit(struct consdev *cp)
   1518 {
   1519 	if (cp == NULL) {
   1520 		/* XXX cp == NULL means that MMU is disabled. */
   1521 		sacomconsioh = SACOM3_BASE;
   1522 		sacomconstag = &sa11x0_bs_tag;
   1523 		cn_tab = &sacomcons;
   1524 		return;
   1525 	}
   1526 
   1527 	if (sacominit(&sa11x0_bs_tag, CONADDR, CONSPEED,
   1528 			  CONMODE, &sacomconsioh))
   1529 		panic("can't init serial console @%x", CONADDR);
   1530 	cn_tab = &sacomcons;
   1531 	sacomconstag = &sa11x0_bs_tag;
   1532 }
   1533 
   1534 int
   1535 sacomcngetc(dev_t dev)
   1536 {
   1537 	int c, s;
   1538 
   1539 	s = spltty();	/* XXX do we need this? */
   1540 
   1541 	while (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1542 		 & SR1_RNE)) {
   1543 #if defined(DDB) || defined(KGDB)
   1544 #ifndef DDB_BREAK_CHAR
   1545 		u_int sr0;
   1546 		extern int db_active;
   1547 
   1548 		sr0 = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR0);
   1549 		if (ISSET(sr0, SR0_RBB))
   1550 			bus_space_write_4(sacomconstag, sacomconsioh,
   1551 					  SACOM_SR0, SR0_RBB);
   1552 		if (ISSET(sr0, SR0_REB)) {
   1553 			bus_space_write_4(sacomconstag, sacomconsioh,
   1554 					  SACOM_SR0, SR0_REB);
   1555 			if (db_active == 0)
   1556 				console_debugger();
   1557 		}
   1558 #endif
   1559 #endif /* DDB || KGDB */
   1560 	}
   1561 
   1562 	c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
   1563 	c &= 0xff;
   1564 	splx(s);
   1565 
   1566 	return c;
   1567 }
   1568 
   1569 void
   1570 sacomcnputc(dev_t dev, int c)
   1571 {
   1572 	int s;
   1573 
   1574 	s = spltty();	/* XXX do we need this? */
   1575 
   1576 	while (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1577 	    & SR1_TNF))
   1578 		continue;
   1579 
   1580 	bus_space_write_4(sacomconstag, sacomconsioh, SACOM_DR, c);
   1581 	splx(s);
   1582 }
   1583 
   1584 void
   1585 sacomcnpollc(dev_t dev, int on)
   1586 {
   1587 
   1588 }
   1589 
   1590 #if 0
   1591 #ifdef DEBUG
   1592 int
   1593 sacomcncharpoll(void)
   1594 {
   1595 	int c;
   1596 
   1597 	if (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1598 		 & SR1_RNE))
   1599 		return -1;
   1600 
   1601 	c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
   1602 	c &= 0xff;
   1603 
   1604 	return c;
   1605 }
   1606 #endif
   1607 #endif
   1608 
   1609 /* helper function to identify the com ports used by
   1610  console or KGDB (and not yet autoconf attached) */
   1611 int
   1612 sacom_is_console(bus_space_tag_t iot, bus_addr_t iobase,
   1613     bus_space_handle_t *ioh)
   1614 {
   1615 	bus_space_handle_t help;
   1616 
   1617 	if (!sacomconsattached &&
   1618 	    iot == sacomconstag && iobase == sacomconsaddr)
   1619 		help = sacomconsioh;
   1620 	else
   1621 		return 0;
   1622 
   1623 	if (ioh)
   1624 		*ioh = help;
   1625 	return 1;
   1626 }
   1627