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