Home | History | Annotate | Line # | Download | only in imx
imxuart.c revision 1.3.18.2
      1  1.3.18.1  rmind /* $NetBSD: imxuart.c,v 1.3.18.2 2011/05/31 03:03:55 rmind Exp $ */
      2       1.2   matt 
      3  1.3.18.1  rmind /*
      4  1.3.18.1  rmind  * Copyright (c) 2009, 2010  Genetec Corporation.  All rights reserved.
      5  1.3.18.1  rmind  * Written by Hiroyuki Bessho for Genetec Corporation.
      6  1.3.18.1  rmind  *
      7  1.3.18.1  rmind  * Redistribution and use in source and binary forms, with or without
      8  1.3.18.1  rmind  * modification, are permitted provided that the following conditions
      9  1.3.18.1  rmind  * are met:
     10  1.3.18.1  rmind  * 1. Redistributions of source code must retain the above copyright
     11  1.3.18.1  rmind  *    notice, this list of conditions and the following disclaimer.
     12  1.3.18.1  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.3.18.1  rmind  *    notice, this list of conditions and the following disclaimer in the
     14  1.3.18.1  rmind  *    documentation and/or other materials provided with the distribution.
     15  1.3.18.1  rmind  *
     16  1.3.18.1  rmind  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     17  1.3.18.1  rmind  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.3.18.1  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.3.18.1  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     20  1.3.18.1  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.3.18.1  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.3.18.1  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.3.18.1  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.3.18.1  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.3.18.1  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.3.18.1  rmind  * POSSIBILITY OF SUCH DAMAGE.
     27  1.3.18.1  rmind  *
     28  1.3.18.1  rmind  */
     29       1.2   matt 
     30  1.3.18.1  rmind /*
     31  1.3.18.1  rmind  * derived from sys/dev/ic/com.c
     32  1.3.18.1  rmind  */
     33       1.2   matt 
     34  1.3.18.1  rmind /*-
     35  1.3.18.1  rmind  * Copyright (c) 1998, 1999, 2004, 2008 The NetBSD Foundation, Inc.
     36  1.3.18.1  rmind  * All rights reserved.
     37  1.3.18.1  rmind  *
     38  1.3.18.1  rmind  * This code is derived from software contributed to The NetBSD Foundation
     39  1.3.18.1  rmind  * by Charles M. Hannum.
     40  1.3.18.1  rmind  *
     41  1.3.18.1  rmind  * Redistribution and use in source and binary forms, with or without
     42  1.3.18.1  rmind  * modification, are permitted provided that the following conditions
     43  1.3.18.1  rmind  * are met:
     44  1.3.18.1  rmind  * 1. Redistributions of source code must retain the above copyright
     45  1.3.18.1  rmind  *    notice, this list of conditions and the following disclaimer.
     46  1.3.18.1  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     47  1.3.18.1  rmind  *    notice, this list of conditions and the following disclaimer in the
     48  1.3.18.1  rmind  *    documentation and/or other materials provided with the distribution.
     49  1.3.18.1  rmind  *
     50  1.3.18.1  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     51  1.3.18.1  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     52  1.3.18.1  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     53  1.3.18.1  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     54  1.3.18.1  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     55  1.3.18.1  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     56  1.3.18.1  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     57  1.3.18.1  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     58  1.3.18.1  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     59  1.3.18.1  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     60  1.3.18.1  rmind  * POSSIBILITY OF SUCH DAMAGE.
     61  1.3.18.1  rmind  */
     62       1.2   matt 
     63  1.3.18.1  rmind /*
     64  1.3.18.1  rmind  * Copyright (c) 1991 The Regents of the University of California.
     65  1.3.18.1  rmind  * All rights reserved.
     66  1.3.18.1  rmind  *
     67  1.3.18.1  rmind  * Redistribution and use in source and binary forms, with or without
     68  1.3.18.1  rmind  * modification, are permitted provided that the following conditions
     69  1.3.18.1  rmind  * are met:
     70  1.3.18.1  rmind  * 1. Redistributions of source code must retain the above copyright
     71  1.3.18.1  rmind  *    notice, this list of conditions and the following disclaimer.
     72  1.3.18.1  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     73  1.3.18.1  rmind  *    notice, this list of conditions and the following disclaimer in the
     74  1.3.18.1  rmind  *    documentation and/or other materials provided with the distribution.
     75  1.3.18.1  rmind  * 3. Neither the name of the University nor the names of its contributors
     76  1.3.18.1  rmind  *    may be used to endorse or promote products derived from this software
     77  1.3.18.1  rmind  *    without specific prior written permission.
     78  1.3.18.1  rmind  *
     79  1.3.18.1  rmind  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     80  1.3.18.1  rmind  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     81  1.3.18.1  rmind  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     82  1.3.18.1  rmind  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     83  1.3.18.1  rmind  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     84  1.3.18.1  rmind  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     85  1.3.18.1  rmind  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     86  1.3.18.1  rmind  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     87  1.3.18.1  rmind  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     88  1.3.18.1  rmind  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     89  1.3.18.1  rmind  * SUCH DAMAGE.
     90  1.3.18.1  rmind  *
     91  1.3.18.1  rmind  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     92  1.3.18.1  rmind  */
     93  1.3.18.1  rmind 
     94  1.3.18.1  rmind /*
     95  1.3.18.1  rmind  * driver for UART in i.MX SoC.
     96  1.3.18.1  rmind  */
     97       1.2   matt 
     98  1.3.18.1  rmind #include <sys/cdefs.h>
     99  1.3.18.1  rmind __KERNEL_RCSID(0, "$NetBSD: imxuart.c,v 1.3.18.2 2011/05/31 03:03:55 rmind Exp $");
    100       1.2   matt 
    101  1.3.18.1  rmind #include "opt_imxuart.h"
    102  1.3.18.1  rmind #include "opt_ddb.h"
    103  1.3.18.1  rmind #include "opt_kgdb.h"
    104  1.3.18.1  rmind #include "opt_lockdebug.h"
    105  1.3.18.1  rmind #include "opt_multiprocessor.h"
    106  1.3.18.1  rmind #include "opt_ntp.h"
    107  1.3.18.1  rmind #include "opt_imxuart.h"
    108  1.3.18.1  rmind #include "opt_imx.h"
    109  1.3.18.1  rmind 
    110  1.3.18.1  rmind #include "rnd.h"
    111  1.3.18.1  rmind #if NRND > 0 && defined(RND_COM)
    112  1.3.18.1  rmind #include <sys/rnd.h>
    113  1.3.18.1  rmind #endif
    114       1.2   matt 
    115  1.3.18.1  rmind #ifndef	IMXUART_TOLERANCE
    116  1.3.18.1  rmind #define	IMXUART_TOLERANCE	30	/* baud rate tolerance, in 0.1% units */
    117  1.3.18.1  rmind #endif
    118       1.2   matt 
    119  1.3.18.1  rmind #ifndef	IMXUART_FREQDIV
    120  1.3.18.1  rmind #define	IMXUART_FREQDIV		2	/* XXX */
    121  1.3.18.1  rmind #endif
    122       1.2   matt 
    123  1.3.18.1  rmind #ifndef	IMXUART_FREQ
    124  1.3.18.1  rmind #define	IMXUART_FREQ	(56900000)
    125  1.3.18.1  rmind #endif
    126       1.2   matt 
    127  1.3.18.1  rmind /*
    128  1.3.18.1  rmind  * Override cnmagic(9) macro before including <sys/systm.h>.
    129  1.3.18.1  rmind  * We need to know if cn_check_magic triggered debugger, so set a flag.
    130  1.3.18.1  rmind  * Callers of cn_check_magic must declare int cn_trapped = 0;
    131  1.3.18.1  rmind  * XXX: this is *ugly*!
    132  1.3.18.1  rmind  */
    133  1.3.18.1  rmind #define	cn_trap()				\
    134  1.3.18.1  rmind 	do {					\
    135  1.3.18.1  rmind 		console_debugger();		\
    136  1.3.18.1  rmind 		cn_trapped = 1;			\
    137  1.3.18.1  rmind 	} while (/* CONSTCOND */ 0)
    138       1.2   matt 
    139  1.3.18.1  rmind #include <sys/param.h>
    140  1.3.18.1  rmind #include <sys/systm.h>
    141  1.3.18.1  rmind #include <sys/ioctl.h>
    142  1.3.18.1  rmind #include <sys/select.h>
    143  1.3.18.1  rmind #include <sys/poll.h>
    144  1.3.18.1  rmind #include <sys/tty.h>
    145  1.3.18.1  rmind #include <sys/proc.h>
    146  1.3.18.1  rmind #include <sys/conf.h>
    147  1.3.18.1  rmind #include <sys/file.h>
    148  1.3.18.1  rmind #include <sys/uio.h>
    149  1.3.18.1  rmind #include <sys/kernel.h>
    150  1.3.18.1  rmind #include <sys/syslog.h>
    151  1.3.18.1  rmind #include <sys/device.h>
    152  1.3.18.1  rmind #include <sys/malloc.h>
    153  1.3.18.1  rmind #include <sys/timepps.h>
    154  1.3.18.1  rmind #include <sys/vnode.h>
    155  1.3.18.1  rmind #include <sys/kauth.h>
    156  1.3.18.1  rmind #include <sys/intr.h>
    157       1.2   matt 
    158  1.3.18.1  rmind #include <sys/bus.h>
    159       1.2   matt 
    160  1.3.18.1  rmind #include <arm/imx/imxuartreg.h>
    161  1.3.18.1  rmind #include <arm/imx/imxuartvar.h>
    162  1.3.18.1  rmind #include <dev/cons.h>
    163       1.2   matt 
    164  1.3.18.1  rmind #ifndef	IMXUART_RING_SIZE
    165  1.3.18.1  rmind #define	IMXUART_RING_SIZE	2048
    166       1.2   matt #endif
    167       1.2   matt 
    168  1.3.18.1  rmind typedef struct imxuart_softc {
    169  1.3.18.1  rmind 	device_t	sc_dev;
    170       1.2   matt 
    171  1.3.18.1  rmind 	struct imxuart_regs {
    172  1.3.18.1  rmind 		bus_space_tag_t		ur_iot;
    173  1.3.18.1  rmind 		bus_space_handle_t	ur_ioh;
    174  1.3.18.1  rmind 		bus_addr_t		ur_iobase;
    175  1.3.18.1  rmind #if 0
    176  1.3.18.1  rmind 		bus_size_t		ur_nports;
    177  1.3.18.1  rmind 		bus_size_t		ur_map[16];
    178  1.3.18.1  rmind #endif
    179  1.3.18.1  rmind 	} sc_regs;
    180       1.2   matt 
    181  1.3.18.1  rmind #define	sc_bt	sc_regs.ur_iot
    182  1.3.18.1  rmind #define	sc_bh	sc_regs.ur_ioh
    183       1.2   matt 
    184  1.3.18.1  rmind 	uint32_t		sc_intrspec_enb;
    185  1.3.18.1  rmind 	uint32_t	sc_ucr2_d;	/* target value for UCR2 */
    186  1.3.18.1  rmind 	uint32_t	sc_ucr[4];	/* cached value of UCRn */
    187  1.3.18.1  rmind #define	sc_ucr1	sc_ucr[0]
    188  1.3.18.1  rmind #define	sc_ucr2	sc_ucr[1]
    189  1.3.18.1  rmind #define	sc_ucr3	sc_ucr[2]
    190  1.3.18.1  rmind #define	sc_ucr4	sc_ucr[3]
    191  1.3.18.1  rmind 
    192  1.3.18.1  rmind 	uint			sc_init_cnt;
    193  1.3.18.1  rmind 
    194  1.3.18.1  rmind 	bus_addr_t		sc_addr;
    195  1.3.18.1  rmind 	bus_size_t		sc_size;
    196  1.3.18.1  rmind 	int			sc_intr;
    197  1.3.18.1  rmind 
    198  1.3.18.1  rmind 	u_char	sc_hwflags;
    199  1.3.18.1  rmind /* Hardware flag masks */
    200  1.3.18.1  rmind #define	IMXUART_HW_FLOW 	__BIT(0)
    201  1.3.18.1  rmind #define	IMXUART_HW_DEV_OK	__BIT(1)
    202  1.3.18.1  rmind #define	IMXUART_HW_CONSOLE	__BIT(2)
    203  1.3.18.1  rmind #define	IMXUART_HW_KGDB 	__BIT(3)
    204  1.3.18.1  rmind 
    205  1.3.18.1  rmind 
    206  1.3.18.1  rmind 	bool	enabled;
    207  1.3.18.1  rmind 
    208  1.3.18.1  rmind 	u_char	sc_swflags;
    209  1.3.18.1  rmind 
    210  1.3.18.1  rmind 	u_char sc_rx_flags;
    211  1.3.18.1  rmind #define	IMXUART_RX_TTY_BLOCKED  	__BIT(0)
    212  1.3.18.1  rmind #define	IMXUART_RX_TTY_OVERFLOWED	__BIT(1)
    213  1.3.18.1  rmind #define	IMXUART_RX_IBUF_BLOCKED 	__BIT(2)
    214  1.3.18.1  rmind #define	IMXUART_RX_IBUF_OVERFLOWED	__BIT(3)
    215  1.3.18.1  rmind #define	IMXUART_RX_ANY_BLOCK					\
    216  1.3.18.1  rmind 	(IMXUART_RX_TTY_BLOCKED|IMXUART_RX_TTY_OVERFLOWED| 	\
    217  1.3.18.1  rmind 	    IMXUART_RX_IBUF_BLOCKED|IMXUART_RX_IBUF_OVERFLOWED)
    218  1.3.18.1  rmind 
    219  1.3.18.1  rmind 	bool	sc_tx_busy, sc_tx_done, sc_tx_stopped;
    220  1.3.18.1  rmind 	bool	sc_rx_ready,sc_st_check;
    221  1.3.18.1  rmind 	u_short	sc_txfifo_len, sc_txfifo_thresh;
    222  1.3.18.1  rmind 
    223  1.3.18.1  rmind 	uint16_t	*sc_rbuf;
    224  1.3.18.1  rmind 	u_int		sc_rbuf_size;
    225  1.3.18.1  rmind 	u_int		sc_rbuf_in;
    226  1.3.18.1  rmind 	u_int		sc_rbuf_out;
    227  1.3.18.1  rmind #define	IMXUART_RBUF_AVAIL(sc)					\
    228  1.3.18.1  rmind 	((sc->sc_rbuf_out <= sc->sc_rbuf_in) ?			\
    229  1.3.18.1  rmind 	(sc->sc_rbuf_in - sc->sc_rbuf_out) :			\
    230  1.3.18.1  rmind 	(sc->sc_rbuf_size - (sc->sc_rbuf_out - sc->sc_rbuf_in)))
    231  1.3.18.1  rmind 
    232  1.3.18.1  rmind #define	IMXUART_RBUF_SPACE(sc)	\
    233  1.3.18.1  rmind 	((sc->sc_rbuf_in <= sc->sc_rbuf_out ?			    \
    234  1.3.18.1  rmind 	    sc->sc_rbuf_size - (sc->sc_rbuf_out - sc->sc_rbuf_in) : \
    235  1.3.18.1  rmind 	    sc->sc_rbuf_in - sc->sc_rbuf_out) - 1)
    236  1.3.18.1  rmind /* increment ringbuffer pointer */
    237  1.3.18.1  rmind #define	IMXUART_RBUF_INC(sc,v,i)	(((v) + (i))&((sc->sc_rbuf_size)-1))
    238  1.3.18.1  rmind 	u_int	sc_r_lowat;
    239  1.3.18.1  rmind 	u_int	sc_r_hiwat;
    240  1.3.18.1  rmind 
    241  1.3.18.1  rmind 	/* output chunk */
    242  1.3.18.1  rmind  	u_char *sc_tba;
    243  1.3.18.1  rmind  	u_int sc_tbc;
    244  1.3.18.1  rmind 	u_int sc_heldtbc;
    245  1.3.18.1  rmind 	/* pending parameter changes */
    246  1.3.18.1  rmind 	u_char	sc_pending;
    247  1.3.18.1  rmind #define	IMXUART_PEND_PARAM	__BIT(0)
    248  1.3.18.1  rmind #define	IMXUART_PEND_SPEED	__BIT(1)
    249  1.3.18.1  rmind 
    250  1.3.18.1  rmind 
    251  1.3.18.1  rmind 	struct callout sc_diag_callout;
    252  1.3.18.1  rmind 	kmutex_t sc_lock;
    253  1.3.18.1  rmind 	void *sc_ih;		/* interrupt handler */
    254  1.3.18.1  rmind 	void *sc_si;		/* soft interrupt */
    255  1.3.18.1  rmind 	struct tty		*sc_tty;
    256  1.3.18.1  rmind 
    257  1.3.18.1  rmind 	/* power management hooks */
    258  1.3.18.1  rmind 	int (*enable)(struct imxuart_softc *);
    259  1.3.18.1  rmind 	void (*disable)(struct imxuart_softc *);
    260  1.3.18.1  rmind 
    261  1.3.18.1  rmind 	struct {
    262  1.3.18.1  rmind 		ulong err;
    263  1.3.18.1  rmind 		ulong brk;
    264  1.3.18.1  rmind 		ulong prerr;
    265  1.3.18.1  rmind 		ulong frmerr;
    266  1.3.18.1  rmind 		ulong ovrrun;
    267  1.3.18.1  rmind 	}	sc_errors;
    268  1.3.18.1  rmind 
    269  1.3.18.1  rmind 	struct imxuart_baudrate_ratio {
    270  1.3.18.1  rmind 		uint16_t numerator;	/* UBIR */
    271  1.3.18.1  rmind 		uint16_t modulator;	/* UBMR */
    272  1.3.18.1  rmind 	} sc_ratio;
    273  1.3.18.1  rmind 
    274  1.3.18.1  rmind } imxuart_softc_t;
    275  1.3.18.1  rmind 
    276  1.3.18.1  rmind 
    277  1.3.18.1  rmind int	imxuspeed(long, struct imxuart_baudrate_ratio *);
    278  1.3.18.1  rmind int	imxuparam(struct tty *, struct termios *);
    279  1.3.18.1  rmind void	imxustart(struct tty *);
    280  1.3.18.1  rmind int	imxuhwiflow(struct tty *, int);
    281  1.3.18.1  rmind 
    282  1.3.18.1  rmind void	imxuart_shutdown(struct imxuart_softc *);
    283  1.3.18.1  rmind void	imxuart_loadchannelregs(struct imxuart_softc *);
    284  1.3.18.1  rmind void	imxuart_hwiflow(struct imxuart_softc *);
    285  1.3.18.1  rmind void	imxuart_break(struct imxuart_softc *, bool);
    286  1.3.18.1  rmind void	imxuart_modem(struct imxuart_softc *, int);
    287  1.3.18.1  rmind void	tiocm_to_imxu(struct imxuart_softc *, u_long, int);
    288  1.3.18.1  rmind int	imxuart_to_tiocm(struct imxuart_softc *);
    289  1.3.18.1  rmind void	imxuart_iflush(struct imxuart_softc *);
    290  1.3.18.1  rmind int	imxuintr(void *);
    291  1.3.18.1  rmind 
    292  1.3.18.1  rmind int	imxuart_common_getc(dev_t, struct imxuart_regs *);
    293  1.3.18.1  rmind void	imxuart_common_putc(dev_t, struct imxuart_regs *, int);
    294  1.3.18.1  rmind 
    295  1.3.18.1  rmind 
    296  1.3.18.1  rmind int	imxuart_init(struct imxuart_regs *, int, tcflag_t);
    297  1.3.18.1  rmind 
    298  1.3.18.1  rmind int	imxucngetc(dev_t);
    299  1.3.18.1  rmind void	imxucnputc(dev_t, int);
    300  1.3.18.1  rmind void	imxucnpollc(dev_t, int);
    301  1.3.18.1  rmind 
    302  1.3.18.1  rmind static void imxuintr_read(struct imxuart_softc *);
    303  1.3.18.1  rmind static void imxuintr_send(struct imxuart_softc *);
    304  1.3.18.1  rmind 
    305  1.3.18.1  rmind static void imxuart_enable_debugport(struct imxuart_softc *);
    306  1.3.18.1  rmind static void imxuart_disable_all_interrupts(struct imxuart_softc *);
    307  1.3.18.1  rmind static void imxuart_control_rxint(struct imxuart_softc *, bool);
    308  1.3.18.1  rmind static void imxuart_control_txint(struct imxuart_softc *, bool);
    309  1.3.18.1  rmind static u_int imxuart_txfifo_space(struct imxuart_softc *sc);
    310       1.2   matt 
    311  1.3.18.1  rmind static	uint32_t	cflag_to_ucr2(tcflag_t, uint32_t);
    312       1.2   matt 
    313  1.3.18.1  rmind CFATTACH_DECL_NEW(imxuart, sizeof(struct imxuart_softc),
    314       1.2   matt     imxuart_match, imxuart_attach, NULL, NULL);
    315       1.2   matt 
    316       1.2   matt 
    317  1.3.18.1  rmind #define	integrate	static inline
    318  1.3.18.1  rmind void 	imxusoft(void *);
    319  1.3.18.1  rmind integrate void imxuart_rxsoft(struct imxuart_softc *, struct tty *);
    320  1.3.18.1  rmind integrate void imxuart_txsoft(struct imxuart_softc *, struct tty *);
    321  1.3.18.1  rmind integrate void imxuart_stsoft(struct imxuart_softc *, struct tty *);
    322  1.3.18.1  rmind integrate void imxuart_schedrx(struct imxuart_softc *);
    323  1.3.18.1  rmind void	imxudiag(void *);
    324  1.3.18.1  rmind static void imxuart_load_speed(struct imxuart_softc *);
    325  1.3.18.1  rmind static void imxuart_load_params(struct imxuart_softc *);
    326  1.3.18.1  rmind integrate void imxuart_load_pendings(struct imxuart_softc *);
    327  1.3.18.1  rmind 
    328  1.3.18.1  rmind 
    329  1.3.18.1  rmind extern struct cfdriver imxuart_cd;
    330  1.3.18.1  rmind 
    331  1.3.18.1  rmind dev_type_open(imxuopen);
    332  1.3.18.1  rmind dev_type_close(imxuclose);
    333  1.3.18.1  rmind dev_type_read(imxuread);
    334  1.3.18.1  rmind dev_type_write(imxuwrite);
    335  1.3.18.1  rmind dev_type_ioctl(imxuioctl);
    336  1.3.18.1  rmind dev_type_stop(imxustop);
    337  1.3.18.1  rmind dev_type_tty(imxutty);
    338  1.3.18.1  rmind dev_type_poll(imxupoll);
    339  1.3.18.1  rmind 
    340  1.3.18.1  rmind const struct cdevsw imxcom_cdevsw = {
    341  1.3.18.1  rmind 	imxuopen, imxuclose, imxuread, imxuwrite, imxuioctl,
    342  1.3.18.1  rmind 	imxustop, imxutty, imxupoll, nommap, ttykqfilter, D_TTY
    343  1.3.18.1  rmind };
    344  1.3.18.1  rmind 
    345       1.2   matt /*
    346  1.3.18.1  rmind  * Make this an option variable one can patch.
    347  1.3.18.1  rmind  * But be warned:  this must be a power of 2!
    348  1.3.18.1  rmind  */
    349  1.3.18.1  rmind u_int imxuart_rbuf_size = IMXUART_RING_SIZE;
    350       1.2   matt 
    351  1.3.18.1  rmind /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    352  1.3.18.1  rmind u_int imxuart_rbuf_hiwat = (IMXUART_RING_SIZE * 1) / 4;
    353  1.3.18.1  rmind u_int imxuart_rbuf_lowat = (IMXUART_RING_SIZE * 3) / 4;
    354       1.2   matt 
    355  1.3.18.1  rmind static struct imxuart_regs imxuconsregs;
    356  1.3.18.1  rmind static int imxuconsattached;
    357  1.3.18.1  rmind static int imxuconsrate;
    358  1.3.18.1  rmind static tcflag_t imxuconscflag;
    359  1.3.18.1  rmind static struct cnm_state imxuart_cnm_state;
    360       1.2   matt 
    361  1.3.18.1  rmind u_int imxuart_freq = IMXUART_FREQ;
    362  1.3.18.1  rmind u_int imxuart_freqdiv = IMXUART_FREQDIV;
    363       1.2   matt 
    364  1.3.18.1  rmind #ifdef KGDB
    365  1.3.18.1  rmind #include <sys/kgdb.h>
    366       1.2   matt 
    367  1.3.18.1  rmind static struct imxuart_regs imxu_kgdb_regs;
    368  1.3.18.1  rmind static int imxu_kgdb_attached;
    369       1.2   matt 
    370  1.3.18.1  rmind int	imxuart_kgdb_getc(void *);
    371  1.3.18.1  rmind void	imxuart_kgdb_putc(void *, int);
    372  1.3.18.1  rmind #endif /* KGDB */
    373  1.3.18.1  rmind 
    374  1.3.18.1  rmind #define	IMXUART_UNIT_MASK	0x7ffff
    375  1.3.18.1  rmind #define	IMXUART_DIALOUT_MASK	0x80000
    376  1.3.18.1  rmind 
    377  1.3.18.1  rmind #define	IMXUART_UNIT(x)	(minor(x) & IMXUART_UNIT_MASK)
    378  1.3.18.1  rmind #define	IMXUART_DIALOUT(x)	(minor(x) & IMXUART_DIALOUT_MASK)
    379  1.3.18.1  rmind 
    380  1.3.18.1  rmind #define	IMXUART_ISALIVE(sc)	((sc)->enabled != 0 && \
    381  1.3.18.1  rmind 			 device_is_active((sc)->sc_dev))
    382  1.3.18.1  rmind 
    383  1.3.18.1  rmind #define	BR	BUS_SPACE_BARRIER_READ
    384  1.3.18.1  rmind #define	BW	BUS_SPACE_BARRIER_WRITE
    385  1.3.18.1  rmind #define	IMXUART_BARRIER(r, f) \
    386  1.3.18.1  rmind 	bus_space_barrier((r)->ur_iot, (r)->ur_ioh, 0, IMX_UART_SIZE, (f))
    387  1.3.18.1  rmind 
    388  1.3.18.1  rmind 
    389  1.3.18.1  rmind void
    390  1.3.18.1  rmind imxuart_attach_common(device_t parent, device_t self,
    391  1.3.18.1  rmind     bus_space_tag_t iot, paddr_t iobase, size_t size, int intr, int flags)
    392       1.2   matt {
    393  1.3.18.1  rmind 	imxuart_softc_t *sc = device_private(self);
    394  1.3.18.1  rmind 	struct imxuart_regs *regsp = &sc->sc_regs;
    395  1.3.18.1  rmind 	struct tty *tp;
    396  1.3.18.1  rmind 	bus_space_handle_t ioh;
    397       1.2   matt 
    398  1.3.18.1  rmind 	aprint_naive("\n");
    399  1.3.18.1  rmind 	aprint_normal("\n");
    400       1.2   matt 
    401  1.3.18.1  rmind 	sc->sc_dev = self;
    402       1.2   matt 
    403  1.3.18.1  rmind 	if (size <= 0)
    404  1.3.18.1  rmind 		size = IMX_UART_SIZE;
    405       1.2   matt 
    406  1.3.18.1  rmind 	sc->sc_intr = intr;
    407  1.3.18.1  rmind 	regsp->ur_iot = iot;
    408  1.3.18.1  rmind 	regsp->ur_iobase = iobase;
    409       1.2   matt 
    410  1.3.18.1  rmind 	if (bus_space_map(iot, regsp->ur_iobase, size, 0, &ioh)) {
    411  1.3.18.1  rmind 		return;
    412  1.3.18.1  rmind 	}
    413  1.3.18.1  rmind 	regsp->ur_ioh = ioh;
    414  1.3.18.1  rmind 
    415  1.3.18.1  rmind 	callout_init(&sc->sc_diag_callout, 0);
    416  1.3.18.1  rmind 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
    417  1.3.18.1  rmind 
    418  1.3.18.1  rmind 	bus_space_read_region_4(iot, ioh, IMX_UCR1, sc->sc_ucr, 4);
    419  1.3.18.1  rmind 	sc->sc_ucr2_d = sc->sc_ucr2;
    420  1.3.18.1  rmind 
    421  1.3.18.1  rmind 	/* Disable interrupts before configuring the device. */
    422  1.3.18.1  rmind 	imxuart_disable_all_interrupts(sc);
    423  1.3.18.1  rmind 
    424  1.3.18.1  rmind 	if (regsp->ur_iobase == imxuconsregs.ur_iobase) {
    425  1.3.18.1  rmind 		imxuconsattached = 1;
    426  1.3.18.1  rmind 
    427  1.3.18.1  rmind 		/* Make sure the console is always "hardwired". */
    428  1.3.18.1  rmind #if 0
    429  1.3.18.1  rmind 		delay(10000);			/* wait for output to finish */
    430  1.3.18.1  rmind #endif
    431  1.3.18.1  rmind 		SET(sc->sc_hwflags, IMXUART_HW_CONSOLE);
    432  1.3.18.1  rmind 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    433  1.3.18.1  rmind 	}
    434  1.3.18.1  rmind 
    435  1.3.18.1  rmind 
    436  1.3.18.2  rmind 	tp = tty_alloc();
    437  1.3.18.1  rmind 	tp->t_oproc = imxustart;
    438  1.3.18.1  rmind 	tp->t_param = imxuparam;
    439  1.3.18.1  rmind 	tp->t_hwiflow = imxuhwiflow;
    440  1.3.18.1  rmind 
    441  1.3.18.1  rmind 	sc->sc_tty = tp;
    442  1.3.18.1  rmind 	sc->sc_rbuf = malloc(sizeof (*sc->sc_rbuf) * imxuart_rbuf_size,
    443  1.3.18.1  rmind 	    M_DEVBUF, M_NOWAIT);
    444  1.3.18.1  rmind 	sc->sc_rbuf_size = imxuart_rbuf_size;
    445  1.3.18.1  rmind 	sc->sc_rbuf_in = sc->sc_rbuf_out = 0;
    446  1.3.18.1  rmind 	if (sc->sc_rbuf == NULL) {
    447  1.3.18.1  rmind 		aprint_error_dev(sc->sc_dev,
    448  1.3.18.1  rmind 		    "unable to allocate ring buffer\n");
    449  1.3.18.1  rmind 		return;
    450  1.3.18.1  rmind 	}
    451  1.3.18.1  rmind 
    452  1.3.18.1  rmind 	sc->sc_txfifo_len = 32;
    453  1.3.18.1  rmind 	sc->sc_txfifo_thresh = 16;	/* when USR1.TRDY, fifo has space
    454  1.3.18.1  rmind 					 * for this many characters */
    455  1.3.18.1  rmind 
    456  1.3.18.1  rmind 	tty_attach(tp);
    457  1.3.18.1  rmind 
    458  1.3.18.1  rmind 	if (ISSET(sc->sc_hwflags, IMXUART_HW_CONSOLE)) {
    459  1.3.18.1  rmind 		int maj;
    460  1.3.18.1  rmind 
    461  1.3.18.1  rmind 		/* locate the major number */
    462  1.3.18.1  rmind 		maj = cdevsw_lookup_major(&imxcom_cdevsw);
    463  1.3.18.1  rmind 
    464  1.3.18.1  rmind 		if (maj != NODEVMAJOR) {
    465  1.3.18.1  rmind 			tp->t_dev = cn_tab->cn_dev = makedev(maj,
    466  1.3.18.1  rmind 			    device_unit(sc->sc_dev));
    467  1.3.18.1  rmind 
    468  1.3.18.1  rmind 			aprint_normal_dev(sc->sc_dev, "console\n");
    469  1.3.18.1  rmind 		}
    470  1.3.18.1  rmind 	}
    471  1.3.18.1  rmind 
    472  1.3.18.1  rmind 	sc->sc_ih = intr_establish(sc->sc_intr, IPL_SERIAL, IST_LEVEL,
    473  1.3.18.1  rmind 	    imxuintr, sc);
    474  1.3.18.1  rmind 	if (sc->sc_ih == NULL)
    475  1.3.18.1  rmind 		aprint_error_dev(sc->sc_dev, "intr_establish failed\n");
    476  1.3.18.1  rmind 
    477  1.3.18.1  rmind #ifdef KGDB
    478  1.3.18.1  rmind 	/*
    479  1.3.18.1  rmind 	 * Allow kgdb to "take over" this port.  If this is
    480  1.3.18.1  rmind 	 * not the console and is the kgdb device, it has
    481  1.3.18.1  rmind 	 * exclusive use.  If it's the console _and_ the
    482  1.3.18.1  rmind 	 * kgdb device, it doesn't.
    483  1.3.18.1  rmind 	 */
    484  1.3.18.1  rmind 	if (regsp->ur_iobase == imxu_kgdb_regs.ur_iobase) {
    485  1.3.18.1  rmind 		if (!ISSET(sc->sc_hwflags, IMXUART_HW_CONSOLE)) {
    486  1.3.18.1  rmind 			imxu_kgdb_attached = 1;
    487  1.3.18.1  rmind 
    488  1.3.18.1  rmind 			SET(sc->sc_hwflags, IMXUART_HW_KGDB);
    489  1.3.18.1  rmind 		}
    490  1.3.18.1  rmind 		aprint_normal_dev(sc->sc_dev, "kgdb\n");
    491  1.3.18.1  rmind 	}
    492  1.3.18.1  rmind #endif
    493  1.3.18.1  rmind 
    494  1.3.18.1  rmind 	sc->sc_si = softint_establish(SOFTINT_SERIAL, imxusoft, sc);
    495  1.3.18.1  rmind 
    496  1.3.18.1  rmind #if NRND > 0 && defined(RND_IMXUART)
    497  1.3.18.1  rmind 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    498  1.3.18.1  rmind 			  RND_TYPE_TTY, 0);
    499  1.3.18.1  rmind #endif
    500  1.3.18.1  rmind 
    501  1.3.18.1  rmind 	/* if there are no enable/disable functions, assume the device
    502  1.3.18.1  rmind 	   is always enabled */
    503  1.3.18.1  rmind 	if (!sc->enable)
    504  1.3.18.1  rmind 		sc->enabled = 1;
    505  1.3.18.1  rmind 
    506  1.3.18.1  rmind 	imxuart_enable_debugport(sc);
    507  1.3.18.1  rmind 
    508  1.3.18.1  rmind 	SET(sc->sc_hwflags, IMXUART_HW_DEV_OK);
    509  1.3.18.1  rmind 
    510  1.3.18.1  rmind 	//shutdownhook_establish(imxuart_shutdownhook, sc);
    511  1.3.18.1  rmind 
    512  1.3.18.1  rmind 
    513  1.3.18.1  rmind #if 0
    514  1.3.18.1  rmind 	{
    515  1.3.18.1  rmind 		uint32_t reg;
    516  1.3.18.1  rmind 		reg = bus_space_read_4(iot, ioh, IMX_UCR1);
    517  1.3.18.1  rmind 		reg |= IMX_UCR1_TXDMAEN | IMX_UCR1_RXDMAEN;
    518  1.3.18.1  rmind 		bus_space_write_4(iot, ioh, IMX_UCR1, reg);
    519  1.3.18.1  rmind 	}
    520  1.3.18.1  rmind #endif
    521       1.2   matt }
    522       1.2   matt 
    523       1.2   matt /*
    524  1.3.18.1  rmind  * baudrate = RefFreq / (16 * (UMBR + 1)/(UBIR + 1))
    525  1.3.18.1  rmind  *
    526  1.3.18.1  rmind  * (UBIR + 1) / (UBMR + 1) = (16 * BaurdRate) / RefFreq
    527       1.2   matt  */
    528  1.3.18.1  rmind 
    529  1.3.18.1  rmind static long
    530  1.3.18.1  rmind gcd(long m, long n)
    531       1.2   matt {
    532       1.2   matt 
    533  1.3.18.1  rmind 	if (m < n)
    534  1.3.18.1  rmind 		return gcd(n, m);
    535  1.3.18.1  rmind 
    536  1.3.18.1  rmind 	if (n <= 0)
    537  1.3.18.1  rmind 		return m;
    538  1.3.18.1  rmind 	return gcd(n, m % n);
    539       1.2   matt }
    540       1.2   matt 
    541       1.2   matt 
    542       1.2   matt int
    543  1.3.18.1  rmind imxuspeed(long speed, struct imxuart_baudrate_ratio *ratio)
    544       1.2   matt {
    545  1.3.18.1  rmind #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    546  1.3.18.1  rmind 	long b = 16 * speed;
    547  1.3.18.1  rmind 	long f = imxuart_freq / imxuart_freqdiv;
    548  1.3.18.1  rmind 	long d;
    549  1.3.18.1  rmind 	int err = 0;
    550  1.3.18.1  rmind 
    551  1.3.18.1  rmind 	/* reduce b/f */
    552  1.3.18.1  rmind 	while ((f > (1<<16) || b > (1<<16)) && (d = gcd(f, b)) > 1) {
    553  1.3.18.1  rmind 		f /= d;
    554  1.3.18.1  rmind 		b /= d;
    555  1.3.18.1  rmind 	}
    556  1.3.18.1  rmind 
    557  1.3.18.1  rmind 
    558  1.3.18.1  rmind 	while (f > (1<<16) || b > (1<<16)) {
    559  1.3.18.1  rmind 		f /= 2;
    560  1.3.18.1  rmind 		b /= 2;
    561  1.3.18.1  rmind 	}
    562  1.3.18.1  rmind 	if (f <= 0 || b <= 0)
    563  1.3.18.1  rmind 		return -1;
    564  1.3.18.1  rmind 
    565  1.3.18.1  rmind #ifdef	DIAGNOSTIC
    566  1.3.18.1  rmind 	err = divrnd(((uint64_t)imxuart_freq) * 1000 / imxuart_freqdiv,
    567  1.3.18.1  rmind 		     (uint64_t)speed * 16 * f / b) - 1000;
    568  1.3.18.1  rmind 	if (err < 0)
    569  1.3.18.1  rmind 		err = -err;
    570  1.3.18.1  rmind #endif
    571  1.3.18.1  rmind 
    572  1.3.18.1  rmind 	ratio->numerator = b-1;
    573  1.3.18.1  rmind 	ratio->modulator = f-1;
    574       1.2   matt 
    575  1.3.18.1  rmind 	if (err > IMXUART_TOLERANCE)
    576  1.3.18.1  rmind 		return -1;
    577       1.2   matt 
    578       1.2   matt 	return 0;
    579  1.3.18.1  rmind #undef	divrnd
    580       1.2   matt }
    581       1.2   matt 
    582  1.3.18.1  rmind #ifdef IMXUART_DEBUG
    583  1.3.18.1  rmind int	imxuart_debug = 0;
    584  1.3.18.1  rmind 
    585  1.3.18.1  rmind void imxustatus(struct imxuart_softc *, const char *);
    586  1.3.18.1  rmind void
    587  1.3.18.1  rmind imxustatus(struct imxuart_softc *sc, const char *str)
    588       1.2   matt {
    589  1.3.18.1  rmind 	struct tty *tp = sc->sc_tty;
    590       1.2   matt 
    591  1.3.18.1  rmind 	aprint_normal_dev(sc->sc_dev,
    592  1.3.18.1  rmind 	    "%s %cclocal  %cdcd %cts_carr_on %cdtr %ctx_stopped\n",
    593  1.3.18.1  rmind 	    str,
    594  1.3.18.1  rmind 	    ISSET(tp->t_cflag, CLOCAL) ? '+' : '-',
    595  1.3.18.1  rmind 	    ISSET(sc->sc_msr, MSR_DCD) ? '+' : '-',
    596  1.3.18.1  rmind 	    ISSET(tp->t_state, TS_CARR_ON) ? '+' : '-',
    597  1.3.18.1  rmind 	    ISSET(sc->sc_mcr, MCR_DTR) ? '+' : '-',
    598  1.3.18.1  rmind 	    sc->sc_tx_stopped ? '+' : '-');
    599  1.3.18.1  rmind 
    600  1.3.18.1  rmind 	aprint_normal_dev(sc->sc_dev,
    601  1.3.18.1  rmind 	    "%s %ccrtscts %ccts %cts_ttstop  %crts rx_flags=0x%x\n",
    602  1.3.18.1  rmind 	    str,
    603  1.3.18.1  rmind 	    ISSET(tp->t_cflag, CRTSCTS) ? '+' : '-',
    604  1.3.18.1  rmind 	    ISSET(sc->sc_msr, MSR_CTS) ? '+' : '-',
    605  1.3.18.1  rmind 	    ISSET(tp->t_state, TS_TTSTOP) ? '+' : '-',
    606  1.3.18.1  rmind 	    ISSET(sc->sc_mcr, MCR_RTS) ? '+' : '-',
    607  1.3.18.1  rmind 	    sc->sc_rx_flags);
    608       1.2   matt }
    609  1.3.18.1  rmind #endif
    610       1.2   matt 
    611  1.3.18.1  rmind #if 0
    612       1.2   matt int
    613  1.3.18.1  rmind imxuart_detach(device_t self, int flags)
    614       1.2   matt {
    615  1.3.18.1  rmind 	struct imxuart_softc *sc = device_private(self);
    616  1.3.18.1  rmind 	int maj, mn;
    617       1.2   matt 
    618  1.3.18.1  rmind         if (ISSET(sc->sc_hwflags, IMXUART_HW_CONSOLE))
    619  1.3.18.1  rmind 		return EBUSY;
    620  1.3.18.1  rmind 
    621  1.3.18.1  rmind 	/* locate the major number */
    622  1.3.18.1  rmind 	maj = cdevsw_lookup_major(&imxcom_cdevsw);
    623  1.3.18.1  rmind 
    624  1.3.18.1  rmind 	/* Nuke the vnodes for any open instances. */
    625  1.3.18.1  rmind 	mn = device_unit(self);
    626  1.3.18.1  rmind 	vdevgone(maj, mn, mn, VCHR);
    627  1.3.18.1  rmind 
    628  1.3.18.1  rmind 	mn |= IMXUART_DIALOUT_MASK;
    629  1.3.18.1  rmind 	vdevgone(maj, mn, mn, VCHR);
    630  1.3.18.1  rmind 
    631  1.3.18.1  rmind 	if (sc->sc_rbuf == NULL) {
    632  1.3.18.1  rmind 		/*
    633  1.3.18.1  rmind 		 * Ring buffer allocation failed in the imxuart_attach_subr,
    634  1.3.18.1  rmind 		 * only the tty is allocated, and nothing else.
    635  1.3.18.1  rmind 		 */
    636  1.3.18.2  rmind 		tty_free(sc->sc_tty);
    637  1.3.18.1  rmind 		return 0;
    638       1.2   matt 	}
    639       1.2   matt 
    640  1.3.18.1  rmind 	/* Free the receive buffer. */
    641  1.3.18.1  rmind 	free(sc->sc_rbuf, M_DEVBUF);
    642       1.2   matt 
    643  1.3.18.1  rmind 	/* Detach and free the tty. */
    644  1.3.18.1  rmind 	tty_detach(sc->sc_tty);
    645  1.3.18.2  rmind 	tty_free(sc->sc_tty);
    646  1.3.18.1  rmind 
    647  1.3.18.1  rmind 	/* Unhook the soft interrupt handler. */
    648  1.3.18.1  rmind 	softint_disestablish(sc->sc_si);
    649  1.3.18.1  rmind 
    650  1.3.18.1  rmind #if NRND > 0 && defined(RND_IMXU)
    651  1.3.18.1  rmind 	/* Unhook the entropy source. */
    652  1.3.18.1  rmind 	rnd_detach_source(&sc->rnd_source);
    653  1.3.18.1  rmind #endif
    654  1.3.18.1  rmind 	callout_destroy(&sc->sc_diag_callout);
    655  1.3.18.1  rmind 
    656  1.3.18.1  rmind 	/* Destroy the lock. */
    657  1.3.18.1  rmind 	mutex_destroy(&sc->sc_lock);
    658  1.3.18.1  rmind 
    659  1.3.18.1  rmind 	return (0);
    660       1.2   matt }
    661  1.3.18.1  rmind #endif
    662       1.2   matt 
    663  1.3.18.1  rmind #ifdef notyet
    664       1.2   matt int
    665  1.3.18.1  rmind imxuart_activate(device_t self, enum devact act)
    666       1.2   matt {
    667  1.3.18.1  rmind 	struct imxuart_softc *sc = device_private(self);
    668  1.3.18.1  rmind 	int rv = 0;
    669       1.2   matt 
    670  1.3.18.1  rmind 	switch (act) {
    671  1.3.18.1  rmind 	case DVACT_ACTIVATE:
    672  1.3.18.1  rmind 		rv = EOPNOTSUPP;
    673  1.3.18.1  rmind 		break;
    674  1.3.18.1  rmind 
    675  1.3.18.1  rmind 	case DVACT_DEACTIVATE:
    676  1.3.18.1  rmind 		if (sc->sc_hwflags & (IMXUART_HW_CONSOLE|IMXUART_HW_KGDB)) {
    677  1.3.18.1  rmind 			rv = EBUSY;
    678       1.2   matt 			break;
    679       1.2   matt 		}
    680  1.3.18.1  rmind 
    681  1.3.18.1  rmind 		if (sc->disable != NULL && sc->enabled != 0) {
    682  1.3.18.1  rmind 			(*sc->disable)(sc);
    683  1.3.18.1  rmind 			sc->enabled = 0;
    684  1.3.18.1  rmind 		}
    685  1.3.18.1  rmind 		break;
    686       1.2   matt 	}
    687       1.2   matt 
    688  1.3.18.1  rmind 	return (rv);
    689       1.2   matt }
    690       1.2   matt #endif
    691       1.2   matt 
    692  1.3.18.1  rmind void
    693  1.3.18.1  rmind imxuart_shutdown(struct imxuart_softc *sc)
    694       1.2   matt {
    695  1.3.18.1  rmind 	struct tty *tp = sc->sc_tty;
    696       1.2   matt 
    697  1.3.18.1  rmind 	mutex_spin_enter(&sc->sc_lock);
    698  1.3.18.1  rmind 
    699  1.3.18.1  rmind 	/* If we were asserting flow control, then deassert it. */
    700  1.3.18.1  rmind 	SET(sc->sc_rx_flags, IMXUART_RX_IBUF_BLOCKED);
    701  1.3.18.1  rmind 	imxuart_hwiflow(sc);
    702  1.3.18.1  rmind 
    703  1.3.18.1  rmind 	/* Clear any break condition set with TIOCSBRK. */
    704  1.3.18.1  rmind 	imxuart_break(sc, false);
    705  1.3.18.1  rmind 
    706  1.3.18.1  rmind 	/*
    707  1.3.18.1  rmind 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    708  1.3.18.1  rmind 	 * notice even if we immediately open the port again.
    709  1.3.18.1  rmind 	 * Avoid tsleeping above splhigh().
    710  1.3.18.1  rmind 	 */
    711  1.3.18.1  rmind 	if (ISSET(tp->t_cflag, HUPCL)) {
    712  1.3.18.1  rmind 		imxuart_modem(sc, 0);
    713  1.3.18.1  rmind 		mutex_spin_exit(&sc->sc_lock);
    714  1.3.18.1  rmind 		/* XXX will only timeout */
    715  1.3.18.1  rmind 		(void) kpause(ttclos, false, hz, NULL);
    716  1.3.18.1  rmind 		mutex_spin_enter(&sc->sc_lock);
    717       1.2   matt 	}
    718  1.3.18.1  rmind 
    719  1.3.18.1  rmind 	/* Turn off interrupts. */
    720  1.3.18.1  rmind 	imxuart_disable_all_interrupts(sc);
    721  1.3.18.1  rmind 	/* re-enable recv interrupt for console or kgdb port */
    722  1.3.18.1  rmind 	imxuart_enable_debugport(sc);
    723  1.3.18.1  rmind 
    724  1.3.18.1  rmind 	mutex_spin_exit(&sc->sc_lock);
    725  1.3.18.1  rmind 
    726  1.3.18.1  rmind #ifdef	notyet
    727  1.3.18.1  rmind 	if (sc->disable) {
    728  1.3.18.1  rmind #ifdef DIAGNOSTIC
    729  1.3.18.1  rmind 		if (!sc->enabled)
    730  1.3.18.1  rmind 			panic("imxuart_shutdown: not enabled?");
    731  1.3.18.1  rmind #endif
    732  1.3.18.1  rmind 		(*sc->disable)(sc);
    733  1.3.18.1  rmind 		sc->enabled = 0;
    734  1.3.18.1  rmind 	}
    735  1.3.18.1  rmind #endif
    736       1.2   matt }
    737       1.2   matt 
    738       1.2   matt int
    739  1.3.18.1  rmind imxuopen(dev_t dev, int flag, int mode, struct lwp *l)
    740       1.2   matt {
    741  1.3.18.1  rmind 	struct imxuart_softc *sc;
    742  1.3.18.1  rmind 	struct tty *tp;
    743  1.3.18.1  rmind 	int s;
    744  1.3.18.1  rmind 	int error;
    745       1.2   matt 
    746  1.3.18.1  rmind 	sc = device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
    747  1.3.18.1  rmind 	if (sc == NULL || !ISSET(sc->sc_hwflags, IMXUART_HW_DEV_OK) ||
    748  1.3.18.1  rmind 		sc->sc_rbuf == NULL)
    749  1.3.18.1  rmind 		return (ENXIO);
    750  1.3.18.1  rmind 
    751  1.3.18.1  rmind 	if (!device_is_active(sc->sc_dev))
    752  1.3.18.1  rmind 		return (ENXIO);
    753  1.3.18.1  rmind 
    754  1.3.18.1  rmind #ifdef KGDB
    755  1.3.18.1  rmind 	/*
    756  1.3.18.1  rmind 	 * If this is the kgdb port, no other use is permitted.
    757  1.3.18.1  rmind 	 */
    758  1.3.18.1  rmind 	if (ISSET(sc->sc_hwflags, IMXUART_HW_KGDB))
    759  1.3.18.1  rmind 		return (EBUSY);
    760       1.2   matt #endif
    761       1.2   matt 
    762  1.3.18.1  rmind 	tp = sc->sc_tty;
    763       1.2   matt 
    764  1.3.18.1  rmind 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    765  1.3.18.1  rmind 		return (EBUSY);
    766       1.2   matt 
    767  1.3.18.1  rmind 	s = spltty();
    768  1.3.18.1  rmind 
    769  1.3.18.1  rmind 	/*
    770  1.3.18.1  rmind 	 * Do the following iff this is a first open.
    771  1.3.18.1  rmind 	 */
    772  1.3.18.1  rmind 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    773  1.3.18.1  rmind 		struct termios t;
    774  1.3.18.1  rmind 
    775  1.3.18.1  rmind 		tp->t_dev = dev;
    776  1.3.18.1  rmind 
    777  1.3.18.1  rmind 
    778  1.3.18.1  rmind #ifdef notyet
    779  1.3.18.1  rmind 		if (sc->enable) {
    780  1.3.18.1  rmind 			if ((*sc->enable)(sc)) {
    781  1.3.18.1  rmind 				splx(s);
    782  1.3.18.1  rmind 				aprint_error_dev(sc->sc_dev,
    783  1.3.18.1  rmind 				    "device enable failed\n");
    784  1.3.18.1  rmind 				return (EIO);
    785  1.3.18.1  rmind 			}
    786  1.3.18.1  rmind 			sc->enabled = 1;
    787  1.3.18.1  rmind 		}
    788  1.3.18.1  rmind #endif
    789  1.3.18.1  rmind 
    790  1.3.18.1  rmind 		mutex_spin_enter(&sc->sc_lock);
    791  1.3.18.1  rmind 
    792  1.3.18.1  rmind 		imxuart_disable_all_interrupts(sc);
    793  1.3.18.1  rmind 
    794  1.3.18.1  rmind 		/* Fetch the current modem control status, needed later. */
    795  1.3.18.1  rmind 
    796  1.3.18.1  rmind #ifdef	IMXUART_PPS
    797  1.3.18.1  rmind 		/* Clear PPS capture state on first open. */
    798  1.3.18.1  rmind 		mutex_spin_enter(&timecounter_lock);
    799  1.3.18.1  rmind 		memset(&sc->sc_pps_state, 0, sizeof(sc->sc_pps_state));
    800  1.3.18.1  rmind 		sc->sc_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
    801  1.3.18.1  rmind 		pps_init(&sc->sc_pps_state);
    802  1.3.18.1  rmind 		mutex_spin_exit(&timecounter_lock);
    803  1.3.18.1  rmind #endif
    804  1.3.18.1  rmind 
    805  1.3.18.1  rmind 		mutex_spin_exit(&sc->sc_lock);
    806  1.3.18.1  rmind 
    807  1.3.18.1  rmind 		/*
    808  1.3.18.1  rmind 		 * Initialize the termios status to the defaults.  Add in the
    809  1.3.18.1  rmind 		 * sticky bits from TIOCSFLAGS.
    810  1.3.18.1  rmind 		 */
    811  1.3.18.1  rmind 		if (ISSET(sc->sc_hwflags, IMXUART_HW_CONSOLE)) {
    812  1.3.18.1  rmind 			t.c_ospeed = imxuconsrate;
    813  1.3.18.1  rmind 			t.c_cflag = imxuconscflag;
    814  1.3.18.1  rmind 		} else {
    815  1.3.18.1  rmind 			t.c_ospeed = TTYDEF_SPEED;
    816  1.3.18.1  rmind 			t.c_cflag = TTYDEF_CFLAG;
    817  1.3.18.1  rmind 		}
    818  1.3.18.1  rmind 		t.c_ispeed = t.c_ospeed;
    819  1.3.18.1  rmind 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    820  1.3.18.1  rmind 			SET(t.c_cflag, CLOCAL);
    821  1.3.18.1  rmind 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    822  1.3.18.1  rmind 			SET(t.c_cflag, CRTSCTS);
    823  1.3.18.1  rmind 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    824  1.3.18.1  rmind 			SET(t.c_cflag, MDMBUF);
    825  1.3.18.1  rmind 		/* Make sure imxuparam() will do something. */
    826  1.3.18.1  rmind 		tp->t_ospeed = 0;
    827  1.3.18.1  rmind 		(void) imxuparam(tp, &t);
    828  1.3.18.1  rmind 		tp->t_iflag = TTYDEF_IFLAG;
    829  1.3.18.1  rmind 		tp->t_oflag = TTYDEF_OFLAG;
    830  1.3.18.1  rmind 		tp->t_lflag = TTYDEF_LFLAG;
    831  1.3.18.1  rmind 		ttychars(tp);
    832  1.3.18.1  rmind 		ttsetwater(tp);
    833  1.3.18.1  rmind 
    834  1.3.18.1  rmind 		mutex_spin_enter(&sc->sc_lock);
    835  1.3.18.1  rmind 
    836  1.3.18.1  rmind 		/*
    837  1.3.18.1  rmind 		 * Turn on DTR.  We must always do this, even if carrier is not
    838  1.3.18.1  rmind 		 * present, because otherwise we'd have to use TIOCSDTR
    839  1.3.18.1  rmind 		 * immediately after setting CLOCAL, which applications do not
    840  1.3.18.1  rmind 		 * expect.  We always assert DTR while the device is open
    841  1.3.18.1  rmind 		 * unless explicitly requested to deassert it.
    842  1.3.18.1  rmind 		 */
    843  1.3.18.1  rmind 		imxuart_modem(sc, 1);
    844  1.3.18.1  rmind 
    845  1.3.18.1  rmind 		/* Clear the input ring, and unblock. */
    846  1.3.18.1  rmind 		sc->sc_rbuf_in = sc->sc_rbuf_out = 0;
    847  1.3.18.1  rmind 		imxuart_iflush(sc);
    848  1.3.18.1  rmind 		CLR(sc->sc_rx_flags, IMXUART_RX_ANY_BLOCK);
    849  1.3.18.1  rmind 		imxuart_hwiflow(sc);
    850  1.3.18.1  rmind 
    851  1.3.18.1  rmind 		/* Turn on interrupts. */
    852  1.3.18.1  rmind 		imxuart_control_rxint(sc, true);
    853  1.3.18.1  rmind 
    854  1.3.18.1  rmind #ifdef IMXUART_DEBUG
    855  1.3.18.1  rmind 		if (imxuart_debug)
    856  1.3.18.1  rmind 			imxustatus(sc, "imxuopen  ");
    857  1.3.18.1  rmind #endif
    858  1.3.18.1  rmind 
    859  1.3.18.1  rmind 		mutex_spin_exit(&sc->sc_lock);
    860       1.2   matt 	}
    861       1.2   matt 
    862  1.3.18.1  rmind 	splx(s);
    863       1.2   matt 
    864  1.3.18.1  rmind #if 0
    865  1.3.18.1  rmind 	error = ttyopen(tp, IMXUART_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
    866  1.3.18.1  rmind #else
    867  1.3.18.1  rmind 	error = ttyopen(tp, 1, ISSET(flag, O_NONBLOCK));
    868  1.3.18.1  rmind #endif
    869  1.3.18.1  rmind 	if (error)
    870  1.3.18.1  rmind 		goto bad;
    871  1.3.18.1  rmind 
    872  1.3.18.1  rmind 	error = (*tp->t_linesw->l_open)(dev, tp);
    873  1.3.18.1  rmind 	if (error)
    874  1.3.18.1  rmind 		goto bad;
    875  1.3.18.1  rmind 
    876  1.3.18.1  rmind 	return (0);
    877  1.3.18.1  rmind 
    878  1.3.18.1  rmind bad:
    879  1.3.18.1  rmind 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    880  1.3.18.1  rmind 		/*
    881  1.3.18.1  rmind 		 * We failed to open the device, and nobody else had it opened.
    882  1.3.18.1  rmind 		 * Clean up the state as appropriate.
    883  1.3.18.1  rmind 		 */
    884  1.3.18.1  rmind 		imxuart_shutdown(sc);
    885  1.3.18.1  rmind 	}
    886  1.3.18.1  rmind 
    887  1.3.18.1  rmind 	return (error);
    888       1.2   matt }
    889       1.2   matt 
    890  1.3.18.1  rmind int
    891  1.3.18.1  rmind imxuclose(dev_t dev, int flag, int mode, struct lwp *l)
    892       1.2   matt {
    893  1.3.18.1  rmind 	struct imxuart_softc *sc =
    894  1.3.18.1  rmind 	    device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
    895  1.3.18.1  rmind 	struct tty *tp = sc->sc_tty;
    896  1.3.18.1  rmind 
    897  1.3.18.1  rmind 	/* XXX This is for cons.c. */
    898  1.3.18.1  rmind 	if (!ISSET(tp->t_state, TS_ISOPEN))
    899  1.3.18.1  rmind 		return (0);
    900  1.3.18.1  rmind 
    901  1.3.18.1  rmind 	(*tp->t_linesw->l_close)(tp, flag);
    902  1.3.18.1  rmind 	ttyclose(tp);
    903  1.3.18.1  rmind 
    904  1.3.18.1  rmind 	if (IMXUART_ISALIVE(sc) == 0)
    905  1.3.18.1  rmind 		return (0);
    906  1.3.18.1  rmind 
    907  1.3.18.1  rmind 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    908  1.3.18.1  rmind 		/*
    909  1.3.18.1  rmind 		 * Although we got a last close, the device may still be in
    910  1.3.18.1  rmind 		 * use; e.g. if this was the dialout node, and there are still
    911  1.3.18.1  rmind 		 * processes waiting for carrier on the non-dialout node.
    912  1.3.18.1  rmind 		 */
    913  1.3.18.1  rmind 		imxuart_shutdown(sc);
    914       1.2   matt 	}
    915  1.3.18.1  rmind 
    916  1.3.18.1  rmind 	return (0);
    917       1.2   matt }
    918       1.2   matt 
    919  1.3.18.1  rmind int
    920  1.3.18.1  rmind imxuread(dev_t dev, struct uio *uio, int flag)
    921       1.2   matt {
    922  1.3.18.1  rmind 	struct imxuart_softc *sc =
    923  1.3.18.1  rmind 	    device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
    924  1.3.18.1  rmind 	struct tty *tp = sc->sc_tty;
    925       1.2   matt 
    926  1.3.18.1  rmind 	if (IMXUART_ISALIVE(sc) == 0)
    927  1.3.18.1  rmind 		return (EIO);
    928       1.2   matt 
    929  1.3.18.1  rmind 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    930       1.2   matt }
    931       1.2   matt 
    932  1.3.18.1  rmind int
    933  1.3.18.1  rmind imxuwrite(dev_t dev, struct uio *uio, int flag)
    934       1.2   matt {
    935  1.3.18.1  rmind 	struct imxuart_softc *sc =
    936  1.3.18.1  rmind 	    device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
    937  1.3.18.1  rmind 	struct tty *tp = sc->sc_tty;
    938  1.3.18.1  rmind 
    939  1.3.18.1  rmind 	if (IMXUART_ISALIVE(sc) == 0)
    940  1.3.18.1  rmind 		return (EIO);
    941       1.2   matt 
    942  1.3.18.1  rmind 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    943       1.2   matt }
    944       1.2   matt 
    945  1.3.18.1  rmind int
    946  1.3.18.1  rmind imxupoll(dev_t dev, int events, struct lwp *l)
    947       1.2   matt {
    948  1.3.18.1  rmind 	struct imxuart_softc *sc =
    949  1.3.18.1  rmind 	    device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
    950  1.3.18.1  rmind 	struct tty *tp = sc->sc_tty;
    951  1.3.18.1  rmind 
    952  1.3.18.1  rmind 	if (IMXUART_ISALIVE(sc) == 0)
    953  1.3.18.1  rmind 		return (POLLHUP);
    954  1.3.18.1  rmind 
    955  1.3.18.1  rmind 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    956       1.2   matt }
    957       1.2   matt 
    958  1.3.18.1  rmind struct tty *
    959  1.3.18.1  rmind imxutty(dev_t dev)
    960       1.2   matt {
    961  1.3.18.1  rmind 	struct imxuart_softc *sc =
    962  1.3.18.1  rmind 	    device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
    963  1.3.18.1  rmind 	struct tty *tp = sc->sc_tty;
    964  1.3.18.1  rmind 
    965  1.3.18.1  rmind 	return (tp);
    966       1.2   matt }
    967       1.2   matt 
    968  1.3.18.1  rmind int
    969  1.3.18.1  rmind imxuioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    970       1.2   matt {
    971  1.3.18.1  rmind 	struct imxuart_softc *sc;
    972  1.3.18.1  rmind 	struct tty *tp;
    973  1.3.18.1  rmind 	int error;
    974  1.3.18.1  rmind 
    975  1.3.18.1  rmind 	sc = device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
    976  1.3.18.1  rmind 	if (sc == NULL)
    977  1.3.18.1  rmind 		return ENXIO;
    978  1.3.18.1  rmind 	if (IMXUART_ISALIVE(sc) == 0)
    979  1.3.18.1  rmind 		return (EIO);
    980  1.3.18.1  rmind 
    981  1.3.18.1  rmind 	tp = sc->sc_tty;
    982  1.3.18.1  rmind 
    983  1.3.18.1  rmind 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    984  1.3.18.1  rmind 	if (error != EPASSTHROUGH)
    985  1.3.18.1  rmind 		return (error);
    986  1.3.18.1  rmind 
    987  1.3.18.1  rmind 	error = ttioctl(tp, cmd, data, flag, l);
    988  1.3.18.1  rmind 	if (error != EPASSTHROUGH)
    989  1.3.18.1  rmind 		return (error);
    990  1.3.18.1  rmind 
    991  1.3.18.1  rmind 	error = 0;
    992  1.3.18.1  rmind 	switch (cmd) {
    993  1.3.18.1  rmind 	case TIOCSFLAGS:
    994  1.3.18.1  rmind 		error = kauth_authorize_device_tty(l->l_cred,
    995  1.3.18.1  rmind 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    996  1.3.18.1  rmind 		break;
    997  1.3.18.1  rmind 	default:
    998  1.3.18.1  rmind 		/* nothing */
    999  1.3.18.1  rmind 		break;
   1000  1.3.18.1  rmind 	}
   1001  1.3.18.1  rmind 	if (error) {
   1002  1.3.18.1  rmind 		return error;
   1003  1.3.18.1  rmind 	}
   1004  1.3.18.1  rmind 
   1005  1.3.18.1  rmind 	mutex_spin_enter(&sc->sc_lock);
   1006  1.3.18.1  rmind 
   1007  1.3.18.1  rmind 	switch (cmd) {
   1008  1.3.18.1  rmind 	case TIOCSBRK:
   1009  1.3.18.1  rmind 		imxuart_break(sc, true);
   1010  1.3.18.1  rmind 		break;
   1011  1.3.18.1  rmind 
   1012  1.3.18.1  rmind 	case TIOCCBRK:
   1013  1.3.18.1  rmind 		imxuart_break(sc, false);
   1014  1.3.18.1  rmind 		break;
   1015  1.3.18.1  rmind 
   1016  1.3.18.1  rmind 	case TIOCSDTR:
   1017  1.3.18.1  rmind 		imxuart_modem(sc, 1);
   1018  1.3.18.1  rmind 		break;
   1019  1.3.18.1  rmind 
   1020  1.3.18.1  rmind 	case TIOCCDTR:
   1021  1.3.18.1  rmind 		imxuart_modem(sc, 0);
   1022  1.3.18.1  rmind 		break;
   1023  1.3.18.1  rmind 
   1024  1.3.18.1  rmind 	case TIOCGFLAGS:
   1025  1.3.18.1  rmind 		*(int *)data = sc->sc_swflags;
   1026  1.3.18.1  rmind 		break;
   1027  1.3.18.1  rmind 
   1028  1.3.18.1  rmind 	case TIOCSFLAGS:
   1029  1.3.18.1  rmind 		sc->sc_swflags = *(int *)data;
   1030  1.3.18.1  rmind 		break;
   1031  1.3.18.1  rmind 
   1032  1.3.18.1  rmind 	case TIOCMSET:
   1033  1.3.18.1  rmind 	case TIOCMBIS:
   1034  1.3.18.1  rmind 	case TIOCMBIC:
   1035  1.3.18.1  rmind 		tiocm_to_imxu(sc, cmd, *(int *)data);
   1036  1.3.18.1  rmind 		break;
   1037  1.3.18.1  rmind 
   1038  1.3.18.1  rmind 	case TIOCMGET:
   1039  1.3.18.1  rmind 		*(int *)data = imxuart_to_tiocm(sc);
   1040  1.3.18.1  rmind 		break;
   1041  1.3.18.1  rmind 
   1042  1.3.18.1  rmind #ifdef notyet
   1043  1.3.18.1  rmind 	case PPS_IOC_CREATE:
   1044  1.3.18.1  rmind 	case PPS_IOC_DESTROY:
   1045  1.3.18.1  rmind 	case PPS_IOC_GETPARAMS:
   1046  1.3.18.1  rmind 	case PPS_IOC_SETPARAMS:
   1047  1.3.18.1  rmind 	case PPS_IOC_GETCAP:
   1048  1.3.18.1  rmind 	case PPS_IOC_FETCH:
   1049  1.3.18.1  rmind #ifdef PPS_SYNC
   1050  1.3.18.1  rmind 	case PPS_IOC_KCBIND:
   1051  1.3.18.1  rmind #endif
   1052  1.3.18.1  rmind 		mutex_spin_enter(&timecounter_lock);
   1053  1.3.18.1  rmind 		error = pps_ioctl(cmd, data, &sc->sc_pps_state);
   1054  1.3.18.1  rmind 		mutex_spin_exit(&timecounter_lock);
   1055  1.3.18.1  rmind 		break;
   1056  1.3.18.1  rmind 
   1057  1.3.18.1  rmind 	case TIOCDCDTIMESTAMP:	/* XXX old, overloaded  API used by xntpd v3 */
   1058  1.3.18.1  rmind 		mutex_spin_enter(&timecounter_lock);
   1059  1.3.18.1  rmind #ifndef PPS_TRAILING_EDGE
   1060  1.3.18.1  rmind 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1061  1.3.18.1  rmind 		    &sc->sc_pps_state.ppsinfo.assert_timestamp);
   1062  1.3.18.1  rmind #else
   1063  1.3.18.1  rmind 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1064  1.3.18.1  rmind 		    &sc->sc_pps_state.ppsinfo.clear_timestamp);
   1065  1.3.18.1  rmind #endif
   1066  1.3.18.1  rmind 		mutex_spin_exit(&timecounter_lock);
   1067  1.3.18.1  rmind 		break;
   1068  1.3.18.1  rmind #endif
   1069  1.3.18.1  rmind 
   1070  1.3.18.1  rmind 	default:
   1071  1.3.18.1  rmind 		error = EPASSTHROUGH;
   1072  1.3.18.1  rmind 		break;
   1073  1.3.18.1  rmind 	}
   1074  1.3.18.1  rmind 
   1075  1.3.18.1  rmind 	mutex_spin_exit(&sc->sc_lock);
   1076  1.3.18.1  rmind 
   1077  1.3.18.1  rmind #ifdef IMXUART_DEBUG
   1078  1.3.18.1  rmind 	if (imxuart_debug)
   1079  1.3.18.1  rmind 		imxustatus(sc, "imxuioctl ");
   1080  1.3.18.1  rmind #endif
   1081  1.3.18.1  rmind 
   1082  1.3.18.1  rmind 	return (error);
   1083       1.2   matt }
   1084       1.2   matt 
   1085  1.3.18.1  rmind integrate void
   1086  1.3.18.1  rmind imxuart_schedrx(struct imxuart_softc *sc)
   1087       1.2   matt {
   1088  1.3.18.1  rmind 	sc->sc_rx_ready = 1;
   1089  1.3.18.1  rmind 
   1090  1.3.18.1  rmind 	/* Wake up the poller. */
   1091  1.3.18.1  rmind 	softint_schedule(sc->sc_si);
   1092       1.2   matt }
   1093       1.2   matt 
   1094  1.3.18.1  rmind void
   1095  1.3.18.1  rmind imxuart_break(struct imxuart_softc *sc, bool onoff)
   1096       1.2   matt {
   1097  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   1098  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   1099       1.2   matt 
   1100  1.3.18.1  rmind 	if (onoff)
   1101  1.3.18.1  rmind 		SET(sc->sc_ucr1, IMX_UCR1_SNDBRK);
   1102  1.3.18.1  rmind 	else
   1103  1.3.18.1  rmind 		CLR(sc->sc_ucr1, IMX_UCR1_SNDBRK);
   1104  1.3.18.1  rmind 
   1105  1.3.18.1  rmind 	bus_space_write_4(iot, ioh, IMX_UCR1, sc->sc_ucr1);
   1106       1.2   matt }
   1107       1.2   matt 
   1108       1.2   matt void
   1109  1.3.18.1  rmind imxuart_modem(struct imxuart_softc *sc, int onoff)
   1110       1.2   matt {
   1111  1.3.18.1  rmind #ifdef notyet
   1112  1.3.18.1  rmind 	if (sc->sc_mcr_dtr == 0)
   1113  1.3.18.1  rmind 		return;
   1114  1.3.18.1  rmind 
   1115  1.3.18.1  rmind 	if (onoff)
   1116  1.3.18.1  rmind 		SET(sc->sc_mcr, sc->sc_mcr_dtr);
   1117  1.3.18.1  rmind 	else
   1118  1.3.18.1  rmind 		CLR(sc->sc_mcr, sc->sc_mcr_dtr);
   1119  1.3.18.1  rmind 
   1120  1.3.18.1  rmind 	if (!sc->sc_heldchange) {
   1121  1.3.18.1  rmind 		if (sc->sc_tx_busy) {
   1122  1.3.18.1  rmind 			sc->sc_heldtbc = sc->sc_tbc;
   1123  1.3.18.1  rmind 			sc->sc_tbc = 0;
   1124  1.3.18.1  rmind 			sc->sc_heldchange = 1;
   1125  1.3.18.1  rmind 		} else
   1126  1.3.18.1  rmind 			imxuart_loadchannelregs(sc);
   1127  1.3.18.1  rmind 	}
   1128  1.3.18.1  rmind #endif
   1129       1.2   matt }
   1130       1.2   matt 
   1131  1.3.18.1  rmind /*
   1132  1.3.18.1  rmind  * RTS output is controlled by UCR2.CTS bit.
   1133  1.3.18.1  rmind  * DTR output is controlled by UCR3.DSR bit.
   1134  1.3.18.1  rmind  * (i.MX reference manual uses names in DCE mode)
   1135  1.3.18.1  rmind  *
   1136  1.3.18.1  rmind  * note: if UCR2.CTSC == 1 for automatic HW flow control, UCR2.CTS is ignored.
   1137  1.3.18.1  rmind  */
   1138       1.2   matt void
   1139  1.3.18.1  rmind tiocm_to_imxu(struct imxuart_softc *sc, u_long how, int ttybits)
   1140       1.2   matt {
   1141  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   1142  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   1143  1.3.18.1  rmind 
   1144  1.3.18.1  rmind 	uint32_t ucr2 = sc->sc_ucr2_d;
   1145  1.3.18.1  rmind 	uint32_t ucr3 = sc->sc_ucr3;
   1146  1.3.18.1  rmind 
   1147  1.3.18.1  rmind 	uint32_t ucr2_mask = 0;
   1148  1.3.18.1  rmind 	uint32_t ucr3_mask = 0;
   1149  1.3.18.1  rmind 
   1150  1.3.18.1  rmind 
   1151  1.3.18.1  rmind 	if (ISSET(ttybits, TIOCM_DTR))
   1152  1.3.18.1  rmind 		ucr3_mask = IMX_UCR3_DSR;
   1153  1.3.18.1  rmind 	if (ISSET(ttybits, TIOCM_RTS))
   1154  1.3.18.1  rmind 		ucr2_mask = IMX_UCR2_CTS;
   1155  1.3.18.1  rmind 
   1156  1.3.18.1  rmind 	switch (how) {
   1157  1.3.18.1  rmind 	case TIOCMBIC:
   1158  1.3.18.1  rmind 		CLR(ucr2, ucr2_mask);
   1159  1.3.18.1  rmind 		CLR(ucr3, ucr3_mask);
   1160  1.3.18.1  rmind 		break;
   1161  1.3.18.1  rmind 
   1162  1.3.18.1  rmind 	case TIOCMBIS:
   1163  1.3.18.1  rmind 		SET(ucr2, ucr2_mask);
   1164  1.3.18.1  rmind 		SET(ucr3, ucr3_mask);
   1165  1.3.18.1  rmind 		break;
   1166  1.3.18.1  rmind 
   1167  1.3.18.1  rmind 	case TIOCMSET:
   1168  1.3.18.1  rmind 		CLR(ucr2, ucr2_mask);
   1169  1.3.18.1  rmind 		CLR(ucr3, ucr3_mask);
   1170  1.3.18.1  rmind 		SET(ucr2, ucr2_mask);
   1171  1.3.18.1  rmind 		SET(ucr3, ucr3_mask);
   1172  1.3.18.1  rmind 		break;
   1173  1.3.18.1  rmind 	}
   1174  1.3.18.1  rmind 
   1175  1.3.18.1  rmind 	if (ucr3 != sc->sc_ucr3) {
   1176  1.3.18.1  rmind 		bus_space_write_4(iot, ioh, IMX_UCR3, ucr3);
   1177  1.3.18.1  rmind 		sc->sc_ucr3 = ucr3;
   1178  1.3.18.1  rmind 	}
   1179  1.3.18.1  rmind 
   1180  1.3.18.1  rmind 	if (ucr2 == sc->sc_ucr2_d)
   1181  1.3.18.1  rmind 		return;
   1182  1.3.18.1  rmind 
   1183  1.3.18.1  rmind 	sc->sc_ucr2_d = ucr2;
   1184  1.3.18.1  rmind 	/* update CTS bit only */
   1185  1.3.18.1  rmind 	ucr2 = (sc->sc_ucr2 & ~IMX_UCR2_CTS) |
   1186  1.3.18.1  rmind 	    (ucr2 & IMX_UCR2_CTS);
   1187  1.3.18.1  rmind 
   1188  1.3.18.1  rmind 	bus_space_write_4(iot, ioh, IMX_UCR2, ucr2);
   1189  1.3.18.1  rmind 	sc->sc_ucr2 = ucr2;
   1190       1.2   matt }
   1191  1.3.18.1  rmind 
   1192       1.2   matt int
   1193  1.3.18.1  rmind imxuart_to_tiocm(struct imxuart_softc *sc)
   1194       1.2   matt {
   1195  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   1196  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   1197  1.3.18.1  rmind 	int ttybits = 0;
   1198  1.3.18.1  rmind 	uint32_t usr[2];
   1199  1.3.18.1  rmind 
   1200  1.3.18.1  rmind 	if (ISSET(sc->sc_ucr3, IMX_UCR3_DSR))
   1201  1.3.18.1  rmind 		SET(ttybits, TIOCM_DTR);
   1202  1.3.18.1  rmind 	if (ISSET(sc->sc_ucr2, IMX_UCR2_CTS))
   1203  1.3.18.1  rmind 		SET(ttybits, TIOCM_RTS);
   1204  1.3.18.1  rmind 
   1205  1.3.18.1  rmind 	bus_space_read_region_4(iot, ioh, IMX_USR1, usr, 2);
   1206  1.3.18.1  rmind 
   1207  1.3.18.1  rmind 	if (ISSET(usr[0], IMX_USR1_RTSS))
   1208  1.3.18.1  rmind 		SET(ttybits, TIOCM_CTS);
   1209  1.3.18.1  rmind 
   1210  1.3.18.1  rmind 	if (ISSET(usr[1], IMX_USR2_DCDIN))
   1211  1.3.18.1  rmind 		SET(ttybits, TIOCM_CD);
   1212  1.3.18.1  rmind 
   1213  1.3.18.1  rmind #if 0
   1214  1.3.18.1  rmind 	/* XXXbsh: I couldn't find the way to read ipp_uart_dsr_dte_i signal,
   1215  1.3.18.1  rmind 	   although there are bits in UART registers to detect delta of DSR.
   1216  1.3.18.1  rmind 	*/
   1217  1.3.18.1  rmind 	if (ISSET(imxubits, MSR_DSR))
   1218  1.3.18.1  rmind 		SET(ttybits, TIOCM_DSR);
   1219  1.3.18.1  rmind #endif
   1220  1.3.18.1  rmind 
   1221  1.3.18.1  rmind 	if (ISSET(usr[1], IMX_USR2_RIIN))
   1222  1.3.18.1  rmind 		SET(ttybits, TIOCM_RI);
   1223  1.3.18.1  rmind 
   1224  1.3.18.1  rmind 
   1225  1.3.18.1  rmind #ifdef	notyet
   1226  1.3.18.1  rmind 	if (ISSET(sc->sc_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC))
   1227  1.3.18.1  rmind 		SET(ttybits, TIOCM_LE);
   1228  1.3.18.1  rmind #endif
   1229  1.3.18.1  rmind 
   1230  1.3.18.1  rmind 	return (ttybits);
   1231  1.3.18.1  rmind }
   1232  1.3.18.1  rmind 
   1233  1.3.18.1  rmind static uint32_t
   1234  1.3.18.1  rmind cflag_to_ucr2(tcflag_t cflag, uint32_t oldval)
   1235  1.3.18.1  rmind {
   1236  1.3.18.1  rmind 	uint32_t val = oldval;
   1237  1.3.18.1  rmind 
   1238  1.3.18.1  rmind 	CLR(val,IMX_UCR2_WS|IMX_UCR2_PREN|IMX_UCR2_PROE|IMX_UCR2_STPB);
   1239  1.3.18.1  rmind 
   1240  1.3.18.1  rmind 	switch (cflag & CSIZE) {
   1241  1.3.18.1  rmind 	case CS5:
   1242  1.3.18.1  rmind 	case CS6:
   1243  1.3.18.1  rmind 		/* not suppreted. use 7-bits */
   1244  1.3.18.1  rmind 	case CS7:
   1245  1.3.18.1  rmind 		break;
   1246  1.3.18.1  rmind 	case CS8:
   1247  1.3.18.1  rmind 		SET(val, IMX_UCR2_WS);
   1248  1.3.18.1  rmind 		break;
   1249  1.3.18.1  rmind 	}
   1250  1.3.18.1  rmind 
   1251  1.3.18.1  rmind 
   1252  1.3.18.1  rmind 	if (ISSET(cflag, PARENB)) {
   1253  1.3.18.1  rmind 		SET(val, IMX_UCR2_PREN);
   1254  1.3.18.1  rmind 
   1255  1.3.18.1  rmind 		/* odd parity */
   1256  1.3.18.1  rmind 		if (!ISSET(cflag, PARODD))
   1257  1.3.18.1  rmind 			SET(val, IMX_UCR2_PROE);
   1258  1.3.18.1  rmind 	}
   1259  1.3.18.1  rmind 
   1260  1.3.18.1  rmind 	if (ISSET(cflag, CSTOPB))
   1261  1.3.18.1  rmind 		SET(val, IMX_UCR2_STPB);
   1262  1.3.18.1  rmind 
   1263  1.3.18.1  rmind 	val |= IMX_UCR2_TXEN| IMX_UCR2_RXEN|IMX_UCR2_SRST;
   1264  1.3.18.1  rmind 
   1265  1.3.18.1  rmind 	return val;
   1266       1.2   matt }
   1267       1.2   matt 
   1268  1.3.18.1  rmind int
   1269  1.3.18.1  rmind imxuparam(struct tty *tp, struct termios *t)
   1270  1.3.18.1  rmind {
   1271  1.3.18.1  rmind 	struct imxuart_softc *sc =
   1272  1.3.18.1  rmind 	    device_lookup_private(&imxuart_cd, IMXUART_UNIT(tp->t_dev));
   1273  1.3.18.1  rmind 	struct imxuart_baudrate_ratio ratio;
   1274  1.3.18.1  rmind 	uint32_t ucr2;
   1275  1.3.18.1  rmind 	bool change_speed = tp->t_ospeed != t->c_ospeed;
   1276  1.3.18.1  rmind 
   1277  1.3.18.1  rmind 	if (IMXUART_ISALIVE(sc) == 0)
   1278  1.3.18.1  rmind 		return (EIO);
   1279  1.3.18.1  rmind 
   1280  1.3.18.1  rmind 	/* Check requested parameters. */
   1281  1.3.18.1  rmind 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
   1282  1.3.18.1  rmind 		return (EINVAL);
   1283  1.3.18.1  rmind 
   1284  1.3.18.1  rmind 	/*
   1285  1.3.18.1  rmind 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1286  1.3.18.1  rmind 	 * is always active.
   1287  1.3.18.1  rmind 	 */
   1288  1.3.18.1  rmind 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
   1289  1.3.18.1  rmind 	    ISSET(sc->sc_hwflags, IMXUART_HW_CONSOLE)) {
   1290  1.3.18.1  rmind 		SET(t->c_cflag, CLOCAL);
   1291  1.3.18.1  rmind 		CLR(t->c_cflag, HUPCL);
   1292  1.3.18.1  rmind 	}
   1293  1.3.18.1  rmind 
   1294  1.3.18.1  rmind 	/*
   1295  1.3.18.1  rmind 	 * If there were no changes, don't do anything.  This avoids dropping
   1296  1.3.18.1  rmind 	 * input and improves performance when all we did was frob things like
   1297  1.3.18.1  rmind 	 * VMIN and VTIME.
   1298  1.3.18.1  rmind 	 */
   1299  1.3.18.1  rmind 	if ( !change_speed && tp->t_cflag == t->c_cflag)
   1300  1.3.18.1  rmind 		return (0);
   1301  1.3.18.1  rmind 
   1302  1.3.18.1  rmind 	if (change_speed) {
   1303  1.3.18.1  rmind 		/* calculate baudrate modulator value */
   1304  1.3.18.1  rmind 		if (imxuspeed(t->c_ospeed, &ratio) < 0)
   1305  1.3.18.1  rmind 			return (EINVAL);
   1306  1.3.18.1  rmind 		sc->sc_ratio = ratio;
   1307  1.3.18.1  rmind 	}
   1308  1.3.18.1  rmind 
   1309  1.3.18.1  rmind 	ucr2 = cflag_to_ucr2(t->c_cflag, sc->sc_ucr2_d);
   1310  1.3.18.1  rmind 
   1311  1.3.18.1  rmind 	mutex_spin_enter(&sc->sc_lock);
   1312  1.3.18.1  rmind 
   1313  1.3.18.1  rmind #if 0	/* flow control stuff.  not yet */
   1314  1.3.18.1  rmind 	/*
   1315  1.3.18.1  rmind 	 * If we're not in a mode that assumes a connection is present, then
   1316  1.3.18.1  rmind 	 * ignore carrier changes.
   1317  1.3.18.1  rmind 	 */
   1318  1.3.18.1  rmind 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
   1319  1.3.18.1  rmind 		sc->sc_msr_dcd = 0;
   1320  1.3.18.1  rmind 	else
   1321  1.3.18.1  rmind 		sc->sc_msr_dcd = MSR_DCD;
   1322  1.3.18.1  rmind 	/*
   1323  1.3.18.1  rmind 	 * Set the flow control pins depending on the current flow control
   1324  1.3.18.1  rmind 	 * mode.
   1325  1.3.18.1  rmind 	 */
   1326  1.3.18.1  rmind 	if (ISSET(t->c_cflag, CRTSCTS)) {
   1327  1.3.18.1  rmind 		sc->sc_mcr_dtr = MCR_DTR;
   1328  1.3.18.1  rmind 		sc->sc_mcr_rts = MCR_RTS;
   1329  1.3.18.1  rmind 		sc->sc_msr_cts = MSR_CTS;
   1330  1.3.18.1  rmind 		sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
   1331  1.3.18.1  rmind 	} else if (ISSET(t->c_cflag, MDMBUF)) {
   1332  1.3.18.1  rmind 		/*
   1333  1.3.18.1  rmind 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
   1334  1.3.18.1  rmind 		 * carrier detection.
   1335  1.3.18.1  rmind 		 */
   1336  1.3.18.1  rmind 		sc->sc_mcr_dtr = 0;
   1337  1.3.18.1  rmind 		sc->sc_mcr_rts = MCR_DTR;
   1338  1.3.18.1  rmind 		sc->sc_msr_cts = MSR_DCD;
   1339  1.3.18.1  rmind 		sc->sc_efr = 0;
   1340  1.3.18.1  rmind 	} else {
   1341  1.3.18.1  rmind 		/*
   1342  1.3.18.1  rmind 		 * If no flow control, then always set RTS.  This will make
   1343  1.3.18.1  rmind 		 * the other side happy if it mistakenly thinks we're doing
   1344  1.3.18.1  rmind 		 * RTS/CTS flow control.
   1345  1.3.18.1  rmind 		 */
   1346  1.3.18.1  rmind 		sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
   1347  1.3.18.1  rmind 		sc->sc_mcr_rts = 0;
   1348  1.3.18.1  rmind 		sc->sc_msr_cts = 0;
   1349  1.3.18.1  rmind 		sc->sc_efr = 0;
   1350  1.3.18.1  rmind 		if (ISSET(sc->sc_mcr, MCR_DTR))
   1351  1.3.18.1  rmind 			SET(sc->sc_mcr, MCR_RTS);
   1352  1.3.18.1  rmind 		else
   1353  1.3.18.1  rmind 			CLR(sc->sc_mcr, MCR_RTS);
   1354  1.3.18.1  rmind 	}
   1355  1.3.18.1  rmind 	sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
   1356  1.3.18.1  rmind #endif
   1357  1.3.18.1  rmind 
   1358  1.3.18.1  rmind 	/* And copy to tty. */
   1359  1.3.18.1  rmind 	tp->t_ispeed = t->c_ospeed;
   1360  1.3.18.1  rmind 	tp->t_ospeed = t->c_ospeed;
   1361  1.3.18.1  rmind 	tp->t_cflag = t->c_cflag;
   1362  1.3.18.1  rmind 
   1363  1.3.18.1  rmind 	if (!change_speed && ucr2 == sc->sc_ucr2_d) {
   1364  1.3.18.1  rmind 		/* noop */
   1365  1.3.18.1  rmind 	}
   1366  1.3.18.1  rmind 	else if (!sc->sc_pending && !sc->sc_tx_busy) {
   1367  1.3.18.1  rmind 		if (ucr2 != sc->sc_ucr2_d) {
   1368  1.3.18.1  rmind 			sc->sc_ucr2_d = ucr2;
   1369  1.3.18.1  rmind 			imxuart_load_params(sc);
   1370  1.3.18.1  rmind 		}
   1371  1.3.18.1  rmind 		if (change_speed)
   1372  1.3.18.1  rmind 			imxuart_load_speed(sc);
   1373  1.3.18.1  rmind 	}
   1374  1.3.18.1  rmind 	else {
   1375  1.3.18.1  rmind 		if (!sc->sc_pending) {
   1376  1.3.18.1  rmind 			sc->sc_heldtbc = sc->sc_tbc;
   1377  1.3.18.1  rmind 			sc->sc_tbc = 0;
   1378  1.3.18.1  rmind 		}
   1379  1.3.18.1  rmind 		sc->sc_pending |=
   1380  1.3.18.1  rmind 		    (ucr2 == sc->sc_ucr2_d ? 0 : IMXUART_PEND_PARAM) |
   1381  1.3.18.1  rmind 		    (change_speed ? 0 : IMXUART_PEND_SPEED);
   1382  1.3.18.1  rmind 		sc->sc_ucr2_d = ucr2;
   1383  1.3.18.1  rmind 	}
   1384  1.3.18.1  rmind 
   1385  1.3.18.1  rmind 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1386  1.3.18.1  rmind 		/* Disable the high water mark. */
   1387  1.3.18.1  rmind 		sc->sc_r_hiwat = 0;
   1388  1.3.18.1  rmind 		sc->sc_r_lowat = 0;
   1389  1.3.18.1  rmind 		if (ISSET(sc->sc_rx_flags, IMXUART_RX_TTY_OVERFLOWED)) {
   1390  1.3.18.1  rmind 			CLR(sc->sc_rx_flags, IMXUART_RX_TTY_OVERFLOWED);
   1391  1.3.18.1  rmind 			imxuart_schedrx(sc);
   1392  1.3.18.1  rmind 		}
   1393  1.3.18.1  rmind 		if (ISSET(sc->sc_rx_flags,
   1394  1.3.18.1  rmind 			IMXUART_RX_TTY_BLOCKED|IMXUART_RX_IBUF_BLOCKED)) {
   1395  1.3.18.1  rmind 			CLR(sc->sc_rx_flags,
   1396  1.3.18.1  rmind 			    IMXUART_RX_TTY_BLOCKED|IMXUART_RX_IBUF_BLOCKED);
   1397  1.3.18.1  rmind 			imxuart_hwiflow(sc);
   1398  1.3.18.1  rmind 		}
   1399  1.3.18.1  rmind 	} else {
   1400  1.3.18.1  rmind 		sc->sc_r_hiwat = imxuart_rbuf_hiwat;
   1401  1.3.18.1  rmind 		sc->sc_r_lowat = imxuart_rbuf_lowat;
   1402  1.3.18.1  rmind 	}
   1403  1.3.18.1  rmind 
   1404  1.3.18.1  rmind 	mutex_spin_exit(&sc->sc_lock);
   1405  1.3.18.1  rmind 
   1406  1.3.18.1  rmind #if 0
   1407  1.3.18.1  rmind 	/*
   1408  1.3.18.1  rmind 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1409  1.3.18.1  rmind 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1410  1.3.18.1  rmind 	 * explicit request.
   1411  1.3.18.1  rmind 	 */
   1412  1.3.18.1  rmind 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
   1413  1.3.18.1  rmind #else
   1414  1.3.18.1  rmind 	/* XXX: always report that we have DCD */
   1415  1.3.18.1  rmind 	(void) (*tp->t_linesw->l_modem)(tp, 1);
   1416  1.3.18.1  rmind #endif
   1417  1.3.18.1  rmind 
   1418  1.3.18.1  rmind #ifdef IMXUART_DEBUG
   1419  1.3.18.1  rmind 	if (imxuart_debug)
   1420  1.3.18.1  rmind 		imxustatus(sc, "imxuparam ");
   1421  1.3.18.1  rmind #endif
   1422  1.3.18.1  rmind 
   1423  1.3.18.1  rmind 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1424  1.3.18.1  rmind 		if (sc->sc_tx_stopped) {
   1425  1.3.18.1  rmind 			sc->sc_tx_stopped = 0;
   1426  1.3.18.1  rmind 			imxustart(tp);
   1427  1.3.18.1  rmind 		}
   1428  1.3.18.1  rmind 	}
   1429  1.3.18.1  rmind 
   1430  1.3.18.1  rmind 	return (0);
   1431  1.3.18.1  rmind }
   1432  1.3.18.1  rmind 
   1433  1.3.18.1  rmind void
   1434  1.3.18.1  rmind imxuart_iflush(struct imxuart_softc *sc)
   1435  1.3.18.1  rmind {
   1436  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   1437  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   1438  1.3.18.1  rmind #ifdef DIAGNOSTIC
   1439  1.3.18.1  rmind 	uint32_t reg = 0xffff;
   1440  1.3.18.1  rmind #endif
   1441  1.3.18.1  rmind 	int timo;
   1442  1.3.18.1  rmind 
   1443  1.3.18.1  rmind 	timo = 50000;
   1444  1.3.18.1  rmind 	/* flush any pending I/O */
   1445  1.3.18.1  rmind 	while (ISSET(bus_space_read_4(iot, ioh, IMX_USR2), IMX_USR2_RDR)
   1446  1.3.18.1  rmind 	    && --timo)
   1447  1.3.18.1  rmind #ifdef DIAGNOSTIC
   1448  1.3.18.1  rmind 		reg =
   1449  1.3.18.1  rmind #else
   1450  1.3.18.1  rmind 		    (void)
   1451  1.3.18.1  rmind #endif
   1452  1.3.18.1  rmind 		    bus_space_read_4(iot, ioh, IMX_URXD);
   1453  1.3.18.1  rmind #ifdef DIAGNOSTIC
   1454  1.3.18.1  rmind 	if (!timo)
   1455  1.3.18.1  rmind 		aprint_error_dev(sc->sc_dev, "imxuart_iflush timeout %02x\n", reg);
   1456  1.3.18.1  rmind #endif
   1457  1.3.18.1  rmind }
   1458  1.3.18.1  rmind 
   1459  1.3.18.1  rmind int
   1460  1.3.18.1  rmind imxuhwiflow(struct tty *tp, int block)
   1461  1.3.18.1  rmind {
   1462  1.3.18.1  rmind 	struct imxuart_softc *sc =
   1463  1.3.18.1  rmind 	    device_lookup_private(&imxuart_cd, IMXUART_UNIT(tp->t_dev));
   1464  1.3.18.1  rmind 
   1465  1.3.18.1  rmind 	if (IMXUART_ISALIVE(sc) == 0)
   1466  1.3.18.1  rmind 		return (0);
   1467  1.3.18.1  rmind 
   1468  1.3.18.1  rmind #ifdef notyet
   1469  1.3.18.1  rmind 	if (sc->sc_mcr_rts == 0)
   1470  1.3.18.1  rmind 		return (0);
   1471  1.3.18.1  rmind #endif
   1472  1.3.18.1  rmind 
   1473  1.3.18.1  rmind 	mutex_spin_enter(&sc->sc_lock);
   1474  1.3.18.1  rmind 
   1475  1.3.18.1  rmind 	if (block) {
   1476  1.3.18.1  rmind 		if (!ISSET(sc->sc_rx_flags, IMXUART_RX_TTY_BLOCKED)) {
   1477  1.3.18.1  rmind 			SET(sc->sc_rx_flags, IMXUART_RX_TTY_BLOCKED);
   1478  1.3.18.1  rmind 			imxuart_hwiflow(sc);
   1479  1.3.18.1  rmind 		}
   1480  1.3.18.1  rmind 	} else {
   1481  1.3.18.1  rmind 		if (ISSET(sc->sc_rx_flags, IMXUART_RX_TTY_OVERFLOWED)) {
   1482  1.3.18.1  rmind 			CLR(sc->sc_rx_flags, IMXUART_RX_TTY_OVERFLOWED);
   1483  1.3.18.1  rmind 			imxuart_schedrx(sc);
   1484  1.3.18.1  rmind 		}
   1485  1.3.18.1  rmind 		if (ISSET(sc->sc_rx_flags, IMXUART_RX_TTY_BLOCKED)) {
   1486  1.3.18.1  rmind 			CLR(sc->sc_rx_flags, IMXUART_RX_TTY_BLOCKED);
   1487  1.3.18.1  rmind 			imxuart_hwiflow(sc);
   1488  1.3.18.1  rmind 		}
   1489  1.3.18.1  rmind 	}
   1490  1.3.18.1  rmind 
   1491  1.3.18.1  rmind 	mutex_spin_exit(&sc->sc_lock);
   1492  1.3.18.1  rmind 	return (1);
   1493  1.3.18.1  rmind }
   1494  1.3.18.1  rmind 
   1495  1.3.18.1  rmind /*
   1496  1.3.18.1  rmind  * (un)block input via hw flowcontrol
   1497  1.3.18.1  rmind  */
   1498  1.3.18.1  rmind void
   1499  1.3.18.1  rmind imxuart_hwiflow(struct imxuart_softc *sc)
   1500  1.3.18.1  rmind {
   1501  1.3.18.1  rmind #ifdef notyet
   1502  1.3.18.1  rmind 	struct imxuart_regs *regsp= &sc->sc_regs;
   1503  1.3.18.1  rmind 
   1504  1.3.18.1  rmind 	if (sc->sc_mcr_rts == 0)
   1505  1.3.18.1  rmind 		return;
   1506  1.3.18.1  rmind 
   1507  1.3.18.1  rmind 	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
   1508  1.3.18.1  rmind 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
   1509  1.3.18.1  rmind 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
   1510  1.3.18.1  rmind 	} else {
   1511  1.3.18.1  rmind 		SET(sc->sc_mcr, sc->sc_mcr_rts);
   1512  1.3.18.1  rmind 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
   1513  1.3.18.1  rmind 	}
   1514  1.3.18.1  rmind 	UR_WRITE_1(regsp, IMXUART_REG_MCR, sc->sc_mcr_active);
   1515  1.3.18.1  rmind #endif
   1516  1.3.18.1  rmind }
   1517  1.3.18.1  rmind 
   1518  1.3.18.1  rmind 
   1519  1.3.18.1  rmind void
   1520  1.3.18.1  rmind imxustart(struct tty *tp)
   1521  1.3.18.1  rmind {
   1522  1.3.18.1  rmind 	struct imxuart_softc *sc =
   1523  1.3.18.1  rmind 	    device_lookup_private(&imxuart_cd, IMXUART_UNIT(tp->t_dev));
   1524  1.3.18.1  rmind 	int s;
   1525  1.3.18.1  rmind 	u_char *tba;
   1526  1.3.18.1  rmind 	int tbc;
   1527  1.3.18.1  rmind 	u_int n;
   1528  1.3.18.1  rmind 	u_int space;
   1529  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   1530  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   1531  1.3.18.1  rmind 
   1532  1.3.18.1  rmind 	if (IMXUART_ISALIVE(sc) == 0)
   1533  1.3.18.1  rmind 		return;
   1534  1.3.18.1  rmind 
   1535  1.3.18.1  rmind 	s = spltty();
   1536  1.3.18.1  rmind 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1537  1.3.18.1  rmind 		goto out;
   1538  1.3.18.1  rmind 	if (sc->sc_tx_stopped)
   1539  1.3.18.1  rmind 		goto out;
   1540  1.3.18.1  rmind 	if (!ttypull(tp))
   1541  1.3.18.1  rmind 		goto out;
   1542  1.3.18.1  rmind 
   1543  1.3.18.1  rmind 	/* Grab the first contiguous region of buffer space. */
   1544  1.3.18.1  rmind 	tba = tp->t_outq.c_cf;
   1545  1.3.18.1  rmind 	tbc = ndqb(&tp->t_outq, 0);
   1546  1.3.18.1  rmind 
   1547  1.3.18.1  rmind 	mutex_spin_enter(&sc->sc_lock);
   1548  1.3.18.1  rmind 
   1549  1.3.18.1  rmind 	sc->sc_tba = tba;
   1550  1.3.18.1  rmind 	sc->sc_tbc = tbc;
   1551  1.3.18.1  rmind 
   1552  1.3.18.1  rmind 	SET(tp->t_state, TS_BUSY);
   1553  1.3.18.1  rmind 	sc->sc_tx_busy = 1;
   1554  1.3.18.1  rmind 
   1555  1.3.18.1  rmind 	space = imxuart_txfifo_space(sc);
   1556  1.3.18.1  rmind 	n = MIN(sc->sc_tbc, space);
   1557  1.3.18.1  rmind 
   1558  1.3.18.1  rmind 	bus_space_write_multi_1(iot, ioh, IMX_UTXD, sc->sc_tba, n);
   1559  1.3.18.1  rmind 	sc->sc_tbc -= n;
   1560  1.3.18.1  rmind 	sc->sc_tba += n;
   1561  1.3.18.1  rmind 
   1562  1.3.18.1  rmind 	/* Enable transmit completion interrupts */
   1563  1.3.18.1  rmind 	imxuart_control_txint(sc, true);
   1564  1.3.18.1  rmind 
   1565  1.3.18.1  rmind 	mutex_spin_exit(&sc->sc_lock);
   1566  1.3.18.1  rmind out:
   1567  1.3.18.1  rmind 	splx(s);
   1568  1.3.18.1  rmind 	return;
   1569  1.3.18.1  rmind }
   1570  1.3.18.1  rmind 
   1571  1.3.18.1  rmind /*
   1572  1.3.18.1  rmind  * Stop output on a line.
   1573  1.3.18.1  rmind  */
   1574  1.3.18.1  rmind void
   1575  1.3.18.1  rmind imxustop(struct tty *tp, int flag)
   1576  1.3.18.1  rmind {
   1577  1.3.18.1  rmind 	struct imxuart_softc *sc =
   1578  1.3.18.1  rmind 	    device_lookup_private(&imxuart_cd, IMXUART_UNIT(tp->t_dev));
   1579  1.3.18.1  rmind 
   1580  1.3.18.1  rmind 	mutex_spin_enter(&sc->sc_lock);
   1581  1.3.18.1  rmind 	if (ISSET(tp->t_state, TS_BUSY)) {
   1582  1.3.18.1  rmind 		/* Stop transmitting at the next chunk. */
   1583  1.3.18.1  rmind 		sc->sc_tbc = 0;
   1584  1.3.18.1  rmind 		sc->sc_heldtbc = 0;
   1585  1.3.18.1  rmind 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1586  1.3.18.1  rmind 			SET(tp->t_state, TS_FLUSH);
   1587  1.3.18.1  rmind 	}
   1588  1.3.18.1  rmind 	mutex_spin_exit(&sc->sc_lock);
   1589  1.3.18.1  rmind }
   1590  1.3.18.1  rmind 
   1591  1.3.18.1  rmind void
   1592  1.3.18.1  rmind imxudiag(void *arg)
   1593  1.3.18.1  rmind {
   1594  1.3.18.1  rmind #ifdef notyet
   1595  1.3.18.1  rmind 	struct imxuart_softc *sc = arg;
   1596  1.3.18.1  rmind 	int overflows, floods;
   1597  1.3.18.1  rmind 
   1598  1.3.18.1  rmind 	mutex_spin_enter(&sc->sc_lock);
   1599  1.3.18.1  rmind 	overflows = sc->sc_overflows;
   1600  1.3.18.1  rmind 	sc->sc_overflows = 0;
   1601  1.3.18.1  rmind 	floods = sc->sc_floods;
   1602  1.3.18.1  rmind 	sc->sc_floods = 0;
   1603  1.3.18.1  rmind 	sc->sc_errors = 0;
   1604  1.3.18.1  rmind 	mutex_spin_exit(&sc->sc_lock);
   1605  1.3.18.1  rmind 
   1606  1.3.18.1  rmind 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1607  1.3.18.1  rmind 	    device_xname(sc->sc_dev),
   1608  1.3.18.1  rmind 	    overflows, overflows == 1 ? "" : "s",
   1609  1.3.18.1  rmind 	    floods, floods == 1 ? "" : "s");
   1610  1.3.18.1  rmind #endif
   1611  1.3.18.1  rmind }
   1612  1.3.18.1  rmind 
   1613  1.3.18.1  rmind integrate void
   1614  1.3.18.1  rmind imxuart_rxsoft(struct imxuart_softc *sc, struct tty *tp)
   1615  1.3.18.1  rmind {
   1616  1.3.18.1  rmind 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
   1617  1.3.18.1  rmind 	u_int cc, scc, outp;
   1618  1.3.18.1  rmind 	uint16_t data;
   1619  1.3.18.1  rmind 	u_int code;
   1620  1.3.18.1  rmind 
   1621  1.3.18.1  rmind 	scc = cc = IMXUART_RBUF_AVAIL(sc);
   1622  1.3.18.1  rmind 
   1623  1.3.18.1  rmind #if 0
   1624  1.3.18.1  rmind 	if (cc == imxuart_rbuf_size-1) {
   1625  1.3.18.1  rmind 		sc->sc_floods++;
   1626  1.3.18.1  rmind 		if (sc->sc_errors++ == 0)
   1627  1.3.18.1  rmind 			callout_reset(&sc->sc_diag_callout, 60 * hz,
   1628  1.3.18.1  rmind 			    imxudiag, sc);
   1629  1.3.18.1  rmind 	}
   1630  1.3.18.1  rmind #endif
   1631  1.3.18.1  rmind 
   1632  1.3.18.1  rmind 	/* If not yet open, drop the entire buffer content here */
   1633  1.3.18.1  rmind 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
   1634  1.3.18.1  rmind 		sc->sc_rbuf_out = sc->sc_rbuf_in;
   1635  1.3.18.1  rmind 		cc = 0;
   1636  1.3.18.1  rmind 	}
   1637  1.3.18.1  rmind 
   1638  1.3.18.1  rmind 	outp = sc->sc_rbuf_out;
   1639  1.3.18.1  rmind 
   1640  1.3.18.1  rmind #define	ERRBITS (IMX_URXD_PRERR|IMX_URXD_BRK|IMX_URXD_FRMERR|IMX_URXD_OVRRUN)
   1641  1.3.18.1  rmind 
   1642  1.3.18.1  rmind 	while (cc) {
   1643  1.3.18.1  rmind 	        data = sc->sc_rbuf[outp];
   1644  1.3.18.1  rmind 		code = data & IMX_URXD_RX_DATA;
   1645  1.3.18.1  rmind 		if (ISSET(data, ERRBITS)) {
   1646  1.3.18.1  rmind 			if (sc->sc_errors.err == 0)
   1647  1.3.18.1  rmind 				callout_reset(&sc->sc_diag_callout,
   1648  1.3.18.1  rmind 				    60 * hz, imxudiag, sc);
   1649  1.3.18.1  rmind 			if (ISSET(data, IMX_URXD_OVRRUN))
   1650  1.3.18.1  rmind 				sc->sc_errors.ovrrun++;
   1651  1.3.18.1  rmind 			if (ISSET(data, IMX_URXD_BRK)) {
   1652  1.3.18.1  rmind 				sc->sc_errors.brk++;
   1653  1.3.18.1  rmind 				SET(code, TTY_FE);
   1654  1.3.18.1  rmind 			}
   1655  1.3.18.1  rmind 			if (ISSET(data, IMX_URXD_FRMERR)) {
   1656  1.3.18.1  rmind 				sc->sc_errors.frmerr++;
   1657  1.3.18.1  rmind 				SET(code, TTY_FE);
   1658  1.3.18.1  rmind 			}
   1659  1.3.18.1  rmind 			if (ISSET(data, IMX_URXD_PRERR)) {
   1660  1.3.18.1  rmind 				sc->sc_errors.prerr++;
   1661  1.3.18.1  rmind 				SET(code, TTY_PE);
   1662  1.3.18.1  rmind 			}
   1663  1.3.18.1  rmind 		}
   1664  1.3.18.1  rmind 		if ((*rint)(code, tp) == -1) {
   1665  1.3.18.1  rmind 			/*
   1666  1.3.18.1  rmind 			 * The line discipline's buffer is out of space.
   1667  1.3.18.1  rmind 			 */
   1668  1.3.18.1  rmind 			if (!ISSET(sc->sc_rx_flags, IMXUART_RX_TTY_BLOCKED)) {
   1669  1.3.18.1  rmind 				/*
   1670  1.3.18.1  rmind 				 * We're either not using flow control, or the
   1671  1.3.18.1  rmind 				 * line discipline didn't tell us to block for
   1672  1.3.18.1  rmind 				 * some reason.  Either way, we have no way to
   1673  1.3.18.1  rmind 				 * know when there's more space available, so
   1674  1.3.18.1  rmind 				 * just drop the rest of the data.
   1675  1.3.18.1  rmind 				 */
   1676  1.3.18.1  rmind 				sc->sc_rbuf_out = sc->sc_rbuf_in;
   1677  1.3.18.1  rmind 				cc = 0;
   1678  1.3.18.1  rmind 			} else {
   1679  1.3.18.1  rmind 				/*
   1680  1.3.18.1  rmind 				 * Don't schedule any more receive processing
   1681  1.3.18.1  rmind 				 * until the line discipline tells us there's
   1682  1.3.18.1  rmind 				 * space available (through imxuhwiflow()).
   1683  1.3.18.1  rmind 				 * Leave the rest of the data in the input
   1684  1.3.18.1  rmind 				 * buffer.
   1685  1.3.18.1  rmind 				 */
   1686  1.3.18.1  rmind 				SET(sc->sc_rx_flags, IMXUART_RX_TTY_OVERFLOWED);
   1687  1.3.18.1  rmind 			}
   1688  1.3.18.1  rmind 			break;
   1689  1.3.18.1  rmind 		}
   1690  1.3.18.1  rmind 		outp = IMXUART_RBUF_INC(sc, outp, 1);
   1691  1.3.18.1  rmind 		cc--;
   1692  1.3.18.1  rmind 	}
   1693  1.3.18.1  rmind 
   1694  1.3.18.1  rmind 	if (cc != scc) {
   1695  1.3.18.1  rmind 		sc->sc_rbuf_out = outp;
   1696  1.3.18.1  rmind 		mutex_spin_enter(&sc->sc_lock);
   1697  1.3.18.1  rmind 
   1698  1.3.18.1  rmind 		cc = IMXUART_RBUF_SPACE(sc);
   1699  1.3.18.1  rmind 
   1700  1.3.18.1  rmind 		/* Buffers should be ok again, release possible block. */
   1701  1.3.18.1  rmind 		if (cc >= sc->sc_r_lowat) {
   1702  1.3.18.1  rmind 			if (ISSET(sc->sc_rx_flags, IMXUART_RX_IBUF_OVERFLOWED)) {
   1703  1.3.18.1  rmind 				CLR(sc->sc_rx_flags, IMXUART_RX_IBUF_OVERFLOWED);
   1704  1.3.18.1  rmind 				imxuart_control_rxint(sc, true);
   1705  1.3.18.1  rmind 			}
   1706  1.3.18.1  rmind 			if (ISSET(sc->sc_rx_flags, IMXUART_RX_IBUF_BLOCKED)) {
   1707  1.3.18.1  rmind 				CLR(sc->sc_rx_flags, IMXUART_RX_IBUF_BLOCKED);
   1708  1.3.18.1  rmind 				imxuart_hwiflow(sc);
   1709  1.3.18.1  rmind 			}
   1710  1.3.18.1  rmind 		}
   1711  1.3.18.1  rmind 		mutex_spin_exit(&sc->sc_lock);
   1712  1.3.18.1  rmind 	}
   1713  1.3.18.1  rmind }
   1714  1.3.18.1  rmind 
   1715  1.3.18.1  rmind integrate void
   1716  1.3.18.1  rmind imxuart_txsoft(struct imxuart_softc *sc, struct tty *tp)
   1717  1.3.18.1  rmind {
   1718  1.3.18.1  rmind 
   1719  1.3.18.1  rmind 	CLR(tp->t_state, TS_BUSY);
   1720  1.3.18.1  rmind 	if (ISSET(tp->t_state, TS_FLUSH))
   1721  1.3.18.1  rmind 		CLR(tp->t_state, TS_FLUSH);
   1722  1.3.18.1  rmind 	else
   1723  1.3.18.1  rmind 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1724  1.3.18.1  rmind 	(*tp->t_linesw->l_start)(tp);
   1725  1.3.18.1  rmind }
   1726  1.3.18.1  rmind 
   1727  1.3.18.1  rmind integrate void
   1728  1.3.18.1  rmind imxuart_stsoft(struct imxuart_softc *sc, struct tty *tp)
   1729  1.3.18.1  rmind {
   1730  1.3.18.1  rmind #ifdef notyet
   1731  1.3.18.1  rmind 	u_char msr, delta;
   1732  1.3.18.1  rmind 
   1733  1.3.18.1  rmind 	mutex_spin_enter(&sc->sc_lock);
   1734  1.3.18.1  rmind 	msr = sc->sc_msr;
   1735  1.3.18.1  rmind 	delta = sc->sc_msr_delta;
   1736  1.3.18.1  rmind 	sc->sc_msr_delta = 0;
   1737  1.3.18.1  rmind 	mutex_spin_exit(&sc->sc_lock);
   1738  1.3.18.1  rmind 
   1739  1.3.18.1  rmind 	if (ISSET(delta, sc->sc_msr_dcd)) {
   1740  1.3.18.1  rmind 		/*
   1741  1.3.18.1  rmind 		 * Inform the tty layer that carrier detect changed.
   1742  1.3.18.1  rmind 		 */
   1743  1.3.18.1  rmind 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
   1744  1.3.18.1  rmind 	}
   1745  1.3.18.1  rmind 
   1746  1.3.18.1  rmind 	if (ISSET(delta, sc->sc_msr_cts)) {
   1747  1.3.18.1  rmind 		/* Block or unblock output according to flow control. */
   1748  1.3.18.1  rmind 		if (ISSET(msr, sc->sc_msr_cts)) {
   1749  1.3.18.1  rmind 			sc->sc_tx_stopped = 0;
   1750  1.3.18.1  rmind 			(*tp->t_linesw->l_start)(tp);
   1751  1.3.18.1  rmind 		} else {
   1752  1.3.18.1  rmind 			sc->sc_tx_stopped = 1;
   1753  1.3.18.1  rmind 		}
   1754  1.3.18.1  rmind 	}
   1755  1.3.18.1  rmind 
   1756  1.3.18.1  rmind #endif
   1757  1.3.18.1  rmind #ifdef IMXUART_DEBUG
   1758  1.3.18.1  rmind 	if (imxuart_debug)
   1759  1.3.18.1  rmind 		imxustatus(sc, "imxuart_stsoft");
   1760  1.3.18.1  rmind #endif
   1761  1.3.18.1  rmind }
   1762  1.3.18.1  rmind 
   1763  1.3.18.1  rmind void
   1764  1.3.18.1  rmind imxusoft(void *arg)
   1765  1.3.18.1  rmind {
   1766  1.3.18.1  rmind 	struct imxuart_softc *sc = arg;
   1767  1.3.18.1  rmind 	struct tty *tp;
   1768  1.3.18.1  rmind 
   1769  1.3.18.1  rmind 	if (IMXUART_ISALIVE(sc) == 0)
   1770  1.3.18.1  rmind 		return;
   1771  1.3.18.1  rmind 
   1772  1.3.18.1  rmind 	tp = sc->sc_tty;
   1773  1.3.18.1  rmind 
   1774  1.3.18.1  rmind 	if (sc->sc_rx_ready) {
   1775  1.3.18.1  rmind 		sc->sc_rx_ready = 0;
   1776  1.3.18.1  rmind 		imxuart_rxsoft(sc, tp);
   1777  1.3.18.1  rmind 	}
   1778  1.3.18.1  rmind 
   1779  1.3.18.1  rmind 	if (sc->sc_st_check) {
   1780  1.3.18.1  rmind 		sc->sc_st_check = 0;
   1781  1.3.18.1  rmind 		imxuart_stsoft(sc, tp);
   1782  1.3.18.1  rmind 	}
   1783  1.3.18.1  rmind 
   1784  1.3.18.1  rmind 	if (sc->sc_tx_done) {
   1785  1.3.18.1  rmind 		sc->sc_tx_done = 0;
   1786  1.3.18.1  rmind 		imxuart_txsoft(sc, tp);
   1787  1.3.18.1  rmind 	}
   1788  1.3.18.1  rmind }
   1789  1.3.18.1  rmind 
   1790  1.3.18.1  rmind int
   1791  1.3.18.1  rmind imxuintr(void *arg)
   1792  1.3.18.1  rmind {
   1793  1.3.18.1  rmind 	struct imxuart_softc *sc = arg;
   1794  1.3.18.1  rmind 	uint32_t usr1, usr2;
   1795  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   1796  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   1797  1.3.18.1  rmind 
   1798  1.3.18.1  rmind 
   1799  1.3.18.1  rmind 	if (IMXUART_ISALIVE(sc) == 0)
   1800  1.3.18.1  rmind 		return (0);
   1801  1.3.18.1  rmind 
   1802  1.3.18.1  rmind 	mutex_spin_enter(&sc->sc_lock);
   1803  1.3.18.1  rmind 
   1804  1.3.18.1  rmind 	usr2 = bus_space_read_4(iot, ioh, IMX_USR2);
   1805  1.3.18.1  rmind 
   1806  1.3.18.1  rmind 
   1807  1.3.18.1  rmind 	do {
   1808  1.3.18.1  rmind 		bus_space_write_4(iot, ioh, IMX_USR2,
   1809  1.3.18.1  rmind 		    usr2 & (IMX_USR2_BRCD|IMX_USR2_ORE));
   1810  1.3.18.1  rmind 		if (usr2 & IMX_USR2_BRCD) {
   1811  1.3.18.1  rmind 			/* Break signal detected */
   1812  1.3.18.1  rmind 			int cn_trapped = 0;
   1813  1.3.18.1  rmind 
   1814  1.3.18.1  rmind 			cn_check_magic(sc->sc_tty->t_dev,
   1815  1.3.18.1  rmind 				       CNC_BREAK, imxuart_cnm_state);
   1816  1.3.18.1  rmind 			if (cn_trapped)
   1817  1.3.18.1  rmind 				continue;
   1818  1.3.18.1  rmind #if defined(KGDB) && !defined(DDB)
   1819  1.3.18.1  rmind 			if (ISSET(sc->sc_hwflags, IMXUART_HW_KGDB)) {
   1820  1.3.18.1  rmind 				kgdb_connect(1);
   1821  1.3.18.1  rmind 				continue;
   1822  1.3.18.1  rmind 			}
   1823  1.3.18.1  rmind #endif
   1824  1.3.18.1  rmind 		}
   1825  1.3.18.1  rmind 
   1826  1.3.18.1  rmind 		if (usr2 & IMX_USR2_RDR)
   1827  1.3.18.1  rmind 			imxuintr_read(sc);
   1828  1.3.18.1  rmind 
   1829  1.3.18.1  rmind #ifdef	IMXUART_PPS
   1830  1.3.18.1  rmind 		{
   1831  1.3.18.1  rmind 			u_char	msr, delta;
   1832  1.3.18.1  rmind 
   1833  1.3.18.1  rmind 			msr = CSR_READ_1(regsp, IMXUART_REG_MSR);
   1834  1.3.18.1  rmind 			delta = msr ^ sc->sc_msr;
   1835  1.3.18.1  rmind 			sc->sc_msr = msr;
   1836  1.3.18.1  rmind 			if ((sc->sc_pps_state.ppsparam.mode & PPS_CAPTUREBOTH) &&
   1837  1.3.18.1  rmind 			    (delta & MSR_DCD)) {
   1838  1.3.18.1  rmind 				mutex_spin_enter(&timecounter_lock);
   1839  1.3.18.1  rmind 				pps_capture(&sc->sc_pps_state);
   1840  1.3.18.1  rmind 				pps_event(&sc->sc_pps_state,
   1841  1.3.18.1  rmind 				    (msr & MSR_DCD) ?
   1842  1.3.18.1  rmind 				    PPS_CAPTUREASSERT :
   1843  1.3.18.1  rmind 				    PPS_CAPTURECLEAR);
   1844  1.3.18.1  rmind 				mutex_spin_exit(&timecounter_lock);
   1845  1.3.18.1  rmind 			}
   1846  1.3.18.1  rmind 		}
   1847  1.3.18.1  rmind #endif
   1848  1.3.18.1  rmind 
   1849  1.3.18.1  rmind #ifdef notyet
   1850  1.3.18.1  rmind 		/*
   1851  1.3.18.1  rmind 		 * Process normal status changes
   1852  1.3.18.1  rmind 		 */
   1853  1.3.18.1  rmind 		if (ISSET(delta, sc->sc_msr_mask)) {
   1854  1.3.18.1  rmind 			SET(sc->sc_msr_delta, delta);
   1855  1.3.18.1  rmind 
   1856  1.3.18.1  rmind 			/*
   1857  1.3.18.1  rmind 			 * Stop output immediately if we lose the output
   1858  1.3.18.1  rmind 			 * flow control signal or carrier detect.
   1859  1.3.18.1  rmind 			 */
   1860  1.3.18.1  rmind 			if (ISSET(~msr, sc->sc_msr_mask)) {
   1861  1.3.18.1  rmind 				sc->sc_tbc = 0;
   1862  1.3.18.1  rmind 				sc->sc_heldtbc = 0;
   1863  1.3.18.1  rmind #ifdef IMXUART_DEBUG
   1864  1.3.18.1  rmind 				if (imxuart_debug)
   1865  1.3.18.1  rmind 					imxustatus(sc, "imxuintr  ");
   1866  1.3.18.1  rmind #endif
   1867  1.3.18.1  rmind 			}
   1868  1.3.18.1  rmind 
   1869  1.3.18.1  rmind 			sc->sc_st_check = 1;
   1870  1.3.18.1  rmind 		}
   1871  1.3.18.1  rmind #endif
   1872  1.3.18.1  rmind 
   1873  1.3.18.1  rmind 		usr2 = bus_space_read_4(iot, ioh, IMX_USR2);
   1874  1.3.18.1  rmind 	} while (usr2 & (IMX_USR2_RDR|IMX_USR2_BRCD));
   1875  1.3.18.1  rmind 
   1876  1.3.18.1  rmind 	usr1 = bus_space_read_4(iot, ioh, IMX_USR1);
   1877  1.3.18.1  rmind 	if (usr1 & IMX_USR1_TRDY)
   1878  1.3.18.1  rmind 		imxuintr_send(sc);
   1879  1.3.18.1  rmind 
   1880  1.3.18.1  rmind 	mutex_spin_exit(&sc->sc_lock);
   1881  1.3.18.1  rmind 
   1882  1.3.18.1  rmind 	/* Wake up the poller. */
   1883  1.3.18.1  rmind 	softint_schedule(sc->sc_si);
   1884  1.3.18.1  rmind 
   1885  1.3.18.1  rmind #if NRND > 0 && defined(RND_COM)
   1886  1.3.18.1  rmind 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
   1887  1.3.18.1  rmind #endif
   1888  1.3.18.1  rmind 
   1889  1.3.18.1  rmind 	return (1);
   1890  1.3.18.1  rmind }
   1891  1.3.18.1  rmind 
   1892  1.3.18.1  rmind 
   1893  1.3.18.1  rmind /*
   1894  1.3.18.1  rmind  * called when there is least one character in rxfifo
   1895  1.3.18.1  rmind  *
   1896  1.3.18.1  rmind  */
   1897  1.3.18.1  rmind 
   1898  1.3.18.1  rmind static void
   1899  1.3.18.1  rmind imxuintr_read(struct imxuart_softc *sc)
   1900  1.3.18.1  rmind {
   1901  1.3.18.1  rmind 	int cc;
   1902  1.3.18.1  rmind 	uint16_t rd;
   1903  1.3.18.1  rmind 	uint32_t usr2;
   1904  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   1905  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   1906  1.3.18.1  rmind 
   1907  1.3.18.1  rmind 	cc = IMXUART_RBUF_SPACE(sc);
   1908  1.3.18.1  rmind 
   1909  1.3.18.1  rmind 	/* clear aging timer interrupt */
   1910  1.3.18.1  rmind 	bus_space_write_4(iot, ioh, IMX_USR1, IMX_USR1_AGTIM);
   1911  1.3.18.1  rmind 
   1912  1.3.18.1  rmind 	while (cc > 0) {
   1913  1.3.18.1  rmind 		int cn_trapped = 0;
   1914  1.3.18.1  rmind 
   1915  1.3.18.1  rmind 
   1916  1.3.18.1  rmind 		sc->sc_rbuf[sc->sc_rbuf_in] = rd =
   1917  1.3.18.1  rmind 		    bus_space_read_4(iot, ioh, IMX_URXD);
   1918  1.3.18.1  rmind 
   1919  1.3.18.1  rmind 		cn_check_magic(sc->sc_tty->t_dev,
   1920  1.3.18.1  rmind 		    rd & 0xff, imxuart_cnm_state);
   1921  1.3.18.1  rmind 
   1922  1.3.18.1  rmind 		if (!cn_trapped) {
   1923  1.3.18.1  rmind 			sc->sc_rbuf_in = IMXUART_RBUF_INC(sc, sc->sc_rbuf_in, 1);
   1924  1.3.18.1  rmind 			cc--;
   1925  1.3.18.1  rmind 		}
   1926  1.3.18.1  rmind 
   1927  1.3.18.1  rmind 		usr2 = bus_space_read_4(iot, ioh, IMX_USR2);
   1928  1.3.18.1  rmind 		if (!(usr2 & IMX_USR2_RDR))
   1929  1.3.18.1  rmind 			break;
   1930  1.3.18.1  rmind 	}
   1931  1.3.18.1  rmind 
   1932  1.3.18.1  rmind 	/*
   1933  1.3.18.1  rmind 	 * Current string of incoming characters ended because
   1934  1.3.18.1  rmind 	 * no more data was available or we ran out of space.
   1935  1.3.18.1  rmind 	 * Schedule a receive event if any data was received.
   1936  1.3.18.1  rmind 	 * If we're out of space, turn off receive interrupts.
   1937  1.3.18.1  rmind 	 */
   1938  1.3.18.1  rmind 	if (!ISSET(sc->sc_rx_flags, IMXUART_RX_TTY_OVERFLOWED))
   1939  1.3.18.1  rmind 		sc->sc_rx_ready = 1;
   1940  1.3.18.1  rmind 	/*
   1941  1.3.18.1  rmind 	 * See if we are in danger of overflowing a buffer. If
   1942  1.3.18.1  rmind 	 * so, use hardware flow control to ease the pressure.
   1943  1.3.18.1  rmind 	 */
   1944  1.3.18.1  rmind 	if (!ISSET(sc->sc_rx_flags, IMXUART_RX_IBUF_BLOCKED) &&
   1945  1.3.18.1  rmind 	    cc < sc->sc_r_hiwat) {
   1946  1.3.18.1  rmind 		sc->sc_rx_flags |= IMXUART_RX_IBUF_BLOCKED;
   1947  1.3.18.1  rmind 		imxuart_hwiflow(sc);
   1948  1.3.18.1  rmind 	}
   1949  1.3.18.1  rmind 
   1950  1.3.18.1  rmind 	/*
   1951  1.3.18.1  rmind 	 * If we're out of space, disable receive interrupts
   1952  1.3.18.1  rmind 	 * until the queue has drained a bit.
   1953  1.3.18.1  rmind 	 */
   1954  1.3.18.1  rmind 	if (!cc) {
   1955  1.3.18.1  rmind 		sc->sc_rx_flags |= IMXUART_RX_IBUF_OVERFLOWED;
   1956  1.3.18.1  rmind 		imxuart_control_rxint(sc, false);
   1957  1.3.18.1  rmind 	}
   1958  1.3.18.1  rmind }
   1959  1.3.18.1  rmind 
   1960  1.3.18.1  rmind 
   1961  1.3.18.1  rmind 
   1962  1.3.18.1  rmind /*
   1963  1.3.18.1  rmind  * find how many chars we can put into tx-fifo
   1964  1.3.18.1  rmind  */
   1965  1.3.18.1  rmind static u_int
   1966  1.3.18.1  rmind imxuart_txfifo_space(struct imxuart_softc *sc)
   1967  1.3.18.1  rmind {
   1968  1.3.18.1  rmind 	uint32_t usr1, usr2;
   1969  1.3.18.1  rmind 	u_int cc;
   1970  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   1971  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   1972  1.3.18.1  rmind 
   1973  1.3.18.1  rmind 	usr2 = bus_space_read_4(iot, ioh, IMX_USR2);
   1974  1.3.18.1  rmind 	if (usr2 & IMX_USR2_TXFE)
   1975  1.3.18.1  rmind 		cc = sc->sc_txfifo_len;
   1976  1.3.18.1  rmind 	else {
   1977  1.3.18.1  rmind 		usr1 = bus_space_read_4(iot, ioh, IMX_USR1);
   1978  1.3.18.1  rmind 		if (usr1 & IMX_USR1_TRDY)
   1979  1.3.18.1  rmind 			cc = sc->sc_txfifo_thresh;
   1980  1.3.18.1  rmind 		else
   1981  1.3.18.1  rmind 			cc = 0;
   1982  1.3.18.1  rmind 	}
   1983  1.3.18.1  rmind 
   1984  1.3.18.1  rmind 	return cc;
   1985  1.3.18.1  rmind }
   1986  1.3.18.1  rmind 
   1987  1.3.18.1  rmind void
   1988  1.3.18.1  rmind imxuintr_send(struct imxuart_softc *sc)
   1989  1.3.18.1  rmind {
   1990  1.3.18.1  rmind 	uint32_t usr2;
   1991  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   1992  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   1993  1.3.18.1  rmind 	int cc = 0;
   1994  1.3.18.1  rmind 
   1995  1.3.18.1  rmind 	usr2 = bus_space_read_4(iot, ioh, IMX_USR2);
   1996  1.3.18.1  rmind 
   1997  1.3.18.1  rmind 	if (sc->sc_pending) {
   1998  1.3.18.1  rmind 		if (usr2 & IMX_USR2_TXFE) {
   1999  1.3.18.1  rmind 			imxuart_load_pendings(sc);
   2000  1.3.18.1  rmind 			sc->sc_tbc = sc->sc_heldtbc;
   2001  1.3.18.1  rmind 			sc->sc_heldtbc = 0;
   2002  1.3.18.1  rmind 		}
   2003  1.3.18.1  rmind 		else {
   2004  1.3.18.1  rmind 			/* wait for TX fifo empty */
   2005  1.3.18.1  rmind 			imxuart_control_txint(sc, true);
   2006  1.3.18.1  rmind 			return;
   2007  1.3.18.1  rmind 		}
   2008  1.3.18.1  rmind 	}
   2009  1.3.18.1  rmind 
   2010  1.3.18.1  rmind 	cc = imxuart_txfifo_space(sc);
   2011  1.3.18.1  rmind 	cc = MIN(cc, sc->sc_tbc);
   2012  1.3.18.1  rmind 
   2013  1.3.18.1  rmind 	if (cc > 0) {
   2014  1.3.18.1  rmind 		bus_space_write_multi_1(iot, ioh, IMX_UTXD, sc->sc_tba, cc);
   2015  1.3.18.1  rmind 		sc->sc_tbc -= cc;
   2016  1.3.18.1  rmind 		sc->sc_tba += cc;
   2017  1.3.18.1  rmind 	}
   2018  1.3.18.1  rmind 
   2019  1.3.18.1  rmind 	if (sc->sc_tbc > 0)
   2020  1.3.18.1  rmind 		imxuart_control_txint(sc, true);
   2021  1.3.18.1  rmind 	else {
   2022  1.3.18.1  rmind 		/* no more chars to send.
   2023  1.3.18.1  rmind 		   we don't need tx interrupt any more. */
   2024  1.3.18.1  rmind 		imxuart_control_txint(sc, false);
   2025  1.3.18.1  rmind 		if (sc->sc_tx_busy) {
   2026  1.3.18.1  rmind 			sc->sc_tx_busy = 0;
   2027  1.3.18.1  rmind 			sc->sc_tx_done = 1;
   2028  1.3.18.1  rmind 		}
   2029  1.3.18.1  rmind 	}
   2030  1.3.18.1  rmind }
   2031  1.3.18.1  rmind 
   2032  1.3.18.1  rmind static void
   2033  1.3.18.1  rmind imxuart_disable_all_interrupts(struct imxuart_softc *sc)
   2034  1.3.18.1  rmind {
   2035  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   2036  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   2037  1.3.18.1  rmind 
   2038  1.3.18.1  rmind 	sc->sc_ucr1 &= ~IMXUART_INTRS_UCR1;
   2039  1.3.18.1  rmind 	sc->sc_ucr2 &= ~IMXUART_INTRS_UCR2;
   2040  1.3.18.1  rmind 	sc->sc_ucr3 &= ~IMXUART_INTRS_UCR3;
   2041  1.3.18.1  rmind 	sc->sc_ucr4 &= ~IMXUART_INTRS_UCR4;
   2042  1.3.18.1  rmind 
   2043  1.3.18.1  rmind 
   2044  1.3.18.1  rmind 	bus_space_write_region_4(iot, ioh, IMX_UCR1, sc->sc_ucr, 4);
   2045  1.3.18.1  rmind }
   2046  1.3.18.1  rmind 
   2047  1.3.18.1  rmind static void
   2048  1.3.18.1  rmind imxuart_control_rxint(struct imxuart_softc *sc, bool enable)
   2049  1.3.18.1  rmind {
   2050  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   2051  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   2052  1.3.18.1  rmind 	uint32_t ucr1, ucr2;
   2053  1.3.18.1  rmind 
   2054  1.3.18.1  rmind 	ucr1 = sc->sc_ucr1;
   2055  1.3.18.1  rmind 	ucr2 = sc->sc_ucr2;
   2056  1.3.18.1  rmind 
   2057  1.3.18.1  rmind 	if (enable) {
   2058  1.3.18.1  rmind 		ucr1 |= IMX_UCR1_RRDYEN;
   2059  1.3.18.1  rmind 		ucr2 |= IMX_UCR2_ATEN;
   2060  1.3.18.1  rmind 	}
   2061  1.3.18.1  rmind 	else {
   2062  1.3.18.1  rmind 		ucr1 &= ~IMX_UCR1_RRDYEN;
   2063  1.3.18.1  rmind 		ucr2 &= ~IMX_UCR2_ATEN;
   2064  1.3.18.1  rmind 	}
   2065  1.3.18.1  rmind 
   2066  1.3.18.1  rmind 	if (ucr1 != sc->sc_ucr1 || ucr2 != sc->sc_ucr2) {
   2067  1.3.18.1  rmind 		sc->sc_ucr1 = ucr1;
   2068  1.3.18.1  rmind 		sc->sc_ucr2 = ucr2;
   2069  1.3.18.1  rmind 		bus_space_write_region_4(iot, ioh, IMX_UCR1, sc->sc_ucr, 2);
   2070  1.3.18.1  rmind 	}
   2071  1.3.18.1  rmind }
   2072  1.3.18.1  rmind 
   2073  1.3.18.1  rmind static void
   2074  1.3.18.1  rmind imxuart_control_txint(struct imxuart_softc *sc, bool enable)
   2075  1.3.18.1  rmind {
   2076  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   2077  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   2078  1.3.18.1  rmind 	uint32_t ucr1;
   2079  1.3.18.1  rmind 	uint32_t mask;
   2080  1.3.18.1  rmind 
   2081  1.3.18.1  rmind 	/* if parameter change is pending, get interrupt when Tx fifo
   2082  1.3.18.1  rmind 	   is completely empty.  otherwise, get interrupt when txfifo
   2083  1.3.18.1  rmind 	   has less characters than threshold */
   2084  1.3.18.1  rmind 	mask = sc->sc_pending ? IMX_UCR1_TXMPTYEN : IMX_UCR1_TRDYEN;
   2085  1.3.18.1  rmind 
   2086  1.3.18.1  rmind 	ucr1 = sc->sc_ucr1;
   2087  1.3.18.1  rmind 
   2088  1.3.18.1  rmind 	CLR(ucr1, IMX_UCR1_TXMPTYEN|IMX_UCR1_TRDYEN);
   2089  1.3.18.1  rmind 	if (enable)
   2090  1.3.18.1  rmind 		SET(ucr1, mask);
   2091  1.3.18.1  rmind 
   2092  1.3.18.1  rmind 	if (ucr1 != sc->sc_ucr1) {
   2093  1.3.18.1  rmind 		bus_space_write_4(iot, ioh, IMX_UCR1, ucr1);
   2094  1.3.18.1  rmind 		sc->sc_ucr1 = ucr1;
   2095  1.3.18.1  rmind 	}
   2096  1.3.18.1  rmind }
   2097  1.3.18.1  rmind 
   2098  1.3.18.1  rmind 
   2099  1.3.18.1  rmind static void
   2100  1.3.18.1  rmind imxuart_load_params(struct imxuart_softc *sc)
   2101  1.3.18.1  rmind {
   2102  1.3.18.1  rmind 	uint32_t ucr2;
   2103  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   2104  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   2105  1.3.18.1  rmind 
   2106  1.3.18.1  rmind 	ucr2 = (sc->sc_ucr2_d & ~IMX_UCR2_ATEN) |
   2107  1.3.18.1  rmind 	    (sc->sc_ucr2 & IMX_UCR2_ATEN);
   2108  1.3.18.1  rmind 
   2109  1.3.18.1  rmind 	bus_space_write_4(iot, ioh, IMX_UCR2, ucr2);
   2110  1.3.18.1  rmind 	sc->sc_ucr2 = ucr2;
   2111  1.3.18.1  rmind }
   2112  1.3.18.1  rmind 
   2113  1.3.18.1  rmind static void
   2114  1.3.18.1  rmind imxuart_load_speed(struct imxuart_softc *sc)
   2115  1.3.18.1  rmind {
   2116  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   2117  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   2118  1.3.18.1  rmind 	int n, rfdiv, ufcr;
   2119  1.3.18.1  rmind 
   2120  1.3.18.1  rmind #ifdef notyet
   2121  1.3.18.1  rmind 	/*
   2122  1.3.18.1  rmind 	 * Set the FIFO threshold based on the receive speed.
   2123  1.3.18.1  rmind 	 *
   2124  1.3.18.1  rmind 	 *  * If it's a low speed, it's probably a mouse or some other
   2125  1.3.18.1  rmind 	 *    interactive device, so set the threshold low.
   2126  1.3.18.1  rmind 	 *  * If it's a high speed, trim the trigger level down to prevent
   2127  1.3.18.1  rmind 	 *    overflows.
   2128  1.3.18.1  rmind 	 *  * Otherwise set it a bit higher.
   2129  1.3.18.1  rmind 	 */
   2130  1.3.18.1  rmind 	if (t->c_ospeed <= 1200)
   2131  1.3.18.1  rmind 		sc->sc_fifo = FIFO_ENABLE | FIFO_TRIGGER_1;
   2132  1.3.18.1  rmind 	else if (t->c_ospeed <= 38400)
   2133  1.3.18.1  rmind 		sc->sc_fifo = FIFO_ENABLE | FIFO_TRIGGER_8;
   2134  1.3.18.1  rmind 	else
   2135  1.3.18.1  rmind 		sc->sc_fifo = FIFO_ENABLE | FIFO_TRIGGER_4;
   2136  1.3.18.1  rmind #endif
   2137  1.3.18.1  rmind 
   2138  1.3.18.1  rmind 	n = 32 - sc->sc_txfifo_thresh;
   2139  1.3.18.1  rmind 	n = MAX(2, n);
   2140  1.3.18.1  rmind 
   2141  1.3.18.1  rmind 	rfdiv = IMX_UFCR_DIVIDER_TO_RFDIV(imxuart_freqdiv);
   2142  1.3.18.1  rmind 
   2143  1.3.18.1  rmind 	ufcr = (n << IMX_UFCR_TXTL_SHIFT) |
   2144  1.3.18.1  rmind 		(rfdiv << IMX_UFCR_RFDIV_SHIFT) |
   2145  1.3.18.1  rmind 		(16 << IMX_UFCR_RXTL_SHIFT);
   2146  1.3.18.1  rmind 
   2147  1.3.18.1  rmind 	/* keep DCE/DTE bit */
   2148  1.3.18.1  rmind 	ufcr |= bus_space_read_4(iot, ioh, IMX_UFCR) & IMX_UFCR_DCEDTE;
   2149  1.3.18.1  rmind 
   2150  1.3.18.1  rmind 	bus_space_write_4(iot, ioh, IMX_UFCR, ufcr);
   2151  1.3.18.1  rmind 
   2152  1.3.18.1  rmind 	/* UBIR must updated before UBMR */
   2153  1.3.18.1  rmind 	bus_space_write_4(iot, ioh,
   2154  1.3.18.1  rmind 	    IMX_UBIR, sc->sc_ratio.numerator);
   2155  1.3.18.1  rmind 	bus_space_write_4(iot, ioh,
   2156  1.3.18.1  rmind 	    IMX_UBMR, sc->sc_ratio.modulator);
   2157  1.3.18.1  rmind 
   2158  1.3.18.1  rmind 
   2159  1.3.18.1  rmind }
   2160  1.3.18.1  rmind 
   2161  1.3.18.1  rmind 
   2162  1.3.18.1  rmind static void
   2163  1.3.18.1  rmind imxuart_load_pendings(struct imxuart_softc *sc)
   2164  1.3.18.1  rmind {
   2165  1.3.18.1  rmind 	if (sc->sc_pending & IMXUART_PEND_PARAM)
   2166  1.3.18.1  rmind 		imxuart_load_params(sc);
   2167  1.3.18.1  rmind 	if (sc->sc_pending & IMXUART_PEND_SPEED)
   2168  1.3.18.1  rmind 		imxuart_load_speed(sc);
   2169  1.3.18.1  rmind 	sc->sc_pending = 0;
   2170  1.3.18.1  rmind }
   2171  1.3.18.1  rmind 
   2172  1.3.18.1  rmind #if defined(IMXUARTCONSOLE) || defined(KGDB)
   2173  1.3.18.1  rmind 
   2174  1.3.18.1  rmind /*
   2175  1.3.18.1  rmind  * The following functions are polled getc and putc routines, shared
   2176  1.3.18.1  rmind  * by the console and kgdb glue.
   2177  1.3.18.1  rmind  *
   2178  1.3.18.1  rmind  * The read-ahead code is so that you can detect pending in-band
   2179  1.3.18.1  rmind  * cn_magic in polled mode while doing output rather than having to
   2180  1.3.18.1  rmind  * wait until the kernel decides it needs input.
   2181  1.3.18.1  rmind  */
   2182  1.3.18.1  rmind 
   2183  1.3.18.1  rmind #define	READAHEAD_RING_LEN	16
   2184  1.3.18.1  rmind static int imxuart_readahead[READAHEAD_RING_LEN];
   2185  1.3.18.1  rmind static int imxuart_readahead_in = 0;
   2186  1.3.18.1  rmind static int imxuart_readahead_out = 0;
   2187  1.3.18.1  rmind #define	READAHEAD_IS_EMPTY()	(imxuart_readahead_in==imxuart_readahead_out)
   2188  1.3.18.1  rmind #define	READAHEAD_IS_FULL()	\
   2189  1.3.18.1  rmind 	(((imxuart_readahead_in+1) & (READAHEAD_RING_LEN-1)) ==imxuart_readahead_out)
   2190  1.3.18.1  rmind 
   2191  1.3.18.1  rmind int
   2192  1.3.18.1  rmind imxuart_common_getc(dev_t dev, struct imxuart_regs *regsp)
   2193  1.3.18.1  rmind {
   2194  1.3.18.1  rmind 	int s = splserial();
   2195  1.3.18.1  rmind 	u_char c;
   2196  1.3.18.1  rmind 	bus_space_tag_t iot = regsp->ur_iot;
   2197  1.3.18.1  rmind 	bus_space_handle_t ioh = regsp->ur_ioh;
   2198  1.3.18.1  rmind 	uint32_t usr2;
   2199  1.3.18.1  rmind 
   2200  1.3.18.1  rmind 	/* got a character from reading things earlier */
   2201  1.3.18.1  rmind 	if (imxuart_readahead_in != imxuart_readahead_out) {
   2202  1.3.18.1  rmind 
   2203  1.3.18.1  rmind 		c = imxuart_readahead[imxuart_readahead_out];
   2204  1.3.18.1  rmind 		imxuart_readahead_out = (imxuart_readahead_out + 1) &
   2205  1.3.18.1  rmind 		    (READAHEAD_RING_LEN-1);
   2206  1.3.18.1  rmind 		splx(s);
   2207  1.3.18.1  rmind 		return (c);
   2208  1.3.18.1  rmind 	}
   2209  1.3.18.1  rmind 
   2210  1.3.18.1  rmind 	/* block until a character becomes available */
   2211  1.3.18.1  rmind 	while (!((usr2 = bus_space_read_4(iot, ioh, IMX_USR2)) & IMX_USR2_RDR))
   2212  1.3.18.1  rmind 		;
   2213  1.3.18.1  rmind 
   2214  1.3.18.1  rmind 	c = 0xff & bus_space_read_4(iot, ioh, IMX_URXD);
   2215  1.3.18.1  rmind 
   2216  1.3.18.1  rmind 	{
   2217  1.3.18.1  rmind 		int cn_trapped = 0; /* unused */
   2218  1.3.18.1  rmind #ifdef DDB
   2219  1.3.18.1  rmind 		extern int db_active;
   2220  1.3.18.1  rmind 		if (!db_active)
   2221  1.3.18.1  rmind #endif
   2222  1.3.18.1  rmind 			cn_check_magic(dev, c, imxuart_cnm_state);
   2223  1.3.18.1  rmind 	}
   2224  1.3.18.1  rmind 	splx(s);
   2225  1.3.18.1  rmind 	return (c);
   2226  1.3.18.1  rmind }
   2227  1.3.18.1  rmind 
   2228  1.3.18.1  rmind void
   2229  1.3.18.1  rmind imxuart_common_putc(dev_t dev, struct imxuart_regs *regsp, int c)
   2230  1.3.18.1  rmind {
   2231  1.3.18.1  rmind 	int s = splserial();
   2232  1.3.18.1  rmind 	int cin, timo;
   2233  1.3.18.1  rmind 	bus_space_tag_t iot = regsp->ur_iot;
   2234  1.3.18.1  rmind 	bus_space_handle_t ioh = regsp->ur_ioh;
   2235  1.3.18.1  rmind 	uint32_t usr2;
   2236  1.3.18.1  rmind 
   2237  1.3.18.1  rmind 	if (!READAHEAD_IS_FULL() &&
   2238  1.3.18.1  rmind 	    ((usr2 = bus_space_read_4(iot, ioh, IMX_USR2)) & IMX_USR2_RDR)) {
   2239  1.3.18.1  rmind 
   2240  1.3.18.1  rmind 		int cn_trapped = 0;
   2241  1.3.18.1  rmind 		cin = bus_space_read_4(iot, ioh, IMX_URXD);
   2242  1.3.18.1  rmind 		cn_check_magic(dev, cin & 0xff, imxuart_cnm_state);
   2243  1.3.18.1  rmind 		imxuart_readahead_in = (imxuart_readahead_in + 1) &
   2244  1.3.18.1  rmind 		    (READAHEAD_RING_LEN-1);
   2245  1.3.18.1  rmind 	}
   2246  1.3.18.1  rmind 
   2247  1.3.18.1  rmind 	/* wait for any pending transmission to finish */
   2248  1.3.18.1  rmind 	timo = 150000;
   2249  1.3.18.1  rmind 	do {
   2250  1.3.18.1  rmind 		if (bus_space_read_4(iot, ioh, IMX_USR1) & IMX_USR1_TRDY) {
   2251  1.3.18.1  rmind 			bus_space_write_4(iot, ioh, IMX_UTXD, c);
   2252  1.3.18.1  rmind 			break;
   2253  1.3.18.1  rmind 		}
   2254  1.3.18.1  rmind 	} while(--timo > 0);
   2255  1.3.18.1  rmind 
   2256  1.3.18.1  rmind 	IMXUART_BARRIER(regsp, BR | BW);
   2257  1.3.18.1  rmind 
   2258  1.3.18.1  rmind 	splx(s);
   2259  1.3.18.1  rmind }
   2260  1.3.18.1  rmind 
   2261  1.3.18.1  rmind /*
   2262  1.3.18.1  rmind  * Initialize UART for use as console or KGDB line.
   2263  1.3.18.1  rmind  */
   2264  1.3.18.1  rmind int
   2265  1.3.18.1  rmind imxuart_init(struct imxuart_regs *regsp, int rate, tcflag_t cflag)
   2266  1.3.18.1  rmind {
   2267  1.3.18.1  rmind 	struct imxuart_baudrate_ratio ratio;
   2268  1.3.18.1  rmind 	int rfdiv = IMX_UFCR_DIVIDER_TO_RFDIV(imxuart_freqdiv);
   2269  1.3.18.1  rmind 	uint32_t ufcr;
   2270  1.3.18.1  rmind 
   2271  1.3.18.1  rmind 	if (bus_space_map(regsp->ur_iot, regsp->ur_iobase, IMX_UART_SIZE, 0,
   2272  1.3.18.1  rmind 		&regsp->ur_ioh))
   2273  1.3.18.1  rmind 		return ENOMEM; /* ??? */
   2274  1.3.18.1  rmind 
   2275  1.3.18.1  rmind 	if (imxuspeed(rate, &ratio) < 0)
   2276  1.3.18.1  rmind 		return EINVAL;
   2277  1.3.18.1  rmind 
   2278  1.3.18.1  rmind 	/* UBIR must updated before UBMR */
   2279  1.3.18.1  rmind 	bus_space_write_4(regsp->ur_iot, regsp->ur_ioh,
   2280  1.3.18.1  rmind 	    IMX_UBIR, ratio.numerator);
   2281  1.3.18.1  rmind 	bus_space_write_4(regsp->ur_iot, regsp->ur_ioh,
   2282  1.3.18.1  rmind 	    IMX_UBMR, ratio.modulator);
   2283  1.3.18.1  rmind 
   2284  1.3.18.1  rmind 
   2285  1.3.18.1  rmind 	/* XXX: DTREN, DPEC */
   2286  1.3.18.1  rmind 	bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_UCR3,
   2287  1.3.18.1  rmind 	    IMX_UCR3_DSR|IMX_UCR3_RXDMUXSEL);
   2288  1.3.18.1  rmind 
   2289  1.3.18.1  rmind 	ufcr = (8 << IMX_UFCR_TXTL_SHIFT) | (rfdiv << IMX_UFCR_RFDIV_SHIFT) |
   2290  1.3.18.1  rmind 		(1 << IMX_UFCR_RXTL_SHIFT);
   2291  1.3.18.1  rmind 	/* XXX: keep DCE/DTE bit */
   2292  1.3.18.1  rmind 	ufcr |= bus_space_read_4(regsp->ur_iot, regsp->ur_ioh, IMX_UFCR) &
   2293  1.3.18.1  rmind 		IMX_UFCR_DCEDTE;
   2294  1.3.18.1  rmind 
   2295  1.3.18.1  rmind 	bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_UFCR, ufcr);
   2296  1.3.18.1  rmind 
   2297  1.3.18.1  rmind 	bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_ONEMS,
   2298  1.3.18.1  rmind 	    imxuart_freq / imxuart_freqdiv / 1000);
   2299  1.3.18.1  rmind 
   2300  1.3.18.1  rmind 	bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_UCR2,
   2301  1.3.18.1  rmind 			  IMX_UCR2_IRTS|
   2302  1.3.18.1  rmind 			  IMX_UCR2_CTSC|
   2303  1.3.18.1  rmind 			  IMX_UCR2_WS|IMX_UCR2_TXEN|
   2304  1.3.18.1  rmind 			  IMX_UCR2_RXEN|IMX_UCR2_SRST);
   2305  1.3.18.1  rmind 	/* clear status registers */
   2306  1.3.18.1  rmind 	bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_USR1, 0xffff);
   2307  1.3.18.1  rmind 	bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_USR2, 0xffff);
   2308  1.3.18.1  rmind 
   2309  1.3.18.1  rmind 
   2310  1.3.18.1  rmind 	bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_UCR1,
   2311  1.3.18.1  rmind 	    IMX_UCR1_UARTEN);
   2312  1.3.18.1  rmind 
   2313  1.3.18.1  rmind 	return (0);
   2314  1.3.18.1  rmind }
   2315  1.3.18.1  rmind 
   2316  1.3.18.1  rmind 
   2317  1.3.18.1  rmind #endif
   2318  1.3.18.1  rmind 
   2319  1.3.18.1  rmind 
   2320  1.3.18.1  rmind #ifdef	IMXUARTCONSOLE
   2321  1.3.18.1  rmind /*
   2322  1.3.18.1  rmind  * Following are all routines needed for UART to act as console
   2323  1.3.18.1  rmind  */
   2324  1.3.18.1  rmind struct consdev imxucons = {
   2325  1.3.18.1  rmind 	NULL, NULL, imxucngetc, imxucnputc, imxucnpollc, NULL, NULL, NULL,
   2326  1.3.18.1  rmind 	NODEV, CN_NORMAL
   2327  1.3.18.1  rmind };
   2328  1.3.18.1  rmind 
   2329  1.3.18.1  rmind 
   2330  1.3.18.1  rmind int
   2331  1.3.18.1  rmind imxuart_cons_attach(bus_space_tag_t iot, paddr_t iobase, u_int rate,
   2332  1.3.18.1  rmind 		    tcflag_t cflag)
   2333  1.3.18.1  rmind {
   2334  1.3.18.1  rmind 	struct imxuart_regs regs;
   2335  1.3.18.1  rmind 	int res;
   2336  1.3.18.1  rmind 
   2337  1.3.18.1  rmind 	regs.ur_iot = iot;
   2338  1.3.18.1  rmind 	regs.ur_iobase = iobase;
   2339  1.3.18.1  rmind 
   2340  1.3.18.1  rmind 	res = imxuart_init(&regs, rate, cflag);
   2341  1.3.18.1  rmind 	if (res)
   2342  1.3.18.1  rmind 		return (res);
   2343  1.3.18.1  rmind 
   2344  1.3.18.1  rmind 	cn_tab = &imxucons;
   2345  1.3.18.1  rmind 	cn_init_magic(&imxuart_cnm_state);
   2346  1.3.18.1  rmind 	cn_set_magic("\047\001"); /* default magic is BREAK */
   2347  1.3.18.1  rmind 
   2348  1.3.18.1  rmind 	imxuconsrate = rate;
   2349  1.3.18.1  rmind 	imxuconscflag = cflag;
   2350  1.3.18.1  rmind 
   2351  1.3.18.1  rmind 	imxuconsregs = regs;
   2352  1.3.18.1  rmind 
   2353  1.3.18.1  rmind 	return 0;
   2354  1.3.18.1  rmind }
   2355  1.3.18.1  rmind 
   2356  1.3.18.1  rmind int
   2357  1.3.18.1  rmind imxucngetc(dev_t dev)
   2358  1.3.18.1  rmind {
   2359  1.3.18.1  rmind 	return (imxuart_common_getc(dev, &imxuconsregs));
   2360  1.3.18.1  rmind }
   2361  1.3.18.1  rmind 
   2362  1.3.18.1  rmind /*
   2363  1.3.18.1  rmind  * Console kernel output character routine.
   2364  1.3.18.1  rmind  */
   2365  1.3.18.1  rmind void
   2366  1.3.18.1  rmind imxucnputc(dev_t dev, int c)
   2367  1.3.18.1  rmind {
   2368  1.3.18.1  rmind 	imxuart_common_putc(dev, &imxuconsregs, c);
   2369  1.3.18.1  rmind }
   2370  1.3.18.1  rmind 
   2371  1.3.18.1  rmind void
   2372  1.3.18.1  rmind imxucnpollc(dev_t dev, int on)
   2373  1.3.18.1  rmind {
   2374  1.3.18.1  rmind 
   2375  1.3.18.1  rmind }
   2376  1.3.18.1  rmind 
   2377  1.3.18.1  rmind #endif	/* IMXUARTCONSOLE */
   2378  1.3.18.1  rmind 
   2379  1.3.18.1  rmind #ifdef KGDB
   2380  1.3.18.1  rmind int
   2381  1.3.18.1  rmind imxuart_kgdb_attach(bus_space_tag_t iot, paddr_t iobase, u_int rate,
   2382  1.3.18.1  rmind     tcflag_t cflag)
   2383  1.3.18.1  rmind {
   2384  1.3.18.1  rmind 	int res;
   2385  1.3.18.1  rmind 
   2386  1.3.18.1  rmind 	if (iot == imxuconsregs.ur_iot &&
   2387  1.3.18.1  rmind 	    iobase == imxuconsregs.ur_iobase) {
   2388  1.3.18.1  rmind #if !defined(DDB)
   2389  1.3.18.1  rmind 		return (EBUSY); /* cannot share with console */
   2390  1.3.18.1  rmind #else
   2391  1.3.18.1  rmind 		imxu_kgdb_regs.ur_iot = iot;
   2392  1.3.18.1  rmind 		imxu_kgdb_regs.ur_ioh = imxuconsregs.ur_ioh;
   2393  1.3.18.1  rmind 		imxu_kgdb_regs.ur_iobase = iobase;
   2394  1.3.18.1  rmind #endif
   2395  1.3.18.1  rmind 	} else {
   2396  1.3.18.1  rmind 		imxu_kgdb_regs.ur_iot = iot;
   2397  1.3.18.1  rmind 		imxu_kgdb_regs.ur_iobase = iobase;
   2398  1.3.18.1  rmind 
   2399  1.3.18.1  rmind 		res = imxuart_init(&imxu_kgdb_regs, rate, cflag);
   2400  1.3.18.1  rmind 		if (res)
   2401  1.3.18.1  rmind 			return (res);
   2402  1.3.18.1  rmind 
   2403  1.3.18.1  rmind 		/*
   2404  1.3.18.1  rmind 		 * XXXfvdl this shouldn't be needed, but the cn_magic goo
   2405  1.3.18.1  rmind 		 * expects this to be initialized
   2406  1.3.18.1  rmind 		 */
   2407  1.3.18.1  rmind 		cn_init_magic(&imxuart_cnm_state);
   2408  1.3.18.1  rmind 		cn_set_magic("\047\001");
   2409  1.3.18.1  rmind 	}
   2410  1.3.18.1  rmind 
   2411  1.3.18.1  rmind 	kgdb_attach(imxuart_kgdb_getc, imxuart_kgdb_putc, &imxu_kgdb_regs);
   2412  1.3.18.1  rmind 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
   2413  1.3.18.1  rmind 
   2414  1.3.18.1  rmind 	return (0);
   2415  1.3.18.1  rmind }
   2416  1.3.18.1  rmind 
   2417  1.3.18.1  rmind /* ARGSUSED */
   2418  1.3.18.1  rmind int
   2419  1.3.18.1  rmind imxuart_kgdb_getc(void *arg)
   2420  1.3.18.1  rmind {
   2421  1.3.18.1  rmind 	struct imxuart_regs *regs = arg;
   2422  1.3.18.1  rmind 
   2423  1.3.18.1  rmind 	return (imxuart_common_getc(NODEV, regs));
   2424  1.3.18.1  rmind }
   2425  1.3.18.1  rmind 
   2426  1.3.18.1  rmind /* ARGSUSED */
   2427  1.3.18.1  rmind void
   2428  1.3.18.1  rmind imxuart_kgdb_putc(void *arg, int c)
   2429  1.3.18.1  rmind {
   2430  1.3.18.1  rmind 	struct imxuart_regs *regs = arg;
   2431  1.3.18.1  rmind 
   2432  1.3.18.1  rmind 	imxuart_common_putc(NODEV, regs, c);
   2433  1.3.18.1  rmind }
   2434  1.3.18.1  rmind #endif /* KGDB */
   2435  1.3.18.1  rmind 
   2436  1.3.18.1  rmind /* helper function to identify the imxu ports used by
   2437  1.3.18.1  rmind  console or KGDB (and not yet autoconf attached) */
   2438  1.3.18.1  rmind int
   2439  1.3.18.1  rmind imxuart_is_console(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t *ioh)
   2440  1.3.18.1  rmind {
   2441  1.3.18.1  rmind 	bus_space_handle_t help;
   2442  1.3.18.1  rmind 
   2443  1.3.18.1  rmind 	if (!imxuconsattached &&
   2444  1.3.18.1  rmind 	    iot == imxuconsregs.ur_iot && iobase == imxuconsregs.ur_iobase)
   2445  1.3.18.1  rmind 		help = imxuconsregs.ur_ioh;
   2446  1.3.18.1  rmind #ifdef KGDB
   2447  1.3.18.1  rmind 	else if (!imxu_kgdb_attached &&
   2448  1.3.18.1  rmind 	    iot == imxu_kgdb_regs.ur_iot && iobase == imxu_kgdb_regs.ur_iobase)
   2449  1.3.18.1  rmind 		help = imxu_kgdb_regs.ur_ioh;
   2450  1.3.18.1  rmind #endif
   2451  1.3.18.1  rmind 	else
   2452  1.3.18.1  rmind 		return (0);
   2453  1.3.18.1  rmind 
   2454  1.3.18.1  rmind 	if (ioh)
   2455  1.3.18.1  rmind 		*ioh = help;
   2456  1.3.18.1  rmind 	return (1);
   2457  1.3.18.1  rmind }
   2458  1.3.18.1  rmind 
   2459  1.3.18.1  rmind #ifdef notyet
   2460  1.3.18.1  rmind 
   2461  1.3.18.1  rmind bool
   2462  1.3.18.1  rmind imxuart_cleanup(device_t self, int how)
   2463  1.3.18.1  rmind {
   2464  1.3.18.1  rmind /*
   2465  1.3.18.1  rmind  * this routine exists to serve as a shutdown hook for systems that
   2466  1.3.18.1  rmind  * have firmware which doesn't interact properly with a imxuart device in
   2467  1.3.18.1  rmind  * FIFO mode.
   2468  1.3.18.1  rmind  */
   2469  1.3.18.1  rmind 	struct imxuart_softc *sc = device_private(self);
   2470  1.3.18.1  rmind 
   2471  1.3.18.1  rmind 	if (ISSET(sc->sc_hwflags, IMXUART_HW_FIFO))
   2472  1.3.18.1  rmind 		UR_WRITE_1(&sc->sc_regs, IMXUART_REG_FIFO, 0);
   2473  1.3.18.1  rmind 
   2474  1.3.18.1  rmind 	return true;
   2475  1.3.18.1  rmind }
   2476  1.3.18.1  rmind #endif
   2477  1.3.18.1  rmind 
   2478  1.3.18.1  rmind #ifdef notyet
   2479  1.3.18.1  rmind bool
   2480  1.3.18.1  rmind imxuart_suspend(device_t self PMF_FN_ARGS)
   2481  1.3.18.1  rmind {
   2482  1.3.18.1  rmind 	struct imxuart_softc *sc = device_private(self);
   2483  1.3.18.1  rmind 
   2484  1.3.18.1  rmind 	UR_WRITE_1(&sc->sc_regs, IMXUART_REG_IER, 0);
   2485  1.3.18.1  rmind 	(void)CSR_READ_1(&sc->sc_regs, IMXUART_REG_IIR);
   2486  1.3.18.1  rmind 
   2487  1.3.18.1  rmind 	return true;
   2488  1.3.18.1  rmind }
   2489  1.3.18.1  rmind #endif
   2490  1.3.18.1  rmind 
   2491  1.3.18.1  rmind #ifdef notyet
   2492  1.3.18.1  rmind bool
   2493  1.3.18.1  rmind imxuart_resume(device_t self PMF_FN_ARGS)
   2494  1.3.18.1  rmind {
   2495  1.3.18.1  rmind 	struct imxuart_softc *sc = device_private(self);
   2496  1.3.18.1  rmind 
   2497  1.3.18.1  rmind 	mutex_spin_enter(&sc->sc_lock);
   2498  1.3.18.1  rmind 	imxuart_loadchannelregs(sc);
   2499  1.3.18.1  rmind 	mutex_spin_exit(&sc->sc_lock);
   2500  1.3.18.1  rmind 
   2501  1.3.18.1  rmind 	return true;
   2502  1.3.18.1  rmind }
   2503  1.3.18.1  rmind #endif
   2504  1.3.18.1  rmind 
   2505  1.3.18.1  rmind static void
   2506  1.3.18.1  rmind imxuart_enable_debugport(struct imxuart_softc *sc)
   2507  1.3.18.1  rmind {
   2508  1.3.18.1  rmind 	bus_space_tag_t iot = sc->sc_regs.ur_iot;
   2509  1.3.18.1  rmind 	bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
   2510  1.3.18.1  rmind 
   2511  1.3.18.1  rmind 	if (sc->sc_hwflags & (IMXUART_HW_CONSOLE|IMXUART_HW_KGDB)) {
   2512  1.3.18.1  rmind 
   2513  1.3.18.1  rmind 		/* Turn on line break interrupt, set carrier. */
   2514  1.3.18.1  rmind 
   2515  1.3.18.1  rmind 		sc->sc_ucr3 |= IMX_UCR3_DSR;
   2516  1.3.18.1  rmind 		bus_space_write_4(iot, ioh, IMX_UCR3, sc->sc_ucr3);
   2517  1.3.18.1  rmind 
   2518  1.3.18.1  rmind 		sc->sc_ucr4 |= IMX_UCR4_BKEN;
   2519  1.3.18.1  rmind 		bus_space_write_4(iot, ioh, IMX_UCR4, sc->sc_ucr4);
   2520  1.3.18.1  rmind 
   2521  1.3.18.1  rmind 		sc->sc_ucr2 |= IMX_UCR2_TXEN|IMX_UCR2_RXEN|
   2522  1.3.18.1  rmind 		    IMX_UCR2_CTS;
   2523  1.3.18.1  rmind 		bus_space_write_4(iot, ioh, IMX_UCR2, sc->sc_ucr2);
   2524  1.3.18.1  rmind 
   2525  1.3.18.1  rmind 		sc->sc_ucr1 |= IMX_UCR1_UARTEN;
   2526  1.3.18.1  rmind 		bus_space_write_4(iot, ioh, IMX_UCR1, sc->sc_ucr1);
   2527  1.3.18.1  rmind 	}
   2528  1.3.18.1  rmind }
   2529  1.3.18.1  rmind 
   2530  1.3.18.1  rmind 
   2531  1.3.18.1  rmind void
   2532  1.3.18.1  rmind imxuart_set_frequency(u_int freq, u_int div)
   2533  1.3.18.1  rmind {
   2534  1.3.18.1  rmind 	imxuart_freq = freq;
   2535  1.3.18.1  rmind 	imxuart_freqdiv = div;
   2536  1.3.18.1  rmind }
   2537