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