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