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