Home | History | Annotate | Line # | Download | only in dev
sbscn.c revision 1.37.6.1
      1  1.37.6.1       tls /* $NetBSD: sbscn.c,v 1.37.6.1 2014/08/20 00:03:13 tls Exp $ */
      2       1.1    simonb 
      3       1.1    simonb /*
      4       1.1    simonb  * Copyright 2000, 2001
      5       1.1    simonb  * Broadcom Corporation. All rights reserved.
      6       1.1    simonb  *
      7       1.1    simonb  * This software is furnished under license and may be used and copied only
      8       1.1    simonb  * in accordance with the following terms and conditions.  Subject to these
      9       1.1    simonb  * conditions, you may download, copy, install, use, modify and distribute
     10       1.1    simonb  * modified or unmodified copies of this software in source and/or binary
     11       1.1    simonb  * form. No title or ownership is transferred hereby.
     12       1.1    simonb  *
     13       1.1    simonb  * 1) Any source code used, modified or distributed must reproduce and
     14       1.1    simonb  *    retain this copyright notice and list of conditions as they appear in
     15       1.1    simonb  *    the source file.
     16       1.1    simonb  *
     17       1.1    simonb  * 2) No right is granted to use any trade name, trademark, or logo of
     18       1.7       cgd  *    Broadcom Corporation.  The "Broadcom Corporation" name may not be
     19       1.7       cgd  *    used to endorse or promote products derived from this software
     20       1.7       cgd  *    without the prior written permission of Broadcom Corporation.
     21       1.1    simonb  *
     22       1.1    simonb  * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
     23       1.1    simonb  *    WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
     24       1.1    simonb  *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
     25       1.1    simonb  *    NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
     26       1.1    simonb  *    FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
     27       1.1    simonb  *    LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28       1.1    simonb  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29       1.1    simonb  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     30       1.1    simonb  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     31       1.1    simonb  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     32       1.1    simonb  *    OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33       1.1    simonb  */
     34       1.1    simonb 
     35       1.1    simonb /* from: $NetBSD: com.c,v 1.172 2000/05/03 19:19:04 thorpej Exp */
     36       1.1    simonb 
     37       1.1    simonb /*-
     38       1.1    simonb  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
     39       1.1    simonb  * All rights reserved.
     40       1.1    simonb  *
     41       1.1    simonb  * This code is derived from software contributed to The NetBSD Foundation
     42       1.1    simonb  * by Charles M. Hannum.
     43       1.1    simonb  *
     44       1.1    simonb  * Redistribution and use in source and binary forms, with or without
     45       1.1    simonb  * modification, are permitted provided that the following conditions
     46       1.1    simonb  * are met:
     47       1.1    simonb  * 1. Redistributions of source code must retain the above copyright
     48       1.1    simonb  *    notice, this list of conditions and the following disclaimer.
     49       1.1    simonb  * 2. Redistributions in binary form must reproduce the above copyright
     50       1.1    simonb  *    notice, this list of conditions and the following disclaimer in the
     51       1.1    simonb  *    documentation and/or other materials provided with the distribution.
     52       1.1    simonb  *
     53       1.1    simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     54       1.1    simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     55       1.1    simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     56       1.1    simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     57       1.1    simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     58       1.1    simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     59       1.1    simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     60       1.1    simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     61       1.1    simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     62       1.1    simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     63       1.1    simonb  * POSSIBILITY OF SUCH DAMAGE.
     64       1.1    simonb  */
     65       1.1    simonb 
     66       1.1    simonb /*
     67       1.1    simonb  * Copyright (c) 1991 The Regents of the University of California.
     68       1.1    simonb  * All rights reserved.
     69       1.1    simonb  *
     70       1.1    simonb  * Redistribution and use in source and binary forms, with or without
     71       1.1    simonb  * modification, are permitted provided that the following conditions
     72       1.1    simonb  * are met:
     73       1.1    simonb  * 1. Redistributions of source code must retain the above copyright
     74       1.1    simonb  *    notice, this list of conditions and the following disclaimer.
     75       1.1    simonb  * 2. Redistributions in binary form must reproduce the above copyright
     76       1.1    simonb  *    notice, this list of conditions and the following disclaimer in the
     77       1.1    simonb  *    documentation and/or other materials provided with the distribution.
     78      1.12       agc  * 3. Neither the name of the University nor the names of its contributors
     79       1.1    simonb  *    may be used to endorse or promote products derived from this software
     80       1.1    simonb  *    without specific prior written permission.
     81       1.1    simonb  *
     82       1.1    simonb  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     83       1.1    simonb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     84       1.1    simonb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     85       1.1    simonb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     86       1.1    simonb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     87       1.1    simonb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     88       1.1    simonb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     89       1.1    simonb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     90       1.1    simonb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     91       1.1    simonb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     92       1.1    simonb  * SUCH DAMAGE.
     93       1.1    simonb  *
     94       1.1    simonb  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     95       1.1    simonb  */
     96       1.1    simonb 
     97       1.1    simonb /*
     98       1.1    simonb  * `sbscn' driver, supports SiByte SB-1250 SOC DUART.
     99       1.1    simonb  *
    100       1.1    simonb  * This DUART is quite similar in programming model to the scn2681/68681
    101       1.1    simonb  * DUARTs supported by the NetBSD/amiga `mfc' and NetBSD/pc532 `scn'
    102       1.1    simonb  * driver, but substantial rework would have been necessary to make
    103       1.1    simonb  * those drivers sane w.r.t. bus_space (which would then have been
    104       1.1    simonb  * required on NetBSD/sbmips very early on), and to accommodate the
    105       1.1    simonb  * different register mappings.
    106       1.1    simonb  *
    107       1.1    simonb  * So, another driver.  Eventually there should be One True Driver,
    108       1.1    simonb  * but we're not here to save the world.
    109       1.1    simonb  */
    110      1.11     lukem 
    111      1.11     lukem #include <sys/cdefs.h>
    112  1.37.6.1       tls __KERNEL_RCSID(0, "$NetBSD: sbscn.c,v 1.37.6.1 2014/08/20 00:03:13 tls Exp $");
    113      1.11     lukem 
    114      1.11     lukem #define	SBSCN_DEBUG
    115       1.1    simonb 
    116       1.1    simonb #include "opt_ddb.h"
    117      1.32      matt #include "ioconf.h"
    118       1.1    simonb 
    119       1.1    simonb #include "rnd.h"
    120      1.37       tls #ifdef RND_SBSCN
    121       1.1    simonb #include <sys/rnd.h>
    122       1.1    simonb #endif
    123       1.1    simonb 
    124       1.1    simonb #include <sys/param.h>
    125       1.1    simonb #include <sys/systm.h>
    126       1.1    simonb #include <sys/ioctl.h>
    127       1.1    simonb #include <sys/select.h>
    128       1.1    simonb #include <sys/tty.h>
    129       1.1    simonb #include <sys/proc.h>
    130       1.1    simonb #include <sys/conf.h>
    131       1.1    simonb #include <sys/file.h>
    132       1.1    simonb #include <sys/uio.h>
    133       1.1    simonb #include <sys/kernel.h>
    134       1.1    simonb #include <sys/syslog.h>
    135       1.1    simonb #include <sys/types.h>
    136       1.1    simonb #include <sys/device.h>
    137       1.1    simonb #include <sys/malloc.h>
    138       1.1    simonb #include <sys/vnode.h>
    139      1.17      elad #include <sys/kauth.h>
    140      1.25        ad #include <sys/intr.h>
    141       1.1    simonb 
    142       1.1    simonb #include <mips/sibyte/dev/sbobiovar.h>
    143       1.1    simonb #if 0
    144       1.1    simonb #include <mips/sibyte/dev/sbscnreg.h>
    145       1.1    simonb #endif
    146       1.1    simonb #include <mips/sibyte/dev/sbscnvar.h>
    147       1.1    simonb #include <dev/cons.h>
    148      1.36      matt #include <mips/locore.h>
    149       1.1    simonb 
    150       1.1    simonb void	sbscn_attach_channel(struct sbscn_softc *sc, int chan, int intr);
    151       1.1    simonb #if defined(DDB) || defined(KGDB)
    152       1.1    simonb static void sbscn_enable_debugport(struct sbscn_channel *ch);
    153       1.1    simonb #endif
    154       1.1    simonb void	sbscn_config(struct sbscn_channel *ch);
    155       1.1    simonb void	sbscn_shutdown(struct sbscn_channel *ch);
    156       1.1    simonb int	sbscn_speed(long, long *);
    157       1.1    simonb static int cflag2modes(tcflag_t, u_char *, u_char *);
    158       1.1    simonb int	sbscn_param(struct tty *, struct termios *);
    159       1.1    simonb void	sbscn_start(struct tty *);
    160       1.1    simonb int	sbscn_hwiflow(struct tty *, int);
    161       1.1    simonb 
    162       1.1    simonb void	sbscn_loadchannelregs(struct sbscn_channel *);
    163       1.1    simonb void	sbscn_dohwiflow(struct sbscn_channel *);
    164       1.1    simonb void	sbscn_break(struct sbscn_channel *, int);
    165       1.1    simonb void	sbscn_modem(struct sbscn_channel *, int);
    166       1.1    simonb void	tiocm_to_sbscn(struct sbscn_channel *, int, int);
    167       1.1    simonb int	sbscn_to_tiocm(struct sbscn_channel *);
    168       1.1    simonb void	sbscn_iflush(struct sbscn_channel *);
    169       1.1    simonb 
    170       1.1    simonb int	sbscn_init(u_long addr, int chan, int rate, tcflag_t cflag);
    171       1.1    simonb int	sbscn_common_getc(u_long addr, int chan);
    172       1.1    simonb void	sbscn_common_putc(u_long addr, int chan, int c);
    173      1.31      matt void	sbscn_intr(void *arg, uint32_t status, vaddr_t pc);
    174       1.1    simonb 
    175       1.1    simonb int	sbscn_cngetc(dev_t dev);
    176       1.1    simonb void	sbscn_cnputc(dev_t dev, int c);
    177       1.1    simonb void	sbscn_cnpollc(dev_t dev, int on);
    178       1.1    simonb 
    179       1.2   gehenna dev_type_open(sbscnopen);
    180       1.2   gehenna dev_type_close(sbscnclose);
    181       1.2   gehenna dev_type_read(sbscnread);
    182       1.2   gehenna dev_type_write(sbscnwrite);
    183       1.2   gehenna dev_type_ioctl(sbscnioctl);
    184       1.2   gehenna dev_type_stop(sbscnstop);
    185       1.2   gehenna dev_type_tty(sbscntty);
    186       1.2   gehenna dev_type_poll(sbscnpoll);
    187       1.2   gehenna 
    188       1.2   gehenna const struct cdevsw sbscn_cdevsw = {
    189  1.37.6.1       tls 	.d_open = sbscnopen,
    190  1.37.6.1       tls 	.d_close = sbscnclose,
    191  1.37.6.1       tls 	.d_read = sbscnread,
    192  1.37.6.1       tls 	.d_write = sbscnwrite,
    193  1.37.6.1       tls 	.d_ioctl = sbscnioctl,
    194  1.37.6.1       tls 	.d_stop = sbscnstop,
    195  1.37.6.1       tls 	.d_tty = sbscntty,
    196  1.37.6.1       tls 	.d_poll = sbscnpoll,
    197  1.37.6.1       tls 	.d_mmap = nommap,
    198  1.37.6.1       tls 	.d_kqfilter = ttykqfilter,
    199  1.37.6.1       tls 	.d_discard = nodiscard,
    200  1.37.6.1       tls 	.d_flag = D_TTY
    201       1.2   gehenna };
    202       1.2   gehenna 
    203       1.1    simonb #define	integrate	static inline
    204       1.1    simonb void 	sbscn_soft(void *);
    205       1.1    simonb integrate void sbscn_rxsoft(struct sbscn_channel *, struct tty *);
    206       1.1    simonb integrate void sbscn_txsoft(struct sbscn_channel *, struct tty *);
    207       1.1    simonb integrate void sbscn_stsoft(struct sbscn_channel *, struct tty *);
    208       1.1    simonb integrate void sbscn_schedrx(struct sbscn_channel *);
    209       1.1    simonb void	sbscn_diag(void *);
    210       1.1    simonb 
    211       1.1    simonb /*
    212       1.1    simonb  * Make this an option variable one can patch.
    213       1.1    simonb  * But be warned:  this must be a power of 2!
    214       1.1    simonb  */
    215       1.1    simonb u_int sbscn_rbuf_size = SBSCN_RING_SIZE;
    216       1.1    simonb 
    217       1.1    simonb /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    218       1.1    simonb u_int sbscn_rbuf_hiwat = (SBSCN_RING_SIZE * 1) / 4;
    219       1.1    simonb u_int sbscn_rbuf_lowat = (SBSCN_RING_SIZE * 3) / 4;
    220       1.1    simonb 
    221       1.1    simonb static int	sbscn_cons_present;
    222       1.1    simonb static int	sbscn_cons_attached;
    223       1.1    simonb static u_long	sbscn_cons_addr;
    224       1.1    simonb static int	sbscn_cons_chan;
    225       1.1    simonb static int	sbscn_cons_rate;
    226       1.1    simonb static tcflag_t	sbscn_cons_cflag;
    227       1.1    simonb 
    228       1.1    simonb #ifdef KGDB
    229       1.1    simonb #include <sys/kgdb.h>
    230       1.1    simonb 
    231       1.1    simonb static int	sbscn_kgdb_present;
    232       1.1    simonb static int	sbscn_kgdb_attached;
    233       1.1    simonb static u_long	sbscn_kgdb_addr;
    234       1.1    simonb static int	sbscn_kgdb_chan;
    235       1.1    simonb 
    236       1.1    simonb int	sbscn_kgdb_getc(void *);
    237       1.1    simonb void	sbscn_kgdb_putc(void *, int);
    238       1.1    simonb #endif /* KGDB */
    239       1.1    simonb 
    240      1.32      matt static int	sbscn_match(device_t, cfdata_t, void *);
    241      1.32      matt static void	sbscn_attach(device_t, device_t, void *);
    242       1.1    simonb 
    243      1.32      matt CFATTACH_DECL_NEW(sbscn, sizeof(struct sbscn_softc),
    244       1.4   thorpej     sbscn_match, sbscn_attach, NULL, NULL);
    245       1.1    simonb 
    246      1.31      matt #define	READ_REG(rp)		(mips3_ld((volatile uint64_t *)(rp)))
    247      1.31      matt #define	WRITE_REG(rp, val)	(mips3_sd((volatile uint64_t *)(rp), (val)))
    248       1.1    simonb 
    249       1.1    simonb /*
    250       1.1    simonb  * input and output signals are actually the _inverse_ of the bits in the
    251       1.1    simonb  * input and output port registers!
    252       1.1    simonb  */
    253       1.1    simonb #define	GET_INPUT_SIGNALS(ch) \
    254       1.1    simonb     ((~READ_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x280))) & (ch)->ch_i_mask)
    255       1.1    simonb #define	SET_OUTPUT_SIGNALS(ch, val) 					\
    256       1.1    simonb     do {								\
    257       1.1    simonb 	int sigs_to_set, sigs_to_clr;					\
    258       1.1    simonb 									\
    259       1.1    simonb 	sigs_to_set = (ch)->ch_o_mask & val;				\
    260       1.1    simonb 	sigs_to_clr = (ch)->ch_o_mask & ~val;				\
    261       1.1    simonb 									\
    262       1.1    simonb 	/* set signals by clearing op bits, and vice versa */		\
    263       1.1    simonb 	WRITE_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x2c0),	\
    264       1.1    simonb 	    sigs_to_set);						\
    265       1.1    simonb 	WRITE_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x2b0),	\
    266       1.1    simonb 	    sigs_to_clr);						\
    267       1.1    simonb     } while (0)
    268       1.1    simonb 
    269       1.1    simonb static int
    270      1.32      matt sbscn_match(device_t parent, cfdata_t match, void *aux)
    271       1.1    simonb {
    272      1.33      matt 	struct sbobio_attach_args *sa = aux;
    273       1.1    simonb 
    274      1.33      matt 	if (sa->sa_locs.sa_type != SBOBIO_DEVTYPE_DUART)
    275       1.1    simonb 		return (0);
    276       1.1    simonb 
    277       1.1    simonb 	return 1;
    278       1.1    simonb }
    279       1.1    simonb 
    280       1.1    simonb static void
    281      1.32      matt sbscn_attach(device_t parent, device_t self, void *aux)
    282       1.1    simonb {
    283      1.32      matt 	struct sbscn_softc *sc = device_private(self);
    284      1.33      matt 	struct sbobio_attach_args *sa = aux;
    285       1.1    simonb 	int i;
    286       1.1    simonb 
    287      1.33      matt 	sc->sc_dev = self;
    288      1.34      matt 	sc->sc_addr = sa->sa_base + sa->sa_locs.sa_offset;
    289       1.1    simonb 
    290      1.33      matt 	aprint_normal("\n");
    291       1.1    simonb 	for (i = 0; i < 2; i++)
    292      1.33      matt 		sbscn_attach_channel(sc, i, sa->sa_locs.sa_intr[i]);
    293       1.1    simonb 
    294       1.1    simonb 	/* init duart_opcr */
    295       1.1    simonb 	WRITE_REG(MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x270), 0);
    296       1.1    simonb 	/* init duart_aux_ctrl */
    297       1.1    simonb 	WRITE_REG(MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x210), 0x0f); /* XXX */
    298       1.1    simonb }
    299       1.1    simonb 
    300       1.1    simonb void
    301       1.1    simonb sbscn_attach_channel(struct sbscn_softc *sc, int chan, int intr)
    302       1.1    simonb {
    303       1.1    simonb 	struct sbscn_channel *ch = &sc->sc_channels[chan];
    304       1.1    simonb 	u_long chan_addr;
    305       1.1    simonb 	struct tty *tp;
    306       1.1    simonb 
    307       1.1    simonb 	ch->ch_sc = sc;
    308       1.1    simonb 	ch->ch_num = chan;
    309       1.1    simonb 
    310       1.1    simonb 	chan_addr = sc->sc_addr + (0x100 * chan);
    311       1.1    simonb 	ch->ch_base = (void *)MIPS_PHYS_TO_KSEG1(chan_addr);
    312       1.1    simonb 	ch->ch_isr_base =
    313       1.1    simonb 	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x220 + (0x20 * chan));
    314       1.1    simonb 	ch->ch_imr_base =
    315       1.1    simonb 	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x230 + (0x20 * chan));
    316       1.1    simonb #ifdef XXXCGDnotyet
    317       1.1    simonb 	ch->ch_inchg_base =
    318       1.1    simonb 	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x2d0 + (0x10 * chan));
    319       1.1    simonb #endif
    320       1.1    simonb 
    321       1.1    simonb 	ch->ch_i_dcd = ch->ch_i_dcd_pin = 0 /* XXXCGD */;
    322       1.1    simonb 	ch->ch_i_cts = ch->ch_i_cts_pin = 0 /* XXXCGD */;
    323       1.1    simonb 	ch->ch_i_dsr = ch->ch_i_dsr_pin = 0 /* XXXCGD */;
    324       1.1    simonb 	ch->ch_i_ri = ch->ch_i_ri_pin = 0 /* XXXCGD */;
    325       1.1    simonb 	ch->ch_i_mask =
    326       1.1    simonb 	    ch->ch_i_dcd | ch->ch_i_cts | ch->ch_i_dsr | ch->ch_i_ri;
    327       1.1    simonb 	ch->ch_o_dtr = ch->ch_o_dtr_pin = 0 /* XXXCGD */;
    328       1.1    simonb 	ch->ch_o_rts = ch->ch_o_rts_pin = 0 /* XXXCGD */;
    329       1.1    simonb 	ch->ch_o_mask = ch->ch_o_dtr | ch->ch_o_rts;
    330       1.1    simonb 
    331       1.1    simonb 	ch->ch_intrhand = cpu_intr_establish(intr, IPL_SERIAL, sbscn_intr, ch);
    332      1.22        ad 	callout_init(&ch->ch_diag_callout, 0);
    333       1.1    simonb 
    334       1.1    simonb 	/* Disable interrupts before configuring the device. */
    335       1.1    simonb 	ch->ch_imr = 0;
    336       1.1    simonb 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
    337       1.1    simonb 
    338       1.1    simonb 	if (sbscn_cons_present &&
    339       1.1    simonb 	    sbscn_cons_addr == chan_addr && sbscn_cons_chan == chan) {
    340       1.1    simonb 		sbscn_cons_attached = 1;
    341       1.1    simonb 
    342       1.1    simonb 		/* Make sure the console is always "hardwired". */
    343       1.1    simonb 		delay(1000);			/* wait for output to finish */
    344       1.1    simonb 		SET(ch->ch_hwflags, SBSCN_HW_CONSOLE);
    345       1.1    simonb 		SET(ch->ch_swflags, TIOCFLAG_SOFTCAR);
    346       1.1    simonb 	}
    347       1.1    simonb 
    348      1.35     rmind 	tp = tty_alloc();
    349       1.1    simonb 	tp->t_oproc = sbscn_start;
    350       1.1    simonb 	tp->t_param = sbscn_param;
    351       1.1    simonb 	tp->t_hwiflow = sbscn_hwiflow;
    352       1.1    simonb 
    353       1.1    simonb 	ch->ch_tty = tp;
    354       1.1    simonb 	ch->ch_rbuf = malloc(sbscn_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
    355       1.1    simonb 	if (ch->ch_rbuf == NULL) {
    356      1.32      matt 		aprint_error_dev(sc->sc_dev,
    357      1.32      matt 		    "channel %d: unable to allocate ring buffer\n",
    358      1.32      matt 		    chan);
    359       1.1    simonb 		return;
    360       1.1    simonb 	}
    361       1.1    simonb 	ch->ch_ebuf = ch->ch_rbuf + (sbscn_rbuf_size << 1);
    362       1.1    simonb 
    363       1.1    simonb 	tty_attach(tp);
    364       1.1    simonb 
    365       1.1    simonb 	if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
    366       1.1    simonb 		int maj;
    367       1.1    simonb 
    368       1.1    simonb 		/* locate the major number */
    369       1.2   gehenna 		maj = cdevsw_lookup_major(&sbscn_cdevsw);
    370       1.1    simonb 
    371      1.16   thorpej 		cn_tab->cn_dev = makedev(maj,
    372      1.32      matt 		    (device_unit(sc->sc_dev) << 1) + chan);
    373       1.1    simonb 
    374      1.32      matt 		aprint_normal_dev(sc->sc_dev, "channel %d: %s\n",
    375      1.32      matt 		    chan, "console");
    376       1.1    simonb 	}
    377       1.1    simonb 
    378       1.1    simonb #ifdef KGDB
    379       1.1    simonb 	/*
    380       1.1    simonb 	 * Allow kgdb to "take over" this port.  If this is
    381       1.1    simonb 	 * the kgdb device, it has exclusive use.
    382       1.1    simonb 	 */
    383       1.1    simonb 	if (sbscn_kgdb_present &&
    384       1.1    simonb 	    sbscn_kgdb_addr == chan_addr && sbscn_kgdb_chan == chan) {
    385       1.1    simonb 		sbscn_kgdb_attached = 1;
    386       1.1    simonb 
    387       1.1    simonb 		SET(sc->sc_hwflags, SBSCN_HW_KGDB);
    388      1.32      matt 		aprint_normal_dev(sc->sc_dev, "channel %d: %s\n",
    389      1.32      matt 		    chan, "kgdb");
    390       1.1    simonb 	}
    391       1.1    simonb #endif
    392       1.1    simonb 
    393      1.25        ad 	ch->ch_si = softint_establish(SOFTINT_SERIAL, sbscn_soft, ch);
    394       1.1    simonb 
    395      1.37       tls #ifdef RND_SBSCN
    396      1.32      matt 	rnd_attach_source(&ch->ch_rnd_source, device_xname(sc->sc_dev),
    397  1.37.6.1       tls 			  RND_TYPE_TTY, RND_FLAG_COLLECT_TIME|
    398  1.37.6.1       tls 					RND_FLAG_ESTIMATE_TIME);
    399       1.1    simonb #endif
    400       1.1    simonb 
    401       1.1    simonb 	sbscn_config(ch);
    402       1.1    simonb 
    403       1.1    simonb 	SET(ch->ch_hwflags, SBSCN_HW_DEV_OK);
    404       1.1    simonb }
    405       1.1    simonb 
    406       1.1    simonb int
    407       1.1    simonb sbscn_speed(long speed, long *brcp)
    408       1.1    simonb {
    409       1.1    simonb #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    410       1.1    simonb 
    411       1.1    simonb 	int x, err;
    412       1.1    simonb 	int frequency = 100000000;
    413       1.1    simonb 
    414       1.1    simonb 	*brcp = divrnd(frequency / 20, speed) - 1;
    415       1.1    simonb 
    416       1.1    simonb 	if (speed <= 0)
    417       1.1    simonb 		return (-1);
    418       1.1    simonb 	x = divrnd(frequency / 20, speed);
    419       1.1    simonb 	if (x <= 0)
    420       1.1    simonb 		return (-1);
    421       1.1    simonb 	err = divrnd(((quad_t)frequency) * 1000 / 20, speed * x) - 1000;
    422       1.1    simonb 	if (err < 0)
    423       1.1    simonb 		err = -err;
    424       1.1    simonb 	if (err > SBSCN_TOLERANCE)
    425       1.1    simonb 		return (-1);
    426       1.1    simonb 	*brcp = x - 1;
    427       1.1    simonb 	return (0);
    428       1.1    simonb 
    429       1.1    simonb #undef	divrnd
    430       1.1    simonb }
    431       1.1    simonb 
    432       1.1    simonb #ifdef SBSCN_DEBUG
    433      1.13       jmc void	sbscn_status(struct sbscn_channel *, const char *);
    434       1.1    simonb 
    435       1.1    simonb int	sbscn_debug = 0 /* XXXCGD */;
    436       1.1    simonb 
    437       1.1    simonb void
    438      1.13       jmc sbscn_status(struct sbscn_channel *ch, const char *str)
    439       1.1    simonb {
    440       1.1    simonb 	struct sbscn_softc *sc = ch->ch_sc;
    441       1.1    simonb 	struct tty *tp = ch->ch_tty;
    442       1.1    simonb 
    443      1.33      matt 	aprint_normal_dev(sc->sc_dev,
    444      1.33      matt 	    "chan %d: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
    445      1.33      matt 	    ch->ch_num, str,
    446       1.1    simonb 	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
    447       1.1    simonb 	    ISSET(ch->ch_iports, ch->ch_i_dcd) ? "+" : "-",
    448       1.1    simonb 	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
    449       1.1    simonb 	    ISSET(ch->ch_oports, ch->ch_o_dtr) ? "+" : "-",
    450       1.1    simonb 	    ch->ch_tx_stopped ? "+" : "-");
    451       1.1    simonb 
    452      1.33      matt 	aprint_normal_dev(sc->sc_dev,
    453      1.33      matt 	    "chan %d: %s %scrtscts %scts %sts_ttstop  %srts %xrx_flags\n",
    454      1.33      matt 	    ch->ch_num, str,
    455       1.1    simonb 	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
    456       1.1    simonb 	    ISSET(ch->ch_iports, ch->ch_i_cts) ? "+" : "-",
    457       1.1    simonb 	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
    458       1.1    simonb 	    ISSET(ch->ch_oports, ch->ch_o_rts) ? "+" : "-",
    459       1.1    simonb 	    ch->ch_rx_flags);
    460       1.1    simonb }
    461       1.1    simonb #endif
    462       1.1    simonb 
    463       1.1    simonb #if defined(DDB) || defined(KGDB)
    464       1.1    simonb static void
    465       1.1    simonb sbscn_enable_debugport(struct sbscn_channel *ch)
    466       1.1    simonb {
    467       1.1    simonb 	int s;
    468       1.1    simonb 
    469       1.1    simonb 	/* Turn on line break interrupt, set carrier. */
    470       1.1    simonb 	s = splserial();
    471       1.1    simonb 
    472       1.1    simonb #if 0	/* DO NOT turn on break interrupt at this time. */
    473       1.1    simonb 	ch->ch_imr = 0x04;
    474       1.1    simonb #else
    475       1.1    simonb 	ch->ch_imr = 0x00;
    476       1.1    simonb #endif
    477       1.1    simonb 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
    478       1.1    simonb 	SET(ch->ch_oports, ch->ch_o_dtr | ch->ch_o_rts);
    479       1.1    simonb 	SET_OUTPUT_SIGNALS(ch, ch->ch_oports);
    480       1.1    simonb 
    481       1.1    simonb 	splx(s);
    482       1.1    simonb }
    483       1.1    simonb #endif
    484       1.1    simonb 
    485       1.1    simonb void
    486       1.1    simonb sbscn_config(struct sbscn_channel *ch)
    487       1.1    simonb {
    488       1.1    simonb 
    489       1.1    simonb 	/* Disable interrupts before configuring the device. */
    490       1.1    simonb 	ch->ch_imr = 0x00;
    491       1.1    simonb 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
    492       1.1    simonb 
    493       1.1    simonb #ifdef DDB
    494       1.1    simonb 	if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE))
    495       1.1    simonb 		sbscn_enable_debugport(ch);
    496       1.1    simonb #endif
    497       1.1    simonb 
    498       1.1    simonb #ifdef KGDB
    499       1.1    simonb 	/*
    500       1.1    simonb 	 * Allow kgdb to "take over" this port.  If this is
    501       1.1    simonb 	 * the kgdb device, it has exclusive use.
    502       1.1    simonb 	 */
    503       1.1    simonb 	if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB))
    504       1.1    simonb 		sbscn_enable_debugport(ch);
    505       1.1    simonb #endif
    506       1.1    simonb }
    507       1.1    simonb 
    508       1.1    simonb void
    509       1.1    simonb sbscn_shutdown(struct sbscn_channel *ch)
    510       1.1    simonb {
    511       1.1    simonb 	struct tty *tp = ch->ch_tty;
    512       1.1    simonb 	int s;
    513       1.1    simonb 
    514       1.1    simonb 	s = splserial();
    515       1.1    simonb 
    516       1.1    simonb 	/* If we were asserting flow control, then deassert it. */
    517       1.1    simonb 	SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
    518       1.1    simonb 	sbscn_dohwiflow(ch);
    519       1.1    simonb 
    520       1.1    simonb 	/* Clear any break condition set with TIOCSBRK. */
    521       1.1    simonb 	sbscn_break(ch, 0);
    522       1.1    simonb 
    523       1.1    simonb 	/*
    524       1.1    simonb 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    525       1.1    simonb 	 * notice even if we immediately open the port again.
    526       1.6    simonb 	 * Avoid tsleeping above splhigh().
    527       1.1    simonb 	 */
    528       1.1    simonb 	if (ISSET(tp->t_cflag, HUPCL)) {
    529       1.1    simonb 		sbscn_modem(ch, 0);
    530       1.6    simonb 		splx(s);
    531       1.6    simonb 		/* XXX tsleep will only timeout */
    532       1.1    simonb 		(void) tsleep(ch, TTIPRI, ttclos, hz);
    533       1.6    simonb 		s = splserial();
    534       1.1    simonb 	}
    535       1.1    simonb 
    536       1.1    simonb 	/* Turn off interrupts. */
    537       1.1    simonb #ifdef DDB
    538       1.1    simonb 	if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE))
    539       1.1    simonb #if 0	/* DO NOT turn on break interrupt at this time. */
    540       1.1    simonb 		ch->ch_imr = 0x04; /* interrupt on break */
    541       1.1    simonb #else
    542       1.1    simonb 		ch->ch_imr = 0x00;
    543       1.1    simonb #endif
    544       1.1    simonb 	else
    545       1.1    simonb #endif
    546       1.1    simonb 		ch->ch_imr = 0;
    547       1.1    simonb 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
    548       1.1    simonb 
    549       1.1    simonb 	splx(s);
    550       1.1    simonb }
    551       1.1    simonb 
    552       1.1    simonb int
    553      1.15  christos sbscnopen(dev_t dev, int flag, int mode, struct lwp *l)
    554       1.1    simonb {
    555       1.1    simonb 	int chan = SBSCN_CHAN(dev);
    556       1.1    simonb 	struct sbscn_softc *sc;
    557       1.1    simonb 	struct sbscn_channel *ch;
    558       1.1    simonb 	struct tty *tp;
    559       1.1    simonb 	int s, s2;
    560       1.1    simonb 	int error;
    561       1.1    simonb 
    562      1.27    cegger 	sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
    563      1.27    cegger 	if (sc == NULL)
    564       1.1    simonb 		return (ENXIO);
    565       1.1    simonb 	ch = &sc->sc_channels[chan];
    566       1.1    simonb 	if (!ISSET(ch->ch_hwflags, SBSCN_HW_DEV_OK) || ch->ch_rbuf == NULL)
    567       1.1    simonb 		return (ENXIO);
    568       1.1    simonb 
    569       1.1    simonb #ifdef KGDB
    570       1.1    simonb 	/*
    571       1.1    simonb 	 * If this is the kgdb port, no other use is permitted.
    572       1.1    simonb 	 */
    573       1.1    simonb 	if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB))
    574       1.1    simonb 		return (EBUSY);
    575       1.1    simonb #endif
    576       1.1    simonb 
    577       1.1    simonb 	tp = ch->ch_tty;
    578       1.1    simonb 
    579      1.19      elad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    580       1.1    simonb 		return (EBUSY);
    581       1.1    simonb 
    582       1.1    simonb 	s = spltty();
    583       1.1    simonb 
    584       1.1    simonb 	/*
    585       1.1    simonb 	 * Do the following iff this is a first open.
    586       1.1    simonb 	 */
    587       1.1    simonb 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    588       1.1    simonb 		struct termios t;
    589       1.1    simonb 
    590       1.1    simonb 		tp->t_dev = dev;
    591       1.1    simonb 
    592       1.1    simonb 		s2 = splserial();
    593       1.1    simonb 
    594       1.1    simonb 		/* Turn on receive, break, and status change interrupts. */
    595       1.1    simonb #if 0	/* DO NOT turn on break or status change interrupt at this time. */
    596       1.1    simonb 		ch->ch_imr = 0xe;
    597       1.1    simonb #else
    598       1.1    simonb 		ch->ch_imr = 0x2;
    599       1.1    simonb #endif
    600       1.1    simonb 		WRITE_REG(ch->ch_imr_base, ch->ch_imr);
    601       1.1    simonb 
    602       1.1    simonb 		/* Fetch the current modem control status, needed later. */
    603       1.1    simonb 		ch->ch_iports = GET_INPUT_SIGNALS(ch);
    604       1.1    simonb 		ch->ch_iports_delta = 0;
    605       1.1    simonb 		splx(s2);
    606       1.1    simonb 
    607       1.1    simonb 		/*
    608       1.1    simonb 		 * Initialize the termios status to the defaults.  Add in the
    609       1.1    simonb 		 * sticky bits from TIOCSFLAGS.
    610       1.1    simonb 		 */
    611       1.1    simonb 		t.c_ispeed = 0;
    612       1.1    simonb 		if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
    613       1.1    simonb 			t.c_ospeed = sbscn_cons_rate;
    614       1.1    simonb 			t.c_cflag = sbscn_cons_cflag;
    615       1.1    simonb 		} else {
    616       1.1    simonb 			t.c_ospeed = TTYDEF_SPEED;
    617       1.1    simonb 			t.c_cflag = TTYDEF_CFLAG;
    618       1.1    simonb 		}
    619       1.1    simonb 		if (ISSET(ch->ch_swflags, TIOCFLAG_CLOCAL))
    620       1.1    simonb 			SET(t.c_cflag, CLOCAL);
    621       1.1    simonb 		if (ISSET(ch->ch_swflags, TIOCFLAG_CRTSCTS))
    622       1.1    simonb 			SET(t.c_cflag, CRTSCTS);
    623       1.1    simonb 		if (ISSET(ch->ch_swflags, TIOCFLAG_MDMBUF))
    624       1.1    simonb 			SET(t.c_cflag, MDMBUF);
    625       1.1    simonb 		/* Make sure sbscn_param() will do something. */
    626       1.1    simonb 		tp->t_ospeed = 0;
    627       1.1    simonb 		(void) sbscn_param(tp, &t);
    628       1.1    simonb 		tp->t_iflag = TTYDEF_IFLAG;
    629       1.1    simonb 		tp->t_oflag = TTYDEF_OFLAG;
    630       1.1    simonb 		tp->t_lflag = TTYDEF_LFLAG;
    631       1.1    simonb 		ttychars(tp);
    632       1.1    simonb 		ttsetwater(tp);
    633       1.1    simonb 
    634       1.1    simonb 		s2 = splserial();
    635       1.1    simonb 
    636       1.1    simonb 		/*
    637       1.1    simonb 		 * Turn on DTR.  We must always do this, even if carrier is not
    638       1.1    simonb 		 * present, because otherwise we'd have to use TIOCSDTR
    639       1.1    simonb 		 * immediately after setting CLOCAL, which applications do not
    640       1.1    simonb 		 * expect.  We always assert DTR while the device is open
    641       1.1    simonb 		 * unless explicitly requested to deassert it.
    642       1.1    simonb 		 */
    643       1.1    simonb 		sbscn_modem(ch, 1);
    644       1.1    simonb 
    645       1.1    simonb 		/* Clear the input ring, and unblock. */
    646       1.1    simonb 		ch->ch_rbput = ch->ch_rbget = ch->ch_rbuf;
    647       1.1    simonb 		ch->ch_rbavail = sbscn_rbuf_size;
    648       1.1    simonb 		sbscn_iflush(ch);
    649       1.1    simonb 		CLR(ch->ch_rx_flags, RX_ANY_BLOCK);
    650       1.1    simonb 		sbscn_dohwiflow(ch);
    651       1.1    simonb 
    652       1.1    simonb #ifdef SBSCN_DEBUG
    653       1.1    simonb 		if (sbscn_debug)
    654       1.1    simonb 			sbscn_status(ch, "sbscnopen  ");
    655       1.1    simonb #endif
    656       1.1    simonb 
    657       1.1    simonb 		splx(s2);
    658       1.1    simonb 	}
    659       1.1    simonb 
    660       1.1    simonb 	splx(s);
    661       1.1    simonb 
    662       1.1    simonb 	error = ttyopen(tp, SBSCN_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
    663       1.1    simonb 	if (error)
    664       1.1    simonb 		goto bad;
    665       1.1    simonb 
    666       1.1    simonb 	error = (*tp->t_linesw->l_open)(dev, tp);
    667       1.1    simonb 	if (error)
    668       1.1    simonb 		goto bad;
    669       1.1    simonb 
    670       1.1    simonb 	return (0);
    671       1.1    simonb 
    672       1.1    simonb bad:
    673       1.1    simonb 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    674       1.1    simonb 		/*
    675       1.1    simonb 		 * We failed to open the device, and nobody else had it opened.
    676       1.1    simonb 		 * Clean up the state as appropriate.
    677       1.1    simonb 		 */
    678       1.1    simonb 		sbscn_shutdown(ch);
    679       1.1    simonb 	}
    680       1.1    simonb 
    681       1.1    simonb 	return (error);
    682       1.1    simonb }
    683       1.1    simonb 
    684       1.1    simonb int
    685      1.15  christos sbscnclose(dev_t dev, int flag, int mode, struct lwp *l)
    686       1.1    simonb {
    687      1.27    cegger 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
    688       1.1    simonb 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
    689       1.1    simonb 	struct tty *tp = ch->ch_tty;
    690       1.1    simonb 
    691       1.1    simonb 	/* XXX This is for cons.c. */
    692       1.1    simonb 	if (!ISSET(tp->t_state, TS_ISOPEN))
    693       1.1    simonb 		return (0);
    694       1.1    simonb 
    695       1.1    simonb 	(*tp->t_linesw->l_close)(tp, flag);
    696       1.1    simonb 	ttyclose(tp);
    697       1.1    simonb 
    698       1.1    simonb 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    699       1.1    simonb 		/*
    700       1.1    simonb 		 * Although we got a last close, the device may still be in
    701       1.1    simonb 		 * use; e.g. if this was the dialout node, and there are still
    702       1.1    simonb 		 * processes waiting for carrier on the non-dialout node.
    703       1.1    simonb 		 */
    704       1.1    simonb 		sbscn_shutdown(ch);
    705       1.1    simonb 	}
    706       1.1    simonb 
    707       1.1    simonb 	return (0);
    708       1.1    simonb }
    709       1.1    simonb 
    710       1.1    simonb int
    711       1.1    simonb sbscnread(dev_t dev, struct uio *uio, int flag)
    712       1.1    simonb {
    713      1.27    cegger 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
    714       1.1    simonb 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
    715       1.1    simonb 	struct tty *tp = ch->ch_tty;
    716       1.1    simonb 
    717       1.1    simonb 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    718       1.1    simonb }
    719       1.1    simonb 
    720       1.1    simonb int
    721       1.1    simonb sbscnwrite(dev_t dev, struct uio *uio, int flag)
    722       1.1    simonb {
    723      1.27    cegger 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
    724       1.1    simonb 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
    725       1.1    simonb 	struct tty *tp = ch->ch_tty;
    726       1.1    simonb 
    727       1.1    simonb 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    728       1.1    simonb }
    729       1.1    simonb 
    730       1.1    simonb int
    731      1.15  christos sbscnpoll(dev_t dev, int events, struct lwp *l)
    732       1.1    simonb {
    733      1.27    cegger 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
    734       1.1    simonb 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
    735       1.1    simonb 	struct tty *tp = ch->ch_tty;
    736       1.1    simonb 
    737      1.15  christos 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    738       1.1    simonb }
    739       1.1    simonb 
    740       1.1    simonb struct tty *
    741       1.1    simonb sbscntty(dev_t dev)
    742       1.1    simonb {
    743      1.27    cegger 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
    744       1.1    simonb 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
    745       1.1    simonb 	struct tty *tp = ch->ch_tty;
    746       1.1    simonb 
    747       1.1    simonb 	return (tp);
    748       1.1    simonb }
    749       1.1    simonb 
    750       1.1    simonb int
    751      1.21  christos sbscnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    752       1.1    simonb {
    753      1.27    cegger 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
    754       1.1    simonb 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
    755       1.1    simonb 	struct tty *tp = ch->ch_tty;
    756       1.1    simonb 	int error;
    757       1.1    simonb 	int s;
    758       1.1    simonb 
    759      1.15  christos 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    760       1.6    simonb 	if (error != EPASSTHROUGH)
    761       1.1    simonb 		return (error);
    762       1.1    simonb 
    763      1.15  christos 	error = ttioctl(tp, cmd, data, flag, l);
    764       1.6    simonb 	if (error != EPASSTHROUGH)
    765       1.1    simonb 		return (error);
    766       1.1    simonb 
    767       1.1    simonb 	error = 0;
    768       1.1    simonb 
    769       1.1    simonb 	s = splserial();
    770       1.1    simonb 
    771       1.1    simonb 	switch (cmd) {
    772       1.1    simonb 	case TIOCSBRK:
    773       1.1    simonb 		sbscn_break(ch, 1);
    774       1.1    simonb 		break;
    775       1.1    simonb 
    776       1.1    simonb 	case TIOCCBRK:
    777       1.1    simonb 		sbscn_break(ch, 0);
    778       1.1    simonb 		break;
    779       1.1    simonb 
    780       1.1    simonb 	case TIOCSDTR:
    781       1.1    simonb 		sbscn_modem(ch, 1);
    782       1.1    simonb 		break;
    783       1.1    simonb 
    784       1.1    simonb 	case TIOCCDTR:
    785       1.1    simonb 		sbscn_modem(ch, 0);
    786       1.1    simonb 		break;
    787       1.1    simonb 
    788       1.1    simonb 	case TIOCGFLAGS:
    789       1.1    simonb 		*(int *)data = ch->ch_swflags;
    790       1.1    simonb 		break;
    791       1.1    simonb 
    792       1.1    simonb 	case TIOCSFLAGS:
    793      1.20      elad 		error = kauth_authorize_device_tty(l->l_cred,
    794      1.20      elad 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    795       1.1    simonb 		if (error)
    796       1.1    simonb 			break;
    797       1.1    simonb 		ch->ch_swflags = *(int *)data;
    798       1.1    simonb 		break;
    799       1.1    simonb 
    800       1.1    simonb 	case TIOCMSET:
    801       1.1    simonb 	case TIOCMBIS:
    802       1.1    simonb 	case TIOCMBIC:
    803       1.1    simonb 		tiocm_to_sbscn(ch, cmd, *(int *)data);
    804       1.1    simonb 		break;
    805       1.1    simonb 
    806       1.1    simonb 	case TIOCMGET:
    807       1.1    simonb 		*(int *)data = sbscn_to_tiocm(ch);
    808       1.1    simonb 		break;
    809       1.1    simonb 
    810       1.1    simonb 	default:
    811       1.6    simonb 		error = EPASSTHROUGH;
    812       1.1    simonb 		break;
    813       1.1    simonb 	}
    814       1.1    simonb 
    815       1.1    simonb 	splx(s);
    816       1.1    simonb 
    817       1.1    simonb #ifdef SBSCN_DEBUG
    818       1.1    simonb 	if (sbscn_debug)
    819       1.1    simonb 		sbscn_status(ch, "sbscn_ioctl ");
    820       1.1    simonb #endif
    821       1.1    simonb 
    822       1.1    simonb 	return (error);
    823       1.1    simonb }
    824       1.1    simonb 
    825       1.1    simonb integrate void
    826       1.1    simonb sbscn_schedrx(struct sbscn_channel *ch)
    827       1.1    simonb {
    828       1.1    simonb 
    829       1.1    simonb 	ch->ch_rx_ready = 1;
    830       1.1    simonb 
    831       1.1    simonb 	/* Wake up the poller. */
    832      1.25        ad 	softint_schedule(ch->ch_si);
    833       1.1    simonb }
    834       1.1    simonb 
    835       1.1    simonb void
    836       1.1    simonb sbscn_break(struct sbscn_channel *ch, int onoff)
    837       1.1    simonb {
    838       1.1    simonb 
    839       1.1    simonb /* XXXCGD delay break until not busy */
    840       1.1    simonb 	if (onoff)
    841       1.1    simonb 		WRITE_REG(ch->ch_base + 0x50, 0x60);
    842       1.1    simonb 	else
    843       1.1    simonb 		WRITE_REG(ch->ch_base + 0x50, 0x70);
    844       1.1    simonb 
    845       1.1    simonb #if 0
    846       1.1    simonb 	if (!ch->ch_heldchange) {
    847       1.1    simonb 		if (ch->ch_tx_busy) {
    848       1.1    simonb 			ch->ch_heldtbc = ch->ch_tbc;
    849       1.1    simonb 			ch->ch_tbc = 0;
    850       1.1    simonb 			ch->ch_heldchange = 1;
    851       1.1    simonb 		} else
    852       1.1    simonb 			sbscn_loadchannelregs(ch);
    853       1.1    simonb 	}
    854       1.1    simonb #endif
    855       1.1    simonb }
    856       1.1    simonb 
    857       1.1    simonb void
    858       1.1    simonb sbscn_modem(struct sbscn_channel *ch, int onoff)
    859       1.1    simonb {
    860       1.1    simonb 
    861       1.1    simonb 	if (ch->ch_o_dtr == 0)
    862       1.1    simonb 		return;
    863       1.1    simonb 
    864       1.1    simonb 	if (onoff)
    865       1.1    simonb 		SET(ch->ch_oports, ch->ch_o_dtr);
    866       1.1    simonb 	else
    867       1.1    simonb 		CLR(ch->ch_oports, ch->ch_o_dtr);
    868       1.1    simonb 
    869       1.1    simonb 	if (!ch->ch_heldchange) {
    870       1.1    simonb 		if (ch->ch_tx_busy) {
    871       1.1    simonb 			ch->ch_heldtbc = ch->ch_tbc;
    872       1.1    simonb 			ch->ch_tbc = 0;
    873       1.1    simonb 			ch->ch_heldchange = 1;
    874       1.1    simonb 		} else
    875       1.1    simonb 			sbscn_loadchannelregs(ch);
    876       1.1    simonb 	}
    877       1.1    simonb }
    878       1.1    simonb 
    879       1.1    simonb void
    880       1.1    simonb tiocm_to_sbscn(struct sbscn_channel *ch, int how, int ttybits)
    881       1.1    simonb {
    882       1.1    simonb 	u_char bits;
    883       1.1    simonb 
    884       1.1    simonb 	bits = 0;
    885       1.1    simonb 	if (ISSET(ttybits, TIOCM_DTR))
    886       1.1    simonb 		SET(bits, ch->ch_o_dtr);
    887       1.1    simonb 	if (ISSET(ttybits, TIOCM_RTS))
    888       1.1    simonb 		SET(bits, ch->ch_o_rts);
    889       1.1    simonb 
    890       1.1    simonb 	switch (how) {
    891       1.1    simonb 	case TIOCMBIC:
    892       1.1    simonb 		CLR(ch->ch_oports, bits);
    893       1.1    simonb 		break;
    894       1.1    simonb 
    895       1.1    simonb 	case TIOCMBIS:
    896       1.1    simonb 		SET(ch->ch_oports, bits);
    897       1.1    simonb 		break;
    898       1.1    simonb 
    899       1.1    simonb 	case TIOCMSET:
    900       1.1    simonb 		ch->ch_oports = bits;
    901       1.1    simonb 		break;
    902       1.1    simonb 	}
    903       1.1    simonb 
    904       1.1    simonb 	if (!ch->ch_heldchange) {
    905       1.1    simonb 		if (ch->ch_tx_busy) {
    906       1.1    simonb 			ch->ch_heldtbc = ch->ch_tbc;
    907       1.1    simonb 			ch->ch_tbc = 0;
    908       1.1    simonb 			ch->ch_heldchange = 1;
    909       1.1    simonb 		} else
    910       1.1    simonb 			sbscn_loadchannelregs(ch);
    911       1.1    simonb 	}
    912       1.1    simonb }
    913       1.1    simonb 
    914       1.1    simonb int
    915       1.1    simonb sbscn_to_tiocm(struct sbscn_channel *ch)
    916       1.1    simonb {
    917       1.1    simonb 	u_char hwbits;
    918       1.1    simonb 	int ttybits = 0;
    919       1.1    simonb 
    920       1.1    simonb 	hwbits = ch->ch_oports;
    921       1.1    simonb 	if (ISSET(hwbits, ch->ch_o_dtr))
    922       1.1    simonb 		SET(ttybits, TIOCM_DTR);
    923       1.1    simonb 	if (ISSET(hwbits, ch->ch_o_rts))
    924       1.1    simonb 		SET(ttybits, TIOCM_RTS);
    925       1.1    simonb 
    926       1.1    simonb 	hwbits = ch->ch_iports;
    927       1.1    simonb 	if (ISSET(hwbits, ch->ch_i_dcd))
    928       1.1    simonb 		SET(ttybits, TIOCM_CD);
    929       1.1    simonb 	if (ISSET(hwbits, ch->ch_i_cts))
    930       1.1    simonb 		SET(ttybits, TIOCM_CTS);
    931       1.1    simonb 	if (ISSET(hwbits, ch->ch_i_dsr))
    932       1.1    simonb 		SET(ttybits, TIOCM_DSR);
    933       1.1    simonb 	if (ISSET(hwbits, ch->ch_i_ri))
    934       1.1    simonb 		SET(ttybits, TIOCM_RI);
    935       1.1    simonb 
    936       1.1    simonb 	if (ch->ch_imr != 0)
    937       1.1    simonb 		SET(ttybits, TIOCM_LE);
    938       1.1    simonb 
    939       1.1    simonb 	return (ttybits);
    940       1.1    simonb }
    941       1.1    simonb 
    942       1.1    simonb static int
    943      1.28       dsl cflag2modes(tcflag_t cflag, u_char *mode1p, u_char *mode2p)
    944       1.1    simonb {
    945       1.1    simonb 	u_char mode1;
    946       1.1    simonb 	u_char mode2;
    947       1.1    simonb 	int err = 0;
    948       1.1    simonb 
    949       1.1    simonb 	mode1 = mode2 = 0;
    950       1.1    simonb 
    951       1.1    simonb 	switch (ISSET(cflag, CSIZE)) {
    952       1.1    simonb 	case CS7:
    953       1.1    simonb 		mode1 |= 2;			/* XXX */
    954       1.1    simonb 		break;
    955       1.1    simonb 	default:
    956       1.1    simonb 		err = -1;
    957       1.1    simonb 		/* FALLTHRU for sanity */
    958       1.1    simonb 	case CS8:
    959       1.1    simonb 		mode1 |= 3;			/* XXX */
    960       1.1    simonb 		break;
    961       1.1    simonb 	}
    962       1.1    simonb 	if (!ISSET(cflag, PARENB))
    963       1.1    simonb 		mode1 |= 2 << 3;
    964       1.1    simonb 	else {
    965       1.1    simonb 		mode1 |= 0 << 3;
    966       1.1    simonb 		if (ISSET(cflag, PARODD))
    967       1.1    simonb 			mode1 |= 1 << 2;
    968       1.1    simonb 	}
    969       1.1    simonb 
    970       1.1    simonb 	if (ISSET(cflag, CSTOPB))
    971       1.1    simonb 		mode2 |= 1 << 3;		/* two stop bits XXX not std */
    972       1.1    simonb 
    973       1.1    simonb 	if (ISSET(cflag, CRTSCTS)) {
    974       1.1    simonb 		mode1 |= 1 << 7;
    975       1.1    simonb 		mode2 |= 1 << 4;
    976       1.1    simonb 	}
    977       1.1    simonb 
    978       1.1    simonb 	*mode1p = mode1;
    979       1.1    simonb 	*mode2p = mode2;
    980       1.1    simonb 	return (err);
    981       1.1    simonb }
    982       1.1    simonb 
    983       1.1    simonb int
    984       1.1    simonb sbscn_param(struct tty *tp, struct termios *t)
    985       1.1    simonb {
    986      1.27    cegger 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(tp->t_dev));
    987       1.1    simonb 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
    988       1.1    simonb 	long brc;
    989       1.1    simonb 	u_char mode1, mode2;
    990       1.1    simonb 	int s;
    991       1.1    simonb 
    992       1.1    simonb /* XXX reset to console parameters if console? */
    993       1.1    simonb #if 0
    994       1.1    simonb XXX disable, enable.
    995       1.1    simonb #endif
    996       1.1    simonb 
    997       1.1    simonb 	/* Check requested parameters. */
    998       1.1    simonb 	if (sbscn_speed(t->c_ospeed, &brc) < 0)
    999       1.1    simonb 		return (EINVAL);
   1000       1.1    simonb 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
   1001       1.1    simonb 		return (EINVAL);
   1002       1.1    simonb 
   1003       1.1    simonb 	/*
   1004       1.1    simonb 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1005       1.1    simonb 	 * is always active.
   1006       1.1    simonb 	 */
   1007       1.1    simonb 	if (ISSET(ch->ch_swflags, TIOCFLAG_SOFTCAR) ||
   1008       1.1    simonb 	    ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
   1009       1.1    simonb 		SET(t->c_cflag, CLOCAL);
   1010       1.1    simonb 		CLR(t->c_cflag, HUPCL);
   1011       1.1    simonb 	}
   1012       1.1    simonb 
   1013       1.1    simonb 	/*
   1014       1.1    simonb 	 * If there were no changes, don't do anything.  This avoids dropping
   1015       1.1    simonb 	 * input and improves performance when all we did was frob things like
   1016       1.1    simonb 	 * VMIN and VTIME.
   1017       1.1    simonb 	 */
   1018       1.1    simonb 	if (tp->t_ospeed == t->c_ospeed &&
   1019       1.1    simonb 	    tp->t_cflag == t->c_cflag)
   1020       1.1    simonb 		return (0);
   1021       1.1    simonb 
   1022       1.1    simonb 	if (cflag2modes(t->c_cflag, &mode1, &mode2) < 0)
   1023       1.1    simonb 		return (EINVAL);
   1024       1.1    simonb 
   1025       1.1    simonb 	s = splserial();
   1026       1.1    simonb 
   1027       1.1    simonb 	ch->ch_mode1 = mode1;
   1028       1.1    simonb 	ch->ch_mode2 = mode2;
   1029       1.1    simonb 
   1030       1.1    simonb 	/*
   1031       1.1    simonb 	 * If we're not in a mode that assumes a connection is present, then
   1032       1.1    simonb 	 * ignore carrier changes.
   1033       1.1    simonb 	 */
   1034       1.1    simonb 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
   1035       1.1    simonb 		ch->ch_i_dcd = 0;
   1036       1.1    simonb 	else
   1037       1.1    simonb 		ch->ch_i_dcd = ch->ch_i_dcd_pin;
   1038       1.1    simonb 	/*
   1039       1.1    simonb 	 * Set the flow control pins depending on the current flow control
   1040       1.1    simonb 	 * mode.
   1041       1.1    simonb 	 */
   1042       1.1    simonb 	if (ISSET(t->c_cflag, CRTSCTS)) {
   1043       1.1    simonb 		ch->ch_o_dtr = ch->ch_o_dtr_pin;
   1044       1.1    simonb 		ch->ch_o_rts = ch->ch_o_rts_pin;
   1045       1.1    simonb 		ch->ch_i_cts = ch->ch_i_cts_pin;
   1046       1.1    simonb 		/* hw controle enable bits in mod regs set by cflag2modes */
   1047       1.1    simonb 	} else if (ISSET(t->c_cflag, MDMBUF)) {
   1048       1.1    simonb 		/*
   1049       1.1    simonb 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
   1050       1.1    simonb 		 * carrier detection.
   1051       1.1    simonb 		 */
   1052       1.1    simonb 		ch->ch_o_dtr = 0;
   1053       1.1    simonb 		ch->ch_o_rts = ch->ch_o_dtr_pin;
   1054       1.1    simonb 		ch->ch_i_cts = ch->ch_i_dcd_pin;
   1055       1.1    simonb 	} else {
   1056       1.1    simonb 		/*
   1057       1.1    simonb 		 * If no flow control, then always set RTS.  This will make
   1058       1.1    simonb 		 * the other side happy if it mistakenly thinks we're doing
   1059       1.1    simonb 		 * RTS/CTS flow control.
   1060       1.1    simonb 		 */
   1061       1.1    simonb 		ch->ch_o_dtr = ch->ch_o_dtr_pin | ch->ch_o_rts_pin;
   1062       1.1    simonb 		ch->ch_o_rts = 0;
   1063       1.1    simonb 		ch->ch_i_cts = 0;
   1064       1.1    simonb 		if (ISSET(ch->ch_oports, ch->ch_o_dtr_pin))
   1065       1.1    simonb 			SET(ch->ch_oports, ch->ch_o_rts_pin);
   1066       1.1    simonb 		else
   1067       1.1    simonb 			CLR(ch->ch_oports, ch->ch_o_rts_pin);
   1068       1.1    simonb 	}
   1069       1.1    simonb 	/* XXX maybe mask the ports which generate intrs? */
   1070       1.1    simonb 
   1071       1.1    simonb 	ch->ch_brc = brc;
   1072       1.1    simonb 
   1073       1.1    simonb 	/* XXX maybe set fifo-full receive mode if RTSCTS and high speed? */
   1074       1.1    simonb 
   1075       1.1    simonb 	/* And copy to tty. */
   1076       1.1    simonb 	tp->t_ispeed = 0;
   1077       1.1    simonb 	tp->t_ospeed = t->c_ospeed;
   1078       1.1    simonb 	tp->t_cflag = t->c_cflag;
   1079       1.1    simonb 
   1080       1.1    simonb 	if (!ch->ch_heldchange) {
   1081       1.1    simonb 		if (ch->ch_tx_busy) {
   1082       1.1    simonb 			ch->ch_heldtbc = ch->ch_tbc;
   1083       1.1    simonb 			ch->ch_tbc = 0;
   1084       1.1    simonb 			ch->ch_heldchange = 1;
   1085       1.1    simonb 		} else
   1086       1.1    simonb 			sbscn_loadchannelregs(ch);
   1087       1.1    simonb 	}
   1088       1.1    simonb 
   1089       1.1    simonb 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1090       1.1    simonb 		/* Disable the high water mark. */
   1091       1.1    simonb 		ch->ch_r_hiwat = 0;
   1092       1.1    simonb 		ch->ch_r_lowat = 0;
   1093       1.1    simonb 		if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
   1094       1.1    simonb 			CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
   1095       1.1    simonb 			sbscn_schedrx(ch);
   1096       1.1    simonb 		}
   1097       1.1    simonb 		if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
   1098       1.1    simonb 			CLR(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1099       1.1    simonb 			sbscn_dohwiflow(ch);
   1100       1.1    simonb 		}
   1101       1.1    simonb 	} else {
   1102       1.1    simonb 		ch->ch_r_hiwat = sbscn_rbuf_hiwat;
   1103       1.1    simonb 		ch->ch_r_lowat = sbscn_rbuf_lowat;
   1104       1.1    simonb 	}
   1105       1.1    simonb 
   1106       1.1    simonb 	splx(s);
   1107       1.1    simonb 
   1108       1.1    simonb 	/*
   1109       1.1    simonb 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1110       1.1    simonb 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1111       1.1    simonb 	 * explicit request.
   1112       1.1    simonb 	 */
   1113       1.1    simonb 	(void) (*tp->t_linesw->l_modem)(tp,
   1114       1.1    simonb 	    ISSET(ch->ch_iports, ch->ch_i_dcd));
   1115       1.1    simonb 
   1116       1.1    simonb #ifdef SBSCN_DEBUG
   1117       1.1    simonb 	if (sbscn_debug)
   1118       1.1    simonb 		sbscn_status(ch, "sbscnparam ");
   1119       1.1    simonb #endif
   1120       1.1    simonb 
   1121       1.1    simonb 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1122       1.1    simonb 		if (ch->ch_tx_stopped) {
   1123       1.1    simonb 			ch->ch_tx_stopped = 0;
   1124       1.1    simonb 			sbscn_start(tp);
   1125       1.1    simonb 		}
   1126       1.1    simonb 	}
   1127       1.1    simonb 
   1128       1.1    simonb 	return (0);
   1129       1.1    simonb }
   1130       1.1    simonb 
   1131       1.1    simonb void
   1132       1.1    simonb sbscn_iflush(struct sbscn_channel *ch)
   1133       1.1    simonb {
   1134       1.1    simonb #ifdef DIAGNOSTIC
   1135       1.1    simonb 	int reg;
   1136       1.1    simonb #endif
   1137       1.1    simonb 	int timo;
   1138       1.1    simonb 
   1139       1.1    simonb #ifdef DIAGNOSTIC
   1140       1.1    simonb 	reg = 0xffff;
   1141       1.1    simonb #endif
   1142       1.1    simonb 	timo = 50000;
   1143       1.1    simonb 	/* flush any pending I/O */
   1144       1.1    simonb 	while (ISSET(READ_REG(ch->ch_base + 0x20), 0x01)
   1145       1.1    simonb 	    && --timo)
   1146       1.1    simonb #ifdef DIAGNOSTIC
   1147       1.1    simonb 		reg =
   1148       1.1    simonb #else
   1149       1.1    simonb 		    (void)
   1150       1.1    simonb #endif
   1151       1.1    simonb 		    READ_REG(ch->ch_base + 0x60);
   1152       1.1    simonb #ifdef DIAGNOSTIC
   1153       1.1    simonb 	if (!timo)
   1154      1.32      matt 		aprint_debug_dev(ch->ch_sc->sc_dev,
   1155      1.32      matt 		    "sbscn_iflush timeout %02x\n", reg & 0xff);
   1156       1.1    simonb #endif
   1157       1.1    simonb }
   1158       1.1    simonb 
   1159       1.1    simonb void
   1160       1.1    simonb sbscn_loadchannelregs(struct sbscn_channel *ch)
   1161       1.1    simonb {
   1162       1.1    simonb 
   1163       1.1    simonb 	/* XXXXX necessary? */
   1164       1.1    simonb 	sbscn_iflush(ch);
   1165       1.1    simonb 
   1166       1.1    simonb 	WRITE_REG(ch->ch_imr_base, 0);
   1167       1.1    simonb 
   1168       1.1    simonb // XXX disable?
   1169       1.1    simonb 	WRITE_REG(ch->ch_base + 0x00, ch->ch_mode1);
   1170       1.1    simonb 	WRITE_REG(ch->ch_base + 0x10, ch->ch_mode2);
   1171       1.1    simonb 	WRITE_REG(ch->ch_base + 0x30, ch->ch_brc);
   1172       1.1    simonb 
   1173       1.1    simonb 	ch->ch_oports_active = ch->ch_oports;
   1174       1.1    simonb 	SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active);
   1175       1.1    simonb 
   1176       1.1    simonb 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1177       1.1    simonb }
   1178       1.1    simonb 
   1179       1.1    simonb int
   1180       1.1    simonb sbscn_hwiflow(struct tty *tp, int block)
   1181       1.1    simonb {
   1182      1.27    cegger 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(tp->t_dev));
   1183       1.1    simonb 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
   1184       1.1    simonb 	int s;
   1185       1.1    simonb 
   1186       1.1    simonb 	if (ch->ch_o_rts == 0)
   1187       1.1    simonb 		return (0);
   1188       1.1    simonb 
   1189       1.1    simonb 	s = splserial();
   1190       1.1    simonb 	if (block) {
   1191       1.1    simonb 		if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
   1192       1.1    simonb 			SET(ch->ch_rx_flags, RX_TTY_BLOCKED);
   1193       1.1    simonb 			sbscn_dohwiflow(ch);
   1194       1.1    simonb 		}
   1195       1.1    simonb 	} else {
   1196       1.1    simonb 		if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
   1197       1.1    simonb 			CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
   1198       1.1    simonb 			sbscn_schedrx(ch);
   1199       1.1    simonb 		}
   1200       1.1    simonb 		if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
   1201       1.1    simonb 			CLR(ch->ch_rx_flags, RX_TTY_BLOCKED);
   1202       1.1    simonb 			sbscn_dohwiflow(ch);
   1203       1.1    simonb 		}
   1204       1.1    simonb 	}
   1205       1.1    simonb 	splx(s);
   1206       1.1    simonb 	return (1);
   1207       1.1    simonb }
   1208       1.1    simonb 
   1209       1.1    simonb /*
   1210       1.1    simonb  * (un)block input via hw flowcontrol
   1211       1.1    simonb  */
   1212       1.1    simonb void
   1213       1.1    simonb sbscn_dohwiflow(struct sbscn_channel *ch)
   1214       1.1    simonb {
   1215       1.1    simonb 
   1216       1.1    simonb 	if (ch->ch_o_rts == 0)
   1217       1.1    simonb 		return;
   1218       1.1    simonb 
   1219       1.1    simonb 	if (ISSET(ch->ch_rx_flags, RX_ANY_BLOCK)) {
   1220       1.1    simonb 		CLR(ch->ch_oports, ch->ch_o_rts);
   1221       1.1    simonb 		CLR(ch->ch_oports_active, ch->ch_o_rts);
   1222       1.1    simonb 	} else {
   1223       1.1    simonb 		SET(ch->ch_oports, ch->ch_o_rts);
   1224       1.1    simonb 		SET(ch->ch_oports_active, ch->ch_o_rts);
   1225       1.1    simonb 	}
   1226       1.1    simonb 	SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active);
   1227       1.1    simonb }
   1228       1.1    simonb 
   1229       1.1    simonb void
   1230       1.1    simonb sbscn_start(struct tty *tp)
   1231       1.1    simonb {
   1232      1.27    cegger 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(tp->t_dev));
   1233       1.1    simonb 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
   1234       1.1    simonb 	int s;
   1235       1.1    simonb 
   1236       1.1    simonb 	s = spltty();
   1237       1.1    simonb 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1238       1.1    simonb 		goto out;
   1239       1.1    simonb 	if (ch->ch_tx_stopped)
   1240       1.1    simonb 		goto out;
   1241      1.24        ad 	if (!ttypull(tp))
   1242      1.24        ad 		goto out;
   1243       1.1    simonb 
   1244       1.1    simonb 	/* Grab the first contiguous region of buffer space. */
   1245       1.1    simonb 	{
   1246       1.1    simonb 		u_char *tba;
   1247       1.1    simonb 		int tbc;
   1248       1.1    simonb 
   1249       1.1    simonb 		tba = tp->t_outq.c_cf;
   1250       1.1    simonb 		tbc = ndqb(&tp->t_outq, 0);
   1251       1.1    simonb 
   1252       1.1    simonb 		(void)splserial();
   1253       1.1    simonb 
   1254       1.1    simonb 		ch->ch_tba = tba;
   1255       1.1    simonb 		ch->ch_tbc = tbc;
   1256       1.1    simonb 	}
   1257       1.1    simonb 
   1258       1.1    simonb 	SET(tp->t_state, TS_BUSY);
   1259       1.1    simonb 	ch->ch_tx_busy = 1;
   1260       1.1    simonb 
   1261       1.1    simonb 	/* Enable transmit completion interrupts if necessary. */
   1262       1.1    simonb 	if (!ISSET(ch->ch_imr, 0x1)) {
   1263       1.1    simonb 		SET(ch->ch_imr, 0x1);
   1264       1.1    simonb 		WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1265       1.1    simonb 	}
   1266       1.1    simonb 
   1267       1.1    simonb 	/* Output the first chunk of the contiguous buffer. */
   1268       1.1    simonb 	{
   1269       1.1    simonb 		u_char c;
   1270       1.1    simonb 
   1271       1.1    simonb 		while (ch->ch_tbc && READ_REG(ch->ch_base + 0x20) & 0x04) {
   1272       1.1    simonb 			c = *ch->ch_tba++;
   1273       1.1    simonb 			ch->ch_tbc--;
   1274       1.1    simonb 			WRITE_REG(ch->ch_base + 0x70, c);
   1275       1.1    simonb 		}
   1276       1.1    simonb 	}
   1277       1.1    simonb out:
   1278       1.1    simonb 	splx(s);
   1279       1.1    simonb 	return;
   1280       1.1    simonb }
   1281       1.1    simonb 
   1282       1.1    simonb /*
   1283       1.1    simonb  * Stop output on a line.
   1284       1.1    simonb  */
   1285       1.1    simonb void
   1286       1.1    simonb sbscnstop(struct tty *tp, int flag)
   1287       1.1    simonb {
   1288      1.27    cegger 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(tp->t_dev));
   1289       1.1    simonb 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
   1290       1.1    simonb 	int s;
   1291       1.1    simonb 
   1292       1.1    simonb 	s = splserial();
   1293       1.1    simonb 	if (ISSET(tp->t_state, TS_BUSY)) {
   1294       1.1    simonb 		/* Stop transmitting at the next chunk. */
   1295       1.1    simonb 		ch->ch_tbc = 0;
   1296       1.1    simonb 		ch->ch_heldtbc = 0;
   1297       1.1    simonb 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1298       1.1    simonb 			SET(tp->t_state, TS_FLUSH);
   1299       1.1    simonb 	}
   1300       1.1    simonb 	splx(s);
   1301       1.1    simonb }
   1302       1.1    simonb 
   1303       1.1    simonb void
   1304      1.27    cegger sbscn_diag(void *arg)
   1305       1.1    simonb {
   1306       1.1    simonb 	struct sbscn_channel *ch = arg;
   1307       1.1    simonb 	struct sbscn_softc *sc = ch->ch_sc;
   1308       1.1    simonb 	int overflows, floods;
   1309       1.1    simonb 	int s;
   1310       1.1    simonb 
   1311       1.1    simonb 	s = splserial();
   1312       1.1    simonb 	overflows = ch->ch_overflows;
   1313       1.1    simonb 	ch->ch_overflows = 0;
   1314       1.1    simonb 	floods = ch->ch_floods;
   1315       1.1    simonb 	ch->ch_floods = 0;
   1316       1.1    simonb 	ch->ch_errors = 0;
   1317       1.1    simonb 	splx(s);
   1318       1.1    simonb 
   1319       1.1    simonb 	log(LOG_WARNING, "%s: channel %d: %d fifo overflow%s, %d ibuf flood%s\n",
   1320      1.32      matt 	    device_xname(sc->sc_dev), ch->ch_num,
   1321       1.1    simonb 	    overflows, overflows == 1 ? "" : "s",
   1322       1.1    simonb 	    floods, floods == 1 ? "" : "s");
   1323       1.1    simonb }
   1324       1.1    simonb 
   1325       1.1    simonb integrate void
   1326       1.1    simonb sbscn_rxsoft(struct sbscn_channel *ch, struct tty *tp)
   1327       1.1    simonb {
   1328      1.13       jmc 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
   1329       1.1    simonb 	u_char *get, *end;
   1330       1.1    simonb 	u_int cc, scc;
   1331       1.1    simonb 	u_char sr;
   1332       1.1    simonb 	int code;
   1333       1.1    simonb 	int s;
   1334       1.1    simonb 
   1335       1.1    simonb 	end = ch->ch_ebuf;
   1336       1.1    simonb 	get = ch->ch_rbget;
   1337       1.1    simonb 	scc = cc = sbscn_rbuf_size - ch->ch_rbavail;
   1338       1.1    simonb 
   1339       1.1    simonb 	if (cc == sbscn_rbuf_size) {
   1340       1.1    simonb 		ch->ch_floods++;
   1341       1.1    simonb 		if (ch->ch_errors++ == 0)
   1342       1.1    simonb 			callout_reset(&ch->ch_diag_callout, 60 * hz,
   1343       1.1    simonb 			    sbscn_diag, ch);
   1344       1.1    simonb 	}
   1345       1.1    simonb 
   1346       1.1    simonb 	while (cc) {
   1347       1.1    simonb 		code = get[0];
   1348       1.1    simonb 		sr = get[1];
   1349       1.1    simonb 		if (ISSET(sr, 0xf0)) {
   1350       1.1    simonb 			if (ISSET(sr, 0x10)) {
   1351       1.1    simonb 				ch->ch_overflows++;
   1352       1.1    simonb 				if (ch->ch_errors++ == 0)
   1353       1.1    simonb 					callout_reset(&ch->ch_diag_callout,
   1354       1.1    simonb 					    60 * hz, sbscn_diag, ch);
   1355       1.1    simonb 			}
   1356       1.1    simonb 			if (ISSET(sr, 0xc0))
   1357       1.1    simonb 				SET(code, TTY_FE);
   1358       1.1    simonb 			if (ISSET(sr, 0x20))
   1359       1.1    simonb 				SET(code, TTY_PE);
   1360       1.1    simonb 		}
   1361       1.1    simonb 		if ((*rint)(code, tp) == -1) {
   1362       1.1    simonb 			/*
   1363       1.1    simonb 			 * The line discipline's buffer is out of space.
   1364       1.1    simonb 			 */
   1365       1.1    simonb 			if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
   1366       1.1    simonb 				/*
   1367       1.1    simonb 				 * We're either not using flow control, or the
   1368       1.1    simonb 				 * line discipline didn't tell us to block for
   1369       1.1    simonb 				 * some reason.  Either way, we have no way to
   1370       1.1    simonb 				 * know when there's more space available, so
   1371       1.1    simonb 				 * just drop the rest of the data.
   1372       1.1    simonb 				 */
   1373       1.1    simonb 				get += cc << 1;
   1374       1.1    simonb 				if (get >= end)
   1375       1.1    simonb 					get -= sbscn_rbuf_size << 1;
   1376       1.1    simonb 				cc = 0;
   1377       1.1    simonb 			} else {
   1378       1.1    simonb 				/*
   1379       1.1    simonb 				 * Don't schedule any more receive processing
   1380       1.1    simonb 				 * until the line discipline tells us there's
   1381       1.1    simonb 				 * space available (through comhwiflow()).
   1382       1.1    simonb 				 * Leave the rest of the data in the input
   1383       1.1    simonb 				 * buffer.
   1384       1.1    simonb 				 */
   1385       1.1    simonb 				SET(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
   1386       1.1    simonb 			}
   1387       1.1    simonb 			break;
   1388       1.1    simonb 		}
   1389       1.1    simonb 		get += 2;
   1390       1.1    simonb 		if (get >= end)
   1391       1.1    simonb 			get = ch->ch_rbuf;
   1392       1.1    simonb 		cc--;
   1393       1.1    simonb 	}
   1394       1.1    simonb 
   1395       1.1    simonb 	if (cc != scc) {
   1396       1.1    simonb 		ch->ch_rbget = get;
   1397       1.1    simonb 		s = splserial();
   1398       1.1    simonb 		cc = ch->ch_rbavail += scc - cc;
   1399       1.1    simonb 		/* Buffers should be ok again, release possible block. */
   1400       1.1    simonb 		if (cc >= ch->ch_r_lowat) {
   1401       1.1    simonb 			if (ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
   1402       1.1    simonb 				CLR(ch->ch_rx_flags, RX_IBUF_OVERFLOWED);
   1403       1.1    simonb 				SET(ch->ch_imr, 0x02);
   1404       1.1    simonb 				WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1405       1.1    simonb 			}
   1406       1.1    simonb 			if (ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED)) {
   1407       1.1    simonb 				CLR(ch->ch_rx_flags, RX_IBUF_BLOCKED);
   1408       1.1    simonb 				sbscn_dohwiflow(ch);
   1409       1.1    simonb 			}
   1410       1.1    simonb 		}
   1411       1.1    simonb 		splx(s);
   1412       1.1    simonb 	}
   1413       1.1    simonb }
   1414       1.1    simonb 
   1415       1.1    simonb integrate void
   1416       1.1    simonb sbscn_txsoft(struct sbscn_channel *ch, struct tty *tp)
   1417       1.1    simonb {
   1418       1.1    simonb 
   1419       1.1    simonb 	CLR(tp->t_state, TS_BUSY);
   1420       1.1    simonb 	if (ISSET(tp->t_state, TS_FLUSH))
   1421       1.1    simonb 		CLR(tp->t_state, TS_FLUSH);
   1422       1.1    simonb 	else
   1423       1.1    simonb 		ndflush(&tp->t_outq, (int)(ch->ch_tba - tp->t_outq.c_cf));
   1424       1.1    simonb 	(*tp->t_linesw->l_start)(tp);
   1425       1.1    simonb }
   1426       1.1    simonb 
   1427       1.1    simonb integrate void
   1428       1.1    simonb sbscn_stsoft(struct sbscn_channel *ch, struct tty *tp)
   1429       1.1    simonb {
   1430       1.1    simonb 	u_char iports, delta;
   1431       1.1    simonb 	int s;
   1432       1.1    simonb 
   1433       1.1    simonb 	s = splserial();
   1434       1.1    simonb 	iports = ch->ch_iports;
   1435       1.1    simonb 	delta = ch->ch_iports_delta;
   1436       1.1    simonb 	ch->ch_iports_delta = 0;
   1437       1.1    simonb 	splx(s);
   1438       1.1    simonb 
   1439       1.1    simonb 	if (ISSET(delta, ch->ch_i_dcd)) {
   1440       1.1    simonb 		/*
   1441       1.1    simonb 		 * Inform the tty layer that carrier detect changed.
   1442       1.1    simonb 		 */
   1443       1.1    simonb 		(void) (*tp->t_linesw->l_modem)(tp,
   1444       1.1    simonb 		    ISSET(iports, ch->ch_i_dcd));
   1445       1.1    simonb 	}
   1446       1.1    simonb 
   1447       1.1    simonb 	if (ISSET(delta, ch->ch_i_cts)) {
   1448       1.1    simonb 		/* Block or unblock output according to flow control. */
   1449       1.1    simonb 		if (ISSET(iports, ch->ch_i_cts)) {
   1450       1.1    simonb 			ch->ch_tx_stopped = 0;
   1451       1.1    simonb 			(*tp->t_linesw->l_start)(tp);
   1452       1.1    simonb 		} else {
   1453       1.1    simonb 			ch->ch_tx_stopped = 1;
   1454       1.1    simonb 		}
   1455       1.1    simonb 	}
   1456       1.1    simonb 
   1457       1.1    simonb #ifdef SBSCN_DEBUG
   1458       1.1    simonb 	if (sbscn_debug)
   1459       1.1    simonb 		sbscn_status(ch, "sbscn_stsoft");
   1460       1.1    simonb #endif
   1461       1.1    simonb }
   1462       1.1    simonb 
   1463       1.1    simonb void
   1464       1.1    simonb sbscn_soft(void *arg)
   1465       1.1    simonb {
   1466       1.1    simonb 	struct sbscn_channel *ch = arg;
   1467       1.1    simonb 	struct tty *tp = ch->ch_tty;
   1468       1.1    simonb 
   1469       1.1    simonb 	if (ch->ch_rx_ready) {
   1470       1.1    simonb 		ch->ch_rx_ready = 0;
   1471       1.1    simonb 		sbscn_rxsoft(ch, tp);
   1472       1.1    simonb 	}
   1473       1.1    simonb 
   1474       1.1    simonb 	if (ch->ch_st_check) {
   1475       1.1    simonb 		ch->ch_st_check = 0;
   1476       1.1    simonb 		sbscn_stsoft(ch, tp);
   1477       1.1    simonb 	}
   1478       1.1    simonb 
   1479       1.1    simonb 	if (ch->ch_tx_done) {
   1480       1.1    simonb 		ch->ch_tx_done = 0;
   1481       1.1    simonb 		sbscn_txsoft(ch, tp);
   1482       1.1    simonb 	}
   1483       1.1    simonb }
   1484       1.1    simonb 
   1485       1.1    simonb void
   1486      1.31      matt sbscn_intr(void *arg, uint32_t status, vaddr_t pc)
   1487       1.1    simonb {
   1488       1.1    simonb 	struct sbscn_channel *ch = arg;
   1489       1.1    simonb 	u_char *put, *end;
   1490       1.1    simonb 	u_int cc;
   1491       1.1    simonb 	u_char isr, sr;
   1492       1.1    simonb 
   1493       1.1    simonb 	/* read ISR */
   1494       1.1    simonb 	isr = READ_REG(ch->ch_isr_base) & ch->ch_imr;
   1495       1.1    simonb 	if (isr == 0)
   1496       1.1    simonb 		return;
   1497       1.1    simonb 
   1498       1.1    simonb 	end = ch->ch_ebuf;
   1499       1.1    simonb 	put = ch->ch_rbput;
   1500       1.1    simonb 	cc = ch->ch_rbavail;
   1501       1.1    simonb 
   1502       1.1    simonb 	do {
   1503       1.1    simonb 		u_char	iports, delta;
   1504       1.1    simonb 
   1505       1.1    simonb 		if (isr & 0x02) {
   1506       1.1    simonb 
   1507       1.1    simonb 			sr = READ_REG(ch->ch_base + 0x20);
   1508       1.1    simonb 			/* XXX sr 0x01 bit must be set at this point */
   1509       1.1    simonb 
   1510       1.1    simonb #if defined(DDB) || defined(KGDB)
   1511       1.1    simonb 			if ((sr & 0x80) == 0x80) {
   1512       1.1    simonb #ifdef DDB
   1513       1.1    simonb 				if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
   1514       1.1    simonb 					(void)READ_REG(ch->ch_base + 0x60);
   1515       1.1    simonb 					console_debugger();
   1516       1.1    simonb 					continue;
   1517       1.1    simonb 				}
   1518       1.1    simonb #endif
   1519       1.1    simonb #ifdef KGDB
   1520       1.1    simonb 				if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB)) {
   1521       1.1    simonb 					(void)READ_REG(ch->ch_base + 0x60);
   1522       1.1    simonb 					kgdb_connect(1);
   1523       1.1    simonb 					continue;
   1524       1.1    simonb 				}
   1525       1.1    simonb #endif
   1526       1.1    simonb 			}
   1527       1.1    simonb #endif /* DDB || KGDB */
   1528       1.1    simonb 
   1529       1.1    simonb 		    	if (!ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
   1530       1.1    simonb 				while (cc > 0) {
   1531       1.1    simonb 					put[0] = READ_REG(ch->ch_base + 0x60);
   1532       1.1    simonb 					put[1] = sr;
   1533       1.1    simonb 					put += 2;
   1534       1.1    simonb 					if (put >= end)
   1535       1.1    simonb 						put = ch->ch_rbuf;
   1536       1.1    simonb 					cc--;
   1537       1.1    simonb 
   1538       1.1    simonb 					sr = READ_REG(ch->ch_base + 0x20);
   1539       1.1    simonb 					if (!ISSET(sr, 0x02))
   1540       1.1    simonb 						break;
   1541       1.1    simonb 				}
   1542       1.1    simonb 
   1543       1.1    simonb 				/*
   1544       1.1    simonb 				 * Current string of incoming characters ended
   1545       1.1    simonb 				 * because no more data was available or we
   1546       1.1    simonb 				 * ran out of space.  Schedule a receive event
   1547       1.1    simonb 				 * if any data was received.  If we're out of
   1548       1.1    simonb 				 * space, turn off receive interrupts.
   1549       1.1    simonb 				 */
   1550       1.1    simonb 				ch->ch_rbput = put;
   1551       1.1    simonb 				ch->ch_rbavail = cc;
   1552       1.1    simonb 				if (!ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED))
   1553       1.1    simonb 					ch->ch_rx_ready = 1;
   1554       1.1    simonb 
   1555       1.1    simonb 				/*
   1556       1.1    simonb 				 * See if we are in danger of overflowing a
   1557       1.1    simonb 				 * buffer. If so, use hardware flow control
   1558       1.1    simonb 				 * to ease the pressure.
   1559       1.1    simonb 				 */
   1560       1.1    simonb 				if (!ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED) &&
   1561       1.1    simonb 				    cc < ch->ch_r_hiwat) {
   1562       1.1    simonb 					SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
   1563       1.1    simonb 					sbscn_dohwiflow(ch);
   1564       1.1    simonb 				}
   1565       1.1    simonb 
   1566       1.1    simonb 				/*
   1567       1.1    simonb 				 * If we're out of space, disable receive
   1568       1.1    simonb 				 * interrupts until the queue has drained
   1569       1.1    simonb 				 * a bit.
   1570       1.1    simonb 				 */
   1571       1.1    simonb 				if (!cc) {
   1572       1.1    simonb 					SET(ch->ch_rx_flags,
   1573       1.1    simonb 					    RX_IBUF_OVERFLOWED);
   1574       1.1    simonb 					CLR(ch->ch_imr, 0x02);
   1575       1.1    simonb 					WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1576       1.1    simonb 				}
   1577       1.1    simonb 			} else {
   1578       1.1    simonb 				/* XXX panic? */
   1579       1.1    simonb 				CLR(ch->ch_imr, 0x02);
   1580       1.1    simonb 				WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1581       1.1    simonb 				continue;
   1582       1.1    simonb 			}
   1583       1.1    simonb 		}
   1584       1.1    simonb 
   1585       1.1    simonb 		if (isr & 0x01) {
   1586       1.1    simonb 			CLR(ch->ch_imr, 0x01);
   1587       1.1    simonb 			WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1588       1.1    simonb 		}
   1589       1.1    simonb 
   1590       1.1    simonb #if 0
   1591       1.1    simonb XXX
   1592       1.1    simonb 		if (isr & 0x08) {
   1593       1.1    simonb 			clear input signal change!
   1594       1.1    simonb 		}
   1595       1.1    simonb #endif
   1596       1.1    simonb 		iports = GET_INPUT_SIGNALS(ch);
   1597       1.1    simonb 		delta = iports ^ ch->ch_iports;
   1598       1.1    simonb 		ch->ch_iports = iports;
   1599       1.1    simonb 
   1600       1.1    simonb 		/*
   1601       1.1    simonb 		 * Process normal status changes
   1602       1.1    simonb 		 */
   1603       1.1    simonb 		if (ISSET(delta, ch->ch_i_mask)) {
   1604       1.1    simonb 			SET(ch->ch_iports_delta, delta);
   1605       1.1    simonb 
   1606       1.1    simonb 			/*
   1607       1.1    simonb 			 * Stop output immediately if we lose the output
   1608       1.1    simonb 			 * flow control signal or carrier detect.
   1609       1.1    simonb 			 */
   1610       1.1    simonb 			if (ISSET(~iports, ch->ch_i_mask)) {
   1611       1.1    simonb 				ch->ch_tbc = 0;
   1612       1.1    simonb 				ch->ch_heldtbc = 0;
   1613       1.1    simonb #ifdef SBSCN_DEBUG
   1614       1.1    simonb 				if (sbscn_debug)
   1615       1.1    simonb 					sbscn_status(ch, "sbscn_intr  ");
   1616       1.1    simonb #endif
   1617       1.1    simonb 			}
   1618       1.1    simonb 
   1619       1.1    simonb 			ch->ch_st_check = 1;
   1620       1.1    simonb 		}
   1621       1.1    simonb 	} while ((isr = (READ_REG(ch->ch_isr_base) & ch->ch_imr)) != 0);
   1622       1.1    simonb 
   1623       1.1    simonb 	/*
   1624       1.1    simonb 	 * Done handling any receive interrupts and status changes, and
   1625       1.1    simonb 	 * clearing the tx-ready interrupt if it was set.  See if data can
   1626       1.1    simonb 	 * be transmitted as well. Schedule tx done event if no data left
   1627       1.1    simonb 	 * and tty was marked busy.
   1628       1.1    simonb 	 */
   1629       1.1    simonb 	sr = READ_REG(ch->ch_base + 0x20);
   1630       1.1    simonb 	if (ISSET(sr, 0x4)) {
   1631       1.1    simonb 
   1632       1.1    simonb 		/*
   1633       1.1    simonb 		 * If we've delayed a parameter change, do it now, and restart
   1634       1.1    simonb 		 * output.
   1635       1.1    simonb 		 */
   1636       1.1    simonb 		if (ch->ch_heldchange) {
   1637       1.1    simonb 			sbscn_loadchannelregs(ch);
   1638       1.1    simonb 			ch->ch_heldchange = 0;
   1639       1.1    simonb 			ch->ch_tbc = ch->ch_heldtbc;
   1640       1.1    simonb 			ch->ch_heldtbc = 0;
   1641       1.1    simonb 		}
   1642       1.1    simonb 
   1643       1.1    simonb 		/* Output the next chunk of the contiguous buffer, if any. */
   1644       1.1    simonb 		if (ch->ch_tbc > 0) {
   1645       1.1    simonb 			int wrote1;
   1646       1.1    simonb 			u_char c;
   1647       1.1    simonb 
   1648       1.1    simonb 			wrote1 = 0;
   1649       1.1    simonb 			while (ch->ch_tbc &&
   1650       1.1    simonb 			    READ_REG(ch->ch_base + 0x20) & 0x04) {
   1651       1.1    simonb 				wrote1 = 1;
   1652       1.1    simonb 				c = *ch->ch_tba++;
   1653       1.1    simonb 				ch->ch_tbc--;
   1654       1.1    simonb 				WRITE_REG(ch->ch_base + 0x70, c);
   1655       1.1    simonb 			}
   1656       1.1    simonb 			if (wrote1) {
   1657       1.1    simonb 				SET(ch->ch_imr, 0x01);
   1658       1.1    simonb 				WRITE_REG(ch->ch_imr_base, ch->ch_imr);
   1659       1.1    simonb 			}
   1660       1.1    simonb 		} else {
   1661       1.1    simonb 			/*
   1662       1.1    simonb 			 * transmit completion interrupts already disabled,
   1663       1.1    simonb 			 * mark the channel tx state as done.
   1664       1.1    simonb 			 */
   1665       1.1    simonb 			if (ch->ch_tx_busy) {
   1666       1.1    simonb 				ch->ch_tx_busy = 0;
   1667       1.1    simonb 				ch->ch_tx_done = 1;
   1668       1.1    simonb 			}
   1669       1.1    simonb 		}
   1670       1.1    simonb 	}
   1671       1.1    simonb 
   1672       1.1    simonb 	/* Wake up the poller. */
   1673      1.25        ad 	softint_schedule(ch->ch_si);
   1674       1.1    simonb 
   1675      1.37       tls #ifdef RND_SBSCN
   1676       1.1    simonb 	rnd_add_uint32(&ch->ch_rnd_source, isr | sr);
   1677       1.1    simonb #endif
   1678       1.1    simonb }
   1679       1.1    simonb 
   1680       1.1    simonb /*
   1681       1.1    simonb  * The following functions are polled getc and putc routines, shared
   1682       1.1    simonb  * by the console and kgdb glue.
   1683       1.1    simonb  */
   1684       1.1    simonb 
   1685       1.1    simonb int
   1686       1.1    simonb sbscn_common_getc(u_long addr, int chan)
   1687       1.1    simonb {
   1688       1.1    simonb 	int s = splhigh();
   1689       1.1    simonb 	u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
   1690       1.1    simonb 	int c;
   1691       1.1    simonb 
   1692       1.1    simonb 	/* block until a character becomes available */
   1693       1.1    simonb 	while ((READ_REG(base + 0x20) & 0x01) == 0)
   1694       1.1    simonb 		continue;
   1695       1.1    simonb 
   1696       1.1    simonb 	c = READ_REG(base + 0x60) & 0xff;
   1697       1.1    simonb 	splx(s);
   1698       1.1    simonb 	return (c);
   1699       1.1    simonb }
   1700       1.1    simonb 
   1701       1.1    simonb void
   1702       1.1    simonb sbscn_common_putc(u_long addr, int chan, int c)
   1703       1.1    simonb {
   1704       1.1    simonb 	int s = splhigh();
   1705       1.1    simonb 	int timo;
   1706       1.1    simonb 	u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
   1707       1.1    simonb 
   1708       1.1    simonb 	/* wait for any pending transmission to finish */
   1709       1.1    simonb 	timo = 1500000;
   1710       1.1    simonb 	while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo)
   1711       1.1    simonb 		continue;
   1712       1.1    simonb 
   1713       1.1    simonb 	WRITE_REG(base + 0x70, c);
   1714       1.1    simonb 
   1715       1.1    simonb 	/* wait for this transmission to complete */
   1716       1.1    simonb 	timo = 15000000;
   1717       1.1    simonb 	while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo)
   1718       1.1    simonb 		continue;
   1719       1.1    simonb 
   1720       1.1    simonb 	splx(s);
   1721       1.1    simonb }
   1722       1.1    simonb 
   1723       1.1    simonb /*
   1724       1.1    simonb  * Initialize UART for use as console or KGDB line.
   1725       1.1    simonb  */
   1726       1.1    simonb int
   1727       1.1    simonb sbscn_init(u_long addr, int chan, int rate, tcflag_t cflag)
   1728       1.1    simonb {
   1729       1.1    simonb #if 1
   1730       1.1    simonb 	u_long chanregbase = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
   1731       1.1    simonb 	u_long imaskreg = MIPS_PHYS_TO_KSEG1(addr + 0x230 + (chan * 0x20));
   1732       1.1    simonb 	u_char mode1, mode2;
   1733       1.1    simonb 	u_long brc;
   1734       1.1    simonb 	volatile int timo;
   1735       1.1    simonb 
   1736       1.1    simonb 	WRITE_REG(imaskreg, 0);			/* disable channel intrs */
   1737       1.1    simonb 
   1738       1.1    simonb 	/* XXX should we really do the following?  how about only if enabled? */
   1739       1.1    simonb 	/* wait for any pending transmission to finish */
   1740       1.1    simonb 	timo = 1500000;
   1741       1.1    simonb 	while ((READ_REG(chanregbase + 0x20) & 0x08) == 0 && --timo)
   1742       1.1    simonb 		continue;
   1743       1.1    simonb 
   1744       1.1    simonb 	/* XXX: wait a little.  THIS SHOULD NOT BE NECESSARY!!! (?) */
   1745       1.1    simonb 	timo = 1500000;
   1746       1.1    simonb 	while (--timo)
   1747       1.1    simonb 		;
   1748       1.1    simonb 
   1749       1.1    simonb 	WRITE_REG(chanregbase + 0x50, 2 << 4);	/* reset receiver */
   1750       1.1    simonb 	WRITE_REG(chanregbase + 0x50, 3 << 4);	/* reset transmitter */
   1751       1.1    simonb 
   1752       1.1    simonb 	/* set up the line for use */
   1753       1.1    simonb 	(void)cflag2modes(cflag, &mode1, &mode2);
   1754       1.1    simonb 	(void)sbscn_speed(rate, &brc);
   1755       1.1    simonb 	WRITE_REG(chanregbase + 0x00, mode1);
   1756       1.1    simonb 	WRITE_REG(chanregbase + 0x10, mode2);
   1757       1.1    simonb 	WRITE_REG(chanregbase + 0x30, brc);
   1758       1.1    simonb 
   1759       1.1    simonb 	/* enable transmit and receive */
   1760       1.1    simonb #define	M_DUART_RX_EN			0x01
   1761       1.1    simonb #define	M_DUART_TX_EN			0x04
   1762       1.1    simonb 	WRITE_REG(chanregbase + 0x50,M_DUART_RX_EN | M_DUART_TX_EN);
   1763       1.1    simonb #endif
   1764       1.1    simonb 
   1765       1.1    simonb 	/* XXX: wait a little.  THIS SHOULD NOT BE NECESSARY!!! (?) */
   1766       1.1    simonb 	timo = 1500000;
   1767       1.1    simonb 	while (--timo)
   1768       1.1    simonb 		;
   1769       1.1    simonb 
   1770       1.1    simonb #if 0 /* XXXCGD */
   1771       1.1    simonb 	bus_space_handle_t ioh;
   1772       1.1    simonb 
   1773       1.1    simonb 	if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
   1774       1.1    simonb 		return (ENOMEM); /* ??? */
   1775       1.1    simonb 
   1776       1.1    simonb 	bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
   1777       1.1    simonb 	bus_space_write_1(iot, ioh, com_efr, 0);
   1778       1.1    simonb 	bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
   1779       1.1    simonb 	rate = comspeed(rate, frequency);
   1780       1.1    simonb 	bus_space_write_1(iot, ioh, com_dlbl, rate);
   1781       1.1    simonb 	bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
   1782       1.1    simonb 	bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
   1783       1.1    simonb 	bus_space_write_1(iot, ioh, com_mcr, MCR_DTR | MCR_RTS);
   1784       1.1    simonb 	bus_space_write_1(iot, ioh, com_fifo,
   1785       1.1    simonb 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
   1786       1.1    simonb 	bus_space_write_1(iot, ioh, com_ier, 0);
   1787       1.1    simonb 
   1788       1.1    simonb 	*iohp = ioh;
   1789       1.1    simonb 	return (0);
   1790       1.1    simonb #endif /* XXXCGD */
   1791       1.1    simonb 	/* XXXCGD */
   1792       1.1    simonb 	return (0);
   1793       1.1    simonb }
   1794       1.1    simonb 
   1795       1.1    simonb /*
   1796       1.1    simonb  * Following are all routines needed for sbscn to act as console
   1797       1.1    simonb  */
   1798       1.1    simonb int
   1799       1.1    simonb sbscn_cnattach(u_long addr, int chan, int rate, tcflag_t cflag)
   1800       1.1    simonb {
   1801       1.1    simonb 	int res;
   1802       1.1    simonb 	static struct consdev sbscn_cons = {
   1803       1.1    simonb 		NULL, NULL, sbscn_cngetc, sbscn_cnputc, sbscn_cnpollc, NULL,
   1804       1.8        he 		    NULL, NULL, NODEV, CN_NORMAL
   1805       1.1    simonb 	};
   1806       1.1    simonb 
   1807       1.1    simonb 	res = sbscn_init(addr, chan, rate, cflag);
   1808       1.1    simonb 	if (res)
   1809       1.1    simonb 		return (res);
   1810       1.1    simonb 
   1811       1.1    simonb 	cn_tab = &sbscn_cons;
   1812       1.1    simonb 
   1813       1.1    simonb 	sbscn_cons_present = 1;
   1814       1.1    simonb 	sbscn_cons_addr = addr;
   1815       1.1    simonb 	sbscn_cons_chan = chan;
   1816       1.1    simonb 	sbscn_cons_rate = rate;
   1817       1.1    simonb 	sbscn_cons_cflag = cflag;
   1818       1.1    simonb 
   1819       1.1    simonb 	return (0);
   1820       1.1    simonb }
   1821       1.1    simonb 
   1822       1.1    simonb int
   1823       1.1    simonb sbscn_cngetc(dev_t dev)
   1824       1.1    simonb {
   1825       1.1    simonb 
   1826       1.1    simonb 	return (sbscn_common_getc(sbscn_cons_addr, sbscn_cons_chan));
   1827       1.1    simonb }
   1828       1.1    simonb 
   1829       1.1    simonb /*
   1830       1.1    simonb  * Console kernel output character routine.
   1831       1.1    simonb  */
   1832       1.1    simonb void
   1833       1.1    simonb sbscn_cnputc(dev_t dev, int c)
   1834       1.1    simonb {
   1835       1.1    simonb 
   1836       1.1    simonb 	sbscn_common_putc(sbscn_cons_addr, sbscn_cons_chan, c);
   1837       1.1    simonb }
   1838       1.1    simonb 
   1839       1.1    simonb void
   1840       1.1    simonb sbscn_cnpollc(dev_t dev, int on)
   1841       1.1    simonb {
   1842       1.1    simonb 
   1843       1.1    simonb }
   1844       1.1    simonb 
   1845       1.1    simonb #ifdef KGDB
   1846       1.1    simonb int
   1847       1.1    simonb sbscn_kgdb_attach(u_long addr, int chan, int rate, tcflag_t cflag)
   1848       1.1    simonb {
   1849       1.1    simonb 	int res;
   1850       1.1    simonb 
   1851       1.1    simonb 	if (!sbscn_cons_present &&
   1852       1.1    simonb 	    sbscn_cons_addr == addr && sbscn_cons_chan == chan)
   1853       1.1    simonb 		return (EBUSY); /* cannot share with console */
   1854       1.1    simonb 
   1855       1.1    simonb 	res = sbscn_init(addr, chan, rate, cflag);
   1856       1.1    simonb 	if (res)
   1857       1.1    simonb 		return (res);
   1858       1.1    simonb 
   1859       1.1    simonb 	kgdb_attach(sbscn_kgdb_getc, sbscn_kgdb_putc, NULL);
   1860       1.1    simonb 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   1861       1.1    simonb 
   1862       1.1    simonb 	sbscn_kgdb_present = 1;
   1863       1.1    simonb 	sbscn_kgdb_addr = addr;
   1864       1.1    simonb 	sbscn_kgdb_chan = chan;
   1865       1.1    simonb 
   1866       1.1    simonb 	return (0);
   1867       1.1    simonb }
   1868       1.1    simonb 
   1869       1.1    simonb /* ARGSUSED */
   1870       1.1    simonb int
   1871      1.28       dsl sbscn_kgdb_getc(void *arg)
   1872       1.1    simonb {
   1873       1.1    simonb 
   1874       1.1    simonb 	return (sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan));
   1875       1.1    simonb }
   1876       1.1    simonb 
   1877       1.1    simonb /* ARGSUSED */
   1878       1.1    simonb void
   1879      1.28       dsl sbscn_kgdb_putc(void *arg, int c)
   1880       1.1    simonb {
   1881       1.1    simonb 
   1882       1.1    simonb 	sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan, c);
   1883       1.1    simonb }
   1884       1.1    simonb #endif /* KGDB */
   1885       1.1    simonb 
   1886       1.1    simonb /*
   1887       1.1    simonb  * helper function to identify the sbscn channels used by
   1888       1.1    simonb  * console or KGDB (and not yet autoconf attached)
   1889       1.1    simonb  */
   1890       1.1    simonb int
   1891       1.1    simonb sbscn_is_console(u_long addr, int chan)
   1892       1.1    simonb {
   1893       1.1    simonb 
   1894       1.1    simonb 	if (sbscn_cons_present && !sbscn_cons_attached &&
   1895       1.1    simonb 	    sbscn_cons_addr == addr && sbscn_cons_chan == chan)
   1896       1.1    simonb 		return (1);
   1897       1.1    simonb #ifdef KGDB
   1898       1.1    simonb 	if (sbscn_kgdb_present && !sbscn_kgdb_attached &&
   1899       1.1    simonb 	    sbscn_kgdb_addr == addr && sbscn_kgdb_chan == chan)
   1900       1.1    simonb 		return (1);
   1901       1.1    simonb #endif
   1902       1.1    simonb 	return (0);
   1903       1.1    simonb }
   1904