1 // SPDX-License-Identifier: GPL-2.0+
3 * Universal/legacy driver for 8250/16550-type serial ports
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 * Copyright (C) 2001 Russell King.
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
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/uaccess.h>
38 #include <linux/sunserialcore.h>
47 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
48 * is unsafe when used on edge-triggered interrupts.
50 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
52 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
54 static struct uart_driver serial8250_reg;
56 static unsigned int skip_txen_test; /* force skip of txen test at init time */
58 #define PASS_LIMIT 512
60 #include <asm/serial.h>
62 * SERIAL_PORT_DFNS tells us about built-in ports that have no
63 * standard enumeration mechanism. Platforms that can find all
64 * serial ports via mechanisms like ACPI or PCI need not supply it.
66 #ifndef SERIAL_PORT_DFNS
67 #define SERIAL_PORT_DFNS
70 static const struct old_serial_port old_serial_port[] = {
71 SERIAL_PORT_DFNS /* defined in asm/serial.h */
74 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
76 #ifdef CONFIG_SERIAL_8250_RSA
78 #define PORT_RSA_MAX 4
79 static unsigned long probe_rsa[PORT_RSA_MAX];
80 static unsigned int probe_rsa_count;
81 #endif /* CONFIG_SERIAL_8250_RSA */
84 struct hlist_node node;
86 spinlock_t lock; /* Protects list not the hash */
87 struct list_head *head;
90 #define NR_IRQ_HASH 32 /* Can be adjusted later */
91 static struct hlist_head irq_lists[NR_IRQ_HASH];
92 static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
95 * This is the serial driver's interrupt routine.
97 * Arjan thinks the old way was overly complex, so it got simplified.
98 * Alan disagrees, saying that need the complexity to handle the weird
99 * nature of ISA shared interrupts. (This is a special exception.)
101 * In order to handle ISA shared interrupts properly, we need to check
102 * that all ports have been serviced, and therefore the ISA interrupt
103 * line has been de-asserted.
105 * This means we need to loop through all ports. checking that they
106 * don't have an interrupt pending.
108 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
110 struct irq_info *i = dev_id;
111 struct list_head *l, *end = NULL;
112 int pass_counter = 0, handled = 0;
114 pr_debug("%s(%d): start\n", __func__, irq);
120 struct uart_8250_port *up;
121 struct uart_port *port;
123 up = list_entry(l, struct uart_8250_port, list);
126 if (port->handle_irq(port)) {
129 } else if (end == NULL)
134 if (l == i->head && pass_counter++ > PASS_LIMIT)
138 spin_unlock(&i->lock);
140 pr_debug("%s(%d): end\n", __func__, irq);
142 return IRQ_RETVAL(handled);
146 * To support ISA shared interrupts, we need to have one interrupt
147 * handler that ensures that the IRQ line has been deasserted
148 * before returning. Failing to do this will result in the IRQ
149 * line being stuck active, and, since ISA irqs are edge triggered,
150 * no more IRQs will be seen.
152 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
154 spin_lock_irq(&i->lock);
156 if (!list_empty(i->head)) {
157 if (i->head == &up->list)
158 i->head = i->head->next;
161 BUG_ON(i->head != &up->list);
164 spin_unlock_irq(&i->lock);
165 /* List empty so throw away the hash node */
166 if (i->head == NULL) {
172 static int serial_link_irq_chain(struct uart_8250_port *up)
174 struct hlist_head *h;
178 mutex_lock(&hash_mutex);
180 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
182 hlist_for_each_entry(i, h, node)
183 if (i->irq == up->port.irq)
187 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
189 mutex_unlock(&hash_mutex);
192 spin_lock_init(&i->lock);
193 i->irq = up->port.irq;
194 hlist_add_head(&i->node, h);
196 mutex_unlock(&hash_mutex);
198 spin_lock_irq(&i->lock);
201 list_add(&up->list, i->head);
202 spin_unlock_irq(&i->lock);
206 INIT_LIST_HEAD(&up->list);
208 spin_unlock_irq(&i->lock);
209 ret = request_irq(up->port.irq, serial8250_interrupt,
210 up->port.irqflags, up->port.name, i);
212 serial_do_unlink(i, up);
218 static void serial_unlink_irq_chain(struct uart_8250_port *up)
221 struct hlist_head *h;
223 mutex_lock(&hash_mutex);
225 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
227 hlist_for_each_entry(i, h, node)
228 if (i->irq == up->port.irq)
232 BUG_ON(i->head == NULL);
234 if (list_empty(i->head))
235 free_irq(up->port.irq, i);
237 serial_do_unlink(i, up);
238 mutex_unlock(&hash_mutex);
242 * This function is used to handle ports that do not have an
243 * interrupt. This doesn't work very well for 16450's, but gives
244 * barely passable results for a 16550A. (Although at the expense
245 * of much CPU overhead).
247 static void serial8250_timeout(struct timer_list *t)
249 struct uart_8250_port *up = from_timer(up, t, timer);
251 up->port.handle_irq(&up->port);
252 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
255 static void serial8250_backup_timeout(struct timer_list *t)
257 struct uart_8250_port *up = from_timer(up, t, timer);
258 unsigned int iir, ier = 0, lsr;
261 spin_lock_irqsave(&up->port.lock, flags);
264 * Must disable interrupts or else we risk racing with the interrupt
268 ier = serial_in(up, UART_IER);
269 serial_out(up, UART_IER, 0);
272 iir = serial_in(up, UART_IIR);
275 * This should be a safe test for anyone who doesn't trust the
276 * IIR bits on their UART, but it's specifically designed for
277 * the "Diva" UART used on the management processor on many HP
278 * ia64 and parisc boxes.
280 lsr = serial_in(up, UART_LSR);
281 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
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;
289 if (!(iir & UART_IIR_NO_INT))
290 serial8250_tx_chars(up);
293 serial_out(up, UART_IER, ier);
295 spin_unlock_irqrestore(&up->port.lock, flags);
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);
302 static int univ8250_setup_irq(struct uart_8250_port *up)
304 struct uart_port *port = &up->port;
308 * The above check will only give an accurate result the first time
309 * the port is opened so this value needs to be preserved.
311 if (up->bugs & UART_BUG_THRE) {
312 pr_debug("%s - using backup timer\n", port->name);
314 up->timer.function = serial8250_backup_timeout;
315 mod_timer(&up->timer, jiffies +
316 uart_poll_timeout(port) + HZ / 5);
320 * If the "interrupt" for this port doesn't correspond with any
321 * hardware interrupt, we use a timer-based system. The original
322 * driver used to do this with IRQ0.
325 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
327 retval = serial_link_irq_chain(up);
332 static void univ8250_release_irq(struct uart_8250_port *up)
334 struct uart_port *port = &up->port;
336 del_timer_sync(&up->timer);
337 up->timer.function = serial8250_timeout;
339 serial_unlink_irq_chain(up);
342 #ifdef CONFIG_SERIAL_8250_RSA
343 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
345 unsigned long start = UART_RSA_BASE << up->port.regshift;
346 unsigned int size = 8 << up->port.regshift;
347 struct uart_port *port = &up->port;
350 switch (port->iotype) {
353 start += port->iobase;
354 if (request_region(start, size, "serial-rsa"))
364 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
366 unsigned long offset = UART_RSA_BASE << up->port.regshift;
367 unsigned int size = 8 << up->port.regshift;
368 struct uart_port *port = &up->port;
370 switch (port->iotype) {
373 release_region(port->iobase + offset, size);
379 static const struct uart_ops *base_ops;
380 static struct uart_ops univ8250_port_ops;
382 static const struct uart_8250_ops univ8250_driver_ops = {
383 .setup_irq = univ8250_setup_irq,
384 .release_irq = univ8250_release_irq,
387 static struct uart_8250_port serial8250_ports[UART_NR];
390 * serial8250_get_port - retrieve struct uart_8250_port
391 * @line: serial line number
393 * This function retrieves struct uart_8250_port for the specific line.
394 * This struct *must* *not* be used to perform a 8250 or serial core operation
395 * which is not accessible otherwise. Its only purpose is to make the struct
396 * accessible to the runtime-pm callbacks for context suspend/restore.
397 * The lock assumption made here is none because runtime-pm suspend/resume
398 * callbacks should not be invoked if there is any operation performed on the
401 struct uart_8250_port *serial8250_get_port(int line)
403 return &serial8250_ports[line];
405 EXPORT_SYMBOL_GPL(serial8250_get_port);
407 static void (*serial8250_isa_config)(int port, struct uart_port *up,
410 void serial8250_set_isa_configurator(
411 void (*v)(int port, struct uart_port *up, u32 *capabilities))
413 serial8250_isa_config = v;
415 EXPORT_SYMBOL(serial8250_set_isa_configurator);
417 #ifdef CONFIG_SERIAL_8250_RSA
419 static void univ8250_config_port(struct uart_port *port, int flags)
421 struct uart_8250_port *up = up_to_u8250p(port);
423 up->probe &= ~UART_PROBE_RSA;
424 if (port->type == PORT_RSA) {
425 if (serial8250_request_rsa_resource(up) == 0)
426 up->probe |= UART_PROBE_RSA;
427 } else if (flags & UART_CONFIG_TYPE) {
430 for (i = 0; i < probe_rsa_count; i++) {
431 if (probe_rsa[i] == up->port.iobase) {
432 if (serial8250_request_rsa_resource(up) == 0)
433 up->probe |= UART_PROBE_RSA;
439 base_ops->config_port(port, flags);
441 if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
442 serial8250_release_rsa_resource(up);
445 static int univ8250_request_port(struct uart_port *port)
447 struct uart_8250_port *up = up_to_u8250p(port);
450 ret = base_ops->request_port(port);
451 if (ret == 0 && port->type == PORT_RSA) {
452 ret = serial8250_request_rsa_resource(up);
454 base_ops->release_port(port);
460 static void univ8250_release_port(struct uart_port *port)
462 struct uart_8250_port *up = up_to_u8250p(port);
464 if (port->type == PORT_RSA)
465 serial8250_release_rsa_resource(up);
466 base_ops->release_port(port);
469 static void univ8250_rsa_support(struct uart_ops *ops)
471 ops->config_port = univ8250_config_port;
472 ops->request_port = univ8250_request_port;
473 ops->release_port = univ8250_release_port;
477 #define univ8250_rsa_support(x) do { } while (0)
478 #endif /* CONFIG_SERIAL_8250_RSA */
480 static inline void serial8250_apply_quirks(struct uart_8250_port *up)
482 up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0;
485 static void __init serial8250_isa_init_ports(void)
487 struct uart_8250_port *up;
488 static int first = 1;
495 if (nr_uarts > UART_NR)
498 for (i = 0; i < nr_uarts; i++) {
499 struct uart_8250_port *up = &serial8250_ports[i];
500 struct uart_port *port = &up->port;
503 serial8250_init_port(up);
505 base_ops = port->ops;
506 port->ops = &univ8250_port_ops;
508 timer_setup(&up->timer, serial8250_timeout, 0);
510 up->ops = &univ8250_driver_ops;
512 if (IS_ENABLED(CONFIG_ALPHA_JENSEN) ||
513 (IS_ENABLED(CONFIG_ALPHA_GENERIC) && alpha_jensen()))
514 port->set_mctrl = alpha_jensen_set_mctrl;
516 serial8250_set_defaults(up);
519 /* chain base port ops to support Remote Supervisor Adapter */
520 univ8250_port_ops = *base_ops;
521 univ8250_rsa_support(&univ8250_port_ops);
524 irqflag = IRQF_SHARED;
526 for (i = 0, up = serial8250_ports;
527 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
529 struct uart_port *port = &up->port;
531 port->iobase = old_serial_port[i].port;
532 port->irq = irq_canonicalize(old_serial_port[i].irq);
534 port->uartclk = old_serial_port[i].baud_base * 16;
535 port->flags = old_serial_port[i].flags;
537 port->membase = old_serial_port[i].iomem_base;
538 port->iotype = old_serial_port[i].io_type;
539 port->regshift = old_serial_port[i].iomem_reg_shift;
541 port->irqflags |= irqflag;
542 if (serial8250_isa_config != NULL)
543 serial8250_isa_config(i, &up->port, &up->capabilities);
548 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
552 for (i = 0; i < nr_uarts; i++) {
553 struct uart_8250_port *up = &serial8250_ports[i];
555 if (up->port.type == PORT_8250_CIR)
563 if (uart_console_enabled(&up->port))
564 pm_runtime_get_sync(up->port.dev);
566 serial8250_apply_quirks(up);
567 uart_add_one_port(drv, &up->port);
571 #ifdef CONFIG_SERIAL_8250_CONSOLE
573 static void univ8250_console_write(struct console *co, const char *s,
576 struct uart_8250_port *up = &serial8250_ports[co->index];
578 serial8250_console_write(up, s, count);
581 static int univ8250_console_setup(struct console *co, char *options)
583 struct uart_port *port;
587 * Check whether an invalid uart number has been specified, and
588 * if so, search for the first available port that does have
591 if (co->index >= nr_uarts)
593 port = &serial8250_ports[co->index].port;
594 /* link port to console */
597 retval = serial8250_console_setup(port, options, false);
603 static int univ8250_console_exit(struct console *co)
605 struct uart_port *port;
607 port = &serial8250_ports[co->index].port;
608 return serial8250_console_exit(port);
612 * univ8250_console_match - non-standard console matching
613 * @co: registering console
614 * @name: name from console command line
615 * @idx: index from console command line
616 * @options: ptr to option string from console command line
618 * Only attempts to match console command lines of the form:
619 * console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>]
620 * console=uart[8250],0x<addr>[,<options>]
621 * This form is used to register an initial earlycon boot console and
622 * replace it with the serial8250_console at 8250 driver init.
624 * Performs console setup for a match (as required by interface)
625 * If no <options> are specified, then assume the h/w is already setup.
627 * Returns 0 if console matches; otherwise non-zero to use default matching
629 static int univ8250_console_match(struct console *co, char *name, int idx,
632 char match[] = "uart"; /* 8250-specific earlycon name */
633 unsigned char iotype;
634 resource_size_t addr;
637 if (strncmp(name, match, 4) != 0)
640 if (uart_parse_earlycon(options, &iotype, &addr, &options))
643 /* try to match the port specified on the command line */
644 for (i = 0; i < nr_uarts; i++) {
645 struct uart_port *port = &serial8250_ports[i].port;
647 if (port->iotype != iotype)
649 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
650 iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
651 && (port->mapbase != addr))
653 if (iotype == UPIO_PORT && port->iobase != addr)
658 return serial8250_console_setup(port, options, true);
664 static struct console univ8250_console = {
666 .write = univ8250_console_write,
667 .device = uart_console_device,
668 .setup = univ8250_console_setup,
669 .exit = univ8250_console_exit,
670 .match = univ8250_console_match,
671 .flags = CON_PRINTBUFFER | CON_ANYTIME,
673 .data = &serial8250_reg,
676 static int __init univ8250_console_init(void)
681 serial8250_isa_init_ports();
682 register_console(&univ8250_console);
685 console_initcall(univ8250_console_init);
687 #define SERIAL8250_CONSOLE (&univ8250_console)
689 #define SERIAL8250_CONSOLE NULL
692 static struct uart_driver serial8250_reg = {
693 .owner = THIS_MODULE,
694 .driver_name = "serial",
698 .cons = SERIAL8250_CONSOLE,
702 * early_serial_setup - early registration for 8250 ports
704 * Setup an 8250 port structure prior to console initialisation. Use
705 * after console initialisation will cause undefined behaviour.
707 int __init early_serial_setup(struct uart_port *port)
711 if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
714 serial8250_isa_init_ports();
715 p = &serial8250_ports[port->line].port;
716 p->iobase = port->iobase;
717 p->membase = port->membase;
719 p->irqflags = port->irqflags;
720 p->uartclk = port->uartclk;
721 p->fifosize = port->fifosize;
722 p->regshift = port->regshift;
723 p->iotype = port->iotype;
724 p->flags = port->flags;
725 p->mapbase = port->mapbase;
726 p->mapsize = port->mapsize;
727 p->private_data = port->private_data;
728 p->type = port->type;
729 p->line = port->line;
731 serial8250_set_defaults(up_to_u8250p(p));
734 p->serial_in = port->serial_in;
735 if (port->serial_out)
736 p->serial_out = port->serial_out;
737 if (port->handle_irq)
738 p->handle_irq = port->handle_irq;
744 * serial8250_suspend_port - suspend one serial port
745 * @line: serial line number
747 * Suspend one serial port.
749 void serial8250_suspend_port(int line)
751 struct uart_8250_port *up = &serial8250_ports[line];
752 struct uart_port *port = &up->port;
754 if (!console_suspend_enabled && uart_console(port) &&
755 port->type != PORT_8250) {
756 unsigned char canary = 0xa5;
758 serial_out(up, UART_SCR, canary);
759 if (serial_in(up, UART_SCR) == canary)
763 uart_suspend_port(&serial8250_reg, port);
765 EXPORT_SYMBOL(serial8250_suspend_port);
768 * serial8250_resume_port - resume one serial port
769 * @line: serial line number
771 * Resume one serial port.
773 void serial8250_resume_port(int line)
775 struct uart_8250_port *up = &serial8250_ports[line];
776 struct uart_port *port = &up->port;
780 if (up->capabilities & UART_NATSEMI) {
781 /* Ensure it's still in high speed mode */
782 serial_port_out(port, UART_LCR, 0xE0);
784 ns16550a_goto_highspeed(up);
786 serial_port_out(port, UART_LCR, 0);
787 port->uartclk = 921600*16;
789 uart_resume_port(&serial8250_reg, port);
791 EXPORT_SYMBOL(serial8250_resume_port);
794 * Register a set of serial devices attached to a platform device. The
795 * list is terminated with a zero flags entry, which means we expect
796 * all entries to have at least UPF_BOOT_AUTOCONF set.
798 static int serial8250_probe(struct platform_device *dev)
800 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
801 struct uart_8250_port uart;
802 int ret, i, irqflag = 0;
804 memset(&uart, 0, sizeof(uart));
807 irqflag = IRQF_SHARED;
809 for (i = 0; p && p->flags != 0; p++, i++) {
810 uart.port.iobase = p->iobase;
811 uart.port.membase = p->membase;
812 uart.port.irq = p->irq;
813 uart.port.irqflags = p->irqflags;
814 uart.port.uartclk = p->uartclk;
815 uart.port.regshift = p->regshift;
816 uart.port.iotype = p->iotype;
817 uart.port.flags = p->flags;
818 uart.port.mapbase = p->mapbase;
819 uart.port.hub6 = p->hub6;
820 uart.port.has_sysrq = p->has_sysrq;
821 uart.port.private_data = p->private_data;
822 uart.port.type = p->type;
823 uart.port.serial_in = p->serial_in;
824 uart.port.serial_out = p->serial_out;
825 uart.port.handle_irq = p->handle_irq;
826 uart.port.handle_break = p->handle_break;
827 uart.port.set_termios = p->set_termios;
828 uart.port.set_ldisc = p->set_ldisc;
829 uart.port.get_mctrl = p->get_mctrl;
830 uart.port.pm = p->pm;
831 uart.port.dev = &dev->dev;
832 uart.port.irqflags |= irqflag;
833 ret = serial8250_register_8250_port(&uart);
835 dev_err(&dev->dev, "unable to register port at index %d "
836 "(IO%lx MEM%llx IRQ%d): %d\n", i,
837 p->iobase, (unsigned long long)p->mapbase,
845 * Remove serial ports registered against a platform device.
847 static int serial8250_remove(struct platform_device *dev)
851 for (i = 0; i < nr_uarts; i++) {
852 struct uart_8250_port *up = &serial8250_ports[i];
854 if (up->port.dev == &dev->dev)
855 serial8250_unregister_port(i);
860 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
864 for (i = 0; i < UART_NR; i++) {
865 struct uart_8250_port *up = &serial8250_ports[i];
867 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
868 uart_suspend_port(&serial8250_reg, &up->port);
874 static int serial8250_resume(struct platform_device *dev)
878 for (i = 0; i < UART_NR; i++) {
879 struct uart_8250_port *up = &serial8250_ports[i];
881 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
882 serial8250_resume_port(i);
888 static struct platform_driver serial8250_isa_driver = {
889 .probe = serial8250_probe,
890 .remove = serial8250_remove,
891 .suspend = serial8250_suspend,
892 .resume = serial8250_resume,
894 .name = "serial8250",
899 * This "device" covers _all_ ISA 8250-compatible serial devices listed
900 * in the table in include/asm/serial.h
902 static struct platform_device *serial8250_isa_devs;
905 * serial8250_register_8250_port and serial8250_unregister_port allows for
906 * 16x50 serial ports to be configured at run-time, to support PCMCIA
907 * modems and PCI multiport cards.
909 static DEFINE_MUTEX(serial_mutex);
911 static struct uart_8250_port *serial8250_find_match_or_unused(const struct uart_port *port)
916 * First, find a port entry which matches.
918 for (i = 0; i < nr_uarts; i++)
919 if (uart_match_port(&serial8250_ports[i].port, port))
920 return &serial8250_ports[i];
922 /* try line number first if still available */
924 if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
925 serial8250_ports[i].port.iobase == 0)
926 return &serial8250_ports[i];
928 * We didn't find a matching entry, so look for the first
929 * free entry. We look for one which hasn't been previously
930 * used (indicated by zero iobase).
932 for (i = 0; i < nr_uarts; i++)
933 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
934 serial8250_ports[i].port.iobase == 0)
935 return &serial8250_ports[i];
938 * That also failed. Last resort is to find any entry which
939 * doesn't have a real port associated with it.
941 for (i = 0; i < nr_uarts; i++)
942 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
943 return &serial8250_ports[i];
948 static void serial_8250_overrun_backoff_work(struct work_struct *work)
950 struct uart_8250_port *up =
951 container_of(to_delayed_work(work), struct uart_8250_port,
953 struct uart_port *port = &up->port;
956 spin_lock_irqsave(&port->lock, flags);
957 up->ier |= UART_IER_RLSI | UART_IER_RDI;
958 up->port.read_status_mask |= UART_LSR_DR;
959 serial_out(up, UART_IER, up->ier);
960 spin_unlock_irqrestore(&port->lock, flags);
964 * serial8250_register_8250_port - register a serial port
965 * @up: serial port template
967 * Configure the serial port specified by the request. If the
968 * port exists and is in use, it is hung up and unregistered
971 * The port is then probed and if necessary the IRQ is autodetected
972 * If this fails an error is returned.
974 * On success the port is ready to use and the line number is returned.
976 int serial8250_register_8250_port(const struct uart_8250_port *up)
978 struct uart_8250_port *uart;
981 if (up->port.uartclk == 0)
984 mutex_lock(&serial_mutex);
986 uart = serial8250_find_match_or_unused(&up->port);
987 if (uart && uart->port.type != PORT_8250_CIR) {
988 struct mctrl_gpios *gpios;
991 uart_remove_one_port(&serial8250_reg, &uart->port);
993 uart->port.iobase = up->port.iobase;
994 uart->port.membase = up->port.membase;
995 uart->port.irq = up->port.irq;
996 uart->port.irqflags = up->port.irqflags;
997 uart->port.uartclk = up->port.uartclk;
998 uart->port.fifosize = up->port.fifosize;
999 uart->port.regshift = up->port.regshift;
1000 uart->port.iotype = up->port.iotype;
1001 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
1002 uart->bugs = up->bugs;
1003 uart->port.mapbase = up->port.mapbase;
1004 uart->port.mapsize = up->port.mapsize;
1005 uart->port.private_data = up->port.private_data;
1006 uart->tx_loadsz = up->tx_loadsz;
1007 uart->capabilities = up->capabilities;
1008 uart->port.throttle = up->port.throttle;
1009 uart->port.unthrottle = up->port.unthrottle;
1010 uart->port.rs485_config = up->port.rs485_config;
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->dma = up->dma;
1016 /* Take tx_loadsz from fifosize if it wasn't set separately */
1017 if (uart->port.fifosize && !uart->tx_loadsz)
1018 uart->tx_loadsz = uart->port.fifosize;
1021 uart->port.dev = up->port.dev;
1022 ret = uart_get_rs485_mode(&uart->port);
1027 if (up->port.flags & UPF_FIXED_TYPE)
1028 uart->port.type = up->port.type;
1031 * Only call mctrl_gpio_init(), if the device has no ACPI
1034 if (!has_acpi_companion(uart->port.dev)) {
1035 gpios = mctrl_gpio_init(&uart->port, 0);
1036 if (IS_ERR(gpios)) {
1037 ret = PTR_ERR(gpios);
1040 uart->gpios = gpios;
1044 serial8250_set_defaults(uart);
1046 /* Possibly override default I/O functions. */
1047 if (up->port.serial_in)
1048 uart->port.serial_in = up->port.serial_in;
1049 if (up->port.serial_out)
1050 uart->port.serial_out = up->port.serial_out;
1051 if (up->port.handle_irq)
1052 uart->port.handle_irq = up->port.handle_irq;
1053 /* Possibly override set_termios call */
1054 if (up->port.set_termios)
1055 uart->port.set_termios = up->port.set_termios;
1056 if (up->port.set_ldisc)
1057 uart->port.set_ldisc = up->port.set_ldisc;
1058 if (up->port.get_mctrl)
1059 uart->port.get_mctrl = up->port.get_mctrl;
1060 if (up->port.set_mctrl)
1061 uart->port.set_mctrl = up->port.set_mctrl;
1062 if (up->port.get_divisor)
1063 uart->port.get_divisor = up->port.get_divisor;
1064 if (up->port.set_divisor)
1065 uart->port.set_divisor = up->port.set_divisor;
1066 if (up->port.startup)
1067 uart->port.startup = up->port.startup;
1068 if (up->port.shutdown)
1069 uart->port.shutdown = up->port.shutdown;
1071 uart->port.pm = up->port.pm;
1072 if (up->port.handle_break)
1073 uart->port.handle_break = up->port.handle_break;
1075 uart->dl_read = up->dl_read;
1077 uart->dl_write = up->dl_write;
1079 if (uart->port.type != PORT_8250_CIR) {
1080 if (serial8250_isa_config != NULL)
1081 serial8250_isa_config(0, &uart->port,
1082 &uart->capabilities);
1084 serial8250_apply_quirks(uart);
1085 ret = uart_add_one_port(&serial8250_reg,
1090 ret = uart->port.line;
1092 dev_info(uart->port.dev,
1093 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
1095 (unsigned long long)uart->port.mapbase,
1101 /* Initialise interrupt backoff work if required */
1102 if (up->overrun_backoff_time_ms > 0) {
1103 uart->overrun_backoff_time_ms =
1104 up->overrun_backoff_time_ms;
1105 INIT_DELAYED_WORK(&uart->overrun_backoff,
1106 serial_8250_overrun_backoff_work);
1108 uart->overrun_backoff_time_ms = 0;
1112 mutex_unlock(&serial_mutex);
1117 uart->port.dev = NULL;
1118 mutex_unlock(&serial_mutex);
1121 EXPORT_SYMBOL(serial8250_register_8250_port);
1124 * serial8250_unregister_port - remove a 16x50 serial port at runtime
1125 * @line: serial line number
1127 * Remove one serial port. This may not be called from interrupt
1128 * context. We hand the port back to the our control.
1130 void serial8250_unregister_port(int line)
1132 struct uart_8250_port *uart = &serial8250_ports[line];
1134 mutex_lock(&serial_mutex);
1137 unsigned long flags;
1139 spin_lock_irqsave(&uart->port.lock, flags);
1140 serial8250_em485_destroy(uart);
1141 spin_unlock_irqrestore(&uart->port.lock, flags);
1144 uart_remove_one_port(&serial8250_reg, &uart->port);
1145 if (serial8250_isa_devs) {
1146 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
1147 uart->port.type = PORT_UNKNOWN;
1148 uart->port.dev = &serial8250_isa_devs->dev;
1149 uart->capabilities = 0;
1150 serial8250_apply_quirks(uart);
1151 uart_add_one_port(&serial8250_reg, &uart->port);
1153 uart->port.dev = NULL;
1155 mutex_unlock(&serial_mutex);
1157 EXPORT_SYMBOL(serial8250_unregister_port);
1159 static int __init serial8250_init(void)
1166 serial8250_isa_init_ports();
1168 pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %sabled\n",
1169 nr_uarts, share_irqs ? "en" : "dis");
1172 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
1174 serial8250_reg.nr = UART_NR;
1175 ret = uart_register_driver(&serial8250_reg);
1180 ret = serial8250_pnp_init();
1182 goto unreg_uart_drv;
1184 serial8250_isa_devs = platform_device_alloc("serial8250",
1185 PLAT8250_DEV_LEGACY);
1186 if (!serial8250_isa_devs) {
1191 ret = platform_device_add(serial8250_isa_devs);
1195 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
1197 ret = platform_driver_register(&serial8250_isa_driver);
1201 platform_device_del(serial8250_isa_devs);
1203 platform_device_put(serial8250_isa_devs);
1205 serial8250_pnp_exit();
1208 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1210 uart_unregister_driver(&serial8250_reg);
1216 static void __exit serial8250_exit(void)
1218 struct platform_device *isa_dev = serial8250_isa_devs;
1221 * This tells serial8250_unregister_port() not to re-register
1222 * the ports (thereby making serial8250_isa_driver permanently
1225 serial8250_isa_devs = NULL;
1227 platform_driver_unregister(&serial8250_isa_driver);
1228 platform_device_unregister(isa_dev);
1230 serial8250_pnp_exit();
1233 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1235 uart_unregister_driver(&serial8250_reg);
1239 module_init(serial8250_init);
1240 module_exit(serial8250_exit);
1242 MODULE_LICENSE("GPL");
1243 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
1245 module_param_hw(share_irqs, uint, other, 0644);
1246 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
1248 module_param(nr_uarts, uint, 0644);
1249 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
1251 module_param(skip_txen_test, uint, 0644);
1252 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
1254 #ifdef CONFIG_SERIAL_8250_RSA
1255 module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
1256 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
1258 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
1260 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
1262 /* This module was renamed to 8250_core in 3.7. Keep the old "8250" name
1263 * working as well for the module options so we don't break people. We
1264 * need to keep the names identical and the convenient macros will happily
1265 * refuse to let us do that by failing the build with redefinition errors
1266 * of global variables. So we stick them inside a dummy function to avoid
1267 * those conflicts. The options still get parsed, and the redefined
1268 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
1270 * This is hacky. I'm sorry.
1272 static void __used s8250_options(void)
1274 #undef MODULE_PARAM_PREFIX
1275 #define MODULE_PARAM_PREFIX "8250_core."
1277 module_param_cb(share_irqs, ¶m_ops_uint, &share_irqs, 0644);
1278 module_param_cb(nr_uarts, ¶m_ops_uint, &nr_uarts, 0644);
1279 module_param_cb(skip_txen_test, ¶m_ops_uint, &skip_txen_test, 0644);
1280 #ifdef CONFIG_SERIAL_8250_RSA
1281 __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
1282 ¶m_array_ops, .arr = &__param_arr_probe_rsa,
1287 MODULE_ALIAS("8250_core");