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 * A note about mapbase / membase
17 * mapbase is the physical address of the IO port.
18 * membase is an 'ioremapped' cookie.
21 #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/ioport.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
30 #include <linux/sysrq.h>
31 #include <linux/delay.h>
32 #include <linux/platform_device.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/serial_reg.h>
36 #include <linux/serial_core.h>
37 #include <linux/serial.h>
38 #include <linux/serial_8250.h>
39 #include <linux/nmi.h>
40 #include <linux/mutex.h>
53 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
54 * is unsafe when used on edge-triggered interrupts.
56 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
58 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
60 static struct uart_driver serial8250_reg;
62 static int serial_index(struct uart_port *port)
64 return (serial8250_reg.minor - 64) + port->line;
71 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
73 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
77 #define DEBUG_INTR(fmt...) printk(fmt)
79 #define DEBUG_INTR(fmt...) do { } while (0)
82 #define PASS_LIMIT 256
85 * We default to IRQ0 for the "no irq" hack. Some
86 * machine types want others as well - they're free
87 * to redefine this in their header file.
89 #define is_real_interrupt(irq) ((irq) != 0)
91 #ifdef CONFIG_SERIAL_8250_DETECT_IRQ
92 #define CONFIG_SERIAL_DETECT_IRQ 1
94 #ifdef CONFIG_SERIAL_8250_MANY_PORTS
95 #define CONFIG_SERIAL_MANY_PORTS 1
99 * HUB6 is always on. This will be removed once the header
100 * files have been cleaned.
102 #define CONFIG_HUB6 1
104 #include <asm/serial.h>
106 * SERIAL_PORT_DFNS tells us about built-in ports that have no
107 * standard enumeration mechanism. Platforms that can find all
108 * serial ports via mechanisms like ACPI or PCI need not supply it.
110 #ifndef SERIAL_PORT_DFNS
111 #define SERIAL_PORT_DFNS
114 static const struct old_serial_port old_serial_port[] = {
115 SERIAL_PORT_DFNS /* defined in asm/serial.h */
118 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
120 #ifdef CONFIG_SERIAL_8250_RSA
122 #define PORT_RSA_MAX 4
123 static unsigned long probe_rsa[PORT_RSA_MAX];
124 static unsigned int probe_rsa_count;
125 #endif /* CONFIG_SERIAL_8250_RSA */
127 struct uart_8250_port {
128 struct uart_port port;
129 struct timer_list timer; /* "no irq" timer */
130 struct list_head list; /* ports on this IRQ */
131 unsigned short capabilities; /* port capabilities */
132 unsigned short bugs; /* port bugs */
133 unsigned int tx_loadsz; /* transmit fifo load size */
138 unsigned char mcr_mask; /* mask of user bits */
139 unsigned char mcr_force; /* mask of forced bits */
142 * Some bits in registers are cleared on a read, so they must
143 * be saved whenever the register is read but the bits will not
144 * be immediately processed.
146 #define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS
147 unsigned char lsr_saved_flags;
148 #define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
149 unsigned char msr_saved_flags;
152 * We provide a per-port pm hook.
154 void (*pm)(struct uart_port *port,
155 unsigned int state, unsigned int old);
159 struct hlist_node node;
161 spinlock_t lock; /* Protects list not the hash */
162 struct list_head *head;
165 #define NR_IRQ_HASH 32 /* Can be adjusted later */
166 static struct hlist_head irq_lists[NR_IRQ_HASH];
167 static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
170 * Here we define the default xmit fifo size used for each type of UART.
172 static const struct serial8250_config uart_config[] = {
197 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
198 .flags = UART_CAP_FIFO,
209 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
215 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
217 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
223 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
225 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
233 .name = "16C950/954",
236 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
237 .flags = UART_CAP_FIFO,
243 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
245 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
251 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
252 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
258 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
259 .flags = UART_CAP_FIFO,
265 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
266 .flags = UART_CAP_FIFO | UART_NATSEMI,
272 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
273 .flags = UART_CAP_FIFO | UART_CAP_UUE,
279 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
280 .flags = UART_CAP_FIFO,
284 #if defined (CONFIG_SERIAL_8250_AU1X00)
286 /* Au1x00 UART hardware has a weird register layout */
287 static const u8 au_io_in_map[] = {
297 static const u8 au_io_out_map[] = {
305 /* sane hardware needs no mapping */
306 static inline int map_8250_in_reg(struct uart_8250_port *up, int offset)
308 if (up->port.iotype != UPIO_AU)
310 return au_io_in_map[offset];
313 static inline int map_8250_out_reg(struct uart_8250_port *up, int offset)
315 if (up->port.iotype != UPIO_AU)
317 return au_io_out_map[offset];
320 #elif defined(CONFIG_SERIAL_8250_RM9K)
344 static inline int map_8250_in_reg(struct uart_8250_port *up, int offset)
346 if (up->port.iotype != UPIO_RM9000)
348 return regmap_in[offset];
351 static inline int map_8250_out_reg(struct uart_8250_port *up, int offset)
353 if (up->port.iotype != UPIO_RM9000)
355 return regmap_out[offset];
360 /* sane hardware needs no mapping */
361 #define map_8250_in_reg(up, offset) (offset)
362 #define map_8250_out_reg(up, offset) (offset)
366 static unsigned int serial_in(struct uart_8250_port *up, int offset)
369 offset = map_8250_in_reg(up, offset) << up->port.regshift;
371 switch (up->port.iotype) {
373 outb(up->port.hub6 - 1 + offset, up->port.iobase);
374 return inb(up->port.iobase + 1);
378 return readb(up->port.membase + offset);
382 return readl(up->port.membase + offset);
384 #ifdef CONFIG_SERIAL_8250_AU1X00
386 return __raw_readl(up->port.membase + offset);
390 if (offset == UART_IIR) {
391 tmp = readl(up->port.membase + (UART_IIR & ~3));
392 return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
394 return readb(up->port.membase + offset);
397 return inb(up->port.iobase + offset);
402 serial_out(struct uart_8250_port *up, int offset, int value)
404 /* Save the offset before it's remapped */
405 int save_offset = offset;
406 offset = map_8250_out_reg(up, offset) << up->port.regshift;
408 switch (up->port.iotype) {
410 outb(up->port.hub6 - 1 + offset, up->port.iobase);
411 outb(value, up->port.iobase + 1);
415 writeb(value, up->port.membase + offset);
420 writel(value, up->port.membase + offset);
423 #ifdef CONFIG_SERIAL_8250_AU1X00
425 __raw_writel(value, up->port.membase + offset);
429 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
430 writeb(value, up->port.membase + offset);
434 /* Save the LCR value so it can be re-written when a
435 * Busy Detect interrupt occurs. */
436 if (save_offset == UART_LCR)
438 writeb(value, up->port.membase + offset);
439 /* Read the IER to ensure any interrupt is cleared before
440 * returning from ISR. */
441 if (save_offset == UART_TX || save_offset == UART_IER)
442 value = serial_in(up, UART_IER);
446 outb(value, up->port.iobase + offset);
451 serial_out_sync(struct uart_8250_port *up, int offset, int value)
453 switch (up->port.iotype) {
456 #ifdef CONFIG_SERIAL_8250_AU1X00
460 serial_out(up, offset, value);
461 serial_in(up, UART_LCR); /* safe, no side-effects */
464 serial_out(up, offset, value);
469 * We used to support using pause I/O for certain machines. We
470 * haven't supported this for a while, but just in case it's badly
471 * needed for certain old 386 machines, I've left these #define's
474 #define serial_inp(up, offset) serial_in(up, offset)
475 #define serial_outp(up, offset, value) serial_out(up, offset, value)
477 /* Uart divisor latch read */
478 static inline int _serial_dl_read(struct uart_8250_port *up)
480 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
483 /* Uart divisor latch write */
484 static inline void _serial_dl_write(struct uart_8250_port *up, int value)
486 serial_outp(up, UART_DLL, value & 0xff);
487 serial_outp(up, UART_DLM, value >> 8 & 0xff);
490 #if defined(CONFIG_SERIAL_8250_AU1X00)
491 /* Au1x00 haven't got a standard divisor latch */
492 static int serial_dl_read(struct uart_8250_port *up)
494 if (up->port.iotype == UPIO_AU)
495 return __raw_readl(up->port.membase + 0x28);
497 return _serial_dl_read(up);
500 static void serial_dl_write(struct uart_8250_port *up, int value)
502 if (up->port.iotype == UPIO_AU)
503 __raw_writel(value, up->port.membase + 0x28);
505 _serial_dl_write(up, value);
507 #elif defined(CONFIG_SERIAL_8250_RM9K)
508 static int serial_dl_read(struct uart_8250_port *up)
510 return (up->port.iotype == UPIO_RM9000) ?
511 (((__raw_readl(up->port.membase + 0x10) << 8) |
512 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
516 static void serial_dl_write(struct uart_8250_port *up, int value)
518 if (up->port.iotype == UPIO_RM9000) {
519 __raw_writel(value, up->port.membase + 0x08);
520 __raw_writel(value >> 8, up->port.membase + 0x10);
522 _serial_dl_write(up, value);
526 #define serial_dl_read(up) _serial_dl_read(up)
527 #define serial_dl_write(up, value) _serial_dl_write(up, value)
533 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
535 serial_out(up, UART_SCR, offset);
536 serial_out(up, UART_ICR, value);
539 static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
543 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
544 serial_out(up, UART_SCR, offset);
545 value = serial_in(up, UART_ICR);
546 serial_icr_write(up, UART_ACR, up->acr);
554 static void serial8250_clear_fifos(struct uart_8250_port *p)
556 if (p->capabilities & UART_CAP_FIFO) {
557 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
558 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
559 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
560 serial_outp(p, UART_FCR, 0);
565 * IER sleep support. UARTs which have EFRs need the "extended
566 * capability" bit enabled. Note that on XR16C850s, we need to
567 * reset LCR to write to IER.
569 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
571 if (p->capabilities & UART_CAP_SLEEP) {
572 if (p->capabilities & UART_CAP_EFR) {
573 serial_outp(p, UART_LCR, 0xBF);
574 serial_outp(p, UART_EFR, UART_EFR_ECB);
575 serial_outp(p, UART_LCR, 0);
577 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
578 if (p->capabilities & UART_CAP_EFR) {
579 serial_outp(p, UART_LCR, 0xBF);
580 serial_outp(p, UART_EFR, 0);
581 serial_outp(p, UART_LCR, 0);
586 #ifdef CONFIG_SERIAL_8250_RSA
588 * Attempts to turn on the RSA FIFO. Returns zero on failure.
589 * We set the port uart clock rate if we succeed.
591 static int __enable_rsa(struct uart_8250_port *up)
596 mode = serial_inp(up, UART_RSA_MSR);
597 result = mode & UART_RSA_MSR_FIFO;
600 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
601 mode = serial_inp(up, UART_RSA_MSR);
602 result = mode & UART_RSA_MSR_FIFO;
606 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
611 static void enable_rsa(struct uart_8250_port *up)
613 if (up->port.type == PORT_RSA) {
614 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
615 spin_lock_irq(&up->port.lock);
617 spin_unlock_irq(&up->port.lock);
619 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
620 serial_outp(up, UART_RSA_FRR, 0);
625 * Attempts to turn off the RSA FIFO. Returns zero on failure.
626 * It is unknown why interrupts were disabled in here. However,
627 * the caller is expected to preserve this behaviour by grabbing
628 * the spinlock before calling this function.
630 static void disable_rsa(struct uart_8250_port *up)
635 if (up->port.type == PORT_RSA &&
636 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
637 spin_lock_irq(&up->port.lock);
639 mode = serial_inp(up, UART_RSA_MSR);
640 result = !(mode & UART_RSA_MSR_FIFO);
643 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
644 mode = serial_inp(up, UART_RSA_MSR);
645 result = !(mode & UART_RSA_MSR_FIFO);
649 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
650 spin_unlock_irq(&up->port.lock);
653 #endif /* CONFIG_SERIAL_8250_RSA */
656 * This is a quickie test to see how big the FIFO is.
657 * It doesn't work at all the time, more's the pity.
659 static int size_fifo(struct uart_8250_port *up)
661 unsigned char old_fcr, old_mcr, old_lcr;
662 unsigned short old_dl;
665 old_lcr = serial_inp(up, UART_LCR);
666 serial_outp(up, UART_LCR, 0);
667 old_fcr = serial_inp(up, UART_FCR);
668 old_mcr = serial_inp(up, UART_MCR);
669 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
670 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
671 serial_outp(up, UART_MCR, UART_MCR_LOOP);
672 serial_outp(up, UART_LCR, UART_LCR_DLAB);
673 old_dl = serial_dl_read(up);
674 serial_dl_write(up, 0x0001);
675 serial_outp(up, UART_LCR, 0x03);
676 for (count = 0; count < 256; count++)
677 serial_outp(up, UART_TX, count);
678 mdelay(20);/* FIXME - schedule_timeout */
679 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
680 (count < 256); count++)
681 serial_inp(up, UART_RX);
682 serial_outp(up, UART_FCR, old_fcr);
683 serial_outp(up, UART_MCR, old_mcr);
684 serial_outp(up, UART_LCR, UART_LCR_DLAB);
685 serial_dl_write(up, old_dl);
686 serial_outp(up, UART_LCR, old_lcr);
692 * Read UART ID using the divisor method - set DLL and DLM to zero
693 * and the revision will be in DLL and device type in DLM. We
694 * preserve the device state across this.
696 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
698 unsigned char old_dll, old_dlm, old_lcr;
701 old_lcr = serial_inp(p, UART_LCR);
702 serial_outp(p, UART_LCR, UART_LCR_DLAB);
704 old_dll = serial_inp(p, UART_DLL);
705 old_dlm = serial_inp(p, UART_DLM);
707 serial_outp(p, UART_DLL, 0);
708 serial_outp(p, UART_DLM, 0);
710 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
712 serial_outp(p, UART_DLL, old_dll);
713 serial_outp(p, UART_DLM, old_dlm);
714 serial_outp(p, UART_LCR, old_lcr);
720 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
721 * When this function is called we know it is at least a StarTech
722 * 16650 V2, but it might be one of several StarTech UARTs, or one of
723 * its clones. (We treat the broken original StarTech 16650 V1 as a
724 * 16550, and why not? Startech doesn't seem to even acknowledge its
727 * What evil have men's minds wrought...
729 static void autoconfig_has_efr(struct uart_8250_port *up)
731 unsigned int id1, id2, id3, rev;
734 * Everything with an EFR has SLEEP
736 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
739 * First we check to see if it's an Oxford Semiconductor UART.
741 * If we have to do this here because some non-National
742 * Semiconductor clone chips lock up if you try writing to the
743 * LSR register (which serial_icr_read does)
747 * Check for Oxford Semiconductor 16C950.
749 * EFR [4] must be set else this test fails.
751 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
752 * claims that it's needed for 952 dual UART's (which are not
753 * recommended for new designs).
756 serial_out(up, UART_LCR, 0xBF);
757 serial_out(up, UART_EFR, UART_EFR_ECB);
758 serial_out(up, UART_LCR, 0x00);
759 id1 = serial_icr_read(up, UART_ID1);
760 id2 = serial_icr_read(up, UART_ID2);
761 id3 = serial_icr_read(up, UART_ID3);
762 rev = serial_icr_read(up, UART_REV);
764 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
766 if (id1 == 0x16 && id2 == 0xC9 &&
767 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
768 up->port.type = PORT_16C950;
771 * Enable work around for the Oxford Semiconductor 952 rev B
772 * chip which causes it to seriously miscalculate baud rates
775 if (id3 == 0x52 && rev == 0x01)
776 up->bugs |= UART_BUG_QUOT;
781 * We check for a XR16C850 by setting DLL and DLM to 0, and then
782 * reading back DLL and DLM. The chip type depends on the DLM
784 * 0x10 - XR16C850 and the DLL contains the chip revision.
788 id1 = autoconfig_read_divisor_id(up);
789 DEBUG_AUTOCONF("850id=%04x ", id1);
792 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
793 up->port.type = PORT_16850;
798 * It wasn't an XR16C850.
800 * We distinguish between the '654 and the '650 by counting
801 * how many bytes are in the FIFO. I'm using this for now,
802 * since that's the technique that was sent to me in the
803 * serial driver update, but I'm not convinced this works.
804 * I've had problems doing this in the past. -TYT
806 if (size_fifo(up) == 64)
807 up->port.type = PORT_16654;
809 up->port.type = PORT_16650V2;
813 * We detected a chip without a FIFO. Only two fall into
814 * this category - the original 8250 and the 16450. The
815 * 16450 has a scratch register (accessible with LCR=0)
817 static void autoconfig_8250(struct uart_8250_port *up)
819 unsigned char scratch, status1, status2;
821 up->port.type = PORT_8250;
823 scratch = serial_in(up, UART_SCR);
824 serial_outp(up, UART_SCR, 0xa5);
825 status1 = serial_in(up, UART_SCR);
826 serial_outp(up, UART_SCR, 0x5a);
827 status2 = serial_in(up, UART_SCR);
828 serial_outp(up, UART_SCR, scratch);
830 if (status1 == 0xa5 && status2 == 0x5a)
831 up->port.type = PORT_16450;
834 static int broken_efr(struct uart_8250_port *up)
837 * Exar ST16C2550 "A2" devices incorrectly detect as
838 * having an EFR, and report an ID of 0x0201. See
839 * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf
841 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
848 * We know that the chip has FIFOs. Does it have an EFR? The
849 * EFR is located in the same register position as the IIR and
850 * we know the top two bits of the IIR are currently set. The
851 * EFR should contain zero. Try to read the EFR.
853 static void autoconfig_16550a(struct uart_8250_port *up)
855 unsigned char status1, status2;
856 unsigned int iersave;
858 up->port.type = PORT_16550A;
859 up->capabilities |= UART_CAP_FIFO;
862 * Check for presence of the EFR when DLAB is set.
863 * Only ST16C650V1 UARTs pass this test.
865 serial_outp(up, UART_LCR, UART_LCR_DLAB);
866 if (serial_in(up, UART_EFR) == 0) {
867 serial_outp(up, UART_EFR, 0xA8);
868 if (serial_in(up, UART_EFR) != 0) {
869 DEBUG_AUTOCONF("EFRv1 ");
870 up->port.type = PORT_16650;
871 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
873 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
875 serial_outp(up, UART_EFR, 0);
880 * Maybe it requires 0xbf to be written to the LCR.
881 * (other ST16C650V2 UARTs, TI16C752A, etc)
883 serial_outp(up, UART_LCR, 0xBF);
884 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
885 DEBUG_AUTOCONF("EFRv2 ");
886 autoconfig_has_efr(up);
891 * Check for a National Semiconductor SuperIO chip.
892 * Attempt to switch to bank 2, read the value of the LOOP bit
893 * from EXCR1. Switch back to bank 0, change it in MCR. Then
894 * switch back to bank 2, read it from EXCR1 again and check
895 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
897 serial_outp(up, UART_LCR, 0);
898 status1 = serial_in(up, UART_MCR);
899 serial_outp(up, UART_LCR, 0xE0);
900 status2 = serial_in(up, 0x02); /* EXCR1 */
902 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
903 serial_outp(up, UART_LCR, 0);
904 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
905 serial_outp(up, UART_LCR, 0xE0);
906 status2 = serial_in(up, 0x02); /* EXCR1 */
907 serial_outp(up, UART_LCR, 0);
908 serial_outp(up, UART_MCR, status1);
910 if ((status2 ^ status1) & UART_MCR_LOOP) {
913 serial_outp(up, UART_LCR, 0xE0);
915 quot = serial_dl_read(up);
918 status1 = serial_in(up, 0x04); /* EXCR2 */
919 status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
920 status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
921 serial_outp(up, 0x04, status1);
923 serial_dl_write(up, quot);
925 serial_outp(up, UART_LCR, 0);
927 up->port.uartclk = 921600*16;
928 up->port.type = PORT_NS16550A;
929 up->capabilities |= UART_NATSEMI;
935 * No EFR. Try to detect a TI16750, which only sets bit 5 of
936 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
937 * Try setting it with and without DLAB set. Cheap clones
938 * set bit 5 without DLAB set.
940 serial_outp(up, UART_LCR, 0);
941 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
942 status1 = serial_in(up, UART_IIR) >> 5;
943 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
944 serial_outp(up, UART_LCR, UART_LCR_DLAB);
945 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
946 status2 = serial_in(up, UART_IIR) >> 5;
947 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
948 serial_outp(up, UART_LCR, 0);
950 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
952 if (status1 == 6 && status2 == 7) {
953 up->port.type = PORT_16750;
954 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
959 * Try writing and reading the UART_IER_UUE bit (b6).
960 * If it works, this is probably one of the Xscale platform's
962 * We're going to explicitly set the UUE bit to 0 before
963 * trying to write and read a 1 just to make sure it's not
964 * already a 1 and maybe locked there before we even start start.
966 iersave = serial_in(up, UART_IER);
967 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
968 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
970 * OK it's in a known zero state, try writing and reading
971 * without disturbing the current state of the other bits.
973 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
974 if (serial_in(up, UART_IER) & UART_IER_UUE) {
977 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
979 DEBUG_AUTOCONF("Xscale ");
980 up->port.type = PORT_XSCALE;
981 up->capabilities |= UART_CAP_UUE;
986 * If we got here we couldn't force the IER_UUE bit to 0.
987 * Log it and continue.
989 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
991 serial_outp(up, UART_IER, iersave);
995 * This routine is called by rs_init() to initialize a specific serial
996 * port. It determines what type of UART chip this serial port is
997 * using: 8250, 16450, 16550, 16550A. The important question is
998 * whether or not this UART is a 16550A or not, since this will
999 * determine whether or not we can use its FIFO features or not.
1001 static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
1003 unsigned char status1, scratch, scratch2, scratch3;
1004 unsigned char save_lcr, save_mcr;
1005 unsigned long flags;
1007 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
1010 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ",
1011 serial_index(&up->port), up->port.iobase, up->port.membase);
1014 * We really do need global IRQs disabled here - we're going to
1015 * be frobbing the chips IRQ enable register to see if it exists.
1017 spin_lock_irqsave(&up->port.lock, flags);
1019 up->capabilities = 0;
1022 if (!(up->port.flags & UPF_BUGGY_UART)) {
1024 * Do a simple existence test first; if we fail this,
1025 * there's no point trying anything else.
1027 * 0x80 is used as a nonsense port to prevent against
1028 * false positives due to ISA bus float. The
1029 * assumption is that 0x80 is a non-existent port;
1030 * which should be safe since include/asm/io.h also
1031 * makes this assumption.
1033 * Note: this is safe as long as MCR bit 4 is clear
1034 * and the device is in "PC" mode.
1036 scratch = serial_inp(up, UART_IER);
1037 serial_outp(up, UART_IER, 0);
1042 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1043 * 16C754B) allow only to modify them if an EFR bit is set.
1045 scratch2 = serial_inp(up, UART_IER) & 0x0f;
1046 serial_outp(up, UART_IER, 0x0F);
1050 scratch3 = serial_inp(up, UART_IER) & 0x0f;
1051 serial_outp(up, UART_IER, scratch);
1052 if (scratch2 != 0 || scratch3 != 0x0F) {
1054 * We failed; there's nothing here
1056 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1057 scratch2, scratch3);
1062 save_mcr = serial_in(up, UART_MCR);
1063 save_lcr = serial_in(up, UART_LCR);
1066 * Check to see if a UART is really there. Certain broken
1067 * internal modems based on the Rockwell chipset fail this
1068 * test, because they apparently don't implement the loopback
1069 * test mode. So this test is skipped on the COM 1 through
1070 * COM 4 ports. This *should* be safe, since no board
1071 * manufacturer would be stupid enough to design a board
1072 * that conflicts with COM 1-4 --- we hope!
1074 if (!(up->port.flags & UPF_SKIP_TEST)) {
1075 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1076 status1 = serial_inp(up, UART_MSR) & 0xF0;
1077 serial_outp(up, UART_MCR, save_mcr);
1078 if (status1 != 0x90) {
1079 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1086 * We're pretty sure there's a port here. Lets find out what
1087 * type of port it is. The IIR top two bits allows us to find
1088 * out if it's 8250 or 16450, 16550, 16550A or later. This
1089 * determines what we test for next.
1091 * We also initialise the EFR (if any) to zero for later. The
1092 * EFR occupies the same register location as the FCR and IIR.
1094 serial_outp(up, UART_LCR, 0xBF);
1095 serial_outp(up, UART_EFR, 0);
1096 serial_outp(up, UART_LCR, 0);
1098 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1099 scratch = serial_in(up, UART_IIR) >> 6;
1101 DEBUG_AUTOCONF("iir=%d ", scratch);
1105 autoconfig_8250(up);
1108 up->port.type = PORT_UNKNOWN;
1111 up->port.type = PORT_16550;
1114 autoconfig_16550a(up);
1118 #ifdef CONFIG_SERIAL_8250_RSA
1120 * Only probe for RSA ports if we got the region.
1122 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1125 for (i = 0 ; i < probe_rsa_count; ++i) {
1126 if (probe_rsa[i] == up->port.iobase &&
1128 up->port.type = PORT_RSA;
1135 #ifdef CONFIG_SERIAL_8250_AU1X00
1136 /* if access method is AU, it is a 16550 with a quirk */
1137 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
1138 up->bugs |= UART_BUG_NOMSR;
1141 serial_outp(up, UART_LCR, save_lcr);
1143 if (up->capabilities != uart_config[up->port.type].flags) {
1145 "ttyS%d: detected caps %08x should be %08x\n",
1146 serial_index(&up->port), up->capabilities,
1147 uart_config[up->port.type].flags);
1150 up->port.fifosize = uart_config[up->port.type].fifo_size;
1151 up->capabilities = uart_config[up->port.type].flags;
1152 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1154 if (up->port.type == PORT_UNKNOWN)
1160 #ifdef CONFIG_SERIAL_8250_RSA
1161 if (up->port.type == PORT_RSA)
1162 serial_outp(up, UART_RSA_FRR, 0);
1164 serial_outp(up, UART_MCR, save_mcr);
1165 serial8250_clear_fifos(up);
1166 serial_in(up, UART_RX);
1167 if (up->capabilities & UART_CAP_UUE)
1168 serial_outp(up, UART_IER, UART_IER_UUE);
1170 serial_outp(up, UART_IER, 0);
1173 spin_unlock_irqrestore(&up->port.lock, flags);
1174 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1177 static void autoconfig_irq(struct uart_8250_port *up)
1179 unsigned char save_mcr, save_ier;
1180 unsigned char save_ICP = 0;
1181 unsigned int ICP = 0;
1185 if (up->port.flags & UPF_FOURPORT) {
1186 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1187 save_ICP = inb_p(ICP);
1192 /* forget possible initially masked and pending IRQ */
1193 probe_irq_off(probe_irq_on());
1194 save_mcr = serial_inp(up, UART_MCR);
1195 save_ier = serial_inp(up, UART_IER);
1196 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1198 irqs = probe_irq_on();
1199 serial_outp(up, UART_MCR, 0);
1201 if (up->port.flags & UPF_FOURPORT) {
1202 serial_outp(up, UART_MCR,
1203 UART_MCR_DTR | UART_MCR_RTS);
1205 serial_outp(up, UART_MCR,
1206 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1208 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1209 (void)serial_inp(up, UART_LSR);
1210 (void)serial_inp(up, UART_RX);
1211 (void)serial_inp(up, UART_IIR);
1212 (void)serial_inp(up, UART_MSR);
1213 serial_outp(up, UART_TX, 0xFF);
1215 irq = probe_irq_off(irqs);
1217 serial_outp(up, UART_MCR, save_mcr);
1218 serial_outp(up, UART_IER, save_ier);
1220 if (up->port.flags & UPF_FOURPORT)
1221 outb_p(save_ICP, ICP);
1223 up->port.irq = (irq > 0) ? irq : 0;
1226 static inline void __stop_tx(struct uart_8250_port *p)
1228 if (p->ier & UART_IER_THRI) {
1229 p->ier &= ~UART_IER_THRI;
1230 serial_out(p, UART_IER, p->ier);
1234 static void serial8250_stop_tx(struct uart_port *port)
1236 struct uart_8250_port *up = (struct uart_8250_port *)port;
1241 * We really want to stop the transmitter from sending.
1243 if (up->port.type == PORT_16C950) {
1244 up->acr |= UART_ACR_TXDIS;
1245 serial_icr_write(up, UART_ACR, up->acr);
1249 static void transmit_chars(struct uart_8250_port *up);
1251 static void serial8250_start_tx(struct uart_port *port)
1253 struct uart_8250_port *up = (struct uart_8250_port *)port;
1255 if (!(up->ier & UART_IER_THRI)) {
1256 up->ier |= UART_IER_THRI;
1257 serial_out(up, UART_IER, up->ier);
1259 if (up->bugs & UART_BUG_TXEN) {
1260 unsigned char lsr, iir;
1261 lsr = serial_in(up, UART_LSR);
1262 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1263 iir = serial_in(up, UART_IIR) & 0x0f;
1264 if ((up->port.type == PORT_RM9000) ?
1265 (lsr & UART_LSR_THRE &&
1266 (iir == UART_IIR_NO_INT || iir == UART_IIR_THRI)) :
1267 (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT))
1273 * Re-enable the transmitter if we disabled it.
1275 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1276 up->acr &= ~UART_ACR_TXDIS;
1277 serial_icr_write(up, UART_ACR, up->acr);
1281 static void serial8250_stop_rx(struct uart_port *port)
1283 struct uart_8250_port *up = (struct uart_8250_port *)port;
1285 up->ier &= ~UART_IER_RLSI;
1286 up->port.read_status_mask &= ~UART_LSR_DR;
1287 serial_out(up, UART_IER, up->ier);
1290 static void serial8250_enable_ms(struct uart_port *port)
1292 struct uart_8250_port *up = (struct uart_8250_port *)port;
1294 /* no MSR capabilities */
1295 if (up->bugs & UART_BUG_NOMSR)
1298 up->ier |= UART_IER_MSI;
1299 serial_out(up, UART_IER, up->ier);
1303 receive_chars(struct uart_8250_port *up, unsigned int *status)
1305 struct tty_struct *tty = up->port.info->port.tty;
1306 unsigned char ch, lsr = *status;
1307 int max_count = 256;
1311 if (likely(lsr & UART_LSR_DR))
1312 ch = serial_inp(up, UART_RX);
1315 * Intel 82571 has a Serial Over Lan device that will
1316 * set UART_LSR_BI without setting UART_LSR_DR when
1317 * it receives a break. To avoid reading from the
1318 * receive buffer without UART_LSR_DR bit set, we
1319 * just force the read character to be 0
1324 up->port.icount.rx++;
1326 lsr |= up->lsr_saved_flags;
1327 up->lsr_saved_flags = 0;
1329 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1331 * For statistics only
1333 if (lsr & UART_LSR_BI) {
1334 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1335 up->port.icount.brk++;
1337 * We do the SysRQ and SAK checking
1338 * here because otherwise the break
1339 * may get masked by ignore_status_mask
1340 * or read_status_mask.
1342 if (uart_handle_break(&up->port))
1344 } else if (lsr & UART_LSR_PE)
1345 up->port.icount.parity++;
1346 else if (lsr & UART_LSR_FE)
1347 up->port.icount.frame++;
1348 if (lsr & UART_LSR_OE)
1349 up->port.icount.overrun++;
1352 * Mask off conditions which should be ignored.
1354 lsr &= up->port.read_status_mask;
1356 if (lsr & UART_LSR_BI) {
1357 DEBUG_INTR("handling break....");
1359 } else if (lsr & UART_LSR_PE)
1361 else if (lsr & UART_LSR_FE)
1364 if (uart_handle_sysrq_char(&up->port, ch))
1367 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1370 lsr = serial_inp(up, UART_LSR);
1371 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
1372 spin_unlock(&up->port.lock);
1373 tty_flip_buffer_push(tty);
1374 spin_lock(&up->port.lock);
1378 static void transmit_chars(struct uart_8250_port *up)
1380 struct circ_buf *xmit = &up->port.info->xmit;
1383 if (up->port.x_char) {
1384 serial_outp(up, UART_TX, up->port.x_char);
1385 up->port.icount.tx++;
1386 up->port.x_char = 0;
1389 if (uart_tx_stopped(&up->port)) {
1390 serial8250_stop_tx(&up->port);
1393 if (uart_circ_empty(xmit)) {
1398 count = up->tx_loadsz;
1400 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1401 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1402 up->port.icount.tx++;
1403 if (uart_circ_empty(xmit))
1405 } while (--count > 0);
1407 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1408 uart_write_wakeup(&up->port);
1410 DEBUG_INTR("THRE...");
1412 if (uart_circ_empty(xmit))
1416 static unsigned int check_modem_status(struct uart_8250_port *up)
1418 unsigned int status = serial_in(up, UART_MSR);
1420 status |= up->msr_saved_flags;
1421 up->msr_saved_flags = 0;
1422 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1423 up->port.info != NULL) {
1424 if (status & UART_MSR_TERI)
1425 up->port.icount.rng++;
1426 if (status & UART_MSR_DDSR)
1427 up->port.icount.dsr++;
1428 if (status & UART_MSR_DDCD)
1429 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1430 if (status & UART_MSR_DCTS)
1431 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
1433 wake_up_interruptible(&up->port.info->delta_msr_wait);
1440 * This handles the interrupt from one port.
1442 static void serial8250_handle_port(struct uart_8250_port *up)
1444 unsigned int status;
1445 unsigned long flags;
1447 spin_lock_irqsave(&up->port.lock, flags);
1449 status = serial_inp(up, UART_LSR);
1451 DEBUG_INTR("status = %x...", status);
1453 if (status & (UART_LSR_DR | UART_LSR_BI))
1454 receive_chars(up, &status);
1455 check_modem_status(up);
1456 if (status & UART_LSR_THRE)
1459 spin_unlock_irqrestore(&up->port.lock, flags);
1463 * This is the serial driver's interrupt routine.
1465 * Arjan thinks the old way was overly complex, so it got simplified.
1466 * Alan disagrees, saying that need the complexity to handle the weird
1467 * nature of ISA shared interrupts. (This is a special exception.)
1469 * In order to handle ISA shared interrupts properly, we need to check
1470 * that all ports have been serviced, and therefore the ISA interrupt
1471 * line has been de-asserted.
1473 * This means we need to loop through all ports. checking that they
1474 * don't have an interrupt pending.
1476 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
1478 struct irq_info *i = dev_id;
1479 struct list_head *l, *end = NULL;
1480 int pass_counter = 0, handled = 0;
1482 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1484 spin_lock(&i->lock);
1488 struct uart_8250_port *up;
1491 up = list_entry(l, struct uart_8250_port, list);
1493 iir = serial_in(up, UART_IIR);
1494 if (!(iir & UART_IIR_NO_INT)) {
1495 serial8250_handle_port(up);
1500 } else if (up->port.iotype == UPIO_DWAPB &&
1501 (iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
1502 /* The DesignWare APB UART has an Busy Detect (0x07)
1503 * interrupt meaning an LCR write attempt occured while the
1504 * UART was busy. The interrupt must be cleared by reading
1505 * the UART status register (USR) and the LCR re-written. */
1506 unsigned int status;
1507 status = *(volatile u32 *)up->port.private_data;
1508 serial_out(up, UART_LCR, up->lcr);
1513 } else if (end == NULL)
1518 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1519 /* If we hit this, we're dead. */
1520 printk(KERN_ERR "serial8250: too much work for "
1526 spin_unlock(&i->lock);
1528 DEBUG_INTR("end.\n");
1530 #ifdef CONFIG_ARCH_OMAP15XX
1531 return IRQ_HANDLED; /* FIXME: iir status not ready on 1510 */
1533 return IRQ_RETVAL(handled);
1538 * To support ISA shared interrupts, we need to have one interrupt
1539 * handler that ensures that the IRQ line has been deasserted
1540 * before returning. Failing to do this will result in the IRQ
1541 * line being stuck active, and, since ISA irqs are edge triggered,
1542 * no more IRQs will be seen.
1544 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1546 spin_lock_irq(&i->lock);
1548 if (!list_empty(i->head)) {
1549 if (i->head == &up->list)
1550 i->head = i->head->next;
1551 list_del(&up->list);
1553 BUG_ON(i->head != &up->list);
1556 spin_unlock_irq(&i->lock);
1557 /* List empty so throw away the hash node */
1558 if (i->head == NULL) {
1559 hlist_del(&i->node);
1564 static int serial_link_irq_chain(struct uart_8250_port *up)
1566 struct hlist_head *h;
1567 struct hlist_node *n;
1569 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
1571 mutex_lock(&hash_mutex);
1573 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1575 hlist_for_each(n, h) {
1576 i = hlist_entry(n, struct irq_info, node);
1577 if (i->irq == up->port.irq)
1582 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1584 mutex_unlock(&hash_mutex);
1587 spin_lock_init(&i->lock);
1588 i->irq = up->port.irq;
1589 hlist_add_head(&i->node, h);
1591 mutex_unlock(&hash_mutex);
1593 spin_lock_irq(&i->lock);
1596 list_add(&up->list, i->head);
1597 spin_unlock_irq(&i->lock);
1601 INIT_LIST_HEAD(&up->list);
1602 i->head = &up->list;
1603 spin_unlock_irq(&i->lock);
1605 ret = request_irq(up->port.irq, serial8250_interrupt,
1606 irq_flags, "serial", i);
1608 serial_do_unlink(i, up);
1614 static void serial_unlink_irq_chain(struct uart_8250_port *up)
1617 struct hlist_node *n;
1618 struct hlist_head *h;
1620 mutex_lock(&hash_mutex);
1622 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1624 hlist_for_each(n, h) {
1625 i = hlist_entry(n, struct irq_info, node);
1626 if (i->irq == up->port.irq)
1631 BUG_ON(i->head == NULL);
1633 if (list_empty(i->head))
1634 free_irq(up->port.irq, i);
1636 serial_do_unlink(i, up);
1637 mutex_unlock(&hash_mutex);
1640 /* Base timer interval for polling */
1641 static inline int poll_timeout(int timeout)
1643 return timeout > 6 ? (timeout / 2 - 2) : 1;
1647 * This function is used to handle ports that do not have an
1648 * interrupt. This doesn't work very well for 16450's, but gives
1649 * barely passable results for a 16550A. (Although at the expense
1650 * of much CPU overhead).
1652 static void serial8250_timeout(unsigned long data)
1654 struct uart_8250_port *up = (struct uart_8250_port *)data;
1657 iir = serial_in(up, UART_IIR);
1658 if (!(iir & UART_IIR_NO_INT))
1659 serial8250_handle_port(up);
1660 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
1663 static void serial8250_backup_timeout(unsigned long data)
1665 struct uart_8250_port *up = (struct uart_8250_port *)data;
1666 unsigned int iir, ier = 0, lsr;
1667 unsigned long flags;
1670 * Must disable interrupts or else we risk racing with the interrupt
1673 if (is_real_interrupt(up->port.irq)) {
1674 ier = serial_in(up, UART_IER);
1675 serial_out(up, UART_IER, 0);
1678 iir = serial_in(up, UART_IIR);
1681 * This should be a safe test for anyone who doesn't trust the
1682 * IIR bits on their UART, but it's specifically designed for
1683 * the "Diva" UART used on the management processor on many HP
1684 * ia64 and parisc boxes.
1686 spin_lock_irqsave(&up->port.lock, flags);
1687 lsr = serial_in(up, UART_LSR);
1688 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1689 spin_unlock_irqrestore(&up->port.lock, flags);
1690 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
1691 (!uart_circ_empty(&up->port.info->xmit) || up->port.x_char) &&
1692 (lsr & UART_LSR_THRE)) {
1693 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1694 iir |= UART_IIR_THRI;
1697 if (!(iir & UART_IIR_NO_INT))
1698 serial8250_handle_port(up);
1700 if (is_real_interrupt(up->port.irq))
1701 serial_out(up, UART_IER, ier);
1703 /* Standard timer interval plus 0.2s to keep the port running */
1704 mod_timer(&up->timer,
1705 jiffies + poll_timeout(up->port.timeout) + HZ / 5);
1708 static unsigned int serial8250_tx_empty(struct uart_port *port)
1710 struct uart_8250_port *up = (struct uart_8250_port *)port;
1711 unsigned long flags;
1714 spin_lock_irqsave(&up->port.lock, flags);
1715 lsr = serial_in(up, UART_LSR);
1716 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1717 spin_unlock_irqrestore(&up->port.lock, flags);
1719 return lsr & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
1722 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1724 struct uart_8250_port *up = (struct uart_8250_port *)port;
1725 unsigned int status;
1728 status = check_modem_status(up);
1731 if (status & UART_MSR_DCD)
1733 if (status & UART_MSR_RI)
1735 if (status & UART_MSR_DSR)
1737 if (status & UART_MSR_CTS)
1742 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1744 struct uart_8250_port *up = (struct uart_8250_port *)port;
1745 unsigned char mcr = 0;
1747 if (mctrl & TIOCM_RTS)
1748 mcr |= UART_MCR_RTS;
1749 if (mctrl & TIOCM_DTR)
1750 mcr |= UART_MCR_DTR;
1751 if (mctrl & TIOCM_OUT1)
1752 mcr |= UART_MCR_OUT1;
1753 if (mctrl & TIOCM_OUT2)
1754 mcr |= UART_MCR_OUT2;
1755 if (mctrl & TIOCM_LOOP)
1756 mcr |= UART_MCR_LOOP;
1758 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1760 serial_out(up, UART_MCR, mcr);
1763 static void serial8250_break_ctl(struct uart_port *port, int break_state)
1765 struct uart_8250_port *up = (struct uart_8250_port *)port;
1766 unsigned long flags;
1768 spin_lock_irqsave(&up->port.lock, flags);
1769 if (break_state == -1)
1770 up->lcr |= UART_LCR_SBC;
1772 up->lcr &= ~UART_LCR_SBC;
1773 serial_out(up, UART_LCR, up->lcr);
1774 spin_unlock_irqrestore(&up->port.lock, flags);
1777 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1780 * Wait for transmitter & holding register to empty
1782 static void wait_for_xmitr(struct uart_8250_port *up, int bits)
1784 unsigned int status, tmout = 10000;
1786 /* Wait up to 10ms for the character(s) to be sent. */
1788 status = serial_in(up, UART_LSR);
1790 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
1795 } while ((status & bits) != bits);
1797 /* Wait up to 1s for flow control if necessary */
1798 if (up->port.flags & UPF_CONS_FLOW) {
1800 for (tmout = 1000000; tmout; tmout--) {
1801 unsigned int msr = serial_in(up, UART_MSR);
1802 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1803 if (msr & UART_MSR_CTS)
1806 touch_nmi_watchdog();
1811 #ifdef CONFIG_CONSOLE_POLL
1813 * Console polling routines for writing and reading from the uart while
1814 * in an interrupt or debug context.
1817 static int serial8250_get_poll_char(struct uart_port *port)
1819 struct uart_8250_port *up = (struct uart_8250_port *)port;
1820 unsigned char lsr = serial_inp(up, UART_LSR);
1822 while (!(lsr & UART_LSR_DR))
1823 lsr = serial_inp(up, UART_LSR);
1825 return serial_inp(up, UART_RX);
1829 static void serial8250_put_poll_char(struct uart_port *port,
1833 struct uart_8250_port *up = (struct uart_8250_port *)port;
1836 * First save the IER then disable the interrupts
1838 ier = serial_in(up, UART_IER);
1839 if (up->capabilities & UART_CAP_UUE)
1840 serial_out(up, UART_IER, UART_IER_UUE);
1842 serial_out(up, UART_IER, 0);
1844 wait_for_xmitr(up, BOTH_EMPTY);
1846 * Send the character out.
1847 * If a LF, also do CR...
1849 serial_out(up, UART_TX, c);
1851 wait_for_xmitr(up, BOTH_EMPTY);
1852 serial_out(up, UART_TX, 13);
1856 * Finally, wait for transmitter to become empty
1857 * and restore the IER
1859 wait_for_xmitr(up, BOTH_EMPTY);
1860 serial_out(up, UART_IER, ier);
1863 #endif /* CONFIG_CONSOLE_POLL */
1865 static int serial8250_startup(struct uart_port *port)
1867 struct uart_8250_port *up = (struct uart_8250_port *)port;
1868 unsigned long flags;
1869 unsigned char lsr, iir;
1872 up->capabilities = uart_config[up->port.type].flags;
1875 if (up->port.type == PORT_16C950) {
1876 /* Wake up and initialize UART */
1878 serial_outp(up, UART_LCR, 0xBF);
1879 serial_outp(up, UART_EFR, UART_EFR_ECB);
1880 serial_outp(up, UART_IER, 0);
1881 serial_outp(up, UART_LCR, 0);
1882 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1883 serial_outp(up, UART_LCR, 0xBF);
1884 serial_outp(up, UART_EFR, UART_EFR_ECB);
1885 serial_outp(up, UART_LCR, 0);
1888 #ifdef CONFIG_SERIAL_8250_RSA
1890 * If this is an RSA port, see if we can kick it up to the
1891 * higher speed clock.
1897 * Clear the FIFO buffers and disable them.
1898 * (they will be reenabled in set_termios())
1900 serial8250_clear_fifos(up);
1903 * Clear the interrupt registers.
1905 (void) serial_inp(up, UART_LSR);
1906 (void) serial_inp(up, UART_RX);
1907 (void) serial_inp(up, UART_IIR);
1908 (void) serial_inp(up, UART_MSR);
1911 * At this point, there's no way the LSR could still be 0xff;
1912 * if it is, then bail out, because there's likely no UART
1915 if (!(up->port.flags & UPF_BUGGY_UART) &&
1916 (serial_inp(up, UART_LSR) == 0xff)) {
1917 printk(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
1918 serial_index(&up->port));
1923 * For a XR16C850, we need to set the trigger levels
1925 if (up->port.type == PORT_16850) {
1928 serial_outp(up, UART_LCR, 0xbf);
1930 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
1931 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
1932 serial_outp(up, UART_TRG, UART_TRG_96);
1933 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
1934 serial_outp(up, UART_TRG, UART_TRG_96);
1936 serial_outp(up, UART_LCR, 0);
1939 if (is_real_interrupt(up->port.irq)) {
1942 * Test for UARTs that do not reassert THRE when the
1943 * transmitter is idle and the interrupt has already
1944 * been cleared. Real 16550s should always reassert
1945 * this interrupt whenever the transmitter is idle and
1946 * the interrupt is enabled. Delays are necessary to
1947 * allow register changes to become visible.
1949 spin_lock_irqsave(&up->port.lock, flags);
1950 if (up->port.flags & UPF_SHARE_IRQ)
1951 disable_irq_nosync(up->port.irq);
1953 wait_for_xmitr(up, UART_LSR_THRE);
1954 serial_out_sync(up, UART_IER, UART_IER_THRI);
1955 udelay(1); /* allow THRE to set */
1956 iir1 = serial_in(up, UART_IIR);
1957 serial_out(up, UART_IER, 0);
1958 serial_out_sync(up, UART_IER, UART_IER_THRI);
1959 udelay(1); /* allow a working UART time to re-assert THRE */
1960 iir = serial_in(up, UART_IIR);
1961 serial_out(up, UART_IER, 0);
1963 if (up->port.flags & UPF_SHARE_IRQ)
1964 enable_irq(up->port.irq);
1965 spin_unlock_irqrestore(&up->port.lock, flags);
1968 * If the interrupt is not reasserted, setup a timer to
1969 * kick the UART on a regular basis.
1971 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
1972 up->bugs |= UART_BUG_THRE;
1973 pr_debug("ttyS%d - using backup timer\n",
1974 serial_index(port));
1979 * The above check will only give an accurate result the first time
1980 * the port is opened so this value needs to be preserved.
1982 if (up->bugs & UART_BUG_THRE) {
1983 up->timer.function = serial8250_backup_timeout;
1984 up->timer.data = (unsigned long)up;
1985 mod_timer(&up->timer, jiffies +
1986 poll_timeout(up->port.timeout) + HZ / 5);
1990 * If the "interrupt" for this port doesn't correspond with any
1991 * hardware interrupt, we use a timer-based system. The original
1992 * driver used to do this with IRQ0.
1994 if (!is_real_interrupt(up->port.irq)) {
1995 up->timer.data = (unsigned long)up;
1996 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
1998 retval = serial_link_irq_chain(up);
2004 * Now, initialize the UART
2006 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
2008 spin_lock_irqsave(&up->port.lock, flags);
2009 if (up->port.flags & UPF_FOURPORT) {
2010 if (!is_real_interrupt(up->port.irq))
2011 up->port.mctrl |= TIOCM_OUT1;
2014 * Most PC uarts need OUT2 raised to enable interrupts.
2016 if (is_real_interrupt(up->port.irq))
2017 up->port.mctrl |= TIOCM_OUT2;
2019 serial8250_set_mctrl(&up->port, up->port.mctrl);
2022 * Do a quick test to see if we receive an
2023 * interrupt when we enable the TX irq.
2025 serial_outp(up, UART_IER, UART_IER_THRI);
2026 lsr = serial_in(up, UART_LSR);
2027 iir = serial_in(up, UART_IIR);
2028 serial_outp(up, UART_IER, 0);
2030 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2031 if (!(up->bugs & UART_BUG_TXEN)) {
2032 up->bugs |= UART_BUG_TXEN;
2033 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2034 serial_index(port));
2037 up->bugs &= ~UART_BUG_TXEN;
2040 spin_unlock_irqrestore(&up->port.lock, flags);
2043 * Clear the interrupt registers again for luck, and clear the
2044 * saved flags to avoid getting false values from polling
2045 * routines or the previous session.
2047 serial_inp(up, UART_LSR);
2048 serial_inp(up, UART_RX);
2049 serial_inp(up, UART_IIR);
2050 serial_inp(up, UART_MSR);
2051 up->lsr_saved_flags = 0;
2052 up->msr_saved_flags = 0;
2055 * Finally, enable interrupts. Note: Modem status interrupts
2056 * are set via set_termios(), which will be occurring imminently
2057 * anyway, so we don't enable them here.
2059 up->ier = UART_IER_RLSI | UART_IER_RDI;
2060 serial_outp(up, UART_IER, up->ier);
2062 if (up->port.flags & UPF_FOURPORT) {
2065 * Enable interrupts on the AST Fourport board
2067 icp = (up->port.iobase & 0xfe0) | 0x01f;
2075 static void serial8250_shutdown(struct uart_port *port)
2077 struct uart_8250_port *up = (struct uart_8250_port *)port;
2078 unsigned long flags;
2081 * Disable interrupts from this port
2084 serial_outp(up, UART_IER, 0);
2086 spin_lock_irqsave(&up->port.lock, flags);
2087 if (up->port.flags & UPF_FOURPORT) {
2088 /* reset interrupts on the AST Fourport board */
2089 inb((up->port.iobase & 0xfe0) | 0x1f);
2090 up->port.mctrl |= TIOCM_OUT1;
2092 up->port.mctrl &= ~TIOCM_OUT2;
2094 serial8250_set_mctrl(&up->port, up->port.mctrl);
2095 spin_unlock_irqrestore(&up->port.lock, flags);
2098 * Disable break condition and FIFOs
2100 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
2101 serial8250_clear_fifos(up);
2103 #ifdef CONFIG_SERIAL_8250_RSA
2105 * Reset the RSA board back to 115kbps compat mode.
2111 * Read data port to reset things, and then unlink from
2114 (void) serial_in(up, UART_RX);
2116 del_timer_sync(&up->timer);
2117 up->timer.function = serial8250_timeout;
2118 if (is_real_interrupt(up->port.irq))
2119 serial_unlink_irq_chain(up);
2122 static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2127 * Handle magic divisors for baud rates above baud_base on
2128 * SMSC SuperIO chips.
2130 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2131 baud == (port->uartclk/4))
2133 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2134 baud == (port->uartclk/8))
2137 quot = uart_get_divisor(port, baud);
2143 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2144 struct ktermios *old)
2146 struct uart_8250_port *up = (struct uart_8250_port *)port;
2147 unsigned char cval, fcr = 0;
2148 unsigned long flags;
2149 unsigned int baud, quot;
2151 switch (termios->c_cflag & CSIZE) {
2153 cval = UART_LCR_WLEN5;
2156 cval = UART_LCR_WLEN6;
2159 cval = UART_LCR_WLEN7;
2163 cval = UART_LCR_WLEN8;
2167 if (termios->c_cflag & CSTOPB)
2168 cval |= UART_LCR_STOP;
2169 if (termios->c_cflag & PARENB)
2170 cval |= UART_LCR_PARITY;
2171 if (!(termios->c_cflag & PARODD))
2172 cval |= UART_LCR_EPAR;
2174 if (termios->c_cflag & CMSPAR)
2175 cval |= UART_LCR_SPAR;
2179 * Ask the core to calculate the divisor for us.
2181 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
2182 quot = serial8250_get_divisor(port, baud);
2185 * Oxford Semi 952 rev B workaround
2187 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2190 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
2192 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2194 fcr = uart_config[up->port.type].fcr;
2198 * MCR-based auto flow control. When AFE is enabled, RTS will be
2199 * deasserted when the receive FIFO contains more characters than
2200 * the trigger, or the MCR RTS bit is cleared. In the case where
2201 * the remote UART is not using CTS auto flow control, we must
2202 * have sufficient FIFO entries for the latency of the remote
2203 * UART to respond. IOW, at least 32 bytes of FIFO.
2205 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
2206 up->mcr &= ~UART_MCR_AFE;
2207 if (termios->c_cflag & CRTSCTS)
2208 up->mcr |= UART_MCR_AFE;
2212 * Ok, we're now changing the port state. Do it with
2213 * interrupts disabled.
2215 spin_lock_irqsave(&up->port.lock, flags);
2218 * Update the per-port timeout.
2220 uart_update_timeout(port, termios->c_cflag, baud);
2222 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2223 if (termios->c_iflag & INPCK)
2224 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2225 if (termios->c_iflag & (BRKINT | PARMRK))
2226 up->port.read_status_mask |= UART_LSR_BI;
2229 * Characteres to ignore
2231 up->port.ignore_status_mask = 0;
2232 if (termios->c_iflag & IGNPAR)
2233 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2234 if (termios->c_iflag & IGNBRK) {
2235 up->port.ignore_status_mask |= UART_LSR_BI;
2237 * If we're ignoring parity and break indicators,
2238 * ignore overruns too (for real raw support).
2240 if (termios->c_iflag & IGNPAR)
2241 up->port.ignore_status_mask |= UART_LSR_OE;
2245 * ignore all characters if CREAD is not set
2247 if ((termios->c_cflag & CREAD) == 0)
2248 up->port.ignore_status_mask |= UART_LSR_DR;
2251 * CTS flow control flag and modem status interrupts
2253 up->ier &= ~UART_IER_MSI;
2254 if (!(up->bugs & UART_BUG_NOMSR) &&
2255 UART_ENABLE_MS(&up->port, termios->c_cflag))
2256 up->ier |= UART_IER_MSI;
2257 if (up->capabilities & UART_CAP_UUE)
2258 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
2260 serial_out(up, UART_IER, up->ier);
2262 if (up->capabilities & UART_CAP_EFR) {
2263 unsigned char efr = 0;
2265 * TI16C752/Startech hardware flow control. FIXME:
2266 * - TI16C752 requires control thresholds to be set.
2267 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2269 if (termios->c_cflag & CRTSCTS)
2270 efr |= UART_EFR_CTS;
2272 serial_outp(up, UART_LCR, 0xBF);
2273 serial_outp(up, UART_EFR, efr);
2276 #ifdef CONFIG_ARCH_OMAP
2277 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2278 if (cpu_is_omap1510() && is_omap_port(up)) {
2279 if (baud == 115200) {
2281 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2283 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2287 if (up->capabilities & UART_NATSEMI) {
2288 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2289 serial_outp(up, UART_LCR, 0xe0);
2291 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2294 serial_dl_write(up, quot);
2297 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2298 * is written without DLAB set, this mode will be disabled.
2300 if (up->port.type == PORT_16750)
2301 serial_outp(up, UART_FCR, fcr);
2303 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2304 up->lcr = cval; /* Save LCR */
2305 if (up->port.type != PORT_16750) {
2306 if (fcr & UART_FCR_ENABLE_FIFO) {
2307 /* emulated UARTs (Lucent Venus 167x) need two steps */
2308 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2311 /* Note that we need to set ECB to access write water mark
2312 * bits. First allow FCR tx fifo write, then set fcr with
2313 * possible TX fifo settings. */
2314 if (uart_config[up->port.type].flags & UART_CAP_EFR) {
2315 serial_outp(up, UART_LCR, 0xbf); /* Access EFR */
2316 serial_outp(up, UART_EFR, UART_EFR_ECB);
2317 serial_outp(up, UART_LCR, 0x0); /* Access FCR */
2318 serial_outp(up, UART_FCR, fcr);
2319 serial_outp(up, UART_LCR, 0xbf); /* Access EFR */
2320 serial_outp(up, UART_EFR, 0);
2321 serial_outp(up, UART_LCR, cval); /* Access FCR */
2323 serial_outp(up, UART_FCR, fcr); /* set fcr */
2325 serial8250_set_mctrl(&up->port, up->port.mctrl);
2326 spin_unlock_irqrestore(&up->port.lock, flags);
2327 /* Don't rewrite B0 */
2328 if (tty_termios_baud_rate(termios))
2329 tty_termios_encode_baud_rate(termios, baud, baud);
2333 serial8250_pm(struct uart_port *port, unsigned int state,
2334 unsigned int oldstate)
2336 struct uart_8250_port *p = (struct uart_8250_port *)port;
2338 serial8250_set_sleep(p, state != 0);
2341 p->pm(port, state, oldstate);
2344 static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2346 if (pt->port.iotype == UPIO_AU)
2348 #ifdef CONFIG_ARCH_OMAP
2349 if (is_omap_port(pt))
2350 return 0x16 << pt->port.regshift;
2352 return 8 << pt->port.regshift;
2356 * Resource handling.
2358 static int serial8250_request_std_resource(struct uart_8250_port *up)
2360 unsigned int size = serial8250_port_size(up);
2363 switch (up->port.iotype) {
2369 if (!up->port.mapbase)
2372 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2377 if (up->port.flags & UPF_IOREMAP) {
2378 up->port.membase = ioremap_nocache(up->port.mapbase,
2380 if (!up->port.membase) {
2381 release_mem_region(up->port.mapbase, size);
2389 if (!request_region(up->port.iobase, size, "serial"))
2396 static void serial8250_release_std_resource(struct uart_8250_port *up)
2398 unsigned int size = serial8250_port_size(up);
2400 switch (up->port.iotype) {
2406 if (!up->port.mapbase)
2409 if (up->port.flags & UPF_IOREMAP) {
2410 iounmap(up->port.membase);
2411 up->port.membase = NULL;
2414 release_mem_region(up->port.mapbase, size);
2419 release_region(up->port.iobase, size);
2424 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2426 unsigned long start = UART_RSA_BASE << up->port.regshift;
2427 unsigned int size = 8 << up->port.regshift;
2430 switch (up->port.iotype) {
2433 start += up->port.iobase;
2434 if (request_region(start, size, "serial-rsa"))
2444 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2446 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2447 unsigned int size = 8 << up->port.regshift;
2449 switch (up->port.iotype) {
2452 release_region(up->port.iobase + offset, size);
2457 static void serial8250_release_port(struct uart_port *port)
2459 struct uart_8250_port *up = (struct uart_8250_port *)port;
2461 serial8250_release_std_resource(up);
2462 if (up->port.type == PORT_RSA)
2463 serial8250_release_rsa_resource(up);
2466 static int serial8250_request_port(struct uart_port *port)
2468 struct uart_8250_port *up = (struct uart_8250_port *)port;
2471 ret = serial8250_request_std_resource(up);
2472 if (ret == 0 && up->port.type == PORT_RSA) {
2473 ret = serial8250_request_rsa_resource(up);
2475 serial8250_release_std_resource(up);
2481 static void serial8250_config_port(struct uart_port *port, int flags)
2483 struct uart_8250_port *up = (struct uart_8250_port *)port;
2484 int probeflags = PROBE_ANY;
2488 * Find the region that we can probe for. This in turn
2489 * tells us whether we can probe for the type of port.
2491 ret = serial8250_request_std_resource(up);
2495 ret = serial8250_request_rsa_resource(up);
2497 probeflags &= ~PROBE_RSA;
2499 if (flags & UART_CONFIG_TYPE)
2500 autoconfig(up, probeflags);
2501 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2504 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2505 serial8250_release_rsa_resource(up);
2506 if (up->port.type == PORT_UNKNOWN)
2507 serial8250_release_std_resource(up);
2511 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2513 if (ser->irq >= nr_irqs || ser->irq < 0 ||
2514 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2515 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2516 ser->type == PORT_STARTECH)
2522 serial8250_type(struct uart_port *port)
2524 int type = port->type;
2526 if (type >= ARRAY_SIZE(uart_config))
2528 return uart_config[type].name;
2531 static struct uart_ops serial8250_pops = {
2532 .tx_empty = serial8250_tx_empty,
2533 .set_mctrl = serial8250_set_mctrl,
2534 .get_mctrl = serial8250_get_mctrl,
2535 .stop_tx = serial8250_stop_tx,
2536 .start_tx = serial8250_start_tx,
2537 .stop_rx = serial8250_stop_rx,
2538 .enable_ms = serial8250_enable_ms,
2539 .break_ctl = serial8250_break_ctl,
2540 .startup = serial8250_startup,
2541 .shutdown = serial8250_shutdown,
2542 .set_termios = serial8250_set_termios,
2543 .pm = serial8250_pm,
2544 .type = serial8250_type,
2545 .release_port = serial8250_release_port,
2546 .request_port = serial8250_request_port,
2547 .config_port = serial8250_config_port,
2548 .verify_port = serial8250_verify_port,
2549 #ifdef CONFIG_CONSOLE_POLL
2550 .poll_get_char = serial8250_get_poll_char,
2551 .poll_put_char = serial8250_put_poll_char,
2555 static struct uart_8250_port serial8250_ports[UART_NR];
2557 static void __init serial8250_isa_init_ports(void)
2559 struct uart_8250_port *up;
2560 static int first = 1;
2567 for (i = 0; i < nr_uarts; i++) {
2568 struct uart_8250_port *up = &serial8250_ports[i];
2571 spin_lock_init(&up->port.lock);
2573 init_timer(&up->timer);
2574 up->timer.function = serial8250_timeout;
2577 * ALPHA_KLUDGE_MCR needs to be killed.
2579 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2580 up->mcr_force = ALPHA_KLUDGE_MCR;
2582 up->port.ops = &serial8250_pops;
2585 for (i = 0, up = serial8250_ports;
2586 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
2588 up->port.iobase = old_serial_port[i].port;
2589 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
2590 up->port.uartclk = old_serial_port[i].baud_base * 16;
2591 up->port.flags = old_serial_port[i].flags;
2592 up->port.hub6 = old_serial_port[i].hub6;
2593 up->port.membase = old_serial_port[i].iomem_base;
2594 up->port.iotype = old_serial_port[i].io_type;
2595 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2597 up->port.flags |= UPF_SHARE_IRQ;
2602 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2606 serial8250_isa_init_ports();
2608 for (i = 0; i < nr_uarts; i++) {
2609 struct uart_8250_port *up = &serial8250_ports[i];
2612 uart_add_one_port(drv, &up->port);
2616 #ifdef CONFIG_SERIAL_8250_CONSOLE
2618 static void serial8250_console_putchar(struct uart_port *port, int ch)
2620 struct uart_8250_port *up = (struct uart_8250_port *)port;
2622 wait_for_xmitr(up, UART_LSR_THRE);
2623 serial_out(up, UART_TX, ch);
2627 * Print a string to the serial port trying not to disturb
2628 * any possible real use of the port...
2630 * The console_lock must be held when we get here.
2633 serial8250_console_write(struct console *co, const char *s, unsigned int count)
2635 struct uart_8250_port *up = &serial8250_ports[co->index];
2636 unsigned long flags;
2640 touch_nmi_watchdog();
2642 local_irq_save(flags);
2643 if (up->port.sysrq) {
2644 /* serial8250_handle_port() already took the lock */
2646 } else if (oops_in_progress) {
2647 locked = spin_trylock(&up->port.lock);
2649 spin_lock(&up->port.lock);
2652 * First save the IER then disable the interrupts
2654 ier = serial_in(up, UART_IER);
2656 if (up->capabilities & UART_CAP_UUE)
2657 serial_out(up, UART_IER, UART_IER_UUE);
2659 serial_out(up, UART_IER, 0);
2661 uart_console_write(&up->port, s, count, serial8250_console_putchar);
2664 * Finally, wait for transmitter to become empty
2665 * and restore the IER
2667 wait_for_xmitr(up, BOTH_EMPTY);
2668 serial_out(up, UART_IER, ier);
2671 * The receive handling will happen properly because the
2672 * receive ready bit will still be set; it is not cleared
2673 * on read. However, modem control will not, we must
2674 * call it if we have saved something in the saved flags
2675 * while processing with interrupts off.
2677 if (up->msr_saved_flags)
2678 check_modem_status(up);
2681 spin_unlock(&up->port.lock);
2682 local_irq_restore(flags);
2685 static int __init serial8250_console_setup(struct console *co, char *options)
2687 struct uart_port *port;
2694 * Check whether an invalid uart number has been specified, and
2695 * if so, search for the first available port that does have
2698 if (co->index >= nr_uarts)
2700 port = &serial8250_ports[co->index].port;
2701 if (!port->iobase && !port->membase)
2705 uart_parse_options(options, &baud, &parity, &bits, &flow);
2707 return uart_set_options(port, co, baud, parity, bits, flow);
2710 static int serial8250_console_early_setup(void)
2712 return serial8250_find_port_for_earlycon();
2715 static struct console serial8250_console = {
2717 .write = serial8250_console_write,
2718 .device = uart_console_device,
2719 .setup = serial8250_console_setup,
2720 .early_setup = serial8250_console_early_setup,
2721 .flags = CON_PRINTBUFFER,
2723 .data = &serial8250_reg,
2726 static int __init serial8250_console_init(void)
2728 if (nr_uarts > UART_NR)
2731 serial8250_isa_init_ports();
2732 register_console(&serial8250_console);
2735 console_initcall(serial8250_console_init);
2737 int serial8250_find_port(struct uart_port *p)
2740 struct uart_port *port;
2742 for (line = 0; line < nr_uarts; line++) {
2743 port = &serial8250_ports[line].port;
2744 if (uart_match_port(p, port))
2750 #define SERIAL8250_CONSOLE &serial8250_console
2752 #define SERIAL8250_CONSOLE NULL
2755 static struct uart_driver serial8250_reg = {
2756 .owner = THIS_MODULE,
2757 .driver_name = "serial",
2761 .cons = SERIAL8250_CONSOLE,
2765 * early_serial_setup - early registration for 8250 ports
2767 * Setup an 8250 port structure prior to console initialisation. Use
2768 * after console initialisation will cause undefined behaviour.
2770 int __init early_serial_setup(struct uart_port *port)
2772 if (port->line >= ARRAY_SIZE(serial8250_ports))
2775 serial8250_isa_init_ports();
2776 serial8250_ports[port->line].port = *port;
2777 serial8250_ports[port->line].port.ops = &serial8250_pops;
2782 * serial8250_suspend_port - suspend one serial port
2783 * @line: serial line number
2785 * Suspend one serial port.
2787 void serial8250_suspend_port(int line)
2789 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2793 * serial8250_resume_port - resume one serial port
2794 * @line: serial line number
2796 * Resume one serial port.
2798 void serial8250_resume_port(int line)
2800 struct uart_8250_port *up = &serial8250_ports[line];
2802 if (up->capabilities & UART_NATSEMI) {
2805 /* Ensure it's still in high speed mode */
2806 serial_outp(up, UART_LCR, 0xE0);
2808 tmp = serial_in(up, 0x04); /* EXCR2 */
2809 tmp &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
2810 tmp |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
2811 serial_outp(up, 0x04, tmp);
2813 serial_outp(up, UART_LCR, 0);
2815 uart_resume_port(&serial8250_reg, &up->port);
2819 * Register a set of serial devices attached to a platform device. The
2820 * list is terminated with a zero flags entry, which means we expect
2821 * all entries to have at least UPF_BOOT_AUTOCONF set.
2823 static int __devinit serial8250_probe(struct platform_device *dev)
2825 struct plat_serial8250_port *p = dev->dev.platform_data;
2826 struct uart_port port;
2829 memset(&port, 0, sizeof(struct uart_port));
2831 for (i = 0; p && p->flags != 0; p++, i++) {
2832 port.iobase = p->iobase;
2833 port.membase = p->membase;
2835 port.uartclk = p->uartclk;
2836 port.regshift = p->regshift;
2837 port.iotype = p->iotype;
2838 port.flags = p->flags;
2839 port.mapbase = p->mapbase;
2840 port.hub6 = p->hub6;
2841 port.private_data = p->private_data;
2842 port.dev = &dev->dev;
2844 port.flags |= UPF_SHARE_IRQ;
2845 ret = serial8250_register_port(&port);
2847 dev_err(&dev->dev, "unable to register port at index %d "
2848 "(IO%lx MEM%llx IRQ%d): %d\n", i,
2849 p->iobase, (unsigned long long)p->mapbase,
2857 * Remove serial ports registered against a platform device.
2859 static int __devexit serial8250_remove(struct platform_device *dev)
2863 for (i = 0; i < nr_uarts; i++) {
2864 struct uart_8250_port *up = &serial8250_ports[i];
2866 if (up->port.dev == &dev->dev)
2867 serial8250_unregister_port(i);
2872 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
2876 for (i = 0; i < UART_NR; i++) {
2877 struct uart_8250_port *up = &serial8250_ports[i];
2879 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
2880 uart_suspend_port(&serial8250_reg, &up->port);
2886 static int serial8250_resume(struct platform_device *dev)
2890 for (i = 0; i < UART_NR; i++) {
2891 struct uart_8250_port *up = &serial8250_ports[i];
2893 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
2894 serial8250_resume_port(i);
2900 static struct platform_driver serial8250_isa_driver = {
2901 .probe = serial8250_probe,
2902 .remove = __devexit_p(serial8250_remove),
2903 .suspend = serial8250_suspend,
2904 .resume = serial8250_resume,
2906 .name = "serial8250",
2907 .owner = THIS_MODULE,
2912 * This "device" covers _all_ ISA 8250-compatible serial devices listed
2913 * in the table in include/asm/serial.h
2915 static struct platform_device *serial8250_isa_devs;
2918 * serial8250_register_port and serial8250_unregister_port allows for
2919 * 16x50 serial ports to be configured at run-time, to support PCMCIA
2920 * modems and PCI multiport cards.
2922 static DEFINE_MUTEX(serial_mutex);
2924 static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
2929 * First, find a port entry which matches.
2931 for (i = 0; i < nr_uarts; i++)
2932 if (uart_match_port(&serial8250_ports[i].port, port))
2933 return &serial8250_ports[i];
2936 * We didn't find a matching entry, so look for the first
2937 * free entry. We look for one which hasn't been previously
2938 * used (indicated by zero iobase).
2940 for (i = 0; i < nr_uarts; i++)
2941 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
2942 serial8250_ports[i].port.iobase == 0)
2943 return &serial8250_ports[i];
2946 * That also failed. Last resort is to find any entry which
2947 * doesn't have a real port associated with it.
2949 for (i = 0; i < nr_uarts; i++)
2950 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
2951 return &serial8250_ports[i];
2957 * serial8250_register_port - register a serial port
2958 * @port: serial port template
2960 * Configure the serial port specified by the request. If the
2961 * port exists and is in use, it is hung up and unregistered
2964 * The port is then probed and if necessary the IRQ is autodetected
2965 * If this fails an error is returned.
2967 * On success the port is ready to use and the line number is returned.
2969 int serial8250_register_port(struct uart_port *port)
2971 struct uart_8250_port *uart;
2974 if (port->uartclk == 0)
2977 mutex_lock(&serial_mutex);
2979 uart = serial8250_find_match_or_unused(port);
2981 uart_remove_one_port(&serial8250_reg, &uart->port);
2983 uart->port.iobase = port->iobase;
2984 uart->port.membase = port->membase;
2985 uart->port.irq = port->irq;
2986 uart->port.uartclk = port->uartclk;
2987 uart->port.fifosize = port->fifosize;
2988 uart->port.regshift = port->regshift;
2989 uart->port.iotype = port->iotype;
2990 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
2991 uart->port.mapbase = port->mapbase;
2992 uart->port.private_data = port->private_data;
2994 uart->port.dev = port->dev;
2996 ret = uart_add_one_port(&serial8250_reg, &uart->port);
2998 ret = uart->port.line;
3000 mutex_unlock(&serial_mutex);
3004 EXPORT_SYMBOL(serial8250_register_port);
3007 * serial8250_unregister_port - remove a 16x50 serial port at runtime
3008 * @line: serial line number
3010 * Remove one serial port. This may not be called from interrupt
3011 * context. We hand the port back to the our control.
3013 void serial8250_unregister_port(int line)
3015 struct uart_8250_port *uart = &serial8250_ports[line];
3017 mutex_lock(&serial_mutex);
3018 uart_remove_one_port(&serial8250_reg, &uart->port);
3019 if (serial8250_isa_devs) {
3020 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3021 uart->port.type = PORT_UNKNOWN;
3022 uart->port.dev = &serial8250_isa_devs->dev;
3023 uart_add_one_port(&serial8250_reg, &uart->port);
3025 uart->port.dev = NULL;
3027 mutex_unlock(&serial_mutex);
3029 EXPORT_SYMBOL(serial8250_unregister_port);
3031 static int __init serial8250_init(void)
3035 if (nr_uarts > UART_NR)
3038 printk(KERN_INFO "Serial: 8250/16550 driver"
3039 "%d ports, IRQ sharing %sabled\n", nr_uarts,
3040 share_irqs ? "en" : "dis");
3043 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3045 serial8250_reg.nr = UART_NR;
3046 ret = uart_register_driver(&serial8250_reg);
3051 serial8250_isa_devs = platform_device_alloc("serial8250",
3052 PLAT8250_DEV_LEGACY);
3053 if (!serial8250_isa_devs) {
3055 goto unreg_uart_drv;
3058 ret = platform_device_add(serial8250_isa_devs);
3062 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3064 ret = platform_driver_register(&serial8250_isa_driver);
3068 platform_device_del(serial8250_isa_devs);
3070 platform_device_put(serial8250_isa_devs);
3073 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3075 uart_unregister_driver(&serial8250_reg);
3081 static void __exit serial8250_exit(void)
3083 struct platform_device *isa_dev = serial8250_isa_devs;
3086 * This tells serial8250_unregister_port() not to re-register
3087 * the ports (thereby making serial8250_isa_driver permanently
3090 serial8250_isa_devs = NULL;
3092 platform_driver_unregister(&serial8250_isa_driver);
3093 platform_device_unregister(isa_dev);
3096 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3098 uart_unregister_driver(&serial8250_reg);
3102 module_init(serial8250_init);
3103 module_exit(serial8250_exit);
3105 EXPORT_SYMBOL(serial8250_suspend_port);
3106 EXPORT_SYMBOL(serial8250_resume_port);
3108 MODULE_LICENSE("GPL");
3109 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
3111 module_param(share_irqs, uint, 0644);
3112 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3115 module_param(nr_uarts, uint, 0644);
3116 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3118 #ifdef CONFIG_SERIAL_8250_RSA
3119 module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3120 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3122 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);