Home | History | Annotate | Line # | Download | only in ic
clmpcc.c revision 1.1
      1 /*	$NetBSD: clmpcc.c,v 1.1 1999/02/13 17:05:19 scw Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Steve C. Woodford.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Cirrus Logic CD2400/CD2401 Four Channel Multi-Protocol Comms. Controller.
     41  */
     42 
     43 #include "opt_ddb.h"
     44 
     45 #include <sys/types.h>
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/select.h>
     50 #include <sys/tty.h>
     51 #include <sys/proc.h>
     52 #include <sys/user.h>
     53 #include <sys/conf.h>
     54 #include <sys/file.h>
     55 #include <sys/uio.h>
     56 #include <sys/kernel.h>
     57 #include <sys/syslog.h>
     58 #include <sys/device.h>
     59 #include <sys/malloc.h>
     60 
     61 #include <machine/bus.h>
     62 
     63 #include <dev/ic/clmpccreg.h>
     64 #include <dev/ic/clmpccvar.h>
     65 #include <dev/cons.h>
     66 
     67 
     68 #if defined(CLMPCC_ONLY_BYTESWAP_LOW) && defined(CLMPCC_ONLY_BYTESWAP_HIGH)
     69 #error	"CLMPCC_ONLY_BYTESWAP_LOW and CLMPCC_ONLY_BYTESWAP_HIGH are mutually exclusive."
     70 #endif
     71 
     72 
     73 static int	clmpcc_init	__P((struct clmpcc_softc *sc));
     74 static void	clmpcc_shutdown	__P((struct clmpcc_chan *));
     75 static int	clmpcc_speed	__P((struct clmpcc_softc *, speed_t,
     76 					int *, int *));
     77 static int	clmpcc_param	__P((struct tty *, struct termios *));
     78 static void	clmpcc_start	__P((struct tty *));
     79 static int 	clmpcc_modem_control	__P((struct clmpcc_chan *, int, int));
     80 
     81 
     82 cdev_decl(clmpcc);
     83 
     84 #define	CLMPCCUNIT(x)		(minor(x) & 0x7fffc)
     85 #define CLMPCCCHAN(x)		(minor(x) & 0x00003)
     86 #define	CLMPCCDIALOUT(x)	(minor(x) & 0x80000)
     87 
     88 /*
     89  * These should be in a header file somewhere...
     90  */
     91 #define	ISSET(v, f)	(((v) & (f)) != 0)
     92 #define	ISCLR(v, f)	(((v) & (f)) == 0)
     93 #define SET(v, f)	(v) |= (f)
     94 #define CLR(v, f)	(v) &= ~(f)
     95 
     96 
     97 extern struct cfdriver clmpcc_cd;
     98 
     99 
    100 /*
    101  * Make this an option variable one can patch.
    102  * But be warned:  this must be a power of 2!
    103  */
    104 u_int clmpcc_ibuf_size = CLMPCC_RING_SIZE;
    105 
    106 
    107 /*
    108  * Things needed when the device is used as a console
    109  */
    110 static struct clmpcc_softc *cons_sc = NULL;
    111 static int cons_chan;
    112 static int cons_rate;
    113 
    114 static int	clmpcc_common_getc	__P((struct clmpcc_softc *, int));
    115 static void	clmpcc_common_putc	__P((struct clmpcc_softc *, int, int));
    116 int		clmpcccngetc	__P((dev_t));
    117 void		clmpcccnputc	__P((dev_t, int));
    118 
    119 
    120 /*
    121  * Convenience functions, inlined for speed
    122  */
    123 #define	integrate   static inline
    124 integrate u_int8_t  clmpcc_rdreg __P((struct clmpcc_softc *, u_int));
    125 integrate void      clmpcc_wrreg __P((struct clmpcc_softc *, u_int, u_int));
    126 integrate u_int8_t  clmpcc_rdreg_odd __P((struct clmpcc_softc *, u_int));
    127 integrate void      clmpcc_wrreg_odd __P((struct clmpcc_softc *, u_int, u_int));
    128 integrate u_int8_t  clmpcc_select_channel __P((struct clmpcc_softc *, u_int));
    129 integrate void      clmpcc_channel_cmd __P((struct clmpcc_softc *,int,int));
    130 integrate void      clmpcc_enable_transmitter __P((struct clmpcc_chan *));
    131 
    132 #define clmpcc_rd_msvr(s)	clmpcc_rdreg_odd(s,CLMPCC_REG_MSVR)
    133 #define clmpcc_wr_msvr(s,r,v)	clmpcc_wrreg_odd(s,r,v)
    134 #define clmpcc_wr_pilr(s,r,v)	clmpcc_wrreg_odd(s,r,v)
    135 #define clmpcc_rd_rxdata(s)	clmpcc_rdreg_odd(s,CLMPCC_REG_RDR)
    136 #define clmpcc_wr_txdata(s,v)	clmpcc_wrreg_odd(s,CLMPCC_REG_TDR,v)
    137 
    138 
    139 integrate u_int8_t
    140 clmpcc_rdreg(sc, offset)
    141 	struct clmpcc_softc *sc;
    142 	u_int offset;
    143 {
    144 #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    145 	offset ^= sc->sc_byteswap;
    146 #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    147 	offset ^= CLMPCC_BYTESWAP_HIGH;
    148 #endif
    149 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, offset);
    150 }
    151 
    152 integrate void
    153 clmpcc_wrreg(sc, offset, val)
    154 	struct clmpcc_softc *sc;
    155 	u_int offset;
    156 	u_int val;
    157 {
    158 #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    159 	offset ^= sc->sc_byteswap;
    160 #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    161 	offset ^= CLMPCC_BYTESWAP_HIGH;
    162 #endif
    163 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, offset, val);
    164 }
    165 
    166 integrate u_int8_t
    167 clmpcc_rdreg_odd(sc, offset)
    168 	struct clmpcc_softc *sc;
    169 	u_int offset;
    170 {
    171 #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    172 	offset ^= (sc->sc_byteswap & 2);
    173 #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    174 	offset ^= (CLMPCC_BYTESWAP_HIGH & 2);
    175 #endif
    176 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, offset);
    177 }
    178 
    179 integrate void
    180 clmpcc_wrreg_odd(sc, offset, val)
    181 	struct clmpcc_softc *sc;
    182 	u_int offset;
    183 	u_int val;
    184 {
    185 #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    186 	offset ^= (sc->sc_byteswap & 2);
    187 #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
    188 	offset ^= (CLMPCC_BYTESWAP_HIGH & 2);
    189 #endif
    190 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, offset, val);
    191 }
    192 
    193 integrate u_int8_t
    194 clmpcc_select_channel(sc, new_chan)
    195 	struct clmpcc_softc *sc;
    196 	u_int new_chan;
    197 {
    198 	u_int old_chan = clmpcc_rdreg_odd(sc, CLMPCC_REG_CAR);
    199 
    200 	clmpcc_wrreg_odd(sc, CLMPCC_REG_CAR, new_chan);
    201 
    202 	return old_chan;
    203 }
    204 
    205 integrate void
    206 clmpcc_channel_cmd(sc, chan, cmd)
    207 	struct clmpcc_softc *sc;
    208 	int chan;
    209 	int cmd;
    210 {
    211 	int i;
    212 
    213 	for (i = 5000; i; i--) {
    214 		if ( clmpcc_rdreg(sc, CLMPCC_REG_CCR) == 0 )
    215 			break;
    216 		delay(1);
    217 	}
    218 
    219 	if ( i == 0 )
    220 		printf("%s: channel %d command timeout (idle)\n",
    221 			sc->sc_dev.dv_xname, chan);
    222 
    223 	clmpcc_wrreg(sc, CLMPCC_REG_CCR, cmd);
    224 }
    225 
    226 integrate void
    227 clmpcc_enable_transmitter(ch)
    228 	struct clmpcc_chan *ch;
    229 {
    230 	u_int old;
    231 
    232 	old = clmpcc_select_channel(ch->ch_sc, ch->ch_car);
    233 
    234 	clmpcc_wrreg(ch->ch_sc, CLMPCC_REG_IER,
    235 		clmpcc_rdreg(ch->ch_sc, CLMPCC_REG_IER) | CLMPCC_IER_TX_EMPTY);
    236 
    237 	clmpcc_select_channel(ch->ch_sc, old);
    238 }
    239 
    240 static int
    241 clmpcc_speed(sc, speed, cor, bpr)
    242 	struct clmpcc_softc *sc;
    243 	speed_t speed;
    244 	int *cor, *bpr;
    245 {
    246 	int c, co, br;
    247 
    248 	for (co = 0, c = 8; c <= 2048; co++, c *= 4) {
    249 		br = ((sc->sc_clk / c) / speed) - 1;
    250 		if ( br < 0x100 ) {
    251 			*cor = co;
    252 			*bpr = br;
    253 			return 0;
    254 		}
    255 	}
    256 
    257 	return -1;
    258 }
    259 
    260 void
    261 clmpcc_attach(sc)
    262 	struct clmpcc_softc *sc;
    263 {
    264 	struct clmpcc_chan *ch;
    265 	struct tty *tp;
    266 	int chan;
    267 
    268 	if ( cons_sc != NULL &&
    269 	     sc->sc_iot == cons_sc->sc_iot && sc->sc_ioh == cons_sc->sc_ioh )
    270 		cons_sc = sc;
    271 
    272 	/* Initialise the chip */
    273 	clmpcc_init(sc);
    274 
    275 	printf(": Cirrus Logic CD240%c Serial Controller\n",
    276 		(clmpcc_rd_msvr(sc) & CLMPCC_MSVR_PORT_ID) ? '0' : '1');
    277 
    278 	sc->sc_soft_running = 0;
    279 	memset(&(sc->sc_chans[0]), 0, sizeof(sc->sc_chans));
    280 
    281 	for (chan = 0; chan < CLMPCC_NUM_CHANS; chan++) {
    282 		ch = &sc->sc_chans[chan];
    283 
    284 		ch->ch_sc = sc;
    285 		ch->ch_car = chan;
    286 
    287 		tp = ttymalloc();
    288 		tp->t_oproc = clmpcc_start;
    289 		tp->t_param = clmpcc_param;
    290 
    291 		ch->ch_tty = tp;
    292 
    293 		ch->ch_ibuf = malloc(clmpcc_ibuf_size * 2, M_DEVBUF, M_NOWAIT);
    294 		if ( ch->ch_ibuf == NULL ) {
    295 			printf("%s(%d): unable to allocate ring buffer\n",
    296 		    		sc->sc_dev.dv_xname, chan);
    297 			return;
    298 		}
    299 
    300 		ch->ch_ibuf_end = &(ch->ch_ibuf[clmpcc_ibuf_size * 2]);
    301 		ch->ch_ibuf_rd = ch->ch_ibuf_wr = ch->ch_ibuf;
    302 
    303 		tty_attach(tp);
    304 	}
    305 
    306 	printf("%s: %d channels available", sc->sc_dev.dv_xname,
    307 					    CLMPCC_NUM_CHANS);
    308 	if ( cons_sc == sc ) {
    309 		printf(", console on channel %d.\n", cons_chan);
    310 		SET(sc->sc_chans[cons_chan].ch_flags, CLMPCC_FLG_IS_CONSOLE);
    311 		SET(sc->sc_chans[cons_chan].ch_openflags, TIOCFLAG_SOFTCAR);
    312 	} else
    313 		printf(".\n");
    314 }
    315 
    316 static int
    317 clmpcc_init(sc)
    318 	struct clmpcc_softc *sc;
    319 {
    320 	u_int tcor, tbpr;
    321 	u_int rcor, rbpr;
    322 	u_int msvr_rts, msvr_dtr;
    323 	u_int ccr;
    324 	int is_console;
    325 	int i;
    326 
    327 	/*
    328 	 * All we're really concerned about here is putting the chip
    329 	 * into a quiescent state so that it won't do anything until
    330 	 * clmpccopen() is called. (Except the console channel.)
    331 	 */
    332 
    333 	/*
    334 	 * If the chip is acting as console, set all channels to the supplied
    335 	 * console baud rate. Otherwise, plump for 9600.
    336 	 */
    337 	if ( cons_sc &&
    338 	     sc->sc_ioh == cons_sc->sc_ioh && sc->sc_iot == cons_sc->sc_iot ) {
    339 		clmpcc_speed(sc, cons_rate, &tcor, &tbpr);
    340 		clmpcc_speed(sc, cons_rate, &rcor, &rbpr);
    341 		is_console = 1;
    342 	} else {
    343 		clmpcc_speed(sc, 9600, &tcor, &tbpr);
    344 		clmpcc_speed(sc, 9600, &rcor, &rbpr);
    345 		is_console = 0;
    346 	}
    347 
    348 	/* Allow any pending output to be sent */
    349 	delay(10000);
    350 
    351 	/* Send the Reset All command  to channel 0 (resets all channels!) */
    352 	clmpcc_channel_cmd(sc, 0, CLMPCC_CCR_T0_RESET_ALL);
    353 
    354 	delay(1000);
    355 
    356 	/*
    357 	 * The chip will set it's firmware revision register to a non-zero
    358 	 * value to indicate completion of reset.
    359 	 */
    360 	for (i = 10000; clmpcc_rdreg(sc, CLMPCC_REG_GFRCR) == 0 && i; i--)
    361 		delay(1);
    362 
    363 	if ( i == 0 ) {
    364 		/*
    365 		 * Watch out... If this chip is console, the message
    366 		 * probably won't be sent since we just reset it!
    367 		 */
    368 		printf("%s: Failed to reset chip\n", sc->sc_dev.dv_xname);
    369 		return -1;
    370 	}
    371 
    372 	for (i = 0; i < CLMPCC_NUM_CHANS; i++) {
    373 		clmpcc_select_channel(sc, i);
    374 
    375 		/* All interrupts are disabled to begin with */
    376 		clmpcc_wrreg(sc, CLMPCC_REG_IER, 0);
    377 
    378 		/* Make sure the channel interrupts on the correct vectors */
    379 		clmpcc_wrreg(sc, CLMPCC_REG_LIVR, sc->sc_vector_base);
    380 		clmpcc_wr_pilr(sc, CLMPCC_REG_RPILR, sc->sc_rpilr);
    381 		clmpcc_wr_pilr(sc, CLMPCC_REG_TPILR, sc->sc_tpilr);
    382 		clmpcc_wr_pilr(sc, CLMPCC_REG_MPILR, sc->sc_mpilr);
    383 
    384 		/* Receive timer prescaler set to 1ms */
    385 		clmpcc_wrreg(sc, CLMPCC_REG_TPR,
    386 				 CLMPCC_MSEC_TO_TPR(sc->sc_clk, 1));
    387 
    388 		/* We support Async mode only */
    389 		clmpcc_wrreg(sc, CLMPCC_REG_CMR, CLMPCC_CMR_ASYNC);
    390 
    391 		/* Set the required baud rate */
    392 		clmpcc_wrreg(sc, CLMPCC_REG_TCOR, CLMPCC_TCOR_CLK(tcor));
    393 		clmpcc_wrreg(sc, CLMPCC_REG_TBPR, tbpr);
    394 		clmpcc_wrreg(sc, CLMPCC_REG_RCOR, CLMPCC_RCOR_CLK(rcor));
    395 		clmpcc_wrreg(sc, CLMPCC_REG_RBPR, rbpr);
    396 
    397 		/* Always default to 8N1 (XXX what about console?) */
    398 		clmpcc_wrreg(sc, CLMPCC_REG_COR1, CLMPCC_COR1_CHAR_8BITS |
    399 						  CLMPCC_COR1_NO_PARITY |
    400 						  CLMPCC_COR1_IGNORE_PAR);
    401 
    402 		clmpcc_wrreg(sc, CLMPCC_REG_COR2, 0);
    403 
    404 		clmpcc_wrreg(sc, CLMPCC_REG_COR3, CLMPCC_COR3_STOP_1);
    405 
    406 		clmpcc_wrreg(sc, CLMPCC_REG_COR4, CLMPCC_COR4_DSRzd |
    407 						  CLMPCC_COR4_CDzd |
    408 						  CLMPCC_COR4_CTSzd);
    409 
    410 		clmpcc_wrreg(sc, CLMPCC_REG_COR5, CLMPCC_COR5_DSRod |
    411 						  CLMPCC_COR5_CDod |
    412 						  CLMPCC_COR5_CTSod |
    413 						  CLMPCC_COR5_FLOW_NORM);
    414 
    415 		clmpcc_wrreg(sc, CLMPCC_REG_COR6, 0);
    416 		clmpcc_wrreg(sc, CLMPCC_REG_COR7, 0);
    417 
    418 		/* Set the receive FIFO timeout */
    419 		clmpcc_wrreg(sc, CLMPCC_REG_RTPRl, CLMPCC_RTPR_DEFAULT);
    420 		clmpcc_wrreg(sc, CLMPCC_REG_RTPRh, 0);
    421 
    422 		/* At this point, we set up the console differently */
    423 		if ( is_console && i == cons_chan ) {
    424 			msvr_rts = CLMPCC_MSVR_RTS;
    425 			msvr_dtr = CLMPCC_MSVR_DTR;
    426 			ccr = CLMPCC_CCR_T0_RX_EN | CLMPCC_CCR_T0_TX_EN;
    427 		} else {
    428 			msvr_rts = 0;
    429 			msvr_dtr = 0;
    430 			ccr = CLMPCC_CCR_T0_RX_DIS | CLMPCC_CCR_T0_TX_DIS;
    431 		}
    432 
    433 		clmpcc_wrreg(sc, CLMPCC_REG_MSVR_RTS, msvr_rts);
    434 		clmpcc_wrreg(sc, CLMPCC_REG_MSVR_DTR, msvr_dtr);
    435 		clmpcc_channel_cmd(sc, i, CLMPCC_CCR_T0_INIT | ccr);
    436 		delay(100);
    437 	}
    438 
    439 	return 0;
    440 }
    441 
    442 static void
    443 clmpcc_shutdown(ch)
    444 	struct clmpcc_chan *ch;
    445 {
    446 	int oldch;
    447 
    448 	oldch = clmpcc_select_channel(ch->ch_sc, ch->ch_car);
    449 
    450 	/* Turn off interrupts. */
    451 	clmpcc_wrreg(ch->ch_sc, CLMPCC_REG_IER, 0);
    452 
    453 	if ( ISCLR(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) ) {
    454 		/* Disable the transmitter and receiver */
    455 		clmpcc_channel_cmd(ch->ch_sc, ch->ch_car, CLMPCC_CCR_T0_RX_DIS |
    456 							  CLMPCC_CCR_T0_TX_DIS);
    457 
    458 		/* Drop RTS and DTR */
    459 		clmpcc_modem_control(ch, TIOCM_RTS | TIOCM_DTR, DMBIS);
    460 	}
    461 
    462 	clmpcc_select_channel(ch->ch_sc, oldch);
    463 }
    464 
    465 int
    466 clmpccopen(dev, flag, mode, p)
    467 	dev_t dev;
    468 	int flag, mode;
    469 	struct proc *p;
    470 {
    471 	struct clmpcc_softc *sc;
    472 	struct clmpcc_chan *ch;
    473 	struct tty *tp;
    474 	int oldch;
    475 	int error;
    476 	int unit;
    477 
    478 	if ( (unit = CLMPCCUNIT(dev)) >= clmpcc_cd.cd_ndevs ||
    479 	     (sc = clmpcc_cd.cd_devs[unit]) == NULL ) {
    480 		return ENXIO;
    481 	}
    482 
    483 	ch = &sc->sc_chans[CLMPCCCHAN(dev)];
    484 
    485 	tp = ch->ch_tty;
    486 
    487 	if ( ISSET(tp->t_state, TS_ISOPEN) &&
    488 	     ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0 )
    489 		return EBUSY;
    490 
    491 	/*
    492 	 * Do the following iff this is a first open.
    493 	 */
    494 	if ( ISCLR(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0 ) {
    495 
    496 		ttychars(tp);
    497 
    498 		tp->t_dev = dev;
    499 		tp->t_iflag = TTYDEF_IFLAG;
    500 		tp->t_oflag = TTYDEF_OFLAG;
    501 		tp->t_lflag = TTYDEF_LFLAG;
    502 		tp->t_cflag = TTYDEF_CFLAG;
    503 		tp->t_ospeed = tp->t_ispeed = TTYDEF_SPEED;
    504 
    505 		if ( ISSET(ch->ch_openflags, TIOCFLAG_CLOCAL) )
    506 			SET(tp->t_cflag, CLOCAL);
    507 		if ( ISSET(ch->ch_openflags, TIOCFLAG_CRTSCTS) )
    508 			SET(tp->t_cflag, CRTSCTS);
    509 		if ( ISSET(ch->ch_openflags, TIOCFLAG_MDMBUF) )
    510 			SET(tp->t_cflag, MDMBUF);
    511 
    512 		/*
    513 		 * Override some settings if the channel is being
    514 		 * used as the console.
    515 		 */
    516 		if ( ISSET(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) ) {
    517 			tp->t_ospeed = tp->t_ispeed = cons_rate;
    518 			SET(tp->t_cflag, CLOCAL);
    519 			CLR(tp->t_cflag, CRTSCTS);
    520 			CLR(tp->t_cflag, HUPCL);
    521 		}
    522 
    523 		ch->ch_control = 0;
    524 
    525 		clmpcc_param(tp, &tp->t_termios);
    526 		ttsetwater(tp);
    527 
    528 		/* Clear the input ring */
    529 		ch->ch_ibuf_rd = ch->ch_ibuf_wr = ch->ch_ibuf;
    530 
    531 		/* Select the channel */
    532 		oldch = clmpcc_select_channel(sc, ch->ch_car);
    533 
    534 		/* Reset it */
    535 		clmpcc_channel_cmd(sc, ch->ch_car, CLMPCC_CCR_T0_CLEAR |
    536 						   CLMPCC_CCR_T0_RX_EN |
    537 						   CLMPCC_CCR_T0_TX_EN);
    538 
    539 		/* Enable receiver and modem change interrupts. */
    540 		clmpcc_wrreg(sc, CLMPCC_REG_IER, CLMPCC_IER_MODEM |
    541 						 CLMPCC_IER_RET |
    542 						 CLMPCC_IER_RX_FIFO);
    543 
    544 		/* Raise RTS and DTR */
    545 		clmpcc_modem_control(ch, TIOCM_RTS | TIOCM_DTR, DMBIS);
    546 
    547 		clmpcc_select_channel(sc, oldch);
    548 	} else
    549 	if ( ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0 )
    550 		return EBUSY;
    551 
    552 	error = ttyopen(tp, CLMPCCDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    553 	if (error)
    554 		goto bad;
    555 
    556 	error = (*linesw[tp->t_line].l_open)(dev, tp);
    557 	if (error)
    558 		goto bad;
    559 
    560 	return 0;
    561 
    562 bad:
    563 	if ( ISCLR(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0 ) {
    564 		/*
    565 		 * We failed to open the device, and nobody else had it opened.
    566 		 * Clean up the state as appropriate.
    567 		 */
    568 		clmpcc_shutdown(ch);
    569 	}
    570 
    571 	return error;
    572 }
    573 
    574 int
    575 clmpccclose(dev, flag, mode, p)
    576 	dev_t dev;
    577 	int flag, mode;
    578 	struct proc *p;
    579 {
    580 	struct clmpcc_softc	*sc = clmpcc_cd.cd_devs[CLMPCCUNIT(dev)];
    581 	struct clmpcc_chan	*ch = &sc->sc_chans[CLMPCCCHAN(dev)];
    582 	struct tty		*tp = ch->ch_tty;
    583 	int s;
    584 
    585 	if ( ISCLR(tp->t_state, TS_ISOPEN) )
    586 		return 0;
    587 
    588 	(*linesw[tp->t_line].l_close)(tp, flag);
    589 
    590 	s = spltty();
    591 
    592 	if ( ISCLR(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0 ) {
    593 		/*
    594 		 * Although we got a last close, the device may still be in
    595 		 * use; e.g. if this was the dialout node, and there are still
    596 		 * processes waiting for carrier on the non-dialout node.
    597 		 */
    598 		clmpcc_shutdown(ch);
    599 	}
    600 
    601 	ttyclose(tp);
    602 
    603 	splx(s);
    604 
    605 	return 0;
    606 }
    607 
    608 int
    609 clmpccread(dev, uio, flag)
    610 	dev_t dev;
    611 	struct uio *uio;
    612 	int flag;
    613 {
    614 	struct clmpcc_softc *sc = clmpcc_cd.cd_devs[CLMPCCUNIT(dev)];
    615 	struct tty *tp = sc->sc_chans[CLMPCCCHAN(dev)].ch_tty;
    616 
    617 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    618 }
    619 
    620 int
    621 clmpccwrite(dev, uio, flag)
    622 	dev_t dev;
    623 	struct uio *uio;
    624 	int flag;
    625 {
    626 	struct clmpcc_softc *sc = clmpcc_cd.cd_devs[CLMPCCUNIT(dev)];
    627 	struct tty *tp = sc->sc_chans[CLMPCCCHAN(dev)].ch_tty;
    628 
    629 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    630 }
    631 
    632 struct tty *
    633 clmpcctty(dev)
    634 	dev_t dev;
    635 {
    636 	struct clmpcc_softc *sc = clmpcc_cd.cd_devs[CLMPCCUNIT(dev)];
    637 
    638 	return (sc->sc_chans[CLMPCCCHAN(dev)].ch_tty);
    639 }
    640 
    641 int
    642 clmpccioctl(dev, cmd, data, flag, p)
    643 	dev_t dev;
    644 	u_long cmd;
    645 	caddr_t data;
    646 	int flag;
    647 	struct proc *p;
    648 {
    649 	struct clmpcc_softc *sc = clmpcc_cd.cd_devs[CLMPCCUNIT(dev)];
    650 	struct clmpcc_chan *ch = &sc->sc_chans[CLMPCCCHAN(dev)];
    651 	struct tty *tp = ch->ch_tty;
    652 	int error;
    653 
    654 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    655 	if (error >= 0)
    656 		return error;
    657 
    658 	error = ttioctl(tp, cmd, data, flag, p);
    659 	if (error >= 0)
    660 		return error;
    661 
    662 	error = 0;
    663 
    664 	switch (cmd) {
    665 	case TIOCSBRK:
    666 		SET(ch->ch_flags, CLMPCC_FLG_START_BREAK);
    667 		clmpcc_enable_transmitter(ch);
    668 		break;
    669 
    670 	case TIOCCBRK:
    671 		SET(ch->ch_flags, CLMPCC_FLG_END_BREAK);
    672 		clmpcc_enable_transmitter(ch);
    673 		break;
    674 
    675 	case TIOCSDTR:
    676 		clmpcc_modem_control(ch, TIOCM_DTR, DMBIS);
    677 		break;
    678 
    679 	case TIOCCDTR:
    680 		clmpcc_modem_control(ch, TIOCM_DTR, DMBIC);
    681 		break;
    682 
    683 	case TIOCMSET:
    684 		clmpcc_modem_control(ch, *((int *)data), DMSET);
    685 		break;
    686 
    687 	case TIOCMBIS:
    688 		clmpcc_modem_control(ch, *((int *)data), DMBIS);
    689 		break;
    690 
    691 	case TIOCMBIC:
    692 		clmpcc_modem_control(ch, *((int *)data), DMBIC);
    693 		break;
    694 
    695 	case TIOCMGET:
    696 		*((int *)data) = clmpcc_modem_control(ch, 0, DMGET);
    697 		break;
    698 
    699 	case TIOCGFLAGS:
    700 		*((int *)data) = ch->ch_openflags;
    701 		break;
    702 
    703 	case TIOCSFLAGS:
    704 		error = suser(p->p_ucred, &p->p_acflag);
    705 		if ( error )
    706 			break;
    707 		ch->ch_openflags = *((int *)data) &
    708 			(TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
    709 			 TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
    710 		if ( ISSET(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) )
    711 			SET(ch->ch_openflags, TIOCFLAG_SOFTCAR);
    712 		break;
    713 
    714 	default:
    715 		error = ENOTTY;
    716 		break;
    717 	}
    718 
    719 	return error;
    720 }
    721 
    722 int
    723 clmpcc_modem_control(ch, bits, howto)
    724 	struct clmpcc_chan *ch;
    725 	int bits;
    726 	int howto;
    727 {
    728 	struct clmpcc_softc *sc = ch->ch_sc;
    729 	struct tty *tp = ch->ch_tty;
    730 	int oldch;
    731 	int msvr;
    732 	int rbits = 0;
    733 
    734 	oldch = clmpcc_select_channel(sc, ch->ch_car);
    735 
    736 	switch ( howto ) {
    737 	case DMGET:
    738 		msvr = clmpcc_rd_msvr(sc);
    739 
    740 		if ( sc->sc_swaprtsdtr ) {
    741 			rbits |= (msvr & CLMPCC_MSVR_RTS) ? TIOCM_DTR : 0;
    742 			rbits |= (msvr & CLMPCC_MSVR_DTR) ? TIOCM_RTS : 0;
    743 		} else {
    744 			rbits |= (msvr & CLMPCC_MSVR_RTS) ? TIOCM_RTS : 0;
    745 			rbits |= (msvr & CLMPCC_MSVR_DTR) ? TIOCM_DTR : 0;
    746 		}
    747 
    748 		rbits |= (msvr & CLMPCC_MSVR_CTS) ? TIOCM_CTS : 0;
    749 		rbits |= (msvr & CLMPCC_MSVR_CD)  ? TIOCM_CD  : 0;
    750 		rbits |= (msvr & CLMPCC_MSVR_DSR) ? TIOCM_DSR : 0;
    751 		break;
    752 
    753 	case DMSET:
    754 		if ( sc->sc_swaprtsdtr ) {
    755 		    if ( ISCLR(tp->t_cflag, CRTSCTS) )
    756 			clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_DTR,
    757 					bits & TIOCM_RTS ? CLMPCC_MSVR_DTR : 0);
    758 		    clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_RTS,
    759 				bits & TIOCM_DTR ? CLMPCC_MSVR_RTS : 0);
    760 		} else {
    761 		    if ( ISCLR(tp->t_cflag, CRTSCTS) )
    762 			clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_RTS,
    763 					bits & TIOCM_RTS ? CLMPCC_MSVR_RTS : 0);
    764 		    clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_DTR,
    765 				bits & TIOCM_DTR ? CLMPCC_MSVR_DTR : 0);
    766 		}
    767 		break;
    768 
    769 	case DMBIS:
    770 		if ( sc->sc_swaprtsdtr ) {
    771 		    if ( ISCLR(tp->t_cflag, CRTSCTS) && ISSET(bits, TIOCM_RTS) )
    772 			clmpcc_wr_msvr(sc,CLMPCC_REG_MSVR_DTR, CLMPCC_MSVR_DTR);
    773 		    if ( ISSET(bits, TIOCM_DTR) )
    774 			clmpcc_wr_msvr(sc,CLMPCC_REG_MSVR_RTS, CLMPCC_MSVR_RTS);
    775 		} else {
    776 		    if ( ISCLR(tp->t_cflag, CRTSCTS) && ISSET(bits, TIOCM_RTS) )
    777 			clmpcc_wr_msvr(sc,CLMPCC_REG_MSVR_RTS, CLMPCC_MSVR_RTS);
    778 		    if ( ISSET(bits, TIOCM_DTR) )
    779 			clmpcc_wr_msvr(sc,CLMPCC_REG_MSVR_DTR, CLMPCC_MSVR_DTR);
    780 		}
    781 		break;
    782 
    783 	case DMBIC:
    784 		if ( sc->sc_swaprtsdtr ) {
    785 		    if ( ISCLR(tp->t_cflag, CRTSCTS) && ISCLR(bits, TIOCM_RTS) )
    786 			clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_DTR, 0);
    787 		    if ( ISCLR(bits, TIOCM_DTR) )
    788 			clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_RTS, 0);
    789 		} else {
    790 		    if ( ISCLR(tp->t_cflag, CRTSCTS) && ISCLR(bits, TIOCM_RTS) )
    791 			clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_RTS, 0);
    792 		    if ( ISCLR(bits, TIOCM_DTR) )
    793 			clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_DTR, 0);
    794 		}
    795 		break;
    796 	}
    797 
    798 	clmpcc_select_channel(sc, oldch);
    799 
    800 	return rbits;
    801 }
    802 
    803 static int
    804 clmpcc_param(tp, t)
    805 	struct tty *tp;
    806 	struct termios *t;
    807 {
    808 	struct clmpcc_softc *sc = clmpcc_cd.cd_devs[CLMPCCUNIT(tp->t_dev)];
    809 	struct clmpcc_chan *ch = &sc->sc_chans[CLMPCCCHAN(tp->t_dev)];
    810 	int oclk, obpr;
    811 	int iclk, ibpr;
    812 	int oldch;
    813 	int cor;
    814 	int ier;
    815 	int s;
    816 
    817 	/* Check requested parameters. */
    818 	if ( t->c_ospeed && clmpcc_speed(sc, t->c_ospeed, &oclk, &obpr) < 0 )
    819 		return EINVAL;
    820 
    821 	if ( t->c_ispeed && clmpcc_speed(sc, t->c_ispeed, &iclk, &ibpr) < 0 )
    822 		return EINVAL;
    823 
    824 	oldch = clmpcc_select_channel(sc, ch->ch_car);
    825 
    826 	s = splhigh();
    827 	/* Disable channel interrupt while we do all this */
    828 	ier = clmpcc_rdreg(sc, CLMPCC_REG_IER);
    829 	clmpcc_wrreg(sc, CLMPCC_REG_IER, 0);
    830 	splx(s);
    831 
    832 	/*
    833 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    834 	 * is always active.
    835 	 */
    836 	if ( ISSET(ch->ch_openflags, TIOCFLAG_SOFTCAR) ||
    837 	     ISSET(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) ) {
    838 		SET(t->c_cflag, CLOCAL);
    839 		CLR(t->c_cflag, HUPCL);
    840 	}
    841 
    842 	/* If ospeed it zero, hangup the line */
    843 	clmpcc_modem_control(ch, TIOCM_DTR, t->c_ospeed == 0 ? DMBIC : DMBIS);
    844 
    845 	if ( t->c_ospeed ) {
    846 		clmpcc_wrreg(sc, CLMPCC_REG_TCOR, CLMPCC_TCOR_CLK(oclk));
    847 		clmpcc_wrreg(sc, CLMPCC_REG_TBPR, obpr);
    848 	}
    849 
    850 	if ( t->c_ispeed ) {
    851 		clmpcc_wrreg(sc, CLMPCC_REG_RCOR, CLMPCC_RCOR_CLK(iclk));
    852 		clmpcc_wrreg(sc, CLMPCC_REG_RBPR, ibpr);
    853 	}
    854 
    855 	/* Work out value to use for COR1 */
    856 	cor = 0;
    857 	if ( ISSET(t->c_cflag, PARENB) ) {
    858 		cor |= CLMPCC_COR1_NORM_PARITY;
    859 		if ( ISSET(t->c_cflag, PARODD) )
    860 			cor |= CLMPCC_COR1_ODD_PARITY;
    861 	}
    862 
    863 	if ( ISCLR(t->c_cflag, INPCK) )
    864 		cor |= CLMPCC_COR1_IGNORE_PAR;
    865 
    866 	switch ( t->c_cflag & CSIZE ) {
    867 	  case CS5:
    868 		cor |= CLMPCC_COR1_CHAR_5BITS;
    869 		break;
    870 
    871 	  case CS6:
    872 		cor |= CLMPCC_COR1_CHAR_6BITS;
    873 		break;
    874 
    875 	  case CS7:
    876 		cor |= CLMPCC_COR1_CHAR_7BITS;
    877 		break;
    878 
    879 	  case CS8:
    880 		cor |= CLMPCC_COR1_CHAR_8BITS;
    881 		break;
    882 	}
    883 
    884 	clmpcc_wrreg(sc, CLMPCC_REG_COR1, cor);
    885 
    886 	/*
    887 	 * The only interesting bit in COR2 is 'CTS Automatic Enable'
    888 	 * when hardware flow control is in effect.
    889 	 */
    890 	cor = ISSET(t->c_cflag, CRTSCTS) ? CLMPCC_COR2_CtsAE : 0;
    891 	clmpcc_wrreg(sc, CLMPCC_REG_COR2, cor);
    892 
    893 	/* COR3 needs to be set to the number of stop bits... */
    894 	cor = ISSET(t->c_cflag, CSTOPB) ? CLMPCC_COR3_STOP_2 :
    895 					  CLMPCC_COR3_STOP_1;
    896 	clmpcc_wrreg(sc, CLMPCC_REG_COR3, cor);
    897 
    898 	/*
    899 	 * COR4 contains the FIFO threshold setting.
    900 	 * We adjust the threshold depending on the input speed...
    901 	 */
    902 	cor = clmpcc_rdreg(sc, CLMPCC_REG_COR4) & ~CLMPCC_COR4_FIFO_MASK;
    903 	if ( t->c_ispeed <= 1200 )
    904 		ch->ch_fifo = CLMPCC_COR4_FIFO_LOW;
    905 	else if ( t->c_ispeed <= 19200 )
    906 		ch->ch_fifo = CLMPCC_COR4_FIFO_MED;
    907 	else
    908 		ch->ch_fifo = CLMPCC_COR4_FIFO_HIGH;
    909 	clmpcc_wrreg(sc, CLMPCC_REG_COR4, cor | ch->ch_fifo);
    910 
    911 	/* This ensure the new fifo threshold causes an initial interrupt */
    912 	ier |= CLMPCC_IER_RET;
    913 	CLR(ch->ch_flags, CLMPCC_FLG_FIFO_CLEAR);
    914 
    915 	/*
    916 	 * If chip is used with CTS and DTR swapped, we can enable
    917 	 * automatic hardware flow control.
    918 	 */
    919 	cor = clmpcc_rdreg(sc, CLMPCC_REG_COR5) & ~CLMPCC_COR5_FLOW_MASK;
    920 	if ( sc->sc_swaprtsdtr && ISSET(t->c_cflag, CRTSCTS) )
    921 		cor |= CLMPCC_COR5_FLOW_NORM;
    922 	clmpcc_wrreg(sc, CLMPCC_REG_COR5, cor);
    923 
    924 	/* The chip needs to be told that registers have changed... */
    925 	clmpcc_channel_cmd(sc, ch->ch_car, CLMPCC_CCR_T0_INIT |
    926 					   CLMPCC_CCR_T0_RX_EN |
    927 					   CLMPCC_CCR_T0_TX_EN);
    928 
    929 	/* Restore channel interrupts */
    930 	clmpcc_wrreg(sc, CLMPCC_REG_IER, ier);
    931 
    932 	/*
    933 	 * Update the tty layer's idea of the carrier bit, in case we changed
    934 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
    935 	 * explicit request.
    936 	 */
    937 	cor = clmpcc_rd_msvr(sc) & CLMPCC_MSVR_CD;
    938 	(void) (*linesw[tp->t_line].l_modem)(tp, cor != 0);
    939 
    940 	clmpcc_select_channel(sc, oldch);
    941 
    942 	return 0;
    943 }
    944 
    945 static void
    946 clmpcc_start(tp)
    947 	struct tty *tp;
    948 {
    949 	struct clmpcc_softc *sc = clmpcc_cd.cd_devs[CLMPCCUNIT(tp->t_dev)];
    950 	struct clmpcc_chan *ch = &sc->sc_chans[CLMPCCCHAN(tp->t_dev)];
    951 	int s;
    952 
    953 	s = spltty();
    954 
    955 	if ( ISCLR(tp->t_state, TS_TTSTOP | TS_TIMEOUT | TS_BUSY) ) {
    956 		if ( tp->t_outq.c_cc <= tp->t_lowat ) {
    957 			if ( ISSET(tp->t_state, TS_ASLEEP) ) {
    958 				CLR(tp->t_state, TS_ASLEEP);
    959 				wakeup(&tp->t_outq);
    960 			}
    961 			selwakeup(&tp->t_wsel);
    962 
    963 			if ( tp->t_outq.c_cc == 0 )
    964 				goto out;
    965 		}
    966 		SET(tp->t_state, TS_BUSY);
    967 		clmpcc_enable_transmitter(ch);
    968 	}
    969 
    970 out:
    971 	splx(s);
    972 }
    973 
    974 /*
    975  * Stop output on a line.
    976  */
    977 void
    978 clmpccstop(tp, flag)
    979 	struct tty *tp;
    980 	int flag;
    981 {
    982 	struct clmpcc_softc *sc = clmpcc_cd.cd_devs[CLMPCCUNIT(tp->t_dev)];
    983 	struct clmpcc_chan *ch = &sc->sc_chans[CLMPCCCHAN(tp->t_dev)];
    984 	int s;
    985 
    986 	s = spltty();
    987 
    988 	if ( ISSET(tp->t_state, TS_BUSY) ) {
    989 		if ( ISCLR(tp->t_state, TS_TTSTOP) )
    990 			SET(tp->t_state, TS_FLUSH);
    991 
    992 		/*
    993 		 * The transmit interrupt routine will disable transmit when it
    994 		 * notices that CLMPCC_FLG_STOP has been set.
    995 		 */
    996 		SET(ch->ch_flags, CLMPCC_FLG_STOP);
    997 	}
    998 	splx(s);
    999 }
   1000 
   1001 /*
   1002  * RX interrupt routine
   1003  */
   1004 int
   1005 clmpcc_rxintr(arg)
   1006 	void *arg;
   1007 {
   1008 	struct clmpcc_softc *sc = (struct clmpcc_softc *)arg;
   1009 	struct clmpcc_chan *ch;
   1010 	u_int8_t *put, *end, rxd;
   1011 	u_char errstat;
   1012 	u_int fc, tc;
   1013 	int risr;
   1014 	int rir;
   1015 #ifdef DDB
   1016 	int saw_break = 0;
   1017 #endif
   1018 
   1019 	/* Receive interrupt active? */
   1020 	rir = clmpcc_rdreg(sc, CLMPCC_REG_RIR);
   1021 
   1022 	/*
   1023 	 * If we're using auto-vectored interrupts, we have to
   1024 	 * verify if the chip is generating the interrupt.
   1025 	 */
   1026 	if ( sc->sc_vector_base == 0 && (rir & CLMPCC_RIR_RACT) == 0 )
   1027 		return 0;
   1028 
   1029 	/* Get pointer to interrupting channel's data structure */
   1030 	ch = &sc->sc_chans[rir & CLMPCC_RIR_RCN_MASK];
   1031 
   1032 	/* Get the interrupt status register */
   1033 	risr = clmpcc_rdreg(sc, CLMPCC_REG_RISRl);
   1034 	if ( risr & CLMPCC_RISR_TIMEOUT ) {
   1035 		u_char reg;
   1036 		/*
   1037 		 * Set the FIFO threshold to zero, and disable
   1038 		 * further receive timeout interrupts.
   1039 		 */
   1040 		reg = clmpcc_rdreg(sc, CLMPCC_REG_COR4);
   1041 		clmpcc_wrreg(sc, CLMPCC_REG_COR4, reg & CLMPCC_COR4_FIFO_MASK);
   1042 		reg = clmpcc_rdreg(sc, CLMPCC_REG_IER);
   1043 		clmpcc_wrreg(sc, CLMPCC_REG_IER, reg & ~CLMPCC_IER_RET);
   1044 		clmpcc_wrreg(sc, CLMPCC_REG_REOIR, CLMPCC_REOIR_NO_TRANS);
   1045 		SET(ch->ch_flags, CLMPCC_FLG_FIFO_CLEAR);
   1046 		return 1;
   1047 	}
   1048 
   1049 	/* How many bytes are waiting in the FIFO?  */
   1050 	fc = tc = clmpcc_rdreg(sc, CLMPCC_REG_RFOC) & CLMPCC_RFOC_MASK;
   1051 
   1052 #ifdef DDB
   1053 	/*
   1054 	 * Allow BREAK on the console to drop to the debugger.
   1055 	 */
   1056 	if ( ISSET(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) &&
   1057 	     risr & CLMPCC_RISR_BREAK ) {
   1058 		saw_break = 1;
   1059 	}
   1060 #endif
   1061 
   1062 	if ( ISCLR(ch->ch_tty->t_state, TS_ISOPEN) && fc ) {
   1063 		/* Just get rid of the data */
   1064 		while ( fc-- )
   1065 			(void) clmpcc_rd_rxdata(sc);
   1066 		goto rx_done;
   1067 	}
   1068 
   1069 	put = ch->ch_ibuf_wr;
   1070 	end = ch->ch_ibuf_end;
   1071 
   1072 	/*
   1073 	 * Note: The chip is completely hosed WRT these error
   1074 	 *       conditions; there seems to be no way to associate
   1075 	 *       the error with the correct character in the FIFO.
   1076 	 *       We compromise by tagging the first character we read
   1077 	 *       with the error. Not perfect, but there's no other way.
   1078 	 */
   1079 	errstat = 0;
   1080 	if ( risr & CLMPCC_RISR_PARITY )
   1081 		errstat |= TTY_PE;
   1082 	if ( risr & (CLMPCC_RISR_FRAMING | CLMPCC_RISR_BREAK) )
   1083 		errstat |= TTY_FE;
   1084 
   1085 	/*
   1086 	 * As long as there are characters in the FIFO, and we
   1087 	 * have space for them...
   1088 	 */
   1089 	while ( fc > 0 ) {
   1090 
   1091 		*put++ = rxd = clmpcc_rd_rxdata(sc);
   1092 		*put++ = errstat;
   1093 
   1094 		if ( put >= end )
   1095 			put = ch->ch_ibuf;
   1096 
   1097 		if ( put == ch->ch_ibuf_rd ) {
   1098 			put -= 2;
   1099 			if ( put < ch->ch_ibuf )
   1100 				put = end - 2;
   1101 		}
   1102 
   1103 		errstat = 0;
   1104 		fc--;
   1105 	}
   1106 
   1107 	ch->ch_ibuf_wr = put;
   1108 
   1109 #if 0
   1110 	if ( sc->sc_swaprtsdtr == 0 &&
   1111 	     ISSET(cy->cy_tty->t_cflag, CRTSCTS) && cc < ch->ch_r_hiwat) {
   1112 		/*
   1113 		 * If RTS/DTR are not physically swapped, we have to
   1114 		 * do hardware flow control manually
   1115 		 */
   1116 		clmpcc_wr_msvr(sc, CLMPCC_MSVR_RTS, 0);
   1117 	}
   1118 #endif
   1119 
   1120 rx_done:
   1121 	if ( fc != tc ) {
   1122 		if ( ISSET(ch->ch_flags, CLMPCC_FLG_FIFO_CLEAR) ) {
   1123 			u_char reg;
   1124 			/*
   1125 			 * Set the FIFO threshold to the preset value,
   1126 			 * and enable receive timeout interrupts.
   1127 			 */
   1128 			reg = clmpcc_rdreg(sc, CLMPCC_REG_COR4);
   1129 			reg = (reg & ~CLMPCC_COR4_FIFO_MASK) | ch->ch_fifo;
   1130 			clmpcc_wrreg(sc, CLMPCC_REG_COR4, reg);
   1131 			reg = clmpcc_rdreg(sc, CLMPCC_REG_IER);
   1132 			clmpcc_wrreg(sc, CLMPCC_REG_IER, reg | CLMPCC_IER_RET);
   1133 			CLR(ch->ch_flags, CLMPCC_FLG_FIFO_CLEAR);
   1134 		}
   1135 
   1136 		clmpcc_wrreg(sc, CLMPCC_REG_REOIR, 0);
   1137 		if ( sc->sc_soft_running == 0 ) {
   1138 			sc->sc_soft_running = 1;
   1139 			(sc->sc_softhook)(sc);
   1140 		}
   1141 	} else
   1142 		clmpcc_wrreg(sc, CLMPCC_REG_REOIR, CLMPCC_REOIR_NO_TRANS);
   1143 
   1144 #ifdef DDB
   1145 	/*
   1146 	 * Only =after= we write REOIR is it safe to drop to the debugger.
   1147 	 */
   1148 	if ( saw_break )
   1149 		Debugger();
   1150 #endif
   1151 
   1152 	return 1;
   1153 }
   1154 
   1155 /*
   1156  * Tx interrupt routine
   1157  */
   1158 int
   1159 clmpcc_txintr(arg)
   1160 	void *arg;
   1161 {
   1162 	struct clmpcc_softc *sc = (struct clmpcc_softc *)arg;
   1163 	struct clmpcc_chan *ch;
   1164 	struct tty *tp;
   1165 	int ftc, oftc;
   1166 	int tir;
   1167 
   1168 	/* Tx interrupt active? */
   1169 	tir = clmpcc_rdreg(sc, CLMPCC_REG_TIR);
   1170 
   1171 	/*
   1172 	 * If we're using auto-vectored interrupts, we have to
   1173 	 * verify if the chip is generating the interrupt.
   1174 	 */
   1175 	if ( sc->sc_vector_base == 0 && (tir & CLMPCC_TIR_TACT) == 0 )
   1176 		return 0;
   1177 
   1178 	/* Get pointer to interrupting channel's data structure */
   1179 	ch = &sc->sc_chans[tir & CLMPCC_TIR_TCN_MASK];
   1180 
   1181 	/* Dummy read of the interrupt status register */
   1182 	(void) clmpcc_rdreg(sc, CLMPCC_REG_TISR);
   1183 
   1184 	ftc = oftc = clmpcc_rdreg(sc, CLMPCC_REG_TFTC);
   1185 
   1186 	/* Stop transmitting if CLMPCC_FLG_STOP is set */
   1187 	tp = ch->ch_tty;
   1188 	if ( ISSET(ch->ch_flags, CLMPCC_FLG_STOP) )
   1189 		goto tx_done;
   1190 
   1191 	if ( tp->t_outq.c_cc > 0 ) {
   1192 		SET(tp->t_state, TS_BUSY);
   1193 		while (tp->t_outq.c_cc > 0 && ftc > 0 ) {
   1194 			clmpcc_wr_txdata(sc, getc(&tp->t_outq));
   1195 			ftc--;
   1196 		}
   1197 	} else {
   1198 		/*
   1199 		 * No data to send -- check if we should
   1200 		 * start/stop a break
   1201 		 */
   1202 		/*
   1203 		 * XXX does this cause too much delay before
   1204 		 * breaks?
   1205 		 */
   1206 		if ( ISSET(ch->ch_flags, CLMPCC_FLG_START_BREAK) ) {
   1207 			CLR(ch->ch_flags, CLMPCC_FLG_START_BREAK);
   1208 		}
   1209 
   1210 		if ( ISSET(ch->ch_flags, CLMPCC_FLG_END_BREAK) ) {
   1211 			CLR(ch->ch_flags, CLMPCC_FLG_END_BREAK);
   1212 		}
   1213 	}
   1214 
   1215 	if ( tp->t_outq.c_cc == 0 ) {
   1216 tx_done:
   1217 		/*
   1218 		 * No data to send or requested to stop.
   1219 		 * Disable transmit interrupt
   1220 		 */
   1221 		clmpcc_wrreg(ch->ch_sc, CLMPCC_REG_IER,
   1222 			clmpcc_rdreg(ch->ch_sc, CLMPCC_REG_IER) &
   1223 						~CLMPCC_IER_TX_EMPTY);
   1224 		CLR(ch->ch_flags, CLMPCC_FLG_STOP);
   1225 		CLR(tp->t_state, TS_BUSY);
   1226 	}
   1227 
   1228 	if ( tp->t_outq.c_cc <= tp->t_lowat )
   1229 		SET(ch->ch_flags, CLMPCC_FLG_START);
   1230 
   1231 	if ( ftc != oftc )
   1232 		clmpcc_wrreg(sc, CLMPCC_REG_TEOIR, 0);
   1233 	else
   1234 		clmpcc_wrreg(sc, CLMPCC_REG_TEOIR, CLMPCC_TEOIR_NO_TRANS);
   1235 
   1236 	return 1;
   1237 }
   1238 
   1239 /*
   1240  * Modem change interrupt routine
   1241  */
   1242 int
   1243 clmpcc_mdintr(arg)
   1244 	void *arg;
   1245 {
   1246 	struct clmpcc_softc *sc = (struct clmpcc_softc *)arg;
   1247 	int mir;
   1248 
   1249 	/* Modem status interrupt active? */
   1250 	mir = clmpcc_rdreg(sc, CLMPCC_REG_MIR);
   1251 
   1252 	/*
   1253 	 * If we're using auto-vectored interrupts, we have to
   1254 	 * verify if the chip is generating the interrupt.
   1255 	 */
   1256 	if ( sc->sc_vector_base == 0 && (mir & CLMPCC_MIR_MACT) == 0 )
   1257 		return 0;
   1258 
   1259 	/* Dummy read of the interrupt status register */
   1260 	(void) clmpcc_rdreg(sc, CLMPCC_REG_MISR);
   1261 
   1262 	/* Retrieve current status of modem lines. */
   1263 	sc->sc_chans[mir & CLMPCC_MIR_MCN_MASK].ch_control |=
   1264 		clmpcc_rd_msvr(sc) & CLMPCC_MSVR_CD;
   1265 
   1266 	clmpcc_wrreg(sc, CLMPCC_REG_MEOIR, 0);
   1267 
   1268 	if ( sc->sc_soft_running == 0 ) {
   1269 		sc->sc_soft_running = 1;
   1270 		(sc->sc_softhook)(sc);
   1271 	}
   1272 
   1273 	return 1;
   1274 }
   1275 
   1276 int
   1277 clmpcc_softintr(arg)
   1278 	void *arg;
   1279 {
   1280 	struct clmpcc_softc *sc = (struct clmpcc_softc *)arg;
   1281 	struct clmpcc_chan *ch;
   1282 	int (*rint) __P((int, struct tty *));
   1283 	u_char *get;
   1284 	u_int c;
   1285 	int chan;
   1286 
   1287 	sc->sc_soft_running = 0;
   1288 
   1289 	/* Handle Modem state changes too... */
   1290 
   1291 	for (chan = 0; chan < CLMPCC_NUM_CHANS; chan++) {
   1292 		ch = &sc->sc_chans[chan];
   1293 		get = ch->ch_ibuf_rd;
   1294 		rint = linesw[ch->ch_tty->t_line].l_rint;
   1295 
   1296 		/* Squirt buffered incoming data into the tty layer */
   1297 		while ( get != ch->ch_ibuf_wr ) {
   1298 			c = *get++;
   1299 			c |= ((u_int)*get++) << 8;
   1300 			(rint)(c, ch->ch_tty);
   1301 
   1302 			if ( get == ch->ch_ibuf_end )
   1303 				get = ch->ch_ibuf;
   1304 
   1305 			ch->ch_ibuf_rd = get;
   1306 		}
   1307 	}
   1308 
   1309 	return 0;
   1310 }
   1311 
   1312 
   1313 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
   1314 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
   1315 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
   1316 /*
   1317  * Following are all routines needed for a cd240x channel to act as console
   1318  */
   1319 int
   1320 clmpcc_cnattach(sc, chan, rate)
   1321 	struct clmpcc_softc *sc;
   1322 	int chan;
   1323 	int rate;
   1324 {
   1325 	cons_sc = sc;
   1326 	cons_chan = chan;
   1327 	cons_rate = rate;
   1328 
   1329 	return 0;
   1330 }
   1331 
   1332 /*
   1333  * The following functions are polled getc and putc routines, for console use.
   1334  */
   1335 static int
   1336 clmpcc_common_getc(sc, chan)
   1337 	struct clmpcc_softc *sc;
   1338 	int chan;
   1339 {
   1340 	u_char old_chan;
   1341 	u_char old_ier;
   1342 	u_char ch, rir, risr;
   1343 	int s;
   1344 
   1345 	s = splhigh();
   1346 
   1347 	old_chan = clmpcc_select_channel(sc, chan);
   1348 
   1349 	/*
   1350 	 * We have to put the channel into RX interrupt mode before
   1351 	 * trying to read the Rx data register. So save the previous
   1352 	 * interrupt mode.
   1353 	 */
   1354 	old_ier = clmpcc_rdreg(sc, CLMPCC_REG_IER);
   1355 	clmpcc_wrreg(sc, CLMPCC_REG_IER, CLMPCC_IER_RX_FIFO);
   1356 
   1357 	/* Loop until we get a character */
   1358 	for (;;) {
   1359 		/*
   1360 		 * The REN bit will be set in the Receive Interrupt Register
   1361 		 * when the CD240x has a character to process. Remember,
   1362 		 * the RACT bit won't be set until we generate an interrupt
   1363 		 * acknowledge cycle via the MD front-end.
   1364 		 */
   1365 		rir = clmpcc_rdreg(sc, CLMPCC_REG_RIR);
   1366 		if ( (rir & CLMPCC_RIR_REN) == 0 )
   1367 			continue;
   1368 
   1369 		/* Acknowledge the request */
   1370 		if ( sc->sc_iackhook )
   1371 			(sc->sc_iackhook)(sc, CLMPCC_IACK_RX);
   1372 
   1373 		/*
   1374 		 * Determine if the interrupt is for the required channel
   1375 		 * and if valid data is available.
   1376 		 */
   1377 		rir = clmpcc_rdreg(sc, CLMPCC_REG_RIR);
   1378 		risr = clmpcc_rdreg(sc, CLMPCC_REG_RISR);
   1379 		if ( (rir & CLMPCC_RIR_RCN_MASK) != chan ||
   1380 		     risr != 0 ) {
   1381 			/* Rx error, or BREAK */
   1382 			clmpcc_wrreg(sc, CLMPCC_REG_REOIR,
   1383 					 CLMPCC_REOIR_NO_TRANS);
   1384 		} else {
   1385 			/* Dummy read of the FIFO count register */
   1386 			(void) clmpcc_rdreg(sc, CLMPCC_REG_RFOC);
   1387 
   1388 			/* Fetch the received character */
   1389 			ch = clmpcc_rd_rxdata(sc);
   1390 
   1391 			clmpcc_wrreg(sc, CLMPCC_REG_REOIR, 0);
   1392 			break;
   1393 		}
   1394 	}
   1395 
   1396 	clmpcc_wrreg(sc, CLMPCC_REG_IER, old_ier);
   1397 	clmpcc_select_channel(sc, old_chan);
   1398 
   1399 	splx(s);
   1400 	return ch;
   1401 }
   1402 
   1403 
   1404 static void
   1405 clmpcc_common_putc(sc, chan, c)
   1406 	struct clmpcc_softc *sc;
   1407 	int chan;
   1408 	int c;
   1409 {
   1410 	u_char old_chan;
   1411 	int s = splhigh();
   1412 
   1413 	old_chan = clmpcc_select_channel(sc, chan);
   1414 
   1415 	clmpcc_wrreg(sc, CLMPCC_REG_SCHR4, c);
   1416 	clmpcc_wrreg(sc, CLMPCC_REG_STCR, CLMPCC_STCR_SSPC(4) |
   1417 					  CLMPCC_STCR_SND_SPC);
   1418 
   1419 	while ( clmpcc_rdreg(sc, CLMPCC_REG_STCR) != 0 )
   1420 		;
   1421 
   1422 	delay(5);
   1423 
   1424 	clmpcc_select_channel(sc, old_chan);
   1425 
   1426 	splx(s);
   1427 }
   1428 
   1429 int
   1430 clmpcccngetc(dev)
   1431 	dev_t dev;
   1432 {
   1433 	return clmpcc_common_getc(cons_sc, cons_chan);
   1434 }
   1435 
   1436 /*
   1437  * Console kernel output character routine.
   1438  */
   1439 void
   1440 clmpcccnputc(dev, c)
   1441 	dev_t dev;
   1442 	int c;
   1443 {
   1444 	if ( c == '\n' )
   1445 		clmpcc_common_putc(cons_sc, cons_chan, '\r');
   1446 
   1447 	clmpcc_common_putc(cons_sc, cons_chan, c);
   1448 }
   1449