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