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