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