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_AR7] = { 174 .name = "AR7", 175 .fifo_size = 16, 176 .tx_loadsz = 16, 177 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00, 178 .flags = UART_CAP_FIFO /* | UART_CAP_AFE */, 179 }, 180 [PORT_U6_16550A] = { 181 .name = "U6_16550A", 182 .fifo_size = 64, 183 .tx_loadsz = 64, 184 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 185 .flags = UART_CAP_FIFO | UART_CAP_AFE, 186 }, 187 [PORT_TEGRA] = { 188 .name = "Tegra", 189 .fifo_size = 32, 190 .tx_loadsz = 8, 191 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | 192 UART_FCR_T_TRIG_01, 193 .rxtrig_bytes = {1, 4, 8, 14}, 194 .flags = UART_CAP_FIFO | UART_CAP_RTOIE, 195 }, 196 [PORT_XR17D15X] = { 197 .name = "XR17D15X", 198 .fifo_size = 64, 199 .tx_loadsz = 64, 200 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 201 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR | 202 UART_CAP_SLEEP, 203 }, 204 [PORT_XR17V35X] = { 205 .name = "XR17V35X", 206 .fifo_size = 256, 207 .tx_loadsz = 256, 208 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 | 209 UART_FCR_T_TRIG_11, 210 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR | 211 UART_CAP_SLEEP, 212 }, 213 [PORT_LPC3220] = { 214 .name = "LPC3220", 215 .fifo_size = 64, 216 .tx_loadsz = 32, 217 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO | 218 UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00, 219 .flags = UART_CAP_FIFO, 220 }, 221 [PORT_BRCM_TRUMANAGE] = { 222 .name = "TruManage", 223 .fifo_size = 1, 224 .tx_loadsz = 1024, 225 .flags = UART_CAP_HFIFO, 226 }, 227 [PORT_8250_CIR] = { 228 .name = "CIR port" 229 }, 230 [PORT_ALTR_16550_F32] = { 231 .name = "Altera 16550 FIFO32", 232 .fifo_size = 32, 233 .tx_loadsz = 32, 234 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 235 .rxtrig_bytes = {1, 8, 16, 30}, 236 .flags = UART_CAP_FIFO | UART_CAP_AFE, 237 }, 238 [PORT_ALTR_16550_F64] = { 239 .name = "Altera 16550 FIFO64", 240 .fifo_size = 64, 241 .tx_loadsz = 64, 242 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 243 .rxtrig_bytes = {1, 16, 32, 62}, 244 .flags = UART_CAP_FIFO | UART_CAP_AFE, 245 }, 246 [PORT_ALTR_16550_F128] = { 247 .name = "Altera 16550 FIFO128", 248 .fifo_size = 128, 249 .tx_loadsz = 128, 250 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 251 .rxtrig_bytes = {1, 32, 64, 126}, 252 .flags = UART_CAP_FIFO | UART_CAP_AFE, 253 }, 254 /* 255 * tx_loadsz is set to 63-bytes instead of 64-bytes to implement 256 * workaround of errata A-008006 which states that tx_loadsz should 257 * be configured less than Maximum supported fifo bytes. 258 */ 259 [PORT_16550A_FSL64] = { 260 .name = "16550A_FSL64", 261 .fifo_size = 64, 262 .tx_loadsz = 63, 263 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 | 264 UART_FCR7_64BYTE, 265 .flags = UART_CAP_FIFO | UART_CAP_NOTEMT, 266 }, 267 [PORT_RT2880] = { 268 .name = "Palmchip BK-3103", 269 .fifo_size = 16, 270 .tx_loadsz = 16, 271 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 272 .rxtrig_bytes = {1, 4, 8, 14}, 273 .flags = UART_CAP_FIFO, 274 }, 275 [PORT_DA830] = { 276 .name = "TI DA8xx/66AK2x", 277 .fifo_size = 16, 278 .tx_loadsz = 16, 279 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO | 280 UART_FCR_R_TRIG_10, 281 .rxtrig_bytes = {1, 4, 8, 14}, 282 .flags = UART_CAP_FIFO | UART_CAP_AFE, 283 }, 284 [PORT_MTK_BTIF] = { 285 .name = "MediaTek BTIF", 286 .fifo_size = 16, 287 .tx_loadsz = 16, 288 .fcr = UART_FCR_ENABLE_FIFO | 289 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT, 290 .flags = UART_CAP_FIFO, 291 }, 292 [PORT_NPCM] = { 293 .name = "Nuvoton 16550", 294 .fifo_size = 16, 295 .tx_loadsz = 16, 296 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 | 297 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT, 298 .rxtrig_bytes = {1, 4, 8, 14}, 299 .flags = UART_CAP_FIFO, 300 }, 301 [PORT_SUNIX] = { 302 .name = "Sunix", 303 .fifo_size = 128, 304 .tx_loadsz = 128, 305 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 306 .rxtrig_bytes = {1, 32, 64, 112}, 307 .flags = UART_CAP_FIFO | UART_CAP_SLEEP, 308 }, 309 [PORT_ASPEED_VUART] = { 310 .name = "ASPEED VUART", 311 .fifo_size = 16, 312 .tx_loadsz = 16, 313 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00, 314 .rxtrig_bytes = {1, 4, 8, 14}, 315 .flags = UART_CAP_FIFO, 316 }, 317 [PORT_MCHP16550A] = { 318 .name = "MCHP16550A", 319 .fifo_size = 256, 320 .tx_loadsz = 256, 321 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01, 322 .rxtrig_bytes = {2, 66, 130, 194}, 323 .flags = UART_CAP_FIFO, 324 }, 325 }; 326 327 /* Uart divisor latch read */ 328 static u32 default_serial_dl_read(struct uart_8250_port *up) 329 { 330 /* Assign these in pieces to truncate any bits above 7. */ 331 unsigned char dll = serial_in(up, UART_DLL); 332 unsigned char dlm = serial_in(up, UART_DLM); 333 334 return dll | dlm << 8; 335 } 336 337 /* Uart divisor latch write */ 338 static void default_serial_dl_write(struct uart_8250_port *up, u32 value) 339 { 340 serial_out(up, UART_DLL, value & 0xff); 341 serial_out(up, UART_DLM, value >> 8 & 0xff); 342 } 343 344 static unsigned int hub6_serial_in(struct uart_port *p, int offset) 345 { 346 offset = offset << p->regshift; 347 outb(p->hub6 - 1 + offset, p->iobase); 348 return inb(p->iobase + 1); 349 } 350 351 static void hub6_serial_out(struct uart_port *p, int offset, int value) 352 { 353 offset = offset << p->regshift; 354 outb(p->hub6 - 1 + offset, p->iobase); 355 outb(value, p->iobase + 1); 356 } 357 358 static unsigned int mem_serial_in(struct uart_port *p, int offset) 359 { 360 offset = offset << p->regshift; 361 return readb(p->membase + offset); 362 } 363 364 static void mem_serial_out(struct uart_port *p, int offset, int value) 365 { 366 offset = offset << p->regshift; 367 writeb(value, p->membase + offset); 368 } 369 370 static void mem16_serial_out(struct uart_port *p, int offset, int value) 371 { 372 offset = offset << p->regshift; 373 writew(value, p->membase + offset); 374 } 375 376 static unsigned int mem16_serial_in(struct uart_port *p, int offset) 377 { 378 offset = offset << p->regshift; 379 return readw(p->membase + offset); 380 } 381 382 static void mem32_serial_out(struct uart_port *p, int offset, int value) 383 { 384 offset = offset << p->regshift; 385 writel(value, p->membase + offset); 386 } 387 388 static unsigned int mem32_serial_in(struct uart_port *p, int offset) 389 { 390 offset = offset << p->regshift; 391 return readl(p->membase + offset); 392 } 393 394 static void mem32be_serial_out(struct uart_port *p, int offset, int value) 395 { 396 offset = offset << p->regshift; 397 iowrite32be(value, p->membase + offset); 398 } 399 400 static unsigned int mem32be_serial_in(struct uart_port *p, int offset) 401 { 402 offset = offset << p->regshift; 403 return ioread32be(p->membase + offset); 404 } 405 406 static unsigned int io_serial_in(struct uart_port *p, int offset) 407 { 408 offset = offset << p->regshift; 409 return inb(p->iobase + offset); 410 } 411 412 static void io_serial_out(struct uart_port *p, int offset, int value) 413 { 414 offset = offset << p->regshift; 415 outb(value, p->iobase + offset); 416 } 417 418 static int serial8250_default_handle_irq(struct uart_port *port); 419 420 static void set_io_from_upio(struct uart_port *p) 421 { 422 struct uart_8250_port *up = up_to_u8250p(p); 423 424 up->dl_read = default_serial_dl_read; 425 up->dl_write = default_serial_dl_write; 426 427 switch (p->iotype) { 428 case UPIO_HUB6: 429 p->serial_in = hub6_serial_in; 430 p->serial_out = hub6_serial_out; 431 break; 432 433 case UPIO_MEM: 434 p->serial_in = mem_serial_in; 435 p->serial_out = mem_serial_out; 436 break; 437 438 case UPIO_MEM16: 439 p->serial_in = mem16_serial_in; 440 p->serial_out = mem16_serial_out; 441 break; 442 443 case UPIO_MEM32: 444 p->serial_in = mem32_serial_in; 445 p->serial_out = mem32_serial_out; 446 break; 447 448 case UPIO_MEM32BE: 449 p->serial_in = mem32be_serial_in; 450 p->serial_out = mem32be_serial_out; 451 break; 452 453 default: 454 p->serial_in = io_serial_in; 455 p->serial_out = io_serial_out; 456 break; 457 } 458 /* Remember loaded iotype */ 459 up->cur_iotype = p->iotype; 460 p->handle_irq = serial8250_default_handle_irq; 461 } 462 463 static void 464 serial_port_out_sync(struct uart_port *p, int offset, int value) 465 { 466 switch (p->iotype) { 467 case UPIO_MEM: 468 case UPIO_MEM16: 469 case UPIO_MEM32: 470 case UPIO_MEM32BE: 471 case UPIO_AU: 472 p->serial_out(p, offset, value); 473 p->serial_in(p, UART_LCR); /* safe, no side-effects */ 474 break; 475 default: 476 p->serial_out(p, offset, value); 477 } 478 } 479 480 /* 481 * FIFO support. 482 */ 483 static void serial8250_clear_fifos(struct uart_8250_port *p) 484 { 485 if (p->capabilities & UART_CAP_FIFO) { 486 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO); 487 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO | 488 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT); 489 serial_out(p, UART_FCR, 0); 490 } 491 } 492 493 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t); 494 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t); 495 496 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p) 497 { 498 serial8250_clear_fifos(p); 499 serial_out(p, UART_FCR, p->fcr); 500 } 501 EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos); 502 503 void serial8250_rpm_get(struct uart_8250_port *p) 504 { 505 if (!(p->capabilities & UART_CAP_RPM)) 506 return; 507 pm_runtime_get_sync(p->port.dev); 508 } 509 EXPORT_SYMBOL_GPL(serial8250_rpm_get); 510 511 void serial8250_rpm_put(struct uart_8250_port *p) 512 { 513 if (!(p->capabilities & UART_CAP_RPM)) 514 return; 515 pm_runtime_mark_last_busy(p->port.dev); 516 pm_runtime_put_autosuspend(p->port.dev); 517 } 518 EXPORT_SYMBOL_GPL(serial8250_rpm_put); 519 520 /** 521 * serial8250_em485_init() - put uart_8250_port into rs485 emulating 522 * @p: uart_8250_port port instance 523 * 524 * The function is used to start rs485 software emulating on the 525 * &struct uart_8250_port* @p. Namely, RTS is switched before/after 526 * transmission. The function is idempotent, so it is safe to call it 527 * multiple times. 528 * 529 * The caller MUST enable interrupt on empty shift register before 530 * calling serial8250_em485_init(). This interrupt is not a part of 531 * 8250 standard, but implementation defined. 532 * 533 * The function is supposed to be called from .rs485_config callback 534 * or from any other callback protected with p->port.lock spinlock. 535 * 536 * See also serial8250_em485_destroy() 537 * 538 * Return 0 - success, -errno - otherwise 539 */ 540 static int serial8250_em485_init(struct uart_8250_port *p) 541 { 542 /* Port locked to synchronize UART_IER access against the console. */ 543 lockdep_assert_held_once(&p->port.lock); 544 545 if (p->em485) 546 goto deassert_rts; 547 548 p->em485 = kmalloc(sizeof(struct uart_8250_em485), GFP_ATOMIC); 549 if (!p->em485) 550 return -ENOMEM; 551 552 hrtimer_init(&p->em485->stop_tx_timer, CLOCK_MONOTONIC, 553 HRTIMER_MODE_REL); 554 hrtimer_init(&p->em485->start_tx_timer, CLOCK_MONOTONIC, 555 HRTIMER_MODE_REL); 556 p->em485->stop_tx_timer.function = &serial8250_em485_handle_stop_tx; 557 p->em485->start_tx_timer.function = &serial8250_em485_handle_start_tx; 558 p->em485->port = p; 559 p->em485->active_timer = NULL; 560 p->em485->tx_stopped = true; 561 562 deassert_rts: 563 if (p->em485->tx_stopped) 564 p->rs485_stop_tx(p); 565 566 return 0; 567 } 568 569 /** 570 * serial8250_em485_destroy() - put uart_8250_port into normal state 571 * @p: uart_8250_port port instance 572 * 573 * The function is used to stop rs485 software emulating on the 574 * &struct uart_8250_port* @p. The function is idempotent, so it is safe to 575 * call it multiple times. 576 * 577 * The function is supposed to be called from .rs485_config callback 578 * or from any other callback protected with p->port.lock spinlock. 579 * 580 * See also serial8250_em485_init() 581 */ 582 void serial8250_em485_destroy(struct uart_8250_port *p) 583 { 584 if (!p->em485) 585 return; 586 587 hrtimer_cancel(&p->em485->start_tx_timer); 588 hrtimer_cancel(&p->em485->stop_tx_timer); 589 590 kfree(p->em485); 591 p->em485 = NULL; 592 } 593 EXPORT_SYMBOL_GPL(serial8250_em485_destroy); 594 595 struct serial_rs485 serial8250_em485_supported = { 596 .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND | 597 SER_RS485_TERMINATE_BUS | SER_RS485_RX_DURING_TX, 598 .delay_rts_before_send = 1, 599 .delay_rts_after_send = 1, 600 }; 601 EXPORT_SYMBOL_GPL(serial8250_em485_supported); 602 603 /** 604 * serial8250_em485_config() - generic ->rs485_config() callback 605 * @port: uart port 606 * @termios: termios structure 607 * @rs485: rs485 settings 608 * 609 * Generic callback usable by 8250 uart drivers to activate rs485 settings 610 * if the uart is incapable of driving RTS as a Transmit Enable signal in 611 * hardware, relying on software emulation instead. 612 */ 613 int serial8250_em485_config(struct uart_port *port, struct ktermios *termios, 614 struct serial_rs485 *rs485) 615 { 616 struct uart_8250_port *up = up_to_u8250p(port); 617 618 /* pick sane settings if the user hasn't */ 619 if (!!(rs485->flags & SER_RS485_RTS_ON_SEND) == 620 !!(rs485->flags & SER_RS485_RTS_AFTER_SEND)) { 621 rs485->flags |= SER_RS485_RTS_ON_SEND; 622 rs485->flags &= ~SER_RS485_RTS_AFTER_SEND; 623 } 624 625 /* 626 * Both serial8250_em485_init() and serial8250_em485_destroy() 627 * are idempotent. 628 */ 629 if (rs485->flags & SER_RS485_ENABLED) 630 return serial8250_em485_init(up); 631 632 serial8250_em485_destroy(up); 633 return 0; 634 } 635 EXPORT_SYMBOL_GPL(serial8250_em485_config); 636 637 /* 638 * These two wrappers ensure that enable_runtime_pm_tx() can be called more than 639 * once and disable_runtime_pm_tx() will still disable RPM because the fifo is 640 * empty and the HW can idle again. 641 */ 642 void serial8250_rpm_get_tx(struct uart_8250_port *p) 643 { 644 unsigned char rpm_active; 645 646 if (!(p->capabilities & UART_CAP_RPM)) 647 return; 648 649 rpm_active = xchg(&p->rpm_tx_active, 1); 650 if (rpm_active) 651 return; 652 pm_runtime_get_sync(p->port.dev); 653 } 654 EXPORT_SYMBOL_GPL(serial8250_rpm_get_tx); 655 656 void serial8250_rpm_put_tx(struct uart_8250_port *p) 657 { 658 unsigned char rpm_active; 659 660 if (!(p->capabilities & UART_CAP_RPM)) 661 return; 662 663 rpm_active = xchg(&p->rpm_tx_active, 0); 664 if (!rpm_active) 665 return; 666 pm_runtime_mark_last_busy(p->port.dev); 667 pm_runtime_put_autosuspend(p->port.dev); 668 } 669 EXPORT_SYMBOL_GPL(serial8250_rpm_put_tx); 670 671 /* 672 * IER sleep support. UARTs which have EFRs need the "extended 673 * capability" bit enabled. Note that on XR16C850s, we need to 674 * reset LCR to write to IER. 675 */ 676 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep) 677 { 678 unsigned char lcr = 0, efr = 0; 679 680 serial8250_rpm_get(p); 681 682 if (p->capabilities & UART_CAP_SLEEP) { 683 /* Synchronize UART_IER access against the console. */ 684 spin_lock_irq(&p->port.lock); 685 if (p->capabilities & UART_CAP_EFR) { 686 lcr = serial_in(p, UART_LCR); 687 efr = serial_in(p, UART_EFR); 688 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B); 689 serial_out(p, UART_EFR, UART_EFR_ECB); 690 serial_out(p, UART_LCR, 0); 691 } 692 serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0); 693 if (p->capabilities & UART_CAP_EFR) { 694 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B); 695 serial_out(p, UART_EFR, efr); 696 serial_out(p, UART_LCR, lcr); 697 } 698 spin_unlock_irq(&p->port.lock); 699 } 700 701 serial8250_rpm_put(p); 702 } 703 704 static void serial8250_clear_IER(struct uart_8250_port *up) 705 { 706 /* Port locked to synchronize UART_IER access against the console. */ 707 lockdep_assert_held_once(&up->port.lock); 708 709 if (up->capabilities & UART_CAP_UUE) 710 serial_out(up, UART_IER, UART_IER_UUE); 711 else 712 serial_out(up, UART_IER, 0); 713 } 714 715 #ifdef CONFIG_SERIAL_8250_RSA 716 /* 717 * Attempts to turn on the RSA FIFO. Returns zero on failure. 718 * We set the port uart clock rate if we succeed. 719 */ 720 static int __enable_rsa(struct uart_8250_port *up) 721 { 722 unsigned char mode; 723 int result; 724 725 mode = serial_in(up, UART_RSA_MSR); 726 result = mode & UART_RSA_MSR_FIFO; 727 728 if (!result) { 729 serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO); 730 mode = serial_in(up, UART_RSA_MSR); 731 result = mode & UART_RSA_MSR_FIFO; 732 } 733 734 if (result) 735 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16; 736 737 return result; 738 } 739 740 static void enable_rsa(struct uart_8250_port *up) 741 { 742 if (up->port.type == PORT_RSA) { 743 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) { 744 spin_lock_irq(&up->port.lock); 745 __enable_rsa(up); 746 spin_unlock_irq(&up->port.lock); 747 } 748 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) 749 serial_out(up, UART_RSA_FRR, 0); 750 } 751 } 752 753 /* 754 * Attempts to turn off the RSA FIFO. Returns zero on failure. 755 * It is unknown why interrupts were disabled in here. However, 756 * the caller is expected to preserve this behaviour by grabbing 757 * the spinlock before calling this function. 758 */ 759 static void disable_rsa(struct uart_8250_port *up) 760 { 761 unsigned char mode; 762 int result; 763 764 if (up->port.type == PORT_RSA && 765 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) { 766 spin_lock_irq(&up->port.lock); 767 768 mode = serial_in(up, UART_RSA_MSR); 769 result = !(mode & UART_RSA_MSR_FIFO); 770 771 if (!result) { 772 serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO); 773 mode = serial_in(up, UART_RSA_MSR); 774 result = !(mode & UART_RSA_MSR_FIFO); 775 } 776 777 if (result) 778 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16; 779 spin_unlock_irq(&up->port.lock); 780 } 781 } 782 #endif /* CONFIG_SERIAL_8250_RSA */ 783 784 /* 785 * This is a quickie test to see how big the FIFO is. 786 * It doesn't work at all the time, more's the pity. 787 */ 788 static int size_fifo(struct uart_8250_port *up) 789 { 790 unsigned char old_fcr, old_mcr, old_lcr; 791 u32 old_dl; 792 int count; 793 794 old_lcr = serial_in(up, UART_LCR); 795 serial_out(up, UART_LCR, 0); 796 old_fcr = serial_in(up, UART_FCR); 797 old_mcr = serial8250_in_MCR(up); 798 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | 799 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT); 800 serial8250_out_MCR(up, UART_MCR_LOOP); 801 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A); 802 old_dl = serial_dl_read(up); 803 serial_dl_write(up, 0x0001); 804 serial_out(up, UART_LCR, UART_LCR_WLEN8); 805 for (count = 0; count < 256; count++) 806 serial_out(up, UART_TX, count); 807 mdelay(20);/* FIXME - schedule_timeout */ 808 for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) && 809 (count < 256); count++) 810 serial_in(up, UART_RX); 811 serial_out(up, UART_FCR, old_fcr); 812 serial8250_out_MCR(up, old_mcr); 813 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A); 814 serial_dl_write(up, old_dl); 815 serial_out(up, UART_LCR, old_lcr); 816 817 return count; 818 } 819 820 /* 821 * Read UART ID using the divisor method - set DLL and DLM to zero 822 * and the revision will be in DLL and device type in DLM. We 823 * preserve the device state across this. 824 */ 825 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p) 826 { 827 unsigned char old_lcr; 828 unsigned int id, old_dl; 829 830 old_lcr = serial_in(p, UART_LCR); 831 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A); 832 old_dl = serial_dl_read(p); 833 serial_dl_write(p, 0); 834 id = serial_dl_read(p); 835 serial_dl_write(p, old_dl); 836 837 serial_out(p, UART_LCR, old_lcr); 838 839 return id; 840 } 841 842 /* 843 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's. 844 * When this function is called we know it is at least a StarTech 845 * 16650 V2, but it might be one of several StarTech UARTs, or one of 846 * its clones. (We treat the broken original StarTech 16650 V1 as a 847 * 16550, and why not? Startech doesn't seem to even acknowledge its 848 * existence.) 849 * 850 * What evil have men's minds wrought... 851 */ 852 static void autoconfig_has_efr(struct uart_8250_port *up) 853 { 854 unsigned int id1, id2, id3, rev; 855 856 /* 857 * Everything with an EFR has SLEEP 858 */ 859 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP; 860 861 /* 862 * First we check to see if it's an Oxford Semiconductor UART. 863 * 864 * If we have to do this here because some non-National 865 * Semiconductor clone chips lock up if you try writing to the 866 * LSR register (which serial_icr_read does) 867 */ 868 869 /* 870 * Check for Oxford Semiconductor 16C950. 871 * 872 * EFR [4] must be set else this test fails. 873 * 874 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca) 875 * claims that it's needed for 952 dual UART's (which are not 876 * recommended for new designs). 877 */ 878 up->acr = 0; 879 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); 880 serial_out(up, UART_EFR, UART_EFR_ECB); 881 serial_out(up, UART_LCR, 0x00); 882 id1 = serial_icr_read(up, UART_ID1); 883 id2 = serial_icr_read(up, UART_ID2); 884 id3 = serial_icr_read(up, UART_ID3); 885 rev = serial_icr_read(up, UART_REV); 886 887 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev); 888 889 if (id1 == 0x16 && id2 == 0xC9 && 890 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) { 891 up->port.type = PORT_16C950; 892 893 /* 894 * Enable work around for the Oxford Semiconductor 952 rev B 895 * chip which causes it to seriously miscalculate baud rates 896 * when DLL is 0. 897 */ 898 if (id3 == 0x52 && rev == 0x01) 899 up->bugs |= UART_BUG_QUOT; 900 return; 901 } 902 903 /* 904 * We check for a XR16C850 by setting DLL and DLM to 0, and then 905 * reading back DLL and DLM. The chip type depends on the DLM 906 * value read back: 907 * 0x10 - XR16C850 and the DLL contains the chip revision. 908 * 0x12 - XR16C2850. 909 * 0x14 - XR16C854. 910 */ 911 id1 = autoconfig_read_divisor_id(up); 912 DEBUG_AUTOCONF("850id=%04x ", id1); 913 914 id2 = id1 >> 8; 915 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) { 916 up->port.type = PORT_16850; 917 return; 918 } 919 920 /* 921 * It wasn't an XR16C850. 922 * 923 * We distinguish between the '654 and the '650 by counting 924 * how many bytes are in the FIFO. I'm using this for now, 925 * since that's the technique that was sent to me in the 926 * serial driver update, but I'm not convinced this works. 927 * I've had problems doing this in the past. -TYT 928 */ 929 if (size_fifo(up) == 64) 930 up->port.type = PORT_16654; 931 else 932 up->port.type = PORT_16650V2; 933 } 934 935 /* 936 * We detected a chip without a FIFO. Only two fall into 937 * this category - the original 8250 and the 16450. The 938 * 16450 has a scratch register (accessible with LCR=0) 939 */ 940 static void autoconfig_8250(struct uart_8250_port *up) 941 { 942 unsigned char scratch, status1, status2; 943 944 up->port.type = PORT_8250; 945 946 scratch = serial_in(up, UART_SCR); 947 serial_out(up, UART_SCR, 0xa5); 948 status1 = serial_in(up, UART_SCR); 949 serial_out(up, UART_SCR, 0x5a); 950 status2 = serial_in(up, UART_SCR); 951 serial_out(up, UART_SCR, scratch); 952 953 if (status1 == 0xa5 && status2 == 0x5a) 954 up->port.type = PORT_16450; 955 } 956 957 static int broken_efr(struct uart_8250_port *up) 958 { 959 /* 960 * Exar ST16C2550 "A2" devices incorrectly detect as 961 * having an EFR, and report an ID of 0x0201. See 962 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html 963 */ 964 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16) 965 return 1; 966 967 return 0; 968 } 969 970 /* 971 * We know that the chip has FIFOs. Does it have an EFR? The 972 * EFR is located in the same register position as the IIR and 973 * we know the top two bits of the IIR are currently set. The 974 * EFR should contain zero. Try to read the EFR. 975 */ 976 static void autoconfig_16550a(struct uart_8250_port *up) 977 { 978 unsigned char status1, status2; 979 unsigned int iersave; 980 981 /* Port locked to synchronize UART_IER access against the console. */ 982 lockdep_assert_held_once(&up->port.lock); 983 984 up->port.type = PORT_16550A; 985 up->capabilities |= UART_CAP_FIFO; 986 987 if (!IS_ENABLED(CONFIG_SERIAL_8250_16550A_VARIANTS) && 988 !(up->port.flags & UPF_FULL_PROBE)) 989 return; 990 991 /* 992 * Check for presence of the EFR when DLAB is set. 993 * Only ST16C650V1 UARTs pass this test. 994 */ 995 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A); 996 if (serial_in(up, UART_EFR) == 0) { 997 serial_out(up, UART_EFR, 0xA8); 998 if (serial_in(up, UART_EFR) != 0) { 999 DEBUG_AUTOCONF("EFRv1 "); 1000 up->port.type = PORT_16650; 1001 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP; 1002 } else { 1003 serial_out(up, UART_LCR, 0); 1004 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | 1005 UART_FCR7_64BYTE); 1006 status1 = serial_in(up, UART_IIR) & (UART_IIR_64BYTE_FIFO | 1007 UART_IIR_FIFO_ENABLED); 1008 serial_out(up, UART_FCR, 0); 1009 serial_out(up, UART_LCR, 0); 1010 1011 if (status1 == (UART_IIR_64BYTE_FIFO | UART_IIR_FIFO_ENABLED)) 1012 up->port.type = PORT_16550A_FSL64; 1013 else 1014 DEBUG_AUTOCONF("Motorola 8xxx DUART "); 1015 } 1016 serial_out(up, UART_EFR, 0); 1017 return; 1018 } 1019 1020 /* 1021 * Maybe it requires 0xbf to be written to the LCR. 1022 * (other ST16C650V2 UARTs, TI16C752A, etc) 1023 */ 1024 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); 1025 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) { 1026 DEBUG_AUTOCONF("EFRv2 "); 1027 autoconfig_has_efr(up); 1028 return; 1029 } 1030 1031 /* 1032 * Check for a National Semiconductor SuperIO chip. 1033 * Attempt to switch to bank 2, read the value of the LOOP bit 1034 * from EXCR1. Switch back to bank 0, change it in MCR. Then 1035 * switch back to bank 2, read it from EXCR1 again and check 1036 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2 1037 */ 1038 serial_out(up, UART_LCR, 0); 1039 status1 = serial8250_in_MCR(up); 1040 serial_out(up, UART_LCR, 0xE0); 1041 status2 = serial_in(up, 0x02); /* EXCR1 */ 1042 1043 if (!((status2 ^ status1) & UART_MCR_LOOP)) { 1044 serial_out(up, UART_LCR, 0); 1045 serial8250_out_MCR(up, status1 ^ UART_MCR_LOOP); 1046 serial_out(up, UART_LCR, 0xE0); 1047 status2 = serial_in(up, 0x02); /* EXCR1 */ 1048 serial_out(up, UART_LCR, 0); 1049 serial8250_out_MCR(up, status1); 1050 1051 if ((status2 ^ status1) & UART_MCR_LOOP) { 1052 unsigned short quot; 1053 1054 serial_out(up, UART_LCR, 0xE0); 1055 1056 quot = serial_dl_read(up); 1057 quot <<= 3; 1058 1059 if (ns16550a_goto_highspeed(up)) 1060 serial_dl_write(up, quot); 1061 1062 serial_out(up, UART_LCR, 0); 1063 1064 up->port.uartclk = 921600*16; 1065 up->port.type = PORT_NS16550A; 1066 up->capabilities |= UART_NATSEMI; 1067 return; 1068 } 1069 } 1070 1071 /* 1072 * No EFR. Try to detect a TI16750, which only sets bit 5 of 1073 * the IIR when 64 byte FIFO mode is enabled when DLAB is set. 1074 * Try setting it with and without DLAB set. Cheap clones 1075 * set bit 5 without DLAB set. 1076 */ 1077 serial_out(up, UART_LCR, 0); 1078 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE); 1079 status1 = serial_in(up, UART_IIR) & (UART_IIR_64BYTE_FIFO | UART_IIR_FIFO_ENABLED); 1080 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO); 1081 1082 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A); 1083 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE); 1084 status2 = serial_in(up, UART_IIR) & (UART_IIR_64BYTE_FIFO | UART_IIR_FIFO_ENABLED); 1085 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO); 1086 1087 serial_out(up, UART_LCR, 0); 1088 1089 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2); 1090 1091 if (status1 == UART_IIR_FIFO_ENABLED_16550A && 1092 status2 == (UART_IIR_64BYTE_FIFO | UART_IIR_FIFO_ENABLED_16550A)) { 1093 up->port.type = PORT_16750; 1094 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP; 1095 return; 1096 } 1097 1098 /* 1099 * Try writing and reading the UART_IER_UUE bit (b6). 1100 * If it works, this is probably one of the Xscale platform's 1101 * internal UARTs. 1102 * We're going to explicitly set the UUE bit to 0 before 1103 * trying to write and read a 1 just to make sure it's not 1104 * already a 1 and maybe locked there before we even start. 1105 */ 1106 iersave = serial_in(up, UART_IER); 1107 serial_out(up, UART_IER, iersave & ~UART_IER_UUE); 1108 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) { 1109 /* 1110 * OK it's in a known zero state, try writing and reading 1111 * without disturbing the current state of the other bits. 1112 */ 1113 serial_out(up, UART_IER, iersave | UART_IER_UUE); 1114 if (serial_in(up, UART_IER) & UART_IER_UUE) { 1115 /* 1116 * It's an Xscale. 1117 * We'll leave the UART_IER_UUE bit set to 1 (enabled). 1118 */ 1119 DEBUG_AUTOCONF("Xscale "); 1120 up->port.type = PORT_XSCALE; 1121 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE; 1122 return; 1123 } 1124 } else { 1125 /* 1126 * If we got here we couldn't force the IER_UUE bit to 0. 1127 * Log it and continue. 1128 */ 1129 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 "); 1130 } 1131 serial_out(up, UART_IER, iersave); 1132 1133 /* 1134 * We distinguish between 16550A and U6 16550A by counting 1135 * how many bytes are in the FIFO. 1136 */ 1137 if (up->port.type == PORT_16550A && size_fifo(up) == 64) { 1138 up->port.type = PORT_U6_16550A; 1139 up->capabilities |= UART_CAP_AFE; 1140 } 1141 } 1142 1143 /* 1144 * This routine is called by rs_init() to initialize a specific serial 1145 * port. It determines what type of UART chip this serial port is 1146 * using: 8250, 16450, 16550, 16550A. The important question is 1147 * whether or not this UART is a 16550A or not, since this will 1148 * determine whether or not we can use its FIFO features or not. 1149 */ 1150 static void autoconfig(struct uart_8250_port *up) 1151 { 1152 unsigned char status1, scratch, scratch2, scratch3; 1153 unsigned char save_lcr, save_mcr; 1154 struct uart_port *port = &up->port; 1155 unsigned long flags; 1156 unsigned int old_capabilities; 1157 1158 if (!port->iobase && !port->mapbase && !port->membase) 1159 return; 1160 1161 DEBUG_AUTOCONF("%s: autoconf (0x%04lx, 0x%p): ", 1162 port->name, port->iobase, port->membase); 1163 1164 /* 1165 * We really do need global IRQs disabled here - we're going to 1166 * be frobbing the chips IRQ enable register to see if it exists. 1167 * 1168 * Synchronize UART_IER access against the console. 1169 */ 1170 spin_lock_irqsave(&port->lock, flags); 1171 1172 up->capabilities = 0; 1173 up->bugs = 0; 1174 1175 if (!(port->flags & UPF_BUGGY_UART)) { 1176 /* 1177 * Do a simple existence test first; if we fail this, 1178 * there's no point trying anything else. 1179 * 1180 * 0x80 is used as a nonsense port to prevent against 1181 * false positives due to ISA bus float. The 1182 * assumption is that 0x80 is a non-existent port; 1183 * which should be safe since include/asm/io.h also 1184 * makes this assumption. 1185 * 1186 * Note: this is safe as long as MCR bit 4 is clear 1187 * and the device is in "PC" mode. 1188 */ 1189 scratch = serial_in(up, UART_IER); 1190 serial_out(up, UART_IER, 0); 1191 #ifdef __i386__ 1192 outb(0xff, 0x080); 1193 #endif 1194 /* 1195 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL 1196 * 16C754B) allow only to modify them if an EFR bit is set. 1197 */ 1198 scratch2 = serial_in(up, UART_IER) & UART_IER_ALL_INTR; 1199 serial_out(up, UART_IER, UART_IER_ALL_INTR); 1200 #ifdef __i386__ 1201 outb(0, 0x080); 1202 #endif 1203 scratch3 = serial_in(up, UART_IER) & UART_IER_ALL_INTR; 1204 serial_out(up, UART_IER, scratch); 1205 if (scratch2 != 0 || scratch3 != UART_IER_ALL_INTR) { 1206 /* 1207 * We failed; there's nothing here 1208 */ 1209 spin_unlock_irqrestore(&port->lock, flags); 1210 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ", 1211 scratch2, scratch3); 1212 goto out; 1213 } 1214 } 1215 1216 save_mcr = serial8250_in_MCR(up); 1217 save_lcr = serial_in(up, UART_LCR); 1218 1219 /* 1220 * Check to see if a UART is really there. Certain broken 1221 * internal modems based on the Rockwell chipset fail this 1222 * test, because they apparently don't implement the loopback 1223 * test mode. So this test is skipped on the COM 1 through 1224 * COM 4 ports. This *should* be safe, since no board 1225 * manufacturer would be stupid enough to design a board 1226 * that conflicts with COM 1-4 --- we hope! 1227 */ 1228 if (!(port->flags & UPF_SKIP_TEST)) { 1229 serial8250_out_MCR(up, UART_MCR_LOOP | UART_MCR_OUT2 | UART_MCR_RTS); 1230 status1 = serial_in(up, UART_MSR) & UART_MSR_STATUS_BITS; 1231 serial8250_out_MCR(up, save_mcr); 1232 if (status1 != (UART_MSR_DCD | UART_MSR_CTS)) { 1233 spin_unlock_irqrestore(&port->lock, flags); 1234 DEBUG_AUTOCONF("LOOP test failed (%02x) ", 1235 status1); 1236 goto out; 1237 } 1238 } 1239 1240 /* 1241 * We're pretty sure there's a port here. Lets find out what 1242 * type of port it is. The IIR top two bits allows us to find 1243 * out if it's 8250 or 16450, 16550, 16550A or later. This 1244 * determines what we test for next. 1245 * 1246 * We also initialise the EFR (if any) to zero for later. The 1247 * EFR occupies the same register location as the FCR and IIR. 1248 */ 1249 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); 1250 serial_out(up, UART_EFR, 0); 1251 serial_out(up, UART_LCR, 0); 1252 1253 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO); 1254 1255 switch (serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED) { 1256 case UART_IIR_FIFO_ENABLED_8250: 1257 autoconfig_8250(up); 1258 break; 1259 case UART_IIR_FIFO_ENABLED_16550: 1260 port->type = PORT_16550; 1261 break; 1262 case UART_IIR_FIFO_ENABLED_16550A: 1263 autoconfig_16550a(up); 1264 break; 1265 default: 1266 port->type = PORT_UNKNOWN; 1267 break; 1268 } 1269 1270 #ifdef CONFIG_SERIAL_8250_RSA 1271 /* 1272 * Only probe for RSA ports if we got the region. 1273 */ 1274 if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA && 1275 __enable_rsa(up)) 1276 port->type = PORT_RSA; 1277 #endif 1278 1279 serial_out(up, UART_LCR, save_lcr); 1280 1281 port->fifosize = uart_config[up->port.type].fifo_size; 1282 old_capabilities = up->capabilities; 1283 up->capabilities = uart_config[port->type].flags; 1284 up->tx_loadsz = uart_config[port->type].tx_loadsz; 1285 1286 if (port->type == PORT_UNKNOWN) 1287 goto out_unlock; 1288 1289 /* 1290 * Reset the UART. 1291 */ 1292 #ifdef CONFIG_SERIAL_8250_RSA 1293 if (port->type == PORT_RSA) 1294 serial_out(up, UART_RSA_FRR, 0); 1295 #endif 1296 serial8250_out_MCR(up, save_mcr); 1297 serial8250_clear_fifos(up); 1298 serial_in(up, UART_RX); 1299 serial8250_clear_IER(up); 1300 1301 out_unlock: 1302 spin_unlock_irqrestore(&port->lock, flags); 1303 1304 /* 1305 * Check if the device is a Fintek F81216A 1306 */ 1307 if (port->type == PORT_16550A && port->iotype == UPIO_PORT) 1308 fintek_8250_probe(up); 1309 1310 if (up->capabilities != old_capabilities) { 1311 dev_warn(port->dev, "detected caps %08x should be %08x\n", 1312 old_capabilities, up->capabilities); 1313 } 1314 out: 1315 DEBUG_AUTOCONF("iir=%d ", scratch); 1316 DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name); 1317 } 1318 1319 static void autoconfig_irq(struct uart_8250_port *up) 1320 { 1321 struct uart_port *port = &up->port; 1322 unsigned char save_mcr, save_ier; 1323 unsigned char save_ICP = 0; 1324 unsigned int ICP = 0; 1325 unsigned long irqs; 1326 int irq; 1327 1328 if (port->flags & UPF_FOURPORT) { 1329 ICP = (port->iobase & 0xfe0) | 0x1f; 1330 save_ICP = inb_p(ICP); 1331 outb_p(0x80, ICP); 1332 inb_p(ICP); 1333 } 1334 1335 if (uart_console(port)) 1336 console_lock(); 1337 1338 /* forget possible initially masked and pending IRQ */ 1339 probe_irq_off(probe_irq_on()); 1340 save_mcr = serial8250_in_MCR(up); 1341 /* Synchronize UART_IER access against the console. */ 1342 spin_lock_irq(&port->lock); 1343 save_ier = serial_in(up, UART_IER); 1344 spin_unlock_irq(&port->lock); 1345 serial8250_out_MCR(up, UART_MCR_OUT1 | UART_MCR_OUT2); 1346 1347 irqs = probe_irq_on(); 1348 serial8250_out_MCR(up, 0); 1349 udelay(10); 1350 if (port->flags & UPF_FOURPORT) { 1351 serial8250_out_MCR(up, UART_MCR_DTR | UART_MCR_RTS); 1352 } else { 1353 serial8250_out_MCR(up, 1354 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2); 1355 } 1356 /* Synchronize UART_IER access against the console. */ 1357 spin_lock_irq(&port->lock); 1358 serial_out(up, UART_IER, UART_IER_ALL_INTR); 1359 spin_unlock_irq(&port->lock); 1360 serial_in(up, UART_LSR); 1361 serial_in(up, UART_RX); 1362 serial_in(up, UART_IIR); 1363 serial_in(up, UART_MSR); 1364 serial_out(up, UART_TX, 0xFF); 1365 udelay(20); 1366 irq = probe_irq_off(irqs); 1367 1368 serial8250_out_MCR(up, save_mcr); 1369 /* Synchronize UART_IER access against the console. */ 1370 spin_lock_irq(&port->lock); 1371 serial_out(up, UART_IER, save_ier); 1372 spin_unlock_irq(&port->lock); 1373 1374 if (port->flags & UPF_FOURPORT) 1375 outb_p(save_ICP, ICP); 1376 1377 if (uart_console(port)) 1378 console_unlock(); 1379 1380 port->irq = (irq > 0) ? irq : 0; 1381 } 1382 1383 static void serial8250_stop_rx(struct uart_port *port) 1384 { 1385 struct uart_8250_port *up = up_to_u8250p(port); 1386 1387 /* Port locked to synchronize UART_IER access against the console. */ 1388 lockdep_assert_held_once(&port->lock); 1389 1390 serial8250_rpm_get(up); 1391 1392 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI); 1393 up->port.read_status_mask &= ~UART_LSR_DR; 1394 serial_port_out(port, UART_IER, up->ier); 1395 1396 serial8250_rpm_put(up); 1397 } 1398 1399 /** 1400 * serial8250_em485_stop_tx() - generic ->rs485_stop_tx() callback 1401 * @p: uart 8250 port 1402 * 1403 * Generic callback usable by 8250 uart drivers to stop rs485 transmission. 1404 */ 1405 void serial8250_em485_stop_tx(struct uart_8250_port *p) 1406 { 1407 unsigned char mcr = serial8250_in_MCR(p); 1408 1409 /* Port locked to synchronize UART_IER access against the console. */ 1410 lockdep_assert_held_once(&p->port.lock); 1411 1412 if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND) 1413 mcr |= UART_MCR_RTS; 1414 else 1415 mcr &= ~UART_MCR_RTS; 1416 serial8250_out_MCR(p, mcr); 1417 1418 /* 1419 * Empty the RX FIFO, we are not interested in anything 1420 * received during the half-duplex transmission. 1421 * Enable previously disabled RX interrupts. 1422 */ 1423 if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX)) { 1424 serial8250_clear_and_reinit_fifos(p); 1425 1426 p->ier |= UART_IER_RLSI | UART_IER_RDI; 1427 serial_port_out(&p->port, UART_IER, p->ier); 1428 } 1429 } 1430 EXPORT_SYMBOL_GPL(serial8250_em485_stop_tx); 1431 1432 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t) 1433 { 1434 struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485, 1435 stop_tx_timer); 1436 struct uart_8250_port *p = em485->port; 1437 unsigned long flags; 1438 1439 serial8250_rpm_get(p); 1440 spin_lock_irqsave(&p->port.lock, flags); 1441 if (em485->active_timer == &em485->stop_tx_timer) { 1442 p->rs485_stop_tx(p); 1443 em485->active_timer = NULL; 1444 em485->tx_stopped = true; 1445 } 1446 spin_unlock_irqrestore(&p->port.lock, flags); 1447 serial8250_rpm_put(p); 1448 1449 return HRTIMER_NORESTART; 1450 } 1451 1452 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec) 1453 { 1454 hrtimer_start(hrt, ms_to_ktime(msec), HRTIMER_MODE_REL); 1455 } 1456 1457 static void __stop_tx_rs485(struct uart_8250_port *p, u64 stop_delay) 1458 { 1459 struct uart_8250_em485 *em485 = p->em485; 1460 1461 /* Port locked to synchronize UART_IER access against the console. */ 1462 lockdep_assert_held_once(&p->port.lock); 1463 1464 stop_delay += (u64)p->port.rs485.delay_rts_after_send * NSEC_PER_MSEC; 1465 1466 /* 1467 * rs485_stop_tx() is going to set RTS according to config 1468 * AND flush RX FIFO if required. 1469 */ 1470 if (stop_delay > 0) { 1471 em485->active_timer = &em485->stop_tx_timer; 1472 hrtimer_start(&em485->stop_tx_timer, ns_to_ktime(stop_delay), HRTIMER_MODE_REL); 1473 } else { 1474 p->rs485_stop_tx(p); 1475 em485->active_timer = NULL; 1476 em485->tx_stopped = true; 1477 } 1478 } 1479 1480 static inline void __stop_tx(struct uart_8250_port *p) 1481 { 1482 struct uart_8250_em485 *em485 = p->em485; 1483 1484 if (em485) { 1485 u16 lsr = serial_lsr_in(p); 1486 u64 stop_delay = 0; 1487 1488 if (!(lsr & UART_LSR_THRE)) 1489 return; 1490 /* 1491 * To provide required timing and allow FIFO transfer, 1492 * __stop_tx_rs485() must be called only when both FIFO and 1493 * shift register are empty. The device driver should either 1494 * enable interrupt on TEMT or set UART_CAP_NOTEMT that will 1495 * enlarge stop_tx_timer by the tx time of one frame to cover 1496 * for emptying of the shift register. 1497 */ 1498 if (!(lsr & UART_LSR_TEMT)) { 1499 if (!(p->capabilities & UART_CAP_NOTEMT)) 1500 return; 1501 /* 1502 * RTS might get deasserted too early with the normal 1503 * frame timing formula. It seems to suggest THRE might 1504 * get asserted already during tx of the stop bit 1505 * rather than after it is fully sent. 1506 * Roughly estimate 1 extra bit here with / 7. 1507 */ 1508 stop_delay = p->port.frame_time + DIV_ROUND_UP(p->port.frame_time, 7); 1509 } 1510 1511 __stop_tx_rs485(p, stop_delay); 1512 } 1513 1514 if (serial8250_clear_THRI(p)) 1515 serial8250_rpm_put_tx(p); 1516 } 1517 1518 static void serial8250_stop_tx(struct uart_port *port) 1519 { 1520 struct uart_8250_port *up = up_to_u8250p(port); 1521 1522 serial8250_rpm_get(up); 1523 __stop_tx(up); 1524 1525 /* 1526 * We really want to stop the transmitter from sending. 1527 */ 1528 if (port->type == PORT_16C950) { 1529 up->acr |= UART_ACR_TXDIS; 1530 serial_icr_write(up, UART_ACR, up->acr); 1531 } 1532 serial8250_rpm_put(up); 1533 } 1534 1535 static inline void __start_tx(struct uart_port *port) 1536 { 1537 struct uart_8250_port *up = up_to_u8250p(port); 1538 1539 if (up->dma && !up->dma->tx_dma(up)) 1540 return; 1541 1542 if (serial8250_set_THRI(up)) { 1543 if (up->bugs & UART_BUG_TXEN) { 1544 u16 lsr = serial_lsr_in(up); 1545 1546 if (lsr & UART_LSR_THRE) 1547 serial8250_tx_chars(up); 1548 } 1549 } 1550 1551 /* 1552 * Re-enable the transmitter if we disabled it. 1553 */ 1554 if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) { 1555 up->acr &= ~UART_ACR_TXDIS; 1556 serial_icr_write(up, UART_ACR, up->acr); 1557 } 1558 } 1559 1560 /** 1561 * serial8250_em485_start_tx() - generic ->rs485_start_tx() callback 1562 * @up: uart 8250 port 1563 * 1564 * Generic callback usable by 8250 uart drivers to start rs485 transmission. 1565 * Assumes that setting the RTS bit in the MCR register means RTS is high. 1566 * (Some chips use inverse semantics.) Further assumes that reception is 1567 * stoppable by disabling the UART_IER_RDI interrupt. (Some chips set the 1568 * UART_LSR_DR bit even when UART_IER_RDI is disabled, foiling this approach.) 1569 */ 1570 void serial8250_em485_start_tx(struct uart_8250_port *up) 1571 { 1572 unsigned char mcr = serial8250_in_MCR(up); 1573 1574 if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX)) 1575 serial8250_stop_rx(&up->port); 1576 1577 if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND) 1578 mcr |= UART_MCR_RTS; 1579 else 1580 mcr &= ~UART_MCR_RTS; 1581 serial8250_out_MCR(up, mcr); 1582 } 1583 EXPORT_SYMBOL_GPL(serial8250_em485_start_tx); 1584 1585 /* Returns false, if start_tx_timer was setup to defer TX start */ 1586 static bool start_tx_rs485(struct uart_port *port) 1587 { 1588 struct uart_8250_port *up = up_to_u8250p(port); 1589 struct uart_8250_em485 *em485 = up->em485; 1590 1591 /* 1592 * While serial8250_em485_handle_stop_tx() is a noop if 1593 * em485->active_timer != &em485->stop_tx_timer, it might happen that 1594 * the timer is still armed and triggers only after the current bunch of 1595 * chars is send and em485->active_timer == &em485->stop_tx_timer again. 1596 * So cancel the timer. There is still a theoretical race condition if 1597 * the timer is already running and only comes around to check for 1598 * em485->active_timer when &em485->stop_tx_timer is armed again. 1599 */ 1600 if (em485->active_timer == &em485->stop_tx_timer) 1601 hrtimer_try_to_cancel(&em485->stop_tx_timer); 1602 1603 em485->active_timer = NULL; 1604 1605 if (em485->tx_stopped) { 1606 em485->tx_stopped = false; 1607 1608 up->rs485_start_tx(up); 1609 1610 if (up->port.rs485.delay_rts_before_send > 0) { 1611 em485->active_timer = &em485->start_tx_timer; 1612 start_hrtimer_ms(&em485->start_tx_timer, 1613 up->port.rs485.delay_rts_before_send); 1614 return false; 1615 } 1616 } 1617 1618 return true; 1619 } 1620 1621 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t) 1622 { 1623 struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485, 1624 start_tx_timer); 1625 struct uart_8250_port *p = em485->port; 1626 unsigned long flags; 1627 1628 spin_lock_irqsave(&p->port.lock, flags); 1629 if (em485->active_timer == &em485->start_tx_timer) { 1630 __start_tx(&p->port); 1631 em485->active_timer = NULL; 1632 } 1633 spin_unlock_irqrestore(&p->port.lock, flags); 1634 1635 return HRTIMER_NORESTART; 1636 } 1637 1638 static void serial8250_start_tx(struct uart_port *port) 1639 { 1640 struct uart_8250_port *up = up_to_u8250p(port); 1641 struct uart_8250_em485 *em485 = up->em485; 1642 1643 /* Port locked to synchronize UART_IER access against the console. */ 1644 lockdep_assert_held_once(&port->lock); 1645 1646 if (!port->x_char && uart_circ_empty(&port->state->xmit)) 1647 return; 1648 1649 serial8250_rpm_get_tx(up); 1650 1651 if (em485) { 1652 if ((em485->active_timer == &em485->start_tx_timer) || 1653 !start_tx_rs485(port)) 1654 return; 1655 } 1656 __start_tx(port); 1657 } 1658 1659 static void serial8250_throttle(struct uart_port *port) 1660 { 1661 port->throttle(port); 1662 } 1663 1664 static void serial8250_unthrottle(struct uart_port *port) 1665 { 1666 port->unthrottle(port); 1667 } 1668 1669 static void serial8250_disable_ms(struct uart_port *port) 1670 { 1671 struct uart_8250_port *up = up_to_u8250p(port); 1672 1673 /* Port locked to synchronize UART_IER access against the console. */ 1674 lockdep_assert_held_once(&port->lock); 1675 1676 /* no MSR capabilities */ 1677 if (up->bugs & UART_BUG_NOMSR) 1678 return; 1679 1680 mctrl_gpio_disable_ms(up->gpios); 1681 1682 up->ier &= ~UART_IER_MSI; 1683 serial_port_out(port, UART_IER, up->ier); 1684 } 1685 1686 static void serial8250_enable_ms(struct uart_port *port) 1687 { 1688 struct uart_8250_port *up = up_to_u8250p(port); 1689 1690 /* Port locked to synchronize UART_IER access against the console. */ 1691 lockdep_assert_held_once(&port->lock); 1692 1693 /* no MSR capabilities */ 1694 if (up->bugs & UART_BUG_NOMSR) 1695 return; 1696 1697 mctrl_gpio_enable_ms(up->gpios); 1698 1699 up->ier |= UART_IER_MSI; 1700 1701 serial8250_rpm_get(up); 1702 serial_port_out(port, UART_IER, up->ier); 1703 serial8250_rpm_put(up); 1704 } 1705 1706 void serial8250_read_char(struct uart_8250_port *up, u16 lsr) 1707 { 1708 struct uart_port *port = &up->port; 1709 u8 ch, flag = TTY_NORMAL; 1710 1711 if (likely(lsr & UART_LSR_DR)) 1712 ch = serial_in(up, UART_RX); 1713 else 1714 /* 1715 * Intel 82571 has a Serial Over Lan device that will 1716 * set UART_LSR_BI without setting UART_LSR_DR when 1717 * it receives a break. To avoid reading from the 1718 * receive buffer without UART_LSR_DR bit set, we 1719 * just force the read character to be 0 1720 */ 1721 ch = 0; 1722 1723 port->icount.rx++; 1724 1725 lsr |= up->lsr_saved_flags; 1726 up->lsr_saved_flags = 0; 1727 1728 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) { 1729 if (lsr & UART_LSR_BI) { 1730 lsr &= ~(UART_LSR_FE | UART_LSR_PE); 1731 port->icount.brk++; 1732 /* 1733 * We do the SysRQ and SAK checking 1734 * here because otherwise the break 1735 * may get masked by ignore_status_mask 1736 * or read_status_mask. 1737 */ 1738 if (uart_handle_break(port)) 1739 return; 1740 } else if (lsr & UART_LSR_PE) 1741 port->icount.parity++; 1742 else if (lsr & UART_LSR_FE) 1743 port->icount.frame++; 1744 if (lsr & UART_LSR_OE) 1745 port->icount.overrun++; 1746 1747 /* 1748 * Mask off conditions which should be ignored. 1749 */ 1750 lsr &= port->read_status_mask; 1751 1752 if (lsr & UART_LSR_BI) { 1753 dev_dbg(port->dev, "handling break\n"); 1754 flag = TTY_BREAK; 1755 } else if (lsr & UART_LSR_PE) 1756 flag = TTY_PARITY; 1757 else if (lsr & UART_LSR_FE) 1758 flag = TTY_FRAME; 1759 } 1760 if (uart_prepare_sysrq_char(port, ch)) 1761 return; 1762 1763 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag); 1764 } 1765 EXPORT_SYMBOL_GPL(serial8250_read_char); 1766 1767 /* 1768 * serial8250_rx_chars - Read characters. The first LSR value must be passed in. 1769 * 1770 * Returns LSR bits. The caller should rely only on non-Rx related LSR bits 1771 * (such as THRE) because the LSR value might come from an already consumed 1772 * character. 1773 */ 1774 u16 serial8250_rx_chars(struct uart_8250_port *up, u16 lsr) 1775 { 1776 struct uart_port *port = &up->port; 1777 int max_count = 256; 1778 1779 do { 1780 serial8250_read_char(up, lsr); 1781 if (--max_count == 0) 1782 break; 1783 lsr = serial_in(up, UART_LSR); 1784 } while (lsr & (UART_LSR_DR | UART_LSR_BI)); 1785 1786 tty_flip_buffer_push(&port->state->port); 1787 return lsr; 1788 } 1789 EXPORT_SYMBOL_GPL(serial8250_rx_chars); 1790 1791 void serial8250_tx_chars(struct uart_8250_port *up) 1792 { 1793 struct uart_port *port = &up->port; 1794 struct circ_buf *xmit = &port->state->xmit; 1795 int count; 1796 1797 if (port->x_char) { 1798 uart_xchar_out(port, UART_TX); 1799 return; 1800 } 1801 if (uart_tx_stopped(port)) { 1802 serial8250_stop_tx(port); 1803 return; 1804 } 1805 if (uart_circ_empty(xmit)) { 1806 __stop_tx(up); 1807 return; 1808 } 1809 1810 count = up->tx_loadsz; 1811 do { 1812 serial_out(up, UART_TX, xmit->buf[xmit->tail]); 1813 if (up->bugs & UART_BUG_TXRACE) { 1814 /* 1815 * The Aspeed BMC virtual UARTs have a bug where data 1816 * may get stuck in the BMC's Tx FIFO from bursts of 1817 * writes on the APB interface. 1818 * 1819 * Delay back-to-back writes by a read cycle to avoid 1820 * stalling the VUART. Read a register that won't have 1821 * side-effects and discard the result. 1822 */ 1823 serial_in(up, UART_SCR); 1824 } 1825 uart_xmit_advance(port, 1); 1826 if (uart_circ_empty(xmit)) 1827 break; 1828 if ((up->capabilities & UART_CAP_HFIFO) && 1829 !uart_lsr_tx_empty(serial_in(up, UART_LSR))) 1830 break; 1831 /* The BCM2835 MINI UART THRE bit is really a not-full bit. */ 1832 if ((up->capabilities & UART_CAP_MINI) && 1833 !(serial_in(up, UART_LSR) & UART_LSR_THRE)) 1834 break; 1835 } while (--count > 0); 1836 1837 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 1838 uart_write_wakeup(port); 1839 1840 /* 1841 * With RPM enabled, we have to wait until the FIFO is empty before the 1842 * HW can go idle. So we get here once again with empty FIFO and disable 1843 * the interrupt and RPM in __stop_tx() 1844 */ 1845 if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM)) 1846 __stop_tx(up); 1847 } 1848 EXPORT_SYMBOL_GPL(serial8250_tx_chars); 1849 1850 /* Caller holds uart port lock */ 1851 unsigned int serial8250_modem_status(struct uart_8250_port *up) 1852 { 1853 struct uart_port *port = &up->port; 1854 unsigned int status = serial_in(up, UART_MSR); 1855 1856 status |= up->msr_saved_flags; 1857 up->msr_saved_flags = 0; 1858 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI && 1859 port->state != NULL) { 1860 if (status & UART_MSR_TERI) 1861 port->icount.rng++; 1862 if (status & UART_MSR_DDSR) 1863 port->icount.dsr++; 1864 if (status & UART_MSR_DDCD) 1865 uart_handle_dcd_change(port, status & UART_MSR_DCD); 1866 if (status & UART_MSR_DCTS) 1867 uart_handle_cts_change(port, status & UART_MSR_CTS); 1868 1869 wake_up_interruptible(&port->state->port.delta_msr_wait); 1870 } 1871 1872 return status; 1873 } 1874 EXPORT_SYMBOL_GPL(serial8250_modem_status); 1875 1876 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir) 1877 { 1878 switch (iir & 0x3f) { 1879 case UART_IIR_THRI: 1880 /* 1881 * Postpone DMA or not decision to IIR_RDI or IIR_RX_TIMEOUT 1882 * because it's impossible to do an informed decision about 1883 * that with IIR_THRI. 1884 * 1885 * This also fixes one known DMA Rx corruption issue where 1886 * DR is asserted but DMA Rx only gets a corrupted zero byte 1887 * (too early DR?). 1888 */ 1889 return false; 1890 case UART_IIR_RDI: 1891 if (!up->dma->rx_running) 1892 break; 1893 fallthrough; 1894 case UART_IIR_RLSI: 1895 case UART_IIR_RX_TIMEOUT: 1896 serial8250_rx_dma_flush(up); 1897 return true; 1898 } 1899 return up->dma->rx_dma(up); 1900 } 1901 1902 /* 1903 * This handles the interrupt from one port. 1904 */ 1905 int serial8250_handle_irq(struct uart_port *port, unsigned int iir) 1906 { 1907 struct uart_8250_port *up = up_to_u8250p(port); 1908 struct tty_port *tport = &port->state->port; 1909 bool skip_rx = false; 1910 unsigned long flags; 1911 u16 status; 1912 1913 if (iir & UART_IIR_NO_INT) 1914 return 0; 1915 1916 spin_lock_irqsave(&port->lock, flags); 1917 1918 status = serial_lsr_in(up); 1919 1920 /* 1921 * If port is stopped and there are no error conditions in the 1922 * FIFO, then don't drain the FIFO, as this may lead to TTY buffer 1923 * overflow. Not servicing, RX FIFO would trigger auto HW flow 1924 * control when FIFO occupancy reaches preset threshold, thus 1925 * halting RX. This only works when auto HW flow control is 1926 * available. 1927 */ 1928 if (!(status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS)) && 1929 (port->status & (UPSTAT_AUTOCTS | UPSTAT_AUTORTS)) && 1930 !(port->read_status_mask & UART_LSR_DR)) 1931 skip_rx = true; 1932 1933 if (status & (UART_LSR_DR | UART_LSR_BI) && !skip_rx) { 1934 if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) 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 spin_lock_irqsave(&port->lock, flags); 1984 serial8250_tx_chars(up); 1985 spin_unlock_irqrestore(&port->lock, 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 spin_lock_irqsave(&port->lock, flags); 2001 if (!serial8250_tx_dma_running(up) && uart_lsr_tx_empty(serial_lsr_in(up))) 2002 result = TIOCSER_TEMT; 2003 spin_unlock_irqrestore(&port->lock, 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 spin_lock_irqsave(&port->lock, 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 spin_unlock_irqrestore(&port->lock, 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 spin_lock_irqsave(&port->lock, 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 spin_unlock_irqrestore(&port->lock, 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 spin_lock_irqsave(&port->lock, flags); 2226 serial_port_out(port, UART_IER, 0); 2227 serial_port_out(port, UART_DA830_PWREMU_MGMT, 0); 2228 spin_unlock_irqrestore(&port->lock, 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 spin_lock_irqsave(&port->lock, 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 spin_unlock_irqrestore(&port->lock, 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 spin_lock_irqsave(&port->lock, 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 spin_unlock_irqrestore(&port->lock, 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 spin_lock_irqsave(&port->lock, flags); 2495 up->ier = 0; 2496 serial_port_out(port, UART_IER, 0); 2497 spin_unlock_irqrestore(&port->lock, flags); 2498 2499 synchronize_irq(port->irq); 2500 2501 if (up->dma) 2502 serial8250_release_dma(up); 2503 2504 spin_lock_irqsave(&port->lock, 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 spin_unlock_irqrestore(&port->lock, 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 spin_lock_irqsave(&port->lock, 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 spin_unlock_irqrestore(&port->lock, 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 spin_lock_irqsave(&port->lock, 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 spin_unlock_irqrestore(&port->lock, 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 spin_lock_irq(&port->lock); 2920 serial8250_enable_ms(port); 2921 spin_unlock_irq(&port->lock); 2922 } else { 2923 port->flags &= ~UPF_HARDPPS_CD; 2924 if (!UART_ENABLE_MS(port, termios->c_cflag)) { 2925 spin_lock_irq(&port->lock); 2926 serial8250_disable_ms(port); 2927 spin_unlock_irq(&port->lock); 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->ops = &serial8250_pops; 3281 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE); 3282 3283 up->cur_iotype = 0xFF; 3284 } 3285 EXPORT_SYMBOL_GPL(serial8250_init_port); 3286 3287 void serial8250_set_defaults(struct uart_8250_port *up) 3288 { 3289 struct uart_port *port = &up->port; 3290 3291 if (up->port.flags & UPF_FIXED_TYPE) { 3292 unsigned int type = up->port.type; 3293 3294 if (!up->port.fifosize) 3295 up->port.fifosize = uart_config[type].fifo_size; 3296 if (!up->tx_loadsz) 3297 up->tx_loadsz = uart_config[type].tx_loadsz; 3298 if (!up->capabilities) 3299 up->capabilities = uart_config[type].flags; 3300 } 3301 3302 set_io_from_upio(port); 3303 3304 /* default dma handlers */ 3305 if (up->dma) { 3306 if (!up->dma->tx_dma) 3307 up->dma->tx_dma = serial8250_tx_dma; 3308 if (!up->dma->rx_dma) 3309 up->dma->rx_dma = serial8250_rx_dma; 3310 } 3311 } 3312 EXPORT_SYMBOL_GPL(serial8250_set_defaults); 3313 3314 #ifdef CONFIG_SERIAL_8250_CONSOLE 3315 3316 static void serial8250_console_putchar(struct uart_port *port, unsigned char ch) 3317 { 3318 struct uart_8250_port *up = up_to_u8250p(port); 3319 3320 wait_for_xmitr(up, UART_LSR_THRE); 3321 serial_port_out(port, UART_TX, ch); 3322 } 3323 3324 /* 3325 * Restore serial console when h/w power-off detected 3326 */ 3327 static void serial8250_console_restore(struct uart_8250_port *up) 3328 { 3329 struct uart_port *port = &up->port; 3330 struct ktermios termios; 3331 unsigned int baud, quot, frac = 0; 3332 3333 termios.c_cflag = port->cons->cflag; 3334 termios.c_ispeed = port->cons->ispeed; 3335 termios.c_ospeed = port->cons->ospeed; 3336 if (port->state->port.tty && termios.c_cflag == 0) { 3337 termios.c_cflag = port->state->port.tty->termios.c_cflag; 3338 termios.c_ispeed = port->state->port.tty->termios.c_ispeed; 3339 termios.c_ospeed = port->state->port.tty->termios.c_ospeed; 3340 } 3341 3342 baud = serial8250_get_baud_rate(port, &termios, NULL); 3343 quot = serial8250_get_divisor(port, baud, &frac); 3344 3345 serial8250_set_divisor(port, baud, quot, frac); 3346 serial_port_out(port, UART_LCR, up->lcr); 3347 serial8250_out_MCR(up, up->mcr | UART_MCR_DTR | UART_MCR_RTS); 3348 } 3349 3350 /* 3351 * Print a string to the serial port using the device FIFO 3352 * 3353 * It sends fifosize bytes and then waits for the fifo 3354 * to get empty. 3355 */ 3356 static void serial8250_console_fifo_write(struct uart_8250_port *up, 3357 const char *s, unsigned int count) 3358 { 3359 int i; 3360 const char *end = s + count; 3361 unsigned int fifosize = up->tx_loadsz; 3362 bool cr_sent = false; 3363 3364 while (s != end) { 3365 wait_for_lsr(up, UART_LSR_THRE); 3366 3367 for (i = 0; i < fifosize && s != end; ++i) { 3368 if (*s == '\n' && !cr_sent) { 3369 serial_out(up, UART_TX, '\r'); 3370 cr_sent = true; 3371 } else { 3372 serial_out(up, UART_TX, *s++); 3373 cr_sent = false; 3374 } 3375 } 3376 } 3377 } 3378 3379 /* 3380 * Print a string to the serial port trying not to disturb 3381 * any possible real use of the port... 3382 * 3383 * The console_lock must be held when we get here. 3384 * 3385 * Doing runtime PM is really a bad idea for the kernel console. 3386 * Thus, we assume the function is called when device is powered up. 3387 */ 3388 void serial8250_console_write(struct uart_8250_port *up, const char *s, 3389 unsigned int count) 3390 { 3391 struct uart_8250_em485 *em485 = up->em485; 3392 struct uart_port *port = &up->port; 3393 unsigned long flags; 3394 unsigned int ier, use_fifo; 3395 int locked = 1; 3396 3397 touch_nmi_watchdog(); 3398 3399 if (oops_in_progress) 3400 locked = spin_trylock_irqsave(&port->lock, flags); 3401 else 3402 spin_lock_irqsave(&port->lock, flags); 3403 3404 /* 3405 * First save the IER then disable the interrupts 3406 */ 3407 ier = serial_port_in(port, UART_IER); 3408 serial8250_clear_IER(up); 3409 3410 /* check scratch reg to see if port powered off during system sleep */ 3411 if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) { 3412 serial8250_console_restore(up); 3413 up->canary = 0; 3414 } 3415 3416 if (em485) { 3417 if (em485->tx_stopped) 3418 up->rs485_start_tx(up); 3419 mdelay(port->rs485.delay_rts_before_send); 3420 } 3421 3422 use_fifo = (up->capabilities & UART_CAP_FIFO) && 3423 /* 3424 * BCM283x requires to check the fifo 3425 * after each byte. 3426 */ 3427 !(up->capabilities & UART_CAP_MINI) && 3428 /* 3429 * tx_loadsz contains the transmit fifo size 3430 */ 3431 up->tx_loadsz > 1 && 3432 (up->fcr & UART_FCR_ENABLE_FIFO) && 3433 port->state && 3434 test_bit(TTY_PORT_INITIALIZED, &port->state->port.iflags) && 3435 /* 3436 * After we put a data in the fifo, the controller will send 3437 * it regardless of the CTS state. Therefore, only use fifo 3438 * if we don't use control flow. 3439 */ 3440 !(up->port.flags & UPF_CONS_FLOW); 3441 3442 if (likely(use_fifo)) 3443 serial8250_console_fifo_write(up, s, count); 3444 else 3445 uart_console_write(port, s, count, serial8250_console_putchar); 3446 3447 /* 3448 * Finally, wait for transmitter to become empty 3449 * and restore the IER 3450 */ 3451 wait_for_xmitr(up, UART_LSR_BOTH_EMPTY); 3452 3453 if (em485) { 3454 mdelay(port->rs485.delay_rts_after_send); 3455 if (em485->tx_stopped) 3456 up->rs485_stop_tx(up); 3457 } 3458 3459 serial_port_out(port, UART_IER, ier); 3460 3461 /* 3462 * The receive handling will happen properly because the 3463 * receive ready bit will still be set; it is not cleared 3464 * on read. However, modem control will not, we must 3465 * call it if we have saved something in the saved flags 3466 * while processing with interrupts off. 3467 */ 3468 if (up->msr_saved_flags) 3469 serial8250_modem_status(up); 3470 3471 if (locked) 3472 spin_unlock_irqrestore(&port->lock, flags); 3473 } 3474 3475 static unsigned int probe_baud(struct uart_port *port) 3476 { 3477 unsigned char lcr, dll, dlm; 3478 unsigned int quot; 3479 3480 lcr = serial_port_in(port, UART_LCR); 3481 serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB); 3482 dll = serial_port_in(port, UART_DLL); 3483 dlm = serial_port_in(port, UART_DLM); 3484 serial_port_out(port, UART_LCR, lcr); 3485 3486 quot = (dlm << 8) | dll; 3487 return (port->uartclk / 16) / quot; 3488 } 3489 3490 int serial8250_console_setup(struct uart_port *port, char *options, bool probe) 3491 { 3492 int baud = 9600; 3493 int bits = 8; 3494 int parity = 'n'; 3495 int flow = 'n'; 3496 int ret; 3497 3498 if (!port->iobase && !port->membase) 3499 return -ENODEV; 3500 3501 if (options) 3502 uart_parse_options(options, &baud, &parity, &bits, &flow); 3503 else if (probe) 3504 baud = probe_baud(port); 3505 3506 ret = uart_set_options(port, port->cons, baud, parity, bits, flow); 3507 if (ret) 3508 return ret; 3509 3510 if (port->dev) 3511 pm_runtime_get_sync(port->dev); 3512 3513 return 0; 3514 } 3515 3516 int serial8250_console_exit(struct uart_port *port) 3517 { 3518 if (port->dev) 3519 pm_runtime_put_sync(port->dev); 3520 3521 return 0; 3522 } 3523 3524 #endif /* CONFIG_SERIAL_8250_CONSOLE */ 3525 3526 MODULE_LICENSE("GPL"); 3527