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