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