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