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