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