1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Universal/legacy driver for 8250/16550-type serial ports 4 * 5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. 6 * 7 * Copyright (C) 2001 Russell King. 8 * 9 * Supports: ISA-compatible 8250/16550 ports 10 * PNP 8250/16550 ports 11 * early_serial_setup() ports 12 * userspace-configurable "phantom" ports 13 * "serial8250" platform devices 14 * serial8250_register_8250_port() ports 15 */ 16 17 #include <linux/acpi.h> 18 #include <linux/module.h> 19 #include <linux/moduleparam.h> 20 #include <linux/ioport.h> 21 #include <linux/init.h> 22 #include <linux/console.h> 23 #include <linux/sysrq.h> 24 #include <linux/delay.h> 25 #include <linux/platform_device.h> 26 #include <linux/pm_runtime.h> 27 #include <linux/tty.h> 28 #include <linux/ratelimit.h> 29 #include <linux/tty_flip.h> 30 #include <linux/serial.h> 31 #include <linux/serial_8250.h> 32 #include <linux/nmi.h> 33 #include <linux/mutex.h> 34 #include <linux/slab.h> 35 #include <linux/string_helpers.h> 36 #include <linux/uaccess.h> 37 #include <linux/io.h> 38 #ifdef CONFIG_SPARC 39 #include <linux/sunserialcore.h> 40 #endif 41 42 #include <asm/irq.h> 43 44 #include "8250.h" 45 46 /* 47 * Configuration: 48 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option 49 * is unsafe when used on edge-triggered interrupts. 50 */ 51 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS; 52 53 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS; 54 55 static struct uart_driver serial8250_reg; 56 57 static unsigned int skip_txen_test; /* force skip of txen test at init time */ 58 59 #define PASS_LIMIT 512 60 61 #include <asm/serial.h> 62 /* 63 * SERIAL_PORT_DFNS tells us about built-in ports that have no 64 * standard enumeration mechanism. Platforms that can find all 65 * serial ports via mechanisms like ACPI or PCI need not supply it. 66 */ 67 #ifndef SERIAL_PORT_DFNS 68 #define SERIAL_PORT_DFNS 69 #endif 70 71 static const struct old_serial_port old_serial_port[] = { 72 SERIAL_PORT_DFNS /* defined in asm/serial.h */ 73 }; 74 75 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS 76 77 #ifdef CONFIG_SERIAL_8250_RSA 78 79 #define PORT_RSA_MAX 4 80 static unsigned long probe_rsa[PORT_RSA_MAX]; 81 static unsigned int probe_rsa_count; 82 #endif /* CONFIG_SERIAL_8250_RSA */ 83 84 struct irq_info { 85 struct hlist_node node; 86 int irq; 87 spinlock_t lock; /* Protects list not the hash */ 88 struct list_head *head; 89 }; 90 91 #define NR_IRQ_HASH 32 /* Can be adjusted later */ 92 static struct hlist_head irq_lists[NR_IRQ_HASH]; 93 static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */ 94 95 /* 96 * This is the serial driver's interrupt routine. 97 * 98 * Arjan thinks the old way was overly complex, so it got simplified. 99 * Alan disagrees, saying that need the complexity to handle the weird 100 * nature of ISA shared interrupts. (This is a special exception.) 101 * 102 * In order to handle ISA shared interrupts properly, we need to check 103 * that all ports have been serviced, and therefore the ISA interrupt 104 * line has been de-asserted. 105 * 106 * This means we need to loop through all ports. checking that they 107 * don't have an interrupt pending. 108 */ 109 static irqreturn_t serial8250_interrupt(int irq, void *dev_id) 110 { 111 struct irq_info *i = dev_id; 112 struct list_head *l, *end = NULL; 113 int pass_counter = 0, handled = 0; 114 115 pr_debug("%s(%d): start\n", __func__, irq); 116 117 spin_lock(&i->lock); 118 119 l = i->head; 120 do { 121 struct uart_8250_port *up; 122 struct uart_port *port; 123 124 up = list_entry(l, struct uart_8250_port, list); 125 port = &up->port; 126 127 if (port->handle_irq(port)) { 128 handled = 1; 129 end = NULL; 130 } else if (end == NULL) 131 end = l; 132 133 l = l->next; 134 135 if (l == i->head && pass_counter++ > PASS_LIMIT) 136 break; 137 } while (l != end); 138 139 spin_unlock(&i->lock); 140 141 pr_debug("%s(%d): end\n", __func__, irq); 142 143 return IRQ_RETVAL(handled); 144 } 145 146 /* 147 * To support ISA shared interrupts, we need to have one interrupt 148 * handler that ensures that the IRQ line has been deasserted 149 * before returning. Failing to do this will result in the IRQ 150 * line being stuck active, and, since ISA irqs are edge triggered, 151 * no more IRQs will be seen. 152 */ 153 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up) 154 { 155 spin_lock_irq(&i->lock); 156 157 if (!list_empty(i->head)) { 158 if (i->head == &up->list) 159 i->head = i->head->next; 160 list_del(&up->list); 161 } else { 162 BUG_ON(i->head != &up->list); 163 i->head = NULL; 164 } 165 spin_unlock_irq(&i->lock); 166 /* List empty so throw away the hash node */ 167 if (i->head == NULL) { 168 hlist_del(&i->node); 169 kfree(i); 170 } 171 } 172 173 static int serial_link_irq_chain(struct uart_8250_port *up) 174 { 175 struct hlist_head *h; 176 struct irq_info *i; 177 int ret; 178 179 mutex_lock(&hash_mutex); 180 181 h = &irq_lists[up->port.irq % NR_IRQ_HASH]; 182 183 hlist_for_each_entry(i, h, node) 184 if (i->irq == up->port.irq) 185 break; 186 187 if (i == NULL) { 188 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL); 189 if (i == NULL) { 190 mutex_unlock(&hash_mutex); 191 return -ENOMEM; 192 } 193 spin_lock_init(&i->lock); 194 i->irq = up->port.irq; 195 hlist_add_head(&i->node, h); 196 } 197 mutex_unlock(&hash_mutex); 198 199 spin_lock_irq(&i->lock); 200 201 if (i->head) { 202 list_add(&up->list, i->head); 203 spin_unlock_irq(&i->lock); 204 205 ret = 0; 206 } else { 207 INIT_LIST_HEAD(&up->list); 208 i->head = &up->list; 209 spin_unlock_irq(&i->lock); 210 ret = request_irq(up->port.irq, serial8250_interrupt, 211 up->port.irqflags, up->port.name, i); 212 if (ret < 0) 213 serial_do_unlink(i, up); 214 } 215 216 return ret; 217 } 218 219 static void serial_unlink_irq_chain(struct uart_8250_port *up) 220 { 221 struct irq_info *i; 222 struct hlist_head *h; 223 224 mutex_lock(&hash_mutex); 225 226 h = &irq_lists[up->port.irq % NR_IRQ_HASH]; 227 228 hlist_for_each_entry(i, h, node) 229 if (i->irq == up->port.irq) 230 break; 231 232 BUG_ON(i == NULL); 233 BUG_ON(i->head == NULL); 234 235 if (list_empty(i->head)) 236 free_irq(up->port.irq, i); 237 238 serial_do_unlink(i, up); 239 mutex_unlock(&hash_mutex); 240 } 241 242 /* 243 * This function is used to handle ports that do not have an 244 * interrupt. This doesn't work very well for 16450's, but gives 245 * barely passable results for a 16550A. (Although at the expense 246 * of much CPU overhead). 247 */ 248 static void serial8250_timeout(struct timer_list *t) 249 { 250 struct uart_8250_port *up = from_timer(up, t, timer); 251 252 up->port.handle_irq(&up->port); 253 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port)); 254 } 255 256 static void serial8250_backup_timeout(struct timer_list *t) 257 { 258 struct uart_8250_port *up = from_timer(up, t, timer); 259 unsigned int iir, ier = 0, lsr; 260 unsigned long flags; 261 262 uart_port_lock_irqsave(&up->port, &flags); 263 264 /* 265 * Must disable interrupts or else we risk racing with the interrupt 266 * based handler. 267 */ 268 if (up->port.irq) { 269 ier = serial_in(up, UART_IER); 270 serial_out(up, UART_IER, 0); 271 } 272 273 iir = serial_in(up, UART_IIR); 274 275 /* 276 * This should be a safe test for anyone who doesn't trust the 277 * IIR bits on their UART, but it's specifically designed for 278 * the "Diva" UART used on the management processor on many HP 279 * ia64 and parisc boxes. 280 */ 281 lsr = serial_lsr_in(up); 282 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) && 283 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) && 284 (lsr & UART_LSR_THRE)) { 285 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT); 286 iir |= UART_IIR_THRI; 287 } 288 289 if (!(iir & UART_IIR_NO_INT)) 290 serial8250_tx_chars(up); 291 292 if (up->port.irq) 293 serial_out(up, UART_IER, ier); 294 295 uart_port_unlock_irqrestore(&up->port, flags); 296 297 /* Standard timer interval plus 0.2s to keep the port running */ 298 mod_timer(&up->timer, 299 jiffies + uart_poll_timeout(&up->port) + HZ / 5); 300 } 301 302 static void univ8250_setup_timer(struct uart_8250_port *up) 303 { 304 struct uart_port *port = &up->port; 305 306 /* 307 * The above check will only give an accurate result the first time 308 * the port is opened so this value needs to be preserved. 309 */ 310 if (up->bugs & UART_BUG_THRE) { 311 pr_debug("%s - using backup timer\n", port->name); 312 313 up->timer.function = serial8250_backup_timeout; 314 mod_timer(&up->timer, jiffies + 315 uart_poll_timeout(port) + HZ / 5); 316 } 317 318 /* 319 * If the "interrupt" for this port doesn't correspond with any 320 * hardware interrupt, we use a timer-based system. The original 321 * driver used to do this with IRQ0. 322 */ 323 if (!port->irq) 324 mod_timer(&up->timer, jiffies + uart_poll_timeout(port)); 325 } 326 327 static int univ8250_setup_irq(struct uart_8250_port *up) 328 { 329 struct uart_port *port = &up->port; 330 331 if (port->irq) 332 return serial_link_irq_chain(up); 333 334 return 0; 335 } 336 337 static void univ8250_release_irq(struct uart_8250_port *up) 338 { 339 struct uart_port *port = &up->port; 340 341 del_timer_sync(&up->timer); 342 up->timer.function = serial8250_timeout; 343 if (port->irq) 344 serial_unlink_irq_chain(up); 345 } 346 347 #ifdef CONFIG_SERIAL_8250_RSA 348 static int serial8250_request_rsa_resource(struct uart_8250_port *up) 349 { 350 unsigned long start = UART_RSA_BASE << up->port.regshift; 351 unsigned int size = 8 << up->port.regshift; 352 struct uart_port *port = &up->port; 353 int ret = -EINVAL; 354 355 switch (port->iotype) { 356 case UPIO_HUB6: 357 case UPIO_PORT: 358 start += port->iobase; 359 if (request_region(start, size, "serial-rsa")) 360 ret = 0; 361 else 362 ret = -EBUSY; 363 break; 364 } 365 366 return ret; 367 } 368 369 static void serial8250_release_rsa_resource(struct uart_8250_port *up) 370 { 371 unsigned long offset = UART_RSA_BASE << up->port.regshift; 372 unsigned int size = 8 << up->port.regshift; 373 struct uart_port *port = &up->port; 374 375 switch (port->iotype) { 376 case UPIO_HUB6: 377 case UPIO_PORT: 378 release_region(port->iobase + offset, size); 379 break; 380 } 381 } 382 #endif 383 384 static const struct uart_ops *base_ops; 385 static struct uart_ops univ8250_port_ops; 386 387 static const struct uart_8250_ops univ8250_driver_ops = { 388 .setup_irq = univ8250_setup_irq, 389 .release_irq = univ8250_release_irq, 390 .setup_timer = univ8250_setup_timer, 391 }; 392 393 static struct uart_8250_port serial8250_ports[UART_NR]; 394 395 /** 396 * serial8250_get_port - retrieve struct uart_8250_port 397 * @line: serial line number 398 * 399 * This function retrieves struct uart_8250_port for the specific line. 400 * This struct *must* *not* be used to perform a 8250 or serial core operation 401 * which is not accessible otherwise. Its only purpose is to make the struct 402 * accessible to the runtime-pm callbacks for context suspend/restore. 403 * The lock assumption made here is none because runtime-pm suspend/resume 404 * callbacks should not be invoked if there is any operation performed on the 405 * port. 406 */ 407 struct uart_8250_port *serial8250_get_port(int line) 408 { 409 return &serial8250_ports[line]; 410 } 411 EXPORT_SYMBOL_GPL(serial8250_get_port); 412 413 static void (*serial8250_isa_config)(int port, struct uart_port *up, 414 u32 *capabilities); 415 416 void serial8250_set_isa_configurator( 417 void (*v)(int port, struct uart_port *up, u32 *capabilities)) 418 { 419 serial8250_isa_config = v; 420 } 421 EXPORT_SYMBOL(serial8250_set_isa_configurator); 422 423 #ifdef CONFIG_SERIAL_8250_RSA 424 425 static void univ8250_config_port(struct uart_port *port, int flags) 426 { 427 struct uart_8250_port *up = up_to_u8250p(port); 428 429 up->probe &= ~UART_PROBE_RSA; 430 if (port->type == PORT_RSA) { 431 if (serial8250_request_rsa_resource(up) == 0) 432 up->probe |= UART_PROBE_RSA; 433 } else if (flags & UART_CONFIG_TYPE) { 434 int i; 435 436 for (i = 0; i < probe_rsa_count; i++) { 437 if (probe_rsa[i] == up->port.iobase) { 438 if (serial8250_request_rsa_resource(up) == 0) 439 up->probe |= UART_PROBE_RSA; 440 break; 441 } 442 } 443 } 444 445 base_ops->config_port(port, flags); 446 447 if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA) 448 serial8250_release_rsa_resource(up); 449 } 450 451 static int univ8250_request_port(struct uart_port *port) 452 { 453 struct uart_8250_port *up = up_to_u8250p(port); 454 int ret; 455 456 ret = base_ops->request_port(port); 457 if (ret == 0 && port->type == PORT_RSA) { 458 ret = serial8250_request_rsa_resource(up); 459 if (ret < 0) 460 base_ops->release_port(port); 461 } 462 463 return ret; 464 } 465 466 static void univ8250_release_port(struct uart_port *port) 467 { 468 struct uart_8250_port *up = up_to_u8250p(port); 469 470 if (port->type == PORT_RSA) 471 serial8250_release_rsa_resource(up); 472 base_ops->release_port(port); 473 } 474 475 static void univ8250_rsa_support(struct uart_ops *ops) 476 { 477 ops->config_port = univ8250_config_port; 478 ops->request_port = univ8250_request_port; 479 ops->release_port = univ8250_release_port; 480 } 481 482 #else 483 #define univ8250_rsa_support(x) do { } while (0) 484 #endif /* CONFIG_SERIAL_8250_RSA */ 485 486 static inline void serial8250_apply_quirks(struct uart_8250_port *up) 487 { 488 up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0; 489 } 490 491 static struct uart_8250_port *serial8250_setup_port(int index) 492 { 493 struct uart_8250_port *up; 494 495 if (index >= UART_NR) 496 return NULL; 497 498 up = &serial8250_ports[index]; 499 up->port.line = index; 500 up->port.port_id = index; 501 502 serial8250_init_port(up); 503 if (!base_ops) 504 base_ops = up->port.ops; 505 up->port.ops = &univ8250_port_ops; 506 507 timer_setup(&up->timer, serial8250_timeout, 0); 508 509 up->ops = &univ8250_driver_ops; 510 511 serial8250_set_defaults(up); 512 513 return up; 514 } 515 516 static void __init serial8250_isa_init_ports(void) 517 { 518 struct uart_8250_port *up; 519 static int first = 1; 520 int i, irqflag = 0; 521 522 if (!first) 523 return; 524 first = 0; 525 526 if (nr_uarts > UART_NR) 527 nr_uarts = UART_NR; 528 529 /* 530 * Set up initial isa ports based on nr_uart module param, or else 531 * default to CONFIG_SERIAL_8250_RUNTIME_UARTS. Note that we do not 532 * need to increase nr_uarts when setting up the initial isa ports. 533 */ 534 for (i = 0; i < nr_uarts; i++) 535 serial8250_setup_port(i); 536 537 /* chain base port ops to support Remote Supervisor Adapter */ 538 univ8250_port_ops = *base_ops; 539 univ8250_rsa_support(&univ8250_port_ops); 540 541 if (share_irqs) 542 irqflag = IRQF_SHARED; 543 544 for (i = 0, up = serial8250_ports; 545 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts; 546 i++, up++) { 547 struct uart_port *port = &up->port; 548 549 port->iobase = old_serial_port[i].port; 550 port->irq = irq_canonicalize(old_serial_port[i].irq); 551 port->irqflags = 0; 552 port->uartclk = old_serial_port[i].baud_base * 16; 553 port->flags = old_serial_port[i].flags; 554 port->hub6 = 0; 555 port->membase = old_serial_port[i].iomem_base; 556 port->iotype = old_serial_port[i].io_type; 557 port->regshift = old_serial_port[i].iomem_reg_shift; 558 559 port->irqflags |= irqflag; 560 if (serial8250_isa_config != NULL) 561 serial8250_isa_config(i, &up->port, &up->capabilities); 562 } 563 } 564 565 static void __init 566 serial8250_register_ports(struct uart_driver *drv, struct device *dev) 567 { 568 int i; 569 570 for (i = 0; i < nr_uarts; i++) { 571 struct uart_8250_port *up = &serial8250_ports[i]; 572 573 if (up->port.type == PORT_8250_CIR) 574 continue; 575 576 if (up->port.dev) 577 continue; 578 579 up->port.dev = dev; 580 581 if (uart_console_registered(&up->port)) 582 pm_runtime_get_sync(up->port.dev); 583 584 serial8250_apply_quirks(up); 585 uart_add_one_port(drv, &up->port); 586 } 587 } 588 589 #ifdef CONFIG_SERIAL_8250_CONSOLE 590 591 static void univ8250_console_write(struct console *co, const char *s, 592 unsigned int count) 593 { 594 struct uart_8250_port *up = &serial8250_ports[co->index]; 595 596 serial8250_console_write(up, s, count); 597 } 598 599 static int univ8250_console_setup(struct console *co, char *options) 600 { 601 struct uart_8250_port *up; 602 struct uart_port *port; 603 int retval, i; 604 605 /* 606 * Check whether an invalid uart number has been specified, and 607 * if so, search for the first available port that does have 608 * console support. 609 */ 610 if (co->index < 0 || co->index >= UART_NR) 611 co->index = 0; 612 613 /* 614 * If the console is past the initial isa ports, init more ports up to 615 * co->index as needed and increment nr_uarts accordingly. 616 */ 617 for (i = nr_uarts; i <= co->index; i++) { 618 up = serial8250_setup_port(i); 619 if (!up) 620 return -ENODEV; 621 nr_uarts++; 622 } 623 624 port = &serial8250_ports[co->index].port; 625 /* link port to console */ 626 port->cons = co; 627 628 retval = serial8250_console_setup(port, options, false); 629 if (retval != 0) 630 port->cons = NULL; 631 return retval; 632 } 633 634 static int univ8250_console_exit(struct console *co) 635 { 636 struct uart_port *port; 637 638 port = &serial8250_ports[co->index].port; 639 return serial8250_console_exit(port); 640 } 641 642 /** 643 * univ8250_console_match - non-standard console matching 644 * @co: registering console 645 * @name: name from console command line 646 * @idx: index from console command line 647 * @options: ptr to option string from console command line 648 * 649 * Only attempts to match console command lines of the form: 650 * console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>] 651 * console=uart[8250],0x<addr>[,<options>] 652 * This form is used to register an initial earlycon boot console and 653 * replace it with the serial8250_console at 8250 driver init. 654 * 655 * Performs console setup for a match (as required by interface) 656 * If no <options> are specified, then assume the h/w is already setup. 657 * 658 * Returns 0 if console matches; otherwise non-zero to use default matching 659 */ 660 static int univ8250_console_match(struct console *co, char *name, int idx, 661 char *options) 662 { 663 char match[] = "uart"; /* 8250-specific earlycon name */ 664 unsigned char iotype; 665 resource_size_t addr; 666 int i; 667 668 if (strncmp(name, match, 4) != 0) 669 return -ENODEV; 670 671 if (uart_parse_earlycon(options, &iotype, &addr, &options)) 672 return -ENODEV; 673 674 /* try to match the port specified on the command line */ 675 for (i = 0; i < nr_uarts; i++) { 676 struct uart_port *port = &serial8250_ports[i].port; 677 678 if (port->iotype != iotype) 679 continue; 680 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 || 681 iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE) 682 && (port->mapbase != addr)) 683 continue; 684 if (iotype == UPIO_PORT && port->iobase != addr) 685 continue; 686 687 co->index = i; 688 port->cons = co; 689 return serial8250_console_setup(port, options, true); 690 } 691 692 return -ENODEV; 693 } 694 695 static struct console univ8250_console = { 696 .name = "ttyS", 697 .write = univ8250_console_write, 698 .device = uart_console_device, 699 .setup = univ8250_console_setup, 700 .exit = univ8250_console_exit, 701 .match = univ8250_console_match, 702 .flags = CON_PRINTBUFFER | CON_ANYTIME, 703 .index = -1, 704 .data = &serial8250_reg, 705 }; 706 707 static int __init univ8250_console_init(void) 708 { 709 if (nr_uarts == 0) 710 return -ENODEV; 711 712 serial8250_isa_init_ports(); 713 register_console(&univ8250_console); 714 return 0; 715 } 716 console_initcall(univ8250_console_init); 717 718 #define SERIAL8250_CONSOLE (&univ8250_console) 719 #else 720 #define SERIAL8250_CONSOLE NULL 721 #endif 722 723 static struct uart_driver serial8250_reg = { 724 .owner = THIS_MODULE, 725 .driver_name = "serial", 726 .dev_name = "ttyS", 727 .major = TTY_MAJOR, 728 .minor = 64, 729 .cons = SERIAL8250_CONSOLE, 730 }; 731 732 /* 733 * early_serial_setup - early registration for 8250 ports 734 * 735 * Setup an 8250 port structure prior to console initialisation. Use 736 * after console initialisation will cause undefined behaviour. 737 */ 738 int __init early_serial_setup(struct uart_port *port) 739 { 740 struct uart_port *p; 741 742 if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0) 743 return -ENODEV; 744 745 serial8250_isa_init_ports(); 746 p = &serial8250_ports[port->line].port; 747 p->iobase = port->iobase; 748 p->membase = port->membase; 749 p->irq = port->irq; 750 p->irqflags = port->irqflags; 751 p->uartclk = port->uartclk; 752 p->fifosize = port->fifosize; 753 p->regshift = port->regshift; 754 p->iotype = port->iotype; 755 p->flags = port->flags; 756 p->mapbase = port->mapbase; 757 p->mapsize = port->mapsize; 758 p->private_data = port->private_data; 759 p->type = port->type; 760 p->line = port->line; 761 762 serial8250_set_defaults(up_to_u8250p(p)); 763 764 if (port->serial_in) 765 p->serial_in = port->serial_in; 766 if (port->serial_out) 767 p->serial_out = port->serial_out; 768 if (port->handle_irq) 769 p->handle_irq = port->handle_irq; 770 771 return 0; 772 } 773 774 /** 775 * serial8250_suspend_port - suspend one serial port 776 * @line: serial line number 777 * 778 * Suspend one serial port. 779 */ 780 void serial8250_suspend_port(int line) 781 { 782 struct uart_8250_port *up = &serial8250_ports[line]; 783 struct uart_port *port = &up->port; 784 785 if (!console_suspend_enabled && uart_console(port) && 786 port->type != PORT_8250) { 787 unsigned char canary = 0xa5; 788 789 serial_out(up, UART_SCR, canary); 790 if (serial_in(up, UART_SCR) == canary) 791 up->canary = canary; 792 } 793 794 uart_suspend_port(&serial8250_reg, port); 795 } 796 EXPORT_SYMBOL(serial8250_suspend_port); 797 798 /** 799 * serial8250_resume_port - resume one serial port 800 * @line: serial line number 801 * 802 * Resume one serial port. 803 */ 804 void serial8250_resume_port(int line) 805 { 806 struct uart_8250_port *up = &serial8250_ports[line]; 807 struct uart_port *port = &up->port; 808 809 up->canary = 0; 810 811 if (up->capabilities & UART_NATSEMI) { 812 /* Ensure it's still in high speed mode */ 813 serial_port_out(port, UART_LCR, 0xE0); 814 815 ns16550a_goto_highspeed(up); 816 817 serial_port_out(port, UART_LCR, 0); 818 port->uartclk = 921600*16; 819 } 820 uart_resume_port(&serial8250_reg, port); 821 } 822 EXPORT_SYMBOL(serial8250_resume_port); 823 824 /* 825 * Register a set of serial devices attached to a platform device. The 826 * list is terminated with a zero flags entry, which means we expect 827 * all entries to have at least UPF_BOOT_AUTOCONF set. 828 */ 829 static int serial8250_probe(struct platform_device *dev) 830 { 831 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev); 832 struct uart_8250_port uart; 833 int ret, i, irqflag = 0; 834 835 memset(&uart, 0, sizeof(uart)); 836 837 if (share_irqs) 838 irqflag = IRQF_SHARED; 839 840 for (i = 0; p && p->flags != 0; p++, i++) { 841 uart.port.iobase = p->iobase; 842 uart.port.membase = p->membase; 843 uart.port.irq = p->irq; 844 uart.port.irqflags = p->irqflags; 845 uart.port.uartclk = p->uartclk; 846 uart.port.regshift = p->regshift; 847 uart.port.iotype = p->iotype; 848 uart.port.flags = p->flags; 849 uart.port.mapbase = p->mapbase; 850 uart.port.mapsize = p->mapsize; 851 uart.port.hub6 = p->hub6; 852 uart.port.has_sysrq = p->has_sysrq; 853 uart.port.private_data = p->private_data; 854 uart.port.type = p->type; 855 uart.bugs = p->bugs; 856 uart.port.serial_in = p->serial_in; 857 uart.port.serial_out = p->serial_out; 858 uart.dl_read = p->dl_read; 859 uart.dl_write = p->dl_write; 860 uart.port.handle_irq = p->handle_irq; 861 uart.port.handle_break = p->handle_break; 862 uart.port.set_termios = p->set_termios; 863 uart.port.set_ldisc = p->set_ldisc; 864 uart.port.get_mctrl = p->get_mctrl; 865 uart.port.pm = p->pm; 866 uart.port.dev = &dev->dev; 867 uart.port.irqflags |= irqflag; 868 ret = serial8250_register_8250_port(&uart); 869 if (ret < 0) { 870 dev_err(&dev->dev, "unable to register port at index %d " 871 "(IO%lx MEM%llx IRQ%d): %d\n", i, 872 p->iobase, (unsigned long long)p->mapbase, 873 p->irq, ret); 874 } 875 } 876 return 0; 877 } 878 879 /* 880 * Remove serial ports registered against a platform device. 881 */ 882 static void serial8250_remove(struct platform_device *dev) 883 { 884 int i; 885 886 for (i = 0; i < nr_uarts; i++) { 887 struct uart_8250_port *up = &serial8250_ports[i]; 888 889 if (up->port.dev == &dev->dev) 890 serial8250_unregister_port(i); 891 } 892 } 893 894 static int serial8250_suspend(struct platform_device *dev, pm_message_t state) 895 { 896 int i; 897 898 for (i = 0; i < UART_NR; i++) { 899 struct uart_8250_port *up = &serial8250_ports[i]; 900 901 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev) 902 uart_suspend_port(&serial8250_reg, &up->port); 903 } 904 905 return 0; 906 } 907 908 static int serial8250_resume(struct platform_device *dev) 909 { 910 int i; 911 912 for (i = 0; i < UART_NR; i++) { 913 struct uart_8250_port *up = &serial8250_ports[i]; 914 915 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev) 916 serial8250_resume_port(i); 917 } 918 919 return 0; 920 } 921 922 static struct platform_driver serial8250_isa_driver = { 923 .probe = serial8250_probe, 924 .remove_new = serial8250_remove, 925 .suspend = serial8250_suspend, 926 .resume = serial8250_resume, 927 .driver = { 928 .name = "serial8250", 929 }, 930 }; 931 932 /* 933 * This "device" covers _all_ ISA 8250-compatible serial devices listed 934 * in the table in include/asm/serial.h 935 */ 936 static struct platform_device *serial8250_isa_devs; 937 938 /* 939 * serial8250_register_8250_port and serial8250_unregister_port allows for 940 * 16x50 serial ports to be configured at run-time, to support PCMCIA 941 * modems and PCI multiport cards. 942 */ 943 static DEFINE_MUTEX(serial_mutex); 944 945 static struct uart_8250_port *serial8250_find_match_or_unused(const struct uart_port *port) 946 { 947 int i; 948 949 /* 950 * First, find a port entry which matches. 951 */ 952 for (i = 0; i < nr_uarts; i++) 953 if (uart_match_port(&serial8250_ports[i].port, port)) 954 return &serial8250_ports[i]; 955 956 /* try line number first if still available */ 957 i = port->line; 958 if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN && 959 serial8250_ports[i].port.iobase == 0) 960 return &serial8250_ports[i]; 961 /* 962 * We didn't find a matching entry, so look for the first 963 * free entry. We look for one which hasn't been previously 964 * used (indicated by zero iobase). 965 */ 966 for (i = 0; i < nr_uarts; i++) 967 if (serial8250_ports[i].port.type == PORT_UNKNOWN && 968 serial8250_ports[i].port.iobase == 0) 969 return &serial8250_ports[i]; 970 971 /* 972 * That also failed. Last resort is to find any entry which 973 * doesn't have a real port associated with it. 974 */ 975 for (i = 0; i < nr_uarts; i++) 976 if (serial8250_ports[i].port.type == PORT_UNKNOWN) 977 return &serial8250_ports[i]; 978 979 return NULL; 980 } 981 982 static void serial_8250_overrun_backoff_work(struct work_struct *work) 983 { 984 struct uart_8250_port *up = 985 container_of(to_delayed_work(work), struct uart_8250_port, 986 overrun_backoff); 987 struct uart_port *port = &up->port; 988 unsigned long flags; 989 990 uart_port_lock_irqsave(port, &flags); 991 up->ier |= UART_IER_RLSI | UART_IER_RDI; 992 up->port.read_status_mask |= UART_LSR_DR; 993 serial_out(up, UART_IER, up->ier); 994 uart_port_unlock_irqrestore(port, flags); 995 } 996 997 /** 998 * serial8250_register_8250_port - register a serial port 999 * @up: serial port template 1000 * 1001 * Configure the serial port specified by the request. If the 1002 * port exists and is in use, it is hung up and unregistered 1003 * first. 1004 * 1005 * The port is then probed and if necessary the IRQ is autodetected 1006 * If this fails an error is returned. 1007 * 1008 * On success the port is ready to use and the line number is returned. 1009 */ 1010 int serial8250_register_8250_port(const struct uart_8250_port *up) 1011 { 1012 struct uart_8250_port *uart; 1013 int ret = -ENOSPC; 1014 1015 if (up->port.uartclk == 0) 1016 return -EINVAL; 1017 1018 mutex_lock(&serial_mutex); 1019 1020 uart = serial8250_find_match_or_unused(&up->port); 1021 if (!uart) { 1022 /* 1023 * If the port is past the initial isa ports, initialize a new 1024 * port and increment nr_uarts accordingly. 1025 */ 1026 uart = serial8250_setup_port(nr_uarts); 1027 if (!uart) 1028 goto unlock; 1029 nr_uarts++; 1030 } 1031 1032 if (uart->port.type != PORT_8250_CIR) { 1033 struct mctrl_gpios *gpios; 1034 1035 if (uart->port.dev) 1036 uart_remove_one_port(&serial8250_reg, &uart->port); 1037 1038 uart->port.ctrl_id = up->port.ctrl_id; 1039 uart->port.port_id = up->port.port_id; 1040 uart->port.iobase = up->port.iobase; 1041 uart->port.membase = up->port.membase; 1042 uart->port.irq = up->port.irq; 1043 uart->port.irqflags = up->port.irqflags; 1044 uart->port.uartclk = up->port.uartclk; 1045 uart->port.fifosize = up->port.fifosize; 1046 uart->port.regshift = up->port.regshift; 1047 uart->port.iotype = up->port.iotype; 1048 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF; 1049 uart->bugs = up->bugs; 1050 uart->port.mapbase = up->port.mapbase; 1051 uart->port.mapsize = up->port.mapsize; 1052 uart->port.private_data = up->port.private_data; 1053 uart->tx_loadsz = up->tx_loadsz; 1054 uart->capabilities = up->capabilities; 1055 uart->port.throttle = up->port.throttle; 1056 uart->port.unthrottle = up->port.unthrottle; 1057 uart->port.rs485_config = up->port.rs485_config; 1058 uart->port.rs485_supported = up->port.rs485_supported; 1059 uart->port.rs485 = up->port.rs485; 1060 uart->rs485_start_tx = up->rs485_start_tx; 1061 uart->rs485_stop_tx = up->rs485_stop_tx; 1062 uart->lsr_save_mask = up->lsr_save_mask; 1063 uart->dma = up->dma; 1064 1065 /* Take tx_loadsz from fifosize if it wasn't set separately */ 1066 if (uart->port.fifosize && !uart->tx_loadsz) 1067 uart->tx_loadsz = uart->port.fifosize; 1068 1069 if (up->port.dev) { 1070 uart->port.dev = up->port.dev; 1071 ret = uart_get_rs485_mode(&uart->port); 1072 if (ret) 1073 goto err; 1074 } 1075 1076 if (up->port.flags & UPF_FIXED_TYPE) 1077 uart->port.type = up->port.type; 1078 1079 /* 1080 * Only call mctrl_gpio_init(), if the device has no ACPI 1081 * companion device 1082 */ 1083 if (!has_acpi_companion(uart->port.dev)) { 1084 gpios = mctrl_gpio_init(&uart->port, 0); 1085 if (IS_ERR(gpios)) { 1086 ret = PTR_ERR(gpios); 1087 goto err; 1088 } else { 1089 uart->gpios = gpios; 1090 } 1091 } 1092 1093 serial8250_set_defaults(uart); 1094 1095 /* Possibly override default I/O functions. */ 1096 if (up->port.serial_in) 1097 uart->port.serial_in = up->port.serial_in; 1098 if (up->port.serial_out) 1099 uart->port.serial_out = up->port.serial_out; 1100 if (up->port.handle_irq) 1101 uart->port.handle_irq = up->port.handle_irq; 1102 /* Possibly override set_termios call */ 1103 if (up->port.set_termios) 1104 uart->port.set_termios = up->port.set_termios; 1105 if (up->port.set_ldisc) 1106 uart->port.set_ldisc = up->port.set_ldisc; 1107 if (up->port.get_mctrl) 1108 uart->port.get_mctrl = up->port.get_mctrl; 1109 if (up->port.set_mctrl) 1110 uart->port.set_mctrl = up->port.set_mctrl; 1111 if (up->port.get_divisor) 1112 uart->port.get_divisor = up->port.get_divisor; 1113 if (up->port.set_divisor) 1114 uart->port.set_divisor = up->port.set_divisor; 1115 if (up->port.startup) 1116 uart->port.startup = up->port.startup; 1117 if (up->port.shutdown) 1118 uart->port.shutdown = up->port.shutdown; 1119 if (up->port.pm) 1120 uart->port.pm = up->port.pm; 1121 if (up->port.handle_break) 1122 uart->port.handle_break = up->port.handle_break; 1123 if (up->dl_read) 1124 uart->dl_read = up->dl_read; 1125 if (up->dl_write) 1126 uart->dl_write = up->dl_write; 1127 1128 if (uart->port.type != PORT_8250_CIR) { 1129 if (serial8250_isa_config != NULL) 1130 serial8250_isa_config(0, &uart->port, 1131 &uart->capabilities); 1132 1133 serial8250_apply_quirks(uart); 1134 ret = uart_add_one_port(&serial8250_reg, 1135 &uart->port); 1136 if (ret) 1137 goto err; 1138 1139 ret = uart->port.line; 1140 } else { 1141 dev_info(uart->port.dev, 1142 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n", 1143 uart->port.iobase, 1144 (unsigned long long)uart->port.mapbase, 1145 uart->port.irq); 1146 1147 ret = 0; 1148 } 1149 1150 if (!uart->lsr_save_mask) 1151 uart->lsr_save_mask = LSR_SAVE_FLAGS; /* Use default LSR mask */ 1152 1153 /* Initialise interrupt backoff work if required */ 1154 if (up->overrun_backoff_time_ms > 0) { 1155 uart->overrun_backoff_time_ms = 1156 up->overrun_backoff_time_ms; 1157 INIT_DELAYED_WORK(&uart->overrun_backoff, 1158 serial_8250_overrun_backoff_work); 1159 } else { 1160 uart->overrun_backoff_time_ms = 0; 1161 } 1162 } 1163 1164 unlock: 1165 mutex_unlock(&serial_mutex); 1166 1167 return ret; 1168 1169 err: 1170 uart->port.dev = NULL; 1171 mutex_unlock(&serial_mutex); 1172 return ret; 1173 } 1174 EXPORT_SYMBOL(serial8250_register_8250_port); 1175 1176 /** 1177 * serial8250_unregister_port - remove a 16x50 serial port at runtime 1178 * @line: serial line number 1179 * 1180 * Remove one serial port. This may not be called from interrupt 1181 * context. We hand the port back to the our control. 1182 */ 1183 void serial8250_unregister_port(int line) 1184 { 1185 struct uart_8250_port *uart = &serial8250_ports[line]; 1186 1187 mutex_lock(&serial_mutex); 1188 1189 if (uart->em485) { 1190 unsigned long flags; 1191 1192 uart_port_lock_irqsave(&uart->port, &flags); 1193 serial8250_em485_destroy(uart); 1194 uart_port_unlock_irqrestore(&uart->port, flags); 1195 } 1196 1197 uart_remove_one_port(&serial8250_reg, &uart->port); 1198 if (serial8250_isa_devs) { 1199 uart->port.flags &= ~UPF_BOOT_AUTOCONF; 1200 uart->port.type = PORT_UNKNOWN; 1201 uart->port.dev = &serial8250_isa_devs->dev; 1202 uart->port.port_id = line; 1203 uart->capabilities = 0; 1204 serial8250_init_port(uart); 1205 serial8250_apply_quirks(uart); 1206 uart_add_one_port(&serial8250_reg, &uart->port); 1207 } else { 1208 uart->port.dev = NULL; 1209 } 1210 mutex_unlock(&serial_mutex); 1211 } 1212 EXPORT_SYMBOL(serial8250_unregister_port); 1213 1214 static int __init serial8250_init(void) 1215 { 1216 int ret; 1217 1218 if (nr_uarts == 0) 1219 return -ENODEV; 1220 1221 serial8250_isa_init_ports(); 1222 1223 pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %s\n", 1224 nr_uarts, str_enabled_disabled(share_irqs)); 1225 1226 #ifdef CONFIG_SPARC 1227 ret = sunserial_register_minors(&serial8250_reg, UART_NR); 1228 #else 1229 serial8250_reg.nr = UART_NR; 1230 ret = uart_register_driver(&serial8250_reg); 1231 #endif 1232 if (ret) 1233 goto out; 1234 1235 ret = serial8250_pnp_init(); 1236 if (ret) 1237 goto unreg_uart_drv; 1238 1239 serial8250_isa_devs = platform_device_alloc("serial8250", 1240 PLAT8250_DEV_LEGACY); 1241 if (!serial8250_isa_devs) { 1242 ret = -ENOMEM; 1243 goto unreg_pnp; 1244 } 1245 1246 ret = platform_device_add(serial8250_isa_devs); 1247 if (ret) 1248 goto put_dev; 1249 1250 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev); 1251 1252 ret = platform_driver_register(&serial8250_isa_driver); 1253 if (ret == 0) 1254 goto out; 1255 1256 platform_device_del(serial8250_isa_devs); 1257 put_dev: 1258 platform_device_put(serial8250_isa_devs); 1259 unreg_pnp: 1260 serial8250_pnp_exit(); 1261 unreg_uart_drv: 1262 #ifdef CONFIG_SPARC 1263 sunserial_unregister_minors(&serial8250_reg, UART_NR); 1264 #else 1265 uart_unregister_driver(&serial8250_reg); 1266 #endif 1267 out: 1268 return ret; 1269 } 1270 1271 static void __exit serial8250_exit(void) 1272 { 1273 struct platform_device *isa_dev = serial8250_isa_devs; 1274 1275 /* 1276 * This tells serial8250_unregister_port() not to re-register 1277 * the ports (thereby making serial8250_isa_driver permanently 1278 * in use.) 1279 */ 1280 serial8250_isa_devs = NULL; 1281 1282 platform_driver_unregister(&serial8250_isa_driver); 1283 platform_device_unregister(isa_dev); 1284 1285 serial8250_pnp_exit(); 1286 1287 #ifdef CONFIG_SPARC 1288 sunserial_unregister_minors(&serial8250_reg, UART_NR); 1289 #else 1290 uart_unregister_driver(&serial8250_reg); 1291 #endif 1292 } 1293 1294 module_init(serial8250_init); 1295 module_exit(serial8250_exit); 1296 1297 MODULE_LICENSE("GPL"); 1298 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver"); 1299 1300 module_param_hw(share_irqs, uint, other, 0644); 1301 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)"); 1302 1303 module_param(nr_uarts, uint, 0644); 1304 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")"); 1305 1306 module_param(skip_txen_test, uint, 0644); 1307 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time"); 1308 1309 #ifdef CONFIG_SERIAL_8250_RSA 1310 module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444); 1311 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA"); 1312 #endif 1313 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR); 1314 1315 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS 1316 #ifndef MODULE 1317 /* This module was renamed to 8250_core in 3.7. Keep the old "8250" name 1318 * working as well for the module options so we don't break people. We 1319 * need to keep the names identical and the convenient macros will happily 1320 * refuse to let us do that by failing the build with redefinition errors 1321 * of global variables. So we stick them inside a dummy function to avoid 1322 * those conflicts. The options still get parsed, and the redefined 1323 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive. 1324 * 1325 * This is hacky. I'm sorry. 1326 */ 1327 static void __used s8250_options(void) 1328 { 1329 #undef MODULE_PARAM_PREFIX 1330 #define MODULE_PARAM_PREFIX "8250_core." 1331 1332 module_param_cb(share_irqs, ¶m_ops_uint, &share_irqs, 0644); 1333 module_param_cb(nr_uarts, ¶m_ops_uint, &nr_uarts, 0644); 1334 module_param_cb(skip_txen_test, ¶m_ops_uint, &skip_txen_test, 0644); 1335 #ifdef CONFIG_SERIAL_8250_RSA 1336 __module_param_call(MODULE_PARAM_PREFIX, probe_rsa, 1337 ¶m_array_ops, .arr = &__param_arr_probe_rsa, 1338 0444, -1, 0); 1339 #endif 1340 } 1341 #else 1342 MODULE_ALIAS("8250_core"); 1343 #endif 1344 #endif 1345