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