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