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/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/ioport.h>
20 #include <linux/init.h>
21 #include <linux/console.h>
22 #include <linux/sysrq.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
25 #include <linux/tty.h>
26 #include <linux/ratelimit.h>
27 #include <linux/tty_flip.h>
28 #include <linux/serial.h>
29 #include <linux/serial_8250.h>
30 #include <linux/nmi.h>
31 #include <linux/mutex.h>
32 #include <linux/slab.h>
33 #include <linux/uaccess.h>
34 #include <linux/pm_runtime.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) {
134 /* If we hit this, we're dead. */
135 printk_ratelimited(KERN_ERR
136 "serial8250: too much work for irq%d\n", irq);
141 spin_unlock(&i->lock);
143 pr_debug("%s(%d): end\n", __func__, irq);
145 return IRQ_RETVAL(handled);
149 * To support ISA shared interrupts, we need to have one interrupt
150 * handler that ensures that the IRQ line has been deasserted
151 * before returning. Failing to do this will result in the IRQ
152 * line being stuck active, and, since ISA irqs are edge triggered,
153 * no more IRQs will be seen.
155 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
157 spin_lock_irq(&i->lock);
159 if (!list_empty(i->head)) {
160 if (i->head == &up->list)
161 i->head = i->head->next;
164 BUG_ON(i->head != &up->list);
167 spin_unlock_irq(&i->lock);
168 /* List empty so throw away the hash node */
169 if (i->head == NULL) {
175 static int serial_link_irq_chain(struct uart_8250_port *up)
177 struct hlist_head *h;
178 struct hlist_node *n;
182 mutex_lock(&hash_mutex);
184 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
186 hlist_for_each(n, h) {
187 i = hlist_entry(n, struct irq_info, node);
188 if (i->irq == up->port.irq)
193 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
195 mutex_unlock(&hash_mutex);
198 spin_lock_init(&i->lock);
199 i->irq = up->port.irq;
200 hlist_add_head(&i->node, h);
202 mutex_unlock(&hash_mutex);
204 spin_lock_irq(&i->lock);
207 list_add(&up->list, i->head);
208 spin_unlock_irq(&i->lock);
212 INIT_LIST_HEAD(&up->list);
214 spin_unlock_irq(&i->lock);
215 ret = request_irq(up->port.irq, serial8250_interrupt,
216 up->port.irqflags, up->port.name, i);
218 serial_do_unlink(i, up);
224 static void serial_unlink_irq_chain(struct uart_8250_port *up)
227 * yes, some broken gcc emit "warning: 'i' may be used uninitialized"
228 * but no, we are not going to take a patch that assigns NULL below.
231 struct hlist_node *n;
232 struct hlist_head *h;
234 mutex_lock(&hash_mutex);
236 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
238 hlist_for_each(n, h) {
239 i = hlist_entry(n, struct irq_info, node);
240 if (i->irq == up->port.irq)
245 BUG_ON(i->head == NULL);
247 if (list_empty(i->head))
248 free_irq(up->port.irq, i);
250 serial_do_unlink(i, up);
251 mutex_unlock(&hash_mutex);
255 * This function is used to handle ports that do not have an
256 * interrupt. This doesn't work very well for 16450's, but gives
257 * barely passable results for a 16550A. (Although at the expense
258 * of much CPU overhead).
260 static void serial8250_timeout(struct timer_list *t)
262 struct uart_8250_port *up = from_timer(up, t, timer);
264 up->port.handle_irq(&up->port);
265 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
268 static void serial8250_backup_timeout(struct timer_list *t)
270 struct uart_8250_port *up = from_timer(up, t, timer);
271 unsigned int iir, ier = 0, lsr;
274 spin_lock_irqsave(&up->port.lock, flags);
277 * Must disable interrupts or else we risk racing with the interrupt
281 ier = serial_in(up, UART_IER);
282 serial_out(up, UART_IER, 0);
285 iir = serial_in(up, UART_IIR);
288 * This should be a safe test for anyone who doesn't trust the
289 * IIR bits on their UART, but it's specifically designed for
290 * the "Diva" UART used on the management processor on many HP
291 * ia64 and parisc boxes.
293 lsr = serial_in(up, UART_LSR);
294 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
295 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
296 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
297 (lsr & UART_LSR_THRE)) {
298 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
299 iir |= UART_IIR_THRI;
302 if (!(iir & UART_IIR_NO_INT))
303 serial8250_tx_chars(up);
306 serial_out(up, UART_IER, ier);
308 spin_unlock_irqrestore(&up->port.lock, flags);
310 /* Standard timer interval plus 0.2s to keep the port running */
311 mod_timer(&up->timer,
312 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
315 static int univ8250_setup_irq(struct uart_8250_port *up)
317 struct uart_port *port = &up->port;
321 * The above check will only give an accurate result the first time
322 * the port is opened so this value needs to be preserved.
324 if (up->bugs & UART_BUG_THRE) {
325 pr_debug("%s - using backup timer\n", port->name);
327 up->timer.function = serial8250_backup_timeout;
328 mod_timer(&up->timer, jiffies +
329 uart_poll_timeout(port) + HZ / 5);
333 * If the "interrupt" for this port doesn't correspond with any
334 * hardware interrupt, we use a timer-based system. The original
335 * driver used to do this with IRQ0.
338 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
340 retval = serial_link_irq_chain(up);
345 static void univ8250_release_irq(struct uart_8250_port *up)
347 struct uart_port *port = &up->port;
349 del_timer_sync(&up->timer);
350 up->timer.function = serial8250_timeout;
352 serial_unlink_irq_chain(up);
355 #ifdef CONFIG_SERIAL_8250_RSA
356 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
358 unsigned long start = UART_RSA_BASE << up->port.regshift;
359 unsigned int size = 8 << up->port.regshift;
360 struct uart_port *port = &up->port;
363 switch (port->iotype) {
366 start += port->iobase;
367 if (request_region(start, size, "serial-rsa"))
377 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
379 unsigned long offset = UART_RSA_BASE << up->port.regshift;
380 unsigned int size = 8 << up->port.regshift;
381 struct uart_port *port = &up->port;
383 switch (port->iotype) {
386 release_region(port->iobase + offset, size);
392 static const struct uart_ops *base_ops;
393 static struct uart_ops univ8250_port_ops;
395 static const struct uart_8250_ops univ8250_driver_ops = {
396 .setup_irq = univ8250_setup_irq,
397 .release_irq = univ8250_release_irq,
400 static struct uart_8250_port serial8250_ports[UART_NR];
403 * serial8250_get_port - retrieve struct uart_8250_port
404 * @line: serial line number
406 * This function retrieves struct uart_8250_port for the specific line.
407 * This struct *must* *not* be used to perform a 8250 or serial core operation
408 * which is not accessible otherwise. Its only purpose is to make the struct
409 * accessible to the runtime-pm callbacks for context suspend/restore.
410 * The lock assumption made here is none because runtime-pm suspend/resume
411 * callbacks should not be invoked if there is any operation performed on the
414 struct uart_8250_port *serial8250_get_port(int line)
416 return &serial8250_ports[line];
418 EXPORT_SYMBOL_GPL(serial8250_get_port);
420 static void (*serial8250_isa_config)(int port, struct uart_port *up,
423 void serial8250_set_isa_configurator(
424 void (*v)(int port, struct uart_port *up, u32 *capabilities))
426 serial8250_isa_config = v;
428 EXPORT_SYMBOL(serial8250_set_isa_configurator);
430 #ifdef CONFIG_SERIAL_8250_RSA
432 static void univ8250_config_port(struct uart_port *port, int flags)
434 struct uart_8250_port *up = up_to_u8250p(port);
436 up->probe &= ~UART_PROBE_RSA;
437 if (port->type == PORT_RSA) {
438 if (serial8250_request_rsa_resource(up) == 0)
439 up->probe |= UART_PROBE_RSA;
440 } else if (flags & UART_CONFIG_TYPE) {
443 for (i = 0; i < probe_rsa_count; i++) {
444 if (probe_rsa[i] == up->port.iobase) {
445 if (serial8250_request_rsa_resource(up) == 0)
446 up->probe |= UART_PROBE_RSA;
452 base_ops->config_port(port, flags);
454 if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
455 serial8250_release_rsa_resource(up);
458 static int univ8250_request_port(struct uart_port *port)
460 struct uart_8250_port *up = up_to_u8250p(port);
463 ret = base_ops->request_port(port);
464 if (ret == 0 && port->type == PORT_RSA) {
465 ret = serial8250_request_rsa_resource(up);
467 base_ops->release_port(port);
473 static void univ8250_release_port(struct uart_port *port)
475 struct uart_8250_port *up = up_to_u8250p(port);
477 if (port->type == PORT_RSA)
478 serial8250_release_rsa_resource(up);
479 base_ops->release_port(port);
482 static void univ8250_rsa_support(struct uart_ops *ops)
484 ops->config_port = univ8250_config_port;
485 ops->request_port = univ8250_request_port;
486 ops->release_port = univ8250_release_port;
490 #define univ8250_rsa_support(x) do { } while (0)
491 #endif /* CONFIG_SERIAL_8250_RSA */
493 static inline void serial8250_apply_quirks(struct uart_8250_port *up)
495 up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0;
498 static void __init serial8250_isa_init_ports(void)
500 struct uart_8250_port *up;
501 static int first = 1;
508 if (nr_uarts > UART_NR)
511 for (i = 0; i < nr_uarts; i++) {
512 struct uart_8250_port *up = &serial8250_ports[i];
513 struct uart_port *port = &up->port;
516 serial8250_init_port(up);
518 base_ops = port->ops;
519 port->ops = &univ8250_port_ops;
521 timer_setup(&up->timer, serial8250_timeout, 0);
523 up->ops = &univ8250_driver_ops;
526 * ALPHA_KLUDGE_MCR needs to be killed.
528 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
529 up->mcr_force = ALPHA_KLUDGE_MCR;
530 serial8250_set_defaults(up);
533 /* chain base port ops to support Remote Supervisor Adapter */
534 univ8250_port_ops = *base_ops;
535 univ8250_rsa_support(&univ8250_port_ops);
538 irqflag = IRQF_SHARED;
540 for (i = 0, up = serial8250_ports;
541 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
543 struct uart_port *port = &up->port;
545 port->iobase = old_serial_port[i].port;
546 port->irq = irq_canonicalize(old_serial_port[i].irq);
548 port->uartclk = old_serial_port[i].baud_base * 16;
549 port->flags = old_serial_port[i].flags;
551 port->membase = old_serial_port[i].iomem_base;
552 port->iotype = old_serial_port[i].io_type;
553 port->regshift = old_serial_port[i].iomem_reg_shift;
555 port->irqflags |= irqflag;
556 if (serial8250_isa_config != NULL)
557 serial8250_isa_config(i, &up->port, &up->capabilities);
562 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
566 for (i = 0; i < nr_uarts; i++) {
567 struct uart_8250_port *up = &serial8250_ports[i];
569 if (up->port.type == PORT_8250_CIR)
577 serial8250_apply_quirks(up);
578 uart_add_one_port(drv, &up->port);
582 #ifdef CONFIG_SERIAL_8250_CONSOLE
584 static void univ8250_console_write(struct console *co, const char *s,
587 struct uart_8250_port *up = &serial8250_ports[co->index];
589 serial8250_console_write(up, s, count);
592 static int univ8250_console_setup(struct console *co, char *options)
594 struct uart_port *port;
598 * Check whether an invalid uart number has been specified, and
599 * if so, search for the first available port that does have
602 if (co->index >= nr_uarts)
604 port = &serial8250_ports[co->index].port;
605 /* link port to console */
608 retval = serial8250_console_setup(port, options, false);
615 * univ8250_console_match - non-standard console matching
616 * @co: registering console
617 * @name: name from console command line
618 * @idx: index from console command line
619 * @options: ptr to option string from console command line
621 * Only attempts to match console command lines of the form:
622 * console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>]
623 * console=uart[8250],0x<addr>[,<options>]
624 * This form is used to register an initial earlycon boot console and
625 * replace it with the serial8250_console at 8250 driver init.
627 * Performs console setup for a match (as required by interface)
628 * If no <options> are specified, then assume the h/w is already setup.
630 * Returns 0 if console matches; otherwise non-zero to use default matching
632 static int univ8250_console_match(struct console *co, char *name, int idx,
635 char match[] = "uart"; /* 8250-specific earlycon name */
636 unsigned char iotype;
637 resource_size_t addr;
640 if (strncmp(name, match, 4) != 0)
643 if (uart_parse_earlycon(options, &iotype, &addr, &options))
646 /* try to match the port specified on the command line */
647 for (i = 0; i < nr_uarts; i++) {
648 struct uart_port *port = &serial8250_ports[i].port;
650 if (port->iotype != iotype)
652 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
653 iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
654 && (port->mapbase != addr))
656 if (iotype == UPIO_PORT && port->iobase != addr)
661 return serial8250_console_setup(port, options, true);
667 static struct console univ8250_console = {
669 .write = univ8250_console_write,
670 .device = uart_console_device,
671 .setup = univ8250_console_setup,
672 .match = univ8250_console_match,
673 .flags = CON_PRINTBUFFER | CON_ANYTIME,
675 .data = &serial8250_reg,
678 static int __init univ8250_console_init(void)
683 serial8250_isa_init_ports();
684 register_console(&univ8250_console);
687 console_initcall(univ8250_console_init);
689 #define SERIAL8250_CONSOLE (&univ8250_console)
691 #define SERIAL8250_CONSOLE NULL
694 static struct uart_driver serial8250_reg = {
695 .owner = THIS_MODULE,
696 .driver_name = "serial",
700 .cons = SERIAL8250_CONSOLE,
704 * early_serial_setup - early registration for 8250 ports
706 * Setup an 8250 port structure prior to console initialisation. Use
707 * after console initialisation will cause undefined behaviour.
709 int __init early_serial_setup(struct uart_port *port)
713 if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
716 serial8250_isa_init_ports();
717 p = &serial8250_ports[port->line].port;
718 p->iobase = port->iobase;
719 p->membase = port->membase;
721 p->irqflags = port->irqflags;
722 p->uartclk = port->uartclk;
723 p->fifosize = port->fifosize;
724 p->regshift = port->regshift;
725 p->iotype = port->iotype;
726 p->flags = port->flags;
727 p->mapbase = port->mapbase;
728 p->mapsize = port->mapsize;
729 p->private_data = port->private_data;
730 p->type = port->type;
731 p->line = port->line;
733 serial8250_set_defaults(up_to_u8250p(p));
736 p->serial_in = port->serial_in;
737 if (port->serial_out)
738 p->serial_out = port->serial_out;
739 if (port->handle_irq)
740 p->handle_irq = port->handle_irq;
746 * serial8250_suspend_port - suspend one serial port
747 * @line: serial line number
749 * Suspend one serial port.
751 void serial8250_suspend_port(int line)
753 struct uart_8250_port *up = &serial8250_ports[line];
754 struct uart_port *port = &up->port;
756 if (!console_suspend_enabled && uart_console(port) &&
757 port->type != PORT_8250) {
758 unsigned char canary = 0xa5;
759 serial_out(up, UART_SCR, canary);
760 if (serial_in(up, UART_SCR) == canary)
764 uart_suspend_port(&serial8250_reg, port);
766 EXPORT_SYMBOL(serial8250_suspend_port);
769 * serial8250_resume_port - resume one serial port
770 * @line: serial line number
772 * Resume one serial port.
774 void serial8250_resume_port(int line)
776 struct uart_8250_port *up = &serial8250_ports[line];
777 struct uart_port *port = &up->port;
781 if (up->capabilities & UART_NATSEMI) {
782 /* Ensure it's still in high speed mode */
783 serial_port_out(port, UART_LCR, 0xE0);
785 ns16550a_goto_highspeed(up);
787 serial_port_out(port, UART_LCR, 0);
788 port->uartclk = 921600*16;
790 uart_resume_port(&serial8250_reg, port);
792 EXPORT_SYMBOL(serial8250_resume_port);
795 * Register a set of serial devices attached to a platform device. The
796 * list is terminated with a zero flags entry, which means we expect
797 * all entries to have at least UPF_BOOT_AUTOCONF set.
799 static int serial8250_probe(struct platform_device *dev)
801 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
802 struct uart_8250_port uart;
803 int ret, i, irqflag = 0;
805 memset(&uart, 0, sizeof(uart));
808 irqflag = IRQF_SHARED;
810 for (i = 0; p && p->flags != 0; p++, i++) {
811 uart.port.iobase = p->iobase;
812 uart.port.membase = p->membase;
813 uart.port.irq = p->irq;
814 uart.port.irqflags = p->irqflags;
815 uart.port.uartclk = p->uartclk;
816 uart.port.regshift = p->regshift;
817 uart.port.iotype = p->iotype;
818 uart.port.flags = p->flags;
819 uart.port.mapbase = p->mapbase;
820 uart.port.hub6 = p->hub6;
821 uart.port.private_data = p->private_data;
822 uart.port.type = p->type;
823 uart.port.serial_in = p->serial_in;
824 uart.port.serial_out = p->serial_out;
825 uart.port.handle_irq = p->handle_irq;
826 uart.port.handle_break = p->handle_break;
827 uart.port.set_termios = p->set_termios;
828 uart.port.set_ldisc = p->set_ldisc;
829 uart.port.get_mctrl = p->get_mctrl;
830 uart.port.pm = p->pm;
831 uart.port.dev = &dev->dev;
832 uart.port.irqflags |= irqflag;
833 ret = serial8250_register_8250_port(&uart);
835 dev_err(&dev->dev, "unable to register port at index %d "
836 "(IO%lx MEM%llx IRQ%d): %d\n", i,
837 p->iobase, (unsigned long long)p->mapbase,
845 * Remove serial ports registered against a platform device.
847 static int serial8250_remove(struct platform_device *dev)
851 for (i = 0; i < nr_uarts; i++) {
852 struct uart_8250_port *up = &serial8250_ports[i];
854 if (up->port.dev == &dev->dev)
855 serial8250_unregister_port(i);
860 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
864 for (i = 0; i < UART_NR; i++) {
865 struct uart_8250_port *up = &serial8250_ports[i];
867 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
868 uart_suspend_port(&serial8250_reg, &up->port);
874 static int serial8250_resume(struct platform_device *dev)
878 for (i = 0; i < UART_NR; i++) {
879 struct uart_8250_port *up = &serial8250_ports[i];
881 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
882 serial8250_resume_port(i);
888 static struct platform_driver serial8250_isa_driver = {
889 .probe = serial8250_probe,
890 .remove = serial8250_remove,
891 .suspend = serial8250_suspend,
892 .resume = serial8250_resume,
894 .name = "serial8250",
899 * This "device" covers _all_ ISA 8250-compatible serial devices listed
900 * in the table in include/asm/serial.h
902 static struct platform_device *serial8250_isa_devs;
905 * serial8250_register_8250_port and serial8250_unregister_port allows for
906 * 16x50 serial ports to be configured at run-time, to support PCMCIA
907 * modems and PCI multiport cards.
909 static DEFINE_MUTEX(serial_mutex);
911 static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
916 * First, find a port entry which matches.
918 for (i = 0; i < nr_uarts; i++)
919 if (uart_match_port(&serial8250_ports[i].port, port))
920 return &serial8250_ports[i];
922 /* try line number first if still available */
924 if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
925 serial8250_ports[i].port.iobase == 0)
926 return &serial8250_ports[i];
928 * We didn't find a matching entry, so look for the first
929 * free entry. We look for one which hasn't been previously
930 * used (indicated by zero iobase).
932 for (i = 0; i < nr_uarts; i++)
933 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
934 serial8250_ports[i].port.iobase == 0)
935 return &serial8250_ports[i];
938 * That also failed. Last resort is to find any entry which
939 * doesn't have a real port associated with it.
941 for (i = 0; i < nr_uarts; i++)
942 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
943 return &serial8250_ports[i];
948 static void serial_8250_overrun_backoff_work(struct work_struct *work)
950 struct uart_8250_port *up =
951 container_of(to_delayed_work(work), struct uart_8250_port,
953 struct uart_port *port = &up->port;
956 spin_lock_irqsave(&port->lock, flags);
957 up->ier |= UART_IER_RLSI | UART_IER_RDI;
958 up->port.read_status_mask |= UART_LSR_DR;
959 serial_out(up, UART_IER, up->ier);
960 spin_unlock_irqrestore(&port->lock, flags);
964 * serial8250_register_8250_port - register a serial port
965 * @up: serial port template
967 * Configure the serial port specified by the request. If the
968 * port exists and is in use, it is hung up and unregistered
971 * The port is then probed and if necessary the IRQ is autodetected
972 * If this fails an error is returned.
974 * On success the port is ready to use and the line number is returned.
976 int serial8250_register_8250_port(struct uart_8250_port *up)
978 struct uart_8250_port *uart;
981 if (up->port.uartclk == 0)
984 mutex_lock(&serial_mutex);
986 uart = serial8250_find_match_or_unused(&up->port);
987 if (uart && uart->port.type != PORT_8250_CIR) {
989 uart_remove_one_port(&serial8250_reg, &uart->port);
991 uart->port.iobase = up->port.iobase;
992 uart->port.membase = up->port.membase;
993 uart->port.irq = up->port.irq;
994 uart->port.irqflags = up->port.irqflags;
995 uart->port.uartclk = up->port.uartclk;
996 uart->port.fifosize = up->port.fifosize;
997 uart->port.regshift = up->port.regshift;
998 uart->port.iotype = up->port.iotype;
999 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
1000 uart->bugs = up->bugs;
1001 uart->port.mapbase = up->port.mapbase;
1002 uart->port.mapsize = up->port.mapsize;
1003 uart->port.private_data = up->port.private_data;
1004 uart->tx_loadsz = up->tx_loadsz;
1005 uart->capabilities = up->capabilities;
1006 uart->port.throttle = up->port.throttle;
1007 uart->port.unthrottle = up->port.unthrottle;
1008 uart->port.rs485_config = up->port.rs485_config;
1009 uart->port.rs485 = up->port.rs485;
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;
1019 if (up->port.flags & UPF_FIXED_TYPE)
1020 uart->port.type = up->port.type;
1022 serial8250_set_defaults(uart);
1024 /* Possibly override default I/O functions. */
1025 if (up->port.serial_in)
1026 uart->port.serial_in = up->port.serial_in;
1027 if (up->port.serial_out)
1028 uart->port.serial_out = up->port.serial_out;
1029 if (up->port.handle_irq)
1030 uart->port.handle_irq = up->port.handle_irq;
1031 /* Possibly override set_termios call */
1032 if (up->port.set_termios)
1033 uart->port.set_termios = up->port.set_termios;
1034 if (up->port.set_ldisc)
1035 uart->port.set_ldisc = up->port.set_ldisc;
1036 if (up->port.get_mctrl)
1037 uart->port.get_mctrl = up->port.get_mctrl;
1038 if (up->port.set_mctrl)
1039 uart->port.set_mctrl = up->port.set_mctrl;
1040 if (up->port.get_divisor)
1041 uart->port.get_divisor = up->port.get_divisor;
1042 if (up->port.set_divisor)
1043 uart->port.set_divisor = up->port.set_divisor;
1044 if (up->port.startup)
1045 uart->port.startup = up->port.startup;
1046 if (up->port.shutdown)
1047 uart->port.shutdown = up->port.shutdown;
1049 uart->port.pm = up->port.pm;
1050 if (up->port.handle_break)
1051 uart->port.handle_break = up->port.handle_break;
1053 uart->dl_read = up->dl_read;
1055 uart->dl_write = up->dl_write;
1057 if (uart->port.type != PORT_8250_CIR) {
1058 if (serial8250_isa_config != NULL)
1059 serial8250_isa_config(0, &uart->port,
1060 &uart->capabilities);
1062 serial8250_apply_quirks(uart);
1063 ret = uart_add_one_port(&serial8250_reg,
1068 ret = uart->port.line;
1070 dev_info(uart->port.dev,
1071 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
1073 (unsigned long long)uart->port.mapbase,
1079 /* Initialise interrupt backoff work if required */
1080 if (up->overrun_backoff_time_ms > 0) {
1081 uart->overrun_backoff_time_ms =
1082 up->overrun_backoff_time_ms;
1083 INIT_DELAYED_WORK(&uart->overrun_backoff,
1084 serial_8250_overrun_backoff_work);
1086 uart->overrun_backoff_time_ms = 0;
1090 mutex_unlock(&serial_mutex);
1095 uart->port.dev = NULL;
1096 mutex_unlock(&serial_mutex);
1099 EXPORT_SYMBOL(serial8250_register_8250_port);
1102 * serial8250_unregister_port - remove a 16x50 serial port at runtime
1103 * @line: serial line number
1105 * Remove one serial port. This may not be called from interrupt
1106 * context. We hand the port back to the our control.
1108 void serial8250_unregister_port(int line)
1110 struct uart_8250_port *uart = &serial8250_ports[line];
1112 mutex_lock(&serial_mutex);
1115 unsigned long flags;
1117 spin_lock_irqsave(&uart->port.lock, flags);
1118 serial8250_em485_destroy(uart);
1119 spin_unlock_irqrestore(&uart->port.lock, flags);
1122 uart_remove_one_port(&serial8250_reg, &uart->port);
1123 if (serial8250_isa_devs) {
1124 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
1125 uart->port.type = PORT_UNKNOWN;
1126 uart->port.dev = &serial8250_isa_devs->dev;
1127 uart->capabilities = 0;
1128 serial8250_init_port(uart);
1129 serial8250_apply_quirks(uart);
1130 uart_add_one_port(&serial8250_reg, &uart->port);
1132 uart->port.dev = NULL;
1134 mutex_unlock(&serial_mutex);
1136 EXPORT_SYMBOL(serial8250_unregister_port);
1138 static int __init serial8250_init(void)
1145 serial8250_isa_init_ports();
1147 pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %sabled\n",
1148 nr_uarts, share_irqs ? "en" : "dis");
1151 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
1153 serial8250_reg.nr = UART_NR;
1154 ret = uart_register_driver(&serial8250_reg);
1159 ret = serial8250_pnp_init();
1161 goto unreg_uart_drv;
1163 serial8250_isa_devs = platform_device_alloc("serial8250",
1164 PLAT8250_DEV_LEGACY);
1165 if (!serial8250_isa_devs) {
1170 ret = platform_device_add(serial8250_isa_devs);
1174 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
1176 ret = platform_driver_register(&serial8250_isa_driver);
1180 platform_device_del(serial8250_isa_devs);
1182 platform_device_put(serial8250_isa_devs);
1184 serial8250_pnp_exit();
1187 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1189 uart_unregister_driver(&serial8250_reg);
1195 static void __exit serial8250_exit(void)
1197 struct platform_device *isa_dev = serial8250_isa_devs;
1200 * This tells serial8250_unregister_port() not to re-register
1201 * the ports (thereby making serial8250_isa_driver permanently
1204 serial8250_isa_devs = NULL;
1206 platform_driver_unregister(&serial8250_isa_driver);
1207 platform_device_unregister(isa_dev);
1209 serial8250_pnp_exit();
1212 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1214 uart_unregister_driver(&serial8250_reg);
1218 module_init(serial8250_init);
1219 module_exit(serial8250_exit);
1221 MODULE_LICENSE("GPL");
1222 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
1224 module_param_hw(share_irqs, uint, other, 0644);
1225 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
1227 module_param(nr_uarts, uint, 0644);
1228 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
1230 module_param(skip_txen_test, uint, 0644);
1231 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
1233 #ifdef CONFIG_SERIAL_8250_RSA
1234 module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
1235 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
1237 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
1239 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
1241 /* This module was renamed to 8250_core in 3.7. Keep the old "8250" name
1242 * working as well for the module options so we don't break people. We
1243 * need to keep the names identical and the convenient macros will happily
1244 * refuse to let us do that by failing the build with redefinition errors
1245 * of global variables. So we stick them inside a dummy function to avoid
1246 * those conflicts. The options still get parsed, and the redefined
1247 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
1249 * This is hacky. I'm sorry.
1251 static void __used s8250_options(void)
1253 #undef MODULE_PARAM_PREFIX
1254 #define MODULE_PARAM_PREFIX "8250_core."
1256 module_param_cb(share_irqs, ¶m_ops_uint, &share_irqs, 0644);
1257 module_param_cb(nr_uarts, ¶m_ops_uint, &nr_uarts, 0644);
1258 module_param_cb(skip_txen_test, ¶m_ops_uint, &skip_txen_test, 0644);
1259 #ifdef CONFIG_SERIAL_8250_RSA
1260 __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
1261 ¶m_array_ops, .arr = &__param_arr_probe_rsa,
1266 MODULE_ALIAS("8250_core");