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