2 * linux/drivers/char/8250.c
4 * Driver for 8250/16550-type serial ports
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * Copyright (C) 2001 Russell King.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * $Id: 8250.c,v 1.90 2002/07/28 10:03:27 rmk Exp $
17 * A note about mapbase / membase
19 * mapbase is the physical address of the IO port.
20 * membase is an 'ioremapped' cookie.
23 #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/ioport.h>
30 #include <linux/init.h>
31 #include <linux/console.h>
32 #include <linux/sysrq.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/tty.h>
36 #include <linux/tty_flip.h>
37 #include <linux/serial_reg.h>
38 #include <linux/serial_core.h>
39 #include <linux/serial.h>
40 #include <linux/serial_8250.h>
41 #include <linux/nmi.h>
42 #include <linux/mutex.h>
51 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
52 * is unsafe when used on edge-triggered interrupts.
54 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
56 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
62 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
64 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
68 #define DEBUG_INTR(fmt...) printk(fmt)
70 #define DEBUG_INTR(fmt...) do { } while (0)
73 #define PASS_LIMIT 256
76 * We default to IRQ0 for the "no irq" hack. Some
77 * machine types want others as well - they're free
78 * to redefine this in their header file.
80 #define is_real_interrupt(irq) ((irq) != 0)
82 #ifdef CONFIG_SERIAL_8250_DETECT_IRQ
83 #define CONFIG_SERIAL_DETECT_IRQ 1
85 #ifdef CONFIG_SERIAL_8250_MANY_PORTS
86 #define CONFIG_SERIAL_MANY_PORTS 1
90 * HUB6 is always on. This will be removed once the header
91 * files have been cleaned.
95 #include <asm/serial.h>
98 * SERIAL_PORT_DFNS tells us about built-in ports that have no
99 * standard enumeration mechanism. Platforms that can find all
100 * serial ports via mechanisms like ACPI or PCI need not supply it.
102 #ifndef SERIAL_PORT_DFNS
103 #define SERIAL_PORT_DFNS
106 static const struct old_serial_port old_serial_port[] = {
107 SERIAL_PORT_DFNS /* defined in asm/serial.h */
110 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
112 #ifdef CONFIG_SERIAL_8250_RSA
114 #define PORT_RSA_MAX 4
115 static unsigned long probe_rsa[PORT_RSA_MAX];
116 static unsigned int probe_rsa_count;
117 #endif /* CONFIG_SERIAL_8250_RSA */
119 struct uart_8250_port {
120 struct uart_port port;
121 struct timer_list timer; /* "no irq" timer */
122 struct list_head list; /* ports on this IRQ */
123 unsigned short capabilities; /* port capabilities */
124 unsigned short bugs; /* port bugs */
125 unsigned int tx_loadsz; /* transmit fifo load size */
130 unsigned char mcr_mask; /* mask of user bits */
131 unsigned char mcr_force; /* mask of forced bits */
132 unsigned char lsr_break_flag;
135 * We provide a per-port pm hook.
137 void (*pm)(struct uart_port *port,
138 unsigned int state, unsigned int old);
143 struct list_head *head;
146 static struct irq_info irq_lists[NR_IRQS];
149 * Here we define the default xmit fifo size used for each type of UART.
151 static const struct serial8250_config uart_config[] = {
176 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
177 .flags = UART_CAP_FIFO,
188 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
194 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
196 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
202 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
204 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
212 .name = "16C950/954",
215 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
216 .flags = UART_CAP_FIFO,
222 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
224 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
230 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
231 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
237 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
238 .flags = UART_CAP_FIFO,
244 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
245 .flags = UART_CAP_FIFO | UART_NATSEMI,
251 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
252 .flags = UART_CAP_FIFO | UART_CAP_UUE,
256 #ifdef CONFIG_SERIAL_8250_AU1X00
258 /* Au1x00 UART hardware has a weird register layout */
259 static const u8 au_io_in_map[] = {
269 static const u8 au_io_out_map[] = {
277 /* sane hardware needs no mapping */
278 static inline int map_8250_in_reg(struct uart_8250_port *up, int offset)
280 if (up->port.iotype != UPIO_AU)
282 return au_io_in_map[offset];
285 static inline int map_8250_out_reg(struct uart_8250_port *up, int offset)
287 if (up->port.iotype != UPIO_AU)
289 return au_io_out_map[offset];
294 /* sane hardware needs no mapping */
295 #define map_8250_in_reg(up, offset) (offset)
296 #define map_8250_out_reg(up, offset) (offset)
300 static unsigned int serial_in(struct uart_8250_port *up, int offset)
303 offset = map_8250_in_reg(up, offset) << up->port.regshift;
305 switch (up->port.iotype) {
307 outb(up->port.hub6 - 1 + offset, up->port.iobase);
308 return inb(up->port.iobase + 1);
311 return readb(up->port.membase + offset);
314 return readl(up->port.membase + offset);
316 #ifdef CONFIG_SERIAL_8250_AU1X00
318 return __raw_readl(up->port.membase + offset);
322 if (offset == UART_IIR) {
323 tmp = readl(up->port.membase + (UART_IIR & ~3));
324 return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
326 return readb(up->port.membase + offset);
329 return inb(up->port.iobase + offset);
334 serial_out(struct uart_8250_port *up, int offset, int value)
336 offset = map_8250_out_reg(up, offset) << up->port.regshift;
338 switch (up->port.iotype) {
340 outb(up->port.hub6 - 1 + offset, up->port.iobase);
341 outb(value, up->port.iobase + 1);
345 writeb(value, up->port.membase + offset);
349 writel(value, up->port.membase + offset);
352 #ifdef CONFIG_SERIAL_8250_AU1X00
354 __raw_writel(value, up->port.membase + offset);
358 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
359 writeb(value, up->port.membase + offset);
363 outb(value, up->port.iobase + offset);
368 serial_out_sync(struct uart_8250_port *up, int offset, int value)
370 switch (up->port.iotype) {
373 #ifdef CONFIG_SERIAL_8250_AU1X00
376 serial_out(up, offset, value);
377 serial_in(up, UART_LCR); /* safe, no side-effects */
380 serial_out(up, offset, value);
385 * We used to support using pause I/O for certain machines. We
386 * haven't supported this for a while, but just in case it's badly
387 * needed for certain old 386 machines, I've left these #define's
390 #define serial_inp(up, offset) serial_in(up, offset)
391 #define serial_outp(up, offset, value) serial_out(up, offset, value)
393 /* Uart divisor latch read */
394 static inline int _serial_dl_read(struct uart_8250_port *up)
396 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
399 /* Uart divisor latch write */
400 static inline void _serial_dl_write(struct uart_8250_port *up, int value)
402 serial_outp(up, UART_DLL, value & 0xff);
403 serial_outp(up, UART_DLM, value >> 8 & 0xff);
406 #ifdef CONFIG_SERIAL_8250_AU1X00
407 /* Au1x00 haven't got a standard divisor latch */
408 static int serial_dl_read(struct uart_8250_port *up)
410 if (up->port.iotype == UPIO_AU)
411 return __raw_readl(up->port.membase + 0x28);
413 return _serial_dl_read(up);
416 static void serial_dl_write(struct uart_8250_port *up, int value)
418 if (up->port.iotype == UPIO_AU)
419 __raw_writel(value, up->port.membase + 0x28);
421 _serial_dl_write(up, value);
424 #define serial_dl_read(up) _serial_dl_read(up)
425 #define serial_dl_write(up, value) _serial_dl_write(up, value)
431 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
433 serial_out(up, UART_SCR, offset);
434 serial_out(up, UART_ICR, value);
437 static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
441 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
442 serial_out(up, UART_SCR, offset);
443 value = serial_in(up, UART_ICR);
444 serial_icr_write(up, UART_ACR, up->acr);
452 static inline void serial8250_clear_fifos(struct uart_8250_port *p)
454 if (p->capabilities & UART_CAP_FIFO) {
455 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
456 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
457 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
458 serial_outp(p, UART_FCR, 0);
463 * IER sleep support. UARTs which have EFRs need the "extended
464 * capability" bit enabled. Note that on XR16C850s, we need to
465 * reset LCR to write to IER.
467 static inline void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
469 if (p->capabilities & UART_CAP_SLEEP) {
470 if (p->capabilities & UART_CAP_EFR) {
471 serial_outp(p, UART_LCR, 0xBF);
472 serial_outp(p, UART_EFR, UART_EFR_ECB);
473 serial_outp(p, UART_LCR, 0);
475 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
476 if (p->capabilities & UART_CAP_EFR) {
477 serial_outp(p, UART_LCR, 0xBF);
478 serial_outp(p, UART_EFR, 0);
479 serial_outp(p, UART_LCR, 0);
484 #ifdef CONFIG_SERIAL_8250_RSA
486 * Attempts to turn on the RSA FIFO. Returns zero on failure.
487 * We set the port uart clock rate if we succeed.
489 static int __enable_rsa(struct uart_8250_port *up)
494 mode = serial_inp(up, UART_RSA_MSR);
495 result = mode & UART_RSA_MSR_FIFO;
498 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
499 mode = serial_inp(up, UART_RSA_MSR);
500 result = mode & UART_RSA_MSR_FIFO;
504 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
509 static void enable_rsa(struct uart_8250_port *up)
511 if (up->port.type == PORT_RSA) {
512 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
513 spin_lock_irq(&up->port.lock);
515 spin_unlock_irq(&up->port.lock);
517 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
518 serial_outp(up, UART_RSA_FRR, 0);
523 * Attempts to turn off the RSA FIFO. Returns zero on failure.
524 * It is unknown why interrupts were disabled in here. However,
525 * the caller is expected to preserve this behaviour by grabbing
526 * the spinlock before calling this function.
528 static void disable_rsa(struct uart_8250_port *up)
533 if (up->port.type == PORT_RSA &&
534 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
535 spin_lock_irq(&up->port.lock);
537 mode = serial_inp(up, UART_RSA_MSR);
538 result = !(mode & UART_RSA_MSR_FIFO);
541 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
542 mode = serial_inp(up, UART_RSA_MSR);
543 result = !(mode & UART_RSA_MSR_FIFO);
547 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
548 spin_unlock_irq(&up->port.lock);
551 #endif /* CONFIG_SERIAL_8250_RSA */
554 * This is a quickie test to see how big the FIFO is.
555 * It doesn't work at all the time, more's the pity.
557 static int size_fifo(struct uart_8250_port *up)
559 unsigned char old_fcr, old_mcr, old_lcr;
560 unsigned short old_dl;
563 old_lcr = serial_inp(up, UART_LCR);
564 serial_outp(up, UART_LCR, 0);
565 old_fcr = serial_inp(up, UART_FCR);
566 old_mcr = serial_inp(up, UART_MCR);
567 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
568 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
569 serial_outp(up, UART_MCR, UART_MCR_LOOP);
570 serial_outp(up, UART_LCR, UART_LCR_DLAB);
571 old_dl = serial_dl_read(up);
572 serial_dl_write(up, 0x0001);
573 serial_outp(up, UART_LCR, 0x03);
574 for (count = 0; count < 256; count++)
575 serial_outp(up, UART_TX, count);
576 mdelay(20);/* FIXME - schedule_timeout */
577 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
578 (count < 256); count++)
579 serial_inp(up, UART_RX);
580 serial_outp(up, UART_FCR, old_fcr);
581 serial_outp(up, UART_MCR, old_mcr);
582 serial_outp(up, UART_LCR, UART_LCR_DLAB);
583 serial_dl_write(up, old_dl);
584 serial_outp(up, UART_LCR, old_lcr);
590 * Read UART ID using the divisor method - set DLL and DLM to zero
591 * and the revision will be in DLL and device type in DLM. We
592 * preserve the device state across this.
594 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
596 unsigned char old_dll, old_dlm, old_lcr;
599 old_lcr = serial_inp(p, UART_LCR);
600 serial_outp(p, UART_LCR, UART_LCR_DLAB);
602 old_dll = serial_inp(p, UART_DLL);
603 old_dlm = serial_inp(p, UART_DLM);
605 serial_outp(p, UART_DLL, 0);
606 serial_outp(p, UART_DLM, 0);
608 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
610 serial_outp(p, UART_DLL, old_dll);
611 serial_outp(p, UART_DLM, old_dlm);
612 serial_outp(p, UART_LCR, old_lcr);
618 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
619 * When this function is called we know it is at least a StarTech
620 * 16650 V2, but it might be one of several StarTech UARTs, or one of
621 * its clones. (We treat the broken original StarTech 16650 V1 as a
622 * 16550, and why not? Startech doesn't seem to even acknowledge its
625 * What evil have men's minds wrought...
627 static void autoconfig_has_efr(struct uart_8250_port *up)
629 unsigned int id1, id2, id3, rev;
632 * Everything with an EFR has SLEEP
634 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
637 * First we check to see if it's an Oxford Semiconductor UART.
639 * If we have to do this here because some non-National
640 * Semiconductor clone chips lock up if you try writing to the
641 * LSR register (which serial_icr_read does)
645 * Check for Oxford Semiconductor 16C950.
647 * EFR [4] must be set else this test fails.
649 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
650 * claims that it's needed for 952 dual UART's (which are not
651 * recommended for new designs).
654 serial_out(up, UART_LCR, 0xBF);
655 serial_out(up, UART_EFR, UART_EFR_ECB);
656 serial_out(up, UART_LCR, 0x00);
657 id1 = serial_icr_read(up, UART_ID1);
658 id2 = serial_icr_read(up, UART_ID2);
659 id3 = serial_icr_read(up, UART_ID3);
660 rev = serial_icr_read(up, UART_REV);
662 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
664 if (id1 == 0x16 && id2 == 0xC9 &&
665 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
666 up->port.type = PORT_16C950;
669 * Enable work around for the Oxford Semiconductor 952 rev B
670 * chip which causes it to seriously miscalculate baud rates
673 if (id3 == 0x52 && rev == 0x01)
674 up->bugs |= UART_BUG_QUOT;
679 * We check for a XR16C850 by setting DLL and DLM to 0, and then
680 * reading back DLL and DLM. The chip type depends on the DLM
682 * 0x10 - XR16C850 and the DLL contains the chip revision.
686 id1 = autoconfig_read_divisor_id(up);
687 DEBUG_AUTOCONF("850id=%04x ", id1);
690 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
691 up->port.type = PORT_16850;
696 * It wasn't an XR16C850.
698 * We distinguish between the '654 and the '650 by counting
699 * how many bytes are in the FIFO. I'm using this for now,
700 * since that's the technique that was sent to me in the
701 * serial driver update, but I'm not convinced this works.
702 * I've had problems doing this in the past. -TYT
704 if (size_fifo(up) == 64)
705 up->port.type = PORT_16654;
707 up->port.type = PORT_16650V2;
711 * We detected a chip without a FIFO. Only two fall into
712 * this category - the original 8250 and the 16450. The
713 * 16450 has a scratch register (accessible with LCR=0)
715 static void autoconfig_8250(struct uart_8250_port *up)
717 unsigned char scratch, status1, status2;
719 up->port.type = PORT_8250;
721 scratch = serial_in(up, UART_SCR);
722 serial_outp(up, UART_SCR, 0xa5);
723 status1 = serial_in(up, UART_SCR);
724 serial_outp(up, UART_SCR, 0x5a);
725 status2 = serial_in(up, UART_SCR);
726 serial_outp(up, UART_SCR, scratch);
728 if (status1 == 0xa5 && status2 == 0x5a)
729 up->port.type = PORT_16450;
732 static int broken_efr(struct uart_8250_port *up)
735 * Exar ST16C2550 "A2" devices incorrectly detect as
736 * having an EFR, and report an ID of 0x0201. See
737 * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf
739 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
746 * We know that the chip has FIFOs. Does it have an EFR? The
747 * EFR is located in the same register position as the IIR and
748 * we know the top two bits of the IIR are currently set. The
749 * EFR should contain zero. Try to read the EFR.
751 static void autoconfig_16550a(struct uart_8250_port *up)
753 unsigned char status1, status2;
754 unsigned int iersave;
756 up->port.type = PORT_16550A;
757 up->capabilities |= UART_CAP_FIFO;
760 * Check for presence of the EFR when DLAB is set.
761 * Only ST16C650V1 UARTs pass this test.
763 serial_outp(up, UART_LCR, UART_LCR_DLAB);
764 if (serial_in(up, UART_EFR) == 0) {
765 serial_outp(up, UART_EFR, 0xA8);
766 if (serial_in(up, UART_EFR) != 0) {
767 DEBUG_AUTOCONF("EFRv1 ");
768 up->port.type = PORT_16650;
769 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
771 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
773 serial_outp(up, UART_EFR, 0);
778 * Maybe it requires 0xbf to be written to the LCR.
779 * (other ST16C650V2 UARTs, TI16C752A, etc)
781 serial_outp(up, UART_LCR, 0xBF);
782 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
783 DEBUG_AUTOCONF("EFRv2 ");
784 autoconfig_has_efr(up);
789 * Check for a National Semiconductor SuperIO chip.
790 * Attempt to switch to bank 2, read the value of the LOOP bit
791 * from EXCR1. Switch back to bank 0, change it in MCR. Then
792 * switch back to bank 2, read it from EXCR1 again and check
793 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
795 serial_outp(up, UART_LCR, 0);
796 status1 = serial_in(up, UART_MCR);
797 serial_outp(up, UART_LCR, 0xE0);
798 status2 = serial_in(up, 0x02); /* EXCR1 */
800 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
801 serial_outp(up, UART_LCR, 0);
802 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
803 serial_outp(up, UART_LCR, 0xE0);
804 status2 = serial_in(up, 0x02); /* EXCR1 */
805 serial_outp(up, UART_LCR, 0);
806 serial_outp(up, UART_MCR, status1);
808 if ((status2 ^ status1) & UART_MCR_LOOP) {
811 serial_outp(up, UART_LCR, 0xE0);
813 quot = serial_dl_read(up);
816 status1 = serial_in(up, 0x04); /* EXCR1 */
817 status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
818 status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
819 serial_outp(up, 0x04, status1);
821 serial_dl_write(up, quot);
823 serial_outp(up, UART_LCR, 0);
825 up->port.uartclk = 921600*16;
826 up->port.type = PORT_NS16550A;
827 up->capabilities |= UART_NATSEMI;
833 * No EFR. Try to detect a TI16750, which only sets bit 5 of
834 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
835 * Try setting it with and without DLAB set. Cheap clones
836 * set bit 5 without DLAB set.
838 serial_outp(up, UART_LCR, 0);
839 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
840 status1 = serial_in(up, UART_IIR) >> 5;
841 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
842 serial_outp(up, UART_LCR, UART_LCR_DLAB);
843 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
844 status2 = serial_in(up, UART_IIR) >> 5;
845 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
846 serial_outp(up, UART_LCR, 0);
848 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
850 if (status1 == 6 && status2 == 7) {
851 up->port.type = PORT_16750;
852 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
857 * Try writing and reading the UART_IER_UUE bit (b6).
858 * If it works, this is probably one of the Xscale platform's
860 * We're going to explicitly set the UUE bit to 0 before
861 * trying to write and read a 1 just to make sure it's not
862 * already a 1 and maybe locked there before we even start start.
864 iersave = serial_in(up, UART_IER);
865 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
866 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
868 * OK it's in a known zero state, try writing and reading
869 * without disturbing the current state of the other bits.
871 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
872 if (serial_in(up, UART_IER) & UART_IER_UUE) {
875 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
877 DEBUG_AUTOCONF("Xscale ");
878 up->port.type = PORT_XSCALE;
879 up->capabilities |= UART_CAP_UUE;
884 * If we got here we couldn't force the IER_UUE bit to 0.
885 * Log it and continue.
887 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
889 serial_outp(up, UART_IER, iersave);
893 * This routine is called by rs_init() to initialize a specific serial
894 * port. It determines what type of UART chip this serial port is
895 * using: 8250, 16450, 16550, 16550A. The important question is
896 * whether or not this UART is a 16550A or not, since this will
897 * determine whether or not we can use its FIFO features or not.
899 static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
901 unsigned char status1, scratch, scratch2, scratch3;
902 unsigned char save_lcr, save_mcr;
905 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
908 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ",
909 up->port.line, up->port.iobase, up->port.membase);
912 * We really do need global IRQs disabled here - we're going to
913 * be frobbing the chips IRQ enable register to see if it exists.
915 spin_lock_irqsave(&up->port.lock, flags);
916 // save_flags(flags); cli();
918 up->capabilities = 0;
921 if (!(up->port.flags & UPF_BUGGY_UART)) {
923 * Do a simple existence test first; if we fail this,
924 * there's no point trying anything else.
926 * 0x80 is used as a nonsense port to prevent against
927 * false positives due to ISA bus float. The
928 * assumption is that 0x80 is a non-existent port;
929 * which should be safe since include/asm/io.h also
930 * makes this assumption.
932 * Note: this is safe as long as MCR bit 4 is clear
933 * and the device is in "PC" mode.
935 scratch = serial_inp(up, UART_IER);
936 serial_outp(up, UART_IER, 0);
941 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
942 * 16C754B) allow only to modify them if an EFR bit is set.
944 scratch2 = serial_inp(up, UART_IER) & 0x0f;
945 serial_outp(up, UART_IER, 0x0F);
949 scratch3 = serial_inp(up, UART_IER) & 0x0f;
950 serial_outp(up, UART_IER, scratch);
951 if (scratch2 != 0 || scratch3 != 0x0F) {
953 * We failed; there's nothing here
955 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
961 save_mcr = serial_in(up, UART_MCR);
962 save_lcr = serial_in(up, UART_LCR);
965 * Check to see if a UART is really there. Certain broken
966 * internal modems based on the Rockwell chipset fail this
967 * test, because they apparently don't implement the loopback
968 * test mode. So this test is skipped on the COM 1 through
969 * COM 4 ports. This *should* be safe, since no board
970 * manufacturer would be stupid enough to design a board
971 * that conflicts with COM 1-4 --- we hope!
973 if (!(up->port.flags & UPF_SKIP_TEST)) {
974 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
975 status1 = serial_inp(up, UART_MSR) & 0xF0;
976 serial_outp(up, UART_MCR, save_mcr);
977 if (status1 != 0x90) {
978 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
985 * We're pretty sure there's a port here. Lets find out what
986 * type of port it is. The IIR top two bits allows us to find
987 * out if it's 8250 or 16450, 16550, 16550A or later. This
988 * determines what we test for next.
990 * We also initialise the EFR (if any) to zero for later. The
991 * EFR occupies the same register location as the FCR and IIR.
993 serial_outp(up, UART_LCR, 0xBF);
994 serial_outp(up, UART_EFR, 0);
995 serial_outp(up, UART_LCR, 0);
997 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
998 scratch = serial_in(up, UART_IIR) >> 6;
1000 DEBUG_AUTOCONF("iir=%d ", scratch);
1004 autoconfig_8250(up);
1007 up->port.type = PORT_UNKNOWN;
1010 up->port.type = PORT_16550;
1013 autoconfig_16550a(up);
1017 #ifdef CONFIG_SERIAL_8250_RSA
1019 * Only probe for RSA ports if we got the region.
1021 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1024 for (i = 0 ; i < probe_rsa_count; ++i) {
1025 if (probe_rsa[i] == up->port.iobase &&
1027 up->port.type = PORT_RSA;
1034 #ifdef CONFIG_SERIAL_8250_AU1X00
1035 /* if access method is AU, it is a 16550 with a quirk */
1036 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
1037 up->bugs |= UART_BUG_NOMSR;
1040 serial_outp(up, UART_LCR, save_lcr);
1042 if (up->capabilities != uart_config[up->port.type].flags) {
1044 "ttyS%d: detected caps %08x should be %08x\n",
1045 up->port.line, up->capabilities,
1046 uart_config[up->port.type].flags);
1049 up->port.fifosize = uart_config[up->port.type].fifo_size;
1050 up->capabilities = uart_config[up->port.type].flags;
1051 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1053 if (up->port.type == PORT_UNKNOWN)
1059 #ifdef CONFIG_SERIAL_8250_RSA
1060 if (up->port.type == PORT_RSA)
1061 serial_outp(up, UART_RSA_FRR, 0);
1063 serial_outp(up, UART_MCR, save_mcr);
1064 serial8250_clear_fifos(up);
1065 serial_in(up, UART_RX);
1066 if (up->capabilities & UART_CAP_UUE)
1067 serial_outp(up, UART_IER, UART_IER_UUE);
1069 serial_outp(up, UART_IER, 0);
1072 spin_unlock_irqrestore(&up->port.lock, flags);
1073 // restore_flags(flags);
1074 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1077 static void autoconfig_irq(struct uart_8250_port *up)
1079 unsigned char save_mcr, save_ier;
1080 unsigned char save_ICP = 0;
1081 unsigned int ICP = 0;
1085 if (up->port.flags & UPF_FOURPORT) {
1086 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1087 save_ICP = inb_p(ICP);
1092 /* forget possible initially masked and pending IRQ */
1093 probe_irq_off(probe_irq_on());
1094 save_mcr = serial_inp(up, UART_MCR);
1095 save_ier = serial_inp(up, UART_IER);
1096 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1098 irqs = probe_irq_on();
1099 serial_outp(up, UART_MCR, 0);
1101 if (up->port.flags & UPF_FOURPORT) {
1102 serial_outp(up, UART_MCR,
1103 UART_MCR_DTR | UART_MCR_RTS);
1105 serial_outp(up, UART_MCR,
1106 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1108 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1109 (void)serial_inp(up, UART_LSR);
1110 (void)serial_inp(up, UART_RX);
1111 (void)serial_inp(up, UART_IIR);
1112 (void)serial_inp(up, UART_MSR);
1113 serial_outp(up, UART_TX, 0xFF);
1115 irq = probe_irq_off(irqs);
1117 serial_outp(up, UART_MCR, save_mcr);
1118 serial_outp(up, UART_IER, save_ier);
1120 if (up->port.flags & UPF_FOURPORT)
1121 outb_p(save_ICP, ICP);
1123 up->port.irq = (irq > 0) ? irq : 0;
1126 static inline void __stop_tx(struct uart_8250_port *p)
1128 if (p->ier & UART_IER_THRI) {
1129 p->ier &= ~UART_IER_THRI;
1130 serial_out(p, UART_IER, p->ier);
1134 static void serial8250_stop_tx(struct uart_port *port)
1136 struct uart_8250_port *up = (struct uart_8250_port *)port;
1141 * We really want to stop the transmitter from sending.
1143 if (up->port.type == PORT_16C950) {
1144 up->acr |= UART_ACR_TXDIS;
1145 serial_icr_write(up, UART_ACR, up->acr);
1149 static void transmit_chars(struct uart_8250_port *up);
1151 static void serial8250_start_tx(struct uart_port *port)
1153 struct uart_8250_port *up = (struct uart_8250_port *)port;
1155 if (!(up->ier & UART_IER_THRI)) {
1156 up->ier |= UART_IER_THRI;
1157 serial_out(up, UART_IER, up->ier);
1159 if (up->bugs & UART_BUG_TXEN) {
1160 unsigned char lsr, iir;
1161 lsr = serial_in(up, UART_LSR);
1162 iir = serial_in(up, UART_IIR);
1163 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT)
1169 * Re-enable the transmitter if we disabled it.
1171 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1172 up->acr &= ~UART_ACR_TXDIS;
1173 serial_icr_write(up, UART_ACR, up->acr);
1177 static void serial8250_stop_rx(struct uart_port *port)
1179 struct uart_8250_port *up = (struct uart_8250_port *)port;
1181 up->ier &= ~UART_IER_RLSI;
1182 up->port.read_status_mask &= ~UART_LSR_DR;
1183 serial_out(up, UART_IER, up->ier);
1186 static void serial8250_enable_ms(struct uart_port *port)
1188 struct uart_8250_port *up = (struct uart_8250_port *)port;
1190 /* no MSR capabilities */
1191 if (up->bugs & UART_BUG_NOMSR)
1194 up->ier |= UART_IER_MSI;
1195 serial_out(up, UART_IER, up->ier);
1199 receive_chars(struct uart_8250_port *up, unsigned int *status)
1201 struct tty_struct *tty = up->port.info->tty;
1202 unsigned char ch, lsr = *status;
1203 int max_count = 256;
1207 ch = serial_inp(up, UART_RX);
1209 up->port.icount.rx++;
1211 #ifdef CONFIG_SERIAL_8250_CONSOLE
1213 * Recover the break flag from console xmit
1215 if (up->port.line == up->port.cons->index) {
1216 lsr |= up->lsr_break_flag;
1217 up->lsr_break_flag = 0;
1221 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
1222 UART_LSR_FE | UART_LSR_OE))) {
1224 * For statistics only
1226 if (lsr & UART_LSR_BI) {
1227 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1228 up->port.icount.brk++;
1230 * We do the SysRQ and SAK checking
1231 * here because otherwise the break
1232 * may get masked by ignore_status_mask
1233 * or read_status_mask.
1235 if (uart_handle_break(&up->port))
1237 } else if (lsr & UART_LSR_PE)
1238 up->port.icount.parity++;
1239 else if (lsr & UART_LSR_FE)
1240 up->port.icount.frame++;
1241 if (lsr & UART_LSR_OE)
1242 up->port.icount.overrun++;
1245 * Mask off conditions which should be ignored.
1247 lsr &= up->port.read_status_mask;
1249 if (lsr & UART_LSR_BI) {
1250 DEBUG_INTR("handling break....");
1252 } else if (lsr & UART_LSR_PE)
1254 else if (lsr & UART_LSR_FE)
1257 if (uart_handle_sysrq_char(&up->port, ch))
1260 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1263 lsr = serial_inp(up, UART_LSR);
1264 } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
1265 spin_unlock(&up->port.lock);
1266 tty_flip_buffer_push(tty);
1267 spin_lock(&up->port.lock);
1271 static void transmit_chars(struct uart_8250_port *up)
1273 struct circ_buf *xmit = &up->port.info->xmit;
1276 if (up->port.x_char) {
1277 serial_outp(up, UART_TX, up->port.x_char);
1278 up->port.icount.tx++;
1279 up->port.x_char = 0;
1282 if (uart_tx_stopped(&up->port)) {
1283 serial8250_stop_tx(&up->port);
1286 if (uart_circ_empty(xmit)) {
1291 count = up->tx_loadsz;
1293 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1294 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1295 up->port.icount.tx++;
1296 if (uart_circ_empty(xmit))
1298 } while (--count > 0);
1300 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1301 uart_write_wakeup(&up->port);
1303 DEBUG_INTR("THRE...");
1305 if (uart_circ_empty(xmit))
1309 static unsigned int check_modem_status(struct uart_8250_port *up)
1311 unsigned int status = serial_in(up, UART_MSR);
1313 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1314 up->port.info != NULL) {
1315 if (status & UART_MSR_TERI)
1316 up->port.icount.rng++;
1317 if (status & UART_MSR_DDSR)
1318 up->port.icount.dsr++;
1319 if (status & UART_MSR_DDCD)
1320 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1321 if (status & UART_MSR_DCTS)
1322 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
1324 wake_up_interruptible(&up->port.info->delta_msr_wait);
1331 * This handles the interrupt from one port.
1334 serial8250_handle_port(struct uart_8250_port *up)
1336 unsigned int status;
1337 unsigned long flags;
1339 spin_lock_irqsave(&up->port.lock, flags);
1341 status = serial_inp(up, UART_LSR);
1343 DEBUG_INTR("status = %x...", status);
1345 if (status & UART_LSR_DR)
1346 receive_chars(up, &status);
1347 check_modem_status(up);
1348 if (status & UART_LSR_THRE)
1351 spin_unlock_irqrestore(&up->port.lock, flags);
1355 * This is the serial driver's interrupt routine.
1357 * Arjan thinks the old way was overly complex, so it got simplified.
1358 * Alan disagrees, saying that need the complexity to handle the weird
1359 * nature of ISA shared interrupts. (This is a special exception.)
1361 * In order to handle ISA shared interrupts properly, we need to check
1362 * that all ports have been serviced, and therefore the ISA interrupt
1363 * line has been de-asserted.
1365 * This means we need to loop through all ports. checking that they
1366 * don't have an interrupt pending.
1368 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
1370 struct irq_info *i = dev_id;
1371 struct list_head *l, *end = NULL;
1372 int pass_counter = 0, handled = 0;
1374 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1376 spin_lock(&i->lock);
1380 struct uart_8250_port *up;
1383 up = list_entry(l, struct uart_8250_port, list);
1385 iir = serial_in(up, UART_IIR);
1386 if (!(iir & UART_IIR_NO_INT)) {
1387 serial8250_handle_port(up);
1392 } else if (end == NULL)
1397 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1398 /* If we hit this, we're dead. */
1399 printk(KERN_ERR "serial8250: too much work for "
1405 spin_unlock(&i->lock);
1407 DEBUG_INTR("end.\n");
1409 return IRQ_RETVAL(handled);
1413 * To support ISA shared interrupts, we need to have one interrupt
1414 * handler that ensures that the IRQ line has been deasserted
1415 * before returning. Failing to do this will result in the IRQ
1416 * line being stuck active, and, since ISA irqs are edge triggered,
1417 * no more IRQs will be seen.
1419 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1421 spin_lock_irq(&i->lock);
1423 if (!list_empty(i->head)) {
1424 if (i->head == &up->list)
1425 i->head = i->head->next;
1426 list_del(&up->list);
1428 BUG_ON(i->head != &up->list);
1432 spin_unlock_irq(&i->lock);
1435 static int serial_link_irq_chain(struct uart_8250_port *up)
1437 struct irq_info *i = irq_lists + up->port.irq;
1438 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
1440 spin_lock_irq(&i->lock);
1443 list_add(&up->list, i->head);
1444 spin_unlock_irq(&i->lock);
1448 INIT_LIST_HEAD(&up->list);
1449 i->head = &up->list;
1450 spin_unlock_irq(&i->lock);
1452 ret = request_irq(up->port.irq, serial8250_interrupt,
1453 irq_flags, "serial", i);
1455 serial_do_unlink(i, up);
1461 static void serial_unlink_irq_chain(struct uart_8250_port *up)
1463 struct irq_info *i = irq_lists + up->port.irq;
1465 BUG_ON(i->head == NULL);
1467 if (list_empty(i->head))
1468 free_irq(up->port.irq, i);
1470 serial_do_unlink(i, up);
1473 /* Base timer interval for polling */
1474 static inline int poll_timeout(int timeout)
1476 return timeout > 6 ? (timeout / 2 - 2) : 1;
1480 * This function is used to handle ports that do not have an
1481 * interrupt. This doesn't work very well for 16450's, but gives
1482 * barely passable results for a 16550A. (Although at the expense
1483 * of much CPU overhead).
1485 static void serial8250_timeout(unsigned long data)
1487 struct uart_8250_port *up = (struct uart_8250_port *)data;
1490 iir = serial_in(up, UART_IIR);
1491 if (!(iir & UART_IIR_NO_INT))
1492 serial8250_handle_port(up);
1493 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
1496 static void serial8250_backup_timeout(unsigned long data)
1498 struct uart_8250_port *up = (struct uart_8250_port *)data;
1499 unsigned int iir, ier = 0;
1502 * Must disable interrupts or else we risk racing with the interrupt
1505 if (is_real_interrupt(up->port.irq)) {
1506 ier = serial_in(up, UART_IER);
1507 serial_out(up, UART_IER, 0);
1510 iir = serial_in(up, UART_IIR);
1513 * This should be a safe test for anyone who doesn't trust the
1514 * IIR bits on their UART, but it's specifically designed for
1515 * the "Diva" UART used on the management processor on many HP
1516 * ia64 and parisc boxes.
1518 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
1519 (!uart_circ_empty(&up->port.info->xmit) || up->port.x_char) &&
1520 (serial_in(up, UART_LSR) & UART_LSR_THRE)) {
1521 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1522 iir |= UART_IIR_THRI;
1525 if (!(iir & UART_IIR_NO_INT))
1526 serial8250_handle_port(up);
1528 if (is_real_interrupt(up->port.irq))
1529 serial_out(up, UART_IER, ier);
1531 /* Standard timer interval plus 0.2s to keep the port running */
1532 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout) + HZ/5);
1535 static unsigned int serial8250_tx_empty(struct uart_port *port)
1537 struct uart_8250_port *up = (struct uart_8250_port *)port;
1538 unsigned long flags;
1541 spin_lock_irqsave(&up->port.lock, flags);
1542 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
1543 spin_unlock_irqrestore(&up->port.lock, flags);
1548 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1550 struct uart_8250_port *up = (struct uart_8250_port *)port;
1551 unsigned int status;
1554 status = check_modem_status(up);
1557 if (status & UART_MSR_DCD)
1559 if (status & UART_MSR_RI)
1561 if (status & UART_MSR_DSR)
1563 if (status & UART_MSR_CTS)
1568 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1570 struct uart_8250_port *up = (struct uart_8250_port *)port;
1571 unsigned char mcr = 0;
1573 if (mctrl & TIOCM_RTS)
1574 mcr |= UART_MCR_RTS;
1575 if (mctrl & TIOCM_DTR)
1576 mcr |= UART_MCR_DTR;
1577 if (mctrl & TIOCM_OUT1)
1578 mcr |= UART_MCR_OUT1;
1579 if (mctrl & TIOCM_OUT2)
1580 mcr |= UART_MCR_OUT2;
1581 if (mctrl & TIOCM_LOOP)
1582 mcr |= UART_MCR_LOOP;
1584 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1586 serial_out(up, UART_MCR, mcr);
1589 static void serial8250_break_ctl(struct uart_port *port, int break_state)
1591 struct uart_8250_port *up = (struct uart_8250_port *)port;
1592 unsigned long flags;
1594 spin_lock_irqsave(&up->port.lock, flags);
1595 if (break_state == -1)
1596 up->lcr |= UART_LCR_SBC;
1598 up->lcr &= ~UART_LCR_SBC;
1599 serial_out(up, UART_LCR, up->lcr);
1600 spin_unlock_irqrestore(&up->port.lock, flags);
1603 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1606 * Wait for transmitter & holding register to empty
1608 static inline void wait_for_xmitr(struct uart_8250_port *up, int bits)
1610 unsigned int status, tmout = 10000;
1612 /* Wait up to 10ms for the character(s) to be sent. */
1614 status = serial_in(up, UART_LSR);
1616 if (status & UART_LSR_BI)
1617 up->lsr_break_flag = UART_LSR_BI;
1622 } while ((status & bits) != bits);
1624 /* Wait up to 1s for flow control if necessary */
1625 if (up->port.flags & UPF_CONS_FLOW) {
1627 while (!(serial_in(up, UART_MSR) & UART_MSR_CTS) && --tmout) {
1629 touch_nmi_watchdog();
1634 static int serial8250_startup(struct uart_port *port)
1636 struct uart_8250_port *up = (struct uart_8250_port *)port;
1637 unsigned long flags;
1638 unsigned char lsr, iir;
1641 up->capabilities = uart_config[up->port.type].flags;
1644 if (up->port.type == PORT_16C950) {
1645 /* Wake up and initialize UART */
1647 serial_outp(up, UART_LCR, 0xBF);
1648 serial_outp(up, UART_EFR, UART_EFR_ECB);
1649 serial_outp(up, UART_IER, 0);
1650 serial_outp(up, UART_LCR, 0);
1651 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1652 serial_outp(up, UART_LCR, 0xBF);
1653 serial_outp(up, UART_EFR, UART_EFR_ECB);
1654 serial_outp(up, UART_LCR, 0);
1657 #ifdef CONFIG_SERIAL_8250_RSA
1659 * If this is an RSA port, see if we can kick it up to the
1660 * higher speed clock.
1666 * Clear the FIFO buffers and disable them.
1667 * (they will be reenabled in set_termios())
1669 serial8250_clear_fifos(up);
1672 * Clear the interrupt registers.
1674 (void) serial_inp(up, UART_LSR);
1675 (void) serial_inp(up, UART_RX);
1676 (void) serial_inp(up, UART_IIR);
1677 (void) serial_inp(up, UART_MSR);
1680 * At this point, there's no way the LSR could still be 0xff;
1681 * if it is, then bail out, because there's likely no UART
1684 if (!(up->port.flags & UPF_BUGGY_UART) &&
1685 (serial_inp(up, UART_LSR) == 0xff)) {
1686 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
1691 * For a XR16C850, we need to set the trigger levels
1693 if (up->port.type == PORT_16850) {
1696 serial_outp(up, UART_LCR, 0xbf);
1698 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
1699 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
1700 serial_outp(up, UART_TRG, UART_TRG_96);
1701 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
1702 serial_outp(up, UART_TRG, UART_TRG_96);
1704 serial_outp(up, UART_LCR, 0);
1707 if (is_real_interrupt(up->port.irq)) {
1709 * Test for UARTs that do not reassert THRE when the
1710 * transmitter is idle and the interrupt has already
1711 * been cleared. Real 16550s should always reassert
1712 * this interrupt whenever the transmitter is idle and
1713 * the interrupt is enabled. Delays are necessary to
1714 * allow register changes to become visible.
1716 spin_lock_irqsave(&up->port.lock, flags);
1718 wait_for_xmitr(up, UART_LSR_THRE);
1719 serial_out_sync(up, UART_IER, UART_IER_THRI);
1720 udelay(1); /* allow THRE to set */
1721 serial_in(up, UART_IIR);
1722 serial_out(up, UART_IER, 0);
1723 serial_out_sync(up, UART_IER, UART_IER_THRI);
1724 udelay(1); /* allow a working UART time to re-assert THRE */
1725 iir = serial_in(up, UART_IIR);
1726 serial_out(up, UART_IER, 0);
1728 spin_unlock_irqrestore(&up->port.lock, flags);
1731 * If the interrupt is not reasserted, setup a timer to
1732 * kick the UART on a regular basis.
1734 if (iir & UART_IIR_NO_INT) {
1735 pr_debug("ttyS%d - using backup timer\n", port->line);
1736 up->timer.function = serial8250_backup_timeout;
1737 up->timer.data = (unsigned long)up;
1738 mod_timer(&up->timer, jiffies +
1739 poll_timeout(up->port.timeout) + HZ/5);
1744 * If the "interrupt" for this port doesn't correspond with any
1745 * hardware interrupt, we use a timer-based system. The original
1746 * driver used to do this with IRQ0.
1748 if (!is_real_interrupt(up->port.irq)) {
1749 up->timer.data = (unsigned long)up;
1750 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
1752 retval = serial_link_irq_chain(up);
1758 * Now, initialize the UART
1760 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
1762 spin_lock_irqsave(&up->port.lock, flags);
1763 if (up->port.flags & UPF_FOURPORT) {
1764 if (!is_real_interrupt(up->port.irq))
1765 up->port.mctrl |= TIOCM_OUT1;
1768 * Most PC uarts need OUT2 raised to enable interrupts.
1770 if (is_real_interrupt(up->port.irq))
1771 up->port.mctrl |= TIOCM_OUT2;
1773 serial8250_set_mctrl(&up->port, up->port.mctrl);
1776 * Do a quick test to see if we receive an
1777 * interrupt when we enable the TX irq.
1779 serial_outp(up, UART_IER, UART_IER_THRI);
1780 lsr = serial_in(up, UART_LSR);
1781 iir = serial_in(up, UART_IIR);
1782 serial_outp(up, UART_IER, 0);
1784 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
1785 if (!(up->bugs & UART_BUG_TXEN)) {
1786 up->bugs |= UART_BUG_TXEN;
1787 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
1791 up->bugs &= ~UART_BUG_TXEN;
1794 spin_unlock_irqrestore(&up->port.lock, flags);
1797 * Finally, enable interrupts. Note: Modem status interrupts
1798 * are set via set_termios(), which will be occurring imminently
1799 * anyway, so we don't enable them here.
1801 up->ier = UART_IER_RLSI | UART_IER_RDI;
1802 serial_outp(up, UART_IER, up->ier);
1804 if (up->port.flags & UPF_FOURPORT) {
1807 * Enable interrupts on the AST Fourport board
1809 icp = (up->port.iobase & 0xfe0) | 0x01f;
1815 * And clear the interrupt registers again for luck.
1817 (void) serial_inp(up, UART_LSR);
1818 (void) serial_inp(up, UART_RX);
1819 (void) serial_inp(up, UART_IIR);
1820 (void) serial_inp(up, UART_MSR);
1825 static void serial8250_shutdown(struct uart_port *port)
1827 struct uart_8250_port *up = (struct uart_8250_port *)port;
1828 unsigned long flags;
1831 * Disable interrupts from this port
1834 serial_outp(up, UART_IER, 0);
1836 spin_lock_irqsave(&up->port.lock, flags);
1837 if (up->port.flags & UPF_FOURPORT) {
1838 /* reset interrupts on the AST Fourport board */
1839 inb((up->port.iobase & 0xfe0) | 0x1f);
1840 up->port.mctrl |= TIOCM_OUT1;
1842 up->port.mctrl &= ~TIOCM_OUT2;
1844 serial8250_set_mctrl(&up->port, up->port.mctrl);
1845 spin_unlock_irqrestore(&up->port.lock, flags);
1848 * Disable break condition and FIFOs
1850 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
1851 serial8250_clear_fifos(up);
1853 #ifdef CONFIG_SERIAL_8250_RSA
1855 * Reset the RSA board back to 115kbps compat mode.
1861 * Read data port to reset things, and then unlink from
1864 (void) serial_in(up, UART_RX);
1866 del_timer_sync(&up->timer);
1867 up->timer.function = serial8250_timeout;
1868 if (is_real_interrupt(up->port.irq))
1869 serial_unlink_irq_chain(up);
1872 static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
1877 * Handle magic divisors for baud rates above baud_base on
1878 * SMSC SuperIO chips.
1880 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1881 baud == (port->uartclk/4))
1883 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1884 baud == (port->uartclk/8))
1887 quot = uart_get_divisor(port, baud);
1893 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
1894 struct ktermios *old)
1896 struct uart_8250_port *up = (struct uart_8250_port *)port;
1897 unsigned char cval, fcr = 0;
1898 unsigned long flags;
1899 unsigned int baud, quot;
1901 switch (termios->c_cflag & CSIZE) {
1903 cval = UART_LCR_WLEN5;
1906 cval = UART_LCR_WLEN6;
1909 cval = UART_LCR_WLEN7;
1913 cval = UART_LCR_WLEN8;
1917 if (termios->c_cflag & CSTOPB)
1918 cval |= UART_LCR_STOP;
1919 if (termios->c_cflag & PARENB)
1920 cval |= UART_LCR_PARITY;
1921 if (!(termios->c_cflag & PARODD))
1922 cval |= UART_LCR_EPAR;
1924 if (termios->c_cflag & CMSPAR)
1925 cval |= UART_LCR_SPAR;
1929 * Ask the core to calculate the divisor for us.
1931 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
1932 quot = serial8250_get_divisor(port, baud);
1935 * Oxford Semi 952 rev B workaround
1937 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
1940 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
1942 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1944 fcr = uart_config[up->port.type].fcr;
1948 * MCR-based auto flow control. When AFE is enabled, RTS will be
1949 * deasserted when the receive FIFO contains more characters than
1950 * the trigger, or the MCR RTS bit is cleared. In the case where
1951 * the remote UART is not using CTS auto flow control, we must
1952 * have sufficient FIFO entries for the latency of the remote
1953 * UART to respond. IOW, at least 32 bytes of FIFO.
1955 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
1956 up->mcr &= ~UART_MCR_AFE;
1957 if (termios->c_cflag & CRTSCTS)
1958 up->mcr |= UART_MCR_AFE;
1962 * Ok, we're now changing the port state. Do it with
1963 * interrupts disabled.
1965 spin_lock_irqsave(&up->port.lock, flags);
1968 * Update the per-port timeout.
1970 uart_update_timeout(port, termios->c_cflag, baud);
1972 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1973 if (termios->c_iflag & INPCK)
1974 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1975 if (termios->c_iflag & (BRKINT | PARMRK))
1976 up->port.read_status_mask |= UART_LSR_BI;
1979 * Characteres to ignore
1981 up->port.ignore_status_mask = 0;
1982 if (termios->c_iflag & IGNPAR)
1983 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1984 if (termios->c_iflag & IGNBRK) {
1985 up->port.ignore_status_mask |= UART_LSR_BI;
1987 * If we're ignoring parity and break indicators,
1988 * ignore overruns too (for real raw support).
1990 if (termios->c_iflag & IGNPAR)
1991 up->port.ignore_status_mask |= UART_LSR_OE;
1995 * ignore all characters if CREAD is not set
1997 if ((termios->c_cflag & CREAD) == 0)
1998 up->port.ignore_status_mask |= UART_LSR_DR;
2001 * CTS flow control flag and modem status interrupts
2003 up->ier &= ~UART_IER_MSI;
2004 if (!(up->bugs & UART_BUG_NOMSR) &&
2005 UART_ENABLE_MS(&up->port, termios->c_cflag))
2006 up->ier |= UART_IER_MSI;
2007 if (up->capabilities & UART_CAP_UUE)
2008 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
2010 serial_out(up, UART_IER, up->ier);
2012 if (up->capabilities & UART_CAP_EFR) {
2013 unsigned char efr = 0;
2015 * TI16C752/Startech hardware flow control. FIXME:
2016 * - TI16C752 requires control thresholds to be set.
2017 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2019 if (termios->c_cflag & CRTSCTS)
2020 efr |= UART_EFR_CTS;
2022 serial_outp(up, UART_LCR, 0xBF);
2023 serial_outp(up, UART_EFR, efr);
2026 #ifdef CONFIG_ARCH_OMAP15XX
2027 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2028 if (cpu_is_omap1510() && is_omap_port((unsigned int)up->port.membase)) {
2029 if (baud == 115200) {
2031 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2033 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2037 if (up->capabilities & UART_NATSEMI) {
2038 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2039 serial_outp(up, UART_LCR, 0xe0);
2041 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2044 serial_dl_write(up, quot);
2047 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2048 * is written without DLAB set, this mode will be disabled.
2050 if (up->port.type == PORT_16750)
2051 serial_outp(up, UART_FCR, fcr);
2053 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2054 up->lcr = cval; /* Save LCR */
2055 if (up->port.type != PORT_16750) {
2056 if (fcr & UART_FCR_ENABLE_FIFO) {
2057 /* emulated UARTs (Lucent Venus 167x) need two steps */
2058 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2060 serial_outp(up, UART_FCR, fcr); /* set fcr */
2062 serial8250_set_mctrl(&up->port, up->port.mctrl);
2063 spin_unlock_irqrestore(&up->port.lock, flags);
2067 serial8250_pm(struct uart_port *port, unsigned int state,
2068 unsigned int oldstate)
2070 struct uart_8250_port *p = (struct uart_8250_port *)port;
2072 serial8250_set_sleep(p, state != 0);
2075 p->pm(port, state, oldstate);
2079 * Resource handling.
2081 static int serial8250_request_std_resource(struct uart_8250_port *up)
2083 unsigned int size = 8 << up->port.regshift;
2086 switch (up->port.iotype) {
2093 if (!up->port.mapbase)
2096 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2101 if (up->port.flags & UPF_IOREMAP) {
2102 up->port.membase = ioremap(up->port.mapbase, size);
2103 if (!up->port.membase) {
2104 release_mem_region(up->port.mapbase, size);
2112 if (!request_region(up->port.iobase, size, "serial"))
2119 static void serial8250_release_std_resource(struct uart_8250_port *up)
2121 unsigned int size = 8 << up->port.regshift;
2123 switch (up->port.iotype) {
2130 if (!up->port.mapbase)
2133 if (up->port.flags & UPF_IOREMAP) {
2134 iounmap(up->port.membase);
2135 up->port.membase = NULL;
2138 release_mem_region(up->port.mapbase, size);
2143 release_region(up->port.iobase, size);
2148 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2150 unsigned long start = UART_RSA_BASE << up->port.regshift;
2151 unsigned int size = 8 << up->port.regshift;
2154 switch (up->port.iotype) {
2157 start += up->port.iobase;
2158 if (request_region(start, size, "serial-rsa"))
2168 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2170 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2171 unsigned int size = 8 << up->port.regshift;
2173 switch (up->port.iotype) {
2176 release_region(up->port.iobase + offset, size);
2181 static void serial8250_release_port(struct uart_port *port)
2183 struct uart_8250_port *up = (struct uart_8250_port *)port;
2185 serial8250_release_std_resource(up);
2186 if (up->port.type == PORT_RSA)
2187 serial8250_release_rsa_resource(up);
2190 static int serial8250_request_port(struct uart_port *port)
2192 struct uart_8250_port *up = (struct uart_8250_port *)port;
2195 ret = serial8250_request_std_resource(up);
2196 if (ret == 0 && up->port.type == PORT_RSA) {
2197 ret = serial8250_request_rsa_resource(up);
2199 serial8250_release_std_resource(up);
2205 static void serial8250_config_port(struct uart_port *port, int flags)
2207 struct uart_8250_port *up = (struct uart_8250_port *)port;
2208 int probeflags = PROBE_ANY;
2212 * Find the region that we can probe for. This in turn
2213 * tells us whether we can probe for the type of port.
2215 ret = serial8250_request_std_resource(up);
2219 ret = serial8250_request_rsa_resource(up);
2221 probeflags &= ~PROBE_RSA;
2223 if (flags & UART_CONFIG_TYPE)
2224 autoconfig(up, probeflags);
2225 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2228 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2229 serial8250_release_rsa_resource(up);
2230 if (up->port.type == PORT_UNKNOWN)
2231 serial8250_release_std_resource(up);
2235 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2237 if (ser->irq >= NR_IRQS || ser->irq < 0 ||
2238 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2239 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2240 ser->type == PORT_STARTECH)
2246 serial8250_type(struct uart_port *port)
2248 int type = port->type;
2250 if (type >= ARRAY_SIZE(uart_config))
2252 return uart_config[type].name;
2255 static struct uart_ops serial8250_pops = {
2256 .tx_empty = serial8250_tx_empty,
2257 .set_mctrl = serial8250_set_mctrl,
2258 .get_mctrl = serial8250_get_mctrl,
2259 .stop_tx = serial8250_stop_tx,
2260 .start_tx = serial8250_start_tx,
2261 .stop_rx = serial8250_stop_rx,
2262 .enable_ms = serial8250_enable_ms,
2263 .break_ctl = serial8250_break_ctl,
2264 .startup = serial8250_startup,
2265 .shutdown = serial8250_shutdown,
2266 .set_termios = serial8250_set_termios,
2267 .pm = serial8250_pm,
2268 .type = serial8250_type,
2269 .release_port = serial8250_release_port,
2270 .request_port = serial8250_request_port,
2271 .config_port = serial8250_config_port,
2272 .verify_port = serial8250_verify_port,
2275 static struct uart_8250_port serial8250_ports[UART_NR];
2277 static void __init serial8250_isa_init_ports(void)
2279 struct uart_8250_port *up;
2280 static int first = 1;
2287 for (i = 0; i < nr_uarts; i++) {
2288 struct uart_8250_port *up = &serial8250_ports[i];
2291 spin_lock_init(&up->port.lock);
2293 init_timer(&up->timer);
2294 up->timer.function = serial8250_timeout;
2297 * ALPHA_KLUDGE_MCR needs to be killed.
2299 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2300 up->mcr_force = ALPHA_KLUDGE_MCR;
2302 up->port.ops = &serial8250_pops;
2305 for (i = 0, up = serial8250_ports;
2306 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
2308 up->port.iobase = old_serial_port[i].port;
2309 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
2310 up->port.uartclk = old_serial_port[i].baud_base * 16;
2311 up->port.flags = old_serial_port[i].flags;
2312 up->port.hub6 = old_serial_port[i].hub6;
2313 up->port.membase = old_serial_port[i].iomem_base;
2314 up->port.iotype = old_serial_port[i].io_type;
2315 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2317 up->port.flags |= UPF_SHARE_IRQ;
2322 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2326 serial8250_isa_init_ports();
2328 for (i = 0; i < nr_uarts; i++) {
2329 struct uart_8250_port *up = &serial8250_ports[i];
2332 uart_add_one_port(drv, &up->port);
2336 #ifdef CONFIG_SERIAL_8250_CONSOLE
2338 static void serial8250_console_putchar(struct uart_port *port, int ch)
2340 struct uart_8250_port *up = (struct uart_8250_port *)port;
2342 wait_for_xmitr(up, UART_LSR_THRE);
2343 serial_out(up, UART_TX, ch);
2347 * Print a string to the serial port trying not to disturb
2348 * any possible real use of the port...
2350 * The console_lock must be held when we get here.
2353 serial8250_console_write(struct console *co, const char *s, unsigned int count)
2355 struct uart_8250_port *up = &serial8250_ports[co->index];
2356 unsigned long flags;
2360 touch_nmi_watchdog();
2362 local_irq_save(flags);
2363 if (up->port.sysrq) {
2364 /* serial8250_handle_port() already took the lock */
2366 } else if (oops_in_progress) {
2367 locked = spin_trylock(&up->port.lock);
2369 spin_lock(&up->port.lock);
2372 * First save the IER then disable the interrupts
2374 ier = serial_in(up, UART_IER);
2376 if (up->capabilities & UART_CAP_UUE)
2377 serial_out(up, UART_IER, UART_IER_UUE);
2379 serial_out(up, UART_IER, 0);
2381 uart_console_write(&up->port, s, count, serial8250_console_putchar);
2384 * Finally, wait for transmitter to become empty
2385 * and restore the IER
2387 wait_for_xmitr(up, BOTH_EMPTY);
2388 serial_out(up, UART_IER, ier);
2391 spin_unlock(&up->port.lock);
2392 local_irq_restore(flags);
2395 static int __init serial8250_console_setup(struct console *co, char *options)
2397 struct uart_port *port;
2404 * Check whether an invalid uart number has been specified, and
2405 * if so, search for the first available port that does have
2408 if (co->index >= nr_uarts)
2410 port = &serial8250_ports[co->index].port;
2411 if (!port->iobase && !port->membase)
2415 uart_parse_options(options, &baud, &parity, &bits, &flow);
2417 return uart_set_options(port, co, baud, parity, bits, flow);
2420 static struct uart_driver serial8250_reg;
2421 static struct console serial8250_console = {
2423 .write = serial8250_console_write,
2424 .device = uart_console_device,
2425 .setup = serial8250_console_setup,
2426 .flags = CON_PRINTBUFFER,
2428 .data = &serial8250_reg,
2431 static int __init serial8250_console_init(void)
2433 serial8250_isa_init_ports();
2434 register_console(&serial8250_console);
2437 console_initcall(serial8250_console_init);
2439 static int __init find_port(struct uart_port *p)
2442 struct uart_port *port;
2444 for (line = 0; line < nr_uarts; line++) {
2445 port = &serial8250_ports[line].port;
2446 if (uart_match_port(p, port))
2452 int __init serial8250_start_console(struct uart_port *port, char *options)
2456 line = find_port(port);
2460 add_preferred_console("ttyS", line, options);
2461 printk("Adding console on ttyS%d at %s 0x%lx (options '%s')\n",
2462 line, port->iotype == UPIO_MEM ? "MMIO" : "I/O port",
2463 port->iotype == UPIO_MEM ? (unsigned long) port->mapbase :
2464 (unsigned long) port->iobase, options);
2465 if (!(serial8250_console.flags & CON_ENABLED)) {
2466 serial8250_console.flags &= ~CON_PRINTBUFFER;
2467 register_console(&serial8250_console);
2472 #define SERIAL8250_CONSOLE &serial8250_console
2474 #define SERIAL8250_CONSOLE NULL
2477 static struct uart_driver serial8250_reg = {
2478 .owner = THIS_MODULE,
2479 .driver_name = "serial",
2484 .cons = SERIAL8250_CONSOLE,
2488 * early_serial_setup - early registration for 8250 ports
2490 * Setup an 8250 port structure prior to console initialisation. Use
2491 * after console initialisation will cause undefined behaviour.
2493 int __init early_serial_setup(struct uart_port *port)
2495 if (port->line >= ARRAY_SIZE(serial8250_ports))
2498 serial8250_isa_init_ports();
2499 serial8250_ports[port->line].port = *port;
2500 serial8250_ports[port->line].port.ops = &serial8250_pops;
2505 * serial8250_suspend_port - suspend one serial port
2506 * @line: serial line number
2508 * Suspend one serial port.
2510 void serial8250_suspend_port(int line)
2512 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2516 * serial8250_resume_port - resume one serial port
2517 * @line: serial line number
2519 * Resume one serial port.
2521 void serial8250_resume_port(int line)
2523 uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
2527 * Register a set of serial devices attached to a platform device. The
2528 * list is terminated with a zero flags entry, which means we expect
2529 * all entries to have at least UPF_BOOT_AUTOCONF set.
2531 static int __devinit serial8250_probe(struct platform_device *dev)
2533 struct plat_serial8250_port *p = dev->dev.platform_data;
2534 struct uart_port port;
2537 memset(&port, 0, sizeof(struct uart_port));
2539 for (i = 0; p && p->flags != 0; p++, i++) {
2540 port.iobase = p->iobase;
2541 port.membase = p->membase;
2543 port.uartclk = p->uartclk;
2544 port.regshift = p->regshift;
2545 port.iotype = p->iotype;
2546 port.flags = p->flags;
2547 port.mapbase = p->mapbase;
2548 port.hub6 = p->hub6;
2549 port.dev = &dev->dev;
2551 port.flags |= UPF_SHARE_IRQ;
2552 ret = serial8250_register_port(&port);
2554 dev_err(&dev->dev, "unable to register port at index %d "
2555 "(IO%lx MEM%lx IRQ%d): %d\n", i,
2556 p->iobase, p->mapbase, p->irq, ret);
2563 * Remove serial ports registered against a platform device.
2565 static int __devexit serial8250_remove(struct platform_device *dev)
2569 for (i = 0; i < nr_uarts; i++) {
2570 struct uart_8250_port *up = &serial8250_ports[i];
2572 if (up->port.dev == &dev->dev)
2573 serial8250_unregister_port(i);
2578 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
2582 for (i = 0; i < UART_NR; i++) {
2583 struct uart_8250_port *up = &serial8250_ports[i];
2585 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
2586 uart_suspend_port(&serial8250_reg, &up->port);
2592 static int serial8250_resume(struct platform_device *dev)
2596 for (i = 0; i < UART_NR; i++) {
2597 struct uart_8250_port *up = &serial8250_ports[i];
2599 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
2600 uart_resume_port(&serial8250_reg, &up->port);
2606 static struct platform_driver serial8250_isa_driver = {
2607 .probe = serial8250_probe,
2608 .remove = __devexit_p(serial8250_remove),
2609 .suspend = serial8250_suspend,
2610 .resume = serial8250_resume,
2612 .name = "serial8250",
2613 .owner = THIS_MODULE,
2618 * This "device" covers _all_ ISA 8250-compatible serial devices listed
2619 * in the table in include/asm/serial.h
2621 static struct platform_device *serial8250_isa_devs;
2624 * serial8250_register_port and serial8250_unregister_port allows for
2625 * 16x50 serial ports to be configured at run-time, to support PCMCIA
2626 * modems and PCI multiport cards.
2628 static DEFINE_MUTEX(serial_mutex);
2630 static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
2635 * First, find a port entry which matches.
2637 for (i = 0; i < nr_uarts; i++)
2638 if (uart_match_port(&serial8250_ports[i].port, port))
2639 return &serial8250_ports[i];
2642 * We didn't find a matching entry, so look for the first
2643 * free entry. We look for one which hasn't been previously
2644 * used (indicated by zero iobase).
2646 for (i = 0; i < nr_uarts; i++)
2647 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
2648 serial8250_ports[i].port.iobase == 0)
2649 return &serial8250_ports[i];
2652 * That also failed. Last resort is to find any entry which
2653 * doesn't have a real port associated with it.
2655 for (i = 0; i < nr_uarts; i++)
2656 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
2657 return &serial8250_ports[i];
2663 * serial8250_register_port - register a serial port
2664 * @port: serial port template
2666 * Configure the serial port specified by the request. If the
2667 * port exists and is in use, it is hung up and unregistered
2670 * The port is then probed and if necessary the IRQ is autodetected
2671 * If this fails an error is returned.
2673 * On success the port is ready to use and the line number is returned.
2675 int serial8250_register_port(struct uart_port *port)
2677 struct uart_8250_port *uart;
2680 if (port->uartclk == 0)
2683 mutex_lock(&serial_mutex);
2685 uart = serial8250_find_match_or_unused(port);
2687 uart_remove_one_port(&serial8250_reg, &uart->port);
2689 uart->port.iobase = port->iobase;
2690 uart->port.membase = port->membase;
2691 uart->port.irq = port->irq;
2692 uart->port.uartclk = port->uartclk;
2693 uart->port.fifosize = port->fifosize;
2694 uart->port.regshift = port->regshift;
2695 uart->port.iotype = port->iotype;
2696 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
2697 uart->port.mapbase = port->mapbase;
2699 uart->port.dev = port->dev;
2701 ret = uart_add_one_port(&serial8250_reg, &uart->port);
2703 ret = uart->port.line;
2705 mutex_unlock(&serial_mutex);
2709 EXPORT_SYMBOL(serial8250_register_port);
2712 * serial8250_unregister_port - remove a 16x50 serial port at runtime
2713 * @line: serial line number
2715 * Remove one serial port. This may not be called from interrupt
2716 * context. We hand the port back to the our control.
2718 void serial8250_unregister_port(int line)
2720 struct uart_8250_port *uart = &serial8250_ports[line];
2722 mutex_lock(&serial_mutex);
2723 uart_remove_one_port(&serial8250_reg, &uart->port);
2724 if (serial8250_isa_devs) {
2725 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
2726 uart->port.type = PORT_UNKNOWN;
2727 uart->port.dev = &serial8250_isa_devs->dev;
2728 uart_add_one_port(&serial8250_reg, &uart->port);
2730 uart->port.dev = NULL;
2732 mutex_unlock(&serial_mutex);
2734 EXPORT_SYMBOL(serial8250_unregister_port);
2736 static int __init serial8250_init(void)
2740 if (nr_uarts > UART_NR)
2743 printk(KERN_INFO "Serial: 8250/16550 driver $Revision: 1.90 $ "
2744 "%d ports, IRQ sharing %sabled\n", nr_uarts,
2745 share_irqs ? "en" : "dis");
2747 for (i = 0; i < NR_IRQS; i++)
2748 spin_lock_init(&irq_lists[i].lock);
2750 ret = uart_register_driver(&serial8250_reg);
2754 serial8250_isa_devs = platform_device_alloc("serial8250",
2755 PLAT8250_DEV_LEGACY);
2756 if (!serial8250_isa_devs) {
2758 goto unreg_uart_drv;
2761 ret = platform_device_add(serial8250_isa_devs);
2765 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
2767 ret = platform_driver_register(&serial8250_isa_driver);
2771 platform_device_del(serial8250_isa_devs);
2773 platform_device_put(serial8250_isa_devs);
2775 uart_unregister_driver(&serial8250_reg);
2780 static void __exit serial8250_exit(void)
2782 struct platform_device *isa_dev = serial8250_isa_devs;
2785 * This tells serial8250_unregister_port() not to re-register
2786 * the ports (thereby making serial8250_isa_driver permanently
2789 serial8250_isa_devs = NULL;
2791 platform_driver_unregister(&serial8250_isa_driver);
2792 platform_device_unregister(isa_dev);
2794 uart_unregister_driver(&serial8250_reg);
2797 module_init(serial8250_init);
2798 module_exit(serial8250_exit);
2800 EXPORT_SYMBOL(serial8250_suspend_port);
2801 EXPORT_SYMBOL(serial8250_resume_port);
2803 MODULE_LICENSE("GPL");
2804 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver $Revision: 1.90 $");
2806 module_param(share_irqs, uint, 0644);
2807 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
2810 module_param(nr_uarts, uint, 0644);
2811 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
2813 #ifdef CONFIG_SERIAL_8250_RSA
2814 module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
2815 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
2817 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);