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