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