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