Home | History | Annotate | Line # | Download | only in tx
txcom.c revision 1.45
      1  1.45     rmind /*	$NetBSD: txcom.c,v 1.45 2011/04/24 16:26:56 rmind Exp $ */
      2   1.1       uch 
      3   1.9       uch /*-
      4  1.24       uch  * Copyright (c) 1999, 2000, 2004 The NetBSD Foundation, Inc.
      5   1.1       uch  * All rights reserved.
      6   1.1       uch  *
      7   1.9       uch  * This code is derived from software contributed to The NetBSD Foundation
      8   1.9       uch  * by UCHIYAMA Yasushi.
      9   1.9       uch  *
     10   1.1       uch  * Redistribution and use in source and binary forms, with or without
     11   1.1       uch  * modification, are permitted provided that the following conditions
     12   1.1       uch  * are met:
     13   1.1       uch  * 1. Redistributions of source code must retain the above copyright
     14   1.1       uch  *    notice, this list of conditions and the following disclaimer.
     15   1.9       uch  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.9       uch  *    notice, this list of conditions and the following disclaimer in the
     17   1.9       uch  *    documentation and/or other materials provided with the distribution.
     18   1.1       uch  *
     19   1.9       uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.9       uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.9       uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.9       uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.9       uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.9       uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.9       uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.9       uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.9       uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.9       uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.9       uch  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1       uch  */
     31  1.22     lukem 
     32  1.22     lukem #include <sys/cdefs.h>
     33  1.45     rmind __KERNEL_RCSID(0, "$NetBSD: txcom.c,v 1.45 2011/04/24 16:26:56 rmind Exp $");
     34  1.15       uch 
     35  1.15       uch #include "opt_tx39uart_debug.h"
     36   1.1       uch 
     37   1.1       uch #include <sys/param.h>
     38   1.1       uch #include <sys/systm.h>
     39   1.3       uch #include <sys/kernel.h>
     40   1.1       uch #include <sys/device.h>
     41   1.3       uch #include <sys/malloc.h>
     42  1.30      elad #include <sys/kauth.h>
     43   1.1       uch 
     44   1.1       uch #include <sys/proc.h> /* tsleep/wakeup */
     45   1.1       uch 
     46   1.1       uch #include <sys/ioctl.h>
     47   1.1       uch #include <sys/select.h>
     48   1.1       uch #include <sys/file.h>
     49   1.1       uch 
     50   1.1       uch #include <sys/tty.h>
     51   1.1       uch #include <sys/conf.h>
     52   1.1       uch #include <dev/cons.h> /* consdev */
     53   1.1       uch 
     54   1.1       uch #include <machine/bus.h>
     55   1.9       uch #include <machine/config_hook.h>
     56   1.1       uch 
     57   1.1       uch #include <hpcmips/tx/tx39var.h>
     58   1.1       uch #include <hpcmips/tx/tx39icureg.h>
     59   1.1       uch #include <hpcmips/tx/tx39uartvar.h>
     60   1.1       uch #include <hpcmips/tx/tx39uartreg.h>
     61   1.1       uch 
     62   1.5       uch #include <hpcmips/tx/tx39irvar.h>
     63   1.5       uch 
     64   1.1       uch #include <hpcmips/tx/tx39clockreg.h> /* XXX */
     65   1.6       uch 
     66   1.9       uch /*
     67   1.9       uch  * UARTA channel has DTR, DSR, RTS, CTS lines. and they  wired to MFIO/IO port.
     68   1.9       uch  */
     69   1.9       uch #define IS_COM0(s)	((s) == 0)
     70   1.9       uch #define IS_COM1(s)	((s) == 1)
     71   1.9       uch #define ON		((void *)1)
     72   1.9       uch #define OFF		((void *)0)
     73   1.1       uch 
     74  1.15       uch #ifdef	TX39UART_DEBUG
     75  1.15       uch #define DPRINTF_ENABLE
     76  1.15       uch #define DPRINTF_DEBUG	tx39uart_debug
     77   1.1       uch #endif
     78  1.15       uch #include <machine/debug.h>
     79   1.1       uch 
     80   1.3       uch #define TXCOM_HW_CONSOLE	0x40
     81   1.3       uch #define	TXCOM_RING_SIZE		256 /* must be a power of two! */
     82   1.3       uch #define TXCOM_RING_MASK		(TXCOM_RING_SIZE - 1)
     83   1.1       uch 
     84   1.3       uch struct txcom_chip {
     85   1.1       uch 	tx_chipset_tag_t sc_tc;
     86   1.1       uch 	int sc_slot;	/* UARTA or UARTB */
     87   1.1       uch 	int sc_cflag;
     88   1.1       uch 	int sc_speed;
     89   1.3       uch 	int sc_swflags;
     90   1.1       uch 	int sc_hwflags;
     91   1.6       uch 
     92   1.6       uch 	int sc_dcd;
     93   1.9       uch 	int sc_msr_cts;
     94   1.9       uch 	int sc_tx_stopped;
     95   1.3       uch };
     96   1.1       uch 
     97   1.3       uch struct txcom_softc {
     98   1.3       uch 	struct	device		sc_dev;
     99   1.3       uch 	struct tty		*sc_tty;
    100   1.3       uch 	struct txcom_chip	*sc_chip;
    101   1.3       uch 
    102  1.44   tsutsui 	void		*sc_txsoft_cookie;
    103  1.44   tsutsui 	void		*sc_rxsoft_cookie;
    104   1.8   thorpej 
    105   1.3       uch  	u_int8_t	*sc_tba;	/* transmit buffer address */
    106   1.3       uch  	int		sc_tbc;		/* transmit byte count */
    107   1.3       uch 	int		sc_heldtbc;
    108   1.3       uch 	u_int8_t	*sc_rbuf;	/* receive buffer address */
    109   1.3       uch 	int		sc_rbput;	/* receive byte count */
    110   1.3       uch 	int		sc_rbget;
    111   1.1       uch };
    112   1.1       uch 
    113   1.1       uch extern struct cfdriver txcom_cd;
    114   1.1       uch 
    115   1.9       uch int	txcom_match(struct device *, struct cfdata *, void *);
    116   1.9       uch void	txcom_attach(struct device *, struct device *, void *);
    117   1.9       uch int	txcom_print(void*, const char *);
    118   1.9       uch 
    119   1.9       uch int	txcom_txintr(void *);
    120   1.9       uch int	txcom_rxintr(void *);
    121   1.9       uch int	txcom_frameerr_intr(void *);
    122   1.9       uch int	txcom_parityerr_intr(void *);
    123   1.9       uch int	txcom_break_intr(void *);
    124   1.3       uch 
    125   1.9       uch void	txcom_rxsoft(void *);
    126   1.9       uch void	txcom_txsoft(void *);
    127   1.3       uch 
    128   1.9       uch int	txcom_stsoft(void *);
    129   1.9       uch int	txcom_stsoft2(void *);
    130   1.9       uch int	txcom_stsoft3(void *);
    131   1.9       uch int	txcom_stsoft4(void *);
    132   1.9       uch 
    133   1.9       uch 
    134   1.9       uch void	txcom_shutdown(struct txcom_softc *);
    135   1.9       uch void	txcom_break(struct txcom_softc *, int);
    136   1.9       uch void	txcom_modem(struct txcom_softc *, int);
    137   1.9       uch void	txcomstart(struct tty *);
    138   1.9       uch int	txcomparam(struct tty *, struct termios *);
    139   1.9       uch 
    140   1.9       uch void	txcom_reset	(struct txcom_chip *);
    141  1.35   thorpej int	txcom_enable	(struct txcom_chip *, bool);
    142   1.9       uch void	txcom_disable	(struct txcom_chip *);
    143   1.9       uch void	txcom_setmode	(struct txcom_chip *);
    144   1.9       uch void	txcom_setbaudrate(struct txcom_chip *);
    145   1.9       uch int	txcom_cngetc	(dev_t);
    146   1.9       uch void	txcom_cnputc	(dev_t, int);
    147   1.9       uch void	txcom_cnpollc	(dev_t, int);
    148   1.3       uch 
    149   1.9       uch int	txcom_dcd_hook(void *, int, long, void *);
    150   1.9       uch int	txcom_cts_hook(void *, int, long, void *);
    151   1.1       uch 
    152   1.9       uch 
    153  1.27     perry inline int	__txcom_txbufready(struct txcom_chip *, int);
    154   1.9       uch const char *__txcom_slotname(int);
    155   1.9       uch 
    156   1.9       uch #ifdef TX39UARTDEBUG
    157   1.9       uch void	txcom_dump(struct txcom_chip *);
    158   1.9       uch #endif
    159   1.6       uch 
    160   1.3       uch struct consdev txcomcons = {
    161  1.21  nakayama 	NULL, NULL, txcom_cngetc, txcom_cnputc, txcom_cnpollc, NULL, NULL,
    162  1.14       uch 	NULL, NODEV, CN_NORMAL
    163   1.3       uch };
    164   1.3       uch 
    165   1.1       uch /* Serial console */
    166   1.3       uch struct txcom_chip txcom_chip;
    167   1.1       uch 
    168  1.19   thorpej CFATTACH_DECL(txcom, sizeof(struct txcom_softc),
    169  1.19   thorpej     txcom_match, txcom_attach, NULL, NULL);
    170   1.1       uch 
    171  1.17   gehenna dev_type_open(txcomopen);
    172  1.17   gehenna dev_type_close(txcomclose);
    173  1.17   gehenna dev_type_read(txcomread);
    174  1.17   gehenna dev_type_write(txcomwrite);
    175  1.17   gehenna dev_type_ioctl(txcomioctl);
    176  1.17   gehenna dev_type_stop(txcomstop);
    177  1.17   gehenna dev_type_tty(txcomtty);
    178  1.17   gehenna dev_type_poll(txcompoll);
    179  1.17   gehenna 
    180  1.17   gehenna const struct cdevsw txcom_cdevsw = {
    181  1.17   gehenna 	txcomopen, txcomclose, txcomread, txcomwrite, txcomioctl,
    182  1.20  jdolecek 	txcomstop, txcomtty, txcompoll, nommap, ttykqfilter, D_TTY
    183  1.17   gehenna };
    184  1.17   gehenna 
    185   1.1       uch int
    186  1.41       dsl txcom_match(struct device *parent, struct cfdata *cf, void *aux)
    187   1.1       uch {
    188   1.1       uch 	/* if the autoconfiguration got this far, there's a slot here */
    189   1.1       uch 	return 1;
    190   1.1       uch }
    191   1.1       uch 
    192   1.1       uch void
    193   1.9       uch txcom_attach(struct device *parent, struct device *self, void *aux)
    194   1.1       uch {
    195   1.1       uch 	struct tx39uart_attach_args *ua = aux;
    196   1.1       uch 	struct txcom_softc *sc = (void*)self;
    197   1.1       uch 	tx_chipset_tag_t tc;
    198   1.1       uch 	struct tty *tp;
    199   1.3       uch 	struct txcom_chip *chip;
    200   1.6       uch 	int slot, console;
    201   1.1       uch 
    202   1.1       uch 	/* Check this slot used as serial console */
    203   1.6       uch 	console = (ua->ua_slot == txcom_chip.sc_slot) &&
    204  1.14       uch 	    (txcom_chip.sc_hwflags & TXCOM_HW_CONSOLE);
    205   1.6       uch 
    206   1.6       uch 	if (console) {
    207   1.3       uch 		sc->sc_chip = &txcom_chip;
    208   1.3       uch 	} else {
    209   1.3       uch 		if (!(sc->sc_chip = malloc(sizeof(struct txcom_chip),
    210  1.14       uch 		    M_DEVBUF, M_WAITOK))) {
    211   1.3       uch 			printf(": can't allocate chip\n");
    212   1.3       uch 			return;
    213   1.3       uch 		}
    214   1.3       uch 		memset(sc->sc_chip, 0, sizeof(struct txcom_chip));
    215   1.1       uch 	}
    216   1.1       uch 
    217   1.3       uch 	chip = sc->sc_chip;
    218   1.3       uch 	tc = chip->sc_tc = ua->ua_tc;
    219   1.3       uch 	slot = chip->sc_slot = ua->ua_slot;
    220   1.3       uch 
    221   1.6       uch #ifdef TX39UARTDEBUG
    222   1.6       uch 	txcom_dump(chip);
    223   1.6       uch #endif
    224   1.6       uch 	if (!console)
    225   1.6       uch 		txcom_reset(chip);
    226   1.6       uch 
    227   1.3       uch 	if (!(sc->sc_rbuf = malloc(TXCOM_RING_SIZE, M_DEVBUF, M_WAITOK))) {
    228   1.3       uch 		printf(": can't allocate buffer.\n");
    229   1.3       uch 		return;
    230   1.3       uch 	}
    231   1.3       uch 	memset(sc->sc_rbuf, 0, TXCOM_RING_SIZE);
    232   1.1       uch 
    233  1.45     rmind 	tp = tty_alloc();
    234   1.1       uch 	tp->t_oproc = txcomstart;
    235   1.1       uch 	tp->t_param = txcomparam;
    236   1.1       uch 	tp->t_hwiflow = NULL;
    237   1.1       uch 	sc->sc_tty = tp;
    238   1.1       uch 	tty_attach(tp);
    239   1.1       uch 
    240   1.3       uch 	if (ISSET(chip->sc_hwflags, TXCOM_HW_CONSOLE)) {
    241   1.1       uch 		int maj;
    242   1.1       uch 		/* locate the major number */
    243  1.17   gehenna 		maj = cdevsw_lookup_major(&txcom_cdevsw);
    244   1.1       uch 
    245  1.29   thorpej 		cn_tab->cn_dev = makedev(maj, device_unit(&sc->sc_dev));
    246   1.1       uch 
    247   1.3       uch 		printf(": console");
    248   1.1       uch 	}
    249   1.1       uch 
    250   1.3       uch 	printf("\n");
    251   1.1       uch 
    252   1.1       uch 	/*
    253   1.1       uch 	 * Enable interrupt
    254   1.1       uch 	 */
    255   1.3       uch #define TXCOMINTR(i, s) MAKEINTR(2, TX39_INTRSTATUS2_UART##i##INT(s))
    256   1.3       uch 
    257   1.3       uch 	tx_intr_establish(tc, TXCOMINTR(RX, slot), IST_EDGE, IPL_TTY,
    258  1.14       uch 	    txcom_rxintr, sc);
    259   1.3       uch 	tx_intr_establish(tc, TXCOMINTR(TX, slot), IST_EDGE, IPL_TTY,
    260  1.14       uch 	    txcom_txintr, sc);
    261   1.3       uch 	tx_intr_establish(tc, TXCOMINTR(RXOVERRUN, slot), IST_EDGE, IPL_TTY,
    262  1.14       uch 	    txcom_rxintr, sc);
    263   1.3       uch 	tx_intr_establish(tc, TXCOMINTR(TXOVERRUN, slot), IST_EDGE, IPL_TTY,
    264  1.14       uch 	    txcom_txintr, sc);
    265   1.3       uch 	tx_intr_establish(tc, TXCOMINTR(FRAMEERR, slot), IST_EDGE, IPL_TTY,
    266  1.14       uch 	    txcom_frameerr_intr, sc);
    267   1.3       uch 	tx_intr_establish(tc, TXCOMINTR(PARITYERR, slot), IST_EDGE, IPL_TTY,
    268  1.14       uch 	    txcom_parityerr_intr, sc);
    269   1.3       uch 	tx_intr_establish(tc, TXCOMINTR(BREAK, slot), IST_EDGE, IPL_TTY,
    270  1.14       uch 	    txcom_break_intr, sc);
    271   1.5       uch 
    272  1.44   tsutsui 	sc->sc_txsoft_cookie =
    273  1.44   tsutsui 	    softint_establish(SOFTINT_SERIAL, txcom_txsoft, sc);
    274  1.44   tsutsui 	sc->sc_rxsoft_cookie =
    275  1.44   tsutsui 	    softint_establish(SOFTINT_SERIAL, txcom_rxsoft, sc);
    276  1.44   tsutsui 
    277   1.9       uch 	/*
    278   1.9       uch 	 * UARTA has external signal line. (its wiring is platform dependent)
    279   1.9       uch 	 */
    280   1.9       uch 	if (IS_COM0(slot)) {
    281   1.9       uch 		/* install DCD, CTS hooks. */
    282  1.11      sato 		config_hook(CONFIG_HOOK_EVENT, CONFIG_HOOK_COM0_DCD,
    283  1.14       uch 		    CONFIG_HOOK_EXCLUSIVE, txcom_dcd_hook, sc);
    284  1.11      sato 		config_hook(CONFIG_HOOK_EVENT, CONFIG_HOOK_COM0_CTS,
    285  1.14       uch 		    CONFIG_HOOK_EXCLUSIVE, txcom_cts_hook, sc);
    286   1.9       uch 	}
    287   1.6       uch 
    288   1.5       uch 	/*
    289   1.5       uch 	 * UARTB can connect IR module
    290   1.5       uch 	 */
    291   1.9       uch 	if (IS_COM1(slot)) {
    292   1.5       uch 		struct txcom_attach_args tca;
    293   1.5       uch 		tca.tca_tc = tc;
    294   1.5       uch 		tca.tca_parent = self;
    295   1.5       uch 		config_found(self, &tca, txcom_print);
    296   1.5       uch 	}
    297   1.5       uch }
    298   1.5       uch 
    299   1.5       uch int
    300   1.9       uch txcom_print(void *aux, const char *pnp)
    301   1.5       uch {
    302   1.5       uch 	return pnp ? QUIET : UNCONF;
    303   1.1       uch }
    304   1.1       uch 
    305   1.6       uch void
    306   1.9       uch txcom_reset(struct txcom_chip *chip)
    307   1.6       uch {
    308   1.6       uch 	tx_chipset_tag_t tc;
    309   1.6       uch 	int slot, ofs;
    310   1.6       uch 	txreg_t reg;
    311   1.6       uch 
    312   1.6       uch 	tc = chip->sc_tc;
    313   1.6       uch 	slot = chip->sc_slot;
    314   1.6       uch 	ofs = TX39_UARTCTRL1_REG(slot);
    315   1.6       uch 
    316   1.6       uch 	/* Supply clock */
    317   1.6       uch 	reg = tx_conf_read(tc, TX39_CLOCKCTRL_REG);
    318   1.6       uch 	reg |= (slot ? TX39_CLOCK_ENUARTBCLK : TX39_CLOCK_ENUARTACLK);
    319   1.6       uch 	tx_conf_write(tc, TX39_CLOCKCTRL_REG, reg);
    320   1.6       uch 
    321   1.6       uch 	/* reset UART module */
    322   1.6       uch 	tx_conf_write(tc, ofs, 0);
    323   1.6       uch }
    324   1.6       uch 
    325   1.1       uch int
    326  1.35   thorpej txcom_enable(struct txcom_chip *chip, bool console)
    327   1.1       uch {
    328   1.1       uch 	tx_chipset_tag_t tc;
    329   1.1       uch 	txreg_t reg;
    330   1.3       uch 	int slot, ofs, timeout;
    331   1.1       uch 
    332   1.3       uch 	tc = chip->sc_tc;
    333   1.3       uch 	slot = chip->sc_slot;
    334   1.3       uch 	ofs = TX39_UARTCTRL1_REG(slot);
    335   1.1       uch 
    336  1.24       uch 	/*
    337  1.24       uch 	 * External power supply (if any)
    338  1.24       uch 	 * When serial console, Windows CE already powered on it.
    339  1.24       uch 	 */
    340  1.24       uch 	if (!console) {
    341  1.24       uch 		config_hook_call(CONFIG_HOOK_POWERCONTROL,
    342  1.24       uch 		    CONFIG_HOOK_POWERCONTROL_COM0, PWCTL_ON);
    343  1.24       uch 		delay(3);
    344  1.24       uch 	}
    345   1.9       uch 
    346   1.6       uch 	/* Supply clock */
    347   1.5       uch 	reg = tx_conf_read(tc, TX39_CLOCKCTRL_REG);
    348   1.5       uch 	reg |= (slot ? TX39_CLOCK_ENUARTBCLK : TX39_CLOCK_ENUARTACLK);
    349   1.5       uch 	tx_conf_write(tc, TX39_CLOCKCTRL_REG, reg);
    350   1.5       uch 
    351   1.6       uch 	/*
    352   1.6       uch 	 * XXX Disable DMA (DMA not coded yet)
    353   1.6       uch 	 */
    354   1.6       uch 	reg = tx_conf_read(tc, ofs);
    355   1.6       uch 	reg &= ~(TX39_UARTCTRL1_ENDMARX | TX39_UARTCTRL1_ENDMATX);
    356   1.6       uch 	tx_conf_write(tc, ofs, reg);
    357   1.6       uch 
    358   1.6       uch 	/* enable */
    359   1.3       uch 	reg = tx_conf_read(tc, ofs);
    360   1.1       uch 	reg |= TX39_UARTCTRL1_ENUART;
    361   1.1       uch 	reg &= ~TX39_UARTCTRL1_ENBREAHALT;
    362   1.3       uch 	tx_conf_write(tc, ofs, reg);
    363   1.3       uch 
    364   1.9       uch 	timeout = 100000;
    365   1.3       uch 
    366   1.3       uch 	while(!(tx_conf_read(tc, ofs) & TX39_UARTCTRL1_UARTON) &&
    367  1.14       uch 	    --timeout > 0)
    368   1.3       uch 		;
    369   1.3       uch 
    370   1.5       uch 	if (timeout == 0 && !cold) {
    371   1.6       uch 		printf("%s never power up\n", __txcom_slotname(slot));
    372   1.3       uch 		return 1;
    373   1.3       uch 	}
    374   1.3       uch 
    375   1.1       uch 	return 0;
    376   1.1       uch }
    377   1.1       uch 
    378   1.1       uch void
    379   1.9       uch txcom_disable(struct txcom_chip *chip)
    380   1.1       uch {
    381   1.1       uch 	tx_chipset_tag_t tc;
    382   1.1       uch 	txreg_t reg;
    383   1.1       uch 	int slot;
    384   1.1       uch 
    385   1.3       uch 	tc = chip->sc_tc;
    386   1.3       uch 	slot = chip->sc_slot;
    387   1.1       uch 
    388   1.1       uch 	reg = tx_conf_read(tc, TX39_UARTCTRL1_REG(slot));
    389   1.1       uch 	/* DMA */
    390   1.1       uch 	reg &= ~(TX39_UARTCTRL1_ENDMARX | TX39_UARTCTRL1_ENDMATX);
    391   1.3       uch 
    392   1.6       uch 	/* disable module */
    393   1.1       uch 	reg &= ~TX39_UARTCTRL1_ENUART;
    394   1.1       uch 	tx_conf_write(tc, TX39_UARTCTRL1_REG(slot), reg);
    395   1.3       uch 
    396   1.1       uch 	/* Clock */
    397   1.1       uch 	reg = tx_conf_read(tc, TX39_CLOCKCTRL_REG);
    398   1.1       uch 	reg &= ~(slot ? TX39_CLOCK_ENUARTBCLK : TX39_CLOCK_ENUARTACLK);
    399   1.1       uch 	tx_conf_write(tc, TX39_CLOCKCTRL_REG, reg);
    400   1.1       uch 
    401   1.1       uch }
    402   1.1       uch 
    403  1.27     perry inline int
    404   1.9       uch __txcom_txbufready(struct txcom_chip *chip, int retry)
    405   1.3       uch {
    406   1.3       uch 	tx_chipset_tag_t tc = chip->sc_tc;
    407   1.3       uch 	int ofs = TX39_UARTCTRL1_REG(chip->sc_slot);
    408   1.3       uch 
    409   1.3       uch 	do {
    410   1.3       uch 		if (tx_conf_read(tc, ofs) & TX39_UARTCTRL1_EMPTY)
    411   1.3       uch 			return 1;
    412   1.3       uch 	} while(--retry != 0);
    413   1.3       uch 
    414   1.1       uch 	return 0;
    415   1.1       uch }
    416   1.1       uch 
    417   1.5       uch void
    418   1.9       uch txcom_pulse_mode(struct device *dev)
    419   1.5       uch {
    420   1.5       uch 	struct txcom_softc *sc = (void*)dev;
    421   1.5       uch 	struct txcom_chip *chip = sc->sc_chip;
    422   1.5       uch 	tx_chipset_tag_t tc = chip->sc_tc;
    423   1.5       uch 	int ofs;
    424   1.5       uch 	txreg_t reg;
    425   1.5       uch 
    426   1.5       uch 	ofs = TX39_UARTCTRL1_REG(chip->sc_slot);
    427   1.5       uch 
    428   1.5       uch 	reg = tx_conf_read(tc, ofs);
    429   1.6       uch 	/* WindowsCE use this setting */
    430   1.6       uch 	reg |= TX39_UARTCTRL1_PULSEOPT1;
    431   1.6       uch 	reg &= ~TX39_UARTCTRL1_PULSEOPT2;
    432   1.6       uch 	reg |= TX39_UARTCTRL1_DTINVERT;
    433   1.6       uch 
    434   1.5       uch 	tx_conf_write(tc, ofs, reg);
    435   1.5       uch }
    436   1.5       uch 
    437   1.3       uch /*
    438   1.3       uch  * console
    439   1.3       uch  */
    440   1.1       uch int
    441   1.9       uch txcom_cngetc(dev_t dev)
    442   1.1       uch {
    443   1.1       uch 	tx_chipset_tag_t tc;
    444   1.2       uch 	int ofs, c, s;
    445   1.2       uch 
    446   1.3       uch 	s = spltty();
    447   1.2       uch 
    448   1.3       uch 	tc = txcom_chip.sc_tc;
    449   1.3       uch 	ofs = TX39_UARTCTRL1_REG(txcom_chip.sc_slot);
    450   1.1       uch 
    451   1.1       uch 	while(!(TX39_UARTCTRL1_RXHOLDFULL & tx_conf_read(tc, ofs)))
    452   1.1       uch 		;
    453   1.2       uch 
    454   1.3       uch 	c = TX39_UARTRXHOLD_RXDATA(
    455   1.3       uch 		tx_conf_read(tc, TX39_UARTRXHOLD_REG(txcom_chip.sc_slot)));
    456   1.2       uch 
    457   1.3       uch 	if (c == '\r')
    458   1.1       uch 		c = '\n';
    459   1.1       uch 
    460   1.2       uch 	splx(s);
    461   1.2       uch 
    462   1.1       uch 	return c;
    463   1.1       uch }
    464   1.1       uch 
    465   1.1       uch void
    466   1.9       uch txcom_cnputc(dev_t dev, int c)
    467   1.1       uch {
    468   1.3       uch 	struct txcom_chip *chip = &txcom_chip;
    469   1.3       uch 	tx_chipset_tag_t tc = chip->sc_tc;
    470   1.3       uch 	int s;
    471   1.2       uch 
    472   1.3       uch 	s = spltty();
    473   1.1       uch 
    474   1.3       uch 	/* Wait for transmitter to empty */
    475   1.3       uch 	__txcom_txbufready(chip, -1);
    476   1.1       uch 
    477   1.3       uch 	tx_conf_write(tc, TX39_UARTTXHOLD_REG(chip->sc_slot),
    478  1.14       uch 	    (c & TX39_UARTTXHOLD_TXDATA_MASK));
    479   1.1       uch 
    480   1.3       uch 	__txcom_txbufready(chip, -1);
    481   1.3       uch 
    482   1.2       uch 	splx(s);
    483   1.1       uch }
    484   1.1       uch 
    485   1.1       uch void
    486   1.9       uch txcom_cnpollc(dev_t dev, int on)
    487   1.1       uch {
    488   1.1       uch }
    489   1.1       uch 
    490   1.1       uch void
    491   1.9       uch txcom_setmode(struct txcom_chip *chip)
    492   1.1       uch {
    493   1.3       uch 	tcflag_t cflag = chip->sc_cflag;
    494   1.3       uch 	int ofs = TX39_UARTCTRL1_REG(chip->sc_slot);
    495   1.1       uch 	txreg_t reg;
    496   1.1       uch 
    497   1.3       uch 	reg = tx_conf_read(chip->sc_tc, ofs);
    498   1.6       uch 	reg &= ~TX39_UARTCTRL1_ENUART;
    499   1.6       uch 	tx_conf_write(chip->sc_tc, ofs, reg);
    500   1.6       uch 
    501   1.1       uch 	switch (ISSET(cflag, CSIZE)) {
    502   1.1       uch 	default:
    503   1.1       uch 		printf("txcom_setmode: CS7, CS8 only. use CS7");
    504   1.1       uch 		/* FALL THROUGH */
    505   1.1       uch 	case CS7:
    506   1.1       uch 		reg |= TX39_UARTCTRL1_BIT7;
    507   1.1       uch 		break;
    508   1.1       uch 	case CS8:
    509   1.1       uch 		reg &= ~TX39_UARTCTRL1_BIT7;
    510   1.1       uch 		break;
    511   1.1       uch 	}
    512   1.3       uch 
    513   1.1       uch 	if (ISSET(cflag, PARENB)) {
    514   1.1       uch 		reg |= TX39_UARTCTRL1_ENPARITY;
    515   1.1       uch 		if (ISSET(cflag, PARODD)) {
    516   1.1       uch 			reg &= ~TX39_UARTCTRL1_EVENPARITY;
    517   1.1       uch 		} else {
    518   1.1       uch 			reg |= TX39_UARTCTRL1_EVENPARITY;
    519   1.1       uch 		}
    520   1.1       uch 	} else {
    521   1.1       uch 		reg &= ~TX39_UARTCTRL1_ENPARITY;
    522   1.1       uch 	}
    523   1.3       uch 
    524   1.6       uch 	if (ISSET(cflag, CSTOPB))
    525   1.1       uch 		reg |= TX39_UARTCTRL1_TWOSTOP;
    526   1.6       uch 	else
    527   1.6       uch 		reg &= ~TX39_UARTCTRL1_TWOSTOP;
    528   1.6       uch 
    529   1.6       uch 	reg |= TX39_UARTCTRL1_ENUART;
    530   1.3       uch 	tx_conf_write(chip->sc_tc, ofs, reg);
    531   1.3       uch }
    532   1.3       uch 
    533   1.3       uch void
    534   1.9       uch txcom_setbaudrate(struct txcom_chip *chip)
    535   1.3       uch {
    536   1.3       uch 	int baudrate;
    537   1.6       uch 	int ofs = TX39_UARTCTRL1_REG(chip->sc_slot);
    538   1.6       uch 	txreg_t reg, reg1;
    539   1.3       uch 
    540   1.3       uch 	if (chip->sc_speed == 0)
    541   1.3       uch 		return;
    542   1.3       uch 
    543   1.5       uch 	if (!cold)
    544  1.15       uch 		DPRINTF("%d\n", chip->sc_speed);
    545   1.5       uch 
    546   1.6       uch 	reg1 = tx_conf_read(chip->sc_tc, ofs);
    547   1.6       uch 	reg1 &= ~TX39_UARTCTRL1_ENUART;
    548   1.6       uch 	tx_conf_write(chip->sc_tc, ofs, reg1);
    549   1.6       uch 
    550   1.3       uch 	baudrate = TX39_UARTCLOCKHZ / (chip->sc_speed * 16) - 1;
    551   1.3       uch 	reg = TX39_UARTCTRL2_BAUDRATE_SET(0, baudrate);
    552   1.3       uch 
    553   1.3       uch 	tx_conf_write(chip->sc_tc, TX39_UARTCTRL2_REG(chip->sc_slot), reg);
    554   1.6       uch 
    555   1.6       uch 	reg1 |= TX39_UARTCTRL1_ENUART;
    556   1.6       uch 	tx_conf_write(chip->sc_tc, ofs, reg1);
    557   1.3       uch }
    558   1.3       uch 
    559   1.3       uch int
    560   1.9       uch txcom_cnattach(int slot, int speed, int cflag)
    561   1.3       uch {
    562   1.3       uch 	cn_tab = &txcomcons;
    563   1.3       uch 
    564   1.3       uch 	txcom_chip.sc_tc	= tx_conf_get_tag();
    565   1.3       uch 	txcom_chip.sc_slot	= slot;
    566   1.3       uch 	txcom_chip.sc_cflag	= cflag;
    567   1.3       uch 	txcom_chip.sc_speed	= speed;
    568   1.3       uch 	txcom_chip.sc_hwflags |= TXCOM_HW_CONSOLE;
    569   1.6       uch #if notyet
    570   1.6       uch 	txcom_reset(&txcom_chip);
    571   1.6       uch #endif
    572   1.6       uch 	txcom_setmode(&txcom_chip);
    573   1.6       uch 	txcom_setbaudrate(&txcom_chip);
    574   1.3       uch 
    575  1.36   thorpej 	if (txcom_enable(&txcom_chip, true) != 0)
    576   1.5       uch 		return 1;
    577   1.5       uch 
    578   1.3       uch 	return 0;
    579   1.3       uch }
    580   1.3       uch 
    581   1.3       uch /*
    582   1.3       uch  * tty
    583   1.3       uch  */
    584   1.3       uch void
    585   1.9       uch txcom_break(struct txcom_softc *sc, int on)
    586   1.3       uch {
    587   1.3       uch 	struct txcom_chip *chip = sc->sc_chip;
    588   1.1       uch 
    589   1.3       uch 	tx_conf_write(chip->sc_tc, TX39_UARTTXHOLD_REG(chip->sc_slot),
    590  1.14       uch 	    on ? TX39_UARTTXHOLD_BREAK : 0);
    591   1.1       uch }
    592   1.1       uch 
    593   1.1       uch void
    594   1.9       uch txcom_modem(struct txcom_softc *sc, int on)
    595   1.1       uch {
    596   1.3       uch 	struct txcom_chip *chip = sc->sc_chip;
    597   1.3       uch 	tx_chipset_tag_t tc = chip->sc_tc;
    598   1.3       uch 	int slot = chip->sc_slot;
    599   1.1       uch 	txreg_t reg;
    600   1.1       uch 
    601   1.9       uch 	/* assert DTR */
    602   1.9       uch 	if (IS_COM0(slot)) {
    603  1.11      sato 		config_hook_call(CONFIG_HOOK_SET,
    604  1.14       uch 		    CONFIG_HOOK_COM0_DTR,
    605  1.14       uch 		    (void *)on);
    606   1.9       uch 	}
    607   1.9       uch 
    608   1.3       uch 	reg = tx_conf_read(tc, TX39_UARTCTRL1_REG(slot));
    609   1.6       uch 	reg &= ~TX39_UARTCTRL1_ENUART;
    610   1.6       uch 	tx_conf_write(tc, TX39_UARTCTRL1_REG(slot), reg);
    611   1.3       uch 
    612   1.3       uch 	if (on) {
    613   1.3       uch 		reg &= ~TX39_UARTCTRL1_DISTXD;
    614   1.3       uch 	} else {
    615   1.6       uch 		reg |= TX39_UARTCTRL1_DISTXD; /* low UARTTXD */
    616   1.3       uch 	}
    617   1.6       uch 
    618   1.6       uch 	reg |= TX39_UARTCTRL1_ENUART;
    619   1.6       uch 	tx_conf_write(tc, TX39_UARTCTRL1_REG(slot), reg);
    620   1.1       uch }
    621   1.1       uch 
    622   1.1       uch void
    623   1.9       uch txcom_shutdown(struct txcom_softc *sc)
    624   1.1       uch {
    625   1.1       uch 	struct tty *tp = sc->sc_tty;
    626   1.3       uch 	int s = spltty();
    627   1.1       uch 
    628   1.3       uch 	/* Clear any break condition set with TIOCSBRK. */
    629   1.3       uch 	txcom_break(sc, 0);
    630   1.3       uch 
    631   1.3       uch 	/*
    632   1.3       uch 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    633   1.3       uch 	 * notice even if we immediately open the port again.
    634   1.3       uch 	 */
    635   1.3       uch 	if (ISSET(tp->t_cflag, HUPCL)) {
    636   1.3       uch 		txcom_modem(sc, 0);
    637   1.3       uch 		(void) tsleep(sc, TTIPRI, ttclos, hz);
    638   1.3       uch 	}
    639   1.3       uch 
    640   1.3       uch 
    641   1.3       uch 	/* Turn off interrupts if not the console. */
    642   1.3       uch 	if (!ISSET(sc->sc_chip->sc_hwflags, TXCOM_HW_CONSOLE)) {
    643   1.3       uch 		txcom_disable(sc->sc_chip);
    644   1.3       uch 	}
    645   1.1       uch 
    646   1.3       uch 	splx(s);
    647   1.3       uch }
    648   1.1       uch 
    649   1.9       uch const char *
    650   1.9       uch __txcom_slotname(int slot)
    651   1.3       uch {
    652   1.9       uch 	static const char *slotname[] = {"UARTA", "UARTB", "unknown"};
    653   1.9       uch 
    654   1.9       uch 	if (slot != 0 && slot != 1)
    655   1.9       uch 		return slotname[2];
    656   1.9       uch 
    657   1.9       uch 	return slotname[slot];
    658   1.2       uch }
    659   1.2       uch 
    660   1.2       uch int
    661   1.9       uch txcom_frameerr_intr(void *arg)
    662   1.3       uch {
    663   1.3       uch 	struct txcom_softc *sc = arg;
    664   1.3       uch 
    665   1.3       uch 	printf("%s frame error\n", __txcom_slotname(sc->sc_chip->sc_slot));
    666   1.3       uch 
    667   1.3       uch 	return 0;
    668   1.3       uch }
    669   1.3       uch 
    670   1.3       uch int
    671   1.9       uch txcom_parityerr_intr(void *arg)
    672   1.3       uch {
    673   1.3       uch 	struct txcom_softc *sc = arg;
    674   1.3       uch 
    675   1.3       uch 	printf("%s parity error\n", __txcom_slotname(sc->sc_chip->sc_slot));
    676   1.2       uch 
    677   1.2       uch 	return 0;
    678   1.1       uch }
    679   1.1       uch 
    680   1.1       uch int
    681   1.9       uch txcom_break_intr(void *arg)
    682   1.1       uch {
    683   1.1       uch 	struct txcom_softc *sc = arg;
    684   1.3       uch 
    685   1.3       uch 	printf("%s break\n", __txcom_slotname(sc->sc_chip->sc_slot));
    686   1.3       uch 
    687   1.3       uch 	return 0;
    688   1.3       uch }
    689   1.3       uch 
    690   1.3       uch int
    691   1.9       uch txcom_rxintr(void *arg)
    692   1.3       uch {
    693   1.3       uch 	struct txcom_softc *sc = arg;
    694   1.3       uch 	struct txcom_chip *chip = sc->sc_chip;
    695   1.1       uch 	u_int8_t c;
    696   1.1       uch 
    697   1.3       uch 	c = TX39_UARTRXHOLD_RXDATA(
    698   1.3       uch 		tx_conf_read(chip->sc_tc,
    699  1.14       uch 		    TX39_UARTRXHOLD_REG(chip->sc_slot)));
    700   1.3       uch 
    701   1.3       uch 	sc->sc_rbuf[sc->sc_rbput] = c;
    702   1.3       uch 	sc->sc_rbput = (sc->sc_rbput + 1) % TXCOM_RING_MASK;
    703   1.3       uch 
    704  1.44   tsutsui 	softint_schedule(sc->sc_rxsoft_cookie);
    705   1.1       uch 
    706   1.1       uch 	return 0;
    707   1.1       uch }
    708   1.1       uch 
    709   1.3       uch void
    710   1.9       uch txcom_rxsoft(void *arg)
    711   1.3       uch {
    712   1.3       uch 	struct txcom_softc *sc = arg;
    713   1.3       uch 	struct tty *tp = sc->sc_tty;
    714  1.14       uch 	int (*rint)(int, struct tty *);
    715   1.3       uch 	int code;
    716   1.3       uch 	int s, end, get;
    717   1.3       uch 
    718  1.10       eeh 	rint = tp->t_linesw->l_rint;
    719   1.3       uch 
    720   1.3       uch 	s = spltty();
    721   1.3       uch 	end = sc->sc_rbput;
    722   1.3       uch 	get = sc->sc_rbget;
    723   1.3       uch 
    724   1.3       uch 	while (get != end) {
    725   1.3       uch 		code = sc->sc_rbuf[get];
    726   1.3       uch 
    727   1.3       uch 		if ((*rint)(code, tp) == -1) {
    728   1.3       uch 			/*
    729   1.3       uch 			 * The line discipline's buffer is out of space.
    730   1.3       uch 			 */
    731   1.3       uch 		}
    732   1.3       uch 		get = (get + 1) % TXCOM_RING_MASK;
    733   1.3       uch 	}
    734   1.3       uch 	sc->sc_rbget = get;
    735   1.3       uch 
    736   1.3       uch 	splx(s);
    737   1.3       uch }
    738   1.3       uch 
    739   1.1       uch int
    740   1.9       uch txcom_txintr(void *arg)
    741   1.1       uch {
    742   1.1       uch 	struct txcom_softc *sc = arg;
    743   1.3       uch 	struct txcom_chip *chip = sc->sc_chip;
    744   1.3       uch 	tx_chipset_tag_t tc = chip->sc_tc;
    745   1.1       uch 
    746   1.3       uch 	if (sc->sc_tbc > 0) {
    747   1.3       uch 		tx_conf_write(tc, TX39_UARTTXHOLD_REG(chip->sc_slot),
    748  1.14       uch 		    (*sc->sc_tba &
    749  1.14       uch 			TX39_UARTTXHOLD_TXDATA_MASK));
    750   1.3       uch 		sc->sc_tbc--;
    751   1.3       uch 		sc->sc_tba++;
    752   1.3       uch 	} else {
    753  1.44   tsutsui 		softint_schedule(sc->sc_txsoft_cookie);
    754   1.3       uch 	}
    755   1.1       uch 
    756   1.1       uch 	return 0;
    757   1.1       uch }
    758   1.1       uch 
    759   1.3       uch void
    760   1.9       uch txcom_txsoft(void *arg)
    761   1.3       uch {
    762   1.3       uch 	struct txcom_softc *sc = arg;
    763   1.3       uch 	struct tty *tp = sc->sc_tty;
    764   1.3       uch 	int s = spltty();
    765   1.3       uch 
    766   1.3       uch 	CLR(tp->t_state, TS_BUSY);
    767   1.3       uch 	if (ISSET(tp->t_state, TS_FLUSH)) {
    768   1.3       uch 		CLR(tp->t_state, TS_FLUSH);
    769   1.3       uch 	} else {
    770   1.3       uch 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
    771   1.3       uch 	}
    772   1.3       uch 
    773  1.10       eeh 	(*tp->t_linesw->l_start)(tp);
    774   1.3       uch 
    775   1.3       uch 	splx(s);
    776   1.3       uch }
    777   1.1       uch 
    778   1.1       uch int
    779  1.26  christos txcomopen(dev_t dev, int flag, int mode, struct lwp *l)
    780   1.1       uch {
    781  1.40   tsutsui 	struct txcom_softc *sc;
    782   1.3       uch 	struct txcom_chip *chip;
    783   1.3       uch 	struct tty *tp;
    784  1.23      shin 	int s, err = ENXIO;
    785   1.1       uch 
    786  1.40   tsutsui 	sc = device_lookup_private(&txcom_cd, minor(dev));
    787  1.40   tsutsui 	if (sc == NULL)
    788  1.23      shin 		return err;
    789   1.3       uch 
    790   1.3       uch 	chip = sc->sc_chip;
    791   1.3       uch 	tp = sc->sc_tty;
    792   1.3       uch 
    793  1.32      elad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    794   1.3       uch 		return (EBUSY);
    795   1.3       uch 
    796   1.3       uch 	s = spltty();
    797   1.3       uch 
    798  1.36   thorpej 	if (txcom_enable(sc->sc_chip, false)) {
    799   1.6       uch 		splx(s);
    800   1.5       uch 		goto out;
    801   1.6       uch 	}
    802   1.5       uch 	/*
    803   1.5       uch 	 * Do the following iff this is a first open.
    804   1.5       uch 	 */
    805   1.5       uch 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    806   1.5       uch 		struct termios t;
    807   1.5       uch 
    808   1.5       uch 		tp->t_dev = dev;
    809   1.1       uch 
    810   1.5       uch 		t.c_ispeed = 0;
    811   1.5       uch 		if (ISSET(chip->sc_hwflags, TXCOM_HW_CONSOLE)) {
    812   1.5       uch 			t.c_ospeed = chip->sc_speed;
    813   1.5       uch 			t.c_cflag = chip->sc_cflag;
    814   1.5       uch 		} else {
    815   1.5       uch 			t.c_ospeed = TTYDEF_SPEED;
    816   1.5       uch 			t.c_cflag = TTYDEF_CFLAG;
    817   1.5       uch 		}
    818   1.3       uch 
    819   1.5       uch 		if (ISSET(chip->sc_swflags, TIOCFLAG_CLOCAL))
    820   1.5       uch 			SET(t.c_cflag, CLOCAL);
    821   1.5       uch 		if (ISSET(chip->sc_swflags, TIOCFLAG_CRTSCTS))
    822   1.5       uch 			SET(t.c_cflag, CRTSCTS);
    823   1.5       uch 		if (ISSET(chip->sc_swflags, TIOCFLAG_MDMBUF))
    824   1.5       uch 			SET(t.c_cflag, MDMBUF);
    825   1.5       uch 
    826   1.5       uch 		/* Make sure txcomparam() will do something. */
    827   1.5       uch 		tp->t_ospeed = 0;
    828   1.5       uch 		txcomparam(tp, &t);
    829   1.5       uch 
    830   1.5       uch 		tp->t_iflag = TTYDEF_IFLAG;
    831   1.5       uch 		tp->t_oflag = TTYDEF_OFLAG;
    832   1.5       uch 		tp->t_lflag = TTYDEF_LFLAG;
    833   1.1       uch 
    834   1.5       uch 		ttychars(tp);
    835   1.5       uch 		ttsetwater(tp);
    836   1.3       uch 
    837   1.5       uch 		/*
    838   1.5       uch 		 * Turn on DTR.  We must always do this, even if carrier is not
    839   1.5       uch 		 * present, because otherwise we'd have to use TIOCSDTR
    840   1.5       uch 		 * immediately after setting CLOCAL, which applications do not
    841   1.5       uch 		 * expect.  We always assert DTR while the device is open
    842   1.5       uch 		 * unless explicitly requested to deassert it.
    843   1.5       uch 		 */
    844   1.5       uch 		txcom_modem(sc, 1);
    845   1.3       uch 
    846   1.5       uch 		/* Clear the input ring, and unblock. */
    847   1.5       uch 		sc->sc_rbget = sc->sc_rbput = 0;
    848   1.5       uch 	}
    849   1.3       uch 
    850   1.3       uch 	splx(s);
    851   1.6       uch #define	TXCOMDIALOUT(x)	(minor(x) & 0x80000)
    852   1.6       uch 	if ((err = ttyopen(tp, TXCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK)))) {
    853  1.15       uch 		DPRINTF("ttyopen failed\n");
    854   1.3       uch 		goto out;
    855   1.1       uch 	}
    856  1.10       eeh 	if ((err = (*tp->t_linesw->l_open)(dev, tp))) {
    857  1.15       uch 		DPRINTF("line dicipline open failed\n");
    858   1.3       uch 		goto out;
    859   1.3       uch 	}
    860   1.3       uch 
    861   1.3       uch 	return err;
    862   1.3       uch 
    863   1.3       uch  out:
    864   1.3       uch 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    865   1.3       uch 		/*
    866   1.3       uch 		 * We failed to open the device, and nobody else had it opened.
    867   1.3       uch 		 * Clean up the state as appropriate.
    868   1.3       uch 		 */
    869   1.3       uch 		txcom_shutdown(sc);
    870   1.1       uch 	}
    871   1.1       uch 
    872   1.1       uch 	return err;
    873   1.3       uch 
    874   1.1       uch }
    875   1.1       uch 
    876   1.1       uch int
    877  1.26  christos txcomclose(dev_t dev, int flag, int mode, struct lwp *l)
    878   1.1       uch {
    879  1.40   tsutsui 	struct txcom_softc *sc = device_lookup_private(&txcom_cd, minor(dev));
    880   1.1       uch 	struct tty *tp = sc->sc_tty;
    881   1.1       uch 
    882   1.3       uch 	/* XXX This is for cons.c. */
    883   1.3       uch 	if (!ISSET(tp->t_state, TS_ISOPEN))
    884   1.3       uch 		return 0;
    885   1.3       uch 
    886  1.10       eeh 	(*tp->t_linesw->l_close)(tp, flag);
    887   1.1       uch 	ttyclose(tp);
    888   1.1       uch 
    889   1.3       uch 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    890   1.3       uch 		/*
    891   1.3       uch 		 * Although we got a last close, the device may still be in
    892   1.3       uch 		 * use; e.g. if this was the dialout node, and there are still
    893   1.3       uch 		 * processes waiting for carrier on the non-dialout node.
    894   1.3       uch 		 */
    895   1.3       uch 		txcom_shutdown(sc);
    896   1.3       uch 	}
    897   1.3       uch 
    898   1.1       uch 	return 0;
    899   1.1       uch }
    900   1.1       uch 
    901   1.1       uch int
    902   1.9       uch txcomread(dev_t dev, struct uio *uio, int flag)
    903   1.1       uch {
    904  1.40   tsutsui 	struct txcom_softc *sc = device_lookup_private(&txcom_cd, minor(dev));
    905   1.1       uch 	struct tty *tp = sc->sc_tty;
    906   1.3       uch 
    907  1.10       eeh 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    908   1.1       uch }
    909   1.1       uch 
    910   1.1       uch int
    911   1.9       uch txcomwrite(dev_t dev, struct uio *uio, int flag)
    912   1.1       uch {
    913  1.40   tsutsui 	struct txcom_softc *sc = device_lookup_private(&txcom_cd, minor(dev));
    914   1.1       uch 	struct tty *tp = sc->sc_tty;
    915   1.1       uch 
    916  1.10       eeh 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    917  1.12       scw }
    918  1.12       scw 
    919  1.12       scw int
    920  1.26  christos txcompoll(dev_t dev, int events, struct lwp *l)
    921  1.12       scw {
    922  1.40   tsutsui 	struct txcom_softc *sc = device_lookup_private(&txcom_cd, minor(dev));
    923  1.12       scw 	struct tty *tp = sc->sc_tty;
    924  1.12       scw 
    925  1.26  christos 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    926   1.1       uch }
    927   1.1       uch 
    928   1.1       uch struct tty *
    929   1.9       uch txcomtty(dev_t dev)
    930   1.1       uch {
    931  1.40   tsutsui 	struct txcom_softc *sc = device_lookup_private(&txcom_cd, minor(dev));
    932   1.3       uch 
    933   1.3       uch 	return sc->sc_tty;
    934   1.1       uch }
    935   1.1       uch 
    936   1.1       uch int
    937  1.37  christos txcomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    938   1.1       uch {
    939  1.40   tsutsui 	struct txcom_softc *sc = device_lookup_private(&txcom_cd, minor(dev));
    940   1.1       uch 	struct tty *tp = sc->sc_tty;
    941   1.3       uch 	int s, err;
    942   1.3       uch 
    943  1.26  christos 	err = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    944  1.16    atatat 	if (err != EPASSTHROUGH) {
    945   1.3       uch 		return err;
    946   1.3       uch 	}
    947   1.3       uch 
    948  1.26  christos 	err = ttioctl(tp, cmd, data, flag, l);
    949  1.16    atatat 	if (err != EPASSTHROUGH) {
    950   1.3       uch 		return err;
    951   1.3       uch 	}
    952   1.3       uch 
    953   1.3       uch 	err = 0;
    954   1.3       uch 
    955   1.3       uch 	s = spltty();
    956   1.3       uch 
    957   1.3       uch 	switch (cmd) {
    958   1.5       uch 	default:
    959  1.16    atatat 		err = EPASSTHROUGH;
    960   1.5       uch 		break;
    961   1.5       uch 
    962   1.3       uch 	case TIOCSBRK:
    963   1.3       uch 		txcom_break(sc, 1);
    964   1.3       uch 		break;
    965   1.3       uch 
    966   1.3       uch 	case TIOCCBRK:
    967   1.3       uch 		txcom_break(sc, 0);
    968   1.3       uch 		break;
    969   1.3       uch 
    970   1.3       uch 	case TIOCSDTR:
    971   1.3       uch 		txcom_modem(sc, 1);
    972   1.3       uch 		break;
    973   1.3       uch 
    974   1.3       uch 	case TIOCCDTR:
    975   1.3       uch 		txcom_modem(sc, 0);
    976   1.3       uch 		break;
    977   1.3       uch 
    978   1.3       uch 	case TIOCGFLAGS:
    979   1.3       uch 		*(int *)data = sc->sc_chip->sc_swflags;
    980   1.3       uch 		break;
    981   1.3       uch 
    982   1.3       uch 	case TIOCSFLAGS:
    983  1.34      elad 		err = kauth_authorize_device_tty(l->l_cred,
    984  1.34      elad 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    985   1.3       uch 		if (err) {
    986   1.3       uch 			break;
    987   1.3       uch 		}
    988   1.3       uch 		sc->sc_chip->sc_swflags = *(int *)data;
    989   1.3       uch 		break;
    990   1.3       uch 
    991   1.3       uch 	}
    992   1.1       uch 
    993   1.3       uch 	splx(s);
    994   1.1       uch 
    995   1.3       uch 	return err;
    996   1.1       uch }
    997   1.1       uch 
    998   1.1       uch void
    999   1.9       uch txcomstop(struct tty *tp, int flag)
   1000   1.1       uch {
   1001  1.40   tsutsui 	struct txcom_softc *sc;
   1002   1.1       uch 	int s;
   1003   1.1       uch 
   1004  1.40   tsutsui 	sc = device_lookup_private(&txcom_cd, minor(tp->t_dev));
   1005  1.40   tsutsui 
   1006   1.1       uch 	s = spltty();
   1007   1.1       uch 
   1008   1.1       uch 	if (ISSET(tp->t_state, TS_BUSY)) {
   1009   1.1       uch 		/* Stop transmitting at the next chunk. */
   1010   1.1       uch 		sc->sc_tbc = 0;
   1011   1.1       uch 		sc->sc_heldtbc = 0;
   1012   1.1       uch 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1013   1.1       uch 			SET(tp->t_state, TS_FLUSH);
   1014   1.1       uch 	}
   1015   1.3       uch 
   1016   1.1       uch 	splx(s);
   1017   1.1       uch }
   1018   1.1       uch 
   1019   1.1       uch void
   1020   1.9       uch txcomstart(struct tty *tp)
   1021   1.1       uch {
   1022  1.40   tsutsui 	struct txcom_softc *sc;
   1023  1.40   tsutsui 	struct txcom_chip *chip;
   1024  1.40   tsutsui 	tx_chipset_tag_t tc;
   1025  1.40   tsutsui 	int slot;
   1026   1.1       uch 	int s;
   1027   1.1       uch 
   1028  1.40   tsutsui 	sc = device_lookup_private(&txcom_cd, minor(tp->t_dev));
   1029  1.40   tsutsui 	chip = sc->sc_chip;
   1030  1.40   tsutsui 	tc = chip->sc_tc;
   1031  1.40   tsutsui 	slot = chip->sc_slot;
   1032  1.40   tsutsui 
   1033   1.1       uch 	s = spltty();
   1034   1.3       uch 
   1035   1.3       uch 	if (!__txcom_txbufready(chip, 0) ||
   1036   1.3       uch 	    ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1037   1.3       uch 		goto out;
   1038   1.1       uch 
   1039  1.38        ad 	if (!ttypull(tp))
   1040  1.38        ad 		goto out;
   1041   1.3       uch 
   1042   1.1       uch 	sc->sc_tba = tp->t_outq.c_cf;
   1043   1.1       uch 	sc->sc_tbc = ndqb(&tp->t_outq, 0);
   1044   1.3       uch 	SET(tp->t_state, TS_BUSY);
   1045   1.3       uch 
   1046   1.3       uch 	/* Output the first character of the contiguous buffer. */
   1047   1.3       uch 	tx_conf_write(tc, TX39_UARTTXHOLD_REG(slot),
   1048  1.14       uch 	    (*sc->sc_tba & TX39_UARTTXHOLD_TXDATA_MASK));
   1049   1.3       uch 
   1050   1.3       uch 	sc->sc_tbc--;
   1051   1.3       uch 	sc->sc_tba++;
   1052   1.1       uch 
   1053   1.3       uch  out:
   1054   1.1       uch 	splx(s);
   1055   1.1       uch }
   1056   1.1       uch 
   1057   1.3       uch /*
   1058   1.3       uch  * Set TXcom tty parameters from termios.
   1059   1.3       uch  */
   1060   1.1       uch int
   1061   1.9       uch txcomparam(struct tty *tp, struct termios *t)
   1062   1.1       uch {
   1063  1.40   tsutsui 	struct txcom_softc *sc;
   1064   1.3       uch 	struct txcom_chip *chip;
   1065   1.5       uch 	int ospeed;
   1066   1.3       uch 	int s;
   1067   1.3       uch 
   1068  1.40   tsutsui 	sc = device_lookup_private(&txcom_cd, minor(tp->t_dev));
   1069  1.40   tsutsui 	if (sc == NULL)
   1070   1.3       uch 		return ENXIO;
   1071   1.3       uch 
   1072   1.3       uch 	ospeed = t->c_ospeed;
   1073   1.3       uch 
   1074   1.3       uch 	/* Check requested parameters. */
   1075   1.3       uch 	if (ospeed < 0) {
   1076   1.3       uch 		return EINVAL;
   1077   1.3       uch 	}
   1078   1.3       uch 	if (t->c_ispeed && t->c_ispeed != ospeed) {
   1079   1.3       uch 		return EINVAL;
   1080   1.3       uch 	}
   1081   1.3       uch 
   1082   1.3       uch 	s = spltty();
   1083   1.3       uch 	chip = sc->sc_chip;
   1084   1.3       uch 	/*
   1085   1.3       uch 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1086   1.3       uch 	 * is always active.
   1087   1.3       uch 	 */
   1088   1.3       uch 	if (ISSET(chip->sc_swflags, TIOCFLAG_SOFTCAR) ||
   1089   1.3       uch 	    ISSET(chip->sc_hwflags, TXCOM_HW_CONSOLE)) {
   1090   1.5       uch 		SET(t->c_cflag, CLOCAL);
   1091   1.5       uch 		CLR(t->c_cflag, HUPCL);
   1092   1.3       uch 	}
   1093   1.3       uch 	splx(s);
   1094   1.3       uch 
   1095   1.3       uch 	/*
   1096   1.6       uch 	 * If we're not in a mode that assumes a connection is present, then
   1097   1.6       uch 	 * ignore carrier changes.
   1098   1.6       uch 	 */
   1099   1.6       uch 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
   1100   1.6       uch 		chip->sc_dcd = 0;
   1101   1.6       uch 	else
   1102   1.6       uch 		chip->sc_dcd = 1;
   1103   1.6       uch 
   1104   1.6       uch 	/*
   1105   1.3       uch 	 * Only whack the UART when params change.
   1106   1.3       uch 	 * Some callers need to clear tp->t_ospeed
   1107   1.3       uch 	 * to make sure initialization gets done.
   1108   1.3       uch 	 */
   1109   1.5       uch 	if (tp->t_ospeed == ospeed && tp->t_cflag == t->c_cflag) {
   1110   1.3       uch 		return 0;
   1111   1.3       uch 	}
   1112   1.3       uch 
   1113   1.3       uch 	s = spltty();
   1114   1.3       uch 	chip = sc->sc_chip;
   1115   1.3       uch 	chip->sc_speed = ospeed;
   1116   1.5       uch 	chip->sc_cflag = t->c_cflag;
   1117   1.3       uch 
   1118   1.3       uch 	txcom_setmode(chip);
   1119   1.3       uch 	txcom_setbaudrate(chip);
   1120   1.6       uch 
   1121   1.3       uch 	/* And copy to tty. */
   1122   1.3       uch 	tp->t_ispeed = 0;
   1123   1.3       uch 	tp->t_ospeed = chip->sc_speed;
   1124   1.3       uch 	tp->t_cflag = chip->sc_cflag;
   1125   1.3       uch 
   1126   1.3       uch 	/*
   1127   1.6       uch 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1128   1.6       uch 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1129   1.6       uch 	 * explicit request.
   1130   1.6       uch 	 */
   1131  1.10       eeh 	(void) (*tp->t_linesw->l_modem)(tp, chip->sc_dcd);
   1132   1.6       uch 
   1133   1.6       uch 	/*
   1134   1.3       uch 	 * If hardware flow control is disabled, unblock any hard flow
   1135   1.3       uch 	 * control state.
   1136   1.3       uch 	 */
   1137   1.3       uch 	if (!ISSET(chip->sc_cflag, CHWFLOW)) {
   1138   1.3       uch 		txcomstart(tp);
   1139   1.3       uch 	}
   1140   1.3       uch 
   1141   1.3       uch 	splx(s);
   1142   1.6       uch 
   1143   1.6       uch 	return 0;
   1144   1.6       uch }
   1145   1.6       uch 
   1146   1.9       uch int
   1147   1.9       uch txcom_dcd_hook(void *arg, int type, long id, void *msg)
   1148   1.9       uch {
   1149   1.9       uch 	struct txcom_softc *sc = arg;
   1150   1.9       uch 	struct tty *tp = sc->sc_tty;
   1151   1.9       uch 	struct txcom_chip *chip = sc->sc_chip;
   1152   1.9       uch 	int modem = !(int)msg; /* p-edge 1, n-edge 0 */
   1153   1.9       uch 
   1154  1.15       uch 	DPRINTF("DCD %s\n", modem ? "ON" : "OFF");
   1155   1.9       uch 
   1156   1.9       uch 	if (modem && chip->sc_dcd)
   1157  1.10       eeh 		(void) (*tp->t_linesw->l_modem)(tp, chip->sc_dcd);
   1158   1.9       uch 
   1159   1.9       uch 	return 0;
   1160   1.9       uch }
   1161   1.9       uch 
   1162   1.9       uch int
   1163   1.9       uch txcom_cts_hook(void *arg, int type, long id, void *msg)
   1164   1.9       uch {
   1165   1.9       uch 	struct txcom_softc *sc = arg;
   1166   1.9       uch 	struct tty *tp = sc->sc_tty;
   1167   1.9       uch 	struct txcom_chip *chip = sc->sc_chip;
   1168   1.9       uch 	int clear = !(int)msg; /* p-edge 1, n-edge 0 */
   1169   1.9       uch 
   1170  1.15       uch 	DPRINTF("CTS %s\n", clear ? "ON"  : "OFF");
   1171   1.9       uch 
   1172   1.9       uch 	if (chip->sc_msr_cts) {
   1173   1.9       uch 		if (!clear) {
   1174   1.9       uch 			chip->sc_tx_stopped = 1;
   1175   1.9       uch 		} else {
   1176   1.9       uch 			chip->sc_tx_stopped = 0;
   1177  1.10       eeh 			(*tp->t_linesw->l_start)(tp);
   1178   1.9       uch 		}
   1179   1.9       uch 	}
   1180   1.9       uch 
   1181   1.9       uch 	return 0;
   1182   1.9       uch }
   1183   1.9       uch 
   1184   1.9       uch #ifdef TX39UARTDEBUG
   1185   1.6       uch void
   1186   1.9       uch txcom_dump(struct txcom_chip *chip)
   1187   1.6       uch {
   1188   1.6       uch 	tx_chipset_tag_t tc = chip->sc_tc;
   1189   1.6       uch 	int slot = chip->sc_slot;
   1190   1.6       uch 	txreg_t reg;
   1191   1.6       uch 
   1192   1.6       uch 	reg = tx_conf_read(tc, TX39_UARTCTRL1_REG(slot));
   1193   1.6       uch #define ISSETPRINT(r, m) \
   1194  1.15       uch 	dbg_bitmask_print(r, TX39_UARTCTRL1_##m, #m)
   1195   1.6       uch 	ISSETPRINT(reg, UARTON);
   1196   1.6       uch 	ISSETPRINT(reg, EMPTY);
   1197   1.6       uch 	ISSETPRINT(reg, PRXHOLDFULL);
   1198   1.6       uch 	ISSETPRINT(reg, RXHOLDFULL);
   1199   1.6       uch 	ISSETPRINT(reg, ENDMARX);
   1200   1.6       uch 	ISSETPRINT(reg, ENDMATX);
   1201   1.6       uch 	ISSETPRINT(reg, TESTMODE);
   1202   1.6       uch 	ISSETPRINT(reg, ENBREAHALT);
   1203   1.6       uch 	ISSETPRINT(reg, ENDMATEST);
   1204   1.6       uch 	ISSETPRINT(reg, ENDMALOOP);
   1205   1.6       uch 	ISSETPRINT(reg, PULSEOPT2);
   1206   1.6       uch 	ISSETPRINT(reg, PULSEOPT1);
   1207   1.6       uch 	ISSETPRINT(reg, DTINVERT);
   1208   1.6       uch 	ISSETPRINT(reg, DISTXD);
   1209   1.6       uch 	ISSETPRINT(reg, TWOSTOP);
   1210   1.6       uch 	ISSETPRINT(reg, LOOPBACK);
   1211   1.6       uch 	ISSETPRINT(reg, BIT7);
   1212   1.6       uch 	ISSETPRINT(reg, EVENPARITY);
   1213   1.6       uch 	ISSETPRINT(reg, ENPARITY);
   1214   1.6       uch 	ISSETPRINT(reg, ENUART);
   1215   1.6       uch }
   1216   1.9       uch #endif /* TX39UARTDEBUG */
   1217