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