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