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