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