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;
180 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
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 irq_flags |= up->port.irqflags;
216 ret = request_irq(up->port.irq, serial8250_interrupt,
217 irq_flags, up->port.name, i);
219 serial_do_unlink(i, up);
225 static void serial_unlink_irq_chain(struct uart_8250_port *up)
228 * yes, some broken gcc emit "warning: 'i' may be used uninitialized"
229 * but no, we are not going to take a patch that assigns NULL below.
232 struct hlist_node *n;
233 struct hlist_head *h;
235 mutex_lock(&hash_mutex);
237 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
239 hlist_for_each(n, h) {
240 i = hlist_entry(n, struct irq_info, node);
241 if (i->irq == up->port.irq)
246 BUG_ON(i->head == NULL);
248 if (list_empty(i->head))
249 free_irq(up->port.irq, i);
251 serial_do_unlink(i, up);
252 mutex_unlock(&hash_mutex);
256 * This function is used to handle ports that do not have an
257 * interrupt. This doesn't work very well for 16450's, but gives
258 * barely passable results for a 16550A. (Although at the expense
259 * of much CPU overhead).
261 static void serial8250_timeout(struct timer_list *t)
263 struct uart_8250_port *up = from_timer(up, t, timer);
265 up->port.handle_irq(&up->port);
266 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
269 static void serial8250_backup_timeout(struct timer_list *t)
271 struct uart_8250_port *up = from_timer(up, t, timer);
272 unsigned int iir, ier = 0, lsr;
275 spin_lock_irqsave(&up->port.lock, flags);
278 * Must disable interrupts or else we risk racing with the interrupt
282 ier = serial_in(up, UART_IER);
283 serial_out(up, UART_IER, 0);
286 iir = serial_in(up, UART_IIR);
289 * This should be a safe test for anyone who doesn't trust the
290 * IIR bits on their UART, but it's specifically designed for
291 * the "Diva" UART used on the management processor on many HP
292 * ia64 and parisc boxes.
294 lsr = serial_in(up, UART_LSR);
295 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
296 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
297 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
298 (lsr & UART_LSR_THRE)) {
299 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
300 iir |= UART_IIR_THRI;
303 if (!(iir & UART_IIR_NO_INT))
304 serial8250_tx_chars(up);
307 serial_out(up, UART_IER, ier);
309 spin_unlock_irqrestore(&up->port.lock, flags);
311 /* Standard timer interval plus 0.2s to keep the port running */
312 mod_timer(&up->timer,
313 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
316 static int univ8250_setup_irq(struct uart_8250_port *up)
318 struct uart_port *port = &up->port;
322 * The above check will only give an accurate result the first time
323 * the port is opened so this value needs to be preserved.
325 if (up->bugs & UART_BUG_THRE) {
326 pr_debug("ttyS%d - using backup timer\n", serial_index(port));
328 up->timer.function = serial8250_backup_timeout;
329 mod_timer(&up->timer, jiffies +
330 uart_poll_timeout(port) + HZ / 5);
334 * If the "interrupt" for this port doesn't correspond with any
335 * hardware interrupt, we use a timer-based system. The original
336 * driver used to do this with IRQ0.
339 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
341 retval = serial_link_irq_chain(up);
346 static void univ8250_release_irq(struct uart_8250_port *up)
348 struct uart_port *port = &up->port;
350 del_timer_sync(&up->timer);
351 up->timer.function = serial8250_timeout;
353 serial_unlink_irq_chain(up);
356 #ifdef CONFIG_SERIAL_8250_RSA
357 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
359 unsigned long start = UART_RSA_BASE << up->port.regshift;
360 unsigned int size = 8 << up->port.regshift;
361 struct uart_port *port = &up->port;
364 switch (port->iotype) {
367 start += port->iobase;
368 if (request_region(start, size, "serial-rsa"))
378 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
380 unsigned long offset = UART_RSA_BASE << up->port.regshift;
381 unsigned int size = 8 << up->port.regshift;
382 struct uart_port *port = &up->port;
384 switch (port->iotype) {
387 release_region(port->iobase + offset, size);
393 static const struct uart_ops *base_ops;
394 static struct uart_ops univ8250_port_ops;
396 static const struct uart_8250_ops univ8250_driver_ops = {
397 .setup_irq = univ8250_setup_irq,
398 .release_irq = univ8250_release_irq,
401 static struct uart_8250_port serial8250_ports[UART_NR];
404 * serial8250_get_port - retrieve struct uart_8250_port
405 * @line: serial line number
407 * This function retrieves struct uart_8250_port for the specific line.
408 * This struct *must* *not* be used to perform a 8250 or serial core operation
409 * which is not accessible otherwise. Its only purpose is to make the struct
410 * accessible to the runtime-pm callbacks for context suspend/restore.
411 * The lock assumption made here is none because runtime-pm suspend/resume
412 * callbacks should not be invoked if there is any operation performed on the
415 struct uart_8250_port *serial8250_get_port(int line)
417 return &serial8250_ports[line];
419 EXPORT_SYMBOL_GPL(serial8250_get_port);
421 static void (*serial8250_isa_config)(int port, struct uart_port *up,
424 void serial8250_set_isa_configurator(
425 void (*v)(int port, struct uart_port *up, u32 *capabilities))
427 serial8250_isa_config = v;
429 EXPORT_SYMBOL(serial8250_set_isa_configurator);
431 #ifdef CONFIG_SERIAL_8250_RSA
433 static void univ8250_config_port(struct uart_port *port, int flags)
435 struct uart_8250_port *up = up_to_u8250p(port);
437 up->probe &= ~UART_PROBE_RSA;
438 if (port->type == PORT_RSA) {
439 if (serial8250_request_rsa_resource(up) == 0)
440 up->probe |= UART_PROBE_RSA;
441 } else if (flags & UART_CONFIG_TYPE) {
444 for (i = 0; i < probe_rsa_count; i++) {
445 if (probe_rsa[i] == up->port.iobase) {
446 if (serial8250_request_rsa_resource(up) == 0)
447 up->probe |= UART_PROBE_RSA;
453 base_ops->config_port(port, flags);
455 if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
456 serial8250_release_rsa_resource(up);
459 static int univ8250_request_port(struct uart_port *port)
461 struct uart_8250_port *up = up_to_u8250p(port);
464 ret = base_ops->request_port(port);
465 if (ret == 0 && port->type == PORT_RSA) {
466 ret = serial8250_request_rsa_resource(up);
468 base_ops->release_port(port);
474 static void univ8250_release_port(struct uart_port *port)
476 struct uart_8250_port *up = up_to_u8250p(port);
478 if (port->type == PORT_RSA)
479 serial8250_release_rsa_resource(up);
480 base_ops->release_port(port);
483 static void univ8250_rsa_support(struct uart_ops *ops)
485 ops->config_port = univ8250_config_port;
486 ops->request_port = univ8250_request_port;
487 ops->release_port = univ8250_release_port;
491 #define univ8250_rsa_support(x) do { } while (0)
492 #endif /* CONFIG_SERIAL_8250_RSA */
494 static inline void serial8250_apply_quirks(struct uart_8250_port *up)
496 up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0;
499 static void __init serial8250_isa_init_ports(void)
501 struct uart_8250_port *up;
502 static int first = 1;
509 if (nr_uarts > UART_NR)
512 for (i = 0; i < nr_uarts; i++) {
513 struct uart_8250_port *up = &serial8250_ports[i];
514 struct uart_port *port = &up->port;
517 serial8250_init_port(up);
519 base_ops = port->ops;
520 port->ops = &univ8250_port_ops;
522 timer_setup(&up->timer, serial8250_timeout, 0);
524 up->ops = &univ8250_driver_ops;
527 * ALPHA_KLUDGE_MCR needs to be killed.
529 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
530 up->mcr_force = ALPHA_KLUDGE_MCR;
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;
554 serial8250_set_defaults(up);
556 port->irqflags |= irqflag;
557 if (serial8250_isa_config != NULL)
558 serial8250_isa_config(i, &up->port, &up->capabilities);
563 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
567 for (i = 0; i < nr_uarts; i++) {
568 struct uart_8250_port *up = &serial8250_ports[i];
570 if (up->port.type == PORT_8250_CIR)
578 serial8250_apply_quirks(up);
579 uart_add_one_port(drv, &up->port);
583 #ifdef CONFIG_SERIAL_8250_CONSOLE
585 static void univ8250_console_write(struct console *co, const char *s,
588 struct uart_8250_port *up = &serial8250_ports[co->index];
590 serial8250_console_write(up, s, count);
593 static int univ8250_console_setup(struct console *co, char *options)
595 struct uart_port *port;
599 * Check whether an invalid uart number has been specified, and
600 * if so, search for the first available port that does have
603 if (co->index >= nr_uarts)
605 port = &serial8250_ports[co->index].port;
606 /* link port to console */
609 retval = serial8250_console_setup(port, options, false);
616 * univ8250_console_match - non-standard console matching
617 * @co: registering console
618 * @name: name from console command line
619 * @idx: index from console command line
620 * @options: ptr to option string from console command line
622 * Only attempts to match console command lines of the form:
623 * console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>]
624 * console=uart[8250],0x<addr>[,<options>]
625 * This form is used to register an initial earlycon boot console and
626 * replace it with the serial8250_console at 8250 driver init.
628 * Performs console setup for a match (as required by interface)
629 * If no <options> are specified, then assume the h/w is already setup.
631 * Returns 0 if console matches; otherwise non-zero to use default matching
633 static int univ8250_console_match(struct console *co, char *name, int idx,
636 char match[] = "uart"; /* 8250-specific earlycon name */
637 unsigned char iotype;
638 resource_size_t addr;
641 if (strncmp(name, match, 4) != 0)
644 if (uart_parse_earlycon(options, &iotype, &addr, &options))
647 /* try to match the port specified on the command line */
648 for (i = 0; i < nr_uarts; i++) {
649 struct uart_port *port = &serial8250_ports[i].port;
651 if (port->iotype != iotype)
653 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
654 iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
655 && (port->mapbase != addr))
657 if (iotype == UPIO_PORT && port->iobase != addr)
662 return serial8250_console_setup(port, options, true);
668 static struct console univ8250_console = {
670 .write = univ8250_console_write,
671 .device = uart_console_device,
672 .setup = univ8250_console_setup,
673 .match = univ8250_console_match,
674 .flags = CON_PRINTBUFFER | CON_ANYTIME,
676 .data = &serial8250_reg,
679 static int __init univ8250_console_init(void)
684 serial8250_isa_init_ports();
685 register_console(&univ8250_console);
688 console_initcall(univ8250_console_init);
690 #define SERIAL8250_CONSOLE (&univ8250_console)
692 #define SERIAL8250_CONSOLE NULL
695 static struct uart_driver serial8250_reg = {
696 .owner = THIS_MODULE,
697 .driver_name = "serial",
701 .cons = SERIAL8250_CONSOLE,
705 * early_serial_setup - early registration for 8250 ports
707 * Setup an 8250 port structure prior to console initialisation. Use
708 * after console initialisation will cause undefined behaviour.
710 int __init early_serial_setup(struct uart_port *port)
714 if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
717 serial8250_isa_init_ports();
718 p = &serial8250_ports[port->line].port;
719 p->iobase = port->iobase;
720 p->membase = port->membase;
722 p->irqflags = port->irqflags;
723 p->uartclk = port->uartclk;
724 p->fifosize = port->fifosize;
725 p->regshift = port->regshift;
726 p->iotype = port->iotype;
727 p->flags = port->flags;
728 p->mapbase = port->mapbase;
729 p->mapsize = port->mapsize;
730 p->private_data = port->private_data;
731 p->type = port->type;
732 p->line = port->line;
734 serial8250_set_defaults(up_to_u8250p(p));
737 p->serial_in = port->serial_in;
738 if (port->serial_out)
739 p->serial_out = port->serial_out;
740 if (port->handle_irq)
741 p->handle_irq = port->handle_irq;
747 * serial8250_suspend_port - suspend one serial port
748 * @line: serial line number
750 * Suspend one serial port.
752 void serial8250_suspend_port(int line)
754 struct uart_8250_port *up = &serial8250_ports[line];
755 struct uart_port *port = &up->port;
757 if (!console_suspend_enabled && uart_console(port) &&
758 port->type != PORT_8250) {
759 unsigned char canary = 0xa5;
760 serial_out(up, UART_SCR, canary);
761 if (serial_in(up, UART_SCR) == canary)
765 uart_suspend_port(&serial8250_reg, port);
767 EXPORT_SYMBOL(serial8250_suspend_port);
770 * serial8250_resume_port - resume one serial port
771 * @line: serial line number
773 * Resume one serial port.
775 void serial8250_resume_port(int line)
777 struct uart_8250_port *up = &serial8250_ports[line];
778 struct uart_port *port = &up->port;
782 if (up->capabilities & UART_NATSEMI) {
783 /* Ensure it's still in high speed mode */
784 serial_port_out(port, UART_LCR, 0xE0);
786 ns16550a_goto_highspeed(up);
788 serial_port_out(port, UART_LCR, 0);
789 port->uartclk = 921600*16;
791 uart_resume_port(&serial8250_reg, port);
793 EXPORT_SYMBOL(serial8250_resume_port);
796 * Register a set of serial devices attached to a platform device. The
797 * list is terminated with a zero flags entry, which means we expect
798 * all entries to have at least UPF_BOOT_AUTOCONF set.
800 static int serial8250_probe(struct platform_device *dev)
802 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
803 struct uart_8250_port uart;
804 int ret, i, irqflag = 0;
806 memset(&uart, 0, sizeof(uart));
809 irqflag = IRQF_SHARED;
811 for (i = 0; p && p->flags != 0; p++, i++) {
812 uart.port.iobase = p->iobase;
813 uart.port.membase = p->membase;
814 uart.port.irq = p->irq;
815 uart.port.irqflags = p->irqflags;
816 uart.port.uartclk = p->uartclk;
817 uart.port.regshift = p->regshift;
818 uart.port.iotype = p->iotype;
819 uart.port.flags = p->flags;
820 uart.port.mapbase = p->mapbase;
821 uart.port.hub6 = p->hub6;
822 uart.port.private_data = p->private_data;
823 uart.port.type = p->type;
824 uart.port.serial_in = p->serial_in;
825 uart.port.serial_out = p->serial_out;
826 uart.port.handle_irq = p->handle_irq;
827 uart.port.handle_break = p->handle_break;
828 uart.port.set_termios = p->set_termios;
829 uart.port.set_ldisc = p->set_ldisc;
830 uart.port.get_mctrl = p->get_mctrl;
831 uart.port.pm = p->pm;
832 uart.port.dev = &dev->dev;
833 uart.port.irqflags |= irqflag;
834 ret = serial8250_register_8250_port(&uart);
836 dev_err(&dev->dev, "unable to register port at index %d "
837 "(IO%lx MEM%llx IRQ%d): %d\n", i,
838 p->iobase, (unsigned long long)p->mapbase,
846 * Remove serial ports registered against a platform device.
848 static int serial8250_remove(struct platform_device *dev)
852 for (i = 0; i < nr_uarts; i++) {
853 struct uart_8250_port *up = &serial8250_ports[i];
855 if (up->port.dev == &dev->dev)
856 serial8250_unregister_port(i);
861 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
865 for (i = 0; i < UART_NR; i++) {
866 struct uart_8250_port *up = &serial8250_ports[i];
868 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
869 uart_suspend_port(&serial8250_reg, &up->port);
875 static int serial8250_resume(struct platform_device *dev)
879 for (i = 0; i < UART_NR; i++) {
880 struct uart_8250_port *up = &serial8250_ports[i];
882 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
883 serial8250_resume_port(i);
889 static struct platform_driver serial8250_isa_driver = {
890 .probe = serial8250_probe,
891 .remove = serial8250_remove,
892 .suspend = serial8250_suspend,
893 .resume = serial8250_resume,
895 .name = "serial8250",
900 * This "device" covers _all_ ISA 8250-compatible serial devices listed
901 * in the table in include/asm/serial.h
903 static struct platform_device *serial8250_isa_devs;
906 * serial8250_register_8250_port and serial8250_unregister_port allows for
907 * 16x50 serial ports to be configured at run-time, to support PCMCIA
908 * modems and PCI multiport cards.
910 static DEFINE_MUTEX(serial_mutex);
912 static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
917 * First, find a port entry which matches.
919 for (i = 0; i < nr_uarts; i++)
920 if (uart_match_port(&serial8250_ports[i].port, port))
921 return &serial8250_ports[i];
923 /* try line number first if still available */
925 if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
926 serial8250_ports[i].port.iobase == 0)
927 return &serial8250_ports[i];
929 * We didn't find a matching entry, so look for the first
930 * free entry. We look for one which hasn't been previously
931 * used (indicated by zero iobase).
933 for (i = 0; i < nr_uarts; i++)
934 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
935 serial8250_ports[i].port.iobase == 0)
936 return &serial8250_ports[i];
939 * That also failed. Last resort is to find any entry which
940 * doesn't have a real port associated with it.
942 for (i = 0; i < nr_uarts; i++)
943 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
944 return &serial8250_ports[i];
950 * serial8250_register_8250_port - register a serial port
951 * @up: serial port template
953 * Configure the serial port specified by the request. If the
954 * port exists and is in use, it is hung up and unregistered
957 * The port is then probed and if necessary the IRQ is autodetected
958 * If this fails an error is returned.
960 * On success the port is ready to use and the line number is returned.
962 int serial8250_register_8250_port(struct uart_8250_port *up)
964 struct uart_8250_port *uart;
967 if (up->port.uartclk == 0)
970 mutex_lock(&serial_mutex);
972 uart = serial8250_find_match_or_unused(&up->port);
973 if (uart && uart->port.type != PORT_8250_CIR) {
975 uart_remove_one_port(&serial8250_reg, &uart->port);
977 uart->port.iobase = up->port.iobase;
978 uart->port.membase = up->port.membase;
979 uart->port.irq = up->port.irq;
980 uart->port.irqflags = up->port.irqflags;
981 uart->port.uartclk = up->port.uartclk;
982 uart->port.fifosize = up->port.fifosize;
983 uart->port.regshift = up->port.regshift;
984 uart->port.iotype = up->port.iotype;
985 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
986 uart->bugs = up->bugs;
987 uart->port.mapbase = up->port.mapbase;
988 uart->port.mapsize = up->port.mapsize;
989 uart->port.private_data = up->port.private_data;
990 uart->tx_loadsz = up->tx_loadsz;
991 uart->capabilities = up->capabilities;
992 uart->port.throttle = up->port.throttle;
993 uart->port.unthrottle = up->port.unthrottle;
994 uart->port.rs485_config = up->port.rs485_config;
995 uart->port.rs485 = up->port.rs485;
998 /* Take tx_loadsz from fifosize if it wasn't set separately */
999 if (uart->port.fifosize && !uart->tx_loadsz)
1000 uart->tx_loadsz = uart->port.fifosize;
1003 uart->port.dev = up->port.dev;
1005 if (up->port.flags & UPF_FIXED_TYPE)
1006 uart->port.type = up->port.type;
1008 serial8250_set_defaults(uart);
1010 /* Possibly override default I/O functions. */
1011 if (up->port.serial_in)
1012 uart->port.serial_in = up->port.serial_in;
1013 if (up->port.serial_out)
1014 uart->port.serial_out = up->port.serial_out;
1015 if (up->port.handle_irq)
1016 uart->port.handle_irq = up->port.handle_irq;
1017 /* Possibly override set_termios call */
1018 if (up->port.set_termios)
1019 uart->port.set_termios = up->port.set_termios;
1020 if (up->port.set_ldisc)
1021 uart->port.set_ldisc = up->port.set_ldisc;
1022 if (up->port.get_mctrl)
1023 uart->port.get_mctrl = up->port.get_mctrl;
1024 if (up->port.set_mctrl)
1025 uart->port.set_mctrl = up->port.set_mctrl;
1026 if (up->port.get_divisor)
1027 uart->port.get_divisor = up->port.get_divisor;
1028 if (up->port.set_divisor)
1029 uart->port.set_divisor = up->port.set_divisor;
1030 if (up->port.startup)
1031 uart->port.startup = up->port.startup;
1032 if (up->port.shutdown)
1033 uart->port.shutdown = up->port.shutdown;
1035 uart->port.pm = up->port.pm;
1036 if (up->port.handle_break)
1037 uart->port.handle_break = up->port.handle_break;
1039 uart->dl_read = up->dl_read;
1041 uart->dl_write = up->dl_write;
1043 if (uart->port.type != PORT_8250_CIR) {
1044 if (serial8250_isa_config != NULL)
1045 serial8250_isa_config(0, &uart->port,
1046 &uart->capabilities);
1048 serial8250_apply_quirks(uart);
1049 ret = uart_add_one_port(&serial8250_reg,
1052 ret = uart->port.line;
1054 dev_info(uart->port.dev,
1055 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
1057 (unsigned long long)uart->port.mapbase,
1063 mutex_unlock(&serial_mutex);
1067 EXPORT_SYMBOL(serial8250_register_8250_port);
1070 * serial8250_unregister_port - remove a 16x50 serial port at runtime
1071 * @line: serial line number
1073 * Remove one serial port. This may not be called from interrupt
1074 * context. We hand the port back to the our control.
1076 void serial8250_unregister_port(int line)
1078 struct uart_8250_port *uart = &serial8250_ports[line];
1080 mutex_lock(&serial_mutex);
1083 unsigned long flags;
1085 spin_lock_irqsave(&uart->port.lock, flags);
1086 serial8250_em485_destroy(uart);
1087 spin_unlock_irqrestore(&uart->port.lock, flags);
1090 uart_remove_one_port(&serial8250_reg, &uart->port);
1091 if (serial8250_isa_devs) {
1092 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
1093 uart->port.type = PORT_UNKNOWN;
1094 uart->port.dev = &serial8250_isa_devs->dev;
1095 uart->capabilities = 0;
1096 serial8250_apply_quirks(uart);
1097 uart_add_one_port(&serial8250_reg, &uart->port);
1099 uart->port.dev = NULL;
1101 mutex_unlock(&serial_mutex);
1103 EXPORT_SYMBOL(serial8250_unregister_port);
1105 static int __init serial8250_init(void)
1112 serial8250_isa_init_ports();
1114 pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %sabled\n",
1115 nr_uarts, share_irqs ? "en" : "dis");
1118 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
1120 serial8250_reg.nr = UART_NR;
1121 ret = uart_register_driver(&serial8250_reg);
1126 ret = serial8250_pnp_init();
1128 goto unreg_uart_drv;
1130 serial8250_isa_devs = platform_device_alloc("serial8250",
1131 PLAT8250_DEV_LEGACY);
1132 if (!serial8250_isa_devs) {
1137 ret = platform_device_add(serial8250_isa_devs);
1141 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
1143 ret = platform_driver_register(&serial8250_isa_driver);
1147 platform_device_del(serial8250_isa_devs);
1149 platform_device_put(serial8250_isa_devs);
1151 serial8250_pnp_exit();
1154 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1156 uart_unregister_driver(&serial8250_reg);
1162 static void __exit serial8250_exit(void)
1164 struct platform_device *isa_dev = serial8250_isa_devs;
1167 * This tells serial8250_unregister_port() not to re-register
1168 * the ports (thereby making serial8250_isa_driver permanently
1171 serial8250_isa_devs = NULL;
1173 platform_driver_unregister(&serial8250_isa_driver);
1174 platform_device_unregister(isa_dev);
1176 serial8250_pnp_exit();
1179 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1181 uart_unregister_driver(&serial8250_reg);
1185 module_init(serial8250_init);
1186 module_exit(serial8250_exit);
1188 MODULE_LICENSE("GPL");
1189 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
1191 module_param_hw(share_irqs, uint, other, 0644);
1192 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
1194 module_param(nr_uarts, uint, 0644);
1195 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
1197 module_param(skip_txen_test, uint, 0644);
1198 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
1200 #ifdef CONFIG_SERIAL_8250_RSA
1201 module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
1202 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
1204 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
1206 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
1208 /* This module was renamed to 8250_core in 3.7. Keep the old "8250" name
1209 * working as well for the module options so we don't break people. We
1210 * need to keep the names identical and the convenient macros will happily
1211 * refuse to let us do that by failing the build with redefinition errors
1212 * of global variables. So we stick them inside a dummy function to avoid
1213 * those conflicts. The options still get parsed, and the redefined
1214 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
1216 * This is hacky. I'm sorry.
1218 static void __used s8250_options(void)
1220 #undef MODULE_PARAM_PREFIX
1221 #define MODULE_PARAM_PREFIX "8250_core."
1223 module_param_cb(share_irqs, ¶m_ops_uint, &share_irqs, 0644);
1224 module_param_cb(nr_uarts, ¶m_ops_uint, &nr_uarts, 0644);
1225 module_param_cb(skip_txen_test, ¶m_ops_uint, &skip_txen_test, 0644);
1226 #ifdef CONFIG_SERIAL_8250_RSA
1227 __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
1228 ¶m_array_ops, .arr = &__param_arr_probe_rsa,
1233 MODULE_ALIAS("8250_core");