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