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