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 */
62 * On -rt we can have a more delays, and legitimately
63 * so - so don't drop work spuriously and spam the
66 #ifdef CONFIG_PREEMPT_RT_FULL
67 # define PASS_LIMIT 1000000
69 # define PASS_LIMIT 512
72 #include <asm/serial.h>
74 * SERIAL_PORT_DFNS tells us about built-in ports that have no
75 * standard enumeration mechanism. Platforms that can find all
76 * serial ports via mechanisms like ACPI or PCI need not supply it.
78 #ifndef SERIAL_PORT_DFNS
79 #define SERIAL_PORT_DFNS
82 static const struct old_serial_port old_serial_port[] = {
83 SERIAL_PORT_DFNS /* defined in asm/serial.h */
86 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
88 #ifdef CONFIG_SERIAL_8250_RSA
90 #define PORT_RSA_MAX 4
91 static unsigned long probe_rsa[PORT_RSA_MAX];
92 static unsigned int probe_rsa_count;
93 #endif /* CONFIG_SERIAL_8250_RSA */
96 struct hlist_node node;
98 spinlock_t lock; /* Protects list not the hash */
99 struct list_head *head;
102 #define NR_IRQ_HASH 32 /* Can be adjusted later */
103 static struct hlist_head irq_lists[NR_IRQ_HASH];
104 static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
107 * This is the serial driver's interrupt routine.
109 * Arjan thinks the old way was overly complex, so it got simplified.
110 * Alan disagrees, saying that need the complexity to handle the weird
111 * nature of ISA shared interrupts. (This is a special exception.)
113 * In order to handle ISA shared interrupts properly, we need to check
114 * that all ports have been serviced, and therefore the ISA interrupt
115 * line has been de-asserted.
117 * This means we need to loop through all ports. checking that they
118 * don't have an interrupt pending.
120 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
122 struct irq_info *i = dev_id;
123 struct list_head *l, *end = NULL;
124 int pass_counter = 0, handled = 0;
126 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
132 struct uart_8250_port *up;
133 struct uart_port *port;
135 up = list_entry(l, struct uart_8250_port, list);
138 if (port->handle_irq(port)) {
141 } else if (end == NULL)
146 if (l == i->head && pass_counter++ > PASS_LIMIT) {
147 /* If we hit this, we're dead. */
148 printk_ratelimited(KERN_ERR
149 "serial8250: too much work for irq%d\n", irq);
154 spin_unlock(&i->lock);
156 DEBUG_INTR("end.\n");
158 return IRQ_RETVAL(handled);
162 * To support ISA shared interrupts, we need to have one interrupt
163 * handler that ensures that the IRQ line has been deasserted
164 * before returning. Failing to do this will result in the IRQ
165 * line being stuck active, and, since ISA irqs are edge triggered,
166 * no more IRQs will be seen.
168 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
170 spin_lock_irq(&i->lock);
172 if (!list_empty(i->head)) {
173 if (i->head == &up->list)
174 i->head = i->head->next;
177 BUG_ON(i->head != &up->list);
180 spin_unlock_irq(&i->lock);
181 /* List empty so throw away the hash node */
182 if (i->head == NULL) {
188 static int serial_link_irq_chain(struct uart_8250_port *up)
190 struct hlist_head *h;
191 struct hlist_node *n;
193 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
195 mutex_lock(&hash_mutex);
197 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
199 hlist_for_each(n, h) {
200 i = hlist_entry(n, struct irq_info, node);
201 if (i->irq == up->port.irq)
206 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
208 mutex_unlock(&hash_mutex);
211 spin_lock_init(&i->lock);
212 i->irq = up->port.irq;
213 hlist_add_head(&i->node, h);
215 mutex_unlock(&hash_mutex);
217 spin_lock_irq(&i->lock);
220 list_add(&up->list, i->head);
221 spin_unlock_irq(&i->lock);
225 INIT_LIST_HEAD(&up->list);
227 spin_unlock_irq(&i->lock);
228 irq_flags |= up->port.irqflags;
229 ret = request_irq(up->port.irq, serial8250_interrupt,
230 irq_flags, "serial", i);
232 serial_do_unlink(i, up);
238 static void serial_unlink_irq_chain(struct uart_8250_port *up)
241 * yes, some broken gcc emit "warning: 'i' may be used uninitialized"
242 * but no, we are not going to take a patch that assigns NULL below.
245 struct hlist_node *n;
246 struct hlist_head *h;
248 mutex_lock(&hash_mutex);
250 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
252 hlist_for_each(n, h) {
253 i = hlist_entry(n, struct irq_info, node);
254 if (i->irq == up->port.irq)
259 BUG_ON(i->head == NULL);
261 if (list_empty(i->head))
262 free_irq(up->port.irq, i);
264 serial_do_unlink(i, up);
265 mutex_unlock(&hash_mutex);
269 * This function is used to handle ports that do not have an
270 * interrupt. This doesn't work very well for 16450's, but gives
271 * barely passable results for a 16550A. (Although at the expense
272 * of much CPU overhead).
274 static void serial8250_timeout(unsigned long data)
276 struct uart_8250_port *up = (struct uart_8250_port *)data;
278 up->port.handle_irq(&up->port);
279 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
282 static void serial8250_backup_timeout(unsigned long data)
284 struct uart_8250_port *up = (struct uart_8250_port *)data;
285 unsigned int iir, ier = 0, lsr;
288 spin_lock_irqsave(&up->port.lock, flags);
291 * Must disable interrupts or else we risk racing with the interrupt
295 ier = serial_in(up, UART_IER);
296 serial_out(up, UART_IER, 0);
299 iir = serial_in(up, UART_IIR);
302 * This should be a safe test for anyone who doesn't trust the
303 * IIR bits on their UART, but it's specifically designed for
304 * the "Diva" UART used on the management processor on many HP
305 * ia64 and parisc boxes.
307 lsr = serial_in(up, UART_LSR);
308 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
309 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
310 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
311 (lsr & UART_LSR_THRE)) {
312 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
313 iir |= UART_IIR_THRI;
316 if (!(iir & UART_IIR_NO_INT))
317 serial8250_tx_chars(up);
320 serial_out(up, UART_IER, ier);
322 spin_unlock_irqrestore(&up->port.lock, flags);
324 /* Standard timer interval plus 0.2s to keep the port running */
325 mod_timer(&up->timer,
326 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
329 static int univ8250_setup_irq(struct uart_8250_port *up)
331 struct uart_port *port = &up->port;
335 * The above check will only give an accurate result the first time
336 * the port is opened so this value needs to be preserved.
338 if (up->bugs & UART_BUG_THRE) {
339 pr_debug("ttyS%d - using backup timer\n", serial_index(port));
341 up->timer.function = serial8250_backup_timeout;
342 up->timer.data = (unsigned long)up;
343 mod_timer(&up->timer, jiffies +
344 uart_poll_timeout(port) + HZ / 5);
348 * If the "interrupt" for this port doesn't correspond with any
349 * hardware interrupt, we use a timer-based system. The original
350 * driver used to do this with IRQ0.
353 up->timer.data = (unsigned long)up;
354 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
356 retval = serial_link_irq_chain(up);
361 static void univ8250_release_irq(struct uart_8250_port *up)
363 struct uart_port *port = &up->port;
365 del_timer_sync(&up->timer);
366 up->timer.function = serial8250_timeout;
368 serial_unlink_irq_chain(up);
371 #ifdef CONFIG_SERIAL_8250_RSA
372 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
374 unsigned long start = UART_RSA_BASE << up->port.regshift;
375 unsigned int size = 8 << up->port.regshift;
376 struct uart_port *port = &up->port;
379 switch (port->iotype) {
382 start += port->iobase;
383 if (request_region(start, size, "serial-rsa"))
393 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
395 unsigned long offset = UART_RSA_BASE << up->port.regshift;
396 unsigned int size = 8 << up->port.regshift;
397 struct uart_port *port = &up->port;
399 switch (port->iotype) {
402 release_region(port->iobase + offset, size);
408 static const struct uart_ops *base_ops;
409 static struct uart_ops univ8250_port_ops;
411 static const struct uart_8250_ops univ8250_driver_ops = {
412 .setup_irq = univ8250_setup_irq,
413 .release_irq = univ8250_release_irq,
416 static struct uart_8250_port serial8250_ports[UART_NR];
419 * serial8250_get_port - retrieve struct uart_8250_port
420 * @line: serial line number
422 * This function retrieves struct uart_8250_port for the specific line.
423 * This struct *must* *not* be used to perform a 8250 or serial core operation
424 * which is not accessible otherwise. Its only purpose is to make the struct
425 * accessible to the runtime-pm callbacks for context suspend/restore.
426 * The lock assumption made here is none because runtime-pm suspend/resume
427 * callbacks should not be invoked if there is any operation performed on the
430 struct uart_8250_port *serial8250_get_port(int line)
432 return &serial8250_ports[line];
434 EXPORT_SYMBOL_GPL(serial8250_get_port);
436 static void (*serial8250_isa_config)(int port, struct uart_port *up,
437 unsigned short *capabilities);
439 void serial8250_set_isa_configurator(
440 void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
442 serial8250_isa_config = v;
444 EXPORT_SYMBOL(serial8250_set_isa_configurator);
446 #ifdef CONFIG_SERIAL_8250_RSA
448 static void univ8250_config_port(struct uart_port *port, int flags)
450 struct uart_8250_port *up = up_to_u8250p(port);
452 up->probe &= ~UART_PROBE_RSA;
453 if (port->type == PORT_RSA) {
454 if (serial8250_request_rsa_resource(up) == 0)
455 up->probe |= UART_PROBE_RSA;
456 } else if (flags & UART_CONFIG_TYPE) {
459 for (i = 0; i < probe_rsa_count; i++) {
460 if (probe_rsa[i] == up->port.iobase) {
461 if (serial8250_request_rsa_resource(up) == 0)
462 up->probe |= UART_PROBE_RSA;
468 base_ops->config_port(port, flags);
470 if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
471 serial8250_release_rsa_resource(up);
474 static int univ8250_request_port(struct uart_port *port)
476 struct uart_8250_port *up = up_to_u8250p(port);
479 ret = base_ops->request_port(port);
480 if (ret == 0 && port->type == PORT_RSA) {
481 ret = serial8250_request_rsa_resource(up);
483 base_ops->release_port(port);
489 static void univ8250_release_port(struct uart_port *port)
491 struct uart_8250_port *up = up_to_u8250p(port);
493 if (port->type == PORT_RSA)
494 serial8250_release_rsa_resource(up);
495 base_ops->release_port(port);
498 static void univ8250_rsa_support(struct uart_ops *ops)
500 ops->config_port = univ8250_config_port;
501 ops->request_port = univ8250_request_port;
502 ops->release_port = univ8250_release_port;
506 #define univ8250_rsa_support(x) do { } while (0)
507 #endif /* CONFIG_SERIAL_8250_RSA */
509 static void __init serial8250_isa_init_ports(void)
511 struct uart_8250_port *up;
512 static int first = 1;
519 if (nr_uarts > UART_NR)
522 for (i = 0; i < nr_uarts; i++) {
523 struct uart_8250_port *up = &serial8250_ports[i];
524 struct uart_port *port = &up->port;
527 serial8250_init_port(up);
529 base_ops = port->ops;
530 port->ops = &univ8250_port_ops;
532 init_timer(&up->timer);
533 up->timer.function = serial8250_timeout;
535 up->ops = &univ8250_driver_ops;
538 * ALPHA_KLUDGE_MCR needs to be killed.
540 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
541 up->mcr_force = ALPHA_KLUDGE_MCR;
544 /* chain base port ops to support Remote Supervisor Adapter */
545 univ8250_port_ops = *base_ops;
546 univ8250_rsa_support(&univ8250_port_ops);
549 irqflag = IRQF_SHARED;
551 for (i = 0, up = serial8250_ports;
552 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
554 struct uart_port *port = &up->port;
556 port->iobase = old_serial_port[i].port;
557 port->irq = irq_canonicalize(old_serial_port[i].irq);
558 port->irqflags = old_serial_port[i].irqflags;
559 port->uartclk = old_serial_port[i].baud_base * 16;
560 port->flags = old_serial_port[i].flags;
561 port->hub6 = old_serial_port[i].hub6;
562 port->membase = old_serial_port[i].iomem_base;
563 port->iotype = old_serial_port[i].io_type;
564 port->regshift = old_serial_port[i].iomem_reg_shift;
565 serial8250_set_defaults(up);
567 port->irqflags |= irqflag;
568 if (serial8250_isa_config != NULL)
569 serial8250_isa_config(i, &up->port, &up->capabilities);
574 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
578 for (i = 0; i < nr_uarts; i++) {
579 struct uart_8250_port *up = &serial8250_ports[i];
581 if (up->port.type == PORT_8250_CIR)
590 up->port.flags |= UPF_NO_TXEN_TEST;
592 uart_add_one_port(drv, &up->port);
596 #ifdef CONFIG_SERIAL_8250_CONSOLE
598 static void univ8250_console_write(struct console *co, const char *s,
601 struct uart_8250_port *up = &serial8250_ports[co->index];
603 serial8250_console_write(up, s, count);
606 static int univ8250_console_setup(struct console *co, char *options)
608 struct uart_port *port;
611 * Check whether an invalid uart number has been specified, and
612 * if so, search for the first available port that does have
615 if (co->index >= nr_uarts)
617 port = &serial8250_ports[co->index].port;
618 /* link port to console */
621 return serial8250_console_setup(port, options, false);
625 * univ8250_console_match - non-standard console matching
626 * @co: registering console
627 * @name: name from console command line
628 * @idx: index from console command line
629 * @options: ptr to option string from console command line
631 * Only attempts to match console command lines of the form:
632 * console=uart[8250],io|mmio|mmio32,<addr>[,<options>]
633 * console=uart[8250],0x<addr>[,<options>]
634 * This form is used to register an initial earlycon boot console and
635 * replace it with the serial8250_console at 8250 driver init.
637 * Performs console setup for a match (as required by interface)
638 * If no <options> are specified, then assume the h/w is already setup.
640 * Returns 0 if console matches; otherwise non-zero to use default matching
642 static int univ8250_console_match(struct console *co, char *name, int idx,
645 char match[] = "uart"; /* 8250-specific earlycon name */
646 unsigned char iotype;
650 if (strncmp(name, match, 4) != 0)
653 if (uart_parse_earlycon(options, &iotype, &addr, &options))
656 /* try to match the port specified on the command line */
657 for (i = 0; i < nr_uarts; i++) {
658 struct uart_port *port = &serial8250_ports[i].port;
660 if (port->iotype != iotype)
662 if ((iotype == UPIO_MEM || iotype == UPIO_MEM32) &&
663 (port->mapbase != addr))
665 if (iotype == UPIO_PORT && port->iobase != addr)
670 return serial8250_console_setup(port, options, true);
676 static struct console univ8250_console = {
678 .write = univ8250_console_write,
679 .device = uart_console_device,
680 .setup = univ8250_console_setup,
681 .match = univ8250_console_match,
682 .flags = CON_PRINTBUFFER | CON_ANYTIME,
684 .data = &serial8250_reg,
687 static int __init univ8250_console_init(void)
692 serial8250_isa_init_ports();
693 register_console(&univ8250_console);
696 console_initcall(univ8250_console_init);
698 #define SERIAL8250_CONSOLE &univ8250_console
700 #define SERIAL8250_CONSOLE NULL
703 static struct uart_driver serial8250_reg = {
704 .owner = THIS_MODULE,
705 .driver_name = "serial",
709 .cons = SERIAL8250_CONSOLE,
713 * early_serial_setup - early registration for 8250 ports
715 * Setup an 8250 port structure prior to console initialisation. Use
716 * after console initialisation will cause undefined behaviour.
718 int __init early_serial_setup(struct uart_port *port)
722 if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
725 serial8250_isa_init_ports();
726 p = &serial8250_ports[port->line].port;
727 p->iobase = port->iobase;
728 p->membase = port->membase;
730 p->irqflags = port->irqflags;
731 p->uartclk = port->uartclk;
732 p->fifosize = port->fifosize;
733 p->regshift = port->regshift;
734 p->iotype = port->iotype;
735 p->flags = port->flags;
736 p->mapbase = port->mapbase;
737 p->mapsize = port->mapsize;
738 p->private_data = port->private_data;
739 p->type = port->type;
740 p->line = port->line;
742 serial8250_set_defaults(up_to_u8250p(p));
745 p->serial_in = port->serial_in;
746 if (port->serial_out)
747 p->serial_out = port->serial_out;
748 if (port->handle_irq)
749 p->handle_irq = port->handle_irq;
755 * serial8250_suspend_port - suspend one serial port
756 * @line: serial line number
758 * Suspend one serial port.
760 void serial8250_suspend_port(int line)
762 struct uart_8250_port *up = &serial8250_ports[line];
763 struct uart_port *port = &up->port;
765 if (!console_suspend_enabled && uart_console(port) &&
766 port->type != PORT_8250) {
767 unsigned char canary = 0xa5;
768 serial_out(up, UART_SCR, canary);
769 if (serial_in(up, UART_SCR) == canary)
773 uart_suspend_port(&serial8250_reg, port);
777 * serial8250_resume_port - resume one serial port
778 * @line: serial line number
780 * Resume one serial port.
782 void serial8250_resume_port(int line)
784 struct uart_8250_port *up = &serial8250_ports[line];
785 struct uart_port *port = &up->port;
789 if (up->capabilities & UART_NATSEMI) {
790 /* Ensure it's still in high speed mode */
791 serial_port_out(port, UART_LCR, 0xE0);
793 ns16550a_goto_highspeed(up);
795 serial_port_out(port, UART_LCR, 0);
796 port->uartclk = 921600*16;
798 uart_resume_port(&serial8250_reg, port);
802 * Register a set of serial devices attached to a platform device. The
803 * list is terminated with a zero flags entry, which means we expect
804 * all entries to have at least UPF_BOOT_AUTOCONF set.
806 static int serial8250_probe(struct platform_device *dev)
808 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
809 struct uart_8250_port uart;
810 int ret, i, irqflag = 0;
812 memset(&uart, 0, sizeof(uart));
815 irqflag = IRQF_SHARED;
817 for (i = 0; p && p->flags != 0; p++, i++) {
818 uart.port.iobase = p->iobase;
819 uart.port.membase = p->membase;
820 uart.port.irq = p->irq;
821 uart.port.irqflags = p->irqflags;
822 uart.port.uartclk = p->uartclk;
823 uart.port.regshift = p->regshift;
824 uart.port.iotype = p->iotype;
825 uart.port.flags = p->flags;
826 uart.port.mapbase = p->mapbase;
827 uart.port.hub6 = p->hub6;
828 uart.port.private_data = p->private_data;
829 uart.port.type = p->type;
830 uart.port.serial_in = p->serial_in;
831 uart.port.serial_out = p->serial_out;
832 uart.port.handle_irq = p->handle_irq;
833 uart.port.handle_break = p->handle_break;
834 uart.port.set_termios = p->set_termios;
835 uart.port.pm = p->pm;
836 uart.port.dev = &dev->dev;
837 uart.port.irqflags |= irqflag;
838 ret = serial8250_register_8250_port(&uart);
840 dev_err(&dev->dev, "unable to register port at index %d "
841 "(IO%lx MEM%llx IRQ%d): %d\n", i,
842 p->iobase, (unsigned long long)p->mapbase,
850 * Remove serial ports registered against a platform device.
852 static int serial8250_remove(struct platform_device *dev)
856 for (i = 0; i < nr_uarts; i++) {
857 struct uart_8250_port *up = &serial8250_ports[i];
859 if (up->port.dev == &dev->dev)
860 serial8250_unregister_port(i);
865 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
869 for (i = 0; i < UART_NR; i++) {
870 struct uart_8250_port *up = &serial8250_ports[i];
872 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
873 uart_suspend_port(&serial8250_reg, &up->port);
879 static int serial8250_resume(struct platform_device *dev)
883 for (i = 0; i < UART_NR; i++) {
884 struct uart_8250_port *up = &serial8250_ports[i];
886 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
887 serial8250_resume_port(i);
893 static struct platform_driver serial8250_isa_driver = {
894 .probe = serial8250_probe,
895 .remove = serial8250_remove,
896 .suspend = serial8250_suspend,
897 .resume = serial8250_resume,
899 .name = "serial8250",
904 * This "device" covers _all_ ISA 8250-compatible serial devices listed
905 * in the table in include/asm/serial.h
907 static struct platform_device *serial8250_isa_devs;
910 * serial8250_register_8250_port and serial8250_unregister_port allows for
911 * 16x50 serial ports to be configured at run-time, to support PCMCIA
912 * modems and PCI multiport cards.
914 static DEFINE_MUTEX(serial_mutex);
916 static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
921 * First, find a port entry which matches.
923 for (i = 0; i < nr_uarts; i++)
924 if (uart_match_port(&serial8250_ports[i].port, port))
925 return &serial8250_ports[i];
927 /* try line number first if still available */
929 if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
930 serial8250_ports[i].port.iobase == 0)
931 return &serial8250_ports[i];
933 * We didn't find a matching entry, so look for the first
934 * free entry. We look for one which hasn't been previously
935 * used (indicated by zero iobase).
937 for (i = 0; i < nr_uarts; i++)
938 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
939 serial8250_ports[i].port.iobase == 0)
940 return &serial8250_ports[i];
943 * That also failed. Last resort is to find any entry which
944 * doesn't have a real port associated with it.
946 for (i = 0; i < nr_uarts; i++)
947 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
948 return &serial8250_ports[i];
954 * serial8250_register_8250_port - register a serial port
955 * @up: serial port template
957 * Configure the serial port specified by the request. If the
958 * port exists and is in use, it is hung up and unregistered
961 * The port is then probed and if necessary the IRQ is autodetected
962 * If this fails an error is returned.
964 * On success the port is ready to use and the line number is returned.
966 int serial8250_register_8250_port(struct uart_8250_port *up)
968 struct uart_8250_port *uart;
971 if (up->port.uartclk == 0)
974 mutex_lock(&serial_mutex);
976 uart = serial8250_find_match_or_unused(&up->port);
977 if (uart && uart->port.type != PORT_8250_CIR) {
979 uart_remove_one_port(&serial8250_reg, &uart->port);
981 uart->port.iobase = up->port.iobase;
982 uart->port.membase = up->port.membase;
983 uart->port.irq = up->port.irq;
984 uart->port.irqflags = up->port.irqflags;
985 uart->port.uartclk = up->port.uartclk;
986 uart->port.fifosize = up->port.fifosize;
987 uart->port.regshift = up->port.regshift;
988 uart->port.iotype = up->port.iotype;
989 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
990 uart->bugs = up->bugs;
991 uart->port.mapbase = up->port.mapbase;
992 uart->port.mapsize = up->port.mapsize;
993 uart->port.private_data = up->port.private_data;
994 uart->tx_loadsz = up->tx_loadsz;
995 uart->capabilities = up->capabilities;
996 uart->port.throttle = up->port.throttle;
997 uart->port.unthrottle = up->port.unthrottle;
998 uart->port.rs485_config = up->port.rs485_config;
999 uart->port.rs485 = up->port.rs485;
1000 uart->dma = up->dma;
1002 /* Take tx_loadsz from fifosize if it wasn't set separately */
1003 if (uart->port.fifosize && !uart->tx_loadsz)
1004 uart->tx_loadsz = uart->port.fifosize;
1007 uart->port.dev = up->port.dev;
1010 uart->port.flags |= UPF_NO_TXEN_TEST;
1012 if (up->port.flags & UPF_FIXED_TYPE)
1013 uart->port.type = up->port.type;
1015 serial8250_set_defaults(uart);
1017 /* Possibly override default I/O functions. */
1018 if (up->port.serial_in)
1019 uart->port.serial_in = up->port.serial_in;
1020 if (up->port.serial_out)
1021 uart->port.serial_out = up->port.serial_out;
1022 if (up->port.handle_irq)
1023 uart->port.handle_irq = up->port.handle_irq;
1024 /* Possibly override set_termios call */
1025 if (up->port.set_termios)
1026 uart->port.set_termios = up->port.set_termios;
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,
1050 ret = uart->port.line;
1052 dev_info(uart->port.dev,
1053 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
1055 (unsigned long long)uart->port.mapbase,
1061 mutex_unlock(&serial_mutex);
1065 EXPORT_SYMBOL(serial8250_register_8250_port);
1068 * serial8250_unregister_port - remove a 16x50 serial port at runtime
1069 * @line: serial line number
1071 * Remove one serial port. This may not be called from interrupt
1072 * context. We hand the port back to the our control.
1074 void serial8250_unregister_port(int line)
1076 struct uart_8250_port *uart = &serial8250_ports[line];
1078 mutex_lock(&serial_mutex);
1079 uart_remove_one_port(&serial8250_reg, &uart->port);
1080 if (serial8250_isa_devs) {
1081 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
1083 uart->port.flags |= UPF_NO_TXEN_TEST;
1084 uart->port.type = PORT_UNKNOWN;
1085 uart->port.dev = &serial8250_isa_devs->dev;
1086 uart->capabilities = 0;
1087 uart_add_one_port(&serial8250_reg, &uart->port);
1089 uart->port.dev = NULL;
1091 mutex_unlock(&serial_mutex);
1093 EXPORT_SYMBOL(serial8250_unregister_port);
1095 static int __init serial8250_init(void)
1102 serial8250_isa_init_ports();
1104 printk(KERN_INFO "Serial: 8250/16550 driver, "
1105 "%d ports, IRQ sharing %sabled\n", nr_uarts,
1106 share_irqs ? "en" : "dis");
1109 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
1111 serial8250_reg.nr = UART_NR;
1112 ret = uart_register_driver(&serial8250_reg);
1117 ret = serial8250_pnp_init();
1119 goto unreg_uart_drv;
1121 serial8250_isa_devs = platform_device_alloc("serial8250",
1122 PLAT8250_DEV_LEGACY);
1123 if (!serial8250_isa_devs) {
1128 ret = platform_device_add(serial8250_isa_devs);
1132 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
1134 ret = platform_driver_register(&serial8250_isa_driver);
1138 platform_device_del(serial8250_isa_devs);
1140 platform_device_put(serial8250_isa_devs);
1142 serial8250_pnp_exit();
1145 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1147 uart_unregister_driver(&serial8250_reg);
1153 static void __exit serial8250_exit(void)
1155 struct platform_device *isa_dev = serial8250_isa_devs;
1158 * This tells serial8250_unregister_port() not to re-register
1159 * the ports (thereby making serial8250_isa_driver permanently
1162 serial8250_isa_devs = NULL;
1164 platform_driver_unregister(&serial8250_isa_driver);
1165 platform_device_unregister(isa_dev);
1167 serial8250_pnp_exit();
1170 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1172 uart_unregister_driver(&serial8250_reg);
1176 module_init(serial8250_init);
1177 module_exit(serial8250_exit);
1179 EXPORT_SYMBOL(serial8250_suspend_port);
1180 EXPORT_SYMBOL(serial8250_resume_port);
1182 MODULE_LICENSE("GPL");
1183 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
1185 module_param(share_irqs, uint, 0644);
1186 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
1189 module_param(nr_uarts, uint, 0644);
1190 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
1192 module_param(skip_txen_test, uint, 0644);
1193 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
1195 #ifdef CONFIG_SERIAL_8250_RSA
1196 module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
1197 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
1199 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
1201 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
1203 /* This module was renamed to 8250_core in 3.7. Keep the old "8250" name
1204 * working as well for the module options so we don't break people. We
1205 * need to keep the names identical and the convenient macros will happily
1206 * refuse to let us do that by failing the build with redefinition errors
1207 * of global variables. So we stick them inside a dummy function to avoid
1208 * those conflicts. The options still get parsed, and the redefined
1209 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
1211 * This is hacky. I'm sorry.
1213 static void __used s8250_options(void)
1215 #undef MODULE_PARAM_PREFIX
1216 #define MODULE_PARAM_PREFIX "8250_core."
1218 module_param_cb(share_irqs, ¶m_ops_uint, &share_irqs, 0644);
1219 module_param_cb(nr_uarts, ¶m_ops_uint, &nr_uarts, 0644);
1220 module_param_cb(skip_txen_test, ¶m_ops_uint, &skip_txen_test, 0644);
1221 #ifdef CONFIG_SERIAL_8250_RSA
1222 __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
1223 ¶m_array_ops, .arr = &__param_arr_probe_rsa,
1228 MODULE_ALIAS("8250_core");