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