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