2 * Universal/legacy driver for 8250/16550-type serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Copyright (C) 2001 Russell King.
8 * Supports: ISA-compatible 8250/16550 ports
10 * early_serial_setup() ports
11 * userspace-configurable "phantom" ports
12 * "serial8250" platform devices
13 * serial8250_register_8250_port() ports
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/ioport.h>
24 #include <linux/init.h>
25 #include <linux/console.h>
26 #include <linux/sysrq.h>
27 #include <linux/delay.h>
28 #include <linux/platform_device.h>
29 #include <linux/tty.h>
30 #include <linux/ratelimit.h>
31 #include <linux/tty_flip.h>
32 #include <linux/serial.h>
33 #include <linux/serial_8250.h>
34 #include <linux/nmi.h>
35 #include <linux/mutex.h>
36 #include <linux/slab.h>
37 #include <linux/uaccess.h>
38 #include <linux/pm_runtime.h>
41 #include <linux/sunserialcore.h>
50 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
51 * is unsafe when used on edge-triggered interrupts.
53 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
55 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
57 static struct uart_driver serial8250_reg;
59 static unsigned int skip_txen_test; /* force skip of txen test at init time */
61 #define PASS_LIMIT 512
63 #include <asm/serial.h>
65 * SERIAL_PORT_DFNS tells us about built-in ports that have no
66 * standard enumeration mechanism. Platforms that can find all
67 * serial ports via mechanisms like ACPI or PCI need not supply it.
69 #ifndef SERIAL_PORT_DFNS
70 #define SERIAL_PORT_DFNS
73 static const struct old_serial_port old_serial_port[] = {
74 SERIAL_PORT_DFNS /* defined in asm/serial.h */
77 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
79 #ifdef CONFIG_SERIAL_8250_RSA
81 #define PORT_RSA_MAX 4
82 static unsigned long probe_rsa[PORT_RSA_MAX];
83 static unsigned int probe_rsa_count;
84 #endif /* CONFIG_SERIAL_8250_RSA */
87 struct hlist_node node;
89 spinlock_t lock; /* Protects list not the hash */
90 struct list_head *head;
93 #define NR_IRQ_HASH 32 /* Can be adjusted later */
94 static struct hlist_head irq_lists[NR_IRQ_HASH];
95 static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
98 * This is the serial driver's interrupt routine.
100 * Arjan thinks the old way was overly complex, so it got simplified.
101 * Alan disagrees, saying that need the complexity to handle the weird
102 * nature of ISA shared interrupts. (This is a special exception.)
104 * In order to handle ISA shared interrupts properly, we need to check
105 * that all ports have been serviced, and therefore the ISA interrupt
106 * line has been de-asserted.
108 * This means we need to loop through all ports. checking that they
109 * don't have an interrupt pending.
111 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
113 struct irq_info *i = dev_id;
114 struct list_head *l, *end = NULL;
115 int pass_counter = 0, handled = 0;
117 pr_debug("%s(%d): start\n", __func__, irq);
123 struct uart_8250_port *up;
124 struct uart_port *port;
126 up = list_entry(l, struct uart_8250_port, list);
129 if (port->handle_irq(port)) {
132 } else if (end == NULL)
137 if (l == i->head && pass_counter++ > PASS_LIMIT) {
138 /* If we hit this, we're dead. */
139 printk_ratelimited(KERN_ERR
140 "serial8250: too much work for irq%d\n", irq);
145 spin_unlock(&i->lock);
147 pr_debug("%s(%d): end\n", __func__, irq);
149 return IRQ_RETVAL(handled);
153 * To support ISA shared interrupts, we need to have one interrupt
154 * handler that ensures that the IRQ line has been deasserted
155 * before returning. Failing to do this will result in the IRQ
156 * line being stuck active, and, since ISA irqs are edge triggered,
157 * no more IRQs will be seen.
159 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
161 spin_lock_irq(&i->lock);
163 if (!list_empty(i->head)) {
164 if (i->head == &up->list)
165 i->head = i->head->next;
168 BUG_ON(i->head != &up->list);
171 spin_unlock_irq(&i->lock);
172 /* List empty so throw away the hash node */
173 if (i->head == NULL) {
179 static int serial_link_irq_chain(struct uart_8250_port *up)
181 struct hlist_head *h;
182 struct hlist_node *n;
186 mutex_lock(&hash_mutex);
188 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
190 hlist_for_each(n, h) {
191 i = hlist_entry(n, struct irq_info, node);
192 if (i->irq == up->port.irq)
197 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
199 mutex_unlock(&hash_mutex);
202 spin_lock_init(&i->lock);
203 i->irq = up->port.irq;
204 hlist_add_head(&i->node, h);
206 mutex_unlock(&hash_mutex);
208 spin_lock_irq(&i->lock);
211 list_add(&up->list, i->head);
212 spin_unlock_irq(&i->lock);
216 INIT_LIST_HEAD(&up->list);
218 spin_unlock_irq(&i->lock);
219 ret = request_irq(up->port.irq, serial8250_interrupt,
220 up->port.irqflags, "serial", i);
222 serial_do_unlink(i, up);
228 static void serial_unlink_irq_chain(struct uart_8250_port *up)
231 * yes, some broken gcc emit "warning: 'i' may be used uninitialized"
232 * but no, we are not going to take a patch that assigns NULL below.
235 struct hlist_node *n;
236 struct hlist_head *h;
238 mutex_lock(&hash_mutex);
240 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
242 hlist_for_each(n, h) {
243 i = hlist_entry(n, struct irq_info, node);
244 if (i->irq == up->port.irq)
249 BUG_ON(i->head == NULL);
251 if (list_empty(i->head))
252 free_irq(up->port.irq, i);
254 serial_do_unlink(i, up);
255 mutex_unlock(&hash_mutex);
259 * This function is used to handle ports that do not have an
260 * interrupt. This doesn't work very well for 16450's, but gives
261 * barely passable results for a 16550A. (Although at the expense
262 * of much CPU overhead).
264 static void serial8250_timeout(unsigned long data)
266 struct uart_8250_port *up = (struct uart_8250_port *)data;
268 up->port.handle_irq(&up->port);
269 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
272 static void serial8250_backup_timeout(unsigned long data)
274 struct uart_8250_port *up = (struct uart_8250_port *)data;
275 unsigned int iir, ier = 0, lsr;
278 spin_lock_irqsave(&up->port.lock, flags);
281 * Must disable interrupts or else we risk racing with the interrupt
285 ier = serial_in(up, UART_IER);
286 serial_out(up, UART_IER, 0);
289 iir = serial_in(up, UART_IIR);
292 * This should be a safe test for anyone who doesn't trust the
293 * IIR bits on their UART, but it's specifically designed for
294 * the "Diva" UART used on the management processor on many HP
295 * ia64 and parisc boxes.
297 lsr = serial_in(up, UART_LSR);
298 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
299 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
300 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
301 (lsr & UART_LSR_THRE)) {
302 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
303 iir |= UART_IIR_THRI;
306 if (!(iir & UART_IIR_NO_INT))
307 serial8250_tx_chars(up);
310 serial_out(up, UART_IER, ier);
312 spin_unlock_irqrestore(&up->port.lock, flags);
314 /* Standard timer interval plus 0.2s to keep the port running */
315 mod_timer(&up->timer,
316 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
319 static int univ8250_setup_irq(struct uart_8250_port *up)
321 struct uart_port *port = &up->port;
325 * The above check will only give an accurate result the first time
326 * the port is opened so this value needs to be preserved.
328 if (up->bugs & UART_BUG_THRE) {
329 pr_debug("ttyS%d - using backup timer\n", serial_index(port));
331 up->timer.function = serial8250_backup_timeout;
332 up->timer.data = (unsigned long)up;
333 mod_timer(&up->timer, jiffies +
334 uart_poll_timeout(port) + HZ / 5);
338 * If the "interrupt" for this port doesn't correspond with any
339 * hardware interrupt, we use a timer-based system. The original
340 * driver used to do this with IRQ0.
343 up->timer.data = (unsigned long)up;
344 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
346 retval = serial_link_irq_chain(up);
351 static void univ8250_release_irq(struct uart_8250_port *up)
353 struct uart_port *port = &up->port;
355 del_timer_sync(&up->timer);
356 up->timer.function = serial8250_timeout;
358 serial_unlink_irq_chain(up);
361 #ifdef CONFIG_SERIAL_8250_RSA
362 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
364 unsigned long start = UART_RSA_BASE << up->port.regshift;
365 unsigned int size = 8 << up->port.regshift;
366 struct uart_port *port = &up->port;
369 switch (port->iotype) {
372 start += port->iobase;
373 if (request_region(start, size, "serial-rsa"))
383 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
385 unsigned long offset = UART_RSA_BASE << up->port.regshift;
386 unsigned int size = 8 << up->port.regshift;
387 struct uart_port *port = &up->port;
389 switch (port->iotype) {
392 release_region(port->iobase + offset, size);
398 static const struct uart_ops *base_ops;
399 static struct uart_ops univ8250_port_ops;
401 static const struct uart_8250_ops univ8250_driver_ops = {
402 .setup_irq = univ8250_setup_irq,
403 .release_irq = univ8250_release_irq,
406 static struct uart_8250_port serial8250_ports[UART_NR];
409 * serial8250_get_port - retrieve struct uart_8250_port
410 * @line: serial line number
412 * This function retrieves struct uart_8250_port for the specific line.
413 * This struct *must* *not* be used to perform a 8250 or serial core operation
414 * which is not accessible otherwise. Its only purpose is to make the struct
415 * accessible to the runtime-pm callbacks for context suspend/restore.
416 * The lock assumption made here is none because runtime-pm suspend/resume
417 * callbacks should not be invoked if there is any operation performed on the
420 struct uart_8250_port *serial8250_get_port(int line)
422 return &serial8250_ports[line];
424 EXPORT_SYMBOL_GPL(serial8250_get_port);
426 static void (*serial8250_isa_config)(int port, struct uart_port *up,
427 unsigned short *capabilities);
429 void serial8250_set_isa_configurator(
430 void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
432 serial8250_isa_config = v;
434 EXPORT_SYMBOL(serial8250_set_isa_configurator);
436 #ifdef CONFIG_SERIAL_8250_RSA
438 static void univ8250_config_port(struct uart_port *port, int flags)
440 struct uart_8250_port *up = up_to_u8250p(port);
442 up->probe &= ~UART_PROBE_RSA;
443 if (port->type == PORT_RSA) {
444 if (serial8250_request_rsa_resource(up) == 0)
445 up->probe |= UART_PROBE_RSA;
446 } else if (flags & UART_CONFIG_TYPE) {
449 for (i = 0; i < probe_rsa_count; i++) {
450 if (probe_rsa[i] == up->port.iobase) {
451 if (serial8250_request_rsa_resource(up) == 0)
452 up->probe |= UART_PROBE_RSA;
458 base_ops->config_port(port, flags);
460 if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
461 serial8250_release_rsa_resource(up);
464 static int univ8250_request_port(struct uart_port *port)
466 struct uart_8250_port *up = up_to_u8250p(port);
469 ret = base_ops->request_port(port);
470 if (ret == 0 && port->type == PORT_RSA) {
471 ret = serial8250_request_rsa_resource(up);
473 base_ops->release_port(port);
479 static void univ8250_release_port(struct uart_port *port)
481 struct uart_8250_port *up = up_to_u8250p(port);
483 if (port->type == PORT_RSA)
484 serial8250_release_rsa_resource(up);
485 base_ops->release_port(port);
488 static void univ8250_rsa_support(struct uart_ops *ops)
490 ops->config_port = univ8250_config_port;
491 ops->request_port = univ8250_request_port;
492 ops->release_port = univ8250_release_port;
496 #define univ8250_rsa_support(x) do { } while (0)
497 #endif /* CONFIG_SERIAL_8250_RSA */
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 init_timer(&up->timer);
523 up->timer.function = serial8250_timeout;
525 up->ops = &univ8250_driver_ops;
528 * ALPHA_KLUDGE_MCR needs to be killed.
530 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
531 up->mcr_force = ALPHA_KLUDGE_MCR;
532 serial8250_set_defaults(up);
535 /* chain base port ops to support Remote Supervisor Adapter */
536 univ8250_port_ops = *base_ops;
537 univ8250_rsa_support(&univ8250_port_ops);
540 irqflag = IRQF_SHARED;
542 for (i = 0, up = serial8250_ports;
543 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
545 struct uart_port *port = &up->port;
547 port->iobase = old_serial_port[i].port;
548 port->irq = irq_canonicalize(old_serial_port[i].irq);
550 port->uartclk = old_serial_port[i].baud_base * 16;
551 port->flags = old_serial_port[i].flags;
553 port->membase = old_serial_port[i].iomem_base;
554 port->iotype = old_serial_port[i].io_type;
555 port->regshift = old_serial_port[i].iomem_reg_shift;
557 port->irqflags |= irqflag;
558 if (serial8250_isa_config != NULL)
559 serial8250_isa_config(i, &up->port, &up->capabilities);
564 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
568 for (i = 0; i < nr_uarts; i++) {
569 struct uart_8250_port *up = &serial8250_ports[i];
571 if (up->port.type == PORT_8250_CIR)
580 up->port.flags |= UPF_NO_TXEN_TEST;
582 uart_add_one_port(drv, &up->port);
586 #ifdef CONFIG_SERIAL_8250_CONSOLE
588 static void univ8250_console_write(struct console *co, const char *s,
591 struct uart_8250_port *up = &serial8250_ports[co->index];
593 serial8250_console_write(up, s, count);
596 static int univ8250_console_setup(struct console *co, char *options)
598 struct uart_port *port;
602 * Check whether an invalid uart number has been specified, and
603 * if so, search for the first available port that does have
606 if (co->index >= nr_uarts)
608 port = &serial8250_ports[co->index].port;
609 /* link port to console */
612 retval = serial8250_console_setup(port, options, false);
619 * univ8250_console_match - non-standard console matching
620 * @co: registering console
621 * @name: name from console command line
622 * @idx: index from console command line
623 * @options: ptr to option string from console command line
625 * Only attempts to match console command lines of the form:
626 * console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>]
627 * console=uart[8250],0x<addr>[,<options>]
628 * This form is used to register an initial earlycon boot console and
629 * replace it with the serial8250_console at 8250 driver init.
631 * Performs console setup for a match (as required by interface)
632 * If no <options> are specified, then assume the h/w is already setup.
634 * Returns 0 if console matches; otherwise non-zero to use default matching
636 static int univ8250_console_match(struct console *co, char *name, int idx,
639 char match[] = "uart"; /* 8250-specific earlycon name */
640 unsigned char iotype;
641 resource_size_t addr;
644 if (strncmp(name, match, 4) != 0)
647 if (uart_parse_earlycon(options, &iotype, &addr, &options))
650 /* try to match the port specified on the command line */
651 for (i = 0; i < nr_uarts; i++) {
652 struct uart_port *port = &serial8250_ports[i].port;
654 if (port->iotype != iotype)
656 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
657 iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
658 && (port->mapbase != addr))
660 if (iotype == UPIO_PORT && port->iobase != addr)
665 return serial8250_console_setup(port, options, true);
671 static struct console univ8250_console = {
673 .write = univ8250_console_write,
674 .device = uart_console_device,
675 .setup = univ8250_console_setup,
676 .match = univ8250_console_match,
677 .flags = CON_PRINTBUFFER | CON_ANYTIME,
679 .data = &serial8250_reg,
682 static int __init univ8250_console_init(void)
687 serial8250_isa_init_ports();
688 register_console(&univ8250_console);
691 console_initcall(univ8250_console_init);
693 #define SERIAL8250_CONSOLE (&univ8250_console)
695 #define SERIAL8250_CONSOLE NULL
698 static struct uart_driver serial8250_reg = {
699 .owner = THIS_MODULE,
700 .driver_name = "serial",
704 .cons = SERIAL8250_CONSOLE,
708 * early_serial_setup - early registration for 8250 ports
710 * Setup an 8250 port structure prior to console initialisation. Use
711 * after console initialisation will cause undefined behaviour.
713 int __init early_serial_setup(struct uart_port *port)
717 if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
720 serial8250_isa_init_ports();
721 p = &serial8250_ports[port->line].port;
722 p->iobase = port->iobase;
723 p->membase = port->membase;
725 p->irqflags = port->irqflags;
726 p->uartclk = port->uartclk;
727 p->fifosize = port->fifosize;
728 p->regshift = port->regshift;
729 p->iotype = port->iotype;
730 p->flags = port->flags;
731 p->mapbase = port->mapbase;
732 p->mapsize = port->mapsize;
733 p->private_data = port->private_data;
734 p->type = port->type;
735 p->line = port->line;
737 serial8250_set_defaults(up_to_u8250p(p));
740 p->serial_in = port->serial_in;
741 if (port->serial_out)
742 p->serial_out = port->serial_out;
743 if (port->handle_irq)
744 p->handle_irq = port->handle_irq;
750 * serial8250_suspend_port - suspend one serial port
751 * @line: serial line number
753 * Suspend one serial port.
755 void serial8250_suspend_port(int line)
757 struct uart_8250_port *up = &serial8250_ports[line];
758 struct uart_port *port = &up->port;
760 if (!console_suspend_enabled && uart_console(port) &&
761 port->type != PORT_8250) {
762 unsigned char canary = 0xa5;
763 serial_out(up, UART_SCR, canary);
764 if (serial_in(up, UART_SCR) == canary)
768 uart_suspend_port(&serial8250_reg, port);
770 EXPORT_SYMBOL(serial8250_suspend_port);
773 * serial8250_resume_port - resume one serial port
774 * @line: serial line number
776 * Resume one serial port.
778 void serial8250_resume_port(int line)
780 struct uart_8250_port *up = &serial8250_ports[line];
781 struct uart_port *port = &up->port;
785 if (up->capabilities & UART_NATSEMI) {
786 /* Ensure it's still in high speed mode */
787 serial_port_out(port, UART_LCR, 0xE0);
789 ns16550a_goto_highspeed(up);
791 serial_port_out(port, UART_LCR, 0);
792 port->uartclk = 921600*16;
794 uart_resume_port(&serial8250_reg, port);
796 EXPORT_SYMBOL(serial8250_resume_port);
799 * Register a set of serial devices attached to a platform device. The
800 * list is terminated with a zero flags entry, which means we expect
801 * all entries to have at least UPF_BOOT_AUTOCONF set.
803 static int serial8250_probe(struct platform_device *dev)
805 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
806 struct uart_8250_port uart;
807 int ret, i, irqflag = 0;
809 memset(&uart, 0, sizeof(uart));
812 irqflag = IRQF_SHARED;
814 for (i = 0; p && p->flags != 0; p++, i++) {
815 uart.port.iobase = p->iobase;
816 uart.port.membase = p->membase;
817 uart.port.irq = p->irq;
818 uart.port.irqflags = p->irqflags;
819 uart.port.uartclk = p->uartclk;
820 uart.port.regshift = p->regshift;
821 uart.port.iotype = p->iotype;
822 uart.port.flags = p->flags;
823 uart.port.mapbase = p->mapbase;
824 uart.port.hub6 = p->hub6;
825 uart.port.private_data = p->private_data;
826 uart.port.type = p->type;
827 uart.port.serial_in = p->serial_in;
828 uart.port.serial_out = p->serial_out;
829 uart.port.handle_irq = p->handle_irq;
830 uart.port.handle_break = p->handle_break;
831 uart.port.set_termios = p->set_termios;
832 uart.port.get_mctrl = p->get_mctrl;
833 uart.port.pm = p->pm;
834 uart.port.dev = &dev->dev;
835 uart.port.irqflags |= irqflag;
836 ret = serial8250_register_8250_port(&uart);
838 dev_err(&dev->dev, "unable to register port at index %d "
839 "(IO%lx MEM%llx IRQ%d): %d\n", i,
840 p->iobase, (unsigned long long)p->mapbase,
848 * Remove serial ports registered against a platform device.
850 static int serial8250_remove(struct platform_device *dev)
854 for (i = 0; i < nr_uarts; i++) {
855 struct uart_8250_port *up = &serial8250_ports[i];
857 if (up->port.dev == &dev->dev)
858 serial8250_unregister_port(i);
863 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
867 for (i = 0; i < UART_NR; i++) {
868 struct uart_8250_port *up = &serial8250_ports[i];
870 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
871 uart_suspend_port(&serial8250_reg, &up->port);
877 static int serial8250_resume(struct platform_device *dev)
881 for (i = 0; i < UART_NR; i++) {
882 struct uart_8250_port *up = &serial8250_ports[i];
884 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
885 serial8250_resume_port(i);
891 static struct platform_driver serial8250_isa_driver = {
892 .probe = serial8250_probe,
893 .remove = serial8250_remove,
894 .suspend = serial8250_suspend,
895 .resume = serial8250_resume,
897 .name = "serial8250",
902 * This "device" covers _all_ ISA 8250-compatible serial devices listed
903 * in the table in include/asm/serial.h
905 static struct platform_device *serial8250_isa_devs;
908 * serial8250_register_8250_port and serial8250_unregister_port allows for
909 * 16x50 serial ports to be configured at run-time, to support PCMCIA
910 * modems and PCI multiport cards.
912 static DEFINE_MUTEX(serial_mutex);
914 static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
919 * First, find a port entry which matches.
921 for (i = 0; i < nr_uarts; i++)
922 if (uart_match_port(&serial8250_ports[i].port, port))
923 return &serial8250_ports[i];
925 /* try line number first if still available */
927 if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
928 serial8250_ports[i].port.iobase == 0)
929 return &serial8250_ports[i];
931 * We didn't find a matching entry, so look for the first
932 * free entry. We look for one which hasn't been previously
933 * used (indicated by zero iobase).
935 for (i = 0; i < nr_uarts; i++)
936 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
937 serial8250_ports[i].port.iobase == 0)
938 return &serial8250_ports[i];
941 * That also failed. Last resort is to find any entry which
942 * doesn't have a real port associated with it.
944 for (i = 0; i < nr_uarts; i++)
945 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
946 return &serial8250_ports[i];
952 * serial8250_register_8250_port - register a serial port
953 * @up: serial port template
955 * Configure the serial port specified by the request. If the
956 * port exists and is in use, it is hung up and unregistered
959 * The port is then probed and if necessary the IRQ is autodetected
960 * If this fails an error is returned.
962 * On success the port is ready to use and the line number is returned.
964 int serial8250_register_8250_port(struct uart_8250_port *up)
966 struct uart_8250_port *uart;
969 if (up->port.uartclk == 0)
972 mutex_lock(&serial_mutex);
974 uart = serial8250_find_match_or_unused(&up->port);
975 if (uart && uart->port.type != PORT_8250_CIR) {
977 uart_remove_one_port(&serial8250_reg, &uart->port);
979 uart->port.iobase = up->port.iobase;
980 uart->port.membase = up->port.membase;
981 uart->port.irq = up->port.irq;
982 uart->port.irqflags = up->port.irqflags;
983 uart->port.uartclk = up->port.uartclk;
984 uart->port.fifosize = up->port.fifosize;
985 uart->port.regshift = up->port.regshift;
986 uart->port.iotype = up->port.iotype;
987 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
988 uart->bugs = up->bugs;
989 uart->port.mapbase = up->port.mapbase;
990 uart->port.mapsize = up->port.mapsize;
991 uart->port.private_data = up->port.private_data;
992 uart->tx_loadsz = up->tx_loadsz;
993 uart->capabilities = up->capabilities;
994 uart->port.throttle = up->port.throttle;
995 uart->port.unthrottle = up->port.unthrottle;
996 uart->port.rs485_config = up->port.rs485_config;
997 uart->port.rs485 = up->port.rs485;
1000 /* Take tx_loadsz from fifosize if it wasn't set separately */
1001 if (uart->port.fifosize && !uart->tx_loadsz)
1002 uart->tx_loadsz = uart->port.fifosize;
1005 uart->port.dev = up->port.dev;
1008 uart->port.flags |= UPF_NO_TXEN_TEST;
1010 if (up->port.flags & UPF_FIXED_TYPE)
1011 uart->port.type = up->port.type;
1013 serial8250_set_defaults(uart);
1015 /* Possibly override default I/O functions. */
1016 if (up->port.serial_in)
1017 uart->port.serial_in = up->port.serial_in;
1018 if (up->port.serial_out)
1019 uart->port.serial_out = up->port.serial_out;
1020 if (up->port.handle_irq)
1021 uart->port.handle_irq = up->port.handle_irq;
1022 /* Possibly override set_termios call */
1023 if (up->port.set_termios)
1024 uart->port.set_termios = up->port.set_termios;
1025 if (up->port.get_mctrl)
1026 uart->port.get_mctrl = up->port.get_mctrl;
1027 if (up->port.set_mctrl)
1028 uart->port.set_mctrl = up->port.set_mctrl;
1029 if (up->port.startup)
1030 uart->port.startup = up->port.startup;
1031 if (up->port.shutdown)
1032 uart->port.shutdown = up->port.shutdown;
1034 uart->port.pm = up->port.pm;
1035 if (up->port.handle_break)
1036 uart->port.handle_break = up->port.handle_break;
1038 uart->dl_read = up->dl_read;
1040 uart->dl_write = up->dl_write;
1042 if (uart->port.type != PORT_8250_CIR) {
1043 if (serial8250_isa_config != NULL)
1044 serial8250_isa_config(0, &uart->port,
1045 &uart->capabilities);
1047 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);
1068 uart->port.dev = NULL;
1069 mutex_unlock(&serial_mutex);
1072 EXPORT_SYMBOL(serial8250_register_8250_port);
1075 * serial8250_unregister_port - remove a 16x50 serial port at runtime
1076 * @line: serial line number
1078 * Remove one serial port. This may not be called from interrupt
1079 * context. We hand the port back to the our control.
1081 void serial8250_unregister_port(int line)
1083 struct uart_8250_port *uart = &serial8250_ports[line];
1085 mutex_lock(&serial_mutex);
1088 unsigned long flags;
1090 spin_lock_irqsave(&uart->port.lock, flags);
1091 serial8250_em485_destroy(uart);
1092 spin_unlock_irqrestore(&uart->port.lock, flags);
1095 uart_remove_one_port(&serial8250_reg, &uart->port);
1096 if (serial8250_isa_devs) {
1097 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
1099 uart->port.flags |= UPF_NO_TXEN_TEST;
1100 uart->port.type = PORT_UNKNOWN;
1101 uart->port.dev = &serial8250_isa_devs->dev;
1102 uart->capabilities = 0;
1103 uart_add_one_port(&serial8250_reg, &uart->port);
1105 uart->port.dev = NULL;
1107 mutex_unlock(&serial_mutex);
1109 EXPORT_SYMBOL(serial8250_unregister_port);
1111 static int __init serial8250_init(void)
1118 serial8250_isa_init_ports();
1120 pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %sabled\n",
1121 nr_uarts, share_irqs ? "en" : "dis");
1124 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
1126 serial8250_reg.nr = UART_NR;
1127 ret = uart_register_driver(&serial8250_reg);
1132 ret = serial8250_pnp_init();
1134 goto unreg_uart_drv;
1136 serial8250_isa_devs = platform_device_alloc("serial8250",
1137 PLAT8250_DEV_LEGACY);
1138 if (!serial8250_isa_devs) {
1143 ret = platform_device_add(serial8250_isa_devs);
1147 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
1149 ret = platform_driver_register(&serial8250_isa_driver);
1153 platform_device_del(serial8250_isa_devs);
1155 platform_device_put(serial8250_isa_devs);
1157 serial8250_pnp_exit();
1160 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1162 uart_unregister_driver(&serial8250_reg);
1168 static void __exit serial8250_exit(void)
1170 struct platform_device *isa_dev = serial8250_isa_devs;
1173 * This tells serial8250_unregister_port() not to re-register
1174 * the ports (thereby making serial8250_isa_driver permanently
1177 serial8250_isa_devs = NULL;
1179 platform_driver_unregister(&serial8250_isa_driver);
1180 platform_device_unregister(isa_dev);
1182 serial8250_pnp_exit();
1185 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1187 uart_unregister_driver(&serial8250_reg);
1191 module_init(serial8250_init);
1192 module_exit(serial8250_exit);
1194 MODULE_LICENSE("GPL");
1195 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
1197 module_param(share_irqs, uint, 0644);
1198 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
1200 module_param(nr_uarts, uint, 0644);
1201 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
1203 module_param(skip_txen_test, uint, 0644);
1204 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
1206 #ifdef CONFIG_SERIAL_8250_RSA
1207 module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
1208 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
1210 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
1212 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
1214 /* This module was renamed to 8250_core in 3.7. Keep the old "8250" name
1215 * working as well for the module options so we don't break people. We
1216 * need to keep the names identical and the convenient macros will happily
1217 * refuse to let us do that by failing the build with redefinition errors
1218 * of global variables. So we stick them inside a dummy function to avoid
1219 * those conflicts. The options still get parsed, and the redefined
1220 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
1222 * This is hacky. I'm sorry.
1224 static void __used s8250_options(void)
1226 #undef MODULE_PARAM_PREFIX
1227 #define MODULE_PARAM_PREFIX "8250_core."
1229 module_param_cb(share_irqs, ¶m_ops_uint, &share_irqs, 0644);
1230 module_param_cb(nr_uarts, ¶m_ops_uint, &nr_uarts, 0644);
1231 module_param_cb(skip_txen_test, ¶m_ops_uint, &skip_txen_test, 0644);
1232 #ifdef CONFIG_SERIAL_8250_RSA
1233 __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
1234 ¶m_array_ops, .arr = &__param_arr_probe_rsa,
1239 MODULE_ALIAS("8250_core");