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