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