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