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