treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / tty / serial / 8250 / 8250_core.c
blob0894a22fd70280eee749cb14f2101c51b051f9c8
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Universal/legacy driver for 8250/16550-type serial ports
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 * Copyright (C) 2001 Russell King.
9 * Supports: ISA-compatible 8250/16550 ports
10 * PNP 8250/16550 ports
11 * early_serial_setup() ports
12 * userspace-configurable "phantom" ports
13 * "serial8250" platform devices
14 * serial8250_register_8250_port() ports
17 #include <linux/acpi.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/ioport.h>
21 #include <linux/init.h>
22 #include <linux/console.h>
23 #include <linux/sysrq.h>
24 #include <linux/delay.h>
25 #include <linux/platform_device.h>
26 #include <linux/tty.h>
27 #include <linux/ratelimit.h>
28 #include <linux/tty_flip.h>
29 #include <linux/serial.h>
30 #include <linux/serial_8250.h>
31 #include <linux/nmi.h>
32 #include <linux/mutex.h>
33 #include <linux/slab.h>
34 #include <linux/uaccess.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/io.h>
37 #ifdef CONFIG_SPARC
38 #include <linux/sunserialcore.h>
39 #endif
41 #include <asm/irq.h>
43 #include "8250.h"
46 * Configuration:
47 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
48 * is unsafe when used on edge-triggered interrupts.
50 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
52 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
54 static struct uart_driver serial8250_reg;
56 static unsigned int skip_txen_test; /* force skip of txen test at init time */
58 #define PASS_LIMIT 512
60 #include <asm/serial.h>
62 * SERIAL_PORT_DFNS tells us about built-in ports that have no
63 * standard enumeration mechanism. Platforms that can find all
64 * serial ports via mechanisms like ACPI or PCI need not supply it.
66 #ifndef SERIAL_PORT_DFNS
67 #define SERIAL_PORT_DFNS
68 #endif
70 static const struct old_serial_port old_serial_port[] = {
71 SERIAL_PORT_DFNS /* defined in asm/serial.h */
74 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
76 #ifdef CONFIG_SERIAL_8250_RSA
78 #define PORT_RSA_MAX 4
79 static unsigned long probe_rsa[PORT_RSA_MAX];
80 static unsigned int probe_rsa_count;
81 #endif /* CONFIG_SERIAL_8250_RSA */
83 struct irq_info {
84 struct hlist_node node;
85 int irq;
86 spinlock_t lock; /* Protects list not the hash */
87 struct list_head *head;
90 #define NR_IRQ_HASH 32 /* Can be adjusted later */
91 static struct hlist_head irq_lists[NR_IRQ_HASH];
92 static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
95 * This is the serial driver's interrupt routine.
97 * Arjan thinks the old way was overly complex, so it got simplified.
98 * Alan disagrees, saying that need the complexity to handle the weird
99 * nature of ISA shared interrupts. (This is a special exception.)
101 * In order to handle ISA shared interrupts properly, we need to check
102 * that all ports have been serviced, and therefore the ISA interrupt
103 * line has been de-asserted.
105 * This means we need to loop through all ports. checking that they
106 * don't have an interrupt pending.
108 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
110 struct irq_info *i = dev_id;
111 struct list_head *l, *end = NULL;
112 int pass_counter = 0, handled = 0;
114 pr_debug("%s(%d): start\n", __func__, irq);
116 spin_lock(&i->lock);
118 l = i->head;
119 do {
120 struct uart_8250_port *up;
121 struct uart_port *port;
123 up = list_entry(l, struct uart_8250_port, list);
124 port = &up->port;
126 if (port->handle_irq(port)) {
127 handled = 1;
128 end = NULL;
129 } else if (end == NULL)
130 end = l;
132 l = l->next;
134 if (l == i->head && pass_counter++ > PASS_LIMIT)
135 break;
136 } while (l != end);
138 spin_unlock(&i->lock);
140 pr_debug("%s(%d): end\n", __func__, irq);
142 return IRQ_RETVAL(handled);
146 * To support ISA shared interrupts, we need to have one interrupt
147 * handler that ensures that the IRQ line has been deasserted
148 * before returning. Failing to do this will result in the IRQ
149 * line being stuck active, and, since ISA irqs are edge triggered,
150 * no more IRQs will be seen.
152 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
154 spin_lock_irq(&i->lock);
156 if (!list_empty(i->head)) {
157 if (i->head == &up->list)
158 i->head = i->head->next;
159 list_del(&up->list);
160 } else {
161 BUG_ON(i->head != &up->list);
162 i->head = NULL;
164 spin_unlock_irq(&i->lock);
165 /* List empty so throw away the hash node */
166 if (i->head == NULL) {
167 hlist_del(&i->node);
168 kfree(i);
172 static int serial_link_irq_chain(struct uart_8250_port *up)
174 struct hlist_head *h;
175 struct hlist_node *n;
176 struct irq_info *i;
177 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
179 mutex_lock(&hash_mutex);
181 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
183 hlist_for_each(n, h) {
184 i = hlist_entry(n, struct irq_info, node);
185 if (i->irq == up->port.irq)
186 break;
189 if (n == NULL) {
190 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
191 if (i == NULL) {
192 mutex_unlock(&hash_mutex);
193 return -ENOMEM;
195 spin_lock_init(&i->lock);
196 i->irq = up->port.irq;
197 hlist_add_head(&i->node, h);
199 mutex_unlock(&hash_mutex);
201 spin_lock_irq(&i->lock);
203 if (i->head) {
204 list_add(&up->list, i->head);
205 spin_unlock_irq(&i->lock);
207 ret = 0;
208 } else {
209 INIT_LIST_HEAD(&up->list);
210 i->head = &up->list;
211 spin_unlock_irq(&i->lock);
212 irq_flags |= up->port.irqflags;
213 ret = request_irq(up->port.irq, serial8250_interrupt,
214 irq_flags, up->port.name, i);
215 if (ret < 0)
216 serial_do_unlink(i, up);
219 return ret;
222 static void serial_unlink_irq_chain(struct uart_8250_port *up)
225 * yes, some broken gcc emit "warning: 'i' may be used uninitialized"
226 * but no, we are not going to take a patch that assigns NULL below.
228 struct irq_info *i;
229 struct hlist_node *n;
230 struct hlist_head *h;
232 mutex_lock(&hash_mutex);
234 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
236 hlist_for_each(n, h) {
237 i = hlist_entry(n, struct irq_info, node);
238 if (i->irq == up->port.irq)
239 break;
242 BUG_ON(n == NULL);
243 BUG_ON(i->head == NULL);
245 if (list_empty(i->head))
246 free_irq(up->port.irq, i);
248 serial_do_unlink(i, up);
249 mutex_unlock(&hash_mutex);
253 * This function is used to handle ports that do not have an
254 * interrupt. This doesn't work very well for 16450's, but gives
255 * barely passable results for a 16550A. (Although at the expense
256 * of much CPU overhead).
258 static void serial8250_timeout(struct timer_list *t)
260 struct uart_8250_port *up = from_timer(up, t, timer);
262 up->port.handle_irq(&up->port);
263 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
266 static void serial8250_backup_timeout(struct timer_list *t)
268 struct uart_8250_port *up = from_timer(up, t, timer);
269 unsigned int iir, ier = 0, lsr;
270 unsigned long flags;
272 spin_lock_irqsave(&up->port.lock, flags);
275 * Must disable interrupts or else we risk racing with the interrupt
276 * based handler.
278 if (up->port.irq) {
279 ier = serial_in(up, UART_IER);
280 serial_out(up, UART_IER, 0);
283 iir = serial_in(up, UART_IIR);
286 * This should be a safe test for anyone who doesn't trust the
287 * IIR bits on their UART, but it's specifically designed for
288 * the "Diva" UART used on the management processor on many HP
289 * ia64 and parisc boxes.
291 lsr = serial_in(up, UART_LSR);
292 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
293 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
294 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
295 (lsr & UART_LSR_THRE)) {
296 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
297 iir |= UART_IIR_THRI;
300 if (!(iir & UART_IIR_NO_INT))
301 serial8250_tx_chars(up);
303 if (up->port.irq)
304 serial_out(up, UART_IER, ier);
306 spin_unlock_irqrestore(&up->port.lock, flags);
308 /* Standard timer interval plus 0.2s to keep the port running */
309 mod_timer(&up->timer,
310 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
313 static int univ8250_setup_irq(struct uart_8250_port *up)
315 struct uart_port *port = &up->port;
316 int retval = 0;
319 * The above check will only give an accurate result the first time
320 * the port is opened so this value needs to be preserved.
322 if (up->bugs & UART_BUG_THRE) {
323 pr_debug("%s - using backup timer\n", port->name);
325 up->timer.function = serial8250_backup_timeout;
326 mod_timer(&up->timer, jiffies +
327 uart_poll_timeout(port) + HZ / 5);
331 * If the "interrupt" for this port doesn't correspond with any
332 * hardware interrupt, we use a timer-based system. The original
333 * driver used to do this with IRQ0.
335 if (!port->irq) {
336 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
337 } else
338 retval = serial_link_irq_chain(up);
340 return retval;
343 static void univ8250_release_irq(struct uart_8250_port *up)
345 struct uart_port *port = &up->port;
347 del_timer_sync(&up->timer);
348 up->timer.function = serial8250_timeout;
349 if (port->irq)
350 serial_unlink_irq_chain(up);
353 #ifdef CONFIG_SERIAL_8250_RSA
354 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
356 unsigned long start = UART_RSA_BASE << up->port.regshift;
357 unsigned int size = 8 << up->port.regshift;
358 struct uart_port *port = &up->port;
359 int ret = -EINVAL;
361 switch (port->iotype) {
362 case UPIO_HUB6:
363 case UPIO_PORT:
364 start += port->iobase;
365 if (request_region(start, size, "serial-rsa"))
366 ret = 0;
367 else
368 ret = -EBUSY;
369 break;
372 return ret;
375 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
377 unsigned long offset = UART_RSA_BASE << up->port.regshift;
378 unsigned int size = 8 << up->port.regshift;
379 struct uart_port *port = &up->port;
381 switch (port->iotype) {
382 case UPIO_HUB6:
383 case UPIO_PORT:
384 release_region(port->iobase + offset, size);
385 break;
388 #endif
390 static const struct uart_ops *base_ops;
391 static struct uart_ops univ8250_port_ops;
393 static const struct uart_8250_ops univ8250_driver_ops = {
394 .setup_irq = univ8250_setup_irq,
395 .release_irq = univ8250_release_irq,
398 static struct uart_8250_port serial8250_ports[UART_NR];
401 * serial8250_get_port - retrieve struct uart_8250_port
402 * @line: serial line number
404 * This function retrieves struct uart_8250_port for the specific line.
405 * This struct *must* *not* be used to perform a 8250 or serial core operation
406 * which is not accessible otherwise. Its only purpose is to make the struct
407 * accessible to the runtime-pm callbacks for context suspend/restore.
408 * The lock assumption made here is none because runtime-pm suspend/resume
409 * callbacks should not be invoked if there is any operation performed on the
410 * port.
412 struct uart_8250_port *serial8250_get_port(int line)
414 return &serial8250_ports[line];
416 EXPORT_SYMBOL_GPL(serial8250_get_port);
418 static void (*serial8250_isa_config)(int port, struct uart_port *up,
419 u32 *capabilities);
421 void serial8250_set_isa_configurator(
422 void (*v)(int port, struct uart_port *up, u32 *capabilities))
424 serial8250_isa_config = v;
426 EXPORT_SYMBOL(serial8250_set_isa_configurator);
428 #ifdef CONFIG_SERIAL_8250_RSA
430 static void univ8250_config_port(struct uart_port *port, int flags)
432 struct uart_8250_port *up = up_to_u8250p(port);
434 up->probe &= ~UART_PROBE_RSA;
435 if (port->type == PORT_RSA) {
436 if (serial8250_request_rsa_resource(up) == 0)
437 up->probe |= UART_PROBE_RSA;
438 } else if (flags & UART_CONFIG_TYPE) {
439 int i;
441 for (i = 0; i < probe_rsa_count; i++) {
442 if (probe_rsa[i] == up->port.iobase) {
443 if (serial8250_request_rsa_resource(up) == 0)
444 up->probe |= UART_PROBE_RSA;
445 break;
450 base_ops->config_port(port, flags);
452 if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
453 serial8250_release_rsa_resource(up);
456 static int univ8250_request_port(struct uart_port *port)
458 struct uart_8250_port *up = up_to_u8250p(port);
459 int ret;
461 ret = base_ops->request_port(port);
462 if (ret == 0 && port->type == PORT_RSA) {
463 ret = serial8250_request_rsa_resource(up);
464 if (ret < 0)
465 base_ops->release_port(port);
468 return ret;
471 static void univ8250_release_port(struct uart_port *port)
473 struct uart_8250_port *up = up_to_u8250p(port);
475 if (port->type == PORT_RSA)
476 serial8250_release_rsa_resource(up);
477 base_ops->release_port(port);
480 static void univ8250_rsa_support(struct uart_ops *ops)
482 ops->config_port = univ8250_config_port;
483 ops->request_port = univ8250_request_port;
484 ops->release_port = univ8250_release_port;
487 #else
488 #define univ8250_rsa_support(x) do { } while (0)
489 #endif /* CONFIG_SERIAL_8250_RSA */
491 static inline void serial8250_apply_quirks(struct uart_8250_port *up)
493 up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0;
496 static void __init serial8250_isa_init_ports(void)
498 struct uart_8250_port *up;
499 static int first = 1;
500 int i, irqflag = 0;
502 if (!first)
503 return;
504 first = 0;
506 if (nr_uarts > UART_NR)
507 nr_uarts = UART_NR;
509 for (i = 0; i < nr_uarts; i++) {
510 struct uart_8250_port *up = &serial8250_ports[i];
511 struct uart_port *port = &up->port;
513 port->line = i;
514 serial8250_init_port(up);
515 if (!base_ops)
516 base_ops = port->ops;
517 port->ops = &univ8250_port_ops;
519 timer_setup(&up->timer, serial8250_timeout, 0);
521 up->ops = &univ8250_driver_ops;
524 * ALPHA_KLUDGE_MCR needs to be killed.
526 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
527 up->mcr_force = ALPHA_KLUDGE_MCR;
530 /* chain base port ops to support Remote Supervisor Adapter */
531 univ8250_port_ops = *base_ops;
532 univ8250_rsa_support(&univ8250_port_ops);
534 if (share_irqs)
535 irqflag = IRQF_SHARED;
537 for (i = 0, up = serial8250_ports;
538 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
539 i++, up++) {
540 struct uart_port *port = &up->port;
542 port->iobase = old_serial_port[i].port;
543 port->irq = irq_canonicalize(old_serial_port[i].irq);
544 port->irqflags = 0;
545 port->uartclk = old_serial_port[i].baud_base * 16;
546 port->flags = old_serial_port[i].flags;
547 port->hub6 = 0;
548 port->membase = old_serial_port[i].iomem_base;
549 port->iotype = old_serial_port[i].io_type;
550 port->regshift = old_serial_port[i].iomem_reg_shift;
551 serial8250_set_defaults(up);
553 port->irqflags |= irqflag;
554 if (serial8250_isa_config != NULL)
555 serial8250_isa_config(i, &up->port, &up->capabilities);
559 static void __init
560 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
562 int i;
564 for (i = 0; i < nr_uarts; i++) {
565 struct uart_8250_port *up = &serial8250_ports[i];
567 if (up->port.type == PORT_8250_CIR)
568 continue;
570 if (up->port.dev)
571 continue;
573 up->port.dev = dev;
575 serial8250_apply_quirks(up);
576 uart_add_one_port(drv, &up->port);
580 #ifdef CONFIG_SERIAL_8250_CONSOLE
582 static void univ8250_console_write(struct console *co, const char *s,
583 unsigned int count)
585 struct uart_8250_port *up = &serial8250_ports[co->index];
587 serial8250_console_write(up, s, count);
590 static int univ8250_console_setup(struct console *co, char *options)
592 struct uart_port *port;
593 int retval;
596 * Check whether an invalid uart number has been specified, and
597 * if so, search for the first available port that does have
598 * console support.
600 if (co->index >= nr_uarts)
601 co->index = 0;
602 port = &serial8250_ports[co->index].port;
603 /* link port to console */
604 port->cons = co;
606 retval = serial8250_console_setup(port, options, false);
607 if (retval != 0)
608 port->cons = NULL;
609 return retval;
613 * univ8250_console_match - non-standard console matching
614 * @co: registering console
615 * @name: name from console command line
616 * @idx: index from console command line
617 * @options: ptr to option string from console command line
619 * Only attempts to match console command lines of the form:
620 * console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>]
621 * console=uart[8250],0x<addr>[,<options>]
622 * This form is used to register an initial earlycon boot console and
623 * replace it with the serial8250_console at 8250 driver init.
625 * Performs console setup for a match (as required by interface)
626 * If no <options> are specified, then assume the h/w is already setup.
628 * Returns 0 if console matches; otherwise non-zero to use default matching
630 static int univ8250_console_match(struct console *co, char *name, int idx,
631 char *options)
633 char match[] = "uart"; /* 8250-specific earlycon name */
634 unsigned char iotype;
635 resource_size_t addr;
636 int i;
638 if (strncmp(name, match, 4) != 0)
639 return -ENODEV;
641 if (uart_parse_earlycon(options, &iotype, &addr, &options))
642 return -ENODEV;
644 /* try to match the port specified on the command line */
645 for (i = 0; i < nr_uarts; i++) {
646 struct uart_port *port = &serial8250_ports[i].port;
648 if (port->iotype != iotype)
649 continue;
650 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
651 iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
652 && (port->mapbase != addr))
653 continue;
654 if (iotype == UPIO_PORT && port->iobase != addr)
655 continue;
657 co->index = i;
658 port->cons = co;
659 return serial8250_console_setup(port, options, true);
662 return -ENODEV;
665 static struct console univ8250_console = {
666 .name = "ttyS",
667 .write = univ8250_console_write,
668 .device = uart_console_device,
669 .setup = univ8250_console_setup,
670 .match = univ8250_console_match,
671 .flags = CON_PRINTBUFFER | CON_ANYTIME,
672 .index = -1,
673 .data = &serial8250_reg,
676 static int __init univ8250_console_init(void)
678 if (nr_uarts == 0)
679 return -ENODEV;
681 serial8250_isa_init_ports();
682 register_console(&univ8250_console);
683 return 0;
685 console_initcall(univ8250_console_init);
687 #define SERIAL8250_CONSOLE (&univ8250_console)
688 #else
689 #define SERIAL8250_CONSOLE NULL
690 #endif
692 static struct uart_driver serial8250_reg = {
693 .owner = THIS_MODULE,
694 .driver_name = "serial",
695 .dev_name = "ttyS",
696 .major = TTY_MAJOR,
697 .minor = 64,
698 .cons = SERIAL8250_CONSOLE,
702 * early_serial_setup - early registration for 8250 ports
704 * Setup an 8250 port structure prior to console initialisation. Use
705 * after console initialisation will cause undefined behaviour.
707 int __init early_serial_setup(struct uart_port *port)
709 struct uart_port *p;
711 if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
712 return -ENODEV;
714 serial8250_isa_init_ports();
715 p = &serial8250_ports[port->line].port;
716 p->iobase = port->iobase;
717 p->membase = port->membase;
718 p->irq = port->irq;
719 p->irqflags = port->irqflags;
720 p->uartclk = port->uartclk;
721 p->fifosize = port->fifosize;
722 p->regshift = port->regshift;
723 p->iotype = port->iotype;
724 p->flags = port->flags;
725 p->mapbase = port->mapbase;
726 p->mapsize = port->mapsize;
727 p->private_data = port->private_data;
728 p->type = port->type;
729 p->line = port->line;
731 serial8250_set_defaults(up_to_u8250p(p));
733 if (port->serial_in)
734 p->serial_in = port->serial_in;
735 if (port->serial_out)
736 p->serial_out = port->serial_out;
737 if (port->handle_irq)
738 p->handle_irq = port->handle_irq;
740 return 0;
744 * serial8250_suspend_port - suspend one serial port
745 * @line: serial line number
747 * Suspend one serial port.
749 void serial8250_suspend_port(int line)
751 struct uart_8250_port *up = &serial8250_ports[line];
752 struct uart_port *port = &up->port;
754 if (!console_suspend_enabled && uart_console(port) &&
755 port->type != PORT_8250) {
756 unsigned char canary = 0xa5;
757 serial_out(up, UART_SCR, canary);
758 if (serial_in(up, UART_SCR) == canary)
759 up->canary = canary;
762 uart_suspend_port(&serial8250_reg, port);
764 EXPORT_SYMBOL(serial8250_suspend_port);
767 * serial8250_resume_port - resume one serial port
768 * @line: serial line number
770 * Resume one serial port.
772 void serial8250_resume_port(int line)
774 struct uart_8250_port *up = &serial8250_ports[line];
775 struct uart_port *port = &up->port;
777 up->canary = 0;
779 if (up->capabilities & UART_NATSEMI) {
780 /* Ensure it's still in high speed mode */
781 serial_port_out(port, UART_LCR, 0xE0);
783 ns16550a_goto_highspeed(up);
785 serial_port_out(port, UART_LCR, 0);
786 port->uartclk = 921600*16;
788 uart_resume_port(&serial8250_reg, port);
790 EXPORT_SYMBOL(serial8250_resume_port);
793 * Register a set of serial devices attached to a platform device. The
794 * list is terminated with a zero flags entry, which means we expect
795 * all entries to have at least UPF_BOOT_AUTOCONF set.
797 static int serial8250_probe(struct platform_device *dev)
799 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
800 struct uart_8250_port uart;
801 int ret, i, irqflag = 0;
803 memset(&uart, 0, sizeof(uart));
805 if (share_irqs)
806 irqflag = IRQF_SHARED;
808 for (i = 0; p && p->flags != 0; p++, i++) {
809 uart.port.iobase = p->iobase;
810 uart.port.membase = p->membase;
811 uart.port.irq = p->irq;
812 uart.port.irqflags = p->irqflags;
813 uart.port.uartclk = p->uartclk;
814 uart.port.regshift = p->regshift;
815 uart.port.iotype = p->iotype;
816 uart.port.flags = p->flags;
817 uart.port.mapbase = p->mapbase;
818 uart.port.hub6 = p->hub6;
819 uart.port.has_sysrq = p->has_sysrq;
820 uart.port.private_data = p->private_data;
821 uart.port.type = p->type;
822 uart.port.serial_in = p->serial_in;
823 uart.port.serial_out = p->serial_out;
824 uart.port.handle_irq = p->handle_irq;
825 uart.port.handle_break = p->handle_break;
826 uart.port.set_termios = p->set_termios;
827 uart.port.set_ldisc = p->set_ldisc;
828 uart.port.get_mctrl = p->get_mctrl;
829 uart.port.pm = p->pm;
830 uart.port.dev = &dev->dev;
831 uart.port.irqflags |= irqflag;
832 ret = serial8250_register_8250_port(&uart);
833 if (ret < 0) {
834 dev_err(&dev->dev, "unable to register port at index %d "
835 "(IO%lx MEM%llx IRQ%d): %d\n", i,
836 p->iobase, (unsigned long long)p->mapbase,
837 p->irq, ret);
840 return 0;
844 * Remove serial ports registered against a platform device.
846 static int serial8250_remove(struct platform_device *dev)
848 int i;
850 for (i = 0; i < nr_uarts; i++) {
851 struct uart_8250_port *up = &serial8250_ports[i];
853 if (up->port.dev == &dev->dev)
854 serial8250_unregister_port(i);
856 return 0;
859 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
861 int i;
863 for (i = 0; i < UART_NR; i++) {
864 struct uart_8250_port *up = &serial8250_ports[i];
866 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
867 uart_suspend_port(&serial8250_reg, &up->port);
870 return 0;
873 static int serial8250_resume(struct platform_device *dev)
875 int i;
877 for (i = 0; i < UART_NR; i++) {
878 struct uart_8250_port *up = &serial8250_ports[i];
880 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
881 serial8250_resume_port(i);
884 return 0;
887 static struct platform_driver serial8250_isa_driver = {
888 .probe = serial8250_probe,
889 .remove = serial8250_remove,
890 .suspend = serial8250_suspend,
891 .resume = serial8250_resume,
892 .driver = {
893 .name = "serial8250",
898 * This "device" covers _all_ ISA 8250-compatible serial devices listed
899 * in the table in include/asm/serial.h
901 static struct platform_device *serial8250_isa_devs;
904 * serial8250_register_8250_port and serial8250_unregister_port allows for
905 * 16x50 serial ports to be configured at run-time, to support PCMCIA
906 * modems and PCI multiport cards.
908 static DEFINE_MUTEX(serial_mutex);
910 static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
912 int i;
915 * First, find a port entry which matches.
917 for (i = 0; i < nr_uarts; i++)
918 if (uart_match_port(&serial8250_ports[i].port, port))
919 return &serial8250_ports[i];
921 /* try line number first if still available */
922 i = port->line;
923 if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
924 serial8250_ports[i].port.iobase == 0)
925 return &serial8250_ports[i];
927 * We didn't find a matching entry, so look for the first
928 * free entry. We look for one which hasn't been previously
929 * used (indicated by zero iobase).
931 for (i = 0; i < nr_uarts; i++)
932 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
933 serial8250_ports[i].port.iobase == 0)
934 return &serial8250_ports[i];
937 * That also failed. Last resort is to find any entry which
938 * doesn't have a real port associated with it.
940 for (i = 0; i < nr_uarts; i++)
941 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
942 return &serial8250_ports[i];
944 return NULL;
947 static void serial_8250_overrun_backoff_work(struct work_struct *work)
949 struct uart_8250_port *up =
950 container_of(to_delayed_work(work), struct uart_8250_port,
951 overrun_backoff);
952 struct uart_port *port = &up->port;
953 unsigned long flags;
955 spin_lock_irqsave(&port->lock, flags);
956 up->ier |= UART_IER_RLSI | UART_IER_RDI;
957 up->port.read_status_mask |= UART_LSR_DR;
958 serial_out(up, UART_IER, up->ier);
959 spin_unlock_irqrestore(&port->lock, flags);
963 * serial8250_register_8250_port - register a serial port
964 * @up: serial port template
966 * Configure the serial port specified by the request. If the
967 * port exists and is in use, it is hung up and unregistered
968 * first.
970 * The port is then probed and if necessary the IRQ is autodetected
971 * If this fails an error is returned.
973 * On success the port is ready to use and the line number is returned.
975 int serial8250_register_8250_port(struct uart_8250_port *up)
977 struct uart_8250_port *uart;
978 int ret = -ENOSPC;
980 if (up->port.uartclk == 0)
981 return -EINVAL;
983 mutex_lock(&serial_mutex);
985 uart = serial8250_find_match_or_unused(&up->port);
986 if (uart && uart->port.type != PORT_8250_CIR) {
987 struct mctrl_gpios *gpios;
989 if (uart->port.dev)
990 uart_remove_one_port(&serial8250_reg, &uart->port);
992 uart->port.iobase = up->port.iobase;
993 uart->port.membase = up->port.membase;
994 uart->port.irq = up->port.irq;
995 uart->port.irqflags = up->port.irqflags;
996 uart->port.uartclk = up->port.uartclk;
997 uart->port.fifosize = up->port.fifosize;
998 uart->port.regshift = up->port.regshift;
999 uart->port.iotype = up->port.iotype;
1000 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
1001 uart->bugs = up->bugs;
1002 uart->port.mapbase = up->port.mapbase;
1003 uart->port.mapsize = up->port.mapsize;
1004 uart->port.private_data = up->port.private_data;
1005 uart->tx_loadsz = up->tx_loadsz;
1006 uart->capabilities = up->capabilities;
1007 uart->port.throttle = up->port.throttle;
1008 uart->port.unthrottle = up->port.unthrottle;
1009 uart->port.rs485_config = up->port.rs485_config;
1010 uart->port.rs485 = up->port.rs485;
1011 uart->dma = up->dma;
1013 /* Take tx_loadsz from fifosize if it wasn't set separately */
1014 if (uart->port.fifosize && !uart->tx_loadsz)
1015 uart->tx_loadsz = uart->port.fifosize;
1017 if (up->port.dev)
1018 uart->port.dev = up->port.dev;
1020 if (up->port.flags & UPF_FIXED_TYPE)
1021 uart->port.type = up->port.type;
1024 * Only call mctrl_gpio_init(), if the device has no ACPI
1025 * companion device
1027 if (!has_acpi_companion(uart->port.dev)) {
1028 gpios = mctrl_gpio_init(&uart->port, 0);
1029 if (IS_ERR(gpios)) {
1030 ret = PTR_ERR(gpios);
1031 goto out_unlock;
1032 } else {
1033 uart->gpios = gpios;
1037 serial8250_set_defaults(uart);
1039 /* Possibly override default I/O functions. */
1040 if (up->port.serial_in)
1041 uart->port.serial_in = up->port.serial_in;
1042 if (up->port.serial_out)
1043 uart->port.serial_out = up->port.serial_out;
1044 if (up->port.handle_irq)
1045 uart->port.handle_irq = up->port.handle_irq;
1046 /* Possibly override set_termios call */
1047 if (up->port.set_termios)
1048 uart->port.set_termios = up->port.set_termios;
1049 if (up->port.set_ldisc)
1050 uart->port.set_ldisc = up->port.set_ldisc;
1051 if (up->port.get_mctrl)
1052 uart->port.get_mctrl = up->port.get_mctrl;
1053 if (up->port.set_mctrl)
1054 uart->port.set_mctrl = up->port.set_mctrl;
1055 if (up->port.get_divisor)
1056 uart->port.get_divisor = up->port.get_divisor;
1057 if (up->port.set_divisor)
1058 uart->port.set_divisor = up->port.set_divisor;
1059 if (up->port.startup)
1060 uart->port.startup = up->port.startup;
1061 if (up->port.shutdown)
1062 uart->port.shutdown = up->port.shutdown;
1063 if (up->port.pm)
1064 uart->port.pm = up->port.pm;
1065 if (up->port.handle_break)
1066 uart->port.handle_break = up->port.handle_break;
1067 if (up->dl_read)
1068 uart->dl_read = up->dl_read;
1069 if (up->dl_write)
1070 uart->dl_write = up->dl_write;
1072 if (uart->port.type != PORT_8250_CIR) {
1073 if (serial8250_isa_config != NULL)
1074 serial8250_isa_config(0, &uart->port,
1075 &uart->capabilities);
1077 serial8250_apply_quirks(uart);
1078 ret = uart_add_one_port(&serial8250_reg,
1079 &uart->port);
1080 if (ret == 0)
1081 ret = uart->port.line;
1082 } else {
1083 dev_info(uart->port.dev,
1084 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
1085 uart->port.iobase,
1086 (unsigned long long)uart->port.mapbase,
1087 uart->port.irq);
1089 ret = 0;
1092 /* Initialise interrupt backoff work if required */
1093 if (up->overrun_backoff_time_ms > 0) {
1094 uart->overrun_backoff_time_ms =
1095 up->overrun_backoff_time_ms;
1096 INIT_DELAYED_WORK(&uart->overrun_backoff,
1097 serial_8250_overrun_backoff_work);
1098 } else {
1099 uart->overrun_backoff_time_ms = 0;
1103 out_unlock:
1104 mutex_unlock(&serial_mutex);
1106 return ret;
1108 EXPORT_SYMBOL(serial8250_register_8250_port);
1111 * serial8250_unregister_port - remove a 16x50 serial port at runtime
1112 * @line: serial line number
1114 * Remove one serial port. This may not be called from interrupt
1115 * context. We hand the port back to the our control.
1117 void serial8250_unregister_port(int line)
1119 struct uart_8250_port *uart = &serial8250_ports[line];
1121 mutex_lock(&serial_mutex);
1123 if (uart->em485) {
1124 unsigned long flags;
1126 spin_lock_irqsave(&uart->port.lock, flags);
1127 serial8250_em485_destroy(uart);
1128 spin_unlock_irqrestore(&uart->port.lock, flags);
1131 uart_remove_one_port(&serial8250_reg, &uart->port);
1132 if (serial8250_isa_devs) {
1133 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
1134 uart->port.type = PORT_UNKNOWN;
1135 uart->port.dev = &serial8250_isa_devs->dev;
1136 uart->capabilities = 0;
1137 serial8250_apply_quirks(uart);
1138 uart_add_one_port(&serial8250_reg, &uart->port);
1139 } else {
1140 uart->port.dev = NULL;
1142 mutex_unlock(&serial_mutex);
1144 EXPORT_SYMBOL(serial8250_unregister_port);
1146 static int __init serial8250_init(void)
1148 int ret;
1150 if (nr_uarts == 0)
1151 return -ENODEV;
1153 serial8250_isa_init_ports();
1155 pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %sabled\n",
1156 nr_uarts, share_irqs ? "en" : "dis");
1158 #ifdef CONFIG_SPARC
1159 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
1160 #else
1161 serial8250_reg.nr = UART_NR;
1162 ret = uart_register_driver(&serial8250_reg);
1163 #endif
1164 if (ret)
1165 goto out;
1167 ret = serial8250_pnp_init();
1168 if (ret)
1169 goto unreg_uart_drv;
1171 serial8250_isa_devs = platform_device_alloc("serial8250",
1172 PLAT8250_DEV_LEGACY);
1173 if (!serial8250_isa_devs) {
1174 ret = -ENOMEM;
1175 goto unreg_pnp;
1178 ret = platform_device_add(serial8250_isa_devs);
1179 if (ret)
1180 goto put_dev;
1182 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
1184 ret = platform_driver_register(&serial8250_isa_driver);
1185 if (ret == 0)
1186 goto out;
1188 platform_device_del(serial8250_isa_devs);
1189 put_dev:
1190 platform_device_put(serial8250_isa_devs);
1191 unreg_pnp:
1192 serial8250_pnp_exit();
1193 unreg_uart_drv:
1194 #ifdef CONFIG_SPARC
1195 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1196 #else
1197 uart_unregister_driver(&serial8250_reg);
1198 #endif
1199 out:
1200 return ret;
1203 static void __exit serial8250_exit(void)
1205 struct platform_device *isa_dev = serial8250_isa_devs;
1208 * This tells serial8250_unregister_port() not to re-register
1209 * the ports (thereby making serial8250_isa_driver permanently
1210 * in use.)
1212 serial8250_isa_devs = NULL;
1214 platform_driver_unregister(&serial8250_isa_driver);
1215 platform_device_unregister(isa_dev);
1217 serial8250_pnp_exit();
1219 #ifdef CONFIG_SPARC
1220 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1221 #else
1222 uart_unregister_driver(&serial8250_reg);
1223 #endif
1226 module_init(serial8250_init);
1227 module_exit(serial8250_exit);
1229 MODULE_LICENSE("GPL");
1230 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
1232 module_param_hw(share_irqs, uint, other, 0644);
1233 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
1235 module_param(nr_uarts, uint, 0644);
1236 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
1238 module_param(skip_txen_test, uint, 0644);
1239 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
1241 #ifdef CONFIG_SERIAL_8250_RSA
1242 module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
1243 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
1244 #endif
1245 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
1247 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
1248 #ifndef MODULE
1249 /* This module was renamed to 8250_core in 3.7. Keep the old "8250" name
1250 * working as well for the module options so we don't break people. We
1251 * need to keep the names identical and the convenient macros will happily
1252 * refuse to let us do that by failing the build with redefinition errors
1253 * of global variables. So we stick them inside a dummy function to avoid
1254 * those conflicts. The options still get parsed, and the redefined
1255 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
1257 * This is hacky. I'm sorry.
1259 static void __used s8250_options(void)
1261 #undef MODULE_PARAM_PREFIX
1262 #define MODULE_PARAM_PREFIX "8250_core."
1264 module_param_cb(share_irqs, &param_ops_uint, &share_irqs, 0644);
1265 module_param_cb(nr_uarts, &param_ops_uint, &nr_uarts, 0644);
1266 module_param_cb(skip_txen_test, &param_ops_uint, &skip_txen_test, 0644);
1267 #ifdef CONFIG_SERIAL_8250_RSA
1268 __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
1269 &param_array_ops, .arr = &__param_arr_probe_rsa,
1270 0444, -1, 0);
1271 #endif
1273 #else
1274 MODULE_ALIAS("8250_core");
1275 #endif
1276 #endif