Home | History | Annotate | Line # | Download | only in dev
sbjcn.c revision 1.24
      1 /* $NetBSD: sbjcn.c,v 1.24 2009/12/14 00:46:08 matt Exp $ */
      2 
      3 /*
      4  * Copyright 2000, 2001
      5  * Broadcom Corporation. All rights reserved.
      6  *
      7  * This software is furnished under license and may be used and copied only
      8  * in accordance with the following terms and conditions.  Subject to these
      9  * conditions, you may download, copy, install, use, modify and distribute
     10  * modified or unmodified copies of this software in source and/or binary
     11  * form. No title or ownership is transferred hereby.
     12  *
     13  * 1) Any source code used, modified or distributed must reproduce and
     14  *    retain this copyright notice and list of conditions as they appear in
     15  *    the source file.
     16  *
     17  * 2) No right is granted to use any trade name, trademark, or logo of
     18  *    Broadcom Corporation.  The "Broadcom Corporation" name may not be
     19  *    used to endorse or promote products derived from this software
     20  *    without the prior written permission of Broadcom Corporation.
     21  *
     22  * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
     23  *    WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
     24  *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
     25  *    NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
     26  *    FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
     27  *    LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     30  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     31  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     32  *    OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /* from: $NetBSD: com.c,v 1.172 2000/05/03 19:19:04 thorpej Exp */
     36 
     37 /*-
     38  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
     39  * All rights reserved.
     40  *
     41  * This code is derived from software contributed to The NetBSD Foundation
     42  * by Charles M. Hannum.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     54  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     56  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     57  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     58  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     59  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     61  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     63  * POSSIBILITY OF SUCH DAMAGE.
     64  */
     65 
     66 /*
     67  * Copyright (c) 1991 The Regents of the University of California.
     68  * All rights reserved.
     69  *
     70  * Redistribution and use in source and binary forms, with or without
     71  * modification, are permitted provided that the following conditions
     72  * are met:
     73  * 1. Redistributions of source code must retain the above copyright
     74  *    notice, this list of conditions and the following disclaimer.
     75  * 2. Redistributions in binary form must reproduce the above copyright
     76  *    notice, this list of conditions and the following disclaimer in the
     77  *    documentation and/or other materials provided with the distribution.
     78  * 3. Neither the name of the University nor the names of its contributors
     79  *    may be used to endorse or promote products derived from this software
     80  *    without specific prior written permission.
     81  *
     82  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     83  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     84  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     85  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     86  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     87  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     88  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     89  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     90  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     91  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     92  * SUCH DAMAGE.
     93  *
     94  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     95  */
     96 
     97 /*
     98  * `sbjcn' driver, supports console over SiByte SB-1250 JTAG.
     99  *
    100  *  Accesses a section of JTAG memory space to mimic a console,
    101  *  if there's a matching program outside to communicate with.
    102  *  If nobody is there, things will be very quiet.
    103  */
    104 
    105 #include <sys/cdefs.h>
    106 __KERNEL_RCSID(0, "$NetBSD: sbjcn.c,v 1.24 2009/12/14 00:46:08 matt Exp $");
    107 
    108 #define	SBJCN_DEBUG
    109 
    110 #include "opt_ddb.h"
    111 
    112 #include <sys/param.h>
    113 #include <sys/systm.h>
    114 #include <sys/ioctl.h>
    115 #include <sys/select.h>
    116 #include <sys/tty.h>
    117 #include <sys/proc.h>
    118 #include <sys/conf.h>
    119 #include <sys/file.h>
    120 #include <sys/uio.h>
    121 #include <sys/kernel.h>
    122 #include <sys/syslog.h>
    123 #include <sys/types.h>
    124 #include <sys/device.h>
    125 #include <sys/malloc.h>
    126 #include <sys/vnode.h>
    127 #include <sys/kauth.h>
    128 
    129 #include <sbmips/dev/sbscd/sbscdvar.h>
    130 #include <sbmips/dev/sbscd/sbjcnvar.h>
    131 #include <dev/cons.h>
    132 #include <machine/locore.h>
    133 
    134 void	sbjcn_attach_channel(struct sbjcn_softc *sc, int chan, int intr);
    135 static void sbjcncn_grabdword(struct sbjcn_channel *ch);
    136 static char sbjcncn_nextbyte(struct sbjcn_channel *ch);
    137 static void sbjcn_cngrabdword(void);
    138 
    139 #if defined(DDB) || defined(KGDB)
    140 static void sbjcn_enable_debugport(struct sbjcn_channel *ch);
    141 #endif
    142 void	sbjcn_config(struct sbjcn_channel *ch);
    143 void	sbjcn_shutdown(struct sbjcn_channel *ch);
    144 int	sbjcn_speed(long, long *);
    145 static int cflag2modes(tcflag_t, u_char *, u_char *);
    146 int	sbjcn_param(struct tty *, struct termios *);
    147 void	sbjcn_start(struct tty *);
    148 int	sbjcn_hwiflow(struct tty *, int);
    149 
    150 void	sbjcn_loadchannelregs(struct sbjcn_channel *);
    151 void	sbjcn_dohwiflow(struct sbjcn_channel *);
    152 void	sbjcn_break(struct sbjcn_channel *, int);
    153 void	sbjcn_modem(struct sbjcn_channel *, int);
    154 void	tiocm_to_sbjcn(struct sbjcn_channel *, int, int);
    155 int	sbjcn_to_tiocm(struct sbjcn_channel *);
    156 void	sbjcn_iflush(struct sbjcn_channel *);
    157 
    158 int	sbjcn_init(u_long addr, int chan, int rate, tcflag_t cflag);
    159 int	sbjcn_common_getc(u_long addr, int chan);
    160 void	sbjcn_common_putc(u_long addr, int chan, int c);
    161 
    162 int	sbjcn_cngetc(dev_t dev);
    163 void	sbjcn_cnputc(dev_t dev, int c);
    164 void	sbjcn_cnpollc(dev_t dev, int on);
    165 
    166 extern struct cfdriver sbjcn_cd;
    167 
    168 dev_type_open(sbjcnopen);
    169 dev_type_close(sbjcnclose);
    170 dev_type_read(sbjcnread);
    171 dev_type_write(sbjcnwrite);
    172 dev_type_ioctl(sbjcnioctl);
    173 dev_type_stop(sbjcnstop);
    174 dev_type_tty(sbjcntty);
    175 
    176 const struct cdevsw sbjcn_cdevsw = {
    177 	sbjcnopen, sbjcnclose, sbjcnread, sbjcnwrite, sbjcnioctl,
    178 	sbjcnstop, sbjcntty, nopoll, nommap, ttykqfilter, D_TTY
    179 };
    180 
    181 #define	integrate	static inline
    182 integrate void sbjcn_rxsoft(struct sbjcn_channel *, struct tty *);
    183 integrate void sbjcn_txsoft(struct sbjcn_channel *, struct tty *);
    184 integrate void sbjcn_stsoft(struct sbjcn_channel *, struct tty *);
    185 integrate void sbjcn_schedrx(struct sbjcn_channel *);
    186 integrate void sbjcn_recv(struct sbjcn_channel *ch);
    187 void	sbjcn_diag(void *);
    188 void	sbjcn_callout(void *);
    189 
    190 /*
    191  * Make this an option variable one can patch.
    192  * But be warned:  this must be a power of 2!
    193  */
    194 u_int sbjcn_rbuf_size = SBJCN_RING_SIZE;
    195 
    196 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    197 u_int sbjcn_rbuf_hiwat = (SBJCN_RING_SIZE * 1) / 4;
    198 u_int sbjcn_rbuf_lowat = (SBJCN_RING_SIZE * 3) / 4;
    199 
    200 static int	sbjcn_cons_present;
    201 static int	sbjcn_cons_attached;
    202 static u_long	sbjcn_cons_addr;
    203 static int	sbjcn_cons_chan;
    204 static int	sbjcn_cons_rate;
    205 static tcflag_t	sbjcn_cons_cflag;
    206 static int	sbjcn_cons_waiting_input;
    207 static uint64_t	sbjcn_cons_input_buf;
    208 
    209 #ifdef KGDB
    210 #include <sys/kgdb.h>
    211 
    212 static int	sbjcn_kgdb_present;
    213 static int	sbjcn_kgdb_attached;
    214 static u_long	sbjcn_kgdb_addr;
    215 static int	sbjcn_kgdb_chan;
    216 
    217 int	sbjcn_kgdb_getc(void *);
    218 void	sbjcn_kgdb_putc(void *, int);
    219 #endif /* KGDB */
    220 
    221 static int	sbjcn_match(struct device *, struct cfdata *, void *);
    222 static void	sbjcn_attach(struct device *, struct device *, void *);
    223 
    224 CFATTACH_DECL(sbjcn, sizeof(struct sbjcn_softc),
    225     sbjcn_match, sbjcn_attach, NULL, NULL);
    226 
    227 #define	READ_REG(rp)		(mips3_ld((volatile uint64_t *)(rp)))
    228 #define	WRITE_REG(rp, val)	(mips3_sd((volatile uint64_t *)(rp), (val)))
    229 
    230 #define	JTAG_CONS_CONTROL  0x00
    231 #define	JTAG_CONS_INPUT    0x20
    232 #define	JTAG_CONS_OUTPUT   0x40
    233 #define	JTAG_CONS_MAGICNUM 0x50FABEEF12349873
    234 
    235 #define	jtag_input_len(data)    (((data) >> 56) & 0xFF)
    236 
    237 
    238 static int
    239 sbjcn_match(struct device *parent, struct cfdata *match, void *aux)
    240 {
    241 	struct sbscd_attach_args *sap = aux;
    242 
    243 	if (sap->sa_locs.sa_type != SBSCD_DEVTYPE_JTAGCONS)
    244 		return (0);
    245 
    246 	return 1;
    247 }
    248 
    249 static void
    250 sbjcn_attach(struct device *parent, struct device *self, void *aux)
    251 {
    252 	struct sbjcn_softc *sc = (struct sbjcn_softc *)self;
    253 	struct sbscd_attach_args *sap = aux;
    254 
    255 	sc->sc_addr = sap->sa_base + sap->sa_locs.sa_offset;
    256 
    257 	printf("\n");
    258 	sbjcn_attach_channel(sc, 0, sap->sa_locs.sa_intr[0]);
    259 }
    260 
    261 void
    262 sbjcn_attach_channel(struct sbjcn_softc *sc, int chan, int intr)
    263 {
    264 	struct sbjcn_channel *ch = &sc->sc_channels[chan];
    265 	u_long chan_addr;
    266 	struct tty *tp;
    267 
    268 	ch->ch_sc = sc;
    269 	ch->ch_num = chan;
    270 
    271 	chan_addr = sc->sc_addr + (0x100 * chan);
    272 	ch->ch_base = (void *)MIPS_PHYS_TO_KSEG1(chan_addr);
    273 	ch->ch_input_reg =
    274 	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + JTAG_CONS_INPUT);
    275 	ch->ch_output_reg =
    276 	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + JTAG_CONS_OUTPUT);
    277 	ch->ch_control_reg =
    278 	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + JTAG_CONS_CONTROL);
    279 	ch->ch_waiting_input = 0;
    280 
    281 	ch->ch_i_dcd = ch->ch_i_dcd_pin = 0 /* XXXCGD */;
    282 	ch->ch_i_cts = ch->ch_i_cts_pin = 0 /* XXXCGD */;
    283 	ch->ch_i_dsr = ch->ch_i_dsr_pin = 0 /* XXXCGD */;
    284 	ch->ch_i_ri = ch->ch_i_ri_pin = 0 /* XXXCGD */;
    285 	ch->ch_i_mask =
    286 	    ch->ch_i_dcd | ch->ch_i_cts | ch->ch_i_dsr | ch->ch_i_ri;
    287 	ch->ch_o_dtr = ch->ch_o_dtr_pin = 0 /* XXXCGD */;
    288 	ch->ch_o_rts = ch->ch_o_rts_pin = 0 /* XXXCGD */;
    289 	ch->ch_o_mask = ch->ch_o_dtr | ch->ch_o_rts;
    290 
    291 	callout_init(&ch->ch_diag_callout, 0);
    292 	callout_init(&ch->ch_callout, 0);
    293 
    294 	/* Disable interrupts before configuring the device. */
    295 	ch->ch_imr = 0;
    296 
    297 	if (sbjcn_cons_present &&
    298 	    sbjcn_cons_addr == chan_addr && sbjcn_cons_chan == chan) {
    299 		sbjcn_cons_attached = 1;
    300 
    301 		/* Make sure the console is always "hardwired". */
    302 		delay(1000);			/* wait for output to finish */
    303 		SET(ch->ch_hwflags, SBJCN_HW_CONSOLE);
    304 		SET(ch->ch_swflags, TIOCFLAG_SOFTCAR);
    305 	}
    306 
    307 	tp = ttymalloc();
    308 	tp->t_oproc = sbjcn_start;
    309 	tp->t_param = sbjcn_param;
    310 	tp->t_hwiflow = sbjcn_hwiflow;
    311 
    312 	ch->ch_tty = tp;
    313 	ch->ch_rbuf = malloc(sbjcn_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
    314 	if (ch->ch_rbuf == NULL) {
    315 		printf("%s: channel %d: unable to allocate ring buffer\n",
    316 		    sc->sc_dev.dv_xname, chan);
    317 		return;
    318 	}
    319 	ch->ch_ebuf = ch->ch_rbuf + (sbjcn_rbuf_size << 1);
    320 
    321 	tty_attach(tp);
    322 
    323 	if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE)) {
    324 		int maj;
    325 
    326 		/* locate the major number */
    327 		maj = cdevsw_lookup_major(&sbjcn_cdevsw);
    328 
    329 		cn_tab->cn_dev = makedev(maj,
    330 		    (device_unit(&sc->sc_dev) << 1) + chan);
    331 
    332 		printf("%s: channel %d: console\n", sc->sc_dev.dv_xname, chan);
    333 	}
    334 
    335 #ifdef KGDB
    336 	/*
    337 	 * Allow kgdb to "take over" this port.  If this is
    338 	 * the kgdb device, it has exclusive use.
    339 	 */
    340 	if (sbjcn_kgdb_present &&
    341 	    sbjcn_kgdb_addr == chan_addr && sbjcn_kgdb_chan == chan) {
    342 		sbjcn_kgdb_attached = 1;
    343 
    344 		SET(ch->ch_hwflags, SBJCN_HW_KGDB);
    345 		printf("%s: channel %d: kgdb\n", sc->sc_dev.dv_xname, chan);
    346 	}
    347 #endif
    348 
    349 	sbjcn_config(ch);
    350 
    351 	callout_reset(&ch->ch_callout, hz/10, sbjcn_callout, ch);
    352 
    353 	SET(ch->ch_hwflags, SBJCN_HW_DEV_OK);
    354 }
    355 
    356 int
    357 sbjcn_speed(long speed, long *brcp)
    358 {
    359 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    360 
    361 	int x, err;
    362 	int frequency = 100000000;
    363 
    364 	*brcp = divrnd(frequency / 20, speed) - 1;
    365 
    366 	if (speed <= 0)
    367 		return (-1);
    368 	x = divrnd(frequency / 20, speed);
    369 	if (x <= 0)
    370 		return (-1);
    371 	err = divrnd(((quad_t)frequency) * 1000 / 20, speed * x) - 1000;
    372 	if (err < 0)
    373 		err = -err;
    374 	if (err > SBJCN_TOLERANCE)
    375 		return (-1);
    376 	*brcp = x - 1;
    377 	return (0);
    378 
    379 #undef	divrnd
    380 }
    381 
    382 #ifdef SBJCN_DEBUG
    383 void	sbjcn_status(struct sbjcn_channel *, char *);
    384 
    385 int	sbjcn_debug = 1 /* XXXCGD */;
    386 
    387 void
    388 sbjcn_status(struct sbjcn_channel *ch, char *str)
    389 {
    390 	struct sbjcn_softc *sc = ch->ch_sc;
    391 	struct tty *tp = ch->ch_tty;
    392 
    393 	printf("%s: chan %d: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
    394 	    sc->sc_dev.dv_xname, ch->ch_num, str,
    395 	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
    396 	    ISSET(ch->ch_iports, ch->ch_i_dcd) ? "+" : "-",
    397 	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
    398 	    ISSET(ch->ch_oports, ch->ch_o_dtr) ? "+" : "-",
    399 	    ch->ch_tx_stopped ? "+" : "-");
    400 
    401 	printf("%s: chan %d: %s %scrtscts %scts %sts_ttstop  %srts %xrx_flags\n",
    402 	    sc->sc_dev.dv_xname, ch->ch_num, str,
    403 	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
    404 	    ISSET(ch->ch_iports, ch->ch_i_cts) ? "+" : "-",
    405 	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
    406 	    ISSET(ch->ch_oports, ch->ch_o_rts) ? "+" : "-",
    407 	    ch->ch_rx_flags);
    408 }
    409 #endif
    410 
    411 #if defined(DDB) || defined(KGDB)
    412 static void
    413 sbjcn_enable_debugport(struct sbjcn_channel *ch)
    414 {
    415 	int s;
    416 
    417 	/* Turn on line break interrupt, set carrier. */
    418 	s = splserial();
    419 
    420 	ch->ch_imr = 0x04;
    421 	SET(ch->ch_oports, ch->ch_o_dtr | ch->ch_o_rts);
    422 
    423 	splx(s);
    424 }
    425 #endif
    426 
    427 void
    428 sbjcn_config(struct sbjcn_channel *ch)
    429 {
    430 
    431 	/* Disable interrupts before configuring the device. */
    432 	ch->ch_imr = 0x00;
    433 
    434 #ifdef DDB
    435 	if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE))
    436 		sbjcn_enable_debugport(ch);
    437 #endif
    438 
    439 #ifdef KGDB
    440 	/*
    441 	 * Allow kgdb to "take over" this port.  If this is
    442 	 * the kgdb device, it has exclusive use.
    443 	 */
    444 	if (ISSET(ch->ch_hwflags, SBJCN_HW_KGDB))
    445 		sbjcn_enable_debugport(ch);
    446 #endif
    447 }
    448 
    449 void
    450 sbjcn_shutdown(struct sbjcn_channel *ch)
    451 {
    452 	struct tty *tp = ch->ch_tty;
    453 	int s;
    454 
    455 	s = splserial();
    456 
    457 	/* If we were asserting flow control, then deassert it. */
    458 	SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
    459 	sbjcn_dohwiflow(ch);
    460 
    461 	/* Clear any break condition set with TIOCSBRK. */
    462 	sbjcn_break(ch, 0);
    463 
    464 	/*
    465 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    466 	 * notice even if we immediately open the port again.
    467 	 */
    468 	if (ISSET(tp->t_cflag, HUPCL)) {
    469 		sbjcn_modem(ch, 0);
    470 		(void) tsleep(ch, TTIPRI, ttclos, hz);
    471 	}
    472 
    473 	/* Turn off interrupts. */
    474 #ifdef DDB
    475 	if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE))
    476 		ch->ch_imr = 0x04; /* interrupt on break */
    477 	else
    478 #endif
    479 		ch->ch_imr = 0;
    480 
    481 	splx(s);
    482 }
    483 
    484 int
    485 sbjcnopen(dev_t dev, int flag, int mode, struct lwp *l)
    486 {
    487 	int chan = SBJCN_CHAN(dev);
    488 	struct sbjcn_softc *sc;
    489 	struct sbjcn_channel *ch;
    490 	struct tty *tp;
    491 	int s, s2;
    492 	int error;
    493 
    494 	sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(dev));
    495 	if (sc == NULL)
    496 		return (ENXIO);
    497 
    498 	ch = &sc->sc_channels[chan];
    499 	if (!ISSET(ch->ch_hwflags, SBJCN_HW_DEV_OK) || ch->ch_rbuf == NULL)
    500 		return (ENXIO);
    501 
    502 #ifdef KGDB
    503 	/*
    504 	 * If this is the kgdb port, no other use is permitted.
    505 	 */
    506 	if (ISSET(ch->ch_hwflags, SBJCN_HW_KGDB))
    507 		return (EBUSY);
    508 #endif
    509 
    510 	tp = ch->ch_tty;
    511 
    512 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    513 		return (EBUSY);
    514 
    515 	s = spltty();
    516 
    517 	/*
    518 	 * Do the following iff this is a first open.
    519 	 */
    520 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    521 		struct termios t;
    522 
    523 		tp->t_dev = dev;
    524 
    525 		s2 = splserial();
    526 
    527 		/* Turn on receive, break, and status change interrupts. */
    528 		ch->ch_imr = 0xe;
    529 
    530 		/* Fetch the current modem control status, needed later. */
    531 		ch->ch_iports = 0;
    532 		ch->ch_iports_delta = 0;
    533 		splx(s2);
    534 
    535 		/*
    536 		 * Initialize the termios status to the defaults.  Add in the
    537 		 * sticky bits from TIOCSFLAGS.
    538 		 */
    539 		t.c_ispeed = 0;
    540 		if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE)) {
    541 			t.c_ospeed = sbjcn_cons_rate;
    542 			t.c_cflag = sbjcn_cons_cflag;
    543 		} else {
    544 			t.c_ospeed = TTYDEF_SPEED;
    545 			t.c_cflag = TTYDEF_CFLAG;
    546 		}
    547 		if (ISSET(ch->ch_swflags, TIOCFLAG_CLOCAL))
    548 			SET(t.c_cflag, CLOCAL);
    549 		if (ISSET(ch->ch_swflags, TIOCFLAG_CRTSCTS))
    550 			SET(t.c_cflag, CRTSCTS);
    551 		if (ISSET(ch->ch_swflags, TIOCFLAG_MDMBUF))
    552 			SET(t.c_cflag, MDMBUF);
    553 		/* Make sure sbjcn_param() will do something. */
    554 		tp->t_ospeed = 0;
    555 		(void) sbjcn_param(tp, &t);
    556 		tp->t_iflag = TTYDEF_IFLAG;
    557 		tp->t_oflag = TTYDEF_OFLAG;
    558 		tp->t_lflag = TTYDEF_LFLAG;
    559 		ttychars(tp);
    560 		ttsetwater(tp);
    561 
    562 		s2 = splserial();
    563 
    564 		/*
    565 		 * Turn on DTR.  We must always do this, even if carrier is not
    566 		 * present, because otherwise we'd have to use TIOCSDTR
    567 		 * immediately after setting CLOCAL, which applications do not
    568 		 * expect.  We always assert DTR while the device is open
    569 		 * unless explicitly requested to deassert it.
    570 		 */
    571 		sbjcn_modem(ch, 1);
    572 
    573 		/* Clear the input ring, and unblock. */
    574 		ch->ch_rbput = ch->ch_rbget = ch->ch_rbuf;
    575 		ch->ch_rbavail = sbjcn_rbuf_size;
    576 		sbjcn_iflush(ch);
    577 		CLR(ch->ch_rx_flags, RX_ANY_BLOCK);
    578 		sbjcn_dohwiflow(ch);
    579 
    580 #ifdef SBJCN_DEBUG
    581 		if (sbjcn_debug)
    582 			sbjcn_status(ch, "sbjcnopen  ");
    583 #endif
    584 
    585 		splx(s2);
    586 	}
    587 
    588 	splx(s);
    589 
    590 	error = ttyopen(tp, SBJCN_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
    591 	if (error)
    592 		goto bad;
    593 
    594 	error = (*tp->t_linesw->l_open)(dev, tp);
    595 	if (error)
    596 		goto bad;
    597 
    598 	return (0);
    599 
    600 bad:
    601 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    602 		/*
    603 		 * We failed to open the device, and nobody else had it opened.
    604 		 * Clean up the state as appropriate.
    605 		 */
    606 		sbjcn_shutdown(ch);
    607 	}
    608 
    609 	return (error);
    610 }
    611 
    612 int
    613 sbjcnclose(dev_t dev, int flag, int mode, struct proc *p)
    614 {
    615 	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(dev));
    616 	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
    617 	struct tty *tp = ch->ch_tty;
    618 
    619 	/* XXX This is for cons.c. */
    620 	if (!ISSET(tp->t_state, TS_ISOPEN))
    621 		return (0);
    622 
    623 	(*tp->t_linesw->l_close)(tp, flag);
    624 	ttyclose(tp);
    625 
    626 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    627 		/*
    628 		 * Although we got a last close, the device may still be in
    629 		 * use; e.g. if this was the dialout node, and there are still
    630 		 * processes waiting for carrier on the non-dialout node.
    631 		 */
    632 		sbjcn_shutdown(ch);
    633 	}
    634 
    635 	return (0);
    636 }
    637 
    638 int
    639 sbjcnread(dev_t dev, struct uio *uio, int flag)
    640 {
    641 	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(dev));
    642 	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
    643 	struct tty *tp = ch->ch_tty;
    644 
    645 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    646 }
    647 
    648 int
    649 sbjcnwrite(dev_t dev, struct uio *uio, int flag)
    650 {
    651 	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(dev));
    652 	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
    653 	struct tty *tp = ch->ch_tty;
    654 
    655 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    656 }
    657 
    658 struct tty *
    659 sbjcntty(dev_t dev)
    660 {
    661 	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(dev));
    662 	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
    663 	struct tty *tp = ch->ch_tty;
    664 
    665 	return (tp);
    666 }
    667 
    668 int
    669 sbjcnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    670 {
    671 	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(dev));
    672 	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
    673 	struct tty *tp = ch->ch_tty;
    674 	int error;
    675 	int s;
    676 
    677 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    678 	if (error >= 0)
    679 		return (error);
    680 
    681 	error = ttioctl(tp, cmd, data, flag, p);
    682 	if (error >= 0)
    683 		return (error);
    684 
    685 	error = 0;
    686 
    687 	s = splserial();
    688 
    689 	switch (cmd) {
    690 	case TIOCSBRK:
    691 		sbjcn_break(ch, 1);
    692 		break;
    693 
    694 	case TIOCCBRK:
    695 		sbjcn_break(ch, 0);
    696 		break;
    697 
    698 	case TIOCSDTR:
    699 		sbjcn_modem(ch, 1);
    700 		break;
    701 
    702 	case TIOCCDTR:
    703 		sbjcn_modem(ch, 0);
    704 		break;
    705 
    706 	case TIOCGFLAGS:
    707 		*(int *)data = ch->ch_swflags;
    708 		break;
    709 
    710 	case TIOCSFLAGS:
    711 		error = kauth_authorize_device_tty(l->l_cred,
    712 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    713 		if (error)
    714 			break;
    715 		ch->ch_swflags = *(int *)data;
    716 		break;
    717 
    718 	case TIOCMSET:
    719 	case TIOCMBIS:
    720 	case TIOCMBIC:
    721 		tiocm_to_sbjcn(ch, cmd, *(int *)data);
    722 		break;
    723 
    724 	case TIOCMGET:
    725 		*(int *)data = sbjcn_to_tiocm(ch);
    726 		break;
    727 
    728 	default:
    729 		error = ENOTTY;
    730 		break;
    731 	}
    732 
    733 	splx(s);
    734 
    735 #ifdef SBJCN_DEBUG
    736 	if (sbjcn_debug)
    737 		sbjcn_status(ch, "sbjcn_ioctl ");
    738 #endif
    739 
    740 	return (error);
    741 }
    742 
    743 integrate void
    744 sbjcn_schedrx(struct sbjcn_channel *ch)
    745 {
    746 
    747 	ch->ch_rx_ready = 1;
    748 
    749 	/* Next callout will detect this flag. */
    750 }
    751 
    752 void
    753 sbjcn_break(struct sbjcn_channel *ch, int onoff)
    754 {
    755 	/* XXXKW do something? */
    756 }
    757 
    758 void
    759 sbjcn_modem(struct sbjcn_channel *ch, int onoff)
    760 {
    761 
    762 	if (ch->ch_o_dtr == 0)
    763 		return;
    764 
    765 	if (onoff)
    766 		SET(ch->ch_oports, ch->ch_o_dtr);
    767 	else
    768 		CLR(ch->ch_oports, ch->ch_o_dtr);
    769 
    770 	if (!ch->ch_heldchange) {
    771 		if (ch->ch_tx_busy) {
    772 			ch->ch_heldtbc = ch->ch_tbc;
    773 			ch->ch_tbc = 0;
    774 			ch->ch_heldchange = 1;
    775 		} else
    776 			sbjcn_loadchannelregs(ch);
    777 	}
    778 }
    779 
    780 void
    781 tiocm_to_sbjcn(struct sbjcn_channel *ch, int how, int ttybits)
    782 {
    783 	u_char bits;
    784 
    785 	bits = 0;
    786 	if (ISSET(ttybits, TIOCM_DTR))
    787 		SET(bits, ch->ch_o_dtr);
    788 	if (ISSET(ttybits, TIOCM_RTS))
    789 		SET(bits, ch->ch_o_rts);
    790 
    791 	switch (how) {
    792 	case TIOCMBIC:
    793 		CLR(ch->ch_oports, bits);
    794 		break;
    795 
    796 	case TIOCMBIS:
    797 		SET(ch->ch_oports, bits);
    798 		break;
    799 
    800 	case TIOCMSET:
    801 		ch->ch_oports = bits;
    802 		break;
    803 	}
    804 
    805 	if (!ch->ch_heldchange) {
    806 		if (ch->ch_tx_busy) {
    807 			ch->ch_heldtbc = ch->ch_tbc;
    808 			ch->ch_tbc = 0;
    809 			ch->ch_heldchange = 1;
    810 		} else
    811 			sbjcn_loadchannelregs(ch);
    812 	}
    813 }
    814 
    815 int
    816 sbjcn_to_tiocm(struct sbjcn_channel *ch)
    817 {
    818 	u_char hwbits;
    819 	int ttybits = 0;
    820 
    821 	hwbits = ch->ch_oports;
    822 	if (ISSET(hwbits, ch->ch_o_dtr))
    823 		SET(ttybits, TIOCM_DTR);
    824 	if (ISSET(hwbits, ch->ch_o_rts))
    825 		SET(ttybits, TIOCM_RTS);
    826 
    827 	hwbits = ch->ch_iports;
    828 	if (ISSET(hwbits, ch->ch_i_dcd))
    829 		SET(ttybits, TIOCM_CD);
    830 	if (ISSET(hwbits, ch->ch_i_cts))
    831 		SET(ttybits, TIOCM_CTS);
    832 	if (ISSET(hwbits, ch->ch_i_dsr))
    833 		SET(ttybits, TIOCM_DSR);
    834 	if (ISSET(hwbits, ch->ch_i_ri))
    835 		SET(ttybits, TIOCM_RI);
    836 
    837 	if (ch->ch_imr != 0)
    838 		SET(ttybits, TIOCM_LE);
    839 
    840 	return (ttybits);
    841 }
    842 
    843 static int
    844 cflag2modes(tcflag_t cflag, u_char *mode1p, u_char *mode2p)
    845 {
    846 	u_char mode1;
    847 	u_char mode2;
    848 	int err = 0;
    849 
    850 	mode1 = mode2 = 0;
    851 
    852 	switch (ISSET(cflag, CSIZE)) {
    853 	case CS7:
    854 		mode1 |= 2;			/* XXX */
    855 		break;
    856 	default:
    857 		err = -1;
    858 		/* FALLTHRU for sanity */
    859 	case CS8:
    860 		mode1 |= 3;			/* XXX */
    861 		break;
    862 	}
    863 	if (!ISSET(cflag, PARENB))
    864 		mode1 |= 2 << 3;
    865 	else {
    866 		mode1 |= 0 << 3;
    867 		if (ISSET(cflag, PARODD))
    868 			mode1 |= 1 << 2;
    869 	}
    870 
    871 	if (ISSET(cflag, CSTOPB))
    872 		mode2 |= 1 << 3;		/* two stop bits XXX not std */
    873 
    874 	if (ISSET(cflag, CRTSCTS)) {
    875 		mode1 |= 1 << 7;
    876 		mode2 |= 1 << 4;
    877 	}
    878 
    879 	*mode1p = mode1;
    880 	*mode2p = mode2;
    881 	return (err);
    882 }
    883 
    884 int
    885 sbjcn_param(struct tty *tp, struct termios *t)
    886 {
    887 	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(tp->t_dev));
    888 	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
    889 	long brc;
    890 	u_char mode1, mode2;
    891 	int s;
    892 
    893 /* XXX reset to console parameters if console? */
    894 #if 0
    895 XXX disable, enable.
    896 #endif
    897 
    898 	/* Check requested parameters. */
    899 	if (sbjcn_speed(t->c_ospeed, &brc) < 0)
    900 		return (EINVAL);
    901 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    902 		return (EINVAL);
    903 
    904 	/*
    905 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    906 	 * is always active.
    907 	 */
    908 	if (ISSET(ch->ch_swflags, TIOCFLAG_SOFTCAR) ||
    909 	    ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE)) {
    910 		SET(t->c_cflag, CLOCAL);
    911 		CLR(t->c_cflag, HUPCL);
    912 	}
    913 
    914 	/*
    915 	 * If there were no changes, don't do anything.  This avoids dropping
    916 	 * input and improves performance when all we did was frob things like
    917 	 * VMIN and VTIME.
    918 	 */
    919 	if (tp->t_ospeed == t->c_ospeed &&
    920 	    tp->t_cflag == t->c_cflag)
    921 		return (0);
    922 
    923 	if (cflag2modes(t->c_cflag, &mode1, &mode2) < 0)
    924 		return (EINVAL);
    925 
    926 	s = splserial();
    927 
    928 	ch->ch_mode1 = mode1;
    929 	ch->ch_mode2 = mode2;
    930 
    931 	/*
    932 	 * If we're not in a mode that assumes a connection is present, then
    933 	 * ignore carrier changes.
    934 	 */
    935 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
    936 		ch->ch_i_dcd = 0;
    937 	else
    938 		ch->ch_i_dcd = ch->ch_i_dcd_pin;
    939 	/*
    940 	 * Set the flow control pins depending on the current flow control
    941 	 * mode.
    942 	 */
    943 	if (ISSET(t->c_cflag, CRTSCTS)) {
    944 		ch->ch_o_dtr = ch->ch_o_dtr_pin;
    945 		ch->ch_o_rts = ch->ch_o_rts_pin;
    946 		ch->ch_i_cts = ch->ch_i_cts_pin;
    947 		/* hw controle enable bits in mod regs set by cflag2modes */
    948 	} else if (ISSET(t->c_cflag, MDMBUF)) {
    949 		/*
    950 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
    951 		 * carrier detection.
    952 		 */
    953 		ch->ch_o_dtr = 0;
    954 		ch->ch_o_rts = ch->ch_o_dtr_pin;
    955 		ch->ch_i_cts = ch->ch_i_dcd_pin;
    956 	} else {
    957 		/*
    958 		 * If no flow control, then always set RTS.  This will make
    959 		 * the other side happy if it mistakenly thinks we're doing
    960 		 * RTS/CTS flow control.
    961 		 */
    962 		ch->ch_o_dtr = ch->ch_o_dtr_pin | ch->ch_o_rts_pin;
    963 		ch->ch_o_rts = 0;
    964 		ch->ch_i_cts = 0;
    965 		if (ISSET(ch->ch_oports, ch->ch_o_dtr_pin))
    966 			SET(ch->ch_oports, ch->ch_o_rts_pin);
    967 		else
    968 			CLR(ch->ch_oports, ch->ch_o_rts_pin);
    969 	}
    970 	/* XXX maybe mask the ports which generate intrs? */
    971 
    972 	ch->ch_brc = brc;
    973 
    974 	/* XXX maybe set fifo-full receive mode if RTSCTS and high speed? */
    975 
    976 	/* And copy to tty. */
    977 	tp->t_ispeed = 0;
    978 	tp->t_ospeed = t->c_ospeed;
    979 	tp->t_cflag = t->c_cflag;
    980 
    981 	if (!ch->ch_heldchange) {
    982 		if (ch->ch_tx_busy) {
    983 			ch->ch_heldtbc = ch->ch_tbc;
    984 			ch->ch_tbc = 0;
    985 			ch->ch_heldchange = 1;
    986 		} else
    987 			sbjcn_loadchannelregs(ch);
    988 	}
    989 
    990 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    991 		/* Disable the high water mark. */
    992 		ch->ch_r_hiwat = 0;
    993 		ch->ch_r_lowat = 0;
    994 		if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
    995 			CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
    996 			sbjcn_schedrx(ch);
    997 		}
    998 		if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
    999 			CLR(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1000 			sbjcn_dohwiflow(ch);
   1001 		}
   1002 	} else {
   1003 		ch->ch_r_hiwat = sbjcn_rbuf_hiwat;
   1004 		ch->ch_r_lowat = sbjcn_rbuf_lowat;
   1005 	}
   1006 
   1007 	splx(s);
   1008 
   1009 	/*
   1010 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1011 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1012 	 * explicit request.
   1013 	 */
   1014 	(void) (*tp->t_linesw->l_modem)(tp,
   1015 	    ISSET(ch->ch_iports, ch->ch_i_dcd));
   1016 
   1017 #ifdef SBJCN_DEBUG
   1018 	if (sbjcn_debug)
   1019 		sbjcn_status(ch, "sbjcnparam ");
   1020 #endif
   1021 
   1022 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1023 		if (ch->ch_tx_stopped) {
   1024 			ch->ch_tx_stopped = 0;
   1025 			sbjcn_start(tp);
   1026 		}
   1027 	}
   1028 
   1029 	return (0);
   1030 }
   1031 
   1032 void
   1033 sbjcn_iflush(struct sbjcn_channel *ch)
   1034 {
   1035 	uint64_t reg;
   1036 	int timo;
   1037 
   1038 	timo = 50000;
   1039 	/* flush any pending I/O */
   1040 	while (((reg = READ_REG(ch->ch_input_reg)) != 0) && --timo)
   1041 		;
   1042 
   1043 #ifdef DIAGNOSTIC
   1044 	if (!timo)
   1045 		printf("%s: sbjcn_iflush timeout %02x\n",
   1046 		    ch->ch_sc->sc_dev.dv_xname, reg);
   1047 #endif
   1048 }
   1049 
   1050 void
   1051 sbjcn_loadchannelregs(struct sbjcn_channel *ch)
   1052 {
   1053 }
   1054 
   1055 int
   1056 sbjcn_hwiflow(struct tty *tp, int block)
   1057 {
   1058 	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(tp->t_dev));
   1059 	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
   1060 	int s;
   1061 
   1062 	if (ch->ch_o_rts == 0)
   1063 		return (0);
   1064 
   1065 	s = splserial();
   1066 	if (block) {
   1067 		if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
   1068 			SET(ch->ch_rx_flags, RX_TTY_BLOCKED);
   1069 			sbjcn_dohwiflow(ch);
   1070 		}
   1071 	} else {
   1072 		if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
   1073 			CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
   1074 			sbjcn_schedrx(ch);
   1075 		}
   1076 		if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
   1077 			CLR(ch->ch_rx_flags, RX_TTY_BLOCKED);
   1078 			sbjcn_dohwiflow(ch);
   1079 		}
   1080 	}
   1081 	splx(s);
   1082 	return (1);
   1083 }
   1084 
   1085 /*
   1086  * (un)block input via hw flowcontrol
   1087  */
   1088 void
   1089 sbjcn_dohwiflow(struct sbjcn_channel *ch)
   1090 {
   1091 
   1092 	if (ch->ch_o_rts == 0)
   1093 		return;
   1094 
   1095 	if (ISSET(ch->ch_rx_flags, RX_ANY_BLOCK)) {
   1096 		CLR(ch->ch_oports, ch->ch_o_rts);
   1097 		CLR(ch->ch_oports_active, ch->ch_o_rts);
   1098 	} else {
   1099 		SET(ch->ch_oports, ch->ch_o_rts);
   1100 		SET(ch->ch_oports_active, ch->ch_o_rts);
   1101 	}
   1102 }
   1103 
   1104 void
   1105 sbjcn_start(struct tty *tp)
   1106 {
   1107 	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(tp->t_dev));
   1108 	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
   1109 	int s;
   1110 
   1111 	s = spltty();
   1112 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1113 		goto out;
   1114 	if (ch->ch_tx_stopped)
   1115 		goto out;
   1116 	if (!ttypull(tp))
   1117 		goto out;
   1118 
   1119 	/* Grab the first contiguous region of buffer space. */
   1120 	{
   1121 		u_char *tba;
   1122 		int tbc;
   1123 
   1124 		tba = tp->t_outq.c_cf;
   1125 		tbc = ndqb(&tp->t_outq, 0);
   1126 
   1127 		(void)splserial();
   1128 
   1129 		ch->ch_tba = tba;
   1130 		ch->ch_tbc = tbc;
   1131 	}
   1132 
   1133 	SET(tp->t_state, TS_BUSY);
   1134 	ch->ch_tx_busy = 1;
   1135 
   1136 	/* Output the first chunk of the contiguous buffer. */
   1137 	{
   1138 		while (ch->ch_tbc) {
   1139 			uint64_t data;
   1140 			int bytes, i;
   1141 
   1142 			bytes = (ch->ch_tbc > 7) ? 7 : ch->ch_tbc;
   1143 			data = bytes;
   1144 			for (i=0; i<bytes; i++) {
   1145 				data <<= 8;
   1146 				data |= *ch->ch_tba++;
   1147 			}
   1148 			if (bytes < 7)
   1149 				data <<= 56-(bytes<<3);
   1150 			ch->ch_tbc -= bytes;
   1151 			WRITE_REG(ch->ch_output_reg, data);
   1152 		}
   1153 		ch->ch_tx_busy = 0;
   1154 		ch->ch_tx_done = 1;
   1155 	}
   1156 out:
   1157 	splx(s);
   1158 	return;
   1159 }
   1160 
   1161 /*
   1162  * Stop output on a line.
   1163  */
   1164 void
   1165 sbjcnstop(struct tty *tp, int flag)
   1166 {
   1167 	struct sbjcn_softc *sc = device_lookup_private(&sbjcn_cd, SBJCN_UNIT(tp->t_dev));
   1168 	struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
   1169 	int s;
   1170 
   1171 	s = splserial();
   1172 	if (ISSET(tp->t_state, TS_BUSY)) {
   1173 		/* Stop transmitting at the next chunk. */
   1174 		ch->ch_tbc = 0;
   1175 		ch->ch_heldtbc = 0;
   1176 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1177 			SET(tp->t_state, TS_FLUSH);
   1178 	}
   1179 	splx(s);
   1180 }
   1181 
   1182 void
   1183 sbjcn_diag(void *arg)
   1184 {
   1185 	struct sbjcn_channel *ch = arg;
   1186 	struct sbjcn_softc *sc = ch->ch_sc;
   1187 	int overflows, floods;
   1188 	int s;
   1189 
   1190 	s = splserial();
   1191 	overflows = ch->ch_overflows;
   1192 	ch->ch_overflows = 0;
   1193 	floods = ch->ch_floods;
   1194 	ch->ch_floods = 0;
   1195 	ch->ch_errors = 0;
   1196 	splx(s);
   1197 
   1198 	log(LOG_WARNING, "%s: channel %d: %d fifo overflow%s, %d ibuf flood%s\n",
   1199 	    sc->sc_dev.dv_xname, ch->ch_num,
   1200 	    overflows, overflows == 1 ? "" : "s",
   1201 	    floods, floods == 1 ? "" : "s");
   1202 }
   1203 
   1204 integrate void
   1205 sbjcn_rxsoft(struct sbjcn_channel *ch, struct tty *tp)
   1206 {
   1207 	int (*rint)(int c, struct tty *tp) = tp->t_linesw->l_rint;
   1208 	u_char *get, *end;
   1209 	u_int cc, scc;
   1210 	u_char sr;
   1211 	int code;
   1212 	int s;
   1213 
   1214 	end = ch->ch_ebuf;
   1215 	get = ch->ch_rbget;
   1216 	scc = cc = sbjcn_rbuf_size - ch->ch_rbavail;
   1217 
   1218 	if (cc == sbjcn_rbuf_size) {
   1219 		ch->ch_floods++;
   1220 		if (ch->ch_errors++ == 0)
   1221 			callout_reset(&ch->ch_diag_callout, 60 * hz,
   1222 			    sbjcn_diag, ch);
   1223 	}
   1224 
   1225 	while (cc) {
   1226 		code = get[0];
   1227 		sr = get[1];
   1228 		if (ISSET(sr, 0xf0)) {
   1229 			if (ISSET(sr, 0x10)) {
   1230 				ch->ch_overflows++;
   1231 				if (ch->ch_errors++ == 0)
   1232 					callout_reset(&ch->ch_diag_callout,
   1233 					    60 * hz, sbjcn_diag, ch);
   1234 			}
   1235 			if (ISSET(sr, 0xc0))
   1236 				SET(code, TTY_FE);
   1237 			if (ISSET(sr, 0x20))
   1238 				SET(code, TTY_PE);
   1239 		}
   1240 		if ((*rint)(code, tp) == -1) {
   1241 			/*
   1242 			 * The line discipline's buffer is out of space.
   1243 			 */
   1244 			if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
   1245 				/*
   1246 				 * We're either not using flow control, or the
   1247 				 * line discipline didn't tell us to block for
   1248 				 * some reason.  Either way, we have no way to
   1249 				 * know when there's more space available, so
   1250 				 * just drop the rest of the data.
   1251 				 */
   1252 				get += cc << 1;
   1253 				if (get >= end)
   1254 					get -= sbjcn_rbuf_size << 1;
   1255 				cc = 0;
   1256 			} else {
   1257 				/*
   1258 				 * Don't schedule any more receive processing
   1259 				 * until the line discipline tells us there's
   1260 				 * space available (through comhwiflow()).
   1261 				 * Leave the rest of the data in the input
   1262 				 * buffer.
   1263 				 */
   1264 				SET(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
   1265 			}
   1266 			break;
   1267 		}
   1268 		get += 2;
   1269 		if (get >= end)
   1270 			get = ch->ch_rbuf;
   1271 		cc--;
   1272 	}
   1273 
   1274 	if (cc != scc) {
   1275 		ch->ch_rbget = get;
   1276 		s = splserial();
   1277 		cc = ch->ch_rbavail += scc - cc;
   1278 		/* Buffers should be ok again, release possible block. */
   1279 		if (cc >= ch->ch_r_lowat) {
   1280 			if (ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
   1281 				CLR(ch->ch_rx_flags, RX_IBUF_OVERFLOWED);
   1282 				SET(ch->ch_imr, 0x02);
   1283 			}
   1284 			if (ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED)) {
   1285 				CLR(ch->ch_rx_flags, RX_IBUF_BLOCKED);
   1286 				sbjcn_dohwiflow(ch);
   1287 			}
   1288 		}
   1289 		splx(s);
   1290 	}
   1291 }
   1292 
   1293 integrate void
   1294 sbjcn_txsoft(struct sbjcn_channel *ch, struct tty *tp)
   1295 {
   1296 
   1297 	CLR(tp->t_state, TS_BUSY);
   1298 	if (ISSET(tp->t_state, TS_FLUSH))
   1299 		CLR(tp->t_state, TS_FLUSH);
   1300 	else
   1301 		ndflush(&tp->t_outq, (int)(ch->ch_tba - tp->t_outq.c_cf));
   1302 	(*tp->t_linesw->l_start)(tp);
   1303 }
   1304 
   1305 integrate void
   1306 sbjcn_stsoft(struct sbjcn_channel *ch, struct tty *tp)
   1307 {
   1308 	u_char iports, delta;
   1309 	int s;
   1310 
   1311 	s = splserial();
   1312 	iports = ch->ch_iports;
   1313 	delta = ch->ch_iports_delta;
   1314 	ch->ch_iports_delta = 0;
   1315 	splx(s);
   1316 
   1317 	if (ISSET(delta, ch->ch_i_dcd)) {
   1318 		/*
   1319 		 * Inform the tty layer that carrier detect changed.
   1320 		 */
   1321 		(void) (*tp->t_linesw->l_modem)(tp,
   1322 		    ISSET(iports, ch->ch_i_dcd));
   1323 	}
   1324 
   1325 	if (ISSET(delta, ch->ch_i_cts)) {
   1326 		/* Block or unblock output according to flow control. */
   1327 		if (ISSET(iports, ch->ch_i_cts)) {
   1328 			ch->ch_tx_stopped = 0;
   1329 			(*tp->t_linesw->l_start)(tp);
   1330 		} else {
   1331 			ch->ch_tx_stopped = 1;
   1332 		}
   1333 	}
   1334 
   1335 #ifdef SBJCN_DEBUG
   1336 	if (sbjcn_debug)
   1337 		sbjcn_status(ch, "sbjcn_stsoft");
   1338 #endif
   1339 }
   1340 
   1341 integrate void
   1342 sbjcn_recv(struct sbjcn_channel *ch)
   1343 {
   1344 	u_char *put, *end;
   1345 	u_int cc;
   1346 
   1347 	end = ch->ch_ebuf;
   1348 	put = ch->ch_rbput;
   1349 	cc = ch->ch_rbavail;
   1350 
   1351 	/* XXX process break */
   1352 
   1353 	sbjcncn_grabdword(ch);
   1354 	if (ch->ch_waiting_input) {
   1355 		if (!ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
   1356 			while (cc > 0) {
   1357 				put[0] = sbjcncn_nextbyte(ch);
   1358 				put[1] = 1; /* XXXKW ? */
   1359 				put += 2;
   1360 				if (put >= end)
   1361 					put = ch->ch_rbuf;
   1362 				cc--;
   1363 
   1364 				if (!ch->ch_waiting_input)
   1365 					break;
   1366 			}
   1367 
   1368 			/*
   1369 			 * Current string of incoming characters ended
   1370 			 * because no more data was available or we
   1371 			 * ran out of space.  Schedule a receive event
   1372 			 * if any data was received.  If we're out of
   1373 			 * space, turn off receive interrupts.
   1374 			 */
   1375 			ch->ch_rbput = put;
   1376 			ch->ch_rbavail = cc;
   1377 			if (!ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED))
   1378 				ch->ch_rx_ready = 1;
   1379 
   1380 			/*
   1381 			 * See if we are in danger of overflowing a
   1382 			 * buffer. If so, use hardware flow control
   1383 			 * to ease the pressure.
   1384 			 */
   1385 			if (!ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED) &&
   1386 			    cc < ch->ch_r_hiwat) {
   1387 				SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
   1388 				sbjcn_dohwiflow(ch);
   1389 			}
   1390 
   1391 			/*
   1392 			 * If we're out of space, disable receive
   1393 			 * interrupts until the queue has drained
   1394 			 * a bit.
   1395 			 */
   1396 			if (!cc) {
   1397 				SET(ch->ch_rx_flags,
   1398 				RX_IBUF_OVERFLOWED);
   1399 				CLR(ch->ch_imr, 0x02);
   1400 			}
   1401 		} else {
   1402 			/* XXX panic? */
   1403 			CLR(ch->ch_imr, 0x02);
   1404 			//			continue;
   1405 		}
   1406 	}
   1407 
   1408 	/*
   1409 	 * If we've delayed a parameter change, do it now, and restart
   1410 	 * output.
   1411 	 */
   1412 	if (ch->ch_heldchange) {
   1413 		sbjcn_loadchannelregs(ch);
   1414 		ch->ch_heldchange = 0;
   1415 		ch->ch_tbc = ch->ch_heldtbc;
   1416 		ch->ch_heldtbc = 0;
   1417 	}
   1418 }
   1419 
   1420 void
   1421 sbjcn_callout(void *arg)
   1422 {
   1423 	struct sbjcn_channel *ch = arg;
   1424 	struct tty *tp = ch->ch_tty;
   1425 
   1426 	/* XXX get stuff */
   1427 	sbjcn_recv(ch);
   1428 
   1429 	/* XXX check receive */
   1430 	if (ch->ch_rx_ready) {
   1431 		ch->ch_rx_ready = 0;
   1432 		sbjcn_rxsoft(ch, tp);
   1433 	}
   1434 
   1435 	/* XXX check transmit */
   1436 	if (ch->ch_tx_done) {
   1437 		ch->ch_tx_done = 0;
   1438 		sbjcn_txsoft(ch, tp);
   1439 	}
   1440 
   1441 	callout_reset(&ch->ch_callout, hz/10, sbjcn_callout, ch);
   1442 }
   1443 
   1444 static char sbjcncn_nextbyte(struct sbjcn_channel *ch)
   1445 {
   1446 	char c;
   1447 
   1448 	sbjcncn_grabdword(ch);
   1449 	c = (ch->ch_input_buf >> 56) & 0xff;
   1450 	ch->ch_input_buf <<= 8;
   1451 	ch->ch_waiting_input--;
   1452 	return c;
   1453 }
   1454 
   1455 static void sbjcncn_grabdword(struct sbjcn_channel *ch)
   1456 {
   1457 	uint64_t inbuf;
   1458 
   1459 	if (ch->ch_waiting_input)
   1460 		return;
   1461 
   1462 	inbuf = READ_REG(ch->ch_input_reg);
   1463 	ch->ch_waiting_input = jtag_input_len(inbuf);
   1464 	ch->ch_input_buf = inbuf << 8;
   1465 }
   1466 
   1467 /*
   1468  * Initialize UART for use as console or KGDB line.
   1469  */
   1470 int
   1471 sbjcn_init(u_long addr, int chan, int rate, tcflag_t cflag)
   1472 {
   1473 	/* XXXKW Anything to do here? */
   1474 	return (0);
   1475 }
   1476 
   1477 /*
   1478  * Following are all routines needed for sbjcn to act as console
   1479  */
   1480 int
   1481 sbjcn_cnattach(u_long addr, int chan, int rate, tcflag_t cflag)
   1482 {
   1483 	int res;
   1484 	uint64_t ctrl_val;
   1485 	static struct consdev sbjcn_cons = {
   1486 		NULL, NULL, sbjcn_cngetc, sbjcn_cnputc, sbjcn_cnpollc, NULL,
   1487 		    NODEV, CN_NORMAL
   1488 	};
   1489 
   1490 	res = sbjcn_init(addr, chan, rate, cflag);
   1491 	if (res)
   1492 		return (res);
   1493 
   1494 	cn_tab = &sbjcn_cons;
   1495 
   1496 	sbjcn_cons_present = 1;
   1497 	sbjcn_cons_addr = addr;
   1498 	sbjcn_cons_waiting_input = 0;
   1499 	sbjcn_cons_chan = chan;
   1500 	sbjcn_cons_rate = rate;
   1501 	sbjcn_cons_cflag = cflag;
   1502 
   1503 	/* Wait for sign of life from the other end */
   1504 	while ((ctrl_val = READ_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_CONTROL))) == 0)
   1505 		;
   1506 
   1507 	return (ctrl_val != JTAG_CONS_MAGICNUM);
   1508 }
   1509 
   1510 int
   1511 sbjcn_cngetc(dev_t dev)
   1512 {
   1513 	char c;
   1514 
   1515 	while (sbjcn_cons_waiting_input == 0)
   1516 		sbjcn_cngrabdword();
   1517 
   1518 	c = (sbjcn_cons_input_buf >> 56) & 0xff;
   1519 	sbjcn_cons_input_buf <<= 8;
   1520 	sbjcn_cons_waiting_input--;
   1521 
   1522 	return c;
   1523 }
   1524 
   1525 /*
   1526  * Console kernel output character routine.
   1527  */
   1528 void
   1529 sbjcn_cnputc(dev_t dev, int c)
   1530 {
   1531 	uint64_t outbuf;
   1532 
   1533 	outbuf = (1LL << 56) | (((uint64_t)c) << 48);
   1534 	WRITE_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_OUTPUT), outbuf);
   1535 }
   1536 
   1537 void
   1538 sbjcn_cnpollc(dev_t dev, int on)
   1539 {
   1540 
   1541 }
   1542 
   1543 static void sbjcn_cngrabdword(void)
   1544 {
   1545 	uint64_t inbuf;
   1546 
   1547 	if (sbjcn_cons_waiting_input)
   1548 		return;
   1549 
   1550 	inbuf = READ_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_INPUT));
   1551 	sbjcn_cons_waiting_input = jtag_input_len(inbuf);
   1552 	sbjcn_cons_input_buf = inbuf << 8;
   1553 }
   1554 
   1555 #ifdef KGDB
   1556 int
   1557 sbjcn_kgdb_attach(u_long addr, int chan, int rate, tcflag_t cflag)
   1558 {
   1559 	int res;
   1560 
   1561 	if (!sbjcn_cons_present &&
   1562 	    sbjcn_cons_addr == addr && sbjcn_cons_chan == chan)
   1563 		return (EBUSY); /* cannot share with console */
   1564 
   1565 	res = sbjcn_init(addr, chan, rate, cflag);
   1566 	if (res)
   1567 		return (res);
   1568 
   1569 	kgdb_attach(sbjcn_kgdb_getc, sbjcn_kgdb_putc, NULL);
   1570 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   1571 	kgdb_rate = rate;
   1572 
   1573 	sbjcn_kgdb_present = 1;
   1574 	/* XXX sbjcn_init wants addr, but we need the offset addr */
   1575 	sbjcn_kgdb_addr = addr + (chan * 0x100);
   1576 	sbjcn_kgdb_chan = chan;
   1577 
   1578 	return (0);
   1579 }
   1580 
   1581 /* ARGSUSED */
   1582 int
   1583 sbjcn_kgdb_getc(void *arg)
   1584 {
   1585 
   1586 	return (sbjcn_common_getc(sbjcn_kgdb_addr, sbjcn_kgdb_chan));
   1587 }
   1588 
   1589 /* ARGSUSED */
   1590 void
   1591 sbjcn_kgdb_putc(void *arg, int c)
   1592 {
   1593 
   1594 	sbjcn_common_putc(sbjcn_kgdb_addr, sbjcn_kgdb_chan, c);
   1595 }
   1596 #endif /* KGDB */
   1597 
   1598 /*
   1599  * helper function to identify the sbjcn channels used by
   1600  * console or KGDB (and not yet autoconf attached)
   1601  */
   1602 int
   1603 sbjcn_is_console(u_long addr, int chan)
   1604 {
   1605 
   1606 	if (sbjcn_cons_present && !sbjcn_cons_attached &&
   1607 	    sbjcn_cons_addr == addr && sbjcn_cons_chan == chan)
   1608 		return (1);
   1609 #ifdef KGDB
   1610 	if (sbjcn_kgdb_present && !sbjcn_kgdb_attached &&
   1611 	    sbjcn_kgdb_addr == addr && sbjcn_kgdb_chan == chan)
   1612 		return (1);
   1613 #endif
   1614 	return (0);
   1615 }
   1616