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>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/tty.h>
26 #include <linux/ioport.h>
27 #include <linux/init.h>
28 #include <linux/console.h>
29 #include <linux/sysrq.h>
30 #include <linux/serial_reg.h>
31 #include <linux/serial.h>
32 #include <linux/serialP.h>
33 #include <linux/delay.h>
34 #include <linux/device.h>
39 #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
43 #include <linux/serial_core.h>
48 * share_irqs - whether we pass SA_SHIRQ to request_irq(). This option
49 * is unsafe when used on edge-triggered interrupts.
51 unsigned int share_irqs
= SERIAL8250_SHARE_IRQS
;
57 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
59 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
63 #define DEBUG_INTR(fmt...) printk(fmt)
65 #define DEBUG_INTR(fmt...) do { } while (0)
68 #define PASS_LIMIT 256
71 * We default to IRQ0 for the "no irq" hack. Some
72 * machine types want others as well - they're free
73 * to redefine this in their header file.
75 #define is_real_interrupt(irq) ((irq) != 0)
78 * This converts from our new CONFIG_ symbols to the symbols
79 * that asm/serial.h expects. You _NEED_ to comment out the
80 * linux/config.h include contained inside asm/serial.h for
83 #undef CONFIG_SERIAL_MANY_PORTS
84 #undef CONFIG_SERIAL_DETECT_IRQ
85 #undef CONFIG_SERIAL_MULTIPORT
88 #ifdef CONFIG_SERIAL_8250_DETECT_IRQ
89 #define CONFIG_SERIAL_DETECT_IRQ 1
91 #ifdef CONFIG_SERIAL_8250_MULTIPORT
92 #define CONFIG_SERIAL_MULTIPORT 1
94 #ifdef CONFIG_SERIAL_8250_MANY_PORTS
95 #define CONFIG_SERIAL_MANY_PORTS 1
99 * HUB6 is always on. This will be removed once the header
100 * files have been cleaned.
102 #define CONFIG_HUB6 1
104 #include <asm/serial.h>
107 * SERIAL_PORT_DFNS tells us about built-in ports that have no
108 * standard enumeration mechanism. Platforms that can find all
109 * serial ports via mechanisms like ACPI or PCI need not supply it.
111 #ifndef SERIAL_PORT_DFNS
112 #define SERIAL_PORT_DFNS
115 static struct old_serial_port old_serial_port
[] = {
116 SERIAL_PORT_DFNS
/* defined in asm/serial.h */
119 #define UART_NR (ARRAY_SIZE(old_serial_port) + CONFIG_SERIAL_8250_NR_UARTS)
121 #ifdef CONFIG_SERIAL_8250_RSA
123 #define PORT_RSA_MAX 4
124 static unsigned long probe_rsa
[PORT_RSA_MAX
];
125 static unsigned int probe_rsa_count
;
126 #endif /* CONFIG_SERIAL_8250_RSA */
128 struct uart_8250_port
{
129 struct uart_port port
;
130 struct timer_list timer
; /* "no irq" timer */
131 struct list_head list
; /* ports on this IRQ */
132 unsigned int capabilities
; /* port capabilities */
133 unsigned int tx_loadsz
; /* transmit fifo load size */
139 unsigned char mcr_mask
; /* mask of user bits */
140 unsigned char mcr_force
; /* mask of forced bits */
141 unsigned char lsr_break_flag
;
144 * We provide a per-port pm hook.
146 void (*pm
)(struct uart_port
*port
,
147 unsigned int state
, unsigned int old
);
152 struct list_head
*head
;
155 static struct irq_info irq_lists
[NR_IRQS
];
158 * Here we define the default xmit fifo size used for each type of UART.
160 static const struct serial8250_config uart_config
[PORT_MAX_8250
+1] = {
161 { "unknown", 1, 1, 0 },
163 { "16450", 1, 1, 0 },
164 { "16550", 1, 1, 0 },
165 { "16550A", 16, 16, UART_CAP_FIFO
},
166 { "Cirrus", 1, 1, 0 },
167 { "ST16650", 1, 1, UART_CAP_FIFO
| UART_CAP_SLEEP
| UART_CAP_EFR
},
168 { "ST16650V2", 32, 16, UART_CAP_FIFO
| UART_CAP_SLEEP
| UART_CAP_EFR
},
169 { "TI16750", 64, 64, UART_CAP_FIFO
| UART_CAP_SLEEP
},
170 { "Startech", 1, 1, 0 },
171 { "16C950/954", 128, 128, UART_CAP_FIFO
},
172 { "ST16654", 64, 32, UART_CAP_FIFO
| UART_CAP_SLEEP
| UART_CAP_EFR
},
173 { "XR16850", 128, 128, UART_CAP_FIFO
| UART_CAP_SLEEP
| UART_CAP_EFR
},
174 { "RSA", 2048, 2048, UART_CAP_FIFO
},
175 { "NS16550A", 16, 16, UART_CAP_FIFO
| UART_NATSEMI
},
176 { "XScale", 32, 32, UART_CAP_FIFO
},
179 static _INLINE_
unsigned int serial_in(struct uart_8250_port
*up
, int offset
)
181 offset
<<= up
->port
.regshift
;
183 switch (up
->port
.iotype
) {
185 outb(up
->port
.hub6
- 1 + offset
, up
->port
.iobase
);
186 return inb(up
->port
.iobase
+ 1);
189 return readb(up
->port
.membase
+ offset
);
192 return readl(up
->port
.membase
+ offset
);
195 return inb(up
->port
.iobase
+ offset
);
200 serial_out(struct uart_8250_port
*up
, int offset
, int value
)
202 offset
<<= up
->port
.regshift
;
204 switch (up
->port
.iotype
) {
206 outb(up
->port
.hub6
- 1 + offset
, up
->port
.iobase
);
207 outb(value
, up
->port
.iobase
+ 1);
211 writeb(value
, up
->port
.membase
+ offset
);
215 writel(value
, up
->port
.membase
+ offset
);
219 outb(value
, up
->port
.iobase
+ offset
);
224 * We used to support using pause I/O for certain machines. We
225 * haven't supported this for a while, but just in case it's badly
226 * needed for certain old 386 machines, I've left these #define's
229 #define serial_inp(up, offset) serial_in(up, offset)
230 #define serial_outp(up, offset, value) serial_out(up, offset, value)
236 static void serial_icr_write(struct uart_8250_port
*up
, int offset
, int value
)
238 serial_out(up
, UART_SCR
, offset
);
239 serial_out(up
, UART_ICR
, value
);
242 static unsigned int serial_icr_read(struct uart_8250_port
*up
, int offset
)
246 serial_icr_write(up
, UART_ACR
, up
->acr
| UART_ACR_ICRRD
);
247 serial_out(up
, UART_SCR
, offset
);
248 value
= serial_in(up
, UART_ICR
);
249 serial_icr_write(up
, UART_ACR
, up
->acr
);
257 static inline void serial8250_clear_fifos(struct uart_8250_port
*p
)
259 if (p
->capabilities
& UART_CAP_FIFO
) {
260 serial_outp(p
, UART_FCR
, UART_FCR_ENABLE_FIFO
);
261 serial_outp(p
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
262 UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
);
263 serial_outp(p
, UART_FCR
, 0);
268 * IER sleep support. UARTs which have EFRs need the "extended
269 * capability" bit enabled. Note that on XR16C850s, we need to
270 * reset LCR to write to IER.
272 static inline void serial8250_set_sleep(struct uart_8250_port
*p
, int sleep
)
274 if (p
->capabilities
& UART_CAP_SLEEP
) {
275 if (p
->capabilities
& UART_CAP_EFR
) {
276 serial_outp(p
, UART_LCR
, 0xBF);
277 serial_outp(p
, UART_EFR
, UART_EFR_ECB
);
278 serial_outp(p
, UART_LCR
, 0);
280 serial_outp(p
, UART_IER
, sleep
? UART_IERX_SLEEP
: 0);
281 if (p
->capabilities
& UART_CAP_EFR
) {
282 serial_outp(p
, UART_LCR
, 0xBF);
283 serial_outp(p
, UART_EFR
, 0);
284 serial_outp(p
, UART_LCR
, 0);
289 #ifdef CONFIG_SERIAL_8250_RSA
291 * Attempts to turn on the RSA FIFO. Returns zero on failure.
292 * We set the port uart clock rate if we succeed.
294 static int __enable_rsa(struct uart_8250_port
*up
)
299 mode
= serial_inp(up
, UART_RSA_MSR
);
300 result
= mode
& UART_RSA_MSR_FIFO
;
303 serial_outp(up
, UART_RSA_MSR
, mode
| UART_RSA_MSR_FIFO
);
304 mode
= serial_inp(up
, UART_RSA_MSR
);
305 result
= mode
& UART_RSA_MSR_FIFO
;
309 up
->port
.uartclk
= SERIAL_RSA_BAUD_BASE
* 16;
314 static void enable_rsa(struct uart_8250_port
*up
)
316 if (up
->port
.type
== PORT_RSA
) {
317 if (up
->port
.uartclk
!= SERIAL_RSA_BAUD_BASE
* 16) {
318 spin_lock_irq(&up
->port
.lock
);
320 spin_unlock_irq(&up
->port
.lock
);
322 if (up
->port
.uartclk
== SERIAL_RSA_BAUD_BASE
* 16)
323 serial_outp(up
, UART_RSA_FRR
, 0);
328 * Attempts to turn off the RSA FIFO. Returns zero on failure.
329 * It is unknown why interrupts were disabled in here. However,
330 * the caller is expected to preserve this behaviour by grabbing
331 * the spinlock before calling this function.
333 static void disable_rsa(struct uart_8250_port
*up
)
338 if (up
->port
.type
== PORT_RSA
&&
339 up
->port
.uartclk
== SERIAL_RSA_BAUD_BASE
* 16) {
340 spin_lock_irq(&up
->port
.lock
);
342 mode
= serial_inp(up
, UART_RSA_MSR
);
343 result
= !(mode
& UART_RSA_MSR_FIFO
);
346 serial_outp(up
, UART_RSA_MSR
, mode
& ~UART_RSA_MSR_FIFO
);
347 mode
= serial_inp(up
, UART_RSA_MSR
);
348 result
= !(mode
& UART_RSA_MSR_FIFO
);
352 up
->port
.uartclk
= SERIAL_RSA_BAUD_BASE_LO
* 16;
353 spin_unlock_irq(&up
->port
.lock
);
356 #endif /* CONFIG_SERIAL_8250_RSA */
359 * This is a quickie test to see how big the FIFO is.
360 * It doesn't work at all the time, more's the pity.
362 static int size_fifo(struct uart_8250_port
*up
)
364 unsigned char old_fcr
, old_mcr
, old_dll
, old_dlm
;
367 old_fcr
= serial_inp(up
, UART_FCR
);
368 old_mcr
= serial_inp(up
, UART_MCR
);
369 serial_outp(up
, UART_FCR
, UART_FCR_ENABLE_FIFO
|
370 UART_FCR_CLEAR_RCVR
| UART_FCR_CLEAR_XMIT
);
371 serial_outp(up
, UART_MCR
, UART_MCR_LOOP
);
372 serial_outp(up
, UART_LCR
, UART_LCR_DLAB
);
373 old_dll
= serial_inp(up
, UART_DLL
);
374 old_dlm
= serial_inp(up
, UART_DLM
);
375 serial_outp(up
, UART_DLL
, 0x01);
376 serial_outp(up
, UART_DLM
, 0x00);
377 serial_outp(up
, UART_LCR
, 0x03);
378 for (count
= 0; count
< 256; count
++)
379 serial_outp(up
, UART_TX
, count
);
380 mdelay(20);/* FIXME - schedule_timeout */
381 for (count
= 0; (serial_inp(up
, UART_LSR
) & UART_LSR_DR
) &&
382 (count
< 256); count
++)
383 serial_inp(up
, UART_RX
);
384 serial_outp(up
, UART_FCR
, old_fcr
);
385 serial_outp(up
, UART_MCR
, old_mcr
);
386 serial_outp(up
, UART_LCR
, UART_LCR_DLAB
);
387 serial_outp(up
, UART_DLL
, old_dll
);
388 serial_outp(up
, UART_DLM
, old_dlm
);
394 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
395 * When this function is called we know it is at least a StarTech
396 * 16650 V2, but it might be one of several StarTech UARTs, or one of
397 * its clones. (We treat the broken original StarTech 16650 V1 as a
398 * 16550, and why not? Startech doesn't seem to even acknowledge its
401 * What evil have men's minds wrought...
403 static void autoconfig_has_efr(struct uart_8250_port
*up
)
405 unsigned char id1
, id2
, id3
, rev
, saved_dll
, saved_dlm
;
408 * First we check to see if it's an Oxford Semiconductor UART.
410 * If we have to do this here because some non-National
411 * Semiconductor clone chips lock up if you try writing to the
412 * LSR register (which serial_icr_read does)
416 * Check for Oxford Semiconductor 16C950.
418 * EFR [4] must be set else this test fails.
420 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
421 * claims that it's needed for 952 dual UART's (which are not
422 * recommended for new designs).
425 serial_out(up
, UART_LCR
, 0xBF);
426 serial_out(up
, UART_EFR
, UART_EFR_ECB
);
427 serial_out(up
, UART_LCR
, 0x00);
428 id1
= serial_icr_read(up
, UART_ID1
);
429 id2
= serial_icr_read(up
, UART_ID2
);
430 id3
= serial_icr_read(up
, UART_ID3
);
431 rev
= serial_icr_read(up
, UART_REV
);
433 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1
, id2
, id3
, rev
);
435 if (id1
== 0x16 && id2
== 0xC9 &&
436 (id3
== 0x50 || id3
== 0x52 || id3
== 0x54)) {
437 up
->port
.type
= PORT_16C950
;
438 up
->rev
= rev
| (id3
<< 8);
443 * We check for a XR16C850 by setting DLL and DLM to 0, and then
444 * reading back DLL and DLM. The chip type depends on the DLM
446 * 0x10 - XR16C850 and the DLL contains the chip revision.
450 serial_outp(up
, UART_LCR
, UART_LCR_DLAB
);
451 saved_dll
= serial_inp(up
, UART_DLL
);
452 saved_dlm
= serial_inp(up
, UART_DLM
);
453 serial_outp(up
, UART_DLL
, 0);
454 serial_outp(up
, UART_DLM
, 0);
455 id2
= serial_inp(up
, UART_DLL
);
456 id1
= serial_inp(up
, UART_DLM
);
457 serial_outp(up
, UART_DLL
, saved_dll
);
458 serial_outp(up
, UART_DLM
, saved_dlm
);
460 DEBUG_AUTOCONF("850id=%02x:%02x ", id1
, id2
);
462 if (id1
== 0x10 || id1
== 0x12 || id1
== 0x14) {
465 up
->port
.type
= PORT_16850
;
470 * It wasn't an XR16C850.
472 * We distinguish between the '654 and the '650 by counting
473 * how many bytes are in the FIFO. I'm using this for now,
474 * since that's the technique that was sent to me in the
475 * serial driver update, but I'm not convinced this works.
476 * I've had problems doing this in the past. -TYT
478 if (size_fifo(up
) == 64)
479 up
->port
.type
= PORT_16654
;
481 up
->port
.type
= PORT_16650V2
;
485 * We detected a chip without a FIFO. Only two fall into
486 * this category - the original 8250 and the 16450. The
487 * 16450 has a scratch register (accessible with LCR=0)
489 static void autoconfig_8250(struct uart_8250_port
*up
)
491 unsigned char scratch
, status1
, status2
;
493 up
->port
.type
= PORT_8250
;
495 scratch
= serial_in(up
, UART_SCR
);
496 serial_outp(up
, UART_SCR
, 0xa5);
497 status1
= serial_in(up
, UART_SCR
);
498 serial_outp(up
, UART_SCR
, 0x5a);
499 status2
= serial_in(up
, UART_SCR
);
500 serial_outp(up
, UART_SCR
, scratch
);
502 if (status1
== 0xa5 && status2
== 0x5a)
503 up
->port
.type
= PORT_16450
;
507 * We know that the chip has FIFOs. Does it have an EFR? The
508 * EFR is located in the same register position as the IIR and
509 * we know the top two bits of the IIR are currently set. The
510 * EFR should contain zero. Try to read the EFR.
512 static void autoconfig_16550a(struct uart_8250_port
*up
)
514 unsigned char status1
, status2
;
516 up
->port
.type
= PORT_16550A
;
518 #ifdef CONFIG_ARCH_IXP425
520 * Need to determine some way to detect XSCALE/IXP425 uarts here...
522 up
->port
.type
= PORT_XSCALE
;
527 * Check for presence of the EFR when DLAB is set.
528 * Only ST16C650V1 UARTs pass this test.
530 serial_outp(up
, UART_LCR
, UART_LCR_DLAB
);
531 if (serial_in(up
, UART_EFR
) == 0) {
532 serial_outp(up
, UART_EFR
, 0xA8);
533 if (serial_in(up
, UART_EFR
) != 0) {
534 DEBUG_AUTOCONF("EFRv1 ");
535 up
->port
.type
= PORT_16650
;
537 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
539 serial_outp(up
, UART_EFR
, 0);
544 * Maybe it requires 0xbf to be written to the LCR.
545 * (other ST16C650V2 UARTs, TI16C752A, etc)
547 serial_outp(up
, UART_LCR
, 0xBF);
548 if (serial_in(up
, UART_EFR
) == 0) {
549 DEBUG_AUTOCONF("EFRv2 ");
550 autoconfig_has_efr(up
);
555 * Check for a National Semiconductor SuperIO chip.
556 * Attempt to switch to bank 2, read the value of the LOOP bit
557 * from EXCR1. Switch back to bank 0, change it in MCR. Then
558 * switch back to bank 2, read it from EXCR1 again and check
559 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
560 * On PowerPC we don't want to change baud_base, as we have
561 * a number of different divisors. -- Tom Rini
563 serial_outp(up
, UART_LCR
, 0);
564 status1
= serial_in(up
, UART_MCR
);
565 serial_outp(up
, UART_LCR
, 0xE0);
566 status2
= serial_in(up
, 0x02); /* EXCR1 */
568 if (!((status2
^ status1
) & UART_MCR_LOOP
)) {
569 serial_outp(up
, UART_LCR
, 0);
570 serial_outp(up
, UART_MCR
, status1
^ UART_MCR_LOOP
);
571 serial_outp(up
, UART_LCR
, 0xE0);
572 status2
= serial_in(up
, 0x02); /* EXCR1 */
573 serial_outp(up
, UART_LCR
, 0);
574 serial_outp(up
, UART_MCR
, status1
);
576 if ((status2
^ status1
) & UART_MCR_LOOP
) {
578 serial_outp(up
, UART_LCR
, 0xE0);
579 status1
= serial_in(up
, 0x04); /* EXCR1 */
580 status1
&= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
581 status1
|= 0x10; /* 1.625 divisor for baud_base --> 921600 */
582 serial_outp(up
, 0x04, status1
);
583 serial_outp(up
, UART_LCR
, 0);
584 up
->port
.uartclk
= 921600*16;
587 up
->port
.type
= PORT_NS16550A
;
593 * No EFR. Try to detect a TI16750, which only sets bit 5 of
594 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
595 * Try setting it with and without DLAB set. Cheap clones
596 * set bit 5 without DLAB set.
598 serial_outp(up
, UART_LCR
, 0);
599 serial_outp(up
, UART_FCR
, UART_FCR_ENABLE_FIFO
| UART_FCR7_64BYTE
);
600 status1
= serial_in(up
, UART_IIR
) >> 5;
601 serial_outp(up
, UART_FCR
, UART_FCR_ENABLE_FIFO
);
602 serial_outp(up
, UART_LCR
, UART_LCR_DLAB
);
603 serial_outp(up
, UART_FCR
, UART_FCR_ENABLE_FIFO
| UART_FCR7_64BYTE
);
604 status2
= serial_in(up
, UART_IIR
) >> 5;
605 serial_outp(up
, UART_FCR
, UART_FCR_ENABLE_FIFO
);
607 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1
, status2
);
609 if (status1
== 6 && status2
== 7) {
610 up
->port
.type
= PORT_16750
;
616 * This routine is called by rs_init() to initialize a specific serial
617 * port. It determines what type of UART chip this serial port is
618 * using: 8250, 16450, 16550, 16550A. The important question is
619 * whether or not this UART is a 16550A or not, since this will
620 * determine whether or not we can use its FIFO features or not.
622 static void autoconfig(struct uart_8250_port
*up
, unsigned int probeflags
)
624 unsigned char status1
, scratch
, scratch2
, scratch3
;
625 unsigned char save_lcr
, save_mcr
;
628 if (!up
->port
.iobase
&& !up
->port
.mapbase
&& !up
->port
.membase
)
631 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ",
632 up
->port
.line
, up
->port
.iobase
, up
->port
.membase
);
635 * We really do need global IRQs disabled here - we're going to
636 * be frobbing the chips IRQ enable register to see if it exists.
638 spin_lock_irqsave(&up
->port
.lock
, flags
);
639 // save_flags(flags); cli();
641 if (!(up
->port
.flags
& UPF_BUGGY_UART
)) {
643 * Do a simple existence test first; if we fail this,
644 * there's no point trying anything else.
646 * 0x80 is used as a nonsense port to prevent against
647 * false positives due to ISA bus float. The
648 * assumption is that 0x80 is a non-existent port;
649 * which should be safe since include/asm/io.h also
650 * makes this assumption.
652 * Note: this is safe as long as MCR bit 4 is clear
653 * and the device is in "PC" mode.
655 scratch
= serial_inp(up
, UART_IER
);
656 serial_outp(up
, UART_IER
, 0);
660 scratch2
= serial_inp(up
, UART_IER
);
661 serial_outp(up
, UART_IER
, 0x0F);
665 scratch3
= serial_inp(up
, UART_IER
);
666 serial_outp(up
, UART_IER
, scratch
);
667 if (scratch2
!= 0 || scratch3
!= 0x0F) {
669 * We failed; there's nothing here
671 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
677 save_mcr
= serial_in(up
, UART_MCR
);
678 save_lcr
= serial_in(up
, UART_LCR
);
681 * Check to see if a UART is really there. Certain broken
682 * internal modems based on the Rockwell chipset fail this
683 * test, because they apparently don't implement the loopback
684 * test mode. So this test is skipped on the COM 1 through
685 * COM 4 ports. This *should* be safe, since no board
686 * manufacturer would be stupid enough to design a board
687 * that conflicts with COM 1-4 --- we hope!
689 if (!(up
->port
.flags
& UPF_SKIP_TEST
)) {
690 serial_outp(up
, UART_MCR
, UART_MCR_LOOP
| 0x0A);
691 status1
= serial_inp(up
, UART_MSR
) & 0xF0;
692 serial_outp(up
, UART_MCR
, save_mcr
);
693 if (status1
!= 0x90) {
694 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
701 * We're pretty sure there's a port here. Lets find out what
702 * type of port it is. The IIR top two bits allows us to find
703 * out if its 8250 or 16450, 16550, 16550A or later. This
704 * determines what we test for next.
706 * We also initialise the EFR (if any) to zero for later. The
707 * EFR occupies the same register location as the FCR and IIR.
709 serial_outp(up
, UART_LCR
, 0xBF);
710 serial_outp(up
, UART_EFR
, 0);
711 serial_outp(up
, UART_LCR
, 0);
713 serial_outp(up
, UART_FCR
, UART_FCR_ENABLE_FIFO
);
714 scratch
= serial_in(up
, UART_IIR
) >> 6;
716 DEBUG_AUTOCONF("iir=%d ", scratch
);
723 up
->port
.type
= PORT_UNKNOWN
;
726 up
->port
.type
= PORT_16550
;
729 autoconfig_16550a(up
);
733 #ifdef CONFIG_SERIAL_8250_RSA
735 * Only probe for RSA ports if we got the region.
737 if (up
->port
.type
== PORT_16550A
&& probeflags
& PROBE_RSA
) {
740 for (i
= 0 ; i
< probe_rsa_count
; ++i
) {
741 if (probe_rsa
[i
] == up
->port
.iobase
&&
743 up
->port
.type
= PORT_RSA
;
749 serial_outp(up
, UART_LCR
, save_lcr
);
751 up
->port
.fifosize
= uart_config
[up
->port
.type
].fifo_size
;
752 up
->capabilities
= uart_config
[up
->port
.type
].flags
;
753 up
->tx_loadsz
= uart_config
[up
->port
.type
].tx_loadsz
;
755 if (up
->port
.type
== PORT_UNKNOWN
)
761 #ifdef CONFIG_SERIAL_8250_RSA
762 if (up
->port
.type
== PORT_RSA
)
763 serial_outp(up
, UART_RSA_FRR
, 0);
765 serial_outp(up
, UART_MCR
, save_mcr
);
766 serial8250_clear_fifos(up
);
767 (void)serial_in(up
, UART_RX
);
768 serial_outp(up
, UART_IER
, 0);
771 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
772 // restore_flags(flags);
773 DEBUG_AUTOCONF("type=%s\n", uart_config
[up
->port
.type
].name
);
776 static void autoconfig_irq(struct uart_8250_port
*up
)
778 unsigned char save_mcr
, save_ier
;
779 unsigned char save_ICP
= 0;
780 unsigned int ICP
= 0;
784 if (up
->port
.flags
& UPF_FOURPORT
) {
785 ICP
= (up
->port
.iobase
& 0xfe0) | 0x1f;
786 save_ICP
= inb_p(ICP
);
791 /* forget possible initially masked and pending IRQ */
792 probe_irq_off(probe_irq_on());
793 save_mcr
= serial_inp(up
, UART_MCR
);
794 save_ier
= serial_inp(up
, UART_IER
);
795 serial_outp(up
, UART_MCR
, UART_MCR_OUT1
| UART_MCR_OUT2
);
797 irqs
= probe_irq_on();
798 serial_outp(up
, UART_MCR
, 0);
800 if (up
->port
.flags
& UPF_FOURPORT
) {
801 serial_outp(up
, UART_MCR
,
802 UART_MCR_DTR
| UART_MCR_RTS
);
804 serial_outp(up
, UART_MCR
,
805 UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
);
807 serial_outp(up
, UART_IER
, 0x0f); /* enable all intrs */
808 (void)serial_inp(up
, UART_LSR
);
809 (void)serial_inp(up
, UART_RX
);
810 (void)serial_inp(up
, UART_IIR
);
811 (void)serial_inp(up
, UART_MSR
);
812 serial_outp(up
, UART_TX
, 0xFF);
814 irq
= probe_irq_off(irqs
);
816 serial_outp(up
, UART_MCR
, save_mcr
);
817 serial_outp(up
, UART_IER
, save_ier
);
819 if (up
->port
.flags
& UPF_FOURPORT
)
820 outb_p(save_ICP
, ICP
);
822 up
->port
.irq
= (irq
> 0) ? irq
: 0;
825 static void serial8250_stop_tx(struct uart_port
*port
, unsigned int tty_stop
)
827 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
829 if (up
->ier
& UART_IER_THRI
) {
830 up
->ier
&= ~UART_IER_THRI
;
831 serial_out(up
, UART_IER
, up
->ier
);
833 if (up
->port
.type
== PORT_16C950
&& tty_stop
) {
834 up
->acr
|= UART_ACR_TXDIS
;
835 serial_icr_write(up
, UART_ACR
, up
->acr
);
839 static void serial8250_start_tx(struct uart_port
*port
, unsigned int tty_start
)
841 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
843 if (!(up
->ier
& UART_IER_THRI
)) {
844 up
->ier
|= UART_IER_THRI
;
845 serial_out(up
, UART_IER
, up
->ier
);
848 * We only do this from uart_start
850 if (tty_start
&& up
->port
.type
== PORT_16C950
) {
851 up
->acr
&= ~UART_ACR_TXDIS
;
852 serial_icr_write(up
, UART_ACR
, up
->acr
);
856 static void serial8250_stop_rx(struct uart_port
*port
)
858 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
860 up
->ier
&= ~UART_IER_RLSI
;
861 up
->port
.read_status_mask
&= ~UART_LSR_DR
;
862 serial_out(up
, UART_IER
, up
->ier
);
865 static void serial8250_enable_ms(struct uart_port
*port
)
867 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
869 up
->ier
|= UART_IER_MSI
;
870 serial_out(up
, UART_IER
, up
->ier
);
874 receive_chars(struct uart_8250_port
*up
, int *status
, struct pt_regs
*regs
)
876 struct tty_struct
*tty
= up
->port
.info
->tty
;
881 if (unlikely(tty
->flip
.count
>= TTY_FLIPBUF_SIZE
)) {
882 tty
->flip
.work
.func((void *)tty
);
883 if (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
)
884 return; // if TTY_DONT_FLIP is set
886 ch
= serial_inp(up
, UART_RX
);
887 *tty
->flip
.char_buf_ptr
= ch
;
888 *tty
->flip
.flag_buf_ptr
= TTY_NORMAL
;
889 up
->port
.icount
.rx
++;
891 if (unlikely(*status
& (UART_LSR_BI
| UART_LSR_PE
|
892 UART_LSR_FE
| UART_LSR_OE
))) {
894 * For statistics only
896 if (*status
& UART_LSR_BI
) {
897 *status
&= ~(UART_LSR_FE
| UART_LSR_PE
);
898 up
->port
.icount
.brk
++;
900 * We do the SysRQ and SAK checking
901 * here because otherwise the break
902 * may get masked by ignore_status_mask
903 * or read_status_mask.
905 if (uart_handle_break(&up
->port
))
907 } else if (*status
& UART_LSR_PE
)
908 up
->port
.icount
.parity
++;
909 else if (*status
& UART_LSR_FE
)
910 up
->port
.icount
.frame
++;
911 if (*status
& UART_LSR_OE
)
912 up
->port
.icount
.overrun
++;
915 * Mask off conditions which should be ingored.
917 *status
&= up
->port
.read_status_mask
;
919 #ifdef CONFIG_SERIAL_8250_CONSOLE
920 if (up
->port
.line
== up
->port
.cons
->index
) {
921 /* Recover the break flag from console xmit */
922 *status
|= up
->lsr_break_flag
;
923 up
->lsr_break_flag
= 0;
926 if (*status
& UART_LSR_BI
) {
927 DEBUG_INTR("handling break....");
928 *tty
->flip
.flag_buf_ptr
= TTY_BREAK
;
929 } else if (*status
& UART_LSR_PE
)
930 *tty
->flip
.flag_buf_ptr
= TTY_PARITY
;
931 else if (*status
& UART_LSR_FE
)
932 *tty
->flip
.flag_buf_ptr
= TTY_FRAME
;
934 if (uart_handle_sysrq_char(&up
->port
, ch
, regs
))
936 if ((*status
& up
->port
.ignore_status_mask
) == 0) {
937 tty
->flip
.flag_buf_ptr
++;
938 tty
->flip
.char_buf_ptr
++;
941 if ((*status
& UART_LSR_OE
) &&
942 tty
->flip
.count
< TTY_FLIPBUF_SIZE
) {
944 * Overrun is special, since it's reported
945 * immediately, and doesn't affect the current
948 *tty
->flip
.flag_buf_ptr
= TTY_OVERRUN
;
949 tty
->flip
.flag_buf_ptr
++;
950 tty
->flip
.char_buf_ptr
++;
954 *status
= serial_inp(up
, UART_LSR
);
955 } while ((*status
& UART_LSR_DR
) && (max_count
-- > 0));
956 tty_flip_buffer_push(tty
);
959 static _INLINE_
void transmit_chars(struct uart_8250_port
*up
)
961 struct circ_buf
*xmit
= &up
->port
.info
->xmit
;
964 if (up
->port
.x_char
) {
965 serial_outp(up
, UART_TX
, up
->port
.x_char
);
966 up
->port
.icount
.tx
++;
970 if (uart_circ_empty(xmit
) || uart_tx_stopped(&up
->port
)) {
971 serial8250_stop_tx(&up
->port
, 0);
975 count
= up
->tx_loadsz
;
977 serial_out(up
, UART_TX
, xmit
->buf
[xmit
->tail
]);
978 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
979 up
->port
.icount
.tx
++;
980 if (uart_circ_empty(xmit
))
982 } while (--count
> 0);
984 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
985 uart_write_wakeup(&up
->port
);
987 DEBUG_INTR("THRE...");
989 if (uart_circ_empty(xmit
))
990 serial8250_stop_tx(&up
->port
, 0);
993 static _INLINE_
void check_modem_status(struct uart_8250_port
*up
)
997 status
= serial_in(up
, UART_MSR
);
999 if ((status
& UART_MSR_ANY_DELTA
) == 0)
1002 if (status
& UART_MSR_TERI
)
1003 up
->port
.icount
.rng
++;
1004 if (status
& UART_MSR_DDSR
)
1005 up
->port
.icount
.dsr
++;
1006 if (status
& UART_MSR_DDCD
) {
1007 //printk("8250 serial status= 0x%x, iobase=0x%x\n", status, up->port.iobase);
1008 uart_handle_dcd_change(&up
->port
, status
& UART_MSR_DCD
);
1011 if (status
& UART_MSR_DCTS
)
1012 uart_handle_cts_change(&up
->port
, status
& UART_MSR_CTS
);
1014 wake_up_interruptible(&up
->port
.info
->delta_msr_wait
);
1018 * This handles the interrupt from one port.
1021 serial8250_handle_port(struct uart_8250_port
*up
, struct pt_regs
*regs
)
1023 unsigned int status
= serial_inp(up
, UART_LSR
);
1025 DEBUG_INTR("status = %x...", status
);
1027 if (status
& UART_LSR_DR
)
1028 receive_chars(up
, &status
, regs
);
1029 check_modem_status(up
);
1030 if (status
& UART_LSR_THRE
)
1035 * This is the serial driver's interrupt routine.
1037 * Arjan thinks the old way was overly complex, so it got simplified.
1038 * Alan disagrees, saying that need the complexity to handle the weird
1039 * nature of ISA shared interrupts. (This is a special exception.)
1041 * In order to handle ISA shared interrupts properly, we need to check
1042 * that all ports have been serviced, and therefore the ISA interrupt
1043 * line has been de-asserted.
1045 * This means we need to loop through all ports. checking that they
1046 * don't have an interrupt pending.
1048 static irqreturn_t
serial8250_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
1050 struct irq_info
*i
= dev_id
;
1051 struct list_head
*l
, *end
= NULL
;
1052 int pass_counter
= 0;
1054 DEBUG_INTR("serial8250_interrupt(%d)...", irq
);
1056 spin_lock(&i
->lock
);
1060 struct uart_8250_port
*up
;
1063 up
= list_entry(l
, struct uart_8250_port
, list
);
1065 iir
= serial_in(up
, UART_IIR
);
1066 if (!(iir
& UART_IIR_NO_INT
)) {
1067 spin_lock(&up
->port
.lock
);
1068 serial8250_handle_port(up
, regs
);
1069 spin_unlock(&up
->port
.lock
);
1072 } else if (end
== NULL
)
1077 if (l
== i
->head
&& pass_counter
++ > PASS_LIMIT
) {
1078 /* If we hit this, we're dead. */
1079 printk(KERN_ERR
"serial8250: too much work for "
1085 spin_unlock(&i
->lock
);
1087 DEBUG_INTR("end.\n");
1088 /* FIXME! Was it really ours? */
1093 * To support ISA shared interrupts, we need to have one interrupt
1094 * handler that ensures that the IRQ line has been deasserted
1095 * before returning. Failing to do this will result in the IRQ
1096 * line being stuck active, and, since ISA irqs are edge triggered,
1097 * no more IRQs will be seen.
1099 static void serial_do_unlink(struct irq_info
*i
, struct uart_8250_port
*up
)
1101 spin_lock_irq(&i
->lock
);
1103 if (!list_empty(i
->head
)) {
1104 if (i
->head
== &up
->list
)
1105 i
->head
= i
->head
->next
;
1106 list_del(&up
->list
);
1108 BUG_ON(i
->head
!= &up
->list
);
1112 spin_unlock_irq(&i
->lock
);
1115 static int serial_link_irq_chain(struct uart_8250_port
*up
)
1117 struct irq_info
*i
= irq_lists
+ up
->port
.irq
;
1118 int ret
, irq_flags
= up
->port
.flags
& UPF_SHARE_IRQ
? SA_SHIRQ
: 0;
1120 spin_lock_irq(&i
->lock
);
1123 list_add(&up
->list
, i
->head
);
1124 spin_unlock_irq(&i
->lock
);
1128 INIT_LIST_HEAD(&up
->list
);
1129 i
->head
= &up
->list
;
1130 spin_unlock_irq(&i
->lock
);
1132 #if 1 // add by Victor Yu. 06-02-2005
1133 #ifdef CONFIG_ARCH_MOXACPU
1134 extern void cpe_int_set_irq(unsigned int irq
, int mode
, int level
);
1135 cpe_int_set_irq(up
->port
.irq
, LEVEL
, H_ACTIVE
);
1136 #endif // CONFIG_ARCH_MOXACPU
1138 ret
= request_irq(up
->port
.irq
, serial8250_interrupt
,
1139 irq_flags
, "serial", i
);
1141 serial_do_unlink(i
, up
);
1147 static void serial_unlink_irq_chain(struct uart_8250_port
*up
)
1149 struct irq_info
*i
= irq_lists
+ up
->port
.irq
;
1151 BUG_ON(i
->head
== NULL
);
1153 if (list_empty(i
->head
))
1154 free_irq(up
->port
.irq
, i
);
1156 serial_do_unlink(i
, up
);
1160 * This function is used to handle ports that do not have an
1161 * interrupt. This doesn't work very well for 16450's, but gives
1162 * barely passable results for a 16550A. (Although at the expense
1163 * of much CPU overhead).
1165 static void serial8250_timeout(unsigned long data
)
1167 struct uart_8250_port
*up
= (struct uart_8250_port
*)data
;
1168 unsigned int timeout
;
1171 iir
= serial_in(up
, UART_IIR
);
1172 if (!(iir
& UART_IIR_NO_INT
)) {
1173 spin_lock(&up
->port
.lock
);
1174 serial8250_handle_port(up
, NULL
);
1175 spin_unlock(&up
->port
.lock
);
1178 timeout
= up
->port
.timeout
;
1179 timeout
= timeout
> 6 ? (timeout
/ 2 - 2) : 1;
1180 mod_timer(&up
->timer
, jiffies
+ timeout
);
1183 static unsigned int serial8250_tx_empty(struct uart_port
*port
)
1185 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
1186 unsigned long flags
;
1189 spin_lock_irqsave(&up
->port
.lock
, flags
);
1190 ret
= serial_in(up
, UART_LSR
) & UART_LSR_TEMT
? TIOCSER_TEMT
: 0;
1191 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
1196 static unsigned int serial8250_get_mctrl(struct uart_port
*port
)
1198 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
1199 unsigned long flags
;
1200 unsigned char status
;
1203 spin_lock_irqsave(&up
->port
.lock
, flags
);
1204 status
= serial_in(up
, UART_MSR
);
1205 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
1208 if (status
& UART_MSR_DCD
)
1210 if (status
& UART_MSR_RI
)
1212 if (status
& UART_MSR_DSR
)
1214 if (status
& UART_MSR_CTS
)
1219 static void serial8250_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
1221 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
1222 unsigned char mcr
= 0;
1224 if (mctrl
& TIOCM_RTS
)
1225 mcr
|= UART_MCR_RTS
;
1226 if (mctrl
& TIOCM_DTR
)
1227 mcr
|= UART_MCR_DTR
;
1228 if (mctrl
& TIOCM_OUT1
)
1229 mcr
|= UART_MCR_OUT1
;
1230 if (mctrl
& TIOCM_OUT2
)
1231 mcr
|= UART_MCR_OUT2
;
1232 if (mctrl
& TIOCM_LOOP
)
1233 mcr
|= UART_MCR_LOOP
;
1235 mcr
= (mcr
& up
->mcr_mask
) | up
->mcr_force
| up
->mcr
;
1237 serial_out(up
, UART_MCR
, mcr
);
1240 static void serial8250_break_ctl(struct uart_port
*port
, int break_state
)
1242 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
1243 unsigned long flags
;
1245 spin_lock_irqsave(&up
->port
.lock
, flags
);
1246 if (break_state
== -1)
1247 up
->lcr
|= UART_LCR_SBC
;
1249 up
->lcr
&= ~UART_LCR_SBC
;
1250 serial_out(up
, UART_LCR
, up
->lcr
);
1251 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
1254 static int serial8250_startup(struct uart_port
*port
)
1256 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
1257 unsigned long flags
;
1260 up
->capabilities
= uart_config
[up
->port
.type
].flags
;
1263 if (up
->port
.type
== PORT_16C950
) {
1264 /* Wake up and initialize UART */
1266 serial_outp(up
, UART_LCR
, 0xBF);
1267 serial_outp(up
, UART_EFR
, UART_EFR_ECB
);
1268 serial_outp(up
, UART_IER
, 0);
1269 serial_outp(up
, UART_LCR
, 0);
1270 serial_icr_write(up
, UART_CSR
, 0); /* Reset the UART */
1271 serial_outp(up
, UART_LCR
, 0xBF);
1272 serial_outp(up
, UART_EFR
, UART_EFR_ECB
);
1273 serial_outp(up
, UART_LCR
, 0);
1276 #ifdef CONFIG_SERIAL_8250_RSA
1278 * If this is an RSA port, see if we can kick it up to the
1279 * higher speed clock.
1285 * Clear the FIFO buffers and disable them.
1286 * (they will be reeanbled in set_termios())
1288 serial8250_clear_fifos(up
);
1291 * Clear the interrupt registers.
1293 (void) serial_inp(up
, UART_LSR
);
1294 (void) serial_inp(up
, UART_RX
);
1295 (void) serial_inp(up
, UART_IIR
);
1296 (void) serial_inp(up
, UART_MSR
);
1299 * At this point, there's no way the LSR could still be 0xff;
1300 * if it is, then bail out, because there's likely no UART
1303 if (!(up
->port
.flags
& UPF_BUGGY_UART
) &&
1304 (serial_inp(up
, UART_LSR
) == 0xff)) {
1305 printk("ttyS%d: LSR safety check engaged!\n", up
->port
.line
);
1310 * For a XR16C850, we need to set the trigger levels
1312 if (up
->port
.type
== PORT_16850
) {
1315 serial_outp(up
, UART_LCR
, 0xbf);
1317 fctr
= serial_inp(up
, UART_FCTR
) & ~(UART_FCTR_RX
|UART_FCTR_TX
);
1318 serial_outp(up
, UART_FCTR
, fctr
| UART_FCTR_TRGD
| UART_FCTR_RX
);
1319 serial_outp(up
, UART_TRG
, UART_TRG_96
);
1320 serial_outp(up
, UART_FCTR
, fctr
| UART_FCTR_TRGD
| UART_FCTR_TX
);
1321 serial_outp(up
, UART_TRG
, UART_TRG_96
);
1323 serial_outp(up
, UART_LCR
, 0);
1327 * If the "interrupt" for this port doesn't correspond with any
1328 * hardware interrupt, we use a timer-based system. The original
1329 * driver used to do this with IRQ0.
1331 if (!is_real_interrupt(up
->port
.irq
)) {
1332 unsigned int timeout
= up
->port
.timeout
;
1334 timeout
= timeout
> 6 ? (timeout
/ 2 - 2) : 1;
1336 up
->timer
.data
= (unsigned long)up
;
1337 mod_timer(&up
->timer
, jiffies
+ timeout
);
1339 retval
= serial_link_irq_chain(up
);
1345 * Now, initialize the UART
1347 serial_outp(up
, UART_LCR
, UART_LCR_WLEN8
);
1349 spin_lock_irqsave(&up
->port
.lock
, flags
);
1350 if (up
->port
.flags
& UPF_FOURPORT
) {
1351 if (!is_real_interrupt(up
->port
.irq
))
1352 up
->port
.mctrl
|= TIOCM_OUT1
;
1355 * Most PC uarts need OUT2 raised to enable interrupts.
1357 if (is_real_interrupt(up
->port
.irq
))
1358 up
->port
.mctrl
|= TIOCM_OUT2
;
1360 serial8250_set_mctrl(&up
->port
, up
->port
.mctrl
);
1361 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
1362 //printk("8250 startup, iobase=0x%x, mctrl=0x%x, msr=0x%x\n", up->port.iobase, up->port.mctrl, serial_in(up, UART_MSR));
1365 * Finally, enable interrupts. Note: Modem status interrupts
1366 * are set via set_termios(), which will be occurring imminently
1367 * anyway, so we don't enable them here.
1369 up
->ier
= UART_IER_RLSI
| UART_IER_RDI
;
1370 serial_outp(up
, UART_IER
, up
->ier
);
1372 if (up
->port
.flags
& UPF_FOURPORT
) {
1375 * Enable interrupts on the AST Fourport board
1377 icp
= (up
->port
.iobase
& 0xfe0) | 0x01f;
1383 * And clear the interrupt registers again for luck.
1385 (void) serial_inp(up
, UART_LSR
);
1386 (void) serial_inp(up
, UART_RX
);
1387 (void) serial_inp(up
, UART_IIR
);
1388 (void) serial_inp(up
, UART_MSR
);
1393 static void serial8250_shutdown(struct uart_port
*port
)
1395 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
1396 unsigned long flags
;
1399 * Disable interrupts from this port
1402 serial_outp(up
, UART_IER
, 0);
1404 spin_lock_irqsave(&up
->port
.lock
, flags
);
1405 if (up
->port
.flags
& UPF_FOURPORT
) {
1406 /* reset interrupts on the AST Fourport board */
1407 inb((up
->port
.iobase
& 0xfe0) | 0x1f);
1408 up
->port
.mctrl
|= TIOCM_OUT1
;
1410 up
->port
.mctrl
&= ~TIOCM_OUT2
;
1412 serial8250_set_mctrl(&up
->port
, up
->port
.mctrl
);
1413 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
1414 //printk("8250 shutdown, iobase=0x%x, mctrl=0x%x, msr=0x%x\n", up->port.iobase, up->port.mctrl, serial_in(up, UART_MSR));
1417 * Disable break condition and FIFOs
1419 serial_out(up
, UART_LCR
, serial_inp(up
, UART_LCR
) & ~UART_LCR_SBC
);
1420 serial8250_clear_fifos(up
);
1422 #ifdef CONFIG_SERIAL_8250_RSA
1424 * Reset the RSA board back to 115kbps compat mode.
1430 * Read data port to reset things, and then unlink from
1433 (void) serial_in(up
, UART_RX
);
1435 if (!is_real_interrupt(up
->port
.irq
))
1436 del_timer_sync(&up
->timer
);
1438 serial_unlink_irq_chain(up
);
1441 static unsigned int serial8250_get_divisor(struct uart_port
*port
, unsigned int baud
)
1446 * Handle magic divisors for baud rates above baud_base on
1447 * SMSC SuperIO chips.
1449 if ((port
->flags
& UPF_MAGIC_MULTIPLIER
) &&
1450 baud
== (port
->uartclk
/4))
1452 else if ((port
->flags
& UPF_MAGIC_MULTIPLIER
) &&
1453 baud
== (port
->uartclk
/8))
1456 quot
= uart_get_divisor(port
, baud
);
1462 serial8250_set_termios(struct uart_port
*port
, struct termios
*termios
,
1463 struct termios
*old
)
1465 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
1466 unsigned char cval
, fcr
= 0;
1467 unsigned long flags
;
1468 unsigned int baud
, quot
;
1470 switch (termios
->c_cflag
& CSIZE
) {
1486 if (termios
->c_cflag
& CSTOPB
)
1488 if (termios
->c_cflag
& PARENB
)
1489 cval
|= UART_LCR_PARITY
;
1490 if (!(termios
->c_cflag
& PARODD
))
1491 cval
|= UART_LCR_EPAR
;
1493 if (termios
->c_cflag
& CMSPAR
)
1494 cval
|= UART_LCR_SPAR
;
1499 * Ask the core to calculate the divisor for us.
1501 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
1502 quot
= serial8250_get_divisor(port
, baud
);
1505 * Work around a bug in the Oxford Semiconductor 952 rev B
1506 * chip which causes it to seriously miscalculate baud rates
1509 if ((quot
& 0xff) == 0 && up
->port
.type
== PORT_16C950
&&
1513 if (up
->capabilities
& UART_CAP_FIFO
&& up
->port
.fifosize
> 1) {
1515 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_TRIGGER_1
;
1516 #ifdef CONFIG_SERIAL_8250_RSA
1517 else if (up
->port
.type
== PORT_RSA
)
1518 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_TRIGGER_14
;
1521 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_TRIGGER_8
;
1525 * TI16C750: hardware flow control and 64 byte FIFOs. When AFE is
1526 * enabled, RTS will be deasserted when the receive FIFO contains
1527 * more characters than the trigger, or the MCR RTS bit is cleared.
1529 if (up
->port
.type
== PORT_16750
) {
1530 up
->mcr
&= ~UART_MCR_AFE
;
1531 if (termios
->c_cflag
& CRTSCTS
)
1532 up
->mcr
|= UART_MCR_AFE
;
1534 fcr
|= UART_FCR7_64BYTE
;
1538 * Ok, we're now changing the port state. Do it with
1539 * interrupts disabled.
1541 spin_lock_irqsave(&up
->port
.lock
, flags
);
1544 * Update the per-port timeout.
1546 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1548 up
->port
.read_status_mask
= UART_LSR_OE
| UART_LSR_THRE
| UART_LSR_DR
;
1549 if (termios
->c_iflag
& INPCK
)
1550 up
->port
.read_status_mask
|= UART_LSR_FE
| UART_LSR_PE
;
1551 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
1552 up
->port
.read_status_mask
|= UART_LSR_BI
;
1555 * Characteres to ignore
1557 up
->port
.ignore_status_mask
= 0;
1558 if (termios
->c_iflag
& IGNPAR
)
1559 up
->port
.ignore_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
1560 if (termios
->c_iflag
& IGNBRK
) {
1561 up
->port
.ignore_status_mask
|= UART_LSR_BI
;
1563 * If we're ignoring parity and break indicators,
1564 * ignore overruns too (for real raw support).
1566 if (termios
->c_iflag
& IGNPAR
)
1567 up
->port
.ignore_status_mask
|= UART_LSR_OE
;
1571 * ignore all characters if CREAD is not set
1573 if ((termios
->c_cflag
& CREAD
) == 0)
1574 up
->port
.ignore_status_mask
|= UART_LSR_DR
;
1577 * CTS flow control flag and modem status interrupts
1579 up
->ier
&= ~UART_IER_MSI
;
1580 if (UART_ENABLE_MS(&up
->port
, termios
->c_cflag
))
1581 up
->ier
|= UART_IER_MSI
;
1582 if (up
->port
.type
== PORT_XSCALE
)
1583 up
->ier
|= UART_IER_UUE
| UART_IER_RTOIE
;
1585 serial_out(up
, UART_IER
, up
->ier
);
1587 if (up
->capabilities
& UART_CAP_EFR
) {
1588 serial_outp(up
, UART_LCR
, 0xBF);
1589 serial_outp(up
, UART_EFR
,
1590 termios
->c_cflag
& CRTSCTS
? UART_EFR_CTS
:0);
1593 if (up
->capabilities
& UART_NATSEMI
) {
1594 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
1595 serial_outp(up
, UART_LCR
, 0xe0);
1597 serial_outp(up
, UART_LCR
, cval
| UART_LCR_DLAB
);/* set DLAB */
1600 serial_outp(up
, UART_DLL
, quot
& 0xff); /* LS of divisor */
1601 serial_outp(up
, UART_DLM
, quot
>> 8); /* MS of divisor */
1604 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
1605 * is written without DLAB set, this mode will be disabled.
1607 if (up
->port
.type
== PORT_16750
)
1608 serial_outp(up
, UART_FCR
, fcr
);
1610 serial_outp(up
, UART_LCR
, cval
); /* reset DLAB */
1611 up
->lcr
= cval
; /* Save LCR */
1612 if (up
->port
.type
!= PORT_16750
) {
1613 if (fcr
& UART_FCR_ENABLE_FIFO
) {
1614 /* emulated UARTs (Lucent Venus 167x) need two steps */
1615 serial_outp(up
, UART_FCR
, UART_FCR_ENABLE_FIFO
);
1617 serial_outp(up
, UART_FCR
, fcr
); /* set fcr */
1619 serial8250_set_mctrl(&up
->port
, up
->port
.mctrl
);
1620 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
1621 //printk("8250 set_termios, iobase=0x%x, mctrl=0x%x, msr=0x%x\n", up->port.iobase, up->port.mctrl, serial_in(up, UART_MSR));
1625 serial8250_pm(struct uart_port
*port
, unsigned int state
,
1626 unsigned int oldstate
)
1628 struct uart_8250_port
*p
= (struct uart_8250_port
*)port
;
1630 serial8250_set_sleep(p
, state
!= 0);
1633 p
->pm(port
, state
, oldstate
);
1637 * Resource handling. This is complicated by the fact that resources
1638 * depend on the port type. Maybe we should be claiming the standard
1639 * 8250 ports, and then trying to get other resources as necessary?
1642 serial8250_request_std_resource(struct uart_8250_port
*up
, struct resource
**res
)
1644 unsigned int size
= 8 << up
->port
.regshift
;
1647 switch (up
->port
.iotype
) {
1649 if (up
->port
.mapbase
) {
1650 *res
= request_mem_region(up
->port
.mapbase
, size
, "serial");
1658 *res
= request_region(up
->port
.iobase
, size
, "serial");
1667 serial8250_request_rsa_resource(struct uart_8250_port
*up
, struct resource
**res
)
1669 unsigned int size
= 8 << up
->port
.regshift
;
1670 unsigned long start
;
1673 switch (up
->port
.iotype
) {
1675 if (up
->port
.mapbase
) {
1676 start
= up
->port
.mapbase
;
1677 start
+= UART_RSA_BASE
<< up
->port
.regshift
;
1678 *res
= request_mem_region(start
, size
, "serial-rsa");
1686 start
= up
->port
.iobase
;
1687 start
+= UART_RSA_BASE
<< up
->port
.regshift
;
1688 *res
= request_region(start
, size
, "serial-rsa");
1697 static void serial8250_release_port(struct uart_port
*port
)
1699 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
1700 unsigned long start
, offset
= 0, size
= 0;
1702 if (up
->port
.type
== PORT_RSA
) {
1703 offset
= UART_RSA_BASE
<< up
->port
.regshift
;
1707 size
<<= up
->port
.regshift
;
1709 switch (up
->port
.iotype
) {
1711 if (up
->port
.mapbase
) {
1715 iounmap(up
->port
.membase
);
1716 up
->port
.membase
= NULL
;
1718 start
= up
->port
.mapbase
;
1721 release_mem_region(start
+ offset
, size
);
1722 release_mem_region(start
, 8 << up
->port
.regshift
);
1728 start
= up
->port
.iobase
;
1731 release_region(start
+ offset
, size
);
1732 release_region(start
+ offset
, 8 << up
->port
.regshift
);
1740 static int serial8250_request_port(struct uart_port
*port
)
1742 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
1743 struct resource
*res
= NULL
, *res_rsa
= NULL
;
1746 if (up
->port
.type
== PORT_RSA
) {
1747 ret
= serial8250_request_rsa_resource(up
, &res_rsa
);
1752 ret
= serial8250_request_std_resource(up
, &res
);
1755 * If we have a mapbase, then request that as well.
1757 if (ret
== 0 && up
->port
.flags
& UPF_IOREMAP
) {
1758 int size
= res
->end
- res
->start
+ 1;
1760 up
->port
.membase
= ioremap(up
->port
.mapbase
, size
);
1761 if (!up
->port
.membase
)
1767 release_resource(res_rsa
);
1769 release_resource(res
);
1774 static void serial8250_config_port(struct uart_port
*port
, int flags
)
1776 struct uart_8250_port
*up
= (struct uart_8250_port
*)port
;
1777 struct resource
*res_std
= NULL
, *res_rsa
= NULL
;
1778 int probeflags
= PROBE_ANY
;
1783 * Don't probe for MCA ports on non-MCA machines.
1785 if (up
->port
.flags
& UPF_BOOT_ONLYMCA
&& !MCA_bus
)
1790 * Find the region that we can probe for. This in turn
1791 * tells us whether we can probe for the type of port.
1793 ret
= serial8250_request_std_resource(up
, &res_std
);
1797 ret
= serial8250_request_rsa_resource(up
, &res_rsa
);
1799 probeflags
&= ~PROBE_RSA
;
1801 if (flags
& UART_CONFIG_TYPE
)
1802 autoconfig(up
, probeflags
);
1803 if (up
->port
.type
!= PORT_UNKNOWN
&& flags
& UART_CONFIG_IRQ
)
1807 * If the port wasn't an RSA port, release the resource.
1809 if (up
->port
.type
!= PORT_RSA
&& res_rsa
)
1810 release_resource(res_rsa
);
1812 if (up
->port
.type
== PORT_UNKNOWN
&& res_std
)
1813 release_resource(res_std
);
1817 serial8250_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
1819 if (ser
->irq
>= NR_IRQS
|| ser
->irq
< 0 ||
1820 ser
->baud_base
< 9600 || ser
->type
< PORT_UNKNOWN
||
1821 ser
->type
> PORT_MAX_8250
|| ser
->type
== PORT_CIRRUS
||
1822 ser
->type
== PORT_STARTECH
)
1828 serial8250_type(struct uart_port
*port
)
1830 int type
= port
->type
;
1832 if (type
>= ARRAY_SIZE(uart_config
))
1834 return uart_config
[type
].name
;
1837 static struct uart_ops serial8250_pops
= {
1838 .tx_empty
= serial8250_tx_empty
,
1839 .set_mctrl
= serial8250_set_mctrl
,
1840 .get_mctrl
= serial8250_get_mctrl
,
1841 .stop_tx
= serial8250_stop_tx
,
1842 .start_tx
= serial8250_start_tx
,
1843 .stop_rx
= serial8250_stop_rx
,
1844 .enable_ms
= serial8250_enable_ms
,
1845 .break_ctl
= serial8250_break_ctl
,
1846 .startup
= serial8250_startup
,
1847 .shutdown
= serial8250_shutdown
,
1848 .set_termios
= serial8250_set_termios
,
1849 .pm
= serial8250_pm
,
1850 .type
= serial8250_type
,
1851 .release_port
= serial8250_release_port
,
1852 .request_port
= serial8250_request_port
,
1853 .config_port
= serial8250_config_port
,
1854 .verify_port
= serial8250_verify_port
,
1857 static struct uart_8250_port serial8250_ports
[UART_NR
];
1859 static void __init
serial8250_isa_init_ports(void)
1861 struct uart_8250_port
*up
;
1862 static int first
= 1;
1869 for (i
= 0, up
= serial8250_ports
; i
< ARRAY_SIZE(old_serial_port
);
1871 up
->port
.iobase
= old_serial_port
[i
].port
;
1872 up
->port
.irq
= irq_canonicalize(old_serial_port
[i
].irq
);
1873 up
->port
.uartclk
= old_serial_port
[i
].baud_base
* 16;
1874 up
->port
.flags
= old_serial_port
[i
].flags
;
1875 up
->port
.hub6
= old_serial_port
[i
].hub6
;
1876 up
->port
.membase
= old_serial_port
[i
].iomem_base
;
1877 up
->port
.iotype
= old_serial_port
[i
].io_type
;
1878 up
->port
.regshift
= old_serial_port
[i
].iomem_reg_shift
;
1879 up
->port
.ops
= &serial8250_pops
;
1881 up
->port
.flags
|= UPF_SHARE_IRQ
;
1885 static void __init
serial8250_register_ports(struct uart_driver
*drv
)
1889 serial8250_isa_init_ports();
1891 for (i
= 0; i
< UART_NR
; i
++) {
1892 struct uart_8250_port
*up
= &serial8250_ports
[i
];
1895 up
->port
.ops
= &serial8250_pops
;
1896 init_timer(&up
->timer
);
1897 up
->timer
.function
= serial8250_timeout
;
1900 * ALPHA_KLUDGE_MCR needs to be killed.
1902 up
->mcr_mask
= ~ALPHA_KLUDGE_MCR
;
1903 up
->mcr_force
= ALPHA_KLUDGE_MCR
;
1905 uart_add_one_port(drv
, &up
->port
);
1909 #ifdef CONFIG_SERIAL_8250_CONSOLE
1911 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1914 * Wait for transmitter & holding register to empty
1916 static inline void wait_for_xmitr(struct uart_8250_port
*up
)
1918 unsigned int status
, tmout
= 10000;
1920 /* Wait up to 10ms for the character(s) to be sent. */
1922 status
= serial_in(up
, UART_LSR
);
1924 if (status
& UART_LSR_BI
)
1925 up
->lsr_break_flag
= UART_LSR_BI
;
1930 } while ((status
& BOTH_EMPTY
) != BOTH_EMPTY
);
1932 /* Wait up to 1s for flow control if necessary */
1933 if (up
->port
.flags
& UPF_CONS_FLOW
) {
1936 ((serial_in(up
, UART_MSR
) & UART_MSR_CTS
) == 0))
1942 * Print a string to the serial port trying not to disturb
1943 * any possible real use of the port...
1945 * The console_lock must be held when we get here.
1948 serial8250_console_write(struct console
*co
, const char *s
, unsigned int count
)
1950 struct uart_8250_port
*up
= &serial8250_ports
[co
->index
];
1955 * First save the UER then disable the interrupts
1957 ier
= serial_in(up
, UART_IER
);
1959 if (up
->port
.type
== PORT_XSCALE
)
1960 serial_out(up
, UART_IER
, UART_IER_UUE
);
1962 serial_out(up
, UART_IER
, 0);
1965 * Now, do each character
1967 for (i
= 0; i
< count
; i
++, s
++) {
1971 * Send the character out.
1972 * If a LF, also do CR...
1974 serial_out(up
, UART_TX
, *s
);
1977 serial_out(up
, UART_TX
, 13);
1982 * Finally, wait for transmitter to become empty
1983 * and restore the IER
1986 serial_out(up
, UART_IER
, ier
);
1989 static int __init
serial8250_console_setup(struct console
*co
, char *options
)
1991 struct uart_port
*port
;
1998 * Check whether an invalid uart number has been specified, and
1999 * if so, search for the first available port that does have
2002 if (co
->index
>= UART_NR
)
2004 port
= &serial8250_ports
[co
->index
].port
;
2011 spin_lock_init(&port
->lock
);
2014 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
2016 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
2019 static struct uart_driver serial8250_reg
;
2020 static struct console serial8250_console
= {
2022 .write
= serial8250_console_write
,
2023 .device
= uart_console_device
,
2024 .setup
= serial8250_console_setup
,
2025 .flags
= CON_PRINTBUFFER
,
2027 .data
= &serial8250_reg
,
2030 static int __init
serial8250_console_init(void)
2032 serial8250_isa_init_ports();
2033 register_console(&serial8250_console
);
2036 console_initcall(serial8250_console_init
);
2038 static int __init
serial8250_late_console_init(void)
2040 if (!(serial8250_console
.flags
& CON_ENABLED
))
2041 register_console(&serial8250_console
);
2044 late_initcall(serial8250_late_console_init
);
2046 #define SERIAL8250_CONSOLE &serial8250_console
2048 #define SERIAL8250_CONSOLE NULL
2051 static struct uart_driver serial8250_reg
= {
2052 .owner
= THIS_MODULE
,
2053 .driver_name
= "serial",
2054 .devfs_name
= "tts/",
2059 .cons
= SERIAL8250_CONSOLE
,
2063 * register_serial and unregister_serial allows for 16x50 serial ports to be
2064 * configured at run-time, to support PCMCIA modems.
2067 static int __register_serial(struct serial_struct
*req
, int line
)
2069 struct uart_port port
;
2071 port
.iobase
= req
->port
;
2072 port
.membase
= req
->iomem_base
;
2073 port
.irq
= req
->irq
;
2074 port
.uartclk
= req
->baud_base
* 16;
2075 port
.fifosize
= req
->xmit_fifo_size
;
2076 port
.regshift
= req
->iomem_reg_shift
;
2077 port
.iotype
= req
->io_type
;
2078 port
.flags
= req
->flags
| UPF_BOOT_AUTOCONF
;
2079 port
.mapbase
= req
->iomap_base
;
2083 port
.flags
|= UPF_SHARE_IRQ
;
2085 if (HIGH_BITS_OFFSET
)
2086 port
.iobase
|= (long) req
->port_high
<< HIGH_BITS_OFFSET
;
2089 * If a clock rate wasn't specified by the low level
2090 * driver, then default to the standard clock rate.
2092 if (port
.uartclk
== 0)
2093 port
.uartclk
= BASE_BAUD
* 16;
2095 return uart_register_port(&serial8250_reg
, &port
);
2099 * register_serial - configure a 16x50 serial port at runtime
2100 * @req: request structure
2102 * Configure the serial port specified by the request. If the
2103 * port exists and is in use an error is returned. If the port
2104 * is not currently in the table it is added.
2106 * The port is then probed and if necessary the IRQ is autodetected
2107 * If this fails an error is returned.
2109 * On success the port is ready to use and the line number is returned.
2111 int register_serial(struct serial_struct
*req
)
2113 return __register_serial(req
, -1);
2116 int __init
early_serial_setup(struct uart_port
*port
)
2118 if (port
->line
>= ARRAY_SIZE(serial8250_ports
))
2121 serial8250_isa_init_ports();
2122 serial8250_ports
[port
->line
].port
= *port
;
2123 serial8250_ports
[port
->line
].port
.ops
= &serial8250_pops
;
2128 * unregister_serial - remove a 16x50 serial port at runtime
2129 * @line: serial line number
2131 * Remove one serial port. This may be called from interrupt
2134 void unregister_serial(int line
)
2136 uart_unregister_port(&serial8250_reg
, line
);
2140 * This is for ISAPNP only.
2142 void serial8250_get_irq_map(unsigned int *map
)
2146 for (i
= 0; i
< UART_NR
; i
++) {
2147 if (serial8250_ports
[i
].port
.type
!= PORT_UNKNOWN
&&
2148 serial8250_ports
[i
].port
.irq
< 16)
2149 *map
|= 1 << serial8250_ports
[i
].port
.irq
;
2154 * serial8250_suspend_port - suspend one serial port
2155 * @line: serial line number
2156 * @level: the level of port suspension, as per uart_suspend_port
2158 * Suspend one serial port.
2160 void serial8250_suspend_port(int line
)
2162 uart_suspend_port(&serial8250_reg
, &serial8250_ports
[line
].port
);
2166 * serial8250_resume_port - resume one serial port
2167 * @line: serial line number
2168 * @level: the level of port resumption, as per uart_resume_port
2170 * Resume one serial port.
2172 void serial8250_resume_port(int line
)
2174 uart_resume_port(&serial8250_reg
, &serial8250_ports
[line
].port
);
2177 static int __init
serial8250_init(void)
2181 printk(KERN_INFO
"Serial: 8250/16550 driver $Revision: 1.90 $ "
2182 "%d ports, IRQ sharing %sabled\n", (int) UART_NR
,
2183 share_irqs
? "en" : "dis");
2185 for (i
= 0; i
< NR_IRQS
; i
++)
2186 spin_lock_init(&irq_lists
[i
].lock
);
2188 ret
= uart_register_driver(&serial8250_reg
);
2190 serial8250_register_ports(&serial8250_reg
);
2192 #if defined(CONFIG_X86) && defined(CONFIG_SERIAL_8250_CONSOLE)
2193 serial8250_console_setup(&serial8250_console
, NULL
);
2198 static void __exit
serial8250_exit(void)
2202 for (i
= 0; i
< UART_NR
; i
++)
2203 uart_remove_one_port(&serial8250_reg
, &serial8250_ports
[i
].port
);
2205 uart_unregister_driver(&serial8250_reg
);
2208 module_init(serial8250_init
);
2209 module_exit(serial8250_exit
);
2211 EXPORT_SYMBOL(register_serial
);
2212 EXPORT_SYMBOL(unregister_serial
);
2213 EXPORT_SYMBOL(serial8250_get_irq_map
);
2214 EXPORT_SYMBOL(serial8250_suspend_port
);
2215 EXPORT_SYMBOL(serial8250_resume_port
);
2217 MODULE_LICENSE("GPL");
2218 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver $Revision: 1.90 $");
2220 module_param(share_irqs
, uint
, 0644);
2221 MODULE_PARM_DESC(share_irqs
, "Share IRQs with other non-8250/16x50 devices"
2224 #ifdef CONFIG_SERIAL_8250_RSA
2225 module_param_array(probe_rsa
, ulong
, probe_rsa_count
, 0444);
2226 MODULE_PARM_DESC(probe_rsa
, "Probe I/O ports for RSA");
2228 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR
);