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