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