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>
46 #include <asm/serial.h>
52 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
53 * is unsafe when used on edge-triggered interrupts.
55 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
57 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
63 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
65 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
69 #define DEBUG_INTR(fmt...) printk(fmt)
71 #define DEBUG_INTR(fmt...) do { } while (0)
74 #define PASS_LIMIT 256
77 * We default to IRQ0 for the "no irq" hack. Some
78 * machine types want others as well - they're free
79 * to redefine this in their header file.
81 #define is_real_interrupt(irq) ((irq) != 0)
83 #ifdef CONFIG_SERIAL_8250_DETECT_IRQ
84 #define CONFIG_SERIAL_DETECT_IRQ 1
86 #ifdef CONFIG_SERIAL_8250_MANY_PORTS
87 #define CONFIG_SERIAL_MANY_PORTS 1
91 * HUB6 is always on. This will be removed once the header
92 * files have been cleaned.
97 * SERIAL_PORT_DFNS tells us about built-in ports that have no
98 * standard enumeration mechanism. Platforms that can find all
99 * serial ports via mechanisms like ACPI or PCI need not supply it.
101 #ifndef SERIAL_PORT_DFNS
102 #define SERIAL_PORT_DFNS
105 static const struct old_serial_port old_serial_port[] = {
106 SERIAL_PORT_DFNS /* defined in asm/serial.h */
109 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
111 #ifdef CONFIG_SERIAL_8250_RSA
113 #define PORT_RSA_MAX 4
114 static unsigned long probe_rsa[PORT_RSA_MAX];
115 static unsigned int probe_rsa_count;
116 #endif /* CONFIG_SERIAL_8250_RSA */
118 struct uart_8250_port {
119 struct uart_port port;
120 struct timer_list timer; /* "no irq" timer */
121 struct list_head list; /* ports on this IRQ */
122 unsigned short capabilities; /* port capabilities */
123 unsigned short bugs; /* port bugs */
124 unsigned int tx_loadsz; /* transmit fifo load size */
129 unsigned char mcr_mask; /* mask of user bits */
130 unsigned char mcr_force; /* mask of forced bits */
133 * Some bits in registers are cleared on a read, so they must
134 * be saved whenever the register is read but the bits will not
135 * be immediately processed.
137 #define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS
138 unsigned char lsr_saved_flags;
139 #define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
140 unsigned char msr_saved_flags;
143 * We provide a per-port pm hook.
145 void (*pm)(struct uart_port *port,
146 unsigned int state, unsigned int old);
151 struct list_head *head;
154 static struct irq_info irq_lists[NR_IRQS];
157 * Here we define the default xmit fifo size used for each type of UART.
159 static const struct serial8250_config uart_config[] = {
184 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
185 .flags = UART_CAP_FIFO,
196 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
202 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
204 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
210 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
212 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
220 .name = "16C950/954",
223 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
224 .flags = UART_CAP_FIFO,
230 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
232 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
238 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
239 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
245 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
246 .flags = UART_CAP_FIFO,
252 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
253 .flags = UART_CAP_FIFO | UART_NATSEMI,
259 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
260 .flags = UART_CAP_FIFO | UART_CAP_UUE,
266 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
267 .flags = UART_CAP_FIFO,
271 #if defined (CONFIG_SERIAL_8250_AU1X00)
273 /* Au1x00 UART hardware has a weird register layout */
274 static const u8 au_io_in_map[] = {
284 static const u8 au_io_out_map[] = {
292 /* sane hardware needs no mapping */
293 static inline int map_8250_in_reg(struct uart_8250_port *up, int offset)
295 if (up->port.iotype != UPIO_AU)
297 return au_io_in_map[offset];
300 static inline int map_8250_out_reg(struct uart_8250_port *up, int offset)
302 if (up->port.iotype != UPIO_AU)
304 return au_io_out_map[offset];
307 #elif defined(CONFIG_SERIAL_8250_RM9K)
331 static inline int map_8250_in_reg(struct uart_8250_port *up, int offset)
333 if (up->port.iotype != UPIO_RM9000)
335 return regmap_in[offset];
338 static inline int map_8250_out_reg(struct uart_8250_port *up, int offset)
340 if (up->port.iotype != UPIO_RM9000)
342 return regmap_out[offset];
347 /* sane hardware needs no mapping */
348 #define map_8250_in_reg(up, offset) (offset)
349 #define map_8250_out_reg(up, offset) (offset)
353 static unsigned int serial_in(struct uart_8250_port *up, int offset)
356 offset = map_8250_in_reg(up, offset) << up->port.regshift;
358 switch (up->port.iotype) {
360 outb(up->port.hub6 - 1 + offset, up->port.iobase);
361 return inb(up->port.iobase + 1);
365 return readb(up->port.membase + offset);
369 return readl(up->port.membase + offset);
371 #ifdef CONFIG_SERIAL_8250_AU1X00
373 return __raw_readl(up->port.membase + offset);
377 if (offset == UART_IIR) {
378 tmp = readl(up->port.membase + (UART_IIR & ~3));
379 return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
381 return readb(up->port.membase + offset);
384 return inb(up->port.iobase + offset);
389 serial_out(struct uart_8250_port *up, int offset, int value)
391 /* Save the offset before it's remapped */
392 int save_offset = offset;
393 offset = map_8250_out_reg(up, offset) << up->port.regshift;
395 switch (up->port.iotype) {
397 outb(up->port.hub6 - 1 + offset, up->port.iobase);
398 outb(value, up->port.iobase + 1);
402 writeb(value, up->port.membase + offset);
407 writel(value, up->port.membase + offset);
410 #ifdef CONFIG_SERIAL_8250_AU1X00
412 __raw_writel(value, up->port.membase + offset);
416 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
417 writeb(value, up->port.membase + offset);
421 /* Save the LCR value so it can be re-written when a
422 * Busy Detect interrupt occurs. */
423 if (save_offset == UART_LCR)
425 writeb(value, up->port.membase + offset);
426 /* Read the IER to ensure any interrupt is cleared before
427 * returning from ISR. */
428 if (save_offset == UART_TX || save_offset == UART_IER)
429 value = serial_in(up, UART_IER);
433 outb(value, up->port.iobase + offset);
438 serial_out_sync(struct uart_8250_port *up, int offset, int value)
440 switch (up->port.iotype) {
443 #ifdef CONFIG_SERIAL_8250_AU1X00
447 serial_out(up, offset, value);
448 serial_in(up, UART_LCR); /* safe, no side-effects */
451 serial_out(up, offset, value);
456 * We used to support using pause I/O for certain machines. We
457 * haven't supported this for a while, but just in case it's badly
458 * needed for certain old 386 machines, I've left these #define's
461 #define serial_inp(up, offset) serial_in(up, offset)
462 #define serial_outp(up, offset, value) serial_out(up, offset, value)
464 /* Uart divisor latch read */
465 static inline int _serial_dl_read(struct uart_8250_port *up)
467 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
470 /* Uart divisor latch write */
471 static inline void _serial_dl_write(struct uart_8250_port *up, int value)
473 serial_outp(up, UART_DLL, value & 0xff);
474 serial_outp(up, UART_DLM, value >> 8 & 0xff);
477 #if defined(CONFIG_SERIAL_8250_AU1X00)
478 /* Au1x00 haven't got a standard divisor latch */
479 static int serial_dl_read(struct uart_8250_port *up)
481 if (up->port.iotype == UPIO_AU)
482 return __raw_readl(up->port.membase + 0x28);
484 return _serial_dl_read(up);
487 static void serial_dl_write(struct uart_8250_port *up, int value)
489 if (up->port.iotype == UPIO_AU)
490 __raw_writel(value, up->port.membase + 0x28);
492 _serial_dl_write(up, value);
494 #elif defined(CONFIG_SERIAL_8250_RM9K)
495 static int serial_dl_read(struct uart_8250_port *up)
497 return (up->port.iotype == UPIO_RM9000) ?
498 (((__raw_readl(up->port.membase + 0x10) << 8) |
499 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
503 static void serial_dl_write(struct uart_8250_port *up, int value)
505 if (up->port.iotype == UPIO_RM9000) {
506 __raw_writel(value, up->port.membase + 0x08);
507 __raw_writel(value >> 8, up->port.membase + 0x10);
509 _serial_dl_write(up, value);
513 #define serial_dl_read(up) _serial_dl_read(up)
514 #define serial_dl_write(up, value) _serial_dl_write(up, value)
520 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
522 serial_out(up, UART_SCR, offset);
523 serial_out(up, UART_ICR, value);
526 static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
530 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
531 serial_out(up, UART_SCR, offset);
532 value = serial_in(up, UART_ICR);
533 serial_icr_write(up, UART_ACR, up->acr);
541 static inline void serial8250_clear_fifos(struct uart_8250_port *p)
543 if (p->capabilities & UART_CAP_FIFO) {
544 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
545 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
546 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
547 serial_outp(p, UART_FCR, 0);
552 * IER sleep support. UARTs which have EFRs need the "extended
553 * capability" bit enabled. Note that on XR16C850s, we need to
554 * reset LCR to write to IER.
556 static inline void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
558 if (p->capabilities & UART_CAP_SLEEP) {
559 if (p->capabilities & UART_CAP_EFR) {
560 serial_outp(p, UART_LCR, 0xBF);
561 serial_outp(p, UART_EFR, UART_EFR_ECB);
562 serial_outp(p, UART_LCR, 0);
564 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
565 if (p->capabilities & UART_CAP_EFR) {
566 serial_outp(p, UART_LCR, 0xBF);
567 serial_outp(p, UART_EFR, 0);
568 serial_outp(p, UART_LCR, 0);
573 #ifdef CONFIG_SERIAL_8250_RSA
575 * Attempts to turn on the RSA FIFO. Returns zero on failure.
576 * We set the port uart clock rate if we succeed.
578 static int __enable_rsa(struct uart_8250_port *up)
583 mode = serial_inp(up, UART_RSA_MSR);
584 result = mode & UART_RSA_MSR_FIFO;
587 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
588 mode = serial_inp(up, UART_RSA_MSR);
589 result = mode & UART_RSA_MSR_FIFO;
593 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
598 static void enable_rsa(struct uart_8250_port *up)
600 if (up->port.type == PORT_RSA) {
601 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
602 spin_lock_irq(&up->port.lock);
604 spin_unlock_irq(&up->port.lock);
606 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
607 serial_outp(up, UART_RSA_FRR, 0);
612 * Attempts to turn off the RSA FIFO. Returns zero on failure.
613 * It is unknown why interrupts were disabled in here. However,
614 * the caller is expected to preserve this behaviour by grabbing
615 * the spinlock before calling this function.
617 static void disable_rsa(struct uart_8250_port *up)
622 if (up->port.type == PORT_RSA &&
623 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
624 spin_lock_irq(&up->port.lock);
626 mode = serial_inp(up, UART_RSA_MSR);
627 result = !(mode & UART_RSA_MSR_FIFO);
630 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
631 mode = serial_inp(up, UART_RSA_MSR);
632 result = !(mode & UART_RSA_MSR_FIFO);
636 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
637 spin_unlock_irq(&up->port.lock);
640 #endif /* CONFIG_SERIAL_8250_RSA */
643 * This is a quickie test to see how big the FIFO is.
644 * It doesn't work at all the time, more's the pity.
646 static int size_fifo(struct uart_8250_port *up)
648 unsigned char old_fcr, old_mcr, old_lcr;
649 unsigned short old_dl;
652 old_lcr = serial_inp(up, UART_LCR);
653 serial_outp(up, UART_LCR, 0);
654 old_fcr = serial_inp(up, UART_FCR);
655 old_mcr = serial_inp(up, UART_MCR);
656 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
657 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
658 serial_outp(up, UART_MCR, UART_MCR_LOOP);
659 serial_outp(up, UART_LCR, UART_LCR_DLAB);
660 old_dl = serial_dl_read(up);
661 serial_dl_write(up, 0x0001);
662 serial_outp(up, UART_LCR, 0x03);
663 for (count = 0; count < 256; count++)
664 serial_outp(up, UART_TX, count);
665 mdelay(20);/* FIXME - schedule_timeout */
666 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
667 (count < 256); count++)
668 serial_inp(up, UART_RX);
669 serial_outp(up, UART_FCR, old_fcr);
670 serial_outp(up, UART_MCR, old_mcr);
671 serial_outp(up, UART_LCR, UART_LCR_DLAB);
672 serial_dl_write(up, old_dl);
673 serial_outp(up, UART_LCR, old_lcr);
679 * Read UART ID using the divisor method - set DLL and DLM to zero
680 * and the revision will be in DLL and device type in DLM. We
681 * preserve the device state across this.
683 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
685 unsigned char old_dll, old_dlm, old_lcr;
688 old_lcr = serial_inp(p, UART_LCR);
689 serial_outp(p, UART_LCR, UART_LCR_DLAB);
691 old_dll = serial_inp(p, UART_DLL);
692 old_dlm = serial_inp(p, UART_DLM);
694 serial_outp(p, UART_DLL, 0);
695 serial_outp(p, UART_DLM, 0);
697 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
699 serial_outp(p, UART_DLL, old_dll);
700 serial_outp(p, UART_DLM, old_dlm);
701 serial_outp(p, UART_LCR, old_lcr);
707 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
708 * When this function is called we know it is at least a StarTech
709 * 16650 V2, but it might be one of several StarTech UARTs, or one of
710 * its clones. (We treat the broken original StarTech 16650 V1 as a
711 * 16550, and why not? Startech doesn't seem to even acknowledge its
714 * What evil have men's minds wrought...
716 static void autoconfig_has_efr(struct uart_8250_port *up)
718 unsigned int id1, id2, id3, rev;
721 * Everything with an EFR has SLEEP
723 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
726 * First we check to see if it's an Oxford Semiconductor UART.
728 * If we have to do this here because some non-National
729 * Semiconductor clone chips lock up if you try writing to the
730 * LSR register (which serial_icr_read does)
734 * Check for Oxford Semiconductor 16C950.
736 * EFR [4] must be set else this test fails.
738 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
739 * claims that it's needed for 952 dual UART's (which are not
740 * recommended for new designs).
743 serial_out(up, UART_LCR, 0xBF);
744 serial_out(up, UART_EFR, UART_EFR_ECB);
745 serial_out(up, UART_LCR, 0x00);
746 id1 = serial_icr_read(up, UART_ID1);
747 id2 = serial_icr_read(up, UART_ID2);
748 id3 = serial_icr_read(up, UART_ID3);
749 rev = serial_icr_read(up, UART_REV);
751 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
753 if (id1 == 0x16 && id2 == 0xC9 &&
754 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
755 up->port.type = PORT_16C950;
758 * Enable work around for the Oxford Semiconductor 952 rev B
759 * chip which causes it to seriously miscalculate baud rates
762 if (id3 == 0x52 && rev == 0x01)
763 up->bugs |= UART_BUG_QUOT;
768 * We check for a XR16C850 by setting DLL and DLM to 0, and then
769 * reading back DLL and DLM. The chip type depends on the DLM
771 * 0x10 - XR16C850 and the DLL contains the chip revision.
775 id1 = autoconfig_read_divisor_id(up);
776 DEBUG_AUTOCONF("850id=%04x ", id1);
779 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
780 up->port.type = PORT_16850;
785 * It wasn't an XR16C850.
787 * We distinguish between the '654 and the '650 by counting
788 * how many bytes are in the FIFO. I'm using this for now,
789 * since that's the technique that was sent to me in the
790 * serial driver update, but I'm not convinced this works.
791 * I've had problems doing this in the past. -TYT
793 if (size_fifo(up) == 64)
794 up->port.type = PORT_16654;
796 up->port.type = PORT_16650V2;
800 * We detected a chip without a FIFO. Only two fall into
801 * this category - the original 8250 and the 16450. The
802 * 16450 has a scratch register (accessible with LCR=0)
804 static void autoconfig_8250(struct uart_8250_port *up)
806 unsigned char scratch, status1, status2;
808 up->port.type = PORT_8250;
810 scratch = serial_in(up, UART_SCR);
811 serial_outp(up, UART_SCR, 0xa5);
812 status1 = serial_in(up, UART_SCR);
813 serial_outp(up, UART_SCR, 0x5a);
814 status2 = serial_in(up, UART_SCR);
815 serial_outp(up, UART_SCR, scratch);
817 if (status1 == 0xa5 && status2 == 0x5a)
818 up->port.type = PORT_16450;
821 static int broken_efr(struct uart_8250_port *up)
824 * Exar ST16C2550 "A2" devices incorrectly detect as
825 * having an EFR, and report an ID of 0x0201. See
826 * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf
828 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
835 * We know that the chip has FIFOs. Does it have an EFR? The
836 * EFR is located in the same register position as the IIR and
837 * we know the top two bits of the IIR are currently set. The
838 * EFR should contain zero. Try to read the EFR.
840 static void autoconfig_16550a(struct uart_8250_port *up)
842 unsigned char status1, status2;
843 unsigned int iersave;
845 up->port.type = PORT_16550A;
846 up->capabilities |= UART_CAP_FIFO;
849 * Check for presence of the EFR when DLAB is set.
850 * Only ST16C650V1 UARTs pass this test.
852 serial_outp(up, UART_LCR, UART_LCR_DLAB);
853 if (serial_in(up, UART_EFR) == 0) {
854 serial_outp(up, UART_EFR, 0xA8);
855 if (serial_in(up, UART_EFR) != 0) {
856 DEBUG_AUTOCONF("EFRv1 ");
857 up->port.type = PORT_16650;
858 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
860 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
862 serial_outp(up, UART_EFR, 0);
867 * Maybe it requires 0xbf to be written to the LCR.
868 * (other ST16C650V2 UARTs, TI16C752A, etc)
870 serial_outp(up, UART_LCR, 0xBF);
871 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
872 DEBUG_AUTOCONF("EFRv2 ");
873 autoconfig_has_efr(up);
878 * Check for a National Semiconductor SuperIO chip.
879 * Attempt to switch to bank 2, read the value of the LOOP bit
880 * from EXCR1. Switch back to bank 0, change it in MCR. Then
881 * switch back to bank 2, read it from EXCR1 again and check
882 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
884 serial_outp(up, UART_LCR, 0);
885 status1 = serial_in(up, UART_MCR);
886 serial_outp(up, UART_LCR, 0xE0);
887 status2 = serial_in(up, 0x02); /* EXCR1 */
889 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
890 serial_outp(up, UART_LCR, 0);
891 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
892 serial_outp(up, UART_LCR, 0xE0);
893 status2 = serial_in(up, 0x02); /* EXCR1 */
894 serial_outp(up, UART_LCR, 0);
895 serial_outp(up, UART_MCR, status1);
897 if ((status2 ^ status1) & UART_MCR_LOOP) {
900 serial_outp(up, UART_LCR, 0xE0);
902 quot = serial_dl_read(up);
905 status1 = serial_in(up, 0x04); /* EXCR2 */
906 status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
907 status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
908 serial_outp(up, 0x04, status1);
910 serial_dl_write(up, quot);
912 serial_outp(up, UART_LCR, 0);
914 up->port.uartclk = 921600*16;
915 up->port.type = PORT_NS16550A;
916 up->capabilities |= UART_NATSEMI;
922 * No EFR. Try to detect a TI16750, which only sets bit 5 of
923 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
924 * Try setting it with and without DLAB set. Cheap clones
925 * set bit 5 without DLAB set.
927 serial_outp(up, UART_LCR, 0);
928 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
929 status1 = serial_in(up, UART_IIR) >> 5;
930 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
931 serial_outp(up, UART_LCR, UART_LCR_DLAB);
932 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
933 status2 = serial_in(up, UART_IIR) >> 5;
934 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
935 serial_outp(up, UART_LCR, 0);
937 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
939 if (status1 == 6 && status2 == 7) {
940 up->port.type = PORT_16750;
941 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
946 * Try writing and reading the UART_IER_UUE bit (b6).
947 * If it works, this is probably one of the Xscale platform's
949 * We're going to explicitly set the UUE bit to 0 before
950 * trying to write and read a 1 just to make sure it's not
951 * already a 1 and maybe locked there before we even start start.
953 iersave = serial_in(up, UART_IER);
954 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
955 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
957 * OK it's in a known zero state, try writing and reading
958 * without disturbing the current state of the other bits.
960 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
961 if (serial_in(up, UART_IER) & UART_IER_UUE) {
964 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
966 DEBUG_AUTOCONF("Xscale ");
967 up->port.type = PORT_XSCALE;
968 up->capabilities |= UART_CAP_UUE;
973 * If we got here we couldn't force the IER_UUE bit to 0.
974 * Log it and continue.
976 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
978 serial_outp(up, UART_IER, iersave);
982 * This routine is called by rs_init() to initialize a specific serial
983 * port. It determines what type of UART chip this serial port is
984 * using: 8250, 16450, 16550, 16550A. The important question is
985 * whether or not this UART is a 16550A or not, since this will
986 * determine whether or not we can use its FIFO features or not.
988 static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
990 unsigned char status1, scratch, scratch2, scratch3;
991 unsigned char save_lcr, save_mcr;
994 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
997 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ",
998 up->port.line, up->port.iobase, up->port.membase);
1001 * We really do need global IRQs disabled here - we're going to
1002 * be frobbing the chips IRQ enable register to see if it exists.
1004 spin_lock_irqsave(&up->port.lock, flags);
1006 up->capabilities = 0;
1009 if (!(up->port.flags & UPF_BUGGY_UART)) {
1011 * Do a simple existence test first; if we fail this,
1012 * there's no point trying anything else.
1014 * 0x80 is used as a nonsense port to prevent against
1015 * false positives due to ISA bus float. The
1016 * assumption is that 0x80 is a non-existent port;
1017 * which should be safe since include/asm/io.h also
1018 * makes this assumption.
1020 * Note: this is safe as long as MCR bit 4 is clear
1021 * and the device is in "PC" mode.
1023 scratch = serial_inp(up, UART_IER);
1024 serial_outp(up, UART_IER, 0);
1029 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1030 * 16C754B) allow only to modify them if an EFR bit is set.
1032 scratch2 = serial_inp(up, UART_IER) & 0x0f;
1033 serial_outp(up, UART_IER, 0x0F);
1037 scratch3 = serial_inp(up, UART_IER) & 0x0f;
1038 serial_outp(up, UART_IER, scratch);
1039 if (scratch2 != 0 || scratch3 != 0x0F) {
1041 * We failed; there's nothing here
1043 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1044 scratch2, scratch3);
1049 save_mcr = serial_in(up, UART_MCR);
1050 save_lcr = serial_in(up, UART_LCR);
1053 * Check to see if a UART is really there. Certain broken
1054 * internal modems based on the Rockwell chipset fail this
1055 * test, because they apparently don't implement the loopback
1056 * test mode. So this test is skipped on the COM 1 through
1057 * COM 4 ports. This *should* be safe, since no board
1058 * manufacturer would be stupid enough to design a board
1059 * that conflicts with COM 1-4 --- we hope!
1061 if (!(up->port.flags & UPF_SKIP_TEST)) {
1062 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1063 status1 = serial_inp(up, UART_MSR) & 0xF0;
1064 serial_outp(up, UART_MCR, save_mcr);
1065 if (status1 != 0x90) {
1066 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1073 * We're pretty sure there's a port here. Lets find out what
1074 * type of port it is. The IIR top two bits allows us to find
1075 * out if it's 8250 or 16450, 16550, 16550A or later. This
1076 * determines what we test for next.
1078 * We also initialise the EFR (if any) to zero for later. The
1079 * EFR occupies the same register location as the FCR and IIR.
1081 serial_outp(up, UART_LCR, 0xBF);
1082 serial_outp(up, UART_EFR, 0);
1083 serial_outp(up, UART_LCR, 0);
1085 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1086 scratch = serial_in(up, UART_IIR) >> 6;
1088 DEBUG_AUTOCONF("iir=%d ", scratch);
1092 autoconfig_8250(up);
1095 up->port.type = PORT_UNKNOWN;
1098 up->port.type = PORT_16550;
1101 autoconfig_16550a(up);
1105 #ifdef CONFIG_SERIAL_8250_RSA
1107 * Only probe for RSA ports if we got the region.
1109 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1112 for (i = 0 ; i < probe_rsa_count; ++i) {
1113 if (probe_rsa[i] == up->port.iobase &&
1115 up->port.type = PORT_RSA;
1122 #ifdef CONFIG_SERIAL_8250_AU1X00
1123 /* if access method is AU, it is a 16550 with a quirk */
1124 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
1125 up->bugs |= UART_BUG_NOMSR;
1128 serial_outp(up, UART_LCR, save_lcr);
1130 if (up->capabilities != uart_config[up->port.type].flags) {
1132 "ttyS%d: detected caps %08x should be %08x\n",
1133 up->port.line, up->capabilities,
1134 uart_config[up->port.type].flags);
1137 up->port.fifosize = uart_config[up->port.type].fifo_size;
1138 up->capabilities = uart_config[up->port.type].flags;
1139 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1141 if (up->port.type == PORT_UNKNOWN)
1147 #ifdef CONFIG_SERIAL_8250_RSA
1148 if (up->port.type == PORT_RSA)
1149 serial_outp(up, UART_RSA_FRR, 0);
1151 serial_outp(up, UART_MCR, save_mcr);
1152 serial8250_clear_fifos(up);
1153 serial_in(up, UART_RX);
1154 if (up->capabilities & UART_CAP_UUE)
1155 serial_outp(up, UART_IER, UART_IER_UUE);
1157 serial_outp(up, UART_IER, 0);
1160 spin_unlock_irqrestore(&up->port.lock, flags);
1161 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1164 static void autoconfig_irq(struct uart_8250_port *up)
1166 unsigned char save_mcr, save_ier;
1167 unsigned char save_ICP = 0;
1168 unsigned int ICP = 0;
1172 if (up->port.flags & UPF_FOURPORT) {
1173 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1174 save_ICP = inb_p(ICP);
1179 /* forget possible initially masked and pending IRQ */
1180 probe_irq_off(probe_irq_on());
1181 save_mcr = serial_inp(up, UART_MCR);
1182 save_ier = serial_inp(up, UART_IER);
1183 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1185 irqs = probe_irq_on();
1186 serial_outp(up, UART_MCR, 0);
1188 if (up->port.flags & UPF_FOURPORT) {
1189 serial_outp(up, UART_MCR,
1190 UART_MCR_DTR | UART_MCR_RTS);
1192 serial_outp(up, UART_MCR,
1193 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1195 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1196 (void)serial_inp(up, UART_LSR);
1197 (void)serial_inp(up, UART_RX);
1198 (void)serial_inp(up, UART_IIR);
1199 (void)serial_inp(up, UART_MSR);
1200 serial_outp(up, UART_TX, 0xFF);
1202 irq = probe_irq_off(irqs);
1204 serial_outp(up, UART_MCR, save_mcr);
1205 serial_outp(up, UART_IER, save_ier);
1207 if (up->port.flags & UPF_FOURPORT)
1208 outb_p(save_ICP, ICP);
1210 up->port.irq = (irq > 0) ? irq : 0;
1213 static inline void __stop_tx(struct uart_8250_port *p)
1215 if (p->ier & UART_IER_THRI) {
1216 p->ier &= ~UART_IER_THRI;
1217 serial_out(p, UART_IER, p->ier);
1221 static void serial8250_stop_tx(struct uart_port *port)
1223 struct uart_8250_port *up = (struct uart_8250_port *)port;
1228 * We really want to stop the transmitter from sending.
1230 if (up->port.type == PORT_16C950) {
1231 up->acr |= UART_ACR_TXDIS;
1232 serial_icr_write(up, UART_ACR, up->acr);
1236 static void transmit_chars(struct uart_8250_port *up);
1238 static void serial8250_start_tx(struct uart_port *port)
1240 struct uart_8250_port *up = (struct uart_8250_port *)port;
1242 if (!(up->ier & UART_IER_THRI)) {
1243 up->ier |= UART_IER_THRI;
1244 serial_out(up, UART_IER, up->ier);
1246 if (up->bugs & UART_BUG_TXEN) {
1247 unsigned char lsr, iir;
1248 lsr = serial_in(up, UART_LSR);
1249 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1250 iir = serial_in(up, UART_IIR) & 0x0f;
1251 if ((up->port.type == PORT_RM9000) ?
1252 (lsr & UART_LSR_THRE &&
1253 (iir == UART_IIR_NO_INT || iir == UART_IIR_THRI)) :
1254 (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT))
1260 * Re-enable the transmitter if we disabled it.
1262 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1263 up->acr &= ~UART_ACR_TXDIS;
1264 serial_icr_write(up, UART_ACR, up->acr);
1268 static void serial8250_stop_rx(struct uart_port *port)
1270 struct uart_8250_port *up = (struct uart_8250_port *)port;
1272 up->ier &= ~UART_IER_RLSI;
1273 up->port.read_status_mask &= ~UART_LSR_DR;
1274 serial_out(up, UART_IER, up->ier);
1277 static void serial8250_enable_ms(struct uart_port *port)
1279 struct uart_8250_port *up = (struct uart_8250_port *)port;
1281 /* no MSR capabilities */
1282 if (up->bugs & UART_BUG_NOMSR)
1285 up->ier |= UART_IER_MSI;
1286 serial_out(up, UART_IER, up->ier);
1290 receive_chars(struct uart_8250_port *up, unsigned int *status)
1292 struct tty_struct *tty = up->port.info->tty;
1293 unsigned char ch, lsr = *status;
1294 int max_count = 256;
1298 ch = serial_inp(up, UART_RX);
1300 up->port.icount.rx++;
1302 lsr |= up->lsr_saved_flags;
1303 up->lsr_saved_flags = 0;
1305 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1307 * For statistics only
1309 if (lsr & UART_LSR_BI) {
1310 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1311 up->port.icount.brk++;
1313 * We do the SysRQ and SAK checking
1314 * here because otherwise the break
1315 * may get masked by ignore_status_mask
1316 * or read_status_mask.
1318 if (uart_handle_break(&up->port))
1320 } else if (lsr & UART_LSR_PE)
1321 up->port.icount.parity++;
1322 else if (lsr & UART_LSR_FE)
1323 up->port.icount.frame++;
1324 if (lsr & UART_LSR_OE)
1325 up->port.icount.overrun++;
1328 * Mask off conditions which should be ignored.
1330 lsr &= up->port.read_status_mask;
1332 if (lsr & UART_LSR_BI) {
1333 DEBUG_INTR("handling break....");
1335 } else if (lsr & UART_LSR_PE)
1337 else if (lsr & UART_LSR_FE)
1340 if (uart_handle_sysrq_char(&up->port, ch))
1343 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1346 lsr = serial_inp(up, UART_LSR);
1347 } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
1348 spin_unlock(&up->port.lock);
1349 tty_flip_buffer_push(tty);
1350 spin_lock(&up->port.lock);
1354 static void transmit_chars(struct uart_8250_port *up)
1356 struct circ_buf *xmit = &up->port.info->xmit;
1359 if (up->port.x_char) {
1360 serial_outp(up, UART_TX, up->port.x_char);
1361 up->port.icount.tx++;
1362 up->port.x_char = 0;
1365 if (uart_tx_stopped(&up->port)) {
1366 serial8250_stop_tx(&up->port);
1369 if (uart_circ_empty(xmit)) {
1374 count = up->tx_loadsz;
1376 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1377 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1378 up->port.icount.tx++;
1379 if (uart_circ_empty(xmit))
1381 } while (--count > 0);
1383 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1384 uart_write_wakeup(&up->port);
1386 DEBUG_INTR("THRE...");
1388 if (uart_circ_empty(xmit))
1392 static unsigned int check_modem_status(struct uart_8250_port *up)
1394 unsigned int status = serial_in(up, UART_MSR);
1396 status |= up->msr_saved_flags;
1397 up->msr_saved_flags = 0;
1398 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1399 up->port.info != NULL) {
1400 if (status & UART_MSR_TERI)
1401 up->port.icount.rng++;
1402 if (status & UART_MSR_DDSR)
1403 up->port.icount.dsr++;
1404 if (status & UART_MSR_DDCD)
1405 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1406 if (status & UART_MSR_DCTS)
1407 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
1409 wake_up_interruptible(&up->port.info->delta_msr_wait);
1416 * This handles the interrupt from one port.
1419 serial8250_handle_port(struct uart_8250_port *up)
1421 unsigned int status;
1422 unsigned long flags;
1424 spin_lock_irqsave(&up->port.lock, flags);
1426 status = serial_inp(up, UART_LSR);
1428 DEBUG_INTR("status = %x...", status);
1430 if (status & UART_LSR_DR)
1431 receive_chars(up, &status);
1432 check_modem_status(up);
1433 if (status & UART_LSR_THRE)
1436 spin_unlock_irqrestore(&up->port.lock, flags);
1440 * This is the serial driver's interrupt routine.
1442 * Arjan thinks the old way was overly complex, so it got simplified.
1443 * Alan disagrees, saying that need the complexity to handle the weird
1444 * nature of ISA shared interrupts. (This is a special exception.)
1446 * In order to handle ISA shared interrupts properly, we need to check
1447 * that all ports have been serviced, and therefore the ISA interrupt
1448 * line has been de-asserted.
1450 * This means we need to loop through all ports. checking that they
1451 * don't have an interrupt pending.
1453 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
1455 struct irq_info *i = dev_id;
1456 struct list_head *l, *end = NULL;
1457 int pass_counter = 0, handled = 0;
1459 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1461 spin_lock(&i->lock);
1465 struct uart_8250_port *up;
1468 up = list_entry(l, struct uart_8250_port, list);
1470 iir = serial_in(up, UART_IIR);
1471 if (!(iir & UART_IIR_NO_INT)) {
1472 serial8250_handle_port(up);
1477 } else if (up->port.iotype == UPIO_DWAPB &&
1478 (iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
1479 /* The DesignWare APB UART has an Busy Detect (0x07)
1480 * interrupt meaning an LCR write attempt occured while the
1481 * UART was busy. The interrupt must be cleared by reading
1482 * the UART status register (USR) and the LCR re-written. */
1483 unsigned int status;
1484 status = *(volatile u32 *)up->port.private_data;
1485 serial_out(up, UART_LCR, up->lcr);
1490 } else if (end == NULL)
1495 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1496 /* If we hit this, we're dead. */
1497 printk(KERN_ERR "serial8250: too much work for "
1503 spin_unlock(&i->lock);
1505 DEBUG_INTR("end.\n");
1507 #ifdef CONFIG_ARCH_OMAP15XX
1508 return IRQ_HANDLED; /* FIXME: iir status not ready on 1510 */
1510 return IRQ_RETVAL(handled);
1515 * To support ISA shared interrupts, we need to have one interrupt
1516 * handler that ensures that the IRQ line has been deasserted
1517 * before returning. Failing to do this will result in the IRQ
1518 * line being stuck active, and, since ISA irqs are edge triggered,
1519 * no more IRQs will be seen.
1521 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1523 spin_lock_irq(&i->lock);
1525 if (!list_empty(i->head)) {
1526 if (i->head == &up->list)
1527 i->head = i->head->next;
1528 list_del(&up->list);
1530 BUG_ON(i->head != &up->list);
1534 spin_unlock_irq(&i->lock);
1537 static int serial_link_irq_chain(struct uart_8250_port *up)
1539 struct irq_info *i = irq_lists + up->port.irq;
1540 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
1542 spin_lock_irq(&i->lock);
1545 list_add(&up->list, i->head);
1546 spin_unlock_irq(&i->lock);
1550 INIT_LIST_HEAD(&up->list);
1551 i->head = &up->list;
1552 spin_unlock_irq(&i->lock);
1554 irq_flags |= SERIAL_EXTRA_IRQ_FLAGS;
1556 ret = request_irq(up->port.irq, serial8250_interrupt,
1557 irq_flags, "serial", i);
1559 serial_do_unlink(i, up);
1565 static void serial_unlink_irq_chain(struct uart_8250_port *up)
1567 struct irq_info *i = irq_lists + up->port.irq;
1569 BUG_ON(i->head == NULL);
1571 if (list_empty(i->head))
1572 free_irq(up->port.irq, i);
1574 serial_do_unlink(i, up);
1577 /* Base timer interval for polling */
1578 static inline int poll_timeout(int timeout)
1580 return timeout > 6 ? (timeout / 2 - 2) : 1;
1584 * This function is used to handle ports that do not have an
1585 * interrupt. This doesn't work very well for 16450's, but gives
1586 * barely passable results for a 16550A. (Although at the expense
1587 * of much CPU overhead).
1589 static void serial8250_timeout(unsigned long data)
1591 struct uart_8250_port *up = (struct uart_8250_port *)data;
1594 iir = serial_in(up, UART_IIR);
1595 if (!(iir & UART_IIR_NO_INT))
1596 serial8250_handle_port(up);
1597 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
1600 static void serial8250_backup_timeout(unsigned long data)
1602 struct uart_8250_port *up = (struct uart_8250_port *)data;
1603 unsigned int iir, ier = 0, lsr;
1604 unsigned long flags;
1607 * Must disable interrupts or else we risk racing with the interrupt
1610 if (is_real_interrupt(up->port.irq)) {
1611 ier = serial_in(up, UART_IER);
1612 serial_out(up, UART_IER, 0);
1615 iir = serial_in(up, UART_IIR);
1618 * This should be a safe test for anyone who doesn't trust the
1619 * IIR bits on their UART, but it's specifically designed for
1620 * the "Diva" UART used on the management processor on many HP
1621 * ia64 and parisc boxes.
1623 spin_lock_irqsave(&up->port.lock, flags);
1624 lsr = serial_in(up, UART_LSR);
1625 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1626 spin_unlock_irqrestore(&up->port.lock, flags);
1627 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
1628 (!uart_circ_empty(&up->port.info->xmit) || up->port.x_char) &&
1629 (lsr & UART_LSR_THRE)) {
1630 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1631 iir |= UART_IIR_THRI;
1634 if (!(iir & UART_IIR_NO_INT))
1635 serial8250_handle_port(up);
1637 if (is_real_interrupt(up->port.irq))
1638 serial_out(up, UART_IER, ier);
1640 /* Standard timer interval plus 0.2s to keep the port running */
1641 mod_timer(&up->timer,
1642 jiffies + poll_timeout(up->port.timeout) + HZ / 5);
1645 static unsigned int serial8250_tx_empty(struct uart_port *port)
1647 struct uart_8250_port *up = (struct uart_8250_port *)port;
1648 unsigned long flags;
1651 spin_lock_irqsave(&up->port.lock, flags);
1652 lsr = serial_in(up, UART_LSR);
1653 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1654 spin_unlock_irqrestore(&up->port.lock, flags);
1656 return lsr & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
1659 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1661 struct uart_8250_port *up = (struct uart_8250_port *)port;
1662 unsigned int status;
1665 status = check_modem_status(up);
1668 if (status & UART_MSR_DCD)
1670 if (status & UART_MSR_RI)
1672 if (status & UART_MSR_DSR)
1674 if (status & UART_MSR_CTS)
1679 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1681 struct uart_8250_port *up = (struct uart_8250_port *)port;
1682 unsigned char mcr = 0;
1684 if (mctrl & TIOCM_RTS)
1685 mcr |= UART_MCR_RTS;
1686 if (mctrl & TIOCM_DTR)
1687 mcr |= UART_MCR_DTR;
1688 if (mctrl & TIOCM_OUT1)
1689 mcr |= UART_MCR_OUT1;
1690 if (mctrl & TIOCM_OUT2)
1691 mcr |= UART_MCR_OUT2;
1692 if (mctrl & TIOCM_LOOP)
1693 mcr |= UART_MCR_LOOP;
1695 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1697 serial_out(up, UART_MCR, mcr);
1700 static void serial8250_break_ctl(struct uart_port *port, int break_state)
1702 struct uart_8250_port *up = (struct uart_8250_port *)port;
1703 unsigned long flags;
1705 spin_lock_irqsave(&up->port.lock, flags);
1706 if (break_state == -1)
1707 up->lcr |= UART_LCR_SBC;
1709 up->lcr &= ~UART_LCR_SBC;
1710 serial_out(up, UART_LCR, up->lcr);
1711 spin_unlock_irqrestore(&up->port.lock, flags);
1714 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1717 * Wait for transmitter & holding register to empty
1719 static inline void wait_for_xmitr(struct uart_8250_port *up, int bits)
1721 unsigned int status, tmout = 10000;
1723 /* Wait up to 10ms for the character(s) to be sent. */
1725 status = serial_in(up, UART_LSR);
1727 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
1732 } while ((status & bits) != bits);
1734 /* Wait up to 1s for flow control if necessary */
1735 if (up->port.flags & UPF_CONS_FLOW) {
1737 for (tmout = 1000000; tmout; tmout--) {
1738 unsigned int msr = serial_in(up, UART_MSR);
1739 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1740 if (msr & UART_MSR_CTS)
1743 touch_nmi_watchdog();
1748 #ifdef CONFIG_CONSOLE_POLL
1750 * Console polling routines for writing and reading from the uart while
1751 * in an interrupt or debug context.
1754 static int serial8250_get_poll_char(struct uart_port *port)
1756 struct uart_8250_port *up = (struct uart_8250_port *)port;
1757 unsigned char lsr = serial_inp(up, UART_LSR);
1759 while (!(lsr & UART_LSR_DR))
1760 lsr = serial_inp(up, UART_LSR);
1762 return serial_inp(up, UART_RX);
1766 static void serial8250_put_poll_char(struct uart_port *port,
1770 struct uart_8250_port *up = (struct uart_8250_port *)port;
1773 * First save the IER then disable the interrupts
1775 ier = serial_in(up, UART_IER);
1776 if (up->capabilities & UART_CAP_UUE)
1777 serial_out(up, UART_IER, UART_IER_UUE);
1779 serial_out(up, UART_IER, 0);
1781 wait_for_xmitr(up, BOTH_EMPTY);
1783 * Send the character out.
1784 * If a LF, also do CR...
1786 serial_out(up, UART_TX, c);
1788 wait_for_xmitr(up, BOTH_EMPTY);
1789 serial_out(up, UART_TX, 13);
1793 * Finally, wait for transmitter to become empty
1794 * and restore the IER
1796 wait_for_xmitr(up, BOTH_EMPTY);
1797 serial_out(up, UART_IER, ier);
1800 #endif /* CONFIG_CONSOLE_POLL */
1802 static int serial8250_startup(struct uart_port *port)
1804 struct uart_8250_port *up = (struct uart_8250_port *)port;
1805 unsigned long flags;
1806 unsigned char lsr, iir;
1809 up->capabilities = uart_config[up->port.type].flags;
1812 if (up->port.type == PORT_16C950) {
1813 /* Wake up and initialize UART */
1815 serial_outp(up, UART_LCR, 0xBF);
1816 serial_outp(up, UART_EFR, UART_EFR_ECB);
1817 serial_outp(up, UART_IER, 0);
1818 serial_outp(up, UART_LCR, 0);
1819 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1820 serial_outp(up, UART_LCR, 0xBF);
1821 serial_outp(up, UART_EFR, UART_EFR_ECB);
1822 serial_outp(up, UART_LCR, 0);
1825 #ifdef CONFIG_SERIAL_8250_RSA
1827 * If this is an RSA port, see if we can kick it up to the
1828 * higher speed clock.
1834 * Clear the FIFO buffers and disable them.
1835 * (they will be reenabled in set_termios())
1837 serial8250_clear_fifos(up);
1840 * Clear the interrupt registers.
1842 (void) serial_inp(up, UART_LSR);
1843 (void) serial_inp(up, UART_RX);
1844 (void) serial_inp(up, UART_IIR);
1845 (void) serial_inp(up, UART_MSR);
1848 * At this point, there's no way the LSR could still be 0xff;
1849 * if it is, then bail out, because there's likely no UART
1852 if (!(up->port.flags & UPF_BUGGY_UART) &&
1853 (serial_inp(up, UART_LSR) == 0xff)) {
1854 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
1859 * For a XR16C850, we need to set the trigger levels
1861 if (up->port.type == PORT_16850) {
1864 serial_outp(up, UART_LCR, 0xbf);
1866 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
1867 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
1868 serial_outp(up, UART_TRG, UART_TRG_96);
1869 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
1870 serial_outp(up, UART_TRG, UART_TRG_96);
1872 serial_outp(up, UART_LCR, 0);
1875 if (is_real_interrupt(up->port.irq)) {
1878 * Test for UARTs that do not reassert THRE when the
1879 * transmitter is idle and the interrupt has already
1880 * been cleared. Real 16550s should always reassert
1881 * this interrupt whenever the transmitter is idle and
1882 * the interrupt is enabled. Delays are necessary to
1883 * allow register changes to become visible.
1885 spin_lock_irqsave(&up->port.lock, flags);
1887 wait_for_xmitr(up, UART_LSR_THRE);
1888 serial_out_sync(up, UART_IER, UART_IER_THRI);
1889 udelay(1); /* allow THRE to set */
1890 iir1 = serial_in(up, UART_IIR);
1891 serial_out(up, UART_IER, 0);
1892 serial_out_sync(up, UART_IER, UART_IER_THRI);
1893 udelay(1); /* allow a working UART time to re-assert THRE */
1894 iir = serial_in(up, UART_IIR);
1895 serial_out(up, UART_IER, 0);
1897 spin_unlock_irqrestore(&up->port.lock, flags);
1900 * If the interrupt is not reasserted, setup a timer to
1901 * kick the UART on a regular basis.
1903 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
1904 pr_debug("ttyS%d - using backup timer\n", port->line);
1905 up->timer.function = serial8250_backup_timeout;
1906 up->timer.data = (unsigned long)up;
1907 mod_timer(&up->timer, jiffies +
1908 poll_timeout(up->port.timeout) + HZ / 5);
1913 * If the "interrupt" for this port doesn't correspond with any
1914 * hardware interrupt, we use a timer-based system. The original
1915 * driver used to do this with IRQ0.
1917 if (!is_real_interrupt(up->port.irq)) {
1918 up->timer.data = (unsigned long)up;
1919 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
1921 retval = serial_link_irq_chain(up);
1927 * Now, initialize the UART
1929 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
1931 spin_lock_irqsave(&up->port.lock, flags);
1932 if (up->port.flags & UPF_FOURPORT) {
1933 if (!is_real_interrupt(up->port.irq))
1934 up->port.mctrl |= TIOCM_OUT1;
1937 * Most PC uarts need OUT2 raised to enable interrupts.
1939 if (is_real_interrupt(up->port.irq))
1940 up->port.mctrl |= TIOCM_OUT2;
1942 serial8250_set_mctrl(&up->port, up->port.mctrl);
1945 * Do a quick test to see if we receive an
1946 * interrupt when we enable the TX irq.
1948 serial_outp(up, UART_IER, UART_IER_THRI);
1949 lsr = serial_in(up, UART_LSR);
1950 iir = serial_in(up, UART_IIR);
1951 serial_outp(up, UART_IER, 0);
1953 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
1954 if (!(up->bugs & UART_BUG_TXEN)) {
1955 up->bugs |= UART_BUG_TXEN;
1956 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
1960 up->bugs &= ~UART_BUG_TXEN;
1963 spin_unlock_irqrestore(&up->port.lock, flags);
1966 * Clear the interrupt registers again for luck, and clear the
1967 * saved flags to avoid getting false values from polling
1968 * routines or the previous session.
1970 serial_inp(up, UART_LSR);
1971 serial_inp(up, UART_RX);
1972 serial_inp(up, UART_IIR);
1973 serial_inp(up, UART_MSR);
1974 up->lsr_saved_flags = 0;
1975 up->msr_saved_flags = 0;
1978 * Finally, enable interrupts. Note: Modem status interrupts
1979 * are set via set_termios(), which will be occurring imminently
1980 * anyway, so we don't enable them here.
1982 up->ier = UART_IER_RLSI | UART_IER_RDI;
1983 serial_outp(up, UART_IER, up->ier);
1985 if (up->port.flags & UPF_FOURPORT) {
1988 * Enable interrupts on the AST Fourport board
1990 icp = (up->port.iobase & 0xfe0) | 0x01f;
1998 static void serial8250_shutdown(struct uart_port *port)
2000 struct uart_8250_port *up = (struct uart_8250_port *)port;
2001 unsigned long flags;
2004 * Disable interrupts from this port
2007 serial_outp(up, UART_IER, 0);
2009 spin_lock_irqsave(&up->port.lock, flags);
2010 if (up->port.flags & UPF_FOURPORT) {
2011 /* reset interrupts on the AST Fourport board */
2012 inb((up->port.iobase & 0xfe0) | 0x1f);
2013 up->port.mctrl |= TIOCM_OUT1;
2015 up->port.mctrl &= ~TIOCM_OUT2;
2017 serial8250_set_mctrl(&up->port, up->port.mctrl);
2018 spin_unlock_irqrestore(&up->port.lock, flags);
2021 * Disable break condition and FIFOs
2023 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
2024 serial8250_clear_fifos(up);
2026 #ifdef CONFIG_SERIAL_8250_RSA
2028 * Reset the RSA board back to 115kbps compat mode.
2034 * Read data port to reset things, and then unlink from
2037 (void) serial_in(up, UART_RX);
2039 del_timer_sync(&up->timer);
2040 up->timer.function = serial8250_timeout;
2041 if (is_real_interrupt(up->port.irq))
2042 serial_unlink_irq_chain(up);
2045 static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2050 * Handle magic divisors for baud rates above baud_base on
2051 * SMSC SuperIO chips.
2053 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2054 baud == (port->uartclk/4))
2056 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2057 baud == (port->uartclk/8))
2060 quot = uart_get_divisor(port, baud);
2066 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2067 struct ktermios *old)
2069 struct uart_8250_port *up = (struct uart_8250_port *)port;
2070 unsigned char cval, fcr = 0;
2071 unsigned long flags;
2072 unsigned int baud, quot;
2074 switch (termios->c_cflag & CSIZE) {
2076 cval = UART_LCR_WLEN5;
2079 cval = UART_LCR_WLEN6;
2082 cval = UART_LCR_WLEN7;
2086 cval = UART_LCR_WLEN8;
2090 if (termios->c_cflag & CSTOPB)
2091 cval |= UART_LCR_STOP;
2092 if (termios->c_cflag & PARENB)
2093 cval |= UART_LCR_PARITY;
2094 if (!(termios->c_cflag & PARODD))
2095 cval |= UART_LCR_EPAR;
2097 if (termios->c_cflag & CMSPAR)
2098 cval |= UART_LCR_SPAR;
2102 * Ask the core to calculate the divisor for us.
2104 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
2105 quot = serial8250_get_divisor(port, baud);
2108 * Oxford Semi 952 rev B workaround
2110 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2113 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
2115 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2117 fcr = uart_config[up->port.type].fcr;
2121 * MCR-based auto flow control. When AFE is enabled, RTS will be
2122 * deasserted when the receive FIFO contains more characters than
2123 * the trigger, or the MCR RTS bit is cleared. In the case where
2124 * the remote UART is not using CTS auto flow control, we must
2125 * have sufficient FIFO entries for the latency of the remote
2126 * UART to respond. IOW, at least 32 bytes of FIFO.
2128 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
2129 up->mcr &= ~UART_MCR_AFE;
2130 if (termios->c_cflag & CRTSCTS)
2131 up->mcr |= UART_MCR_AFE;
2135 * Ok, we're now changing the port state. Do it with
2136 * interrupts disabled.
2138 spin_lock_irqsave(&up->port.lock, flags);
2141 * Update the per-port timeout.
2143 uart_update_timeout(port, termios->c_cflag, baud);
2145 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2146 if (termios->c_iflag & INPCK)
2147 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2148 if (termios->c_iflag & (BRKINT | PARMRK))
2149 up->port.read_status_mask |= UART_LSR_BI;
2152 * Characteres to ignore
2154 up->port.ignore_status_mask = 0;
2155 if (termios->c_iflag & IGNPAR)
2156 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2157 if (termios->c_iflag & IGNBRK) {
2158 up->port.ignore_status_mask |= UART_LSR_BI;
2160 * If we're ignoring parity and break indicators,
2161 * ignore overruns too (for real raw support).
2163 if (termios->c_iflag & IGNPAR)
2164 up->port.ignore_status_mask |= UART_LSR_OE;
2168 * ignore all characters if CREAD is not set
2170 if ((termios->c_cflag & CREAD) == 0)
2171 up->port.ignore_status_mask |= UART_LSR_DR;
2174 * CTS flow control flag and modem status interrupts
2176 up->ier &= ~UART_IER_MSI;
2177 if (!(up->bugs & UART_BUG_NOMSR) &&
2178 UART_ENABLE_MS(&up->port, termios->c_cflag))
2179 up->ier |= UART_IER_MSI;
2180 if (up->capabilities & UART_CAP_UUE)
2181 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
2183 serial_out(up, UART_IER, up->ier);
2185 if (up->capabilities & UART_CAP_EFR) {
2186 unsigned char efr = 0;
2188 * TI16C752/Startech hardware flow control. FIXME:
2189 * - TI16C752 requires control thresholds to be set.
2190 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2192 if (termios->c_cflag & CRTSCTS)
2193 efr |= UART_EFR_CTS;
2195 serial_outp(up, UART_LCR, 0xBF);
2196 serial_outp(up, UART_EFR, efr);
2199 #ifdef CONFIG_ARCH_OMAP15XX
2200 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2201 if (cpu_is_omap1510() && is_omap_port((unsigned int)up->port.membase)) {
2202 if (baud == 115200) {
2204 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2206 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2210 if (up->capabilities & UART_NATSEMI) {
2211 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2212 serial_outp(up, UART_LCR, 0xe0);
2214 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2217 serial_dl_write(up, quot);
2220 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2221 * is written without DLAB set, this mode will be disabled.
2223 if (up->port.type == PORT_16750)
2224 serial_outp(up, UART_FCR, fcr);
2226 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2227 up->lcr = cval; /* Save LCR */
2228 if (up->port.type != PORT_16750) {
2229 if (fcr & UART_FCR_ENABLE_FIFO) {
2230 /* emulated UARTs (Lucent Venus 167x) need two steps */
2231 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2234 /* Note that we need to set ECB to access write water mark
2235 * bits. First allow FCR tx fifo write, then set fcr with
2236 * possible TX fifo settings. */
2237 if (uart_config[up->port.type].flags & UART_CAP_EFR) {
2238 serial_outp(up, UART_LCR, 0xbf); /* Access EFR */
2239 serial_outp(up, UART_EFR, UART_EFR_ECB);
2240 serial_outp(up, UART_LCR, 0x0); /* Access FCR */
2241 serial_outp(up, UART_FCR, fcr);
2242 serial_outp(up, UART_LCR, 0xbf); /* Access EFR */
2243 serial_outp(up, UART_EFR, 0);
2244 serial_outp(up, UART_LCR, cval); /* Access FCR */
2246 serial_outp(up, UART_FCR, fcr); /* set fcr */
2248 serial8250_set_mctrl(&up->port, up->port.mctrl);
2249 spin_unlock_irqrestore(&up->port.lock, flags);
2250 /* Don't rewrite B0 */
2251 if (tty_termios_baud_rate(termios))
2252 tty_termios_encode_baud_rate(termios, baud, baud);
2256 serial8250_pm(struct uart_port *port, unsigned int state,
2257 unsigned int oldstate)
2259 struct uart_8250_port *p = (struct uart_8250_port *)port;
2261 serial8250_set_sleep(p, state != 0);
2264 p->pm(port, state, oldstate);
2268 * Resource handling.
2270 static int serial8250_request_std_resource(struct uart_8250_port *up)
2272 unsigned int size = 8 << up->port.regshift;
2275 #ifdef CONFIG_ARCH_OMAP
2276 if (is_omap_port((unsigned int)up->port.membase))
2277 size = 0x16 << up->port.regshift;
2280 switch (up->port.iotype) {
2288 if (!up->port.mapbase)
2291 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2296 if (up->port.flags & UPF_IOREMAP) {
2297 up->port.membase = ioremap_nocache(up->port.mapbase,
2299 if (!up->port.membase) {
2300 release_mem_region(up->port.mapbase, size);
2308 if (!request_region(up->port.iobase, size, "serial"))
2315 static void serial8250_release_std_resource(struct uart_8250_port *up)
2317 unsigned int size = 8 << up->port.regshift;
2319 switch (up->port.iotype) {
2327 if (!up->port.mapbase)
2330 if (up->port.flags & UPF_IOREMAP) {
2331 iounmap(up->port.membase);
2332 up->port.membase = NULL;
2335 release_mem_region(up->port.mapbase, size);
2340 release_region(up->port.iobase, size);
2345 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2347 unsigned long start = UART_RSA_BASE << up->port.regshift;
2348 unsigned int size = 8 << up->port.regshift;
2351 switch (up->port.iotype) {
2354 start += up->port.iobase;
2355 if (request_region(start, size, "serial-rsa"))
2365 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2367 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2368 unsigned int size = 8 << up->port.regshift;
2370 switch (up->port.iotype) {
2373 release_region(up->port.iobase + offset, size);
2378 static void serial8250_release_port(struct uart_port *port)
2380 struct uart_8250_port *up = (struct uart_8250_port *)port;
2382 serial8250_release_std_resource(up);
2383 if (up->port.type == PORT_RSA)
2384 serial8250_release_rsa_resource(up);
2387 static int serial8250_request_port(struct uart_port *port)
2389 struct uart_8250_port *up = (struct uart_8250_port *)port;
2392 ret = serial8250_request_std_resource(up);
2393 if (ret == 0 && up->port.type == PORT_RSA) {
2394 ret = serial8250_request_rsa_resource(up);
2396 serial8250_release_std_resource(up);
2402 static void serial8250_config_port(struct uart_port *port, int flags)
2404 struct uart_8250_port *up = (struct uart_8250_port *)port;
2405 int probeflags = PROBE_ANY;
2409 * Find the region that we can probe for. This in turn
2410 * tells us whether we can probe for the type of port.
2412 ret = serial8250_request_std_resource(up);
2416 ret = serial8250_request_rsa_resource(up);
2418 probeflags &= ~PROBE_RSA;
2420 if (flags & UART_CONFIG_TYPE)
2421 autoconfig(up, probeflags);
2422 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2425 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2426 serial8250_release_rsa_resource(up);
2427 if (up->port.type == PORT_UNKNOWN)
2428 serial8250_release_std_resource(up);
2432 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2434 if (ser->irq >= NR_IRQS || ser->irq < 0 ||
2435 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2436 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2437 ser->type == PORT_STARTECH)
2443 serial8250_type(struct uart_port *port)
2445 int type = port->type;
2447 if (type >= ARRAY_SIZE(uart_config))
2449 return uart_config[type].name;
2452 static struct uart_ops serial8250_pops = {
2453 .tx_empty = serial8250_tx_empty,
2454 .set_mctrl = serial8250_set_mctrl,
2455 .get_mctrl = serial8250_get_mctrl,
2456 .stop_tx = serial8250_stop_tx,
2457 .start_tx = serial8250_start_tx,
2458 .stop_rx = serial8250_stop_rx,
2459 .enable_ms = serial8250_enable_ms,
2460 .break_ctl = serial8250_break_ctl,
2461 .startup = serial8250_startup,
2462 .shutdown = serial8250_shutdown,
2463 .set_termios = serial8250_set_termios,
2464 .pm = serial8250_pm,
2465 .type = serial8250_type,
2466 .release_port = serial8250_release_port,
2467 .request_port = serial8250_request_port,
2468 .config_port = serial8250_config_port,
2469 .verify_port = serial8250_verify_port,
2470 #ifdef CONFIG_CONSOLE_POLL
2471 .poll_get_char = serial8250_get_poll_char,
2472 .poll_put_char = serial8250_put_poll_char,
2476 static struct uart_8250_port serial8250_ports[UART_NR];
2478 static void __init serial8250_isa_init_ports(void)
2480 struct uart_8250_port *up;
2481 static int first = 1;
2488 for (i = 0; i < nr_uarts; i++) {
2489 struct uart_8250_port *up = &serial8250_ports[i];
2492 spin_lock_init(&up->port.lock);
2494 init_timer(&up->timer);
2495 up->timer.function = serial8250_timeout;
2498 * ALPHA_KLUDGE_MCR needs to be killed.
2500 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2501 up->mcr_force = ALPHA_KLUDGE_MCR;
2503 up->port.ops = &serial8250_pops;
2506 for (i = 0, up = serial8250_ports;
2507 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
2509 up->port.iobase = old_serial_port[i].port;
2510 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
2511 up->port.uartclk = old_serial_port[i].baud_base * 16;
2512 up->port.flags = old_serial_port[i].flags;
2513 up->port.hub6 = old_serial_port[i].hub6;
2514 up->port.membase = old_serial_port[i].iomem_base;
2515 up->port.iotype = old_serial_port[i].io_type;
2516 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2518 up->port.flags |= UPF_SHARE_IRQ;
2523 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2527 serial8250_isa_init_ports();
2529 for (i = 0; i < nr_uarts; i++) {
2530 struct uart_8250_port *up = &serial8250_ports[i];
2533 uart_add_one_port(drv, &up->port);
2537 #ifdef CONFIG_SERIAL_8250_CONSOLE
2539 static void serial8250_console_putchar(struct uart_port *port, int ch)
2541 struct uart_8250_port *up = (struct uart_8250_port *)port;
2543 wait_for_xmitr(up, UART_LSR_THRE);
2544 serial_out(up, UART_TX, ch);
2548 * Print a string to the serial port trying not to disturb
2549 * any possible real use of the port...
2551 * The console_lock must be held when we get here.
2554 serial8250_console_write(struct console *co, const char *s, unsigned int count)
2556 struct uart_8250_port *up = &serial8250_ports[co->index];
2557 unsigned long flags;
2561 touch_nmi_watchdog();
2563 local_irq_save(flags);
2564 if (up->port.sysrq) {
2565 /* serial8250_handle_port() already took the lock */
2567 } else if (oops_in_progress) {
2568 locked = spin_trylock(&up->port.lock);
2570 spin_lock(&up->port.lock);
2573 * First save the IER then disable the interrupts
2575 ier = serial_in(up, UART_IER);
2577 if (up->capabilities & UART_CAP_UUE)
2578 serial_out(up, UART_IER, UART_IER_UUE);
2580 serial_out(up, UART_IER, 0);
2582 uart_console_write(&up->port, s, count, serial8250_console_putchar);
2585 * Finally, wait for transmitter to become empty
2586 * and restore the IER
2588 wait_for_xmitr(up, BOTH_EMPTY);
2589 serial_out(up, UART_IER, ier);
2592 * The receive handling will happen properly because the
2593 * receive ready bit will still be set; it is not cleared
2594 * on read. However, modem control will not, we must
2595 * call it if we have saved something in the saved flags
2596 * while processing with interrupts off.
2598 if (up->msr_saved_flags)
2599 check_modem_status(up);
2602 spin_unlock(&up->port.lock);
2603 local_irq_restore(flags);
2606 static int __init serial8250_console_setup(struct console *co, char *options)
2608 struct uart_port *port;
2615 * Check whether an invalid uart number has been specified, and
2616 * if so, search for the first available port that does have
2619 if (co->index >= nr_uarts)
2621 port = &serial8250_ports[co->index].port;
2622 if (!port->iobase && !port->membase)
2626 uart_parse_options(options, &baud, &parity, &bits, &flow);
2628 return uart_set_options(port, co, baud, parity, bits, flow);
2631 static int serial8250_console_early_setup(void)
2633 return serial8250_find_port_for_earlycon();
2636 static struct uart_driver serial8250_reg;
2637 static struct console serial8250_console = {
2639 .write = serial8250_console_write,
2640 .device = uart_console_device,
2641 .setup = serial8250_console_setup,
2642 .early_setup = serial8250_console_early_setup,
2643 .flags = CON_PRINTBUFFER,
2645 .data = &serial8250_reg,
2648 static int __init serial8250_console_init(void)
2650 serial8250_isa_init_ports();
2651 register_console(&serial8250_console);
2654 console_initcall(serial8250_console_init);
2656 int serial8250_find_port(struct uart_port *p)
2659 struct uart_port *port;
2661 for (line = 0; line < nr_uarts; line++) {
2662 port = &serial8250_ports[line].port;
2663 if (uart_match_port(p, port))
2669 #define SERIAL8250_CONSOLE &serial8250_console
2671 #define SERIAL8250_CONSOLE NULL
2674 static struct uart_driver serial8250_reg = {
2675 .owner = THIS_MODULE,
2676 .driver_name = "serial",
2681 .cons = SERIAL8250_CONSOLE,
2685 * early_serial_setup - early registration for 8250 ports
2687 * Setup an 8250 port structure prior to console initialisation. Use
2688 * after console initialisation will cause undefined behaviour.
2690 int __init early_serial_setup(struct uart_port *port)
2692 if (port->line >= ARRAY_SIZE(serial8250_ports))
2695 serial8250_isa_init_ports();
2696 serial8250_ports[port->line].port = *port;
2697 serial8250_ports[port->line].port.ops = &serial8250_pops;
2702 * serial8250_suspend_port - suspend one serial port
2703 * @line: serial line number
2705 * Suspend one serial port.
2707 void serial8250_suspend_port(int line)
2709 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2713 * serial8250_resume_port - resume one serial port
2714 * @line: serial line number
2716 * Resume one serial port.
2718 void serial8250_resume_port(int line)
2720 struct uart_8250_port *up = &serial8250_ports[line];
2722 if (up->capabilities & UART_NATSEMI) {
2725 /* Ensure it's still in high speed mode */
2726 serial_outp(up, UART_LCR, 0xE0);
2728 tmp = serial_in(up, 0x04); /* EXCR2 */
2729 tmp &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
2730 tmp |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
2731 serial_outp(up, 0x04, tmp);
2733 serial_outp(up, UART_LCR, 0);
2735 uart_resume_port(&serial8250_reg, &up->port);
2739 * Register a set of serial devices attached to a platform device. The
2740 * list is terminated with a zero flags entry, which means we expect
2741 * all entries to have at least UPF_BOOT_AUTOCONF set.
2743 static int __devinit serial8250_probe(struct platform_device *dev)
2745 struct plat_serial8250_port *p = dev->dev.platform_data;
2746 struct uart_port port;
2749 memset(&port, 0, sizeof(struct uart_port));
2751 for (i = 0; p && p->flags != 0; p++, i++) {
2752 port.iobase = p->iobase;
2753 port.membase = p->membase;
2755 port.uartclk = p->uartclk;
2756 port.regshift = p->regshift;
2757 port.iotype = p->iotype;
2758 port.flags = p->flags;
2759 port.mapbase = p->mapbase;
2760 port.hub6 = p->hub6;
2761 port.private_data = p->private_data;
2762 port.dev = &dev->dev;
2764 port.flags |= UPF_SHARE_IRQ;
2765 ret = serial8250_register_port(&port);
2767 dev_err(&dev->dev, "unable to register port at index %d "
2768 "(IO%lx MEM%llx IRQ%d): %d\n", i,
2769 p->iobase, (unsigned long long)p->mapbase,
2777 * Remove serial ports registered against a platform device.
2779 static int __devexit serial8250_remove(struct platform_device *dev)
2783 for (i = 0; i < nr_uarts; i++) {
2784 struct uart_8250_port *up = &serial8250_ports[i];
2786 if (up->port.dev == &dev->dev)
2787 serial8250_unregister_port(i);
2792 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
2796 for (i = 0; i < UART_NR; i++) {
2797 struct uart_8250_port *up = &serial8250_ports[i];
2799 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
2800 uart_suspend_port(&serial8250_reg, &up->port);
2806 static int serial8250_resume(struct platform_device *dev)
2810 for (i = 0; i < UART_NR; i++) {
2811 struct uart_8250_port *up = &serial8250_ports[i];
2813 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
2814 serial8250_resume_port(i);
2820 static struct platform_driver serial8250_isa_driver = {
2821 .probe = serial8250_probe,
2822 .remove = __devexit_p(serial8250_remove),
2823 .suspend = serial8250_suspend,
2824 .resume = serial8250_resume,
2826 .name = "serial8250",
2827 .owner = THIS_MODULE,
2832 * This "device" covers _all_ ISA 8250-compatible serial devices listed
2833 * in the table in include/asm/serial.h
2835 static struct platform_device *serial8250_isa_devs;
2838 * serial8250_register_port and serial8250_unregister_port allows for
2839 * 16x50 serial ports to be configured at run-time, to support PCMCIA
2840 * modems and PCI multiport cards.
2842 static DEFINE_MUTEX(serial_mutex);
2844 static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
2849 * First, find a port entry which matches.
2851 for (i = 0; i < nr_uarts; i++)
2852 if (uart_match_port(&serial8250_ports[i].port, port))
2853 return &serial8250_ports[i];
2856 * We didn't find a matching entry, so look for the first
2857 * free entry. We look for one which hasn't been previously
2858 * used (indicated by zero iobase).
2860 for (i = 0; i < nr_uarts; i++)
2861 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
2862 serial8250_ports[i].port.iobase == 0)
2863 return &serial8250_ports[i];
2866 * That also failed. Last resort is to find any entry which
2867 * doesn't have a real port associated with it.
2869 for (i = 0; i < nr_uarts; i++)
2870 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
2871 return &serial8250_ports[i];
2877 * serial8250_register_port - register a serial port
2878 * @port: serial port template
2880 * Configure the serial port specified by the request. If the
2881 * port exists and is in use, it is hung up and unregistered
2884 * The port is then probed and if necessary the IRQ is autodetected
2885 * If this fails an error is returned.
2887 * On success the port is ready to use and the line number is returned.
2889 int serial8250_register_port(struct uart_port *port)
2891 struct uart_8250_port *uart;
2894 if (port->uartclk == 0)
2897 mutex_lock(&serial_mutex);
2899 uart = serial8250_find_match_or_unused(port);
2901 uart_remove_one_port(&serial8250_reg, &uart->port);
2903 uart->port.iobase = port->iobase;
2904 uart->port.membase = port->membase;
2905 uart->port.irq = port->irq;
2906 uart->port.uartclk = port->uartclk;
2907 uart->port.fifosize = port->fifosize;
2908 uart->port.regshift = port->regshift;
2909 uart->port.iotype = port->iotype;
2910 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
2911 uart->port.mapbase = port->mapbase;
2912 uart->port.private_data = port->private_data;
2914 uart->port.dev = port->dev;
2916 ret = uart_add_one_port(&serial8250_reg, &uart->port);
2918 ret = uart->port.line;
2920 mutex_unlock(&serial_mutex);
2924 EXPORT_SYMBOL(serial8250_register_port);
2927 * serial8250_unregister_port - remove a 16x50 serial port at runtime
2928 * @line: serial line number
2930 * Remove one serial port. This may not be called from interrupt
2931 * context. We hand the port back to the our control.
2933 void serial8250_unregister_port(int line)
2935 struct uart_8250_port *uart = &serial8250_ports[line];
2937 mutex_lock(&serial_mutex);
2938 uart_remove_one_port(&serial8250_reg, &uart->port);
2939 if (serial8250_isa_devs) {
2940 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
2941 uart->port.type = PORT_UNKNOWN;
2942 uart->port.dev = &serial8250_isa_devs->dev;
2943 uart_add_one_port(&serial8250_reg, &uart->port);
2945 uart->port.dev = NULL;
2947 mutex_unlock(&serial_mutex);
2949 EXPORT_SYMBOL(serial8250_unregister_port);
2951 static int __init serial8250_init(void)
2955 if (nr_uarts > UART_NR)
2958 printk(KERN_INFO "Serial: 8250/16550 driver $Revision: 1.90 $ "
2959 "%d ports, IRQ sharing %sabled\n", nr_uarts,
2960 share_irqs ? "en" : "dis");
2962 for (i = 0; i < NR_IRQS; i++)
2963 spin_lock_init(&irq_lists[i].lock);
2965 ret = uart_register_driver(&serial8250_reg);
2969 serial8250_isa_devs = platform_device_alloc("serial8250",
2970 PLAT8250_DEV_LEGACY);
2971 if (!serial8250_isa_devs) {
2973 goto unreg_uart_drv;
2976 ret = platform_device_add(serial8250_isa_devs);
2980 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
2982 ret = platform_driver_register(&serial8250_isa_driver);
2986 platform_device_del(serial8250_isa_devs);
2988 platform_device_put(serial8250_isa_devs);
2990 uart_unregister_driver(&serial8250_reg);
2995 static void __exit serial8250_exit(void)
2997 struct platform_device *isa_dev = serial8250_isa_devs;
3000 * This tells serial8250_unregister_port() not to re-register
3001 * the ports (thereby making serial8250_isa_driver permanently
3004 serial8250_isa_devs = NULL;
3006 platform_driver_unregister(&serial8250_isa_driver);
3007 platform_device_unregister(isa_dev);
3009 uart_unregister_driver(&serial8250_reg);
3012 module_init(serial8250_init);
3013 module_exit(serial8250_exit);
3015 EXPORT_SYMBOL(serial8250_suspend_port);
3016 EXPORT_SYMBOL(serial8250_resume_port);
3018 MODULE_LICENSE("GPL");
3019 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver $Revision: 1.90 $");
3021 module_param(share_irqs, uint, 0644);
3022 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3025 module_param(nr_uarts, uint, 0644);
3026 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3028 #ifdef CONFIG_SERIAL_8250_RSA
3029 module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3030 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3032 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);