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