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_lsr_in(up);
281 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
282 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
283 (lsr & UART_LSR_THRE)) {
284 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
285 iir |= UART_IIR_THRI;
288 if (!(iir & UART_IIR_NO_INT))
289 serial8250_tx_chars(up);
292 serial_out(up, UART_IER, ier);
294 spin_unlock_irqrestore(&up->port.lock, flags);
296 /* Standard timer interval plus 0.2s to keep the port running */
297 mod_timer(&up->timer,
298 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
301 static void univ8250_setup_timer(struct uart_8250_port *up)
303 struct uart_port *port = &up->port;
306 * The above check will only give an accurate result the first time
307 * the port is opened so this value needs to be preserved.
309 if (up->bugs & UART_BUG_THRE) {
310 pr_debug("%s - using backup timer\n", port->name);
312 up->timer.function = serial8250_backup_timeout;
313 mod_timer(&up->timer, jiffies +
314 uart_poll_timeout(port) + HZ / 5);
318 * If the "interrupt" for this port doesn't correspond with any
319 * hardware interrupt, we use a timer-based system. The original
320 * driver used to do this with IRQ0.
323 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
326 static int univ8250_setup_irq(struct uart_8250_port *up)
328 struct uart_port *port = &up->port;
331 return serial_link_irq_chain(up);
336 static void univ8250_release_irq(struct uart_8250_port *up)
338 struct uart_port *port = &up->port;
340 del_timer_sync(&up->timer);
341 up->timer.function = serial8250_timeout;
343 serial_unlink_irq_chain(up);
346 #ifdef CONFIG_SERIAL_8250_RSA
347 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
349 unsigned long start = UART_RSA_BASE << up->port.regshift;
350 unsigned int size = 8 << up->port.regshift;
351 struct uart_port *port = &up->port;
354 switch (port->iotype) {
357 start += port->iobase;
358 if (request_region(start, size, "serial-rsa"))
368 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
370 unsigned long offset = UART_RSA_BASE << up->port.regshift;
371 unsigned int size = 8 << up->port.regshift;
372 struct uart_port *port = &up->port;
374 switch (port->iotype) {
377 release_region(port->iobase + offset, size);
383 static const struct uart_ops *base_ops;
384 static struct uart_ops univ8250_port_ops;
386 static const struct uart_8250_ops univ8250_driver_ops = {
387 .setup_irq = univ8250_setup_irq,
388 .release_irq = univ8250_release_irq,
389 .setup_timer = univ8250_setup_timer,
392 static struct uart_8250_port serial8250_ports[UART_NR];
395 * serial8250_get_port - retrieve struct uart_8250_port
396 * @line: serial line number
398 * This function retrieves struct uart_8250_port for the specific line.
399 * This struct *must* *not* be used to perform a 8250 or serial core operation
400 * which is not accessible otherwise. Its only purpose is to make the struct
401 * accessible to the runtime-pm callbacks for context suspend/restore.
402 * The lock assumption made here is none because runtime-pm suspend/resume
403 * callbacks should not be invoked if there is any operation performed on the
406 struct uart_8250_port *serial8250_get_port(int line)
408 return &serial8250_ports[line];
410 EXPORT_SYMBOL_GPL(serial8250_get_port);
412 static void (*serial8250_isa_config)(int port, struct uart_port *up,
415 void serial8250_set_isa_configurator(
416 void (*v)(int port, struct uart_port *up, u32 *capabilities))
418 serial8250_isa_config = v;
420 EXPORT_SYMBOL(serial8250_set_isa_configurator);
422 #ifdef CONFIG_SERIAL_8250_RSA
424 static void univ8250_config_port(struct uart_port *port, int flags)
426 struct uart_8250_port *up = up_to_u8250p(port);
428 up->probe &= ~UART_PROBE_RSA;
429 if (port->type == PORT_RSA) {
430 if (serial8250_request_rsa_resource(up) == 0)
431 up->probe |= UART_PROBE_RSA;
432 } else if (flags & UART_CONFIG_TYPE) {
435 for (i = 0; i < probe_rsa_count; i++) {
436 if (probe_rsa[i] == up->port.iobase) {
437 if (serial8250_request_rsa_resource(up) == 0)
438 up->probe |= UART_PROBE_RSA;
444 base_ops->config_port(port, flags);
446 if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
447 serial8250_release_rsa_resource(up);
450 static int univ8250_request_port(struct uart_port *port)
452 struct uart_8250_port *up = up_to_u8250p(port);
455 ret = base_ops->request_port(port);
456 if (ret == 0 && port->type == PORT_RSA) {
457 ret = serial8250_request_rsa_resource(up);
459 base_ops->release_port(port);
465 static void univ8250_release_port(struct uart_port *port)
467 struct uart_8250_port *up = up_to_u8250p(port);
469 if (port->type == PORT_RSA)
470 serial8250_release_rsa_resource(up);
471 base_ops->release_port(port);
474 static void univ8250_rsa_support(struct uart_ops *ops)
476 ops->config_port = univ8250_config_port;
477 ops->request_port = univ8250_request_port;
478 ops->release_port = univ8250_release_port;
482 #define univ8250_rsa_support(x) do { } while (0)
483 #endif /* CONFIG_SERIAL_8250_RSA */
485 static inline void serial8250_apply_quirks(struct uart_8250_port *up)
487 up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0;
490 static void __init serial8250_isa_init_ports(void)
492 struct uart_8250_port *up;
493 static int first = 1;
500 if (nr_uarts > UART_NR)
503 for (i = 0; i < nr_uarts; i++) {
504 struct uart_8250_port *up = &serial8250_ports[i];
505 struct uart_port *port = &up->port;
508 serial8250_init_port(up);
510 base_ops = port->ops;
511 port->ops = &univ8250_port_ops;
513 timer_setup(&up->timer, serial8250_timeout, 0);
515 up->ops = &univ8250_driver_ops;
517 if (IS_ENABLED(CONFIG_ALPHA_JENSEN) ||
518 (IS_ENABLED(CONFIG_ALPHA_GENERIC) && alpha_jensen()))
519 port->set_mctrl = alpha_jensen_set_mctrl;
521 serial8250_set_defaults(up);
524 /* chain base port ops to support Remote Supervisor Adapter */
525 univ8250_port_ops = *base_ops;
526 univ8250_rsa_support(&univ8250_port_ops);
529 irqflag = IRQF_SHARED;
531 for (i = 0, up = serial8250_ports;
532 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
534 struct uart_port *port = &up->port;
536 port->iobase = old_serial_port[i].port;
537 port->irq = irq_canonicalize(old_serial_port[i].irq);
539 port->uartclk = old_serial_port[i].baud_base * 16;
540 port->flags = old_serial_port[i].flags;
542 port->membase = old_serial_port[i].iomem_base;
543 port->iotype = old_serial_port[i].io_type;
544 port->regshift = old_serial_port[i].iomem_reg_shift;
546 port->irqflags |= irqflag;
547 if (serial8250_isa_config != NULL)
548 serial8250_isa_config(i, &up->port, &up->capabilities);
553 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
557 for (i = 0; i < nr_uarts; i++) {
558 struct uart_8250_port *up = &serial8250_ports[i];
560 if (up->port.type == PORT_8250_CIR)
568 if (uart_console_registered(&up->port))
569 pm_runtime_get_sync(up->port.dev);
571 serial8250_apply_quirks(up);
572 uart_add_one_port(drv, &up->port);
576 #ifdef CONFIG_SERIAL_8250_CONSOLE
578 static void univ8250_console_write(struct console *co, const char *s,
581 struct uart_8250_port *up = &serial8250_ports[co->index];
583 serial8250_console_write(up, s, count);
586 static int univ8250_console_setup(struct console *co, char *options)
588 struct uart_port *port;
592 * Check whether an invalid uart number has been specified, and
593 * if so, search for the first available port that does have
596 if (co->index >= nr_uarts)
598 port = &serial8250_ports[co->index].port;
599 /* link port to console */
602 retval = serial8250_console_setup(port, options, false);
608 static int univ8250_console_exit(struct console *co)
610 struct uart_port *port;
612 port = &serial8250_ports[co->index].port;
613 return serial8250_console_exit(port);
617 * univ8250_console_match - non-standard console matching
618 * @co: registering console
619 * @name: name from console command line
620 * @idx: index from console command line
621 * @options: ptr to option string from console command line
623 * Only attempts to match console command lines of the form:
624 * console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>]
625 * console=uart[8250],0x<addr>[,<options>]
626 * This form is used to register an initial earlycon boot console and
627 * replace it with the serial8250_console at 8250 driver init.
629 * Performs console setup for a match (as required by interface)
630 * If no <options> are specified, then assume the h/w is already setup.
632 * Returns 0 if console matches; otherwise non-zero to use default matching
634 static int univ8250_console_match(struct console *co, char *name, int idx,
637 char match[] = "uart"; /* 8250-specific earlycon name */
638 unsigned char iotype;
639 resource_size_t addr;
642 if (strncmp(name, match, 4) != 0)
645 if (uart_parse_earlycon(options, &iotype, &addr, &options))
648 /* try to match the port specified on the command line */
649 for (i = 0; i < nr_uarts; i++) {
650 struct uart_port *port = &serial8250_ports[i].port;
652 if (port->iotype != iotype)
654 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
655 iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
656 && (port->mapbase != addr))
658 if (iotype == UPIO_PORT && port->iobase != addr)
663 return serial8250_console_setup(port, options, true);
669 static struct console univ8250_console = {
671 .write = univ8250_console_write,
672 .device = uart_console_device,
673 .setup = univ8250_console_setup,
674 .exit = univ8250_console_exit,
675 .match = univ8250_console_match,
676 .flags = CON_PRINTBUFFER | CON_ANYTIME,
678 .data = &serial8250_reg,
681 static int __init univ8250_console_init(void)
686 serial8250_isa_init_ports();
687 register_console(&univ8250_console);
690 console_initcall(univ8250_console_init);
692 #define SERIAL8250_CONSOLE (&univ8250_console)
694 #define SERIAL8250_CONSOLE NULL
697 static struct uart_driver serial8250_reg = {
698 .owner = THIS_MODULE,
699 .driver_name = "serial",
703 .cons = SERIAL8250_CONSOLE,
707 * early_serial_setup - early registration for 8250 ports
709 * Setup an 8250 port structure prior to console initialisation. Use
710 * after console initialisation will cause undefined behaviour.
712 int __init early_serial_setup(struct uart_port *port)
716 if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
719 serial8250_isa_init_ports();
720 p = &serial8250_ports[port->line].port;
721 p->iobase = port->iobase;
722 p->membase = port->membase;
724 p->irqflags = port->irqflags;
725 p->uartclk = port->uartclk;
726 p->fifosize = port->fifosize;
727 p->regshift = port->regshift;
728 p->iotype = port->iotype;
729 p->flags = port->flags;
730 p->mapbase = port->mapbase;
731 p->mapsize = port->mapsize;
732 p->private_data = port->private_data;
733 p->type = port->type;
734 p->line = port->line;
736 serial8250_set_defaults(up_to_u8250p(p));
739 p->serial_in = port->serial_in;
740 if (port->serial_out)
741 p->serial_out = port->serial_out;
742 if (port->handle_irq)
743 p->handle_irq = port->handle_irq;
749 * serial8250_suspend_port - suspend one serial port
750 * @line: serial line number
752 * Suspend one serial port.
754 void serial8250_suspend_port(int line)
756 struct uart_8250_port *up = &serial8250_ports[line];
757 struct uart_port *port = &up->port;
759 if (!console_suspend_enabled && uart_console(port) &&
760 port->type != PORT_8250) {
761 unsigned char canary = 0xa5;
763 serial_out(up, UART_SCR, canary);
764 if (serial_in(up, UART_SCR) == canary)
768 uart_suspend_port(&serial8250_reg, port);
770 EXPORT_SYMBOL(serial8250_suspend_port);
773 * serial8250_resume_port - resume one serial port
774 * @line: serial line number
776 * Resume one serial port.
778 void serial8250_resume_port(int line)
780 struct uart_8250_port *up = &serial8250_ports[line];
781 struct uart_port *port = &up->port;
785 if (up->capabilities & UART_NATSEMI) {
786 /* Ensure it's still in high speed mode */
787 serial_port_out(port, UART_LCR, 0xE0);
789 ns16550a_goto_highspeed(up);
791 serial_port_out(port, UART_LCR, 0);
792 port->uartclk = 921600*16;
794 uart_resume_port(&serial8250_reg, port);
796 EXPORT_SYMBOL(serial8250_resume_port);
799 * Register a set of serial devices attached to a platform device. The
800 * list is terminated with a zero flags entry, which means we expect
801 * all entries to have at least UPF_BOOT_AUTOCONF set.
803 static int serial8250_probe(struct platform_device *dev)
805 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
806 struct uart_8250_port uart;
807 int ret, i, irqflag = 0;
809 memset(&uart, 0, sizeof(uart));
812 irqflag = IRQF_SHARED;
814 for (i = 0; p && p->flags != 0; p++, i++) {
815 uart.port.iobase = p->iobase;
816 uart.port.membase = p->membase;
817 uart.port.irq = p->irq;
818 uart.port.irqflags = p->irqflags;
819 uart.port.uartclk = p->uartclk;
820 uart.port.regshift = p->regshift;
821 uart.port.iotype = p->iotype;
822 uart.port.flags = p->flags;
823 uart.port.mapbase = p->mapbase;
824 uart.port.hub6 = p->hub6;
825 uart.port.has_sysrq = p->has_sysrq;
826 uart.port.private_data = p->private_data;
827 uart.port.type = p->type;
828 uart.port.serial_in = p->serial_in;
829 uart.port.serial_out = p->serial_out;
830 uart.port.handle_irq = p->handle_irq;
831 uart.port.handle_break = p->handle_break;
832 uart.port.set_termios = p->set_termios;
833 uart.port.set_ldisc = p->set_ldisc;
834 uart.port.get_mctrl = p->get_mctrl;
835 uart.port.pm = p->pm;
836 uart.port.dev = &dev->dev;
837 uart.port.irqflags |= irqflag;
838 ret = serial8250_register_8250_port(&uart);
840 dev_err(&dev->dev, "unable to register port at index %d "
841 "(IO%lx MEM%llx IRQ%d): %d\n", i,
842 p->iobase, (unsigned long long)p->mapbase,
850 * Remove serial ports registered against a platform device.
852 static int serial8250_remove(struct platform_device *dev)
856 for (i = 0; i < nr_uarts; i++) {
857 struct uart_8250_port *up = &serial8250_ports[i];
859 if (up->port.dev == &dev->dev)
860 serial8250_unregister_port(i);
865 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
869 for (i = 0; i < UART_NR; i++) {
870 struct uart_8250_port *up = &serial8250_ports[i];
872 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
873 uart_suspend_port(&serial8250_reg, &up->port);
879 static int serial8250_resume(struct platform_device *dev)
883 for (i = 0; i < UART_NR; i++) {
884 struct uart_8250_port *up = &serial8250_ports[i];
886 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
887 serial8250_resume_port(i);
893 static struct platform_driver serial8250_isa_driver = {
894 .probe = serial8250_probe,
895 .remove = serial8250_remove,
896 .suspend = serial8250_suspend,
897 .resume = serial8250_resume,
899 .name = "serial8250",
904 * This "device" covers _all_ ISA 8250-compatible serial devices listed
905 * in the table in include/asm/serial.h
907 static struct platform_device *serial8250_isa_devs;
910 * serial8250_register_8250_port and serial8250_unregister_port allows for
911 * 16x50 serial ports to be configured at run-time, to support PCMCIA
912 * modems and PCI multiport cards.
914 static DEFINE_MUTEX(serial_mutex);
916 static struct uart_8250_port *serial8250_find_match_or_unused(const struct uart_port *port)
921 * First, find a port entry which matches.
923 for (i = 0; i < nr_uarts; i++)
924 if (uart_match_port(&serial8250_ports[i].port, port))
925 return &serial8250_ports[i];
927 /* try line number first if still available */
929 if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
930 serial8250_ports[i].port.iobase == 0)
931 return &serial8250_ports[i];
933 * We didn't find a matching entry, so look for the first
934 * free entry. We look for one which hasn't been previously
935 * used (indicated by zero iobase).
937 for (i = 0; i < nr_uarts; i++)
938 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
939 serial8250_ports[i].port.iobase == 0)
940 return &serial8250_ports[i];
943 * That also failed. Last resort is to find any entry which
944 * doesn't have a real port associated with it.
946 for (i = 0; i < nr_uarts; i++)
947 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
948 return &serial8250_ports[i];
953 static void serial_8250_overrun_backoff_work(struct work_struct *work)
955 struct uart_8250_port *up =
956 container_of(to_delayed_work(work), struct uart_8250_port,
958 struct uart_port *port = &up->port;
961 spin_lock_irqsave(&port->lock, flags);
962 up->ier |= UART_IER_RLSI | UART_IER_RDI;
963 up->port.read_status_mask |= UART_LSR_DR;
964 serial_out(up, UART_IER, up->ier);
965 spin_unlock_irqrestore(&port->lock, flags);
969 * serial8250_register_8250_port - register a serial port
970 * @up: serial port template
972 * Configure the serial port specified by the request. If the
973 * port exists and is in use, it is hung up and unregistered
976 * The port is then probed and if necessary the IRQ is autodetected
977 * If this fails an error is returned.
979 * On success the port is ready to use and the line number is returned.
981 int serial8250_register_8250_port(const struct uart_8250_port *up)
983 struct uart_8250_port *uart;
986 if (up->port.uartclk == 0)
989 mutex_lock(&serial_mutex);
991 uart = serial8250_find_match_or_unused(&up->port);
992 if (uart && uart->port.type != PORT_8250_CIR) {
993 struct mctrl_gpios *gpios;
996 uart_remove_one_port(&serial8250_reg, &uart->port);
998 uart->port.iobase = up->port.iobase;
999 uart->port.membase = up->port.membase;
1000 uart->port.irq = up->port.irq;
1001 uart->port.irqflags = up->port.irqflags;
1002 uart->port.uartclk = up->port.uartclk;
1003 uart->port.fifosize = up->port.fifosize;
1004 uart->port.regshift = up->port.regshift;
1005 uart->port.iotype = up->port.iotype;
1006 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
1007 uart->bugs = up->bugs;
1008 uart->port.mapbase = up->port.mapbase;
1009 uart->port.mapsize = up->port.mapsize;
1010 uart->port.private_data = up->port.private_data;
1011 uart->tx_loadsz = up->tx_loadsz;
1012 uart->capabilities = up->capabilities;
1013 uart->port.throttle = up->port.throttle;
1014 uart->port.unthrottle = up->port.unthrottle;
1015 uart->port.rs485_config = up->port.rs485_config;
1016 uart->port.rs485_supported = up->port.rs485_supported;
1017 uart->port.rs485 = up->port.rs485;
1018 uart->rs485_start_tx = up->rs485_start_tx;
1019 uart->rs485_stop_tx = up->rs485_stop_tx;
1020 uart->lsr_save_mask = up->lsr_save_mask;
1021 uart->dma = up->dma;
1023 /* Take tx_loadsz from fifosize if it wasn't set separately */
1024 if (uart->port.fifosize && !uart->tx_loadsz)
1025 uart->tx_loadsz = uart->port.fifosize;
1028 uart->port.dev = up->port.dev;
1029 ret = uart_get_rs485_mode(&uart->port);
1034 if (up->port.flags & UPF_FIXED_TYPE)
1035 uart->port.type = up->port.type;
1038 * Only call mctrl_gpio_init(), if the device has no ACPI
1041 if (!has_acpi_companion(uart->port.dev)) {
1042 gpios = mctrl_gpio_init(&uart->port, 0);
1043 if (IS_ERR(gpios)) {
1044 ret = PTR_ERR(gpios);
1047 uart->gpios = gpios;
1051 serial8250_set_defaults(uart);
1053 /* Possibly override default I/O functions. */
1054 if (up->port.serial_in)
1055 uart->port.serial_in = up->port.serial_in;
1056 if (up->port.serial_out)
1057 uart->port.serial_out = up->port.serial_out;
1058 if (up->port.handle_irq)
1059 uart->port.handle_irq = up->port.handle_irq;
1060 /* Possibly override set_termios call */
1061 if (up->port.set_termios)
1062 uart->port.set_termios = up->port.set_termios;
1063 if (up->port.set_ldisc)
1064 uart->port.set_ldisc = up->port.set_ldisc;
1065 if (up->port.get_mctrl)
1066 uart->port.get_mctrl = up->port.get_mctrl;
1067 if (up->port.set_mctrl)
1068 uart->port.set_mctrl = up->port.set_mctrl;
1069 if (up->port.get_divisor)
1070 uart->port.get_divisor = up->port.get_divisor;
1071 if (up->port.set_divisor)
1072 uart->port.set_divisor = up->port.set_divisor;
1073 if (up->port.startup)
1074 uart->port.startup = up->port.startup;
1075 if (up->port.shutdown)
1076 uart->port.shutdown = up->port.shutdown;
1078 uart->port.pm = up->port.pm;
1079 if (up->port.handle_break)
1080 uart->port.handle_break = up->port.handle_break;
1082 uart->dl_read = up->dl_read;
1084 uart->dl_write = up->dl_write;
1086 if (uart->port.type != PORT_8250_CIR) {
1087 if (serial8250_isa_config != NULL)
1088 serial8250_isa_config(0, &uart->port,
1089 &uart->capabilities);
1091 serial8250_apply_quirks(uart);
1092 ret = uart_add_one_port(&serial8250_reg,
1097 ret = uart->port.line;
1099 dev_info(uart->port.dev,
1100 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
1102 (unsigned long long)uart->port.mapbase,
1108 if (!uart->lsr_save_mask)
1109 uart->lsr_save_mask = LSR_SAVE_FLAGS; /* Use default LSR mask */
1111 /* Initialise interrupt backoff work if required */
1112 if (up->overrun_backoff_time_ms > 0) {
1113 uart->overrun_backoff_time_ms =
1114 up->overrun_backoff_time_ms;
1115 INIT_DELAYED_WORK(&uart->overrun_backoff,
1116 serial_8250_overrun_backoff_work);
1118 uart->overrun_backoff_time_ms = 0;
1122 mutex_unlock(&serial_mutex);
1127 uart->port.dev = NULL;
1128 mutex_unlock(&serial_mutex);
1131 EXPORT_SYMBOL(serial8250_register_8250_port);
1134 * serial8250_unregister_port - remove a 16x50 serial port at runtime
1135 * @line: serial line number
1137 * Remove one serial port. This may not be called from interrupt
1138 * context. We hand the port back to the our control.
1140 void serial8250_unregister_port(int line)
1142 struct uart_8250_port *uart = &serial8250_ports[line];
1144 mutex_lock(&serial_mutex);
1147 unsigned long flags;
1149 spin_lock_irqsave(&uart->port.lock, flags);
1150 serial8250_em485_destroy(uart);
1151 spin_unlock_irqrestore(&uart->port.lock, flags);
1154 uart_remove_one_port(&serial8250_reg, &uart->port);
1155 if (serial8250_isa_devs) {
1156 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
1157 uart->port.type = PORT_UNKNOWN;
1158 uart->port.dev = &serial8250_isa_devs->dev;
1159 uart->capabilities = 0;
1160 serial8250_apply_quirks(uart);
1161 uart_add_one_port(&serial8250_reg, &uart->port);
1163 uart->port.dev = NULL;
1165 mutex_unlock(&serial_mutex);
1167 EXPORT_SYMBOL(serial8250_unregister_port);
1169 static int __init serial8250_init(void)
1176 serial8250_isa_init_ports();
1178 pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %sabled\n",
1179 nr_uarts, share_irqs ? "en" : "dis");
1182 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
1184 serial8250_reg.nr = UART_NR;
1185 ret = uart_register_driver(&serial8250_reg);
1190 ret = serial8250_pnp_init();
1192 goto unreg_uart_drv;
1194 serial8250_isa_devs = platform_device_alloc("serial8250",
1195 PLAT8250_DEV_LEGACY);
1196 if (!serial8250_isa_devs) {
1201 ret = platform_device_add(serial8250_isa_devs);
1205 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
1207 ret = platform_driver_register(&serial8250_isa_driver);
1211 platform_device_del(serial8250_isa_devs);
1213 platform_device_put(serial8250_isa_devs);
1215 serial8250_pnp_exit();
1218 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1220 uart_unregister_driver(&serial8250_reg);
1226 static void __exit serial8250_exit(void)
1228 struct platform_device *isa_dev = serial8250_isa_devs;
1231 * This tells serial8250_unregister_port() not to re-register
1232 * the ports (thereby making serial8250_isa_driver permanently
1235 serial8250_isa_devs = NULL;
1237 platform_driver_unregister(&serial8250_isa_driver);
1238 platform_device_unregister(isa_dev);
1240 serial8250_pnp_exit();
1243 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1245 uart_unregister_driver(&serial8250_reg);
1249 module_init(serial8250_init);
1250 module_exit(serial8250_exit);
1252 MODULE_LICENSE("GPL");
1253 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
1255 module_param_hw(share_irqs, uint, other, 0644);
1256 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
1258 module_param(nr_uarts, uint, 0644);
1259 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
1261 module_param(skip_txen_test, uint, 0644);
1262 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
1264 #ifdef CONFIG_SERIAL_8250_RSA
1265 module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
1266 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
1268 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
1270 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
1272 /* This module was renamed to 8250_core in 3.7. Keep the old "8250" name
1273 * working as well for the module options so we don't break people. We
1274 * need to keep the names identical and the convenient macros will happily
1275 * refuse to let us do that by failing the build with redefinition errors
1276 * of global variables. So we stick them inside a dummy function to avoid
1277 * those conflicts. The options still get parsed, and the redefined
1278 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
1280 * This is hacky. I'm sorry.
1282 static void __used s8250_options(void)
1284 #undef MODULE_PARAM_PREFIX
1285 #define MODULE_PARAM_PREFIX "8250_core."
1287 module_param_cb(share_irqs, ¶m_ops_uint, &share_irqs, 0644);
1288 module_param_cb(nr_uarts, ¶m_ops_uint, &nr_uarts, 0644);
1289 module_param_cb(skip_txen_test, ¶m_ops_uint, &skip_txen_test, 0644);
1290 #ifdef CONFIG_SERIAL_8250_RSA
1291 __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
1292 ¶m_array_ops, .arr = &__param_arr_probe_rsa,
1297 MODULE_ALIAS("8250_core");