xref: /linux/drivers/tty/serial/8250/8250_core.c (revision 436381eaf2a423e60fc8340399f7d2458091b383)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Universal/legacy driver for 8250/16550-type serial ports
4  *
5  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6  *
7  *  Copyright (C) 2001 Russell King.
8  *
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
15  */
16 
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/pm_runtime.h>
27 #include <linux/tty.h>
28 #include <linux/ratelimit.h>
29 #include <linux/tty_flip.h>
30 #include <linux/serial.h>
31 #include <linux/serial_8250.h>
32 #include <linux/nmi.h>
33 #include <linux/mutex.h>
34 #include <linux/slab.h>
35 #include <linux/string_helpers.h>
36 #include <linux/uaccess.h>
37 #include <linux/io.h>
38 #ifdef CONFIG_SPARC
39 #include <linux/sunserialcore.h>
40 #endif
41 
42 #include <asm/irq.h>
43 
44 #include "8250.h"
45 
46 /*
47  * Configuration:
48  *   share_irqs - whether we pass IRQF_SHARED to request_irq().  This option
49  *                is unsafe when used on edge-triggered interrupts.
50  */
51 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
52 
53 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
54 
55 static struct uart_driver serial8250_reg;
56 
57 static unsigned int skip_txen_test; /* force skip of txen test at init time */
58 
59 #define PASS_LIMIT	512
60 
61 #include <asm/serial.h>
62 /*
63  * SERIAL_PORT_DFNS tells us about built-in ports that have no
64  * standard enumeration mechanism.   Platforms that can find all
65  * serial ports via mechanisms like ACPI or PCI need not supply it.
66  */
67 #ifndef SERIAL_PORT_DFNS
68 #define SERIAL_PORT_DFNS
69 #endif
70 
71 static const struct old_serial_port old_serial_port[] = {
72 	SERIAL_PORT_DFNS /* defined in asm/serial.h */
73 };
74 
75 #define UART_NR	CONFIG_SERIAL_8250_NR_UARTS
76 
77 #ifdef CONFIG_SERIAL_8250_RSA
78 
79 #define PORT_RSA_MAX 4
80 static unsigned long probe_rsa[PORT_RSA_MAX];
81 static unsigned int probe_rsa_count;
82 #endif /* CONFIG_SERIAL_8250_RSA  */
83 
84 struct irq_info {
85 	struct			hlist_node node;
86 	int			irq;
87 	spinlock_t		lock;	/* Protects list not the hash */
88 	struct list_head	*head;
89 };
90 
91 #define NR_IRQ_HASH		32	/* Can be adjusted later */
92 static struct hlist_head irq_lists[NR_IRQ_HASH];
93 static DEFINE_MUTEX(hash_mutex);	/* Used to walk the hash */
94 
95 /*
96  * This is the serial driver's interrupt routine.
97  *
98  * Arjan thinks the old way was overly complex, so it got simplified.
99  * Alan disagrees, saying that need the complexity to handle the weird
100  * nature of ISA shared interrupts.  (This is a special exception.)
101  *
102  * In order to handle ISA shared interrupts properly, we need to check
103  * that all ports have been serviced, and therefore the ISA interrupt
104  * line has been de-asserted.
105  *
106  * This means we need to loop through all ports. checking that they
107  * don't have an interrupt pending.
108  */
109 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
110 {
111 	struct irq_info *i = dev_id;
112 	struct list_head *l, *end = NULL;
113 	int pass_counter = 0, handled = 0;
114 
115 	pr_debug("%s(%d): start\n", __func__, irq);
116 
117 	spin_lock(&i->lock);
118 
119 	l = i->head;
120 	do {
121 		struct uart_8250_port *up;
122 		struct uart_port *port;
123 
124 		up = list_entry(l, struct uart_8250_port, list);
125 		port = &up->port;
126 
127 		if (port->handle_irq(port)) {
128 			handled = 1;
129 			end = NULL;
130 		} else if (end == NULL)
131 			end = l;
132 
133 		l = l->next;
134 
135 		if (l == i->head && pass_counter++ > PASS_LIMIT)
136 			break;
137 	} while (l != end);
138 
139 	spin_unlock(&i->lock);
140 
141 	pr_debug("%s(%d): end\n", __func__, irq);
142 
143 	return IRQ_RETVAL(handled);
144 }
145 
146 /*
147  * To support ISA shared interrupts, we need to have one interrupt
148  * handler that ensures that the IRQ line has been deasserted
149  * before returning.  Failing to do this will result in the IRQ
150  * line being stuck active, and, since ISA irqs are edge triggered,
151  * no more IRQs will be seen.
152  */
153 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
154 {
155 	spin_lock_irq(&i->lock);
156 
157 	if (!list_empty(i->head)) {
158 		if (i->head == &up->list)
159 			i->head = i->head->next;
160 		list_del(&up->list);
161 	} else {
162 		BUG_ON(i->head != &up->list);
163 		i->head = NULL;
164 	}
165 	spin_unlock_irq(&i->lock);
166 	/* List empty so throw away the hash node */
167 	if (i->head == NULL) {
168 		hlist_del(&i->node);
169 		kfree(i);
170 	}
171 }
172 
173 static int serial_link_irq_chain(struct uart_8250_port *up)
174 {
175 	struct hlist_head *h;
176 	struct irq_info *i;
177 	int ret;
178 
179 	mutex_lock(&hash_mutex);
180 
181 	h = &irq_lists[up->port.irq % NR_IRQ_HASH];
182 
183 	hlist_for_each_entry(i, h, node)
184 		if (i->irq == up->port.irq)
185 			break;
186 
187 	if (i == NULL) {
188 		i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
189 		if (i == NULL) {
190 			mutex_unlock(&hash_mutex);
191 			return -ENOMEM;
192 		}
193 		spin_lock_init(&i->lock);
194 		i->irq = up->port.irq;
195 		hlist_add_head(&i->node, h);
196 	}
197 	mutex_unlock(&hash_mutex);
198 
199 	spin_lock_irq(&i->lock);
200 
201 	if (i->head) {
202 		list_add(&up->list, i->head);
203 		spin_unlock_irq(&i->lock);
204 
205 		ret = 0;
206 	} else {
207 		INIT_LIST_HEAD(&up->list);
208 		i->head = &up->list;
209 		spin_unlock_irq(&i->lock);
210 		ret = request_irq(up->port.irq, serial8250_interrupt,
211 				  up->port.irqflags, up->port.name, i);
212 		if (ret < 0)
213 			serial_do_unlink(i, up);
214 	}
215 
216 	return ret;
217 }
218 
219 static void serial_unlink_irq_chain(struct uart_8250_port *up)
220 {
221 	struct irq_info *i;
222 	struct hlist_head *h;
223 
224 	mutex_lock(&hash_mutex);
225 
226 	h = &irq_lists[up->port.irq % NR_IRQ_HASH];
227 
228 	hlist_for_each_entry(i, h, node)
229 		if (i->irq == up->port.irq)
230 			break;
231 
232 	BUG_ON(i == NULL);
233 	BUG_ON(i->head == NULL);
234 
235 	if (list_empty(i->head))
236 		free_irq(up->port.irq, i);
237 
238 	serial_do_unlink(i, up);
239 	mutex_unlock(&hash_mutex);
240 }
241 
242 /*
243  * This function is used to handle ports that do not have an
244  * interrupt.  This doesn't work very well for 16450's, but gives
245  * barely passable results for a 16550A.  (Although at the expense
246  * of much CPU overhead).
247  */
248 static void serial8250_timeout(struct timer_list *t)
249 {
250 	struct uart_8250_port *up = from_timer(up, t, timer);
251 
252 	up->port.handle_irq(&up->port);
253 	mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
254 }
255 
256 static void serial8250_backup_timeout(struct timer_list *t)
257 {
258 	struct uart_8250_port *up = from_timer(up, t, timer);
259 	unsigned int iir, ier = 0, lsr;
260 	unsigned long flags;
261 
262 	uart_port_lock_irqsave(&up->port, &flags);
263 
264 	/*
265 	 * Must disable interrupts or else we risk racing with the interrupt
266 	 * based handler.
267 	 */
268 	if (up->port.irq) {
269 		ier = serial_in(up, UART_IER);
270 		serial_out(up, UART_IER, 0);
271 	}
272 
273 	iir = serial_in(up, UART_IIR);
274 
275 	/*
276 	 * This should be a safe test for anyone who doesn't trust the
277 	 * IIR bits on their UART, but it's specifically designed for
278 	 * the "Diva" UART used on the management processor on many HP
279 	 * ia64 and parisc boxes.
280 	 */
281 	lsr = serial_lsr_in(up);
282 	if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
283 	    (!kfifo_is_empty(&up->port.state->port.xmit_fifo) ||
284 	     up->port.x_char) &&
285 	    (lsr & UART_LSR_THRE)) {
286 		iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
287 		iir |= UART_IIR_THRI;
288 	}
289 
290 	if (!(iir & UART_IIR_NO_INT))
291 		serial8250_tx_chars(up);
292 
293 	if (up->port.irq)
294 		serial_out(up, UART_IER, ier);
295 
296 	uart_port_unlock_irqrestore(&up->port, flags);
297 
298 	/* Standard timer interval plus 0.2s to keep the port running */
299 	mod_timer(&up->timer,
300 		jiffies + uart_poll_timeout(&up->port) + HZ / 5);
301 }
302 
303 static void univ8250_setup_timer(struct uart_8250_port *up)
304 {
305 	struct uart_port *port = &up->port;
306 
307 	/*
308 	 * The above check will only give an accurate result the first time
309 	 * the port is opened so this value needs to be preserved.
310 	 */
311 	if (up->bugs & UART_BUG_THRE) {
312 		pr_debug("%s - using backup timer\n", port->name);
313 
314 		up->timer.function = serial8250_backup_timeout;
315 		mod_timer(&up->timer, jiffies +
316 			  uart_poll_timeout(port) + HZ / 5);
317 	}
318 
319 	/*
320 	 * If the "interrupt" for this port doesn't correspond with any
321 	 * hardware interrupt, we use a timer-based system.  The original
322 	 * driver used to do this with IRQ0.
323 	 */
324 	if (!port->irq)
325 		mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
326 }
327 
328 static int univ8250_setup_irq(struct uart_8250_port *up)
329 {
330 	struct uart_port *port = &up->port;
331 
332 	if (port->irq)
333 		return serial_link_irq_chain(up);
334 
335 	return 0;
336 }
337 
338 static void univ8250_release_irq(struct uart_8250_port *up)
339 {
340 	struct uart_port *port = &up->port;
341 
342 	del_timer_sync(&up->timer);
343 	up->timer.function = serial8250_timeout;
344 	if (port->irq)
345 		serial_unlink_irq_chain(up);
346 }
347 
348 #ifdef CONFIG_SERIAL_8250_RSA
349 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
350 {
351 	unsigned long start = UART_RSA_BASE << up->port.regshift;
352 	unsigned int size = 8 << up->port.regshift;
353 	struct uart_port *port = &up->port;
354 	int ret = -EINVAL;
355 
356 	switch (port->iotype) {
357 	case UPIO_HUB6:
358 	case UPIO_PORT:
359 		start += port->iobase;
360 		if (request_region(start, size, "serial-rsa"))
361 			ret = 0;
362 		else
363 			ret = -EBUSY;
364 		break;
365 	}
366 
367 	return ret;
368 }
369 
370 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
371 {
372 	unsigned long offset = UART_RSA_BASE << up->port.regshift;
373 	unsigned int size = 8 << up->port.regshift;
374 	struct uart_port *port = &up->port;
375 
376 	switch (port->iotype) {
377 	case UPIO_HUB6:
378 	case UPIO_PORT:
379 		release_region(port->iobase + offset, size);
380 		break;
381 	}
382 }
383 #endif
384 
385 static const struct uart_ops *base_ops;
386 static struct uart_ops univ8250_port_ops;
387 
388 static const struct uart_8250_ops univ8250_driver_ops = {
389 	.setup_irq	= univ8250_setup_irq,
390 	.release_irq	= univ8250_release_irq,
391 	.setup_timer	= univ8250_setup_timer,
392 };
393 
394 static struct uart_8250_port serial8250_ports[UART_NR];
395 
396 /**
397  * serial8250_get_port - retrieve struct uart_8250_port
398  * @line: serial line number
399  *
400  * This function retrieves struct uart_8250_port for the specific line.
401  * This struct *must* *not* be used to perform a 8250 or serial core operation
402  * which is not accessible otherwise. Its only purpose is to make the struct
403  * accessible to the runtime-pm callbacks for context suspend/restore.
404  * The lock assumption made here is none because runtime-pm suspend/resume
405  * callbacks should not be invoked if there is any operation performed on the
406  * port.
407  */
408 struct uart_8250_port *serial8250_get_port(int line)
409 {
410 	return &serial8250_ports[line];
411 }
412 EXPORT_SYMBOL_GPL(serial8250_get_port);
413 
414 static void (*serial8250_isa_config)(int port, struct uart_port *up,
415 	u32 *capabilities);
416 
417 void serial8250_set_isa_configurator(
418 	void (*v)(int port, struct uart_port *up, u32 *capabilities))
419 {
420 	serial8250_isa_config = v;
421 }
422 EXPORT_SYMBOL(serial8250_set_isa_configurator);
423 
424 #ifdef CONFIG_SERIAL_8250_RSA
425 
426 static void univ8250_config_port(struct uart_port *port, int flags)
427 {
428 	struct uart_8250_port *up = up_to_u8250p(port);
429 
430 	up->probe &= ~UART_PROBE_RSA;
431 	if (port->type == PORT_RSA) {
432 		if (serial8250_request_rsa_resource(up) == 0)
433 			up->probe |= UART_PROBE_RSA;
434 	} else if (flags & UART_CONFIG_TYPE) {
435 		int i;
436 
437 		for (i = 0; i < probe_rsa_count; i++) {
438 			if (probe_rsa[i] == up->port.iobase) {
439 				if (serial8250_request_rsa_resource(up) == 0)
440 					up->probe |= UART_PROBE_RSA;
441 				break;
442 			}
443 		}
444 	}
445 
446 	base_ops->config_port(port, flags);
447 
448 	if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
449 		serial8250_release_rsa_resource(up);
450 }
451 
452 static int univ8250_request_port(struct uart_port *port)
453 {
454 	struct uart_8250_port *up = up_to_u8250p(port);
455 	int ret;
456 
457 	ret = base_ops->request_port(port);
458 	if (ret == 0 && port->type == PORT_RSA) {
459 		ret = serial8250_request_rsa_resource(up);
460 		if (ret < 0)
461 			base_ops->release_port(port);
462 	}
463 
464 	return ret;
465 }
466 
467 static void univ8250_release_port(struct uart_port *port)
468 {
469 	struct uart_8250_port *up = up_to_u8250p(port);
470 
471 	if (port->type == PORT_RSA)
472 		serial8250_release_rsa_resource(up);
473 	base_ops->release_port(port);
474 }
475 
476 static void univ8250_rsa_support(struct uart_ops *ops)
477 {
478 	ops->config_port  = univ8250_config_port;
479 	ops->request_port = univ8250_request_port;
480 	ops->release_port = univ8250_release_port;
481 }
482 
483 #else
484 #define univ8250_rsa_support(x)		do { } while (0)
485 #endif /* CONFIG_SERIAL_8250_RSA */
486 
487 static inline void serial8250_apply_quirks(struct uart_8250_port *up)
488 {
489 	up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0;
490 }
491 
492 static struct uart_8250_port *serial8250_setup_port(int index)
493 {
494 	struct uart_8250_port *up;
495 
496 	if (index >= UART_NR)
497 		return NULL;
498 
499 	up = &serial8250_ports[index];
500 	up->port.line = index;
501 	up->port.port_id = index;
502 
503 	serial8250_init_port(up);
504 	if (!base_ops)
505 		base_ops = up->port.ops;
506 	up->port.ops = &univ8250_port_ops;
507 
508 	timer_setup(&up->timer, serial8250_timeout, 0);
509 
510 	up->ops = &univ8250_driver_ops;
511 
512 	serial8250_set_defaults(up);
513 
514 	return up;
515 }
516 
517 static void __init serial8250_isa_init_ports(void)
518 {
519 	struct uart_8250_port *up;
520 	static int first = 1;
521 	int i, irqflag = 0;
522 
523 	if (!first)
524 		return;
525 	first = 0;
526 
527 	if (nr_uarts > UART_NR)
528 		nr_uarts = UART_NR;
529 
530 	/*
531 	 * Set up initial isa ports based on nr_uart module param, or else
532 	 * default to CONFIG_SERIAL_8250_RUNTIME_UARTS. Note that we do not
533 	 * need to increase nr_uarts when setting up the initial isa ports.
534 	 */
535 	for (i = 0; i < nr_uarts; i++)
536 		serial8250_setup_port(i);
537 
538 	/* chain base port ops to support Remote Supervisor Adapter */
539 	univ8250_port_ops = *base_ops;
540 	univ8250_rsa_support(&univ8250_port_ops);
541 
542 	if (share_irqs)
543 		irqflag = IRQF_SHARED;
544 
545 	for (i = 0, up = serial8250_ports;
546 	     i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
547 	     i++, up++) {
548 		struct uart_port *port = &up->port;
549 
550 		port->iobase   = old_serial_port[i].port;
551 		port->irq      = irq_canonicalize(old_serial_port[i].irq);
552 		port->irqflags = 0;
553 		port->uartclk  = old_serial_port[i].baud_base * 16;
554 		port->flags    = old_serial_port[i].flags;
555 		port->hub6     = 0;
556 		port->membase  = old_serial_port[i].iomem_base;
557 		port->iotype   = old_serial_port[i].io_type;
558 		port->regshift = old_serial_port[i].iomem_reg_shift;
559 
560 		port->irqflags |= irqflag;
561 		if (serial8250_isa_config != NULL)
562 			serial8250_isa_config(i, &up->port, &up->capabilities);
563 	}
564 }
565 
566 static void __init
567 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
568 {
569 	int i;
570 
571 	for (i = 0; i < nr_uarts; i++) {
572 		struct uart_8250_port *up = &serial8250_ports[i];
573 
574 		if (up->port.type == PORT_8250_CIR)
575 			continue;
576 
577 		if (up->port.dev)
578 			continue;
579 
580 		up->port.dev = dev;
581 
582 		if (uart_console_registered(&up->port))
583 			pm_runtime_get_sync(up->port.dev);
584 
585 		serial8250_apply_quirks(up);
586 		uart_add_one_port(drv, &up->port);
587 	}
588 }
589 
590 #ifdef CONFIG_SERIAL_8250_CONSOLE
591 
592 static void univ8250_console_write(struct console *co, const char *s,
593 				   unsigned int count)
594 {
595 	struct uart_8250_port *up = &serial8250_ports[co->index];
596 
597 	serial8250_console_write(up, s, count);
598 }
599 
600 static int univ8250_console_setup(struct console *co, char *options)
601 {
602 	struct uart_8250_port *up;
603 	struct uart_port *port;
604 	int retval, i;
605 
606 	/*
607 	 * Check whether an invalid uart number has been specified, and
608 	 * if so, search for the first available port that does have
609 	 * console support.
610 	 */
611 	if (co->index < 0 || co->index >= UART_NR)
612 		co->index = 0;
613 
614 	/*
615 	 * If the console is past the initial isa ports, init more ports up to
616 	 * co->index as needed and increment nr_uarts accordingly.
617 	 */
618 	for (i = nr_uarts; i <= co->index; i++) {
619 		up = serial8250_setup_port(i);
620 		if (!up)
621 			return -ENODEV;
622 		nr_uarts++;
623 	}
624 
625 	port = &serial8250_ports[co->index].port;
626 	/* link port to console */
627 	port->cons = co;
628 
629 	retval = serial8250_console_setup(port, options, false);
630 	if (retval != 0)
631 		port->cons = NULL;
632 	return retval;
633 }
634 
635 static int univ8250_console_exit(struct console *co)
636 {
637 	struct uart_port *port;
638 
639 	port = &serial8250_ports[co->index].port;
640 	return serial8250_console_exit(port);
641 }
642 
643 /**
644  *	univ8250_console_match - non-standard console matching
645  *	@co:	  registering console
646  *	@name:	  name from console command line
647  *	@idx:	  index from console command line
648  *	@options: ptr to option string from console command line
649  *
650  *	Only attempts to match console command lines of the form:
651  *	    console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>]
652  *	    console=uart[8250],0x<addr>[,<options>]
653  *	This form is used to register an initial earlycon boot console and
654  *	replace it with the serial8250_console at 8250 driver init.
655  *
656  *	Performs console setup for a match (as required by interface)
657  *	If no <options> are specified, then assume the h/w is already setup.
658  *
659  *	Returns 0 if console matches; otherwise non-zero to use default matching
660  */
661 static int univ8250_console_match(struct console *co, char *name, int idx,
662 				  char *options)
663 {
664 	char match[] = "uart";	/* 8250-specific earlycon name */
665 	unsigned char iotype;
666 	resource_size_t addr;
667 	int i;
668 
669 	if (strncmp(name, match, 4) != 0)
670 		return -ENODEV;
671 
672 	if (uart_parse_earlycon(options, &iotype, &addr, &options))
673 		return -ENODEV;
674 
675 	/* try to match the port specified on the command line */
676 	for (i = 0; i < nr_uarts; i++) {
677 		struct uart_port *port = &serial8250_ports[i].port;
678 
679 		if (port->iotype != iotype)
680 			continue;
681 		if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
682 		     iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
683 		    && (port->mapbase != addr))
684 			continue;
685 		if (iotype == UPIO_PORT && port->iobase != addr)
686 			continue;
687 
688 		co->index = i;
689 		port->cons = co;
690 		return serial8250_console_setup(port, options, true);
691 	}
692 
693 	return -ENODEV;
694 }
695 
696 static struct console univ8250_console = {
697 	.name		= "ttyS",
698 	.write		= univ8250_console_write,
699 	.device		= uart_console_device,
700 	.setup		= univ8250_console_setup,
701 	.exit		= univ8250_console_exit,
702 	.match		= univ8250_console_match,
703 	.flags		= CON_PRINTBUFFER | CON_ANYTIME,
704 	.index		= -1,
705 	.data		= &serial8250_reg,
706 };
707 
708 static int __init univ8250_console_init(void)
709 {
710 	if (nr_uarts == 0)
711 		return -ENODEV;
712 
713 	serial8250_isa_init_ports();
714 	register_console(&univ8250_console);
715 	return 0;
716 }
717 console_initcall(univ8250_console_init);
718 
719 #define SERIAL8250_CONSOLE	(&univ8250_console)
720 #else
721 #define SERIAL8250_CONSOLE	NULL
722 #endif
723 
724 static struct uart_driver serial8250_reg = {
725 	.owner			= THIS_MODULE,
726 	.driver_name		= "serial",
727 	.dev_name		= "ttyS",
728 	.major			= TTY_MAJOR,
729 	.minor			= 64,
730 	.cons			= SERIAL8250_CONSOLE,
731 };
732 
733 /*
734  * early_serial_setup - early registration for 8250 ports
735  *
736  * Setup an 8250 port structure prior to console initialisation.  Use
737  * after console initialisation will cause undefined behaviour.
738  */
739 int __init early_serial_setup(struct uart_port *port)
740 {
741 	struct uart_port *p;
742 
743 	if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
744 		return -ENODEV;
745 
746 	serial8250_isa_init_ports();
747 	p = &serial8250_ports[port->line].port;
748 	p->iobase       = port->iobase;
749 	p->membase      = port->membase;
750 	p->irq          = port->irq;
751 	p->irqflags     = port->irqflags;
752 	p->uartclk      = port->uartclk;
753 	p->fifosize     = port->fifosize;
754 	p->regshift     = port->regshift;
755 	p->iotype       = port->iotype;
756 	p->flags        = port->flags;
757 	p->mapbase      = port->mapbase;
758 	p->mapsize      = port->mapsize;
759 	p->private_data = port->private_data;
760 	p->type		= port->type;
761 	p->line		= port->line;
762 
763 	serial8250_set_defaults(up_to_u8250p(p));
764 
765 	if (port->serial_in)
766 		p->serial_in = port->serial_in;
767 	if (port->serial_out)
768 		p->serial_out = port->serial_out;
769 	if (port->handle_irq)
770 		p->handle_irq = port->handle_irq;
771 
772 	return 0;
773 }
774 
775 /**
776  *	serial8250_suspend_port - suspend one serial port
777  *	@line:  serial line number
778  *
779  *	Suspend one serial port.
780  */
781 void serial8250_suspend_port(int line)
782 {
783 	struct uart_8250_port *up = &serial8250_ports[line];
784 	struct uart_port *port = &up->port;
785 
786 	if (!console_suspend_enabled && uart_console(port) &&
787 	    port->type != PORT_8250) {
788 		unsigned char canary = 0xa5;
789 
790 		serial_out(up, UART_SCR, canary);
791 		if (serial_in(up, UART_SCR) == canary)
792 			up->canary = canary;
793 	}
794 
795 	uart_suspend_port(&serial8250_reg, port);
796 }
797 EXPORT_SYMBOL(serial8250_suspend_port);
798 
799 /**
800  *	serial8250_resume_port - resume one serial port
801  *	@line:  serial line number
802  *
803  *	Resume one serial port.
804  */
805 void serial8250_resume_port(int line)
806 {
807 	struct uart_8250_port *up = &serial8250_ports[line];
808 	struct uart_port *port = &up->port;
809 
810 	up->canary = 0;
811 
812 	if (up->capabilities & UART_NATSEMI) {
813 		/* Ensure it's still in high speed mode */
814 		serial_port_out(port, UART_LCR, 0xE0);
815 
816 		ns16550a_goto_highspeed(up);
817 
818 		serial_port_out(port, UART_LCR, 0);
819 		port->uartclk = 921600*16;
820 	}
821 	uart_resume_port(&serial8250_reg, port);
822 }
823 EXPORT_SYMBOL(serial8250_resume_port);
824 
825 /*
826  * Register a set of serial devices attached to a platform device.  The
827  * list is terminated with a zero flags entry, which means we expect
828  * all entries to have at least UPF_BOOT_AUTOCONF set.
829  */
830 static int serial8250_probe(struct platform_device *dev)
831 {
832 	struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
833 	struct uart_8250_port uart;
834 	int ret, i, irqflag = 0;
835 
836 	memset(&uart, 0, sizeof(uart));
837 
838 	if (share_irqs)
839 		irqflag = IRQF_SHARED;
840 
841 	for (i = 0; p && p->flags != 0; p++, i++) {
842 		uart.port.iobase	= p->iobase;
843 		uart.port.membase	= p->membase;
844 		uart.port.irq		= p->irq;
845 		uart.port.irqflags	= p->irqflags;
846 		uart.port.uartclk	= p->uartclk;
847 		uart.port.regshift	= p->regshift;
848 		uart.port.iotype	= p->iotype;
849 		uart.port.flags		= p->flags;
850 		uart.port.mapbase	= p->mapbase;
851 		uart.port.mapsize	= p->mapsize;
852 		uart.port.hub6		= p->hub6;
853 		uart.port.has_sysrq	= p->has_sysrq;
854 		uart.port.private_data	= p->private_data;
855 		uart.port.type		= p->type;
856 		uart.bugs		= p->bugs;
857 		uart.port.serial_in	= p->serial_in;
858 		uart.port.serial_out	= p->serial_out;
859 		uart.dl_read		= p->dl_read;
860 		uart.dl_write		= p->dl_write;
861 		uart.port.handle_irq	= p->handle_irq;
862 		uart.port.handle_break	= p->handle_break;
863 		uart.port.set_termios	= p->set_termios;
864 		uart.port.set_ldisc	= p->set_ldisc;
865 		uart.port.get_mctrl	= p->get_mctrl;
866 		uart.port.pm		= p->pm;
867 		uart.port.dev		= &dev->dev;
868 		uart.port.irqflags	|= irqflag;
869 		ret = serial8250_register_8250_port(&uart);
870 		if (ret < 0) {
871 			dev_err(&dev->dev, "unable to register port at index %d "
872 				"(IO%lx MEM%llx IRQ%d): %d\n", i,
873 				p->iobase, (unsigned long long)p->mapbase,
874 				p->irq, ret);
875 		}
876 	}
877 	return 0;
878 }
879 
880 /*
881  * Remove serial ports registered against a platform device.
882  */
883 static void serial8250_remove(struct platform_device *dev)
884 {
885 	int i;
886 
887 	for (i = 0; i < nr_uarts; i++) {
888 		struct uart_8250_port *up = &serial8250_ports[i];
889 
890 		if (up->port.dev == &dev->dev)
891 			serial8250_unregister_port(i);
892 	}
893 }
894 
895 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
896 {
897 	int i;
898 
899 	for (i = 0; i < UART_NR; i++) {
900 		struct uart_8250_port *up = &serial8250_ports[i];
901 
902 		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
903 			uart_suspend_port(&serial8250_reg, &up->port);
904 	}
905 
906 	return 0;
907 }
908 
909 static int serial8250_resume(struct platform_device *dev)
910 {
911 	int i;
912 
913 	for (i = 0; i < UART_NR; i++) {
914 		struct uart_8250_port *up = &serial8250_ports[i];
915 
916 		if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
917 			serial8250_resume_port(i);
918 	}
919 
920 	return 0;
921 }
922 
923 static struct platform_driver serial8250_isa_driver = {
924 	.probe		= serial8250_probe,
925 	.remove_new	= serial8250_remove,
926 	.suspend	= serial8250_suspend,
927 	.resume		= serial8250_resume,
928 	.driver		= {
929 		.name	= "serial8250",
930 	},
931 };
932 
933 /*
934  * This "device" covers _all_ ISA 8250-compatible serial devices listed
935  * in the table in include/asm/serial.h
936  */
937 static struct platform_device *serial8250_isa_devs;
938 
939 /*
940  * serial8250_register_8250_port and serial8250_unregister_port allows for
941  * 16x50 serial ports to be configured at run-time, to support PCMCIA
942  * modems and PCI multiport cards.
943  */
944 static DEFINE_MUTEX(serial_mutex);
945 
946 static struct uart_8250_port *serial8250_find_match_or_unused(const struct uart_port *port)
947 {
948 	int i;
949 
950 	/*
951 	 * First, find a port entry which matches.
952 	 */
953 	for (i = 0; i < nr_uarts; i++)
954 		if (uart_match_port(&serial8250_ports[i].port, port))
955 			return &serial8250_ports[i];
956 
957 	/* try line number first if still available */
958 	i = port->line;
959 	if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
960 			serial8250_ports[i].port.iobase == 0)
961 		return &serial8250_ports[i];
962 	/*
963 	 * We didn't find a matching entry, so look for the first
964 	 * free entry.  We look for one which hasn't been previously
965 	 * used (indicated by zero iobase).
966 	 */
967 	for (i = 0; i < nr_uarts; i++)
968 		if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
969 		    serial8250_ports[i].port.iobase == 0)
970 			return &serial8250_ports[i];
971 
972 	/*
973 	 * That also failed.  Last resort is to find any entry which
974 	 * doesn't have a real port associated with it.
975 	 */
976 	for (i = 0; i < nr_uarts; i++)
977 		if (serial8250_ports[i].port.type == PORT_UNKNOWN)
978 			return &serial8250_ports[i];
979 
980 	return NULL;
981 }
982 
983 static void serial_8250_overrun_backoff_work(struct work_struct *work)
984 {
985 	struct uart_8250_port *up =
986 	    container_of(to_delayed_work(work), struct uart_8250_port,
987 			 overrun_backoff);
988 	struct uart_port *port = &up->port;
989 	unsigned long flags;
990 
991 	uart_port_lock_irqsave(port, &flags);
992 	up->ier |= UART_IER_RLSI | UART_IER_RDI;
993 	up->port.read_status_mask |= UART_LSR_DR;
994 	serial_out(up, UART_IER, up->ier);
995 	uart_port_unlock_irqrestore(port, flags);
996 }
997 
998 /**
999  *	serial8250_register_8250_port - register a serial port
1000  *	@up: serial port template
1001  *
1002  *	Configure the serial port specified by the request. If the
1003  *	port exists and is in use, it is hung up and unregistered
1004  *	first.
1005  *
1006  *	The port is then probed and if necessary the IRQ is autodetected
1007  *	If this fails an error is returned.
1008  *
1009  *	On success the port is ready to use and the line number is returned.
1010  */
1011 int serial8250_register_8250_port(const struct uart_8250_port *up)
1012 {
1013 	struct uart_8250_port *uart;
1014 	int ret = -ENOSPC;
1015 
1016 	if (up->port.uartclk == 0)
1017 		return -EINVAL;
1018 
1019 	mutex_lock(&serial_mutex);
1020 
1021 	uart = serial8250_find_match_or_unused(&up->port);
1022 	if (!uart) {
1023 		/*
1024 		 * If the port is past the initial isa ports, initialize a new
1025 		 * port and increment nr_uarts accordingly.
1026 		 */
1027 		uart = serial8250_setup_port(nr_uarts);
1028 		if (!uart)
1029 			goto unlock;
1030 		nr_uarts++;
1031 	}
1032 
1033 	if (uart->port.type != PORT_8250_CIR) {
1034 		struct mctrl_gpios *gpios;
1035 
1036 		if (uart->port.dev)
1037 			uart_remove_one_port(&serial8250_reg, &uart->port);
1038 
1039 		uart->port.ctrl_id	= up->port.ctrl_id;
1040 		uart->port.port_id	= up->port.port_id;
1041 		uart->port.iobase       = up->port.iobase;
1042 		uart->port.membase      = up->port.membase;
1043 		uart->port.irq          = up->port.irq;
1044 		uart->port.irqflags     = up->port.irqflags;
1045 		uart->port.uartclk      = up->port.uartclk;
1046 		uart->port.fifosize     = up->port.fifosize;
1047 		uart->port.regshift     = up->port.regshift;
1048 		uart->port.iotype       = up->port.iotype;
1049 		uart->port.flags        = up->port.flags | UPF_BOOT_AUTOCONF;
1050 		uart->bugs		= up->bugs;
1051 		uart->port.mapbase      = up->port.mapbase;
1052 		uart->port.mapsize      = up->port.mapsize;
1053 		uart->port.private_data = up->port.private_data;
1054 		uart->tx_loadsz		= up->tx_loadsz;
1055 		uart->capabilities	= up->capabilities;
1056 		uart->port.throttle	= up->port.throttle;
1057 		uart->port.unthrottle	= up->port.unthrottle;
1058 		uart->port.rs485_config	= up->port.rs485_config;
1059 		uart->port.rs485_supported = up->port.rs485_supported;
1060 		uart->port.rs485	= up->port.rs485;
1061 		uart->rs485_start_tx	= up->rs485_start_tx;
1062 		uart->rs485_stop_tx	= up->rs485_stop_tx;
1063 		uart->lsr_save_mask	= up->lsr_save_mask;
1064 		uart->dma		= up->dma;
1065 
1066 		/* Take tx_loadsz from fifosize if it wasn't set separately */
1067 		if (uart->port.fifosize && !uart->tx_loadsz)
1068 			uart->tx_loadsz = uart->port.fifosize;
1069 
1070 		if (up->port.dev) {
1071 			uart->port.dev = up->port.dev;
1072 			ret = uart_get_rs485_mode(&uart->port);
1073 			if (ret)
1074 				goto err;
1075 		}
1076 
1077 		if (up->port.flags & UPF_FIXED_TYPE)
1078 			uart->port.type = up->port.type;
1079 
1080 		/*
1081 		 * Only call mctrl_gpio_init(), if the device has no ACPI
1082 		 * companion device
1083 		 */
1084 		if (!has_acpi_companion(uart->port.dev)) {
1085 			gpios = mctrl_gpio_init(&uart->port, 0);
1086 			if (IS_ERR(gpios)) {
1087 				ret = PTR_ERR(gpios);
1088 				goto err;
1089 			} else {
1090 				uart->gpios = gpios;
1091 			}
1092 		}
1093 
1094 		serial8250_set_defaults(uart);
1095 
1096 		/* Possibly override default I/O functions.  */
1097 		if (up->port.serial_in)
1098 			uart->port.serial_in = up->port.serial_in;
1099 		if (up->port.serial_out)
1100 			uart->port.serial_out = up->port.serial_out;
1101 		if (up->port.handle_irq)
1102 			uart->port.handle_irq = up->port.handle_irq;
1103 		/*  Possibly override set_termios call */
1104 		if (up->port.set_termios)
1105 			uart->port.set_termios = up->port.set_termios;
1106 		if (up->port.set_ldisc)
1107 			uart->port.set_ldisc = up->port.set_ldisc;
1108 		if (up->port.get_mctrl)
1109 			uart->port.get_mctrl = up->port.get_mctrl;
1110 		if (up->port.set_mctrl)
1111 			uart->port.set_mctrl = up->port.set_mctrl;
1112 		if (up->port.get_divisor)
1113 			uart->port.get_divisor = up->port.get_divisor;
1114 		if (up->port.set_divisor)
1115 			uart->port.set_divisor = up->port.set_divisor;
1116 		if (up->port.startup)
1117 			uart->port.startup = up->port.startup;
1118 		if (up->port.shutdown)
1119 			uart->port.shutdown = up->port.shutdown;
1120 		if (up->port.pm)
1121 			uart->port.pm = up->port.pm;
1122 		if (up->port.handle_break)
1123 			uart->port.handle_break = up->port.handle_break;
1124 		if (up->dl_read)
1125 			uart->dl_read = up->dl_read;
1126 		if (up->dl_write)
1127 			uart->dl_write = up->dl_write;
1128 
1129 		if (uart->port.type != PORT_8250_CIR) {
1130 			if (serial8250_isa_config != NULL)
1131 				serial8250_isa_config(0, &uart->port,
1132 						&uart->capabilities);
1133 
1134 			serial8250_apply_quirks(uart);
1135 			ret = uart_add_one_port(&serial8250_reg,
1136 						&uart->port);
1137 			if (ret)
1138 				goto err;
1139 
1140 			ret = uart->port.line;
1141 		} else {
1142 			dev_info(uart->port.dev,
1143 				"skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
1144 				uart->port.iobase,
1145 				(unsigned long long)uart->port.mapbase,
1146 				uart->port.irq);
1147 
1148 			ret = 0;
1149 		}
1150 
1151 		if (!uart->lsr_save_mask)
1152 			uart->lsr_save_mask = LSR_SAVE_FLAGS;	/* Use default LSR mask */
1153 
1154 		/* Initialise interrupt backoff work if required */
1155 		if (up->overrun_backoff_time_ms > 0) {
1156 			uart->overrun_backoff_time_ms =
1157 				up->overrun_backoff_time_ms;
1158 			INIT_DELAYED_WORK(&uart->overrun_backoff,
1159 					serial_8250_overrun_backoff_work);
1160 		} else {
1161 			uart->overrun_backoff_time_ms = 0;
1162 		}
1163 	}
1164 
1165 unlock:
1166 	mutex_unlock(&serial_mutex);
1167 
1168 	return ret;
1169 
1170 err:
1171 	uart->port.dev = NULL;
1172 	mutex_unlock(&serial_mutex);
1173 	return ret;
1174 }
1175 EXPORT_SYMBOL(serial8250_register_8250_port);
1176 
1177 /**
1178  *	serial8250_unregister_port - remove a 16x50 serial port at runtime
1179  *	@line: serial line number
1180  *
1181  *	Remove one serial port.  This may not be called from interrupt
1182  *	context.  We hand the port back to the our control.
1183  */
1184 void serial8250_unregister_port(int line)
1185 {
1186 	struct uart_8250_port *uart = &serial8250_ports[line];
1187 
1188 	mutex_lock(&serial_mutex);
1189 
1190 	if (uart->em485) {
1191 		unsigned long flags;
1192 
1193 		uart_port_lock_irqsave(&uart->port, &flags);
1194 		serial8250_em485_destroy(uart);
1195 		uart_port_unlock_irqrestore(&uart->port, flags);
1196 	}
1197 
1198 	uart_remove_one_port(&serial8250_reg, &uart->port);
1199 	if (serial8250_isa_devs) {
1200 		uart->port.flags &= ~UPF_BOOT_AUTOCONF;
1201 		uart->port.type = PORT_UNKNOWN;
1202 		uart->port.dev = &serial8250_isa_devs->dev;
1203 		uart->port.port_id = line;
1204 		uart->capabilities = 0;
1205 		serial8250_init_port(uart);
1206 		serial8250_apply_quirks(uart);
1207 		uart_add_one_port(&serial8250_reg, &uart->port);
1208 	} else {
1209 		uart->port.dev = NULL;
1210 	}
1211 	mutex_unlock(&serial_mutex);
1212 }
1213 EXPORT_SYMBOL(serial8250_unregister_port);
1214 
1215 static int __init serial8250_init(void)
1216 {
1217 	int ret;
1218 
1219 	if (nr_uarts == 0)
1220 		return -ENODEV;
1221 
1222 	serial8250_isa_init_ports();
1223 
1224 	pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %s\n",
1225 		nr_uarts, str_enabled_disabled(share_irqs));
1226 
1227 #ifdef CONFIG_SPARC
1228 	ret = sunserial_register_minors(&serial8250_reg, UART_NR);
1229 #else
1230 	serial8250_reg.nr = UART_NR;
1231 	ret = uart_register_driver(&serial8250_reg);
1232 #endif
1233 	if (ret)
1234 		goto out;
1235 
1236 	ret = serial8250_pnp_init();
1237 	if (ret)
1238 		goto unreg_uart_drv;
1239 
1240 	serial8250_isa_devs = platform_device_alloc("serial8250",
1241 						    PLAT8250_DEV_LEGACY);
1242 	if (!serial8250_isa_devs) {
1243 		ret = -ENOMEM;
1244 		goto unreg_pnp;
1245 	}
1246 
1247 	ret = platform_device_add(serial8250_isa_devs);
1248 	if (ret)
1249 		goto put_dev;
1250 
1251 	serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
1252 
1253 	ret = platform_driver_register(&serial8250_isa_driver);
1254 	if (ret == 0)
1255 		goto out;
1256 
1257 	platform_device_del(serial8250_isa_devs);
1258 put_dev:
1259 	platform_device_put(serial8250_isa_devs);
1260 unreg_pnp:
1261 	serial8250_pnp_exit();
1262 unreg_uart_drv:
1263 #ifdef CONFIG_SPARC
1264 	sunserial_unregister_minors(&serial8250_reg, UART_NR);
1265 #else
1266 	uart_unregister_driver(&serial8250_reg);
1267 #endif
1268 out:
1269 	return ret;
1270 }
1271 
1272 static void __exit serial8250_exit(void)
1273 {
1274 	struct platform_device *isa_dev = serial8250_isa_devs;
1275 
1276 	/*
1277 	 * This tells serial8250_unregister_port() not to re-register
1278 	 * the ports (thereby making serial8250_isa_driver permanently
1279 	 * in use.)
1280 	 */
1281 	serial8250_isa_devs = NULL;
1282 
1283 	platform_driver_unregister(&serial8250_isa_driver);
1284 	platform_device_unregister(isa_dev);
1285 
1286 	serial8250_pnp_exit();
1287 
1288 #ifdef CONFIG_SPARC
1289 	sunserial_unregister_minors(&serial8250_reg, UART_NR);
1290 #else
1291 	uart_unregister_driver(&serial8250_reg);
1292 #endif
1293 }
1294 
1295 module_init(serial8250_init);
1296 module_exit(serial8250_exit);
1297 
1298 MODULE_LICENSE("GPL");
1299 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
1300 
1301 module_param_hw(share_irqs, uint, other, 0644);
1302 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
1303 
1304 module_param(nr_uarts, uint, 0644);
1305 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
1306 
1307 module_param(skip_txen_test, uint, 0644);
1308 MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
1309 
1310 #ifdef CONFIG_SERIAL_8250_RSA
1311 module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
1312 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
1313 #endif
1314 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
1315 
1316 #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
1317 #ifndef MODULE
1318 /* This module was renamed to 8250_core in 3.7.  Keep the old "8250" name
1319  * working as well for the module options so we don't break people.  We
1320  * need to keep the names identical and the convenient macros will happily
1321  * refuse to let us do that by failing the build with redefinition errors
1322  * of global variables.  So we stick them inside a dummy function to avoid
1323  * those conflicts.  The options still get parsed, and the redefined
1324  * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
1325  *
1326  * This is hacky.  I'm sorry.
1327  */
1328 static void __used s8250_options(void)
1329 {
1330 #undef MODULE_PARAM_PREFIX
1331 #define MODULE_PARAM_PREFIX "8250_core."
1332 
1333 	module_param_cb(share_irqs, &param_ops_uint, &share_irqs, 0644);
1334 	module_param_cb(nr_uarts, &param_ops_uint, &nr_uarts, 0644);
1335 	module_param_cb(skip_txen_test, &param_ops_uint, &skip_txen_test, 0644);
1336 #ifdef CONFIG_SERIAL_8250_RSA
1337 	__module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
1338 		&param_array_ops, .arr = &__param_arr_probe_rsa,
1339 		0444, -1, 0);
1340 #endif
1341 }
1342 #else
1343 MODULE_ALIAS("8250_core");
1344 #endif
1345 #endif
1346