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