1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/acpi.h>
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20#include <linux/ioport.h>
21#include <linux/init.h>
22#include <linux/console.h>
23#include <linux/sysrq.h>
24#include <linux/delay.h>
25#include <linux/platform_device.h>
26#include <linux/tty.h>
27#include <linux/ratelimit.h>
28#include <linux/tty_flip.h>
29#include <linux/serial.h>
30#include <linux/serial_8250.h>
31#include <linux/nmi.h>
32#include <linux/mutex.h>
33#include <linux/slab.h>
34#include <linux/uaccess.h>
35#include <linux/pm_runtime.h>
36#include <linux/io.h>
37#ifdef CONFIG_SPARC
38#include <linux/sunserialcore.h>
39#endif
40
41#include <asm/irq.h>
42
43#include "8250.h"
44
45
46
47
48
49
50static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
51
52static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
53
54static struct uart_driver serial8250_reg;
55
56static unsigned int skip_txen_test;
57
58#define PASS_LIMIT 512
59
60#include <asm/serial.h>
61
62
63
64
65
66#ifndef SERIAL_PORT_DFNS
67#define SERIAL_PORT_DFNS
68#endif
69
70static const struct old_serial_port old_serial_port[] = {
71 SERIAL_PORT_DFNS
72};
73
74#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
75
76#ifdef CONFIG_SERIAL_8250_RSA
77
78#define PORT_RSA_MAX 4
79static unsigned long probe_rsa[PORT_RSA_MAX];
80static unsigned int probe_rsa_count;
81#endif
82
83struct irq_info {
84 struct hlist_node node;
85 int irq;
86 spinlock_t lock;
87 struct list_head *head;
88};
89
90#define NR_IRQ_HASH 32
91static struct hlist_head irq_lists[NR_IRQ_HASH];
92static DEFINE_MUTEX(hash_mutex);
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
109{
110 struct irq_info *i = dev_id;
111 struct list_head *l, *end = NULL;
112 int pass_counter = 0, handled = 0;
113
114 pr_debug("%s(%d): start\n", __func__, irq);
115
116 spin_lock(&i->lock);
117
118 l = i->head;
119 do {
120 struct uart_8250_port *up;
121 struct uart_port *port;
122
123 up = list_entry(l, struct uart_8250_port, list);
124 port = &up->port;
125
126 if (port->handle_irq(port)) {
127 handled = 1;
128 end = NULL;
129 } else if (end == NULL)
130 end = l;
131
132 l = l->next;
133
134 if (l == i->head && pass_counter++ > PASS_LIMIT)
135 break;
136 } while (l != end);
137
138 spin_unlock(&i->lock);
139
140 pr_debug("%s(%d): end\n", __func__, irq);
141
142 return IRQ_RETVAL(handled);
143}
144
145
146
147
148
149
150
151
152static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
153{
154 spin_lock_irq(&i->lock);
155
156 if (!list_empty(i->head)) {
157 if (i->head == &up->list)
158 i->head = i->head->next;
159 list_del(&up->list);
160 } else {
161 BUG_ON(i->head != &up->list);
162 i->head = NULL;
163 }
164 spin_unlock_irq(&i->lock);
165
166 if (i->head == NULL) {
167 hlist_del(&i->node);
168 kfree(i);
169 }
170}
171
172static int serial_link_irq_chain(struct uart_8250_port *up)
173{
174 struct hlist_head *h;
175 struct hlist_node *n;
176 struct irq_info *i;
177 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
178
179 mutex_lock(&hash_mutex);
180
181 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
182
183 hlist_for_each(n, h) {
184 i = hlist_entry(n, struct irq_info, node);
185 if (i->irq == up->port.irq)
186 break;
187 }
188
189 if (n == NULL) {
190 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
191 if (i == NULL) {
192 mutex_unlock(&hash_mutex);
193 return -ENOMEM;
194 }
195 spin_lock_init(&i->lock);
196 i->irq = up->port.irq;
197 hlist_add_head(&i->node, h);
198 }
199 mutex_unlock(&hash_mutex);
200
201 spin_lock_irq(&i->lock);
202
203 if (i->head) {
204 list_add(&up->list, i->head);
205 spin_unlock_irq(&i->lock);
206
207 ret = 0;
208 } else {
209 INIT_LIST_HEAD(&up->list);
210 i->head = &up->list;
211 spin_unlock_irq(&i->lock);
212 irq_flags |= up->port.irqflags;
213 ret = request_irq(up->port.irq, serial8250_interrupt,
214 irq_flags, up->port.name, i);
215 if (ret < 0)
216 serial_do_unlink(i, up);
217 }
218
219 return ret;
220}
221
222static void serial_unlink_irq_chain(struct uart_8250_port *up)
223{
224
225
226
227
228 struct irq_info *i;
229 struct hlist_node *n;
230 struct hlist_head *h;
231
232 mutex_lock(&hash_mutex);
233
234 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
235
236 hlist_for_each(n, h) {
237 i = hlist_entry(n, struct irq_info, node);
238 if (i->irq == up->port.irq)
239 break;
240 }
241
242 BUG_ON(n == NULL);
243 BUG_ON(i->head == NULL);
244
245 if (list_empty(i->head))
246 free_irq(up->port.irq, i);
247
248 serial_do_unlink(i, up);
249 mutex_unlock(&hash_mutex);
250}
251
252
253
254
255
256
257
258static void serial8250_timeout(struct timer_list *t)
259{
260 struct uart_8250_port *up = from_timer(up, t, timer);
261
262 up->port.handle_irq(&up->port);
263 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
264}
265
266static void serial8250_backup_timeout(struct timer_list *t)
267{
268 struct uart_8250_port *up = from_timer(up, t, timer);
269 unsigned int iir, ier = 0, lsr;
270 unsigned long flags;
271
272 spin_lock_irqsave(&up->port.lock, flags);
273
274
275
276
277
278 if (up->port.irq) {
279 ier = serial_in(up, UART_IER);
280 serial_out(up, UART_IER, 0);
281 }
282
283 iir = serial_in(up, UART_IIR);
284
285
286
287
288
289
290
291 lsr = serial_in(up, UART_LSR);
292 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
293 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
294 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
295 (lsr & UART_LSR_THRE)) {
296 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
297 iir |= UART_IIR_THRI;
298 }
299
300 if (!(iir & UART_IIR_NO_INT))
301 serial8250_tx_chars(up);
302
303 if (up->port.irq)
304 serial_out(up, UART_IER, ier);
305
306 spin_unlock_irqrestore(&up->port.lock, flags);
307
308
309 mod_timer(&up->timer,
310 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
311}
312
313static int univ8250_setup_irq(struct uart_8250_port *up)
314{
315 struct uart_port *port = &up->port;
316 int retval = 0;
317
318
319
320
321
322 if (up->bugs & UART_BUG_THRE) {
323 pr_debug("%s - using backup timer\n", port->name);
324
325 up->timer.function = serial8250_backup_timeout;
326 mod_timer(&up->timer, jiffies +
327 uart_poll_timeout(port) + HZ / 5);
328 }
329
330
331
332
333
334
335 if (!port->irq) {
336 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
337 } else
338 retval = serial_link_irq_chain(up);
339
340 return retval;
341}
342
343static void univ8250_release_irq(struct uart_8250_port *up)
344{
345 struct uart_port *port = &up->port;
346
347 del_timer_sync(&up->timer);
348 up->timer.function = serial8250_timeout;
349 if (port->irq)
350 serial_unlink_irq_chain(up);
351}
352
353#ifdef CONFIG_SERIAL_8250_RSA
354static int serial8250_request_rsa_resource(struct uart_8250_port *up)
355{
356 unsigned long start = UART_RSA_BASE << up->port.regshift;
357 unsigned int size = 8 << up->port.regshift;
358 struct uart_port *port = &up->port;
359 int ret = -EINVAL;
360
361 switch (port->iotype) {
362 case UPIO_HUB6:
363 case UPIO_PORT:
364 start += port->iobase;
365 if (request_region(start, size, "serial-rsa"))
366 ret = 0;
367 else
368 ret = -EBUSY;
369 break;
370 }
371
372 return ret;
373}
374
375static void serial8250_release_rsa_resource(struct uart_8250_port *up)
376{
377 unsigned long offset = UART_RSA_BASE << up->port.regshift;
378 unsigned int size = 8 << up->port.regshift;
379 struct uart_port *port = &up->port;
380
381 switch (port->iotype) {
382 case UPIO_HUB6:
383 case UPIO_PORT:
384 release_region(port->iobase + offset, size);
385 break;
386 }
387}
388#endif
389
390static const struct uart_ops *base_ops;
391static struct uart_ops univ8250_port_ops;
392
393static const struct uart_8250_ops univ8250_driver_ops = {
394 .setup_irq = univ8250_setup_irq,
395 .release_irq = univ8250_release_irq,
396};
397
398static struct uart_8250_port serial8250_ports[UART_NR];
399
400
401
402
403
404
405
406
407
408
409
410
411
412struct uart_8250_port *serial8250_get_port(int line)
413{
414 return &serial8250_ports[line];
415}
416EXPORT_SYMBOL_GPL(serial8250_get_port);
417
418static void (*serial8250_isa_config)(int port, struct uart_port *up,
419 u32 *capabilities);
420
421void serial8250_set_isa_configurator(
422 void (*v)(int port, struct uart_port *up, u32 *capabilities))
423{
424 serial8250_isa_config = v;
425}
426EXPORT_SYMBOL(serial8250_set_isa_configurator);
427
428#ifdef CONFIG_SERIAL_8250_RSA
429
430static void univ8250_config_port(struct uart_port *port, int flags)
431{
432 struct uart_8250_port *up = up_to_u8250p(port);
433
434 up->probe &= ~UART_PROBE_RSA;
435 if (port->type == PORT_RSA) {
436 if (serial8250_request_rsa_resource(up) == 0)
437 up->probe |= UART_PROBE_RSA;
438 } else if (flags & UART_CONFIG_TYPE) {
439 int i;
440
441 for (i = 0; i < probe_rsa_count; i++) {
442 if (probe_rsa[i] == up->port.iobase) {
443 if (serial8250_request_rsa_resource(up) == 0)
444 up->probe |= UART_PROBE_RSA;
445 break;
446 }
447 }
448 }
449
450 base_ops->config_port(port, flags);
451
452 if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
453 serial8250_release_rsa_resource(up);
454}
455
456static int univ8250_request_port(struct uart_port *port)
457{
458 struct uart_8250_port *up = up_to_u8250p(port);
459 int ret;
460
461 ret = base_ops->request_port(port);
462 if (ret == 0 && port->type == PORT_RSA) {
463 ret = serial8250_request_rsa_resource(up);
464 if (ret < 0)
465 base_ops->release_port(port);
466 }
467
468 return ret;
469}
470
471static void univ8250_release_port(struct uart_port *port)
472{
473 struct uart_8250_port *up = up_to_u8250p(port);
474
475 if (port->type == PORT_RSA)
476 serial8250_release_rsa_resource(up);
477 base_ops->release_port(port);
478}
479
480static void univ8250_rsa_support(struct uart_ops *ops)
481{
482 ops->config_port = univ8250_config_port;
483 ops->request_port = univ8250_request_port;
484 ops->release_port = univ8250_release_port;
485}
486
487#else
488#define univ8250_rsa_support(x) do { } while (0)
489#endif
490
491static inline void serial8250_apply_quirks(struct uart_8250_port *up)
492{
493 up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0;
494}
495
496static void __init serial8250_isa_init_ports(void)
497{
498 struct uart_8250_port *up;
499 static int first = 1;
500 int i, irqflag = 0;
501
502 if (!first)
503 return;
504 first = 0;
505
506 if (nr_uarts > UART_NR)
507 nr_uarts = UART_NR;
508
509 for (i = 0; i < nr_uarts; i++) {
510 struct uart_8250_port *up = &serial8250_ports[i];
511 struct uart_port *port = &up->port;
512
513 port->line = i;
514 serial8250_init_port(up);
515 if (!base_ops)
516 base_ops = port->ops;
517 port->ops = &univ8250_port_ops;
518
519 timer_setup(&up->timer, serial8250_timeout, 0);
520
521 up->ops = &univ8250_driver_ops;
522
523
524
525
526 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
527 up->mcr_force = ALPHA_KLUDGE_MCR;
528 }
529
530
531 univ8250_port_ops = *base_ops;
532 univ8250_rsa_support(&univ8250_port_ops);
533
534 if (share_irqs)
535 irqflag = IRQF_SHARED;
536
537 for (i = 0, up = serial8250_ports;
538 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
539 i++, up++) {
540 struct uart_port *port = &up->port;
541
542 port->iobase = old_serial_port[i].port;
543 port->irq = irq_canonicalize(old_serial_port[i].irq);
544 port->irqflags = 0;
545 port->uartclk = old_serial_port[i].baud_base * 16;
546 port->flags = old_serial_port[i].flags;
547 port->hub6 = 0;
548 port->membase = old_serial_port[i].iomem_base;
549 port->iotype = old_serial_port[i].io_type;
550 port->regshift = old_serial_port[i].iomem_reg_shift;
551 serial8250_set_defaults(up);
552
553 port->irqflags |= irqflag;
554 if (serial8250_isa_config != NULL)
555 serial8250_isa_config(i, &up->port, &up->capabilities);
556 }
557}
558
559static void __init
560serial8250_register_ports(struct uart_driver *drv, struct device *dev)
561{
562 int i;
563
564 for (i = 0; i < nr_uarts; i++) {
565 struct uart_8250_port *up = &serial8250_ports[i];
566
567 if (up->port.type == PORT_8250_CIR)
568 continue;
569
570 if (up->port.dev)
571 continue;
572
573 up->port.dev = dev;
574
575 serial8250_apply_quirks(up);
576 uart_add_one_port(drv, &up->port);
577 }
578}
579
580#ifdef CONFIG_SERIAL_8250_CONSOLE
581
582static void univ8250_console_write(struct console *co, const char *s,
583 unsigned int count)
584{
585 struct uart_8250_port *up = &serial8250_ports[co->index];
586
587 serial8250_console_write(up, s, count);
588}
589
590static int univ8250_console_setup(struct console *co, char *options)
591{
592 struct uart_port *port;
593 int retval;
594
595
596
597
598
599
600 if (co->index >= nr_uarts)
601 co->index = 0;
602 port = &serial8250_ports[co->index].port;
603
604 port->cons = co;
605
606 retval = serial8250_console_setup(port, options, false);
607 if (retval != 0)
608 port->cons = NULL;
609 return retval;
610}
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630static int univ8250_console_match(struct console *co, char *name, int idx,
631 char *options)
632{
633 char match[] = "uart";
634 unsigned char iotype;
635 resource_size_t addr;
636 int i;
637
638 if (strncmp(name, match, 4) != 0)
639 return -ENODEV;
640
641 if (uart_parse_earlycon(options, &iotype, &addr, &options))
642 return -ENODEV;
643
644
645 for (i = 0; i < nr_uarts; i++) {
646 struct uart_port *port = &serial8250_ports[i].port;
647
648 if (port->iotype != iotype)
649 continue;
650 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
651 iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
652 && (port->mapbase != addr))
653 continue;
654 if (iotype == UPIO_PORT && port->iobase != addr)
655 continue;
656
657 co->index = i;
658 port->cons = co;
659 return serial8250_console_setup(port, options, true);
660 }
661
662 return -ENODEV;
663}
664
665static struct console univ8250_console = {
666 .name = "ttyS",
667 .write = univ8250_console_write,
668 .device = uart_console_device,
669 .setup = univ8250_console_setup,
670 .match = univ8250_console_match,
671 .flags = CON_PRINTBUFFER | CON_ANYTIME,
672 .index = -1,
673 .data = &serial8250_reg,
674};
675
676static int __init univ8250_console_init(void)
677{
678 if (nr_uarts == 0)
679 return -ENODEV;
680
681 serial8250_isa_init_ports();
682 register_console(&univ8250_console);
683 return 0;
684}
685console_initcall(univ8250_console_init);
686
687#define SERIAL8250_CONSOLE (&univ8250_console)
688#else
689#define SERIAL8250_CONSOLE NULL
690#endif
691
692static struct uart_driver serial8250_reg = {
693 .owner = THIS_MODULE,
694 .driver_name = "serial",
695 .dev_name = "ttyS",
696 .major = TTY_MAJOR,
697 .minor = 64,
698 .cons = SERIAL8250_CONSOLE,
699};
700
701
702
703
704
705
706
707int __init early_serial_setup(struct uart_port *port)
708{
709 struct uart_port *p;
710
711 if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
712 return -ENODEV;
713
714 serial8250_isa_init_ports();
715 p = &serial8250_ports[port->line].port;
716 p->iobase = port->iobase;
717 p->membase = port->membase;
718 p->irq = port->irq;
719 p->irqflags = port->irqflags;
720 p->uartclk = port->uartclk;
721 p->fifosize = port->fifosize;
722 p->regshift = port->regshift;
723 p->iotype = port->iotype;
724 p->flags = port->flags;
725 p->mapbase = port->mapbase;
726 p->mapsize = port->mapsize;
727 p->private_data = port->private_data;
728 p->type = port->type;
729 p->line = port->line;
730
731 serial8250_set_defaults(up_to_u8250p(p));
732
733 if (port->serial_in)
734 p->serial_in = port->serial_in;
735 if (port->serial_out)
736 p->serial_out = port->serial_out;
737 if (port->handle_irq)
738 p->handle_irq = port->handle_irq;
739
740 return 0;
741}
742
743
744
745
746
747
748
749void serial8250_suspend_port(int line)
750{
751 struct uart_8250_port *up = &serial8250_ports[line];
752 struct uart_port *port = &up->port;
753
754 if (!console_suspend_enabled && uart_console(port) &&
755 port->type != PORT_8250) {
756 unsigned char canary = 0xa5;
757 serial_out(up, UART_SCR, canary);
758 if (serial_in(up, UART_SCR) == canary)
759 up->canary = canary;
760 }
761
762 uart_suspend_port(&serial8250_reg, port);
763}
764EXPORT_SYMBOL(serial8250_suspend_port);
765
766
767
768
769
770
771
772void serial8250_resume_port(int line)
773{
774 struct uart_8250_port *up = &serial8250_ports[line];
775 struct uart_port *port = &up->port;
776
777 up->canary = 0;
778
779 if (up->capabilities & UART_NATSEMI) {
780
781 serial_port_out(port, UART_LCR, 0xE0);
782
783 ns16550a_goto_highspeed(up);
784
785 serial_port_out(port, UART_LCR, 0);
786 port->uartclk = 921600*16;
787 }
788 uart_resume_port(&serial8250_reg, port);
789}
790EXPORT_SYMBOL(serial8250_resume_port);
791
792
793
794
795
796
797static int serial8250_probe(struct platform_device *dev)
798{
799 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
800 struct uart_8250_port uart;
801 int ret, i, irqflag = 0;
802
803 memset(&uart, 0, sizeof(uart));
804
805 if (share_irqs)
806 irqflag = IRQF_SHARED;
807
808 for (i = 0; p && p->flags != 0; p++, i++) {
809 uart.port.iobase = p->iobase;
810 uart.port.membase = p->membase;
811 uart.port.irq = p->irq;
812 uart.port.irqflags = p->irqflags;
813 uart.port.uartclk = p->uartclk;
814 uart.port.regshift = p->regshift;
815 uart.port.iotype = p->iotype;
816 uart.port.flags = p->flags;
817 uart.port.mapbase = p->mapbase;
818 uart.port.hub6 = p->hub6;
819 uart.port.private_data = p->private_data;
820 uart.port.type = p->type;
821 uart.port.serial_in = p->serial_in;
822 uart.port.serial_out = p->serial_out;
823 uart.port.handle_irq = p->handle_irq;
824 uart.port.handle_break = p->handle_break;
825 uart.port.set_termios = p->set_termios;
826 uart.port.set_ldisc = p->set_ldisc;
827 uart.port.get_mctrl = p->get_mctrl;
828 uart.port.pm = p->pm;
829 uart.port.dev = &dev->dev;
830 uart.port.irqflags |= irqflag;
831 ret = serial8250_register_8250_port(&uart);
832 if (ret < 0) {
833 dev_err(&dev->dev, "unable to register port at index %d "
834 "(IO%lx MEM%llx IRQ%d): %d\n", i,
835 p->iobase, (unsigned long long)p->mapbase,
836 p->irq, ret);
837 }
838 }
839 return 0;
840}
841
842
843
844
845static int serial8250_remove(struct platform_device *dev)
846{
847 int i;
848
849 for (i = 0; i < nr_uarts; i++) {
850 struct uart_8250_port *up = &serial8250_ports[i];
851
852 if (up->port.dev == &dev->dev)
853 serial8250_unregister_port(i);
854 }
855 return 0;
856}
857
858static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
859{
860 int i;
861
862 for (i = 0; i < UART_NR; i++) {
863 struct uart_8250_port *up = &serial8250_ports[i];
864
865 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
866 uart_suspend_port(&serial8250_reg, &up->port);
867 }
868
869 return 0;
870}
871
872static int serial8250_resume(struct platform_device *dev)
873{
874 int i;
875
876 for (i = 0; i < UART_NR; i++) {
877 struct uart_8250_port *up = &serial8250_ports[i];
878
879 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
880 serial8250_resume_port(i);
881 }
882
883 return 0;
884}
885
886static struct platform_driver serial8250_isa_driver = {
887 .probe = serial8250_probe,
888 .remove = serial8250_remove,
889 .suspend = serial8250_suspend,
890 .resume = serial8250_resume,
891 .driver = {
892 .name = "serial8250",
893 },
894};
895
896
897
898
899
900static struct platform_device *serial8250_isa_devs;
901
902
903
904
905
906
907static DEFINE_MUTEX(serial_mutex);
908
909static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
910{
911 int i;
912
913
914
915
916 for (i = 0; i < nr_uarts; i++)
917 if (uart_match_port(&serial8250_ports[i].port, port))
918 return &serial8250_ports[i];
919
920
921 i = port->line;
922 if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
923 serial8250_ports[i].port.iobase == 0)
924 return &serial8250_ports[i];
925
926
927
928
929
930 for (i = 0; i < nr_uarts; i++)
931 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
932 serial8250_ports[i].port.iobase == 0)
933 return &serial8250_ports[i];
934
935
936
937
938
939 for (i = 0; i < nr_uarts; i++)
940 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
941 return &serial8250_ports[i];
942
943 return NULL;
944}
945
946static void serial_8250_overrun_backoff_work(struct work_struct *work)
947{
948 struct uart_8250_port *up =
949 container_of(to_delayed_work(work), struct uart_8250_port,
950 overrun_backoff);
951 struct uart_port *port = &up->port;
952 unsigned long flags;
953
954 spin_lock_irqsave(&port->lock, flags);
955 up->ier |= UART_IER_RLSI | UART_IER_RDI;
956 up->port.read_status_mask |= UART_LSR_DR;
957 serial_out(up, UART_IER, up->ier);
958 spin_unlock_irqrestore(&port->lock, flags);
959}
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974int serial8250_register_8250_port(struct uart_8250_port *up)
975{
976 struct uart_8250_port *uart;
977 int ret = -ENOSPC;
978
979 if (up->port.uartclk == 0)
980 return -EINVAL;
981
982 mutex_lock(&serial_mutex);
983
984 uart = serial8250_find_match_or_unused(&up->port);
985 if (uart && uart->port.type != PORT_8250_CIR) {
986 struct mctrl_gpios *gpios;
987
988 if (uart->port.dev)
989 uart_remove_one_port(&serial8250_reg, &uart->port);
990
991 uart->port.iobase = up->port.iobase;
992 uart->port.membase = up->port.membase;
993 uart->port.irq = up->port.irq;
994 uart->port.irqflags = up->port.irqflags;
995 uart->port.uartclk = up->port.uartclk;
996 uart->port.fifosize = up->port.fifosize;
997 uart->port.regshift = up->port.regshift;
998 uart->port.iotype = up->port.iotype;
999 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
1000 uart->bugs = up->bugs;
1001 uart->port.mapbase = up->port.mapbase;
1002 uart->port.mapsize = up->port.mapsize;
1003 uart->port.private_data = up->port.private_data;
1004 uart->tx_loadsz = up->tx_loadsz;
1005 uart->capabilities = up->capabilities;
1006 uart->port.throttle = up->port.throttle;
1007 uart->port.unthrottle = up->port.unthrottle;
1008 uart->port.rs485_config = up->port.rs485_config;
1009 uart->port.rs485 = up->port.rs485;
1010 uart->dma = up->dma;
1011
1012
1013 if (uart->port.fifosize && !uart->tx_loadsz)
1014 uart->tx_loadsz = uart->port.fifosize;
1015
1016 if (up->port.dev)
1017 uart->port.dev = up->port.dev;
1018
1019 if (up->port.flags & UPF_FIXED_TYPE)
1020 uart->port.type = up->port.type;
1021
1022
1023
1024
1025
1026 if (!has_acpi_companion(uart->port.dev)) {
1027 gpios = mctrl_gpio_init(&uart->port, 0);
1028 if (IS_ERR(gpios)) {
1029 ret = PTR_ERR(gpios);
1030 goto out_unlock;
1031 } else {
1032 uart->gpios = gpios;
1033 }
1034 }
1035
1036 serial8250_set_defaults(uart);
1037
1038
1039 if (up->port.serial_in)
1040 uart->port.serial_in = up->port.serial_in;
1041 if (up->port.serial_out)
1042 uart->port.serial_out = up->port.serial_out;
1043 if (up->port.handle_irq)
1044 uart->port.handle_irq = up->port.handle_irq;
1045
1046 if (up->port.set_termios)
1047 uart->port.set_termios = up->port.set_termios;
1048 if (up->port.set_ldisc)
1049 uart->port.set_ldisc = up->port.set_ldisc;
1050 if (up->port.get_mctrl)
1051 uart->port.get_mctrl = up->port.get_mctrl;
1052 if (up->port.set_mctrl)
1053 uart->port.set_mctrl = up->port.set_mctrl;
1054 if (up->port.get_divisor)
1055 uart->port.get_divisor = up->port.get_divisor;
1056 if (up->port.set_divisor)
1057 uart->port.set_divisor = up->port.set_divisor;
1058 if (up->port.startup)
1059 uart->port.startup = up->port.startup;
1060 if (up->port.shutdown)
1061 uart->port.shutdown = up->port.shutdown;
1062 if (up->port.pm)
1063 uart->port.pm = up->port.pm;
1064 if (up->port.handle_break)
1065 uart->port.handle_break = up->port.handle_break;
1066 if (up->dl_read)
1067 uart->dl_read = up->dl_read;
1068 if (up->dl_write)
1069 uart->dl_write = up->dl_write;
1070
1071 if (uart->port.type != PORT_8250_CIR) {
1072 if (serial8250_isa_config != NULL)
1073 serial8250_isa_config(0, &uart->port,
1074 &uart->capabilities);
1075
1076 serial8250_apply_quirks(uart);
1077 ret = uart_add_one_port(&serial8250_reg,
1078 &uart->port);
1079 if (ret == 0)
1080 ret = uart->port.line;
1081 } else {
1082 dev_info(uart->port.dev,
1083 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
1084 uart->port.iobase,
1085 (unsigned long long)uart->port.mapbase,
1086 uart->port.irq);
1087
1088 ret = 0;
1089 }
1090
1091
1092 if (up->overrun_backoff_time_ms > 0) {
1093 uart->overrun_backoff_time_ms =
1094 up->overrun_backoff_time_ms;
1095 INIT_DELAYED_WORK(&uart->overrun_backoff,
1096 serial_8250_overrun_backoff_work);
1097 } else {
1098 uart->overrun_backoff_time_ms = 0;
1099 }
1100 }
1101
1102out_unlock:
1103 mutex_unlock(&serial_mutex);
1104
1105 return ret;
1106}
1107EXPORT_SYMBOL(serial8250_register_8250_port);
1108
1109
1110
1111
1112
1113
1114
1115
1116void serial8250_unregister_port(int line)
1117{
1118 struct uart_8250_port *uart = &serial8250_ports[line];
1119
1120 mutex_lock(&serial_mutex);
1121
1122 if (uart->em485) {
1123 unsigned long flags;
1124
1125 spin_lock_irqsave(&uart->port.lock, flags);
1126 serial8250_em485_destroy(uart);
1127 spin_unlock_irqrestore(&uart->port.lock, flags);
1128 }
1129
1130 uart_remove_one_port(&serial8250_reg, &uart->port);
1131 if (serial8250_isa_devs) {
1132 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
1133 uart->port.type = PORT_UNKNOWN;
1134 uart->port.dev = &serial8250_isa_devs->dev;
1135 uart->capabilities = 0;
1136 serial8250_apply_quirks(uart);
1137 uart_add_one_port(&serial8250_reg, &uart->port);
1138 } else {
1139 uart->port.dev = NULL;
1140 }
1141 mutex_unlock(&serial_mutex);
1142}
1143EXPORT_SYMBOL(serial8250_unregister_port);
1144
1145static int __init serial8250_init(void)
1146{
1147 int ret;
1148
1149 if (nr_uarts == 0)
1150 return -ENODEV;
1151
1152 serial8250_isa_init_ports();
1153
1154 pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %sabled\n",
1155 nr_uarts, share_irqs ? "en" : "dis");
1156
1157#ifdef CONFIG_SPARC
1158 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
1159#else
1160 serial8250_reg.nr = UART_NR;
1161 ret = uart_register_driver(&serial8250_reg);
1162#endif
1163 if (ret)
1164 goto out;
1165
1166 ret = serial8250_pnp_init();
1167 if (ret)
1168 goto unreg_uart_drv;
1169
1170 serial8250_isa_devs = platform_device_alloc("serial8250",
1171 PLAT8250_DEV_LEGACY);
1172 if (!serial8250_isa_devs) {
1173 ret = -ENOMEM;
1174 goto unreg_pnp;
1175 }
1176
1177 ret = platform_device_add(serial8250_isa_devs);
1178 if (ret)
1179 goto put_dev;
1180
1181 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
1182
1183 ret = platform_driver_register(&serial8250_isa_driver);
1184 if (ret == 0)
1185 goto out;
1186
1187 platform_device_del(serial8250_isa_devs);
1188put_dev:
1189 platform_device_put(serial8250_isa_devs);
1190unreg_pnp:
1191 serial8250_pnp_exit();
1192unreg_uart_drv:
1193#ifdef CONFIG_SPARC
1194 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1195#else
1196 uart_unregister_driver(&serial8250_reg);
1197#endif
1198out:
1199 return ret;
1200}
1201
1202static void __exit serial8250_exit(void)
1203{
1204 struct platform_device *isa_dev = serial8250_isa_devs;
1205
1206
1207
1208
1209
1210
1211 serial8250_isa_devs = NULL;
1212
1213 platform_driver_unregister(&serial8250_isa_driver);
1214 platform_device_unregister(isa_dev);
1215
1216 serial8250_pnp_exit();
1217
1218#ifdef CONFIG_SPARC
1219 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1220#else
1221 uart_unregister_driver(&serial8250_reg);
1222#endif
1223}
1224
1225module_init(serial8250_init);
1226module_exit(serial8250_exit);
1227
1228MODULE_LICENSE("GPL");
1229MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
1230
1231module_param_hw(share_irqs, uint, other, 0644);
1232MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
1233
1234module_param(nr_uarts, uint, 0644);
1235MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
1236
1237module_param(skip_txen_test, uint, 0644);
1238MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
1239
1240#ifdef CONFIG_SERIAL_8250_RSA
1241module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444);
1242MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
1243#endif
1244MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
1245
1246#ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
1247#ifndef MODULE
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258static void __used s8250_options(void)
1259{
1260#undef MODULE_PARAM_PREFIX
1261#define MODULE_PARAM_PREFIX "8250_core."
1262
1263 module_param_cb(share_irqs, ¶m_ops_uint, &share_irqs, 0644);
1264 module_param_cb(nr_uarts, ¶m_ops_uint, &nr_uarts, 0644);
1265 module_param_cb(skip_txen_test, ¶m_ops_uint, &skip_txen_test, 0644);
1266#ifdef CONFIG_SERIAL_8250_RSA
1267 __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
1268 ¶m_array_ops, .arr = &__param_arr_probe_rsa,
1269 0444, -1, 0);
1270#endif
1271}
1272#else
1273MODULE_ALIAS("8250_core");
1274#endif
1275#endif
1276