Home | History | Annotate | Line # | Download | only in sa11x0
sa11x0_com.c revision 1.32
      1 /*      $NetBSD: sa11x0_com.c,v 1.32 2006/05/14 21:55:10 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.32 2006/05/14 21:55:10 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 	return (1);
    237 }
    238 
    239 void
    240 sacom_attach(struct device *parent, struct device *self, void *aux)
    241 {
    242 	struct sacom_softc *sc = (struct sacom_softc*)self;
    243 	struct sa11x0_attach_args *sa = aux;
    244 
    245 #ifdef hpcarm
    246 	struct platid_data *p;
    247 	void (*mdinit)(struct device *, struct sacom_softc *);
    248 #endif
    249 
    250 	printf("\n");
    251 
    252 	sc->sc_iot = sa->sa_iot;
    253 	sc->sc_baseaddr = sa->sa_addr;
    254 
    255 	if(bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
    256 			&sc->sc_ioh)) {
    257 		printf("%s: unable to map registers\n", sc->sc_dev.dv_xname);
    258 		return;
    259 	}
    260 
    261 	printf("%s: ", sc->sc_dev.dv_xname);
    262 	switch(sc->sc_baseaddr) {
    263 	case 0x80050000:
    264 		printf("SA11x0 UART3\n");
    265 		break;
    266 	case 0x80010000:
    267 		printf("SA11x0 UART1\n");
    268 		break;
    269 	case 0x80030000:
    270 		printf("SA11x0 UART2 (IRDA)\n");
    271 		break;
    272 	default:
    273 		printf("unknown SA11x0 UART\n");
    274 		break;
    275 	}
    276 
    277 	sacom_attach_subr(sc);
    278 
    279 #ifdef hpcarm
    280 	/* Do hpcarm specific initialization, if any */
    281 	if ((p = platid_search_data(&platid, sacom_platid_table)) != NULL) {
    282 		mdinit = p->data;
    283 		(mdinit)(parent, sc);
    284 	}
    285 #endif
    286 
    287 	sa11x0_intr_establish(0, sa->sa_intr, 1, IPL_SERIAL, sacomintr, sc);
    288 }
    289 
    290 void
    291 sacom_attach_subr(struct sacom_softc *sc)
    292 {
    293 	bus_addr_t iobase = sc->sc_baseaddr;
    294 	bus_space_tag_t iot = sc->sc_iot;
    295 	struct tty *tp;
    296 
    297 	/* XXX Do we need to disable interrupts here? */
    298 
    299 	if (iot == sacomconstag && iobase == sacomconsaddr) {
    300 		sacomconsattached = 1;
    301 		sc->sc_speed = SACOMSPEED(sacomconsrate);
    302 
    303 		/* Make sure the console is always "hardwired". */
    304 		delay(10000);			/* wait for output to finish */
    305 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    306 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    307 	}
    308 
    309 	tp = ttymalloc();
    310 	tp->t_oproc = sacomstart;
    311 	tp->t_param = sacomparam;
    312 	tp->t_hwiflow = sacomhwiflow;
    313 
    314 	sc->sc_tty = tp;
    315 	sc->sc_rbuf = malloc(SACOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
    316 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    317 	sc->sc_rbavail = SACOM_RING_SIZE;
    318 	if (sc->sc_rbuf == NULL) {
    319 		printf("%s: unable to allocate ring buffer\n",
    320 		    sc->sc_dev.dv_xname);
    321 		return;
    322 	}
    323 	sc->sc_ebuf = sc->sc_rbuf + (SACOM_RING_SIZE << 1);
    324 	sc->sc_tbc = 0;
    325 
    326 	tty_attach(tp);
    327 
    328 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    329 		int maj;
    330 
    331 		/* locate the major number */
    332 		maj = cdevsw_lookup_major(&sacom_cdevsw);
    333 
    334 		cn_tab->cn_dev = makedev(maj, device_unit(&sc->sc_dev));
    335 
    336 		delay(10000); /* XXX */
    337 		printf("%s: console\n", sc->sc_dev.dv_xname);
    338 		delay(10000); /* XXX */
    339 	}
    340 
    341 
    342 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    343 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, sacomsoft, sc);
    344 #endif
    345 
    346 #if NRND > 0 && defined(RND_COM)
    347 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    348 			  RND_TYPE_TTY, 0);
    349 #endif
    350 
    351 	/* if there are no enable/disable functions, assume the device
    352 	   is always enabled */
    353 	if (!sc->enable)
    354 		sc->enabled = 1;
    355 
    356 	sacom_config(sc);
    357 
    358 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
    359 }
    360 
    361 /* This is necessary when dynamically changing SAIP configuration. */
    362 int
    363 sacom_detach(struct device *self, int flags)
    364 {
    365 	struct sacom_softc *sc = (struct sacom_softc *)self;
    366 	int maj, mn;
    367 
    368 	/* locate the major number */
    369 	maj = cdevsw_lookup_major(&sacom_cdevsw);
    370 
    371 	/* Nuke the vnodes for any open instances. */
    372 	mn = device_unit(self);
    373 	vdevgone(maj, mn, mn, VCHR);
    374 
    375 	mn |= COMDIALOUT_MASK;
    376 	vdevgone(maj, mn, mn, VCHR);
    377 
    378 	/* Free the receive buffer. */
    379 	free(sc->sc_rbuf, M_DEVBUF);
    380 
    381 	/* Detach and free the tty. */
    382 	tty_detach(sc->sc_tty);
    383 	ttyfree(sc->sc_tty);
    384 
    385 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    386 	/* Unhook the soft interrupt handler. */
    387 	softintr_disestablish(sc->sc_si);
    388 #endif
    389 
    390 #if NRND > 0 && defined(RND_COM)
    391 	/* Unhook the entropy source. */
    392 	rnd_detach_source(&sc->rnd_source);
    393 #endif
    394 
    395 	return (0);
    396 }
    397 
    398 void
    399 sacom_config(struct sacom_softc *sc)
    400 {
    401 	bus_space_tag_t iot = sc->sc_iot;
    402 	bus_space_handle_t ioh = sc->sc_ioh;
    403 
    404 	/* Disable engine before configuring the device. */
    405 	sc->sc_cr3 = 0;
    406 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
    407 
    408 #ifdef DDB
    409 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    410 		sacom_enable_debugport(sc);
    411 #endif
    412 }
    413 
    414 #ifdef DDB
    415 static void
    416 sacom_enable_debugport(struct sacom_softc *sc)
    417 {
    418 	bus_space_tag_t iot = sc->sc_iot;
    419 	bus_space_handle_t ioh = sc->sc_ioh;
    420 	int s;
    421 
    422 	s = splserial();
    423 	COM_LOCK(sc);
    424 	sc->sc_cr3 = CR3_RXE | CR3_TXE;
    425 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
    426 	COM_UNLOCK(sc);
    427 	splx(s);
    428 }
    429 #endif
    430 
    431 int
    432 sacom_activate(struct device *self, enum devact act)
    433 {
    434 	struct sacom_softc *sc = (struct sacom_softc *)self;
    435 	int s, rv = 0;
    436 
    437 	s = splserial();
    438 	COM_LOCK(sc);
    439 	switch (act) {
    440 	case DVACT_ACTIVATE:
    441 		rv = EOPNOTSUPP;
    442 		break;
    443 
    444 	case DVACT_DEACTIVATE:
    445 		if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB)) {
    446 			rv = EBUSY;
    447 			break;
    448 		}
    449 
    450 		if (sc->disable != NULL && sc->enabled != 0) {
    451 			(*sc->disable)(sc);
    452 			sc->enabled = 0;
    453 		}
    454 		break;
    455 	}
    456 
    457 	COM_UNLOCK(sc);
    458 	splx(s);
    459 	return (rv);
    460 }
    461 
    462 void
    463 sacom_shutdown(struct sacom_softc *sc)
    464 {
    465 	struct tty *tp = sc->sc_tty;
    466 	int s;
    467 
    468 	s = splserial();
    469 	COM_LOCK(sc);
    470 
    471 	/* Clear any break condition set with TIOCSBRK. */
    472 	sacom_break(sc, 0);
    473 
    474 	/*
    475 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    476 	 * notice even if we immediately open the port again.
    477 	 * Avoid tsleeping above splhigh().
    478 	 */
    479 	if (ISSET(tp->t_cflag, HUPCL)) {
    480 		sacom_modem(sc, 0);
    481 		COM_UNLOCK(sc);
    482 		splx(s);
    483 		/* XXX tsleep will only timeout */
    484 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    485 		s = splserial();
    486 		COM_LOCK(sc);
    487 	}
    488 
    489 	/* Turn off interrupts. */
    490 	sc->sc_cr3 = 0;
    491 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3, sc->sc_cr3);
    492 
    493 	if (sc->disable) {
    494 #ifdef DIAGNOSTIC
    495 		if (!sc->enabled)
    496 			panic("sacom_shutdown: not enabled?");
    497 #endif
    498 		(*sc->disable)(sc);
    499 		sc->enabled = 0;
    500 	}
    501 	COM_UNLOCK(sc);
    502 	splx(s);
    503 }
    504 
    505 int
    506 sacomopen(dev_t dev, int flag, int mode, struct lwp *l)
    507 {
    508 	struct sacom_softc *sc;
    509 	struct tty *tp;
    510 	int s, s2;
    511 	int error;
    512 
    513 	sc = device_lookup(&sacom_cd, COMUNIT(dev));
    514 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
    515 		sc->sc_rbuf == NULL)
    516 		return (ENXIO);
    517 
    518 	if (!device_is_active(&sc->sc_dev))
    519 		return (ENXIO);
    520 
    521 	tp = sc->sc_tty;
    522 
    523 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    524 	    ISSET(tp->t_state, TS_XCLUDE) &&
    525 	    kauth_authorize_generic(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER, &l->l_proc->p_acflag) != 0)
    526 		return (EBUSY);
    527 
    528 	s = spltty();
    529 
    530 	/*
    531 	 * Do the following iff this is a first open.
    532 	 */
    533 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    534 		struct termios t;
    535 
    536 		tp->t_dev = dev;
    537 
    538 		s2 = splserial();
    539 		COM_LOCK(sc);
    540 
    541 		if (sc->enable) {
    542 			if ((*sc->enable)(sc)) {
    543 				COM_UNLOCK(sc);
    544 				splx(s2);
    545 				splx(s);
    546 				printf("%s: device enable failed\n",
    547 				       sc->sc_dev.dv_xname);
    548 				return (EIO);
    549 			}
    550 			sc->enabled = 1;
    551 			sacom_config(sc);
    552 		}
    553 
    554 		/* Turn on interrupts. */
    555 		sc->sc_cr3 = CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE;
    556 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3,
    557 				  sc->sc_cr3);
    558 
    559 
    560 		COM_UNLOCK(sc);
    561 		splx(s2);
    562 
    563 		/*
    564 		 * Initialize the termios status to the defaults.  Add in the
    565 		 * sticky bits from TIOCSFLAGS.
    566 		 */
    567 		t.c_ispeed = 0;
    568 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    569 			t.c_ospeed = sacomconsrate;
    570 			t.c_cflag = sacomconscflag;
    571 		} else {
    572 			t.c_ospeed = TTYDEF_SPEED;
    573 			t.c_cflag = TTYDEF_CFLAG;
    574 		}
    575 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    576 			SET(t.c_cflag, CLOCAL);
    577 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    578 			SET(t.c_cflag, CRTSCTS);
    579 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    580 			SET(t.c_cflag, MDMBUF);
    581 		/* Make sure sacomparam() will do something. */
    582 		tp->t_ospeed = 0;
    583 		(void) sacomparam(tp, &t);
    584 		tp->t_iflag = TTYDEF_IFLAG;
    585 		tp->t_oflag = TTYDEF_OFLAG;
    586 		tp->t_lflag = TTYDEF_LFLAG;
    587 		ttychars(tp);
    588 		ttsetwater(tp);
    589 
    590 		s2 = splserial();
    591 		COM_LOCK(sc);
    592 
    593 		/*
    594 		 * Turn on DTR.  We must always do this, even if carrier is not
    595 		 * present, because otherwise we'd have to use TIOCSDTR
    596 		 * immediately after setting CLOCAL, which applications do not
    597 		 * expect.  We always assert DTR while the device is open
    598 		 * unless explicitly requested to deassert it.
    599 		 */
    600 		sacom_modem(sc, 1);
    601 
    602 		/* Clear the input ring, and unblock. */
    603 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    604 		sc->sc_rbavail = SACOM_RING_SIZE;
    605 		sacom_iflush(sc);
    606 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    607 		sacom_hwiflow(sc);
    608 
    609 #ifdef COM_DEBUG
    610 		if (sacom_debug)
    611 			comstatus(sc, "sacomopen  ");
    612 #endif
    613 
    614 		COM_UNLOCK(sc);
    615 		splx(s2);
    616 	}
    617 
    618 	splx(s);
    619 
    620 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    621 	if (error)
    622 		goto bad;
    623 
    624 	error = (*tp->t_linesw->l_open)(dev, tp);
    625 	if (error)
    626 		goto bad;
    627 
    628 	return (0);
    629 
    630 bad:
    631 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    632 		/*
    633 		 * We failed to open the device, and nobody else had it opened.
    634 		 * Clean up the state as appropriate.
    635 		 */
    636 		sacom_shutdown(sc);
    637 	}
    638 
    639 	return (error);
    640 }
    641 
    642 int
    643 sacomclose(dev_t dev, int flag, int mode, struct lwp *l)
    644 {
    645 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    646 	struct tty *tp = sc->sc_tty;
    647 
    648 	/* XXX This is for cons.c. */
    649 	if (!ISSET(tp->t_state, TS_ISOPEN))
    650 		return (0);
    651 
    652 	(*tp->t_linesw->l_close)(tp, flag);
    653 	ttyclose(tp);
    654 
    655 	if (COM_ISALIVE(sc) == 0)
    656 		return (0);
    657 
    658 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    659 		/*
    660 		 * Although we got a last close, the device may still be in
    661 		 * use; e.g. if this was the dialout node, and there are still
    662 		 * processes waiting for carrier on the non-dialout node.
    663 		 */
    664 		sacom_shutdown(sc);
    665 	}
    666 
    667 	return (0);
    668 }
    669 
    670 int
    671 sacomread(dev_t dev, struct uio *uio, int flag)
    672 {
    673 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    674 	struct tty *tp = sc->sc_tty;
    675 
    676 	if (COM_ISALIVE(sc) == 0)
    677 		return (EIO);
    678 
    679 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    680 }
    681 
    682 int
    683 sacomwrite(dev_t dev, struct uio *uio, int flag)
    684 {
    685 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    686 	struct tty *tp = sc->sc_tty;
    687 
    688 	if (COM_ISALIVE(sc) == 0)
    689 		return (EIO);
    690 
    691 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    692 }
    693 
    694 int
    695 sacompoll(dev_t dev, int events, struct lwp *l)
    696 {
    697 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    698 	struct tty *tp = sc->sc_tty;
    699 
    700 	if (COM_ISALIVE(sc) == 0)
    701 		return (EIO);
    702 
    703 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    704 }
    705 
    706 struct tty *
    707 sacomtty(dev_t dev)
    708 {
    709 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    710 	struct tty *tp = sc->sc_tty;
    711 
    712 	return (tp);
    713 }
    714 
    715 int
    716 sacomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    717 {
    718 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    719 	struct tty *tp = sc->sc_tty;
    720 	int error;
    721 	int s;
    722 
    723 	if (COM_ISALIVE(sc) == 0)
    724 		return (EIO);
    725 
    726 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    727 	if (error != EPASSTHROUGH)
    728 		return (error);
    729 
    730 	error = ttioctl(tp, cmd, data, flag, l);
    731 	if (error != EPASSTHROUGH)
    732 		return (error);
    733 
    734 	error = 0;
    735 
    736 	s = splserial();
    737 	COM_LOCK(sc);
    738 
    739 	switch (cmd) {
    740 	case TIOCSBRK:
    741 		sacom_break(sc, 1);
    742 		break;
    743 
    744 	case TIOCCBRK:
    745 		sacom_break(sc, 0);
    746 		break;
    747 
    748 	case TIOCSDTR:
    749 		sacom_modem(sc, 1);
    750 		break;
    751 
    752 	case TIOCCDTR:
    753 		sacom_modem(sc, 0);
    754 		break;
    755 
    756 	case TIOCGFLAGS:
    757 		*(int *)data = sc->sc_swflags;
    758 		break;
    759 
    760 	case TIOCSFLAGS:
    761 		error = kauth_authorize_generic(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER, &l->l_proc->p_acflag);
    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)
   1132 	      & SR1_TNF) {
   1133 		if (n == SACOM_TXFIFOLEN || n == sc->sc_tbc)
   1134 			break;
   1135 		c = *(sc->sc_tba + n);
   1136 		c &= 0xff;
   1137 		bus_space_write_4(iot, ioh, SACOM_DR, c);
   1138 		n++;
   1139 	}
   1140 	sc->sc_tbc -= n;
   1141 	sc->sc_tba += n;
   1142 }
   1143 
   1144 /*
   1145  * Stop output on a line.
   1146  */
   1147 void
   1148 sacomstop(struct tty *tp, int flag)
   1149 {
   1150 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
   1151 	int s;
   1152 
   1153 	s = splserial();
   1154 	COM_LOCK(sc);
   1155 	if (ISSET(tp->t_state, TS_BUSY)) {
   1156 		/* Stop transmitting at the next chunk. */
   1157 		sc->sc_tbc = 0;
   1158 		sc->sc_heldtbc = 0;
   1159 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1160 			SET(tp->t_state, TS_FLUSH);
   1161 	}
   1162 	COM_UNLOCK(sc);
   1163 	splx(s);
   1164 }
   1165 
   1166 static inline void
   1167 sacom_rxsoft(struct sacom_softc *sc, struct tty *tp)
   1168 {
   1169 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
   1170 	u_char *get, *end;
   1171 	u_int cc, scc;
   1172 	u_char sr1;
   1173 	int code;
   1174 	int s;
   1175 
   1176 	end = sc->sc_ebuf;
   1177 	get = sc->sc_rbget;
   1178 	scc = cc = SACOM_RING_SIZE - sc->sc_rbavail;
   1179 
   1180 	while (cc) {
   1181 		code = get[0];
   1182 		sr1 = get[1];
   1183 		if (ISSET(sr1, SR1_FRE))
   1184 			SET(code, TTY_FE);
   1185 		if (ISSET(sr1, SR1_PRE))
   1186 			SET(code, TTY_PE);
   1187 		if ((*rint)(code, tp) == -1) {
   1188 			/*
   1189 			 * The line discipline's buffer is out of space.
   1190 			 */
   1191 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1192 				/*
   1193 				 * We're either not using flow control, or the
   1194 				 * line discipline didn't tell us to block for
   1195 				 * some reason.  Either way, we have no way to
   1196 				 * know when there's more space available, so
   1197 				 * just drop the rest of the data.
   1198 				 */
   1199 				get += cc << 1;
   1200 				if (get >= end)
   1201 					get -= SACOM_RING_SIZE << 1;
   1202 				cc = 0;
   1203 			} else {
   1204 				/*
   1205 				 * Don't schedule any more receive processing
   1206 				 * until the line discipline tells us there's
   1207 				 * space available (through comhwiflow()).
   1208 				 * Leave the rest of the data in the input
   1209 				 * buffer.
   1210 				 */
   1211 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1212 			}
   1213 			break;
   1214 		}
   1215 		get += 2;
   1216 		if (get >= end)
   1217 			get = sc->sc_rbuf;
   1218 		cc--;
   1219 	}
   1220 
   1221 	if (cc != scc) {
   1222 		sc->sc_rbget = get;
   1223 		s = splserial();
   1224 		COM_LOCK(sc);
   1225 
   1226 		cc = sc->sc_rbavail += scc - cc;
   1227 		/* Buffers should be ok again, release possible block. */
   1228 		if (cc >= 1) {
   1229 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1230 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1231 				SET(sc->sc_cr3, CR3_RIE);
   1232 				bus_space_write_4(sc->sc_iot, sc->sc_ioh,
   1233 						  SACOM_CR3, sc->sc_cr3);
   1234 			}
   1235 		}
   1236 		COM_UNLOCK(sc);
   1237 		splx(s);
   1238 	}
   1239 }
   1240 
   1241 static inline void
   1242 sacom_txsoft(struct sacom_softc *sc, struct tty *tp)
   1243 {
   1244 
   1245 	CLR(tp->t_state, TS_BUSY);
   1246 	if (ISSET(tp->t_state, TS_FLUSH))
   1247 		CLR(tp->t_state, TS_FLUSH);
   1248 	else
   1249 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1250 	(*tp->t_linesw->l_start)(tp);
   1251 }
   1252 
   1253 static inline void
   1254 sacom_stsoft(struct sacom_softc *sc, struct tty *tp)
   1255 {
   1256 }
   1257 
   1258 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1259 void
   1260 sacomsoft(void *arg)
   1261 {
   1262 	struct sacom_softc *sc = arg;
   1263 	struct tty *tp;
   1264 
   1265 	if (COM_ISALIVE(sc) == 0)
   1266 		return;
   1267 
   1268 	{
   1269 #else
   1270 void
   1271 sacomsoft(void)
   1272 {
   1273 	struct sacom_softc	*sc;
   1274 	struct tty	*tp;
   1275 	int	unit;
   1276 
   1277 	for (unit = 0; unit < sacom_cd.cd_ndevs; unit++) {
   1278 		sc = device_lookup(&sacom_cd, unit);
   1279 		if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
   1280 			continue;
   1281 
   1282 		if (COM_ISALIVE(sc) == 0)
   1283 			continue;
   1284 
   1285 		tp = sc->sc_tty;
   1286 		if (tp == NULL)
   1287 			continue;
   1288 		if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
   1289 			continue;
   1290 #endif
   1291 		tp = sc->sc_tty;
   1292 
   1293 		if (sc->sc_rx_ready) {
   1294 			sc->sc_rx_ready = 0;
   1295 			sacom_rxsoft(sc, tp);
   1296 		}
   1297 
   1298 		if (sc->sc_st_check) {
   1299 			sc->sc_st_check = 0;
   1300 			sacom_stsoft(sc, tp);
   1301 		}
   1302 
   1303 		if (sc->sc_tx_done) {
   1304 			sc->sc_tx_done = 0;
   1305 			sacom_txsoft(sc, tp);
   1306 		}
   1307 	}
   1308 
   1309 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
   1310 #endif
   1311 }
   1312 
   1313 #ifdef __ALIGN_BRACKET_LEVEL_FOR_CTAGS
   1314 	/* there has got to be a better way to do comsoft() */
   1315 }}
   1316 #endif
   1317 
   1318 int
   1319 sacomintr(void *arg)
   1320 {
   1321 	struct sacom_softc *sc = arg;
   1322 	bus_space_tag_t iot = sc->sc_iot;
   1323 	bus_space_handle_t ioh = sc->sc_ioh;
   1324 	u_char *put, *end;
   1325 	u_int cc;
   1326 	u_int sr0, sr1;
   1327 
   1328 	if (COM_ISALIVE(sc) == 0)
   1329 		return (0);
   1330 
   1331 	COM_LOCK(sc);
   1332 	sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
   1333 	if (! sr0) {
   1334 		COM_UNLOCK(sc);
   1335 		return (0);
   1336 	}
   1337 	if (ISSET(sr0, SR0_EIF))
   1338 		/* XXX silently discard error bits */
   1339 		bus_space_read_4(iot, ioh, SACOM_DR);
   1340 	if (ISSET(sr0, SR0_RBB))
   1341 		bus_space_write_4(iot, ioh, SACOM_SR0, SR0_RBB);
   1342 	if (ISSET(sr0, SR0_REB)) {
   1343 		bus_space_write_4(iot, ioh, SACOM_SR0, SR0_REB);
   1344 #if defined(DDB) || defined(KGDB)
   1345 #ifndef DDB_BREAK_CHAR
   1346 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1347 			console_debugger();
   1348 		}
   1349 #endif
   1350 #endif /* DDB || KGDB */
   1351 	}
   1352 
   1353 
   1354 	end = sc->sc_ebuf;
   1355 	put = sc->sc_rbput;
   1356 	cc = sc->sc_rbavail;
   1357 
   1358 	sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
   1359 	if (ISSET(sr0, SR0_RFS | SR0_RID)) {
   1360 		if (! ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1361 			while (cc > 0) {
   1362 				if (!ISSET(sr1, SR1_RNE)) {
   1363 					bus_space_write_4(iot, ioh, SACOM_SR0,
   1364 							  SR0_RID);
   1365 					break;
   1366 				}
   1367 				put[0] = bus_space_read_4(iot, ioh, SACOM_DR);
   1368 				put[1] = sr1;
   1369 #if defined(DDB) && defined(DDB_BREAK_CHAR)
   1370 				if (put[0] == DDB_BREAK_CHAR &&
   1371 				    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1372 					console_debugger();
   1373 
   1374 					sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
   1375 					continue;
   1376 				}
   1377 #endif
   1378 				put += 2;
   1379 				if (put >= end)
   1380 					put = sc->sc_rbuf;
   1381 				cc--;
   1382 
   1383 				sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
   1384 			}
   1385 
   1386 			/*
   1387 			 * Current string of incoming characters ended because
   1388 			 * no more data was available or we ran out of space.
   1389 			 * Schedule a receive event if any data was received.
   1390 			 * If we're out of space, turn off receive interrupts.
   1391 			 */
   1392 			sc->sc_rbput = put;
   1393 			sc->sc_rbavail = cc;
   1394 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1395 				sc->sc_rx_ready = 1;
   1396 
   1397 			/* XXX do RX hardware flow control */
   1398 
   1399 			/*
   1400 			 * If we're out of space, disable receive interrupts
   1401 			 * until the queue has drained a bit.
   1402 			 */
   1403 			if (!cc) {
   1404 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1405 				CLR(sc->sc_cr3, CR3_RIE);
   1406 				bus_space_write_4(iot, ioh, SACOM_CR3,
   1407 						  sc->sc_cr3);
   1408 			}
   1409 		} else {
   1410 #ifdef DIAGNOSTIC
   1411 			panic("sacomintr: we shouldn't reach here");
   1412 #endif
   1413 			CLR(sc->sc_cr3, CR3_RIE);
   1414 			bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
   1415 		}
   1416 	}
   1417 
   1418 	/*
   1419 	 * Done handling any receive interrupts. See if data can be
   1420 	 * transmitted as well. Schedule tx done event if no data left
   1421 	 * and tty was marked busy.
   1422 	 */
   1423 	sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
   1424 	if (ISSET(sr0, SR0_TFS)) {
   1425 		/*
   1426 		 * If we've delayed a parameter change, do it now, and restart
   1427 		 * output.
   1428 		 * XXX sacom_loadchanelregs() waits TX completion,
   1429 		 * XXX resulting in ~0.1s hang (300bps, 4 bytes) in worst case
   1430 		 */
   1431 		if (sc->sc_heldchange) {
   1432 			sacom_loadchannelregs(sc);
   1433 			sc->sc_heldchange = 0;
   1434 			sc->sc_tbc = sc->sc_heldtbc;
   1435 			sc->sc_heldtbc = 0;
   1436 		}
   1437 
   1438 		/* Output the next chunk of the contiguous buffer, if any. */
   1439 		if (sc->sc_tbc > 0) {
   1440 			sacom_filltx(sc);
   1441 		} else {
   1442 			/* Disable transmit completion interrupts if necessary. */
   1443 			if (ISSET(sc->sc_cr3, CR3_TIE)) {
   1444 				CLR(sc->sc_cr3, CR3_TIE);
   1445 				bus_space_write_4(iot, ioh, SACOM_CR3,
   1446 						  sc->sc_cr3);
   1447 			}
   1448 			if (sc->sc_tx_busy) {
   1449 				sc->sc_tx_busy = 0;
   1450 				sc->sc_tx_done = 1;
   1451 			}
   1452 		}
   1453 	}
   1454 	COM_UNLOCK(sc);
   1455 
   1456 	/* Wake up the poller. */
   1457 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1458 	softintr_schedule(sc->sc_si);
   1459 #else
   1460 	setsoftserial();
   1461 #endif
   1462 
   1463 #if NRND > 0 && defined(RND_COM)
   1464 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   1465 #endif
   1466 	return (1);
   1467 }
   1468 
   1469 static void
   1470 sacom_j720_init(struct sa11x0_softc *parent, struct sacom_softc *sc) {
   1471 
   1472 	/* XXX  this should be done at sc->enable function */
   1473 	bus_space_write_4(parent->sc_iot, parent->sc_gpioh,
   1474 	    SAGPIO_PCR, 0xa0000);
   1475 	bus_space_write_4(parent->sc_iot, parent->sc_gpioh,
   1476 	    SAGPIO_PSR, 0x100);
   1477 }
   1478 
   1479 /* Initialization for serial console */
   1480 int
   1481 sacominit(bus_space_tag_t iot, bus_addr_t iobase, int baud, tcflag_t cflag,
   1482     bus_space_handle_t *iohp)
   1483 {
   1484 	int brd, cr0;
   1485 
   1486 	if (bus_space_map(iot, iobase, SACOM_NPORTS, 0, iohp))
   1487 		printf("register map failed\n");
   1488 
   1489 	/* wait for the Tx queue to drain and disable the UART */
   1490 	while(bus_space_read_4(iot, *iohp, SACOM_SR1) & SR1_TBY)
   1491 		;
   1492 	bus_space_write_4(iot, *iohp, SACOM_CR3, 0);
   1493 
   1494 	cr0  = cflag2cr0(cflag);
   1495 	bus_space_write_4(iot, *iohp, SACOM_CR0, cr0);
   1496 
   1497 	brd = SACOMSPEED(baud);
   1498 	sacomconsrate = baud;
   1499 	sacomconsaddr = iobase;
   1500 	sacomconscflag = cflag;
   1501 	/* XXX assumes little endian */
   1502 	bus_space_write_4(iot, *iohp, SACOM_CR1, brd >> 8);
   1503 	bus_space_write_4(iot, *iohp, SACOM_CR2, brd & 0xff);
   1504 
   1505 	/* enable the UART */
   1506 	bus_space_write_4(iot, *iohp, SACOM_CR3, CR3_RXE | CR3_TXE);
   1507 
   1508 	return (0);
   1509 }
   1510 
   1511 void
   1512 sacomcnprobe(struct consdev *cp)
   1513 {
   1514 	cp->cn_pri = CN_REMOTE;
   1515 }
   1516 
   1517 void
   1518 sacomcninit(struct consdev *cp)
   1519 {
   1520 	if (cp == NULL) {
   1521 		/* XXX cp == NULL means that MMU is disabled. */
   1522 		sacomconsioh = SACOM3_BASE;
   1523 		sacomconstag = &sa11x0_bs_tag;
   1524 		cn_tab = &sacomcons;
   1525 		return;
   1526 	}
   1527 
   1528 	if (sacominit(&sa11x0_bs_tag, CONADDR, CONSPEED,
   1529 			  CONMODE, &sacomconsioh))
   1530 		panic("can't init serial console @%x", CONADDR);
   1531 	cn_tab = &sacomcons;
   1532 	sacomconstag = &sa11x0_bs_tag;
   1533 }
   1534 
   1535 int
   1536 sacomcngetc(dev_t dev)
   1537 {
   1538 	int c, s;
   1539 
   1540 	s = spltty();	/* XXX do we need this? */
   1541 
   1542 	while(! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1543 		 & SR1_RNE)) {
   1544 #if defined(DDB) || defined(KGDB)
   1545 #ifndef DDB_BREAK_CHAR
   1546 		u_int sr0;
   1547 		extern int db_active;
   1548 
   1549 		sr0 = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR0);
   1550 		if (ISSET(sr0, SR0_RBB))
   1551 			bus_space_write_4(sacomconstag, sacomconsioh,
   1552 					  SACOM_SR0, SR0_RBB);
   1553 		if (ISSET(sr0, SR0_REB)) {
   1554 			bus_space_write_4(sacomconstag, sacomconsioh,
   1555 					  SACOM_SR0, SR0_REB);
   1556 			if (db_active == 0)
   1557 				console_debugger();
   1558 		}
   1559 #endif
   1560 #endif /* DDB || KGDB */
   1561 	}
   1562 
   1563 	c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
   1564 	c &= 0xff;
   1565 	splx(s);
   1566 
   1567 	return (c);
   1568 }
   1569 
   1570 void
   1571 sacomcnputc(dev_t dev, int c)
   1572 {
   1573 	int s;
   1574 
   1575 	s = spltty();	/* XXX do we need this? */
   1576 
   1577 	while(! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1578 		 & SR1_TNF))
   1579 		;
   1580 
   1581 	bus_space_write_4(sacomconstag, sacomconsioh, SACOM_DR, c);
   1582 	splx(s);
   1583 }
   1584 
   1585 void
   1586 sacomcnpollc(dev_t dev, int on)
   1587 {
   1588 
   1589 }
   1590 
   1591 #if 0
   1592 #ifdef DEBUG
   1593 int
   1594 sacomcncharpoll(void)
   1595 {
   1596 	int c;
   1597 
   1598 	if (! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1599 		 & SR1_RNE))
   1600 		return -1;
   1601 
   1602 	c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
   1603 	c &= 0xff;
   1604 
   1605 	return (c);
   1606 }
   1607 #endif
   1608 #endif
   1609 
   1610 /* helper function to identify the com ports used by
   1611  console or KGDB (and not yet autoconf attached) */
   1612 int
   1613 sacom_is_console(bus_space_tag_t iot, bus_addr_t iobase,
   1614     bus_space_handle_t *ioh)
   1615 {
   1616 	bus_space_handle_t help;
   1617 
   1618 	if (! sacomconsattached &&
   1619 	    iot == sacomconstag && iobase == sacomconsaddr)
   1620 		help = sacomconsioh;
   1621 	else
   1622 		return (0);
   1623 
   1624 	if (ioh)
   1625 		*ioh = help;
   1626 	return (1);
   1627 }
   1628