Home | History | Annotate | Line # | Download | only in sa11x0
sa11x0_com.c revision 1.1
      1  1.1  rjs /*      $NetBSD: sa11x0_com.c,v 1.1 2001/07/08 23:37:52 rjs Exp $        */
      2  1.1  rjs 
      3  1.1  rjs /*-
      4  1.1  rjs  * Copyright (c) 1998, 1999, 2001 The NetBSD Foundation, Inc.
      5  1.1  rjs  * All rights reserved.
      6  1.1  rjs  *
      7  1.1  rjs  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  rjs  * by IWAMOTO Toshihiro.
      9  1.1  rjs  *
     10  1.1  rjs  * This code is derived from software contributed to The NetBSD Foundation
     11  1.1  rjs  * by Charles M. Hannum.
     12  1.1  rjs  *
     13  1.1  rjs  * Redistribution and use in source and binary forms, with or without
     14  1.1  rjs  * modification, are permitted provided that the following conditions
     15  1.1  rjs  * are met:
     16  1.1  rjs  * 1. Redistributions of source code must retain the above copyright
     17  1.1  rjs  *    notice, this list of conditions and the following disclaimer.
     18  1.1  rjs  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.1  rjs  *    notice, this list of conditions and the following disclaimer in the
     20  1.1  rjs  *    documentation and/or other materials provided with the distribution.
     21  1.1  rjs  * 3. All advertising materials mentioning features or use of this software
     22  1.1  rjs  *    must display the following acknowledgement:
     23  1.1  rjs  *        This product includes software developed by the NetBSD
     24  1.1  rjs  *        Foundation, Inc. and its contributors.
     25  1.1  rjs  * 4. Neither the name of The NetBSD Foundation nor the names of its
     26  1.1  rjs  *    contributors may be used to endorse or promote products derived
     27  1.1  rjs  *    from this software without specific prior written permission.
     28  1.1  rjs  *
     29  1.1  rjs  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     30  1.1  rjs  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     31  1.1  rjs  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     32  1.1  rjs  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     33  1.1  rjs  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     34  1.1  rjs  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     35  1.1  rjs  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     36  1.1  rjs  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     37  1.1  rjs  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     38  1.1  rjs  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     39  1.1  rjs  * POSSIBILITY OF SUCH DAMAGE.
     40  1.1  rjs  */
     41  1.1  rjs 
     42  1.1  rjs /*
     43  1.1  rjs  * Copyright (c) 1991 The Regents of the University of California.
     44  1.1  rjs  * All rights reserved.
     45  1.1  rjs  *
     46  1.1  rjs  * Redistribution and use in source and binary forms, with or without
     47  1.1  rjs  * modification, are permitted provided that the following conditions
     48  1.1  rjs  * are met:
     49  1.1  rjs  * 1. Redistributions of source code must retain the above copyright
     50  1.1  rjs  *    notice, this list of conditions and the following disclaimer.
     51  1.1  rjs  * 2. Redistributions in binary form must reproduce the above copyright
     52  1.1  rjs  *    notice, this list of conditions and the following disclaimer in the
     53  1.1  rjs  *    documentation and/or other materials provided with the distribution.
     54  1.1  rjs  * 3. All advertising materials mentioning features or use of this software
     55  1.1  rjs  *    must display the following acknowledgement:
     56  1.1  rjs  *	This product includes software developed by the University of
     57  1.1  rjs  *	California, Berkeley and its contributors.
     58  1.1  rjs  * 4. Neither the name of the University nor the names of its contributors
     59  1.1  rjs  *    may be used to endorse or promote products derived from this software
     60  1.1  rjs  *    without specific prior written permission.
     61  1.1  rjs  *
     62  1.1  rjs  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63  1.1  rjs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64  1.1  rjs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65  1.1  rjs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66  1.1  rjs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67  1.1  rjs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68  1.1  rjs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69  1.1  rjs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70  1.1  rjs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71  1.1  rjs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72  1.1  rjs  * SUCH DAMAGE.
     73  1.1  rjs  *
     74  1.1  rjs  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     75  1.1  rjs  */
     76  1.1  rjs 
     77  1.1  rjs #include "opt_com.h"
     78  1.1  rjs #include "opt_ddb.h"
     79  1.1  rjs #include "opt_ddbparam.h"
     80  1.1  rjs #include "opt_kgdb.h"
     81  1.1  rjs 
     82  1.1  rjs #include "rnd.h"
     83  1.1  rjs #if NRND > 0 && defined(RND_COM)
     84  1.1  rjs #include <sys/rnd.h>
     85  1.1  rjs #endif
     86  1.1  rjs 
     87  1.1  rjs #include <sys/param.h>
     88  1.1  rjs #include <sys/systm.h>
     89  1.1  rjs #include <sys/types.h>
     90  1.1  rjs #include <sys/conf.h>
     91  1.1  rjs #include <sys/file.h>
     92  1.1  rjs #include <sys/device.h>
     93  1.1  rjs #include <sys/kernel.h>
     94  1.1  rjs #include <sys/malloc.h>
     95  1.1  rjs #include <sys/tty.h>
     96  1.1  rjs #include <sys/uio.h>
     97  1.1  rjs #include <sys/vnode.h>
     98  1.1  rjs 
     99  1.1  rjs #include <dev/cons.h>
    100  1.1  rjs 
    101  1.1  rjs #include <machine/bus.h>
    102  1.1  rjs #include <arm/sa11x0/sa11x0_reg.h>
    103  1.1  rjs #include <arm/sa11x0/sa11x0_var.h>
    104  1.1  rjs #include <arm/sa11x0/sa11x0_comreg.h>
    105  1.1  rjs #include <arm/sa11x0/sa11x0_comvar.h>
    106  1.1  rjs 
    107  1.1  rjs #include "sacom.h"
    108  1.1  rjs 
    109  1.1  rjs cdev_decl(sacom);
    110  1.1  rjs 
    111  1.1  rjs static	int	sacom_match(struct device *, struct cfdata *, void *);
    112  1.1  rjs static	void	sacom_attach(struct device *, struct device *, void *);
    113  1.1  rjs static	void	sacom_filltx(struct sacom_softc *);
    114  1.1  rjs static	void	sacom_attach_subr(struct sacom_softc *);
    115  1.1  rjs #if defined(DDB) || defined(KGDB)
    116  1.1  rjs static	void	sacom_enable_debugport(struct sacom_softc *);
    117  1.1  rjs #endif
    118  1.1  rjs void		sacom_config(struct sacom_softc *);
    119  1.1  rjs void		sacom_shutdown(struct sacom_softc *);
    120  1.1  rjs static	u_int	cflag2cr0(tcflag_t);
    121  1.1  rjs int		sacomparam(struct tty *, struct termios *);
    122  1.1  rjs void		sacomstart(struct tty *);
    123  1.1  rjs void		sacomstop(struct tty *, int);
    124  1.1  rjs int		sacomhwiflow(struct tty *, int);
    125  1.1  rjs 
    126  1.1  rjs void		sacom_loadchannelregs(struct sacom_softc *);
    127  1.1  rjs void		sacom_hwiflow(struct sacom_softc *);
    128  1.1  rjs void		sacom_break(struct sacom_softc *, int);
    129  1.1  rjs void		sacom_modem(struct sacom_softc *, int);
    130  1.1  rjs void		tiocm_to_sacom(struct sacom_softc *, u_long, int);
    131  1.1  rjs int		sacom_to_tiocm(struct sacom_softc *);
    132  1.1  rjs void		sacom_iflush(struct sacom_softc *);
    133  1.1  rjs 
    134  1.1  rjs #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    135  1.1  rjs void 		sacomsoft(void *);
    136  1.1  rjs #else
    137  1.1  rjs void 		sacomsoft(void);
    138  1.1  rjs #endif
    139  1.1  rjs 
    140  1.1  rjs static inline void sacom_rxsoft(struct sacom_softc *, struct tty *);
    141  1.1  rjs static inline void sacom_txsoft(struct sacom_softc *, struct tty *);
    142  1.1  rjs static inline void sacom_stsoft(struct sacom_softc *, struct tty *);
    143  1.1  rjs static inline void sacom_schedrx(struct sacom_softc *);
    144  1.1  rjs 
    145  1.1  rjs 
    146  1.1  rjs #define COMUNIT_MASK	0x7ffff
    147  1.1  rjs #define COMDIALOUT_MASK	0x80000
    148  1.1  rjs 
    149  1.1  rjs #define COMUNIT(x)	(minor(x) & COMUNIT_MASK)
    150  1.1  rjs #define COMDIALOUT(x)	(minor(x) & COMDIALOUT_MASK)
    151  1.1  rjs 
    152  1.1  rjs #define COM_ISALIVE(sc)	((sc)->enabled != 0 && \
    153  1.1  rjs 			 ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
    154  1.1  rjs 
    155  1.1  rjs #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
    156  1.1  rjs #define COM_LOCK(sc)
    157  1.1  rjs #define COM_UNLOCK(sc)
    158  1.1  rjs 
    159  1.1  rjs #define SET(t, f)	(t) |= (f)
    160  1.1  rjs #define CLR(t, f)	(t) &= ~(f)
    161  1.1  rjs #define ISSET(t, f)	((t) & (f))
    162  1.1  rjs 
    163  1.1  rjs int		sacomintr(void *);
    164  1.1  rjs int		sacomcngetc(dev_t);
    165  1.1  rjs void		sacomcnputc(dev_t, int);
    166  1.1  rjs void		sacomcnpollc(dev_t, int);
    167  1.1  rjs 
    168  1.1  rjs void		sacomcnprobe(struct consdev *);
    169  1.1  rjs void		sacomcninit(struct consdev *);
    170  1.1  rjs 
    171  1.1  rjs extern struct bus_space sa11x0_bs_tag;
    172  1.1  rjs 
    173  1.1  rjs static bus_space_tag_t sacomconstag;
    174  1.1  rjs static bus_space_handle_t sacomconsioh;
    175  1.1  rjs static bus_addr_t sacomconsaddr = SACOM3_BASE; /* XXX */
    176  1.1  rjs 
    177  1.1  rjs static int sacomconsattached;
    178  1.1  rjs static int sacomconsrate;
    179  1.1  rjs static tcflag_t sacomconscflag;
    180  1.1  rjs 
    181  1.1  rjs struct cfattach sacom_ca = {
    182  1.1  rjs 	sizeof(struct sacom_softc), sacom_match, sacom_attach
    183  1.1  rjs };
    184  1.1  rjs extern struct cfdriver sacom_cd;
    185  1.1  rjs 
    186  1.1  rjs struct consdev sacomcons = {
    187  1.1  rjs 	NULL, NULL, sacomcngetc, sacomcnputc, sacomcnpollc, NULL,
    188  1.1  rjs 	NODEV, CN_NORMAL
    189  1.1  rjs };
    190  1.1  rjs 
    191  1.1  rjs #ifndef CONMODE
    192  1.1  rjs #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    193  1.1  rjs #endif
    194  1.1  rjs #ifndef CONSPEED
    195  1.1  rjs #define CONSPEED 9600
    196  1.1  rjs #endif
    197  1.1  rjs #ifndef CONADDR
    198  1.1  rjs #define CONADDR SACOM3_BASE
    199  1.1  rjs #endif
    200  1.1  rjs 
    201  1.1  rjs static int
    202  1.1  rjs sacom_match(parent, match, aux)
    203  1.1  rjs 	struct device *parent;
    204  1.1  rjs 	struct cfdata *match;
    205  1.1  rjs 	void *aux;
    206  1.1  rjs {
    207  1.1  rjs 	return (1);
    208  1.1  rjs }
    209  1.1  rjs 
    210  1.1  rjs void
    211  1.1  rjs sacom_attach(parent, self, aux)
    212  1.1  rjs 	struct device *parent;
    213  1.1  rjs 	struct device *self;
    214  1.1  rjs 	void *aux;
    215  1.1  rjs {
    216  1.1  rjs 	struct sacom_softc *sc = (struct sacom_softc*)self;
    217  1.1  rjs 	struct sa11x0_attach_args *sa = aux;
    218  1.1  rjs 
    219  1.1  rjs 	printf("\n");
    220  1.1  rjs 
    221  1.1  rjs 	sc->sc_iot = sa->sa_iot;
    222  1.1  rjs 	sc->sc_baseaddr = sa->sa_addr;
    223  1.1  rjs 
    224  1.1  rjs 	if(bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
    225  1.1  rjs 			&sc->sc_ioh)) {
    226  1.1  rjs 		printf("%s: unable to map registers\n", sc->sc_dev.dv_xname);
    227  1.1  rjs 		return;
    228  1.1  rjs 	}
    229  1.1  rjs 
    230  1.1  rjs 	printf("%s: ", sc->sc_dev.dv_xname);
    231  1.1  rjs 	switch(sc->sc_baseaddr) {
    232  1.1  rjs 	case 0x80050000:
    233  1.1  rjs 		printf("SA11x0 UART3\n");
    234  1.1  rjs 		break;
    235  1.1  rjs 	case 0x80010000:
    236  1.1  rjs 		printf("SA11x0 UART1\n");
    237  1.1  rjs 		break;
    238  1.1  rjs 	case 0x80030000:
    239  1.1  rjs 		printf("SA11x0 UART2 (IRDA)\n");
    240  1.1  rjs 		break;
    241  1.1  rjs 	default:
    242  1.1  rjs 		printf("unknown SA11x0 UART\n");
    243  1.1  rjs 		break;
    244  1.1  rjs 	}
    245  1.1  rjs 
    246  1.1  rjs 	sacom_attach_subr(sc);
    247  1.1  rjs 
    248  1.1  rjs 	sa11x0_intr_establish(0, sa->sa_intr, 1, IPL_SERIAL, sacomintr, sc);
    249  1.1  rjs }
    250  1.1  rjs 
    251  1.1  rjs void
    252  1.1  rjs sacom_attach_subr(sc)
    253  1.1  rjs 	struct sacom_softc *sc;
    254  1.1  rjs {
    255  1.1  rjs 	bus_addr_t iobase = sc->sc_baseaddr;
    256  1.1  rjs 	bus_space_tag_t iot = sc->sc_iot;
    257  1.1  rjs 	struct tty *tp;
    258  1.1  rjs 
    259  1.1  rjs #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(COM_MPLOCK)
    260  1.1  rjs 	simple_lock_init(&sc->sc_lock);
    261  1.1  rjs #endif
    262  1.1  rjs 
    263  1.1  rjs 	/* XXX Do we need to disable interrupts here? */
    264  1.1  rjs 
    265  1.1  rjs 	if (iot == sacomconstag && iobase == sacomconsaddr) {
    266  1.1  rjs 		sacomconsattached = 1;
    267  1.1  rjs 		sc->sc_speed = SACOMSPEED(sacomconsrate);
    268  1.1  rjs 
    269  1.1  rjs 		/* Make sure the console is always "hardwired". */
    270  1.1  rjs 		delay(10000);			/* wait for output to finish */
    271  1.1  rjs 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    272  1.1  rjs 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    273  1.1  rjs 	}
    274  1.1  rjs 
    275  1.1  rjs 	tp = ttymalloc();
    276  1.1  rjs 	tp->t_oproc = sacomstart;
    277  1.1  rjs 	tp->t_param = sacomparam;
    278  1.1  rjs 	tp->t_hwiflow = sacomhwiflow;
    279  1.1  rjs 
    280  1.1  rjs 	sc->sc_tty = tp;
    281  1.1  rjs 	sc->sc_rbuf = malloc(SACOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
    282  1.1  rjs 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    283  1.1  rjs 	sc->sc_rbavail = SACOM_RING_SIZE;
    284  1.1  rjs 	if (sc->sc_rbuf == NULL) {
    285  1.1  rjs 		printf("%s: unable to allocate ring buffer\n",
    286  1.1  rjs 		    sc->sc_dev.dv_xname);
    287  1.1  rjs 		return;
    288  1.1  rjs 	}
    289  1.1  rjs 	sc->sc_ebuf = sc->sc_rbuf + (SACOM_RING_SIZE << 1);
    290  1.1  rjs 	sc->sc_tbc = 0;
    291  1.1  rjs 
    292  1.1  rjs 	tty_attach(tp);
    293  1.1  rjs 
    294  1.1  rjs 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    295  1.1  rjs 		int maj;
    296  1.1  rjs 
    297  1.1  rjs 		/* locate the major number */
    298  1.1  rjs 		for (maj = 0; maj < nchrdev; maj++)
    299  1.1  rjs 			if (cdevsw[maj].d_open == sacomopen)
    300  1.1  rjs 				break;
    301  1.1  rjs 
    302  1.1  rjs 		cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
    303  1.1  rjs 
    304  1.1  rjs 		delay(10000); /* XXX */
    305  1.1  rjs 		printf("%s: console\n", sc->sc_dev.dv_xname);
    306  1.1  rjs 		delay(10000); /* XXX */
    307  1.1  rjs 	}
    308  1.1  rjs 
    309  1.1  rjs 
    310  1.1  rjs #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    311  1.1  rjs 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, sacomsoft, sc);
    312  1.1  rjs #endif
    313  1.1  rjs 
    314  1.1  rjs #if NRND > 0 && defined(RND_COM)
    315  1.1  rjs 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    316  1.1  rjs 			  RND_TYPE_TTY, 0);
    317  1.1  rjs #endif
    318  1.1  rjs 
    319  1.1  rjs 	/* if there are no enable/disable functions, assume the device
    320  1.1  rjs 	   is always enabled */
    321  1.1  rjs 	if (!sc->enable)
    322  1.1  rjs 		sc->enabled = 1;
    323  1.1  rjs 
    324  1.1  rjs 	sacom_config(sc);
    325  1.1  rjs 
    326  1.1  rjs 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
    327  1.1  rjs }
    328  1.1  rjs 
    329  1.1  rjs /* This is necessary when dynamically changing SAIP configuration. */
    330  1.1  rjs int
    331  1.1  rjs sacom_detach(self, flags)
    332  1.1  rjs 	struct device *self;
    333  1.1  rjs 	int flags;
    334  1.1  rjs {
    335  1.1  rjs 	struct sacom_softc *sc = (struct sacom_softc *)self;
    336  1.1  rjs 	int maj, mn;
    337  1.1  rjs 
    338  1.1  rjs 	/* locate the major number */
    339  1.1  rjs 	for (maj = 0; maj < nchrdev; maj++)
    340  1.1  rjs 		if (cdevsw[maj].d_open == sacomopen)
    341  1.1  rjs 			break;
    342  1.1  rjs 
    343  1.1  rjs 	/* Nuke the vnodes for any open instances. */
    344  1.1  rjs 	mn = self->dv_unit;
    345  1.1  rjs 	vdevgone(maj, mn, mn, VCHR);
    346  1.1  rjs 
    347  1.1  rjs 	mn |= COMDIALOUT_MASK;
    348  1.1  rjs 	vdevgone(maj, mn, mn, VCHR);
    349  1.1  rjs 
    350  1.1  rjs 	/* Free the receive buffer. */
    351  1.1  rjs 	free(sc->sc_rbuf, M_DEVBUF);
    352  1.1  rjs 
    353  1.1  rjs 	/* Detach and free the tty. */
    354  1.1  rjs 	tty_detach(sc->sc_tty);
    355  1.1  rjs 	ttyfree(sc->sc_tty);
    356  1.1  rjs 
    357  1.1  rjs #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    358  1.1  rjs 	/* Unhook the soft interrupt handler. */
    359  1.1  rjs 	softintr_disestablish(sc->sc_si);
    360  1.1  rjs #endif
    361  1.1  rjs 
    362  1.1  rjs #if NRND > 0 && defined(RND_COM)
    363  1.1  rjs 	/* Unhook the entropy source. */
    364  1.1  rjs 	rnd_detach_source(&sc->rnd_source);
    365  1.1  rjs #endif
    366  1.1  rjs 
    367  1.1  rjs 	return (0);
    368  1.1  rjs }
    369  1.1  rjs 
    370  1.1  rjs void
    371  1.1  rjs sacom_config(sc)
    372  1.1  rjs 	struct sacom_softc *sc;
    373  1.1  rjs {
    374  1.1  rjs 	bus_space_tag_t iot = sc->sc_iot;
    375  1.1  rjs 	bus_space_handle_t ioh = sc->sc_ioh;
    376  1.1  rjs 
    377  1.1  rjs 	/* Disable engine before configuring the device. */
    378  1.1  rjs 	sc->sc_cr3 = 0;
    379  1.1  rjs 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
    380  1.1  rjs 
    381  1.1  rjs #ifdef DDB
    382  1.1  rjs 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
    383  1.1  rjs 		sacom_enable_debugport(sc);
    384  1.1  rjs #endif
    385  1.1  rjs }
    386  1.1  rjs 
    387  1.1  rjs #ifdef DDB
    388  1.1  rjs static void
    389  1.1  rjs sacom_enable_debugport(sc)
    390  1.1  rjs 	struct sacom_softc *sc;
    391  1.1  rjs {
    392  1.1  rjs 	bus_space_tag_t iot = sc->sc_iot;
    393  1.1  rjs 	bus_space_handle_t ioh = sc->sc_ioh;
    394  1.1  rjs 	int s;
    395  1.1  rjs 
    396  1.1  rjs 	s = splserial();
    397  1.1  rjs 	COM_LOCK(sc);
    398  1.1  rjs 	sc->sc_cr3 = CR3_RXE | CR3_TXE;
    399  1.1  rjs 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
    400  1.1  rjs 	COM_UNLOCK(sc);
    401  1.1  rjs 	splx(s);
    402  1.1  rjs }
    403  1.1  rjs #endif
    404  1.1  rjs 
    405  1.1  rjs int
    406  1.1  rjs sacom_activate(self, act)
    407  1.1  rjs 	struct device *self;
    408  1.1  rjs 	enum devact act;
    409  1.1  rjs {
    410  1.1  rjs 	struct sacom_softc *sc = (struct sacom_softc *)self;
    411  1.1  rjs 	int s, rv = 0;
    412  1.1  rjs 
    413  1.1  rjs 	s = splserial();
    414  1.1  rjs 	COM_LOCK(sc);
    415  1.1  rjs 	switch (act) {
    416  1.1  rjs 	case DVACT_ACTIVATE:
    417  1.1  rjs 		rv = EOPNOTSUPP;
    418  1.1  rjs 		break;
    419  1.1  rjs 
    420  1.1  rjs 	case DVACT_DEACTIVATE:
    421  1.1  rjs 		if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB)) {
    422  1.1  rjs 			rv = EBUSY;
    423  1.1  rjs 			break;
    424  1.1  rjs 		}
    425  1.1  rjs 
    426  1.1  rjs 		if (sc->disable != NULL && sc->enabled != 0) {
    427  1.1  rjs 			(*sc->disable)(sc);
    428  1.1  rjs 			sc->enabled = 0;
    429  1.1  rjs 		}
    430  1.1  rjs 		break;
    431  1.1  rjs 	}
    432  1.1  rjs 
    433  1.1  rjs 	COM_UNLOCK(sc);
    434  1.1  rjs 	splx(s);
    435  1.1  rjs 	return (rv);
    436  1.1  rjs }
    437  1.1  rjs 
    438  1.1  rjs void
    439  1.1  rjs sacom_shutdown(sc)
    440  1.1  rjs 	struct sacom_softc *sc;
    441  1.1  rjs {
    442  1.1  rjs 	struct tty *tp = sc->sc_tty;
    443  1.1  rjs 	int s;
    444  1.1  rjs 
    445  1.1  rjs 	s = splserial();
    446  1.1  rjs 	COM_LOCK(sc);
    447  1.1  rjs 
    448  1.1  rjs 	/* Clear any break condition set with TIOCSBRK. */
    449  1.1  rjs 	sacom_break(sc, 0);
    450  1.1  rjs 
    451  1.1  rjs 	/*
    452  1.1  rjs 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    453  1.1  rjs 	 * notice even if we immediately open the port again.
    454  1.1  rjs 	 * Avoid tsleeping above splhigh().
    455  1.1  rjs 	 */
    456  1.1  rjs 	if (ISSET(tp->t_cflag, HUPCL)) {
    457  1.1  rjs 		sacom_modem(sc, 0);
    458  1.1  rjs 		COM_UNLOCK(sc);
    459  1.1  rjs 		splx(s);
    460  1.1  rjs 		/* XXX tsleep will only timeout */
    461  1.1  rjs 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    462  1.1  rjs 		s = splserial();
    463  1.1  rjs 		COM_LOCK(sc);
    464  1.1  rjs 	}
    465  1.1  rjs 
    466  1.1  rjs 	/* Turn off interrupts. */
    467  1.1  rjs 	sc->sc_cr3 = 0;
    468  1.1  rjs 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3, sc->sc_cr3);
    469  1.1  rjs 
    470  1.1  rjs 	if (sc->disable) {
    471  1.1  rjs #ifdef DIAGNOSTIC
    472  1.1  rjs 		if (!sc->enabled)
    473  1.1  rjs 			panic("sacom_shutdown: not enabled?");
    474  1.1  rjs #endif
    475  1.1  rjs 		(*sc->disable)(sc);
    476  1.1  rjs 		sc->enabled = 0;
    477  1.1  rjs 	}
    478  1.1  rjs 	COM_UNLOCK(sc);
    479  1.1  rjs 	splx(s);
    480  1.1  rjs }
    481  1.1  rjs 
    482  1.1  rjs int
    483  1.1  rjs sacomopen(dev, flag, mode, p)
    484  1.1  rjs 	dev_t dev;
    485  1.1  rjs 	int flag, mode;
    486  1.1  rjs 	struct proc *p;
    487  1.1  rjs {
    488  1.1  rjs 	struct sacom_softc *sc;
    489  1.1  rjs 	struct tty *tp;
    490  1.1  rjs 	int s, s2;
    491  1.1  rjs 	int error;
    492  1.1  rjs 
    493  1.1  rjs 	sc = device_lookup(&sacom_cd, COMUNIT(dev));
    494  1.1  rjs 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
    495  1.1  rjs 		sc->sc_rbuf == NULL)
    496  1.1  rjs 		return (ENXIO);
    497  1.1  rjs 
    498  1.1  rjs 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
    499  1.1  rjs 		return (ENXIO);
    500  1.1  rjs 
    501  1.1  rjs 	tp = sc->sc_tty;
    502  1.1  rjs 
    503  1.1  rjs 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    504  1.1  rjs 	    ISSET(tp->t_state, TS_XCLUDE) &&
    505  1.1  rjs 		p->p_ucred->cr_uid != 0)
    506  1.1  rjs 		return (EBUSY);
    507  1.1  rjs 
    508  1.1  rjs 	s = spltty();
    509  1.1  rjs 
    510  1.1  rjs 	/*
    511  1.1  rjs 	 * Do the following iff this is a first open.
    512  1.1  rjs 	 */
    513  1.1  rjs 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    514  1.1  rjs 		struct termios t;
    515  1.1  rjs 
    516  1.1  rjs 		tp->t_dev = dev;
    517  1.1  rjs 
    518  1.1  rjs 		s2 = splserial();
    519  1.1  rjs 		COM_LOCK(sc);
    520  1.1  rjs 
    521  1.1  rjs 		if (sc->enable) {
    522  1.1  rjs 			if ((*sc->enable)(sc)) {
    523  1.1  rjs 				COM_UNLOCK(sc);
    524  1.1  rjs 				splx(s2);
    525  1.1  rjs 				splx(s);
    526  1.1  rjs 				printf("%s: device enable failed\n",
    527  1.1  rjs 				       sc->sc_dev.dv_xname);
    528  1.1  rjs 				return (EIO);
    529  1.1  rjs 			}
    530  1.1  rjs 			sc->enabled = 1;
    531  1.1  rjs 			sacom_config(sc);
    532  1.1  rjs 		}
    533  1.1  rjs 
    534  1.1  rjs 		/* Turn on interrupts. */
    535  1.1  rjs 		sc->sc_cr3 = CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE;
    536  1.1  rjs 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3,
    537  1.1  rjs 				  sc->sc_cr3);
    538  1.1  rjs 
    539  1.1  rjs 
    540  1.1  rjs 		COM_UNLOCK(sc);
    541  1.1  rjs 		splx(s2);
    542  1.1  rjs 
    543  1.1  rjs 		/*
    544  1.1  rjs 		 * Initialize the termios status to the defaults.  Add in the
    545  1.1  rjs 		 * sticky bits from TIOCSFLAGS.
    546  1.1  rjs 		 */
    547  1.1  rjs 		t.c_ispeed = 0;
    548  1.1  rjs 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    549  1.1  rjs 			t.c_ospeed = sacomconsrate;
    550  1.1  rjs 			t.c_cflag = sacomconscflag;
    551  1.1  rjs 		} else {
    552  1.1  rjs 			t.c_ospeed = TTYDEF_SPEED;
    553  1.1  rjs 			t.c_cflag = TTYDEF_CFLAG;
    554  1.1  rjs 		}
    555  1.1  rjs 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    556  1.1  rjs 			SET(t.c_cflag, CLOCAL);
    557  1.1  rjs 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    558  1.1  rjs 			SET(t.c_cflag, CRTSCTS);
    559  1.1  rjs 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    560  1.1  rjs 			SET(t.c_cflag, MDMBUF);
    561  1.1  rjs 		/* Make sure sacomparam() will do something. */
    562  1.1  rjs 		tp->t_ospeed = 0;
    563  1.1  rjs 		(void) sacomparam(tp, &t);
    564  1.1  rjs 		tp->t_iflag = TTYDEF_IFLAG;
    565  1.1  rjs 		tp->t_oflag = TTYDEF_OFLAG;
    566  1.1  rjs 		tp->t_lflag = TTYDEF_LFLAG;
    567  1.1  rjs 		ttychars(tp);
    568  1.1  rjs 		ttsetwater(tp);
    569  1.1  rjs 
    570  1.1  rjs 		s2 = splserial();
    571  1.1  rjs 		COM_LOCK(sc);
    572  1.1  rjs 
    573  1.1  rjs 		/*
    574  1.1  rjs 		 * Turn on DTR.  We must always do this, even if carrier is not
    575  1.1  rjs 		 * present, because otherwise we'd have to use TIOCSDTR
    576  1.1  rjs 		 * immediately after setting CLOCAL, which applications do not
    577  1.1  rjs 		 * expect.  We always assert DTR while the device is open
    578  1.1  rjs 		 * unless explicitly requested to deassert it.
    579  1.1  rjs 		 */
    580  1.1  rjs 		sacom_modem(sc, 1);
    581  1.1  rjs 
    582  1.1  rjs 		/* Clear the input ring, and unblock. */
    583  1.1  rjs 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    584  1.1  rjs 		sc->sc_rbavail = SACOM_RING_SIZE;
    585  1.1  rjs 		sacom_iflush(sc);
    586  1.1  rjs 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    587  1.1  rjs 		sacom_hwiflow(sc);
    588  1.1  rjs 
    589  1.1  rjs #ifdef COM_DEBUG
    590  1.1  rjs 		if (sacom_debug)
    591  1.1  rjs 			comstatus(sc, "sacomopen  ");
    592  1.1  rjs #endif
    593  1.1  rjs 
    594  1.1  rjs 		COM_UNLOCK(sc);
    595  1.1  rjs 		splx(s2);
    596  1.1  rjs 	}
    597  1.1  rjs 
    598  1.1  rjs 	splx(s);
    599  1.1  rjs 
    600  1.1  rjs 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    601  1.1  rjs 	if (error)
    602  1.1  rjs 		goto bad;
    603  1.1  rjs 
    604  1.1  rjs 	error = (*tp->t_linesw->l_open)(dev, tp);
    605  1.1  rjs 	if (error)
    606  1.1  rjs 		goto bad;
    607  1.1  rjs 
    608  1.1  rjs 	return (0);
    609  1.1  rjs 
    610  1.1  rjs bad:
    611  1.1  rjs 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    612  1.1  rjs 		/*
    613  1.1  rjs 		 * We failed to open the device, and nobody else had it opened.
    614  1.1  rjs 		 * Clean up the state as appropriate.
    615  1.1  rjs 		 */
    616  1.1  rjs 		sacom_shutdown(sc);
    617  1.1  rjs 	}
    618  1.1  rjs 
    619  1.1  rjs 	return (error);
    620  1.1  rjs }
    621  1.1  rjs 
    622  1.1  rjs int
    623  1.1  rjs sacomclose(dev, flag, mode, p)
    624  1.1  rjs 	dev_t dev;
    625  1.1  rjs 	int flag, mode;
    626  1.1  rjs 	struct proc *p;
    627  1.1  rjs {
    628  1.1  rjs 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    629  1.1  rjs 	struct tty *tp = sc->sc_tty;
    630  1.1  rjs 
    631  1.1  rjs 	/* XXX This is for cons.c. */
    632  1.1  rjs 	if (!ISSET(tp->t_state, TS_ISOPEN))
    633  1.1  rjs 		return (0);
    634  1.1  rjs 
    635  1.1  rjs 	(*tp->t_linesw->l_close)(tp, flag);
    636  1.1  rjs 	ttyclose(tp);
    637  1.1  rjs 
    638  1.1  rjs 	if (COM_ISALIVE(sc) == 0)
    639  1.1  rjs 		return (0);
    640  1.1  rjs 
    641  1.1  rjs 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    642  1.1  rjs 		/*
    643  1.1  rjs 		 * Although we got a last close, the device may still be in
    644  1.1  rjs 		 * use; e.g. if this was the dialout node, and there are still
    645  1.1  rjs 		 * processes waiting for carrier on the non-dialout node.
    646  1.1  rjs 		 */
    647  1.1  rjs 		sacom_shutdown(sc);
    648  1.1  rjs 	}
    649  1.1  rjs 
    650  1.1  rjs 	return (0);
    651  1.1  rjs }
    652  1.1  rjs 
    653  1.1  rjs int
    654  1.1  rjs sacomread(dev, uio, flag)
    655  1.1  rjs 	dev_t dev;
    656  1.1  rjs 	struct uio *uio;
    657  1.1  rjs 	int flag;
    658  1.1  rjs {
    659  1.1  rjs 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    660  1.1  rjs 	struct tty *tp = sc->sc_tty;
    661  1.1  rjs 
    662  1.1  rjs 	if (COM_ISALIVE(sc) == 0)
    663  1.1  rjs 		return (EIO);
    664  1.1  rjs 
    665  1.1  rjs 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    666  1.1  rjs }
    667  1.1  rjs 
    668  1.1  rjs int
    669  1.1  rjs sacomwrite(dev, uio, flag)
    670  1.1  rjs 	dev_t dev;
    671  1.1  rjs 	struct uio *uio;
    672  1.1  rjs 	int flag;
    673  1.1  rjs {
    674  1.1  rjs 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    675  1.1  rjs 	struct tty *tp = sc->sc_tty;
    676  1.1  rjs 
    677  1.1  rjs 	if (COM_ISALIVE(sc) == 0)
    678  1.1  rjs 		return (EIO);
    679  1.1  rjs 
    680  1.1  rjs 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    681  1.1  rjs }
    682  1.1  rjs 
    683  1.1  rjs int
    684  1.1  rjs sacompoll(dev, events, p)
    685  1.1  rjs 	dev_t dev;
    686  1.1  rjs 	int events;
    687  1.1  rjs 	struct proc *p;
    688  1.1  rjs {
    689  1.1  rjs 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    690  1.1  rjs 	struct tty *tp = sc->sc_tty;
    691  1.1  rjs 
    692  1.1  rjs 	if (COM_ISALIVE(sc) == 0)
    693  1.1  rjs 		return (EIO);
    694  1.1  rjs 
    695  1.1  rjs 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    696  1.1  rjs }
    697  1.1  rjs 
    698  1.1  rjs struct tty *
    699  1.1  rjs sacomtty(dev)
    700  1.1  rjs 	dev_t dev;
    701  1.1  rjs {
    702  1.1  rjs 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    703  1.1  rjs 	struct tty *tp = sc->sc_tty;
    704  1.1  rjs 
    705  1.1  rjs 	return (tp);
    706  1.1  rjs }
    707  1.1  rjs 
    708  1.1  rjs int
    709  1.1  rjs sacomioctl(dev, cmd, data, flag, p)
    710  1.1  rjs 	dev_t dev;
    711  1.1  rjs 	u_long cmd;
    712  1.1  rjs 	caddr_t data;
    713  1.1  rjs 	int flag;
    714  1.1  rjs 	struct proc *p;
    715  1.1  rjs {
    716  1.1  rjs 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
    717  1.1  rjs 	struct tty *tp = sc->sc_tty;
    718  1.1  rjs 	int error;
    719  1.1  rjs 	int s;
    720  1.1  rjs 
    721  1.1  rjs 	if (COM_ISALIVE(sc) == 0)
    722  1.1  rjs 		return (EIO);
    723  1.1  rjs 
    724  1.1  rjs 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    725  1.1  rjs 	if (error >= 0)
    726  1.1  rjs 		return (error);
    727  1.1  rjs 
    728  1.1  rjs 	error = ttioctl(tp, cmd, data, flag, p);
    729  1.1  rjs 	if (error >= 0)
    730  1.1  rjs 		return (error);
    731  1.1  rjs 
    732  1.1  rjs 	error = 0;
    733  1.1  rjs 
    734  1.1  rjs 	s = splserial();
    735  1.1  rjs 	COM_LOCK(sc);
    736  1.1  rjs 
    737  1.1  rjs 	switch (cmd) {
    738  1.1  rjs 	case TIOCSBRK:
    739  1.1  rjs 		sacom_break(sc, 1);
    740  1.1  rjs 		break;
    741  1.1  rjs 
    742  1.1  rjs 	case TIOCCBRK:
    743  1.1  rjs 		sacom_break(sc, 0);
    744  1.1  rjs 		break;
    745  1.1  rjs 
    746  1.1  rjs 	case TIOCSDTR:
    747  1.1  rjs 		sacom_modem(sc, 1);
    748  1.1  rjs 		break;
    749  1.1  rjs 
    750  1.1  rjs 	case TIOCCDTR:
    751  1.1  rjs 		sacom_modem(sc, 0);
    752  1.1  rjs 		break;
    753  1.1  rjs 
    754  1.1  rjs 	case TIOCGFLAGS:
    755  1.1  rjs 		*(int *)data = sc->sc_swflags;
    756  1.1  rjs 		break;
    757  1.1  rjs 
    758  1.1  rjs 	case TIOCSFLAGS:
    759  1.1  rjs 		error = suser(p->p_ucred, &p->p_acflag);
    760  1.1  rjs 		if (error)
    761  1.1  rjs 			break;
    762  1.1  rjs 		sc->sc_swflags = *(int *)data;
    763  1.1  rjs 		break;
    764  1.1  rjs 
    765  1.1  rjs 	case TIOCMSET:
    766  1.1  rjs 	case TIOCMBIS:
    767  1.1  rjs 	case TIOCMBIC:
    768  1.1  rjs 		tiocm_to_sacom(sc, cmd, *(int *)data);
    769  1.1  rjs 		break;
    770  1.1  rjs 
    771  1.1  rjs 	case TIOCMGET:
    772  1.1  rjs 		*(int *)data = sacom_to_tiocm(sc);
    773  1.1  rjs 		break;
    774  1.1  rjs 
    775  1.1  rjs 	default:
    776  1.1  rjs 		error = ENOTTY;
    777  1.1  rjs 		break;
    778  1.1  rjs 	}
    779  1.1  rjs 
    780  1.1  rjs 	COM_UNLOCK(sc);
    781  1.1  rjs 	splx(s);
    782  1.1  rjs 
    783  1.1  rjs #ifdef COM_DEBUG
    784  1.1  rjs 	if (sacom_debug)
    785  1.1  rjs 		comstatus(sc, "comioctl ");
    786  1.1  rjs #endif
    787  1.1  rjs 
    788  1.1  rjs 	return (error);
    789  1.1  rjs }
    790  1.1  rjs 
    791  1.1  rjs static inline void
    792  1.1  rjs sacom_schedrx(sc)
    793  1.1  rjs 	struct sacom_softc *sc;
    794  1.1  rjs {
    795  1.1  rjs 
    796  1.1  rjs 	sc->sc_rx_ready = 1;
    797  1.1  rjs 
    798  1.1  rjs 	/* Wake up the poller. */
    799  1.1  rjs #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    800  1.1  rjs 	softintr_schedule(sc->sc_si);
    801  1.1  rjs #else
    802  1.1  rjs 	setsoftserial();
    803  1.1  rjs #endif
    804  1.1  rjs }
    805  1.1  rjs 
    806  1.1  rjs void
    807  1.1  rjs sacom_break(sc, onoff)
    808  1.1  rjs 	struct sacom_softc *sc;
    809  1.1  rjs 	int onoff;
    810  1.1  rjs {
    811  1.1  rjs 
    812  1.1  rjs 	if (onoff)
    813  1.1  rjs 		SET(sc->sc_cr3, CR3_BRK);
    814  1.1  rjs 	else
    815  1.1  rjs 		CLR(sc->sc_cr3, CR3_BRK);
    816  1.1  rjs 
    817  1.1  rjs 	if (!sc->sc_heldchange) {
    818  1.1  rjs 		if (sc->sc_tx_busy) {
    819  1.1  rjs 			sc->sc_heldtbc = sc->sc_tbc;
    820  1.1  rjs 			sc->sc_tbc = 0;
    821  1.1  rjs 			sc->sc_heldchange = 1;
    822  1.1  rjs 		} else
    823  1.1  rjs 			sacom_loadchannelregs(sc);
    824  1.1  rjs 	}
    825  1.1  rjs }
    826  1.1  rjs 
    827  1.1  rjs void
    828  1.1  rjs sacom_modem(sc, onoff)
    829  1.1  rjs 	struct sacom_softc *sc;
    830  1.1  rjs 	int onoff;
    831  1.1  rjs {
    832  1.1  rjs 	if (!sc->sc_heldchange) {
    833  1.1  rjs 		if (sc->sc_tx_busy) {
    834  1.1  rjs 			sc->sc_heldtbc = sc->sc_tbc;
    835  1.1  rjs 			sc->sc_tbc = 0;
    836  1.1  rjs 			sc->sc_heldchange = 1;
    837  1.1  rjs 		} else
    838  1.1  rjs 			sacom_loadchannelregs(sc);
    839  1.1  rjs 	}
    840  1.1  rjs }
    841  1.1  rjs 
    842  1.1  rjs void
    843  1.1  rjs tiocm_to_sacom(sc, how, ttybits)
    844  1.1  rjs 	struct sacom_softc *sc;
    845  1.1  rjs 	u_long how;
    846  1.1  rjs 	int ttybits;
    847  1.1  rjs {
    848  1.1  rjs }
    849  1.1  rjs 
    850  1.1  rjs int
    851  1.1  rjs sacom_to_tiocm(sc)
    852  1.1  rjs 	struct sacom_softc *sc;
    853  1.1  rjs {
    854  1.1  rjs 	int ttybits = 0;
    855  1.1  rjs 
    856  1.1  rjs 	if (sc->sc_cr3 != 0)
    857  1.1  rjs 		SET(ttybits, TIOCM_LE);
    858  1.1  rjs 
    859  1.1  rjs 	return (ttybits);
    860  1.1  rjs }
    861  1.1  rjs 
    862  1.1  rjs static u_int
    863  1.1  rjs cflag2cr0(cflag)
    864  1.1  rjs 	tcflag_t cflag;
    865  1.1  rjs {
    866  1.1  rjs 	u_int cr0;
    867  1.1  rjs 
    868  1.1  rjs 	cr0  = (cflag & PARENB) ? CR0_PE : 0;
    869  1.1  rjs 	cr0 |= (cflag & PARODD) ? 0 : CR0_OES;
    870  1.1  rjs 	cr0 |= (cflag & CSTOPB) ? CR0_SBS : 0;
    871  1.1  rjs 	cr0 |= ((cflag & CSIZE) == CS8) ? CR0_DSS : 0;
    872  1.1  rjs 
    873  1.1  rjs 	return (cr0);
    874  1.1  rjs }
    875  1.1  rjs 
    876  1.1  rjs int
    877  1.1  rjs sacomparam(tp, t)
    878  1.1  rjs 	struct tty *tp;
    879  1.1  rjs 	struct termios *t;
    880  1.1  rjs {
    881  1.1  rjs 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
    882  1.1  rjs 	int ospeed = SACOMSPEED(t->c_ospeed);
    883  1.1  rjs 	u_int cr0;
    884  1.1  rjs 	int s;
    885  1.1  rjs 
    886  1.1  rjs 	if (COM_ISALIVE(sc) == 0)
    887  1.1  rjs 		return (EIO);
    888  1.1  rjs 
    889  1.1  rjs 	/* Check requested parameters. */
    890  1.1  rjs 	if (ospeed < 0)
    891  1.1  rjs 		return (EINVAL);
    892  1.1  rjs 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    893  1.1  rjs 		return (EINVAL);
    894  1.1  rjs 
    895  1.1  rjs 	/*
    896  1.1  rjs 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    897  1.1  rjs 	 * is always active.
    898  1.1  rjs 	 */
    899  1.1  rjs 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    900  1.1  rjs 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    901  1.1  rjs 		SET(t->c_cflag, CLOCAL);
    902  1.1  rjs 		CLR(t->c_cflag, HUPCL);
    903  1.1  rjs 	}
    904  1.1  rjs 
    905  1.1  rjs 	/*
    906  1.1  rjs 	 * If there were no changes, don't do anything.  This avoids dropping
    907  1.1  rjs 	 * input and improves performance when all we did was frob things like
    908  1.1  rjs 	 * VMIN and VTIME.
    909  1.1  rjs 	 */
    910  1.1  rjs 	if (tp->t_ospeed == t->c_ospeed &&
    911  1.1  rjs 	    tp->t_cflag == t->c_cflag)
    912  1.1  rjs 		return (0);
    913  1.1  rjs 
    914  1.1  rjs 	cr0 = cflag2cr0(t->c_cflag);
    915  1.1  rjs 
    916  1.1  rjs 	s = splserial();
    917  1.1  rjs 	COM_LOCK(sc);
    918  1.1  rjs 
    919  1.1  rjs 	sc->sc_cr0 = cr0;
    920  1.1  rjs 
    921  1.1  rjs 	sc->sc_speed = ospeed;
    922  1.1  rjs 
    923  1.1  rjs 	/* And copy to tty. */
    924  1.1  rjs 	tp->t_ispeed = 0;
    925  1.1  rjs 	tp->t_ospeed = t->c_ospeed;
    926  1.1  rjs 	tp->t_cflag = t->c_cflag;
    927  1.1  rjs 
    928  1.1  rjs 	if (!sc->sc_heldchange) {
    929  1.1  rjs 		if (sc->sc_tx_busy) {
    930  1.1  rjs 			sc->sc_heldtbc = sc->sc_tbc;
    931  1.1  rjs 			sc->sc_tbc = 0;
    932  1.1  rjs 			sc->sc_heldchange = 1;
    933  1.1  rjs 		} else
    934  1.1  rjs 			sacom_loadchannelregs(sc);
    935  1.1  rjs 	}
    936  1.1  rjs 
    937  1.1  rjs 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    938  1.1  rjs 		/* Disable the high water mark. */
    939  1.1  rjs 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
    940  1.1  rjs 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
    941  1.1  rjs 			sacom_schedrx(sc);
    942  1.1  rjs 		}
    943  1.1  rjs 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
    944  1.1  rjs 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
    945  1.1  rjs 			sacom_hwiflow(sc);
    946  1.1  rjs 		}
    947  1.1  rjs 	}
    948  1.1  rjs 
    949  1.1  rjs 	COM_UNLOCK(sc);
    950  1.1  rjs 	splx(s);
    951  1.1  rjs 
    952  1.1  rjs 	(void) (*tp->t_linesw->l_modem)(tp, 1);
    953  1.1  rjs 
    954  1.1  rjs #ifdef COM_DEBUG
    955  1.1  rjs 	if (sacom_debug)
    956  1.1  rjs 		comstatus(sc, "comparam ");
    957  1.1  rjs #endif
    958  1.1  rjs 
    959  1.1  rjs 	return (0);
    960  1.1  rjs }
    961  1.1  rjs 
    962  1.1  rjs void
    963  1.1  rjs sacom_iflush(sc)
    964  1.1  rjs 	struct sacom_softc *sc;
    965  1.1  rjs {
    966  1.1  rjs 	bus_space_tag_t iot = sc->sc_iot;
    967  1.1  rjs 	bus_space_handle_t ioh = sc->sc_ioh;
    968  1.1  rjs #ifdef DIAGNOSTIC
    969  1.1  rjs 	int reg;
    970  1.1  rjs #endif
    971  1.1  rjs 	int timo;
    972  1.1  rjs 
    973  1.1  rjs #ifdef DIAGNOSTIC
    974  1.1  rjs 	reg = 0xffff;
    975  1.1  rjs #endif
    976  1.1  rjs 	timo = 50;
    977  1.1  rjs 	/* flush any pending I/O */
    978  1.1  rjs 	if (sc->sc_cr3 & CR3_RXE) {
    979  1.1  rjs 	while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_RNE)
    980  1.1  rjs 	    && --timo)
    981  1.1  rjs #ifdef DIAGNOSTIC
    982  1.1  rjs 		reg =
    983  1.1  rjs #else
    984  1.1  rjs 		    (void)
    985  1.1  rjs #endif
    986  1.1  rjs 		    bus_space_read_4(iot, ioh, SACOM_DR);
    987  1.1  rjs 	}
    988  1.1  rjs #if 0
    989  1.1  rjs 	/* XXX is it good idea to wait TX finish? */
    990  1.1  rjs 	if (sc->sc_cr3 & CR3_TXE) {
    991  1.1  rjs 	timo = 500;
    992  1.1  rjs 	while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_TBY)
    993  1.1  rjs 	    && --timo)
    994  1.1  rjs 		delay(100);
    995  1.1  rjs 	}
    996  1.1  rjs #endif
    997  1.1  rjs #ifdef DIAGNOSTIC
    998  1.1  rjs 	if (!timo)
    999  1.1  rjs 		printf("%s: sacom_iflush timeout %02x\n", sc->sc_dev.dv_xname,
   1000  1.1  rjs 		       reg);
   1001  1.1  rjs #endif
   1002  1.1  rjs }
   1003  1.1  rjs 
   1004  1.1  rjs void
   1005  1.1  rjs sacom_loadchannelregs(sc)
   1006  1.1  rjs 	struct sacom_softc *sc;
   1007  1.1  rjs {
   1008  1.1  rjs 	bus_space_tag_t iot = sc->sc_iot;
   1009  1.1  rjs 	bus_space_handle_t ioh = sc->sc_ioh;
   1010  1.1  rjs 
   1011  1.1  rjs 	/* XXXXX necessary? */
   1012  1.1  rjs 	sacom_iflush(sc);
   1013  1.1  rjs 
   1014  1.1  rjs 	/* Need to stop engines first. */
   1015  1.1  rjs 	bus_space_write_4(iot, ioh, SACOM_CR3, 0);
   1016  1.1  rjs 
   1017  1.1  rjs 	bus_space_write_4(iot, ioh, SACOM_CR1, sc->sc_speed >> 8);
   1018  1.1  rjs 	bus_space_write_4(iot, ioh, SACOM_CR2, sc->sc_speed & 0xff);
   1019  1.1  rjs 
   1020  1.1  rjs 	bus_space_write_4(iot, ioh, SACOM_CR0, sc->sc_cr0);
   1021  1.1  rjs 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
   1022  1.1  rjs }
   1023  1.1  rjs 
   1024  1.1  rjs int
   1025  1.1  rjs sacomhwiflow(tp, block)
   1026  1.1  rjs 	struct tty *tp;
   1027  1.1  rjs 	int block;
   1028  1.1  rjs {
   1029  1.1  rjs #if 0
   1030  1.1  rjs 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
   1031  1.1  rjs 	int s;
   1032  1.1  rjs 
   1033  1.1  rjs 	if (COM_ISALIVE(sc) == 0)
   1034  1.1  rjs 		return (0);
   1035  1.1  rjs 
   1036  1.1  rjs 	if (sc->sc_mcr_rts == 0)
   1037  1.1  rjs 		return (0);
   1038  1.1  rjs 
   1039  1.1  rjs 	s = splserial();
   1040  1.1  rjs 	COM_LOCK(sc);
   1041  1.1  rjs 
   1042  1.1  rjs 	if (block) {
   1043  1.1  rjs 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1044  1.1  rjs 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1045  1.1  rjs 			sacom_hwiflow(sc);
   1046  1.1  rjs 		}
   1047  1.1  rjs 	} else {
   1048  1.1  rjs 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1049  1.1  rjs 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1050  1.1  rjs 			sacom_schedrx(sc);
   1051  1.1  rjs 		}
   1052  1.1  rjs 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1053  1.1  rjs 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1054  1.1  rjs 			sacom_hwiflow(sc);
   1055  1.1  rjs 		}
   1056  1.1  rjs 	}
   1057  1.1  rjs 
   1058  1.1  rjs 	COM_UNLOCK(sc);
   1059  1.1  rjs 	splx(s);
   1060  1.1  rjs #endif
   1061  1.1  rjs 	return (1);
   1062  1.1  rjs }
   1063  1.1  rjs 
   1064  1.1  rjs /*
   1065  1.1  rjs  * (un)block input via hw flowcontrol
   1066  1.1  rjs  */
   1067  1.1  rjs void
   1068  1.1  rjs sacom_hwiflow(sc)
   1069  1.1  rjs 	struct sacom_softc *sc;
   1070  1.1  rjs {
   1071  1.1  rjs #if 0
   1072  1.1  rjs 	bus_space_tag_t iot = sc->sc_iot;
   1073  1.1  rjs 	bus_space_handle_t ioh = sc->sc_ioh;
   1074  1.1  rjs 
   1075  1.1  rjs 	/* XXX implement */
   1076  1.1  rjs #endif
   1077  1.1  rjs }
   1078  1.1  rjs 
   1079  1.1  rjs 
   1080  1.1  rjs void
   1081  1.1  rjs sacomstart(tp)
   1082  1.1  rjs 	struct tty *tp;
   1083  1.1  rjs {
   1084  1.1  rjs 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
   1085  1.1  rjs 	bus_space_tag_t iot = sc->sc_iot;
   1086  1.1  rjs 	bus_space_handle_t ioh = sc->sc_ioh;
   1087  1.1  rjs 	int s;
   1088  1.1  rjs 
   1089  1.1  rjs 	if (COM_ISALIVE(sc) == 0)
   1090  1.1  rjs 		return;
   1091  1.1  rjs 
   1092  1.1  rjs 	s = spltty();
   1093  1.1  rjs 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1094  1.1  rjs 		goto out;
   1095  1.1  rjs 
   1096  1.1  rjs 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1097  1.1  rjs 		if (ISSET(tp->t_state, TS_ASLEEP)) {
   1098  1.1  rjs 			CLR(tp->t_state, TS_ASLEEP);
   1099  1.1  rjs 			wakeup(&tp->t_outq);
   1100  1.1  rjs 		}
   1101  1.1  rjs 		selwakeup(&tp->t_wsel);
   1102  1.1  rjs 		if (tp->t_outq.c_cc == 0)
   1103  1.1  rjs 			goto out;
   1104  1.1  rjs 	}
   1105  1.1  rjs 
   1106  1.1  rjs 	/* Grab the first contiguous region of buffer space. */
   1107  1.1  rjs 	{
   1108  1.1  rjs 		u_char *tba;
   1109  1.1  rjs 		int tbc;
   1110  1.1  rjs 
   1111  1.1  rjs 		tba = tp->t_outq.c_cf;
   1112  1.1  rjs 		tbc = ndqb(&tp->t_outq, 0);
   1113  1.1  rjs 
   1114  1.1  rjs 		(void)splserial();
   1115  1.1  rjs 		COM_LOCK(sc);
   1116  1.1  rjs 
   1117  1.1  rjs 		sc->sc_tba = tba;
   1118  1.1  rjs 		sc->sc_tbc = tbc;
   1119  1.1  rjs 	}
   1120  1.1  rjs 
   1121  1.1  rjs 	SET(tp->t_state, TS_BUSY);
   1122  1.1  rjs 	sc->sc_tx_busy = 1;
   1123  1.1  rjs 
   1124  1.1  rjs 	/* Enable transmit completion interrupts if necessary. */
   1125  1.1  rjs 	if (!ISSET(sc->sc_cr3, CR3_TIE)) {
   1126  1.1  rjs 		SET(sc->sc_cr3, CR3_TIE);
   1127  1.1  rjs 		bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
   1128  1.1  rjs 	}
   1129  1.1  rjs 
   1130  1.1  rjs 	/* Output the first chunk of the contiguous buffer. */
   1131  1.1  rjs 	sacom_filltx(sc);
   1132  1.1  rjs 
   1133  1.1  rjs 	COM_UNLOCK(sc);
   1134  1.1  rjs out:
   1135  1.1  rjs 	splx(s);
   1136  1.1  rjs 	return;
   1137  1.1  rjs }
   1138  1.1  rjs 
   1139  1.1  rjs void
   1140  1.1  rjs sacom_filltx(sc)
   1141  1.1  rjs 	struct sacom_softc *sc;
   1142  1.1  rjs {
   1143  1.1  rjs 	bus_space_tag_t iot = sc->sc_iot;
   1144  1.1  rjs 	bus_space_handle_t ioh = sc->sc_ioh;
   1145  1.1  rjs 	int c, n;
   1146  1.1  rjs 
   1147  1.1  rjs 	n = 0;
   1148  1.1  rjs 	while(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1149  1.1  rjs 	      & SR1_TNF) {
   1150  1.1  rjs 		if (n == SACOM_TXFIFOLEN || n == sc->sc_tbc)
   1151  1.1  rjs 			break;
   1152  1.1  rjs 		c = *(sc->sc_tba + n);
   1153  1.1  rjs 		c &= 0xff;
   1154  1.1  rjs 		bus_space_write_4(iot, ioh, SACOM_DR, c);
   1155  1.1  rjs 		n++;
   1156  1.1  rjs 	}
   1157  1.1  rjs 	sc->sc_tbc -= n;
   1158  1.1  rjs 	sc->sc_tba += n;
   1159  1.1  rjs }
   1160  1.1  rjs 
   1161  1.1  rjs /*
   1162  1.1  rjs  * Stop output on a line.
   1163  1.1  rjs  */
   1164  1.1  rjs void
   1165  1.1  rjs sacomstop(tp, flag)
   1166  1.1  rjs 	struct tty *tp;
   1167  1.1  rjs 	int flag;
   1168  1.1  rjs {
   1169  1.1  rjs 	struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
   1170  1.1  rjs 	int s;
   1171  1.1  rjs 
   1172  1.1  rjs 	s = splserial();
   1173  1.1  rjs 	COM_LOCK(sc);
   1174  1.1  rjs 	if (ISSET(tp->t_state, TS_BUSY)) {
   1175  1.1  rjs 		/* Stop transmitting at the next chunk. */
   1176  1.1  rjs 		sc->sc_tbc = 0;
   1177  1.1  rjs 		sc->sc_heldtbc = 0;
   1178  1.1  rjs 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1179  1.1  rjs 			SET(tp->t_state, TS_FLUSH);
   1180  1.1  rjs 	}
   1181  1.1  rjs 	COM_UNLOCK(sc);
   1182  1.1  rjs 	splx(s);
   1183  1.1  rjs }
   1184  1.1  rjs 
   1185  1.1  rjs static inline void
   1186  1.1  rjs sacom_rxsoft(sc, tp)
   1187  1.1  rjs 	struct sacom_softc *sc;
   1188  1.1  rjs 	struct tty *tp;
   1189  1.1  rjs {
   1190  1.1  rjs 	int (*rint)(int c, struct tty *tp) = tp->t_linesw->l_rint;
   1191  1.1  rjs 	u_char *get, *end;
   1192  1.1  rjs 	u_int cc, scc;
   1193  1.1  rjs 	u_char sr1;
   1194  1.1  rjs 	int code;
   1195  1.1  rjs 	int s;
   1196  1.1  rjs 
   1197  1.1  rjs 	end = sc->sc_ebuf;
   1198  1.1  rjs 	get = sc->sc_rbget;
   1199  1.1  rjs 	scc = cc = SACOM_RING_SIZE - sc->sc_rbavail;
   1200  1.1  rjs 
   1201  1.1  rjs 	while (cc) {
   1202  1.1  rjs 		code = get[0];
   1203  1.1  rjs 		sr1 = get[1];
   1204  1.1  rjs 		if (ISSET(sr1, SR1_FRE))
   1205  1.1  rjs 			SET(code, TTY_FE);
   1206  1.1  rjs 		if (ISSET(sr1, SR1_PRE))
   1207  1.1  rjs 			SET(code, TTY_PE);
   1208  1.1  rjs 		if ((*rint)(code, tp) == -1) {
   1209  1.1  rjs 			/*
   1210  1.1  rjs 			 * The line discipline's buffer is out of space.
   1211  1.1  rjs 			 */
   1212  1.1  rjs 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1213  1.1  rjs 				/*
   1214  1.1  rjs 				 * We're either not using flow control, or the
   1215  1.1  rjs 				 * line discipline didn't tell us to block for
   1216  1.1  rjs 				 * some reason.  Either way, we have no way to
   1217  1.1  rjs 				 * know when there's more space available, so
   1218  1.1  rjs 				 * just drop the rest of the data.
   1219  1.1  rjs 				 */
   1220  1.1  rjs 				get += cc << 1;
   1221  1.1  rjs 				if (get >= end)
   1222  1.1  rjs 					get -= SACOM_RING_SIZE << 1;
   1223  1.1  rjs 				cc = 0;
   1224  1.1  rjs 			} else {
   1225  1.1  rjs 				/*
   1226  1.1  rjs 				 * Don't schedule any more receive processing
   1227  1.1  rjs 				 * until the line discipline tells us there's
   1228  1.1  rjs 				 * space available (through comhwiflow()).
   1229  1.1  rjs 				 * Leave the rest of the data in the input
   1230  1.1  rjs 				 * buffer.
   1231  1.1  rjs 				 */
   1232  1.1  rjs 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1233  1.1  rjs 			}
   1234  1.1  rjs 			break;
   1235  1.1  rjs 		}
   1236  1.1  rjs 		get += 2;
   1237  1.1  rjs 		if (get >= end)
   1238  1.1  rjs 			get = sc->sc_rbuf;
   1239  1.1  rjs 		cc--;
   1240  1.1  rjs 	}
   1241  1.1  rjs 
   1242  1.1  rjs 	if (cc != scc) {
   1243  1.1  rjs 		sc->sc_rbget = get;
   1244  1.1  rjs 		s = splserial();
   1245  1.1  rjs 		COM_LOCK(sc);
   1246  1.1  rjs 
   1247  1.1  rjs 		cc = sc->sc_rbavail += scc - cc;
   1248  1.1  rjs 		/* Buffers should be ok again, release possible block. */
   1249  1.1  rjs 		if (cc >= 1) {
   1250  1.1  rjs 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1251  1.1  rjs 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1252  1.1  rjs 				SET(sc->sc_cr3, CR3_RIE);
   1253  1.1  rjs 				bus_space_write_4(sc->sc_iot, sc->sc_ioh,
   1254  1.1  rjs 						  SACOM_CR3, sc->sc_cr3);
   1255  1.1  rjs 			}
   1256  1.1  rjs 		}
   1257  1.1  rjs 		COM_UNLOCK(sc);
   1258  1.1  rjs 		splx(s);
   1259  1.1  rjs 	}
   1260  1.1  rjs }
   1261  1.1  rjs 
   1262  1.1  rjs static inline void
   1263  1.1  rjs sacom_txsoft(sc, tp)
   1264  1.1  rjs 	struct sacom_softc *sc;
   1265  1.1  rjs 	struct tty *tp;
   1266  1.1  rjs {
   1267  1.1  rjs 
   1268  1.1  rjs 	CLR(tp->t_state, TS_BUSY);
   1269  1.1  rjs 	if (ISSET(tp->t_state, TS_FLUSH))
   1270  1.1  rjs 		CLR(tp->t_state, TS_FLUSH);
   1271  1.1  rjs 	else
   1272  1.1  rjs 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1273  1.1  rjs 	(*tp->t_linesw->l_start)(tp);
   1274  1.1  rjs }
   1275  1.1  rjs 
   1276  1.1  rjs static inline void
   1277  1.1  rjs sacom_stsoft(sc, tp)
   1278  1.1  rjs 	struct sacom_softc *sc;
   1279  1.1  rjs 	struct tty *tp;
   1280  1.1  rjs {
   1281  1.1  rjs }
   1282  1.1  rjs 
   1283  1.1  rjs #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1284  1.1  rjs void
   1285  1.1  rjs sacomsoft(arg)
   1286  1.1  rjs 	void *arg;
   1287  1.1  rjs {
   1288  1.1  rjs 	struct sacom_softc *sc = arg;
   1289  1.1  rjs 	struct tty *tp;
   1290  1.1  rjs 
   1291  1.1  rjs 	if (COM_ISALIVE(sc) == 0)
   1292  1.1  rjs 		return;
   1293  1.1  rjs 
   1294  1.1  rjs 	{
   1295  1.1  rjs #else
   1296  1.1  rjs void
   1297  1.1  rjs sacomsoft()
   1298  1.1  rjs {
   1299  1.1  rjs 	struct sacom_softc	*sc;
   1300  1.1  rjs 	struct tty	*tp;
   1301  1.1  rjs 	int	unit;
   1302  1.1  rjs 
   1303  1.1  rjs 	for (unit = 0; unit < sacom_cd.cd_ndevs; unit++) {
   1304  1.1  rjs 		sc = device_lookup(&sacom_cd, unit);
   1305  1.1  rjs 		if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
   1306  1.1  rjs 			continue;
   1307  1.1  rjs 
   1308  1.1  rjs 		if (COM_ISALIVE(sc) == 0)
   1309  1.1  rjs 			continue;
   1310  1.1  rjs 
   1311  1.1  rjs 		tp = sc->sc_tty;
   1312  1.1  rjs 		if (tp == NULL)
   1313  1.1  rjs 			continue;
   1314  1.1  rjs 		if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
   1315  1.1  rjs 			continue;
   1316  1.1  rjs #endif
   1317  1.1  rjs 		tp = sc->sc_tty;
   1318  1.1  rjs 
   1319  1.1  rjs 		if (sc->sc_rx_ready) {
   1320  1.1  rjs 			sc->sc_rx_ready = 0;
   1321  1.1  rjs 			sacom_rxsoft(sc, tp);
   1322  1.1  rjs 		}
   1323  1.1  rjs 
   1324  1.1  rjs 		if (sc->sc_st_check) {
   1325  1.1  rjs 			sc->sc_st_check = 0;
   1326  1.1  rjs 			sacom_stsoft(sc, tp);
   1327  1.1  rjs 		}
   1328  1.1  rjs 
   1329  1.1  rjs 		if (sc->sc_tx_done) {
   1330  1.1  rjs 			sc->sc_tx_done = 0;
   1331  1.1  rjs 			sacom_txsoft(sc, tp);
   1332  1.1  rjs 		}
   1333  1.1  rjs 	}
   1334  1.1  rjs 
   1335  1.1  rjs #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
   1336  1.1  rjs #endif
   1337  1.1  rjs }
   1338  1.1  rjs 
   1339  1.1  rjs #ifdef __ALIGN_BRACKET_LEVEL_FOR_CTAGS
   1340  1.1  rjs 	/* there has got to be a better way to do comsoft() */
   1341  1.1  rjs }}
   1342  1.1  rjs #endif
   1343  1.1  rjs 
   1344  1.1  rjs int
   1345  1.1  rjs sacomintr(arg)
   1346  1.1  rjs 	void *arg;
   1347  1.1  rjs {
   1348  1.1  rjs 	struct sacom_softc *sc = arg;
   1349  1.1  rjs 	bus_space_tag_t iot = sc->sc_iot;
   1350  1.1  rjs 	bus_space_handle_t ioh = sc->sc_ioh;
   1351  1.1  rjs 	u_char *put, *end;
   1352  1.1  rjs 	u_int cc;
   1353  1.1  rjs 	u_int sr0, sr1;
   1354  1.1  rjs 
   1355  1.1  rjs 	if (COM_ISALIVE(sc) == 0)
   1356  1.1  rjs 		return (0);
   1357  1.1  rjs 
   1358  1.1  rjs 	COM_LOCK(sc);
   1359  1.1  rjs 	sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
   1360  1.1  rjs 	if (! sr0) {
   1361  1.1  rjs 		COM_UNLOCK(sc);
   1362  1.1  rjs 		return (0);
   1363  1.1  rjs 	}
   1364  1.1  rjs 	if (ISSET(sr0, SR0_EIF))
   1365  1.1  rjs 		/* XXX silently discard error bits */
   1366  1.1  rjs 		bus_space_read_4(iot, ioh, SACOM_DR);
   1367  1.1  rjs 	if (ISSET(sr0, SR0_RBB))
   1368  1.1  rjs 		bus_space_write_4(iot, ioh, SACOM_SR0, SR0_RBB);
   1369  1.1  rjs 	if (ISSET(sr0, SR0_REB)) {
   1370  1.1  rjs 		bus_space_write_4(iot, ioh, SACOM_SR0, SR0_REB);
   1371  1.1  rjs #if defined(DDB) || defined(KGDB)
   1372  1.1  rjs #ifndef DDB_BREAK_CHAR
   1373  1.1  rjs 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1374  1.1  rjs 			console_debugger();
   1375  1.1  rjs 		}
   1376  1.1  rjs #endif
   1377  1.1  rjs #endif /* DDB || KGDB */
   1378  1.1  rjs 	}
   1379  1.1  rjs 
   1380  1.1  rjs 
   1381  1.1  rjs 	end = sc->sc_ebuf;
   1382  1.1  rjs 	put = sc->sc_rbput;
   1383  1.1  rjs 	cc = sc->sc_rbavail;
   1384  1.1  rjs 
   1385  1.1  rjs 	sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
   1386  1.1  rjs 	if (ISSET(sr0, SR0_RFS | SR0_RID)) {
   1387  1.1  rjs 		if (! ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1388  1.1  rjs 			while (cc > 0) {
   1389  1.1  rjs 				if (!ISSET(sr1, SR1_RNE)) {
   1390  1.1  rjs 					bus_space_write_4(iot, ioh, SACOM_SR0,
   1391  1.1  rjs 							  SR0_RID);
   1392  1.1  rjs 					break;
   1393  1.1  rjs 				}
   1394  1.1  rjs 				put[0] = bus_space_read_4(iot, ioh, SACOM_DR);
   1395  1.1  rjs 				put[1] = sr1;
   1396  1.1  rjs #if defined(DDB) && defined(DDB_BREAK_CHAR)
   1397  1.1  rjs 				if (put[0] == DDB_BREAK_CHAR &&
   1398  1.1  rjs 				    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
   1399  1.1  rjs 					console_debugger();
   1400  1.1  rjs 
   1401  1.1  rjs 					sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
   1402  1.1  rjs 					continue;
   1403  1.1  rjs 				}
   1404  1.1  rjs #endif
   1405  1.1  rjs 				put += 2;
   1406  1.1  rjs 				if (put >= end)
   1407  1.1  rjs 					put = sc->sc_rbuf;
   1408  1.1  rjs 				cc--;
   1409  1.1  rjs 
   1410  1.1  rjs 				sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
   1411  1.1  rjs 			}
   1412  1.1  rjs 
   1413  1.1  rjs 			/*
   1414  1.1  rjs 			 * Current string of incoming characters ended because
   1415  1.1  rjs 			 * no more data was available or we ran out of space.
   1416  1.1  rjs 			 * Schedule a receive event if any data was received.
   1417  1.1  rjs 			 * If we're out of space, turn off receive interrupts.
   1418  1.1  rjs 			 */
   1419  1.1  rjs 			sc->sc_rbput = put;
   1420  1.1  rjs 			sc->sc_rbavail = cc;
   1421  1.1  rjs 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1422  1.1  rjs 				sc->sc_rx_ready = 1;
   1423  1.1  rjs 
   1424  1.1  rjs 			/* XXX do RX hardware flow control */
   1425  1.1  rjs 
   1426  1.1  rjs 			/*
   1427  1.1  rjs 			 * If we're out of space, disable receive interrupts
   1428  1.1  rjs 			 * until the queue has drained a bit.
   1429  1.1  rjs 			 */
   1430  1.1  rjs 			if (!cc) {
   1431  1.1  rjs 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1432  1.1  rjs 				CLR(sc->sc_cr3, CR3_RIE);
   1433  1.1  rjs 				bus_space_write_4(iot, ioh, SACOM_CR3,
   1434  1.1  rjs 						  sc->sc_cr3);
   1435  1.1  rjs 			}
   1436  1.1  rjs 		} else {
   1437  1.1  rjs #ifdef DIAGNOSTIC
   1438  1.1  rjs 			panic("sacomintr: we shouldn't reach here\n");
   1439  1.1  rjs #endif
   1440  1.1  rjs 			CLR(sc->sc_cr3, CR3_RIE);
   1441  1.1  rjs 			bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
   1442  1.1  rjs 		}
   1443  1.1  rjs 	}
   1444  1.1  rjs 
   1445  1.1  rjs 	/*
   1446  1.1  rjs 	 * Done handling any receive interrupts. See if data can be
   1447  1.1  rjs 	 * transmitted as well. Schedule tx done event if no data left
   1448  1.1  rjs 	 * and tty was marked busy.
   1449  1.1  rjs 	 */
   1450  1.1  rjs 	sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
   1451  1.1  rjs 	if (ISSET(sr0, SR0_TFS)) {
   1452  1.1  rjs 		/*
   1453  1.1  rjs 		 * If we've delayed a parameter change, do it now, and restart
   1454  1.1  rjs 		 * output.
   1455  1.1  rjs 		 * XXX sacom_loadchanelregs() waits TX completion,
   1456  1.1  rjs 		 * XXX resulting in ~0.1s hang (300bps, 4 bytes) in worst case
   1457  1.1  rjs 		 */
   1458  1.1  rjs 		if (sc->sc_heldchange) {
   1459  1.1  rjs 			sacom_loadchannelregs(sc);
   1460  1.1  rjs 			sc->sc_heldchange = 0;
   1461  1.1  rjs 			sc->sc_tbc = sc->sc_heldtbc;
   1462  1.1  rjs 			sc->sc_heldtbc = 0;
   1463  1.1  rjs 		}
   1464  1.1  rjs 
   1465  1.1  rjs 		/* Output the next chunk of the contiguous buffer, if any. */
   1466  1.1  rjs 		if (sc->sc_tbc > 0) {
   1467  1.1  rjs 			sacom_filltx(sc);
   1468  1.1  rjs 		} else {
   1469  1.1  rjs 			/* Disable transmit completion interrupts if necessary. */
   1470  1.1  rjs 			if (ISSET(sc->sc_cr3, CR3_TIE)) {
   1471  1.1  rjs 				CLR(sc->sc_cr3, CR3_TIE);
   1472  1.1  rjs 				bus_space_write_4(iot, ioh, SACOM_CR3,
   1473  1.1  rjs 						  sc->sc_cr3);
   1474  1.1  rjs 			}
   1475  1.1  rjs 			if (sc->sc_tx_busy) {
   1476  1.1  rjs 				sc->sc_tx_busy = 0;
   1477  1.1  rjs 				sc->sc_tx_done = 1;
   1478  1.1  rjs 			}
   1479  1.1  rjs 		}
   1480  1.1  rjs 	}
   1481  1.1  rjs 	COM_UNLOCK(sc);
   1482  1.1  rjs 
   1483  1.1  rjs 	/* Wake up the poller. */
   1484  1.1  rjs #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1485  1.1  rjs 	softintr_schedule(sc->sc_si);
   1486  1.1  rjs #else
   1487  1.1  rjs 	setsoftserial();
   1488  1.1  rjs #endif
   1489  1.1  rjs 
   1490  1.1  rjs #if NRND > 0 && defined(RND_COM)
   1491  1.1  rjs 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   1492  1.1  rjs #endif
   1493  1.1  rjs 	return (1);
   1494  1.1  rjs }
   1495  1.1  rjs 
   1496  1.1  rjs /* Initialization for serial console */
   1497  1.1  rjs int
   1498  1.1  rjs sacominit(iot, iobase, baud, cflag, iohp)
   1499  1.1  rjs 	bus_space_tag_t iot;
   1500  1.1  rjs 	bus_addr_t iobase;
   1501  1.1  rjs 	int baud;
   1502  1.1  rjs 	tcflag_t cflag;
   1503  1.1  rjs 	bus_space_handle_t *iohp;
   1504  1.1  rjs {
   1505  1.1  rjs 	int brd, cr0;
   1506  1.1  rjs 
   1507  1.1  rjs 	if (bus_space_map(iot, iobase, SACOM_NPORTS, 0, iohp))
   1508  1.1  rjs 		printf("register map failed\n");
   1509  1.1  rjs 
   1510  1.1  rjs 	/* wait for the Tx queue to drain and disable the UART */
   1511  1.1  rjs 	while(bus_space_read_4(iot, *iohp, SACOM_SR1) & SR1_TBY)
   1512  1.1  rjs 		;
   1513  1.1  rjs 	bus_space_write_4(iot, *iohp, SACOM_CR3, 0);
   1514  1.1  rjs 
   1515  1.1  rjs 	cr0  = cflag2cr0(cflag);
   1516  1.1  rjs 	bus_space_write_4(iot, *iohp, SACOM_CR0, cr0);
   1517  1.1  rjs 
   1518  1.1  rjs 	brd = SACOMSPEED(baud);
   1519  1.1  rjs 	sacomconsrate = baud;
   1520  1.1  rjs 	sacomconsaddr = iobase;
   1521  1.1  rjs 	sacomconscflag = cflag;
   1522  1.1  rjs 	/* XXX assumes little endian */
   1523  1.1  rjs 	bus_space_write_4(iot, *iohp, SACOM_CR1, brd >> 8);
   1524  1.1  rjs 	bus_space_write_4(iot, *iohp, SACOM_CR2, brd & 0xff);
   1525  1.1  rjs 
   1526  1.1  rjs 	/* enable the UART */
   1527  1.1  rjs 	bus_space_write_4(iot, *iohp, SACOM_CR3, CR3_RXE | CR3_TXE);
   1528  1.1  rjs 
   1529  1.1  rjs 	return (0);
   1530  1.1  rjs }
   1531  1.1  rjs 
   1532  1.1  rjs void
   1533  1.1  rjs sacomcnprobe(cp)
   1534  1.1  rjs 	struct consdev *cp;
   1535  1.1  rjs {
   1536  1.1  rjs 	cp->cn_pri = CN_REMOTE;
   1537  1.1  rjs }
   1538  1.1  rjs 
   1539  1.1  rjs void
   1540  1.1  rjs sacomcninit(cp)
   1541  1.1  rjs 	struct consdev *cp;
   1542  1.1  rjs {
   1543  1.1  rjs 	if (cp == NULL) {
   1544  1.1  rjs 		/* XXX cp == NULL means that MMU is disabled. */
   1545  1.1  rjs 		sacomconsioh = SACOM3_HW_BASE;
   1546  1.1  rjs 		sacomconstag = &sa11x0_bs_tag;
   1547  1.1  rjs 		cn_tab = &sacomcons;
   1548  1.1  rjs 		return;
   1549  1.1  rjs 	}
   1550  1.1  rjs 
   1551  1.1  rjs 	if (sacominit(&sa11x0_bs_tag, CONADDR, CONSPEED,
   1552  1.1  rjs 			  CONMODE, &sacomconsioh))
   1553  1.1  rjs 		panic("can't init serial console @%x", CONADDR);
   1554  1.1  rjs 	cn_tab = &sacomcons;
   1555  1.1  rjs 	sacomconstag = &sa11x0_bs_tag;
   1556  1.1  rjs }
   1557  1.1  rjs 
   1558  1.1  rjs int
   1559  1.1  rjs sacomcngetc(dev)
   1560  1.1  rjs 	dev_t dev;
   1561  1.1  rjs {
   1562  1.1  rjs 	int c, s;
   1563  1.1  rjs 
   1564  1.1  rjs 	s = spltty();	/* XXX do we need this? */
   1565  1.1  rjs 
   1566  1.1  rjs 	while(! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1567  1.1  rjs 		 & SR1_RNE)) {
   1568  1.1  rjs #if defined(DDB) || defined(KGDB)
   1569  1.1  rjs #ifndef DDB_BREAK_CHAR
   1570  1.1  rjs 		u_int sr0;
   1571  1.1  rjs 		extern int db_active;
   1572  1.1  rjs 
   1573  1.1  rjs 		sr0 = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR0);
   1574  1.1  rjs 		if (ISSET(sr0, SR0_RBB))
   1575  1.1  rjs 			bus_space_write_4(sacomconstag, sacomconsioh,
   1576  1.1  rjs 					  SACOM_SR0, SR0_RBB);
   1577  1.1  rjs 		if (ISSET(sr0, SR0_REB)) {
   1578  1.1  rjs 			bus_space_write_4(sacomconstag, sacomconsioh,
   1579  1.1  rjs 					  SACOM_SR0, SR0_REB);
   1580  1.1  rjs 			if (db_active == 0)
   1581  1.1  rjs 				console_debugger();
   1582  1.1  rjs 		}
   1583  1.1  rjs #endif
   1584  1.1  rjs #endif /* DDB || KGDB */
   1585  1.1  rjs 	}
   1586  1.1  rjs 
   1587  1.1  rjs 	c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
   1588  1.1  rjs 	c &= 0xff;
   1589  1.1  rjs 	splx(s);
   1590  1.1  rjs 
   1591  1.1  rjs 	return (c);
   1592  1.1  rjs }
   1593  1.1  rjs 
   1594  1.1  rjs void
   1595  1.1  rjs sacomcnputc(dev, c)
   1596  1.1  rjs 	dev_t dev;
   1597  1.1  rjs 	int c;
   1598  1.1  rjs {
   1599  1.1  rjs 	int s;
   1600  1.1  rjs 
   1601  1.1  rjs 	s = spltty();	/* XXX do we need this? */
   1602  1.1  rjs 
   1603  1.1  rjs 	while(! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1604  1.1  rjs 		 & SR1_TNF))
   1605  1.1  rjs 		;
   1606  1.1  rjs 
   1607  1.1  rjs 	bus_space_write_4(sacomconstag, sacomconsioh, SACOM_DR, c);
   1608  1.1  rjs 	splx(s);
   1609  1.1  rjs }
   1610  1.1  rjs 
   1611  1.1  rjs void
   1612  1.1  rjs sacomcnpollc(dev, on)
   1613  1.1  rjs 	dev_t dev;
   1614  1.1  rjs 	int on;
   1615  1.1  rjs {
   1616  1.1  rjs 
   1617  1.1  rjs }
   1618  1.1  rjs 
   1619  1.1  rjs #ifdef DEBUG
   1620  1.1  rjs int
   1621  1.1  rjs sacomcncharpoll()
   1622  1.1  rjs {
   1623  1.1  rjs 	int c;
   1624  1.1  rjs 
   1625  1.1  rjs 	if (! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
   1626  1.1  rjs 		 & SR1_RNE))
   1627  1.1  rjs 		return -1;
   1628  1.1  rjs 
   1629  1.1  rjs 	c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
   1630  1.1  rjs 	c &= 0xff;
   1631  1.1  rjs 
   1632  1.1  rjs 	return (c);
   1633  1.1  rjs }
   1634  1.1  rjs 
   1635  1.1  rjs #endif
   1636  1.1  rjs 
   1637  1.1  rjs /* helper function to identify the com ports used by
   1638  1.1  rjs  console or KGDB (and not yet autoconf attached) */
   1639  1.1  rjs int
   1640  1.1  rjs sacom_is_console(iot, iobase, ioh)
   1641  1.1  rjs 	bus_space_tag_t iot;
   1642  1.1  rjs 	bus_addr_t iobase;
   1643  1.1  rjs 	bus_space_handle_t *ioh;
   1644  1.1  rjs {
   1645  1.1  rjs 	bus_space_handle_t help;
   1646  1.1  rjs 
   1647  1.1  rjs 	if (! sacomconsattached &&
   1648  1.1  rjs 	    iot == sacomconstag && iobase == sacomconsaddr)
   1649  1.1  rjs 		help = sacomconsioh;
   1650  1.1  rjs 	else
   1651  1.1  rjs 		return (0);
   1652  1.1  rjs 
   1653  1.1  rjs 	if (ioh)
   1654  1.1  rjs 		*ioh = help;
   1655  1.1  rjs 	return (1);
   1656  1.1  rjs }
   1657