19 #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
23 #include <linux/module.h>
31 #include <linux/tty.h>
35 #include <linux/serial_core.h>
36 #include <linux/serial.h>
40 #include <linux/slab.h>
57 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
63 return (serial8250_reg.minor - 64) + port->
line;
66 static unsigned int skip_txen_test;
72 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
74 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
78 #define DEBUG_INTR(fmt...) printk(fmt)
80 #define DEBUG_INTR(fmt...) do { } while (0)
83 #define PASS_LIMIT 512
85 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
88 #ifdef CONFIG_SERIAL_8250_DETECT_IRQ
89 #define CONFIG_SERIAL_DETECT_IRQ 1
91 #ifdef CONFIG_SERIAL_8250_MANY_PORTS
92 #define CONFIG_SERIAL_MANY_PORTS 1
101 #include <asm/serial.h>
107 #ifndef SERIAL_PORT_DFNS
108 #define SERIAL_PORT_DFNS
115 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
117 #ifdef CONFIG_SERIAL_8250_RSA
119 #define PORT_RSA_MAX 4
120 static unsigned long probe_rsa[PORT_RSA_MAX];
121 static unsigned int probe_rsa_count;
131 #define NR_IRQ_HASH 32
199 .name =
"16C950/954",
307 serial_out(up,
UART_DLL, value & 0xff);
308 serial_out(up,
UART_DLM, value >> 8 & 0xff);
311 #ifdef CONFIG_MIPS_ALCHEMY
314 static const u8 au_io_in_map[] = {
324 static const u8 au_io_out_map[] = {
357 #ifdef CONFIG_SERIAL_8250_RM9K
457 static int serial8250_default_handle_irq(
struct uart_port *port);
459 static void set_io_from_upio(
struct uart_port *
p)
464 up->
dl_read = default_serial_dl_read;
465 up->
dl_write = default_serial_dl_write;
483 #ifdef CONFIG_SERIAL_8250_RM9K
487 up->
dl_read = rm9k_serial_dl_read;
488 up->
dl_write = rm9k_serial_dl_write;
492 #ifdef CONFIG_MIPS_ALCHEMY
496 up->
dl_read = au_serial_dl_read;
508 p->
handle_irq = serial8250_default_handle_irq;
529 static void serial_icr_write(
struct uart_8250_port *up,
int offset,
int value)
535 static unsigned int serial_icr_read(
struct uart_8250_port *up,
int offset)
564 serial8250_clear_fifos(p);
565 fcr = uart_config[p->
port.type].
fcr;
575 static void serial8250_set_sleep(
struct uart_8250_port *p,
int sleep)
592 #ifdef CONFIG_SERIAL_8250_RSA
621 spin_lock_irq(&up->
port.lock);
623 spin_unlock_irq(&up->
port.lock);
643 spin_lock_irq(&up->
port.lock);
649 serial_out(up,
UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
656 spin_unlock_irq(&up->
port.lock);
667 unsigned char old_fcr, old_mcr, old_lcr;
668 unsigned short old_dl;
679 old_dl = serial_dl_read(up);
680 serial_dl_write(up, 0x0001);
682 for (count = 0; count < 256; count++)
683 serial_out(up,
UART_TX, count);
686 (count < 256); count++)
691 serial_dl_write(up, old_dl);
702 static unsigned int autoconfig_read_divisor_id(
struct uart_8250_port *p)
704 unsigned char old_dll, old_dlm, old_lcr;
765 id1 = serial_icr_read(up,
UART_ID1);
766 id2 = serial_icr_read(up,
UART_ID2);
767 id3 = serial_icr_read(up,
UART_ID3);
768 rev = serial_icr_read(up,
UART_REV);
772 if (id1 == 0x16 && id2 == 0xC9 &&
773 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
781 if (id3 == 0x52 && rev == 0x01)
794 id1 = autoconfig_read_divisor_id(up);
798 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
812 if (size_fifo(up) == 64)
836 if (status1 == 0xa5 && status2 == 0x5a)
847 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
853 static inline int ns16550a_goto_highspeed(
struct uart_8250_port *up)
857 status = serial_in(up, 0x04);
858 #define PRESL(x) ((x) & 0x30)
859 if (
PRESL(status) == 0x10) {
865 serial_out(up, 0x04, status);
879 unsigned int iersave;
907 if (serial_in(up,
UART_EFR) == 0 && !broken_efr(up)) {
909 autoconfig_has_efr(up);
923 status2 = serial_in(up, 0x02);
927 serial_out(up,
UART_MCR, status1 ^ UART_MCR_LOOP);
929 status2 = serial_in(up, 0x02);
933 if ((status2 ^ status1) & UART_MCR_LOOP) {
938 quot = serial_dl_read(up);
941 if (ns16550a_goto_highspeed(up))
942 serial_dl_write(up, quot);
946 up->
port.uartclk = 921600*16;
961 status1 = serial_in(up,
UART_IIR) >> 5;
965 status2 = serial_in(up,
UART_IIR) >> 5;
971 if (status1 == 6 && status2 == 7) {
992 serial_out(up,
UART_IER, iersave | UART_IER_UUE);
993 if (serial_in(up,
UART_IER) & UART_IER_UUE) {
1037 static void autoconfig(
struct uart_8250_port *up,
unsigned int probeflags)
1040 unsigned char save_lcr, save_mcr;
1042 unsigned long flags;
1043 unsigned int old_capabilities;
1090 if (scratch2 != 0 || scratch3 != 0x0F) {
1094 spin_unlock_irqrestore(&port->
lock, flags);
1096 scratch2, scratch3);
1117 if (status1 != 0x90) {
1118 spin_unlock_irqrestore(&port->
lock, flags);
1143 autoconfig_8250(up);
1152 autoconfig_16550a(up);
1156 #ifdef CONFIG_SERIAL_8250_RSA
1163 for (i = 0 ; i < probe_rsa_count; ++
i) {
1164 if (probe_rsa[i] == port->
iobase && __enable_rsa(up)) {
1185 #ifdef CONFIG_SERIAL_8250_RSA
1190 serial8250_clear_fifos(up);
1198 spin_unlock_irqrestore(&port->
lock, flags);
1201 "ttyS%d: detected caps %08x should be %08x\n",
1202 serial_index(port), old_capabilities,
1213 unsigned char save_mcr, save_ier;
1214 unsigned char save_ICP = 0;
1215 unsigned int ICP = 0;
1220 ICP = (port->
iobase & 0xfe0) | 0x1f;
1221 save_ICP =
inb_p(ICP);
1257 port->
irq = (irq > 0) ? irq : 0;
1268 static void serial8250_stop_tx(
struct uart_port *port)
1284 static void serial8250_start_tx(
struct uart_port *port)
1313 static void serial8250_stop_rx(
struct uart_port *port)
1323 static void serial8250_enable_ms(
struct uart_port *port)
1379 if (uart_handle_break(port))
1393 if (lsr & UART_LSR_BI) {
1396 }
else if (lsr & UART_LSR_PE)
1398 else if (lsr & UART_LSR_FE)
1409 spin_unlock(&port->
lock);
1411 spin_lock(&port->
lock);
1428 if (uart_tx_stopped(port)) {
1429 serial8250_stop_tx(port);
1444 }
while (--count > 0);
1487 unsigned long flags;
1496 status = serial_port_in(port,
UART_LSR);
1506 spin_unlock_irqrestore(&port->
lock, flags);
1511 static int serial8250_default_handle_irq(
struct uart_port *port)
1513 unsigned int iir = serial_port_in(port,
UART_IIR);
1536 int pass_counter = 0, handled = 0;
1538 DEBUG_INTR(
"serial8250_interrupt(%d)...", irq);
1540 spin_lock(&i->
lock);
1553 }
else if (end ==
NULL)
1561 "serial8250: too much work for irq%d\n", irq);
1566 spin_unlock(&i->
lock);
1582 spin_lock_irq(&i->
lock);
1584 if (!list_empty(i->
head)) {
1592 spin_unlock_irq(&i->
lock);
1595 hlist_del(&i->
node);
1625 hlist_add_head(&i->
node, h);
1629 spin_lock_irq(&i->
lock);
1633 spin_unlock_irq(&i->
lock);
1637 INIT_LIST_HEAD(&up->
list);
1639 spin_unlock_irq(&i->
lock);
1640 irq_flags |= up->
port.irqflags;
1642 irq_flags,
"serial", i);
1644 serial_do_unlink(i, up);
1669 if (list_empty(i->
head))
1672 serial_do_unlink(i, up);
1682 static void serial8250_timeout(
unsigned long data)
1690 static void serial8250_backup_timeout(
unsigned long data)
1693 unsigned int iir,
ier = 0, lsr;
1694 unsigned long flags;
1724 if (!(iir & UART_IIR_NO_INT))
1730 spin_unlock_irqrestore(&up->
port.lock, flags);
1734 jiffies + uart_poll_timeout(&up->
port) +
HZ / 5);
1737 static unsigned int serial8250_tx_empty(
struct uart_port *port)
1741 unsigned long flags;
1745 lsr = serial_port_in(port,
UART_LSR);
1747 spin_unlock_irqrestore(&port->
lock, flags);
1752 static unsigned int serial8250_get_mctrl(
struct uart_port *port)
1773 static void serial8250_set_mctrl(
struct uart_port *port,
unsigned int mctrl)
1777 unsigned char mcr = 0;
1792 serial_port_out(port,
UART_MCR, mcr);
1795 static void serial8250_break_ctl(
struct uart_port *port,
int break_state)
1799 unsigned long flags;
1802 if (break_state == -1)
1807 spin_unlock_irqrestore(&port->
lock, flags);
1815 unsigned int status, tmout = 10000;
1823 if ((status & bits) == bits)
1833 for (tmout = 1000000; tmout; tmout--) {
1834 unsigned int msr = serial_in(up,
UART_MSR);
1844 #ifdef CONFIG_CONSOLE_POLL
1850 static int serial8250_get_poll_char(
struct uart_port *port)
1852 unsigned char lsr = serial_port_in(port,
UART_LSR);
1857 return serial_port_in(port,
UART_RX);
1861 static void serial8250_put_poll_char(
struct uart_port *port,
1871 ier = serial_port_in(port,
UART_IER);
1873 serial_port_out(port,
UART_IER, UART_IER_UUE);
1875 serial_port_out(port,
UART_IER, 0);
1882 serial_port_out(port,
UART_TX, c);
1885 serial_port_out(port,
UART_TX, 13);
1893 serial_port_out(port,
UART_IER, ier);
1898 static int serial8250_startup(
struct uart_port *port)
1902 unsigned long flags;
1903 unsigned char lsr,
iir;
1915 set_io_from_upio(port);
1922 serial_port_out(port,
UART_IER, 0);
1923 serial_port_out(port,
UART_LCR, 0);
1927 serial_port_out(port,
UART_LCR, 0);
1930 #ifdef CONFIG_SERIAL_8250_RSA
1942 serial8250_clear_fifos(up);
1948 serial_port_in(port,
UART_RX);
1958 (serial_port_in(port,
UART_LSR) == 0xff)) {
1960 serial_index(port));
1980 serial_port_out(port,
UART_LCR, 0);
2000 iir1 = serial_port_in(port,
UART_IIR);
2001 serial_port_out(port,
UART_IER, 0);
2004 iir = serial_port_in(port,
UART_IIR);
2005 serial_port_out(port,
UART_IER, 0);
2009 spin_unlock_irqrestore(&port->
lock, flags);
2016 if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2019 pr_debug(
"ttyS%d - using backup timer\n",
2020 serial_index(port));
2029 up->
timer.function = serial8250_backup_timeout;
2032 uart_poll_timeout(port) +
HZ / 5);
2044 retval = serial_link_irq_chain(up);
2065 serial8250_set_mctrl(port, port->
mctrl);
2079 goto dont_test_tx_en;
2086 lsr = serial_port_in(port,
UART_LSR);
2087 iir = serial_port_in(port,
UART_IIR);
2088 serial_port_out(port,
UART_IER, 0);
2093 pr_debug(
"ttyS%d - enabling bad tx status workarounds\n",
2094 serial_index(port));
2101 spin_unlock_irqrestore(&port->
lock, flags);
2109 serial_port_in(port,
UART_RX);
2128 icp = (port->
iobase & 0xfe0) | 0x01f;
2136 static void serial8250_shutdown(
struct uart_port *port)
2140 unsigned long flags;
2146 serial_port_out(port,
UART_IER, 0);
2154 port->
mctrl &= ~TIOCM_OUT2;
2156 serial8250_set_mctrl(port, port->
mctrl);
2157 spin_unlock_irqrestore(&port->
lock, flags);
2164 serial8250_clear_fifos(up);
2166 #ifdef CONFIG_SERIAL_8250_RSA
2177 serial_port_in(port,
UART_RX);
2180 up->
timer.function = serial8250_timeout;
2182 serial_unlink_irq_chain(up);
2185 static unsigned int serial8250_get_divisor(
struct uart_port *port,
unsigned int baud)
2211 unsigned char cval,
fcr = 0;
2212 unsigned long flags;
2213 unsigned int baud, quot;
2252 quot = serial8250_get_divisor(port, baud);
2261 fcr = uart_config[port->
type].
fcr;
2262 if (baud < 2400 || fifo_bug) {
2336 unsigned char efr = 0;
2349 serial_port_out(port,
UART_EFR, efr);
2352 #ifdef CONFIG_ARCH_OMAP1
2355 if (baud == 115200) {
2368 serial_port_out(port,
UART_LCR, 0xe0);
2372 serial_dl_write(up, quot);
2379 serial_port_out(port,
UART_FCR, fcr);
2381 serial_port_out(port,
UART_LCR, cval);
2386 serial_port_out(port,
UART_FCR, UART_FCR_ENABLE_FIFO);
2387 serial_port_out(port,
UART_FCR, fcr);
2389 serial8250_set_mctrl(port, port->
mctrl);
2390 spin_unlock_irqrestore(&port->
lock, flags);
2408 serial8250_set_ldisc(
struct uart_port *port,
int new)
2412 serial8250_enable_ms(port);
2419 unsigned int oldstate)
2424 serial8250_set_sleep(p, state != 0);
2430 unsigned int oldstate)
2433 port->
pm(port, state, oldstate);
2438 static unsigned int serial8250_port_size(
struct uart_8250_port *pt)
2442 #ifdef CONFIG_ARCH_OMAP1
2444 return 0x16 << pt->
port.regshift;
2446 return 8 << pt->
port.regshift;
2452 static int serial8250_request_std_resource(
struct uart_8250_port *up)
2454 unsigned int size = serial8250_port_size(up);
2489 static void serial8250_release_std_resource(
struct uart_8250_port *up)
2491 unsigned int size = serial8250_port_size(up);
2517 static int serial8250_request_rsa_resource(
struct uart_8250_port *up)
2520 unsigned int size = 8 << up->
port.regshift;
2538 static void serial8250_release_rsa_resource(
struct uart_8250_port *up)
2541 unsigned int size = 8 << up->
port.regshift;
2552 static void serial8250_release_port(
struct uart_port *port)
2557 serial8250_release_std_resource(up);
2559 serial8250_release_rsa_resource(up);
2562 static int serial8250_request_port(
struct uart_port *port)
2571 ret = serial8250_request_std_resource(up);
2573 ret = serial8250_request_rsa_resource(up);
2575 serial8250_release_std_resource(up);
2581 static void serial8250_config_port(
struct uart_port *port,
int flags)
2595 ret = serial8250_request_std_resource(up);
2599 ret = serial8250_request_rsa_resource(up);
2601 probeflags &= ~PROBE_RSA;
2604 set_io_from_upio(port);
2607 autoconfig(up, probeflags);
2617 serial8250_release_rsa_resource(up);
2619 serial8250_release_std_resource(up);
2643 static struct uart_ops serial8250_pops = {
2644 .tx_empty = serial8250_tx_empty,
2645 .set_mctrl = serial8250_set_mctrl,
2646 .get_mctrl = serial8250_get_mctrl,
2647 .stop_tx = serial8250_stop_tx,
2648 .start_tx = serial8250_start_tx,
2649 .stop_rx = serial8250_stop_rx,
2650 .enable_ms = serial8250_enable_ms,
2651 .break_ctl = serial8250_break_ctl,
2652 .startup = serial8250_startup,
2653 .shutdown = serial8250_shutdown,
2654 .set_termios = serial8250_set_termios,
2655 .set_ldisc = serial8250_set_ldisc,
2656 .pm = serial8250_pm,
2657 .type = serial8250_type,
2658 .release_port = serial8250_release_port,
2659 .request_port = serial8250_request_port,
2660 .config_port = serial8250_config_port,
2661 .verify_port = serial8250_verify_port,
2662 #ifdef CONFIG_CONSOLE_POLL
2663 .poll_get_char = serial8250_get_poll_char,
2664 .poll_put_char = serial8250_put_poll_char,
2676 serial8250_isa_config =
v;
2680 static void __init serial8250_isa_init_ports(
void)
2683 static int first = 1;
2693 for (i = 0; i < nr_uarts; i++) {
2701 up->
timer.function = serial8250_timeout;
2710 port->
ops = &serial8250_pops;
2716 for (i = 0, up = serial8250_ports;
2717 i <
ARRAY_SIZE(old_serial_port) && i < nr_uarts;
2730 set_io_from_upio(port);
2732 if (serial8250_isa_config !=
NULL)
2739 serial8250_init_fixed_type_port(
struct uart_8250_port *up,
unsigned int type)
2752 for (i = 0; i < nr_uarts; i++) {
2761 serial8250_init_fixed_type_port(up, up->
port.type);
2767 #ifdef CONFIG_SERIAL_8250_CONSOLE
2769 static void serial8250_console_putchar(
struct uart_port *port,
int ch)
2775 serial_port_out(port,
UART_TX, ch);
2785 serial8250_console_write(
struct console *co,
const char *
s,
unsigned int count)
2789 unsigned long flags;
2800 locked = spin_trylock(&port->
lock);
2802 spin_lock(&port->
lock);
2807 ier = serial_port_in(port,
UART_IER);
2810 serial_port_out(port,
UART_IER, UART_IER_UUE);
2812 serial_port_out(port,
UART_IER, 0);
2821 serial_port_out(port,
UART_IER, ier);
2834 spin_unlock(&port->
lock);
2851 if (co->
index >= nr_uarts)
2853 port = &serial8250_ports[co->
index].port;
2863 static int serial8250_console_early_setup(
void)
2868 static struct console serial8250_console = {
2870 .write = serial8250_console_write,
2872 .setup = serial8250_console_setup,
2873 .early_setup = serial8250_console_early_setup,
2876 .data = &serial8250_reg,
2879 static int __init serial8250_console_init(
void)
2881 serial8250_isa_init_ports();
2892 for (line = 0; line < nr_uarts; line++) {
2893 port = &serial8250_ports[
line].port;
2900 #define SERIAL8250_CONSOLE &serial8250_console
2902 #define SERIAL8250_CONSOLE NULL
2907 .driver_name =
"serial",
2927 serial8250_isa_init_ports();
2928 p = &serial8250_ports[port->
line].port;
2943 set_io_from_upio(p);
2951 p->
handle_irq = serial8250_default_handle_irq;
2980 serial_port_out(port,
UART_LCR, 0xE0);
2982 ns16550a_goto_highspeed(up);
2984 serial_port_out(port,
UART_LCR, 0);
2999 int ret,
i, irqflag = 0;
3006 for (i = 0; p && p->
flags != 0; p++, i++) {
3026 uart.port.irqflags |= irqflag;
3029 dev_err(&dev->
dev,
"unable to register port at index %d "
3030 "(IO%lx MEM%llx IRQ%d): %d\n", i,
3045 for (i = 0; i < nr_uarts; i++) {
3048 if (up->
port.dev == &dev->
dev)
3058 for (i = 0; i <
UART_NR; i++) {
3072 for (i = 0; i <
UART_NR; i++) {
3083 .probe = serial8250_probe,
3085 .suspend = serial8250_suspend,
3086 .resume = serial8250_resume,
3088 .name =
"serial8250",
3113 for (i = 0; i < nr_uarts; i++)
3115 return &serial8250_ports[
i];
3122 for (i = 0; i < nr_uarts; i++)
3124 serial8250_ports[i].port.iobase == 0)
3125 return &serial8250_ports[
i];
3131 for (i = 0; i < nr_uarts; i++)
3133 return &serial8250_ports[
i];
3156 if (up->
port.uartclk == 0)
3161 uart = serial8250_find_match_or_unused(&up->
port);
3166 uart->
port.iobase = up->
port.iobase;
3167 uart->
port.membase = up->
port.membase;
3169 uart->
port.irqflags = up->
port.irqflags;
3170 uart->
port.uartclk = up->
port.uartclk;
3171 uart->
port.fifosize = up->
port.fifosize;
3172 uart->
port.regshift = up->
port.regshift;
3173 uart->
port.iotype = up->
port.iotype;
3176 uart->
port.mapbase = up->
port.mapbase;
3177 uart->
port.private_data = up->
port.private_data;
3182 serial8250_init_fixed_type_port(uart, up->
port.type);
3184 set_io_from_upio(&uart->
port);
3186 if (up->
port.serial_in)
3187 uart->
port.serial_in = up->
port.serial_in;
3188 if (up->
port.serial_out)
3189 uart->
port.serial_out = up->
port.serial_out;
3190 if (up->
port.handle_irq)
3191 uart->
port.handle_irq = up->
port.handle_irq;
3193 if (up->
port.set_termios)
3194 uart->
port.set_termios = up->
port.set_termios;
3197 if (up->
port.handle_break)
3198 uart->
port.handle_break = up->
port.handle_break;
3204 if (serial8250_isa_config !=
NULL)
3205 serial8250_isa_config(0, &uart->
port,
3210 ret = uart->
port.line;
3231 if (serial8250_isa_devs) {
3234 uart->
port.dev = &serial8250_isa_devs->
dev;
3244 static int __init serial8250_init(
void)
3248 serial8250_isa_init_ports();
3251 "%d ports, IRQ sharing %sabled\n", nr_uarts,
3252 share_irqs ?
"en" :
"dis");
3265 goto unreg_uart_drv;
3269 if (!serial8250_isa_devs) {
3278 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->
dev);
3299 static void __exit serial8250_exit(
void)
3308 serial8250_isa_devs =
NULL;
3332 MODULE_PARM_DESC(share_irqs,
"Share IRQs with other non-8250/16x50 devices"
3339 MODULE_PARM_DESC(skip_txen_test,
"Skip checking for the TXEN bug at init time");
3341 #ifdef CONFIG_SERIAL_8250_RSA