Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  *  Universal/legacy driver for 8250/16550-type serial ports
0004  *
0005  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
0006  *
0007  *  Copyright (C) 2001 Russell King.
0008  *
0009  *  Supports: ISA-compatible 8250/16550 ports
0010  *        PNP 8250/16550 ports
0011  *        early_serial_setup() ports
0012  *        userspace-configurable "phantom" ports
0013  *        "serial8250" platform devices
0014  *        serial8250_register_8250_port() ports
0015  */
0016 
0017 #include <linux/acpi.h>
0018 #include <linux/module.h>
0019 #include <linux/moduleparam.h>
0020 #include <linux/ioport.h>
0021 #include <linux/init.h>
0022 #include <linux/console.h>
0023 #include <linux/sysrq.h>
0024 #include <linux/delay.h>
0025 #include <linux/platform_device.h>
0026 #include <linux/pm_runtime.h>
0027 #include <linux/tty.h>
0028 #include <linux/ratelimit.h>
0029 #include <linux/tty_flip.h>
0030 #include <linux/serial.h>
0031 #include <linux/serial_8250.h>
0032 #include <linux/nmi.h>
0033 #include <linux/mutex.h>
0034 #include <linux/slab.h>
0035 #include <linux/uaccess.h>
0036 #include <linux/io.h>
0037 #ifdef CONFIG_SPARC
0038 #include <linux/sunserialcore.h>
0039 #endif
0040 
0041 #include <asm/irq.h>
0042 
0043 #include "8250.h"
0044 
0045 /*
0046  * Configuration:
0047  *   share_irqs - whether we pass IRQF_SHARED to request_irq().  This option
0048  *                is unsafe when used on edge-triggered interrupts.
0049  */
0050 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
0051 
0052 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
0053 
0054 static struct uart_driver serial8250_reg;
0055 
0056 static unsigned int skip_txen_test; /* force skip of txen test at init time */
0057 
0058 #define PASS_LIMIT  512
0059 
0060 #include <asm/serial.h>
0061 /*
0062  * SERIAL_PORT_DFNS tells us about built-in ports that have no
0063  * standard enumeration mechanism.   Platforms that can find all
0064  * serial ports via mechanisms like ACPI or PCI need not supply it.
0065  */
0066 #ifndef SERIAL_PORT_DFNS
0067 #define SERIAL_PORT_DFNS
0068 #endif
0069 
0070 static const struct old_serial_port old_serial_port[] = {
0071     SERIAL_PORT_DFNS /* defined in asm/serial.h */
0072 };
0073 
0074 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
0075 
0076 #ifdef CONFIG_SERIAL_8250_RSA
0077 
0078 #define PORT_RSA_MAX 4
0079 static unsigned long probe_rsa[PORT_RSA_MAX];
0080 static unsigned int probe_rsa_count;
0081 #endif /* CONFIG_SERIAL_8250_RSA  */
0082 
0083 struct irq_info {
0084     struct          hlist_node node;
0085     int         irq;
0086     spinlock_t      lock;   /* Protects list not the hash */
0087     struct list_head    *head;
0088 };
0089 
0090 #define NR_IRQ_HASH     32  /* Can be adjusted later */
0091 static struct hlist_head irq_lists[NR_IRQ_HASH];
0092 static DEFINE_MUTEX(hash_mutex);    /* Used to walk the hash */
0093 
0094 /*
0095  * This is the serial driver's interrupt routine.
0096  *
0097  * Arjan thinks the old way was overly complex, so it got simplified.
0098  * Alan disagrees, saying that need the complexity to handle the weird
0099  * nature of ISA shared interrupts.  (This is a special exception.)
0100  *
0101  * In order to handle ISA shared interrupts properly, we need to check
0102  * that all ports have been serviced, and therefore the ISA interrupt
0103  * line has been de-asserted.
0104  *
0105  * This means we need to loop through all ports. checking that they
0106  * don't have an interrupt pending.
0107  */
0108 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
0109 {
0110     struct irq_info *i = dev_id;
0111     struct list_head *l, *end = NULL;
0112     int pass_counter = 0, handled = 0;
0113 
0114     pr_debug("%s(%d): start\n", __func__, irq);
0115 
0116     spin_lock(&i->lock);
0117 
0118     l = i->head;
0119     do {
0120         struct uart_8250_port *up;
0121         struct uart_port *port;
0122 
0123         up = list_entry(l, struct uart_8250_port, list);
0124         port = &up->port;
0125 
0126         if (port->handle_irq(port)) {
0127             handled = 1;
0128             end = NULL;
0129         } else if (end == NULL)
0130             end = l;
0131 
0132         l = l->next;
0133 
0134         if (l == i->head && pass_counter++ > PASS_LIMIT)
0135             break;
0136     } while (l != end);
0137 
0138     spin_unlock(&i->lock);
0139 
0140     pr_debug("%s(%d): end\n", __func__, irq);
0141 
0142     return IRQ_RETVAL(handled);
0143 }
0144 
0145 /*
0146  * To support ISA shared interrupts, we need to have one interrupt
0147  * handler that ensures that the IRQ line has been deasserted
0148  * before returning.  Failing to do this will result in the IRQ
0149  * line being stuck active, and, since ISA irqs are edge triggered,
0150  * no more IRQs will be seen.
0151  */
0152 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
0153 {
0154     spin_lock_irq(&i->lock);
0155 
0156     if (!list_empty(i->head)) {
0157         if (i->head == &up->list)
0158             i->head = i->head->next;
0159         list_del(&up->list);
0160     } else {
0161         BUG_ON(i->head != &up->list);
0162         i->head = NULL;
0163     }
0164     spin_unlock_irq(&i->lock);
0165     /* List empty so throw away the hash node */
0166     if (i->head == NULL) {
0167         hlist_del(&i->node);
0168         kfree(i);
0169     }
0170 }
0171 
0172 static int serial_link_irq_chain(struct uart_8250_port *up)
0173 {
0174     struct hlist_head *h;
0175     struct irq_info *i;
0176     int ret;
0177 
0178     mutex_lock(&hash_mutex);
0179 
0180     h = &irq_lists[up->port.irq % NR_IRQ_HASH];
0181 
0182     hlist_for_each_entry(i, h, node)
0183         if (i->irq == up->port.irq)
0184             break;
0185 
0186     if (i == NULL) {
0187         i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
0188         if (i == NULL) {
0189             mutex_unlock(&hash_mutex);
0190             return -ENOMEM;
0191         }
0192         spin_lock_init(&i->lock);
0193         i->irq = up->port.irq;
0194         hlist_add_head(&i->node, h);
0195     }
0196     mutex_unlock(&hash_mutex);
0197 
0198     spin_lock_irq(&i->lock);
0199 
0200     if (i->head) {
0201         list_add(&up->list, i->head);
0202         spin_unlock_irq(&i->lock);
0203 
0204         ret = 0;
0205     } else {
0206         INIT_LIST_HEAD(&up->list);
0207         i->head = &up->list;
0208         spin_unlock_irq(&i->lock);
0209         ret = request_irq(up->port.irq, serial8250_interrupt,
0210                   up->port.irqflags, up->port.name, i);
0211         if (ret < 0)
0212             serial_do_unlink(i, up);
0213     }
0214 
0215     return ret;
0216 }
0217 
0218 static void serial_unlink_irq_chain(struct uart_8250_port *up)
0219 {
0220     struct irq_info *i;
0221     struct hlist_head *h;
0222 
0223     mutex_lock(&hash_mutex);
0224 
0225     h = &irq_lists[up->port.irq % NR_IRQ_HASH];
0226 
0227     hlist_for_each_entry(i, h, node)
0228         if (i->irq == up->port.irq)
0229             break;
0230 
0231     BUG_ON(i == NULL);
0232     BUG_ON(i->head == NULL);
0233 
0234     if (list_empty(i->head))
0235         free_irq(up->port.irq, i);
0236 
0237     serial_do_unlink(i, up);
0238     mutex_unlock(&hash_mutex);
0239 }
0240 
0241 /*
0242  * This function is used to handle ports that do not have an
0243  * interrupt.  This doesn't work very well for 16450's, but gives
0244  * barely passable results for a 16550A.  (Although at the expense
0245  * of much CPU overhead).
0246  */
0247 static void serial8250_timeout(struct timer_list *t)
0248 {
0249     struct uart_8250_port *up = from_timer(up, t, timer);
0250 
0251     up->port.handle_irq(&up->port);
0252     mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
0253 }
0254 
0255 static void serial8250_backup_timeout(struct timer_list *t)
0256 {
0257     struct uart_8250_port *up = from_timer(up, t, timer);
0258     unsigned int iir, ier = 0, lsr;
0259     unsigned long flags;
0260 
0261     spin_lock_irqsave(&up->port.lock, flags);
0262 
0263     /*
0264      * Must disable interrupts or else we risk racing with the interrupt
0265      * based handler.
0266      */
0267     if (up->port.irq) {
0268         ier = serial_in(up, UART_IER);
0269         serial_out(up, UART_IER, 0);
0270     }
0271 
0272     iir = serial_in(up, UART_IIR);
0273 
0274     /*
0275      * This should be a safe test for anyone who doesn't trust the
0276      * IIR bits on their UART, but it's specifically designed for
0277      * the "Diva" UART used on the management processor on many HP
0278      * ia64 and parisc boxes.
0279      */
0280     lsr = serial_lsr_in(up);
0281     if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
0282         (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
0283         (lsr & UART_LSR_THRE)) {
0284         iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
0285         iir |= UART_IIR_THRI;
0286     }
0287 
0288     if (!(iir & UART_IIR_NO_INT))
0289         serial8250_tx_chars(up);
0290 
0291     if (up->port.irq)
0292         serial_out(up, UART_IER, ier);
0293 
0294     spin_unlock_irqrestore(&up->port.lock, flags);
0295 
0296     /* Standard timer interval plus 0.2s to keep the port running */
0297     mod_timer(&up->timer,
0298         jiffies + uart_poll_timeout(&up->port) + HZ / 5);
0299 }
0300 
0301 static int univ8250_setup_irq(struct uart_8250_port *up)
0302 {
0303     struct uart_port *port = &up->port;
0304     int retval = 0;
0305 
0306     /*
0307      * The above check will only give an accurate result the first time
0308      * the port is opened so this value needs to be preserved.
0309      */
0310     if (up->bugs & UART_BUG_THRE) {
0311         pr_debug("%s - using backup timer\n", port->name);
0312 
0313         up->timer.function = serial8250_backup_timeout;
0314         mod_timer(&up->timer, jiffies +
0315               uart_poll_timeout(port) + HZ / 5);
0316     }
0317 
0318     /*
0319      * If the "interrupt" for this port doesn't correspond with any
0320      * hardware interrupt, we use a timer-based system.  The original
0321      * driver used to do this with IRQ0.
0322      */
0323     if (!port->irq)
0324         mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
0325     else
0326         retval = serial_link_irq_chain(up);
0327 
0328     return retval;
0329 }
0330 
0331 static void univ8250_release_irq(struct uart_8250_port *up)
0332 {
0333     struct uart_port *port = &up->port;
0334 
0335     del_timer_sync(&up->timer);
0336     up->timer.function = serial8250_timeout;
0337     if (port->irq)
0338         serial_unlink_irq_chain(up);
0339 }
0340 
0341 #ifdef CONFIG_SERIAL_8250_RSA
0342 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
0343 {
0344     unsigned long start = UART_RSA_BASE << up->port.regshift;
0345     unsigned int size = 8 << up->port.regshift;
0346     struct uart_port *port = &up->port;
0347     int ret = -EINVAL;
0348 
0349     switch (port->iotype) {
0350     case UPIO_HUB6:
0351     case UPIO_PORT:
0352         start += port->iobase;
0353         if (request_region(start, size, "serial-rsa"))
0354             ret = 0;
0355         else
0356             ret = -EBUSY;
0357         break;
0358     }
0359 
0360     return ret;
0361 }
0362 
0363 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
0364 {
0365     unsigned long offset = UART_RSA_BASE << up->port.regshift;
0366     unsigned int size = 8 << up->port.regshift;
0367     struct uart_port *port = &up->port;
0368 
0369     switch (port->iotype) {
0370     case UPIO_HUB6:
0371     case UPIO_PORT:
0372         release_region(port->iobase + offset, size);
0373         break;
0374     }
0375 }
0376 #endif
0377 
0378 static const struct uart_ops *base_ops;
0379 static struct uart_ops univ8250_port_ops;
0380 
0381 static const struct uart_8250_ops univ8250_driver_ops = {
0382     .setup_irq  = univ8250_setup_irq,
0383     .release_irq    = univ8250_release_irq,
0384 };
0385 
0386 static struct uart_8250_port serial8250_ports[UART_NR];
0387 
0388 /**
0389  * serial8250_get_port - retrieve struct uart_8250_port
0390  * @line: serial line number
0391  *
0392  * This function retrieves struct uart_8250_port for the specific line.
0393  * This struct *must* *not* be used to perform a 8250 or serial core operation
0394  * which is not accessible otherwise. Its only purpose is to make the struct
0395  * accessible to the runtime-pm callbacks for context suspend/restore.
0396  * The lock assumption made here is none because runtime-pm suspend/resume
0397  * callbacks should not be invoked if there is any operation performed on the
0398  * port.
0399  */
0400 struct uart_8250_port *serial8250_get_port(int line)
0401 {
0402     return &serial8250_ports[line];
0403 }
0404 EXPORT_SYMBOL_GPL(serial8250_get_port);
0405 
0406 static void (*serial8250_isa_config)(int port, struct uart_port *up,
0407     u32 *capabilities);
0408 
0409 void serial8250_set_isa_configurator(
0410     void (*v)(int port, struct uart_port *up, u32 *capabilities))
0411 {
0412     serial8250_isa_config = v;
0413 }
0414 EXPORT_SYMBOL(serial8250_set_isa_configurator);
0415 
0416 #ifdef CONFIG_SERIAL_8250_RSA
0417 
0418 static void univ8250_config_port(struct uart_port *port, int flags)
0419 {
0420     struct uart_8250_port *up = up_to_u8250p(port);
0421 
0422     up->probe &= ~UART_PROBE_RSA;
0423     if (port->type == PORT_RSA) {
0424         if (serial8250_request_rsa_resource(up) == 0)
0425             up->probe |= UART_PROBE_RSA;
0426     } else if (flags & UART_CONFIG_TYPE) {
0427         int i;
0428 
0429         for (i = 0; i < probe_rsa_count; i++) {
0430             if (probe_rsa[i] == up->port.iobase) {
0431                 if (serial8250_request_rsa_resource(up) == 0)
0432                     up->probe |= UART_PROBE_RSA;
0433                 break;
0434             }
0435         }
0436     }
0437 
0438     base_ops->config_port(port, flags);
0439 
0440     if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
0441         serial8250_release_rsa_resource(up);
0442 }
0443 
0444 static int univ8250_request_port(struct uart_port *port)
0445 {
0446     struct uart_8250_port *up = up_to_u8250p(port);
0447     int ret;
0448 
0449     ret = base_ops->request_port(port);
0450     if (ret == 0 && port->type == PORT_RSA) {
0451         ret = serial8250_request_rsa_resource(up);
0452         if (ret < 0)
0453             base_ops->release_port(port);
0454     }
0455 
0456     return ret;
0457 }
0458 
0459 static void univ8250_release_port(struct uart_port *port)
0460 {
0461     struct uart_8250_port *up = up_to_u8250p(port);
0462 
0463     if (port->type == PORT_RSA)
0464         serial8250_release_rsa_resource(up);
0465     base_ops->release_port(port);
0466 }
0467 
0468 static void univ8250_rsa_support(struct uart_ops *ops)
0469 {
0470     ops->config_port  = univ8250_config_port;
0471     ops->request_port = univ8250_request_port;
0472     ops->release_port = univ8250_release_port;
0473 }
0474 
0475 #else
0476 #define univ8250_rsa_support(x)     do { } while (0)
0477 #endif /* CONFIG_SERIAL_8250_RSA */
0478 
0479 static inline void serial8250_apply_quirks(struct uart_8250_port *up)
0480 {
0481     up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0;
0482 }
0483 
0484 static void __init serial8250_isa_init_ports(void)
0485 {
0486     struct uart_8250_port *up;
0487     static int first = 1;
0488     int i, irqflag = 0;
0489 
0490     if (!first)
0491         return;
0492     first = 0;
0493 
0494     if (nr_uarts > UART_NR)
0495         nr_uarts = UART_NR;
0496 
0497     for (i = 0; i < nr_uarts; i++) {
0498         struct uart_8250_port *up = &serial8250_ports[i];
0499         struct uart_port *port = &up->port;
0500 
0501         port->line = i;
0502         serial8250_init_port(up);
0503         if (!base_ops)
0504             base_ops = port->ops;
0505         port->ops = &univ8250_port_ops;
0506 
0507         timer_setup(&up->timer, serial8250_timeout, 0);
0508 
0509         up->ops = &univ8250_driver_ops;
0510 
0511         if (IS_ENABLED(CONFIG_ALPHA_JENSEN) ||
0512             (IS_ENABLED(CONFIG_ALPHA_GENERIC) && alpha_jensen()))
0513             port->set_mctrl = alpha_jensen_set_mctrl;
0514 
0515         serial8250_set_defaults(up);
0516     }
0517 
0518     /* chain base port ops to support Remote Supervisor Adapter */
0519     univ8250_port_ops = *base_ops;
0520     univ8250_rsa_support(&univ8250_port_ops);
0521 
0522     if (share_irqs)
0523         irqflag = IRQF_SHARED;
0524 
0525     for (i = 0, up = serial8250_ports;
0526          i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
0527          i++, up++) {
0528         struct uart_port *port = &up->port;
0529 
0530         port->iobase   = old_serial_port[i].port;
0531         port->irq      = irq_canonicalize(old_serial_port[i].irq);
0532         port->irqflags = 0;
0533         port->uartclk  = old_serial_port[i].baud_base * 16;
0534         port->flags    = old_serial_port[i].flags;
0535         port->hub6     = 0;
0536         port->membase  = old_serial_port[i].iomem_base;
0537         port->iotype   = old_serial_port[i].io_type;
0538         port->regshift = old_serial_port[i].iomem_reg_shift;
0539 
0540         port->irqflags |= irqflag;
0541         if (serial8250_isa_config != NULL)
0542             serial8250_isa_config(i, &up->port, &up->capabilities);
0543     }
0544 }
0545 
0546 static void __init
0547 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
0548 {
0549     int i;
0550 
0551     for (i = 0; i < nr_uarts; i++) {
0552         struct uart_8250_port *up = &serial8250_ports[i];
0553 
0554         if (up->port.type == PORT_8250_CIR)
0555             continue;
0556 
0557         if (up->port.dev)
0558             continue;
0559 
0560         up->port.dev = dev;
0561 
0562         if (uart_console_enabled(&up->port))
0563             pm_runtime_get_sync(up->port.dev);
0564 
0565         serial8250_apply_quirks(up);
0566         uart_add_one_port(drv, &up->port);
0567     }
0568 }
0569 
0570 #ifdef CONFIG_SERIAL_8250_CONSOLE
0571 
0572 static void univ8250_console_write(struct console *co, const char *s,
0573                    unsigned int count)
0574 {
0575     struct uart_8250_port *up = &serial8250_ports[co->index];
0576 
0577     serial8250_console_write(up, s, count);
0578 }
0579 
0580 static int univ8250_console_setup(struct console *co, char *options)
0581 {
0582     struct uart_port *port;
0583     int retval;
0584 
0585     /*
0586      * Check whether an invalid uart number has been specified, and
0587      * if so, search for the first available port that does have
0588      * console support.
0589      */
0590     if (co->index >= nr_uarts)
0591         co->index = 0;
0592     port = &serial8250_ports[co->index].port;
0593     /* link port to console */
0594     port->cons = co;
0595 
0596     retval = serial8250_console_setup(port, options, false);
0597     if (retval != 0)
0598         port->cons = NULL;
0599     return retval;
0600 }
0601 
0602 static int univ8250_console_exit(struct console *co)
0603 {
0604     struct uart_port *port;
0605 
0606     port = &serial8250_ports[co->index].port;
0607     return serial8250_console_exit(port);
0608 }
0609 
0610 /**
0611  *  univ8250_console_match - non-standard console matching
0612  *  @co:      registering console
0613  *  @name:    name from console command line
0614  *  @idx:     index from console command line
0615  *  @options: ptr to option string from console command line
0616  *
0617  *  Only attempts to match console command lines of the form:
0618  *      console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>]
0619  *      console=uart[8250],0x<addr>[,<options>]
0620  *  This form is used to register an initial earlycon boot console and
0621  *  replace it with the serial8250_console at 8250 driver init.
0622  *
0623  *  Performs console setup for a match (as required by interface)
0624  *  If no <options> are specified, then assume the h/w is already setup.
0625  *
0626  *  Returns 0 if console matches; otherwise non-zero to use default matching
0627  */
0628 static int univ8250_console_match(struct console *co, char *name, int idx,
0629                   char *options)
0630 {
0631     char match[] = "uart";  /* 8250-specific earlycon name */
0632     unsigned char iotype;
0633     resource_size_t addr;
0634     int i;
0635 
0636     if (strncmp(name, match, 4) != 0)
0637         return -ENODEV;
0638 
0639     if (uart_parse_earlycon(options, &iotype, &addr, &options))
0640         return -ENODEV;
0641 
0642     /* try to match the port specified on the command line */
0643     for (i = 0; i < nr_uarts; i++) {
0644         struct uart_port *port = &serial8250_ports[i].port;
0645 
0646         if (port->iotype != iotype)
0647             continue;
0648         if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
0649              iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
0650             && (port->mapbase != addr))
0651             continue;
0652         if (iotype == UPIO_PORT && port->iobase != addr)
0653             continue;
0654 
0655         co->index = i;
0656         port->cons = co;
0657         return serial8250_console_setup(port, options, true);
0658     }
0659 
0660     return -ENODEV;
0661 }
0662 
0663 static struct console univ8250_console = {
0664     .name       = "ttyS",
0665     .write      = univ8250_console_write,
0666     .device     = uart_console_device,
0667     .setup      = univ8250_console_setup,
0668     .exit       = univ8250_console_exit,
0669     .match      = univ8250_console_match,
0670     .flags      = CON_PRINTBUFFER | CON_ANYTIME,
0671     .index      = -1,
0672     .data       = &serial8250_reg,
0673 };
0674 
0675 static int __init univ8250_console_init(void)
0676 {
0677     if (nr_uarts == 0)
0678         return -ENODEV;
0679 
0680     serial8250_isa_init_ports();
0681     register_console(&univ8250_console);
0682     return 0;
0683 }
0684 console_initcall(univ8250_console_init);
0685 
0686 #define SERIAL8250_CONSOLE  (&univ8250_console)
0687 #else
0688 #define SERIAL8250_CONSOLE  NULL
0689 #endif
0690 
0691 static struct uart_driver serial8250_reg = {
0692     .owner          = THIS_MODULE,
0693     .driver_name        = "serial",
0694     .dev_name       = "ttyS",
0695     .major          = TTY_MAJOR,
0696     .minor          = 64,
0697     .cons           = SERIAL8250_CONSOLE,
0698 };
0699 
0700 /*
0701  * early_serial_setup - early registration for 8250 ports
0702  *
0703  * Setup an 8250 port structure prior to console initialisation.  Use
0704  * after console initialisation will cause undefined behaviour.
0705  */
0706 int __init early_serial_setup(struct uart_port *port)
0707 {
0708     struct uart_port *p;
0709 
0710     if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
0711         return -ENODEV;
0712 
0713     serial8250_isa_init_ports();
0714     p = &serial8250_ports[port->line].port;
0715     p->iobase       = port->iobase;
0716     p->membase      = port->membase;
0717     p->irq          = port->irq;
0718     p->irqflags     = port->irqflags;
0719     p->uartclk      = port->uartclk;
0720     p->fifosize     = port->fifosize;
0721     p->regshift     = port->regshift;
0722     p->iotype       = port->iotype;
0723     p->flags        = port->flags;
0724     p->mapbase      = port->mapbase;
0725     p->mapsize      = port->mapsize;
0726     p->private_data = port->private_data;
0727     p->type     = port->type;
0728     p->line     = port->line;
0729 
0730     serial8250_set_defaults(up_to_u8250p(p));
0731 
0732     if (port->serial_in)
0733         p->serial_in = port->serial_in;
0734     if (port->serial_out)
0735         p->serial_out = port->serial_out;
0736     if (port->handle_irq)
0737         p->handle_irq = port->handle_irq;
0738 
0739     return 0;
0740 }
0741 
0742 /**
0743  *  serial8250_suspend_port - suspend one serial port
0744  *  @line:  serial line number
0745  *
0746  *  Suspend one serial port.
0747  */
0748 void serial8250_suspend_port(int line)
0749 {
0750     struct uart_8250_port *up = &serial8250_ports[line];
0751     struct uart_port *port = &up->port;
0752 
0753     if (!console_suspend_enabled && uart_console(port) &&
0754         port->type != PORT_8250) {
0755         unsigned char canary = 0xa5;
0756 
0757         serial_out(up, UART_SCR, canary);
0758         if (serial_in(up, UART_SCR) == canary)
0759             up->canary = canary;
0760     }
0761 
0762     uart_suspend_port(&serial8250_reg, port);
0763 }
0764 EXPORT_SYMBOL(serial8250_suspend_port);
0765 
0766 /**
0767  *  serial8250_resume_port - resume one serial port
0768  *  @line:  serial line number
0769  *
0770  *  Resume one serial port.
0771  */
0772 void serial8250_resume_port(int line)
0773 {
0774     struct uart_8250_port *up = &serial8250_ports[line];
0775     struct uart_port *port = &up->port;
0776 
0777     up->canary = 0;
0778 
0779     if (up->capabilities & UART_NATSEMI) {
0780         /* Ensure it's still in high speed mode */
0781         serial_port_out(port, UART_LCR, 0xE0);
0782 
0783         ns16550a_goto_highspeed(up);
0784 
0785         serial_port_out(port, UART_LCR, 0);
0786         port->uartclk = 921600*16;
0787     }
0788     uart_resume_port(&serial8250_reg, port);
0789 }
0790 EXPORT_SYMBOL(serial8250_resume_port);
0791 
0792 /*
0793  * Register a set of serial devices attached to a platform device.  The
0794  * list is terminated with a zero flags entry, which means we expect
0795  * all entries to have at least UPF_BOOT_AUTOCONF set.
0796  */
0797 static int serial8250_probe(struct platform_device *dev)
0798 {
0799     struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
0800     struct uart_8250_port uart;
0801     int ret, i, irqflag = 0;
0802 
0803     memset(&uart, 0, sizeof(uart));
0804 
0805     if (share_irqs)
0806         irqflag = IRQF_SHARED;
0807 
0808     for (i = 0; p && p->flags != 0; p++, i++) {
0809         uart.port.iobase    = p->iobase;
0810         uart.port.membase   = p->membase;
0811         uart.port.irq       = p->irq;
0812         uart.port.irqflags  = p->irqflags;
0813         uart.port.uartclk   = p->uartclk;
0814         uart.port.regshift  = p->regshift;
0815         uart.port.iotype    = p->iotype;
0816         uart.port.flags     = p->flags;
0817         uart.port.mapbase   = p->mapbase;
0818         uart.port.hub6      = p->hub6;
0819         uart.port.has_sysrq = p->has_sysrq;
0820         uart.port.private_data  = p->private_data;
0821         uart.port.type      = p->type;
0822         uart.port.serial_in = p->serial_in;
0823         uart.port.serial_out    = p->serial_out;
0824         uart.port.handle_irq    = p->handle_irq;
0825         uart.port.handle_break  = p->handle_break;
0826         uart.port.set_termios   = p->set_termios;
0827         uart.port.set_ldisc = p->set_ldisc;
0828         uart.port.get_mctrl = p->get_mctrl;
0829         uart.port.pm        = p->pm;
0830         uart.port.dev       = &dev->dev;
0831         uart.port.irqflags  |= irqflag;
0832         ret = serial8250_register_8250_port(&uart);
0833         if (ret < 0) {
0834             dev_err(&dev->dev, "unable to register port at index %d "
0835                 "(IO%lx MEM%llx IRQ%d): %d\n", i,
0836                 p->iobase, (unsigned long long)p->mapbase,
0837                 p->irq, ret);
0838         }
0839     }
0840     return 0;
0841 }
0842 
0843 /*
0844  * Remove serial ports registered against a platform device.
0845  */
0846 static int serial8250_remove(struct platform_device *dev)
0847 {
0848     int i;
0849 
0850     for (i = 0; i < nr_uarts; i++) {
0851         struct uart_8250_port *up = &serial8250_ports[i];
0852 
0853         if (up->port.dev == &dev->dev)
0854             serial8250_unregister_port(i);
0855     }
0856     return 0;
0857 }
0858 
0859 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
0860 {
0861     int i;
0862 
0863     for (i = 0; i < UART_NR; i++) {
0864         struct uart_8250_port *up = &serial8250_ports[i];
0865 
0866         if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
0867             uart_suspend_port(&serial8250_reg, &up->port);
0868     }
0869 
0870     return 0;
0871 }
0872 
0873 static int serial8250_resume(struct platform_device *dev)
0874 {
0875     int i;
0876 
0877     for (i = 0; i < UART_NR; i++) {
0878         struct uart_8250_port *up = &serial8250_ports[i];
0879 
0880         if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
0881             serial8250_resume_port(i);
0882     }
0883 
0884     return 0;
0885 }
0886 
0887 static struct platform_driver serial8250_isa_driver = {
0888     .probe      = serial8250_probe,
0889     .remove     = serial8250_remove,
0890     .suspend    = serial8250_suspend,
0891     .resume     = serial8250_resume,
0892     .driver     = {
0893         .name   = "serial8250",
0894     },
0895 };
0896 
0897 /*
0898  * This "device" covers _all_ ISA 8250-compatible serial devices listed
0899  * in the table in include/asm/serial.h
0900  */
0901 static struct platform_device *serial8250_isa_devs;
0902 
0903 /*
0904  * serial8250_register_8250_port and serial8250_unregister_port allows for
0905  * 16x50 serial ports to be configured at run-time, to support PCMCIA
0906  * modems and PCI multiport cards.
0907  */
0908 static DEFINE_MUTEX(serial_mutex);
0909 
0910 static struct uart_8250_port *serial8250_find_match_or_unused(const struct uart_port *port)
0911 {
0912     int i;
0913 
0914     /*
0915      * First, find a port entry which matches.
0916      */
0917     for (i = 0; i < nr_uarts; i++)
0918         if (uart_match_port(&serial8250_ports[i].port, port))
0919             return &serial8250_ports[i];
0920 
0921     /* try line number first if still available */
0922     i = port->line;
0923     if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
0924             serial8250_ports[i].port.iobase == 0)
0925         return &serial8250_ports[i];
0926     /*
0927      * We didn't find a matching entry, so look for the first
0928      * free entry.  We look for one which hasn't been previously
0929      * used (indicated by zero iobase).
0930      */
0931     for (i = 0; i < nr_uarts; i++)
0932         if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
0933             serial8250_ports[i].port.iobase == 0)
0934             return &serial8250_ports[i];
0935 
0936     /*
0937      * That also failed.  Last resort is to find any entry which
0938      * doesn't have a real port associated with it.
0939      */
0940     for (i = 0; i < nr_uarts; i++)
0941         if (serial8250_ports[i].port.type == PORT_UNKNOWN)
0942             return &serial8250_ports[i];
0943 
0944     return NULL;
0945 }
0946 
0947 static void serial_8250_overrun_backoff_work(struct work_struct *work)
0948 {
0949     struct uart_8250_port *up =
0950         container_of(to_delayed_work(work), struct uart_8250_port,
0951              overrun_backoff);
0952     struct uart_port *port = &up->port;
0953     unsigned long flags;
0954 
0955     spin_lock_irqsave(&port->lock, flags);
0956     up->ier |= UART_IER_RLSI | UART_IER_RDI;
0957     up->port.read_status_mask |= UART_LSR_DR;
0958     serial_out(up, UART_IER, up->ier);
0959     spin_unlock_irqrestore(&port->lock, flags);
0960 }
0961 
0962 /**
0963  *  serial8250_register_8250_port - register a serial port
0964  *  @up: serial port template
0965  *
0966  *  Configure the serial port specified by the request. If the
0967  *  port exists and is in use, it is hung up and unregistered
0968  *  first.
0969  *
0970  *  The port is then probed and if necessary the IRQ is autodetected
0971  *  If this fails an error is returned.
0972  *
0973  *  On success the port is ready to use and the line number is returned.
0974  */
0975 int serial8250_register_8250_port(const struct uart_8250_port *up)
0976 {
0977     struct uart_8250_port *uart;
0978     int ret = -ENOSPC;
0979 
0980     if (up->port.uartclk == 0)
0981         return -EINVAL;
0982 
0983     mutex_lock(&serial_mutex);
0984 
0985     uart = serial8250_find_match_or_unused(&up->port);
0986     if (uart && uart->port.type != PORT_8250_CIR) {
0987         struct mctrl_gpios *gpios;
0988 
0989         if (uart->port.dev)
0990             uart_remove_one_port(&serial8250_reg, &uart->port);
0991 
0992         uart->port.iobase       = up->port.iobase;
0993         uart->port.membase      = up->port.membase;
0994         uart->port.irq          = up->port.irq;
0995         uart->port.irqflags     = up->port.irqflags;
0996         uart->port.uartclk      = up->port.uartclk;
0997         uart->port.fifosize     = up->port.fifosize;
0998         uart->port.regshift     = up->port.regshift;
0999         uart->port.iotype       = up->port.iotype;
1000         uart->port.flags        = up->port.flags | UPF_BOOT_AUTOCONF;
1001         uart->bugs      = up->bugs;
1002         uart->port.mapbase      = up->port.mapbase;
1003         uart->port.mapsize      = up->port.mapsize;
1004         uart->port.private_data = up->port.private_data;
1005         uart->tx_loadsz     = up->tx_loadsz;
1006         uart->capabilities  = up->capabilities;
1007         uart->port.throttle = up->port.throttle;
1008         uart->port.unthrottle   = up->port.unthrottle;
1009         uart->port.rs485_config = up->port.rs485_config;
1010         uart->port.rs485_supported = up->port.rs485_supported;
1011         uart->port.rs485    = up->port.rs485;
1012         uart->rs485_start_tx    = up->rs485_start_tx;
1013         uart->rs485_stop_tx = up->rs485_stop_tx;
1014         uart->lsr_save_mask = up->lsr_save_mask;
1015         uart->dma       = up->dma;
1016 
1017         /* Take tx_loadsz from fifosize if it wasn't set separately */
1018         if (uart->port.fifosize && !uart->tx_loadsz)
1019             uart->tx_loadsz = uart->port.fifosize;
1020 
1021         if (up->port.dev) {
1022             uart->port.dev = up->port.dev;
1023             ret = uart_get_rs485_mode(&uart->port);
1024             if (ret)
1025                 goto err;
1026         }
1027 
1028         if (up->port.flags & UPF_FIXED_TYPE)
1029             uart->port.type = up->port.type;
1030 
1031         /*
1032          * Only call mctrl_gpio_init(), if the device has no ACPI
1033          * companion device
1034          */
1035         if (!has_acpi_companion(uart->port.dev)) {
1036             gpios = mctrl_gpio_init(&uart->port, 0);
1037             if (IS_ERR(gpios)) {
1038                 ret = PTR_ERR(gpios);
1039                 goto err;
1040             } else {
1041                 uart->gpios = gpios;
1042             }
1043         }
1044 
1045         serial8250_set_defaults(uart);
1046 
1047         /* Possibly override default I/O functions.  */
1048         if (up->port.serial_in)
1049             uart->port.serial_in = up->port.serial_in;
1050         if (up->port.serial_out)
1051             uart->port.serial_out = up->port.serial_out;
1052         if (up->port.handle_irq)
1053             uart->port.handle_irq = up->port.handle_irq;
1054         /*  Possibly override set_termios call */
1055         if (up->port.set_termios)
1056             uart->port.set_termios = up->port.set_termios;
1057         if (up->port.set_ldisc)
1058             uart->port.set_ldisc = up->port.set_ldisc;
1059         if (up->port.get_mctrl)
1060             uart->port.get_mctrl = up->port.get_mctrl;
1061         if (up->port.set_mctrl)
1062             uart->port.set_mctrl = up->port.set_mctrl;
1063         if (up->port.get_divisor)
1064             uart->port.get_divisor = up->port.get_divisor;
1065         if (up->port.set_divisor)
1066             uart->port.set_divisor = up->port.set_divisor;
1067         if (up->port.startup)
1068             uart->port.startup = up->port.startup;
1069         if (up->port.shutdown)
1070             uart->port.shutdown = up->port.shutdown;
1071         if (up->port.pm)
1072             uart->port.pm = up->port.pm;
1073         if (up->port.handle_break)
1074             uart->port.handle_break = up->port.handle_break;
1075         if (up->dl_read)
1076             uart->dl_read = up->dl_read;
1077         if (up->dl_write)
1078             uart->dl_write = up->dl_write;
1079 
1080         if (uart->port.type != PORT_8250_CIR) {
1081             if (serial8250_isa_config != NULL)
1082                 serial8250_isa_config(0, &uart->port,
1083                         &uart->capabilities);
1084 
1085             serial8250_apply_quirks(uart);
1086             ret = uart_add_one_port(&serial8250_reg,
1087                         &uart->port);
1088             if (ret)
1089                 goto err;
1090 
1091             ret = uart->port.line;
1092         } else {
1093             dev_info(uart->port.dev,
1094                 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
1095                 uart->port.iobase,
1096                 (unsigned long long)uart->port.mapbase,
1097                 uart->port.irq);
1098 
1099             ret = 0;
1100         }
1101 
1102         if (!uart->lsr_save_mask)
1103             uart->lsr_save_mask = LSR_SAVE_FLAGS;   /* Use default LSR mask */
1104 
1105         /* Initialise interrupt backoff work if required */
1106         if (up->overrun_backoff_time_ms > 0) {
1107             uart->overrun_backoff_time_ms =
1108                 up->overrun_backoff_time_ms;
1109             INIT_DELAYED_WORK(&uart->overrun_backoff,
1110                     serial_8250_overrun_backoff_work);
1111         } else {
1112             uart->overrun_backoff_time_ms = 0;
1113         }
1114     }
1115 
1116     mutex_unlock(&serial_mutex);
1117 
1118     return ret;
1119 
1120 err:
1121     uart->port.dev = NULL;
1122     mutex_unlock(&serial_mutex);
1123     return ret;
1124 }
1125 EXPORT_SYMBOL(serial8250_register_8250_port);
1126 
1127 /**
1128  *  serial8250_unregister_port - remove a 16x50 serial port at runtime
1129  *  @line: serial line number
1130  *
1131  *  Remove one serial port.  This may not be called from interrupt
1132  *  context.  We hand the port back to the our control.
1133  */
1134 void serial8250_unregister_port(int line)
1135 {
1136     struct uart_8250_port *uart = &serial8250_ports[line];
1137 
1138     mutex_lock(&serial_mutex);
1139 
1140     if (uart->em485) {
1141         unsigned long flags;
1142 
1143         spin_lock_irqsave(&uart->port.lock, flags);
1144         serial8250_em485_destroy(uart);
1145         spin_unlock_irqrestore(&uart->port.lock, flags);
1146     }
1147 
1148     uart_remove_one_port(&serial8250_reg, &uart->port);
1149     if (serial8250_isa_devs) {
1150         uart->port.flags &= ~UPF_BOOT_AUTOCONF;
1151         uart->port.type = PORT_UNKNOWN;
1152         uart->port.dev = &serial8250_isa_devs->dev;
1153         uart->capabilities = 0;
1154         serial8250_apply_quirks(uart);
1155         uart_add_one_port(&serial8250_reg, &uart->port);
1156     } else {
1157         uart->port.dev = NULL;
1158     }
1159     mutex_unlock(&serial_mutex);
1160 }
1161 EXPORT_SYMBOL(serial8250_unregister_port);
1162 
1163 static int __init serial8250_init(void)
1164 {
1165     int ret;
1166 
1167     if (nr_uarts == 0)
1168         return -ENODEV;
1169 
1170     serial8250_isa_init_ports();
1171 
1172     pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %sabled\n",
1173         nr_uarts, share_irqs ? "en" : "dis");
1174 
1175 #ifdef CONFIG_SPARC
1176     ret = sunserial_register_minors(&serial8250_reg, UART_NR);
1177 #else
1178     serial8250_reg.nr = UART_NR;
1179     ret = uart_register_driver(&serial8250_reg);
1180 #endif
1181     if (ret)
1182         goto out;
1183 
1184     ret = serial8250_pnp_init();
1185     if (ret)
1186         goto unreg_uart_drv;
1187 
1188     serial8250_isa_devs = platform_device_alloc("serial8250",
1189                             PLAT8250_DEV_LEGACY);
1190     if (!serial8250_isa_devs) {
1191         ret = -ENOMEM;
1192         goto unreg_pnp;
1193     }
1194 
1195     ret = platform_device_add(serial8250_isa_devs);
1196     if (ret)
1197         goto put_dev;
1198 
1199     serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
1200 
1201     ret = platform_driver_register(&serial8250_isa_driver);
1202     if (ret == 0)
1203         goto out;
1204 
1205     platform_device_del(serial8250_isa_devs);
1206 put_dev:
1207     platform_device_put(serial8250_isa_devs);
1208 unreg_pnp:
1209     serial8250_pnp_exit();
1210 unreg_uart_drv:
1211 #ifdef CONFIG_SPARC
1212     sunserial_unregister_minors(&serial8250_reg, UART_NR);
1213 #else
1214     uart_unregister_driver(&serial8250_reg);
1215 #endif
1216 out:
1217     return ret;
1218 }
1219 
1220 static void __exit serial8250_exit(void)
1221 {
1222     struct platform_device *isa_dev = serial8250_isa_devs;
1223 
1224     /*
1225      * This tells serial8250_unregister_port() not to re-register
1226      * the ports (thereby making serial8250_isa_driver permanently
1227      * in use.)
1228      */
1229     serial8250_isa_devs = NULL;
1230 
1231     platform_driver_unregister(&serial8250_isa_driver);
1232     platform_device_unregister(isa_dev);
1233 
1234     serial8250_pnp_exit();
1235 
1236 #ifdef CONFIG_SPARC
1237     sunserial_unregister_minors(&serial8250_reg, UART_NR);
1238 #else
1239     uart_unregister_driver(&serial8250_reg);
1240 #endif
1241 }
1242 
1243 module_init(serial8250_init);
1244 module_exit(serial8250_exit);
1245 
1246 MODULE_LICENSE("GPL");
1247 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
1248 
1249 module_param_hw(share_irqs, uint, other, 0644);
1250 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
1251 
1252 module_param(nr_uarts, uint, 0644);
1253 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
1254 
1255 module_param(skip_txen_test, uint, 0644);
1256 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
1257 
1258 #ifdef CONFIG_SERIAL_8250_RSA
1259 module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
1260 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
1261 #endif
1262 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
1263 
1264 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
1265 #ifndef MODULE
1266 /* This module was renamed to 8250_core in 3.7.  Keep the old "8250" name
1267  * working as well for the module options so we don't break people.  We
1268  * need to keep the names identical and the convenient macros will happily
1269  * refuse to let us do that by failing the build with redefinition errors
1270  * of global variables.  So we stick them inside a dummy function to avoid
1271  * those conflicts.  The options still get parsed, and the redefined
1272  * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
1273  *
1274  * This is hacky.  I'm sorry.
1275  */
1276 static void __used s8250_options(void)
1277 {
1278 #undef MODULE_PARAM_PREFIX
1279 #define MODULE_PARAM_PREFIX "8250_core."
1280 
1281     module_param_cb(share_irqs, &param_ops_uint, &share_irqs, 0644);
1282     module_param_cb(nr_uarts, &param_ops_uint, &nr_uarts, 0644);
1283     module_param_cb(skip_txen_test, &param_ops_uint, &skip_txen_test, 0644);
1284 #ifdef CONFIG_SERIAL_8250_RSA
1285     __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
1286         &param_array_ops, .arr = &__param_arr_probe_rsa,
1287         0444, -1, 0);
1288 #endif
1289 }
1290 #else
1291 MODULE_ALIAS("8250_core");
1292 #endif
1293 #endif