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