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