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