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/string_helpers.h>
36 #include <linux/uaccess.h>
39 #include <linux/sunserialcore.h>
48 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
49 * is unsafe when used on edge-triggered interrupts.
51 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
53 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
55 static struct uart_driver serial8250_reg;
57 static unsigned int skip_txen_test; /* force skip of txen test at init time */
59 #define PASS_LIMIT 512
61 #include <asm/serial.h>
63 * SERIAL_PORT_DFNS tells us about built-in ports that have no
64 * standard enumeration mechanism. Platforms that can find all
65 * serial ports via mechanisms like ACPI or PCI need not supply it.
67 #ifndef SERIAL_PORT_DFNS
68 #define SERIAL_PORT_DFNS
71 static const struct old_serial_port old_serial_port[] = {
72 SERIAL_PORT_DFNS /* defined in asm/serial.h */
75 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
77 #ifdef CONFIG_SERIAL_8250_RSA
79 #define PORT_RSA_MAX 4
80 static unsigned long probe_rsa[PORT_RSA_MAX];
81 static unsigned int probe_rsa_count;
82 #endif /* CONFIG_SERIAL_8250_RSA */
85 struct hlist_node node;
87 spinlock_t lock; /* Protects list not the hash */
88 struct list_head *head;
91 #define NR_IRQ_HASH 32 /* Can be adjusted later */
92 static struct hlist_head irq_lists[NR_IRQ_HASH];
93 static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
96 * This is the serial driver's interrupt routine.
98 * Arjan thinks the old way was overly complex, so it got simplified.
99 * Alan disagrees, saying that need the complexity to handle the weird
100 * nature of ISA shared interrupts. (This is a special exception.)
102 * In order to handle ISA shared interrupts properly, we need to check
103 * that all ports have been serviced, and therefore the ISA interrupt
104 * line has been de-asserted.
106 * This means we need to loop through all ports. checking that they
107 * don't have an interrupt pending.
109 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
111 struct irq_info *i = dev_id;
112 struct list_head *l, *end = NULL;
113 int pass_counter = 0, handled = 0;
115 pr_debug("%s(%d): start\n", __func__, irq);
121 struct uart_8250_port *up;
122 struct uart_port *port;
124 up = list_entry(l, struct uart_8250_port, list);
127 if (port->handle_irq(port)) {
130 } else if (end == NULL)
135 if (l == i->head && pass_counter++ > PASS_LIMIT)
139 spin_unlock(&i->lock);
141 pr_debug("%s(%d): end\n", __func__, irq);
143 return IRQ_RETVAL(handled);
147 * To support ISA shared interrupts, we need to have one interrupt
148 * handler that ensures that the IRQ line has been deasserted
149 * before returning. Failing to do this will result in the IRQ
150 * line being stuck active, and, since ISA irqs are edge triggered,
151 * no more IRQs will be seen.
153 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
155 spin_lock_irq(&i->lock);
157 if (!list_empty(i->head)) {
158 if (i->head == &up->list)
159 i->head = i->head->next;
162 BUG_ON(i->head != &up->list);
165 spin_unlock_irq(&i->lock);
166 /* List empty so throw away the hash node */
167 if (i->head == NULL) {
173 static int serial_link_irq_chain(struct uart_8250_port *up)
175 struct hlist_head *h;
179 mutex_lock(&hash_mutex);
181 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
183 hlist_for_each_entry(i, h, node)
184 if (i->irq == up->port.irq)
188 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
190 mutex_unlock(&hash_mutex);
193 spin_lock_init(&i->lock);
194 i->irq = up->port.irq;
195 hlist_add_head(&i->node, h);
197 mutex_unlock(&hash_mutex);
199 spin_lock_irq(&i->lock);
202 list_add(&up->list, i->head);
203 spin_unlock_irq(&i->lock);
207 INIT_LIST_HEAD(&up->list);
209 spin_unlock_irq(&i->lock);
210 ret = request_irq(up->port.irq, serial8250_interrupt,
211 up->port.irqflags, up->port.name, i);
213 serial_do_unlink(i, up);
219 static void serial_unlink_irq_chain(struct uart_8250_port *up)
222 struct hlist_head *h;
224 mutex_lock(&hash_mutex);
226 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
228 hlist_for_each_entry(i, h, node)
229 if (i->irq == up->port.irq)
233 BUG_ON(i->head == NULL);
235 if (list_empty(i->head))
236 free_irq(up->port.irq, i);
238 serial_do_unlink(i, up);
239 mutex_unlock(&hash_mutex);
243 * This function is used to handle ports that do not have an
244 * interrupt. This doesn't work very well for 16450's, but gives
245 * barely passable results for a 16550A. (Although at the expense
246 * of much CPU overhead).
248 static void serial8250_timeout(struct timer_list *t)
250 struct uart_8250_port *up = from_timer(up, t, timer);
252 up->port.handle_irq(&up->port);
253 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
256 static void serial8250_backup_timeout(struct timer_list *t)
258 struct uart_8250_port *up = from_timer(up, t, timer);
259 unsigned int iir, ier = 0, lsr;
262 spin_lock_irqsave(&up->port.lock, flags);
265 * Must disable interrupts or else we risk racing with the interrupt
269 ier = serial_in(up, UART_IER);
270 serial_out(up, UART_IER, 0);
273 iir = serial_in(up, UART_IIR);
276 * This should be a safe test for anyone who doesn't trust the
277 * IIR bits on their UART, but it's specifically designed for
278 * the "Diva" UART used on the management processor on many HP
279 * ia64 and parisc boxes.
281 lsr = serial_lsr_in(up);
282 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
283 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
284 (lsr & UART_LSR_THRE)) {
285 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
286 iir |= UART_IIR_THRI;
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 void univ8250_setup_timer(struct uart_8250_port *up)
304 struct uart_port *port = &up->port;
307 * The above check will only give an accurate result the first time
308 * the port is opened so this value needs to be preserved.
310 if (up->bugs & UART_BUG_THRE) {
311 pr_debug("%s - using backup timer\n", port->name);
313 up->timer.function = serial8250_backup_timeout;
314 mod_timer(&up->timer, jiffies +
315 uart_poll_timeout(port) + HZ / 5);
319 * If the "interrupt" for this port doesn't correspond with any
320 * hardware interrupt, we use a timer-based system. The original
321 * driver used to do this with IRQ0.
324 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
327 static int univ8250_setup_irq(struct uart_8250_port *up)
329 struct uart_port *port = &up->port;
332 return serial_link_irq_chain(up);
337 static void univ8250_release_irq(struct uart_8250_port *up)
339 struct uart_port *port = &up->port;
341 del_timer_sync(&up->timer);
342 up->timer.function = serial8250_timeout;
344 serial_unlink_irq_chain(up);
347 #ifdef CONFIG_SERIAL_8250_RSA
348 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
350 unsigned long start = UART_RSA_BASE << up->port.regshift;
351 unsigned int size = 8 << up->port.regshift;
352 struct uart_port *port = &up->port;
355 switch (port->iotype) {
358 start += port->iobase;
359 if (request_region(start, size, "serial-rsa"))
369 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
371 unsigned long offset = UART_RSA_BASE << up->port.regshift;
372 unsigned int size = 8 << up->port.regshift;
373 struct uart_port *port = &up->port;
375 switch (port->iotype) {
378 release_region(port->iobase + offset, size);
384 static const struct uart_ops *base_ops;
385 static struct uart_ops univ8250_port_ops;
387 static const struct uart_8250_ops univ8250_driver_ops = {
388 .setup_irq = univ8250_setup_irq,
389 .release_irq = univ8250_release_irq,
390 .setup_timer = univ8250_setup_timer,
393 static struct uart_8250_port serial8250_ports[UART_NR];
396 * serial8250_get_port - retrieve struct uart_8250_port
397 * @line: serial line number
399 * This function retrieves struct uart_8250_port for the specific line.
400 * This struct *must* *not* be used to perform a 8250 or serial core operation
401 * which is not accessible otherwise. Its only purpose is to make the struct
402 * accessible to the runtime-pm callbacks for context suspend/restore.
403 * The lock assumption made here is none because runtime-pm suspend/resume
404 * callbacks should not be invoked if there is any operation performed on the
407 struct uart_8250_port *serial8250_get_port(int line)
409 return &serial8250_ports[line];
411 EXPORT_SYMBOL_GPL(serial8250_get_port);
413 static void (*serial8250_isa_config)(int port, struct uart_port *up,
416 void serial8250_set_isa_configurator(
417 void (*v)(int port, struct uart_port *up, u32 *capabilities))
419 serial8250_isa_config = v;
421 EXPORT_SYMBOL(serial8250_set_isa_configurator);
423 #ifdef CONFIG_SERIAL_8250_RSA
425 static void univ8250_config_port(struct uart_port *port, int flags)
427 struct uart_8250_port *up = up_to_u8250p(port);
429 up->probe &= ~UART_PROBE_RSA;
430 if (port->type == PORT_RSA) {
431 if (serial8250_request_rsa_resource(up) == 0)
432 up->probe |= UART_PROBE_RSA;
433 } else if (flags & UART_CONFIG_TYPE) {
436 for (i = 0; i < probe_rsa_count; i++) {
437 if (probe_rsa[i] == up->port.iobase) {
438 if (serial8250_request_rsa_resource(up) == 0)
439 up->probe |= UART_PROBE_RSA;
445 base_ops->config_port(port, flags);
447 if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
448 serial8250_release_rsa_resource(up);
451 static int univ8250_request_port(struct uart_port *port)
453 struct uart_8250_port *up = up_to_u8250p(port);
456 ret = base_ops->request_port(port);
457 if (ret == 0 && port->type == PORT_RSA) {
458 ret = serial8250_request_rsa_resource(up);
460 base_ops->release_port(port);
466 static void univ8250_release_port(struct uart_port *port)
468 struct uart_8250_port *up = up_to_u8250p(port);
470 if (port->type == PORT_RSA)
471 serial8250_release_rsa_resource(up);
472 base_ops->release_port(port);
475 static void univ8250_rsa_support(struct uart_ops *ops)
477 ops->config_port = univ8250_config_port;
478 ops->request_port = univ8250_request_port;
479 ops->release_port = univ8250_release_port;
483 #define univ8250_rsa_support(x) do { } while (0)
484 #endif /* CONFIG_SERIAL_8250_RSA */
486 static inline void serial8250_apply_quirks(struct uart_8250_port *up)
488 up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0;
491 static void __init serial8250_isa_init_ports(void)
493 struct uart_8250_port *up;
494 static int first = 1;
501 if (nr_uarts > UART_NR)
504 for (i = 0; i < nr_uarts; i++) {
505 struct uart_8250_port *up = &serial8250_ports[i];
506 struct uart_port *port = &up->port;
509 serial8250_init_port(up);
511 base_ops = port->ops;
512 port->ops = &univ8250_port_ops;
514 timer_setup(&up->timer, serial8250_timeout, 0);
516 up->ops = &univ8250_driver_ops;
518 if (IS_ENABLED(CONFIG_ALPHA_JENSEN) ||
519 (IS_ENABLED(CONFIG_ALPHA_GENERIC) && alpha_jensen()))
520 port->set_mctrl = alpha_jensen_set_mctrl;
522 serial8250_set_defaults(up);
525 /* chain base port ops to support Remote Supervisor Adapter */
526 univ8250_port_ops = *base_ops;
527 univ8250_rsa_support(&univ8250_port_ops);
530 irqflag = IRQF_SHARED;
532 for (i = 0, up = serial8250_ports;
533 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
535 struct uart_port *port = &up->port;
537 port->iobase = old_serial_port[i].port;
538 port->irq = irq_canonicalize(old_serial_port[i].irq);
540 port->uartclk = old_serial_port[i].baud_base * 16;
541 port->flags = old_serial_port[i].flags;
543 port->membase = old_serial_port[i].iomem_base;
544 port->iotype = old_serial_port[i].io_type;
545 port->regshift = old_serial_port[i].iomem_reg_shift;
547 port->irqflags |= irqflag;
548 if (serial8250_isa_config != NULL)
549 serial8250_isa_config(i, &up->port, &up->capabilities);
554 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
558 for (i = 0; i < nr_uarts; i++) {
559 struct uart_8250_port *up = &serial8250_ports[i];
561 if (up->port.type == PORT_8250_CIR)
569 if (uart_console_registered(&up->port))
570 pm_runtime_get_sync(up->port.dev);
572 serial8250_apply_quirks(up);
573 uart_add_one_port(drv, &up->port);
577 #ifdef CONFIG_SERIAL_8250_CONSOLE
579 static void univ8250_console_write(struct console *co, const char *s,
582 struct uart_8250_port *up = &serial8250_ports[co->index];
584 serial8250_console_write(up, s, count);
587 static int univ8250_console_setup(struct console *co, char *options)
589 struct uart_port *port;
593 * Check whether an invalid uart number has been specified, and
594 * if so, search for the first available port that does have
597 if (co->index >= nr_uarts)
599 port = &serial8250_ports[co->index].port;
600 /* link port to console */
603 retval = serial8250_console_setup(port, options, false);
609 static int univ8250_console_exit(struct console *co)
611 struct uart_port *port;
613 port = &serial8250_ports[co->index].port;
614 return serial8250_console_exit(port);
618 * univ8250_console_match - non-standard console matching
619 * @co: registering console
620 * @name: name from console command line
621 * @idx: index from console command line
622 * @options: ptr to option string from console command line
624 * Only attempts to match console command lines of the form:
625 * console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>]
626 * console=uart[8250],0x<addr>[,<options>]
627 * This form is used to register an initial earlycon boot console and
628 * replace it with the serial8250_console at 8250 driver init.
630 * Performs console setup for a match (as required by interface)
631 * If no <options> are specified, then assume the h/w is already setup.
633 * Returns 0 if console matches; otherwise non-zero to use default matching
635 static int univ8250_console_match(struct console *co, char *name, int idx,
638 char match[] = "uart"; /* 8250-specific earlycon name */
639 unsigned char iotype;
640 resource_size_t addr;
643 if (strncmp(name, match, 4) != 0)
646 if (uart_parse_earlycon(options, &iotype, &addr, &options))
649 /* try to match the port specified on the command line */
650 for (i = 0; i < nr_uarts; i++) {
651 struct uart_port *port = &serial8250_ports[i].port;
653 if (port->iotype != iotype)
655 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
656 iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
657 && (port->mapbase != addr))
659 if (iotype == UPIO_PORT && port->iobase != addr)
664 return serial8250_console_setup(port, options, true);
670 static struct console univ8250_console = {
672 .write = univ8250_console_write,
673 .device = uart_console_device,
674 .setup = univ8250_console_setup,
675 .exit = univ8250_console_exit,
676 .match = univ8250_console_match,
677 .flags = CON_PRINTBUFFER | CON_ANYTIME,
679 .data = &serial8250_reg,
682 static int __init univ8250_console_init(void)
687 serial8250_isa_init_ports();
688 register_console(&univ8250_console);
691 console_initcall(univ8250_console_init);
693 #define SERIAL8250_CONSOLE (&univ8250_console)
695 #define SERIAL8250_CONSOLE NULL
698 static struct uart_driver serial8250_reg = {
699 .owner = THIS_MODULE,
700 .driver_name = "serial",
704 .cons = SERIAL8250_CONSOLE,
708 * early_serial_setup - early registration for 8250 ports
710 * Setup an 8250 port structure prior to console initialisation. Use
711 * after console initialisation will cause undefined behaviour.
713 int __init early_serial_setup(struct uart_port *port)
717 if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
720 serial8250_isa_init_ports();
721 p = &serial8250_ports[port->line].port;
722 p->iobase = port->iobase;
723 p->membase = port->membase;
725 p->irqflags = port->irqflags;
726 p->uartclk = port->uartclk;
727 p->fifosize = port->fifosize;
728 p->regshift = port->regshift;
729 p->iotype = port->iotype;
730 p->flags = port->flags;
731 p->mapbase = port->mapbase;
732 p->mapsize = port->mapsize;
733 p->private_data = port->private_data;
734 p->type = port->type;
735 p->line = port->line;
737 serial8250_set_defaults(up_to_u8250p(p));
740 p->serial_in = port->serial_in;
741 if (port->serial_out)
742 p->serial_out = port->serial_out;
743 if (port->handle_irq)
744 p->handle_irq = port->handle_irq;
750 * serial8250_suspend_port - suspend one serial port
751 * @line: serial line number
753 * Suspend one serial port.
755 void serial8250_suspend_port(int line)
757 struct uart_8250_port *up = &serial8250_ports[line];
758 struct uart_port *port = &up->port;
760 if (!console_suspend_enabled && uart_console(port) &&
761 port->type != PORT_8250) {
762 unsigned char canary = 0xa5;
764 serial_out(up, UART_SCR, canary);
765 if (serial_in(up, UART_SCR) == canary)
769 uart_suspend_port(&serial8250_reg, port);
771 EXPORT_SYMBOL(serial8250_suspend_port);
774 * serial8250_resume_port - resume one serial port
775 * @line: serial line number
777 * Resume one serial port.
779 void serial8250_resume_port(int line)
781 struct uart_8250_port *up = &serial8250_ports[line];
782 struct uart_port *port = &up->port;
786 if (up->capabilities & UART_NATSEMI) {
787 /* Ensure it's still in high speed mode */
788 serial_port_out(port, UART_LCR, 0xE0);
790 ns16550a_goto_highspeed(up);
792 serial_port_out(port, UART_LCR, 0);
793 port->uartclk = 921600*16;
795 uart_resume_port(&serial8250_reg, port);
797 EXPORT_SYMBOL(serial8250_resume_port);
800 * Register a set of serial devices attached to a platform device. The
801 * list is terminated with a zero flags entry, which means we expect
802 * all entries to have at least UPF_BOOT_AUTOCONF set.
804 static int serial8250_probe(struct platform_device *dev)
806 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
807 struct uart_8250_port uart;
808 int ret, i, irqflag = 0;
810 memset(&uart, 0, sizeof(uart));
813 irqflag = IRQF_SHARED;
815 for (i = 0; p && p->flags != 0; p++, i++) {
816 uart.port.iobase = p->iobase;
817 uart.port.membase = p->membase;
818 uart.port.irq = p->irq;
819 uart.port.irqflags = p->irqflags;
820 uart.port.uartclk = p->uartclk;
821 uart.port.regshift = p->regshift;
822 uart.port.iotype = p->iotype;
823 uart.port.flags = p->flags;
824 uart.port.mapbase = p->mapbase;
825 uart.port.hub6 = p->hub6;
826 uart.port.has_sysrq = p->has_sysrq;
827 uart.port.private_data = p->private_data;
828 uart.port.type = p->type;
829 uart.port.serial_in = p->serial_in;
830 uart.port.serial_out = p->serial_out;
831 uart.port.handle_irq = p->handle_irq;
832 uart.port.handle_break = p->handle_break;
833 uart.port.set_termios = p->set_termios;
834 uart.port.set_ldisc = p->set_ldisc;
835 uart.port.get_mctrl = p->get_mctrl;
836 uart.port.pm = p->pm;
837 uart.port.dev = &dev->dev;
838 uart.port.irqflags |= irqflag;
839 ret = serial8250_register_8250_port(&uart);
841 dev_err(&dev->dev, "unable to register port at index %d "
842 "(IO%lx MEM%llx IRQ%d): %d\n", i,
843 p->iobase, (unsigned long long)p->mapbase,
851 * Remove serial ports registered against a platform device.
853 static int serial8250_remove(struct platform_device *dev)
857 for (i = 0; i < nr_uarts; i++) {
858 struct uart_8250_port *up = &serial8250_ports[i];
860 if (up->port.dev == &dev->dev)
861 serial8250_unregister_port(i);
866 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
870 for (i = 0; i < UART_NR; i++) {
871 struct uart_8250_port *up = &serial8250_ports[i];
873 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
874 uart_suspend_port(&serial8250_reg, &up->port);
880 static int serial8250_resume(struct platform_device *dev)
884 for (i = 0; i < UART_NR; i++) {
885 struct uart_8250_port *up = &serial8250_ports[i];
887 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
888 serial8250_resume_port(i);
894 static struct platform_driver serial8250_isa_driver = {
895 .probe = serial8250_probe,
896 .remove = serial8250_remove,
897 .suspend = serial8250_suspend,
898 .resume = serial8250_resume,
900 .name = "serial8250",
905 * This "device" covers _all_ ISA 8250-compatible serial devices listed
906 * in the table in include/asm/serial.h
908 static struct platform_device *serial8250_isa_devs;
911 * serial8250_register_8250_port and serial8250_unregister_port allows for
912 * 16x50 serial ports to be configured at run-time, to support PCMCIA
913 * modems and PCI multiport cards.
915 static DEFINE_MUTEX(serial_mutex);
917 static struct uart_8250_port *serial8250_find_match_or_unused(const struct uart_port *port)
922 * First, find a port entry which matches.
924 for (i = 0; i < nr_uarts; i++)
925 if (uart_match_port(&serial8250_ports[i].port, port))
926 return &serial8250_ports[i];
928 /* try line number first if still available */
930 if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
931 serial8250_ports[i].port.iobase == 0)
932 return &serial8250_ports[i];
934 * We didn't find a matching entry, so look for the first
935 * free entry. We look for one which hasn't been previously
936 * used (indicated by zero iobase).
938 for (i = 0; i < nr_uarts; i++)
939 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
940 serial8250_ports[i].port.iobase == 0)
941 return &serial8250_ports[i];
944 * That also failed. Last resort is to find any entry which
945 * doesn't have a real port associated with it.
947 for (i = 0; i < nr_uarts; i++)
948 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
949 return &serial8250_ports[i];
954 static void serial_8250_overrun_backoff_work(struct work_struct *work)
956 struct uart_8250_port *up =
957 container_of(to_delayed_work(work), struct uart_8250_port,
959 struct uart_port *port = &up->port;
962 spin_lock_irqsave(&port->lock, flags);
963 up->ier |= UART_IER_RLSI | UART_IER_RDI;
964 up->port.read_status_mask |= UART_LSR_DR;
965 serial_out(up, UART_IER, up->ier);
966 spin_unlock_irqrestore(&port->lock, flags);
970 * serial8250_register_8250_port - register a serial port
971 * @up: serial port template
973 * Configure the serial port specified by the request. If the
974 * port exists and is in use, it is hung up and unregistered
977 * The port is then probed and if necessary the IRQ is autodetected
978 * If this fails an error is returned.
980 * On success the port is ready to use and the line number is returned.
982 int serial8250_register_8250_port(const struct uart_8250_port *up)
984 struct uart_8250_port *uart;
987 if (up->port.uartclk == 0)
990 mutex_lock(&serial_mutex);
992 uart = serial8250_find_match_or_unused(&up->port);
993 if (uart && uart->port.type != PORT_8250_CIR) {
994 struct mctrl_gpios *gpios;
997 uart_remove_one_port(&serial8250_reg, &uart->port);
999 uart->port.iobase = up->port.iobase;
1000 uart->port.membase = up->port.membase;
1001 uart->port.irq = up->port.irq;
1002 uart->port.irqflags = up->port.irqflags;
1003 uart->port.uartclk = up->port.uartclk;
1004 uart->port.fifosize = up->port.fifosize;
1005 uart->port.regshift = up->port.regshift;
1006 uart->port.iotype = up->port.iotype;
1007 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
1008 uart->bugs = up->bugs;
1009 uart->port.mapbase = up->port.mapbase;
1010 uart->port.mapsize = up->port.mapsize;
1011 uart->port.private_data = up->port.private_data;
1012 uart->tx_loadsz = up->tx_loadsz;
1013 uart->capabilities = up->capabilities;
1014 uart->port.throttle = up->port.throttle;
1015 uart->port.unthrottle = up->port.unthrottle;
1016 uart->port.rs485_config = up->port.rs485_config;
1017 uart->port.rs485_supported = up->port.rs485_supported;
1018 uart->port.rs485 = up->port.rs485;
1019 uart->rs485_start_tx = up->rs485_start_tx;
1020 uart->rs485_stop_tx = up->rs485_stop_tx;
1021 uart->lsr_save_mask = up->lsr_save_mask;
1022 uart->dma = up->dma;
1024 /* Take tx_loadsz from fifosize if it wasn't set separately */
1025 if (uart->port.fifosize && !uart->tx_loadsz)
1026 uart->tx_loadsz = uart->port.fifosize;
1029 uart->port.dev = up->port.dev;
1030 ret = uart_get_rs485_mode(&uart->port);
1035 if (up->port.flags & UPF_FIXED_TYPE)
1036 uart->port.type = up->port.type;
1039 * Only call mctrl_gpio_init(), if the device has no ACPI
1042 if (!has_acpi_companion(uart->port.dev)) {
1043 gpios = mctrl_gpio_init(&uart->port, 0);
1044 if (IS_ERR(gpios)) {
1045 ret = PTR_ERR(gpios);
1048 uart->gpios = gpios;
1052 serial8250_set_defaults(uart);
1054 /* Possibly override default I/O functions. */
1055 if (up->port.serial_in)
1056 uart->port.serial_in = up->port.serial_in;
1057 if (up->port.serial_out)
1058 uart->port.serial_out = up->port.serial_out;
1059 if (up->port.handle_irq)
1060 uart->port.handle_irq = up->port.handle_irq;
1061 /* Possibly override set_termios call */
1062 if (up->port.set_termios)
1063 uart->port.set_termios = up->port.set_termios;
1064 if (up->port.set_ldisc)
1065 uart->port.set_ldisc = up->port.set_ldisc;
1066 if (up->port.get_mctrl)
1067 uart->port.get_mctrl = up->port.get_mctrl;
1068 if (up->port.set_mctrl)
1069 uart->port.set_mctrl = up->port.set_mctrl;
1070 if (up->port.get_divisor)
1071 uart->port.get_divisor = up->port.get_divisor;
1072 if (up->port.set_divisor)
1073 uart->port.set_divisor = up->port.set_divisor;
1074 if (up->port.startup)
1075 uart->port.startup = up->port.startup;
1076 if (up->port.shutdown)
1077 uart->port.shutdown = up->port.shutdown;
1079 uart->port.pm = up->port.pm;
1080 if (up->port.handle_break)
1081 uart->port.handle_break = up->port.handle_break;
1083 uart->dl_read = up->dl_read;
1085 uart->dl_write = up->dl_write;
1087 if (uart->port.type != PORT_8250_CIR) {
1088 if (serial8250_isa_config != NULL)
1089 serial8250_isa_config(0, &uart->port,
1090 &uart->capabilities);
1092 serial8250_apply_quirks(uart);
1093 ret = uart_add_one_port(&serial8250_reg,
1098 ret = uart->port.line;
1100 dev_info(uart->port.dev,
1101 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
1103 (unsigned long long)uart->port.mapbase,
1109 if (!uart->lsr_save_mask)
1110 uart->lsr_save_mask = LSR_SAVE_FLAGS; /* Use default LSR mask */
1112 /* Initialise interrupt backoff work if required */
1113 if (up->overrun_backoff_time_ms > 0) {
1114 uart->overrun_backoff_time_ms =
1115 up->overrun_backoff_time_ms;
1116 INIT_DELAYED_WORK(&uart->overrun_backoff,
1117 serial_8250_overrun_backoff_work);
1119 uart->overrun_backoff_time_ms = 0;
1123 mutex_unlock(&serial_mutex);
1128 uart->port.dev = NULL;
1129 mutex_unlock(&serial_mutex);
1132 EXPORT_SYMBOL(serial8250_register_8250_port);
1135 * serial8250_unregister_port - remove a 16x50 serial port at runtime
1136 * @line: serial line number
1138 * Remove one serial port. This may not be called from interrupt
1139 * context. We hand the port back to the our control.
1141 void serial8250_unregister_port(int line)
1143 struct uart_8250_port *uart = &serial8250_ports[line];
1145 mutex_lock(&serial_mutex);
1148 unsigned long flags;
1150 spin_lock_irqsave(&uart->port.lock, flags);
1151 serial8250_em485_destroy(uart);
1152 spin_unlock_irqrestore(&uart->port.lock, flags);
1155 uart_remove_one_port(&serial8250_reg, &uart->port);
1156 if (serial8250_isa_devs) {
1157 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
1158 uart->port.type = PORT_UNKNOWN;
1159 uart->port.dev = &serial8250_isa_devs->dev;
1160 uart->capabilities = 0;
1161 serial8250_apply_quirks(uart);
1162 uart_add_one_port(&serial8250_reg, &uart->port);
1164 uart->port.dev = NULL;
1166 mutex_unlock(&serial_mutex);
1168 EXPORT_SYMBOL(serial8250_unregister_port);
1170 static int __init serial8250_init(void)
1177 serial8250_isa_init_ports();
1179 pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %s\n",
1180 nr_uarts, str_enabled_disabled(share_irqs));
1183 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
1185 serial8250_reg.nr = UART_NR;
1186 ret = uart_register_driver(&serial8250_reg);
1191 ret = serial8250_pnp_init();
1193 goto unreg_uart_drv;
1195 serial8250_isa_devs = platform_device_alloc("serial8250",
1196 PLAT8250_DEV_LEGACY);
1197 if (!serial8250_isa_devs) {
1202 ret = platform_device_add(serial8250_isa_devs);
1206 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
1208 ret = platform_driver_register(&serial8250_isa_driver);
1212 platform_device_del(serial8250_isa_devs);
1214 platform_device_put(serial8250_isa_devs);
1216 serial8250_pnp_exit();
1219 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1221 uart_unregister_driver(&serial8250_reg);
1227 static void __exit serial8250_exit(void)
1229 struct platform_device *isa_dev = serial8250_isa_devs;
1232 * This tells serial8250_unregister_port() not to re-register
1233 * the ports (thereby making serial8250_isa_driver permanently
1236 serial8250_isa_devs = NULL;
1238 platform_driver_unregister(&serial8250_isa_driver);
1239 platform_device_unregister(isa_dev);
1241 serial8250_pnp_exit();
1244 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1246 uart_unregister_driver(&serial8250_reg);
1250 module_init(serial8250_init);
1251 module_exit(serial8250_exit);
1253 MODULE_LICENSE("GPL");
1254 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
1256 module_param_hw(share_irqs, uint, other, 0644);
1257 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
1259 module_param(nr_uarts, uint, 0644);
1260 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
1262 module_param(skip_txen_test, uint, 0644);
1263 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
1265 #ifdef CONFIG_SERIAL_8250_RSA
1266 module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
1267 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
1269 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
1271 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
1273 /* This module was renamed to 8250_core in 3.7. Keep the old "8250" name
1274 * working as well for the module options so we don't break people. We
1275 * need to keep the names identical and the convenient macros will happily
1276 * refuse to let us do that by failing the build with redefinition errors
1277 * of global variables. So we stick them inside a dummy function to avoid
1278 * those conflicts. The options still get parsed, and the redefined
1279 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
1281 * This is hacky. I'm sorry.
1283 static void __used s8250_options(void)
1285 #undef MODULE_PARAM_PREFIX
1286 #define MODULE_PARAM_PREFIX "8250_core."
1288 module_param_cb(share_irqs, ¶m_ops_uint, &share_irqs, 0644);
1289 module_param_cb(nr_uarts, ¶m_ops_uint, &nr_uarts, 0644);
1290 module_param_cb(skip_txen_test, ¶m_ops_uint, &skip_txen_test, 0644);
1291 #ifdef CONFIG_SERIAL_8250_RSA
1292 __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
1293 ¶m_array_ops, .arr = &__param_arr_probe_rsa,
1298 MODULE_ALIAS("8250_core");