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