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