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