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