Home | History | Annotate | Line # | Download | only in dev
sbscn.c revision 1.22.8.1
      1 /* $NetBSD: sbscn.c,v 1.22.8.1 2007/11/21 21:53:23 joerg 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  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *        This product includes software developed by the NetBSD
     55  *        Foundation, Inc. and its contributors.
     56  * 4. Neither the name of The NetBSD Foundation nor the names of its
     57  *    contributors may be used to endorse or promote products derived
     58  *    from this software without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     61  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     62  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     63  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     64  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     65  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     66  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     67  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     68  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     69  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     70  * POSSIBILITY OF SUCH DAMAGE.
     71  */
     72 
     73 /*
     74  * Copyright (c) 1991 The Regents of the University of California.
     75  * All rights reserved.
     76  *
     77  * Redistribution and use in source and binary forms, with or without
     78  * modification, are permitted provided that the following conditions
     79  * are met:
     80  * 1. Redistributions of source code must retain the above copyright
     81  *    notice, this list of conditions and the following disclaimer.
     82  * 2. Redistributions in binary form must reproduce the above copyright
     83  *    notice, this list of conditions and the following disclaimer in the
     84  *    documentation and/or other materials provided with the distribution.
     85  * 3. Neither the name of the University nor the names of its contributors
     86  *    may be used to endorse or promote products derived from this software
     87  *    without specific prior written permission.
     88  *
     89  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     90  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     91  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     92  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     93  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     94  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     95  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     96  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     97  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     98  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     99  * SUCH DAMAGE.
    100  *
    101  *	@(#)com.c	7.5 (Berkeley) 5/16/91
    102  */
    103 
    104 /*
    105  * `sbscn' driver, supports SiByte SB-1250 SOC DUART.
    106  *
    107  * This DUART is quite similar in programming model to the scn2681/68681
    108  * DUARTs supported by the NetBSD/amiga `mfc' and NetBSD/pc532 `scn'
    109  * driver, but substantial rework would have been necessary to make
    110  * those drivers sane w.r.t. bus_space (which would then have been
    111  * required on NetBSD/sbmips very early on), and to accommodate the
    112  * different register mappings.
    113  *
    114  * So, another driver.  Eventually there should be One True Driver,
    115  * but we're not here to save the world.
    116  */
    117 
    118 #include <sys/cdefs.h>
    119 __KERNEL_RCSID(0, "$NetBSD: sbscn.c,v 1.22.8.1 2007/11/21 21:53:23 joerg Exp $");
    120 
    121 #define	SBSCN_DEBUG
    122 
    123 #include "opt_ddb.h"
    124 
    125 #include "rnd.h"
    126 #if NRND > 0 && defined(RND_SBSCN)
    127 #include <sys/rnd.h>
    128 #endif
    129 
    130 #include <sys/param.h>
    131 #include <sys/systm.h>
    132 #include <sys/ioctl.h>
    133 #include <sys/select.h>
    134 #include <sys/tty.h>
    135 #include <sys/proc.h>
    136 #include <sys/user.h>
    137 #include <sys/conf.h>
    138 #include <sys/file.h>
    139 #include <sys/uio.h>
    140 #include <sys/kernel.h>
    141 #include <sys/syslog.h>
    142 #include <sys/types.h>
    143 #include <sys/device.h>
    144 #include <sys/malloc.h>
    145 #include <sys/vnode.h>
    146 #include <sys/kauth.h>
    147 
    148 #include <mips/sibyte/dev/sbobiovar.h>
    149 #if 0
    150 #include <mips/sibyte/dev/sbscnreg.h>
    151 #endif
    152 #include <mips/sibyte/dev/sbscnvar.h>
    153 #include <dev/cons.h>
    154 #include <machine/locore.h>
    155 
    156 void	sbscn_attach_channel(struct sbscn_softc *sc, int chan, int intr);
    157 #if defined(DDB) || defined(KGDB)
    158 static void sbscn_enable_debugport(struct sbscn_channel *ch);
    159 #endif
    160 void	sbscn_config(struct sbscn_channel *ch);
    161 void	sbscn_shutdown(struct sbscn_channel *ch);
    162 int	sbscn_speed(long, long *);
    163 static int cflag2modes(tcflag_t, u_char *, u_char *);
    164 int	sbscn_param(struct tty *, struct termios *);
    165 void	sbscn_start(struct tty *);
    166 int	sbscn_hwiflow(struct tty *, int);
    167 
    168 void	sbscn_loadchannelregs(struct sbscn_channel *);
    169 void	sbscn_dohwiflow(struct sbscn_channel *);
    170 void	sbscn_break(struct sbscn_channel *, int);
    171 void	sbscn_modem(struct sbscn_channel *, int);
    172 void	tiocm_to_sbscn(struct sbscn_channel *, int, int);
    173 int	sbscn_to_tiocm(struct sbscn_channel *);
    174 void	sbscn_iflush(struct sbscn_channel *);
    175 
    176 int	sbscn_init(u_long addr, int chan, int rate, tcflag_t cflag);
    177 int	sbscn_common_getc(u_long addr, int chan);
    178 void	sbscn_common_putc(u_long addr, int chan, int c);
    179 void	sbscn_intr(void *arg, uint32_t status, uint32_t pc);
    180 
    181 int	sbscn_cngetc(dev_t dev);
    182 void	sbscn_cnputc(dev_t dev, int c);
    183 void	sbscn_cnpollc(dev_t dev, int on);
    184 
    185 extern struct cfdriver sbscn_cd;
    186 
    187 dev_type_open(sbscnopen);
    188 dev_type_close(sbscnclose);
    189 dev_type_read(sbscnread);
    190 dev_type_write(sbscnwrite);
    191 dev_type_ioctl(sbscnioctl);
    192 dev_type_stop(sbscnstop);
    193 dev_type_tty(sbscntty);
    194 dev_type_poll(sbscnpoll);
    195 
    196 const struct cdevsw sbscn_cdevsw = {
    197 	sbscnopen, sbscnclose, sbscnread, sbscnwrite, sbscnioctl,
    198 	sbscnstop, sbscntty, sbscnpoll, nommap, ttykqfilter, D_TTY
    199 };
    200 
    201 #define	integrate	static inline
    202 void 	sbscn_soft(void *);
    203 integrate void sbscn_rxsoft(struct sbscn_channel *, struct tty *);
    204 integrate void sbscn_txsoft(struct sbscn_channel *, struct tty *);
    205 integrate void sbscn_stsoft(struct sbscn_channel *, struct tty *);
    206 integrate void sbscn_schedrx(struct sbscn_channel *);
    207 void	sbscn_diag(void *);
    208 
    209 /*
    210  * Make this an option variable one can patch.
    211  * But be warned:  this must be a power of 2!
    212  */
    213 u_int sbscn_rbuf_size = SBSCN_RING_SIZE;
    214 
    215 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    216 u_int sbscn_rbuf_hiwat = (SBSCN_RING_SIZE * 1) / 4;
    217 u_int sbscn_rbuf_lowat = (SBSCN_RING_SIZE * 3) / 4;
    218 
    219 static int	sbscn_cons_present;
    220 static int	sbscn_cons_attached;
    221 static u_long	sbscn_cons_addr;
    222 static int	sbscn_cons_chan;
    223 static int	sbscn_cons_rate;
    224 static tcflag_t	sbscn_cons_cflag;
    225 
    226 #ifdef KGDB
    227 #include <sys/kgdb.h>
    228 
    229 static int	sbscn_kgdb_present;
    230 static int	sbscn_kgdb_attached;
    231 static u_long	sbscn_kgdb_addr;
    232 static int	sbscn_kgdb_chan;
    233 
    234 int	sbscn_kgdb_getc(void *);
    235 void	sbscn_kgdb_putc(void *, int);
    236 #endif /* KGDB */
    237 
    238 static int	sbscn_match(struct device *, struct cfdata *, void *);
    239 static void	sbscn_attach(struct device *, struct device *, void *);
    240 
    241 CFATTACH_DECL(sbscn, sizeof(struct sbscn_softc),
    242     sbscn_match, sbscn_attach, NULL, NULL);
    243 
    244 #define	READ_REG(rp)		(mips3_ld((int64_t *)__UNVOLATILE(rp)))
    245 #define	WRITE_REG(rp, val)	(mips3_sd((uint64_t *)__UNVOLATILE(rp), (val)))
    246 
    247 /*
    248  * input and output signals are actually the _inverse_ of the bits in the
    249  * input and output port registers!
    250  */
    251 #define	GET_INPUT_SIGNALS(ch) \
    252     ((~READ_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x280))) & (ch)->ch_i_mask)
    253 #define	SET_OUTPUT_SIGNALS(ch, val) 					\
    254     do {								\
    255 	int sigs_to_set, sigs_to_clr;					\
    256 									\
    257 	sigs_to_set = (ch)->ch_o_mask & val;				\
    258 	sigs_to_clr = (ch)->ch_o_mask & ~val;				\
    259 									\
    260 	/* set signals by clearing op bits, and vice versa */		\
    261 	WRITE_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x2c0),	\
    262 	    sigs_to_set);						\
    263 	WRITE_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x2b0),	\
    264 	    sigs_to_clr);						\
    265     } while (0)
    266 
    267 static int
    268 sbscn_match(struct device *parent, struct cfdata *match, void *aux)
    269 {
    270 	struct sbobio_attach_args *sap = aux;
    271 
    272 	if (sap->sa_locs.sa_type != SBOBIO_DEVTYPE_DUART)
    273 		return (0);
    274 
    275 	return 1;
    276 }
    277 
    278 static void
    279 sbscn_attach(struct device *parent, struct device *self, void *aux)
    280 {
    281 	struct sbscn_softc *sc = (struct sbscn_softc *)self;
    282 	struct sbobio_attach_args *sap = aux;
    283 	int i;
    284 
    285 	sc->sc_addr = sap->sa_base + sap->sa_locs.sa_offset;
    286 
    287 	printf("\n");
    288 	for (i = 0; i < 2; i++)
    289 		sbscn_attach_channel(sc, i, sap->sa_locs.sa_intr[i]);
    290 
    291 	/* init duart_opcr */
    292 	WRITE_REG(MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x270), 0);
    293 	/* init duart_aux_ctrl */
    294 	WRITE_REG(MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x210), 0x0f); /* XXX */
    295 }
    296 
    297 void
    298 sbscn_attach_channel(struct sbscn_softc *sc, int chan, int intr)
    299 {
    300 	struct sbscn_channel *ch = &sc->sc_channels[chan];
    301 	u_long chan_addr;
    302 	struct tty *tp;
    303 
    304 	ch->ch_sc = sc;
    305 	ch->ch_num = chan;
    306 
    307 	chan_addr = sc->sc_addr + (0x100 * chan);
    308 	ch->ch_base = (void *)MIPS_PHYS_TO_KSEG1(chan_addr);
    309 	ch->ch_isr_base =
    310 	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x220 + (0x20 * chan));
    311 	ch->ch_imr_base =
    312 	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x230 + (0x20 * chan));
    313 #ifdef XXXCGDnotyet
    314 	ch->ch_inchg_base =
    315 	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x2d0 + (0x10 * chan));
    316 #endif
    317 
    318 	ch->ch_i_dcd = ch->ch_i_dcd_pin = 0 /* XXXCGD */;
    319 	ch->ch_i_cts = ch->ch_i_cts_pin = 0 /* XXXCGD */;
    320 	ch->ch_i_dsr = ch->ch_i_dsr_pin = 0 /* XXXCGD */;
    321 	ch->ch_i_ri = ch->ch_i_ri_pin = 0 /* XXXCGD */;
    322 	ch->ch_i_mask =
    323 	    ch->ch_i_dcd | ch->ch_i_cts | ch->ch_i_dsr | ch->ch_i_ri;
    324 	ch->ch_o_dtr = ch->ch_o_dtr_pin = 0 /* XXXCGD */;
    325 	ch->ch_o_rts = ch->ch_o_rts_pin = 0 /* XXXCGD */;
    326 	ch->ch_o_mask = ch->ch_o_dtr | ch->ch_o_rts;
    327 
    328 	ch->ch_intrhand = cpu_intr_establish(intr, IPL_SERIAL, sbscn_intr, ch);
    329 	callout_init(&ch->ch_diag_callout, 0);
    330 
    331 	/* Disable interrupts before configuring the device. */
    332 	ch->ch_imr = 0;
    333 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
    334 
    335 	if (sbscn_cons_present &&
    336 	    sbscn_cons_addr == chan_addr && sbscn_cons_chan == chan) {
    337 		sbscn_cons_attached = 1;
    338 
    339 		/* Make sure the console is always "hardwired". */
    340 		delay(1000);			/* wait for output to finish */
    341 		SET(ch->ch_hwflags, SBSCN_HW_CONSOLE);
    342 		SET(ch->ch_swflags, TIOCFLAG_SOFTCAR);
    343 	}
    344 
    345 	tp = ttymalloc();
    346 	tp->t_oproc = sbscn_start;
    347 	tp->t_param = sbscn_param;
    348 	tp->t_hwiflow = sbscn_hwiflow;
    349 
    350 	ch->ch_tty = tp;
    351 	ch->ch_rbuf = malloc(sbscn_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
    352 	if (ch->ch_rbuf == NULL) {
    353 		printf("%s: channel %d: unable to allocate ring buffer\n",
    354 		    sc->sc_dev.dv_xname, chan);
    355 		return;
    356 	}
    357 	ch->ch_ebuf = ch->ch_rbuf + (sbscn_rbuf_size << 1);
    358 
    359 	tty_attach(tp);
    360 
    361 	if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
    362 		int maj;
    363 
    364 		/* locate the major number */
    365 		maj = cdevsw_lookup_major(&sbscn_cdevsw);
    366 
    367 		cn_tab->cn_dev = makedev(maj,
    368 		    (device_unit(&sc->sc_dev) << 1) + chan);
    369 
    370 		printf("%s: channel %d: console\n", sc->sc_dev.dv_xname, chan);
    371 	}
    372 
    373 #ifdef KGDB
    374 	/*
    375 	 * Allow kgdb to "take over" this port.  If this is
    376 	 * the kgdb device, it has exclusive use.
    377 	 */
    378 	if (sbscn_kgdb_present &&
    379 	    sbscn_kgdb_addr == chan_addr && sbscn_kgdb_chan == chan) {
    380 		sbscn_kgdb_attached = 1;
    381 
    382 		SET(sc->sc_hwflags, SBSCN_HW_KGDB);
    383 		printf("%s: channel %d: kgdb\n", sc->sc_dev.dv_xname, chan);
    384 	}
    385 #endif
    386 
    387 	ch->ch_si = softintr_establish(IPL_SOFTSERIAL, sbscn_soft, ch);
    388 
    389 #if NRND > 0 && defined(RND_SBSCN)
    390 	rnd_attach_source(&ch->ch_rnd_source, sc->sc_dev.dv_xname,
    391 			  RND_TYPE_TTY, 0);
    392 #endif
    393 
    394 	sbscn_config(ch);
    395 
    396 	SET(ch->ch_hwflags, SBSCN_HW_DEV_OK);
    397 }
    398 
    399 int
    400 sbscn_speed(long speed, long *brcp)
    401 {
    402 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    403 
    404 	int x, err;
    405 	int frequency = 100000000;
    406 
    407 	*brcp = divrnd(frequency / 20, speed) - 1;
    408 
    409 	if (speed <= 0)
    410 		return (-1);
    411 	x = divrnd(frequency / 20, speed);
    412 	if (x <= 0)
    413 		return (-1);
    414 	err = divrnd(((quad_t)frequency) * 1000 / 20, speed * x) - 1000;
    415 	if (err < 0)
    416 		err = -err;
    417 	if (err > SBSCN_TOLERANCE)
    418 		return (-1);
    419 	*brcp = x - 1;
    420 	return (0);
    421 
    422 #undef	divrnd
    423 }
    424 
    425 #ifdef SBSCN_DEBUG
    426 void	sbscn_status(struct sbscn_channel *, const char *);
    427 
    428 int	sbscn_debug = 0 /* XXXCGD */;
    429 
    430 void
    431 sbscn_status(struct sbscn_channel *ch, const char *str)
    432 {
    433 	struct sbscn_softc *sc = ch->ch_sc;
    434 	struct tty *tp = ch->ch_tty;
    435 
    436 	printf("%s: chan %d: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
    437 	    sc->sc_dev.dv_xname, ch->ch_num, str,
    438 	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
    439 	    ISSET(ch->ch_iports, ch->ch_i_dcd) ? "+" : "-",
    440 	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
    441 	    ISSET(ch->ch_oports, ch->ch_o_dtr) ? "+" : "-",
    442 	    ch->ch_tx_stopped ? "+" : "-");
    443 
    444 	printf("%s: chan %d: %s %scrtscts %scts %sts_ttstop  %srts %xrx_flags\n",
    445 	    sc->sc_dev.dv_xname, ch->ch_num, str,
    446 	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
    447 	    ISSET(ch->ch_iports, ch->ch_i_cts) ? "+" : "-",
    448 	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
    449 	    ISSET(ch->ch_oports, ch->ch_o_rts) ? "+" : "-",
    450 	    ch->ch_rx_flags);
    451 }
    452 #endif
    453 
    454 #if defined(DDB) || defined(KGDB)
    455 static void
    456 sbscn_enable_debugport(struct sbscn_channel *ch)
    457 {
    458 	int s;
    459 
    460 	/* Turn on line break interrupt, set carrier. */
    461 	s = splserial();
    462 
    463 #if 0	/* DO NOT turn on break interrupt at this time. */
    464 	ch->ch_imr = 0x04;
    465 #else
    466 	ch->ch_imr = 0x00;
    467 #endif
    468 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
    469 	SET(ch->ch_oports, ch->ch_o_dtr | ch->ch_o_rts);
    470 	SET_OUTPUT_SIGNALS(ch, ch->ch_oports);
    471 
    472 	splx(s);
    473 }
    474 #endif
    475 
    476 void
    477 sbscn_config(struct sbscn_channel *ch)
    478 {
    479 
    480 	/* Disable interrupts before configuring the device. */
    481 	ch->ch_imr = 0x00;
    482 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
    483 
    484 #ifdef DDB
    485 	if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE))
    486 		sbscn_enable_debugport(ch);
    487 #endif
    488 
    489 #ifdef KGDB
    490 	/*
    491 	 * Allow kgdb to "take over" this port.  If this is
    492 	 * the kgdb device, it has exclusive use.
    493 	 */
    494 	if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB))
    495 		sbscn_enable_debugport(ch);
    496 #endif
    497 }
    498 
    499 void
    500 sbscn_shutdown(struct sbscn_channel *ch)
    501 {
    502 	struct tty *tp = ch->ch_tty;
    503 	int s;
    504 
    505 	s = splserial();
    506 
    507 	/* If we were asserting flow control, then deassert it. */
    508 	SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
    509 	sbscn_dohwiflow(ch);
    510 
    511 	/* Clear any break condition set with TIOCSBRK. */
    512 	sbscn_break(ch, 0);
    513 
    514 	/*
    515 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    516 	 * notice even if we immediately open the port again.
    517 	 * Avoid tsleeping above splhigh().
    518 	 */
    519 	if (ISSET(tp->t_cflag, HUPCL)) {
    520 		sbscn_modem(ch, 0);
    521 		splx(s);
    522 		/* XXX tsleep will only timeout */
    523 		(void) tsleep(ch, TTIPRI, ttclos, hz);
    524 		s = splserial();
    525 	}
    526 
    527 	/* Turn off interrupts. */
    528 #ifdef DDB
    529 	if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE))
    530 #if 0	/* DO NOT turn on break interrupt at this time. */
    531 		ch->ch_imr = 0x04; /* interrupt on break */
    532 #else
    533 		ch->ch_imr = 0x00;
    534 #endif
    535 	else
    536 #endif
    537 		ch->ch_imr = 0;
    538 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
    539 
    540 	splx(s);
    541 }
    542 
    543 int
    544 sbscnopen(dev_t dev, int flag, int mode, struct lwp *l)
    545 {
    546 	int unit = SBSCN_UNIT(dev);
    547 	int chan = SBSCN_CHAN(dev);
    548 	struct sbscn_softc *sc;
    549 	struct sbscn_channel *ch;
    550 	struct tty *tp;
    551 	int s, s2;
    552 	int error;
    553 
    554 	if (unit >= sbscn_cd.cd_ndevs)
    555 		return (ENXIO);
    556 	sc = sbscn_cd.cd_devs[unit];
    557 	if (sc == 0)
    558 		return (ENXIO);
    559 	ch = &sc->sc_channels[chan];
    560 	if (!ISSET(ch->ch_hwflags, SBSCN_HW_DEV_OK) || ch->ch_rbuf == NULL)
    561 		return (ENXIO);
    562 
    563 #ifdef KGDB
    564 	/*
    565 	 * If this is the kgdb port, no other use is permitted.
    566 	 */
    567 	if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB))
    568 		return (EBUSY);
    569 #endif
    570 
    571 	tp = ch->ch_tty;
    572 
    573 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    574 		return (EBUSY);
    575 
    576 	s = spltty();
    577 
    578 	/*
    579 	 * Do the following iff this is a first open.
    580 	 */
    581 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    582 		struct termios t;
    583 
    584 		tp->t_dev = dev;
    585 
    586 		s2 = splserial();
    587 
    588 		/* Turn on receive, break, and status change interrupts. */
    589 #if 0	/* DO NOT turn on break or status change interrupt at this time. */
    590 		ch->ch_imr = 0xe;
    591 #else
    592 		ch->ch_imr = 0x2;
    593 #endif
    594 		WRITE_REG(ch->ch_imr_base, ch->ch_imr);
    595 
    596 		/* Fetch the current modem control status, needed later. */
    597 		ch->ch_iports = GET_INPUT_SIGNALS(ch);
    598 		ch->ch_iports_delta = 0;
    599 		splx(s2);
    600 
    601 		/*
    602 		 * Initialize the termios status to the defaults.  Add in the
    603 		 * sticky bits from TIOCSFLAGS.
    604 		 */
    605 		t.c_ispeed = 0;
    606 		if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
    607 			t.c_ospeed = sbscn_cons_rate;
    608 			t.c_cflag = sbscn_cons_cflag;
    609 		} else {
    610 			t.c_ospeed = TTYDEF_SPEED;
    611 			t.c_cflag = TTYDEF_CFLAG;
    612 		}
    613 		if (ISSET(ch->ch_swflags, TIOCFLAG_CLOCAL))
    614 			SET(t.c_cflag, CLOCAL);
    615 		if (ISSET(ch->ch_swflags, TIOCFLAG_CRTSCTS))
    616 			SET(t.c_cflag, CRTSCTS);
    617 		if (ISSET(ch->ch_swflags, TIOCFLAG_MDMBUF))
    618 			SET(t.c_cflag, MDMBUF);
    619 		/* Make sure sbscn_param() will do something. */
    620 		tp->t_ospeed = 0;
    621 		(void) sbscn_param(tp, &t);
    622 		tp->t_iflag = TTYDEF_IFLAG;
    623 		tp->t_oflag = TTYDEF_OFLAG;
    624 		tp->t_lflag = TTYDEF_LFLAG;
    625 		ttychars(tp);
    626 		ttsetwater(tp);
    627 
    628 		s2 = splserial();
    629 
    630 		/*
    631 		 * Turn on DTR.  We must always do this, even if carrier is not
    632 		 * present, because otherwise we'd have to use TIOCSDTR
    633 		 * immediately after setting CLOCAL, which applications do not
    634 		 * expect.  We always assert DTR while the device is open
    635 		 * unless explicitly requested to deassert it.
    636 		 */
    637 		sbscn_modem(ch, 1);
    638 
    639 		/* Clear the input ring, and unblock. */
    640 		ch->ch_rbput = ch->ch_rbget = ch->ch_rbuf;
    641 		ch->ch_rbavail = sbscn_rbuf_size;
    642 		sbscn_iflush(ch);
    643 		CLR(ch->ch_rx_flags, RX_ANY_BLOCK);
    644 		sbscn_dohwiflow(ch);
    645 
    646 #ifdef SBSCN_DEBUG
    647 		if (sbscn_debug)
    648 			sbscn_status(ch, "sbscnopen  ");
    649 #endif
    650 
    651 		splx(s2);
    652 	}
    653 
    654 	splx(s);
    655 
    656 	error = ttyopen(tp, SBSCN_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
    657 	if (error)
    658 		goto bad;
    659 
    660 	error = (*tp->t_linesw->l_open)(dev, tp);
    661 	if (error)
    662 		goto bad;
    663 
    664 	return (0);
    665 
    666 bad:
    667 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    668 		/*
    669 		 * We failed to open the device, and nobody else had it opened.
    670 		 * Clean up the state as appropriate.
    671 		 */
    672 		sbscn_shutdown(ch);
    673 	}
    674 
    675 	return (error);
    676 }
    677 
    678 int
    679 sbscnclose(dev_t dev, int flag, int mode, struct lwp *l)
    680 {
    681 	struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
    682 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
    683 	struct tty *tp = ch->ch_tty;
    684 
    685 	/* XXX This is for cons.c. */
    686 	if (!ISSET(tp->t_state, TS_ISOPEN))
    687 		return (0);
    688 
    689 	(*tp->t_linesw->l_close)(tp, flag);
    690 	ttyclose(tp);
    691 
    692 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    693 		/*
    694 		 * Although we got a last close, the device may still be in
    695 		 * use; e.g. if this was the dialout node, and there are still
    696 		 * processes waiting for carrier on the non-dialout node.
    697 		 */
    698 		sbscn_shutdown(ch);
    699 	}
    700 
    701 	return (0);
    702 }
    703 
    704 int
    705 sbscnread(dev_t dev, struct uio *uio, int flag)
    706 {
    707 	struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
    708 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
    709 	struct tty *tp = ch->ch_tty;
    710 
    711 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    712 }
    713 
    714 int
    715 sbscnwrite(dev_t dev, struct uio *uio, int flag)
    716 {
    717 	struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
    718 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
    719 	struct tty *tp = ch->ch_tty;
    720 
    721 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    722 }
    723 
    724 int
    725 sbscnpoll(dev_t dev, int events, struct lwp *l)
    726 {
    727 	struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
    728 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
    729 	struct tty *tp = ch->ch_tty;
    730 
    731 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    732 }
    733 
    734 struct tty *
    735 sbscntty(dev_t dev)
    736 {
    737 	struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
    738 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
    739 	struct tty *tp = ch->ch_tty;
    740 
    741 	return (tp);
    742 }
    743 
    744 int
    745 sbscnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    746 {
    747 	struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
    748 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
    749 	struct tty *tp = ch->ch_tty;
    750 	int error;
    751 	int s;
    752 
    753 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    754 	if (error != EPASSTHROUGH)
    755 		return (error);
    756 
    757 	error = ttioctl(tp, cmd, data, flag, l);
    758 	if (error != EPASSTHROUGH)
    759 		return (error);
    760 
    761 	error = 0;
    762 
    763 	s = splserial();
    764 
    765 	switch (cmd) {
    766 	case TIOCSBRK:
    767 		sbscn_break(ch, 1);
    768 		break;
    769 
    770 	case TIOCCBRK:
    771 		sbscn_break(ch, 0);
    772 		break;
    773 
    774 	case TIOCSDTR:
    775 		sbscn_modem(ch, 1);
    776 		break;
    777 
    778 	case TIOCCDTR:
    779 		sbscn_modem(ch, 0);
    780 		break;
    781 
    782 	case TIOCGFLAGS:
    783 		*(int *)data = ch->ch_swflags;
    784 		break;
    785 
    786 	case TIOCSFLAGS:
    787 		error = kauth_authorize_device_tty(l->l_cred,
    788 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    789 		if (error)
    790 			break;
    791 		ch->ch_swflags = *(int *)data;
    792 		break;
    793 
    794 	case TIOCMSET:
    795 	case TIOCMBIS:
    796 	case TIOCMBIC:
    797 		tiocm_to_sbscn(ch, cmd, *(int *)data);
    798 		break;
    799 
    800 	case TIOCMGET:
    801 		*(int *)data = sbscn_to_tiocm(ch);
    802 		break;
    803 
    804 	default:
    805 		error = EPASSTHROUGH;
    806 		break;
    807 	}
    808 
    809 	splx(s);
    810 
    811 #ifdef SBSCN_DEBUG
    812 	if (sbscn_debug)
    813 		sbscn_status(ch, "sbscn_ioctl ");
    814 #endif
    815 
    816 	return (error);
    817 }
    818 
    819 integrate void
    820 sbscn_schedrx(struct sbscn_channel *ch)
    821 {
    822 
    823 	ch->ch_rx_ready = 1;
    824 
    825 	/* Wake up the poller. */
    826 	softintr_schedule(ch->ch_si);
    827 }
    828 
    829 void
    830 sbscn_break(struct sbscn_channel *ch, int onoff)
    831 {
    832 
    833 /* XXXCGD delay break until not busy */
    834 	if (onoff)
    835 		WRITE_REG(ch->ch_base + 0x50, 0x60);
    836 	else
    837 		WRITE_REG(ch->ch_base + 0x50, 0x70);
    838 
    839 #if 0
    840 	if (!ch->ch_heldchange) {
    841 		if (ch->ch_tx_busy) {
    842 			ch->ch_heldtbc = ch->ch_tbc;
    843 			ch->ch_tbc = 0;
    844 			ch->ch_heldchange = 1;
    845 		} else
    846 			sbscn_loadchannelregs(ch);
    847 	}
    848 #endif
    849 }
    850 
    851 void
    852 sbscn_modem(struct sbscn_channel *ch, int onoff)
    853 {
    854 
    855 	if (ch->ch_o_dtr == 0)
    856 		return;
    857 
    858 	if (onoff)
    859 		SET(ch->ch_oports, ch->ch_o_dtr);
    860 	else
    861 		CLR(ch->ch_oports, ch->ch_o_dtr);
    862 
    863 	if (!ch->ch_heldchange) {
    864 		if (ch->ch_tx_busy) {
    865 			ch->ch_heldtbc = ch->ch_tbc;
    866 			ch->ch_tbc = 0;
    867 			ch->ch_heldchange = 1;
    868 		} else
    869 			sbscn_loadchannelregs(ch);
    870 	}
    871 }
    872 
    873 void
    874 tiocm_to_sbscn(struct sbscn_channel *ch, int how, int ttybits)
    875 {
    876 	u_char bits;
    877 
    878 	bits = 0;
    879 	if (ISSET(ttybits, TIOCM_DTR))
    880 		SET(bits, ch->ch_o_dtr);
    881 	if (ISSET(ttybits, TIOCM_RTS))
    882 		SET(bits, ch->ch_o_rts);
    883 
    884 	switch (how) {
    885 	case TIOCMBIC:
    886 		CLR(ch->ch_oports, bits);
    887 		break;
    888 
    889 	case TIOCMBIS:
    890 		SET(ch->ch_oports, bits);
    891 		break;
    892 
    893 	case TIOCMSET:
    894 		ch->ch_oports = bits;
    895 		break;
    896 	}
    897 
    898 	if (!ch->ch_heldchange) {
    899 		if (ch->ch_tx_busy) {
    900 			ch->ch_heldtbc = ch->ch_tbc;
    901 			ch->ch_tbc = 0;
    902 			ch->ch_heldchange = 1;
    903 		} else
    904 			sbscn_loadchannelregs(ch);
    905 	}
    906 }
    907 
    908 int
    909 sbscn_to_tiocm(struct sbscn_channel *ch)
    910 {
    911 	u_char hwbits;
    912 	int ttybits = 0;
    913 
    914 	hwbits = ch->ch_oports;
    915 	if (ISSET(hwbits, ch->ch_o_dtr))
    916 		SET(ttybits, TIOCM_DTR);
    917 	if (ISSET(hwbits, ch->ch_o_rts))
    918 		SET(ttybits, TIOCM_RTS);
    919 
    920 	hwbits = ch->ch_iports;
    921 	if (ISSET(hwbits, ch->ch_i_dcd))
    922 		SET(ttybits, TIOCM_CD);
    923 	if (ISSET(hwbits, ch->ch_i_cts))
    924 		SET(ttybits, TIOCM_CTS);
    925 	if (ISSET(hwbits, ch->ch_i_dsr))
    926 		SET(ttybits, TIOCM_DSR);
    927 	if (ISSET(hwbits, ch->ch_i_ri))
    928 		SET(ttybits, TIOCM_RI);
    929 
    930 	if (ch->ch_imr != 0)
    931 		SET(ttybits, TIOCM_LE);
    932 
    933 	return (ttybits);
    934 }
    935 
    936 static int
    937 cflag2modes(cflag, mode1p, mode2p)
    938 	tcflag_t cflag;
    939 	u_char *mode1p;
    940 	u_char *mode2p;
    941 {
    942 	u_char mode1;
    943 	u_char mode2;
    944 	int err = 0;
    945 
    946 	mode1 = mode2 = 0;
    947 
    948 	switch (ISSET(cflag, CSIZE)) {
    949 	case CS7:
    950 		mode1 |= 2;			/* XXX */
    951 		break;
    952 	default:
    953 		err = -1;
    954 		/* FALLTHRU for sanity */
    955 	case CS8:
    956 		mode1 |= 3;			/* XXX */
    957 		break;
    958 	}
    959 	if (!ISSET(cflag, PARENB))
    960 		mode1 |= 2 << 3;
    961 	else {
    962 		mode1 |= 0 << 3;
    963 		if (ISSET(cflag, PARODD))
    964 			mode1 |= 1 << 2;
    965 	}
    966 
    967 	if (ISSET(cflag, CSTOPB))
    968 		mode2 |= 1 << 3;		/* two stop bits XXX not std */
    969 
    970 	if (ISSET(cflag, CRTSCTS)) {
    971 		mode1 |= 1 << 7;
    972 		mode2 |= 1 << 4;
    973 	}
    974 
    975 	*mode1p = mode1;
    976 	*mode2p = mode2;
    977 	return (err);
    978 }
    979 
    980 int
    981 sbscn_param(struct tty *tp, struct termios *t)
    982 {
    983 	struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)];
    984 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
    985 	long brc;
    986 	u_char mode1, mode2;
    987 	int s;
    988 
    989 /* XXX reset to console parameters if console? */
    990 #if 0
    991 XXX disable, enable.
    992 #endif
    993 
    994 	/* Check requested parameters. */
    995 	if (sbscn_speed(t->c_ospeed, &brc) < 0)
    996 		return (EINVAL);
    997 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    998 		return (EINVAL);
    999 
   1000 	/*
   1001 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1002 	 * is always active.
   1003 	 */
   1004 	if (ISSET(ch->ch_swflags, TIOCFLAG_SOFTCAR) ||
   1005 	    ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
   1006 		SET(t->c_cflag, CLOCAL);
   1007 		CLR(t->c_cflag, HUPCL);
   1008 	}
   1009 
   1010 	/*
   1011 	 * If there were no changes, don't do anything.  This avoids dropping
   1012 	 * input and improves performance when all we did was frob things like
   1013 	 * VMIN and VTIME.
   1014 	 */
   1015 	if (tp->t_ospeed == t->c_ospeed &&
   1016 	    tp->t_cflag == t->c_cflag)
   1017 		return (0);
   1018 
   1019 	if (cflag2modes(t->c_cflag, &mode1, &mode2) < 0)
   1020 		return (EINVAL);
   1021 
   1022 	s = splserial();
   1023 
   1024 	ch->ch_mode1 = mode1;
   1025 	ch->ch_mode2 = mode2;
   1026 
   1027 	/*
   1028 	 * If we're not in a mode that assumes a connection is present, then
   1029 	 * ignore carrier changes.
   1030 	 */
   1031 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
   1032 		ch->ch_i_dcd = 0;
   1033 	else
   1034 		ch->ch_i_dcd = ch->ch_i_dcd_pin;
   1035 	/*
   1036 	 * Set the flow control pins depending on the current flow control
   1037 	 * mode.
   1038 	 */
   1039 	if (ISSET(t->c_cflag, CRTSCTS)) {
   1040 		ch->ch_o_dtr = ch->ch_o_dtr_pin;
   1041 		ch->ch_o_rts = ch->ch_o_rts_pin;
   1042 		ch->ch_i_cts = ch->ch_i_cts_pin;
   1043 		/* hw controle enable bits in mod regs set by cflag2modes */
   1044 	} else if (ISSET(t->c_cflag, MDMBUF)) {
   1045 		/*
   1046 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
   1047 		 * carrier detection.
   1048 		 */
   1049 		ch->ch_o_dtr = 0;
   1050 		ch->ch_o_rts = ch->ch_o_dtr_pin;
   1051 		ch->ch_i_cts = ch->ch_i_dcd_pin;
   1052 	} else {
   1053 		/*
   1054 		 * If no flow control, then always set RTS.  This will make
   1055 		 * the other side happy if it mistakenly thinks we're doing
   1056 		 * RTS/CTS flow control.
   1057 		 */
   1058 		ch->ch_o_dtr = ch->ch_o_dtr_pin | ch->ch_o_rts_pin;
   1059 		ch->ch_o_rts = 0;
   1060 		ch->ch_i_cts = 0;
   1061 		if (ISSET(ch->ch_oports, ch->ch_o_dtr_pin))
   1062 			SET(ch->ch_oports, ch->ch_o_rts_pin);
   1063 		else
   1064 			CLR(ch->ch_oports, ch->ch_o_rts_pin);
   1065 	}
   1066 	/* XXX maybe mask the ports which generate intrs? */
   1067 
   1068 	ch->ch_brc = brc;
   1069 
   1070 	/* XXX maybe set fifo-full receive mode if RTSCTS and high speed? */
   1071 
   1072 	/* And copy to tty. */
   1073 	tp->t_ispeed = 0;
   1074 	tp->t_ospeed = t->c_ospeed;
   1075 	tp->t_cflag = t->c_cflag;
   1076 
   1077 	if (!ch->ch_heldchange) {
   1078 		if (ch->ch_tx_busy) {
   1079 			ch->ch_heldtbc = ch->ch_tbc;
   1080 			ch->ch_tbc = 0;
   1081 			ch->ch_heldchange = 1;
   1082 		} else
   1083 			sbscn_loadchannelregs(ch);
   1084 	}
   1085 
   1086 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1087 		/* Disable the high water mark. */
   1088 		ch->ch_r_hiwat = 0;
   1089 		ch->ch_r_lowat = 0;
   1090 		if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
   1091 			CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
   1092 			sbscn_schedrx(ch);
   1093 		}
   1094 		if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
   1095 			CLR(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1096 			sbscn_dohwiflow(ch);
   1097 		}
   1098 	} else {
   1099 		ch->ch_r_hiwat = sbscn_rbuf_hiwat;
   1100 		ch->ch_r_lowat = sbscn_rbuf_lowat;
   1101 	}
   1102 
   1103 	splx(s);
   1104 
   1105 	/*
   1106 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1107 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1108 	 * explicit request.
   1109 	 */
   1110 	(void) (*tp->t_linesw->l_modem)(tp,
   1111 	    ISSET(ch->ch_iports, ch->ch_i_dcd));
   1112 
   1113 #ifdef SBSCN_DEBUG
   1114 	if (sbscn_debug)
   1115 		sbscn_status(ch, "sbscnparam ");
   1116 #endif
   1117 
   1118 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1119 		if (ch->ch_tx_stopped) {
   1120 			ch->ch_tx_stopped = 0;
   1121 			sbscn_start(tp);
   1122 		}
   1123 	}
   1124 
   1125 	return (0);
   1126 }
   1127 
   1128 void
   1129 sbscn_iflush(struct sbscn_channel *ch)
   1130 {
   1131 #ifdef DIAGNOSTIC
   1132 	int reg;
   1133 #endif
   1134 	int timo;
   1135 
   1136 #ifdef DIAGNOSTIC
   1137 	reg = 0xffff;
   1138 #endif
   1139 	timo = 50000;
   1140 	/* flush any pending I/O */
   1141 	while (ISSET(READ_REG(ch->ch_base + 0x20), 0x01)
   1142 	    && --timo)
   1143 #ifdef DIAGNOSTIC
   1144 		reg =
   1145 #else
   1146 		    (void)
   1147 #endif
   1148 		    READ_REG(ch->ch_base + 0x60);
   1149 #ifdef DIAGNOSTIC
   1150 	if (!timo)
   1151 		printf("%s: sbscn_iflush timeout %02x\n",
   1152 		    ch->ch_sc->sc_dev.dv_xname, reg & 0xff);
   1153 #endif
   1154 }
   1155 
   1156 void
   1157 sbscn_loadchannelregs(struct sbscn_channel *ch)
   1158 {
   1159 
   1160 	/* XXXXX necessary? */
   1161 	sbscn_iflush(ch);
   1162 
   1163 	WRITE_REG(ch->ch_imr_base, 0);
   1164 
   1165 // XXX disable?
   1166 	WRITE_REG(ch->ch_base + 0x00, ch->ch_mode1);
   1167 	WRITE_REG(ch->ch_base + 0x10, ch->ch_mode2);
   1168 	WRITE_REG(ch->ch_base + 0x30, ch->ch_brc);
   1169 
   1170 	ch->ch_oports_active = ch->ch_oports;
   1171 	SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active);
   1172 
   1173 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1174 }
   1175 
   1176 int
   1177 sbscn_hwiflow(struct tty *tp, int block)
   1178 {
   1179 	struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)];
   1180 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
   1181 	int s;
   1182 
   1183 	if (ch->ch_o_rts == 0)
   1184 		return (0);
   1185 
   1186 	s = splserial();
   1187 	if (block) {
   1188 		if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
   1189 			SET(ch->ch_rx_flags, RX_TTY_BLOCKED);
   1190 			sbscn_dohwiflow(ch);
   1191 		}
   1192 	} else {
   1193 		if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
   1194 			CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
   1195 			sbscn_schedrx(ch);
   1196 		}
   1197 		if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
   1198 			CLR(ch->ch_rx_flags, RX_TTY_BLOCKED);
   1199 			sbscn_dohwiflow(ch);
   1200 		}
   1201 	}
   1202 	splx(s);
   1203 	return (1);
   1204 }
   1205 
   1206 /*
   1207  * (un)block input via hw flowcontrol
   1208  */
   1209 void
   1210 sbscn_dohwiflow(struct sbscn_channel *ch)
   1211 {
   1212 
   1213 	if (ch->ch_o_rts == 0)
   1214 		return;
   1215 
   1216 	if (ISSET(ch->ch_rx_flags, RX_ANY_BLOCK)) {
   1217 		CLR(ch->ch_oports, ch->ch_o_rts);
   1218 		CLR(ch->ch_oports_active, ch->ch_o_rts);
   1219 	} else {
   1220 		SET(ch->ch_oports, ch->ch_o_rts);
   1221 		SET(ch->ch_oports_active, ch->ch_o_rts);
   1222 	}
   1223 	SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active);
   1224 }
   1225 
   1226 void
   1227 sbscn_start(struct tty *tp)
   1228 {
   1229 	struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)];
   1230 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
   1231 	int s;
   1232 
   1233 	s = spltty();
   1234 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1235 		goto out;
   1236 	if (ch->ch_tx_stopped)
   1237 		goto out;
   1238 	if (!ttypull(tp))
   1239 		goto out;
   1240 
   1241 	/* Grab the first contiguous region of buffer space. */
   1242 	{
   1243 		u_char *tba;
   1244 		int tbc;
   1245 
   1246 		tba = tp->t_outq.c_cf;
   1247 		tbc = ndqb(&tp->t_outq, 0);
   1248 
   1249 		(void)splserial();
   1250 
   1251 		ch->ch_tba = tba;
   1252 		ch->ch_tbc = tbc;
   1253 	}
   1254 
   1255 	SET(tp->t_state, TS_BUSY);
   1256 	ch->ch_tx_busy = 1;
   1257 
   1258 	/* Enable transmit completion interrupts if necessary. */
   1259 	if (!ISSET(ch->ch_imr, 0x1)) {
   1260 		SET(ch->ch_imr, 0x1);
   1261 		WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1262 	}
   1263 
   1264 	/* Output the first chunk of the contiguous buffer. */
   1265 	{
   1266 		u_char c;
   1267 
   1268 		while (ch->ch_tbc && READ_REG(ch->ch_base + 0x20) & 0x04) {
   1269 			c = *ch->ch_tba++;
   1270 			ch->ch_tbc--;
   1271 			WRITE_REG(ch->ch_base + 0x70, c);
   1272 		}
   1273 	}
   1274 out:
   1275 	splx(s);
   1276 	return;
   1277 }
   1278 
   1279 /*
   1280  * Stop output on a line.
   1281  */
   1282 void
   1283 sbscnstop(struct tty *tp, int flag)
   1284 {
   1285 	struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)];
   1286 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
   1287 	int s;
   1288 
   1289 	s = splserial();
   1290 	if (ISSET(tp->t_state, TS_BUSY)) {
   1291 		/* Stop transmitting at the next chunk. */
   1292 		ch->ch_tbc = 0;
   1293 		ch->ch_heldtbc = 0;
   1294 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1295 			SET(tp->t_state, TS_FLUSH);
   1296 	}
   1297 	splx(s);
   1298 }
   1299 
   1300 void
   1301 sbscn_diag(arg)
   1302 	void *arg;
   1303 {
   1304 	struct sbscn_channel *ch = arg;
   1305 	struct sbscn_softc *sc = ch->ch_sc;
   1306 	int overflows, floods;
   1307 	int s;
   1308 
   1309 	s = splserial();
   1310 	overflows = ch->ch_overflows;
   1311 	ch->ch_overflows = 0;
   1312 	floods = ch->ch_floods;
   1313 	ch->ch_floods = 0;
   1314 	ch->ch_errors = 0;
   1315 	splx(s);
   1316 
   1317 	log(LOG_WARNING, "%s: channel %d: %d fifo overflow%s, %d ibuf flood%s\n",
   1318 	    sc->sc_dev.dv_xname, ch->ch_num,
   1319 	    overflows, overflows == 1 ? "" : "s",
   1320 	    floods, floods == 1 ? "" : "s");
   1321 }
   1322 
   1323 integrate void
   1324 sbscn_rxsoft(struct sbscn_channel *ch, struct tty *tp)
   1325 {
   1326 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
   1327 	u_char *get, *end;
   1328 	u_int cc, scc;
   1329 	u_char sr;
   1330 	int code;
   1331 	int s;
   1332 
   1333 	end = ch->ch_ebuf;
   1334 	get = ch->ch_rbget;
   1335 	scc = cc = sbscn_rbuf_size - ch->ch_rbavail;
   1336 
   1337 	if (cc == sbscn_rbuf_size) {
   1338 		ch->ch_floods++;
   1339 		if (ch->ch_errors++ == 0)
   1340 			callout_reset(&ch->ch_diag_callout, 60 * hz,
   1341 			    sbscn_diag, ch);
   1342 	}
   1343 
   1344 	while (cc) {
   1345 		code = get[0];
   1346 		sr = get[1];
   1347 		if (ISSET(sr, 0xf0)) {
   1348 			if (ISSET(sr, 0x10)) {
   1349 				ch->ch_overflows++;
   1350 				if (ch->ch_errors++ == 0)
   1351 					callout_reset(&ch->ch_diag_callout,
   1352 					    60 * hz, sbscn_diag, ch);
   1353 			}
   1354 			if (ISSET(sr, 0xc0))
   1355 				SET(code, TTY_FE);
   1356 			if (ISSET(sr, 0x20))
   1357 				SET(code, TTY_PE);
   1358 		}
   1359 		if ((*rint)(code, tp) == -1) {
   1360 			/*
   1361 			 * The line discipline's buffer is out of space.
   1362 			 */
   1363 			if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
   1364 				/*
   1365 				 * We're either not using flow control, or the
   1366 				 * line discipline didn't tell us to block for
   1367 				 * some reason.  Either way, we have no way to
   1368 				 * know when there's more space available, so
   1369 				 * just drop the rest of the data.
   1370 				 */
   1371 				get += cc << 1;
   1372 				if (get >= end)
   1373 					get -= sbscn_rbuf_size << 1;
   1374 				cc = 0;
   1375 			} else {
   1376 				/*
   1377 				 * Don't schedule any more receive processing
   1378 				 * until the line discipline tells us there's
   1379 				 * space available (through comhwiflow()).
   1380 				 * Leave the rest of the data in the input
   1381 				 * buffer.
   1382 				 */
   1383 				SET(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
   1384 			}
   1385 			break;
   1386 		}
   1387 		get += 2;
   1388 		if (get >= end)
   1389 			get = ch->ch_rbuf;
   1390 		cc--;
   1391 	}
   1392 
   1393 	if (cc != scc) {
   1394 		ch->ch_rbget = get;
   1395 		s = splserial();
   1396 		cc = ch->ch_rbavail += scc - cc;
   1397 		/* Buffers should be ok again, release possible block. */
   1398 		if (cc >= ch->ch_r_lowat) {
   1399 			if (ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
   1400 				CLR(ch->ch_rx_flags, RX_IBUF_OVERFLOWED);
   1401 				SET(ch->ch_imr, 0x02);
   1402 				WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1403 			}
   1404 			if (ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED)) {
   1405 				CLR(ch->ch_rx_flags, RX_IBUF_BLOCKED);
   1406 				sbscn_dohwiflow(ch);
   1407 			}
   1408 		}
   1409 		splx(s);
   1410 	}
   1411 }
   1412 
   1413 integrate void
   1414 sbscn_txsoft(struct sbscn_channel *ch, struct tty *tp)
   1415 {
   1416 
   1417 	CLR(tp->t_state, TS_BUSY);
   1418 	if (ISSET(tp->t_state, TS_FLUSH))
   1419 		CLR(tp->t_state, TS_FLUSH);
   1420 	else
   1421 		ndflush(&tp->t_outq, (int)(ch->ch_tba - tp->t_outq.c_cf));
   1422 	(*tp->t_linesw->l_start)(tp);
   1423 }
   1424 
   1425 integrate void
   1426 sbscn_stsoft(struct sbscn_channel *ch, struct tty *tp)
   1427 {
   1428 	u_char iports, delta;
   1429 	int s;
   1430 
   1431 	s = splserial();
   1432 	iports = ch->ch_iports;
   1433 	delta = ch->ch_iports_delta;
   1434 	ch->ch_iports_delta = 0;
   1435 	splx(s);
   1436 
   1437 	if (ISSET(delta, ch->ch_i_dcd)) {
   1438 		/*
   1439 		 * Inform the tty layer that carrier detect changed.
   1440 		 */
   1441 		(void) (*tp->t_linesw->l_modem)(tp,
   1442 		    ISSET(iports, ch->ch_i_dcd));
   1443 	}
   1444 
   1445 	if (ISSET(delta, ch->ch_i_cts)) {
   1446 		/* Block or unblock output according to flow control. */
   1447 		if (ISSET(iports, ch->ch_i_cts)) {
   1448 			ch->ch_tx_stopped = 0;
   1449 			(*tp->t_linesw->l_start)(tp);
   1450 		} else {
   1451 			ch->ch_tx_stopped = 1;
   1452 		}
   1453 	}
   1454 
   1455 #ifdef SBSCN_DEBUG
   1456 	if (sbscn_debug)
   1457 		sbscn_status(ch, "sbscn_stsoft");
   1458 #endif
   1459 }
   1460 
   1461 void
   1462 sbscn_soft(void *arg)
   1463 {
   1464 	struct sbscn_channel *ch = arg;
   1465 	struct tty *tp = ch->ch_tty;
   1466 
   1467 	if (ch->ch_rx_ready) {
   1468 		ch->ch_rx_ready = 0;
   1469 		sbscn_rxsoft(ch, tp);
   1470 	}
   1471 
   1472 	if (ch->ch_st_check) {
   1473 		ch->ch_st_check = 0;
   1474 		sbscn_stsoft(ch, tp);
   1475 	}
   1476 
   1477 	if (ch->ch_tx_done) {
   1478 		ch->ch_tx_done = 0;
   1479 		sbscn_txsoft(ch, tp);
   1480 	}
   1481 }
   1482 
   1483 void
   1484 sbscn_intr(void *arg, uint32_t status, uint32_t pc)
   1485 {
   1486 	struct sbscn_channel *ch = arg;
   1487 	u_char *put, *end;
   1488 	u_int cc;
   1489 	u_char isr, sr;
   1490 
   1491 	/* read ISR */
   1492 	isr = READ_REG(ch->ch_isr_base) & ch->ch_imr;
   1493 	if (isr == 0)
   1494 		return;
   1495 
   1496 	end = ch->ch_ebuf;
   1497 	put = ch->ch_rbput;
   1498 	cc = ch->ch_rbavail;
   1499 
   1500 	do {
   1501 		u_char	iports, delta;
   1502 
   1503 		if (isr & 0x02) {
   1504 
   1505 			sr = READ_REG(ch->ch_base + 0x20);
   1506 			/* XXX sr 0x01 bit must be set at this point */
   1507 
   1508 #if defined(DDB) || defined(KGDB)
   1509 			if ((sr & 0x80) == 0x80) {
   1510 #ifdef DDB
   1511 				if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
   1512 					(void)READ_REG(ch->ch_base + 0x60);
   1513 					console_debugger();
   1514 					continue;
   1515 				}
   1516 #endif
   1517 #ifdef KGDB
   1518 				if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB)) {
   1519 					(void)READ_REG(ch->ch_base + 0x60);
   1520 					kgdb_connect(1);
   1521 					continue;
   1522 				}
   1523 #endif
   1524 			}
   1525 #endif /* DDB || KGDB */
   1526 
   1527 		    	if (!ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
   1528 				while (cc > 0) {
   1529 					put[0] = READ_REG(ch->ch_base + 0x60);
   1530 					put[1] = sr;
   1531 					put += 2;
   1532 					if (put >= end)
   1533 						put = ch->ch_rbuf;
   1534 					cc--;
   1535 
   1536 					sr = READ_REG(ch->ch_base + 0x20);
   1537 					if (!ISSET(sr, 0x02))
   1538 						break;
   1539 				}
   1540 
   1541 				/*
   1542 				 * Current string of incoming characters ended
   1543 				 * because no more data was available or we
   1544 				 * ran out of space.  Schedule a receive event
   1545 				 * if any data was received.  If we're out of
   1546 				 * space, turn off receive interrupts.
   1547 				 */
   1548 				ch->ch_rbput = put;
   1549 				ch->ch_rbavail = cc;
   1550 				if (!ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED))
   1551 					ch->ch_rx_ready = 1;
   1552 
   1553 				/*
   1554 				 * See if we are in danger of overflowing a
   1555 				 * buffer. If so, use hardware flow control
   1556 				 * to ease the pressure.
   1557 				 */
   1558 				if (!ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED) &&
   1559 				    cc < ch->ch_r_hiwat) {
   1560 					SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
   1561 					sbscn_dohwiflow(ch);
   1562 				}
   1563 
   1564 				/*
   1565 				 * If we're out of space, disable receive
   1566 				 * interrupts until the queue has drained
   1567 				 * a bit.
   1568 				 */
   1569 				if (!cc) {
   1570 					SET(ch->ch_rx_flags,
   1571 					    RX_IBUF_OVERFLOWED);
   1572 					CLR(ch->ch_imr, 0x02);
   1573 					WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1574 				}
   1575 			} else {
   1576 				/* XXX panic? */
   1577 				CLR(ch->ch_imr, 0x02);
   1578 				WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1579 				continue;
   1580 			}
   1581 		}
   1582 
   1583 		if (isr & 0x01) {
   1584 			CLR(ch->ch_imr, 0x01);
   1585 			WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1586 		}
   1587 
   1588 #if 0
   1589 XXX
   1590 		if (isr & 0x08) {
   1591 			clear input signal change!
   1592 		}
   1593 #endif
   1594 		iports = GET_INPUT_SIGNALS(ch);
   1595 		delta = iports ^ ch->ch_iports;
   1596 		ch->ch_iports = iports;
   1597 
   1598 		/*
   1599 		 * Process normal status changes
   1600 		 */
   1601 		if (ISSET(delta, ch->ch_i_mask)) {
   1602 			SET(ch->ch_iports_delta, delta);
   1603 
   1604 			/*
   1605 			 * Stop output immediately if we lose the output
   1606 			 * flow control signal or carrier detect.
   1607 			 */
   1608 			if (ISSET(~iports, ch->ch_i_mask)) {
   1609 				ch->ch_tbc = 0;
   1610 				ch->ch_heldtbc = 0;
   1611 #ifdef SBSCN_DEBUG
   1612 				if (sbscn_debug)
   1613 					sbscn_status(ch, "sbscn_intr  ");
   1614 #endif
   1615 			}
   1616 
   1617 			ch->ch_st_check = 1;
   1618 		}
   1619 	} while ((isr = (READ_REG(ch->ch_isr_base) & ch->ch_imr)) != 0);
   1620 
   1621 	/*
   1622 	 * Done handling any receive interrupts and status changes, and
   1623 	 * clearing the tx-ready interrupt if it was set.  See if data can
   1624 	 * be transmitted as well. Schedule tx done event if no data left
   1625 	 * and tty was marked busy.
   1626 	 */
   1627 	sr = READ_REG(ch->ch_base + 0x20);
   1628 	if (ISSET(sr, 0x4)) {
   1629 
   1630 		/*
   1631 		 * If we've delayed a parameter change, do it now, and restart
   1632 		 * output.
   1633 		 */
   1634 		if (ch->ch_heldchange) {
   1635 			sbscn_loadchannelregs(ch);
   1636 			ch->ch_heldchange = 0;
   1637 			ch->ch_tbc = ch->ch_heldtbc;
   1638 			ch->ch_heldtbc = 0;
   1639 		}
   1640 
   1641 		/* Output the next chunk of the contiguous buffer, if any. */
   1642 		if (ch->ch_tbc > 0) {
   1643 			int wrote1;
   1644 			u_char c;
   1645 
   1646 			wrote1 = 0;
   1647 			while (ch->ch_tbc &&
   1648 			    READ_REG(ch->ch_base + 0x20) & 0x04) {
   1649 				wrote1 = 1;
   1650 				c = *ch->ch_tba++;
   1651 				ch->ch_tbc--;
   1652 				WRITE_REG(ch->ch_base + 0x70, c);
   1653 			}
   1654 			if (wrote1) {
   1655 				SET(ch->ch_imr, 0x01);
   1656 				WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1657 			}
   1658 		} else {
   1659 			/*
   1660 			 * transmit completion interrupts already disabled,
   1661 			 * mark the channel tx state as done.
   1662 			 */
   1663 			if (ch->ch_tx_busy) {
   1664 				ch->ch_tx_busy = 0;
   1665 				ch->ch_tx_done = 1;
   1666 			}
   1667 		}
   1668 	}
   1669 
   1670 	/* Wake up the poller. */
   1671 	softintr_schedule(ch->ch_si);
   1672 
   1673 #if NRND > 0 && defined(RND_SBSCN)
   1674 	rnd_add_uint32(&ch->ch_rnd_source, isr | sr);
   1675 #endif
   1676 }
   1677 
   1678 /*
   1679  * The following functions are polled getc and putc routines, shared
   1680  * by the console and kgdb glue.
   1681  */
   1682 
   1683 int
   1684 sbscn_common_getc(u_long addr, int chan)
   1685 {
   1686 	int s = splhigh();
   1687 	u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
   1688 	int c;
   1689 
   1690 	/* block until a character becomes available */
   1691 	while ((READ_REG(base + 0x20) & 0x01) == 0)
   1692 		continue;
   1693 
   1694 	c = READ_REG(base + 0x60) & 0xff;
   1695 	splx(s);
   1696 	return (c);
   1697 }
   1698 
   1699 void
   1700 sbscn_common_putc(u_long addr, int chan, int c)
   1701 {
   1702 	int s = splhigh();
   1703 	int timo;
   1704 	u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
   1705 
   1706 	/* wait for any pending transmission to finish */
   1707 	timo = 1500000;
   1708 	while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo)
   1709 		continue;
   1710 
   1711 	WRITE_REG(base + 0x70, c);
   1712 
   1713 	/* wait for this transmission to complete */
   1714 	timo = 15000000;
   1715 	while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo)
   1716 		continue;
   1717 
   1718 	splx(s);
   1719 }
   1720 
   1721 /*
   1722  * Initialize UART for use as console or KGDB line.
   1723  */
   1724 int
   1725 sbscn_init(u_long addr, int chan, int rate, tcflag_t cflag)
   1726 {
   1727 #if 1
   1728 	u_long chanregbase = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
   1729 	u_long imaskreg = MIPS_PHYS_TO_KSEG1(addr + 0x230 + (chan * 0x20));
   1730 	u_char mode1, mode2;
   1731 	u_long brc;
   1732 	volatile int timo;
   1733 
   1734 	WRITE_REG(imaskreg, 0);			/* disable channel intrs */
   1735 
   1736 	/* XXX should we really do the following?  how about only if enabled? */
   1737 	/* wait for any pending transmission to finish */
   1738 	timo = 1500000;
   1739 	while ((READ_REG(chanregbase + 0x20) & 0x08) == 0 && --timo)
   1740 		continue;
   1741 
   1742 	/* XXX: wait a little.  THIS SHOULD NOT BE NECESSARY!!! (?) */
   1743 	timo = 1500000;
   1744 	while (--timo)
   1745 		;
   1746 
   1747 	WRITE_REG(chanregbase + 0x50, 2 << 4);	/* reset receiver */
   1748 	WRITE_REG(chanregbase + 0x50, 3 << 4);	/* reset transmitter */
   1749 
   1750 	/* set up the line for use */
   1751 	(void)cflag2modes(cflag, &mode1, &mode2);
   1752 	(void)sbscn_speed(rate, &brc);
   1753 	WRITE_REG(chanregbase + 0x00, mode1);
   1754 	WRITE_REG(chanregbase + 0x10, mode2);
   1755 	WRITE_REG(chanregbase + 0x30, brc);
   1756 
   1757 	/* enable transmit and receive */
   1758 #define	M_DUART_RX_EN			0x01
   1759 #define	M_DUART_TX_EN			0x04
   1760 	WRITE_REG(chanregbase + 0x50,M_DUART_RX_EN | M_DUART_TX_EN);
   1761 #endif
   1762 
   1763 	/* XXX: wait a little.  THIS SHOULD NOT BE NECESSARY!!! (?) */
   1764 	timo = 1500000;
   1765 	while (--timo)
   1766 		;
   1767 
   1768 #if 0 /* XXXCGD */
   1769 	bus_space_handle_t ioh;
   1770 
   1771 	if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
   1772 		return (ENOMEM); /* ??? */
   1773 
   1774 	bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
   1775 	bus_space_write_1(iot, ioh, com_efr, 0);
   1776 	bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
   1777 	rate = comspeed(rate, frequency);
   1778 	bus_space_write_1(iot, ioh, com_dlbl, rate);
   1779 	bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
   1780 	bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
   1781 	bus_space_write_1(iot, ioh, com_mcr, MCR_DTR | MCR_RTS);
   1782 	bus_space_write_1(iot, ioh, com_fifo,
   1783 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
   1784 	bus_space_write_1(iot, ioh, com_ier, 0);
   1785 
   1786 	*iohp = ioh;
   1787 	return (0);
   1788 #endif /* XXXCGD */
   1789 	/* XXXCGD */
   1790 	return (0);
   1791 }
   1792 
   1793 /*
   1794  * Following are all routines needed for sbscn to act as console
   1795  */
   1796 int
   1797 sbscn_cnattach(u_long addr, int chan, int rate, tcflag_t cflag)
   1798 {
   1799 	int res;
   1800 	static struct consdev sbscn_cons = {
   1801 		NULL, NULL, sbscn_cngetc, sbscn_cnputc, sbscn_cnpollc, NULL,
   1802 		    NULL, NULL, NODEV, CN_NORMAL
   1803 	};
   1804 
   1805 	res = sbscn_init(addr, chan, rate, cflag);
   1806 	if (res)
   1807 		return (res);
   1808 
   1809 	cn_tab = &sbscn_cons;
   1810 
   1811 	sbscn_cons_present = 1;
   1812 	sbscn_cons_addr = addr;
   1813 	sbscn_cons_chan = chan;
   1814 	sbscn_cons_rate = rate;
   1815 	sbscn_cons_cflag = cflag;
   1816 
   1817 	return (0);
   1818 }
   1819 
   1820 int
   1821 sbscn_cngetc(dev_t dev)
   1822 {
   1823 
   1824 	return (sbscn_common_getc(sbscn_cons_addr, sbscn_cons_chan));
   1825 }
   1826 
   1827 /*
   1828  * Console kernel output character routine.
   1829  */
   1830 void
   1831 sbscn_cnputc(dev_t dev, int c)
   1832 {
   1833 
   1834 	sbscn_common_putc(sbscn_cons_addr, sbscn_cons_chan, c);
   1835 }
   1836 
   1837 void
   1838 sbscn_cnpollc(dev_t dev, int on)
   1839 {
   1840 
   1841 }
   1842 
   1843 #ifdef KGDB
   1844 int
   1845 sbscn_kgdb_attach(u_long addr, int chan, int rate, tcflag_t cflag)
   1846 {
   1847 	int res;
   1848 
   1849 	if (!sbscn_cons_present &&
   1850 	    sbscn_cons_addr == addr && sbscn_cons_chan == chan)
   1851 		return (EBUSY); /* cannot share with console */
   1852 
   1853 	res = sbscn_init(addr, chan, rate, cflag);
   1854 	if (res)
   1855 		return (res);
   1856 
   1857 	kgdb_attach(sbscn_kgdb_getc, sbscn_kgdb_putc, NULL);
   1858 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   1859 
   1860 	sbscn_kgdb_present = 1;
   1861 	sbscn_kgdb_addr = addr;
   1862 	sbscn_kgdb_chan = chan;
   1863 
   1864 	return (0);
   1865 }
   1866 
   1867 /* ARGSUSED */
   1868 int
   1869 sbscn_kgdb_getc(arg)
   1870 	void *arg;
   1871 {
   1872 
   1873 	return (sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan));
   1874 }
   1875 
   1876 /* ARGSUSED */
   1877 void
   1878 sbscn_kgdb_putc(arg, c)
   1879 	void *arg;
   1880 	int c;
   1881 {
   1882 
   1883 	sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan, c);
   1884 }
   1885 #endif /* KGDB */
   1886 
   1887 /*
   1888  * helper function to identify the sbscn channels used by
   1889  * console or KGDB (and not yet autoconf attached)
   1890  */
   1891 int
   1892 sbscn_is_console(u_long addr, int chan)
   1893 {
   1894 
   1895 	if (sbscn_cons_present && !sbscn_cons_attached &&
   1896 	    sbscn_cons_addr == addr && sbscn_cons_chan == chan)
   1897 		return (1);
   1898 #ifdef KGDB
   1899 	if (sbscn_kgdb_present && !sbscn_kgdb_attached &&
   1900 	    sbscn_kgdb_addr == addr && sbscn_kgdb_chan == chan)
   1901 		return (1);
   1902 #endif
   1903 	return (0);
   1904 }
   1905