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