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