1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Base port operations for 8250/16550-type serial ports 4 * 5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. 6 * Split from 8250_core.c, Copyright (C) 2001 Russell King. 7 * 8 * A note about mapbase / membase 9 * 10 * mapbase is the physical address of the IO port. 11 * membase is an 'ioremapped' cookie. 12 */ 13 14 #include <linux/module.h> 15 #include <linux/moduleparam.h> 16 #include <linux/ioport.h> 17 #include <linux/init.h> 18 #include <linux/irq.h> 19 #include <linux/console.h> 20 #include <linux/gpio/consumer.h> 21 #include <linux/sysrq.h> 22 #include <linux/delay.h> 23 #include <linux/platform_device.h> 24 #include <linux/tty.h> 25 #include <linux/ratelimit.h> 26 #include <linux/tty_flip.h> 27 #include <linux/serial.h> 28 #include <linux/serial_8250.h> 29 #include <linux/nmi.h> 30 #include <linux/mutex.h> 31 #include <linux/slab.h> 32 #include <linux/uaccess.h> 33 #include <linux/pm_runtime.h> 34 #include <linux/ktime.h> 35 36 #include <asm/io.h> 37 #include <asm/irq.h> 38 39 #include "8250.h" 40 41 /* 42 * Debugging. 43 */ 44 #if 0 45 #define DEBUG_AUTOCONF(fmt...) printk(fmt) 46 #else 47 #define DEBUG_AUTOCONF(fmt...) do { } while (0) 48 #endif 49 50 /* 51 * Here we define the default xmit fifo size used for each type of UART. 52 */ 53 static const struct serial8250_config uart_config[] = { 54 [PORT_UNKNOWN] = { 55 .name = "unknown", 56 .fifo_size = 1, 57 .tx_loadsz = 1, 58 }, 59 [PORT_8250] = { 60 .name = "8250", 61 .fifo_size = 1, 62 .tx_loadsz = 1, 63 }, 64 [PORT_16450] = { 65 .name = "16450", 66 .fifo_size = 1, 67 .tx_loadsz = 1, 68 }, 69 [PORT_16550] = { 70 .name = "16550", 71 .fifo_size = 1, 72 .tx_loadsz = 1, 73 }, 74 [PORT_16550A] = { 75 .name = "16550A", 76 .fifo_size = 16, 77 .tx_loadsz = 16, 78 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 79 .rxtrig_bytes = {1, 4, 8, 14}, 80 .flags = UART_CAP_FIFO, 81 }, 82 [PORT_CIRRUS] = { 83 .name = "Cirrus", 84 .fifo_size = 1, 85 .tx_loadsz = 1, 86 }, 87 [PORT_16650] = { 88 .name = "ST16650", 89 .fifo_size = 1, 90 .tx_loadsz = 1, 91 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, 92 }, 93 [PORT_16650V2] = { 94 .name = "ST16650V2", 95 .fifo_size = 32, 96 .tx_loadsz = 16, 97 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | 98 UART_FCR_T_TRIG_00, 99 .rxtrig_bytes = {8, 16, 24, 28}, 100 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, 101 }, 102 [PORT_16750] = { 103 .name = "TI16750", 104 .fifo_size = 64, 105 .tx_loadsz = 64, 106 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 | 107 UART_FCR7_64BYTE, 108 .rxtrig_bytes = {1, 16, 32, 56}, 109 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE, 110 }, 111 [PORT_STARTECH] = { 112 .name = "Startech", 113 .fifo_size = 1, 114 .tx_loadsz = 1, 115 }, 116 [PORT_16C950] = { 117 .name = "16C950/954", 118 .fifo_size = 128, 119 .tx_loadsz = 128, 120 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01, 121 .rxtrig_bytes = {16, 32, 112, 120}, 122 /* UART_CAP_EFR breaks billionon CF bluetooth card. */ 123 .flags = UART_CAP_FIFO | UART_CAP_SLEEP, 124 }, 125 [PORT_16654] = { 126 .name = "ST16654", 127 .fifo_size = 64, 128 .tx_loadsz = 32, 129 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | 130 UART_FCR_T_TRIG_10, 131 .rxtrig_bytes = {8, 16, 56, 60}, 132 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, 133 }, 134 [PORT_16850] = { 135 .name = "XR16850", 136 .fifo_size = 128, 137 .tx_loadsz = 128, 138 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 139 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, 140 }, 141 [PORT_RSA] = { 142 .name = "RSA", 143 .fifo_size = 2048, 144 .tx_loadsz = 2048, 145 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11, 146 .flags = UART_CAP_FIFO, 147 }, 148 [PORT_NS16550A] = { 149 .name = "NS16550A", 150 .fifo_size = 16, 151 .tx_loadsz = 16, 152 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 153 .flags = UART_CAP_FIFO | UART_NATSEMI, 154 }, 155 [PORT_XSCALE] = { 156 .name = "XScale", 157 .fifo_size = 32, 158 .tx_loadsz = 32, 159 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 160 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE, 161 }, 162 [PORT_OCTEON] = { 163 .name = "OCTEON", 164 .fifo_size = 64, 165 .tx_loadsz = 64, 166 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 167 .flags = UART_CAP_FIFO, 168 }, 169 [PORT_U6_16550A] = { 170 .name = "U6_16550A", 171 .fifo_size = 64, 172 .tx_loadsz = 64, 173 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 174 .flags = UART_CAP_FIFO | UART_CAP_AFE, 175 }, 176 [PORT_TEGRA] = { 177 .name = "Tegra", 178 .fifo_size = 32, 179 .tx_loadsz = 8, 180 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | 181 UART_FCR_T_TRIG_01, 182 .rxtrig_bytes = {1, 4, 8, 14}, 183 .flags = UART_CAP_FIFO | UART_CAP_RTOIE, 184 }, 185 [PORT_XR17D15X] = { 186 .name = "XR17D15X", 187 .fifo_size = 64, 188 .tx_loadsz = 64, 189 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 190 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR | 191 UART_CAP_SLEEP, 192 }, 193 [PORT_XR17V35X] = { 194 .name = "XR17V35X", 195 .fifo_size = 256, 196 .tx_loadsz = 256, 197 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 | 198 UART_FCR_T_TRIG_11, 199 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR | 200 UART_CAP_SLEEP, 201 }, 202 [PORT_LPC3220] = { 203 .name = "LPC3220", 204 .fifo_size = 64, 205 .tx_loadsz = 32, 206 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO | 207 UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00, 208 .flags = UART_CAP_FIFO, 209 }, 210 [PORT_BRCM_TRUMANAGE] = { 211 .name = "TruManage", 212 .fifo_size = 1, 213 .tx_loadsz = 1024, 214 .flags = UART_CAP_HFIFO, 215 }, 216 [PORT_8250_CIR] = { 217 .name = "CIR port" 218 }, 219 [PORT_ALTR_16550_F32] = { 220 .name = "Altera 16550 FIFO32", 221 .fifo_size = 32, 222 .tx_loadsz = 32, 223 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 224 .rxtrig_bytes = {1, 8, 16, 30}, 225 .flags = UART_CAP_FIFO | UART_CAP_AFE, 226 }, 227 [PORT_ALTR_16550_F64] = { 228 .name = "Altera 16550 FIFO64", 229 .fifo_size = 64, 230 .tx_loadsz = 64, 231 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 232 .rxtrig_bytes = {1, 16, 32, 62}, 233 .flags = UART_CAP_FIFO | UART_CAP_AFE, 234 }, 235 [PORT_ALTR_16550_F128] = { 236 .name = "Altera 16550 FIFO128", 237 .fifo_size = 128, 238 .tx_loadsz = 128, 239 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 240 .rxtrig_bytes = {1, 32, 64, 126}, 241 .flags = UART_CAP_FIFO | UART_CAP_AFE, 242 }, 243 /* 244 * tx_loadsz is set to 63-bytes instead of 64-bytes to implement 245 * workaround of errata A-008006 which states that tx_loadsz should 246 * be configured less than Maximum supported fifo bytes. 247 */ 248 [PORT_16550A_FSL64] = { 249 .name = "16550A_FSL64", 250 .fifo_size = 64, 251 .tx_loadsz = 63, 252 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 | 253 UART_FCR7_64BYTE, 254 .flags = UART_CAP_FIFO | UART_CAP_NOTEMT, 255 }, 256 [PORT_RT2880] = { 257 .name = "Palmchip BK-3103", 258 .fifo_size = 16, 259 .tx_loadsz = 16, 260 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 261 .rxtrig_bytes = {1, 4, 8, 14}, 262 .flags = UART_CAP_FIFO, 263 }, 264 [PORT_DA830] = { 265 .name = "TI DA8xx/66AK2x", 266 .fifo_size = 16, 267 .tx_loadsz = 16, 268 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO | 269 UART_FCR_R_TRIG_10, 270 .rxtrig_bytes = {1, 4, 8, 14}, 271 .flags = UART_CAP_FIFO | UART_CAP_AFE, 272 }, 273 [PORT_MTK_BTIF] = { 274 .name = "MediaTek BTIF", 275 .fifo_size = 16, 276 .tx_loadsz = 16, 277 .fcr = UART_FCR_ENABLE_FIFO | 278 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT, 279 .flags = UART_CAP_FIFO, 280 }, 281 [PORT_NPCM] = { 282 .name = "Nuvoton 16550", 283 .fifo_size = 16, 284 .tx_loadsz = 16, 285 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 | 286 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT, 287 .rxtrig_bytes = {1, 4, 8, 14}, 288 .flags = UART_CAP_FIFO, 289 }, 290 [PORT_SUNIX] = { 291 .name = "Sunix", 292 .fifo_size = 128, 293 .tx_loadsz = 128, 294 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 295 .rxtrig_bytes = {1, 32, 64, 112}, 296 .flags = UART_CAP_FIFO | UART_CAP_SLEEP, 297 }, 298 [PORT_ASPEED_VUART] = { 299 .name = "ASPEED VUART", 300 .fifo_size = 16, 301 .tx_loadsz = 16, 302 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00, 303 .rxtrig_bytes = {1, 4, 8, 14}, 304 .flags = UART_CAP_FIFO, 305 }, 306 [PORT_MCHP16550A] = { 307 .name = "MCHP16550A", 308 .fifo_size = 256, 309 .tx_loadsz = 256, 310 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01, 311 .rxtrig_bytes = {2, 66, 130, 194}, 312 .flags = UART_CAP_FIFO, 313 }, 314 [PORT_BCM7271] = { 315 .name = "Broadcom BCM7271 UART", 316 .fifo_size = 32, 317 .tx_loadsz = 32, 318 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01, 319 .rxtrig_bytes = {1, 8, 16, 30}, 320 .flags = UART_CAP_FIFO | UART_CAP_AFE, 321 }, 322 }; 323 324 /* Uart divisor latch read */ 325 static u32 default_serial_dl_read(struct uart_8250_port *up) 326 { 327 /* Assign these in pieces to truncate any bits above 7. */ 328 unsigned char dll = serial_in(up, UART_DLL); 329 unsigned char dlm = serial_in(up, UART_DLM); 330 331 return dll | dlm << 8; 332 } 333 334 /* Uart divisor latch write */ 335 static void default_serial_dl_write(struct uart_8250_port *up, u32 value) 336 { 337 serial_out(up, UART_DLL, value & 0xff); 338 serial_out(up, UART_DLM, value >> 8 & 0xff); 339 } 340 341 #ifdef CONFIG_HAS_IOPORT 342 static unsigned int hub6_serial_in(struct uart_port *p, int offset) 343 { 344 offset = offset << p->regshift; 345 outb(p->hub6 - 1 + offset, p->iobase); 346 return inb(p->iobase + 1); 347 } 348 349 static void hub6_serial_out(struct uart_port *p, int offset, int value) 350 { 351 offset = offset << p->regshift; 352 outb(p->hub6 - 1 + offset, p->iobase); 353 outb(value, p->iobase + 1); 354 } 355 #endif /* CONFIG_HAS_IOPORT */ 356 357 static unsigned int mem_serial_in(struct uart_port *p, int offset) 358 { 359 offset = offset << p->regshift; 360 return readb(p->membase + offset); 361 } 362 363 static void mem_serial_out(struct uart_port *p, int offset, int value) 364 { 365 offset = offset << p->regshift; 366 writeb(value, p->membase + offset); 367 } 368 369 static void mem16_serial_out(struct uart_port *p, int offset, int value) 370 { 371 offset = offset << p->regshift; 372 writew(value, p->membase + offset); 373 } 374 375 static unsigned int mem16_serial_in(struct uart_port *p, int offset) 376 { 377 offset = offset << p->regshift; 378 return readw(p->membase + offset); 379 } 380 381 static void mem32_serial_out(struct uart_port *p, int offset, int value) 382 { 383 offset = offset << p->regshift; 384 writel(value, p->membase + offset); 385 } 386 387 static unsigned int mem32_serial_in(struct uart_port *p, int offset) 388 { 389 offset = offset << p->regshift; 390 return readl(p->membase + offset); 391 } 392 393 static void mem32be_serial_out(struct uart_port *p, int offset, int value) 394 { 395 offset = offset << p->regshift; 396 iowrite32be(value, p->membase + offset); 397 } 398 399 static unsigned int mem32be_serial_in(struct uart_port *p, int offset) 400 { 401 offset = offset << p->regshift; 402 return ioread32be(p->membase + offset); 403 } 404 405 #ifdef CONFIG_HAS_IOPORT 406 static unsigned int io_serial_in(struct uart_port *p, int offset) 407 { 408 offset = offset << p->regshift; 409 return inb(p->iobase + offset); 410 } 411 412 static void io_serial_out(struct uart_port *p, int offset, int value) 413 { 414 offset = offset << p->regshift; 415 outb(value, p->iobase + offset); 416 } 417 #endif 418 static unsigned int no_serial_in(struct uart_port *p, int offset) 419 { 420 return (unsigned int)-1; 421 } 422 423 static void no_serial_out(struct uart_port *p, int offset, int value) 424 { 425 } 426 427 static int serial8250_default_handle_irq(struct uart_port *port); 428 429 static void set_io_from_upio(struct uart_port *p) 430 { 431 struct uart_8250_port *up = up_to_u8250p(p); 432 433 up->dl_read = default_serial_dl_read; 434 up->dl_write = default_serial_dl_write; 435 436 switch (p->iotype) { 437 #ifdef CONFIG_HAS_IOPORT 438 case UPIO_HUB6: 439 p->serial_in = hub6_serial_in; 440 p->serial_out = hub6_serial_out; 441 break; 442 #endif 443 444 case UPIO_MEM: 445 p->serial_in = mem_serial_in; 446 p->serial_out = mem_serial_out; 447 break; 448 449 case UPIO_MEM16: 450 p->serial_in = mem16_serial_in; 451 p->serial_out = mem16_serial_out; 452 break; 453 454 case UPIO_MEM32: 455 p->serial_in = mem32_serial_in; 456 p->serial_out = mem32_serial_out; 457 break; 458 459 case UPIO_MEM32BE: 460 p->serial_in = mem32be_serial_in; 461 p->serial_out = mem32be_serial_out; 462 break; 463 #ifdef CONFIG_HAS_IOPORT 464 case UPIO_PORT: 465 p->serial_in = io_serial_in; 466 p->serial_out = io_serial_out; 467 break; 468 #endif 469 default: 470 WARN(p->iotype != UPIO_PORT || p->iobase, 471 "Unsupported UART type %x\n", p->iotype); 472 p->serial_in = no_serial_in; 473 p->serial_out = no_serial_out; 474 } 475 /* Remember loaded iotype */ 476 up->cur_iotype = p->iotype; 477 p->handle_irq = serial8250_default_handle_irq; 478 } 479 480 static void 481 serial_port_out_sync(struct uart_port *p, int offset, int value) 482 { 483 switch (p->iotype) { 484 case UPIO_MEM: 485 case UPIO_MEM16: 486 case UPIO_MEM32: 487 case UPIO_MEM32BE: 488 case UPIO_AU: 489 p->serial_out(p, offset, value); 490 p->serial_in(p, UART_LCR); /* safe, no side-effects */ 491 break; 492 default: 493 p->serial_out(p, offset, value); 494 } 495 } 496 497 /* 498 * FIFO support. 499 */ 500 static void serial8250_clear_fifos(struct uart_8250_port *p) 501 { 502 if (p->capabilities & UART_CAP_FIFO) { 503 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO); 504 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO | 505 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT); 506 serial_out(p, UART_FCR, 0); 507 } 508 } 509 510 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t); 511 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t); 512 513 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p) 514 { 515 serial8250_clear_fifos(p); 516 serial_out(p, UART_FCR, p->fcr); 517 } 518 EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos); 519 520 void serial8250_rpm_get(struct uart_8250_port *p) 521 { 522 if (!(p->capabilities & UART_CAP_RPM)) 523 return; 524 pm_runtime_get_sync(p->port.dev); 525 } 526 EXPORT_SYMBOL_GPL(serial8250_rpm_get); 527 528 void serial8250_rpm_put(struct uart_8250_port *p) 529 { 530 if (!(p->capabilities & UART_CAP_RPM)) 531 return; 532 pm_runtime_mark_last_busy(p->port.dev); 533 pm_runtime_put_autosuspend(p->port.dev); 534 } 535 EXPORT_SYMBOL_GPL(serial8250_rpm_put); 536 537 /** 538 * serial8250_em485_init() - put uart_8250_port into rs485 emulating 539 * @p: uart_8250_port port instance 540 * 541 * The function is used to start rs485 software emulating on the 542 * &struct uart_8250_port* @p. Namely, RTS is switched before/after 543 * transmission. The function is idempotent, so it is safe to call it 544 * multiple times. 545 * 546 * The caller MUST enable interrupt on empty shift register before 547 * calling serial8250_em485_init(). This interrupt is not a part of 548 * 8250 standard, but implementation defined. 549 * 550 * The function is supposed to be called from .rs485_config callback 551 * or from any other callback protected with p->port.lock spinlock. 552 * 553 * See also serial8250_em485_destroy() 554 * 555 * Return 0 - success, -errno - otherwise 556 */ 557 static int serial8250_em485_init(struct uart_8250_port *p) 558 { 559 /* Port locked to synchronize UART_IER access against the console. */ 560 lockdep_assert_held_once(&p->port.lock); 561 562 if (p->em485) 563 goto deassert_rts; 564 565 p->em485 = kmalloc(sizeof(struct uart_8250_em485), GFP_ATOMIC); 566 if (!p->em485) 567 return -ENOMEM; 568 569 hrtimer_setup(&p->em485->stop_tx_timer, &serial8250_em485_handle_stop_tx, CLOCK_MONOTONIC, 570 HRTIMER_MODE_REL); 571 hrtimer_setup(&p->em485->start_tx_timer, &serial8250_em485_handle_start_tx, CLOCK_MONOTONIC, 572 HRTIMER_MODE_REL); 573 p->em485->port = p; 574 p->em485->active_timer = NULL; 575 p->em485->tx_stopped = true; 576 577 deassert_rts: 578 if (p->em485->tx_stopped) 579 p->rs485_stop_tx(p, true); 580 581 return 0; 582 } 583 584 /** 585 * serial8250_em485_destroy() - put uart_8250_port into normal state 586 * @p: uart_8250_port port instance 587 * 588 * The function is used to stop rs485 software emulating on the 589 * &struct uart_8250_port* @p. The function is idempotent, so it is safe to 590 * call it multiple times. 591 * 592 * The function is supposed to be called from .rs485_config callback 593 * or from any other callback protected with p->port.lock spinlock. 594 * 595 * See also serial8250_em485_init() 596 */ 597 void serial8250_em485_destroy(struct uart_8250_port *p) 598 { 599 if (!p->em485) 600 return; 601 602 hrtimer_cancel(&p->em485->start_tx_timer); 603 hrtimer_cancel(&p->em485->stop_tx_timer); 604 605 kfree(p->em485); 606 p->em485 = NULL; 607 } 608 EXPORT_SYMBOL_GPL(serial8250_em485_destroy); 609 610 struct serial_rs485 serial8250_em485_supported = { 611 .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND | 612 SER_RS485_TERMINATE_BUS | SER_RS485_RX_DURING_TX, 613 .delay_rts_before_send = 1, 614 .delay_rts_after_send = 1, 615 }; 616 EXPORT_SYMBOL_GPL(serial8250_em485_supported); 617 618 /** 619 * serial8250_em485_config() - generic ->rs485_config() callback 620 * @port: uart port 621 * @termios: termios structure 622 * @rs485: rs485 settings 623 * 624 * Generic callback usable by 8250 uart drivers to activate rs485 settings 625 * if the uart is incapable of driving RTS as a Transmit Enable signal in 626 * hardware, relying on software emulation instead. 627 */ 628 int serial8250_em485_config(struct uart_port *port, struct ktermios *termios, 629 struct serial_rs485 *rs485) 630 { 631 struct uart_8250_port *up = up_to_u8250p(port); 632 633 /* 634 * Both serial8250_em485_init() and serial8250_em485_destroy() 635 * are idempotent. 636 */ 637 if (rs485->flags & SER_RS485_ENABLED) 638 return serial8250_em485_init(up); 639 640 serial8250_em485_destroy(up); 641 return 0; 642 } 643 EXPORT_SYMBOL_GPL(serial8250_em485_config); 644 645 /* 646 * These two wrappers ensure that enable_runtime_pm_tx() can be called more than 647 * once and disable_runtime_pm_tx() will still disable RPM because the fifo is 648 * empty and the HW can idle again. 649 */ 650 void serial8250_rpm_get_tx(struct uart_8250_port *p) 651 { 652 unsigned char rpm_active; 653 654 if (!(p->capabilities & UART_CAP_RPM)) 655 return; 656 657 rpm_active = xchg(&p->rpm_tx_active, 1); 658 if (rpm_active) 659 return; 660 pm_runtime_get_sync(p->port.dev); 661 } 662 EXPORT_SYMBOL_GPL(serial8250_rpm_get_tx); 663 664 void serial8250_rpm_put_tx(struct uart_8250_port *p) 665 { 666 unsigned char rpm_active; 667 668 if (!(p->capabilities & UART_CAP_RPM)) 669 return; 670 671 rpm_active = xchg(&p->rpm_tx_active, 0); 672 if (!rpm_active) 673 return; 674 pm_runtime_mark_last_busy(p->port.dev); 675 pm_runtime_put_autosuspend(p->port.dev); 676 } 677 EXPORT_SYMBOL_GPL(serial8250_rpm_put_tx); 678 679 /* 680 * IER sleep support. UARTs which have EFRs need the "extended 681 * capability" bit enabled. Note that on XR16C850s, we need to 682 * reset LCR to write to IER. 683 */ 684 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep) 685 { 686 unsigned char lcr = 0, efr = 0; 687 688 serial8250_rpm_get(p); 689 690 if (p->capabilities & UART_CAP_SLEEP) { 691 /* Synchronize UART_IER access against the console. */ 692 uart_port_lock_irq(&p->port); 693 if (p->capabilities & UART_CAP_EFR) { 694 lcr = serial_in(p, UART_LCR); 695 efr = serial_in(p, UART_EFR); 696 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B); 697 serial_out(p, UART_EFR, UART_EFR_ECB); 698 serial_out(p, UART_LCR, 0); 699 } 700 serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0); 701 if (p->capabilities & UART_CAP_EFR) { 702 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B); 703 serial_out(p, UART_EFR, efr); 704 serial_out(p, UART_LCR, lcr); 705 } 706 uart_port_unlock_irq(&p->port); 707 } 708 709 serial8250_rpm_put(p); 710 } 711 712 static void serial8250_clear_IER(struct uart_8250_port *up) 713 { 714 if (up->capabilities & UART_CAP_UUE) 715 serial_out(up, UART_IER, UART_IER_UUE); 716 else 717 serial_out(up, UART_IER, 0); 718 } 719 720 #ifdef CONFIG_SERIAL_8250_RSA 721 /* 722 * Attempts to turn on the RSA FIFO. Returns zero on failure. 723 * We set the port uart clock rate if we succeed. 724 */ 725 static int __enable_rsa(struct uart_8250_port *up) 726 { 727 unsigned char mode; 728 int result; 729 730 mode = serial_in(up, UART_RSA_MSR); 731 result = mode & UART_RSA_MSR_FIFO; 732 733 if (!result) { 734 serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO); 735 mode = serial_in(up, UART_RSA_MSR); 736 result = mode & UART_RSA_MSR_FIFO; 737 } 738 739 if (result) 740 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16; 741 742 return result; 743 } 744 745 static void enable_rsa(struct uart_8250_port *up) 746 { 747 if (up->port.type == PORT_RSA) { 748 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) { 749 uart_port_lock_irq(&up->port); 750 __enable_rsa(up); 751 uart_port_unlock_irq(&up->port); 752 } 753 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) 754 serial_out(up, UART_RSA_FRR, 0); 755 } 756 } 757 758 /* 759 * Attempts to turn off the RSA FIFO. Returns zero on failure. 760 * It is unknown why interrupts were disabled in here. However, 761 * the caller is expected to preserve this behaviour by grabbing 762 * the spinlock before calling this function. 763 */ 764 static void disable_rsa(struct uart_8250_port *up) 765 { 766 unsigned char mode; 767 int result; 768 769 if (up->port.type == PORT_RSA && 770 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) { 771 uart_port_lock_irq(&up->port); 772 773 mode = serial_in(up, UART_RSA_MSR); 774 result = !(mode & UART_RSA_MSR_FIFO); 775 776 if (!result) { 777 serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO); 778 mode = serial_in(up, UART_RSA_MSR); 779 result = !(mode & UART_RSA_MSR_FIFO); 780 } 781 782 if (result) 783 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16; 784 uart_port_unlock_irq(&up->port); 785 } 786 } 787 #endif /* CONFIG_SERIAL_8250_RSA */ 788 789 /* 790 * This is a quickie test to see how big the FIFO is. 791 * It doesn't work at all the time, more's the pity. 792 */ 793 static int size_fifo(struct uart_8250_port *up) 794 { 795 unsigned char old_fcr, old_mcr, old_lcr; 796 u32 old_dl; 797 int count; 798 799 old_lcr = serial_in(up, UART_LCR); 800 serial_out(up, UART_LCR, 0); 801 old_fcr = serial_in(up, UART_FCR); 802 old_mcr = serial8250_in_MCR(up); 803 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | 804 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT); 805 serial8250_out_MCR(up, UART_MCR_LOOP); 806 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A); 807 old_dl = serial_dl_read(up); 808 serial_dl_write(up, 0x0001); 809 serial_out(up, UART_LCR, UART_LCR_WLEN8); 810 for (count = 0; count < 256; count++) 811 serial_out(up, UART_TX, count); 812 mdelay(20);/* FIXME - schedule_timeout */ 813 for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) && 814 (count < 256); count++) 815 serial_in(up, UART_RX); 816 serial_out(up, UART_FCR, old_fcr); 817 serial8250_out_MCR(up, old_mcr); 818 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A); 819 serial_dl_write(up, old_dl); 820 serial_out(up, UART_LCR, old_lcr); 821 822 return count; 823 } 824 825 /* 826 * Read UART ID using the divisor method - set DLL and DLM to zero 827 * and the revision will be in DLL and device type in DLM. We 828 * preserve the device state across this. 829 */ 830 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p) 831 { 832 unsigned char old_lcr; 833 unsigned int id, old_dl; 834 835 old_lcr = serial_in(p, UART_LCR); 836 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A); 837 old_dl = serial_dl_read(p); 838 serial_dl_write(p, 0); 839 id = serial_dl_read(p); 840 serial_dl_write(p, old_dl); 841 842 serial_out(p, UART_LCR, old_lcr); 843 844 return id; 845 } 846 847 /* 848 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's. 849 * When this function is called we know it is at least a StarTech 850 * 16650 V2, but it might be one of several StarTech UARTs, or one of 851 * its clones. (We treat the broken original StarTech 16650 V1 as a 852 * 16550, and why not? Startech doesn't seem to even acknowledge its 853 * existence.) 854 * 855 * What evil have men's minds wrought... 856 */ 857 static void autoconfig_has_efr(struct uart_8250_port *up) 858 { 859 unsigned int id1, id2, id3, rev; 860 861 /* 862 * Everything with an EFR has SLEEP 863 */ 864 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP; 865 866 /* 867 * First we check to see if it's an Oxford Semiconductor UART. 868 * 869 * If we have to do this here because some non-National 870 * Semiconductor clone chips lock up if you try writing to the 871 * LSR register (which serial_icr_read does) 872 */ 873 874 /* 875 * Check for Oxford Semiconductor 16C950. 876 * 877 * EFR [4] must be set else this test fails. 878 * 879 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca) 880 * claims that it's needed for 952 dual UART's (which are not 881 * recommended for new designs). 882 */ 883 up->acr = 0; 884 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); 885 serial_out(up, UART_EFR, UART_EFR_ECB); 886 serial_out(up, UART_LCR, 0x00); 887 id1 = serial_icr_read(up, UART_ID1); 888 id2 = serial_icr_read(up, UART_ID2); 889 id3 = serial_icr_read(up, UART_ID3); 890 rev = serial_icr_read(up, UART_REV); 891 892 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev); 893 894 if (id1 == 0x16 && id2 == 0xC9 && 895 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) { 896 up->port.type = PORT_16C950; 897 898 /* 899 * Enable work around for the Oxford Semiconductor 952 rev B 900 * chip which causes it to seriously miscalculate baud rates 901 * when DLL is 0. 902 */ 903 if (id3 == 0x52 && rev == 0x01) 904 up->bugs |= UART_BUG_QUOT; 905 return; 906 } 907 908 /* 909 * We check for a XR16C850 by setting DLL and DLM to 0, and then 910 * reading back DLL and DLM. The chip type depends on the DLM 911 * value read back: 912 * 0x10 - XR16C850 and the DLL contains the chip revision. 913 * 0x12 - XR16C2850. 914 * 0x14 - XR16C854. 915 */ 916 id1 = autoconfig_read_divisor_id(up); 917 DEBUG_AUTOCONF("850id=%04x ", id1); 918 919 id2 = id1 >> 8; 920 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) { 921 up->port.type = PORT_16850; 922 return; 923 } 924 925 /* 926 * It wasn't an XR16C850. 927 * 928 * We distinguish between the '654 and the '650 by counting 929 * how many bytes are in the FIFO. I'm using this for now, 930 * since that's the technique that was sent to me in the 931 * serial driver update, but I'm not convinced this works. 932 * I've had problems doing this in the past. -TYT 933 */ 934 if (size_fifo(up) == 64) 935 up->port.type = PORT_16654; 936 else 937 up->port.type = PORT_16650V2; 938 } 939 940 /* 941 * We detected a chip without a FIFO. Only two fall into 942 * this category - the original 8250 and the 16450. The 943 * 16450 has a scratch register (accessible with LCR=0) 944 */ 945 static void autoconfig_8250(struct uart_8250_port *up) 946 { 947 unsigned char scratch, status1, status2; 948 949 up->port.type = PORT_8250; 950 951 scratch = serial_in(up, UART_SCR); 952 serial_out(up, UART_SCR, 0xa5); 953 status1 = serial_in(up, UART_SCR); 954 serial_out(up, UART_SCR, 0x5a); 955 status2 = serial_in(up, UART_SCR); 956 serial_out(up, UART_SCR, scratch); 957 958 if (status1 == 0xa5 && status2 == 0x5a) 959 up->port.type = PORT_16450; 960 } 961 962 static int broken_efr(struct uart_8250_port *up) 963 { 964 /* 965 * Exar ST16C2550 "A2" devices incorrectly detect as 966 * having an EFR, and report an ID of 0x0201. See 967 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html 968 */ 969 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16) 970 return 1; 971 972 return 0; 973 } 974 975 /* 976 * We know that the chip has FIFOs. Does it have an EFR? The 977 * EFR is located in the same register position as the IIR and 978 * we know the top two bits of the IIR are currently set. The 979 * EFR should contain zero. Try to read the EFR. 980 */ 981 static void autoconfig_16550a(struct uart_8250_port *up) 982 { 983 unsigned char status1, status2; 984 unsigned int iersave; 985 986 /* Port locked to synchronize UART_IER access against the console. */ 987 lockdep_assert_held_once(&up->port.lock); 988 989 up->port.type = PORT_16550A; 990 up->capabilities |= UART_CAP_FIFO; 991 992 if (!IS_ENABLED(CONFIG_SERIAL_8250_16550A_VARIANTS) && 993 !(up->port.flags & UPF_FULL_PROBE)) 994 return; 995 996 /* 997 * Check for presence of the EFR when DLAB is set. 998 * Only ST16C650V1 UARTs pass this test. 999 */ 1000 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A); 1001 if (serial_in(up, UART_EFR) == 0) { 1002 serial_out(up, UART_EFR, 0xA8); 1003 if (serial_in(up, UART_EFR) != 0) { 1004 DEBUG_AUTOCONF("EFRv1 "); 1005 up->port.type = PORT_16650; 1006 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP; 1007 } else { 1008 serial_out(up, UART_LCR, 0); 1009 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | 1010 UART_FCR7_64BYTE); 1011 status1 = serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED_16750; 1012 serial_out(up, UART_FCR, 0); 1013 serial_out(up, UART_LCR, 0); 1014 1015 if (status1 == UART_IIR_FIFO_ENABLED_16750) 1016 up->port.type = PORT_16550A_FSL64; 1017 else 1018 DEBUG_AUTOCONF("Motorola 8xxx DUART "); 1019 } 1020 serial_out(up, UART_EFR, 0); 1021 return; 1022 } 1023 1024 /* 1025 * Maybe it requires 0xbf to be written to the LCR. 1026 * (other ST16C650V2 UARTs, TI16C752A, etc) 1027 */ 1028 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); 1029 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) { 1030 DEBUG_AUTOCONF("EFRv2 "); 1031 autoconfig_has_efr(up); 1032 return; 1033 } 1034 1035 /* 1036 * Check for a National Semiconductor SuperIO chip. 1037 * Attempt to switch to bank 2, read the value of the LOOP bit 1038 * from EXCR1. Switch back to bank 0, change it in MCR. Then 1039 * switch back to bank 2, read it from EXCR1 again and check 1040 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2 1041 */ 1042 serial_out(up, UART_LCR, 0); 1043 status1 = serial8250_in_MCR(up); 1044 serial_out(up, UART_LCR, 0xE0); 1045 status2 = serial_in(up, 0x02); /* EXCR1 */ 1046 1047 if (!((status2 ^ status1) & UART_MCR_LOOP)) { 1048 serial_out(up, UART_LCR, 0); 1049 serial8250_out_MCR(up, status1 ^ UART_MCR_LOOP); 1050 serial_out(up, UART_LCR, 0xE0); 1051 status2 = serial_in(up, 0x02); /* EXCR1 */ 1052 serial_out(up, UART_LCR, 0); 1053 serial8250_out_MCR(up, status1); 1054 1055 if ((status2 ^ status1) & UART_MCR_LOOP) { 1056 unsigned short quot; 1057 1058 serial_out(up, UART_LCR, 0xE0); 1059 1060 quot = serial_dl_read(up); 1061 quot <<= 3; 1062 1063 if (ns16550a_goto_highspeed(up)) 1064 serial_dl_write(up, quot); 1065 1066 serial_out(up, UART_LCR, 0); 1067 1068 up->port.uartclk = 921600*16; 1069 up->port.type = PORT_NS16550A; 1070 up->capabilities |= UART_NATSEMI; 1071 return; 1072 } 1073 } 1074 1075 /* 1076 * No EFR. Try to detect a TI16750, which only sets bit 5 of 1077 * the IIR when 64 byte FIFO mode is enabled when DLAB is set. 1078 * Try setting it with and without DLAB set. Cheap clones 1079 * set bit 5 without DLAB set. 1080 */ 1081 serial_out(up, UART_LCR, 0); 1082 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE); 1083 status1 = serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED_16750; 1084 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO); 1085 1086 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A); 1087 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE); 1088 status2 = serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED_16750; 1089 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO); 1090 1091 serial_out(up, UART_LCR, 0); 1092 1093 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2); 1094 1095 if (status1 == UART_IIR_FIFO_ENABLED_16550A && 1096 status2 == UART_IIR_FIFO_ENABLED_16750) { 1097 up->port.type = PORT_16750; 1098 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP; 1099 return; 1100 } 1101 1102 /* 1103 * Try writing and reading the UART_IER_UUE bit (b6). 1104 * If it works, this is probably one of the Xscale platform's 1105 * internal UARTs. 1106 * We're going to explicitly set the UUE bit to 0 before 1107 * trying to write and read a 1 just to make sure it's not 1108 * already a 1 and maybe locked there before we even start. 1109 */ 1110 iersave = serial_in(up, UART_IER); 1111 serial_out(up, UART_IER, iersave & ~UART_IER_UUE); 1112 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) { 1113 /* 1114 * OK it's in a known zero state, try writing and reading 1115 * without disturbing the current state of the other bits. 1116 */ 1117 serial_out(up, UART_IER, iersave | UART_IER_UUE); 1118 if (serial_in(up, UART_IER) & UART_IER_UUE) { 1119 /* 1120 * It's an Xscale. 1121 * We'll leave the UART_IER_UUE bit set to 1 (enabled). 1122 */ 1123 DEBUG_AUTOCONF("Xscale "); 1124 up->port.type = PORT_XSCALE; 1125 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE; 1126 return; 1127 } 1128 } else { 1129 /* 1130 * If we got here we couldn't force the IER_UUE bit to 0. 1131 * Log it and continue. 1132 */ 1133 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 "); 1134 } 1135 serial_out(up, UART_IER, iersave); 1136 1137 /* 1138 * We distinguish between 16550A and U6 16550A by counting 1139 * how many bytes are in the FIFO. 1140 */ 1141 if (up->port.type == PORT_16550A && size_fifo(up) == 64) { 1142 up->port.type = PORT_U6_16550A; 1143 up->capabilities |= UART_CAP_AFE; 1144 } 1145 } 1146 1147 /* 1148 * This routine is called by rs_init() to initialize a specific serial 1149 * port. It determines what type of UART chip this serial port is 1150 * using: 8250, 16450, 16550, 16550A. The important question is 1151 * whether or not this UART is a 16550A or not, since this will 1152 * determine whether or not we can use its FIFO features or not. 1153 */ 1154 static void autoconfig(struct uart_8250_port *up) 1155 { 1156 unsigned char status1, scratch, scratch2, scratch3; 1157 unsigned char save_lcr, save_mcr; 1158 struct uart_port *port = &up->port; 1159 unsigned long flags; 1160 unsigned int old_capabilities; 1161 1162 if (!port->iobase && !port->mapbase && !port->membase) 1163 return; 1164 1165 DEBUG_AUTOCONF("%s: autoconf (0x%04lx, 0x%p): ", 1166 port->name, port->iobase, port->membase); 1167 1168 /* 1169 * We really do need global IRQs disabled here - we're going to 1170 * be frobbing the chips IRQ enable register to see if it exists. 1171 * 1172 * Synchronize UART_IER access against the console. 1173 */ 1174 uart_port_lock_irqsave(port, &flags); 1175 1176 up->capabilities = 0; 1177 up->bugs = 0; 1178 1179 if (!(port->flags & UPF_BUGGY_UART)) { 1180 /* 1181 * Do a simple existence test first; if we fail this, 1182 * there's no point trying anything else. 1183 * 1184 * 0x80 is used as a nonsense port to prevent against 1185 * false positives due to ISA bus float. The 1186 * assumption is that 0x80 is a non-existent port; 1187 * which should be safe since include/asm/io.h also 1188 * makes this assumption. 1189 * 1190 * Note: this is safe as long as MCR bit 4 is clear 1191 * and the device is in "PC" mode. 1192 */ 1193 scratch = serial_in(up, UART_IER); 1194 serial_out(up, UART_IER, 0); 1195 #if defined(__i386__) && defined(CONFIG_HAS_IOPORT) 1196 outb(0xff, 0x080); 1197 #endif 1198 /* 1199 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL 1200 * 16C754B) allow only to modify them if an EFR bit is set. 1201 */ 1202 scratch2 = serial_in(up, UART_IER) & UART_IER_ALL_INTR; 1203 serial_out(up, UART_IER, UART_IER_ALL_INTR); 1204 #if defined(__i386__) && defined(CONFIG_HAS_IOPORT) 1205 outb(0, 0x080); 1206 #endif 1207 scratch3 = serial_in(up, UART_IER) & UART_IER_ALL_INTR; 1208 serial_out(up, UART_IER, scratch); 1209 if (scratch2 != 0 || scratch3 != UART_IER_ALL_INTR) { 1210 /* 1211 * We failed; there's nothing here 1212 */ 1213 uart_port_unlock_irqrestore(port, flags); 1214 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ", 1215 scratch2, scratch3); 1216 goto out; 1217 } 1218 } 1219 1220 save_mcr = serial8250_in_MCR(up); 1221 save_lcr = serial_in(up, UART_LCR); 1222 1223 /* 1224 * Check to see if a UART is really there. Certain broken 1225 * internal modems based on the Rockwell chipset fail this 1226 * test, because they apparently don't implement the loopback 1227 * test mode. So this test is skipped on the COM 1 through 1228 * COM 4 ports. This *should* be safe, since no board 1229 * manufacturer would be stupid enough to design a board 1230 * that conflicts with COM 1-4 --- we hope! 1231 */ 1232 if (!(port->flags & UPF_SKIP_TEST)) { 1233 serial8250_out_MCR(up, UART_MCR_LOOP | UART_MCR_OUT2 | UART_MCR_RTS); 1234 status1 = serial_in(up, UART_MSR) & UART_MSR_STATUS_BITS; 1235 serial8250_out_MCR(up, save_mcr); 1236 if (status1 != (UART_MSR_DCD | UART_MSR_CTS)) { 1237 uart_port_unlock_irqrestore(port, flags); 1238 DEBUG_AUTOCONF("LOOP test failed (%02x) ", 1239 status1); 1240 goto out; 1241 } 1242 } 1243 1244 /* 1245 * We're pretty sure there's a port here. Lets find out what 1246 * type of port it is. The IIR top two bits allows us to find 1247 * out if it's 8250 or 16450, 16550, 16550A or later. This 1248 * determines what we test for next. 1249 * 1250 * We also initialise the EFR (if any) to zero for later. The 1251 * EFR occupies the same register location as the FCR and IIR. 1252 */ 1253 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); 1254 serial_out(up, UART_EFR, 0); 1255 serial_out(up, UART_LCR, 0); 1256 1257 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO); 1258 1259 switch (serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED) { 1260 case UART_IIR_FIFO_ENABLED_8250: 1261 autoconfig_8250(up); 1262 break; 1263 case UART_IIR_FIFO_ENABLED_16550: 1264 port->type = PORT_16550; 1265 break; 1266 case UART_IIR_FIFO_ENABLED_16550A: 1267 autoconfig_16550a(up); 1268 break; 1269 default: 1270 port->type = PORT_UNKNOWN; 1271 break; 1272 } 1273 1274 #ifdef CONFIG_SERIAL_8250_RSA 1275 /* 1276 * Only probe for RSA ports if we got the region. 1277 */ 1278 if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA && 1279 __enable_rsa(up)) 1280 port->type = PORT_RSA; 1281 #endif 1282 1283 serial_out(up, UART_LCR, save_lcr); 1284 1285 port->fifosize = uart_config[up->port.type].fifo_size; 1286 old_capabilities = up->capabilities; 1287 up->capabilities = uart_config[port->type].flags; 1288 up->tx_loadsz = uart_config[port->type].tx_loadsz; 1289 1290 if (port->type == PORT_UNKNOWN) 1291 goto out_unlock; 1292 1293 /* 1294 * Reset the UART. 1295 */ 1296 #ifdef CONFIG_SERIAL_8250_RSA 1297 if (port->type == PORT_RSA) 1298 serial_out(up, UART_RSA_FRR, 0); 1299 #endif 1300 serial8250_out_MCR(up, save_mcr); 1301 serial8250_clear_fifos(up); 1302 serial_in(up, UART_RX); 1303 serial8250_clear_IER(up); 1304 1305 out_unlock: 1306 uart_port_unlock_irqrestore(port, flags); 1307 1308 /* 1309 * Check if the device is a Fintek F81216A 1310 */ 1311 if (port->type == PORT_16550A && port->iotype == UPIO_PORT) 1312 fintek_8250_probe(up); 1313 1314 if (up->capabilities != old_capabilities) { 1315 dev_warn(port->dev, "detected caps %08x should be %08x\n", 1316 old_capabilities, up->capabilities); 1317 } 1318 out: 1319 DEBUG_AUTOCONF("iir=%d ", scratch); 1320 DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name); 1321 } 1322 1323 static void autoconfig_irq(struct uart_8250_port *up) 1324 { 1325 struct uart_port *port = &up->port; 1326 unsigned char save_mcr, save_ier; 1327 unsigned char save_ICP = 0; 1328 unsigned int ICP = 0; 1329 unsigned long irqs; 1330 int irq; 1331 1332 if (port->flags & UPF_FOURPORT) { 1333 ICP = (port->iobase & 0xfe0) | 0x1f; 1334 save_ICP = inb_p(ICP); 1335 outb_p(0x80, ICP); 1336 inb_p(ICP); 1337 } 1338 1339 /* forget possible initially masked and pending IRQ */ 1340 probe_irq_off(probe_irq_on()); 1341 save_mcr = serial8250_in_MCR(up); 1342 /* Synchronize UART_IER access against the console. */ 1343 uart_port_lock_irq(port); 1344 save_ier = serial_in(up, UART_IER); 1345 uart_port_unlock_irq(port); 1346 serial8250_out_MCR(up, UART_MCR_OUT1 | UART_MCR_OUT2); 1347 1348 irqs = probe_irq_on(); 1349 serial8250_out_MCR(up, 0); 1350 udelay(10); 1351 if (port->flags & UPF_FOURPORT) { 1352 serial8250_out_MCR(up, UART_MCR_DTR | UART_MCR_RTS); 1353 } else { 1354 serial8250_out_MCR(up, 1355 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2); 1356 } 1357 /* Synchronize UART_IER access against the console. */ 1358 uart_port_lock_irq(port); 1359 serial_out(up, UART_IER, UART_IER_ALL_INTR); 1360 uart_port_unlock_irq(port); 1361 serial_in(up, UART_LSR); 1362 serial_in(up, UART_RX); 1363 serial_in(up, UART_IIR); 1364 serial_in(up, UART_MSR); 1365 serial_out(up, UART_TX, 0xFF); 1366 udelay(20); 1367 irq = probe_irq_off(irqs); 1368 1369 serial8250_out_MCR(up, save_mcr); 1370 /* Synchronize UART_IER access against the console. */ 1371 uart_port_lock_irq(port); 1372 serial_out(up, UART_IER, save_ier); 1373 uart_port_unlock_irq(port); 1374 1375 if (port->flags & UPF_FOURPORT) 1376 outb_p(save_ICP, ICP); 1377 1378 port->irq = (irq > 0) ? irq : 0; 1379 } 1380 1381 static void serial8250_stop_rx(struct uart_port *port) 1382 { 1383 struct uart_8250_port *up = up_to_u8250p(port); 1384 1385 /* Port locked to synchronize UART_IER access against the console. */ 1386 lockdep_assert_held_once(&port->lock); 1387 1388 serial8250_rpm_get(up); 1389 1390 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI); 1391 serial_port_out(port, UART_IER, up->ier); 1392 1393 serial8250_rpm_put(up); 1394 } 1395 1396 /** 1397 * serial8250_em485_stop_tx() - generic ->rs485_stop_tx() callback 1398 * @p: uart 8250 port 1399 * @toggle_ier: true to allow enabling receive interrupts 1400 * 1401 * Generic callback usable by 8250 uart drivers to stop rs485 transmission. 1402 */ 1403 void serial8250_em485_stop_tx(struct uart_8250_port *p, bool toggle_ier) 1404 { 1405 unsigned char mcr = serial8250_in_MCR(p); 1406 1407 /* Port locked to synchronize UART_IER access against the console. */ 1408 lockdep_assert_held_once(&p->port.lock); 1409 1410 if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND) 1411 mcr |= UART_MCR_RTS; 1412 else 1413 mcr &= ~UART_MCR_RTS; 1414 serial8250_out_MCR(p, mcr); 1415 1416 /* 1417 * Empty the RX FIFO, we are not interested in anything 1418 * received during the half-duplex transmission. 1419 * Enable previously disabled RX interrupts. 1420 */ 1421 if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX)) { 1422 serial8250_clear_and_reinit_fifos(p); 1423 1424 if (toggle_ier) { 1425 p->ier |= UART_IER_RLSI | UART_IER_RDI; 1426 serial_port_out(&p->port, UART_IER, p->ier); 1427 } 1428 } 1429 } 1430 EXPORT_SYMBOL_GPL(serial8250_em485_stop_tx); 1431 1432 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t) 1433 { 1434 struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485, 1435 stop_tx_timer); 1436 struct uart_8250_port *p = em485->port; 1437 unsigned long flags; 1438 1439 serial8250_rpm_get(p); 1440 uart_port_lock_irqsave(&p->port, &flags); 1441 if (em485->active_timer == &em485->stop_tx_timer) { 1442 p->rs485_stop_tx(p, true); 1443 em485->active_timer = NULL; 1444 em485->tx_stopped = true; 1445 } 1446 uart_port_unlock_irqrestore(&p->port, flags); 1447 serial8250_rpm_put(p); 1448 1449 return HRTIMER_NORESTART; 1450 } 1451 1452 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec) 1453 { 1454 hrtimer_start(hrt, ms_to_ktime(msec), HRTIMER_MODE_REL); 1455 } 1456 1457 static void __stop_tx_rs485(struct uart_8250_port *p, u64 stop_delay) 1458 { 1459 struct uart_8250_em485 *em485 = p->em485; 1460 1461 /* Port locked to synchronize UART_IER access against the console. */ 1462 lockdep_assert_held_once(&p->port.lock); 1463 1464 stop_delay += (u64)p->port.rs485.delay_rts_after_send * NSEC_PER_MSEC; 1465 1466 /* 1467 * rs485_stop_tx() is going to set RTS according to config 1468 * AND flush RX FIFO if required. 1469 */ 1470 if (stop_delay > 0) { 1471 em485->active_timer = &em485->stop_tx_timer; 1472 hrtimer_start(&em485->stop_tx_timer, ns_to_ktime(stop_delay), HRTIMER_MODE_REL); 1473 } else { 1474 p->rs485_stop_tx(p, true); 1475 em485->active_timer = NULL; 1476 em485->tx_stopped = true; 1477 } 1478 } 1479 1480 static inline void __stop_tx(struct uart_8250_port *p) 1481 { 1482 struct uart_8250_em485 *em485 = p->em485; 1483 1484 if (em485) { 1485 u16 lsr = serial_lsr_in(p); 1486 u64 stop_delay = 0; 1487 1488 if (!(lsr & UART_LSR_THRE)) 1489 return; 1490 /* 1491 * To provide required timing and allow FIFO transfer, 1492 * __stop_tx_rs485() must be called only when both FIFO and 1493 * shift register are empty. The device driver should either 1494 * enable interrupt on TEMT or set UART_CAP_NOTEMT that will 1495 * enlarge stop_tx_timer by the tx time of one frame to cover 1496 * for emptying of the shift register. 1497 */ 1498 if (!(lsr & UART_LSR_TEMT)) { 1499 if (!(p->capabilities & UART_CAP_NOTEMT)) 1500 return; 1501 /* 1502 * RTS might get deasserted too early with the normal 1503 * frame timing formula. It seems to suggest THRE might 1504 * get asserted already during tx of the stop bit 1505 * rather than after it is fully sent. 1506 * Roughly estimate 1 extra bit here with / 7. 1507 */ 1508 stop_delay = p->port.frame_time + DIV_ROUND_UP(p->port.frame_time, 7); 1509 } 1510 1511 __stop_tx_rs485(p, stop_delay); 1512 } 1513 1514 if (serial8250_clear_THRI(p)) 1515 serial8250_rpm_put_tx(p); 1516 } 1517 1518 static void serial8250_stop_tx(struct uart_port *port) 1519 { 1520 struct uart_8250_port *up = up_to_u8250p(port); 1521 1522 serial8250_rpm_get(up); 1523 __stop_tx(up); 1524 1525 /* 1526 * We really want to stop the transmitter from sending. 1527 */ 1528 if (port->type == PORT_16C950) { 1529 up->acr |= UART_ACR_TXDIS; 1530 serial_icr_write(up, UART_ACR, up->acr); 1531 } 1532 serial8250_rpm_put(up); 1533 } 1534 1535 static inline void __start_tx(struct uart_port *port) 1536 { 1537 struct uart_8250_port *up = up_to_u8250p(port); 1538 1539 if (up->dma && !up->dma->tx_dma(up)) 1540 return; 1541 1542 if (serial8250_set_THRI(up)) { 1543 if (up->bugs & UART_BUG_TXEN) { 1544 u16 lsr = serial_lsr_in(up); 1545 1546 if (lsr & UART_LSR_THRE) 1547 serial8250_tx_chars(up); 1548 } 1549 } 1550 1551 /* 1552 * Re-enable the transmitter if we disabled it. 1553 */ 1554 if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) { 1555 up->acr &= ~UART_ACR_TXDIS; 1556 serial_icr_write(up, UART_ACR, up->acr); 1557 } 1558 } 1559 1560 /** 1561 * serial8250_em485_start_tx() - generic ->rs485_start_tx() callback 1562 * @up: uart 8250 port 1563 * @toggle_ier: true to allow disabling receive interrupts 1564 * 1565 * Generic callback usable by 8250 uart drivers to start rs485 transmission. 1566 * Assumes that setting the RTS bit in the MCR register means RTS is high. 1567 * (Some chips use inverse semantics.) Further assumes that reception is 1568 * stoppable by disabling the UART_IER_RDI interrupt. (Some chips set the 1569 * UART_LSR_DR bit even when UART_IER_RDI is disabled, foiling this approach.) 1570 */ 1571 void serial8250_em485_start_tx(struct uart_8250_port *up, bool toggle_ier) 1572 { 1573 unsigned char mcr = serial8250_in_MCR(up); 1574 1575 if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX) && toggle_ier) 1576 serial8250_stop_rx(&up->port); 1577 1578 if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND) 1579 mcr |= UART_MCR_RTS; 1580 else 1581 mcr &= ~UART_MCR_RTS; 1582 serial8250_out_MCR(up, mcr); 1583 } 1584 EXPORT_SYMBOL_GPL(serial8250_em485_start_tx); 1585 1586 /* Returns false, if start_tx_timer was setup to defer TX start */ 1587 static bool start_tx_rs485(struct uart_port *port) 1588 { 1589 struct uart_8250_port *up = up_to_u8250p(port); 1590 struct uart_8250_em485 *em485 = up->em485; 1591 1592 /* 1593 * While serial8250_em485_handle_stop_tx() is a noop if 1594 * em485->active_timer != &em485->stop_tx_timer, it might happen that 1595 * the timer is still armed and triggers only after the current bunch of 1596 * chars is send and em485->active_timer == &em485->stop_tx_timer again. 1597 * So cancel the timer. There is still a theoretical race condition if 1598 * the timer is already running and only comes around to check for 1599 * em485->active_timer when &em485->stop_tx_timer is armed again. 1600 */ 1601 if (em485->active_timer == &em485->stop_tx_timer) 1602 hrtimer_try_to_cancel(&em485->stop_tx_timer); 1603 1604 em485->active_timer = NULL; 1605 1606 if (em485->tx_stopped) { 1607 em485->tx_stopped = false; 1608 1609 up->rs485_start_tx(up, true); 1610 1611 if (up->port.rs485.delay_rts_before_send > 0) { 1612 em485->active_timer = &em485->start_tx_timer; 1613 start_hrtimer_ms(&em485->start_tx_timer, 1614 up->port.rs485.delay_rts_before_send); 1615 return false; 1616 } 1617 } 1618 1619 return true; 1620 } 1621 1622 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t) 1623 { 1624 struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485, 1625 start_tx_timer); 1626 struct uart_8250_port *p = em485->port; 1627 unsigned long flags; 1628 1629 uart_port_lock_irqsave(&p->port, &flags); 1630 if (em485->active_timer == &em485->start_tx_timer) { 1631 __start_tx(&p->port); 1632 em485->active_timer = NULL; 1633 } 1634 uart_port_unlock_irqrestore(&p->port, flags); 1635 1636 return HRTIMER_NORESTART; 1637 } 1638 1639 static void serial8250_start_tx(struct uart_port *port) 1640 { 1641 struct uart_8250_port *up = up_to_u8250p(port); 1642 struct uart_8250_em485 *em485 = up->em485; 1643 1644 /* Port locked to synchronize UART_IER access against the console. */ 1645 lockdep_assert_held_once(&port->lock); 1646 1647 if (!port->x_char && kfifo_is_empty(&port->state->port.xmit_fifo)) 1648 return; 1649 1650 serial8250_rpm_get_tx(up); 1651 1652 if (em485) { 1653 if ((em485->active_timer == &em485->start_tx_timer) || 1654 !start_tx_rs485(port)) 1655 return; 1656 } 1657 __start_tx(port); 1658 } 1659 1660 static void serial8250_throttle(struct uart_port *port) 1661 { 1662 port->throttle(port); 1663 } 1664 1665 static void serial8250_unthrottle(struct uart_port *port) 1666 { 1667 port->unthrottle(port); 1668 } 1669 1670 static void serial8250_disable_ms(struct uart_port *port) 1671 { 1672 struct uart_8250_port *up = up_to_u8250p(port); 1673 1674 /* Port locked to synchronize UART_IER access against the console. */ 1675 lockdep_assert_held_once(&port->lock); 1676 1677 /* no MSR capabilities */ 1678 if (up->bugs & UART_BUG_NOMSR) 1679 return; 1680 1681 mctrl_gpio_disable_ms_no_sync(up->gpios); 1682 1683 up->ier &= ~UART_IER_MSI; 1684 serial_port_out(port, UART_IER, up->ier); 1685 } 1686 1687 static void serial8250_enable_ms(struct uart_port *port) 1688 { 1689 struct uart_8250_port *up = up_to_u8250p(port); 1690 1691 /* Port locked to synchronize UART_IER access against the console. */ 1692 lockdep_assert_held_once(&port->lock); 1693 1694 /* no MSR capabilities */ 1695 if (up->bugs & UART_BUG_NOMSR) 1696 return; 1697 1698 mctrl_gpio_enable_ms(up->gpios); 1699 1700 up->ier |= UART_IER_MSI; 1701 1702 serial8250_rpm_get(up); 1703 serial_port_out(port, UART_IER, up->ier); 1704 serial8250_rpm_put(up); 1705 } 1706 1707 void serial8250_read_char(struct uart_8250_port *up, u16 lsr) 1708 { 1709 struct uart_port *port = &up->port; 1710 u8 ch, flag = TTY_NORMAL; 1711 1712 if (likely(lsr & UART_LSR_DR)) 1713 ch = serial_in(up, UART_RX); 1714 else 1715 /* 1716 * Intel 82571 has a Serial Over Lan device that will 1717 * set UART_LSR_BI without setting UART_LSR_DR when 1718 * it receives a break. To avoid reading from the 1719 * receive buffer without UART_LSR_DR bit set, we 1720 * just force the read character to be 0 1721 */ 1722 ch = 0; 1723 1724 port->icount.rx++; 1725 1726 lsr |= up->lsr_saved_flags; 1727 up->lsr_saved_flags = 0; 1728 1729 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) { 1730 if (lsr & UART_LSR_BI) { 1731 lsr &= ~(UART_LSR_FE | UART_LSR_PE); 1732 port->icount.brk++; 1733 /* 1734 * We do the SysRQ and SAK checking 1735 * here because otherwise the break 1736 * may get masked by ignore_status_mask 1737 * or read_status_mask. 1738 */ 1739 if (uart_handle_break(port)) 1740 return; 1741 } else if (lsr & UART_LSR_PE) 1742 port->icount.parity++; 1743 else if (lsr & UART_LSR_FE) 1744 port->icount.frame++; 1745 if (lsr & UART_LSR_OE) 1746 port->icount.overrun++; 1747 1748 /* 1749 * Mask off conditions which should be ignored. 1750 */ 1751 lsr &= port->read_status_mask; 1752 1753 if (lsr & UART_LSR_BI) { 1754 dev_dbg(port->dev, "handling break\n"); 1755 flag = TTY_BREAK; 1756 } else if (lsr & UART_LSR_PE) 1757 flag = TTY_PARITY; 1758 else if (lsr & UART_LSR_FE) 1759 flag = TTY_FRAME; 1760 } 1761 if (uart_prepare_sysrq_char(port, ch)) 1762 return; 1763 1764 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag); 1765 } 1766 EXPORT_SYMBOL_GPL(serial8250_read_char); 1767 1768 /* 1769 * serial8250_rx_chars - Read characters. The first LSR value must be passed in. 1770 * 1771 * Returns LSR bits. The caller should rely only on non-Rx related LSR bits 1772 * (such as THRE) because the LSR value might come from an already consumed 1773 * character. 1774 */ 1775 u16 serial8250_rx_chars(struct uart_8250_port *up, u16 lsr) 1776 { 1777 struct uart_port *port = &up->port; 1778 int max_count = 256; 1779 1780 do { 1781 serial8250_read_char(up, lsr); 1782 if (--max_count == 0) 1783 break; 1784 lsr = serial_in(up, UART_LSR); 1785 } while (lsr & (UART_LSR_DR | UART_LSR_BI)); 1786 1787 tty_flip_buffer_push(&port->state->port); 1788 return lsr; 1789 } 1790 EXPORT_SYMBOL_GPL(serial8250_rx_chars); 1791 1792 void serial8250_tx_chars(struct uart_8250_port *up) 1793 { 1794 struct uart_port *port = &up->port; 1795 struct tty_port *tport = &port->state->port; 1796 int count; 1797 1798 if (port->x_char) { 1799 uart_xchar_out(port, UART_TX); 1800 return; 1801 } 1802 if (uart_tx_stopped(port)) { 1803 serial8250_stop_tx(port); 1804 return; 1805 } 1806 if (kfifo_is_empty(&tport->xmit_fifo)) { 1807 __stop_tx(up); 1808 return; 1809 } 1810 1811 count = up->tx_loadsz; 1812 do { 1813 unsigned char c; 1814 1815 if (!uart_fifo_get(port, &c)) 1816 break; 1817 1818 serial_out(up, UART_TX, c); 1819 if (up->bugs & UART_BUG_TXRACE) { 1820 /* 1821 * The Aspeed BMC virtual UARTs have a bug where data 1822 * may get stuck in the BMC's Tx FIFO from bursts of 1823 * writes on the APB interface. 1824 * 1825 * Delay back-to-back writes by a read cycle to avoid 1826 * stalling the VUART. Read a register that won't have 1827 * side-effects and discard the result. 1828 */ 1829 serial_in(up, UART_SCR); 1830 } 1831 1832 if ((up->capabilities & UART_CAP_HFIFO) && 1833 !uart_lsr_tx_empty(serial_in(up, UART_LSR))) 1834 break; 1835 /* The BCM2835 MINI UART THRE bit is really a not-full bit. */ 1836 if ((up->capabilities & UART_CAP_MINI) && 1837 !(serial_in(up, UART_LSR) & UART_LSR_THRE)) 1838 break; 1839 } while (--count > 0); 1840 1841 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS) 1842 uart_write_wakeup(port); 1843 1844 /* 1845 * With RPM enabled, we have to wait until the FIFO is empty before the 1846 * HW can go idle. So we get here once again with empty FIFO and disable 1847 * the interrupt and RPM in __stop_tx() 1848 */ 1849 if (kfifo_is_empty(&tport->xmit_fifo) && 1850 !(up->capabilities & UART_CAP_RPM)) 1851 __stop_tx(up); 1852 } 1853 EXPORT_SYMBOL_GPL(serial8250_tx_chars); 1854 1855 /* Caller holds uart port lock */ 1856 unsigned int serial8250_modem_status(struct uart_8250_port *up) 1857 { 1858 struct uart_port *port = &up->port; 1859 unsigned int status = serial_in(up, UART_MSR); 1860 1861 status |= up->msr_saved_flags; 1862 up->msr_saved_flags = 0; 1863 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI && 1864 port->state != NULL) { 1865 if (status & UART_MSR_TERI) 1866 port->icount.rng++; 1867 if (status & UART_MSR_DDSR) 1868 port->icount.dsr++; 1869 if (status & UART_MSR_DDCD) 1870 uart_handle_dcd_change(port, status & UART_MSR_DCD); 1871 if (status & UART_MSR_DCTS) 1872 uart_handle_cts_change(port, status & UART_MSR_CTS); 1873 1874 wake_up_interruptible(&port->state->port.delta_msr_wait); 1875 } 1876 1877 return status; 1878 } 1879 EXPORT_SYMBOL_GPL(serial8250_modem_status); 1880 1881 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir) 1882 { 1883 switch (iir & 0x3f) { 1884 case UART_IIR_THRI: 1885 /* 1886 * Postpone DMA or not decision to IIR_RDI or IIR_RX_TIMEOUT 1887 * because it's impossible to do an informed decision about 1888 * that with IIR_THRI. 1889 * 1890 * This also fixes one known DMA Rx corruption issue where 1891 * DR is asserted but DMA Rx only gets a corrupted zero byte 1892 * (too early DR?). 1893 */ 1894 return false; 1895 case UART_IIR_RDI: 1896 if (!up->dma->rx_running) 1897 break; 1898 fallthrough; 1899 case UART_IIR_RLSI: 1900 case UART_IIR_RX_TIMEOUT: 1901 serial8250_rx_dma_flush(up); 1902 return true; 1903 } 1904 return up->dma->rx_dma(up); 1905 } 1906 1907 /* 1908 * This handles the interrupt from one port. 1909 */ 1910 int serial8250_handle_irq(struct uart_port *port, unsigned int iir) 1911 { 1912 struct uart_8250_port *up = up_to_u8250p(port); 1913 struct tty_port *tport = &port->state->port; 1914 bool skip_rx = false; 1915 unsigned long flags; 1916 u16 status; 1917 1918 if (iir & UART_IIR_NO_INT) 1919 return 0; 1920 1921 uart_port_lock_irqsave(port, &flags); 1922 1923 status = serial_lsr_in(up); 1924 1925 /* 1926 * If port is stopped and there are no error conditions in the 1927 * FIFO, then don't drain the FIFO, as this may lead to TTY buffer 1928 * overflow. Not servicing, RX FIFO would trigger auto HW flow 1929 * control when FIFO occupancy reaches preset threshold, thus 1930 * halting RX. This only works when auto HW flow control is 1931 * available. 1932 */ 1933 if (!(status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS)) && 1934 (port->status & (UPSTAT_AUTOCTS | UPSTAT_AUTORTS)) && 1935 !(up->ier & (UART_IER_RLSI | UART_IER_RDI))) 1936 skip_rx = true; 1937 1938 if (status & (UART_LSR_DR | UART_LSR_BI) && !skip_rx) { 1939 struct irq_data *d; 1940 1941 d = irq_get_irq_data(port->irq); 1942 if (d && irqd_is_wakeup_set(d)) 1943 pm_wakeup_event(tport->tty->dev, 0); 1944 if (!up->dma || handle_rx_dma(up, iir)) 1945 status = serial8250_rx_chars(up, status); 1946 } 1947 serial8250_modem_status(up); 1948 if ((status & UART_LSR_THRE) && (up->ier & UART_IER_THRI)) { 1949 if (!up->dma || up->dma->tx_err) 1950 serial8250_tx_chars(up); 1951 else if (!up->dma->tx_running) 1952 __stop_tx(up); 1953 } 1954 1955 uart_unlock_and_check_sysrq_irqrestore(port, flags); 1956 1957 return 1; 1958 } 1959 EXPORT_SYMBOL_GPL(serial8250_handle_irq); 1960 1961 static int serial8250_default_handle_irq(struct uart_port *port) 1962 { 1963 struct uart_8250_port *up = up_to_u8250p(port); 1964 unsigned int iir; 1965 int ret; 1966 1967 serial8250_rpm_get(up); 1968 1969 iir = serial_port_in(port, UART_IIR); 1970 ret = serial8250_handle_irq(port, iir); 1971 1972 serial8250_rpm_put(up); 1973 return ret; 1974 } 1975 1976 /* 1977 * Newer 16550 compatible parts such as the SC16C650 & Altera 16550 Soft IP 1978 * have a programmable TX threshold that triggers the THRE interrupt in 1979 * the IIR register. In this case, the THRE interrupt indicates the FIFO 1980 * has space available. Load it up with tx_loadsz bytes. 1981 */ 1982 static int serial8250_tx_threshold_handle_irq(struct uart_port *port) 1983 { 1984 unsigned long flags; 1985 unsigned int iir = serial_port_in(port, UART_IIR); 1986 1987 /* TX Threshold IRQ triggered so load up FIFO */ 1988 if ((iir & UART_IIR_ID) == UART_IIR_THRI) { 1989 struct uart_8250_port *up = up_to_u8250p(port); 1990 1991 uart_port_lock_irqsave(port, &flags); 1992 serial8250_tx_chars(up); 1993 uart_port_unlock_irqrestore(port, flags); 1994 } 1995 1996 iir = serial_port_in(port, UART_IIR); 1997 return serial8250_handle_irq(port, iir); 1998 } 1999 2000 static unsigned int serial8250_tx_empty(struct uart_port *port) 2001 { 2002 struct uart_8250_port *up = up_to_u8250p(port); 2003 unsigned int result = 0; 2004 unsigned long flags; 2005 2006 serial8250_rpm_get(up); 2007 2008 uart_port_lock_irqsave(port, &flags); 2009 if (!serial8250_tx_dma_running(up) && uart_lsr_tx_empty(serial_lsr_in(up))) 2010 result = TIOCSER_TEMT; 2011 uart_port_unlock_irqrestore(port, flags); 2012 2013 serial8250_rpm_put(up); 2014 2015 return result; 2016 } 2017 2018 unsigned int serial8250_do_get_mctrl(struct uart_port *port) 2019 { 2020 struct uart_8250_port *up = up_to_u8250p(port); 2021 unsigned int status; 2022 unsigned int val; 2023 2024 serial8250_rpm_get(up); 2025 status = serial8250_modem_status(up); 2026 serial8250_rpm_put(up); 2027 2028 val = serial8250_MSR_to_TIOCM(status); 2029 if (up->gpios) 2030 return mctrl_gpio_get(up->gpios, &val); 2031 2032 return val; 2033 } 2034 EXPORT_SYMBOL_GPL(serial8250_do_get_mctrl); 2035 2036 static unsigned int serial8250_get_mctrl(struct uart_port *port) 2037 { 2038 if (port->get_mctrl) 2039 return port->get_mctrl(port); 2040 return serial8250_do_get_mctrl(port); 2041 } 2042 2043 void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl) 2044 { 2045 struct uart_8250_port *up = up_to_u8250p(port); 2046 unsigned char mcr; 2047 2048 mcr = serial8250_TIOCM_to_MCR(mctrl); 2049 2050 mcr |= up->mcr; 2051 2052 serial8250_out_MCR(up, mcr); 2053 } 2054 EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl); 2055 2056 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl) 2057 { 2058 if (port->rs485.flags & SER_RS485_ENABLED) 2059 return; 2060 2061 if (port->set_mctrl) 2062 port->set_mctrl(port, mctrl); 2063 else 2064 serial8250_do_set_mctrl(port, mctrl); 2065 } 2066 2067 static void serial8250_break_ctl(struct uart_port *port, int break_state) 2068 { 2069 struct uart_8250_port *up = up_to_u8250p(port); 2070 unsigned long flags; 2071 2072 serial8250_rpm_get(up); 2073 uart_port_lock_irqsave(port, &flags); 2074 if (break_state == -1) 2075 up->lcr |= UART_LCR_SBC; 2076 else 2077 up->lcr &= ~UART_LCR_SBC; 2078 serial_port_out(port, UART_LCR, up->lcr); 2079 uart_port_unlock_irqrestore(port, flags); 2080 serial8250_rpm_put(up); 2081 } 2082 2083 /* Returns true if @bits were set, false on timeout */ 2084 static bool wait_for_lsr(struct uart_8250_port *up, int bits) 2085 { 2086 unsigned int status, tmout; 2087 2088 /* 2089 * Wait for a character to be sent. Fallback to a safe default 2090 * timeout value if @frame_time is not available. 2091 */ 2092 if (up->port.frame_time) 2093 tmout = up->port.frame_time * 2 / NSEC_PER_USEC; 2094 else 2095 tmout = 10000; 2096 2097 for (;;) { 2098 status = serial_lsr_in(up); 2099 2100 if ((status & bits) == bits) 2101 break; 2102 if (--tmout == 0) 2103 break; 2104 udelay(1); 2105 touch_nmi_watchdog(); 2106 } 2107 2108 return (tmout != 0); 2109 } 2110 2111 /* Wait for transmitter and holding register to empty with timeout */ 2112 static void wait_for_xmitr(struct uart_8250_port *up, int bits) 2113 { 2114 unsigned int tmout; 2115 2116 wait_for_lsr(up, bits); 2117 2118 /* Wait up to 1s for flow control if necessary */ 2119 if (up->port.flags & UPF_CONS_FLOW) { 2120 for (tmout = 1000000; tmout; tmout--) { 2121 unsigned int msr = serial_in(up, UART_MSR); 2122 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS; 2123 if (msr & UART_MSR_CTS) 2124 break; 2125 udelay(1); 2126 touch_nmi_watchdog(); 2127 } 2128 } 2129 } 2130 2131 #ifdef CONFIG_CONSOLE_POLL 2132 /* 2133 * Console polling routines for writing and reading from the uart while 2134 * in an interrupt or debug context. 2135 */ 2136 2137 static int serial8250_get_poll_char(struct uart_port *port) 2138 { 2139 struct uart_8250_port *up = up_to_u8250p(port); 2140 int status; 2141 u16 lsr; 2142 2143 serial8250_rpm_get(up); 2144 2145 lsr = serial_port_in(port, UART_LSR); 2146 2147 if (!(lsr & UART_LSR_DR)) { 2148 status = NO_POLL_CHAR; 2149 goto out; 2150 } 2151 2152 status = serial_port_in(port, UART_RX); 2153 out: 2154 serial8250_rpm_put(up); 2155 return status; 2156 } 2157 2158 2159 static void serial8250_put_poll_char(struct uart_port *port, 2160 unsigned char c) 2161 { 2162 unsigned int ier; 2163 struct uart_8250_port *up = up_to_u8250p(port); 2164 2165 /* 2166 * Normally the port is locked to synchronize UART_IER access 2167 * against the console. However, this function is only used by 2168 * KDB/KGDB, where it may not be possible to acquire the port 2169 * lock because all other CPUs are quiesced. The quiescence 2170 * should allow safe lockless usage here. 2171 */ 2172 2173 serial8250_rpm_get(up); 2174 /* 2175 * First save the IER then disable the interrupts 2176 */ 2177 ier = serial_port_in(port, UART_IER); 2178 serial8250_clear_IER(up); 2179 2180 wait_for_xmitr(up, UART_LSR_BOTH_EMPTY); 2181 /* 2182 * Send the character out. 2183 */ 2184 serial_port_out(port, UART_TX, c); 2185 2186 /* 2187 * Finally, wait for transmitter to become empty 2188 * and restore the IER 2189 */ 2190 wait_for_xmitr(up, UART_LSR_BOTH_EMPTY); 2191 serial_port_out(port, UART_IER, ier); 2192 serial8250_rpm_put(up); 2193 } 2194 2195 #endif /* CONFIG_CONSOLE_POLL */ 2196 2197 int serial8250_do_startup(struct uart_port *port) 2198 { 2199 struct uart_8250_port *up = up_to_u8250p(port); 2200 unsigned long flags; 2201 unsigned char iir; 2202 int retval; 2203 u16 lsr; 2204 2205 if (!port->fifosize) 2206 port->fifosize = uart_config[port->type].fifo_size; 2207 if (!up->tx_loadsz) 2208 up->tx_loadsz = uart_config[port->type].tx_loadsz; 2209 if (!up->capabilities) 2210 up->capabilities = uart_config[port->type].flags; 2211 up->mcr = 0; 2212 2213 if (port->iotype != up->cur_iotype) 2214 set_io_from_upio(port); 2215 2216 serial8250_rpm_get(up); 2217 if (port->type == PORT_16C950) { 2218 /* 2219 * Wake up and initialize UART 2220 * 2221 * Synchronize UART_IER access against the console. 2222 */ 2223 uart_port_lock_irqsave(port, &flags); 2224 up->acr = 0; 2225 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B); 2226 serial_port_out(port, UART_EFR, UART_EFR_ECB); 2227 serial_port_out(port, UART_IER, 0); 2228 serial_port_out(port, UART_LCR, 0); 2229 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */ 2230 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B); 2231 serial_port_out(port, UART_EFR, UART_EFR_ECB); 2232 serial_port_out(port, UART_LCR, 0); 2233 uart_port_unlock_irqrestore(port, flags); 2234 } 2235 2236 if (port->type == PORT_DA830) { 2237 /* 2238 * Reset the port 2239 * 2240 * Synchronize UART_IER access against the console. 2241 */ 2242 uart_port_lock_irqsave(port, &flags); 2243 serial_port_out(port, UART_IER, 0); 2244 serial_port_out(port, UART_DA830_PWREMU_MGMT, 0); 2245 uart_port_unlock_irqrestore(port, flags); 2246 mdelay(10); 2247 2248 /* Enable Tx, Rx and free run mode */ 2249 serial_port_out(port, UART_DA830_PWREMU_MGMT, 2250 UART_DA830_PWREMU_MGMT_UTRST | 2251 UART_DA830_PWREMU_MGMT_URRST | 2252 UART_DA830_PWREMU_MGMT_FREE); 2253 } 2254 2255 #ifdef CONFIG_SERIAL_8250_RSA 2256 /* 2257 * If this is an RSA port, see if we can kick it up to the 2258 * higher speed clock. 2259 */ 2260 enable_rsa(up); 2261 #endif 2262 2263 /* 2264 * Clear the FIFO buffers and disable them. 2265 * (they will be reenabled in set_termios()) 2266 */ 2267 serial8250_clear_fifos(up); 2268 2269 /* 2270 * Clear the interrupt registers. 2271 */ 2272 serial_port_in(port, UART_LSR); 2273 serial_port_in(port, UART_RX); 2274 serial_port_in(port, UART_IIR); 2275 serial_port_in(port, UART_MSR); 2276 2277 /* 2278 * At this point, there's no way the LSR could still be 0xff; 2279 * if it is, then bail out, because there's likely no UART 2280 * here. 2281 */ 2282 if (!(port->flags & UPF_BUGGY_UART) && 2283 (serial_port_in(port, UART_LSR) == 0xff)) { 2284 dev_info_ratelimited(port->dev, "LSR safety check engaged!\n"); 2285 retval = -ENODEV; 2286 goto out; 2287 } 2288 2289 /* 2290 * For a XR16C850, we need to set the trigger levels 2291 */ 2292 if (port->type == PORT_16850) { 2293 unsigned char fctr; 2294 2295 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); 2296 2297 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX); 2298 serial_port_out(port, UART_FCTR, 2299 fctr | UART_FCTR_TRGD | UART_FCTR_RX); 2300 serial_port_out(port, UART_TRG, UART_TRG_96); 2301 serial_port_out(port, UART_FCTR, 2302 fctr | UART_FCTR_TRGD | UART_FCTR_TX); 2303 serial_port_out(port, UART_TRG, UART_TRG_96); 2304 2305 serial_port_out(port, UART_LCR, 0); 2306 } 2307 2308 /* 2309 * For the Altera 16550 variants, set TX threshold trigger level. 2310 */ 2311 if (((port->type == PORT_ALTR_16550_F32) || 2312 (port->type == PORT_ALTR_16550_F64) || 2313 (port->type == PORT_ALTR_16550_F128)) && (port->fifosize > 1)) { 2314 /* Bounds checking of TX threshold (valid 0 to fifosize-2) */ 2315 if ((up->tx_loadsz < 2) || (up->tx_loadsz > port->fifosize)) { 2316 dev_err(port->dev, "TX FIFO Threshold errors, skipping\n"); 2317 } else { 2318 serial_port_out(port, UART_ALTR_AFR, 2319 UART_ALTR_EN_TXFIFO_LW); 2320 serial_port_out(port, UART_ALTR_TX_LOW, 2321 port->fifosize - up->tx_loadsz); 2322 port->handle_irq = serial8250_tx_threshold_handle_irq; 2323 } 2324 } 2325 2326 /* Check if we need to have shared IRQs */ 2327 if (port->irq && (up->port.flags & UPF_SHARE_IRQ)) 2328 up->port.irqflags |= IRQF_SHARED; 2329 2330 retval = up->ops->setup_irq(up); 2331 if (retval) 2332 goto out; 2333 2334 if (port->irq && !(up->port.flags & UPF_NO_THRE_TEST)) { 2335 unsigned char iir1; 2336 2337 if (port->irqflags & IRQF_SHARED) 2338 disable_irq_nosync(port->irq); 2339 2340 /* 2341 * Test for UARTs that do not reassert THRE when the 2342 * transmitter is idle and the interrupt has already 2343 * been cleared. Real 16550s should always reassert 2344 * this interrupt whenever the transmitter is idle and 2345 * the interrupt is enabled. Delays are necessary to 2346 * allow register changes to become visible. 2347 * 2348 * Synchronize UART_IER access against the console. 2349 */ 2350 uart_port_lock_irqsave(port, &flags); 2351 2352 wait_for_xmitr(up, UART_LSR_THRE); 2353 serial_port_out_sync(port, UART_IER, UART_IER_THRI); 2354 udelay(1); /* allow THRE to set */ 2355 iir1 = serial_port_in(port, UART_IIR); 2356 serial_port_out(port, UART_IER, 0); 2357 serial_port_out_sync(port, UART_IER, UART_IER_THRI); 2358 udelay(1); /* allow a working UART time to re-assert THRE */ 2359 iir = serial_port_in(port, UART_IIR); 2360 serial_port_out(port, UART_IER, 0); 2361 2362 uart_port_unlock_irqrestore(port, flags); 2363 2364 if (port->irqflags & IRQF_SHARED) 2365 enable_irq(port->irq); 2366 2367 /* 2368 * If the interrupt is not reasserted, or we otherwise 2369 * don't trust the iir, setup a timer to kick the UART 2370 * on a regular basis. 2371 */ 2372 if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) || 2373 up->port.flags & UPF_BUG_THRE) { 2374 up->bugs |= UART_BUG_THRE; 2375 } 2376 } 2377 2378 up->ops->setup_timer(up); 2379 2380 /* 2381 * Now, initialize the UART 2382 */ 2383 serial_port_out(port, UART_LCR, UART_LCR_WLEN8); 2384 2385 uart_port_lock_irqsave(port, &flags); 2386 if (up->port.flags & UPF_FOURPORT) { 2387 if (!up->port.irq) 2388 up->port.mctrl |= TIOCM_OUT1; 2389 } else 2390 /* 2391 * Most PC uarts need OUT2 raised to enable interrupts. 2392 */ 2393 if (port->irq) 2394 up->port.mctrl |= TIOCM_OUT2; 2395 2396 serial8250_set_mctrl(port, port->mctrl); 2397 2398 /* 2399 * Serial over Lan (SoL) hack: 2400 * Intel 8257x Gigabit ethernet chips have a 16550 emulation, to be 2401 * used for Serial Over Lan. Those chips take a longer time than a 2402 * normal serial device to signalize that a transmission data was 2403 * queued. Due to that, the above test generally fails. One solution 2404 * would be to delay the reading of iir. However, this is not 2405 * reliable, since the timeout is variable. So, let's just don't 2406 * test if we receive TX irq. This way, we'll never enable 2407 * UART_BUG_TXEN. 2408 */ 2409 if (!(up->port.quirks & UPQ_NO_TXEN_TEST)) { 2410 /* 2411 * Do a quick test to see if we receive an interrupt when we 2412 * enable the TX irq. 2413 */ 2414 serial_port_out(port, UART_IER, UART_IER_THRI); 2415 lsr = serial_port_in(port, UART_LSR); 2416 iir = serial_port_in(port, UART_IIR); 2417 serial_port_out(port, UART_IER, 0); 2418 2419 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) { 2420 if (!(up->bugs & UART_BUG_TXEN)) { 2421 up->bugs |= UART_BUG_TXEN; 2422 dev_dbg(port->dev, "enabling bad tx status workarounds\n"); 2423 } 2424 } else { 2425 up->bugs &= ~UART_BUG_TXEN; 2426 } 2427 } 2428 2429 uart_port_unlock_irqrestore(port, flags); 2430 2431 /* 2432 * Clear the interrupt registers again for luck, and clear the 2433 * saved flags to avoid getting false values from polling 2434 * routines or the previous session. 2435 */ 2436 serial_port_in(port, UART_LSR); 2437 serial_port_in(port, UART_RX); 2438 serial_port_in(port, UART_IIR); 2439 serial_port_in(port, UART_MSR); 2440 up->lsr_saved_flags = 0; 2441 up->msr_saved_flags = 0; 2442 2443 /* 2444 * Request DMA channels for both RX and TX. 2445 */ 2446 if (up->dma) { 2447 const char *msg = NULL; 2448 2449 if (uart_console(port)) 2450 msg = "forbid DMA for kernel console"; 2451 else if (serial8250_request_dma(up)) 2452 msg = "failed to request DMA"; 2453 if (msg) { 2454 dev_warn_ratelimited(port->dev, "%s\n", msg); 2455 up->dma = NULL; 2456 } 2457 } 2458 2459 /* 2460 * Set the IER shadow for rx interrupts but defer actual interrupt 2461 * enable until after the FIFOs are enabled; otherwise, an already- 2462 * active sender can swamp the interrupt handler with "too much work". 2463 */ 2464 up->ier = UART_IER_RLSI | UART_IER_RDI; 2465 2466 if (port->flags & UPF_FOURPORT) { 2467 unsigned int icp; 2468 /* 2469 * Enable interrupts on the AST Fourport board 2470 */ 2471 icp = (port->iobase & 0xfe0) | 0x01f; 2472 outb_p(0x80, icp); 2473 inb_p(icp); 2474 } 2475 retval = 0; 2476 out: 2477 serial8250_rpm_put(up); 2478 return retval; 2479 } 2480 EXPORT_SYMBOL_GPL(serial8250_do_startup); 2481 2482 static int serial8250_startup(struct uart_port *port) 2483 { 2484 if (port->startup) 2485 return port->startup(port); 2486 return serial8250_do_startup(port); 2487 } 2488 2489 void serial8250_do_shutdown(struct uart_port *port) 2490 { 2491 struct uart_8250_port *up = up_to_u8250p(port); 2492 unsigned long flags; 2493 2494 serial8250_rpm_get(up); 2495 /* 2496 * Disable interrupts from this port 2497 * 2498 * Synchronize UART_IER access against the console. 2499 */ 2500 uart_port_lock_irqsave(port, &flags); 2501 up->ier = 0; 2502 serial_port_out(port, UART_IER, 0); 2503 uart_port_unlock_irqrestore(port, flags); 2504 2505 synchronize_irq(port->irq); 2506 2507 if (up->dma) 2508 serial8250_release_dma(up); 2509 2510 uart_port_lock_irqsave(port, &flags); 2511 if (port->flags & UPF_FOURPORT) { 2512 /* reset interrupts on the AST Fourport board */ 2513 inb((port->iobase & 0xfe0) | 0x1f); 2514 port->mctrl |= TIOCM_OUT1; 2515 } else 2516 port->mctrl &= ~TIOCM_OUT2; 2517 2518 serial8250_set_mctrl(port, port->mctrl); 2519 uart_port_unlock_irqrestore(port, flags); 2520 2521 /* 2522 * Disable break condition and FIFOs 2523 */ 2524 serial_port_out(port, UART_LCR, 2525 serial_port_in(port, UART_LCR) & ~UART_LCR_SBC); 2526 serial8250_clear_fifos(up); 2527 2528 #ifdef CONFIG_SERIAL_8250_RSA 2529 /* 2530 * Reset the RSA board back to 115kbps compat mode. 2531 */ 2532 disable_rsa(up); 2533 #endif 2534 2535 /* 2536 * Read data port to reset things, and then unlink from 2537 * the IRQ chain. 2538 */ 2539 serial_port_in(port, UART_RX); 2540 serial8250_rpm_put(up); 2541 2542 up->ops->release_irq(up); 2543 } 2544 EXPORT_SYMBOL_GPL(serial8250_do_shutdown); 2545 2546 static void serial8250_shutdown(struct uart_port *port) 2547 { 2548 if (port->shutdown) 2549 port->shutdown(port); 2550 else 2551 serial8250_do_shutdown(port); 2552 } 2553 2554 static void serial8250_flush_buffer(struct uart_port *port) 2555 { 2556 struct uart_8250_port *up = up_to_u8250p(port); 2557 2558 if (up->dma) 2559 serial8250_tx_dma_flush(up); 2560 } 2561 2562 static unsigned int serial8250_do_get_divisor(struct uart_port *port, 2563 unsigned int baud, 2564 unsigned int *frac) 2565 { 2566 upf_t magic_multiplier = port->flags & UPF_MAGIC_MULTIPLIER; 2567 struct uart_8250_port *up = up_to_u8250p(port); 2568 unsigned int quot; 2569 2570 /* 2571 * Handle magic divisors for baud rates above baud_base on SMSC 2572 * Super I/O chips. We clamp custom rates from clk/6 and clk/12 2573 * up to clk/4 (0x8001) and clk/8 (0x8002) respectively. These 2574 * magic divisors actually reprogram the baud rate generator's 2575 * reference clock derived from chips's 14.318MHz clock input. 2576 * 2577 * Documentation claims that with these magic divisors the base 2578 * frequencies of 7.3728MHz and 3.6864MHz are used respectively 2579 * for the extra baud rates of 460800bps and 230400bps rather 2580 * than the usual base frequency of 1.8462MHz. However empirical 2581 * evidence contradicts that. 2582 * 2583 * Instead bit 7 of the DLM register (bit 15 of the divisor) is 2584 * effectively used as a clock prescaler selection bit for the 2585 * base frequency of 7.3728MHz, always used. If set to 0, then 2586 * the base frequency is divided by 4 for use by the Baud Rate 2587 * Generator, for the usual arrangement where the value of 1 of 2588 * the divisor produces the baud rate of 115200bps. Conversely, 2589 * if set to 1 and high-speed operation has been enabled with the 2590 * Serial Port Mode Register in the Device Configuration Space, 2591 * then the base frequency is supplied directly to the Baud Rate 2592 * Generator, so for the divisor values of 0x8001, 0x8002, 0x8003, 2593 * 0x8004, etc. the respective baud rates produced are 460800bps, 2594 * 230400bps, 153600bps, 115200bps, etc. 2595 * 2596 * In all cases only low 15 bits of the divisor are used to divide 2597 * the baud base and therefore 32767 is the maximum divisor value 2598 * possible, even though documentation says that the programmable 2599 * Baud Rate Generator is capable of dividing the internal PLL 2600 * clock by any divisor from 1 to 65535. 2601 */ 2602 if (magic_multiplier && baud >= port->uartclk / 6) 2603 quot = 0x8001; 2604 else if (magic_multiplier && baud >= port->uartclk / 12) 2605 quot = 0x8002; 2606 else 2607 quot = uart_get_divisor(port, baud); 2608 2609 /* 2610 * Oxford Semi 952 rev B workaround 2611 */ 2612 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0) 2613 quot++; 2614 2615 return quot; 2616 } 2617 2618 static unsigned int serial8250_get_divisor(struct uart_port *port, 2619 unsigned int baud, 2620 unsigned int *frac) 2621 { 2622 if (port->get_divisor) 2623 return port->get_divisor(port, baud, frac); 2624 2625 return serial8250_do_get_divisor(port, baud, frac); 2626 } 2627 2628 static unsigned char serial8250_compute_lcr(struct uart_8250_port *up, 2629 tcflag_t c_cflag) 2630 { 2631 unsigned char cval; 2632 2633 cval = UART_LCR_WLEN(tty_get_char_size(c_cflag)); 2634 2635 if (c_cflag & CSTOPB) 2636 cval |= UART_LCR_STOP; 2637 if (c_cflag & PARENB) 2638 cval |= UART_LCR_PARITY; 2639 if (!(c_cflag & PARODD)) 2640 cval |= UART_LCR_EPAR; 2641 if (c_cflag & CMSPAR) 2642 cval |= UART_LCR_SPAR; 2643 2644 return cval; 2645 } 2646 2647 void serial8250_do_set_divisor(struct uart_port *port, unsigned int baud, 2648 unsigned int quot) 2649 { 2650 struct uart_8250_port *up = up_to_u8250p(port); 2651 2652 /* Workaround to enable 115200 baud on OMAP1510 internal ports */ 2653 if (is_omap1510_8250(up)) { 2654 if (baud == 115200) { 2655 quot = 1; 2656 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1); 2657 } else 2658 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0); 2659 } 2660 2661 /* 2662 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2, 2663 * otherwise just set DLAB 2664 */ 2665 if (up->capabilities & UART_NATSEMI) 2666 serial_port_out(port, UART_LCR, 0xe0); 2667 else 2668 serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB); 2669 2670 serial_dl_write(up, quot); 2671 } 2672 EXPORT_SYMBOL_GPL(serial8250_do_set_divisor); 2673 2674 static void serial8250_set_divisor(struct uart_port *port, unsigned int baud, 2675 unsigned int quot, unsigned int quot_frac) 2676 { 2677 if (port->set_divisor) 2678 port->set_divisor(port, baud, quot, quot_frac); 2679 else 2680 serial8250_do_set_divisor(port, baud, quot); 2681 } 2682 2683 static unsigned int serial8250_get_baud_rate(struct uart_port *port, 2684 struct ktermios *termios, 2685 const struct ktermios *old) 2686 { 2687 unsigned int tolerance = port->uartclk / 100; 2688 unsigned int min; 2689 unsigned int max; 2690 2691 /* 2692 * Handle magic divisors for baud rates above baud_base on SMSC 2693 * Super I/O chips. Enable custom rates of clk/4 and clk/8, but 2694 * disable divisor values beyond 32767, which are unavailable. 2695 */ 2696 if (port->flags & UPF_MAGIC_MULTIPLIER) { 2697 min = port->uartclk / 16 / UART_DIV_MAX >> 1; 2698 max = (port->uartclk + tolerance) / 4; 2699 } else { 2700 min = port->uartclk / 16 / UART_DIV_MAX; 2701 max = (port->uartclk + tolerance) / 16; 2702 } 2703 2704 /* 2705 * Ask the core to calculate the divisor for us. 2706 * Allow 1% tolerance at the upper limit so uart clks marginally 2707 * slower than nominal still match standard baud rates without 2708 * causing transmission errors. 2709 */ 2710 return uart_get_baud_rate(port, termios, old, min, max); 2711 } 2712 2713 /* 2714 * Note in order to avoid the tty port mutex deadlock don't use the next method 2715 * within the uart port callbacks. Primarily it's supposed to be utilized to 2716 * handle a sudden reference clock rate change. 2717 */ 2718 void serial8250_update_uartclk(struct uart_port *port, unsigned int uartclk) 2719 { 2720 struct tty_port *tport = &port->state->port; 2721 struct tty_struct *tty; 2722 2723 tty = tty_port_tty_get(tport); 2724 if (!tty) { 2725 mutex_lock(&tport->mutex); 2726 port->uartclk = uartclk; 2727 mutex_unlock(&tport->mutex); 2728 return; 2729 } 2730 2731 down_write(&tty->termios_rwsem); 2732 mutex_lock(&tport->mutex); 2733 2734 if (port->uartclk == uartclk) 2735 goto out_unlock; 2736 2737 port->uartclk = uartclk; 2738 2739 if (!tty_port_initialized(tport)) 2740 goto out_unlock; 2741 2742 serial8250_do_set_termios(port, &tty->termios, NULL); 2743 2744 out_unlock: 2745 mutex_unlock(&tport->mutex); 2746 up_write(&tty->termios_rwsem); 2747 tty_kref_put(tty); 2748 } 2749 EXPORT_SYMBOL_GPL(serial8250_update_uartclk); 2750 2751 void 2752 serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios, 2753 const struct ktermios *old) 2754 { 2755 struct uart_8250_port *up = up_to_u8250p(port); 2756 unsigned char cval; 2757 unsigned long flags; 2758 unsigned int baud, quot, frac = 0; 2759 2760 if (up->capabilities & UART_CAP_MINI) { 2761 termios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CMSPAR); 2762 if ((termios->c_cflag & CSIZE) == CS5 || 2763 (termios->c_cflag & CSIZE) == CS6) 2764 termios->c_cflag = (termios->c_cflag & ~CSIZE) | CS7; 2765 } 2766 cval = serial8250_compute_lcr(up, termios->c_cflag); 2767 2768 baud = serial8250_get_baud_rate(port, termios, old); 2769 quot = serial8250_get_divisor(port, baud, &frac); 2770 2771 /* 2772 * Ok, we're now changing the port state. Do it with 2773 * interrupts disabled. 2774 * 2775 * Synchronize UART_IER access against the console. 2776 */ 2777 serial8250_rpm_get(up); 2778 uart_port_lock_irqsave(port, &flags); 2779 2780 up->lcr = cval; /* Save computed LCR */ 2781 2782 if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) { 2783 if (baud < 2400 && !up->dma) { 2784 up->fcr &= ~UART_FCR_TRIGGER_MASK; 2785 up->fcr |= UART_FCR_TRIGGER_1; 2786 } 2787 } 2788 2789 /* 2790 * MCR-based auto flow control. When AFE is enabled, RTS will be 2791 * deasserted when the receive FIFO contains more characters than 2792 * the trigger, or the MCR RTS bit is cleared. 2793 */ 2794 if (up->capabilities & UART_CAP_AFE) { 2795 up->mcr &= ~UART_MCR_AFE; 2796 if (termios->c_cflag & CRTSCTS) 2797 up->mcr |= UART_MCR_AFE; 2798 } 2799 2800 /* 2801 * Update the per-port timeout. 2802 */ 2803 uart_update_timeout(port, termios->c_cflag, baud); 2804 2805 /* 2806 * Specify which conditions may be considered for error 2807 * handling and the ignoring of characters. The actual 2808 * ignoring of characters only occurs if the bit is set 2809 * in @ignore_status_mask as well. 2810 */ 2811 port->read_status_mask = UART_LSR_OE | UART_LSR_DR; 2812 if (termios->c_iflag & INPCK) 2813 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE; 2814 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 2815 port->read_status_mask |= UART_LSR_BI; 2816 2817 /* 2818 * Characters to ignore 2819 */ 2820 port->ignore_status_mask = 0; 2821 if (termios->c_iflag & IGNPAR) 2822 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE; 2823 if (termios->c_iflag & IGNBRK) { 2824 port->ignore_status_mask |= UART_LSR_BI; 2825 /* 2826 * If we're ignoring parity and break indicators, 2827 * ignore overruns too (for real raw support). 2828 */ 2829 if (termios->c_iflag & IGNPAR) 2830 port->ignore_status_mask |= UART_LSR_OE; 2831 } 2832 2833 /* 2834 * ignore all characters if CREAD is not set 2835 */ 2836 if ((termios->c_cflag & CREAD) == 0) 2837 port->ignore_status_mask |= UART_LSR_DR; 2838 2839 /* 2840 * CTS flow control flag and modem status interrupts 2841 */ 2842 up->ier &= ~UART_IER_MSI; 2843 if (!(up->bugs & UART_BUG_NOMSR) && 2844 UART_ENABLE_MS(&up->port, termios->c_cflag)) 2845 up->ier |= UART_IER_MSI; 2846 if (up->capabilities & UART_CAP_UUE) 2847 up->ier |= UART_IER_UUE; 2848 if (up->capabilities & UART_CAP_RTOIE) 2849 up->ier |= UART_IER_RTOIE; 2850 2851 serial_port_out(port, UART_IER, up->ier); 2852 2853 if (up->capabilities & UART_CAP_EFR) { 2854 unsigned char efr = 0; 2855 /* 2856 * TI16C752/Startech hardware flow control. FIXME: 2857 * - TI16C752 requires control thresholds to be set. 2858 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled. 2859 */ 2860 if (termios->c_cflag & CRTSCTS) 2861 efr |= UART_EFR_CTS; 2862 2863 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B); 2864 if (port->flags & UPF_EXAR_EFR) 2865 serial_port_out(port, UART_XR_EFR, efr); 2866 else 2867 serial_port_out(port, UART_EFR, efr); 2868 } 2869 2870 serial8250_set_divisor(port, baud, quot, frac); 2871 2872 /* 2873 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR 2874 * is written without DLAB set, this mode will be disabled. 2875 */ 2876 if (port->type == PORT_16750) 2877 serial_port_out(port, UART_FCR, up->fcr); 2878 2879 serial_port_out(port, UART_LCR, up->lcr); /* reset DLAB */ 2880 if (port->type != PORT_16750) { 2881 /* emulated UARTs (Lucent Venus 167x) need two steps */ 2882 if (up->fcr & UART_FCR_ENABLE_FIFO) 2883 serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO); 2884 serial_port_out(port, UART_FCR, up->fcr); /* set fcr */ 2885 } 2886 serial8250_set_mctrl(port, port->mctrl); 2887 uart_port_unlock_irqrestore(port, flags); 2888 serial8250_rpm_put(up); 2889 2890 /* Don't rewrite B0 */ 2891 if (tty_termios_baud_rate(termios)) 2892 tty_termios_encode_baud_rate(termios, baud, baud); 2893 } 2894 EXPORT_SYMBOL(serial8250_do_set_termios); 2895 2896 static void 2897 serial8250_set_termios(struct uart_port *port, struct ktermios *termios, 2898 const struct ktermios *old) 2899 { 2900 if (port->set_termios) 2901 port->set_termios(port, termios, old); 2902 else 2903 serial8250_do_set_termios(port, termios, old); 2904 } 2905 2906 void serial8250_do_set_ldisc(struct uart_port *port, struct ktermios *termios) 2907 { 2908 if (termios->c_line == N_PPS) { 2909 port->flags |= UPF_HARDPPS_CD; 2910 uart_port_lock_irq(port); 2911 serial8250_enable_ms(port); 2912 uart_port_unlock_irq(port); 2913 } else { 2914 port->flags &= ~UPF_HARDPPS_CD; 2915 if (!UART_ENABLE_MS(port, termios->c_cflag)) { 2916 uart_port_lock_irq(port); 2917 serial8250_disable_ms(port); 2918 uart_port_unlock_irq(port); 2919 } 2920 } 2921 } 2922 EXPORT_SYMBOL_GPL(serial8250_do_set_ldisc); 2923 2924 static void 2925 serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios) 2926 { 2927 if (port->set_ldisc) 2928 port->set_ldisc(port, termios); 2929 else 2930 serial8250_do_set_ldisc(port, termios); 2931 } 2932 2933 void serial8250_do_pm(struct uart_port *port, unsigned int state, 2934 unsigned int oldstate) 2935 { 2936 struct uart_8250_port *p = up_to_u8250p(port); 2937 2938 serial8250_set_sleep(p, state != 0); 2939 } 2940 EXPORT_SYMBOL(serial8250_do_pm); 2941 2942 static void 2943 serial8250_pm(struct uart_port *port, unsigned int state, 2944 unsigned int oldstate) 2945 { 2946 if (port->pm) 2947 port->pm(port, state, oldstate); 2948 else 2949 serial8250_do_pm(port, state, oldstate); 2950 } 2951 2952 static unsigned int serial8250_port_size(struct uart_8250_port *pt) 2953 { 2954 if (pt->port.mapsize) 2955 return pt->port.mapsize; 2956 if (is_omap1_8250(pt)) 2957 return 0x16 << pt->port.regshift; 2958 2959 return 8 << pt->port.regshift; 2960 } 2961 2962 /* 2963 * Resource handling. 2964 */ 2965 static int serial8250_request_std_resource(struct uart_8250_port *up) 2966 { 2967 unsigned int size = serial8250_port_size(up); 2968 struct uart_port *port = &up->port; 2969 2970 switch (port->iotype) { 2971 case UPIO_AU: 2972 case UPIO_TSI: 2973 case UPIO_MEM32: 2974 case UPIO_MEM32BE: 2975 case UPIO_MEM16: 2976 case UPIO_MEM: 2977 if (!port->mapbase) 2978 return -EINVAL; 2979 2980 if (!request_mem_region(port->mapbase, size, "serial")) 2981 return -EBUSY; 2982 2983 if (port->flags & UPF_IOREMAP) { 2984 port->membase = ioremap(port->mapbase, size); 2985 if (!port->membase) { 2986 release_mem_region(port->mapbase, size); 2987 return -ENOMEM; 2988 } 2989 } 2990 return 0; 2991 case UPIO_HUB6: 2992 case UPIO_PORT: 2993 if (!request_region(port->iobase, size, "serial")) 2994 return -EBUSY; 2995 return 0; 2996 } 2997 2998 return 0; 2999 } 3000 3001 static void serial8250_release_std_resource(struct uart_8250_port *up) 3002 { 3003 unsigned int size = serial8250_port_size(up); 3004 struct uart_port *port = &up->port; 3005 3006 switch (port->iotype) { 3007 case UPIO_AU: 3008 case UPIO_TSI: 3009 case UPIO_MEM32: 3010 case UPIO_MEM32BE: 3011 case UPIO_MEM16: 3012 case UPIO_MEM: 3013 if (!port->mapbase) 3014 break; 3015 3016 if (port->flags & UPF_IOREMAP) { 3017 iounmap(port->membase); 3018 port->membase = NULL; 3019 } 3020 3021 release_mem_region(port->mapbase, size); 3022 break; 3023 3024 case UPIO_HUB6: 3025 case UPIO_PORT: 3026 release_region(port->iobase, size); 3027 break; 3028 } 3029 } 3030 3031 static void serial8250_release_port(struct uart_port *port) 3032 { 3033 struct uart_8250_port *up = up_to_u8250p(port); 3034 3035 serial8250_release_std_resource(up); 3036 } 3037 3038 static int serial8250_request_port(struct uart_port *port) 3039 { 3040 struct uart_8250_port *up = up_to_u8250p(port); 3041 3042 return serial8250_request_std_resource(up); 3043 } 3044 3045 static int fcr_get_rxtrig_bytes(struct uart_8250_port *up) 3046 { 3047 const struct serial8250_config *conf_type = &uart_config[up->port.type]; 3048 unsigned char bytes; 3049 3050 bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)]; 3051 3052 return bytes ? bytes : -EOPNOTSUPP; 3053 } 3054 3055 static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes) 3056 { 3057 const struct serial8250_config *conf_type = &uart_config[up->port.type]; 3058 int i; 3059 3060 if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)]) 3061 return -EOPNOTSUPP; 3062 3063 for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) { 3064 if (bytes < conf_type->rxtrig_bytes[i]) 3065 /* Use the nearest lower value */ 3066 return (--i) << UART_FCR_R_TRIG_SHIFT; 3067 } 3068 3069 return UART_FCR_R_TRIG_11; 3070 } 3071 3072 static int do_get_rxtrig(struct tty_port *port) 3073 { 3074 struct uart_state *state = container_of(port, struct uart_state, port); 3075 struct uart_port *uport = state->uart_port; 3076 struct uart_8250_port *up = up_to_u8250p(uport); 3077 3078 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1) 3079 return -EINVAL; 3080 3081 return fcr_get_rxtrig_bytes(up); 3082 } 3083 3084 static int do_serial8250_get_rxtrig(struct tty_port *port) 3085 { 3086 int rxtrig_bytes; 3087 3088 mutex_lock(&port->mutex); 3089 rxtrig_bytes = do_get_rxtrig(port); 3090 mutex_unlock(&port->mutex); 3091 3092 return rxtrig_bytes; 3093 } 3094 3095 static ssize_t rx_trig_bytes_show(struct device *dev, 3096 struct device_attribute *attr, char *buf) 3097 { 3098 struct tty_port *port = dev_get_drvdata(dev); 3099 int rxtrig_bytes; 3100 3101 rxtrig_bytes = do_serial8250_get_rxtrig(port); 3102 if (rxtrig_bytes < 0) 3103 return rxtrig_bytes; 3104 3105 return sysfs_emit(buf, "%d\n", rxtrig_bytes); 3106 } 3107 3108 static int do_set_rxtrig(struct tty_port *port, unsigned char bytes) 3109 { 3110 struct uart_state *state = container_of(port, struct uart_state, port); 3111 struct uart_port *uport = state->uart_port; 3112 struct uart_8250_port *up = up_to_u8250p(uport); 3113 int rxtrig; 3114 3115 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1) 3116 return -EINVAL; 3117 3118 rxtrig = bytes_to_fcr_rxtrig(up, bytes); 3119 if (rxtrig < 0) 3120 return rxtrig; 3121 3122 serial8250_clear_fifos(up); 3123 up->fcr &= ~UART_FCR_TRIGGER_MASK; 3124 up->fcr |= (unsigned char)rxtrig; 3125 serial_out(up, UART_FCR, up->fcr); 3126 return 0; 3127 } 3128 3129 static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes) 3130 { 3131 int ret; 3132 3133 mutex_lock(&port->mutex); 3134 ret = do_set_rxtrig(port, bytes); 3135 mutex_unlock(&port->mutex); 3136 3137 return ret; 3138 } 3139 3140 static ssize_t rx_trig_bytes_store(struct device *dev, 3141 struct device_attribute *attr, const char *buf, size_t count) 3142 { 3143 struct tty_port *port = dev_get_drvdata(dev); 3144 unsigned char bytes; 3145 int ret; 3146 3147 if (!count) 3148 return -EINVAL; 3149 3150 ret = kstrtou8(buf, 10, &bytes); 3151 if (ret < 0) 3152 return ret; 3153 3154 ret = do_serial8250_set_rxtrig(port, bytes); 3155 if (ret < 0) 3156 return ret; 3157 3158 return count; 3159 } 3160 3161 static DEVICE_ATTR_RW(rx_trig_bytes); 3162 3163 static struct attribute *serial8250_dev_attrs[] = { 3164 &dev_attr_rx_trig_bytes.attr, 3165 NULL 3166 }; 3167 3168 static struct attribute_group serial8250_dev_attr_group = { 3169 .attrs = serial8250_dev_attrs, 3170 }; 3171 3172 static void register_dev_spec_attr_grp(struct uart_8250_port *up) 3173 { 3174 const struct serial8250_config *conf_type = &uart_config[up->port.type]; 3175 3176 if (conf_type->rxtrig_bytes[0]) 3177 up->port.attr_group = &serial8250_dev_attr_group; 3178 } 3179 3180 static void serial8250_config_port(struct uart_port *port, int flags) 3181 { 3182 struct uart_8250_port *up = up_to_u8250p(port); 3183 int ret; 3184 3185 /* 3186 * Find the region that we can probe for. This in turn 3187 * tells us whether we can probe for the type of port. 3188 */ 3189 ret = serial8250_request_std_resource(up); 3190 if (ret < 0) 3191 return; 3192 3193 if (port->iotype != up->cur_iotype) 3194 set_io_from_upio(port); 3195 3196 if (flags & UART_CONFIG_TYPE) 3197 autoconfig(up); 3198 3199 /* HW bugs may trigger IRQ while IIR == NO_INT */ 3200 if (port->type == PORT_TEGRA) 3201 up->bugs |= UART_BUG_NOMSR; 3202 3203 if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ) 3204 autoconfig_irq(up); 3205 3206 if (port->type == PORT_UNKNOWN) 3207 serial8250_release_std_resource(up); 3208 3209 register_dev_spec_attr_grp(up); 3210 up->fcr = uart_config[up->port.type].fcr; 3211 } 3212 3213 static int 3214 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser) 3215 { 3216 if (ser->irq >= irq_get_nr_irqs() || ser->irq < 0 || 3217 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN || 3218 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS || 3219 ser->type == PORT_STARTECH) 3220 return -EINVAL; 3221 return 0; 3222 } 3223 3224 static const char *serial8250_type(struct uart_port *port) 3225 { 3226 int type = port->type; 3227 3228 if (type >= ARRAY_SIZE(uart_config)) 3229 type = 0; 3230 return uart_config[type].name; 3231 } 3232 3233 static const struct uart_ops serial8250_pops = { 3234 .tx_empty = serial8250_tx_empty, 3235 .set_mctrl = serial8250_set_mctrl, 3236 .get_mctrl = serial8250_get_mctrl, 3237 .stop_tx = serial8250_stop_tx, 3238 .start_tx = serial8250_start_tx, 3239 .throttle = serial8250_throttle, 3240 .unthrottle = serial8250_unthrottle, 3241 .stop_rx = serial8250_stop_rx, 3242 .enable_ms = serial8250_enable_ms, 3243 .break_ctl = serial8250_break_ctl, 3244 .startup = serial8250_startup, 3245 .shutdown = serial8250_shutdown, 3246 .flush_buffer = serial8250_flush_buffer, 3247 .set_termios = serial8250_set_termios, 3248 .set_ldisc = serial8250_set_ldisc, 3249 .pm = serial8250_pm, 3250 .type = serial8250_type, 3251 .release_port = serial8250_release_port, 3252 .request_port = serial8250_request_port, 3253 .config_port = serial8250_config_port, 3254 .verify_port = serial8250_verify_port, 3255 #ifdef CONFIG_CONSOLE_POLL 3256 .poll_get_char = serial8250_get_poll_char, 3257 .poll_put_char = serial8250_put_poll_char, 3258 #endif 3259 }; 3260 3261 void serial8250_init_port(struct uart_8250_port *up) 3262 { 3263 struct uart_port *port = &up->port; 3264 3265 spin_lock_init(&port->lock); 3266 port->ctrl_id = 0; 3267 port->pm = NULL; 3268 port->ops = &serial8250_pops; 3269 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE); 3270 3271 up->cur_iotype = UPIO_UNKNOWN; 3272 } 3273 EXPORT_SYMBOL_GPL(serial8250_init_port); 3274 3275 void serial8250_set_defaults(struct uart_8250_port *up) 3276 { 3277 struct uart_port *port = &up->port; 3278 3279 if (up->port.flags & UPF_FIXED_TYPE) { 3280 unsigned int type = up->port.type; 3281 3282 if (!up->port.fifosize) 3283 up->port.fifosize = uart_config[type].fifo_size; 3284 if (!up->tx_loadsz) 3285 up->tx_loadsz = uart_config[type].tx_loadsz; 3286 if (!up->capabilities) 3287 up->capabilities = uart_config[type].flags; 3288 } 3289 3290 set_io_from_upio(port); 3291 3292 /* default dma handlers */ 3293 if (up->dma) { 3294 if (!up->dma->tx_dma) 3295 up->dma->tx_dma = serial8250_tx_dma; 3296 if (!up->dma->rx_dma) 3297 up->dma->rx_dma = serial8250_rx_dma; 3298 } 3299 } 3300 EXPORT_SYMBOL_GPL(serial8250_set_defaults); 3301 3302 #ifdef CONFIG_SERIAL_8250_CONSOLE 3303 3304 static void serial8250_console_putchar(struct uart_port *port, unsigned char ch) 3305 { 3306 serial_port_out(port, UART_TX, ch); 3307 } 3308 3309 static void serial8250_console_wait_putchar(struct uart_port *port, unsigned char ch) 3310 { 3311 struct uart_8250_port *up = up_to_u8250p(port); 3312 3313 wait_for_xmitr(up, UART_LSR_THRE); 3314 serial8250_console_putchar(port, ch); 3315 } 3316 3317 /* 3318 * Restore serial console when h/w power-off detected 3319 */ 3320 static void serial8250_console_restore(struct uart_8250_port *up) 3321 { 3322 struct uart_port *port = &up->port; 3323 struct ktermios termios; 3324 unsigned int baud, quot, frac = 0; 3325 3326 termios.c_cflag = port->cons->cflag; 3327 termios.c_ispeed = port->cons->ispeed; 3328 termios.c_ospeed = port->cons->ospeed; 3329 if (port->state->port.tty && termios.c_cflag == 0) { 3330 termios.c_cflag = port->state->port.tty->termios.c_cflag; 3331 termios.c_ispeed = port->state->port.tty->termios.c_ispeed; 3332 termios.c_ospeed = port->state->port.tty->termios.c_ospeed; 3333 } 3334 3335 baud = serial8250_get_baud_rate(port, &termios, NULL); 3336 quot = serial8250_get_divisor(port, baud, &frac); 3337 3338 serial8250_set_divisor(port, baud, quot, frac); 3339 serial_port_out(port, UART_LCR, up->lcr); 3340 serial8250_out_MCR(up, up->mcr | UART_MCR_DTR | UART_MCR_RTS); 3341 } 3342 3343 static void fifo_wait_for_lsr(struct uart_8250_port *up, unsigned int count) 3344 { 3345 unsigned int i; 3346 3347 for (i = 0; i < count; i++) { 3348 if (wait_for_lsr(up, UART_LSR_THRE)) 3349 return; 3350 } 3351 } 3352 3353 /* 3354 * Print a string to the serial port using the device FIFO 3355 * 3356 * It sends fifosize bytes and then waits for the fifo 3357 * to get empty. 3358 */ 3359 static void serial8250_console_fifo_write(struct uart_8250_port *up, 3360 const char *s, unsigned int count) 3361 { 3362 const char *end = s + count; 3363 unsigned int fifosize = up->tx_loadsz; 3364 struct uart_port *port = &up->port; 3365 unsigned int tx_count = 0; 3366 bool cr_sent = false; 3367 unsigned int i; 3368 3369 while (s != end) { 3370 /* Allow timeout for each byte of a possibly full FIFO */ 3371 fifo_wait_for_lsr(up, fifosize); 3372 3373 for (i = 0; i < fifosize && s != end; ++i) { 3374 if (*s == '\n' && !cr_sent) { 3375 serial8250_console_putchar(port, '\r'); 3376 cr_sent = true; 3377 } else { 3378 serial8250_console_putchar(port, *s++); 3379 cr_sent = false; 3380 } 3381 } 3382 tx_count = i; 3383 } 3384 3385 /* 3386 * Allow timeout for each byte written since the caller will only wait 3387 * for UART_LSR_BOTH_EMPTY using the timeout of a single character 3388 */ 3389 fifo_wait_for_lsr(up, tx_count); 3390 } 3391 3392 /* 3393 * Print a string to the serial port trying not to disturb 3394 * any possible real use of the port... 3395 * 3396 * The console_lock must be held when we get here. 3397 * 3398 * Doing runtime PM is really a bad idea for the kernel console. 3399 * Thus, we assume the function is called when device is powered up. 3400 */ 3401 void serial8250_console_write(struct uart_8250_port *up, const char *s, 3402 unsigned int count) 3403 { 3404 struct uart_8250_em485 *em485 = up->em485; 3405 struct uart_port *port = &up->port; 3406 unsigned long flags; 3407 unsigned int ier, use_fifo; 3408 int locked = 1; 3409 3410 touch_nmi_watchdog(); 3411 3412 if (oops_in_progress) 3413 locked = uart_port_trylock_irqsave(port, &flags); 3414 else 3415 uart_port_lock_irqsave(port, &flags); 3416 3417 /* 3418 * First save the IER then disable the interrupts 3419 */ 3420 ier = serial_port_in(port, UART_IER); 3421 serial8250_clear_IER(up); 3422 3423 /* check scratch reg to see if port powered off during system sleep */ 3424 if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) { 3425 serial8250_console_restore(up); 3426 up->canary = 0; 3427 } 3428 3429 if (em485) { 3430 if (em485->tx_stopped) 3431 up->rs485_start_tx(up, false); 3432 mdelay(port->rs485.delay_rts_before_send); 3433 } 3434 3435 use_fifo = (up->capabilities & UART_CAP_FIFO) && 3436 /* 3437 * BCM283x requires to check the fifo 3438 * after each byte. 3439 */ 3440 !(up->capabilities & UART_CAP_MINI) && 3441 /* 3442 * tx_loadsz contains the transmit fifo size 3443 */ 3444 up->tx_loadsz > 1 && 3445 (up->fcr & UART_FCR_ENABLE_FIFO) && 3446 port->state && 3447 test_bit(TTY_PORT_INITIALIZED, &port->state->port.iflags) && 3448 /* 3449 * After we put a data in the fifo, the controller will send 3450 * it regardless of the CTS state. Therefore, only use fifo 3451 * if we don't use control flow. 3452 */ 3453 !(up->port.flags & UPF_CONS_FLOW); 3454 3455 if (likely(use_fifo)) 3456 serial8250_console_fifo_write(up, s, count); 3457 else 3458 uart_console_write(port, s, count, serial8250_console_wait_putchar); 3459 3460 /* 3461 * Finally, wait for transmitter to become empty 3462 * and restore the IER 3463 */ 3464 wait_for_xmitr(up, UART_LSR_BOTH_EMPTY); 3465 3466 if (em485) { 3467 mdelay(port->rs485.delay_rts_after_send); 3468 if (em485->tx_stopped) 3469 up->rs485_stop_tx(up, false); 3470 } 3471 3472 serial_port_out(port, UART_IER, ier); 3473 3474 /* 3475 * The receive handling will happen properly because the 3476 * receive ready bit will still be set; it is not cleared 3477 * on read. However, modem control will not, we must 3478 * call it if we have saved something in the saved flags 3479 * while processing with interrupts off. 3480 */ 3481 if (up->msr_saved_flags) 3482 serial8250_modem_status(up); 3483 3484 if (locked) 3485 uart_port_unlock_irqrestore(port, flags); 3486 } 3487 3488 static unsigned int probe_baud(struct uart_port *port) 3489 { 3490 unsigned char lcr, dll, dlm; 3491 unsigned int quot; 3492 3493 lcr = serial_port_in(port, UART_LCR); 3494 serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB); 3495 dll = serial_port_in(port, UART_DLL); 3496 dlm = serial_port_in(port, UART_DLM); 3497 serial_port_out(port, UART_LCR, lcr); 3498 3499 quot = (dlm << 8) | dll; 3500 return (port->uartclk / 16) / quot; 3501 } 3502 3503 int serial8250_console_setup(struct uart_port *port, char *options, bool probe) 3504 { 3505 int baud = 9600; 3506 int bits = 8; 3507 int parity = 'n'; 3508 int flow = 'n'; 3509 int ret; 3510 3511 if (!port->iobase && !port->membase) 3512 return -ENODEV; 3513 3514 if (options) 3515 uart_parse_options(options, &baud, &parity, &bits, &flow); 3516 else if (probe) 3517 baud = probe_baud(port); 3518 3519 ret = uart_set_options(port, port->cons, baud, parity, bits, flow); 3520 if (ret) 3521 return ret; 3522 3523 if (port->dev) 3524 pm_runtime_get_sync(port->dev); 3525 3526 return 0; 3527 } 3528 3529 int serial8250_console_exit(struct uart_port *port) 3530 { 3531 if (port->dev) 3532 pm_runtime_put_sync(port->dev); 3533 3534 return 0; 3535 } 3536 3537 #endif /* CONFIG_SERIAL_8250_CONSOLE */ 3538 3539 MODULE_DESCRIPTION("Base port operations for 8250/16550-type serial ports"); 3540 MODULE_LICENSE("GPL"); 3541