Home | History | Annotate | Line # | Download | only in sa11x0
sa11x0_com.c revision 1.51
      1 /*      $NetBSD: sa11x0_com.c,v 1.51 2014/03/16 05:20:23 dholland 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.51 2014/03/16 05:20:23 dholland 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_flag = D_TTY
    131 };
    132 
    133 static	int	sacom_match(device_t, cfdata_t, void *);
    134 static	void	sacom_attach(device_t, device_t, void *);
    135 static	void	sacom_filltx(struct sacom_softc *);
    136 static	void	sacom_attach_subr(struct sacom_softc *);
    137 #if defined(DDB) || defined(KGDB)
    138 static	void	sacom_enable_debugport(struct sacom_softc *);
    139 #endif
    140 int		sacom_detach(device_t, int);
    141 void		sacom_config(struct sacom_softc *);
    142 int		sacom_activate(device_t, enum devact);
    143 void		sacom_shutdown(struct sacom_softc *);
    144 static	u_int	cflag2cr0(tcflag_t);
    145 int		sacomparam(struct tty *, struct termios *);
    146 void		sacomstart(struct tty *);
    147 int		sacomhwiflow(struct tty *, int);
    148 
    149 void		sacom_loadchannelregs(struct sacom_softc *);
    150 void		sacom_hwiflow(struct sacom_softc *);
    151 void		sacom_break(struct sacom_softc *, int);
    152 void		sacom_modem(struct sacom_softc *, int);
    153 void		tiocm_to_sacom(struct sacom_softc *, u_long, int);
    154 int		sacom_to_tiocm(struct sacom_softc *);
    155 void		sacom_iflush(struct sacom_softc *);
    156 
    157 int		sacominit(bus_space_tag_t, bus_addr_t, int, tcflag_t,
    158 			  bus_space_handle_t *);
    159 int		sacom_is_console(bus_space_tag_t, bus_addr_t,
    160 				 bus_space_handle_t *);
    161 
    162 void 		sacomsoft(void *);
    163 
    164 static inline void sacom_rxsoft(struct sacom_softc *, struct tty *);
    165 static inline void sacom_txsoft(struct sacom_softc *, struct tty *);
    166 static inline void sacom_stsoft(struct sacom_softc *, struct tty *);
    167 static inline void sacom_schedrx(struct sacom_softc *);
    168 
    169 #ifdef hpcarm
    170 /* HPCARM specific functions */
    171 static void	sacom_j720_init(device_t, device_t);
    172 #endif
    173 
    174 #define COMUNIT_MASK	0x7ffff
    175 #define COMDIALOUT_MASK	0x80000
    176 
    177 #define COMUNIT(x)	(minor(x) & COMUNIT_MASK)
    178 #define COMDIALOUT(x)	(minor(x) & COMDIALOUT_MASK)
    179 
    180 #define COM_ISALIVE(sc)	((sc)->enabled != 0 && \
    181 			 device_is_active((sc)->sc_dev))
    182 
    183 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
    184 #define COM_LOCK(sc)
    185 #define COM_UNLOCK(sc)
    186 
    187 int		sacomintr(void *);
    188 int		sacomcngetc(dev_t);
    189 void		sacomcnputc(dev_t, int);
    190 void		sacomcnpollc(dev_t, int);
    191 
    192 void		sacomcnprobe(struct consdev *);
    193 void		sacomcninit(struct consdev *);
    194 
    195 extern struct bus_space sa11x0_bs_tag;
    196 
    197 static bus_space_tag_t sacomconstag;
    198 static bus_space_handle_t sacomconsioh;
    199 static bus_addr_t sacomconsaddr = SACOM3_BASE; /* XXX */
    200 
    201 static int sacomconsattached;
    202 static int sacomconsrate;
    203 static tcflag_t sacomconscflag;
    204 
    205 CFATTACH_DECL_NEW(sacom, sizeof(struct sacom_softc),
    206     sacom_match, sacom_attach, NULL, NULL);
    207 extern struct cfdriver sacom_cd;
    208 
    209 #ifdef hpcarm
    210 struct platid_data sacom_platid_table[] = {
    211 	{ &platid_mask_MACH_HP_JORNADA_7XX, sacom_j720_init },
    212 	{ NULL, NULL }
    213 };
    214 #endif
    215 
    216 struct consdev sacomcons = {
    217 	NULL, NULL, sacomcngetc, sacomcnputc, sacomcnpollc, NULL,
    218 	NULL, NULL, NODEV, CN_NORMAL
    219 };
    220 
    221 #ifndef CONMODE
    222 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    223 #endif
    224 #ifndef CONSPEED
    225 #define CONSPEED 9600
    226 #endif
    227 #ifndef CONADDR
    228 #define CONADDR SACOM3_BASE
    229 #endif
    230 
    231 static int
    232 sacom_match(device_t parent, cfdata_t match, void *aux)
    233 {
    234 
    235 	return 1;
    236 }
    237 
    238 void
    239 sacom_attach(device_t parent, device_t self, void *aux)
    240 {
    241 	struct sacom_softc *sc = device_private(self);
    242 	struct sa11x0_attach_args *sa = aux;
    243 
    244 #ifdef hpcarm
    245 	struct platid_data *p;
    246 	void (*mdinit)(device_t, device_t);
    247 #endif
    248 
    249 	aprint_normal("\n");
    250 
    251 	sc->sc_dev = self;
    252 	sc->sc_iot = sa->sa_iot;
    253 	sc->sc_baseaddr = sa->sa_addr;
    254 
    255 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
    256 	    &sc->sc_ioh)) {
    257 		aprint_normal_dev(self, "unable to map registers\n");
    258 		return;
    259 	}
    260 
    261 	switch (sc->sc_baseaddr) {
    262 	case SACOM1_BASE:
    263 		aprint_normal_dev(self, "SA-11x0 UART1\n");
    264 		break;
    265 	case SACOM2_BASE:
    266 		aprint_normal_dev(self, "SA-11x0 UART2 (IRDA)\n");
    267 		break;
    268 	case SACOM3_BASE:
    269 		aprint_normal_dev(self, "SA-11x0 UART3\n");
    270 		break;
    271 	default:
    272 		aprint_normal_dev(self, "unknown SA-11x0 UART\n");
    273 		break;
    274 	}
    275 
    276 	sacom_attach_subr(sc);
    277 
    278 #ifdef hpcarm
    279 	/* Do hpcarm specific initialization, if any */
    280 	if ((p = platid_search_data(&platid, sacom_platid_table)) != NULL) {
    281 		mdinit = p->data;
    282 		(*mdinit)(parent, self);
    283 	}
    284 #endif
    285 
    286 	sa11x0_intr_establish(0, sa->sa_intr, 1, IPL_SERIAL, sacomintr, sc);
    287 }
    288 
    289 void
    290 sacom_attach_subr(struct sacom_softc *sc)
    291 {
    292 	bus_addr_t iobase = sc->sc_baseaddr;
    293 	bus_space_tag_t iot = sc->sc_iot;
    294 	struct tty *tp;
    295 
    296 	/* XXX Do we need to disable interrupts here? */
    297 
    298 	if (iot == sacomconstag && iobase == sacomconsaddr) {
    299 		sacomconsattached = 1;
    300 		sc->sc_speed = SACOMSPEED(sacomconsrate);
    301 
    302 		/* Make sure the console is always "hardwired". */
    303 		delay(10000);			/* wait for output to finish */
    304 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    305 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    306 	}
    307 
    308 	tp = tty_alloc();
    309 	tp->t_oproc = sacomstart;
    310 	tp->t_param = sacomparam;
    311 	tp->t_hwiflow = sacomhwiflow;
    312 
    313 	sc->sc_tty = tp;
    314 	sc->sc_rbuf = malloc(SACOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
    315 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    316 	sc->sc_rbavail = SACOM_RING_SIZE;
    317 	if (sc->sc_rbuf == NULL) {
    318 		aprint_normal_dev(sc->sc_dev, "unable to allocate ring buffer\n");
    319 		return;
    320 	}
    321 	sc->sc_ebuf = sc->sc_rbuf + (SACOM_RING_SIZE << 1);
    322 	sc->sc_tbc = 0;
    323 
    324 	tty_attach(tp);
    325 
    326 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    327 		int maj;
    328 
    329 		/* locate the major number */
    330 		maj = cdevsw_lookup_major(&sacom_cdevsw);
    331 
    332 		cn_tab->cn_dev = makedev(maj, device_unit(sc->sc_dev));
    333 
    334 		delay(10000); /* XXX */
    335 		aprint_normal_dev(sc->sc_dev, "console\n");
    336 		delay(10000); /* XXX */
    337 	}
    338 
    339 
    340 	sc->sc_si = softint_establish(SOFTINT_SERIAL, sacomsoft, sc);
    341 
    342 #ifdef RND_COM
    343 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    344 			  RND_TYPE_TTY, 0);
    345 #endif
    346 
    347 	/* if there are no enable/disable functions, assume the device
    348 	   is always enabled */
    349 	if (!sc->enable)
    350 		sc->enabled = 1;
    351 
    352 	sacom_config(sc);
    353 
    354 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
    355 }
    356 
    357 /* This is necessary when dynamically changing SAIP configuration. */
    358 int
    359 sacom_detach(device_t dev, int flags)
    360 {
    361 	struct sacom_softc *sc = device_private(dev);
    362 	int maj, mn;
    363 
    364 	if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB))
    365 		return EBUSY;
    366 
    367 	if (sc->disable != NULL && sc->enabled != 0) {
    368 		(*sc->disable)(sc);
    369 		sc->enabled = 0;
    370 	}
    371 
    372 	/* locate the major number */
    373 	maj = cdevsw_lookup_major(&sacom_cdevsw);
    374 
    375 	/* Nuke the vnodes for any open instances. */
    376 	mn = device_unit(dev);
    377 	vdevgone(maj, mn, mn, VCHR);
    378 
    379 	mn |= COMDIALOUT_MASK;
    380 	vdevgone(maj, mn, mn, VCHR);
    381 
    382 	/* Free the receive buffer. */
    383 	free(sc->sc_rbuf, M_DEVBUF);
    384 
    385 	/* Detach and free the tty. */
    386 	tty_detach(sc->sc_tty);
    387 	tty_free(sc->sc_tty);
    388 
    389 	/* Unhook the soft interrupt handler. */
    390 	softint_disestablish(sc->sc_si);
    391 
    392 #ifdef RND_COM
    393 	/* Unhook the entropy source. */
    394 	rnd_detach_source(&sc->rnd_source);
    395 #endif
    396 
    397 	return 0;
    398 }
    399 
    400 void
    401 sacom_config(struct sacom_softc *sc)
    402 {
    403 	bus_space_tag_t iot = sc->sc_iot;
    404 	bus_space_handle_t ioh = sc->sc_ioh;
    405 
    406 	/* Disable engine before configuring the device. */
    407 	sc->sc_cr3 = 0;
    408 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
    409 
    410 #ifdef DDB
    411 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    412 		sacom_enable_debugport(sc);
    413 #endif
    414 }
    415 
    416 #ifdef DDB
    417 static void
    418 sacom_enable_debugport(struct sacom_softc *sc)
    419 {
    420 	bus_space_tag_t iot = sc->sc_iot;
    421 	bus_space_handle_t ioh = sc->sc_ioh;
    422 	int s;
    423 
    424 	s = splserial();
    425 	COM_LOCK(sc);
    426 	sc->sc_cr3 = CR3_RXE | CR3_TXE;
    427 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
    428 	COM_UNLOCK(sc);
    429 	splx(s);
    430 }
    431 #endif
    432 
    433 int
    434 sacom_activate(device_t dev, enum devact act)
    435 {
    436 	struct sacom_softc *sc = device_private(dev);
    437 
    438 	switch (act) {
    439 	case DVACT_DEACTIVATE:
    440 		sc->enabled = 0;
    441 		return 0;
    442 	default:
    443 		return EOPNOTSUPP;
    444 	}
    445 }
    446 
    447 void
    448 sacom_shutdown(struct sacom_softc *sc)
    449 {
    450 	struct tty *tp = sc->sc_tty;
    451 	int s;
    452 
    453 	s = splserial();
    454 	COM_LOCK(sc);
    455 
    456 	/* Clear any break condition set with TIOCSBRK. */
    457 	sacom_break(sc, 0);
    458 
    459 	/*
    460 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    461 	 * notice even if we immediately open the port again.
    462 	 * Avoid tsleeping above splhigh().
    463 	 */
    464 	if (ISSET(tp->t_cflag, HUPCL)) {
    465 		sacom_modem(sc, 0);
    466 		COM_UNLOCK(sc);
    467 		splx(s);
    468 		/* XXX tsleep will only timeout */
    469 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    470 		s = splserial();
    471 		COM_LOCK(sc);
    472 	}
    473 
    474 	/* Turn off interrupts. */
    475 	sc->sc_cr3 = 0;
    476 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3, sc->sc_cr3);
    477 
    478 	if (sc->disable) {
    479 #ifdef DIAGNOSTIC
    480 		if (!sc->enabled)
    481 			panic("sacom_shutdown: not enabled?");
    482 #endif
    483 		(*sc->disable)(sc);
    484 		sc->enabled = 0;
    485 	}
    486 	COM_UNLOCK(sc);
    487 	splx(s);
    488 }
    489 
    490 int
    491 sacomopen(dev_t dev, int flag, int mode, struct lwp *l)
    492 {
    493 	struct sacom_softc *sc;
    494 	struct tty *tp;
    495 	int s, s2;
    496 	int error;
    497 
    498 	sc = device_lookup_private(&sacom_cd, COMUNIT(dev));
    499 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
    500 		sc->sc_rbuf == NULL)
    501 		return ENXIO;
    502 
    503 	if (!device_is_active(sc->sc_dev))
    504 		return ENXIO;
    505 
    506 	tp = sc->sc_tty;
    507 
    508 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    509 		return (EBUSY);
    510 
    511 	s = spltty();
    512 
    513 	/*
    514 	 * Do the following iff this is a first open.
    515 	 */
    516 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    517 		struct termios t;
    518 
    519 		tp->t_dev = dev;
    520 
    521 		s2 = splserial();
    522 		COM_LOCK(sc);
    523 
    524 		if (sc->enable) {
    525 			if ((*sc->enable)(sc)) {
    526 				COM_UNLOCK(sc);
    527 				splx(s2);
    528 				splx(s);
    529 				aprint_normal_dev(sc->sc_dev, "device enable failed\n");
    530 				return EIO;
    531 			}
    532 			sc->enabled = 1;
    533 			sacom_config(sc);
    534 		}
    535 
    536 		/* Turn on interrupts. */
    537 		sc->sc_cr3 = CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE;
    538 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3,
    539 				  sc->sc_cr3);
    540 
    541 
    542 		COM_UNLOCK(sc);
    543 		splx(s2);
    544 
    545 		/*
    546 		 * Initialize the termios status to the defaults.  Add in the
    547 		 * sticky bits from TIOCSFLAGS.
    548 		 */
    549 		t.c_ispeed = 0;
    550 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    551 			t.c_ospeed = sacomconsrate;
    552 			t.c_cflag = sacomconscflag;
    553 		} else {
    554 			t.c_ospeed = TTYDEF_SPEED;
    555 			t.c_cflag = TTYDEF_CFLAG;
    556 		}
    557 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    558 			SET(t.c_cflag, CLOCAL);
    559 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    560 			SET(t.c_cflag, CRTSCTS);
    561 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    562 			SET(t.c_cflag, MDMBUF);
    563 		/* Make sure sacomparam() will do something. */
    564 		tp->t_ospeed = 0;
    565 		(void) sacomparam(tp, &t);
    566 		tp->t_iflag = TTYDEF_IFLAG;
    567 		tp->t_oflag = TTYDEF_OFLAG;
    568 		tp->t_lflag = TTYDEF_LFLAG;
    569 		ttychars(tp);
    570 		ttsetwater(tp);
    571 
    572 		s2 = splserial();
    573 		COM_LOCK(sc);
    574 
    575 		/*
    576 		 * Turn on DTR.  We must always do this, even if carrier is not
    577 		 * present, because otherwise we'd have to use TIOCSDTR
    578 		 * immediately after setting CLOCAL, which applications do not
    579 		 * expect.  We always assert DTR while the device is open
    580 		 * unless explicitly requested to deassert it.
    581 		 */
    582 		sacom_modem(sc, 1);
    583 
    584 		/* Clear the input ring, and unblock. */
    585 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    586 		sc->sc_rbavail = SACOM_RING_SIZE;
    587 		sacom_iflush(sc);
    588 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    589 		sacom_hwiflow(sc);
    590 
    591 #ifdef COM_DEBUG
    592 		if (sacom_debug)
    593 			comstatus(sc, "sacomopen  ");
    594 #endif
    595 
    596 		COM_UNLOCK(sc);
    597 		splx(s2);
    598 	}
    599 
    600 	splx(s);
    601 
    602 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    603 	if (error)
    604 		goto bad;
    605 
    606 	error = (*tp->t_linesw->l_open)(dev, tp);
    607 	if (error)
    608 		goto bad;
    609 
    610 	return 0;
    611 
    612 bad:
    613 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    614 		/*
    615 		 * We failed to open the device, and nobody else had it opened.
    616 		 * Clean up the state as appropriate.
    617 		 */
    618 		sacom_shutdown(sc);
    619 	}
    620 
    621 	return error;
    622 }
    623 
    624 int
    625 sacomclose(dev_t dev, int flag, int mode, struct lwp *l)
    626 {
    627 	struct sacom_softc *sc =
    628 		device_lookup_private(&sacom_cd, COMUNIT(dev));
    629 	struct tty *tp = sc->sc_tty;
    630 
    631 	/* XXX This is for cons.c. */
    632 	if (!ISSET(tp->t_state, TS_ISOPEN))
    633 		return 0;
    634 
    635 	(*tp->t_linesw->l_close)(tp, flag);
    636 	ttyclose(tp);
    637 
    638 	if (COM_ISALIVE(sc) == 0)
    639 		return 0;
    640 
    641 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    642 		/*
    643 		 * Although we got a last close, the device may still be in
    644 		 * use; e.g. if this was the dialout node, and there are still
    645 		 * processes waiting for carrier on the non-dialout node.
    646 		 */
    647 		sacom_shutdown(sc);
    648 	}
    649 
    650 	return 0;
    651 }
    652 
    653 int
    654 sacomread(dev_t dev, struct uio *uio, int flag)
    655 {
    656 	struct sacom_softc *sc =
    657 		device_lookup_private(&sacom_cd, COMUNIT(dev));
    658 	struct tty *tp = sc->sc_tty;
    659 
    660 	if (COM_ISALIVE(sc) == 0)
    661 		return EIO;
    662 
    663 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    664 }
    665 
    666 int
    667 sacomwrite(dev_t dev, struct uio *uio, int flag)
    668 {
    669 	struct sacom_softc *sc =
    670 		device_lookup_private(&sacom_cd, COMUNIT(dev));
    671 	struct tty *tp = sc->sc_tty;
    672 
    673 	if (COM_ISALIVE(sc) == 0)
    674 		return EIO;
    675 
    676 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    677 }
    678 
    679 int
    680 sacompoll(dev_t dev, int events, struct lwp *l)
    681 {
    682 	struct sacom_softc *sc =
    683 		device_lookup_private(&sacom_cd, COMUNIT(dev));
    684 	struct tty *tp = sc->sc_tty;
    685 
    686 	if (COM_ISALIVE(sc) == 0)
    687 		return EIO;
    688 
    689 	return (*tp->t_linesw->l_poll)(tp, events, l);
    690 }
    691 
    692 struct tty *
    693 sacomtty(dev_t dev)
    694 {
    695 	struct sacom_softc *sc =
    696 		device_lookup_private(&sacom_cd, COMUNIT(dev));
    697 	struct tty *tp = sc->sc_tty;
    698 
    699 	return tp;
    700 }
    701 
    702 int
    703 sacomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    704 {
    705 	struct sacom_softc *sc =
    706 		device_lookup_private(&sacom_cd, COMUNIT(dev));
    707 	struct tty *tp = sc->sc_tty;
    708 	int error;
    709 	int s;
    710 
    711 	if (COM_ISALIVE(sc) == 0)
    712 		return EIO;
    713 
    714 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    715 	if (error != EPASSTHROUGH)
    716 		return error;
    717 
    718 	error = ttioctl(tp, cmd, data, flag, l);
    719 	if (error != EPASSTHROUGH)
    720 		return error;
    721 
    722 	error = 0;
    723 
    724 	s = splserial();
    725 	COM_LOCK(sc);
    726 
    727 	switch (cmd) {
    728 	case TIOCSBRK:
    729 		sacom_break(sc, 1);
    730 		break;
    731 
    732 	case TIOCCBRK:
    733 		sacom_break(sc, 0);
    734 		break;
    735 
    736 	case TIOCSDTR:
    737 		sacom_modem(sc, 1);
    738 		break;
    739 
    740 	case TIOCCDTR:
    741 		sacom_modem(sc, 0);
    742 		break;
    743 
    744 	case TIOCGFLAGS:
    745 		*(int *)data = sc->sc_swflags;
    746 		break;
    747 
    748 	case TIOCSFLAGS:
    749 		error = kauth_authorize_device_tty(l->l_cred,
    750 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    751 		if (error)
    752 			break;
    753 		sc->sc_swflags = *(int *)data;
    754 		break;
    755 
    756 	case TIOCMSET:
    757 	case TIOCMBIS:
    758 	case TIOCMBIC:
    759 		tiocm_to_sacom(sc, cmd, *(int *)data);
    760 		break;
    761 
    762 	case TIOCMGET:
    763 		*(int *)data = sacom_to_tiocm(sc);
    764 		break;
    765 
    766 	default:
    767 		error = EPASSTHROUGH;
    768 		break;
    769 	}
    770 
    771 	COM_UNLOCK(sc);
    772 	splx(s);
    773 
    774 #ifdef COM_DEBUG
    775 	if (sacom_debug)
    776 		comstatus(sc, "comioctl ");
    777 #endif
    778 
    779 	return error;
    780 }
    781 
    782 static inline void
    783 sacom_schedrx(struct sacom_softc *sc)
    784 {
    785 
    786 	sc->sc_rx_ready = 1;
    787 
    788 	/* Wake up the poller. */
    789 	softint_schedule(sc->sc_si);
    790 }
    791 
    792 void
    793 sacom_break(struct sacom_softc *sc, int onoff)
    794 {
    795 
    796 	if (onoff)
    797 		SET(sc->sc_cr3, CR3_BRK);
    798 	else
    799 		CLR(sc->sc_cr3, CR3_BRK);
    800 
    801 	if (!sc->sc_heldchange) {
    802 		if (sc->sc_tx_busy) {
    803 			sc->sc_heldtbc = sc->sc_tbc;
    804 			sc->sc_tbc = 0;
    805 			sc->sc_heldchange = 1;
    806 		} else
    807 			sacom_loadchannelregs(sc);
    808 	}
    809 }
    810 
    811 void
    812 sacom_modem(struct sacom_softc *sc, int onoff)
    813 {
    814 	if (!sc->sc_heldchange) {
    815 		if (sc->sc_tx_busy) {
    816 			sc->sc_heldtbc = sc->sc_tbc;
    817 			sc->sc_tbc = 0;
    818 			sc->sc_heldchange = 1;
    819 		} else
    820 			sacom_loadchannelregs(sc);
    821 	}
    822 }
    823 
    824 void
    825 tiocm_to_sacom(struct sacom_softc *sc, u_long how, int ttybits)
    826 {
    827 }
    828 
    829 int
    830 sacom_to_tiocm(struct sacom_softc *sc)
    831 {
    832 	int ttybits = 0;
    833 
    834 	if (sc->sc_cr3 != 0)
    835 		SET(ttybits, TIOCM_LE);
    836 
    837 	return ttybits;
    838 }
    839 
    840 static u_int
    841 cflag2cr0(tcflag_t cflag)
    842 {
    843 	u_int cr0;
    844 
    845 	cr0  = (cflag & PARENB) ? CR0_PE : 0;
    846 	cr0 |= (cflag & PARODD) ? 0 : CR0_OES;
    847 	cr0 |= (cflag & CSTOPB) ? CR0_SBS : 0;
    848 	cr0 |= ((cflag & CSIZE) == CS8) ? CR0_DSS : 0;
    849 
    850 	return cr0;
    851 }
    852 
    853 int
    854 sacomparam(struct tty *tp, struct termios *t)
    855 {
    856 	struct sacom_softc *sc =
    857 		device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev));
    858 	int ospeed = SACOMSPEED(t->c_ospeed);
    859 	u_int cr0;
    860 	int s;
    861 
    862 	if (COM_ISALIVE(sc) == 0)
    863 		return EIO;
    864 
    865 	/* Check requested parameters. */
    866 	if (ospeed < 0)
    867 		return EINVAL;
    868 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    869 		return EINVAL;
    870 
    871 	/*
    872 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    873 	 * is always active.
    874 	 */
    875 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    876 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    877 		SET(t->c_cflag, CLOCAL);
    878 		CLR(t->c_cflag, HUPCL);
    879 	}
    880 
    881 	/*
    882 	 * If there were no changes, don't do anything.  This avoids dropping
    883 	 * input and improves performance when all we did was frob things like
    884 	 * VMIN and VTIME.
    885 	 */
    886 	if (tp->t_ospeed == t->c_ospeed &&
    887 	    tp->t_cflag == t->c_cflag)
    888 		return 0;
    889 
    890 	cr0 = cflag2cr0(t->c_cflag);
    891 
    892 	s = splserial();
    893 	COM_LOCK(sc);
    894 
    895 	sc->sc_cr0 = cr0;
    896 
    897 	sc->sc_speed = ospeed;
    898 
    899 	/* And copy to tty. */
    900 	tp->t_ispeed = 0;
    901 	tp->t_ospeed = t->c_ospeed;
    902 	tp->t_cflag = t->c_cflag;
    903 
    904 	if (!sc->sc_heldchange) {
    905 		if (sc->sc_tx_busy) {
    906 			sc->sc_heldtbc = sc->sc_tbc;
    907 			sc->sc_tbc = 0;
    908 			sc->sc_heldchange = 1;
    909 		} else
    910 			sacom_loadchannelregs(sc);
    911 	}
    912 
    913 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    914 		/* Disable the high water mark. */
    915 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
    916 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
    917 			sacom_schedrx(sc);
    918 		}
    919 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
    920 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
    921 			sacom_hwiflow(sc);
    922 		}
    923 	}
    924 
    925 	COM_UNLOCK(sc);
    926 	splx(s);
    927 
    928 	(void) (*tp->t_linesw->l_modem)(tp, 1);
    929 
    930 #ifdef COM_DEBUG
    931 	if (sacom_debug)
    932 		comstatus(sc, "comparam ");
    933 #endif
    934 
    935 	return 0;
    936 }
    937 
    938 void
    939 sacom_iflush(struct sacom_softc *sc)
    940 {
    941 	bus_space_tag_t iot = sc->sc_iot;
    942 	bus_space_handle_t ioh = sc->sc_ioh;
    943 #ifdef DIAGNOSTIC
    944 	int reg;
    945 #endif
    946 	int timo;
    947 
    948 #ifdef DIAGNOSTIC
    949 	reg = 0xffff;
    950 #endif
    951 	timo = 50;
    952 	/* flush any pending I/O */
    953 	if (sc->sc_cr3 & CR3_RXE) {
    954 	while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_RNE)
    955 	    && --timo)
    956 #ifdef DIAGNOSTIC
    957 		reg =
    958 #else
    959 		    (void)
    960 #endif
    961 		    bus_space_read_4(iot, ioh, SACOM_DR);
    962 	}
    963 #if 0
    964 	/* XXX is it good idea to wait TX finish? */
    965 	if (sc->sc_cr3 & CR3_TXE) {
    966 	timo = 500;
    967 	while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_TBY)
    968 	    && --timo)
    969 		delay(100);
    970 	}
    971 #endif
    972 #ifdef DIAGNOSTIC
    973 	if (!timo)
    974 		aprint_debug_dev(sc->sc_dev, "sacom_iflush timeout %02x\n", reg);
    975 #endif
    976 }
    977 
    978 void
    979 sacom_loadchannelregs(struct sacom_softc *sc)
    980 {
    981 	bus_space_tag_t iot = sc->sc_iot;
    982 	bus_space_handle_t ioh = sc->sc_ioh;
    983 
    984 	/* XXXXX necessary? */
    985 	sacom_iflush(sc);
    986 
    987 	/* Need to stop engines first. */
    988 	bus_space_write_4(iot, ioh, SACOM_CR3, 0);
    989 
    990 	bus_space_write_4(iot, ioh, SACOM_CR1, sc->sc_speed >> 8);
    991 	bus_space_write_4(iot, ioh, SACOM_CR2, sc->sc_speed & 0xff);
    992 
    993 	bus_space_write_4(iot, ioh, SACOM_CR0, sc->sc_cr0);
    994 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
    995 }
    996 
    997 int
    998 sacomhwiflow(struct tty *tp, int block)
    999 {
   1000 #if 0
   1001 	struct sacom_softc *sc =
   1002 		device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev));
   1003 	int s;
   1004 
   1005 	if (COM_ISALIVE(sc) == 0)
   1006 		return 0;
   1007 
   1008 	if (sc->sc_mcr_rts == 0)
   1009 		return 0;
   1010 
   1011 	s = splserial();
   1012 	COM_LOCK(sc);
   1013 
   1014 	if (block) {
   1015 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1016 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1017 			sacom_hwiflow(sc);
   1018 		}
   1019 	} else {
   1020 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1021 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1022 			sacom_schedrx(sc);
   1023 		}
   1024 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1025 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1026 			sacom_hwiflow(sc);
   1027 		}
   1028 	}
   1029 
   1030 	COM_UNLOCK(sc);
   1031 	splx(s);
   1032 #endif
   1033 	return 1;
   1034 }
   1035 
   1036 /*
   1037  * (un)block input via hw flowcontrol
   1038  */
   1039 void
   1040 sacom_hwiflow(struct sacom_softc *sc)
   1041 {
   1042 #if 0
   1043 	bus_space_tag_t iot = sc->sc_iot;
   1044 	bus_space_handle_t ioh = sc->sc_ioh;
   1045 
   1046 	/* XXX implement */
   1047 #endif
   1048 }
   1049 
   1050 
   1051 void
   1052 sacomstart(struct tty *tp)
   1053 {
   1054 	struct sacom_softc *sc =
   1055 		device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev));
   1056 	bus_space_tag_t iot = sc->sc_iot;
   1057 	bus_space_handle_t ioh = sc->sc_ioh;
   1058 	int s;
   1059 
   1060 	if (COM_ISALIVE(sc) == 0)
   1061 		return;
   1062 
   1063 	s = spltty();
   1064 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1065 		goto out;
   1066 	if (!ttypull(tp))
   1067 		goto out;
   1068 
   1069 	/* Grab the first contiguous region of buffer space. */
   1070 	{
   1071 		u_char *tba;
   1072 		int tbc;
   1073 
   1074 		tba = tp->t_outq.c_cf;
   1075 		tbc = ndqb(&tp->t_outq, 0);
   1076 
   1077 		(void)splserial();
   1078 		COM_LOCK(sc);
   1079 
   1080 		sc->sc_tba = tba;
   1081 		sc->sc_tbc = tbc;
   1082 	}
   1083 
   1084 	SET(tp->t_state, TS_BUSY);
   1085 	sc->sc_tx_busy = 1;
   1086 
   1087 	/* Enable transmit completion interrupts if necessary. */
   1088 	if (!ISSET(sc->sc_cr3, CR3_TIE)) {
   1089 		SET(sc->sc_cr3, CR3_TIE);
   1090 		bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
   1091 	}
   1092 
   1093 	/* Output the first chunk of the contiguous buffer. */
   1094 	sacom_filltx(sc);
   1095 
   1096 	COM_UNLOCK(sc);
   1097 out:
   1098 	splx(s);
   1099 	return;
   1100 }
   1101 
   1102 void
   1103 sacom_filltx(struct sacom_softc *sc)
   1104 {
   1105 	bus_space_tag_t iot = sc->sc_iot;
   1106 	bus_space_handle_t ioh = sc->sc_ioh;
   1107 	int c, n;
   1108 
   1109 	n = 0;
   1110 	while (bus_space_read_4(iot, ioh, SACOM_SR1) & SR1_TNF) {
   1111 		if (n == SACOM_TXFIFOLEN || n == sc->sc_tbc)
   1112 			break;
   1113 		c = *(sc->sc_tba + n);
   1114 		c &= 0xff;
   1115 		bus_space_write_4(iot, ioh, SACOM_DR, c);
   1116 		n++;
   1117 	}
   1118 	sc->sc_tbc -= n;
   1119 	sc->sc_tba += n;
   1120 }
   1121 
   1122 /*
   1123  * Stop output on a line.
   1124  */
   1125 void
   1126 sacomstop(struct tty *tp, int flag)
   1127 {
   1128 	struct sacom_softc *sc =
   1129 		device_lookup_private(&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 	softint_schedule(sc->sc_si);
   1404 
   1405 #ifdef 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(device_t parent, device_t self)
   1413 {
   1414 	struct sa11x0_softc *sasc;
   1415 
   1416 	sasc = device_private(parent);
   1417 
   1418 	/* XXX  this should be done at sc->enable function */
   1419 	bus_space_write_4(sasc->sc_iot, sasc->sc_gpioh,
   1420 	    SAGPIO_PCR, 0xa0000);
   1421 	bus_space_write_4(sasc->sc_iot, sasc->sc_gpioh,
   1422 	    SAGPIO_PSR, 0x100);
   1423 }
   1424 
   1425 /* Initialization for serial console */
   1426 int
   1427 sacominit(bus_space_tag_t iot, bus_addr_t iobase, int baud, tcflag_t cflag,
   1428     bus_space_handle_t *iohp)
   1429 {
   1430 	int brd, cr0;
   1431 
   1432 	if (bus_space_map(iot, iobase, SACOM_NPORTS, 0, iohp))
   1433 		aprint_normal("register map failed\n");
   1434 
   1435 	/* wait for the Tx queue to drain and disable the UART */
   1436 	while (bus_space_read_4(iot, *iohp, SACOM_SR1) & SR1_TBY)
   1437 		continue;
   1438 	bus_space_write_4(iot, *iohp, SACOM_CR3, 0);
   1439 
   1440 	cr0  = cflag2cr0(cflag);
   1441 	bus_space_write_4(iot, *iohp, SACOM_CR0, cr0);
   1442 
   1443 	brd = SACOMSPEED(baud);
   1444 	sacomconsrate = baud;
   1445 	sacomconsaddr = iobase;
   1446 	sacomconscflag = cflag;
   1447 	/* XXX assumes little endian */
   1448 	bus_space_write_4(iot, *iohp, SACOM_CR1, brd >> 8);
   1449 	bus_space_write_4(iot, *iohp, SACOM_CR2, brd & 0xff);
   1450 
   1451 	/* enable the UART */
   1452 	bus_space_write_4(iot, *iohp, SACOM_CR3, CR3_RXE | CR3_TXE);
   1453 
   1454 	return 0;
   1455 }
   1456 
   1457 void
   1458 sacomcnprobe(struct consdev *cp)
   1459 {
   1460 	cp->cn_pri = CN_REMOTE;
   1461 }
   1462 
   1463 void
   1464 sacomcninit(struct consdev *cp)
   1465 {
   1466 	if (cp == NULL) {
   1467 		/* XXX cp == NULL means that MMU is disabled. */
   1468 		sacomconsioh = SACOM3_BASE;
   1469 		sacomconstag = &sa11x0_bs_tag;
   1470 		cn_tab = &sacomcons;
   1471 		return;
   1472 	}
   1473 
   1474 	if (sacominit(&sa11x0_bs_tag, CONADDR, CONSPEED,
   1475 			  CONMODE, &sacomconsioh))
   1476 		panic("can't init serial console @%x", CONADDR);
   1477 	cn_tab = &sacomcons;
   1478 	sacomconstag = &sa11x0_bs_tag;
   1479 }
   1480 
   1481 int
   1482 sacomcngetc(dev_t dev)
   1483 {
   1484 	int c, s;
   1485 
   1486 	s = spltty();	/* XXX do we need this? */
   1487 
   1488 	while (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1489 		 & SR1_RNE)) {
   1490 #if defined(DDB) || defined(KGDB)
   1491 #ifndef DDB_BREAK_CHAR
   1492 		u_int sr0;
   1493 		extern int db_active;
   1494 
   1495 		sr0 = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR0);
   1496 		if (ISSET(sr0, SR0_RBB))
   1497 			bus_space_write_4(sacomconstag, sacomconsioh,
   1498 					  SACOM_SR0, SR0_RBB);
   1499 		if (ISSET(sr0, SR0_REB)) {
   1500 			bus_space_write_4(sacomconstag, sacomconsioh,
   1501 					  SACOM_SR0, SR0_REB);
   1502 			if (db_active == 0)
   1503 				console_debugger();
   1504 		}
   1505 #endif
   1506 #endif /* DDB || KGDB */
   1507 	}
   1508 
   1509 	c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
   1510 	c &= 0xff;
   1511 	splx(s);
   1512 
   1513 	return c;
   1514 }
   1515 
   1516 void
   1517 sacomcnputc(dev_t dev, int c)
   1518 {
   1519 	int s;
   1520 
   1521 	s = spltty();	/* XXX do we need this? */
   1522 
   1523 	while (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1524 	    & SR1_TNF))
   1525 		continue;
   1526 
   1527 	bus_space_write_4(sacomconstag, sacomconsioh, SACOM_DR, c);
   1528 	splx(s);
   1529 }
   1530 
   1531 void
   1532 sacomcnpollc(dev_t dev, int on)
   1533 {
   1534 
   1535 }
   1536 
   1537 #if 0
   1538 #ifdef DEBUG
   1539 int
   1540 sacomcncharpoll(void)
   1541 {
   1542 	int c;
   1543 
   1544 	if (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1545 		 & SR1_RNE))
   1546 		return -1;
   1547 
   1548 	c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
   1549 	c &= 0xff;
   1550 
   1551 	return c;
   1552 }
   1553 #endif
   1554 #endif
   1555 
   1556 /* helper function to identify the com ports used by
   1557  console or KGDB (and not yet autoconf attached) */
   1558 int
   1559 sacom_is_console(bus_space_tag_t iot, bus_addr_t iobase,
   1560     bus_space_handle_t *ioh)
   1561 {
   1562 	bus_space_handle_t help;
   1563 
   1564 	if (!sacomconsattached &&
   1565 	    iot == sacomconstag && iobase == sacomconsaddr)
   1566 		help = sacomconsioh;
   1567 	else
   1568 		return 0;
   1569 
   1570 	if (ioh)
   1571 		*ioh = help;
   1572 	return 1;
   1573 }
   1574