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