V4L/DVB (3916): AverMedia 6 Eyes AVS6EYES support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / serial / 8250.c
blobbbf78aaf9e01f074d15bf6db21042feb9dec5591
1 /*
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.
22 #include <linux/config.h>
24 #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
25 #define SUPPORT_SYSRQ
26 #endif
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/ioport.h>
31 #include <linux/init.h>
32 #include <linux/console.h>
33 #include <linux/sysrq.h>
34 #include <linux/delay.h>
35 #include <linux/platform_device.h>
36 #include <linux/tty.h>
37 #include <linux/tty_flip.h>
38 #include <linux/serial_reg.h>
39 #include <linux/serial_core.h>
40 #include <linux/serial.h>
41 #include <linux/serial_8250.h>
42 #include <linux/nmi.h>
43 #include <linux/mutex.h>
45 #include <asm/io.h>
46 #include <asm/irq.h>
48 #include "8250.h"
51 * Configuration:
52 * share_irqs - whether we pass SA_SHIRQ 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;
60 * Debugging.
62 #if 0
63 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
64 #else
65 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
66 #endif
68 #if 0
69 #define DEBUG_INTR(fmt...) printk(fmt)
70 #else
71 #define DEBUG_INTR(fmt...) do { } while (0)
72 #endif
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
85 #endif
86 #ifdef CONFIG_SERIAL_8250_MANY_PORTS
87 #define CONFIG_SERIAL_MANY_PORTS 1
88 #endif
91 * HUB6 is always on. This will be removed once the header
92 * files have been cleaned.
94 #define CONFIG_HUB6 1
96 #include <asm/serial.h>
99 * SERIAL_PORT_DFNS tells us about built-in ports that have no
100 * standard enumeration mechanism. Platforms that can find all
101 * serial ports via mechanisms like ACPI or PCI need not supply it.
103 #ifndef SERIAL_PORT_DFNS
104 #define SERIAL_PORT_DFNS
105 #endif
107 static const struct old_serial_port old_serial_port[] = {
108 SERIAL_PORT_DFNS /* defined in asm/serial.h */
111 #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
113 #ifdef CONFIG_SERIAL_8250_RSA
115 #define PORT_RSA_MAX 4
116 static unsigned long probe_rsa[PORT_RSA_MAX];
117 static unsigned int probe_rsa_count;
118 #endif /* CONFIG_SERIAL_8250_RSA */
120 struct uart_8250_port {
121 struct uart_port port;
122 struct timer_list timer; /* "no irq" timer */
123 struct list_head list; /* ports on this IRQ */
124 unsigned short capabilities; /* port capabilities */
125 unsigned short bugs; /* port bugs */
126 unsigned int tx_loadsz; /* transmit fifo load size */
127 unsigned char acr;
128 unsigned char ier;
129 unsigned char lcr;
130 unsigned char mcr;
131 unsigned char mcr_mask; /* mask of user bits */
132 unsigned char mcr_force; /* mask of forced bits */
133 unsigned char lsr_break_flag;
136 * We provide a per-port pm hook.
138 void (*pm)(struct uart_port *port,
139 unsigned int state, unsigned int old);
142 struct irq_info {
143 spinlock_t lock;
144 struct list_head *head;
147 static struct irq_info irq_lists[NR_IRQS];
150 * Here we define the default xmit fifo size used for each type of UART.
152 static const struct serial8250_config uart_config[] = {
153 [PORT_UNKNOWN] = {
154 .name = "unknown",
155 .fifo_size = 1,
156 .tx_loadsz = 1,
158 [PORT_8250] = {
159 .name = "8250",
160 .fifo_size = 1,
161 .tx_loadsz = 1,
163 [PORT_16450] = {
164 .name = "16450",
165 .fifo_size = 1,
166 .tx_loadsz = 1,
168 [PORT_16550] = {
169 .name = "16550",
170 .fifo_size = 1,
171 .tx_loadsz = 1,
173 [PORT_16550A] = {
174 .name = "16550A",
175 .fifo_size = 16,
176 .tx_loadsz = 16,
177 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
178 .flags = UART_CAP_FIFO,
180 [PORT_CIRRUS] = {
181 .name = "Cirrus",
182 .fifo_size = 1,
183 .tx_loadsz = 1,
185 [PORT_16650] = {
186 .name = "ST16650",
187 .fifo_size = 1,
188 .tx_loadsz = 1,
189 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
191 [PORT_16650V2] = {
192 .name = "ST16650V2",
193 .fifo_size = 32,
194 .tx_loadsz = 16,
195 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
196 UART_FCR_T_TRIG_00,
197 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
199 [PORT_16750] = {
200 .name = "TI16750",
201 .fifo_size = 64,
202 .tx_loadsz = 64,
203 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
204 UART_FCR7_64BYTE,
205 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
207 [PORT_STARTECH] = {
208 .name = "Startech",
209 .fifo_size = 1,
210 .tx_loadsz = 1,
212 [PORT_16C950] = {
213 .name = "16C950/954",
214 .fifo_size = 128,
215 .tx_loadsz = 128,
216 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
217 .flags = UART_CAP_FIFO,
219 [PORT_16654] = {
220 .name = "ST16654",
221 .fifo_size = 64,
222 .tx_loadsz = 32,
223 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
224 UART_FCR_T_TRIG_10,
225 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
227 [PORT_16850] = {
228 .name = "XR16850",
229 .fifo_size = 128,
230 .tx_loadsz = 128,
231 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
232 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
234 [PORT_RSA] = {
235 .name = "RSA",
236 .fifo_size = 2048,
237 .tx_loadsz = 2048,
238 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
239 .flags = UART_CAP_FIFO,
241 [PORT_NS16550A] = {
242 .name = "NS16550A",
243 .fifo_size = 16,
244 .tx_loadsz = 16,
245 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
246 .flags = UART_CAP_FIFO | UART_NATSEMI,
248 [PORT_XSCALE] = {
249 .name = "XScale",
250 .fifo_size = 32,
251 .tx_loadsz = 32,
252 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
253 .flags = UART_CAP_FIFO | UART_CAP_UUE,
257 #ifdef CONFIG_SERIAL_8250_AU1X00
259 /* Au1x00 UART hardware has a weird register layout */
260 static const u8 au_io_in_map[] = {
261 [UART_RX] = 0,
262 [UART_IER] = 2,
263 [UART_IIR] = 3,
264 [UART_LCR] = 5,
265 [UART_MCR] = 6,
266 [UART_LSR] = 7,
267 [UART_MSR] = 8,
270 static const u8 au_io_out_map[] = {
271 [UART_TX] = 1,
272 [UART_IER] = 2,
273 [UART_FCR] = 4,
274 [UART_LCR] = 5,
275 [UART_MCR] = 6,
278 /* sane hardware needs no mapping */
279 static inline int map_8250_in_reg(struct uart_8250_port *up, int offset)
281 if (up->port.iotype != UPIO_AU)
282 return offset;
283 return au_io_in_map[offset];
286 static inline int map_8250_out_reg(struct uart_8250_port *up, int offset)
288 if (up->port.iotype != UPIO_AU)
289 return offset;
290 return au_io_out_map[offset];
293 #else
295 /* sane hardware needs no mapping */
296 #define map_8250_in_reg(up, offset) (offset)
297 #define map_8250_out_reg(up, offset) (offset)
299 #endif
301 static unsigned int serial_in(struct uart_8250_port *up, int offset)
303 offset = map_8250_in_reg(up, offset) << up->port.regshift;
305 switch (up->port.iotype) {
306 case UPIO_HUB6:
307 outb(up->port.hub6 - 1 + offset, up->port.iobase);
308 return inb(up->port.iobase + 1);
310 case UPIO_MEM:
311 return readb(up->port.membase + offset);
313 case UPIO_MEM32:
314 return readl(up->port.membase + offset);
316 #ifdef CONFIG_SERIAL_8250_AU1X00
317 case UPIO_AU:
318 return __raw_readl(up->port.membase + offset);
319 #endif
321 default:
322 return inb(up->port.iobase + offset);
326 static void
327 serial_out(struct uart_8250_port *up, int offset, int value)
329 offset = map_8250_out_reg(up, offset) << up->port.regshift;
331 switch (up->port.iotype) {
332 case UPIO_HUB6:
333 outb(up->port.hub6 - 1 + offset, up->port.iobase);
334 outb(value, up->port.iobase + 1);
335 break;
337 case UPIO_MEM:
338 writeb(value, up->port.membase + offset);
339 break;
341 case UPIO_MEM32:
342 writel(value, up->port.membase + offset);
343 break;
345 #ifdef CONFIG_SERIAL_8250_AU1X00
346 case UPIO_AU:
347 __raw_writel(value, up->port.membase + offset);
348 break;
349 #endif
351 default:
352 outb(value, up->port.iobase + offset);
357 * We used to support using pause I/O for certain machines. We
358 * haven't supported this for a while, but just in case it's badly
359 * needed for certain old 386 machines, I've left these #define's
360 * in....
362 #define serial_inp(up, offset) serial_in(up, offset)
363 #define serial_outp(up, offset, value) serial_out(up, offset, value)
365 /* Uart divisor latch read */
366 static inline int _serial_dl_read(struct uart_8250_port *up)
368 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
371 /* Uart divisor latch write */
372 static inline void _serial_dl_write(struct uart_8250_port *up, int value)
374 serial_outp(up, UART_DLL, value & 0xff);
375 serial_outp(up, UART_DLM, value >> 8 & 0xff);
378 #ifdef CONFIG_SERIAL_8250_AU1X00
379 /* Au1x00 haven't got a standard divisor latch */
380 static int serial_dl_read(struct uart_8250_port *up)
382 if (up->port.iotype == UPIO_AU)
383 return __raw_readl(up->port.membase + 0x28);
384 else
385 return _serial_dl_read(up);
388 static void serial_dl_write(struct uart_8250_port *up, int value)
390 if (up->port.iotype == UPIO_AU)
391 __raw_writel(value, up->port.membase + 0x28);
392 else
393 _serial_dl_write(up, value);
395 #else
396 #define serial_dl_read(up) _serial_dl_read(up)
397 #define serial_dl_write(up, value) _serial_dl_write(up, value)
398 #endif
401 * For the 16C950
403 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
405 serial_out(up, UART_SCR, offset);
406 serial_out(up, UART_ICR, value);
409 static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
411 unsigned int value;
413 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
414 serial_out(up, UART_SCR, offset);
415 value = serial_in(up, UART_ICR);
416 serial_icr_write(up, UART_ACR, up->acr);
418 return value;
422 * FIFO support.
424 static inline void serial8250_clear_fifos(struct uart_8250_port *p)
426 if (p->capabilities & UART_CAP_FIFO) {
427 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
428 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
429 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
430 serial_outp(p, UART_FCR, 0);
435 * IER sleep support. UARTs which have EFRs need the "extended
436 * capability" bit enabled. Note that on XR16C850s, we need to
437 * reset LCR to write to IER.
439 static inline void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
441 if (p->capabilities & UART_CAP_SLEEP) {
442 if (p->capabilities & UART_CAP_EFR) {
443 serial_outp(p, UART_LCR, 0xBF);
444 serial_outp(p, UART_EFR, UART_EFR_ECB);
445 serial_outp(p, UART_LCR, 0);
447 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
448 if (p->capabilities & UART_CAP_EFR) {
449 serial_outp(p, UART_LCR, 0xBF);
450 serial_outp(p, UART_EFR, 0);
451 serial_outp(p, UART_LCR, 0);
456 #ifdef CONFIG_SERIAL_8250_RSA
458 * Attempts to turn on the RSA FIFO. Returns zero on failure.
459 * We set the port uart clock rate if we succeed.
461 static int __enable_rsa(struct uart_8250_port *up)
463 unsigned char mode;
464 int result;
466 mode = serial_inp(up, UART_RSA_MSR);
467 result = mode & UART_RSA_MSR_FIFO;
469 if (!result) {
470 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
471 mode = serial_inp(up, UART_RSA_MSR);
472 result = mode & UART_RSA_MSR_FIFO;
475 if (result)
476 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
478 return result;
481 static void enable_rsa(struct uart_8250_port *up)
483 if (up->port.type == PORT_RSA) {
484 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
485 spin_lock_irq(&up->port.lock);
486 __enable_rsa(up);
487 spin_unlock_irq(&up->port.lock);
489 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
490 serial_outp(up, UART_RSA_FRR, 0);
495 * Attempts to turn off the RSA FIFO. Returns zero on failure.
496 * It is unknown why interrupts were disabled in here. However,
497 * the caller is expected to preserve this behaviour by grabbing
498 * the spinlock before calling this function.
500 static void disable_rsa(struct uart_8250_port *up)
502 unsigned char mode;
503 int result;
505 if (up->port.type == PORT_RSA &&
506 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
507 spin_lock_irq(&up->port.lock);
509 mode = serial_inp(up, UART_RSA_MSR);
510 result = !(mode & UART_RSA_MSR_FIFO);
512 if (!result) {
513 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
514 mode = serial_inp(up, UART_RSA_MSR);
515 result = !(mode & UART_RSA_MSR_FIFO);
518 if (result)
519 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
520 spin_unlock_irq(&up->port.lock);
523 #endif /* CONFIG_SERIAL_8250_RSA */
526 * This is a quickie test to see how big the FIFO is.
527 * It doesn't work at all the time, more's the pity.
529 static int size_fifo(struct uart_8250_port *up)
531 unsigned char old_fcr, old_mcr, old_lcr;
532 unsigned short old_dl;
533 int count;
535 old_lcr = serial_inp(up, UART_LCR);
536 serial_outp(up, UART_LCR, 0);
537 old_fcr = serial_inp(up, UART_FCR);
538 old_mcr = serial_inp(up, UART_MCR);
539 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
540 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
541 serial_outp(up, UART_MCR, UART_MCR_LOOP);
542 serial_outp(up, UART_LCR, UART_LCR_DLAB);
543 old_dl = serial_dl_read(up);
544 serial_dl_write(up, 0x0001);
545 serial_outp(up, UART_LCR, 0x03);
546 for (count = 0; count < 256; count++)
547 serial_outp(up, UART_TX, count);
548 mdelay(20);/* FIXME - schedule_timeout */
549 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
550 (count < 256); count++)
551 serial_inp(up, UART_RX);
552 serial_outp(up, UART_FCR, old_fcr);
553 serial_outp(up, UART_MCR, old_mcr);
554 serial_outp(up, UART_LCR, UART_LCR_DLAB);
555 serial_dl_write(up, old_dl);
556 serial_outp(up, UART_LCR, old_lcr);
558 return count;
562 * Read UART ID using the divisor method - set DLL and DLM to zero
563 * and the revision will be in DLL and device type in DLM. We
564 * preserve the device state across this.
566 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
568 unsigned char old_dll, old_dlm, old_lcr;
569 unsigned int id;
571 old_lcr = serial_inp(p, UART_LCR);
572 serial_outp(p, UART_LCR, UART_LCR_DLAB);
574 old_dll = serial_inp(p, UART_DLL);
575 old_dlm = serial_inp(p, UART_DLM);
577 serial_outp(p, UART_DLL, 0);
578 serial_outp(p, UART_DLM, 0);
580 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
582 serial_outp(p, UART_DLL, old_dll);
583 serial_outp(p, UART_DLM, old_dlm);
584 serial_outp(p, UART_LCR, old_lcr);
586 return id;
590 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
591 * When this function is called we know it is at least a StarTech
592 * 16650 V2, but it might be one of several StarTech UARTs, or one of
593 * its clones. (We treat the broken original StarTech 16650 V1 as a
594 * 16550, and why not? Startech doesn't seem to even acknowledge its
595 * existence.)
597 * What evil have men's minds wrought...
599 static void autoconfig_has_efr(struct uart_8250_port *up)
601 unsigned int id1, id2, id3, rev;
604 * Everything with an EFR has SLEEP
606 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
609 * First we check to see if it's an Oxford Semiconductor UART.
611 * If we have to do this here because some non-National
612 * Semiconductor clone chips lock up if you try writing to the
613 * LSR register (which serial_icr_read does)
617 * Check for Oxford Semiconductor 16C950.
619 * EFR [4] must be set else this test fails.
621 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
622 * claims that it's needed for 952 dual UART's (which are not
623 * recommended for new designs).
625 up->acr = 0;
626 serial_out(up, UART_LCR, 0xBF);
627 serial_out(up, UART_EFR, UART_EFR_ECB);
628 serial_out(up, UART_LCR, 0x00);
629 id1 = serial_icr_read(up, UART_ID1);
630 id2 = serial_icr_read(up, UART_ID2);
631 id3 = serial_icr_read(up, UART_ID3);
632 rev = serial_icr_read(up, UART_REV);
634 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
636 if (id1 == 0x16 && id2 == 0xC9 &&
637 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
638 up->port.type = PORT_16C950;
641 * Enable work around for the Oxford Semiconductor 952 rev B
642 * chip which causes it to seriously miscalculate baud rates
643 * when DLL is 0.
645 if (id3 == 0x52 && rev == 0x01)
646 up->bugs |= UART_BUG_QUOT;
647 return;
651 * We check for a XR16C850 by setting DLL and DLM to 0, and then
652 * reading back DLL and DLM. The chip type depends on the DLM
653 * value read back:
654 * 0x10 - XR16C850 and the DLL contains the chip revision.
655 * 0x12 - XR16C2850.
656 * 0x14 - XR16C854.
658 id1 = autoconfig_read_divisor_id(up);
659 DEBUG_AUTOCONF("850id=%04x ", id1);
661 id2 = id1 >> 8;
662 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
663 up->port.type = PORT_16850;
664 return;
668 * It wasn't an XR16C850.
670 * We distinguish between the '654 and the '650 by counting
671 * how many bytes are in the FIFO. I'm using this for now,
672 * since that's the technique that was sent to me in the
673 * serial driver update, but I'm not convinced this works.
674 * I've had problems doing this in the past. -TYT
676 if (size_fifo(up) == 64)
677 up->port.type = PORT_16654;
678 else
679 up->port.type = PORT_16650V2;
683 * We detected a chip without a FIFO. Only two fall into
684 * this category - the original 8250 and the 16450. The
685 * 16450 has a scratch register (accessible with LCR=0)
687 static void autoconfig_8250(struct uart_8250_port *up)
689 unsigned char scratch, status1, status2;
691 up->port.type = PORT_8250;
693 scratch = serial_in(up, UART_SCR);
694 serial_outp(up, UART_SCR, 0xa5);
695 status1 = serial_in(up, UART_SCR);
696 serial_outp(up, UART_SCR, 0x5a);
697 status2 = serial_in(up, UART_SCR);
698 serial_outp(up, UART_SCR, scratch);
700 if (status1 == 0xa5 && status2 == 0x5a)
701 up->port.type = PORT_16450;
704 static int broken_efr(struct uart_8250_port *up)
707 * Exar ST16C2550 "A2" devices incorrectly detect as
708 * having an EFR, and report an ID of 0x0201. See
709 * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf
711 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
712 return 1;
714 return 0;
718 * We know that the chip has FIFOs. Does it have an EFR? The
719 * EFR is located in the same register position as the IIR and
720 * we know the top two bits of the IIR are currently set. The
721 * EFR should contain zero. Try to read the EFR.
723 static void autoconfig_16550a(struct uart_8250_port *up)
725 unsigned char status1, status2;
726 unsigned int iersave;
728 up->port.type = PORT_16550A;
729 up->capabilities |= UART_CAP_FIFO;
732 * Check for presence of the EFR when DLAB is set.
733 * Only ST16C650V1 UARTs pass this test.
735 serial_outp(up, UART_LCR, UART_LCR_DLAB);
736 if (serial_in(up, UART_EFR) == 0) {
737 serial_outp(up, UART_EFR, 0xA8);
738 if (serial_in(up, UART_EFR) != 0) {
739 DEBUG_AUTOCONF("EFRv1 ");
740 up->port.type = PORT_16650;
741 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
742 } else {
743 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
745 serial_outp(up, UART_EFR, 0);
746 return;
750 * Maybe it requires 0xbf to be written to the LCR.
751 * (other ST16C650V2 UARTs, TI16C752A, etc)
753 serial_outp(up, UART_LCR, 0xBF);
754 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
755 DEBUG_AUTOCONF("EFRv2 ");
756 autoconfig_has_efr(up);
757 return;
761 * Check for a National Semiconductor SuperIO chip.
762 * Attempt to switch to bank 2, read the value of the LOOP bit
763 * from EXCR1. Switch back to bank 0, change it in MCR. Then
764 * switch back to bank 2, read it from EXCR1 again and check
765 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
767 serial_outp(up, UART_LCR, 0);
768 status1 = serial_in(up, UART_MCR);
769 serial_outp(up, UART_LCR, 0xE0);
770 status2 = serial_in(up, 0x02); /* EXCR1 */
772 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
773 serial_outp(up, UART_LCR, 0);
774 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
775 serial_outp(up, UART_LCR, 0xE0);
776 status2 = serial_in(up, 0x02); /* EXCR1 */
777 serial_outp(up, UART_LCR, 0);
778 serial_outp(up, UART_MCR, status1);
780 if ((status2 ^ status1) & UART_MCR_LOOP) {
781 unsigned short quot;
783 serial_outp(up, UART_LCR, 0xE0);
785 quot = serial_dl_read(up);
786 quot <<= 3;
788 status1 = serial_in(up, 0x04); /* EXCR1 */
789 status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
790 status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
791 serial_outp(up, 0x04, status1);
793 serial_dl_write(up, quot);
795 serial_outp(up, UART_LCR, 0);
797 up->port.uartclk = 921600*16;
798 up->port.type = PORT_NS16550A;
799 up->capabilities |= UART_NATSEMI;
800 return;
805 * No EFR. Try to detect a TI16750, which only sets bit 5 of
806 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
807 * Try setting it with and without DLAB set. Cheap clones
808 * set bit 5 without DLAB set.
810 serial_outp(up, UART_LCR, 0);
811 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
812 status1 = serial_in(up, UART_IIR) >> 5;
813 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
814 serial_outp(up, UART_LCR, UART_LCR_DLAB);
815 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
816 status2 = serial_in(up, UART_IIR) >> 5;
817 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
818 serial_outp(up, UART_LCR, 0);
820 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
822 if (status1 == 6 && status2 == 7) {
823 up->port.type = PORT_16750;
824 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
825 return;
829 * Try writing and reading the UART_IER_UUE bit (b6).
830 * If it works, this is probably one of the Xscale platform's
831 * internal UARTs.
832 * We're going to explicitly set the UUE bit to 0 before
833 * trying to write and read a 1 just to make sure it's not
834 * already a 1 and maybe locked there before we even start start.
836 iersave = serial_in(up, UART_IER);
837 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
838 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
840 * OK it's in a known zero state, try writing and reading
841 * without disturbing the current state of the other bits.
843 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
844 if (serial_in(up, UART_IER) & UART_IER_UUE) {
846 * It's an Xscale.
847 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
849 DEBUG_AUTOCONF("Xscale ");
850 up->port.type = PORT_XSCALE;
851 up->capabilities |= UART_CAP_UUE;
852 return;
854 } else {
856 * If we got here we couldn't force the IER_UUE bit to 0.
857 * Log it and continue.
859 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
861 serial_outp(up, UART_IER, iersave);
865 * This routine is called by rs_init() to initialize a specific serial
866 * port. It determines what type of UART chip this serial port is
867 * using: 8250, 16450, 16550, 16550A. The important question is
868 * whether or not this UART is a 16550A or not, since this will
869 * determine whether or not we can use its FIFO features or not.
871 static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
873 unsigned char status1, scratch, scratch2, scratch3;
874 unsigned char save_lcr, save_mcr;
875 unsigned long flags;
877 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
878 return;
880 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ",
881 up->port.line, up->port.iobase, up->port.membase);
884 * We really do need global IRQs disabled here - we're going to
885 * be frobbing the chips IRQ enable register to see if it exists.
887 spin_lock_irqsave(&up->port.lock, flags);
888 // save_flags(flags); cli();
890 up->capabilities = 0;
891 up->bugs = 0;
893 if (!(up->port.flags & UPF_BUGGY_UART)) {
895 * Do a simple existence test first; if we fail this,
896 * there's no point trying anything else.
898 * 0x80 is used as a nonsense port to prevent against
899 * false positives due to ISA bus float. The
900 * assumption is that 0x80 is a non-existent port;
901 * which should be safe since include/asm/io.h also
902 * makes this assumption.
904 * Note: this is safe as long as MCR bit 4 is clear
905 * and the device is in "PC" mode.
907 scratch = serial_inp(up, UART_IER);
908 serial_outp(up, UART_IER, 0);
909 #ifdef __i386__
910 outb(0xff, 0x080);
911 #endif
912 scratch2 = serial_inp(up, UART_IER);
913 serial_outp(up, UART_IER, 0x0F);
914 #ifdef __i386__
915 outb(0, 0x080);
916 #endif
917 scratch3 = serial_inp(up, UART_IER);
918 serial_outp(up, UART_IER, scratch);
919 if (scratch2 != 0 || scratch3 != 0x0F) {
921 * We failed; there's nothing here
923 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
924 scratch2, scratch3);
925 goto out;
929 save_mcr = serial_in(up, UART_MCR);
930 save_lcr = serial_in(up, UART_LCR);
933 * Check to see if a UART is really there. Certain broken
934 * internal modems based on the Rockwell chipset fail this
935 * test, because they apparently don't implement the loopback
936 * test mode. So this test is skipped on the COM 1 through
937 * COM 4 ports. This *should* be safe, since no board
938 * manufacturer would be stupid enough to design a board
939 * that conflicts with COM 1-4 --- we hope!
941 if (!(up->port.flags & UPF_SKIP_TEST)) {
942 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
943 status1 = serial_inp(up, UART_MSR) & 0xF0;
944 serial_outp(up, UART_MCR, save_mcr);
945 if (status1 != 0x90) {
946 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
947 status1);
948 goto out;
953 * We're pretty sure there's a port here. Lets find out what
954 * type of port it is. The IIR top two bits allows us to find
955 * out if it's 8250 or 16450, 16550, 16550A or later. This
956 * determines what we test for next.
958 * We also initialise the EFR (if any) to zero for later. The
959 * EFR occupies the same register location as the FCR and IIR.
961 serial_outp(up, UART_LCR, 0xBF);
962 serial_outp(up, UART_EFR, 0);
963 serial_outp(up, UART_LCR, 0);
965 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
966 scratch = serial_in(up, UART_IIR) >> 6;
968 DEBUG_AUTOCONF("iir=%d ", scratch);
970 switch (scratch) {
971 case 0:
972 autoconfig_8250(up);
973 break;
974 case 1:
975 up->port.type = PORT_UNKNOWN;
976 break;
977 case 2:
978 up->port.type = PORT_16550;
979 break;
980 case 3:
981 autoconfig_16550a(up);
982 break;
985 #ifdef CONFIG_SERIAL_8250_RSA
987 * Only probe for RSA ports if we got the region.
989 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
990 int i;
992 for (i = 0 ; i < probe_rsa_count; ++i) {
993 if (probe_rsa[i] == up->port.iobase &&
994 __enable_rsa(up)) {
995 up->port.type = PORT_RSA;
996 break;
1000 #endif
1002 #ifdef CONFIG_SERIAL_8250_AU1X00
1003 /* if access method is AU, it is a 16550 with a quirk */
1004 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
1005 up->bugs |= UART_BUG_NOMSR;
1006 #endif
1008 serial_outp(up, UART_LCR, save_lcr);
1010 if (up->capabilities != uart_config[up->port.type].flags) {
1011 printk(KERN_WARNING
1012 "ttyS%d: detected caps %08x should be %08x\n",
1013 up->port.line, up->capabilities,
1014 uart_config[up->port.type].flags);
1017 up->port.fifosize = uart_config[up->port.type].fifo_size;
1018 up->capabilities = uart_config[up->port.type].flags;
1019 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1021 if (up->port.type == PORT_UNKNOWN)
1022 goto out;
1025 * Reset the UART.
1027 #ifdef CONFIG_SERIAL_8250_RSA
1028 if (up->port.type == PORT_RSA)
1029 serial_outp(up, UART_RSA_FRR, 0);
1030 #endif
1031 serial_outp(up, UART_MCR, save_mcr);
1032 serial8250_clear_fifos(up);
1033 (void)serial_in(up, UART_RX);
1034 if (up->capabilities & UART_CAP_UUE)
1035 serial_outp(up, UART_IER, UART_IER_UUE);
1036 else
1037 serial_outp(up, UART_IER, 0);
1039 out:
1040 spin_unlock_irqrestore(&up->port.lock, flags);
1041 // restore_flags(flags);
1042 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1045 static void autoconfig_irq(struct uart_8250_port *up)
1047 unsigned char save_mcr, save_ier;
1048 unsigned char save_ICP = 0;
1049 unsigned int ICP = 0;
1050 unsigned long irqs;
1051 int irq;
1053 if (up->port.flags & UPF_FOURPORT) {
1054 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1055 save_ICP = inb_p(ICP);
1056 outb_p(0x80, ICP);
1057 (void) inb_p(ICP);
1060 /* forget possible initially masked and pending IRQ */
1061 probe_irq_off(probe_irq_on());
1062 save_mcr = serial_inp(up, UART_MCR);
1063 save_ier = serial_inp(up, UART_IER);
1064 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1066 irqs = probe_irq_on();
1067 serial_outp(up, UART_MCR, 0);
1068 udelay (10);
1069 if (up->port.flags & UPF_FOURPORT) {
1070 serial_outp(up, UART_MCR,
1071 UART_MCR_DTR | UART_MCR_RTS);
1072 } else {
1073 serial_outp(up, UART_MCR,
1074 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1076 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1077 (void)serial_inp(up, UART_LSR);
1078 (void)serial_inp(up, UART_RX);
1079 (void)serial_inp(up, UART_IIR);
1080 (void)serial_inp(up, UART_MSR);
1081 serial_outp(up, UART_TX, 0xFF);
1082 udelay (20);
1083 irq = probe_irq_off(irqs);
1085 serial_outp(up, UART_MCR, save_mcr);
1086 serial_outp(up, UART_IER, save_ier);
1088 if (up->port.flags & UPF_FOURPORT)
1089 outb_p(save_ICP, ICP);
1091 up->port.irq = (irq > 0) ? irq : 0;
1094 static inline void __stop_tx(struct uart_8250_port *p)
1096 if (p->ier & UART_IER_THRI) {
1097 p->ier &= ~UART_IER_THRI;
1098 serial_out(p, UART_IER, p->ier);
1102 static void serial8250_stop_tx(struct uart_port *port)
1104 struct uart_8250_port *up = (struct uart_8250_port *)port;
1106 __stop_tx(up);
1109 * We really want to stop the transmitter from sending.
1111 if (up->port.type == PORT_16C950) {
1112 up->acr |= UART_ACR_TXDIS;
1113 serial_icr_write(up, UART_ACR, up->acr);
1117 static void transmit_chars(struct uart_8250_port *up);
1119 static void serial8250_start_tx(struct uart_port *port)
1121 struct uart_8250_port *up = (struct uart_8250_port *)port;
1123 if (!(up->ier & UART_IER_THRI)) {
1124 up->ier |= UART_IER_THRI;
1125 serial_out(up, UART_IER, up->ier);
1127 if (up->bugs & UART_BUG_TXEN) {
1128 unsigned char lsr, iir;
1129 lsr = serial_in(up, UART_LSR);
1130 iir = serial_in(up, UART_IIR);
1131 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT)
1132 transmit_chars(up);
1137 * Re-enable the transmitter if we disabled it.
1139 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1140 up->acr &= ~UART_ACR_TXDIS;
1141 serial_icr_write(up, UART_ACR, up->acr);
1145 static void serial8250_stop_rx(struct uart_port *port)
1147 struct uart_8250_port *up = (struct uart_8250_port *)port;
1149 up->ier &= ~UART_IER_RLSI;
1150 up->port.read_status_mask &= ~UART_LSR_DR;
1151 serial_out(up, UART_IER, up->ier);
1154 static void serial8250_enable_ms(struct uart_port *port)
1156 struct uart_8250_port *up = (struct uart_8250_port *)port;
1158 /* no MSR capabilities */
1159 if (up->bugs & UART_BUG_NOMSR)
1160 return;
1162 up->ier |= UART_IER_MSI;
1163 serial_out(up, UART_IER, up->ier);
1166 static void
1167 receive_chars(struct uart_8250_port *up, int *status, struct pt_regs *regs)
1169 struct tty_struct *tty = up->port.info->tty;
1170 unsigned char ch, lsr = *status;
1171 int max_count = 256;
1172 char flag;
1174 do {
1175 ch = serial_inp(up, UART_RX);
1176 flag = TTY_NORMAL;
1177 up->port.icount.rx++;
1179 #ifdef CONFIG_SERIAL_8250_CONSOLE
1181 * Recover the break flag from console xmit
1183 if (up->port.line == up->port.cons->index) {
1184 lsr |= up->lsr_break_flag;
1185 up->lsr_break_flag = 0;
1187 #endif
1189 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
1190 UART_LSR_FE | UART_LSR_OE))) {
1192 * For statistics only
1194 if (lsr & UART_LSR_BI) {
1195 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1196 up->port.icount.brk++;
1198 * We do the SysRQ and SAK checking
1199 * here because otherwise the break
1200 * may get masked by ignore_status_mask
1201 * or read_status_mask.
1203 if (uart_handle_break(&up->port))
1204 goto ignore_char;
1205 } else if (lsr & UART_LSR_PE)
1206 up->port.icount.parity++;
1207 else if (lsr & UART_LSR_FE)
1208 up->port.icount.frame++;
1209 if (lsr & UART_LSR_OE)
1210 up->port.icount.overrun++;
1213 * Mask off conditions which should be ignored.
1215 lsr &= up->port.read_status_mask;
1217 if (lsr & UART_LSR_BI) {
1218 DEBUG_INTR("handling break....");
1219 flag = TTY_BREAK;
1220 } else if (lsr & UART_LSR_PE)
1221 flag = TTY_PARITY;
1222 else if (lsr & UART_LSR_FE)
1223 flag = TTY_FRAME;
1225 if (uart_handle_sysrq_char(&up->port, ch, regs))
1226 goto ignore_char;
1228 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1230 ignore_char:
1231 lsr = serial_inp(up, UART_LSR);
1232 } while ((lsr & UART_LSR_DR) && (max_count-- > 0));
1233 spin_unlock(&up->port.lock);
1234 tty_flip_buffer_push(tty);
1235 spin_lock(&up->port.lock);
1236 *status = lsr;
1239 static void transmit_chars(struct uart_8250_port *up)
1241 struct circ_buf *xmit = &up->port.info->xmit;
1242 int count;
1244 if (up->port.x_char) {
1245 serial_outp(up, UART_TX, up->port.x_char);
1246 up->port.icount.tx++;
1247 up->port.x_char = 0;
1248 return;
1250 if (uart_tx_stopped(&up->port)) {
1251 serial8250_stop_tx(&up->port);
1252 return;
1254 if (uart_circ_empty(xmit)) {
1255 __stop_tx(up);
1256 return;
1259 count = up->tx_loadsz;
1260 do {
1261 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1262 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1263 up->port.icount.tx++;
1264 if (uart_circ_empty(xmit))
1265 break;
1266 } while (--count > 0);
1268 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1269 uart_write_wakeup(&up->port);
1271 DEBUG_INTR("THRE...");
1273 if (uart_circ_empty(xmit))
1274 __stop_tx(up);
1277 static unsigned int check_modem_status(struct uart_8250_port *up)
1279 unsigned int status = serial_in(up, UART_MSR);
1281 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI) {
1282 if (status & UART_MSR_TERI)
1283 up->port.icount.rng++;
1284 if (status & UART_MSR_DDSR)
1285 up->port.icount.dsr++;
1286 if (status & UART_MSR_DDCD)
1287 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1288 if (status & UART_MSR_DCTS)
1289 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
1291 wake_up_interruptible(&up->port.info->delta_msr_wait);
1294 return status;
1298 * This handles the interrupt from one port.
1300 static inline void
1301 serial8250_handle_port(struct uart_8250_port *up, struct pt_regs *regs)
1303 unsigned int status;
1305 spin_lock(&up->port.lock);
1307 status = serial_inp(up, UART_LSR);
1309 DEBUG_INTR("status = %x...", status);
1311 if (status & UART_LSR_DR)
1312 receive_chars(up, &status, regs);
1313 check_modem_status(up);
1314 if (status & UART_LSR_THRE)
1315 transmit_chars(up);
1317 spin_unlock(&up->port.lock);
1321 * This is the serial driver's interrupt routine.
1323 * Arjan thinks the old way was overly complex, so it got simplified.
1324 * Alan disagrees, saying that need the complexity to handle the weird
1325 * nature of ISA shared interrupts. (This is a special exception.)
1327 * In order to handle ISA shared interrupts properly, we need to check
1328 * that all ports have been serviced, and therefore the ISA interrupt
1329 * line has been de-asserted.
1331 * This means we need to loop through all ports. checking that they
1332 * don't have an interrupt pending.
1334 static irqreturn_t serial8250_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1336 struct irq_info *i = dev_id;
1337 struct list_head *l, *end = NULL;
1338 int pass_counter = 0, handled = 0;
1340 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1342 spin_lock(&i->lock);
1344 l = i->head;
1345 do {
1346 struct uart_8250_port *up;
1347 unsigned int iir;
1349 up = list_entry(l, struct uart_8250_port, list);
1351 iir = serial_in(up, UART_IIR);
1352 if (!(iir & UART_IIR_NO_INT)) {
1353 serial8250_handle_port(up, regs);
1355 handled = 1;
1357 end = NULL;
1358 } else if (end == NULL)
1359 end = l;
1361 l = l->next;
1363 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1364 /* If we hit this, we're dead. */
1365 printk(KERN_ERR "serial8250: too much work for "
1366 "irq%d\n", irq);
1367 break;
1369 } while (l != end);
1371 spin_unlock(&i->lock);
1373 DEBUG_INTR("end.\n");
1375 return IRQ_RETVAL(handled);
1379 * To support ISA shared interrupts, we need to have one interrupt
1380 * handler that ensures that the IRQ line has been deasserted
1381 * before returning. Failing to do this will result in the IRQ
1382 * line being stuck active, and, since ISA irqs are edge triggered,
1383 * no more IRQs will be seen.
1385 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1387 spin_lock_irq(&i->lock);
1389 if (!list_empty(i->head)) {
1390 if (i->head == &up->list)
1391 i->head = i->head->next;
1392 list_del(&up->list);
1393 } else {
1394 BUG_ON(i->head != &up->list);
1395 i->head = NULL;
1398 spin_unlock_irq(&i->lock);
1401 static int serial_link_irq_chain(struct uart_8250_port *up)
1403 struct irq_info *i = irq_lists + up->port.irq;
1404 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? SA_SHIRQ : 0;
1406 spin_lock_irq(&i->lock);
1408 if (i->head) {
1409 list_add(&up->list, i->head);
1410 spin_unlock_irq(&i->lock);
1412 ret = 0;
1413 } else {
1414 INIT_LIST_HEAD(&up->list);
1415 i->head = &up->list;
1416 spin_unlock_irq(&i->lock);
1418 ret = request_irq(up->port.irq, serial8250_interrupt,
1419 irq_flags, "serial", i);
1420 if (ret < 0)
1421 serial_do_unlink(i, up);
1424 return ret;
1427 static void serial_unlink_irq_chain(struct uart_8250_port *up)
1429 struct irq_info *i = irq_lists + up->port.irq;
1431 BUG_ON(i->head == NULL);
1433 if (list_empty(i->head))
1434 free_irq(up->port.irq, i);
1436 serial_do_unlink(i, up);
1440 * This function is used to handle ports that do not have an
1441 * interrupt. This doesn't work very well for 16450's, but gives
1442 * barely passable results for a 16550A. (Although at the expense
1443 * of much CPU overhead).
1445 static void serial8250_timeout(unsigned long data)
1447 struct uart_8250_port *up = (struct uart_8250_port *)data;
1448 unsigned int timeout;
1449 unsigned int iir;
1451 iir = serial_in(up, UART_IIR);
1452 if (!(iir & UART_IIR_NO_INT))
1453 serial8250_handle_port(up, NULL);
1455 timeout = up->port.timeout;
1456 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
1457 mod_timer(&up->timer, jiffies + timeout);
1460 static unsigned int serial8250_tx_empty(struct uart_port *port)
1462 struct uart_8250_port *up = (struct uart_8250_port *)port;
1463 unsigned long flags;
1464 unsigned int ret;
1466 spin_lock_irqsave(&up->port.lock, flags);
1467 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
1468 spin_unlock_irqrestore(&up->port.lock, flags);
1470 return ret;
1473 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1475 struct uart_8250_port *up = (struct uart_8250_port *)port;
1476 unsigned int status;
1477 unsigned int ret;
1479 status = check_modem_status(up);
1481 ret = 0;
1482 if (status & UART_MSR_DCD)
1483 ret |= TIOCM_CAR;
1484 if (status & UART_MSR_RI)
1485 ret |= TIOCM_RNG;
1486 if (status & UART_MSR_DSR)
1487 ret |= TIOCM_DSR;
1488 if (status & UART_MSR_CTS)
1489 ret |= TIOCM_CTS;
1490 return ret;
1493 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1495 struct uart_8250_port *up = (struct uart_8250_port *)port;
1496 unsigned char mcr = 0;
1498 if (mctrl & TIOCM_RTS)
1499 mcr |= UART_MCR_RTS;
1500 if (mctrl & TIOCM_DTR)
1501 mcr |= UART_MCR_DTR;
1502 if (mctrl & TIOCM_OUT1)
1503 mcr |= UART_MCR_OUT1;
1504 if (mctrl & TIOCM_OUT2)
1505 mcr |= UART_MCR_OUT2;
1506 if (mctrl & TIOCM_LOOP)
1507 mcr |= UART_MCR_LOOP;
1509 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1511 serial_out(up, UART_MCR, mcr);
1514 static void serial8250_break_ctl(struct uart_port *port, int break_state)
1516 struct uart_8250_port *up = (struct uart_8250_port *)port;
1517 unsigned long flags;
1519 spin_lock_irqsave(&up->port.lock, flags);
1520 if (break_state == -1)
1521 up->lcr |= UART_LCR_SBC;
1522 else
1523 up->lcr &= ~UART_LCR_SBC;
1524 serial_out(up, UART_LCR, up->lcr);
1525 spin_unlock_irqrestore(&up->port.lock, flags);
1528 static int serial8250_startup(struct uart_port *port)
1530 struct uart_8250_port *up = (struct uart_8250_port *)port;
1531 unsigned long flags;
1532 unsigned char lsr, iir;
1533 int retval;
1535 up->capabilities = uart_config[up->port.type].flags;
1536 up->mcr = 0;
1538 if (up->port.type == PORT_16C950) {
1539 /* Wake up and initialize UART */
1540 up->acr = 0;
1541 serial_outp(up, UART_LCR, 0xBF);
1542 serial_outp(up, UART_EFR, UART_EFR_ECB);
1543 serial_outp(up, UART_IER, 0);
1544 serial_outp(up, UART_LCR, 0);
1545 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1546 serial_outp(up, UART_LCR, 0xBF);
1547 serial_outp(up, UART_EFR, UART_EFR_ECB);
1548 serial_outp(up, UART_LCR, 0);
1551 #ifdef CONFIG_SERIAL_8250_RSA
1553 * If this is an RSA port, see if we can kick it up to the
1554 * higher speed clock.
1556 enable_rsa(up);
1557 #endif
1560 * Clear the FIFO buffers and disable them.
1561 * (they will be reenabled in set_termios())
1563 serial8250_clear_fifos(up);
1566 * Clear the interrupt registers.
1568 (void) serial_inp(up, UART_LSR);
1569 (void) serial_inp(up, UART_RX);
1570 (void) serial_inp(up, UART_IIR);
1571 (void) serial_inp(up, UART_MSR);
1574 * At this point, there's no way the LSR could still be 0xff;
1575 * if it is, then bail out, because there's likely no UART
1576 * here.
1578 if (!(up->port.flags & UPF_BUGGY_UART) &&
1579 (serial_inp(up, UART_LSR) == 0xff)) {
1580 printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
1581 return -ENODEV;
1585 * For a XR16C850, we need to set the trigger levels
1587 if (up->port.type == PORT_16850) {
1588 unsigned char fctr;
1590 serial_outp(up, UART_LCR, 0xbf);
1592 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
1593 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
1594 serial_outp(up, UART_TRG, UART_TRG_96);
1595 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
1596 serial_outp(up, UART_TRG, UART_TRG_96);
1598 serial_outp(up, UART_LCR, 0);
1602 * If the "interrupt" for this port doesn't correspond with any
1603 * hardware interrupt, we use a timer-based system. The original
1604 * driver used to do this with IRQ0.
1606 if (!is_real_interrupt(up->port.irq)) {
1607 unsigned int timeout = up->port.timeout;
1609 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
1611 up->timer.data = (unsigned long)up;
1612 mod_timer(&up->timer, jiffies + timeout);
1613 } else {
1614 retval = serial_link_irq_chain(up);
1615 if (retval)
1616 return retval;
1620 * Now, initialize the UART
1622 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
1624 spin_lock_irqsave(&up->port.lock, flags);
1625 if (up->port.flags & UPF_FOURPORT) {
1626 if (!is_real_interrupt(up->port.irq))
1627 up->port.mctrl |= TIOCM_OUT1;
1628 } else
1630 * Most PC uarts need OUT2 raised to enable interrupts.
1632 if (is_real_interrupt(up->port.irq))
1633 up->port.mctrl |= TIOCM_OUT2;
1635 serial8250_set_mctrl(&up->port, up->port.mctrl);
1638 * Do a quick test to see if we receive an
1639 * interrupt when we enable the TX irq.
1641 serial_outp(up, UART_IER, UART_IER_THRI);
1642 lsr = serial_in(up, UART_LSR);
1643 iir = serial_in(up, UART_IIR);
1644 serial_outp(up, UART_IER, 0);
1646 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
1647 if (!(up->bugs & UART_BUG_TXEN)) {
1648 up->bugs |= UART_BUG_TXEN;
1649 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
1650 port->line);
1652 } else {
1653 up->bugs &= ~UART_BUG_TXEN;
1656 spin_unlock_irqrestore(&up->port.lock, flags);
1659 * Finally, enable interrupts. Note: Modem status interrupts
1660 * are set via set_termios(), which will be occurring imminently
1661 * anyway, so we don't enable them here.
1663 up->ier = UART_IER_RLSI | UART_IER_RDI;
1664 serial_outp(up, UART_IER, up->ier);
1666 if (up->port.flags & UPF_FOURPORT) {
1667 unsigned int icp;
1669 * Enable interrupts on the AST Fourport board
1671 icp = (up->port.iobase & 0xfe0) | 0x01f;
1672 outb_p(0x80, icp);
1673 (void) inb_p(icp);
1677 * And clear the interrupt registers again for luck.
1679 (void) serial_inp(up, UART_LSR);
1680 (void) serial_inp(up, UART_RX);
1681 (void) serial_inp(up, UART_IIR);
1682 (void) serial_inp(up, UART_MSR);
1684 return 0;
1687 static void serial8250_shutdown(struct uart_port *port)
1689 struct uart_8250_port *up = (struct uart_8250_port *)port;
1690 unsigned long flags;
1693 * Disable interrupts from this port
1695 up->ier = 0;
1696 serial_outp(up, UART_IER, 0);
1698 spin_lock_irqsave(&up->port.lock, flags);
1699 if (up->port.flags & UPF_FOURPORT) {
1700 /* reset interrupts on the AST Fourport board */
1701 inb((up->port.iobase & 0xfe0) | 0x1f);
1702 up->port.mctrl |= TIOCM_OUT1;
1703 } else
1704 up->port.mctrl &= ~TIOCM_OUT2;
1706 serial8250_set_mctrl(&up->port, up->port.mctrl);
1707 spin_unlock_irqrestore(&up->port.lock, flags);
1710 * Disable break condition and FIFOs
1712 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
1713 serial8250_clear_fifos(up);
1715 #ifdef CONFIG_SERIAL_8250_RSA
1717 * Reset the RSA board back to 115kbps compat mode.
1719 disable_rsa(up);
1720 #endif
1723 * Read data port to reset things, and then unlink from
1724 * the IRQ chain.
1726 (void) serial_in(up, UART_RX);
1728 if (!is_real_interrupt(up->port.irq))
1729 del_timer_sync(&up->timer);
1730 else
1731 serial_unlink_irq_chain(up);
1734 static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
1736 unsigned int quot;
1739 * Handle magic divisors for baud rates above baud_base on
1740 * SMSC SuperIO chips.
1742 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1743 baud == (port->uartclk/4))
1744 quot = 0x8001;
1745 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
1746 baud == (port->uartclk/8))
1747 quot = 0x8002;
1748 else
1749 quot = uart_get_divisor(port, baud);
1751 return quot;
1754 static void
1755 serial8250_set_termios(struct uart_port *port, struct termios *termios,
1756 struct termios *old)
1758 struct uart_8250_port *up = (struct uart_8250_port *)port;
1759 unsigned char cval, fcr = 0;
1760 unsigned long flags;
1761 unsigned int baud, quot;
1763 switch (termios->c_cflag & CSIZE) {
1764 case CS5:
1765 cval = UART_LCR_WLEN5;
1766 break;
1767 case CS6:
1768 cval = UART_LCR_WLEN6;
1769 break;
1770 case CS7:
1771 cval = UART_LCR_WLEN7;
1772 break;
1773 default:
1774 case CS8:
1775 cval = UART_LCR_WLEN8;
1776 break;
1779 if (termios->c_cflag & CSTOPB)
1780 cval |= UART_LCR_STOP;
1781 if (termios->c_cflag & PARENB)
1782 cval |= UART_LCR_PARITY;
1783 if (!(termios->c_cflag & PARODD))
1784 cval |= UART_LCR_EPAR;
1785 #ifdef CMSPAR
1786 if (termios->c_cflag & CMSPAR)
1787 cval |= UART_LCR_SPAR;
1788 #endif
1791 * Ask the core to calculate the divisor for us.
1793 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
1794 quot = serial8250_get_divisor(port, baud);
1797 * Oxford Semi 952 rev B workaround
1799 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
1800 quot ++;
1802 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
1803 if (baud < 2400)
1804 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1805 else
1806 fcr = uart_config[up->port.type].fcr;
1810 * MCR-based auto flow control. When AFE is enabled, RTS will be
1811 * deasserted when the receive FIFO contains more characters than
1812 * the trigger, or the MCR RTS bit is cleared. In the case where
1813 * the remote UART is not using CTS auto flow control, we must
1814 * have sufficient FIFO entries for the latency of the remote
1815 * UART to respond. IOW, at least 32 bytes of FIFO.
1817 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
1818 up->mcr &= ~UART_MCR_AFE;
1819 if (termios->c_cflag & CRTSCTS)
1820 up->mcr |= UART_MCR_AFE;
1824 * Ok, we're now changing the port state. Do it with
1825 * interrupts disabled.
1827 spin_lock_irqsave(&up->port.lock, flags);
1830 * Update the per-port timeout.
1832 uart_update_timeout(port, termios->c_cflag, baud);
1834 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1835 if (termios->c_iflag & INPCK)
1836 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1837 if (termios->c_iflag & (BRKINT | PARMRK))
1838 up->port.read_status_mask |= UART_LSR_BI;
1841 * Characteres to ignore
1843 up->port.ignore_status_mask = 0;
1844 if (termios->c_iflag & IGNPAR)
1845 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1846 if (termios->c_iflag & IGNBRK) {
1847 up->port.ignore_status_mask |= UART_LSR_BI;
1849 * If we're ignoring parity and break indicators,
1850 * ignore overruns too (for real raw support).
1852 if (termios->c_iflag & IGNPAR)
1853 up->port.ignore_status_mask |= UART_LSR_OE;
1857 * ignore all characters if CREAD is not set
1859 if ((termios->c_cflag & CREAD) == 0)
1860 up->port.ignore_status_mask |= UART_LSR_DR;
1863 * CTS flow control flag and modem status interrupts
1865 up->ier &= ~UART_IER_MSI;
1866 if (!(up->bugs & UART_BUG_NOMSR) &&
1867 UART_ENABLE_MS(&up->port, termios->c_cflag))
1868 up->ier |= UART_IER_MSI;
1869 if (up->capabilities & UART_CAP_UUE)
1870 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
1872 serial_out(up, UART_IER, up->ier);
1874 if (up->capabilities & UART_CAP_EFR) {
1875 unsigned char efr = 0;
1877 * TI16C752/Startech hardware flow control. FIXME:
1878 * - TI16C752 requires control thresholds to be set.
1879 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
1881 if (termios->c_cflag & CRTSCTS)
1882 efr |= UART_EFR_CTS;
1884 serial_outp(up, UART_LCR, 0xBF);
1885 serial_outp(up, UART_EFR, efr);
1888 if (up->capabilities & UART_NATSEMI) {
1889 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
1890 serial_outp(up, UART_LCR, 0xe0);
1891 } else {
1892 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
1895 serial_dl_write(up, quot);
1898 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
1899 * is written without DLAB set, this mode will be disabled.
1901 if (up->port.type == PORT_16750)
1902 serial_outp(up, UART_FCR, fcr);
1904 serial_outp(up, UART_LCR, cval); /* reset DLAB */
1905 up->lcr = cval; /* Save LCR */
1906 if (up->port.type != PORT_16750) {
1907 if (fcr & UART_FCR_ENABLE_FIFO) {
1908 /* emulated UARTs (Lucent Venus 167x) need two steps */
1909 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1911 serial_outp(up, UART_FCR, fcr); /* set fcr */
1913 serial8250_set_mctrl(&up->port, up->port.mctrl);
1914 spin_unlock_irqrestore(&up->port.lock, flags);
1917 static void
1918 serial8250_pm(struct uart_port *port, unsigned int state,
1919 unsigned int oldstate)
1921 struct uart_8250_port *p = (struct uart_8250_port *)port;
1923 serial8250_set_sleep(p, state != 0);
1925 if (p->pm)
1926 p->pm(port, state, oldstate);
1930 * Resource handling.
1932 static int serial8250_request_std_resource(struct uart_8250_port *up)
1934 unsigned int size = 8 << up->port.regshift;
1935 int ret = 0;
1937 switch (up->port.iotype) {
1938 case UPIO_AU:
1939 size = 0x100000;
1940 /* fall thru */
1941 case UPIO_MEM:
1942 if (!up->port.mapbase)
1943 break;
1945 if (!request_mem_region(up->port.mapbase, size, "serial")) {
1946 ret = -EBUSY;
1947 break;
1950 if (up->port.flags & UPF_IOREMAP) {
1951 up->port.membase = ioremap(up->port.mapbase, size);
1952 if (!up->port.membase) {
1953 release_mem_region(up->port.mapbase, size);
1954 ret = -ENOMEM;
1957 break;
1959 case UPIO_HUB6:
1960 case UPIO_PORT:
1961 if (!request_region(up->port.iobase, size, "serial"))
1962 ret = -EBUSY;
1963 break;
1965 return ret;
1968 static void serial8250_release_std_resource(struct uart_8250_port *up)
1970 unsigned int size = 8 << up->port.regshift;
1972 switch (up->port.iotype) {
1973 case UPIO_AU:
1974 size = 0x100000;
1975 /* fall thru */
1976 case UPIO_MEM:
1977 if (!up->port.mapbase)
1978 break;
1980 if (up->port.flags & UPF_IOREMAP) {
1981 iounmap(up->port.membase);
1982 up->port.membase = NULL;
1985 release_mem_region(up->port.mapbase, size);
1986 break;
1988 case UPIO_HUB6:
1989 case UPIO_PORT:
1990 release_region(up->port.iobase, size);
1991 break;
1995 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
1997 unsigned long start = UART_RSA_BASE << up->port.regshift;
1998 unsigned int size = 8 << up->port.regshift;
1999 int ret = 0;
2001 switch (up->port.iotype) {
2002 case UPIO_MEM:
2003 ret = -EINVAL;
2004 break;
2006 case UPIO_HUB6:
2007 case UPIO_PORT:
2008 start += up->port.iobase;
2009 if (!request_region(start, size, "serial-rsa"))
2010 ret = -EBUSY;
2011 break;
2014 return ret;
2017 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2019 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2020 unsigned int size = 8 << up->port.regshift;
2022 switch (up->port.iotype) {
2023 case UPIO_MEM:
2024 break;
2026 case UPIO_HUB6:
2027 case UPIO_PORT:
2028 release_region(up->port.iobase + offset, size);
2029 break;
2033 static void serial8250_release_port(struct uart_port *port)
2035 struct uart_8250_port *up = (struct uart_8250_port *)port;
2037 serial8250_release_std_resource(up);
2038 if (up->port.type == PORT_RSA)
2039 serial8250_release_rsa_resource(up);
2042 static int serial8250_request_port(struct uart_port *port)
2044 struct uart_8250_port *up = (struct uart_8250_port *)port;
2045 int ret = 0;
2047 ret = serial8250_request_std_resource(up);
2048 if (ret == 0 && up->port.type == PORT_RSA) {
2049 ret = serial8250_request_rsa_resource(up);
2050 if (ret < 0)
2051 serial8250_release_std_resource(up);
2054 return ret;
2057 static void serial8250_config_port(struct uart_port *port, int flags)
2059 struct uart_8250_port *up = (struct uart_8250_port *)port;
2060 int probeflags = PROBE_ANY;
2061 int ret;
2064 * Find the region that we can probe for. This in turn
2065 * tells us whether we can probe for the type of port.
2067 ret = serial8250_request_std_resource(up);
2068 if (ret < 0)
2069 return;
2071 ret = serial8250_request_rsa_resource(up);
2072 if (ret < 0)
2073 probeflags &= ~PROBE_RSA;
2075 if (flags & UART_CONFIG_TYPE)
2076 autoconfig(up, probeflags);
2077 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2078 autoconfig_irq(up);
2080 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2081 serial8250_release_rsa_resource(up);
2082 if (up->port.type == PORT_UNKNOWN)
2083 serial8250_release_std_resource(up);
2086 static int
2087 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2089 if (ser->irq >= NR_IRQS || ser->irq < 0 ||
2090 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2091 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2092 ser->type == PORT_STARTECH)
2093 return -EINVAL;
2094 return 0;
2097 static const char *
2098 serial8250_type(struct uart_port *port)
2100 int type = port->type;
2102 if (type >= ARRAY_SIZE(uart_config))
2103 type = 0;
2104 return uart_config[type].name;
2107 static struct uart_ops serial8250_pops = {
2108 .tx_empty = serial8250_tx_empty,
2109 .set_mctrl = serial8250_set_mctrl,
2110 .get_mctrl = serial8250_get_mctrl,
2111 .stop_tx = serial8250_stop_tx,
2112 .start_tx = serial8250_start_tx,
2113 .stop_rx = serial8250_stop_rx,
2114 .enable_ms = serial8250_enable_ms,
2115 .break_ctl = serial8250_break_ctl,
2116 .startup = serial8250_startup,
2117 .shutdown = serial8250_shutdown,
2118 .set_termios = serial8250_set_termios,
2119 .pm = serial8250_pm,
2120 .type = serial8250_type,
2121 .release_port = serial8250_release_port,
2122 .request_port = serial8250_request_port,
2123 .config_port = serial8250_config_port,
2124 .verify_port = serial8250_verify_port,
2127 static struct uart_8250_port serial8250_ports[UART_NR];
2129 static void __init serial8250_isa_init_ports(void)
2131 struct uart_8250_port *up;
2132 static int first = 1;
2133 int i;
2135 if (!first)
2136 return;
2137 first = 0;
2139 for (i = 0; i < nr_uarts; i++) {
2140 struct uart_8250_port *up = &serial8250_ports[i];
2142 up->port.line = i;
2143 spin_lock_init(&up->port.lock);
2145 init_timer(&up->timer);
2146 up->timer.function = serial8250_timeout;
2149 * ALPHA_KLUDGE_MCR needs to be killed.
2151 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2152 up->mcr_force = ALPHA_KLUDGE_MCR;
2154 up->port.ops = &serial8250_pops;
2157 for (i = 0, up = serial8250_ports;
2158 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
2159 i++, up++) {
2160 up->port.iobase = old_serial_port[i].port;
2161 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
2162 up->port.uartclk = old_serial_port[i].baud_base * 16;
2163 up->port.flags = old_serial_port[i].flags;
2164 up->port.hub6 = old_serial_port[i].hub6;
2165 up->port.membase = old_serial_port[i].iomem_base;
2166 up->port.iotype = old_serial_port[i].io_type;
2167 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2168 if (share_irqs)
2169 up->port.flags |= UPF_SHARE_IRQ;
2173 static void __init
2174 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2176 int i;
2178 serial8250_isa_init_ports();
2180 for (i = 0; i < nr_uarts; i++) {
2181 struct uart_8250_port *up = &serial8250_ports[i];
2183 up->port.dev = dev;
2184 uart_add_one_port(drv, &up->port);
2188 #ifdef CONFIG_SERIAL_8250_CONSOLE
2190 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
2193 * Wait for transmitter & holding register to empty
2195 static inline void wait_for_xmitr(struct uart_8250_port *up, int bits)
2197 unsigned int status, tmout = 10000;
2199 /* Wait up to 10ms for the character(s) to be sent. */
2200 do {
2201 status = serial_in(up, UART_LSR);
2203 if (status & UART_LSR_BI)
2204 up->lsr_break_flag = UART_LSR_BI;
2206 if (--tmout == 0)
2207 break;
2208 udelay(1);
2209 } while ((status & bits) != bits);
2211 /* Wait up to 1s for flow control if necessary */
2212 if (up->port.flags & UPF_CONS_FLOW) {
2213 tmout = 1000000;
2214 while (--tmout &&
2215 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
2216 udelay(1);
2220 static void serial8250_console_putchar(struct uart_port *port, int ch)
2222 struct uart_8250_port *up = (struct uart_8250_port *)port;
2224 wait_for_xmitr(up, UART_LSR_THRE);
2225 serial_out(up, UART_TX, ch);
2229 * Print a string to the serial port trying not to disturb
2230 * any possible real use of the port...
2232 * The console_lock must be held when we get here.
2234 static void
2235 serial8250_console_write(struct console *co, const char *s, unsigned int count)
2237 struct uart_8250_port *up = &serial8250_ports[co->index];
2238 unsigned long flags;
2239 unsigned int ier;
2240 int locked = 1;
2242 touch_nmi_watchdog();
2244 if (oops_in_progress) {
2245 locked = spin_trylock_irqsave(&up->port.lock, flags);
2246 } else
2247 spin_lock_irqsave(&up->port.lock, flags);
2250 * First save the IER then disable the interrupts
2252 ier = serial_in(up, UART_IER);
2254 if (up->capabilities & UART_CAP_UUE)
2255 serial_out(up, UART_IER, UART_IER_UUE);
2256 else
2257 serial_out(up, UART_IER, 0);
2259 uart_console_write(&up->port, s, count, serial8250_console_putchar);
2262 * Finally, wait for transmitter to become empty
2263 * and restore the IER
2265 wait_for_xmitr(up, BOTH_EMPTY);
2266 serial_out(up, UART_IER, ier);
2268 if (locked)
2269 spin_unlock_irqrestore(&up->port.lock, flags);
2272 static int serial8250_console_setup(struct console *co, char *options)
2274 struct uart_port *port;
2275 int baud = 9600;
2276 int bits = 8;
2277 int parity = 'n';
2278 int flow = 'n';
2281 * Check whether an invalid uart number has been specified, and
2282 * if so, search for the first available port that does have
2283 * console support.
2285 if (co->index >= nr_uarts)
2286 co->index = 0;
2287 port = &serial8250_ports[co->index].port;
2288 if (!port->iobase && !port->membase)
2289 return -ENODEV;
2291 if (options)
2292 uart_parse_options(options, &baud, &parity, &bits, &flow);
2294 return uart_set_options(port, co, baud, parity, bits, flow);
2297 static struct uart_driver serial8250_reg;
2298 static struct console serial8250_console = {
2299 .name = "ttyS",
2300 .write = serial8250_console_write,
2301 .device = uart_console_device,
2302 .setup = serial8250_console_setup,
2303 .flags = CON_PRINTBUFFER,
2304 .index = -1,
2305 .data = &serial8250_reg,
2308 static int __init serial8250_console_init(void)
2310 serial8250_isa_init_ports();
2311 register_console(&serial8250_console);
2312 return 0;
2314 console_initcall(serial8250_console_init);
2316 static int __init find_port(struct uart_port *p)
2318 int line;
2319 struct uart_port *port;
2321 for (line = 0; line < nr_uarts; line++) {
2322 port = &serial8250_ports[line].port;
2323 if (uart_match_port(p, port))
2324 return line;
2326 return -ENODEV;
2329 int __init serial8250_start_console(struct uart_port *port, char *options)
2331 int line;
2333 line = find_port(port);
2334 if (line < 0)
2335 return -ENODEV;
2337 add_preferred_console("ttyS", line, options);
2338 printk("Adding console on ttyS%d at %s 0x%lx (options '%s')\n",
2339 line, port->iotype == UPIO_MEM ? "MMIO" : "I/O port",
2340 port->iotype == UPIO_MEM ? (unsigned long) port->mapbase :
2341 (unsigned long) port->iobase, options);
2342 if (!(serial8250_console.flags & CON_ENABLED)) {
2343 serial8250_console.flags &= ~CON_PRINTBUFFER;
2344 register_console(&serial8250_console);
2346 return line;
2349 #define SERIAL8250_CONSOLE &serial8250_console
2350 #else
2351 #define SERIAL8250_CONSOLE NULL
2352 #endif
2354 static struct uart_driver serial8250_reg = {
2355 .owner = THIS_MODULE,
2356 .driver_name = "serial",
2357 .devfs_name = "tts/",
2358 .dev_name = "ttyS",
2359 .major = TTY_MAJOR,
2360 .minor = 64,
2361 .nr = UART_NR,
2362 .cons = SERIAL8250_CONSOLE,
2366 * early_serial_setup - early registration for 8250 ports
2368 * Setup an 8250 port structure prior to console initialisation. Use
2369 * after console initialisation will cause undefined behaviour.
2371 int __init early_serial_setup(struct uart_port *port)
2373 if (port->line >= ARRAY_SIZE(serial8250_ports))
2374 return -ENODEV;
2376 serial8250_isa_init_ports();
2377 serial8250_ports[port->line].port = *port;
2378 serial8250_ports[port->line].port.ops = &serial8250_pops;
2379 return 0;
2383 * serial8250_suspend_port - suspend one serial port
2384 * @line: serial line number
2385 * @level: the level of port suspension, as per uart_suspend_port
2387 * Suspend one serial port.
2389 void serial8250_suspend_port(int line)
2391 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2395 * serial8250_resume_port - resume one serial port
2396 * @line: serial line number
2397 * @level: the level of port resumption, as per uart_resume_port
2399 * Resume one serial port.
2401 void serial8250_resume_port(int line)
2403 uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
2407 * Register a set of serial devices attached to a platform device. The
2408 * list is terminated with a zero flags entry, which means we expect
2409 * all entries to have at least UPF_BOOT_AUTOCONF set.
2411 static int __devinit serial8250_probe(struct platform_device *dev)
2413 struct plat_serial8250_port *p = dev->dev.platform_data;
2414 struct uart_port port;
2415 int ret, i;
2417 memset(&port, 0, sizeof(struct uart_port));
2419 for (i = 0; p && p->flags != 0; p++, i++) {
2420 port.iobase = p->iobase;
2421 port.membase = p->membase;
2422 port.irq = p->irq;
2423 port.uartclk = p->uartclk;
2424 port.regshift = p->regshift;
2425 port.iotype = p->iotype;
2426 port.flags = p->flags;
2427 port.mapbase = p->mapbase;
2428 port.hub6 = p->hub6;
2429 port.dev = &dev->dev;
2430 if (share_irqs)
2431 port.flags |= UPF_SHARE_IRQ;
2432 ret = serial8250_register_port(&port);
2433 if (ret < 0) {
2434 dev_err(&dev->dev, "unable to register port at index %d "
2435 "(IO%lx MEM%lx IRQ%d): %d\n", i,
2436 p->iobase, p->mapbase, p->irq, ret);
2439 return 0;
2443 * Remove serial ports registered against a platform device.
2445 static int __devexit serial8250_remove(struct platform_device *dev)
2447 int i;
2449 for (i = 0; i < nr_uarts; i++) {
2450 struct uart_8250_port *up = &serial8250_ports[i];
2452 if (up->port.dev == &dev->dev)
2453 serial8250_unregister_port(i);
2455 return 0;
2458 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
2460 int i;
2462 for (i = 0; i < UART_NR; i++) {
2463 struct uart_8250_port *up = &serial8250_ports[i];
2465 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
2466 uart_suspend_port(&serial8250_reg, &up->port);
2469 return 0;
2472 static int serial8250_resume(struct platform_device *dev)
2474 int i;
2476 for (i = 0; i < UART_NR; i++) {
2477 struct uart_8250_port *up = &serial8250_ports[i];
2479 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
2480 uart_resume_port(&serial8250_reg, &up->port);
2483 return 0;
2486 static struct platform_driver serial8250_isa_driver = {
2487 .probe = serial8250_probe,
2488 .remove = __devexit_p(serial8250_remove),
2489 .suspend = serial8250_suspend,
2490 .resume = serial8250_resume,
2491 .driver = {
2492 .name = "serial8250",
2493 .owner = THIS_MODULE,
2498 * This "device" covers _all_ ISA 8250-compatible serial devices listed
2499 * in the table in include/asm/serial.h
2501 static struct platform_device *serial8250_isa_devs;
2504 * serial8250_register_port and serial8250_unregister_port allows for
2505 * 16x50 serial ports to be configured at run-time, to support PCMCIA
2506 * modems and PCI multiport cards.
2508 static DEFINE_MUTEX(serial_mutex);
2510 static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
2512 int i;
2515 * First, find a port entry which matches.
2517 for (i = 0; i < nr_uarts; i++)
2518 if (uart_match_port(&serial8250_ports[i].port, port))
2519 return &serial8250_ports[i];
2522 * We didn't find a matching entry, so look for the first
2523 * free entry. We look for one which hasn't been previously
2524 * used (indicated by zero iobase).
2526 for (i = 0; i < nr_uarts; i++)
2527 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
2528 serial8250_ports[i].port.iobase == 0)
2529 return &serial8250_ports[i];
2532 * That also failed. Last resort is to find any entry which
2533 * doesn't have a real port associated with it.
2535 for (i = 0; i < nr_uarts; i++)
2536 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
2537 return &serial8250_ports[i];
2539 return NULL;
2543 * serial8250_register_port - register a serial port
2544 * @port: serial port template
2546 * Configure the serial port specified by the request. If the
2547 * port exists and is in use, it is hung up and unregistered
2548 * first.
2550 * The port is then probed and if necessary the IRQ is autodetected
2551 * If this fails an error is returned.
2553 * On success the port is ready to use and the line number is returned.
2555 int serial8250_register_port(struct uart_port *port)
2557 struct uart_8250_port *uart;
2558 int ret = -ENOSPC;
2560 if (port->uartclk == 0)
2561 return -EINVAL;
2563 mutex_lock(&serial_mutex);
2565 uart = serial8250_find_match_or_unused(port);
2566 if (uart) {
2567 uart_remove_one_port(&serial8250_reg, &uart->port);
2569 uart->port.iobase = port->iobase;
2570 uart->port.membase = port->membase;
2571 uart->port.irq = port->irq;
2572 uart->port.uartclk = port->uartclk;
2573 uart->port.fifosize = port->fifosize;
2574 uart->port.regshift = port->regshift;
2575 uart->port.iotype = port->iotype;
2576 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
2577 uart->port.mapbase = port->mapbase;
2578 if (port->dev)
2579 uart->port.dev = port->dev;
2581 ret = uart_add_one_port(&serial8250_reg, &uart->port);
2582 if (ret == 0)
2583 ret = uart->port.line;
2585 mutex_unlock(&serial_mutex);
2587 return ret;
2589 EXPORT_SYMBOL(serial8250_register_port);
2592 * serial8250_unregister_port - remove a 16x50 serial port at runtime
2593 * @line: serial line number
2595 * Remove one serial port. This may not be called from interrupt
2596 * context. We hand the port back to the our control.
2598 void serial8250_unregister_port(int line)
2600 struct uart_8250_port *uart = &serial8250_ports[line];
2602 mutex_lock(&serial_mutex);
2603 uart_remove_one_port(&serial8250_reg, &uart->port);
2604 if (serial8250_isa_devs) {
2605 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
2606 uart->port.type = PORT_UNKNOWN;
2607 uart->port.dev = &serial8250_isa_devs->dev;
2608 uart_add_one_port(&serial8250_reg, &uart->port);
2609 } else {
2610 uart->port.dev = NULL;
2612 mutex_unlock(&serial_mutex);
2614 EXPORT_SYMBOL(serial8250_unregister_port);
2616 static int __init serial8250_init(void)
2618 int ret, i;
2620 if (nr_uarts > UART_NR)
2621 nr_uarts = UART_NR;
2623 printk(KERN_INFO "Serial: 8250/16550 driver $Revision: 1.90 $ "
2624 "%d ports, IRQ sharing %sabled\n", nr_uarts,
2625 share_irqs ? "en" : "dis");
2627 for (i = 0; i < NR_IRQS; i++)
2628 spin_lock_init(&irq_lists[i].lock);
2630 ret = uart_register_driver(&serial8250_reg);
2631 if (ret)
2632 goto out;
2634 serial8250_isa_devs = platform_device_alloc("serial8250",
2635 PLAT8250_DEV_LEGACY);
2636 if (!serial8250_isa_devs) {
2637 ret = -ENOMEM;
2638 goto unreg_uart_drv;
2641 ret = platform_device_add(serial8250_isa_devs);
2642 if (ret)
2643 goto put_dev;
2645 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
2647 ret = platform_driver_register(&serial8250_isa_driver);
2648 if (ret == 0)
2649 goto out;
2651 platform_device_del(serial8250_isa_devs);
2652 put_dev:
2653 platform_device_put(serial8250_isa_devs);
2654 unreg_uart_drv:
2655 uart_unregister_driver(&serial8250_reg);
2656 out:
2657 return ret;
2660 static void __exit serial8250_exit(void)
2662 struct platform_device *isa_dev = serial8250_isa_devs;
2665 * This tells serial8250_unregister_port() not to re-register
2666 * the ports (thereby making serial8250_isa_driver permanently
2667 * in use.)
2669 serial8250_isa_devs = NULL;
2671 platform_driver_unregister(&serial8250_isa_driver);
2672 platform_device_unregister(isa_dev);
2674 uart_unregister_driver(&serial8250_reg);
2677 module_init(serial8250_init);
2678 module_exit(serial8250_exit);
2680 EXPORT_SYMBOL(serial8250_suspend_port);
2681 EXPORT_SYMBOL(serial8250_resume_port);
2683 MODULE_LICENSE("GPL");
2684 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver $Revision: 1.90 $");
2686 module_param(share_irqs, uint, 0644);
2687 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
2688 " (unsafe)");
2690 module_param(nr_uarts, uint, 0644);
2691 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
2693 #ifdef CONFIG_SERIAL_8250_RSA
2694 module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
2695 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
2696 #endif
2697 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);