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