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