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