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/tty.h>
27 #include <linux/ratelimit.h>
28 #include <linux/tty_flip.h>
29 #include <linux/serial.h>
30 #include <linux/serial_8250.h>
31 #include <linux/nmi.h>
32 #include <linux/mutex.h>
33 #include <linux/slab.h>
34 #include <linux/uaccess.h>
37 #include <linux/sunserialcore.h>
46 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
47 * is unsafe when used on edge-triggered interrupts.
49 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
51 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
53 static struct uart_driver serial8250_reg;
55 static unsigned int skip_txen_test; /* force skip of txen test at init time */
57 #define PASS_LIMIT 512
59 #include <asm/serial.h>
61 * SERIAL_PORT_DFNS tells us about built-in ports that have no
62 * standard enumeration mechanism. Platforms that can find all
63 * serial ports via mechanisms like ACPI or PCI need not supply it.
65 #ifndef SERIAL_PORT_DFNS
66 #define SERIAL_PORT_DFNS
69 static const struct old_serial_port old_serial_port[] = {
70 SERIAL_PORT_DFNS /* defined in asm/serial.h */
73 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
75 #ifdef CONFIG_SERIAL_8250_RSA
77 #define PORT_RSA_MAX 4
78 static unsigned long probe_rsa[PORT_RSA_MAX];
79 static unsigned int probe_rsa_count;
80 #endif /* CONFIG_SERIAL_8250_RSA */
83 struct hlist_node node;
85 spinlock_t lock; /* Protects list not the hash */
86 struct list_head *head;
89 #define NR_IRQ_HASH 32 /* Can be adjusted later */
90 static struct hlist_head irq_lists[NR_IRQ_HASH];
91 static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
94 * This is the serial driver's interrupt routine.
96 * Arjan thinks the old way was overly complex, so it got simplified.
97 * Alan disagrees, saying that need the complexity to handle the weird
98 * nature of ISA shared interrupts. (This is a special exception.)
100 * In order to handle ISA shared interrupts properly, we need to check
101 * that all ports have been serviced, and therefore the ISA interrupt
102 * line has been de-asserted.
104 * This means we need to loop through all ports. checking that they
105 * don't have an interrupt pending.
107 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
109 struct irq_info *i = dev_id;
110 struct list_head *l, *end = NULL;
111 int pass_counter = 0, handled = 0;
113 pr_debug("%s(%d): start\n", __func__, irq);
119 struct uart_8250_port *up;
120 struct uart_port *port;
122 up = list_entry(l, struct uart_8250_port, list);
125 if (port->handle_irq(port)) {
128 } else if (end == NULL)
133 if (l == i->head && pass_counter++ > PASS_LIMIT)
137 spin_unlock(&i->lock);
139 pr_debug("%s(%d): end\n", __func__, irq);
141 return IRQ_RETVAL(handled);
145 * To support ISA shared interrupts, we need to have one interrupt
146 * handler that ensures that the IRQ line has been deasserted
147 * before returning. Failing to do this will result in the IRQ
148 * line being stuck active, and, since ISA irqs are edge triggered,
149 * no more IRQs will be seen.
151 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
153 spin_lock_irq(&i->lock);
155 if (!list_empty(i->head)) {
156 if (i->head == &up->list)
157 i->head = i->head->next;
160 BUG_ON(i->head != &up->list);
163 spin_unlock_irq(&i->lock);
164 /* List empty so throw away the hash node */
165 if (i->head == NULL) {
171 static int serial_link_irq_chain(struct uart_8250_port *up)
173 struct hlist_head *h;
177 mutex_lock(&hash_mutex);
179 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
181 hlist_for_each_entry(i, h, node)
182 if (i->irq == up->port.irq)
186 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
188 mutex_unlock(&hash_mutex);
191 spin_lock_init(&i->lock);
192 i->irq = up->port.irq;
193 hlist_add_head(&i->node, h);
195 mutex_unlock(&hash_mutex);
197 spin_lock_irq(&i->lock);
200 list_add(&up->list, i->head);
201 spin_unlock_irq(&i->lock);
205 INIT_LIST_HEAD(&up->list);
207 spin_unlock_irq(&i->lock);
208 ret = request_irq(up->port.irq, serial8250_interrupt,
209 up->port.irqflags, up->port.name, i);
211 serial_do_unlink(i, up);
217 static void serial_unlink_irq_chain(struct uart_8250_port *up)
220 struct hlist_head *h;
222 mutex_lock(&hash_mutex);
224 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
226 hlist_for_each_entry(i, h, node)
227 if (i->irq == up->port.irq)
231 BUG_ON(i->head == NULL);
233 if (list_empty(i->head))
234 free_irq(up->port.irq, i);
236 serial_do_unlink(i, up);
237 mutex_unlock(&hash_mutex);
241 * This function is used to handle ports that do not have an
242 * interrupt. This doesn't work very well for 16450's, but gives
243 * barely passable results for a 16550A. (Although at the expense
244 * of much CPU overhead).
246 static void serial8250_timeout(struct timer_list *t)
248 struct uart_8250_port *up = from_timer(up, t, timer);
250 up->port.handle_irq(&up->port);
251 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
254 static void serial8250_backup_timeout(struct timer_list *t)
256 struct uart_8250_port *up = from_timer(up, t, timer);
257 unsigned int iir, ier = 0, lsr;
260 spin_lock_irqsave(&up->port.lock, flags);
263 * Must disable interrupts or else we risk racing with the interrupt
267 ier = serial_in(up, UART_IER);
268 serial_out(up, UART_IER, 0);
271 iir = serial_in(up, UART_IIR);
274 * This should be a safe test for anyone who doesn't trust the
275 * IIR bits on their UART, but it's specifically designed for
276 * the "Diva" UART used on the management processor on many HP
277 * ia64 and parisc boxes.
279 lsr = serial_in(up, UART_LSR);
280 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
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 int univ8250_setup_irq(struct uart_8250_port *up)
303 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));
326 retval = serial_link_irq_chain(up);
331 static void univ8250_release_irq(struct uart_8250_port *up)
333 struct uart_port *port = &up->port;
335 del_timer_sync(&up->timer);
336 up->timer.function = serial8250_timeout;
338 serial_unlink_irq_chain(up);
341 #ifdef CONFIG_SERIAL_8250_RSA
342 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
344 unsigned long start = UART_RSA_BASE << up->port.regshift;
345 unsigned int size = 8 << up->port.regshift;
346 struct uart_port *port = &up->port;
349 switch (port->iotype) {
352 start += port->iobase;
353 if (request_region(start, size, "serial-rsa"))
363 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
365 unsigned long offset = UART_RSA_BASE << up->port.regshift;
366 unsigned int size = 8 << up->port.regshift;
367 struct uart_port *port = &up->port;
369 switch (port->iotype) {
372 release_region(port->iobase + offset, size);
378 static const struct uart_ops *base_ops;
379 static struct uart_ops univ8250_port_ops;
381 static const struct uart_8250_ops univ8250_driver_ops = {
382 .setup_irq = univ8250_setup_irq,
383 .release_irq = univ8250_release_irq,
386 static struct uart_8250_port serial8250_ports[UART_NR];
389 * serial8250_get_port - retrieve struct uart_8250_port
390 * @line: serial line number
392 * This function retrieves struct uart_8250_port for the specific line.
393 * This struct *must* *not* be used to perform a 8250 or serial core operation
394 * which is not accessible otherwise. Its only purpose is to make the struct
395 * accessible to the runtime-pm callbacks for context suspend/restore.
396 * The lock assumption made here is none because runtime-pm suspend/resume
397 * callbacks should not be invoked if there is any operation performed on the
400 struct uart_8250_port *serial8250_get_port(int line)
402 return &serial8250_ports[line];
404 EXPORT_SYMBOL_GPL(serial8250_get_port);
406 static void (*serial8250_isa_config)(int port, struct uart_port *up,
409 void serial8250_set_isa_configurator(
410 void (*v)(int port, struct uart_port *up, u32 *capabilities))
412 serial8250_isa_config = v;
414 EXPORT_SYMBOL(serial8250_set_isa_configurator);
416 #ifdef CONFIG_SERIAL_8250_RSA
418 static void univ8250_config_port(struct uart_port *port, int flags)
420 struct uart_8250_port *up = up_to_u8250p(port);
422 up->probe &= ~UART_PROBE_RSA;
423 if (port->type == PORT_RSA) {
424 if (serial8250_request_rsa_resource(up) == 0)
425 up->probe |= UART_PROBE_RSA;
426 } else if (flags & UART_CONFIG_TYPE) {
429 for (i = 0; i < probe_rsa_count; i++) {
430 if (probe_rsa[i] == up->port.iobase) {
431 if (serial8250_request_rsa_resource(up) == 0)
432 up->probe |= UART_PROBE_RSA;
438 base_ops->config_port(port, flags);
440 if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
441 serial8250_release_rsa_resource(up);
444 static int univ8250_request_port(struct uart_port *port)
446 struct uart_8250_port *up = up_to_u8250p(port);
449 ret = base_ops->request_port(port);
450 if (ret == 0 && port->type == PORT_RSA) {
451 ret = serial8250_request_rsa_resource(up);
453 base_ops->release_port(port);
459 static void univ8250_release_port(struct uart_port *port)
461 struct uart_8250_port *up = up_to_u8250p(port);
463 if (port->type == PORT_RSA)
464 serial8250_release_rsa_resource(up);
465 base_ops->release_port(port);
468 static void univ8250_rsa_support(struct uart_ops *ops)
470 ops->config_port = univ8250_config_port;
471 ops->request_port = univ8250_request_port;
472 ops->release_port = univ8250_release_port;
476 #define univ8250_rsa_support(x) do { } while (0)
477 #endif /* CONFIG_SERIAL_8250_RSA */
479 static inline void serial8250_apply_quirks(struct uart_8250_port *up)
481 up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0;
484 static void __init serial8250_isa_init_ports(void)
486 struct uart_8250_port *up;
487 static int first = 1;
494 if (nr_uarts > UART_NR)
497 for (i = 0; i < nr_uarts; i++) {
498 struct uart_8250_port *up = &serial8250_ports[i];
499 struct uart_port *port = &up->port;
502 serial8250_init_port(up);
504 base_ops = port->ops;
505 port->ops = &univ8250_port_ops;
507 timer_setup(&up->timer, serial8250_timeout, 0);
509 up->ops = &univ8250_driver_ops;
511 if (IS_ENABLED(CONFIG_ALPHA_JENSEN) ||
512 (IS_ENABLED(CONFIG_ALPHA_GENERIC) && alpha_jensen()))
513 port->set_mctrl = alpha_jensen_set_mctrl;
515 serial8250_set_defaults(up);
518 /* chain base port ops to support Remote Supervisor Adapter */
519 univ8250_port_ops = *base_ops;
520 univ8250_rsa_support(&univ8250_port_ops);
523 irqflag = IRQF_SHARED;
525 for (i = 0, up = serial8250_ports;
526 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
528 struct uart_port *port = &up->port;
530 port->iobase = old_serial_port[i].port;
531 port->irq = irq_canonicalize(old_serial_port[i].irq);
533 port->uartclk = old_serial_port[i].baud_base * 16;
534 port->flags = old_serial_port[i].flags;
536 port->membase = old_serial_port[i].iomem_base;
537 port->iotype = old_serial_port[i].io_type;
538 port->regshift = old_serial_port[i].iomem_reg_shift;
540 port->irqflags |= irqflag;
541 if (serial8250_isa_config != NULL)
542 serial8250_isa_config(i, &up->port, &up->capabilities);
547 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
551 for (i = 0; i < nr_uarts; i++) {
552 struct uart_8250_port *up = &serial8250_ports[i];
554 if (up->port.type == PORT_8250_CIR)
562 serial8250_apply_quirks(up);
563 uart_add_one_port(drv, &up->port);
567 #ifdef CONFIG_SERIAL_8250_CONSOLE
569 static void univ8250_console_write(struct console *co, const char *s,
572 struct uart_8250_port *up = &serial8250_ports[co->index];
574 serial8250_console_write(up, s, count);
577 static int univ8250_console_setup(struct console *co, char *options)
579 struct uart_port *port;
583 * Check whether an invalid uart number has been specified, and
584 * if so, search for the first available port that does have
587 if (co->index >= nr_uarts)
589 port = &serial8250_ports[co->index].port;
590 /* link port to console */
593 retval = serial8250_console_setup(port, options, false);
599 static int univ8250_console_exit(struct console *co)
601 struct uart_port *port;
603 port = &serial8250_ports[co->index].port;
604 return serial8250_console_exit(port);
608 * univ8250_console_match - non-standard console matching
609 * @co: registering console
610 * @name: name from console command line
611 * @idx: index from console command line
612 * @options: ptr to option string from console command line
614 * Only attempts to match console command lines of the form:
615 * console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>]
616 * console=uart[8250],0x<addr>[,<options>]
617 * This form is used to register an initial earlycon boot console and
618 * replace it with the serial8250_console at 8250 driver init.
620 * Performs console setup for a match (as required by interface)
621 * If no <options> are specified, then assume the h/w is already setup.
623 * Returns 0 if console matches; otherwise non-zero to use default matching
625 static int univ8250_console_match(struct console *co, char *name, int idx,
628 char match[] = "uart"; /* 8250-specific earlycon name */
629 unsigned char iotype;
630 resource_size_t addr;
633 if (strncmp(name, match, 4) != 0)
636 if (uart_parse_earlycon(options, &iotype, &addr, &options))
639 /* try to match the port specified on the command line */
640 for (i = 0; i < nr_uarts; i++) {
641 struct uart_port *port = &serial8250_ports[i].port;
643 if (port->iotype != iotype)
645 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
646 iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
647 && (port->mapbase != addr))
649 if (iotype == UPIO_PORT && port->iobase != addr)
654 return serial8250_console_setup(port, options, true);
660 static struct console univ8250_console = {
662 .write = univ8250_console_write,
663 .device = uart_console_device,
664 .setup = univ8250_console_setup,
665 .exit = univ8250_console_exit,
666 .match = univ8250_console_match,
667 .flags = CON_PRINTBUFFER | CON_ANYTIME,
669 .data = &serial8250_reg,
672 static int __init univ8250_console_init(void)
677 serial8250_isa_init_ports();
678 register_console(&univ8250_console);
681 console_initcall(univ8250_console_init);
683 #define SERIAL8250_CONSOLE (&univ8250_console)
685 #define SERIAL8250_CONSOLE NULL
688 static struct uart_driver serial8250_reg = {
689 .owner = THIS_MODULE,
690 .driver_name = "serial",
694 .cons = SERIAL8250_CONSOLE,
698 * early_serial_setup - early registration for 8250 ports
700 * Setup an 8250 port structure prior to console initialisation. Use
701 * after console initialisation will cause undefined behaviour.
703 int __init early_serial_setup(struct uart_port *port)
707 if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
710 serial8250_isa_init_ports();
711 p = &serial8250_ports[port->line].port;
712 p->iobase = port->iobase;
713 p->membase = port->membase;
715 p->irqflags = port->irqflags;
716 p->uartclk = port->uartclk;
717 p->fifosize = port->fifosize;
718 p->regshift = port->regshift;
719 p->iotype = port->iotype;
720 p->flags = port->flags;
721 p->mapbase = port->mapbase;
722 p->mapsize = port->mapsize;
723 p->private_data = port->private_data;
724 p->type = port->type;
725 p->line = port->line;
727 serial8250_set_defaults(up_to_u8250p(p));
730 p->serial_in = port->serial_in;
731 if (port->serial_out)
732 p->serial_out = port->serial_out;
733 if (port->handle_irq)
734 p->handle_irq = port->handle_irq;
740 * serial8250_suspend_port - suspend one serial port
741 * @line: serial line number
743 * Suspend one serial port.
745 void serial8250_suspend_port(int line)
747 struct uart_8250_port *up = &serial8250_ports[line];
748 struct uart_port *port = &up->port;
750 if (!console_suspend_enabled && uart_console(port) &&
751 port->type != PORT_8250) {
752 unsigned char canary = 0xa5;
754 serial_out(up, UART_SCR, canary);
755 if (serial_in(up, UART_SCR) == canary)
759 uart_suspend_port(&serial8250_reg, port);
761 EXPORT_SYMBOL(serial8250_suspend_port);
764 * serial8250_resume_port - resume one serial port
765 * @line: serial line number
767 * Resume one serial port.
769 void serial8250_resume_port(int line)
771 struct uart_8250_port *up = &serial8250_ports[line];
772 struct uart_port *port = &up->port;
776 if (up->capabilities & UART_NATSEMI) {
777 /* Ensure it's still in high speed mode */
778 serial_port_out(port, UART_LCR, 0xE0);
780 ns16550a_goto_highspeed(up);
782 serial_port_out(port, UART_LCR, 0);
783 port->uartclk = 921600*16;
785 uart_resume_port(&serial8250_reg, port);
787 EXPORT_SYMBOL(serial8250_resume_port);
790 * Register a set of serial devices attached to a platform device. The
791 * list is terminated with a zero flags entry, which means we expect
792 * all entries to have at least UPF_BOOT_AUTOCONF set.
794 static int serial8250_probe(struct platform_device *dev)
796 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
797 struct uart_8250_port uart;
798 int ret, i, irqflag = 0;
800 memset(&uart, 0, sizeof(uart));
803 irqflag = IRQF_SHARED;
805 for (i = 0; p && p->flags != 0; p++, i++) {
806 uart.port.iobase = p->iobase;
807 uart.port.membase = p->membase;
808 uart.port.irq = p->irq;
809 uart.port.irqflags = p->irqflags;
810 uart.port.uartclk = p->uartclk;
811 uart.port.regshift = p->regshift;
812 uart.port.iotype = p->iotype;
813 uart.port.flags = p->flags;
814 uart.port.mapbase = p->mapbase;
815 uart.port.hub6 = p->hub6;
816 uart.port.has_sysrq = p->has_sysrq;
817 uart.port.private_data = p->private_data;
818 uart.port.type = p->type;
819 uart.port.serial_in = p->serial_in;
820 uart.port.serial_out = p->serial_out;
821 uart.port.handle_irq = p->handle_irq;
822 uart.port.handle_break = p->handle_break;
823 uart.port.set_termios = p->set_termios;
824 uart.port.set_ldisc = p->set_ldisc;
825 uart.port.get_mctrl = p->get_mctrl;
826 uart.port.pm = p->pm;
827 uart.port.dev = &dev->dev;
828 uart.port.irqflags |= irqflag;
829 ret = serial8250_register_8250_port(&uart);
831 dev_err(&dev->dev, "unable to register port at index %d "
832 "(IO%lx MEM%llx IRQ%d): %d\n", i,
833 p->iobase, (unsigned long long)p->mapbase,
841 * Remove serial ports registered against a platform device.
843 static int serial8250_remove(struct platform_device *dev)
847 for (i = 0; i < nr_uarts; i++) {
848 struct uart_8250_port *up = &serial8250_ports[i];
850 if (up->port.dev == &dev->dev)
851 serial8250_unregister_port(i);
856 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
860 for (i = 0; i < UART_NR; i++) {
861 struct uart_8250_port *up = &serial8250_ports[i];
863 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
864 uart_suspend_port(&serial8250_reg, &up->port);
870 static int serial8250_resume(struct platform_device *dev)
874 for (i = 0; i < UART_NR; i++) {
875 struct uart_8250_port *up = &serial8250_ports[i];
877 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
878 serial8250_resume_port(i);
884 static struct platform_driver serial8250_isa_driver = {
885 .probe = serial8250_probe,
886 .remove = serial8250_remove,
887 .suspend = serial8250_suspend,
888 .resume = serial8250_resume,
890 .name = "serial8250",
895 * This "device" covers _all_ ISA 8250-compatible serial devices listed
896 * in the table in include/asm/serial.h
898 static struct platform_device *serial8250_isa_devs;
901 * serial8250_register_8250_port and serial8250_unregister_port allows for
902 * 16x50 serial ports to be configured at run-time, to support PCMCIA
903 * modems and PCI multiport cards.
905 static DEFINE_MUTEX(serial_mutex);
907 static struct uart_8250_port *serial8250_find_match_or_unused(const struct uart_port *port)
912 * First, find a port entry which matches.
914 for (i = 0; i < nr_uarts; i++)
915 if (uart_match_port(&serial8250_ports[i].port, port))
916 return &serial8250_ports[i];
918 /* try line number first if still available */
920 if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
921 serial8250_ports[i].port.iobase == 0)
922 return &serial8250_ports[i];
924 * We didn't find a matching entry, so look for the first
925 * free entry. We look for one which hasn't been previously
926 * used (indicated by zero iobase).
928 for (i = 0; i < nr_uarts; i++)
929 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
930 serial8250_ports[i].port.iobase == 0)
931 return &serial8250_ports[i];
934 * That also failed. Last resort is to find any entry which
935 * doesn't have a real port associated with it.
937 for (i = 0; i < nr_uarts; i++)
938 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
939 return &serial8250_ports[i];
944 static void serial_8250_overrun_backoff_work(struct work_struct *work)
946 struct uart_8250_port *up =
947 container_of(to_delayed_work(work), struct uart_8250_port,
949 struct uart_port *port = &up->port;
952 spin_lock_irqsave(&port->lock, flags);
953 up->ier |= UART_IER_RLSI | UART_IER_RDI;
954 up->port.read_status_mask |= UART_LSR_DR;
955 serial_out(up, UART_IER, up->ier);
956 spin_unlock_irqrestore(&port->lock, flags);
960 * serial8250_register_8250_port - register a serial port
961 * @up: serial port template
963 * Configure the serial port specified by the request. If the
964 * port exists and is in use, it is hung up and unregistered
967 * The port is then probed and if necessary the IRQ is autodetected
968 * If this fails an error is returned.
970 * On success the port is ready to use and the line number is returned.
972 int serial8250_register_8250_port(const struct uart_8250_port *up)
974 struct uart_8250_port *uart;
977 if (up->port.uartclk == 0)
980 mutex_lock(&serial_mutex);
982 uart = serial8250_find_match_or_unused(&up->port);
983 if (uart && uart->port.type != PORT_8250_CIR) {
984 struct mctrl_gpios *gpios;
987 uart_remove_one_port(&serial8250_reg, &uart->port);
989 uart->port.iobase = up->port.iobase;
990 uart->port.membase = up->port.membase;
991 uart->port.irq = up->port.irq;
992 uart->port.irqflags = up->port.irqflags;
993 uart->port.uartclk = up->port.uartclk;
994 uart->port.fifosize = up->port.fifosize;
995 uart->port.regshift = up->port.regshift;
996 uart->port.iotype = up->port.iotype;
997 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
998 uart->bugs = up->bugs;
999 uart->port.mapbase = up->port.mapbase;
1000 uart->port.mapsize = up->port.mapsize;
1001 uart->port.private_data = up->port.private_data;
1002 uart->tx_loadsz = up->tx_loadsz;
1003 uart->capabilities = up->capabilities;
1004 uart->port.throttle = up->port.throttle;
1005 uart->port.unthrottle = up->port.unthrottle;
1006 uart->port.rs485_config = up->port.rs485_config;
1007 uart->port.rs485 = up->port.rs485;
1008 uart->rs485_start_tx = up->rs485_start_tx;
1009 uart->rs485_stop_tx = up->rs485_stop_tx;
1010 uart->dma = up->dma;
1012 /* Take tx_loadsz from fifosize if it wasn't set separately */
1013 if (uart->port.fifosize && !uart->tx_loadsz)
1014 uart->tx_loadsz = uart->port.fifosize;
1017 uart->port.dev = up->port.dev;
1018 ret = uart_get_rs485_mode(&uart->port);
1023 if (up->port.flags & UPF_FIXED_TYPE)
1024 uart->port.type = up->port.type;
1027 * Only call mctrl_gpio_init(), if the device has no ACPI
1030 if (!has_acpi_companion(uart->port.dev)) {
1031 gpios = mctrl_gpio_init(&uart->port, 0);
1032 if (IS_ERR(gpios)) {
1033 ret = PTR_ERR(gpios);
1036 uart->gpios = gpios;
1040 serial8250_set_defaults(uart);
1042 /* Possibly override default I/O functions. */
1043 if (up->port.serial_in)
1044 uart->port.serial_in = up->port.serial_in;
1045 if (up->port.serial_out)
1046 uart->port.serial_out = up->port.serial_out;
1047 if (up->port.handle_irq)
1048 uart->port.handle_irq = up->port.handle_irq;
1049 /* Possibly override set_termios call */
1050 if (up->port.set_termios)
1051 uart->port.set_termios = up->port.set_termios;
1052 if (up->port.set_ldisc)
1053 uart->port.set_ldisc = up->port.set_ldisc;
1054 if (up->port.get_mctrl)
1055 uart->port.get_mctrl = up->port.get_mctrl;
1056 if (up->port.set_mctrl)
1057 uart->port.set_mctrl = up->port.set_mctrl;
1058 if (up->port.get_divisor)
1059 uart->port.get_divisor = up->port.get_divisor;
1060 if (up->port.set_divisor)
1061 uart->port.set_divisor = up->port.set_divisor;
1062 if (up->port.startup)
1063 uart->port.startup = up->port.startup;
1064 if (up->port.shutdown)
1065 uart->port.shutdown = up->port.shutdown;
1067 uart->port.pm = up->port.pm;
1068 if (up->port.handle_break)
1069 uart->port.handle_break = up->port.handle_break;
1071 uart->dl_read = up->dl_read;
1073 uart->dl_write = up->dl_write;
1075 if (uart->port.type != PORT_8250_CIR) {
1076 if (serial8250_isa_config != NULL)
1077 serial8250_isa_config(0, &uart->port,
1078 &uart->capabilities);
1080 serial8250_apply_quirks(uart);
1081 ret = uart_add_one_port(&serial8250_reg,
1086 ret = uart->port.line;
1088 dev_info(uart->port.dev,
1089 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
1091 (unsigned long long)uart->port.mapbase,
1097 /* Initialise interrupt backoff work if required */
1098 if (up->overrun_backoff_time_ms > 0) {
1099 uart->overrun_backoff_time_ms =
1100 up->overrun_backoff_time_ms;
1101 INIT_DELAYED_WORK(&uart->overrun_backoff,
1102 serial_8250_overrun_backoff_work);
1104 uart->overrun_backoff_time_ms = 0;
1108 mutex_unlock(&serial_mutex);
1113 uart->port.dev = NULL;
1114 mutex_unlock(&serial_mutex);
1117 EXPORT_SYMBOL(serial8250_register_8250_port);
1120 * serial8250_unregister_port - remove a 16x50 serial port at runtime
1121 * @line: serial line number
1123 * Remove one serial port. This may not be called from interrupt
1124 * context. We hand the port back to the our control.
1126 void serial8250_unregister_port(int line)
1128 struct uart_8250_port *uart = &serial8250_ports[line];
1130 mutex_lock(&serial_mutex);
1133 unsigned long flags;
1135 spin_lock_irqsave(&uart->port.lock, flags);
1136 serial8250_em485_destroy(uart);
1137 spin_unlock_irqrestore(&uart->port.lock, flags);
1140 uart_remove_one_port(&serial8250_reg, &uart->port);
1141 if (serial8250_isa_devs) {
1142 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
1143 uart->port.type = PORT_UNKNOWN;
1144 uart->port.dev = &serial8250_isa_devs->dev;
1145 uart->capabilities = 0;
1146 serial8250_apply_quirks(uart);
1147 uart_add_one_port(&serial8250_reg, &uart->port);
1149 uart->port.dev = NULL;
1151 mutex_unlock(&serial_mutex);
1153 EXPORT_SYMBOL(serial8250_unregister_port);
1155 static int __init serial8250_init(void)
1162 serial8250_isa_init_ports();
1164 pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %sabled\n",
1165 nr_uarts, share_irqs ? "en" : "dis");
1168 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
1170 serial8250_reg.nr = UART_NR;
1171 ret = uart_register_driver(&serial8250_reg);
1176 ret = serial8250_pnp_init();
1178 goto unreg_uart_drv;
1180 serial8250_isa_devs = platform_device_alloc("serial8250",
1181 PLAT8250_DEV_LEGACY);
1182 if (!serial8250_isa_devs) {
1187 ret = platform_device_add(serial8250_isa_devs);
1191 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
1193 ret = platform_driver_register(&serial8250_isa_driver);
1197 platform_device_del(serial8250_isa_devs);
1199 platform_device_put(serial8250_isa_devs);
1201 serial8250_pnp_exit();
1204 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1206 uart_unregister_driver(&serial8250_reg);
1212 static void __exit serial8250_exit(void)
1214 struct platform_device *isa_dev = serial8250_isa_devs;
1217 * This tells serial8250_unregister_port() not to re-register
1218 * the ports (thereby making serial8250_isa_driver permanently
1221 serial8250_isa_devs = NULL;
1223 platform_driver_unregister(&serial8250_isa_driver);
1224 platform_device_unregister(isa_dev);
1226 serial8250_pnp_exit();
1229 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1231 uart_unregister_driver(&serial8250_reg);
1235 module_init(serial8250_init);
1236 module_exit(serial8250_exit);
1238 MODULE_LICENSE("GPL");
1239 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
1241 module_param_hw(share_irqs, uint, other, 0644);
1242 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
1244 module_param(nr_uarts, uint, 0644);
1245 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
1247 module_param(skip_txen_test, uint, 0644);
1248 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
1250 #ifdef CONFIG_SERIAL_8250_RSA
1251 module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
1252 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
1254 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
1256 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
1258 /* This module was renamed to 8250_core in 3.7. Keep the old "8250" name
1259 * working as well for the module options so we don't break people. We
1260 * need to keep the names identical and the convenient macros will happily
1261 * refuse to let us do that by failing the build with redefinition errors
1262 * of global variables. So we stick them inside a dummy function to avoid
1263 * those conflicts. The options still get parsed, and the redefined
1264 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
1266 * This is hacky. I'm sorry.
1268 static void __used s8250_options(void)
1270 #undef MODULE_PARAM_PREFIX
1271 #define MODULE_PARAM_PREFIX "8250_core."
1273 module_param_cb(share_irqs, ¶m_ops_uint, &share_irqs, 0644);
1274 module_param_cb(nr_uarts, ¶m_ops_uint, &nr_uarts, 0644);
1275 module_param_cb(skip_txen_test, ¶m_ops_uint, &skip_txen_test, 0644);
1276 #ifdef CONFIG_SERIAL_8250_RSA
1277 __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
1278 ¶m_array_ops, .arr = &__param_arr_probe_rsa,
1283 MODULE_ALIAS("8250_core");