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