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