xref: /freebsd/sys/dev/uart/uart_dev_ns8250.c (revision 8165650389ba2d0a68cea6902ac3750055cad9da)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003 Marcel Moolenaar
5  * All rights reserved.
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "opt_acpi.h"
30 #include "opt_platform.h"
31 #include "opt_uart.h"
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/conf.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #include <machine/bus.h>
40 
41 #ifdef FDT
42 #include <dev/fdt/fdt_common.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #endif
46 
47 #include <dev/uart/uart.h>
48 #include <dev/uart/uart_cpu.h>
49 #ifdef FDT
50 #include <dev/uart/uart_cpu_fdt.h>
51 #endif
52 #include <dev/uart/uart_bus.h>
53 #include <dev/uart/uart_dev_ns8250.h>
54 #include <dev/uart/uart_ppstypes.h>
55 #ifdef DEV_ACPI
56 #include <dev/uart/uart_cpu_acpi.h>
57 #include <contrib/dev/acpica/include/acpi.h>
58 #endif
59 
60 #include <dev/ic/ns16550.h>
61 
62 #include "uart_if.h"
63 
64 #define	DEFAULT_RCLK	1843200
65 
66 /*
67  * Set the default baudrate tolerance to 3.0%.
68  *
69  * Some embedded boards have odd reference clocks (eg 25MHz)
70  * and we need to handle higher variances in the target baud rate.
71  */
72 #ifndef	UART_DEV_TOLERANCE_PCT
73 #define	UART_DEV_TOLERANCE_PCT	30
74 #endif	/* UART_DEV_TOLERANCE_PCT */
75 
76 static int broken_txfifo = 0;
77 SYSCTL_INT(_hw, OID_AUTO, broken_txfifo, CTLFLAG_RWTUN,
78 	&broken_txfifo, 0, "UART FIFO has QEMU emulation bug");
79 
80 /*
81  * To use early printf on x86, add the following to your kernel config:
82  *
83  * options UART_NS8250_EARLY_PORT=0x3f8
84  * options EARLY_PRINTF=ns8250
85 */
86 #if CHECK_EARLY_PRINTF(ns8250)
87 #if !(defined(__amd64__) || defined(__i386__))
88 #error ns8250 early putc is x86 specific as it uses inb/outb
89 #endif
90 static void
91 uart_ns8250_early_putc(int c)
92 {
93 	u_int stat = UART_NS8250_EARLY_PORT + REG_LSR;
94 	u_int tx = UART_NS8250_EARLY_PORT + REG_DATA;
95 	int limit = 10000; /* 10ms is plenty of time */
96 
97 	while ((inb(stat) & LSR_THRE) == 0 && --limit > 0)
98 		continue;
99 	outb(tx, c);
100 }
101 early_putc_t *early_putc = uart_ns8250_early_putc;
102 #endif /* EARLY_PRINTF */
103 
104 /*
105  * Clear pending interrupts. THRE is cleared by reading IIR. Data
106  * that may have been received gets lost here.
107  */
108 static void
109 ns8250_clrint(struct uart_bas *bas)
110 {
111 	uint8_t iir, lsr;
112 
113 	iir = uart_getreg(bas, REG_IIR);
114 	while ((iir & IIR_NOPEND) == 0) {
115 		iir &= IIR_IMASK;
116 		if (iir == IIR_RLS) {
117 			lsr = uart_getreg(bas, REG_LSR);
118 			if (lsr & (LSR_BI|LSR_FE|LSR_PE))
119 				(void)uart_getreg(bas, REG_DATA);
120 		} else if (iir == IIR_RXRDY || iir == IIR_RXTOUT)
121 			(void)uart_getreg(bas, REG_DATA);
122 		else if (iir == IIR_MLSC)
123 			(void)uart_getreg(bas, REG_MSR);
124 		uart_barrier(bas);
125 		iir = uart_getreg(bas, REG_IIR);
126 	}
127 }
128 
129 static int
130 ns8250_delay(struct uart_bas *bas)
131 {
132 	int divisor;
133 	u_char lcr;
134 
135 	lcr = uart_getreg(bas, REG_LCR);
136 	uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
137 	uart_barrier(bas);
138 	divisor = uart_getreg(bas, REG_DLL) | (uart_getreg(bas, REG_DLH) << 8);
139 	uart_barrier(bas);
140 	uart_setreg(bas, REG_LCR, lcr);
141 	uart_barrier(bas);
142 
143 	/* 1/10th the time to transmit 1 character (estimate). */
144 	if (divisor <= 134)
145 		return (16000000 * divisor / bas->rclk);
146 	return (16000 * divisor / (bas->rclk / 1000));
147 }
148 
149 static int
150 ns8250_divisor(int rclk, int baudrate)
151 {
152 	int actual_baud, divisor;
153 	int error;
154 
155 	if (baudrate == 0)
156 		return (0);
157 
158 	divisor = (rclk / (baudrate << 3) + 1) >> 1;
159 	if (divisor == 0 || divisor >= 65536)
160 		return (0);
161 	actual_baud = rclk / (divisor << 4);
162 
163 	/* 10 times error in percent: */
164 	error = ((actual_baud - baudrate) * 2000 / baudrate + 1) / 2;
165 
166 	/* enforce maximum error tolerance: */
167 	if (error < -UART_DEV_TOLERANCE_PCT || error > UART_DEV_TOLERANCE_PCT)
168 		return (0);
169 
170 	return (divisor);
171 }
172 
173 static int
174 ns8250_drain(struct uart_bas *bas, int what)
175 {
176 	int delay, limit;
177 
178 	delay = ns8250_delay(bas);
179 
180 	if (what & UART_DRAIN_TRANSMITTER) {
181 		/*
182 		 * Pick an arbitrary high limit to avoid getting stuck in
183 		 * an infinite loop when the hardware is broken. Make the
184 		 * limit high enough to handle large FIFOs.
185 		 */
186 		limit = 10*1024;
187 		while ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0 && --limit)
188 			DELAY(delay);
189 		if (limit == 0) {
190 			/* printf("ns8250: transmitter appears stuck... "); */
191 			return (EIO);
192 		}
193 	}
194 
195 	if (what & UART_DRAIN_RECEIVER) {
196 		/*
197 		 * Pick an arbitrary high limit to avoid getting stuck in
198 		 * an infinite loop when the hardware is broken. Make the
199 		 * limit high enough to handle large FIFOs and integrated
200 		 * UARTs. The HP rx2600 for example has 3 UARTs on the
201 		 * management board that tend to get a lot of data send
202 		 * to it when the UART is first activated.  Assume that we
203 		 * have finished draining if LSR_RXRDY is not asserted both
204 		 * prior to and after a DELAY; but as long as LSR_RXRDY is
205 		 * asserted, read (and discard) characters as quickly as
206 		 * possible.
207 		 */
208 		limit=10*4096;
209 		while (limit && (uart_getreg(bas, REG_LSR) & LSR_RXRDY) && --limit) {
210 			do {
211 				(void)uart_getreg(bas, REG_DATA);
212 				uart_barrier(bas);
213 			} while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) && --limit);
214 			uart_barrier(bas);
215 			DELAY(delay << 2);
216 		}
217 		if (limit == 0) {
218 			/* printf("ns8250: receiver appears broken... "); */
219 			return (EIO);
220 		}
221 	}
222 
223 	return (0);
224 }
225 
226 /*
227  * We can only flush UARTs with FIFOs. UARTs without FIFOs should be
228  * drained. WARNING: this function clobbers the FIFO setting!
229  */
230 static void
231 ns8250_flush(struct uart_bas *bas, int what)
232 {
233 	uint8_t fcr;
234 	uint8_t lsr;
235 	int drain = 0;
236 
237 	fcr = FCR_ENABLE;
238 	if (what & UART_FLUSH_TRANSMITTER)
239 		fcr |= FCR_XMT_RST;
240 	if (what & UART_FLUSH_RECEIVER)
241 		fcr |= FCR_RCV_RST;
242 	uart_setreg(bas, REG_FCR, fcr);
243 	uart_barrier(bas);
244 
245 	/*
246 	 * Detect and work around emulated UARTs which don't implement the
247 	 * FCR register; on these systems we need to drain the FIFO since
248 	 * the flush we request doesn't happen.  One such system is the
249 	 * Firecracker VMM, aka. the rust-vmm/vm-superio emulation code:
250 	 * https://github.com/rust-vmm/vm-superio/issues/83
251 	 */
252 	lsr = uart_getreg(bas, REG_LSR);
253 	if (((lsr & LSR_TEMT) == 0) && (what & UART_FLUSH_TRANSMITTER))
254 		drain |= UART_DRAIN_TRANSMITTER;
255 	if ((lsr & LSR_RXRDY) && (what & UART_FLUSH_RECEIVER))
256 		drain |= UART_DRAIN_RECEIVER;
257 	if (drain != 0) {
258 		printf("ns8250: UART FCR is broken\n");
259 		ns8250_drain(bas, drain);
260 	}
261 }
262 
263 static int
264 ns8250_param(struct uart_bas *bas, int baudrate, int databits, int stopbits,
265     int parity)
266 {
267 	int divisor;
268 	uint8_t lcr;
269 
270 	/* Don't change settings when running on Hyper-V */
271 	if (vm_guest == VM_GUEST_HV)
272 		return (0);
273 
274 	lcr = 0;
275 	if (databits >= 8)
276 		lcr |= LCR_8BITS;
277 	else if (databits == 7)
278 		lcr |= LCR_7BITS;
279 	else if (databits == 6)
280 		lcr |= LCR_6BITS;
281 	else
282 		lcr |= LCR_5BITS;
283 	if (stopbits > 1)
284 		lcr |= LCR_STOPB;
285 	lcr |= parity << 3;
286 
287 	/* Set baudrate. */
288 	if (baudrate > 0) {
289 		divisor = ns8250_divisor(bas->rclk, baudrate);
290 		if (divisor == 0)
291 			return (EINVAL);
292 		uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
293 		uart_barrier(bas);
294 		uart_setreg(bas, REG_DLL, divisor & 0xff);
295 		uart_setreg(bas, REG_DLH, (divisor >> 8) & 0xff);
296 		uart_barrier(bas);
297 	}
298 
299 	/* Set LCR and clear DLAB. */
300 	uart_setreg(bas, REG_LCR, lcr);
301 	uart_barrier(bas);
302 	return (0);
303 }
304 
305 /*
306  * Low-level UART interface.
307  */
308 static int ns8250_probe(struct uart_bas *bas);
309 static void ns8250_init(struct uart_bas *bas, int, int, int, int);
310 static void ns8250_term(struct uart_bas *bas);
311 static void ns8250_putc(struct uart_bas *bas, int);
312 static int ns8250_rxready(struct uart_bas *bas);
313 static int ns8250_getc(struct uart_bas *bas, struct mtx *);
314 
315 struct uart_ops uart_ns8250_ops = {
316 	.probe = ns8250_probe,
317 	.init = ns8250_init,
318 	.term = ns8250_term,
319 	.putc = ns8250_putc,
320 	.rxready = ns8250_rxready,
321 	.getc = ns8250_getc,
322 };
323 
324 static int
325 ns8250_probe(struct uart_bas *bas)
326 {
327 	u_char val;
328 
329 	/* Check known 0 bits that don't depend on DLAB. */
330 	val = uart_getreg(bas, REG_IIR);
331 	if (val & 0x30)
332 		return (ENXIO);
333 	/*
334 	 * Bit 6 of the MCR (= 0x40) appears to be 1 for the Sun1699
335 	 * chip, but otherwise doesn't seem to have a function. In
336 	 * other words, uart(4) works regardless. Ignore that bit so
337 	 * the probe succeeds.
338 	 */
339 	val = uart_getreg(bas, REG_MCR);
340 	if (val & 0xa0)
341 		return (ENXIO);
342 
343 	return (0);
344 }
345 
346 static void
347 ns8250_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
348     int parity)
349 {
350 	u_char ier;
351 
352 	if (bas->rclk == 0)
353 		bas->rclk = DEFAULT_RCLK;
354 	ns8250_param(bas, baudrate, databits, stopbits, parity);
355 
356 	/* Disable all interrupt sources. */
357 	/*
358 	 * We use 0xe0 instead of 0xf0 as the mask because the XScale PXA
359 	 * UARTs split the receive time-out interrupt bit out separately as
360 	 * 0x10.  This gets handled by ier_mask and ier_rxbits below.
361 	 */
362 	ier = uart_getreg(bas, REG_IER) & 0xe0;
363 	uart_setreg(bas, REG_IER, ier);
364 	uart_barrier(bas);
365 
366 	/* Disable the FIFO (if present). */
367 	uart_setreg(bas, REG_FCR, 0);
368 	uart_barrier(bas);
369 
370 	/* Set RTS & DTR. */
371 	uart_setreg(bas, REG_MCR, MCR_IE | MCR_RTS | MCR_DTR);
372 	uart_barrier(bas);
373 
374 	ns8250_clrint(bas);
375 }
376 
377 static void
378 ns8250_term(struct uart_bas *bas)
379 {
380 
381 	/* Clear RTS & DTR. */
382 	uart_setreg(bas, REG_MCR, MCR_IE);
383 	uart_barrier(bas);
384 }
385 
386 static void
387 ns8250_putc(struct uart_bas *bas, int c)
388 {
389 	int limit;
390 
391 	if (vm_guest != VM_GUEST_HV) {
392 		limit = 250000;
393 		while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0 && --limit)
394 			DELAY(4);
395 	}
396 	uart_setreg(bas, REG_DATA, c);
397 	uart_barrier(bas);
398 }
399 
400 static int
401 ns8250_rxready(struct uart_bas *bas)
402 {
403 
404 	return ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) != 0 ? 1 : 0);
405 }
406 
407 static int
408 ns8250_getc(struct uart_bas *bas, struct mtx *hwmtx)
409 {
410 	int c;
411 
412 	uart_lock(hwmtx);
413 
414 	while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) == 0) {
415 		uart_unlock(hwmtx);
416 		DELAY(4);
417 		uart_lock(hwmtx);
418 	}
419 
420 	c = uart_getreg(bas, REG_DATA);
421 
422 	uart_unlock(hwmtx);
423 
424 	return (c);
425 }
426 
427 static kobj_method_t ns8250_methods[] = {
428 	KOBJMETHOD(uart_attach,		ns8250_bus_attach),
429 	KOBJMETHOD(uart_detach,		ns8250_bus_detach),
430 	KOBJMETHOD(uart_flush,		ns8250_bus_flush),
431 	KOBJMETHOD(uart_getsig,		ns8250_bus_getsig),
432 	KOBJMETHOD(uart_ioctl,		ns8250_bus_ioctl),
433 	KOBJMETHOD(uart_ipend,		ns8250_bus_ipend),
434 	KOBJMETHOD(uart_param,		ns8250_bus_param),
435 	KOBJMETHOD(uart_probe,		ns8250_bus_probe),
436 	KOBJMETHOD(uart_receive,	ns8250_bus_receive),
437 	KOBJMETHOD(uart_setsig,		ns8250_bus_setsig),
438 	KOBJMETHOD(uart_transmit,	ns8250_bus_transmit),
439 	KOBJMETHOD(uart_txbusy,		ns8250_bus_txbusy),
440 	KOBJMETHOD(uart_grab,		ns8250_bus_grab),
441 	KOBJMETHOD(uart_ungrab,		ns8250_bus_ungrab),
442 	KOBJMETHOD_END
443 };
444 
445 struct uart_class uart_ns8250_class = {
446 	"ns8250",
447 	ns8250_methods,
448 	sizeof(struct ns8250_softc),
449 	.uc_ops = &uart_ns8250_ops,
450 	.uc_range = 8,
451 	.uc_rclk = DEFAULT_RCLK,
452 	.uc_rshift = 0
453 };
454 UART_CLASS(uart_ns8250_class);
455 
456 /*
457  * XXX -- refactor out ACPI and FDT ifdefs
458  */
459 #ifdef DEV_ACPI
460 static struct acpi_uart_compat_data acpi_compat_data[] = {
461 	{"AMD0020",	&uart_ns8250_class, 0, 2, 0, 48000000, UART_F_BUSY_DETECT, "AMD / Synopsys Designware UART"},
462 	{"AMDI0020", &uart_ns8250_class, 0, 2, 0, 48000000, UART_F_BUSY_DETECT, "AMD / Synopsys Designware UART"},
463 	{"APMC0D08", &uart_ns8250_class, ACPI_DBG2_16550_COMPATIBLE, 2, 4, 0, 0, "APM compatible UART"},
464 	{"MRVL0001", &uart_ns8250_class, ACPI_DBG2_16550_SUBSET, 2, 0, 200000000, UART_F_BUSY_DETECT, "Marvell / Synopsys Designware UART"},
465 	{"SCX0006",  &uart_ns8250_class, 0, 2, 0, 62500000, UART_F_BUSY_DETECT, "SynQuacer / Synopsys Designware UART"},
466 	{"HISI0031", &uart_ns8250_class, 0, 2, 0, 200000000, UART_F_BUSY_DETECT, "HiSilicon / Synopsys Designware UART"},
467 	{"NXP0018", &uart_ns8250_class, 0, 0, 0, 350000000, UART_F_BUSY_DETECT, "NXP / Synopsys Designware UART"},
468 	{"PNP0500", &uart_ns8250_class, 0, 0, 0, 0, 0, "Standard PC COM port"},
469 	{"PNP0501", &uart_ns8250_class, 0, 0, 0, 0, 0, "16550A-compatible COM port"},
470 	{"PNP0502", &uart_ns8250_class, 0, 0, 0, 0, 0, "Multiport serial device (non-intelligent 16550)"},
471 	{"PNP0510", &uart_ns8250_class, 0, 0, 0, 0, 0, "Generic IRDA-compatible device"},
472 	{"PNP0511", &uart_ns8250_class, 0, 0, 0, 0, 0, "Generic IRDA-compatible device"},
473 	{"WACF004", &uart_ns8250_class, 0, 0, 0, 0, 0, "Wacom Tablet PC Screen"},
474 	{"WACF00E", &uart_ns8250_class, 0, 0, 0, 0, 0, "Wacom Tablet PC Screen 00e"},
475 	{"FUJ02E5", &uart_ns8250_class, 0, 0, 0, 0, 0, "Wacom Tablet at FuS Lifebook T"},
476 	{NULL, 			NULL, 0, 0 , 0, 0, 0, NULL},
477 };
478 UART_ACPI_CLASS_AND_DEVICE(acpi_compat_data);
479 #endif
480 
481 #ifdef FDT
482 static struct ofw_compat_data compat_data[] = {
483 	{"ns16550",		(uintptr_t)&uart_ns8250_class},
484 	{"ns16550a",		(uintptr_t)&uart_ns8250_class},
485 	{NULL,			(uintptr_t)NULL},
486 };
487 UART_FDT_CLASS_AND_DEVICE(compat_data);
488 #endif
489 
490 /* Use token-pasting to form SER_ and MSR_ named constants. */
491 #define	SER(sig)	SER_##sig
492 #define	SERD(sig)	SER_D##sig
493 #define	MSR(sig)	MSR_##sig
494 #define	MSRD(sig)	MSR_D##sig
495 
496 /*
497  * Detect signal changes using software delta detection.  The previous state of
498  * the signals is in 'var' the new hardware state is in 'msr', and 'sig' is the
499  * short name (DCD, CTS, etc) of the signal bit being processed; 'var' gets the
500  * new state of both the signal and the delta bits.
501  */
502 #define SIGCHGSW(var, msr, sig)					\
503 	if ((msr) & MSR(sig)) {					\
504 		if ((var & SER(sig)) == 0)			\
505 			var |= SERD(sig) | SER(sig);		\
506 	} else {						\
507 		if ((var & SER(sig)) != 0)			\
508 			var = SERD(sig) | (var & ~SER(sig));	\
509 	}
510 
511 /*
512  * Detect signal changes using the hardware msr delta bits.  This is currently
513  * used only when PPS timing information is being captured using the "narrow
514  * pulse" option.  With a narrow PPS pulse the signal may not still be asserted
515  * by time the interrupt handler is invoked.  The hardware will latch the fact
516  * that it changed in the delta bits.
517  */
518 #define SIGCHGHW(var, msr, sig)					\
519 	if ((msr) & MSRD(sig)) {				\
520 		if (((msr) & MSR(sig)) != 0)			\
521 			var |= SERD(sig) | SER(sig);		\
522 		else						\
523 			var = SERD(sig) | (var & ~SER(sig));	\
524 	}
525 
526 int
527 ns8250_bus_attach(struct uart_softc *sc)
528 {
529 	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
530 	struct uart_bas *bas;
531 	unsigned int ivar;
532 #ifdef FDT
533 	phandle_t node;
534 	pcell_t cell;
535 #endif
536 
537 #ifdef FDT
538 	/* Check whether uart has a broken txfifo. */
539 	node = ofw_bus_get_node(sc->sc_dev);
540 	if ((OF_getencprop(node, "broken-txfifo", &cell, sizeof(cell))) > 0)
541 		broken_txfifo =  cell ? 1 : 0;
542 #endif
543 
544 	bas = &sc->sc_bas;
545 
546 	ns8250->busy_detect = bas->busy_detect;
547 	ns8250->mcr = uart_getreg(bas, REG_MCR);
548 	ns8250->fcr = FCR_ENABLE;
549 	if (!resource_int_value("uart", device_get_unit(sc->sc_dev), "flags",
550 	    &ivar)) {
551 		if (UART_FLAGS_FCR_RX_LOW(ivar))
552 			ns8250->fcr |= FCR_RX_LOW;
553 		else if (UART_FLAGS_FCR_RX_MEDL(ivar))
554 			ns8250->fcr |= FCR_RX_MEDL;
555 		else if (UART_FLAGS_FCR_RX_HIGH(ivar))
556 			ns8250->fcr |= FCR_RX_HIGH;
557 		else
558 			ns8250->fcr |= FCR_RX_MEDH;
559 	} else
560 		ns8250->fcr |= FCR_RX_MEDH;
561 
562 	/* Get IER mask */
563 	ivar = 0xf0;
564 	resource_int_value("uart", device_get_unit(sc->sc_dev), "ier_mask",
565 	    &ivar);
566 	ns8250->ier_mask = (uint8_t)(ivar & 0xff);
567 
568 	/* Get IER RX interrupt bits */
569 	ivar = IER_EMSC | IER_ERLS | IER_ERXRDY;
570 	resource_int_value("uart", device_get_unit(sc->sc_dev), "ier_rxbits",
571 	    &ivar);
572 	ns8250->ier_rxbits = (uint8_t)(ivar & 0xff);
573 
574 	uart_setreg(bas, REG_FCR, ns8250->fcr);
575 	uart_barrier(bas);
576 	ns8250_bus_flush(sc, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
577 
578 	if (ns8250->mcr & MCR_DTR)
579 		sc->sc_hwsig |= SER_DTR;
580 	if (ns8250->mcr & MCR_RTS)
581 		sc->sc_hwsig |= SER_RTS;
582 	ns8250_bus_getsig(sc);
583 
584 	ns8250_clrint(bas);
585 	ns8250->ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask;
586 	ns8250->ier |= ns8250->ier_rxbits;
587 	uart_setreg(bas, REG_IER, ns8250->ier);
588 	uart_barrier(bas);
589 
590 	/*
591 	 * Timing of the H/W access was changed with r253161 of uart_core.c
592 	 * It has been observed that an ITE IT8513E would signal a break
593 	 * condition with pretty much every character it received, unless
594 	 * it had enough time to settle between ns8250_bus_attach() and
595 	 * ns8250_bus_ipend() -- which it accidentally had before r253161.
596 	 * It's not understood why the UART chip behaves this way and it
597 	 * could very well be that the DELAY make the H/W work in the same
598 	 * accidental manner as before. More analysis is warranted, but
599 	 * at least now we fixed a known regression.
600 	 */
601 	DELAY(200);
602 	return (0);
603 }
604 
605 int
606 ns8250_bus_detach(struct uart_softc *sc)
607 {
608 	struct ns8250_softc *ns8250;
609 	struct uart_bas *bas;
610 	u_char ier;
611 
612 	ns8250 = (struct ns8250_softc *)sc;
613 	bas = &sc->sc_bas;
614 	ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask;
615 	uart_setreg(bas, REG_IER, ier);
616 	uart_barrier(bas);
617 	ns8250_clrint(bas);
618 	return (0);
619 }
620 
621 int
622 ns8250_bus_flush(struct uart_softc *sc, int what)
623 {
624 	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
625 	struct uart_bas *bas;
626 	int error;
627 
628 	bas = &sc->sc_bas;
629 	uart_lock(sc->sc_hwmtx);
630 	if (sc->sc_rxfifosz > 1) {
631 		ns8250_flush(bas, what);
632 		uart_setreg(bas, REG_FCR, ns8250->fcr);
633 		uart_barrier(bas);
634 		error = 0;
635 	} else
636 		error = ns8250_drain(bas, what);
637 	uart_unlock(sc->sc_hwmtx);
638 	return (error);
639 }
640 
641 int
642 ns8250_bus_getsig(struct uart_softc *sc)
643 {
644 	uint32_t old, sig;
645 	uint8_t msr;
646 
647 	/*
648 	 * The delta bits are reputed to be broken on some hardware, so use
649 	 * software delta detection by default.  Use the hardware delta bits
650 	 * when capturing PPS pulses which are too narrow for software detection
651 	 * to see the edges.  Hardware delta for RI doesn't work like the
652 	 * others, so always use software for it.  Other threads may be changing
653 	 * other (non-MSR) bits in sc_hwsig, so loop until it can successfully
654 	 * update without other changes happening.  Note that the SIGCHGxx()
655 	 * macros carefully preserve the delta bits when we have to loop several
656 	 * times and a signal transitions between iterations.
657 	 */
658 	do {
659 		old = sc->sc_hwsig;
660 		sig = old;
661 		uart_lock(sc->sc_hwmtx);
662 		msr = uart_getreg(&sc->sc_bas, REG_MSR);
663 		uart_unlock(sc->sc_hwmtx);
664 		if (sc->sc_pps_mode & UART_PPS_NARROW_PULSE) {
665 			SIGCHGHW(sig, msr, DSR);
666 			SIGCHGHW(sig, msr, CTS);
667 			SIGCHGHW(sig, msr, DCD);
668 		} else {
669 			SIGCHGSW(sig, msr, DSR);
670 			SIGCHGSW(sig, msr, CTS);
671 			SIGCHGSW(sig, msr, DCD);
672 		}
673 		SIGCHGSW(sig, msr, RI);
674 	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, sig & ~SER_MASK_DELTA));
675 	return (sig);
676 }
677 
678 int
679 ns8250_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
680 {
681 	struct uart_bas *bas;
682 	int baudrate, divisor, error;
683 	uint8_t efr, lcr;
684 
685 	bas = &sc->sc_bas;
686 	error = 0;
687 	uart_lock(sc->sc_hwmtx);
688 	switch (request) {
689 	case UART_IOCTL_BREAK:
690 		lcr = uart_getreg(bas, REG_LCR);
691 		if (data)
692 			lcr |= LCR_SBREAK;
693 		else
694 			lcr &= ~LCR_SBREAK;
695 		uart_setreg(bas, REG_LCR, lcr);
696 		uart_barrier(bas);
697 		break;
698 	case UART_IOCTL_IFLOW:
699 		lcr = uart_getreg(bas, REG_LCR);
700 		uart_barrier(bas);
701 		uart_setreg(bas, REG_LCR, 0xbf);
702 		uart_barrier(bas);
703 		efr = uart_getreg(bas, REG_EFR);
704 		if (data)
705 			efr |= EFR_RTS;
706 		else
707 			efr &= ~EFR_RTS;
708 		uart_setreg(bas, REG_EFR, efr);
709 		uart_barrier(bas);
710 		uart_setreg(bas, REG_LCR, lcr);
711 		uart_barrier(bas);
712 		break;
713 	case UART_IOCTL_OFLOW:
714 		lcr = uart_getreg(bas, REG_LCR);
715 		uart_barrier(bas);
716 		uart_setreg(bas, REG_LCR, 0xbf);
717 		uart_barrier(bas);
718 		efr = uart_getreg(bas, REG_EFR);
719 		if (data)
720 			efr |= EFR_CTS;
721 		else
722 			efr &= ~EFR_CTS;
723 		uart_setreg(bas, REG_EFR, efr);
724 		uart_barrier(bas);
725 		uart_setreg(bas, REG_LCR, lcr);
726 		uart_barrier(bas);
727 		break;
728 	case UART_IOCTL_BAUD:
729 		lcr = uart_getreg(bas, REG_LCR);
730 		uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
731 		uart_barrier(bas);
732 		divisor = uart_getreg(bas, REG_DLL) |
733 		    (uart_getreg(bas, REG_DLH) << 8);
734 		uart_barrier(bas);
735 		uart_setreg(bas, REG_LCR, lcr);
736 		uart_barrier(bas);
737 		baudrate = (divisor > 0) ? bas->rclk / divisor / 16 : 0;
738 		if (baudrate > 0)
739 			*(int*)data = baudrate;
740 		else
741 			error = ENXIO;
742 		break;
743 	default:
744 		error = EINVAL;
745 		break;
746 	}
747 	uart_unlock(sc->sc_hwmtx);
748 	return (error);
749 }
750 
751 int
752 ns8250_bus_ipend(struct uart_softc *sc)
753 {
754 	struct uart_bas *bas;
755 	struct ns8250_softc *ns8250;
756 	int ipend;
757 	uint8_t iir, lsr;
758 
759 	ns8250 = (struct ns8250_softc *)sc;
760 	bas = &sc->sc_bas;
761 	uart_lock(sc->sc_hwmtx);
762 	iir = uart_getreg(bas, REG_IIR);
763 
764 	if (ns8250->busy_detect && (iir & IIR_BUSY) == IIR_BUSY) {
765 		(void)uart_getreg(bas, DW_REG_USR);
766 		uart_unlock(sc->sc_hwmtx);
767 		return (0);
768 	}
769 	if (iir & IIR_NOPEND) {
770 		uart_unlock(sc->sc_hwmtx);
771 		return (0);
772 	}
773 	ipend = 0;
774 	if (iir & IIR_RXRDY) {
775 		lsr = uart_getreg(bas, REG_LSR);
776 		if (lsr & LSR_OE)
777 			ipend |= SER_INT_OVERRUN;
778 		if (lsr & LSR_BI)
779 			ipend |= SER_INT_BREAK;
780 		if (lsr & LSR_RXRDY)
781 			ipend |= SER_INT_RXREADY;
782 	} else {
783 		if (iir & IIR_TXRDY) {
784 			ipend |= SER_INT_TXIDLE;
785 			ns8250->ier &= ~IER_ETXRDY;
786 			uart_setreg(bas, REG_IER, ns8250->ier);
787 			uart_barrier(bas);
788 		} else
789 			ipend |= SER_INT_SIGCHG;
790 	}
791 	if (ipend == 0)
792 		ns8250_clrint(bas);
793 	uart_unlock(sc->sc_hwmtx);
794 	return (ipend);
795 }
796 
797 int
798 ns8250_bus_param(struct uart_softc *sc, int baudrate, int databits,
799     int stopbits, int parity)
800 {
801 	struct ns8250_softc *ns8250;
802 	struct uart_bas *bas;
803 	int error, limit;
804 
805 	ns8250 = (struct ns8250_softc*)sc;
806 	bas = &sc->sc_bas;
807 	uart_lock(sc->sc_hwmtx);
808 	/*
809 	 * When using DW UART with BUSY detection it is necessary to wait
810 	 * until all serial transfers are finished before manipulating the
811 	 * line control. LCR will not be affected when UART is busy.
812 	 */
813 	if (ns8250->busy_detect != 0) {
814 		/*
815 		 * Pick an arbitrary high limit to avoid getting stuck in
816 		 * an infinite loop in case when the hardware is broken.
817 		 */
818 		limit = 10 * 1024;
819 		while (((uart_getreg(bas, DW_REG_USR) & USR_BUSY) != 0) &&
820 		    --limit)
821 			DELAY(4);
822 
823 		if (limit <= 0) {
824 			/* UART appears to be stuck */
825 			uart_unlock(sc->sc_hwmtx);
826 			return (EIO);
827 		}
828 	}
829 
830 	error = ns8250_param(bas, baudrate, databits, stopbits, parity);
831 	uart_unlock(sc->sc_hwmtx);
832 	return (error);
833 }
834 
835 int
836 ns8250_bus_probe(struct uart_softc *sc)
837 {
838 	struct uart_bas *bas;
839 	int count, delay, error, limit;
840 	uint8_t lsr, mcr, ier;
841 
842 	bas = &sc->sc_bas;
843 
844 	error = ns8250_probe(bas);
845 	if (error)
846 		return (error);
847 
848 	mcr = MCR_IE;
849 	if (sc->sc_sysdev == NULL) {
850 		/* By using ns8250_init() we also set DTR and RTS. */
851 		ns8250_init(bas, 115200, 8, 1, UART_PARITY_NONE);
852 	} else
853 		mcr |= MCR_DTR | MCR_RTS;
854 
855 	error = ns8250_drain(bas, UART_DRAIN_TRANSMITTER);
856 	if (error)
857 		return (error);
858 
859 	/*
860 	 * Set loopback mode. This avoids having garbage on the wire and
861 	 * also allows us send and receive data. We set DTR and RTS to
862 	 * avoid the possibility that automatic flow-control prevents
863 	 * any data from being sent.
864 	 */
865 	uart_setreg(bas, REG_MCR, MCR_LOOPBACK | MCR_IE | MCR_DTR | MCR_RTS);
866 	uart_barrier(bas);
867 
868 	/*
869 	 * Enable FIFOs. And check that the UART has them. If not, we're
870 	 * done. Since this is the first time we enable the FIFOs, we reset
871 	 * them.
872 	 */
873 	uart_setreg(bas, REG_FCR, FCR_ENABLE);
874 	uart_barrier(bas);
875 	if (!(uart_getreg(bas, REG_IIR) & IIR_FIFO_MASK)) {
876 		/*
877 		 * NS16450 or INS8250. We don't bother to differentiate
878 		 * between them. They're too old to be interesting.
879 		 */
880 		uart_setreg(bas, REG_MCR, mcr);
881 		uart_barrier(bas);
882 		sc->sc_rxfifosz = sc->sc_txfifosz = 1;
883 		device_set_desc(sc->sc_dev, "8250 or 16450 or compatible");
884 		return (0);
885 	}
886 
887 	uart_setreg(bas, REG_FCR, FCR_ENABLE | FCR_XMT_RST | FCR_RCV_RST);
888 	uart_barrier(bas);
889 
890 	count = 0;
891 	delay = ns8250_delay(bas);
892 
893 	/* We have FIFOs. Drain the transmitter and receiver. */
894 	error = ns8250_drain(bas, UART_DRAIN_RECEIVER|UART_DRAIN_TRANSMITTER);
895 	if (error) {
896 		uart_setreg(bas, REG_MCR, mcr);
897 		uart_setreg(bas, REG_FCR, 0);
898 		uart_barrier(bas);
899 		goto describe;
900 	}
901 
902 	/*
903 	 * We should have a sufficiently clean "pipe" to determine the
904 	 * size of the FIFOs. We send as much characters as is reasonable
905 	 * and wait for the overflow bit in the LSR register to be
906 	 * asserted, counting the characters as we send them. Based on
907 	 * that count we know the FIFO size.
908 	 */
909 	do {
910 		uart_setreg(bas, REG_DATA, 0);
911 		uart_barrier(bas);
912 		count++;
913 
914 		limit = 30;
915 		lsr = 0;
916 		/*
917 		 * LSR bits are cleared upon read, so we must accumulate
918 		 * them to be able to test LSR_OE below.
919 		 */
920 		while (((lsr |= uart_getreg(bas, REG_LSR)) & LSR_TEMT) == 0 &&
921 		    --limit)
922 			DELAY(delay);
923 		if (limit == 0) {
924 			/* See the comment in ns8250_init(). */
925 			ier = uart_getreg(bas, REG_IER) & 0xe0;
926 			uart_setreg(bas, REG_IER, ier);
927 			uart_setreg(bas, REG_MCR, mcr);
928 			uart_setreg(bas, REG_FCR, 0);
929 			uart_barrier(bas);
930 			count = 0;
931 			goto describe;
932 		}
933 	} while ((lsr & LSR_OE) == 0 && count < 260);
934 	count--;
935 
936 	uart_setreg(bas, REG_MCR, mcr);
937 
938 	/* Reset FIFOs. */
939 	ns8250_flush(bas, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
940 
941  describe:
942 	if (count >= 14 && count <= 16) {
943 		sc->sc_rxfifosz = 16;
944 		device_set_desc(sc->sc_dev, "16550 or compatible");
945 	} else if (count >= 28 && count <= 32) {
946 		sc->sc_rxfifosz = 32;
947 		device_set_desc(sc->sc_dev, "16650 or compatible");
948 	} else if (count >= 56 && count <= 64) {
949 		sc->sc_rxfifosz = 64;
950 		device_set_desc(sc->sc_dev, "16750 or compatible");
951 	} else if (count >= 112 && count <= 128) {
952 		sc->sc_rxfifosz = 128;
953 		device_set_desc(sc->sc_dev, "16950 or compatible");
954 	} else if (count >= 224 && count <= 256) {
955 		sc->sc_rxfifosz = 256;
956 		device_set_desc(sc->sc_dev, "16x50 with 256 byte FIFO");
957 	} else {
958 		sc->sc_rxfifosz = 16;
959 		device_set_desc(sc->sc_dev,
960 		    "Non-standard ns8250 class UART with FIFOs");
961 	}
962 
963 	/*
964 	 * Force the Tx FIFO size to 16 bytes for now. We don't program the
965 	 * Tx trigger. Also, we assume that all data has been sent when the
966 	 * interrupt happens.
967 	 */
968 	sc->sc_txfifosz = 16;
969 
970 #if 0
971 	/*
972 	 * XXX there are some issues related to hardware flow control and
973 	 * it's likely that uart(4) is the cause. This basically needs more
974 	 * investigation, but we avoid using for hardware flow control
975 	 * until then.
976 	 */
977 	/* 16650s or higher have automatic flow control. */
978 	if (sc->sc_rxfifosz > 16) {
979 		sc->sc_hwiflow = 1;
980 		sc->sc_hwoflow = 1;
981 	}
982 #endif
983 
984 	return (0);
985 }
986 
987 int
988 ns8250_bus_receive(struct uart_softc *sc)
989 {
990 	struct uart_bas *bas;
991 	int xc;
992 	uint8_t lsr;
993 
994 	bas = &sc->sc_bas;
995 	uart_lock(sc->sc_hwmtx);
996 	lsr = uart_getreg(bas, REG_LSR);
997 	while (lsr & LSR_RXRDY) {
998 		if (uart_rx_full(sc)) {
999 			sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
1000 			break;
1001 		}
1002 		xc = uart_getreg(bas, REG_DATA);
1003 		if (lsr & LSR_FE)
1004 			xc |= UART_STAT_FRAMERR;
1005 		if (lsr & LSR_PE)
1006 			xc |= UART_STAT_PARERR;
1007 		uart_rx_put(sc, xc);
1008 		lsr = uart_getreg(bas, REG_LSR);
1009 	}
1010 	/* Discard everything left in the Rx FIFO. */
1011 	while (lsr & LSR_RXRDY) {
1012 		(void)uart_getreg(bas, REG_DATA);
1013 		uart_barrier(bas);
1014 		lsr = uart_getreg(bas, REG_LSR);
1015 	}
1016 	uart_unlock(sc->sc_hwmtx);
1017  	return (0);
1018 }
1019 
1020 int
1021 ns8250_bus_setsig(struct uart_softc *sc, int sig)
1022 {
1023 	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
1024 	struct uart_bas *bas;
1025 	uint32_t new, old;
1026 
1027 	bas = &sc->sc_bas;
1028 	do {
1029 		old = sc->sc_hwsig;
1030 		new = old;
1031 		if (sig & SER_DDTR) {
1032 			new = (new & ~SER_DTR) | (sig & (SER_DTR | SER_DDTR));
1033 		}
1034 		if (sig & SER_DRTS) {
1035 			new = (new & ~SER_RTS) | (sig & (SER_RTS | SER_DRTS));
1036 		}
1037 	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
1038 	uart_lock(sc->sc_hwmtx);
1039 	ns8250->mcr &= ~(MCR_DTR|MCR_RTS);
1040 	if (new & SER_DTR)
1041 		ns8250->mcr |= MCR_DTR;
1042 	if (new & SER_RTS)
1043 		ns8250->mcr |= MCR_RTS;
1044 	uart_setreg(bas, REG_MCR, ns8250->mcr);
1045 	uart_barrier(bas);
1046 	uart_unlock(sc->sc_hwmtx);
1047 	return (0);
1048 }
1049 
1050 int
1051 ns8250_bus_transmit(struct uart_softc *sc)
1052 {
1053 	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
1054 	struct uart_bas *bas;
1055 	int i;
1056 
1057 	bas = &sc->sc_bas;
1058 	uart_lock(sc->sc_hwmtx);
1059 	while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0)
1060 		DELAY(4);
1061 	for (i = 0; i < sc->sc_txdatasz; i++) {
1062 		uart_setreg(bas, REG_DATA, sc->sc_txbuf[i]);
1063 		uart_barrier(bas);
1064 	}
1065 	if (!broken_txfifo)
1066 		ns8250->ier |= IER_ETXRDY;
1067 	uart_setreg(bas, REG_IER, ns8250->ier);
1068 	uart_barrier(bas);
1069 	if (broken_txfifo)
1070 		ns8250_drain(bas, UART_DRAIN_TRANSMITTER);
1071 	else
1072 		sc->sc_txbusy = 1;
1073 	uart_unlock(sc->sc_hwmtx);
1074 	if (broken_txfifo)
1075 		uart_sched_softih(sc, SER_INT_TXIDLE);
1076 	return (0);
1077 }
1078 
1079 bool
1080 ns8250_bus_txbusy(struct uart_softc *sc)
1081 {
1082 	struct uart_bas *bas = &sc->sc_bas;
1083 
1084 	if ((uart_getreg(bas, REG_LSR) & (LSR_TEMT | LSR_THRE)) !=
1085 	    (LSR_TEMT | LSR_THRE))
1086 		return (true);
1087 	return (false);
1088 }
1089 
1090 void
1091 ns8250_bus_grab(struct uart_softc *sc)
1092 {
1093 	struct uart_bas *bas = &sc->sc_bas;
1094 	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
1095 	u_char ier;
1096 
1097 	/*
1098 	 * turn off all interrupts to enter polling mode. Leave the
1099 	 * saved mask alone. We'll restore whatever it was in ungrab.
1100 	 * All pending interrupt signals are reset when IER is set to 0.
1101 	 */
1102 	uart_lock(sc->sc_hwmtx);
1103 	ier = uart_getreg(bas, REG_IER);
1104 	uart_setreg(bas, REG_IER, ier & ns8250->ier_mask);
1105 	uart_barrier(bas);
1106 	uart_unlock(sc->sc_hwmtx);
1107 }
1108 
1109 void
1110 ns8250_bus_ungrab(struct uart_softc *sc)
1111 {
1112 	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
1113 	struct uart_bas *bas = &sc->sc_bas;
1114 
1115 	/*
1116 	 * Restore previous interrupt mask
1117 	 */
1118 	uart_lock(sc->sc_hwmtx);
1119 	uart_setreg(bas, REG_IER, ns8250->ier);
1120 	uart_barrier(bas);
1121 	uart_unlock(sc->sc_hwmtx);
1122 }
1123