Home | History | Annotate | Line # | Download | only in sa11x0
sa11x0_com.c revision 1.30
      1 /*      $NetBSD: sa11x0_com.c,v 1.30 2006/03/26 04:42:50 thorpej 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.30 2006/03/26 04:42:50 thorpej 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 
    100 #include <dev/cons.h>
    101 
    102 #include <machine/bus.h>
    103 #include <arm/sa11x0/sa11x0_reg.h>
    104 #include <arm/sa11x0/sa11x0_var.h>
    105 #include <arm/sa11x0/sa11x0_comreg.h>
    106 #include <arm/sa11x0/sa11x0_comvar.h>
    107 #include <arm/sa11x0/sa11x0_gpioreg.h>
    108 
    109 #ifdef hpcarm
    110 #include <hpc/include/platid.h>
    111 #include <hpc/include/platid_mask.h>
    112 #endif
    113 
    114 #include "sacom.h"
    115 
    116 dev_type_open(sacomopen);
    117 dev_type_close(sacomclose);
    118 dev_type_read(sacomread);
    119 dev_type_write(sacomwrite);
    120 dev_type_ioctl(sacomioctl);
    121 dev_type_stop(sacomstop);
    122 dev_type_tty(sacomtty);
    123 dev_type_poll(sacompoll);
    124 
    125 const struct cdevsw sacom_cdevsw = {
    126 	sacomopen, sacomclose, sacomread, sacomwrite, sacomioctl,
    127 	sacomstop, sacomtty, sacompoll, nommap, ttykqfilter, D_TTY
    128 };
    129 
    130 static	int	sacom_match(struct device *, struct cfdata *, void *);
    131 static	void	sacom_attach(struct device *, struct device *, void *);
    132 static	void	sacom_filltx(struct sacom_softc *);
    133 static	void	sacom_attach_subr(struct sacom_softc *);
    134 #if defined(DDB) || defined(KGDB)
    135 static	void	sacom_enable_debugport(struct sacom_softc *);
    136 #endif
    137 int		sacom_detach(struct device *, int);
    138 void		sacom_config(struct sacom_softc *);
    139 int		sacom_activate(struct device *, enum devact);
    140 void		sacom_shutdown(struct sacom_softc *);
    141 static	u_int	cflag2cr0(tcflag_t);
    142 int		sacomparam(struct tty *, struct termios *);
    143 void		sacomstart(struct tty *);
    144 int		sacomhwiflow(struct tty *, int);
    145 
    146 void		sacom_loadchannelregs(struct sacom_softc *);
    147 void		sacom_hwiflow(struct sacom_softc *);
    148 void		sacom_break(struct sacom_softc *, int);
    149 void		sacom_modem(struct sacom_softc *, int);
    150 void		tiocm_to_sacom(struct sacom_softc *, u_long, int);
    151 int		sacom_to_tiocm(struct sacom_softc *);
    152 void		sacom_iflush(struct sacom_softc *);
    153 
    154 int		sacominit(bus_space_tag_t, bus_addr_t, int, tcflag_t,
    155 			  bus_space_handle_t *);
    156 int		sacom_is_console(bus_space_tag_t, bus_addr_t,
    157 				 bus_space_handle_t *);
    158 
    159 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    160 void 		sacomsoft(void *);
    161 #else
    162 void 		sacomsoft(void);
    163 #endif
    164 
    165 static inline void sacom_rxsoft(struct sacom_softc *, struct tty *);
    166 static inline void sacom_txsoft(struct sacom_softc *, struct tty *);
    167 static inline void sacom_stsoft(struct sacom_softc *, struct tty *);
    168 static inline void sacom_schedrx(struct sacom_softc *);
    169 
    170 #ifdef hpcarm
    171 /* HPCARM specific functions */
    172 static void	sacom_j720_init(struct sa11x0_softc *, struct sacom_softc *);
    173 #endif
    174 
    175 #define COMUNIT_MASK	0x7ffff
    176 #define COMDIALOUT_MASK	0x80000
    177 
    178 #define COMUNIT(x)	(minor(x) & COMUNIT_MASK)
    179 #define COMDIALOUT(x)	(minor(x) & COMDIALOUT_MASK)
    180 
    181 #define COM_ISALIVE(sc)	((sc)->enabled != 0 && \
    182 			 device_is_active(&(sc)->sc_dev))
    183 
    184 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
    185 #define COM_LOCK(sc)
    186 #define COM_UNLOCK(sc)
    187 
    188 int		sacomintr(void *);
    189 int		sacomcngetc(dev_t);
    190 void		sacomcnputc(dev_t, int);
    191 void		sacomcnpollc(dev_t, int);
    192 
    193 void		sacomcnprobe(struct consdev *);
    194 void		sacomcninit(struct consdev *);
    195 
    196 extern struct bus_space sa11x0_bs_tag;
    197 
    198 static bus_space_tag_t sacomconstag;
    199 static bus_space_handle_t sacomconsioh;
    200 static bus_addr_t sacomconsaddr = SACOM3_BASE; /* XXX */
    201 
    202 static int sacomconsattached;
    203 static int sacomconsrate;
    204 static tcflag_t sacomconscflag;
    205 
    206 CFATTACH_DECL(sacom, sizeof(struct sacom_softc),
    207     sacom_match, sacom_attach, NULL, NULL);
    208 extern struct cfdriver sacom_cd;
    209 
    210 #ifdef hpcarm
    211 struct platid_data sacom_platid_table[] = {
    212 	{ &platid_mask_MACH_HP_JORNADA_7XX, sacom_j720_init },
    213 	{ NULL, NULL }
    214 };
    215 #endif
    216 
    217 struct consdev sacomcons = {
    218 	NULL, NULL, sacomcngetc, sacomcnputc, sacomcnpollc, NULL,
    219 	NULL, NULL, NODEV, CN_NORMAL
    220 };
    221 
    222 #ifndef CONMODE
    223 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    224 #endif
    225 #ifndef CONSPEED
    226 #define CONSPEED 9600
    227 #endif
    228 #ifndef CONADDR
    229 #define CONADDR SACOM3_BASE
    230 #endif
    231 
    232 static int
    233 sacom_match(struct device *parent, struct cfdata *match, void *aux)
    234 {
    235 	return (1);
    236 }
    237 
    238 void
    239 sacom_attach(struct device *parent, struct device *self, void *aux)
    240 {
    241 	struct sacom_softc *sc = (struct sacom_softc*)self;
    242 	struct sa11x0_attach_args *sa = aux;
    243 
    244 #ifdef hpcarm
    245 	struct platid_data *p;
    246 	void (*mdinit)(struct device *, struct sacom_softc *);
    247 #endif
    248 
    249 	printf("\n");
    250 
    251 	sc->sc_iot = sa->sa_iot;
    252 	sc->sc_baseaddr = sa->sa_addr;
    253 
    254 	if(bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
    255 			&sc->sc_ioh)) {
    256 		printf("%s: unable to map registers\n", sc->sc_dev.dv_xname);
    257 		return;
    258 	}
    259 
    260 	printf("%s: ", sc->sc_dev.dv_xname);
    261 	switch(sc->sc_baseaddr) {
    262 	case 0x80050000:
    263 		printf("SA11x0 UART3\n");
    264 		break;
    265 	case 0x80010000:
    266 		printf("SA11x0 UART1\n");
    267 		break;
    268 	case 0x80030000:
    269 		printf("SA11x0 UART2 (IRDA)\n");
    270 		break;
    271 	default:
    272 		printf("unknown SA11x0 UART\n");
    273 		break;
    274 	}
    275 
    276 	sacom_attach_subr(sc);
    277 
    278 #ifdef hpcarm
    279 	/* Do hpcarm specific initialization, if any */
    280 	if ((p = platid_search_data(&platid, sacom_platid_table)) != NULL) {
    281 		mdinit = p->data;
    282 		(mdinit)(parent, sc);
    283 	}
    284 #endif
    285 
    286 	sa11x0_intr_establish(0, sa->sa_intr, 1, IPL_SERIAL, sacomintr, sc);
    287 }
    288 
    289 void
    290 sacom_attach_subr(struct sacom_softc *sc)
    291 {
    292 	bus_addr_t iobase = sc->sc_baseaddr;
    293 	bus_space_tag_t iot = sc->sc_iot;
    294 	struct tty *tp;
    295 
    296 	/* XXX Do we need to disable interrupts here? */
    297 
    298 	if (iot == sacomconstag && iobase == sacomconsaddr) {
    299 		sacomconsattached = 1;
    300 		sc->sc_speed = SACOMSPEED(sacomconsrate);
    301 
    302 		/* Make sure the console is always "hardwired". */
    303 		delay(10000);			/* wait for output to finish */
    304 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    305 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    306 	}
    307 
    308 	tp = ttymalloc();
    309 	tp->t_oproc = sacomstart;
    310 	tp->t_param = sacomparam;
    311 	tp->t_hwiflow = sacomhwiflow;
    312 
    313 	sc->sc_tty = tp;
    314 	sc->sc_rbuf = malloc(SACOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
    315 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    316 	sc->sc_rbavail = SACOM_RING_SIZE;
    317 	if (sc->sc_rbuf == NULL) {
    318 		printf("%s: unable to allocate ring buffer\n",
    319 		    sc->sc_dev.dv_xname);
    320 		return;
    321 	}
    322 	sc->sc_ebuf = sc->sc_rbuf + (SACOM_RING_SIZE << 1);
    323 	sc->sc_tbc = 0;
    324 
    325 	tty_attach(tp);
    326 
    327 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    328 		int maj;
    329 
    330 		/* locate the major number */
    331 		maj = cdevsw_lookup_major(&sacom_cdevsw);
    332 
    333 		cn_tab->cn_dev = makedev(maj, device_unit(&sc->sc_dev));
    334 
    335 		delay(10000); /* XXX */
    336 		printf("%s: console\n", sc->sc_dev.dv_xname);
    337 		delay(10000); /* XXX */
    338 	}
    339 
    340 
    341 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    342 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, sacomsoft, sc);
    343 #endif
    344 
    345 #if NRND > 0 && defined(RND_COM)
    346 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    347 			  RND_TYPE_TTY, 0);
    348 #endif
    349 
    350 	/* if there are no enable/disable functions, assume the device
    351 	   is always enabled */
    352 	if (!sc->enable)
    353 		sc->enabled = 1;
    354 
    355 	sacom_config(sc);
    356 
    357 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
    358 }
    359 
    360 /* This is necessary when dynamically changing SAIP configuration. */
    361 int
    362 sacom_detach(struct device *self, int flags)
    363 {
    364 	struct sacom_softc *sc = (struct sacom_softc *)self;
    365 	int maj, mn;
    366 
    367 	/* locate the major number */
    368 	maj = cdevsw_lookup_major(&sacom_cdevsw);
    369 
    370 	/* Nuke the vnodes for any open instances. */
    371 	mn = device_unit(self);
    372 	vdevgone(maj, mn, mn, VCHR);
    373 
    374 	mn |= COMDIALOUT_MASK;
    375 	vdevgone(maj, mn, mn, VCHR);
    376 
    377 	/* Free the receive buffer. */
    378 	free(sc->sc_rbuf, M_DEVBUF);
    379 
    380 	/* Detach and free the tty. */
    381 	tty_detach(sc->sc_tty);
    382 	ttyfree(sc->sc_tty);
    383 
    384 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    385 	/* Unhook the soft interrupt handler. */
    386 	softintr_disestablish(sc->sc_si);
    387 #endif
    388 
    389 #if NRND > 0 && defined(RND_COM)
    390 	/* Unhook the entropy source. */
    391 	rnd_detach_source(&sc->rnd_source);
    392 #endif
    393 
    394 	return (0);
    395 }
    396 
    397 void
    398 sacom_config(struct sacom_softc *sc)
    399 {
    400 	bus_space_tag_t iot = sc->sc_iot;
    401 	bus_space_handle_t ioh = sc->sc_ioh;
    402 
    403 	/* Disable engine before configuring the device. */
    404 	sc->sc_cr3 = 0;
    405 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
    406 
    407 #ifdef DDB
    408 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    409 		sacom_enable_debugport(sc);
    410 #endif
    411 }
    412 
    413 #ifdef DDB
    414 static void
    415 sacom_enable_debugport(struct sacom_softc *sc)
    416 {
    417 	bus_space_tag_t iot = sc->sc_iot;
    418 	bus_space_handle_t ioh = sc->sc_ioh;
    419 	int s;
    420 
    421 	s = splserial();
    422 	COM_LOCK(sc);
    423 	sc->sc_cr3 = CR3_RXE | CR3_TXE;
    424 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
    425 	COM_UNLOCK(sc);
    426 	splx(s);
    427 }
    428 #endif
    429 
    430 int
    431 sacom_activate(struct device *self, enum devact act)
    432 {
    433 	struct sacom_softc *sc = (struct sacom_softc *)self;
    434 	int s, rv = 0;
    435 
    436 	s = splserial();
    437 	COM_LOCK(sc);
    438 	switch (act) {
    439 	case DVACT_ACTIVATE:
    440 		rv = EOPNOTSUPP;
    441 		break;
    442 
    443 	case DVACT_DEACTIVATE:
    444 		if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB)) {
    445 			rv = EBUSY;
    446 			break;
    447 		}
    448 
    449 		if (sc->disable != NULL && sc->enabled != 0) {
    450 			(*sc->disable)(sc);
    451 			sc->enabled = 0;
    452 		}
    453 		break;
    454 	}
    455 
    456 	COM_UNLOCK(sc);
    457 	splx(s);
    458 	return (rv);
    459 }
    460 
    461 void
    462 sacom_shutdown(struct sacom_softc *sc)
    463 {
    464 	struct tty *tp = sc->sc_tty;
    465 	int s;
    466 
    467 	s = splserial();
    468 	COM_LOCK(sc);
    469 
    470 	/* Clear any break condition set with TIOCSBRK. */
    471 	sacom_break(sc, 0);
    472 
    473 	/*
    474 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    475 	 * notice even if we immediately open the port again.
    476 	 * Avoid tsleeping above splhigh().
    477 	 */
    478 	if (ISSET(tp->t_cflag, HUPCL)) {
    479 		sacom_modem(sc, 0);
    480 		COM_UNLOCK(sc);
    481 		splx(s);
    482 		/* XXX tsleep will only timeout */
    483 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    484 		s = splserial();
    485 		COM_LOCK(sc);
    486 	}
    487 
    488 	/* Turn off interrupts. */
    489 	sc->sc_cr3 = 0;
    490 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3, sc->sc_cr3);
    491 
    492 	if (sc->disable) {
    493 #ifdef DIAGNOSTIC
    494 		if (!sc->enabled)
    495 			panic("sacom_shutdown: not enabled?");
    496 #endif
    497 		(*sc->disable)(sc);
    498 		sc->enabled = 0;
    499 	}
    500 	COM_UNLOCK(sc);
    501 	splx(s);
    502 }
    503 
    504 int
    505 sacomopen(dev_t dev, int flag, int mode, struct lwp *l)
    506 {
    507 	struct sacom_softc *sc;
    508 	struct tty *tp;
    509 	int s, s2;
    510 	int error;
    511 
    512 	sc = device_lookup(&sacom_cd, COMUNIT(dev));
    513 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
    514 		sc->sc_rbuf == NULL)
    515 		return (ENXIO);
    516 
    517 	if (!device_is_active(&sc->sc_dev))
    518 		return (ENXIO);
    519 
    520 	tp = sc->sc_tty;
    521 
    522 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    523 	    ISSET(tp->t_state, TS_XCLUDE) &&
    524 	    suser(l->l_proc->p_ucred, &l->l_proc->p_acflag) != 0)
    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 = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
    761 		if (error)
    762 			break;
    763 		sc->sc_swflags = *(int *)data;
    764 		break;
    765 
    766 	case TIOCMSET:
    767 	case TIOCMBIS:
    768 	case TIOCMBIC:
    769 		tiocm_to_sacom(sc, cmd, *(int *)data);
    770 		break;
    771 
    772 	case TIOCMGET:
    773 		*(int *)data = sacom_to_tiocm(sc);
    774 		break;
    775 
    776 	default:
    777 		error = EPASSTHROUGH;
    778 		break;
    779 	}
    780 
    781 	COM_UNLOCK(sc);
    782 	splx(s);
    783 
    784 #ifdef COM_DEBUG
    785 	if (sacom_debug)
    786 		comstatus(sc, "comioctl ");
    787 #endif
    788 
    789 	return (error);
    790 }
    791 
    792 static inline void
    793 sacom_schedrx(struct sacom_softc *sc)
    794 {
    795 
    796 	sc->sc_rx_ready = 1;
    797 
    798 	/* Wake up the poller. */
    799 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    800 	softintr_schedule(sc->sc_si);
    801 #else
    802 	setsoftserial();
    803 #endif
    804 }
    805 
    806 void
    807 sacom_break(struct sacom_softc *sc, int onoff)
    808 {
    809 
    810 	if (onoff)
    811 		SET(sc->sc_cr3, CR3_BRK);
    812 	else
    813 		CLR(sc->sc_cr3, CR3_BRK);
    814 
    815 	if (!sc->sc_heldchange) {
    816 		if (sc->sc_tx_busy) {
    817 			sc->sc_heldtbc = sc->sc_tbc;
    818 			sc->sc_tbc = 0;
    819 			sc->sc_heldchange = 1;
    820 		} else
    821 			sacom_loadchannelregs(sc);
    822 	}
    823 }
    824 
    825 void
    826 sacom_modem(struct sacom_softc *sc, int onoff)
    827 {
    828 	if (!sc->sc_heldchange) {
    829 		if (sc->sc_tx_busy) {
    830 			sc->sc_heldtbc = sc->sc_tbc;
    831 			sc->sc_tbc = 0;
    832 			sc->sc_heldchange = 1;
    833 		} else
    834 			sacom_loadchannelregs(sc);
    835 	}
    836 }
    837 
    838 void
    839 tiocm_to_sacom(struct sacom_softc *sc, u_long how, int ttybits)
    840 {
    841 }
    842 
    843 int
    844 sacom_to_tiocm(struct sacom_softc *sc)
    845 {
    846 	int ttybits = 0;
    847 
    848 	if (sc->sc_cr3 != 0)
    849 		SET(ttybits, TIOCM_LE);
    850 
    851 	return (ttybits);
    852 }
    853 
    854 static u_int
    855 cflag2cr0(tcflag_t cflag)
    856 {
    857 	u_int cr0;
    858 
    859 	cr0  = (cflag & PARENB) ? CR0_PE : 0;
    860 	cr0 |= (cflag & PARODD) ? 0 : CR0_OES;
    861 	cr0 |= (cflag & CSTOPB) ? CR0_SBS : 0;
    862 	cr0 |= ((cflag & CSIZE) == CS8) ? CR0_DSS : 0;
    863 
    864 	return (cr0);
    865 }
    866 
    867 int
    868 sacomparam(struct tty *tp, struct termios *t)
    869 {
    870 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
    871 	int ospeed = SACOMSPEED(t->c_ospeed);
    872 	u_int cr0;
    873 	int s;
    874 
    875 	if (COM_ISALIVE(sc) == 0)
    876 		return (EIO);
    877 
    878 	/* Check requested parameters. */
    879 	if (ospeed < 0)
    880 		return (EINVAL);
    881 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    882 		return (EINVAL);
    883 
    884 	/*
    885 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    886 	 * is always active.
    887 	 */
    888 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    889 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    890 		SET(t->c_cflag, CLOCAL);
    891 		CLR(t->c_cflag, HUPCL);
    892 	}
    893 
    894 	/*
    895 	 * If there were no changes, don't do anything.  This avoids dropping
    896 	 * input and improves performance when all we did was frob things like
    897 	 * VMIN and VTIME.
    898 	 */
    899 	if (tp->t_ospeed == t->c_ospeed &&
    900 	    tp->t_cflag == t->c_cflag)
    901 		return (0);
    902 
    903 	cr0 = cflag2cr0(t->c_cflag);
    904 
    905 	s = splserial();
    906 	COM_LOCK(sc);
    907 
    908 	sc->sc_cr0 = cr0;
    909 
    910 	sc->sc_speed = ospeed;
    911 
    912 	/* And copy to tty. */
    913 	tp->t_ispeed = 0;
    914 	tp->t_ospeed = t->c_ospeed;
    915 	tp->t_cflag = t->c_cflag;
    916 
    917 	if (!sc->sc_heldchange) {
    918 		if (sc->sc_tx_busy) {
    919 			sc->sc_heldtbc = sc->sc_tbc;
    920 			sc->sc_tbc = 0;
    921 			sc->sc_heldchange = 1;
    922 		} else
    923 			sacom_loadchannelregs(sc);
    924 	}
    925 
    926 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    927 		/* Disable the high water mark. */
    928 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
    929 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
    930 			sacom_schedrx(sc);
    931 		}
    932 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
    933 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
    934 			sacom_hwiflow(sc);
    935 		}
    936 	}
    937 
    938 	COM_UNLOCK(sc);
    939 	splx(s);
    940 
    941 	(void) (*tp->t_linesw->l_modem)(tp, 1);
    942 
    943 #ifdef COM_DEBUG
    944 	if (sacom_debug)
    945 		comstatus(sc, "comparam ");
    946 #endif
    947 
    948 	return (0);
    949 }
    950 
    951 void
    952 sacom_iflush(struct sacom_softc *sc)
    953 {
    954 	bus_space_tag_t iot = sc->sc_iot;
    955 	bus_space_handle_t ioh = sc->sc_ioh;
    956 #ifdef DIAGNOSTIC
    957 	int reg;
    958 #endif
    959 	int timo;
    960 
    961 #ifdef DIAGNOSTIC
    962 	reg = 0xffff;
    963 #endif
    964 	timo = 50;
    965 	/* flush any pending I/O */
    966 	if (sc->sc_cr3 & CR3_RXE) {
    967 	while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_RNE)
    968 	    && --timo)
    969 #ifdef DIAGNOSTIC
    970 		reg =
    971 #else
    972 		    (void)
    973 #endif
    974 		    bus_space_read_4(iot, ioh, SACOM_DR);
    975 	}
    976 #if 0
    977 	/* XXX is it good idea to wait TX finish? */
    978 	if (sc->sc_cr3 & CR3_TXE) {
    979 	timo = 500;
    980 	while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_TBY)
    981 	    && --timo)
    982 		delay(100);
    983 	}
    984 #endif
    985 #ifdef DIAGNOSTIC
    986 	if (!timo)
    987 		printf("%s: sacom_iflush timeout %02x\n", sc->sc_dev.dv_xname,
    988 		       reg);
    989 #endif
    990 }
    991 
    992 void
    993 sacom_loadchannelregs(struct sacom_softc *sc)
    994 {
    995 	bus_space_tag_t iot = sc->sc_iot;
    996 	bus_space_handle_t ioh = sc->sc_ioh;
    997 
    998 	/* XXXXX necessary? */
    999 	sacom_iflush(sc);
   1000 
   1001 	/* Need to stop engines first. */
   1002 	bus_space_write_4(iot, ioh, SACOM_CR3, 0);
   1003 
   1004 	bus_space_write_4(iot, ioh, SACOM_CR1, sc->sc_speed >> 8);
   1005 	bus_space_write_4(iot, ioh, SACOM_CR2, sc->sc_speed & 0xff);
   1006 
   1007 	bus_space_write_4(iot, ioh, SACOM_CR0, sc->sc_cr0);
   1008 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
   1009 }
   1010 
   1011 int
   1012 sacomhwiflow(struct tty *tp, int block)
   1013 {
   1014 #if 0
   1015 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
   1016 	int s;
   1017 
   1018 	if (COM_ISALIVE(sc) == 0)
   1019 		return (0);
   1020 
   1021 	if (sc->sc_mcr_rts == 0)
   1022 		return (0);
   1023 
   1024 	s = splserial();
   1025 	COM_LOCK(sc);
   1026 
   1027 	if (block) {
   1028 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1029 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1030 			sacom_hwiflow(sc);
   1031 		}
   1032 	} else {
   1033 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1034 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1035 			sacom_schedrx(sc);
   1036 		}
   1037 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1038 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1039 			sacom_hwiflow(sc);
   1040 		}
   1041 	}
   1042 
   1043 	COM_UNLOCK(sc);
   1044 	splx(s);
   1045 #endif
   1046 	return (1);
   1047 }
   1048 
   1049 /*
   1050  * (un)block input via hw flowcontrol
   1051  */
   1052 void
   1053 sacom_hwiflow(struct sacom_softc *sc)
   1054 {
   1055 #if 0
   1056 	bus_space_tag_t iot = sc->sc_iot;
   1057 	bus_space_handle_t ioh = sc->sc_ioh;
   1058 
   1059 	/* XXX implement */
   1060 #endif
   1061 }
   1062 
   1063 
   1064 void
   1065 sacomstart(struct tty *tp)
   1066 {
   1067 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
   1068 	bus_space_tag_t iot = sc->sc_iot;
   1069 	bus_space_handle_t ioh = sc->sc_ioh;
   1070 	int s;
   1071 
   1072 	if (COM_ISALIVE(sc) == 0)
   1073 		return;
   1074 
   1075 	s = spltty();
   1076 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1077 		goto out;
   1078 
   1079 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1080 		if (ISSET(tp->t_state, TS_ASLEEP)) {
   1081 			CLR(tp->t_state, TS_ASLEEP);
   1082 			wakeup(&tp->t_outq);
   1083 		}
   1084 		selwakeup(&tp->t_wsel);
   1085 		if (tp->t_outq.c_cc == 0)
   1086 			goto out;
   1087 	}
   1088 
   1089 	/* Grab the first contiguous region of buffer space. */
   1090 	{
   1091 		u_char *tba;
   1092 		int tbc;
   1093 
   1094 		tba = tp->t_outq.c_cf;
   1095 		tbc = ndqb(&tp->t_outq, 0);
   1096 
   1097 		(void)splserial();
   1098 		COM_LOCK(sc);
   1099 
   1100 		sc->sc_tba = tba;
   1101 		sc->sc_tbc = tbc;
   1102 	}
   1103 
   1104 	SET(tp->t_state, TS_BUSY);
   1105 	sc->sc_tx_busy = 1;
   1106 
   1107 	/* Enable transmit completion interrupts if necessary. */
   1108 	if (!ISSET(sc->sc_cr3, CR3_TIE)) {
   1109 		SET(sc->sc_cr3, CR3_TIE);
   1110 		bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
   1111 	}
   1112 
   1113 	/* Output the first chunk of the contiguous buffer. */
   1114 	sacom_filltx(sc);
   1115 
   1116 	COM_UNLOCK(sc);
   1117 out:
   1118 	splx(s);
   1119 	return;
   1120 }
   1121 
   1122 void
   1123 sacom_filltx(struct sacom_softc *sc)
   1124 {
   1125 	bus_space_tag_t iot = sc->sc_iot;
   1126 	bus_space_handle_t ioh = sc->sc_ioh;
   1127 	int c, n;
   1128 
   1129 	n = 0;
   1130 	while(bus_space_read_4(iot, ioh, SACOM_SR1)
   1131 	      & 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_loadchanelregs() 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 		;
   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_HW_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 		;
   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