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