Home | History | Annotate | Line # | Download | only in s3c2xx0
sscom.c revision 1.16.4.1
      1  1.16.4.1      elad /*	$NetBSD: sscom.c,v 1.16.4.1 2006/03/08 00:43:05 elad Exp $ */
      2       1.1       bsh 
      3       1.1       bsh /*
      4       1.9       bsh  * Copyright (c) 2002, 2003 Fujitsu Component Limited
      5       1.9       bsh  * Copyright (c) 2002, 2003 Genetec Corporation
      6       1.1       bsh  * All rights reserved.
      7       1.1       bsh  *
      8       1.1       bsh  * Redistribution and use in source and binary forms, with or without
      9       1.1       bsh  * modification, are permitted provided that the following conditions
     10       1.1       bsh  * are met:
     11       1.1       bsh  * 1. Redistributions of source code must retain the above copyright
     12       1.1       bsh  *    notice, this list of conditions and the following disclaimer.
     13       1.1       bsh  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1       bsh  *    notice, this list of conditions and the following disclaimer in the
     15       1.1       bsh  *    documentation and/or other materials provided with the distribution.
     16       1.1       bsh  * 3. Neither the name of The Fujitsu Component Limited nor the name of
     17       1.1       bsh  *    Genetec corporation may not be used to endorse or promote products
     18       1.1       bsh  *    derived from this software without specific prior written permission.
     19       1.1       bsh  *
     20       1.1       bsh  * THIS SOFTWARE IS PROVIDED BY FUJITSU COMPONENT LIMITED AND GENETEC
     21       1.1       bsh  * CORPORATION ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     22       1.1       bsh  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     23       1.1       bsh  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24       1.1       bsh  * DISCLAIMED.  IN NO EVENT SHALL FUJITSU COMPONENT LIMITED OR GENETEC
     25       1.1       bsh  * CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26       1.1       bsh  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     27       1.1       bsh  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     28       1.1       bsh  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     29       1.1       bsh  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30       1.1       bsh  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     31       1.1       bsh  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1       bsh  * SUCH DAMAGE.
     33       1.1       bsh  */
     34       1.1       bsh 
     35       1.1       bsh /*-
     36       1.1       bsh  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
     37       1.1       bsh  * All rights reserved.
     38       1.1       bsh  *
     39       1.1       bsh  * This code is derived from software contributed to The NetBSD Foundation
     40       1.1       bsh  * by Charles M. Hannum.
     41       1.1       bsh  *
     42       1.1       bsh  * Redistribution and use in source and binary forms, with or without
     43       1.1       bsh  * modification, are permitted provided that the following conditions
     44       1.1       bsh  * are met:
     45       1.1       bsh  * 1. Redistributions of source code must retain the above copyright
     46       1.1       bsh  *    notice, this list of conditions and the following disclaimer.
     47       1.1       bsh  * 2. Redistributions in binary form must reproduce the above copyright
     48       1.1       bsh  *    notice, this list of conditions and the following disclaimer in the
     49       1.1       bsh  *    documentation and/or other materials provided with the distribution.
     50       1.1       bsh  * 3. All advertising materials mentioning features or use of this software
     51       1.1       bsh  *    must display the following acknowledgement:
     52       1.1       bsh  *        This product includes software developed by the NetBSD
     53       1.1       bsh  *        Foundation, Inc. and its contributors.
     54       1.1       bsh  * 4. Neither the name of The NetBSD Foundation nor the names of its
     55       1.1       bsh  *    contributors may be used to endorse or promote products derived
     56       1.1       bsh  *    from this software without specific prior written permission.
     57       1.1       bsh  *
     58       1.1       bsh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     59       1.1       bsh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     60       1.1       bsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     61       1.1       bsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     62       1.1       bsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     63       1.1       bsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     64       1.1       bsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     65       1.1       bsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     66       1.1       bsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     67       1.1       bsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     68       1.1       bsh  * POSSIBILITY OF SUCH DAMAGE.
     69       1.1       bsh  */
     70       1.1       bsh 
     71       1.1       bsh /*
     72       1.1       bsh  * Copyright (c) 1991 The Regents of the University of California.
     73       1.1       bsh  * All rights reserved.
     74       1.1       bsh  *
     75       1.1       bsh  * Redistribution and use in source and binary forms, with or without
     76       1.1       bsh  * modification, are permitted provided that the following conditions
     77       1.1       bsh  * are met:
     78       1.1       bsh  * 1. Redistributions of source code must retain the above copyright
     79       1.1       bsh  *    notice, this list of conditions and the following disclaimer.
     80       1.1       bsh  * 2. Redistributions in binary form must reproduce the above copyright
     81       1.1       bsh  *    notice, this list of conditions and the following disclaimer in the
     82       1.1       bsh  *    documentation and/or other materials provided with the distribution.
     83      1.11       agc  * 3. Neither the name of the University nor the names of its contributors
     84       1.1       bsh  *    may be used to endorse or promote products derived from this software
     85       1.1       bsh  *    without specific prior written permission.
     86       1.1       bsh  *
     87       1.1       bsh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     88       1.1       bsh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     89       1.1       bsh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     90       1.1       bsh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     91       1.1       bsh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     92       1.1       bsh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     93       1.1       bsh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     94       1.1       bsh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     95       1.1       bsh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     96       1.1       bsh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     97       1.1       bsh  * SUCH DAMAGE.
     98       1.1       bsh  *
     99       1.1       bsh  *	@(#)com.c	7.5 (Berkeley) 5/16/91
    100       1.1       bsh  */
    101       1.1       bsh 
    102       1.1       bsh /*
    103       1.1       bsh  * Support integrated UARTs of Samsung S3C2800/2400X/2410X
    104       1.1       bsh  * Derived from sys/dev/ic/com.c
    105       1.1       bsh  */
    106       1.8     lukem 
    107       1.8     lukem #include <sys/cdefs.h>
    108  1.16.4.1      elad __KERNEL_RCSID(0, "$NetBSD: sscom.c,v 1.16.4.1 2006/03/08 00:43:05 elad Exp $");
    109       1.1       bsh 
    110       1.1       bsh #include "opt_sscom.h"
    111       1.1       bsh #include "opt_ddb.h"
    112       1.1       bsh #include "opt_kgdb.h"
    113       1.5    martin #include "opt_multiprocessor.h"
    114       1.5    martin #include "opt_lockdebug.h"
    115       1.1       bsh 
    116       1.1       bsh #include "rnd.h"
    117       1.1       bsh #if NRND > 0 && defined(RND_COM)
    118       1.1       bsh #include <sys/rnd.h>
    119       1.1       bsh #endif
    120       1.1       bsh 
    121       1.1       bsh /*
    122       1.1       bsh  * Override cnmagic(9) macro before including <sys/systm.h>.
    123       1.1       bsh  * We need to know if cn_check_magic triggered debugger, so set a flag.
    124       1.1       bsh  * Callers of cn_check_magic must declare int cn_trapped = 0;
    125       1.1       bsh  * XXX: this is *ugly*!
    126       1.1       bsh  */
    127       1.1       bsh #define cn_trap()				\
    128       1.1       bsh 	do {					\
    129       1.1       bsh 		console_debugger();		\
    130       1.1       bsh 		cn_trapped = 1;			\
    131       1.1       bsh 	} while (/* CONSTCOND */ 0)
    132       1.1       bsh 
    133       1.1       bsh #include <sys/param.h>
    134       1.1       bsh #include <sys/systm.h>
    135       1.1       bsh #include <sys/ioctl.h>
    136       1.1       bsh #include <sys/select.h>
    137       1.1       bsh #include <sys/tty.h>
    138       1.1       bsh #include <sys/proc.h>
    139       1.1       bsh #include <sys/user.h>
    140       1.1       bsh #include <sys/conf.h>
    141       1.1       bsh #include <sys/file.h>
    142       1.1       bsh #include <sys/uio.h>
    143       1.1       bsh #include <sys/kernel.h>
    144       1.1       bsh #include <sys/syslog.h>
    145       1.1       bsh #include <sys/types.h>
    146       1.1       bsh #include <sys/device.h>
    147       1.1       bsh #include <sys/malloc.h>
    148       1.1       bsh #include <sys/timepps.h>
    149       1.1       bsh #include <sys/vnode.h>
    150       1.1       bsh 
    151       1.1       bsh #include <machine/intr.h>
    152       1.1       bsh #include <machine/bus.h>
    153       1.1       bsh 
    154       1.1       bsh #include <arm/s3c2xx0/s3c2xx0reg.h>
    155       1.4       bsh #include <arm/s3c2xx0/s3c2xx0var.h>
    156      1.10       bsh #if defined(SSCOM_S3C2410) || defined(SSCOM_S3C2400)
    157      1.10       bsh #include <arm/s3c2xx0/s3c24x0reg.h>
    158      1.10       bsh #elif defined(SSCOM_S3C2800)
    159      1.10       bsh #include <arm/s3c2xx0/s3c2800reg.h>
    160      1.10       bsh #endif
    161       1.1       bsh #include <arm/s3c2xx0/sscom_var.h>
    162       1.1       bsh #include <dev/cons.h>
    163       1.1       bsh 
    164       1.1       bsh dev_type_open(sscomopen);
    165       1.1       bsh dev_type_close(sscomclose);
    166       1.1       bsh dev_type_read(sscomread);
    167       1.1       bsh dev_type_write(sscomwrite);
    168       1.1       bsh dev_type_ioctl(sscomioctl);
    169       1.1       bsh dev_type_stop(sscomstop);
    170       1.1       bsh dev_type_tty(sscomtty);
    171       1.1       bsh dev_type_poll(sscompoll);
    172       1.1       bsh 
    173       1.1       bsh int	sscomcngetc	(dev_t);
    174       1.1       bsh void	sscomcnputc	(dev_t, int);
    175       1.1       bsh void	sscomcnpollc	(dev_t, int);
    176       1.1       bsh 
    177       1.1       bsh #define	integrate	static inline
    178       1.1       bsh void 	sscomsoft	(void *);
    179       1.1       bsh 
    180       1.1       bsh integrate void sscom_rxsoft	(struct sscom_softc *, struct tty *);
    181       1.1       bsh integrate void sscom_txsoft	(struct sscom_softc *, struct tty *);
    182       1.1       bsh integrate void sscom_stsoft	(struct sscom_softc *, struct tty *);
    183       1.1       bsh integrate void sscom_schedrx	(struct sscom_softc *);
    184       1.1       bsh static void	sscom_modem(struct sscom_softc *, int);
    185       1.1       bsh static void	sscom_break(struct sscom_softc *, int);
    186       1.1       bsh static void	sscom_iflush(struct sscom_softc *);
    187       1.1       bsh static void	sscom_hwiflow(struct sscom_softc *);
    188       1.1       bsh static void	sscom_loadchannelregs(struct sscom_softc *);
    189       1.1       bsh static void	tiocm_to_sscom(struct sscom_softc *, u_long, int);
    190       1.1       bsh static int	sscom_to_tiocm(struct sscom_softc *);
    191       1.1       bsh static void	tiocm_to_sscom(struct sscom_softc *, u_long, int);
    192       1.1       bsh static int	sscom_to_tiocm(struct sscom_softc *);
    193       1.1       bsh static void	sscom_iflush(struct sscom_softc *);
    194       1.1       bsh 
    195       1.1       bsh static int	sscomhwiflow(struct tty *tp, int block);
    196       1.1       bsh static int	sscom_init(bus_space_tag_t, const struct sscom_uart_info *,
    197       1.1       bsh 		    int, int, tcflag_t, bus_space_handle_t *);
    198       1.1       bsh 
    199       1.1       bsh extern struct cfdriver sscom_cd;
    200       1.1       bsh 
    201       1.1       bsh const struct cdevsw sscom_cdevsw = {
    202       1.1       bsh 	sscomopen, sscomclose, sscomread, sscomwrite, sscomioctl,
    203       1.1       bsh 	sscomstop, sscomtty, sscompoll, nommap, ttykqfilter, D_TTY
    204       1.1       bsh };
    205       1.1       bsh 
    206       1.1       bsh /*
    207       1.1       bsh  * Make this an option variable one can patch.
    208       1.1       bsh  * But be warned:  this must be a power of 2!
    209       1.1       bsh  */
    210       1.1       bsh u_int sscom_rbuf_size = SSCOM_RING_SIZE;
    211       1.1       bsh 
    212       1.1       bsh /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    213       1.1       bsh u_int sscom_rbuf_hiwat = (SSCOM_RING_SIZE * 1) / 4;
    214       1.1       bsh u_int sscom_rbuf_lowat = (SSCOM_RING_SIZE * 3) / 4;
    215       1.1       bsh 
    216       1.1       bsh static int	sscomconsunit = -1;
    217       1.1       bsh static bus_space_tag_t sscomconstag;
    218       1.1       bsh static bus_space_handle_t sscomconsioh;
    219       1.1       bsh static int	sscomconsattached;
    220       1.1       bsh static int	sscomconsrate;
    221       1.1       bsh static tcflag_t sscomconscflag;
    222       1.1       bsh static struct cnm_state sscom_cnm_state;
    223       1.1       bsh 
    224       1.1       bsh #ifdef KGDB
    225       1.1       bsh #include <sys/kgdb.h>
    226       1.1       bsh 
    227       1.1       bsh static int sscom_kgdb_unit = -1;
    228       1.1       bsh static bus_space_tag_t sscom_kgdb_iot;
    229       1.1       bsh static bus_space_handle_t sscom_kgdb_ioh;
    230       1.1       bsh static int sscom_kgdb_attached;
    231       1.1       bsh 
    232       1.1       bsh int	sscom_kgdb_getc (void *);
    233       1.1       bsh void	sscom_kgdb_putc (void *, int);
    234       1.1       bsh #endif /* KGDB */
    235       1.1       bsh 
    236       1.1       bsh #define	SSCOMUNIT_MASK  	0x7f
    237       1.1       bsh #define	SSCOMDIALOUT_MASK	0x80
    238       1.1       bsh 
    239       1.1       bsh #define	SSCOMUNIT(x)	(minor(x) & SSCOMUNIT_MASK)
    240       1.1       bsh #define	SSCOMDIALOUT(x)	(minor(x) & SSCOMDIALOUT_MASK)
    241       1.1       bsh 
    242       1.1       bsh #if 0
    243       1.1       bsh #define	SSCOM_ISALIVE(sc)	((sc)->enabled != 0 && \
    244      1.16   thorpej 				 device_is_active(&(sc)->sc_dev))
    245       1.1       bsh #else
    246      1.16   thorpej #define	SSCOM_ISALIVE(sc)	device_is_active(&(sc)->sc_dev)
    247       1.1       bsh #endif
    248       1.1       bsh 
    249       1.1       bsh #define	BR	BUS_SPACE_BARRIER_READ
    250       1.1       bsh #define	BW	BUS_SPACE_BARRIER_WRITE
    251       1.1       bsh #define SSCOM_BARRIER(t, h, f) /* no-op */
    252       1.1       bsh 
    253       1.1       bsh #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(SSCOM_MPLOCK)
    254       1.1       bsh 
    255       1.1       bsh #define SSCOM_LOCK(sc) simple_lock(&(sc)->sc_lock)
    256       1.1       bsh #define SSCOM_UNLOCK(sc) simple_unlock(&(sc)->sc_lock)
    257       1.1       bsh 
    258       1.1       bsh #else
    259       1.1       bsh 
    260       1.1       bsh #define SSCOM_LOCK(sc)
    261       1.1       bsh #define SSCOM_UNLOCK(sc)
    262       1.1       bsh 
    263       1.1       bsh #endif
    264       1.1       bsh 
    265       1.1       bsh #ifndef SSCOM_TOLERANCE
    266       1.1       bsh #define	SSCOM_TOLERANCE	30	/* XXX: baud rate tolerance, in 0.1% units */
    267       1.1       bsh #endif
    268       1.1       bsh 
    269       1.1       bsh /* value for UCON */
    270       1.1       bsh #define UCON_RXINT_MASK	  \
    271       1.1       bsh 	(UCON_RXMODE_MASK|UCON_ERRINT|UCON_TOINT|UCON_RXINT_TYPE)
    272       1.1       bsh #define UCON_RXINT_ENABLE \
    273       1.1       bsh 	(UCON_RXMODE_INT|UCON_ERRINT|UCON_TOINT|UCON_RXINT_TYPE_LEVEL)
    274       1.1       bsh #define UCON_TXINT_MASK   (UCON_TXMODE_MASK|UCON_TXINT_TYPE)
    275       1.1       bsh #define UCON_TXINT_ENABLE (UCON_TXMODE_INT|UCON_TXINT_TYPE_LEVEL)
    276       1.1       bsh 
    277       1.1       bsh /* we don't want tx interrupt on debug port, but it is needed to
    278       1.1       bsh    have transmitter active */
    279       1.1       bsh #define UCON_DEBUGPORT	  (UCON_RXINT_ENABLE|UCON_TXINT_ENABLE)
    280       1.1       bsh 
    281       1.1       bsh 
    282      1.15     perry static inline void
    283       1.9       bsh __sscom_output_chunk(struct sscom_softc *sc, int ufstat)
    284       1.1       bsh {
    285       1.1       bsh 	int n, space;
    286       1.1       bsh 	bus_space_tag_t iot = sc->sc_iot;
    287       1.1       bsh 	bus_space_handle_t ioh = sc->sc_ioh;
    288       1.1       bsh 
    289       1.1       bsh 	n = sc->sc_tbc;
    290       1.9       bsh 	space = 16 - ((ufstat & UFSTAT_TXCOUNT) >> UFSTAT_TXCOUNT_SHIFT);
    291       1.1       bsh 
    292       1.1       bsh 	if (n > space)
    293       1.1       bsh 		n = space;
    294       1.1       bsh 
    295       1.3       bsh 	if (n > 0) {
    296       1.1       bsh 		bus_space_write_multi_1(iot, ioh, SSCOM_UTXH, sc->sc_tba, n);
    297       1.1       bsh 		sc->sc_tbc -= n;
    298       1.1       bsh 		sc->sc_tba += n;
    299       1.1       bsh 	}
    300       1.1       bsh }
    301       1.1       bsh 
    302       1.9       bsh static void
    303       1.9       bsh sscom_output_chunk(struct sscom_softc *sc)
    304       1.9       bsh {
    305       1.9       bsh 	int ufstat = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SSCOM_UFSTAT);
    306       1.1       bsh 
    307       1.9       bsh 	if (!(ufstat & UFSTAT_TXFULL))
    308       1.9       bsh 		__sscom_output_chunk(sc, ufstat);
    309       1.9       bsh }
    310       1.1       bsh 
    311       1.1       bsh int
    312       1.1       bsh sscomspeed(long speed, long frequency)
    313       1.1       bsh {
    314       1.1       bsh #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    315       1.1       bsh 
    316       1.1       bsh 	int x, err;
    317       1.1       bsh 
    318       1.1       bsh 	if (speed <= 0)
    319       1.1       bsh 		return -1;
    320       1.1       bsh 	x = divrnd(frequency / 16, speed);
    321       1.1       bsh 	if (x <= 0)
    322       1.1       bsh 		return -1;
    323       1.1       bsh 	err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
    324       1.1       bsh 	if (err < 0)
    325       1.1       bsh 		err = -err;
    326       1.1       bsh 	if (err > SSCOM_TOLERANCE)
    327       1.1       bsh 		return -1;
    328       1.1       bsh 	return x-1;
    329       1.1       bsh 
    330       1.1       bsh #undef	divrnd
    331       1.1       bsh }
    332       1.1       bsh 
    333      1.12        he void sscomstatus (struct sscom_softc *, const char *);
    334       1.1       bsh 
    335       1.1       bsh #ifdef SSCOM_DEBUG
    336       1.1       bsh int	sscom_debug = 0;
    337       1.1       bsh 
    338       1.1       bsh void
    339      1.12        he sscomstatus(struct sscom_softc *sc, const char *str)
    340       1.1       bsh {
    341       1.1       bsh 	struct tty *tp = sc->sc_tty;
    342       1.1       bsh 	int umstat = bus_space_read_1(sc->sc_iot, sc->sc_iot, SSCOM_UMSTAT);
    343       1.1       bsh 	int umcon = bus_space_read_1(sc->sc_iot, sc->sc_iot, SSCOM_UMCON);
    344       1.1       bsh 
    345       1.1       bsh 	printf("%s: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
    346       1.1       bsh 	    sc->sc_dev.dv_xname, str,
    347       1.1       bsh 	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
    348       1.1       bsh 	    "+",			/* DCD */
    349       1.1       bsh 	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
    350       1.1       bsh 	    "+",			/* DTR */
    351       1.1       bsh 	    sc->sc_tx_stopped ? "+" : "-");
    352       1.1       bsh 
    353       1.1       bsh 	printf("%s: %s %scrtscts %scts %sts_ttstop  %srts %xrx_flags\n",
    354       1.1       bsh 	    sc->sc_dev.dv_xname, str,
    355       1.1       bsh 	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
    356       1.1       bsh 	    ISSET(umstat, UMSTAT_CTS) ? "+" : "-",
    357       1.1       bsh 	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
    358       1.1       bsh 	    ISSET(umcon, UMCON_RTS) ? "+" : "-",
    359       1.1       bsh 	    sc->sc_rx_flags);
    360       1.1       bsh }
    361       1.1       bsh #else
    362       1.1       bsh #define sscom_debug  0
    363       1.1       bsh #endif
    364       1.1       bsh 
    365       1.1       bsh static void
    366       1.1       bsh sscom_enable_debugport(struct sscom_softc *sc)
    367       1.1       bsh {
    368       1.1       bsh 	int s;
    369       1.1       bsh 
    370       1.1       bsh 	/* Turn on line break interrupt, set carrier. */
    371       1.1       bsh 	s = splserial();
    372       1.1       bsh 	SSCOM_LOCK(sc);
    373       1.1       bsh 	sc->sc_ucon = UCON_DEBUGPORT;
    374       1.1       bsh 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SSCOM_UCON, sc->sc_ucon);
    375       1.1       bsh 	sc->sc_umcon = UMCON_RTS|UMCON_DTR;
    376       1.1       bsh 	sc->set_modem_control(sc);
    377       1.1       bsh 	sscom_enable_rxint(sc);
    378       1.1       bsh 	sscom_disable_txint(sc);
    379       1.1       bsh 	SSCOM_UNLOCK(sc);
    380       1.1       bsh 	splx(s);
    381       1.1       bsh }
    382       1.1       bsh 
    383       1.1       bsh static void
    384       1.1       bsh sscom_set_modem_control(struct sscom_softc *sc)
    385       1.1       bsh {
    386       1.1       bsh 	/* flob RTS */
    387       1.1       bsh 	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    388       1.1       bsh 	    SSCOM_UMCON, sc->sc_umcon & UMCON_HW_MASK);
    389       1.1       bsh 	/* ignore DTR */
    390       1.1       bsh }
    391       1.1       bsh 
    392       1.1       bsh static int
    393       1.1       bsh sscom_read_modem_status(struct sscom_softc *sc)
    394       1.1       bsh {
    395       1.1       bsh 	int msts;
    396       1.1       bsh 
    397       1.1       bsh 	msts = bus_space_read_1(sc->sc_iot, sc->sc_ioh, SSCOM_UMSTAT);
    398       1.1       bsh 
    399       1.1       bsh 	/* DCD and DSR are always on */
    400       1.1       bsh 	return (msts & UMSTAT_CTS) | MSTS_DCD | MSTS_DSR;
    401       1.1       bsh }
    402       1.1       bsh 
    403       1.1       bsh void
    404       1.1       bsh sscom_attach_subr(struct sscom_softc *sc)
    405       1.1       bsh {
    406       1.1       bsh 	int unit = sc->sc_unit;
    407       1.1       bsh 	bus_space_tag_t iot = sc->sc_iot;
    408       1.1       bsh 	bus_space_handle_t ioh = sc->sc_ioh;
    409       1.1       bsh 	struct tty *tp;
    410       1.1       bsh 
    411       1.1       bsh 	callout_init(&sc->sc_diag_callout);
    412       1.1       bsh #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(SSCOM_MPLOCK)
    413       1.1       bsh 	simple_lock_init(&sc->sc_lock);
    414       1.1       bsh #endif
    415       1.1       bsh 
    416       1.1       bsh 	sc->sc_ucon = UCON_RXINT_ENABLE|UCON_TXINT_ENABLE;
    417       1.1       bsh 
    418       1.1       bsh 	/*
    419       1.1       bsh 	 * set default for modem control hook
    420       1.1       bsh 	 */
    421       1.3       bsh 	if (sc->set_modem_control == NULL)
    422       1.1       bsh 		sc->set_modem_control = sscom_set_modem_control;
    423       1.3       bsh 	if (sc->read_modem_status == NULL)
    424       1.1       bsh 		sc->read_modem_status = sscom_read_modem_status;
    425       1.1       bsh 
    426       1.1       bsh 	/* Disable interrupts before configuring the device. */
    427       1.1       bsh 	sscom_disable_txrxint(sc);
    428       1.1       bsh 
    429       1.1       bsh #ifdef KGDB
    430       1.1       bsh 	/*
    431       1.1       bsh 	 * Allow kgdb to "take over" this port.  If this is
    432       1.1       bsh 	 * the kgdb device, it has exclusive use.
    433       1.1       bsh 	 */
    434       1.3       bsh 	if (unit == sscom_kgdb_unit) {
    435       1.1       bsh 		SET(sc->sc_hwflags, SSCOM_HW_KGDB);
    436       1.1       bsh 		sc->sc_ucon = UCON_DEBUGPORT;
    437       1.1       bsh 	}
    438       1.1       bsh #endif
    439       1.1       bsh 
    440       1.1       bsh 	if (unit == sscomconsunit) {
    441       1.1       bsh 		sscomconsattached = 1;
    442       1.1       bsh 
    443       1.1       bsh 		sscomconstag = iot;
    444       1.1       bsh 		sscomconsioh = ioh;
    445       1.1       bsh 
    446       1.1       bsh 		/* Make sure the console is always "hardwired". */
    447       1.1       bsh 		delay(1000);			/* XXX: wait for output to finish */
    448       1.1       bsh 		SET(sc->sc_hwflags, SSCOM_HW_CONSOLE);
    449       1.1       bsh 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    450       1.1       bsh 
    451       1.1       bsh 		sc->sc_ucon = UCON_DEBUGPORT;
    452       1.1       bsh 	}
    453       1.1       bsh 
    454       1.1       bsh 	bus_space_write_1(iot, ioh, SSCOM_UFCON,
    455       1.1       bsh 	    UFCON_TXTRIGGER_8|UFCON_RXTRIGGER_8|UFCON_FIFO_ENABLE|
    456       1.1       bsh 	    UFCON_TXFIFO_RESET|UFCON_RXFIFO_RESET);
    457       1.1       bsh 
    458       1.1       bsh 	bus_space_write_1(iot, ioh, SSCOM_UCON, sc->sc_ucon);
    459       1.1       bsh 
    460       1.1       bsh #ifdef KGDB
    461       1.3       bsh 	if (ISSET(sc->sc_hwflags, SSCOM_HW_KGDB)) {
    462       1.1       bsh 		sscom_kgdb_attached = 1;
    463       1.1       bsh 		printf("%s: kgdb\n", sc->sc_dev.dv_xname);
    464       1.1       bsh 		sscom_enable_debugport(sc);
    465       1.1       bsh 		return;
    466       1.1       bsh 	}
    467       1.1       bsh #endif
    468       1.1       bsh 
    469       1.1       bsh 
    470       1.1       bsh 
    471       1.1       bsh 	tp = ttymalloc();
    472       1.1       bsh 	tp->t_oproc = sscomstart;
    473       1.1       bsh 	tp->t_param = sscomparam;
    474       1.1       bsh 	tp->t_hwiflow = sscomhwiflow;
    475       1.1       bsh 
    476       1.1       bsh 	sc->sc_tty = tp;
    477       1.1       bsh 	sc->sc_rbuf = malloc(sscom_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
    478       1.1       bsh 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    479       1.1       bsh 	sc->sc_rbavail = sscom_rbuf_size;
    480       1.1       bsh 	if (sc->sc_rbuf == NULL) {
    481       1.1       bsh 		printf("%s: unable to allocate ring buffer\n",
    482       1.1       bsh 		    sc->sc_dev.dv_xname);
    483       1.1       bsh 		return;
    484       1.1       bsh 	}
    485       1.1       bsh 	sc->sc_ebuf = sc->sc_rbuf + (sscom_rbuf_size << 1);
    486       1.1       bsh 
    487       1.1       bsh 	tty_attach(tp);
    488       1.1       bsh 
    489       1.1       bsh 	if (ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE)) {
    490       1.1       bsh 		int maj;
    491       1.1       bsh 
    492       1.1       bsh 		/* locate the major number */
    493       1.1       bsh 		maj = cdevsw_lookup_major(&sscom_cdevsw);
    494       1.1       bsh 
    495       1.1       bsh 		cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
    496       1.1       bsh 
    497       1.1       bsh 		printf("%s: console (major=%d)\n", sc->sc_dev.dv_xname, maj);
    498       1.1       bsh 	}
    499       1.1       bsh 
    500       1.1       bsh 
    501       1.1       bsh 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, sscomsoft, sc);
    502       1.1       bsh 
    503       1.1       bsh #if NRND > 0 && defined(RND_COM)
    504       1.1       bsh 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    505       1.1       bsh 			  RND_TYPE_TTY, 0);
    506       1.1       bsh #endif
    507       1.1       bsh 
    508       1.1       bsh 	/* if there are no enable/disable functions, assume the device
    509       1.1       bsh 	   is always enabled */
    510       1.1       bsh 
    511       1.3       bsh 	if (ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE))
    512       1.1       bsh 		sscom_enable_debugport(sc);
    513       1.1       bsh 	else
    514       1.1       bsh 		sscom_disable_txrxint(sc);
    515       1.1       bsh 
    516       1.1       bsh 	SET(sc->sc_hwflags, SSCOM_HW_DEV_OK);
    517       1.1       bsh }
    518       1.1       bsh 
    519       1.1       bsh int
    520       1.1       bsh sscom_detach(struct device *self, int flags)
    521       1.1       bsh {
    522       1.1       bsh 	return 0;
    523       1.1       bsh }
    524       1.1       bsh 
    525       1.1       bsh int
    526       1.1       bsh sscom_activate(struct device *self, enum devact act)
    527       1.1       bsh {
    528       1.1       bsh #ifdef notyet
    529       1.1       bsh 	struct sscom_softc *sc = (struct sscom_softc *)self;
    530       1.1       bsh 	int s, rv = 0;
    531       1.1       bsh 
    532       1.1       bsh 	s = splserial();
    533       1.1       bsh 	SSCOM_LOCK(sc);
    534       1.1       bsh 	switch (act) {
    535       1.1       bsh 	case DVACT_ACTIVATE:
    536       1.1       bsh 		rv = EOPNOTSUPP;
    537       1.1       bsh 		break;
    538       1.1       bsh 
    539       1.1       bsh 	case DVACT_DEACTIVATE:
    540       1.1       bsh 		if (sc->sc_hwflags & (SSCOM_HW_CONSOLE|SSCOM_HW_KGDB)) {
    541       1.1       bsh 			rv = EBUSY;
    542       1.1       bsh 			break;
    543       1.1       bsh 		}
    544       1.1       bsh 
    545       1.1       bsh 		sc->enabled = 0;
    546       1.1       bsh 		break;
    547       1.1       bsh 	}
    548       1.1       bsh 
    549       1.1       bsh 	SSCOM_UNLOCK(sc);
    550       1.1       bsh 	splx(s);
    551       1.1       bsh 	return rv;
    552       1.1       bsh #else
    553       1.1       bsh 	return 0;
    554       1.1       bsh #endif
    555       1.1       bsh }
    556       1.1       bsh 
    557       1.1       bsh void
    558       1.1       bsh sscom_shutdown(struct sscom_softc *sc)
    559       1.1       bsh {
    560       1.1       bsh #ifdef notyet
    561       1.1       bsh 	struct tty *tp = sc->sc_tty;
    562       1.1       bsh 	int s;
    563       1.1       bsh 
    564       1.1       bsh 	s = splserial();
    565       1.1       bsh 	SSCOM_LOCK(sc);
    566       1.1       bsh 
    567       1.1       bsh 	/* If we were asserting flow control, then deassert it. */
    568       1.1       bsh 	SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
    569       1.1       bsh 	sscom_hwiflow(sc);
    570       1.1       bsh 
    571       1.1       bsh 	/* Clear any break condition set with TIOCSBRK. */
    572       1.1       bsh 	sscom_break(sc, 0);
    573       1.1       bsh 
    574       1.1       bsh 	/*
    575       1.1       bsh 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    576       1.1       bsh 	 * notice even if we immediately open the port again.
    577       1.1       bsh 	 * Avoid tsleeping above splhigh().
    578       1.1       bsh 	 */
    579       1.1       bsh 	if (ISSET(tp->t_cflag, HUPCL)) {
    580       1.1       bsh 		sscom_modem(sc, 0);
    581       1.1       bsh 		SSCOM_UNLOCK(sc);
    582       1.1       bsh 		splx(s);
    583       1.1       bsh 		/* XXX tsleep will only timeout */
    584       1.1       bsh 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    585       1.1       bsh 		s = splserial();
    586       1.1       bsh 		SSCOM_LOCK(sc);
    587       1.1       bsh 	}
    588       1.1       bsh 
    589       1.1       bsh 	if (ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE))
    590       1.1       bsh 		/* interrupt on break */
    591       1.1       bsh 		sc->sc_ucon = UCON_DEBUGPORT;
    592       1.1       bsh 	else
    593       1.1       bsh 		sc->sc_ucon = 0;
    594       1.1       bsh 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SSCOM_UCON, sc->sc_ucon);
    595       1.1       bsh 
    596       1.1       bsh #ifdef DIAGNOSTIC
    597       1.1       bsh 	if (!sc->enabled)
    598       1.1       bsh 		panic("sscom_shutdown: not enabled?");
    599       1.1       bsh #endif
    600       1.1       bsh 	sc->enabled = 0;
    601       1.1       bsh 	SSCOM_UNLOCK(sc);
    602       1.1       bsh 	splx(s);
    603       1.1       bsh #endif
    604       1.1       bsh }
    605       1.1       bsh 
    606       1.1       bsh int
    607      1.14  christos sscomopen(dev_t dev, int flag, int mode, struct lwp *l)
    608       1.1       bsh {
    609       1.1       bsh 	struct sscom_softc *sc;
    610       1.1       bsh 	struct tty *tp;
    611       1.1       bsh 	int s, s2;
    612       1.1       bsh 	int error;
    613       1.1       bsh 
    614       1.1       bsh 	sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    615       1.1       bsh 	if (sc == NULL || !ISSET(sc->sc_hwflags, SSCOM_HW_DEV_OK) ||
    616       1.1       bsh 		sc->sc_rbuf == NULL)
    617       1.1       bsh 		return ENXIO;
    618       1.1       bsh 
    619      1.16   thorpej 	if (!device_is_active(&sc->sc_dev))
    620       1.1       bsh 		return ENXIO;
    621       1.1       bsh 
    622       1.1       bsh #ifdef KGDB
    623       1.1       bsh 	/*
    624       1.1       bsh 	 * If this is the kgdb port, no other use is permitted.
    625       1.1       bsh 	 */
    626       1.1       bsh 	if (ISSET(sc->sc_hwflags, SSCOM_HW_KGDB))
    627       1.1       bsh 		return EBUSY;
    628       1.1       bsh #endif
    629       1.1       bsh 
    630       1.1       bsh 	tp = sc->sc_tty;
    631       1.1       bsh 
    632       1.1       bsh 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    633       1.1       bsh 	    ISSET(tp->t_state, TS_XCLUDE) &&
    634  1.16.4.1      elad 	    generic_authorize(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER, &l->l_proc->p_acflag) != 0)
    635       1.1       bsh 		return EBUSY;
    636       1.1       bsh 
    637       1.1       bsh 	s = spltty();
    638       1.1       bsh 
    639       1.1       bsh 	/*
    640       1.1       bsh 	 * Do the following iff this is a first open.
    641       1.1       bsh 	 */
    642       1.1       bsh 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    643       1.1       bsh 		struct termios t;
    644       1.1       bsh 
    645       1.1       bsh 		tp->t_dev = dev;
    646       1.1       bsh 
    647       1.1       bsh 		s2 = splserial();
    648       1.1       bsh 		SSCOM_LOCK(sc);
    649       1.1       bsh 
    650       1.1       bsh 		/* Turn on interrupts. */
    651       1.1       bsh 		sscom_enable_txrxint(sc);
    652       1.1       bsh 
    653       1.1       bsh 		/* Fetch the current modem control status, needed later. */
    654       1.1       bsh 		sc->sc_msts = sc->read_modem_status(sc);
    655       1.1       bsh 
    656       1.1       bsh #if 0
    657       1.1       bsh 		/* Clear PPS capture state on first open. */
    658       1.1       bsh 		sc->sc_ppsmask = 0;
    659       1.1       bsh 		sc->ppsparam.mode = 0;
    660       1.1       bsh #endif
    661       1.1       bsh 
    662       1.1       bsh 		SSCOM_UNLOCK(sc);
    663       1.1       bsh 		splx(s2);
    664       1.1       bsh 
    665       1.1       bsh 		/*
    666       1.1       bsh 		 * Initialize the termios status to the defaults.  Add in the
    667       1.1       bsh 		 * sticky bits from TIOCSFLAGS.
    668       1.1       bsh 		 */
    669       1.1       bsh 		t.c_ispeed = 0;
    670       1.1       bsh 		if (ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE)) {
    671       1.1       bsh 			t.c_ospeed = sscomconsrate;
    672       1.1       bsh 			t.c_cflag = sscomconscflag;
    673       1.1       bsh 		} else {
    674       1.1       bsh 			t.c_ospeed = TTYDEF_SPEED;
    675       1.1       bsh 			t.c_cflag = TTYDEF_CFLAG;
    676       1.1       bsh 		}
    677       1.1       bsh 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    678       1.1       bsh 			SET(t.c_cflag, CLOCAL);
    679       1.1       bsh 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    680       1.1       bsh 			SET(t.c_cflag, CRTSCTS);
    681       1.1       bsh 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    682       1.1       bsh 			SET(t.c_cflag, MDMBUF);
    683       1.1       bsh 		/* Make sure sscomparam() will do something. */
    684       1.1       bsh 		tp->t_ospeed = 0;
    685       1.1       bsh 		(void) sscomparam(tp, &t);
    686       1.1       bsh 		tp->t_iflag = TTYDEF_IFLAG;
    687       1.1       bsh 		tp->t_oflag = TTYDEF_OFLAG;
    688       1.1       bsh 		tp->t_lflag = TTYDEF_LFLAG;
    689       1.1       bsh 		ttychars(tp);
    690       1.1       bsh 		ttsetwater(tp);
    691       1.1       bsh 
    692       1.1       bsh 		s2 = splserial();
    693       1.1       bsh 		SSCOM_LOCK(sc);
    694       1.1       bsh 
    695       1.1       bsh 		/*
    696       1.1       bsh 		 * Turn on DTR.  We must always do this, even if carrier is not
    697       1.1       bsh 		 * present, because otherwise we'd have to use TIOCSDTR
    698       1.1       bsh 		 * immediately after setting CLOCAL, which applications do not
    699       1.1       bsh 		 * expect.  We always assert DTR while the device is open
    700       1.1       bsh 		 * unless explicitly requested to deassert it.
    701       1.1       bsh 		 */
    702       1.1       bsh 		sscom_modem(sc, 1);
    703       1.1       bsh 
    704       1.1       bsh 		/* Clear the input ring, and unblock. */
    705       1.1       bsh 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    706       1.1       bsh 		sc->sc_rbavail = sscom_rbuf_size;
    707       1.1       bsh 		sscom_iflush(sc);
    708       1.1       bsh 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    709       1.1       bsh 		sscom_hwiflow(sc);
    710       1.1       bsh 
    711       1.1       bsh 		if (sscom_debug)
    712       1.1       bsh 			sscomstatus(sc, "sscomopen  ");
    713       1.1       bsh 
    714       1.1       bsh 		SSCOM_UNLOCK(sc);
    715       1.1       bsh 		splx(s2);
    716       1.1       bsh 	}
    717       1.1       bsh 
    718       1.1       bsh 	splx(s);
    719       1.1       bsh 
    720       1.1       bsh 	error = ttyopen(tp, SSCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    721       1.1       bsh 	if (error)
    722       1.1       bsh 		goto bad;
    723       1.1       bsh 
    724       1.1       bsh 	error = (*tp->t_linesw->l_open)(dev, tp);
    725       1.1       bsh 	if (error)
    726       1.1       bsh 		goto bad;
    727       1.1       bsh 
    728       1.1       bsh 	return 0;
    729       1.1       bsh 
    730       1.1       bsh bad:
    731       1.1       bsh 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    732       1.1       bsh 		/*
    733       1.1       bsh 		 * We failed to open the device, and nobody else had it opened.
    734       1.1       bsh 		 * Clean up the state as appropriate.
    735       1.1       bsh 		 */
    736       1.1       bsh 		sscom_shutdown(sc);
    737       1.1       bsh 	}
    738       1.1       bsh 
    739       1.1       bsh 	return error;
    740       1.1       bsh }
    741       1.1       bsh 
    742       1.1       bsh int
    743      1.14  christos sscomclose(dev_t dev, int flag, int mode, struct lwp *l)
    744       1.1       bsh {
    745       1.1       bsh 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    746       1.1       bsh 	struct tty *tp = sc->sc_tty;
    747       1.1       bsh 
    748       1.1       bsh 	/* XXX This is for cons.c. */
    749       1.1       bsh 	if (!ISSET(tp->t_state, TS_ISOPEN))
    750       1.1       bsh 		return 0;
    751       1.1       bsh 
    752       1.1       bsh 	(*tp->t_linesw->l_close)(tp, flag);
    753       1.1       bsh 	ttyclose(tp);
    754       1.1       bsh 
    755       1.1       bsh 	if (SSCOM_ISALIVE(sc) == 0)
    756       1.1       bsh 		return 0;
    757       1.1       bsh 
    758       1.1       bsh 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    759       1.1       bsh 		/*
    760       1.1       bsh 		 * Although we got a last close, the device may still be in
    761       1.1       bsh 		 * use; e.g. if this was the dialout node, and there are still
    762       1.1       bsh 		 * processes waiting for carrier on the non-dialout node.
    763       1.1       bsh 		 */
    764       1.1       bsh 		sscom_shutdown(sc);
    765       1.1       bsh 	}
    766       1.1       bsh 
    767       1.1       bsh 	return 0;
    768       1.1       bsh }
    769       1.1       bsh 
    770       1.1       bsh int
    771       1.1       bsh sscomread(dev_t dev, struct uio *uio, int flag)
    772       1.1       bsh {
    773       1.1       bsh 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    774       1.1       bsh 	struct tty *tp = sc->sc_tty;
    775       1.1       bsh 
    776       1.1       bsh 	if (SSCOM_ISALIVE(sc) == 0)
    777       1.1       bsh 		return EIO;
    778       1.1       bsh 
    779       1.1       bsh 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    780       1.1       bsh }
    781       1.1       bsh 
    782       1.1       bsh int
    783       1.1       bsh sscomwrite(dev_t dev, struct uio *uio, int flag)
    784       1.1       bsh {
    785       1.1       bsh 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    786       1.1       bsh 	struct tty *tp = sc->sc_tty;
    787       1.1       bsh 
    788       1.1       bsh 	if (SSCOM_ISALIVE(sc) == 0)
    789       1.1       bsh 		return EIO;
    790       1.1       bsh 
    791       1.1       bsh 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    792       1.1       bsh }
    793       1.1       bsh 
    794       1.1       bsh int
    795      1.14  christos sscompoll(dev_t dev, int events, struct lwp *l)
    796       1.1       bsh {
    797       1.1       bsh 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    798       1.1       bsh 	struct tty *tp = sc->sc_tty;
    799       1.1       bsh 
    800       1.1       bsh 	if (SSCOM_ISALIVE(sc) == 0)
    801       1.1       bsh 		return EIO;
    802       1.1       bsh 
    803      1.14  christos 	return (*tp->t_linesw->l_poll)(tp, events, l);
    804       1.1       bsh }
    805       1.1       bsh 
    806       1.1       bsh struct tty *
    807       1.1       bsh sscomtty(dev_t dev)
    808       1.1       bsh {
    809       1.1       bsh 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    810       1.1       bsh 	struct tty *tp = sc->sc_tty;
    811       1.1       bsh 
    812       1.1       bsh 	return tp;
    813       1.1       bsh }
    814       1.1       bsh 
    815       1.1       bsh int
    816      1.14  christos sscomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    817       1.1       bsh {
    818       1.1       bsh 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
    819       1.1       bsh 	struct tty *tp = sc->sc_tty;
    820       1.1       bsh 	int error;
    821       1.1       bsh 	int s;
    822       1.1       bsh 
    823       1.1       bsh 	if (SSCOM_ISALIVE(sc) == 0)
    824       1.1       bsh 		return EIO;
    825       1.1       bsh 
    826      1.14  christos 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    827       1.1       bsh 	if (error != EPASSTHROUGH)
    828       1.1       bsh 		return error;
    829       1.1       bsh 
    830      1.14  christos 	error = ttioctl(tp, cmd, data, flag, l);
    831       1.1       bsh 	if (error != EPASSTHROUGH)
    832       1.1       bsh 		return error;
    833       1.1       bsh 
    834       1.1       bsh 	error = 0;
    835       1.1       bsh 
    836       1.1       bsh 	s = splserial();
    837       1.1       bsh 	SSCOM_LOCK(sc);
    838       1.1       bsh 
    839       1.1       bsh 	switch (cmd) {
    840       1.1       bsh 	case TIOCSBRK:
    841       1.1       bsh 		sscom_break(sc, 1);
    842       1.1       bsh 		break;
    843       1.1       bsh 
    844       1.1       bsh 	case TIOCCBRK:
    845       1.1       bsh 		sscom_break(sc, 0);
    846       1.1       bsh 		break;
    847       1.1       bsh 
    848       1.1       bsh 	case TIOCSDTR:
    849       1.1       bsh 		sscom_modem(sc, 1);
    850       1.1       bsh 		break;
    851       1.1       bsh 
    852       1.1       bsh 	case TIOCCDTR:
    853       1.1       bsh 		sscom_modem(sc, 0);
    854       1.1       bsh 		break;
    855       1.1       bsh 
    856       1.1       bsh 	case TIOCGFLAGS:
    857       1.1       bsh 		*(int *)data = sc->sc_swflags;
    858       1.1       bsh 		break;
    859       1.1       bsh 
    860       1.1       bsh 	case TIOCSFLAGS:
    861  1.16.4.1      elad 		error = generic_authorize(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER, &l->l_proc->p_acflag);
    862       1.1       bsh 		if (error)
    863       1.1       bsh 			break;
    864       1.1       bsh 		sc->sc_swflags = *(int *)data;
    865       1.1       bsh 		break;
    866       1.1       bsh 
    867       1.1       bsh 	case TIOCMSET:
    868       1.1       bsh 	case TIOCMBIS:
    869       1.1       bsh 	case TIOCMBIC:
    870       1.1       bsh 		tiocm_to_sscom(sc, cmd, *(int *)data);
    871       1.1       bsh 		break;
    872       1.1       bsh 
    873       1.1       bsh 	case TIOCMGET:
    874       1.1       bsh 		*(int *)data = sscom_to_tiocm(sc);
    875       1.1       bsh 		break;
    876       1.1       bsh 
    877       1.1       bsh 	default:
    878       1.1       bsh 		error = EPASSTHROUGH;
    879       1.1       bsh 		break;
    880       1.1       bsh 	}
    881       1.1       bsh 
    882       1.1       bsh 	SSCOM_UNLOCK(sc);
    883       1.1       bsh 	splx(s);
    884       1.1       bsh 
    885       1.1       bsh 	if (sscom_debug)
    886       1.1       bsh 		sscomstatus(sc, "sscomioctl ");
    887       1.1       bsh 
    888       1.1       bsh 	return error;
    889       1.1       bsh }
    890       1.1       bsh 
    891       1.1       bsh integrate void
    892       1.1       bsh sscom_schedrx(struct sscom_softc *sc)
    893       1.1       bsh {
    894       1.1       bsh 
    895       1.1       bsh 	sc->sc_rx_ready = 1;
    896       1.1       bsh 
    897       1.1       bsh 	/* Wake up the poller. */
    898       1.1       bsh 	softintr_schedule(sc->sc_si);
    899       1.1       bsh }
    900       1.1       bsh 
    901       1.1       bsh static void
    902       1.1       bsh sscom_break(struct sscom_softc *sc, int onoff)
    903       1.1       bsh {
    904       1.1       bsh 
    905       1.1       bsh 	if (onoff)
    906       1.1       bsh 		SET(sc->sc_ucon, UCON_SBREAK);
    907       1.1       bsh 	else
    908       1.1       bsh 		CLR(sc->sc_ucon, UCON_SBREAK);
    909       1.1       bsh 
    910       1.1       bsh 	if (!sc->sc_heldchange) {
    911       1.1       bsh 		if (sc->sc_tx_busy) {
    912       1.1       bsh 			sc->sc_heldtbc = sc->sc_tbc;
    913       1.1       bsh 			sc->sc_tbc = 0;
    914       1.1       bsh 			sc->sc_heldchange = 1;
    915       1.1       bsh 		} else
    916       1.1       bsh 			sscom_loadchannelregs(sc);
    917       1.1       bsh 	}
    918       1.1       bsh }
    919       1.1       bsh 
    920       1.1       bsh static void
    921       1.1       bsh sscom_modem(struct sscom_softc *sc, int onoff)
    922       1.1       bsh {
    923       1.1       bsh 	if (onoff)
    924       1.1       bsh 		SET(sc->sc_umcon, UMCON_DTR);
    925       1.1       bsh 	else
    926       1.1       bsh 		CLR(sc->sc_umcon, UMCON_DTR);
    927       1.1       bsh 
    928       1.1       bsh 	if (!sc->sc_heldchange) {
    929       1.1       bsh 		if (sc->sc_tx_busy) {
    930       1.1       bsh 			sc->sc_heldtbc = sc->sc_tbc;
    931       1.1       bsh 			sc->sc_tbc = 0;
    932       1.1       bsh 			sc->sc_heldchange = 1;
    933       1.1       bsh 		} else
    934       1.1       bsh 			sscom_loadchannelregs(sc);
    935       1.1       bsh 	}
    936       1.1       bsh }
    937       1.1       bsh 
    938       1.1       bsh static void
    939       1.1       bsh tiocm_to_sscom(struct sscom_softc *sc, u_long how, int ttybits)
    940       1.1       bsh {
    941       1.1       bsh 	u_char sscombits;
    942       1.1       bsh 
    943       1.1       bsh 	sscombits = 0;
    944       1.1       bsh 	if (ISSET(ttybits, TIOCM_DTR))
    945       1.1       bsh 		sscombits = UMCON_DTR;
    946       1.1       bsh 	if (ISSET(ttybits, TIOCM_RTS))
    947       1.1       bsh 		SET(sscombits, UMCON_RTS);
    948       1.1       bsh 
    949       1.1       bsh 	switch (how) {
    950       1.1       bsh 	case TIOCMBIC:
    951       1.1       bsh 		CLR(sc->sc_umcon, sscombits);
    952       1.1       bsh 		break;
    953       1.1       bsh 
    954       1.1       bsh 	case TIOCMBIS:
    955       1.1       bsh 		SET(sc->sc_umcon, sscombits);
    956       1.1       bsh 		break;
    957       1.1       bsh 
    958       1.1       bsh 	case TIOCMSET:
    959       1.9       bsh 		CLR(sc->sc_umcon, UMCON_DTR);
    960       1.1       bsh 		SET(sc->sc_umcon, sscombits);
    961       1.1       bsh 		break;
    962       1.1       bsh 	}
    963       1.1       bsh 
    964       1.1       bsh 	if (!sc->sc_heldchange) {
    965       1.1       bsh 		if (sc->sc_tx_busy) {
    966       1.1       bsh 			sc->sc_heldtbc = sc->sc_tbc;
    967       1.1       bsh 			sc->sc_tbc = 0;
    968       1.1       bsh 			sc->sc_heldchange = 1;
    969       1.1       bsh 		} else
    970       1.1       bsh 			sscom_loadchannelregs(sc);
    971       1.1       bsh 	}
    972       1.1       bsh }
    973       1.1       bsh 
    974       1.1       bsh static int
    975       1.1       bsh sscom_to_tiocm(struct sscom_softc *sc)
    976       1.1       bsh {
    977       1.1       bsh 	u_char sscombits;
    978       1.1       bsh 	int ttybits = 0;
    979       1.1       bsh 
    980       1.1       bsh 	sscombits = sc->sc_umcon;
    981       1.1       bsh #if 0
    982       1.1       bsh 	if (ISSET(sscombits, MCR_DTR))
    983       1.1       bsh 		SET(ttybits, TIOCM_DTR);
    984       1.1       bsh #endif
    985       1.1       bsh 	if (ISSET(sscombits, UMCON_RTS))
    986       1.1       bsh 		SET(ttybits, TIOCM_RTS);
    987       1.1       bsh 
    988       1.1       bsh 	sscombits = sc->sc_msts;
    989       1.1       bsh 	if (ISSET(sscombits, MSTS_DCD))
    990       1.1       bsh 		SET(ttybits, TIOCM_CD);
    991       1.1       bsh 	if (ISSET(sscombits, MSTS_DSR))
    992       1.1       bsh 		SET(ttybits, TIOCM_DSR);
    993       1.1       bsh 	if (ISSET(sscombits, MSTS_CTS))
    994       1.1       bsh 		SET(ttybits, TIOCM_CTS);
    995       1.1       bsh 
    996       1.1       bsh 	if (sc->sc_ucon != 0)
    997       1.1       bsh 		SET(ttybits, TIOCM_LE);
    998       1.1       bsh 
    999       1.1       bsh 	return ttybits;
   1000       1.1       bsh }
   1001       1.1       bsh 
   1002       1.1       bsh static int
   1003       1.1       bsh cflag2lcr(tcflag_t cflag)
   1004       1.1       bsh {
   1005       1.1       bsh 	u_char lcr = ULCON_PARITY_NONE;
   1006       1.1       bsh 
   1007       1.3       bsh 	switch (cflag & (PARENB|PARODD)) {
   1008       1.1       bsh 	case PARENB|PARODD: lcr = ULCON_PARITY_ODD; break;
   1009       1.1       bsh 	case PARENB: lcr = ULCON_PARITY_EVEN;
   1010       1.1       bsh 	}
   1011       1.1       bsh 
   1012       1.1       bsh 	switch (ISSET(cflag, CSIZE)) {
   1013       1.1       bsh 	case CS5:
   1014       1.1       bsh 		SET(lcr, ULCON_LENGTH_5);
   1015       1.1       bsh 		break;
   1016       1.1       bsh 	case CS6:
   1017       1.1       bsh 		SET(lcr, ULCON_LENGTH_6);
   1018       1.1       bsh 		break;
   1019       1.1       bsh 	case CS7:
   1020       1.1       bsh 		SET(lcr, ULCON_LENGTH_7);
   1021       1.1       bsh 		break;
   1022       1.1       bsh 	case CS8:
   1023       1.1       bsh 		SET(lcr, ULCON_LENGTH_8);
   1024       1.1       bsh 		break;
   1025       1.1       bsh 	}
   1026       1.1       bsh 	if (ISSET(cflag, CSTOPB))
   1027       1.1       bsh 		SET(lcr, ULCON_STOP);
   1028       1.1       bsh 
   1029       1.1       bsh 	return lcr;
   1030       1.1       bsh }
   1031       1.1       bsh 
   1032       1.1       bsh int
   1033       1.1       bsh sscomparam(struct tty *tp, struct termios *t)
   1034       1.1       bsh {
   1035       1.1       bsh 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(tp->t_dev));
   1036       1.1       bsh 	int ospeed;
   1037       1.1       bsh 	u_char lcr;
   1038       1.1       bsh 	int s;
   1039       1.1       bsh 
   1040       1.1       bsh 	if (SSCOM_ISALIVE(sc) == 0)
   1041       1.1       bsh 		return EIO;
   1042       1.1       bsh 
   1043       1.1       bsh 	ospeed = sscomspeed(t->c_ospeed, sc->sc_frequency);
   1044       1.1       bsh 
   1045       1.1       bsh 	/* Check requested parameters. */
   1046       1.1       bsh 	if (ospeed < 0)
   1047       1.1       bsh 		return EINVAL;
   1048       1.1       bsh 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
   1049       1.1       bsh 		return EINVAL;
   1050       1.1       bsh 
   1051       1.1       bsh 	/*
   1052       1.1       bsh 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1053       1.1       bsh 	 * is always active.
   1054       1.1       bsh 	 */
   1055       1.1       bsh 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
   1056       1.1       bsh 	    ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE)) {
   1057       1.1       bsh 		SET(t->c_cflag, CLOCAL);
   1058       1.1       bsh 		CLR(t->c_cflag, HUPCL);
   1059       1.1       bsh 	}
   1060       1.1       bsh 
   1061       1.1       bsh 	/*
   1062       1.1       bsh 	 * If there were no changes, don't do anything.  This avoids dropping
   1063       1.1       bsh 	 * input and improves performance when all we did was frob things like
   1064       1.1       bsh 	 * VMIN and VTIME.
   1065       1.1       bsh 	 */
   1066       1.1       bsh 	if (tp->t_ospeed == t->c_ospeed &&
   1067       1.1       bsh 	    tp->t_cflag == t->c_cflag)
   1068       1.1       bsh 		return 0;
   1069       1.1       bsh 
   1070       1.1       bsh 	lcr = cflag2lcr(t->c_cflag);
   1071       1.1       bsh 
   1072       1.1       bsh 	s = splserial();
   1073       1.1       bsh 	SSCOM_LOCK(sc);
   1074       1.1       bsh 
   1075       1.1       bsh 	sc->sc_ulcon = lcr;
   1076       1.1       bsh 
   1077       1.1       bsh 	/*
   1078       1.1       bsh 	 * If we're not in a mode that assumes a connection is present, then
   1079       1.1       bsh 	 * ignore carrier changes.
   1080       1.1       bsh 	 */
   1081       1.1       bsh 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
   1082       1.1       bsh 		sc->sc_msr_dcd = 0;
   1083       1.1       bsh 	else
   1084       1.1       bsh 		sc->sc_msr_dcd = MSTS_DCD;
   1085       1.1       bsh 
   1086       1.1       bsh 	/*
   1087       1.1       bsh 	 * Set the flow control pins depending on the current flow control
   1088       1.1       bsh 	 * mode.
   1089       1.1       bsh 	 */
   1090       1.1       bsh 	if (ISSET(t->c_cflag, CRTSCTS)) {
   1091       1.1       bsh 		sc->sc_mcr_dtr = UMCON_DTR;
   1092       1.1       bsh 		sc->sc_mcr_rts = UMCON_RTS;
   1093       1.1       bsh 		sc->sc_msr_cts = MSTS_CTS;
   1094       1.1       bsh 	}
   1095       1.1       bsh 	else if (ISSET(t->c_cflag, MDMBUF)) {
   1096       1.1       bsh 		/*
   1097       1.1       bsh 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
   1098       1.1       bsh 		 * carrier detection.
   1099       1.1       bsh 		 */
   1100       1.1       bsh 		sc->sc_mcr_dtr = 0;
   1101       1.1       bsh 		sc->sc_mcr_rts = UMCON_DTR;
   1102       1.1       bsh 		sc->sc_msr_cts = MSTS_DCD;
   1103       1.1       bsh 	}
   1104       1.1       bsh 	else {
   1105       1.1       bsh 		/*
   1106       1.1       bsh 		 * If no flow control, then always set RTS.  This will make
   1107       1.1       bsh 		 * the other side happy if it mistakenly thinks we're doing
   1108       1.1       bsh 		 * RTS/CTS flow control.
   1109       1.1       bsh 		 */
   1110       1.1       bsh 		sc->sc_mcr_dtr = UMCON_DTR | UMCON_RTS;
   1111       1.1       bsh 		sc->sc_mcr_rts = 0;
   1112       1.1       bsh 		sc->sc_msr_cts = 0;
   1113       1.1       bsh 		if (ISSET(sc->sc_umcon, UMCON_DTR))
   1114       1.1       bsh 			SET(sc->sc_umcon, UMCON_RTS);
   1115       1.1       bsh 		else
   1116       1.1       bsh 			CLR(sc->sc_umcon, UMCON_RTS);
   1117       1.1       bsh 	}
   1118       1.1       bsh 	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
   1119       1.1       bsh 
   1120       1.1       bsh 	if (ospeed == 0)
   1121       1.1       bsh 		CLR(sc->sc_umcon, sc->sc_mcr_dtr);
   1122       1.1       bsh 	else
   1123       1.1       bsh 		SET(sc->sc_umcon, sc->sc_mcr_dtr);
   1124       1.1       bsh 
   1125       1.1       bsh 	sc->sc_ubrdiv = ospeed;
   1126       1.1       bsh 
   1127       1.1       bsh 	/* And copy to tty. */
   1128       1.1       bsh 	tp->t_ispeed = 0;
   1129       1.1       bsh 	tp->t_ospeed = t->c_ospeed;
   1130       1.1       bsh 	tp->t_cflag = t->c_cflag;
   1131       1.1       bsh 
   1132       1.1       bsh 	if (!sc->sc_heldchange) {
   1133       1.1       bsh 		if (sc->sc_tx_busy) {
   1134       1.1       bsh 			sc->sc_heldtbc = sc->sc_tbc;
   1135       1.1       bsh 			sc->sc_tbc = 0;
   1136       1.1       bsh 			sc->sc_heldchange = 1;
   1137       1.1       bsh 		} else
   1138       1.1       bsh 			sscom_loadchannelregs(sc);
   1139       1.1       bsh 	}
   1140       1.1       bsh 
   1141       1.1       bsh 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1142       1.1       bsh 		/* Disable the high water mark. */
   1143       1.1       bsh 		sc->sc_r_hiwat = 0;
   1144       1.1       bsh 		sc->sc_r_lowat = 0;
   1145       1.1       bsh 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1146       1.1       bsh 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1147       1.1       bsh 			sscom_schedrx(sc);
   1148       1.1       bsh 		}
   1149       1.1       bsh 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
   1150       1.1       bsh 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1151       1.1       bsh 			sscom_hwiflow(sc);
   1152       1.1       bsh 		}
   1153       1.1       bsh 	} else {
   1154       1.1       bsh 		sc->sc_r_hiwat = sscom_rbuf_hiwat;
   1155       1.1       bsh 		sc->sc_r_lowat = sscom_rbuf_lowat;
   1156       1.1       bsh 	}
   1157       1.1       bsh 
   1158       1.1       bsh 	SSCOM_UNLOCK(sc);
   1159       1.1       bsh 	splx(s);
   1160       1.1       bsh 
   1161       1.1       bsh 	/*
   1162       1.1       bsh 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1163       1.1       bsh 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1164       1.1       bsh 	 * explicit request.
   1165       1.1       bsh 	 */
   1166       1.1       bsh 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msts, MSTS_DCD));
   1167       1.1       bsh 
   1168       1.1       bsh 	if (sscom_debug)
   1169       1.1       bsh 		sscomstatus(sc, "sscomparam ");
   1170       1.1       bsh 
   1171       1.1       bsh 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1172       1.1       bsh 		if (sc->sc_tx_stopped) {
   1173       1.1       bsh 			sc->sc_tx_stopped = 0;
   1174       1.1       bsh 			sscomstart(tp);
   1175       1.1       bsh 		}
   1176       1.1       bsh 	}
   1177       1.1       bsh 
   1178       1.1       bsh 	return 0;
   1179       1.1       bsh }
   1180       1.1       bsh 
   1181       1.1       bsh static void
   1182       1.1       bsh sscom_iflush(struct sscom_softc *sc)
   1183       1.1       bsh {
   1184       1.1       bsh 	bus_space_tag_t iot = sc->sc_iot;
   1185       1.1       bsh 	bus_space_handle_t ioh = sc->sc_ioh;
   1186       1.1       bsh 	int timo;
   1187       1.1       bsh 
   1188       1.1       bsh 
   1189       1.1       bsh 	timo = 50000;
   1190       1.1       bsh 	/* flush any pending I/O */
   1191       1.1       bsh 	while ( sscom_rxrdy(iot, ioh) && --timo)
   1192       1.1       bsh 		(void)sscom_getc(iot,ioh);
   1193       1.1       bsh #ifdef DIAGNOSTIC
   1194       1.1       bsh 	if (!timo)
   1195       1.1       bsh 		printf("%s: sscom_iflush timeout\n", sc->sc_dev.dv_xname);
   1196       1.1       bsh #endif
   1197       1.1       bsh }
   1198       1.1       bsh 
   1199       1.1       bsh static void
   1200       1.1       bsh sscom_loadchannelregs(struct sscom_softc *sc)
   1201       1.1       bsh {
   1202       1.1       bsh 	bus_space_tag_t iot = sc->sc_iot;
   1203       1.1       bsh 	bus_space_handle_t ioh = sc->sc_ioh;
   1204       1.1       bsh 
   1205       1.1       bsh 	/* XXXXX necessary? */
   1206       1.1       bsh 	sscom_iflush(sc);
   1207       1.1       bsh 
   1208       1.1       bsh 	bus_space_write_2(iot, ioh, SSCOM_UCON, 0);
   1209       1.1       bsh 
   1210       1.1       bsh #if 0
   1211       1.1       bsh 	if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
   1212       1.1       bsh 		bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
   1213       1.1       bsh 		bus_space_write_1(iot, ioh, com_efr, sc->sc_efr);
   1214       1.1       bsh 	}
   1215       1.1       bsh #endif
   1216       1.1       bsh 
   1217       1.1       bsh 	bus_space_write_2(iot, ioh, SSCOM_UBRDIV, sc->sc_ubrdiv);
   1218       1.1       bsh 	bus_space_write_1(iot, ioh, SSCOM_ULCON, sc->sc_ulcon);
   1219       1.1       bsh 	sc->set_modem_control(sc);
   1220       1.1       bsh 	bus_space_write_2(iot, ioh, SSCOM_UCON, sc->sc_ucon);
   1221       1.1       bsh }
   1222       1.1       bsh 
   1223       1.1       bsh static int
   1224       1.1       bsh sscomhwiflow(struct tty *tp, int block)
   1225       1.1       bsh {
   1226       1.1       bsh 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(tp->t_dev));
   1227       1.1       bsh 	int s;
   1228       1.1       bsh 
   1229       1.1       bsh 	if (SSCOM_ISALIVE(sc) == 0)
   1230       1.1       bsh 		return 0;
   1231       1.1       bsh 
   1232       1.1       bsh 	if (sc->sc_mcr_rts == 0)
   1233       1.1       bsh 		return 0;
   1234       1.1       bsh 
   1235       1.1       bsh 	s = splserial();
   1236       1.1       bsh 	SSCOM_LOCK(sc);
   1237       1.1       bsh 
   1238       1.1       bsh 	if (block) {
   1239       1.1       bsh 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1240       1.1       bsh 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1241       1.1       bsh 			sscom_hwiflow(sc);
   1242       1.1       bsh 		}
   1243       1.1       bsh 	} else {
   1244       1.1       bsh 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
   1245       1.1       bsh 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1246       1.1       bsh 			sscom_schedrx(sc);
   1247       1.1       bsh 		}
   1248       1.1       bsh 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1249       1.1       bsh 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
   1250       1.1       bsh 			sscom_hwiflow(sc);
   1251       1.1       bsh 		}
   1252       1.1       bsh 	}
   1253       1.1       bsh 
   1254       1.1       bsh 	SSCOM_UNLOCK(sc);
   1255       1.1       bsh 	splx(s);
   1256       1.1       bsh 	return 1;
   1257       1.1       bsh }
   1258       1.1       bsh 
   1259       1.1       bsh /*
   1260       1.1       bsh  * (un)block input via hw flowcontrol
   1261       1.1       bsh  */
   1262       1.1       bsh static void
   1263       1.1       bsh sscom_hwiflow(struct sscom_softc *sc)
   1264       1.1       bsh {
   1265       1.1       bsh 	if (sc->sc_mcr_rts == 0)
   1266       1.1       bsh 		return;
   1267       1.1       bsh 
   1268       1.1       bsh 	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
   1269       1.1       bsh 		CLR(sc->sc_umcon, sc->sc_mcr_rts);
   1270       1.1       bsh 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
   1271       1.1       bsh 	} else {
   1272       1.1       bsh 		SET(sc->sc_umcon, sc->sc_mcr_rts);
   1273       1.1       bsh 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
   1274       1.1       bsh 	}
   1275       1.1       bsh 	sc->set_modem_control(sc);
   1276       1.1       bsh }
   1277       1.1       bsh 
   1278       1.1       bsh 
   1279       1.1       bsh void
   1280       1.1       bsh sscomstart(struct tty *tp)
   1281       1.1       bsh {
   1282       1.1       bsh 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(tp->t_dev));
   1283       1.1       bsh 	int s;
   1284       1.1       bsh 
   1285       1.1       bsh 	if (SSCOM_ISALIVE(sc) == 0)
   1286       1.1       bsh 		return;
   1287       1.1       bsh 
   1288       1.1       bsh 	s = spltty();
   1289       1.1       bsh 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1290       1.1       bsh 		goto out;
   1291       1.1       bsh 	if (sc->sc_tx_stopped)
   1292       1.1       bsh 		goto out;
   1293       1.1       bsh 
   1294       1.1       bsh 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1295       1.1       bsh 		if (ISSET(tp->t_state, TS_ASLEEP)) {
   1296       1.1       bsh 			CLR(tp->t_state, TS_ASLEEP);
   1297       1.1       bsh 			wakeup(&tp->t_outq);
   1298       1.1       bsh 		}
   1299       1.1       bsh 		selwakeup(&tp->t_wsel);
   1300       1.1       bsh 		if (tp->t_outq.c_cc == 0)
   1301       1.1       bsh 			goto out;
   1302       1.1       bsh 	}
   1303       1.1       bsh 
   1304       1.1       bsh 	/* Grab the first contiguous region of buffer space. */
   1305       1.1       bsh 	{
   1306       1.1       bsh 		u_char *tba;
   1307       1.1       bsh 		int tbc;
   1308       1.1       bsh 
   1309       1.1       bsh 		tba = tp->t_outq.c_cf;
   1310       1.1       bsh 		tbc = ndqb(&tp->t_outq, 0);
   1311       1.1       bsh 
   1312       1.1       bsh 		(void)splserial();
   1313       1.1       bsh 		SSCOM_LOCK(sc);
   1314       1.1       bsh 
   1315       1.1       bsh 		sc->sc_tba = tba;
   1316       1.1       bsh 		sc->sc_tbc = tbc;
   1317       1.1       bsh 	}
   1318       1.1       bsh 
   1319       1.1       bsh 	SET(tp->t_state, TS_BUSY);
   1320       1.1       bsh 	sc->sc_tx_busy = 1;
   1321       1.1       bsh 
   1322       1.1       bsh 	/* Output the first chunk of the contiguous buffer. */
   1323       1.1       bsh 	sscom_output_chunk(sc);
   1324       1.1       bsh 
   1325       1.1       bsh 	/* Enable transmit completion interrupts if necessary. */
   1326       1.3       bsh 	if ((sc->sc_hwflags & SSCOM_HW_TXINT) == 0)
   1327       1.1       bsh 		sscom_enable_txint(sc);
   1328       1.1       bsh 
   1329       1.1       bsh 	SSCOM_UNLOCK(sc);
   1330       1.1       bsh out:
   1331       1.1       bsh 	splx(s);
   1332       1.1       bsh 	return;
   1333       1.1       bsh }
   1334       1.1       bsh 
   1335       1.1       bsh /*
   1336       1.1       bsh  * Stop output on a line.
   1337       1.1       bsh  */
   1338       1.1       bsh void
   1339       1.1       bsh sscomstop(struct tty *tp, int flag)
   1340       1.1       bsh {
   1341       1.1       bsh 	struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(tp->t_dev));
   1342       1.1       bsh 	int s;
   1343       1.1       bsh 
   1344       1.1       bsh 	s = splserial();
   1345       1.1       bsh 	SSCOM_LOCK(sc);
   1346       1.1       bsh 	if (ISSET(tp->t_state, TS_BUSY)) {
   1347       1.1       bsh 		/* Stop transmitting at the next chunk. */
   1348       1.1       bsh 		sc->sc_tbc = 0;
   1349       1.1       bsh 		sc->sc_heldtbc = 0;
   1350       1.1       bsh 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1351       1.1       bsh 			SET(tp->t_state, TS_FLUSH);
   1352       1.1       bsh 	}
   1353       1.1       bsh 	SSCOM_UNLOCK(sc);
   1354       1.1       bsh 	splx(s);
   1355       1.1       bsh }
   1356       1.1       bsh 
   1357       1.1       bsh void
   1358       1.1       bsh sscomdiag(void *arg)
   1359       1.1       bsh {
   1360       1.1       bsh 	struct sscom_softc *sc = arg;
   1361       1.1       bsh 	int overflows, floods;
   1362       1.1       bsh 	int s;
   1363       1.1       bsh 
   1364       1.1       bsh 	s = splserial();
   1365       1.1       bsh 	SSCOM_LOCK(sc);
   1366       1.1       bsh 	overflows = sc->sc_overflows;
   1367       1.1       bsh 	sc->sc_overflows = 0;
   1368       1.1       bsh 	floods = sc->sc_floods;
   1369       1.1       bsh 	sc->sc_floods = 0;
   1370       1.1       bsh 	sc->sc_errors = 0;
   1371       1.1       bsh 	SSCOM_UNLOCK(sc);
   1372       1.1       bsh 	splx(s);
   1373       1.1       bsh 
   1374       1.1       bsh 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1375       1.1       bsh 	    sc->sc_dev.dv_xname,
   1376       1.1       bsh 	    overflows, overflows == 1 ? "" : "s",
   1377       1.1       bsh 	    floods, floods == 1 ? "" : "s");
   1378       1.1       bsh }
   1379       1.1       bsh 
   1380       1.1       bsh integrate void
   1381       1.1       bsh sscom_rxsoft(struct sscom_softc *sc, struct tty *tp)
   1382       1.1       bsh {
   1383       1.1       bsh 	int (*rint) (int, struct tty *) = tp->t_linesw->l_rint;
   1384       1.1       bsh 	u_char *get, *end;
   1385       1.1       bsh 	u_int cc, scc;
   1386       1.1       bsh 	u_char rsr;
   1387       1.1       bsh 	int code;
   1388       1.1       bsh 	int s;
   1389       1.1       bsh 
   1390       1.1       bsh 	end = sc->sc_ebuf;
   1391       1.1       bsh 	get = sc->sc_rbget;
   1392       1.1       bsh 	scc = cc = sscom_rbuf_size - sc->sc_rbavail;
   1393       1.1       bsh 
   1394       1.1       bsh 	if (cc == sscom_rbuf_size) {
   1395       1.1       bsh 		sc->sc_floods++;
   1396       1.1       bsh 		if (sc->sc_errors++ == 0)
   1397       1.1       bsh 			callout_reset(&sc->sc_diag_callout, 60 * hz,
   1398       1.1       bsh 			    sscomdiag, sc);
   1399       1.1       bsh 	}
   1400       1.1       bsh 
   1401       1.1       bsh 	while (cc) {
   1402       1.1       bsh 		code = get[0];
   1403       1.1       bsh 		rsr = get[1];
   1404       1.3       bsh 		if (rsr) {
   1405       1.1       bsh 			if (ISSET(rsr, UERSTAT_OVERRUN)) {
   1406       1.1       bsh 				sc->sc_overflows++;
   1407       1.1       bsh 				if (sc->sc_errors++ == 0)
   1408       1.1       bsh 					callout_reset(&sc->sc_diag_callout,
   1409       1.1       bsh 					    60 * hz, sscomdiag, sc);
   1410       1.1       bsh 			}
   1411       1.1       bsh 			if (ISSET(rsr, UERSTAT_BREAK | UERSTAT_FRAME))
   1412       1.1       bsh 				SET(code, TTY_FE);
   1413       1.1       bsh 			if (ISSET(rsr, UERSTAT_PARITY))
   1414       1.1       bsh 				SET(code, TTY_PE);
   1415       1.1       bsh 		}
   1416       1.1       bsh 		if ((*rint)(code, tp) == -1) {
   1417       1.1       bsh 			/*
   1418       1.1       bsh 			 * The line discipline's buffer is out of space.
   1419       1.1       bsh 			 */
   1420       1.1       bsh 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
   1421       1.1       bsh 				/*
   1422       1.1       bsh 				 * We're either not using flow control, or the
   1423       1.1       bsh 				 * line discipline didn't tell us to block for
   1424       1.1       bsh 				 * some reason.  Either way, we have no way to
   1425       1.1       bsh 				 * know when there's more space available, so
   1426       1.1       bsh 				 * just drop the rest of the data.
   1427       1.1       bsh 				 */
   1428       1.1       bsh 				get += cc << 1;
   1429       1.1       bsh 				if (get >= end)
   1430       1.1       bsh 					get -= sscom_rbuf_size << 1;
   1431       1.1       bsh 				cc = 0;
   1432       1.1       bsh 			} else {
   1433       1.1       bsh 				/*
   1434       1.1       bsh 				 * Don't schedule any more receive processing
   1435       1.1       bsh 				 * until the line discipline tells us there's
   1436       1.1       bsh 				 * space available (through sscomhwiflow()).
   1437       1.1       bsh 				 * Leave the rest of the data in the input
   1438       1.1       bsh 				 * buffer.
   1439       1.1       bsh 				 */
   1440       1.1       bsh 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1441       1.1       bsh 			}
   1442       1.1       bsh 			break;
   1443       1.1       bsh 		}
   1444       1.1       bsh 		get += 2;
   1445       1.1       bsh 		if (get >= end)
   1446       1.1       bsh 			get = sc->sc_rbuf;
   1447       1.1       bsh 		cc--;
   1448       1.1       bsh 	}
   1449       1.1       bsh 
   1450       1.1       bsh 	if (cc != scc) {
   1451       1.1       bsh 		sc->sc_rbget = get;
   1452       1.1       bsh 		s = splserial();
   1453       1.1       bsh 		SSCOM_LOCK(sc);
   1454       1.1       bsh 
   1455       1.1       bsh 		cc = sc->sc_rbavail += scc - cc;
   1456       1.1       bsh 		/* Buffers should be ok again, release possible block. */
   1457       1.1       bsh 		if (cc >= sc->sc_r_lowat) {
   1458       1.1       bsh 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1459       1.1       bsh 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1460       1.1       bsh 				sscom_enable_rxint(sc);
   1461       1.4       bsh 				sc->sc_ucon |= UCON_ERRINT;
   1462       1.4       bsh 				bus_space_write_2(sc->sc_iot, sc->sc_ioh, SSCOM_UCON,
   1463       1.4       bsh 						  sc->sc_ucon);
   1464       1.4       bsh 
   1465       1.1       bsh 			}
   1466       1.1       bsh 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1467       1.1       bsh 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1468       1.1       bsh 				sscom_hwiflow(sc);
   1469       1.1       bsh 			}
   1470       1.1       bsh 		}
   1471       1.1       bsh 		SSCOM_UNLOCK(sc);
   1472       1.1       bsh 		splx(s);
   1473       1.1       bsh 	}
   1474       1.1       bsh }
   1475       1.1       bsh 
   1476       1.1       bsh integrate void
   1477       1.1       bsh sscom_txsoft(struct sscom_softc *sc, struct tty *tp)
   1478       1.1       bsh {
   1479       1.1       bsh 
   1480       1.1       bsh 	CLR(tp->t_state, TS_BUSY);
   1481       1.1       bsh 	if (ISSET(tp->t_state, TS_FLUSH))
   1482       1.1       bsh 		CLR(tp->t_state, TS_FLUSH);
   1483       1.1       bsh 	else
   1484       1.1       bsh 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1485       1.1       bsh 	(*tp->t_linesw->l_start)(tp);
   1486       1.1       bsh }
   1487       1.1       bsh 
   1488       1.1       bsh integrate void
   1489       1.1       bsh sscom_stsoft(struct sscom_softc *sc, struct tty *tp)
   1490       1.1       bsh {
   1491       1.1       bsh 	u_char msr, delta;
   1492       1.1       bsh 	int s;
   1493       1.1       bsh 
   1494       1.1       bsh 	s = splserial();
   1495       1.1       bsh 	SSCOM_LOCK(sc);
   1496       1.1       bsh 	msr = sc->sc_msts;
   1497       1.1       bsh 	delta = sc->sc_msr_delta;
   1498       1.1       bsh 	sc->sc_msr_delta = 0;
   1499       1.1       bsh 	SSCOM_UNLOCK(sc);
   1500       1.1       bsh 	splx(s);
   1501       1.1       bsh 
   1502       1.1       bsh 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1503       1.1       bsh 		/*
   1504       1.1       bsh 		 * Inform the tty layer that carrier detect changed.
   1505       1.1       bsh 		 */
   1506       1.1       bsh 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSTS_DCD));
   1507       1.1       bsh 	}
   1508       1.1       bsh 
   1509       1.1       bsh 	if (ISSET(delta, sc->sc_msr_cts)) {
   1510       1.1       bsh 		/* Block or unblock output according to flow control. */
   1511       1.1       bsh 		if (ISSET(msr, sc->sc_msr_cts)) {
   1512       1.1       bsh 			sc->sc_tx_stopped = 0;
   1513       1.1       bsh 			(*tp->t_linesw->l_start)(tp);
   1514       1.1       bsh 		} else {
   1515       1.1       bsh 			sc->sc_tx_stopped = 1;
   1516       1.1       bsh 		}
   1517       1.1       bsh 	}
   1518       1.1       bsh 
   1519       1.1       bsh 	if (sscom_debug)
   1520       1.1       bsh 		sscomstatus(sc, "sscom_stsoft");
   1521       1.1       bsh }
   1522       1.1       bsh 
   1523       1.1       bsh #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1524       1.1       bsh void
   1525       1.1       bsh sscomsoft(void *arg)
   1526       1.1       bsh {
   1527       1.1       bsh 	struct sscom_softc *sc = arg;
   1528       1.1       bsh 	struct tty *tp;
   1529       1.1       bsh 
   1530       1.1       bsh 	if (SSCOM_ISALIVE(sc) == 0)
   1531       1.1       bsh 		return;
   1532       1.1       bsh 
   1533       1.1       bsh 	{
   1534       1.1       bsh 		tp = sc->sc_tty;
   1535       1.1       bsh 
   1536       1.1       bsh 		if (sc->sc_rx_ready) {
   1537       1.1       bsh 			sc->sc_rx_ready = 0;
   1538       1.1       bsh 			sscom_rxsoft(sc, tp);
   1539       1.1       bsh 		}
   1540       1.1       bsh 
   1541       1.1       bsh 		if (sc->sc_st_check) {
   1542       1.1       bsh 			sc->sc_st_check = 0;
   1543       1.1       bsh 			sscom_stsoft(sc, tp);
   1544       1.1       bsh 		}
   1545       1.1       bsh 
   1546       1.1       bsh 		if (sc->sc_tx_done) {
   1547       1.1       bsh 			sc->sc_tx_done = 0;
   1548       1.1       bsh 			sscom_txsoft(sc, tp);
   1549       1.1       bsh 		}
   1550       1.1       bsh 	}
   1551       1.1       bsh }
   1552       1.1       bsh #else
   1553       1.1       bsh #error sscom needs GENERIC_SOFT_INERRUPTS
   1554       1.1       bsh #endif
   1555       1.1       bsh 
   1556       1.1       bsh 
   1557       1.1       bsh int
   1558       1.9       bsh sscomrxintr(void *arg)
   1559       1.1       bsh {
   1560       1.1       bsh 	struct sscom_softc *sc = arg;
   1561       1.1       bsh 	bus_space_tag_t iot = sc->sc_iot;
   1562       1.1       bsh 	bus_space_handle_t ioh = sc->sc_ioh;
   1563       1.1       bsh 	u_char *put, *end;
   1564       1.1       bsh 	u_int cc;
   1565       1.1       bsh 
   1566       1.1       bsh 	if (SSCOM_ISALIVE(sc) == 0)
   1567       1.1       bsh 		return 0;
   1568       1.1       bsh 
   1569       1.1       bsh 	SSCOM_LOCK(sc);
   1570       1.1       bsh 
   1571       1.1       bsh 	end = sc->sc_ebuf;
   1572       1.1       bsh 	put = sc->sc_rbput;
   1573       1.1       bsh 	cc = sc->sc_rbavail;
   1574       1.1       bsh 
   1575       1.1       bsh 	do {
   1576       1.1       bsh 		u_char	msts, delta;
   1577       1.1       bsh 		u_char  uerstat;
   1578       1.1       bsh 		uint16_t ufstat;
   1579       1.1       bsh 
   1580       1.1       bsh 		ufstat = bus_space_read_2(iot, ioh, SSCOM_UFSTAT);
   1581       1.1       bsh 
   1582       1.1       bsh 		/* XXX: break interrupt with no character? */
   1583       1.1       bsh 
   1584       1.4       bsh 		if ( (ufstat & (UFSTAT_RXCOUNT|UFSTAT_RXFULL)) &&
   1585       1.1       bsh 		    !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1586       1.9       bsh 
   1587       1.1       bsh 			while (cc > 0) {
   1588       1.1       bsh 				int cn_trapped = 0;
   1589       1.1       bsh 
   1590       1.1       bsh 				/* get status and received character.
   1591       1.1       bsh 				   read status register first */
   1592       1.1       bsh 				uerstat = sscom_geterr(iot, ioh);
   1593       1.1       bsh 				put[0] = sscom_getc(iot, ioh);
   1594       1.1       bsh 
   1595       1.1       bsh 				if (ISSET(uerstat, UERSTAT_BREAK)) {
   1596      1.12        he 					int con_trapped = 0;
   1597       1.1       bsh 					cn_check_magic(sc->sc_tty->t_dev,
   1598       1.1       bsh 					    CNC_BREAK, sscom_cnm_state);
   1599      1.12        he 					if (con_trapped)
   1600       1.1       bsh 						continue;
   1601       1.1       bsh #if defined(KGDB)
   1602       1.1       bsh 					if (ISSET(sc->sc_hwflags,
   1603       1.1       bsh 					    SSCOM_HW_KGDB)) {
   1604       1.1       bsh 						kgdb_connect(1);
   1605       1.1       bsh 						continue;
   1606       1.1       bsh 					}
   1607       1.1       bsh #endif
   1608       1.1       bsh 				}
   1609       1.1       bsh 
   1610       1.1       bsh 				put[1] = uerstat;
   1611       1.1       bsh 				cn_check_magic(sc->sc_tty->t_dev,
   1612       1.1       bsh 					       put[0], sscom_cnm_state);
   1613       1.3       bsh 				if (!cn_trapped) {
   1614       1.1       bsh 					put += 2;
   1615       1.1       bsh 					if (put >= end)
   1616       1.1       bsh 						put = sc->sc_rbuf;
   1617       1.1       bsh 					cc--;
   1618       1.1       bsh 				}
   1619       1.1       bsh 
   1620       1.1       bsh 				ufstat = bus_space_read_2(iot, ioh, SSCOM_UFSTAT);
   1621       1.4       bsh 				if ( (ufstat & (UFSTAT_RXFULL|UFSTAT_RXCOUNT)) == 0 )
   1622       1.1       bsh 					break;
   1623       1.1       bsh 			}
   1624       1.1       bsh 
   1625       1.1       bsh 			/*
   1626       1.1       bsh 			 * Current string of incoming characters ended because
   1627       1.1       bsh 			 * no more data was available or we ran out of space.
   1628       1.1       bsh 			 * Schedule a receive event if any data was received.
   1629       1.1       bsh 			 * If we're out of space, turn off receive interrupts.
   1630       1.1       bsh 			 */
   1631       1.1       bsh 			sc->sc_rbput = put;
   1632       1.1       bsh 			sc->sc_rbavail = cc;
   1633       1.1       bsh 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1634       1.1       bsh 				sc->sc_rx_ready = 1;
   1635       1.1       bsh 
   1636       1.1       bsh 			/*
   1637       1.1       bsh 			 * See if we are in danger of overflowing a buffer. If
   1638       1.1       bsh 			 * so, use hardware flow control to ease the pressure.
   1639       1.1       bsh 			 */
   1640       1.1       bsh 			if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
   1641       1.1       bsh 			    cc < sc->sc_r_hiwat) {
   1642       1.1       bsh 				SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1643       1.1       bsh 				sscom_hwiflow(sc);
   1644       1.1       bsh 			}
   1645       1.1       bsh 
   1646       1.1       bsh 			/*
   1647       1.1       bsh 			 * If we're out of space, disable receive interrupts
   1648       1.1       bsh 			 * until the queue has drained a bit.
   1649       1.1       bsh 			 */
   1650       1.1       bsh 			if (!cc) {
   1651       1.1       bsh 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1652       1.1       bsh 				sscom_disable_rxint(sc);
   1653       1.4       bsh 				sc->sc_ucon &= ~UCON_ERRINT;
   1654       1.4       bsh 				bus_space_write_2(iot, ioh, SSCOM_UCON, sc->sc_ucon);
   1655       1.1       bsh 			}
   1656       1.1       bsh 		}
   1657       1.1       bsh 
   1658       1.9       bsh 
   1659       1.1       bsh 		msts = sc->read_modem_status(sc);
   1660       1.1       bsh 		delta = msts ^ sc->sc_msts;
   1661       1.1       bsh 		sc->sc_msts = msts;
   1662       1.1       bsh 
   1663       1.9       bsh #ifdef notyet
   1664       1.1       bsh 		/*
   1665       1.1       bsh 		 * Pulse-per-second (PSS) signals on edge of DCD?
   1666       1.1       bsh 		 * Process these even if line discipline is ignoring DCD.
   1667       1.1       bsh 		 */
   1668       1.1       bsh 		if (delta & sc->sc_ppsmask) {
   1669       1.1       bsh 			struct timeval tv;
   1670       1.1       bsh 		    	if ((msr & sc->sc_ppsmask) == sc->sc_ppsassert) {
   1671       1.1       bsh 				/* XXX nanotime() */
   1672       1.1       bsh 				microtime(&tv);
   1673       1.1       bsh 				TIMEVAL_TO_TIMESPEC(&tv,
   1674       1.1       bsh 				    &sc->ppsinfo.assert_timestamp);
   1675       1.1       bsh 				if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
   1676       1.1       bsh 					timespecadd(&sc->ppsinfo.assert_timestamp,
   1677       1.1       bsh 					    &sc->ppsparam.assert_offset,
   1678       1.1       bsh 						    &sc->ppsinfo.assert_timestamp);
   1679       1.1       bsh 				}
   1680       1.1       bsh 
   1681       1.1       bsh #ifdef PPS_SYNC
   1682       1.1       bsh 				if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
   1683       1.1       bsh 					hardpps(&tv, tv.tv_usec);
   1684       1.1       bsh #endif
   1685       1.1       bsh 				sc->ppsinfo.assert_sequence++;
   1686       1.1       bsh 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   1687       1.1       bsh 
   1688       1.1       bsh 			} else if ((msr & sc->sc_ppsmask) == sc->sc_ppsclear) {
   1689       1.1       bsh 				/* XXX nanotime() */
   1690       1.1       bsh 				microtime(&tv);
   1691       1.1       bsh 				TIMEVAL_TO_TIMESPEC(&tv,
   1692       1.1       bsh 				    &sc->ppsinfo.clear_timestamp);
   1693       1.1       bsh 				if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
   1694       1.1       bsh 					timespecadd(&sc->ppsinfo.clear_timestamp,
   1695       1.1       bsh 					    &sc->ppsparam.clear_offset,
   1696       1.1       bsh 					    &sc->ppsinfo.clear_timestamp);
   1697       1.1       bsh 				}
   1698       1.1       bsh 
   1699       1.1       bsh #ifdef PPS_SYNC
   1700       1.1       bsh 				if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
   1701       1.1       bsh 					hardpps(&tv, tv.tv_usec);
   1702       1.1       bsh #endif
   1703       1.1       bsh 				sc->ppsinfo.clear_sequence++;
   1704       1.1       bsh 				sc->ppsinfo.current_mode = sc->ppsparam.mode;
   1705       1.1       bsh 			}
   1706       1.1       bsh 		}
   1707       1.1       bsh #endif
   1708       1.1       bsh 
   1709       1.1       bsh 		/*
   1710       1.1       bsh 		 * Process normal status changes
   1711       1.1       bsh 		 */
   1712       1.1       bsh 		if (ISSET(delta, sc->sc_msr_mask)) {
   1713       1.1       bsh 			SET(sc->sc_msr_delta, delta);
   1714       1.1       bsh 
   1715       1.1       bsh 			/*
   1716       1.1       bsh 			 * Stop output immediately if we lose the output
   1717       1.1       bsh 			 * flow control signal or carrier detect.
   1718       1.1       bsh 			 */
   1719       1.1       bsh 			if (ISSET(~msts, sc->sc_msr_mask)) {
   1720       1.1       bsh 				sc->sc_tbc = 0;
   1721       1.1       bsh 				sc->sc_heldtbc = 0;
   1722       1.1       bsh #ifdef SSCOM_DEBUG
   1723       1.1       bsh 				if (sscom_debug)
   1724       1.1       bsh 					sscomstatus(sc, "sscomintr  ");
   1725       1.1       bsh #endif
   1726       1.1       bsh 			}
   1727       1.1       bsh 
   1728       1.1       bsh 			sc->sc_st_check = 1;
   1729       1.1       bsh 		}
   1730       1.1       bsh 
   1731       1.1       bsh 		/*
   1732       1.1       bsh 		 * Done handling any receive interrupts.
   1733       1.1       bsh 		 */
   1734       1.1       bsh 
   1735       1.1       bsh 		/*
   1736       1.1       bsh 		 * If we've delayed a parameter change, do it
   1737       1.1       bsh 		 * now, and restart * output.
   1738       1.1       bsh 		 */
   1739       1.3       bsh 		if ((ufstat & UFSTAT_TXCOUNT) == 0) {
   1740       1.1       bsh 			/* XXX: we should check transmitter empty also */
   1741       1.1       bsh 
   1742       1.1       bsh 			if (sc->sc_heldchange) {
   1743       1.1       bsh 				sscom_loadchannelregs(sc);
   1744       1.1       bsh 				sc->sc_heldchange = 0;
   1745       1.1       bsh 				sc->sc_tbc = sc->sc_heldtbc;
   1746       1.1       bsh 				sc->sc_heldtbc = 0;
   1747       1.1       bsh 			}
   1748       1.1       bsh 		}
   1749       1.1       bsh 
   1750       1.1       bsh 
   1751       1.9       bsh 	} while (0);
   1752       1.9       bsh 
   1753       1.9       bsh 	SSCOM_UNLOCK(sc);
   1754       1.9       bsh 
   1755       1.9       bsh 	/* Wake up the poller. */
   1756       1.9       bsh 	softintr_schedule(sc->sc_si);
   1757       1.9       bsh 
   1758       1.9       bsh #if NRND > 0 && defined(RND_COM)
   1759       1.9       bsh 	rnd_add_uint32(&sc->rnd_source, iir | rsr);
   1760       1.9       bsh #endif
   1761       1.9       bsh 
   1762       1.9       bsh 	return 1;
   1763       1.9       bsh }
   1764       1.9       bsh 
   1765       1.9       bsh int
   1766       1.9       bsh sscomtxintr(void *arg)
   1767       1.9       bsh {
   1768       1.9       bsh 	struct sscom_softc *sc = arg;
   1769       1.9       bsh 	bus_space_tag_t iot = sc->sc_iot;
   1770       1.9       bsh 	bus_space_handle_t ioh = sc->sc_ioh;
   1771       1.9       bsh 	uint16_t ufstat;
   1772       1.9       bsh 
   1773       1.9       bsh 	if (SSCOM_ISALIVE(sc) == 0)
   1774       1.9       bsh 		return 0;
   1775       1.9       bsh 
   1776       1.9       bsh 	SSCOM_LOCK(sc);
   1777       1.9       bsh 
   1778       1.9       bsh 	ufstat = bus_space_read_2(iot, ioh, SSCOM_UFSTAT);
   1779       1.9       bsh 
   1780       1.9       bsh 	/*
   1781       1.9       bsh 	 * If we've delayed a parameter change, do it
   1782       1.9       bsh 	 * now, and restart * output.
   1783       1.9       bsh 	 */
   1784       1.9       bsh 	if (sc->sc_heldchange && (ufstat & UFSTAT_TXCOUNT) == 0) {
   1785       1.9       bsh 		/* XXX: we should check transmitter empty also */
   1786       1.9       bsh 		sscom_loadchannelregs(sc);
   1787       1.9       bsh 		sc->sc_heldchange = 0;
   1788       1.9       bsh 		sc->sc_tbc = sc->sc_heldtbc;
   1789       1.9       bsh 		sc->sc_heldtbc = 0;
   1790       1.9       bsh 	}
   1791       1.9       bsh 
   1792       1.9       bsh 	/*
   1793       1.9       bsh 	 * See if data can be transmitted as well. Schedule tx
   1794       1.9       bsh 	 * done event if no data left and tty was marked busy.
   1795       1.9       bsh 	 */
   1796       1.9       bsh 	if (!ISSET(ufstat,UFSTAT_TXFULL)) {
   1797       1.9       bsh 		/*
   1798       1.9       bsh 		 * Output the next chunk of the contiguous
   1799       1.9       bsh 		 * buffer, if any.
   1800       1.1       bsh 		 */
   1801       1.9       bsh 		if (sc->sc_tbc > 0) {
   1802       1.9       bsh 			__sscom_output_chunk(sc, ufstat);
   1803       1.9       bsh 		}
   1804       1.9       bsh 		else {
   1805       1.9       bsh 			/*
   1806       1.9       bsh 			 * Disable transmit sscompletion
   1807       1.9       bsh 			 * interrupts if necessary.
   1808       1.1       bsh 			 */
   1809       1.9       bsh 			if (sc->sc_hwflags & SSCOM_HW_TXINT)
   1810       1.9       bsh 				sscom_disable_txint(sc);
   1811       1.9       bsh 			if (sc->sc_tx_busy) {
   1812       1.9       bsh 				sc->sc_tx_busy = 0;
   1813       1.9       bsh 				sc->sc_tx_done = 1;
   1814       1.1       bsh 			}
   1815       1.1       bsh 		}
   1816       1.9       bsh 	}
   1817       1.1       bsh 
   1818       1.1       bsh 	SSCOM_UNLOCK(sc);
   1819       1.1       bsh 
   1820       1.1       bsh 	/* Wake up the poller. */
   1821       1.1       bsh 	softintr_schedule(sc->sc_si);
   1822       1.1       bsh 
   1823       1.1       bsh #if NRND > 0 && defined(RND_COM)
   1824       1.1       bsh 	rnd_add_uint32(&sc->rnd_source, iir | rsr);
   1825       1.1       bsh #endif
   1826       1.1       bsh 
   1827       1.1       bsh 	return 1;
   1828       1.1       bsh }
   1829       1.1       bsh 
   1830       1.1       bsh 
   1831       1.1       bsh #if defined(KGDB) || defined(SSCOM0CONSOLE) || defined(SSCOM1CONSOLE)
   1832       1.1       bsh /*
   1833       1.1       bsh  * Initialize UART for use as console or KGDB line.
   1834       1.1       bsh  */
   1835       1.1       bsh static int
   1836       1.1       bsh sscom_init(bus_space_tag_t iot, const struct sscom_uart_info *config,
   1837       1.1       bsh     int rate, int frequency, tcflag_t cflag, bus_space_handle_t *iohp)
   1838       1.1       bsh {
   1839       1.1       bsh 	bus_space_handle_t ioh;
   1840       1.1       bsh 	bus_addr_t iobase = config->iobase;
   1841       1.1       bsh 
   1842       1.1       bsh 	if (bus_space_map(iot, iobase, SSCOM_SIZE, 0, &ioh))
   1843       1.1       bsh 		return ENOMEM; /* ??? */
   1844       1.1       bsh 
   1845       1.1       bsh 	bus_space_write_2(iot, ioh, SSCOM_UCON, 0);
   1846       1.1       bsh 	bus_space_write_1(iot, ioh, SSCOM_UFCON,
   1847       1.1       bsh 	    UFCON_TXTRIGGER_8 | UFCON_RXTRIGGER_8 |
   1848       1.1       bsh 	    UFCON_TXFIFO_RESET | UFCON_RXFIFO_RESET |
   1849       1.1       bsh 	    UFCON_FIFO_ENABLE );
   1850       1.1       bsh 	/* tx/rx fifo reset are auto-cleared */
   1851       1.1       bsh 
   1852       1.1       bsh 	rate = sscomspeed(rate, frequency);
   1853       1.1       bsh 	bus_space_write_2(iot, ioh, SSCOM_UBRDIV, rate);
   1854       1.1       bsh 	bus_space_write_2(iot, ioh, SSCOM_ULCON, cflag2lcr(cflag));
   1855       1.1       bsh 
   1856       1.1       bsh 	/* enable UART */
   1857       1.1       bsh 	bus_space_write_2(iot, ioh, SSCOM_UCON,
   1858       1.1       bsh 	    UCON_TXMODE_INT|UCON_RXMODE_INT);
   1859       1.1       bsh 	bus_space_write_2(iot, ioh, SSCOM_UMCON, UMCON_RTS);
   1860       1.1       bsh 
   1861       1.1       bsh 	*iohp = ioh;
   1862       1.1       bsh 	return 0;
   1863       1.1       bsh }
   1864       1.1       bsh 
   1865       1.1       bsh #endif
   1866       1.1       bsh 
   1867       1.1       bsh #if defined(SSCOM0CONSOLE) || defined(SSCOM1CONSOLE)
   1868       1.1       bsh /*
   1869       1.1       bsh  * Following are all routines needed for SSCOM to act as console
   1870       1.1       bsh  */
   1871       1.1       bsh struct consdev sscomcons = {
   1872       1.1       bsh 	NULL, NULL, sscomcngetc, sscomcnputc, sscomcnpollc, NULL,
   1873       1.2        he 	NULL, NULL, NODEV, CN_NORMAL
   1874       1.1       bsh };
   1875       1.1       bsh 
   1876       1.1       bsh 
   1877       1.1       bsh int
   1878       1.1       bsh sscom_cnattach(bus_space_tag_t iot, const struct sscom_uart_info *config,
   1879       1.1       bsh     int rate, int frequency, tcflag_t cflag)
   1880       1.1       bsh {
   1881       1.1       bsh 	int res;
   1882       1.1       bsh 
   1883       1.1       bsh 	res = sscom_init(iot, config, rate, frequency, cflag, &sscomconsioh);
   1884       1.1       bsh 	if (res)
   1885       1.1       bsh 		return res;
   1886       1.1       bsh 
   1887       1.1       bsh 	cn_tab = &sscomcons;
   1888       1.1       bsh 	cn_init_magic(&sscom_cnm_state);
   1889       1.1       bsh 	cn_set_magic("\047\001"); /* default magic is BREAK */
   1890       1.1       bsh 
   1891       1.1       bsh 	sscomconstag = iot;
   1892       1.1       bsh 	sscomconsunit = config->unit;
   1893       1.1       bsh 	sscomconsrate = rate;
   1894       1.1       bsh 	sscomconscflag = cflag;
   1895       1.1       bsh 
   1896       1.1       bsh 	return 0;
   1897       1.1       bsh }
   1898       1.1       bsh 
   1899       1.1       bsh void
   1900       1.1       bsh sscom_cndetach(void)
   1901       1.1       bsh {
   1902       1.1       bsh 	bus_space_unmap(sscomconstag, sscomconsioh, SSCOM_SIZE);
   1903       1.1       bsh 	sscomconstag = NULL;
   1904       1.1       bsh 
   1905       1.1       bsh 	cn_tab = NULL;
   1906       1.1       bsh }
   1907       1.1       bsh 
   1908       1.1       bsh /*
   1909       1.1       bsh  * The read-ahead code is so that you can detect pending in-band
   1910       1.1       bsh  * cn_magic in polled mode while doing output rather than having to
   1911       1.1       bsh  * wait until the kernel decides it needs input.
   1912       1.1       bsh  */
   1913       1.1       bsh 
   1914       1.1       bsh #define MAX_READAHEAD	20
   1915       1.1       bsh static int sscom_readahead[MAX_READAHEAD];
   1916       1.1       bsh static int sscom_readaheadcount = 0;
   1917       1.1       bsh 
   1918       1.1       bsh int
   1919       1.1       bsh sscomcngetc(dev_t dev)
   1920       1.1       bsh {
   1921       1.1       bsh 	int s = splserial();
   1922       1.1       bsh 	u_char stat, c;
   1923       1.1       bsh 
   1924       1.1       bsh 	/* got a character from reading things earlier */
   1925       1.1       bsh 	if (sscom_readaheadcount > 0) {
   1926       1.1       bsh 		int i;
   1927       1.1       bsh 
   1928       1.1       bsh 		c = sscom_readahead[0];
   1929       1.1       bsh 		for (i = 1; i < sscom_readaheadcount; i++) {
   1930       1.1       bsh 			sscom_readahead[i-1] = sscom_readahead[i];
   1931       1.1       bsh 		}
   1932       1.1       bsh 		sscom_readaheadcount--;
   1933       1.1       bsh 		splx(s);
   1934       1.1       bsh 		return c;
   1935       1.1       bsh 	}
   1936       1.1       bsh 
   1937       1.1       bsh 	/* block until a character becomes available */
   1938       1.3       bsh 	while (!sscom_rxrdy(sscomconstag, sscomconsioh))
   1939       1.1       bsh 		;
   1940       1.1       bsh 
   1941       1.1       bsh 	c = sscom_getc(sscomconstag, sscomconsioh);
   1942       1.1       bsh 	stat = sscom_geterr(sscomconstag, sscomconsioh);
   1943       1.1       bsh 	{
   1944       1.1       bsh 		int cn_trapped = 0; /* unused */
   1945       1.1       bsh #ifdef DDB
   1946       1.1       bsh 		extern int db_active;
   1947       1.1       bsh 		if (!db_active)
   1948       1.1       bsh #endif
   1949       1.1       bsh 			cn_check_magic(dev, c, sscom_cnm_state);
   1950       1.1       bsh 	}
   1951       1.1       bsh 	splx(s);
   1952       1.1       bsh 	return c;
   1953       1.1       bsh }
   1954       1.1       bsh 
   1955       1.1       bsh /*
   1956       1.1       bsh  * Console kernel output character routine.
   1957       1.1       bsh  */
   1958       1.1       bsh void
   1959       1.1       bsh sscomcnputc(dev_t dev, int c)
   1960       1.1       bsh {
   1961       1.1       bsh 	int s = splserial();
   1962       1.1       bsh 	int timo;
   1963       1.1       bsh 
   1964       1.1       bsh 	int cin, stat;
   1965       1.1       bsh 	if (sscom_readaheadcount < MAX_READAHEAD &&
   1966       1.3       bsh 	    sscom_rxrdy(sscomconstag, sscomconsioh)) {
   1967       1.1       bsh 
   1968       1.1       bsh 		int cn_trapped = 0;
   1969       1.1       bsh 		cin = sscom_getc(sscomconstag, sscomconsioh);
   1970       1.1       bsh 		stat = sscom_geterr(sscomconstag, sscomconsioh);
   1971       1.1       bsh 		cn_check_magic(dev, cin, sscom_cnm_state);
   1972       1.1       bsh 		sscom_readahead[sscom_readaheadcount++] = cin;
   1973       1.1       bsh 	}
   1974       1.1       bsh 
   1975       1.1       bsh 	/* wait for any pending transmission to finish */
   1976       1.1       bsh 	timo = 150000;
   1977       1.1       bsh 	while (ISSET(bus_space_read_2(sscomconstag, sscomconsioh, SSCOM_UFSTAT),
   1978       1.1       bsh 		   UFSTAT_TXFULL) && --timo)
   1979       1.1       bsh 		continue;
   1980       1.1       bsh 
   1981       1.1       bsh 	bus_space_write_1(sscomconstag, sscomconsioh, SSCOM_UTXH, c);
   1982       1.1       bsh 	SSCOM_BARRIER(sscomconstag, sscomconsioh, BR | BW);
   1983       1.1       bsh 
   1984       1.1       bsh #if 0
   1985       1.1       bsh 	/* wait for this transmission to complete */
   1986       1.1       bsh 	timo = 1500000;
   1987       1.1       bsh 	while (!ISSET(bus_space_read_1(sscomconstag, sscomconsioh, SSCOM_UTRSTAT),
   1988       1.1       bsh 		   UTRSTAT_TXEMPTY) && --timo)
   1989       1.1       bsh 		continue;
   1990       1.1       bsh #endif
   1991       1.1       bsh 	splx(s);
   1992       1.1       bsh }
   1993       1.1       bsh 
   1994       1.1       bsh void
   1995       1.1       bsh sscomcnpollc(dev_t dev, int on)
   1996       1.1       bsh {
   1997       1.1       bsh 
   1998       1.1       bsh }
   1999       1.1       bsh 
   2000       1.1       bsh #endif /* SSCOM0CONSOLE||SSCOM1CONSOLE */
   2001       1.1       bsh 
   2002       1.1       bsh #ifdef KGDB
   2003       1.1       bsh int
   2004       1.1       bsh sscom_kgdb_attach(bus_space_tag_t iot, const struct sscom_uart_info *config,
   2005       1.1       bsh     int rate, int frequency, tcflag_t cflag)
   2006       1.1       bsh {
   2007       1.1       bsh 	int res;
   2008       1.1       bsh 
   2009       1.3       bsh 	if (iot == sscomconstag && config->unit == sscomconsunit) {
   2010       1.1       bsh 		printf( "console==kgdb_port (%d): kgdb disabled\n", sscomconsunit);
   2011       1.1       bsh 		return EBUSY; /* cannot share with console */
   2012       1.1       bsh 	}
   2013       1.1       bsh 
   2014       1.1       bsh 	res = sscom_init(iot, config, rate, frequency, cflag, &sscom_kgdb_ioh);
   2015       1.1       bsh 	if (res)
   2016       1.1       bsh 		return res;
   2017       1.1       bsh 
   2018       1.1       bsh 	kgdb_attach(sscom_kgdb_getc, sscom_kgdb_putc, NULL);
   2019       1.1       bsh 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   2020       1.1       bsh 
   2021       1.1       bsh 	sscom_kgdb_iot = iot;
   2022       1.1       bsh 	sscom_kgdb_unit = config->unit;
   2023       1.1       bsh 
   2024       1.1       bsh 	return 0;
   2025       1.1       bsh }
   2026       1.1       bsh 
   2027       1.1       bsh /* ARGSUSED */
   2028       1.1       bsh int
   2029       1.1       bsh sscom_kgdb_getc(void *arg)
   2030       1.1       bsh {
   2031       1.1       bsh 	int c, stat;
   2032       1.1       bsh 
   2033       1.1       bsh 	/* block until a character becomes available */
   2034       1.3       bsh 	while (!sscom_rxrdy(sscom_kgdb_iot, sscom_kgdb_ioh))
   2035       1.1       bsh 		;
   2036       1.1       bsh 
   2037       1.1       bsh 	c = sscom_getc(sscom_kgdb_iot, sscom_kgdb_ioh);
   2038       1.1       bsh 	stat = sscom_geterr(sscom_kgdb_iot, sscom_kgdb_ioh);
   2039       1.1       bsh 
   2040       1.1       bsh 	return c;
   2041       1.1       bsh }
   2042       1.1       bsh 
   2043       1.1       bsh /* ARGSUSED */
   2044       1.1       bsh void
   2045       1.1       bsh sscom_kgdb_putc(void *arg, int c)
   2046       1.1       bsh {
   2047       1.1       bsh 	int timo;
   2048       1.1       bsh 
   2049       1.1       bsh 	/* wait for any pending transmission to finish */
   2050       1.1       bsh 	timo = 150000;
   2051       1.1       bsh 	while (ISSET(bus_space_read_2(sscom_kgdb_iot, sscom_kgdb_ioh,
   2052       1.1       bsh 	    SSCOM_UFSTAT), UFSTAT_TXFULL) && --timo)
   2053       1.1       bsh 		continue;
   2054       1.1       bsh 
   2055       1.1       bsh 	bus_space_write_1(sscom_kgdb_iot, sscom_kgdb_ioh, SSCOM_UTXH, c);
   2056       1.1       bsh 	SSCOM_BARRIER(sscom_kgdb_iot, sscom_kgdb_ioh, BR | BW);
   2057       1.1       bsh 
   2058       1.1       bsh #if 0
   2059       1.1       bsh 	/* wait for this transmission to complete */
   2060       1.1       bsh 	timo = 1500000;
   2061       1.1       bsh 	while (!ISSET(bus_space_read_1(sscom_kgdb_iot, sscom_kgdb_ioh,
   2062       1.1       bsh 	    SSCOM_UTRSTAT), UTRSTAT_TXEMPTY) && --timo)
   2063       1.1       bsh 		continue;
   2064       1.1       bsh #endif
   2065       1.1       bsh }
   2066       1.1       bsh #endif /* KGDB */
   2067       1.1       bsh 
   2068       1.1       bsh /* helper function to identify the sscom ports used by
   2069       1.1       bsh  console or KGDB (and not yet autoconf attached) */
   2070       1.1       bsh int
   2071       1.1       bsh sscom_is_console(bus_space_tag_t iot, int unit,
   2072       1.1       bsh     bus_space_handle_t *ioh)
   2073       1.1       bsh {
   2074       1.1       bsh 	bus_space_handle_t help;
   2075       1.1       bsh 
   2076       1.1       bsh 	if (!sscomconsattached &&
   2077       1.1       bsh 	    iot == sscomconstag && unit == sscomconsunit)
   2078       1.1       bsh 		help = sscomconsioh;
   2079       1.1       bsh #ifdef KGDB
   2080       1.1       bsh 	else if (!sscom_kgdb_attached &&
   2081       1.1       bsh 	    iot == sscom_kgdb_iot && unit == sscom_kgdb_unit)
   2082       1.1       bsh 		help = sscom_kgdb_ioh;
   2083       1.1       bsh #endif
   2084       1.1       bsh 	else
   2085       1.1       bsh 		return 0;
   2086       1.1       bsh 
   2087       1.1       bsh 	if (ioh)
   2088       1.1       bsh 		*ioh = help;
   2089       1.1       bsh 	return 1;
   2090       1.1       bsh }
   2091