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